diff --git a/llvm/test/tools/llvm-objcopy/ELF/Inputs/ihex-elf-segments.yaml b/llvm/test/tools/llvm-objcopy/ELF/Inputs/ihex-elf-segments.yaml --- a/llvm/test/tools/llvm-objcopy/ELF/Inputs/ihex-elf-segments.yaml +++ b/llvm/test/tools/llvm-objcopy/ELF/Inputs/ihex-elf-segments.yaml @@ -29,6 +29,12 @@ Content: "40414243" Address: 0x10100 AddressAlign: 0x8 + - Name: .empty + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + Size: 0 + Address: 0x10FFF8 + AddressAlign: 0x0 - Name: .data3 Type: SHT_PROGBITS Flags: [ SHF_ALLOC ] diff --git a/llvm/tools/llvm-objcopy/ELF/Object.h b/llvm/tools/llvm-objcopy/ELF/Object.h --- a/llvm/tools/llvm-objcopy/ELF/Object.h +++ b/llvm/tools/llvm-objcopy/ELF/Object.h @@ -368,12 +368,14 @@ BinaryWriter(Object &Obj, raw_ostream &Out) : Writer(Obj, Out) {} }; -class IHexWriter : public Writer { - struct SectionCompare { - bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const; - }; +namespace internal { +struct SectionCompare { + bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const; +}; +} // namespace internal - std::set Sections; +class IHexWriter : public Writer { + std::set Sections; size_t TotalSize = 0; Error checkSection(const SectionBase &Sec); @@ -434,18 +436,6 @@ }; class Segment { -private: - struct SectionCompare { - bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const { - // Some sections might have the same address if one of them is empty. To - // fix this we can use the lexicographic ordering on ->Addr and the - // original index. - if (Lhs->OriginalOffset == Rhs->OriginalOffset) - return Lhs->OriginalIndex < Rhs->OriginalIndex; - return Lhs->OriginalOffset < Rhs->OriginalOffset; - } - }; - public: uint32_t Type = 0; uint32_t Flags = 0; @@ -460,7 +450,7 @@ uint64_t OriginalOffset = 0; Segment *ParentSegment = nullptr; ArrayRef Contents; - std::set Sections; + std::set Sections; explicit Segment(ArrayRef Data) : Contents(Data) {} Segment() = default; diff --git a/llvm/tools/llvm-objcopy/ELF/Object.cpp b/llvm/tools/llvm-objcopy/ELF/Object.cpp --- a/llvm/tools/llvm-objcopy/ELF/Object.cpp +++ b/llvm/tools/llvm-objcopy/ELF/Object.cpp @@ -176,6 +176,18 @@ return Error::success(); } +namespace internal { +bool SectionCompare::operator()(const SectionBase *Lhs, + const SectionBase *Rhs) const { + // Some sections might have the same address if one of them is empty. To + // fix this we can use the lexicographic ordering on ->Addr and the + // original index. + if (Lhs->OriginalOffset == Rhs->OriginalOffset) + return Lhs->OriginalIndex < Rhs->OriginalIndex; + return Lhs->OriginalOffset < Rhs->OriginalOffset; +} +} // namespace internal + static bool addressOverflows32bit(uint64_t Addr) { // Sign extended 32 bit addresses (e.g 0xFFFFFFFF80000000) are ok return Addr > UINT32_MAX && Addr + 0x80000000 > UINT32_MAX; @@ -2595,12 +2607,6 @@ return Error::success(); } -bool IHexWriter::SectionCompare::operator()(const SectionBase *Lhs, - const SectionBase *Rhs) const { - return (sectionPhysicalAddr(Lhs) & 0xFFFFFFFFU) < - (sectionPhysicalAddr(Rhs) & 0xFFFFFFFFU); -} - uint64_t IHexWriter::writeEntryPointRecord(uint8_t *Buf) { IHexLineData HexData; uint8_t Data[4] = {};