$OpenBSD: patch-tools_lld_ELF_Driver_cpp,v 1.5 2017/09/01 06:40:28 ajacoutot Exp $

- ELF: Add /usr/lib as default search path.

  GNU ld and gold have this path as default search path.
  If you don't want this directory, pass -nostdlib.
- Enable PIE by default.

Index: tools/lld/ELF/Driver.cpp
--- tools/lld/ELF/Driver.cpp.orig
+++ tools/lld/ELF/Driver.cpp
@@ -410,6 +410,15 @@ static std::vector<StringRef> getArgs(opt::InputArgLis
   return V;
 }
 
+static std::vector<StringRef> getSearchPaths(opt::InputArgList &Args) {
+  std::vector<StringRef> V;
+  if (!Args.hasArg(OPT_nostdlib))
+    V.push_back("=/usr/lib");
+  for (auto *Arg : Args.filtered(OPT_L))
+    V.push_back(Arg->getValue());
+  return V;
+}
+
 static std::string getRpath(opt::InputArgList &Args) {
   std::vector<StringRef> V = getArgs(Args, OPT_rpath);
   return llvm::join(V.begin(), V.end(), ":");
@@ -654,12 +663,13 @@ void LinkerDriver::readConfigs(opt::InputArgList &Args
   Config->OptRemarksWithHotness = Args.hasArg(OPT_opt_remarks_with_hotness);
   Config->Optimize = getInteger(Args, OPT_O, 1);
   Config->OutputFile = Args.getLastArgValue(OPT_o);
-  Config->Pie = getArg(Args, OPT_pie, OPT_nopie, false);
+  Config->Pie = getArg(Args, OPT_pie, OPT_nopie,
+      !Args.hasArg(OPT_shared) && !Args.hasArg(OPT_relocatable));
   Config->PrintGcSections = Args.hasArg(OPT_print_gc_sections);
   Config->Rpath = getRpath(Args);
   Config->Relocatable = Args.hasArg(OPT_relocatable);
   Config->SaveTemps = Args.hasArg(OPT_save_temps);
-  Config->SearchPaths = getArgs(Args, OPT_L);
+  Config->SearchPaths = getSearchPaths(Args);
   Config->SectionStartMap = getSectionStartMap(Args);
   Config->Shared = Args.hasArg(OPT_shared);
   Config->SingleRoRx = Args.hasArg(OPT_no_rosegment);
