1 //===--- UEFI.cpp - UEFI ToolChain Implementations -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "UEFI.h"
10 #include "clang/Config/config.h"
11 #include "clang/Driver/CommonArgs.h"
12 #include "clang/Driver/Compilation.h"
13 #include "clang/Driver/Driver.h"
14 #include "clang/Driver/Options.h"
15 #include "clang/Driver/SanitizerArgs.h"
16 #include "llvm/Option/Arg.h"
17 #include "llvm/Option/ArgList.h"
18 #include "llvm/Support/VirtualFileSystem.h"
19 #include "llvm/TargetParser/Host.h"
20
21 using namespace clang::driver;
22 using namespace clang::driver::toolchains;
23 using namespace clang;
24 using namespace llvm::opt;
25
UEFI(const Driver & D,const llvm::Triple & Triple,const ArgList & Args)26 UEFI::UEFI(const Driver &D, const llvm::Triple &Triple, const ArgList &Args)
27 : ToolChain(D, Triple, Args) {}
28
buildLinker() const29 Tool *UEFI::buildLinker() const { return new tools::uefi::Linker(*this); }
30
AddClangSystemIncludeArgs(const ArgList & DriverArgs,ArgStringList & CC1Args) const31 void UEFI::AddClangSystemIncludeArgs(const ArgList &DriverArgs,
32 ArgStringList &CC1Args) const {
33 if (DriverArgs.hasArg(options::OPT_nostdinc))
34 return;
35
36 if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
37 SmallString<128> Dir(getDriver().ResourceDir);
38 llvm::sys::path::append(Dir, "include");
39 addSystemInclude(DriverArgs, CC1Args, Dir.str());
40 }
41
42 if (DriverArgs.hasArg(options::OPT_nostdlibinc))
43 return;
44
45 if (std::optional<std::string> Path = getStdlibIncludePath())
46 addSystemInclude(DriverArgs, CC1Args, *Path);
47 }
48
ConstructJob(Compilation & C,const JobAction & JA,const InputInfo & Output,const InputInfoList & Inputs,const ArgList & Args,const char * LinkingOutput) const49 void tools::uefi::Linker::ConstructJob(Compilation &C, const JobAction &JA,
50 const InputInfo &Output,
51 const InputInfoList &Inputs,
52 const ArgList &Args,
53 const char *LinkingOutput) const {
54 ArgStringList CmdArgs;
55 auto &TC = static_cast<const toolchains::UEFI &>(getToolChain());
56
57 assert((Output.isFilename() || Output.isNothing()) && "invalid output");
58 if (Output.isFilename())
59 CmdArgs.push_back(
60 Args.MakeArgString(std::string("-out:") + Output.getFilename()));
61
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");
67
68 // 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");
72
73 // "Terminal Service Aware" flag is not needed for UEFI applications.
74 CmdArgs.push_back("-tsaware:no");
75
76 if (Args.hasArg(options::OPT_g_Group, options::OPT__SLASH_Z7))
77 CmdArgs.push_back("-debug");
78
79 Args.AddAllArgValues(CmdArgs, options::OPT__SLASH_link);
80
81 AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
82
83 // This should ideally be handled by ToolChain::GetLinkerPath but we need
84 // to special case some linker paths. In the case of lld, we need to
85 // translate 'lld' into 'lld-link'.
86 StringRef Linker =
87 Args.getLastArgValue(options::OPT_fuse_ld_EQ, CLANG_DEFAULT_LINKER);
88 if (Linker.empty() || Linker == "lld")
89 Linker = "lld-link";
90
91 auto LinkerPath = TC.GetProgramPath(Linker.str().c_str());
92 auto LinkCmd = std::make_unique<Command>(
93 JA, *this, ResponseFileSupport::AtFileUTF16(),
94 Args.MakeArgString(LinkerPath), CmdArgs, Inputs, Output);
95 C.addCommand(std::move(LinkCmd));
96 }
97