Skip to content

Commit a271f24

Browse files
committed
[lld-macho][nfc] Canonicalize all pointers to InputSections early on
Having to remember to call `canonical()` all over the place is error-prone; let's do it in a centralized location instead. It also appears to improve performance slightly. base diff difference (95% CI) sys_time 0.984 ± 0.009 0.983 ± 0.014 [ -0.8% .. +0.6%] user_time 6.508 ± 0.035 6.475 ± 0.036 [ -0.8% .. -0.2%] wall_time 5.321 ± 0.034 5.300 ± 0.033 [ -0.7% .. -0.1%] samples 36 23 Reviewed By: #lld-macho, thakis Differential Revision: https://reviews.llvm.org/D112687
1 parent 86972f1 commit a271f24

File tree

5 files changed

+29
-11
lines changed

5 files changed

+29
-11
lines changed

lld/MachO/InputSection.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class ConcatInputSection final : public InputSection {
122122
void writeTo(uint8_t *buf);
123123

124124
void foldIdentical(ConcatInputSection *redundant);
125-
InputSection *canonical() override {
125+
ConcatInputSection *canonical() override {
126126
return replacement ? replacement : this;
127127
}
128128

@@ -131,7 +131,7 @@ class ConcatInputSection final : public InputSection {
131131
}
132132

133133
// Points to the surviving section after this one is folded by ICF
134-
InputSection *replacement = nullptr;
134+
ConcatInputSection *replacement = nullptr;
135135
// Equivalence-class ID for ICF
136136
uint64_t icfEqClass[2] = {0, 0};
137137

lld/MachO/Symbols.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ uint64_t Defined::getVA() const {
6666
if (isAbsolute())
6767
return value;
6868

69-
if (!isec->canonical()->isFinal) {
69+
if (!isec->isFinal) {
7070
// A target arch that does not use thunks ought never ask for
7171
// the address of a function that has not yet been finalized.
7272
assert(target->usesThunks());
@@ -77,7 +77,14 @@ uint64_t Defined::getVA() const {
7777
// expedient to return a contrived out-of-range address.
7878
return TargetInfo::outOfRangeVA;
7979
}
80-
return isec->canonical()->getVA(value);
80+
return isec->getVA(value);
81+
}
82+
83+
void Defined::canonicalize() {
84+
if (compactUnwind)
85+
compactUnwind = compactUnwind->canonical();
86+
if (isec)
87+
isec = isec->canonical();
8188
}
8289

8390
uint64_t DylibSymbol::getVA() const {

lld/MachO/Symbols.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ class Defined : public Symbol {
126126

127127
uint64_t getVA() const override;
128128

129+
// Ensure this symbol's pointers to InputSections point to their canonical
130+
// copies.
131+
void canonicalize();
132+
129133
static bool classof(const Symbol *s) { return s->kind() == DefinedKind; }
130134

131135
InputSection *isec;

lld/MachO/SyntheticSections.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ void SymtabSection::emitStabs() {
924924
}
925925

926926
StabsEntry symStab;
927-
symStab.sect = defined->isec->canonical()->parent->index;
927+
symStab.sect = defined->isec->parent->index;
928928
symStab.strx = stringTableSection.addString(defined->getName());
929929
symStab.value = defined->getVA();
930930

@@ -1049,7 +1049,7 @@ template <class LP> void SymtabSectionImpl<LP>::writeTo(uint8_t *buf) const {
10491049
nList->n_value = defined->value;
10501050
} else {
10511051
nList->n_type = scope | N_SECT;
1052-
nList->n_sect = defined->isec->canonical()->parent->index;
1052+
nList->n_sect = defined->isec->parent->index;
10531053
// For the N_SECT symbol type, n_value is the address of the symbol
10541054
nList->n_value = defined->getVA();
10551055
}

lld/MachO/Writer.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -671,10 +671,11 @@ void Writer::scanRelocations() {
671671

672672
void Writer::scanSymbols() {
673673
TimeTraceScope timeScope("Scan symbols");
674-
for (const Symbol *sym : symtab->getSymbols()) {
675-
if (const auto *defined = dyn_cast<Defined>(sym)) {
674+
for (Symbol *sym : symtab->getSymbols()) {
675+
if (auto *defined = dyn_cast<Defined>(sym)) {
676676
if (!defined->isLive())
677677
continue;
678+
defined->canonicalize();
678679
if (defined->overridesWeakDef)
679680
in.weakBinding->addNonWeakDefinition(defined);
680681
if (!defined->isAbsolute() && isCodeSection(defined->isec))
@@ -691,10 +692,14 @@ void Writer::scanSymbols() {
691692
for (const InputFile *file : inputFiles) {
692693
if (auto *objFile = dyn_cast<ObjFile>(file))
693694
for (Symbol *sym : objFile->symbols) {
694-
if (auto *defined = dyn_cast_or_null<Defined>(sym))
695-
if (!defined->isExternal() && defined->isLive() &&
696-
!defined->isAbsolute() && isCodeSection(defined->isec))
695+
if (auto *defined = dyn_cast_or_null<Defined>(sym)) {
696+
if (!defined->isLive())
697+
continue;
698+
defined->canonicalize();
699+
if (!defined->isExternal() && !defined->isAbsolute() &&
700+
isCodeSection(defined->isec))
697701
in.unwindInfo->addSymbol(defined);
702+
}
698703
}
699704
}
700705
}
@@ -1120,6 +1125,8 @@ template <class LP> void Writer::run() {
11201125
treatSpecialUndefineds();
11211126
if (config->entry && !isa<Undefined>(config->entry))
11221127
prepareBranchTarget(config->entry);
1128+
// Canonicalization of all pointers to InputSections should be handled by
1129+
// these two methods.
11231130
scanSymbols();
11241131
scanRelocations();
11251132

0 commit comments

Comments
 (0)