Index: ELF/Config.h =================================================================== --- ELF/Config.h +++ ELF/Config.h @@ -128,6 +128,7 @@ bool ZNow; bool ZOrigin; bool ZRelro; + bool ExitEarly; bool ZWxneeded; DiscardPolicy Discard; SortSectionPolicy SortSection; Index: ELF/Driver.h =================================================================== --- ELF/Driver.h +++ ELF/Driver.h @@ -27,7 +27,7 @@ class LinkerDriver { public: - void main(ArrayRef Args); + void main(ArrayRef Args, bool CanExitEarly); void addFile(StringRef Path); void addLibrary(StringRef Name); llvm::LLVMContext Context; // to parse bitcode files Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -39,7 +39,8 @@ Configuration *elf::Config; LinkerDriver *elf::Driver; -bool elf::link(ArrayRef Args, raw_ostream &Error) { +bool elf::link(ArrayRef Args, bool CanExitEarly, + raw_ostream &Error) { HasError = false; ErrorOS = &Error; Argv0 = Args[0]; @@ -51,7 +52,7 @@ Driver = &D; ScriptConfig = &SC; - Driver->main(Args); + Driver->main(Args, CanExitEarly); InputFile::freePool(); return !HasError; } @@ -281,7 +282,7 @@ return Default; } -void LinkerDriver::main(ArrayRef ArgsArr) { +void LinkerDriver::main(ArrayRef ArgsArr, bool CanExitEarly) { ELFOptTable Parser; opt::InputArgList Args = Parser.parse(ArgsArr.slice(1)); if (Args.hasArg(OPT_help)) { @@ -290,6 +291,7 @@ } if (Args.hasArg(OPT_version)) outs() << getLLDVersion() << "\n"; + Config->ExitEarly = CanExitEarly && !Args.hasArg(OPT_full_shutdown); if (const char *Path = getReproduceOption(Args)) { // Note that --reproduce is a debug option so you can ignore it Index: ELF/Options.td =================================================================== --- ELF/Options.td +++ ELF/Options.td @@ -80,6 +80,9 @@ def fini: S<"fini">, MetaVarName<"">, HelpText<"Specify a finalizer function">; +def full_shutdown : Flag<["--"], "full-shutdown">, + HelpText<"Perform a full shutdown instead of calling _exit">; + def format: J<"format=">, MetaVarName<"">, HelpText<"Change the input format of the inputs following this option">; Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -23,6 +23,10 @@ #include "llvm/Support/raw_ostream.h" #include +#if !defined(_MSC_VER) && !defined(__MINGW32__) +#include +#endif + using namespace llvm; using namespace llvm::ELF; using namespace llvm::object; @@ -316,6 +320,11 @@ return; if (auto EC = Buffer->commit()) error(EC, "failed to write to the output file"); + if (Config->ExitEarly) { + outs().flush(); + errs().flush(); + _exit(0); + } } template Index: include/lld/Driver/Driver.h =================================================================== --- include/lld/Driver/Driver.h +++ include/lld/Driver/Driver.h @@ -19,7 +19,7 @@ } namespace elf { -bool link(llvm::ArrayRef Args, +bool link(llvm::ArrayRef Args, bool CanExitEarly, llvm::raw_ostream &Diag = llvm::errs()); } Index: test/ELF/amdgpu-globals.s =================================================================== --- test/ELF/amdgpu-globals.s +++ test/ELF/amdgpu-globals.s @@ -1,5 +1,5 @@ # RUN: llvm-mc -filetype=obj -triple amdgcn--amdhsa -mcpu=kaveri %s -o %t.o -# RUN: lld -flavor gnu %t.o -o %t +# RUN: ld.lld %t.o -o %t # RUN: llvm-readobj -sections -symbols -program-headers %t | FileCheck %s # REQUIRES: amdgpu Index: test/lit.cfg =================================================================== --- test/lit.cfg +++ test/lit.cfg @@ -150,6 +150,8 @@ NoPreJunk = r"(? Args(Argv, Argv + Argc); switch (parseFlavor(Args)) { case Gnu: - return !elf::link(Args); + return !elf::link(Args, true); case WinLink: return !coff::link(Args); case Darwin: