Skip to content

Commit dc4df5d

Browse files
authored
[ELF] Always separate relative relocations regardless of -z combreloc (llvm#187964)
Remove the combreloc guard from addReloc and mergeRels so that relative relocations are always routed to relativeRelocs, even with -z nocombreloc or --pack-dyn-relocs=android. Update AndroidPackedRelocationSection::updateAllocSize to iterate both relativeRelocs and relocs.
1 parent 076226f commit dc4df5d

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

lld/ELF/SyntheticSections.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,9 +1519,11 @@ void RelocationBaseSection::mergeRels() {
15191519

15201520
void RelocationBaseSection::finalizeContents() {
15211521
mergeRels();
1522-
// Cache the count for DT_RELACOUNT. This must not change after
1523-
// DynamicSection::finalizeContents sizes the .dynamic section.
1524-
numRelativeRelocs = relativeRelocs.size();
1522+
// Cache the count for DT_RELACOUNT. DynamicSection<ELFT>::computeContents
1523+
// uses ctx.arg.zCombreloc (not the per-section combreloc) to decide whether
1524+
// to emit DT_RELACOUNT, so this must match.
1525+
if (combreloc)
1526+
numRelativeRelocs = relativeRelocs.size();
15251527
SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
15261528

15271529
// When linking glibc statically, .rel{,a}.plt contains R_*_IRELATIVE
@@ -1685,23 +1687,22 @@ bool AndroidPackedRelocationSection<ELFT>::updateAllocSize(Ctx &ctx) {
16851687
// The format header includes the number of relocations and the initial
16861688
// offset (we set this to zero because the first relocation group will
16871689
// perform the initial adjustment).
1688-
add(relocs.size());
1690+
add(relativeRelocs.size() + relocs.size());
16891691
add(0);
16901692

1691-
std::vector<Elf_Rela> relatives, nonRelatives;
1692-
1693-
for (const DynamicReloc &rel : relocs) {
1693+
SymbolTableBaseSection *symTab = getPartition(ctx).dynSymTab.get();
1694+
auto makeRela = [&](const DynamicReloc &rel) {
16941695
Elf_Rela r;
16951696
r.r_offset = rel.getOffset();
1696-
r.setSymbolAndType(rel.getSymIndex(getPartition(ctx).dynSymTab.get()),
1697-
rel.type, false);
1697+
r.setSymbolAndType(rel.getSymIndex(symTab), rel.type, false);
16981698
r.r_addend = ctx.arg.isRela ? rel.computeAddend(ctx) : 0;
1699-
1700-
if (r.getType(ctx.arg.isMips64EL) == ctx.target->relativeRel)
1701-
relatives.push_back(r);
1702-
else
1703-
nonRelatives.push_back(r);
1704-
}
1699+
return r;
1700+
};
1701+
std::vector<Elf_Rela> relatives, nonRelatives;
1702+
for (const DynamicReloc &rel : relativeRelocs)
1703+
relatives.push_back(makeRela(rel));
1704+
for (const DynamicReloc &rel : relocs)
1705+
nonRelatives.push_back(makeRela(rel));
17051706

17061707
llvm::sort(relatives, [](const Elf_Rel &a, const Elf_Rel &b) {
17071708
return a.r_offset < b.r_offset;

lld/ELF/SyntheticSections.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ class RelocationBaseSection : public SyntheticSection {
495495
/// This overload can be used if the addends are written directly instead of
496496
/// using relocations on the input section (e.g. MipsGotSection::writeTo()).
497497
template <bool shard = false> void addReloc(const DynamicReloc &reloc) {
498-
if (combreloc && reloc.type == relativeRel)
498+
if (reloc.type == relativeRel)
499499
relativeRelocs.push_back(reloc);
500500
else
501501
relocs.push_back(reloc);

lld/test/ELF/combreloc.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@
3131
# NOCOMB-NOT: RELACOUNT
3232
# NOCOMB: Relocations [
3333
# NOCOMB-NEXT: Section ({{.*}}) .rela.dyn {
34+
# NOCOMB-NEXT: 0x3418 R_X86_64_RELATIVE - 0x3420
3435
# NOCOMB-NEXT: 0x33F8 R_X86_64_64 aaa 0x0
3536
# NOCOMB-NEXT: 0x3400 R_X86_64_64 ccc 0x0
3637
# NOCOMB-NEXT: 0x3408 R_X86_64_64 bbb 0x0
3738
# NOCOMB-NEXT: 0x3410 R_X86_64_64 aaa 0x0
3839
# NOCOMB-NEXT: 0x23F0 R_X86_64_GLOB_DAT aaa 0x0
39-
# NOCOMB-NEXT: 0x3418 R_X86_64_RELATIVE - 0x3420
4040
# NOCOMB-NEXT: }
4141

4242
.globl aaa, bbb, ccc

0 commit comments

Comments
 (0)