Skip to content

Commit

Permalink
Add DT_REL(A)COUNT tag to .dynamic section
Browse files Browse the repository at this point in the history
This patch groups relative relocations in a single block
in combrelocs mode and adds DT_RELCOUNT or DT_RELACOUNT
tag to .dynamic section

Differential revision: https://reviews.llvm.org/D23661

llvm-svn: 280210
  • Loading branch information
eleviant777 committed Aug 31, 2016
1 parent 9503f6d commit aa49819
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 47 deletions.
11 changes: 11 additions & 0 deletions lld/ELF/OutputSections.cpp
Expand Up @@ -354,11 +354,18 @@ RelocationSection<ELFT>::RelocationSection(StringRef Name, bool Sort)

template <class ELFT>
void RelocationSection<ELFT>::addReloc(const DynamicReloc<ELFT> &Reloc) {
if (Reloc.Type == Target->RelativeRel)
++NumRelativeRelocs;
Relocs.push_back(Reloc);
}

template <class ELFT, class RelTy>
static bool compRelocations(const RelTy &A, const RelTy &B) {
bool AIsRel = A.getType(Config->Mips64EL) == Target->RelativeRel;
bool BIsRel = B.getType(Config->Mips64EL) == Target->RelativeRel;
if (AIsRel != BIsRel)
return AIsRel;

return A.getSymbol(Config->Mips64EL) < B.getSymbol(Config->Mips64EL);
}

