@@ -193,6 +193,7 @@ pub fn get_linker(sess: &Session, linker: &Path, flavor: LinkerFlavor) -> Comman
193
193
_ => None ,
194
194
} ;
195
195
if let Some ( ref a) = arch {
196
+ // FIXME: Move this to `fn linker_with_args`.
196
197
let mut arg = OsString :: from ( "/LIBPATH:" ) ;
197
198
arg. push ( format ! ( "{}\\ lib\\ {}\\ store" , root_lib_path. display( ) , a. to_string( ) ) ) ;
198
199
cmd. arg ( & arg) ;
@@ -1254,9 +1255,29 @@ fn add_post_link_args(cmd: &mut dyn Linker, sess: &'a Session, flavor: LinkerFla
1254
1255
}
1255
1256
}
1256
1257
1258
+ /// Add sysroot and other globally set directories to the directory search list.
1259
+ fn add_library_search_dirs ( cmd : & mut dyn Linker , sess : & ' a Session ) {
1260
+ // The default library location, we need this to find the runtime.
1261
+ // The location of crates will be determined as needed.
1262
+ let lib_path = sess. target_filesearch ( PathKind :: All ) . get_lib_path ( ) ;
1263
+
1264
+ // prefer system mingw-w64 libs, see get_crt_libs_path comment for more details
1265
+ if cfg ! ( windows) && sess. target . target . llvm_target . contains ( "windows-gnu" ) {
1266
+ if let Some ( compiler_libs_path) = get_crt_libs_path ( sess) {
1267
+ cmd. include_path ( & compiler_libs_path) ;
1268
+ }
1269
+ }
1270
+
1271
+ cmd. include_path ( & fix_windows_verbatim_for_gcc ( & lib_path) ) ;
1272
+ }
1273
+
1257
1274
/// Produce the linker command line containing linker path and arguments.
1258
1275
/// `NO-OPT-OUT` marks the arguments that cannot be removed from the command line
1259
1276
/// by the user without creating a custom target specification.
1277
+ /// `OBJECT-FILES` specify whether the arguments can add object files.
1278
+ /// `CUSTOMIZATION-POINT` means that arbitrary arguments defined by the user
1279
+ /// or by the target spec can be inserted here.
1280
+ /// `AUDIT-ORDER` - need to figure out whether the option is order-dependent or not.
1260
1281
fn linker_with_args < ' a , B : ArchiveBuilder < ' a > > (
1261
1282
path : & Path ,
1262
1283
flavor : LinkerFlavor ,
@@ -1273,9 +1294,10 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
1273
1294
assert ! ( base_cmd. get_args( ) . is_empty( ) || sess. target. target. target_vendor == "uwp" ) ;
1274
1295
let cmd = & mut * codegen_results. linker_info . to_linker ( base_cmd, & sess, flavor, target_cpu) ;
1275
1296
1276
- // NO-OPT-OUT
1297
+ // NO-OPT-OUT, OBJECT-FILES-MAYBE, CUSTOMIZATION-POINT
1277
1298
add_pre_link_args ( cmd, sess, flavor, crate_type) ;
1278
1299
1300
+ // NO-OPT-OUT, OBJECT-FILES-NO, AUDIT-ORDER
1279
1301
if sess. target . target . options . is_like_fuchsia {
1280
1302
let prefix = match sess. opts . debugging_opts . sanitizer {
1281
1303
Some ( Sanitizer :: Address ) => "asan/" ,
@@ -1284,9 +1306,10 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
1284
1306
cmd. arg ( format ! ( "--dynamic-linker={}ld.so.1" , prefix) ) ;
1285
1307
}
1286
1308
1287
- // NO-OPT-OUT
1309
+ // NO-OPT-OUT, OBJECT-FILES-YES
1288
1310
add_pre_link_objects ( cmd, sess, crate_type) ;
1289
1311
1312
+ // NO-OPT-OUT, OBJECT-FILES-NO, AUDIT-ORDER
1290
1313
if sess. target . target . options . is_like_emscripten {
1291
1314
cmd. arg ( "-s" ) ;
1292
1315
cmd. arg ( if sess. panic_strategy ( ) == PanicStrategy :: Abort {
@@ -1296,43 +1319,40 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
1296
1319
} ) ;
1297
1320
}
1298
1321
1322
+ // OBJECT-FILES-YES, AUDIT-ORDER
1299
1323
link_sanitizer_runtime ( sess, crate_type, cmd) ;
1300
1324
1325
+ // OBJECT-FILES-NO, AUDIT-ORDER
1301
1326
// Linker plugins should be specified early in the list of arguments
1327
+ // FIXME: How "early" exactly?
1302
1328
cmd. linker_plugin_lto ( ) ;
1303
1329
1304
- // The default library location, we need this to find the runtime.
1305
- // The location of crates will be determined as needed.
1306
- let lib_path = sess. target_filesearch ( PathKind :: All ) . get_lib_path ( ) ;
1307
-
1308
- // target descriptor
1309
- let t = & sess. target . target ;
1310
-
1311
- // prefer system mingw-w64 libs, see get_crt_libs_path comment for more details
1312
- if cfg ! ( windows) && sess. target . target . llvm_target . contains ( "windows-gnu" ) {
1313
- if let Some ( compiler_libs_path) = get_crt_libs_path ( sess) {
1314
- cmd. include_path ( & compiler_libs_path) ;
1315
- }
1316
- }
1317
-
1318
- cmd. include_path ( & fix_windows_verbatim_for_gcc ( & lib_path) ) ;
1330
+ // NO-OPT-OUT, OBJECT-FILES-NO, AUDIT-ORDER
1331
+ // FIXME: Order-dependent, at least relatively to other args adding searh directories.
1332
+ add_library_search_dirs ( cmd, sess) ;
1319
1333
1334
+ // OBJECT-FILES-YES
1320
1335
for obj in codegen_results. modules . iter ( ) . filter_map ( |m| m. object . as_ref ( ) ) {
1321
1336
cmd. add_object ( obj) ;
1322
1337
}
1338
+
1339
+ // NO-OPT-OUT, OBJECT-FILES-NO, AUDIT-ORDER
1323
1340
cmd. output_filename ( out_filename) ;
1324
1341
1342
+ // OBJECT-FILES-NO, AUDIT-ORDER
1325
1343
if crate_type == config:: CrateType :: Executable && sess. target . target . options . is_like_windows {
1326
1344
if let Some ( ref s) = codegen_results. windows_subsystem {
1327
1345
cmd. subsystem ( s) ;
1328
1346
}
1329
1347
}
1330
1348
1349
+ // NO-OPT-OUT, OBJECT-FILES-NO, AUDIT-ORDER
1331
1350
// If we're building something like a dynamic library then some platforms
1332
1351
// need to make sure that all symbols are exported correctly from the
1333
1352
// dynamic library.
1334
1353
cmd. export_symbols ( tmpdir, crate_type) ;
1335
1354
1355
+ // OBJECT-FILES-YES
1336
1356
// When linking a dynamic library, we put the metadata into a section of the
1337
1357
// executable. This metadata is in a separate object file from the main
1338
1358
// object file, so we link that in here.
@@ -1343,22 +1363,26 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
1343
1363
}
1344
1364
}
1345
1365
1366
+ // OBJECT-FILES-YES
1346
1367
let obj = codegen_results. allocator_module . as_ref ( ) . and_then ( |m| m. object . as_ref ( ) ) ;
1347
1368
if let Some ( obj) = obj {
1348
1369
cmd. add_object ( obj) ;
1349
1370
}
1350
1371
1372
+ // OBJECT-FILES-NO, AUDIT-ORDER
1373
+ // FIXME: Order dependent, applies to the following objects. Where should it be placed?
1351
1374
// Try to strip as much out of the generated object by removing unused
1352
1375
// sections if possible. See more comments in linker.rs
1353
1376
if !sess. opts . cg . link_dead_code {
1354
1377
let keep_metadata = crate_type == config:: CrateType :: Dylib ;
1355
1378
cmd. gc_sections ( keep_metadata) ;
1356
1379
}
1357
1380
1381
+ // NO-OPT-OUT, OBJECT-FILES-NO, AUDIT-ORDER
1358
1382
if crate_type == config:: CrateType :: Executable {
1359
1383
let mut position_independent_executable = false ;
1360
1384
1361
- if t . options . position_independent_executables {
1385
+ if sess . target . target . options . position_independent_executables {
1362
1386
if is_pic ( sess)
1363
1387
&& !sess. crt_static ( Some ( crate_type) )
1364
1388
&& !sess
@@ -1385,9 +1409,10 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
1385
1409
}
1386
1410
}
1387
1411
1412
+ // OBJECT-FILES-NO, AUDIT-ORDER
1388
1413
let relro_level = match sess. opts . debugging_opts . relro_level {
1389
1414
Some ( level) => level,
1390
- None => t . options . relro_level ,
1415
+ None => sess . target . target . options . relro_level ,
1391
1416
} ;
1392
1417
match relro_level {
1393
1418
RelroLevel :: Full => {
@@ -1402,23 +1427,29 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
1402
1427
RelroLevel :: None => { }
1403
1428
}
1404
1429
1430
+ // OBJECT-FILES-NO, AUDIT-ORDER
1405
1431
// Pass optimization flags down to the linker.
1406
1432
cmd. optimize ( ) ;
1407
1433
1434
+ // OBJECT-FILES-NO, AUDIT-ORDER
1408
1435
// Pass debuginfo flags down to the linker.
1409
1436
cmd. debuginfo ( ) ;
1410
1437
1438
+ // OBJECT-FILES-NO, AUDIT-ORDER
1411
1439
// We want to, by default, prevent the compiler from accidentally leaking in
1412
1440
// any system libraries, so we may explicitly ask linkers to not link to any
1413
1441
// libraries by default. Note that this does not happen for windows because
1414
1442
// windows pulls in some large number of libraries and I couldn't quite
1415
1443
// figure out which subset we wanted.
1416
1444
//
1417
1445
// This is all naturally configurable via the standard methods as well.
1418
- if !sess. opts . cg . default_linker_libraries . unwrap_or ( false ) && t. options . no_default_libraries {
1446
+ if !sess. opts . cg . default_linker_libraries . unwrap_or ( false )
1447
+ && sess. target . target . options . no_default_libraries
1448
+ {
1419
1449
cmd. no_default_libraries ( ) ;
1420
1450
}
1421
1451
1452
+ // OBJECT-FILES-YES
1422
1453
// Take careful note of the ordering of the arguments we pass to the linker
1423
1454
// here. Linkers will assume that things on the left depend on things to the
1424
1455
// right. Things on the right cannot depend on things on the left. This is
@@ -1456,6 +1487,8 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
1456
1487
if sess. opts . debugging_opts . link_native_libraries . unwrap_or ( true ) {
1457
1488
add_upstream_native_libraries ( cmd, sess, codegen_results, crate_type) ;
1458
1489
}
1490
+
1491
+ // NO-OPT-OUT, OBJECT-FILES-NO, AUDIT-ORDER
1459
1492
// Tell the linker what we're doing.
1460
1493
if crate_type != config:: CrateType :: Executable {
1461
1494
cmd. build_dylib ( out_filename) ;
@@ -1464,14 +1497,17 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
1464
1497
cmd. build_static_executable ( ) ;
1465
1498
}
1466
1499
1500
+ // OBJECT-FILES-NO, AUDIT-ORDER
1467
1501
if sess. opts . cg . profile_generate . enabled ( ) {
1468
1502
cmd. pgo_gen ( ) ;
1469
1503
}
1470
1504
1505
+ // OBJECT-FILES-NO, AUDIT-ORDER
1471
1506
if sess. opts . debugging_opts . control_flow_guard != CFGuard :: Disabled {
1472
1507
cmd. control_flow_guard ( ) ;
1473
1508
}
1474
1509
1510
+ // OBJECT-FILES-NO, AUDIT-ORDER
1475
1511
// FIXME (#2397): At some point we want to rpath our guesses as to
1476
1512
// where extern libraries might live, based on the
1477
1513
// addl_lib_search_paths
@@ -1496,17 +1532,19 @@ fn linker_with_args<'a, B: ArchiveBuilder<'a>>(
1496
1532
cmd. args ( & rpath:: get_rpath_flags ( & mut rpath_config) ) ;
1497
1533
}
1498
1534
1535
+ // OBJECT-FILES-MAYBE, CUSTOMIZATION-POINT
1499
1536
add_user_defined_link_args ( cmd, sess, codegen_results) ;
1500
1537
1538
+ // NO-OPT-OUT, OBJECT-FILES-NO, AUDIT-ORDER
1501
1539
cmd. finalize ( ) ;
1502
1540
1503
- // NO-OPT-OUT
1541
+ // NO-OPT-OUT, OBJECT-FILES-MAYBE, CUSTOMIZATION-POINT
1504
1542
add_late_link_args ( cmd, sess, flavor, crate_type, codegen_results) ;
1505
1543
1506
- // NO-OPT-OUT
1544
+ // NO-OPT-OUT, OBJECT-FILES-YES
1507
1545
add_post_link_objects ( cmd, sess, crate_type) ;
1508
1546
1509
- // NO-OPT-OUT
1547
+ // NO-OPT-OUT, OBJECT-FILES-MAYBE, CUSTOMIZATION-POINT
1510
1548
add_post_link_args ( cmd, sess, flavor) ;
1511
1549
1512
1550
cmd. take_cmd ( )
0 commit comments