Skip to content

Commit 4d5dffb

Browse files
MaskRayc-rhodes
authored andcommitted
[ELF] Initialize Symbol fields in the constructor instead of via memset (#198129)
`initSectionsAndLocalSyms` and `makeDefined` memset the storage to zero and then placement-new a Symbol-derived object into it. Placement new begins a new object's lifetime. The standard does not seem to guarantee the memset bytes carry into members the constructor leaves uninitialized. lld built by GCC 16 can make Valgrind report reads of Symbol::flags (via getSymSectionIndex during finalizeSections) as uses of uninitialized values (ClangBuiltLinux/linux#2162). This patch reinstates the per-field initialization that commit 7787427 ("[ELF] Avoid redundant assignment to Symbol fields. NFC") had replaced with a bulk memset. (cherry picked from commit 905a88b)
1 parent 823afe0 commit 4d5dffb

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

‎lld/ELF/InputFiles.cpp‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,7 +1261,6 @@ void ObjFile<ELFT>::initSectionsAndLocalSyms(bool ignoreComdats) {
12611261
if (!firstGlobal)
12621262
return;
12631263
SymbolUnion *locals = makeThreadLocalN<SymbolUnion>(firstGlobal);
1264-
memset(locals, 0, sizeof(SymbolUnion) * firstGlobal);
12651264

12661265
ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();
12671266
for (size_t i = 0, end = firstGlobal; i != end; ++i) {
@@ -1297,7 +1296,6 @@ void ObjFile<ELFT>::initSectionsAndLocalSyms(bool ignoreComdats) {
12971296
else
12981297
new (symbols[i]) Defined(ctx, this, name, STB_LOCAL, eSym.st_other, type,
12991298
eSym.st_value, eSym.st_size, sec);
1300-
symbols[i]->partition = 1;
13011299
symbols[i]->isUsedInRegularObj = true;
13021300
}
13031301
}

‎lld/ELF/Symbols.h‎

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class Symbol {
102102
uint8_t symbolKind;
103103

104104
// The partition whose dynamic symbol table contains this symbol's definition.
105-
uint8_t partition;
105+
uint8_t partition = 1;
106106

107107
// True if this symbol is preemptible at load time.
108108
//
@@ -242,8 +242,13 @@ class Symbol {
242242
Symbol(Kind k, InputFile *file, StringRef name, uint8_t binding,
243243
uint8_t stOther, uint8_t type)
244244
: file(file), nameData(name.data()), nameSize(name.size()), type(type),
245-
binding(binding), stOther(stOther), symbolKind(k), ltoCanOmit(false),
246-
archSpecificBit(false) {}
245+
binding(binding), stOther(stOther), symbolKind(k), isPreemptible(false),
246+
isUsedInRegularObj(false), isExported(false), ltoCanOmit(false),
247+
traced(false), hasVersionSuffix(false), isInIplt(false),
248+
gotInIgot(false), folded(false), archSpecificBit(false),
249+
scriptDefined(false), dsoDefined(false), dsoProtected(false),
250+
versionScriptAssigned(false), thunkAccessed(false),
251+
inDynamicList(false), referenced(false), referencedAfterWrap(false) {}
247252

248253
void overwrite(Symbol &sym, Kind k) const {
249254
if (sym.traced)
@@ -302,20 +307,20 @@ class Symbol {
302307

303308
// Temporary flags used to communicate which symbol entries need PLT and GOT
304309
// entries during postScanRelocations();
305-
std::atomic<uint16_t> flags;
310+
std::atomic<uint16_t> flags = 0;
306311

307312
// A ctx.symAux index used to access GOT/PLT entry indexes. This is allocated
308313
// in postScanRelocations().
309-
uint32_t auxIdx;
310-
uint32_t dynsymIndex;
314+
uint32_t auxIdx = 0;
315+
uint32_t dynsymIndex = 0;
311316

312317
// If `file` is SharedFile (for SharedSymbol or copy-relocated Defined), this
313318
// represents the Verdef index within the input DSO, which will be converted
314319
// to a Verneed index in the output. Otherwise, this represents the Verdef
315320
// index (VER_NDX_LOCAL, VER_NDX_GLOBAL, or a named version).
316321
// VER_NDX_LOCAL indicates a defined symbol that has been localized by a
317322
// version script's local: directive or --exclude-libs.
318-
uint16_t versionId;
323+
uint16_t versionId = 0;
319324
LLVM_PREFERRED_TYPE(bool)
320325
uint8_t versionScriptAssigned : 1;
321326

@@ -526,7 +531,6 @@ union SymbolUnion {
526531

527532
template <typename... T> Defined *makeDefined(T &&...args) {
528533
auto *sym = getSpecificAllocSingleton<SymbolUnion>().Allocate();
529-
memset(sym, 0, sizeof(Symbol));
530534
auto &s = *new (reinterpret_cast<Defined *>(sym)) Defined(std::forward<T>(args)...);
531535
return &s;
532536
}

0 commit comments

Comments
 (0)