Don't sort sections in -r links.

Unlike gold, bfd, gas or MC we were putting exidx sections first since
they are ro.

The spec doesn't explicitly say that they must come after, but it is
definitely more convenient for the consumer, matches other producers
and matches other areas in ELF (like SHT_GROUP) where sections are
ordered in a natural way.

llvm-svn: 286659
This commit is contained in:
Rafael Espindola 2016-11-11 22:43:27 +00:00
parent be15addffe
commit 1960bcd710
5 changed files with 47 additions and 26 deletions

View File

@ -778,6 +778,10 @@ static bool canSharePtLoad(const OutputSectionBase &S1,
}
template <class ELFT> void Writer<ELFT>::sortSections() {
// Don't sort if using -r. It is not necessary and we want to preserve the
// relative order for SHF_LINK_ORDER sections.
if (Config->Relocatable)
return;
if (!ScriptConfig->HasSections) {
std::stable_sort(OutputSections.begin(), OutputSections.end(),
compareSectionsNonScript<ELFT>);

View File

@ -42,6 +42,9 @@ f3:
.cantunwind
.fnend
// CHECK: Index: 1
// CHECK-NEXT: Name: .text
// CHECK: Name: .ARM.exidx
// CHECK-NEXT: Type: SHT_ARM_EXIDX (0x70000001)
// CHECK-NEXT: Flags [ (0x82)
@ -51,7 +54,11 @@ f3:
// CHECK-NEXT: Address
// CHECK-NEXT: Offset:
// CHECK-NEXT: Size: 24
// CHECK-NEXT: Link: 7
// CHECK-NEXT: Link: 1
// CHECK: Index: 4
// CHECK-NEXT: Name: .text.f1
// CHECK: Name: .ARM.exidx.text.f1
// CHECK-NEXT: Type: SHT_ARM_EXIDX (0x70000001)
@ -62,7 +69,11 @@ f3:
// CHECK-NEXT: Address
// CHECK-NEXT: Offset:
// CHECK-NEXT: Size: 8
// CHECK-NEXT: Link: 8
// CHECK-NEXT: Link: 4
// CHECK: Index: 7
// CHECK-NEXT: Name: .text.f2
// CHECK: Name: .ARM.exidx.text.f2
// CHECK-NEXT: Type: SHT_ARM_EXIDX (0x70000001)
@ -73,7 +84,11 @@ f3:
// CHECK-NEXT: Address
// CHECK-NEXT: Offset:
// CHECK-NEXT: Size: 16
// CHECK-NEXT: Link: 9
// CHECK-NEXT: Link: 7
// CHECK: Index: 10
// CHECK-NEXT: Name: .func1
// CHECK: Name: .ARM.exidx.func1
// CHECK-NEXT: Type: SHT_ARM_EXIDX (0x70000001)
@ -86,6 +101,10 @@ f3:
// CHECK-NEXT: Size: 8
// CHECK-NEXT: Link: 10
// CHECK: Index: 13
// CHECK-NEXT: Name: .func2
// CHECK: Name: .ARM.exidx.func2
// CHECK-NEXT: Type: SHT_ARM_EXIDX (0x70000001)
// CHECK-NEXT: Flags [ (0x82)
@ -95,7 +114,11 @@ f3:
// CHECK-NEXT: Address
// CHECK-NEXT: Offset:
// CHECK-NEXT: Size: 8
// CHECK-NEXT: Link: 11
// CHECK-NEXT: Link: 13
// CHECK: Index: 16
// CHECK-NEXT: Name: .func3
// CHECK: Name: .ARM.exidx.func3
// CHECK-NEXT: Type: SHT_ARM_EXIDX (0x70000001)
@ -106,22 +129,4 @@ f3:
// CHECK-NEXT: Address
// CHECK-NEXT: Offset:
// CHECK-NEXT: Size: 8
// CHECK-NEXT: Link: 12
// CHECK: Index: 7
// CHECK-NEXT: Name: .text
// CHECK: Index: 8
// CHECK-NEXT: Name: .text.f1
// CHECK: Index: 9
// CHECK-NEXT: Name: .text.f2
// CHECK: Index: 10
// CHECK-NEXT: Name: .func1
// CHECK: Index: 11
// CHECK-NEXT: Name: .func2
// CHECK: Index: 12
// CHECK-NEXT: Name: .func3
// CHECK-NEXT: Link: 16

View File

@ -0,0 +1,12 @@
// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t.o
// RUN: ld.lld -r %t.o -o %t2.o
// RUN: ld.lld -shared %t2.o -o %t.so
// We used to crash using the output of -r because of the relative order of
// SHF_LINK_ORDER sections.
// That can be fixed by changing -r or making the regular link more flexible,
// so this is an end to end test.
.fnstart
.fnend

View File

@ -3,10 +3,10 @@
# RUN: llvm-objdump -section-headers %t | FileCheck %s
# CHECK: .text
# CHECK-NEXT: .text._init
# CHECK-NEXT: .text._fini
# CHECK-NEXT: .rela.text
# CHECK: .text._init
# CHECK-NEXT: .rela.text._init
# CHECK: .text._fini
# CHECK-NEXT: .rela.text._fini
.globl _start

View File

@ -42,7 +42,7 @@
# CHECK-NEXT: }
# CHECK: Relocations [
# CHECK-NEXT: Section (3) .rela.text {
# CHECK-NEXT: Section ({{.*}}) .rela.text {
# CHECK-NEXT: 0x3 R_X86_64_32S x 0x0
# CHECK-NEXT: 0xE R_X86_64_32S y 0x0
# CHECK-NEXT: 0x23 R_X86_64_32S xx 0x0