Expand Down Expand Up @@ -661,6 +668,10 @@ template <class ELFT> void DynamicSection<ELFT>::finalize() {
Add({IsRela ? DT_RELASZ : DT_RELSZ, Out<ELFT>::RelaDyn->getSize()});
Add({IsRela ? DT_RELAENT : DT_RELENT,
uintX_t(IsRela ? sizeof(Elf_Rela) : sizeof(Elf_Rel))});

size_t NumRelativeRels = Out<ELFT>::RelaDyn->getRelativeRelocCount();
if (Config->ZCombreloc && NumRelativeRels)
Add({IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels});
}
if (Out<ELFT>::RelaPlt && Out<ELFT>::RelaPlt->hasRelocs()) {
Add({DT_JMPREL, Out<ELFT>::RelaPlt});
Expand Down
2 changes: 2 additions & 0 deletions lld/ELF/OutputSections.h
Expand Up @@ -377,10 +377,12 @@ class RelocationSection final : public OutputSectionBase<ELFT> {
void writeTo(uint8_t *Buf) override;
bool hasRelocs() const { return !Relocs.empty(); }
typename Base::Kind getKind() const override { return Base::Reloc; }
size_t getRelativeRelocCount() const { return NumRelativeRelocs; }
static bool classof(const Base *B) { return B->getKind() == Base::Reloc; }

private:
bool Sort;
size_t NumRelativeRelocs = 0;
std::vector<DynamicReloc<ELFT>> Relocs;
};

Expand Down
34 changes: 20 additions & 14 deletions lld/test/ELF/gotpc-relax-nopic.s
Expand Up @@ -26,7 +26,7 @@
# DISASM-NEXT: 11038: 49 f7 c7 00 20 01 00 testq $73728, %r15

# RUN: ld.lld -shared %t.o -o %t2
# RUN: llvm-readobj -s %t2 | FileCheck --check-prefix=SEC-PIC %s
# RUN: llvm-readobj -s -r -d %t2 | FileCheck --check-prefix=SEC-PIC %s
# RUN: llvm-objdump -d %t2 | FileCheck --check-prefix=DISASM-PIC %s
# SEC-PIC: Section {
# SEC-PIC: Index:
Expand All @@ -36,29 +36,35 @@
# SEC-PIC-NEXT: SHF_ALLOC
# SEC-PIC-NEXT: SHF_WRITE
# SEC-PIC-NEXT: ]
# SEC-PIC-NEXT: Address: 0x2090
# SEC-PIC-NEXT: Offset: 0x2090
# SEC-PIC-NEXT: Address: 0x20A0
# SEC-PIC-NEXT: Offset: 0x20A0
# SEC-PIC-NEXT: Size: 8
# SEC-PIC-NEXT: Link:
# SEC-PIC-NEXT: Info:
# SEC-PIC-NEXT: AddressAlignment:
# SEC-PIC-NEXT: EntrySize:
# SEC-PIC-NEXT: }
# SEC-PIC: Relocations [
# SEC-PIC-NEXT: Section ({{.*}}) .rela.dyn {
# SEC-PIC-NEXT: 0x20A0 R_X86_64_RELATIVE - 0x3000
# SEC-PIC-NEXT: }
# SEC-PIC-NEXT: ]
# SEC-PIC: 0x000000006FFFFFF9 RELACOUNT 1

## Check that there was no relaxation performed. All values refer to got entry.
## Ex: 0x1000 + 4233 + 7 = 0x2090
## 0x102a + 4191 + 7 = 0x2090
## Ex: 0x1000 + 4249 + 7 = 0x20A0
## 0x102a + 4207 + 7 = 0x20A0
# DISASM-PIC: Disassembly of section .text:
# DISASM-PIC-NEXT: _start:
# DISASM-PIC-NEXT: 1000: 48 13 05 89 10 00 00 adcq 4233(%rip), %rax
# DISASM-PIC-NEXT: 1007: 48 03 1d 82 10 00 00 addq 4226(%rip), %rbx
# DISASM-PIC-NEXT: 100e: 48 23 0d 7b 10 00 00 andq 4219(%rip), %rcx
# DISASM-PIC-NEXT: 1015: 48 3b 15 74 10 00 00 cmpq 4212(%rip), %rdx
# DISASM-PIC-NEXT: 101c: 48 0b 3d 6d 10 00 00 orq 4205(%rip), %rdi
# DISASM-PIC-NEXT: 1023: 48 1b 35 66 10 00 00 sbbq 4198(%rip), %rsi
# DISASM-PIC-NEXT: 102a: 48 2b 2d 5f 10 00 00 subq 4191(%rip), %rbp
# DISASM-PIC-NEXT: 1031: 4c 33 05 58 10 00 00 xorq 4184(%rip), %r8
# DISASM-PIC-NEXT: 1038: 4c 85 3d 51 10 00 00 testq 4177(%rip), %r15
# DISASM-PIC-NEXT: 1000: 48 13 05 99 10 00 00 adcq 4249(%rip), %rax
# DISASM-PIC-NEXT: 1007: 48 03 1d 92 10 00 00 addq 4242(%rip), %rbx
# DISASM-PIC-NEXT: 100e: 48 23 0d 8b 10 00 00 andq 4235(%rip), %rcx
# DISASM-PIC-NEXT: 1015: 48 3b 15 84 10 00 00 cmpq 4228(%rip), %rdx
# DISASM-PIC-NEXT: 101c: 48 0b 3d 7d 10 00 00 orq 4221(%rip), %rdi
# DISASM-PIC-NEXT: 1023: 48 1b 35 76 10 00 00 sbbq 4214(%rip), %rsi
# DISASM-PIC-NEXT: 102a: 48 2b 2d 6f 10 00 00 subq 4207(%rip), %rbp
# DISASM-PIC-NEXT: 1031: 4c 33 05 68 10 00 00 xorq 4200(%rip), %r8
# DISASM-PIC-NEXT: 1038: 4c 85 3d 61 10 00 00 testq 4193(%rip), %r15

.data
.type bar, @object
Expand Down
47 changes: 24 additions & 23 deletions lld/test/ELF/i386-tls-ie-shared.s
Expand Up @@ -2,7 +2,7 @@
// RUN: llvm-mc -filetype=obj -triple=i686-pc-linux %p/Inputs/tls-opt-iele-i686-nopic.s -o %tso.o
// RUN: ld.lld -shared %tso.o -o %tso
// RUN: ld.lld -shared %t.o %tso -o %t1
// RUN: llvm-readobj -s -r %t1 | FileCheck --check-prefix=GOTRELSHARED %s
// RUN: llvm-readobj -s -r -d %t1 | FileCheck --check-prefix=GOTRELSHARED %s
// RUN: llvm-objdump -d %t1 | FileCheck --check-prefix=DISASMSHARED %s

// GOTRELSHARED: Section {
Expand All @@ -13,8 +13,8 @@
// GOTRELSHARED-NEXT: SHF_ALLOC
// GOTRELSHARED-NEXT: SHF_WRITE
// GOTRELSHARED-NEXT: ]
// GOTRELSHARED-NEXT: Address: 0x1050
// GOTRELSHARED-NEXT: Offset: 0x1050
// GOTRELSHARED-NEXT: Address: 0x1058
// GOTRELSHARED-NEXT: Offset: 0x1058
// GOTRELSHARED-NEXT: Size: 16
// GOTRELSHARED-NEXT: Link: 0
// GOTRELSHARED-NEXT: Info: 0
Expand All @@ -31,35 +31,36 @@
// GOTRELSHARED-NEXT: 0x202D R_386_RELATIVE - 0x0
// GOTRELSHARED-NEXT: 0x2036 R_386_RELATIVE - 0x0
// GOTRELSHARED-NEXT: 0x203F R_386_RELATIVE - 0x0
// GOTRELSHARED-NEXT: 0x1050 R_386_TLS_TPOFF tlslocal0 0x0
// GOTRELSHARED-NEXT: 0x1054 R_386_TLS_TPOFF tlslocal1 0x0
// GOTRELSHARED-NEXT: 0x1058 R_386_TLS_TPOFF tlsshared0 0x0
// GOTRELSHARED-NEXT: 0x105C R_386_TLS_TPOFF tlsshared1 0x0
// GOTRELSHARED-NEXT: 0x1058 R_386_TLS_TPOFF tlslocal0 0x0
// GOTRELSHARED-NEXT: 0x105C R_386_TLS_TPOFF tlslocal1 0x0
// GOTRELSHARED-NEXT: 0x1060 R_386_TLS_TPOFF tlsshared0 0x0
// GOTRELSHARED-NEXT: 0x1064 R_386_TLS_TPOFF tlsshared1 0x0
// GOTRELSHARED-NEXT: }
// GOTRELSHARED-NEXT: ]
// GOTRELSHARED: 0x6FFFFFFA RELCOUNT 8

// DISASMSHARED: Disassembly of section test:
// DISASMSHARED-NEXT: _start:
// (.got)[0] = 0x2050 = 8272
// (.got)[1] = 0x2054 = 8276
// (.got)[2] = 0x2058 = 8280
// (.got)[3] = 0x205C = 8284
// DISASMSHARED-NEXT: 2000: 8b 0d 50 10 00 00 movl 4176, %ecx
// DISASMSHARED-NEXT: 2006: 65 8b 01 movl %gs:(%ecx), %eax
// DISASMSHARED-NEXT: 2009: a1 50 10 00 00 movl 4176, %eax
// DISASMSHARED-NEXT: 200e: 65 8b 00 movl %gs:(%eax), %eax
// DISASMSHARED-NEXT: 2011: 03 0d 50 10 00 00 addl 4176, %ecx
// DISASMSHARED-NEXT: 2017: 65 8b 01 movl %gs:(%ecx), %eax
// DISASMSHARED-NEXT: 201a: 8b 0d 54 10 00 00 movl 4180, %ecx
// DISASMSHARED-NEXT: 2020: 65 8b 01 movl %gs:(%ecx), %eax
// DISASMSHARED-NEXT: 2023: a1 54 10 00 00 movl 4180, %eax
// DISASMSHARED-NEXT: 2028: 65 8b 00 movl %gs:(%eax), %eax
// DISASMSHARED-NEXT: 202b: 03 0d 54 10 00 00 addl 4180, %ecx
// DISASMSHARED-NEXT: 2031: 65 8b 01 movl %gs:(%ecx), %eax
// DISASMSHARED-NEXT: 2034: 8b 0d 58 10 00 00 movl 4184, %ecx
// DISASMSHARED-NEXT: 203a: 65 8b 01 movl %gs:(%ecx), %eax
// DISASMSHARED-NEXT: 203d: 03 0d 5c 10 00 00 addl 4188, %ecx
// DISASMSHARED-NEXT: 2043: 65 8b 01 movl %gs:(%ecx), %eax
// DISASMSHARED-NEXT: 2000: 8b 0d 58 10 00 00 movl 4184, %ecx
// DISASMSHARED-NEXT: 2006: 65 8b 01 movl %gs:(%ecx), %eax
// DISASMSHARED-NEXT: 2009: a1 58 10 00 00 movl 4184, %eax
// DISASMSHARED-NEXT: 200e: 65 8b 00 movl %gs:(%eax), %eax
// DISASMSHARED-NEXT: 2011: 03 0d 58 10 00 00 addl 4184, %ecx
// DISASMSHARED-NEXT: 2017: 65 8b 01 movl %gs:(%ecx), %eax
// DISASMSHARED-NEXT: 201a: 8b 0d 5c 10 00 00 movl 4188, %ecx
// DISASMSHARED-NEXT: 2020: 65 8b 01 movl %gs:(%ecx), %eax
// DISASMSHARED-NEXT: 2023: a1 5c 10 00 00 movl 4188, %eax
// DISASMSHARED-NEXT: 2028: 65 8b 00 movl %gs:(%eax), %eax
// DISASMSHARED-NEXT: 202b: 03 0d 5c 10 00 00 addl 4188, %ecx
// DISASMSHARED-NEXT: 2031: 65 8b 01 movl %gs:(%ecx), %eax
// DISASMSHARED-NEXT: 2034: 8b 0d 60 10 00 00 movl 4192, %ecx
// DISASMSHARED-NEXT: 203a: 65 8b 01 movl %gs:(%ecx), %eax
// DISASMSHARED-NEXT: 203d: 03 0d 64 10 00 00 addl 4196, %ecx
// DISASMSHARED-NEXT: 2043: 65 8b 01 movl %gs:(%ecx), %eax

.type tlslocal0,@object
.section .tbss,"awT",@nobits
Expand Down
11 changes: 6 additions & 5 deletions lld/test/ELF/local-got-pie.s
@@ -1,6 +1,6 @@
// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
// RUN: ld.lld %t.o -o %t -pie
// RUN: llvm-readobj -s -r %t | FileCheck %s
// RUN: llvm-readobj -s -r -d %t | FileCheck %s
// RUN: llvm-objdump -d %t | FileCheck --check-prefix=DISASM %s

.globl _start
Expand All @@ -12,10 +12,10 @@ _start:
foo:
nop

// 0x20A0 - 1001 - 5 = 4250
// 0x20B0 - 1001 - 5 = 4266
// DISASM: Disassembly of section .text:
// DISASM-NEXT: _start:
// DISASM-NEXT: 1000: {{.*}} callq 4251
// DISASM-NEXT: 1000: {{.*}} callq 4267
// DISASM: foo:
// DISASM-NEXT: 1005: {{.*}} nop

Expand All @@ -25,12 +25,13 @@ foo:
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_WRITE
// CHECK-NEXT: ]
// CHECK-NEXT: Address: 0x20A0
// CHECK-NEXT: Address: 0x20B0
// CHECK-NEXT: Offset:
// CHECK-NEXT: Size: 8

// CHECK: Relocations [
// CHECK-NEXT: Section ({{.*}}) .rela.dyn {
// CHECK-NEXT: 0x20A0 R_X86_64_RELATIVE - 0x1005
// CHECK-NEXT: 0x20B0 R_X86_64_RELATIVE - 0x1005
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK: 0x000000006FFFFFF9 RELACOUNT 1
11 changes: 6 additions & 5 deletions lld/test/ELF/local-got-shared.s
@@ -1,6 +1,6 @@
// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
// RUN: ld.lld %t.o -o %t -shared
// RUN: llvm-readobj -s -r %t | FileCheck %s
// RUN: llvm-readobj -s -r -d %t | FileCheck %s
// RUN: llvm-objdump -d %t | FileCheck --check-prefix=DISASM %s

bar:
Expand All @@ -11,9 +11,9 @@ bar:
foo:
nop

// 0x2090 - 0x1000 - 5 = 4235
// 0x20A0 - 0x1000 - 5 = 4251
// DISASM: bar:
// DISASM-NEXT: 1000: {{.*}} callq 4235
// DISASM-NEXT: 1000: {{.*}} callq 4251

// DISASM: foo:
// DISASM-NEXT: 1005: {{.*}} nop
Expand All @@ -24,12 +24,13 @@ foo:
// CHECK-NEXT: SHF_ALLOC
// CHECK-NEXT: SHF_WRITE
// CHECK-NEXT: ]
// CHECK-NEXT: Address: 0x2090
// CHECK-NEXT: Address: 0x20A0
// CHECK-NEXT: Offset:
// CHECK-NEXT: Size: 8

// CHECK: Relocations [
// CHECK-NEXT: Section ({{.*}}) .rela.dyn {
// CHECK-NEXT: 0x2090 R_X86_64_RELATIVE - 0x1005
// CHECK-NEXT: 0x20A0 R_X86_64_RELATIVE - 0x1005
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK: 0x000000006FFFFFF9 RELACOUNT 1

0 comments on commit aa49819

Please sign in to comment.