Skip to content

Commit c3c9e45

Browse files
committed
[ELF] Add InputSectionBase::{addRelocs,relocs} and GotSection::addConstant to add/access relocations
to prepare for changing `relocations` from a SmallVector to a pointer. Also change the `isec` parameter in `addAddendOnlyRelocIfNonPreemptible` to `GotSection &`.
1 parent e2f8b80 commit c3c9e45

14 files changed

+83
-88
lines changed

lld/ELF/AArch64ErrataFix.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -544,10 +544,10 @@ static void implementPatch(uint64_t adrpAddr, uint64_t patcheeOffset,
544544
// and replace the relocation with a R_AARCH_JUMP26 branch relocation.
545545
// Case 4: No relocation. We must create a new R_AARCH64_JUMP26 branch
546546
// relocation at the offset.
547-
auto relIt = llvm::find_if(isec->relocations, [=](const Relocation &r) {
547+
auto relIt = llvm::find_if(isec->relocs(), [=](const Relocation &r) {
548548
return r.offset == patcheeOffset;
549549
});
550-
if (relIt != isec->relocations.end() &&
550+
if (relIt != isec->relocs().end() &&
551551
(relIt->type == R_AARCH64_JUMP26 || relIt->expr == R_RELAX_TLS_IE_TO_LE))
552552
return;
553553

@@ -561,12 +561,11 @@ static void implementPatch(uint64_t adrpAddr, uint64_t patcheeOffset,
561561
return Relocation{R_PC, R_AARCH64_JUMP26, offset, 0, patchSym};
562562
};
563563

564-
if (relIt != isec->relocations.end()) {
565-
ps->relocations.push_back(
566-
{relIt->expr, relIt->type, 0, relIt->addend, relIt->sym});
564+
if (relIt != isec->relocs().end()) {
565+
ps->addReloc({relIt->expr, relIt->type, 0, relIt->addend, relIt->sym});
567566
*relIt = makeRelToPatch(patcheeOffset, ps->patchSym);
568567
} else
569-
isec->relocations.push_back(makeRelToPatch(patcheeOffset, ps->patchSym));
568+
isec->addReloc(makeRelToPatch(patcheeOffset, ps->patchSym));
570569
}
571570

572571
// Scan all the instructions in InputSectionDescription, for each instance of

lld/ELF/ARMErrataFix.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ void Patch657417Section::writeTo(uint8_t *buf) {
181181
else
182182
write32le(buf, 0x9000f000);
183183
// If we have a relocation then apply it.
184-
if (!relocations.empty()) {
184+
if (!relocs().empty()) {
185185
target->relocateAlloc(*this, buf);
186186
return;
187187
}
@@ -281,12 +281,12 @@ static ScanResult scanCortexA8Errata657417(InputSection *isec, uint64_t &off,
281281
// Find a relocation for the branch if it exists. This will be used
282282
// to determine the target.
283283
uint64_t branchOff = off + 4;
284-
auto relIt = llvm::find_if(isec->relocations, [=](const Relocation &r) {
284+
auto relIt = llvm::find_if(isec->relocs(), [=](const Relocation &r) {
285285
return r.offset == branchOff &&
286286
(r.type == R_ARM_THM_JUMP19 || r.type == R_ARM_THM_JUMP24 ||
287287
r.type == R_ARM_THM_CALL);
288288
});
289-
if (relIt != isec->relocations.end())
289+
if (relIt != isec->relocs().end())
290290
scanRes.rel = &(*relIt);
291291
if (branchDestInFirstRegion(isec, branchOff, instr2, scanRes.rel)) {
292292
if (patchInRange(isec, branchOff, instr2)) {
@@ -451,7 +451,7 @@ static void implementPatch(ScanResult sr, InputSection *isec,
451451
patchRelType = R_ARM_JUMP24;
452452
patchRelAddend -= 4;
453453
}
454-
psec->relocations.push_back(
454+
psec->addReloc(
455455
Relocation{sr.rel->expr, patchRelType, 0, patchRelAddend, sr.rel->sym});
456456
// Redirect the existing branch relocation to the patch.
457457
sr.rel->expr = R_PC;
@@ -470,8 +470,7 @@ static void implementPatch(ScanResult sr, InputSection *isec,
470470
type = R_ARM_THM_JUMP24;
471471
else
472472
type = R_ARM_THM_CALL;
473-
isec->relocations.push_back(
474-
Relocation{R_PC, type, sr.off, -4, psec->patchSym});
473+
isec->addReloc(Relocation{R_PC, type, sr.off, -4, psec->patchSym});
475474
}
476475
patches.push_back(psec);
477476
}

lld/ELF/Arch/AArch64.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -747,24 +747,24 @@ void AArch64::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
747747
uint64_t secAddr = sec.getOutputSection()->addr;
748748
if (auto *s = dyn_cast<InputSection>(&sec))
749749
secAddr += s->outSecOff;
750-
AArch64Relaxer relaxer(sec.relocations);
751-
for (size_t i = 0, size = sec.relocations.size(); i != size; ++i) {
752-
const Relocation &rel = sec.relocations[i];
750+
AArch64Relaxer relaxer(sec.relocs());
751+
for (size_t i = 0, size = sec.relocs().size(); i != size; ++i) {
752+
const Relocation &rel = sec.relocs()[i];
753753
uint8_t *loc = buf + rel.offset;
754754
const uint64_t val =
755755
sec.getRelocTargetVA(sec.file, rel.type, rel.addend,
756756
secAddr + rel.offset, *rel.sym, rel.expr);
757757
switch (rel.expr) {
758758
case R_AARCH64_GOT_PAGE_PC:
759759
if (i + 1 < size &&
760-
relaxer.tryRelaxAdrpLdr(rel, sec.relocations[i + 1], secAddr, buf)) {
760+
relaxer.tryRelaxAdrpLdr(rel, sec.relocs()[i + 1], secAddr, buf)) {
761761
++i;
762762
continue;
763763
}
764764
break;
765765
case R_AARCH64_PAGE_PC:
766766
if (i + 1 < size &&
767-
relaxer.tryRelaxAdrpAdd(rel, sec.relocations[i + 1], secAddr, buf)) {
767+
relaxer.tryRelaxAdrpAdd(rel, sec.relocs()[i + 1], secAddr, buf)) {
768768
++i;
769769
continue;
770770
}

lld/ELF/Arch/PPC.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ void PPC::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
486486
uint64_t secAddr = sec.getOutputSection()->addr;
487487
if (auto *s = dyn_cast<InputSection>(&sec))
488488
secAddr += s->outSecOff;
489-
for (const Relocation &rel : sec.relocations) {
489+
for (const Relocation &rel : sec.relocs()) {
490490
uint8_t *loc = buf + rel.offset;
491491
const uint64_t val = SignExtend64(
492492
sec.getRelocTargetVA(sec.file, rel.type, rel.addend,

lld/ELF/Arch/PPC64.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,7 @@ void PPC64::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
15181518
if (auto *s = dyn_cast<InputSection>(&sec))
15191519
secAddr += s->outSecOff;
15201520
uint64_t lastPPCRelaxedRelocOff = -1;
1521-
for (const Relocation &rel : sec.relocations) {
1521+
for (const Relocation &rel : sec.relocs()) {
15221522
uint8_t *loc = buf + rel.offset;
15231523
const uint64_t val =
15241524
sec.getRelocTargetVA(sec.file, rel.type, rel.addend,

lld/ELF/Arch/RISCV.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -513,11 +513,11 @@ static void initSymbolAnchors() {
513513
continue;
514514
for (InputSection *sec : getInputSections(*osec, storage)) {
515515
sec->relaxAux = make<RISCVRelaxAux>();
516-
if (sec->relocations.size()) {
516+
if (sec->relocs().size()) {
517517
sec->relaxAux->relocDeltas =
518-
std::make_unique<uint32_t[]>(sec->relocations.size());
518+
std::make_unique<uint32_t[]>(sec->relocs().size());
519519
sec->relaxAux->relocTypes =
520-
std::make_unique<RelType[]>(sec->relocations.size());
520+
std::make_unique<RelType[]>(sec->relocs().size());
521521
}
522522
}
523523
}
@@ -617,7 +617,7 @@ static bool relax(InputSection &sec) {
617617
DenseMap<const Defined *, uint64_t> valueDelta;
618618
ArrayRef<SymbolAnchor> sa = makeArrayRef(aux.anchors);
619619
uint32_t delta = 0;
620-
for (auto [i, r] : llvm::enumerate(sec.relocations)) {
620+
for (auto [i, r] : llvm::enumerate(sec.relocs())) {
621621
for (; sa.size() && sa[0].offset <= r.offset; sa = sa.slice(1))
622622
if (!sa[0].end)
623623
valueDelta[sa[0].d] = delta;
@@ -629,9 +629,9 @@ static bool relax(InputSection &sec) {
629629
sa = makeArrayRef(aux.anchors);
630630
delta = 0;
631631

632-
std::fill_n(aux.relocTypes.get(), sec.relocations.size(), R_RISCV_NONE);
632+
std::fill_n(aux.relocTypes.get(), sec.relocs().size(), R_RISCV_NONE);
633633
aux.writes.clear();
634-
for (auto [i, r] : llvm::enumerate(sec.relocations)) {
634+
for (auto [i, r] : llvm::enumerate(sec.relocs())) {
635635
const uint64_t loc = secAddr + r.offset - delta;
636636
uint32_t &cur = aux.relocDeltas[i], remove = 0;
637637
switch (r.type) {
@@ -646,16 +646,16 @@ static bool relax(InputSection &sec) {
646646
}
647647
case R_RISCV_CALL:
648648
case R_RISCV_CALL_PLT:
649-
if (i + 1 != sec.relocations.size() &&
650-
sec.relocations[i + 1].type == R_RISCV_RELAX)
649+
if (i + 1 != sec.relocs().size() &&
650+
sec.relocs()[i + 1].type == R_RISCV_RELAX)
651651
relaxCall(sec, i, loc, r, remove);
652652
break;
653653
case R_RISCV_TPREL_HI20:
654654
case R_RISCV_TPREL_ADD:
655655
case R_RISCV_TPREL_LO12_I:
656656
case R_RISCV_TPREL_LO12_S:
657-
if (i + 1 != sec.relocations.size() &&
658-
sec.relocations[i + 1].type == R_RISCV_RELAX)
657+
if (i + 1 != sec.relocs().size() &&
658+
sec.relocs()[i + 1].type == R_RISCV_RELAX)
659659
relaxTlsLe(sec, i, loc, r, remove);
660660
break;
661661
}
@@ -727,10 +727,9 @@ void elf::riscvFinalizeRelax(int passes) {
727727
if (!aux.relocDeltas)
728728
continue;
729729

730-
auto &rels = sec->relocations;
730+
MutableArrayRef<Relocation> rels = sec->relocs();
731731
ArrayRef<uint8_t> old = sec->content();
732-
size_t newSize =
733-
old.size() - aux.relocDeltas[sec->relocations.size() - 1];
732+
size_t newSize = old.size() - aux.relocDeltas[rels.size() - 1];
734733
size_t writesIdx = 0;
735734
uint8_t *p = context().bAlloc.Allocate<uint8_t>(newSize);
736735
uint64_t offset = 0;

lld/ELF/Arch/X86.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ void X86::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
472472
uint64_t secAddr = sec.getOutputSection()->addr;
473473
if (auto *s = dyn_cast<InputSection>(&sec))
474474
secAddr += s->outSecOff;
475-
for (const Relocation &rel : sec.relocations) {
475+
for (const Relocation &rel : sec.relocs()) {
476476
uint8_t *loc = buf + rel.offset;
477477
const uint64_t val = SignExtend64(
478478
sec.getRelocTargetVA(sec.file, rel.type, rel.addend,

lld/ELF/Arch/X86_64.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ static JmpInsnOpcode getJmpInsnType(const uint8_t *first,
151151
// Returns the maximum size of the vector if no such relocation is found.
152152
static unsigned getRelocationWithOffset(const InputSection &is,
153153
uint64_t offset) {
154-
unsigned size = is.relocations.size();
154+
unsigned size = is.relocs().size();
155155
for (unsigned i = size - 1; i + 1 > 0; --i) {
156-
if (is.relocations[i].offset == offset && is.relocations[i].expr != R_NONE)
156+
if (is.relocs()[i].offset == offset && is.relocs()[i].expr != R_NONE)
157157
return i;
158158
}
159159
return size;
@@ -247,10 +247,10 @@ bool X86_64::deleteFallThruJmpInsn(InputSection &is, InputFile *file,
247247
// If this jmp insn can be removed, it is the last insn and the
248248
// relocation is 4 bytes before the end.
249249
unsigned rIndex = getRelocationWithOffset(is, is.getSize() - 4);
250-
if (rIndex == is.relocations.size())
250+
if (rIndex == is.relocs().size())
251251
return false;
252252

253-
Relocation &r = is.relocations[rIndex];
253+
Relocation &r = is.relocs()[rIndex];
254254

255255
// Check if the relocation corresponds to a direct jmp.
256256
const uint8_t *secContents = is.content().data();
@@ -275,10 +275,10 @@ bool X86_64::deleteFallThruJmpInsn(InputSection &is, InputFile *file,
275275

276276
unsigned rbIndex =
277277
getRelocationWithOffset(is, (is.getSize() - sizeOfDirectJmpInsn - 4));
278-
if (rbIndex == is.relocations.size())
278+
if (rbIndex == is.relocs().size())
279279
return false;
280280

281-
Relocation &rB = is.relocations[rbIndex];
281+
Relocation &rB = is.relocs()[rbIndex];
282282

283283
const uint8_t *jmpInsnB = secContents + rB.offset - 1;
284284
JmpInsnOpcode jmpOpcodeB = getJmpInsnType(jmpInsnB - 1, jmpInsnB);
@@ -989,7 +989,7 @@ void X86_64::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
989989
uint64_t secAddr = sec.getOutputSection()->addr;
990990
if (auto *s = dyn_cast<InputSection>(&sec))
991991
secAddr += s->outSecOff;
992-
for (const Relocation &rel : sec.relocations) {
992+
for (const Relocation &rel : sec.relocs()) {
993993
if (rel.expr == R_NONE) // See deleteFallThruJmpInsn
994994
continue;
995995
uint8_t *loc = buf + rel.offset;

lld/ELF/InputSection.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ void InputSection::copyRelocations(uint8_t *buf, ArrayRef<RelTy> rels) {
432432
if (RelTy::IsRela)
433433
p->r_addend = sym.getVA(addend) - section->getOutputSection()->addr;
434434
else if (config->relocatable && type != target.noneRel)
435-
sec->relocations.push_back({R_ABS, type, rel.r_offset, addend, &sym});
435+
sec->addReloc({R_ABS, type, rel.r_offset, addend, &sym});
436436
} else if (config->emachine == EM_PPC && type == R_PPC_PLTREL24 &&
437437
p->r_addend >= 0x8000 && sec->file->ppc32Got2) {
438438
// Similar to R_MIPS_GPREL{16,32}. If the addend of R_PPC_PLTREL24
@@ -561,7 +561,7 @@ static Relocation *getRISCVPCRelHi20(const Symbol *sym, uint64_t addend) {
561561
Relocation r;
562562
r.offset = d->value;
563563
auto range =
564-
std::equal_range(isec->relocations.begin(), isec->relocations.end(), r,
564+
std::equal_range(isec->relocs().begin(), isec->relocs().end(), r,
565565
[](const Relocation &lhs, const Relocation &rhs) {
566566
return lhs.offset < rhs.offset;
567567
});
@@ -950,7 +950,7 @@ void InputSection::relocateNonAlloc(uint8_t *buf, ArrayRef<RelTy> rels) {
950950
static void relocateNonAllocForRelocatable(InputSection *sec, uint8_t *buf) {
951951
const unsigned bits = config->is64 ? 64 : 32;
952952

953-
for (const Relocation &rel : sec->relocations) {
953+
for (const Relocation &rel : sec->relocs()) {
954954
// InputSection::copyRelocations() adds only R_ABS relocations.
955955
assert(rel.expr == R_ABS);
956956
uint8_t *bufLoc = buf + rel.offset;
@@ -1037,7 +1037,7 @@ void InputSectionBase::adjustSplitStackFunctionPrologues(uint8_t *buf,
10371037
DenseSet<Defined *> prologues;
10381038
SmallVector<Relocation *, 0> morestackCalls;
10391039

1040-
for (Relocation &rel : relocations) {
1040+
for (Relocation &rel : relocs()) {
10411041
// Ignore calls into the split-stack api.
10421042
if (rel.sym->getName().startswith("__morestack")) {
10431043
if (rel.sym->getName().equals("__morestack"))

lld/ELF/InputSection.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ class InputSectionBase : public SectionBase {
210210
// This vector contains such "cooked" relocations.
211211
SmallVector<Relocation, 0> relocations;
212212

213+
void addReloc(const Relocation &r) { relocations.push_back(r); }
214+
MutableArrayRef<Relocation> relocs() { return relocations; }
215+
ArrayRef<Relocation> relocs() const { return relocations; }
216+
213217
union {
214218
// These are modifiers to jump instructions that are necessary when basic
215219
// block sections are enabled. Basic block sections creates opportunities

0 commit comments

Comments
 (0)