Skip to content

Commit 6faba31

Browse files
committed
[ELF] --emit-relocs: adjust offsets of .rel[a].eh_frame relocations
.eh_frame pieces may be dropped due to GC/ICF. When --emit-relocs adds relocations against .eh_frame, the offsets need to be adjusted. Use the same way as MergeInputSection with a special case handling outSecOff==-1 for an invalid piece (see eh-frame-marker.s). This exposes an issue in mips64-eh-abs-reloc.s that we don't reliably handle anyway. Just add --no-check-dynamic-relocations to paper over it. Original patch by Ayrton Muñoz Differential Revision: https://reviews.llvm.org/D122459
1 parent f7381a7 commit 6faba31

File tree

4 files changed

+30
-7
lines changed

4 files changed

+30
-7
lines changed

lld/ELF/InputSection.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,15 @@ uint64_t SectionBase::getOffset(uint64_t offset) const {
160160
case Regular:
161161
case Synthetic:
162162
return cast<InputSection>(this)->outSecOff + offset;
163-
case EHFrame:
163+
case EHFrame: {
164164
// The file crtbeginT.o has relocations pointing to the start of an empty
165165
// .eh_frame that is known to be the first in the link. It does that to
166166
// identify the start of the output .eh_frame.
167-
return offset;
167+
const EhInputSection *es = cast<EhInputSection>(this);
168+
if (InputSection *isec = es->getParent())
169+
return isec->outSecOff + es->getParentOffset(offset);
170+
return es->getParentOffset(offset);
171+
}
168172
case Merge:
169173
const MergeInputSection *ms = cast<MergeInputSection>(this);
170174
if (InputSection *isec = ms->getParent())
@@ -1334,6 +1338,21 @@ void EhInputSection::split(ArrayRef<RelTy> rels) {
13341338
getObjMsg(d.data() - rawData.data()));
13351339
}
13361340

1341+
const EhSectionPiece &EhInputSection::getSectionPiece(uint64_t offset) const {
1342+
if (this->data().size() < offset)
1343+
fatal(toString(this) + ": offset is outside the section");
1344+
return partition_point(
1345+
pieces, [=](EhSectionPiece p) { return p.inputOff <= offset; })[-1];
1346+
}
1347+
1348+
// Return the offset in an output section for a given input offset.
1349+
uint64_t EhInputSection::getParentOffset(uint64_t offset) const {
1350+
const EhSectionPiece &piece = getSectionPiece(offset);
1351+
if (piece.outputOff == -1) // invalid piece
1352+
return offset - piece.inputOff;
1353+
return piece.outputOff + (offset - piece.inputOff);
1354+
}
1355+
13371356
static size_t findNull(StringRef s, size_t entSize) {
13381357
for (unsigned i = 0, n = s.size(); i != n; i += entSize) {
13391358
const char *b = s.begin() + i;

lld/ELF/InputSection.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,10 @@ class EhInputSection : public InputSectionBase {
325325
SmallVector<EhSectionPiece, 0> pieces;
326326

327327
SyntheticSection *getParent() const;
328+
uint64_t getParentOffset(uint64_t offset) const;
329+
330+
// Return the EHSectionPiece at a given input section offset.
331+
const EhSectionPiece &getSectionPiece(uint64_t offset) const;
328332
};
329333

330334
// This is a section that is added directly to an output section

lld/test/ELF/eh-frame-merge.s

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
# RELOC: Offset Info Type Symbol's Value Symbol's Name + Addend
1818
# RELOC-NEXT: {{0*}}[[#%x,OFF:]] [[#%x,]] R_X86_64_PC32 [[#%x,]] foo + 0
19-
# RELOC-NEXT: {{0*}}[[#%x,OFF+20]] [[#%x,]] R_X86_64_PC32 [[#%x,]] bar + 0
20-
# RELOC-NEXT: {{0*}}[[#OFF]] [[#%x,]] R_X86_64_PC32 [[#%x,]] foo + 1
21-
# RELOC-NEXT: {{0*}}[[#OFF+20]] [[#%x,]] R_X86_64_NONE 0{{$}}
19+
# RELOC-NEXT: {{0*}}[[#%x,OFF+24]] [[#%x,]] R_X86_64_PC32 [[#%x,]] bar + 0
20+
# RELOC-NEXT: {{0*}}[[#OFF+48]] [[#%x,]] R_X86_64_PC32 [[#%x,]] foo + 1
21+
# RELOC-NEXT: {{0*}}[[#%x,OFF-24]] [[#%x,]] R_X86_64_NONE 0{{$}}
2222

2323
# EH: Format: DWARF32
2424
# EH: 00000018 00000014 0000001c FDE cie=00000000 pc={{0*}}[[#%x,FOO:]]...

lld/test/ELF/mips64-eh-abs-reloc.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
# Having an R_MIPS_64 relocation in eh_frame would previously crash LLD
33
# RUN: llvm-mc -filetype=obj -triple=mips64-unknown-freebsd %s -o %t.o
44
# RUN: llvm-readobj -r %t.o | FileCheck %s -check-prefix OBJ
5-
# RUN: ld.lld --eh-frame-hdr -shared -z notext -o %t.so %t.o
5+
# RUN: ld.lld --eh-frame-hdr -shared -z notext -o %t.so %t.o --no-check-dynamic-relocations
66
# RUN: llvm-readobj -r %t.so | FileCheck %s -check-prefix PIC-RELOCS
77

88
# Linking this as a PIE executable would also previously crash
99
# RUN: llvm-mc -filetype=obj -triple=mips64-unknown-freebsd %S/Inputs/archive2.s -o %t-foo.o
1010
# -pie needs -z notext because of the R_MIPS_64 relocation
11-
# RUN: ld.lld --eh-frame-hdr -Bdynamic -pie -z notext -o %t-pie-dynamic.exe %t.o %t-foo.o
11+
# RUN: ld.lld --eh-frame-hdr -Bdynamic -pie -z notext -o %t-pie-dynamic.exe %t.o %t-foo.o --no-check-dynamic-relocations
1212
# RUN: llvm-readobj -r %t-pie-dynamic.exe | FileCheck %s -check-prefix PIC-RELOCS
1313

1414

0 commit comments

Comments
 (0)