Index: llvm/test/tools/obj2yaml/ELF/offset.yaml =================================================================== --- llvm/test/tools/obj2yaml/ELF/offset.yaml +++ llvm/test/tools/obj2yaml/ELF/offset.yaml @@ -81,6 +81,24 @@ Type: SHT_PROGBITS AddressAlign: 0x10 Offset: 0x200 +SectionHeaderTable: + Sections: +## By default we have the same order of sections as defined by the "Sections" key. + - Name: [[SEC1=.foo1]] + - Name: [[SEC2=.foo2]] + - Name: [[SEC3=.foo3]] + - Name: [[SEC4=.bar1]] + - Name: [[SEC5=.bar2]] + - Name: [[SEC6=.bar3]] + - Name: .strtab + - Name: .shstrtab + +## In this case we change the order of sections in the section header table. +## Check that we still dump offsets correctly. + +# RUN: yaml2obj %s -DSEC1=.bar3 -DSEC2=.bar2 -DSEC3=.bar1 \ +# RUN: -DSEC4=.foo3 -DSEC5=.foo2 -DSEC6=.foo1 -o %t1-sechdr.o +# RUN: obj2yaml %t1-sechdr.o | FileCheck --check-prefix=BASIC %s ## Show we dump the "Offset" key for the first section when ## it has a non expected file offset. Index: llvm/test/tools/obj2yaml/ELF/section-headers.yaml =================================================================== --- /dev/null +++ llvm/test/tools/obj2yaml/ELF/section-headers.yaml @@ -0,0 +1,60 @@ +## Test how we dump objects where the order of sections in the +## section header table does not match their actual order. + +## Check we sort sections by their file offsets when dumping them. + +# RUN: yaml2obj %s -o %t.o +# RUN: obj2yaml %t.o | FileCheck %s + +# CHECK: --- !ELF +# CHECK-NEXT: FileHeader: +# CHECK-NEXT: Class: ELFCLASS64 +# CHECK-NEXT: Data: ELFDATA2LSB +# CHECK-NEXT: Type: ET_EXEC +# CHECK-NEXT: Sections: +# CHECK-NEXT: - Name: .foo +# CHECK-NEXT: Type: SHT_PROGBITS +# CHECK-NEXT: Flags: [ SHF_ALLOC ] +# CHECK-NEXT: Offset: 0x0000000000000300 +# CHECK-NEXT: Content: '00' +# CHECK-NEXT: - Name: .bar +# CHECK-NEXT: Type: SHT_PROGBITS +# CHECK-NEXT: Flags: [ SHF_ALLOC ] +# CHECK-NEXT: Address: 0x0000000000000001 +# CHECK-NEXT: Offset: 0x0000000000000100 +# CHECK-NEXT: Content: '0000' +# CHECK-NEXT: - Name: .zed +# CHECK-NEXT: Type: SHT_PROGBITS +# CHECK-NEXT: Flags: [ SHF_ALLOC ] +# CHECK-NEXT: Address: 0x0000000000000003 +# CHECK-NEXT: Offset: 0x0000000000000200 +# CHECK-NEXT: Content: '000000' + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC +Sections: + - Name: .foo + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + Size: 0x1 + Offset: 0x100 + - Name: .bar + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + Size: 0x2 + Offset: 0x200 + - Name: .zed + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] + Size: 0x3 + Offset: 0x300 +SectionHeaderTable: + Sections: + - Name: .zed + - Name: .foo + - Name: .bar + - Name: .strtab + - Name: .shstrtab Index: llvm/tools/obj2yaml/elf2yaml.cpp =================================================================== --- llvm/tools/obj2yaml/elf2yaml.cpp +++ llvm/tools/obj2yaml/elf2yaml.cpp @@ -577,6 +577,14 @@ return std::move(E); } + // Sometimes the order of sections in the section header table does not match + // their actual order. Here we sort sections by the file offset. + llvm::stable_sort(Ret, [&](const std::unique_ptr &A, + const std::unique_ptr &B) { + return Sections[cast(A.get())->OriginalSecNdx].sh_offset < + Sections[cast(B.get())->OriginalSecNdx].sh_offset; + }); + setOffsets(Obj.getHeader(), Ret, Sections); return std::move(Ret); }