Skip to content

Commit

Permalink
[ELF] Sort IRELATIVE by offset
Browse files Browse the repository at this point in the history
Improve the test gnu-ifunc-nonpreemptible.s to check IRELATIVE offsets.
Ensure that IRELATIVE offsets are ordered to improve locality.
  • Loading branch information
MaskRay committed Apr 3, 2024
1 parent c925c16 commit 01e2274
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 54 deletions.
2 changes: 1 addition & 1 deletion lld/ELF/SyntheticSections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1666,7 +1666,7 @@ void RelocationBaseSection::computeRels() {
parallelForEach(relocs,
[symTab](DynamicReloc &rel) { rel.computeRaw(symTab); });

auto irelative = std::partition(
auto irelative = std::stable_partition(
relocs.begin() + numRelativeRelocs, relocs.end(),
[t = target->iRelativeRel](auto &r) { return r.type != t; });

Expand Down
75 changes: 47 additions & 28 deletions lld/test/ELF/gnu-ifunc-nonpreemptible.s
Original file line number Diff line number Diff line change
@@ -1,62 +1,75 @@
# REQUIRES: x86
# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
# RUN: ld.lld %t.o -o %t
# RUN: llvm-objdump --no-print-imm-hex -d --no-show-raw-insn %t | FileCheck %s --check-prefix=DISASM
# RUN: llvm-readelf -r -s %t | FileCheck %s
# RUN: rm -rf %t && split-file %s %t && cd %t
# RUN: llvm-mc -filetype=obj -triple=x86_64 a.s -o a.o
# RUN: llvm-mc -filetype=obj -triple=x86_64 b.s -o b.o
# RUN: ld.lld -shared -soname=b.so b.o -o b.so

# RUN: ld.lld --export-dynamic %t.o -o %t
# RUN: llvm-readelf -r -s %t | FileCheck %s
# RUN: ld.lld a.o -o a
# RUN: llvm-objdump --no-print-imm-hex -d --no-show-raw-insn a | FileCheck %s --check-prefix=DISASM
# RUN: llvm-readelf -r -s a | FileCheck %s

# CHECK: Relocation section '.rela.dyn' at offset {{.*}} contains 2 entries:
# CHECK: Relocation section '.rela.dyn' at offset {{.*}} contains 3 entries:
# CHECK-NEXT: Type
# CHECK-NEXT: R_X86_64_IRELATIVE
# CHECK-NEXT: R_X86_64_IRELATIVE
# CHECK-NEXT: {{0*}}[[#%x,O:]] [[#%x,]] R_X86_64_IRELATIVE [[#%x,QUX:]]
# CHECK-NEXT: {{0*}}[[#O+8]] [[#%x,]] R_X86_64_IRELATIVE
# CHECK-NEXT: {{0*}}[[#O+16]] [[#%x,]] R_X86_64_IRELATIVE

# CHECK: 0 NOTYPE LOCAL HIDDEN [[#]] __rela_iplt_start
# CHECK-NEXT: 0 NOTYPE LOCAL HIDDEN [[#]] __rela_iplt_end
# CHECK: 0 NOTYPE LOCAL HIDDEN [[#]] __rela_iplt_start
# CHECK-NEXT: 0 NOTYPE LOCAL HIDDEN [[#]] __rela_iplt_end
# CHECK-NEXT: {{0*}}[[#QUX]] 0 IFUNC GLOBAL DEFAULT [[#]] qux

# RUN: ld.lld -pie %t.o -o %t1
# RUN: llvm-readelf -s %t1 | FileCheck %s --check-prefix=PIC
# RUN: ld.lld -shared %t.o -o %t2
# RUN: llvm-readelf -s %t2 | FileCheck %s --check-prefix=PIC
# RUN: ld.lld -pie a.o b.so -o a1
# RUN: llvm-readelf -rs a1 | FileCheck %s --check-prefixes=PIC,PIE
# RUN: ld.lld -shared a.o b.so -o a2
# RUN: llvm-readelf -rs a2 | FileCheck %s --check-prefix=PIC

# PIC: R_X86_64_GLOB_DAT 0000000000000000 ext + 0
# PIC-NEXT: {{0*}}[[#%x,O:]] [[#%x,]] R_X86_64_64 0000000000000000 __rela_iplt_start + 0
# PIC-NEXT: {{0*}}[[#O+8]] [[#%x,]] R_X86_64_64 0000000000000000 __rela_iplt_end + 0
# PIE-NEXT: {{0*}}[[#O+16]] [[#%x,]] R_X86_64_IRELATIVE
# PIE-NEXT: {{0*}}[[#O+24]] [[#%x,]] R_X86_64_IRELATIVE
# PIE-NEXT: {{0*}}[[#O+32]] [[#%x,]] R_X86_64_IRELATIVE

# PIC: 0 NOTYPE WEAK DEFAULT UND __rela_iplt_start
# PIC-NEXT: 0 NOTYPE WEAK DEFAULT UND __rela_iplt_end

# DISASM: Disassembly of section .text:
# DISASM-EMPTY:
# DISASM-NEXT: <foo>:
# DISASM-NEXT: <qux>:
# DISASM: <foo>:
# DISASM: <bar>:
# DISASM: <unused>:
# DISASM: <_start>:
# DISASM-NEXT: callq 0x[[#%x,foo:]]
# DISASM-NEXT: callq 0x[[#%x,bar:]]
# DISASM-NEXT: callq 0x[[#%x,qux:]]
# DISASM-EMPTY:
# DISASM-NEXT: Disassembly of section .iplt:
# DISASM-EMPTY:
# DISASM-NEXT: <.iplt>:
# DISASM-NEXT: [[#foo]]: jmpq *{{.*}}(%rip)
# DISASM-NEXT: [[#qux]]: jmpq *{{.*}}(%rip)
# DISASM-NEXT: pushq $0
# DISASM-NEXT: jmp 0x0
# DISASM-NEXT: [[#bar]]: jmpq *{{.*}}(%rip)
# DISASM-NEXT: [[#foo]]: jmpq *{{.*}}(%rip)
# DISASM-NEXT: pushq $1
# DISASM-NEXT: jmp 0x0
# DISASM-NEXT: [[#bar]]: jmpq *{{.*}}(%rip)
# DISASM-NEXT: pushq $2
# DISASM-NEXT: jmp 0x0

.text
#--- a.s
.globl qux, foo, bar
.type qux, @gnu_indirect_function
.type foo STT_GNU_IFUNC
.globl foo
foo:
ret

.type bar STT_GNU_IFUNC
.globl bar
bar:
ret
qux: ret
foo: ret
bar: ret

.type unused, @gnu_indirect_function
.globl unused
unused:
ret
.weak ext
unused: mov ext@gotpcrel(%rip), %rax

.weak __rela_iplt_start
.weak __rela_iplt_end
Expand All @@ -65,7 +78,13 @@ unused:
_start:
call foo
call bar
call qux

.data
.quad __rela_iplt_start
.quad __rela_iplt_end

#--- b.s
.globl ext
ext:
ret
25 changes: 0 additions & 25 deletions lld/test/ELF/gnu-ifunc-relative.s

This file was deleted.

0 comments on commit 01e2274

Please sign in to comment.