@@ -874,19 +874,43 @@ static void createOptionalSymbols() {
874
874
WasmSym::tlsBase = createOptionalGlobal (" __tls_base" , false );
875
875
}
876
876
877
+ static void processStubLibrariesPreLTO () {
878
+ log (" -- processStubLibrariesPreLTO" );
879
+ for (auto &stub_file : symtab->stubFiles ) {
880
+ LLVM_DEBUG (llvm::dbgs ()
881
+ << " processing stub file: " << stub_file->getName () << " \n " );
882
+ for (auto [name, deps]: stub_file->symbolDependencies ) {
883
+ auto * sym = symtab->find (name);
884
+ // If the symbol is not present at all (yet), or if it is present but
885
+ // undefined, then mark the dependent symbols as used by a regular
886
+ // object so they will be preserved and exported by the LTO process.
887
+ if (!sym || sym->isUndefined ()) {
888
+ for (const auto dep : deps) {
889
+ auto * needed = symtab->find (dep);
890
+ if (needed ) {
891
+ needed->isUsedInRegularObj = true ;
892
+ }
893
+ }
894
+ }
895
+ }
896
+ }
897
+ }
898
+
877
899
static void processStubLibraries () {
878
900
log (" -- processStubLibraries" );
879
901
for (auto &stub_file : symtab->stubFiles ) {
880
902
LLVM_DEBUG (llvm::dbgs ()
881
903
<< " processing stub file: " << stub_file->getName () << " \n " );
882
904
for (auto [name, deps]: stub_file->symbolDependencies ) {
883
905
auto * sym = symtab->find (name);
884
- if (!sym || !sym->isUndefined () || sym-> forceImport ) {
885
- LLVM_DEBUG (llvm::dbgs () << " stub not in needed: " << name << " \n " );
906
+ if (!sym || !sym->isUndefined ()) {
907
+ LLVM_DEBUG (llvm::dbgs () << " stub symbol not needed: " << name << " \n " );
886
908
continue ;
887
909
}
888
910
// The first stub library to define a given symbol sets this and
889
911
// definitions in later stub libraries are ignored.
912
+ if (sym->forceImport )
913
+ continue ; // Already handled
890
914
sym->forceImport = true ;
891
915
if (sym->traced )
892
916
message (toString (stub_file) + " : importing " + name);
@@ -1213,9 +1237,9 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
1213
1237
if (errorCount ())
1214
1238
return ;
1215
1239
1216
- // processStubLibraries must happen before LTO because it can trigger the
1217
- // export of arbirary symbols that might themselves be defined in LTO objects .
1218
- processStubLibraries ();
1240
+ // We process the stub libraries once beofore LTO to ensure that any possible
1241
+ // required exports are preserved by the LTO process .
1242
+ processStubLibrariesPreLTO ();
1219
1243
1220
1244
// Do link-time optimization if given files are LLVM bitcode files.
1221
1245
// This compiles bitcode files into real object files.
@@ -1225,8 +1249,8 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
1225
1249
1226
1250
// The LTO process can generate new undefined symbols, specifically libcall
1227
1251
// functions. Because those symbols might be declared in a stub library we
1228
- // need the process the stub libraries once again after LTO to handle any
1229
- // newly undefined symbols.
1252
+ // need the process the stub libraries once again after LTO to handle all
1253
+ // undefined symbols, including ones that didn't exist prior to LTO .
1230
1254
processStubLibraries ();
1231
1255
1232
1256
writeWhyExtract ();
0 commit comments