Skip to content

Commit 8ca46bb

Browse files
committed
[ELF] Move isUsedInRegularObj assignment from ctor to call sites. NFC
This removes the tricky `isUsedInRegularObj(!file || file->kind() == InputFile::ObjKind)` and the copy from `Symbol::mergeProperties`.
1 parent 5b7ac10 commit 8ca46bb

File tree

7 files changed

+20
-28
lines changed

7 files changed

+20
-28
lines changed

lld/ELF/Driver.cpp

+2-9
Original file line numberDiff line numberDiff line change
@@ -1958,16 +1958,9 @@ static void readSymbolPartitionSection(InputSectionBase *s) {
19581958
sym->partition = newPart.getNumber();
19591959
}
19601960

1961-
static Symbol *addUndefined(StringRef name) {
1962-
return symtab->addSymbol(
1963-
Undefined{nullptr, name, STB_GLOBAL, STV_DEFAULT, 0});
1964-
}
1965-
19661961
static Symbol *addUnusedUndefined(StringRef name,
19671962
uint8_t binding = STB_GLOBAL) {
1968-
Undefined sym{nullptr, name, binding, STV_DEFAULT, 0};
1969-
sym.isUsedInRegularObj = false;
1970-
return symtab->addSymbol(sym);
1963+
return symtab->addSymbol(Undefined{nullptr, name, binding, STV_DEFAULT, 0});
19711964
}
19721965

