Skip to content

lld: Target-specific section-folding logic for ICF #133990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions lld/ELF/Arch/AArch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@

#include "InputFiles.h"
#include "OutputSections.h"
#include "Relocations.h"
#include "Symbols.h"
#include "SyntheticSections.h"
#include "Target.h"
#include "lld/Common/ErrorHandler.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/Support/Endian.h"

Expand Down Expand Up @@ -83,6 +86,7 @@ class AArch64 : public TargetInfo {
uint64_t val) const override;
RelExpr adjustTlsExpr(RelType type, RelExpr expr) const override;
void relocateAlloc(InputSectionBase &sec, uint8_t *buf) const override;
bool canFoldSection(const SmallVector<Relocation> &relocs) const override;

private:
void relaxTlsGdToLe(uint8_t *loc, const Relocation &rel, uint64_t val) const;
Expand Down Expand Up @@ -975,6 +979,20 @@ void AArch64::relocateAlloc(InputSectionBase &sec, uint8_t *buf) const {
}
}

bool AArch64::canFoldSection(const SmallVector<Relocation> &relocs) const {
// Section cannot be folded as part of ICF if it contains unpaired relocations
// eg: ADR_GOT_PAGE and LD64_GOT_LO12_NC don't point to same symbol.
SmallSet<Symbol*, 4> syms;
for (const Relocation &reloc : relocs) {
if (reloc.type == R_AARCH64_ADR_GOT_PAGE)
syms.insert(reloc.sym);
else if (reloc.type == R_AARCH64_LD64_GOT_LO12_NC && !syms.contains(reloc.sym))
return false;
}

return true;
}

// AArch64 may use security features in variant PLT sequences. These are:
// Pointer Authentication (PAC), introduced in armv8.3-a and Branch Target
// Indicator (BTI) introduced in armv8.5-a. The additional instructions used
Expand Down
21 changes: 19 additions & 2 deletions lld/ELF/ICF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@
#include "InputFiles.h"
#include "LinkerScript.h"
#include "OutputSections.h"
#include "Relocations.h"
#include "SymbolTable.h"
#include "Symbols.h"
#include "SyntheticSections.h"
#include "Target.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/Object/ELF.h"
#include "llvm/Support/Parallel.h"
Expand Down Expand Up @@ -239,6 +241,7 @@ template <class ELFT>
template <class RelTy>
bool ICF<ELFT>::constantEq(const InputSection *secA, Relocs<RelTy> ra,
const InputSection *secB, Relocs<RelTy> rb) {
SmallVector<Relocation> relocsA, relocsB;
if (ra.size() != rb.size())
return false;
auto rai = ra.begin(), rae = ra.end(), rbi = rb.begin();
Expand All @@ -247,11 +250,21 @@ bool ICF<ELFT>::constantEq(const InputSection *secA, Relocs<RelTy> ra,
rai->getType(ctx.arg.isMips64EL) != rbi->getType(ctx.arg.isMips64EL))
return false;

uint64_t addA = getAddend<ELFT>(*rai);
uint64_t addB = getAddend<ELFT>(*rbi);
int64_t addA = getAddend<ELFT>(*rai);
int64_t addB = getAddend<ELFT>(*rbi);

Symbol &sa = secA->file->getRelocTargetSym(*rai);
Symbol &sb = secB->file->getRelocTargetSym(*rbi);

// We only need relocation type and relocation target symbol. Offset is
// not used (as of now). So we don't bother populating one.
relocsA.push_back(
{ctx.target->getRelExpr(rai->getType(ctx.arg.isMips64EL), sa, 0),
rai->getType(ctx.arg.isMips64EL), 0, addA, &sa});
relocsB.push_back(
{ctx.target->getRelExpr(rbi->getType(ctx.arg.isMips64EL), sb, 0),
rbi->getType(ctx.arg.isMips64EL), 0, addB, &sb});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

happy to either only pass rel.type and Symbol to TargetInfo::canFoldSection but passing Relocation sounded better to me.


if (&sa == &sb) {
if (addA == addB)
continue;
Expand Down Expand Up @@ -308,6 +321,10 @@ bool ICF<ELFT>::constantEq(const InputSection *secA, Relocs<RelTy> ra,
return false;
}

// Target-specific section-folding logic based on section's relocation list
if (!ctx.target->canFoldSection(relocsA) || !ctx.target->canFoldSection(relocsB))
return false;

return true;
}

Expand Down
3 changes: 3 additions & 0 deletions lld/ELF/Target.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class TargetInfo {
virtual void applyJumpInstrMod(uint8_t *loc, JumpModType type,
JumpModType val) const {}

// Used by ICF to determine if section full of @relocs can be folded safely with another
virtual bool canFoldSection(const SmallVector<Relocation> &relocs) const { return true; }

virtual ~TargetInfo();

// This deletes a jump insn at the end of the section if it is a fall thru to
Expand Down
48 changes: 48 additions & 0 deletions lld/test/ELF/aarch64-icf-unpaired.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// REQUIRES: aarch64

# RUN: llvm-mc -filetype=obj -triple=aarch64 %s -o %t
# RUN: ld.lld %t -o %t2 --icf=all --print-icf-sections | FileCheck %s

# CHECK-NOT: selected section {{.*}}(.text.f2_0)
# CHECK-NOT: removing identical section {{.*}}(.text.f2_1)
# CHECK-NOT: removing identical section {{.*}}(.text.f2_2)

.addrsig

callee:
ret

.macro f, index

.section .text.f1_\index,"ax",@progbits
f1_\index:
adrp x0, :got:g\index
mov x1, #\index
b f2_\index

.section .text.f2_\index,"ax",@progbits
f2_\index:
ldr x0, [x0, :got_lo12:g\index]
b callee

.globl g\index
.section .rodata.g\index,"a",@progbits
g_\index:
.long 111
.long 122

g\index:
.byte 123

.section .text._start,"ax",@progbits
bl f1_\index

.endm

.section .text._start,"ax",@progbits
.globl _start
_start:

f 0
f 1
f 2
Loading