Skip to content

Commit bf81bd8

Browse files
committed
[ELF] Pass Ctx &
1 parent 3cb4d20 commit bf81bd8

File tree

5 files changed

+46
-19
lines changed

5 files changed

+46
-19
lines changed

lld/ELF/Arch/ARM.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ class ARM final : public TargetInfo {
4848
bool inBranchRange(RelType type, uint64_t src, uint64_t dst) const override;
4949
void relocate(uint8_t *loc, const Relocation &rel,
5050
uint64_t val) const override;
51+
52+
private:
53+
void encodeAluGroup(uint8_t *loc, const Relocation &rel, uint64_t val,
54+
int group, bool check) const;
5155
};
5256
enum class CodeState { Data = 0, Thumb = 2, Arm = 4 };
5357
} // namespace
@@ -534,8 +538,8 @@ static std::pair<uint32_t, uint32_t> getRemAndLZForGroup(unsigned group,
534538
return {rem, lz};
535539
}
536540

537-
static void encodeAluGroup(uint8_t *loc, const Relocation &rel, uint64_t val,
538-
int group, bool check) {
541+
void ARM::encodeAluGroup(uint8_t *loc, const Relocation &rel, uint64_t val,
542+
int group, bool check) const {
539543
// ADD/SUB (immediate) add = bit23, sub = bit22
540544
// immediate field carries is a 12-bit modified immediate, made up of a 4-bit
541545
// even rotate right and an 8-bit immediate.

lld/ELF/Arch/LoongArch.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ static bool isJirl(uint32_t insn) {
159159
return (insn & 0xfc000000) == JIRL;
160160
}
161161

162-
static void handleUleb128(uint8_t *loc, uint64_t val) {
162+
static void handleUleb128(Ctx &ctx, uint8_t *loc, uint64_t val) {
163163
const uint32_t maxcount = 1 + 64 / 7;
164164
uint32_t count;
165165
const char *error = nullptr;
@@ -700,7 +700,7 @@ void LoongArch::relocate(uint8_t *loc, const Relocation &rel,
700700
write64le(loc, read64le(loc) + val);
701701
return;
702702
case R_LARCH_ADD_ULEB128:
703-
handleUleb128(loc, val);
703+
handleUleb128(ctx, loc, val);
704704
return;
705705
case R_LARCH_SUB6:
706706
*loc = (*loc & 0xc0) | ((*loc - val) & 0x3f);
@@ -718,7 +718,7 @@ void LoongArch::relocate(uint8_t *loc, const Relocation &rel,
718718
write64le(loc, read64le(loc) - val);
719719
return;
720720
case R_LARCH_SUB_ULEB128:
721-
handleUleb128(loc, -val);
721+
handleUleb128(ctx, loc, -val);
722722
return;
723723

724724
case R_LARCH_MARK_LA:

lld/ELF/Arch/X86.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ class X86 : public TargetInfo {
3939

4040
RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override;
4141
void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;
42+
43+
private:
44+
void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
45+
void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
46+
void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
47+
void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
4248
};
4349
} // namespace
4450

@@ -344,7 +350,8 @@ void X86::relocate(uint8_t *loc, const Relocation &rel, uint64_t val) const {
344350
}
345351
}
346352

347-
static void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) {
353+
void X86::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
354+
uint64_t val) const {
348355
if (rel.type == R_386_TLS_GD) {
349356
// Convert (loc[-2] == 0x04)
350357
// leal x@tlsgd(, %ebx, 1), %eax
@@ -379,7 +386,8 @@ static void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) {
379386
}
380387
}
381388

382-
static void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) {
389+
void X86::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
390+
uint64_t val) const {
383391
if (rel.type == R_386_TLS_GD) {
384392
// Convert (loc[-2] == 0x04)
385393
// leal x@tlsgd(, %ebx, 1), %eax
@@ -413,7 +421,8 @@ static void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) {
413421

414422
// In some conditions, relocations can be optimized to avoid using GOT.
415423
// This function does that for Initial Exec to Local Exec case.
416-
static void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, uint64_t val) {
424+
void X86::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
425+
uint64_t val) const {
417426
// Ulrich's document section 6.2 says that @gotntpoff can
418427
// be used with MOVL or ADDL instructions.
419428
// @indntpoff is similar to @gotntpoff, but for use in
@@ -450,7 +459,8 @@ static void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, uint64_t val) {
450459
write32le(loc, val);
451460
}
452461

453-
static void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) {
462+
void X86::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel,
463+
uint64_t val) const {
454464
if (rel.type == R_386_TLS_LDO_32) {
455465
write32le(loc, val);
456466
return;

lld/ELF/Arch/X86_64.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ class X86_64 : public TargetInfo {
5050
bool deleteFallThruJmpInsn(InputSection &is, InputFile *file,
5151
InputSection *nextIS) const override;
5252
bool relaxOnce(int pass) const override;
53+
54+
private:
55+
void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
56+
void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
57+
void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
58+
void relaxTlsIeToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
5359
};
5460
} // namespace
5561

@@ -460,7 +466,8 @@ RelType X86_64::getDynRel(RelType type) const {
460466
return R_X86_64_NONE;
461467
}
462468

463-
static void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) {
469+
void X86_64::relaxTlsGdToLe(uint8_t *loc, const Relocation &rel,
470+
uint64_t val) const {
464471
if (rel.type == R_X86_64_TLSGD) {
465472
// Convert
466473
// .byte 0x66
@@ -500,7 +507,8 @@ static void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) {
500507
}
501508
}
502509

503-
static void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) {
510+
void X86_64::relaxTlsGdToIe(uint8_t *loc, const Relocation &rel,
511+
uint64_t val) const {
504512
if (rel.type == R_X86_64_TLSGD) {
505513
// Convert
506514
// .byte 0x66
@@ -541,7 +549,8 @@ static void relaxTlsGdToIe(uint8_t *loc, const Relocation &rel, uint64_t val) {
541549

542550
// In some conditions, R_X86_64_GOTTPOFF relocation can be optimized to
543551
// R_X86_64_TPOFF32 so that it does not use GOT.
544-
static void relaxTlsIeToLe(uint8_t *loc, const Relocation &, uint64_t val) {
552+
void X86_64::relaxTlsIeToLe(uint8_t *loc, const Relocation &rel,
553+
uint64_t val) const {
545554
uint8_t *inst = loc - 3;
546555
uint8_t reg = loc[-1] >> 3;
547556
uint8_t *regSlot = loc - 1;
@@ -582,7 +591,8 @@ static void relaxTlsIeToLe(uint8_t *loc, const Relocation &, uint64_t val) {
582591
write32le(loc, val + 4);
583592
}
584593

585-
static void relaxTlsLdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) {
594+
void X86_64::relaxTlsLdToLe(uint8_t *loc, const Relocation &rel,
595+
uint64_t val) const {
586596
const uint8_t inst[] = {
587597
0x66, 0x66, // .word 0x6666
588598
0x66, // .byte 0x66

lld/ELF/LinkerScript.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ static void sortSections(MutableArrayRef<InputSectionBase *> vec,
479479
// --sort-section is handled as an inner SORT command.
480480
// 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
481481
// 4. If no SORT command is given, sort according to --sort-section.
482-
static void sortInputSections(MutableArrayRef<InputSectionBase *> vec,
482+
static void sortInputSections(Ctx &ctx, MutableArrayRef<InputSectionBase *> vec,
483483
SortSectionPolicy outer,
484484
SortSectionPolicy inner) {
485485
if (outer == SortSectionPolicy::None)
@@ -517,6 +517,7 @@ LinkerScript::computeInputSections(const InputSectionDescription *cmd,
517517
for (size_t i = begin; i != end; ++i)
518518
ret[i] = sections[indexes[i]];
519519
sortInputSections(
520+
ctx,
520521
MutableArrayRef<InputSectionBase *>(ret).slice(begin, end - begin),
521522
ctx.arg.sortSection, SortSectionPolicy::None);
522523
};
@@ -584,6 +585,7 @@ LinkerScript::computeInputSections(const InputSectionDescription *cmd,
584585
// ret[sizeBeforeCurrPat,ret.size()) are already in the input order, so we
585586
// just sort by sortOuter and sortInner.
586587
sortInputSections(
588+
ctx,
587589
MutableArrayRef<InputSectionBase *>(ret).slice(sizeBeforeCurrPat),
588590
pat.sortOuter, pat.sortInner);
589591
sizeAfterPrevSort = ret.size();
@@ -865,7 +867,8 @@ static OutputDesc *createSection(InputSectionBase *isec, StringRef outsecName) {
865867
return osd;
866868
}
867869

868-
static OutputDesc *addInputSec(StringMap<TinyPtrVector<OutputSection *>> &map,
870+
static OutputDesc *addInputSec(Ctx &ctx,
871+
StringMap<TinyPtrVector<OutputSection *>> &map,
869872
InputSectionBase *isec, StringRef outsecName) {
870873
// Sections with SHT_GROUP or SHF_GROUP attributes reach here only when the -r
871874
// option is given. A section with SHT_GROUP defines a "section group", and
@@ -983,7 +986,7 @@ void LinkerScript::addOrphanSections() {
983986
} else if (OutputSection *sec = findByName(sectionCommands, name)) {
984987
sec->recordSection(s);
985988
} else {
986-
if (OutputDesc *osd = addInputSec(map, s, name))
989+
if (OutputDesc *osd = addInputSec(ctx, map, s, name))
987990
v.push_back(osd);
988991
assert(isa<MergeInputSection>(s) ||
989992
s->getOutputSection()->sectionIndex == UINT32_MAX);
@@ -1114,7 +1117,7 @@ LinkerScript::findMemoryRegion(OutputSection *sec, MemoryRegion *hint) {
11141117
return {nullptr, nullptr};
11151118
}
11161119

1117-
static OutputSection *findFirstSection(PhdrEntry *load) {
1120+
static OutputSection *findFirstSection(Ctx &ctx, PhdrEntry *load) {
11181121
for (OutputSection *sec : ctx.outputSections)
11191122
if (sec->ptLoad == load)
11201123
return sec;
@@ -1187,7 +1190,7 @@ bool LinkerScript::assignOffsets(OutputSection *sec) {
11871190

11881191
// Propagate state->lmaOffset to the first "non-header" section.
11891192
if (PhdrEntry *l = sec->ptLoad)
1190-
if (sec == findFirstSection(l))
1193+
if (sec == findFirstSection(ctx, l))
11911194
l->lmaOffset = state->lmaOffset;
11921195

11931196
// We can call this method multiple times during the creation of
@@ -1462,7 +1465,7 @@ void LinkerScript::allocateHeaders(SmallVector<PhdrEntry *, 0> &phdrs) {
14621465

14631466
ctx.out.elfHeader->ptLoad = nullptr;
14641467
ctx.out.programHeaders->ptLoad = nullptr;
1465-
firstPTLoad->firstSec = findFirstSection(firstPTLoad);
1468+
firstPTLoad->firstSec = findFirstSection(ctx, firstPTLoad);
14661469

14671470
llvm::erase_if(phdrs,
14681471
[](const PhdrEntry *e) { return e->p_type == PT_PHDR; });

0 commit comments

Comments
 (0)