Skip to content

Commit 255ea48

Browse files
committed
[ELF] Merge verdefIndex into versionId. NFC (#72208)
The two fields are similar. `versionId` is the Verdef index in the output file. It is set for `--exclude-id=`, version script patterns, and `sym@ver` symbols. `verdefIndex` is the Verdef index of a Sharedfile (SharedSymbol or a copy-relocated Defined), the default value -1 is also used to indicate that the symbol has not been matched by a version script pattern (https://reviews.llvm.org/D65716). It seems confusing to have two fields. Merge them so that we can allocate one bit for #70130 (suppress --no-allow-shlib-undefined error in the presence of a DSO definition).
1 parent c95d581 commit 255ea48

File tree

6 files changed

+28
-28
lines changed

6 files changed

+28
-28
lines changed

lld/ELF/InputFiles.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,7 +1546,7 @@ template <class ELFT> void SharedFile::parse() {
15461546
SharedSymbol{*this, name, sym.getBinding(), sym.st_other,
15471547
sym.getType(), sym.st_value, sym.st_size, alignment});
15481548
if (s->file == this)
1549-
s->verdefIndex = ver;
1549+
s->versionId = ver;
15501550
}
15511551

15521552
// Also add the symbol with the versioned name to handle undefined symbols
@@ -1563,7 +1563,7 @@ template <class ELFT> void SharedFile::parse() {
15631563
SharedSymbol{*this, saver().save(name), sym.getBinding(), sym.st_other,
15641564
sym.getType(), sym.st_value, sym.st_size, alignment});
15651565
if (s->file == this)
1566-
s->verdefIndex = idx;
1566+
s->versionId = idx;
15671567
}
15681568
}
15691569

lld/ELF/Relocations.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ static void replaceWithDefined(Symbol &sym, SectionBase &sec, uint64_t value,
309309
size, &sec)
310310
.overwrite(sym);
311311

312-
sym.verdefIndex = old.verdefIndex;
312+
sym.versionId = old.versionId;
313313
sym.exportDynamic = true;
314314
sym.isUsedInRegularObj = true;
315315
// A copy relocated alias may need a GOT entry.

lld/ELF/SymbolTable.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ Symbol *SymbolTable::insert(StringRef name) {
9292
memset(sym, 0, sizeof(Symbol));
9393
sym->setName(name);
9494
sym->partition = 1;
95-
sym->verdefIndex = -1;
9695
sym->versionId = VER_NDX_GLOBAL;
9796
if (pos != StringRef::npos)
9897
sym->hasVersionSuffix = true;
@@ -235,10 +234,9 @@ bool SymbolTable::assignExactVersion(SymbolVersion ver, uint16_t versionId,
235234
sym->getName().contains('@'))
236235
continue;
237236

238-
// If the version has not been assigned, verdefIndex is -1. Use an arbitrary
239-
// number (0) to indicate the version has been assigned.
240-
if (sym->verdefIndex == uint16_t(-1)) {
241-
sym->verdefIndex = 0;
237+
// If the version has not been assigned, assign versionId to the symbol.
238+
if (!sym->versionScriptAssigned) {
239+
sym->versionScriptAssigned = true;
242240
sym->versionId = versionId;
243241
}
244242
if (sym->versionId == versionId)
@@ -256,8 +254,8 @@ void SymbolTable::assignWildcardVersion(SymbolVersion ver, uint16_t versionId,
256254
// so we set a version to a symbol only if no version has been assigned
257255
// to the symbol. This behavior is compatible with GNU.
258256
for (Symbol *sym : findAllByVersion(ver, includeNonDefault))
259-
if (sym->verdefIndex == uint16_t(-1)) {
260-
sym->verdefIndex = 0;
257+
if (!sym->versionScriptAssigned) {
258+
sym->versionScriptAssigned = true;
261259
sym->versionId = versionId;
262260
}
263261
}

lld/ELF/Symbols.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,3 +683,13 @@ void Symbol::resolve(const SharedSymbol &other) {
683683
} else if (traced)
684684
printTraceSymbol(other, getName());
685685
}
686+
687+
void Defined::overwrite(Symbol &sym) const {
688+
if (isa_and_nonnull<SharedFile>(sym.file))
689+
sym.versionId = VER_NDX_GLOBAL;
690+
Symbol::overwrite(sym, DefinedKind);
691+
auto &s = static_cast<Defined &>(sym);
692+
s.value = value;
693+
s.size = size;
694+
s.section = section;
695+
}

lld/ELF/Symbols.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,12 @@ class Symbol {
313313
uint32_t auxIdx;
314314
uint32_t dynsymIndex;
315315

316-
// This field is a index to the symbol's version definition.
317-
uint16_t verdefIndex;
318-
319-
// Version definition index.
316+
// If `file` is SharedFile (for SharedSymbol or copy-relocated Defined), this
317+
// represents the Verdef index within the input DSO, which will be converted
318+
// to a Verneed index in the output. Otherwise, this represents the Verdef
319+
// index (VER_NDX_LOCAL, VER_NDX_GLOBAL, or a named version).
320320
uint16_t versionId;
321+
uint8_t versionScriptAssigned : 1;
321322

322323
void setFlags(uint16_t bits) {
323324
flags.fetch_or(bits, std::memory_order_relaxed);
@@ -355,14 +356,7 @@ class Defined : public Symbol {
355356
size(size), section(section) {
356357
exportDynamic = config->exportDynamic;
357358
}
358-
void overwrite(Symbol &sym) const {
359-
Symbol::overwrite(sym, DefinedKind);
360-
sym.verdefIndex = -1;
361-
auto &s = static_cast<Defined &>(sym);
362-
s.value = value;
363-
s.size = size;
364-
s.section = section;
365-
}
359+
void overwrite(Symbol &sym) const;
366360

367361
static bool classof(const Symbol *s) { return s->isDefined(); }
368362

lld/ELF/SyntheticSections.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3140,10 +3140,8 @@ bool VersionTableSection::isNeeded() const {
31403140

31413141
void elf::addVerneed(Symbol *ss) {
31423142
auto &file = cast<SharedFile>(*ss->file);
3143-
if (ss->verdefIndex == VER_NDX_GLOBAL) {
3144-
ss->versionId = VER_NDX_GLOBAL;
3143+
if (ss->versionId == VER_NDX_GLOBAL)
31453144
return;
3146-
}
31473145

31483146
if (file.vernauxs.empty())
31493147
file.vernauxs.resize(file.verdefs.size());
@@ -3152,10 +3150,10 @@ void elf::addVerneed(Symbol *ss) {
31523150
// already allocated one. The verdef identifiers cover the range
31533151
// [1..getVerDefNum()]; this causes the vernaux identifiers to start from
31543152
// getVerDefNum()+1.
3155-
if (file.vernauxs[ss->verdefIndex] == 0)
3156-
file.vernauxs[ss->verdefIndex] = ++SharedFile::vernauxNum + getVerDefNum();
3153+
if (file.vernauxs[ss->versionId] == 0)
3154+
file.vernauxs[ss->versionId] = ++SharedFile::vernauxNum + getVerDefNum();
31573155

3158-
ss->versionId = file.vernauxs[ss->verdefIndex];
3156+
ss->versionId = file.vernauxs[ss->versionId];
31593157
}
31603158

31613159
template <class ELFT>

0 commit comments

Comments
 (0)