From 7cdb18a213e94971cce6ac684eb64cbd6db23e2f Mon Sep 17 00:00:00 2001 From: Sonal Santan Date: Fri, 28 Feb 2025 08:22:02 -0800 Subject: [PATCH] Store 'raw' md5sum value in note.xrt.uid (#69) * Save raw md5sum value in note.xrt.UID and not its ascii hex representation Signed-off-by: Sonal Santan * Updated md5sum gold files Signed-off-by: Sonal Santan * Use early exit oin add_text_data_section() Signed-off-by: Sonal Santan * Fix the md5sum calculation logic to resolve failures on Windows/Linux Signed-off-by: Sonal Santan * Cleanup the md5sum byte swapping code Signed-off-by: Sonal Santan --------- Signed-off-by: Sonal Santan --- src/cpp/aiebu/src/common/uid_md5.h | 46 +++++++++++---- src/cpp/aiebu/src/elf/elfwriter.cpp | 68 +++++++++++----------- src/cpp/aiebu/src/elf/elfwriter.h | 2 +- test/aie2-ctrlcode/basic/gold.md5 | 2 +- test/cpp_test/aie2ps/eff_net_coal/gold.md5 | 2 +- 5 files changed, 71 insertions(+), 49 deletions(-) diff --git a/src/cpp/aiebu/src/common/uid_md5.h b/src/cpp/aiebu/src/common/uid_md5.h index cdb6784..39df3ac 100644 --- a/src/cpp/aiebu/src/common/uid_md5.h +++ b/src/cpp/aiebu/src/common/uid_md5.h @@ -4,43 +4,65 @@ #ifndef _AIEBU_COMMON_UID_MD5_H_ #define _AIEBU_COMMON_UID_MD5_H_ +#include #include #include namespace aiebu { +unsigned constexpr md5_size = 16; + class uid_md5 { - boost::uuids::detail::md5 hasher; + boost::uuids::detail::md5 hasher; + std::vector sig = std::vector(md5_size, 0); public: - uid_md5() - { - } + uid_md5() = default; void update(const std::vector& data) { - hasher.process_bytes(data.data(), data.size()); + hasher.process_bytes(data.data(), data.size()); } - std::string calculate() + const std::vector& calculate() { // Creating local copy of context, so calculate() return same md5sum on every call. boost::uuids::detail::md5 hasher_copy = hasher; boost::uuids::detail::md5::digest_type digest; hasher_copy.get_digest(digest); + std::memcpy(sig.data(), digest, md5_size); + + constexpr size_t element_size = sizeof(digest[0]); + + if (element_size == 1) + return sig; - std::stringstream md5; // Different boost versions model digest_type differently: // 1. typedef unsigned int(digest_type)[4]; // 2. typedef unsigned char digest_type[16]; - // The code sets the print width to number of chars needed to print the element type - // used by digest_type. This results in the same string representation for both cases - // which matches with that reported by command line md5sum utility + // For case 1. the following code swaps the bytes of each integer in + // the signature. This solves the little endian issue of integer bytes + // stored in the reverse order than in which they are printed. + + auto tcurr = sig.begin(); + auto done = sig.end(); + while (tcurr < done) { + auto tend = tcurr + element_size; + std::reverse(tcurr, tend); + tcurr = tend; + } + + return sig; + } + + [[nodiscard]] std::string str() const { + std::stringstream md5; md5 << std::hex << std::setfill('0'); - for (auto ele : digest) { - md5 << std::setw(sizeof(ele) * 2) << (unsigned int)ele; + for (auto ele : sig) { + auto c = (unsigned char)ele; + md5 << std::setw(sizeof(ele) * 2) << (unsigned int)c; } return md5.str(); } diff --git a/src/cpp/aiebu/src/elf/elfwriter.cpp b/src/cpp/aiebu/src/elf/elfwriter.cpp index 84ca415..574948e 100644 --- a/src/cpp/aiebu/src/elf/elfwriter.cpp +++ b/src/cpp/aiebu/src/elf/elfwriter.cpp @@ -150,22 +150,22 @@ add_dynamic_section_segment() void elf_writer:: -add_note(ELFIO::Elf_Word type, const std::string& name, const std::string& dec) +add_note(ELFIO::Elf_Word type, const std::string& name, const std::vector& dec) { ELFIO::section* note_sec = m_elfio.sections.add( name.c_str() ); note_sec->set_type( ELFIO::SHT_NOTE ); note_sec->set_addr_align( 1 ); ELFIO::note_section_accessor note_writer( m_elfio, note_sec ); - note_writer.add_note( type, "XRT", dec.c_str(), dec.size() ); + note_writer.add_note( type, "XRT", dec.data(), dec.size() ); } std::vector elf_writer:: finalize() { - std::cout << "UID:" << m_uid.calculate() << "\n"; add_note(NT_XRT_UID, ".note.xrt.UID", m_uid.calculate()); + std::cout << "UID:" << m_uid.str() << "\n"; std::stringstream stream; stream << std::noskipws; //m_elfio.save( "hello_32" ); @@ -183,38 +183,38 @@ add_text_data_section(std::vector& mwriter, std::vector& syms) { for(auto buffer : mwriter) { - if(buffer.get_data().size()) + if (buffer.get_data().size() == 0) + continue; + + m_uid.update(buffer.get_data()); + elf_section sec_data; + sec_data.set_name(buffer.get_name()); + sec_data.set_type(ELFIO::SHT_PROGBITS); + if (buffer.get_type() == code_section::text) + sec_data.set_flags(ELFIO::SHF_ALLOC | ELFIO::SHF_EXECINSTR); + else + sec_data.set_flags(ELFIO::SHF_ALLOC | ELFIO::SHF_WRITE); + sec_data.set_align(align); + sec_data.set_buffer(buffer.get_data()); + sec_data.set_link(""); + + elf_segment seg_data; + seg_data.set_type(ELFIO::PT_LOAD); + if (buffer.get_type() == code_section::text) + seg_data.set_flags(ELFIO::PF_X | ELFIO::PF_R); + else + seg_data.set_flags(ELFIO::PF_W | ELFIO::PF_R); + seg_data.set_vaddr(0x0); + seg_data.set_paddr(0x0); + seg_data.set_link(buffer.get_name()); + seg_data.set_align(text_align); + + add_section(sec_data); + add_segment(seg_data); + if (buffer.hassymbols()) { - m_uid.update(buffer.get_data()); - elf_section sec_data; - sec_data.set_name(buffer.get_name()); - sec_data.set_type(ELFIO::SHT_PROGBITS); - if (buffer.get_type() == code_section::text) - sec_data.set_flags(ELFIO::SHF_ALLOC | ELFIO::SHF_EXECINSTR); - else - sec_data.set_flags(ELFIO::SHF_ALLOC | ELFIO::SHF_WRITE); - sec_data.set_align(align); - sec_data.set_buffer(buffer.get_data()); - sec_data.set_link(""); - - elf_segment seg_data; - seg_data.set_type(ELFIO::PT_LOAD); - if (buffer.get_type() == code_section::text) - seg_data.set_flags(ELFIO::PF_X | ELFIO::PF_R); - else - seg_data.set_flags(ELFIO::PF_W | ELFIO::PF_R); - seg_data.set_vaddr(0x0); - seg_data.set_paddr(0x0); - seg_data.set_link(buffer.get_name()); - seg_data.set_align(text_align); - - add_section(sec_data); - add_segment(seg_data); - if (buffer.hassymbols()) - { - auto lsyms = buffer.get_symbols(); - syms.insert(syms.end(), lsyms.begin(), lsyms.end()); - } + auto lsyms = buffer.get_symbols(); + syms.insert(syms.end(), lsyms.begin(), lsyms.end()); } } } diff --git a/src/cpp/aiebu/src/elf/elfwriter.h b/src/cpp/aiebu/src/elf/elfwriter.h index 7fab205..7d91cb0 100644 --- a/src/cpp/aiebu/src/elf/elfwriter.h +++ b/src/cpp/aiebu/src/elf/elfwriter.h @@ -83,7 +83,7 @@ class elf_writer void add_dynamic_section_segment(); std::vector finalize(); void add_text_data_section(std::vector& mwriter, std::vector& syms); - void add_note(ELFIO::Elf_Word type, const std::string& name, const std::string& dec); + void add_note(ELFIO::Elf_Word type, const std::string& name, const std::vector& dec); public: diff --git a/test/aie2-ctrlcode/basic/gold.md5 b/test/aie2-ctrlcode/basic/gold.md5 index a0f5edb..a981919 100644 --- a/test/aie2-ctrlcode/basic/gold.md5 +++ b/test/aie2-ctrlcode/basic/gold.md5 @@ -1 +1 @@ -8bcab5baf77209dba2a124383324ac3c \ No newline at end of file +2845df67bdf17ba52829164da1aab533 diff --git a/test/cpp_test/aie2ps/eff_net_coal/gold.md5 b/test/cpp_test/aie2ps/eff_net_coal/gold.md5 index 83bdb3f..ea2002d 100644 --- a/test/cpp_test/aie2ps/eff_net_coal/gold.md5 +++ b/test/cpp_test/aie2ps/eff_net_coal/gold.md5 @@ -1 +1 @@ -d43f2a43f5175300eeee04e84366b559 +97845ba07a6739ddac7a3767250e5072