Skip to content

Commit c135a68

Browse files
author
Georgii Rymar
committed
[LLD][ELF] - Do not produce an invalid dynamic relocation order with --shuffle-sections.
Normally (when not on android with android relocation packing enabled), we put IRelative relocations to ".rel[a].dyn", after other relocations, to ensure that IRelatives are processed last by the dynamic loader. To achieve that we add the `in.relaIplt` after the `part.relaDyn`: https://github.com/llvm/llvm-project/blob/master/lld/ELF/Writer.cpp#L540 The problem is that `--shuffle-sections` might break the sections order. This patch fixes it. Fixes https://bugs.llvm.org/show_bug.cgi?id=47056. Differential revision: https://reviews.llvm.org/D85651
1 parent 7e6c437 commit c135a68

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

lld/ELF/Writer.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,14 @@ static void sortSection(OutputSection *sec,
14371437
if (name == ".init" || name == ".fini")
14381438
return;
14391439

1440+
// IRelative relocations that usually live in the .rel[a].dyn section should
1441+
// be proccessed last by the dynamic loader. To achieve that we add synthetic
1442+
// sections in the required order from the begining so that the in.relaIplt
1443+
// section is placed last in an output section. Here we just do not apply
1444+
// sorting for an output section which holds the in.relaIplt section.
1445+
if (in.relaIplt->getParent() == sec)
1446+
return;
1447+
14401448
// Sort input sections by priority using the list provided by
14411449
// --symbol-ordering-file or --shuffle-sections=. This is a least significant
14421450
// digit radix sort. The sections may be sorted stably again by a more

lld/test/ELF/gnu-ifunc-plt.s

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,20 @@
7777
// DISASM-NEXT: 201386: pushq $0x1
7878
// DISASM-NEXT: 20138b: jmp 0x201340 <.plt>
7979

80+
// Test that --shuffle-sections does not affect the order of relocations and that
81+
// we still place IRELATIVE relocations last. Check both random seed (0) and an
82+
// arbitrary seed that was known to break the order of relocations previously (3).
83+
// RUN: ld.lld --shuffle-sections=3 %t.so %t.o -o %tout2
84+
// RUN: llvm-readobj --relocations %tout2 | FileCheck %s --check-prefix=SHUFFLE
85+
// RUN: ld.lld --shuffle-sections=0 %t.so %t.o -o %tout3
86+
// RUN: llvm-readobj --relocations %tout3 | FileCheck %s --check-prefix=SHUFFLE
87+
88+
// SHUFFLE: Section {{.*}} .rela.dyn {
89+
// SHUFFLE-NEXT: R_X86_64_GLOB_DAT
90+
// SHUFFLE-NEXT: R_X86_64_IRELATIVE
91+
// SHUFFLE-NEXT: R_X86_64_IRELATIVE
92+
// SHUFFLE-NEXT: }
93+
8094
.text
8195
.type foo STT_GNU_IFUNC
8296
.globl foo

0 commit comments

Comments
 (0)