Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -629,11 +629,11 @@ void LinkerScript::addOrphanSections() { unsigned End = SectionCommands.size(); StringMap Map; - std::vector V; - for (InputSectionBase *S : InputSections) { + + auto Add = [&](InputSectionBase *S) { if (!S->Live || S->Parent) - continue; + return; StringRef Name = getOutputSectionName(S); @@ -645,13 +645,24 @@ if (OutputSection *Sec = findByName(makeArrayRef(SectionCommands).slice(0, End), Name)) { Sec->addSection(cast(S)); - continue; + return; } if (OutputSection *OS = addInputSec(Map, S, Name)) V.push_back(OS); assert(S->getOutputSection()->SectionIndex == UINT32_MAX); - } + }; + + // For futher --emit-reloc handling code we need target output section + // to be created before we create relocation output section, so we want + // to create target sections first. We do not need or want delay handling + // of synthetic sections because them are special. + for (InputSectionBase *IS : InputSections) + if (isa(IS) || + (IS->Type != SHT_REL && IS->Type != SHT_RELA)) + Add(IS); + for (InputSectionBase *IS : InputSections) + Add(IS); // If no SECTIONS command was given, we should insert sections commands // before others, so that we can handle scripts which refers them, Index: test/ELF/arm-exidx-relocatable.s =================================================================== --- test/ELF/arm-exidx-relocatable.s +++ test/ELF/arm-exidx-relocatable.s @@ -57,8 +57,8 @@ // CHECK-NEXT: Link: 1 -// CHECK: Index: 4 -// CHECK-NEXT: Name: .text.f1 +// CHECK: Index: 3 +// CHECK: Name: .text.f1 // CHECK: Name: .ARM.exidx.text.f1 // CHECK-NEXT: Type: SHT_ARM_EXIDX (0x70000001) @@ -69,10 +69,10 @@ // CHECK-NEXT: Address // CHECK-NEXT: Offset: // CHECK-NEXT: Size: 8 -// CHECK-NEXT: Link: 4 +// CHECK-NEXT: Link: 3 -// CHECK: Index: 7 +// CHECK: Index: 5 // CHECK-NEXT: Name: .text.f2 // CHECK: Name: .ARM.exidx.text.f2 @@ -84,10 +84,10 @@ // CHECK-NEXT: Address // CHECK-NEXT: Offset: // CHECK-NEXT: Size: 16 -// CHECK-NEXT: Link: 7 +// CHECK-NEXT: Link: 5 -// CHECK: Index: 10 +// CHECK: Index: 7 // CHECK-NEXT: Name: .func1 // CHECK: Name: .ARM.exidx.func1 @@ -99,10 +99,10 @@ // CHECK-NEXT: Address // CHECK-NEXT: Offset: // CHECK-NEXT: Size: 8 -// CHECK-NEXT: Link: 10 +// CHECK-NEXT: Link: 7 -// CHECK: Index: 13 +// CHECK: Index: 9 // CHECK-NEXT: Name: .func2 // CHECK: Name: .ARM.exidx.func2 @@ -114,10 +114,10 @@ // CHECK-NEXT: Address // CHECK-NEXT: Offset: // CHECK-NEXT: Size: 8 -// CHECK-NEXT: Link: 13 +// CHECK-NEXT: Link: 9 -// CHECK: Index: 16 +// CHECK: Index: 11 // CHECK-NEXT: Name: .func3 // CHECK: Name: .ARM.exidx.func3 @@ -129,4 +129,4 @@ // CHECK-NEXT: Address // CHECK-NEXT: Offset: // CHECK-NEXT: Size: 8 -// CHECK-NEXT: Link: 16 +// CHECK-NEXT: Link: 11 Index: test/ELF/emit-relocs-eh-frame.s =================================================================== --- test/ELF/emit-relocs-eh-frame.s +++ test/ELF/emit-relocs-eh-frame.s @@ -0,0 +1,18 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o +# RUN: ld.lld --emit-relocs %t1.o -o %t +# RUN: llvm-readobj -r %t | FileCheck %s + +# CHECK: Relocations [ +# CHECK-NEXT: Section {{.*}} .rela.eh_frame { +# CHECK-NEXT: 0x{{.*}} R_X86_64_PC32 .text 0x0 +# CHECK-NEXT: } +# CHECK-NEXT: ] + +.text +.globl foo +foo: + .cfi_startproc + .Lfunc_end0: + .size foo, .Lfunc_end0-foo + .cfi_endproc Index: test/ELF/emit-relocs-shared.s =================================================================== --- test/ELF/emit-relocs-shared.s +++ test/ELF/emit-relocs-shared.s @@ -7,10 +7,10 @@ .quad foo # CHECK: Relocations [ -# CHECK-NEXT: Section (4) .rela.dyn { +# CHECK-NEXT: Section {{.*}} .rela.dyn { # CHECK-NEXT: 0x1000 R_X86_64_64 foo 0x0 # CHECK-NEXT: } -# CHECK-NEXT: Section (8) .rela.data { +# CHECK-NEXT: Section {{.*}} .rela.data { # CHECK-NEXT: 0x1000 R_X86_64_64 foo 0x0 # CHECK-NEXT: } # CHECK-NEXT: ] Index: test/ELF/emit-relocs.s =================================================================== --- test/ELF/emit-relocs.s +++ test/ELF/emit-relocs.s @@ -13,7 +13,7 @@ # CHECK: Section { # CHECK: Index: 2 -# CHECK-NEXT: Name: .rela.text +# CHECK: Name: .rela.text # CHECK-NEXT: Type: SHT_RELA # CHECK-NEXT: Flags [ # CHECK-NEXT: SHF_INFO_LINK Index: test/ELF/linkerscript/eh-frame-emit-relocs.s =================================================================== --- test/ELF/linkerscript/eh-frame-emit-relocs.s +++ test/ELF/linkerscript/eh-frame-emit-relocs.s @@ -0,0 +1,13 @@ +# REQUIRES: x86 +# RUN: echo "SECTIONS { .foo : { *(.eh_frame) } }" > %t.script +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +# RUN: ld.lld --emit-relocs %t.o -T %t.script -o %t +# RUN: llvm-objdump -section-headers %t | FileCheck %s + +# CHECK-NOT: eh_frame +# CHECK: .rela.foo +# CHECK-NOT: eh_frame + +.text + .cfi_startproc + .cfi_endproc Index: test/ELF/relocatable-sections.s =================================================================== --- test/ELF/relocatable-sections.s +++ test/ELF/relocatable-sections.s @@ -3,12 +3,12 @@ # RUN: ld.lld -r %t1.o -o %t # RUN: llvm-objdump -section-headers %t | FileCheck %s -# CHECK: .text -# CHECK-NEXT: .rela.text +# CHECK: .text # CHECK: .text._init -# CHECK-NEXT: .rela.text._init # CHECK: .text._fini -# CHECK-NEXT: .rela.text._fini +# CHECK: .rela.text +# CHECK: .rela.text._init +# CHECK: .rela.text._fini .globl _start _start: Index: test/ELF/relocatable.s =================================================================== --- test/ELF/relocatable.s +++ test/ELF/relocatable.s @@ -38,7 +38,7 @@ # CHECK-NEXT: ProgramHeaderCount: 0 # CHECK-NEXT: SectionHeaderEntrySize: 64 # CHECK-NEXT: SectionHeaderCount: 7 -# CHECK-NEXT: StringTableSectionIndex: 5 +# CHECK-NEXT: StringTableSectionIndex: 4 # CHECK-NEXT: } # CHECK: Relocations [