Skip to content

Commit 055906e

Browse files
committedMay 20, 2019
[ELF] -z combreloc: sort dynamic relocations by (!is_relative,symbol_index,r_offset)
We currently sort dynamic relocations by (!is_relative,symbol_index). Add r_offset as the third key. This makes `readelf -r` debugging easier (relocations to the same symbol are ordered by r_offset). Refactor the test combreloc.s (renamed from combrelocs.s) to check R_X86_64_RELATIVE, and delete --expand-relocs. The difference from the reverted D61477 is that we keep !is_relative as the first key. In local dynamic TLS model, DTPMOD (e.g. R_ARM_TLS_DTPMOD32 R_X86_64_DTPMOD and R_PPC{,64}_DTPMOD) may use 0 as the symbol index. Reviewed By: grimar Differential Revision: https://reviews.llvm.org/D62141 llvm-svn: 361164
1 parent 1e593f3 commit 055906e

File tree

4 files changed

+58
-105
lines changed

4 files changed

+58
-105
lines changed
 

‎lld/ELF/SyntheticSections.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -1488,17 +1488,17 @@ RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)
14881488
this->Entsize = Config->IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel);
14891489
}
14901490

1491-
static bool compRelocations(const DynamicReloc &A, const DynamicReloc &B) {
1492-
bool AIsRel = A.Type == Target->RelativeRel;
1493-
bool BIsRel = B.Type == Target->RelativeRel;
1494-
if (AIsRel != BIsRel)
1495-
return AIsRel;
1496-
return A.getSymIndex() < B.getSymIndex();
1497-
}
1498-
14991491
template <class ELFT> void RelocationSection<ELFT>::writeTo(uint8_t *Buf) {
1492+
// Sort by (!IsRelative,SymIndex,r_offset). DT_REL[A]COUNT requires us to
1493+
// place R_*_RELATIVE first. SymIndex is to improve locality, while r_offset
1494+
// is to make results easier to read.
15001495
if (Sort)
1501-
llvm::stable_sort(Relocs, compRelocations);
1496+
llvm::stable_sort(Relocs, [](const DynamicReloc &A, const DynamicReloc &B) {
1497+
return std::make_tuple(A.Type != Target->RelativeRel, A.getSymIndex(),
1498+
A.getOffset()) <
1499+
std::make_tuple(B.Type != Target->RelativeRel, B.getSymIndex(),
1500+
B.getOffset());
1501+
});
15021502

15031503
for (const DynamicReloc &Rel : Relocs) {
15041504
encodeDynamicReloc<ELFT>(reinterpret_cast<Elf_Rela *>(Buf), Rel);

‎lld/test/ELF/combreloc.s

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# REQUIRES: x86
2+
3+
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
4+
# RUN: echo 'mov aaa@gotpcrel(%rip), %rax' | llvm-mc -filetype=obj -triple=x86_64 - -o %t1.o
5+
6+
# RUN: ld.lld -shared %t.o %t1.o -o %t.so
7+
# RUN: llvm-readobj -r --dynamic-table %t.so | FileCheck %s
8+
# RUN: ld.lld -shared %t.o %t1.o -o %t.so -z combreloc
9+
# RUN: llvm-readobj -r --dynamic-table %t.so | FileCheck %s
10+
11+
# -z combreloc is the default: sort relocations by (!IsRelative,SymIndex,r_offset),
12+
# and emit DT_RELACOUNT (except on MIPS) to indicate the number of relative
13+
# relocations.
14+
15+
# CHECK: Relocations [
16+
# CHECK-NEXT: Section ({{.*}}) .rela.dyn {
17+
# CHECK-NEXT: 0x3020 R_X86_64_RELATIVE - 0x3028
18+
# CHECK-NEXT: 0x20B0 R_X86_64_GLOB_DAT aaa 0x0
19+
# CHECK-NEXT: 0x3000 R_X86_64_64 aaa 0x0
20+
# CHECK-NEXT: 0x3018 R_X86_64_64 aaa 0x0
21+
# CHECK-NEXT: 0x3010 R_X86_64_64 bbb 0x0
22+
# CHECK-NEXT: 0x3008 R_X86_64_64 ccc 0x0
23+
# CHECK-NEXT: }
24+
# CHECK: DynamicSection [
25+
# CHECK: RELACOUNT 1
26+
27+
# RUN: ld.lld -z nocombreloc -shared %t.o %t1.o -o %t.so
28+
# RUN: llvm-readobj -r --dynamic-table %t.so | FileCheck --check-prefix=NOCOMB %s
29+
30+
# NOCOMB: Relocations [
31+
# NOCOMB-NEXT: Section ({{.*}}) .rela.dyn {
32+
# NOCOMB-NEXT: 0x3000 R_X86_64_64 aaa 0x0
33+
# NOCOMB-NEXT: 0x3008 R_X86_64_64 ccc 0x0
34+
# NOCOMB-NEXT: 0x3010 R_X86_64_64 bbb 0x0
35+
# NOCOMB-NEXT: 0x3018 R_X86_64_64 aaa 0x0
36+
# NOCOMB-NEXT: 0x3020 R_X86_64_RELATIVE - 0x3028
37+
# NOCOMB-NEXT: 0x20A0 R_X86_64_GLOB_DAT aaa 0x0
38+
# NOCOMB-NEXT: }
39+
# NOCOMB: DynamicSection [
40+
# NOCOMB-NOT: RELACOUNT
41+
42+
.data
43+
.quad aaa
44+
.quad ccc
45+
.quad bbb
46+
.quad aaa
47+
.quad relative
48+
relative:

‎lld/test/ELF/combrelocs.s

-95
This file was deleted.

‎lld/test/ELF/i386-got-and-copy.s

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
# CHECK: Relocations [
1616
# CHECK-NEXT: Section (4) .rel.dyn {
17-
# CHECK-NEXT: 0x{{[0-9A-F]+}} R_386_COPY foo
1817
# CHECK-NEXT: 0x{{[0-9A-F]+}} R_386_GLOB_DAT foo
18+
# CHECK-NEXT: 0x{{[0-9A-F]+}} R_386_COPY foo
1919
# CHECK-NEXT: }
2020
# CHECK-NEXT: ]
2121

0 commit comments

Comments
 (0)
Please sign in to comment.