@@ -2470,6 +2470,12 @@ static void readSymbolPartitionSection(InputSectionBase *s) {
24702470 sym->partition = newPart.getNumber ();
24712471}
24722472
2473+ static Symbol *addUnusedUndefined (StringRef name,
2474+ uint8_t binding = STB_GLOBAL) {
2475+ return symtab.addSymbol (
2476+ Undefined{ctx.internalFile , name, binding, STV_DEFAULT, 0 });
2477+ }
2478+
24732479static void markBuffersAsDontNeed (bool skipLinkedOutput) {
24742480 // With --thinlto-index-only, all buffers are nearly unused from now on
24752481 // (except symbol/section names used by infrequent passes). Mark input file
@@ -2556,19 +2562,19 @@ static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &args) {
25562562 continue ;
25572563
25582564 Symbol *wrap =
2559- symtab. addUnusedUndefined (saver ().save (" __wrap_" + name), sym->binding );
2565+ addUnusedUndefined (saver ().save (" __wrap_" + name), sym->binding );
25602566
25612567 // If __real_ is referenced, pull in the symbol if it is lazy. Do this after
25622568 // processing __wrap_ as that may have referenced __real_.
25632569 StringRef realName = saver ().save (" __real_" + name);
25642570 if (Symbol *real = symtab.find (realName)) {
2565- symtab. addUnusedUndefined (name, sym->binding );
2571+ addUnusedUndefined (name, sym->binding );
25662572 // Update sym's binding, which will replace real's later in
25672573 // SymbolTable::wrap.
25682574 sym->binding = real->binding ;
25692575 }
25702576
2571- Symbol *real = symtab. addUnusedUndefined (realName);
2577+ Symbol *real = addUnusedUndefined (realName);
25722578 v.push_back ({sym, real, wrap});
25732579
25742580 // We want to tell LTO not to inline symbols to be overwritten
@@ -2845,14 +2851,20 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &args) {
28452851 // Handle -u/--undefined before input files. If both a.a and b.so define foo,
28462852 // -u foo a.a b.so will extract a.a.
28472853 for (StringRef name : config->undefined )
2848- symtab. addUnusedUndefined (name)->referenced = true ;
2854+ addUnusedUndefined (name)->referenced = true ;
28492855
28502856 parseFiles (files, armCmseImpLib);
28512857
28522858 // Create dynamic sections for dynamic linking and static PIE.
28532859 config->hasDynSymTab = !ctx.sharedFiles .empty () || config->isPic ;
28542860
2855- script->addScriptReferencedSymbolsToSymTable ();
2861+ // Some symbols (such as __ehdr_start) are defined lazily only when there
2862+ // are undefined symbols for them, so we add these to trigger that logic.
2863+ for (StringRef name : script->referencedSymbols ) {
2864+ Symbol *sym = addUnusedUndefined (name);
2865+ sym->isUsedInRegularObj = true ;
2866+ sym->referenced = true ;
2867+ }
28562868
28572869 // Prevent LTO from removing any definition referenced by -u.
28582870 for (StringRef name : config->undefined )
0 commit comments