|
| 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 "CommonArgs.h" |
| 11 | +#include "Darwin.h" |
| 12 | +#include "clang/Basic/CharInfo.h" |
| 13 | +#include "clang/Basic/Version.h" |
| 14 | +#include "clang/Config/config.h" |
| 15 | +#include "clang/Driver/Compilation.h" |
| 16 | +#include "clang/Driver/Driver.h" |
| 17 | +#include "clang/Driver/Options.h" |
| 18 | +#include "clang/Driver/SanitizerArgs.h" |
| 19 | +#include "llvm/ADT/StringExtras.h" |
| 20 | +#include "llvm/ADT/StringSwitch.h" |
| 21 | +#include "llvm/Option/Arg.h" |
| 22 | +#include "llvm/Option/ArgList.h" |
| 23 | +#include "llvm/Support/FileSystem.h" |
| 24 | +#include "llvm/Support/MemoryBuffer.h" |
| 25 | +#include "llvm/Support/VirtualFileSystem.h" |
| 26 | +#include "llvm/TargetParser/Host.h" |
| 27 | + |
| 28 | +using namespace clang::driver; |
| 29 | +using namespace clang::driver::toolchains; |
| 30 | +using namespace clang; |
| 31 | +using namespace llvm::opt; |
| 32 | + |
| 33 | +UEFI::UEFI(const Driver &D, const llvm::Triple &Triple, const ArgList &Args) |
| 34 | + : ToolChain(D, Triple, Args) {} |
| 35 | + |
| 36 | +Tool *UEFI::buildLinker() const { return new tools::uefi::Linker(*this); } |
| 37 | + |
| 38 | +void tools::uefi::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
| 39 | + const InputInfo &Output, |
| 40 | + const InputInfoList &Inputs, |
| 41 | + const ArgList &Args, |
| 42 | + const char *LinkingOutput) const { |
| 43 | + ArgStringList CmdArgs; |
| 44 | + auto &TC = static_cast<const toolchains::UEFI &>(getToolChain()); |
| 45 | + |
| 46 | + assert((Output.isFilename() || Output.isNothing()) && "invalid output"); |
| 47 | + if (Output.isFilename()) |
| 48 | + CmdArgs.push_back( |
| 49 | + Args.MakeArgString(std::string("-out:") + Output.getFilename())); |
| 50 | + |
| 51 | + CmdArgs.push_back("-nologo"); |
| 52 | + |
| 53 | + // TODO: Other UEFI binary subsystems that are currently unsupported: |
| 54 | + // efi_boot_service_driver, efi_rom, efi_runtime_driver. |
| 55 | + CmdArgs.push_back("-subsystem:efi_application"); |
| 56 | + |
| 57 | + // Default entry function name according to the TianoCore reference |
| 58 | + // implementation is EfiMain. |
| 59 | + // TODO: Provide a flag to override the entry function name. |
| 60 | + CmdArgs.push_back("-entry:EfiMain"); |
| 61 | + |
| 62 | + // "Terminal Service Aware" flag is not needed for UEFI applications. |
| 63 | + CmdArgs.push_back("-tsaware:no"); |
| 64 | + |
| 65 | + // EFI_APPLICATION to be linked as DLL by default. |
| 66 | + CmdArgs.push_back("-dll"); |
| 67 | + |
| 68 | + if (Args.hasArg(options::OPT_g_Group, options::OPT__SLASH_Z7)) |
| 69 | + CmdArgs.push_back("-debug"); |
| 70 | + |
| 71 | + Args.AddAllArgValues(CmdArgs, options::OPT__SLASH_link); |
| 72 | + |
| 73 | + AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA); |
| 74 | + |
| 75 | + // This should ideally be handled by ToolChain::GetLinkerPath but we need |
| 76 | + // to special case some linker paths. In the case of lld, we need to |
| 77 | + // translate 'lld' into 'lld-link'. |
| 78 | + StringRef Linker = |
| 79 | + Args.getLastArgValue(options::OPT_fuse_ld_EQ, CLANG_DEFAULT_LINKER); |
| 80 | + if (Linker.empty() || Linker == "lld") |
| 81 | + Linker = "lld-link"; |
| 82 | + |
| 83 | + auto LinkerPath = TC.GetProgramPath(Linker.str().c_str()); |
| 84 | + auto LinkCmd = std::make_unique<Command>( |
| 85 | + JA, *this, ResponseFileSupport::AtFileUTF16(), |
| 86 | + Args.MakeArgString(LinkerPath), CmdArgs, Inputs, Output); |
| 87 | + C.addCommand(std::move(LinkCmd)); |
| 88 | +} |
0 commit comments