37
37
#include " llvm/Support/BinaryStreamReader.h"
38
38
#include " llvm/Support/CommandLine.h"
39
39
#include " llvm/Support/Debug.h"
40
- #include " llvm/Support/GlobPattern.h"
41
40
#include " llvm/Support/LEB128.h"
42
41
#include " llvm/Support/MathExtras.h"
43
42
#include " llvm/Support/Parallel.h"
@@ -172,23 +171,10 @@ static std::future<MBErrPair> createFutureForFile(std::string path) {
172
171
});
173
172
}
174
173
175
- // Symbol names are mangled by prepending "_" on x86.
176
- StringRef LinkerDriver::mangle (StringRef sym) {
177
- assert (ctx.config .machine != IMAGE_FILE_MACHINE_UNKNOWN);
178
- if (ctx.config .machine == I386)
179
- return saver ().save (" _" + sym);
180
- return sym;
181
- }
182
-
183
174
llvm::Triple::ArchType LinkerDriver::getArch () {
184
175
return getMachineArchType (ctx.config .machine );
185
176
}
186
177
187
- bool LinkerDriver::findUnderscoreMangle (StringRef sym) {
188
- Symbol *s = ctx.symtab .findMangle (mangle (sym));
189
- return s && !isa<Undefined>(s);
190
- }
191
-
192
178
static bool compatibleMachineType (COFFLinkerContext &ctx, MachineTypes mt) {
193
179
if (mt == IMAGE_FILE_MACHINE_UNKNOWN)
194
180
return true ;
@@ -486,7 +472,7 @@ void LinkerDriver::parseDirectives(InputFile *file) {
486
472
SmallVector<StringRef, 2 > vec;
487
473
e.split (vec, ' ,' );
488
474
for (StringRef sym : vec)
489
- excludedSymbols.insert (mangle (sym));
475
+ excludedSymbols.insert (file-> symtab . mangle (sym));
490
476
}
491
477
492
478
// https://docs.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp?view=msvc-160
@@ -505,7 +491,8 @@ void LinkerDriver::parseDirectives(InputFile *file) {
505
491
case OPT_entry:
506
492
if (!arg->getValue ()[0 ])
507
493
Fatal (ctx) << " missing entry point symbol name" ;
508
- ctx.config .entry = file->symtab .addGCRoot (mangle (arg->getValue ()), true );
494
+ ctx.config .entry =
495
+ file->symtab .addGCRoot (file->symtab .mangle (arg->getValue ()), true );
509
496
break ;
510
497
case OPT_failifmismatch:
511
498
checkFailIfMismatch (arg->getValue (), file);
@@ -805,97 +792,6 @@ void LinkerDriver::addLibSearchPaths() {
805
792
}
806
793
}
807
794
808
- void LinkerDriver::addUndefinedGlob (StringRef arg) {
809
- Expected<GlobPattern> pat = GlobPattern::create (arg);
810
- if (!pat) {
811
- Err (ctx) << " /includeglob: " << toString (pat.takeError ());
812
- return ;
813
- }
814
-
815
- SmallVector<Symbol *, 0 > syms;
816
- ctx.symtab .forEachSymbol ([&syms, &pat](Symbol *sym) {
817
- if (pat->match (sym->getName ())) {
818
- syms.push_back (sym);
819
- }
820
- });
821
-
822
- for (Symbol *sym : syms)
823
- ctx.symtab .addGCRoot (sym->getName ());
824
- }
825
-
826
- StringRef LinkerDriver::mangleMaybe (Symbol *s) {
827
- // If the plain symbol name has already been resolved, do nothing.
828
- Undefined *unmangled = dyn_cast<Undefined>(s);
829
- if (!unmangled)
830
- return " " ;
831
-
832
- // Otherwise, see if a similar, mangled symbol exists in the symbol table.
833
- Symbol *mangled = ctx.symtab .findMangle (unmangled->getName ());
834
- if (!mangled)
835
- return " " ;
836
-
837
- // If we find a similar mangled symbol, make this an alias to it and return
838
- // its name.
839
- Log (ctx) << unmangled->getName () << " aliased to " << mangled->getName ();
840
- unmangled->setWeakAlias (ctx.symtab .addUndefined (mangled->getName ()));
841
- return mangled->getName ();
842
- }
843
-
844
- // Windows specific -- find default entry point name.
845
- //
846
- // There are four different entry point functions for Windows executables,
847
- // each of which corresponds to a user-defined "main" function. This function
848
- // infers an entry point from a user-defined "main" function.
849
- StringRef LinkerDriver::findDefaultEntry () {
850
- assert (ctx.config .subsystem != IMAGE_SUBSYSTEM_UNKNOWN &&
851
- " must handle /subsystem before calling this" );
852
-
853
- if (ctx.config .mingw )
854
- return mangle (ctx.config .subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI
855
- ? " WinMainCRTStartup"
856
- : " mainCRTStartup" );
857
-
858
- if (ctx.config .subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI) {
859
- if (findUnderscoreMangle (" wWinMain" )) {
860
- if (!findUnderscoreMangle (" WinMain" ))
861
- return mangle (" wWinMainCRTStartup" );
862
- Warn (ctx) << " found both wWinMain and WinMain; using latter" ;
863
- }
864
- return mangle (" WinMainCRTStartup" );
865
- }
866
- if (findUnderscoreMangle (" wmain" )) {
867
- if (!findUnderscoreMangle (" main" ))
868
- return mangle (" wmainCRTStartup" );
869
- Warn (ctx) << " found both wmain and main; using latter" ;
870
- }
871
- return mangle (" mainCRTStartup" );
872
- }
873
-
874
- WindowsSubsystem LinkerDriver::inferSubsystem () {
875
- if (ctx.config .dll )
876
- return IMAGE_SUBSYSTEM_WINDOWS_GUI;
877
- if (ctx.config .mingw )
878
- return IMAGE_SUBSYSTEM_WINDOWS_CUI;
879
- // Note that link.exe infers the subsystem from the presence of these
880
- // functions even if /entry: or /nodefaultlib are passed which causes them
881
- // to not be called.
882
- bool haveMain = findUnderscoreMangle (" main" );
883
- bool haveWMain = findUnderscoreMangle (" wmain" );
884
- bool haveWinMain = findUnderscoreMangle (" WinMain" );
885
- bool haveWWinMain = findUnderscoreMangle (" wWinMain" );
886
- if (haveMain || haveWMain) {
887
- if (haveWinMain || haveWWinMain) {
888
- Warn (ctx) << " found " << (haveMain ? " main" : " wmain" ) << " and "
889
- << (haveWinMain ? " WinMain" : " wWinMain" )
890
- << " ; defaulting to /subsystem:console" ;
891
- }
892
- return IMAGE_SUBSYSTEM_WINDOWS_CUI;
893
- }
894
- if (haveWinMain || haveWWinMain)
895
- return IMAGE_SUBSYSTEM_WINDOWS_GUI;
896
- return IMAGE_SUBSYSTEM_UNKNOWN;
897
- }
898
-
899
795
uint64_t LinkerDriver::getDefaultImageBase () {
900
796
if (ctx.config .is64 ())
901
797
return ctx.config .dll ? 0x180000000 : 0x140000000 ;
@@ -1539,7 +1435,7 @@ void LinkerDriver::maybeExportMinGWSymbols(const opt::InputArgList &args) {
1539
1435
SmallVector<StringRef, 2 > vec;
1540
1436
StringRef (arg->getValue ()).split (vec, ' ,' );
1541
1437
for (StringRef sym : vec)
1542
- exporter.addExcludedSymbol (mangle (sym));
1438
+ exporter.addExcludedSymbol (ctx. symtab . mangle (sym));
1543
1439
}
1544
1440
1545
1441
ctx.symtab .forEachSymbol ([&](Symbol *s) {
@@ -2455,7 +2351,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
2455
2351
// and after the early return when just writing an import library.
2456
2352
if (config->subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
2457
2353
llvm::TimeTraceScope timeScope (" Infer subsystem" );
2458
- config->subsystem = inferSubsystem ();
2354
+ config->subsystem = ctx. symtab . inferSubsystem ();
2459
2355
if (config->subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
2460
2356
Fatal (ctx) << " subsystem must be defined" ;
2461
2357
}
@@ -2466,19 +2362,21 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
2466
2362
if (auto *arg = args.getLastArg (OPT_entry)) {
2467
2363
if (!arg->getValue ()[0 ])
2468
2364
Fatal (ctx) << " missing entry point symbol name" ;
2469
- config->entry = ctx.symtab .addGCRoot (mangle (arg->getValue ()), true );
2365
+ config->entry =
2366
+ ctx.symtab .addGCRoot (ctx.symtab .mangle (arg->getValue ()), true );
2470
2367
} else if (!config->entry && !config->noEntry ) {
2471
2368
if (args.hasArg (OPT_dll)) {
2472
2369
StringRef s = (config->machine == I386) ? " __DllMainCRTStartup@12"
2473
2370
: " _DllMainCRTStartup" ;
2474
2371
config->entry = ctx.symtab .addGCRoot (s, true );
2475
2372
} else if (config->driverWdm ) {
2476
2373
// /driver:wdm implies /entry:_NtProcessStartup
2477
- config->entry = ctx.symtab .addGCRoot (mangle (" _NtProcessStartup" ), true );
2374
+ config->entry =
2375
+ ctx.symtab .addGCRoot (ctx.symtab .mangle (" _NtProcessStartup" ), true );
2478
2376
} else {
2479
2377
// Windows specific -- If entry point name is not given, we need to
2480
2378
// infer that from user-defined entry name.
2481
- StringRef s = findDefaultEntry ();
2379
+ StringRef s = ctx. symtab . findDefaultEntry ();
2482
2380
if (s.empty ())
2483
2381
Fatal (ctx) << " entry point must be defined" ;
2484
2382
config->entry = ctx.symtab .addGCRoot (s, true );
@@ -2568,24 +2466,24 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
2568
2466
config->imageBase = getDefaultImageBase ();
2569
2467
2570
2468
ctx.forEachSymtab ([&](SymbolTable &symtab) {
2571
- symtab.addSynthetic (mangle (" __ImageBase" ), nullptr );
2469
+ symtab.addSynthetic (symtab. mangle (" __ImageBase" ), nullptr );
2572
2470
if (symtab.machine == I386) {
2573
2471
symtab.addAbsolute (" ___safe_se_handler_table" , 0 );
2574
2472
symtab.addAbsolute (" ___safe_se_handler_count" , 0 );
2575
2473
}
2576
2474
2577
- symtab.addAbsolute (mangle (" __guard_fids_count" ), 0 );
2578
- symtab.addAbsolute (mangle (" __guard_fids_table" ), 0 );
2579
- symtab.addAbsolute (mangle (" __guard_flags" ), 0 );
2580
- symtab.addAbsolute (mangle (" __guard_iat_count" ), 0 );
2581
- symtab.addAbsolute (mangle (" __guard_iat_table" ), 0 );
2582
- symtab.addAbsolute (mangle (" __guard_longjmp_count" ), 0 );
2583
- symtab.addAbsolute (mangle (" __guard_longjmp_table" ), 0 );
2475
+ symtab.addAbsolute (symtab. mangle (" __guard_fids_count" ), 0 );
2476
+ symtab.addAbsolute (symtab. mangle (" __guard_fids_table" ), 0 );
2477
+ symtab.addAbsolute (symtab. mangle (" __guard_flags" ), 0 );
2478
+ symtab.addAbsolute (symtab. mangle (" __guard_iat_count" ), 0 );
2479
+ symtab.addAbsolute (symtab. mangle (" __guard_iat_table" ), 0 );
2480
+ symtab.addAbsolute (symtab. mangle (" __guard_longjmp_count" ), 0 );
2481
+ symtab.addAbsolute (symtab. mangle (" __guard_longjmp_table" ), 0 );
2584
2482
// Needed for MSVC 2017 15.5 CRT.
2585
- symtab.addAbsolute (mangle (" __enclave_config" ), 0 );
2483
+ symtab.addAbsolute (symtab. mangle (" __enclave_config" ), 0 );
2586
2484
// Needed for MSVC 2019 16.8 CRT.
2587
- symtab.addAbsolute (mangle (" __guard_eh_cont_count" ), 0 );
2588
- symtab.addAbsolute (mangle (" __guard_eh_cont_table" ), 0 );
2485
+ symtab.addAbsolute (symtab. mangle (" __guard_eh_cont_count" ), 0 );
2486
+ symtab.addAbsolute (symtab. mangle (" __guard_eh_cont_table" ), 0 );
2589
2487
2590
2488
if (symtab.isEC ()) {
2591
2489
symtab.addAbsolute (" __arm64x_extra_rfe_table" , 0 );
@@ -2606,16 +2504,16 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
2606
2504
}
2607
2505
2608
2506
if (config->pseudoRelocs ) {
2609
- symtab.addAbsolute (mangle (" __RUNTIME_PSEUDO_RELOC_LIST__" ), 0 );
2610
- symtab.addAbsolute (mangle (" __RUNTIME_PSEUDO_RELOC_LIST_END__" ), 0 );
2507
+ symtab.addAbsolute (symtab. mangle (" __RUNTIME_PSEUDO_RELOC_LIST__" ), 0 );
2508
+ symtab.addAbsolute (symtab. mangle (" __RUNTIME_PSEUDO_RELOC_LIST_END__" ), 0 );
2611
2509
}
2612
2510
if (config->mingw ) {
2613
- symtab.addAbsolute (mangle (" __CTOR_LIST__" ), 0 );
2614
- symtab.addAbsolute (mangle (" __DTOR_LIST__" ), 0 );
2511
+ symtab.addAbsolute (symtab. mangle (" __CTOR_LIST__" ), 0 );
2512
+ symtab.addAbsolute (symtab. mangle (" __DTOR_LIST__" ), 0 );
2615
2513
}
2616
2514
if (config->debug || config->buildIDHash != BuildIDHash::None)
2617
2515
if (symtab.findUnderscore (" __buildid" ))
2618
- symtab.addUndefined (mangle (" __buildid" ));
2516
+ symtab.addUndefined (symtab. mangle (" __buildid" ));
2619
2517
});
2620
2518
2621
2519
// This code may add new undefined symbols to the link, which may enqueue more
@@ -2627,15 +2525,15 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
2627
2525
// Windows specific -- if entry point is not found,
2628
2526
// search for its mangled names.
2629
2527
if (config->entry )
2630
- mangleMaybe (config->entry );
2528
+ ctx. symtab . mangleMaybe (config->entry );
2631
2529
2632
2530
// Windows specific -- Make sure we resolve all dllexported symbols.
2633
2531
for (Export &e : config->exports ) {
2634
2532
if (!e.forwardTo .empty ())
2635
2533
continue ;
2636
2534
e.sym = ctx.symtab .addGCRoot (e.name , !e.data );
2637
2535
if (e.source != ExportSource::Directives)
2638
- e.symbolName = mangleMaybe (e.sym );
2536
+ e.symbolName = ctx. symtab . mangleMaybe (e.sym );
2639
2537
}
2640
2538
2641
2539
// Add weak aliases. Weak aliases is a mechanism to give remaining
@@ -2675,7 +2573,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
2675
2573
2676
2574
// Windows specific -- if __load_config_used can be resolved, resolve it.
2677
2575
if (ctx.symtab .findUnderscore (" _load_config_used" ))
2678
- ctx.symtab .addGCRoot (mangle (" _load_config_used" ));
2576
+ ctx.symtab .addGCRoot (ctx. symtab . mangle (" _load_config_used" ));
2679
2577
2680
2578
if (args.hasArg (OPT_include_optional)) {
2681
2579
// Handle /includeoptional
@@ -2688,7 +2586,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
2688
2586
2689
2587
// Handle /includeglob
2690
2588
for (StringRef pat : args::getStrings (args, OPT_incl_glob))
2691
- addUndefinedGlob (pat);
2589
+ ctx. symtab . addUndefinedGlob (pat);
2692
2590
2693
2591
// Create wrapped symbols for -wrap option.
2694
2592
std::vector<WrappedSymbol> wrapped = addWrappedSymbols (ctx, args);
0 commit comments