Index: ELF/InputFiles.cpp =================================================================== --- ELF/InputFiles.cpp +++ ELF/InputFiles.cpp @@ -256,6 +256,23 @@ Sections[I] = createInputSection(Sec); } } + // .ARM.exidx sections have a reverse dependency on the InputSection they + // have a SHF_LINK_ORDER dependency, this is identified by the sh_link. + if (Config->EMachine == EM_ARM) { + I = -1; + for (const Elf_Shdr &Sec : Obj.sections()) { + ++I; + if (Sections[I] == &InputSection::Discarded) + continue; + if (Sec.sh_flags & SHF_LINK_ORDER) { + if (Sec.sh_link >= Sections.size()) + fatal(getFilename(this) + ": invalid sh_link index: " + + Twine(Sec.sh_link)); + auto *IS = dyn_cast>(Sections[Sec.sh_link]); + IS->dependentSection = Sections[I]; + } + } + } } template Index: ELF/InputSection.h =================================================================== --- ELF/InputSection.h +++ ELF/InputSection.h @@ -228,6 +228,9 @@ // to. The writer sets a value. uint64_t OutSecOff = 0; + // InputSection that is dependent on us (reverse dependency for GC) + InputSectionBase *dependentSection = nullptr; + static bool classof(const InputSectionBase *S); InputSectionBase *getRelocatedSection(); Index: ELF/MarkLive.cpp =================================================================== --- ELF/MarkLive.cpp +++ ELF/MarkLive.cpp @@ -91,6 +91,8 @@ Fn(resolveReloc(Sec, Rel)); } } + if (Sec.dependentSection) + Fn({Sec.dependentSection, 0}); } // The .eh_frame section is an unfortunate special case. Index: test/ELF/arm-exidx-gc.s =================================================================== --- /dev/null +++ test/ELF/arm-exidx-gc.s @@ -0,0 +1,153 @@ +// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t +// RUN: ld.lld %t -o %t2 --gc-sections 2>&1 +// RUN: llvm-objdump -d -triple=armv7a-none-linux-gnueabi %t2 | FileCheck %s +// RUN: llvm-objdump -s -triple=armv7a-none-linux-gnueabi %t2 | FileCheck -check-prefix=CHECK-EXIDX %s +// REQUIRES: arm + +// Test the behavior of .ARM.exidx sections under garbage collection +// A .ARM.exidx section is live if it has a relocation to a live executable +// section. +// A .ARM.exidx section may have a relocation to a .ARM.extab section, if the +// .ARM.exidx is live then the .ARM.extab section is live + + .syntax unified + .section .text.func1, "ax",%progbits + .align 2 + .global func1 + .type func1, %function +func1: + .fnstart + bx lr + .save {r7, lr} + .setfp r7, sp, #0 + .fnend + + .section .text.unusedfunc1, "ax",%progbits + .align 2 + .global unusedfunc1 + .type unusedfunc1, %function +unusedfunc1: + .fnstart + bx lr + .save {r7, lr} + .setfp r7, sp, #0 + .fnend + + // Unwinding instructions for .text2 too large for an inline entry ARM.exidx + // entry. A separate .ARM.extab section is created to hold the unwind entries + // The .ARM.exidx table entry has a reference to the .ARM.extab section. + .section .text.func2, "ax",%progbits + .align 2 + .global func2 + .type func2, %function +func2: + .fnstart + .save {r7, lr} + .setfp r7, sp + bx lr + .personality __gxx_personality_v0 + .handlerdata + .align 2 + .byte 0xff + .byte 0x90 + .uleb128 0x16 + .byte 0x1 + .uleb128 0x32 + .uleb128 0 + .byte 0x1 + .byte 0 + .section .text.func2 + .fnend + + // An unused function with a reference to a .ARM.extab section. Both should + // be removed by gc. + .section .text.unusedfunc2, "ax",%progbits + .align 2 + .global unusedfunc2 + .type unusedfunc2, %function +unusedfunc2: + .fnstart + .save {r7, lr} + .setfp r7, sp + bx lr + .personality __gxx_personality_v0 + .handlerdata + .align 2 + .byte 0xff + .byte 0x90 + .uleb128 0x16 + .byte 0x1 + .uleb128 0x32 + .uleb128 0 + .byte 0x1 + .byte 0 + .section .text.unusedfunc2 + .fnend + + // Dummy implementation of personality routines to satisfy reference from + // exception tables + .section .text.__gcc_personality_v0, "ax", %progbits + .global __gxx_personality_v0 + .type __gxx_personality_v0, %function +__gxx_personality_v0: + .fnstart + bx lr + .cantunwind + .fnend + + .section .text.__aeabi_unwind_cpp_pr0, "ax", %progbits + .global __aeabi_unwind_cpp_pr0 + .type __aeabi_unwind_cpp_pr0, %function +__aeabi_unwind_cpp_pr0: + .fnstart + bx lr + .cantunwind + .fnend + + .text + .global _start + .type _start, %function +_start: + .fnstart + .cantunwind + bl func1 + bl func2 + bx lr + .fnend + +// GC should have only removed unusedfunc1 and unusedfunc2 the personality +// routines are kept alive by references from live .ARM.exidx and .ARM.extab +// sections +// CHECK: Disassembly of section .text: +// CHECK-NEXT: _start: +// CHECK-NEXT: 11000: 01 00 00 eb bl #4 +// CHECK-NEXT: 11004: 01 00 00 eb bl #4 +// CHECK-NEXT: 11008: 1e ff 2f e1 bx lr +// CHECK: func1: +// CHECK-NEXT: 1100c: 1e ff 2f e1 bx lr +// CHECK: func2: +// CHECK-NEXT: 11010: 1e ff 2f e1 bx lr +// CHECK: __gxx_personality_v0: +// CHECK-NEXT: 11014: 1e ff 2f e1 bx lr +// CHECK: __aeabi_unwind_cpp_pr0: +// CHECK-NEXT: 11018: 1e ff 2f e1 bx lr + +// CHECK-NOT: unusedfunc1 +// CHECK-NOT: unusedfunc2 + +// GC should have removed table entries for unusedfunc1 and unusedfunc2 +// CHECK-EXIDX: Contents of section .ARM.exidx: +// 100d4 + f2c = 11000 = _start (cantunwind) +// 100dc + f30 = 1100c = func1 (Inline unwind 80978408) +// CHECK-EXIDX-NEXT: 100d4 2c0f0000 01000000 300f0000 08849780 +// 100e4 + f2c = 11010 = func2 (100e8 + 14 = 100fc = extab entry for func2) +// 100ec + f28 = 11014 = __gxx_personality_v0 +// CHECK-EXIDX-NEXT: 100e4 2c0f0000 14000000 280f0000 01000000 +// 100f4 + f24 = 11018 = __aeabi_unwind_cpp_pr0 +// CHECK-EXIDX-NEXT: 100f4 240f0000 01000000 + + +// CHECK-EXIDX: Contents of section .ARM.extab.text.func2: +// 100fc + f18 = 11018 = __aeabi_unwind_cpp_pr0 (cantunwind) +// CHECK-EXIDX-NEXT: 100fc 180f0000 08849700 ff901601 32000100 +// CHECK-EXIDX-NOT: Contents of section .ARM.extab.text.unusedfunc2: