Skip to content

Commit 0053cb8

Browse files
committed
[BOLT] Fix .relr section addend patching
The new relocation offset in .relr section patching was calculated wrong previously. Pass the new file offset to lambda instead of re-calculating it in it. Test removes relocation from mytext section, so in case of wrong offset calculation we won't emit right addend value in expected place, i.e. on the new relocation offset. Differential Revision: https://reviews.llvm.org/D159543
1 parent 08086c1 commit 0053cb8

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4720,9 +4720,11 @@ void RewriteInstance::patchELFAllocatableRelrSection(
47204720
const uint8_t PSize = BC->AsmInfo->getCodePointerSize();
47214721
const uint64_t MaxDelta = ((CHAR_BIT * DynamicRelrEntrySize) - 1) * PSize;
47224722

4723-
auto FixAddend = [&](const BinarySection &Section, const Relocation &Rel) {
4723+
auto FixAddend = [&](const BinarySection &Section, const Relocation &Rel,
4724+
uint64_t FileOffset) {
47244725
// Fix relocation symbol value in place if no static relocation found
4725-
// on the same address
4726+
// on the same address. We won't check the BF relocations here since it
4727+
// is rare case and no optimization is required.
47264728
if (Section.getRelocationAt(Rel.Offset))
47274729
return;
47284730

@@ -4731,11 +4733,6 @@ void RewriteInstance::patchELFAllocatableRelrSection(
47314733
if (!Addend)
47324734
return;
47334735

4734-
uint64_t FileOffset = Section.getOutputFileOffset();
4735-
if (!FileOffset)
4736-
FileOffset = Section.getInputFileOffset();
4737-
4738-
FileOffset += Rel.Offset;
47394736
OS.pwrite(reinterpret_cast<const char *>(&Addend), PSize, FileOffset);
47404737
};
47414738

@@ -4757,7 +4754,7 @@ void RewriteInstance::patchELFAllocatableRelrSection(
47574754
RelOffset = RelOffset == 0 ? SectionAddress + Rel.Offset : RelOffset;
47584755
assert((RelOffset & 1) == 0 && "Wrong relocation offset");
47594756
RelOffsets.emplace(RelOffset);
4760-
FixAddend(Section, Rel);
4757+
FixAddend(Section, Rel, RelOffset);
47614758
}
47624759
}
47634760

bolt/test/AArch64/constant_island_pie_update.s

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
// .relr.dyn
1414
# RUN: %clang %cflags -fPIC -pie %t.o -o %t.relr.exe -nostdlib \
1515
# RUN: -Wl,-q -Wl,-z,notext -Wl,--pack-dyn-relocs=relr
16+
# RUN: llvm-objcopy --remove-section .rela.mytext %t.relr.exe
1617
# RUN: llvm-bolt %t.relr.exe -o %t.relr.bolt --use-old-text=0 --lite=0
1718
# RUN: llvm-objdump -j .text -d --show-all-symbols %t.relr.bolt | FileCheck %s
19+
# RUN: llvm-objdump -j .text -d %t.relr.bolt | \
20+
# RUN: FileCheck %s --check-prefix=ADDENDCHECK
1821
# RUN: llvm-readelf -rsW %t.relr.bolt | FileCheck --check-prefix=ELFCHECK %s
1922
# RUN: llvm-readelf -SW %t.relr.bolt | FileCheck --check-prefix=RELRSZCHECK %s
2023

@@ -30,6 +33,11 @@
3033
# CHECK-NEXT: {{.*}} .word 0x{{[0]+}}[[#ADDR]]
3134
# CHECK-NEXT: {{.*}} .word 0x00000000
3235

36+
// Check that addend was properly patched in mytextP with stripped relocations
37+
# ADDENDCHECK: [[#%x,ADDR:]] <exitLocal>:
38+
# ADDENDCHECK: {{.*}} <mytextP>:
39+
# ADDENDCHECK-NEXT: {{.*}} .word 0x{{[0]+}}[[#ADDR]]
40+
# ADDENDCHECK-NEXT: {{.*}} .word 0x00000000
3341

3442
// Check that we've relaxed adr to adrp + add to refer external CI
3543
# CHECK: <addressDynCi>:
@@ -40,9 +48,10 @@
4048
# ELFCHECK: [[#%x,OFF:]] [[#%x,INFO_DYN:]] R_AARCH64_RELATIVE
4149
# ELFCHECK-NEXT: [[#OFF + 8]] {{0*}}[[#INFO_DYN]] R_AARCH64_RELATIVE
4250
# ELFCHECK-NEXT: [[#OFF + 24]] {{0*}}[[#INFO_DYN]] R_AARCH64_RELATIVE
51+
# ELFCHECK-NEXT: {{.*}} R_AARCH64_RELATIVE
4352
# ELFCHECK: {{.*}}[[#OFF]] {{.*}} $d
4453

45-
// Check that .relr.dyn size is 2 bytes to ensure that last 2 relocations were
54+
// Check that .relr.dyn size is 2 bytes to ensure that last 3 relocations were
4655
// encoded as a bitmap so the total section size for 3 relocations is 2 bytes.
4756
# RELRSZCHECK: .relr.dyn RELR [[#%x,ADDR:]] [[#%x,OFF:]] {{0*}}10
4857

@@ -81,3 +90,17 @@ addressDynCi:
8190
adr x1, .Lci
8291
bl _start
8392
.size addressDynCi, .-addressDynCi
93+
94+
.section ".mytext", "ax"
95+
.balign 8
96+
.global dummy
97+
.type dummy, %function
98+
dummy:
99+
nop
100+
.word 0
101+
.size dummy, .-dummy
102+
103+
.global mytextP
104+
mytextP:
105+
.xword exitLocal
106+
.size mytextP, .-mytextP

0 commit comments

Comments
 (0)