Skip to content

Commit 8f35ec3

Browse files
committed
[clang][Driver] Clean up UEFI linker argument handling
This makes the UEFI driver's handling of linking more canonical in a few ways: * Use /option:value syntax with lld-link as in the MSVC driver. * Handle -nostdlib, -nodefaultlibs, -r and call common subroutines when they aren't set. This covers sanitizer and profile runtimes and their associated switches; compiler-rt builds do not yet provide these libraries, but the driver behavior is opt-in and supports all the opt-in/out plumbing like other targets do. This lets command lines immediately use the opt-out switches even when they are superfluous for opt-in features, as build system plumbing often needs to do. It also updates some TODO comments for how the driver behavior will look when more runtime support is ready.
1 parent 8c41859 commit 8f35ec3

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

clang/lib/Driver/ToolChains/UEFI.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,44 @@ void tools::uefi::Linker::ConstructJob(Compilation &C, const JobAction &JA,
5757
assert((Output.isFilename() || Output.isNothing()) && "invalid output");
5858
if (Output.isFilename())
5959
CmdArgs.push_back(
60-
Args.MakeArgString(std::string("-out:") + Output.getFilename()));
60+
Args.MakeArgString(std::string("/out:") + Output.getFilename()));
6161

62-
CmdArgs.push_back("-nologo");
63-
64-
// TODO: Other UEFI binary subsystems that are currently unsupported:
65-
// efi_boot_service_driver, efi_rom, efi_runtime_driver.
66-
CmdArgs.push_back("-subsystem:efi_application");
62+
CmdArgs.push_back("/nologo");
6763

6864
// Default entry function name according to the TianoCore reference
69-
// implementation is EfiMain.
70-
// TODO: Provide a flag to override the entry function name.
71-
CmdArgs.push_back("-entry:EfiMain");
65+
// implementation is EfiMain. -Wl,/subsystem:... or -Wl,/entry:... can
66+
// override these since they will be added later in AddLinkerInputs.
67+
CmdArgs.push_back("/subsystem:efi_application");
68+
CmdArgs.push_back("/entry:EfiMain");
7269

7370
// "Terminal Service Aware" flag is not needed for UEFI applications.
74-
CmdArgs.push_back("-tsaware:no");
71+
CmdArgs.push_back("/tsaware:no");
7572

7673
if (Args.hasArg(options::OPT_g_Group, options::OPT__SLASH_Z7))
77-
CmdArgs.push_back("-debug");
74+
CmdArgs.push_back("/debug");
7875

7976
Args.AddAllArgValues(CmdArgs, options::OPT__SLASH_link);
8077

8178
AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
8279

80+
// Sample these options first so they are claimed even under -nostdlib et al.
81+
bool NoLibc = Args.hasArg(options::OPT_nolibc);
82+
if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs,
83+
options::OPT_r)) {
84+
addSanitizerRuntimes(TC, Args, CmdArgs);
85+
86+
addXRayRuntime(TC, Args, CmdArgs);
87+
88+
TC.addProfileRTLibs(Args, CmdArgs);
89+
90+
// TODO: When compiler-rt/lib/builtins is ready, enable this call:
91+
// AddRunTimeLibs(TC, TC.getDriver(), CmdArgs, Args);
92+
93+
if (!NoLibc) {
94+
// TODO: When there is a libc ready, add it here.
95+
}
96+
}
97+
8398
// This should ideally be handled by ToolChain::GetLinkerPath but we need
8499
// to special case some linker paths. In the case of lld, we need to
85100
// translate 'lld' into 'lld-link'.

clang/test/Driver/uefi-constructed-args.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
// CHECK-SAME: "-mrelocation-model" "pic" "-pic-level" "2"
88
// CHECK-SAME: "-mframe-pointer=all"
99
// CHECK-SAME: "-fms-extensions"
10-
// CHECK-NEXT: "-nologo"
11-
// CHECK-SAME: "-subsystem:efi_application"
12-
// CHECK-SAME: "-entry:EfiMain"
13-
// CHECK-SAME: "-tsaware:no"
14-
// CHECK-SAME: "-debug"
10+
// CHECK-NEXT: "/nologo"
11+
// CHECK-SAME: "/subsystem:efi_application"
12+
// CHECK-SAME: "/entry:EfiMain"
13+
// CHECK-SAME: "/tsaware:no"
14+
// CHECK-SAME: "/debug"

0 commit comments

Comments
 (0)