@@ -172,6 +172,8 @@ BareMetal::BareMetal(const Driver &D, const llvm::Triple &Triple,
172172 : Generic_ELF(D, Triple, Args) {
173173 GCCInstallation.init (Triple, Args);
174174 SysRoot = computeSysRoot ();
175+ UseLD =
176+ Args.getLastArgValue (options::OPT_fuse_ld_EQ).equals_insensitive (" ld" );
175177 if (GCCInstallation.isValid ()) {
176178 Multilibs = GCCInstallation.getMultilibs ();
177179 SelectedMultilibs.assign ({GCCInstallation.getMultilib ()});
@@ -342,6 +344,32 @@ BareMetal::OrderedMultilibs BareMetal::getOrderedMultilibs() const {
342344 return llvm::reverse (Default);
343345}
344346
347+ ToolChain::CXXStdlibType BareMetal::GetDefaultCXXStdlibType () const {
348+ if (getTriple ().isRISCV () && GCCInstallation.isValid ())
349+ return ToolChain::CST_Libstdcxx;
350+ return ToolChain::CST_Libcxx;
351+ }
352+
353+ ToolChain::RuntimeLibType BareMetal::GetDefaultRuntimeLibType () const {
354+ if (getTriple ().isRISCV () && GCCInstallation.isValid ())
355+ return ToolChain::RLT_Libgcc;
356+ return ToolChain::RLT_CompilerRT;
357+ }
358+
359+ ToolChain::UnwindLibType
360+ BareMetal::GetUnwindLibType (const llvm::opt::ArgList &Args) const {
361+ if (getTriple ().isRISCV ())
362+ return ToolChain::UNW_None;
363+
364+ return ToolChain::GetUnwindLibType (Args);
365+ }
366+
367+ const char *BareMetal::getDefaultLinker () const {
368+ if (isUsingLD ())
369+ return " ld" ;
370+ return " ld.lld" ;
371+ }
372+
345373void BareMetal::AddClangSystemIncludeArgs (const ArgList &DriverArgs,
346374 ArgStringList &CC1Args) const {
347375 if (DriverArgs.hasArg (options::OPT_nostdinc))
@@ -535,12 +563,21 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
535563 const llvm::Triple::ArchType Arch = TC.getArch ();
536564 const llvm::Triple &Triple = getToolChain ().getEffectiveTriple ();
537565
538- AddLinkerInputs (TC, Inputs, Args, CmdArgs, JA);
566+ if (!D.SysRoot .empty ())
567+ CmdArgs.push_back (Args.MakeArgString (" --sysroot=" + D.SysRoot ));
539568
540569 CmdArgs.push_back (" -Bstatic" );
541570
542- if (TC.getTriple ().isRISCV () && Args.hasArg (options::OPT_mno_relax))
543- CmdArgs.push_back (" --no-relax" );
571+ if (Triple.isRISCV ()) {
572+ if (Args.hasArg (options::OPT_mno_relax))
573+ CmdArgs.push_back (" --no-relax" );
574+ if (TC.isUsingLD ()) {
575+ CmdArgs.push_back (" -m" );
576+ CmdArgs.push_back (Arch == llvm::Triple::riscv64 ? " elf64lriscv"
577+ : " elf32lriscv" );
578+ }
579+ CmdArgs.push_back (" -X" );
580+ }
544581
545582 if (Triple.isARM () || Triple.isThumb ()) {
546583 bool IsBigEndian = arm::isARMBigEndian (Triple, Args);
@@ -551,19 +588,54 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
551588 CmdArgs.push_back (Arch == llvm::Triple::aarch64_be ? " -EB" : " -EL" );
552589 }
553590
554- if (!Args.hasArg (options::OPT_nostdlib, options::OPT_nostartfiles,
555- options::OPT_r)) {
556- CmdArgs.push_back (Args.MakeArgString (TC.GetFilePath (" crt0.o" )));
591+ bool WantCRTs =
592+ !Args.hasArg (options::OPT_nostdlib, options::OPT_nostartfiles);
593+
594+ const char *crtbegin, *crtend;
595+ if (WantCRTs) {
596+ if (!Args.hasArg (options::OPT_r))
597+ CmdArgs.push_back (Args.MakeArgString (TC.GetFilePath (" crt0.o" )));
598+ if (TC.isUsingLD ()) {
599+ auto RuntimeLib = TC.GetRuntimeLibType (Args);
600+ if (RuntimeLib == ToolChain::RLT_Libgcc) {
601+ crtbegin = " crtbegin.o" ;
602+ crtend = " crtend.o" ;
603+ } else {
604+ assert (RuntimeLib == ToolChain::RLT_CompilerRT);
605+ crtbegin =
606+ TC.getCompilerRTArgString (Args, " crtbegin" , ToolChain::FT_Object);
607+ crtend =
608+ TC.getCompilerRTArgString (Args, " crtend" , ToolChain::FT_Object);
609+ }
610+ CmdArgs.push_back (Args.MakeArgString (TC.GetFilePath (crtbegin)));
611+ }
557612 }
558613
559- Args.addAllArgs (CmdArgs, {options::OPT_L, options::OPT_T_Group,
560- options::OPT_s, options::OPT_t, options::OPT_r});
614+ Args.addAllArgs (CmdArgs,
615+ {options::OPT_L, options::OPT_u, options::OPT_T_Group,
616+ options::OPT_s, options::OPT_t, options::OPT_r});
561617
562618 TC.AddFilePathLibArgs (Args, CmdArgs);
563619
564620 for (const auto &LibPath : TC.getLibraryPaths ())
565621 CmdArgs.push_back (Args.MakeArgString (llvm::Twine (" -L" , LibPath)));
566622
623+ if (D.isUsingLTO ()) {
624+ assert (!Inputs.empty () && " Must have at least one input." );
625+ // Find the first filename InputInfo object.
626+ auto Input = llvm::find_if (
627+ Inputs, [](const InputInfo &II) -> bool { return II.isFilename (); });
628+ if (Input == Inputs.end ())
629+ // For a very rare case, all of the inputs to the linker are
630+ // InputArg. If that happens, just use the first InputInfo.
631+ Input = Inputs.begin ();
632+
633+ addLTOOptions (TC, Args, CmdArgs, Output, *Input,
634+ D.getLTOMode () == LTOK_Thin);
635+ }
636+
637+ AddLinkerInputs (TC, Inputs, Args, CmdArgs, JA);
638+
567639 if (TC.ShouldLinkCXXStdlib (Args)) {
568640 bool OnlyLibstdcxxStatic = Args.hasArg (options::OPT_static_libstdcxx) &&
569641 !Args.hasArg (options::OPT_static);
@@ -576,26 +648,16 @@ void baremetal::Linker::ConstructJob(Compilation &C, const JobAction &JA,
576648 }
577649
578650 if (!Args.hasArg (options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
651+ CmdArgs.push_back (" --start-group" );
579652 AddRunTimeLibs (TC, D, CmdArgs, Args);
580-
581653 CmdArgs.push_back (" -lc" );
654+ if (TC.isUsingLD ())
655+ CmdArgs.push_back (" -lgloss" );
656+ CmdArgs.push_back (" --end-group" );
582657 }
583658
584- if (D.isUsingLTO ()) {
585- assert (!Inputs.empty () && " Must have at least one input." );
586- // Find the first filename InputInfo object.
587- auto Input = llvm::find_if (
588- Inputs, [](const InputInfo &II) -> bool { return II.isFilename (); });
589- if (Input == Inputs.end ())
590- // For a very rare case, all of the inputs to the linker are
591- // InputArg. If that happens, just use the first InputInfo.
592- Input = Inputs.begin ();
593-
594- addLTOOptions (TC, Args, CmdArgs, Output, *Input,
595- D.getLTOMode () == LTOK_Thin);
596- }
597- if (TC.getTriple ().isRISCV ())
598- CmdArgs.push_back (" -X" );
659+ if (TC.isUsingLD () && WantCRTs)
660+ CmdArgs.push_back (Args.MakeArgString (TC.GetFilePath (crtend)));
599661
600662 // The R_ARM_TARGET2 relocation must be treated as R_ARM_REL32 on arm*-*-elf
601663 // and arm*-*-eabi (the default is R_ARM_GOT_PREL, used on arm*-*-linux and
0 commit comments