[lld] [ELF] Synthesize STT_FILE if necessary (PR #209087)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 14 22:20:04 PDT 2026


================
@@ -2003,34 +2003,59 @@ void SymbolTableBaseSection::finalizeContents() {
     s.sym->dynsymIndex = ++i;
 }
 
-// The ELF spec requires that all local symbols precede global symbols, so we
-// sort symbol entries in this function. (For .dynsym, we don't do that because
-// symbols for dynamic linking are inherently all globals.)
+// The ELF spec requires local symbols to precede globals. We additionally group
+// the locals by file, each led by its first STT_FILE.
 //
-// Aside from above, we put local symbols in groups starting with the STT_FILE
-// symbol. That is convenient for purpose of identifying where are local symbols
-// coming from.
+// symbols[firstGlobalIdx, synthSttFileIdx) can be converted local: move them
+// after the per-file groups, behind the synthetic STT_FILE synthSttFileSym.
+// Locals added later (e.g. thunks) fall outside that range and stay in their
+// file's group.
 void SymbolTableBaseSection::sortSymTabSymbols() {
-  // Move all local symbols before global symbols.
-  auto e = std::stable_partition(
-      symbols.begin(), symbols.end(),
-      [](const SymbolTableEntry &s) { return s.sym->isLocal(); });
-  size_t numLocals = e - symbols.begin();
-  getParent()->info = numLocals + 1;
-
-  // We want to group the local symbols by file. For that we rebuild the local
-  // part of the symbols vector. We do not need to care about the STT_FILE
-  // symbols, they are already naturally placed first in each group. That
-  // happens because STT_FILE is always the first symbol in the object and hence
-  // precede all other local symbols we add for a file.
   MapVector<InputFile *, SmallVector<SymbolTableEntry, 0>> arr;
----------------
MaskRay wrote:

Done

https://github.com/llvm/llvm-project/pull/209087


More information about the llvm-commits mailing list