19731966
static void markBuffersAsDontNeed(bool skipLinkedOutput) {
@@ -2319,7 +2312,7 @@ void LinkerDriver::link(opt::InputArgList &args) {
23192312
// Some symbols (such as __ehdr_start) are defined lazily only when there
23202313
// are undefined symbols for them, so we add these to trigger that logic.
23212314
for (StringRef name : script->referencedSymbols)
2322-
addUndefined(name);
2315+
addUnusedUndefined(name)->isUsedInRegularObj = true;
23232316

23242317
// Prevent LTO from removing any definition referenced by -u.
23252318
for (StringRef name : config->undefined)

lld/ELF/InputFiles.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -1054,6 +1054,7 @@ void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
10541054
else
10551055
new (symbols[i]) Defined(this, name, STB_LOCAL, eSym.st_other, type,
10561056
eSym.st_value, eSym.st_size, sec);
1057+
symbols[i]->isUsedInRegularObj = true;
10571058
}
10581059

10591060
// Some entries have been filled by LazyObjFile.
@@ -1091,6 +1092,7 @@ void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
10911092
uint64_t size = eSym.st_size;
10921093

10931094
Symbol *sym = symbols[i];
1095+
sym->isUsedInRegularObj = true;
10941096
if (LLVM_UNLIKELY(eSym.st_shndx == SHN_COMMON)) {
10951097
if (value == 0 || value >= UINT32_MAX)
10961098
fatal(toString(this) + ": common symbol '" + sym->getName() +
@@ -1113,12 +1115,9 @@ void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
11131115
// extract. We should demote the lazy symbol to an Undefined so that any
11141116
// relocations outside of the group to it will trigger a discarded section
11151117
// error.
1116-
if (sym->symbolKind == Symbol::LazyObjectKind && !sym->file->lazy) {
1118+
if (sym->symbolKind == Symbol::LazyObjectKind && !sym->file->lazy)
11171119
sym->replace(und);
1118-
// Prevent LTO from internalizing the symbol in case there is a
1119-
// reference to this symbol from this file.
1120-
sym->isUsedInRegularObj = true;
1121-
} else
1120+
else
11221121
sym->resolve(und);
11231122
continue;
11241123
}
@@ -1145,6 +1144,7 @@ void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {
11451144
Symbol *sym = symbols[i];
11461145
sym->resolve(Undefined{this, StringRef(), eSym.getBinding(), eSym.st_other,
11471146
eSym.getType()});
1147+
sym->isUsedInRegularObj = true;
11481148
sym->referenced = true;
11491149
}
11501150
}

lld/ELF/LinkerScript.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ void LinkerScript::addSymbol(SymbolAssignment *cmd) {
239239
Symbol *sym = symtab->insert(cmd->name);
240240
sym->mergeProperties(newSym);
241241
sym->replace(newSym);
242+
sym->isUsedInRegularObj = true;
242243
cmd->sym = cast<Defined>(sym);
243244
}
244245

@@ -259,6 +260,7 @@ static void declareSymbol(SymbolAssignment *cmd) {
259260

260261
cmd->sym = cast<Defined>(sym);
261262
cmd->provide = false;
263+
sym->isUsedInRegularObj = true;
262264
sym->scriptDefined = true;
263265
}
264266

lld/ELF/SymbolTable.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ Symbol *SymbolTable::addAndCheckDuplicate(const Defined &newSym) {
110110
if (sym->isDefined())
111111
sym->checkDuplicate(newSym);
112112
sym->resolve(newSym);
113+
sym->isUsedInRegularObj = true;
113114
return sym;
114115
}
115116

lld/ELF/Symbols.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,6 @@ static uint8_t getMinVisibility(uint8_t va, uint8_t vb) {
385385
void Symbol::mergeProperties(const Symbol &other) {
386386
if (other.exportDynamic)
387387
exportDynamic = true;
388-
if (other.isUsedInRegularObj)
389-
isUsedInRegularObj = true;
390388

391389
// DSO symbols do not affect visibility in the output.
392390
if (!other.isShared())

lld/ELF/Symbols.h

+6-9
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,11 @@ class Symbol {
238238
: file(file), nameData(name.data()), nameSize(name.size()),
239239
binding(binding), type(type), stOther(stOther), symbolKind(k),
240240
visibility(stOther & 3), isPreemptible(false),
241-
isUsedInRegularObj(!file || file->kind() == InputFile::ObjKind),
242-
used(false), exportDynamic(false), inDynamicList(false),
243-
referenced(false), traced(false), hasVersionSuffix(false),
244-
isInIplt(false), gotInIgot(false), folded(false),
245-
needsTocRestore(false), scriptDefined(false), needsCopy(false),
246-
needsGot(false), needsPlt(false), needsTlsDesc(false),
241+
isUsedInRegularObj(false), used(false), exportDynamic(false),
242+
inDynamicList(false), referenced(false), traced(false),
243+
hasVersionSuffix(false), isInIplt(false), gotInIgot(false),
244+
folded(false), needsTocRestore(false), scriptDefined(false),
245+
needsCopy(false), needsGot(false), needsPlt(false), needsTlsDesc(false),
247246
needsTlsGd(false), needsTlsGdToIe(false), needsGotDtprel(false),
248247
needsTlsIe(false), hasDirectReloc(false) {}
249248

@@ -427,9 +426,7 @@ class LazyObject : public Symbol {
427426
public:
428427
LazyObject(InputFile &file)
429428
: Symbol(LazyObjectKind, &file, {}, llvm::ELF::STB_GLOBAL,
430-
llvm::ELF::STV_DEFAULT, llvm::ELF::STT_NOTYPE) {
431-
isUsedInRegularObj = false;
432-
}
429+
llvm::ELF::STV_DEFAULT, llvm::ELF::STT_NOTYPE) {}
433430

434431
static bool classof(const Symbol *s) { return s->kind() == LazyObjectKind; }
435432
};

lld/ELF/Writer.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,14 @@ static Defined *addOptionalRegular(StringRef name, SectionBase *sec,
171171

172172
s->resolve(Defined{nullptr, StringRef(), STB_GLOBAL, stOther, STT_NOTYPE, val,
173173
/*size=*/0, sec});
174+
s->isUsedInRegularObj = true;
174175
return cast<Defined>(s);
175176
}
176177

177178
static Defined *addAbsolute(StringRef name) {
178179
Symbol *sym = symtab->addSymbol(Defined{nullptr, name, STB_GLOBAL, STV_HIDDEN,
179180
STT_NOTYPE, 0, 0, nullptr});
181+
sym->isUsedInRegularObj = true;
180182
return cast<Defined>(sym);
181183
}
182184

@@ -1830,9 +1832,8 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
18301832
// Even the author of gold doesn't remember why gold behaves that way.
18311833
// https://sourceware.org/ml/binutils/2002-03/msg00360.html
18321834
if (mainPart->dynamic->parent)
1833-
symtab->addSymbol(
1834-
Defined{/*file=*/nullptr, "_DYNAMIC", STB_WEAK, STV_HIDDEN, STT_NOTYPE,
1835-
/*value=*/0, /*size=*/0, mainPart->dynamic.get()});
1835+
symtab->addSymbol(Defined{/*file=*/nullptr, "_DYNAMIC", STB_WEAK, STV_HIDDEN, STT_NOTYPE,
1836+
/*value=*/0, /*size=*/0, mainPart->dynamic.get()})->isUsedInRegularObj = true;
18361837

18371838
// Define __rel[a]_iplt_{start,end} symbols if needed.
18381839
addRelIpltSymbols();

0 commit comments

Comments
 (0)