Merge e260fac81 for LLVM update to 339409

Change-Id: I7ceabb7f9c7dc283e3984cd377be1a43363ddba1
diff --git a/COFF/Chunks.cpp b/COFF/Chunks.cpp
index ab6160c..412ff78 100644
--- a/COFF/Chunks.cpp
+++ b/COFF/Chunks.cpp
@@ -98,7 +98,8 @@
   case IMAGE_REL_AMD64_SECTION:  applySecIdx(Off, OS); break;
   case IMAGE_REL_AMD64_SECREL:   applySecRel(this, Off, OS, S); break;
   default:
-    fatal("unsupported relocation type 0x" + Twine::utohexstr(Type));
+    fatal("unsupported relocation type 0x" + Twine::utohexstr(Type) + " in " +
+          toString(File));
   }
 }
 
@@ -112,7 +113,8 @@
   case IMAGE_REL_I386_SECTION:  applySecIdx(Off, OS); break;
   case IMAGE_REL_I386_SECREL:   applySecRel(this, Off, OS, S); break;
   default:
-    fatal("unsupported relocation type 0x" + Twine::utohexstr(Type));
+    fatal("unsupported relocation type 0x" + Twine::utohexstr(Type) + " in " +
+          toString(File));
   }
 }
 
@@ -174,7 +176,8 @@
   case IMAGE_REL_ARM_SECTION:   applySecIdx(Off, OS); break;
   case IMAGE_REL_ARM_SECREL:    applySecRel(this, Off, OS, S); break;
   default:
-    fatal("unsupported relocation type 0x" + Twine::utohexstr(Type));
+    fatal("unsupported relocation type 0x" + Twine::utohexstr(Type) + " in " +
+          toString(File));
   }
 }
 
@@ -284,7 +287,8 @@
   case IMAGE_REL_ARM64_SECREL_LOW12L:  applySecRelLdr(this, Off, OS, S); break;
   case IMAGE_REL_ARM64_SECTION:        applySecIdx(Off, OS); break;
   default:
-    fatal("unsupported relocation type 0x" + Twine::utohexstr(Type));
+    fatal("unsupported relocation type 0x" + Twine::utohexstr(Type) + " in " +
+          toString(File));
   }
 }
 
diff --git a/COFF/Config.h b/COFF/Config.h
index 3ccccb6..3ae50b8 100644
--- a/COFF/Config.h
+++ b/COFF/Config.h
@@ -98,11 +98,13 @@
   bool Debug = false;
   bool DebugDwarf = false;
   bool DebugGHashes = false;
+  bool DebugSymtab = false;
   bool ShowTiming = false;
   unsigned DebugTypes = static_cast<unsigned>(DebugType::None);
   std::vector<std::string> NatvisFiles;
   llvm::SmallString<128> PDBAltPath;
   llvm::SmallString<128> PDBPath;
+  llvm::SmallString<128> PDBSourcePath;
   std::vector<llvm::StringRef> Argv;
 
   // Symbols in this set are considered as live by the garbage collector.
diff --git a/COFF/Driver.cpp b/COFF/Driver.cpp
index 64a7d72..c21f0b4 100644
--- a/COFF/Driver.cpp
+++ b/COFF/Driver.cpp
@@ -56,7 +56,7 @@
 LinkerDriver *Driver;
 
 bool link(ArrayRef<const char *> Args, bool CanExitEarly, raw_ostream &Diag) {
-  errorHandler().LogName = Args[0];
+  errorHandler().LogName = sys::path::filename(Args[0]);
   errorHandler().ErrorOS = &Diag;
   errorHandler().ColorDiagnostics = Diag.has_colors();
   errorHandler().ErrorLimitExceededMsg =
@@ -64,7 +64,6 @@
       " (use /errorlimit:0 to see all errors)";
   errorHandler().ExitEarly = CanExitEarly;
   Config = make<Configuration>();
-  Config->Argv = {Args.begin(), Args.end()};
 
   Symtab = make<SymbolTable>();
 
@@ -76,6 +75,9 @@
     exitLld(errorCount() ? 1 : 0);
 
   freeArena();
+  ObjFile::Instances.clear();
+  ImportFile::Instances.clear();
+  BitcodeFile::Instances.clear();
   return !errorCount();
 }
 
@@ -114,6 +116,19 @@
   });
 }
 
+// Symbol names are mangled by prepending "_" on x86.
+static StringRef mangle(StringRef Sym) {
+  assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN);
+  if (Config->Machine == I386)
+    return Saver.save("_" + Sym);
+  return Sym;
+}
+
+static bool findUnderscoreMangle(StringRef Sym) {
+  StringRef Entry = Symtab->findMangle(mangle(Sym));
+  return !Entry.empty() && !isa<Undefined>(Symtab->find(Entry));
+}
+
 MemoryBufferRef LinkerDriver::takeBuffer(std::unique_ptr<MemoryBuffer> MB) {
   MemoryBufferRef MBRef = *MB;
   make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take ownership
@@ -332,13 +347,24 @@
   return Filename;
 }
 
+static Optional<sys::fs::UniqueID> getUniqueID(StringRef Path) {
+  sys::fs::UniqueID Ret;
+  if (sys::fs::getUniqueID(Path, Ret))
+    return None;
+  return Ret;
+}
+
 // Resolves a file path. This never returns the same path
 // (in that case, it returns None).
 Optional<StringRef> LinkerDriver::findFile(StringRef Filename) {
   StringRef Path = doFindFile(Filename);
-  bool Seen = !VisitedFiles.insert(Path.lower()).second;
-  if (Seen)
-    return None;
+
+  if (Optional<sys::fs::UniqueID> ID = getUniqueID(Path)) {
+    bool Seen = !VisitedFiles.insert(*ID).second;
+    if (Seen)
+      return None;
+  }
+
   if (Path.endswith_lower(".lib"))
     VisitedLibs.insert(sys::path::filename(Path));
   return Path;
@@ -361,11 +387,14 @@
     return None;
   if (!VisitedLibs.insert(Filename.lower()).second)
     return None;
+
   StringRef Path = doFindLib(Filename);
   if (Config->NoDefaultLibs.count(Path))
     return None;
-  if (!VisitedFiles.insert(Path.lower()).second)
-    return None;
+
+  if (Optional<sys::fs::UniqueID> ID = getUniqueID(Path))
+    if (!VisitedFiles.insert(*ID).second)
+      return None;
   return Path;
 }
 
@@ -391,37 +420,38 @@
   return B;
 }
 
-// Symbol names are mangled by appending "_" prefix on x86.
-StringRef LinkerDriver::mangle(StringRef Sym) {
-  assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN);
-  if (Config->Machine == I386)
-    return Saver.save("_" + Sym);
-  return Sym;
-}
-
 // Windows specific -- find default entry point name.
+//
+// There are four different entry point functions for Windows executables,
+// each of which corresponds to a user-defined "main" function. This function
+// infers an entry point from a user-defined "main" function.
 StringRef LinkerDriver::findDefaultEntry() {
-  // User-defined main functions and their corresponding entry points.
-  static const char *Entries[][2] = {
-      {"main", "mainCRTStartup"},
-      {"wmain", "wmainCRTStartup"},
-      {"WinMain", "WinMainCRTStartup"},
-      {"wWinMain", "wWinMainCRTStartup"},
-  };
-  for (auto E : Entries) {
-    StringRef Entry = Symtab->findMangle(mangle(E[0]));
-    if (!Entry.empty() && !isa<Undefined>(Symtab->find(Entry)))
-      return mangle(E[1]);
+  assert(Config->Subsystem != IMAGE_SUBSYSTEM_UNKNOWN &&
+         "must handle /subsystem before calling this");
+
+  // As a special case, if /nodefaultlib is given, we directly look for an
+  // entry point. This is because, if no default library is linked, users
+  // need to define an entry point instead of a "main".
+  bool FindMain = !Config->NoDefaultLibAll;
+  if (Config->Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI) {
+    if (findUnderscoreMangle(FindMain ? "WinMain" : "WinMainCRTStartup"))
+      return mangle("WinMainCRTStartup");
+    if (findUnderscoreMangle(FindMain ? "wWinMain" : "wWinMainCRTStartup"))
+      return mangle("wWinMainCRTStartup");
   }
+  if (findUnderscoreMangle(FindMain ? "main" : "mainCRTStartup"))
+    return mangle("mainCRTStartup");
+  if (findUnderscoreMangle(FindMain ? "wmain" : "wmainCRTStartup"))
+    return mangle("wmainCRTStartup");
   return "";
 }
 
 WindowsSubsystem LinkerDriver::inferSubsystem() {
   if (Config->DLL)
     return IMAGE_SUBSYSTEM_WINDOWS_GUI;
-  if (Symtab->findUnderscore("main") || Symtab->findUnderscore("wmain"))
+  if (findUnderscoreMangle("main") || findUnderscoreMangle("wmain"))
     return IMAGE_SUBSYSTEM_WINDOWS_CUI;
-  if (Symtab->findUnderscore("WinMain") || Symtab->findUnderscore("wWinMain"))
+  if (findUnderscoreMangle("WinMain") || findUnderscoreMangle("wWinMain"))
     return IMAGE_SUBSYSTEM_WINDOWS_GUI;
   return IMAGE_SUBSYSTEM_UNKNOWN;
 }
@@ -646,131 +676,6 @@
   }
 }
 
-// A helper function for filterBitcodeFiles.
-static bool needsRebuilding(MemoryBufferRef MB) {
-  // The MSVC linker doesn't support thin archives, so if it's a thin
-  // archive, we always need to rebuild it.
-  std::unique_ptr<Archive> File =
-      CHECK(Archive::create(MB), "Failed to read " + MB.getBufferIdentifier());
-  if (File->isThin())
-    return true;
-
-  // Returns true if the archive contains at least one bitcode file.
-  for (MemoryBufferRef Member : getArchiveMembers(File.get()))
-    if (identify_magic(Member.getBuffer()) == file_magic::bitcode)
-      return true;
-  return false;
-}
-
-// Opens a given path as an archive file and removes bitcode files
-// from them if exists. This function is to appease the MSVC linker as
-// their linker doesn't like archive files containing non-native
-// object files.
-//
-// If a given archive doesn't contain bitcode files, the archive path
-// is returned as-is. Otherwise, a new temporary file is created and
-// its path is returned.
-static Optional<std::string>
-filterBitcodeFiles(StringRef Path, std::vector<std::string> &TemporaryFiles) {
-  std::unique_ptr<MemoryBuffer> MB = CHECK(
-      MemoryBuffer::getFile(Path, -1, false, true), "could not open " + Path);
-  MemoryBufferRef MBRef = MB->getMemBufferRef();
-  file_magic Magic = identify_magic(MBRef.getBuffer());
-
-  if (Magic == file_magic::bitcode)
-    return None;
-  if (Magic != file_magic::archive)
-    return Path.str();
-  if (!needsRebuilding(MBRef))
-    return Path.str();
-
-  std::unique_ptr<Archive> File =
-      CHECK(Archive::create(MBRef),
-            MBRef.getBufferIdentifier() + ": failed to parse archive");
-
-  std::vector<NewArchiveMember> New;
-  for (MemoryBufferRef Member : getArchiveMembers(File.get()))
-    if (identify_magic(Member.getBuffer()) != file_magic::bitcode)
-      New.emplace_back(Member);
-
-  if (New.empty())
-    return None;
-
-  log("Creating a temporary archive for " + Path + " to remove bitcode files");
-
-  SmallString<128> S;
-  if (std::error_code EC = sys::fs::createTemporaryFile(
-          "lld-" + sys::path::stem(Path), ".lib", S))
-    fatal("cannot create a temporary file: " + EC.message());
-  std::string Temp = S.str();
-  TemporaryFiles.push_back(Temp);
-
-  Error E =
-      llvm::writeArchive(Temp, New, /*WriteSymtab=*/true, Archive::Kind::K_GNU,
-                         /*Deterministics=*/true,
-                         /*Thin=*/false);
-  handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
-    error("failed to create a new archive " + S.str() + ": " + EI.message());
-  });
-  return Temp;
-}
-
-// Create response file contents and invoke the MSVC linker.
-void LinkerDriver::invokeMSVC(opt::InputArgList &Args) {
-  std::string Rsp = "/nologo\n";
-  std::vector<std::string> Temps;
-
-  // Write out archive members that we used in symbol resolution and pass these
-  // to MSVC before any archives, so that MSVC uses the same objects to satisfy
-  // references.
-  for (ObjFile *Obj : ObjFile::Instances) {
-    if (Obj->ParentName.empty())
-      continue;
-    SmallString<128> S;
-    int Fd;
-    if (auto EC = sys::fs::createTemporaryFile(
-            "lld-" + sys::path::filename(Obj->ParentName), ".obj", Fd, S))
-      fatal("cannot create a temporary file: " + EC.message());
-    raw_fd_ostream OS(Fd, /*shouldClose*/ true);
-    OS << Obj->MB.getBuffer();
-    Temps.push_back(S.str());
-    Rsp += quote(S) + "\n";
-  }
-
-  for (auto *Arg : Args) {
-    switch (Arg->getOption().getID()) {
-    case OPT_linkrepro:
-    case OPT_lldmap:
-    case OPT_lldmap_file:
-    case OPT_lldsavetemps:
-    case OPT_msvclto:
-      // LLD-specific options are stripped.
-      break;
-    case OPT_opt:
-      if (!StringRef(Arg->getValue()).startswith("lld"))
-        Rsp += toString(*Arg) + " ";
-      break;
-    case OPT_INPUT: {
-      if (Optional<StringRef> Path = doFindFile(Arg->getValue())) {
-        if (Optional<std::string> S = filterBitcodeFiles(*Path, Temps))
-          Rsp += quote(*S) + "\n";
-        continue;
-      }
-      Rsp += quote(Arg->getValue()) + "\n";
-      break;
-    }
-    default:
-      Rsp += toString(*Arg) + "\n";
-    }
-  }
-
-  std::vector<StringRef> ObjFiles = Symtab->compileBitcodeFiles();
-  runMSVCLinker(Rsp, ObjFiles);
-
-  for (StringRef Path : Temps)
-    sys::fs::remove(Path);
-}
-
 void LinkerDriver::enqueueTask(std::function<void()> Task) {
   TaskQueue.push_back(std::move(Task));
 }
@@ -844,7 +749,7 @@
 
   // Parse command line options.
   ArgParser Parser;
-  opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1));
+  opt::InputArgList Args = Parser.parseLINK(ArgsArr);
 
   // Parse and evaluate -mllvm options.
   std::vector<const char *> V;
@@ -953,6 +858,9 @@
       Config->PDBAltPath = Arg->getValue();
     if (Args.hasArg(OPT_natvis))
       Config->NatvisFiles = Args.getAllArgValues(OPT_natvis);
+
+    if (auto *Arg = Args.getLastArg(OPT_pdb_source_path))
+      Config->PDBSourcePath = Arg->getValue();
   }
 
   // Handle /noentry
@@ -1203,6 +1111,7 @@
       !Config->DLL && Args.hasFlag(OPT_tsaware, OPT_tsaware_no, true);
   Config->DebugDwarf = Args.hasArg(OPT_debug_dwarf);
   Config->DebugGHashes = Args.hasArg(OPT_debug_ghash);
+  Config->DebugSymtab = Args.hasArg(OPT_debug_symtab);
 
   Config->MapFile = getMapFile(Args);
 
@@ -1231,22 +1140,30 @@
   if (errorCount())
     return;
 
-  bool WholeArchiveFlag = Args.hasArg(OPT_wholearchive_flag);
+  std::set<sys::fs::UniqueID> WholeArchives;
+  for (auto *Arg : Args.filtered(OPT_wholearchive_file))
+    if (Optional<StringRef> Path = doFindFile(Arg->getValue()))
+      if (Optional<sys::fs::UniqueID> ID = getUniqueID(*Path))
+        WholeArchives.insert(*ID);
+
+  // A predicate returning true if a given path is an argument for
+  // /wholearchive:, or /wholearchive is enabled globally.
+  // This function is a bit tricky because "foo.obj /wholearchive:././foo.obj"
+  // needs to be handled as "/wholearchive:foo.obj foo.obj".
+  auto IsWholeArchive = [&](StringRef Path) -> bool {
+    if (Args.hasArg(OPT_wholearchive_flag))
+      return true;
+    if (Optional<sys::fs::UniqueID> ID = getUniqueID(Path))
+      return WholeArchives.count(*ID);
+    return false;
+  };
+
   // Create a list of input files. Files can be given as arguments
   // for /defaultlib option.
-  std::vector<MemoryBufferRef> MBs;
-  for (auto *Arg : Args.filtered(OPT_INPUT, OPT_wholearchive_file)) {
-    switch (Arg->getOption().getID()) {
-    case OPT_INPUT:
-      if (Optional<StringRef> Path = findFile(Arg->getValue()))
-        enqueuePath(*Path, WholeArchiveFlag);
-      break;
-    case OPT_wholearchive_file:
-      if (Optional<StringRef> Path = findFile(Arg->getValue()))
-        enqueuePath(*Path, true);
-      break;
-    }
-  }
+  for (auto *Arg : Args.filtered(OPT_INPUT, OPT_wholearchive_file))
+    if (Optional<StringRef> Path = findFile(Arg->getValue()))
+      enqueuePath(*Path, IsWholeArchive(*Path));
+
   for (auto *Arg : Args.filtered(OPT_defaultlib))
     if (Optional<StringRef> Path = findLib(Arg->getValue()))
       enqueuePath(*Path, false);
@@ -1290,25 +1207,6 @@
     error("/dynamicbase:no is not compatible with " +
           machineToStr(Config->Machine));
 
-  // Handle /entry and /dll
-  if (auto *Arg = Args.getLastArg(OPT_entry)) {
-    Config->Entry = addUndefined(mangle(Arg->getValue()));
-  } else if (!Config->Entry && !Config->NoEntry) {
-    if (Args.hasArg(OPT_dll)) {
-      StringRef S = (Config->Machine == I386) ? "__DllMainCRTStartup@12"
-                                              : "_DllMainCRTStartup";
-      Config->Entry = addUndefined(S);
-    } else {
-      // Windows specific -- If entry point name is not given, we need to
-      // infer that from user-defined entry name.
-      StringRef S = findDefaultEntry();
-      if (S.empty())
-        fatal("entry point must be defined");
-      Config->Entry = addUndefined(S);
-      log("Entry name inferred: " + S);
-    }
-  }
-
   // Handle /export
   for (auto *Arg : Args.filtered(OPT_export)) {
     Export E = parseExport(Arg->getValue());
@@ -1334,6 +1232,34 @@
     return;
   }
 
+  // Windows specific -- if no /subsystem is given, we need to infer
+  // that from entry point name.  Must happen before /entry handling,
+  // and after the early return when just writing an import library.
+  if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
+    Config->Subsystem = inferSubsystem();
+    if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
+      fatal("subsystem must be defined");
+  }
+
+  // Handle /entry and /dll
+  if (auto *Arg = Args.getLastArg(OPT_entry)) {
+    Config->Entry = addUndefined(mangle(Arg->getValue()));
+  } else if (!Config->Entry && !Config->NoEntry) {
+    if (Args.hasArg(OPT_dll)) {
+      StringRef S = (Config->Machine == I386) ? "__DllMainCRTStartup@12"
+                                              : "_DllMainCRTStartup";
+      Config->Entry = addUndefined(S);
+    } else {
+      // Windows specific -- If entry point name is not given, we need to
+      // infer that from user-defined entry name.
+      StringRef S = findDefaultEntry();
+      if (S.empty())
+        fatal("entry point must be defined");
+      Config->Entry = addUndefined(S);
+      log("Entry name inferred: " + S);
+    }
+  }
+
   // Handle /delayload
   for (auto *Arg : Args.filtered(OPT_delayload)) {
     Config->DelayLoads.insert(StringRef(Arg->getValue()).lower());
@@ -1361,7 +1287,12 @@
     // /pdbaltpath flag was passed.
     if (Config->PDBAltPath.empty()) {
       Config->PDBAltPath = Config->PDBPath;
+
+      // It's important to make the path absolute and remove dots.  This path
+      // will eventually be written into the PE header, and certain Microsoft
+      // tools won't work correctly if these assumptions are not held.
       sys::fs::make_absolute(Config->PDBAltPath);
+      sys::path::remove_dots(Config->PDBAltPath);
     }
   }
 
@@ -1424,13 +1355,6 @@
   if (errorCount())
     return;
 
-  // If /msvclto is given, we use the MSVC linker to link LTO output files.
-  // This is useful because MSVC link.exe can generate complete PDBs.
-  if (Args.hasArg(OPT_msvclto)) {
-    invokeMSVC(Args);
-    return;
-  }
-
   // Do LTO by compiling bitcode input files to a set of native COFF files then
   // link those files.
   Symtab->addCombinedLTOObjects();
@@ -1441,14 +1365,6 @@
   if (errorCount())
     return;
 
-  // Windows specific -- if no /subsystem is given, we need to infer
-  // that from entry point name.
-  if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) {
-    Config->Subsystem = inferSubsystem();
-    if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN)
-      fatal("subsystem must be defined");
-  }
-
   // Handle /safeseh.
   if (Args.hasFlag(OPT_safeseh, OPT_safeseh_no, false)) {
     for (ObjFile *File : ObjFile::Instances)
@@ -1501,11 +1417,11 @@
       continue;
     }
 
+    // If the symbol isn't common, it must have been replaced with a regular
+    // symbol, which will carry its own alignment.
     auto *DC = dyn_cast<DefinedCommon>(Sym);
-    if (!DC) {
-      warn("/aligncomm symbol " + Name + " of wrong kind");
+    if (!DC)
       continue;
-    }
 
     CommonChunk *C = DC->getChunk();
     C->Alignment = std::max(C->Alignment, Alignment);
diff --git a/COFF/Driver.h b/COFF/Driver.h
index 9bd6c0f..e917955 100644
--- a/COFF/Driver.h
+++ b/COFF/Driver.h
@@ -21,6 +21,7 @@
 #include "llvm/Object/COFF.h"
 #include "llvm/Option/Arg.h"
 #include "llvm/Option/ArgList.h"
+#include "llvm/Support/FileSystem.h"
 #include "llvm/Support/TarWriter.h"
 #include <memory>
 #include <set>
@@ -94,11 +95,14 @@
 
   // Library search path. The first element is always "" (current directory).
   std::vector<StringRef> SearchPaths;
-  std::set<std::string> VisitedFiles;
+
+  // We don't want to add the same file more than once.
+  // Files are uniquified by their filesystem and file number.
+  std::set<llvm::sys::fs::UniqueID> VisitedFiles;
+
   std::set<std::string> VisitedLibs;
 
   Symbol *addUndefined(StringRef Sym);
-  StringRef mangle(StringRef Sym);
 
   // Windows specific -- "main" is not the only main function in Windows.
   // You can choose one from these four -- {w,}{WinMain,main}.
@@ -110,8 +114,6 @@
   StringRef findDefaultEntry();
   WindowsSubsystem inferSubsystem();
 
-  void invokeMSVC(llvm::opt::InputArgList &Args);
-
   void addBuffer(std::unique_ptr<MemoryBuffer> MB, bool WholeArchive);
   void addArchiveBuffer(MemoryBufferRef MBRef, StringRef SymName,
                         StringRef ParentName);
diff --git a/COFF/DriverUtils.cpp b/COFF/DriverUtils.cpp
index 9875119..f7d8fe5 100644
--- a/COFF/DriverUtils.cpp
+++ b/COFF/DriverUtils.cpp
@@ -61,12 +61,7 @@
     StringRef Exe = Saver.save(*ExeOrErr);
     Args.insert(Args.begin(), Exe);
 
-    std::vector<const char *> Vec;
-    for (StringRef S : Args)
-      Vec.push_back(S.data());
-    Vec.push_back(nullptr);
-
-    if (sys::ExecuteAndWait(Args[0], Vec.data()) != 0)
+    if (sys::ExecuteAndWait(Args[0], Args) != 0)
       fatal("ExecuteAndWait failed: " +
             llvm::join(Args.begin(), Args.end(), " "));
   }
@@ -718,26 +713,6 @@
   return MBRef;
 }
 
-// Run MSVC link.exe for given in-memory object files.
-// Command line options are copied from those given to LLD.
-// This is for the /msvclto option.
-void runMSVCLinker(std::string Rsp, ArrayRef<StringRef> Objects) {
-  // Write the in-memory object files to disk.
-  std::vector<TemporaryFile> Temps;
-  for (StringRef S : Objects) {
-    Temps.emplace_back("lto", "obj", S);
-    Rsp += quote(Temps.back().Path) + "\n";
-  }
-
-  log("link.exe " + Rsp);
-
-  // Run MSVC link.exe.
-  Temps.emplace_back("lto", "rsp", Rsp);
-  Executor E("link.exe");
-  E.add(Twine("@" + Temps.back().Path));
-  E.run();
-}
-
 // Create OptTable
 
 // Create prefix string literals used in Options.td
@@ -796,26 +771,31 @@
   // Make InputArgList from string vectors.
   unsigned MissingIndex;
   unsigned MissingCount;
-  SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size());
 
   // We need to get the quoting style for response files before parsing all
   // options so we parse here before and ignore all the options but
   // --rsp-quoting.
-  opt::InputArgList Args = Table.ParseArgs(Vec, MissingIndex, MissingCount);
+  opt::InputArgList Args = Table.ParseArgs(Argv, MissingIndex, MissingCount);
 
   // Expand response files (arguments in the form of @<filename>)
   // and then parse the argument again.
-  cl::ExpandResponseFiles(Saver, getQuotingStyle(Args), Vec);
-  Args = Table.ParseArgs(Vec, MissingIndex, MissingCount);
+  SmallVector<const char *, 256> ExpandedArgv(Argv.data(), Argv.data() + Argv.size());
+  cl::ExpandResponseFiles(Saver, getQuotingStyle(Args), ExpandedArgv);
+  Args = Table.ParseArgs(makeArrayRef(ExpandedArgv).drop_front(), MissingIndex,
+                         MissingCount);
 
   // Print the real command line if response files are expanded.
-  if (Args.hasArg(OPT_verbose) && Argv.size() != Vec.size()) {
+  if (Args.hasArg(OPT_verbose) && Argv.size() != ExpandedArgv.size()) {
     std::string Msg = "Command line:";
-    for (const char *S : Vec)
+    for (const char *S : ExpandedArgv)
       Msg += " " + std::string(S);
     message(Msg);
   }
 
+  // Save the command line after response file expansion so we can write it to
+  // the PDB if necessary.
+  Config->Argv = {ExpandedArgv.begin(), ExpandedArgv.end()};
+
   // Handle /WX early since it converts missing argument warnings to errors.
   errorHandler().FatalWarnings = Args.hasFlag(OPT_WX, OPT_WX_no, false);
 
@@ -826,6 +806,10 @@
 
   for (auto *Arg : Args.filtered(OPT_UNKNOWN))
     warn("ignoring unknown argument: " + Arg->getSpelling());
+
+  if (Args.hasArg(OPT_lib))
+    warn("ignoring /lib since it's not the first argument");
+
   return Args;
 }
 
@@ -863,11 +847,11 @@
   // Concatenate LINK env and command line arguments, and then parse them.
   if (Optional<std::string> S = Process::GetEnv("LINK")) {
     std::vector<const char *> V = tokenize(*S);
-    Argv.insert(Argv.begin(), V.begin(), V.end());
+    Argv.insert(std::next(Argv.begin()), V.begin(), V.end());
   }
   if (Optional<std::string> S = Process::GetEnv("_LINK_")) {
     std::vector<const char *> V = tokenize(*S);
-    Argv.insert(Argv.begin(), V.begin(), V.end());
+    Argv.insert(std::next(Argv.begin()), V.begin(), V.end());
   }
   return parse(Argv);
 }
diff --git a/COFF/ICF.cpp b/COFF/ICF.cpp
index 6297209..7feb3c4 100644
--- a/COFF/ICF.cpp
+++ b/COFF/ICF.cpp
@@ -27,6 +27,7 @@
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/Parallel.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/xxhash.h"
 #include <algorithm>
 #include <atomic>
 #include <vector>
@@ -65,13 +66,6 @@
   std::atomic<bool> Repeat = {false};
 };
 
-// Returns a hash value for S.
-uint32_t ICF::getHash(SectionChunk *C) {
-  return hash_combine(C->getOutputCharacteristics(), C->SectionName,
-                      C->Relocs.size(), uint32_t(C->Header->SizeOfRawData),
-                      C->Checksum, C->getContents());
-}
-
 // Returns true if section S is subject of ICF.
 //
 // Microsoft's documentation
@@ -265,7 +259,7 @@
   // Initially, we use hash values to partition sections.
   for_each(parallel::par, Chunks.begin(), Chunks.end(), [&](SectionChunk *SC) {
     // Set MSB to 1 to avoid collisions with non-hash classs.
-    SC->Class[0] = getHash(SC) | (1 << 31);
+    SC->Class[0] = xxHash64(SC->getContents()) | (1 << 31);
   });
 
   // From now on, sections in Chunks are ordered so that sections in
diff --git a/COFF/InputFiles.cpp b/COFF/InputFiles.cpp
index 9e2345b..289cdb1 100644
--- a/COFF/InputFiles.cpp
+++ b/COFF/InputFiles.cpp
@@ -27,6 +27,7 @@
 #include "llvm/Support/Error.h"
 #include "llvm/Support/ErrorOr.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
 #include "llvm/Target/TargetOptions.h"
 #include <cstring>
 #include <system_error>
@@ -204,7 +205,13 @@
 
 void ObjFile::readAssociativeDefinition(
     COFFSymbolRef Sym, const coff_aux_section_definition *Def) {
-  SectionChunk *Parent = SparseChunks[Def->getNumber(Sym.isBigObj())];
+  readAssociativeDefinition(Sym, Def, Def->getNumber(Sym.isBigObj()));
+}
+
+void ObjFile::readAssociativeDefinition(COFFSymbolRef Sym,
+                                        const coff_aux_section_definition *Def,
+                                        uint32_t ParentSection) {
+  SectionChunk *Parent = SparseChunks[ParentSection];
 
   // If the parent is pending, it probably means that its section definition
   // appears after us in the symbol table. Leave the associated section as
@@ -224,6 +231,35 @@
   }
 }
 
+void ObjFile::recordPrevailingSymbolForMingw(
+    COFFSymbolRef Sym, DenseMap<StringRef, uint32_t> &PrevailingSectionMap) {
+  // For comdat symbols in executable sections, where this is the copy
+  // of the section chunk we actually include instead of discarding it,
+  // add the symbol to a map to allow using it for implicitly
+  // associating .[px]data$<func> sections to it.
+  int32_t SectionNumber = Sym.getSectionNumber();
+  SectionChunk *SC = SparseChunks[SectionNumber];
+  if (SC && SC->getOutputCharacteristics() & IMAGE_SCN_MEM_EXECUTE) {
+    StringRef Name;
+    COFFObj->getSymbolName(Sym, Name);
+    PrevailingSectionMap[Name] = SectionNumber;
+  }
+}
+
+void ObjFile::maybeAssociateSEHForMingw(
+    COFFSymbolRef Sym, const coff_aux_section_definition *Def,
+    const DenseMap<StringRef, uint32_t> &PrevailingSectionMap) {
+  StringRef Name;
+  COFFObj->getSymbolName(Sym, Name);
+  if (Name.consume_front(".pdata$") || Name.consume_front(".xdata$")) {
+    // For MinGW, treat .[px]data$<func> as implicitly associative to
+    // the symbol <func>.
+    auto ParentSym = PrevailingSectionMap.find(Name);
+    if (ParentSym != PrevailingSectionMap.end())
+      readAssociativeDefinition(Sym, Def, ParentSym->second);
+  }
+}
+
 Symbol *ObjFile::createRegular(COFFSymbolRef Sym) {
   SectionChunk *SC = SparseChunks[Sym.getSectionNumber()];
   if (Sym.isExternal()) {
@@ -247,19 +283,24 @@
   std::vector<uint32_t> PendingIndexes;
   PendingIndexes.reserve(NumSymbols);
 
+  DenseMap<StringRef, uint32_t> PrevailingSectionMap;
   std::vector<const coff_aux_section_definition *> ComdatDefs(
       COFFObj->getNumberOfSections() + 1);
 
   for (uint32_t I = 0; I < NumSymbols; ++I) {
     COFFSymbolRef COFFSym = check(COFFObj->getSymbol(I));
+    bool PrevailingComdat;
     if (COFFSym.isUndefined()) {
       Symbols[I] = createUndefined(COFFSym);
     } else if (COFFSym.isWeakExternal()) {
       Symbols[I] = createUndefined(COFFSym);
       uint32_t TagIndex = COFFSym.getAux<coff_aux_weak_external>()->TagIndex;
       WeakAliases.emplace_back(Symbols[I], TagIndex);
-    } else if (Optional<Symbol *> OptSym = createDefined(COFFSym, ComdatDefs)) {
+    } else if (Optional<Symbol *> OptSym =
+                   createDefined(COFFSym, ComdatDefs, PrevailingComdat)) {
       Symbols[I] = *OptSym;
+      if (Config->MinGW && PrevailingComdat)
+        recordPrevailingSymbolForMingw(COFFSym, PrevailingSectionMap);
     } else {
       // createDefined() returns None if a symbol belongs to a section that
       // was pending at the point when the symbol was read. This can happen in
@@ -277,9 +318,19 @@
 
   for (uint32_t I : PendingIndexes) {
     COFFSymbolRef Sym = check(COFFObj->getSymbol(I));
-    if (auto *Def = Sym.getSectionDefinition())
+    if (auto *Def = Sym.getSectionDefinition()) {
       if (Def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
         readAssociativeDefinition(Sym, Def);
+      else if (Config->MinGW)
+        maybeAssociateSEHForMingw(Sym, Def, PrevailingSectionMap);
+    }
+    if (SparseChunks[Sym.getSectionNumber()] == PendingComdat) {
+      StringRef Name;
+      COFFObj->getSymbolName(Sym, Name);
+      log("comdat section " + Name +
+          " without leader and unassociated, discarding");
+      continue;
+    }
     Symbols[I] = createRegular(Sym);
   }
 
@@ -298,18 +349,25 @@
 
 Optional<Symbol *> ObjFile::createDefined(
     COFFSymbolRef Sym,
-    std::vector<const coff_aux_section_definition *> &ComdatDefs) {
-  StringRef Name;
+    std::vector<const coff_aux_section_definition *> &ComdatDefs,
+    bool &Prevailing) {
+  Prevailing = false;
+  auto GetName = [&]() {
+    StringRef S;
+    COFFObj->getSymbolName(Sym, S);
+    return S;
+  };
+
   if (Sym.isCommon()) {
     auto *C = make<CommonChunk>(Sym);
     Chunks.push_back(C);
-    COFFObj->getSymbolName(Sym, Name);
-    Symbol *S =
-        Symtab->addCommon(this, Name, Sym.getValue(), Sym.getGeneric(), C);
-    return S;
+    return Symtab->addCommon(this, GetName(), Sym.getValue(), Sym.getGeneric(),
+                             C);
   }
+
   if (Sym.isAbsolute()) {
-    COFFObj->getSymbolName(Sym, Name);
+    StringRef Name = GetName();
+
     // Skip special symbols.
     if (Name == "@comp.id")
       return nullptr;
@@ -317,39 +375,39 @@
       Feat00Flags = Sym.getValue();
       return nullptr;
     }
+
     if (Sym.isExternal())
       return Symtab->addAbsolute(Name, Sym);
-    else
-      return make<DefinedAbsolute>(Name, Sym);
+    return make<DefinedAbsolute>(Name, Sym);
   }
+
   int32_t SectionNumber = Sym.getSectionNumber();
   if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG)
     return nullptr;
 
   if (llvm::COFF::isReservedSectionNumber(SectionNumber))
-    fatal(toString(this) + ": " + Name +
+    fatal(toString(this) + ": " + GetName() +
           " should not refer to special section " + Twine(SectionNumber));
 
   if ((uint32_t)SectionNumber >= SparseChunks.size())
-    fatal(toString(this) + ": " + Name +
+    fatal(toString(this) + ": " + GetName() +
           " should not refer to non-existent section " + Twine(SectionNumber));
 
   // Handle comdat leader symbols.
   if (const coff_aux_section_definition *Def = ComdatDefs[SectionNumber]) {
     ComdatDefs[SectionNumber] = nullptr;
     Symbol *Leader;
-    bool Prevailing;
     if (Sym.isExternal()) {
-      COFFObj->getSymbolName(Sym, Name);
       std::tie(Leader, Prevailing) =
-          Symtab->addComdat(this, Name, Sym.getGeneric());
+          Symtab->addComdat(this, GetName(), Sym.getGeneric());
     } else {
       Leader = make<DefinedRegular>(this, /*Name*/ "", false,
                                     /*IsExternal*/ false, Sym.getGeneric());
       Prevailing = true;
     }
+
     if (Prevailing) {
-      SectionChunk *C = readSection(SectionNumber, Def, Name);
+      SectionChunk *C = readSection(SectionNumber, Def, GetName());
       SparseChunks[SectionNumber] = C;
       C->Sym = cast<DefinedRegular>(Leader);
       cast<DefinedRegular>(Leader)->Data = &C->Repl;
@@ -431,7 +489,8 @@
   // address pointed by the __imp_ symbol. (This allows you to call
   // DLL functions just like regular non-DLL functions.)
   if (Hdr->getType() == llvm::COFF::IMPORT_CODE)
-    ThunkSym = Symtab->addImportThunk(Name, ImpSym, Hdr->Machine);
+    ThunkSym = Symtab->addImportThunk(
+        Name, cast_or_null<DefinedImportData>(ImpSym), Hdr->Machine);
 }
 
 void BitcodeFile::parse() {
@@ -488,10 +547,7 @@
 
 // Returns the last element of a path, which is supposed to be a filename.
 static StringRef getBasename(StringRef Path) {
-  size_t Pos = Path.find_last_of("\\/");
-  if (Pos == StringRef::npos)
-    return Path;
-  return Path.substr(Pos + 1);
+  return sys::path::filename(Path, sys::path::Style::windows);
 }
 
 // Returns a string in the format of "foo.obj" or "foo.obj(bar.lib)".
diff --git a/COFF/InputFiles.h b/COFF/InputFiles.h
index 9f4db45..2bfb9e4 100644
--- a/COFF/InputFiles.h
+++ b/COFF/InputFiles.h
@@ -13,6 +13,7 @@
 #include "Config.h"
 #include "lld/Common/LLVM.h"
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/LTO/LTO.h"
 #include "llvm/Object/Archive.h"
@@ -157,10 +158,24 @@
       COFFSymbolRef COFFSym,
       const llvm::object::coff_aux_section_definition *Def);
 
+  void readAssociativeDefinition(
+      COFFSymbolRef COFFSym,
+      const llvm::object::coff_aux_section_definition *Def,
+      uint32_t ParentSection);
+
+  void recordPrevailingSymbolForMingw(
+      COFFSymbolRef COFFSym,
+      llvm::DenseMap<StringRef, uint32_t> &PrevailingSectionMap);
+
+  void maybeAssociateSEHForMingw(
+      COFFSymbolRef Sym, const llvm::object::coff_aux_section_definition *Def,
+      const llvm::DenseMap<StringRef, uint32_t> &PrevailingSectionMap);
+
   llvm::Optional<Symbol *>
   createDefined(COFFSymbolRef Sym,
                 std::vector<const llvm::object::coff_aux_section_definition *>
-                    &ComdatDefs);
+                    &ComdatDefs,
+                bool &PrevailingComdat);
   Symbol *createRegular(COFFSymbolRef Sym);
   Symbol *createUndefined(COFFSymbolRef Sym);
 
@@ -207,8 +222,8 @@
 
   static std::vector<ImportFile *> Instances;
 
-  DefinedImportData *ImpSym = nullptr;
-  DefinedImportThunk *ThunkSym = nullptr;
+  Symbol *ImpSym = nullptr;
+  Symbol *ThunkSym = nullptr;
   std::string DLLName;
 
 private:
diff --git a/COFF/Options.td b/COFF/Options.td
index 9051b0b..8536763 100644
--- a/COFF/Options.td
+++ b/COFF/Options.td
@@ -36,6 +36,8 @@
 def heap    : P<"heap", "Size of the heap">;
 def ignore : P<"ignore", "Specify warning codes to ignore">;
 def implib  : P<"implib", "Import library name">;
+def lib : F<"lib">,
+    HelpText<"Act like lib.exe; must be first argument if present">;
 def libpath : P<"libpath", "Additional library search path">;
 def linkrepro : P<"linkrepro", "Dump linker invocation and input files for debugging">;
 def lldltocache : P<"lldltocache", "Path to ThinLTO cached object file directory">;
@@ -139,11 +141,13 @@
 // LLD extensions
 def debug_ghash : F<"debug:ghash">;
 def debug_dwarf : F<"debug:dwarf">;
+def debug_symtab : F<"debug:symtab">;
 def export_all_symbols : F<"export-all-symbols">;
 def kill_at : F<"kill-at">;
 def lldmingw : F<"lldmingw">;
-def msvclto : F<"msvclto">;
 def output_def : Joined<["/", "-"], "output-def:">;
+def pdb_source_path : P<"pdbsourcepath",
+                        "Base path used to make relative source file path absolute in PDB">;
 def rsp_quoting : Joined<["--"], "rsp-quoting=">,
   HelpText<"Quoting style for response files, 'windows' (default) or 'posix'">;
 def dash_dash_version : Flag<["--"], "version">,
diff --git a/COFF/PDB.cpp b/COFF/PDB.cpp
index 6dc2cdd..766bf3f 100644
--- a/COFF/PDB.cpp
+++ b/COFF/PDB.cpp
@@ -932,7 +932,15 @@
   // subsections.
   auto NewChecksums = make_unique<DebugChecksumsSubsection>(PDBStrTab);
   for (FileChecksumEntry &FC : Checksums) {
-    StringRef FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset));
+    SmallString<128> FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset));
+    if (!sys::path::is_absolute(FileName) &&
+        !Config->PDBSourcePath.empty()) {
+      SmallString<128> AbsoluteFileName = Config->PDBSourcePath;
+      sys::path::append(AbsoluteFileName, FileName);
+      sys::path::native(AbsoluteFileName);
+      sys::path::remove_dots(AbsoluteFileName, /*remove_dot_dots=*/true);
+      FileName = std::move(AbsoluteFileName);
+    }
     ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile(*File->ModuleDBI,
                                                           FileName));
     NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum);
diff --git a/COFF/SymbolTable.cpp b/COFF/SymbolTable.cpp
index 6b07c35..62188b5 100644
--- a/COFF/SymbolTable.cpp
+++ b/COFF/SymbolTable.cpp
@@ -189,7 +189,7 @@
   }
 
   for (ObjFile *File : ObjFile::Instances) {
-    size_t SymIndex = -1ull;
+    size_t SymIndex = (size_t)-1;
     for (Symbol *Sym : File->getSymbols()) {
       ++SymIndex;
       if (!Sym)
@@ -206,23 +206,25 @@
   }
 }
 
-std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
+std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, InputFile *File) {
+  bool Inserted = false;
   Symbol *&Sym = SymMap[CachedHashStringRef(Name)];
-  if (Sym)
-    return {Sym, false};
-  Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
-  Sym->IsUsedInRegularObj = false;
-  Sym->PendingArchiveLoad = false;
-  return {Sym, true};
+  if (!Sym) {
+    Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
+    Sym->IsUsedInRegularObj = false;
+    Sym->PendingArchiveLoad = false;
+    Inserted = true;
+  }
+  if (!File || !isa<BitcodeFile>(File))
+    Sym->IsUsedInRegularObj = true;
+  return {Sym, Inserted};
 }
 
 Symbol *SymbolTable::addUndefined(StringRef Name, InputFile *F,
                                   bool IsWeakAlias) {
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(Name);
-  if (!F || !isa<BitcodeFile>(F))
-    S->IsUsedInRegularObj = true;
+  std::tie(S, WasInserted) = insert(Name, F);
   if (WasInserted || (isa<Lazy>(S) && IsWeakAlias)) {
     replaceSymbol<Undefined>(S, Name);
     return S;
@@ -240,7 +242,7 @@
   StringRef Name = Sym.getName();
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(Name);
+  std::tie(S, WasInserted) = insert(Name, nullptr);
   if (WasInserted) {
     replaceSymbol<Lazy>(S, F, Sym);
     return;
@@ -260,7 +262,7 @@
 Symbol *SymbolTable::addAbsolute(StringRef N, COFFSymbolRef Sym) {
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(N);
+  std::tie(S, WasInserted) = insert(N, nullptr);
   S->IsUsedInRegularObj = true;
   if (WasInserted || isa<Undefined>(S) || isa<Lazy>(S))
     replaceSymbol<DefinedAbsolute>(S, N, Sym);
@@ -272,7 +274,7 @@
 Symbol *SymbolTable::addAbsolute(StringRef N, uint64_t VA) {
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(N);
+  std::tie(S, WasInserted) = insert(N, nullptr);
   S->IsUsedInRegularObj = true;
   if (WasInserted || isa<Undefined>(S) || isa<Lazy>(S))
     replaceSymbol<DefinedAbsolute>(S, N, VA);
@@ -284,7 +286,7 @@
 Symbol *SymbolTable::addSynthetic(StringRef N, Chunk *C) {
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(N);
+  std::tie(S, WasInserted) = insert(N, nullptr);
   S->IsUsedInRegularObj = true;
   if (WasInserted || isa<Undefined>(S) || isa<Lazy>(S))
     replaceSymbol<DefinedSynthetic>(S, N, C);
@@ -298,9 +300,7 @@
                                 SectionChunk *C) {
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(N);
-  if (!isa<BitcodeFile>(F))
-    S->IsUsedInRegularObj = true;
+  std::tie(S, WasInserted) = insert(N, F);
   if (WasInserted || !isa<DefinedRegular>(S))
     replaceSymbol<DefinedRegular>(S, F, N, /*IsCOMDAT*/ false,
                                   /*IsExternal*/ true, Sym, C);
@@ -314,9 +314,7 @@
                        const coff_symbol_generic *Sym) {
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(N);
-  if (!isa<BitcodeFile>(F))
-    S->IsUsedInRegularObj = true;
+  std::tie(S, WasInserted) = insert(N, F);
   if (WasInserted || !isa<DefinedRegular>(S)) {
     replaceSymbol<DefinedRegular>(S, F, N, /*IsCOMDAT*/ true,
                                   /*IsExternal*/ true, Sym, nullptr);
@@ -331,9 +329,7 @@
                                const coff_symbol_generic *Sym, CommonChunk *C) {
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(N);
-  if (!isa<BitcodeFile>(F))
-    S->IsUsedInRegularObj = true;
+  std::tie(S, WasInserted) = insert(N, F);
   if (WasInserted || !isa<DefinedCOFF>(S))
     replaceSymbol<DefinedCommon>(S, F, N, Size, Sym, C);
   else if (auto *DC = dyn_cast<DefinedCommon>(S))
@@ -342,30 +338,29 @@
   return S;
 }
 
-DefinedImportData *SymbolTable::addImportData(StringRef N, ImportFile *F) {
+Symbol *SymbolTable::addImportData(StringRef N, ImportFile *F) {
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(N);
+  std::tie(S, WasInserted) = insert(N, nullptr);
   S->IsUsedInRegularObj = true;
   if (WasInserted || isa<Undefined>(S) || isa<Lazy>(S)) {
     replaceSymbol<DefinedImportData>(S, N, F);
-    return cast<DefinedImportData>(S);
+    return S;
   }
 
   reportDuplicate(S, F);
   return nullptr;
 }
 
-DefinedImportThunk *SymbolTable::addImportThunk(StringRef Name,
-                                               DefinedImportData *ID,
-                                               uint16_t Machine) {
+Symbol *SymbolTable::addImportThunk(StringRef Name, DefinedImportData *ID,
+                                    uint16_t Machine) {
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(Name);
+  std::tie(S, WasInserted) = insert(Name, nullptr);
   S->IsUsedInRegularObj = true;
   if (WasInserted || isa<Undefined>(S) || isa<Lazy>(S)) {
     replaceSymbol<DefinedImportThunk>(S, Name, ID, Machine);
-    return cast<DefinedImportThunk>(S);
+    return S;
   }
 
   reportDuplicate(S, ID->File);
diff --git a/COFF/SymbolTable.h b/COFF/SymbolTable.h
index 55481e6..2258257 100644
--- a/COFF/SymbolTable.h
+++ b/COFF/SymbolTable.h
@@ -92,9 +92,9 @@
   Symbol *addCommon(InputFile *F, StringRef N, uint64_t Size,
                     const llvm::object::coff_symbol_generic *S = nullptr,
                     CommonChunk *C = nullptr);
-  DefinedImportData *addImportData(StringRef N, ImportFile *F);
-  DefinedImportThunk *addImportThunk(StringRef Name, DefinedImportData *S,
-                                     uint16_t Machine);
+  Symbol *addImportData(StringRef N, ImportFile *F);
+  Symbol *addImportThunk(StringRef Name, DefinedImportData *S,
+                         uint16_t Machine);
 
   void reportDuplicate(Symbol *Existing, InputFile *NewFile);
 
@@ -108,7 +108,7 @@
   }
 
 private:
-  std::pair<Symbol *, bool> insert(StringRef Name);
+  std::pair<Symbol *, bool> insert(StringRef Name, InputFile *F);
   StringRef findByPrefix(StringRef Prefix);
 
   llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> SymMap;
diff --git a/COFF/Symbols.h b/COFF/Symbols.h
index 783965a..7414376 100644
--- a/COFF/Symbols.h
+++ b/COFF/Symbols.h
@@ -331,8 +331,8 @@
   Chunk *Data;
 };
 
-// If you have a symbol "__imp_foo" in your object file, a symbol name
-// "foo" becomes automatically available as a pointer to "__imp_foo".
+// If you have a symbol "foo" in your object file, a symbol name
+// "__imp_foo" becomes automatically available as a pointer to "foo".
 // This class is for such automatically-created symbols.
 // Yes, this is an odd feature. We didn't intend to implement that.
 // This is here just for compatibility with MSVC.
diff --git a/COFF/Writer.cpp b/COFF/Writer.cpp
index dff87c5..925a8e4 100644
--- a/COFF/Writer.cpp
+++ b/COFF/Writer.cpp
@@ -201,6 +201,7 @@
 
   OutputSection *TextSec;
   OutputSection *RdataSec;
+  OutputSection *BuildidSec;
   OutputSection *DataSec;
   OutputSection *PdataSec;
   OutputSection *IdataSec;
@@ -420,6 +421,7 @@
   TextSec = CreateSection(".text", CODE | R | X);
   CreateSection(".bss", BSS | R | W);
   RdataSec = CreateSection(".rdata", DATA | R);
+  BuildidSec = CreateSection(".buildid", DATA | R);
   DataSec = CreateSection(".data", DATA | R | W);
   PdataSec = CreateSection(".pdata", DATA | R);
   IdataSec = CreateSection(".idata", DATA | R);
@@ -468,12 +470,9 @@
 
   // Finally, move some output sections to the end.
   auto SectionOrder = [&](OutputSection *S) {
-    // .reloc should come last of all since it refers to RVAs of data in the
-    // previous sections.
-    if (S == RelocSec)
-      return 3;
     // Move DISCARDABLE (or non-memory-mapped) sections to the end of file because
-    // the loader cannot handle holes.
+    // the loader cannot handle holes. Stripping can remove other discardable ones
+    // than .reloc, which is first of them (created early).
     if (S->Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
       return 2;
     // .rsrc should come at the end of the non-discardable sections because its
@@ -503,6 +502,8 @@
   if (Config->Debug) {
     DebugDirectory = make<DebugDirectoryChunk>(DebugRecords);
 
+    OutputSection *DebugInfoSec = Config->MinGW ? BuildidSec : RdataSec;
+
     // Make a CVDebugRecordChunk even when /DEBUG:CV is not specified.  We
     // output a PDB no matter what, and this chunk provides the only means of
     // allowing a debugger to match a PDB and an executable.  So we need it even
@@ -511,9 +512,9 @@
     BuildId = CVChunk;
     DebugRecords.push_back(CVChunk);
 
-    RdataSec->addChunk(DebugDirectory);
+    DebugInfoSec->addChunk(DebugDirectory);
     for (Chunk *C : DebugRecords)
-      RdataSec->addChunk(C);
+      DebugInfoSec->addChunk(C);
   }
 
   // Create SEH table. x86-only.
@@ -544,17 +545,24 @@
     if (Config->DLLOrder.count(DLL) == 0)
       Config->DLLOrder[DLL] = Config->DLLOrder.size();
 
-    if (DefinedImportThunk *Thunk = File->ThunkSym)
+    if (File->ThunkSym) {
+      if (!isa<DefinedImportThunk>(File->ThunkSym))
+        fatal(toString(*File->ThunkSym) + " was replaced");
+      DefinedImportThunk *Thunk = cast<DefinedImportThunk>(File->ThunkSym);
       if (File->ThunkLive)
         TextSec->addChunk(Thunk->getChunk());
+    }
 
+    if (File->ImpSym && !isa<DefinedImportData>(File->ImpSym))
+      fatal(toString(*File->ImpSym) + " was replaced");
+    DefinedImportData *ImpSym = cast_or_null<DefinedImportData>(File->ImpSym);
     if (Config->DelayLoads.count(StringRef(File->DLLName).lower())) {
       if (!File->ThunkSym)
         fatal("cannot delay-load " + toString(File) +
-              " due to import of data: " + toString(*File->ImpSym));
-      DelayIdata.add(File->ImpSym);
+              " due to import of data: " + toString(*ImpSym));
+      DelayIdata.add(ImpSym);
     } else {
-      Idata.add(File->ImpSym);
+      Idata.add(ImpSym);
     }
   }
 
@@ -614,7 +622,10 @@
   default: {
     // Don't write symbols that won't be written to the output to the symbol
     // table.
-    OutputSection *OS = Def->getChunk()->getOutputSection();
+    Chunk *C = Def->getChunk();
+    if (!C)
+      return None;
+    OutputSection *OS = C->getOutputSection();
     if (!OS)
       return None;
 
@@ -662,7 +673,7 @@
     Sec->setStringTableOff(addEntryToStringTable(Sec->Name));
   }
 
-  if (Config->DebugDwarf) {
+  if (Config->DebugDwarf || Config->DebugSymtab) {
     for (ObjFile *File : ObjFile::Instances) {
       for (Symbol *B : File->getSymbols()) {
         auto *D = dyn_cast_or_null<Defined>(B);
@@ -1002,13 +1013,19 @@
     if (!SC || !SC->isLive())
       continue;
 
-    // Look for relocations in this section against symbols in executable output
-    // sections.
-    for (Symbol *Ref : SC->symbols()) {
-      // FIXME: Do further testing to see if the relocation type matters,
-      // especially for 32-bit where taking the address of something usually
-      // uses an absolute relocation instead of a relative one.
-      if (auto *D = dyn_cast_or_null<Defined>(Ref)) {
+    for (const coff_relocation &Reloc : SC->Relocs) {
+      if (Config->Machine == I386 && Reloc.Type == COFF::IMAGE_REL_I386_REL32)
+        // Ignore relative relocations on x86. On x86_64 they can't be ignored
+        // since they're also used to compute absolute addresses.
+        continue;
+
+      Symbol *Ref = SC->File->getSymbol(Reloc.SymbolTableIndex);
+      if (auto *D = dyn_cast_or_null<DefinedCOFF>(Ref)) {
+        if (D->getCOFFSymbol().getComplexType() != COFF::IMAGE_SYM_DTYPE_FUNCTION)
+          // Ignore relocations against non-functions (e.g. labels).
+          continue;
+
+        // Mark the symbol if it's in an executable section.
         Chunk *RefChunk = D->getChunk();
         OutputSection *OS = RefChunk ? RefChunk->getOutputSection() : nullptr;
         if (OS && OS->Header.Characteristics & IMAGE_SCN_MEM_EXECUTE)
@@ -1042,6 +1059,11 @@
   if (Config->Entry)
     addSymbolToRVASet(AddressTakenSyms, cast<Defined>(Config->Entry));
 
+  // Ensure sections referenced in the gfid table are 16-byte aligned.
+  for (const ChunkAndOffset &C : AddressTakenSyms)
+    if (C.InputChunk->Alignment < 16)
+      C.InputChunk->Alignment = 16;
+
   maybeAddRVATable(std::move(AddressTakenSyms), "__guard_fids_table",
                    "__guard_fids_count");
 
@@ -1245,7 +1267,7 @@
     return;
   std::vector<Baserel> V;
   for (OutputSection *Sec : OutputSections) {
-    if (Sec == RelocSec)
+    if (Sec->Header.Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
       continue;
     // Collect all locations for base relocations.
     for (Chunk *C : Sec->getChunks())
diff --git a/Common/ErrorHandler.cpp b/Common/ErrorHandler.cpp
index 6dacc2c..d1cb3db 100644
--- a/Common/ErrorHandler.cpp
+++ b/Common/ErrorHandler.cpp
@@ -65,7 +65,18 @@
   raw_svector_ostream OS(S);
   DiagnosticPrinterRawOStream DP(OS);
   DI.print(DP);
-  warn(S);
+  switch (DI.getSeverity()) {
+  case DS_Error:
+    error(S);
+    break;
+  case DS_Warning:
+    warn(S);
+    break;
+  case DS_Remark:
+  case DS_Note:
+    message(S);
+    break;
+  }
 }
 
 void lld::checkError(Error E) {
diff --git a/ELF/Arch/AMDGPU.cpp b/ELF/Arch/AMDGPU.cpp
index 505e0e6..48b27f2 100644
--- a/ELF/Arch/AMDGPU.cpp
+++ b/ELF/Arch/AMDGPU.cpp
@@ -66,6 +66,7 @@
     write32le(Loc, Val);
     break;
   case R_AMDGPU_ABS64:
+  case R_AMDGPU_REL64:
     write64le(Loc, Val);
     break;
   case R_AMDGPU_GOTPCREL32_HI:
@@ -86,6 +87,7 @@
   case R_AMDGPU_REL32:
   case R_AMDGPU_REL32_LO:
   case R_AMDGPU_REL32_HI:
+  case R_AMDGPU_REL64:
     return R_PC;
   case R_AMDGPU_GOTPCREL:
   case R_AMDGPU_GOTPCREL32_LO:
diff --git a/ELF/Arch/ARM.cpp b/ELF/Arch/ARM.cpp
index d99be9b..acf9a61 100644
--- a/ELF/Arch/ARM.cpp
+++ b/ELF/Arch/ARM.cpp
@@ -97,10 +97,19 @@
 }
 
 uint32_t ARM::calcEFlags() const {
+  // The ABIFloatType is used by loaders to detect the floating point calling
+  // convention.
+  uint32_t ABIFloatType = 0;
+  if (Config->ARMVFPArgs == ARMVFPArgKind::Base ||
+      Config->ARMVFPArgs == ARMVFPArgKind::Default)
+    ABIFloatType = EF_ARM_ABI_FLOAT_SOFT;
+  else if (Config->ARMVFPArgs == ARMVFPArgKind::VFP)
+    ABIFloatType = EF_ARM_ABI_FLOAT_HARD;
+
   // We don't currently use any features incompatible with EF_ARM_EABI_VER5,
   // but we don't have any firm guarantees of conformance. Linux AArch64
   // kernels (as of 2016) require an EABI version to be set.
-  return EF_ARM_EABI_VER5;
+  return EF_ARM_EABI_VER5 | ABIFloatType;
 }
 
 RelExpr ARM::getRelExpr(RelType Type, const Symbol &S,
diff --git a/ELF/Arch/Hexagon.cpp b/ELF/Arch/Hexagon.cpp
new file mode 100644
index 0000000..c3c4aba
--- /dev/null
+++ b/ELF/Arch/Hexagon.cpp
@@ -0,0 +1,153 @@
+//===-- Hexagon.cpp -------------------------------------------------------===//
+//
+//                             The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "InputFiles.h"
+#include "Symbols.h"
+#include "Target.h"
+#include "lld/Common/ErrorHandler.h"
+#include "llvm/BinaryFormat/ELF.h"
+#include "llvm/Object/ELF.h"
+#include "llvm/Support/Endian.h"
+
+using namespace llvm;
+using namespace llvm::object;
+using namespace llvm::support::endian;
+using namespace llvm::ELF;
+using namespace lld;
+using namespace lld::elf;
+
+namespace {
+class Hexagon final : public TargetInfo {
+public:
+  uint32_t calcEFlags() const override;
+  RelExpr getRelExpr(RelType Type, const Symbol &S,
+                     const uint8_t *Loc) const override;
+  void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override;
+};
+} // namespace
+
+// Support V60 only at the moment.
+uint32_t Hexagon::calcEFlags() const { return 0x60; }
+
+static uint32_t applyMask(uint32_t Mask, uint32_t Data) {
+  uint32_t Result = 0;
+  size_t Off = 0;
+
+  for (size_t Bit = 0; Bit != 32; ++Bit) {
+    uint32_t ValBit = (Data >> Off) & 1;
+    uint32_t MaskBit = (Mask >> Bit) & 1;
+    if (MaskBit) {
+      Result |= (ValBit << Bit);
+      ++Off;
+    }
+  }
+  return Result;
+}
+
+RelExpr Hexagon::getRelExpr(RelType Type, const Symbol &S,
+                            const uint8_t *Loc) const {
+  switch (Type) {
+  case R_HEX_B15_PCREL:
+  case R_HEX_B15_PCREL_X:
+  case R_HEX_B22_PCREL:
+  case R_HEX_B22_PCREL_X:
+  case R_HEX_B32_PCREL_X:
+  case R_HEX_6_PCREL_X:
+    return R_PC;
+  default:
+    return R_ABS;
+  }
+}
+
+static uint32_t findMaskR6(uint32_t Insn) {
+  // There are (arguably too) many relocation masks for the DSP's
+  // R_HEX_6_X type.  The table below is used to select the correct mask
+  // for the given instruction.
+  struct InstructionMask {
+    uint32_t CmpMask;
+    uint32_t RelocMask;
+  };
+
+  static const InstructionMask R6[] = {
+      {0x38000000, 0x0000201f}, {0x39000000, 0x0000201f},
+      {0x3e000000, 0x00001f80}, {0x3f000000, 0x00001f80},
+      {0x40000000, 0x000020f8}, {0x41000000, 0x000007e0},
+      {0x42000000, 0x000020f8}, {0x43000000, 0x000007e0},
+      {0x44000000, 0x000020f8}, {0x45000000, 0x000007e0},
+      {0x46000000, 0x000020f8}, {0x47000000, 0x000007e0},
+      {0x6a000000, 0x00001f80}, {0x7c000000, 0x001f2000},
+      {0x9a000000, 0x00000f60}, {0x9b000000, 0x00000f60},
+      {0x9c000000, 0x00000f60}, {0x9d000000, 0x00000f60},
+      {0x9f000000, 0x001f0100}, {0xab000000, 0x0000003f},
+      {0xad000000, 0x0000003f}, {0xaf000000, 0x00030078},
+      {0xd7000000, 0x006020e0}, {0xd8000000, 0x006020e0},
+      {0xdb000000, 0x006020e0}, {0xdf000000, 0x006020e0}};
+
+  // Duplex forms have a fixed mask and parse bits 15:14 are always
+  // zero.  Non-duplex insns will always have at least one bit set in the
+  // parse field.
+  if ((0xC000 & Insn) == 0x0)
+    return 0x03f00000;
+
+  for (InstructionMask I : R6)
+    if ((0xff000000 & Insn) == I.CmpMask)
+      return I.RelocMask;
+
+  error("unrecognized instruction for R_HEX_6 relocation: 0x" +
+        utohexstr(Insn));
+  return 0;
+}
+
+static void or32le(uint8_t *P, int32_t V) { write32le(P, read32le(P) | V); }
+
+void Hexagon::relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const {
+  switch (Type) {
+  case R_HEX_NONE:
+    break;
+  case R_HEX_6_PCREL_X:
+  case R_HEX_6_X:
+    or32le(Loc, applyMask(findMaskR6(read32le(Loc)), Val));
+    break;
+  case R_HEX_12_X:
+    or32le(Loc, applyMask(0x000007e0, Val));
+    break;
+  case R_HEX_32_6_X:
+    or32le(Loc, applyMask(0x0fff3fff, Val >> 6));
+    break;
+  case R_HEX_B15_PCREL:
+    or32le(Loc, applyMask(0x00df20fe, Val >> 2));
+    break;
+  case R_HEX_B15_PCREL_X:
+    or32le(Loc, applyMask(0x00df20fe, Val & 0x3f));
+    break;
+  case R_HEX_B22_PCREL:
+    or32le(Loc, applyMask(0x1ff3ffe, Val >> 2));
+    break;
+  case R_HEX_B22_PCREL_X:
+    or32le(Loc, applyMask(0x1ff3ffe, Val & 0x3f));
+    break;
+  case R_HEX_B32_PCREL_X:
+    or32le(Loc, applyMask(0x0fff3fff, Val >> 6));
+    break;
+  case R_HEX_HI16:
+    or32le(Loc, applyMask(0x00c03fff, Val >> 16));
+    break;
+  case R_HEX_LO16:
+    or32le(Loc, applyMask(0x00c03fff, Val));
+    break;
+  default:
+    error(getErrorLocation(Loc) + "unrecognized reloc " + toString(Type));
+    break;
+  }
+}
+
+TargetInfo *elf::getHexagonTargetInfo() {
+  static Hexagon Target;
+  return &Target;
+}
diff --git a/ELF/Arch/Mips.cpp b/ELF/Arch/Mips.cpp
index f2a1aea..dc70401 100644
--- a/ELF/Arch/Mips.cpp
+++ b/ELF/Arch/Mips.cpp
@@ -101,8 +101,6 @@
   case R_MIPS_HIGHEST:
   case R_MICROMIPS_HI16:
   case R_MICROMIPS_LO16:
-  case R_MICROMIPS_HIGHER:
-  case R_MICROMIPS_HIGHEST:
     // R_MIPS_HI16/R_MIPS_LO16 relocations against _gp_disp calculate
     // offset between start of function and 'gp' value which by default
     // equal to the start of .got section. In that case we consider these
@@ -124,8 +122,6 @@
   case R_MIPS_TLS_TPREL_LO16:
   case R_MIPS_TLS_TPREL32:
   case R_MIPS_TLS_TPREL64:
-  case R_MICROMIPS_GOT_OFST:
-  case R_MICROMIPS_SUB:
   case R_MICROMIPS_TLS_DTPREL_HI16:
   case R_MICROMIPS_TLS_DTPREL_LO16:
   case R_MICROMIPS_TLS_TPREL_HI16:
@@ -155,7 +151,6 @@
   case R_MIPS_GOT_DISP:
   case R_MIPS_TLS_GOTTPREL:
   case R_MICROMIPS_CALL16:
-  case R_MICROMIPS_GOT_DISP:
   case R_MICROMIPS_TLS_GOTTPREL:
     return R_MIPS_GOT_OFF;
   case R_MIPS_CALL_HI16:
@@ -168,7 +163,6 @@
   case R_MICROMIPS_GOT_LO16:
     return R_MIPS_GOT_OFF32;
   case R_MIPS_GOT_PAGE:
-  case R_MICROMIPS_GOT_PAGE:
     return R_MIPS_GOT_LOCAL_PAGE;
   case R_MIPS_TLS_GD:
   case R_MICROMIPS_TLS_GD:
@@ -457,9 +451,6 @@
     return std::make_pair(Type2, Val);
   if (Type2 == R_MIPS_SUB && (Type3 == R_MIPS_HI16 || Type3 == R_MIPS_LO16))
     return std::make_pair(Type3, -Val);
-  if (Type2 == R_MICROMIPS_SUB &&
-      (Type3 == R_MICROMIPS_HI16 || Type3 == R_MICROMIPS_LO16))
-    return std::make_pair(Type3, -Val);
   error(getErrorLocation(Loc) + "unsupported relocations combination " +
         Twine(Type));
   return std::make_pair(Type & 0xff, Val);
@@ -538,8 +529,6 @@
   case R_MIPS_TLS_TPREL_LO16:
     writeValue<E>(Loc, Val, 16, 0);
     break;
-  case R_MICROMIPS_GOT_DISP:
-  case R_MICROMIPS_GOT_PAGE:
   case R_MICROMIPS_GPREL16:
   case R_MICROMIPS_TLS_GD:
   case R_MICROMIPS_TLS_LDM:
@@ -548,7 +537,6 @@
     break;
   case R_MICROMIPS_CALL16:
   case R_MICROMIPS_CALL_LO16:
-  case R_MICROMIPS_GOT_OFST:
   case R_MICROMIPS_LO16:
   case R_MICROMIPS_TLS_DTPREL_LO16:
   case R_MICROMIPS_TLS_GOTTPREL:
@@ -580,12 +568,6 @@
   case R_MIPS_HIGHEST:
     writeValue<E>(Loc, Val + 0x800080008000, 16, 48);
     break;
-  case R_MICROMIPS_HIGHER:
-    writeShuffleValue<E>(Loc, Val + 0x80008000, 16, 32);
-    break;
-  case R_MICROMIPS_HIGHEST:
-    writeShuffleValue<E>(Loc, Val + 0x800080008000, 16, 48);
-    break;
   case R_MIPS_JALR:
   case R_MICROMIPS_JALR:
     // Ignore this optimization relocation for now
@@ -653,7 +635,7 @@
 
 template <class ELFT> bool MIPS<ELFT>::usesOnlyLowPageBits(RelType Type) const {
   return Type == R_MIPS_LO16 || Type == R_MIPS_GOT_OFST ||
-         Type == R_MICROMIPS_LO16 || Type == R_MICROMIPS_GOT_OFST;
+         Type == R_MICROMIPS_LO16;
 }
 
 // Return true if the symbol is a PIC function.
diff --git a/ELF/Arch/MipsArchTree.cpp b/ELF/Arch/MipsArchTree.cpp
index 754a470..98ceac3 100644
--- a/ELF/Arch/MipsArchTree.cpp
+++ b/ELF/Arch/MipsArchTree.cpp
@@ -65,25 +65,30 @@
 static StringRef getFpName(bool IsFp64) { return IsFp64 ? "64" : "32"; }
 
 static void checkFlags(ArrayRef<FileFlags> Files) {
+  assert(!Files.empty() && "expected non-empty file list");
+
   uint32_t ABI = Files[0].Flags & (EF_MIPS_ABI | EF_MIPS_ABI2);
   bool Nan = Files[0].Flags & EF_MIPS_NAN2008;
   bool Fp = Files[0].Flags & EF_MIPS_FP64;
 
-  for (const FileFlags &F : Files.slice(1)) {
+  for (const FileFlags &F : Files) {
+    if (Config->Is64 && F.Flags & EF_MIPS_MICROMIPS)
+      error(toString(F.File) + ": microMIPS 64-bit is not supported");
+
     uint32_t ABI2 = F.Flags & (EF_MIPS_ABI | EF_MIPS_ABI2);
     if (ABI != ABI2)
-      error("target ABI '" + getAbiName(ABI) + "' is incompatible with '" +
-            getAbiName(ABI2) + "': " + toString(F.File));
+      error(toString(F.File) + ": ABI '" + getAbiName(ABI2) +
+            "' is incompatible with target ABI '" + getAbiName(ABI) + "'");
 
     bool Nan2 = F.Flags & EF_MIPS_NAN2008;
     if (Nan != Nan2)
-      error("target -mnan=" + getNanName(Nan) + " is incompatible with -mnan=" +
-            getNanName(Nan2) + ": " + toString(F.File));
+      error(toString(F.File) + ": -mnan=" + getNanName(Nan2) +
+            " is incompatible with target -mnan=" + getNanName(Nan));
 
     bool Fp2 = F.Flags & EF_MIPS_FP64;
     if (Fp != Fp2)
-      error("target -mfp" + getFpName(Fp) + " is incompatible with -mfp" +
-            getFpName(Fp2) + ": " + toString(F.File));
+      error(toString(F.File) + ": -mfp" + getFpName(Fp2) +
+            " is incompatible with target -mfp" + getFpName(Fp));
   }
 }
 
@@ -102,11 +107,13 @@
   for (const FileFlags &F : Files.slice(1)) {
     bool IsPic2 = F.Flags & (EF_MIPS_PIC | EF_MIPS_CPIC);
     if (IsPic && !IsPic2)
-      warn("linking abicalls code " + toString(Files[0].File) +
-           " with non-abicalls file: " + toString(F.File));
+      warn(toString(F.File) +
+           ": linking non-abicalls code with abicalls code " +
+           toString(Files[0].File));
     if (!IsPic && IsPic2)
-      warn("linking non-abicalls code " + toString(Files[0].File) +
-           " with abicalls file: " + toString(F.File));
+      warn(toString(F.File) +
+           ": linking abicalls code with non-abicalls code " +
+           toString(Files[0].File));
   }
 
   // Compute the result PIC/non-PIC flag.
@@ -326,7 +333,7 @@
   case Mips::Val_GNU_MIPS_ABI_FP_SOFT:
     return "-msoft-float";
   case Mips::Val_GNU_MIPS_ABI_FP_OLD_64:
-    return "-mips32r2 -mfp64 (old)";
+    return "-mgp32 -mfp64 (old)";
   case Mips::Val_GNU_MIPS_ABI_FP_XX:
     return "-mfpxx";
   case Mips::Val_GNU_MIPS_ABI_FP_64:
@@ -343,9 +350,9 @@
   if (compareMipsFpAbi(NewFlag, OldFlag) >= 0)
     return NewFlag;
   if (compareMipsFpAbi(OldFlag, NewFlag) < 0)
-    error("target floating point ABI '" + getMipsFpAbiName(OldFlag) +
-          "' is incompatible with '" + getMipsFpAbiName(NewFlag) +
-          "': " + FileName);
+    error(FileName + ": floating point ABI '" + getMipsFpAbiName(NewFlag) +
+          "' is incompatible with target floating point ABI '" +
+          getMipsFpAbiName(OldFlag) + "'");
   return OldFlag;
 }
 
diff --git a/ELF/Arch/PPC64.cpp b/ELF/Arch/PPC64.cpp
index 1d5d939..fa3bf6c 100644
--- a/ELF/Arch/PPC64.cpp
+++ b/ELF/Arch/PPC64.cpp
@@ -21,6 +21,7 @@
 using namespace lld::elf;
 
 static uint64_t PPC64TocOffset = 0x8000;
+static uint64_t DynamicThreadPointerOffset = 0x8000;
 
 uint64_t elf::getPPC64TocBase() {
   // The TOC consists of sections .got, .toc, .tocbss, .plt in that order. The
@@ -50,6 +51,11 @@
   void writeGotHeader(uint8_t *Buf) const override;
   bool needsThunk(RelExpr Expr, RelType Type, const InputFile *File,
                   uint64_t BranchAddr, const Symbol &S) const override;
+  RelExpr adjustRelaxExpr(RelType Type, const uint8_t *Data,
+                          RelExpr Expr) const override;
+  void relaxTlsGdToIe(uint8_t *Loc, RelType Type, uint64_t Val) const override;
+  void relaxTlsGdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override;
+  void relaxTlsLdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override;
 };
 } // namespace
 
@@ -57,13 +63,13 @@
 // #higher(value), #highera(value), #highest(value), and #highesta(value)
 // macros defined in section 4.5.1. Relocation Types of the PPC-elf64abi
 // document.
-static uint16_t applyPPCLo(uint64_t V) { return V; }
-static uint16_t applyPPCHi(uint64_t V) { return V >> 16; }
-static uint16_t applyPPCHa(uint64_t V) { return (V + 0x8000) >> 16; }
-static uint16_t applyPPCHigher(uint64_t V) { return V >> 32; }
-static uint16_t applyPPCHighera(uint64_t V) { return (V + 0x8000) >> 32; }
-static uint16_t applyPPCHighest(uint64_t V) { return V >> 48; }
-static uint16_t applyPPCHighesta(uint64_t V) { return (V + 0x8000) >> 48; }
+static uint16_t lo(uint64_t V) { return V; }
+static uint16_t hi(uint64_t V) { return V >> 16; }
+static uint16_t ha(uint64_t V) { return (V + 0x8000) >> 16; }
+static uint16_t higher(uint64_t V) { return V >> 32; }
+static uint16_t highera(uint64_t V) { return (V + 0x8000) >> 32; }
+static uint16_t highest(uint64_t V) { return V >> 48; }
+static uint16_t highesta(uint64_t V) { return (V + 0x8000) >> 48; }
 
 PPC64::PPC64() {
   GotRel = R_PPC64_GLOB_DAT;
@@ -79,6 +85,8 @@
   GotPltHeaderEntriesNum = 2;
   PltHeaderSize = 60;
   NeedsThunks = true;
+  TcbSize = 8;
+  TlsTpOffset = 0x7000;
 
   TlsModuleIndexRel = R_PPC64_DTPMOD64;
   TlsOffsetRel = R_PPC64_DTPREL64;
@@ -104,55 +112,106 @@
 }
 
 static uint32_t getEFlags(InputFile *File) {
-  // Get the e_flag from the input file and issue an error if incompatible
-  // e_flag encountered.
-  uint32_t EFlags;
-  switch (Config->EKind) {
-  case ELF64BEKind:
-    EFlags = cast<ObjFile<ELF64BE>>(File)->getObj().getHeader()->e_flags;
-    break;
-  case ELF64LEKind:
-    EFlags = cast<ObjFile<ELF64LE>>(File)->getObj().getHeader()->e_flags;
-    break;
-  default:
-    llvm_unreachable("unknown Config->EKind");
-  }
-  if (EFlags > 2) {
-    error("incompatible e_flags: " +  toString(File));
-    return 0;
-  }
-  return EFlags;
+  if (Config->EKind == ELF64BEKind)
+    return cast<ObjFile<ELF64BE>>(File)->getObj().getHeader()->e_flags;
+  return cast<ObjFile<ELF64LE>>(File)->getObj().getHeader()->e_flags;
 }
 
+// This file implements v2 ABI. This function makes sure that all
+// object files have v2 or an unspecified version as an ABI version.
 uint32_t PPC64::calcEFlags() const {
-  assert(!ObjectFiles.empty());
-
-  uint32_t NonZeroFlag;
-  for (InputFile *F : makeArrayRef(ObjectFiles)) {
-    NonZeroFlag = getEFlags(F);
-    if (NonZeroFlag)
-      break;
-  }
-
-  // Verify that all input files have either the same e_flags, or zero.
-  for (InputFile *F : makeArrayRef(ObjectFiles)) {
+  for (InputFile *F : ObjectFiles) {
     uint32_t Flag = getEFlags(F);
-    if (Flag == 0 || Flag == NonZeroFlag)
-      continue;
-    error(toString(F) + ": ABI version " + Twine(Flag) +
-          " is not compatible with ABI version " + Twine(NonZeroFlag) +
-          " output");
-    return 0;
+    if (Flag == 1)
+      error(toString(F) + ": ABI version 1 is not supported");
+    else if (Flag > 2)
+      error(toString(F) + ": unrecognized e_flags: " + Twine(Flag));
   }
-
-  if (NonZeroFlag == 1) {
-    error("PPC64 V1 ABI not supported");
-    return 0;
-  }
-
   return 2;
 }
 
+void PPC64::relaxTlsGdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const {
+  // Reference: 3.7.4.2 of the 64-bit ELF V2 abi supplement.
+  // The general dynamic code sequence for a global `x` will look like:
+  // Instruction                    Relocation                Symbol
+  // addis r3, r2, x@got@tlsgd@ha   R_PPC64_GOT_TLSGD16_HA      x
+  // addi  r3, r3, x@got@tlsgd@l    R_PPC64_GOT_TLSGD16_LO      x
+  // bl __tls_get_addr(x@tlsgd)     R_PPC64_TLSGD               x
+  //                                R_PPC64_REL24               __tls_get_addr
+  // nop                            None                       None
+
+  // Relaxing to local exec entails converting:
+  // addis r3, r2, x@got@tlsgd@ha    into      nop
+  // addi  r3, r3, x@got@tlsgd@l     into      addis r3, r13, x@tprel@ha
+  // bl __tls_get_addr(x@tlsgd)      into      nop
+  // nop                             into      addi r3, r3, x@tprel@l
+
+  uint32_t EndianOffset = Config->EKind == ELF64BEKind ? 2U : 0U;
+
+  switch (Type) {
+  case R_PPC64_GOT_TLSGD16_HA:
+    write32(Loc - EndianOffset, 0x60000000); // nop
+    break;
+  case R_PPC64_GOT_TLSGD16_LO:
+    write32(Loc - EndianOffset, 0x3c6d0000); // addis r3, r13
+    relocateOne(Loc, R_PPC64_TPREL16_HA, Val);
+    break;
+  case R_PPC64_TLSGD:
+    write32(Loc, 0x60000000);     // nop
+    write32(Loc + 4, 0x38630000); // addi r3, r3
+    relocateOne(Loc + 4 + EndianOffset, R_PPC64_TPREL16_LO, Val);
+    break;
+  default:
+    llvm_unreachable("unsupported relocation for TLS GD to LE relaxation");
+  }
+}
+
+
+void PPC64::relaxTlsLdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const {
+  // Reference: 3.7.4.3 of the 64-bit ELF V2 abi supplement.
+  // The local dynamic code sequence for a global `x` will look like:
+  // Instruction                    Relocation                Symbol
+  // addis r3, r2, x@got@tlsld@ha   R_PPC64_GOT_TLSLD16_HA      x
+  // addi  r3, r3, x@got@tlsld@l    R_PPC64_GOT_TLSLD16_LO      x
+  // bl __tls_get_addr(x@tlsgd)     R_PPC64_TLSLD               x
+  //                                R_PPC64_REL24               __tls_get_addr
+  // nop                            None                       None
+
+  // Relaxing to local exec entails converting:
+  // addis r3, r2, x@got@tlsld@ha   into      nop
+  // addi  r3, r3, x@got@tlsld@l    into      addis r3, r13, 0
+  // bl __tls_get_addr(x@tlsgd)     into      nop
+  // nop                            into      addi r3, r3, 4096
+
+  uint32_t EndianOffset = Config->EKind == ELF64BEKind ? 2U : 0U;
+  switch (Type) {
+  case R_PPC64_GOT_TLSLD16_HA:
+    write32(Loc - EndianOffset, 0x60000000); // nop
+    break;
+  case R_PPC64_GOT_TLSLD16_LO:
+    write32(Loc - EndianOffset, 0x3c6d0000); // addis r3, r13, 0
+    break;
+  case R_PPC64_TLSLD:
+    write32(Loc, 0x60000000);     // nop
+    write32(Loc + 4, 0x38631000); // addi r3, r3, 4096
+    break;
+  case R_PPC64_DTPREL16:
+  case R_PPC64_DTPREL16_HA:
+  case R_PPC64_DTPREL16_HI:
+  case R_PPC64_DTPREL16_DS:
+  case R_PPC64_DTPREL16_LO:
+  case R_PPC64_DTPREL16_LO_DS:
+  case R_PPC64_GOT_DTPREL16_HA:
+  case R_PPC64_GOT_DTPREL16_LO_DS:
+  case R_PPC64_GOT_DTPREL16_DS:
+  case R_PPC64_GOT_DTPREL16_HI:
+    relocateOne(Loc, Type, Val);
+    break;
+  default:
+    llvm_unreachable("unsupported relocation for TLS LD to LE relaxation");
+  }
+}
+
 RelExpr PPC64::getRelExpr(RelType Type, const Symbol &S,
                           const uint8_t *Loc) const {
   switch (Type) {
@@ -187,8 +246,38 @@
   case R_PPC64_GOT_TPREL16_DS:
   case R_PPC64_GOT_TPREL16_HI:
     return R_GOT_OFF;
+  case R_PPC64_GOT_DTPREL16_HA:
+  case R_PPC64_GOT_DTPREL16_LO_DS:
+  case R_PPC64_GOT_DTPREL16_DS:
+  case R_PPC64_GOT_DTPREL16_HI:
+    return R_TLSLD_GOT_OFF;
+  case R_PPC64_TPREL16:
+  case R_PPC64_TPREL16_HA:
+  case R_PPC64_TPREL16_LO:
+  case R_PPC64_TPREL16_HI:
+  case R_PPC64_TPREL16_DS:
+  case R_PPC64_TPREL16_LO_DS:
+  case R_PPC64_TPREL16_HIGHER:
+  case R_PPC64_TPREL16_HIGHERA:
+  case R_PPC64_TPREL16_HIGHEST:
+  case R_PPC64_TPREL16_HIGHESTA:
+    return R_TLS;
+  case R_PPC64_DTPREL16:
+  case R_PPC64_DTPREL16_DS:
+  case R_PPC64_DTPREL16_HA:
+  case R_PPC64_DTPREL16_HI:
+  case R_PPC64_DTPREL16_HIGHER:
+  case R_PPC64_DTPREL16_HIGHERA:
+  case R_PPC64_DTPREL16_HIGHEST:
+  case R_PPC64_DTPREL16_HIGHESTA:
+  case R_PPC64_DTPREL16_LO:
+  case R_PPC64_DTPREL16_LO_DS:
+  case R_PPC64_DTPREL64:
+    return R_ABS;
   case R_PPC64_TLSGD:
+    return R_TLSDESC_CALL;
   case R_PPC64_TLSLD:
+    return R_TLSLD_HINT;
   case R_PPC64_TLS:
     return R_HINT;
   default:
@@ -232,32 +321,66 @@
 }
 
 static std::pair<RelType, uint64_t> toAddr16Rel(RelType Type, uint64_t Val) {
-  uint64_t V = Val - PPC64TocOffset;
+  // Relocations relative to the toc-base need to be adjusted by the Toc offset.
+  uint64_t TocBiasedVal = Val - PPC64TocOffset;
+  // Relocations relative to dtv[dtpmod] need to be adjusted by the DTP offset.
+  uint64_t DTPBiasedVal = Val - DynamicThreadPointerOffset;
+
   switch (Type) {
+  // TOC biased relocation.
   case R_PPC64_GOT_TLSGD16:
   case R_PPC64_GOT_TLSLD16:
   case R_PPC64_TOC16:
-    return {R_PPC64_ADDR16, V};
+    return {R_PPC64_ADDR16, TocBiasedVal};
   case R_PPC64_TOC16_DS:
   case R_PPC64_GOT_TPREL16_DS:
-    return {R_PPC64_ADDR16_DS, V};
+  case R_PPC64_GOT_DTPREL16_DS:
+    return {R_PPC64_ADDR16_DS, TocBiasedVal};
   case R_PPC64_GOT_TLSGD16_HA:
   case R_PPC64_GOT_TLSLD16_HA:
   case R_PPC64_GOT_TPREL16_HA:
+  case R_PPC64_GOT_DTPREL16_HA:
   case R_PPC64_TOC16_HA:
-    return {R_PPC64_ADDR16_HA, V};
+    return {R_PPC64_ADDR16_HA, TocBiasedVal};
   case R_PPC64_GOT_TLSGD16_HI:
   case R_PPC64_GOT_TLSLD16_HI:
   case R_PPC64_GOT_TPREL16_HI:
+  case R_PPC64_GOT_DTPREL16_HI:
   case R_PPC64_TOC16_HI:
-    return {R_PPC64_ADDR16_HI, V};
+    return {R_PPC64_ADDR16_HI, TocBiasedVal};
   case R_PPC64_GOT_TLSGD16_LO:
   case R_PPC64_GOT_TLSLD16_LO:
   case R_PPC64_TOC16_LO:
-    return {R_PPC64_ADDR16_LO, V};
+    return {R_PPC64_ADDR16_LO, TocBiasedVal};
   case R_PPC64_TOC16_LO_DS:
   case R_PPC64_GOT_TPREL16_LO_DS:
-    return {R_PPC64_ADDR16_LO_DS, V};
+  case R_PPC64_GOT_DTPREL16_LO_DS:
+    return {R_PPC64_ADDR16_LO_DS, TocBiasedVal};
+
+  // Dynamic Thread pointer biased relocation types.
+  case R_PPC64_DTPREL16:
+    return {R_PPC64_ADDR16, DTPBiasedVal};
+  case R_PPC64_DTPREL16_DS:
+    return {R_PPC64_ADDR16_DS, DTPBiasedVal};
+  case R_PPC64_DTPREL16_HA:
+    return {R_PPC64_ADDR16_HA, DTPBiasedVal};
+  case R_PPC64_DTPREL16_HI:
+    return {R_PPC64_ADDR16_HI, DTPBiasedVal};
+  case R_PPC64_DTPREL16_HIGHER:
+    return {R_PPC64_ADDR16_HIGHER, DTPBiasedVal};
+  case R_PPC64_DTPREL16_HIGHERA:
+    return {R_PPC64_ADDR16_HIGHERA, DTPBiasedVal};
+  case R_PPC64_DTPREL16_HIGHEST:
+    return {R_PPC64_ADDR16_HIGHEST, DTPBiasedVal};
+  case R_PPC64_DTPREL16_HIGHESTA:
+    return {R_PPC64_ADDR16_HIGHESTA, DTPBiasedVal};
+  case R_PPC64_DTPREL16_LO:
+    return {R_PPC64_ADDR16_LO, DTPBiasedVal};
+  case R_PPC64_DTPREL16_LO_DS:
+    return {R_PPC64_ADDR16_LO_DS, DTPBiasedVal};
+  case R_PPC64_DTPREL64:
+    return {R_PPC64_ADDR64, DTPBiasedVal};
+
   default:
     return {Type, Val};
   }
@@ -277,39 +400,49 @@
     break;
   }
   case R_PPC64_ADDR16:
+  case R_PPC64_TPREL16:
     checkInt(Loc, Val, 16, Type);
     write16(Loc, Val);
     break;
   case R_PPC64_ADDR16_DS:
+  case R_PPC64_TPREL16_DS:
     checkInt(Loc, Val, 16, Type);
     write16(Loc, (read16(Loc) & 3) | (Val & ~3));
     break;
   case R_PPC64_ADDR16_HA:
   case R_PPC64_REL16_HA:
-    write16(Loc, applyPPCHa(Val));
+  case R_PPC64_TPREL16_HA:
+    write16(Loc, ha(Val));
     break;
   case R_PPC64_ADDR16_HI:
   case R_PPC64_REL16_HI:
-    write16(Loc, applyPPCHi(Val));
+  case R_PPC64_TPREL16_HI:
+    write16(Loc, hi(Val));
     break;
   case R_PPC64_ADDR16_HIGHER:
-    write16(Loc, applyPPCHigher(Val));
+  case R_PPC64_TPREL16_HIGHER:
+    write16(Loc, higher(Val));
     break;
   case R_PPC64_ADDR16_HIGHERA:
-    write16(Loc, applyPPCHighera(Val));
+  case R_PPC64_TPREL16_HIGHERA:
+    write16(Loc, highera(Val));
     break;
   case R_PPC64_ADDR16_HIGHEST:
-    write16(Loc, applyPPCHighest(Val));
+  case R_PPC64_TPREL16_HIGHEST:
+    write16(Loc, highest(Val));
     break;
   case R_PPC64_ADDR16_HIGHESTA:
-    write16(Loc, applyPPCHighesta(Val));
+  case R_PPC64_TPREL16_HIGHESTA:
+    write16(Loc, highesta(Val));
     break;
   case R_PPC64_ADDR16_LO:
   case R_PPC64_REL16_LO:
-    write16(Loc, applyPPCLo(Val));
+  case R_PPC64_TPREL16_LO:
+    write16(Loc, lo(Val));
     break;
   case R_PPC64_ADDR16_LO_DS:
-    write16(Loc, (read16(Loc) & 3) | (applyPPCLo(Val) & ~3));
+  case R_PPC64_TPREL16_LO_DS:
+    write16(Loc, (read16(Loc) & 3) | (lo(Val) & ~3));
     break;
   case R_PPC64_ADDR32:
   case R_PPC64_REL32:
@@ -327,6 +460,9 @@
     write32(Loc, (read32(Loc) & ~Mask) | (Val & Mask));
     break;
   }
+  case R_PPC64_DTPREL64:
+    write64(Loc, Val - DynamicThreadPointerOffset);
+    break;
   default:
     error(getErrorLocation(Loc) + "unrecognized reloc " + Twine(Type));
   }
@@ -339,6 +475,57 @@
   return Type == R_PPC64_REL24 && S.isInPlt();
 }
 
+RelExpr PPC64::adjustRelaxExpr(RelType Type, const uint8_t *Data,
+                               RelExpr Expr) const {
+  if (Expr == R_RELAX_TLS_GD_TO_IE)
+    return R_RELAX_TLS_GD_TO_IE_GOT_OFF;
+  if (Expr == R_RELAX_TLS_LD_TO_LE)
+    return R_RELAX_TLS_LD_TO_LE_ABS;
+  return Expr;
+}
+
+// Reference: 3.7.4.1 of the 64-bit ELF V2 abi supplement.
+// The general dynamic code sequence for a global `x` uses 4 instructions.
+// Instruction                    Relocation                Symbol
+// addis r3, r2, x@got@tlsgd@ha   R_PPC64_GOT_TLSGD16_HA      x
+// addi  r3, r3, x@got@tlsgd@l    R_PPC64_GOT_TLSGD16_LO      x
+// bl __tls_get_addr(x@tlsgd)     R_PPC64_TLSGD               x
+//                                R_PPC64_REL24               __tls_get_addr
+// nop                            None                       None
+//
+// Relaxing to initial-exec entails:
+// 1) Convert the addis/addi pair that builds the address of the tls_index
+//    struct for 'x' to an addis/ld pair that loads an offset from a got-entry.
+// 2) Convert the call to __tls_get_addr to a nop.
+// 3) Convert the nop following the call to an add of the loaded offset to the
+//    thread pointer.
+// Since the nop must directly follow the call, the R_PPC64_TLSGD relocation is
+// used as the relaxation hint for both steps 2 and 3.
+void PPC64::relaxTlsGdToIe(uint8_t *Loc, RelType Type, uint64_t Val) const {
+  switch (Type) {
+  case R_PPC64_GOT_TLSGD16_HA:
+    // This is relaxed from addis rT, r2, sym@got@tlsgd@ha to
+    //                      addis rT, r2, sym@got@tprel@ha.
+    relocateOne(Loc, R_PPC64_GOT_TPREL16_HA, Val);
+    return;
+  case R_PPC64_GOT_TLSGD16_LO: {
+    // Relax from addi  r3, rA, sym@got@tlsgd@l to
+    //            ld r3, sym@got@tprel@l(rA)
+    uint32_t EndianOffset = Config->EKind == ELF64BEKind ? 2U : 0U;
+    uint32_t InputRegister = (read32(Loc - EndianOffset) & (0x1f << 16));
+    write32(Loc - EndianOffset, 0xE8600000 | InputRegister);
+    relocateOne(Loc, R_PPC64_GOT_TPREL16_LO_DS, Val);
+    return;
+  }
+  case R_PPC64_TLSGD:
+    write32(Loc, 0x60000000);     // bl __tls_get_addr(sym@tlsgd) --> nop
+    write32(Loc + 4, 0x7c636A14); // nop --> add r3, r3, r13
+    return;
+  default:
+    llvm_unreachable("unsupported relocation for TLS GD to IE relaxation");
+  }
+}
+
 TargetInfo *elf::getPPC64TargetInfo() {
   static PPC64 Target;
   return &Target;
diff --git a/ELF/Arch/RISCV.cpp b/ELF/Arch/RISCV.cpp
new file mode 100644
index 0000000..18a9c51
--- /dev/null
+++ b/ELF/Arch/RISCV.cpp
@@ -0,0 +1,277 @@
+//===- RISCV.cpp ----------------------------------------------------------===//
+//
+//                             The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "InputFiles.h"
+#include "Target.h"
+
+using namespace llvm;
+using namespace llvm::object;
+using namespace llvm::support::endian;
+using namespace llvm::ELF;
+using namespace lld;
+using namespace lld::elf;
+
+namespace {
+
+class RISCV final : public TargetInfo {
+public:
+  virtual uint32_t calcEFlags() const override;
+  RelExpr getRelExpr(RelType Type, const Symbol &S,
+                     const uint8_t *Loc) const override;
+  void relocateOne(uint8_t *Loc, RelType Type, uint64_t Val) const override;
+};
+
+} // end anonymous namespace
+
+static uint32_t getEFlags(InputFile *F) {
+  if (Config->Is64)
+    return cast<ObjFile<ELF64LE>>(F)->getObj().getHeader()->e_flags;
+  else
+    return cast<ObjFile<ELF32LE>>(F)->getObj().getHeader()->e_flags;
+}
+
+uint32_t RISCV::calcEFlags() const {
+  assert(!ObjectFiles.empty());
+
+  uint32_t Target = getEFlags(ObjectFiles.front());
+
+  for (InputFile *F : ObjectFiles) {
+    uint32_t EFlags = getEFlags(F);
+    if (EFlags & EF_RISCV_RVC)
+      Target |= EF_RISCV_RVC;
+
+    if ((EFlags & EF_RISCV_FLOAT_ABI) != (Target & EF_RISCV_FLOAT_ABI))
+      error(toString(F) +
+            ": cannot link object files with different floating-point ABI");
+
+    if ((EFlags & EF_RISCV_RVE) != (Target & EF_RISCV_RVE))
+      error(toString(F) +
+            ": cannot link object files with different EF_RISCV_RVE");
+  }
+
+  return Target;
+}
+
+RelExpr RISCV::getRelExpr(const RelType Type, const Symbol &S,
+                          const uint8_t *Loc) const {
+  switch (Type) {
+  case R_RISCV_JAL:
+  case R_RISCV_BRANCH:
+  case R_RISCV_CALL:
+  case R_RISCV_PCREL_HI20:
+  case R_RISCV_RVC_BRANCH:
+  case R_RISCV_RVC_JUMP:
+  case R_RISCV_32_PCREL:
+    return R_PC;
+  case R_RISCV_PCREL_LO12_I:
+  case R_RISCV_PCREL_LO12_S:
+    return R_RISCV_PC_INDIRECT;
+  case R_RISCV_RELAX:
+  case R_RISCV_ALIGN:
+    return R_HINT;
+  default:
+    return R_ABS;
+  }
+}
+
+// Extract bits V[Begin:End], where range is inclusive, and Begin must be < 63.
+static uint32_t extractBits(uint64_t V, uint32_t Begin, uint32_t End) {
+  return (V & ((1ULL << (Begin + 1)) - 1)) >> End;
+}
+
+void RISCV::relocateOne(uint8_t *Loc, const RelType Type,
+                        const uint64_t Val) const {
+  switch (Type) {
+  case R_RISCV_32:
+    write32le(Loc, Val);
+    return;
+  case R_RISCV_64:
+    write64le(Loc, Val);
+    return;
+
+  case R_RISCV_RVC_BRANCH: {
+    checkInt(Loc, static_cast<int64_t>(Val) >> 1, 8, Type);
+    checkAlignment(Loc, Val, 2, Type);
+    uint16_t Insn = read16le(Loc) & 0xE383;
+    uint16_t Imm8 = extractBits(Val, 8, 8) << 12;
+    uint16_t Imm4_3 = extractBits(Val, 4, 3) << 10;
+    uint16_t Imm7_6 = extractBits(Val, 7, 6) << 5;
+    uint16_t Imm2_1 = extractBits(Val, 2, 1) << 3;
+    uint16_t Imm5 = extractBits(Val, 5, 5) << 2;
+    Insn |= Imm8 | Imm4_3 | Imm7_6 | Imm2_1 | Imm5;
+
+    write16le(Loc, Insn);
+    return;
+  }
+
+  case R_RISCV_RVC_JUMP: {
+    checkInt(Loc, static_cast<int64_t>(Val) >> 1, 11, Type);
+    checkAlignment(Loc, Val, 2, Type);
+    uint16_t Insn = read16le(Loc) & 0xE003;
+    uint16_t Imm11 = extractBits(Val, 11, 11) << 12;
+    uint16_t Imm4 = extractBits(Val, 4, 4) << 11;
+    uint16_t Imm9_8 = extractBits(Val, 9, 8) << 9;
+    uint16_t Imm10 = extractBits(Val, 10, 10) << 8;
+    uint16_t Imm6 = extractBits(Val, 6, 6) << 7;
+    uint16_t Imm7 = extractBits(Val, 7, 7) << 6;
+    uint16_t Imm3_1 = extractBits(Val, 3, 1) << 3;
+    uint16_t Imm5 = extractBits(Val, 5, 5) << 2;
+    Insn |= Imm11 | Imm4 | Imm9_8 | Imm10 | Imm6 | Imm7 | Imm3_1 | Imm5;
+
+    write16le(Loc, Insn);
+    return;
+  }
+
+  case R_RISCV_RVC_LUI: {
+    int32_t Imm = ((Val + 0x800) >> 12);
+    checkUInt(Loc, Imm, 6, Type);
+    if (Imm == 0) { // `c.lui rd, 0` is illegal, convert to `c.li rd, 0`
+      write16le(Loc, (read16le(Loc) & 0x0F83) | 0x4000);
+    } else {
+      uint16_t Imm17 = extractBits(Val + 0x800, 17, 17) << 12;
+      uint16_t Imm16_12 = extractBits(Val + 0x800, 16, 12) << 2;
+      write16le(Loc, (read16le(Loc) & 0xEF83) | Imm17 | Imm16_12);
+    }
+    return;
+  }
+
+  case R_RISCV_JAL: {
+    checkInt(Loc, static_cast<int64_t>(Val) >> 1, 20, Type);
+    checkAlignment(Loc, Val, 2, Type);
+
+    uint32_t Insn = read32le(Loc) & 0xFFF;
+    uint32_t Imm20 = extractBits(Val, 20, 20) << 31;
+    uint32_t Imm10_1 = extractBits(Val, 10, 1) << 21;
+    uint32_t Imm11 = extractBits(Val, 11, 11) << 20;
+    uint32_t Imm19_12 = extractBits(Val, 19, 12) << 12;
+    Insn |= Imm20 | Imm10_1 | Imm11 | Imm19_12;
+
+    write32le(Loc, Insn);
+    return;
+  }
+
+  case R_RISCV_BRANCH: {
+    checkInt(Loc, static_cast<int64_t>(Val) >> 1, 12, Type);
+    checkAlignment(Loc, Val, 2, Type);
+
+    uint32_t Insn = read32le(Loc) & 0x1FFF07F;
+    uint32_t Imm12 = extractBits(Val, 12, 12) << 31;
+    uint32_t Imm10_5 = extractBits(Val, 10, 5) << 25;
+    uint32_t Imm4_1 = extractBits(Val, 4, 1) << 8;
+    uint32_t Imm11 = extractBits(Val, 11, 11) << 7;
+    Insn |= Imm12 | Imm10_5 | Imm4_1 | Imm11;
+
+    write32le(Loc, Insn);
+    return;
+  }
+
+  // auipc + jalr pair
+  case R_RISCV_CALL: {
+    checkInt(Loc, Val, 32, Type);
+    if (isInt<32>(Val)) {
+      relocateOne(Loc, R_RISCV_PCREL_HI20, Val);
+      relocateOne(Loc + 4, R_RISCV_PCREL_LO12_I, Val);
+    }
+    return;
+  }
+
+  case R_RISCV_PCREL_HI20:
+  case R_RISCV_HI20: {
+    checkInt(Loc, Val, 32, Type);
+    uint32_t Hi = Val + 0x800;
+    write32le(Loc, (read32le(Loc) & 0xFFF) | (Hi & 0xFFFFF000));
+    return;
+  }
+
+  case R_RISCV_PCREL_LO12_I:
+  case R_RISCV_LO12_I: {
+    checkInt(Loc, Val, 32, Type);
+    uint32_t Hi = Val + 0x800;
+    uint32_t Lo = Val - (Hi & 0xFFFFF000);
+    write32le(Loc, (read32le(Loc) & 0xFFFFF) | ((Lo & 0xFFF) << 20));
+    return;
+  }
+
+  case R_RISCV_PCREL_LO12_S:
+  case R_RISCV_LO12_S: {
+    checkInt(Loc, Val, 32, Type);
+    uint32_t Hi = Val + 0x800;
+    uint32_t Lo = Val - (Hi & 0xFFFFF000);
+    uint32_t Imm11_5 = extractBits(Lo, 11, 5) << 25;
+    uint32_t Imm4_0 = extractBits(Lo, 4, 0) << 7;
+    write32le(Loc, (read32le(Loc) & 0x1FFF07F) | Imm11_5 | Imm4_0);
+    return;
+  }
+
+  case R_RISCV_ADD8:
+    *Loc += Val;
+    return;
+  case R_RISCV_ADD16:
+    write16le(Loc, read16le(Loc) + Val);
+    return;
+  case R_RISCV_ADD32:
+    write32le(Loc, read32le(Loc) + Val);
+    return;
+  case R_RISCV_ADD64:
+    write64le(Loc, read64le(Loc) + Val);
+    return;
+  case R_RISCV_SUB6:
+    *Loc = (*Loc & 0xc0) | (((*Loc & 0x3f) - Val) & 0x3f);
+    return;
+  case R_RISCV_SUB8:
+    *Loc -= Val;
+    return;
+  case R_RISCV_SUB16:
+    write16le(Loc, read16le(Loc) - Val);
+    return;
+  case R_RISCV_SUB32:
+    write32le(Loc, read32le(Loc) - Val);
+    return;
+  case R_RISCV_SUB64:
+    write64le(Loc, read64le(Loc) - Val);
+    return;
+  case R_RISCV_SET6:
+    *Loc = (*Loc & 0xc0) | (Val & 0x3f);
+    return;
+  case R_RISCV_SET8:
+    *Loc = Val;
+    return;
+  case R_RISCV_SET16:
+    write16le(Loc, Val);
+    return;
+  case R_RISCV_SET32:
+  case R_RISCV_32_PCREL:
+    write32le(Loc, Val);
+    return;
+
+  case R_RISCV_ALIGN:
+  case R_RISCV_RELAX:
+    return; // Ignored (for now)
+  case R_RISCV_NONE:
+    return; // Do nothing
+
+  // These are handled by the dynamic linker
+  case R_RISCV_RELATIVE:
+  case R_RISCV_COPY:
+  case R_RISCV_JUMP_SLOT:
+  // GP-relative relocations are only produced after relaxation, which
+  // we don't support for now
+  case R_RISCV_GPREL_I:
+  case R_RISCV_GPREL_S:
+  default:
+    error(getErrorLocation(Loc) +
+          "unimplemented relocation: " + toString(Type));
+    return;
+  }
+}
+
+TargetInfo *elf::getRISCVTargetInfo() {
+  static RISCV Target;
+  return &Target;
+}
diff --git a/ELF/Arch/X86_64.cpp b/ELF/Arch/X86_64.cpp
index 8fd28d3..795de8d 100644
--- a/ELF/Arch/X86_64.cpp
+++ b/ELF/Arch/X86_64.cpp
@@ -43,6 +43,8 @@
   void relaxTlsGdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override;
   void relaxTlsIeToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override;
   void relaxTlsLdToLe(uint8_t *Loc, RelType Type, uint64_t Val) const override;
+  bool adjustPrologueForCrossSplitStack(uint8_t *Loc,
+                                        uint8_t *End) const override;
 
 private:
   void relaxGotNoPic(uint8_t *Loc, uint64_t Val, uint8_t Op,
@@ -105,6 +107,11 @@
   case R_X86_64_REX_GOTPCRELX:
   case R_X86_64_GOTTPOFF:
     return R_GOT_PC;
+  case R_X86_64_GOTOFF64:
+    return R_GOTREL_FROM_END;
+  case R_X86_64_GOTPC32:
+  case R_X86_64_GOTPC64:
+    return R_GOTONLY_PC_FROM_END;
   case R_X86_64_NONE:
     return R_NONE;
   default:
@@ -300,6 +307,7 @@
   case R_X86_64_32S:
   case R_X86_64_TPOFF32:
   case R_X86_64_GOT32:
+  case R_X86_64_GOTPC32:
   case R_X86_64_GOTPCREL:
   case R_X86_64_GOTPCRELX:
   case R_X86_64_REX_GOTPCRELX:
@@ -319,6 +327,8 @@
   case R_X86_64_PC64:
   case R_X86_64_SIZE64:
   case R_X86_64_GOT64:
+  case R_X86_64_GOTOFF64:
+  case R_X86_64_GOTPC64:
     write64le(Loc, Val);
     break;
   default:
@@ -461,6 +471,49 @@
   write32le(Loc - 1, Val + 1);
 }
 
+// This anonymous namespace works around a warning bug in
+// old versions of gcc. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480
+namespace {
+
+// A split-stack prologue starts by checking the amount of stack remaining
+// in one of two ways:
+// A) Comparing of the stack pointer to a field in the tcb.
+// B) Or a load of a stack pointer offset with an lea to r10 or r11.
+template <>
+bool X86_64<ELF64LE>::adjustPrologueForCrossSplitStack(uint8_t *Loc,
+                                                       uint8_t *End) const {
+  if (Loc + 8 >= End)
+    return false;
+
+  // Replace "cmp %fs:0x70,%rsp" and subsequent branch
+  // with "stc, nopl 0x0(%rax,%rax,1)"
+  if (memcmp(Loc, "\x64\x48\x3b\x24\x25", 5) == 0) {
+    memcpy(Loc, "\xf9\x0f\x1f\x84\x00\x00\x00\x00", 8);
+    return true;
+  }
+
+  // Adjust "lea X(%rsp),%rYY" to lea "(X - 0x4000)(%rsp),%rYY" where rYY could
+  // be r10 or r11. The lea instruction feeds a subsequent compare which checks
+  // if there is X available stack space. Making X larger effectively reserves
+  // that much additional space. The stack grows downward so subtract the value.
+  if (memcmp(Loc, "\x4c\x8d\x94\x24", 4) == 0 ||
+      memcmp(Loc, "\x4c\x8d\x9c\x24", 4) == 0) {
+    // The offset bytes are encoded four bytes after the start of the
+    // instruction.
+    write32le(Loc + 4, read32le(Loc + 4) - 0x4000);
+    return true;
+  }
+  return false;
+}
+
+template <>
+bool X86_64<ELF32LE>::adjustPrologueForCrossSplitStack(uint8_t *Loc,
+                                                       uint8_t *End) const {
+  llvm_unreachable("Target doesn't support split stacks.");
+}
+
+} // namespace
+
 // These nonstandard PLT entries are to migtigate Spectre v2 security
 // vulnerability. In order to mitigate Spectre v2, we want to avoid indirect
 // branch instructions such as `jmp *GOTPLT(%rip)`. So, in the following PLT
diff --git a/ELF/CMakeLists.txt b/ELF/CMakeLists.txt
index af2bed3..5d080a7 100644
--- a/ELF/CMakeLists.txt
+++ b/ELF/CMakeLists.txt
@@ -12,10 +12,12 @@
   Arch/AMDGPU.cpp
   Arch/ARM.cpp
   Arch/AVR.cpp
+  Arch/Hexagon.cpp
   Arch/Mips.cpp
   Arch/MipsArchTree.cpp
   Arch/PPC.cpp
   Arch/PPC64.cpp
+  Arch/RISCV.cpp
   Arch/SPARCV9.cpp
   Arch/X86.cpp
   Arch/X86_64.cpp
diff --git a/ELF/Config.h b/ELF/Config.h
index 60276f9..763111c 100644
--- a/ELF/Config.h
+++ b/ELF/Config.h
@@ -40,6 +40,9 @@
 // For --discard-{all,locals,none}.
 enum class DiscardPolicy { Default, All, Locals, None };
 
+// For --icf={none,safe,all}.
+enum class ICFLevel { None, Safe, All };
+
 // For --strip-{all,debug}.
 enum class StripPolicy { None, All, Debug };
 
@@ -55,6 +58,9 @@
 // For --target2
 enum class Target2Policy { Abs, Rel, GotRel };
 
+// For tracking ARM Float Argument PCS
+enum class ARMVFPArgKind { Default, Base, VFP, ToolChain };
+
 struct SymbolVersion {
   llvm::StringRef Name;
   bool IsExternCpp;
@@ -80,6 +86,7 @@
   llvm::StringMap<uint64_t> SectionStartMap;
   llvm::StringRef Chroot;
   llvm::StringRef DynamicLinker;
+  llvm::StringRef DwoDir;
   llvm::StringRef Entry;
   llvm::StringRef Emulation;
   llvm::StringRef Fini;
@@ -113,7 +120,7 @@
                   uint64_t>
       CallGraphProfile;
   bool AllowMultipleDefinition;
-  bool AndroidPackDynRelocs = false;
+  bool AndroidPackDynRelocs;
   bool ARMHasBlx = false;
   bool ARMHasMovtMovw = false;
   bool ARMJ1J2BranchEncoding = false;
@@ -129,15 +136,16 @@
   bool EhFrameHdr;
   bool EmitRelocs;
   bool EnableNewDtags;
+  bool ExecuteOnly;
   bool ExportDynamic;
   bool FixCortexA53Errata843419;
+  bool FormatBinary = false;
   bool GcSections;
   bool GdbIndex;
   bool GnuHash = false;
   bool GnuUnique;
   bool HasDynamicList = false;
   bool HasDynSymTab;
-  bool ICF;
   bool IgnoreDataAddressEquality;
   bool IgnoreFunctionAddressEquality;
   bool LTODebugPassManager;
@@ -153,6 +161,7 @@
   bool PrintGcSections;
   bool PrintIcfSections;
   bool Relocatable;
+  bool RelrPackDynRelocs;
   bool SaveTemps;
   bool SingleRoRx;
   bool Shared;
@@ -163,6 +172,7 @@
   bool ThinLTOEmitImportsFiles;
   bool ThinLTOIndexOnly;
   bool UndefinedVersion;
+  bool UseAndroidRelrTags = false;
   bool WarnBackrefs;
   bool WarnCommon;
   bool WarnMissingEntry;
@@ -172,6 +182,7 @@
   bool ZCopyreloc;
   bool ZExecstack;
   bool ZHazardplt;
+  bool ZInitfirst;
   bool ZKeepTextSectionPrefix;
   bool ZNodelete;
   bool ZNodlopen;
@@ -183,17 +194,20 @@
   bool ZRetpolineplt;
   bool ZWxneeded;
   DiscardPolicy Discard;
+  ICFLevel ICF;
   OrphanHandlingPolicy OrphanHandling;
   SortSectionPolicy SortSection;
   StripPolicy Strip;
   UnresolvedPolicy UnresolvedSymbols;
   Target2Policy Target2;
+  ARMVFPArgKind ARMVFPArgs = ARMVFPArgKind::Default;
   BuildIdKind BuildId = BuildIdKind::None;
   ELFKind EKind = ELFNoneKind;
   uint16_t DefaultSymbolVersion = llvm::ELF::VER_NDX_GLOBAL;
   uint16_t EMachine = llvm::ELF::EM_NONE;
   llvm::Optional<uint64_t> ImageBase;
   uint64_t MaxPageSize;
+  uint64_t MipsGotSize;
   uint64_t ZStackSize;
   unsigned LTOPartitions;
   unsigned LTOO;
diff --git a/ELF/Driver.cpp b/ELF/Driver.cpp
index 2e3a425..a030d11 100644
--- a/ELF/Driver.cpp
+++ b/ELF/Driver.cpp
@@ -51,6 +51,7 @@
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Compression.h"
+#include "llvm/Support/LEB128.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/TarWriter.h"
 #include "llvm/Support/TargetSelect.h"
@@ -73,7 +74,7 @@
 
 bool elf::link(ArrayRef<const char *> Args, bool CanExitEarly,
                raw_ostream &Error) {
-  errorHandler().LogName = Args[0];
+  errorHandler().LogName = sys::path::filename(Args[0]);
   errorHandler().ErrorLimitExceededMsg =
       "too many errors emitted, stopping now (use "
       "-error-limit=0 to see all errors)";
@@ -124,9 +125,11 @@
           .Case("elf32_x86_64", {ELF32LEKind, EM_X86_64})
           .Cases("elf32btsmip", "elf32btsmipn32", {ELF32BEKind, EM_MIPS})
           .Cases("elf32ltsmip", "elf32ltsmipn32", {ELF32LEKind, EM_MIPS})
+          .Case("elf32lriscv", {ELF32LEKind, EM_RISCV})
           .Case("elf32ppc", {ELF32BEKind, EM_PPC})
           .Case("elf64btsmip", {ELF64BEKind, EM_MIPS})
           .Case("elf64ltsmip", {ELF64LEKind, EM_MIPS})
+          .Case("elf64lriscv", {ELF64LEKind, EM_RISCV})
           .Case("elf64ppc", {ELF64BEKind, EM_PPC64})
           .Case("elf64lppc", {ELF64LEKind, EM_PPC64})
           .Cases("elf_amd64", "elf_x86_64", {ELF64LEKind, EM_X86_64})
@@ -182,7 +185,7 @@
     return;
   MemoryBufferRef MBRef = *Buffer;
 
-  if (InBinary) {
+  if (Config->FormatBinary) {
     Files.push_back(make<BinaryFile>(MBRef));
     return;
   }
@@ -294,11 +297,21 @@
       error("-r and -shared may not be used together");
     if (Config->GcSections)
       error("-r and --gc-sections may not be used together");
-    if (Config->ICF)
+    if (Config->GdbIndex)
+      error("-r and --gdb-index may not be used together");
+    if (Config->ICF != ICFLevel::None)
       error("-r and --icf may not be used together");
     if (Config->Pie)
       error("-r and -pie may not be used together");
   }
+
+  if (Config->ExecuteOnly) {
+    if (Config->EMachine != EM_AARCH64)
+      error("-execute-only is only supported on AArch64 targets");
+
+    if (Config->SingleRoRx && !Script->HasSectionsCommand)
+      error("-execute-only and -no-rosegment cannot be used together");
+  }
 }
 
 static const char *getReproduceOption(opt::InputArgList &Args) {
@@ -325,6 +338,25 @@
   return Default;
 }
 
+static bool isKnown(StringRef S) {
+  return S == "combreloc" || S == "copyreloc" || S == "defs" ||
+         S == "execstack" || S == "hazardplt" || S == "initfirst" ||
+         S == "keep-text-section-prefix" || S == "lazy" || S == "muldefs" ||
+         S == "nocombreloc" || S == "nocopyreloc" || S == "nodelete" ||
+         S == "nodlopen" || S == "noexecstack" ||
+         S == "nokeep-text-section-prefix" || S == "norelro" || S == "notext" ||
+         S == "now" || S == "origin" || S == "relro" || S == "retpolineplt" ||
+         S == "rodynamic" || S == "text" || S == "wxneeded" ||
+         S.startswith("max-page-size=") || S.startswith("stack-size=");
+}
+
+// Report an error for an unknown -z option.
+static void checkZOptions(opt::InputArgList &Args) {
+  for (auto *Arg : Args.filtered(OPT_z))
+    if (!isKnown(Arg->getValue()))
+      error("unknown -z value: " + StringRef(Arg->getValue()));
+}
+
 void LinkerDriver::main(ArrayRef<const char *> ArgsArr) {
   ELFOptTable Parser;
   opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
@@ -380,8 +412,12 @@
   }
 
   readConfigs(Args);
+  checkZOptions(Args);
   initLLVM();
   createFiles(Args);
+  if (errorCount())
+    return;
+
   inferMachineType();
   setConfigs(Args);
   checkOptions(Args);
@@ -463,12 +499,11 @@
 }
 
 static bool isOutputFormatBinary(opt::InputArgList &Args) {
-  if (auto *Arg = Args.getLastArg(OPT_oformat)) {
-    StringRef S = Arg->getValue();
-    if (S == "binary")
-      return true;
+  StringRef S = Args.getLastArgValue(OPT_oformat, "elf");
+  if (S == "binary")
+    return true;
+  if (!S.startswith("elf"))
     error("unknown --oformat value: " + S);
-  }
   return false;
 }
 
@@ -494,6 +529,15 @@
   return Arg->getValue();
 }
 
+static ICFLevel getICF(opt::InputArgList &Args) {
+  auto *Arg = Args.getLastArg(OPT_icf_none, OPT_icf_safe, OPT_icf_all);
+  if (!Arg || Arg->getOption().getID() == OPT_icf_none)
+    return ICFLevel::None;
+  if (Arg->getOption().getID() == OPT_icf_safe)
+    return ICFLevel::Safe;
+  return ICFLevel::All;
+}
+
 static StripPolicy getStrip(opt::InputArgList &Args) {
   if (Args.hasArg(OPT_relocatable))
     return StripPolicy::None;
@@ -584,6 +628,20 @@
   return {BuildIdKind::None, {}};
 }
 
+static std::pair<bool, bool> getPackDynRelocs(opt::InputArgList &Args) {
+  StringRef S = Args.getLastArgValue(OPT_pack_dyn_relocs, "none");
+  if (S == "android")
+    return {true, false};
+  if (S == "relr")
+    return {false, true};
+  if (S == "android+relr")
+    return {true, true};
+
+  if (S != "none")
+    error("unknown -pack-dyn-relocs format: " + S);
+  return {false, false};
+}
+
 static void readCallGraph(MemoryBufferRef MB) {
   // Build a map from symbol name to section
   DenseMap<StringRef, const Symbol *> SymbolNameToSymbol;
@@ -591,35 +649,28 @@
     for (Symbol *Sym : File->getSymbols())
       SymbolNameToSymbol[Sym->getName()] = Sym;
 
+  auto FindSection = [&](StringRef SymName) -> InputSectionBase * {
+    const Symbol *Sym = SymbolNameToSymbol.lookup(SymName);
+    if (Sym)
+      warnUnorderableSymbol(Sym);
+    else if (Config->WarnSymbolOrdering)
+      warn(MB.getBufferIdentifier() + ": no such symbol: " + SymName);
+
+    if (const Defined *DR = dyn_cast_or_null<Defined>(Sym))
+      return dyn_cast_or_null<InputSectionBase>(DR->Section);
+    return nullptr;
+  };
+
   for (StringRef L : args::getLines(MB)) {
     SmallVector<StringRef, 3> Fields;
     L.split(Fields, ' ');
-    if (Fields.size() != 3)
-      fatal("parse error");
     uint64_t Count;
-    if (!to_integer(Fields[2], Count))
-      fatal("parse error");
-    const Symbol *FromSym = SymbolNameToSymbol.lookup(Fields[0]);
-    const Symbol *ToSym = SymbolNameToSymbol.lookup(Fields[1]);
-    if (Config->WarnSymbolOrdering) {
-      if (!FromSym)
-        warn("call graph file: no such symbol: " + Fields[0]);
-      if (!ToSym)
-        warn("call graph file: no such symbol: " + Fields[1]);
-    }
-    if (!FromSym || !ToSym || Count == 0)
-      continue;
-    warnUnorderableSymbol(FromSym);
-    warnUnorderableSymbol(ToSym);
-    const Defined *FromSymD = dyn_cast<Defined>(FromSym);
-    const Defined *ToSymD = dyn_cast<Defined>(ToSym);
-    if (!FromSymD || !ToSymD)
-      continue;
-    const auto *FromSB = dyn_cast_or_null<InputSectionBase>(FromSymD->Section);
-    const auto *ToSB = dyn_cast_or_null<InputSectionBase>(ToSymD->Section);
-    if (!FromSB || !ToSB)
-      continue;
-    Config->CallGraphProfile[std::make_pair(FromSB, ToSB)] += Count;
+    if (Fields.size() != 3 || !to_integer(Fields[2], Count))
+      fatal(MB.getBufferIdentifier() + ": parse error");
+
+    if (const InputSectionBase *FromSB = FindSection(Fields[0]))
+      if (const InputSectionBase *ToSB = FindSection(Fields[1]))
+        Config->CallGraphProfile[std::make_pair(FromSB, ToSB)] += Count;
   }
 }
 
@@ -692,6 +743,7 @@
   Config->Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true);
   Config->DisableVerify = Args.hasArg(OPT_disable_verify);
   Config->Discard = getDiscard(Args);
+  Config->DwoDir = Args.getLastArgValue(OPT_plugin_opt_dwo_dir_eq);
   Config->DynamicLinker = getDynamicLinker(Args);
   Config->EhFrameHdr =
       Args.hasFlag(OPT_eh_frame_hdr, OPT_no_eh_frame_hdr, false);
@@ -699,6 +751,8 @@
   Config->EnableNewDtags =
       Args.hasFlag(OPT_enable_new_dtags, OPT_disable_new_dtags, true);
   Config->Entry = Args.getLastArgValue(OPT_entry);
+  Config->ExecuteOnly =
+      Args.hasFlag(OPT_execute_only, OPT_no_execute_only, false);
   Config->ExportDynamic =
       Args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, false);
   Config->FilterList = args::getStrings(Args, OPT_filter);
@@ -707,7 +761,7 @@
   Config->GcSections = Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, false);
   Config->GnuUnique = Args.hasFlag(OPT_gnu_unique, OPT_no_gnu_unique, true);
   Config->GdbIndex = Args.hasFlag(OPT_gdb_index, OPT_no_gdb_index, false);
-  Config->ICF = Args.hasFlag(OPT_icf_all, OPT_icf_none, false);
+  Config->ICF = getICF(Args);
   Config->IgnoreDataAddressEquality =
       Args.hasArg(OPT_ignore_data_address_equality);
   Config->IgnoreFunctionAddressEquality =
@@ -722,6 +776,7 @@
   Config->LTOPartitions = args::getInteger(Args, OPT_lto_partitions, 1);
   Config->LTOSampleProfile = Args.getLastArgValue(OPT_lto_sample_profile);
   Config->MapFile = Args.getLastArgValue(OPT_Map);
+  Config->MipsGotSize = args::getInteger(Args, OPT_mips_got_size, 0xfff0);
   Config->MergeArmExidx =
       Args.hasFlag(OPT_merge_exidx_entries, OPT_no_merge_exidx_entries, true);
   Config->NoinhibitExec = Args.hasArg(OPT_noinhibit_exec);
@@ -770,6 +825,8 @@
   Config->Undefined = args::getStrings(Args, OPT_undefined);
   Config->UndefinedVersion =
       Args.hasFlag(OPT_undefined_version, OPT_no_undefined_version, true);
+  Config->UseAndroidRelrTags = Args.hasFlag(
+      OPT_use_android_relr_tags, OPT_no_use_android_relr_tags, false);
   Config->UnresolvedSymbols = getUnresolvedSymbolPolicy(Args);
   Config->WarnBackrefs =
       Args.hasFlag(OPT_warn_backrefs, OPT_no_warn_backrefs, false);
@@ -780,6 +837,7 @@
   Config->ZCopyreloc = getZFlag(Args, "copyreloc", "nocopyreloc", true);
   Config->ZExecstack = getZFlag(Args, "execstack", "noexecstack", false);
   Config->ZHazardplt = hasZOption(Args, "hazardplt");
+  Config->ZInitfirst = hasZOption(Args, "initfirst");
   Config->ZKeepTextSectionPrefix = getZFlag(
       Args, "keep-text-section-prefix", "nokeep-text-section-prefix", false);
   Config->ZNodelete = hasZOption(Args, "nodelete");
@@ -846,13 +904,8 @@
 
   std::tie(Config->BuildId, Config->BuildIdVector) = getBuildId(Args);
 
-  if (auto *Arg = Args.getLastArg(OPT_pack_dyn_relocs)) {
-    StringRef S = Arg->getValue();
-    if (S == "android")
-      Config->AndroidPackDynRelocs = true;
-    else if (S != "none")
-      error("unknown -pack-dyn-relocs format: " + S);
-  }
+  std::tie(Config->AndroidPackDynRelocs, Config->RelrPackDynRelocs) =
+      getPackDynRelocs(Args);
 
   if (auto *Arg = Args.getLastArg(OPT_symbol_ordering_file))
     if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
@@ -892,8 +945,12 @@
     Config->Undefined.push_back(Arg->getValue());
 
   for (auto *Arg : Args.filtered(OPT_version_script))
-    if (Optional<MemoryBufferRef> Buffer = readFile(Arg->getValue()))
-      readVersionScript(*Buffer);
+    if (Optional<std::string> Path = searchScript(Arg->getValue())) {
+      if (Optional<MemoryBufferRef> Buffer = readFile(*Path))
+        readVersionScript(*Buffer);
+    } else {
+      error(Twine("cannot find version script ") + Arg->getValue());
+    }
 }
 
 // Some Config members do not directly correspond to any particular
@@ -904,20 +961,36 @@
   ELFKind Kind = Config->EKind;
   uint16_t Machine = Config->EMachine;
 
-  // There is an ILP32 ABI for x86-64, although it's not very popular.
-  // It is called the x32 ABI.
-  bool IsX32 = (Kind == ELF32LEKind && Machine == EM_X86_64);
-
   Config->CopyRelocs = (Config->Relocatable || Config->EmitRelocs);
   Config->Is64 = (Kind == ELF64LEKind || Kind == ELF64BEKind);
   Config->IsLE = (Kind == ELF32LEKind || Kind == ELF64LEKind);
   Config->Endianness =
       Config->IsLE ? support::endianness::little : support::endianness::big;
   Config->IsMips64EL = (Kind == ELF64LEKind && Machine == EM_MIPS);
-  Config->IsRela =
-      (Config->Is64 || IsX32 || Machine == EM_PPC) && Machine != EM_MIPS;
   Config->Pic = Config->Pie || Config->Shared;
   Config->Wordsize = Config->Is64 ? 8 : 4;
+
+  // There is an ILP32 ABI for x86-64, although it's not very popular.
+  // It is called the x32 ABI.
+  bool IsX32 = (Kind == ELF32LEKind && Machine == EM_X86_64);
+
+  // ELF defines two different ways to store relocation addends as shown below:
+  //
+  //  Rel:  Addends are stored to the location where relocations are applied.
+  //  Rela: Addends are stored as part of relocation entry.
+  //
+  // In other words, Rela makes it easy to read addends at the price of extra
+  // 4 or 8 byte for each relocation entry. We don't know why ELF defined two
+  // different mechanisms in the first place, but this is how the spec is
+  // defined.
+  //
+  // You cannot choose which one, Rel or Rela, you want to use. Instead each
+  // ABI defines which one you need to use. The following expression expresses
+  // that.
+  Config->IsRela =
+      (Config->Is64 || IsX32 || Machine == EM_PPC || Machine == EM_RISCV) &&
+      Machine != EM_MIPS;
+
   // If the output uses REL relocations we must store the dynamic relocation
   // addends to the output sections. We also store addends for RELA relocations
   // if --apply-dynamic-relocs is used.
@@ -929,7 +1002,7 @@
 }
 
 // Returns a value of "-format" option.
-static bool getBinaryOption(StringRef S) {
+static bool isFormatBinary(StringRef S) {
   if (S == "binary")
     return true;
   if (S == "elf" || S == "default")
@@ -960,7 +1033,7 @@
       break;
     }
     case OPT_script:
-      if (Optional<std::string> Path = searchLinkerScript(Arg->getValue())) {
+      if (Optional<std::string> Path = searchScript(Arg->getValue())) {
         if (Optional<MemoryBufferRef> MB = readFile(*Path))
           readLinkerScript(*MB);
         break;
@@ -971,7 +1044,7 @@
       Config->AsNeeded = true;
       break;
     case OPT_format:
-      InBinary = getBinaryOption(Arg->getValue());
+      Config->FormatBinary = isFormatBinary(Arg->getValue());
       break;
     case OPT_no_as_needed:
       Config->AsNeeded = false;
@@ -1113,12 +1186,19 @@
   DenseSet<StringRef> Libs = getExcludeLibs(Args);
   bool All = Libs.count("ALL");
 
-  for (InputFile *File : ObjectFiles)
+  auto Visit = [&](InputFile *File) {
     if (!File->ArchiveName.empty())
       if (All || Libs.count(path::filename(File->ArchiveName)))
         for (Symbol *Sym : File->getSymbols())
           if (!Sym->isLocal() && Sym->File == File)
             Sym->VersionId = VER_NDX_LOCAL;
+  };
+
+  for (InputFile *File : ObjectFiles)
+    Visit(File);
+
+  for (BitcodeFile *File : BitcodeFiles)
+    Visit(File);
 }
 
 // Force Sym to be entered in the output. Used for -u or equivalent.
@@ -1135,6 +1215,21 @@
     Symtab->fetchLazy<ELFT>(Sym);
 }
 
+template <class ELFT> static void handleLibcall(StringRef Name) {
+  Symbol *Sym = Symtab->find(Name);
+  if (!Sym || !Sym->isLazy())
+    return;
+
+  MemoryBufferRef MB;
+  if (auto *LO = dyn_cast<LazyObject>(Sym))
+    MB = LO->File->MB;
+  else
+    MB = cast<LazyArchive>(Sym)->getMemberBuffer();
+
+  if (isBitcode(MB))
+    Symtab->fetchLazy<ELFT>(Sym);
+}
+
 template <class ELFT> static bool shouldDemote(Symbol &Sym) {
   // If all references to a DSO happen to be weak, the DSO is not added to
   // DT_NEEDED. If that happens, we need to eliminate shared symbols created
@@ -1149,6 +1244,12 @@
   return Sym.isLazy() && Sym.IsUsedInRegularObj;
 }
 
+// Some files, such as .so or files between -{start,end}-lib may be removed
+// after their symbols are added to the symbol table. If that happens, we
+// need to remove symbols that refer files that no longer exist, so that
+// they won't appear in the symbol table of the output file.
+//
+// We remove symbols by demoting them to undefined symbol.
 template <class ELFT> static void demoteSymbols() {
   for (Symbol *Sym : Symtab->getSymbols()) {
     if (shouldDemote<ELFT>(*Sym)) {
@@ -1160,18 +1261,76 @@
   }
 }
 
+// The section referred to by S is considered address-significant. Set the
+// KeepUnique flag on the section if appropriate.
+static void markAddrsig(Symbol *S) {
+  if (auto *D = dyn_cast_or_null<Defined>(S))
+    if (D->Section)
+      // We don't need to keep text sections unique under --icf=all even if they
+      // are address-significant.
+      if (Config->ICF == ICFLevel::Safe || !(D->Section->Flags & SHF_EXECINSTR))
+        D->Section->KeepUnique = true;
+}
+
 // Record sections that define symbols mentioned in --keep-unique <symbol>
-// these sections are inelligible for ICF.
+// and symbols referred to by address-significance tables. These sections are
+// ineligible for ICF.
+template <class ELFT>
 static void findKeepUniqueSections(opt::InputArgList &Args) {
   for (auto *Arg : Args.filtered(OPT_keep_unique)) {
     StringRef Name = Arg->getValue();
-    if (auto *Sym = dyn_cast_or_null<Defined>(Symtab->find(Name)))
-      Sym->Section->KeepUnique = true;
-    else
+    auto *D = dyn_cast_or_null<Defined>(Symtab->find(Name));
+    if (!D || !D->Section) {
       warn("could not find symbol " + Name + " to keep unique");
+      continue;
+    }
+    D->Section->KeepUnique = true;
+  }
+
+  // --icf=all --ignore-data-address-equality means that we can ignore
+  // the dynsym and address-significance tables entirely.
+  if (Config->ICF == ICFLevel::All && Config->IgnoreDataAddressEquality)
+    return;
+
+  // Symbols in the dynsym could be address-significant in other executables
+  // or DSOs, so we conservatively mark them as address-significant.
+  for (Symbol *S : Symtab->getSymbols())
+    if (S->includeInDynsym())
+      markAddrsig(S);
+
+  // Visit the address-significance table in each object file and mark each
+  // referenced symbol as address-significant.
+  for (InputFile *F : ObjectFiles) {
+    auto *Obj = cast<ObjFile<ELFT>>(F);
+    ArrayRef<Symbol *> Syms = Obj->getSymbols();
+    if (Obj->AddrsigSec) {
+      ArrayRef<uint8_t> Contents =
+          check(Obj->getObj().getSectionContents(Obj->AddrsigSec));
+      const uint8_t *Cur = Contents.begin();
+      while (Cur != Contents.end()) {
+        unsigned Size;
+        const char *Err;
+        uint64_t SymIndex = decodeULEB128(Cur, &Size, Contents.end(), &Err);
+        if (Err)
+          fatal(toString(F) + ": could not decode addrsig section: " + Err);
+        markAddrsig(Syms[SymIndex]);
+        Cur += Size;
+      }
+    } else {
+      // If an object file does not have an address-significance table,
+      // conservatively mark all of its symbols as address-significant.
+      for (Symbol *S : Syms)
+        markAddrsig(S);
+    }
   }
 }
 
+static const char *LibcallRoutineNames[] = {
+#define HANDLE_LIBCALL(code, name) name,
+#include "llvm/IR/RuntimeLibcalls.def"
+#undef HANDLE_LIBCALL
+};
+
 // Do actual linking. Note that when this function is called,
 // all linker scripts have already been parsed.
 template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
@@ -1238,11 +1397,30 @@
   for (StringRef S : Config->Undefined)
     handleUndefined<ELFT>(S);
 
-  // If an entry symbol is in a static archive, pull out that file now
-  // to complete the symbol table. After this, no new names except a
-  // few linker-synthesized ones will be added to the symbol table.
+  // If an entry symbol is in a static archive, pull out that file now.
   handleUndefined<ELFT>(Config->Entry);
 
+  // If any of our inputs are bitcode files, the LTO code generator may create
+  // references to certain library functions that might not be explicit in the
+  // bitcode file's symbol table. If any of those library functions are defined
+  // in a bitcode file in an archive member, we need to arrange to use LTO to
+  // compile those archive members by adding them to the link beforehand.
+  //
+  // However, adding all libcall symbols to the link can have undesired
+  // consequences. For example, the libgcc implementation of
+  // __sync_val_compare_and_swap_8 on 32-bit ARM pulls in an .init_array entry
+  // that aborts the program if the Linux kernel does not support 64-bit
+  // atomics, which would prevent the program from running even if it does not
+  // use 64-bit atomics.
+  //
+  // Therefore, we only add libcall symbols to the link before LTO if we have
+  // to, i.e. if the symbol's definition is in bitcode. Any other required
+  // libcall symbols will be added to the link after LTO when we add the LTO
+  // object file to the link.
+  if (!BitcodeFiles.empty())
+    for (const char *S : LibcallRoutineNames)
+      handleLibcall<ELFT>(S);
+
   // Return if there were name resolution errors.
   if (errorCount())
     return;
@@ -1283,6 +1461,9 @@
 
   // Do link-time optimization if given files are LLVM bitcode files.
   // This compiles bitcode files into real object files.
+  //
+  // With this the symbol table should be complete. After this, no new names
+  // except a few linker-synthesized ones will be added to the symbol table.
   Symtab->addCombinedLTOObject<ELFT>();
   if (errorCount())
     return;
@@ -1344,8 +1525,8 @@
   markLive<ELFT>();
   demoteSymbols<ELFT>();
   mergeSections();
-  if (Config->ICF) {
-    findKeepUniqueSections(Args);
+  if (Config->ICF != ICFLevel::None) {
+    findKeepUniqueSections<ELFT>(Args);
     doIcf<ELFT>();
   }
 
diff --git a/ELF/Driver.h b/ELF/Driver.h
index 3e4e130..81d7f60 100644
--- a/ELF/Driver.h
+++ b/ELF/Driver.h
@@ -42,9 +42,6 @@
   // True if we are in --start-lib and --end-lib.
   bool InLib = false;
 
-  // True if we are in -format=binary and -format=elf.
-  bool InBinary = false;
-
   std::vector<InputFile *> Files;
 };
 
@@ -67,7 +64,7 @@
 std::string createResponseFile(const llvm::opt::InputArgList &Args);
 
 llvm::Optional<std::string> findFromSearchPaths(StringRef Path);
-llvm::Optional<std::string> searchLinkerScript(StringRef Path);
+llvm::Optional<std::string> searchScript(StringRef Path);
 llvm::Optional<std::string> searchLibrary(StringRef Path);
 
 } // namespace elf
diff --git a/ELF/DriverUtils.cpp b/ELF/DriverUtils.cpp
index 35e3ba1..698e06e 100644
--- a/ELF/DriverUtils.cpp
+++ b/ELF/DriverUtils.cpp
@@ -227,10 +227,10 @@
   return None;
 }
 
-// If a linker script doesn't exist in the current directory, we also look for
-// the script in the '-L' search paths. This matches the behaviour of both '-T'
-// and linker script INPUT() directives in ld.bfd.
-Optional<std::string> elf::searchLinkerScript(StringRef Name) {
+// If a linker/version script doesn't exist in the current directory, we also
+// look for the script in the '-L' search paths. This matches the behaviour of
+// '-T', --version-script=, and linker script INPUT() command in ld.bfd.
+Optional<std::string> elf::searchScript(StringRef Name) {
   if (fs::exists(Name))
     return Name.str();
   return findFromSearchPaths(Name);
diff --git a/ELF/GdbIndex.h b/ELF/GdbIndex.h
index 41ae9d7..eba1ba2 100644
--- a/ELF/GdbIndex.h
+++ b/ELF/GdbIndex.h
@@ -49,7 +49,6 @@
     return LineSection;
   }
   StringRef getFileName() const override { return ""; }
-  StringRef getCUIndexSection() const override { return ""; }
   StringRef getAbbrevSection() const override { return AbbrevSection; }
   StringRef getStringSection() const override { return StrSection; }
   StringRef getGnuPubNamesSection() const override {
diff --git a/ELF/ICF.cpp b/ELF/ICF.cpp
index f708d96..075938b 100644
--- a/ELF/ICF.cpp
+++ b/ELF/ICF.cpp
@@ -80,9 +80,10 @@
 #include "SyntheticSections.h"
 #include "Writer.h"
 #include "lld/Common/Threads.h"
-#include "llvm/ADT/Hashing.h"
+#include "llvm/ADT/StringExtras.h"
 #include "llvm/BinaryFormat/ELF.h"
 #include "llvm/Object/ELF.h"
+#include "llvm/Support/xxhash.h"
 #include <algorithm>
 #include <atomic>
 
@@ -114,9 +115,9 @@
   size_t findBoundary(size_t Begin, size_t End);
 
   void forEachClassRange(size_t Begin, size_t End,
-                         std::function<void(size_t, size_t)> Fn);
+                         llvm::function_ref<void(size_t, size_t)> Fn);
 
-  void forEachClass(std::function<void(size_t, size_t)> Fn);
+  void forEachClass(llvm::function_ref<void(size_t, size_t)> Fn);
 
   std::vector<InputSection *> Sections;
 
@@ -155,12 +156,6 @@
 };
 }
 
-// Returns a hash value for S. Note that the information about
-// relocation targets is not included in the hash value.
-template <class ELFT> static uint32_t getHash(InputSection *S) {
-  return hash_combine(S->Flags, S->getSize(), S->NumRelocations, S->Data);
-}
-
 // Returns true if section S is subject of ICF.
 static bool isEligible(InputSection *S) {
   if (!S->Live || S->KeepUnique || !(S->Flags & SHF_ALLOC))
@@ -172,9 +167,9 @@
       !S->Name.startswith(".data.rel.ro."))
     return false;
 
-  // Don't merge read only data sections unless
-  // --ignore-data-address-equality was passed.
-  if (!(S->Flags & SHF_EXECINSTR) && !Config->IgnoreDataAddressEquality)
+  // SHF_LINK_ORDER sections are ICF'd as a unit with their dependent sections,
+  // so we don't consider them for ICF individually.
+  if (S->Flags & SHF_LINK_ORDER)
     return false;
 
   // Don't merge synthetic sections as their Data member is not valid and empty.
@@ -239,9 +234,6 @@
 template <class RelTy>
 bool ICF<ELFT>::constantEq(const InputSection *SecA, ArrayRef<RelTy> RA,
                            const InputSection *SecB, ArrayRef<RelTy> RB) {
-  if (RA.size() != RB.size())
-    return false;
-
   for (size_t I = 0; I < RA.size(); ++I) {
     if (RA[I].r_offset != RB[I].r_offset ||
         RA[I].getType(Config->IsMips64EL) != RB[I].getType(Config->IsMips64EL))
@@ -385,7 +377,7 @@
 // This function calls Fn on every group within [Begin, End).
 template <class ELFT>
 void ICF<ELFT>::forEachClassRange(size_t Begin, size_t End,
-                                  std::function<void(size_t, size_t)> Fn) {
+                                  llvm::function_ref<void(size_t, size_t)> Fn) {
   while (Begin < End) {
     size_t Mid = findBoundary(Begin, End);
     Fn(Begin, Mid);
@@ -395,7 +387,7 @@
 
 // Call Fn on each equivalence class.
 template <class ELFT>
-void ICF<ELFT>::forEachClass(std::function<void(size_t, size_t)> Fn) {
+void ICF<ELFT>::forEachClass(llvm::function_ref<void(size_t, size_t)> Fn) {
   // If threading is disabled or the number of sections are
   // too small to use threading, call Fn sequentially.
   if (!ThreadsEnabled || Sections.size() < 1024) {
@@ -444,7 +436,7 @@
   // Initially, we use hash values to partition sections.
   parallelForEach(Sections, [&](InputSection *S) {
     // Set MSB to 1 to avoid collisions with non-hash IDs.
-    S->Class[0] = getHash<ELFT>(S) | (1 << 31);
+    S->Class[0] = xxHash64(S->Data) | (1U << 31);
   });
 
   // From now on, sections in Sections vector are ordered so that sections
diff --git a/ELF/InputFiles.cpp b/ELF/InputFiles.cpp
index 8e4fbe3..e4a32be 100644
--- a/ELF/InputFiles.cpp
+++ b/ELF/InputFiles.cpp
@@ -126,11 +126,10 @@
 template <class ELFT> void ObjFile<ELFT>::initializeDwarf() {
   Dwarf = llvm::make_unique<DWARFContext>(make_unique<LLDDwarfObj<ELFT>>(this));
   const DWARFObject &Obj = Dwarf->getDWARFObj();
-  DwarfLine.reset(new DWARFDebugLine);
   DWARFDataExtractor LineData(Obj, Obj.getLineSection(), Config->IsLE,
                               Config->Wordsize);
 
-  for (std::unique_ptr<DWARFCompileUnit> &CU : Dwarf->compile_units()) {
+  for (std::unique_ptr<DWARFUnit> &CU : Dwarf->compile_units()) {
     auto Report = [](Error Err) {
       handleAllErrors(std::move(Err),
                       [](ErrorInfoBase &Info) { warn(Info.message()); });
@@ -167,10 +166,15 @@
       // Get the line number on which the variable is declared.
       unsigned Line = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_line), 0);
 
-      // Get the name of the variable and add the collected information to
-      // VariableLoc. Usually Name is non-empty, but it can be empty if the
+      // Here we want to take the variable name to add it into VariableLoc.
+      // Variable can have regular and linkage name associated. At first, we try
+      // to get linkage name as it can be different, for example when we have
+      // two variables in different namespaces of the same object. Use common
+      // name otherwise, but handle the case when it also absent in case if the
       // input object file lacks some debug info.
-      StringRef Name = dwarf::toString(Die.find(dwarf::DW_AT_name), "");
+      StringRef Name =
+          dwarf::toString(Die.find(dwarf::DW_AT_linkage_name),
+                          dwarf::toString(Die.find(dwarf::DW_AT_name), ""));
       if (!Name.empty())
         VariableLoc.insert({Name, {LT, File, Line}});
     }
@@ -217,14 +221,6 @@
   return None;
 }
 
-// Returns source line information for a given offset using DWARF debug info.
-template <class ELFT>
-std::string ObjFile<ELFT>::getLineInfo(InputSectionBase *S, uint64_t Offset) {
-  if (Optional<DILineInfo> Info = getDILineInfo(S, Offset))
-    return Info->FileName + ":" + std::to_string(Info->Line);
-  return "";
-}
-
 // Returns "<internal>", "foo.a(bar.o)" or "baz.o".
 std::string lld::toString(const InputFile *F) {
   if (!F)
@@ -424,6 +420,17 @@
     // if -r is given, we'll let the final link discard such sections.
     // This is compatible with GNU.
     if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) {
+      if (Sec.sh_type == SHT_LLVM_ADDRSIG) {
+        // We ignore the address-significance table if we know that the object
+        // file was created by objcopy or ld -r. This is because these tools
+        // will reorder the symbols in the symbol table, invalidating the data
+        // in the address-significance table, which refers to symbols by index.
+        if (Sec.sh_link != 0)
+          this->AddrsigSec = &Sec;
+        else if (Config->ICF == ICFLevel::Safe)
+          warn(toString(this) + ": --icf=safe is incompatible with object "
+                                "files created using objcopy or ld -r");
+      }
       this->Sections[I] = &InputSection::Discarded;
       continue;
     }
@@ -487,6 +494,46 @@
   }
 }
 
+// For ARM only, to set the EF_ARM_ABI_FLOAT_SOFT or EF_ARM_ABI_FLOAT_HARD
+// flag in the ELF Header we need to look at Tag_ABI_VFP_args to find out how
+// the input objects have been compiled.
+static void updateARMVFPArgs(const ARMAttributeParser &Attributes,
+                             const InputFile *F) {
+  if (!Attributes.hasAttribute(ARMBuildAttrs::ABI_VFP_args))
+    // If an ABI tag isn't present then it is implicitly given the value of 0
+    // which maps to ARMBuildAttrs::BaseAAPCS. However many assembler files,
+    // including some in glibc that don't use FP args (and should have value 3)
+    // don't have the attribute so we do not consider an implicit value of 0
+    // as a clash.
+    return;
+
+  unsigned VFPArgs = Attributes.getAttributeValue(ARMBuildAttrs::ABI_VFP_args);
+  ARMVFPArgKind Arg;
+  switch (VFPArgs) {
+  case ARMBuildAttrs::BaseAAPCS:
+    Arg = ARMVFPArgKind::Base;
+    break;
+  case ARMBuildAttrs::HardFPAAPCS:
+    Arg = ARMVFPArgKind::VFP;
+    break;
+  case ARMBuildAttrs::ToolChainFPPCS:
+    // Tool chain specific convention that conforms to neither AAPCS variant.
+    Arg = ARMVFPArgKind::ToolChain;
+    break;
+  case ARMBuildAttrs::CompatibleFPAAPCS:
+    // Object compatible with all conventions.
+    return;
+  default:
+    error(toString(F) + ": unknown Tag_ABI_VFP_args value: " + Twine(VFPArgs));
+    return;
+  }
+  // Follow ld.bfd and error if there is a mix of calling conventions.
+  if (Config->ARMVFPArgs != Arg && Config->ARMVFPArgs != ARMVFPArgKind::Default)
+    error(toString(F) + ": incompatible Tag_ABI_VFP_args");
+  else
+    Config->ARMVFPArgs = Arg;
+}
+
 // The ARM support in lld makes some use of instructions that are not available
 // on all ARM architectures. Namely:
 // - Use of BLX instruction for interworking between ARM and Thumb state.
@@ -566,6 +613,8 @@
     ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec));
     Attributes.Parse(Contents, /*isLittle*/ Config->EKind == ELF32LEKind);
     updateSupportedARMFeatures(Attributes);
+    updateARMVFPArgs(Attributes, this);
+
     // FIXME: Retain the first attribute section we see. The eglibc ARM
     // dynamic loaders require the presence of an attribute section for dlopen
     // to work. In a full implementation we would merge all attribute sections.
@@ -649,13 +698,24 @@
   if (Name == ".note.GNU-stack")
     return &InputSection::Discarded;
 
-  // Split stacks is a feature to support a discontiguous stack. At least
-  // as of 2017, it seems that the feature is not being used widely.
-  // Only GNU gold supports that. We don't. For the details about that,
-  // see https://gcc.gnu.org/wiki/SplitStacks
+  // Split stacks is a feature to support a discontiguous stack,
+  // commonly used in the programming language Go. For the details,
+  // see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled
+  // for split stack will include a .note.GNU-split-stack section.
   if (Name == ".note.GNU-split-stack") {
-    error(toString(this) +
-          ": object file compiled with -fsplit-stack is not supported");
+    if (Config->Relocatable) {
+      error("Cannot mix split-stack and non-split-stack in a relocatable link");
+      return &InputSection::Discarded;
+    }
+    this->SplitStack = true;
+    return &InputSection::Discarded;
+  }
+
+  // An object file cmpiled for split stack, but where some of the
+  // functions were compiled with the no_split_stack_attribute will
+  // include a .note.GNU-no-split-stack section.
+  if (Name == ".note.GNU-no-split-stack") {
+    this->SomeNoSplitStack = true;
     return &InputSection::Discarded;
   }
 
@@ -880,8 +940,7 @@
     auto *CurVerdef = reinterpret_cast<const Elf_Verdef *>(Verdef);
     Verdef += CurVerdef->vd_next;
     unsigned VerdefIndex = CurVerdef->vd_ndx;
-    if (Verdefs.size() <= VerdefIndex)
-      Verdefs.resize(VerdefIndex + 1);
+    Verdefs.resize(VerdefIndex + 1);
     Verdefs[VerdefIndex] = CurVerdef;
   }
 
@@ -895,16 +954,12 @@
 template <class ELFT>
 uint32_t SharedFile<ELFT>::getAlignment(ArrayRef<Elf_Shdr> Sections,
                                         const Elf_Sym &Sym) {
-  uint64_t Ret = 1;
+  uint64_t Ret = UINT64_MAX;
   if (Sym.st_value)
     Ret = 1ULL << countTrailingZeros((uint64_t)Sym.st_value);
   if (0 < Sym.st_shndx && Sym.st_shndx < Sections.size())
     Ret = std::min<uint64_t>(Ret, Sections[Sym.st_shndx].sh_addralign);
-
-  if (Ret > UINT32_MAX)
-    error(toString(this) + ": alignment too large: " +
-          CHECK(Sym.getName(this->StringTable), this));
-  return Ret;
+  return (Ret > UINT32_MAX) ? 0 : Ret;
 }
 
 // Fully parse the shared object file. This must be called after parseSoName().
diff --git a/ELF/InputFiles.h b/ELF/InputFiles.h
index 072a5df..0db3203 100644
--- a/ELF/InputFiles.h
+++ b/ELF/InputFiles.h
@@ -120,6 +120,9 @@
   static bool IsInGroup;
   static uint32_t NextGroupId;
 
+  // Index of MIPS GOT built for this file.
+  llvm::Optional<size_t> MipsGotIndex;
+
 protected:
   InputFile(Kind K, MemoryBufferRef M);
   std::vector<InputSectionBase *> Sections;
@@ -191,9 +194,6 @@
     return getSymbol(SymIndex);
   }
 
-  // Returns source line information for a given offset.
-  // If no information is available, returns "".
-  std::string getLineInfo(InputSectionBase *S, uint64_t Offset);
   llvm::Optional<llvm::DILineInfo> getDILineInfo(InputSectionBase *, uint64_t);
   llvm::Optional<std::pair<std::string, unsigned>> getVariableLoc(StringRef Name);
 
@@ -207,6 +207,17 @@
   // symbol table.
   StringRef SourceFile;
 
+  // True if the file defines functions compiled with
+  // -fsplit-stack. Usually false.
+  bool SplitStack = false;
+
+  // True if the file defines functions compiled with -fsplit-stack,
+  // but had one or more functions with the no_split_stack attribute.
+  bool SomeNoSplitStack = false;
+
+  // Pointer to this input file's .llvm_addrsig section, if it has one.
+  const Elf_Shdr *AddrsigSec = nullptr;
+
 private:
   void
   initializeSections(llvm::DenseSet<llvm::CachedHashStringRef> &ComdatGroups);
@@ -229,7 +240,6 @@
   // parse it only once for each object file we link.
   std::unique_ptr<llvm::DWARFContext> Dwarf;
   std::vector<const llvm::DWARFDebugLine::LineTable *> LineTables;
-  std::unique_ptr<llvm::DWARFDebugLine> DwarfLine;
   struct VarLoc {
     const llvm::DWARFDebugLine::LineTable *LT;
     unsigned File;
diff --git a/ELF/InputSection.cpp b/ELF/InputSection.cpp
index 09bc39e..48603a3 100644
--- a/ELF/InputSection.cpp
+++ b/ELF/InputSection.cpp
@@ -14,6 +14,7 @@
 #include "LinkerScript.h"
 #include "OutputSections.h"
 #include "Relocations.h"
+#include "SymbolTable.h"
 #include "Symbols.h"
 #include "SyntheticSections.h"
 #include "Target.h"
@@ -26,7 +27,10 @@
 #include "llvm/Support/Endian.h"
 #include "llvm/Support/Threading.h"
 #include "llvm/Support/xxhash.h"
+#include <algorithm>
 #include <mutex>
+#include <set>
+#include <vector>
 
 using namespace llvm;
 using namespace llvm::ELF;
@@ -212,6 +216,17 @@
   return cast<InputSection>(File->getSections()[Link]);
 }
 
+// Find a function symbol that encloses a given location.
+template <class ELFT>
+Defined *InputSectionBase::getEnclosingFunction(uint64_t Offset) {
+  for (Symbol *B : File->getSymbols())
+    if (Defined *D = dyn_cast<Defined>(B))
+      if (D->Section == this && D->Type == STT_FUNC && D->Value <= Offset &&
+          Offset < D->Value + D->Size)
+        return D;
+  return nullptr;
+}
+
 // Returns a source location string. Used to construct an error message.
 template <class ELFT>
 std::string InputSectionBase::getLocation(uint64_t Offset) {
@@ -221,9 +236,8 @@
         .str();
 
   // First check if we can get desired values from debugging information.
-  std::string LineInfo = getFile<ELFT>()->getLineInfo(this, Offset);
-  if (!LineInfo.empty())
-    return LineInfo;
+  if (Optional<DILineInfo> Info = getFile<ELFT>()->getDILineInfo(this, Offset))
+    return Info->FileName + ":" + std::to_string(Info->Line);
 
   // File->SourceFile contains STT_FILE symbol that contains a
   // source file name. If it's missing, we use an object file name.
@@ -231,12 +245,8 @@
   if (SrcFile.empty())
     SrcFile = toString(File);
 
-  // Find a function symbol that encloses a given location.
-  for (Symbol *B : File->getSymbols())
-    if (auto *D = dyn_cast<Defined>(B))
-      if (D->Section == this && D->Type == STT_FUNC)
-        if (D->Value <= Offset && Offset < D->Value + D->Size)
-          return SrcFile + ":(function " + toString(*D) + ")";
+  if (Defined *D = getEnclosingFunction<ELFT>(Offset))
+    return SrcFile + ":(function " + toString(*D) + ")";
 
   // If there's no symbol, print out the offset in the section.
   return (SrcFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")").str();
@@ -249,9 +259,6 @@
 //
 //  Returns an empty string if there's no way to get line info.
 std::string InputSectionBase::getSrcMsg(const Symbol &Sym, uint64_t Offset) {
-  // Synthetic sections don't have input files.
-  if (!File)
-    return "";
   return File->getSrcMsg(Sym, *this, Offset);
 }
 
@@ -265,9 +272,6 @@
 //
 //   path/to/foo.o:(function bar) in archive path/to/bar.a
 std::string InputSectionBase::getObjMsg(uint64_t Off) {
-  // Synthetic sections don't have input files.
-  if (!File)
-    return ("<internal>:(" + Name + "+0x" + utohexstr(Off) + ")").str();
   std::string Filename = File->getName();
 
   std::string Archive;
@@ -477,12 +481,40 @@
   return OS->PtLoad->FirstSec->Addr;
 }
 
-static uint64_t getRelocTargetVA(RelType Type, int64_t A, uint64_t P,
-                                 const Symbol &Sym, RelExpr Expr) {
+// For R_RISCV_PC_INDIRECT (R_RISCV_PCREL_LO12_{I,S}), the symbol actually
+// points the corresponding R_RISCV_PCREL_HI20 relocation, and the target VA
+// is calculated using PCREL_HI20's symbol.
+//
+// This function returns the R_RISCV_PCREL_HI20 relocation from
+// R_RISCV_PCREL_LO12's symbol and addend.
+Relocation *lld::elf::getRISCVPCRelHi20(const Symbol *Sym, uint64_t Addend) {
+  const Defined *D = cast<Defined>(Sym);
+  InputSection *IS = cast<InputSection>(D->Section);
+
+  if (Addend != 0)
+    warn("Non-zero addend in R_RISCV_PCREL_LO12 relocation to " +
+         IS->getObjMsg(D->Value) + " is ignored");
+
+  // Relocations are sorted by offset, so we can use std::equal_range to do
+  // binary search.
+  auto Range = std::equal_range(IS->Relocations.begin(), IS->Relocations.end(),
+                                D->Value, RelocationOffsetComparator{});
+  for (auto It = std::get<0>(Range); It != std::get<1>(Range); ++It)
+    if (isRelExprOneOf<R_PC>(It->Expr))
+      return &*It;
+
+  error("R_RISCV_PCREL_LO12 relocation points to " + IS->getObjMsg(D->Value) +
+        " without an associated R_RISCV_PCREL_HI20 relocation");
+  return nullptr;
+}
+
+static uint64_t getRelocTargetVA(const InputFile *File, RelType Type, int64_t A,
+                                 uint64_t P, const Symbol &Sym, RelExpr Expr) {
   switch (Expr) {
   case R_INVALID:
     return 0;
   case R_ABS:
+  case R_RELAX_TLS_LD_TO_LE_ABS:
   case R_RELAX_GOT_PC_NOPIC:
     return Sym.getVA(A);
   case R_ADDEND:
@@ -503,7 +535,9 @@
   case R_GOT_FROM_END:
   case R_RELAX_TLS_GD_TO_IE_END:
     return Sym.getGotOffset() + A - InX::Got->getSize();
+  case R_TLSLD_GOT_OFF:
   case R_GOT_OFF:
+  case R_RELAX_TLS_GD_TO_IE_GOT_OFF:
     return Sym.getGotOffset() + A;
   case R_GOT_PAGE_PC:
   case R_RELAX_TLS_GD_TO_IE_PAGE_PC:
@@ -511,14 +545,10 @@
   case R_GOT_PC:
   case R_RELAX_TLS_GD_TO_IE:
     return Sym.getGotVA() + A - P;
-  case R_HINT:
-  case R_NONE:
-  case R_TLSDESC_CALL:
-    llvm_unreachable("cannot relocate hint relocs");
   case R_MIPS_GOTREL:
-    return Sym.getVA(A) - InX::MipsGot->getGp();
+    return Sym.getVA(A) - InX::MipsGot->getGp(File);
   case R_MIPS_GOT_GP:
-    return InX::MipsGot->getGp() + A;
+    return InX::MipsGot->getGp(File) + A;
   case R_MIPS_GOT_GP_PC: {
     // R_MIPS_LO16 expression has R_MIPS_GOT_GP_PC type iif the target
     // is _gp_disp symbol. In that case we should use the following
@@ -527,7 +557,7 @@
     // microMIPS variants of these relocations use slightly different
     // expressions: AHL + GP - P + 3 for %lo() and AHL + GP - P - 1 for %hi()
     // to correctly handle less-sugnificant bit of the microMIPS symbol.
-    uint64_t V = InX::MipsGot->getGp() + A - P;
+    uint64_t V = InX::MipsGot->getGp(File) + A - P;
     if (Type == R_MIPS_LO16 || Type == R_MICROMIPS_LO16)
       V += 4;
     if (Type == R_MICROMIPS_LO16 || Type == R_MICROMIPS_HI16)
@@ -538,21 +568,23 @@
     // If relocation against MIPS local symbol requires GOT entry, this entry
     // should be initialized by 'page address'. This address is high 16-bits
     // of sum the symbol's value and the addend.
-    return InX::MipsGot->getVA() + InX::MipsGot->getPageEntryOffset(Sym, A) -
-           InX::MipsGot->getGp();
+    return InX::MipsGot->getVA() +
+           InX::MipsGot->getPageEntryOffset(File, Sym, A) -
+           InX::MipsGot->getGp(File);
   case R_MIPS_GOT_OFF:
   case R_MIPS_GOT_OFF32:
     // In case of MIPS if a GOT relocation has non-zero addend this addend
     // should be applied to the GOT entry content not to the GOT entry offset.
     // That is why we use separate expression type.
-    return InX::MipsGot->getVA() + InX::MipsGot->getSymEntryOffset(Sym, A) -
-           InX::MipsGot->getGp();
+    return InX::MipsGot->getVA() +
+           InX::MipsGot->getSymEntryOffset(File, Sym, A) -
+           InX::MipsGot->getGp(File);
   case R_MIPS_TLSGD:
-    return InX::MipsGot->getVA() + InX::MipsGot->getTlsOffset() +
-           InX::MipsGot->getGlobalDynOffset(Sym) - InX::MipsGot->getGp();
+    return InX::MipsGot->getVA() + InX::MipsGot->getGlobalDynOffset(File, Sym) -
+           InX::MipsGot->getGp(File);
   case R_MIPS_TLSLD:
-    return InX::MipsGot->getVA() + InX::MipsGot->getTlsOffset() +
-           InX::MipsGot->getTlsIndexOff() - InX::MipsGot->getGp();
+    return InX::MipsGot->getVA() + InX::MipsGot->getTlsIndexOffset(File) -
+           InX::MipsGot->getGp(File);
   case R_PAGE_PC:
   case R_PLT_PAGE_PC: {
     uint64_t Dest;
@@ -562,6 +594,13 @@
       Dest = getAArch64Page(Sym.getVA(A));
     return Dest - getAArch64Page(P);
   }
+  case R_RISCV_PC_INDIRECT: {
+    const Relocation *HiRel = getRISCVPCRelHi20(&Sym, A);
+    if (!HiRel)
+      return 0;
+    return getRelocTargetVA(File, HiRel->Type, HiRel->Addend, Sym.getVA(),
+                            *HiRel->Sym, HiRel->Expr);
+  }
   case R_PC: {
     uint64_t Dest;
     if (Sym.isUndefWeak()) {
@@ -618,8 +657,23 @@
     // statically to zero.
     if (Sym.isTls() && Sym.isUndefWeak())
       return 0;
-    if (Target->TcbSize)
+
+    // For TLS variant 1 the TCB is a fixed size, whereas for TLS variant 2 the
+    // TCB is on unspecified size and content. Targets that implement variant 1
+    // should set TcbSize.
+    if (Target->TcbSize) {
+      // PPC64 V2 ABI has the thread pointer offset into the middle of the TLS
+      // storage area by TlsTpOffset for efficient addressing TCB and up to
+      // 4KB – 8 B of other thread library information (placed before the TCB).
+      // Subtracting this offset will get the address of the first TLS block.
+      if (Target->TlsTpOffset)
+        return Sym.getVA(A) - Target->TlsTpOffset;
+
+      // If thread pointer is not offset into the middle, the first thing in the
+      // TLS storage area is the TCB. Add the TcbSize to get the address of the
+      // first TLS block.
       return Sym.getVA(A) + alignTo(Target->TcbSize, Out::TlsPhdr->p_align);
+    }
     return Sym.getVA(A) - Out::TlsPhdr->p_memsz;
   case R_RELAX_TLS_GD_TO_LE_NEG:
   case R_NEG_TLS:
@@ -640,11 +694,12 @@
   case R_TLSLD_GOT_FROM_END:
     return InX::Got->getTlsIndexOff() + A - InX::Got->getSize();
   case R_TLSLD_GOT:
-      return InX::Got->getTlsIndexOff() + A;
+    return InX::Got->getTlsIndexOff() + A;
   case R_TLSLD_PC:
     return InX::Got->getTlsIndexVA() + A - P;
+  default:
+    llvm_unreachable("invalid expression");
   }
-  llvm_unreachable("Invalid expression");
 }
 
 // This function applies relocations to sections without SHF_ALLOC bit.
@@ -708,15 +763,37 @@
   }
 }
 
+// This is used when '-r' is given.
+// For REL targets, InputSection::copyRelocations() may store artificial
+// relocations aimed to update addends. They are handled in relocateAlloc()
+// for allocatable sections, and this function does the same for
+// non-allocatable sections, such as sections with debug information.
+static void relocateNonAllocForRelocatable(InputSection *Sec, uint8_t *Buf) {
+  const unsigned Bits = Config->Is64 ? 64 : 32;
+
+  for (const Relocation &Rel : Sec->Relocations) {
+    // InputSection::copyRelocations() adds only R_ABS relocations.
+    assert(Rel.Expr == R_ABS);
+    uint8_t *BufLoc = Buf + Rel.Offset + Sec->OutSecOff;
+    uint64_t TargetVA = SignExtend64(Rel.Sym->getVA(Rel.Addend), Bits);
+    Target->relocateOne(BufLoc, Rel.Type, TargetVA);
+  }
+}
+
 template <class ELFT>
 void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) {
+  if (Flags & SHF_EXECINSTR)
+    adjustSplitStackFunctionPrologues<ELFT>(Buf, BufEnd);
+
   if (Flags & SHF_ALLOC) {
     relocateAlloc(Buf, BufEnd);
     return;
   }
 
   auto *Sec = cast<InputSection>(this);
-  if (Sec->AreRelocsRela)
+  if (Config->Relocatable)
+    relocateNonAllocForRelocatable(Sec, Buf);
+  else if (Sec->AreRelocsRela)
     Sec->relocateNonAlloc<ELFT>(Buf, Sec->template relas<ELFT>());
   else
     Sec->relocateNonAlloc<ELFT>(Buf, Sec->template rels<ELFT>());
@@ -736,7 +813,8 @@
     uint64_t AddrLoc = getOutputSection()->Addr + Offset;
     RelExpr Expr = Rel.Expr;
     uint64_t TargetVA = SignExtend64(
-        getRelocTargetVA(Type, Rel.Addend, AddrLoc, *Rel.Sym, Expr), Bits);
+        getRelocTargetVA(File, Type, Rel.Addend, AddrLoc, *Rel.Sym, Expr),
+        Bits);
 
     switch (Expr) {
     case R_RELAX_GOT_PC:
@@ -747,6 +825,7 @@
       Target->relaxTlsIeToLe(BufLoc, Type, TargetVA);
       break;
     case R_RELAX_TLS_LD_TO_LE:
+    case R_RELAX_TLS_LD_TO_LE_ABS:
       Target->relaxTlsLdToLe(BufLoc, Type, TargetVA);
       break;
     case R_RELAX_TLS_GD_TO_LE:
@@ -755,11 +834,18 @@
       break;
     case R_RELAX_TLS_GD_TO_IE:
     case R_RELAX_TLS_GD_TO_IE_ABS:
+    case R_RELAX_TLS_GD_TO_IE_GOT_OFF:
     case R_RELAX_TLS_GD_TO_IE_PAGE_PC:
     case R_RELAX_TLS_GD_TO_IE_END:
       Target->relaxTlsGdToIe(BufLoc, Type, TargetVA);
       break;
     case R_PPC_CALL:
+      // If this is a call to __tls_get_addr, it may be part of a TLS
+      // sequence that has been relaxed and turned into a nop. In this
+      // case, we don't want to handle it as a call.
+      if (read32(BufLoc) == 0x60000000) // nop
+        break;
+
       // Patch a nop (0x60000000) to a ld.
       if (Rel.Sym->NeedsTocRestore) {
         if (BufLoc + 8 > BufEnd || read32(BufLoc + 4) != 0x60000000) {
@@ -777,6 +863,106 @@
   }
 }
 
+// For each function-defining prologue, find any calls to __morestack,
+// and replace them with calls to __morestack_non_split.
+static void switchMorestackCallsToMorestackNonSplit(
+    DenseSet<Defined *> &Prologues, std::vector<Relocation *> &MorestackCalls) {
+
+  // If the target adjusted a function's prologue, all calls to
+  // __morestack inside that function should be switched to
+  // __morestack_non_split.
+  Symbol *MoreStackNonSplit = Symtab->find("__morestack_non_split");
+  if (!MoreStackNonSplit) {
+    error("Mixing split-stack objects requires a definition of "
+          "__morestack_non_split");
+    return;
+  }
+
+  // Sort both collections to compare addresses efficiently.
+  llvm::sort(MorestackCalls.begin(), MorestackCalls.end(),
+             [](const Relocation *L, const Relocation *R) {
+               return L->Offset < R->Offset;
+             });
+  std::vector<Defined *> Functions(Prologues.begin(), Prologues.end());
+  llvm::sort(
+      Functions.begin(), Functions.end(),
+      [](const Defined *L, const Defined *R) { return L->Value < R->Value; });
+
+  auto It = MorestackCalls.begin();
+  for (Defined *F : Functions) {
+    // Find the first call to __morestack within the function.
+    while (It != MorestackCalls.end() && (*It)->Offset < F->Value)
+      ++It;
+    // Adjust all calls inside the function.
+    while (It != MorestackCalls.end() && (*It)->Offset < F->Value + F->Size) {
+      (*It)->Sym = MoreStackNonSplit;
+      ++It;
+    }
+  }
+}
+
+static bool enclosingPrologueAttempted(uint64_t Offset,
+                                       const DenseSet<Defined *> &Prologues) {
+  for (Defined *F : Prologues)
+    if (F->Value <= Offset && Offset < F->Value + F->Size)
+      return true;
+  return false;
+}
+
+// If a function compiled for split stack calls a function not
+// compiled for split stack, then the caller needs its prologue
+// adjusted to ensure that the called function will have enough stack
+// available. Find those functions, and adjust their prologues.
+template <class ELFT>
+void InputSectionBase::adjustSplitStackFunctionPrologues(uint8_t *Buf,
+                                                         uint8_t *End) {
+  if (!getFile<ELFT>()->SplitStack)
+    return;
+  DenseSet<Defined *> Prologues;
+  std::vector<Relocation *> MorestackCalls;
+
+  for (Relocation &Rel : Relocations) {
+    // Local symbols can't possibly be cross-calls, and should have been
+    // resolved long before this line.
+    if (Rel.Sym->isLocal())
+      continue;
+
+    // Ignore calls into the split-stack api.
+    Defined *D = cast<Defined>(Rel.Sym);
+    if (D->getName().startswith("__morestack")) {
+      if (D->getName().equals("__morestack"))
+        MorestackCalls.push_back(&Rel);
+      continue;
+    }
+
+    // A relocation to non-function isn't relevant. Sometimes
+    // __morestack is not marked as a function, so this check comes
+    // after the name check.
+    if (D->Type != STT_FUNC)
+      continue;
+
+    // If the callee's-file was compiled with split stack, nothing to do.
+    auto *IS = cast_or_null<InputSection>(D->Section);
+    if (!IS || IS->getFile<ELFT>()->SplitStack)
+      continue;
+
+    if (enclosingPrologueAttempted(Rel.Offset, Prologues))
+      continue;
+
+    if (Defined *F = getEnclosingFunction<ELFT>(Rel.Offset)) {
+      Prologues.insert(F);
+      if (Target->adjustPrologueForCrossSplitStack(Buf + getOffset(F->Value),
+                                                   End))
+        continue;
+      if (!getFile<ELFT>()->SomeNoSplitStack)
+        error(lld::toString(this) + ": " + F->getName() +
+              " (with -fsplit-stack) calls " + D->getName() +
+              " (without -fsplit-stack), but couldn't adjust its prologue");
+    }
+  }
+  switchMorestackCallsToMorestackNonSplit(Prologues, MorestackCalls);
+}
+
 template <class ELFT> void InputSection::writeTo(uint8_t *Buf) {
   if (Type == SHT_NOBITS)
     return;
@@ -912,8 +1098,7 @@
   bool IsAlloc = Flags & SHF_ALLOC;
 
   for (size_t I = 0; I != Size; I += EntSize)
-    Pieces.emplace_back(I, xxHash64(toStringRef(Data.slice(I, EntSize))),
-                        !IsAlloc);
+    Pieces.emplace_back(I, xxHash64(Data.slice(I, EntSize)), !IsAlloc);
 }
 
 template <class ELFT>
diff --git a/ELF/InputSection.h b/ELF/InputSection.h
index 7a83bf9..c68b7d1 100644
--- a/ELF/InputSection.h
+++ b/ELF/InputSection.h
@@ -106,7 +106,7 @@
 
   static bool classof(const SectionBase *S) { return S->kind() != Output; }
 
-  // The file which contains this section. It's dynamic type is always
+  // The file which contains this section. Its dynamic type is always
   // ObjFile<ELFT>, but in order to avoid ELFT, we use InputFile as
   // its static type.
   InputFile *File;
@@ -164,6 +164,11 @@
 
   InputSection *getLinkOrderDep() const;
 
+  // Get the function symbol that encloses this offset from within the
+  // section.
+  template <class ELFT>
+  Defined *getEnclosingFunction(uint64_t Offset);
+
   // Compilers emit zlib-compressed debug sections if the -gz option
   // is given. This function checks if this section is compressed, and
   // if so, decompress in memory.
@@ -185,6 +190,15 @@
   // This vector contains such "cooked" relocations.
   std::vector<Relocation> Relocations;
 
+  // A function compiled with -fsplit-stack calling a function
+  // compiled without -fsplit-stack needs its prologue adjusted. Find
+  // such functions and adjust their prologues.  This is very similar
+  // to relocation. See https://gcc.gnu.org/wiki/SplitStacks for more
+  // information.
+  template <typename ELFT>
+  void adjustSplitStackFunctionPrologues(uint8_t *Buf, uint8_t *End);
+
+
   template <typename T> llvm::ArrayRef<T> getDataAs() const {
     size_t S = Data.size();
     assert(S % sizeof(T) == 0);
@@ -339,6 +353,8 @@
 
 // The list of all input sections.
 extern std::vector<InputSectionBase *> InputSections;
+
+Relocation *getRISCVPCRelHi20(const Symbol *Sym, const uint64_t Addend);
 } // namespace elf
 
 std::string toString(const elf::InputSectionBase *);
diff --git a/ELF/LTO.cpp b/ELF/LTO.cpp
index ee55e7c..cb5bb64 100644
--- a/ELF/LTO.cpp
+++ b/ELF/LTO.cpp
@@ -67,9 +67,10 @@
 static lto::Config createConfig() {
   lto::Config C;
 
-  // LLD supports the new relocations.
+  // LLD supports the new relocations and address-significance tables.
   C.Options = InitTargetOptionsFromCodeGenFlags();
   C.Options.RelaxELFRelocations = true;
+  C.Options.EmitAddrsig = true;
 
   // Always emit a section per function/datum with LTO.
   C.Options.FunctionSections = true;
@@ -99,6 +100,7 @@
   C.SampleProfile = Config->LTOSampleProfile;
   C.UseNewPM = Config->LTONewPassManager;
   C.DebugPassManager = Config->LTODebugPassManager;
+  C.DwoDir = Config->DwoDir;
 
   if (Config->SaveTemps)
     checkError(C.addSaveTemps(Config->OutputFile.str() + ".",
@@ -201,14 +203,11 @@
 
 static void createEmptyIndex(StringRef ModulePath) {
   std::string Path = replaceThinLTOSuffix(getThinLTOOutputFile(ModulePath));
-  if (Path.empty())
-    return;
-
   std::unique_ptr<raw_fd_ostream> OS = openFile(Path + ".thinlto.bc");
   if (!OS)
     return;
 
-  ModuleSummaryIndex M(false);
+  ModuleSummaryIndex M(/*HaveGVs*/ false);
   M.setSkipModuleByDistributedBackend();
   WriteIndexToFile(M, *OS);
 
diff --git a/ELF/LinkerScript.cpp b/ELF/LinkerScript.cpp
index 9cc836b..c865e9b 100644
--- a/ELF/LinkerScript.cpp
+++ b/ELF/LinkerScript.cpp
@@ -116,7 +116,8 @@
   if (Ctx->MemRegion)
     expandMemoryRegion(Ctx->MemRegion, Size, Ctx->MemRegion->Name,
                        Ctx->OutSec->Name);
-  if (Ctx->LMARegion)
+  // Only expand the LMARegion if it is different from MemRegion.
+  if (Ctx->LMARegion && Ctx->MemRegion != Ctx->LMARegion)
     expandMemoryRegion(Ctx->LMARegion, Size, Ctx->LMARegion->Name,
                        Ctx->OutSec->Name);
 }
@@ -135,6 +136,9 @@
   // Update to location counter means update to section size.
   if (InSec)
     expandOutputSection(Val - Dot);
+  else
+    expandMemoryRegions(Val - Dot);
+
   Dot = Val;
 }
 
@@ -245,13 +249,12 @@
       declareSymbol(Cmd);
       continue;
     }
-    auto *Sec = dyn_cast<OutputSection>(Base);
-    if (!Sec)
-      continue;
+
     // If the output section directive has constraints,
     // we can't say for sure if it is going to be included or not.
     // Skip such sections for now. Improve the checks if we ever
     // need symbols from that sections to be declared early.
+    auto *Sec = cast<OutputSection>(Base);
     if (Sec->Constraint != ConstraintKind::NoConstraint)
       continue;
     for (BaseCommand *Base2 : Sec->SectionCommands)
@@ -412,7 +415,8 @@
 void LinkerScript::discard(ArrayRef<InputSection *> V) {
   for (InputSection *S : V) {
     if (S == InX::ShStrTab || S == InX::Dynamic || S == InX::DynSymTab ||
-        S == InX::DynStrTab || S == InX::RelaPlt || S == InX::RelaDyn)
+        S == InX::DynStrTab || S == InX::RelaPlt || S == InX::RelaDyn ||
+        S == InX::RelrDyn)
       error("discarding " + S->Name + " section is not allowed");
 
     // You can discard .hash and .gnu.hash sections by linker scripts. Since
@@ -697,6 +701,7 @@
 }
 
 void LinkerScript::output(InputSection *S) {
+  assert(Ctx->OutSec == S->getParent());
   uint64_t Before = advance(0, 1);
   uint64_t Pos = advance(S->getSize(), S->Alignment);
   S->OutSecOff = Pos - S->getSize() - Ctx->OutSec->Addr;
@@ -708,8 +713,6 @@
 }
 
 void LinkerScript::switchTo(OutputSection *Sec) {
-  if (Ctx->OutSec == Sec)
-    return;
   Ctx->OutSec = Sec;
 
   uint64_t Before = advance(0, 1);
@@ -749,6 +752,13 @@
   return nullptr;
 }
 
+static OutputSection *findFirstSection(PhdrEntry *Load) {
+  for (OutputSection *Sec : OutputSections)
+    if (Sec->PtLoad == Load)
+      return Sec;
+  return nullptr;
+}
+
 // This function assigns offsets to input sections and an output section
 // for a single sections command (e.g. ".text { *(.text); }").
 void LinkerScript::assignOffsets(OutputSection *Sec) {
@@ -774,8 +784,11 @@
   // will set the LMA such that the difference between VMA and LMA for the
   // section is the same as the preceding output section in the same region
   // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
+  // This, however, should only be done by the first "non-header" section
+  // in the segment.
   if (PhdrEntry *L = Ctx->OutSec->PtLoad)
-    L->LMAOffset = Ctx->LMAOffset;
+    if (Sec == findFirstSection(L))
+      L->LMAOffset = Ctx->LMAOffset;
 
   // We can call this method multiple times during the creation of
   // thunks and want to start over calculation each time.
@@ -804,21 +817,8 @@
     // Handle a single input section description command.
     // It calculates and assigns the offsets for each section and also
     // updates the output section size.
-    auto *Cmd = cast<InputSectionDescription>(Base);
-    for (InputSection *Sec : Cmd->Sections) {
-      // We tentatively added all synthetic sections at the beginning and
-      // removed empty ones afterwards (because there is no way to know
-      // whether they were going be empty or not other than actually running
-      // linker scripts.) We need to ignore remains of empty sections.
-      if (auto *S = dyn_cast<SyntheticSection>(Sec))
-        if (S->empty())
-          continue;
-
-      if (!Sec->Live)
-        continue;
-      assert(Ctx->OutSec == Sec->getParent());
+    for (InputSection *Sec : cast<InputSectionDescription>(Base)->Sections)
       output(Sec);
-    }
   }
 }
 
@@ -834,9 +834,16 @@
   if (Sec.ExpressionsUseSymbols)
     return false;
 
-  for (BaseCommand *Base : Sec.SectionCommands)
+  for (BaseCommand *Base : Sec.SectionCommands) {
+    if (auto Cmd = dyn_cast<SymbolAssignment>(Base))
+      // Don't create empty output sections just for unreferenced PROVIDE
+      // symbols.
+      if (Cmd->Name != "." && !Cmd->Sym)
+        continue;
+
     if (!isa<InputSectionDescription>(*Base))
       return false;
+  }
   return true;
 }
 
@@ -945,13 +952,6 @@
   }
 }
 
-static OutputSection *findFirstSection(PhdrEntry *Load) {
-  for (OutputSection *Sec : OutputSections)
-    if (Sec->PtLoad == Load)
-      return Sec;
-  return nullptr;
-}
-
 static uint64_t computeBase(uint64_t Min, bool AllocateHeaders) {
   // If there is no SECTIONS or if the linkerscript is explicit about program
   // headers, do our best to allocate them.
diff --git a/ELF/MapFile.cpp b/ELF/MapFile.cpp
index c5984c9..54fddfb 100644
--- a/ELF/MapFile.cpp
+++ b/ELF/MapFile.cpp
@@ -173,10 +173,7 @@
       continue;
     }
 
-    auto *OSec = dyn_cast<OutputSection>(Base);
-    if (!OSec)
-      continue;
-
+    auto *OSec = cast<OutputSection>(Base);
     writeHeader(OS, OSec->Addr, OSec->getLMA(), OSec->Size, OSec->Alignment);
     OS << OSec->Name << '\n';
 
diff --git a/ELF/MarkLive.cpp b/ELF/MarkLive.cpp
index bacbf0e..a8371e2 100644
--- a/ELF/MarkLive.cpp
+++ b/ELF/MarkLive.cpp
@@ -60,8 +60,9 @@
 static DenseMap<StringRef, std::vector<InputSectionBase *>> CNamedSections;
 
 template <class ELFT, class RelT>
-static void resolveReloc(InputSectionBase &Sec, RelT &Rel,
-                         std::function<void(InputSectionBase *, uint64_t)> Fn) {
+static void
+resolveReloc(InputSectionBase &Sec, RelT &Rel,
+             llvm::function_ref<void(InputSectionBase *, uint64_t)> Fn) {
   Symbol &B = Sec.getFile<ELFT>()->getRelocTargetSym(Rel);
 
   // If a symbol is referenced in a live section, it is used.
@@ -90,7 +91,7 @@
 template <class ELFT>
 static void
 forEachSuccessor(InputSection &Sec,
-                 std::function<void(InputSectionBase *, uint64_t)> Fn) {
+                 llvm::function_ref<void(InputSectionBase *, uint64_t)> Fn) {
   if (Sec.AreRelocsRela) {
     for (const typename ELFT::Rela &Rel : Sec.template relas<ELFT>())
       resolveReloc<ELFT>(Sec, Rel, Fn);
@@ -120,7 +121,7 @@
 template <class ELFT, class RelTy>
 static void
 scanEhFrameSection(EhInputSection &EH, ArrayRef<RelTy> Rels,
-                   std::function<void(InputSectionBase *, uint64_t)> Fn) {
+                   llvm::function_ref<void(InputSectionBase *, uint64_t)> Fn) {
   const endianness E = ELFT::TargetEndianness;
 
   for (unsigned I = 0, N = EH.Pieces.size(); I < N; ++I) {
@@ -155,7 +156,7 @@
 template <class ELFT>
 static void
 scanEhFrameSection(EhInputSection &EH,
-                   std::function<void(InputSectionBase *, uint64_t)> Fn) {
+                   llvm::function_ref<void(InputSectionBase *, uint64_t)> Fn) {
   if (!EH.NumRelocations)
     return;
 
diff --git a/ELF/Options.td b/ELF/Options.td
index 256dc5a..04a4f8f 100644
--- a/ELF/Options.td
+++ b/ELF/Options.td
@@ -58,8 +58,8 @@
     "Do not allow multiple definitions (default)">;
 
 defm apply_dynamic_relocs: B<"apply-dynamic-relocs",
-    "Apply dynamic relocations to place",
-    "Do not apply dynamic relocations to place">;
+    "Apply link-time values for dynamic relocations",
+    "Do not apply link-time values for dynamic relocations (default)">;
 
 defm as_needed: B<"as-needed",
     "Only set DT_NEEDED for shared libraries if used",
@@ -131,6 +131,10 @@
 
 defm exclude_libs: Eq<"exclude-libs", "Exclude static libraries from automatic export">;
 
+defm execute_only: B<"execute-only",
+    "Do not mark executable sections readable",
+    "Mark executable sections readable (default)">;
+
 defm export_dynamic: B<"export-dynamic",
     "Put symbols in the dynamic symbol table",
     "Do not put symbols in the dynamic symbol table (default)">;
@@ -170,6 +174,8 @@
 
 def icf_all: F<"icf=all">, HelpText<"Enable identical code folding">;
 
+def icf_safe: F<"icf=safe">, HelpText<"Enable safe identical code folding">;
+
 def icf_none: F<"icf=none">, HelpText<"Disable identical code folding (default)">;
 
 def ignore_function_address_equality: F<"ignore-function-address-equality">,
@@ -233,7 +239,11 @@
 
 defm pack_dyn_relocs:
   Eq<"pack-dyn-relocs", "Pack dynamic relocations in the given format">,
-  MetaVarName<"[none,android]">;
+  MetaVarName<"[none,android,relr,android+relr]">;
+
+defm use_android_relr_tags: B<"use-android-relr-tags",
+    "Use SHT_ANDROID_RELR / DT_ANDROID_RELR* tags instead of SHT_RELR / DT_RELR*",
+    "Use SHT_RELR / DT_RELR* tags (default)">;
 
 defm pie: B<"pie",
     "Create a position independent executable",
@@ -428,6 +438,8 @@
 def: F<"plugin-opt=debug-pass-manager">,
   Alias<lto_debug_pass_manager>, HelpText<"Alias for -lto-debug-pass-manager">;
 def: F<"plugin-opt=disable-verify">, Alias<disable_verify>, HelpText<"Alias for -disable-verify">;
+def plugin_opt_dwo_dir_eq: J<"plugin-opt=dwo_dir=">,
+  HelpText<"Directory to store .dwo files when LTO and debug fission are used">;
 def: J<"plugin-opt=jobs=">, Alias<thinlto_jobs>, HelpText<"Alias for -thinlto-jobs">;
 def: J<"plugin-opt=lto-partitions=">, Alias<lto_partitions>, HelpText<"Alias for -lto-partitions">;
 def plugin_opt_mcpu_eq: J<"plugin-opt=mcpu=">;
@@ -480,3 +492,8 @@
 def: F<"EL">;
 def: JoinedOrSeparate<["-"], "G">;
 def: F<"Qy">;
+
+// Hidden option used for testing MIPS multi-GOT implementation.
+defm mips_got_size:
+  Eq<"mips-got-size", "Max size of a single MIPS GOT. 0x10000 by default.">,
+  Flags<[HelpHidden]>;
diff --git a/ELF/OutputSections.cpp b/ELF/OutputSections.cpp
index 31e1d19..8253b18 100644
--- a/ELF/OutputSections.cpp
+++ b/ELF/OutputSections.cpp
@@ -139,7 +139,7 @@
 }
 
 static void sortByOrder(MutableArrayRef<InputSection *> In,
-                        std::function<int(InputSectionBase *S)> Order) {
+                        llvm::function_ref<int(InputSectionBase *S)> Order) {
   typedef std::pair<int, InputSection *> Pair;
   auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
 
@@ -162,7 +162,7 @@
   return C->Kind == OutputSectionKind;
 }
 
-void OutputSection::sort(std::function<int(InputSectionBase *S)> Order) {
+void OutputSection::sort(llvm::function_ref<int(InputSectionBase *S)> Order) {
   assert(Live);
   for (BaseCommand *B : SectionCommands)
     if (auto *ISD = dyn_cast<InputSectionDescription>(B))
diff --git a/ELF/OutputSections.h b/ELF/OutputSections.h
index 9720aab..efb6aab 100644
--- a/ELF/OutputSections.h
+++ b/ELF/OutputSections.h
@@ -102,12 +102,13 @@
   bool NonAlloc = false;
   bool Noload = false;
   bool ExpressionsUseSymbols = false;
+  bool InOverlay = false;
 
   template <class ELFT> void finalize();
   template <class ELFT> void writeTo(uint8_t *Buf);
   template <class ELFT> void maybeCompress();
 
-  void sort(std::function<int(InputSectionBase *S)> Order);
+  void sort(llvm::function_ref<int(InputSectionBase *S)> Order);
   void sortInitFini();
   void sortCtorsDtors();
 
diff --git a/ELF/Relocations.cpp b/ELF/Relocations.cpp
index 5c4a797..113f871 100644
--- a/ELF/Relocations.cpp
+++ b/ELF/Relocations.cpp
@@ -52,6 +52,7 @@
 #include "Thunks.h"
 #include "lld/Common/Memory.h"
 #include "lld/Common/Strings.h"
+#include "llvm/ADT/SmallSet.h"
 #include "llvm/Support/Endian.h"
 #include "llvm/Support/raw_ostream.h"
 #include <algorithm>
@@ -85,27 +86,16 @@
 // pollute other `handleTlsRelocation` by MIPS `ifs` statements.
 // Mips has a custom MipsGotSection that handles the writing of GOT entries
 // without dynamic relocations.
-template <class ELFT>
 static unsigned handleMipsTlsRelocation(RelType Type, Symbol &Sym,
                                         InputSectionBase &C, uint64_t Offset,
                                         int64_t Addend, RelExpr Expr) {
   if (Expr == R_MIPS_TLSLD) {
-    if (InX::MipsGot->addTlsIndex() && Config->Pic)
-      InX::RelaDyn->addReloc(Target->TlsModuleIndexRel, InX::MipsGot,
-                             InX::MipsGot->getTlsIndexOff(), nullptr);
+    InX::MipsGot->addTlsIndex(*C.File);
     C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
     return 1;
   }
-
   if (Expr == R_MIPS_TLSGD) {
-    if (InX::MipsGot->addDynTlsEntry(Sym) && Sym.IsPreemptible) {
-      uint64_t Off = InX::MipsGot->getGlobalDynOffset(Sym);
-      InX::RelaDyn->addReloc(Target->TlsModuleIndexRel, InX::MipsGot, Off,
-                             &Sym);
-      if (Sym.IsPreemptible)
-        InX::RelaDyn->addReloc(Target->TlsOffsetRel, InX::MipsGot,
-                               Off + Config->Wordsize, &Sym);
-    }
+    InX::MipsGot->addDynTlsEntry(*C.File, Sym);
     C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
     return 1;
   }
@@ -184,7 +174,7 @@
   if (Config->EMachine == EM_ARM)
     return handleARMTlsRelocation<ELFT>(Type, Sym, C, Offset, Addend, Expr);
   if (Config->EMachine == EM_MIPS)
-    return handleMipsTlsRelocation<ELFT>(Type, Sym, C, Offset, Addend, Expr);
+    return handleMipsTlsRelocation(Type, Sym, C, Offset, Addend, Expr);
 
   if (isRelExprOneOf<R_TLSDESC, R_TLSDESC_PAGE, R_TLSDESC_CALL>(Expr) &&
       Config->Shared) {
@@ -198,13 +188,17 @@
     return 1;
   }
 
-  if (isRelExprOneOf<R_TLSLD_GOT, R_TLSLD_GOT_FROM_END, R_TLSLD_PC>(Expr)) {
+  if (isRelExprOneOf<R_TLSLD_GOT, R_TLSLD_GOT_FROM_END, R_TLSLD_PC,
+                     R_TLSLD_HINT>(Expr)) {
     // Local-Dynamic relocs can be relaxed to Local-Exec.
     if (!Config->Shared) {
       C.Relocations.push_back(
-          {R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Sym});
-      return 2;
+          {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
+           Offset, Addend, &Sym});
+      return Target->TlsGdRelaxSkip;
     }
+    if (Expr == R_TLSLD_HINT)
+      return 1;
     if (InX::Got->addTlsIndex())
       InX::RelaDyn->addReloc(Target->TlsModuleIndexRel, InX::Got,
                              InX::Got->getTlsIndexOff(), nullptr);
@@ -213,9 +207,27 @@
   }
 
   // Local-Dynamic relocs can be relaxed to Local-Exec.
-  if (isRelExprOneOf<R_ABS, R_TLSLD_GOT_FROM_END, R_TLSLD_PC>(Expr) &&
-      !Config->Shared) {
-    C.Relocations.push_back({R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Sym});
+  if (Expr == R_ABS && !Config->Shared) {
+    C.Relocations.push_back(
+        {Target->adjustRelaxExpr(Type, nullptr, R_RELAX_TLS_LD_TO_LE), Type,
+         Offset, Addend, &Sym});
+    return 1;
+  }
+
+  // Local-Dynamic sequence where offset of tls variable relative to dynamic
+  // thread pointer is stored in the got.
+  if (Expr == R_TLSLD_GOT_OFF) {
+    // Local-Dynamic relocs can be relaxed to local-exec
+    if (!Config->Shared) {
+      C.Relocations.push_back({R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Sym});
+      return 1;
+    }
+    if (!Sym.isInGot()) {
+      InX::Got->addEntry(Sym);
+      uint64_t Off = Sym.getGotOffset();
+      InX::Got->Relocations.push_back({R_ABS, Target->TlsOffsetRel, Off, 0, &Sym});
+    }
+    C.Relocations.push_back({Expr, Type, Offset, Addend, &Sym});
     return 1;
   }
 
@@ -345,12 +357,13 @@
 static bool isStaticLinkTimeConstant(RelExpr E, RelType Type, const Symbol &Sym,
                                      InputSectionBase &S, uint64_t RelOff) {
   // These expressions always compute a constant
-  if (isRelExprOneOf<R_GOT_FROM_END, R_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE,
-                     R_MIPS_GOTREL, R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32,
-                     R_MIPS_GOT_GP_PC, R_MIPS_TLSGD, R_GOT_PAGE_PC, R_GOT_PC,
-                     R_GOTONLY_PC, R_GOTONLY_PC_FROM_END, R_PLT_PC, R_TLSGD_GOT,
-                     R_TLSGD_GOT_FROM_END, R_TLSGD_PC, R_PPC_CALL_PLT,
-                     R_TLSDESC_CALL, R_TLSDESC_PAGE, R_HINT>(E))
+  if (isRelExprOneOf<
+          R_GOT_FROM_END, R_GOT_OFF, R_TLSLD_GOT_OFF, R_MIPS_GOT_LOCAL_PAGE,
+          R_MIPS_GOTREL, R_MIPS_GOT_OFF, R_MIPS_GOT_OFF32, R_MIPS_GOT_GP_PC,
+          R_MIPS_TLSGD, R_GOT_PAGE_PC, R_GOT_PC, R_GOTONLY_PC,
+          R_GOTONLY_PC_FROM_END, R_PLT_PC, R_TLSGD_GOT, R_TLSGD_GOT_FROM_END,
+          R_TLSGD_PC, R_PPC_CALL_PLT, R_TLSDESC_CALL, R_TLSDESC_PAGE, R_HINT,
+          R_TLSLD_HINT>(E))
     return true;
 
   // These never do, except if the entire file is position dependent or if
@@ -443,22 +456,22 @@
 //
 // If two or more symbols are at the same offset, and at least one of
 // them are copied by a copy relocation, all of them need to be copied.
-// Otherwise, they would refer different places at runtime.
+// Otherwise, they would refer to different places at runtime.
 template <class ELFT>
-static std::vector<SharedSymbol *> getSymbolsAt(SharedSymbol &SS) {
+static SmallSet<SharedSymbol *, 4> getSymbolsAt(SharedSymbol &SS) {
   typedef typename ELFT::Sym Elf_Sym;
 
   SharedFile<ELFT> &File = SS.getFile<ELFT>();
 
-  std::vector<SharedSymbol *> Ret;
+  SmallSet<SharedSymbol *, 4> Ret;
   for (const Elf_Sym &S : File.getGlobalELFSyms()) {
     if (S.st_shndx == SHN_UNDEF || S.st_shndx == SHN_ABS ||
-        S.st_value != SS.Value)
+        S.getType() == STT_TLS || S.st_value != SS.Value)
       continue;
     StringRef Name = check(S.getName(File.getStringTable()));
     Symbol *Sym = Symtab->find(Name);
     if (auto *Alias = dyn_cast_or_null<SharedSymbol>(Sym))
-      Ret.push_back(Alias);
+      Ret.insert(Alias);
   }
   return Ret;
 }
@@ -476,7 +489,6 @@
   Sym.PltIndex = Old.PltIndex;
   Sym.GotIndex = Old.GotIndex;
   Sym.VerdefIndex = Old.VerdefIndex;
-  Sym.IsInGlobalMipsGot = Old.IsInGlobalMipsGot;
   Sym.IsPreemptible = true;
   Sym.ExportDynamic = true;
   Sym.IsUsedInRegularObj = true;
@@ -528,7 +540,7 @@
 template <class ELFT> static void addCopyRelSymbol(SharedSymbol &SS) {
   // Copy relocation against zero-sized symbol doesn't make sense.
   uint64_t SymSize = SS.getSize();
-  if (SymSize == 0)
+  if (SymSize == 0 || SS.Alignment == 0)
     fatal("cannot create a copy relocation for symbol " + toString(SS));
 
   // See if this symbol is in a read-only segment. If so, preserve the symbol's
@@ -705,6 +717,24 @@
 };
 } // namespace
 
+static void addRelativeReloc(InputSectionBase *IS, uint64_t OffsetInSec,
+                             Symbol *Sym, int64_t Addend, RelExpr Expr,
+                             RelType Type) {
+  // Add a relative relocation. If RelrDyn section is enabled, and the
+  // relocation offset is guaranteed to be even, add the relocation to
+  // the RelrDyn section, otherwise add it to the RelaDyn section.
+  // RelrDyn sections don't support odd offsets. Also, RelrDyn sections
+  // don't store the addend values, so we must write it to the relocated
+  // address.
+  if (InX::RelrDyn && IS->Alignment >= 2 && OffsetInSec % 2 == 0) {
+    IS->Relocations.push_back({Expr, Type, OffsetInSec, Addend, Sym});
+    InX::RelrDyn->Relocs.push_back({IS, OffsetInSec});
+    return;
+  }
+  InX::RelaDyn->addReloc(Target->RelativeRel, IS, OffsetInSec, Sym, Addend,
+                         Expr, Type);
+}
+
 template <class ELFT, class GotPltSection>
 static void addPltEntry(PltSection *Plt, GotPltSection *GotPlt,
                         RelocationBaseSection *Rel, RelType Type, Symbol &Sym) {
@@ -736,14 +766,12 @@
 
   // Otherwise, we emit a dynamic relocation to .rel[a].dyn so that
   // the GOT slot will be fixed at load-time.
-  RelType Type;
-  if (Sym.isTls())
-    Type = Target->TlsGotRel;
-  else if (!Sym.IsPreemptible && Config->Pic && !isAbsolute(Sym))
-    Type = Target->RelativeRel;
-  else
-    Type = Target->GotRel;
-  InX::RelaDyn->addReloc(Type, InX::Got, Off, &Sym, 0,
+  if (!Sym.isTls() && !Sym.IsPreemptible && Config->Pic && !isAbsolute(Sym)) {
+    addRelativeReloc(InX::Got, Off, &Sym, 0, R_ABS, Target->GotRel);
+    return;
+  }
+  InX::RelaDyn->addReloc(Sym.isTls() ? Target->TlsGotRel : Target->GotRel,
+                         InX::Got, Off, &Sym, 0,
                          Sym.IsPreemptible ? R_ADDEND : R_ABS, Target->GotRel);
 }
 
@@ -794,8 +822,7 @@
     bool IsPreemptibleValue = Sym.IsPreemptible && Expr != R_GOT;
 
     if (!IsPreemptibleValue) {
-      InX::RelaDyn->addReloc(Target->RelativeRel, &Sec, Offset, &Sym, Addend,
-                             Expr, Type);
+      addRelativeReloc(&Sec, Offset, &Sym, Addend, Expr, Type);
       return;
     } else if (RelType Rel = Target->getDynRel(Type)) {
       InX::RelaDyn->addReloc(Rel, &Sec, Offset, &Sym, Addend, R_ADDEND, Type);
@@ -816,7 +843,7 @@
       // a dynamic relocation.
       // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf p.4-19
       if (Config->EMachine == EM_MIPS)
-        InX::MipsGot->addEntry(Sym, Addend, Expr);
+        InX::MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
       return;
     }
   }
@@ -1002,10 +1029,7 @@
       // See "Global Offset Table" in Chapter 5 in the following document
       // for detailed description:
       // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
-      InX::MipsGot->addEntry(Sym, Addend, Expr);
-      if (Sym.isTls() && Sym.IsPreemptible)
-        InX::RelaDyn->addReloc(Target->TlsGotRel, InX::MipsGot,
-                               Sym.getGotOffset(), &Sym);
+      InX::MipsGot->addEntry(*Sec.File, Sym, Addend, Expr);
     } else if (!Sym.isInGot()) {
       addGotEntry<ELFT>(Sym);
     }
@@ -1023,6 +1047,11 @@
 
   for (auto I = Rels.begin(), End = Rels.end(); I != End;)
     scanReloc<ELFT>(Sec, GetOffset, I, End);
+
+  // Sort relocations by offset to binary search for R_RISCV_PCREL_HI20
+  if (Config->EMachine == EM_RISCV)
+    std::stable_sort(Sec.Relocations.begin(), Sec.Relocations.end(),
+                     RelocationOffsetComparator{});
 }
 
 template <class ELFT> void elf::scanRelocations(InputSectionBase &S) {
@@ -1309,7 +1338,7 @@
 // InputSectionDescription::Sections.
 void ThunkCreator::forEachInputSectionDescription(
     ArrayRef<OutputSection *> OutputSections,
-    std::function<void(OutputSection *, InputSectionDescription *)> Fn) {
+    llvm::function_ref<void(OutputSection *, InputSectionDescription *)> Fn) {
   for (OutputSection *OS : OutputSections) {
     if (!(OS->Flags & SHF_ALLOC) || !(OS->Flags & SHF_EXECINSTR))
       continue;
diff --git a/ELF/Relocations.h b/ELF/Relocations.h
index 3a930d9..76bb41d 100644
--- a/ELF/Relocations.h
+++ b/ELF/Relocations.h
@@ -67,11 +67,14 @@
   R_RELAX_TLS_GD_TO_IE,
   R_RELAX_TLS_GD_TO_IE_ABS,
   R_RELAX_TLS_GD_TO_IE_END,
+  R_RELAX_TLS_GD_TO_IE_GOT_OFF,
   R_RELAX_TLS_GD_TO_IE_PAGE_PC,
   R_RELAX_TLS_GD_TO_LE,
   R_RELAX_TLS_GD_TO_LE_NEG,
   R_RELAX_TLS_IE_TO_LE,
   R_RELAX_TLS_LD_TO_LE,
+  R_RELAX_TLS_LD_TO_LE_ABS,
+  R_RISCV_PC_INDIRECT,
   R_SIZE,
   R_TLS,
   R_TLSDESC,
@@ -80,8 +83,10 @@
   R_TLSGD_GOT,
   R_TLSGD_GOT_FROM_END,
   R_TLSGD_PC,
-  R_TLSLD_GOT_FROM_END,
   R_TLSLD_GOT,
+  R_TLSLD_GOT_FROM_END,
+  R_TLSLD_GOT_OFF,
+  R_TLSLD_HINT,
   R_TLSLD_PC,
 };
 
@@ -124,6 +129,21 @@
   Symbol *Sym;
 };
 
+struct RelocationOffsetComparator {
+  bool operator()(const Relocation &Lhs, const Relocation &Rhs) {
+    return Lhs.Offset < Rhs.Offset;
+  }
+
+  // For std::lower_bound, std::upper_bound, std::equal_range.
+  bool operator()(const Relocation &Rel, uint64_t Val) {
+    return Rel.Offset < Val;
+  }
+
+  bool operator()(uint64_t Val, const Relocation &Rel) {
+    return Val < Rel.Offset;
+  }
+};
+
 template <class ELFT> void scanRelocations(InputSectionBase &);
 
 class ThunkSection;
@@ -153,7 +173,7 @@
 
   void forEachInputSectionDescription(
       ArrayRef<OutputSection *> OutputSections,
-      std::function<void(OutputSection *, InputSectionDescription *)> Fn);
+      llvm::function_ref<void(OutputSection *, InputSectionDescription *)> Fn);
 
   std::pair<Thunk *, bool> getThunk(Symbol &Sym, RelType Type, uint64_t Src);
 
diff --git a/ELF/ScriptLexer.cpp b/ELF/ScriptLexer.cpp
index ef5a1cf..d4b1f6d 100644
--- a/ELF/ScriptLexer.cpp
+++ b/ELF/ScriptLexer.cpp
@@ -66,8 +66,6 @@
 
 std::string ScriptLexer::getCurrentLocation() {
   std::string Filename = getCurrentMB().getBufferIdentifier();
-  if (!Pos)
-    return Filename;
   return (Filename + ":" + Twine(getLineNumber())).str();
 }
 
@@ -116,8 +114,9 @@
     }
 
     // ">foo" is parsed to ">" and "foo", but ">>" is parsed to ">>".
+    // "|", "||", "&" and "&&" are different operators.
     if (S.startswith("<<") || S.startswith("<=") || S.startswith(">>") ||
-        S.startswith(">=")) {
+        S.startswith(">=") || S.startswith("||") || S.startswith("&&")) {
       Vec.push_back(S.substr(0, 2));
       S = S.substr(2);
       continue;
@@ -282,10 +281,7 @@
 
 MemoryBufferRef ScriptLexer::getCurrentMB() {
   // Find input buffer containing the current token.
-  assert(!MBs.empty());
-  if (!Pos)
-    return MBs[0];
-
+  assert(!MBs.empty() && Pos > 0);
   for (MemoryBufferRef MB : MBs)
     if (encloses(MB.getBuffer(), Tokens[Pos - 1]))
       return MB;
diff --git a/ELF/ScriptParser.cpp b/ELF/ScriptParser.cpp
index 707e104..ffc69b2 100644
--- a/ELF/ScriptParser.cpp
+++ b/ELF/ScriptParser.cpp
@@ -72,6 +72,7 @@
   void readRegionAlias();
   void readSearchDir();
   void readSections();
+  void readTarget();
   void readVersion();
   void readVersionScriptCommand();
 
@@ -80,7 +81,9 @@
   uint32_t readFill();
   uint32_t parseFill(StringRef Tok);
   void readSectionAddressType(OutputSection *Cmd);
+  OutputSection *readOverlaySectionDescription();
   OutputSection *readOutputSectionDescription(StringRef OutSec);
+  std::vector<BaseCommand *> readOverlay();
   std::vector<StringRef> readOutputSectionPhdrs();
   InputSectionDescription *readInputSectionDescription(StringRef Tok);
   StringMatcher readFilePatterns();
@@ -253,6 +256,8 @@
       readSearchDir();
     } else if (Tok == "SECTIONS") {
       readSections();
+    } else if (Tok == "TARGET") {
+      readTarget();
     } else if (Tok == "VERSION") {
       readVersion();
     } else if (SymbolAssignment *Cmd = readAssignment(Tok)) {
@@ -342,7 +347,7 @@
     return;
   }
 
-  if (Optional<std::string> Path = searchLinkerScript(Tok)) {
+  if (Optional<std::string> Path = searchScript(Tok)) {
     if (Optional<MemoryBufferRef> MB = readFile(*Path))
       tokenize(*MB);
     return;
@@ -436,6 +441,49 @@
   expect(")");
 }
 
+// This reads an overlay description. Overlays are used to describe output
+// sections that use the same virtual memory range and normally would trigger
+// linker's sections sanity check failures.
+// https://sourceware.org/binutils/docs/ld/Overlay-Description.html#Overlay-Description
+std::vector<BaseCommand *> ScriptParser::readOverlay() {
+  // VA and LMA expressions are optional, though for simplicity of
+  // implementation we assume they are not. That is what OVERLAY was designed
+  // for first of all: to allow sections with overlapping VAs at different LMAs.
+  Expr AddrExpr = readExpr();
+  expect(":");
+  expect("AT");
+  Expr LMAExpr = readParenExpr();
+  expect("{");
+
+  std::vector<BaseCommand *> V;
+  OutputSection *Prev = nullptr;
+  while (!errorCount() && !consume("}")) {
+    // VA is the same for all sections. The LMAs are consecutive in memory
+    // starting from the base load address specified.
+    OutputSection *OS = readOverlaySectionDescription();
+    OS->AddrExpr = AddrExpr;
+    if (Prev)
+      OS->LMAExpr = [=] { return Prev->getLMA() + Prev->Size; };
+    else
+      OS->LMAExpr = LMAExpr;
+    V.push_back(OS);
+    Prev = OS;
+  }
+
+  // According to the specification, at the end of the overlay, the location
+  // counter should be equal to the overlay base address plus size of the
+  // largest section seen in the overlay.
+  // Here we want to create the Dot assignment command to achieve that.
+  Expr MoveDot = [=] {
+    uint64_t Max = 0;
+    for (BaseCommand *Cmd : V)
+      Max = std::max(Max, cast<OutputSection>(Cmd)->Size);
+    return AddrExpr().getValue() + Max;
+  };
+  V.push_back(make<SymbolAssignment>(".", MoveDot, getCurrentLocation()));
+  return V;
+}
+
 void ScriptParser::readSections() {
   Script->HasSectionsCommand = true;
 
@@ -448,6 +496,12 @@
   std::vector<BaseCommand *> V;
   while (!errorCount() && !consume("}")) {
     StringRef Tok = next();
+    if (Tok == "OVERLAY") {
+      for (BaseCommand *Cmd : readOverlay())
+        V.push_back(Cmd);
+      continue;
+    }
+
     if (BaseCommand *Cmd = readAssignment(Tok))
       V.push_back(Cmd);
     else
@@ -471,14 +525,33 @@
                                  V.end());
 }
 
+void ScriptParser::readTarget() {
+  // TARGET(foo) is an alias for "--format foo". Unlike GNU linkers,
+  // we accept only a limited set of BFD names (i.e. "elf" or "binary")
+  // for --format. We recognize only /^elf/ and "binary" in the linker
+  // script as well.
+  expect("(");
+  StringRef Tok = next();
+  expect(")");
+
+  if (Tok.startswith("elf"))
+    Config->FormatBinary = false;
+  else if (Tok == "binary")
+    Config->FormatBinary = true;
+  else
+    setError("unknown target: " + Tok);
+}
+
 static int precedence(StringRef Op) {
   return StringSwitch<int>(Op)
-      .Cases("*", "/", "%", 6)
-      .Cases("+", "-", 5)
-      .Cases("<<", ">>", 4)
-      .Cases("<", "<=", ">", ">=", "==", "!=", 3)
-      .Case("&", 2)
-      .Case("|", 1)
+      .Cases("*", "/", "%", 8)
+      .Cases("+", "-", 7)
+      .Cases("<<", ">>", 6)
+      .Cases("<", "<=", ">", ">=", "==", "!=", 5)
+      .Case("&", 4)
+      .Case("|", 3)
+      .Case("&&", 2)
+      .Case("||", 1)
       .Default(-1);
 }
 
@@ -673,6 +746,17 @@
   };
 }
 
+OutputSection *ScriptParser::readOverlaySectionDescription() {
+  OutputSection *Cmd =
+      Script->createOutputSection(next(), getCurrentLocation());
+  Cmd->InOverlay = true;
+  expect("{");
+  while (!errorCount() && !consume("}"))
+    Cmd->SectionCommands.push_back(readInputSectionRules(next()));
+  Cmd->Phdrs = readOutputSectionPhdrs();
+  return Cmd;
+}
+
 OutputSection *ScriptParser::readOutputSectionDescription(StringRef OutSec) {
   OutputSection *Cmd =
       Script->createOutputSection(OutSec, getCurrentLocation());
@@ -862,6 +946,10 @@
     return [=] { return L().getValue() == R().getValue(); };
   if (Op == "!=")
     return [=] { return L().getValue() != R().getValue(); };
+  if (Op == "||")
+    return [=] { return L().getValue() || R().getValue(); };
+  if (Op == "&&")
+    return [=] { return L().getValue() && R().getValue(); };
   if (Op == "&")
     return [=] { return bitAnd(L(), R()); };
   if (Op == "|")
diff --git a/ELF/SymbolTable.cpp b/ELF/SymbolTable.cpp
index 00c7f74..1f5a84e 100644
--- a/ELF/SymbolTable.cpp
+++ b/ELF/SymbolTable.cpp
@@ -38,7 +38,7 @@
     return ObjectFiles[0];
   if (!SharedFiles.empty())
     return SharedFiles[0];
-  return nullptr;
+  return BitcodeFiles[0];
 }
 
 // All input object files must be for the same architecture
@@ -158,6 +158,12 @@
   Symbol *Sym = find(Name);
   if (!Sym)
     return;
+
+  // Do not wrap the same symbol twice.
+  for (const WrappedSymbol &S : WrappedSymbols)
+    if (S.Sym == Sym)
+      return;
+
   Symbol *Real = addUndefined<ELFT>(Saver.save("__real_" + Name));
   Symbol *Wrap = addUndefined<ELFT>(Saver.save("__wrap_" + Name));
   WrappedSymbols.push_back({Sym, Real, Wrap});
diff --git a/ELF/Symbols.cpp b/ELF/Symbols.cpp
index f2f9ae0..c16bda8 100644
--- a/ELF/Symbols.cpp
+++ b/ELF/Symbols.cpp
@@ -39,6 +39,7 @@
 Defined *ElfSym::MipsGpDisp;
 Defined *ElfSym::MipsLocalGp;
 Defined *ElfSym::RelaIpltEnd;
+Defined *ElfSym::RISCVGlobalPointer;
 
 static uint64_t getSymVA(const Symbol &Sym, int64_t &Addend) {
   switch (Sym.kind()) {
@@ -144,9 +145,7 @@
 uint64_t Symbol::getSize() const {
   if (const auto *DR = dyn_cast<Defined>(this))
     return DR->Size;
-  if (const auto *S = dyn_cast<SharedSymbol>(this))
-    return S->Size;
-  return 0;
+  return cast<SharedSymbol>(this)->Size;
 }
 
 OutputSection *Symbol::getOutputSection() const {
@@ -206,6 +205,15 @@
 
 InputFile *LazyArchive::fetch() { return cast<ArchiveFile>(File)->fetch(Sym); }
 
+MemoryBufferRef LazyArchive::getMemberBuffer() {
+  Archive::Child C = CHECK(
+      Sym.getMember(), "could not get the member for symbol " + Sym.getName());
+
+  return CHECK(C.getMemoryBufferRef(),
+               "could not get the buffer for the member defining symbol " +
+                   Sym.getName());
+}
+
 uint8_t Symbol::computeBinding() const {
   if (Config->Relocatable)
     return Binding;
diff --git a/ELF/Symbols.h b/ELF/Symbols.h
index f0912d2..a1bab2e 100644
--- a/ELF/Symbols.h
+++ b/ELF/Symbols.h
@@ -158,19 +158,13 @@
          uint8_t StOther, uint8_t Type)
       : File(File), NameData(Name.Data), NameSize(Name.Size), Binding(Binding),
         Type(Type), StOther(StOther), SymbolKind(K), NeedsPltAddr(false),
-        IsInGlobalMipsGot(false), Is32BitMipsGot(false), IsInIplt(false),
-        IsInIgot(false), IsPreemptible(false), Used(!Config->GcSections),
-        NeedsTocRestore(false) {}
+        IsInIplt(false), IsInIgot(false), IsPreemptible(false),
+        Used(!Config->GcSections), NeedsTocRestore(false) {}
 
 public:
   // True the symbol should point to its PLT entry.
   // For SharedSymbol only.
   unsigned NeedsPltAddr : 1;
-  // True if this symbol has an entry in the global part of MIPS GOT.
-  unsigned IsInGlobalMipsGot : 1;
-
-  // True if this symbol is referenced by 32-bit GOT relocations.
-  unsigned Is32BitMipsGot : 1;
 
   // True if this symbol is in the Iplt sub-section of the Plt.
   unsigned IsInIplt : 1;
@@ -292,6 +286,7 @@
   static bool classof(const Symbol *S) { return S->kind() == LazyArchiveKind; }
 
   InputFile *fetch();
+  MemoryBufferRef getMemberBuffer();
 
 private:
   const llvm::object::Archive::Symbol Sym;
@@ -338,6 +333,9 @@
 
   // __rela_iplt_end or __rel_iplt_end
   static Defined *RelaIpltEnd;
+
+  // __global_pointer$ in RISC-V.
+  static Defined *RISCVGlobalPointer;
 };
 
 // A buffer class that is large enough to hold any Symbol-derived
diff --git a/ELF/SyntheticSections.cpp b/ELF/SyntheticSections.cpp
index 1c66bcb..907eaf1 100644
--- a/ELF/SyntheticSections.cpp
+++ b/ELF/SyntheticSections.cpp
@@ -29,6 +29,7 @@
 #include "lld/Common/Strings.h"
 #include "lld/Common/Threads.h"
 #include "lld/Common/Version.h"
+#include "llvm/ADT/SetOperations.h"
 #include "llvm/BinaryFormat/Dwarf.h"
 #include "llvm/DebugInfo/DWARF/DWARFDebugPubTable.h"
 #include "llvm/Object/Decompressor.h"
@@ -51,6 +52,7 @@
 using namespace lld;
 using namespace lld::elf;
 
+using llvm::support::endian::read32le;
 using llvm::support::endian::write32le;
 using llvm::support::endian::write64le;
 
@@ -326,8 +328,6 @@
 BssSection::BssSection(StringRef Name, uint64_t Size, uint32_t Alignment)
     : SyntheticSection(SHF_ALLOC | SHF_WRITE, SHT_NOBITS, Alignment, Name) {
   this->Bss = true;
-  if (OutputSection *Sec = getParent())
-    Sec->Alignment = std::max(Sec->Alignment, Alignment);
   this->Size = Size;
 }
 
@@ -335,7 +335,7 @@
   switch (Config->BuildId) {
   case BuildIdKind::Fast:
     computeHash(Buf, [](uint8_t *Dest, ArrayRef<uint8_t> Arr) {
-      write64le(Dest, xxHash64(toStringRef(Arr)));
+      write64le(Dest, xxHash64(Arr));
     });
     break;
   case BuildIdKind::Md5:
@@ -368,15 +368,11 @@
 // and where their relocations point to.
 template <class ELFT, class RelTy>
 CieRecord *EhFrameSection::addCie(EhSectionPiece &Cie, ArrayRef<RelTy> Rels) {
-  auto *Sec = cast<EhInputSection>(Cie.Sec);
-  if (read32(Cie.data().data() + 4) != 0)
-    fatal(toString(Sec) + ": CIE expected at beginning of .eh_frame");
-
   Symbol *Personality = nullptr;
   unsigned FirstRelI = Cie.FirstRelocation;
   if (FirstRelI != (unsigned)-1)
     Personality =
-        &Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
+        &Cie.Sec->template getFile<ELFT>()->getRelocTargetSym(Rels[FirstRelI]);
 
   // Search for an existing CIE by CIE contents/relocation target pair.
   CieRecord *&Rec = CieMap[{Cie.data(), Personality}];
@@ -478,9 +474,7 @@
 }
 
 void EhFrameSection::finalizeContents() {
-  if (this->Size)
-    return; // Already finalized.
-
+  assert(!this->Size); // Not finalized.
   size_t Off = 0;
   for (CieRecord *Rec : CieRecords) {
     Rec->Cie->OutputOff = Off;
@@ -508,14 +502,31 @@
   uint8_t *Buf = getParent()->Loc + OutSecOff;
   std::vector<FdeData> Ret;
 
+  uint64_t VA = InX::EhFrameHdr->getVA();
   for (CieRecord *Rec : CieRecords) {
     uint8_t Enc = getFdeEncoding(Rec->Cie);
     for (EhSectionPiece *Fde : Rec->Fdes) {
-      uint32_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
-      uint32_t FdeVA = getParent()->Addr + Fde->OutputOff;
-      Ret.push_back({Pc, FdeVA});
+      uint64_t Pc = getFdePc(Buf, Fde->OutputOff, Enc);
+      uint64_t FdeVA = getParent()->Addr + Fde->OutputOff;
+      if (!isInt<32>(Pc - VA))
+        fatal(toString(Fde->Sec) + ": PC offset is too large: 0x" +
+              Twine::utohexstr(Pc - VA));
+      Ret.push_back({uint32_t(Pc - VA), uint32_t(FdeVA - VA)});
     }
   }
+
+  // Sort the FDE list by their PC and uniqueify. Usually there is only
+  // one FDE for a PC (i.e. function), but if ICF merges two functions
+  // into one, there can be more than one FDEs pointing to the address.
+  auto Less = [](const FdeData &A, const FdeData &B) {
+    return A.PcRel < B.PcRel;
+  };
+  std::stable_sort(Ret.begin(), Ret.end(), Less);
+  auto Eq = [](const FdeData &A, const FdeData &B) {
+    return A.PcRel == B.PcRel;
+  };
+  Ret.erase(std::unique(Ret.begin(), Ret.end(), Eq), Ret.end());
+
   return Ret;
 }
 
@@ -523,9 +534,14 @@
   switch (Size) {
   case DW_EH_PE_udata2:
     return read16(Buf);
+  case DW_EH_PE_sdata2:
+    return (int16_t)read16(Buf);
   case DW_EH_PE_udata4:
     return read32(Buf);
+  case DW_EH_PE_sdata4:
+    return (int32_t)read32(Buf);
   case DW_EH_PE_udata8:
+  case DW_EH_PE_sdata8:
     return read64(Buf);
   case DW_EH_PE_absptr:
     return readUint(Buf);
@@ -540,7 +556,7 @@
   // The starting address to which this FDE applies is
   // stored at FDE + 8 byte.
   size_t Off = FdeOff + 8;
-  uint64_t Addr = readFdeAddr(Buf + Off, Enc & 0x7);
+  uint64_t Addr = readFdeAddr(Buf + Off, Enc & 0xf);
   if ((Enc & 0x70) == DW_EH_PE_absptr)
     return Addr;
   if ((Enc & 0x70) == DW_EH_PE_pcrel)
@@ -632,102 +648,9 @@
   // whereas InputSectionBase::relocateAlloc() expects its argument
   // to point to the start of the output section.
   Target->writeGotHeader(Buf);
-  Buf += Target->GotHeaderEntriesNum * Target->GotEntrySize;
   relocateAlloc(Buf - OutSecOff, Buf - OutSecOff + Size);
 }
 
-MipsGotSection::MipsGotSection()
-    : SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16,
-                       ".got") {}
-
-void MipsGotSection::addEntry(Symbol &Sym, int64_t Addend, RelExpr Expr) {
-  // For "true" local symbols which can be referenced from the same module
-  // only compiler creates two instructions for address loading:
-  //
-  // lw   $8, 0($gp) # R_MIPS_GOT16
-  // addi $8, $8, 0  # R_MIPS_LO16
-  //
-  // The first instruction loads high 16 bits of the symbol address while
-  // the second adds an offset. That allows to reduce number of required
-  // GOT entries because only one global offset table entry is necessary
-  // for every 64 KBytes of local data. So for local symbols we need to
-  // allocate number of GOT entries to hold all required "page" addresses.
-  //
-  // All global symbols (hidden and regular) considered by compiler uniformly.
-  // It always generates a single `lw` instruction and R_MIPS_GOT16 relocation
-  // to load address of the symbol. So for each such symbol we need to
-  // allocate dedicated GOT entry to store its address.
-  //
-  // If a symbol is preemptible we need help of dynamic linker to get its
-  // final address. The corresponding GOT entries are allocated in the
-  // "global" part of GOT. Entries for non preemptible global symbol allocated
-  // in the "local" part of GOT.
-  //
-  // See "Global Offset Table" in Chapter 5:
-  // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
-  if (Expr == R_MIPS_GOT_LOCAL_PAGE) {
-    // At this point we do not know final symbol value so to reduce number
-    // of allocated GOT entries do the following trick. Save all output
-    // sections referenced by GOT relocations. Then later in the `finalize`
-    // method calculate number of "pages" required to cover all saved output
-    // section and allocate appropriate number of GOT entries.
-    PageIndexMap.insert({Sym.getOutputSection(), 0});
-    return;
-  }
-  if (Sym.isTls()) {
-    // GOT entries created for MIPS TLS relocations behave like
-    // almost GOT entries from other ABIs. They go to the end
-    // of the global offset table.
-    Sym.GotIndex = TlsEntries.size();
-    TlsEntries.push_back(&Sym);
-    return;
-  }
-  auto AddEntry = [&](Symbol &S, uint64_t A, GotEntries &Items) {
-    if (S.isInGot() && !A)
-      return;
-    size_t NewIndex = Items.size();
-    if (!EntryIndexMap.insert({{&S, A}, NewIndex}).second)
-      return;
-    Items.emplace_back(&S, A);
-    if (!A)
-      S.GotIndex = NewIndex;
-  };
-  if (Sym.IsPreemptible) {
-    // Ignore addends for preemptible symbols. They got single GOT entry anyway.
-    AddEntry(Sym, 0, GlobalEntries);
-    Sym.IsInGlobalMipsGot = true;
-  } else if (Expr == R_MIPS_GOT_OFF32) {
-    AddEntry(Sym, Addend, LocalEntries32);
-    Sym.Is32BitMipsGot = true;
-  } else {
-    // Hold local GOT entries accessed via a 16-bit index separately.
-    // That allows to write them in the beginning of the GOT and keep
-    // their indexes as less as possible to escape relocation's overflow.
-    AddEntry(Sym, Addend, LocalEntries);
-  }
-}
-
-bool MipsGotSection::addDynTlsEntry(Symbol &Sym) {
-  if (Sym.GlobalDynIndex != -1U)
-    return false;
-  Sym.GlobalDynIndex = TlsEntries.size();
-  // Global Dynamic TLS entries take two GOT slots.
-  TlsEntries.push_back(nullptr);
-  TlsEntries.push_back(&Sym);
-  return true;
-}
-
-// Reserves TLS entries for a TLS module ID and a TLS block offset.
-// In total it takes two GOT slots.
-bool MipsGotSection::addTlsIndex() {
-  if (TlsIndexOff != uint32_t(-1))
-    return false;
-  TlsIndexOff = TlsEntries.size() * Config->Wordsize;
-  TlsEntries.push_back(nullptr);
-  TlsEntries.push_back(nullptr);
-  return true;
-}
-
 static uint64_t getMipsPageAddr(uint64_t Addr) {
   return (Addr + 0x8000) & ~0xffff;
 }
@@ -736,80 +659,355 @@
   return (Size + 0xfffe) / 0xffff + 1;
 }
 
-uint64_t MipsGotSection::getPageEntryOffset(const Symbol &B,
-                                            int64_t Addend) const {
-  const OutputSection *OutSec = B.getOutputSection();
-  uint64_t SecAddr = getMipsPageAddr(OutSec->Addr);
-  uint64_t SymAddr = getMipsPageAddr(B.getVA(Addend));
-  uint64_t Index = PageIndexMap.lookup(OutSec) + (SymAddr - SecAddr) / 0xffff;
-  assert(Index < PageEntriesNum);
-  return (HeaderEntriesNum + Index) * Config->Wordsize;
+MipsGotSection::MipsGotSection()
+    : SyntheticSection(SHF_ALLOC | SHF_WRITE | SHF_MIPS_GPREL, SHT_PROGBITS, 16,
+                       ".got") {}
+
+void MipsGotSection::addEntry(InputFile &File, Symbol &Sym, int64_t Addend,
+                              RelExpr Expr) {
+  FileGot &G = getGot(File);
+  if (Expr == R_MIPS_GOT_LOCAL_PAGE) {
+    if (const OutputSection *OS = Sym.getOutputSection())
+      G.PagesMap.insert({OS, {}});
+    else
+      G.Local16.insert({{nullptr, getMipsPageAddr(Sym.getVA(Addend))}, 0});
+  } else if (Sym.isTls())
+    G.Tls.insert({&Sym, 0});
+  else if (Sym.IsPreemptible && Expr == R_ABS)
+    G.Relocs.insert({&Sym, 0});
+  else if (Sym.IsPreemptible)
+    G.Global.insert({&Sym, 0});
+  else if (Expr == R_MIPS_GOT_OFF32)
+    G.Local32.insert({{&Sym, Addend}, 0});
+  else
+    G.Local16.insert({{&Sym, Addend}, 0});
 }
 
-uint64_t MipsGotSection::getSymEntryOffset(const Symbol &B,
-                                           int64_t Addend) const {
-  // Calculate offset of the GOT entries block: TLS, global, local.
-  uint64_t Index = HeaderEntriesNum + PageEntriesNum;
-  if (B.isTls())
-    Index += LocalEntries.size() + LocalEntries32.size() + GlobalEntries.size();
-  else if (B.IsInGlobalMipsGot)
-    Index += LocalEntries.size() + LocalEntries32.size();
-  else if (B.Is32BitMipsGot)
-    Index += LocalEntries.size();
-  // Calculate offset of the GOT entry in the block.
-  if (B.isInGot())
-    Index += B.GotIndex;
-  else {
-    auto It = EntryIndexMap.find({&B, Addend});
-    assert(It != EntryIndexMap.end());
-    Index += It->second;
+void MipsGotSection::addDynTlsEntry(InputFile &File, Symbol &Sym) {
+  getGot(File).DynTlsSymbols.insert({&Sym, 0});
+}
+
+void MipsGotSection::addTlsIndex(InputFile &File) {
+  getGot(File).DynTlsSymbols.insert({nullptr, 0});
+}
+
+size_t MipsGotSection::FileGot::getEntriesNum() const {
+  return getPageEntriesNum() + Local16.size() + Global.size() + Relocs.size() +
+         Tls.size() + DynTlsSymbols.size() * 2;
+}
+
+size_t MipsGotSection::FileGot::getPageEntriesNum() const {
+  size_t Num = 0;
+  for (const std::pair<const OutputSection *, FileGot::PageBlock> &P : PagesMap)
+    Num += P.second.Count;
+  return Num;
+}
+
+size_t MipsGotSection::FileGot::getIndexedEntriesNum() const {
+  size_t Count = getPageEntriesNum() + Local16.size() + Global.size();
+  // If there are relocation-only entries in the GOT, TLS entries
+  // are allocated after them. TLS entries should be addressable
+  // by 16-bit index so count both reloc-only and TLS entries.
+  if (!Tls.empty() || !DynTlsSymbols.empty())
+    Count += Relocs.size() + Tls.size() + DynTlsSymbols.size() * 2;
+  return Count;
+}
+
+MipsGotSection::FileGot &MipsGotSection::getGot(InputFile &F) {
+  if (!F.MipsGotIndex.hasValue()) {
+    Gots.emplace_back();
+    Gots.back().File = &F;
+    F.MipsGotIndex = Gots.size() - 1;
+  }
+  return Gots[*F.MipsGotIndex];
+}
+
+uint64_t MipsGotSection::getPageEntryOffset(const InputFile *F,
+                                            const Symbol &Sym,
+                                            int64_t Addend) const {
+  const FileGot &G = Gots[*F->MipsGotIndex];
+  uint64_t Index = 0;
+  if (const OutputSection *OutSec = Sym.getOutputSection()) {
+    uint64_t SecAddr = getMipsPageAddr(OutSec->Addr);
+    uint64_t SymAddr = getMipsPageAddr(Sym.getVA(Addend));
+    Index = G.PagesMap.lookup(OutSec).FirstIndex + (SymAddr - SecAddr) / 0xffff;
+  } else {
+    Index = G.Local16.lookup({nullptr, getMipsPageAddr(Sym.getVA(Addend))});
   }
   return Index * Config->Wordsize;
 }
 
-uint64_t MipsGotSection::getTlsOffset() const {
-  return (getLocalEntriesNum() + GlobalEntries.size()) * Config->Wordsize;
+uint64_t MipsGotSection::getSymEntryOffset(const InputFile *F, const Symbol &S,
+                                           int64_t Addend) const {
+  const FileGot &G = Gots[*F->MipsGotIndex];
+  Symbol *Sym = const_cast<Symbol *>(&S);
+  if (Sym->isTls())
+    return G.Tls.lookup(Sym) * Config->Wordsize;
+  if (Sym->IsPreemptible)
+    return G.Global.lookup(Sym) * Config->Wordsize;
+  return G.Local16.lookup({Sym, Addend}) * Config->Wordsize;
 }
 
-uint64_t MipsGotSection::getGlobalDynOffset(const Symbol &B) const {
-  return B.GlobalDynIndex * Config->Wordsize;
+uint64_t MipsGotSection::getTlsIndexOffset(const InputFile *F) const {
+  const FileGot &G = Gots[*F->MipsGotIndex];
+  return G.DynTlsSymbols.lookup(nullptr) * Config->Wordsize;
+}
+
+uint64_t MipsGotSection::getGlobalDynOffset(const InputFile *F,
+                                            const Symbol &S) const {
+  const FileGot &G = Gots[*F->MipsGotIndex];
+  Symbol *Sym = const_cast<Symbol *>(&S);
+  return G.DynTlsSymbols.lookup(Sym) * Config->Wordsize;
 }
 
 const Symbol *MipsGotSection::getFirstGlobalEntry() const {
-  return GlobalEntries.empty() ? nullptr : GlobalEntries.front().first;
+  if (Gots.empty())
+    return nullptr;
+  const FileGot &PrimGot = Gots.front();
+  if (!PrimGot.Global.empty())
+    return PrimGot.Global.front().first;
+  if (!PrimGot.Relocs.empty())
+    return PrimGot.Relocs.front().first;
+  return nullptr;
 }
 
 unsigned MipsGotSection::getLocalEntriesNum() const {
-  return HeaderEntriesNum + PageEntriesNum + LocalEntries.size() +
-         LocalEntries32.size();
+  if (Gots.empty())
+    return HeaderEntriesNum;
+  return HeaderEntriesNum + Gots.front().getPageEntriesNum() +
+         Gots.front().Local16.size();
+}
+
+bool MipsGotSection::tryMergeGots(FileGot &Dst, FileGot &Src, bool IsPrimary) {
+  FileGot Tmp = Dst;
+  set_union(Tmp.PagesMap, Src.PagesMap);
+  set_union(Tmp.Local16, Src.Local16);
+  set_union(Tmp.Global, Src.Global);
+  set_union(Tmp.Relocs, Src.Relocs);
+  set_union(Tmp.Tls, Src.Tls);
+  set_union(Tmp.DynTlsSymbols, Src.DynTlsSymbols);
+
+  size_t Count = IsPrimary ? HeaderEntriesNum : 0;
+  Count += Tmp.getIndexedEntriesNum();
+
+  if (Count * Config->Wordsize > Config->MipsGotSize)
+    return false;
+
+  std::swap(Tmp, Dst);
+  return true;
 }
 
 void MipsGotSection::finalizeContents() { updateAllocSize(); }
 
 bool MipsGotSection::updateAllocSize() {
-  PageEntriesNum = 0;
-  for (std::pair<const OutputSection *, size_t> &P : PageIndexMap) {
-    // For each output section referenced by GOT page relocations calculate
-    // and save into PageIndexMap an upper bound of MIPS GOT entries required
-    // to store page addresses of local symbols. We assume the worst case -
-    // each 64kb page of the output section has at least one GOT relocation
-    // against it. And take in account the case when the section intersects
-    // page boundaries.
-    P.second = PageEntriesNum;
-    PageEntriesNum += getMipsPageCount(P.first->Size);
-  }
-  Size = (getLocalEntriesNum() + GlobalEntries.size() + TlsEntries.size()) *
-         Config->Wordsize;
+  Size = HeaderEntriesNum * Config->Wordsize;
+  for (const FileGot &G : Gots)
+    Size += G.getEntriesNum() * Config->Wordsize;
   return false;
 }
 
+template <class ELFT> void MipsGotSection::build() {
+  if (Gots.empty())
+    return;
+
+  std::vector<FileGot> MergedGots(1);
+
+  // For each GOT move non-preemptible symbols from the `Global`
+  // to `Local16` list. Preemptible symbol might become non-preemptible
+  // one if, for example, it gets a related copy relocation.
+  for (FileGot &Got : Gots) {
+    for (auto &P: Got.Global)
+      if (!P.first->IsPreemptible)
+        Got.Local16.insert({{P.first, 0}, 0});
+    Got.Global.remove_if([&](const std::pair<Symbol *, size_t> &P) {
+      return !P.first->IsPreemptible;
+    });
+  }
+
+  // For each GOT remove "reloc-only" entry if there is "global"
+  // entry for the same symbol. And add local entries which indexed
+  // using 32-bit value at the end of 16-bit entries.
+  for (FileGot &Got : Gots) {
+    Got.Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
+      return Got.Global.count(P.first);
+    });
+    set_union(Got.Local16, Got.Local32);
+    Got.Local32.clear();
+  }
+
+  // Evaluate number of "reloc-only" entries in the resulting GOT.
+  // To do that put all unique "reloc-only" and "global" entries
+  // from all GOTs to the future primary GOT.
+  FileGot *PrimGot = &MergedGots.front();
+  for (FileGot &Got : Gots) {
+    set_union(PrimGot->Relocs, Got.Global);
+    set_union(PrimGot->Relocs, Got.Relocs);
+    Got.Relocs.clear();
+  }
+
+  // Evaluate number of "page" entries in each GOT.
+  for (FileGot &Got : Gots) {
+    for (std::pair<const OutputSection *, FileGot::PageBlock> &P :
+         Got.PagesMap) {
+      const OutputSection *OS = P.first;
+      uint64_t SecSize = 0;
+      for (BaseCommand *Cmd : OS->SectionCommands) {
+        if (auto *ISD = dyn_cast<InputSectionDescription>(Cmd))
+          for (InputSection *IS : ISD->Sections) {
+            uint64_t Off = alignTo(SecSize, IS->Alignment);
+            SecSize = Off + IS->getSize();
+          }
+      }
+      P.second.Count = getMipsPageCount(SecSize);
+    }
+  }
+
+  // Merge GOTs. Try to join as much as possible GOTs but do not exceed
+  // maximum GOT size. At first, try to fill the primary GOT because
+  // the primary GOT can be accessed in the most effective way. If it
+  // is not possible, try to fill the last GOT in the list, and finally
+  // create a new GOT if both attempts failed.
+  for (FileGot &SrcGot : Gots) {
+    InputFile *File = SrcGot.File;
+    if (tryMergeGots(MergedGots.front(), SrcGot, true)) {
+      File->MipsGotIndex = 0;
+    } else {
+      // If this is the first time we failed to merge with the primary GOT,
+      // MergedGots.back() will also be the primary GOT. We must make sure not
+      // to try to merge again with IsPrimary=false, as otherwise, if the
+      // inputs are just right, we could allow the primary GOT to become 1 or 2
+      // words too big due to ignoring the header size.
+      if (MergedGots.size() == 1 ||
+          !tryMergeGots(MergedGots.back(), SrcGot, false)) {
+        MergedGots.emplace_back();
+        std::swap(MergedGots.back(), SrcGot);
+      }
+      File->MipsGotIndex = MergedGots.size() - 1;
+    }
+  }
+  std::swap(Gots, MergedGots);
+
+  // Reduce number of "reloc-only" entries in the primary GOT
+  // by substracting "global" entries exist in the primary GOT.
+  PrimGot = &Gots.front();
+  PrimGot->Relocs.remove_if([&](const std::pair<Symbol *, size_t> &P) {
+    return PrimGot->Global.count(P.first);
+  });
+
+  // Calculate indexes for each GOT entry.
+  size_t Index = HeaderEntriesNum;
+  for (FileGot &Got : Gots) {
+    Got.StartIndex = &Got == PrimGot ? 0 : Index;
+    for (std::pair<const OutputSection *, FileGot::PageBlock> &P :
+         Got.PagesMap) {
+      // For each output section referenced by GOT page relocations calculate
+      // and save into PagesMap an upper bound of MIPS GOT entries required
+      // to store page addresses of local symbols. We assume the worst case -
+      // each 64kb page of the output section has at least one GOT relocation
+      // against it. And take in account the case when the section intersects
+      // page boundaries.
+      P.second.FirstIndex = Index;
+      Index += P.second.Count;
+    }
+    for (auto &P: Got.Local16)
+      P.second = Index++;
+    for (auto &P: Got.Global)
+      P.second = Index++;
+    for (auto &P: Got.Relocs)
+      P.second = Index++;
+    for (auto &P: Got.Tls)
+      P.second = Index++;
+    for (auto &P: Got.DynTlsSymbols) {
+      P.second = Index;
+      Index += 2;
+    }
+  }
+
+  // Update Symbol::GotIndex field to use this
+  // value later in the `sortMipsSymbols` function.
+  for (auto &P : PrimGot->Global)
+    P.first->GotIndex = P.second;
+  for (auto &P : PrimGot->Relocs)
+    P.first->GotIndex = P.second;
+
+  // Create dynamic relocations.
+  for (FileGot &Got : Gots) {
+    // Create dynamic relocations for TLS entries.
+    for (std::pair<Symbol *, size_t> &P : Got.Tls) {
+      Symbol *S = P.first;
+      uint64_t Offset = P.second * Config->Wordsize;
+      if (S->IsPreemptible)
+        InX::RelaDyn->addReloc(Target->TlsGotRel, this, Offset, S);
+    }
+    for (std::pair<Symbol *, size_t> &P : Got.DynTlsSymbols) {
+      Symbol *S = P.first;
+      uint64_t Offset = P.second * Config->Wordsize;
+      if (S == nullptr) {
+        if (!Config->Pic)
+          continue;
+        InX::RelaDyn->addReloc(Target->TlsModuleIndexRel, this, Offset, S);
+      } else {
+        // When building a shared library we still need a dynamic relocation
+        // for the module index. Therefore only checking for
+        // S->IsPreemptible is not sufficient (this happens e.g. for
+        // thread-locals that have been marked as local through a linker script)
+        if (!S->IsPreemptible && !Config->Pic)
+          continue;
+        InX::RelaDyn->addReloc(Target->TlsModuleIndexRel, this, Offset, S);
+        // However, we can skip writing the TLS offset reloc for non-preemptible
+        // symbols since it is known even in shared libraries
+        if (!S->IsPreemptible)
+          continue;
+        Offset += Config->Wordsize;
+        InX::RelaDyn->addReloc(Target->TlsOffsetRel, this, Offset, S);
+      }
+    }
+
+    // Do not create dynamic relocations for non-TLS
+    // entries in the primary GOT.
+    if (&Got == PrimGot)
+      continue;
+
+    // Dynamic relocations for "global" entries.
+    for (const std::pair<Symbol *, size_t> &P : Got.Global) {
+      uint64_t Offset = P.second * Config->Wordsize;
+      InX::RelaDyn->addReloc(Target->RelativeRel, this, Offset, P.first);
+    }
+    if (!Config->Pic)
+      continue;
+    // Dynamic relocations for "local" entries in case of PIC.
+    for (const std::pair<const OutputSection *, FileGot::PageBlock> &L :
+         Got.PagesMap) {
+      size_t PageCount = L.second.Count;
+      for (size_t PI = 0; PI < PageCount; ++PI) {
+        uint64_t Offset = (L.second.FirstIndex + PI) * Config->Wordsize;
+        InX::RelaDyn->addReloc({Target->RelativeRel, this, Offset, L.first,
+                                int64_t(PI * 0x10000)});
+      }
+    }
+    for (const std::pair<GotEntry, size_t> &P : Got.Local16) {
+      uint64_t Offset = P.second * Config->Wordsize;
+      InX::RelaDyn->addReloc({Target->RelativeRel, this, Offset, true,
+                              P.first.first, P.first.second});
+    }
+  }
+}
+
 bool MipsGotSection::empty() const {
   // We add the .got section to the result for dynamic MIPS target because
   // its address and properties are mentioned in the .dynamic section.
   return Config->Relocatable;
 }
 
-uint64_t MipsGotSection::getGp() const { return ElfSym::MipsGp->getVA(0); }
+uint64_t MipsGotSection::getGp(const InputFile *F) const {
+  // For files without related GOT or files refer a primary GOT
+  // returns "common" _gp value. For secondary GOTs calculate
+  // individual _gp values.
+  if (!F || !F->MipsGotIndex.hasValue() || *F->MipsGotIndex == 0)
+    return ElfSym::MipsGp->getVA(0);
+  return getVA() + Gots[*F->MipsGotIndex].StartIndex * Config->Wordsize +
+         0x7ff0;
+}
 
 void MipsGotSection::writeTo(uint8_t *Buf) {
   // Set the MSB of the second GOT slot. This is not required by any
@@ -827,49 +1025,51 @@
   // keep doing this for now. We really need to revisit this to see
   // if we had to do this.
   writeUint(Buf + Config->Wordsize, (uint64_t)1 << (Config->Wordsize * 8 - 1));
-  Buf += HeaderEntriesNum * Config->Wordsize;
-  // Write 'page address' entries to the local part of the GOT.
-  for (std::pair<const OutputSection *, size_t> &L : PageIndexMap) {
-    size_t PageCount = getMipsPageCount(L.first->Size);
-    uint64_t FirstPageAddr = getMipsPageAddr(L.first->Addr);
-    for (size_t PI = 0; PI < PageCount; ++PI) {
-      uint8_t *Entry = Buf + (L.second + PI) * Config->Wordsize;
-      writeUint(Entry, FirstPageAddr + PI * 0x10000);
+  for (const FileGot &G : Gots) {
+    auto Write = [&](size_t I, const Symbol *S, int64_t A) {
+      uint64_t VA = A;
+      if (S) {
+        VA = S->getVA(A);
+        if (S->StOther & STO_MIPS_MICROMIPS)
+          VA |= 1;
+      }
+      writeUint(Buf + I * Config->Wordsize, VA);
+    };
+    // Write 'page address' entries to the local part of the GOT.
+    for (const std::pair<const OutputSection *, FileGot::PageBlock> &L :
+         G.PagesMap) {
+      size_t PageCount = L.second.Count;
+      uint64_t FirstPageAddr = getMipsPageAddr(L.first->Addr);
+      for (size_t PI = 0; PI < PageCount; ++PI)
+        Write(L.second.FirstIndex + PI, nullptr, FirstPageAddr + PI * 0x10000);
     }
-  }
-  Buf += PageEntriesNum * Config->Wordsize;
-  auto AddEntry = [&](const GotEntry &SA) {
-    uint8_t *Entry = Buf;
-    Buf += Config->Wordsize;
-    const Symbol *Sym = SA.first;
-    uint64_t VA = Sym->getVA(SA.second);
-    if (Sym->StOther & STO_MIPS_MICROMIPS)
-      VA |= 1;
-    writeUint(Entry, VA);
-  };
-  std::for_each(std::begin(LocalEntries), std::end(LocalEntries), AddEntry);
-  std::for_each(std::begin(LocalEntries32), std::end(LocalEntries32), AddEntry);
-  std::for_each(std::begin(GlobalEntries), std::end(GlobalEntries), AddEntry);
-  // Initialize TLS-related GOT entries. If the entry has a corresponding
-  // dynamic relocations, leave it initialized by zero. Write down adjusted
-  // TLS symbol's values otherwise. To calculate the adjustments use offsets
-  // for thread-local storage.
-  // https://www.linux-mips.org/wiki/NPTL
-  if (TlsIndexOff != -1U && !Config->Pic)
-    writeUint(Buf + TlsIndexOff, 1);
-  for (const Symbol *B : TlsEntries) {
-    if (!B || B->IsPreemptible)
-      continue;
-    uint64_t VA = B->getVA();
-    if (B->GotIndex != -1U) {
-      uint8_t *Entry = Buf + B->GotIndex * Config->Wordsize;
-      writeUint(Entry, VA - 0x7000);
-    }
-    if (B->GlobalDynIndex != -1U) {
-      uint8_t *Entry = Buf + B->GlobalDynIndex * Config->Wordsize;
-      writeUint(Entry, 1);
-      Entry += Config->Wordsize;
-      writeUint(Entry, VA - 0x8000);
+    // Local, global, TLS, reloc-only  entries.
+    // If TLS entry has a corresponding dynamic relocations, leave it
+    // initialized by zero. Write down adjusted TLS symbol's values otherwise.
+    // To calculate the adjustments use offsets for thread-local storage.
+    // https://www.linux-mips.org/wiki/NPTL
+    for (const std::pair<GotEntry, size_t> &P : G.Local16)
+      Write(P.second, P.first.first, P.first.second);
+    // Write VA to the primary GOT only. For secondary GOTs that
+    // will be done by REL32 dynamic relocations.
+    if (&G == &Gots.front())
+      for (const std::pair<const Symbol *, size_t> &P : G.Global)
+        Write(P.second, P.first, 0);
+    for (const std::pair<Symbol *, size_t> &P : G.Relocs)
+      Write(P.second, P.first, 0);
+    for (const std::pair<Symbol *, size_t> &P : G.Tls)
+      Write(P.second, P.first, P.first->IsPreemptible ? 0 : -0x7000);
+    for (const std::pair<Symbol *, size_t> &P : G.DynTlsSymbols) {
+      if (P.first == nullptr && !Config->Pic)
+        Write(P.second, nullptr, 1);
+      else if (P.first && !P.first->IsPreemptible) {
+        // If we are emitting PIC code with relocations we mustn't write
+        // anything to the GOT here. When using Elf_Rel relocations the value
+        // one will be treated as an addend and will cause crashes at runtime
+        if (!Config->Pic)
+          Write(P.second, nullptr, 1);
+        Write(P.second + 1, P.first, -0x8000);
+      }
     }
   }
 }
@@ -1064,6 +1264,8 @@
   uint32_t DtFlags1 = 0;
   if (Config->Bsymbolic)
     DtFlags |= DF_SYMBOLIC;
+  if (Config->ZInitfirst)
+    DtFlags1 |= DF_1_INITFIRST;
   if (Config->ZNodelete)
     DtFlags1 |= DF_1_NODELETE;
   if (Config->ZNodlopen)
@@ -1113,6 +1315,14 @@
         addInt(IsRela ? DT_RELACOUNT : DT_RELCOUNT, NumRelativeRels);
     }
   }
+  if (InX::RelrDyn && !InX::RelrDyn->Relocs.empty()) {
+    addInSec(Config->UseAndroidRelrTags ? DT_ANDROID_RELR : DT_RELR,
+             InX::RelrDyn);
+    addSize(Config->UseAndroidRelrTags ? DT_ANDROID_RELRSZ : DT_RELRSZ,
+            InX::RelrDyn->getParent());
+    addInt(Config->UseAndroidRelrTags ? DT_ANDROID_RELRENT : DT_RELRENT,
+           sizeof(Elf_Relr));
+  }
   // .rel[a].plt section usually consists of two parts, containing plt and
   // iplt relocations. It is possible to have only iplt relocations in the
   // output. In that case RelaPlt is empty and have zero offset, the same offset
@@ -1234,7 +1444,10 @@
 int64_t DynamicReloc::computeAddend() const {
   if (UseSymVA)
     return Sym->getVA(Addend);
-  return Addend;
+  if (!OutputSec)
+    return Addend;
+  // See the comment in the DynamicReloc ctor.
+  return getMipsPageAddr(OutputSec->Addr) + Addend;
 }
 
 uint32_t DynamicReloc::getSymIndex() const {
@@ -1282,23 +1495,17 @@
   getParent()->Link = Link;
 }
 
+RelrBaseSection::RelrBaseSection()
+    : SyntheticSection(SHF_ALLOC,
+                       Config->UseAndroidRelrTags ? SHT_ANDROID_RELR : SHT_RELR,
+                       Config->Wordsize, ".relr.dyn") {}
+
 template <class ELFT>
 static void encodeDynamicReloc(typename ELFT::Rela *P,
                                const DynamicReloc &Rel) {
   if (Config->IsRela)
     P->r_addend = Rel.computeAddend();
   P->r_offset = Rel.getOffset();
-  if (Config->EMachine == EM_MIPS && Rel.getInputSec() == InX::MipsGot)
-    // The MIPS GOT section contains dynamic relocations that correspond to TLS
-    // entries. These entries are placed after the global and local sections of
-    // the GOT. At the point when we create these relocations, the size of the
-    // global and local sections is unknown, so the offset that we store in the
-    // TLS entry's DynamicReloc is relative to the start of the TLS section of
-    // the GOT, rather than being relative to the start of the GOT. This line of
-    // code adds the size of the global and local sections to the virtual
-    // address computed by getOffset() in order to adjust it into the TLS
-    // section.
-    P->r_offset += InX::MipsGot->getTlsOffset();
   P->setSymbolAndType(Rel.getSymIndex(), Rel.Type, Config->IsMips64EL);
 }
 
@@ -1521,6 +1728,97 @@
   return RelocData.size() != OldSize;
 }
 
+template <class ELFT> RelrSection<ELFT>::RelrSection() {
+  this->Entsize = Config->Wordsize;
+}
+
+template <class ELFT> bool RelrSection<ELFT>::updateAllocSize() {
+  // This function computes the contents of an SHT_RELR packed relocation
+  // section.
+  //
+  // Proposal for adding SHT_RELR sections to generic-abi is here:
+  //   https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
+  //
+  // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
+  // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
+  //
+  // i.e. start with an address, followed by any number of bitmaps. The address
+  // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
+  // relocations each, at subsequent offsets following the last address entry.
+  //
+  // The bitmap entries must have 1 in the least significant bit. The assumption
+  // here is that an address cannot have 1 in lsb. Odd addresses are not
+  // supported.
+  //
+  // Excluding the least significant bit in the bitmap, each non-zero bit in
+  // the bitmap represents a relocation to be applied to a corresponding machine
+  // word that follows the base address word. The second least significant bit
+  // represents the machine word immediately following the initial address, and
+  // each bit that follows represents the next word, in linear order. As such,
+  // a single bitmap can encode up to 31 relocations in a 32-bit object, and
+  // 63 relocations in a 64-bit object.
+  //
+  // This encoding has a couple of interesting properties:
+  // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
+  //    even means address, odd means bitmap.
+  // 2. Just a simple list of addresses is a valid encoding.
+
+  size_t OldSize = RelrRelocs.size();
+  RelrRelocs.clear();
+
+  // Same as Config->Wordsize but faster because this is a compile-time
+  // constant.
+  const size_t Wordsize = sizeof(typename ELFT::uint);
+
+  // Number of bits to use for the relocation offsets bitmap.
+  // Must be either 63 or 31.
+  const size_t NBits = Wordsize * 8 - 1;
+
+  // Get offsets for all relative relocations and sort them.
+  std::vector<uint64_t> Offsets;
+  for (const RelativeReloc &Rel : Relocs)
+    Offsets.push_back(Rel.getOffset());
+  llvm::sort(Offsets.begin(), Offsets.end());
+
+  // For each leading relocation, find following ones that can be folded
+  // as a bitmap and fold them.
+  for (size_t I = 0, E = Offsets.size(); I < E;) {
+    // Add a leading relocation.
+    RelrRelocs.push_back(Elf_Relr(Offsets[I]));
+    uint64_t Base = Offsets[I] + Wordsize;
+    ++I;
+
+    // Find foldable relocations to construct bitmaps.
+    while (I < E) {
+      uint64_t Bitmap = 0;
+
+      while (I < E) {
+        uint64_t Delta = Offsets[I] - Base;
+
+        // If it is too far, it cannot be folded.
+        if (Delta >= NBits * Wordsize)
+          break;
+
+        // If it is not a multiple of wordsize away, it cannot be folded.
+        if (Delta % Wordsize)
+          break;
+
+        // Fold it.
+        Bitmap |= 1ULL << (Delta / Wordsize);
+        ++I;
+      }
+
+      if (!Bitmap)
+        break;
+
+      RelrRelocs.push_back(Elf_Relr((Bitmap << 1) | 1));
+      Base += NBits * Wordsize;
+    }
+  }
+
+  return RelrRelocs.size() != OldSize;
+}
+
 SymbolTableBaseSection::SymbolTableBaseSection(StringTableSection &StrTabSec)
     : SyntheticSection(StrTabSec.isDynamic() ? (uint64_t)SHF_ALLOC : 0,
                        StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
@@ -1536,12 +1834,12 @@
 static bool sortMipsSymbols(const SymbolTableEntry &L,
                             const SymbolTableEntry &R) {
   // Sort entries related to non-local preemptible symbols by GOT indexes.
-  // All other entries go to the first part of GOT in arbitrary order.
-  bool LIsInLocalGot = !L.Sym->IsInGlobalMipsGot;
-  bool RIsInLocalGot = !R.Sym->IsInGlobalMipsGot;
-  if (LIsInLocalGot || RIsInLocalGot)
-    return !RIsInLocalGot;
-  return L.Sym->GotIndex < R.Sym->GotIndex;
+  // All other entries go to the beginning of a dynsym in arbitrary order.
+  if (L.Sym->isInGot() && R.Sym->isInGot())
+    return L.Sym->GotIndex < R.Sym->GotIndex;
+  if (!L.Sym->isInGot() && !R.Sym->isInGot())
+    return false;
+  return !L.Sym->isInGot();
 }
 
 void SymbolTableBaseSection::finalizeContents() {
@@ -1577,8 +1875,7 @@
 // symbol. That is convenient for purpose of identifying where are local symbols
 // coming from.
 void SymbolTableBaseSection::postThunkContents() {
-  if (this->Type == SHT_DYNSYM)
-    return;
+  assert(this->Type == SHT_SYMTAB);
 
   // Move all local symbols before global symbols.
   auto E = std::stable_partition(
@@ -1588,19 +1885,19 @@
   size_t NumLocals = E - Symbols.begin();
   getParent()->Info = NumLocals + 1;
 
-  // Assign the growing unique ID for each local symbol's file.
-  DenseMap<InputFile *, unsigned> FileIDs;
-  for (auto I = Symbols.begin(); I != E; ++I)
-    FileIDs.insert({I->Sym->File, FileIDs.size()});
+  // We want to group the local symbols by file. For that we rebuild the local
+  // part of the symbols vector. We do not need to care about the STT_FILE
+  // symbols, they are already naturally placed first in each group. That
+  // happens because STT_FILE is always the first symbol in the object and hence
+  // precede all other local symbols we add for a file.
+  MapVector<InputFile *, std::vector<SymbolTableEntry>> Arr;
+  for (const SymbolTableEntry &S : llvm::make_range(Symbols.begin(), E))
+    Arr[S.Sym->File].push_back(S);
 
-  // Sort the local symbols to group them by file. We do not need to care about
-  // the STT_FILE symbols, they are already naturally placed first in each group.
-  // That happens because STT_FILE is always the first symbol in the object and
-  // hence precede all other local symbols we add for a file.
-  std::stable_sort(Symbols.begin(), E,
-                   [&](const SymbolTableEntry &L, const SymbolTableEntry &R) {
-                     return FileIDs[L.Sym->File] < FileIDs[R.Sym->File];
-                   });
+  auto I = Symbols.begin();
+  for (std::pair<InputFile *, std::vector<SymbolTableEntry>> &P : Arr)
+    for (SymbolTableEntry &Entry : P.second)
+      *I++ = Entry;
 }
 
 void SymbolTableBaseSection::addSymbol(Symbol *B) {
@@ -1638,6 +1935,23 @@
   this->Entsize = sizeof(Elf_Sym);
 }
 
+static BssSection *getCommonSec(Symbol *Sym) {
+  if (!Config->DefineCommon)
+    if (auto *D = dyn_cast<Defined>(Sym))
+      return dyn_cast_or_null<BssSection>(D->Section);
+  return nullptr;
+}
+
+static uint32_t getSymSectionIndex(Symbol *Sym) {
+  if (getCommonSec(Sym))
+    return SHN_COMMON;
+  if (!isa<Defined>(Sym) || Sym->NeedsPltAddr)
+    return SHN_UNDEF;
+  if (const OutputSection *OS = Sym->getOutputSection())
+    return OS->SectionIndex >= SHN_LORESERVE ? SHN_XINDEX : OS->SectionIndex;
+  return SHN_ABS;
+}
+
 // Write the internal symbol table contents to the output symbol table.
 template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
   // The first entry is a null entry as per the ELF spec.
@@ -1659,22 +1973,7 @@
     }
 
     ESym->st_name = Ent.StrTabOffset;
-
-    // Set a section index.
-    BssSection *CommonSec = nullptr;
-    if (!Config->DefineCommon)
-      if (auto *D = dyn_cast<Defined>(Sym))
-        CommonSec = dyn_cast_or_null<BssSection>(D->Section);
-    if (CommonSec)
-      ESym->st_shndx = SHN_COMMON;
-    else if (Sym->NeedsPltAddr)
-      ESym->st_shndx = SHN_UNDEF;
-    else if (const OutputSection *OutSec = Sym->getOutputSection())
-      ESym->st_shndx = OutSec->SectionIndex;
-    else if (isa<Defined>(Sym))
-      ESym->st_shndx = SHN_ABS;
-    else
-      ESym->st_shndx = SHN_UNDEF;
+    ESym->st_shndx = getSymSectionIndex(Ent.Sym);
 
     // Copy symbol size if it is a defined symbol. st_size is not significant
     // for undefined symbols, so whether copying it or not is up to us if that's
@@ -1689,7 +1988,7 @@
     // st_value is usually an address of a symbol, but that has a
     // special meaining for uninstantiated common symbols (this can
     // occur if -r is given).
-    if (CommonSec)
+    if (BssSection *CommonSec = getCommonSec(Ent.Sym))
       ESym->st_value = CommonSec->Alignment;
     else
       ESym->st_value = Sym->getVA();
@@ -1729,6 +2028,44 @@
   }
 }
 
+SymtabShndxSection::SymtabShndxSection()
+    : SyntheticSection(0, SHT_SYMTAB_SHNDX, 4, ".symtab_shndxr") {
+  this->Entsize = 4;
+}
+
+void SymtabShndxSection::writeTo(uint8_t *Buf) {
+  // We write an array of 32 bit values, where each value has 1:1 association
+  // with an entry in .symtab. If the corresponding entry contains SHN_XINDEX,
+  // we need to write actual index, otherwise, we must write SHN_UNDEF(0).
+  Buf += 4; // Ignore .symtab[0] entry.
+  for (const SymbolTableEntry &Entry : InX::SymTab->getSymbols()) {
+    if (getSymSectionIndex(Entry.Sym) == SHN_XINDEX)
+      write32(Buf, Entry.Sym->getOutputSection()->SectionIndex);
+    Buf += 4;
+  }
+}
+
+bool SymtabShndxSection::empty() const {
+  // SHT_SYMTAB can hold symbols with section indices values up to
+  // SHN_LORESERVE. If we need more, we want to use extension SHT_SYMTAB_SHNDX
+  // section. Problem is that we reveal the final section indices a bit too
+  // late, and we do not know them here. For simplicity, we just always create
+  // a .symtab_shndxr section when the amount of output sections is huge.
+  size_t Size = 0;
+  for (BaseCommand *Base : Script->SectionCommands)
+    if (isa<OutputSection>(Base))
+      ++Size;
+  return Size < SHN_LORESERVE;
+}
+
+void SymtabShndxSection::finalizeContents() {
+  getParent()->Link = InX::SymTab->getParent()->SectionIndex;
+}
+
+size_t SymtabShndxSection::getSize() const {
+  return InX::SymTab->getNumSymbols() * 4;
+}
+
 // .hash and .gnu.hash sections contain on-disk hash tables that map
 // symbol names to their dynamic symbol table indices. Their purpose
 // is to help the dynamic linker resolve symbols quickly. If ELF files
@@ -2000,19 +2337,51 @@
   return H;
 }
 
-static std::vector<GdbIndexChunk::CuEntry> readCuList(DWARFContext &Dwarf) {
-  std::vector<GdbIndexChunk::CuEntry> Ret;
-  for (std::unique_ptr<DWARFCompileUnit> &Cu : Dwarf.compile_units())
+GdbIndexSection::GdbIndexSection()
+    : SyntheticSection(0, SHT_PROGBITS, 1, ".gdb_index") {}
+
+// Returns the desired size of an on-disk hash table for a .gdb_index section.
+// There's a tradeoff between size and collision rate. We aim 75% utilization.
+size_t GdbIndexSection::computeSymtabSize() const {
+  return std::max<size_t>(NextPowerOf2(Symbols.size() * 4 / 3), 1024);
+}
+
+// Compute the output section size.
+void GdbIndexSection::initOutputSize() {
+  Size = sizeof(GdbIndexHeader) + computeSymtabSize() * 8;
+
+  for (GdbChunk &Chunk : Chunks)
+    Size += Chunk.CompilationUnits.size() * 16 + Chunk.AddressAreas.size() * 20;
+
+  // Add the constant pool size if exists.
+  if (!Symbols.empty()) {
+    GdbSymbol &Sym = Symbols.back();
+    Size += Sym.NameOff + Sym.Name.size() + 1;
+  }
+}
+
+static std::vector<InputSection *> getDebugInfoSections() {
+  std::vector<InputSection *> Ret;
+  for (InputSectionBase *S : InputSections)
+    if (InputSection *IS = dyn_cast<InputSection>(S))
+      if (IS->Name == ".debug_info")
+        Ret.push_back(IS);
+  return Ret;
+}
+
+static std::vector<GdbIndexSection::CuEntry> readCuList(DWARFContext &Dwarf) {
+  std::vector<GdbIndexSection::CuEntry> Ret;
+  for (std::unique_ptr<DWARFUnit> &Cu : Dwarf.compile_units())
     Ret.push_back({Cu->getOffset(), Cu->getLength() + 4});
   return Ret;
 }
 
-static std::vector<GdbIndexChunk::AddressEntry>
+static std::vector<GdbIndexSection::AddressEntry>
 readAddressAreas(DWARFContext &Dwarf, InputSection *Sec) {
-  std::vector<GdbIndexChunk::AddressEntry> Ret;
+  std::vector<GdbIndexSection::AddressEntry> Ret;
 
   uint32_t CuIdx = 0;
-  for (std::unique_ptr<DWARFCompileUnit> &Cu : Dwarf.compile_units()) {
+  for (std::unique_ptr<DWARFUnit> &Cu : Dwarf.compile_units()) {
     DWARFAddressRangesVector Ranges;
     Cu->collectAddressRanges(Ranges);
 
@@ -2033,211 +2402,186 @@
   return Ret;
 }
 
-static std::vector<GdbIndexChunk::NameTypeEntry>
-readPubNamesAndTypes(DWARFContext &Dwarf) {
+static std::vector<GdbIndexSection::NameTypeEntry>
+readPubNamesAndTypes(DWARFContext &Dwarf, uint32_t Idx) {
   StringRef Sec1 = Dwarf.getDWARFObj().getGnuPubNamesSection();
   StringRef Sec2 = Dwarf.getDWARFObj().getGnuPubTypesSection();
 
-  std::vector<GdbIndexChunk::NameTypeEntry> Ret;
+  std::vector<GdbIndexSection::NameTypeEntry> Ret;
   for (StringRef Sec : {Sec1, Sec2}) {
     DWARFDebugPubTable Table(Sec, Config->IsLE, true);
-    for (const DWARFDebugPubTable::Set &Set : Table.getData()) {
-      for (const DWARFDebugPubTable::Entry &Ent : Set.Entries) {
-        CachedHashStringRef S(Ent.Name, computeGdbHash(Ent.Name));
-        Ret.push_back({S, Ent.Descriptor.toBits()});
+    for (const DWARFDebugPubTable::Set &Set : Table.getData())
+      for (const DWARFDebugPubTable::Entry &Ent : Set.Entries)
+        Ret.push_back({{Ent.Name, computeGdbHash(Ent.Name)},
+                       (Ent.Descriptor.toBits() << 24) | Idx});
+  }
+  return Ret;
+}
+
+// Create a list of symbols from a given list of symbol names and types
+// by uniquifying them by name.
+static std::vector<GdbIndexSection::GdbSymbol>
+createSymbols(ArrayRef<std::vector<GdbIndexSection::NameTypeEntry>> NameTypes) {
+  typedef GdbIndexSection::GdbSymbol GdbSymbol;
+  typedef GdbIndexSection::NameTypeEntry NameTypeEntry;
+
+  // The number of symbols we will handle in this function is of the order
+  // of millions for very large executables, so we use multi-threading to
+  // speed it up.
+  size_t NumShards = 32;
+  size_t Concurrency = 1;
+  if (ThreadsEnabled)
+    Concurrency =
+        std::min<size_t>(PowerOf2Floor(hardware_concurrency()), NumShards);
+
+  // A sharded map to uniquify symbols by name.
+  std::vector<DenseMap<CachedHashStringRef, size_t>> Map(NumShards);
+  size_t Shift = 32 - countTrailingZeros(NumShards);
+
+  // Instantiate GdbSymbols while uniqufying them by name.
+  std::vector<std::vector<GdbSymbol>> Symbols(NumShards);
+  parallelForEachN(0, Concurrency, [&](size_t ThreadId) {
+    for (ArrayRef<NameTypeEntry> Entries : NameTypes) {
+      for (const NameTypeEntry &Ent : Entries) {
+        size_t ShardId = Ent.Name.hash() >> Shift;
+        if ((ShardId & (Concurrency - 1)) != ThreadId)
+          continue;
+
+        size_t &Idx = Map[ShardId][Ent.Name];
+        if (Idx) {
+          Symbols[ShardId][Idx - 1].CuVector.push_back(Ent.Type);
+          continue;
+        }
+
+        Idx = Symbols[ShardId].size() + 1;
+        Symbols[ShardId].push_back({Ent.Name, {Ent.Type}, 0, 0});
       }
     }
-  }
-  return Ret;
-}
-
-static std::vector<InputSection *> getDebugInfoSections() {
-  std::vector<InputSection *> Ret;
-  for (InputSectionBase *S : InputSections)
-    if (InputSection *IS = dyn_cast<InputSection>(S))
-      if (IS->Name == ".debug_info")
-        Ret.push_back(IS);
-  return Ret;
-}
-
-void GdbIndexSection::fixCuIndex() {
-  uint32_t Idx = 0;
-  for (GdbIndexChunk &Chunk : Chunks) {
-    for (GdbIndexChunk::AddressEntry &Ent : Chunk.AddressAreas)
-      Ent.CuIndex += Idx;
-    Idx += Chunk.CompilationUnits.size();
-  }
-}
-
-std::vector<std::vector<uint32_t>> GdbIndexSection::createCuVectors() {
-  std::vector<std::vector<uint32_t>> Ret;
-  uint32_t Idx = 0;
-  uint32_t Off = 0;
-
-  for (GdbIndexChunk &Chunk : Chunks) {
-    for (GdbIndexChunk::NameTypeEntry &Ent : Chunk.NamesAndTypes) {
-      GdbSymbol *&Sym = Symbols[Ent.Name];
-      if (!Sym) {
-        Sym = make<GdbSymbol>(GdbSymbol{Ent.Name.hash(), Off, Ret.size()});
-        Off += Ent.Name.size() + 1;
-        Ret.push_back({});
-      }
-
-      // gcc 5.4.1 produces a buggy .debug_gnu_pubnames that contains
-      // duplicate entries, so we want to dedup them.
-      std::vector<uint32_t> &Vec = Ret[Sym->CuVectorIndex];
-      uint32_t Val = (Ent.Type << 24) | Idx;
-      if (Vec.empty() || Vec.back() != Val)
-        Vec.push_back(Val);
-    }
-    Idx += Chunk.CompilationUnits.size();
-  }
-
-  StringPoolSize = Off;
-  return Ret;
-}
-
-template <class ELFT> GdbIndexSection *elf::createGdbIndex() {
-  // Gather debug info to create a .gdb_index section.
-  std::vector<InputSection *> Sections = getDebugInfoSections();
-  std::vector<GdbIndexChunk> Chunks(Sections.size());
-
-  parallelForEachN(0, Chunks.size(), [&](size_t I) {
-    ObjFile<ELFT> *File = Sections[I]->getFile<ELFT>();
-    DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(File));
-
-    Chunks[I].DebugInfoSec = Sections[I];
-    Chunks[I].CompilationUnits = readCuList(Dwarf);
-    Chunks[I].AddressAreas = readAddressAreas(Dwarf, Sections[I]);
-    Chunks[I].NamesAndTypes = readPubNamesAndTypes(Dwarf);
   });
 
+  size_t NumSymbols = 0;
+  for (ArrayRef<GdbSymbol> V : Symbols)
+    NumSymbols += V.size();
+
+  // The return type is a flattened vector, so we'll copy each vector
+  // contents to Ret.
+  std::vector<GdbSymbol> Ret;
+  Ret.reserve(NumSymbols);
+  for (std::vector<GdbSymbol> &Vec : Symbols)
+    for (GdbSymbol &Sym : Vec)
+      Ret.push_back(std::move(Sym));
+
+  // CU vectors and symbol names are adjacent in the output file.
+  // We can compute their offsets in the output file now.
+  size_t Off = 0;
+  for (GdbSymbol &Sym : Ret) {
+    Sym.CuVectorOff = Off;
+    Off += (Sym.CuVector.size() + 1) * 4;
+  }
+  for (GdbSymbol &Sym : Ret) {
+    Sym.NameOff = Off;
+    Off += Sym.Name.size() + 1;
+  }
+
+  return Ret;
+}
+
+// Returns a newly-created .gdb_index section.
+template <class ELFT> GdbIndexSection *GdbIndexSection::create() {
+  std::vector<InputSection *> Sections = getDebugInfoSections();
+
   // .debug_gnu_pub{names,types} are useless in executables.
   // They are present in input object files solely for creating
-  // a .gdb_index. So we can remove it from the output.
+  // a .gdb_index. So we can remove them from the output.
   for (InputSectionBase *S : InputSections)
     if (S->Name == ".debug_gnu_pubnames" || S->Name == ".debug_gnu_pubtypes")
       S->Live = false;
 
-  // Create a .gdb_index and returns it.
-  return make<GdbIndexSection>(std::move(Chunks));
-}
+  std::vector<GdbChunk> Chunks(Sections.size());
+  std::vector<std::vector<NameTypeEntry>> NameTypes(Sections.size());
 
-static size_t getCuSize(ArrayRef<GdbIndexChunk> Arr) {
-  size_t Ret = 0;
-  for (const GdbIndexChunk &D : Arr)
-    Ret += D.CompilationUnits.size();
+  parallelForEachN(0, Sections.size(), [&](size_t I) {
+    ObjFile<ELFT> *File = Sections[I]->getFile<ELFT>();
+    DWARFContext Dwarf(make_unique<LLDDwarfObj<ELFT>>(File));
+
+    Chunks[I].Sec = Sections[I];
+    Chunks[I].CompilationUnits = readCuList(Dwarf);
+    Chunks[I].AddressAreas = readAddressAreas(Dwarf, Sections[I]);
+    NameTypes[I] = readPubNamesAndTypes(Dwarf, I);
+  });
+
+  auto *Ret = make<GdbIndexSection>();
+  Ret->Chunks = std::move(Chunks);
+  Ret->Symbols = createSymbols(NameTypes);
+  Ret->initOutputSize();
   return Ret;
 }
 
-static size_t getAddressAreaSize(ArrayRef<GdbIndexChunk> Arr) {
-  size_t Ret = 0;
-  for (const GdbIndexChunk &D : Arr)
-    Ret += D.AddressAreas.size();
-  return Ret;
-}
-
-std::vector<GdbSymbol *> GdbIndexSection::createGdbSymtab() {
-  uint32_t Size = NextPowerOf2(Symbols.size() * 4 / 3);
-  if (Size < 1024)
-    Size = 1024;
-
-  uint32_t Mask = Size - 1;
-  std::vector<GdbSymbol *> Ret(Size);
-
-  for (auto &KV : Symbols) {
-    GdbSymbol *Sym = KV.second;
-    uint32_t I = Sym->NameHash & Mask;
-    uint32_t Step = ((Sym->NameHash * 17) & Mask) | 1;
-
-    while (Ret[I])
-      I = (I + Step) & Mask;
-    Ret[I] = Sym;
-  }
-  return Ret;
-}
-
-GdbIndexSection::GdbIndexSection(std::vector<GdbIndexChunk> &&C)
-    : SyntheticSection(0, SHT_PROGBITS, 1, ".gdb_index"), Chunks(std::move(C)) {
-  fixCuIndex();
-  CuVectors = createCuVectors();
-  GdbSymtab = createGdbSymtab();
-
-  // Compute offsets early to know the section size.
-  // Each chunk size needs to be in sync with what we write in writeTo.
-  CuTypesOffset = CuListOffset + getCuSize(Chunks) * 16;
-  SymtabOffset = CuTypesOffset + getAddressAreaSize(Chunks) * 20;
-  ConstantPoolOffset = SymtabOffset + GdbSymtab.size() * 8;
-
-  size_t Off = 0;
-  for (ArrayRef<uint32_t> Vec : CuVectors) {
-    CuVectorOffsets.push_back(Off);
-    Off += (Vec.size() + 1) * 4;
-  }
-  StringPoolOffset = ConstantPoolOffset + Off;
-}
-
-size_t GdbIndexSection::getSize() const {
-  return StringPoolOffset + StringPoolSize;
-}
-
 void GdbIndexSection::writeTo(uint8_t *Buf) {
-  // Write the section header.
-  write32le(Buf, 7);
-  write32le(Buf + 4, CuListOffset);
-  write32le(Buf + 8, CuTypesOffset);
-  write32le(Buf + 12, CuTypesOffset);
-  write32le(Buf + 16, SymtabOffset);
-  write32le(Buf + 20, ConstantPoolOffset);
-  Buf += 24;
+  // Write the header.
+  auto *Hdr = reinterpret_cast<GdbIndexHeader *>(Buf);
+  uint8_t *Start = Buf;
+  Hdr->Version = 7;
+  Buf += sizeof(*Hdr);
 
   // Write the CU list.
-  for (GdbIndexChunk &D : Chunks) {
-    for (GdbIndexChunk::CuEntry &Cu : D.CompilationUnits) {
-      write64le(Buf, D.DebugInfoSec->OutSecOff + Cu.CuOffset);
+  Hdr->CuListOff = Buf - Start;
+  for (GdbChunk &Chunk : Chunks) {
+    for (CuEntry &Cu : Chunk.CompilationUnits) {
+      write64le(Buf, Chunk.Sec->OutSecOff + Cu.CuOffset);
       write64le(Buf + 8, Cu.CuLength);
       Buf += 16;
     }
   }
 
   // Write the address area.
-  for (GdbIndexChunk &D : Chunks) {
-    for (GdbIndexChunk::AddressEntry &E : D.AddressAreas) {
+  Hdr->CuTypesOff = Buf - Start;
+  Hdr->AddressAreaOff = Buf - Start;
+  uint32_t CuOff = 0;
+  for (GdbChunk &Chunk : Chunks) {
+    for (AddressEntry &E : Chunk.AddressAreas) {
       uint64_t BaseAddr = E.Section->getVA(0);
       write64le(Buf, BaseAddr + E.LowAddress);
       write64le(Buf + 8, BaseAddr + E.HighAddress);
-      write32le(Buf + 16, E.CuIndex);
+      write32le(Buf + 16, E.CuIndex + CuOff);
       Buf += 20;
     }
+    CuOff += Chunk.CompilationUnits.size();
   }
 
-  // Write the symbol table.
-  for (GdbSymbol *Sym : GdbSymtab) {
-    if (Sym) {
-      write32le(Buf, Sym->NameOffset + StringPoolOffset - ConstantPoolOffset);
-      write32le(Buf + 4, CuVectorOffsets[Sym->CuVectorIndex]);
-    }
-    Buf += 8;
+  // Write the on-disk open-addressing hash table containing symbols.
+  Hdr->SymtabOff = Buf - Start;
+  size_t SymtabSize = computeSymtabSize();
+  uint32_t Mask = SymtabSize - 1;
+
+  for (GdbSymbol &Sym : Symbols) {
+    uint32_t H = Sym.Name.hash();
+    uint32_t I = H & Mask;
+    uint32_t Step = ((H * 17) & Mask) | 1;
+
+    while (read32le(Buf + I * 8))
+      I = (I + Step) & Mask;
+
+    write32le(Buf + I * 8, Sym.NameOff);
+    write32le(Buf + I * 8 + 4, Sym.CuVectorOff);
   }
 
+  Buf += SymtabSize * 8;
+
+  // Write the string pool.
+  Hdr->ConstantPoolOff = Buf - Start;
+  for (GdbSymbol &Sym : Symbols)
+    memcpy(Buf + Sym.NameOff, Sym.Name.data(), Sym.Name.size());
+
   // Write the CU vectors.
-  for (ArrayRef<uint32_t> Vec : CuVectors) {
-    write32le(Buf, Vec.size());
+  for (GdbSymbol &Sym : Symbols) {
+    write32le(Buf, Sym.CuVector.size());
     Buf += 4;
-    for (uint32_t Val : Vec) {
+    for (uint32_t Val : Sym.CuVector) {
       write32le(Buf, Val);
       Buf += 4;
     }
   }
-
-  // Write the string pool.
-  for (auto &KV : Symbols) {
-    CachedHashStringRef S = KV.first;
-    GdbSymbol *Sym = KV.second;
-    size_t Off = Sym->NameOffset;
-    memcpy(Buf + Off, S.val().data(), S.size());
-    Buf[Off + S.size()] = '\0';
-  }
 }
 
 bool GdbIndexSection::empty() const { return !Out::DebugInfo; }
@@ -2254,14 +2598,6 @@
 
   std::vector<FdeData> Fdes = InX::EhFrame->getFdeData();
 
-  // Sort the FDE list by their PC and uniqueify. Usually there is only
-  // one FDE for a PC (i.e. function), but if ICF merges two functions
-  // into one, there can be more than one FDEs pointing to the address.
-  auto Less = [](const FdeData &A, const FdeData &B) { return A.Pc < B.Pc; };
-  std::stable_sort(Fdes.begin(), Fdes.end(), Less);
-  auto Eq = [](const FdeData &A, const FdeData &B) { return A.Pc == B.Pc; };
-  Fdes.erase(std::unique(Fdes.begin(), Fdes.end(), Eq), Fdes.end());
-
   Buf[0] = 1;
   Buf[1] = DW_EH_PE_pcrel | DW_EH_PE_sdata4;
   Buf[2] = DW_EH_PE_udata4;
@@ -2270,10 +2606,9 @@
   write32(Buf + 8, Fdes.size());
   Buf += 12;
 
-  uint64_t VA = this->getVA();
   for (FdeData &Fde : Fdes) {
-    write32(Buf, Fde.Pc - VA);
-    write32(Buf + 4, Fde.FdeVA - VA);
+    write32(Buf, Fde.PcRel);
+    write32(Buf + 4, Fde.FdeVARel);
     Buf += 8;
   }
 }
@@ -2661,6 +2996,10 @@
   return true;
 }
 
+bool ARMExidxSentinelSection::classof(const SectionBase *D) {
+  return D->kind() == InputSectionBase::Synthetic && D->Type == SHT_ARM_EXIDX;
+}
+
 ThunkSection::ThunkSection(OutputSection *OS, uint64_t Off)
     : SyntheticSection(SHF_ALLOC | SHF_EXECINSTR, SHT_PROGBITS,
                        Config->Wordsize, ".text.thunk") {
@@ -2720,16 +3059,18 @@
 PltSection *InX::Plt;
 PltSection *InX::Iplt;
 RelocationBaseSection *InX::RelaDyn;
+RelrBaseSection *InX::RelrDyn;
 RelocationBaseSection *InX::RelaPlt;
 RelocationBaseSection *InX::RelaIplt;
 StringTableSection *InX::ShStrTab;
 StringTableSection *InX::StrTab;
 SymbolTableBaseSection *InX::SymTab;
+SymtabShndxSection *InX::SymTabShndx;
 
-template GdbIndexSection *elf::createGdbIndex<ELF32LE>();
-template GdbIndexSection *elf::createGdbIndex<ELF32BE>();
-template GdbIndexSection *elf::createGdbIndex<ELF64LE>();
-template GdbIndexSection *elf::createGdbIndex<ELF64BE>();
+template GdbIndexSection *GdbIndexSection::create<ELF32LE>();
+template GdbIndexSection *GdbIndexSection::create<ELF32BE>();
+template GdbIndexSection *GdbIndexSection::create<ELF64LE>();
+template GdbIndexSection *GdbIndexSection::create<ELF64BE>();
 
 template void elf::splitSections<ELF32LE>();
 template void elf::splitSections<ELF32BE>();
@@ -2746,6 +3087,11 @@
 template void PltSection::addEntry<ELF64LE>(Symbol &Sym);
 template void PltSection::addEntry<ELF64BE>(Symbol &Sym);
 
+template void MipsGotSection::build<ELF32LE>();
+template void MipsGotSection::build<ELF32BE>();
+template void MipsGotSection::build<ELF64LE>();
+template void MipsGotSection::build<ELF64BE>();
+
 template class elf::MipsAbiFlagsSection<ELF32LE>;
 template class elf::MipsAbiFlagsSection<ELF32BE>;
 template class elf::MipsAbiFlagsSection<ELF64LE>;
@@ -2776,6 +3122,11 @@
 template class elf::AndroidPackedRelocationSection<ELF64LE>;
 template class elf::AndroidPackedRelocationSection<ELF64BE>;
 
+template class elf::RelrSection<ELF32LE>;
+template class elf::RelrSection<ELF32BE>;
+template class elf::RelrSection<ELF64LE>;
+template class elf::RelrSection<ELF64BE>;
+
 template class elf::SymbolTableSection<ELF32LE>;
 template class elf::SymbolTableSection<ELF32BE>;
 template class elf::SymbolTableSection<ELF64LE>;
diff --git a/ELF/SyntheticSections.h b/ELF/SyntheticSections.h
index fc5ffa3..a780c66 100644
--- a/ELF/SyntheticSections.h
+++ b/ELF/SyntheticSections.h
@@ -26,6 +26,7 @@
 #include "InputSection.h"
 #include "llvm/ADT/MapVector.h"
 #include "llvm/MC/StringTableBuilder.h"
+#include "llvm/Support/Endian.h"
 #include <functional>
 
 namespace lld {
@@ -78,8 +79,8 @@
   size_t NumFdes = 0;
 
   struct FdeData {
-    uint32_t Pc;
-    uint32_t FdeVA;
+    uint32_t PcRel;
+    uint32_t FdeVARel;
   };
 
   std::vector<FdeData> getFdeData() const;
@@ -178,12 +179,21 @@
   bool updateAllocSize() override;
   void finalizeContents() override;
   bool empty() const override;
-  void addEntry(Symbol &Sym, int64_t Addend, RelExpr Expr);
-  bool addDynTlsEntry(Symbol &Sym);
-  bool addTlsIndex();
-  uint64_t getPageEntryOffset(const Symbol &B, int64_t Addend) const;
-  uint64_t getSymEntryOffset(const Symbol &B, int64_t Addend) const;
-  uint64_t getGlobalDynOffset(const Symbol &B) const;
+
+  // Join separate GOTs built for each input file to generate
+  // primary and optional multiple secondary GOTs.
+  template <class ELFT> void build();
+
+  void addEntry(InputFile &File, Symbol &Sym, int64_t Addend, RelExpr Expr);
+  void addDynTlsEntry(InputFile &File, Symbol &Sym);
+  void addTlsIndex(InputFile &File);
+
+  uint64_t getPageEntryOffset(const InputFile *F, const Symbol &S,
+                              int64_t Addend) const;
+  uint64_t getSymEntryOffset(const InputFile *F, const Symbol &S,
+                             int64_t Addend) const;
+  uint64_t getGlobalDynOffset(const InputFile *F, const Symbol &S) const;
+  uint64_t getTlsIndexOffset(const InputFile *F) const;
 
   // Returns the symbol which corresponds to the first entry of the global part
   // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
@@ -195,13 +205,8 @@
   // the number of reserved entries.
   unsigned getLocalEntriesNum() const;
 
-  // Returns offset of TLS part of the MIPS GOT table. This part goes
-  // after 'local' and 'global' entries.
-  uint64_t getTlsOffset() const;
-
-  uint32_t getTlsIndexOff() const { return TlsIndexOff; }
-
-  uint64_t getGp() const;
+  // Return _gp value for primary GOT (nullptr) or particular input file.
+  uint64_t getGp(const InputFile *F = nullptr) const;
 
 private:
   // MIPS GOT consists of three parts: local, global and tls. Each part
@@ -240,32 +245,110 @@
   //   addressing, but MIPS ABI requires that these entries be present in GOT.
   // TLS entries:
   //   Entries created by TLS relocations.
+  //
+  // If the sum of local, global and tls entries is less than 64K only single
+  // got is enough. Otherwise, multi-got is created. Series of primary and
+  // multiple secondary GOTs have the following layout:
+  // - Primary GOT
+  //     Header
+  //     Local entries
+  //     Global entries
+  //     Relocation only entries
+  //     TLS entries
+  //
+  // - Secondary GOT
+  //     Local entries
+  //     Global entries
+  //     TLS entries
+  // ...
+  //
+  // All GOT entries required by relocations from a single input file entirely
+  // belong to either primary or one of secondary GOTs. To reference GOT entries
+  // each GOT has its own _gp value points to the "middle" of the GOT.
+  // In the code this value loaded to the register which is used for GOT access.
+  //
+  // MIPS 32 function's prologue:
+  //   lui     v0,0x0
+  //   0: R_MIPS_HI16  _gp_disp
+  //   addiu   v0,v0,0
+  //   4: R_MIPS_LO16  _gp_disp
+  //
+  // MIPS 64:
+  //   lui     at,0x0
+  //   14: R_MIPS_GPREL16  main
+  //
+  // Dynamic linker does not know anything about secondary GOTs and cannot
+  // use a regular MIPS mechanism for GOT entries initialization. So we have
+  // to use an approach accepted by other architectures and create dynamic
+  // relocations R_MIPS_REL32 to initialize global entries (and local in case
+  // of PIC code) in secondary GOTs. But ironically MIPS dynamic linker
+  // requires GOT entries and correspondingly ordered dynamic symbol table
+  // entries to deal with dynamic relocations. To handle this problem
+  // relocation-only section in the primary GOT contains entries for all
+  // symbols referenced in global parts of secondary GOTs. Although the sum
+  // of local and normal global entries of the primary got should be less
+  // than 64K, the size of the primary got (including relocation-only entries
+  // can be greater than 64K, because parts of the primary got that overflow
+  // the 64K limit are used only by the dynamic linker at dynamic link-time
+  // and not by 16-bit gp-relative addressing at run-time.
+  //
+  // For complete multi-GOT description see the following link
+  // https://dmz-portal.mips.com/wiki/MIPS_Multi_GOT
 
   // Number of "Header" entries.
   static const unsigned HeaderEntriesNum = 2;
-  // Number of allocated "Page" entries.
-  uint32_t PageEntriesNum = 0;
-  // Map output sections referenced by MIPS GOT relocations
-  // to the first index of "Page" entries allocated for this section.
-  llvm::SmallMapVector<const OutputSection *, size_t, 16> PageIndexMap;
 
-  typedef std::pair<const Symbol *, uint64_t> GotEntry;
-  typedef std::vector<GotEntry> GotEntries;
-  // Map from Symbol-Addend pair to the GOT index.
-  llvm::DenseMap<GotEntry, size_t> EntryIndexMap;
-  // Local entries (16-bit access).
-  GotEntries LocalEntries;
-  // Local entries (32-bit access).
-  GotEntries LocalEntries32;
-
-  // Normal and reloc-only global entries.
-  GotEntries GlobalEntries;
-
-  // TLS entries.
-  std::vector<const Symbol *> TlsEntries;
-
-  uint32_t TlsIndexOff = -1;
   uint64_t Size = 0;
+
+  size_t LocalEntriesNum = 0;
+
+  // Symbol and addend.
+  typedef std::pair<Symbol *, int64_t> GotEntry;
+
+  struct FileGot {
+    InputFile *File = nullptr;
+    size_t StartIndex = 0;
+
+    struct PageBlock {
+      size_t FirstIndex = 0;
+      size_t Count = 0;
+    };
+
+    // Map output sections referenced by MIPS GOT relocations
+    // to the description (index/count) "page" entries allocated
+    // for this section.
+    llvm::SmallMapVector<const OutputSection *, PageBlock, 16> PagesMap;
+    // Maps from Symbol+Addend pair or just Symbol to the GOT entry index.
+    llvm::MapVector<GotEntry, size_t> Local16;
+    llvm::MapVector<GotEntry, size_t> Local32;
+    llvm::MapVector<Symbol *, size_t> Global;
+    llvm::MapVector<Symbol *, size_t> Relocs;
+    llvm::MapVector<Symbol *, size_t> Tls;
+    // Set of symbols referenced by dynamic TLS relocations.
+    llvm::MapVector<Symbol *, size_t> DynTlsSymbols;
+
+    // Total number of all entries.
+    size_t getEntriesNum() const;
+    // Number of "page" entries.
+    size_t getPageEntriesNum() const;
+    // Number of entries require 16-bit index to access.
+    size_t getIndexedEntriesNum() const;
+
+    bool isOverflow() const;
+  };
+
+  // Container of GOT created for each input file.
+  // After building a final series of GOTs this container
+  // holds primary and secondary GOT's.
+  std::vector<FileGot> Gots;
+
+  // Return (and create if necessary) `FileGot`.
+  FileGot &getGot(InputFile &F);
+
+  // Try to merge two GOTs. In case of success the `Dst` contains
+  // result of merging and the function returns true. In case of
+  // ovwerflow the `Dst` is unchanged and the function returns false.
+  bool tryMergeGots(FileGot & Dst, FileGot & Src, bool IsPrimary);
 };
 
 class GotPltSection final : public SyntheticSection {
@@ -318,7 +401,15 @@
   DynamicReloc(RelType Type, const InputSectionBase *InputSec,
                uint64_t OffsetInSec, bool UseSymVA, Symbol *Sym, int64_t Addend)
       : Type(Type), Sym(Sym), InputSec(InputSec), OffsetInSec(OffsetInSec),
-        UseSymVA(UseSymVA), Addend(Addend) {}
+        UseSymVA(UseSymVA), Addend(Addend), OutputSec(nullptr) {}
+  // This constructor records dynamic relocation settings used by MIPS
+  // multi-GOT implementation. It's to relocate addresses of 64kb pages
+  // lie inside the output section.
+  DynamicReloc(RelType Type, const InputSectionBase *InputSec,
+               uint64_t OffsetInSec, const OutputSection *OutputSec,
+               int64_t Addend)
+      : Type(Type), Sym(nullptr), InputSec(InputSec), OffsetInSec(OffsetInSec),
+        UseSymVA(false), Addend(Addend), OutputSec(OutputSec) {}
 
   uint64_t getOffset() const;
   uint32_t getSymIndex() const;
@@ -341,12 +432,14 @@
   // plus the original addend as the final relocation addend.
   bool UseSymVA;
   int64_t Addend;
+  const OutputSection *OutputSec;
 };
 
 template <class ELFT> class DynamicSection final : public SyntheticSection {
   typedef typename ELFT::Dyn Elf_Dyn;
   typedef typename ELFT::Rel Elf_Rel;
   typedef typename ELFT::Rela Elf_Rela;
+  typedef typename ELFT::Relr Elf_Relr;
   typedef typename ELFT::Shdr Elf_Shdr;
   typedef typename ELFT::Sym Elf_Sym;
 
@@ -426,6 +519,39 @@
   SmallVector<char, 0> RelocData;
 };
 
+struct RelativeReloc {
+  uint64_t getOffset() const { return InputSec->getVA(OffsetInSec); }
+
+  const InputSectionBase *InputSec;
+  uint64_t OffsetInSec;
+};
+
+class RelrBaseSection : public SyntheticSection {
+public:
+  RelrBaseSection();
+  std::vector<RelativeReloc> Relocs;
+};
+
+// RelrSection is used to encode offsets for relative relocations.
+// Proposal for adding SHT_RELR sections to generic-abi is here:
+//   https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
+// For more details, see the comment in RelrSection::updateAllocSize().
+template <class ELFT> class RelrSection final : public RelrBaseSection {
+  typedef typename ELFT::Relr Elf_Relr;
+
+public:
+  RelrSection();
+
+  bool updateAllocSize() override;
+  size_t getSize() const override { return RelrRelocs.size() * this->Entsize; }
+  void writeTo(uint8_t *Buf) override {
+    memcpy(Buf, RelrRelocs.data(), getSize());
+  }
+
+private:
+  std::vector<Elf_Relr> RelrRelocs;
+};
+
 struct SymbolTableEntry {
   Symbol *Sym;
   size_t StrTabOffset;
@@ -462,6 +588,16 @@
   void writeTo(uint8_t *Buf) override;
 };
 
+class SymtabShndxSection final : public SyntheticSection {
+public:
+  SymtabShndxSection();
+
+  void writeTo(uint8_t *Buf) override;
+  size_t getSize() const override;
+  bool empty() const override;
+  void finalizeContents() override;
+};
+
 // Outputs GNU Hash section. For detailed explanation see:
 // https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
 class GnuHashTableSection final : public SyntheticSection {
@@ -526,9 +662,8 @@
   bool IsIplt;
 };
 
-// GdbIndexChunk is created for each .debug_info section and contains
-// information to create a part of .gdb_index for a given input section.
-struct GdbIndexChunk {
+class GdbIndexSection final : public SyntheticSection {
+public:
   struct AddressEntry {
     InputSection *Section;
     uint64_t LowAddress;
@@ -543,59 +678,51 @@
 
   struct NameTypeEntry {
     llvm::CachedHashStringRef Name;
-    uint8_t Type;
+    uint32_t Type;
   };
 
-  InputSection *DebugInfoSec;
-  std::vector<AddressEntry> AddressAreas;
-  std::vector<CuEntry> CompilationUnits;
-  std::vector<NameTypeEntry> NamesAndTypes;
-};
+  struct GdbChunk {
+    InputSection *Sec;
+    std::vector<AddressEntry> AddressAreas;
+    std::vector<CuEntry> CompilationUnits;
+  };
 
-// The symbol type for the .gdb_index section.
-struct GdbSymbol {
-  uint32_t NameHash;
-  size_t NameOffset;
-  size_t CuVectorIndex;
-};
+  struct GdbSymbol {
+    llvm::CachedHashStringRef Name;
+    std::vector<uint32_t> CuVector;
+    uint32_t NameOff;
+    uint32_t CuVectorOff;
+  };
 
-class GdbIndexSection final : public SyntheticSection {
-public:
-  GdbIndexSection(std::vector<GdbIndexChunk> &&Chunks);
+  GdbIndexSection();
+  template <typename ELFT> static GdbIndexSection *create();
   void writeTo(uint8_t *Buf) override;
-  size_t getSize() const override;
+  size_t getSize() const override { return Size; }
   bool empty() const override;
 
 private:
-  void fixCuIndex();
-  std::vector<std::vector<uint32_t>> createCuVectors();
-  std::vector<GdbSymbol *> createGdbSymtab();
+  struct GdbIndexHeader {
+    llvm::support::ulittle32_t Version;
+    llvm::support::ulittle32_t CuListOff;
+    llvm::support::ulittle32_t CuTypesOff;
+    llvm::support::ulittle32_t AddressAreaOff;
+    llvm::support::ulittle32_t SymtabOff;
+    llvm::support::ulittle32_t ConstantPoolOff;
+  };
+
+  void initOutputSize();
+  size_t computeSymtabSize() const;
+
+  // Each chunk contains information gathered from debug sections of a
+  // single object file.
+  std::vector<GdbChunk> Chunks;
 
   // A symbol table for this .gdb_index section.
-  std::vector<GdbSymbol *> GdbSymtab;
+  std::vector<GdbSymbol> Symbols;
 
-  // CU vector is a part of constant pool area of section.
-  std::vector<std::vector<uint32_t>> CuVectors;
-
-  // Symbol table contents.
-  llvm::DenseMap<llvm::CachedHashStringRef, GdbSymbol *> Symbols;
-
-  // Each chunk contains information gathered from a debug sections of single
-  // object and used to build different areas of gdb index.
-  std::vector<GdbIndexChunk> Chunks;
-
-  static constexpr uint32_t CuListOffset = 24;
-  uint32_t CuTypesOffset;
-  uint32_t SymtabOffset;
-  uint32_t ConstantPoolOffset;
-  uint32_t StringPoolOffset;
-  uint32_t StringPoolSize;
-
-  std::vector<size_t> CuVectorOffsets;
+  size_t Size;
 };
 
-template <class ELFT> GdbIndexSection *createGdbIndex();
-
 // --eh-frame-hdr option tells linker to construct a header for all the
 // .eh_frame sections. This header is placed to a section named .eh_frame_hdr
 // and also to a PT_GNU_EH_FRAME segment.
@@ -807,6 +934,8 @@
   void writeTo(uint8_t *Buf) override;
   bool empty() const override;
 
+  static bool classof(const SectionBase *D);
+
   // The last section referenced by a regular .ARM.exidx section.
   // It is found and filled in Writer<ELFT>::resolveShfLinkOrder().
   // The sentinel points at the end of that section.
@@ -867,11 +996,13 @@
   static PltSection *Plt;
   static PltSection *Iplt;
   static RelocationBaseSection *RelaDyn;
+  static RelrBaseSection *RelrDyn;
   static RelocationBaseSection *RelaPlt;
   static RelocationBaseSection *RelaIplt;
   static StringTableSection *ShStrTab;
   static StringTableSection *StrTab;
   static SymbolTableBaseSection *SymTab;
+  static SymtabShndxSection* SymTabShndx;
 };
 
 template <class ELFT> struct In {
diff --git a/ELF/Target.cpp b/ELF/Target.cpp
index c19cb5f..0c8c940 100644
--- a/ELF/Target.cpp
+++ b/ELF/Target.cpp
@@ -60,6 +60,8 @@
     return getARMTargetInfo();
   case EM_AVR:
     return getAVRTargetInfo();
+  case EM_HEXAGON:
+    return getHexagonTargetInfo();
   case EM_MIPS:
     switch (Config->EKind) {
     case ELF32LEKind:
@@ -77,6 +79,8 @@
     return getPPCTargetInfo();
   case EM_PPC64:
     return getPPC64TargetInfo();
+  case EM_RISCV:
+    return getRISCVTargetInfo();
   case EM_SPARCV9:
     return getSPARCV9TargetInfo();
   case EM_X86_64:
@@ -89,8 +93,8 @@
 
 template <class ELFT> static ErrorPlace getErrPlace(const uint8_t *Loc) {
   for (InputSectionBase *D : InputSections) {
-    auto *IS = dyn_cast<InputSection>(D);
-    if (!IS || !IS->getParent())
+    auto *IS = cast<InputSection>(D);
+    if (!IS->getParent())
       continue;
 
     uint8_t *ISLoc = IS->getParent()->Loc + IS->OutSecOff;
@@ -128,6 +132,12 @@
   return false;
 }
 
+bool TargetInfo::adjustPrologueForCrossSplitStack(uint8_t *Loc,
+                                                  uint8_t *End) const {
+  llvm_unreachable("Target doesn't support split stacks.");
+}
+
+
 bool TargetInfo::inBranchRange(RelType Type, uint64_t Src, uint64_t Dst) const {
   return true;
 }
diff --git a/ELF/Target.h b/ELF/Target.h
index c7d3906..b897095 100644
--- a/ELF/Target.h
+++ b/ELF/Target.h
@@ -13,6 +13,7 @@
 #include "InputSection.h"
 #include "lld/Common/ErrorHandler.h"
 #include "llvm/Object/ELF.h"
+#include "llvm/Support/MathExtras.h"
 
 namespace lld {
 std::string toString(elf::RelType Type);
@@ -59,6 +60,13 @@
   virtual bool needsThunk(RelExpr Expr, RelType RelocType,
                           const InputFile *File, uint64_t BranchAddr,
                           const Symbol &S) const;
+
+  // The function with a prologue starting at Loc was compiled with
+  // -fsplit-stack and it calls a function compiled without. Adjust the prologue
+  // to do the right thing. See https://gcc.gnu.org/wiki/SplitStacks.
+  virtual bool adjustPrologueForCrossSplitStack(uint8_t *Loc,
+                                                uint8_t *End) const;
+
   // Return true if we can reach Dst from Src with Relocation RelocType
   virtual bool inBranchRange(RelType Type, uint64_t Src,
                              uint64_t Dst) const;
@@ -105,9 +113,15 @@
   // On PPC ELF V2 abi, the first entry in the .got is the .TOC.
   unsigned GotHeaderEntriesNum = 0;
 
-  // Set to 0 for variant 2
+  // For TLS variant 1, the TCB is a fixed size specified by the Target.
+  // For variant 2, the TCB is an unspecified size.
+  // Set to 0 for variant 2.
   unsigned TcbSize = 0;
 
+  // Set to the offset (in bytes) that the thread pointer is initialized to
+  // point to, relative to the start of the thread local storage.
+  unsigned TlsTpOffset = 0;
+
   bool NeedsThunks = false;
 
   // A 4-byte field corresponding to one or more trap instructions, used to pad
@@ -134,8 +148,10 @@
 TargetInfo *getAMDGPUTargetInfo();
 TargetInfo *getARMTargetInfo();
 TargetInfo *getAVRTargetInfo();
+TargetInfo *getHexagonTargetInfo();
 TargetInfo *getPPC64TargetInfo();
 TargetInfo *getPPCTargetInfo();
+TargetInfo *getRISCVTargetInfo();
 TargetInfo *getSPARCV9TargetInfo();
 TargetInfo *getX32TargetInfo();
 TargetInfo *getX86TargetInfo();
@@ -175,14 +191,9 @@
         Twine(Max).str() + "]" + Hint);
 }
 
-// Sign-extend Nth bit all the way to MSB.
-inline int64_t signExtend(uint64_t V, int N) {
-  return int64_t(V << (64 - N)) >> (64 - N);
-}
-
 // Make sure that V can be represented as an N bit signed integer.
 inline void checkInt(uint8_t *Loc, int64_t V, int N, RelType Type) {
-  if (V != signExtend(V, N))
+  if (V != llvm::SignExtend64(V, N))
     reportRangeError(Loc, Type, Twine(V), llvm::minIntN(N), llvm::maxIntN(N));
 }
 
@@ -196,7 +207,7 @@
 inline void checkIntUInt(uint8_t *Loc, uint64_t V, int N, RelType Type) {
   // For the error message we should cast V to a signed integer so that error
   // messages show a small negative value rather than an extremely large one
-  if (V != (uint64_t)signExtend(V, N) && (V >> N) != 0)
+  if (V != (uint64_t)llvm::SignExtend64(V, N) && (V >> N) != 0)
     reportRangeError(Loc, Type, Twine((int64_t)V), llvm::minIntN(N),
                      llvm::maxIntN(N));
 }
diff --git a/ELF/Writer.cpp b/ELF/Writer.cpp
index 998b0dc..6e561bb 100644
--- a/ELF/Writer.cpp
+++ b/ELF/Writer.cpp
@@ -50,7 +50,7 @@
 private:
   void copyLocalSymbols();
   void addSectionSymbols();
-  void forEachRelSec(std::function<void(InputSectionBase &)> Fn);
+  void forEachRelSec(llvm::function_ref<void(InputSectionBase &)> Fn);
   void sortSections();
   void resolveShfLinkOrder();
   void sortInputSections();
@@ -83,8 +83,6 @@
 
   uint64_t FileSize;
   uint64_t SectionHeaderOff;
-
-  bool HasGotBaseSym = false;
 };
 } // anonymous namespace
 
@@ -289,6 +287,7 @@
   if (Config->Strip != StripPolicy::All) {
     InX::StrTab = make<StringTableSection>(".strtab", false);
     InX::SymTab = make<SymbolTableSection<ELFT>>(*InX::StrTab);
+    InX::SymTabShndx = make<SymtabShndxSection>();
   }
 
   if (Config->BuildId != BuildIdKind::None) {
@@ -351,6 +350,11 @@
     Add(InX::RelaDyn);
   }
 
+  if (Config->RelrPackDynRelocs) {
+    InX::RelrDyn = make<RelrSection<ELFT>>();
+    Add(InX::RelrDyn);
+  }
+
   // Add .got. MIPS' .got is so different from the other archs,
   // it has its own class.
   if (Config->EMachine == EM_MIPS) {
@@ -367,7 +371,7 @@
   Add(InX::IgotPlt);
 
   if (Config->GdbIndex) {
-    InX::GdbIndex = createGdbIndex<ELFT>();
+    InX::GdbIndex = GdbIndexSection::create<ELFT>();
     Add(InX::GdbIndex);
   }
 
@@ -406,6 +410,8 @@
 
   if (InX::SymTab)
     Add(InX::SymTab);
+  if (InX::SymTabShndx)
+    Add(InX::SymTabShndx);
   Add(InX::ShStrTab);
   if (InX::StrTab)
     Add(InX::StrTab);
@@ -515,10 +521,6 @@
   if (B.isSection())
     return false;
 
-  // If sym references a section in a discarded group, don't keep it.
-  if (Sec == &InputSection::Discarded)
-    return false;
-
   if (Config->Discard == DiscardPolicy::None)
     return true;
 
@@ -703,10 +705,10 @@
   RF_WRITE = 1 << 15,
   RF_EXEC_WRITE = 1 << 14,
   RF_EXEC = 1 << 13,
-  RF_NON_TLS_BSS = 1 << 12,
-  RF_NON_TLS_BSS_RO = 1 << 11,
-  RF_NOT_TLS = 1 << 10,
-  RF_ALLOC_FIRST = 1 << 9,
+  RF_RODATA = 1 << 12,
+  RF_NON_TLS_BSS = 1 << 11,
+  RF_NON_TLS_BSS_RO = 1 << 10,
+  RF_NOT_TLS = 1 << 9,
   RF_BSS = 1 << 8,
   RF_NOTE = 1 << 7,
   RF_PPC_NOT_TOCBSS = 1 << 6,
@@ -738,23 +740,12 @@
   if (!(Sec->Flags & SHF_ALLOC))
     return Rank | RF_NOT_ALLOC;
 
-  // Place .dynsym and .dynstr at the beginning of SHF_ALLOC
-  // sections. We want to do this to mitigate the possibility that
-  // huge .dynsym and .dynstr sections placed between ro-data and text
-  // sections cause relocation overflow.  Note: .dynstr has SHT_STRTAB
-  // type and SHF_ALLOC attribute, whereas sections that only have
-  // SHT_STRTAB but without SHF_ALLOC is placed at the end. All "Sec"
-  // reaching here has SHF_ALLOC bit set.
-  if (Sec->Type == SHT_DYNSYM || Sec->Type == SHT_STRTAB)
-    return Rank | RF_ALLOC_FIRST;
-
   // Sort sections based on their access permission in the following
   // order: R, RX, RWX, RW.  This order is based on the following
   // considerations:
   // * Read-only sections come first such that they go in the
   //   PT_LOAD covering the program headers at the start of the file.
-  // * Read-only, executable sections come next, unless the
-  //   -no-rosegment option is used.
+  // * Read-only, executable sections come next.
   // * Writable, executable sections follow such that .plt on
   //   architectures where it needs to be writable will be placed
   //   between .text and .data.
@@ -766,11 +757,16 @@
   if (IsExec) {
     if (IsWrite)
       Rank |= RF_EXEC_WRITE;
-    else if (!Config->SingleRoRx)
+    else
       Rank |= RF_EXEC;
-  } else {
-    if (IsWrite)
-      Rank |= RF_WRITE;
+  } else if (IsWrite) {
+    Rank |= RF_WRITE;
+  } else if (Sec->Type == SHT_PROGBITS) {
+    // Make non-executable and non-writable PROGBITS sections (e.g .rodata
+    // .eh_frame) closer to .text. They likely contain PC or GOT relative
+    // relocations and there could be relocation overflow if other huge sections
+    // (.dynstr .dynsym) were placed in between.
+    Rank |= RF_RODATA;
   }
 
   // If we got here we know that both A and B are in the same PT_LOAD.
@@ -889,7 +885,8 @@
 }
 
 template <class ELFT>
-void Writer<ELFT>::forEachRelSec(std::function<void(InputSectionBase &)> Fn) {
+void Writer<ELFT>::forEachRelSec(
+    llvm::function_ref<void(InputSectionBase &)> Fn) {
   // Scan all relocations. Each relocation goes through a series
   // of tests to determine if it needs special treatment, such as
   // creating GOT, PLT, copy relocations, etc.
@@ -1009,11 +1006,9 @@
 //  rw_sec : { *(rw_sec) }
 // would mean that the RW PT_LOAD would become unaligned.
 static bool shouldSkip(BaseCommand *Cmd) {
-  if (isa<OutputSection>(Cmd))
-    return false;
   if (auto *Assign = dyn_cast<SymbolAssignment>(Cmd))
     return Assign->Name != ".";
-  return true;
+  return false;
 }
 
 // We want to place orphan sections so that they share as much
@@ -1382,8 +1377,7 @@
   };
 
   // Get the last table Entry from the previous .ARM.exidx section.
-  const ExidxEntry &PrevEntry = *reinterpret_cast<const ExidxEntry *>(
-      Prev->Data.data() + Prev->getSize() - sizeof(ExidxEntry));
+  const ExidxEntry &PrevEntry = Prev->getDataAs<ExidxEntry>().back();
   if (IsExtabRef(PrevEntry.Unwind))
     return false;
 
@@ -1395,14 +1389,7 @@
   // consecutive identical entries are rare and the effort to check that they
   // are identical is high.
 
-  if (isa<SyntheticSection>(Cur))
-    // Exidx sentinel section has implicit EXIDX_CANTUNWIND;
-    return PrevEntry.Unwind == 0x1;
-
-  ArrayRef<const ExidxEntry> Entries(
-      reinterpret_cast<const ExidxEntry *>(Cur->Data.data()),
-      Cur->getSize() / sizeof(ExidxEntry));
-  for (const ExidxEntry &Entry : Entries)
+  for (const ExidxEntry Entry : Cur->getDataAs<ExidxEntry>())
     if (IsExtabRef(Entry.Unwind) || Entry.Unwind != PrevEntry.Unwind)
       return false;
   // All table entries in this .ARM.exidx Section can be merged into the
@@ -1432,13 +1419,12 @@
     if (!Config->Relocatable && Config->EMachine == EM_ARM &&
         Sec->Type == SHT_ARM_EXIDX) {
 
-      if (!Sections.empty() && isa<ARMExidxSentinelSection>(Sections.back())) {
+      if (auto *Sentinel = dyn_cast<ARMExidxSentinelSection>(Sections.back())) {
         assert(Sections.size() >= 2 &&
                "We should create a sentinel section only if there are "
                "alive regular exidx sections.");
         // The last executable section is required to fill the sentinel.
         // Remember it here so that we don't have to find it again.
-        auto *Sentinel = cast<ARMExidxSentinelSection>(Sections.back());
         Sentinel->Highest = Sections[Sections.size() - 2]->getLinkOrderDep();
       }
 
@@ -1449,16 +1435,13 @@
         // previous one. This does not require any rewriting of InputSection
         // contents but misses opportunities for fine grained deduplication
         // where only a subset of the InputSection contents can be merged.
-        int Cur = 1;
-        int Prev = 0;
+        size_t Prev = 0;
         // The last one is a sentinel entry which should not be removed.
-        int N = Sections.size() - 1;
-        while (Cur < N) {
-          if (isDuplicateArmExidxSec(Sections[Prev], Sections[Cur]))
-            Sections[Cur] = nullptr;
+        for (size_t I = 1; I < Sections.size() - 1; ++I) {
+          if (isDuplicateArmExidxSec(Sections[Prev], Sections[I]))
+            Sections[I] = nullptr;
           else
-            Prev = Cur;
-          ++Cur;
+            Prev = I;
         }
       }
     }
@@ -1474,7 +1457,7 @@
 }
 
 static void applySynthetic(const std::vector<SyntheticSection *> &Sections,
-                           std::function<void(SyntheticSection *)> Fn) {
+                           llvm::function_ref<void(SyntheticSection *)> Fn) {
   for (SyntheticSection *SS : Sections)
     if (SS && SS->getParent() && !SS->empty())
       Fn(SS);
@@ -1572,6 +1555,15 @@
   // Define __rel[a]_iplt_{start,end} symbols if needed.
   addRelIpltSymbols();
 
+  // RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800 if not defined.
+  if (Config->EMachine == EM_RISCV) {
+    ElfSym::RISCVGlobalPointer =
+        dyn_cast_or_null<Defined>(Symtab->find("__global_pointer$"));
+    if (!ElfSym::RISCVGlobalPointer)
+      ElfSym::RISCVGlobalPointer =
+          addOptionalRegular("__global_pointer$", findSection(".sdata"), 0x800);
+  }
+
   // This responsible for splitting up .eh_frame section into
   // pieces. The relocation scan uses those pieces, so this has to be
   // earlier.
@@ -1611,6 +1603,9 @@
   if (errorCount())
     return;
 
+  if (InX::MipsGot)
+    InX::MipsGot->build<ELFT>();
+
   removeUnusedSyntheticSections();
 
   sortSections();
@@ -1621,6 +1616,15 @@
     if (auto *Sec = dyn_cast<OutputSection>(Base))
       OutputSections.push_back(Sec);
 
+  // Ensure data sections are not mixed with executable sections when
+  // -execute-only is used.
+  if (Config->ExecuteOnly)
+    for (OutputSection *OS : OutputSections)
+      if (OS->Flags & SHF_EXECINSTR)
+        for (InputSection *IS : getInputSections(OS))
+          if (!(IS->Flags & SHF_EXECINSTR))
+            error("-execute-only does not support intermingling data and code");
+
   // Prefer command line supplied address over other constraints.
   for (OutputSection *Sec : OutputSections) {
     auto I = Config->SectionStartMap.find(Sec->Name);
@@ -1655,12 +1659,13 @@
   // Dynamic section must be the last one in this list and dynamic
   // symbol table section (DynSymTab) must be the first one.
   applySynthetic(
-      {InX::DynSymTab,   InX::Bss,          InX::BssRelRo, InX::GnuHashTab,
-       InX::HashTab,     InX::SymTab,       InX::ShStrTab, InX::StrTab,
-       In<ELFT>::VerDef, InX::DynStrTab,    InX::Got,      InX::MipsGot,
-       InX::IgotPlt,     InX::GotPlt,       InX::RelaDyn,  InX::RelaIplt,
-       InX::RelaPlt,     InX::Plt,          InX::Iplt,     InX::EhFrameHdr,
-       In<ELFT>::VerSym, In<ELFT>::VerNeed, InX::Dynamic},
+      {InX::DynSymTab, InX::Bss,         InX::BssRelRo,    InX::GnuHashTab,
+       InX::HashTab,   InX::SymTab,      InX::SymTabShndx, InX::ShStrTab,
+       InX::StrTab,    In<ELFT>::VerDef, InX::DynStrTab,   InX::Got,
+       InX::MipsGot,   InX::IgotPlt,     InX::GotPlt,      InX::RelaDyn,
+       InX::RelrDyn,   InX::RelaIplt,    InX::RelaPlt,     InX::Plt,
+       InX::Iplt,      InX::EhFrameHdr,  In<ELFT>::VerSym, In<ELFT>::VerNeed,
+       InX::Dynamic},
       [](SyntheticSection *SS) { SS->finalizeContents(); });
 
   if (!Script->HasSectionsCommand && !Config->Relocatable)
@@ -1675,7 +1680,8 @@
   // for jump instructions that is the linker's responsibility for creating
   // range extension thunks for. As the generation of the content may also
   // alter InputSection addresses we must converge to a fixed point.
-  if (Target->NeedsThunks || Config->AndroidPackDynRelocs) {
+  if (Target->NeedsThunks || Config->AndroidPackDynRelocs ||
+      Config->RelrPackDynRelocs) {
     ThunkCreator TC;
     AArch64Err843419Patcher A64P;
     bool Changed;
@@ -1692,6 +1698,8 @@
       if (InX::MipsGot)
         InX::MipsGot->updateAllocSize();
       Changed |= InX::RelaDyn->updateAllocSize();
+      if (InX::RelrDyn)
+        Changed |= InX::RelrDyn->updateAllocSize();
     } while (Changed);
   }
 
@@ -1776,6 +1784,8 @@
 static uint64_t computeFlags(uint64_t Flags) {
   if (Config->Omagic)
     return PF_R | PF_W | PF_X;
+  if (Config->ExecuteOnly && (Flags & PF_X))
+    return Flags & ~PF_R;
   if (Config->SingleRoRx && !(Flags & PF_W))
     return Flags | PF_X;
   return Flags;
@@ -1814,12 +1824,14 @@
     // Segments are contiguous memory regions that has the same attributes
     // (e.g. executable or writable). There is one phdr for each segment.
     // Therefore, we need to create a new phdr when the next section has
-    // different flags or is loaded at a discontiguous address using AT linker
-    // script command. At the same time, we don't want to create a separate
-    // load segment for the headers, even if the first output section has
-    // an AT attribute.
+    // different flags or is loaded at a discontiguous address or memory
+    // region using AT or AT> linker script command, respectively. At the same
+    // time, we don't want to create a separate load segment for the headers,
+    // even if the first output section has an AT or AT> attribute.
     uint64_t NewFlags = computeFlags(Sec->getPhdrFlags());
-    if ((Sec->LMAExpr && Load->LastSec != Out::ProgramHeaders) ||
+    if (((Sec->LMAExpr ||
+          (Sec->LMARegion && (Sec->LMARegion != Load->FirstSec->LMARegion))) &&
+         Load->LastSec != Out::ProgramHeaders) ||
         Sec->MemRegion != Load->FirstSec->MemRegion || Flags != NewFlags) {
 
       Load = AddHdr(PT_LOAD, NewFlags);
@@ -2003,8 +2015,6 @@
 }
 
 static std::string rangeToString(uint64_t Addr, uint64_t Len) {
-  if (Len == 0)
-    return "<empty range at 0x" + utohexstr(Addr) + ">";
   return "[0x" + utohexstr(Addr) + ", 0x" + utohexstr(Addr + Len - 1) + "]";
 }
 
@@ -2047,7 +2057,7 @@
       continue;
     if ((Sec->Offset > FileSize) || (Sec->Offset + Sec->Size > FileSize))
       error("unable to place section " + Sec->Name + " at file offset " +
-            rangeToString(Sec->Offset, Sec->Offset + Sec->Size) +
+            rangeToString(Sec->Offset, Sec->Size) +
             "; check your linker script for overflows");
   }
 }
@@ -2098,7 +2108,8 @@
 
 // Check whether sections overlap for a specific address range (file offsets,
 // load and virtual adresses).
-static void checkOverlap(StringRef Name, std::vector<SectionOffset> &Sections) {
+static void checkOverlap(StringRef Name, std::vector<SectionOffset> &Sections,
+                         bool IsVirtualAddr) {
   llvm::sort(Sections.begin(), Sections.end(),
              [=](const SectionOffset &A, const SectionOffset &B) {
                return A.Offset < B.Offset;
@@ -2109,12 +2120,19 @@
   for (size_t I = 1, End = Sections.size(); I < End; ++I) {
     SectionOffset A = Sections[I - 1];
     SectionOffset B = Sections[I];
-    if (B.Offset < A.Offset + A.Sec->Size)
-      errorOrWarn(
-          "section " + A.Sec->Name + " " + Name + " range overlaps with " +
-          B.Sec->Name + "\n>>> " + A.Sec->Name + " range is " +
-          rangeToString(A.Offset, A.Sec->Size) + "\n>>> " + B.Sec->Name +
-          " range is " + rangeToString(B.Offset, B.Sec->Size));
+    if (B.Offset >= A.Offset + A.Sec->Size)
+      continue;
+
+    // If both sections are in OVERLAY we allow the overlapping of virtual
+    // addresses, because it is what OVERLAY was designed for.
+    if (IsVirtualAddr && A.Sec->InOverlay && B.Sec->InOverlay)
+      continue;
+
+    errorOrWarn("section " + A.Sec->Name + " " + Name +
+                " range overlaps with " + B.Sec->Name + "\n>>> " + A.Sec->Name +
+                " range is " + rangeToString(A.Offset, A.Sec->Size) + "\n>>> " +
+                B.Sec->Name + " range is " +
+                rangeToString(B.Offset, B.Sec->Size));
   }
 }
 
@@ -2139,10 +2157,10 @@
   // file so we skip any non-allocated sections in that case.
   std::vector<SectionOffset> FileOffs;
   for (OutputSection *Sec : OutputSections)
-    if (0 < Sec->Size && Sec->Type != SHT_NOBITS &&
+    if (Sec->Size > 0 && Sec->Type != SHT_NOBITS &&
         (!Config->OFormatBinary || (Sec->Flags & SHF_ALLOC)))
       FileOffs.push_back({Sec, Sec->Offset});
-  checkOverlap("file", FileOffs);
+  checkOverlap("file", FileOffs, false);
 
   // When linking with -r there is no need to check for overlapping virtual/load
   // addresses since those addresses will only be assigned when the final
@@ -2157,18 +2175,18 @@
   // ranges in the file.
   std::vector<SectionOffset> VMAs;
   for (OutputSection *Sec : OutputSections)
-    if (0 < Sec->Size && (Sec->Flags & SHF_ALLOC) && !(Sec->Flags & SHF_TLS))
+    if (Sec->Size > 0 && (Sec->Flags & SHF_ALLOC) && !(Sec->Flags & SHF_TLS))
       VMAs.push_back({Sec, Sec->Addr});
-  checkOverlap("virtual address", VMAs);
+  checkOverlap("virtual address", VMAs, true);
 
   // Finally, check that the load addresses don't overlap. This will usually be
   // the same as the virtual addresses but can be different when using a linker
   // script with AT().
   std::vector<SectionOffset> LMAs;
   for (OutputSection *Sec : OutputSections)
-    if (0 < Sec->Size && (Sec->Flags & SHF_ALLOC) && !(Sec->Flags & SHF_TLS))
+    if (Sec->Size > 0 && (Sec->Flags & SHF_ALLOC) && !(Sec->Flags & SHF_TLS))
       LMAs.push_back({Sec, Sec->getLMA()});
-  checkOverlap("load address", LMAs);
+  checkOverlap("load address", LMAs, false);
 }
 
 // The entry point address is chosen in the following ways.
@@ -2244,8 +2262,6 @@
   EHdr->e_ehsize = sizeof(Elf_Ehdr);
   EHdr->e_phnum = Phdrs.size();
   EHdr->e_shentsize = sizeof(Elf_Shdr);
-  EHdr->e_shnum = OutputSections.size() + 1;
-  EHdr->e_shstrndx = InX::ShStrTab->getParent()->SectionIndex;
 
   if (!Config->Relocatable) {
     EHdr->e_phoff = sizeof(Elf_Ehdr);
@@ -2266,8 +2282,30 @@
     ++HBuf;
   }
 
-  // Write the section header table. Note that the first table entry is null.
+  // Write the section header table.
+  //
+  // The ELF header can only store numbers up to SHN_LORESERVE in the e_shnum
+  // and e_shstrndx fields. When the value of one of these fields exceeds
+  // SHN_LORESERVE ELF requires us to put sentinel values in the ELF header and
+  // use fields in the section header at index 0 to store
+  // the value. The sentinel values and fields are:
+  // e_shnum = 0, SHdrs[0].sh_size = number of sections.
+  // e_shstrndx = SHN_XINDEX, SHdrs[0].sh_link = .shstrtab section index.
   auto *SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff);
+  size_t Num = OutputSections.size() + 1;
+  if (Num >= SHN_LORESERVE)
+    SHdrs->sh_size = Num;
+  else
+    EHdr->e_shnum = Num;
+
+  uint32_t StrTabIndex = InX::ShStrTab->getParent()->SectionIndex;
+  if (StrTabIndex >= SHN_LORESERVE) {
+    SHdrs->sh_link = StrTabIndex;
+    EHdr->e_shstrndx = SHN_XINDEX;
+  } else {
+    EHdr->e_shstrndx = StrTabIndex;
+  }
+
   for (OutputSection *Sec : OutputSections)
     Sec->writeHeaderTo<ELFT>(++SHdrs);
 }
diff --git a/LICENSE.TXT b/LICENSE.TXT
index ec97986..e05e184 100644
--- a/LICENSE.TXT
+++ b/LICENSE.TXT
@@ -4,7 +4,7 @@
 University of Illinois/NCSA
 Open Source License
 
-Copyright (c) 2011-2016 by the contributors listed in CREDITS.TXT
+Copyright (c) 2011-2018 by the contributors listed in CREDITS.TXT
 All rights reserved.
 
 Developed by:
diff --git a/MinGW/Driver.cpp b/MinGW/Driver.cpp
index 3e285e7..27a5550 100644
--- a/MinGW/Driver.cpp
+++ b/MinGW/Driver.cpp
@@ -149,6 +149,8 @@
   if (auto *A = Args.getLastArg(OPT_pdb)) {
     Add("-debug");
     Add("-pdb:" + StringRef(A->getValue()));
+  } else if (Args.hasArg(OPT_strip_debug)) {
+    Add("-debug:symtab");
   } else if (!Args.hasArg(OPT_strip_all)) {
     Add("-debug:dwarf");
   }
diff --git a/MinGW/Options.td b/MinGW/Options.td
index 7e15bcd..ad699f7 100644
--- a/MinGW/Options.td
+++ b/MinGW/Options.td
@@ -35,6 +35,8 @@
 def stack: S<"stack">;
 def strip_all: F<"strip-all">,
     HelpText<"Omit all symbol information from the output binary">;
+def strip_debug: F<"strip-debug">,
+    HelpText<"Omit all debug information, but keep symbol information">;
 def whole_archive: F<"whole-archive">,
     HelpText<"Include all object files for following archives">;
 def verbose: F<"verbose">, HelpText<"Verbose mode">;
@@ -72,3 +74,4 @@
 // Alias
 def alias_entry_e: JoinedOrSeparate<["-"], "e">, Alias<entry>;
 def alias_strip_s: Flag<["-"], "s">, Alias<strip_all>;
+def alias_strip_S: Flag<["-"], "S">, Alias<strip_debug>;
diff --git a/docs/ReleaseNotes.rst b/docs/ReleaseNotes.rst
index 7ac1f9c..a1ed6b2 100644
--- a/docs/ReleaseNotes.rst
+++ b/docs/ReleaseNotes.rst
@@ -1,19 +1,19 @@
 =======================
-LLD 7.0.0 Release Notes
+LLD 8.0.0 Release Notes
 =======================
 
 .. contents::
     :local:
 
 .. warning::
-   These are in-progress notes for the upcoming LLVM 7.0.0 release.
+   These are in-progress notes for the upcoming LLVM 8.0.0 release.
    Release notes for previous releases can be found on
    `the Download Page <http://releases.llvm.org/download.html>`_.
 
 Introduction
 ============
 
-This document contains the release notes for the lld linker, release 7.0.0.
+This document contains the release notes for the lld linker, release 8.0.0.
 Here we describe the status of lld, including major improvements
 from the previous release. All lld releases may be downloaded
 from the `LLVM releases web site <http://llvm.org/releases/>`_.
diff --git a/docs/conf.py b/docs/conf.py
index 3598fbf..62404b2 100644
--- a/docs/conf.py
+++ b/docs/conf.py
@@ -48,9 +48,9 @@
 # built documents.
 #
 # The short version.
-version = '7'
+version = '8'
 # The full version, including alpha/beta/rc tags.
-release = '7'
+release = '8'
 
 # The language for content autogenerated by Sphinx. Refer to documentation
 # for a list of supported languages.
diff --git a/docs/index.rst b/docs/index.rst
index f4bf3be..2821ce4 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -8,7 +8,7 @@
 The linker supports ELF (Unix), PE/COFF (Windows), Mach-O (macOS) and
 WebAssembly in descending order of completeness. Internally, LLD consists of
 several different linkers. The ELF port is the one that will be described in
-this document. The PE/COFF port is almost complete except the lack of the
+this document. The PE/COFF port is complete, including
 Windows debug info (PDB) support. The WebAssembly port is still a work in
 progress (See :doc:`WebAssembly`).  The Mach-O port is built based on a
 different architecture than the others. For the details about Mach-O, please
diff --git a/docs/ld.lld.1 b/docs/ld.lld.1
index 7f3a747..0fdfe0a 100644
--- a/docs/ld.lld.1
+++ b/docs/ld.lld.1
@@ -3,7 +3,7 @@
 .\"
 .\" This man page documents only lld's ELF linking support, obtained originally
 .\" from FreeBSD.
-.Dd April 28, 2018
+.Dd July 30, 2018
 .Dt LD.LLD 1
 .Os
 .Sh NAME
@@ -30,6 +30,8 @@
 .It Fl -allow-multiple-definition
 Do not error if a symbol is defined multiple times.
 The first definition will be used.
+.It Fl -apply-dynamic-relocs
+Apply link-time values for dynamic relocations.
 .It Fl -as-needed
 Only set
 .Dv DT_NEEDED
@@ -38,14 +40,14 @@
 Set the
 .Dv DT_AUXILIARY
 field to the specified name.
-.It Fl -Bdynamic
+.It Fl -Bdynamic , Fl -dy
 Link against shared libraries.
-.It Fl -Bstatic
+.It Fl -Bstatic , Fl -static , Fl -dn
 Do not link against shared libraries.
-.It Fl -Bsymbolic-functions
-Bind defined function symbols locally.
 .It Fl -Bsymbolic
 Bind defined symbols locally.
+.It Fl -Bsymbolic-functions
+Bind defined function symbols locally.
 .It Fl -build-id Ns = Ns Ar value
 Generate a build ID note.
 .Ar value
@@ -93,7 +95,9 @@
 .Cm none
 or
 .Cm zlib .
-.It Fl -define-common
+.It Fl -cref
+Output cross reference table.
+.It Fl -define-common , Fl d
 Assign space to common symbols.
 .It Fl -defsym Ns = Ns Ar symbol Ns = Ns Ar expression
 Define a symbol alias.
@@ -107,9 +111,9 @@
 Demangle symbol names.
 .It Fl -disable-new-dtags
 Disable new dynamic tags.
-.It Fl -discard-all
+.It Fl -discard-all , Fl x
 Delete all local symbols.
-.It Fl -discard-locals
+.It Fl -discard-locals , Fl X
 Delete temporary local symbols.
 .It Fl -discard-none
 Keep all symbols in the symbol table.
@@ -126,7 +130,7 @@
 section and
 .Dv PT_GNU_EH_FRAME
 segment header.
-.It Fl -emit-relocs
+.It Fl -emit-relocs , Fl q
 Generate relocations in the output.
 .It Fl -enable-new-dtags
 Enable new dynamic tags.
@@ -142,21 +146,21 @@
 Report unresolved symbols as errors.
 .It Fl -exclude-libs Ns = Ns Ar value
 Exclude static libraries from automatic export.
+.It Fl -export-dynamic , Fl E
+Put symbols in the dynamic symbol table.
 .It Fl -export-dynamic-symbol Ns = Ns Ar symbol
 Include
 .Ar symbol
 in the dynamic symbol table.
-.It Fl -export-dynamic
-Put symbols in the dynamic symbol table.
 .It Fl -fatal-warnings
 Treat warnings as errors.
-.It Fl -filter Ns = Ns Ar value
+.It Fl -filter Ns = Ns Ar value , Fl F Ar value
 Set the
 .Dv DT_FILTER
 field to the specified value.
 .It Fl -fini Ns = Ns Ar symbol
 Specify a finalizer function.
-.It Fl -format Ns = Ns Ar input-format
+.It Fl -format Ns = Ns Ar input-format , Fl b Ar input-format
 Specify the format of the inputs following this option.
 .Ar input-format
 may be one of
@@ -187,6 +191,8 @@
 Print a help message.
 .It Fl -icf Ns = Ns Cm all
 Enable identical code folding.
+.It Fl -icf Ns = Ns Cm safe
+Enable safe identical code folding.
 .It Fl -icf Ns = Ns Cm none
 Disable identical code folding.
 .It Fl -image-base Ns = Ns Ar value
@@ -194,6 +200,14 @@
 .Ar value .
 .It Fl -init Ns = Ns Ar symbol
 Specify an initializer function.
+.It Fl -keep-unique Ns = Ns Ar symbol
+Do not fold
+.Ar symbol
+during ICF.
+.It Fl l Ar libName, Fl -library Ns = Ns Ar libName
+Root name of library to use.
+.It Fl L Ar dir , Fl -library-path Ns = Ns Ar dir
+Add a directory to the library search path.
 .It Fl -lto-aa-pipeline Ns = Ns Ar value
 AA pipeline to run during LTO.
 Used in conjunction with
@@ -204,15 +218,11 @@
 Optimization level for LTO.
 .It Fl -lto-partitions Ns = Ns Ar value
 Number of LTO codegen partitions.
-.It Fl L Ar dir
-Add a directory to the library search path.
-.It Fl l Ar libName
-Root name of library to use.
-.It Fl -Map Ns = Ns Ar file
-Print a link map to
-.Ar file .
 .It Fl m Ar value
 Set target emulation.
+.It Fl -Map Ns = Ns Ar file , Fl M Ar file
+Print a link map to
+.Ar file .
 .It Fl -no-as-needed
 Always set
 .Dv DT_NEEDED
@@ -241,26 +251,18 @@
 Report unresolved symbols even if the linker is creating a shared library.
 .It Fl -no-whole-archive
 Restores the default behavior of loading archive members.
-.It Fl -noinhibit-exec
-Retain the executable output file whenever it is still usable.
 .It Fl -no-pie
 Do not create a position independent executable.
+.It Fl -noinhibit-exec
+Retain the executable output file whenever it is still usable.
 .It Fl -nostdlib
 Only search directories specified on the command line.
-.It Fl -oformat Ns = Ns Ar format
-Specify the format for the output object file.
-The only supported
-.Ar format
-is
-.Cm binary ,
-which produces output with no ELF header.
-.It Fl -omagic
-Set the text and data sections to be readable and writable.
-.It Fl -opt-remarks-filename Ar file
-Write optimization remarks in YAML format to
-.Ar file .
-.It Fl -opt-remarks-with-hotness
-Include hotness information in the optimization remarks file.
+.It Fl o Ar path
+Write the output executable, library, or object to
+.Ar path .
+If not specified,
+.Dv a.out
+is used as a default.
 .It Fl O Ns Ar value
 Optimize output file size.
 .Ar value
@@ -277,12 +279,20 @@
 .Pp
 .Fl O Ns Cm 1
 is the default.
-.It Fl o Ar path
-Write the output executable, library, or object to
-.Ar path .
-If not specified,
-.Dv a.out
-is used as a default.
+.It Fl -oformat Ns = Ns Ar format
+Specify the format for the output object file.
+The only supported
+.Ar format
+is
+.Cm binary ,
+which produces output with no ELF header.
+.It Fl -omagic , Fl N
+Set the text and data sections to be readable and writable.
+.It Fl -opt-remarks-filename Ar file
+Write optimization remarks in YAML format to
+.Ar file .
+.It Fl -opt-remarks-with-hotness
+Include hotness information in the optimization remarks file.
 .It Fl -pie
 Create a position independent executable.
 .It Fl -print-gc-sections
@@ -298,13 +308,13 @@
 .It Fl -pop-state
 Undo the effect of
 .Fl -push-state.
-.It Fl -relocatable
+.It Fl -relocatable , Fl r
 Create relocatable object file.
 .It Fl -reproduce Ns = Ns Ar value
 Dump linker invocation and input files for debugging.
 .It Fl -retain-symbols-file Ns = Ns Ar file
 Retain only the symbols listed in the file.
-.It Fl -rpath Ns = Ns Ar value
+.It Fl -rpath Ns = Ns Ar value , Fl R Ar value
 Add a
 .Dv DT_RUNPATH
 to the output.
@@ -314,14 +324,14 @@
 .Cm windows
 and
 .Cm posix .
-.It Fl -script Ns = Ns Ar file
+.It Fl -script Ns = Ns Ar file , Fl T Ar file
 Read linker script from
 .Ar file .
-.It Fl -section-start Ns = Ar section Ns = Ns Ar address
+.It Fl -section-start Ns = Ns Ar section Ns = Ns Ar address
 Set address of section.
-.It Fl -shared
+.It Fl -shared , Fl -Bsharable
 Build a shared object.
-.It Fl -soname Ns = Ns Ar value
+.It Fl -soname Ns = Ns Ar value , Fl h Ar value
 Set
 .Dv DT_SONAME
 to
@@ -331,9 +341,9 @@
 .It Fl -start-lib
 Start a grouping of objects that should be treated as if they were together
 in an archive.
-.It Fl -strip-all
+.It Fl -strip-all , Fl s
 Strip all symbols.
-.It Fl -strip-debug
+.It Fl -strip-debug , Fl S
 Strip debugging information.
 .It Fl -symbol-ordering-file Ns = Ns Ar file
 Lay out sections in the order specified by
@@ -374,6 +384,12 @@
 with
 .Li .data
 as the sectionname.
+.It Fl -Ttext Ns = Ns Ar value
+Same as
+.Fl -section-start
+with
+.Li .text
+as the sectionname.
 .It Fl -thinlto-cache-dir Ns = Ns Ar value
 Path to ThinLTO cached object file directory.
 .It Fl -thinlto-cache-policy Ns = Ns Ar value
@@ -383,33 +399,27 @@
 .It Fl -threads
 Run the linker multi-threaded.
 This option is enabled by default.
-.It Fl -trace-symbol Ns = Ns Ar symbol
-Trace references to
-.Ar symbol .
 .It Fl -trace
 Print the names of the input files.
-.It Fl -Ttext Ns = Ns Ar value
-Same as
-.Fl -section-start
-with
-.Li .text
-as the sectionname.
-.It Fl -undefined Ns = Ns Ar symbol
+.It Fl -trace-symbol Ns = Ns Ar symbol , Fl y Ar symbol
+Trace references to
+.Ar symbol .
+.It Fl -undefined Ns = Ns Ar symbol , Fl u Ar symbol
 Force
 .Ar symbol
 to be an undefined symbol during linking.
 .It Fl -unresolved-symbols Ns = Ns Ar value
 Determine how to handle unresolved symbols.
+.It Fl v
+Display the version number and proceed with linking if object files are
+specified.
+.It Fl V , Fl -version
+Display the version number and exit.
 .It Fl -verbose
 Verbose mode.
 .It Fl -version-script Ns = Ns Ar file
 Read version script from
 .Ar file .
-.It Fl V , Fl -version
-Display the version number and exit.
-.It Fl v
-Display the version number and proceed with linking if object files are
-specified.
 .It Fl -warn-backrefs
 Warn about reverse or cyclic dependencies to or between static archives.
 This can be used to ensure linker invocation remains compatible with
@@ -430,6 +440,10 @@
 Stack permissions are recorded in the
 .Dv PT_GNU_STACK
 segment.
+.It Cm initfirst
+Sets the
+.Dv DF_1_INITFIRST
+flag to indicate the module should be initialized first.
 .It Cm muldefs
 Do not error if a symbol is defined multiple times.
 The first definition will be used.
diff --git a/docs/windows_support.rst b/docs/windows_support.rst
index 780c663..a0a2c4d 100644
--- a/docs/windows_support.rst
+++ b/docs/windows_support.rst
@@ -60,6 +60,13 @@
   link.exe.  However, LLD does not support /DEBUG:FASTLINK.
 
 
+Downloading LLD
+===============
+
+The Windows version of LLD is included in the `pre-built binaries of LLVM's
+releases <https://releases.llvm.org/download.html>`_ and in the `LLVM Snapshot
+Builds <https://llvm.org/builds/>`_.
+
 Building LLD
 ============
 
diff --git a/include/lld/Common/Driver.h b/include/lld/Common/Driver.h
index 15ec3cd..f6d9293 100644
--- a/include/lld/Common/Driver.h
+++ b/include/lld/Common/Driver.h
@@ -30,7 +30,7 @@
 }
 
 namespace mach_o {
-bool link(llvm::ArrayRef<const char *> Args,
+bool link(llvm::ArrayRef<const char *> Args, bool CanExitEarly,
           llvm::raw_ostream &Diag = llvm::errs());
 }
 
diff --git a/include/lld/Common/ErrorHandler.h b/include/lld/Common/ErrorHandler.h
index b6c78f1..f17f7cc 100644
--- a/include/lld/Common/ErrorHandler.h
+++ b/include/lld/Common/ErrorHandler.h
@@ -7,21 +7,62 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// In LLD, we have three levels of errors: fatal, error or warn.
+// We designed lld's error handlers with the following goals in mind:
 //
-// Fatal makes the program exit immediately with an error message.
-// You shouldn't use it except for reporting a corrupted input file.
+//  - Errors can occur at any place where we handle user input, but we don't
+//    want them to affect the normal execution path too much. Ideally,
+//    handling errors should be as simple as reporting them and exit (but
+//    without actually doing exit).
 //
-// Error prints out an error message and increment a global variable
-// ErrorCount to record the fact that we met an error condition. It does
-// not exit, so it is safe for a lld-as-a-library use case. It is generally
-// useful because it can report more than one error in a single run.
+//    In particular, the design to wrap all functions that could fail with
+//    ErrorOr<T> is rejected because otherwise we would have to wrap a large
+//    number of functions in lld with ErrorOr. With that approach, if some
+//    function F can fail, not only F but all functions that transitively call
+//    F have to be wrapped with ErrorOr. That seemed too much.
 //
-// Warn doesn't do anything but printing out a given message.
+//  - Finding only one error at a time is not sufficient. We want to find as
+//    many errors as possible with one execution of the linker. That means the
+//    linker needs to keep running after a first error and give up at some
+//    checkpoint (beyond which it would find cascading, false errors caused by
+//    the previous errors).
 //
-// It is not recommended to use llvm::outs() or llvm::errs() directly
-// in LLD because they are not thread-safe. The functions declared in
-// this file are mutually excluded, so you want to use them instead.
+//  - We want a simple interface to report errors. Unlike Clang, the data we
+//    handle is compiled binary, so we don't need an error reporting mechanism
+//    that's as sophisticated as the one that Clang has.
+//
+// The current lld's error handling mechanism is simple:
+//
+//  - When you find an error, report it using error() and continue as far as
+//    you can. An internal error counter is incremented by one every time you
+//    call error().
+//
+//    A common idiom to handle an error is calling error() and then returning
+//    a reasonable default value. For example, if your function handles a
+//    user-supplied alignment value, and if you find an invalid alignment
+//    (e.g. 17 which is not 2^n), you may report it using error() and continue
+//    as if it were alignment 1 (which is the simplest reasonable value).
+//
+//    Note that you should not continue with an invalid value; that breaks the
+//    internal consistency. You need to maintain all variables have some sane
+//    value even after an error occurred. So, when you have to continue with
+//    some value, always use a dummy value.
+//
+//  - Find a reasonable checkpoint at where you want to stop the linker, and
+//    add code to return from the function if errorCount() > 0. In most cases,
+//    a checkpoint already exists, so you don't need to do anything for this.
+//
+// This interface satisfies all the goals that we mentioned above.
+//
+// You should never call fatal() except for reporting a corrupted input file.
+// fatal() immediately terminates the linker, so the function is not desirable
+// if you are using lld as a subroutine in other program, and with that you
+// can find only one error at a time.
+//
+// warn() doesn't do anything but printing out a given message.
+//
+// It is not recommended to use llvm::outs() or llvm::errs() directly in lld
+// because they are not thread-safe. The functions declared in this file are
+// thread-safe.
 //
 //===----------------------------------------------------------------------===//
 
diff --git a/include/lld/Core/LinkingContext.h b/include/lld/Core/LinkingContext.h
index 6dffdd4..52ab1a2 100644
--- a/include/lld/Core/LinkingContext.h
+++ b/include/lld/Core/LinkingContext.h
@@ -16,7 +16,6 @@
 #include "llvm/ADT/StringRef.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/Error.h"
-#include "llvm/Support/raw_ostream.h"
 #include <cassert>
 #include <cstdint>
 #include <memory>
@@ -167,10 +166,10 @@
   /// After all set* methods are called, the Driver calls this method
   /// to validate that there are no missing options or invalid combinations
   /// of options.  If there is a problem, a description of the problem
-  /// is written to the supplied stream.
+  /// is written to the global error handler.
   ///
   /// \returns true if there is an error with the current settings.
-  bool validate(raw_ostream &diagnostics);
+  bool validate();
 
   /// Formats symbol name for use in error messages.
   virtual std::string demangle(StringRef symbolName) const = 0;
@@ -250,7 +249,7 @@
 
 private:
   /// Validate the subclass bits. Only called by validate.
-  virtual bool validateImpl(raw_ostream &diagnostics) = 0;
+  virtual bool validateImpl() = 0;
 };
 
 } // end namespace lld
diff --git a/include/lld/Core/TODO.txt b/include/lld/Core/TODO.txt
index 8b52304..2aa61ff 100644
--- a/include/lld/Core/TODO.txt
+++ b/include/lld/Core/TODO.txt
@@ -6,9 +6,6 @@
   abstraction only works for returning low level OS errors.  It does not
   work for describing formatting issues.
 
-* We need to design a diagnostics interface.  It would be nice to share code
-  with Clang_ where possible.
-
 * We need to add more attributes to File.  In particular, we need cpu
   and OS information (like target triples).  We should also provide explicit
   support for `LLVM IR module flags metadata`__.
diff --git a/include/lld/ReaderWriter/MachOLinkingContext.h b/include/lld/ReaderWriter/MachOLinkingContext.h
index 5f9588d..fde6588 100644
--- a/include/lld/ReaderWriter/MachOLinkingContext.h
+++ b/include/lld/ReaderWriter/MachOLinkingContext.h
@@ -89,7 +89,7 @@
                  bool exportDynamicSymbols);
 
   void addPasses(PassManager &pm) override;
-  bool validateImpl(raw_ostream &diagnostics) override;
+  bool validateImpl() override;
   std::string demangle(StringRef symbolName) const override;
 
   void createImplicitFiles(std::vector<std::unique_ptr<File>> &) override;
diff --git a/lib/Core/LinkingContext.cpp b/lib/Core/LinkingContext.cpp
index 5de863a..0f225c3 100644
--- a/lib/Core/LinkingContext.cpp
+++ b/lib/Core/LinkingContext.cpp
@@ -20,8 +20,8 @@
 
 LinkingContext::~LinkingContext() = default;
 
-bool LinkingContext::validate(raw_ostream &diagnostics) {
-  return validateImpl(diagnostics);
+bool LinkingContext::validate() {
+  return validateImpl();
 }
 
 llvm::Error LinkingContext::writeFile(const File &linkedFile) const {
diff --git a/lib/Driver/CMakeLists.txt b/lib/Driver/CMakeLists.txt
index 097a817..ff67c28 100644
--- a/lib/Driver/CMakeLists.txt
+++ b/lib/Driver/CMakeLists.txt
@@ -13,6 +13,7 @@
     Support
 
   LINK_LIBS
+    lldCommon
     lldCore
     lldMachO
     lldReaderWriter
diff --git a/lib/Driver/DarwinLdDriver.cpp b/lib/Driver/DarwinLdDriver.cpp
index e016a3d..ad22845 100644
--- a/lib/Driver/DarwinLdDriver.cpp
+++ b/lib/Driver/DarwinLdDriver.cpp
@@ -13,6 +13,8 @@
 ///
 //===----------------------------------------------------------------------===//
 
+#include "lld/Common/Args.h"
+#include "lld/Common/ErrorHandler.h"
 #include "lld/Common/LLVM.h"
 #include "lld/Core/ArchiveLibraryFile.h"
 #include "lld/Core/Error.h"
@@ -110,11 +112,11 @@
   return members;
 }
 
-std::vector<std::unique_ptr<File>>
-loadFile(MachOLinkingContext &ctx, StringRef path,
-         raw_ostream &diag, bool wholeArchive, bool upwardDylib) {
+std::vector<std::unique_ptr<File>> loadFile(MachOLinkingContext &ctx,
+                                            StringRef path, bool wholeArchive,
+                                            bool upwardDylib) {
   if (ctx.logInputFiles())
-    diag << path << "\n";
+    message(path);
 
   ErrorOr<std::unique_ptr<MemoryBuffer>> mbOrErr = ctx.getMemoryBuffer(path);
   if (std::error_code ec = mbOrErr.getError())
@@ -155,10 +157,9 @@
 }
 
 static void addFile(StringRef path, MachOLinkingContext &ctx,
-                    bool loadWholeArchive,
-                    bool upwardDylib, raw_ostream &diag) {
+                    bool loadWholeArchive, bool upwardDylib) {
   std::vector<std::unique_ptr<File>> files =
-      loadFile(ctx, path, diag, loadWholeArchive, upwardDylib);
+      loadFile(ctx, path, loadWholeArchive, upwardDylib);
   for (std::unique_ptr<File> &file : files)
     ctx.getNodes().push_back(llvm::make_unique<FileNode>(std::move(file)));
 }
@@ -166,8 +167,7 @@
 // Export lists are one symbol per line.  Blank lines are ignored.
 // Trailing comments start with #.
 static std::error_code parseExportsList(StringRef exportFilePath,
-                                        MachOLinkingContext &ctx,
-                                        raw_ostream &diagnostics) {
+                                        MachOLinkingContext &ctx) {
   // Map in export list file.
   ErrorOr<std::unique_ptr<MemoryBuffer>> mb =
                                    MemoryBuffer::getFileOrSTDIN(exportFilePath);
@@ -197,8 +197,7 @@
 ///     libfrob.a(bar.o):_bar
 ///     x86_64:_foo64
 static std::error_code parseOrderFile(StringRef orderFilePath,
-                                      MachOLinkingContext &ctx,
-                                      raw_ostream &diagnostics) {
+                                      MachOLinkingContext &ctx) {
   // Map in order file.
   ErrorOr<std::unique_ptr<MemoryBuffer>> mb =
                                    MemoryBuffer::getFileOrSTDIN(orderFilePath);
@@ -252,8 +251,7 @@
 // per line. The <dir> prefix is prepended to each partial path.
 //
 static llvm::Error loadFileList(StringRef fileListPath,
-                                MachOLinkingContext &ctx, bool forceLoad,
-                                raw_ostream &diagnostics) {
+                                MachOLinkingContext &ctx, bool forceLoad) {
   // If there is a comma, split off <dir>.
   std::pair<StringRef, StringRef> opt = fileListPath.split(',');
   StringRef filePath = opt.first;
@@ -286,9 +284,9 @@
                                             + "'");
     }
     if (ctx.testingFileUsage()) {
-      diagnostics << "Found filelist entry " << canonicalizePath(path) << '\n';
+      message("Found filelist entry " + canonicalizePath(path));
     }
-    addFile(path, ctx, forceLoad, false, diagnostics);
+    addFile(path, ctx, forceLoad, false);
     buffer = lineAndRest.second;
   }
   return llvm::Error::success();
@@ -317,8 +315,7 @@
 namespace lld {
 namespace mach_o {
 
-bool parse(llvm::ArrayRef<const char *> args, MachOLinkingContext &ctx,
-           raw_ostream &diagnostics) {
+bool parse(llvm::ArrayRef<const char *> args, MachOLinkingContext &ctx) {
   // Parse command line options using DarwinLdOptions.td
   DarwinLdOptTable table;
   unsigned missingIndex;
@@ -326,17 +323,20 @@
   llvm::opt::InputArgList parsedArgs =
       table.ParseArgs(args.slice(1), missingIndex, missingCount);
   if (missingCount) {
-    diagnostics << "error: missing arg value for '"
-                << parsedArgs.getArgString(missingIndex) << "' expected "
-                << missingCount << " argument(s).\n";
+    error("missing arg value for '" +
+          Twine(parsedArgs.getArgString(missingIndex)) + "' expected " +
+          Twine(missingCount) + " argument(s).");
     return false;
   }
 
   for (auto unknownArg : parsedArgs.filtered(OPT_UNKNOWN)) {
-    diagnostics << "warning: ignoring unknown argument: "
-                << unknownArg->getAsString(parsedArgs) << "\n";
+    warn("ignoring unknown argument: " +
+         Twine(unknownArg->getAsString(parsedArgs)));
   }
 
+  errorHandler().Verbose = parsedArgs.hasArg(OPT_v);
+  errorHandler().ErrorLimit = args::getInteger(parsedArgs, OPT_error_limit, 20);
+
   // Figure out output kind ( -dylib, -r, -bundle, -preload, or -static )
   llvm::MachO::HeaderFileType fileType = llvm::MachO::MH_EXECUTE;
   bool isStaticExecutable = false;
@@ -367,8 +367,7 @@
   if (llvm::opt::Arg *archStr = parsedArgs.getLastArg(OPT_arch)) {
     arch = MachOLinkingContext::archFromName(archStr->getValue());
     if (arch == MachOLinkingContext::arch_unknown) {
-      diagnostics << "error: unknown arch named '" << archStr->getValue()
-                  << "'\n";
+      error("unknown arch named '" + Twine(archStr->getValue()) + "'");
       return false;
     }
   }
@@ -386,7 +385,7 @@
       if (parsedArgs.size() == 0)
         table.PrintHelp(llvm::outs(), args[0], "LLVM Linker", false);
       else
-        diagnostics << "error: -arch not specified and could not be inferred\n";
+        error("-arch not specified and could not be inferred");
       return false;
     }
   }
@@ -402,7 +401,7 @@
       os = MachOLinkingContext::OS::macOSX;
       if (MachOLinkingContext::parsePackedVersion(minOS->getValue(),
                                                   minOSVersion)) {
-        diagnostics << "error: malformed macosx_version_min value\n";
+        error("malformed macosx_version_min value");
         return false;
       }
       break;
@@ -410,7 +409,7 @@
       os = MachOLinkingContext::OS::iOS;
       if (MachOLinkingContext::parsePackedVersion(minOS->getValue(),
                                                   minOSVersion)) {
-        diagnostics << "error: malformed ios_version_min value\n";
+        error("malformed ios_version_min value");
         return false;
       }
       break;
@@ -418,7 +417,7 @@
       os = MachOLinkingContext::OS::iOS_simulator;
       if (MachOLinkingContext::parsePackedVersion(minOS->getValue(),
                                                   minOSVersion)) {
-        diagnostics << "error: malformed ios_simulator_version_min value\n";
+        error("malformed ios_simulator_version_min value");
         return false;
       }
       break;
@@ -452,14 +451,14 @@
   if (llvm::opt::Arg *imageBase = parsedArgs.getLastArg(OPT_image_base)) {
     uint64_t baseAddress;
     if (parseNumberBase16(imageBase->getValue(), baseAddress)) {
-      diagnostics << "error: image_base expects a hex number\n";
+      error("image_base expects a hex number");
       return false;
     } else if (baseAddress < ctx.pageZeroSize()) {
-      diagnostics << "error: image_base overlaps with __PAGEZERO\n";
+      error("image_base overlaps with __PAGEZERO");
       return false;
     } else if (baseAddress % ctx.pageSize()) {
-      diagnostics << "error: image_base must be a multiple of page size ("
-                  << "0x" << llvm::utohexstr(ctx.pageSize()) << ")\n";
+      error("image_base must be a multiple of page size (0x" +
+            llvm::utohexstr(ctx.pageSize()) + ")");
       return false;
     }
 
@@ -488,13 +487,12 @@
   // Handle -compatibility_version and -current_version
   if (llvm::opt::Arg *vers = parsedArgs.getLastArg(OPT_compatibility_version)) {
     if (ctx.outputMachOType() != llvm::MachO::MH_DYLIB) {
-      diagnostics
-          << "error: -compatibility_version can only be used with -dylib\n";
+      error("-compatibility_version can only be used with -dylib");
       return false;
     }
     uint32_t parsedVers;
     if (MachOLinkingContext::parsePackedVersion(vers->getValue(), parsedVers)) {
-      diagnostics << "error: -compatibility_version value is malformed\n";
+      error("-compatibility_version value is malformed");
       return false;
     }
     ctx.setCompatibilityVersion(parsedVers);
@@ -502,12 +500,12 @@
 
   if (llvm::opt::Arg *vers = parsedArgs.getLastArg(OPT_current_version)) {
     if (ctx.outputMachOType() != llvm::MachO::MH_DYLIB) {
-      diagnostics << "-current_version can only be used with -dylib\n";
+      error("-current_version can only be used with -dylib");
       return false;
     }
     uint32_t parsedVers;
     if (MachOLinkingContext::parsePackedVersion(vers->getValue(), parsedVers)) {
-      diagnostics << "error: -current_version value is malformed\n";
+      error("-current_version value is malformed");
       return false;
     }
     ctx.setCurrentVersion(parsedVers);
@@ -526,17 +524,19 @@
       alignStr += 2;
     unsigned long long alignValue;
     if (llvm::getAsUnsignedInteger(alignStr, 16, alignValue)) {
-      diagnostics << "error: -sectalign alignment value '"
-                  << alignStr << "' not a valid number\n";
+      error("-sectalign alignment value '" + Twine(alignStr) +
+            "' not a valid number");
       return false;
     }
     uint16_t align = 1 << llvm::countTrailingZeros(alignValue);
     if (!llvm::isPowerOf2_64(alignValue)) {
-      diagnostics << "warning: alignment for '-sectalign "
-                  << segName << " " << sectName
-                  << llvm::format(" 0x%llX", alignValue)
-                  << "' is not a power of two, using "
-                  << llvm::format("0x%08X", align) << "\n";
+      std::string Msg;
+      llvm::raw_string_ostream OS(Msg);
+      OS << "alignment for '-sectalign " << segName << " " << sectName
+         << llvm::format(" 0x%llX", alignValue)
+         << "' is not a power of two, using " << llvm::format("0x%08X", align);
+      OS.flush();
+      warn(Msg);
     }
     ctx.addSectionAlignment(segName, sectName, align);
   }
@@ -562,18 +562,14 @@
   if (parsedArgs.getLastArg(OPT_keep_private_externs)) {
     ctx.setKeepPrivateExterns(true);
     if (ctx.outputMachOType() != llvm::MachO::MH_OBJECT)
-      diagnostics << "warning: -keep_private_externs only used in -r mode\n";
+      warn("-keep_private_externs only used in -r mode");
   }
 
   // Handle -dependency_info <path> used by Xcode.
-  if (llvm::opt::Arg *depInfo = parsedArgs.getLastArg(OPT_dependency_info)) {
-    if (std::error_code ec = ctx.createDependencyFile(depInfo->getValue())) {
-      diagnostics << "warning: " << ec.message()
-                  << ", processing '-dependency_info "
-                  << depInfo->getValue()
-                  << "'\n";
-    }
-  }
+  if (llvm::opt::Arg *depInfo = parsedArgs.getLastArg(OPT_dependency_info))
+    if (std::error_code ec = ctx.createDependencyFile(depInfo->getValue()))
+      warn(ec.message() + ", processing '-dependency_info " +
+           depInfo->getValue());
 
   // In -test_file_usage mode, we'll be given an explicit list of paths that
   // exist. We'll also be expected to print out information about how we located
@@ -639,31 +635,28 @@
 
   // Now that we've constructed the final set of search paths, print out those
   // search paths in verbose mode.
-  if (parsedArgs.getLastArg(OPT_v)) {
-    diagnostics << "Library search paths:\n";
+  if (errorHandler().Verbose) {
+    message("Library search paths:");
     for (auto path : ctx.searchDirs()) {
-      diagnostics << "    " << path << '\n';
+      message("    " + path);
     }
-    diagnostics << "Framework search paths:\n";
+    message("Framework search paths:");
     for (auto path : ctx.frameworkDirs()) {
-      diagnostics << "    " << path << '\n';
+      message("    " + path);
     }
   }
 
   // Handle -exported_symbols_list <file>
   for (auto expFile : parsedArgs.filtered(OPT_exported_symbols_list)) {
     if (ctx.exportMode() == MachOLinkingContext::ExportMode::blackList) {
-      diagnostics << "error: -exported_symbols_list cannot be combined "
-                  << "with -unexported_symbol[s_list]\n";
-       return false;
+      error("-exported_symbols_list cannot be combined with "
+            "-unexported_symbol[s_list]");
+      return false;
     }
     ctx.setExportMode(MachOLinkingContext::ExportMode::whiteList);
-    if (std::error_code ec = parseExportsList(expFile->getValue(), ctx,
-                                              diagnostics)) {
-      diagnostics << "error: " << ec.message()
-                  << ", processing '-exported_symbols_list "
-                  << expFile->getValue()
-                  << "'\n";
+    if (std::error_code ec = parseExportsList(expFile->getValue(), ctx)) {
+      error(ec.message() + ", processing '-exported_symbols_list " +
+            expFile->getValue());
       return false;
     }
   }
@@ -671,9 +664,9 @@
   // Handle -exported_symbol <symbol>
   for (auto symbol : parsedArgs.filtered(OPT_exported_symbol)) {
     if (ctx.exportMode() == MachOLinkingContext::ExportMode::blackList) {
-      diagnostics << "error: -exported_symbol cannot be combined "
-                  << "with -unexported_symbol[s_list]\n";
-       return false;
+      error("-exported_symbol cannot be combined with "
+            "-unexported_symbol[s_list]");
+      return false;
     }
     ctx.setExportMode(MachOLinkingContext::ExportMode::whiteList);
     ctx.addExportSymbol(symbol->getValue());
@@ -682,17 +675,14 @@
   // Handle -unexported_symbols_list <file>
   for (auto expFile : parsedArgs.filtered(OPT_unexported_symbols_list)) {
     if (ctx.exportMode() == MachOLinkingContext::ExportMode::whiteList) {
-      diagnostics << "error: -unexported_symbols_list cannot be combined "
-                  << "with -exported_symbol[s_list]\n";
-       return false;
+      error("-unexported_symbols_list cannot be combined with "
+            "-exported_symbol[s_list]");
+      return false;
     }
     ctx.setExportMode(MachOLinkingContext::ExportMode::blackList);
-    if (std::error_code ec = parseExportsList(expFile->getValue(), ctx,
-                                              diagnostics)) {
-      diagnostics << "error: " << ec.message()
-                  << ", processing '-unexported_symbols_list "
-                  << expFile->getValue()
-                  << "'\n";
+    if (std::error_code ec = parseExportsList(expFile->getValue(), ctx)) {
+      error(ec.message() + ", processing '-unexported_symbols_list " +
+            expFile->getValue());
       return false;
     }
   }
@@ -700,9 +690,9 @@
   // Handle -unexported_symbol <symbol>
   for (auto symbol : parsedArgs.filtered(OPT_unexported_symbol)) {
     if (ctx.exportMode() == MachOLinkingContext::ExportMode::whiteList) {
-      diagnostics << "error: -unexported_symbol cannot be combined "
-                  << "with -exported_symbol[s_list]\n";
-       return false;
+      error("-unexported_symbol cannot be combined with "
+            "-exported_symbol[s_list]");
+      return false;
     }
     ctx.setExportMode(MachOLinkingContext::ExportMode::blackList);
     ctx.addExportSymbol(symbol->getValue());
@@ -711,30 +701,26 @@
   // Handle obosolete -multi_module and -single_module
   if (llvm::opt::Arg *mod =
           parsedArgs.getLastArg(OPT_multi_module, OPT_single_module)) {
-    if (mod->getOption().getID() == OPT_multi_module) {
-      diagnostics << "warning: -multi_module is obsolete and being ignored\n";
-    }
-    else {
-      if (ctx.outputMachOType() != llvm::MachO::MH_DYLIB) {
-        diagnostics << "warning: -single_module being ignored. "
-                       "It is only for use when producing a dylib\n";
-      }
-    }
+    if (mod->getOption().getID() == OPT_multi_module)
+      warn("-multi_module is obsolete and being ignored");
+    else if (ctx.outputMachOType() != llvm::MachO::MH_DYLIB)
+      warn("-single_module being ignored. It is only for use when producing a "
+           "dylib");
   }
 
   // Handle obsolete ObjC options: -objc_gc_compaction, -objc_gc, -objc_gc_only
   if (parsedArgs.getLastArg(OPT_objc_gc_compaction)) {
-    diagnostics << "error: -objc_gc_compaction is not supported\n";
+    error("-objc_gc_compaction is not supported");
     return false;
   }
 
   if (parsedArgs.getLastArg(OPT_objc_gc)) {
-    diagnostics << "error: -objc_gc is not supported\n";
+    error("-objc_gc is not supported");
     return false;
   }
 
   if (parsedArgs.getLastArg(OPT_objc_gc_only)) {
-    diagnostics << "error: -objc_gc_only is not supported\n";
+    error("-objc_gc_only is not supported");
     return false;
   }
 
@@ -746,22 +732,20 @@
       case MachOLinkingContext::OS::macOSX:
         if ((minOSVersion < 0x000A0500) &&
             (pie->getOption().getID() == OPT_pie)) {
-          diagnostics << "-pie can only be used when targeting "
-                         "Mac OS X 10.5 or later\n";
+          error("-pie can only be used when targeting Mac OS X 10.5 or later");
           return false;
         }
         break;
       case MachOLinkingContext::OS::iOS:
         if ((minOSVersion < 0x00040200) &&
             (pie->getOption().getID() == OPT_pie)) {
-          diagnostics << "-pie can only be used when targeting "
-                         "iOS 4.2 or later\n";
+          error("-pie can only be used when targeting iOS 4.2 or later");
           return false;
         }
         break;
       case MachOLinkingContext::OS::iOS_simulator:
         if (pie->getOption().getID() == OPT_no_pie) {
-          diagnostics << "iOS simulator programs must be built PIE\n";
+          error("iOS simulator programs must be built PIE");
           return false;
         }
         break;
@@ -774,12 +758,12 @@
       break;
     case llvm::MachO::MH_DYLIB:
     case llvm::MachO::MH_BUNDLE:
-      diagnostics << "warning: " << pie->getSpelling() << " being ignored. "
-                  << "It is only used when linking main executables\n";
+      warn(pie->getSpelling() +
+           " being ignored. It is only used when linking main executables");
       break;
     default:
-      diagnostics << pie->getSpelling()
-                  << " can only used when linking main executables\n";
+      error(pie->getSpelling() +
+            " can only used when linking main executables");
       return false;
     }
   }
@@ -934,7 +918,7 @@
     uint32_t sdkVersion = 0;
     if (MachOLinkingContext::parsePackedVersion(arg->getValue(),
                                                 sdkVersion)) {
-      diagnostics << "error: malformed sdkVersion value\n";
+      error("malformed sdkVersion value");
       return false;
     }
     ctx.setSdkVersion(sdkVersion);
@@ -943,9 +927,8 @@
     // with min_version, then we need to give an warning as we have no sdk
     // version to put in that command.
     // FIXME: We need to decide whether to make this an error.
-    diagnostics << "warning: -sdk_version is required when emitting "
-                   "min version load command.  "
-                   "Setting sdk version to match provided min version\n";
+    warn("-sdk_version is required when emitting min version load command.  "
+         "Setting sdk version to match provided min version");
     ctx.setSdkVersion(ctx.osMinVersion());
   }
 
@@ -954,7 +937,7 @@
     uint64_t version = 0;
     if (MachOLinkingContext::parsePackedVersion(arg->getValue(),
                                                 version)) {
-      diagnostics << "error: malformed source_version value\n";
+      error("malformed source_version value");
       return false;
     }
     ctx.setSourceVersion(version);
@@ -964,12 +947,12 @@
   if (llvm::opt::Arg *stackSize = parsedArgs.getLastArg(OPT_stack_size)) {
     uint64_t stackSizeVal;
     if (parseNumberBase16(stackSize->getValue(), stackSizeVal)) {
-      diagnostics << "error: stack_size expects a hex number\n";
+      error("stack_size expects a hex number");
       return false;
     }
     if ((stackSizeVal % ctx.pageSize()) != 0) {
-      diagnostics << "error: stack_size must be a multiple of page size ("
-                  << "0x" << llvm::utohexstr(ctx.pageSize()) << ")\n";
+      error("stack_size must be a multiple of page size (0x" +
+            llvm::utohexstr(ctx.pageSize()) + ")");
       return false;
     }
 
@@ -982,12 +965,9 @@
 
   // Handle -order_file <file>
   for (auto orderFile : parsedArgs.filtered(OPT_order_file)) {
-    if (std::error_code ec = parseOrderFile(orderFile->getValue(), ctx,
-                                              diagnostics)) {
-      diagnostics << "error: " << ec.message()
-                  << ", processing '-order_file "
-                  << orderFile->getValue()
-                  << "'\n";
+    if (std::error_code ec = parseOrderFile(orderFile->getValue(), ctx)) {
+      error(ec.message() + ", processing '-order_file " + orderFile->getValue()
+            + "'");
       return false;
     }
   }
@@ -1011,8 +991,8 @@
     else if (StringRef(undef->getValue()).equals("dynamic_lookup"))
       UndefMode = MachOLinkingContext::UndefinedMode::dynamicLookup;
     else {
-      diagnostics << "error: invalid option to -undefined "
-                     "[ warning | error | suppress | dynamic_lookup ]\n";
+      error("invalid option to -undefined [ warning | error | suppress | "
+            "dynamic_lookup ]");
       return false;
     }
 
@@ -1026,8 +1006,8 @@
       // illegal. Emit a diagnostic if they've been (mis)used.
       if (UndefMode == MachOLinkingContext::UndefinedMode::warning ||
           UndefMode == MachOLinkingContext::UndefinedMode::suppress) {
-        diagnostics << "error: can't use -undefined warning or suppress with "
-                       "-twolevel_namespace\n";
+        error("can't use -undefined warning or suppress with "
+              "-twolevel_namespace");
         return false;
       }
     }
@@ -1046,19 +1026,16 @@
       case llvm::MachO::MH_DYLIB:
       case llvm::MachO::MH_BUNDLE:
         if (!ctx.minOS("10.5", "2.0")) {
-          if (ctx.os() == MachOLinkingContext::OS::macOSX) {
-            diagnostics << "error: -rpath can only be used when targeting "
-                           "OS X 10.5 or later\n";
-          } else {
-            diagnostics << "error: -rpath can only be used when targeting "
-                           "iOS 2.0 or later\n";
-          }
+          if (ctx.os() == MachOLinkingContext::OS::macOSX)
+            error("-rpath can only be used when targeting OS X 10.5 or later");
+          else
+            error("-rpath can only be used when targeting iOS 2.0 or later");
           return false;
         }
         break;
       default:
-        diagnostics << "error: -rpath can only be used when creating "
-                       "a dynamic final linked image\n";
+        error("-rpath can only be used when creating a dynamic final linked "
+              "image");
         return false;
     }
 
@@ -1079,52 +1056,46 @@
     default:
       continue;
     case OPT_INPUT:
-      addFile(arg->getValue(), ctx, globalWholeArchive, false, diagnostics);
+      addFile(arg->getValue(), ctx, globalWholeArchive, false);
       break;
     case OPT_upward_library:
-      addFile(arg->getValue(), ctx, false, true, diagnostics);
+      addFile(arg->getValue(), ctx, false, true);
       break;
     case OPT_force_load:
-      addFile(arg->getValue(), ctx, true, false, diagnostics);
+      addFile(arg->getValue(), ctx, true, false);
       break;
     case OPT_l:
     case OPT_upward_l:
       upward = (arg->getOption().getID() == OPT_upward_l);
       resolvedPath = ctx.searchLibrary(arg->getValue());
       if (!resolvedPath) {
-        diagnostics << "Unable to find library for " << arg->getSpelling()
-                    << arg->getValue() << "\n";
+        error("Unable to find library for " + arg->getSpelling() +
+              arg->getValue());
         return false;
       } else if (ctx.testingFileUsage()) {
-        diagnostics << "Found " << (upward ? "upward " : " ") << "library "
-                   << canonicalizePath(resolvedPath.getValue()) << '\n';
+        message(Twine("Found ") + (upward ? "upward " : " ") + "library " +
+                canonicalizePath(resolvedPath.getValue()));
       }
-      addFile(resolvedPath.getValue(), ctx, globalWholeArchive,
-              upward, diagnostics);
+      addFile(resolvedPath.getValue(), ctx, globalWholeArchive, upward);
       break;
     case OPT_framework:
     case OPT_upward_framework:
       upward = (arg->getOption().getID() == OPT_upward_framework);
       resolvedPath = ctx.findPathForFramework(arg->getValue());
       if (!resolvedPath) {
-        diagnostics << "Unable to find framework for "
-                    << arg->getSpelling() << " " << arg->getValue() << "\n";
+        error("Unable to find framework for " + arg->getSpelling() + " " +
+              arg->getValue());
         return false;
       } else if (ctx.testingFileUsage()) {
-        diagnostics << "Found " << (upward ? "upward " : " ") << "framework "
-                    << canonicalizePath(resolvedPath.getValue()) << '\n';
+        message(Twine("Found ") + (upward ? "upward " : " ") + "framework " +
+                canonicalizePath(resolvedPath.getValue()));
       }
-      addFile(resolvedPath.getValue(), ctx, globalWholeArchive,
-              upward, diagnostics);
+      addFile(resolvedPath.getValue(), ctx, globalWholeArchive, upward);
       break;
     case OPT_filelist:
-      if (auto ec = loadFileList(arg->getValue(),
-                                 ctx, globalWholeArchive,
-                                 diagnostics)) {
+      if (auto ec = loadFileList(arg->getValue(), ctx, globalWholeArchive)) {
         handleAllErrors(std::move(ec), [&](const llvm::ErrorInfoBase &EI) {
-          diagnostics << "error: ";
-          EI.log(diagnostics);
-          diagnostics << ", processing '-filelist " << arg->getValue() << "'\n";
+          error(EI.message() + ", processing '-filelist " + arg->getValue());
         });
         return false;
       }
@@ -1138,7 +1109,7 @@
           MemoryBuffer::getFile(fileName);
 
         if (!contentOrErr) {
-          diagnostics << "error: can't open -sectcreate file " << fileName << "\n";
+          error("can't open -sectcreate file " + Twine(fileName));
           return false;
         }
 
@@ -1149,12 +1120,12 @@
   }
 
   if (ctx.getNodes().empty()) {
-    diagnostics << "No input files\n";
+    error("No input files");
     return false;
   }
 
   // Validate the combination of options used.
-  return ctx.validate(diagnostics);
+  return ctx.validate();
 }
 
 static void createFiles(MachOLinkingContext &ctx, bool Implicit) {
@@ -1170,9 +1141,18 @@
 }
 
 /// This is where the link is actually performed.
-bool link(llvm::ArrayRef<const char *> args, raw_ostream &diagnostics) {
+bool link(llvm::ArrayRef<const char *> args, bool CanExitEarly,
+          raw_ostream &Error) {
+  errorHandler().LogName = llvm::sys::path::filename(args[0]);
+  errorHandler().ErrorLimitExceededMsg =
+      "too many errors emitted, stopping now (use "
+      "'-error-limit 0' to see all errors)";
+  errorHandler().ErrorOS = &Error;
+  errorHandler().ExitEarly = CanExitEarly;
+  errorHandler().ColorDiagnostics = Error.has_colors();
+
   MachOLinkingContext ctx;
-  if (!parse(args, ctx, diagnostics))
+  if (!parse(args, ctx))
     return false;
   if (ctx.doNothing())
     return true;
@@ -1214,9 +1194,10 @@
   if (auto ec = pm.runOnFile(*merged)) {
     // FIXME: This should be passed to logAllUnhandledErrors but it needs
     // to be passed a Twine instead of a string.
-    diagnostics << "Failed to run passes on file '" << ctx.outputPath()
-                << "': ";
-    logAllUnhandledErrors(std::move(ec), diagnostics, std::string());
+    *errorHandler().ErrorOS << "Failed to run passes on file '"
+                            << ctx.outputPath() << "': ";
+    logAllUnhandledErrors(std::move(ec), *errorHandler().ErrorOS,
+                          std::string());
     return false;
   }
 
@@ -1227,11 +1208,18 @@
   if (auto ec = ctx.writeFile(*merged)) {
     // FIXME: This should be passed to logAllUnhandledErrors but it needs
     // to be passed a Twine instead of a string.
-    diagnostics << "Failed to write file '" << ctx.outputPath() << "': ";
-    logAllUnhandledErrors(std::move(ec), diagnostics, std::string());
+    *errorHandler().ErrorOS << "Failed to write file '" << ctx.outputPath()
+                            << "': ";
+    logAllUnhandledErrors(std::move(ec), *errorHandler().ErrorOS,
+                          std::string());
     return false;
   }
 
+  // Call exit() if we can to avoid calling destructors.
+  if (CanExitEarly)
+    exitLld(errorCount() ? 1 : 0);
+
+
   return true;
 }
 
diff --git a/lib/Driver/DarwinLdOptions.td b/lib/Driver/DarwinLdOptions.td
index fa07f33..3bbde8b 100644
--- a/lib/Driver/DarwinLdOptions.td
+++ b/lib/Driver/DarwinLdOptions.td
@@ -227,6 +227,14 @@
      HelpText<"Print the names of the input files as ld processes them">;
 def v : Flag<["-"], "v">,
      HelpText<"Print linker information">;
+def error_limit : Separate<["-", "--"], "error-limit">,
+     MetaVarName<"<number>">,
+     HelpText<"Maximum number of errors to emit before stopping (0 = no limit)">;
+
+// Ignored options
+def lto_library : Separate<["-"], "lto_library">,
+    MetaVarName<"<path>">,
+    HelpText<"Ignored for compatibility with other linkers">;
 
 // Obsolete options
 def grp_obsolete : OptionGroup<"obsolete">, HelpText<"OBSOLETE OPTIONS">;
diff --git a/lib/ReaderWriter/MachO/CMakeLists.txt b/lib/ReaderWriter/MachO/CMakeLists.txt
index f2fc347..37d1de4 100644
--- a/lib/ReaderWriter/MachO/CMakeLists.txt
+++ b/lib/ReaderWriter/MachO/CMakeLists.txt
@@ -26,6 +26,7 @@
     Support
 
   LINK_LIBS
+    lldCommon
     lldCore
     lldYAML
     ${LLVM_PTHREAD_LIB}
diff --git a/lib/ReaderWriter/MachO/MachOLinkingContext.cpp b/lib/ReaderWriter/MachO/MachOLinkingContext.cpp
index 4ef7a62..ce423d0 100644
--- a/lib/ReaderWriter/MachO/MachOLinkingContext.cpp
+++ b/lib/ReaderWriter/MachO/MachOLinkingContext.cpp
@@ -7,6 +7,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "lld/Common/ErrorHandler.h"
 #include "lld/ReaderWriter/MachOLinkingContext.h"
 #include "ArchHandler.h"
 #include "File.h"
@@ -579,29 +580,26 @@
   return llvm::None;
 }
 
-bool MachOLinkingContext::validateImpl(raw_ostream &diagnostics) {
+bool MachOLinkingContext::validateImpl() {
   // TODO: if -arch not specified, look at arch of first .o file.
 
   if (_currentVersion && _outputMachOType != MH_DYLIB) {
-    diagnostics << "error: -current_version can only be used with dylibs\n";
+    error("-current_version can only be used with dylibs");
     return false;
   }
 
   if (_compatibilityVersion && _outputMachOType != MH_DYLIB) {
-    diagnostics
-        << "error: -compatibility_version can only be used with dylibs\n";
+    error("-compatibility_version can only be used with dylibs");
     return false;
   }
 
   if (_deadStrippableDylib && _outputMachOType != MH_DYLIB) {
-    diagnostics
-        << "error: -mark_dead_strippable_dylib can only be used with dylibs.\n";
+    error("-mark_dead_strippable_dylib can only be used with dylibs");
     return false;
   }
 
   if (!_bundleLoader.empty() && outputMachOType() != MH_BUNDLE) {
-    diagnostics
-        << "error: -bundle_loader can only be used with Mach-O bundles\n";
+    error("-bundle_loader can only be used with Mach-O bundles");
     return false;
   }
 
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index e5284ca..12fb7c3 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -32,9 +32,9 @@
 set(LLD_TEST_DEPS lld)
 if (NOT LLD_BUILT_STANDALONE)
   list(APPEND LLD_TEST_DEPS
-    FileCheck count not llvm-ar llvm-as llvm-dis llvm-dwarfdump llvm-nm
-    llc llvm-config llvm-objdump llvm-readelf llvm-readobj yaml2obj obj2yaml
-    llvm-mc llvm-lib llvm-pdbutil opt llvm-bcanalyzer
+    FileCheck count llc llvm-ar llvm-as llvm-bcanalyzer llvm-config llvm-dis
+    llvm-dwarfdump llvm-lib llvm-mc llvm-nm llvm-objcopy llvm-objdump
+    llvm-pdbutil llvm-readelf llvm-readobj not obj2yaml opt yaml2obj
     )
 endif()
 
diff --git a/test/COFF/Inputs/associative-comdat-mingw-2.s b/test/COFF/Inputs/associative-comdat-mingw-2.s
new file mode 100644
index 0000000..edb6a82
--- /dev/null
+++ b/test/COFF/Inputs/associative-comdat-mingw-2.s
@@ -0,0 +1,34 @@
+        .section        .xdata$foo,"dr"
+        .linkonce       discard
+        .p2align        3
+        .long           42
+
+        .section        .xdata$bar,"dr"
+        .linkonce       discard
+        .p2align        3
+        .long           43
+
+        .section        .xdata$baz,"dr"
+        .linkonce       discard
+        .p2align        3
+        .long           44
+
+        .def            foo;
+        .scl            2;
+        .type           32;
+        .endef
+        .section        .text$foo,"xr",discard,foo
+        .globl          foo
+        .p2align        4
+foo:
+        ret
+
+        .def            bar;
+        .scl            2;
+        .type           32;
+        .endef
+        .section        .text$bar,"xr",discard,bar
+        .globl          bar
+        .p2align        4
+bar:
+        ret
diff --git a/test/COFF/Inputs/common-replacement.s b/test/COFF/Inputs/common-replacement.s
new file mode 100644
index 0000000..eaaeee2
--- /dev/null
+++ b/test/COFF/Inputs/common-replacement.s
@@ -0,0 +1,5 @@
+        .globl          foo
+        .data
+        .p2align        2, 0
+foo:
+        .long           42
diff --git a/test/COFF/Inputs/globals-dia-func-collision3.obj b/test/COFF/Inputs/globals-dia-func-collision3.obj
new file mode 100644
index 0000000..ce9b873
--- /dev/null
+++ b/test/COFF/Inputs/globals-dia-func-collision3.obj
Binary files differ
diff --git a/test/COFF/Inputs/globals-dia-vfunc-collision.obj b/test/COFF/Inputs/globals-dia-vfunc-collision.obj
new file mode 100644
index 0000000..3191c3e
--- /dev/null
+++ b/test/COFF/Inputs/globals-dia-vfunc-collision.obj
Binary files differ
diff --git a/test/COFF/Inputs/globals-dia-vfunc-collision2.obj b/test/COFF/Inputs/globals-dia-vfunc-collision2.obj
new file mode 100644
index 0000000..f406102
--- /dev/null
+++ b/test/COFF/Inputs/globals-dia-vfunc-collision2.obj
Binary files differ
diff --git a/test/COFF/Inputs/globals-dia-vfunc-simple.obj b/test/COFF/Inputs/globals-dia-vfunc-simple.obj
new file mode 100644
index 0000000..f0a9b4d
--- /dev/null
+++ b/test/COFF/Inputs/globals-dia-vfunc-simple.obj
Binary files differ
diff --git a/test/COFF/Inputs/guardcf-align-foobar.yaml b/test/COFF/Inputs/guardcf-align-foobar.yaml
new file mode 100644
index 0000000..7878c45
--- /dev/null
+++ b/test/COFF/Inputs/guardcf-align-foobar.yaml
@@ -0,0 +1,51 @@
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:            .text.foo
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       1
+    SectionData:     31C0C3
+  - Name:            .text.bar
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       1
+    SectionData:     FFE1
+symbols:
+  - Name:            .text.foo
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          3
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        3963538403
+      Number:          1
+  - Name:            .text.bar
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          2
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        1143549852
+      Number:          2
+  - Name:            foo
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            bar
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...
diff --git a/test/COFF/Inputs/otherFunc.s b/test/COFF/Inputs/otherFunc.s
new file mode 100644
index 0000000..ae8b922
--- /dev/null
+++ b/test/COFF/Inputs/otherFunc.s
@@ -0,0 +1,7 @@
+.global otherFunc
+.global MessageBoxA
+.text
+otherFunc:
+  ret
+MessageBoxA:
+  ret
diff --git a/test/COFF/Inputs/pdb_lines_1_relative.yaml b/test/COFF/Inputs/pdb_lines_1_relative.yaml
new file mode 100644
index 0000000..947de41
--- /dev/null
+++ b/test/COFF/Inputs/pdb_lines_1_relative.yaml
@@ -0,0 +1,401 @@
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       16
+    SectionData:     4883EC28C744242400000000E800000000B82A0000004883C428C3
+    Relocations:
+      - VirtualAddress:  13
+        SymbolName:      foo
+        Type:            IMAGE_REL_AMD64_REL32
+  - Name:            .data
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+    Alignment:       4
+    SectionData:     ''
+  - Name:            .bss
+    Characteristics: [ IMAGE_SCN_CNT_UNINITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+    Alignment:       4
+    SectionData:     ''
+  - Name:            .xdata
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     '0104010004420000'
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       16
+    SectionData:     4883EC28E800000000904883C428C3
+    Relocations:
+      - VirtualAddress:  5
+        SymbolName:      bar
+        Type:            IMAGE_REL_AMD64_REL32
+  - Name:            .drectve
+    Characteristics: [ IMAGE_SCN_LNK_INFO, IMAGE_SCN_LNK_REMOVE ]
+    Alignment:       1
+    SectionData:     202F44454641554C544C49423A6C6962636D742E6C6962202F44454641554C544C49423A6F6C646E616D65732E6C6962
+  - Name:            '.debug$S'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     04000000F10000002F0000002D003C1100000000D0000700000000000000581B000000000000636C616E672076657273696F6E20372E302E30200000F1000000300000002A0047110000000000000000000000001B000000000000000000000002100000000000000000006D61696E0002004F11F20000003000000000000000000000001B00000000000000030000002400000000000000020000000C000000030000001100000004000000F400000030000000010000001001EA6429BCE282CCF3F0E3CD93B216EB410000110000001001061EB73ABB642532857A4F1D9CBAC3230000F30000001C000000002E5C7064625F6C696E65735F312E63002E5C666F6F2E6800000000
+    Subsections:
+      - !Symbols
+        Records:
+          - Kind:            S_COMPILE3
+            Compile3Sym:
+              Flags:           [  ]
+              Machine:         X64
+              FrontendMajor:   7
+              FrontendMinor:   0
+              FrontendBuild:   0
+              FrontendQFE:     0
+              BackendMajor:    7000
+              BackendMinor:    0
+              BackendBuild:    0
+              BackendQFE:      0
+              Version:         'clang version 7.0.0 '
+      - !Symbols
+        Records:
+          - Kind:            S_GPROC32_ID
+            ProcSym:
+              CodeSize:        27
+              DbgStart:        0
+              DbgEnd:          0
+              FunctionType:    4098
+              Flags:           [  ]
+              DisplayName:     main
+          - Kind:            S_PROC_ID_END
+            ScopeEndSym:
+      - !Lines
+        CodeSize:        27
+        Flags:           [  ]
+        RelocOffset:     0
+        RelocSegment:    0
+        Blocks:
+          - FileName:        '.\pdb_lines_1.c'
+            Lines:
+              - Offset:          0
+                LineStart:       2
+                IsStatement:     false
+                EndDelta:        0
+              - Offset:          12
+                LineStart:       3
+                IsStatement:     false
+                EndDelta:        0
+              - Offset:          17
+                LineStart:       4
+                IsStatement:     false
+                EndDelta:        0
+            Columns:
+      - !FileChecksums
+        Checksums:
+          - FileName:        '.\pdb_lines_1.c'
+            Kind:            MD5
+            Checksum:        EA6429BCE282CCF3F0E3CD93B216EB41
+          - FileName:        '.\foo.h'
+            Kind:            MD5
+            Checksum:        061EB73ABB642532857A4F1D9CBAC323
+      - !StringTable
+        Strings:
+          - '.\pdb_lines_1.c'
+          - '.\foo.h'
+          - ''
+          - ''
+          - ''
+    Relocations:
+      - VirtualAddress:  100
+        SymbolName:      main
+        Type:            IMAGE_REL_AMD64_SECREL
+      - VirtualAddress:  104
+        SymbolName:      main
+        Type:            IMAGE_REL_AMD64_SECTION
+      - VirtualAddress:  124
+        SymbolName:      main
+        Type:            IMAGE_REL_AMD64_SECREL
+      - VirtualAddress:  128
+        SymbolName:      main
+        Type:            IMAGE_REL_AMD64_SECTION
+  - Name:            '.debug$T'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     0400000006000112000000000E0008107400000000000000001000001200011600000000011000006D61696E00F3F2F10E0008100300000000000000001000000E0001160000000003100000666F6F00
+    Types:
+      - Kind:            LF_ARGLIST
+        ArgList:
+          ArgIndices:      [  ]
+      - Kind:            LF_PROCEDURE
+        Procedure:
+          ReturnType:      116
+          CallConv:        NearC
+          Options:         [ None ]
+          ParameterCount:  0
+          ArgumentList:    4096
+      - Kind:            LF_FUNC_ID
+        FuncId:
+          ParentScope:     0
+          FunctionType:    4097
+          Name:            main
+      - Kind:            LF_PROCEDURE
+        Procedure:
+          ReturnType:      3
+          CallConv:        NearC
+          Options:         [ None ]
+          ParameterCount:  0
+          ArgumentList:    4096
+      - Kind:            LF_FUNC_ID
+        FuncId:
+          ParentScope:     0
+          FunctionType:    4099
+          Name:            foo
+  - Name:            .pdata
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     000000001B00000000000000
+    Relocations:
+      - VirtualAddress:  0
+        SymbolName:      main
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  4
+        SymbolName:      main
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  8
+        SymbolName:      .xdata
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+  - Name:            .xdata
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     '0104010004420000'
+  - Name:            '.debug$S'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     04000000F10000002F000000290047110000000000000000000000000F00000000000000000000000410000000000000000000666F6F0002004F1100F20000003000000000000000000000000F000000180000000300000024000000000000000200000004000000030000000900000004000000
+    Subsections:
+      - !Symbols
+        Records:
+          - Kind:            S_GPROC32_ID
+            ProcSym:
+              CodeSize:        15
+              DbgStart:        0
+              DbgEnd:          0
+              FunctionType:    4100
+              Flags:           [  ]
+              DisplayName:     foo
+          - Kind:            S_PROC_ID_END
+            ScopeEndSym:
+      - !Lines
+        CodeSize:        15
+        Flags:           [  ]
+        RelocOffset:     0
+        RelocSegment:    0
+        Blocks:
+          - FileName:        '.\foo.h'
+            Lines:
+              - Offset:          0
+                LineStart:       2
+                IsStatement:     false
+                EndDelta:        0
+              - Offset:          4
+                LineStart:       3
+                IsStatement:     false
+                EndDelta:        0
+              - Offset:          9
+                LineStart:       4
+                IsStatement:     false
+                EndDelta:        0
+            Columns:
+    Relocations:
+      - VirtualAddress:  44
+        SymbolName:      foo
+        Type:            IMAGE_REL_AMD64_SECREL
+      - VirtualAddress:  48
+        SymbolName:      foo
+        Type:            IMAGE_REL_AMD64_SECTION
+      - VirtualAddress:  68
+        SymbolName:      foo
+        Type:            IMAGE_REL_AMD64_SECREL
+      - VirtualAddress:  72
+        SymbolName:      foo
+        Type:            IMAGE_REL_AMD64_SECTION
+  - Name:            .pdata
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     000000000F00000000000000
+    Relocations:
+      - VirtualAddress:  0
+        SymbolName:      foo
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  4
+        SymbolName:      foo
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+      - VirtualAddress:  8
+        SymbolName:      .xdata
+        Type:            IMAGE_REL_AMD64_ADDR32NB
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          27
+      NumberOfRelocations: 1
+      NumberOfLinenumbers: 0
+      CheckSum:        3051916600
+      Number:          1
+  - Name:            .data
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          2
+  - Name:            .bss
+    Value:           0
+    SectionNumber:   3
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          3
+  - Name:            .xdata
+    Value:           0
+    SectionNumber:   4
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          8
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        264583633
+      Number:          4
+  - Name:            .text
+    Value:           0
+    SectionNumber:   5
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          15
+      NumberOfRelocations: 1
+      NumberOfLinenumbers: 0
+      CheckSum:        236440503
+      Number:          5
+      Selection:       IMAGE_COMDAT_SELECT_ANY
+  - Name:            foo
+    Value:           0
+    SectionNumber:   5
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            .xdata
+    Value:           0
+    SectionNumber:   10
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          8
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        264583633
+      Number:          5
+      Selection:       IMAGE_COMDAT_SELECT_ASSOCIATIVE
+  - Name:            .drectve
+    Value:           0
+    SectionNumber:   6
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          48
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        149686238
+      Number:          6
+  - Name:            '.debug$S'
+    Value:           0
+    SectionNumber:   7
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          264
+      NumberOfRelocations: 4
+      NumberOfLinenumbers: 0
+      CheckSum:        2204933783
+      Number:          7
+  - Name:            '.debug$S'
+    Value:           0
+    SectionNumber:   11
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          116
+      NumberOfRelocations: 4
+      NumberOfLinenumbers: 0
+      CheckSum:        2691661839
+      Number:          5
+      Selection:       IMAGE_COMDAT_SELECT_ASSOCIATIVE
+  - Name:            '.debug$T'
+    Value:           0
+    SectionNumber:   8
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          80
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        3541780432
+      Number:          8
+  - Name:            .pdata
+    Value:           0
+    SectionNumber:   9
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          12
+      NumberOfRelocations: 3
+      NumberOfLinenumbers: 0
+      CheckSum:        567356797
+      Number:          9
+  - Name:            .pdata
+    Value:           0
+    SectionNumber:   12
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          12
+      NumberOfRelocations: 3
+      NumberOfLinenumbers: 0
+      CheckSum:        3642757804
+      Number:          5
+      Selection:       IMAGE_COMDAT_SELECT_ASSOCIATIVE
+  - Name:            main
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            bar
+    Value:           0
+    SectionNumber:   0
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...
diff --git a/test/COFF/Inputs/pdb_lines_2_relative.yaml b/test/COFF/Inputs/pdb_lines_2_relative.yaml
new file mode 100644
index 0000000..1b051d8
--- /dev/null
+++ b/test/COFF/Inputs/pdb_lines_2_relative.yaml
@@ -0,0 +1,190 @@
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: [  ]
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       16
+    SectionData:     C3
+  - Name:            .data
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+    Alignment:       4
+    SectionData:     ''
+  - Name:            .bss
+    Characteristics: [ IMAGE_SCN_CNT_UNINITIALIZED_DATA, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+    Alignment:       4
+    SectionData:     ''
+  - Name:            .drectve
+    Characteristics: [ IMAGE_SCN_LNK_INFO, IMAGE_SCN_LNK_REMOVE ]
+    Alignment:       1
+    SectionData:     202F44454641554C544C49423A6C6962636D742E6C6962202F44454641554C544C49423A6F6C646E616D65732E6C6962
+  - Name:            '.debug$S'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     04000000F10000002F0000002D003C1100000000D0000700000000000000581B000000000000636C616E672076657273696F6E20372E302E30200000F10000002F0000002900471100000000000000000000000001000000000000000000000002100000000000000000006261720002004F1100F2000000200000000000000000000000010000000000000001000000140000000000000002000000F400000018000000010000001001DF91CB3A2B8D917486574BB50CAC4CC70000F300000014000000002E5C7064625F6C696E65735F322E6300000000
+    Subsections:
+      - !Symbols
+        Records:
+          - Kind:            S_COMPILE3
+            Compile3Sym:
+              Flags:           [  ]
+              Machine:         X64
+              FrontendMajor:   7
+              FrontendMinor:   0
+              FrontendBuild:   0
+              FrontendQFE:     0
+              BackendMajor:    7000
+              BackendMinor:    0
+              BackendBuild:    0
+              BackendQFE:      0
+              Version:         'clang version 7.0.0 '
+      - !Symbols
+        Records:
+          - Kind:            S_GPROC32_ID
+            ProcSym:
+              CodeSize:        1
+              DbgStart:        0
+              DbgEnd:          0
+              FunctionType:    4098
+              Flags:           [  ]
+              DisplayName:     bar
+          - Kind:            S_PROC_ID_END
+            ScopeEndSym:
+      - !Lines
+        CodeSize:        1
+        Flags:           [  ]
+        RelocOffset:     0
+        RelocSegment:    0
+        Blocks:
+          - FileName:        '.\pdb_lines_2.c'
+            Lines:
+              - Offset:          0
+                LineStart:       2
+                IsStatement:     false
+                EndDelta:        0
+            Columns:
+      - !FileChecksums
+        Checksums:
+          - FileName:        '.\pdb_lines_2.c'
+            Kind:            MD5
+            Checksum:        DF91CB3A2B8D917486574BB50CAC4CC7
+      - !StringTable
+        Strings:
+          - '.\pdb_lines_2.c'
+          - ''
+          - ''
+          - ''
+    Relocations:
+      - VirtualAddress:  100
+        SymbolName:      bar
+        Type:            IMAGE_REL_AMD64_SECREL
+      - VirtualAddress:  104
+        SymbolName:      bar
+        Type:            IMAGE_REL_AMD64_SECTION
+      - VirtualAddress:  124
+        SymbolName:      bar
+        Type:            IMAGE_REL_AMD64_SECREL
+      - VirtualAddress:  128
+        SymbolName:      bar
+        Type:            IMAGE_REL_AMD64_SECTION
+  - Name:            '.debug$T'
+    Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_MEM_DISCARDABLE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     0400000006000112000000000E0008100300000000000000001000000E000116000000000110000062617200
+    Types:
+      - Kind:            LF_ARGLIST
+        ArgList:
+          ArgIndices:      [  ]
+      - Kind:            LF_PROCEDURE
+        Procedure:
+          ReturnType:      3
+          CallConv:        NearC
+          Options:         [ None ]
+          ParameterCount:  0
+          ArgumentList:    4096
+      - Kind:            LF_FUNC_ID
+        FuncId:
+          ParentScope:     0
+          FunctionType:    4097
+          Name:            bar
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          1
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        40735498
+      Number:          1
+  - Name:            .data
+    Value:           0
+    SectionNumber:   2
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          2
+  - Name:            .bss
+    Value:           0
+    SectionNumber:   3
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          0
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          3
+  - Name:            .drectve
+    Value:           0
+    SectionNumber:   4
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          48
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        149686238
+      Number:          4
+  - Name:            '.debug$S'
+    Value:           0
+    SectionNumber:   5
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          216
+      NumberOfRelocations: 4
+      NumberOfLinenumbers: 0
+      CheckSum:        2383431754
+      Number:          5
+  - Name:            '.debug$T'
+    Value:           0
+    SectionNumber:   6
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          44
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        179171995
+      Number:          6
+  - Name:            bar
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...
diff --git a/test/COFF/associative-comdat-mingw.s b/test/COFF/associative-comdat-mingw.s
new file mode 100644
index 0000000..09cba9c
--- /dev/null
+++ b/test/COFF/associative-comdat-mingw.s
@@ -0,0 +1,73 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -triple=x86_64-windows-gnu %s -filetype=obj -o %t1.obj
+# RUN: llvm-mc -triple=x86_64-windows-gnu %S/Inputs/associative-comdat-mingw-2.s -filetype=obj -o %t2.obj
+
+# RUN: lld-link -lldmingw -entry:main %t1.obj %t2.obj -out:%t.gc.exe -verbose
+# RUN: llvm-readobj -sections %t.gc.exe | FileCheck %s
+
+# CHECK: Sections [
+# CHECK:   Section {
+# CHECK:     Number: 2
+# CHECK-LABEL:     Name: .rdata (2E 72 64 61 74 61 00 00)
+#             This is the critical check to show that only *one* definition of
+#             .xdata$foo was retained. This *must* be 4.
+#             Make sure that no other .xdata sections get included, which would
+#             increase the size here.
+# CHECK-NEXT:     VirtualSize: 0x4
+
+        .text
+        .def            main;
+        .scl            2;
+        .type           32;
+        .endef
+        .globl          main
+        .p2align        4, 0x90
+main:
+        call            foo
+        retq
+
+# Defines .text$foo (which has a leader symbol and is referenced like
+# normally), and .xdata$foo (which lacks a leader symbol, which normally
+# would be declared associative to the symbol foo).
+# .xdata$foo should be implicitly treated as associative to foo and brought
+# in, while .xdata$bar, implicitly associative to bar, not included, and
+# .xdata$baz not included since there's no symbol baz.
+
+# GNU binutils ld doesn't do this at all, but always includes all .xdata/.pdata
+# comdat sections, even if --gc-sections is used.
+
+        .section        .xdata$foo,"dr"
+        .linkonce       discard
+        .p2align        3
+        .long           42
+
+        .section        .xdata$bar,"dr"
+        .linkonce       discard
+        .p2align        3
+        .long           43
+
+        .section        .xdata$baz,"dr"
+        .linkonce       discard
+        .p2align        3
+        .long           44
+
+        .def            foo;
+        .scl            2;
+        .type           32;
+        .endef
+        .section        .text$foo,"xr",discard,foo
+        .globl          foo
+        .p2align        4
+foo:
+        ret
+
+        .def            bar;
+        .scl            2;
+        .type           32;
+        .endef
+        .section        .text$bar,"xr",discard,bar
+        .globl          bar
+        .p2align        4
+bar:
+        ret
diff --git a/test/COFF/associative-comdat.s b/test/COFF/associative-comdat.s
index a2c3bea..a3d069b 100644
--- a/test/COFF/associative-comdat.s
+++ b/test/COFF/associative-comdat.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -triple=x86_64-windows-msvc %s -filetype=obj -o %t1.obj
 # RUN: llvm-mc -triple=x86_64-windows-msvc %S/Inputs/associative-comdat-2.s -filetype=obj -o %t2.obj
 
diff --git a/test/COFF/common-replacement.s b/test/COFF/common-replacement.s
new file mode 100644
index 0000000..51e31fa
--- /dev/null
+++ b/test/COFF/common-replacement.s
@@ -0,0 +1,35 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -triple=x86_64-windows-gnu %s -filetype=obj -o %t1.obj
+# RUN: llvm-mc -triple=x86_64-windows-gnu %S/Inputs/common-replacement.s -filetype=obj -o %t2.obj
+
+# RUN: lld-link -lldmingw -entry:main %t1.obj %t2.obj -out:%t.exe -verbose 2>&1 \
+# RUN:   | FileCheck -check-prefix VERBOSE %s
+# RUN: llvm-readobj -s %t.exe | FileCheck -check-prefix SECTIONS %s
+
+# VERBOSE: -aligncomm:"foo",2
+
+# As long as the .comm symbol is replaced with actual data, RawDataSize
+# below should be nonzero.
+
+# SECTIONS:         Name: .data (2E 64 61 74 61 00 00 00)
+# SECTIONS-NEXT:    VirtualSize: 0x8
+# SECTIONS-NEXT:    VirtualAddress: 0x2000
+# SECTIONS-NEXT:    RawDataSize: 512
+
+
+        .text
+        .def            main;
+        .scl            2;
+        .type           32;
+        .endef
+        .globl          main
+        .p2align        4, 0x90
+main:
+        movl            foo(%rip), %eax
+        retq
+
+# This produces an aligncomm directive, but when linking in
+# Inputs/common-replacement.s, this symbol is replaced by a normal defined
+# symbol instead.
+        .comm           foo, 4, 2
diff --git a/test/COFF/debug-reloc.s b/test/COFF/debug-reloc.s
new file mode 100644
index 0000000..57ab9bf
--- /dev/null
+++ b/test/COFF/debug-reloc.s
@@ -0,0 +1,58 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -triple=x86_64-windows-gnu %s -filetype=obj -o %t.obj
+
+# RUN: lld-link -lldmingw -debug:dwarf -out:%t.exe -entry:mainfunc -subsystem:console %t.obj
+# RUN: llvm-readobj -sections %t.exe | FileCheck %s -check-prefix SECTIONS
+# RUN: llvm-readobj -coff-basereloc %t.exe | FileCheck %s -check-prefix RELOCS
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck %s -check-prefix HEADERS
+# RUN: llvm-readobj -coff-debug-directory %t.exe | FileCheck %s -check-prefix DEBUG
+
+# SECTIONS:         Number: 2
+# SECTIONS-NEXT:    Name: .buildid (2E 62 75 69 6C 64 69 64)
+# SECTIONS-NEXT:    VirtualSize: 0x35
+# SECTIONS-NEXT:    VirtualAddress: 0x2000
+# SECTIONS:         Number: 3
+# SECTIONS-NEXT:    Name: .data (2E 64 61 74 61 00 00 00)
+# SECTIONS-NEXT:    VirtualSize: 0x8
+# SECTIONS-NEXT:    VirtualAddress: 0x3000
+
+# RELOCS:      BaseReloc [
+# RELOCS-NEXT:   Entry {
+# RELOCS-NEXT:     Type: DIR64
+# RELOCS-NEXT:     Address: 0x3000
+# RELOCS-NEXT:   }
+# RELOCS-NEXT:   Entry {
+# RELOCS-NEXT:     Type: ABSOLUTE
+# RELOCS-NEXT:     Address: 0x3000
+# RELOCS-NEXT:   }
+# RELOCS-NEXT: ]
+
+# HEADERS:     DebugRVA: 0x2000
+# HEADERS:     DebugSize: 0x1C
+
+# DEBUG: DebugDirectory [
+# DEBUG:   DebugEntry {
+# DEBUG:     Type: CodeView (0x2)
+# DEBUG:     SizeOfData: 0x19
+# DEBUG:     AddressOfRawData: 0x201C
+# DEBUG:     PointerToRawData: 0x61C
+
+	.text
+	.def	 mainfunc;
+	.scl	2;
+	.type	32;
+	.endef
+	.globl	mainfunc
+mainfunc:
+.Lfunc_begin0:
+	xorl	%eax, %eax
+	retq
+
+	.data
+	.globl	ptr
+ptr:
+	.quad	mainfunc
+
+	.section	.debug_info,"dr"
+	.quad	.Lfunc_begin0
diff --git a/test/COFF/dll.test b/test/COFF/dll.test
index 9bd8698..f04e28c 100644
--- a/test/COFF/dll.test
+++ b/test/COFF/dll.test
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: yaml2obj < %p/Inputs/export.yaml > %t.obj
 # RUN: lld-link /out:%t.dll /dll %t.obj /export:exportfn1 /export:exportfn2 \
 # RUN:   /export:mangled
diff --git a/test/COFF/dllexport-mingw.s b/test/COFF/dllexport-mingw.s
index 8bf035b..a96003b 100644
--- a/test/COFF/dllexport-mingw.s
+++ b/test/COFF/dllexport-mingw.s
@@ -1,4 +1,4 @@
-# REQEUIRES: x86
+# REQUIRES: x86
 
 # RUN: llvm-mc -triple=i686-windows-gnu %s -filetype=obj -o %t.obj
 
diff --git a/test/COFF/driver.test b/test/COFF/driver.test
index 5f503f2..aef7046 100644
--- a/test/COFF/driver.test
+++ b/test/COFF/driver.test
@@ -9,3 +9,9 @@
 # RUN: lld-link /out:%t.dll /dll %t.obj
 # RUN: not lld-link /out:%t.exe %t.dll 2>&1 | FileCheck -check-prefix=BADFILE %s
 BADFILE: bad file type. Did you specify a DLL instead of an import library?
+
+# RUN: lld-link /lib /help | FileCheck -check-prefix=LIBHELP %s
+LIBHELP: OVERVIEW: LLVM Lib
+
+# RUN: not lld-link /WX /lib 2>&1 | FileCheck -check-prefix=LIBBAD %s
+LIBBAD: ignoring /lib since it's not the first argument
diff --git a/test/COFF/duplicate.test b/test/COFF/duplicate.test
index c2f743e..e2d3181 100644
--- a/test/COFF/duplicate.test
+++ b/test/COFF/duplicate.test
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 RUN: llc -mtriple x86_64-windows-msvc -filetype obj -o alpha.obj %S/Inputs/alpha.ll
 RUN: llc -mtriple x86_64-windows-msvc -filetype obj -o beta.obj %S/Inputs/beta.ll
 RUN: lld-link /out:alpha.dll /dll alpha.obj /implib:alpha.lib
diff --git a/test/COFF/entry-inference3.test b/test/COFF/entry-inference3.test
new file mode 100644
index 0000000..53550f7
--- /dev/null
+++ b/test/COFF/entry-inference3.test
@@ -0,0 +1,39 @@
+# RUN: sed -e s/ENTRYNAME/mainCRTStartup/ %s | yaml2obj > %t.obj
+# RUN: lld-link /subsystem:console /out:%t.exe %t.obj /verbose /nodefaultlib > %t.log 2>&1
+# RUN: FileCheck %s < %t.log
+
+# RUN: sed -e s/ENTRYNAME/?mainCRTStartup@@YAHXZ/ %s | yaml2obj > %t.obj
+# RUN: lld-link /subsystem:console /out:%t.exe %t.obj /verbose /nodefaultlib > %t.log 2>&1
+# RUN: FileCheck %s < %t.log
+
+# CHECK: Entry name inferred: mainCRTStartup
+
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: []
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     B82A000000C3
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          6
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          0
+  - Name:            "ENTRYNAME"
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...
diff --git a/test/COFF/entry-inference332.test b/test/COFF/entry-inference332.test
new file mode 100644
index 0000000..75c557a
--- /dev/null
+++ b/test/COFF/entry-inference332.test
@@ -0,0 +1,39 @@
+# RUN: sed -e s/ENTRYNAME/_mainCRTStartup/ %s | yaml2obj > %t.obj
+# RUN: lld-link /subsystem:console /out:%t.exe %t.obj /verbose /nodefaultlib > %t.log 2>&1
+# RUN: FileCheck %s < %t.log
+
+# RUN: sed -e s/ENTRYNAME/?mainCRTStartup@@YAHXZ/ %s | yaml2obj > %t.obj
+# RUN: lld-link /subsystem:console /out:%t.exe %t.obj /verbose /nodefaultlib > %t.log 2>&1
+# RUN: FileCheck %s < %t.log
+
+# CHECK: Entry name inferred: _mainCRTStartup
+
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_I386
+  Characteristics: []
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     B82A000000C3
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          6
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          0
+  - Name:            "ENTRYNAME"
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...
diff --git a/test/COFF/entry-inference4.test b/test/COFF/entry-inference4.test
new file mode 100644
index 0000000..513c9cf
--- /dev/null
+++ b/test/COFF/entry-inference4.test
@@ -0,0 +1,56 @@
+# RUN: sed 's/ENTRY1/WinMain/;s/ENTRY2/main/' %s | yaml2obj > %t.obj
+# RUN: not lld-link /subsystem:windows /out:%t.exe %t.obj > %t.log 2>&1
+# RUN: FileCheck -check-prefix=WINMAIN %s < %t.log
+
+# RUN: sed 's/ENTRY1/wWinMain/;s/ENTRY2/main/' %s | yaml2obj > %t.obj
+# RUN: not lld-link /subsystem:windows /out:%t.exe %t.obj > %t.log 2>&1
+# RUN: FileCheck -check-prefix=WWINMAIN %s < %t.log
+
+# RUN: sed 's/ENTRY1/WinMain/;s/ENTRY2/main/' %s | yaml2obj > %t.obj
+# RUN: not lld-link /subsystem:console /out:%t.exe %t.obj > %t.log 2>&1
+# RUN: FileCheck -check-prefix=MAIN %s < %t.log
+
+# RUN: sed 's/ENTRY1/WinMain/;s/ENTRY2/wmain/' %s | yaml2obj > %t.obj
+# RUN: not lld-link /subsystem:console /out:%t.exe %t.obj > %t.log 2>&1
+# RUN: FileCheck -check-prefix=WMAIN %s < %t.log
+
+# MAIN:     error: <root>: undefined symbol: mainCRTStartup
+# WMAIN:    error: <root>: undefined symbol: wmainCRTStartup
+# WINMAIN:  error: <root>: undefined symbol: WinMainCRTStartup
+# WWINMAIN: error: <root>: undefined symbol: wWinMainCRTStartup
+
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_AMD64
+  Characteristics: []
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     B82A000000C3
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          6
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          0
+  - Name:            ENTRY1
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            ENTRY2
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...
diff --git a/test/COFF/export-all.s b/test/COFF/export-all.s
index 96c7dca..78a2527 100644
--- a/test/COFF/export-all.s
+++ b/test/COFF/export-all.s
@@ -1,4 +1,4 @@
-# REQEUIRES: x86
+# REQUIRES: x86
 
 # RUN: llvm-mc -triple=i686-windows-gnu %s -filetype=obj -o %t.obj
 
@@ -63,6 +63,7 @@
 # RUN: mkdir -p %T/libs
 # RUN: echo -e ".global mingwfunc\n.text\nmingwfunc:\nret\n" > %T/libs/mingwfunc.s
 # RUN: llvm-mc -triple=x86_64-windows-gnu %T/libs/mingwfunc.s -filetype=obj -o %T/libs/mingwfunc.o
+# RUN: rm -f %T/libs/libmingwex.a
 # RUN: llvm-ar rcs %T/libs/libmingwex.a %T/libs/mingwfunc.o
 # RUN: echo -e ".global crtfunc\n.text\ncrtfunc:\nret\n" > %T/libs/crtfunc.s
 # RUN: llvm-mc -triple=x86_64-windows-gnu %T/libs/crtfunc.s -filetype=obj -o %T/libs/crt2.o
diff --git a/test/COFF/gfids-corrupt.s b/test/COFF/gfids-corrupt.s
index b87badf..92cd321 100644
--- a/test/COFF/gfids-corrupt.s
+++ b/test/COFF/gfids-corrupt.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -triple x86_64-windows-msvc %s -filetype=obj -o %t.obj
 # RUN: lld-link %t.obj -opt:noref -guard:nolongjmp -out:%t.exe -entry:main 2>&1 | FileCheck %s --check-prefix=ERRS
 # RUN: llvm-readobj -file-headers -coff-load-config %t.exe | FileCheck %s
diff --git a/test/COFF/gfids-fallback.s b/test/COFF/gfids-fallback.s
index 5c9c081..53fff17 100644
--- a/test/COFF/gfids-fallback.s
+++ b/test/COFF/gfids-fallback.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: grep -B99999 [S]PLITMARKER %s | llvm-mc -triple x86_64-windows-msvc -filetype=obj -o %t1.obj
 # RUN: grep -A99999 [S]PLITMARKER %s | llvm-mc -triple x86_64-windows-msvc -filetype=obj -o %t2.obj
 # RUN: lld-link %t1.obj %t2.obj -guard:nolongjmp -out:%t.exe -entry:main -opt:noref
diff --git a/test/COFF/gfids-gc.s b/test/COFF/gfids-gc.s
index 1ae348c..22fdbf1 100644
--- a/test/COFF/gfids-gc.s
+++ b/test/COFF/gfids-gc.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -triple x86_64-windows-msvc %s -filetype=obj -o %t.obj
 # RUN: lld-link %t.obj -guard:nolongjmp -out:%t.exe -opt:noref -entry:main
 # RUN: llvm-readobj -file-headers -coff-load-config %t.exe | FileCheck %s --check-prefix=CHECK-NOGC
diff --git a/test/COFF/gfids-icf.s b/test/COFF/gfids-icf.s
index 91d57ab..b0b4d70 100644
--- a/test/COFF/gfids-icf.s
+++ b/test/COFF/gfids-icf.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -triple x86_64-windows-msvc %s -filetype=obj -o %t.obj
 # RUN: lld-link %t.obj -guard:nolongjmp -out:%t.exe -opt:icf -entry:main
 # RUN: llvm-readobj -file-headers -coff-load-config %t.exe | FileCheck %s --check-prefix=CHECK
diff --git a/test/COFF/gfids-relocations32.s b/test/COFF/gfids-relocations32.s
new file mode 100644
index 0000000..0f7efeb
--- /dev/null
+++ b/test/COFF/gfids-relocations32.s
@@ -0,0 +1,87 @@
+# REQUIRES: x86
+# RUN: llvm-mc -triple i686-pc-win32 %s -filetype=obj -o %t.obj
+# RUN: lld-link %t.obj -guard:cf -out:%t.exe -entry:main
+# RUN: llvm-readobj -coff-load-config %t.exe | FileCheck %s --check-prefix=CHECK
+
+# Only f and _main should go in the table.
+# (use /lldmap:map.txt to check their addresses).
+#
+# CHECK: GuardFidTable [
+# CHECK-NEXT: 0x401000
+# CHECK-NEXT: 0x401030
+# CHECK-NEXT: ]
+
+# The input was loosly based on studying this program:
+#
+#  void foo() { return; }
+#  void bar() { return; }
+#  int main() {
+#    foo();
+#    void (*arr[])() = { &bar };
+#    (*arr[0])();
+#    return 0;
+#  }
+# cl /c a.cc && dumpbin /disasm a.obj > a.txt &&
+#   link a.obj /guard:cf /map:map.txt && dumpbin /loadconfig a.exe
+
+
+
+        .def    f;
+        .scl    3;
+        .type   32;
+        .endef
+        .section       .text,"xr",one_only,f
+        .p2align 4
+f:      movl $1, %eax
+        ret
+
+
+        .def    g;
+        .scl    3;
+        .type   32;
+        .endef
+        .section       .text,"xr",one_only,g
+        .p2align 4
+g:      movl $2, %eax
+        ret
+
+
+        .def    label;
+        .scl    6;     # StorageClass: Label
+        .type   0;     # Type: Not a function.
+        .endef
+        .section       .text,"xr",one_only,label
+        .p2align 4
+label:  ret
+
+
+        .data
+        .globl fp
+        .p2align 4
+fp:     .long f        # DIR32 relocation to function
+        .long label    # DIR32 relocation to label
+
+
+        .def    _main;
+        .scl    2;
+        .type   32;
+        .endef
+        .section       .text,"xr",one_only,_main
+        .globl  _main
+        .p2align 4
+_main:  call *fp       # DIR32 relocation to data
+        call g         # REL32 relocation to function
+        ret
+
+
+# Load configuration directory entry (winnt.h _IMAGE_LOAD_CONFIG_DIRECTORY32).
+# The linker will define the ___guard_* symbols.
+        .section .rdata,"dr"
+.globl __load_config_used
+__load_config_used:
+        .long 104  # Size.
+        .fill 76, 1, 0
+        .long ___guard_fids_table
+        .long ___guard_fids_count
+        .long ___guard_flags
+        .fill 12, 1, 0
diff --git a/test/COFF/gfids-relocations64.s b/test/COFF/gfids-relocations64.s
new file mode 100644
index 0000000..ae2e687
--- /dev/null
+++ b/test/COFF/gfids-relocations64.s
@@ -0,0 +1,76 @@
+# REQUIRES: x86
+# RUN: llvm-mc -triple x86_64-pc-win32 %s -filetype=obj -o %t.obj
+# RUN: lld-link %t.obj -guard:cf -out:%t.exe -entry:main
+# RUN: llvm-readobj -coff-load-config %t.exe | FileCheck %s --check-prefix=CHECK
+
+# f, g, and main go in the table.
+# Including g isn't strictly necessary since it's not an indirect call target,
+# however the linker can't know that because relative relocations are used both
+# for direct calls and for getting the absolute address of a function.
+# (use /lldmap:map.txt to check their addresses).
+#
+# CHECK: GuardFidTable [
+# CHECK-NEXT: 0x140001000
+# CHECK-NEXT: 0x140001010
+# CHECK-NEXT: 0x140001030
+# CHECK-NEXT: ]
+
+        .def    f;
+        .scl    3;
+        .type   32;
+        .endef
+        .section       .text,"xr",one_only,f
+        .p2align 4
+f:      movl $1, %eax
+        ret
+
+
+        .def    g;
+        .scl    3;
+        .type   32;
+        .endef
+        .section       .text,"xr",one_only,g
+        .p2align 4
+g:      movl $2, %eax
+        ret
+
+
+        .def    label;
+        .scl    6;     # StorageClass: Label
+        .type   0;     # Type: Not a function.
+        .endef
+        .section       .text,"xr",one_only,label
+        .p2align 4
+label:  ret
+
+
+        .data
+        .globl fp
+        .p2align 4
+fp:     .quad f        # DIR32 relocation to function
+        .quad label    # DIR32 relocation to label
+
+
+        .def    main;
+        .scl    2;
+        .type   32;
+        .endef
+        .section       .text,"xr",one_only,main
+        .globl  main
+        .p2align 4
+main:   call *fp       # DIR32 relocation to data
+        call g         # REL32 relocation to function
+        ret
+
+
+# Load configuration directory entry (winnt.h _IMAGE_LOAD_CONFIG_DIRECTORY64).
+# The linker will define the __guard_* symbols.
+        .section .rdata,"dr"
+.globl _load_config_used
+_load_config_used:
+        .long 256
+        .fill 124, 1, 0
+        .quad __guard_fids_table
+        .quad __guard_fids_count
+        .long __guard_flags
+        .fill 128, 1, 0
diff --git a/test/COFF/guard-longjmp.s b/test/COFF/guard-longjmp.s
index 8bde05c..a6d115d 100644
--- a/test/COFF/guard-longjmp.s
+++ b/test/COFF/guard-longjmp.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -triple x86_64-windows-msvc %s -filetype=obj -o %t.obj
 # RUN: lld-link %t.obj -guard:cf -out:%t.exe -entry:main
 # RUN: llvm-readobj -file-headers -coff-load-config %t.exe | FileCheck %s
diff --git a/test/COFF/guardcf-align.s b/test/COFF/guardcf-align.s
new file mode 100644
index 0000000..449d3fc
--- /dev/null
+++ b/test/COFF/guardcf-align.s
@@ -0,0 +1,46 @@
+# REQUIRES: x86
+# RUN: llvm-mc -triple x86_64-windows-msvc -filetype=obj -o %t.obj %s
+# RUN: yaml2obj < %p/Inputs/guardcf-align-foobar.yaml \
+# RUN:     > %T/guardcf-align-foobar.obj
+# RUN: lld-link -out:%T/guardcf-align.exe -entry:main -guard:cf \
+# RUN:     %t.obj %T/guardcf-align-foobar.obj
+# RUN: llvm-readobj -coff-load-config %T/guardcf-align.exe | FileCheck %s
+
+# Check that the gfids table contains at least one entry that ends in 0
+# and no entries that end in something other than 0.
+# CHECK: GuardFidTable [
+# CHECK-NOT: 0x{{[0-9A-Fa-f]+[^0]$}}
+# CHECK: 0x{{[0-9A-Fa-f]+0$}}
+# CHECK-NOT: 0x{{[0-9A-Fa-f]+[^0]$}}
+# CHECK: ]
+
+# @feat.00 and _load_config_used to indicate we have gfids.
+        .def     @feat.00;
+        .scl    3;
+        .type   0;
+        .endef
+        .globl  @feat.00
+@feat.00 = 0x801
+
+        .section .rdata,"dr"
+.globl _load_config_used
+_load_config_used:
+        .long 256
+        .fill 124, 1, 0
+        .quad __guard_fids_table
+        .quad __guard_fids_count
+        .long __guard_flags
+        .fill 128, 1, 0
+
+# Functions that are called indirectly.
+        .section        .gfids$y,"dr"
+        .symidx foo
+
+
+        .section .text,"rx"
+        .global main
+main:
+        movq foo, %rcx
+        xorq %rax, %rax
+        callq bar
+        retq
diff --git a/test/COFF/guardcf-lto.ll b/test/COFF/guardcf-lto.ll
index 8675117..8ab3e1e 100644
--- a/test/COFF/guardcf-lto.ll
+++ b/test/COFF/guardcf-lto.ll
@@ -1,3 +1,4 @@
+; REQUIRES: x86
 ; Set up an import library for a DLL that will do the indirect call.
 ; RUN: echo -e 'LIBRARY library\nEXPORTS\n  do_indirect_call\n' > %t.def
 ; RUN: lld-link -lib -def:%t.def -out:%t.lib -machine:x64
diff --git a/test/COFF/icf-executable.s b/test/COFF/icf-executable.s
index 7f923d2..461439d 100644
--- a/test/COFF/icf-executable.s
+++ b/test/COFF/icf-executable.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -triple=x86_64-windows-msvc %s -filetype=obj -o %t.obj
 # RUN: lld-link -entry:main %t.obj -out:%t.exe -verbose 2>&1 | FileCheck %s
 
diff --git a/test/COFF/icf-pdata.s b/test/COFF/icf-pdata.s
index ebd8479..8f5b3ba 100644
--- a/test/COFF/icf-pdata.s
+++ b/test/COFF/icf-pdata.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc %s -triple x86_64-windows-msvc -filetype=obj -o %t.obj
 # RUN: lld-link %t.obj -dll -noentry -out:%t.dll -merge:.xdata=.xdata
 # RUN: llvm-readobj -sections -coff-exports %t.dll | FileCheck %s
diff --git a/test/COFF/icf-xdata.s b/test/COFF/icf-xdata.s
index ec7f625..f2b6c3d 100644
--- a/test/COFF/icf-xdata.s
+++ b/test/COFF/icf-xdata.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc %s -triple x86_64-windows-msvc -filetype=obj -o %t.obj
 # RUN: lld-link %t.obj -dll -noentry -out:%t.dll -merge:.xdata=.xdata 2>&1 \
 # RUN:     | FileCheck %s --check-prefix=WARN
diff --git a/test/COFF/implib-name.test b/test/COFF/implib-name.test
index 81b5b25..4a875ab 100644
--- a/test/COFF/implib-name.test
+++ b/test/COFF/implib-name.test
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: mkdir -p %T
 # RUN: llvm-mc -triple x86_64-unknown-windows-msvc -filetype obj -o %T/object.obj %S/Inputs/object.s
 
diff --git a/test/COFF/invalid-section-number.test b/test/COFF/invalid-section-number.test
new file mode 100644
index 0000000..bada902
--- /dev/null
+++ b/test/COFF/invalid-section-number.test
@@ -0,0 +1,34 @@
+# RUN: yaml2obj %s > %t.obj
+# RUN: not lld-link %t.obj 2>&1 | FileCheck %s
+
+# CHECK: foo should not refer to special section -10
+
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_I386
+  Characteristics: []
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     B82A000000C3
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          6
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          0
+  - Name:            foo
+    Value:           0
+    SectionNumber:   -10
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...
diff --git a/test/COFF/loadcfg.ll b/test/COFF/loadcfg.ll
index 139ec74..6c1cf69 100644
--- a/test/COFF/loadcfg.ll
+++ b/test/COFF/loadcfg.ll
@@ -1,3 +1,4 @@
+; REQUIRES: x86
 ; RUN: llvm-as -o %t.obj %s
 ; RUN: lld-link /out:%t.exe %t.obj /entry:main /subsystem:console
 ; RUN: llvm-readobj -file-headers %t.exe | FileCheck %s
diff --git a/test/COFF/lto-chkstk.ll b/test/COFF/lto-chkstk.ll
index 43b0bff..1df93c6 100644
--- a/test/COFF/lto-chkstk.ll
+++ b/test/COFF/lto-chkstk.ll
@@ -1,6 +1,8 @@
+; REQUIRES: x86
 ; RUN: llvm-as -o %t.obj %s
 ; RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o %T/lto-chkstk-foo.obj %S/Inputs/lto-chkstk-foo.s
 ; RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o %T/lto-chkstk-chkstk.obj %S/Inputs/lto-chkstk-chkstk.s
+; RUN: rm -f %t.lib
 ; RUN: llvm-ar cru %t.lib %T/lto-chkstk-chkstk.obj
 ; RUN: lld-link /out:%t.exe /entry:main /subsystem:console %t.obj %T/lto-chkstk-foo.obj %t.lib
 
diff --git a/test/COFF/lto-comdat.ll b/test/COFF/lto-comdat.ll
index 9ff3776..c5c1d7b 100644
--- a/test/COFF/lto-comdat.ll
+++ b/test/COFF/lto-comdat.ll
@@ -1,3 +1,4 @@
+; REQUIRES: x86
 ; RUN: llvm-as -o %T/comdat-main.lto.obj %s
 ; RUN: llvm-as -o %T/comdat1.lto.obj %S/Inputs/lto-comdat1.ll
 ; RUN: llvm-as -o %T/comdat2.lto.obj %S/Inputs/lto-comdat2.ll
diff --git a/test/COFF/lto-icf.ll b/test/COFF/lto-icf.ll
index 291188f..b36b572 100644
--- a/test/COFF/lto-icf.ll
+++ b/test/COFF/lto-icf.ll
@@ -1,3 +1,4 @@
+; REQUIRES: x86
 ; Test that ICF works after LTO, i.e. both functions have the same address.
 ; Previously, when we didn't enable function sections, ICF didn't work.
 
diff --git a/test/COFF/lto-lazy-reference.ll b/test/COFF/lto-lazy-reference.ll
index 22f9539..1e92873 100644
--- a/test/COFF/lto-lazy-reference.ll
+++ b/test/COFF/lto-lazy-reference.ll
@@ -1,3 +1,4 @@
+; REQUIRES: x86
 ; RUN: llc -mtriple=i686-pc-windows-msvc -filetype=obj -o %T/lto-lazy-reference-quadruple.obj %S/Inputs/lto-lazy-reference-quadruple.ll
 ; RUN: llvm-as -o %T/lto-lazy-reference-dummy.bc %S/Inputs/lto-lazy-reference-dummy.ll
 ; RUN: rm -f %t.lib
diff --git a/test/COFF/lto-linker-opts.ll b/test/COFF/lto-linker-opts.ll
index 1d78875..1fc4f5e 100644
--- a/test/COFF/lto-linker-opts.ll
+++ b/test/COFF/lto-linker-opts.ll
@@ -1,3 +1,4 @@
+; REQUIRES: x86
 ; RUN: llvm-as -o %T/lto-linker-opts.obj %s
 ; RUN: env LIB=%S/Inputs lld-link /out:%T/lto-linker-opts.exe /entry:main /subsystem:console %T/lto-linker-opts.obj
 
diff --git a/test/COFF/lto-new-symbol.ll b/test/COFF/lto-new-symbol.ll
index d9e14eb..5223f73 100644
--- a/test/COFF/lto-new-symbol.ll
+++ b/test/COFF/lto-new-symbol.ll
@@ -1,3 +1,4 @@
+; REQUIRES: x86
 ; RUN: llvm-as -o %t.obj %s
 ; RUN: lld-link /out:%t.exe /entry:foo /subsystem:console %t.obj
 
diff --git a/test/COFF/lto-opt-level.ll b/test/COFF/lto-opt-level.ll
index cacd063..92f88ea 100644
--- a/test/COFF/lto-opt-level.ll
+++ b/test/COFF/lto-opt-level.ll
@@ -1,3 +1,4 @@
+; REQUIRES: x86
 ; RUN: llvm-as -o %t.obj %s
 ; RUN: lld-link /out:%t0.exe /entry:main /subsystem:console /opt:lldlto=0 /lldmap:%t0.map %t.obj
 ; RUN: FileCheck --check-prefix=CHECK-O0 %s < %t0.map
diff --git a/test/COFF/lto-parallel.ll b/test/COFF/lto-parallel.ll
index df0dc25..7a38a39 100644
--- a/test/COFF/lto-parallel.ll
+++ b/test/COFF/lto-parallel.ll
@@ -1,3 +1,4 @@
+; REQUIRES: x86
 ; RUN: llvm-as -o %t.obj %s
 ; RUN: lld-link -opt:noicf /out:%t.exe /entry:foo /include:bar /opt:lldltopartitions=2 /subsystem:console /lldmap:%t.map %t.obj
 ; RUN: FileCheck %s < %t.map
diff --git a/test/COFF/lto-reloc-model.ll b/test/COFF/lto-reloc-model.ll
index bea19e9..9ac32ae 100644
--- a/test/COFF/lto-reloc-model.ll
+++ b/test/COFF/lto-reloc-model.ll
@@ -1,3 +1,4 @@
+; REQUIRES: x86
 ; RUN: llvm-as -o %t %s
 ; RUN: lld-link /entry:main /subsystem:console /out:%t.exe %t
 ; RUN: llvm-objdump -d %t.exe | FileCheck %s
diff --git a/test/COFF/lto.ll b/test/COFF/lto.ll
index 4e16c9b..c83a151 100644
--- a/test/COFF/lto.ll
+++ b/test/COFF/lto.ll
@@ -1,3 +1,4 @@
+; REQUIRES: x86
 ; RUN: llvm-as -o %T/main.lto.obj %s
 ; RUN: llvm-as -o %T/foo.lto.obj %S/Inputs/lto-dep.ll
 ; RUN: rm -f %T/foo.lto.lib
diff --git a/test/COFF/msvclto-archive.ll b/test/COFF/msvclto-archive.ll
deleted file mode 100644
index f095327..0000000
--- a/test/COFF/msvclto-archive.ll
+++ /dev/null
@@ -1,40 +0,0 @@
-; REQUIRES: x86
-;; Make sure we re-create archive files to strip bitcode files.
-
-;; Do not create empty archives because the MSVC linker
-;; doesn't support them.
-; RUN: llvm-as -o %t.obj %s
-; RUN: rm -f %t-main1.a
-; RUN: llvm-ar cru %t-main1.a %t.obj
-; RUN: mkdir -p %t.dir
-; RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o %t.dir/bitcode.obj %p/Inputs/msvclto.s
-; RUN: lld-link %t-main1.a %t.dir/bitcode.obj /msvclto /out:%t.exe /opt:lldlto=1 /opt:icf \
-; RUN:   /entry:main /verbose 2> %t.log || true
-; RUN: FileCheck -check-prefix=BC %s < %t.log
-; BC-NOT: Creating a temporary archive for
-
-; RUN: rm -f %t-main2.a
-; RUN: llvm-ar cru %t-main2.a %t.dir/bitcode.obj
-; RUN: lld-link %t.obj %t-main2.a /msvclto /out:%t.exe /opt:lldlto=1 /opt:icf \
-; RUN:   /entry:main /verbose 2> %t.log || true
-; RUN: FileCheck -check-prefix=OBJ %s < %t.log
-; OBJ-NOT: Creating a temporary archive
-
-;; Make sure that we always rebuild thin archives because
-;; the MSVC linker doesn't support thin archives.
-; RUN: rm -f %t-main3.a
-; RUN: llvm-ar cruT %t-main3.a %t.dir/bitcode.obj
-; RUN: lld-link %t.obj %t-main3.a /msvclto /out:%t.exe /opt:lldlto=1 /opt:icf \
-; RUN:   /entry:main /verbose 2> %t.log || true
-; RUN: FileCheck -check-prefix=THIN %s < %t.log
-; THIN: Creating a temporary archive
-
-target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
-target triple = "x86_64-pc-windows-msvc"
-
-declare void @foo()
-
-define i32 @main() {
-  call void @foo()
-  ret i32 0
-}
diff --git a/test/COFF/msvclto-order.ll b/test/COFF/msvclto-order.ll
deleted file mode 100644
index 1758077..0000000
--- a/test/COFF/msvclto-order.ll
+++ /dev/null
@@ -1,25 +0,0 @@
-; REQUIRES: x86
-; RUN: opt -thinlto-bc %s -o %t.obj
-; RUN: llc -filetype=obj %S/Inputs/msvclto-order-a.ll -o %T/msvclto-order-a.obj
-; RUN: llvm-ar crs %T/msvclto-order-a.lib %T/msvclto-order-a.obj
-; RUN: llc -filetype=obj %S/Inputs/msvclto-order-b.ll -o %T/msvclto-order-b.obj
-; RUN: llvm-ar crs %T/msvclto-order-b.lib %T/msvclto-order-b.obj
-; RUN: lld-link /verbose /msvclto /out:%t.exe /entry:main %t.obj \
-; RUN:     %T/msvclto-order-a.lib %T/msvclto-order-b.lib 2> %t.log || true
-; RUN: FileCheck %s < %t.log
-
-; CHECK: : link.exe
-; CHECK-NOT: .lib{{$}}
-; CHECK: lld-msvclto-order-a{{.*}}.obj
-; CHECK-NOT: lld-msvclto-order-b{{.*}}.obj
-; CHECK: .lib{{$}}
-
-target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
-target triple = "x86_64-pc-windows-msvc"
-
-declare void @foo()
-
-define i32 @main() {
-  call void @foo()
-  ret i32 0
-}
diff --git a/test/COFF/msvclto.ll b/test/COFF/msvclto.ll
deleted file mode 100644
index b299827..0000000
--- a/test/COFF/msvclto.ll
+++ /dev/null
@@ -1,20 +0,0 @@
-; REQUIRES: x86
-; RUN: llvm-as -o %t.obj %s
-; RUN: mkdir -p %t.dir
-; RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o %t.dir/bitcode.obj %p/Inputs/msvclto.s
-; RUN: lld-link %t.obj %t.dir/bitcode.obj /msvclto /out:%t.exe /opt:lldlto=1 /opt:icf \
-; RUN:   /entry:main /verbose 2> %t.log || true
-; RUN: FileCheck %s < %t.log
-
-; CHECK: /opt:icf /entry:main
-; CHECK: /verbose
-
-target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
-target triple = "x86_64-pc-windows-msvc"
-
-declare void @foo()
-
-define i32 @main() {
-  call void @foo()
-  ret i32 0
-}
diff --git a/test/COFF/pdb-comdat.test b/test/COFF/pdb-comdat.test
index 1659f98..2c40273 100644
--- a/test/COFF/pdb-comdat.test
+++ b/test/COFF/pdb-comdat.test
@@ -43,10 +43,10 @@
 CHECK-NEXT:    Records
 CHECK-NEXT:        84 | S_PROCREF [size = 20] `main`
 CHECK-NEXT:             module = 1, sum name = 0, offset = 120
-CHECK-NEXT:       128 | S_PROCREF [size = 20] `foo`
-CHECK-NEXT:             module = 1, sum name = 0, offset = 208
 CHECK-NEXT:       148 | S_PROCREF [size = 20] `bar`
 CHECK-NEXT:             module = 2, sum name = 0, offset = 120
+CHECK-NEXT:       128 | S_PROCREF [size = 20] `foo`
+CHECK-NEXT:             module = 1, sum name = 0, offset = 208
 CHECK-NEXT:       104 | S_GDATA32 [size = 24] `global`
 CHECK-NEXT:             type = 0x0074 (int), addr = 0000:0000
 CHECK-NEXT:       168 | S_GDATA32 [size = 24] `global`
diff --git a/test/COFF/pdb-exe-path-dots.test b/test/COFF/pdb-exe-path-dots.test
new file mode 100644
index 0000000..1318981
--- /dev/null
+++ b/test/COFF/pdb-exe-path-dots.test
@@ -0,0 +1,10 @@
+RUN: yaml2obj < %p/Inputs/pdb1.yaml > %t1.obj
+RUN: yaml2obj < %p/Inputs/pdb2.yaml > %t2.obj
+RUN: rm -rf %t
+RUN: mkdir %t
+RUN: mkdir %t/foo
+RUN: lld-link /debug /pdb:%t/foo/./out.pdb /out:%t/out.exe /entry:main /nodefaultlib \
+RUN:   %t1.obj %t2.obj
+RUN: llvm-readobj -coff-debug-directory %t/out.exe | FileCheck %s
+
+CHECK: PDBFileName: {{.*}}tmp{{/|\\}}foo{{/|\\}}out.pdb
\ No newline at end of file
diff --git a/test/COFF/pdb-global-gc.yaml b/test/COFF/pdb-global-gc.yaml
index f2c4450..7206196 100644
--- a/test/COFF/pdb-global-gc.yaml
+++ b/test/COFF/pdb-global-gc.yaml
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: yaml2obj %s -o %t.obj
 # RUN: llvm-mc %S/Inputs/pdb-global-gc.s -triple x86_64-windows-msvc -filetype=obj -o %t2.obj
 # RUN: lld-link %t.obj %t2.obj -debug -entry:main \
diff --git a/test/COFF/pdb-globals-dia-func-collision3.test b/test/COFF/pdb-globals-dia-func-collision3.test
new file mode 100644
index 0000000..532bdd4
--- /dev/null
+++ b/test/COFF/pdb-globals-dia-func-collision3.test
@@ -0,0 +1,81 @@
+REQUIRES: diasdk
+
+Input object file reconstruction:
+
+; // foo.cpp
+; void LJPwNRh() {}
+; void HGfxvKdQO() {}
+; void wuN() {}
+; void tEo() {}
+; void VUo() {}
+; void teO() {}
+; void bqSuLGQgWa() {}
+; void SyJYcL() {}
+; void OUV() {}
+; void quH() {}
+; void rbEaPKrlrRwk() {}
+; void oet() {}
+; void tuM() {}
+; void LuU() {}
+; void loxueqJLH() {}
+; void QplRJuDs() {}
+; void rWDokkLG() {}
+; void sEH() {}
+; void pui() {}
+; void xoZvxw() {}
+; 
+; int main(int argc, char **argv) {
+;   return 0;
+; }
+
+clang-cl /Z7 /GS- /GR- /c main.cpp /Foglobals-dia-func-collision3.obj
+
+RUN: lld-link /debug /nodefaultlib /incremental:no /entry:main /out:%t.exe %S/Inputs/globals-dia-func-collision3.obj
+RUN: llvm-pdbutil pretty -with-name=LuU -with-name=oet -with-name=OUV -with-name=pui \
+RUN:                     -with-name=quH -with-name=sEH -with-name=teO -with-name=tEo \
+RUN:                     -with-name=tuM -with-name=VUo -with-name=wuN -with-name=SyJYcL \
+RUN:                     -with-name=xoZvxw -with-name=LJPwNRh -with-name=QplRJuDs -with-name=rWDokkLG \
+RUN:                     -with-name=HGfxvKdQO -with-name=loxueqJLH -with-name=bqSuLGQgWa -with-name=rbEaPKrlrRwk \
+RUN:                     %t.pdb | FileCheck %s
+
+
+CHECK:      [1 occurrences] - LuU
+CHECK-NEXT:   func [0x000010d0+ 0 - 0x000010d1- 1 | sizeof=  1] (FPO) void __cdecl LuU()
+CHECK-NEXT: [1 occurrences] - oet
+CHECK-NEXT:   func [0x000010b0+ 0 - 0x000010b1- 1 | sizeof=  1] (FPO) void __cdecl oet()
+CHECK-NEXT: [1 occurrences] - OUV
+CHECK-NEXT:   func [0x00001080+ 0 - 0x00001081- 1 | sizeof=  1] (FPO) void __cdecl OUV()
+CHECK-NEXT: [1 occurrences] - pui
+CHECK-NEXT:   func [0x00001120+ 0 - 0x00001121- 1 | sizeof=  1] (FPO) void __cdecl pui()
+CHECK-NEXT: [1 occurrences] - quH
+CHECK-NEXT:   func [0x00001090+ 0 - 0x00001091- 1 | sizeof=  1] (FPO) void __cdecl quH()
+CHECK-NEXT: [1 occurrences] - sEH
+CHECK-NEXT:   func [0x00001110+ 0 - 0x00001111- 1 | sizeof=  1] (FPO) void __cdecl sEH()
+CHECK-NEXT: [1 occurrences] - teO
+CHECK-NEXT:   func [0x00001050+ 0 - 0x00001051- 1 | sizeof=  1] (FPO) void __cdecl teO()
+CHECK-NEXT: [1 occurrences] - tEo
+CHECK-NEXT:   func [0x00001030+ 0 - 0x00001031- 1 | sizeof=  1] (FPO) void __cdecl tEo()
+CHECK-NEXT: [1 occurrences] - tuM
+CHECK-NEXT:   func [0x000010c0+ 0 - 0x000010c1- 1 | sizeof=  1] (FPO) void __cdecl tuM()
+CHECK-NEXT: [1 occurrences] - VUo
+CHECK-NEXT:   func [0x00001040+ 0 - 0x00001041- 1 | sizeof=  1] (FPO) void __cdecl VUo()
+CHECK-NEXT: [1 occurrences] - wuN
+CHECK-NEXT:   func [0x00001020+ 0 - 0x00001021- 1 | sizeof=  1] (FPO) void __cdecl wuN()
+CHECK-NEXT: [1 occurrences] - SyJYcL
+CHECK-NEXT:   func [0x00001070+ 0 - 0x00001071- 1 | sizeof=  1] (FPO) void __cdecl SyJYcL()
+CHECK-NEXT: [1 occurrences] - xoZvxw
+CHECK-NEXT:   func [0x00001130+ 0 - 0x00001131- 1 | sizeof=  1] (FPO) void __cdecl xoZvxw()
+CHECK-NEXT: [1 occurrences] - LJPwNRh
+CHECK-NEXT:   func [0x00001000+ 0 - 0x00001001- 1 | sizeof=  1] (FPO) void __cdecl LJPwNRh()
+CHECK-NEXT: [1 occurrences] - QplRJuDs
+CHECK-NEXT:   func [0x000010f0+ 0 - 0x000010f1- 1 | sizeof=  1] (FPO) void __cdecl QplRJuDs()
+CHECK-NEXT: [1 occurrences] - rWDokkLG
+CHECK-NEXT:   func [0x00001100+ 0 - 0x00001101- 1 | sizeof=  1] (FPO) void __cdecl rWDokkLG()
+CHECK-NEXT: [1 occurrences] - HGfxvKdQO
+CHECK-NEXT:   func [0x00001010+ 0 - 0x00001011- 1 | sizeof=  1] (FPO) void __cdecl HGfxvKdQO()
+CHECK-NEXT: [1 occurrences] - loxueqJLH
+CHECK-NEXT:   func [0x000010e0+ 0 - 0x000010e1- 1 | sizeof=  1] (FPO) void __cdecl loxueqJLH()
+CHECK-NEXT: [1 occurrences] - bqSuLGQgWa
+CHECK-NEXT:   func [0x00001060+ 0 - 0x00001061- 1 | sizeof=  1] (FPO) void __cdecl bqSuLGQgWa()
+CHECK-NEXT: [1 occurrences] - rbEaPKrlrRwk
+CHECK-NEXT:   func [0x000010a0+ 0 - 0x000010a1- 1 | sizeof=  1] (FPO) void __cdecl rbEaPKrlrRwk()
diff --git a/test/COFF/pdb-globals-dia-vfunc-collision.test b/test/COFF/pdb-globals-dia-vfunc-collision.test
new file mode 100644
index 0000000..9649018
--- /dev/null
+++ b/test/COFF/pdb-globals-dia-vfunc-collision.test
@@ -0,0 +1,42 @@
+REQUIRES: diasdk
+
+Input object file reconstruction:
+
+; // main.cpp
+; struct S {
+;   // Function names are chosen specifically to generate hash collisions in the
+;   // GSI hash table.
+;   virtual int A307() { return 102; }
+;   virtual int A400() { return 12; }
+;   virtual int A206() { return 201; }
+;   virtual int A105() { return 300; }
+; };
+; 
+; struct T : public S {
+;   int A105() override { return 300; }
+;   int A307() override { return 102; }
+;   int A206() override { return 201; }
+;   int A400() override { return 12; }
+; };
+; 
+; int main(int argc, char **argv) {
+;   T s;
+;   return s.A105() + s.A206() + s.A307() + s.A400();
+; }
+
+clang-cl /Z7 /GS- /GR- /c main.cpp /Foglobals-dia-vfunc-collision.obj
+
+RUN: lld-link /debug /nodefaultlib /entry:main /out:%t.exe %S/Inputs/globals-dia-vfunc-collision.obj
+RUN: llvm-pdbutil pretty -classes %t.pdb | FileCheck %s
+
+CHECK: struct T
+CHECK: func [0x000010c0+ 0 - 0x000010dd-29 | sizeof= 29] (FPO) virtual {{.*}}A105()
+CHECK: func [0x00001100+ 0 - 0x0000111b-27 | sizeof= 27] (FPO) virtual {{.*}}A307()
+CHECK: func [0x000010e0+ 0 - 0x000010fd-29 | sizeof= 29] (FPO) virtual {{.*}}A206()
+CHECK: func [0x00001120+ 0 - 0x0000113b-27 | sizeof= 27] (FPO) virtual {{.*}}A400()
+
+CHECK: struct S
+CHECK: func [0x00001160+ 0 - 0x0000116c-12 | sizeof= 12] (FPO) virtual {{.*}}A307()
+CHECK: func [0x00001170+ 0 - 0x0000117c-12 | sizeof= 12] (FPO) virtual {{.*}}A400()
+CHECK: func [0x00001180+ 0 - 0x0000118c-12 | sizeof= 12] (FPO) virtual {{.*}}A206()
+CHECK: func [0x00001190+ 0 - 0x0000119c-12 | sizeof= 12] (FPO) virtual {{.*}}A105()
diff --git a/test/COFF/pdb-globals-dia-vfunc-collision2.test b/test/COFF/pdb-globals-dia-vfunc-collision2.test
new file mode 100644
index 0000000..cfbc445
--- /dev/null
+++ b/test/COFF/pdb-globals-dia-vfunc-collision2.test
@@ -0,0 +1,25 @@
+REQUIRES: diasdk
+
+Input object file reconstruction:
+
+; // main.cpp
+; struct S {
+;   // Function names are chosen specifically to generate hash collisions in the
+;   // GSI hash table.
+;   virtual int A132() { return 102; }
+;   virtual int A1001() { return 300; }
+; };
+; 
+; int main(int argc, char **argv) {
+;   S s;
+;   return s.A132();
+; }
+
+clang-cl /Z7 /GS- /GR- /c main.cpp /Foglobals-dia-vfunc-collision2.obj
+
+RUN: lld-link /debug /nodefaultlib /entry:main /out:%t.exe %S/Inputs/globals-dia-vfunc-collision2.obj
+RUN: llvm-pdbutil pretty -classes %t.pdb | FileCheck %s
+
+CHECK: struct S
+CHECK: func [0x00001060+ 0 - 0x0000106c-12 | sizeof= 12] (FPO) virtual {{.*}}A132()
+CHECK: func [0x00001070+ 0 - 0x0000107c-12 | sizeof= 12] (FPO) virtual {{.*}}A1001()
diff --git a/test/COFF/pdb-globals-dia-vfunc-simple.test b/test/COFF/pdb-globals-dia-vfunc-simple.test
new file mode 100644
index 0000000..00d95ad
--- /dev/null
+++ b/test/COFF/pdb-globals-dia-vfunc-simple.test
@@ -0,0 +1,26 @@
+REQUIRES: diasdk
+
+Input object file reconstruction:
+
+; // main.cpp
+; struct Base {
+;   virtual int V2() { return 42; }
+; };
+; 
+; struct Derived : public Base {
+;   int V2() override { return 42; }
+; };
+; 
+; int main()
+; {
+;   Derived D;
+;   return D.V2();
+; }
+
+clang-cl /Z7 /GS- /GR- /c main.cpp /Foglobals-dia-vfunc-simple.obj
+
+RUN: lld-link /debug /nodefaultlib /entry:main /out:%t.exe %S/Inputs/globals-dia-vfunc-simple.obj
+RUN: llvm-pdbutil pretty -classes %t.pdb | FileCheck %s
+
+CHECK: func [0x00001070+ 0 - 0x0000107c-12 | sizeof= 12] (FPO) virtual {{.*}}V2()
+CHECK: func [0x000010a0+ 0 - 0x000010ac-12 | sizeof= 12] (FPO) virtual {{.*}}V2()
diff --git a/test/COFF/pdb-globals.test b/test/COFF/pdb-globals.test
index 3eee76a..db0d3f6 100644
--- a/test/COFF/pdb-globals.test
+++ b/test/COFF/pdb-globals.test
@@ -15,20 +15,20 @@
 CHECK-LABEL:                        Global Symbols
 CHECK-NEXT:  ============================================================
 CHECK-NEXT:   Records
+CHECK-NEXT:      208 | S_LPROCREF [size = 24] `LocalFunc`
+CHECK-NEXT:            module = 1, sum name = 0, offset = 292
 CHECK-NEXT:      160 | S_PROCREF [size = 28] `GlobalFunc`
 CHECK-NEXT:            module = 1, sum name = 0, offset = 52
 CHECK-NEXT:      188 | S_PROCREF [size = 20] `main`
 CHECK-NEXT:            module = 1, sum name = 0, offset = 108
-CHECK-NEXT:      208 | S_LPROCREF [size = 24] `LocalFunc`
-CHECK-NEXT:            module = 1, sum name = 0, offset = 292
-CHECK-NEXT:      312 | S_PROCREF [size = 40] `HelloPoint::HelloPoint`
-CHECK-NEXT:            module = 1, sum name = 0, offset = 376
 CHECK-NEXT:      232 | S_GDATA32 [size = 28] `__purecall`
 CHECK-NEXT:            type = 0x0403 (void*), addr = 0003:0004
 CHECK-NEXT:      260 | S_GDATA32 [size = 24] `GlobalVar`
 CHECK-NEXT:            type = 0x100B (const int*), addr = 0003:0000
 CHECK-NEXT:      284 | S_LDATA32 [size = 28] `ConstantVar`
 CHECK-NEXT:            type = 0x100A (const int), addr = 0002:0000
+CHECK-NEXT:      312 | S_PROCREF [size = 40] `HelloPoint::HelloPoint`
+CHECK-NEXT:            module = 1, sum name = 0, offset = 376
 
 CHECK-LABEL:                           Symbols
 CHECK-NEXT:  ============================================================
diff --git a/test/COFF/pdb-linker-module.test b/test/COFF/pdb-linker-module.test
index 96ca1b4..022a447 100644
--- a/test/COFF/pdb-linker-module.test
+++ b/test/COFF/pdb-linker-module.test
@@ -1,4 +1,5 @@
-RUN: lld-link /debug /pdb:%t.pdb /nodefaultlib /entry:main %S/Inputs/pdb-diff.obj
+RUN: echo "/nodefaultlib" > %t.rsp
+RUN: lld-link /debug /pdb:%t.pdb @%t.rsp /entry:main %S/Inputs/pdb-diff.obj
 RUN: llvm-pdbutil dump -modules %t.pdb | FileCheck --check-prefix=MODS %s
 RUN: llvm-pdbutil dump -symbols %t.pdb | FileCheck --check-prefix=SYMS %s
 
diff --git a/test/COFF/pdb-relative-source-lines.test b/test/COFF/pdb-relative-source-lines.test
new file mode 100644
index 0000000..8c0894c
--- /dev/null
+++ b/test/COFF/pdb-relative-source-lines.test
@@ -0,0 +1,45 @@
+Test the linker line tables on roughly the following example:
+
+==> foo.h <==
+void bar(void);
+inline void foo(void) {
+  bar();
+}
+==> pdb_lines_1.c <==
+#include "foo.h"
+int main(void) {
+  foo();
+  return 42;
+}
+==> pdb_lines_2.c <==
+void bar(void) {
+}
+
+$ clang-cl -Xclang -fdebug-compilation-dir -Xclang . -c -Z7 pdb_lines*.c
+
+RUN: yaml2obj %S/Inputs/pdb_lines_1_relative.yaml -o %t.pdb_lines_1_relative.obj
+RUN: yaml2obj %S/Inputs/pdb_lines_2_relative.yaml -o %t.pdb_lines_2_relative.obj
+RUN: rm -f %t.exe %t.pdb
+RUN: lld-link -debug -pdbsourcepath:c:\\src -entry:main -nodefaultlib -out:%t.exe -pdb:%t.pdb %t.pdb_lines_1_relative.obj %t.pdb_lines_2_relative.obj
+RUN: llvm-pdbutil pdb2yaml -modules -module-files -subsections=lines,fc %t.pdb | FileCheck %s
+
+CHECK-LABEL:  - Module:          {{.*}}pdb_lines_1_relative.obj
+CHECK-NEXT:     ObjFile:         {{.*}}pdb_lines_1_relative.obj
+CHECK:          SourceFiles:
+CHECK-NEXT:       - 'c:{{[\\/]}}src{{[\\/]}}pdb_lines_1.c'
+CHECK-NEXT:       - 'c:{{[\\/]}}src{{[\\/]}}foo.h'
+CHECK:          Subsections:
+CHECK:                - FileName:        'c:{{[\\/]}}src{{[\\/]}}pdb_lines_1.c'
+CHECK:                - FileName:        'c:{{[\\/]}}src{{[\\/]}}foo.h'
+CHECK:            - !FileChecksums
+CHECK:                - FileName:        'c:{{[\\/]}}src{{[\\/]}}pdb_lines_1.c'
+CHECK:                - FileName:        'c:{{[\\/]}}src{{[\\/]}}foo.h'
+
+CHECK-LABEL:  - Module:          {{.*}}pdb_lines_2_relative.obj
+CHECK-NEXT:     ObjFile:         {{.*}}pdb_lines_2_relative.obj
+CHECK:          SourceFiles:
+CHECK-NEXT:       - 'c:{{[\\/]}}src{{[\\/]}}pdb_lines_2.c'
+CHECK:          Subsections:
+CHECK:                - FileName:        'c:{{[\\/]}}src{{[\\/]}}pdb_lines_2.c'
+CHECK:            - !FileChecksums
+CHECK:                - FileName:        'c:{{[\\/]}}src{{[\\/]}}pdb_lines_2.c'
diff --git a/test/COFF/pdb.test b/test/COFF/pdb.test
index 5788b51..a7b2a21 100644
--- a/test/COFF/pdb.test
+++ b/test/COFF/pdb.test
@@ -193,6 +193,25 @@
 RAW-NEXT:        0 | S_PUB32 [size = 20] `foo`
 RAW-NEXT:            flags = function, addr = 0001:0016
 RAW-NOT:             S_PUB32
+RAW-NEXT:     Hash Bitmap (
+RAW-NEXT:     0000: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000  |................................|
+RAW-NEXT:     0020: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000  |................................|
+RAW-NEXT:     0040: 00000000 20000000 00000000 00000000 00000000 00000000 00000000 00000000  |.... ...........................|
+RAW-NEXT:     0060: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000  |................................|
+RAW-NEXT:     0080: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000  |................................|
+RAW-NEXT:     00A0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000  |................................|
+RAW-NEXT:     00C0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000  |................................|
+RAW-NEXT:     00E0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000  |................................|
+RAW-NEXT:     0100: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000  |................................|
+RAW-NEXT:     0120: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000  |................................|
+RAW-NEXT:     0140: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000  |................................|
+RAW-NEXT:     0160: 01000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000  |................................|
+RAW-NEXT:     0180: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000  |................................|
+RAW-NEXT:     01A0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000  |................................|
+RAW-NEXT:     01C0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000  |................................|
+RAW-NEXT:     01E0: 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000  |................................|
+RAW-NEXT:     0200: 00000000                                                                 |....|
+RAW-NEXT:   )
 RAW-NEXT:   Hash Entries
 RAW-NEXT:     off = 21, refcnt = 1
 RAW-NEXT:     off = 1, refcnt = 1
diff --git a/test/COFF/pending-comdat.s b/test/COFF/pending-comdat.s
new file mode 100644
index 0000000..b10dce1
--- /dev/null
+++ b/test/COFF/pending-comdat.s
@@ -0,0 +1,21 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -triple=x86_64-windows-gnu %s -filetype=obj -o %t.obj
+
+# RUN: not lld-link -lldmingw -out:%t.exe -entry:main -subsystem:console %t.obj 2>&1 | FileCheck %s
+
+# CHECK: error: undefined symbol: other
+
+# Check that the comdat section without a symbol isn't left pending once we iterate symbols
+# to print source of the undefined symbol.
+
+	.text
+	.globl main
+main:
+	call other
+	ret
+
+	.section	.data$pending,"w"
+	.linkonce	discard
+.Llocal:
+	.byte	0
diff --git a/test/COFF/reloc-discarded-dwarf.s b/test/COFF/reloc-discarded-dwarf.s
index 14dc594..960f3b3 100644
--- a/test/COFF/reloc-discarded-dwarf.s
+++ b/test/COFF/reloc-discarded-dwarf.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -triple=x86_64-windows-msvc -filetype=obj -o %t1.obj %s
 # RUN: llvm-mc -triple=x86_64-windows-msvc -filetype=obj -o %t2.obj %s
 
diff --git a/test/COFF/reloc-discarded-early.s b/test/COFF/reloc-discarded-early.s
index 6d1043d..181859d 100644
--- a/test/COFF/reloc-discarded-early.s
+++ b/test/COFF/reloc-discarded-early.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -triple=x86_64-windows-msvc -filetype=obj -o %t.obj %s
 # RUN: lld-link -entry:__ImageBase -subsystem:console -debug %t.obj
 
diff --git a/test/COFF/reloc-discarded-early2.s b/test/COFF/reloc-discarded-early2.s
index 18e2000..c5589dc 100644
--- a/test/COFF/reloc-discarded-early2.s
+++ b/test/COFF/reloc-discarded-early2.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -triple=x86_64-windows-msvc -filetype=obj -o %t.obj %s
 # RUN: not lld-link -entry:__ImageBase -subsystem:console %t.obj 2>&1 | FileCheck %s
 
diff --git a/test/COFF/reloc-discarded.s b/test/COFF/reloc-discarded.s
index 0be4e11..aa8fb3c 100644
--- a/test/COFF/reloc-discarded.s
+++ b/test/COFF/reloc-discarded.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: echo -e '.section .bss,"bw",discard,main_global\n.global main_global\n main_global:\n .long 0' | \
 # RUN:     llvm-mc - -filetype=obj -o %t1.obj -triple x86_64-windows-msvc
 # RUN: llvm-mc %s -filetype=obj -o %t2.obj -triple x86_64-windows-msvc
diff --git a/test/COFF/reloc-x64.test b/test/COFF/reloc-x64.test
index 7af8fb2..21d7842 100644
--- a/test/COFF/reloc-x64.test
+++ b/test/COFF/reloc-x64.test
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: yaml2obj < %s > %t.obj
 # RUN: lld-link /out:%t.exe /entry:main %t.obj
 # RUN: llvm-objdump -d %t.exe | FileCheck %s
diff --git a/test/COFF/reloc-x86.test b/test/COFF/reloc-x86.test
index 5e14069..bd500be 100644
--- a/test/COFF/reloc-x86.test
+++ b/test/COFF/reloc-x86.test
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: yaml2obj < %s > %t.obj
 # RUN: lld-link /out:%t.exe /entry:main /base:0x400000 %t.obj
 # RUN: llvm-objdump -d %t.exe | FileCheck %s
diff --git a/test/COFF/safeseh-md.s b/test/COFF/safeseh-md.s
index ae731b5..b762457 100644
--- a/test/COFF/safeseh-md.s
+++ b/test/COFF/safeseh-md.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -triple i686-windows-msvc %s -filetype=obj -o %t.obj
 # RUN: lld-link %t.obj %S/Inputs/except_handler3.lib -safeseh -out:%t.exe -opt:noref -entry:main
 # RUN: llvm-readobj -coff-load-config %t.exe | FileCheck %s
diff --git a/test/COFF/safeseh-notable.s b/test/COFF/safeseh-notable.s
index 1f00cb2..8a9d429 100644
--- a/test/COFF/safeseh-notable.s
+++ b/test/COFF/safeseh-notable.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -triple i686-windows-msvc %s -filetype=obj -o %t.obj
 # RUN: lld-link %t.obj -safeseh -out:%t.exe -entry:main
 # RUN: llvm-readobj -file-headers %t.exe | FileCheck %s
diff --git a/test/COFF/safeseh.s b/test/COFF/safeseh.s
index efe20fb..ee6a60d 100644
--- a/test/COFF/safeseh.s
+++ b/test/COFF/safeseh.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -triple i686-windows-msvc %s -filetype=obj -o %t.obj
 # RUN: lld-link %t.obj -safeseh -out:%t.exe -opt:noref -entry:main
 # RUN: llvm-readobj -coff-basereloc -coff-load-config -file-headers %t.exe | FileCheck %s --check-prefix=CHECK-NOGC
diff --git a/test/COFF/secidx-absolute.s b/test/COFF/secidx-absolute.s
index a62d2c7..0b467bb 100644
--- a/test/COFF/secidx-absolute.s
+++ b/test/COFF/secidx-absolute.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc %s -filetype=obj -triple=x86_64-windows-msvc -o %t.obj
 # RUN: lld-link -entry:main -nodefaultlib %t.obj -out:%t.exe
 # RUN: llvm-readobj %t.exe -sections -section-data | FileCheck %s
diff --git a/test/COFF/secrel-absolute.s b/test/COFF/secrel-absolute.s
index bc61fb9..630af6e 100644
--- a/test/COFF/secrel-absolute.s
+++ b/test/COFF/secrel-absolute.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc %s -filetype=obj -triple=x86_64-windows-msvc -o %t.obj
 # RUN: not lld-link -entry:main -nodefaultlib %t.obj -out:%t.exe 2>&1 | FileCheck %s
 
diff --git a/test/COFF/secrel-common.s b/test/COFF/secrel-common.s
index 85cb10d..0d3aafc 100644
--- a/test/COFF/secrel-common.s
+++ b/test/COFF/secrel-common.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc %s -filetype=obj -triple=x86_64-windows-msvc -o %t.obj
 # RUN: lld-link -entry:main -nodefaultlib %t.obj -out:%t.exe
 # RUN: llvm-readobj %t.exe -sections -section-data | FileCheck %s
diff --git a/test/COFF/sort-debug.test b/test/COFF/sort-debug.test
index 3bad013..5e2701b 100644
--- a/test/COFF/sort-debug.test
+++ b/test/COFF/sort-debug.test
@@ -1,15 +1,28 @@
 # RUN: yaml2obj < %s > %t.obj
 # RUN: lld-link /debug /out:%t.exe /entry:main %t.obj
 # RUN: llvm-readobj -sections %t.exe | FileCheck %s
+# RUN: lld-link /debug:dwarf /out:%t.exe /entry:main %t.obj
+# RUN: llvm-readobj -sections %t.exe | FileCheck %s
+# RUN: lld-link /out:%t.exe /entry:main %t.obj
+# RUN: llvm-readobj -sections %t.exe | FileCheck -check-prefix=NODEBUG %s
+# RUN: lld-link /debug:symtab /out:%t.exe /entry:main %t.obj
+# RUN: llvm-readobj -sections %t.exe | FileCheck -check-prefix=NODEBUG %s
 
 # CHECK: Name: .text
+# CHECK: Name: .reloc
 # CHECK: Name: .debug_abbrev
 # CHECK: Name: .debug_info
 # CHECK: Name: .debug_line
 # CHECK: Name: .debug_pubnames
 # CHECK: Name: .debug_pubtypes
-# CHECK: Name: .reloc
 
+# NODEBUG: Name: .text
+# NODEBUG: Name: .reloc
+# NODEBUG-NOT: Name: .debug_abbrev
+# NODEBUG-NOT: Name: .debug_info
+# NODEBUG-NOT: Name: .debug_line
+# NODEBUG-NOT: Name: .debug_pubnames
+# NODEBUG-NOT: Name: .debug_pubtypes
 
 --- !COFF
 header:
diff --git a/test/COFF/subsystem-inference32.test b/test/COFF/subsystem-inference32.test
new file mode 100644
index 0000000..8e66a85
--- /dev/null
+++ b/test/COFF/subsystem-inference32.test
@@ -0,0 +1,74 @@
+# RUN: sed -e s/ENTRYNAME/_main/ %s | yaml2obj > %t.obj
+# RUN: lld-link /out:%t.exe %t.obj
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=MAIN %s
+
+# RUN: sed s/ENTRYNAME/_wmain/ %s | yaml2obj > %t.obj
+# RUN: lld-link /out:%t.exe %t.obj
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=WMAIN %s
+
+# RUN: sed s/ENTRYNAME/_WinMain@16/ %s | yaml2obj > %t.obj
+# RUN: lld-link /out:%t.exe %t.obj
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=WINMAIN %s
+
+# RUN: sed s/ENTRYNAME/_wWinMain@16/ %s | yaml2obj > %t.obj
+# RUN: lld-link /out:%t.exe %t.obj
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck -check-prefix=WWINMAIN %s
+
+# MAIN:     Subsystem: IMAGE_SUBSYSTEM_WINDOWS_CUI
+# WMAIN:    Subsystem: IMAGE_SUBSYSTEM_WINDOWS_CUI
+# WINMAIN:  Subsystem: IMAGE_SUBSYSTEM_WINDOWS_GUI
+# WWINMAIN: Subsystem: IMAGE_SUBSYSTEM_WINDOWS_GUI
+
+--- !COFF
+header:
+  Machine:         IMAGE_FILE_MACHINE_I386
+  Characteristics: []
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     B82A000000C3
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+    SectionDefinition:
+      Length:          6
+      NumberOfRelocations: 0
+      NumberOfLinenumbers: 0
+      CheckSum:        0
+      Number:          0
+  - Name:            ENTRYNAME
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            _mainCRTStartup
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            _wmainCRTStartup
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            _WinMainCRTStartup
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+  - Name:            _wWinMainCRTStartup
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...
diff --git a/test/COFF/symtab-gc.s b/test/COFF/symtab-gc.s
new file mode 100644
index 0000000..5552639
--- /dev/null
+++ b/test/COFF/symtab-gc.s
@@ -0,0 +1,27 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-windows-msvc -o %tobject.obj %S/Inputs/object.s
+# RUN: lld-link -dll -entry:f -out:%t.dll -implib:%t.lib %tobject.obj
+# RUN: llvm-mc -filetype=obj -triple=x86_64-windows-msvc -o %tmain.obj %s
+# RUN: lld-link -entry:main -out:%t.exe -opt:ref -debug:dwarf %tmain.obj %t.lib
+# RUN: llvm-readobj -coff-imports %t.exe | FileCheck %s
+
+# CHECK-NOT: Symbol: f
+
+	.def	 main;
+	.scl	2;
+	.type	32;
+	.endef
+	.section	.text,"xr",one_only,main
+	.globl	main
+main:
+	retq
+
+	.def	 stripped;
+	.scl	3;
+	.type	32;
+	.endef
+	.section	.text,"xr",one_only,stripped
+stripped:
+	callq	__imp_f
+	retq
diff --git a/test/COFF/symtab.test b/test/COFF/symtab.test
index 49302d7..bedcd2f 100644
--- a/test/COFF/symtab.test
+++ b/test/COFF/symtab.test
@@ -3,6 +3,8 @@
 # RUN: llvm-readobj -symbols %t.exe | FileCheck %s
 # RUN: lld-link /debug:dwarf /opt:noref /out:%t.exe /entry:main %t.obj %p/Inputs/std64.lib
 # RUN: llvm-readobj -symbols %t.exe | FileCheck %s
+# RUN: lld-link /debug:symtab /opt:noref /out:%t.exe /entry:main %t.obj %p/Inputs/std64.lib
+# RUN: llvm-readobj -symbols %t.exe | FileCheck %s
 
 # RUN: lld-link /debug /out:%t.exe /entry:main %t.obj %p/Inputs/std64.lib
 # RUN: llvm-readobj -symbols %t.exe | FileCheck -check-prefix=NO %s
diff --git a/test/COFF/thunk-replace.s b/test/COFF/thunk-replace.s
new file mode 100644
index 0000000..2d47fcc
--- /dev/null
+++ b/test/COFF/thunk-replace.s
@@ -0,0 +1,15 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -triple=x86_64-win32 %s -filetype=obj -o %t.main.obj
+# RUN: llvm-mc -triple=x86_64-win32 %p/Inputs/otherFunc.s -filetype=obj -o %t.other.obj
+# RUN: llvm-ar rcs %t.other.lib %t.other.obj
+# RUN: not lld-link -out:%t.exe -entry:main %t.main.obj %p/Inputs/std64.lib %t.other.lib -opt:noref 2>&1 | FileCheck %s
+# CHECK: MessageBoxA was replaced
+
+.global main
+.text
+main:
+  callq MessageBoxA
+  callq ExitProcess
+  callq otherFunc
+  ret
diff --git a/test/COFF/undefined-symbol-cv.s b/test/COFF/undefined-symbol-cv.s
index be57832..31a44c3 100644
--- a/test/COFF/undefined-symbol-cv.s
+++ b/test/COFF/undefined-symbol-cv.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -triple=x86_64-windows-msvc -filetype=obj -o %t.obj %s
 # RUN: not lld-link /out:%t.exe %t.obj 2>&1 | FileCheck %s
 
diff --git a/test/COFF/undefined-symbol.s b/test/COFF/undefined-symbol.s
index ee146ad..5d002d8 100644
--- a/test/COFF/undefined-symbol.s
+++ b/test/COFF/undefined-symbol.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -triple=x86_64-windows-msvc -filetype=obj -o %t.obj %s
 # RUN: not lld-link /out:%t.exe %t.obj 2>&1 | FileCheck %s
 
diff --git a/test/COFF/weak-external.test b/test/COFF/weak-external.test
index 141d5ed..352b14f 100644
--- a/test/COFF/weak-external.test
+++ b/test/COFF/weak-external.test
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: yaml2obj %s > %t.obj
 # RUN: llvm-as -o %t.lto.obj %S/Inputs/weak-external.ll
 # RUN: lld-link /out:%t1.exe /entry:g /subsystem:console %t.obj
diff --git a/test/COFF/weak-external2.test b/test/COFF/weak-external2.test
index 30101d7..dd96620 100644
--- a/test/COFF/weak-external2.test
+++ b/test/COFF/weak-external2.test
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: yaml2obj %s > %t.obj
 # RUN: llvm-as -o %t.lto.obj %S/Inputs/weak-external2.ll
 # RUN: lld-link /out:%t.exe /entry:g /subsystem:console %t.obj %t.lto.obj
diff --git a/test/COFF/weak-external3.test b/test/COFF/weak-external3.test
index 8d2bde2..480b09b 100644
--- a/test/COFF/weak-external3.test
+++ b/test/COFF/weak-external3.test
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: yaml2obj %s > %t.obj
 # RUN: llvm-as -o %t.lto.obj %S/Inputs/weak-external3.ll
 # RUN: lld-link /out:%t1.exe /entry:f /subsystem:console /lldmap:%t1.map %t.lto.obj
diff --git a/test/COFF/wholearchive.s b/test/COFF/wholearchive.s
index da99763..cb19d4d 100644
--- a/test/COFF/wholearchive.s
+++ b/test/COFF/wholearchive.s
@@ -1,6 +1,7 @@
-# REQEUIRES: x86
+# REQUIRES: x86
 
 # RUN: yaml2obj < %p/Inputs/export.yaml > %t.archive.obj
+# RUN: rm -f %t.archive.lib
 # RUN: llvm-ar rcs %t.archive.lib %t.archive.obj
 # RUN: llvm-mc -triple=x86_64-windows-msvc %s -filetype=obj -o %t.main.obj
 
@@ -10,6 +11,14 @@
 # RUN: lld-link -dll -out:%t.dll -entry:main %t.main.obj -wholearchive %t.archive.lib -implib:%t.lib
 # RUN: llvm-readobj %t.lib | FileCheck %s -check-prefix CHECK-IMPLIB
 
+# RUN: lld-link -dll -out:%t.dll -entry:main %t.main.obj %t.archive.lib -wholearchive:%t.archive.lib -implib:%t.lib
+# RUN: llvm-readobj %t.lib | FileCheck %s -check-prefix CHECK-IMPLIB
+
+# RUN: mkdir -p %t.dir
+# RUN: cp %t.archive.lib %t.dir/foo.lib
+# RUN: lld-link -dll -out:%t.dll -entry:main -libpath:%t.dir %t.main.obj %t.dir/./foo.lib -wholearchive:foo.lib -implib:%t.lib
+# RUN: llvm-readobj %t.lib | FileCheck %s -check-prefix CHECK-IMPLIB
+
 # CHECK-IMPLIB: Symbol: __imp_exportfn3
 # CHECK-IMPLIB: Symbol: exportfn3
 
diff --git a/test/ELF/Inputs/arm-vfp-arg-base.s b/test/ELF/Inputs/arm-vfp-arg-base.s
new file mode 100644
index 0000000..9625c0a
--- /dev/null
+++ b/test/ELF/Inputs/arm-vfp-arg-base.s
@@ -0,0 +1,16 @@
+	.arch armv7-a
+	.eabi_attribute 20, 1
+	.eabi_attribute 21, 1
+	.eabi_attribute 23, 3
+	.eabi_attribute 24, 1
+	.eabi_attribute 25, 1
+	.eabi_attribute 26, 2
+	.eabi_attribute 30, 6
+	.eabi_attribute 34, 1
+	.eabi_attribute 18, 4
+        .eabi_attribute 28, 0 // Tag_ABI_VFP_args = 0 (AAPCS, Base variant)
+
+        .syntax unified
+        .global f0
+        .type f0, %function
+f0:     bx lr
diff --git a/test/ELF/Inputs/arm-vfp-arg-compat.s b/test/ELF/Inputs/arm-vfp-arg-compat.s
new file mode 100644
index 0000000..78f6ef6
--- /dev/null
+++ b/test/ELF/Inputs/arm-vfp-arg-compat.s
@@ -0,0 +1,16 @@
+	.arch armv7-a
+	.eabi_attribute 20, 1
+	.eabi_attribute 21, 1
+	.eabi_attribute 23, 3
+	.eabi_attribute 24, 1
+	.eabi_attribute 25, 1
+	.eabi_attribute 26, 2
+	.eabi_attribute 30, 6
+	.eabi_attribute 34, 1
+	.eabi_attribute 18, 4
+        .eabi_attribute 28, 3 // Tag_ABI_VFP_args = 3 (Compatible with all)
+
+        .syntax unified
+        .global f3
+        .type f3, %function
+f3:     bx lr
diff --git a/test/ELF/Inputs/arm-vfp-arg-toolchain.s b/test/ELF/Inputs/arm-vfp-arg-toolchain.s
new file mode 100644
index 0000000..bc8eb14
--- /dev/null
+++ b/test/ELF/Inputs/arm-vfp-arg-toolchain.s
@@ -0,0 +1,15 @@
+	.arch armv7-a
+	.eabi_attribute 20, 1
+	.eabi_attribute 21, 1
+	.eabi_attribute 23, 3
+	.eabi_attribute 24, 1
+	.eabi_attribute 25, 1
+	.eabi_attribute 26, 2
+	.eabi_attribute 30, 6
+	.eabi_attribute 34, 1
+	.eabi_attribute 18, 4
+	.eabi_attribute 28, 2 // Tag_ABI_VFP_args = 2 (Toolchain specific)
+        .syntax unified
+        .global f2
+        .type f1, %function
+f2:     bx lr
diff --git a/test/ELF/Inputs/arm-vfp-arg-vfp.s b/test/ELF/Inputs/arm-vfp-arg-vfp.s
new file mode 100644
index 0000000..e8c8a66
--- /dev/null
+++ b/test/ELF/Inputs/arm-vfp-arg-vfp.s
@@ -0,0 +1,15 @@
+	.arch armv7-a
+	.eabi_attribute 20, 1
+	.eabi_attribute 21, 1
+	.eabi_attribute 23, 3
+	.eabi_attribute 24, 1
+	.eabi_attribute 25, 1
+	.eabi_attribute 26, 2
+	.eabi_attribute 30, 6
+	.eabi_attribute 34, 1
+	.eabi_attribute 18, 4
+	.eabi_attribute 28, 1 // Tag_ABI_VFP_args = 1 (AAPCS, VFP variant)
+        .syntax unified
+        .global f1
+        .type f1, %function
+f1:     bx lr
diff --git a/test/ELF/Inputs/comdat-discarded-reloc.s b/test/ELF/Inputs/comdat-discarded-reloc.s
new file mode 100644
index 0000000..9526f5a
--- /dev/null
+++ b/test/ELF/Inputs/comdat-discarded-reloc.s
@@ -0,0 +1,6 @@
+.section .text.bar1,"aG",@progbits,group,comdat
+
+.section .text.bar2
+.global bar
+bar:
+ .quad .text.bar1
diff --git a/test/ELF/Inputs/copy-rel-tls.s b/test/ELF/Inputs/copy-rel-tls.s
new file mode 100644
index 0000000..c34b692
--- /dev/null
+++ b/test/ELF/Inputs/copy-rel-tls.s
@@ -0,0 +1,12 @@
+.bss
+.global foo
+.type foo, @object
+.size foo, 4
+foo:
+
+.section .tbss,"awT",@nobits
+.global tfoo
+.skip 0x2000
+.type tfoo,@object
+.size tfoo, 4
+tfoo:
diff --git a/test/ELF/Inputs/copy-rel-version.s b/test/ELF/Inputs/copy-rel-version.s
new file mode 100644
index 0000000..36bb1ba
--- /dev/null
+++ b/test/ELF/Inputs/copy-rel-version.s
@@ -0,0 +1,11 @@
+.data
+.global foo@v1
+.type foo@v1, @object
+.size foo@v1, 4
+.global foo@@v2
+.type foo@@v2, @object
+.size foo@@v2, 8
+foo@v1:
+foo@@v2:
+.int 0
+.int 0
diff --git a/test/ELF/Inputs/copy-relocation-zero-abs-addr.s b/test/ELF/Inputs/copy-relocation-zero-abs-addr.s
new file mode 100644
index 0000000..da81e03
--- /dev/null
+++ b/test/ELF/Inputs/copy-relocation-zero-abs-addr.s
@@ -0,0 +1,7 @@
+.globl ver1
+.globl ver2
+ ver1 = 0x0
+ ver2 = 0x0
+
+.type foo,@object
+.comm foo,16,16
diff --git a/test/ELF/Inputs/copy-relocation-zero-nonabs-addr.s b/test/ELF/Inputs/copy-relocation-zero-nonabs-addr.s
new file mode 100644
index 0000000..26ac7be
--- /dev/null
+++ b/test/ELF/Inputs/copy-relocation-zero-nonabs-addr.s
@@ -0,0 +1,7 @@
+.balign 1024
+.type foo,@object
+.globl foo
+goo:
+foo:
+  .long 0
+  .size foo,4
diff --git a/test/ELF/Inputs/copy-relocation-zero-nonabs-addr.script b/test/ELF/Inputs/copy-relocation-zero-nonabs-addr.script
new file mode 100644
index 0000000..a580723
--- /dev/null
+++ b/test/ELF/Inputs/copy-relocation-zero-nonabs-addr.script
@@ -0,0 +1,3 @@
+SECTIONS {
+  goo = 0;
+};
diff --git a/test/ELF/Inputs/eh-frame-pcrel-overflow.s b/test/ELF/Inputs/eh-frame-pcrel-overflow.s
new file mode 100644
index 0000000..7b3cff8
--- /dev/null
+++ b/test/ELF/Inputs/eh-frame-pcrel-overflow.s
@@ -0,0 +1,25 @@
+.text
+.global foo
+foo:
+  ret
+
+.section .eh_frame, "a"
+  .long 12   # Size
+  .long 0x00 # ID
+  .byte 0x01 # Version.
+
+  .byte 0x52 # Augmentation string: 'R','\0'
+  .byte 0x00
+
+  .byte 0x01
+
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x00 # DW_EH_PE_absptr
+
+  .byte 0xFF
+
+  .long 12  # Size
+  .long 0x14 # ID
+  .quad foo + 0x90000000
diff --git a/test/ELF/Inputs/exclude-libs.ll b/test/ELF/Inputs/exclude-libs.ll
new file mode 100644
index 0000000..8bd9470
--- /dev/null
+++ b/test/ELF/Inputs/exclude-libs.ll
@@ -0,0 +1,3 @@
+@fn2 = global void()* null;
+
+target triple = "x86_64-unknown-linux"
diff --git a/test/ELF/Inputs/hexagon.s b/test/ELF/Inputs/hexagon.s
new file mode 100644
index 0000000..921a0c4
--- /dev/null
+++ b/test/ELF/Inputs/hexagon.s
@@ -0,0 +1,6 @@
+.global _start
+_start:
+  nop
+.global foo
+foo:
+  jumpr lr
diff --git a/test/ELF/Inputs/icf-absolute2.s b/test/ELF/Inputs/icf-absolute2.s
new file mode 100644
index 0000000..1092c58
--- /dev/null
+++ b/test/ELF/Inputs/icf-absolute2.s
@@ -0,0 +1,3 @@
+.globl a1, a2
+a1 = 1
+a2 = 2
diff --git a/test/ELF/Inputs/icf-safe.s b/test/ELF/Inputs/icf-safe.s
new file mode 100644
index 0000000..02393f3
--- /dev/null
+++ b/test/ELF/Inputs/icf-safe.s
@@ -0,0 +1,9 @@
+.section .text.non_addrsig1,"ax",@progbits
+.globl non_addrsig1
+non_addrsig1:
+ret
+
+.section .text.non_addrsig2,"ax",@progbits
+.globl non_addrsig2
+non_addrsig2:
+ret
diff --git a/test/ELF/Inputs/mips-64-got-load.s b/test/ELF/Inputs/mips-64-got-load.s
new file mode 100644
index 0000000..dffc6fb
--- /dev/null
+++ b/test/ELF/Inputs/mips-64-got-load.s
@@ -0,0 +1,8 @@
+  .text
+  .global foo1
+foo1:
+  ld $2, %got_disp(local1)($gp)
+
+  .bss
+local1:
+  .word 0
diff --git a/test/ELF/Inputs/mips-mgot-1.s b/test/ELF/Inputs/mips-mgot-1.s
new file mode 100644
index 0000000..def6e58
--- /dev/null
+++ b/test/ELF/Inputs/mips-mgot-1.s
@@ -0,0 +1,10 @@
+  .text
+  .global foo1
+foo1:
+  addiu  $2, $2, %gottprel(tls0)  # tls got entry
+  addiu  $2, $2, %gottprel(tls1)  # tls got entry
+
+  .section .tdata,"awT",%progbits
+  .global tls1
+tls1:
+  .word 0
diff --git a/test/ELF/Inputs/mips-mgot-2.s b/test/ELF/Inputs/mips-mgot-2.s
new file mode 100644
index 0000000..4f6a92d
--- /dev/null
+++ b/test/ELF/Inputs/mips-mgot-2.s
@@ -0,0 +1,17 @@
+  .text
+  .global foo2
+foo2:
+  lw     $2, %got(.data)($gp)     # page entry
+  addi   $2, $2, %lo(.data)
+  lw     $2, %call16(foo0)($gp)   # global entry
+  lw     $2, %call16(foo2)($gp)   # global entry
+  addiu  $2, $2, %tlsgd(tls0)     # tls gd entry
+  addiu  $2, $2, %gottprel(tls0)  # tls got entry
+
+  .data
+  .space 0x20000
+
+  .section .tdata,"awT",%progbits
+  .global tls2
+tls2:
+  .word 0
diff --git a/test/ELF/Inputs/mips-n32-rels.o b/test/ELF/Inputs/mips-n32-rels.o
deleted file mode 100644
index 88cbce6..0000000
--- a/test/ELF/Inputs/mips-n32-rels.o
+++ /dev/null
Binary files differ
diff --git a/test/ELF/Inputs/x86-64-split-stack-extra.s b/test/ELF/Inputs/x86-64-split-stack-extra.s
new file mode 100644
index 0000000..29c4218
--- /dev/null
+++ b/test/ELF/Inputs/x86-64-split-stack-extra.s
@@ -0,0 +1,10 @@
+# This file is split out to provide better code coverage.
+	.global	split
+	.type	split,@function
+split:
+	retq
+
+	.size	split,. - split
+
+	.section	.note.GNU-stack,"",@progbits
+	.section	.note.GNU-split-stack,"",@progbits
diff --git a/test/ELF/Inputs/x86-64-split-stack-main.s b/test/ELF/Inputs/x86-64-split-stack-main.s
new file mode 100644
index 0000000..3be9fac
--- /dev/null
+++ b/test/ELF/Inputs/x86-64-split-stack-main.s
@@ -0,0 +1,16 @@
+	.text
+
+	.global	non_split
+	.type	non_split,@function
+non_split:
+	retq
+	.size	non_split,. - non_split
+
+	.global non_function_text_symbol
+non_function_text_symbol:
+	.byte 0x01
+	.type	non_function_text_symbol,@STT_OBJECT
+	.size	non_function_text_symbol, 1
+
+
+	.section	.note.GNU-stack,"",@progbits
diff --git a/test/ELF/aarch64-call26-thunk.s b/test/ELF/aarch64-call26-thunk.s
index 0fe99ce..067f6db 100644
--- a/test/ELF/aarch64-call26-thunk.s
+++ b/test/ELF/aarch64-call26-thunk.s
@@ -1,8 +1,8 @@
+// REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-freebsd %S/Inputs/abs.s -o %tabs
 // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-freebsd %s -o %t
 // RUN: ld.lld %t %tabs -o %t2 2>&1
 // RUN: llvm-objdump -d -triple=aarch64-pc-freebsd %t2 | FileCheck %s
-// REQUIRES: aarch64
 
 .text
 .globl _start
diff --git a/test/ELF/aarch64-condb-reloc.s b/test/ELF/aarch64-condb-reloc.s
index 23c16c2..8a75814 100644
--- a/test/ELF/aarch64-condb-reloc.s
+++ b/test/ELF/aarch64-condb-reloc.s
@@ -1,3 +1,4 @@
+# REQUIRES: aarch64
 # RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-freebsd %p/Inputs/aarch64-condb-reloc.s -o %t1
 # RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-freebsd %s -o %t2
 # RUN: ld.lld %t1 %t2 -o %t
@@ -5,7 +6,6 @@
 # RUN: ld.lld -shared %t1 %t2 -o %t3
 # RUN: llvm-objdump -d %t3 | FileCheck -check-prefix=DSO %s
 # RUN: llvm-readobj -s -r %t3 | FileCheck -check-prefix=DSOREL %s
-# REQUIRES: aarch64
 
 # 0x11024 - 36 = 0x11000
 # 0x11028 - 24 = 0x11010
diff --git a/test/ELF/aarch64-copy.s b/test/ELF/aarch64-copy.s
index ffdb01b..32e1c76 100644
--- a/test/ELF/aarch64-copy.s
+++ b/test/ELF/aarch64-copy.s
@@ -90,4 +90,4 @@
 
 // RODATA: Contents of section .rodata:
 // S(z) = 0x40014
-// RODATA-NEXT:  10246 14000400
+// RODATA-NEXT:  102e0 14000400
diff --git a/test/ELF/aarch64-cortex-a53-843419-cli.s b/test/ELF/aarch64-cortex-a53-843419-cli.s
index 30abc8f..9c1d485 100644
--- a/test/ELF/aarch64-cortex-a53-843419-cli.s
+++ b/test/ELF/aarch64-cortex-a53-843419-cli.s
@@ -1,6 +1,6 @@
 // REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
-// RUN: not ld.lld %t -fix-cortex-a53-843419 -o %t2 2>&1 | FileCheck %s
+// RUN: not ld.lld %t -fix-cortex-a53-843419 -o /dev/null 2>&1 | FileCheck %s
 
 // CHECK: --fix-cortex-a53-843419 is only supported on AArch64 targets.
 .globl entry
diff --git a/test/ELF/aarch64-cortex-a53-843419-nopatch.s b/test/ELF/aarch64-cortex-a53-843419-nopatch.s
index 389bf45..4f34ea2 100644
--- a/test/ELF/aarch64-cortex-a53-843419-nopatch.s
+++ b/test/ELF/aarch64-cortex-a53-843419-nopatch.s
@@ -1,6 +1,6 @@
 // REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-linux %s -o %t.o
-// RUN: ld.lld -fix-cortex-a53-843419 -verbose -t %t.o -o %t2 | FileCheck %s
+// RUN: ld.lld -fix-cortex-a53-843419 -verbose -t %t.o -o /dev/null | FileCheck %s
 // Test cases for Cortex-A53 Erratum 843419 that we don't expect to recognize
 // as needing a patch as one or more of the conditions isn't satisfied.
 // See ARM-EPM-048406 Cortex_A53_MPCore_Software_Developers_Errata_Notice.pdf
diff --git a/test/ELF/aarch64-cortex-a53-843419-recognize.s b/test/ELF/aarch64-cortex-a53-843419-recognize.s
index a9d0692..174f181 100644
--- a/test/ELF/aarch64-cortex-a53-843419-recognize.s
+++ b/test/ELF/aarch64-cortex-a53-843419-recognize.s
@@ -30,7 +30,7 @@
 // CHECK: t3_ff8_ldr:
 // CHECK-NEXT:    21ff8:        e0 01 00 f0     adrp    x0, #258048
 // CHECK-NEXT:    21ffc:        21 00 40 f9     ldr             x1, [x1]
-// CHECK-FIX:     22000:        03 b8 00 14     b       #188428
+// CHECK-FIX:     22000:        03 c8 00 14     b       #204812
 // CHECK-NOFIX:   22000:        00 00 40 f9     ldr             x0, [x0]
 // CHECK-NEXT:    22004:        c0 03 5f d6     ret
         .section .text.01, "ax", %progbits
@@ -48,7 +48,7 @@
 // CHECK: t3_ff8_ldrsimd:
 // CHECK-NEXT:    23ff8:        e0 01 00 b0     adrp    x0, #249856
 // CHECK-NEXT:    23ffc:        21 00 40 bd     ldr             s1, [x1]
-// CHECK-FIX:     24000:        05 b0 00 14     b       #180244
+// CHECK-FIX:     24000:        05 c0 00 14     b       #196628
 // CHECK-NOFIX:   24000:        02 04 40 f9     ldr     x2, [x0, #8]
 // CHECK-NEXT:    24004:        c0 03 5f d6     ret
         .section .text.02, "ax", %progbits
@@ -66,7 +66,7 @@
 // CHECK: t3_ffc_ldrpost:
 // CHECK-NEXT:    25ffc:        c0 01 00 f0     adrp    x0, #241664
 // CHECK-NEXT:    26000:        21 84 40 bc     ldr     s1, [x1], #8
-// CHECK-FIX:     26004:        06 a8 00 14     b       #172056
+// CHECK-FIX:     26004:        06 b8 00 14     b       #188440
 // CHECK-NOFIX:   26004:        03 08 40 f9     ldr     x3, [x0, #16]
 // CHECK-NEXT:    26008:        c0 03 5f d6     ret
         .section .text.03, "ax", %progbits
@@ -84,7 +84,7 @@
 // CHECK: t3_ff8_strpre:
 // CHECK-NEXT:    27ff8:        c0 01 00 b0     adrp    x0, #233472
 // CHECK-NEXT:    27ffc:        21 8c 00 bc     str     s1, [x1, #8]!
-// CHECK-FIX:     28000:        09 a0 00 14     b       #163876
+// CHECK-FIX:     28000:        09 b0 00 14     b       #180260
 // CHECK-NOFIX:   28000:        02 00 40 f9     ldr             x2, [x0]
 // CHECK-NEXT:    28004:        c0 03 5f d6     ret
         .section .text.04, "ax", %progbits
@@ -102,7 +102,7 @@
 // CHECK: t3_ffc_str:
 // CHECK-NEXT:    29ffc:        bc 01 00 f0     adrp    x28, #225280
 // CHECK-NEXT:    2a000:        42 00 00 f9     str             x2, [x2]
-// CHECK-FIX:     2a004:        0a 98 00 14     b       #155688
+// CHECK-FIX:     2a004:        0a a8 00 14     b       #172072
 // CHECK-NOFIX:   2a004:        9c 07 00 f9     str     x28, [x28, #8]
 // CHECK-NEXT:    2a008:        c0 03 5f d6     ret
         .section .text.05, "ax", %progbits
@@ -120,7 +120,7 @@
 // CHECK: t3_ffc_strsimd:
 // CHECK-NEXT:    2bffc:        bc 01 00 b0     adrp    x28, #217088
 // CHECK-NEXT:    2c000:        44 00 00 b9     str             w4, [x2]
-// CHECK-FIX:     2c004:        0c 90 00 14     b       #147504
+// CHECK-FIX:     2c004:        0c a0 00 14     b       #163888
 // CHECK-NOFIX:   2c004:        84 0b 00 f9     str     x4, [x28, #16]
 // CHECK-NEXT:    2c008:        c0 03 5f d6     ret
         .section .text.06, "ax", %progbits
@@ -138,7 +138,7 @@
 // CHECK: t3_ff8_ldrunpriv:
 // CHECK-NEXT:    2dff8:        9d 01 00 f0     adrp    x29, #208896
 // CHECK-NEXT:    2dffc:        41 08 40 38     ldtrb           w1, [x2]
-// CHECK-FIX:     2e000:        0f 88 00 14     b       #139324
+// CHECK-FIX:     2e000:        0f 98 00 14     b       #155708
 // CHECK-NOFIX:   2e000:        bd 03 40 f9     ldr             x29, [x29]
 // CHECK-NEXT:    2e004:        c0 03 5f d6     ret
         .section .text.07, "ax", %progbits
@@ -156,7 +156,7 @@
 // CHECK: t3_ffc_ldur:
 // CHECK-NEXT:    2fffc:        9d 01 00 b0     adrp    x29, #200704
 // CHECK-NEXT:    30000:        42 40 40 b8     ldur    w2, [x2, #4]
-// CHECK-FIX:     30004:        10 80 00 14     b       #131136
+// CHECK-FIX:     30004:        10 90 00 14     b       #147520
 // CHECK-NOFIX:   30004:        bd 07 40 f9     ldr     x29, [x29, #8]
 // CHECK-NEXT:    30008:        c0 03 5f d6     ret
         .balign 4096
@@ -173,8 +173,8 @@
 // CHECK: t3_ffc_sturh:
 // CHECK-NEXT:    31ffc:        72 01 00 f0     adrp    x18, #192512
 // CHECK-NEXT:    32000:        43 40 00 78     sturh   w3, [x2, #4]
-// CHECK-FIX:     32004:        12 78 00 14     b       #122952
-// CHECK-NOFIX:   32004:       41 0a 40 f9     ldr     x1, [x18, #16]
+// CHECK-FIX:     32004:        12 88 00 14     b       #139336
+// CHECK-NOFIX:   32004:        41 0a 40 f9     ldr     x1, [x18, #16]
 // CHECK-NEXT:    32008:        c0 03 5f d6     ret
         .section .text.09, "ax", %progbits
         .balign 4096
@@ -191,7 +191,7 @@
 // CHECK: t3_ff8_literal:
 // CHECK-NEXT:    33ff8:        72 01 00 b0     adrp    x18, #184320
 // CHECK-NEXT:    33ffc:        e3 ff ff 58     ldr     x3, #-4
-// CHECK-FIX:     34000:        15 70 00 14     b       #114772
+// CHECK-FIX:     34000:        15 80 00 14     b       #131156
 // CHECK-NOFIX:   34000:        52 02 40 f9     ldr             x18, [x18]
 // CHECK-NEXT:    34004:        c0 03 5f d6     ret
         .section .text.10, "ax", %progbits
@@ -209,7 +209,7 @@
 // CHECK: t3_ffc_register:
 // CHECK-NEXT:    35ffc:        4f 01 00 f0     adrp    x15, #176128
 // CHECK-NEXT:    36000:        43 68 61 f8     ldr             x3, [x2, x1]
-// CHECK-FIX:     36004:        16 68 00 14     b       #106584
+// CHECK-FIX:     36004:        16 78 00 14     b       #122968
 // CHECK-NOFIX:   36004:        ea 05 40 f9     ldr     x10, [x15, #8]
 // CHECK-NEXT:    36008:        c0 03 5f d6     ret
         .section .text.11, "ax", %progbits
@@ -227,7 +227,7 @@
 // CHECK: t3_ff8_stp:
 // CHECK-NEXT:    37ff8:        50 01 00 b0     adrp    x16, #167936
 // CHECK-NEXT:    37ffc:        61 08 00 a9     stp             x1, x2, [x3]
-// CHECK-FIX:     38000:        19 60 00 14     b       #98404
+// CHECK-FIX:     38000:        19 70 00 14     b       #114788
 // CHECK-NOFIX:   38000:        0d 0a 40 f9     ldr     x13, [x16, #16]
 // CHECK-NEXT:    38004:        c0 03 5f d6     ret
         .section .text.12, "ax", %progbits
@@ -245,7 +245,7 @@
 // CHECK: t3_ffc_stnp:
 // CHECK-NEXT:    39ffc:        27 01 00 f0     adrp    x7, #159744
 // CHECK-NEXT:    3a000:        61 08 00 a8     stnp            x1, x2, [x3]
-// CHECK-FIX:     3a004:        1a 58 00 14     b       #90216
+// CHECK-FIX:     3a004:        1a 68 00 14     b       #106600
 // CHECK-NOFIX:   3a004:        e9 00 40 f9     ldr             x9, [x7]
 // CHECK-NEXT:    3a008:        c0 03 5f d6     ret
         .section .text.13, "ax", %progbits
@@ -263,7 +263,7 @@
 // CHECK: t3_ffc_st1singlepost:
 // CHECK-NEXT:    3bffc:        37 01 00 b0     adrp    x23, #151552
 // CHECK-NEXT:    3c000:        20 04 82 0d     st1 { v0.b }[1], [x1], x2
-// CHECK-FIX:     3c004:        1c 50 00 14     b       #82032
+// CHECK-FIX:     3c004:        1c 60 00 14     b       #98416
 // CHECK-NOFIX:   3c004:        f6 06 40 f9     ldr     x22, [x23, #8]
 // CHECK-NEXT:    3c008:        c0 03 5f d6     ret
         .section .text.14, "ax", %progbits
@@ -281,7 +281,7 @@
 // CHECK: t3_ff8_st1multiple:
 // CHECK-NEXT:    3dff8:        17 01 00 f0     adrp    x23, #143360
 // CHECK-NEXT:    3dffc:        20 a0 00 4c     st1     { v0.16b, v1.16b }, [x1]
-// CHECK-FIX:     3e000:        1f 48 00 14     b       #73852
+// CHECK-FIX:     3e000:        1f 58 00 14     b       #90236
 // CHECK-NOFIX:   3e000:        f8 0a 40 f9     ldr     x24, [x23, #16]
 // CHECK-NEXT:    3e004:        c0 03 5f d6     ret
         .section .text.15, "ax", %progbits
@@ -300,7 +300,7 @@
 // CHECK-NEXT:    3fff8:        00 01 00 b0     adrp    x0, #135168
 // CHECK-NEXT:    3fffc:        21 00 40 f9     ldr             x1, [x1]
 // CHECK-NEXT:    40000:        42 00 00 8b     add             x2, x2, x0
-// CHECK-FIX:     40004:        20 40 00 14     b       #65664
+// CHECK-FIX:     40004:        20 50 00 14     b       #82048
 // CHECK-NOFIX:   40004:        02 00 40 f9     ldr             x2, [x0]
 // CHECK-NEXT:    40008:        c0 03 5f d6     ret
         .section .text.16, "ax", %progbits
@@ -320,7 +320,7 @@
 // CHECK-NEXT:    41ffc:        fc 00 00 f0     adrp    x28, #126976
 // CHECK-NEXT:    42000:        42 00 00 f9     str             x2, [x2]
 // CHECK-NEXT:    42004:        20 00 02 cb     sub             x0, x1, x2
-// CHECK-FIX:     42008:        21 38 00 14     b       #57476
+// CHECK-FIX:     42008:        21 48 00 14     b       #73860
 // CHECK-NOFIX:   42008:        9b 07 00 f9     str     x27, [x28, #8]
 // CHECK-NEXT:    4200c:        c0 03 5f d6     ret
         .section .text.17, "ax", %progbits
@@ -340,7 +340,7 @@
 // CHECK-NEXT:    43ff8:        f0 00 00 b0     adrp    x16, #118784
 // CHECK-NEXT:    43ffc:        61 08 00 a9     stp             x1, x2, [x3]
 // CHECK-NEXT:    44000:        03 7e 10 9b     mul             x3, x16, x16
-// CHECK-FIX:     44004:        24 30 00 14     b       #49296
+// CHECK-FIX:     44004:        24 40 00 14     b       #65680
 // CHECK-NOFIX:   44004:        0e 0a 40 f9     ldr     x14, [x16, #16]
 // CHECK-NEXT:    44008:        c0 03 5f d6     ret
         .section .text.18, "ax", %progbits
@@ -360,7 +360,7 @@
 // CHECK-NEXT:    45ff8:        d0 00 00 f0     adrp    x16, #110592
 // CHECK-NEXT:    45ffc:        61 08 81 a9     stp     x1, x2, [x3, #16]!
 // CHECK-NEXT:    46000:        03 7e 10 9b     mul             x3, x16, x16
-// CHECK-FIX:     46004:        26 28 00 14     b       #41112
+// CHECK-FIX:     46004:        26 38 00 14     b       #57496
 // CHECK-NOFIX:   46004:        0e 06 40 f9     ldr     x14, [x16, #8]
 // CHECK-NEXT:    46008:        c0 03 5f d6     ret
         .section .text.19, "ax", %progbits
@@ -380,7 +380,7 @@
 // CHECK-NEXT:    47ff8:        d0 00 00 b0     adrp    x16, #102400
 // CHECK-NEXT:    47ffc:        61 08 81 a8     stp     x1, x2, [x3], #16
 // CHECK-NEXT:    48000:        03 7e 10 9b     mul             x3, x16, x16
-// CHECK-FIX:     48004:        28 20 00 14     b       #32928
+// CHECK-FIX:     48004:        28 30 00 14     b       #49312
 // CHECK-NOFIX:   48004:        0e 06 40 f9     ldr     x14, [x16, #8]
 // CHECK-NEXT:    48008:        c0 03 5f d6     ret
         .section .text.20, "ax", %progbits
@@ -400,7 +400,7 @@
 // CHECK-NEXT:    49ffc:        b0 00 00 f0     adrp    x16, #94208
 // CHECK-NEXT:    4a000:        61 08 00 ad     stp             q1, q2, [x3]
 // CHECK-NEXT:    4a004:        03 7e 10 9b     mul             x3, x16, x16
-// CHECK-FIX:     4a008:        29 18 00 14     b       #24740
+// CHECK-FIX:     4a008:        29 28 00 14     b       #41124
 // CHECK-NOFIX:   4a008:        0e 06 40 f9     ldr     x14, [x16, #8]
 // CHECK-NEXT:    4a00c:        c0 03 5f d6     ret
         .section .text.21, "ax", %progbits
@@ -420,7 +420,7 @@
 // CHECK-NEXT:    4bffc:        a7 00 00 b0     adrp    x7, #86016
 // CHECK-NEXT:    4c000:        61 08 00 a8     stnp            x1, x2, [x3]
 // CHECK-NEXT:    4c004:        1f 20 03 d5     nop
-// CHECK-FIX:     4c008:        2b 10 00 14     b       #16556
+// CHECK-FIX:     4c008:        2b 20 00 14     b       #32940
 // CHECK-NOFIX:   4c008:        ea 00 40 f9     ldr             x10, [x7]
 // CHECK-NEXT:    4c00c:        c0 03 5f d6     ret
         .section .text.22, "ax", %progbits
@@ -440,7 +440,7 @@
 // CHECK-NEXT:    4dffc:        98 00 00 f0     adrp    x24, #77824
 // CHECK-NEXT:    4e000:        20 80 00 4d     st1 { v0.s }[2], [x1]
 // CHECK-NEXT:    4e004:        f6 06 40 f9     ldr     x22, [x23, #8]
-// CHECK-FIX:     4e008:        2d 08 00 14     b       #8372
+// CHECK-FIX:     4e008:        2d 18 00 14     b       #24756
 // CHECK-NOFIX:   4e008:        18 ff 3f f9     str     x24, [x24, #32760]
 // CHECK-NEXT:    4e00c:        c0 03 5f d6     ret
         .section .text.23, "ax", %progbits
@@ -459,7 +459,7 @@
 // CHECK: t3_ff8_ldr_once:
 // CHECK-NEXT:    4fff8:        80 00 00 b0     adrp    x0, #69632
 // CHECK-NEXT:    4fffc:        20 70 82 4c     st1     { v0.16b }, [x1], x2
-// CHECK-FIX:     50000:        31 00 00 14     b       #196
+// CHECK-FIX:     50000:        31 10 00 14     b       #16580
 // CHECK-NOFIX:   50000:        01 08 40 f9     ldr     x1, [x0, #16]
 // CHECK-NEXT:    50004:        02 08 40 f9     ldr     x2, [x0, #16]
 // CHECK-NEXT:    50008:        c0 03 5f d6     ret
@@ -475,6 +475,46 @@
         ldr x2, [x0, #16]
         ret
 
+// CHECK-PRINT: detected cortex-a53-843419 erratum sequence starting at 51FF8 in unpatched output.
+// CHECK: t3_ff8_ldxr:
+// CHECK-NEXT:    51ff8:        60 00 00 f0     adrp    x0, #61440
+// CHECK-NEXT:    51ffc:        03 7c 5f c8     ldxr    x3, [x0]
+// CHECK-FIX:     52000:        33 08 00 14     b       #8396
+// CHECK-NOFIX:   52000:        01 08 40 f9     ldr     x1, [x0, #16]
+// CHECK:         52004:        02 08 40 f9     ldr     x2, [x0, #16]
+// CHECK-NEXT:    52008:        c0 03 5f d6     ret
+        .section .text.25, "ax", %progbits
+        .balign 4096
+        .globl t3_ff8_ldxr
+        .type t3_ff8_ldxr, %function
+        .space 4096 - 8
+t3_ff8_ldxr:
+        adrp x0, dat3
+        ldxr x3, [x0]
+        ldr x1, [x0, #16]
+        ldr x2, [x0, #16]
+        ret
+
+// CHECK-PRINT: detected cortex-a53-843419 erratum sequence starting at 53FF8 in unpatched output.
+// CHECK: t3_ff8_stxr:
+// CHECK-NEXT:    53ff8:        60 00 00 b0     adrp    x0, #53248
+// CHECK-NEXT:    53ffc:        03 7c 04 c8     stxr    w4, x3, [x0]
+// CHECK-FIX:     54000:        35 00 00 14     b       #212
+// CHECK-NOFIX:   54000:        01 08 40 f9     ldr     x1, [x0, #16]
+// CHECK:         54004:        02 08 40 f9     ldr     x2, [x0, #16]
+// CHECK-NEXT:    54008:        c0 03 5f d6     ret
+        .section .text.26, "ax", %progbits
+        .balign 4096
+        .globl t3_ff8_stxr
+        .type t3_ff8_stxr, %function
+        .space 4096 - 8
+t3_ff8_stxr:
+        adrp x0, dat3
+        stxr w4, x3, [x0]
+        ldr x1, [x0, #16]
+        ldr x2, [x0, #16]
+        ret
+
         .text
         .globl _start
         .type _start, %function
@@ -482,78 +522,83 @@
         ret
 
 // CHECK-FIX: __CortexA53843419_22000:
-// CHECK-FIX-NEXT:    5000c:    00 00 40 f9     ldr             x0, [x0]
-// CHECK-FIX-NEXT:    50010:    fd 47 ff 17     b       #-188428
+// CHECK-FIX-NEXT:    5400c:    00 00 40 f9     ldr     x0, [x0]
+// CHECK-FIX-NEXT:    54010:    fd 37 ff 17     b       #-204812
 // CHECK-FIX: __CortexA53843419_24000:
-// CHECK-FIX-NEXT:    50014:    02 04 40 f9     ldr     x2, [x0, #8]
-// CHECK-FIX-NEXT:    50018:    fb 4f ff 17     b       #-180244
+// CHECK-FIX-NEXT:    54014:    02 04 40 f9     ldr     x2, [x0, #8]
+// CHECK-FIX-NEXT:    54018:    fb 3f ff 17     b       #-196628
 // CHECK-FIX: __CortexA53843419_26004:
-// CHECK-FIX-NEXT:    5001c:    03 08 40 f9     ldr     x3, [x0, #16]
-// CHECK-FIX-NEXT:    50020:    fa 57 ff 17     b       #-172056
+// CHECK-FIX-NEXT:    5401c:    03 08 40 f9     ldr     x3, [x0, #16]
+// CHECK-FIX-NEXT:    54020:    fa 47 ff 17     b       #-188440
 // CHECK-FIX: __CortexA53843419_28000:
-// CHECK-FIX-NEXT:    50024:    02 00 40 f9     ldr             x2, [x0]
-// CHECK-FIX-NEXT:    50028:    f7 5f ff 17     b       #-163876
+// CHECK-FIX-NEXT:    54024:    02 00 40 f9     ldr     x2, [x0]
+// CHECK-FIX-NEXT:    54028:    f7 4f ff 17     b       #-180260
 // CHECK-FIX: __CortexA53843419_2A004:
-// CHECK-FIX-NEXT:    5002c:    9c 07 00 f9     str     x28, [x28, #8]
-// CHECK-FIX-NEXT:    50030:    f6 67 ff 17     b       #-155688
+// CHECK-FIX-NEXT:    5402c:    9c 07 00 f9     str     x28, [x28, #8]
+// CHECK-FIX-NEXT:    54030:    f6 57 ff 17     b       #-172072
 // CHECK-FIX: __CortexA53843419_2C004:
-// CHECK-FIX-NEXT:    50034:    84 0b 00 f9     str     x4, [x28, #16]
-// CHECK-FIX-NEXT:    50038:    f4 6f ff 17     b       #-147504
+// CHECK-FIX-NEXT:    54034:    84 0b 00 f9     str     x4, [x28, #16]
+// CHECK-FIX-NEXT:    54038:    f4 5f ff 17     b       #-163888
 // CHECK-FIX: __CortexA53843419_2E000:
-// CHECK-FIX-NEXT:    5003c:    bd 03 40 f9     ldr             x29, [x29]
-// CHECK-FIX-NEXT:    50040:    f1 77 ff 17     b       #-139324
+// CHECK-FIX-NEXT:    5403c:    bd 03 40 f9     ldr     x29, [x29]
+// CHECK-FIX-NEXT:    54040:    f1 67 ff 17     b       #-155708
 // CHECK-FIX: __CortexA53843419_30004:
-// CHECK-FIX-NEXT:    50044:    bd 07 40 f9     ldr     x29, [x29, #8]
-// CHECK-FIX-NEXT:    50048:    f0 7f ff 17     b       #-131136
+// CHECK-FIX-NEXT:    54044:    bd 07 40 f9     ldr     x29, [x29, #8]
+// CHECK-FIX-NEXT:    54048:    f0 6f ff 17     b       #-147520
 // CHECK-FIX: __CortexA53843419_32004:
-// CHECK-FIX-NEXT:    5004c:    41 0a 40 f9     ldr     x1, [x18, #16]
-// CHECK-FIX-NEXT:    50050:    ee 87 ff 17     b       #-122952
+// CHECK-FIX-NEXT:    5404c:    41 0a 40 f9     ldr     x1, [x18, #16]
+// CHECK-FIX-NEXT:    54050:    ee 77 ff 17     b       #-139336
 // CHECK-FIX: __CortexA53843419_34000:
-// CHECK-FIX-NEXT:    50054:    52 02 40 f9     ldr             x18, [x18]
-// CHECK-FIX-NEXT:    50058:    eb 8f ff 17     b       #-114772
+// CHECK-FIX-NEXT:    54054:    52 02 40 f9     ldr     x18, [x18]
+// CHECK-FIX-NEXT:    54058:    eb 7f ff 17     b       #-131156
 // CHECK-FIX: __CortexA53843419_36004:
-// CHECK-FIX-NEXT:    5005c:    ea 05 40 f9     ldr     x10, [x15, #8]
-// CHECK-FIX-NEXT:    50060:    ea 97 ff 17     b       #-106584
+// CHECK-FIX-NEXT:    5405c:    ea 05 40 f9     ldr     x10, [x15, #8]
+// CHECK-FIX-NEXT:    54060:    ea 87 ff 17     b       #-122968
 // CHECK-FIX: __CortexA53843419_38000:
-// CHECK-FIX-NEXT:    50064:    0d 0a 40 f9     ldr     x13, [x16, #16]
-// CHECK-FIX-NEXT:    50068:    e7 9f ff 17     b       #-98404
+// CHECK-FIX-NEXT:    54064:    0d 0a 40 f9     ldr     x13, [x16, #16]
+// CHECK-FIX-NEXT:    54068:    e7 8f ff 17     b       #-114788
 // CHECK-FIX: __CortexA53843419_3A004:
-// CHECK-FIX-NEXT:    5006c:    e9 00 40 f9     ldr             x9, [x7]
-// CHECK-FIX-NEXT:    50070:    e6 a7 ff 17     b       #-90216
+// CHECK-FIX-NEXT:    5406c:    e9 00 40 f9     ldr     x9, [x7]
+// CHECK-FIX-NEXT:    54070:    e6 97 ff 17     b       #-106600
 // CHECK-FIX: __CortexA53843419_3C004:
-// CHECK-FIX-NEXT:    50074:    f6 06 40 f9     ldr     x22, [x23, #8]
-// CHECK-FIX-NEXT:    50078:    e4 af ff 17     b       #-82032
+// CHECK-FIX-NEXT:    54074:    f6 06 40 f9     ldr     x22, [x23, #8]
+// CHECK-FIX-NEXT:    54078:    e4 9f ff 17     b       #-98416
 // CHECK-FIX: __CortexA53843419_3E000:
-// CHECK-FIX-NEXT:    5007c:    f8 0a 40 f9     ldr     x24, [x23, #16]
-// CHECK-FIX-NEXT:    50080:    e1 b7 ff 17     b       #-73852
+// CHECK-FIX-NEXT:    5407c:    f8 0a 40 f9     ldr     x24, [x23, #16]
+// CHECK-FIX-NEXT:    54080:    e1 a7 ff 17     b       #-90236
 // CHECK-FIX: __CortexA53843419_40004:
-// CHECK-FIX-NEXT:    50084:    02 00 40 f9     ldr             x2, [x0]
-// CHECK-FIX-NEXT:    50088:    e0 bf ff 17     b       #-65664
+// CHECK-FIX-NEXT:    54084:    02 00 40 f9     ldr     x2, [x0]
+// CHECK-FIX-NEXT:    54088:    e0 af ff 17     b       #-82048
 // CHECK-FIX: __CortexA53843419_42008:
-// CHECK-FIX-NEXT:    5008c:    9b 07 00 f9     str     x27, [x28, #8]
-// CHECK-FIX-NEXT:    50090:    df c7 ff 17     b       #-57476
+// CHECK-FIX-NEXT:    5408c:    9b 07 00 f9     str     x27, [x28, #8]
+// CHECK-FIX-NEXT:    54090:    df b7 ff 17     b       #-73860
 // CHECK-FIX: __CortexA53843419_44004:
-// CHECK-FIX-NEXT:    50094:    0e 0a 40 f9     ldr     x14, [x16, #16]
-// CHECK-FIX-NEXT:    50098:    dc cf ff 17     b       #-49296
+// CHECK-FIX-NEXT:    54094:    0e 0a 40 f9     ldr     x14, [x16, #16]
+// CHECK-FIX-NEXT:    54098:    dc bf ff 17     b       #-65680
 // CHECK-FIX: __CortexA53843419_46004:
-// CHECK-FIX-NEXT:    5009c:    0e 06 40 f9     ldr     x14, [x16, #8]
-// CHECK-FIX-NEXT:    500a0:    da d7 ff 17     b       #-41112
+// CHECK-FIX-NEXT:    5409c:    0e 06 40 f9     ldr     x14, [x16, #8]
+// CHECK-FIX-NEXT:    540a0:    da c7 ff 17     b       #-57496
 // CHECK-FIX: __CortexA53843419_48004:
-// CHECK-FIX-NEXT:    500a4:    0e 06 40 f9     ldr     x14, [x16, #8]
-// CHECK-FIX-NEXT:    500a8:    d8 df ff 17     b       #-32928
+// CHECK-FIX-NEXT:    540a4:    0e 06 40 f9     ldr     x14, [x16, #8]
+// CHECK-FIX-NEXT:    540a8:    d8 cf ff 17     b       #-49312
 // CHECK-FIX: __CortexA53843419_4A008:
-// CHECK-FIX-NEXT:    500ac:    0e 06 40 f9     ldr     x14, [x16, #8]
-// CHECK-FIX-NEXT:    500b0:    d7 e7 ff 17     b       #-24740
+// CHECK-FIX-NEXT:    540ac:    0e 06 40 f9     ldr     x14, [x16, #8]
+// CHECK-FIX-NEXT:    540b0:    d7 d7 ff 17     b       #-41124
 // CHECK-FIX: __CortexA53843419_4C008:
-// CHECK-FIX-NEXT:    500b4:    ea 00 40 f9     ldr             x10, [x7]
-// CHECK-FIX-NEXT:    500b8:    d5 ef ff 17     b       #-16556
+// CHECK-FIX-NEXT:    540b4:    ea 00 40 f9     ldr     x10, [x7]
+// CHECK-FIX-NEXT:    540b8:    d5 df ff 17     b       #-32940
 // CHECK-FIX: __CortexA53843419_4E008:
-// CHECK-FIX-NEXT:    500bc:    18 ff 3f f9     str     x24, [x24, #32760]
-// CHECK-FIX-NEXT:    500c0:    d3 f7 ff 17     b       #-8372
+// CHECK-FIX-NEXT:    540bc:    18 ff 3f f9     str     x24, [x24, #32760]
+// CHECK-FIX-NEXT:    540c0:    d3 e7 ff 17     b       #-24756
 // CHECK-FIX: __CortexA53843419_50000:
-// CHECK-FIX-NEXT:    500c4:    01 08 40 f9     ldr     x1, [x0, #16]
-// CHECK-FIX-NEXT:    500c8:    cf ff ff 17     b       #-196
-
+// CHECK-FIX-NEXT:    540c4:    01 08 40 f9     ldr     x1, [x0, #16]
+// CHECK-FIX-NEXT:    540c8:    cf ef ff 17     b       #-16580
+// CHECK-FIX: __CortexA53843419_52000:
+// CHECK-FIX-NEXT:    540cc:    01 08 40 f9     ldr     x1, [x0, #16]
+// CHECK-FIX-NEXT:    540d0:    cd f7 ff 17     b       #-8396
+// CHECK-FIX: __CortexA53843419_54000:
+// CHECK-FIX-NEXT:    540d4:    01 08 40 f9     ldr     x1, [x0, #16]
+// CHECK-FIX-NEXT:    540d8:    cb ff ff 17     b       #-212
         .data
         .globl dat
         .globl dat2
diff --git a/test/ELF/aarch64-data-relocs.s b/test/ELF/aarch64-data-relocs.s
index 19e11bc..d328715 100644
--- a/test/ELF/aarch64-data-relocs.s
+++ b/test/ELF/aarch64-data-relocs.s
@@ -1,8 +1,8 @@
+// REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %S/Inputs/abs256.s -o %t256.o
 // RUN: ld.lld %t %t256.o -o %t2
 // RUN: llvm-objdump -s %t2 | FileCheck %s
-// REQUIRES: aarch64
 
 .globl _start
 _start:
diff --git a/test/ELF/aarch64-fpic-abs16.s b/test/ELF/aarch64-fpic-abs16.s
index f4d8e28..c180939 100644
--- a/test/ELF/aarch64-fpic-abs16.s
+++ b/test/ELF/aarch64-fpic-abs16.s
@@ -1,6 +1,6 @@
 // REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
-// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
+// RUN: not ld.lld -shared %t.o -o /dev/null 2>&1 | FileCheck %s
 // CHECK:      relocation R_AARCH64_ABS16 cannot be used against symbol foo; recompile with -fPIC
 // CHECK-NEXT: >>> defined in {{.*}}.o
 // CHECK-NEXT: >>> referenced by {{.*}}.o:(.data+0x0)
diff --git a/test/ELF/aarch64-fpic-add_abs_lo12_nc.s b/test/ELF/aarch64-fpic-add_abs_lo12_nc.s
index 9f56081..fc58e06 100644
--- a/test/ELF/aarch64-fpic-add_abs_lo12_nc.s
+++ b/test/ELF/aarch64-fpic-add_abs_lo12_nc.s
@@ -1,6 +1,6 @@
 // REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
-// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
+// RUN: not ld.lld -shared %t.o -o /dev/null 2>&1 | FileCheck %s
 // CHECK: can't create dynamic relocation R_AARCH64_ADD_ABS_LO12_NC against symbol: dat in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
 // CHECK: >>> defined in {{.*}}.o
 // CHECK: >>> referenced by {{.*}}.o:(.text+0x0)
diff --git a/test/ELF/aarch64-fpic-adr_prel_lo21.s b/test/ELF/aarch64-fpic-adr_prel_lo21.s
index eebb3d2..4b6f43f 100644
--- a/test/ELF/aarch64-fpic-adr_prel_lo21.s
+++ b/test/ELF/aarch64-fpic-adr_prel_lo21.s
@@ -1,6 +1,6 @@
 // REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
-// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
+// RUN: not ld.lld -shared %t.o -o /dev/null 2>&1 | FileCheck %s
 // CHECK: relocation R_AARCH64_ADR_PREL_LO21 cannot be used against symbol dat; recompile with -fPIC
 // CHECK: >>> defined in {{.*}}.o
 // CHECK: >>> referenced by {{.*}}.o:(.text+0x0)
diff --git a/test/ELF/aarch64-fpic-adr_prel_pg_hi21.s b/test/ELF/aarch64-fpic-adr_prel_pg_hi21.s
index b8e50e8..651a32e 100644
--- a/test/ELF/aarch64-fpic-adr_prel_pg_hi21.s
+++ b/test/ELF/aarch64-fpic-adr_prel_pg_hi21.s
@@ -1,6 +1,6 @@
 // REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
-// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
+// RUN: not ld.lld -shared %t.o -o /dev/null 2>&1 | FileCheck %s
 // CHECK: relocation R_AARCH64_ADR_PREL_PG_HI21 cannot be used against symbol dat; recompile with -fPIC
 // CHECK: >>> defined in {{.*}}.o
 // CHECK: >>> referenced by {{.*}}.o:(.text+0x0)
diff --git a/test/ELF/aarch64-fpic-ldst32_abs_lo12_nc.s b/test/ELF/aarch64-fpic-ldst32_abs_lo12_nc.s
index b263fe7..b68b9f2 100644
--- a/test/ELF/aarch64-fpic-ldst32_abs_lo12_nc.s
+++ b/test/ELF/aarch64-fpic-ldst32_abs_lo12_nc.s
@@ -1,6 +1,6 @@
 // REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
-// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
+// RUN: not ld.lld -shared %t.o -o /dev/null 2>&1 | FileCheck %s
 // CHECK: can't create dynamic relocation R_AARCH64_LDST32_ABS_LO12_NC against symbol: dat in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
 // CHECK: >>> defined in {{.*}}.o
 // CHECK: >>> referenced by {{.*}}.o:(.text+0x0)
diff --git a/test/ELF/aarch64-fpic-ldst64_abs_lo12_nc.s b/test/ELF/aarch64-fpic-ldst64_abs_lo12_nc.s
index 3739ab3..1d5b943 100644
--- a/test/ELF/aarch64-fpic-ldst64_abs_lo12_nc.s
+++ b/test/ELF/aarch64-fpic-ldst64_abs_lo12_nc.s
@@ -1,6 +1,6 @@
 // REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
-// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
+// RUN: not ld.lld -shared %t.o -o /dev/null 2>&1 | FileCheck %s
 // CHECK: can't create dynamic relocation R_AARCH64_LDST64_ABS_LO12_NC against symbol: dat in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
 // CHECK: >>> defined in {{.*}}.o
 // CHECK: >>> referenced by {{.*}}.o:(.text+0x0)
diff --git a/test/ELF/aarch64-fpic-ldst8_abs_lo12_nc.s b/test/ELF/aarch64-fpic-ldst8_abs_lo12_nc.s
index 4354ffd..a3f8243 100644
--- a/test/ELF/aarch64-fpic-ldst8_abs_lo12_nc.s
+++ b/test/ELF/aarch64-fpic-ldst8_abs_lo12_nc.s
@@ -1,6 +1,6 @@
 // REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
-// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
+// RUN: not ld.lld -shared %t.o -o /dev/null 2>&1 | FileCheck %s
 // CHECK: can't create dynamic relocation R_AARCH64_LDST8_ABS_LO12_NC against symbol: dat in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
 // CHECK: >>> defined in {{.*}}.o
 // CHECK: >>> referenced by {{.*}}.o:(.text+0x0)
diff --git a/test/ELF/aarch64-fpic-prel16.s b/test/ELF/aarch64-fpic-prel16.s
index 48adb06..1de7f6f 100644
--- a/test/ELF/aarch64-fpic-prel16.s
+++ b/test/ELF/aarch64-fpic-prel16.s
@@ -1,6 +1,6 @@
 // REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
-// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
+// RUN: not ld.lld -shared %t.o -o /dev/null 2>&1 | FileCheck %s
 // CHECK: R_AARCH64_PREL16 cannot be used against symbol foo; recompile with -fPIC
 // CHECK: >>> defined in {{.*}}
 // CHECK: >>> referenced by {{.*}}:(.data+0x0)
diff --git a/test/ELF/aarch64-fpic-prel32.s b/test/ELF/aarch64-fpic-prel32.s
index de64bd5..0988b26 100644
--- a/test/ELF/aarch64-fpic-prel32.s
+++ b/test/ELF/aarch64-fpic-prel32.s
@@ -1,6 +1,6 @@
 // REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
-// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
+// RUN: not ld.lld -shared %t.o -o /dev/null 2>&1 | FileCheck %s
 // CHECK: relocation R_AARCH64_PREL32 cannot be used against symbol foo; recompile with -fPIC
 // CHECK: >>> defined in {{.*}}
 // CHECK: >>> referenced by {{.*}}:(.data+0x0)
diff --git a/test/ELF/aarch64-fpic-prel64.s b/test/ELF/aarch64-fpic-prel64.s
index ef1fe8d..653f542 100644
--- a/test/ELF/aarch64-fpic-prel64.s
+++ b/test/ELF/aarch64-fpic-prel64.s
@@ -1,6 +1,6 @@
 // REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-freebsd %s -o %t.o
-// RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
+// RUN: not ld.lld -shared %t.o -o /dev/null 2>&1 | FileCheck %s
 // CHECK: relocation R_AARCH64_PREL64 cannot be used against symbol foo; recompile with -fPIC
 // CHECK: >>> defined in {{.*}}
 // CHECK: >>> referenced by {{.*}}:(.data+0x0)
diff --git a/test/ELF/aarch64-gnu-ifunc-nosym.s b/test/ELF/aarch64-gnu-ifunc-nosym.s
index bb3a0b8..aa0124a 100644
--- a/test/ELF/aarch64-gnu-ifunc-nosym.s
+++ b/test/ELF/aarch64-gnu-ifunc-nosym.s
@@ -1,7 +1,7 @@
+// REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-linux-gnu %s -o %t.o
 // RUN: ld.lld -static %t.o -o %tout
 // RUN: llvm-readobj -symbols %tout | FileCheck %s
-// REQUIRES: aarch64
 
 // Check that no __rela_iplt_end/__rela_iplt_start
 // appear in symtab if there is no references to them.
diff --git a/test/ELF/aarch64-gnu-ifunc-plt.s b/test/ELF/aarch64-gnu-ifunc-plt.s
index 5138675..ca30316 100644
--- a/test/ELF/aarch64-gnu-ifunc-plt.s
+++ b/test/ELF/aarch64-gnu-ifunc-plt.s
@@ -1,3 +1,4 @@
+// REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-linux-gnu %S/Inputs/shared2.s -o %t1.o
 // RUN: ld.lld %t1.o --shared -o %t.so
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-linux-gnu %s -o %t.o
@@ -5,7 +6,6 @@
 // RUN: llvm-objdump -d %tout | FileCheck %s --check-prefix=DISASM
 // RUN: llvm-objdump -s %tout | FileCheck %s --check-prefix=GOTPLT
 // RUN: llvm-readobj -r -dynamic-table %tout | FileCheck %s
-// REQUIRES: aarch64
 
 // Check that the IRELATIVE relocations are after the JUMP_SLOT in the plt
 // CHECK: Relocations [
diff --git a/test/ELF/aarch64-gnu-ifunc.s b/test/ELF/aarch64-gnu-ifunc.s
index 4e0dc32..b3c1571 100644
--- a/test/ELF/aarch64-gnu-ifunc.s
+++ b/test/ELF/aarch64-gnu-ifunc.s
@@ -1,8 +1,8 @@
+// REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-linux-gnu %s -o %t.o
 // RUN: ld.lld -static %t.o -o %tout
 // RUN: llvm-objdump -d %tout | FileCheck %s --check-prefix=DISASM
 // RUN: llvm-readobj -r -symbols -sections %tout | FileCheck %s
-// REQUIRES: aarch64
 
 // CHECK:      Sections [
 // CHECK:       Section {
diff --git a/test/ELF/aarch64-hi21-error.s b/test/ELF/aarch64-hi21-error.s
index 9e2b283..07f9e04 100644
--- a/test/ELF/aarch64-hi21-error.s
+++ b/test/ELF/aarch64-hi21-error.s
@@ -1,7 +1,7 @@
+// REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-freebsd %S/Inputs/abs.s -o %tabs
 // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-freebsd %s -o %t
-// RUN: not ld.lld %tabs %t -o %t2 2>&1 | FileCheck %s
-// REQUIRES: aarch64
+// RUN: not ld.lld %tabs %t -o /dev/null 2>&1 | FileCheck %s
 
 .globl _start
 _start:
diff --git a/test/ELF/aarch64-jump26-thunk.s b/test/ELF/aarch64-jump26-thunk.s
index 088ab3a..13c084a 100644
--- a/test/ELF/aarch64-jump26-thunk.s
+++ b/test/ELF/aarch64-jump26-thunk.s
@@ -1,8 +1,8 @@
+// REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-freebsd %S/Inputs/abs.s -o %tabs
 // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-freebsd %s -o %t
 // RUN: ld.lld %t %tabs -o %t2 2>&1
 // RUN: llvm-objdump -d -triple=aarch64-pc-freebsd %t2 | FileCheck %s
-// REQUIRES: aarch64
 
 .text
 .globl _start
diff --git a/test/ELF/aarch64-ldprel-lo19-invalid.s b/test/ELF/aarch64-ldprel-lo19-invalid.s
index 04df32e..cf38b2c 100644
--- a/test/ELF/aarch64-ldprel-lo19-invalid.s
+++ b/test/ELF/aarch64-ldprel-lo19-invalid.s
@@ -1,7 +1,7 @@
 # REQUIRES: aarch64
 
 # RUN: llvm-mc -filetype=obj -triple=aarch64-linux-none %s -o %t.o
-# RUN: not ld.lld -shared %t.o -o %t 2>&1 | FileCheck %s
+# RUN: not ld.lld -shared %t.o -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: relocation R_AARCH64_LD_PREL_LO19 out of range: 2065536 is not in [-1048576, 1048575]
 
diff --git a/test/ELF/aarch64-lo12-alignment.s b/test/ELF/aarch64-lo12-alignment.s
index 2b30022..7edecd4 100644
--- a/test/ELF/aarch64-lo12-alignment.s
+++ b/test/ELF/aarch64-lo12-alignment.s
@@ -1,6 +1,6 @@
 // REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-linux %s -o %t
-// RUN: not ld.lld %t -o %t2 2>&1 | FileCheck %s
+// RUN: not ld.lld %t -o /dev/null 2>&1 | FileCheck %s
 
 // Test derived from a typical ODR violation where a global is declared
 // extern int but defined as a half or byte sized type.
diff --git a/test/ELF/aarch64-lo21-error.s b/test/ELF/aarch64-lo21-error.s
index 055f894..c1fb8a6 100644
--- a/test/ELF/aarch64-lo21-error.s
+++ b/test/ELF/aarch64-lo21-error.s
@@ -1,7 +1,7 @@
+// REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-freebsd %S/Inputs/abs.s -o %tabs
 // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-freebsd %s -o %t
-// RUN: not ld.lld %tabs %t -o %t2 2>&1 | FileCheck %s
-// REQUIRES: aarch64
+// RUN: not ld.lld %tabs %t -o /dev/null 2>&1 | FileCheck %s
 
 .globl _start
 _start:
diff --git a/test/ELF/aarch64-load-alignment.s b/test/ELF/aarch64-load-alignment.s
index 7b1129b..175f1e6 100644
--- a/test/ELF/aarch64-load-alignment.s
+++ b/test/ELF/aarch64-load-alignment.s
@@ -1,7 +1,7 @@
 # REQUIRES: aarch64
 
 # RUN: llvm-mc -filetype=obj -triple=aarch64-linux-none %s -o %t.o
-# RUN: not ld.lld -shared %t.o -o %t 2>&1 | FileCheck %s
+# RUN: not ld.lld -shared %t.o -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: improper alignment for relocation R_AARCH64_LD_PREL_LO19: 0x10005 is not aligned to 4 bytes
 
diff --git a/test/ELF/aarch64-relocs.s b/test/ELF/aarch64-relocs.s
index 9d02bd5..79caabc 100644
--- a/test/ELF/aarch64-relocs.s
+++ b/test/ELF/aarch64-relocs.s
@@ -1,8 +1,8 @@
+# REQUIRES: aarch64
 # RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-freebsd %s -o %t
 # RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-freebsd %p/Inputs/uabs_label.s -o %t2.o
 # RUN: ld.lld %t %t2.o -o %t2
 # RUN: llvm-objdump -d %t2 | FileCheck %s
-# REQUIRES: aarch64
 
 .section .R_AARCH64_ADR_PREL_LO21,"ax",@progbits
 .globl _start
diff --git a/test/ELF/aarch64-thunk-pi.s b/test/ELF/aarch64-thunk-pi.s
index ddd5897..d5d956c 100644
--- a/test/ELF/aarch64-thunk-pi.s
+++ b/test/ELF/aarch64-thunk-pi.s
@@ -1,3 +1,4 @@
+// REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-linux-gnu %s -o %t
 // RUN: echo "SECTIONS { \
 // RUN:       .text_low : { *(.text_low) } \
@@ -5,7 +6,6 @@
 // RUN:       } " > %t.script
 // RUN: ld.lld --script %t.script --shared %t -o %t2 2>&1
 // RUN: llvm-objdump -d -triple=aarch64-linux-gnu %t2 | FileCheck %s
-// REQUIRES: aarch64
 
 // Check that Position Independent thunks are generated for shared libraries.
  .section .text_low, "ax", %progbits
@@ -16,8 +16,8 @@
  bl high_target
  ret
 // CHECK: low_target:
-// CHECK-NEXT:        0:        04 00 00 94     bl      #16
-// CHECK-NEXT:        4:        c0 03 5f d6     ret
+// CHECK-NEXT:        8:        04 00 00 94     bl      #16
+// CHECK-NEXT:        c:        c0 03 5f d6     ret
 
  .hidden low_target2
  .globl low_target2
@@ -27,19 +27,19 @@
  bl high_target2
  ret
 // CHECK: low_target2:
-// CHECK-NEXT:        8:        05 00 00 94     bl      #20
-// CHECK-NEXT:        c:        c0 03 5f d6     ret
+// CHECK-NEXT:        0:        05 00 00 94     bl      #20
+// CHECK-NEXT:        4:        c0 03 5f d6     ret
 
 // Expect range extension thunks for .text_low
 // adrp calculation is (PC + signed immediate) & (!0xfff)
 // CHECK: __AArch64ADRPThunk_high_target:
-// CHECK-NEXT:       70:       10 00 08 90     adrp    x16, #268435456
-// CHECK-NEXT:       74:       10 02 03 91     add     x16, x16, #192
-// CHECK-NEXT:       78:       00 02 1f d6     br      x16
+// CHECK-NEXT:       e8:       10 00 08 90     adrp    x16, #268435456
+// CHECK-NEXT:       ec:       10 02 01 91     add     x16, x16, #64
+// CHECK-NEXT:       f0:       00 02 1f d6     br      x16
 // CHECK: __AArch64ADRPThunk_high_target2:
-// CHECK-NEXT:       7c:       10 00 08 90     adrp    x16, #268435456
-// CHECK-NEXT:       80:       10 22 00 91     add     x16, x16, #8
-// CHECK-NEXT:       84:       00 02 1f d6     br      x16
+// CHECK-NEXT:       f4:       10 00 08 90     adrp    x16, #268435456
+// CHECK-NEXT:       f8:       10 22 00 91     add     x16, x16, #8
+// CHECK-NEXT:       fc:       00 02 1f d6     br      x16
 
 
  .section .text_high, "ax", %progbits
@@ -50,7 +50,7 @@
  bl low_target
  ret
 // CHECK: high_target:
-// CHECK-NEXT: 10000000:        34 00 00 94     bl      #208
+// CHECK-NEXT: 10000000:        14 00 00 94     bl #80
 // CHECK-NEXT: 10000004:        c0 03 5f d6     ret
 
  .hidden high_target2
@@ -68,24 +68,24 @@
 
 // CHECK: __AArch64ADRPThunk_low_target2:
 // CHECK-NEXT: 10000010:	10 00 f8 90 	adrp	x16, #-268435456
-// CHECK-NEXT: 10000014:	10 a2 01 91 	add	x16, x16, #104
+// CHECK-NEXT: 10000014:	10 82 03 91 	add	x16, x16, #224
 // CHECK-NEXT: 10000018:	00 02 1f d6 	br	x16
 
 // CHECK: Disassembly of section .plt:
 // CHECK-NEXT: .plt:
-// CHECK-NEXT: 100000a0:       f0 7b bf a9     stp     x16, x30, [sp, #-16]!
-// CHECK-NEXT: 100000a4:       10 00 00 90     adrp    x16, #0
-// CHECK-NEXT: 100000a8:       11 7a 40 f9     ldr     x17, [x16, #240]
-// CHECK-NEXT: 100000ac:       10 c2 03 91     add     x16, x16, #240
-// CHECK-NEXT: 100000b0:       20 02 1f d6     br      x17
-// CHECK-NEXT: 100000b4:       1f 20 03 d5     nop
-// CHECK-NEXT: 100000b8:       1f 20 03 d5     nop
-// CHECK-NEXT: 100000bc:       1f 20 03 d5     nop
-// CHECK-NEXT: 100000c0:       10 00 00 90     adrp    x16, #0
-// CHECK-NEXT: 100000c4:       11 7e 40 f9     ldr     x17, [x16, #248]
-// CHECK-NEXT: 100000c8:       10 e2 03 91     add     x16, x16, #248
-// CHECK-NEXT: 100000cc:       20 02 1f d6     br      x17
-// CHECK-NEXT: 100000d0:       10 00 00 90     adrp    x16, #0
-// CHECK-NEXT: 100000d4:       11 82 40 f9     ldr     x17, [x16, #256]
-// CHECK-NEXT: 100000d8:       10 02 04 91     add     x16, x16, #256
-// CHECK-NEXT: 100000dc:       20 02 1f d6     br      x17
+// CHECK-NEXT: 10000020:       f0 7b bf a9     stp     x16, x30, [sp, #-16]!
+// CHECK-NEXT: 10000024:       10 00 00 90     adrp    x16, #0
+// CHECK-NEXT: 10000028:       11 3a 40 f9     ldr     x17, [x16, #112]
+// CHECK-NEXT: 1000002c:       10 c2 01 91     add     x16, x16, #112
+// CHECK-NEXT: 10000030:       20 02 1f d6     br      x17
+// CHECK-NEXT: 10000034:       1f 20 03 d5     nop
+// CHECK-NEXT: 10000038:       1f 20 03 d5     nop
+// CHECK-NEXT: 1000003c:       1f 20 03 d5     nop
+// CHECK-NEXT: 10000040:       10 00 00 90     adrp    x16, #0
+// CHECK-NEXT: 10000044:       11 3e 40 f9     ldr     x17, [x16, #120]
+// CHECK-NEXT: 10000048:       10 e2 01 91     add     x16, x16, #120
+// CHECK-NEXT: 1000004c:       20 02 1f d6     br      x17
+// CHECK-NEXT: 10000050:       10 00 00 90     adrp    x16, #0
+// CHECK-NEXT: 10000054:       11 42 40 f9     ldr     x17, [x16, #128]
+// CHECK-NEXT: 10000058:       10 02 02 91     add     x16, x16, #128
+// CHECK-NEXT: 1000005c:       20 02 1f d6     br      x17
diff --git a/test/ELF/aarch64-thunk-script.s b/test/ELF/aarch64-thunk-script.s
index ebfaf72..1dfdcbc 100644
--- a/test/ELF/aarch64-thunk-script.s
+++ b/test/ELF/aarch64-thunk-script.s
@@ -1,3 +1,4 @@
+// REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-linux-gnu %s -o %t
 // RUN: echo "SECTIONS { \
 // RUN:       .text_low 0x2000: { *(.text_low) } \
@@ -5,7 +6,6 @@
 // RUN:       } " > %t.script
 // RUN: ld.lld --script %t.script %t -o %t2 2>&1
 // RUN: llvm-objdump -d -triple=aarch64-linux-gnu %t2 | FileCheck %s
-// REQUIRES: aarch64
 
 // Check that we have the out of branch range calculation right. The immediate
 // field is signed so we have a slightly higher negative displacement.
diff --git a/test/ELF/aarch64-thunk-section-location.s b/test/ELF/aarch64-thunk-section-location.s
index bf70b7c..606c694 100644
--- a/test/ELF/aarch64-thunk-section-location.s
+++ b/test/ELF/aarch64-thunk-section-location.s
@@ -1,7 +1,7 @@
+// REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-linux-gnu %s -o %t
 // RUN: ld.lld %t -o %t2 2>&1
 // RUN: llvm-objdump -d  -start-address=134086664 -stop-address=134086676 -triple=aarch64-linux-gnu %t2 | FileCheck %s
-// REQUIRES: aarch64
 // Check that the range extension thunks are dumped close to the aarch64 branch
 // range of 128 MiB
  .section .text.1, "ax", %progbits
diff --git a/test/ELF/aarch64-tls-gdle.s b/test/ELF/aarch64-tls-gdle.s
index a111cac..6763c50 100644
--- a/test/ELF/aarch64-tls-gdle.s
+++ b/test/ELF/aarch64-tls-gdle.s
@@ -1,9 +1,9 @@
+# REQUIRES: aarch64
 # RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-linux %p/Inputs/aarch64-tls-ie.s -o %ttlsie.o
 # RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-linux %s -o %tmain.o
 # RUN: ld.lld %tmain.o %ttlsie.o -o %tout
 # RUN: llvm-objdump -d %tout | FileCheck %s
 # RUN: llvm-readobj -s -r %tout | FileCheck -check-prefix=RELOC %s
-# REQUIRES: aarch64
 
 #Local-Dynamic to Initial-Exec relax creates no
 #RELOC:      Relocations [
diff --git a/test/ELF/aarch64-tls-ie.s b/test/ELF/aarch64-tls-ie.s
index 8b74310..fba7cd8 100644
--- a/test/ELF/aarch64-tls-ie.s
+++ b/test/ELF/aarch64-tls-ie.s
@@ -1,11 +1,11 @@
 // REQUIRES: aarch64
+# REQUIRES: aarch64
 # RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-freebsd %p/Inputs/aarch64-tls-ie.s -o %tdso.o
 # RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-freebsd %s -o %tmain.o
 # RUN: ld.lld -shared %tdso.o -o %tdso.so
 # RUN: ld.lld --hash-style=sysv %tmain.o %tdso.so -o %tout
 # RUN: llvm-objdump -d %tout | FileCheck %s
 # RUN: llvm-readobj -s -r %tout | FileCheck -check-prefix=RELOC %s
-# REQUIRES: aarch64
 
 #RELOC:      Section {
 #RELOC:        Index:
diff --git a/test/ELF/aarch64-tls-iele.s b/test/ELF/aarch64-tls-iele.s
index 208b5cd..c97a578 100644
--- a/test/ELF/aarch64-tls-iele.s
+++ b/test/ELF/aarch64-tls-iele.s
@@ -1,9 +1,9 @@
+# REQUIRES: aarch64
 # RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-linux %p/Inputs/aarch64-tls-ie.s -o %ttlsie.o
 # RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-linux %s -o %tmain.o
 # RUN: ld.lld %tmain.o %ttlsie.o -o %tout
 # RUN: llvm-objdump -d %tout | FileCheck %s
 # RUN: llvm-readobj -s -r %tout | FileCheck -check-prefix=RELOC %s
-# REQUIRES: aarch64
 
 # Initial-Exec to Local-Exec relax creates no dynamic relocations.
 # RELOC:      Relocations [
diff --git a/test/ELF/aarch64-tls-le.s b/test/ELF/aarch64-tls-le.s
index df943f7..e5b1c20 100644
--- a/test/ELF/aarch64-tls-le.s
+++ b/test/ELF/aarch64-tls-le.s
@@ -1,8 +1,8 @@
+# REQUIRES: aarch64
 # RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-freebsd %s -o %tmain.o
 # RUN: ld.lld %tmain.o -o %tout
 # RUN: llvm-objdump -d %tout | FileCheck %s
 # RUN: llvm-readobj -s -r %tout | FileCheck -check-prefix=RELOC %s
-# REQUIRES: aarch64
 
 #Local-Dynamic to Initial-Exec relax creates no
 #RELOC:      Relocations [
diff --git a/test/ELF/aarch64-tlsld-ldst.s b/test/ELF/aarch64-tlsld-ldst.s
index 7b9798c..9de3a38 100644
--- a/test/ELF/aarch64-tlsld-ldst.s
+++ b/test/ELF/aarch64-tlsld-ldst.s
@@ -1,8 +1,8 @@
+// REQUIRES: aarch64
 // RUN: llvm-mc -triple=aarch64-linux-gnu -filetype=obj %s -o %t.o
 // RUN: ld.lld %t.o -o %t
 // RUN: llvm-objdump -d %t | FileCheck %s
 // RUN: llvm-readelf --symbols %t | FileCheck -check-prefix CHECK-SYMS %s
-// REQUIRES: aarch64
 
         .text
         .globl _start
diff --git a/test/ELF/aarch64-tstbr14-reloc.s b/test/ELF/aarch64-tstbr14-reloc.s
index c0a0a54..779ca6b 100644
--- a/test/ELF/aarch64-tstbr14-reloc.s
+++ b/test/ELF/aarch64-tstbr14-reloc.s
@@ -1,3 +1,4 @@
+# REQUIRES: aarch64
 # RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-freebsd %p/Inputs/aarch64-tstbr14-reloc.s -o %t1
 # RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-freebsd %s -o %t2
 # RUN: ld.lld %t1 %t2 -o %t
@@ -5,7 +6,6 @@
 # RUN: ld.lld -shared %t1 %t2 -o %t3
 # RUN: llvm-objdump -d %t3 | FileCheck -check-prefix=DSO %s
 # RUN: llvm-readobj -s -r %t3 | FileCheck -check-prefix=DSOREL %s
-# REQUIRES: aarch64
 
 # 0x1101c - 28 = 0x20000
 # 0x11020 - 16 = 0x20010
diff --git a/test/ELF/aarch64-undefined-weak.s b/test/ELF/aarch64-undefined-weak.s
index 35f5041..e2316ac 100644
--- a/test/ELF/aarch64-undefined-weak.s
+++ b/test/ELF/aarch64-undefined-weak.s
@@ -1,7 +1,7 @@
+// REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-none-linux %s -o %t
 // RUN: ld.lld %t -o %t2 2>&1
 // RUN: llvm-objdump -triple=aarch64-none-linux -d %t2 | FileCheck %s
-// REQUIRES: aarch64
 
 // Check that the ARM 64-bit ABI rules for undefined weak symbols are applied.
 // Branch instructions are resolved to the next instruction. Undefined
diff --git a/test/ELF/amdgpu-elf-flags-err.s b/test/ELF/amdgpu-elf-flags-err.s
index e30c90e..b561935 100644
--- a/test/ELF/amdgpu-elf-flags-err.s
+++ b/test/ELF/amdgpu-elf-flags-err.s
@@ -1,7 +1,6 @@
+# REQUIRES: amdgpu
 # RUN: llvm-mc -triple amdgcn-amd-amdhsa -mcpu=gfx802 -filetype=obj %S/Inputs/amdgpu-kernel-0.s -o %t-0.o
 # RUN: llvm-mc -triple amdgcn-amd-amdhsa -mcpu=gfx803 -filetype=obj %S/Inputs/amdgpu-kernel-1.s -o %t-1.o
-# RUN: not ld.lld -shared %t-0.o %t-1.o -o %t.so 2>&1 | FileCheck %s
-
-# REQUIRES: amdgpu
+# RUN: not ld.lld -shared %t-0.o %t-1.o -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: error: incompatible e_flags: {{.*}}-1.o
diff --git a/test/ELF/amdgpu-elf-flags.s b/test/ELF/amdgpu-elf-flags.s
index b97263f..d062dac 100644
--- a/test/ELF/amdgpu-elf-flags.s
+++ b/test/ELF/amdgpu-elf-flags.s
@@ -1,10 +1,9 @@
+# REQUIRES: amdgpu
 # RUN: llvm-mc -triple amdgcn-amd-amdhsa -mcpu=gfx803 -filetype=obj %S/Inputs/amdgpu-kernel-0.s -o %t-0.o
 # RUN: llvm-mc -triple amdgcn-amd-amdhsa -mcpu=gfx803 -filetype=obj %S/Inputs/amdgpu-kernel-1.s -o %t-1.o
 # RUN: ld.lld -shared %t-0.o %t-1.o -o %t.so
 # RUN: llvm-readobj -file-headers %t.so | FileCheck %s
 
-# REQUIRES: amdgpu
-
 # CHECK: Flags [
 # CHECK:   EF_AMDGPU_MACH_AMDGCN_GFX803 (0x2A)
 # CHECK: ]
diff --git a/test/ELF/amdgpu-globals.s b/test/ELF/amdgpu-globals.s
index e32159b..eadc4ef 100644
--- a/test/ELF/amdgpu-globals.s
+++ b/test/ELF/amdgpu-globals.s
@@ -1,9 +1,8 @@
+# REQUIRES: amdgpu
 # RUN: llvm-mc -filetype=obj -triple amdgcn--amdhsa -mcpu=kaveri %s -o %t.o
 # RUN: ld.lld -shared %t.o -o %t
 # RUN: llvm-readobj -sections -symbols -program-headers %t | FileCheck %s
 
-# REQUIRES: amdgpu
-
 .type glob0, @object
 .data
   .globl glob0
diff --git a/test/ELF/amdgpu-kernels.s b/test/ELF/amdgpu-kernels.s
index c76613f..01b1ef2 100644
--- a/test/ELF/amdgpu-kernels.s
+++ b/test/ELF/amdgpu-kernels.s
@@ -1,9 +1,8 @@
+# REQUIRES: amdgpu
 # RUN: llvm-mc -filetype=obj -triple amdgcn--amdhsa -mcpu=kaveri %s -o %t.o
 # RUN: ld.lld -shared %t.o -o %t
 # RUN: llvm-readobj -sections -symbols -program-headers %t | FileCheck %s
 
-# REQUIRES: amdgpu
-
 .hsa_code_object_version 1,0
 .hsa_code_object_isa 7,0,0,"AMD","AMDGPU"
 
diff --git a/test/ELF/amdgpu-relocs.s b/test/ELF/amdgpu-relocs.s
index 8b5a61e..eeb7571 100644
--- a/test/ELF/amdgpu-relocs.s
+++ b/test/ELF/amdgpu-relocs.s
@@ -1,10 +1,9 @@
+# REQUIRES: amdgpu
 # RUN: llvm-mc -filetype=obj -triple=amdgcn--amdhsa -mcpu=fiji %s -o %t.o
 # RUN: ld.lld --hash-style=sysv -shared %t.o -o %t.so
 # RUN: llvm-readobj -r %t.so | FileCheck %s
 # RUN: llvm-objdump -s %t.so | FileCheck %s --check-prefix=OBJDUMP
 
-# REQUIRES: amdgpu
-
 .text
 
 kernel0:
@@ -77,6 +76,15 @@
 ptr2:
   .quad temp2
 
+# R_AMDGPU_REL64:
+.type foo, @object
+.rodata
+  .globl foo
+  .p2align 3
+foo:
+  .quad temp2@rel64
+  .size foo, 8
+
 # The relocation for local_var{0, 1, 2} and var should be resolved by the
 # linker.
 # CHECK: Relocations [
@@ -101,6 +109,9 @@
 # CHECK-NEXT: }
 # CHECK-NEXT: ]
 
+# OBJDUMP: Contents of section .rodata:
+# OBJDUMP: d0f8ffff ffffffff
+
 # OBJDUMP: Contents of section nonalloc:
 # OBJDUMP-NEXT: 0000 00000000 04480000 00000000 08440000
 # OBJDUMP-NEXT: 00000000 0c400000
diff --git a/test/ELF/archive-fetch.s b/test/ELF/archive-fetch.s
new file mode 100644
index 0000000..201218f
--- /dev/null
+++ b/test/ELF/archive-fetch.s
@@ -0,0 +1,15 @@
+# REQUIRES: x86
+
+# We have a code in LLD that prevents fetching the same object from archive file twice.
+# This test triggers that code, without it we would fail to link output.
+
+# RUN: echo '.globl foo, bar; foo:' | llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %tfoo.o
+# RUN: echo '.globl foo, bar; bar:' | llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %tbar.o
+# RUN: rm -f %t.a
+# RUN: llvm-ar rcs %t.a %tfoo.o %tbar.o
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: ld.lld %t.a %t.o -o /dev/null
+
+_start:
+callq foo
diff --git a/test/ELF/archive.s b/test/ELF/archive.s
index 3953c28..aa74557 100644
--- a/test/ELF/archive.s
+++ b/test/ELF/archive.s
@@ -1,16 +1,21 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/archive.s -o %t2
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/archive2.s -o %t3
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/archive3.s -o %t4
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/archive4.s -o %t5
+
+# RUN: rm -f %t.a
 # RUN: llvm-ar rcs %t.a %t2 %t3 %t4
+
 # RUN: ld.lld %t %t.a %t5 -o %t.out
 # RUN: llvm-nm %t.out | FileCheck %s
+
 # RUN: rm -f %t.thin
 # RUN: llvm-ar --format=gnu rcsT %t.thin %t2 %t3 %t4
+
 # RUN: ld.lld %t %t.thin %t5 -o %t.out
 # RUN: llvm-nm %t.out | FileCheck %s
-# REQUIRES: x86
 
 # Nothing here. Just needed for the linker to create a undefined _start symbol.
 
diff --git a/test/ELF/arm-attributes.s b/test/ELF/arm-attributes.s
index 14517e8..e4411e7 100644
--- a/test/ELF/arm-attributes.s
+++ b/test/ELF/arm-attributes.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %S/Inputs/arm-attributes1.s -o %t1.o
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t2.o
 
@@ -7,7 +8,6 @@
 // RUN: llvm-readobj -arm-attributes %t2 | FileCheck %s
 // RUN: ld.lld %t1.o %t2.o -r -o %t3
 // RUN: llvm-readobj -arm-attributes %t3 | FileCheck %s
-// REQUIRES: arm
 
 // Check that we retain only 1 SHT_ARM_ATTRIBUTES section. At present we do not
 // try and merge or use the contents of SHT_ARM_ATTRIBUTES sections. We just
diff --git a/test/ELF/arm-bl-v6.s b/test/ELF/arm-bl-v6.s
index 6317aa4..c27a99e 100644
--- a/test/ELF/arm-bl-v6.s
+++ b/test/ELF/arm-bl-v6.s
@@ -1,6 +1,6 @@
-// RUN: llvm-mc -filetype=obj -triple=arm-none-linux-gnueabi %s -o %t
-// RUN: ld.lld %t -o %t2 2>&1 | FileCheck %s
 // REQUIRES: arm
+// RUN: llvm-mc -filetype=obj -triple=arm-none-linux-gnueabi %s -o %t
+// RUN: ld.lld %t -o /dev/null 2>&1 | FileCheck %s
 
 // On Arm v6 the range of a Thumb BL instruction is only 4 megabytes as the
 // extended range encoding is not supported. The following example has a Thumb
diff --git a/test/ELF/arm-blx-v4t.s b/test/ELF/arm-blx-v4t.s
index 858b93f..f526b3b 100644
--- a/test/ELF/arm-blx-v4t.s
+++ b/test/ELF/arm-blx-v4t.s
@@ -1,6 +1,6 @@
-// RUN: llvm-mc -filetype=obj -triple=arm-none-linux-gnueabi %s -o %t
-// RUN: ld.lld %t -o %t2 2>&1 | FileCheck %s
 // REQUIRES: arm
+// RUN: llvm-mc -filetype=obj -triple=arm-none-linux-gnueabi %s -o %t
+// RUN: ld.lld %t -o /dev/null 2>&1 | FileCheck %s
 
 // On Arm v4t there is no blx instruction so all interworking must go via
 // a thunk. At present we don't support v4t so we give a warning for unsupported
diff --git a/test/ELF/arm-blx.s b/test/ELF/arm-blx.s
index 159eee5..5b44c84 100644
--- a/test/ELF/arm-blx.s
+++ b/test/ELF/arm-blx.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %S/Inputs/far-arm-thumb-abs.s -o %tfar
 // RUN: echo "SECTIONS { \
@@ -10,7 +11,6 @@
 // RUN: ld.lld --script %t.script %t %tfar -o %t2 2>&1
 // RUN: llvm-objdump -d -triple=armv7a-none-linux-gnueabi %t2 | FileCheck -check-prefix=CHECK-ARM %s
 // RUN: llvm-objdump -d -triple=thumbv7a-none-linux-gnueabi %t2 | FileCheck -check-prefix=CHECK-THUMB %s
-// REQUIRES: arm
 
 // Test BLX instruction is chosen for ARM BL/BLX instruction and Thumb callee
 // Using two callees to ensure at least one has 2-byte alignment.
diff --git a/test/ELF/arm-branch-rangethunk.s b/test/ELF/arm-branch-rangethunk.s
index e541f98..739a770 100644
--- a/test/ELF/arm-branch-rangethunk.s
+++ b/test/ELF/arm-branch-rangethunk.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %S/Inputs/far-arm-abs.s -o %tfar
 // RUN: ld.lld  %t %tfar -o %t2 2>&1
@@ -5,7 +6,6 @@
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %S/Inputs/far-long-arm-abs.s -o %tfarlong
 // RUN: ld.lld  %t %tfarlong -o %t3 2>&1
 // RUN: llvm-objdump -d -triple=armv7a-none-linux-gnueabi %t3 | FileCheck --check-prefix=LONG %s
-// REQUIRES: arm
  .syntax unified
  .section .text, "ax",%progbits
  .globl _start
diff --git a/test/ELF/arm-branch-undef-weak-plt-thunk.s b/test/ELF/arm-branch-undef-weak-plt-thunk.s
index f95da0d..f47ed61 100644
--- a/test/ELF/arm-branch-undef-weak-plt-thunk.s
+++ b/test/ELF/arm-branch-undef-weak-plt-thunk.s
@@ -1,9 +1,9 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %S/Inputs/arm-shared.s -o %t
 // RUN: ld.lld %t --shared -o %t.so
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t2
 // RUN: ld.lld %t2 %t.so -o %t3
 // RUN: llvm-objdump -d -triple=armv7a-none-linux-gnueabi -start-address=69632 -stop-address=69664 %t3 | FileCheck %s
-// REQUIRES: arm
 
 // When we are dynamic linking, undefined weak references have a PLT entry so
 // we must create a thunk for the branch to the PLT entry.
diff --git a/test/ELF/arm-branch.s b/test/ELF/arm-branch.s
index 986863d..48c4977 100644
--- a/test/ELF/arm-branch.s
+++ b/test/ELF/arm-branch.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %S/Inputs/far-arm-abs.s -o %tfar
 // RUN: echo "SECTIONS { \
@@ -7,7 +8,6 @@
 // RUN:          .callee2 : { *(.callee_high) } } " > %t.script
 // RUN: ld.lld --script %t.script %t %tfar -o %t2 2>&1
 // RUN: llvm-objdump -d -triple=armv7a-none-linux-gnueabi %t2 | FileCheck  %s
-// REQUIRES: arm
  .syntax unified
  .section .callee_low, "ax",%progbits
  .align 2
diff --git a/test/ELF/arm-copy.s b/test/ELF/arm-copy.s
index 6e304b6..e42f93e 100644
--- a/test/ELF/arm-copy.s
+++ b/test/ELF/arm-copy.s
@@ -33,7 +33,7 @@
 // CHECK-NEXT:     AddressAlignment: 16
 
 // CHECK: Relocations [
-// CHECK-NEXT:  Section (5) .rel.dyn {
+// CHECK-NEXT:  Section {{.*}} .rel.dyn {
 // CHECK-NEXT:    Relocation {
 // CHECK-NEXT:      Offset: 0x13000
 // CHECK-NEXT:      Type: R_ARM_COPY
@@ -78,4 +78,4 @@
 
 // RODATA: Contents of section .rodata:
 // S(z) = 0x13004
-// RODATA-NEXT: 10160 04300100
+// RODATA-NEXT: 10190 04300100
diff --git a/test/ELF/arm-data-prel.s b/test/ELF/arm-data-prel.s
index a8c0c28..78b4281 100644
--- a/test/ELF/arm-data-prel.s
+++ b/test/ELF/arm-data-prel.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc %s -triple=armv7-unknown-linux-gnueabi -filetype=obj -o %t.o
 // RUN: echo "SECTIONS { \
 // RUN:          .text : { *(.text) } \
@@ -6,7 +7,6 @@
 // RUN:          .TEST1 : { *(.TEST1) } } " > %t.script
 // RUN: ld.lld --script %t.script %t.o -o %t
 // RUN: llvm-readobj -s -sd %t | FileCheck --check-prefix=CHECK %s
-// REQUIRES: arm
 
 // The R_ARM_PREL31 relocation is used in by the .ARM.exidx exception tables
 // bit31 of the place denotes whether the field is an inline table entry
diff --git a/test/ELF/arm-data-relocs.s b/test/ELF/arm-data-relocs.s
index ed23785..586e1a6 100644
--- a/test/ELF/arm-data-relocs.s
+++ b/test/ELF/arm-data-relocs.s
@@ -1,8 +1,8 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %S/Inputs/abs256.s -o %t256.o
 // RUN: ld.lld %t %t256.o -o %t2
 // RUN: llvm-objdump -d %t2 | FileCheck %s
-// REQUIRES: arm
  .syntax unified
  .globl _start
 _start:
diff --git a/test/ELF/arm-eabi-version.s b/test/ELF/arm-eabi-version.s
index 727b805..4b4f860 100644
--- a/test/ELF/arm-eabi-version.s
+++ b/test/ELF/arm-eabi-version.s
@@ -1,7 +1,7 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t.o
 // RUN: ld.lld -static %t.o -o %tout
 // RUN: llvm-readobj -file-headers %tout | FileCheck %s
-// REQUIRES: arm
  .syntax unified
  .text
  .globl _start
@@ -9,6 +9,7 @@
  bx lr
 
 // CHECK:  Flags [
+// CHECK-NEXT:    0x200        
 // CHECK-NEXT:    0x1000000
 // CHECK-NEXT:    0x4000000
 // CHECK-NEXT:  ]
diff --git a/test/ELF/arm-execute-only.s b/test/ELF/arm-execute-only.s
index b278e07..999d88c 100644
--- a/test/ELF/arm-execute-only.s
+++ b/test/ELF/arm-execute-only.s
@@ -14,24 +14,24 @@
 // RUN: llvm-readelf -l %t.so | FileCheck --check-prefix=DIFF %s
 
 // CHECK-NOT:  LOAD
-// CHECK:      LOAD           0x000000 0x00000000 0x00000000 0x00170 0x00170  R 0x1000
+// CHECK:      LOAD           0x000000 0x00000000 0x00000000 0x0016d 0x0016d  R 0x1000
 // CHECK:      LOAD           0x001000 0x00001000 0x00001000 0x{{.*}} 0x{{.*}} R E 0x1000
 // CHECK:      LOAD           0x002000 0x00002000 0x00002000 0x{{.*}} 0x{{.*}}   E 0x1000
 // CHECK:      LOAD           0x003000 0x00003000 0x00003000 0x00038  0x00038  RW  0x1000
 // CHECK-NOT:  LOAD
 
-// CHECK: 01     .dynsym .dynstr .gnu.hash .hash
+// CHECK: 01     .dynsym .gnu.hash .hash .dynstr
 // CHECK: 02     .text
 // CHECK: 03     .foo
 // CHECK: 04     .dynamic
 
 // DIFF-NOT:  LOAD
-// DIFF:      LOAD           0x000000 0x00000000 0x00000000 0x00150 0x00150 R   0x1000
+// DIFF:      LOAD           0x000000 0x00000000 0x00000000 0x0014d 0x0014d R   0x1000
 // DIFF:      LOAD           0x001000 0x00001000 0x00001000 0x0000c 0x0000c R E 0x1000
 // DIFF:      LOAD           0x002000 0x00002000 0x00002000 0x00038 0x00038 RW  0x1000
 // DIFF-NOT:  LOAD
 
-// DIFF: 01     .dynsym .dynstr .gnu.hash .hash
+// DIFF: 01     .dynsym .gnu.hash .hash .dynstr
 // DIFF: 02     .text .foo
 // DIFF: 03     .dynamic
 
diff --git a/test/ELF/arm-exidx-canunwind.s b/test/ELF/arm-exidx-canunwind.s
index 96a7808..df89d00 100644
--- a/test/ELF/arm-exidx-canunwind.s
+++ b/test/ELF/arm-exidx-canunwind.s
@@ -1,9 +1,9 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: ld.lld %t -o %t2 2>&1
 // RUN: llvm-objdump -d -triple=armv7a-none-linux-gnueabi %t2 | FileCheck %s
 // RUN: llvm-objdump -s -triple=armv7a-none-linux-gnueabi %t2 | FileCheck -check-prefix=CHECK-EXIDX %s
 // RUN: llvm-readobj --program-headers --sections %t2 | FileCheck -check-prefix=CHECK-PT %s
-// REQUIRES: arm
 
 // Test that inline unwinding table entries and references to .ARM.extab
 // entries survive the re-ordering of the .ARM.exidx section
diff --git a/test/ELF/arm-exidx-dedup.s b/test/ELF/arm-exidx-dedup.s
index 1648f77..49d4c2c 100644
--- a/test/ELF/arm-exidx-dedup.s
+++ b/test/ELF/arm-exidx-dedup.s
@@ -1,9 +1,9 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: ld.lld %t --no-merge-exidx-entries -o %t2
 // RUN: llvm-objdump -s %t2 | FileCheck --check-prefix CHECK-DUPS %s
 // RUN: ld.lld %t -o %t3
 // RUN: llvm-objdump -s %t3 | FileCheck %s
-// REQUIRES: arm
 // Test that lld can at least remove duplicate .ARM.exidx sections. A more
 // fine grained implementation will be able to remove duplicate entries within
 // a .ARM.exidx section.
diff --git a/test/ELF/arm-exidx-discard.s b/test/ELF/arm-exidx-discard.s
index 8c53683..2a204a0 100644
--- a/test/ELF/arm-exidx-discard.s
+++ b/test/ELF/arm-exidx-discard.s
@@ -1,8 +1,8 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple arm-gnu-linux-eabi -mcpu cortex-a7 -arm-add-build-attributes %s -o %t.o
 // RUN: echo "ENTRY(__entrypoint) SECTIONS { . = 0x10000; .text : { *(.text .text.*) } /DISCARD/ : { *(.ARM.exidx*) *(.gnu.linkonce.armexidx.*) } }" > %t.script
 // RUN: ld.lld -T %t.script %t.o -o %t.elf 2>&1
 // RUN: llvm-readobj -sections %t.elf | FileCheck %s
-// REQUIRES: arm
 
 .globl  __entrypoint
 __entrypoint:
diff --git a/test/ELF/arm-exidx-gc.s b/test/ELF/arm-exidx-gc.s
index 34bd9db..50c8616 100644
--- a/test/ELF/arm-exidx-gc.s
+++ b/test/ELF/arm-exidx-gc.s
@@ -1,8 +1,8 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: ld.lld %t --no-merge-exidx-entries -o %t2 --gc-sections 2>&1
 // RUN: llvm-objdump -d -triple=armv7a-none-linux-gnueabi %t2 | FileCheck %s
 // RUN: llvm-objdump -s -triple=armv7a-none-linux-gnueabi %t2 | FileCheck -check-prefix=CHECK-EXIDX %s
-// REQUIRES: arm
 
 // Test the behavior of .ARM.exidx sections under garbage collection
 // A .ARM.exidx section is live if it has a relocation to a live executable
diff --git a/test/ELF/arm-exidx-order.s b/test/ELF/arm-exidx-order.s
index c988ad8..7e2d4ce 100644
--- a/test/ELF/arm-exidx-order.s
+++ b/test/ELF/arm-exidx-order.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %S/Inputs/arm-exidx-cantunwind.s -o %tcantunwind
 // RUN: ld.lld --no-merge-exidx-entries %t %tcantunwind -o %t2 2>&1
@@ -11,7 +12,6 @@
 // RUN: ld.lld --no-merge-exidx-entries --script %t.script %tcantunwind %t -o %t3 2>&1
 // RUN: llvm-objdump -d -triple=armv7a-none-linux-gnueabi %t3 | FileCheck -check-prefix=CHECK-SCRIPT %s
 // RUN: llvm-objdump -s -triple=armv7a-none-linux-gnueabi %t3 | FileCheck -check-prefix=CHECK-SCRIPT-EXIDX %s
-// REQUIRES: arm
 
 // Each assembler created .ARM.exidx section has the SHF_LINK_ORDER flag set
 // with the sh_link containing the section index of the executable section
@@ -142,28 +142,28 @@
 // CHECK-SCRIPT-NEXT:    11014:       1e ff 2f e1     bx      lr
 // CHECK-SCRIPT-NEXT: Disassembly of section .func1:
 // CHECK-SCRIPT-NEXT: func1:
-// CHECK-SCRIPT-NEXT:    11068:       1e ff 2f e1     bx      lr
+// CHECK-SCRIPT-NEXT:    11018:       1e ff 2f e1     bx      lr
 // CHECK-SCRIPT-NEXT: Disassembly of section .func2:
 // CHECK-SCRIPT-NEXT: func2:
-// CHECK-SCRIPT-NEXT:    1106c:       1e ff 2f e1     bx      lr
+// CHECK-SCRIPT-NEXT:    1101c:       1e ff 2f e1     bx      lr
 // CHECK-SCRIPT-NEXT: Disassembly of section .func3:
 // CHECK-SCRIPT-NEXT: func3:
-// CHECK-SCRIPT-NEXT:    11070:       1e ff 2f e1     bx      lr
+// CHECK-SCRIPT-NEXT:    11020:       1e ff 2f e1     bx      lr
 
 // Check that the .ARM.exidx section is sorted in order as the functions
 // The offset in field 1, is 32-bit so in the binary the most significant bit
-// 11018 - 18 = 11000 func4
-// 11020 - 1c = 11004 func5
-// CHECK-SCRIPT-EXIDX:       11018 e8ffff7f 01000000 e4ffff7f 01000000
-// 11028 - 20 = 11008 _start
-// 11030 - 24 = 1100c f1
-// CHECK-SCRIPT-EXIDX-NEXT:  11028 e0ffff7f 01000000 dcffff7f 01000000
-// 11038 - 28 = 11010 f2
-// 11040 - 2c = 11014 f3
-// CHECK-SCRIPT-EXIDX-NEXT:  11038 d8ffff7f 01000000 d4ffff7f 01000000
-// 11048 + 20 = 11068 func1
-// 11050 + 1c = 1106c func2
-// CHECK-SCRIPT-EXIDX-NEXT:  11048 20000000 01000000 1c000000 01000000
-// 11058 + 18 = 11070 func3
-// 11060 + 14 = 11074 func3 + sizeof(func3)
-// CHECK-SCRIPT-EXIDX-NEXT:  11058 18000000 01000000 14000000 01000000
+// 11024 - 24 = 11000 func4
+// 1102c - 28 = 11004 func5
+// CHECK-SCRIPT-EXIDX:       11024 dcffff7f 01000000 d8ffff7f 01000000
+// 11034 - 2c = 11008 _start
+// 1103c - 30 = 1100c f1
+// CHECK-SCRIPT-EXIDX-NEXT:  11034 d4ffff7f 01000000 d0ffff7f 01000000
+// 11044 - 34 = 11010 f2
+// 1104c - 38 = 11014 f3
+// CHECK-SCRIPT-EXIDX-NEXT:  11044 ccffff7f 01000000 c8ffff7f 01000000
+// 11054 - 3c = 11018 func1
+// 1105c - 40 = 1101c func2
+// CHECK-SCRIPT-EXIDX-NEXT:  11054 c4ffff7f 01000000 c0ffff7f 01000000
+// 11064 - 44 = 11020 func3
+// 11068 - 48 = 11024 func3 + sizeof(func3)
+// CHECK-SCRIPT-EXIDX-NEXT:  11064 bcffff7f 01000000 b8ffff7f 01000000
diff --git a/test/ELF/arm-exidx-output.s b/test/ELF/arm-exidx-output.s
index dca43a3..4c65c27 100644
--- a/test/ELF/arm-exidx-output.s
+++ b/test/ELF/arm-exidx-output.s
@@ -1,7 +1,7 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: ld.lld %t -o %t2 2>&1
 // RUN: llvm-readobj -sections %t2 | FileCheck %s
-// REQUIRES: arm
 
 // Check that only a single .ARM.exidx output section is created when
 // there are input sections of the form .ARM.exidx.<section-name>. The
diff --git a/test/ELF/arm-exidx-relocatable.s b/test/ELF/arm-exidx-relocatable.s
index 1b6ee3f..422dfc2 100644
--- a/test/ELF/arm-exidx-relocatable.s
+++ b/test/ELF/arm-exidx-relocatable.s
@@ -1,9 +1,9 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %S/Inputs/arm-exidx-cantunwind.s -o %tcantunwind
 // Check that relocatable link maintains SHF_LINK_ORDER
 // RUN: ld.lld -r %t %tcantunwind -o %t4 2>&1
 // RUN: llvm-readobj -s %t4 | FileCheck %s
-// REQUIRES: arm
 
 // Each assembler created .ARM.exidx section has the SHF_LINK_ORDER flag set
 // with the sh_link containing the section index of the executable section
diff --git a/test/ELF/arm-exidx-sentinel-norelocatable.s b/test/ELF/arm-exidx-sentinel-norelocatable.s
index 4a5b64d..22e3a09 100644
--- a/test/ELF/arm-exidx-sentinel-norelocatable.s
+++ b/test/ELF/arm-exidx-sentinel-norelocatable.s
@@ -1,6 +1,6 @@
+// REQUIRES: arm
 // RUN: llvm-mc %s -triple=armv7-unknown-linux-gnueabi -filetype=obj -o %t.o
 // RUN: ld.lld -r %t.o -o %t
-// REQUIRES: arm
 // RUN: llvm-readobj -s %t | FileCheck %s
 // Check that when doing a relocatable link we don't add a terminating entry
 // to the .ARM.exidx section
diff --git a/test/ELF/arm-exidx-sentinel-orphan.s b/test/ELF/arm-exidx-sentinel-orphan.s
index 0e68c24..9aebc42 100644
--- a/test/ELF/arm-exidx-sentinel-orphan.s
+++ b/test/ELF/arm-exidx-sentinel-orphan.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // Use Linker script without .ARM.exidx Output Section so it is treated as
 // an orphan. We must still add the sentinel table entry
@@ -6,7 +7,6 @@
 // RUN:          } " > %t.script
 // RUN: ld.lld --no-merge-exidx-entries --script %t.script %t -o %t2
 // RUN: llvm-objdump -s -triple=armv7a-none-linux-gnueabi %t2 | FileCheck %s
-// REQUIRES: arm
 
  .syntax unified
  .text
@@ -20,4 +20,4 @@
 // CHECK: Contents of section .ARM.exidx:
 // 11004 - 4 = 0x11000 = _start
 // 1100c - 8 = 0x11004 = _start + sizeof(_start)
-// CHECK-NEXT: 11004 fcffff7f 01000000 f8ffff7f 01000000
+// CHECK-NEXT: 0000 00100100 01000000 fc0f0100 01000000
diff --git a/test/ELF/arm-exidx-shared.s b/test/ELF/arm-exidx-shared.s
index bcf2955..631eb07 100644
--- a/test/ELF/arm-exidx-shared.s
+++ b/test/ELF/arm-exidx-shared.s
@@ -1,8 +1,8 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: ld.lld --hash-style=sysv %t --shared -o %t2 2>&1
 // RUN: llvm-readobj --relocations %t2 | FileCheck %s
 // RUN: llvm-objdump -s -triple=armv7a-none-linux-gnueabi %t2 | FileCheck -check-prefix=CHECK-EXTAB %s
-// REQUIRES: arm
 
 // Check that the relative R_ARM_PREL31 relocation can access a PLT entry
 // for when the personality routine is referenced from a shared library.
@@ -37,9 +37,9 @@
  bx lr
 
 // CHECK: Relocations [
-// CHECK-NEXT:   Section (6) .rel.plt {
+// CHECK-NEXT:   Section {{.*}} .rel.plt {
 // CHECK-NEXT:     0x200C R_ARM_JUMP_SLOT __gxx_personality_v0
 
 // CHECK-EXTAB: Contents of section .ARM.extab:
-// 01d8 + 0e58 = 0x1030 = __gxx_personality_v0(PLT)
-// CHECK-EXTAB-NEXT:  01d8 580e0000 b0b0b000 00000000
+// 0x0210 + 0x0e20 = 0x1030 = __gxx_personality_v0(PLT)
+// CHECK-EXTAB-NEXT:  0210 200e0000 b0b0b000 00000000
diff --git a/test/ELF/arm-gnu-ifunc-nosym.s b/test/ELF/arm-gnu-ifunc-nosym.s
index fa79aef..b76ede7 100644
--- a/test/ELF/arm-gnu-ifunc-nosym.s
+++ b/test/ELF/arm-gnu-ifunc-nosym.s
@@ -1,7 +1,7 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t.o
 // RUN: ld.lld -static %t.o -o %tout
 // RUN: llvm-readobj -symbols %tout | FileCheck %s
-// REQUIRES: arm
 
 // Check that no __rel_iplt_end/__rel_iplt_start
 // appear in symtab if there are no references to them.
diff --git a/test/ELF/arm-gnu-ifunc-plt.s b/test/ELF/arm-gnu-ifunc-plt.s
index 2ff2ec0..441c31c 100644
--- a/test/ELF/arm-gnu-ifunc-plt.s
+++ b/test/ELF/arm-gnu-ifunc-plt.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-linux-gnueabihf %S/Inputs/arm-shared.s -o %t1.o
 // RUN: ld.lld %t1.o --shared -o %t.so
 // RUN: llvm-mc -filetype=obj -triple=armv7a-linux-gnueabihf %s -o %t.o
@@ -5,7 +6,6 @@
 // RUN: llvm-objdump -triple=armv7a-linux-gnueabihf -d %tout | FileCheck %s --check-prefix=DISASM
 // RUN: llvm-objdump -s %tout | FileCheck %s --check-prefix=GOTPLT
 // RUN: llvm-readobj -r -dynamic-table %tout | FileCheck %s
-// REQUIRES: arm
 
 // Check that the IRELATIVE relocations are last in the .got
 // CHECK: Relocations [
diff --git a/test/ELF/arm-gnu-ifunc.s b/test/ELF/arm-gnu-ifunc.s
index 799b8b1..8a7cb0a 100644
--- a/test/ELF/arm-gnu-ifunc.s
+++ b/test/ELF/arm-gnu-ifunc.s
@@ -1,8 +1,8 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t.o
 // RUN: ld.lld -static %t.o -o %tout
 // RUN: llvm-objdump -triple armv7a-none-linux-gnueabi -d %tout | FileCheck %s --check-prefix=DISASM
 // RUN: llvm-readobj -r -symbols -sections %tout | FileCheck %s
-// REQUIRES: arm
  .syntax unified
  .text
  .type foo STT_GNU_IFUNC
diff --git a/test/ELF/arm-gotoff.s b/test/ELF/arm-gotoff.s
index 5169f84..b9432b2 100644
--- a/test/ELF/arm-gotoff.s
+++ b/test/ELF/arm-gotoff.s
@@ -1,8 +1,8 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-linux-gnueabi %s -o %t.o
 // RUN: ld.lld %t.o -o %t
 // RUN: llvm-readobj -s -r -t %t | FileCheck %s
 // RUN: llvm-objdump -triple=armv7a-linux-gnueabi -d %t | FileCheck --check-prefix=DISASM %s
-// REQUIRES: arm
 
 // Test the R_ARM_GOTOFF32 relocation
 
diff --git a/test/ELF/arm-mov-relocs.s b/test/ELF/arm-mov-relocs.s
index 7e3ce67..f49e2c1 100644
--- a/test/ELF/arm-mov-relocs.s
+++ b/test/ELF/arm-mov-relocs.s
@@ -1,10 +1,10 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-unknown-linux-gnueabi %s -o %t
 // RUN: ld.lld %t -o %t2
 // RUN: llvm-objdump -d %t2 -triple=armv7a-unknown-linux-gnueabi | FileCheck %s
 // RUN: llvm-mc -filetype=obj -triple=thumbv7a-unknown-linux-gnueabi %s -o %t3
 // RUN: ld.lld %t3 -o %t4
 // RUN: llvm-objdump -d %t4 -triple=thumbv7a-unknown-linux-gnueabi | FileCheck %s
-// REQUIRES: arm
 
 // Test the R_ARM_MOVW_ABS_NC and R_ARM_MOVT_ABS relocations as well as
 // the R_ARM_THM_MOVW_ABS_NC and R_ARM_THM_MOVT_ABS relocations.
diff --git a/test/ELF/arm-pie-relative.s b/test/ELF/arm-pie-relative.s
index f225015..582bb8a 100644
--- a/test/ELF/arm-pie-relative.s
+++ b/test/ELF/arm-pie-relative.s
@@ -1,8 +1,8 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: ld.lld --hash-style=sysv %t --pie -o %t2
 // RUN: llvm-readobj -r %t2 | FileCheck %s
 // RUN: llvm-objdump -s %t2 | FileCheck %s --check-prefix=GOT
-// REQUIRES: arm
 
 // Test that a R_ARM_GOT_BREL relocation with PIE results in a R_ARM_RELATIVE
 // dynamic relocation
diff --git a/test/ELF/arm-plt-reloc.s b/test/ELF/arm-plt-reloc.s
index f8166d6..347bc87 100644
--- a/test/ELF/arm-plt-reloc.s
+++ b/test/ELF/arm-plt-reloc.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %p/Inputs/arm-plt-reloc.s -o %t1
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t2
 // RUN: ld.lld %t1 %t2 -o %t
@@ -5,7 +6,6 @@
 // RUN: ld.lld --hash-style=sysv -shared %t1 %t2 -o %t3
 // RUN: llvm-objdump -triple=armv7a-none-linux-gnueabi -d %t3 | FileCheck -check-prefix=DSO %s
 // RUN: llvm-readobj -s -r %t3 | FileCheck -check-prefix=DSOREL %s
-// REQUIRES: arm
 //
 // Test PLT entry generation
  .syntax unified
@@ -96,7 +96,7 @@
 // DSOREL-NEXT:    AddressAlignment: 4
 // DSOREL-NEXT:    EntrySize:
 // DSOREL:  Relocations [
-// DSOREL-NEXT:  Section (4) .rel.plt {
+// DSOREL-NEXT:  Section {{.*}} .rel.plt {
 // DSOREL-NEXT:    0x200C R_ARM_JUMP_SLOT func1 0x0
 // DSOREL-NEXT:    0x2010 R_ARM_JUMP_SLOT func2 0x0
 // DSOREL-NEXT:    0x2014 R_ARM_JUMP_SLOT func3 0x0
@@ -162,7 +162,7 @@
 // DSORELHIGH-NEXT:     ]
 // DSORELHIGH-NEXT:     Address: 0x1100000
 // DSORELHIGH: Relocations [
-// DSORELHIGH-NEXT:   Section (6) .rel.plt {
+// DSORELHIGH-NEXT:   Section {{.*}} .rel.plt {
 // DSORELHIGH-NEXT:     0x110000C R_ARM_JUMP_SLOT func1 0x0
 // DSORELHIGH-NEXT:     0x1100010 R_ARM_JUMP_SLOT func2 0x0
 // DSORELHIGH-NEXT:     0x1100014 R_ARM_JUMP_SLOT func3 0x0
@@ -227,7 +227,7 @@
 // DSORELLONG-NEXT:     ]
 // DSORELLONG-NEXT:     Address: 0x11111100
 // DSORELLONG: Relocations [
-// DSORELLONG-NEXT:   Section (6) .rel.plt {
+// DSORELLONG-NEXT:   Section {{.*}} .rel.plt {
 // DSORELLONG-NEXT:     0x1111110C R_ARM_JUMP_SLOT func1 0x0
 // DSORELLONG-NEXT:     0x11111110 R_ARM_JUMP_SLOT func2 0x0
 // DSORELLONG-NEXT:     0x11111114 R_ARM_JUMP_SLOT func3 0x0
@@ -292,7 +292,7 @@
 // DSORELMIX-NEXT:       SHF_WRITE
 // DSORELMIX-NEXT:     ]
 // DSORELMIX-NEXT:     Address: 0x8002020
-// DSORELMIX:   Section (6) .rel.plt {
+// DSORELMIX:   Section {{.*}} .rel.plt {
 // DSORELMIX-NEXT:     0x800202C R_ARM_JUMP_SLOT func1 0x0
 // DSORELMIX-NEXT:     0x8002030 R_ARM_JUMP_SLOT func2 0x0
 // DSORELMIX-NEXT:     0x8002034 R_ARM_JUMP_SLOT func3 0x0
diff --git a/test/ELF/arm-sbrel32.s b/test/ELF/arm-sbrel32.s
index 7f12717..064f59b 100644
--- a/test/ELF/arm-sbrel32.s
+++ b/test/ELF/arm-sbrel32.s
@@ -1,7 +1,7 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: ld.lld %t -o %t2 2>&1
 // RUN: llvm-objdump -d -triple=armv7a-none-linux-gnueabi %t2 | FileCheck %s
-// REQUIRES: arm
 
 // Test the R_ARM_SBREL32 relocation which calculates the offset of the Symbol
 // from the static base. We define the static base to be the address of the
diff --git a/test/ELF/arm-static-defines.s b/test/ELF/arm-static-defines.s
index 815c20c..4487ecd 100644
--- a/test/ELF/arm-static-defines.s
+++ b/test/ELF/arm-static-defines.s
@@ -1,7 +1,7 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: ld.lld --no-merge-exidx-entries %t --static -o %t2 2>&1
 // RUN: llvm-readobj --symbols %t2 | FileCheck %s
-// REQUIRES: arm
 
 // Check that on ARM we don't get a multiply defined symbol for __tls_get_addr
 // and undefined symbols for references to __exidx_start and __exidx_end
diff --git a/test/ELF/arm-tag-vfp-args-errs.s b/test/ELF/arm-tag-vfp-args-errs.s
new file mode 100644
index 0000000..622cea0
--- /dev/null
+++ b/test/ELF/arm-tag-vfp-args-errs.s
@@ -0,0 +1,29 @@
+// REQUIRES:arm
+// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %S/Inputs/arm-vfp-arg-base.s -o %tbase.o
+// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %S/Inputs/arm-vfp-arg-vfp.s -o %tvfp.o
+// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %S/Inputs/arm-vfp-arg-toolchain.s -o %ttoolchain.o
+// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t.o
+// RUN: not ld.lld %t.o %tbase.o %tvfp.o -o%t 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.o %tbase.o %ttoolchain.o -o%t 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.o %tvfp.o %tbase.o -o%t 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.o %tvfp.o %ttoolchain.o -o%t 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.o %ttoolchain.o %tbase.o -o%t 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.o %ttoolchain.o %tvfp.o -o%t 2>&1 | FileCheck %s
+
+// CHECK: incompatible Tag_ABI_VFP_args
+	.arch armv7-a
+	.eabi_attribute 20, 1
+	.eabi_attribute 21, 1
+	.eabi_attribute 23, 3
+	.eabi_attribute 24, 1
+	.eabi_attribute 25, 1
+	.eabi_attribute 26, 2
+	.eabi_attribute 30, 6
+	.eabi_attribute 34, 1
+	.eabi_attribute 18, 4
+        .eabi_attribute 28, 3 // Tag_ABI_VFP_args = 3 (Compatible with all)
+
+        .syntax unified
+        .globl _start
+        .type _start, %function
+_start: bx lr
diff --git a/test/ELF/arm-tag-vfp-args-illegal.s b/test/ELF/arm-tag-vfp-args-illegal.s
new file mode 100644
index 0000000..471a024
--- /dev/null
+++ b/test/ELF/arm-tag-vfp-args-illegal.s
@@ -0,0 +1,21 @@
+// REQUIRES:arm
+// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t.o
+// RUN: not ld.lld %t.o -o %t 2>&1 | FileCheck %s
+
+// CHECK: arm-tag-vfp-args-illegal.s.tmp.o: unknown Tag_ABI_VFP_args value: 5
+        .arch armv7-a
+        .eabi_attribute 20, 1
+        .eabi_attribute 21, 1
+        .eabi_attribute 23, 3
+        .eabi_attribute 24, 1
+        .eabi_attribute 25, 1
+        .eabi_attribute 26, 2
+        .eabi_attribute 30, 6
+        .eabi_attribute 34, 1
+        .eabi_attribute 18, 4
+        .eabi_attribute 28, 5 // Tag_ABI_VFP_args = 5 (Illegal value)
+
+        .syntax unified
+        .globl _start
+        .type _start, %function
+_start: bx lr
diff --git a/test/ELF/arm-tag-vfp-args.s b/test/ELF/arm-tag-vfp-args.s
new file mode 100644
index 0000000..b0ff095
--- /dev/null
+++ b/test/ELF/arm-tag-vfp-args.s
@@ -0,0 +1,72 @@
+// REQUIRES:arm
+// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %S/Inputs/arm-vfp-arg-base.s -o %tbase.o
+// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %S/Inputs/arm-vfp-arg-vfp.s -o %tvfp.o
+// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %S/Inputs/arm-vfp-arg-toolchain.s -o %ttoolchain.o
+// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %S/Inputs/arm-vfp-arg-compat.s -o %tcompat.o
+// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t.o
+
+// The default for this file is 0 (Base AAPCS)
+// RUN: ld.lld %t.o -o %tdefault
+// RUN: llvm-readobj -file-headers %tdefault | FileCheck -check-prefix=CHECK-BASE %s
+
+// Expect explicit Base AAPCS.
+// RUN: ld.lld %t.o %tbase.o -o %tbase
+// RUN: llvm-readobj -file-headers %tbase | FileCheck -check-prefix=CHECK-BASE %s
+
+// Expect explicit Base AAPCS when linking Base and Compatible.
+// RUN: ld.lld %t.o %tbase.o %tcompat.o -o %tbasecompat
+// RUN: llvm-readobj -file-headers %tbasecompat | FileCheck -check-prefix=CHECK-BASE %s
+
+// CHECK-BASE:   Flags [ (0x5000200)
+// CHECK-BASE-NEXT:     0x200
+// CHECK-BASE-NEXT:     0x1000000
+// CHECK-BASE-NEXT:     0x4000000
+// CHECK-BASE-NEXT:   ]
+
+// Expect Hard float VFP AAPCS
+// RUN: ld.lld %t.o %tvfp.o -o %tvfp
+// RUN: llvm-readobj -file-headers %tvfp | FileCheck -check-prefix=CHECK-VFP %s
+
+// Expect Hard float VFP AAPCS when linking VFP and Compatible
+// RUN: ld.lld %t.o %tvfp.o %tcompat.o -o %tvfpcompat
+// RUN: llvm-readobj -file-headers %tvfpcompat | FileCheck -check-prefix=CHECK-VFP %s
+
+// CHECK-VFP:   Flags [ (0x5000400)
+// CHECK-VFP-NEXT:     0x400
+// CHECK-VFP-NEXT:     0x1000000
+// CHECK-VFP-NEXT:     0x4000000
+// CHECK-VFP-NEXT:   ]
+
+// Expect Toolchain specifc to not use either Base or VFP AAPCS
+// RUN: ld.lld %t.o %ttoolchain.o -o %ttoolchain
+// RUN: llvm-readobj -file-headers %ttoolchain | FileCheck -check-prefix=CHECK-TOOLCHAIN %s
+
+// Expect Toolchain and Compatible to have same affect as Toolchain.
+// RUN: ld.lld %t.o %ttoolchain.o %tcompat.o -o %ttoolchaincompat
+// RUN: llvm-readobj -file-headers %ttoolchaincompat | FileCheck -check-prefix=CHECK-TOOLCHAIN %s
+
+// CHECK-TOOLCHAIN:   Flags [ (0x5000000)
+// CHECK-TOOLCHAIN-NEXT:     0x1000000
+// CHECK-TOOLCHAIN-NEXT:     0x4000000
+// CHECK-TOOLCHAIN-NEXT:   ]
+
+        .arch armv7-a
+        .eabi_attribute 20, 1
+        .eabi_attribute 21, 1
+        .eabi_attribute 23, 3
+        .eabi_attribute 24, 1
+        .eabi_attribute 25, 1
+        .eabi_attribute 26, 2
+        .eabi_attribute 30, 6
+        .eabi_attribute 34, 1
+        .eabi_attribute 18, 4
+        // We do not specify Tag_ABI_VFP_args (.eabi_attribute 28) in this file.
+        // When omitted the value of the tag defaults to 0, however if there
+        // are other files with explicit Tag_ABI_VFP_args we use that in
+        // preference.
+
+
+        .syntax unified
+        .globl _start
+        .type _start, %function
+_start:  bx lr
diff --git a/test/ELF/arm-target2.s b/test/ELF/arm-target2.s
index a678f7e..4dbcde8 100644
--- a/test/ELF/arm-target2.s
+++ b/test/ELF/arm-target2.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t.o
 // RUN: ld.lld %t.o -o %t 2>&1
 // RUN: llvm-objdump -s -triple=armv7a-none-linux-gnueabi %t | FileCheck %s
@@ -7,7 +8,6 @@
 // RUN: llvm-objdump -s -triple=armv7a-none-linux-gnueabi %t3 | FileCheck -check-prefix=CHECK-ABS %s
 // RUN: ld.lld %t.o --target2=rel -o %t4 2>&1
 // RUN: llvm-objdump -s -triple=armv7a-none-linux-gnueabi %t4 | FileCheck -check-prefix=CHECK-REL %s
-// REQUIRES: arm
 
 // The R_ARM_TARGET2 is present in .ARM.extab sections. It can be handled as
 // either R_ARM_ABS32, R_ARM_REL32 or R_ARM_GOT_PREL. For ARM linux the default
@@ -35,16 +35,16 @@
 _ZTIi:  .word 0
 
 // CHECK: Contents of section .ARM.extab:
-// 1011c + 1ee4 = 12000 = .got
-// CHECK-NEXT:  10114 f00e0000 b0b0b000 e41e0000
+// 0x1012c + 0x1ed4 = 0x12000 = .got
+// CHECK-NEXT:  10124 e00e0000 b0b0b000 d41e0000
 
 // CHECK-ABS: Contents of section .ARM.extab:
-// 100f0 = .rodata
-// CHECK-ABS-NEXT: 100d4 300f0000 b0b0b000 f0000100
+// 0x100f0 = .rodata
+// CHECK-ABS-NEXT: 100e4 200f0000 b0b0b000 f0000100
 
 // CHECK-REL: Contents of section .ARM.extab:
-// 100dc + c = 100e8 = .rodata
-// CHECK-REL-NEXT: 100d4 300f0000 b0b0b000 14000000
+// 0x100ec + 4 = 0x100f0 = .rodata
+// CHECK-REL-NEXT: 100e4 200f0000 b0b0b000 04000000
 
 // CHECK: Contents of section .rodata:
 // CHECK-NEXT: 10130 00000000
diff --git a/test/ELF/arm-thumb-blx.s b/test/ELF/arm-thumb-blx.s
index d79bef1..5316d13 100644
--- a/test/ELF/arm-thumb-blx.s
+++ b/test/ELF/arm-thumb-blx.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=thumbv7a-none-linux-gnueabi %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=thumbv7a-none-linux-gnueabi %S/Inputs/arm-thumb-blx-targets.s -o %ttarget
 // RUN: echo "SECTIONS { \
@@ -9,7 +10,6 @@
 // RUN: ld.lld --script %t.script %t %ttarget -o %t2 2>&1
 // RUN: llvm-objdump -d -triple=thumbv7a-none-linux-gnueabi %t2 | FileCheck -check-prefix=CHECK-THUMB %s
 // RUN: llvm-objdump -d -triple=armv7a-none-linux-gnueabi %t2 | FileCheck -check-prefix=CHECK-ARM %s
-// REQUIRES: arm
 // Test BLX instruction is chosen for Thumb BL/BLX instruction and ARM callee
 // 2 byte nops are used to test the pc-rounding behaviour. As a BLX from a
 // 2 byte aligned destination is defined as Align(PC,4) + immediate:00
diff --git a/test/ELF/arm-thumb-branch-rangethunk.s b/test/ELF/arm-thumb-branch-rangethunk.s
index f83e641..4bbd692 100644
--- a/test/ELF/arm-thumb-branch-rangethunk.s
+++ b/test/ELF/arm-thumb-branch-rangethunk.s
@@ -1,8 +1,8 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=thumbv7a-none-linux-gnueabi %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=thumbv7a-none-linux-gnueabi %S/Inputs/far-arm-thumb-abs.s -o %tfar
 // RUN: ld.lld  %t %tfar -o %t2 2>&1
 // RUN: llvm-objdump -d -triple=thumbv7a-none-linux-gnueabi %t2
-// REQUIRES: arm
  .syntax unified
  .thumb
  .section .text, "ax",%progbits
diff --git a/test/ELF/arm-thumb-branch.s b/test/ELF/arm-thumb-branch.s
index 81bf7a3..89c081a 100644
--- a/test/ELF/arm-thumb-branch.s
+++ b/test/ELF/arm-thumb-branch.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=thumbv7a-none-linux-gnueabi %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=thumbv7a-none-linux-gnueabi %S/Inputs/far-arm-thumb-abs.s -o %tfar
 // RUN: echo "SECTIONS { \
@@ -7,7 +8,6 @@
 // RUN:          .callee2 : { *(.callee_high) } } " > %t.script
 // RUN: ld.lld --script %t.script %t %tfar -o %t2 2>&1
 // RUN: llvm-objdump -d -triple=thumbv7a-none-linux-gnueabi %t2 | FileCheck  %s
-// REQUIRES: arm
 
  .syntax unified
  .thumb
diff --git a/test/ELF/arm-thumb-interwork-shared.s b/test/ELF/arm-thumb-interwork-shared.s
index cadcd45..030ac29 100644
--- a/test/ELF/arm-thumb-interwork-shared.s
+++ b/test/ELF/arm-thumb-interwork-shared.s
@@ -1,8 +1,8 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=thumbv7a-none-linux-gnueabi %s -o %t
 // RUN: ld.lld %t --shared -o %t.so
 // RUN: llvm-objdump -d -triple=thumbv7a-none-linux-gnueabi %t.so | FileCheck %s
 // RUN: llvm-objdump -d -triple=armv7a-none-linux-gnueabi %t.so | FileCheck %s -check-prefix=PLT
-// REQUIRES: arm
  .syntax unified
  .global sym1
  .global elsewhere
diff --git a/test/ELF/arm-thumb-interwork-thunk-range.s b/test/ELF/arm-thumb-interwork-thunk-range.s
index db674f4..d59ee11 100644
--- a/test/ELF/arm-thumb-interwork-thunk-range.s
+++ b/test/ELF/arm-thumb-interwork-thunk-range.s
@@ -1,6 +1,6 @@
 // REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t.o
-// RUN: ld.lld %t.o -o %t -image-base=0x80000000
+// RUN: ld.lld %t.o -o /dev/null -image-base=0x80000000
 
 // Test that when the thunk is at a high address we don't get confused with it
 // being out of range.
diff --git a/test/ELF/arm-thumb-interwork-thunk.s b/test/ELF/arm-thumb-interwork-thunk.s
index 8b37bc1..df5d6c6 100644
--- a/test/ELF/arm-thumb-interwork-thunk.s
+++ b/test/ELF/arm-thumb-interwork-thunk.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: echo "SECTIONS { \
 // RUN:       . = SIZEOF_HEADERS; \
@@ -19,7 +20,6 @@
 // RUN: llvm-objdump -d -triple=thumbv7a-none-linux-gnueabi %t4 | FileCheck -check-prefix=CHECK-THUMB -check-prefix=CHECK-PI-PLT-THUMB %s
 // RUN: llvm-objdump -d -triple=armv7a-none-linux-gnueabi %t4 | FileCheck -check-prefix=CHECK-ARM -check-prefix=CHECK-PI-PLT-ARM %s
 // RUN: llvm-readobj -s -r %t4 | FileCheck -check-prefix=CHECK-DSO-REL %s
-// REQUIRES: arm
 
 // Test ARM Thumb Interworking
 // The file is linked and checked 3 times to check the following contexts
diff --git a/test/ELF/arm-thumb-narrow-branch-check.s b/test/ELF/arm-thumb-narrow-branch-check.s
index 82a7164..27bac59 100644
--- a/test/ELF/arm-thumb-narrow-branch-check.s
+++ b/test/ELF/arm-thumb-narrow-branch-check.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=thumbv7a-none-linux-gnueabi %s -o %t
 // RUN: echo "SECTIONS { \
 // RUN:          . = SIZEOF_HEADERS; \
@@ -7,7 +8,6 @@
 // RUN:          .text : { *(.text) } } " > %t.script
 // RUN: ld.lld --script %t.script %t %S/Inputs/arm-thumb-narrow-branch.o -o %t2 2>&1
 // RUN: llvm-objdump -d -triple=thumbv7a-none-linux-gnueabi %t2 | FileCheck %s
-// REQUIRES: arm
 
 // Test the R_ARM_PC11 relocation which is used with the narrow encoding of B.N
 // the source of these relocations is a binary file arm-thumb-narrow-branch.o
diff --git a/test/ELF/arm-thumb-no-undefined-thunk.s b/test/ELF/arm-thumb-no-undefined-thunk.s
index e8d8d8d..5456e76 100644
--- a/test/ELF/arm-thumb-no-undefined-thunk.s
+++ b/test/ELF/arm-thumb-no-undefined-thunk.s
@@ -1,7 +1,7 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=thumbv7a-none-linux-gnueabi %s -o %t
 // RUN: ld.lld %t -o %t2 2>&1
 // RUN: llvm-objdump -triple=thumbv7a-none-linux-gnueabi -d %t2 | FileCheck %s
-// REQUIRES: arm
 
 // Check that no thunks are created for an undefined weak symbol
  .syntax unified
diff --git a/test/ELF/arm-thumb-plt-reloc.s b/test/ELF/arm-thumb-plt-reloc.s
index dd8770e..742e959 100644
--- a/test/ELF/arm-thumb-plt-reloc.s
+++ b/test/ELF/arm-thumb-plt-reloc.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=thumbv7a-none-linux-gnueabi %p/Inputs/arm-plt-reloc.s -o %t1
 // RUN: llvm-mc -filetype=obj -triple=thumbv7a-none-linux-gnueabi %s -o %t2
 // RUN: ld.lld %t1 %t2 -o %t
@@ -6,7 +7,6 @@
 // RUN: llvm-objdump -triple=thumbv7a-none-linux-gnueabi -d %t3 | FileCheck -check-prefix=DSOTHUMB %s
 // RUN: llvm-objdump -triple=armv7a-none-linux-gnueabi -d %t3 | FileCheck -check-prefix=DSOARM %s
 // RUN: llvm-readobj -s -r %t3 | FileCheck -check-prefix=DSOREL %s
-// REQUIRES: arm
 //
 // Test PLT entry generation
  .syntax unified
diff --git a/test/ELF/arm-thumb-thunk-symbols.s b/test/ELF/arm-thumb-thunk-symbols.s
index faa39fe..457d460 100644
--- a/test/ELF/arm-thumb-thunk-symbols.s
+++ b/test/ELF/arm-thumb-thunk-symbols.s
@@ -1,9 +1,9 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: ld.lld %t -o %t2 2>&1
 // RUN: llvm-readobj --symbols %t2 | FileCheck %s
 // RUN: ld.lld --shared %t -o %t3 2>&1
 // RUN: llvm-readobj --symbols %t3 | FileCheck -check-prefix=CHECK-PI %s
-// REQUIRES: arm
 
 // Check that the symbols generated for Thunks have the correct symbol type
 // of STT_FUNC and the correct value of bit 0 (0 for ARM 1 for Thumb)
diff --git a/test/ELF/arm-thumb-undefined-weak-narrow.test b/test/ELF/arm-thumb-undefined-weak-narrow.test
new file mode 100644
index 0000000..15f428e
--- /dev/null
+++ b/test/ELF/arm-thumb-undefined-weak-narrow.test
@@ -0,0 +1,50 @@
+# REQUIRES: arm
+# RUN: yaml2obj %s -o %t.o
+# RUN: ld.lld %t.o -o %t
+# RUN: llvm-objdump -triple=thumbv7a-linux-gnueabi -d %t | FileCheck %s
+
+# CHECK: Disassembly of section .text:
+# CHECK-NEXT:_start:
+# CHECK-NEXT:    11000:       ff e7   b       #-2
+
+# Test the R_ARM_THM_JUMP11 relocation (102) to an undefined weak reference
+# It should resolve to the next instruction, which is an offset of -2 which
+# when added to the Thumb PC-bias of 4 is +2. We can't use llvm-mc to construct
+# the object as it relaxes b.n to b.w (R_ARM_JUMP24).
+
+!ELF
+FileHeader:
+  Class:           ELFCLASS32
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_ARM
+Sections:
+  - Type:            SHT_PROGBITS
+    Name:            .text
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    Content:         "fee7"
+  - Type:            SHT_REL
+    Name:            .rel.text
+    Link:            .symtab
+    Info:            .text
+    Relocations:
+      - Offset:          0
+        Symbol:          undefined_weak
+        Type:            R_ARM_THM_JUMP11
+
+Symbols:
+ Global:
+   - Type:             STT_FUNC
+     Name:             _start
+     Value:            1
+     Section:          .text
+ Local:
+   - Type:             STT_NOTYPE
+     Name:             "$t"
+     Section:          .text
+     Value:            0
+ Weak:
+   - Type:             STT_NOTYPE
+     Name:             undefined_weak
+     Value:            0
+
diff --git a/test/ELF/arm-thumb-undefined-weak.s b/test/ELF/arm-thumb-undefined-weak.s
index 7f481b0..d973c58 100644
--- a/test/ELF/arm-thumb-undefined-weak.s
+++ b/test/ELF/arm-thumb-undefined-weak.s
@@ -1,7 +1,7 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=thumbv7a-none-linux-gnueabi %s -o %t
 // RUN: ld.lld %t -o %t2 2>&1
 // RUN: llvm-objdump -triple=thumbv7a-none-linux-gnueabi -d %t2 | FileCheck %s
-// REQUIRES: arm
 
 // Check that the ARM ABI rules for undefined weak symbols are applied.
 // Branch instructions are resolved to the next instruction. Relative
diff --git a/test/ELF/arm-thunk-largesection.s b/test/ELF/arm-thunk-largesection.s
index 5e882a9..d68cd0c 100644
--- a/test/ELF/arm-thunk-largesection.s
+++ b/test/ELF/arm-thunk-largesection.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=thumbv7a-none-linux-gnueabi %s -o %t
 // RUN: ld.lld %t -o %t2 2>&1
 // RUN: llvm-objdump -d -triple=thumbv7a-none-linux-gnueabi -start-address=69632 -stop-address=69636 %t2 | FileCheck -check-prefix=CHECK1 %s
@@ -5,7 +6,6 @@
 // RUN: llvm-objdump -d -triple=thumbv7a-none-linux-gnueabi -start-address=16850936 -stop-address=16850940 %t2 | FileCheck -check-prefix=CHECK3 %s
 // RUN: llvm-objdump -d -triple=thumbv7a-none-linux-gnueabi -start-address=33628152 -stop-address=33628156 %t2 | FileCheck -check-prefix=CHECK4 %s
 // RUN: llvm-objdump -d -triple=thumbv7a-none-linux-gnueabi -start-address=50405356 -stop-address=50405376 %t2 | FileCheck -check-prefix=CHECK5 %s
-// REQUIRES: arm
  .syntax unified
  .balign 0x1000
  .thumb
diff --git a/test/ELF/arm-thunk-linkerscript-dotexpr.s b/test/ELF/arm-thunk-linkerscript-dotexpr.s
index bd0e9a2..ea74170 100644
--- a/test/ELF/arm-thunk-linkerscript-dotexpr.s
+++ b/test/ELF/arm-thunk-linkerscript-dotexpr.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: echo "SECTIONS { \
 // RUN:       . = SIZEOF_HEADERS; \
@@ -6,7 +7,6 @@
 // RUN: ld.lld --script %t.script %t -o %t2 2>&1
 // RUN: llvm-objdump -d %t2 -start-address=148 -stop-address=188 -triple=thumbv7a-linux-gnueabihf | FileCheck -check-prefix=CHECK1 %s
 // RUN: llvm-objdump -d %t2 -start-address=33554620 -stop-address=33554654 -triple=thumbv7a-linux-gnueabihf | FileCheck -check-prefix=CHECK2 %s
-// REQUIRES: arm
 // Test that range extension thunks can handle location expressions within
 // a Section Description
  .syntax unified
diff --git a/test/ELF/arm-thunk-linkerscript.s b/test/ELF/arm-thunk-linkerscript.s
index 9aaa292..7728ddf 100644
--- a/test/ELF/arm-thunk-linkerscript.s
+++ b/test/ELF/arm-thunk-linkerscript.s
@@ -1,3 +1,4 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: echo "SECTIONS { \
 // RUN:       . = SIZEOF_HEADERS; \
@@ -6,7 +7,6 @@
 // RUN:       } " > %t.script
 // RUN: ld.lld --script %t.script %t -o %t2 2>&1
 // RUN: llvm-objdump -d -triple=thumbv7a-none-linux-gnueabi %t2 | FileCheck %s
-// REQUIRES: arm
 // Simple test that we can support range extension thunks with linker scripts
  .syntax unified
  .section .text_low, "ax", %progbits
diff --git a/test/ELF/arm-thunk-nosuitable.s b/test/ELF/arm-thunk-nosuitable.s
index 1fac4a5..cde7906 100644
--- a/test/ELF/arm-thunk-nosuitable.s
+++ b/test/ELF/arm-thunk-nosuitable.s
@@ -1,7 +1,7 @@
+// REQUIRES: ARM
 // RUN: llvm-mc %s --arm-add-build-attributes --triple=armv7a-linux-gnueabihf --filetype=obj -o %t.o
 // RUN: ld.lld %t.o -o %t
 // RUN: llvm-objdump -triple=thumbv7a-linux-gnueabihf -d -start-address=2166784 -stop-address=2166794 %t | FileCheck %s
-// REQUIRES: ARM
 
         // Create a conditional branch too far away from a precreated thunk
         // section. This will need a thunk section created within range.
diff --git a/test/ELF/arm-thunk-section-too-large.s b/test/ELF/arm-thunk-section-too-large.s
index 2b2d0d3..9174093 100644
--- a/test/ELF/arm-thunk-section-too-large.s
+++ b/test/ELF/arm-thunk-section-too-large.s
@@ -1,6 +1,6 @@
-// RUN: llvm-mc %s -triple=armv7a-linux-gnueabihf -arm-add-build-attributes -filetype=obj -o %t.o
-// RUN: not ld.lld %t.o -o %t 2>&1 | FileCheck %s
 // REQUIRES: ARM
+// RUN: llvm-mc %s -triple=armv7a-linux-gnueabihf -arm-add-build-attributes -filetype=obj -o %t.o
+// RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
 
 // CHECK: InputSection too large for range extension thunk
         .syntax unified
diff --git a/test/ELF/arm-thunk-toolargesection.s b/test/ELF/arm-thunk-toolargesection.s
index 28fb94a..13d0ea7 100644
--- a/test/ELF/arm-thunk-toolargesection.s
+++ b/test/ELF/arm-thunk-toolargesection.s
@@ -1,6 +1,6 @@
-// RUN: llvm-mc -filetype=obj -triple=thumbv7a-none-linux-gnueabi %s -o %t
-// RUN: not ld.lld %t -o %t2 2>&1 | FileCheck %s
 // REQUIRES: arm
+// RUN: llvm-mc -filetype=obj -triple=thumbv7a-none-linux-gnueabi %s -o %t
+// RUN: not ld.lld %t -o /dev/null 2>&1 | FileCheck %s
  .syntax unified
  .balign 0x1000
  .thumb
diff --git a/test/ELF/arm-tls-gd-nonpreemptible.s b/test/ELF/arm-tls-gd-nonpreemptible.s
index ebaad47..e72d422 100644
--- a/test/ELF/arm-tls-gd-nonpreemptible.s
+++ b/test/ELF/arm-tls-gd-nonpreemptible.s
@@ -1,10 +1,10 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: ld.lld %t -o %t2
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=armv7a-linux-gnueabi
 // RUN: llvm-objdump -s %t2 | FileCheck %s
 // RUN: ld.lld --hash-style=sysv %t --shared -o %t3.so
 // RUN: llvm-objdump -s %t3.so | FileCheck -check-prefix=CHECK-SHARED %s
-// REQUIRES: arm
 
 // For an executable, we write the module index 1 and the offset into the TLS
 // directly into the GOT. For a shared library we can only write the offset
diff --git a/test/ELF/arm-tls-gd32.s b/test/ELF/arm-tls-gd32.s
index a32e26f..28ef496 100644
--- a/test/ELF/arm-tls-gd32.s
+++ b/test/ELF/arm-tls-gd32.s
@@ -1,8 +1,8 @@
+// REQUIRES: arm
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=armv7a-linux-gnueabi
 // RUN: ld.lld --hash-style=sysv %t.o -o %t.so -shared
 // RUN: llvm-readobj -s -dyn-relocations %t.so | FileCheck --check-prefix=SEC %s
 // RUN: llvm-objdump -d -triple=armv7a-linux-gnueabi %t.so | FileCheck %s
-// REQUIRES: arm
 
 // Test the handling of the global-dynamic TLS model. Dynamic Loader finds
 // module index R_ARM_TLS_DTPMOD32 and the offset within the module
diff --git a/test/ELF/arm-tls-ie32.s b/test/ELF/arm-tls-ie32.s
index 26e1265..b12bc1b 100644
--- a/test/ELF/arm-tls-ie32.s
+++ b/test/ELF/arm-tls-ie32.s
@@ -1,8 +1,8 @@
+// REQUIRES: arm
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=armv7a-linux-gnueabi
 // RUN: ld.lld --hash-style=sysv %t.o -o %t.so -shared
 // RUN: llvm-readobj -s -dyn-relocations %t.so | FileCheck --check-prefix=SEC %s
 // RUN: llvm-objdump -d -triple=armv7a-linux-gnueabi %t.so | FileCheck %s
-// REQUIRES: arm
 
 // Test the handling of the initial-exec TLS model. Relative location within
 // static TLS is a run-time constant computed by dynamic loader as a result
diff --git a/test/ELF/arm-tls-ldm32.s b/test/ELF/arm-tls-ldm32.s
index 629dcd0..94931d9 100644
--- a/test/ELF/arm-tls-ldm32.s
+++ b/test/ELF/arm-tls-ldm32.s
@@ -1,8 +1,8 @@
+// REQUIRES: arm
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=armv7a-linux-gnueabi
 // RUN: ld.lld --hash-style=sysv %t.o -o %t.so -shared
 // RUN: llvm-readobj -s -dyn-relocations %t.so | FileCheck --check-prefix=SEC %s
 // RUN: llvm-objdump -d -triple=armv7a-linux-gnueabi %t.so | FileCheck %s
-// REQUIRES: arm
 
 // Test the handling of the local-dynamic TLS model. Dynamic loader finds
 // module index R_ARM_TLS_DTPMOD32. The offset in the next GOT slot is 0
diff --git a/test/ELF/arm-tls-le32.s b/test/ELF/arm-tls-le32.s
index 4d42a06..7834ded 100644
--- a/test/ELF/arm-tls-le32.s
+++ b/test/ELF/arm-tls-le32.s
@@ -1,8 +1,8 @@
+// REQUIRES: arm
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=armv7a-linux-gnueabi
 // RUN: ld.lld %t.o -o %t
 // RUN: llvm-readobj -s -dyn-relocations %t | FileCheck --check-prefix=SEC %s
 // RUN: llvm-objdump -d -triple=armv7a-linux-gnueabi %t | FileCheck %s
-// REQUIRES: arm
 
 // Test the handling of the local exec TLS model. TLS can be resolved
 // statically for an application. The code sequences assume a thread pointer
diff --git a/test/ELF/arm-tls-norelax-gd-ie.s b/test/ELF/arm-tls-norelax-gd-ie.s
index bcee561..ba60521 100644
--- a/test/ELF/arm-tls-norelax-gd-ie.s
+++ b/test/ELF/arm-tls-norelax-gd-ie.s
@@ -1,9 +1,9 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %p/Inputs/arm-tls-get-addr.s -o %t1
 // RUN: ld.lld %t1 --shared -o %t1.so
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=armv7a-linux-gnueabi
 // RUN: ld.lld --hash-style=sysv %t1.so %t.o -o %t
 // RUN: llvm-readobj -s -dyn-relocations %t | FileCheck %s
-// REQUIRES: arm
 
 // This tls global-dynamic sequence is with respect to a preemptible symbol but
 // is in an application so a relaxation to Initial Exec would normally be
diff --git a/test/ELF/arm-tls-norelax-gd-le.s b/test/ELF/arm-tls-norelax-gd-le.s
index 788c845..f1e78c4 100644
--- a/test/ELF/arm-tls-norelax-gd-le.s
+++ b/test/ELF/arm-tls-norelax-gd-le.s
@@ -1,9 +1,9 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %p/Inputs/arm-tls-get-addr.s -o %t1
 // RUN: ld.lld %t1 --shared -o %t1.so
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=armv7a-linux-gnueabi
 // RUN: ld.lld --hash-style=sysv %t1.so %t.o -o %t
 // RUN: llvm-objdump -s %t | FileCheck %s
-// REQUIRES: arm
 
 // This tls global-dynamic sequence is with respect to a non-preemptible
 // symbol in an application so a relaxation to Local Exec would normally be
diff --git a/test/ELF/arm-tls-norelax-ie-le.s b/test/ELF/arm-tls-norelax-ie-le.s
index eb96aa0..be8af97 100644
--- a/test/ELF/arm-tls-norelax-ie-le.s
+++ b/test/ELF/arm-tls-norelax-ie-le.s
@@ -1,9 +1,9 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %p/Inputs/arm-tls-get-addr.s -o %t1
 // RUN: ld.lld %t1 --shared -o %t1.so
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=armv7a-linux-gnueabi
 // RUN: ld.lld --hash-style=sysv %t1.so %t.o -o %t
 // RUN: llvm-objdump -s -triple=armv7a-linux-gnueabi %t | FileCheck %s
-// REQUIRES: arm
 
 // This tls Initial Exec sequence is with respect to a non-preemptible symbol
 // so a relaxation would normally be possible. This would result in an assertion
diff --git a/test/ELF/arm-tls-norelax-ld-le.s b/test/ELF/arm-tls-norelax-ld-le.s
index fc5b72b..ce0697a 100644
--- a/test/ELF/arm-tls-norelax-ld-le.s
+++ b/test/ELF/arm-tls-norelax-ld-le.s
@@ -1,9 +1,9 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %p/Inputs/arm-tls-get-addr.s -o %t1
 // RUN: ld.lld %t1 --shared -o %t1.so
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=armv7a-linux-gnueabi
 // RUN: ld.lld --hash-style=sysv %t1.so %t.o -o %t
 // RUN: llvm-objdump -s %t | FileCheck %s
-// REQUIRES: arm
 
  .global __tls_get_addr
  .text
diff --git a/test/ELF/arm-undefined-weak.s b/test/ELF/arm-undefined-weak.s
index 8af9f49..0d4714a 100644
--- a/test/ELF/arm-undefined-weak.s
+++ b/test/ELF/arm-undefined-weak.s
@@ -1,7 +1,7 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t
 // RUN: ld.lld %t -o %t2 2>&1
 // RUN: llvm-objdump -triple=armv7a-none-linux-gnueabi -d %t2 | FileCheck %s
-// REQUIRES: arm
 
 // Check that the ARM ABI rules for undefined weak symbols are applied.
 // Branch instructions are resolved to the next instruction. Undefined
diff --git a/test/ELF/arm-use-r-output.s b/test/ELF/arm-use-r-output.s
index 9183624..2d23508 100644
--- a/test/ELF/arm-use-r-output.s
+++ b/test/ELF/arm-use-r-output.s
@@ -1,7 +1,7 @@
 // REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t.o
 // RUN: ld.lld -r %t.o -o %t2.o
-// RUN: ld.lld -shared %t2.o -o %t.so
+// RUN: ld.lld -shared %t2.o -o /dev/null
 
 // We used to crash using the output of -r because of the relative order of
 // SHF_LINK_ORDER sections.
diff --git a/test/ELF/basic-aarch64.s b/test/ELF/basic-aarch64.s
index 6527d3d..efbe008 100644
--- a/test/ELF/basic-aarch64.s
+++ b/test/ELF/basic-aarch64.s
@@ -1,8 +1,8 @@
+# REQUIRES: aarch64
 # RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-freebsd %s -o %t
 # RUN: ld.lld %t -o %t2
 # RUN: llvm-readobj -file-headers -sections -program-headers -symbols %t2 \
 # RUN:   | FileCheck %s
-# REQUIRES: aarch64
 
 # exits with return code 42 on FreeBSD/AArch64
 .globl _start
diff --git a/test/ELF/basic-freebsd.s b/test/ELF/basic-freebsd.s
index 375fdb5..f614bb6 100644
--- a/test/ELF/basic-freebsd.s
+++ b/test/ELF/basic-freebsd.s
@@ -1,9 +1,9 @@
+# REQUIRES: x86
 # Verify that OSABI is set to the correct value.
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-freebsd %s -o %t
 # RUN: ld.lld %t -o %t2
 # RUN: llvm-readobj -file-headers %t2 | FileCheck %s
-# REQUIRES: x86
 
 .globl _start
 _start:
diff --git a/test/ELF/basic-mips.s b/test/ELF/basic-mips.s
index edc91b4..9ecabff 100644
--- a/test/ELF/basic-mips.s
+++ b/test/ELF/basic-mips.s
@@ -1,10 +1,9 @@
+# REQUIRES: mips
 # RUN: llvm-mc -filetype=obj -triple=mipsel-unknown-linux %s -o %t.o
 # RUN: ld.lld %t.o -o %t.exe
 # RUN: llvm-readobj -file-headers -sections -program-headers -symbols %t.exe \
 # RUN:   | FileCheck %s
 
-# REQUIRES: mips
-
 # Exits with return code 1 on Linux.
         .globl  __start
 __start:
diff --git a/test/ELF/basic-ppc.s b/test/ELF/basic-ppc.s
index 2e52514..48b146a 100644
--- a/test/ELF/basic-ppc.s
+++ b/test/ELF/basic-ppc.s
@@ -1,7 +1,7 @@
+# REQUIRES: ppc
 # RUN: llvm-mc -filetype=obj -triple=powerpc-unknown-freebsd %s -o %t
 # RUN: ld.lld --hash-style=sysv -discard-all -shared %t -o %t2
 # RUN: llvm-readobj -file-headers -sections -section-data -program-headers %t2 | FileCheck %s
-# REQUIRES: ppc
 
 # exits with return code 42 on FreeBSD
 .text
@@ -65,7 +65,7 @@
 // CHECK-NEXT:     Address: 0x114
 // CHECK-NEXT:     Offset: 0x114
 // CHECK-NEXT:     Size: 16
-// CHECK-NEXT:     Link: 2
+// CHECK-NEXT:     Link: 3
 // CHECK-NEXT:     Info: 1
 // CHECK-NEXT:     AddressAlignment: 4
 // CHECK-NEXT:     EntrySize: 16
@@ -75,31 +75,13 @@
 // CHECK-NEXT:   }
 // CHECK-NEXT:   Section {
 // CHECK-NEXT:     Index: 2
-// CHECK-NEXT:     Name: .dynstr
-// CHECK-NEXT:     Type: SHT_STRTAB (0x3)
-// CHECK-NEXT:     Flags [ (0x2)
-// CHECK-NEXT:       SHF_ALLOC (0x2)
-// CHECK-NEXT:     ]
-// CHECK-NEXT:     Address: 0x124
-// CHECK-NEXT:     Offset: 0x124
-// CHECK-NEXT:     Size: 1
-// CHECK-NEXT:     Link: 0
-// CHECK-NEXT:     Info: 0
-// CHECK-NEXT:     AddressAlignment: 1
-// CHECK-NEXT:     EntrySize: 0
-// CHECK-NEXT:     SectionData (
-// CHECK-NEXT:       0000: 00                                   |.|
-// CHECK-NEXT:     )
-// CHECK-NEXT:   }
-// CHECK-NEXT:   Section {
-// CHECK-NEXT:     Index: 3
 // CHECK-NEXT:     Name: .hash
 // CHECK-NEXT:     Type: SHT_HASH (0x5)
 // CHECK-NEXT:     Flags [ (0x2)
 // CHECK-NEXT:       SHF_ALLOC (0x2)
 // CHECK-NEXT:     ]
-// CHECK-NEXT:     Address: 0x128
-// CHECK-NEXT:     Offset: 0x128
+// CHECK-NEXT:     Address: 0x124
+// CHECK-NEXT:     Offset: 0x124
 // CHECK-NEXT:     Size: 16
 // CHECK-NEXT:     Link: 1
 // CHECK-NEXT:     Info: 0
@@ -110,6 +92,24 @@
 // CHECK-NEXT:     )
 // CHECK-NEXT:   }
 // CHECK-NEXT:   Section {
+// CHECK-NEXT:     Index: 3
+// CHECK-NEXT:     Name: .dynstr
+// CHECK-NEXT:     Type: SHT_STRTAB (0x3)
+// CHECK-NEXT:     Flags [ (0x2)
+// CHECK-NEXT:       SHF_ALLOC (0x2)
+// CHECK-NEXT:     ]
+// CHECK-NEXT:     Address: 0x134
+// CHECK-NEXT:     Offset: 0x134
+// CHECK-NEXT:     Size: 1
+// CHECK-NEXT:     Link: 0
+// CHECK-NEXT:     Info: 0
+// CHECK-NEXT:     AddressAlignment: 1
+// CHECK-NEXT:     EntrySize: 0
+// CHECK-NEXT:     SectionData (
+// CHECK-NEXT:       0000: 00                                   |.|
+// CHECK-NEXT:     )
+// CHECK-NEXT:   }
+// CHECK-NEXT:   Section {
 // CHECK-NEXT:     Index: 4
 // CHECK-NEXT:     Name: .text
 // CHECK-NEXT:     Type: SHT_PROGBITS (0x1)
@@ -139,14 +139,14 @@
 // CHECK-NEXT:     Address: 0x2000
 // CHECK-NEXT:     Offset: 0x2000
 // CHECK-NEXT:     Size: 48
-// CHECK-NEXT:     Link: 2
+// CHECK-NEXT:     Link: 3
 // CHECK-NEXT:     Info: 0
 // CHECK-NEXT:     AddressAlignment: 4
 // CHECK-NEXT:     EntrySize: 8
 // CHECK-NEXT:     SectionData (
-// CHECK-NEXT:       0000: 00000006 00000114 0000000B 00000010  |................|
-// CHECK-NEXT:       0010: 00000005 00000124 0000000A 00000001  |.......$........|
-// CHECK-NEXT:       0020: 00000004 00000128 00000000 00000000  |.......(........|
+// CHECK-NEXT:       0000: 00000006 00000114 0000000B 00000010
+// CHECK-NEXT:       0010: 00000005 00000134 0000000A 00000001
+// CHECK-NEXT:       0020: 00000004 00000124 00000000 00000000
 // CHECK-NEXT:     )
 // CHECK-NEXT:   }
 // CHECK-NEXT:   Section {
@@ -200,8 +200,8 @@
 // CHECK-NEXT:     AddressAlignment: 1
 // CHECK-NEXT:     EntrySize: 0
 // CHECK-NEXT:     SectionData (
-// CHECK-NEXT:       0000: 002E6479 6E73796D 002E6479 6E737472  |..dynsym..dynstr|
-// CHECK-NEXT:       0010: 002E6861 7368002E 74657874 002E6479  |..hash..text..dy|
+// CHECK-NEXT:       0000: 002E6479 6E73796D 002E6861 7368002E  |..dynsym..hash..|
+// CHECK-NEXT:       0010: 64796E73 7472002E 74657874 002E6479  |dynstr..text..dy|
 // CHECK-NEXT:       0020: 6E616D69 63002E63 6F6D6D65 6E74002E  |namic..comment..|
 // CHECK-NEXT:       0030: 73796D74 6162002E 73687374 72746162  |symtab..shstrtab|
 // CHECK-NEXT:       0040: 002E7374 72746162 00                 |..strtab.|
@@ -243,8 +243,8 @@
 // CHECK-NEXT:     Offset: 0x0
 // CHECK-NEXT:     VirtualAddress: 0x0
 // CHECK-NEXT:     PhysicalAddress: 0x0
-// CHECK-NEXT:     FileSize: 312
-// CHECK-NEXT:     MemSize: 312
+// CHECK-NEXT:     FileSize: 309
+// CHECK-NEXT:     MemSize: 309
 // CHECK-NEXT:     Flags [ (0x4)
 // CHECK-NEXT:       PF_R (0x4)
 // CHECK-NEXT:     ]
diff --git a/test/ELF/basic-ppc64.s b/test/ELF/basic-ppc64.s
index fd933b8..f586d63 100644
--- a/test/ELF/basic-ppc64.s
+++ b/test/ELF/basic-ppc64.s
@@ -1,7 +1,7 @@
+# REQUIRES: ppc
 # # RUN: llvm-mc -filetype=obj -triple=powerpc64le-unknown-linux %s -o %t
 # RUN: ld.lld --hash-style=sysv -discard-all -shared %t -o %t2
 # RUN: llvm-readobj -file-headers -sections -section-data -program-headers %t2 | FileCheck %s
-# REQUIRES: ppc
 .abiversion 2
 # Exits with return code 55 on linux.
 .text
@@ -66,7 +66,7 @@
 // CHECK-NEXT:    Address: 0x1C8
 // CHECK-NEXT:    Offset: 0x1C8
 // CHECK-NEXT:    Size: 24
-// CHECK-NEXT:    Link: 2
+// CHECK-NEXT:    Link: 3
 // CHECK-NEXT:    Info: 1
 // CHECK-NEXT:    AddressAlignment: 8
 // CHECK-NEXT:    EntrySize: 24
@@ -77,31 +77,13 @@
 // CHECK-NEXT:  }
 // CHECK-NEXT:  Section {
 // CHECK-NEXT:    Index: 2
-// CHECK-NEXT:    Name: .dynstr (9)
-// CHECK-NEXT:    Type: SHT_STRTAB (0x3)
+// CHECK-NEXT:    Name: .hash (9)
+// CHECK-NEXT:    Type: SHT_HASH (0x5)
 // CHECK-NEXT:    Flags [ (0x2)
 // CHECK-NEXT:      SHF_ALLOC (0x2)
 // CHECK-NEXT:    ]
 // CHECK-NEXT:    Address: 0x1E0
 // CHECK-NEXT:    Offset: 0x1E0
-// CHECK-NEXT:    Size: 1
-// CHECK-NEXT:    Link: 0
-// CHECK-NEXT:    Info: 0
-// CHECK-NEXT:    AddressAlignment: 1
-// CHECK-NEXT:    EntrySize: 0
-// CHECK-NEXT:    SectionData (
-// CHECK-NEXT:      0000: 00                                   |.|
-// CHECK-NEXT:    )
-// CHECK-NEXT:  }
-// CHECK-NEXT:  Section {
-// CHECK-NEXT:    Index: 3
-// CHECK-NEXT:    Name: .hash (17)
-// CHECK-NEXT:    Type: SHT_HASH (0x5)
-// CHECK-NEXT:    Flags [ (0x2)
-// CHECK-NEXT:      SHF_ALLOC (0x2)
-// CHECK-NEXT:    ]
-// CHECK-NEXT:    Address: 0x1E4
-// CHECK-NEXT:    Offset: 0x1E4
 // CHECK-NEXT:    Size: 16
 // CHECK-NEXT:    Link: 1
 // CHECK-NEXT:    Info: 0
@@ -112,6 +94,24 @@
 // CHECK-NEXT:    )
 // CHECK-NEXT:  }
 // CHECK-NEXT:  Section {
+// CHECK-NEXT:    Index: 3
+// CHECK-NEXT:    Name: .dynstr (15)
+// CHECK-NEXT:    Type: SHT_STRTAB (0x3)
+// CHECK-NEXT:    Flags [ (0x2)
+// CHECK-NEXT:      SHF_ALLOC (0x2)
+// CHECK-NEXT:    ]
+// CHECK-NEXT:    Address: 0x1F0
+// CHECK-NEXT:    Offset: 0x1F0
+// CHECK-NEXT:    Size: 1
+// CHECK-NEXT:    Link: 0
+// CHECK-NEXT:    Info: 0
+// CHECK-NEXT:    AddressAlignment: 1
+// CHECK-NEXT:    EntrySize: 0
+// CHECK-NEXT:    SectionData (
+// CHECK-NEXT:      0000: 00                                   |.|
+// CHECK-NEXT:    )
+// CHECK-NEXT:  }
+// CHECK-NEXT:  Section {
 // CHECK-NEXT:    Index: 4
 // CHECK-NEXT:    Name: .text (23)
 // CHECK-NEXT:    Type: SHT_PROGBITS (0x1)
@@ -141,16 +141,16 @@
 // CHECK-NEXT:    Address: 0x20000
 // CHECK-NEXT:    Offset: 0x20000
 // CHECK-NEXT:    Size: 96
-// CHECK-NEXT:    Link: 2
+// CHECK-NEXT:    Link: 3
 // CHECK-NEXT:    Info: 0
 // CHECK-NEXT:    AddressAlignment: 8
 // CHECK-NEXT:    EntrySize: 16
 // CHECK-NEXT:    SectionData (
 // CHECK-NEXT:      0000: 06000000 00000000 C8010000 00000000  |................|
 // CHECK-NEXT:      0010: 0B000000 00000000 18000000 00000000  |................|
-// CHECK-NEXT:      0020: 05000000 00000000 E0010000 00000000  |................|
+// CHECK-NEXT:      0020: 05000000 00000000 F0010000 00000000  |................|
 // CHECK-NEXT:      0030: 0A000000 00000000 01000000 00000000  |................|
-// CHECK-NEXT:      0040: 04000000 00000000 E4010000 00000000  |................|
+// CHECK-NEXT:      0040: 04000000 00000000 E0010000 00000000  |................|
 // CHECK-NEXT:      0050: 00000000 00000000 00000000 00000000  |................|
 // CHECK-NEXT:    )
 // CHECK-NEXT:  }
@@ -206,8 +206,8 @@
 // CHECK-NEXT:    AddressAlignment: 1
 // CHECK-NEXT:    EntrySize: 0
 // CHECK-NEXT:    SectionData (
-// CHECK-NEXT:      0000: 002E6479 6E73796D 002E6479 6E737472  |..dynsym..dynstr|
-// CHECK-NEXT:      0010: 002E6861 7368002E 74657874 002E6479  |..hash..text..dy|
+// CHECK-NEXT:      0000: 002E6479 6E73796D 002E6861 7368002E  |..dynsym..hash..|
+// CHECK-NEXT:      0010: 64796E73 7472002E 74657874 002E6479  |dynstr..text..dy|
 // CHECK-NEXT:      0020: 6E616D69 63002E63 6F6D6D65 6E74002E  |namic..comment..|
 // CHECK-NEXT:      0030: 73796D74 6162002E 73687374 72746162  |symtab..shstrtab|
 // CHECK-NEXT:      0040: 002E7374 72746162 00                 |..strtab.|
@@ -249,8 +249,8 @@
 // CHECK-NEXT:    Offset: 0x0
 // CHECK-NEXT:    VirtualAddress: 0x0
 // CHECK-NEXT:    PhysicalAddress: 0x0
-// CHECK-NEXT:    FileSize: 500
-// CHECK-NEXT:    MemSize: 500
+// CHECK-NEXT:    FileSize: 497
+// CHECK-NEXT:    MemSize: 497
 // CHECK-NEXT:    Flags [ (0x4)
 // CHECK-NEXT:      PF_R (0x4)
 // CHECK-NEXT:    ]
diff --git a/test/ELF/basic-sparcv9.s b/test/ELF/basic-sparcv9.s
index 75c2047..272fe01 100644
--- a/test/ELF/basic-sparcv9.s
+++ b/test/ELF/basic-sparcv9.s
@@ -1,8 +1,8 @@
+# REQUIRES: sparc
 # RUN: llvm-mc -filetype=obj -triple=sparc64-unknown-openbsd %s -o %t
 # RUN: ld.lld %t -o %t2
 # RUN: llvm-readobj -file-headers -sections -program-headers -symbols %t2 \
 # RUN:   | FileCheck %s
-# REQUIRES: sparc
 
 # exits with return code 42 on OpenBSD/sparc64
 .global _start
diff --git a/test/ELF/basic32.s b/test/ELF/basic32.s
index 071a063..72058dc 100644
--- a/test/ELF/basic32.s
+++ b/test/ELF/basic32.s
@@ -1,7 +1,7 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %s -o %t
 # RUN: ld.lld %t -o %t2
 # RUN: llvm-readobj -file-headers -sections -program-headers %t2 | FileCheck %s
-# REQUIRES: x86
 
 # exits with return code 42 on linux
 .globl _start
diff --git a/test/ELF/basic64be.s b/test/ELF/basic64be.s
index 670bc0b..2bef154 100644
--- a/test/ELF/basic64be.s
+++ b/test/ELF/basic64be.s
@@ -1,7 +1,7 @@
+# REQUIRES: ppc
 # RUN: llvm-mc -filetype=obj -triple=powerpc64-unknown-linux %s -o %t
 # RUN: ld.lld -discard-all %t -o %t2
 # RUN: llvm-readobj -file-headers -sections -section-data -program-headers %t2 | FileCheck %s
-# REQUIRES: ppc
 
 # exits with return code 42 on linux
 .text
diff --git a/test/ELF/bss.s b/test/ELF/bss.s
index abd7f2e..d354498 100644
--- a/test/ELF/bss.s
+++ b/test/ELF/bss.s
@@ -1,7 +1,7 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 // RUN: ld.lld %t -o %t2
 // RUN: llvm-readobj -sections %t2 | FileCheck %s
-// REQUIRES: x86
 
 // Test that bss takes no space on disk.
 
diff --git a/test/ELF/bsymbolic-undef.s b/test/ELF/bsymbolic-undef.s
index 19bb316..1269cb4 100644
--- a/test/ELF/bsymbolic-undef.s
+++ b/test/ELF/bsymbolic-undef.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 # RUN: ld.lld -shared -Bsymbolic %t.o -o %t.so
 # RUN: llvm-readobj -dyn-symbols %t.so | FileCheck %s
diff --git a/test/ELF/bsymbolic.s b/test/ELF/bsymbolic.s
index 5a089d5..adb9b3c 100644
--- a/test/ELF/bsymbolic.s
+++ b/test/ELF/bsymbolic.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 // RUN: ld.lld -shared %t.o -o %t0.so
 // RUN: ld.lld -shared -Bsymbolic %t.o -o %t1.so
diff --git a/test/ELF/cgprofile-bad-clusters.s b/test/ELF/cgprofile-bad-clusters.s
index 77bfe69..a6a09bd 100644
--- a/test/ELF/cgprofile-bad-clusters.s
+++ b/test/ELF/cgprofile-bad-clusters.s
@@ -1,8 +1,7 @@
+# REQUIRES: x86
 # This test checks that CallGraphSort ignores edges that would form "bad"
 # clusters.
 
-# REQUIRES: x86
-
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 # RUN: echo "A C 1" > %t.call_graph
 # RUN: echo "E B 4" >> %t.call_graph
diff --git a/test/ELF/cgprofile-err.s b/test/ELF/cgprofile-err.s
new file mode 100644
index 0000000..6b5425d
--- /dev/null
+++ b/test/ELF/cgprofile-err.s
@@ -0,0 +1,13 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+
+# RUN: echo "A B C 100" > %t.call_graph
+# RUN: not ld.lld %t --call-graph-ordering-file \
+# RUN:   %t.call_graph -o /dev/null 2>&1 | FileCheck %s
+
+# CHECK: {{.*}}.call_graph: parse error
+
+# RUN: echo "A B C" > %t.call_graph
+# RUN: not ld.lld %t --call-graph-ordering-file \
+# RUN:   %t.call_graph -o /dev/null 2>&1 | FileCheck %s
diff --git a/test/ELF/cgprofile-warn.s b/test/ELF/cgprofile-warn.s
index 2b30a1a..bfb89c4 100644
--- a/test/ELF/cgprofile-warn.s
+++ b/test/ELF/cgprofile-warn.s
@@ -5,9 +5,10 @@
 # RUN: echo "A B 100" > %t.call_graph
 # RUN: echo "A C 40" >> %t.call_graph
 # RUN: echo "B C 30" >> %t.call_graph
-# RUN: echo "adena A 30" >> %t.call_graph
+# RUN: echo "adena1 A 30" >> %t.call_graph
+# RUN: echo "A adena2 30" >> %t.call_graph
 # RUN: echo "poppy A 30" >> %t.call_graph
-# RUN: ld.lld -e A %t --call-graph-ordering-file %t.call_graph -o %t.out \
+# RUN: ld.lld -e A %t --call-graph-ordering-file %t.call_graph -o /dev/null \
 # RUN:   -noinhibit-exec -icf=all 2>&1 | FileCheck %s
 
     .section    .text.C,"ax",@progbits
@@ -25,5 +26,11 @@
     retq
 
 # CHECK: unable to order absolute symbol: B
-# CHECK: call graph file: no such symbol: adena
+# CHECK: {{.*}}.call_graph: no such symbol: adena1
+# CHECK: {{.*}}.call_graph: no such symbol: adena2
 # CHECK: unable to order undefined symbol: poppy
+
+# RUN: ld.lld %t --call-graph-ordering-file %t.call_graph -o /dev/null \
+# RUN:   -noinhibit-exec -icf=all --no-warn-symbol-ordering 2>&1 \
+# RUN:   | FileCheck %s --check-prefix=NOWARN
+# NOWARN-NOT: unable to order
diff --git a/test/ELF/color-diagnostics.test b/test/ELF/color-diagnostics.test
index 074bba2..6dfa0ab 100644
--- a/test/ELF/color-diagnostics.test
+++ b/test/ELF/color-diagnostics.test
@@ -9,6 +9,9 @@
 # COLOR: {{ld.lld: .\[0;1;31merror: .\[0munknown argument: -xyz}}
 # COLOR: {{ld.lld: .\[0;1;31merror: .\[0mcannot open /nosuchfile}}
 
+# RUN: not ld.lld -color-diagnostics=foobar 2>&1 | FileCheck -check-prefix=ERR %s
+# ERR: unknown option: --color-diagnostics=foobar
+
 # RUN: not ld.lld /nosuchfile 2>&1 | FileCheck -check-prefix=NOCOLOR %s
 # RUN: not ld.lld -color-diagnostics=never /nosuchfile 2>&1 \
 # RUN:   | FileCheck -check-prefix=NOCOLOR %s
diff --git a/test/ELF/comdat-discarded-reloc.s b/test/ELF/comdat-discarded-reloc.s
new file mode 100644
index 0000000..d23baf3
--- /dev/null
+++ b/test/ELF/comdat-discarded-reloc.s
@@ -0,0 +1,17 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/comdat-discarded-reloc.s -o %t2.o
+# RUN: ld.lld -gc-sections %t.o %t2.o -o %t
+
+## ELF spec doesn't allow a relocation to point to a deduplicated
+## COMDAT section. Unfortunately this happens in practice (e.g. .eh_frame)
+## Test case checks we do not crash.
+
+.global bar, _start
+
+.section .text.foo,"aG",@progbits,group,comdat
+
+.section .text
+_start:
+ .quad .text.foo
+ .quad bar
diff --git a/test/ELF/comdat-linkonce.s b/test/ELF/comdat-linkonce.s
index 78611b4..8721f58 100644
--- a/test/ELF/comdat-linkonce.s
+++ b/test/ELF/comdat-linkonce.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/comdat.s -o %t2.o
 // RUN: ld.lld -shared %t.o %t2.o -o %t
diff --git a/test/ELF/comdat.s b/test/ELF/comdat.s
index 7bf2ce5..4728dd3 100644
--- a/test/ELF/comdat.s
+++ b/test/ELF/comdat.s
@@ -1,9 +1,9 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/comdat.s -o %t2.o
 // RUN: ld.lld -shared %t.o %t.o %t2.o -o %t
 // RUN: llvm-objdump -d %t | FileCheck %s
 // RUN: llvm-readobj -s -t %t | FileCheck --check-prefix=READ %s
-// REQUIRES: x86
 
 // Check that we don't crash with --gc-section and that we print a list of
 // reclaimed sections on stderr.
diff --git a/test/ELF/common.s b/test/ELF/common.s
index 7f241ee..da66571 100644
--- a/test/ELF/common.s
+++ b/test/ELF/common.s
@@ -1,8 +1,8 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/common.s -o %t2
 // RUN: ld.lld %t %t2 -o %t3
 // RUN: llvm-readobj -t -s %t3 | FileCheck %s
-// REQUIRES: x86
 
 // CHECK:      Name: .bss
 // CHECK-NEXT: Type: SHT_NOBITS
diff --git a/test/ELF/compatible-section-types.s b/test/ELF/compatible-section-types.s
index a5dadb8..e47006c 100644
--- a/test/ELF/compatible-section-types.s
+++ b/test/ELF/compatible-section-types.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: ld.lld -shared %t.o -o %t
 // RUN: llvm-objdump -section-headers %t | FileCheck %s
diff --git a/test/ELF/compressed-debug-conflict.s b/test/ELF/compressed-debug-conflict.s
index f7ddd2d..e8c2426 100644
--- a/test/ELF/compressed-debug-conflict.s
+++ b/test/ELF/compressed-debug-conflict.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86, zlib
 # RUN: llvm-mc -filetype=obj -triple i686-linux-gnu -compress-debug-sections=zlib %s -o %t.o
 # RUN: llvm-readobj -sections %t.o | FileCheck -check-prefix=OBJ %s
-# RUN: not ld.lld %t.o %t.o -o %tout 2>&1 | FileCheck -check-prefix=ERROR %s
+# RUN: not ld.lld %t.o %t.o -o /dev/null 2>&1 | FileCheck -check-prefix=ERROR %s
 
 # OBJ:      Sections [
 # OBJ:        Section {
diff --git a/test/ELF/compressed-debug-input-err.s b/test/ELF/compressed-debug-input-err.s
new file mode 100644
index 0000000..e32ba31
--- /dev/null
+++ b/test/ELF/compressed-debug-input-err.s
@@ -0,0 +1,11 @@
+# REQUIRES: zlib, x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: not ld.lld %t.o -o %t.so -shared 2>&1 | FileCheck %s
+
+## Check we are able to report zlib decompressor errors.
+# CHECK: error: {{.*}}.o:(.zdebug_str): decompress failed: zlib error: Z_DATA_ERROR
+
+.section .zdebug_str,"MS",@progbits,1
+ .ascii "ZLIB"
+ .byte 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1
diff --git a/test/ELF/conflict-debug-variable-file-index.s b/test/ELF/conflict-debug-variable-file-index.s
new file mode 100644
index 0000000..c7bd8bb
--- /dev/null
+++ b/test/ELF/conflict-debug-variable-file-index.s
@@ -0,0 +1,103 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: not ld.lld %t.o %t.o -o %t 2>&1 | FileCheck %s
+
+## Check we are able to report errors even if DW_AT_decl_file
+## contains invalid file name index.
+## We did not try to support this intentionally but have
+## an error handling and reporting logic that can take care
+## of that and hence needs a test. 
+
+# CHECK:      duplicate symbol: foo
+# CHECK-NEXT: >>> defined at {{.*}}.o:(foo)
+# CHECK-NEXT: >>>            {{.*}}.o:(.bss+0x0)
+
+# Used modified output from the following code and gcc 7.1.0:
+# Source (1.c):
+#  int foo = 0;
+# Invocation: g++ -g -S 1.c
+
+.bss
+.globl  foo
+.type  foo, @object
+.size  foo, 4
+foo:
+
+.text
+.file 1 "1.c"
+
+.section  .debug_info,"",@progbits
+  .long  0x35            # Compile Unit: length = 0x0000004b)
+  .value  0x4            # version = 0x0004
+  .long  0               # abbr_offset = 0x0
+  .byte  0x8             # addr_size = 0x08
+
+  .uleb128 0x1           # DW_TAG_compile_unit [1] *
+  .long  0               # DW_AT_producer [DW_FORM_strp]  ( .debug_str[0x00000000] = )
+  .byte  0x4             # DW_AT_language [DW_FORM_data1]  (DW_LANG_C_plus_plus)
+  .string  "1.c"         # DW_AT_name [DW_FORM_string]  ("1.c")
+  .long  0               # DW_AT_comp_dir [DW_FORM_strp]  ( .debug_str[0x00000000] = )
+  .long  0               # DW_AT_stmt_list [DW_FORM_sec_offset]  (0x00000000)
+
+  .uleb128 0x2           # DW_TAG_variable [2]
+  .string  "foo"         # DW_AT_name [DW_FORM_string]  ("foo")
+  .byte  0xFE            # DW_AT_decl_file [DW_FORM_data1]  <broken file>
+  .byte  0x1             # DW_AT_decl_line [DW_FORM_data1]  (1)
+  .long  0x32            # DW_AT_type [DW_FORM_ref4]  (cu + 0x0032 => {0x00000032})
+  .uleb128 0x9           # DW_AT_external [DW_FORM_flag_present]  (true)
+  .byte  0x3
+  .quad  foo             # DW_AT_location [DW_FORM_exprloc]  (DW_OP_addr 0x0)
+
+  .uleb128 0x3           # DW_TAG_base_type [3]
+  .byte  0x4             # DW_AT_byte_size [DW_FORM_data1]  (0x04)
+  .byte  0x5             # DW_AT_encoding [DW_FORM_data1]  (DW_ATE_signed)
+  .string  "int"         # DW_AT_name [DW_FORM_string]  ("int")
+
+.section  .debug_abbrev,"",@progbits
+  .uleb128 0x1   # Abbreviation code.
+  .uleb128 0x11  # DW_TAG_compile_unit
+
+  .byte  0x1     # ID
+  .uleb128 0x25  # DW_AT_producer, DW_FORM_strp
+  .uleb128 0xe
+  .uleb128 0x13  # DW_AT_language, DW_FORM_data1
+  .uleb128 0xb
+  .uleb128 0x3   # DW_AT_name, DW_FORM_string
+  .uleb128 0x8
+  .uleb128 0x1b  # DW_AT_comp_dir, DW_FORM_strp
+  .uleb128 0xe
+  .uleb128 0x10  # DW_AT_stmt_list, DW_FORM_sec_offset
+  .uleb128 0x17
+  .byte  0
+  .byte  0
+
+  .uleb128 0x2  # ID
+  .uleb128 0x34 # DW_TAG_variable, DW_CHILDREN_no
+  .byte  0
+  .uleb128 0x3  # DW_AT_name, DW_FORM_string
+  .uleb128 0x8
+  .uleb128 0x3a # DW_AT_decl_file, DW_FORM_data1
+  .uleb128 0xb
+  .uleb128 0x3b # DW_AT_decl_line, DW_FORM_data1
+  .uleb128 0xb
+  .uleb128 0x49 # DW_AT_type, DW_FORM_ref4
+  .uleb128 0x13
+  .uleb128 0x3f # DW_AT_external, DW_FORM_flag_present
+  .uleb128 0x19
+  .uleb128 0x2  # DW_AT_location, DW_FORM_exprloc
+  .uleb128 0x18
+  .byte  0
+  .byte  0
+
+  .uleb128 0x3  # ID
+  .uleb128 0x24 # DW_TAG_base_type, DW_CHILDREN_no
+  .byte  0
+  .uleb128 0xb  # DW_AT_byte_size, DW_FORM_data1
+  .uleb128 0xb
+  .uleb128 0x3e # DW_AT_encoding, DW_FORM_data1
+  .uleb128 0xb
+  .uleb128 0x3  # DW_AT_name, DW_FORM_string
+  .uleb128 0x8
+  .byte  0
+  .byte  0
+  .byte  0
diff --git a/test/ELF/conflict-debug-variable.s b/test/ELF/conflict-debug-variable.s
index 409c38e..244ac14 100644
--- a/test/ELF/conflict-debug-variable.s
+++ b/test/ELF/conflict-debug-variable.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 # RUN: llvm-dwarfdump %t.o | FileCheck -check-prefix=INPUT %s
 # RUN: not ld.lld %t.o %t.o -o %t 2>&1 | FileCheck %s
@@ -38,6 +39,7 @@
 # Source (1.c):
 #  int foo = 0;
 #  int bar = 1;
+#  static int zed = 3;
 # Invocation: g++ -g -S 1.c
 
 .bss
@@ -51,12 +53,16 @@
 .type  bar, @object
 .size  bar, 4
 bar:
+ .byte 0
+
+.local zed
+zed:
 
 .text
 .file 1 "1.c"
 
 .section  .debug_info,"",@progbits
-  .long  0x4b            # Compile Unit: length = 0x0000004b)
+  .long  0x5a            # Compile Unit: length = 0x0000004b)
   .value  0x4            # version = 0x0004
   .long  0               # abbr_offset = 0x0
   .byte  0x8             # addr_size = 0x08
@@ -90,6 +96,14 @@
   .uleb128 0x9           # DW_AT_external [DW_FORM_flag_present]  (true)
   .byte  0x3
   .quad  bar             # DW_AT_location [DW_FORM_exprloc]  (DW_OP_addr 0x0)
+  
+  .uleb128 0x4           # DW_TAG_variable [2]
+  .string  "zed"         # DW_AT_name [DW_FORM_string]  ("zed")
+  .byte  0x1             # DW_AT_decl_file [DW_FORM_data1]  ("1.c")
+  .byte  0x3             # DW_AT_decl_line [DW_FORM_data1]  (2)
+  .long  0x32            # DW_AT_type [DW_FORM_ref4]  (cu + 0x0032 => {0x00000032})
+  .quad  zed             # DW_AT_location [DW_FORM_exprloc]  (DW_OP_addr 0x0)
+  
   .byte  0               # END
 
 
@@ -140,5 +154,21 @@
   .uleb128 0x8
   .byte  0
   .byte  0
+  
+  .uleb128 0x4  # ID
+  .uleb128 0x34 # DW_TAG_variable, DW_CHILDREN_no
+  .byte  0
+  .uleb128 0x3  # DW_AT_name, DW_FORM_string
+  .uleb128 0x8
+  .uleb128 0x3a # DW_AT_decl_file, DW_FORM_data1
+  .uleb128 0xb
+  .uleb128 0x3b # DW_AT_decl_line, DW_FORM_data1
+  .uleb128 0xb
+  .uleb128 0x49 # DW_AT_type, DW_FORM_ref4
+  .uleb128 0x13
+  .uleb128 0x2  # DW_AT_location, DW_FORM_exprloc
+  .uleb128 0x18
+  .byte  0
   .byte  0
 
+  .byte  0
diff --git a/test/ELF/conflict-debug-variable2.s b/test/ELF/conflict-debug-variable2.s
index 1fb9b09..3fb59e6 100644
--- a/test/ELF/conflict-debug-variable2.s
+++ b/test/ELF/conflict-debug-variable2.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 
 # RUN: llvm-dwarfdump -v %t.o | FileCheck -check-prefix=INPUT %s
@@ -18,7 +19,7 @@
 # INPUT-NEXT:    DW_AT_location [DW_FORM_exprloc]        (DW_OP_addr 0x0)
 
 ## Check we use information from .debug_info in messages.
-# RUN: not ld.lld %t.o %t.o -o %t 2>&1 | FileCheck %s
+# RUN: not ld.lld %t.o %t.o -o /dev/null 2>&1 | FileCheck %s
 # CHECK:      duplicate symbol: bar
 # CHECK-NEXT: >>> defined at test.c:2
 # CHECK-NEXT: >>>            {{.*}}:(bar)
diff --git a/test/ELF/conflict-variable-linkage-name.s b/test/ELF/conflict-variable-linkage-name.s
new file mode 100644
index 0000000..9b201d9
--- /dev/null
+++ b/test/ELF/conflict-variable-linkage-name.s
@@ -0,0 +1,176 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: not ld.lld %t.o %t.o -o /dev/null 2>&1 | FileCheck %s
+
+## Check we can report the locations of 2 different "bar" variables.
+# CHECK:      duplicate symbol: A::bar
+# CHECK-NEXT: >>> defined at 1.cpp:2
+# CHECK-NEXT: >>>            {{.*}}:(A::bar)
+# CHECK-NEXT: >>> defined at 1.cpp:2
+# CHECK-NEXT: >>>            {{.*}}:(.bss+0x0)
+# CHECK:      duplicate symbol: Z::bar
+# CHECK-NEXT: >>> defined at 1.cpp:6
+# CHECK-NEXT: >>>            {{.*}}:(Z::bar)
+# CHECK-NEXT: >>> defined at 1.cpp:6
+# CHECK-NEXT: >>>            {{.*}}:(.data+0x0)
+
+# Used reduced output from following code and clang version 7.0.0 (trunk 332701)
+# to produce this input file:
+# Source (1.cpp):
+#  namespace A {
+#    int bar;
+#  }
+#  
+#  namespace Z {
+#    int bar;
+#  }
+# Invocation: clang-7 -g -S 1.cpp
+
+.text
+.file  "1.cpp"
+.file  1 "/path" "1.cpp"
+
+.type  _ZN1A3barE,@object
+.bss
+.globl  _ZN1A3barE
+_ZN1A3barE:
+  .long  0
+  .size  _ZN1A3barE, 4
+
+.type  _ZN1Z3barE,@object
+.data
+.globl  _ZN1Z3barE
+_ZN1Z3barE:
+  .long  1
+  .size  _ZN1Z3barE, 4
+
+.section  .debug_str,"MS",@progbits,1
+.Linfo_string0:
+  .asciz  "clang version 7.0.0 (trunk 332701)" # string offset=0
+.Linfo_string1:
+  .asciz  "1.cpp"                 # string offset=35
+.Linfo_string2:
+  .asciz  "/path"                 # string offset=41
+.Linfo_string3:
+  .asciz  "A"                     # string offset=87
+.Linfo_string4:
+  .asciz  "bar"                   # string offset=89
+.Linfo_string5:
+  .asciz  "int"                   # string offset=93
+.Linfo_string6:
+  .asciz  "_ZN1A3barE"            # string offset=97
+.Linfo_string7:
+  .asciz  "Z"                     # string offset=108
+.Linfo_string8:
+  .asciz  "_ZN1Z3barE"            # string offset=110
+
+.section  .debug_abbrev,"",@progbits
+  .byte  1                       # Abbreviation Code
+  .byte  17                      # DW_TAG_compile_unit
+  .byte  1                       # DW_CHILDREN_yes
+  .byte  37                      # DW_AT_producer
+  .byte  14                      # DW_FORM_strp
+  .byte  19                      # DW_AT_language
+  .byte  5                       # DW_FORM_data2
+  .byte  3                       # DW_AT_name
+  .byte  14                      # DW_FORM_strp
+  .byte  16                      # DW_AT_stmt_list
+  .byte  23                      # DW_FORM_sec_offset
+  .byte  27                      # DW_AT_comp_dir
+  .byte  14                      # DW_FORM_strp
+  .ascii  "\264B"                # DW_AT_GNU_pubnames
+  .byte  25                      # DW_FORM_flag_present
+  .byte  0                       # EOM(1)
+  .byte  0                       # EOM(2)
+  
+  .byte  2                       # Abbreviation Code
+  .byte  57                      # DW_TAG_namespace
+  .byte  1                       # DW_CHILDREN_yes
+  .byte  3                       # DW_AT_name
+  .byte  14                      # DW_FORM_strp
+  .byte  0                       # EOM(1)
+  .byte  0                       # EOM(2)
+  
+  .byte  3                       # Abbreviation Code
+  .byte  52                      # DW_TAG_variable
+  .byte  0                       # DW_CHILDREN_no
+  .byte  3                       # DW_AT_name
+  .byte  14                      # DW_FORM_strp
+  .byte  73                      # DW_AT_type
+  .byte  19                      # DW_FORM_ref4
+  .byte  63                      # DW_AT_external
+  .byte  25                      # DW_FORM_flag_present
+  .byte  58                      # DW_AT_decl_file
+  .byte  11                      # DW_FORM_data1
+  .byte  59                      # DW_AT_decl_line
+  .byte  11                      # DW_FORM_data1
+  .byte  2                       # DW_AT_location
+  .byte  24                      # DW_FORM_exprloc
+  .byte  110                     # DW_AT_linkage_name
+  .byte  14                      # DW_FORM_strp
+  .byte  0                       # EOM(1)
+  .byte  0                       # EOM(2)
+  
+  .byte  4                       # Abbreviation Code
+  .byte  36                      # DW_TAG_base_type
+  .byte  0                       # DW_CHILDREN_no
+  .byte  3                       # DW_AT_name
+  .byte  14                      # DW_FORM_strp
+  .byte  62                      # DW_AT_encoding
+  .byte  11                      # DW_FORM_data1
+  .byte  11                      # DW_AT_byte_size
+  .byte  11                      # DW_FORM_data1
+  .byte  0                       # EOM(1)
+  .byte  0                       # EOM(2)
+  .byte  0                       # EOM(3)
+
+  .section  .debug_info,"",@progbits
+  .long  96                      # Length of Unit
+  .short  4                      # DWARF version number
+  .long  .debug_abbrev           # Offset Into Abbrev. Section
+  .byte  8                       # Address Size (in bytes)
+  
+  .byte  1                       # Abbrev [1] 0xb:0x59 DW_TAG_compile_unit
+  .long  .Linfo_string0          # DW_AT_producer
+  .short  4                      # DW_AT_language
+  .long  .Linfo_string1          # DW_AT_name
+  .long  0                       # DW_AT_stmt_list
+  .long  .Linfo_string2          # DW_AT_comp_dir
+                                 # DW_AT_GNU_pubnames
+
+  .byte  2                       # Abbrev [2] 0x1e:0x1f DW_TAG_namespace
+  .long  .Linfo_string3          # DW_AT_name
+  
+  .byte  3                       # Abbrev [3] 0x23:0x19 DW_TAG_variable
+  .long  .Linfo_string4          # DW_AT_name
+  .long  61                      # DW_AT_type
+                                 # DW_AT_external
+  .byte  1                       # DW_AT_decl_file
+  .byte  2                       # DW_AT_decl_line
+  .byte  9                       # DW_AT_location
+  .byte  3
+  .quad  _ZN1A3barE
+  .long  .Linfo_string6          # DW_AT_linkage_name
+  .byte  0                       # End Of Children Mark
+  
+  .byte  4                       # Abbrev [4] 0x3d:0x7 DW_TAG_base_type
+  .long  .Linfo_string5          # DW_AT_name
+  .byte  5                       # DW_AT_encoding
+  .byte  4                       # DW_AT_byte_size
+  
+  .byte  2                       # Abbrev [2] 0x44:0x1f DW_TAG_namespace
+  .long  .Linfo_string7          # DW_AT_name
+  
+  .byte  3                       # Abbrev [3] 0x49:0x19 DW_TAG_variable
+  .long  .Linfo_string4          # DW_AT_name
+  .long  61                      # DW_AT_type
+                                 # DW_AT_external
+  .byte  1                       # DW_AT_decl_file
+  .byte  6                       # DW_AT_decl_line
+  .byte  9                       # DW_AT_location
+  .byte  3
+  .quad  _ZN1Z3barE
+  .long  .Linfo_string8          # DW_AT_linkage_name
+  
+  .byte  0                       # End Of Children Mark
+  .byte  0                       # End Of Children Mark
diff --git a/test/ELF/conflict.s b/test/ELF/conflict.s
index 4318759..6cfb8f5 100644
--- a/test/ELF/conflict.s
+++ b/test/ELF/conflict.s
@@ -26,6 +26,7 @@
 # RUN:   FileCheck -check-prefix=DEMANGLE %s
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %S/Inputs/conflict.s -o %t2.o
+# RUN: rm -f %t3.a
 # RUN: llvm-ar rcs %t3.a %t2.o
 # RUN: not ld.lld %t1.o %t3.a -u baz -o %t2 2>&1 | FileCheck -check-prefix=ARCHIVE %s
 
@@ -34,7 +35,7 @@
 # ARCHIVE-NEXT: >>> defined at {{.*}}:(.text+0x0) in archive {{.*}}.a
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/conflict-debug.s -o %t-dbg.o
-# RUN: not ld.lld %t-dbg.o %t-dbg.o -o %t-dbg 2>&1 | FileCheck -check-prefix=DBGINFO %s
+# RUN: not ld.lld %t-dbg.o %t-dbg.o -o /dev/null 2>&1 | FileCheck -check-prefix=DBGINFO %s
 
 # DBGINFO:      duplicate symbol: zed
 # DBGINFO-NEXT: >>> defined at conflict-debug.s:4
diff --git a/test/ELF/copy-in-shared.s b/test/ELF/copy-in-shared.s
index 6af54c1..a550893 100644
--- a/test/ELF/copy-in-shared.s
+++ b/test/ELF/copy-in-shared.s
@@ -2,7 +2,7 @@
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/copy-in-shared.s -o %t1.o
 // RUN: ld.lld -shared %t1.o -o %t1.so
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t2.o
-// RUN: not ld.lld %t2.o %t1.so -o %t2.so -shared 2>&1 | FileCheck %s
+// RUN: not ld.lld %t2.o %t1.so -o /dev/null -shared 2>&1 | FileCheck %s
 
 // CHECK: can't create dynamic relocation R_X86_64_64 against symbol: foo in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
 // CHECK: >>> defined in {{.*}}.so
diff --git a/test/ELF/copy-rel-corrupted.s b/test/ELF/copy-rel-corrupted.s
index 8df38cf..76f64fa 100644
--- a/test/ELF/copy-rel-corrupted.s
+++ b/test/ELF/copy-rel-corrupted.s
@@ -1,7 +1,8 @@
+// REQUIRES: x86
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
 // RUN: llvm-mc %p/Inputs/copy-rel-corrupted.s -o %t2.o -filetype=obj -triple=x86_64-pc-linux
 // RUN: ld.lld %t2.o -o %t2.so -shared
-// RUN: not ld.lld %t.o %t2.so -o %t.exe 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.o %t2.so -o /dev/null 2>&1 | FileCheck %s
 
 // CHECK: error: cannot create a copy relocation for symbol x
 
diff --git a/test/ELF/copy-rel-pie-error.s b/test/ELF/copy-rel-pie-error.s
index e2fd5b5..379442e 100644
--- a/test/ELF/copy-rel-pie-error.s
+++ b/test/ELF/copy-rel-pie-error.s
@@ -1,7 +1,8 @@
+// REQUIRES: x86
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
 // RUN: llvm-mc %p/Inputs/copy-rel-pie.s -o %t2.o -filetype=obj -triple=x86_64-pc-linux
 // RUN: ld.lld %t2.o -o %t2.so -shared
-// RUN: not ld.lld %t.o %t2.so -o %t.exe -pie 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.o %t2.so -o /dev/null -pie 2>&1 | FileCheck %s
 
 // CHECK: can't create dynamic relocation R_X86_64_64 against symbol: bar in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
 // CHECK: >>> defined in {{.*}}.so
diff --git a/test/ELF/copy-rel-pie.s b/test/ELF/copy-rel-pie.s
index 2a953e0..9bf9159 100644
--- a/test/ELF/copy-rel-pie.s
+++ b/test/ELF/copy-rel-pie.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
 // RUN: llvm-mc %p/Inputs/copy-rel-pie.s -o %t2.o -filetype=obj -triple=x86_64-pc-linux
 // RUN: ld.lld %t2.o -o %t2.so -shared
diff --git a/test/ELF/copy-rel-tls.s b/test/ELF/copy-rel-tls.s
new file mode 100644
index 0000000..265cce1
--- /dev/null
+++ b/test/ELF/copy-rel-tls.s
@@ -0,0 +1,15 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/copy-rel-tls.s -o %t1.o
+// RUN: ld.lld %t1.o -shared -soname t1.so -o %t1.so
+// RUN: ld.lld %t.o %t1.so -o %t
+// RUN: llvm-nm %t1.so | FileCheck %s
+// RUN: llvm-nm %t | FileCheck --check-prefix=TLS %s
+// foo and tfoo have the same st_value but we should not copy tfoo.
+// CHECK: 2000 B foo
+// CHECK: 2000 B tfoo
+// TLS-NOT: tfoo
+
+.global _start
+_start:
+  leaq foo, %rax
diff --git a/test/ELF/copy-rel-version.s b/test/ELF/copy-rel-version.s
new file mode 100644
index 0000000..29fae8e
--- /dev/null
+++ b/test/ELF/copy-rel-version.s
@@ -0,0 +1,15 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/copy-rel-version.s -o %t1.o
+// RUN: echo "v1 {}; v2 {};" > %t.ver
+// RUN: ld.lld %t1.o -shared -soname t1.so --version-script=%t.ver -o %t1.so
+// RUN: ld.lld %t.o %t1.so -o %t
+// RUN: llvm-readobj -t %t | FileCheck %s
+
+.global _start
+_start:
+  leaq foo, %rax
+
+// CHECK:      Name: foo (
+// CHECK-NEXT: Value:
+// CHECK-NEXT: Size: 8
diff --git a/test/ELF/copy-relocation-zero-abs-addr.s b/test/ELF/copy-relocation-zero-abs-addr.s
new file mode 100644
index 0000000..fae963e
--- /dev/null
+++ b/test/ELF/copy-relocation-zero-abs-addr.s
@@ -0,0 +1,44 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/copy-relocation-zero-abs-addr.s -o %t.o
+// RUN: ld.lld -shared -o %t2.so %t.o
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t3.o
+// RUN: ld.lld %t2.so %t3.o -o %t4
+// RUN: llvm-readobj -symbols %t2.so | FileCheck -check-prefix=ABSADDR %s
+// RUN: llvm-readobj -s -r --expand-relocs %t4 | FileCheck %s
+
+// This tests that symbols with absolute addresses are properly
+// handled. Normal DSO symbols are handled as usual.
+
+.text
+.globl _start
+_start:
+  movl $5, foo
+
+// ABSADDR:        Name: ver1
+// ABSADDR-NEXT:   Value: 0x0
+// ABSADDR-NEXT:   Size: 0
+// ABSADDR-NEXT:   Binding: Global
+// ABSADDR-NEXT:   Type: None
+// ABSADDR-NEXT:   Other: 0
+// ABSADDR-NEXT:   Section: Absolute (0xFFF1)
+// ABSADDR-NEXT: }
+// ABSADDR-NEXT: Symbol {
+// ABSADDR-NEXT:   Name: ver2
+// ABSADDR-NEXT:   Value: 0x0
+// ABSADDR-NEXT:   Size: 0
+// ABSADDR-NEXT:   Binding: Global
+// ABSADDR-NEXT:   Type: None
+// ABSADDR-NEXT:   Other: 0
+// ABSADDR-NEXT:   Section: Absolute (0xFFF1)
+// ABSADDR-NEXT: }
+
+// CHECK:      Relocations [
+// CHECK-NEXT:   Section (5) .rela.dyn {
+// CHECK-NEXT:     Relocation {
+// CHECK-NEXT:       Offset:
+// CHECK-NEXT:       Type: R_X86_64_COPY
+// CHECK-NEXT:       Symbol: foo
+// CHECK-NEXT:       Addend:
+// CHECK-NEXT:     }
+// CHECK-NEXT:   }
+// CHECK-NEXT: ]
diff --git a/test/ELF/copy-relocation-zero-nonabs-addr.s b/test/ELF/copy-relocation-zero-nonabs-addr.s
new file mode 100644
index 0000000..5087605
--- /dev/null
+++ b/test/ELF/copy-relocation-zero-nonabs-addr.s
@@ -0,0 +1,29 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/copy-relocation-zero-nonabs-addr.s -o %t1.o
+// RUN: ld.lld -Ttext=0 -o %t2.so --script=%p/Inputs/copy-relocation-zero-nonabs-addr.script %t1.o -shared
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t3.o
+// RUN: ld.lld %t2.so %t3.o -o %t4
+// RUN: llvm-readobj --symbols %t2.so | FileCheck --check-prefix=CHECKSO %s
+// RUN: llvm-readobj --symbols %t4 | FileCheck %s
+
+.text
+.globl _start
+_start:
+  movl $5, foo
+
+// Make sure foo has st_value == 0.
+// CHECKSO:      Name: foo
+// CHECKSO-NEXT: Value: 0x0
+// CHECKSO-NEXT: Size: 4
+// CHECKSO-NEXT: Binding: Global
+// CHECKSO-NEXT: Type: Object
+// CHECKSO-NEXT: Other: 0
+// CHECKSO-NEXT: Section: .text
+
+// When foo has st_value == 0, it carries the section alignment.
+// In this case, section alignment is 2^10, 0x202400 meets the requirement.
+// CHECK:      Name: foo
+// CHECK-NEXT: Value: 0x202400
+// CHECK-NEXT: Size: 4
+// CHECK-NEXT: Binding: Global
+// CHECK-NEXT: Type: Object
diff --git a/test/ELF/corrupted-version-reference.s b/test/ELF/corrupted-version-reference.s
index d37f272..203dc2a 100644
--- a/test/ELF/corrupted-version-reference.s
+++ b/test/ELF/corrupted-version-reference.s
@@ -1,6 +1,6 @@
-# RUN: llvm-mc -triple=mips64-unknown-freebsd %s -filetype=obj -o %t.o
-# RUN: not ld.lld %t.o %S/Inputs/corrupt-version-reference.so -o %t.exe 2>&1 | FileCheck %s
 # REQUIRES: mips
+# RUN: llvm-mc -triple=mips64-unknown-freebsd %s -filetype=obj -o %t.o
+# RUN: not ld.lld %t.o %S/Inputs/corrupt-version-reference.so -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: error: corrupt input file: version definition index 9 for symbol __cxa_finalize is out of bounds
 # CHECK: >>> defined in {{.+}}/corrupt-version-reference.so
diff --git a/test/ELF/ctors_dtors_priority.s b/test/ELF/ctors_dtors_priority.s
index fcddcbb..203bf24 100644
--- a/test/ELF/ctors_dtors_priority.s
+++ b/test/ELF/ctors_dtors_priority.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
 // RUN:   %p/Inputs/ctors_dtors_priority1.s -o %t-crtbegin.o
@@ -7,7 +8,6 @@
 // RUN:   %p/Inputs/ctors_dtors_priority3.s -o %t-crtend.o
 // RUN: ld.lld %t1 %t2 %t-crtend.o %t-crtbegin.o -o %t.exe
 // RUN: llvm-objdump -s %t.exe | FileCheck %s
-// REQUIRES: x86
 
 .globl _start
 _start:
diff --git a/test/ELF/defined-tls_get_addr.s b/test/ELF/defined-tls_get_addr.s
index 509c293..66ec015 100644
--- a/test/ELF/defined-tls_get_addr.s
+++ b/test/ELF/defined-tls_get_addr.s
@@ -1,5 +1,6 @@
+// REQUIRES: x86
 // RUN: llvm-mc %s -o %t.o -triple x86_64-pc-linux -filetype=obj
-// RUN: ld.lld %t.o -o %t
+// RUN: ld.lld %t.o -o /dev/null
 
 // Don't error if __tls_get_addr is defined.
 
diff --git a/test/ELF/defsym.s b/test/ELF/defsym.s
index 2abc08f..c819cd3 100644
--- a/test/ELF/defsym.s
+++ b/test/ELF/defsym.s
@@ -68,8 +68,11 @@
 # EXPR-NEXT:   Section: Absolute
 # EXPR-NEXT: }
 
-# RUN: not ld.lld -o %t %t.o --defsym=foo2=und 2>&1 | FileCheck %s -check-prefix=ERR
-# ERR: error: -defsym:1: symbol not found: und
+# RUN: not ld.lld -o %t %t.o --defsym=foo2=und 2>&1 | FileCheck %s -check-prefix=ERR1
+# ERR1: error: -defsym:1: symbol not found: und
+
+# RUN: not ld.lld -o %t %t.o --defsym=xxx=yyy,zzz 2>&1 | FileCheck %s -check-prefix=ERR2
+# ERR2: -defsym:1: EOF expected, but got ,
 
 .globl foo1
  foo1 = 0x123
diff --git a/test/ELF/discard-locals.s b/test/ELF/discard-locals.s
index 9deaccf..50cf5a0 100644
--- a/test/ELF/discard-locals.s
+++ b/test/ELF/discard-locals.s
@@ -1,7 +1,7 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux -save-temp-labels %s -o %t
 // RUN: ld.lld -discard-locals %t -o %t2
 // RUN: llvm-readobj -s -sd -t %t2 | FileCheck %s
-// REQUIRES: x86
 
 .global _start
 _start:
diff --git a/test/ELF/discard-merge-locals.s b/test/ELF/discard-merge-locals.s
index 01b4d33..20be35c 100644
--- a/test/ELF/discard-merge-locals.s
+++ b/test/ELF/discard-merge-locals.s
@@ -1,7 +1,7 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
 // RUN: ld.lld %t -o %t2 -shared
 // RUN: llvm-readobj -t %t2 | FileCheck %s
-// REQUIRES: x86
 
 	leaq	.L.str(%rip), %rdi
 
diff --git a/test/ELF/discard-none.s b/test/ELF/discard-none.s
index 89e06fd..4a42639 100644
--- a/test/ELF/discard-none.s
+++ b/test/ELF/discard-none.s
@@ -1,7 +1,7 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux -save-temp-labels %s -o %t
 // RUN: ld.lld -discard-none -shared %t -o %t2
 // RUN: llvm-readobj -s -sd -t %t2 | FileCheck %s
-// REQUIRES: x86
 
 .text
 .Lmyvar:
diff --git a/test/ELF/dont-export-hidden.s b/test/ELF/dont-export-hidden.s
index 8088c8d..161e342 100644
--- a/test/ELF/dont-export-hidden.s
+++ b/test/ELF/dont-export-hidden.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc %p/Inputs/shared.s -o %t.o -filetype=obj -triple=x86_64-pc-linux
 // RUN: llvm-mc %s -o %t2.o -filetype=obj -triple=x86_64-pc-linux
 // RUN: ld.lld %t.o -o %t.so -shared
diff --git a/test/ELF/driver-access.test b/test/ELF/driver-access.test
index 46b87c1..da8fa6c 100644
--- a/test/ELF/driver-access.test
+++ b/test/ELF/driver-access.test
@@ -1,4 +1,4 @@
-# REQUIRES: x86, shell
+# REQUIRES: x86
 # Make sure that LLD works even if the current directory is not writable.
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
diff --git a/test/ELF/driver.test b/test/ELF/driver.test
index ac324cb..20bc795 100644
--- a/test/ELF/driver.test
+++ b/test/ELF/driver.test
@@ -35,25 +35,35 @@
 # RUN: not ld.lld -r --gc-sections %t -o %tfail 2>&1 | FileCheck -check-prefix=ERR3 %s
 # ERR3: -r and --gc-sections may not be used together
 
+## Attempt to use -r and --gdb-index together
+# RUN: not ld.lld -r --gdb-index %t -o %tfail 2>&1 | FileCheck -check-prefix=ERR4 %s
+# ERR4: -r and --gdb-index may not be used together
+
 ## Attempt to use -r and --icf together
-# RUN: not ld.lld -r --icf=all %t -o %tfail 2>&1 | FileCheck -check-prefix=ERR4 %s
-# ERR4: -r and --icf may not be used together
+# RUN: not ld.lld -r --icf=all %t -o %tfail 2>&1 | FileCheck -check-prefix=ERR5 %s
+# ERR5: -r and --icf may not be used together
 
 ## Attempt to use -r and -pie together
-# RUN: not ld.lld -r -pie %t -o %tfail 2>&1 | FileCheck -check-prefix=ERR5 %s
-# ERR5: -r and -pie may not be used together
+# RUN: not ld.lld -r -pie %t -o %tfail 2>&1 | FileCheck -check-prefix=ERR6 %s
+# ERR6: -r and -pie may not be used together
 
 ## Attempt to use -shared and -pie together
-# RUN: not ld.lld -shared -pie %t -o %tfail 2>&1 | FileCheck -check-prefix=ERR6 %s
-# ERR6: -shared and -pie may not be used together
+# RUN: not ld.lld -shared -pie %t -o %tfail 2>&1 | FileCheck -check-prefix=ERR7 %s
+# ERR7: -shared and -pie may not be used together
 
 ## "--output=foo" is equivalent to "-o foo".
-# RUN: not ld.lld %t --output=/no/such/file 2>&1 | FileCheck -check-prefix=ERR7 %s
-# ERR7: cannot open output file /no/such/file
+# RUN: not ld.lld %t --output=/no/such/file 2>&1 | FileCheck -check-prefix=ERR8 %s
+# ERR8: cannot open output file /no/such/file
 
 ## "-output=foo" is equivalent to "-o utput=foo".
-# RUN: not ld.lld %t -output=/no/such/file 2>&1 | FileCheck -check-prefix=ERR8 %s
-# ERR8: cannot open output file utput=/no/such/file
+# RUN: not ld.lld %t -output=/no/such/file 2>&1 | FileCheck -check-prefix=ERR9 %s
+# ERR9: cannot open output file utput=/no/such/file
+
+# RUN: not ld.lld %t -z foo 2>&1 | FileCheck -check-prefix=ERR10 %s
+# ERR10: unknown -z value: foo
+
+# RUN: not ld.lld %t -z max-page-size 2>&1 | FileCheck -check-prefix=ERR11 %s
+# ERR11: unknown -z value: max-page-size
 
 .globl _start
 _start:
diff --git a/test/ELF/dt_flags.s b/test/ELF/dt_flags.s
index dffaec8..e160e06 100644
--- a/test/ELF/dt_flags.s
+++ b/test/ELF/dt_flags.s
@@ -3,7 +3,7 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 # RUN: ld.lld -shared %t -o %t.so
 
-# RUN: ld.lld -z now -z nodelete -z nodlopen -z origin -Bsymbolic %t %t.so -o %t1
+# RUN: ld.lld -z initfirst -z now -z nodelete -z nodlopen -z origin -Bsymbolic %t %t.so -o %t1
 # RUN: llvm-readobj -dynamic-table %t1 | FileCheck -check-prefix=FLAGS %s
 
 # RUN: ld.lld %t %t.so -o %t2
@@ -14,12 +14,12 @@
 
 # FLAGS: DynamicSection [
 # FLAGS:   0x000000000000001E FLAGS ORIGIN SYMBOLIC BIND_NOW
-# FLAGS:   0x000000006FFFFFFB FLAGS_1 NOW NODELETE NOOPEN ORIGIN
+# FLAGS:   0x000000006FFFFFFB FLAGS_1 NOW NODELETE INITFIRST NOOPEN ORIGIN
 # FLAGS: ]
 
 # CHECK: DynamicSection [
-# CHECK-NOT:   0x000000000000001E FLAGS ORIGIN SYMBOLIC BIND_NOW
-# CHECK-NOT:   0x000000006FFFFFFB FLAGS_1 NOW NODELETE NOOPEN ORIGIN
+# CHECK-NOT:   FLAGS
+# CHECK-NOT:   FLAGS_1
 # CHECK: ]
 
 .globl _start
diff --git a/test/ELF/duplicated-synthetic-sym.s b/test/ELF/duplicated-synthetic-sym.s
index 095f581..bc4f5cf 100644
--- a/test/ELF/duplicated-synthetic-sym.s
+++ b/test/ELF/duplicated-synthetic-sym.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: rm -rf %t.dir
 // RUN: mkdir %t.dir
diff --git a/test/ELF/dynamic-list-locals.s b/test/ELF/dynamic-list-locals.s
new file mode 100644
index 0000000..36ac849
--- /dev/null
+++ b/test/ELF/dynamic-list-locals.s
@@ -0,0 +1,7 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "{ local: *; };" > %t.list
+# RUN: not ld.lld -dynamic-list %t.list -shared %t.o -o %t.so 2>&1 | FileCheck %s
+
+# CHECK: error: {{.*}}:1: "local:" scope not supported in --dynamic-list
diff --git a/test/ELF/dynamic-list-unexpected-end.s b/test/ELF/dynamic-list-unexpected-end.s
new file mode 100644
index 0000000..f485a66
--- /dev/null
+++ b/test/ELF/dynamic-list-unexpected-end.s
@@ -0,0 +1,7 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "{ }; foo;" > %t.list
+# RUN: not ld.lld -dynamic-list %t.list -shared %t.o -o %t.so 2>&1 | FileCheck %s
+
+# CHECK: error: {{.*}}:1: EOF expected, but got foo
diff --git a/test/ELF/dynamic-no-rosegment.s b/test/ELF/dynamic-no-rosegment.s
index b65e37d..f2b5f35 100644
--- a/test/ELF/dynamic-no-rosegment.s
+++ b/test/ELF/dynamic-no-rosegment.s
@@ -7,9 +7,9 @@
 # CHECK-NEXT:   Tag                Type                 Name/Value
 # CHECK-NEXT:   0x0000000000000006 SYMTAB               0x120
 # CHECK-NEXT:   0x000000000000000B SYMENT               24 (bytes)
-# CHECK-NEXT:   0x0000000000000005 STRTAB               0x138
+# CHECK-NEXT:   0x0000000000000005 STRTAB               0x1D8
 # CHECK-NEXT:   0x000000000000000A STRSZ                1 (bytes)
-# CHECK-NEXT:   0x000000006FFFFEF5 GNU_HASH             0x140
-# CHECK-NEXT:   0x0000000000000004 HASH                 0x15C
+# CHECK-NEXT:   0x000000006FFFFEF5 GNU_HASH             0x138
+# CHECK-NEXT:   0x0000000000000004 HASH                 0x154
 # CHECK-NEXT:   0x0000000000000000 NULL                 0x0
 # CHECK-NEXT: ]
diff --git a/test/ELF/dynamic-reloc-in-ro.s b/test/ELF/dynamic-reloc-in-ro.s
index 4dee4f6..920f1d5 100644
--- a/test/ELF/dynamic-reloc-in-ro.s
+++ b/test/ELF/dynamic-reloc-in-ro.s
@@ -1,6 +1,6 @@
 // REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-// RUN: not ld.lld %t.o -o %t.so -shared 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.o -o /dev/null -shared 2>&1 | FileCheck %s
 
 // CHECK:      can't create dynamic relocation R_X86_64_64 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
 // CHECK-NEXT: >>> defined in {{.*}}.o
diff --git a/test/ELF/dynamic-reloc-index.s b/test/ELF/dynamic-reloc-index.s
index 47e8c6c..0c14d34 100644
--- a/test/ELF/dynamic-reloc-index.s
+++ b/test/ELF/dynamic-reloc-index.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/shared.s -o %t2.o
 // RUN: ld.lld -shared %t2.o -o %t2.so
diff --git a/test/ELF/dynamic-reloc-weak.s b/test/ELF/dynamic-reloc-weak.s
index b4da2e5..8e4840b 100644
--- a/test/ELF/dynamic-reloc-weak.s
+++ b/test/ELF/dynamic-reloc-weak.s
@@ -1,9 +1,9 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/dynamic-reloc-weak.s -o %t2.o
 // RUN: ld.lld -shared %t2.o -o %t2.so
 // RUN: ld.lld %t.o %t2.so -o %t
 // RUN: llvm-readobj -r  %t | FileCheck %s
-// REQUIRES: x86
 
         .globl _start
 _start:
diff --git a/test/ELF/dynamic-reloc.s b/test/ELF/dynamic-reloc.s
index 4d95e41..3a957ac 100644
--- a/test/ELF/dynamic-reloc.s
+++ b/test/ELF/dynamic-reloc.s
@@ -1,10 +1,10 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/shared.s -o %t2.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/dynamic-reloc.s -o %t3.o
 // RUN: ld.lld -shared %t2.o -o %t2.so
 // RUN: ld.lld --hash-style=sysv %t.o %t3.o %t2.so -o %t
 // RUN: llvm-readobj -dynamic-table -r --expand-relocs -s %t | FileCheck %s
-// REQUIRES: x86
 
 // CHECK:      Index: 1
 // CHECK-NEXT: Name: .dynsym
diff --git a/test/ELF/dynsec-at-beginning.s b/test/ELF/dynsec-at-beginning.s
deleted file mode 100644
index 90504c9..0000000
--- a/test/ELF/dynsec-at-beginning.s
+++ /dev/null
@@ -1,16 +0,0 @@
-# REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
-
-# RUN: ld.lld --hash-style=gnu -o %t1  %t -shared
-# RUN: llvm-readobj -elf-output-style=GNU -s %t1 | FileCheck %s
-
-# Dynamic symbol and dynamic strtab sections are at the beginning of
-# SHF_ALLOC sections.
-# CHECK:      .dynsym  {{.*}}   A
-# CHECK-NEXT: .dynstr  {{.*}}   A
-# CHECK-NEXT: foo      {{.*}}   A
-# CHECK-NEXT: .hash    {{.*}}   A
-# CHECK-NEXT: .text    {{.*}}   AX
-
-.section foo, "a"
-.byte 0
diff --git a/test/ELF/dynstr-no-rosegment.s b/test/ELF/dynstr-no-rosegment.s
index 0e12721..bad6300 100644
--- a/test/ELF/dynstr-no-rosegment.s
+++ b/test/ELF/dynstr-no-rosegment.s
@@ -1,6 +1,6 @@
+# REQUIRES: x86
 # Verify that a .dynstr in the .text segment has null byte terminators
 
-# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 # RUN: ld.lld %t.o -no-rosegment -o %t.so -shared
 # RUN: llvm-objdump %t.so -s -j .dynstr | FileCheck %s
diff --git a/test/ELF/edata-etext.s b/test/ELF/edata-etext.s
index 2358399..52070cb 100644
--- a/test/ELF/edata-etext.s
+++ b/test/ELF/edata-etext.s
@@ -10,10 +10,11 @@
 ##    greater than the address of _etext, the address of _end is same as the address
 ##    of _edata." (https://docs.oracle.com/cd/E53394_01/html/E54766/u-etext-3c.html).
 ## 3) Address of _end is different from _edata because of 2.
+## 4) Addresses of _edata == edata, _end == end and _etext == etext.
 # CHECK:      Sections:
 # CHECK-NEXT:  Idx Name          Size      Address          Type
 # CHECK-NEXT:    0               00000000 0000000000000000
-# CHECK-NEXT:    1 .text         00000001 0000000000201000 TEXT DATA
+# CHECK-NEXT:    1 .text         00000001 0000000000201000 TEXT
 # CHECK-NEXT:    2 .data         00000002 0000000000202000 DATA
 # CHECK-NEXT:    3 .bss          00000006 0000000000202004 BSS
 # CHECK:      SYMBOL TABLE:
@@ -22,6 +23,9 @@
 # CHECK-NEXT:  000000000020200a         .bss  00000000 _end
 # CHECK-NEXT:  0000000000201001         .text 00000000 _etext
 # CHECK-NEXT:  0000000000201000         .text 00000000 _start
+# CHECK-NEXT:  0000000000202002         .data 00000000 edata
+# CHECK-NEXT:  000000000020200a         .bss  00000000 end
+# CHECK-NEXT:  0000000000201001         .text 00000000 etext
 
 # RUN: ld.lld -r %t.o -o %t2
 # RUN: llvm-objdump -t %t2 | FileCheck %s --check-prefix=RELOCATABLE
@@ -29,7 +33,7 @@
 # RELOCATABLE-NEXT:  0000000000000000 *UND* 00000000 _end
 # RELOCATABLE-NEXT:  0000000000000000 *UND* 00000000 _etext
 
-.global _start,_end,_etext,_edata
+.global _start,_end,_etext,_edata,end,etext,edata
 .text
 _start:
   nop
diff --git a/test/ELF/eh-frame-dyn-rel.s b/test/ELF/eh-frame-dyn-rel.s
index a244ac6..f54e62b 100644
--- a/test/ELF/eh-frame-dyn-rel.s
+++ b/test/ELF/eh-frame-dyn-rel.s
@@ -1,6 +1,6 @@
 // REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-// RUN: not ld.lld %t.o %t.o -o %t -shared 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.o %t.o -o /dev/null -shared 2>&1 | FileCheck %s
 
 // CHECK: can't create dynamic relocation R_X86_64_64 against symbol: foo in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
 // CHECK: >>> defined in {{.*}}.o
diff --git a/test/ELF/eh-frame-hdr-abs-fde.s b/test/ELF/eh-frame-hdr-abs-fde.s
index c3dc862..7f75058 100644
--- a/test/ELF/eh-frame-hdr-abs-fde.s
+++ b/test/ELF/eh-frame-hdr-abs-fde.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check reading PC values of FDEs and writing lookup table in the .eh_frame_hdr
 # if CIE augmentation string has 'L' token and PC values are encoded using
 # absolute (not relative) format.
@@ -6,8 +7,6 @@
 # RUN: ld.lld --eh-frame-hdr %t.o -o %t
 # RUN: llvm-objdump -s -dwarf=frames %t | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:      Contents of section .eh_frame_hdr:
 # CHECK-NEXT:  10128 011b033b 00000010 00000001 0000fed8
 #                                               ^-- 0x20000 - 0x10138
diff --git a/test/ELF/eh-frame-marker.s b/test/ELF/eh-frame-marker.s
index 30bac46..f5696bd 100644
--- a/test/ELF/eh-frame-marker.s
+++ b/test/ELF/eh-frame-marker.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
 // RUN: ld.lld --eh-frame-hdr %t.o -o %t.so -shared
 // RUN: llvm-readobj -t -s %t.so | FileCheck %s
diff --git a/test/ELF/eh-frame-multilpe-cie.s b/test/ELF/eh-frame-multilpe-cie.s
index 12781ff..a52e686 100644
--- a/test/ELF/eh-frame-multilpe-cie.s
+++ b/test/ELF/eh-frame-multilpe-cie.s
@@ -1,5 +1,6 @@
+// REQUIRES: x86
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
-// RUN: ld.lld --eh-frame-hdr %t.o -o %t.so -shared
+// RUN: ld.lld --eh-frame-hdr %t.o -o /dev/null -shared
 // We would fail to parse multiple cies in the same file.
 
         .cfi_startproc
diff --git a/test/ELF/eh-frame-negative-pcrel-sdata2.s b/test/ELF/eh-frame-negative-pcrel-sdata2.s
new file mode 100644
index 0000000..30ee856
--- /dev/null
+++ b/test/ELF/eh-frame-negative-pcrel-sdata2.s
@@ -0,0 +1,85 @@
+# REQUIRES: x86
+
+# Test handling of FDE pc negative relative addressing with DW_EH_PE_sdata2.
+# This situation can arise when .eh_frame is placed after .text.
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "SECTIONS { .text : { *(.text) } .eh_frame : { *(.eh_frame) } }" > %t.script
+# RUN: ld.lld --eh-frame-hdr --script %t.script --section-start .text=0x1000 %t.o -o %t
+# RUN: llvm-readobj -s -section-data %t | FileCheck %s
+
+# CHECK:      Section {
+# CHECK:        Index:
+# CHECK:        Name: .eh_frame
+# CHECK-NEXT:   Type: SHT_PROGBITS
+# CHECK-NEXT:   Flags [
+# CHECK-NEXT:     SHF_ALLOC
+# CHECK-NEXT:   ]
+# CHECK-NEXT:   Address: 0x1001
+# CHECK-NEXT:   Offset: 0x1001
+# CHECK-NEXT:   Size:
+# CHECK-NEXT:   Link:
+# CHECK-NEXT:   Info:
+# CHECK-NEXT:   AddressAlignment:
+# CHECK-NEXT:   EntrySize:
+# CHECK-NEXT:   SectionData (
+# CHECK-NEXT:     0000: 14000000 00000000 017A5200 01010101
+# CHECK-NEXT:     0010: 1A000000 00000000 0C000000 1C000000
+# CHECK-NEXT:     0020: DFFFFFFF
+#                       ^
+#   DFFFFFFF = _start(0x1000) - PC(.eh_frame(0x1001) + 0x20)
+
+# CHECK:      Section {
+# CHECK:        Index:
+# CHECK:        Name: .eh_frame_hdr
+# CHECK-NEXT:   Type: SHT_PROGBITS
+# CHECK-NEXT:   Flags [
+# CHECK-NEXT:     SHF_ALLOC
+# CHECK-NEXT:   ]
+# CHECK-NEXT:   Address: 0x1030
+# CHECK-NEXT:   Offset: 0x1030
+# CHECK-NEXT:   Size: 20
+# CHECK-NEXT:   Link: 0
+# CHECK-NEXT:   Info: 0
+# CHECK-NEXT:   AddressAlignment: 4
+# CHECK-NEXT:   EntrySize: 0
+# CHECK-NEXT:   SectionData (
+# CHECK-NEXT:     0000: 011B033B CDFFFFFF 01000000 D0FFFFFF
+# CHECK-NEXT:     0010: E9FFFFFF
+#   Header (always 4 bytes): 011B033B
+#   CDFFFFFF = .eh_frame(0x1001) - .eh_frame_hdr(0x1030) - 4
+#   01000000 = 1 = the number of FDE pointers in the table.
+#   D0FFFFFF = _start(0x1000) - .eh_frame_hdr(0x1030)
+#   E9FFFFFF = FDE(.eh_frame(0x1001) + 0x18) - .eh_frame_hdr(0x1030)
+
+.text
+.global _start
+_start:
+ nop
+
+.section .eh_frame, "a"
+  .long 16   # Size
+  .long 0x00 # ID
+  .byte 0x01 # Version.
+
+  .byte 0x7A # Augmentation string: "zR"
+  .byte 0x52
+  .byte 0x00
+
+  .byte 0x01
+
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x01 # LEB128
+  .byte 0x1A # DW_EH_PE_pcrel | DW_EH_PE_sdata2
+
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+
+  .long 10   # Size
+  .long 24   # ID
+fde:
+  .long _start - fde
+  .word 0
diff --git a/test/ELF/eh-frame-negative-pcrel-sdata4.s b/test/ELF/eh-frame-negative-pcrel-sdata4.s
new file mode 100644
index 0000000..71b91cf
--- /dev/null
+++ b/test/ELF/eh-frame-negative-pcrel-sdata4.s
@@ -0,0 +1,85 @@
+# REQUIRES: x86
+
+# Test handling of FDE pc negative relative addressing with DW_EH_PE_sdata4.
+# This situation can arise when .eh_frame is placed after .text.
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "SECTIONS { .text : { *(.text) } .eh_frame : { *(.eh_frame) } }" > %t.script
+# RUN: ld.lld --eh-frame-hdr --script %t.script --section-start .text=0x1000 %t.o -o %t
+# RUN: llvm-readobj -s -section-data %t | FileCheck %s
+
+# CHECK:      Section {
+# CHECK:        Index:
+# CHECK:        Name: .eh_frame
+# CHECK-NEXT:   Type: SHT_PROGBITS
+# CHECK-NEXT:   Flags [
+# CHECK-NEXT:     SHF_ALLOC
+# CHECK-NEXT:   ]
+# CHECK-NEXT:   Address: 0x1001
+# CHECK-NEXT:   Offset: 0x1001
+# CHECK-NEXT:   Size:
+# CHECK-NEXT:   Link:
+# CHECK-NEXT:   Info:
+# CHECK-NEXT:   AddressAlignment:
+# CHECK-NEXT:   EntrySize:
+# CHECK-NEXT:   SectionData (
+# CHECK-NEXT:     0000: 14000000 00000000 017A5200 01010101
+# CHECK-NEXT:     0010: 1B000000 00000000 0C000000 1C000000
+# CHECK-NEXT:     0020: DFFFFFFF
+#                       ^
+#   DFFFFFFF = _start(0x1000) - PC(.eh_frame(0x1001) + 0x20)
+
+# CHECK:      Section {
+# CHECK:        Index:
+# CHECK:        Name: .eh_frame_hdr
+# CHECK-NEXT:   Type: SHT_PROGBITS
+# CHECK-NEXT:   Flags [
+# CHECK-NEXT:     SHF_ALLOC
+# CHECK-NEXT:   ]
+# CHECK-NEXT:   Address: 0x1030
+# CHECK-NEXT:   Offset: 0x1030
+# CHECK-NEXT:   Size: 20
+# CHECK-NEXT:   Link: 0
+# CHECK-NEXT:   Info: 0
+# CHECK-NEXT:   AddressAlignment: 4
+# CHECK-NEXT:   EntrySize: 0
+# CHECK-NEXT:   SectionData (
+# CHECK-NEXT:     0000: 011B033B CDFFFFFF 01000000 D0FFFFFF
+# CHECK-NEXT:     0010: E9FFFFFF
+#   Header (always 4 bytes): 011B033B
+#   CDFFFFFF = .eh_frame(0x1001) - .eh_frame_hdr(0x1030) - 4
+#   01000000 = 1 = the number of FDE pointers in the table.
+#   D0FFFFFF = _start(0x1000) - .eh_frame_hdr(0x1030)
+#   E9FFFFFF = FDE(.eh_frame(0x1001) + 0x18) - .eh_frame_hdr(0x1030)
+
+.text
+.global _start
+_start:
+ nop
+
+.section .eh_frame, "a"
+  .long 16   # Size
+  .long 0x00 # ID
+  .byte 0x01 # Version.
+
+  .byte 0x7A # Augmentation string: "zR"
+  .byte 0x52
+  .byte 0x00
+
+  .byte 0x01
+
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x01 # LEB128
+  .byte 0x1B # DW_EH_PE_pcrel | DW_EH_PE_sdata4
+
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+
+  .long 12   # Size
+  .long 24   # ID
+fde:
+  .long _start - fde
+  .long 0
diff --git a/test/ELF/eh-frame-negative-pcrel-sdata8.s b/test/ELF/eh-frame-negative-pcrel-sdata8.s
new file mode 100644
index 0000000..2c6c9f2
--- /dev/null
+++ b/test/ELF/eh-frame-negative-pcrel-sdata8.s
@@ -0,0 +1,85 @@
+# REQUIRES: x86
+
+# Test handling of FDE pc negative relative addressing with DW_EH_PE_sdata8.
+# This situation can arise when .eh_frame is placed after .text.
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "SECTIONS { .text : { *(.text) } .eh_frame : { *(.eh_frame) } }" > %t.script
+# RUN: ld.lld --eh-frame-hdr --script %t.script --section-start .text=0x1000 %t.o -o %t
+# RUN: llvm-readobj -s -section-data %t | FileCheck %s
+
+# CHECK:      Section {
+# CHECK:        Index:
+# CHECK:        Name: .eh_frame
+# CHECK-NEXT:   Type: SHT_PROGBITS
+# CHECK-NEXT:   Flags [
+# CHECK-NEXT:     SHF_ALLOC
+# CHECK-NEXT:   ]
+# CHECK-NEXT:   Address: 0x1001
+# CHECK-NEXT:   Offset: 0x1001
+# CHECK-NEXT:   Size:
+# CHECK-NEXT:   Link:
+# CHECK-NEXT:   Info:
+# CHECK-NEXT:   AddressAlignment:
+# CHECK-NEXT:   EntrySize:
+# CHECK-NEXT:   SectionData (
+# CHECK-NEXT:     0000: 14000000 00000000 017A5200 01010101
+# CHECK-NEXT:     0010: 1C000000 00000000 14000000 1C000000
+# CHECK-NEXT:     0020: DFFFFFFF FFFFFFFF
+#                       ^
+#   DFFFFFFF FFFFFFFF = _start(0x1000) - PC(.eh_frame(0x1001) + 0x20)
+
+# CHECK:      Section {
+# CHECK:        Index:
+# CHECK:        Name: .eh_frame_hdr
+# CHECK-NEXT:   Type: SHT_PROGBITS
+# CHECK-NEXT:   Flags [
+# CHECK-NEXT:     SHF_ALLOC
+# CHECK-NEXT:   ]
+# CHECK-NEXT:   Address: 0x1038
+# CHECK-NEXT:   Offset: 0x1038
+# CHECK-NEXT:   Size: 20
+# CHECK-NEXT:   Link: 0
+# CHECK-NEXT:   Info: 0
+# CHECK-NEXT:   AddressAlignment: 4
+# CHECK-NEXT:   EntrySize: 0
+# CHECK-NEXT:   SectionData (
+# CHECK-NEXT:     0000: 011B033B C5FFFFFF 01000000 C8FFFFFF
+# CHECK-NEXT:     0010: E1FFFFFF
+#   Header (always 4 bytes): 011B033B
+#   C5FFFFFF = .eh_frame(0x1001) - .eh_frame_hdr(0x1038) - 4
+#   01000000 = 1 = the number of FDE pointers in the table.
+#   C8FFFFFF = _start(0x1000) - .eh_frame_hdr(0x1038)
+#   E1FFFFFF = FDE(.eh_frame(0x1001) + 0x18) - .eh_frame_hdr(0x1038)
+
+.text
+.global _start
+_start:
+ nop
+
+.section .eh_frame, "a"
+  .long 16   # Size
+  .long 0x00 # ID
+  .byte 0x01 # Version.
+
+  .byte 0x7A # Augmentation string: "zR"
+  .byte 0x52
+  .byte 0x00
+
+  .byte 0x01
+
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x01 # LEB128
+  .byte 0x1C # DW_EH_PE_pcrel | DW_EH_PE_sdata8
+
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+
+  .long 16   # Size
+  .long 24   # ID
+fde:
+  .quad _start - fde
+  .long 0
diff --git a/test/ELF/eh-frame-padding-no-rosegment.s b/test/ELF/eh-frame-padding-no-rosegment.s
index 6805de1..222ce0d 100644
--- a/test/ELF/eh-frame-padding-no-rosegment.s
+++ b/test/ELF/eh-frame-padding-no-rosegment.s
@@ -7,6 +7,7 @@
 .global bar
 .hidden bar
 bar:
+  ret
 
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 
@@ -37,8 +38,7 @@
 // RUN: llvm-readobj -l --elf-output-style=GNU %t | FileCheck --check-prefix=PHDR %s
 
 // PHDR: Segment Sections
-// PHDR: .text
-// PHDR-SAME: .eh_frame
+// PHDR: .eh_frame {{.*}}.text
 
 // Check that the CIE and FDE are padded with 0x00 and not 0xCC when the
 // .eh_frame section is placed in the executable segment
@@ -58,7 +58,7 @@
 // CHECK-NEXT: EntrySize:
 // CHECK-NEXT: SectionData (
 // CHECK-NEXT:   0000: 1C000000 00000000 017A5052 00017810
-// CHECK-NEXT:   0010: 061BDAFF FFFF1B0C 07089001 00000000
-// CHECK-NEXT:   0020: 14000000 24000000 C4FFFFFF 00000000
+// CHECK-NEXT:   0010: 061B2A00 00001B0C 07089001 00000000
+// CHECK-NEXT:   0020: 14000000 24000000 14000000 00000000
 // CHECK-NEXT:   0030: 00000000 00000000
 // CHECK-NEXT: )
diff --git a/test/ELF/eh-frame-pcrel-overflow.s b/test/ELF/eh-frame-pcrel-overflow.s
new file mode 100644
index 0000000..9b88eda
--- /dev/null
+++ b/test/ELF/eh-frame-pcrel-overflow.s
@@ -0,0 +1,33 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/eh-frame-pcrel-overflow.s -o %t1.o
+# RUN: ld.lld --eh-frame-hdr -Ttext=0x90000000 %t.o -o /dev/null
+# RUN: not ld.lld --eh-frame-hdr %t.o %t1.o -o /dev/null 2>&1 | FileCheck %s
+# CHECK: error: {{.*}}.o:(.eh_frame): PC offset is too large: 0x90000eac
+
+.text
+.global _start
+_start:
+  ret
+
+.section .eh_frame, "a"
+  .long 12   # Size
+  .long 0x00 # ID
+  .byte 0x01 # Version.
+
+  .byte 0x52 # Augmentation string: 'R','\0'
+  .byte 0x00
+
+  .byte 0x01
+
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x00 # DW_EH_PE_absptr
+
+  .byte 0xFF
+
+  .long 12  # Size
+  .long 0x14 # ID
+  .quad _start + 0x70000000
diff --git a/test/ELF/eh-frame-rel.s b/test/ELF/eh-frame-rel.s
index a417dc1..d633e6a 100644
--- a/test/ELF/eh-frame-rel.s
+++ b/test/ELF/eh-frame-rel.s
@@ -1,6 +1,6 @@
 // REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=i686-pc-linux %s -o %t.o
-// RUN: ld.lld %t.o %t.o -o %t -shared
+// RUN: ld.lld %t.o %t.o -o /dev/null -shared
 // We used to try to read the relocations as RELA and error out
 
 	.cfi_startproc
diff --git a/test/ELF/eh-frame-value-format1.s b/test/ELF/eh-frame-value-format1.s
new file mode 100644
index 0000000..a8bcffb
--- /dev/null
+++ b/test/ELF/eh-frame-value-format1.s
@@ -0,0 +1,35 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: ld.lld --eh-frame-hdr %t -o /dev/null
+
+.section .eh_frame
+  .byte 0x14
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x01
+  
+  .byte 0x50 # Augmentation string: 'P','\0'
+  .byte 0x00
+  
+  .byte 0x01
+  
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x04 # DW_EH_PE_udata8
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  
+  .byte 0xFF
diff --git a/test/ELF/eh-frame-value-format2.s b/test/ELF/eh-frame-value-format2.s
new file mode 100644
index 0000000..6d2d82e
--- /dev/null
+++ b/test/ELF/eh-frame-value-format2.s
@@ -0,0 +1,35 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: ld.lld --eh-frame-hdr %t -o /dev/null
+
+.section .eh_frame
+  .byte 0x14
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x01
+  
+  .byte 0x50 # Augmentation string: 'P','\0'
+  .byte 0x00
+  
+  .byte 0x01
+  
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x0C # DW_EH_PE_sdata8
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  
+  .byte 0xFF
diff --git a/test/ELF/eh-frame-value-format3.s b/test/ELF/eh-frame-value-format3.s
new file mode 100644
index 0000000..1f174ae
--- /dev/null
+++ b/test/ELF/eh-frame-value-format3.s
@@ -0,0 +1,28 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: ld.lld --eh-frame-hdr %t -o /dev/null
+
+.section .eh_frame
+  .byte 0x0E
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x01
+  
+  .byte 0x50 # Augmentation string: 'P','\0'
+  .byte 0x00
+  
+  .byte 0x01
+  
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x0A # DW_EH_PE_sdata2
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
diff --git a/test/ELF/eh-frame-value-format4.s b/test/ELF/eh-frame-value-format4.s
new file mode 100644
index 0000000..d10988a
--- /dev/null
+++ b/test/ELF/eh-frame-value-format4.s
@@ -0,0 +1,28 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: ld.lld --eh-frame-hdr %t -o /dev/null
+
+.section .eh_frame
+  .byte 0x0E
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x01
+  
+  .byte 0x50 # Augmentation string: 'P','\0'
+  .byte 0x00
+  
+  .byte 0x01
+  
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x02 # DW_EH_PE_udata2
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
diff --git a/test/ELF/eh-frame-value-format5.s b/test/ELF/eh-frame-value-format5.s
new file mode 100644
index 0000000..fd5b35a
--- /dev/null
+++ b/test/ELF/eh-frame-value-format5.s
@@ -0,0 +1,35 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: ld.lld --eh-frame-hdr %t -o /dev/null
+
+.section .eh_frame
+  .byte 0x14
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x01
+  
+  .byte 0x50 # Augmentation string: 'P','\0'
+  .byte 0x00
+  
+  .byte 0x01
+  
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x08 # DW_EH_PE_signed
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  
+  .byte 0xFF
diff --git a/test/ELF/eh-frame-value-format6.s b/test/ELF/eh-frame-value-format6.s
new file mode 100644
index 0000000..ee76fa7
--- /dev/null
+++ b/test/ELF/eh-frame-value-format6.s
@@ -0,0 +1,35 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: ld.lld --eh-frame-hdr %t -o /dev/null
+
+.section .eh_frame
+  .byte 0x14
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x01
+  
+  .byte 0x50 # Augmentation string: 'P','\0'
+  .byte 0x00
+  
+  .byte 0x01
+  
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x00 # DW_EH_PE_absptr
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  .byte 0xFF
+  
+  .byte 0xFF
diff --git a/test/ELF/eh-frame-value-format7.s b/test/ELF/eh-frame-value-format7.s
new file mode 100644
index 0000000..90543a2
--- /dev/null
+++ b/test/ELF/eh-frame-value-format7.s
@@ -0,0 +1,75 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: ld.lld --eh-frame-hdr --section-start .text=0x1000 %t.o -o %t
+# RUN: llvm-readobj -s -section-data %t | FileCheck %s
+
+## Check we are able to handle DW_EH_PE_udata2 encoding.
+
+# CHECK:      Section {
+# CHECK:        Index:
+# CHECK:        Name: .eh_frame_hdr
+# CHECK-NEXT:   Type: SHT_PROGBITS
+# CHECK-NEXT:   Flags [
+# CHECK-NEXT:     SHF_ALLOC
+# CHECK-NEXT:   ]
+# CHECK-NEXT:   Address: 0x2000
+# CHECK-NEXT:   Offset: 0x2000
+# CHECK-NEXT:   Size: 20
+# CHECK-NEXT:   Link: 0
+# CHECK-NEXT:   Info: 0
+# CHECK-NEXT:   AddressAlignment: 4
+# CHECK-NEXT:   EntrySize: 0
+# CHECK-NEXT:   SectionData (
+# CHECK-NEXT:     0000: 011B033B 10000000 01000000 34F2FFFF
+# CHECK-NEXT:     0010: 24000000
+# Header (always 4 bytes): 011B033B
+#    10000000 = .eh_frame(0x2014) - .eh_frame_hdr(0x2000) - 4
+#    01000000 = 1 = the number of FDE pointers in the table.
+# 34F2FFFF = foo(0x1000) - 0x234(addend) - .eh_frame_hdr(0x2000)
+  
+# CHECK:      Section {
+# CHECK:        Index:
+# CHECK:        Name: .eh_frame
+# CHECK-NEXT:   Type: SHT_PROGBITS
+# CHECK-NEXT:   Flags [
+# CHECK-NEXT:     SHF_ALLOC
+# CHECK-NEXT:   ]
+# CHECK-NEXT:   Address: 0x2014
+# CHECK-NEXT:   Offset: 0x2014
+# CHECK-NEXT:   Size:
+# CHECK-NEXT:   Link:
+# CHECK-NEXT:   Info:
+# CHECK-NEXT:   AddressAlignment:
+# CHECK-NEXT:   EntrySize:
+# CHECK-NEXT:   SectionData (
+# CHECK-NEXT:     0000: 0C000000 00000000 01520001 010102FF
+# CHECK-NEXT:     0010: 0C000000 14000000 34120000 00000000
+#                                           ^
+#                                           ---> ADDR(foo) + 0x234 = 0x1234
+
+.text
+.global foo
+foo:
+ nop
+
+.section .eh_frame
+  .long 12   # Size
+  .long 0x00 # ID
+  .byte 0x01 # Version.
+  
+  .byte 0x52 # Augmentation string: 'R','\0'
+  .byte 0x00
+  
+  .byte 0x01
+  
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x02 # DW_EH_PE_udata2
+
+  .byte 0xFF
+ 
+  .long 0x6  # Size
+  .long 0x14 # ID
+  .short foo + 0x234
diff --git a/test/ELF/eh-frame-value-format8.s b/test/ELF/eh-frame-value-format8.s
new file mode 100644
index 0000000..6032470
--- /dev/null
+++ b/test/ELF/eh-frame-value-format8.s
@@ -0,0 +1,74 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: ld.lld --eh-frame-hdr --section-start .text=0x1000 %t.o -o %t
+# RUN: llvm-readobj -s -section-data %t | FileCheck %s
+
+## Check we are able to handle DW_EH_PE_absptr encoding.
+
+# CHECK:      Section {
+# CHECK:        Index:
+# CHECK:        Name: .eh_frame_hdr
+# CHECK-NEXT:   Type: SHT_PROGBITS
+# CHECK-NEXT:   Flags [
+# CHECK-NEXT:     SHF_ALLOC
+# CHECK-NEXT:   ]
+# CHECK-NEXT:   Address: 0x2000
+# CHECK-NEXT:   Offset: 0x2000
+# CHECK-NEXT:   Size: 20
+# CHECK-NEXT:   Link: 0
+# CHECK-NEXT:   Info: 0
+# CHECK-NEXT:   AddressAlignment: 4
+# CHECK-NEXT:   EntrySize: 0
+# CHECK-NEXT:   SectionData (
+# CHECK-NEXT:     0000: 011B033B 10000000 01000000 34F2FFFF
+# CHECK-NEXT:     0010: 24000000
+# Header (always 4 bytes): 011B033B
+#    10000000 = .eh_frame(0x2014) - .eh_frame_hdr(0x2000) - 4
+#    01000000 = 1 = the number of FDE pointers in the table.
+# 34F2FFFF = foo(0x1000) - 0x234(addend) - .eh_frame_hdr(0x2000)
+  
+# CHECK:      Section {
+# CHECK:        Index:
+# CHECK:        Name: .eh_frame
+# CHECK-NEXT:   Type: SHT_PROGBITS
+# CHECK-NEXT:   Flags [
+# CHECK-NEXT:     SHF_ALLOC
+# CHECK-NEXT:   ]
+# CHECK-NEXT:   Address: 0x2014
+# CHECK-NEXT:   Offset: 0x2014
+# CHECK-NEXT:   Size:
+# CHECK-NEXT:   Link:
+# CHECK-NEXT:   Info:
+# CHECK-NEXT:   AddressAlignment:
+# CHECK-NEXT:   EntrySize:
+# CHECK-NEXT:   SectionData (
+# CHECK-NEXT:     0000: 0C000000 00000000 01520001 010100FF
+# CHECK-NEXT:     0010: 0C000000 14000000 34120000 00000000
+#                                           ^
+#                                           ---> ADDR(foo) + 0x234 = 0x1234
+.text
+.global foo
+foo:
+ nop
+
+.section .eh_frame, "ax"
+  .long 12   # Size
+  .long 0x00 # ID
+  .byte 0x01 # Version.
+  
+  .byte 0x52 # Augmentation string: 'R','\0'
+  .byte 0x00
+  
+  .byte 0x01
+  
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x00 # DW_EH_PE_absptr
+
+  .byte 0xFF
+ 
+  .long 12  # Size
+  .long 0x14 # ID
+  .quad foo + 0x234
diff --git a/test/ELF/eh-frame-value-format9.s b/test/ELF/eh-frame-value-format9.s
new file mode 100644
index 0000000..1b9ce69
--- /dev/null
+++ b/test/ELF/eh-frame-value-format9.s
@@ -0,0 +1,28 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: not ld.lld --eh-frame-hdr %t.o -o %t 2>&1 | FileCheck %s
+# CHECK: error: unknown FDE size encoding
+
+.section .eh_frame, "ax"
+  .long 12   # Size
+  .long 0x00 # ID
+  .byte 0x01 # Version.
+  
+  .byte 0x52 # Augmentation string: 'R','\0'
+  .byte 0x00
+  
+# Code and data alignment factors.
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+# Return address register.
+  .byte 0x01 # LEB128
+
+  .byte 0xFE # 'R' value: invalid <0xFE>
+
+  .byte 0xFF
+
+  .long 12  # Size
+  .long 0x14 # ID
+  .quad .eh_frame
diff --git a/test/ELF/empty-archive.s b/test/ELF/empty-archive.s
index 503dfcc..7223241 100644
--- a/test/ELF/empty-archive.s
+++ b/test/ELF/empty-archive.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-ar rc %t.a
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-// RUN: ld.lld -shared %t.o %t.a -o %t
+// RUN: ld.lld -shared %t.o %t.a -o /dev/null
diff --git a/test/ELF/empty-ver2.s b/test/ELF/empty-ver2.s
new file mode 100644
index 0000000..2aceee1
--- /dev/null
+++ b/test/ELF/empty-ver2.s
@@ -0,0 +1,20 @@
+# REQUIRES: x86
+# RUN: mkdir -p %t.dir
+# RUN: cd %t.dir
+# RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
+# RUN: ld.lld %t.o -o t.so -shared -version-script %p/Inputs/empty-ver.ver
+# RUN: llvm-readobj -version-info t.so | FileCheck %s
+
+# CHECK:       Symbols [
+# CHECK-NEXT:   Symbol {
+# CHECK-NEXT:     Version: 0
+# CHECK-NEXT:     Name: @
+# CHECK-NEXT:   }
+# CHECK-NEXT:   Symbol {
+# CHECK-NEXT:     Version: 1
+# CHECK-NEXT:     Name: bar@@
+# CHECK-NEXT:   }
+# CHECK-NEXT: ]
+
+.global bar@
+bar@:
diff --git a/test/ELF/emulation.s b/test/ELF/emulation.s
index 1a2af0f..98f78b8 100644
--- a/test/ELF/emulation.s
+++ b/test/ELF/emulation.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86,ppc,mips,aarch64
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-freebsd %s -o %tx64
 # RUN: ld.lld -m elf_amd64_fbsd %tx64 -o %t2x64
 # RUN: llvm-readobj -file-headers %t2x64 | FileCheck --check-prefix=AMD64 %s
@@ -391,7 +392,5 @@
 # AARCH64-NEXT:   Flags [ (0x0)
 # AARCH64-NEXT:   ]
 
-# REQUIRES: x86,ppc,mips,aarch64
-
 .globl _start
 _start:
diff --git a/test/ELF/end-preserve.s b/test/ELF/end-preserve.s
index 71d86d1..d251706 100644
--- a/test/ELF/end-preserve.s
+++ b/test/ELF/end-preserve.s
@@ -1,5 +1,5 @@
-// Should preserve the value of the "end" symbol if it is defined.
 // REQUIRES: x86
+// Should preserve the value of the "end" symbol if it is defined.
 
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: ld.lld %t.o -o %t
diff --git a/test/ELF/end-update.s b/test/ELF/end-update.s
index afb137f..9ece23c 100644
--- a/test/ELF/end-update.s
+++ b/test/ELF/end-update.s
@@ -1,5 +1,5 @@
-// Should set the value of the "end" symbol if it is undefined.
 // REQUIRES: x86
+// Should set the value of the "end" symbol if it is undefined.
 
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: ld.lld %t.o -o %t
diff --git a/test/ELF/end.s b/test/ELF/end.s
index f40c5db..530a007 100644
--- a/test/ELF/end.s
+++ b/test/ELF/end.s
@@ -1,5 +1,5 @@
-// Should set the value of the "_end" symbol to the end of the data segment.
 // REQUIRES: x86
+// Should set the value of the "_end" symbol to the end of the data segment.
 
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 
diff --git a/test/ELF/entry.s b/test/ELF/entry.s
index f288bcf..cc7a724 100644
--- a/test/ELF/entry.s
+++ b/test/ELF/entry.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1
 
 # RUN: ld.lld -e foobar %t1 -o %t2 2>&1 | FileCheck -check-prefix=WARN1 %s
diff --git a/test/ELF/exclude-libs.s b/test/ELF/exclude-libs.s
index 68217e4..c061c48 100644
--- a/test/ELF/exclude-libs.s
+++ b/test/ELF/exclude-libs.s
@@ -3,9 +3,10 @@
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
 // RUN:   %p/Inputs/exclude-libs.s -o %t2.o
+// RUN: llvm-as --data-layout=elf %p/Inputs/exclude-libs.ll -o %t3.o
 // RUN: mkdir -p %t.dir
 // RUN: rm -f %t.dir/exc.a
-// RUN: llvm-ar rcs %t.dir/exc.a %t2.o
+// RUN: llvm-ar rcs %t.dir/exc.a %t2.o %t3.o
 
 // RUN: ld.lld -shared %t.o %t.dir/exc.a -o %t.exe
 // RUN: llvm-readobj -dyn-symbols %t.exe | FileCheck --check-prefix=DEFAULT %s
@@ -22,7 +23,7 @@
 // RUN: ld.lld -shared %t.o %t.dir/exc.a -o %t.exe --exclude-libs=ALL
 // RUN: llvm-readobj -dyn-symbols %t.exe | FileCheck --check-prefix=EXCLUDE %s
 
-// RUN: ld.lld -shared %t.o %t2.o %t.dir/exc.a -o %t.exe --exclude-libs=ALL
+// RUN: ld.lld -shared %t.o %t2.o %t3.o %t.dir/exc.a -o %t.exe --exclude-libs=ALL
 // RUN: llvm-readobj -dyn-symbols %t.exe | FileCheck --check-prefix=DEFAULT %s
 
 // RUN: ld.lld -shared --whole-archive %t.o %t.dir/exc.a -o %t.exe --exclude-libs foo,bar,exc.a
@@ -32,10 +33,13 @@
 // RUN: llvm-readobj -dyn-symbols %t.exe | FileCheck --check-prefix=EXCLUDE %s
 
 // DEFAULT: Name: fn
+// DEFAULT: Name: fn2
 // DEFAULT: Name: foo
 // EXCLUDE-NOT: Name: fn
+// EXCLUDE-NOT: Name: fn2
 // EXCLUDE: Name: foo
 
-.globl fn, foo
+.globl fn, fn2, foo
 foo:
   call fn@PLT
+  call fn2@PLT
diff --git a/test/ELF/executable-undefined-protected-ignoreall.s b/test/ELF/executable-undefined-protected-ignoreall.s
index 3791179..967a693 100644
--- a/test/ELF/executable-undefined-protected-ignoreall.s
+++ b/test/ELF/executable-undefined-protected-ignoreall.s
@@ -1,6 +1,6 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
-# RUN: not ld.lld %t -o %tout --unresolved-symbols=ignore-all -pie 2>&1 | FileCheck %s
+# RUN: not ld.lld %t -o /dev/null --unresolved-symbols=ignore-all -pie 2>&1 | FileCheck %s
 # CHECK: error: undefined symbol: foo
 
 .protected foo
diff --git a/test/ELF/execute-only-mixed-data.s b/test/ELF/execute-only-mixed-data.s
new file mode 100644
index 0000000..6ef6236
--- /dev/null
+++ b/test/ELF/execute-only-mixed-data.s
@@ -0,0 +1,26 @@
+// REQUIRES: aarch64
+
+// RUN: llvm-mc -filetype=obj -triple=aarch64-linux-none %s -o %t.o
+
+// RUN: echo "SECTIONS \
+// RUN: { \
+// RUN:  .text : { *(.text) *(.rodata.foo) } \
+// RUN:  .rodata : { *(.rodata.bar) } \
+// RUN: }" > %t.lds
+// RUN: not ld.lld -T%t.lds %t.o -o %t -execute-only 2>&1 | FileCheck %s
+
+// RUN: echo "SECTIONS \
+// RUN: { \
+// RUN:  .text : { *(.text) } \
+// RUN:  .rodata : { *(.rodata.bar) *(.rodata.foo) } \
+// RUN: }" > %t.lds
+// RUN: ld.lld -T%t.lds %t.o -o %t -execute-only 2>&1
+
+// CHECK: -execute-only does not support intermingling data and code
+
+    br lr
+
+.section .rodata.foo
+.word 0x1
+.section .rodata.bar
+.word 0x2
diff --git a/test/ELF/execute-only.s b/test/ELF/execute-only.s
new file mode 100644
index 0000000..7a825cb
--- /dev/null
+++ b/test/ELF/execute-only.s
@@ -0,0 +1,10 @@
+// REQUIRES: aarch64
+
+// RUN: llvm-mc -filetype=obj -triple=aarch64-linux-none %s -o %t.o
+// RUN: ld.lld -Ttext=0xcafe0000 %t.o -o %t.so -shared -execute-only
+// RUN: llvm-readelf -l %t.so | FileCheck %s
+
+// CHECK:      LOAD {{.*}} 0x00000000cafe0000 0x000004 0x000004   E 0x{{.*}}
+// CHECK-NOT:  LOAD {{.*}} 0x00000000cafe0000 0x000004 0x000004 R E 0x{{.*}}
+
+        br lr
diff --git a/test/ELF/fatal-warnings.s b/test/ELF/fatal-warnings.s
index 0bc2a2b..51f2864 100644
--- a/test/ELF/fatal-warnings.s
+++ b/test/ELF/fatal-warnings.s
@@ -2,11 +2,11 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t1.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/warn-common.s -o %t2.o
 
-# RUN: ld.lld --warn-common %t1.o %t2.o -o %t1.out 2>&1 | \
+# RUN: ld.lld --warn-common %t1.o %t2.o -o /dev/null 2>&1 | \
 # RUN:   FileCheck -check-prefix=ERR %s
 # ERR: multiple common of
 
-# RUN: not ld.lld --warn-common --fatal-warnings %t1.o %t2.o -o %t2.out 2>&1 | \
+# RUN: not ld.lld --warn-common --fatal-warnings %t1.o %t2.o -o /dev/null 2>&1 | \
 # RUN:   FileCheck -check-prefix=ERR %s
 
 .globl _start
diff --git a/test/ELF/filter.s b/test/ELF/filter.s
index 4c9104a..2b07c01 100644
--- a/test/ELF/filter.s
+++ b/test/ELF/filter.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 # RUN: ld.lld %t.o -shared -F foo.so -F boo.so -o %t1
 # RUN: llvm-readobj --dynamic-table %t1 | FileCheck %s
@@ -15,5 +16,5 @@
 # CHECK-NEXT: 0x000000007FFFFFFF FILTER        Filter library: [foo.so]
 # CHECK-NEXT: 0x000000007FFFFFFF FILTER        Filter library: [boo.so]
 
-# RUN: not ld.lld %t.o -F x -o %t 2>&1 | FileCheck -check-prefix=ERR %s
+# RUN: not ld.lld %t.o -F x -o /dev/null 2>&1 | FileCheck -check-prefix=ERR %s
 # ERR: -F may not be used without -shared
diff --git a/test/ELF/gc-absolute.s b/test/ELF/gc-absolute.s
index 29e6716..bd0870c 100644
--- a/test/ELF/gc-absolute.s
+++ b/test/ELF/gc-absolute.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
-# RUN: ld.lld %t -o %t2 -shared --gc-sections
+# RUN: ld.lld %t -o /dev/null -shared --gc-sections
 
 .global foo
 foo = 0x123
diff --git a/test/ELF/gc-debuginfo-tls.s b/test/ELF/gc-debuginfo-tls.s
index d1a250b..e23578f 100644
--- a/test/ELF/gc-debuginfo-tls.s
+++ b/test/ELF/gc-debuginfo-tls.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 # RUN: ld.lld %t.o --gc-sections -shared -o %t1
 # RUN: ld.lld %t.o -shared -o %t2
diff --git a/test/ELF/gc-merge-local-sym.s b/test/ELF/gc-merge-local-sym.s
index 9cb4907..b82d0ce 100644
--- a/test/ELF/gc-merge-local-sym.s
+++ b/test/ELF/gc-merge-local-sym.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
 // RUN: ld.lld %t.o -o %t.so -shared -O3 --gc-sections
 // RUN: llvm-readobj -s -section-data -t %t.so | FileCheck %s
@@ -9,7 +10,7 @@
 // CHECK-NEXT:   SHF_MERGE
 // CHECK-NEXT:   SHF_STRINGS
 // CHECK-NEXT: ]
-// CHECK-NEXT: Address: 0x1FD
+// CHECK-NEXT: Address: 0x235
 // CHECK-NEXT: Offset:
 // CHECK-NEXT: Size: 4
 // CHECK-NEXT: Link: 0
diff --git a/test/ELF/gc-sections-local-sym.s b/test/ELF/gc-sections-local-sym.s
index 89121e2..15bca37 100644
--- a/test/ELF/gc-sections-local-sym.s
+++ b/test/ELF/gc-sections-local-sym.s
@@ -1,7 +1,7 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 // RUN: ld.lld %t -o %t2 -shared --gc-sections
 // RUN: llvm-readobj -t -s -section-data %t2 | FileCheck %s
-// REQUIRES: x86
 
 .global foo
 foo:
diff --git a/test/ELF/gc-sections-merge-addend.s b/test/ELF/gc-sections-merge-addend.s
index 8595f58..4dd95b4 100644
--- a/test/ELF/gc-sections-merge-addend.s
+++ b/test/ELF/gc-sections-merge-addend.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
 // RUN: ld.lld %t.o -o %t.so -shared --gc-sections
 // RUN: llvm-readobj -s -section-data %t.so | FileCheck %s
diff --git a/test/ELF/gc-sections-merge-implicit-addend.s b/test/ELF/gc-sections-merge-implicit-addend.s
index 8a7c804..36bb211 100644
--- a/test/ELF/gc-sections-merge-implicit-addend.s
+++ b/test/ELF/gc-sections-merge-implicit-addend.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=i386-pc-linux
 // RUN: ld.lld %t.o -o %t.so -shared --gc-sections
 // RUN: llvm-readobj -s -section-data %t.so | FileCheck %s
diff --git a/test/ELF/gc-sections-merge.s b/test/ELF/gc-sections-merge.s
index ef26886..08dfdaa 100644
--- a/test/ELF/gc-sections-merge.s
+++ b/test/ELF/gc-sections-merge.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
 // RUN: ld.lld %t.o -o %t.so -shared
 // RUN: ld.lld %t.o -o %t.gc.so -shared --gc-sections
diff --git a/test/ELF/gc-sections-metadata-startstop.s b/test/ELF/gc-sections-metadata-startstop.s
index 10c0b54..ede1899 100644
--- a/test/ELF/gc-sections-metadata-startstop.s
+++ b/test/ELF/gc-sections-metadata-startstop.s
@@ -1,5 +1,5 @@
-# LINK_ORDER cnamed sections are not kept alive by the __start_* reference.
 # REQUIRES: x86
+# LINK_ORDER cnamed sections are not kept alive by the __start_* reference.
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 # RUN: ld.lld --gc-sections %t.o -o %t
diff --git a/test/ELF/gc-sections-protected.s b/test/ELF/gc-sections-protected.s
index 9f1efed..28e779b 100644
--- a/test/ELF/gc-sections-protected.s
+++ b/test/ELF/gc-sections-protected.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
 // RUN: ld.lld %t.o -o %t.so -shared --gc-sections
 // RUN: llvm-readobj -s %t.so | FileCheck %s
diff --git a/test/ELF/gdb-index-dup-types.s b/test/ELF/gdb-index-dup-types.s
deleted file mode 100644
index f5df00c..0000000
--- a/test/ELF/gdb-index-dup-types.s
+++ /dev/null
@@ -1,60 +0,0 @@
-# REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
-# RUN: ld.lld --gdb-index %t.o -o %t
-# RUN: llvm-dwarfdump -gdb-index %t | FileCheck %s
-
-## Testcase is based on output produced by gcc version 5.4.1 20160904
-## it has duplicate entries in .debug_gnu_pubtypes which seems to be
-## compiler bug. In that case it is useless to have them in .gdb_index
-## and we filter such entries out to reduce size of .gdb_index.
-
-## CHECK: Constant pool offset = {{.*}}, has 1 CU vectors:
-## CHECK-NOT: 0(0x0): 0x90000000 0x90000000
-
-.section .debug_abbrev,"",@progbits
- .byte 1                       # Abbreviation Code
- .byte 17                      # DW_TAG_compile_unit
- .byte 0                       # DW_CHILDREN_no
- .byte 16                      # DW_AT_stmt_list
- .byte 23                      # DW_FORM_sec_offset
- .ascii "\260B"                # DW_AT_GNU_dwo_name
- .byte 14                      # DW_FORM_strp
- .byte 27                      # DW_AT_comp_dir
- .byte 14                      # DW_FORM_strp
- .ascii "\264B"                # DW_AT_GNU_pubnames
- .byte 25                      # DW_FORM_flag_present
- .ascii "\261B"                # DW_AT_GNU_dwo_id
- .byte 7                       # DW_FORM_data8
- .ascii "\263B"                # DW_AT_GNU_addr_base
- .byte 23                      # DW_FORM_sec_offset
- .byte 0                       # EOM(1)
- .byte 0                       # EOM(2)
- .byte 0                       # EOM(3)
-
-.section .debug_info,"",@progbits
-.Lcu_begin0:
- .long 32                       # Length of Unit
- .short 4                       # DWARF version number
- .long .debug_abbrev            # Offset Into Abbrev. Section
- .byte 8                        # Address Size (in bytes)
- .byte 1                        # Abbrev [1] 0xb:0x19 DW_TAG_compile_unit
- .long 0                        # DW_AT_stmt_list
- .long 0                        # DW_AT_GNU_dwo_name
- .long 0                        # DW_AT_comp_dir
- .quad 0                        # DW_AT_GNU_dwo_id
- .long 0                        # DW_AT_GNU_addr_base
-
-.section .debug_gnu_pubtypes,"",@progbits
-.long .LpubTypes_end0-.LpubTypes_begin0 # Length of Public Types Info
-.LpubTypes_begin0:
- .short 2                      # DWARF Version
- .long .Lcu_begin0             # Offset of Compilation Unit Info
- .long 36                      # Compilation Unit Length
- .long 36                      # DIE offset
- .byte 144                     # Kind: TYPE, STATIC
- .asciz "int"                  # External Name
- .long 36                      # DIE offset
- .byte 144                     # Kind: TYPE, STATIC
- .asciz "int"                  # External Name
- .long 0                       # End Mark
-.LpubTypes_end0:
diff --git a/test/ELF/gdb-index-noranges.s b/test/ELF/gdb-index-noranges.s
index 29ba91e..1208064 100644
--- a/test/ELF/gdb-index-noranges.s
+++ b/test/ELF/gdb-index-noranges.s
@@ -10,7 +10,7 @@
 ##
 ## Debug information does not contain any address ranges.
 ## We crashed in that case. Check we don't.
-# RUN: ld.lld --gdb-index %t1.o -o %t
+# RUN: ld.lld --gdb-index %t1.o -o /dev/null
 
 .section  .debug_str,"MS",@progbits,1
 .Lskel_string0:
diff --git a/test/ELF/gdb-index-tls.s b/test/ELF/gdb-index-tls.s
index 0fd7b61..785e13f 100644
--- a/test/ELF/gdb-index-tls.s
+++ b/test/ELF/gdb-index-tls.s
@@ -1,6 +1,6 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-# RUN: ld.lld --gdb-index -shared %t.o -o %t
+# RUN: ld.lld --gdb-index -shared %t.o -o /dev/null
 
 # This used to fail trying to compute R_X86_64_DTPOFF64
 
diff --git a/test/ELF/gdb-index.s b/test/ELF/gdb-index.s
index 9c763ab..e7f9606 100644
--- a/test/ELF/gdb-index.s
+++ b/test/ELF/gdb-index.s
@@ -34,16 +34,16 @@
 # DWARF-NEXT:    Low/High address = [0x201000, 0x201001) (Size: 0x1), CU id = 0
 # DWARF-NEXT:    Low/High address = [0x201004, 0x201006) (Size: 0x2), CU id = 1
 # DWARF:       Symbol table offset = 0x60, size = 1024, filled slots:
-# DWARF-NEXT:    512: Name offset = 0x2b, CU vector offset = 0x14
-# DWARF-NEXT:      String name: aaaaaaaaaaaaaaaa, CU vector index: 2
-# DWARF-NEXT:    754: Name offset = 0x27, CU vector offset = 0x8
-# DWARF-NEXT:      String name: int, CU vector index: 1
-# DWARF-NEXT:    822: Name offset = 0x1c, CU vector offset = 0x0
-# DWARF-NEXT:      String name: entrypoint, CU vector index: 0
+# DWARF-NEXT:    512: Name offset = 0x1c, CU vector offset = 0x0
+# DWARF-NEXT:      String name: aaaaaaaaaaaaaaaa, CU vector index: 0
+# DWARF-NEXT:    754: Name offset = 0x38, CU vector offset = 0x10
+# DWARF-NEXT:      String name: int, CU vector index: 2
+# DWARF-NEXT:    822: Name offset = 0x2d, CU vector offset = 0x8
+# DWARF-NEXT:      String name: entrypoint, CU vector index: 1
 # DWARF:       Constant pool offset = 0x2060, has 3 CU vectors:
-# DWARF-NEXT:    0(0x0): 0x30000000
-# DWARF-NEXT:    1(0x8): 0x90000000 0x90000001
-# DWARF-NEXT:    2(0x14): 0x30000001
+# DWARF-NEXT:    0(0x0): 0x30000001
+# DWARF-NEXT:    1(0x8): 0x30000000
+# DWARF-NEXT:    2(0x10): 0x90000000 0x90000001
 
 # SECTION-NOT: debug_gnu_pubnames
 
diff --git a/test/ELF/global-offset-table-position-aarch64.s b/test/ELF/global-offset-table-position-aarch64.s
index 8e26913..7fdc797 100644
--- a/test/ELF/global-offset-table-position-aarch64.s
+++ b/test/ELF/global-offset-table-position-aarch64.s
@@ -1,7 +1,7 @@
+// REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-linux-gnu %s -o %t
 // RUN: ld.lld --hash-style=sysv -shared %t -o %t2
 // RUN: llvm-readobj -t %t2 | FileCheck %s
-// REQUIRES: aarch64
 .globl  a
 .type   a,@object
 .comm   a,4,4
diff --git a/test/ELF/global-offset-table-position-arm.s b/test/ELF/global-offset-table-position-arm.s
index 19619b2..9abca8b 100644
--- a/test/ELF/global-offset-table-position-arm.s
+++ b/test/ELF/global-offset-table-position-arm.s
@@ -1,7 +1,7 @@
+// REQUIRES: arm
 // RUN: llvm-mc -filetype=obj -triple=armv7a-linux-gnueabihf %s -o %t
 // RUN: ld.lld --hash-style=sysv -shared %t -o %t2
 // RUN: llvm-readobj -t %t2 | FileCheck %s
-// REQUIRES: arm
 
 // The ARM _GLOBAL_OFFSET_TABLE_ should be defined at the start of the .got
 .globl  a
diff --git a/test/ELF/global-offset-table-position-i386.s b/test/ELF/global-offset-table-position-i386.s
index 8d50b49..e3d3434 100644
--- a/test/ELF/global-offset-table-position-i386.s
+++ b/test/ELF/global-offset-table-position-i386.s
@@ -1,7 +1,7 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=i386-pc-linux %s -o %t
 // RUN: ld.lld --hash-style=sysv -shared %t -o %t2
 // RUN: llvm-readobj -t %t2 | FileCheck %s
-// REQUIRES: x86
 
 // The X86 _GLOBAL_OFFSET_TABLE_ is defined at the start of the .got.plt
 // section.
diff --git a/test/ELF/global-offset-table-position-mips.s b/test/ELF/global-offset-table-position-mips.s
index 92daed1..a5577f2 100644
--- a/test/ELF/global-offset-table-position-mips.s
+++ b/test/ELF/global-offset-table-position-mips.s
@@ -1,9 +1,8 @@
+// REQUIRES: mips
 // RUN: llvm-mc -filetype=obj -triple=mips64-unknown-linux %s -o %t
 // RUN: ld.lld -shared %t -o %t2
 // RUN: llvm-readobj -t %t2 | FileCheck %s
 
-// REQUIRES: mips
-
 // The Mips _GLOBAL_OFFSET_TABLE_ should be defined at the start of the .got
 
 .globl  a
diff --git a/test/ELF/global-offset-table-position.s b/test/ELF/global-offset-table-position.s
index 57fe6c8..aa80836 100644
--- a/test/ELF/global-offset-table-position.s
+++ b/test/ELF/global-offset-table-position.s
@@ -1,7 +1,7 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
 // RUN: ld.lld --hash-style=sysv -shared %t -o %t2
 // RUN: llvm-readobj -t %t2 | FileCheck %s
-// REQUIRES: x86
 
 // The X86_64 _GLOBAL_OFFSET_TABLE_ is defined at the start of the .got.plt
 // section.
diff --git a/test/ELF/global_offset_table.s b/test/ELF/global_offset_table.s
index 47e95e9..3b86f00 100644
--- a/test/ELF/global_offset_table.s
+++ b/test/ELF/global_offset_table.s
@@ -1,5 +1,6 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
-// RUN: ld.lld %t -o %t2
+// RUN: ld.lld %t -o /dev/null
 .global _start
 _start:
 .long _GLOBAL_OFFSET_TABLE_
diff --git a/test/ELF/global_offset_table_shared.s b/test/ELF/global_offset_table_shared.s
index 06bd7b3..7af6a40 100644
--- a/test/ELF/global_offset_table_shared.s
+++ b/test/ELF/global_offset_table_shared.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
 // RUN: ld.lld --hash-style=sysv -shared %t -o %t2
 // RUN: llvm-readobj -t %t2 | FileCheck %s
diff --git a/test/ELF/gnu-hash-table.s b/test/ELF/gnu-hash-table.s
index ffbf19f..642b445 100644
--- a/test/ELF/gnu-hash-table.s
+++ b/test/ELF/gnu-hash-table.s
@@ -13,7 +13,7 @@
 # RUN: llvm-mc -filetype=obj -triple=powerpc64le-unknown-linux %t2.s -o %t2-ppc64le.o
 # RUN: llvm-mc -filetype=obj -triple=powerpc64-unknown-linux %t2.s -o %t2-ppc64.o
 
-# RUN: rm -f %t2-i386.a %t2-x86_64.a %t2-ppc64.a
+# RUN: rm -f %t2-i386.a %t2-x86_64.a %t2-ppc64.a %t2-ppc64le.a
 # RUN: llvm-ar rc %t2-i386.a %t2-i386.o
 # RUN: llvm-ar rc %t2-x86_64.a %t2-x86_64.o
 # RUN: llvm-ar rc %t2-ppc64le.a %t2-ppc64le.o
diff --git a/test/ELF/gnu-ifunc-dyntags.s b/test/ELF/gnu-ifunc-dyntags.s
index 37d46d2..81bd338 100644
--- a/test/ELF/gnu-ifunc-dyntags.s
+++ b/test/ELF/gnu-ifunc-dyntags.s
@@ -8,7 +8,7 @@
 ## when there are no other relocations except R_*_IRELATIVE.
 
 # CHECK:  Name          Size      Address
-# CHECK:  .rela.plt   00000030 0000000000000218
+# CHECK:  .rela.plt   00000030 0000000000000210
 # CHECK:  .got.plt    00000010 0000000000002000
 
 # TAGS:      Relocations [
@@ -19,7 +19,7 @@
 # TAGS-NEXT: ]
 
 # TAGS:   Tag                Type                 Name/Value
-# TAGS:   0x0000000000000017 JMPREL               0x218
+# TAGS:   0x0000000000000017 JMPREL               0x210
 # TAGS:   0x0000000000000002 PLTRELSZ             48
 # TAGS:   0x0000000000000003 PLTGOT               0x2000
 # TAGS:   0x0000000000000014 PLTREL               RELA
diff --git a/test/ELF/gnu-ifunc-i386.s b/test/ELF/gnu-ifunc-i386.s
index 4eda32f..f379bf1 100644
--- a/test/ELF/gnu-ifunc-i386.s
+++ b/test/ELF/gnu-ifunc-i386.s
@@ -1,8 +1,8 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=i686-pc-linux %s -o %t.o
 // RUN: ld.lld -static %t.o -o %tout
 // RUN: llvm-objdump -d %tout | FileCheck %s --check-prefix=DISASM
 // RUN: llvm-readobj -r -symbols -sections %tout | FileCheck %s
-// REQUIRES: x86
 
 // CHECK:      Sections [
 // CHECK:       Section {
diff --git a/test/ELF/gnu-ifunc-nosym-i386.s b/test/ELF/gnu-ifunc-nosym-i386.s
index d22cedb..564b87e 100644
--- a/test/ELF/gnu-ifunc-nosym-i386.s
+++ b/test/ELF/gnu-ifunc-nosym-i386.s
@@ -1,7 +1,7 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=i686-pc-linux %s -o %t.o
 // RUN: ld.lld -static %t.o -o %tout
 // RUN: llvm-readobj -symbols %tout | FileCheck %s
-// REQUIRES: x86
 
 // Check that no __rel_iplt_end/__rel_iplt_start
 // appear in symtab if there is no references to them.
diff --git a/test/ELF/gnu-ifunc-nosym.s b/test/ELF/gnu-ifunc-nosym.s
index 08e498e..4206f57 100644
--- a/test/ELF/gnu-ifunc-nosym.s
+++ b/test/ELF/gnu-ifunc-nosym.s
@@ -1,7 +1,7 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: ld.lld -static %t.o -o %tout
 // RUN: llvm-readobj -symbols %tout | FileCheck %s
-// REQUIRES: x86
 
 // Check that no __rela_iplt_end/__rela_iplt_start
 // appear in symtab if there is no references to them.
diff --git a/test/ELF/gnu-ifunc-plt-i386.s b/test/ELF/gnu-ifunc-plt-i386.s
index d0d21e7..14369bf 100644
--- a/test/ELF/gnu-ifunc-plt-i386.s
+++ b/test/ELF/gnu-ifunc-plt-i386.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=i686-pc-linux %S/Inputs/shared2-x86-64.s -o %t1.o
 // RUN: ld.lld %t1.o --shared -o %t.so
 // RUN: llvm-mc -filetype=obj -triple=i686-pc-linux %s -o %t.o
@@ -5,7 +6,6 @@
 // RUN: llvm-objdump -d %tout | FileCheck %s --check-prefix=DISASM
 // RUN: llvm-objdump -s %tout | FileCheck %s --check-prefix=GOTPLT
 // RUN: llvm-readobj -r -dynamic-table %tout | FileCheck %s
-// REQUIRES: x86
 
 // Check that the IRELATIVE relocations are after the JUMP_SLOT in the plt
 // CHECK: Relocations [
diff --git a/test/ELF/gnu-ifunc-plt.s b/test/ELF/gnu-ifunc-plt.s
index 88a0993..b88f32c 100644
--- a/test/ELF/gnu-ifunc-plt.s
+++ b/test/ELF/gnu-ifunc-plt.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/shared2-x86-64.s -o %t1.o
 // RUN: ld.lld %t1.o --shared -o %t.so
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
@@ -5,7 +6,6 @@
 // RUN: llvm-objdump -d %tout | FileCheck %s --check-prefix=DISASM
 // RUN: llvm-objdump -s %tout | FileCheck %s --check-prefix=GOTPLT
 // RUN: llvm-readobj -r -dynamic-table %tout | FileCheck %s
-// REQUIRES: x86
 
 // Check that the IRELATIVE relocations are after the JUMP_SLOT in the plt
 // CHECK: Relocations [
diff --git a/test/ELF/gnu-ifunc-relative.s b/test/ELF/gnu-ifunc-relative.s
index dc35102..d797301 100644
--- a/test/ELF/gnu-ifunc-relative.s
+++ b/test/ELF/gnu-ifunc-relative.s
@@ -1,7 +1,7 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: ld.lld -static %t.o -o %tout
 // RUN: llvm-readobj -r -t %tout | FileCheck %s
-// REQUIRES: x86
 
 .type foo STT_GNU_IFUNC
 .globl foo
diff --git a/test/ELF/gnu-ifunc.s b/test/ELF/gnu-ifunc.s
index 4911da6..faf51b4 100644
--- a/test/ELF/gnu-ifunc.s
+++ b/test/ELF/gnu-ifunc.s
@@ -1,8 +1,8 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: ld.lld -static %t.o -o %tout
 // RUN: llvm-objdump -d %tout | FileCheck %s --check-prefix=DISASM
 // RUN: llvm-readobj -r -symbols -sections %tout | FileCheck %s
-// REQUIRES: x86
 
 // CHECK:      Sections [
 // CHECK:       Section {
diff --git a/test/ELF/got-aarch64.s b/test/ELF/got-aarch64.s
index f46946c..c9e2027 100644
--- a/test/ELF/got-aarch64.s
+++ b/test/ELF/got-aarch64.s
@@ -1,8 +1,8 @@
+// REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-linux %s -o %t.o
 // RUN: ld.lld --hash-style=sysv -shared %t.o -o %t.so
 // RUN: llvm-readobj -s -r %t.so | FileCheck %s
 // RUN: llvm-objdump -d %t.so | FileCheck --check-prefix=DISASM %s
-// REQUIRES: aarch64
 
 // CHECK:      Name: .got
 // CHECK-NEXT: Type: SHT_PROGBITS
diff --git a/test/ELF/got-i386.s b/test/ELF/got-i386.s
index 679eb2e..7fb87e0 100644
--- a/test/ELF/got-i386.s
+++ b/test/ELF/got-i386.s
@@ -1,8 +1,8 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %s -o %t.o
 // RUN: ld.lld %t.o -o %t
 // RUN: llvm-readobj -s -r -t %t | FileCheck %s
 // RUN: llvm-objdump -d %t | FileCheck --check-prefix=DISASM %s
-// REQUIRES: x86
 
 // CHECK:      Name: .got
 // CHECK-NEXT: Type: SHT_PROGBITS
diff --git a/test/ELF/got-plt-header.s b/test/ELF/got-plt-header.s
index a6b10fa..f8412e1 100644
--- a/test/ELF/got-plt-header.s
+++ b/test/ELF/got-plt-header.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
 // RUN: ld.lld %t.o -o %t.so -shared
 // RUN: llvm-readobj -s -section-data %t.so | FileCheck %s
diff --git a/test/ELF/got.s b/test/ELF/got.s
index f67ea13..9d2d804 100644
--- a/test/ELF/got.s
+++ b/test/ELF/got.s
@@ -1,10 +1,10 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/shared.s -o %t2.o
 // RUN: ld.lld -shared %t2.o -o %t2.so
 // RUN: ld.lld --hash-style=sysv %t.o %t2.so -o %t
 // RUN: llvm-readobj -s -r %t | FileCheck %s
 // RUN: llvm-objdump -d %t | FileCheck --check-prefix=DISASM %s
-// REQUIRES: x86
 
 // CHECK:      Name: .got
 // CHECK-NEXT: Type: SHT_PROGBITS
diff --git a/test/ELF/gotpcrelx.s b/test/ELF/gotpcrelx.s
index 3ccbc56..d9a7b8e 100644
--- a/test/ELF/gotpcrelx.s
+++ b/test/ELF/gotpcrelx.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -relax-relocations -triple x86_64-pc-linux-gnu \
 // RUN: %s -o %t.o
 // RUN: llvm-readobj -r %t.o | FileCheck --check-prefix=RELS %s
diff --git a/test/ELF/hexagon.s b/test/ELF/hexagon.s
new file mode 100644
index 0000000..d3eaa37
--- /dev/null
+++ b/test/ELF/hexagon.s
@@ -0,0 +1,161 @@
+# REQUIRES: hexagon
+# RUN: llvm-mc -filetype=obj -triple=hexagon-unknown-elf %s -o %t
+# RUN: llvm-mc -filetype=obj -triple=hexagon-unknown-elf %S/Inputs/hexagon.s -o %t2
+# RUN: ld.lld %t2 %t  -o %t3
+# RUN: llvm-objdump -d  %t3 | FileCheck %s
+
+# Note: 69632 == 0x11000
+# R_HEX_32_6_X
+# R_HEX_12_X
+if (p0) r0 = ##_start
+# CHECK: immext(#69632)
+# CHECK: if (p0) r0 = ##69632
+
+# R_HEX_B15_PCREL
+if (p0) jump:nt #_start
+# CHECK: if (p0) jump:nt 0x11000
+
+# R_HEX_B32_PCREL_X
+# R_HEX_B15_PCREL_X
+if (p0) jump:nt ##_start
+# CHECK: if (p0) jump:nt 0x11000
+
+# R_HEX_B22_PCREL
+call #_start
+# CHECK: call 0x11000
+
+# R_HEX_B32_PCREL_X
+# R_HEX_B22_PCREL_X
+call ##_start
+# CHECK: immext(#4294967232)
+# CHECK: call 0x11000
+
+# R_HEX_6_X tests:
+# One test for each mask in the lookup table.
+
+#0x38000000
+if (!P0) memw(r0+#8)=##_start
+# CHECK: 38c0c100   	if (!p0) memw(r0+#8) = ##69632 }
+
+#0x39000000
+{ p0 = p1
+  if (!P0.new) memw(r0+#0)=##_start }
+# CHECK: 39c0c000   	if (!p0.new) memw(r0+#0) = ##69632 }
+
+#0x3e000000
+memw(r0+##_start)+=r1
+# CHECK: 3e40c001   	memw(r0+##69632) += r1 }
+
+#0x3f000000
+memw(r0+##_start)+=#4
+# CHECK: 3f40c004   	memw(r0+##69632) += #4 }
+
+#0x40000000
+{ r0 = r1
+  if (p0) memb(r0+##_start)=r0.new }
+# CHECK: 40a0c200   	if (p0) memb(r0+##69632) = r0.new }
+
+#0x41000000
+if (p0) r0=memb(r1+##_start)
+# CHECK: 4101c000   	if (p0) r0 = memb(r1+##69632) }
+
+#0x42000000
+{ r0 = r1
+  p0 = p1
+  if (p0.new) memb(r0+##_start)=r0.new }
+# CHECK: 42a0c200   	if (p0.new) memb(r0+##69632) = r0.new }
+
+#0x43000000
+{ p0 = p1
+ if (P0.new) r0=memb(r0+##_start) }
+# CHECK: 4300c000   	if (p0.new) r0 = memb(r0+##69632) }
+
+#0x44000000
+if (!p0) memb(r0+##_start)=r1
+# CHECK: 4400c100   	if (!p0) memb(r0+##69632) = r1 }
+
+#0x45000000
+if (!p0) r0=memb(r1+##_start)
+# CHECK: 4501c000   	if (!p0) r0 = memb(r1+##69632) }
+
+#0x46000000
+{ p0 = p1
+  if (!p0.new) memb(r0+##_start)=r1 }
+# CHECK: 4600c100   	if (!p0.new) memb(r0+##69632) = r1 }
+
+#0x47000000
+{ p0 = p1
+  if (!p0.new) r0=memb(r1+##_start) }
+# CHECK: 4701c000   	if (!p0.new) r0 = memb(r1+##69632) }
+
+#0x6a000000 -- Note 4294967132 == -0xa4 the distance between
+#              here and _start, so this will change if
+#              tests are added between here and _start
+r0=add(pc,##_start@pcrel)
+# CHECK: 6a49ce00  	r0 = add(pc,##4294967132) }
+
+#0x7c000000
+r1:0=combine(#8,##_start)
+# CHECK: 7c80c100   	r1:0 = combine(#8,##69632) }
+
+#0x9a000000
+r1:0=memb_fifo(r2=##_start)
+# CHECK: 9a82d000   	r1:0 = memb_fifo(r2=##69632) }
+
+#0x9b000000
+r0=memb(r1=##_start)
+# CHECK: 9b01d000   	r0 = memb(r1=##69632) }
+
+#0x9c000000
+r1:0=memb_fifo(r2<<#2+##_start)
+# CHECK: 9c82f000   	r1:0 = memb_fifo(r2<<#2+##69632) }
+
+#0x9d000000
+r0=memb(r1<<#2+##_start)
+# CHECK: 9d01f000   	r0 = memb(r1<<#2+##69632) }
+
+#0x9f000000
+if (!p0) r0=memb(##_start)
+# CHECK: 9f00e880   	if (!p0) r0 = memb(##69632) }
+
+#0xab000000
+memb(r0=##_start)=r1
+# CHECK: ab00c180   	memb(r0=##69632) = r1 }
+
+#0xad000000
+memb(r0<<#2+##_start)=r1
+# CHECK: ad00e180   	memb(r0<<#2+##69632) = r1 }
+
+#0xaf000000
+if (!p0) memb(##_start)=r1
+# CHECK: af00c184   	if (!p0) memb(##69632) = r1 }
+
+#0xd7000000
+r0=add(##_start,mpyi(r1,r2))
+# CHECK: d701c200   	r0 = add(##69632,mpyi(r1,r2)) }
+
+#0xd8000000
+R0=add(##_start,mpyi(r0,#2))
+# CHECK: d800c002   	r0 = add(##69632,mpyi(r0,#2)) }
+
+#0xdb000000
+r0=add(r1,add(r2,##_start))
+# CHECK: db01c002   	r0 = add(r1,add(r2,##69632)) }
+
+#0xdf000000
+r0=add(r1,mpyi(r2,##_start))
+# CHECK: df82c001   	r0 = add(r1,mpyi(r2,##69632)) }
+
+# Duplex form of R_HEX_6_X
+# R_HEX_32_6_X
+# R_HEX_6_X
+{ r0 = ##_start; r2 = r16 }
+# CHECK: 28003082   	r0 = ##69632; 	r2 = r16 }
+
+# R_HEX_HI16
+r0.h = #HI(_start)
+# CHECK: r0.h = #1
+
+# R_HEX_LO16
+r0.l = #LO(_start)
+# CHECK: r0.l = #4096
diff --git a/test/ELF/i386-debug-noabs.test b/test/ELF/i386-debug-noabs.test
index c0eb4d9..dbc7a57 100644
--- a/test/ELF/i386-debug-noabs.test
+++ b/test/ELF/i386-debug-noabs.test
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 
 # RUN: yaml2obj %s -o %t.o
-# RUN: ld.lld %t.o -o %t.exe --entry 0 --fatal-warnings
+# RUN: ld.lld %t.o -o /dev/null --entry 0 --fatal-warnings
 
 ## This is for https://bugs.llvm.org//show_bug.cgi?id=34852. GCC 8.0 or
 ## earlier have a bug which creates non-absolute R_386_GOTPC relocations
diff --git a/test/ELF/i386-got-value.s b/test/ELF/i386-got-value.s
index 8803fcf..2d7bd68 100644
--- a/test/ELF/i386-got-value.s
+++ b/test/ELF/i386-got-value.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc %s -o %t.o -filetype=obj -triple=i386-pc-linux
 # RUN: ld.lld %t.o -o %t.so -shared
 # RUN: llvm-readobj --relocations --sections --section-data %t.so | FileCheck %s
diff --git a/test/ELF/i386-merge.s b/test/ELF/i386-merge.s
index 91a153e..d895c73 100644
--- a/test/ELF/i386-merge.s
+++ b/test/ELF/i386-merge.s
@@ -9,7 +9,7 @@
 // CHECK-NEXT:   SHF_ALLOC
 // CHECK-NEXT:   SHF_MERGE
 // CHECK-NEXT: ]
-// CHECK-NEXT: Address: 0x128
+// CHECK-NEXT: Address: 0x158
 // CHECK-NEXT: Offset:
 // CHECK-NEXT: Size:
 // CHECK-NEXT: Link:
@@ -35,11 +35,10 @@
 // CHECK-NEXT: AddressAlignment: 1
 // CHECK-NEXT: EntrySize: 0
 // CHECK-NEXT: SectionData (
-// CHECK-NEXT:   0000: 28010000 |
+// CHECK-NEXT:   0000: 58010000 |
 // CHECK-NEXT: )
 
-// The content of .data should be the address of .mysec. 14010000 is 0x114 in
-// little endian.
+// The content of .data should be the address of .mysec.
 
         .data
         .long .mysec+4
diff --git a/test/ELF/i386-reloc-16.s b/test/ELF/i386-reloc-16.s
index 33b6c44..9a099a6 100644
--- a/test/ELF/i386-reloc-16.s
+++ b/test/ELF/i386-reloc-16.s
@@ -9,7 +9,7 @@
 // CHECK:      Contents of section .text:
 // CHECK-NEXT:   1000 42
 
-// RUN: not ld.lld -shared %t %t2 -o %t4 2>&1 | FileCheck --check-prefix=ERROR %s
+// RUN: not ld.lld -shared %t %t2 -o /dev/null 2>&1 | FileCheck --check-prefix=ERROR %s
 // ERROR: relocation R_386_16 out of range: 65536 is not in [-32768, 32767]
 
 .short foo
diff --git a/test/ELF/i386-reloc-8.s b/test/ELF/i386-reloc-8.s
index 9e9f0cc..b46aaa6 100644
--- a/test/ELF/i386-reloc-8.s
+++ b/test/ELF/i386-reloc-8.s
@@ -9,7 +9,7 @@
 // CHECK:      Contents of section .text:
 // CHECK-NEXT:   1000 ff
 
-// RUN: not ld.lld -shared %t %t2 -o %t4 2>&1 | FileCheck --check-prefix=ERROR %s
+// RUN: not ld.lld -shared %t %t2 -o /dev/null 2>&1 | FileCheck --check-prefix=ERROR %s
 // ERROR: relocation R_386_8 out of range: 256 is not in [-128, 127]
 
 .byte foo
diff --git a/test/ELF/i386-reloc-range.s b/test/ELF/i386-reloc-range.s
index 6f72f7a..4378bb6 100644
--- a/test/ELF/i386-reloc-range.s
+++ b/test/ELF/i386-reloc-range.s
@@ -14,7 +14,7 @@
 // CHECK-NEXT:      200: {{.*}} jmp -1
 //              0x10202 - 0x203 == 0xffff
 
-// RUN: not ld.lld -Ttext 0x200 %t.o %t2.o -o %t2 2>&1 | FileCheck --check-prefix=ERR %s
+// RUN: not ld.lld -Ttext 0x200 %t.o %t2.o -o /dev/null 2>&1 | FileCheck --check-prefix=ERR %s
 
 // ERR: {{.*}}:(.text+0x1): relocation R_386_PC16 out of range: 65536 is not in [-65536, 65535]
 
diff --git a/test/ELF/i386-retpoline-nopic-linkerscript.s b/test/ELF/i386-retpoline-nopic-linkerscript.s
index 88fbfe9..4243761 100644
--- a/test/ELF/i386-retpoline-nopic-linkerscript.s
+++ b/test/ELF/i386-retpoline-nopic-linkerscript.s
@@ -14,9 +14,9 @@
 
 // CHECK:      Disassembly of section .plt:
 // CHECK-NEXT: .plt:
-// CHECK-NEXT: 10:       ff 35 cc 00 00 00       pushl   204
+// CHECK-NEXT: 10:       ff 35 84 00 00 00       pushl   132
 // CHECK-NEXT: 16:       50      pushl   %eax
-// CHECK-NEXT: 17:       a1 d0 00 00 00 movl    208, %eax
+// CHECK-NEXT: 17:       a1 88 00 00 00 movl    136, %eax
 // CHECK-NEXT: 1c:       e8 0f 00 00 00  calll   15 <.plt+0x20>
 // CHECK-NEXT: 21:       f3 90   pause
 // CHECK-NEXT: 23:       0f ae e8        lfence
@@ -37,7 +37,7 @@
 // CHECK-NEXT: 3e:       c3      retl
 // CHECK-NEXT: 3f:       cc      int3
 // CHECK-NEXT: 40:       50      pushl   %eax
-// CHECK-NEXT: 41:       a1 d4 00 00 00  movl    212, %eax
+// CHECK-NEXT: 41:       a1 8c 00 00 00  movl    140, %eax
 // CHECK-NEXT: 46:       e8 e5 ff ff ff  calll   -27 <.plt+0x20>
 // CHECK-NEXT: 4b:       e9 d1 ff ff ff  jmp     -47 <.plt+0x11>
 // CHECK-NEXT: 50:       68 00 00 00 00  pushl   $0
@@ -49,7 +49,7 @@
 // CHECK-NEXT: 5e:       cc      int3
 // CHECK-NEXT: 5f:       cc      int3
 // CHECK-NEXT: 60:       50      pushl   %eax
-// CHECK-NEXT: 61:       a1 d8 00 00 00  movl    216, %eax
+// CHECK-NEXT: 61:       a1 90 00 00 00  movl    144, %eax
 // CHECK-NEXT: 66:       e8 c5 ff ff ff  calll   -59 <.plt+0x20>
 // CHECK-NEXT: 6b:       e9 b1 ff ff ff  jmp     -79 <.plt+0x11>
 // CHECK-NEXT: 70:       68 08 00 00 00  pushl   $8
diff --git a/test/ELF/i386-retpoline-pic-linkerscript.s b/test/ELF/i386-retpoline-pic-linkerscript.s
index a502f3f..6220332 100644
--- a/test/ELF/i386-retpoline-pic-linkerscript.s
+++ b/test/ELF/i386-retpoline-pic-linkerscript.s
@@ -14,9 +14,9 @@
 
 // CHECK:      Disassembly of section .plt:
 // CHECK-NEXT: .plt:
-// CHECK-NEXT: 10:       ff b3 cc 00 00 00       pushl   204(%ebx)
+// CHECK-NEXT: 10:       ff b3 84 00 00 00       pushl   132(%ebx)
 // CHECK-NEXT: 16:       50      pushl   %eax
-// CHECK-NEXT: 17:       8b 83 d0 00 00 00 movl    208(%ebx), %eax
+// CHECK-NEXT: 17:       8b 83 88 00 00 00 movl    136(%ebx), %eax
 // CHECK-NEXT: 1d:       e8 0e 00 00 00  calll   14 <.plt+0x20>
 // CHECK-NEXT: 22:       f3 90   pause
 // CHECK-NEXT: 24:       0f ae e8        lfence
@@ -36,7 +36,7 @@
 // CHECK-NEXT: 3e:       c3      retl
 // CHECK-NEXT: 3f:       cc      int3
 // CHECK-NEXT: 40:       50      pushl   %eax
-// CHECK-NEXT: 41:       8b 83 d4 00 00 00       movl    212(%ebx), %eax
+// CHECK-NEXT: 41:       8b 83 8c 00 00 00       movl    140(%ebx), %eax
 // CHECK-NEXT: 47:       e8 e4 ff ff ff  calll   -28 <.plt+0x20>
 // CHECK-NEXT: 4c:       e9 d1 ff ff ff  jmp     -47 <.plt+0x12>
 // CHECK-NEXT: 51:       68 00 00 00 00  pushl   $0
@@ -47,7 +47,7 @@
 // CHECK-NEXT: 5e:       cc      int3
 // CHECK-NEXT: 5f:       cc      int3
 // CHECK-NEXT: 60:       50      pushl   %eax
-// CHECK-NEXT: 61:       8b 83 d8 00 00 00       movl    216(%ebx), %eax
+// CHECK-NEXT: 61:       8b 83 90 00 00 00       movl    144(%ebx), %eax
 // CHECK-NEXT: 67:       e8 c4 ff ff ff  calll   -60 <.plt+0x20>
 // CHECK-NEXT: 6c:       e9 b1 ff ff ff  jmp     -79 <.plt+0x12>
 // CHECK-NEXT: 71:       68 08 00 00 00  pushl   $8
diff --git a/test/ELF/i386-tls-got.s b/test/ELF/i386-tls-got.s
index 56be4a1..efd4515 100644
--- a/test/ELF/i386-tls-got.s
+++ b/test/ELF/i386-tls-got.s
@@ -2,6 +2,6 @@
 # RUN: llvm-mc -filetype=obj -triple=i386-pc-linux %S/Inputs/i386-tls-got.s -o %t1.o
 # RUN: ld.lld %t1.o -o %t1.so -shared
 # RUN: llvm-mc -filetype=obj -triple=i386-pc-linux %s -o %t2.o
-# RUN: ld.lld %t2.o %t1.so -o %t
+# RUN: ld.lld %t2.o %t1.so -o /dev/null
 
 	addl	foobar@INDNTPOFF, %eax
diff --git a/test/ELF/i386-tls-ie-shared.s b/test/ELF/i386-tls-ie-shared.s
index 2b842a8..9e5ed1b 100644
--- a/test/ELF/i386-tls-ie-shared.s
+++ b/test/ELF/i386-tls-ie-shared.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=i686-pc-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=i686-pc-linux %p/Inputs/tls-opt-iele-i686-nopic.s -o %tso.o
 // RUN: ld.lld -shared %tso.o -o %tso
diff --git a/test/ELF/icf-absolute.s b/test/ELF/icf-absolute.s
index dcba213..3eef7a2 100644
--- a/test/ELF/icf-absolute.s
+++ b/test/ELF/icf-absolute.s
@@ -2,7 +2,7 @@
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %S/Inputs/icf-absolute.s -o %t2
-# RUN: ld.lld %t %t2 -o %t3 --icf=all --print-icf-sections | FileCheck %s
+# RUN: ld.lld %t %t2 -o /dev/null --icf=all --print-icf-sections | FileCheck %s
 
 # CHECK: selected section {{.*}}:(.text.f1)
 # CHECK:   removing identical section {{.*}}:(.text.f2)
diff --git a/test/ELF/icf-absolute2.s b/test/ELF/icf-absolute2.s
new file mode 100644
index 0000000..37e26a9
--- /dev/null
+++ b/test/ELF/icf-absolute2.s
@@ -0,0 +1,21 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %S/Inputs/icf-absolute2.s -o %t2
+# RUN: ld.lld %t %t2 -o /dev/null --icf=all --print-icf-sections | FileCheck -allow-empty %s
+
+## Test we do not crash and do not fold sections which relocations reffering to
+## absolute symbols with a different values.
+# CHECK-NOT: selected
+
+.globl _start, f1, f2
+_start:
+  ret
+
+.section .text.f1, "ax"
+f1:
+  .byte a1
+
+.section .text.f2, "ax"
+f2:
+  .byte a2
diff --git a/test/ELF/icf-c-identifier.s b/test/ELF/icf-c-identifier.s
index b1bd934..cd11b98 100644
--- a/test/ELF/icf-c-identifier.s
+++ b/test/ELF/icf-c-identifier.s
@@ -1,6 +1,6 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
-# RUN: ld.lld %t -o %t2 --icf=all --print-icf-sections | count 0
+# RUN: ld.lld %t -o /dev/null --icf=all --print-icf-sections | count 0
 
 .section foo,"ax",@progbits,unique,0
 .byte 42
diff --git a/test/ELF/icf-comdat.s b/test/ELF/icf-comdat.s
index 32899f2..761dd2e 100644
--- a/test/ELF/icf-comdat.s
+++ b/test/ELF/icf-comdat.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
-# RUN: ld.lld %t -o %t2 --icf=all --print-icf-sections | FileCheck %s
+# RUN: ld.lld %t -o /dev/null --icf=all --print-icf-sections | FileCheck %s
 
 # CHECK: selected section {{.*}}:(.text.f1)
 # CHECK:   removing identical section {{.*}}:(.text.f2)
diff --git a/test/ELF/icf-different-output-sections.s b/test/ELF/icf-different-output-sections.s
index 5f44f9b..0855731 100644
--- a/test/ELF/icf-different-output-sections.s
+++ b/test/ELF/icf-different-output-sections.s
@@ -1,6 +1,6 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
-# RUN: ld.lld %t -o %t2 --icf=all --print-icf-sections | count 0
+# RUN: ld.lld %t -o /dev/null --icf=all --print-icf-sections | count 0
 
 .section .foo,"ax"
 .byte 42
diff --git a/test/ELF/icf-i386.s b/test/ELF/icf-i386.s
index f43787e..67c7262 100644
--- a/test/ELF/icf-i386.s
+++ b/test/ELF/icf-i386.s
@@ -2,7 +2,7 @@
 # This test is to make sure that we can handle implicit addends properly.
 
 # RUN: llvm-mc -filetype=obj -triple=i386-unknown-linux %s -o %t
-# RUN: ld.lld %t -o %t2 --icf=all --print-icf-sections 2>&1 | FileCheck %s
+# RUN: ld.lld %t -o /dev/null --icf=all --print-icf-sections 2>&1 | FileCheck %s
 
 # CHECK:     selected section {{.*}}:(.text.f1)
 # CHECK:       removing identical section {{.*}}:(.text.f2)
diff --git a/test/ELF/icf-link-order.s b/test/ELF/icf-link-order.s
new file mode 100644
index 0000000..440971d
--- /dev/null
+++ b/test/ELF/icf-link-order.s
@@ -0,0 +1,18 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: ld.lld %t.o -o %t --icf=all --print-icf-sections | count 0
+
+.section .foo,"a",@progbits,unique,1
+foo1:
+.byte 1
+
+.section .foo,"a",@progbits,unique,2
+foo2:
+.byte 2
+
+.section .bar,"ao",@progbits,foo1,unique,1
+.byte 3
+
+.section .bar,"ao",@progbits,foo2,unique,2
+.byte 3
diff --git a/test/ELF/icf-many-sections.s b/test/ELF/icf-many-sections.s
new file mode 100644
index 0000000..766a003
--- /dev/null
+++ b/test/ELF/icf-many-sections.s
@@ -0,0 +1,62 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o %t
+# RUN: ld.lld --icf=all --print-icf-sections %t -o /dev/null | FileCheck %s -allow-empty
+
+# CHECK-NOT: selected
+
+.macro gen_sections4 z
+        .section .a\z,"ax"
+        .section .b\z,"ax"
+        .section .c\z,"ax"
+        .section .d\z,"ax"
+.endm
+
+.macro gen_sections8 z
+        gen_sections4 a\z
+        gen_sections4 b\z
+.endm
+
+.macro gen_sections16 z
+        gen_sections8 a\z
+        gen_sections8 b\z
+.endm
+
+.macro gen_sections32 x
+        gen_sections16 a\x
+        gen_sections16 b\x
+.endm
+
+.macro gen_sections64 z
+        gen_sections32 a\z
+        gen_sections32 b\z
+.endm
+
+.macro gen_sections128 z
+        gen_sections64 a\z
+        gen_sections64 b\z
+.endm
+
+.macro gen_sections256 z
+        gen_sections128 a\z
+        gen_sections128 b\z
+.endm
+
+.macro gen_sections512 z
+        gen_sections256 a\z
+        gen_sections256 b\z
+.endm
+
+.macro gen_sections1024 z
+        gen_sections512 a\z
+        gen_sections512 b\z
+.endm
+
+.macro gen_sections2048 z
+        gen_sections1024 a\z
+        gen_sections1024 b\z
+.endm
+
+gen_sections2048 a
+
+.global _start
+_start:
diff --git a/test/ELF/icf-merge-sec.s b/test/ELF/icf-merge-sec.s
index 2eef3f8..060f19d 100644
--- a/test/ELF/icf-merge-sec.s
+++ b/test/ELF/icf-merge-sec.s
@@ -2,7 +2,7 @@
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %S/Inputs/icf-merge-sec.s -o %t2
-# RUN: ld.lld %t %t2 -o %t3 --icf=all --print-icf-sections | FileCheck %s
+# RUN: ld.lld %t %t2 -o /dev/null --icf=all --print-icf-sections | FileCheck %s
 
 # CHECK: selected section {{.*}}:(.text.f1)
 # CHECK:   removing identical section {{.*}}:(.text.f2)
diff --git a/test/ELF/icf-merge.s b/test/ELF/icf-merge.s
index f3f605c..5aa79f9 100644
--- a/test/ELF/icf-merge.s
+++ b/test/ELF/icf-merge.s
@@ -2,7 +2,7 @@
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %S/Inputs/icf-merge.s -o %t1
-# RUN: ld.lld %t %t1 -o %t1.out --icf=all --print-icf-sections | FileCheck %s
+# RUN: ld.lld %t %t1 -o /dev/null --icf=all --print-icf-sections | FileCheck %s
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %S/Inputs/icf-merge2.s -o %t2
 # RUN: ld.lld %t %t2 -o %t3.out --icf=all --print-icf-sections | FileCheck --check-prefix=NOMERGE %s
diff --git a/test/ELF/icf-non-mergeable.s b/test/ELF/icf-non-mergeable.s
index cefe360..978ac15 100644
--- a/test/ELF/icf-non-mergeable.s
+++ b/test/ELF/icf-non-mergeable.s
@@ -8,7 +8,7 @@
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
 // RUN:    %p/Inputs/icf-non-mergeable.s -o %t2
 
-// RUN: ld.lld %t1 %t2 -o %t3 --icf=all --verbose 2>&1 | FileCheck %s
+// RUN: ld.lld %t1 %t2 -o /dev/null --icf=all --verbose 2>&1 | FileCheck %s
 
 // CHECK-NOT: selected section '.text.f1'
 // CHECK-NOT:   removing identical section '.text.f2'
diff --git a/test/ELF/icf-none.s b/test/ELF/icf-none.s
index ed3e2dc..7c73361 100644
--- a/test/ELF/icf-none.s
+++ b/test/ELF/icf-none.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
-# RUN: ld.lld %t -o %t2 --icf=all --icf=none --verbose 2>&1 | FileCheck %s
+# RUN: ld.lld %t -o /dev/null --icf=all --icf=none --verbose 2>&1 | FileCheck %s
 
 # CHECK-NOT: selected section '.text.f1'
 
diff --git a/test/ELF/icf-relro.s b/test/ELF/icf-relro.s
index 492c042..874fa7b 100644
--- a/test/ELF/icf-relro.s
+++ b/test/ELF/icf-relro.s
@@ -1,6 +1,6 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
-# RUN: ld.lld %t -o %t2 --icf=all --ignore-data-address-equality --print-icf-sections | FileCheck %s
+# RUN: ld.lld %t -o /dev/null --icf=all --ignore-data-address-equality --print-icf-sections | FileCheck %s
 
 # CHECK: selected section {{.*}}:(.data.rel.ro)
 # CHECK:   removing identical section {{.*}}:(.data.rel.ro.foo)
diff --git a/test/ELF/icf-safe.s b/test/ELF/icf-safe.s
new file mode 100644
index 0000000..2ced78f
--- /dev/null
+++ b/test/ELF/icf-safe.s
@@ -0,0 +1,182 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
+# RUN: llvm-objcopy %t1.o %t1copy.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %S/Inputs/icf-safe.s -o %t2.o
+# RUN: ld.lld %t1.o %t2.o -o %t2 --icf=safe --print-icf-sections | FileCheck %s
+# RUN: ld.lld %t1.o %t2.o -o %t3 --icf=safe --print-icf-sections -shared | FileCheck --check-prefix=EXPORT %s
+# RUN: ld.lld %t1.o %t2.o -o %t3 --icf=safe --print-icf-sections --export-dynamic | FileCheck --check-prefix=EXPORT %s
+# RUN: ld.lld %t1.o %t2.o -o %t2 --icf=all --print-icf-sections | FileCheck --check-prefix=ALL %s
+# RUN: ld.lld %t1.o %t2.o -o %t2 --icf=all --print-icf-sections --export-dynamic | FileCheck --check-prefix=ALL-EXPORT %s
+# RUN: ld.lld %t1copy.o -o %t4 --icf=safe 2>&1 | FileCheck --check-prefix=OBJCOPY %s
+
+# CHECK-NOT: selected section {{.*}}:(.text.f1)
+# CHECK: selected section {{.*}}:(.text.f3)
+# CHECK:   removing identical section {{.*}}:(.text.f4)
+
+# CHECK-NOT: selected section {{.*}}:(.rodata.h1)
+# CHECK: selected section {{.*}}:(.rodata.h3)
+# CHECK:   removing identical section {{.*}}:(.rodata.h4)
+
+# CHECK-NOT: selected section {{.*}}:(.rodata.l1)
+# CHECK: selected section {{.*}}:(.rodata.l3)
+# CHECK:   removing identical section {{.*}}:(.rodata.l4)
+
+# CHECK-NOT: selected section {{.*}}:(.rodata.g1)
+# CHECK: selected section {{.*}}:(.rodata.g3)
+# CHECK:   removing identical section {{.*}}:(.rodata.g4)
+
+# CHECK-NOT: selected section {{.*}}:(.text.non_addrsig{{.}})
+
+# With --icf=all address-significance implies keep-unique only for rodata, not
+# text.
+# ALL: selected section {{.*}}:(.text.f3)
+# ALL:   removing identical section {{.*}}:(.text.f4)
+
+# ALL-NOT: selected section {{.*}}:(.rodata.h1)
+# ALL: selected section {{.*}}:(.rodata.h3)
+# ALL:   removing identical section {{.*}}:(.rodata.h4)
+
+# ALL-NOT: selected section {{.*}}:(.rodata.l1)
+# ALL: selected section {{.*}}:(.rodata.l3)
+# ALL:   removing identical section {{.*}}:(.rodata.l4)
+
+# ALL-NOT: selected section {{.*}}:(.rodata.g1)
+# ALL: selected section {{.*}}:(.rodata.g3)
+# ALL:   removing identical section {{.*}}:(.rodata.g4)
+
+# ALL: selected section {{.*}}:(.text.f1)
+# ALL:   removing identical section {{.*}}:(.text.f2)
+# ALL:   removing identical section {{.*}}:(.text.non_addrsig1)
+# ALL:   removing identical section {{.*}}:(.text.non_addrsig2)
+
+# llvm-mc normally emits an empty .text section into every object file. Since
+# nothing actually refers to it via a relocation, it doesn't have any associated
+# symbols (thus nor can anything refer to it via a relocation, making it safe to
+# merge with the empty section in the other input file). Here we check that the
+# only two sections merged are the two empty sections and the sections with only
+# STB_LOCAL or STV_HIDDEN symbols. The dynsym entries should have prevented
+# anything else from being merged.
+# EXPORT-NOT: selected section
+# EXPORT: selected section {{.*}}:(.rodata.h3)
+# EXPORT:   removing identical section {{.*}}:(.rodata.h4)
+# EXPORT-NOT: selected section
+# EXPORT: selected section {{.*}}:(.text)
+# EXPORT:   removing identical section {{.*}}:(.text)
+# EXPORT-NOT: selected section
+# EXPORT: selected section {{.*}}:(.rodata.l3)
+# EXPORT:   removing identical section {{.*}}:(.rodata.l4)
+# EXPORT-NOT: selected section
+
+# If --icf=all is specified when exporting we can also merge the exported text
+# sections, but not the exported rodata.
+# ALL-EXPORT-NOT: selected section
+# ALL-EXPORT: selected section {{.*}}:(.text.f3)
+# ALL-EXPORT:   removing identical section {{.*}}:(.text.f4)
+# ALL-EXPORT-NOT: selected section
+# ALL-EXPORT: selected section {{.*}}:(.rodata.h3)
+# ALL-EXPORT:   removing identical section {{.*}}:(.rodata.h4)
+# ALL-EXPORT-NOT: selected section
+# ALL-EXPORT: selected section {{.*}}:(.text)
+# ALL-EXPORT:   removing identical section {{.*}}:(.text)
+# ALL-EXPORT-NOT: selected section
+# ALL-EXPORT: selected section {{.*}}:(.rodata.l3)
+# ALL-EXPORT:   removing identical section {{.*}}:(.rodata.l4)
+# ALL-EXPORT-NOT: selected section
+# ALL-EXPORT: selected section {{.*}}:(.text.f1)
+# ALL-EXPORT:   removing identical section {{.*}}:(.text.f2)
+# ALL-EXPORT:   removing identical section {{.*}}:(.text.non_addrsig1)
+# ALL-EXPORT:   removing identical section {{.*}}:(.text.non_addrsig2)
+# ALL-EXPORT-NOT: selected section
+
+# OBJCOPY: --icf=safe is incompatible with object files created using objcopy or ld -r
+
+.section .text.f1,"ax",@progbits
+.globl f1
+f1:
+ret
+
+.section .text.f2,"ax",@progbits
+.globl f2
+f2:
+ret
+
+.section .text.f3,"ax",@progbits
+.globl f3
+f3:
+ud2
+
+.section .text.f4,"ax",@progbits
+.globl f4
+f4:
+ud2
+
+.section .rodata.g1,"a",@progbits
+.globl g1
+g1:
+.byte 1
+
+.section .rodata.g2,"a",@progbits
+.globl g2
+g2:
+.byte 1
+
+.section .rodata.g3,"a",@progbits
+.globl g3
+g3:
+.byte 2
+
+.section .rodata.g4,"a",@progbits
+.globl g4
+g4:
+.byte 2
+
+.section .rodata.l1,"a",@progbits
+l1:
+.byte 3
+
+.section .rodata.l2,"a",@progbits
+l2:
+.byte 3
+
+.section .rodata.l3,"a",@progbits
+l3:
+.byte 4
+
+.section .rodata.l4,"a",@progbits
+l4:
+.byte 4
+
+.section .rodata.h1,"a",@progbits
+.globl h1
+.hidden h1
+h1:
+.byte 5
+
+.section .rodata.h2,"a",@progbits
+.globl h2
+.hidden h2
+h2:
+.byte 5
+
+.section .rodata.h3,"a",@progbits
+.globl h3
+.hidden h3
+h3:
+.byte 6
+
+.section .rodata.h4,"a",@progbits
+.globl h4
+.hidden h4
+h4:
+.byte 6
+
+.addrsig
+.addrsig_sym f1
+.addrsig_sym f2
+.addrsig_sym g1
+.addrsig_sym g2
+.addrsig_sym l1
+.addrsig_sym l2
+.addrsig_sym h1
+.addrsig_sym h2
diff --git a/test/ELF/icf1.s b/test/ELF/icf1.s
index e0db01e..5c6e667 100644
--- a/test/ELF/icf1.s
+++ b/test/ELF/icf1.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
-# RUN: ld.lld %t -o %t2 --icf=all --print-icf-sections | FileCheck %s
+# RUN: ld.lld %t -o /dev/null --icf=all --print-icf-sections | FileCheck %s
 
 # CHECK: selected section {{.*}}:(.text.f1)
 # CHECK:   removing identical section {{.*}}:(.text.f2)
diff --git a/test/ELF/icf10.test b/test/ELF/icf10.test
new file mode 100644
index 0000000..96b4caf
--- /dev/null
+++ b/test/ELF/icf10.test
@@ -0,0 +1,40 @@
+# RUN: yaml2obj %s -o %t.o
+# RUN: ld.lld --icf=all %t.o -o /dev/null --print-icf-sections 2>&1 | FileCheck %s
+
+# Checks that ICF does not merge 2 sections the offset of
+# the relocations of which differ.
+
+# CHECK-NOT: selected
+
+!ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  OSABI:           ELFOSABI_FREEBSD
+  Type:            ET_REL
+  Machine:         EM_X86_64
+Sections:
+  - Name:            .text.foo
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR]
+    Content:         "FFFFFFFFFFFFFFFF"
+  - Name:            .text.bar
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR]
+    Content:         "FFFFFFFFFFFFFFFF"
+  - Name:            .rela.text.foo
+    Type:            SHT_RELA
+    Link:            .symtab
+    Info:            .text.foo
+    Relocations:
+      - Offset:          0x0000000000000000
+        Symbol:          ''
+        Type:            R_X86_64_NONE
+  - Name:            .rela.text.bar
+    Type:            SHT_RELA
+    Link:            .symtab
+    Info:            .text.bar
+    Relocations:
+      - Offset:          0x0000000000000001
+        Symbol:          ''
+        Type:            R_X86_64_NONE
diff --git a/test/ELF/icf11.test b/test/ELF/icf11.test
new file mode 100644
index 0000000..8c3aa93
--- /dev/null
+++ b/test/ELF/icf11.test
@@ -0,0 +1,52 @@
+# RUN: yaml2obj %s -o %t.o
+# RUN: ld.lld --icf=all %t.o -o /dev/null --print-icf-sections 2>&1 | FileCheck %s
+
+# Checks that ICF does not merge 2 sections the type of
+# the relocations of which differ.
+
+# CHECK-NOT: selected
+
+!ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  OSABI:           ELFOSABI_FREEBSD
+  Type:            ET_REL
+  Machine:         EM_X86_64
+Sections:
+  - Type:            SHT_PROGBITS
+    Name:            .text
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x04
+    Content:         "0000000000000000"
+  - Name:            .text.foo
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR]
+    Content:         "FFFFFFFFFFFFFFFF"
+  - Name:            .text.bar
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR]
+    Content:         "FFFFFFFFFFFFFFFF"
+  - Name:            .rela.text.foo
+    Type:            SHT_RELA
+    Link:            .symtab
+    Info:            .text.foo
+    Relocations:
+      - Offset:          0x0000000000000000
+        Symbol:          ''
+        Type:            R_X86_64_NONE
+  - Name:            .rela.text.bar
+    Type:            SHT_RELA
+    Link:            .symtab
+    Info:            .text.bar
+    Relocations:
+      - Offset:          0
+        Symbol:          zed
+        Type:            R_X86_64_64
+Symbols:
+  Global:
+    - Name:     zed
+      Type:     STT_FUNC
+      Section:  .text
+      Value:    0x0
+      Size:     8
diff --git a/test/ELF/icf12.s b/test/ELF/icf12.s
new file mode 100644
index 0000000..aa1e8af
--- /dev/null
+++ b/test/ELF/icf12.s
@@ -0,0 +1,20 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1
+# RUN: ld.lld %t1 -o /dev/null --icf=all --print-icf-sections 2>&1 | FileCheck -allow-empty %s
+
+# Check that ICF does not merge 2 sections which relocations
+# differs in addend only.
+
+# CHECK-NOT: selected
+
+.section .text
+.globl _start
+_start:
+  ret
+
+.section .text.foo, "ax"
+.quad _start + 1
+
+.section .text.bar, "ax"
+.quad _start + 2
diff --git a/test/ELF/icf13.s b/test/ELF/icf13.s
new file mode 100644
index 0000000..c0d4935
--- /dev/null
+++ b/test/ELF/icf13.s
@@ -0,0 +1,21 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1
+# RUN: ld.lld -shared -z notext %t1 -o /dev/null --icf=all --print-icf-sections 2>&1 | FileCheck -allow-empty %s
+
+## Check that ICF does not merge sections which relocations point to symbols
+## that are not of the regular defined kind. 
+
+# CHECK-NOT: selected
+
+.globl und
+
+.section .text
+.globl _start
+_start:
+  ret
+
+.section .text.foo, "ax"
+.quad _start
+
+.section .text.bar, "ax"
+.quad und
diff --git a/test/ELF/icf14.s b/test/ELF/icf14.s
new file mode 100644
index 0000000..caa33e4
--- /dev/null
+++ b/test/ELF/icf14.s
@@ -0,0 +1,26 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1
+# RUN: ld.lld %t1 -o /dev/null --icf=all --print-icf-sections 2>&1 | FileCheck -allow-empty %s
+
+# Check that ICF does not merge 2 sections which relocations
+# refer to symbols that live in sections of the different types
+# (regular input section and mergeable input sections in this case).
+
+# CHECK-NOT: selected
+
+.section .text
+.globl _start
+_start:
+  ret
+
+.section .rodata.str,"aMS",@progbits,1
+.globl rodata
+rodata:
+.asciz "foo"
+
+.section .text.foo, "ax"
+.quad rodata
+
+.section .text.bar, "ax"
+.quad _start
diff --git a/test/ELF/icf15.s b/test/ELF/icf15.s
new file mode 100644
index 0000000..5c651fb
--- /dev/null
+++ b/test/ELF/icf15.s
@@ -0,0 +1,24 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1
+# RUN: ld.lld %t1 -o /dev/null --icf=all --print-icf-sections 2>&1 | FileCheck -allow-empty %s
+
+## Check that ICF does not merge sections which relocations have equal addends,
+## but different target values.
+
+# CHECK-NOT: selected
+
+.globl und
+
+.section .text
+.globl foo
+foo:
+  .byte 0
+.globl bar
+bar:
+  .byte 0
+
+.section .text.foo, "ax"
+.quad foo
+
+.section .text.bar, "ax"
+.quad bar
diff --git a/test/ELF/icf16.s b/test/ELF/icf16.s
new file mode 100644
index 0000000..13cb8ec
--- /dev/null
+++ b/test/ELF/icf16.s
@@ -0,0 +1,24 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1
+# RUN: ld.lld -shared -z notext %t1 -o /dev/null --icf=all --print-icf-sections 2>&1 | FileCheck -allow-empty %s
+
+## ICF is able to merge sections which relocations referring regular input sections
+## or mergeable sections. .eh_frame is represented with a different kind of section,
+## here we check that ICF code is able to handle and will not merge sections which
+## relocations referring .eh_frame.
+
+# CHECK-NOT: selected
+
+.section ".eh_frame", "a", @progbits
+.globl foo
+foo:
+  .quad 0
+.globl bar
+bar:
+  .quad 0
+
+.section .text.foo, "ax"
+.quad foo
+
+.section .text.bar, "ax"
+.quad bar
diff --git a/test/ELF/icf17.s b/test/ELF/icf17.s
new file mode 100644
index 0000000..cab5c12
--- /dev/null
+++ b/test/ELF/icf17.s
@@ -0,0 +1,16 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1
+# RUN: ld.lld %t1 -o /dev/null --icf=all --print-icf-sections 2>&1 | FileCheck -allow-empty %s
+
+# CHECK-NOT: selected
+
+.section .text
+.globl _start
+_start:
+  ret
+
+.section .aaa, "ax",%progbits,unique,1
+.quad _start
+
+.section .aaa, "axS",%progbits,unique,2
+.quad _start
diff --git a/test/ELF/icf2.s b/test/ELF/icf2.s
index f76d214..8a456c7 100644
--- a/test/ELF/icf2.s
+++ b/test/ELF/icf2.s
@@ -2,7 +2,7 @@
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/icf2.s -o %t2
-# RUN: ld.lld %t1 %t2 -o %t --icf=all --print-icf-sections 2>&1 | FileCheck %s
+# RUN: ld.lld %t1 %t2 -o /dev/null --icf=all --print-icf-sections 2>&1 | FileCheck %s
 
 # CHECK: selected section {{.*}}:(.text.f1)
 # CHECK:   removing identical section {{.*}}:(.text.f2)
diff --git a/test/ELF/icf3.s b/test/ELF/icf3.s
index d2a7b40..7ae4acf 100644
--- a/test/ELF/icf3.s
+++ b/test/ELF/icf3.s
@@ -2,7 +2,7 @@
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/icf2.s -o %t2
-# RUN: ld.lld %t1 %t2 -o %t --icf=all --verbose 2>&1 | FileCheck %s
+# RUN: ld.lld %t1 %t2 -o /dev/null --icf=all --print-icf-sections 2>&1 | FileCheck -allow-empty %s
 
 # CHECK-NOT: selected section '.text.f1' from file
 # CHECK-NOT: selected section '.text.f2' from file
diff --git a/test/ELF/icf4.s b/test/ELF/icf4.s
index b44e71e..2b04796 100644
--- a/test/ELF/icf4.s
+++ b/test/ELF/icf4.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
-# RUN: ld.lld %t -o %t2 --icf=all --verbose 2>&1 | FileCheck %s
+# RUN: ld.lld %t -o /dev/null --icf=all --print-icf-sections 2>&1 | FileCheck -allow-empty %s
 
 # CHECK-NOT: selected section '.text.f1'
 # CHECK-NOT: selected section '.text.f2'
diff --git a/test/ELF/icf5.s b/test/ELF/icf5.s
index bdc28c1..86c0bc4 100644
--- a/test/ELF/icf5.s
+++ b/test/ELF/icf5.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
-# RUN: ld.lld %t -o %t2 --icf=all --verbose 2>&1 | FileCheck %s
+# RUN: ld.lld %t -o /dev/null --icf=all --print-icf-sections 2>&1 | FileCheck -allow-empty %s
 
 # CHECK-NOT: selected section '.text.f1'
 # CHECK-NOT: selected section '.text.f2'
diff --git a/test/ELF/icf6.s b/test/ELF/icf6.s
index 97449ca..0819d51 100644
--- a/test/ELF/icf6.s
+++ b/test/ELF/icf6.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
-# RUN: ld.lld %t -o %t2 --icf=all --verbose 2>&1 | FileCheck %s
+# RUN: ld.lld %t -o /dev/null --icf=all --print-icf-sections 2>&1 | FileCheck -allow-empty %s
 
 # CHECK-NOT: selected section '.text.f1'
 # CHECK-NOT: selected section '.text.f2'
diff --git a/test/ELF/icf9.s b/test/ELF/icf9.s
index 3a8bb4b..8092677 100644
--- a/test/ELF/icf9.s
+++ b/test/ELF/icf9.s
@@ -2,7 +2,7 @@
 
 ### Make sure that we do not merge data.
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
-# RUN: ld.lld %t -o %t2 --icf=all --verbose --print-icf-sections 2>&1 | FileCheck %s
+# RUN: ld.lld %t -o %t2 --icf=all --print-icf-sections 2>&1 | FileCheck -allow-empty %s
 # RUN: llvm-readelf -S -W %t2 | FileCheck --check-prefix=SEC %s
 
 # SEC:  .rodata      PROGBITS  0000000000200120 000120 000002 00 A 0 0 1
diff --git a/test/ELF/incompatible-ar-first.s b/test/ELF/incompatible-ar-first.s
index 02d14e8..e491719 100644
--- a/test/ELF/incompatible-ar-first.s
+++ b/test/ELF/incompatible-ar-first.s
@@ -1,11 +1,12 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/archive.s -o %ta.o
+// RUN: rm -f %t.a
 // RUN: llvm-ar rc %t.a %ta.o
 // RUN: llvm-mc -filetype=obj -triple=i686-linux %s -o %tb.o
-// RUN: not ld.lld %t.a %tb.o -o %t 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.a %tb.o -o /dev/null 2>&1 | FileCheck %s
 
 // We used to crash when
 // * The first object seen by the symbol table is from an archive.
 // * -m was not used.
 // CHECK: .a({{.*}}a.o) is incompatible with {{.*}}b.o
 
-// REQUIRES: x86
diff --git a/test/ELF/incompatible-section-flags.s b/test/ELF/incompatible-section-flags.s
index 25d9994..30bbe75 100644
--- a/test/ELF/incompatible-section-flags.s
+++ b/test/ELF/incompatible-section-flags.s
@@ -1,5 +1,6 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-// RUN: not ld.lld -shared %t.o -o %t 2>&1 | FileCheck %s
+// RUN: not ld.lld -shared %t.o -o /dev/null 2>&1 | FileCheck %s
 
 // CHECK:      error: incompatible section flags for .foo
 // CHECK-NEXT: >>> {{.*}}incompatible-section-flags.s.tmp.o:(.foo): 0x3
diff --git a/test/ELF/incompatible-section-types2.s b/test/ELF/incompatible-section-types2.s
index 146e680..3e281ce 100644
--- a/test/ELF/incompatible-section-types2.s
+++ b/test/ELF/incompatible-section-types2.s
@@ -1,5 +1,6 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-// RUN: not ld.lld %t.o -o %t 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
 
 // CHECK:      error: section type mismatch for .shstrtab
 // CHECK-NEXT: >>> <internal>:(.shstrtab): SHT_STRTAB
diff --git a/test/ELF/incompatible.s b/test/ELF/incompatible.s
index 9054293..283146a 100644
--- a/test/ELF/incompatible.s
+++ b/test/ELF/incompatible.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86,aarch64
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %ta.o
 // RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %s -o %tb.o
 // RUN: ld.lld -shared %tb.o -o %ti686.so
@@ -47,6 +48,7 @@
 // We used to fail to identify this incompatibility and crash trying to
 // read a 64 bit file as a 32 bit one.
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/archive2.s -o %ta.o
+// RUN: rm -f %t.a
 // RUN: llvm-ar rc %t.a %ta.o
 // RUN: llvm-mc -filetype=obj -triple=i686-linux %s -o %tb.o
 // RUN: not ld.lld %t.a %tb.o 2>&1 -o %t | FileCheck --check-prefix=ARCHIVE %s
@@ -56,4 +58,3 @@
 .data
         .long foo
 
-// REQUIRES: x86,aarch64
diff --git a/test/ELF/init_fini_priority.s b/test/ELF/init_fini_priority.s
index b10b925..17003ce 100644
--- a/test/ELF/init_fini_priority.s
+++ b/test/ELF/init_fini_priority.s
@@ -1,8 +1,8 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 // RUN: llvm-objdump -section-headers %t | FileCheck %s --check-prefix=OBJ
 // RUN: ld.lld %t -o %t.exe
 // RUN: llvm-objdump -s %t.exe | FileCheck %s
-// REQUIRES: x86
 
 // OBJ:       3 .init_array
 // OBJ-NEXT:  4 .init_array.100
diff --git a/test/ELF/invalid-cie-length.s b/test/ELF/invalid-cie-length.s
index c6da95e..7e73dd8 100644
--- a/test/ELF/invalid-cie-length.s
+++ b/test/ELF/invalid-cie-length.s
@@ -1,7 +1,7 @@
 // REQUIRES: x86
 
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
-// RUN: not ld.lld %t -o %t2 2>&1 | FileCheck %s
+// RUN: not ld.lld %t -o /dev/null 2>&1 | FileCheck %s
 
 .section .eh_frame
 .byte 0
diff --git a/test/ELF/invalid-cie-length2.s b/test/ELF/invalid-cie-length2.s
index 9140280..a43491c 100644
--- a/test/ELF/invalid-cie-length2.s
+++ b/test/ELF/invalid-cie-length2.s
@@ -1,7 +1,7 @@
 // REQUIRES: x86
 
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
-// RUN: not ld.lld %t -o %t2 2>&1 | FileCheck %s
+// RUN: not ld.lld %t -o /dev/null 2>&1 | FileCheck %s
 
 .section .eh_frame
 .long 42
diff --git a/test/ELF/invalid-cie-length3.s b/test/ELF/invalid-cie-length3.s
index fcbfa7f..3417efc 100644
--- a/test/ELF/invalid-cie-length3.s
+++ b/test/ELF/invalid-cie-length3.s
@@ -1,7 +1,7 @@
 // REQUIRES: x86
 
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
-// RUN: not ld.lld %t -o %t2 2>&1 | FileCheck %s
+// RUN: not ld.lld %t -o /dev/null 2>&1 | FileCheck %s
 
 .section .eh_frame
 .long 0xFFFFFFFC
diff --git a/test/ELF/invalid-cie-length4.s b/test/ELF/invalid-cie-length4.s
index 04f8eb2..cf3a6f5 100644
--- a/test/ELF/invalid-cie-length4.s
+++ b/test/ELF/invalid-cie-length4.s
@@ -1,7 +1,7 @@
 // REQUIRES: x86
 
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
-// RUN: not ld.lld %t -o %t2 2>&1 | FileCheck %s
+// RUN: not ld.lld %t -o /dev/null 2>&1 | FileCheck %s
 
 .section .eh_frame
 .long 0xFFFFFFFF
diff --git a/test/ELF/invalid-cie-length5.s b/test/ELF/invalid-cie-length5.s
index bfa35ed..223ce12 100644
--- a/test/ELF/invalid-cie-length5.s
+++ b/test/ELF/invalid-cie-length5.s
@@ -1,7 +1,7 @@
 // REQUIRES: x86
 
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
-// RUN: not ld.lld %t -o %t2 2>&1 | FileCheck %s
+// RUN: not ld.lld %t -o /dev/null 2>&1 | FileCheck %s
 
  .section .eh_frame
  .long 0xFFFFFFFF
diff --git a/test/ELF/invalid-cie-reference.s b/test/ELF/invalid-cie-reference.s
index fba2467..0f64c4a 100644
--- a/test/ELF/invalid-cie-reference.s
+++ b/test/ELF/invalid-cie-reference.s
@@ -1,7 +1,7 @@
 // REQUIRES: x86
 
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
-// RUN: not ld.lld %t -o %t2 2>&1 | FileCheck %s
+// RUN: not ld.lld %t -o /dev/null 2>&1 | FileCheck %s
 
         .section .eh_frame
         .long 0x14
diff --git a/test/ELF/invalid-eh-frame.s b/test/ELF/invalid-eh-frame.s
new file mode 100644
index 0000000..5334428
--- /dev/null
+++ b/test/ELF/invalid-eh-frame.s
@@ -0,0 +1,17 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: not ld.lld --eh-frame-hdr %t -o /dev/null 2>&1 | FileCheck %s
+
+# CHECK:      error: corrupted .eh_frame: unexpected end of CIE
+# CHECK-NEXT: >>> defined in {{.*}}:(.eh_frame+0x8)
+
+.section .eh_frame
+  .byte 0x04
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
diff --git a/test/ELF/invalid-eh-frame2.s b/test/ELF/invalid-eh-frame2.s
new file mode 100644
index 0000000..c8995cb
--- /dev/null
+++ b/test/ELF/invalid-eh-frame2.s
@@ -0,0 +1,22 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: not ld.lld --eh-frame-hdr %t -o /dev/null 2>&1 | FileCheck %s
+
+# CHECK:      error: corrupted .eh_frame: corrupted CIE (failed to read string)
+# CHECK-NEXT: >>> defined in {{.*}}:(.eh_frame+0x9)
+
+.section .eh_frame
+.align 1
+  .byte 0x08
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x01
+  .byte 0x01
+  .byte 0x01
+  .byte 0x01
diff --git a/test/ELF/invalid-eh-frame3.s b/test/ELF/invalid-eh-frame3.s
new file mode 100644
index 0000000..44592cb
--- /dev/null
+++ b/test/ELF/invalid-eh-frame3.s
@@ -0,0 +1,21 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: not ld.lld --eh-frame-hdr %t -o /dev/null 2>&1 | FileCheck %s
+
+# CHECK:      error: corrupted .eh_frame: corrupted CIE (failed to read LEB128)
+# CHECK-NEXT: >>> defined in {{.*}}:(.eh_frame+0xC)
+
+.section .eh_frame
+  .byte 0x08
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x01
+  .byte 0x01
+  .byte 0x00
+  .byte 0x01
diff --git a/test/ELF/invalid-eh-frame4.s b/test/ELF/invalid-eh-frame4.s
new file mode 100644
index 0000000..4022c04
--- /dev/null
+++ b/test/ELF/invalid-eh-frame4.s
@@ -0,0 +1,28 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: not ld.lld --eh-frame-hdr %t -o /dev/null 2>&1 | FileCheck %s
+
+# CHECK: corrupted .eh_frame: unknown .eh_frame augmentation string:
+
+.section .eh_frame
+  .byte 0x0E
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x01
+  .byte 0x01
+  .byte 0x00
+  .byte 0x01
+  
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x01
+  .byte 0x01
+  .byte 0x01
+  .byte 0x01
diff --git a/test/ELF/invalid-eh-frame5.s b/test/ELF/invalid-eh-frame5.s
new file mode 100644
index 0000000..eb153fa
--- /dev/null
+++ b/test/ELF/invalid-eh-frame5.s
@@ -0,0 +1,28 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: not ld.lld --eh-frame-hdr %t -o /dev/null 2>&1 | FileCheck %s
+
+# CHECK: corrupted .eh_frame: unknown .eh_frame augmentation string:
+
+.section .eh_frame
+  .byte 0x0E
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x03
+  .byte 0x01
+  .byte 0x00
+  .byte 0x01
+  
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x01
+  .byte 0x01
+  .byte 0x01
+  .byte 0x01
diff --git a/test/ELF/invalid-eh-frame6.s b/test/ELF/invalid-eh-frame6.s
new file mode 100644
index 0000000..9b6b7f8
--- /dev/null
+++ b/test/ELF/invalid-eh-frame6.s
@@ -0,0 +1,31 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: not ld.lld --eh-frame-hdr %t -o /dev/null 2>&1 | FileCheck %s
+
+# CHECK: error: corrupted .eh_frame: unknown FDE encoding
+# CHECK-NEXT: >>> defined in {{.*}}:(.eh_frame+0xE)
+
+.section .eh_frame
+  .byte 0x0E
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x01
+  
+  .byte 0x50 # Augmentation string: 'P','\0'
+  .byte 0x00
+  
+  .byte 0x01
+  
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x01
+  .byte 0x01
+  .byte 0x01
+  .byte 0x01
diff --git a/test/ELF/invalid-eh-frame7.s b/test/ELF/invalid-eh-frame7.s
new file mode 100644
index 0000000..81a0014
--- /dev/null
+++ b/test/ELF/invalid-eh-frame7.s
@@ -0,0 +1,30 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: not ld.lld --eh-frame-hdr %t -o /dev/null 2>&1 | FileCheck %s
+
+# CHECK: error: corrupted .eh_frame: DW_EH_PE_aligned encoding is not supported
+
+.section .eh_frame
+  .byte 0x0E
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x01
+  
+  .byte 0x50 # Augmentation string: 'P','\0'
+  .byte 0x00
+  
+  .byte 0x01
+  
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x51
+  .byte 0x01
+  .byte 0x01
+  .byte 0x01
diff --git a/test/ELF/invalid-eh-frame8.s b/test/ELF/invalid-eh-frame8.s
new file mode 100644
index 0000000..484fef2
--- /dev/null
+++ b/test/ELF/invalid-eh-frame8.s
@@ -0,0 +1,30 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: not ld.lld --eh-frame-hdr %t -o /dev/null 2>&1 | FileCheck %s
+
+# CHECK: error: corrupted .eh_frame: corrupted CIE
+
+.section .eh_frame
+  .byte 0x0E
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x01
+  
+  .byte 0x50 # Augmentation string: 'P','\0'
+  .byte 0x00
+  
+  .byte 0x01
+  
+  .byte 0x01 # LEB128
+  .byte 0x01 # LEB128
+
+  .byte 0x03
+  .byte 0x01
+  .byte 0x01
+  .byte 0x01
diff --git a/test/ELF/invalid-eh-frame9.s b/test/ELF/invalid-eh-frame9.s
new file mode 100644
index 0000000..73a102b
--- /dev/null
+++ b/test/ELF/invalid-eh-frame9.s
@@ -0,0 +1,15 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: not ld.lld --eh-frame-hdr %t -o /dev/null 2>&1 | FileCheck %s
+
+# CHECK: error: corrupted .eh_frame: CIE is too small
+
+.section .eh_frame
+  .byte 0x03
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
diff --git a/test/ELF/invalid-relocations.test b/test/ELF/invalid-relocations.test
index cfeb44b..7c32058 100644
--- a/test/ELF/invalid-relocations.test
+++ b/test/ELF/invalid-relocations.test
@@ -1,5 +1,5 @@
 # RUN: yaml2obj %s -o %t
-# RUN: not ld.lld %t -o %tout 2>&1 | FileCheck %s
+# RUN: not ld.lld %t -o /dev/null 2>&1 | FileCheck %s
 
 !ELF
 FileHeader:
diff --git a/test/ELF/invalid-undef-section-symbol.test b/test/ELF/invalid-undef-section-symbol.test
index 9d51f56..cb89306 100644
--- a/test/ELF/invalid-undef-section-symbol.test
+++ b/test/ELF/invalid-undef-section-symbol.test
@@ -1,5 +1,5 @@
 # RUN: yaml2obj %s -o %t.o
-# RUN: not ld.lld -r %t.o -o %t 2>&1 | FileCheck %s
+# RUN: not ld.lld -r %t.o -o /dev/null 2>&1 | FileCheck %s
 
 # We used to crash at this.
 # CHECK: STT_SECTION symbol should be defined
diff --git a/test/ELF/invalid-z.s b/test/ELF/invalid-z.s
deleted file mode 100644
index a5343c9..0000000
--- a/test/ELF/invalid-z.s
+++ /dev/null
@@ -1,9 +0,0 @@
-# REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-# RUN: not ld.lld %t.o -o %t -z max-page-size 2>&1 | FileCheck %s
-# CHECK: invalid max-page-size
-# CHECK-NOT: error
-
-.global _start
-_start:
-  nop
diff --git a/test/ELF/invalid/Inputs/cie-version2.elf b/test/ELF/invalid/Inputs/cie-version2.elf
deleted file mode 100644
index 87f8a5b..0000000
--- a/test/ELF/invalid/Inputs/cie-version2.elf
+++ /dev/null
Binary files differ
diff --git a/test/ELF/invalid/dynamic-section-size.s b/test/ELF/invalid/dynamic-section-size.s
index 323daba..58a7d06 100644
--- a/test/ELF/invalid/dynamic-section-size.s
+++ b/test/ELF/invalid/dynamic-section-size.s
@@ -1,4 +1,4 @@
 ## dynamic-section-sh_size.elf has incorrect sh_size of dynamic section.
-# RUN: not ld.lld %p/Inputs/dynamic-section-sh_size.elf -o %t2 2>&1 | \
+# RUN: not ld.lld %p/Inputs/dynamic-section-sh_size.elf -o /dev/null 2>&1 | \
 # RUN:   FileCheck %s
 # CHECK: error: {{.*}}: invalid sh_entsize
diff --git a/test/ELF/invalid/eh-frame-hdr-no-out.s b/test/ELF/invalid/eh-frame-hdr-no-out.s
index 8379253..221ca20 100644
--- a/test/ELF/invalid/eh-frame-hdr-no-out.s
+++ b/test/ELF/invalid/eh-frame-hdr-no-out.s
@@ -1,6 +1,19 @@
 // REQUIRES: x86
-// RUN: not ld.lld --eh-frame-hdr %p/Inputs/cie-version2.elf -o %t >& %t.log
-// RUN: FileCheck %s < %t.log
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+// RUN: not ld.lld --eh-frame-hdr %t -o /dev/null 2>&1 | FileCheck %s
 
-// cie-version2.elf contains unsupported version of CIE = 2.
-// CHECK: FDE version 1 or 3 expected, but got 2
+// CHECK: error: corrupted .eh_frame: FDE version 1 or 3 expected, but got 2
+
+.section .eh_frame
+  .byte 0x08
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
+  .byte 0x02
+  .byte 0x00
+  .byte 0x00
+  .byte 0x00
diff --git a/test/ELF/invalid/executable.s b/test/ELF/invalid/executable.s
index 33ef564..2540751 100644
--- a/test/ELF/invalid/executable.s
+++ b/test/ELF/invalid/executable.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 # RUN: ld.lld -o %t1.exe %t.o
-# RUN: not ld.lld -o %t2.exe %t1.exe 2>&1 | FileCheck %s
+# RUN: not ld.lld -o /dev/null %t1.exe 2>&1 | FileCheck %s
 # CHECK: unknown file type
 
 .global _start
diff --git a/test/ELF/invalid/invalid-e_shnum.s b/test/ELF/invalid/invalid-e_shnum.s
index 0c720ff..34a742e 100644
--- a/test/ELF/invalid/invalid-e_shnum.s
+++ b/test/ELF/invalid/invalid-e_shnum.s
@@ -1,3 +1,3 @@
 ## Spec says that "If a file has no section header table, e_shnum holds the value zero.", though
 ## in this test case it holds non-zero and lld used to crash.
-# RUN: ld.lld %p/Inputs/invalid-e_shnum.elf -o %t2
+# RUN: ld.lld %p/Inputs/invalid-e_shnum.elf -o /dev/null
diff --git a/test/ELF/invalid/invalid-elf.test b/test/ELF/invalid/invalid-elf.test
index 8be0437..80c8f41 100644
--- a/test/ELF/invalid/invalid-elf.test
+++ b/test/ELF/invalid/invalid-elf.test
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc %s -o %t -filetype=obj -triple x86_64-pc-linux
 
 # RUN: not ld.lld %t %p/Inputs/data-encoding.a -o %t2 2>&1 | \
diff --git a/test/ELF/invalid/merge-invalid-size.s b/test/ELF/invalid/merge-invalid-size.s
index 1dbd7cf..cc2566d 100644
--- a/test/ELF/invalid/merge-invalid-size.s
+++ b/test/ELF/invalid/merge-invalid-size.s
@@ -1,10 +1,10 @@
 // REQUIRES: x86
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
-// RUN: not ld.lld %t.o -o %t.so 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
 // CHECK: SHF_MERGE section size must be a multiple of sh_entsize
 
 // Test that we accept a zero sh_entsize.
-// RUN: ld.lld %p/Inputs/shentsize-zero.elf -o %t2
+// RUN: ld.lld %p/Inputs/shentsize-zero.elf -o /dev/null
 
 .section .foo,"aM",@progbits,4
 .short 42
diff --git a/test/ELF/invalid/mips-invalid-options-descriptor.s b/test/ELF/invalid/mips-invalid-options-descriptor.s
index b23ecee..c05e347 100644
--- a/test/ELF/invalid/mips-invalid-options-descriptor.s
+++ b/test/ELF/invalid/mips-invalid-options-descriptor.s
@@ -1,5 +1,5 @@
 ## mips-invalid-options-descriptor.elf has option descriptor in
 ## .MIPS.options with size of zero.
-# RUN: not ld.lld %p/Inputs/mips-invalid-options-descriptor.elf -o %t2 2>&1 | \
+# RUN: not ld.lld %p/Inputs/mips-invalid-options-descriptor.elf -o /dev/null 2>&1 | \
 # RUN:   FileCheck %s
 # CHECK: error: {{.*}}: invalid section offset
diff --git a/test/ELF/invalid/non-terminated-string.test b/test/ELF/invalid/non-terminated-string.test
new file mode 100644
index 0000000..82e94fe
--- /dev/null
+++ b/test/ELF/invalid/non-terminated-string.test
@@ -0,0 +1,19 @@
+# RUN: yaml2obj %s -o %t
+# RUN: not ld.lld %t -o %tout 2>&1 | FileCheck %s
+
+# CHECK: {{.*}}:(.merge): string is not null terminated
+
+!ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  OSABI:           ELFOSABI_FREEBSD
+  Type:            ET_REL
+  Machine:         EM_X86_64
+Sections:
+  - Type:            SHT_PROGBITS
+    Name:            .merge
+    Flags:           [ SHF_ALLOC, SHF_MERGE, SHF_STRINGS ]
+    AddressAlign:    0x04
+    Content:         "AABB"
+    EntSize:         0x2
diff --git a/test/ELF/invalid/section-alignment2.s b/test/ELF/invalid/section-alignment2.s
index aaef9f8..879ba8c 100644
--- a/test/ELF/invalid/section-alignment2.s
+++ b/test/ELF/invalid/section-alignment2.s
@@ -1,5 +1,5 @@
 ## section-alignment-notpow2.elf has section alignment
 ## 0xFFFFFFFF which is not a power of 2.
-# RUN: not ld.lld %p/Inputs/section-alignment-notpow2.elf -o %t2 2>&1 | \
+# RUN: not ld.lld %p/Inputs/section-alignment-notpow2.elf -o /dev/null 2>&1 | \
 # RUN:   FileCheck %s
 # CHECK: section sh_addralign is not a power of 2
diff --git a/test/ELF/invalid/sht-group.s b/test/ELF/invalid/sht-group.s
index f28035f..a4b684c 100644
--- a/test/ELF/invalid/sht-group.s
+++ b/test/ELF/invalid/sht-group.s
@@ -1,3 +1,3 @@
 ## sht-group.elf contains SHT_GROUP section with invalid sh_info.
-# RUN: not ld.lld %p/Inputs/sht-group.elf -o %t2 2>&1 | FileCheck %s
+# RUN: not ld.lld %p/Inputs/sht-group.elf -o /dev/null 2>&1 | FileCheck %s
 # CHECK: invalid symbol index
diff --git a/test/ELF/invalid/symbol-index.s b/test/ELF/invalid/symbol-index.s
index 4ad1d6c..e3989b4 100644
--- a/test/ELF/invalid/symbol-index.s
+++ b/test/ELF/invalid/symbol-index.s
@@ -5,6 +5,6 @@
 ##   [ 0]                   NULL            0000000000000000 000000 000000 00      0   0  0
 ## ...
 ##   [ 4] .symtab           RELA            0000000000000000 000048 000030 18      1   2  8
-# RUN: not ld.lld %p/Inputs/symbol-index.elf -o %t2 2>&1 | \
+# RUN: not ld.lld %p/Inputs/symbol-index.elf -o /dev/null 2>&1 | \
 # RUN:   FileCheck --check-prefix=INVALID-SYMBOL-INDEX %s
 # INVALID-SYMBOL-INDEX: invalid symbol index
diff --git a/test/ELF/invalid/symbol-name.s b/test/ELF/invalid/symbol-name.s
index 8daee1a..53a20ef 100644
--- a/test/ELF/invalid/symbol-name.s
+++ b/test/ELF/invalid/symbol-name.s
@@ -3,5 +3,5 @@
 ## symbol-name-offset.elf contains symbol with invalid (too large)
 ## st_name value.
 # RUN: not ld.lld %S/Inputs/symbol-name-offset.elf \
-# RUN:   -o %t 2>&1 | FileCheck %s
+# RUN:   -o /dev/null 2>&1 | FileCheck %s
 # CHECK: invalid symbol name offset
diff --git a/test/ELF/invalid/tls-symbol.s b/test/ELF/invalid/tls-symbol.s
index 354ca57..99c47dc 100644
--- a/test/ELF/invalid/tls-symbol.s
+++ b/test/ELF/invalid/tls-symbol.s
@@ -1,5 +1,5 @@
 # REQUIRES: x86
 
 ## The test file contains an STT_TLS symbol but has no TLS section.
-# RUN: not ld.lld %S/Inputs/tls-symbol.elf -o %t 2>&1 | FileCheck %s
+# RUN: not ld.lld %S/Inputs/tls-symbol.elf -o /dev/null 2>&1 | FileCheck %s
 # CHECK: has an STT_TLS symbol but doesn't have an SHF_TLS section
diff --git a/test/ELF/libsearch.s b/test/ELF/libsearch.s
index d21baf9..246a5d1 100644
--- a/test/ELF/libsearch.s
+++ b/test/ELF/libsearch.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
 // RUN:   %p/Inputs/libsearch-dyn.s -o %tdyn.o
@@ -10,7 +11,6 @@
 // RUN: cp -f %t.dir/libls.so %t.dir/libls2.so
 // RUN: rm -f %t.dir/libls.a
 // RUN: llvm-ar rcs %t.dir/libls.a %tst.o
-// REQUIRES: x86
 
 // Should fail if no library specified
 // RUN: not ld.lld -l 2>&1 \
diff --git a/test/ELF/linkerscript/Inputs/at6.s b/test/ELF/linkerscript/Inputs/at6.s
new file mode 100644
index 0000000..2d22d4d
--- /dev/null
+++ b/test/ELF/linkerscript/Inputs/at6.s
@@ -0,0 +1,11 @@
+.global _start
+.text
+_start:
+nop
+
+.section .sec1,"aw",@progbits
+.long 1
+
+.section .sec2,"aw",@progbits
+.long 2
+
diff --git a/test/ELF/linkerscript/Inputs/at7.s b/test/ELF/linkerscript/Inputs/at7.s
new file mode 100644
index 0000000..29d2963
--- /dev/null
+++ b/test/ELF/linkerscript/Inputs/at7.s
@@ -0,0 +1,7 @@
+.global _start
+.text
+_start:
+nop
+
+.section .sec, "aw"
+.word 4
diff --git a/test/ELF/linkerscript/Inputs/at8.s b/test/ELF/linkerscript/Inputs/at8.s
new file mode 100644
index 0000000..e15e4cd
--- /dev/null
+++ b/test/ELF/linkerscript/Inputs/at8.s
@@ -0,0 +1,8 @@
+.section .sec1,"aw",@progbits
+.quad   1
+
+.section .sec2,"aw",@progbits
+.quad   2
+
+.section .sec3,"aw",@progbits
+.quad   3
diff --git a/test/ELF/linkerscript/addr-zero.test b/test/ELF/linkerscript/addr-zero.test
index 0133fc0..6253f61 100644
--- a/test/ELF/linkerscript/addr-zero.test
+++ b/test/ELF/linkerscript/addr-zero.test
@@ -8,7 +8,7 @@
 
 # CHECK:      Symbol {
 # CHECK:        Name: foo
-# CHECK-NEXT:   Value: 0x38
+# CHECK-NEXT:   Value: 0x70
 # CHECK-NEXT:   Size: 0
 # CHECK-NEXT:   Binding: Global
 # CHECK-NEXT:   Type: None
diff --git a/test/ELF/linkerscript/addr.test b/test/ELF/linkerscript/addr.test
index bacb842..db0568e 100644
--- a/test/ELF/linkerscript/addr.test
+++ b/test/ELF/linkerscript/addr.test
@@ -6,7 +6,7 @@
 # CHECK:      Sections:
 # CHECK-NEXT: Idx Name          Size      Address          Type
 # CHECK-NEXT:   0               00000000 0000000000000000
-# CHECK-NEXT:   1 .text         00000000 0000000000001000 TEXT DATA
+# CHECK-NEXT:   1 .text         00000000 0000000000001000 TEXT
 # CHECK-NEXT:   2 .foo.1        00000008 0000000000001000 DATA
 # CHECK-NEXT:   3 .foo.2        00000008 0000000000001100 DATA
 # CHECK-NEXT:   4 .foo.3        00000008 0000000000001108 DATA
diff --git a/test/ELF/linkerscript/align-empty.test b/test/ELF/linkerscript/align-empty.test
index 9f879dd..63fe328 100644
--- a/test/ELF/linkerscript/align-empty.test
+++ b/test/ELF/linkerscript/align-empty.test
@@ -16,5 +16,7 @@
 # CHECK-NEXT: Idx Name          Size      Address
 # CHECK-NEXT:   0               00000000 0000000000000000
 # CHECK-NEXT:   1 .dynsym       00000018 0000000000000190
-# CHECK-NEXT:   2 .dynstr       00000001 00000000000001a8
-# CHECK-NEXT:   3 foo           00000001 0000000000001000
+# CHECK-NEXT:   2 .gnu.hash     0000001c 00000000000001a8
+# CHECK-NEXT:   3 .hash         00000010 00000000000001c4
+# CHECK-NEXT:   4 .dynstr       00000001 00000000000001d4
+# CHECK-NEXT:   5 foo           00000001 0000000000001000
diff --git a/test/ELF/linkerscript/assert.s b/test/ELF/linkerscript/assert.s
index 100631a..f7113e5 100644
--- a/test/ELF/linkerscript/assert.s
+++ b/test/ELF/linkerscript/assert.s
@@ -6,7 +6,7 @@
 # RUN: llvm-readobj %t1 > /dev/null
 
 # RUN: echo "SECTIONS { ASSERT(0, fail) }" > %t3.script
-# RUN: not ld.lld -shared -o %t3 --script %t3.script %t1.o > %t.log 2>&1
+# RUN: not ld.lld -shared -o /dev/null --script %t3.script %t1.o > %t.log 2>&1
 # RUN: FileCheck %s -check-prefix=FAIL < %t.log
 # FAIL: fail
 
@@ -34,7 +34,7 @@
 ## It is consistent with how ASSERT can be written outside of the
 ## output section declaration.
 # RUN: echo "SECTIONS { .foo : { ASSERT(1, \"true\") } }" > %t7.script
-# RUN: ld.lld -shared -o %t7 --script %t7.script %t1.o
+# RUN: ld.lld -shared -o /dev/null --script %t7.script %t1.o
 
 .section .foo, "a"
  .quad 0
diff --git a/test/ELF/linkerscript/at6.test b/test/ELF/linkerscript/at6.test
new file mode 100644
index 0000000..498c0ef
--- /dev/null
+++ b/test/ELF/linkerscript/at6.test
@@ -0,0 +1,30 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/at6.s -o %t.o
+# RUN: ld.lld %t.o --script %s -o %t
+# RUN: llvm-readelf -sections -program-headers %t | FileCheck %s
+
+MEMORY {
+  FLASH : ORIGIN = 0x08000000, LENGTH = 0x100
+  RAM   : ORIGIN = 0x20000000, LENGTH = 0x200
+}
+
+SECTIONS {
+ .text : { *(.text) } > FLASH
+ .sec1 : { *(.sec1) } > RAM
+ .sec2 : { *(.sec2) } > RAM AT > FLASH
+}
+
+# Make sure we create a separate PT_LOAD entry for .sec2. Previously,
+# it was added to the PT_LOAD entry of .sec1
+
+# CHECK: Name              Type            Address          Off
+# CHECK: .text             PROGBITS        0000000008000000 001000
+# CHECK: .sec1             PROGBITS        0000000020000000 002000
+# CHECK: .sec2             PROGBITS        0000000020000004 002004
+
+# CHECK: Program Headers:
+# CHECK:      Type  Offset   VirtAddr           PhysAddr
+# CHECK-NEXT: LOAD  0x001000 0x0000000008000000 0x0000000008000000
+# CHECK-NEXT: LOAD  0x002000 0x0000000020000000 0x0000000020000000
+# CHECK-NEXT: LOAD  0x002004 0x0000000020000004 0x0000000008000001
+# CHECK-NOT: LOAD
diff --git a/test/ELF/linkerscript/at7.test b/test/ELF/linkerscript/at7.test
new file mode 100644
index 0000000..1f67df2
--- /dev/null
+++ b/test/ELF/linkerscript/at7.test
@@ -0,0 +1,28 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/at7.s -o %t.o
+# RUN: ld.lld %t.o --script %s -o %t
+# RUN: llvm-readelf -sections -program-headers %t | FileCheck %s
+
+MEMORY {
+  RAM    : ORIGIN = 0x20000000, LENGTH = 0x200
+}
+
+SECTIONS {
+ .text : { *(.text) } > RAM AT> RAM
+ .sec  : { *(.sec)  } > RAM
+}
+
+# Make sure the memory for the .text section is only reserved once.
+# Previously, the location counter for both MemRegion and LMARegion
+# was increased unconditionally.
+
+
+# CHECK: Name              Type            Address          Off
+# CHECK: .text             PROGBITS        0000000020000000 001000
+# CHECK: .sec             PROGBITS        0000000020000001 001001
+
+# CHECK: Program Headers:
+# CHECK:      Type  Offset   VirtAddr           PhysAddr
+# CHECK-NEXT: LOAD  0x001000 0x0000000020000000 0x0000000020000000
+# CHECK-NEXT: LOAD  0x001001 0x0000000020000001 0x0000000020000001
+# CHECK-NOT: LOAD
diff --git a/test/ELF/linkerscript/at8.test b/test/ELF/linkerscript/at8.test
new file mode 100644
index 0000000..48c0d45
--- /dev/null
+++ b/test/ELF/linkerscript/at8.test
@@ -0,0 +1,31 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/at8.s -o %t.o
+# RUN: ld.lld %t.o --script %s -o %t
+# RUN: llvm-readelf -sections -program-headers %t | FileCheck %s
+
+MEMORY {
+  FLASH  : ORIGIN = 0x08000000, LENGTH = 0x100
+  RAM    : ORIGIN = 0x20000000, LENGTH = 0x200
+}
+
+SECTIONS {
+ .text : { *(.text) } > FLASH
+ .sec1 : { *(.sec1) } > RAM AT > FLASH
+ .sec2 : { *(.sec2) } > RAM
+ .sec3 : { *(.sec3) } > RAM AT > FLASH
+}
+
+# Make sure we do not issue a load-address overlap error
+# Previously, .sec3 would overwrite the LMAOffset in the
+# PT_LOAD header.
+
+# CHECK: Name              Type            Address          Off
+# CHECK: .text             PROGBITS        0000000008000000 001000
+# CHECK: .sec1             PROGBITS        0000000020000000 001000
+# CHECK: .sec2             PROGBITS        0000000020000008 001008
+# CHECK: .sec3             PROGBITS        0000000020000010 001010
+
+# CHECK: Program Headers:
+# CHECK:      Type  Offset   VirtAddr           PhysAddr
+# CHECK-NEXT: LOAD  0x001000 0x0000000020000000 0x0000000008000000
+# CHECK-NOT: LOAD
diff --git a/test/ELF/linkerscript/copy-rel-symbol-value-err.s b/test/ELF/linkerscript/copy-rel-symbol-value-err.s
index f134edb..cd5262b 100644
--- a/test/ELF/linkerscript/copy-rel-symbol-value-err.s
+++ b/test/ELF/linkerscript/copy-rel-symbol-value-err.s
@@ -3,7 +3,7 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/copy-rel-symbol-value.s -o %t2.o
 # RUN: ld.lld %t2.o -o %t2.so -shared
 # RUN: echo "SECTIONS { . = . + SIZEOF_HEADERS; foo = bar; }" > %t.script
-# RUN: not ld.lld %t.o %t2.so --script %t.script -o %t 2>&1 | FileCheck %s
+# RUN: not ld.lld %t.o %t2.so --script %t.script -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: symbol not found: bar
 
diff --git a/test/ELF/linkerscript/data-commands-gc.s b/test/ELF/linkerscript/data-commands-gc.s
index 1afcc9a..6d5ae8c 100644
--- a/test/ELF/linkerscript/data-commands-gc.s
+++ b/test/ELF/linkerscript/data-commands-gc.s
@@ -4,7 +4,7 @@
 # RUN: ld.lld --gc-sections -o %t %t.o --script %t.script
 # RUN: llvm-objdump -t %t | FileCheck %s
 
-# CHECK: 0000000000000011         .rodata                 00000000 bar
+# CHECK: 0000000000000008         .rodata                 00000000 bar
 
 .section .rodata.bar
 .quad 0x1122334455667788
diff --git a/test/ELF/linkerscript/define.test b/test/ELF/linkerscript/define.test
index 95b8831..3a2e242 100644
--- a/test/ELF/linkerscript/define.test
+++ b/test/ELF/linkerscript/define.test
@@ -12,4 +12,4 @@
 
 # CHECK: 1 .foo  00000008 0000000000011000 DATA
 # CHECK: 2 .bar  00000008 0000000000013000 DATA
-# CHECK: 3 .text 00000000 0000000000013008 TEXT DATA
+# CHECK: 3 .text 00000000 0000000000013008 TEXT
diff --git a/test/ELF/linkerscript/discard-interp.test b/test/ELF/linkerscript/discard-interp.test
index 97a6037..02e97b9 100644
--- a/test/ELF/linkerscript/discard-interp.test
+++ b/test/ELF/linkerscript/discard-interp.test
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux /dev/null -o %t.o
 # RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %p/../Inputs/shared.s -o %t2.o
 # RUN: ld.lld -shared %t2.o -o %t2.so
diff --git a/test/ELF/linkerscript/dot-is-not-abs.s b/test/ELF/linkerscript/dot-is-not-abs.s
index 1fae25e..a93d1c8 100644
--- a/test/ELF/linkerscript/dot-is-not-abs.s
+++ b/test/ELF/linkerscript/dot-is-not-abs.s
@@ -26,7 +26,7 @@
 # CHECK-NEXT:     SHF_ALLOC
 # CHECK-NEXT:     SHF_EXECINSTR
 # CHECK-NEXT:   ]
-# CHECK-NEXT:   Address: 0x1C
+# CHECK-NEXT:   Address: 0x0
 # CHECK-NEXT:   Offset:
 # CHECK-NEXT:   Size: 4
 # CHECK-NEXT:   Link:
@@ -40,7 +40,7 @@
 
 # CHECK:      Symbol {
 # CHECK:        Name: foo
-# CHECK-NEXT:   Value: 0x20
+# CHECK-NEXT:   Value: 0x4
 # CHECK-NEXT:   Size: 0
 # CHECK-NEXT:   Binding: Local
 # CHECK-NEXT:   Type: None
diff --git a/test/ELF/linkerscript/edata-etext.s b/test/ELF/linkerscript/edata-etext.s
index ab723ce..c15cf4c 100644
--- a/test/ELF/linkerscript/edata-etext.s
+++ b/test/ELF/linkerscript/edata-etext.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 # RUN: echo "SECTIONS { }" > %t.script
-# RUN: not ld.lld %t.o -script %t.script -o %t 2>&1 | FileCheck %s
+# RUN: not ld.lld %t.o -script %t.script -o /dev/null 2>&1 | FileCheck %s
 # CHECK: error: undefined symbol: _edata
 # CHECK: >>> referenced by {{.*}}:(.text+0x0)
 # CHECK: error: undefined symbol: _etext
diff --git a/test/ELF/linkerscript/expr-sections.test b/test/ELF/linkerscript/expr-sections.test
index f7d541d..1d16cc2 100644
--- a/test/ELF/linkerscript/expr-sections.test
+++ b/test/ELF/linkerscript/expr-sections.test
@@ -13,11 +13,11 @@
   }
 };
 
-# CHECK:  3 .text         00000000 00000000000000d0 TEXT DATA
+# CHECK:  5 .text         00000000 000000000000014c
 
-# CHECK: 00000000000000d1         .text		 00000000 foo1
-# CHECK: 00000000000000d1         .text		 00000000 bar1
+# CHECK: 000000000000014d         .text		 00000000 foo1
+# CHECK: 000000000000014d         .text		 00000000 bar1
 # CHECK: 0000000000000000         .text		 00000000 foo2
 # CHECK: 0000000000000000         .text		 00000000 bar2
-# CHECK: 00000000000000d1         .text		 00000000 foo3
-# CHECK: 00000000000000d1         .text		 00000000 bar3
+# CHECK: 000000000000014d         .text		 00000000 foo3
+# CHECK: 000000000000014d         .text		 00000000 bar3
diff --git a/test/ELF/linkerscript/filename-spec.s b/test/ELF/linkerscript/filename-spec.s
index 66fd417..8a1f660 100644
--- a/test/ELF/linkerscript/filename-spec.s
+++ b/test/ELF/linkerscript/filename-spec.s
@@ -43,6 +43,7 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.testdir1/filename-spec1.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
 # RUN:   %p/Inputs/filename-spec.s -o %t.testdir2/filename-spec2.o
+# RUN: rm -f %t.testdir1/lib1.a %t.testdir2/lib2.a
 # RUN: llvm-ar rsc %t.testdir1/lib1.a %t.testdir1/filename-spec1.o
 # RUN: llvm-ar rsc %t.testdir2/lib2.a %t.testdir2/filename-spec2.o
 
diff --git a/test/ELF/linkerscript/header-phdr2.s b/test/ELF/linkerscript/header-phdr2.s
index c595a6d..fbcd03f 100644
--- a/test/ELF/linkerscript/header-phdr2.s
+++ b/test/ELF/linkerscript/header-phdr2.s
@@ -2,7 +2,7 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 # RUN: echo "PHDRS { foobar PT_LOAD FILEHDR PHDRS; }"  > %t.script
 # RUN: echo "SECTIONS { .text : { *(.text) } : foobar }" >> %t.script
-# RUN: not ld.lld --script %t.script %t.o -o %t 2>&1 | FileCheck %s
+# RUN: not ld.lld --script %t.script %t.o -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: could not allocate headers
 
diff --git a/test/ELF/linkerscript/i386-sections-max-va-overflow.s b/test/ELF/linkerscript/i386-sections-max-va-overflow.s
index 80c4fb5..d424112 100644
--- a/test/ELF/linkerscript/i386-sections-max-va-overflow.s
+++ b/test/ELF/linkerscript/i386-sections-max-va-overflow.s
@@ -3,7 +3,7 @@
 
 # RUN: echo "SECTIONS { . = 0xfffffff1;" > %t.script
 # RUN: echo "           .bar : { *(.bar*) } }" >> %t.script
-# RUN: not ld.lld -o %t.so --script %t.script %t.o 2>&1 | FileCheck %s -check-prefix=ERR
+# RUN: not ld.lld -o /dev/null --script %t.script %t.o 2>&1 | FileCheck %s -check-prefix=ERR
 
 ## .bar section has data in [0xfffffff1, 0xfffffff1 + 0x10] == [0xffffff1, 0x1]. 
 ## Check we can catch this overflow.
diff --git a/test/ELF/linkerscript/implicit-program-header.test b/test/ELF/linkerscript/implicit-program-header.test
index 06b6161..8a3a4c6 100644
--- a/test/ELF/linkerscript/implicit-program-header.test
+++ b/test/ELF/linkerscript/implicit-program-header.test
@@ -3,13 +3,12 @@
 # RUN: echo '.section .text,"ax"; .quad 0' > %t.s
 # RUN: echo '.section .foo,"ax"; .quad 0' >> %t.s
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %t.s -o %t.o
-# RUN: ld.lld --hash-style=sysv -o %t1 --script %s  \
-# RUN:   %t.o -shared
-# RUN: llvm-readobj -elf-output-style=GNU -l %t1 | FileCheck %s
+# RUN: ld.lld --hash-style=sysv -o %t1 --script %s %t.o -shared
+# RUN: llvm-readelf -l %t1 | FileCheck %s
 
 # CHECK:      Segment Sections...
-# CHECK-NEXT:   00     .text .hash .dynamic
-# CHECK-NEXT:   01     .bar .dynsym .dynstr .foo
+# CHECK-NEXT:   00     .dynsym .hash .dynstr .bar .foo .text .dynamic
+# CHECK-NEXT:   01     .bar .foo
 
 PHDRS {
   ph_write PT_LOAD FLAGS(2);
diff --git a/test/ELF/linkerscript/insert-after.test b/test/ELF/linkerscript/insert-after.test
index ab5ba18..4260cd7 100644
--- a/test/ELF/linkerscript/insert-after.test
+++ b/test/ELF/linkerscript/insert-after.test
@@ -18,8 +18,8 @@
 # CHECK:      Sections:
 # CHECK-NEXT: Idx Name          Size      Address          Type
 # CHECK-NEXT:   0               00000000 0000000000000000
-# CHECK-NEXT:   1 .text         00000008 0000000000000000 TEXT DATA
-# CHECK-NEXT:   2 .foo.text     00000008 0000000000000008 TEXT DATA
+# CHECK-NEXT:   1 .text         00000008 0000000000000000 TEXT
+# CHECK-NEXT:   2 .foo.text     00000008 0000000000000008 TEXT
 # CHECK-NEXT:   3 .data         00000008 0000000000000010 DATA
 # CHECK-NEXT:   4 .foo.data     00000008 0000000000000018 DATA
 
diff --git a/test/ELF/linkerscript/insert-before.test b/test/ELF/linkerscript/insert-before.test
index 507e8bd..52317be 100644
--- a/test/ELF/linkerscript/insert-before.test
+++ b/test/ELF/linkerscript/insert-before.test
@@ -18,8 +18,8 @@
 # CHECK:      Sections:
 # CHECK-NEXT: Idx Name          Size      Address          Type
 # CHECK-NEXT:   0               00000000 0000000000000000
-# CHECK-NEXT:   1 .foo.text     00000008 0000000000000000 TEXT DATA
-# CHECK-NEXT:   2 .text         00000008 0000000000000008 TEXT DATA
+# CHECK-NEXT:   1 .foo.text     00000008 0000000000000000 TEXT
+# CHECK-NEXT:   2 .text         00000008 0000000000000008 TEXT
 # CHECK-NEXT:   3 .foo.data     00000008 0000000000000010 DATA
 # CHECK-NEXT:   4 .data         00000008 0000000000000018 DATA
 
diff --git a/test/ELF/linkerscript/lazy-symbols.test b/test/ELF/linkerscript/lazy-symbols.test
index 579df93..f409b83 100644
--- a/test/ELF/linkerscript/lazy-symbols.test
+++ b/test/ELF/linkerscript/lazy-symbols.test
@@ -1,5 +1,6 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %S/Inputs/lazy-symbols.s -o %t1
+# RUN: rm -f %tar
 # RUN: llvm-ar rcs %tar %t1
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux /dev/null -o %t2
 # RUN: ld.lld %t2 %tar --script %s -o %tout
diff --git a/test/ELF/linkerscript/linker-script-in-search-path.s b/test/ELF/linkerscript/linker-script-in-search-path.s
index be83b55..8f18022 100644
--- a/test/ELF/linkerscript/linker-script-in-search-path.s
+++ b/test/ELF/linkerscript/linker-script-in-search-path.s
@@ -4,16 +4,16 @@
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 # RUN: mkdir -p %T/searchpath
-# RUN: echo "OUTPUT(\"%t.out\")" > %T/searchpath/foo.script
-# RUN: ld.lld -T%T/searchpath/foo.script %t.o
+# RUN: echo 'OUTPUT("%t.out")' > %T/searchpath/%basename_t.script
+# RUN: ld.lld -T%T/searchpath/%basename_t.script %t.o
 # RUN: llvm-readobj %t.out | FileCheck %s
 # CHECK: Format: ELF64-x86-64
 
 # If the linker script specified with -T is missing we should emit an error
-# RUN: not ld.lld -Tfoo.script %t.o 2>&1 | FileCheck %s -check-prefix ERROR
-# ERROR: error: cannot find linker script foo.script
+# RUN: not ld.lld -T%basename_t.script %t.o 2>&1 | FileCheck %s -check-prefix ERROR
+# ERROR: error: cannot find linker script {{.*}}.script
 
 # But if it exists in the search path we should fall back to that instead:
 # RUN: rm %t.out
-# RUN: ld.lld -L %T/searchpath -Tfoo.script %t.o
+# RUN: ld.lld -L %T/searchpath -T%basename_t.script %t.o
 # RUN: llvm-readobj %t.out | FileCheck %s
diff --git a/test/ELF/linkerscript/locationcountererr2.s b/test/ELF/linkerscript/locationcountererr2.s
index 7e7bd72..9efe86a 100644
--- a/test/ELF/linkerscript/locationcountererr2.s
+++ b/test/ELF/linkerscript/locationcountererr2.s
@@ -4,8 +4,8 @@
 # RUN: echo ". = 0x150; . = 0x10; .text : {} }" >> %t.script
 # RUN: ld.lld %t.o --script %t.script -o %t -shared
 # RUN: llvm-objdump -section-headers %t | FileCheck %s
-# CHECK: Idx Name   Size      Address
-# CHECK:  3 .text 00000000 0000000000000010
+# CHECK:  Name   Size      Address
+# CHECK: .text 00000000 0000000000000010
 
 # RUN: echo "SECTIONS { . = 0x20; . = ASSERT(0x1, "foo"); }" > %t2.script
 # RUN: ld.lld %t.o --script %t2.script -o %t -shared
diff --git a/test/ELF/linkerscript/map-file2.test b/test/ELF/linkerscript/map-file2.test
index 7c4689c..d9ed339 100644
--- a/test/ELF/linkerscript/map-file2.test
+++ b/test/ELF/linkerscript/map-file2.test
@@ -28,12 +28,12 @@
 # CHECK-NEXT:       1018             3008        1     1         BYTE ( 0x11 )
 # CHECK-NEXT:       1019             3009      100     1         . += 0x100
 # CHECK-NEXT:       1119             3109        8     1         {{.*}}{{/|\\}}map-file2.test.tmp.o:(.ddd)
-# CHECK-NEXT:       1124             3114        1     4 .text
-# CHECK-NEXT:       1124             3114        1     4         {{.*}}{{/|\\}}map-file2.test.tmp.o:(.text)
-# CHECK-NEXT:       1124             3114        0     1                 f(int)
-# CHECK-NEXT:       1124             3114        0     1                 _start
 # CHECK-NEXT:       1128             3118       34     8 .eh_frame
 # CHECK-NEXT:       1128             3118       30     1         {{.*}}{{/|\\}}map-file2.test.tmp.o:(.eh_frame+0x0)
+# CHECK-NEXT:       115c             314c        1     4 .text
+# CHECK-NEXT:       115c             314c        1     4         {{.*}}{{/|\\}}map-file2.test.tmp.o:(.text)
+# CHECK-NEXT:       115c             314c        0     1                 f(int)
+# CHECK-NEXT:       115c             314c        0     1                 _start
 # CHECK-NEXT:          0                0        8     1 .comment
 # CHECK-NEXT:          0                0        8     1         <internal>:(.comment)
 # CHECK-NEXT:          0                0       48     8 .symtab
diff --git a/test/ELF/linkerscript/memory.s b/test/ELF/linkerscript/memory.s
index 1727683..0c17142 100644
--- a/test/ELF/linkerscript/memory.s
+++ b/test/ELF/linkerscript/memory.s
@@ -11,7 +11,7 @@
 # RUN: ld.lld -o %t1 --script %t.script %t
 # RUN: llvm-objdump -section-headers %t1 | FileCheck -check-prefix=RAM %s
 
-# RAM:       1 .text         00000001 0000000000008000 TEXT DATA
+# RAM:       1 .text         00000001 0000000000008000 TEXT
 # RAM-NEXT:  2 .data         00001000 0000000000008001 DATA
 
 ## Check RAM and ROM memory regions.
@@ -27,7 +27,7 @@
 # RUN: ld.lld -o %t1 --script %t.script %t
 # RUN: llvm-objdump -section-headers %t1 | FileCheck -check-prefix=RAMROM %s
 
-# RAMROM:       1 .text         00000001 0000000080000000 TEXT DATA
+# RAMROM:       1 .text         00000001 0000000080000000 TEXT
 # RAMROM-NEXT:  2 .data         00001000 0000000000000000 DATA
 
 ## Check memory region placement by attributes.
@@ -43,7 +43,7 @@
 # RUN: ld.lld -o %t1 --script %t.script %t
 # RUN: llvm-objdump -section-headers %t1 | FileCheck -check-prefix=ATTRS %s
 
-# ATTRS:  1 .text         00000001 0000000080000000 TEXT DATA
+# ATTRS:  1 .text         00000001 0000000080000000 TEXT
 # ATTRS:  2 .data         00001000 0000000000000000 DATA
 
 ## Check bad `ORIGIN`.
diff --git a/test/ELF/linkerscript/memory2.s b/test/ELF/linkerscript/memory2.s
index 2e7381f..7f86ece 100644
--- a/test/ELF/linkerscript/memory2.s
+++ b/test/ELF/linkerscript/memory2.s
@@ -2,7 +2,7 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 # RUN: echo "MEMORY { ram (rwx) : ORIGIN = 0, LENGTH = 2K } \
 # RUN: SECTIONS { .text : { *(.text*) } > ram }" > %t.script
-# RUN: ld.lld -o %t2 --script %t.script %t
+# RUN: ld.lld -o /dev/null --script %t.script %t
 
 .text
 .global _start
diff --git a/test/ELF/linkerscript/memory4.test b/test/ELF/linkerscript/memory4.test
new file mode 100644
index 0000000..e73d360
--- /dev/null
+++ b/test/ELF/linkerscript/memory4.test
@@ -0,0 +1,19 @@
+# REQUIRES: x86
+# RUN: echo ".section .text,\"ax\"; nop; .section .data,\"aw\"; nop;" \
+# RUN:   | llvm-mc -filetype=obj -triple=x86_64-pc-linux - -o %t.o
+# RUN: ld.lld -o %t.so --script %s %t.o
+# RUN: llvm-objdump -section-headers %t.so | FileCheck %s
+
+# CHECK:      1 .text         00000001 0000000000042000
+# CHECK-NEXT: 2 .data         00000001 0000000000042400
+
+## Test that address expressions changes the position in a memory region.
+
+MEMORY {
+  ram (wxa) : ORIGIN = 0x42000, LENGTH = 0x100000
+}
+SECTIONS {
+  .text : { *(.text*) }
+  data_addr = ALIGN(1024); 
+  .data data_addr : { *(.data*) }
+}
diff --git a/test/ELF/linkerscript/memory5.test b/test/ELF/linkerscript/memory5.test
new file mode 100644
index 0000000..150ddf0
--- /dev/null
+++ b/test/ELF/linkerscript/memory5.test
@@ -0,0 +1,19 @@
+# REQUIRES: x86
+# RUN: echo ".section .text,\"ax\"; nop; .section .data,\"aw\"; nop;" \
+# RUN:   | llvm-mc -filetype=obj -triple=x86_64-pc-linux - -o %t.o
+# RUN: ld.lld -o %t.so --script %s %t.o
+# RUN: llvm-objdump -section-headers %t.so | FileCheck %s
+
+# CHECK:      1 .text         00000001 0000000000042000
+# CHECK-NEXT: 2 .data         00000001 0000000000044001
+
+## Test that assign to Dot changes the position in a memory region.
+
+MEMORY {
+  ram (wxa) : ORIGIN = 0x42000, LENGTH = 0x100000
+}
+SECTIONS {
+  .text : { *(.text*) }
+  . += 0x2000;
+  .data : { *(.data*) }
+}
diff --git a/test/ELF/linkerscript/merge-sections-syms.s b/test/ELF/linkerscript/merge-sections-syms.s
index 37b25c3..421749b 100644
--- a/test/ELF/linkerscript/merge-sections-syms.s
+++ b/test/ELF/linkerscript/merge-sections-syms.s
@@ -20,7 +20,7 @@
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Symbol {
 # CHECK-NEXT:     Name: A
-# CHECK-NEXT:     Value: 0x1E2
+# CHECK-NEXT:     Value: 0x226
 # CHECK-NEXT:     Size:
 # CHECK-NEXT:     Binding:
 # CHECK-NEXT:     Type:
@@ -29,7 +29,7 @@
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Symbol {
 # CHECK-NEXT:     Name: B
-# CHECK-NEXT:     Value: 0x1E3
+# CHECK-NEXT:     Value: 0x227
 # CHECK-NEXT:     Size:
 # CHECK-NEXT:     Binding:
 # CHECK-NEXT:     Type:
diff --git a/test/ELF/linkerscript/merge-sections.s b/test/ELF/linkerscript/merge-sections.s
index e76e2cd..8fb9e87 100644
--- a/test/ELF/linkerscript/merge-sections.s
+++ b/test/ELF/linkerscript/merge-sections.s
@@ -28,8 +28,7 @@
 # CHECK-NEXT: Value: 0x[[ADDR1]]
 
 # CHECK:      Name: end
-# 0x19E = begin + sizeof(.foo) = 0x190 + 0xE
-# CHECK-NEXT: Value: 0x1F2
+# CHECK-NEXT: Value: 0x236
 
 # Check that we don't crash with --gc-sections
 # RUN: ld.lld --gc-sections -o %t2 --script %t.script %t -shared
diff --git a/test/ELF/linkerscript/no-space.s b/test/ELF/linkerscript/no-space.s
index a423a5e..7232495 100644
--- a/test/ELF/linkerscript/no-space.s
+++ b/test/ELF/linkerscript/no-space.s
@@ -3,11 +3,11 @@
 
 # RUN: echo "SECTIONS {foo 0 : {*(foo*)} .dynsym : {*(.dynsym)} .dynstr : {*(.dynstr)} }" > %t.script
 # RUN: ld.lld --hash-style=sysv -o %t --script %t.script %t.o -shared
-# RUN: llvm-readobj -elf-output-style=GNU -l %t | FileCheck %s
+# RUN: llvm-readelf -l %t | FileCheck %s
 
 # RUN: echo "SECTIONS {foo : {*(foo*)} .dynsym : { *(.dynsym) } .dynstr : { *(.dynstr) } }" > %t.script
 # RUN: ld.lld --hash-style=sysv -o %t --script %t.script %t.o -shared
-# RUN: llvm-readobj -elf-output-style=GNU -l %t | FileCheck %s
+# RUN: llvm-readelf -l %t | FileCheck %s
 
 # There is not enough address space available for the header, so just start the PT_LOAD
 # after it. Don't create a PT_PHDR as the header is not allocated.
@@ -18,7 +18,7 @@
 
 # CHECK:      Section to Segment mapping:
 # CHECK-NEXT:  Segment Sections...
-# CHECK-NEXT:   00     foo .text .hash .dynsym .dynstr
+# CHECK-NEXT:   00     foo .dynsym .dynstr .hash
 
 .section foo, "a"
 .quad 0
diff --git a/test/ELF/linkerscript/non-absolute.s b/test/ELF/linkerscript/non-absolute.s
index 6e8d3e6..b4b25a7 100644
--- a/test/ELF/linkerscript/non-absolute.s
+++ b/test/ELF/linkerscript/non-absolute.s
@@ -5,9 +5,11 @@
 # RUN: llvm-objdump -d %t | FileCheck %s --check-prefix=DUMP
 # RUN: llvm-readobj -t %t | FileCheck %s --check-prefix=SYMBOL
 
+# B = A + 0x1 = -0x10 + 0x1 = -0xf -> 0xFFFFFFFFFFFFFFF1
+# B - (0x94+6) = -0xf - (0x94+6) = -169
 # DUMP:       Disassembly of section .text:
 # DUMP-NEXT:  foo:
-# DUMP-NEXT:   50: {{.*}} -101(%rip), %eax
+# DUMP-NEXT:   94: {{.*}} -169(%rip), %eax
 
 # SYMBOL:     Symbol {
 # SYMBOL:        Name: B
diff --git a/test/ELF/linkerscript/non-absolute2.test b/test/ELF/linkerscript/non-absolute2.test
index 8935602..b606664 100644
--- a/test/ELF/linkerscript/non-absolute2.test
+++ b/test/ELF/linkerscript/non-absolute2.test
@@ -9,10 +9,9 @@
 }
 
 # CHECK:       Sections:
-# CHECK-NEXT:  Idx Name          Size      Address          Type
-# CHECK-NEXT:    0               00000000 0000000000000000 
-# CHECK-NEXT:    1 .dynsym       00000030 0000000000001000 
-# CHECK-NEXT:    2 .dynstr       00000003 0000000000001030 
-# CHECK-NEXT:    3 .text         00000000 0000000000001034
+# CHECK-NEXT:  Idx Name          Size      Address
+# CHECK-NEXT:    0               00000000 0000000000000000
+# CHECK-NEXT:    1 .dynsym       00000030 0000000000001000
+# CHECK:         5 .text         00000000 000000000000106c
 
 # CHECK: 0000000000000001         .dynsym            00000000 A
diff --git a/test/ELF/linkerscript/non-alloc-segment.s b/test/ELF/linkerscript/non-alloc-segment.s
index 229f028..d9984b3 100644
--- a/test/ELF/linkerscript/non-alloc-segment.s
+++ b/test/ELF/linkerscript/non-alloc-segment.s
@@ -16,7 +16,7 @@
 # RUN:           .foo : {*(.foo)} :foo \
 # RUN:       }" > %t.script
 # RUN: ld.lld -o %t --script %t.script %t.o
-# RUN: llvm-readobj -elf-output-style=GNU -s -l %t | FileCheck %s
+# RUN: llvm-readelf -s -l %t | FileCheck %s
 # RUN: llvm-readobj -l %t | FileCheck --check-prefix=PHDR %s
 
 # CHECK: Program Headers:
diff --git a/test/ELF/linkerscript/non-alloc.s b/test/ELF/linkerscript/non-alloc.s
index a40d29d..87f9aff 100644
--- a/test/ELF/linkerscript/non-alloc.s
+++ b/test/ELF/linkerscript/non-alloc.s
@@ -1,9 +1,9 @@
 # REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 
 # RUN: echo "SECTIONS { .foo 0 : {*(foo)} }" > %t.script
-# RUN: ld.lld --hash-style=sysv -o %t1 --script %t.script %t -shared
-# RUN: llvm-readobj -elf-output-style=GNU -s -l %t1 | FileCheck %s
+# RUN: ld.lld --hash-style=sysv -o %t --script %t.script %t.o -shared
+# RUN: llvm-readelf -s -l %t | FileCheck %s
 
 # Test that we create all necessary PT_LOAD. We use to stop at the first
 # non-alloc, causing us to not create PT_LOAD for linker generated sections.
@@ -15,7 +15,7 @@
 
 # CHECK:      Section to Segment mapping:
 # CHECK-NEXT:  Segment Sections...
-# CHECK-NEXT:   00     .dynsym .dynstr .text .hash 
+# CHECK-NEXT:   00     .dynsym .hash .dynstr .text
 # CHECK-NEXT:   01     .dynamic
 
 nop
diff --git a/test/ELF/linkerscript/numbers.s b/test/ELF/linkerscript/numbers.s
index d4fd13f..98d7e33 100644
--- a/test/ELF/linkerscript/numbers.s
+++ b/test/ELF/linkerscript/numbers.s
@@ -29,12 +29,12 @@
 
 ## Mailformed number errors.
 # RUN: echo "SECTIONS { . = 0x11h; }" > %t2.script
-# RUN: not ld.lld %t --script %t2.script -o %t3 2>&1 | \
+# RUN: not ld.lld %t --script %t2.script -o /dev/null 2>&1 | \
 # RUN:  FileCheck --check-prefix=ERR1 %s
 # ERR1: malformed number: 0x11h
 
 # RUN: echo "SECTIONS { . = 0x11k; }" > %t3.script
-# RUN: not ld.lld %t --script %t3.script -o %t4 2>&1 | \
+# RUN: not ld.lld %t --script %t3.script -o /dev/null 2>&1 | \
 # RUN:  FileCheck --check-prefix=ERR2 %s
 # ERR2: malformed number: 0x11k
 
@@ -43,13 +43,28 @@
 # RUN:  FileCheck --check-prefix=ERR3 %s
 # ERR3: malformed number: 0x11m
 
+# RUN: echo "SECTIONS { . = 1zh; }" > %t5.script
+# RUN: not ld.lld %t --script %t5.script -o %t5 2>&1 | \
+# RUN:  FileCheck --check-prefix=ERR4 %s
+# ERR4: malformed number: 1zh
+
+# RUN: echo "SECTIONS { . = 1zk; }" > %t6.script
+# RUN: not ld.lld %t --script %t6.script -o %t6 2>&1 | \
+# RUN:  FileCheck --check-prefix=ERR5 %s
+# ERR5: malformed number: 1zk
+
+# RUN: echo "SECTIONS { . = 1zm; }" > %t7.script
+# RUN: not ld.lld %t --script %t7.script -o /dev/null 2>&1 | \
+# RUN:  FileCheck --check-prefix=ERR6 %s
+# ERR6: malformed number: 1zm
+
 ## Make sure that numbers can be followed by a ":" with and without a space,
 ## e.g. "0x100 :" or "0x100:"
 # RUN: echo "SECTIONS { \
 # RUN:  .hex1 0x400 : { *(.hex.1) } \
 # RUN:  .hex2 0x500:{ *(.hex.2) } \
-# RUN: }" > %t5.script
-# RUN: ld.lld %t --script %t5.script -o %t6
+# RUN: }" > %t8.script
+# RUN: ld.lld %t --script %t8.script -o %t6
 # RUN: llvm-objdump -section-headers %t6 | FileCheck -check-prefix=SECADDR %s
 # SECADDR:     Sections:
 # SECADDR-NEXT: Idx Name          Size      Address
diff --git a/test/ELF/linkerscript/openbsd-bootdata.test b/test/ELF/linkerscript/openbsd-bootdata.test
index ad3a569..6846c7f 100644
--- a/test/ELF/linkerscript/openbsd-bootdata.test
+++ b/test/ELF/linkerscript/openbsd-bootdata.test
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux /dev/null -o %t.o
 # RUN: ld.lld --script %s %t.o -o %t
 # RUN: llvm-readobj --program-headers -s %t | FileCheck %s
diff --git a/test/ELF/linkerscript/openbsd-randomize.s b/test/ELF/linkerscript/openbsd-randomize.s
index bf885f4..575a6b2 100644
--- a/test/ELF/linkerscript/openbsd-randomize.s
+++ b/test/ELF/linkerscript/openbsd-randomize.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %s -o %t.o
 # RUN: echo "PHDRS { text PT_LOAD FILEHDR PHDRS; rand PT_OPENBSD_RANDOMIZE; } \
 # RUN:       SECTIONS { . = SIZEOF_HEADERS; \
diff --git a/test/ELF/linkerscript/openbsd-wxneeded.test b/test/ELF/linkerscript/openbsd-wxneeded.test
index 3b82a94..1868c0e 100644
--- a/test/ELF/linkerscript/openbsd-wxneeded.test
+++ b/test/ELF/linkerscript/openbsd-wxneeded.test
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux /dev/null -o %t.o
 # RUN: ld.lld -z wxneeded --script %s %t.o -o %t
 # RUN: llvm-readobj --program-headers %t | FileCheck %s
diff --git a/test/ELF/linkerscript/operators.test b/test/ELF/linkerscript/operators.test
index 7996044..2be24df 100644
--- a/test/ELF/linkerscript/operators.test
+++ b/test/ELF/linkerscript/operators.test
@@ -38,6 +38,14 @@
   minus_abs = _end - _start;
   max = MAX(11, 22);
   min = MIN(11, 22);
+  logicaland1 = 0 && 0;
+  logicaland2 = 0 && 1;
+  logicaland3 = 1 && 0;
+  logicaland4 = 1 && 1;
+  logicalor1 = 0 || 0;
+  logicalor2 = 0 || 1;
+  logicalor3 = 1 || 0;
+  logicalor4 = 1 || 1;
 }
 
 # CHECK: 00000000000006 *ABS* 00000000 plus
@@ -70,6 +78,14 @@
 # CHECK: 0000000000fff0 *ABS* 00000000 minus_abs
 # CHECK: 00000000000016 *ABS* 00000000 max
 # CHECK: 0000000000000b *ABS* 00000000 min
+# CHECK: 00000000000000 *ABS* 00000000 logicaland1
+# CHECK: 00000000000000 *ABS* 00000000 logicaland2
+# CHECK: 00000000000000 *ABS* 00000000 logicaland3
+# CHECK: 00000000000001 *ABS* 00000000 logicaland4
+# CHECK: 00000000000000 *ABS* 00000000 logicalor1
+# CHECK: 00000000000001 *ABS* 00000000 logicalor2
+# CHECK: 00000000000001 *ABS* 00000000 logicalor3
+# CHECK: 00000000000001 *ABS* 00000000 logicalor4
 
 ## Mailformed number error.
 # RUN: echo "SECTIONS { . = 0x12Q41; }" > %t.script
diff --git a/test/ELF/linkerscript/orphan-first-cmd.test b/test/ELF/linkerscript/orphan-first-cmd.test
index 31bff59..84b183f 100644
--- a/test/ELF/linkerscript/orphan-first-cmd.test
+++ b/test/ELF/linkerscript/orphan-first-cmd.test
@@ -17,4 +17,4 @@
 # CHECK-NEXT:   SHF_ALLOC
 # CHECK-NEXT:   SHF_EXECINSTR
 # CHECK-NEXT: ]
-# CHECK-NEXT: Address: 0x1038
+# CHECK-NEXT: Address: 0x1070
diff --git a/test/ELF/linkerscript/orphan-phdrs.s b/test/ELF/linkerscript/orphan-phdrs.s
index 2d5f4aa..f9d1467 100644
--- a/test/ELF/linkerscript/orphan-phdrs.s
+++ b/test/ELF/linkerscript/orphan-phdrs.s
@@ -10,7 +10,7 @@
 # RUN:  .rw : { *(.rw) } \
 # RUN: }" > %t.script
 # RUN: ld.lld -o %t --script %t.script %t.o
-# RUN: llvm-readobj -elf-output-style=GNU -s -l %t | FileCheck %s
+# RUN: llvm-readelf -s -l %t | FileCheck %s
 
 ## Check that the orphan section is placed correctly and belongs to
 ## the correct segment.
diff --git a/test/ELF/linkerscript/orphan-report.s b/test/ELF/linkerscript/orphan-report.s
index 241857b..903ef79 100644
--- a/test/ELF/linkerscript/orphan-report.s
+++ b/test/ELF/linkerscript/orphan-report.s
@@ -36,6 +36,7 @@
 # REPORT-NEXT: <internal>:(.plt) is being placed in '.plt'
 # REPORT-NEXT: <internal>:(.eh_frame) is being placed in '.eh_frame'
 # REPORT-NEXT: <internal>:(.symtab) is being placed in '.symtab'
+# REPORT-NEXT: <internal>:(.symtab_shndxr) is being placed in '.symtab_shndxr'
 # REPORT-NEXT: <internal>:(.shstrtab) is being placed in '.shstrtab'
 # REPORT-NEXT: <internal>:(.strtab) is being placed in '.strtab'
 
diff --git a/test/ELF/linkerscript/orphan.s b/test/ELF/linkerscript/orphan.s
index f510853..4dbaf37 100644
--- a/test/ELF/linkerscript/orphan.s
+++ b/test/ELF/linkerscript/orphan.s
@@ -13,7 +13,7 @@
 ## .bss is SHT_NOBITS section and should be last RW section, so some space
 ## in ELF file could be saved.
 # CHECK:       0               00000000 0000000000000000
-# CHECK-NEXT:  1 .text         00000000 0000000000000000 TEXT DATA
+# CHECK-NEXT:  1 .text         00000000 0000000000000000 TEXT
 # CHECK-NEXT:  2 .rw1          00000008 0000000000000000 DATA
 # CHECK-NEXT:  3 .rw2          00000008 0000000000000008 DATA
 # CHECK-NEXT:  4 .rw3          00000008 0000000000000010 DATA
diff --git a/test/ELF/linkerscript/out-of-order.s b/test/ELF/linkerscript/out-of-order.s
index c76604a..da8c103 100644
--- a/test/ELF/linkerscript/out-of-order.s
+++ b/test/ELF/linkerscript/out-of-order.s
@@ -26,10 +26,10 @@
 # CHECK-NEXT:   0               00000000 0000000000000000
 # CHECK-NEXT:   1 .data         00000008 0000000000004000
 # CHECK-NEXT:   2 .dynamic      00000060 0000000000004008
-# CHECK-NEXT:   3 .dynsym       00000018 0000000000002000 
+# CHECK-NEXT:   3 .dynsym       00000018 0000000000002000
 # CHECK-NEXT:   4 .dynstr       00000001 0000000000002018
-# CHECK-NEXT:   5 .text         00000008 000000000000201c
-# CHECK-NEXT:   6 .hash         00000010 0000000000002024
+# CHECK-NEXT:   5 .hash         00000010 000000000000201c
+# CHECK-NEXT:   6 .text         00000008 000000000000202c
 
 .quad 0
 .data
diff --git a/test/ELF/linkerscript/output-too-large.s b/test/ELF/linkerscript/output-too-large.s
index 5b9c04a..ca85465 100644
--- a/test/ELF/linkerscript/output-too-large.s
+++ b/test/ELF/linkerscript/output-too-large.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %s -o %t.o
 # RUN: echo "SECTIONS { .text : { . = 0xffffffff; *(.text*); } }" > %t.script
-# RUN: not ld.lld --no-check-sections --script %t.script %t.o -o %t 2>&1 | FileCheck %s
+# RUN: not ld.lld --no-check-sections --script %t.script %t.o -o /dev/null 2>&1 | FileCheck %s
 # CHECK: error: output file too large
 
 .global _start
diff --git a/test/ELF/linkerscript/overlapping-sections.s b/test/ELF/linkerscript/overlapping-sections.s
index 98fa374..818301f 100644
--- a/test/ELF/linkerscript/overlapping-sections.s
+++ b/test/ELF/linkerscript/overlapping-sections.s
@@ -23,7 +23,7 @@
 # BAD-LMA: .sec2             PROGBITS        0000000000008800 002800 000100 00  WA  0   0  1
 # BAD-LMA-LABEL: Program Headers:
 # BAD-LMA-NEXT:  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
-# BAD-LMA-NEXT:  LOAD           0x001000 0x0000000000000000 0x0000000000000000 0x000104 0x000104 R E 0x1000
+# BAD-LMA-NEXT:  LOAD           0x001000 0x0000000000000000 0x0000000000000000 0x000100 0x000100 R E 0x1000
 # BAD-LMA-NEXT:  LOAD           0x002000 0x0000000000008000 0x0000000000008000 0x000100 0x000100 RW  0x1000
 # BAD-LMA-NEXT:  LOAD           0x002800 0x0000000000008800 0x0000000000008080 0x000170 0x000170 RW  0x1000
 # BAD-LMA-LABEL: Section to Segment mapping:
@@ -49,7 +49,7 @@
 # BAD-VADDR: .sec2             PROGBITS        0000000000008020 003020 000100 00  WA  0   0  1
 # BAD-VADDR-LABEL: Program Headers:
 # BAD-VADDR-NEXT:  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
-# BAD-VADDR-NEXT:  LOAD           0x001000 0x0000000000000000 0x0000000000000000 0x000104 0x000104 R E 0x1000
+# BAD-VADDR-NEXT:  LOAD           0x001000 0x0000000000000000 0x0000000000000000 0x000100 0x000100 R E 0x1000
 # BAD-VADDR-NEXT:  LOAD           0x002000 0x0000000000008000 0x0000000000008000 0x000100 0x000100 RW  0x1000
 # BAD-VADDR-NEXT:  LOAD           0x003020 0x0000000000008020 0x0000000000008800 0x000170 0x000170 RW  0x1000
 # BAD-VADDR-LABEL: Section to Segment mapping:
@@ -97,7 +97,7 @@
 # BAD-BOTH: .sec2             PROGBITS        0000000000008040 002040 000100 00  WA  0   0  1
 # BAD-BOTH-LABEL: Program Headers:
 # BAD-BOTH-NEXT:  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
-# BAD-BOTH-NEXT:  LOAD 0x001000 0x0000000000000000 0x0000000000000000 0x000104 0x000104 R E 0x1000
+# BAD-BOTH-NEXT:  LOAD 0x001000 0x0000000000000000 0x0000000000000000 0x000100 0x000100 R E 0x1000
 # BAD-BOTH-NEXT:  LOAD           0x002000 0x0000000000008000 0x0000000000008000 0x0001b0 0x0001b0 RW  0x1000
 # BAD-BOTH-LABEL: Section to Segment mapping:
 # BAD-BOTH:   01     .sec1 .sec2 .dynamic
diff --git a/test/ELF/linkerscript/overlay-reject.test b/test/ELF/linkerscript/overlay-reject.test
new file mode 100644
index 0000000..fcb82b6
--- /dev/null
+++ b/test/ELF/linkerscript/overlay-reject.test
@@ -0,0 +1,13 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux /dev/null -o %t.o
+# RUN: not ld.lld %t.o --script %s -o %t 2>&1 | FileCheck %s
+
+# CHECK:      {{.*}}.test:{{.*}}: { expected, but got 0x3000
+# CHECK-NEXT: >>>     .out.aaa 0x3000 : { *(.aaa) }
+# CHECK-NEXT: >>>              ^
+
+SECTIONS {
+  OVERLAY 0x1000 : AT ( 0x2000 ) {
+    .out.aaa 0x3000 : { *(.aaa) } 
+  } 
+}
diff --git a/test/ELF/linkerscript/overlay-reject2.test b/test/ELF/linkerscript/overlay-reject2.test
new file mode 100644
index 0000000..490533c
--- /dev/null
+++ b/test/ELF/linkerscript/overlay-reject2.test
@@ -0,0 +1,17 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux /dev/null -o %t.o
+# RUN: not ld.lld %t.o --script %s -o %t 2>&1 | FileCheck %s
+
+# CHECK:      {{.*}}.test:{{.*}}: { expected, but got AX
+# CHECK-NEXT: >>>      .out.aaa { *(.aaa) } > AX AT>FLASH
+# CHECK-NEXT: >>>                             ^
+
+MEMORY {
+  AX (ax)    : ORIGIN = 0x3000, LENGTH = 0x4000
+}
+
+SECTIONS {
+  OVERLAY 0x1000 : AT ( 0x2000 ) {
+    .out.aaa { *(.aaa) } > AX AT>FLASH
+  }
+}
diff --git a/test/ELF/linkerscript/overlay.test b/test/ELF/linkerscript/overlay.test
new file mode 100644
index 0000000..a28ab61
--- /dev/null
+++ b/test/ELF/linkerscript/overlay.test
@@ -0,0 +1,30 @@
+# REQUIRES: x86
+# RUN: echo 'nop; .section .small, "a"; .long 0; .section .big, "a"; .quad 1;' \
+# RUN:   | llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %t.o
+# RUN: ld.lld %t.o --script %s -o %t
+
+SECTIONS {
+  OVERLAY 0x1000 : AT ( 0x4000 ) {
+    .out.big { *(.big) } 
+    .out.small { *(.small) } 
+  } 
+}
+
+## Here we check that can handle OVERLAY which will produce sections 
+## .out.big and .out.small with the same starting VAs, but different LMAs.
+## Section .big is larger than .small, we check that placing of section
+## .text does not cause overlapping error and that
+## .text's VA is 0x1000 + max(sizeof(.out.big), sizeof(.out.small)).
+
+# RUN: llvm-readobj -sections -program-headers -elf-output-style=GNU %t | FileCheck %s
+
+# CHECK: Section Headers:
+# CHECK: Name       Type     Address          Off    Size
+# CHECK: .out.big   PROGBITS 0000000000001000 001000 000008
+# CHECK: .out.small PROGBITS 0000000000001000 002000 000004
+# CHECK: .text      PROGBITS 0000000000001008 002008 000001
+
+# CHECK:      Program Headers:
+# CHECK:      Type Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
+# CHECK-NEXT: LOAD 0x001000 0x0000000000001000 0x0000000000004000 0x000008 0x000008 R E 0x1000
+# CHECK-NEXT: LOAD 0x002000 0x0000000000001000 0x0000000000004008 0x000009 0x000009 R E 0x1000
diff --git a/test/ELF/linkerscript/phdrs.s b/test/ELF/linkerscript/phdrs.s
index b650159..c688bef 100644
--- a/test/ELF/linkerscript/phdrs.s
+++ b/test/ELF/linkerscript/phdrs.s
@@ -130,6 +130,14 @@
 
 # BADHDR:       {{.*}}.script:1: section header 'bar' is not listed in PHDRS
 
+# RUN: echo "PHDRS { text PT_LOAD FOOHDR; }" > %t1.script
+# RUN: not ld.lld -o /dev/null --script %t1.script %t 2>&1 | FileCheck --check-prefix=FOOHDR %s
+# FOOHDR: error: {{.*}}.script:1: unexpected header attribute: FOOHDR
+
+# RUN: echo "PHDRS { text PT_FOO FOOHDR; }" > %t1.script
+# RUN: not ld.lld -o /dev/null --script %t1.script %t 2>&1 | FileCheck --check-prefix=PTFOO %s
+# PTFOO: invalid program header type: PT_FOO
+
 .global _start
 _start:
  nop
diff --git a/test/ELF/linkerscript/provide-empty-section.s b/test/ELF/linkerscript/provide-empty-section.s
new file mode 100644
index 0000000..56cb6ac
--- /dev/null
+++ b/test/ELF/linkerscript/provide-empty-section.s
@@ -0,0 +1,30 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %tundefined.o
+# RUN: echo "foo=42" | llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %tdefined.o
+# RUN: echo "call foo" | llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %treference.o
+
+# RUN: echo "SECTIONS { .bar : { PROVIDE(foo = .); } }" > %t.script
+
+# Case 1: Provided symbol is undefined and not referenced - empty section should be removed.
+# RUN: ld.lld %tundefined.o -T %t.script -o %t1.elf
+# RUN: llvm-readobj -sections %t1.elf | FileCheck %s --check-prefix=NOSECTION
+
+# Case 2: Provided symbol is undefined and referenced - empty section should not be removed.
+# RUN: ld.lld %tundefined.o %treference.o -T %t.script -o %t2.elf
+# RUN: llvm-readobj -sections %t2.elf | FileCheck %s --check-prefix=SECTION
+
+# Case 3: Provided symbol is defined and not referenced - empty section should be removed.
+# RUN: ld.lld %tdefined.o -T %t.script -o %t3.elf
+# RUN: llvm-readobj -sections %t3.elf | FileCheck %s --check-prefix=NOSECTION
+
+# Case 4: Provided symbol is defined and referenced - empty section should not be removed.
+# RUN: ld.lld %tdefined.o %treference.o -T %t.script -o %t4.elf
+# RUN: llvm-readobj -sections %t4.elf | FileCheck %s --check-prefix=SECTION
+
+.global _start
+_start:
+    ret
+
+# SECTION: .bar
+# NOSECTION-NOT: .bar
diff --git a/test/ELF/linkerscript/pt-interp.test b/test/ELF/linkerscript/pt-interp.test
new file mode 100644
index 0000000..0441817
--- /dev/null
+++ b/test/ELF/linkerscript/pt-interp.test
@@ -0,0 +1,21 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux /dev/null -o %t.o
+# RUN: ld.lld -o %t.so -shared %t.o
+
+## Check we create PT_INTERP program header when it is specified in PHDRS.
+# RUN: echo "PHDRS { interp PT_INTERP; }" > %t1.script
+# RUN: ld.lld -o %t1 --script %t1.script %t.o %t.so --dynamic-linker foo
+# RUN: llvm-readobj -program-headers %t1 | FileCheck %s
+# CHECK: PT_INTERP
+
+## Check we do not create it if it is not specified,
+## but only if PHDRS is not empty by itself.
+# RUN: echo "PHDRS { ph_text PT_LOAD; }" > %t2.script
+# RUN: ld.lld -o %t1 --script %t2.script %t.o %t.so --dynamic-linker foo
+# RUN: llvm-readobj -program-headers %t1 | FileCheck %s --check-prefix=NOINTERP
+# NOINTERP-NOT: PT_INTERP
+
+## Otherwise, if PHDRS is empty, we create PT_INTERP header.
+# RUN: echo "PHDRS {}" > %t3.script
+# RUN: ld.lld -o %t1 --script %t3.script %t.o %t.so --dynamic-linker foo
+# RUN: llvm-readobj -program-headers %t1 | FileCheck %s
diff --git a/test/ELF/linkerscript/pt_gnu_eh_frame.s b/test/ELF/linkerscript/pt_gnu_eh_frame.s
index 81b4c63..7f9ebaa 100644
--- a/test/ELF/linkerscript/pt_gnu_eh_frame.s
+++ b/test/ELF/linkerscript/pt_gnu_eh_frame.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 # RUN: echo "SECTIONS { /DISCARD/ : { *(.eh_frame*) *(.eh_frame_hdr*) } }" > %t.script
-# RUN: ld.lld -o %t1 --eh-frame-hdr --script %t.script %t
+# RUN: ld.lld -o /dev/null --eh-frame-hdr --script %t.script %t
 
 .global _start
 _start:
diff --git a/test/ELF/linkerscript/region-alias.s b/test/ELF/linkerscript/region-alias.s
index 8a88f6f..af4a0f3 100644
--- a/test/ELF/linkerscript/region-alias.s
+++ b/test/ELF/linkerscript/region-alias.s
@@ -15,7 +15,7 @@
 # RUN: echo "REGION_ALIAS (\"ALIAS_DATA\", RAM);" >> %t.script.inc
 # RUN: ld.lld %t --script %t.script -o %t2
 # RUN: llvm-objdump -section-headers %t2 | FileCheck %s
-# CHECK: .text       00000001 0000000000001000 TEXT DATA
+# CHECK: .text       00000001 0000000000001000 TEXT
 # CHECK: .data       00000008 0000000000002000 DATA
 
 ## All to ROM.
@@ -23,7 +23,7 @@
 # RUN: echo "REGION_ALIAS (\"ALIAS_DATA\", ROM);" >> %t.script.inc
 # RUN: ld.lld %t --script %t.script -o %t2
 # RUN: llvm-objdump -section-headers %t2 | FileCheck %s --check-prefix=RAM
-# RAM: .text         00000001 0000000000001000 TEXT DATA
+# RAM: .text         00000001 0000000000001000 TEXT
 # RAM: .data         00000008 0000000000001001 DATA
 
 ## Redefinition of region.
diff --git a/test/ELF/linkerscript/section-metadata.s b/test/ELF/linkerscript/section-metadata.s
index f447240..44547b8 100644
--- a/test/ELF/linkerscript/section-metadata.s
+++ b/test/ELF/linkerscript/section-metadata.s
@@ -10,15 +10,15 @@
 # RUN: llvm-objdump -s %t | FileCheck --check-prefix=INV %s
 
 
-# CHECK:      Contents of section .text:
-# CHECK-NEXT: 02000000 00000000 01000000 00000000
 # CHECK:      Contents of section .rodata:
 # CHECK-NEXT: 02000000 00000000 01000000 00000000
+# CHECK:      Contents of section .text:
+# CHECK-NEXT: 02000000 00000000 01000000 00000000
 
-# INV:      Contents of section .text:
-# INV-NEXT: 01000000 00000000 02000000 00000000
 # INV:      Contents of section .rodata:
 # INV-NEXT: 01000000 00000000 02000000 00000000
+# INV:      Contents of section .text:
+# INV-NEXT: 01000000 00000000 02000000 00000000
 
 .global _start
 _start:
diff --git a/test/ELF/linkerscript/section-metadata2.s b/test/ELF/linkerscript/section-metadata2.s
index 6302492..4a538b6 100644
--- a/test/ELF/linkerscript/section-metadata2.s
+++ b/test/ELF/linkerscript/section-metadata2.s
@@ -7,20 +7,20 @@
 # RUN: ld.lld --symbol-ordering-file %t.ord -o %t --script %t.script %t.o
 # RUN: llvm-objdump -s %t | FileCheck %s
 
-# CHECK:      Contents of section .text:
-# CHECK-NEXT: 02000000 00000000 01000000 00000000
 # CHECK:      Contents of section .rodata:
 # CHECK-NEXT: 02000000 00000000 01000000 00000000
+# CHECK:      Contents of section .text:
+# CHECK-NEXT: 02000000 00000000 01000000 00000000
 
 # RUN: echo "_foo" > %t.ord
 # RUN: echo "_bar" >> %t.ord
 # RUN: ld.lld --symbol-ordering-file %t.ord -o %t --script %t.script %t.o
 # RUN: llvm-objdump -s %t | FileCheck %s --check-prefix=INV
 
-# INV:      Contents of section .text:
-# INV-NEXT: 01000000 00000000 02000000 00000000
 # INV:      Contents of section .rodata:
 # INV-NEXT: 01000000 00000000 02000000 00000000
+# INV:      Contents of section .text:
+# INV-NEXT: 01000000 00000000 02000000 00000000
 
 .section .text.foo,"a",@progbits
 _foo:
diff --git a/test/ELF/linkerscript/sections-keep.s b/test/ELF/linkerscript/sections-keep.s
index feb0bac..2c778e3 100644
--- a/test/ELF/linkerscript/sections-keep.s
+++ b/test/ELF/linkerscript/sections-keep.s
@@ -1,14 +1,14 @@
 # REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
-# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/keep.s -o %t2.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/keep.s -o %t1.o
 
 ## First check that section "keep" is garbage collected without using KEEP
 # RUN: echo "SECTIONS { \
 # RUN:  .text : { *(.text) } \
 # RUN:  .keep : { *(.keep) } \
 # RUN:  .temp : { *(.temp) }}" > %t.script
-# RUN: ld.lld --gc-sections -o %t1 --script %t.script %t
-# RUN: llvm-objdump -section-headers %t1 | \
+# RUN: ld.lld --gc-sections -o %t --script %t.script %t.o
+# RUN: llvm-objdump -section-headers %t | \
 # RUN:   FileCheck -check-prefix=SECGC %s
 # SECGC:      Sections:
 # SECGC-NEXT: Idx Name          Size
@@ -21,8 +21,8 @@
 # RUN:  .text : { *(.text) } \
 # RUN:  .keep : { KEEP(*(.keep)) } \
 # RUN:  .temp : { *(.temp) }}" > %t.script
-# RUN: ld.lld --gc-sections -o %t1 --script %t.script %t
-# RUN: llvm-objdump -section-headers %t1 | \
+# RUN: ld.lld --gc-sections -o %t --script %t.script %t.o
+# RUN: llvm-objdump -section-headers %t | \
 # RUN:   FileCheck -check-prefix=SECNOGC %s
 # SECNOGC:      Sections:
 # SECNOGC-NEXT: Idx Name          Size
@@ -38,14 +38,14 @@
 # RUN:  . = SIZEOF_HEADERS; \
 # RUN:  .keep : { KEEP(*(.keep)) } \
 # RUN:  .nokeep : { *(.keep) }}" > %t.script
-# RUN: ld.lld --gc-sections -o %t1 --script %t.script %t
-# RUN: llvm-objdump -section-headers %t1 | FileCheck -check-prefix=MIXED1 %s
+# RUN: ld.lld --gc-sections -o %t --script %t.script %t.o
+# RUN: llvm-objdump -section-headers %t | FileCheck -check-prefix=MIXED1 %s
 # MIXED1:      Sections:
 # MIXED1-NEXT: Idx Name          Size
 # MIXED1-NEXT:   0               00000000
 # MIXED1-NEXT:   1 .keep         00000004
-# MIXED1-NEXT:   2 .text         00000007 00000000000000ec TEXT DATA
-# MIXED1-NEXT:   3 .temp         00000004 00000000000000f3 DATA
+# MIXED1-NEXT:   2 .temp         00000004 00000000000000ec
+# MIXED1-NEXT:   3 .text         00000007 00000000000000f0
 # MIXED1-NEXT:   4 .comment      00000008 0000000000000000
 # MIXED1-NEXT:   5 .symtab       00000060 0000000000000000
 # MIXED1-NEXT:   6 .shstrtab     00000036 0000000000000000
@@ -59,14 +59,14 @@
 # RUN:  . = SIZEOF_HEADERS; \
 # RUN:  .nokeep : { *(.keep) } \
 # RUN:  .keep : { KEEP(*(.keep)) }}" > %t.script
-# RUN: ld.lld --gc-sections -o %t1 --script %t.script %t
-# RUN: llvm-objdump -section-headers %t1 | FileCheck -check-prefix=MIXED2 %s
+# RUN: ld.lld --gc-sections -o %t --script %t.script %t.o
+# RUN: llvm-objdump -section-headers %t | FileCheck -check-prefix=MIXED2 %s
 # MIXED2:      Sections:
 # MIXED2-NEXT: Idx Name          Size
 # MIXED2-NEXT:   0               00000000
-# MIXED2-NEXT:   1 .nokeep       00000004 00000000000000e8 DATA
-# MIXED2-NEXT:   2 .text         00000007 00000000000000ec TEXT DATA
-# MIXED2-NEXT:   3 .temp         00000004 00000000000000f3 DATA
+# MIXED2-NEXT:   1 .nokeep       00000004 00000000000000e8
+# MIXED2-NEXT:   2 .temp         00000004 00000000000000ec
+# MIXED2-NEXT:   3 .text         00000007 00000000000000f0
 # MIXED2-NEXT:   4 .comment      00000008 0000000000000000
 # MIXED2-NEXT:   5 .symtab       00000060 0000000000000000
 # MIXED2-NEXT:   6 .shstrtab     00000038 0000000000000000
@@ -75,10 +75,10 @@
 # Check file pattern for kept sections.
 # RUN: echo "SECTIONS { \
 # RUN:  . = SIZEOF_HEADERS; \
-# RUN:  .keep : { KEEP(*2.o(.keep)) } \
+# RUN:  .keep : { KEEP(*1.o(.keep)) } \
 # RUN:  }" > %t.script
-# RUN: ld.lld --gc-sections -o %t1 --script %t.script %t2.o %t
-# RUN: llvm-objdump -s %t1 | FileCheck -check-prefix=FILEMATCH %s
+# RUN: ld.lld --gc-sections -o %t --script %t.script %t1.o %t.o
+# RUN: llvm-objdump -s %t | FileCheck -check-prefix=FILEMATCH %s
 # FILEMATCH:        Contents of section .keep:
 # FILEMATCH-NEXT:   00e8 41414141  AAAA
 
diff --git a/test/ELF/linkerscript/sections-max-va-overflow.s b/test/ELF/linkerscript/sections-max-va-overflow.s
index e8fcd8d..ce771b4 100644
--- a/test/ELF/linkerscript/sections-max-va-overflow.s
+++ b/test/ELF/linkerscript/sections-max-va-overflow.s
@@ -3,7 +3,7 @@
 
 # RUN: echo "SECTIONS { . = 0xfffffffffffffff1;" > %t.script
 # RUN: echo "           .bar : { *(.bar*) } }" >> %t.script
-# RUN: not ld.lld -o %t.so --script %t.script %t.o 2>&1 | FileCheck %s -check-prefix=ERR
+# RUN: not ld.lld -o /dev/null --script %t.script %t.o 2>&1 | FileCheck %s -check-prefix=ERR
 
 ## .bar section has data in [0xfffffffffffffff1, 0xfffffffffffffff1 + 0x10] ==
 ## [0xfffffffffffffff1, 0x1]. Check we can catch this overflow.
diff --git a/test/ELF/linkerscript/sections-sort.s b/test/ELF/linkerscript/sections-sort.s
index dd93f5a..e665c9a 100644
--- a/test/ELF/linkerscript/sections-sort.s
+++ b/test/ELF/linkerscript/sections-sort.s
@@ -13,13 +13,13 @@
 .section foo, "a"
 .byte 0
 
-# CHECK: Id
+# CHECK: Idx
 # CHECK-NEXT: 0
-# CHECK-NEXT: 1 .dynsym
-# CHECK-NEXT: 2 .dynstr
-# CHECK-NEXT: 3 .text
-# CHECK-NEXT: 4 foo
-# CHECK-NEXT: 5 .hash
+# CHECK-NEXT: 1 .text
+# CHECK-NEXT: 2 .dynsym
+# CHECK-NEXT: 3 .hash
+# CHECK-NEXT: 4 .dynstr
+# CHECK-NEXT: 5 foo
 # CHECK-NEXT: 6 .dynamic
 # CHECK-NEXT: 7 .comment
 # CHECK-NEXT: 8 .symtab
diff --git a/test/ELF/linkerscript/sections-va-overflow.test b/test/ELF/linkerscript/sections-va-overflow.test
index 7ede6ec..142d2e5 100644
--- a/test/ELF/linkerscript/sections-va-overflow.test
+++ b/test/ELF/linkerscript/sections-va-overflow.test
@@ -7,7 +7,7 @@
   ph_text PT_LOAD FILEHDR PHDRS FLAGS (0x1 | 0x4);
 }
 
-SECTIONS { 
+SECTIONS {
  . = 0xffffffff20000000;
  .text : { *(.text*) } : ph_text
  .test 0x1000 : { BYTE(0) }
@@ -18,5 +18,5 @@
 ## with VA 0xffffffff20000000. That might be technically correct, but most probably
 ## is a result of a broken script file and causes file offset calculation overflow.
 ## It seems we do not have to support it, so we don't and we report an error in this case.
-# ERR: error: unable to place section .text at file offset [0xFFFFFFFF20000000, 0xFFFFFFFE40000000]; check your linker script for overflows
+# ERR: error: unable to place section .text at file offset [0xFFFFFFFF20000000, 0xFFFFFFFF20000000]; check your linker script for overflows
 # ERR-NOT: unable to place section .bss
diff --git a/test/ELF/linkerscript/sections.s b/test/ELF/linkerscript/sections.s
index dd4b12f..b1e8fb5 100644
--- a/test/ELF/linkerscript/sections.s
+++ b/test/ELF/linkerscript/sections.s
@@ -16,7 +16,7 @@
 # RUN:   FileCheck -check-prefix=SEC-DEFAULT %s
 
 #             Idx Name          Size
-# SEC-DEFAULT: 1 .text         0000000e {{[0-9a-f]*}} TEXT DATA
+# SEC-DEFAULT: 1 .text         0000000e {{[0-9a-f]*}} TEXT
 # SEC-DEFAULT: 2 .data         00000020 {{[0-9a-f]*}} DATA
 # SEC-DEFAULT: 3 other         00000003 {{[0-9a-f]*}} DATA
 # SEC-DEFAULT: 4 .bss          00000002 {{[0-9a-f]*}} BSS
@@ -47,7 +47,7 @@
 # SEC-ORDER: 5 .strtab       00000008 {{[0-9a-f]*}}
 # SEC-ORDER: 6 .comment      00000008 {{[0-9a-f]*}}
 # SEC-ORDER: 7 .data         00000020 {{[0-9a-f]*}} DATA
-# SEC-ORDER: 8 .text         0000000e {{[0-9a-f]*}} TEXT DATA
+# SEC-ORDER: 8 .text         0000000e {{[0-9a-f]*}} TEXT
 
 # .text and .data have swapped names but proper sizes and types.
 # RUN: echo "SECTIONS { \
@@ -58,7 +58,7 @@
 # RUN:   FileCheck -check-prefix=SEC-SWAP-NAMES %s
 
 #                Idx Name          Size
-# SEC-SWAP-NAMES: 1 .data         0000000e {{[0-9a-f]*}} TEXT DATA
+# SEC-SWAP-NAMES: 1 .data         0000000e {{[0-9a-f]*}} TEXT
 # SEC-SWAP-NAMES: 2 .text         00000020 {{[0-9a-f]*}} DATA
 # SEC-SWAP-NAMES: 3 other         00000003 {{[0-9a-f]*}} DATA
 # SEC-SWAP-NAMES: 4 .bss          00000002 {{[0-9a-f]*}} BSS
@@ -80,7 +80,7 @@
 # RUN:   FileCheck -check-prefix=SEC-MULTI %s
 
 #           Idx Name          Size
-# SEC-MULTI:      1 .text         0000000e {{[0-9a-f]*}} TEXT DATA
+# SEC-MULTI:      1 .text         0000000e {{[0-9a-f]*}} TEXT
 # SEC-MULTI-NEXT:   .data         00000020 {{[0-9a-f]*}} DATA
 # SEC-MULTI-NEXT:   .data         00000003 {{[0-9a-f]*}} DATA
 # SEC-MULTI-NEXT:   .bss          00000002 {{[0-9a-f]*}} BSS
diff --git a/test/ELF/linkerscript/segment-none.s b/test/ELF/linkerscript/segment-none.s
index d54e835..0656652 100644
--- a/test/ELF/linkerscript/segment-none.s
+++ b/test/ELF/linkerscript/segment-none.s
@@ -9,7 +9,7 @@
 # RUN:           .foo : {*(.foo)} :NONE \
 # RUN:       }" > %t.script
 # RUN: ld.lld -o %t --script %t.script %t.o
-# RUN: llvm-readobj -elf-output-style=GNU -s -l %t | FileCheck %s
+# RUN: llvm-readelf -s -l %t | FileCheck %s
 
 ## Test that section .foo is placed in segment NONE when assigned to segment
 ## NONE in the linker script and segment NONE is defined.
@@ -19,7 +19,7 @@
 # RUN:           .foo : {*(.foo)} :NONE \
 # RUN:       }" > %t.script
 # RUN: ld.lld -o %t --script %t.script %t.o
-# RUN: llvm-readobj -elf-output-style=GNU -s -l %t | FileCheck --check-prefix=DEFINED %s
+# RUN: llvm-readelf -s -l %t | FileCheck --check-prefix=DEFINED %s
 
 # CHECK: Section to Segment mapping:
 # CHECK-NEXT: Segment Sections...
diff --git a/test/ELF/linkerscript/segment-start.s b/test/ELF/linkerscript/segment-start.s
index 69897d6..cb47cb6 100644
--- a/test/ELF/linkerscript/segment-start.s
+++ b/test/ELF/linkerscript/segment-start.s
@@ -22,6 +22,6 @@
 .quad foobar4
 
 // RUN: echo "SECTIONS { . = SEGMENT_START(\"foobar\", foo); }" > %t.script
-// RUN: not ld.lld %t.o %t.script -shared -o %t2.so 2>&1 \
+// RUN: not ld.lld %t.o %t.script -shared -o /dev/null 2>&1 \
 // RUN: | FileCheck --check-prefix=ERR %s
 // ERR: {{.*}}.script:1: symbol not found: foo
diff --git a/test/ELF/linkerscript/sort-init.s b/test/ELF/linkerscript/sort-init.s
index 894b8ae..dd030ac 100644
--- a/test/ELF/linkerscript/sort-init.s
+++ b/test/ELF/linkerscript/sort-init.s
@@ -1,16 +1,18 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
-# RUN: echo "SECTIONS { .init_array : { *(SORT_BY_INIT_PRIORITY(.init_array.*)) } }" > %t1.script
+# RUN: echo "SECTIONS { .init_array : { *(SORT_BY_INIT_PRIORITY(.init_array.* foo*)) } }" > %t1.script
 # RUN: ld.lld --script %t1.script %t1.o -o %t2
 # RUN: llvm-objdump -s %t2 | FileCheck %s
 
 # CHECK:      Contents of section .init_array:
-# CHECK-NEXT: 03020000 00000000 010405
+# CHECK-NEXT: 03020000 00060000 010405
 
 .globl _start
 _start:
   nop
 
+.section foo, "aw", @init_array
+  .byte 6
 .section .init_array, "aw", @init_array
   .align 8
   .byte 1
diff --git a/test/ELF/linkerscript/sort-non-script.s b/test/ELF/linkerscript/sort-non-script.s
index 563843c..2477c83 100644
--- a/test/ELF/linkerscript/sort-non-script.s
+++ b/test/ELF/linkerscript/sort-non-script.s
@@ -1,14 +1,14 @@
 # REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 
 # RUN: echo "SECTIONS { foo : {*(foo)} }" > %t.script
-# RUN: ld.lld --hash-style=sysv -o %t1 --script %t.script %t -shared
-# RUN: llvm-readobj -elf-output-style=GNU -s %t1 | FileCheck %s
+# RUN: ld.lld --hash-style=sysv -o %t --script %t.script %t.o -shared
+# RUN: llvm-readelf -s %t | FileCheck %s
 
 # CHECK:      .dynsym  {{.*}}   A
+# CHECK-NEXT: .hash    {{.*}}   A
 # CHECK-NEXT: .dynstr  {{.*}}   A
 # CHECK-NEXT: .text    {{.*}}   AX
-# CHECK-NEXT: .hash    {{.*}}   A
 # CHECK-NEXT: foo      {{.*}}  WA
 # CHECK-NEXT: .dynamic {{.*}}  WA
 
diff --git a/test/ELF/linkerscript/sort.s b/test/ELF/linkerscript/sort.s
index 6390ced..abee468 100644
--- a/test/ELF/linkerscript/sort.s
+++ b/test/ELF/linkerscript/sort.s
@@ -95,6 +95,11 @@
 # RUN: ld.lld -o %t11 --script %t10.script %t2.o %t1.o
 # RUN: llvm-objdump -s %t11 | FileCheck -check-prefix=SORTED_A %s
 
+## There is no SORTFOO command, check we handle it properly.
+# RUN: echo "SECTIONS { .aaa : { SORTFOO } }" > %t3.script
+# RUN: not ld.lld -o %t3 --script %t3.script %t1.o 2>&1 | FileCheck %s -check-prefix=SORTFOO
+# SORTFOO: unknown command SORTFOO
+
 .global _start
 _start:
  nop
diff --git a/test/ELF/linkerscript/subalign.s b/test/ELF/linkerscript/subalign.s
index 1396798..99cb3f1 100644
--- a/test/ELF/linkerscript/subalign.s
+++ b/test/ELF/linkerscript/subalign.s
@@ -36,7 +36,7 @@
 
 ## Test we fail gracefuly when alignment value is not a power of 2.
 # RUN: echo "SECTIONS { .aaa : SUBALIGN(3) { *(.aaa*) } }" > %t5.script
-# RUN: not ld.lld %t1.o --script %t5.script -o %t5 2>&1 | FileCheck -check-prefix=ERR %s
+# RUN: not ld.lld %t1.o --script %t5.script -o /dev/null 2>&1 | FileCheck -check-prefix=ERR %s
 # ERR: {{.*}}.script:1: alignment must be power of 2
 
 .global _start
diff --git a/test/ELF/linkerscript/symbol-assignexpr.s b/test/ELF/linkerscript/symbol-assignexpr.s
index 9ab03a1..3be7d05 100644
--- a/test/ELF/linkerscript/symbol-assignexpr.s
+++ b/test/ELF/linkerscript/symbol-assignexpr.s
@@ -47,7 +47,7 @@
 # CHECK-NEXT: 0000000000000001 *ABS* 00000000 symbol15
 
 # RUN: echo "SECTIONS { symbol2 = symbol; }" > %t2.script
-# RUN: not ld.lld -o %t2 --script %t2.script %t 2>&1 \
+# RUN: not ld.lld -o /dev/null --script %t2.script %t 2>&1 \
 # RUN:  | FileCheck -check-prefix=ERR %s
 # ERR: {{.*}}.script:1: symbol not found: symbol
 
diff --git a/test/ELF/linkerscript/symbol-memoryexpr.s b/test/ELF/linkerscript/symbol-memoryexpr.s
index 9c75274..cdd821d 100644
--- a/test/ELF/linkerscript/symbol-memoryexpr.s
+++ b/test/ELF/linkerscript/symbol-memoryexpr.s
@@ -23,7 +23,7 @@
 # RUN:         no_exist_origin = ORIGIN(ram); \
 # RUN:         no_exist_length = LENGTH(ram); \
 # RUN:       }" > %t2.script
-# RUN: not ld.lld -o %t2 --script %t2.script %t 2>&1 \
+# RUN: not ld.lld -o /dev/null --script %t2.script %t 2>&1 \
 # RUN:  | FileCheck -check-prefix=ERR %s
 # ERR: {{.*}}.script:1: memory region not defined: ram
 
diff --git a/test/ELF/linkerscript/symbol-only.test b/test/ELF/linkerscript/symbol-only.test
index 6763423..f2fefdc 100644
--- a/test/ELF/linkerscript/symbol-only.test
+++ b/test/ELF/linkerscript/symbol-only.test
@@ -14,10 +14,8 @@
 # CHECK:      Sections:
 # CHECK-NEXT: Idx Name          Size      Address
 # CHECK-NEXT:   0               00000000 0000000000000000
-# CHECK:          abc           00000000 [[ADDR:[0-9a-f]*]] BSS
-# CHECK-NEXT:     .dynsym       00000030 0000000000000190
-# CHECK-NEXT:     .dynstr       00000005 00000000000001c0
-# CHECK-NEXT:     bar           00000000 0000000000001000 DATA
+# CHECK:          abc           00000000 [[ADDR:[0-9a-f]*]]
+# CHECK:          bar           00000000 0000000000001000
 
 # CHECK: SYMBOL TABLE:
 # CHECK:     [[ADDR]]         abc                00000000 foo
diff --git a/test/ELF/linkerscript/synthetic-symbols4.test b/test/ELF/linkerscript/synthetic-symbols4.test
index fb857e7..fde06e3 100644
--- a/test/ELF/linkerscript/synthetic-symbols4.test
+++ b/test/ELF/linkerscript/synthetic-symbols4.test
@@ -1,7 +1,7 @@
 # REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/synthetic-symbols.s -o %t
-# RUN: ld.lld -o %t.exe --eh-frame-hdr --script %s %t
-# RUN: llvm-objdump -t %t.exe | FileCheck %s
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/synthetic-symbols.s -o %t.o
+# RUN: ld.lld -o %t --eh-frame-hdr --script %s %t.o
+# RUN: llvm-objdump -t %t | FileCheck %s
 
 SECTIONS {
   . = 0x201000;
@@ -10,5 +10,5 @@
   PROVIDE_HIDDEN(_end_sec = ADDR(.text) + SIZEOF(.text));
 }
 
-# CHECK:       0000000000201000         .text     00000000 .hidden _begin_sec
-# CHECK-NEXT:  0000000000201001         .text     00000000 .hidden _end_sec
+# CHECK:       0000000000201054         .text     00000000 .hidden _begin_sec
+# CHECK-NEXT:  0000000000201055         .text     00000000 .hidden _end_sec
diff --git a/test/ELF/linkerscript/target.s b/test/ELF/linkerscript/target.s
new file mode 100644
index 0000000..32db5b78
--- /dev/null
+++ b/test/ELF/linkerscript/target.s
@@ -0,0 +1,18 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: echo "TARGET(binary) INPUT(\"%t.o\") TARGET(elf64-x86-64) INPUT(\"%t.o\")" > %t.script
+# RUN: ld.lld --script %t.script -o %t.exe
+# RUN: llvm-readelf -symbols %t.exe | FileCheck %s
+
+# CHECK: _binary_
+# CHECK: foobar
+
+# RUN: echo "TARGET(foo)" > %t2.script
+# RUN: not ld.lld --script %t2.script -o /dev/null 2>&1 | FileCheck -check-prefix=ERR %s
+
+# ERR: unknown target: foo
+
+.global foobar
+foobar:
+  nop
diff --git a/test/ELF/linkerscript/unused-synthetic.s b/test/ELF/linkerscript/unused-synthetic.s
index 80f0627..6ddbf50 100644
--- a/test/ELF/linkerscript/unused-synthetic.s
+++ b/test/ELF/linkerscript/unused-synthetic.s
@@ -7,13 +7,11 @@
 # RUN:  }" > %t.script
 # RUN: ld.lld -shared -o %t.so --script %t.script %t.o
 
-# RUN: llvm-objdump -section-headers %t.so | FileCheck %s
+# RUN: llvm-readelf -s %t.so | FileCheck %s
 # CHECK-NOT:  .got
 # CHECK-NOT:  .plt
 # CHECK:      .dynsym
-# CHECK-NEXT: .dynstr
-# CHECK-NEXT: .text
-# CHECK-NEXT: .gnu.hash
+# CHECK:      .text
 
 # Test that the size of a removed unused synthetic input section is not added
 # to the output section size. Adding a symbol assignment prevents removal of
diff --git a/test/ELF/linkerscript/va.s b/test/ELF/linkerscript/va.s
index 854ebce..c305f06 100644
--- a/test/ELF/linkerscript/va.s
+++ b/test/ELF/linkerscript/va.s
@@ -5,11 +5,11 @@
 # RUN: ld.lld -o %t1 --script %t.script %t
 # RUN: llvm-objdump -section-headers %t1 | FileCheck %s
 # CHECK:      Sections:
-# CHECK-NEXT: Idx Name          Size      Address          Type
+# CHECK-NEXT: Idx Name          Size      Address
 # CHECK-NEXT:   0               00000000 0000000000000000
-# CHECK-NEXT:   1 .text         00000001 0000000000000000 TEXT DATA
-# CHECK-NEXT:   2 .foo          00000004 0000000000000001 DATA
-# CHECK-NEXT:   3 .boo          00000004 0000000000000005 DATA
+# CHECK-NEXT:   1 .foo          00000004 0000000000000000
+# CHECK-NEXT:   2 .boo          00000004 0000000000000004
+# CHECK-NEXT:   3 .text         00000001 0000000000000008
 
 .global _start
 _start:
diff --git a/test/ELF/llvm33-rela-outside-group.s b/test/ELF/llvm33-rela-outside-group.s
index b5f1b35..1c87817 100644
--- a/test/ELF/llvm33-rela-outside-group.s
+++ b/test/ELF/llvm33-rela-outside-group.s
@@ -1,7 +1,7 @@
 // Input file generated with:
 // llvm33/llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %S/Inputs/llvm33-rela-outside-group.o
 //
-// RUN: ld.lld -shared %S/Inputs/llvm33-rela-outside-group.o %S/Inputs/llvm33-rela-outside-group.o -o %t
+// RUN: ld.lld -shared %S/Inputs/llvm33-rela-outside-group.o %S/Inputs/llvm33-rela-outside-group.o -o /dev/null
 
 	.global bar
 	.weak	_Z3fooIiEvv
diff --git a/test/ELF/local-dynamic.s b/test/ELF/local-dynamic.s
index 797a107..c122074 100644
--- a/test/ELF/local-dynamic.s
+++ b/test/ELF/local-dynamic.s
@@ -1,8 +1,8 @@
+// REQUIRES: x86
 // Check that local symbols are not inserted into dynamic table.
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
 // RUN: ld.lld %t -shared -o %t1.so
 // RUN: llvm-readobj -t -dyn-symbols %t1.so | FileCheck %s
-// REQUIRES: x86
 
 // CHECK: Symbols [
 // CHECK-NEXT:   Symbol {
diff --git a/test/ELF/local-got-pie.s b/test/ELF/local-got-pie.s
index b1b213a..c89fc1c 100644
--- a/test/ELF/local-got-pie.s
+++ b/test/ELF/local-got-pie.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 // RUN: ld.lld --hash-style=sysv %t.o -o %t -pie
 // RUN: llvm-readobj -s -r -d %t | FileCheck %s
diff --git a/test/ELF/local-got-shared.s b/test/ELF/local-got-shared.s
index c858424..284135d 100644
--- a/test/ELF/local-got-shared.s
+++ b/test/ELF/local-got-shared.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 // RUN: ld.lld --hash-style=sysv %t.o -o %t -shared
 // RUN: llvm-readobj -s -r -d %t | FileCheck %s
diff --git a/test/ELF/local-got.s b/test/ELF/local-got.s
index 17517f6..2c1bd58 100644
--- a/test/ELF/local-got.s
+++ b/test/ELF/local-got.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/shared.s -o %t2.o
 // RUN: ld.lld -shared %t2.o -o %t2.so
diff --git a/test/ELF/local.s b/test/ELF/local.s
index 983d7ff..cb9adc8 100644
--- a/test/ELF/local.s
+++ b/test/ELF/local.s
@@ -1,8 +1,8 @@
+// REQUIRES: x86
 // Check that symbol table is correctly populated with local symbols.
 // RUN: llvm-mc -save-temp-labels -filetype=obj -triple=x86_64-pc-linux %s -o %t
 // RUN: ld.lld %t -o %t1
 // RUN: llvm-readobj -t -s %t1 | FileCheck %s
-// REQUIRES: x86
 
 // Check that Info is equal to the number of local symbols.
 // CHECK:   Section {
diff --git a/test/ELF/lto/Inputs/i386-empty.ll b/test/ELF/lto/Inputs/i386-empty.ll
new file mode 100644
index 0000000..6029cb6
--- /dev/null
+++ b/test/ELF/lto/Inputs/i386-empty.ll
@@ -0,0 +1,2 @@
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "i686-linux-gnu"
diff --git a/test/ELF/lto/Inputs/libcall-archive.ll b/test/ELF/lto/Inputs/libcall-archive.ll
new file mode 100644
index 0000000..4e6421c
--- /dev/null
+++ b/test/ELF/lto/Inputs/libcall-archive.ll
@@ -0,0 +1,6 @@
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @memcpy() {
+  ret void
+}
diff --git a/test/ELF/lto/Inputs/libcall-archive.s b/test/ELF/lto/Inputs/libcall-archive.s
new file mode 100644
index 0000000..6ca6e5f
--- /dev/null
+++ b/test/ELF/lto/Inputs/libcall-archive.s
@@ -0,0 +1,2 @@
+.globl __sync_val_compare_and_swap_8
+__sync_val_compare_and_swap_8:
diff --git a/test/ELF/lto/Inputs/sample-profile.prof b/test/ELF/lto/Inputs/sample-profile.prof
new file mode 100644
index 0000000..0ccd747
--- /dev/null
+++ b/test/ELF/lto/Inputs/sample-profile.prof
@@ -0,0 +1 @@
+f:0:0
diff --git a/test/ELF/lto/cache.ll b/test/ELF/lto/cache.ll
index 3f2bea9..5ab74f5 100644
--- a/test/ELF/lto/cache.ll
+++ b/test/ELF/lto/cache.ll
@@ -13,7 +13,7 @@
 ; RUN: ls %t.cache | count 4
 
 ; Create a file of size 64KB.
-; RUN: "%python" -c "print(' ' * 65536)" > %t.cache/llvmcache-foo
+; RUN: %python -c "print(' ' * 65536)" > %t.cache/llvmcache-foo
 
 ; This should leave the file in place.
 ; RUN: ld.lld --thinlto-cache-dir=%t.cache --thinlto-cache-policy cache_size_bytes=128k:prune_interval=0s -o %t3 %t2.o %t.o
diff --git a/test/ELF/lto/data-ordering-lto.s b/test/ELF/lto/data-ordering-lto.s
index 8291ef0..bdacccc 100644
--- a/test/ELF/lto/data-ordering-lto.s
+++ b/test/ELF/lto/data-ordering-lto.s
@@ -8,7 +8,7 @@
 # RUN: echo "pat " >> %t_order_lto.txt
 
 # RUN: ld.lld --symbol-ordering-file %t_order_lto.txt %t.o %t.bc -o %t2.out
-# RUN: llvm-readobj -elf-output-style=GNU -t %t2.out| FileCheck %s
+# RUN: llvm-readelf -t %t2.out| FileCheck %s
 
 # Check that the order is tin -> dipsy -> pat.
 
diff --git a/test/ELF/lto/libcall-archive.ll b/test/ELF/lto/libcall-archive.ll
new file mode 100644
index 0000000..7e8ac18
--- /dev/null
+++ b/test/ELF/lto/libcall-archive.ll
@@ -0,0 +1,25 @@
+; REQUIRES: x86
+; RUN: rm -f %t.a
+; RUN: llvm-as -o %t.o %s
+; RUN: llvm-as -o %t2.o %S/Inputs/libcall-archive.ll
+; RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux -o %t3.o %S/Inputs/libcall-archive.s
+; RUN: llvm-ar rcs %t.a %t2.o %t3.o
+; RUN: ld.lld -o %t %t.o %t.a
+; RUN: llvm-nm %t | FileCheck %s
+; RUN: ld.lld -o %t2 %t.o --start-lib %t2.o %t3.o --end-lib
+; RUN: llvm-nm %t2 | FileCheck %s
+
+; CHECK-NOT: T __sync_val_compare_and_swap_8
+; CHECK: T _start
+; CHECK: T memcpy
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @_start(i8* %a, i8* %b) {
+entry:
+  call void @llvm.memcpy.p0i8.p0i8.i64(i8* %a, i8* %b, i64 1024, i1 false)
+  ret void
+}
+
+declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i1)
diff --git a/test/ELF/lto/mix-platforms2.ll b/test/ELF/lto/mix-platforms2.ll
new file mode 100644
index 0000000..1bd989e
--- /dev/null
+++ b/test/ELF/lto/mix-platforms2.ll
@@ -0,0 +1,9 @@
+; REQUIRES: x86
+; RUN: llvm-as %s -o %tx64.o
+; RUN: llvm-as %S/Inputs/i386-empty.ll -o %ti386.o
+; RUN: not ld.lld %ti386.o %tx64.o -o %t.out 2>&1 | FileCheck %s
+
+; CHECK: {{.*}}x64.o is incompatible with {{.*}}i386.o
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
diff --git a/test/ELF/lto/opt-remarks.ll b/test/ELF/lto/opt-remarks.ll
index 19b141f..bef7e01 100644
--- a/test/ELF/lto/opt-remarks.ll
+++ b/test/ELF/lto/opt-remarks.ll
@@ -23,9 +23,10 @@
 ; YAML-NEXT:   - Callee:          tinkywinky
 ; YAML-NEXT:   - String:          ' inlined into '
 ; YAML-NEXT:   - Caller:          main
-; YAML-NEXT:   - String:          ' with cost='
+; YAML-NEXT:   - String:          ' with '
+; YAML-NEXT:   - String:          '(cost='
 ; YAML-NEXT:   - Cost:            '0'
-; YAML-NEXT:   - String:          ' (threshold='
+; YAML-NEXT:   - String:          ', threshold='
 ; YAML-NEXT:   - Threshold:       '337'
 ; YAML-NEXT:   - String:          ')'
 ; YAML-NEXT: ...
@@ -39,9 +40,10 @@
 ; YAML-HOT-NEXT:   - Callee:          tinkywinky
 ; YAML-HOT-NEXT:   - String:          ' inlined into '
 ; YAML-HOT-NEXT:   - Caller:          main
-; YAML-HOT-NEXT:   - String:          ' with cost='
+; YAML-HOT-NEXT:   - String:          ' with '
+; YAML-HOT-NEXT:   - String:          '(cost='
 ; YAML-HOT-NEXT:   - Cost:            '0'
-; YAML-HOT-NEXT:   - String:          ' (threshold='
+; YAML-HOT-NEXT:   - String:          ', threshold='
 ; YAML-HOT-NEXT:   - Threshold:       '337'
 ; YAML-HOT-NEXT:   - String:          ')'
 ; YAML-HOT-NEXT: ...
diff --git a/test/ELF/lto/relocatable.ll b/test/ELF/lto/relocatable.ll
index 2ec9144..5a0ed42 100644
--- a/test/ELF/lto/relocatable.ll
+++ b/test/ELF/lto/relocatable.ll
@@ -41,6 +41,15 @@
 ; CHECK-NEXT:     Section: .text.foo
 ; CHECK-NEXT:   }
 ; CHECK-NEXT:   Symbol {
+; CHECK-NEXT:     Name:
+; CHECK-NEXT:     Value: 0x0
+; CHECK-NEXT:     Size: 0
+; CHECK-NEXT:     Binding: Local
+; CHECK-NEXT:     Type: Section
+; CHECK-NEXT:     Other: 0
+; CHECK-NEXT:     Section: .llvm_addrsig
+; CHECK-NEXT:   }
+; CHECK-NEXT:   Symbol {
 ; CHECK-NEXT:     Name: foo
 ; CHECK-NEXT:     Value: 0x0
 ; CHECK-NEXT:     Size: 1
diff --git a/test/ELF/lto/sample-profile.ll b/test/ELF/lto/sample-profile.ll
index 17eead7..a8b1104 100644
--- a/test/ELF/lto/sample-profile.ll
+++ b/test/ELF/lto/sample-profile.ll
@@ -3,11 +3,11 @@
 ; RUN: opt -module-summary %p/Inputs/thinlto.ll -o %t2.o
 
 ; RUN: rm -f %t1.lto.o %t2.lto.o
-; RUN: ld.lld --lto-sample-profile=/dev/null %t1.o %t2.o -o %t3
+; RUN: ld.lld --lto-sample-profile=%p/Inputs/sample-profile.prof %t1.o %t2.o -o %t3
 ; RUN  opt -S %t3.lto.o | FileCheck %s
 
 ; RUN: rm -f %t1.lto.o %t2.lto.o
-; RUN: ld.lld --plugin-opt=sample-profile=/dev/null %t1.o %t2.o -o %t3
+; RUN: ld.lld --plugin-opt=sample-profile=%p/Inputs/sample-profile.prof %t1.o %t2.o -o %t3
 ; RUN  opt -S %t3.lto.o | FileCheck %s
 
 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
diff --git a/test/ELF/lto/symbol-ordering-lto.s b/test/ELF/lto/symbol-ordering-lto.s
index 232817c..530b63c 100644
--- a/test/ELF/lto/symbol-ordering-lto.s
+++ b/test/ELF/lto/symbol-ordering-lto.s
@@ -8,7 +8,7 @@
 # RUN: echo "pat " >> %t_order_lto.txt
 
 # RUN: ld.lld --symbol-ordering-file %t_order_lto.txt %t.o %t.bc -o %t2.out
-# RUN: llvm-readobj -elf-output-style=GNU -t %t2.out| FileCheck %s
+# RUN: llvm-readelf -t %t2.out| FileCheck %s
 
 # Check that the order is tin -> _start -> pat.
 
diff --git a/test/ELF/lto/thinlto-debug-fission.ll b/test/ELF/lto/thinlto-debug-fission.ll
new file mode 100644
index 0000000..b779ad4
--- /dev/null
+++ b/test/ELF/lto/thinlto-debug-fission.ll
@@ -0,0 +1,21 @@
+; REQUIRES: x86
+
+; RUN: opt %s -o %t1.o
+; RUN: rm -rf %T/dwo
+
+; Test to ensure that --plugin-opt=dwo_dir=$DIR creates .dwo files under $DIR
+; RUN: ld.lld --plugin-opt=dwo_dir=%T/dwo -shared %t1.o -o /dev/null
+; RUN: llvm-readobj -h %T/dwo/0.dwo | FileCheck %s
+
+; CHECK: Format: ELF64-x86-64
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+declare void @g(...)
+
+define void @f() {
+entry:
+  call void (...) @g()
+  ret void
+}
diff --git a/test/ELF/lto/thinlto-index-only.ll b/test/ELF/lto/thinlto-index-only.ll
index dba2fbc..6263799 100644
--- a/test/ELF/lto/thinlto-index-only.ll
+++ b/test/ELF/lto/thinlto-index-only.ll
@@ -29,6 +29,25 @@
 ; RUN: ls %t2.o.thinlto.bc
 ; RUN: not test -e %t4
 
+; Test that LLD generates an empty index even for lazy object file that is not added to link.
+; Test LLD generates empty imports file either because of thinlto-emit-imports-files option.
+; RUN: rm -f %t1.o.thinlto.bc
+; RUN: rm -f %t1.o.imports
+; RUN: ld.lld --plugin-opt=thinlto-index-only -shared %t2.o --start-lib %t1.o --end-lib \
+; RUN: --plugin-opt=thinlto-emit-imports-files -o %t3
+; RUN: ls %t1.o.thinlto.bc
+; RUN: ls %t1.o.imports
+
+; Ensure lld generates an error if unable to write an empty index file
+; for lazy object file that is not added to link.
+; RUN: rm -f %t1.o.thinlto.bc
+; RUN: touch %t1.o.thinlto.bc
+; RUN: chmod 400 %t1.o.thinlto.bc
+; RUN: not ld.lld --plugin-opt=thinlto-index-only -shared %t2.o --start-lib %t1.o --end-lib \
+; RUN:   -o %t3 2>&1 | FileCheck %s
+; CHECK: cannot open {{.*}}1.o.thinlto.bc: {{P|p}}ermission denied
+; RUN: rm -f %t1.o.thinlto.bc
+
 ; NM: T f
 
 ; The backend index for this module contains summaries from itself and
diff --git a/test/ELF/map-file-i686.s b/test/ELF/map-file-i686.s
new file mode 100644
index 0000000..bab2c4b
--- /dev/null
+++ b/test/ELF/map-file-i686.s
@@ -0,0 +1,21 @@
+// REQUIRES: x86
+
+// RUN: llvm-mc -filetype=obj -triple=i386-pc-linux %s -o %t1.o
+// RUN: ld.lld %t1.o -o /dev/null -M | FileCheck -strict-whitespace %s
+
+.global _start
+_start:
+ nop
+
+// CHECK:        VMA      LMA     Size Align Out     In      Symbol
+// CHECK-NEXT: 11000    11000        1     4 .text
+// CHECK-NEXT: 11000    11000        1     4         {{.*}}{{/|\\}}map-file-i686.s.tmp1.o:(.text)
+// CHECK-NEXT: 11000    11000        0     1                 _start
+// CHECK-NEXT:     0        0        8     1 .comment
+// CHECK-NEXT:     0        0        8     1         <internal>:(.comment)
+// CHECK-NEXT:     0        0       20     4 .symtab
+// CHECK-NEXT:     0        0       20     4         <internal>:(.symtab)
+// CHECK-NEXT:     0        0       2a     1 .shstrtab
+// CHECK-NEXT:     0        0       2a     1         <internal>:(.shstrtab)
+// CHECK-NEXT:     0        0        8     1 .strtab
+// CHECK-NEXT:     0        0        8     1         <internal>:(.strtab)
diff --git a/test/ELF/map-file.s b/test/ELF/map-file.s
index 390d880..76e50fb 100644
--- a/test/ELF/map-file.s
+++ b/test/ELF/map-file.s
@@ -43,20 +43,20 @@
 // CHECK:         VMA              LMA     Size Align Out     In      Symbol
 // CHECK-NEXT: 2001c8           2001c8       78     8 .dynsym
 // CHECK-NEXT: 2001c8           2001c8       78     8         <internal>:(.dynsym)
-// CHECK-NEXT: 200240           200240       31     1 .dynstr
-// CHECK-NEXT: 200240           200240       31     1         <internal>:(.dynstr)
-// CHECK-NEXT: 200278           200278       2c     8 .gnu.hash
-// CHECK-NEXT: 200278           200278       2c     8         <internal>:(.gnu.hash)
-// CHECK-NEXT: 2002a4           2002a4       30     4 .hash
-// CHECK-NEXT: 2002a4           2002a4       30     4         <internal>:(.hash)
-// CHECK-NEXT: 2002d8           2002d8       30     8 .rela.dyn
-// CHECK-NEXT: 2002d8           2002d8       30     8         <internal>:(.rela.dyn)
-// CHECK-NEXT: 200308           200308       30     8 .rela.plt
-// CHECK-NEXT: 200308           200308       30     8         <internal>:(.rela.plt)
-// CHECK-NEXT: 200338           200338       64     8 .eh_frame
-// CHECK-NEXT: 200338           200338       2c     1         {{.*}}{{/|\\}}map-file.s.tmp1.o:(.eh_frame+0x0)
-// CHECK-NEXT: 200368           200368       14     1         {{.*}}{{/|\\}}map-file.s.tmp1.o:(.eh_frame+0x2c)
-// CHECK-NEXT: 200380           200380       18     1         {{.*}}{{/|\\}}map-file.s.tmp2.o:(.eh_frame+0x18)
+// CHECK-NEXT: 200240           200240       2c     8 .gnu.hash
+// CHECK-NEXT: 200240           200240       2c     8         <internal>:(.gnu.hash)
+// CHECK-NEXT: 20026c           20026c       30     4 .hash
+// CHECK-NEXT: 20026c           20026c       30     4         <internal>:(.hash)
+// CHECK-NEXT: 20029c           20029c       31     1 .dynstr
+// CHECK-NEXT: 20029c           20029c       31     1         <internal>:(.dynstr)
+// CHECK-NEXT: 2002d0           2002d0       30     8 .rela.dyn
+// CHECK-NEXT: 2002d0           2002d0       30     8         <internal>:(.rela.dyn)
+// CHECK-NEXT: 200300           200300       30     8 .rela.plt
+// CHECK-NEXT: 200300           200300       30     8         <internal>:(.rela.plt)
+// CHECK-NEXT: 200330           200330       64     8 .eh_frame
+// CHECK-NEXT: 200330           200330       2c     1         {{.*}}{{/|\\}}map-file.s.tmp1.o:(.eh_frame+0x0)
+// CHECK-NEXT: 200360           200360       14     1         {{.*}}{{/|\\}}map-file.s.tmp1.o:(.eh_frame+0x2c)
+// CHECK-NEXT: 200378           200378       18     1         {{.*}}{{/|\\}}map-file.s.tmp2.o:(.eh_frame+0x18)
 // CHECK-NEXT: 201000           201000       2d     4 .text
 // CHECK-NEXT: 201000           201000       28     4         {{.*}}{{/|\\}}map-file.s.tmp1.o:(.text)
 // CHECK-NEXT: 201000           201000        0     1                 _start
diff --git a/test/ELF/map-gc-sections.s b/test/ELF/map-gc-sections.s
index 717ab81..f69edf5 100644
--- a/test/ELF/map-gc-sections.s
+++ b/test/ELF/map-gc-sections.s
@@ -1,5 +1,6 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-// RUN: ld.lld %t.o -o %t -Map=- --gc-sections | FileCheck %s
+// RUN: ld.lld %t.o -o /dev/null -Map=- --gc-sections | FileCheck %s
 
 .section .tbss,"awT",@nobits
 // CHECK-NOT: foo
diff --git a/test/ELF/merge-gc-piece.s b/test/ELF/merge-gc-piece.s
index 95ea17c..4aec3b2 100644
--- a/test/ELF/merge-gc-piece.s
+++ b/test/ELF/merge-gc-piece.s
@@ -10,7 +10,7 @@
 # CHECK-NEXT:   SHF_ALLOC
 # CHECK-NEXT:   SHF_MERGE
 # CHECK-NEXT: ]
-# CHECK-NEXT: Address: 0x1C8
+# CHECK-NEXT: Address: 0x200
 
 # CHECK:      Name: .bar
 # CHECK-NEXT: Type: SHT_PROGBITS
@@ -24,7 +24,7 @@
 # CHECK-NEXT: AddressAlignment:
 # CHECK-NEXT: EntrySize:
 # CHECK-NEXT: SectionData (
-# CHECK-NEXT:   0000: C9010000 00000000 CA010000 00000000
+# CHECK-NEXT:   0000: 01020000 00000000 02020000 00000000
 # CHECK-NEXT: )
 
         .section .foo,"aM",@progbits,8
diff --git a/test/ELF/merge-shared-str.s b/test/ELF/merge-shared-str.s
index e06d00d..7502eb9 100644
--- a/test/ELF/merge-shared-str.s
+++ b/test/ELF/merge-shared-str.s
@@ -19,10 +19,10 @@
 // CHECK-NEXT:   SHF_MERGE
 // CHECK-NEXT:   SHF_STRINGS
 // CHECK-NEXT: ]
-// CHECK-NEXT: Address: 0x1E1
+// CHECK-NEXT: Address: 0x228
 
 // CHECK:      Relocations [
 // CHECK-NEXT:   Section ({{.*}}) .rela.dyn {
-// CHECK-NEXT:     0x{{.*}} R_X86_64_RELATIVE - 0x1E2
+// CHECK-NEXT:     0x{{.*}} R_X86_64_RELATIVE - 0x229
 // CHECK-NEXT:   }
 // CHECK-NEXT: ]
diff --git a/test/ELF/merge-shared.s b/test/ELF/merge-shared.s
index 3894e53..6615169 100644
--- a/test/ELF/merge-shared.s
+++ b/test/ELF/merge-shared.s
@@ -17,10 +17,10 @@
 // CHECK-NEXT:   SHF_ALLOC
 // CHECK-NEXT:   SHF_MERGE
 // CHECK-NEXT: ]
-// CHECK-NEXT: Address: 0x1E4
+// CHECK-NEXT: Address: 0x228
 
 // CHECK:      Relocations [
 // CHECK-NEXT:   Section ({{.*}}) .rela.dyn {
-// CHECK-NEXT:     0x{{.*}} R_X86_64_RELATIVE - 0x1E6
+// CHECK-NEXT:     0x{{.*}} R_X86_64_RELATIVE - 0x22A
 // CHECK-NEXT:   }
 // CHECK-NEXT: ]
diff --git a/test/ELF/merge-string-empty.s b/test/ELF/merge-string-empty.s
index 0b82ce7..dc6635c 100644
--- a/test/ELF/merge-string-empty.s
+++ b/test/ELF/merge-string-empty.s
@@ -1,8 +1,8 @@
+// REQUIRES: x86
 // Ensure that a mergeable string with size 0 does not cause any issue.
 
-// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-// RUN: ld.lld %t.o -o %t
+// RUN: ld.lld %t.o -o /dev/null
 
 .globl _start, s
 .section .rodata.str1.1,"aMS",@progbits,1
diff --git a/test/ELF/merge-string-error.s b/test/ELF/merge-string-error.s
index 78895ce..70a361b 100644
--- a/test/ELF/merge-string-error.s
+++ b/test/ELF/merge-string-error.s
@@ -1,6 +1,6 @@
 // REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-// RUN: not ld.lld %t.o -o %t.so -shared 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.o -o /dev/null -shared 2>&1 | FileCheck %s
 
         .section	.rodata.str1.1,"aMS",@progbits,1
 	.asciz	"abc"
diff --git a/test/ELF/merge-string-no-null.s b/test/ELF/merge-string-no-null.s
index fd3f507..ea433fe 100644
--- a/test/ELF/merge-string-no-null.s
+++ b/test/ELF/merge-string-no-null.s
@@ -1,6 +1,6 @@
 // REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-// RUN: not ld.lld %t.o -o %t.so -shared 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.o -o /dev/null -shared 2>&1 | FileCheck %s
 
 	.section	.rodata.str1.1,"aMS",@progbits,1
 	.ascii	"abc"
diff --git a/test/ELF/merge-string.s b/test/ELF/merge-string.s
index 2496205..065e800 100644
--- a/test/ELF/merge-string.s
+++ b/test/ELF/merge-string.s
@@ -28,8 +28,8 @@
 // CHECK-NEXT:   SHF_MERGE
 // CHECK-NEXT:   SHF_STRINGS
 // CHECK-NEXT: ]
-// CHECK-NEXT: Address: 0x1E1
-// CHECK-NEXT: Offset: 0x1E1
+// CHECK-NEXT: Address: 0x20D
+// CHECK-NEXT: Offset: 0x20D
 // CHECK-NEXT: Size:    4
 // CHECK-NEXT: Link: 0
 // CHECK-NEXT: Info: 0
@@ -46,8 +46,8 @@
 // NOTAIL-NEXT:   SHF_MERGE
 // NOTAIL-NEXT:   SHF_STRINGS
 // NOTAIL-NEXT: ]
-// NOTAIL-NEXT: Address: 0x1E1
-// NOTAIL-NEXT: Offset: 0x1E1
+// NOTAIL-NEXT: Address: 0x20D
+// NOTAIL-NEXT: Offset: 0x20D
 // NOTAIL-NEXT: Size:    7
 // NOTAIL-NEXT: Link: 0
 // NOTAIL-NEXT: Info: 0
@@ -64,8 +64,8 @@
 // NOMERGE-NEXT:   SHF_MERGE
 // NOMERGE-NEXT:   SHF_STRINGS
 // NOMERGE-NEXT: ]
-// NOMERGE-NEXT: Address: 0x1E1
-// NOMERGE-NEXT: Offset: 0x1E1
+// NOMERGE-NEXT: Address: 0x20D
+// NOMERGE-NEXT: Offset: 0x20D
 // NOMERGE-NEXT: Size:    11
 // NOMERGE-NEXT: Link: 0
 // NOMERGE-NEXT: Info: 0
@@ -82,8 +82,8 @@
 // CHECK-NEXT:   SHF_MERGE
 // CHECK-NEXT:   SHF_STRINGS
 // CHECK-NEXT: ]
-// CHECK-NEXT: Address: 0x1E6
-// CHECK-NEXT: Offset: 0x1E6
+// CHECK-NEXT: Address: 0x212
+// CHECK-NEXT: Offset: 0x212
 // CHECK-NEXT: Size: 4
 // CHECK-NEXT: Link: 0
 // CHECK-NEXT: Info: 0
@@ -95,11 +95,11 @@
 
 
 // CHECK:      Name:    bar
-// CHECK-NEXT: Value: 0x1E2
+// CHECK-NEXT: Value: 0x20E
 
 // CHECK:      Name:    foo
-// CHECK-NEXT: Value: 0x1E1
+// CHECK-NEXT: Value: 0x20D
 
 // CHECK:      Name: zed
-// CHECK-NEXT: Value: 0x1E6
+// CHECK-NEXT: Value: 0x212
 // CHECK-NEXT: Size: 0
diff --git a/test/ELF/merge-sym.s b/test/ELF/merge-sym.s
index 7329bd4..89becc8 100644
--- a/test/ELF/merge-sym.s
+++ b/test/ELF/merge-sym.s
@@ -15,7 +15,7 @@
 // CHECK-NEXT:   SHF_ALLOC
 // CHECK-NEXT:   SHF_MERGE
 // CHECK-NEXT: ]
-// CHECK-NEXT: Address: 0x1E4
+// CHECK-NEXT: Address: 0x210
 
 // CHECK:      Name: foo
-// CHECK-NEXT: Value: 0x1E6
+// CHECK-NEXT: Value: 0x212
diff --git a/test/ELF/merge-to-non-alloc.s b/test/ELF/merge-to-non-alloc.s
index e2894ed..86f6f26 100644
--- a/test/ELF/merge-to-non-alloc.s
+++ b/test/ELF/merge-to-non-alloc.s
@@ -15,11 +15,11 @@
 // CHECK-NEXT: AddressAlignment:
 // CHECK-NEXT: EntrySize:
 // CHECK-NEXT: SectionData (
-// CHECK-NEXT:   0000: E4010000 00000000 EC010000 00000000  |
+// CHECK-NEXT:   0000: 10020000 00000000 18020000 00000000  |
 // CHECK-NEXT: )
 
 // CHECK:      Name: foo
-// CHECK-NEXT: Value: 0x1E4
+// CHECK-NEXT: Value: 0x210
 
         .section        .foo,"aM",@progbits,4
         .align  4
diff --git a/test/ELF/mips-26-mask.s b/test/ELF/mips-26-mask.s
index 4cf56cf..874d5c4 100644
--- a/test/ELF/mips-26-mask.s
+++ b/test/ELF/mips-26-mask.s
@@ -1,11 +1,10 @@
+# REQUIRES: mips
 # Check reading/writing implicit addend for R_MIPS_26 relocation.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
 # RUN: ld.lld %t.o -o %t.exe
 # RUN: llvm-objdump -d %t.exe | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:      Disassembly of section .text:
 # CHECK:      __start:
 # CHECK-NEXT:   20000:       0e 00 80 00     jal     134348800
diff --git a/test/ELF/mips-26-n32-n64.s b/test/ELF/mips-26-n32-n64.s
index 246bf1e..92f5331 100644
--- a/test/ELF/mips-26-n32-n64.s
+++ b/test/ELF/mips-26-n32-n64.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check R_MIPS_26 relocation handling in case of N64 ABIs.
 
 # RUN: llvm-mc -filetype=obj -triple=mips64-unknown-linux \
@@ -11,8 +12,6 @@
 # RUN: llvm-objdump -d %t.exe | FileCheck %s --check-prefixes=CHECK,HAZARDPLT
 
 
-# REQUIRES: mips
-
 # CHECK:      Disassembly of section .text:
 # CHECK-NEXT: __start:
 # CHECK-NEXT:    20000:       0c 00 80 0c     jal     131120
diff --git a/test/ELF/mips-26.s b/test/ELF/mips-26.s
index 749920b..882129b 100644
--- a/test/ELF/mips-26.s
+++ b/test/ELF/mips-26.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check R_MIPS_26 relocation handling.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t1.o
@@ -9,8 +10,6 @@
 # RUN: llvm-readobj -dynamic-table -s -r -mips-plt-got %t.exe \
 # RUN:   | FileCheck -check-prefix=REL %s
 
-# REQUIRES: mips
-
 # CHECK:      Disassembly of section .text:
 # CHECK-NEXT: bar:
 # CHECK-NEXT:   20000:       0c 00 80 06     jal     131096 <loc>
diff --git a/test/ELF/mips-32.s b/test/ELF/mips-32.s
index ef97afc..7efcfcd 100644
--- a/test/ELF/mips-32.s
+++ b/test/ELF/mips-32.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check R_MIPS_32 relocation calculation.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t-be.o
@@ -14,8 +15,6 @@
 # RUN: llvm-readobj -r -dynamic-table -mips-plt-got %t-el.so \
 # RUN:   | FileCheck -check-prefix=REL %s
 
-# REQUIRES: mips
-
   .globl  __start
 __start:
   nop
diff --git a/test/ELF/mips-64-disp.s b/test/ELF/mips-64-disp.s
index 29b62dc..5d5049c 100644
--- a/test/ELF/mips-64-disp.s
+++ b/test/ELF/mips-64-disp.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check R_MIPS_GOT_DISP relocations against various kind of symbols.
 
 # RUN: llvm-mc -filetype=obj -triple=mips64-unknown-linux \
@@ -8,8 +9,6 @@
 # RUN: llvm-objdump -d -t %t.exe | FileCheck %s
 # RUN: llvm-readobj -r -mips-plt-got %t.exe | FileCheck -check-prefix=GOT %s
 
-# REQUIRES: mips
-
 # CHECK:      __start:
 # CHECK-NEXT:    20000:   24 42 80 40   addiu   $2, $2, -32704
 # CHECK-NEXT:    20004:   24 42 80 20   addiu   $2, $2, -32736
diff --git a/test/ELF/mips-64-got-overflow.s b/test/ELF/mips-64-got-overflow.s
new file mode 100644
index 0000000..5de71b1
--- /dev/null
+++ b/test/ELF/mips-64-got-overflow.s
@@ -0,0 +1,80 @@
+# REQUIRES: mips
+# Check the primary GOT cannot be made to overflow
+
+# RUN: llvm-mc -filetype=obj -triple=mips64-unknown-linux \
+# RUN:         %p/Inputs/mips-64-got-load.s -o %t1.so.o
+# RUN: llvm-mc -filetype=obj -triple=mips64-unknown-linux %s -o %t2.so.o
+# RUN: ld.lld -shared -mips-got-size 32 %t1.so.o %t2.so.o -o %t-sgot.so
+# RUN: ld.lld -shared -mips-got-size 24 %t1.so.o %t2.so.o -o %t-mgot.so
+# RUN: llvm-readobj -r -dt -mips-plt-got %t-sgot.so | FileCheck -check-prefix=SGOT %s
+# RUN: llvm-readobj -r -dt -mips-plt-got %t-mgot.so | FileCheck -check-prefix=MGOT %s
+
+# SGOT:      Primary GOT {
+# SGOT-NEXT:   Canonical gp value: 0x27FF0
+# SGOT-NEXT:   Reserved entries [
+# SGOT-NEXT:     Entry {
+# SGOT-NEXT:       Address:
+# SGOT-NEXT:       Access: -32752
+# SGOT-NEXT:       Initial: 0x0
+# SGOT-NEXT:       Purpose: Lazy resolver
+# SGOT-NEXT:     }
+# SGOT-NEXT:     Entry {
+# SGOT-NEXT:       Address:
+# SGOT-NEXT:       Access: -32744
+# SGOT-NEXT:       Initial: 0x80000000
+# SGOT-NEXT:       Purpose: Module pointer (GNU extension)
+# SGOT-NEXT:     }
+# SGOT-NEXT:   ]
+# SGOT-NEXT:   Local entries [
+# SGOT-NEXT:     Entry {
+# SGOT-NEXT:       Address:
+# SGOT-NEXT:       Access: -32736
+# SGOT-NEXT:       Initial: 0x20020
+# SGOT-NEXT:     }
+# SGOT-NEXT:     Entry {
+# SGOT-NEXT:       Address:
+# SGOT-NEXT:       Access: -32728
+# SGOT-NEXT:       Initial: 0x20030
+# SGOT-NEXT:     }
+# SGOT-NEXT:   ]
+# SGOT-NEXT:   Global entries [
+# SGOT-NEXT:   ]
+# SGOT-NEXT:   Number of TLS and multi-GOT entries: 0
+# SGOT-NEXT: }
+
+# MGOT:      Primary GOT {
+# MGOT-NEXT:   Canonical gp value: 0x27FF0
+# MGOT-NEXT:   Reserved entries [
+# MGOT-NEXT:     Entry {
+# MGOT-NEXT:       Address:
+# MGOT-NEXT:       Access: -32752
+# MGOT-NEXT:       Initial: 0x0
+# MGOT-NEXT:       Purpose: Lazy resolver
+# MGOT-NEXT:     }
+# MGOT-NEXT:     Entry {
+# MGOT-NEXT:       Address:
+# MGOT-NEXT:       Access: -32744
+# MGOT-NEXT:       Initial: 0x80000000
+# MGOT-NEXT:       Purpose: Module pointer (GNU extension)
+# MGOT-NEXT:     }
+# MGOT-NEXT:   ]
+# MGOT-NEXT:   Local entries [
+# MGOT-NEXT:     Entry {
+# MGOT-NEXT:       Address:
+# MGOT-NEXT:       Access: -32736
+# MGOT-NEXT:       Initial: 0x20020
+# MGOT-NEXT:     }
+# MGOT-NEXT:   ]
+# MGOT-NEXT:   Global entries [
+# MGOT-NEXT:   ]
+# MGOT-NEXT:   Number of TLS and multi-GOT entries: 1
+# MGOT-NEXT: }
+
+  .text
+  .global foo2
+foo2:
+  ld $2, %got_disp(local2)($gp)
+
+  .bss
+local2:
+  .word 0
diff --git a/test/ELF/mips-64-got.s b/test/ELF/mips-64-got.s
index f2b4d5b..e1b1f34 100644
--- a/test/ELF/mips-64-got.s
+++ b/test/ELF/mips-64-got.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check MIPS N64 ABI GOT relocations
 
 # RUN: llvm-mc -filetype=obj -triple=mips64-unknown-linux \
@@ -8,8 +9,6 @@
 # RUN: llvm-objdump -d -t %t.exe | FileCheck %s
 # RUN: llvm-readobj -r -mips-plt-got %t.exe | FileCheck -check-prefix=GOT %s
 
-# REQUIRES: mips
-
 # CHECK:      __start:
 
 # CHECK-NEXT:    20000:   df 82 80 20   ld      $2, -32736($gp)
diff --git a/test/ELF/mips-64-gprel-so.s b/test/ELF/mips-64-gprel-so.s
index 437238e..d741dd9 100644
--- a/test/ELF/mips-64-gprel-so.s
+++ b/test/ELF/mips-64-gprel-so.s
@@ -1,11 +1,10 @@
+# REQUIRES: mips
 # Check setup of GP relative offsets in a function's prologue.
 
 # RUN: llvm-mc -filetype=obj -triple=mips64-unknown-linux %s -o %t.o
 # RUN: ld.lld %t.o -shared -o %t.so
 # RUN: llvm-objdump -d -t %t.so | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:      Disassembly of section .text:
 # CHECK-NEXT: foo:
 # CHECK-NEXT:    10000:    3c 1c 00 01    lui     $gp, 1
diff --git a/test/ELF/mips-64-rels.s b/test/ELF/mips-64-rels.s
index 7867155..e641b32 100644
--- a/test/ELF/mips-64-rels.s
+++ b/test/ELF/mips-64-rels.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check handling multiple MIPS N64 ABI relocations packed
 # into the single relocation record.
 
@@ -6,8 +7,6 @@
 # RUN: llvm-objdump -d -s -t %t.exe | FileCheck %s
 # RUN: llvm-readobj -r %t.exe | FileCheck -check-prefix=REL %s
 
-# REQUIRES: mips
-
 # CHECK:      __start:
 # CHECK-NEXT:    20000:   3c 1c 00 01   lui     $gp, 1
 #                                                    ^-- 0x20000 - 0x37ff0
diff --git a/test/ELF/mips-64.s b/test/ELF/mips-64.s
index 501988d..e37b75c 100644
--- a/test/ELF/mips-64.s
+++ b/test/ELF/mips-64.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check R_MIPS_64 relocation calculation.
 
 # RUN: llvm-mc -filetype=obj -triple=mips64-unknown-linux %s -o %t.o
@@ -5,8 +6,6 @@
 # RUN: llvm-objdump -t %t.so | FileCheck -check-prefix=SYM %s
 # RUN: llvm-readobj -r -dynamic-table -mips-plt-got %t.so | FileCheck %s
 
-# REQUIRES: mips
-
   .global  __start
 __start:
   nop
diff --git a/test/ELF/mips-abs-got.s b/test/ELF/mips-abs-got.s
new file mode 100644
index 0000000..4964c85
--- /dev/null
+++ b/test/ELF/mips-abs-got.s
@@ -0,0 +1,36 @@
+# REQUIRES: mips
+
+# Check GOT relocations against absolute symbols.
+
+# RUN: llvm-mc -filetype=obj -triple=mips64-unknown-linux -o %t.o %s
+# RUN: echo "SECTIONS { \
+# RUN:          zero = 0; foo = 0x11004; bar = 0x22000; }" > %t.script
+# RUN: ld.lld --script %t.script -o %t.exe %t.o
+# RUN: llvm-readobj -mips-plt-got %t.exe | FileCheck %s
+
+# CHECK:      Static GOT {
+# CHECK:        Local entries [
+# CHECK-NEXT:     Entry {
+# CHECK-NEXT:       Address:
+# CHECK-NEXT:       Access: -32736
+# CHECK-NEXT:       Initial: 0x0
+# CHECK-NEXT:     }
+# CHECK-NEXT:     Entry {
+# CHECK-NEXT:       Address:
+# CHECK-NEXT:       Access: -32728
+# CHECK-NEXT:       Initial: 0x10000
+# CHECK-NEXT:     }
+# CHECK-NEXT:     Entry {
+# CHECK-NEXT:       Address:
+# CHECK-NEXT:       Access: -32720
+# CHECK-NEXT:       Initial: 0x30000
+# CHECK-NEXT:     }
+# CHECK-NEXT:   ]
+# CHECK-NEXT: }
+
+  .text
+  nop
+  .reloc 0, R_MIPS_GOT_PAGE, 0
+  ld      $v0, %got_page(zero)($gp)
+  ld      $v0, %got_page(foo)($gp)
+  ld      $v0, %got_page(bar+0x10008)($gp)
diff --git a/test/ELF/mips-align-err.s b/test/ELF/mips-align-err.s
index a3bf134..8bf01dc 100644
--- a/test/ELF/mips-align-err.s
+++ b/test/ELF/mips-align-err.s
@@ -3,7 +3,7 @@
 # RUN:         -mcpu=mips32r6
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
 # RUN:         -mcpu=mips32r6 %S/Inputs/mips-align-err.s -o %t2.o
-# RUN: not ld.lld %t.o %t2.o -o %t.exe 2>&1 | FileCheck %s
+# RUN: not ld.lld %t.o %t2.o -o /dev/null 2>&1 | FileCheck %s
 # CHECK: {{.*}}:(.text+0x1): improper alignment for relocation R_MIPS_PC16: 0xB is not aligned to 4 bytes
 
         .globl  __start
diff --git a/test/ELF/mips-call-hilo.s b/test/ELF/mips-call-hilo.s
index 2504612..9c7633a 100644
--- a/test/ELF/mips-call-hilo.s
+++ b/test/ELF/mips-call-hilo.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check R_MIPS_CALL_HI16 / R_MIPS_CALL_LO16 relocations calculation.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
@@ -5,8 +6,6 @@
 # RUN: llvm-objdump -d %t.so | FileCheck %s
 # RUN: llvm-readobj -r -mips-plt-got %t.so | FileCheck -check-prefix=GOT %s
 
-# REQUIRES: mips
-
 # CHECK:      Disassembly of section .text:
 # CHECK-NEXT: foo:
 # CHECK-NEXT:    10000:       3c 02 00 00     lui     $2, 0
diff --git a/test/ELF/mips-call16.s b/test/ELF/mips-call16.s
index 4a5d0bf..7b3da0c 100644
--- a/test/ELF/mips-call16.s
+++ b/test/ELF/mips-call16.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check R_MIPS_CALL16 relocation calculation.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
@@ -6,8 +7,6 @@
 # RUN: llvm-readobj -mips-plt-got -symbols %t.exe \
 # RUN:   | FileCheck -check-prefix=GOT %s
 
-# REQUIRES: mips
-
   .text
   .globl  __start
 __start:
diff --git a/test/ELF/mips-dynamic.s b/test/ELF/mips-dynamic.s
index 820776b..ebc2625 100644
--- a/test/ELF/mips-dynamic.s
+++ b/test/ELF/mips-dynamic.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check MIPS specific .dynamic section entries.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
@@ -20,8 +21,6 @@
 # RUN: llvm-readobj -sections -dyn-symbols -dynamic-table %t.so \
 # RUN:   | FileCheck -check-prefix=DSO %s
 
-# REQUIRES: mips
-
 # EXE:      Sections [
 # EXE:          Name: .dynamic
 # EXE-NEXT:     Type: SHT_DYNAMIC
diff --git a/test/ELF/mips-dynsym-sort.s b/test/ELF/mips-dynsym-sort.s
index 7d4559c..d1b935b 100644
--- a/test/ELF/mips-dynsym-sort.s
+++ b/test/ELF/mips-dynsym-sort.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check the order of dynamic symbols for the MIPS target.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t-be.o
@@ -8,8 +9,6 @@
 # RUN: ld.lld -shared %t-el.o -o %t-el.so
 # RUN: llvm-readobj -symbols -dyn-symbols %t-el.so | FileCheck %s
 
-# REQUIRES: mips
-
   .data
   .globl v1,v2,v3
 v1:
diff --git a/test/ELF/mips-elf-abi.s b/test/ELF/mips-elf-abi.s
index 8721b48..86c02f3 100644
--- a/test/ELF/mips-elf-abi.s
+++ b/test/ELF/mips-elf-abi.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check EI_ABIVERSION flags
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
@@ -10,8 +11,6 @@
 # RUN: ld.lld -r -o %t.rel %t.o
 # RUN: llvm-readobj -h %t.rel | FileCheck -check-prefix=REL %s
 
-# REQUIRES: mips
-
 # DSO: ABIVersion: 0
 # EXE: ABIVersion: 1
 # PIE: ABIVersion: 0
diff --git a/test/ELF/mips-elf-flags-err.s b/test/ELF/mips-elf-flags-err.s
index e1ac8c5..caa33ab 100644
--- a/test/ELF/mips-elf-flags-err.s
+++ b/test/ELF/mips-elf-flags-err.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check MIPS ELF ISA flag calculation if input files have different ISAs.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
@@ -24,14 +25,6 @@
 # RUN: not ld.lld %t1.o %t2.o -o %t.exe 2>&1 \
 # RUN:   | FileCheck -check-prefix=R6OCTEON %s
 
-# Check that lld does not allow to link incompatible floating point ABI.
-
-# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
-# RUN:         -mcpu=mips32 %S/Inputs/mips-dynamic.s -o %t1.o
-# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
-# RUN:         -mcpu=mips32 -mattr=+fp64 %s -o %t2.o
-# RUN: not ld.lld %t1.o %t2.o -o %t.exe 2>&1 | FileCheck -check-prefix=FPABI %s
-
 # Check that lld take in account EF_MIPS_MACH_XXX ISA flags
 
 # RUN: llvm-mc -filetype=obj -triple=mips64-unknown-linux \
@@ -41,24 +34,6 @@
 # RUN: ld.lld %t1.o %t2.o -o %t.exe
 # RUN: llvm-readobj -h %t.exe | FileCheck -check-prefix=OCTEON %s
 
-# Check that lld does not allow to link incompatible ABIs.
-
-# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
-# RUN:         -target-abi n32 %S/Inputs/mips-dynamic.s -o %t1.o
-# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
-# RUN:         -target-abi o32 %s -o %t2.o
-# RUN: not ld.lld %t1.o %t2.o -o %t.exe 2>&1 | FileCheck -check-prefix=N32O32 %s
-
-# Check that lld does not allow to link modules with incompatible NAN flags.
-
-# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
-# RUN:         -mattr=+nan2008 %S/Inputs/mips-dynamic.s -o %t1.o
-# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
-# RUN:         %s -o %t2.o
-# RUN: not ld.lld %t1.o %t2.o -o %t.exe 2>&1 | FileCheck -check-prefix=NAN %s
-
-# REQUIRES: mips
-
   .option pic0
   .text
   .global  __start
@@ -79,14 +54,8 @@
 # R6OCTEON-NEXT: >>> {{.+}}mips-elf-flags-err.s.tmp1.o: mips64r6
 # R6OCTEON-NEXT: >>> {{.+}}mips-elf-flags-err.s.tmp2.o: mips64r2 (octeon)
 
-# FPABI: target floating point ABI '-mdouble-float' is incompatible with '-mgp32 -mfp64': {{.*}}mips-elf-flags-err.s.tmp2.o
-
 # OCTEON:      Flags [
 # OCTEON-NEXT:   EF_MIPS_ARCH_64R2
 # OCTEON-NEXT:   EF_MIPS_CPIC
 # OCTEON-NEXT:   EF_MIPS_MACH_OCTEON
 # OCTEON:      ]
-
-# N32O32: error: {{.*}}mips-elf-flags-err.s.tmp2.o is incompatible with {{.*}}mips-elf-flags-err.s.tmp1.o
-
-# NAN: target -mnan=2008 is incompatible with -mnan=legacy: {{.*}}mips-elf-flags-err.s.tmp2.o
diff --git a/test/ELF/mips-elf-flags-err.test b/test/ELF/mips-elf-flags-err.test
new file mode 100644
index 0000000..12fd418
--- /dev/null
+++ b/test/ELF/mips-elf-flags-err.test
@@ -0,0 +1,89 @@
+# REQUIRES: mips
+#
+# Check warning and errors in case of input
+# files with incompatible ELF header flags.
+
+# RUN: yaml2obj -docnum 1 %s -o %t-n64.o
+# RUN: yaml2obj -docnum 2 %s -o %t-o64.o
+# RUN: yaml2obj -docnum 3 %s -o %t-n32.o
+# RUN: yaml2obj -docnum 4 %s -o %t-o32.o
+# RUN: yaml2obj -docnum 5 %s -o %t-eabi64.o
+# RUN: yaml2obj -docnum 6 %s -o %t-eabi32.o
+
+# RUN: not ld.lld %t-n64.o %t-eabi64.o -shared -o /dev/null 2>&1 \
+# RUN:   | FileCheck -check-prefixes=MM64,N64EABI64 %s
+#
+# RUN: not ld.lld %t-n64.o %t-o64.o -shared -o /dev/null 2>&1 \
+# RUN:   | FileCheck -check-prefixes=MM64,N64O64 %s
+
+# RUN: not ld.lld %t-o32.o %t-eabi32.o -shared -o /dev/null 2>&1 \
+# RUN:   | FileCheck -check-prefixes=O32EABI32,FP64,CPIC1 %s
+
+# RUN: not ld.lld %t-eabi32.o %t-o32.o -shared -o /dev/null 2>&1 \
+# RUN:   | FileCheck -check-prefix=CPIC2 %s
+
+# MM64: {{.*}}n64.o: microMIPS 64-bit is not supported
+
+# N64EABI64: {{.*}}eabi64.o: ABI 'eabi64' is incompatible with target ABI 'n64'
+# N64O64: {{.*}}o64.o: ABI 'o64' is incompatible with target ABI 'n64'
+# O32EABI32: {{.*}}eabi32.o: ABI 'eabi32' is incompatible with target ABI 'o32'
+
+# NAN: {{.*}}o32.o: -mnan=legacy is incompatible with target -mnan=2008
+# FP64: {{.*}}eabi32.o: -mfp64 is incompatible with target -mfp32
+
+# CPIC1: {{.*}}tmp-eabi32.o: linking non-abicalls code with abicalls code {{.*}}o32.o
+# CPIC2: {{.*}}tmp-o32.o: linking abicalls code with non-abicalls code {{.*}}eabi32.o
+
+# n64.o
+--- !ELF
+FileHeader:
+  Class:    ELFCLASS64
+  Data:     ELFDATA2MSB
+  Type:     ET_REL
+  Machine:  EM_MIPS
+  Flags:    [ EF_MIPS_ARCH_64, EF_MIPS_MICROMIPS ]
+
+# o64.o
+--- !ELF
+FileHeader:
+  Class:    ELFCLASS64
+  Data:     ELFDATA2MSB
+  Type:     ET_REL
+  Machine:  EM_MIPS
+  Flags:    [ EF_MIPS_ABI_O64, EF_MIPS_ARCH_64 ]
+
+# n32.o
+--- !ELF
+FileHeader:
+  Class:    ELFCLASS32
+  Data:     ELFDATA2MSB
+  Type:     ET_REL
+  Machine:  EM_MIPS
+  Flags:    [ EF_MIPS_ARCH_64, EF_MIPS_ABI2, EF_MIPS_NAN2008 ]
+
+# o32.o
+--- !ELF
+FileHeader:
+  Class:    ELFCLASS32
+  Data:     ELFDATA2MSB
+  Type:     ET_REL
+  Machine:  EM_MIPS
+  Flags:    [ EF_MIPS_ARCH_32, EF_MIPS_ABI_O32, EF_MIPS_CPIC ]
+
+# eabi64.o
+--- !ELF
+FileHeader:
+  Class:    ELFCLASS64
+  Data:     ELFDATA2MSB
+  Type:     ET_REL
+  Machine:  EM_MIPS
+  Flags:    [ EF_MIPS_ARCH_64, EF_MIPS_ABI_EABI64 ]
+
+# eabi32.o
+--- !ELF
+FileHeader:
+  Class:    ELFCLASS32
+  Data:     ELFDATA2MSB
+  Type:     ET_REL
+  Machine:  EM_MIPS
+  Flags:    [ EF_MIPS_ARCH_32, EF_MIPS_ABI_EABI32, EF_MIPS_FP64 ]
diff --git a/test/ELF/mips-elf-flags.s b/test/ELF/mips-elf-flags.s
index d2b3d92..68f4cc3 100644
--- a/test/ELF/mips-elf-flags.s
+++ b/test/ELF/mips-elf-flags.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check generation of MIPS specific ELF header flags.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
@@ -41,8 +42,6 @@
 # RUN: ld.lld %t.o %t-mm.o -o %t.exe
 # RUN: llvm-readobj -h -mips-abi-flags %t.exe | FileCheck -check-prefix=MICRO %s
 
-# REQUIRES: mips
-
   .text
   .globl  __start
 __start:
diff --git a/test/ELF/mips-fp-flags-err.test b/test/ELF/mips-fp-flags-err.test
new file mode 100644
index 0000000..68a290c
--- /dev/null
+++ b/test/ELF/mips-fp-flags-err.test
@@ -0,0 +1,162 @@
+# REQUIRES: mips
+#
+# Check warning and errors in case of input
+# files with incompatible floating point ABI flags.
+
+# RUN: yaml2obj -docnum 1 %s -o %t-dbl.o
+# RUN: yaml2obj -docnum 2 %s -o %t-sgl.o
+# RUN: yaml2obj -docnum 3 %s -o %t-soft.o
+# RUN: yaml2obj -docnum 4 %s -o %t-fp64.o
+# RUN: yaml2obj -docnum 5 %s -o %t-fp64old.o
+# RUN: yaml2obj -docnum 6 %s -o %t-fp64a.o
+# RUN: yaml2obj -docnum 7 %s -o %t-fpxx.o
+
+# RUN: not ld.lld %t-dbl.o %t-fp64.o -shared -o /dev/null 2>&1 \
+# RUN:   | FileCheck -check-prefixes=DBLFP64 %s
+
+# RUN: not ld.lld %t-sgl.o %t-fp64old.o -shared -o /dev/null 2>&1 \
+# RUN:   | FileCheck -check-prefixes=SGLFP64OLD %s
+
+# RUN: not ld.lld %t-soft.o %t-fp64a.o -shared -o /dev/null 2>&1 \
+# RUN:   | FileCheck -check-prefixes=SOFTFP64A %s
+
+# RUN: not ld.lld %t-sgl.o %t-fpxx.o -shared -o /dev/null 2>&1 \
+# RUN:   | FileCheck -check-prefixes=SGLFPXX %s
+
+# DBLFP64: {{.*}}fp64.o: floating point ABI '-mgp32 -mfp64' is incompatible with target floating point ABI '-mdouble-float'
+# SGLFP64OLD: {{.*}}fp64old.o: floating point ABI '-mgp32 -mfp64 (old)' is incompatible with target floating point ABI '-msingle-float'
+# SOFTFP64A: {{.*}}fp64a.o: floating point ABI '-mgp32 -mfp64  -mno-odd-spreg' is incompatible with target floating point ABI '-msoft-float'
+# SGLFPXX: {{.*}}fpxx.o: floating point ABI '-mfpxx' is incompatible with target floating point ABI '-msingle-float'
+
+# dbl.o
+--- !ELF
+FileHeader:
+  Class:    ELFCLASS64
+  Data:     ELFDATA2MSB
+  Type:     ET_REL
+  Machine:  EM_MIPS
+  Flags:    [ EF_MIPS_ARCH_64 ]
+
+Sections:
+- Name: .MIPS.abiflags
+  Type: SHT_MIPS_ABIFLAGS
+  ISA:          MIPS64
+  ASEs:         []
+  FpABI:        FP_DOUBLE
+  GPRSize:      REG_64
+  CPR1Size:     REG_64
+  CPR2Size:     REG_NONE
+
+# sgl.o
+--- !ELF
+FileHeader:
+  Class:    ELFCLASS64
+  Data:     ELFDATA2MSB
+  Type:     ET_REL
+  Machine:  EM_MIPS
+  Flags:    [ EF_MIPS_ARCH_64 ]
+
+Sections:
+- Name: .MIPS.abiflags
+  Type: SHT_MIPS_ABIFLAGS
+  ISA:          MIPS64
+  ASEs:         []
+  FpABI:        FP_SINGLE
+  GPRSize:      REG_64
+  CPR1Size:     REG_64
+  CPR2Size:     REG_NONE
+
+# soft.o
+--- !ELF
+FileHeader:
+  Class:    ELFCLASS64
+  Data:     ELFDATA2MSB
+  Type:     ET_REL
+  Machine:  EM_MIPS
+  Flags:    [ EF_MIPS_ARCH_64 ]
+
+Sections:
+- Name: .MIPS.abiflags
+  Type: SHT_MIPS_ABIFLAGS
+  ISA:          MIPS64
+  ASEs:         []
+  FpABI:        FP_SOFT
+  GPRSize:      REG_64
+  CPR1Size:     REG_64
+  CPR2Size:     REG_NONE
+
+# fp64.o
+--- !ELF
+FileHeader:
+  Class:    ELFCLASS64
+  Data:     ELFDATA2MSB
+  Type:     ET_REL
+  Machine:  EM_MIPS
+  Flags:    [ EF_MIPS_ARCH_64 ]
+
+Sections:
+- Name: .MIPS.abiflags
+  Type: SHT_MIPS_ABIFLAGS
+  ISA:          MIPS64
+  ASEs:         []
+  FpABI:        FP_64
+  GPRSize:      REG_64
+  CPR1Size:     REG_64
+  CPR2Size:     REG_NONE
+
+# fp64old.o
+--- !ELF
+FileHeader:
+  Class:    ELFCLASS64
+  Data:     ELFDATA2MSB
+  Type:     ET_REL
+  Machine:  EM_MIPS
+  Flags:    [ EF_MIPS_ARCH_64 ]
+
+Sections:
+- Name: .MIPS.abiflags
+  Type: SHT_MIPS_ABIFLAGS
+  ISA:          MIPS64
+  ASEs:         []
+  FpABI:        FP_OLD_64
+  GPRSize:      REG_64
+  CPR1Size:     REG_64
+  CPR2Size:     REG_NONE
+
+# fp64a.o
+--- !ELF
+FileHeader:
+  Class:    ELFCLASS64
+  Data:     ELFDATA2MSB
+  Type:     ET_REL
+  Machine:  EM_MIPS
+  Flags:    [ EF_MIPS_ARCH_64 ]
+
+Sections:
+- Name: .MIPS.abiflags
+  Type: SHT_MIPS_ABIFLAGS
+  ISA:          MIPS64
+  ASEs:         []
+  FpABI:        FP_64A
+  GPRSize:      REG_64
+  CPR1Size:     REG_64
+  CPR2Size:     REG_NONE
+
+# fpxx.o
+--- !ELF
+FileHeader:
+  Class:    ELFCLASS64
+  Data:     ELFDATA2MSB
+  Type:     ET_REL
+  Machine:  EM_MIPS
+  Flags:    [ EF_MIPS_ARCH_64 ]
+
+Sections:
+- Name: .MIPS.abiflags
+  Type: SHT_MIPS_ABIFLAGS
+  ISA:          MIPS64
+  ASEs:         []
+  FpABI:        FP_XX
+  GPRSize:      REG_64
+  CPR1Size:     REG_64
+  CPR2Size:     REG_NONE
diff --git a/test/ELF/mips-gnu-hash.s b/test/ELF/mips-gnu-hash.s
index 288d540..e66bc89 100644
--- a/test/ELF/mips-gnu-hash.s
+++ b/test/ELF/mips-gnu-hash.s
@@ -1,15 +1,14 @@
+# REQUIRES: mips
 # Shouldn't allow the GNU hash style to be selected with the MIPS target.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t-be.o
-# RUN: not ld.lld -shared -hash-style=gnu %t-be.o -o %t-be.so 2>&1 | FileCheck %s
+# RUN: not ld.lld -shared -hash-style=gnu %t-be.o -o /dev/null 2>&1 | FileCheck %s
 
 # RUN: llvm-mc -filetype=obj -triple=mipsel-unknown-linux %s -o %t-el.o
-# RUN: not ld.lld -shared -hash-style=gnu %t-el.o -o %t-el.so 2>&1 | FileCheck %s
+# RUN: not ld.lld -shared -hash-style=gnu %t-el.o -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: the .gnu.hash section is not compatible with the MIPS target.
 
-# REQUIRES: mips
-
   .globl  __start
 __start:
   nop
diff --git a/test/ELF/mips-got-extsym.s b/test/ELF/mips-got-extsym.s
index 3af4ba0..ea57d77 100644
--- a/test/ELF/mips-got-extsym.s
+++ b/test/ELF/mips-got-extsym.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check creation of GOT entries for global symbols in case of executable
 # file linking. Symbols defined in DSO should get entries in the global part
 # of the GOT. Symbols defined in the executable itself should get local GOT
@@ -10,8 +11,6 @@
 # RUN: ld.lld %t.o %t.so -o %t.exe
 # RUN: llvm-readobj -dt -t -mips-plt-got %t.exe | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:      Symbols [
 # CHECK:        Symbol {
 # CHECK:          Name: _foo
diff --git a/test/ELF/mips-got-hilo.s b/test/ELF/mips-got-hilo.s
index fa7e752..1ae24f3 100644
--- a/test/ELF/mips-got-hilo.s
+++ b/test/ELF/mips-got-hilo.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check R_MIPS_GOT_HI16 / R_MIPS_GOT_LO16 relocations calculation.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
@@ -5,8 +6,6 @@
 # RUN: llvm-objdump -d %t.so | FileCheck %s
 # RUN: llvm-readobj -r -mips-plt-got %t.so | FileCheck -check-prefix=GOT %s
 
-# REQUIRES: mips
-
 # CHECK:      Disassembly of section .text:
 # CHECK-NEXT: foo:
 # CHECK-NEXT:    10000:       3c 02 00 00     lui     $2, 0
diff --git a/test/ELF/mips-got-page-script.s b/test/ELF/mips-got-page-script.s
index 056e4fd..0ec19fc 100644
--- a/test/ELF/mips-got-page-script.s
+++ b/test/ELF/mips-got-page-script.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check calculation of MIPS GOT page address entries number
 # when a linker script is provided.
 
@@ -8,8 +9,6 @@
 # RUN: ld.lld -shared --script %t.script -o %t.so %t.o
 # RUN: llvm-readobj -t -mips-plt-got %t.so | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:      Name: foo1
 # CHECK-NEXT: Value: 0x10000
 # CHECK:      Name: foo2
diff --git a/test/ELF/mips-got-page.s b/test/ELF/mips-got-page.s
index e2dc485..46ddf4a 100644
--- a/test/ELF/mips-got-page.s
+++ b/test/ELF/mips-got-page.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check the case when small section (less that 0x10000 bytes) occupies
 # two adjacent 0xffff-bytes pages. We need to create two GOT entries
 # for R_MIPS_GOT_PAGE relocations.
@@ -6,8 +7,6 @@
 # RUN: ld.lld --section-start .rodata=0x27FFC -shared -o %t.so %t.o
 # RUN: llvm-readobj -t -mips-plt-got %t.so | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:       Name: bar
 # CHECK-NEXT:  Value: 0x28000
 #                     ^ page-address = (0x28000 + 0x8000) & ~0xffff = 0x30000
diff --git a/test/ELF/mips-got-redundant.s b/test/ELF/mips-got-redundant.s
index b4c6a2b..24138ca 100644
--- a/test/ELF/mips-got-redundant.s
+++ b/test/ELF/mips-got-redundant.s
@@ -1,11 +1,10 @@
+# REQUIRES: mips
 # Check number of redundant entries in the local part of MIPS GOT.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
 # RUN: ld.lld %t.o -shared -o %t.so
 # RUN: llvm-readobj -mips-plt-got %t.so | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:      Local entries [
 # CHECK-NEXT:   Entry {
 # CHECK-NEXT:     Address:
diff --git a/test/ELF/mips-got-relocs.s b/test/ELF/mips-got-relocs.s
index 5b443e5..d085df0 100644
--- a/test/ELF/mips-got-relocs.s
+++ b/test/ELF/mips-got-relocs.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check R_MIPS_GOT16 relocation calculation.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t-be.o
@@ -30,8 +31,6 @@
 # RUN: llvm-readobj -relocations %t-el.so | FileCheck -check-prefix=NORELOC %s
 # RUN: llvm-readobj -sections %t-el.so | FileCheck -check-prefix=SHFLAGS %s
 
-# REQUIRES: mips
-
   .text
   .globl  __start
 __start:
diff --git a/test/ELF/mips-got-script.s b/test/ELF/mips-got-script.s
index da18584..6590c59 100644
--- a/test/ELF/mips-got-script.s
+++ b/test/ELF/mips-got-script.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check number of got entries is adjusted for linker script-added space.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
@@ -5,10 +6,8 @@
 # RUN: ld.lld %t.o -shared -o %t.so -T %t.script
 # RUN: llvm-readobj -mips-plt-got -dynamic-table %t.so | FileCheck %s
 
-# REQUIRES: mips
-
-# CHECK: 0x7000000A MIPS_LOCAL_GOTNO 5
-#                                    ^-- 2 * header + 3 local entries
+# CHECK: 0x7000000A MIPS_LOCAL_GOTNO 4
+#                                    ^-- 2 * header + 2 local entries
 # CHECK:      Local entries [
 # CHECK-NEXT:   Entry {
 # CHECK-NEXT:     Address:
@@ -22,12 +21,6 @@
 # CHECK-NEXT:     Initial: 0x10000
 #                          ^-- loc2
 # CHECK-NEXT:   }
-# CHECK-NEXT:   Entry {
-# CHECK-NEXT:     Address:
-# CHECK-NEXT:     Access: -32736
-# CHECK-NEXT:     Initial: 0x20000
-#                          ^-- redundant
-# CHECK-NEXT:   }
 # CHECK-NEXT: ]
 
   .text
diff --git a/test/ELF/mips-got-string.s b/test/ELF/mips-got-string.s
index aec80dd..cfdd0da 100644
--- a/test/ELF/mips-got-string.s
+++ b/test/ELF/mips-got-string.s
@@ -1,14 +1,13 @@
+# REQUIRES: mips
 # Check R_MIPS_GOT16 relocation against merge section.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux -o %t.o %s
 # RUN: ld.lld -shared -o %t.so %t.o
 # RUN: llvm-readobj -t -mips-plt-got %t.so | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:      Symbol {
 # CHECK:        Name: $.str
-# CHECK-NEXT:   Value: 0x105
+# CHECK-NEXT:   Value: 0x1B1
 # CHECK:      }
 
 # CHECK:      Local entries [
diff --git a/test/ELF/mips-got-weak.s b/test/ELF/mips-got-weak.s
index e860bb4..478e294 100644
--- a/test/ELF/mips-got-weak.s
+++ b/test/ELF/mips-got-weak.s
@@ -1,17 +1,16 @@
+# REQUIRES: mips
 # Check R_MIPS_GOT16 relocation against weak symbols.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
 # RUN: ld.lld %t.o -shared -o %t1.so
 # RUN: llvm-readobj -r -dt -dynamic-table -mips-plt-got %t1.so \
-# RUN:   | FileCheck -check-prefix=NOSYM %s
+# RUN:   | FileCheck -check-prefixes=CHECK,NOSYM %s
 # RUN: ld.lld %t.o -shared -Bsymbolic -o %t2.so
 # RUN: llvm-readobj -r -dt -dynamic-table -mips-plt-got %t2.so \
-# RUN:   | FileCheck -check-prefix=SYM %s
+# RUN:   | FileCheck -check-prefixes=CHECK,SYM %s
 
-# REQUIRES: mips
-
-# NOSYM:      Relocations [
-# NOSYM-NEXT: ]
+# CHECK:      Relocations [
+# CHECK-NEXT: ]
 
 # NOSYM:        Symbol {
 # NOSYM:          Name: foo
@@ -22,17 +21,19 @@
 # NOSYM-NEXT:     Other: 0
 # NOSYM-NEXT:     Section: .data
 # NOSYM-NEXT:   }
-# NOSYM-NEXT:   Symbol {
-# NOSYM-NEXT:     Name: bar
-# NOSYM-NEXT:     Value: 0x0
-# NOSYM-NEXT:     Size: 0
-# NOSYM-NEXT:     Binding: Weak
-# NOSYM-NEXT:     Type: None
-# NOSYM-NEXT:     Other: 0
-# NOSYM-NEXT:     Section: Undefined
-# NOSYM-NEXT:   }
-# NOSYM-NEXT:   Symbol {
-# NOSYM-NEXT:     Name: sym
+
+# CHECK:        Symbol {
+# CHECK:          Name: bar
+# CHECK-NEXT:     Value: 0x0
+# CHECK-NEXT:     Size: 0
+# CHECK-NEXT:     Binding: Weak
+# CHECK-NEXT:     Type: None
+# CHECK-NEXT:     Other: 0
+# CHECK-NEXT:     Section: Undefined
+# CHECK-NEXT:   }
+
+# NOSYM:        Symbol {
+# NOSYM:          Name: sym
 # NOSYM-NEXT:     Value: 0x20004
 # NOSYM-NEXT:     Size: 0
 # NOSYM-NEXT:     Binding: Global
@@ -40,30 +41,48 @@
 # NOSYM-NEXT:     Other: 0
 # NOSYM-NEXT:     Section: .data
 # NOSYM-NEXT:   }
-# NOSYM-NEXT: ]
 
-# NOSYM:      0x70000011 MIPS_SYMTABNO        4
-# NOSYM-NEXT: 0x7000000A MIPS_LOCAL_GOTNO     2
-# NOSYM-NEXT: 0x70000013 MIPS_GOTSYM          0x1
+# CHECK:      0x70000011 MIPS_SYMTABNO        4
 
-# NOSYM:      Primary GOT {
-# NOSYM-NEXT:   Canonical gp value:
-# NOSYM-NEXT:   Reserved entries [
-# NOSYM-NEXT:     Entry {
-# NOSYM-NEXT:       Address:
-# NOSYM-NEXT:       Access: -32752
-# NOSYM-NEXT:       Initial: 0x0
-# NOSYM-NEXT:       Purpose: Lazy resolver
-# NOSYM-NEXT:     }
-# NOSYM-NEXT:     Entry {
-# NOSYM-NEXT:       Address:
-# NOSYM-NEXT:       Access: -32748
-# NOSYM-NEXT:       Initial: 0x80000000
-# NOSYM-NEXT:       Purpose: Module pointer (GNU extension)
-# NOSYM-NEXT:     }
+# SYM:        0x7000000A MIPS_LOCAL_GOTNO     4
+# SYM:        0x70000013 MIPS_GOTSYM          0x3
+
+# NOSYM:      0x7000000A MIPS_LOCAL_GOTNO     2
+# NOSYM:      0x70000013 MIPS_GOTSYM          0x1
+
+# CHECK:      Primary GOT {
+# CHECK-NEXT:   Canonical gp value:
+# CHECK-NEXT:   Reserved entries [
+# CHECK:        ]
+
+# SYM:         Local entries [
+# SYM-NEXT:       Entry {
+# SYM-NEXT:         Address:
+# SYM-NEXT:         Access: -32744
+# SYM-NEXT:         Initial: 0x20000
+# SYM-NEXT:       }
+# SYM-NEXT:       Entry {
+# SYM-NEXT:         Address:
+# SYM-NEXT:         Access: -32740
+# SYM-NEXT:         Initial: 0x20004
+# SYM-NEXT:       }
+# SYM-NEXT:     ]
+
+# NOSYM:        Local entries [
 # NOSYM-NEXT:   ]
-# NOSYM-NEXT:   Local entries [
-# NOSYM-NEXT:   ]
+
+# SYM-NEXT:     Global entries [
+# SYM-NEXT:       Entry {
+# SYM-NEXT:         Address:
+# SYM-NEXT:         Access: -32736
+# SYM-NEXT:         Initial: 0x0
+# SYM-NEXT:         Value: 0x0
+# SYM-NEXT:         Type: None
+# SYM-NEXT:         Section: Undefined
+# SYM-NEXT:         Name: bar
+# SYM-NEXT:       }
+# SYM-NEXT:     ]
+
 # NOSYM-NEXT:   Global entries [
 # NOSYM-NEXT:     Entry {
 # NOSYM-NEXT:       Address:
@@ -93,68 +112,9 @@
 # NOSYM-NEXT:       Name: sym
 # NOSYM-NEXT:     }
 # NOSYM-NEXT:   ]
-# NOSYM-NEXT:   Number of TLS and multi-GOT entries: 0
-# NOSYM-NEXT: }
 
-# SYM:      Relocations [
-# SYM-NEXT: ]
-
-# SYM:        Symbol {
-# SYM:          Name: bar
-# SYM-NEXT:     Value: 0x0
-# SYM-NEXT:     Size: 0
-# SYM-NEXT:     Binding: Weak
-# SYM-NEXT:     Type: None
-# SYM-NEXT:     Other: 0
-# SYM-NEXT:     Section: Undefined
-# SYM-NEXT:   }
-# SYM-NEXT: ]
-
-# SYM:      0x70000011 MIPS_SYMTABNO        4
-# SYM-NEXT: 0x7000000A MIPS_LOCAL_GOTNO     4
-# SYM-NEXT: 0x70000013 MIPS_GOTSYM          0x3
-
-# SYM:      Primary GOT {
-# SYM-NEXT:   Canonical gp value:
-# SYM-NEXT:   Reserved entries [
-# SYM-NEXT:     Entry {
-# SYM-NEXT:       Address:
-# SYM-NEXT:       Access: -32752
-# SYM-NEXT:       Initial: 0x0
-# SYM-NEXT:       Purpose: Lazy resolver
-# SYM-NEXT:     }
-# SYM-NEXT:     Entry {
-# SYM-NEXT:       Address:
-# SYM-NEXT:       Access: -32748
-# SYM-NEXT:       Initial: 0x80000000
-# SYM-NEXT:       Purpose: Module pointer (GNU extension)
-# SYM-NEXT:     }
-# SYM-NEXT:   ]
-# SYM-NEXT:   Local entries [
-# SYM-NEXT:     Entry {
-# SYM-NEXT:       Address:
-# SYM-NEXT:       Access: -32744
-# SYM-NEXT:       Initial: 0x20000
-# SYM-NEXT:     }
-# SYM-NEXT:     Entry {
-# SYM-NEXT:       Address:
-# SYM-NEXT:       Access: -32740
-# SYM-NEXT:       Initial: 0x20004
-# SYM-NEXT:     }
-# SYM-NEXT:   ]
-# SYM-NEXT:   Global entries [
-# SYM-NEXT:     Entry {
-# SYM-NEXT:       Address:
-# SYM-NEXT:       Access: -32736
-# SYM-NEXT:       Initial: 0x0
-# SYM-NEXT:       Value: 0x0
-# SYM-NEXT:       Type: None
-# SYM-NEXT:       Section: Undefined
-# SYM-NEXT:       Name: bar
-# SYM-NEXT:     }
-# SYM-NEXT:   ]
-# SYM-NEXT:   Number of TLS and multi-GOT entries: 0
-# SYM-NEXT: }
+# CHECK:        Number of TLS and multi-GOT entries: 0
+# CHECK-NEXT: }
 
   .text
   .global  sym
diff --git a/test/ELF/mips-got16-relocatable.s b/test/ELF/mips-got16-relocatable.s
index bbacfdb..04b7cbb 100644
--- a/test/ELF/mips-got16-relocatable.s
+++ b/test/ELF/mips-got16-relocatable.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check writing updated addend for R_MIPS_GOT16 relocation,
 # when produce a relocatable output.
 
@@ -7,8 +8,6 @@
 # RUN: ld.lld -shared -o %t.so %t
 # RUN: llvm-objdump -d %t.so | FileCheck -check-prefix=SO %s
 
-# REQUIRES: mips
-
 # OBJ:      Disassembly of section .text:
 # OBJ-NEXT: .text:
 # OBJ-NEXT:        0:       8f 99 00 00     lw      $25, 0($gp)
diff --git a/test/ELF/mips-got16.s b/test/ELF/mips-got16.s
index 6ad7b2b..cf0847d 100644
--- a/test/ELF/mips-got16.s
+++ b/test/ELF/mips-got16.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check R_MIPS_GOT16 relocation calculation.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
@@ -5,8 +6,6 @@
 # RUN: llvm-objdump -d -t %t.so | FileCheck %s
 # RUN: llvm-readobj -r -mips-plt-got %t.so | FileCheck -check-prefix=GOT %s
 
-# REQUIRES: mips
-
 # CHECK:      Disassembly of section .text:
 # CHECK-NEXT: __start:
 # CHECK-NEXT:    10000:       8f 88 80 18     lw      $8, -32744($gp)
diff --git a/test/ELF/mips-gp-disp-ver.s b/test/ELF/mips-gp-disp-ver.s
index 134a056..8eaee19 100644
--- a/test/ELF/mips-gp-disp-ver.s
+++ b/test/ELF/mips-gp-disp-ver.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # MIPS BFD linker puts _gp_disp symbol into DSO files and assigns zero
 # version definition index to it. This value means 'unversioned local symbol'
 # while _gp_disp is a section global symbol. We have to handle this bug
@@ -7,8 +8,6 @@
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
 # RUN: ld.lld %t.o %S/Inputs/mips-gp-dips-corrupt-ver.so
 
-# REQUIRES: mips
-
   .global __start
   .text
 __start:
diff --git a/test/ELF/mips-gp-disp.s b/test/ELF/mips-gp-disp.s
index 7a0fd64..1b4226d 100644
--- a/test/ELF/mips-gp-disp.s
+++ b/test/ELF/mips-gp-disp.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check that even if _gp_disp symbol is defined in the shared library
 # we use our own value.
 
@@ -9,8 +10,6 @@
 # RUN: llvm-objdump -d -t %t.so | FileCheck -check-prefix=DIS %s
 # RUN: llvm-readobj -relocations %t.so | FileCheck -check-prefix=REL %s
 
-# REQUIRES: mips
-
 # INT-SO:      Name: _gp_disp
 # INT-SO-NEXT: Value:
 # INT-SO-NEXT: Size:
diff --git a/test/ELF/mips-gp-ext.s b/test/ELF/mips-gp-ext.s
index eb9788c..9f12f13 100644
--- a/test/ELF/mips-gp-ext.s
+++ b/test/ELF/mips-gp-ext.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check that the linker use a value of _gp symbol defined
 # in a linker script to calculate GOT relocations.
 
@@ -24,47 +25,45 @@
 # RUN: ld.lld -shared -o %t.abs.so --script %t.abs.script %t.o
 # RUN: llvm-objdump -s -t %t.abs.so | FileCheck --check-prefix=ABS %s
 
-# REQUIRES: mips
+# REL:      Contents of section .reginfo:
+# REL-NEXT:  0018 10000104 00000000 00000000 00000000
+# REL-NEXT:  0028 00000000 000001ec
+#                          ^-- _gp
 
 # REL:      Contents of section .text:
-# REL-NEXT:  0030 3c080000 2108010c 8f82ffcc
+# REL-NEXT:  00e0 3c080000 2108010c 8f82ff1c
 #                 ^-- %hi(_gp_disp)
 #                          ^-- %lo(_gp_disp)
-#                                   ^-- 8 - (0x13c - 0x100)
+#                                   ^-- 8 - (0x1ec - 0x100)
 #                                       G - (GP - .got)
 
-# REL:      Contents of section .reginfo:
-# REL-NEXT:  0058 10000104 00000000 00000000 00000000
-# REL-NEXT:  0068 00000000 0000013c
-#                          ^-- _gp
-
 # REL:      Contents of section .data:
 # REL-NEXT:  00f0 fffffef4
-#                 ^-- 0x30-0x13c
+#                 ^-- 0x30-0x1ec
 #                     foo - GP
 
-# REL: 00000030         .text           00000000 foo
+# REL: 000000e0         .text           00000000 foo
 # REL: 00000000         *ABS*           00000000 .hidden _gp_disp
-# REL: 0000013c         *ABS*           00000000 .hidden _gp
+# REL: 000001ec         *ABS*           00000000 .hidden _gp
+
+# ABS:      Contents of section .reginfo:
+# ABS-NEXT:  0018 10000104 00000000 00000000 00000000
+# ABS-NEXT:  0028 00000000 00000200
+#                          ^-- _gp
 
 # ABS:      Contents of section .text:
-# ABS-NEXT:  0030 3c080000 210801d0 8f82ff08
+# ABS-NEXT:  00e0 3c080000 21080120 8f82ff08
 #                 ^-- %hi(_gp_disp)
 #                          ^-- %lo(_gp_disp)
 #                                   ^-- 8 - (0x200 - 0x100)
 #                                       G - (GP - .got)
 
-# ABS:      Contents of section .reginfo:
-# ABS-NEXT:  0058 10000104 00000000 00000000 00000000
-# ABS-NEXT:  0068 00000000 00000200
-#                          ^-- _gp
-
 # ABS:      Contents of section .data:
-# ABS-NEXT:  00f0 fffffe30
-#                 ^-- 0x30-0x200
+# ABS-NEXT:  00f0 fffffee0
+#                 ^-- 0xe0-0x200
 #                     foo - GP
 
-# ABS: 00000030         .text           00000000 foo
+# ABS: 000000e0         .text           00000000 foo
 # ABS: 00000000         *ABS*           00000000 .hidden _gp_disp
 # ABS: 00000200         *ABS*           00000000 .hidden _gp
 
diff --git a/test/ELF/mips-gp-local.s b/test/ELF/mips-gp-local.s
index 8bb3c23..1146af8 100644
--- a/test/ELF/mips-gp-local.s
+++ b/test/ELF/mips-gp-local.s
@@ -1,11 +1,10 @@
+# REQUIRES: mips
 # Check handling of relocations against __gnu_local_gp symbol.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
 # RUN: ld.lld -o %t.exe %t.o
 # RUN: llvm-objdump -d -t %t.exe | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:      Disassembly of section .text:
 # CHECK-NEXT: __start:
 # CHECK-NEXT:    20000:  3c 08 00 03  lui   $8, 3
diff --git a/test/ELF/mips-gp-lowest.s b/test/ELF/mips-gp-lowest.s
index ecc5f7b..46da503 100644
--- a/test/ELF/mips-gp-lowest.s
+++ b/test/ELF/mips-gp-lowest.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check that default _gp value is calculated relative
 # to the GP-relative section with the lowest address.
 
@@ -8,8 +9,6 @@
 # RUN: ld.lld %t.o --script %t.rel.script -shared -o %t.so
 # RUN: llvm-readobj -s -t %t.so | FileCheck %s
 
-# REQUIRES: mips
-
   .text
   .global foo
 foo:
diff --git a/test/ELF/mips-gprel-sec.s b/test/ELF/mips-gprel-sec.s
index dc54f87..7517983 100644
--- a/test/ELF/mips-gprel-sec.s
+++ b/test/ELF/mips-gprel-sec.s
@@ -1,11 +1,10 @@
+# REQUIRES: mips
 # Check order of gp-relative sections, i.e. sections with SHF_MIPS_GPREL flag.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
 # RUN: ld.lld %t.o -shared -o %t.so
 # RUN: llvm-readobj -s %t.so | FileCheck %s
 
-# REQUIRES: mips
-
   .text
   nop
 
diff --git a/test/ELF/mips-gprel32-relocs-gp0.s b/test/ELF/mips-gprel32-relocs-gp0.s
index f27caa3..1abdeec 100644
--- a/test/ELF/mips-gprel32-relocs-gp0.s
+++ b/test/ELF/mips-gprel32-relocs-gp0.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check that relocatable object produced by LLD has zero gp0 value.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
@@ -8,8 +9,6 @@
 # RUN: llvm-readobj -mips-reginfo %t.so | FileCheck --check-prefix=DSO %s
 # RUN: llvm-objdump -s -t %t.so | FileCheck --check-prefix=DUMP %s
 
-# REQUIRES: mips
-
 # REL: GP: 0x0
 
 # DSO: GP: 0x27FF0
diff --git a/test/ELF/mips-gprel32-relocs.s b/test/ELF/mips-gprel32-relocs.s
index 047165f..8f31aa8 100644
--- a/test/ELF/mips-gprel32-relocs.s
+++ b/test/ELF/mips-gprel32-relocs.s
@@ -1,11 +1,10 @@
+# REQUIRES: mips
 # Check R_MIPS_GPREL32 relocation calculation.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
 # RUN: ld.lld -shared -o %t.so %t.o
 # RUN: llvm-objdump -s -section=.rodata -t %t.so | FileCheck %s
 
-# REQUIRES: mips
-
   .text
   .globl  __start
 __start:
diff --git a/test/ELF/mips-higher-highest.s b/test/ELF/mips-higher-highest.s
index 123b51a..3af7dcb 100644
--- a/test/ELF/mips-higher-highest.s
+++ b/test/ELF/mips-higher-highest.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check R_MIPS_HIGHER / R_MIPS_HIGHEST relocations calculation.
 
 # RUN: llvm-mc -filetype=obj -triple=mips64-unknown-linux %s -o %t1.o
@@ -6,8 +7,6 @@
 # RUN: ld.lld %t1.o %t2.o -o %t.exe
 # RUN: llvm-objdump -d %t.exe | FileCheck %s
 
-# REQUIRES: mips
-
   .global  __start
 __start:
   lui     $6, %highest(_foo+0x300047FFF7FF7)
diff --git a/test/ELF/mips-hilo-gp-disp.s b/test/ELF/mips-hilo-gp-disp.s
index c7229ee..997074e 100644
--- a/test/ELF/mips-hilo-gp-disp.s
+++ b/test/ELF/mips-hilo-gp-disp.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check R_MIPS_HI16 / LO16 relocations calculation against _gp_disp.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t1.o
@@ -8,8 +9,6 @@
 # RUN: ld.lld %t1.o %t2.o -shared -o %t.so
 # RUN: llvm-objdump -d -t %t.so | FileCheck -check-prefix=SO %s
 
-# REQUIRES: mips
-
   .text
   .globl  __start
 __start:
diff --git a/test/ELF/mips-hilo-hi-only.s b/test/ELF/mips-hilo-hi-only.s
index 0858e3f..6fd4c68 100644
--- a/test/ELF/mips-hilo-hi-only.s
+++ b/test/ELF/mips-hilo-hi-only.s
@@ -1,11 +1,10 @@
+# REQUIRES: mips
 # Check warning on orphaned R_MIPS_HI16 relocations.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
 # RUN: ld.lld %t.o -o %t.exe 2>&1 | FileCheck -check-prefix=WARN %s
 # RUN: llvm-objdump -d -t %t.exe | FileCheck %s
 
-# REQUIRES: mips
-
   .text
   .globl  __start
 __start:
diff --git a/test/ELF/mips-hilo.s b/test/ELF/mips-hilo.s
index d5de942..a00ffaa 100644
--- a/test/ELF/mips-hilo.s
+++ b/test/ELF/mips-hilo.s
@@ -1,11 +1,10 @@
+# REQUIRES: mips
 # Check R_MIPS_HI16 / LO16 relocations calculation.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
 # RUN: ld.lld %t.o -o %t.exe
 # RUN: llvm-objdump -d -t %t.exe | FileCheck %s
 
-# REQUIRES: mips
-
   .text
   .globl  __start
 __start:
diff --git a/test/ELF/mips-lo16-not-relative.s b/test/ELF/mips-lo16-not-relative.s
index 614e639..2af1eea 100644
--- a/test/ELF/mips-lo16-not-relative.s
+++ b/test/ELF/mips-lo16-not-relative.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check that R_MIPS_LO16 relocation is handled as non-relative,
 # and if a target symbol is a DSO data symbol, LLD create a copy
 # relocation.
@@ -9,8 +10,6 @@
 # RUN: ld.lld %t.o %t.so -o %t.exe
 # RUN: llvm-readobj -r %t.exe | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:      Relocations [
 # CHECK-NEXT:   Section (7) .rel.dyn {
 # CHECK-NEXT:     0x{{[0-9A-F]+}} R_MIPS_COPY data0 0x0
diff --git a/test/ELF/mips-merge-abiflags.s b/test/ELF/mips-merge-abiflags.s
index 2e8b43b..d061c1b 100644
--- a/test/ELF/mips-merge-abiflags.s
+++ b/test/ELF/mips-merge-abiflags.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Test that lld handles input files with concatenated .MIPS.abiflags sections
 # This happens e.g. with the FreeBSD BFD (BFD 2.17.50 [FreeBSD] 2007-07-03)
 
@@ -8,7 +9,6 @@
 # RUN:     %p/Inputs/mips-concatenated-abiflags.o | \
 # RUN:   FileCheck --check-prefix=INPUT-OBJECT %s
 
-# REQUIRES: mips
         .globl  __start
 __start:
         nop
diff --git a/test/ELF/mips-mgot.s b/test/ELF/mips-mgot.s
new file mode 100644
index 0000000..0bb1a76
--- /dev/null
+++ b/test/ELF/mips-mgot.s
@@ -0,0 +1,117 @@
+# REQUIRES: mips
+# Check MIPS multi-GOT layout.
+
+# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t0.o
+# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
+# RUN:         %p/Inputs/mips-mgot-1.s -o %t1.o
+# RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
+# RUN:         %p/Inputs/mips-mgot-2.s -o %t2.o
+# RUN: ld.lld -shared -mips-got-size 52 %t0.o %t1.o %t2.o -o %t.so
+# RUN: llvm-objdump -s -section=.got -t %t.so | FileCheck %s
+# RUN: llvm-readobj -r -dt -mips-plt-got %t.so | FileCheck -check-prefix=GOT %s
+
+# CHECK:      Contents of section .got:
+# CHECK-NEXT:  60000 00000000 80000000 00010000 00010030
+# CHECK-NEXT:  60010 00000000 00000004 00020000 00030000
+# CHECK-NEXT:  60020 00040000 00050000 00060000 00070000
+# CHECK-NEXT:  60030 00000000 00000000 00000000 00000000
+# CHECK-NEXT:  60040 00000000 00000000 00000000
+
+# CHECK: SYMBOL TABLE:
+# CHECK: 00000000 l       .tdata          00000000 loc0
+# CHECK: 00010000         .text           00000000 foo0
+# CHECK: 00000000 g       .tdata          00000000 tls0
+# CHECK: 00010020         .text           00000000 foo1
+# CHECK: 00000004 g       .tdata          00000000 tls1
+# CHECK: 00010030         .text           00000000 foo2
+# CHECK: 00000008 g       .tdata          00000000 tls2
+
+# GOT:      Relocations [
+# GOT-NEXT:   Section (7) .rel.dyn {
+# GOT-NEXT:     0x60018 R_MIPS_REL32 - 0x0
+# GOT-NEXT:     0x6001C R_MIPS_REL32 - 0x0
+# GOT-NEXT:     0x60020 R_MIPS_REL32 - 0x0
+# GOT-NEXT:     0x60024 R_MIPS_REL32 - 0x0
+# GOT-NEXT:     0x60028 R_MIPS_REL32 - 0x0
+# GOT-NEXT:     0x6002C R_MIPS_REL32 - 0x0
+# GOT-NEXT:     0x60030 R_MIPS_REL32 foo0 0x0
+# GOT-NEXT:     0x60034 R_MIPS_REL32 foo2 0x0
+# GOT-NEXT:     0x60044 R_MIPS_TLS_DTPMOD32 - 0x0
+# GOT-NEXT:     0x60010 R_MIPS_TLS_TPREL32 tls0 0x0
+# GOT-NEXT:     0x60038 R_MIPS_TLS_TPREL32 tls0 0x0
+# GOT-NEXT:     0x6003C R_MIPS_TLS_DTPMOD32 tls0 0x0
+# GOT-NEXT:     0x60040 R_MIPS_TLS_DTPREL32 tls0 0x0
+# GOT-NEXT:     0x60014 R_MIPS_TLS_TPREL32 tls1 0x0
+# GOT-NEXT:   }
+# GOT-NEXT: ]
+
+# GOT:      DynamicSymbols [
+# GOT:        Symbol {
+# GOT:          Name: foo0
+# GOT-NEXT:     Value: 0x10000
+# GOT:        }
+# GOT-NEXT:   Symbol {
+# GOT-NEXT:     Name: foo2
+# GOT-NEXT:     Value: 0x10030
+# GOT:        }
+# GOT-NEXT: ]
+
+# GOT:      Primary GOT {
+# GOT-NEXT:   Canonical gp value: 0x67FF0
+# GOT-NEXT:   Reserved entries [
+# GOT-NEXT:     Entry {
+# GOT-NEXT:       Address:
+# GOT-NEXT:       Access: -32752
+# GOT-NEXT:       Initial: 0x0
+# GOT-NEXT:       Purpose: Lazy resolver
+# GOT-NEXT:     }
+# GOT-NEXT:     Entry {
+# GOT-NEXT:       Address:
+# GOT-NEXT:       Access: -32748
+# GOT-NEXT:       Initial: 0x80000000
+# GOT-NEXT:       Purpose: Module pointer (GNU extension)
+# GOT-NEXT:     }
+# GOT-NEXT:   ]
+# GOT-NEXT:   Local entries [
+# GOT-NEXT:   ]
+# GOT-NEXT:   Global entries [
+# GOT-NEXT:     Entry {
+# GOT-NEXT:       Address:
+# GOT-NEXT:       Access: -32744
+# GOT-NEXT:       Initial: 0x10000
+# GOT-NEXT:       Value: 0x10000
+# GOT-NEXT:       Type: None
+# GOT-NEXT:       Section: .text
+# GOT-NEXT:       Name: foo0
+# GOT-NEXT:     }
+# GOT-NEXT:     Entry {
+# GOT-NEXT:       Address:
+# GOT-NEXT:       Access: -32740
+# GOT-NEXT:       Initial: 0x10030
+# GOT-NEXT:       Value: 0x10030
+# GOT-NEXT:       Type: None
+# GOT-NEXT:       Section: .text
+# GOT-NEXT:       Name: foo2
+# GOT-NEXT:     }
+# GOT-NEXT:   ]
+# GOT-NEXT:   Number of TLS and multi-GOT entries: 15
+# GOT-NEXT: }
+
+  .text
+  .global foo0
+foo0:
+  lw     $2, %got(.data)($gp)     # page entry
+  addi   $2, $2, %lo(.data)
+  lw     $2, %call16(foo0)($gp)   # global entry
+  addiu  $2, $2, %tlsgd(tls0)     # tls gd entry
+  addiu  $2, $2, %gottprel(tls0)  # tls got entry
+  addiu  $2, $2, %tlsldm(loc0)    # tls ld entry
+
+  .data
+  .space 0x20000
+
+  .section .tdata,"awT",%progbits
+  .global tls0
+tls0:
+loc0:
+  .word 0
diff --git a/test/ELF/mips-micro-got.s b/test/ELF/mips-micro-got.s
index 8d077f2..a881e0a 100644
--- a/test/ELF/mips-micro-got.s
+++ b/test/ELF/mips-micro-got.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check microMIPS GOT relocations for O32 ABI.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux -mattr=micromips \
@@ -8,8 +9,6 @@
 # RUN: ld.lld %t1.o %t.so -o %t.exe
 # RUN: llvm-readobj -mips-plt-got %t.exe | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:      Local entries [
 # CHECK-NEXT:   Entry {
 # CHECK-NEXT:     Address:
diff --git a/test/ELF/mips-micro-got64.s b/test/ELF/mips-micro-got64.s
deleted file mode 100644
index 653bfbf..0000000
--- a/test/ELF/mips-micro-got64.s
+++ /dev/null
@@ -1,48 +0,0 @@
-# Check microMIPS GOT relocations for N64 ABI.
-
-# RUN: llvm-mc -filetype=obj -triple=mips64-unknown-linux -mattr=micromips \
-# RUN:         %s -o %t1.o
-# RUN: llvm-mc -filetype=obj -triple=mips64-unknown-linux -mattr=micromips \
-# RUN:         %S/Inputs/mips-dynamic.s -o %t2.o
-# RUN: ld.lld %t2.o -shared -o %t.so
-# RUN: ld.lld %t1.o %t.so -o %t.exe
-# RUN: llvm-readobj -mips-plt-got %t.exe | FileCheck %s
-
-# REQUIRES: mips
-
-# CHECK:      Local entries [
-# CHECK-NEXT:   Entry {
-# CHECK-NEXT:     Address:
-# CHECK-NEXT:     Access: -32736
-# CHECK-NEXT:     Initial: 0x30000
-# CHECK-NEXT:   }
-# CHECK-NEXT:   Entry {
-# CHECK-NEXT:     Address:
-# CHECK-NEXT:     Access: -32728
-# CHECK-NEXT:     Initial: 0x40000
-# CHECK-NEXT:   }
-# CHECK-NEXT: ]
-# CHECK-NEXT: Global entries [
-# CHECK-NEXT:   Entry {
-# CHECK-NEXT:     Address:
-# CHECK-NEXT:     Access: -32720
-# CHECK-NEXT:     Initial: 0x0
-# CHECK-NEXT:     Value: 0x0
-# CHECK-NEXT:     Type: Function
-# CHECK-NEXT:     Section: Undefined
-# CHECK-NEXT:     Name: foo0
-# CHECK-NEXT:   }
-# CHECK-NEXT: ]
-
-  .text
-  .global __start
-__start:
-  lui     $28, %hi(%neg(%gp_rel(foo0)))
-  addiu   $28, $28, %lo(%neg(%gp_rel(foo0)))
-  lw       $4, %got_page(data)($28)
-  addiu    $4, $4, %got_ofst(data)
-  lw      $25, %call16(foo0)($28)
-
-  .data
-data:
-  .word 0
diff --git a/test/ELF/mips-micro-jal.s b/test/ELF/mips-micro-jal.s
index 8382612..18d41cf 100644
--- a/test/ELF/mips-micro-jal.s
+++ b/test/ELF/mips-micro-jal.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check PLT creation for microMIPS to microMIPS calls.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
@@ -45,8 +46,6 @@
 # RUN: llvm-objdump -d -mattr=micromips %teb.exe \
 # RUN:   | FileCheck --check-prefix=MIXED %s
 
-# REQUIRES: mips
-
 # EB:      Disassembly of section .plt:
 # EB-NEXT: .plt:
 # EB-NEXT:    20010:       79 80 3f fd     addiupc $3, 65524
diff --git a/test/ELF/mips-micro-plt.s b/test/ELF/mips-micro-plt.s
index 16f3bd4..6dcd6fb 100644
--- a/test/ELF/mips-micro-plt.s
+++ b/test/ELF/mips-micro-plt.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check less-significant bit setup for microMIPS PLT.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
@@ -9,8 +10,6 @@
 # RUN: llvm-readobj -t -dt -mips-plt-got %t.exe | FileCheck %s
 # RUN: llvm-objdump -d -mattr=micromips %t.exe | FileCheck --check-prefix=ASM %s
 
-# REQUIRES: mips
-
 # CHECK:      Symbols [
 # CHECK:        Symbol {
 # CHECK:          Name: foo
diff --git a/test/ELF/mips-micro-relocs.s b/test/ELF/mips-micro-relocs.s
index 3986711..b539aa9 100644
--- a/test/ELF/mips-micro-relocs.s
+++ b/test/ELF/mips-micro-relocs.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check handling of microMIPS relocations.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
@@ -16,8 +17,6 @@
 # RUN: llvm-objdump -d -t -mattr=micromips %tel.exe \
 # RUN:   | FileCheck --check-prefixes=EL,SYM %s
 
-# REQUIRES: mips
-
 # EB:      __start:
 # EB-NEXT:      20010:       41 a3 00 01     lui     $3, 1
 # EB-NEXT:      20014:       30 63 7f df     addiu   $3, $3, 32735
diff --git a/test/ELF/mips-micro-thunks.s b/test/ELF/mips-micro-thunks.s
index c8695cc..0505361 100644
--- a/test/ELF/mips-micro-thunks.s
+++ b/test/ELF/mips-micro-thunks.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check microMIPS thunk generation.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
@@ -36,8 +37,6 @@
 # RUN: llvm-objdump -d -mattr=+micromips %t-el-r6.exe \
 # RUN:   | FileCheck --check-prefix=EL-R6 %s
 
-# REQUIRES: mips
-
 # EB-R2: __start:
 # EB-R2-NEXT:    20000:       f4 01 00 04  jal   131080 <__microLA25Thunk_foo>
 # EB-R2-NEXT:    20004:       00 00 00 00  nop
diff --git a/test/ELF/mips-micro64-relocs.s b/test/ELF/mips-micro64-relocs.s
deleted file mode 100644
index b440c7a..0000000
--- a/test/ELF/mips-micro64-relocs.s
+++ /dev/null
@@ -1,22 +0,0 @@
-# REQUIRES: mips
-
-# Check handling of some microMIPS relocations in 64-bit mode.
-
-# RUN: llvm-mc -filetype=obj -triple=mips64el-unknown-linux \
-# RUN:         -mattr=micromips %s -o %t1.o
-# RUN: llvm-mc -filetype=obj -triple=mips64el-unknown-linux \
-# RUN:         -mattr=micromips %S/Inputs/mips-dynamic.s -o %t2.o
-# RUN: ld.lld %t1.o %t2.o -o %t.exe
-# RUN: llvm-objdump -d %t.exe | FileCheck %s
-
-  .global  __start
-__start:
-  lui     $7,  %highest(_foo+0x300047FFF7FF8)
-  lui     $7,  %higher (_foo+0x300047FFF7FF8)
-  lui     $gp, %hi(%neg(%gp_rel(__start)))
-  lui     $gp, %lo(%neg(%gp_rel(__start)))
-
-# CHECK:      20000:  a7 41 03 00  lui $7, 3
-# CHECK-NEXT: 20004:  a7 41 05 00  lui $7, 5
-# CHECK-NEXT: 20008:  bc 41 02 00  lui $gp, 2
-# CHECK-NEXT: 2000c:  bc 41 00 80  lui $gp, 32768
diff --git a/test/ELF/mips-n32-emul.s b/test/ELF/mips-n32-emul.s
index d0d81cc..3385d51 100644
--- a/test/ELF/mips-n32-emul.s
+++ b/test/ELF/mips-n32-emul.s
@@ -1,10 +1,9 @@
+# REQUIRES: mips
 # Check that LLD shows an error when N32 ABI emulation argument
 # is combined with non-N32 ABI object files.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
-# RUN: not ld.lld -m elf32btsmipn32 %t.o -o %t.exe 2>&1 | FileCheck %s
-
-# REQUIRES: mips
+# RUN: not ld.lld -m elf32btsmipn32 %t.o -o /dev/null 2>&1 | FileCheck %s
 
   .text
   .global  __start
diff --git a/test/ELF/mips-n32-rels.s b/test/ELF/mips-n32-rels.s
index 954d4c3..b59000c 100644
--- a/test/ELF/mips-n32-rels.s
+++ b/test/ELF/mips-n32-rels.s
@@ -1,29 +1,27 @@
+# REQUIRES: mips
 # Check handling of N32 ABI relocation records.
 
-# For now llvm-mc generates incorrect object files for N32 ABI.
-# We use the binary input file generated by GNU tool.
-# llvm-mc -filetype=obj -triple=mips64-unknown-linux \
-#         -target-abi n32 %s -o %t.o
-# RUN: ld.lld %S/Inputs/mips-n32-rels.o -o %t.exe
+# RUN: llvm-mc -filetype=obj -triple=mips64-unknown-linux \
+# RUN:         -target-abi n32 -o %t.o %s
+# RUN: ld.lld %t.o -o %t.exe
 # RUN: llvm-objdump -t -d -s %t.exe | FileCheck %s
 # RUN: llvm-readobj -h %t.exe | FileCheck -check-prefix=ELF %s
 
-# REQUIRES: mips
+  .option pic2
+  .text
+  .type   __start, @function
+  .global  __start
+__start:
+  lui     $gp,%hi(%neg(%gp_rel(__start)))     # R_MIPS_GPREL16
+                                              # R_MIPS_SUB
+                                              # R_MIPS_HI16
+loc:
+  daddiu  $gp,$gp,%lo(%neg(%gp_rel(__start))) # R_MIPS_GPREL16
+                                              # R_MIPS_SUB
+                                              # R_MIPS_LO16
 
-#   .text
-#   .type   __start, @function
-#   .global  __start
-# __start:
-#   lui     $gp,%hi(%neg(%gp_rel(__start)))     # R_MIPS_GPREL16
-#                                               # R_MIPS_SUB
-#                                               # R_MIPS_HI16
-# loc:
-#   daddiu  $gp,$gp,%lo(%neg(%gp_rel(__start))) # R_MIPS_GPREL16
-#                                               # R_MIPS_SUB
-#                                               # R_MIPS_LO16
-#
-#   .section  .rodata,"a",@progbits
-#   .gpword(loc)                                # R_MIPS_32
+  .section  .rodata,"a",@progbits
+  .gpword(loc)                                # R_MIPS_GPREL32
 
 # CHECK:      Disassembly of section .text:
 # CHECK-NEXT: __start:
@@ -38,8 +36,8 @@
 #                                                       ^-- %lo(0x17ff0)
 
 # CHECK:      Contents of section .rodata:
-# CHECK-NEXT:  {{[0-9a-f]+}} 00020004
-#                            ^-- loc
+# CHECK-NEXT:  {{[0-9a-f]+}} fffe8014
+#                            ^-- loc - _gp
 
 # CHECK: 00020004      .text   00000000 loc
 # CHECK: 00037ff0      .got    00000000 .hidden _gp
@@ -67,5 +65,7 @@
 # ELF-NEXT:   SectionHeaderOffset:
 # ELF-NEXT:   Flags [
 # ELF-NEXT:     EF_MIPS_ABI2
-# ELF-NEXT:     EF_MIPS_ARCH_64R2
+# ELF-NEXT:     EF_MIPS_ARCH_64
+# ELF-NEXT:     EF_MIPS_CPIC
+# ELF-NEXT:     EF_MIPS_PIC
 # ELF-NEXT:   ]
diff --git a/test/ELF/mips-non-zero-gp0.s b/test/ELF/mips-non-zero-gp0.s
index babfde9..9e6b13c 100644
--- a/test/ELF/mips-non-zero-gp0.s
+++ b/test/ELF/mips-non-zero-gp0.s
@@ -8,8 +8,6 @@
 #
 # as -mips32 -o test.o \
 #   && ld.bfd -m elf32btsmip -r test.o -o mips-gp0-non-zero.o
-# as -mips64 -mmicromips -o test.o \
-#   && ld.bfd -m elf64btsmip -r test.o -o mips-micro-gp0-non-zero.o
 # as -mips64 -o test.o \
 #   && ld.bfd -m elf64btsmip -r test.o -o mips-n64-gp0-non-zero.o
 
@@ -44,11 +42,6 @@
 # RUN: llvm-readobj -r %S/Inputs/mips-n64-gp0-non-zero.o %t-64.r \
 # RUN:   | FileCheck --check-prefix=ADDEND64 %s
 
-# RUN: ld.lld -r -o %t-micro.r %S/Inputs/mips-micro-gp0-non-zero.o
-# RUN: llvm-readobj -mips-options %t-micro.r | FileCheck --check-prefix=GPVAL %s
-# RUN: llvm-readobj -r %S/Inputs/mips-micro-gp0-non-zero.o %t-micro.r \
-# RUN:   | FileCheck --check-prefix=ADDENDMM %s
-
 # GPVAL: GP: 0x0
 
 # ADDEND32:      Contents of section .rodata:
@@ -59,8 +52,3 @@
 # ADDEND64: .text 0xFFFFFFFFFFFF8011
 # ADDEND64: File: {{.*}}{{/|\\}}mips-non-zero-gp0.s.tmp-64.r
 # ADDEND64: .text 0x0
-
-# ADDENDMM: File: {{.*}}{{/|\\}}mips-micro-gp0-non-zero.o
-# ADDENDMM: .text 0xFFFFFFFFFFFF8012
-# ADDENDMM: File: {{.*}}{{/|\\}}mips-non-zero-gp0.s.tmp-micro.r
-# ADDENDMM: .text 0x1
diff --git a/test/ELF/mips-nonalloc.s b/test/ELF/mips-nonalloc.s
index 7b0aa94..38d4599 100644
--- a/test/ELF/mips-nonalloc.s
+++ b/test/ELF/mips-nonalloc.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check reading addends for relocations in non-allocatable sections.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t1.o
@@ -6,8 +7,6 @@
 # RUN: ld.lld %t1.o %t2.o -o %t.exe
 # RUN: llvm-objdump -s %t.exe | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:      Contents of section .debug_info:
 # CHECK-NEXT:  0000 ffffffff 00020000 00020000
 #                            ^--------^-- __start
diff --git a/test/ELF/mips-options.s b/test/ELF/mips-options.s
index 18f5af8..ad63417 100644
--- a/test/ELF/mips-options.s
+++ b/test/ELF/mips-options.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check MIPS .MIPS.options section generation.
 
 # RUN: llvm-mc -filetype=obj -triple=mips64-unknown-linux %s -o %t1.o
@@ -9,8 +10,6 @@
 # RUN: ld.lld %t1.o %t2.o --gc-sections --script %t.rel.script -shared -o %t.so
 # RUN: llvm-readobj -symbols -mips-options %t.so | FileCheck %s
 
-# REQUIRES: mips
-
   .text
   .globl  __start
 __start:
diff --git a/test/ELF/mips-out-of-bounds-call16-reloc.s b/test/ELF/mips-out-of-bounds-call16-reloc.s
index 64e9ab3..cc2494a 100644
--- a/test/ELF/mips-out-of-bounds-call16-reloc.s
+++ b/test/ELF/mips-out-of-bounds-call16-reloc.s
@@ -1,8 +1,8 @@
+# REQUIRES: mips
 # Check that we create an error on an out-of-bounds R_MIPS_CALL_16
 
-# REQUIRES: mips
 # RUN: llvm-mc -filetype=obj -triple=mips64-unknown-linux %s -o %t1.o
-# RUN: not ld.lld %t1.o -o %t.exe 2>&1 | FileCheck %s
+# RUN: not ld.lld %t1.o -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: relocation R_MIPS_CALL16 out of range: 32768 is not in [-32768, 32767]
 
diff --git a/test/ELF/mips-pc-relocs.s b/test/ELF/mips-pc-relocs.s
index e0f39e7..46c2b75 100644
--- a/test/ELF/mips-pc-relocs.s
+++ b/test/ELF/mips-pc-relocs.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check R_MIPS_PCxxx relocations calculation.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
@@ -7,8 +8,6 @@
 # RUN: ld.lld %t1.o %t2.o -o %t.exe
 # RUN: llvm-objdump -mcpu=mips32r6 -d -t -s %t.exe | FileCheck %s
 
-# REQUIRES: mips
-
   .text
   .globl  __start
 __start:
diff --git a/test/ELF/mips-plt-copy.s b/test/ELF/mips-plt-copy.s
index 58883d8..e035c5b 100644
--- a/test/ELF/mips-plt-copy.s
+++ b/test/ELF/mips-plt-copy.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check creating of R_MIPS_COPY and R_MIPS_JUMP_SLOT dynamic relocations
 # and corresponding PLT entries.
 
@@ -8,16 +9,14 @@
 # RUN: ld.lld %t.o %t.so -o %t.exe
 # RUN: llvm-readobj -r -mips-plt-got %t.exe | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:      Relocations [
 # CHECK-NEXT:   Section ({{.*}}) .rel.dyn {
-# CHECK-NEXT:     0x{{[0-9A-F]+}} R_MIPS_COPY data0 0x0
-# CHECK-NEXT:     0x{{[0-9A-F]+}} R_MIPS_COPY data1 0x0
+# CHECK-DAG:      0x{{[0-9A-F]+}} R_MIPS_COPY data0 0x0
+# CHECK-DAG:      0x{{[0-9A-F]+}} R_MIPS_COPY data1 0x0
 # CHECK-NEXT:   }
 # CHECK-NEXT:   Section ({{.*}}) .rel.plt {
-# CHECK-NEXT:     0x{{[0-9A-F]+}} R_MIPS_JUMP_SLOT foo0 0x0
-# CHECK-NEXT:     0x{{[0-9A-F]+}} R_MIPS_JUMP_SLOT foo1 0x0
+# CHECK-DAG:      0x{{[0-9A-F]+}} R_MIPS_JUMP_SLOT foo0 0x0
+# CHECK-DAG:      0x{{[0-9A-F]+}} R_MIPS_JUMP_SLOT foo1 0x0
 # CHECK-NEXT:   }
 # CHECK-NEXT: ]
 
diff --git a/test/ELF/mips-plt-r6.s b/test/ELF/mips-plt-r6.s
index 34e6342..b0ceb82 100644
--- a/test/ELF/mips-plt-r6.s
+++ b/test/ELF/mips-plt-r6.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check PLT entries generation in case of R6 ABI version.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
@@ -11,8 +12,6 @@
 # RUN: ld.lld %t1.o %t.so -o %t.exe -z hazardplt
 # RUN: llvm-objdump -d %t.exe | FileCheck %s --check-prefixes=HAZARDPLT,CHECK
 
-# REQUIRES: mips
-
 # CHECK:      Disassembly of section .text:
 # CHECK-NEXT: __start:
 # CHECK-NEXT:   20000:       0c 00 80 0c     jal     131120
diff --git a/test/ELF/mips-reginfo.s b/test/ELF/mips-reginfo.s
index 4024a2f..049950d 100644
--- a/test/ELF/mips-reginfo.s
+++ b/test/ELF/mips-reginfo.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check MIPS .reginfo section generation.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t1.o
@@ -6,8 +7,6 @@
 # RUN: ld.lld %t1.o %t2.o --gc-sections -shared -o %t.so
 # RUN: llvm-readobj -symbols -mips-reginfo %t.so | FileCheck %s
 
-# REQUIRES: mips
-
   .text
   .globl  __start
 __start:
diff --git a/test/ELF/mips-relocatable.s b/test/ELF/mips-relocatable.s
index 168ddf7..0ab6277 100644
--- a/test/ELF/mips-relocatable.s
+++ b/test/ELF/mips-relocatable.s
@@ -1,11 +1,10 @@
+# REQUIRES: mips
 # Check linking MIPS code in case of -r linker's option.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux %s -o %t.o
 # RUN: ld.lld -r -o %t-r.o %t.o
 # RUN: llvm-objdump -s -t %t-r.o | FileCheck %s
 
-# REQUIRES: mips
-
   .text
   .global  __start
 __start:
diff --git a/test/ELF/mips-sto-pic-flag.s b/test/ELF/mips-sto-pic-flag.s
index 3960ba3..ae49697 100644
--- a/test/ELF/mips-sto-pic-flag.s
+++ b/test/ELF/mips-sto-pic-flag.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # In case of linking PIC and non-PIC code together and generation
 # of a relocatable object, all PIC symbols should have STO_MIPS_PIC
 # flag in the symbol table of the ouput file.
@@ -8,8 +9,6 @@
 # RUN: ld.lld -r %t-npic.o %t-pic.o -o %t-rel.o
 # RUN: llvm-readobj -t %t-rel.o | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:      Symbol {
 # CHECK:        Name: main
 # CHECK-NEXT:   Value:
diff --git a/test/ELF/mips-sto-plt.s b/test/ELF/mips-sto-plt.s
index bd8de41..b4d3ee3 100644
--- a/test/ELF/mips-sto-plt.s
+++ b/test/ELF/mips-sto-plt.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check assigning STO_MIPS_PLT flag to symbol needs a pointer equality.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
@@ -7,8 +8,6 @@
 # RUN: ld.lld %t.o %t.so -o %t.exe
 # RUN: llvm-readobj -dt -mips-plt-got %t.exe | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:      Symbol {
 # CHECK:        Name: foo0@
 # CHECK-NEXT:   Value: 0x0
@@ -18,9 +17,9 @@
 # CHECK-NEXT:   Other: 0
 # CHECK-NEXT:   Section: Undefined
 # CHECK-NEXT: }
-# CHECK:      Symbol {
-# CHECK:        Name: foo1@
-# CHECK-NEXT:   Value: 0x20050
+# CHECK-NEXT: Symbol {
+# CHECK-NEXT:   Name: foo1@
+# CHECK-NEXT:   Value: 0x[[FOO1:[0-9A-F]+]]
 # CHECK-NEXT:   Size: 0
 # CHECK-NEXT:   Binding: Global
 # CHECK-NEXT:   Type: Function
@@ -48,7 +47,7 @@
 # CHECK-NEXT:     Entry {
 # CHECK-NEXT:       Address:
 # CHECK-NEXT:       Initial:
-# CHECK-NEXT:       Value: 0x20050
+# CHECK-NEXT:       Value: 0x[[FOO1]]
 # CHECK-NEXT:       Type: Function
 # CHECK-NEXT:       Section: Undefined
 # CHECK-NEXT:       Name: foo1
diff --git a/test/ELF/mips-tls-64-pic-local-variable.s b/test/ELF/mips-tls-64-pic-local-variable.s
new file mode 100644
index 0000000..04d916f
--- /dev/null
+++ b/test/ELF/mips-tls-64-pic-local-variable.s
@@ -0,0 +1,49 @@
+# REQUIRES: mips
+# MIPS TLS variables that are marked as local by a version script were previously
+# writing values to the GOT that caused runtime crashes. This was happending when
+# linking jemalloc_tsd.c in FreeBSD libc. Check that we do the right thing now:
+
+# RUN: llvm-mc -filetype=obj -triple=mips64-unknown-freebsd %s -o %t.o
+# RUN: echo "{ global: foo; local: *; };" > %t.script
+# RUN: ld.lld --version-script %t.script -shared %t.o -o %t.so
+# RUN: llvm-objdump --section=.got -s %t.so | FileCheck %s -check-prefix GOT
+# RUN: llvm-readobj -r %t.so | FileCheck %s -check-prefix RELOCS
+
+# GOT:        Contents of section .got:
+# GOT-NEXT:   20000 00000000 00000000 80000000 00000000
+# GOT-NEXT:   20010 00000000 00000000 00000000 00000000
+# GOT-NEXT:   20020 ffffffff ffff8000
+
+# RELOCS:      Section ({{.+}}) .rel.dyn {
+# RELOCS-NEXT:  0x20018 R_MIPS_TLS_DTPMOD64/R_MIPS_NONE/R_MIPS_NONE
+# RELOCS-NEXT: }
+
+# Test case generated using clang -mcpu=mips4 -target mips64-unknown-freebsd12.0 -fpic -O -G0 -EB -mabi=n64 -msoft-float -std=gnu99 -S %s -o %t.s
+# from the following source:
+#
+# _Thread_local int x;
+# int foo() { return x; }
+#
+        .text
+        .globl  foo
+        .p2align        3
+        .type   foo,@function
+        .ent    foo
+foo:
+        lui     $1, %hi(%neg(%gp_rel(foo)))
+        daddu   $1, $1, $25
+        daddiu  $gp, $1, %lo(%neg(%gp_rel(foo)))
+        ld      $25, %call16(__tls_get_addr)($gp)
+        jalr    $25
+        daddiu  $4, $gp, %tlsgd(x)
+        .end    foo
+
+        .type   x,@object
+        .section        .tbss,"awT",@nobits
+        .globl  x
+        .p2align        2
+x:
+        .4byte  0
+        .size   x, 4
+
+
diff --git a/test/ELF/mips-tls-64.s b/test/ELF/mips-tls-64.s
index 67ff134..f000755 100644
--- a/test/ELF/mips-tls-64.s
+++ b/test/ELF/mips-tls-64.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check MIPS TLS 64-bit relocations handling.
 
 # RUN: llvm-mc -filetype=obj -triple=mips64-unknown-linux \
@@ -13,21 +14,19 @@
 # RUN: llvm-objdump -d -s -t %t-out.so | FileCheck -check-prefix=DIS-SO %s
 # RUN: llvm-readobj -r -mips-plt-got %t-out.so | FileCheck -check-prefix=SO %s
 
-# REQUIRES: mips
-
 # DIS:      __start:
-# DIS-NEXT:    20000:   24 62 80 20   addiu   $2, $3, -32736
-# DIS-NEXT:    20004:   24 62 80 30   addiu   $2, $3, -32720
-# DIS-NEXT:    20008:   24 62 80 38   addiu   $2, $3, -32712
-# DIS-NEXT:    2000c:   24 62 80 48   addiu   $2, $3, -32696
-# DIS-NEXT:    20010:   24 62 80 58   addiu   $2, $3, -32680
+# DIS-NEXT:    20000:   24 62 80 30   addiu   $2, $3, -32720
+# DIS-NEXT:    20004:   24 62 80 20   addiu   $2, $3, -32736
+# DIS-NEXT:    20008:   24 62 80 40   addiu   $2, $3, -32704
+# DIS-NEXT:    2000c:   24 62 80 50   addiu   $2, $3, -32688
+# DIS-NEXT:    20010:   24 62 80 28   addiu   $2, $3, -32728
 
 # DIS:      Contents of section .got:
 # DIS-NEXT:  30010 00000000 00000000 80000000 00000000
-# DIS-NEXT:  30020 00000000 00000000 00000000 00000000
-# DIS-NEXT:  30030 00000000 00000000 00000000 00000001
-# DIS-NEXT:  30040 00000000 00000000 00000000 00000001
-# DIS-NEXT:  30050 ffffffff ffff8004 ffffffff ffff9004
+# DIS-NEXT:  30020 00000000 00000000 ffffffff ffff9004
+# DIS-NEXT:  30030 00000000 00000000 00000000 00000000
+# DIS-NEXT:  30040 00000000 00000001 00000000 00000000
+# DIS-NEXT:  30050 00000000 00000001 ffffffff ffff8004
 
 # DIS: 0000000000000000 l       .tdata          00000000 loc
 # DIS: 0000000000000004 g       .tdata          00000000 bar
@@ -35,9 +34,9 @@
 
 # CHECK:      Relocations [
 # CHECK-NEXT:   Section (7) .rel.dyn {
-# CHECK-NEXT:     0x30020 R_MIPS_TLS_DTPMOD64/R_MIPS_NONE/R_MIPS_NONE foo 0x0
-# CHECK-NEXT:     0x30028 R_MIPS_TLS_DTPREL64/R_MIPS_NONE/R_MIPS_NONE foo 0x0
-# CHECK-NEXT:     0x30030 R_MIPS_TLS_TPREL64/R_MIPS_NONE/R_MIPS_NONE foo 0x0
+# CHECK-NEXT:     0x30020 R_MIPS_TLS_TPREL64/R_MIPS_NONE/R_MIPS_NONE foo 0x0
+# CHECK-NEXT:     0x30030 R_MIPS_TLS_DTPMOD64/R_MIPS_NONE/R_MIPS_NONE foo 0x0
+# CHECK-NEXT:     0x30038 R_MIPS_TLS_DTPREL64/R_MIPS_NONE/R_MIPS_NONE foo 0x0
 # CHECK-NEXT:   }
 # CHECK-NEXT: ]
 # CHECK-NEXT: Primary GOT {
@@ -49,31 +48,31 @@
 # CHECK-NEXT:   Global entries [
 # CHECK-NEXT:   ]
 # CHECK-NEXT:   Number of TLS and multi-GOT entries: 8
-#               ^-- -32736 R_MIPS_TLS_GD       R_MIPS_TLS_DTPMOD64 foo
-#               ^-- -32728                     R_MIPS_TLS_DTPREL64 foo
-#               ^-- -32720 R_MIPS_TLS_GOTTPREL R_MIPS_TLS_TPREL64  foo
-#               ^-- -32712 R_MIPS_TLS_LDM      1 loc
-#               ^-- -32704                     0 loc
-#               ^-- -32696 R_MIPS_TLS_GD       1 bar
-#               ^-- -32688                     VA - 0x8000 bar
-#               ^-- -32680 R_MIPS_TLS_GOTTPREL VA - 0x7000 bar
+#               ^-- -32736 R_MIPS_TLS_GOTTPREL R_MIPS_TLS_TPREL64  foo
+#               ^-- -32728 R_MIPS_TLS_GOTTPREL VA - 0x7000 bar
+#               ^-- -32720 R_MIPS_TLS_GD       R_MIPS_TLS_DTPMOD64 foo
+#               ^-- -32712                     R_MIPS_TLS_DTPREL64 foo
+#               ^-- -32704 R_MIPS_TLS_LDM      1 loc
+#               ^-- -32696                     0 loc
+#               ^-- -32688 R_MIPS_TLS_GD       1 bar
+#               ^-- -32680                     VA - 0x8000 bar
 
 # DIS-SO:      Contents of section .got:
 # DIS-SO-NEXT:  20000 00000000 00000000 80000000 00000000
-# DIS-SO-NEXT:  20010 00000000 00000000 00000000 00000000
+# DIS-SO-NEXT:  20010 00000000 00000000 00000000 00000004
 # DIS-SO-NEXT:  20020 00000000 00000000 00000000 00000000
 # DIS-SO-NEXT:  20030 00000000 00000000 00000000 00000000
 # DIS-SO-NEXT:  20040 00000000 00000000 00000000 00000000
 
 # SO:      Relocations [
 # SO-NEXT:   Section (7) .rel.dyn {
-# SO-NEXT:     0x20028 R_MIPS_TLS_DTPMOD64/R_MIPS_NONE/R_MIPS_NONE - 0x0
-# SO-NEXT:     0x20038 R_MIPS_TLS_DTPMOD64/R_MIPS_NONE/R_MIPS_NONE bar 0x0
-# SO-NEXT:     0x20040 R_MIPS_TLS_DTPREL64/R_MIPS_NONE/R_MIPS_NONE bar 0x0
-# SO-NEXT:     0x20048 R_MIPS_TLS_TPREL64/R_MIPS_NONE/R_MIPS_NONE bar 0x0
-# SO-NEXT:     0x20010 R_MIPS_TLS_DTPMOD64/R_MIPS_NONE/R_MIPS_NONE foo 0x0
-# SO-NEXT:     0x20018 R_MIPS_TLS_DTPREL64/R_MIPS_NONE/R_MIPS_NONE foo 0x0
-# SO-NEXT:     0x20020 R_MIPS_TLS_TPREL64/R_MIPS_NONE/R_MIPS_NONE foo 0x0
+# SO-NEXT:     0x20030 R_MIPS_TLS_DTPMOD64/R_MIPS_NONE/R_MIPS_NONE - 0x0
+# SO-NEXT:     0x20018 R_MIPS_TLS_TPREL64/R_MIPS_NONE/R_MIPS_NONE bar 0x0
+# SO-NEXT:     0x20040 R_MIPS_TLS_DTPMOD64/R_MIPS_NONE/R_MIPS_NONE bar 0x0
+# SO-NEXT:     0x20048 R_MIPS_TLS_DTPREL64/R_MIPS_NONE/R_MIPS_NONE bar 0x0
+# SO-NEXT:     0x20010 R_MIPS_TLS_TPREL64/R_MIPS_NONE/R_MIPS_NONE foo 0x0
+# SO-NEXT:     0x20020 R_MIPS_TLS_DTPMOD64/R_MIPS_NONE/R_MIPS_NONE foo 0x0
+# SO-NEXT:     0x20028 R_MIPS_TLS_DTPREL64/R_MIPS_NONE/R_MIPS_NONE foo 0x0
 # SO-NEXT:   }
 # SO-NEXT: ]
 # SO-NEXT: Primary GOT {
@@ -85,14 +84,14 @@
 # SO-NEXT:   Global entries [
 # SO-NEXT:   ]
 # SO-NEXT:   Number of TLS and multi-GOT entries: 8
-#            ^-- -32736 R_MIPS_TLS_GD       R_MIPS_TLS_DTPMOD64 foo
-#            ^-- -32728                     R_MIPS_TLS_DTPREL64 foo
-#            ^-- -32720 R_MIPS_TLS_GOTTPREL R_MIPS_TLS_TPREL64  foo
-#            ^-- -32712 R_MIPS_TLS_LDM      R_MIPS_TLS_DTPMOD64 loc
-#            ^-- -32704                     0 loc
-#            ^-- -32696 R_MIPS_TLS_GD       R_MIPS_TLS_DTPMOD64 bar
-#            ^-- -32688                     R_MIPS_TLS_DTPREL64 bar
-#            ^-- -32680 R_MIPS_TLS_GOTTPREL R_MIPS_TLS_TPREL64  bar
+#            ^-- -32736 R_MIPS_TLS_GOTTPREL R_MIPS_TLS_TPREL64  foo
+#            ^-- -32728 R_MIPS_TLS_GOTTPREL R_MIPS_TLS_TPREL64  bar
+#            ^-- -32720 R_MIPS_TLS_GD       R_MIPS_TLS_DTPMOD64 foo
+#            ^-- -32712                     R_MIPS_TLS_DTPREL64 foo
+#            ^-- -32704 R_MIPS_TLS_LDM      R_MIPS_TLS_DTPMOD64 loc
+#            ^-- -32696                     0 loc
+#            ^-- -32688 R_MIPS_TLS_GD       R_MIPS_TLS_DTPMOD64 bar
+#            ^-- -32680                     R_MIPS_TLS_DTPREL64 bar
 
   .text
   .global  __start
diff --git a/test/ELF/mips-tls-hilo.s b/test/ELF/mips-tls-hilo.s
index 47fadaa..ae54602 100644
--- a/test/ELF/mips-tls-hilo.s
+++ b/test/ELF/mips-tls-hilo.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check MIPS R_MIPS_TLS_DTPREL_HI16/LO16 and R_MIPS_TLS_TPREL_HI16/LO16
 # relocations handling.
 
@@ -9,8 +10,6 @@
 # RUN: ld.lld %t.o -shared -o %t.so
 # RUN: llvm-readobj -r -mips-plt-got %t.so | FileCheck -check-prefix=SO %s
 
-# REQUIRES: mips
-
 # DIS:      __start:
 # DIS-NEXT:    20000:   24 62 00 00   addiu   $2, $3, 0
 #                       %hi(loc0 - .tdata - 0x8000) --^
diff --git a/test/ELF/mips-tls-static-64.s b/test/ELF/mips-tls-static-64.s
index 6f88e86..04f18fa 100644
--- a/test/ELF/mips-tls-static-64.s
+++ b/test/ELF/mips-tls-static-64.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check handling TLS related relocations and symbols when linking
 # a 64-bit static executable.
 
@@ -5,8 +6,6 @@
 # RUN: ld.lld -static %t -o %t.exe
 # RUN: llvm-objdump -s -t %t.exe | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:      Contents of section .data:
 # CHECK-NEXT:  30000 00020004 ffffffff ffff8004 ffffffff
 # CHECK-NEXT:  30010 ffff9004
diff --git a/test/ELF/mips-tls-static.s b/test/ELF/mips-tls-static.s
index 84b56cb..b09f551 100644
--- a/test/ELF/mips-tls-static.s
+++ b/test/ELF/mips-tls-static.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check handling TLS related relocations and symbols when linking
 # a static executable.
 
@@ -5,13 +6,11 @@
 # RUN: ld.lld -static %t -o %t.exe
 # RUN: llvm-objdump -s -t %t.exe | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:      Contents of section .data:
 # CHECK-NEXT:  30000 0002000c ffff8004 ffff9004
 # CHECK:      Contents of section .got:
-# CHECK-NEXT:  30010 00000000 80000000 00000001 ffff8000
-# CHECK-NEXT:  30020 00000001 00000000 ffff9000
+# CHECK-NEXT:  30010 00000000 80000000 ffff9000 00000001
+# CHECK-NEXT:  30020 ffff8000 00000001 00000000
 #
 # CHECK: SYMBOL TABLE:
 # CHECK: 0002000c         .text           00000000 __tls_get_addr
diff --git a/test/ELF/mips-tls.s b/test/ELF/mips-tls.s
index b64f8db..ece55c6 100644
--- a/test/ELF/mips-tls.s
+++ b/test/ELF/mips-tls.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check MIPS TLS relocations handling.
 
 # RUN: llvm-mc -filetype=obj -triple=mips-unknown-linux \
@@ -13,19 +14,17 @@
 # RUN: llvm-objdump -d -s -t %t-out.so | FileCheck -check-prefix=DIS-SO %s
 # RUN: llvm-readobj -r -mips-plt-got %t-out.so | FileCheck -check-prefix=SO %s
 
-# REQUIRES: mips
-
 # DIS:      __start:
-# DIS-NEXT:    20000:   24 62 80 18   addiu   $2, $3, -32744
-# DIS-NEXT:    20004:   24 62 80 20   addiu   $2, $3, -32736
-# DIS-NEXT:    20008:   24 62 80 24   addiu   $2, $3, -32732
-# DIS-NEXT:    2000c:   24 62 80 2c   addiu   $2, $3, -32724
-# DIS-NEXT:    20010:   24 62 80 34   addiu   $2, $3, -32716
+# DIS-NEXT:    20000:   24 62 80 20   addiu   $2, $3, -32736
+# DIS-NEXT:    20004:   24 62 80 18   addiu   $2, $3, -32744
+# DIS-NEXT:    20008:   24 62 80 28   addiu   $2, $3, -32728
+# DIS-NEXT:    2000c:   24 62 80 30   addiu   $2, $3, -32720
+# DIS-NEXT:    20010:   24 62 80 1c   addiu   $2, $3, -32740
 
 # DIS:      Contents of section .got:
-# DIS-NEXT:  30010 00000000 80000000 00000000 00000000
-# DIS-NEXT:  30020 00000000 00000001 00000000 00000001
-# DIS-NEXT:  30030 ffff8004 ffff9004
+# DIS-NEXT:  30010 00000000 80000000 00000000 ffff9004
+# DIS-NEXT:  30020 00000000 00000000 00000001 00000000
+# DIS-NEXT:  30030 00000001 ffff8004
 
 # DIS: 00000000 l       .tdata          00000000 loc
 # DIS: 00000004 g       .tdata          00000000 bar
@@ -33,9 +32,9 @@
 
 # CHECK:      Relocations [
 # CHECK-NEXT:   Section (7) .rel.dyn {
-# CHECK-NEXT:     0x30018 R_MIPS_TLS_DTPMOD32 foo 0x0
-# CHECK-NEXT:     0x3001C R_MIPS_TLS_DTPREL32 foo 0x0
-# CHECK-NEXT:     0x30020 R_MIPS_TLS_TPREL32 foo 0x0
+# CHECK-NEXT:     0x30018 R_MIPS_TLS_TPREL32 foo 0x0
+# CHECK-NEXT:     0x30020 R_MIPS_TLS_DTPMOD32 foo 0x0
+# CHECK-NEXT:     0x30024 R_MIPS_TLS_DTPREL32 foo 0x0
 # CHECK-NEXT:   }
 # CHECK-NEXT: ]
 # CHECK-NEXT: Primary GOT {
@@ -47,29 +46,29 @@
 # CHECK-NEXT:   Global entries [
 # CHECK-NEXT:   ]
 # CHECK-NEXT:   Number of TLS and multi-GOT entries: 8
-#               ^-- -32744 R_MIPS_TLS_GD       R_MIPS_TLS_DTPMOD32 foo
-#               ^-- -32740                     R_MIPS_TLS_DTPREL32 foo
-#               ^-- -32736 R_MIPS_TLS_GOTTPREL R_MIPS_TLS_TPREL32  foo
-#               ^-- -32732 R_MIPS_TLS_LDM      1 loc
-#               ^-- -32728                     0 loc
-#               ^-- -32724 R_MIPS_TLS_GD       1 bar
-#               ^-- -32720                     VA - 0x8000 bar
-#               ^-- -32716 R_MIPS_TLS_GOTTPREL VA - 0x7000 bar
+#               ^-- -32744 R_MIPS_TLS_GOTTPREL R_MIPS_TLS_TPREL32  foo
+#               ^-- -32740 R_MIPS_TLS_GOTTPREL VA - 0x7000 bar
+#               ^-- -32736 R_MIPS_TLS_GD       R_MIPS_TLS_DTPMOD32 foo
+#               ^-- -32732                     R_MIPS_TLS_DTPREL32 foo
+#               ^-- -32728 R_MIPS_TLS_LDM      1 loc
+#               ^-- -32724                     0 loc
+#               ^-- -32720 R_MIPS_TLS_GD       1 bar
+#               ^-- -32716                     VA - 0x8000 bar
 
 # DIS-SO:      Contents of section .got:
-# DIS-SO-NEXT:  20000 00000000 80000000 00000000 00000000
+# DIS-SO-NEXT:  20000 00000000 80000000 00000000 00000004
 # DIS-SO-NEXT:  20010 00000000 00000000 00000000 00000000
 # DIS-SO-NEXT:  20020 00000000 00000000
 
 # SO:      Relocations [
 # SO-NEXT:   Section (7) .rel.dyn {
-# SO-NEXT:     0x20014 R_MIPS_TLS_DTPMOD32 - 0x0
-# SO-NEXT:     0x2001C R_MIPS_TLS_DTPMOD32 bar 0x0
-# SO-NEXT:     0x20020 R_MIPS_TLS_DTPREL32 bar 0x0
-# SO-NEXT:     0x20024 R_MIPS_TLS_TPREL32 bar 0x0
-# SO-NEXT:     0x20008 R_MIPS_TLS_DTPMOD32 foo 0x0
-# SO-NEXT:     0x2000C R_MIPS_TLS_DTPREL32 foo 0x0
-# SO-NEXT:     0x20010 R_MIPS_TLS_TPREL32 foo 0x0
+# SO-NEXT:     0x20018 R_MIPS_TLS_DTPMOD32 - 0x0
+# SO-NEXT:     0x2000C R_MIPS_TLS_TPREL32 bar 0x0
+# SO-NEXT:     0x20020 R_MIPS_TLS_DTPMOD32 bar 0x0
+# SO-NEXT:     0x20024 R_MIPS_TLS_DTPREL32 bar 0x0
+# SO-NEXT:     0x20008 R_MIPS_TLS_TPREL32 foo 0x0
+# SO-NEXT:     0x20010 R_MIPS_TLS_DTPMOD32 foo 0x0
+# SO-NEXT:     0x20014 R_MIPS_TLS_DTPREL32 foo 0x0
 # SO-NEXT:   }
 # SO-NEXT: ]
 # SO-NEXT: Primary GOT {
@@ -81,14 +80,14 @@
 # SO-NEXT:   Global entries [
 # SO-NEXT:   ]
 # SO-NEXT:   Number of TLS and multi-GOT entries: 8
-#            ^-- -32744 R_MIPS_TLS_GD       R_MIPS_TLS_DTPMOD32 foo
-#            ^-- -32740 R_MIPS_TLS_DTPREL32 foo
-#            ^-- -32736 R_MIPS_TLS_GOTTPREL R_MIPS_TLS_TPREL32  foo
-#            ^-- -32732 R_MIPS_TLS_LDM      R_MIPS_TLS_DTPMOD32 loc
-#            ^-- -32728 0 loc
-#            ^-- -32724 R_MIPS_TLS_GD       R_MIPS_TLS_DTPMOD32 bar
-#            ^-- -32720 R_MIPS_TLS_DTPREL32 bar
-#            ^-- -32716 R_MIPS_TLS_GOTTPREL R_MIPS_TLS_TPREL32  bar
+#            ^-- -32744 R_MIPS_TLS_GOTTPREL R_MIPS_TLS_TPREL32  foo
+#            ^-- -32740 R_MIPS_TLS_GOTTPREL R_MIPS_TLS_TPREL32  bar
+#            ^-- -32736 R_MIPS_TLS_GD       R_MIPS_TLS_DTPMOD32 foo
+#            ^-- -32732 R_MIPS_TLS_DTPREL32 foo
+#            ^-- -32728 R_MIPS_TLS_LDM      R_MIPS_TLS_DTPMOD32 loc
+#            ^-- -32724 0 loc
+#            ^-- -32720 R_MIPS_TLS_GD       R_MIPS_TLS_DTPMOD32 bar
+#            ^-- -32716 R_MIPS_TLS_DTPREL32 bar
 
   .text
   .global  __start
diff --git a/test/ELF/mips-xgot-order.s b/test/ELF/mips-xgot-order.s
index 911731c..c44cf64 100644
--- a/test/ELF/mips-xgot-order.s
+++ b/test/ELF/mips-xgot-order.s
@@ -1,3 +1,4 @@
+# REQUIRES: mips
 # Check that GOT entries accessed via 16-bit indexing are allocated
 # in the beginning of the GOT.
 
@@ -5,8 +6,6 @@
 # RUN: ld.lld %t.o -o %t.exe
 # RUN: llvm-objdump -d -s -t %t.exe | FileCheck %s
 
-# REQUIRES: mips
-
 # CHECK:      Disassembly of section .text:
 # CHECK-NEXT: __start:
 # CHECK-NEXT:    20000:       3c 02 00 00     lui     $2, 0
diff --git a/test/ELF/mips64-eh-abs-reloc.s b/test/ELF/mips64-eh-abs-reloc.s
index aa21a37..7c31e1b 100644
--- a/test/ELF/mips64-eh-abs-reloc.s
+++ b/test/ELF/mips64-eh-abs-reloc.s
@@ -1,5 +1,5 @@
-# Having an R_MIPS_64 relocation in eh_frame would previously crash LLD
 # REQUIRES: mips
+# Having an R_MIPS_64 relocation in eh_frame would previously crash LLD
 # RUN: llvm-mc -filetype=obj -triple=mips64-unknown-freebsd %s -o %t.o
 # RUN: llvm-readobj -r %t.o | FileCheck %s -check-prefix OBJ
 # RUN: ld.lld --eh-frame-hdr -shared -z notext -o %t.so %t.o
diff --git a/test/ELF/multiple-cu.s b/test/ELF/multiple-cu.s
index df2b94d..996a7bc 100644
--- a/test/ELF/multiple-cu.s
+++ b/test/ELF/multiple-cu.s
@@ -2,7 +2,7 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t1.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/multiple-cu.s -o %t2.o
 # RUN: ld.lld -r -o %t.o %t1.o %t2.o
-# RUN: not ld.lld %t.o -o %t 2>&1 | FileCheck %s
+# RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK:      error: undefined symbol: foo
 # CHECK-NEXT: referenced by test1.c:2
diff --git a/test/ELF/no-augmentation.s b/test/ELF/no-augmentation.s
index 31cd92e..f4912ba 100644
--- a/test/ELF/no-augmentation.s
+++ b/test/ELF/no-augmentation.s
@@ -1,7 +1,6 @@
-// RUN: llvm-mc -filetype=obj -triple=mips64-unknown-freebsd %s -o %t.o
-// RUN: ld.lld --eh-frame-hdr %t.o -o %t | FileCheck -allow-empty %s
-
 // REQUIRES: mips
+// RUN: llvm-mc -filetype=obj -triple=mips64-unknown-freebsd %s -o %t.o
+// RUN: ld.lld --eh-frame-hdr %t.o -o /dev/null | FileCheck -allow-empty %s
 
 // CHECK-NOT: corrupted or unsupported CIE information
 // CHECK-NOT: corrupted CIE
diff --git a/test/ELF/no-inhibit-exec.s b/test/ELF/no-inhibit-exec.s
index afb7aed..1535f6e 100644
--- a/test/ELF/no-inhibit-exec.s
+++ b/test/ELF/no-inhibit-exec.s
@@ -1,9 +1,9 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 # RUN: not ld.lld %t -o %t2
 # RUN: ld.lld %t --noinhibit-exec -o %t2
 # RUN: llvm-objdump -d %t2 | FileCheck %s
 # RUN: llvm-readobj -r %t2 | FileCheck %s --check-prefix=RELOC
-# REQUIRES: x86
 
 # CHECK: Disassembly of section .text:
 # CHECK-NEXT: _start
diff --git a/test/ELF/no-line-parser-errors-if-empty-section.s b/test/ELF/no-line-parser-errors-if-empty-section.s
index 56b255e..808052e 100644
--- a/test/ELF/no-line-parser-errors-if-empty-section.s
+++ b/test/ELF/no-line-parser-errors-if-empty-section.s
@@ -5,7 +5,7 @@
 # it, as that would result in errors from the parser.
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-# RUN: not ld.lld %t.o -o %t.elf 2>&1 | FileCheck %s
+# RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK-NOT: warning:
 # CHECK-NOT: error:
diff --git a/test/ELF/no-line-parser-errors-if-no-section.s b/test/ELF/no-line-parser-errors-if-no-section.s
index 9210d85..4624601 100644
--- a/test/ELF/no-line-parser-errors-if-no-section.s
+++ b/test/ELF/no-line-parser-errors-if-no-section.s
@@ -5,7 +5,7 @@
 # it, as that would result in errors from the parser.
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-# RUN: not ld.lld %t.o -o %t.elf 2>&1 | FileCheck %s
+# RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK-NOT: warning:
 # CHECK-NOT: error:
diff --git a/test/ELF/no-obj.s b/test/ELF/no-obj.s
index 693cdf1..4e8bcee 100644
--- a/test/ELF/no-obj.s
+++ b/test/ELF/no-obj.s
@@ -1,7 +1,8 @@
 // REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+// RUN: rm -f %t.a
 // RUN: llvm-ar rcs %t.a %t.o
-// RUN: not ld.lld -o %t2 -u _start %t.a 2>&1 | FileCheck %s
+// RUN: not ld.lld -o /dev/null -u _start %t.a 2>&1 | FileCheck %s
 
 // CHECK: target emulation unknown: -m or at least one .o file required
 
diff --git a/test/ELF/no-symtab.s b/test/ELF/no-symtab.s
index af9df13..2468c4f 100644
--- a/test/ELF/no-symtab.s
+++ b/test/ELF/no-symtab.s
@@ -1,5 +1,5 @@
 // REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-// RUN: ld.lld %t.o %p/Inputs/no-symtab.o -o %t
+// RUN: ld.lld %t.o %p/Inputs/no-symtab.o -o /dev/null
 .global _start
 _start:
diff --git a/test/ELF/no-undefined.s b/test/ELF/no-undefined.s
index 493a389..854114b 100644
--- a/test/ELF/no-undefined.s
+++ b/test/ELF/no-undefined.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
-# RUN: not ld.lld --no-undefined -shared %t -o %t.so
-# RUN: ld.lld -shared %t -o %t1.so
+# RUN: not ld.lld --no-undefined -shared %t -o /dev/null
+# RUN: ld.lld -shared %t -o /dev/null
 
 .globl _shared
 _shared:
diff --git a/test/ELF/note-noalloc.s b/test/ELF/note-noalloc.s
index ddbde3e..80c7a4d 100644
--- a/test/ELF/note-noalloc.s
+++ b/test/ELF/note-noalloc.s
@@ -25,7 +25,7 @@
 // CHECK:        Type: PT_NOTE
 // CHECK-NEXT:   Offset:
 // CHECK-NEXT:   VirtualAddress: 0x[[ADDR]]
-// CHECK-NEXT:   PhysicalAddress: 0x24C
+// CHECK-NEXT:   PhysicalAddress: 0x[[ADDR]]
 // CHECK-NEXT:   FileSize: 16
 // CHECK-NEXT:   MemSize: 16
 // CHECK-NOT:  PT_NOTE
diff --git a/test/ELF/oformat-binary.s b/test/ELF/oformat-binary.s
index acd95c7..22a25f0 100644
--- a/test/ELF/oformat-binary.s
+++ b/test/ELF/oformat-binary.s
@@ -15,10 +15,13 @@
 # RUN: ld.lld -o %t2.out --script %t.script %t --oformat binary
 # RUN: od -t x1 -v %t2.out | FileCheck %s
 
-# RUN: not ld.lld -o %t3.out %t --oformat foo 2>&1 \
+# RUN: not ld.lld -o /dev/null %t --oformat foo 2>&1 \
 # RUN:   | FileCheck %s --check-prefix ERR
 # ERR: unknown --oformat value: foo
 
+# RUN: ld.lld -o /dev/null %t --oformat elf
+# RUN: ld.lld -o /dev/null %t --oformat elf-foo
+
 .text
 .align 4
 .globl _start
diff --git a/test/ELF/output-section.s b/test/ELF/output-section.s
index 6850525..2a119d9 100644
--- a/test/ELF/output-section.s
+++ b/test/ELF/output-section.s
@@ -1,7 +1,7 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 // RUN: ld.lld %t -o %t2
 // RUN: llvm-readobj -t %t2 | FileCheck %s
-// REQUIRES: x86
 
 // CHECK:      Symbol {
 // CHECK:        Name: bar_sym
diff --git a/test/ELF/pack-dyn-relocs.s b/test/ELF/pack-dyn-relocs.s
index e79cac6..b37729d 100644
--- a/test/ELF/pack-dyn-relocs.s
+++ b/test/ELF/pack-dyn-relocs.s
@@ -5,12 +5,10 @@
 // RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t.a32
 // RUN: ld.lld -pie --pack-dyn-relocs=none %t.a32 %t.a32.so -o %t2.a32
 // RUN: llvm-readobj -relocations %t2.a32 | FileCheck --check-prefix=UNPACKED32 %s
-// RUN: ld.lld -pie --pack-dyn-relocs android %t.a32 %t.a32.so -o %t3.a32
-// RUN: llvm-readobj -s -dynamic-table %t3.a32 | FileCheck --check-prefix=PACKED32-HEADERS %s
-// RUN: llvm-readobj -relocations %t3.a32 | FileCheck --check-prefix=PACKED32 %s
 
 // Unpacked should have the relative relocations in their natural order.
-// UNPACKED32:          0x1000 R_ARM_RELATIVE - 0x0
+// UNPACKED32:          Section ({{.+}}) .rel.dyn {
+// UNPACKED32-NEXT:     0x1000 R_ARM_RELATIVE - 0x0
 // UNPACKED32-NEXT:     0x1004 R_ARM_RELATIVE - 0x0
 // UNPACKED32-NEXT:     0x1008 R_ARM_RELATIVE - 0x0
 // UNPACKED32-NEXT:     0x100C R_ARM_RELATIVE - 0x0
@@ -37,70 +35,146 @@
 // UNPACKED32-NEXT:     0x1060 R_ARM_RELATIVE - 0x0
 // UNPACKED32-NEXT:     0x1064 R_ARM_RELATIVE - 0x0
 
+// UNPACKED32-NEXT:     0x1069 R_ARM_RELATIVE - 0x0
 // UNPACKED32-NEXT:     0x1020 R_ARM_ABS32 bar2 0x0
 // UNPACKED32-NEXT:     0x1040 R_ARM_ABS32 zed2 0x0
+// UNPACKED32-NEXT:     }
 
-// PACKED32-HEADERS:       Index: 1
-// PACKED32-HEADERS-NEXT:  Name: .dynsym
+// RUN: ld.lld -pie --pack-dyn-relocs=android %t.a32 %t.a32.so -o %t3.a32
+// RUN: llvm-readobj -s -dynamic-table %t3.a32 | FileCheck --check-prefix=ANDROID32-HEADERS %s
+// RUN: llvm-readobj -relocations %t3.a32 | FileCheck --check-prefix=ANDROID32 %s
 
-// PACKED32-HEADERS:       Name: .rel.dyn
-// PACKED32-HEADERS-NEXT:  Type: SHT_ANDROID_REL
-// PACKED32-HEADERS-NEXT:  Flags [ (0x2)
-// PACKED32-HEADERS-NEXT:    SHF_ALLOC (0x2)
-// PACKED32-HEADERS-NEXT:  ]
-// PACKED32-HEADERS-NEXT:  Address: [[ADDR:.*]]
-// PACKED32-HEADERS-NEXT:  Offset: [[ADDR]]
-// PACKED32-HEADERS-NEXT:  Size: [[SIZE:.*]]
-// PACKED32-HEADERS-NEXT:  Link: 1
-// PACKED32-HEADERS-NEXT:  Info: 0
-// PACKED32-HEADERS-NEXT:  AddressAlignment: 4
-// PACKED32-HEADERS-NEXT:  EntrySize: 1
+// ANDROID32-HEADERS:       Index: 1
+// ANDROID32-HEADERS-NEXT:  Name: .dynsym
 
-// PACKED32-HEADERS: 0x6000000F ANDROID_REL          [[ADDR]]
-// PACKED32-HEADERS: 0x60000010 ANDROID_RELSZ        [[SIZE]]
+// ANDROID32-HEADERS:       Name: .rel.dyn
+// ANDROID32-HEADERS-NEXT:  Type: SHT_ANDROID_REL
+// ANDROID32-HEADERS-NEXT:  Flags [ (0x2)
+// ANDROID32-HEADERS-NEXT:    SHF_ALLOC (0x2)
+// ANDROID32-HEADERS-NEXT:  ]
+// ANDROID32-HEADERS-NEXT:  Address: [[ADDR:.*]]
+// ANDROID32-HEADERS-NEXT:  Offset: [[ADDR]]
+// ANDROID32-HEADERS-NEXT:  Size: [[SIZE:.*]]
+// ANDROID32-HEADERS-NEXT:  Link: 1
+// ANDROID32-HEADERS-NEXT:  Info: 0
+// ANDROID32-HEADERS-NEXT:  AddressAlignment: 4
+// ANDROID32-HEADERS-NEXT:  EntrySize: 1
+
+// ANDROID32-HEADERS: 0x6000000F ANDROID_REL          [[ADDR]]
+// ANDROID32-HEADERS: 0x60000010 ANDROID_RELSZ        [[SIZE]]
 
 // Packed should have the larger groups of relative relocations first,
 // i.e. the 8 and 9 followed by the 7.
-// PACKED32:          0x1000 R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x1004 R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x1008 R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x100C R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x1010 R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x1014 R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x1018 R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x101C R_ARM_RELATIVE - 0x0
+// ANDROID32:          Section ({{.+}}) .rel.dyn {
+// ANDROID32-NEXT:     0x1000 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1004 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1008 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x100C R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1010 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1014 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1018 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x101C R_ARM_RELATIVE - 0x0
 
-// PACKED32-NEXT:     0x1044 R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x1048 R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x104C R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x1050 R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x1054 R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x1058 R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x105C R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x1060 R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x1064 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1044 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1048 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x104C R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1050 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1054 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1058 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x105C R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1060 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1064 R_ARM_RELATIVE - 0x0
 
-// PACKED32-NEXT:     0x1024 R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x1028 R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x102C R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x1030 R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x1034 R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x1038 R_ARM_RELATIVE - 0x0
-// PACKED32-NEXT:     0x103C R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1024 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1028 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x102C R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1030 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1034 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1038 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x103C R_ARM_RELATIVE - 0x0
 
-// PACKED32-NEXT:     0x1020 R_ARM_ABS32 bar2 0x0
-// PACKED32-NEXT:     0x1040 R_ARM_ABS32 zed2 0x0
+// ANDROID32-NEXT:     0x1069 R_ARM_RELATIVE - 0x0
+// ANDROID32-NEXT:     0x1020 R_ARM_ABS32 bar2 0x0
+// ANDROID32-NEXT:     0x1040 R_ARM_ABS32 zed2 0x0
+// ANDROID32-NEXT:     }
+
+// RUN: ld.lld -pie --pack-dyn-relocs=relr %t.a32 %t.a32.so -o %t4.a32
+// RUN: llvm-readobj -s -dynamic-table %t4.a32 | FileCheck --check-prefix=RELR32-HEADERS %s
+// RUN: llvm-readobj -relocations -raw-relr %t4.a32 | FileCheck --check-prefix=RAW-RELR32 %s
+// RUN: llvm-readobj -relocations %t4.a32 | FileCheck --check-prefix=RELR32 %s
+
+// RELR32-HEADERS:       Index: 1
+// RELR32-HEADERS-NEXT:  Name: .dynsym
+
+// RELR32-HEADERS:       Name: .relr.dyn
+// RELR32-HEADERS-NEXT:  Type: SHT_RELR
+// RELR32-HEADERS-NEXT:  Flags [ (0x2)
+// RELR32-HEADERS-NEXT:    SHF_ALLOC (0x2)
+// RELR32-HEADERS-NEXT:  ]
+// RELR32-HEADERS-NEXT:  Address: [[ADDR:.*]]
+// RELR32-HEADERS-NEXT:  Offset: [[ADDR]]
+// RELR32-HEADERS-NEXT:  Size: 8
+// RELR32-HEADERS-NEXT:  Link: 0
+// RELR32-HEADERS-NEXT:  Info: 0
+// RELR32-HEADERS-NEXT:  AddressAlignment: 4
+// RELR32-HEADERS-NEXT:  EntrySize: 4
+
+// RELR32-HEADERS:       0x00000024 RELR                 [[ADDR]]
+// RELR32-HEADERS:       0x00000023 RELRSZ               0x8
+// RELR32-HEADERS:       0x00000025 RELRENT              0x4
+
+// SHT_RELR section contains address/bitmap entries
+// encoding the offsets for relative relocation.
+// RAW-RELR32:           Section ({{.+}}) .relr.dyn {
+// RAW-RELR32-NEXT:      0x1000
+// RAW-RELR32-NEXT:      0x3FEFEFF
+// RAW-RELR32-NEXT:      }
+
+// Decoded SHT_RELR section is same as UNPACKED,
+// but contains only the relative relocations.
+// Any relative relocations with odd offset stay in SHT_REL.
+// RELR32:               Section ({{.+}}) .rel.dyn {
+// RELR32-NEXT:          0x1069 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x1020 R_ARM_ABS32 bar2 0x0
+// RELR32-NEXT:          0x1040 R_ARM_ABS32 zed2 0x0
+// RELR32-NEXT:          }
+// RELR32-NEXT:          Section ({{.+}}) .relr.dyn {
+// RELR32-NEXT:          0x1000 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x1004 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x1008 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x100C R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x1010 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x1014 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x1018 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x101C R_ARM_RELATIVE - 0x0
+
+// RELR32-NEXT:          0x1024 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x1028 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x102C R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x1030 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x1034 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x1038 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x103C R_ARM_RELATIVE - 0x0
+
+// RELR32-NEXT:          0x1044 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x1048 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x104C R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x1050 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x1054 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x1058 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x105C R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x1060 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          0x1064 R_ARM_RELATIVE - 0x0
+// RELR32-NEXT:          }
 
 // RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-linux %p/Inputs/shared2.s -o %t.a64.so.o
 // RUN: ld.lld -shared %t.a64.so.o -o %t.a64.so
 // RUN: llvm-mc -filetype=obj -triple=aarch64-unknown-linux %s -o %t.a64
 // RUN: ld.lld -pie --pack-dyn-relocs=none %t.a64 %t.a64.so -o %t2.a64
 // RUN: llvm-readobj -relocations %t2.a64 | FileCheck --check-prefix=UNPACKED64 %s
-// RUN: ld.lld -pie --pack-dyn-relocs=android %t.a64 %t.a64.so -o %t3.a64
-// RUN: llvm-readobj -s -dynamic-table %t3.a64 | FileCheck --check-prefix=PACKED64-HEADERS %s
-// RUN: llvm-readobj -relocations %t3.a64 | FileCheck --check-prefix=PACKED64 %s
 
-// UNPACKED64:          0x10000 R_AARCH64_RELATIVE - 0x1
+// UNPACKED64:          Section ({{.+}}) .rela.dyn {
+// UNPACKED64-NEXT:     0x10000 R_AARCH64_RELATIVE - 0x1
 // UNPACKED64-NEXT:     0x10008 R_AARCH64_RELATIVE - 0x2
 // UNPACKED64-NEXT:     0x10010 R_AARCH64_RELATIVE - 0x3
 // UNPACKED64-NEXT:     0x10018 R_AARCH64_RELATIVE - 0x4
@@ -127,59 +201,138 @@
 // UNPACKED64-NEXT:     0x100C0 R_AARCH64_RELATIVE - 0x8
 // UNPACKED64-NEXT:     0x100C8 R_AARCH64_RELATIVE - 0x9
 
+// UNPACKED64-NEXT:     0x100D1 R_AARCH64_RELATIVE - 0xA
 // UNPACKED64-NEXT:     0x10040 R_AARCH64_ABS64 bar2 0x1
 // UNPACKED64-NEXT:     0x10080 R_AARCH64_ABS64 zed2 0x0
+// UNPACKED64-NEXT:     }
 
-// PACKED64:          0x10000 R_AARCH64_RELATIVE - 0x1
-// PACKED64-NEXT:     0x10008 R_AARCH64_RELATIVE - 0x2
-// PACKED64-NEXT:     0x10010 R_AARCH64_RELATIVE - 0x3
-// PACKED64-NEXT:     0x10018 R_AARCH64_RELATIVE - 0x4
-// PACKED64-NEXT:     0x10020 R_AARCH64_RELATIVE - 0x5
-// PACKED64-NEXT:     0x10028 R_AARCH64_RELATIVE - 0x6
-// PACKED64-NEXT:     0x10030 R_AARCH64_RELATIVE - 0x7
-// PACKED64-NEXT:     0x10038 R_AARCH64_RELATIVE - 0x8
+// RUN: ld.lld -pie --pack-dyn-relocs=android %t.a64 %t.a64.so -o %t3.a64
+// RUN: llvm-readobj -s -dynamic-table %t3.a64 | FileCheck --check-prefix=ANDROID64-HEADERS %s
+// RUN: llvm-readobj -relocations %t3.a64 | FileCheck --check-prefix=ANDROID64 %s
 
-// PACKED64-NEXT:     0x10088 R_AARCH64_RELATIVE - 0x1
-// PACKED64-NEXT:     0x10090 R_AARCH64_RELATIVE - 0x2
-// PACKED64-NEXT:     0x10098 R_AARCH64_RELATIVE - 0x3
-// PACKED64-NEXT:     0x100A0 R_AARCH64_RELATIVE - 0x4
-// PACKED64-NEXT:     0x100A8 R_AARCH64_RELATIVE - 0x5
-// PACKED64-NEXT:     0x100B0 R_AARCH64_RELATIVE - 0x6
-// PACKED64-NEXT:     0x100B8 R_AARCH64_RELATIVE - 0x7
-// PACKED64-NEXT:     0x100C0 R_AARCH64_RELATIVE - 0x8
-// PACKED64-NEXT:     0x100C8 R_AARCH64_RELATIVE - 0x9
+// ANDROID64-HEADERS:       Index: 1
+// ANDROID64-HEADERS-NEXT:  Name: .dynsym
 
-// PACKED64-NEXT:     0x10048 R_AARCH64_RELATIVE - 0x1
-// PACKED64-NEXT:     0x10050 R_AARCH64_RELATIVE - 0x2
-// PACKED64-NEXT:     0x10058 R_AARCH64_RELATIVE - 0x3
-// PACKED64-NEXT:     0x10060 R_AARCH64_RELATIVE - 0x4
-// PACKED64-NEXT:     0x10068 R_AARCH64_RELATIVE - 0x5
-// PACKED64-NEXT:     0x10070 R_AARCH64_RELATIVE - 0x6
-// PACKED64-NEXT:     0x10078 R_AARCH64_RELATIVE - 0x7
+// ANDROID64-HEADERS:       Name: .rela.dyn
+// ANDROID64-HEADERS-NEXT:  Type: SHT_ANDROID_RELA
+// ANDROID64-HEADERS-NEXT:  Flags [ (0x2)
+// ANDROID64-HEADERS-NEXT:    SHF_ALLOC (0x2)
+// ANDROID64-HEADERS-NEXT:  ]
+// ANDROID64-HEADERS-NEXT:  Address: [[ADDR:.*]]
+// ANDROID64-HEADERS-NEXT:  Offset: [[ADDR]]
+// ANDROID64-HEADERS-NEXT:  Size: [[SIZE:.*]]
+// ANDROID64-HEADERS-NEXT:  Link: 1
+// ANDROID64-HEADERS-NEXT:  Info: 0
+// ANDROID64-HEADERS-NEXT:  AddressAlignment: 8
+// ANDROID64-HEADERS-NEXT:  EntrySize: 1
 
-// PACKED64-NEXT:     0x10040 R_AARCH64_ABS64 bar2 0x1
-// PACKED64-NEXT:     0x10080 R_AARCH64_ABS64 zed2 0x0
+// ANDROID64-HEADERS: 0x0000000060000011 ANDROID_RELA          [[ADDR]]
+// ANDROID64-HEADERS: 0x0000000060000012 ANDROID_RELASZ        [[SIZE]]
 
-// PACKED64-HEADERS:       Index: 1
-// PACKED64-HEADERS-NEXT:  Name: .dynsym
+// ANDROID64:          Section ({{.+}}) .rela.dyn {
+// ANDROID64-NEXT:     0x10000 R_AARCH64_RELATIVE - 0x1
+// ANDROID64-NEXT:     0x10008 R_AARCH64_RELATIVE - 0x2
+// ANDROID64-NEXT:     0x10010 R_AARCH64_RELATIVE - 0x3
+// ANDROID64-NEXT:     0x10018 R_AARCH64_RELATIVE - 0x4
+// ANDROID64-NEXT:     0x10020 R_AARCH64_RELATIVE - 0x5
+// ANDROID64-NEXT:     0x10028 R_AARCH64_RELATIVE - 0x6
+// ANDROID64-NEXT:     0x10030 R_AARCH64_RELATIVE - 0x7
+// ANDROID64-NEXT:     0x10038 R_AARCH64_RELATIVE - 0x8
 
-// PACKED64-HEADERS:       Name: .rela.dyn
-// PACKED64-HEADERS-NEXT:  Type: SHT_ANDROID_RELA
-// PACKED64-HEADERS-NEXT:  Flags [ (0x2)
-// PACKED64-HEADERS-NEXT:    SHF_ALLOC (0x2)
-// PACKED64-HEADERS-NEXT:  ]
-// PACKED64-HEADERS-NEXT:  Address: [[ADDR:.*]]
-// PACKED64-HEADERS-NEXT:  Offset: [[ADDR]]
-// PACKED64-HEADERS-NEXT:  Size: [[SIZE:.*]]
-// PACKED64-HEADERS-NEXT:  Link: 1
-// PACKED64-HEADERS-NEXT:  Info: 0
-// PACKED64-HEADERS-NEXT:  AddressAlignment: 8
-// PACKED64-HEADERS-NEXT:  EntrySize: 1
+// ANDROID64-NEXT:     0x10088 R_AARCH64_RELATIVE - 0x1
+// ANDROID64-NEXT:     0x10090 R_AARCH64_RELATIVE - 0x2
+// ANDROID64-NEXT:     0x10098 R_AARCH64_RELATIVE - 0x3
+// ANDROID64-NEXT:     0x100A0 R_AARCH64_RELATIVE - 0x4
+// ANDROID64-NEXT:     0x100A8 R_AARCH64_RELATIVE - 0x5
+// ANDROID64-NEXT:     0x100B0 R_AARCH64_RELATIVE - 0x6
+// ANDROID64-NEXT:     0x100B8 R_AARCH64_RELATIVE - 0x7
+// ANDROID64-NEXT:     0x100C0 R_AARCH64_RELATIVE - 0x8
+// ANDROID64-NEXT:     0x100C8 R_AARCH64_RELATIVE - 0x9
 
-// PACKED64-HEADERS: 0x0000000060000011 ANDROID_RELA          [[ADDR]]
-// PACKED64-HEADERS: 0x0000000060000012 ANDROID_RELASZ        [[SIZE]]
+// ANDROID64-NEXT:     0x10048 R_AARCH64_RELATIVE - 0x1
+// ANDROID64-NEXT:     0x10050 R_AARCH64_RELATIVE - 0x2
+// ANDROID64-NEXT:     0x10058 R_AARCH64_RELATIVE - 0x3
+// ANDROID64-NEXT:     0x10060 R_AARCH64_RELATIVE - 0x4
+// ANDROID64-NEXT:     0x10068 R_AARCH64_RELATIVE - 0x5
+// ANDROID64-NEXT:     0x10070 R_AARCH64_RELATIVE - 0x6
+// ANDROID64-NEXT:     0x10078 R_AARCH64_RELATIVE - 0x7
+
+// ANDROID64-NEXT:     0x100D1 R_AARCH64_RELATIVE - 0xA
+// ANDROID64-NEXT:     0x10040 R_AARCH64_ABS64 bar2 0x1
+// ANDROID64-NEXT:     0x10080 R_AARCH64_ABS64 zed2 0x0
+// ANDROID64-NEXT:     }
+
+// RUN: ld.lld -pie --pack-dyn-relocs=relr %t.a64 %t.a64.so -o %t4.a64
+// RUN: llvm-readobj -s -dynamic-table %t4.a64 | FileCheck --check-prefix=RELR64-HEADERS %s
+// RUN: llvm-readobj -relocations -raw-relr %t4.a64 | FileCheck --check-prefix=RAW-RELR64 %s
+// RUN: llvm-readobj -relocations %t4.a64 | FileCheck --check-prefix=RELR64 %s
+
+// RELR64-HEADERS:       Index: 1
+// RELR64-HEADERS-NEXT:  Name: .dynsym
+
+// RELR64-HEADERS:       Name: .relr.dyn
+// RELR64-HEADERS-NEXT:  Type: SHT_RELR
+// RELR64-HEADERS-NEXT:  Flags [ (0x2)
+// RELR64-HEADERS-NEXT:    SHF_ALLOC (0x2)
+// RELR64-HEADERS-NEXT:  ]
+// RELR64-HEADERS-NEXT:  Address: [[ADDR:.*]]
+// RELR64-HEADERS-NEXT:  Offset: [[ADDR]]
+// RELR64-HEADERS-NEXT:  Size: 16
+// RELR64-HEADERS-NEXT:  Link: 0
+// RELR64-HEADERS-NEXT:  Info: 0
+// RELR64-HEADERS-NEXT:  AddressAlignment: 8
+// RELR64-HEADERS-NEXT:  EntrySize: 8
+
+// RELR64-HEADERS:       0x0000000000000024 RELR                 [[ADDR]]
+// RELR64-HEADERS:       0x0000000000000023 RELRSZ               0x10
+// RELR64-HEADERS:       0x0000000000000025 RELRENT              0x8
+
+// SHT_RELR section contains address/bitmap entries
+// encoding the offsets for relative relocation.
+// RAW-RELR64:           Section ({{.+}}) .relr.dyn {
+// RAW-RELR64-NEXT:      0x10000
+// RAW-RELR64-NEXT:      0x3FEFEFF
+// RAW-RELR64-NEXT:      }
+
+// Decoded SHT_RELR section is same as UNPACKED,
+// but contains only the relative relocations.
+// Any relative relocations with odd offset stay in SHT_RELA.
+// RELR64:               Section ({{.+}}) .rela.dyn {
+// RELR64-NEXT:          0x100D1 R_AARCH64_RELATIVE - 0xA
+// RELR64-NEXT:          0x10040 R_AARCH64_ABS64 bar2 0x1
+// RELR64-NEXT:          0x10080 R_AARCH64_ABS64 zed2 0x0
+// RELR64-NEXT:          }
+// RELR64-NEXT:          Section ({{.+}}) .relr.dyn {
+// RELR64-NEXT:          0x10000 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x10008 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x10010 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x10018 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x10020 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x10028 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x10030 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x10038 R_AARCH64_RELATIVE - 0x0
+
+// RELR64-NEXT:          0x10048 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x10050 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x10058 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x10060 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x10068 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x10070 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x10078 R_AARCH64_RELATIVE - 0x0
+
+// RELR64-NEXT:          0x10088 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x10090 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x10098 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x100A0 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x100A8 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x100B0 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x100B8 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x100C0 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          0x100C8 R_AARCH64_RELATIVE - 0x0
+// RELR64-NEXT:          }
 
 .data
+.align 2
 .dc.a __ehdr_start + 1
 .dc.a __ehdr_start + 2
 .dc.a __ehdr_start + 3
@@ -208,3 +361,5 @@
 .dc.a __ehdr_start + 7
 .dc.a __ehdr_start + 8
 .dc.a __ehdr_start + 9
+.byte 00
+.dc.a __ehdr_start + 10
diff --git a/test/ELF/pack-dyn-relocs2.s b/test/ELF/pack-dyn-relocs2.s
new file mode 100644
index 0000000..20c7176
--- /dev/null
+++ b/test/ELF/pack-dyn-relocs2.s
@@ -0,0 +1,85 @@
+// REQUIRES: arm, aarch64
+
+// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %p/Inputs/arm-shared.s -o %t.so.o
+// RUN: ld.lld -shared %t.so.o -o %t.so
+
+// RUN: llvm-mc -filetype=obj -triple=armv7a-none-linux-gnueabi %s -o %t.o
+// RUN: ld.lld -pie --pack-dyn-relocs=relr %t.o %t.so -o %t.exe
+// RUN: llvm-readobj -relocations %t.exe | FileCheck %s
+
+// CHECK:      Section (5) .relr.dyn {
+// CHECK-NEXT:   0x1000 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1004 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1008 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x100C R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1010 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1014 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1018 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x101C R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1020 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1024 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1028 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x102C R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1030 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1034 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1038 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x103C R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1040 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1044 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1048 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x104C R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1050 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1054 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1058 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x105C R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1060 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1064 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1068 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x106C R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1070 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1074 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1078 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x107C R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1080 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT:   0x1084 R_ARM_RELATIVE - 0x0
+// CHECK-NEXT: }
+
+// RUN: llvm-readobj -s -dynamic-table %t.exe | FileCheck --check-prefix=HEADER %s
+// HEADER: 0x00000023 RELRSZ 0xC
+
+.data
+.align 2
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
+.dc.a __ehdr_start
diff --git a/test/ELF/plt-aarch64.s b/test/ELF/plt-aarch64.s
index 372186b..8f637bf 100644
--- a/test/ELF/plt-aarch64.s
+++ b/test/ELF/plt-aarch64.s
@@ -1,3 +1,4 @@
+// REQUIRES: aarch64
 // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-freebsd %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=aarch64-pc-freebsd %p/Inputs/plt-aarch64.s -o %t2.o
 // RUN: ld.lld -shared %t2.o -o %t2.so
@@ -10,8 +11,6 @@
 // RUN: llvm-objdump -s -section=.got.plt %t.exe | FileCheck --check-prefix=DUMPEXE %s
 // RUN: llvm-objdump -d %t.exe | FileCheck --check-prefix=DISASMEXE %s
 
-// REQUIRES: aarch64
-
 // CHECKDSO:     Name: .plt
 // CHECKDSO-NEXT:     Type: SHT_PROGBITS
 // CHECKDSO-NEXT:     Flags [
diff --git a/test/ELF/plt-i686.s b/test/ELF/plt-i686.s
index 9a2c7f5..c24cab2 100644
--- a/test/ELF/plt-i686.s
+++ b/test/ELF/plt-i686.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %p/Inputs/shared.s -o %t2.o
 // RUN: ld.lld -shared %t2.o -o %t2.so
@@ -9,7 +10,6 @@
 // RUN: llvm-objdump -d %t | FileCheck --check-prefix=DISASMSHARED %s
 // RUN: ld.lld -pie %t.o %t2.so -o %t
 // RUN: llvm-objdump -d %t | FileCheck --check-prefix=DISASMPIE %s
-// REQUIRES: x86
 
 // CHECK:      Name: .plt
 // CHECK-NEXT: Type: SHT_PROGBITS
diff --git a/test/ELF/plt.s b/test/ELF/plt.s
index 4ab81aa..cce60d7 100644
--- a/test/ELF/plt.s
+++ b/test/ELF/plt.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/shared.s -o %t2.o
 // RUN: ld.lld -shared %t2.o -o %t2.so
@@ -8,8 +9,6 @@
 // RUN: llvm-readobj -s -r %t3 | FileCheck --check-prefix=CHECK2 %s
 // RUN: llvm-objdump -d %t3 | FileCheck --check-prefix=DISASM2 %s
 
-// REQUIRES: x86
-
 // CHECK:      Name: .plt
 // CHECK-NEXT: Type: SHT_PROGBITS
 // CHECK-NEXT: Flags [
diff --git a/test/ELF/ppc-relocs.s b/test/ELF/ppc-relocs.s
index 5aa3474..2681000 100644
--- a/test/ELF/ppc-relocs.s
+++ b/test/ELF/ppc-relocs.s
@@ -1,7 +1,7 @@
+# REQUIRES: ppc
 # RUN: llvm-mc -filetype=obj -triple=powerpc-unknown-freebsd %s -o %t
 # RUN: ld.lld %t -o %t2
 # RUN: llvm-objdump -d %t2 | FileCheck %s
-# REQUIRES: ppc
 
 .section .R_PPC_ADDR16_HA,"ax",@progbits
 .globl _start
diff --git a/test/ELF/ppc64-abi-version.s b/test/ELF/ppc64-abi-version.s
new file mode 100644
index 0000000..806a71e
--- /dev/null
+++ b/test/ELF/ppc64-abi-version.s
@@ -0,0 +1,11 @@
+# REQUIRES: ppc
+
+# RUN: echo '.abiversion 1' | llvm-mc -filetype=obj -triple=powerpc64le-unknown-linux - -o %t1.o
+# RUN: not ld.lld -o /dev/null %t1.o 2>&1 | FileCheck -check-prefix=ERR1 %s
+
+# ERR1: ABI version 1 is not supported
+
+# RUN: echo '.abiversion 3' | llvm-mc -filetype=obj -triple=powerpc64le-unknown-linux - -o %t1.o
+# RUN: not ld.lld -o /dev/null %t1.o 2>&1 | FileCheck -check-prefix=ERR2 %s
+
+# ERR2: unrecognized e_flags: 3
diff --git a/test/ELF/ppc64-addr16-error.s b/test/ELF/ppc64-addr16-error.s
index d258089..5ffca58 100644
--- a/test/ELF/ppc64-addr16-error.s
+++ b/test/ELF/ppc64-addr16-error.s
@@ -2,11 +2,11 @@
 
 // RUN: llvm-mc -filetype=obj -triple=powerpc64le-unknown-linux %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=powerpc64le-unknown-linux %p/Inputs/ppc64-addr16-error.s -o %t2
-// RUN: not ld.lld -shared %t %t2 -o %t3 2>&1 | FileCheck %s
+// RUN: not ld.lld -shared %t %t2 -o /dev/null 2>&1 | FileCheck %s
 
 // RUN: llvm-mc -filetype=obj -triple=powerpc64-unknown-linux %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=powerpc64-unknown-linux %p/Inputs/ppc64-addr16-error.s -o %t2
-// RUN: not ld.lld -shared %t %t2 -o %t3 2>&1 | FileCheck %s
+// RUN: not ld.lld -shared %t %t2 -o /dev/null 2>&1 | FileCheck %s
 
 .short sym+65539
 
diff --git a/test/ELF/ppc64-dtprel.s b/test/ELF/ppc64-dtprel.s
new file mode 100644
index 0000000..43922fa
--- /dev/null
+++ b/test/ELF/ppc64-dtprel.s
@@ -0,0 +1,204 @@
+// REQUIRES: ppc
+
+// RUN: llvm-mc -filetype=obj -triple=powerpc64le-unknown-linux %s -o %t.o
+// RUN: ld.lld -shared %t.o -o %t.so
+// RUN: llvm-readelf -relocations --wide %t.o | FileCheck --check-prefix=InputRelocs %s
+// RUN: llvm-readelf -relocations --wide %t.so | FileCheck --check-prefix=OutputRelocs %s
+// RUN: llvm-objdump -D %t.so | FileCheck --check-prefix=Dis %s
+// RUN: llvm-objdump -D %t.so | FileCheck --check-prefix=GotDisLE %s
+
+// RUN: llvm-mc -filetype=obj -triple=powerpc64-unknown-linux %s -o %t.o
+// RUN: ld.lld -shared %t.o -o %t.so
+// RUN: llvm-readelf -relocations --wide %t.o | FileCheck --check-prefix=InputRelocs %s
+// RUN: llvm-readelf -relocations --wide %t.so | FileCheck --check-prefix=OutputRelocs %s
+// RUN: llvm-objdump -D %t.so | FileCheck --check-prefix=Dis %s
+// RUN: llvm-objdump -D %t.so | FileCheck --check-prefix=GotDisBE %s
+
+        .text
+        .abiversion 2
+        .globl  test
+        .p2align        4
+        .type   test,@function
+test:
+.Lfunc_gep0:
+        addis 2, 12, .TOC.-.Lfunc_gep0@ha
+        addi 2, 2, .TOC.-.Lfunc_gep0@l
+.Lfunc_lep0:
+        .localentry     test, .Lfunc_lep0-.Lfunc_gep0
+        mflr 0
+        std 0, 16(1)
+        stdu 1, -32(1)
+        addis 3, 2, i@got@tlsld@ha
+        addi 3, 3, i@got@tlsld@l
+        bl __tls_get_addr(i@tlsld)
+        nop
+        addi 4, 3, i@dtprel
+        lwa 4, i@dtprel(3)
+        ld 0, 16(1)
+        mtlr 0
+        blr
+
+        .globl test_64
+        .p2align        4
+        .type    test_64,@function
+
+        .globl test_adjusted
+        .p2align        4
+        .type    test_adjusted,@function
+test_adjusted:
+.Lfunc_gep1:
+        addis 2, 12, .TOC.-.Lfunc_gep1@ha
+        addi 2, 2, .TOC.-.Lfunc_gep1@l
+.Lfunc_lep1:
+        .localentry     test_adjusted, .Lfunc_lep1-.Lfunc_gep1
+        mflr 0
+        std 0, 16(1)
+        stdu 1, -32(1)
+        addis 3, 2, k@got@tlsld@ha
+        addi 3, 3, k@got@tlsld@l
+        bl __tls_get_addr(k@tlsld)
+        nop
+        lis 4, k@dtprel@highesta
+        ori 4, 4, k@dtprel@highera
+        lis 5, k@dtprel@ha
+        addi 5, 5, k@dtprel@l
+        sldi 4, 4, 32
+        or   4, 4, 5
+        add  3, 3, 4
+        addi 1, 1, 32
+        ld 0, 16(1)
+        mtlr 0
+        blr
+
+        .globl test_not_adjusted
+        .p2align      4
+        .type test_not_adjusted,@function
+test_not_adjusted:
+.Lfunc_gep2:
+        addis 2, 12, .TOC.-.Lfunc_gep2@ha
+        addi 2, 2, .TOC.-.Lfunc_gep2@l
+.Lfunc_lep2:
+        .localentry     test_not_adjusted, .Lfunc_lep2-.Lfunc_gep2
+        mflr 0
+        std 0, 16(1)
+        stdu 1, -32(1)
+        addis 3, 2, i@got@tlsld@ha
+        addi 3, 3, i@got@tlsld@l
+        bl __tls_get_addr(k@tlsld)
+        nop
+        lis 4, k@dtprel@highest
+        ori 4, 4, k@dtprel@higher
+        sldi 4, 4, 32
+        oris  4, 4, k@dtprel@h
+        ori   4, 4, k@dtprel@l
+        add 3, 3, 4
+        addi 1, 1, 32
+        ld 0, 16(1)
+        mtlr 0
+        blr
+
+        .globl test_got_dtprel
+        .p2align 4
+        .type test_got_dtprel,@function
+test_got_dtprel:
+         addis 3, 2, i@got@dtprel@ha
+         ld 3, i@got@dtprel@l(3)
+         addis 3, 2, i@got@dtprel@h
+         addi 3, 2, i@got@dtprel
+
+        .section        .debug_addr,"",@progbits
+        .quad   i@dtprel+32768
+
+        .type   i,@object
+        .section        .tdata,"awT",@progbits
+        .space 1024
+        .p2align        2
+i:
+        .long   55
+        .size   i, 4
+
+        .space 1024 * 1024 * 4
+        .type k,@object
+        .p2align 2
+k:
+       .long 128
+       .size k,4
+
+// Verify the input has all the remaining DTPREL based relocations we want to
+// test.
+// InputRelocs: Relocation section '.rela.text'
+// InputRelocs: R_PPC64_DTPREL16          {{[0-9a-f]+}} i + 0
+// InputRelocs: R_PPC64_DTPREL16_DS       {{[0-9a-f]+}} i + 0
+// InputRelocs: R_PPC64_DTPREL16_HIGHESTA {{[0-9a-f]+}} k + 0
+// InputRelocs: R_PPC64_DTPREL16_HIGHERA  {{[0-9a-f]+}} k + 0
+// InputRelocs: R_PPC64_DTPREL16_HA       {{[0-9a-f]+}} k + 0
+// InputRelocs: R_PPC64_DTPREL16_LO       {{[0-9a-f]+}} k + 0
+// InputRelocs: R_PPC64_DTPREL16_HIGHEST  {{[0-9a-f]+}} k + 0
+// InputRelocs: R_PPC64_DTPREL16_HIGHER   {{[0-9a-f]+}} k + 0
+// InputRelocs: R_PPC64_DTPREL16_HI       {{[0-9a-f]+}} k + 0
+// InputRelocs: R_PPC64_DTPREL16_LO       {{[0-9a-f]+}} k + 0
+// InputRelocs: R_PPC64_GOT_DTPREL16_HA    {{[0-9a-f]+}} i + 0
+// InputRelocs: R_PPC64_GOT_DTPREL16_LO_DS {{[0-9a-f]+}} i + 0
+// InputRelocs: R_PPC64_GOT_DTPREL16_HI    {{[0-9a-f]+}} i + 0
+// InputRelocs: R_PPC64_GOT_DTPREL16_DS    {{[0-9a-f]+}} i + 0
+// InputRelocs: Relocation section '.rela.debug_addr'
+// InputRelocs: R_PPC64_DTPREL64          {{[0-9a-f]+}} i + 8000
+
+// Expect a single dynamic relocation in the '.rela.dyn section for the module id.
+// OutputRelocs:      Relocation section '.rela.dyn' at offset 0x{{[0-9a-f]+}} contains 1 entries:
+// OutputRelocs-NEXT: Offset Info Type Symbol's Value Symbol's Name + Addend
+// OutputRelocs-NEXT: R_PPC64_DTPMOD64
+
+
+// i@dtprel  --> (1024 - 0x8000) = -31744
+// Dis: test:
+// Dis:    addi 4, 3, -31744
+// Dis:    lwa 4, -31744(3)
+
+// #k@dtprel(1024 + 4 + 1024 * 1024 * 4) = 0x400404
+
+// #highesta(k@dtprel) --> ((0x400404 - 0x8000 + 0x8000) >> 48) & 0xffff = 0
+// #highera(k@dtprel)  --> ((0x400404 - 0x8000 + 0x8000) >> 32) & 0xffff = 0
+// #ha(k@dtprel)       --> ((0x400404 - 0x8000 + 0x8000) >> 16) & 0xffff = 64
+// #lo(k@dtprel)       --> ((0x400404 - 0x8000) & 0xffff = -31740
+// Dis:  test_adjusted:
+// Dis:     lis 4, 0
+// Dis:     ori 4, 4, 0
+// Dis:     lis 5, 64
+// Dis:     addi 5, 5, -31740
+
+// #highest(k@dtprel) --> ((0x400404 - 0x8000) >> 48) & 0xffff = 0
+// #higher(k@dtprel)  --> ((0x400404 - 0x8000) >> 32) & 0xffff = 0
+// #hi(k@dtprel)      --> ((0x400404 - 0x8000) >> 16) & 0xffff = 63
+// #lo(k@dtprel)      --> ((0x400404 - 0x8000) & 0xffff = 33796
+// Dis:  test_not_adjusted:
+// Dis:    lis 4, 0
+// Dis:    ori 4, 4, 0
+// Dis:    oris 4, 4, 63
+// Dis:    ori 4, 4, 33796
+
+// Check for GOT entry for i. There should be a got entry which holds the offset
+// of i relative to the dynamic thread pointer.
+// i@dtprel ->  (1024 - 0x8000) = 0xffff8400
+// GotDisBE: Disassembly of section .got:
+// GotDisBE: 4204f8: 00 00 00 00
+// GotDisBE: 4204fc: 00 42 84 f8
+// GotDisBE: 420510: ff ff ff ff
+// GotDisBE: 420514: ff ff 84 00
+
+// GotDisLE: Disassembly of section .got:
+// GotDisLE: 4204f8: f8 84 42 00
+// GotDisLE: 420510: 00 84 ff ff
+// GotDisLE: 420514: ff ff ff ff
+
+// Check that we have the correct offset to the got entry for i@got@dtprel
+// The got entry for i is 0x420510, and the TOC pointer is 0x4284f8.
+// #ha(i@got@dtprel) --> ((0x420510 - 0x4284f8 + 0x8000) >> 16) & 0xffff = 0
+// #lo(i@got@dtprel) --> (0x420510 - 0x4284f8) & 0xffff = -32744
+// #hi(i@got@dtprel) --> ((0x420510 - 0x4284f8) >> 16) & 0xffff = -1
+// i@got@dtprel --> 0x420510 - 0x4284f8 = -32744
+// Dis: test_got_dtprel:
+// Dis:    addis 3, 2, 0
+// Dis:    ld 3, -32744(3)
+// Dis:    addis 3, 2, -1
+// Dis:    addi 3, 2, -32744
diff --git a/test/ELF/ppc64-gd-to-ie.s b/test/ELF/ppc64-gd-to-ie.s
new file mode 100644
index 0000000..1a6cc5b
--- /dev/null
+++ b/test/ELF/ppc64-gd-to-ie.s
@@ -0,0 +1,104 @@
+# REQUIRES: ppc
+
+# RUN: llvm-mc -filetype=obj -triple=powerpc64le-unknown-linux %s -o %t.o
+# RUN: llvm-mc -filetype=obj -triple=powerpc64le-unknown-linux %p/Inputs/ppc64-tls.s -o %t2.o
+# RUN: ld.lld -shared %t2.o -o %t3.so
+# RUN: ld.lld  %t.o %t3.so -o %t
+# RUN: llvm-objdump --section-headers %t | FileCheck --check-prefix=CheckGot %s
+# RUN: llvm-objdump -D %t | FileCheck --check-prefix=Dis %s
+# RUN: llvm-readelf -relocations --wide %t | FileCheck --check-prefix=OutputRelocs %s
+
+# RUN: llvm-mc -filetype=obj -triple=powerpc64-unknown-linux %s -o %t.o
+# RUN: llvm-mc -filetype=obj -triple=powerpc64-unknown-linux %p/Inputs/ppc64-tls.s -o %t2.o
+# RUN: ld.lld -shared %t2.o -o %t3.so
+# RUN: ld.lld  %t.o %t3.so -o %t
+# RUN: llvm-objdump --section-headers %t | FileCheck --check-prefix=CheckGot %s
+# RUN: llvm-objdump -D %t | FileCheck --check-prefix=Dis %s
+# RUN: llvm-readelf -relocations --wide %t | FileCheck --check-prefix=OutputRelocs %s
+
+        .text
+        .abiversion 2
+        .globl _start
+        .p2align        4
+        .type   _start,@function
+_start:
+.Lfunc_gep0:
+        addis 2, 12, .TOC.-.Lfunc_gep0@ha
+        addi 2, 2, .TOC.-.Lfunc_gep0@l
+.Lfunc_lep0:
+        .localentry     _start, .Lfunc_lep0-.Lfunc_gep0
+        mflr 0
+        std 0, 16(1)
+        stdu 1, -32(1)
+        addis 3, 2, a@got@tlsgd@ha
+        addi 3, 3, a@got@tlsgd@l
+        bl __tls_get_addr(a@tlsgd)
+        nop
+        lwa 3, 0(3)
+        addi 1, 1, 32
+        ld 0, 16(1)
+        mtlr 0
+        blr
+
+
+        .globl other_reg
+        .p2align        4
+        .type   other_reg,@function
+other_reg:
+.Lfunc_gep1:
+        addis 2, 12, .TOC.-.Lfunc_gep1@ha
+        addi 2, 2, .TOC.-.Lfunc_gep1@l
+.Lfunc_lep1:
+        .localentry     other_reg, .Lfunc_lep1-.Lfunc_gep1
+        mflr 0
+        std 0, 16(1)
+        stdu 1, -32(1)
+        addis 5, 2, a@got@tlsgd@ha
+        addi 3, 5, a@got@tlsgd@l
+        bl __tls_get_addr(a@tlsgd)
+        nop
+        lwa 4, 0(3)
+        addis 30, 2, b@got@tlsgd@ha
+        addi 3, 30, b@got@tlsgd@l
+        bl __tls_get_addr(b@tlsgd)
+        nop
+        lwa 3, 0(3)
+        add 3, 4, 3
+        addi 1, 1, 32
+        ld 0, 16(1)
+        mtlr 0
+        blr
+
+        .globl __tls_get_addr
+        .type __tls_get_addr,@function
+__tls_get_addr:
+
+
+# CheckGot: .got          00000018 00000000100200c0 DATA
+# .got is at 0x100200c0 so the toc-base is 100280c0.
+# `a` is at .got[1], we expect the offsets to be:
+# Ha(a) = ((0x100200c8  - 0x100280c0) + 0x8000) >> 16 = 0
+# Lo(a) = (0x100200c8  - 0x100280c0) = -32760
+
+# Dis-LABEL: _start
+# Dis:         addis 3, 2, 0
+# Dis-NEXT:    ld 3, -32760(3)
+# Dis-NEXT:    nop
+# Dis-NEXT:    add 3, 3, 13
+
+# Dis-LABEL: other_reg
+# Dis:         addis 5, 2, 0
+# Dis-NEXT:    ld 3, -32760(5)
+# Dis-NEXT:    nop
+# Dis-NEXT:    add 3, 3, 13
+# Dis:         addis 30, 2, 0
+# Dis:         ld 3, -32752(30)
+# Dis-NEXT:    nop
+# Dis-NEXT:    add 3, 3, 13
+
+# Verify that the only dynamic relocations we emit are TPREL ones rather then
+# the DTPMOD64/DTPREL64 pair for general-dynamic.
+# OutputRelocs: Relocation section '.rela.dyn' at offset 0x{{[0-9a-f]+}} contains 2 entries:
+# OutputRelocs-NEXT:    Offset             Info             Type               Symbol's Value  Symbol's Name + Addend
+# OutputRelocs-NEXT:  {{[0-9a-f]+}}    {{[0-9a-f]+}}   R_PPC64_TPREL64        {{[0-9a-f]+}} a + 0
+# OutputRelocs-NEXT:  {{[0-9a-f]+}}    {{[0-9a-f]+}}   R_PPC64_TPREL64        {{[0-9a-f]+}} b + 0
diff --git a/test/ELF/ppc64-local-dynamic.s b/test/ELF/ppc64-local-dynamic.s
index a051eae..57f324e 100644
--- a/test/ELF/ppc64-local-dynamic.s
+++ b/test/ELF/ppc64-local-dynamic.s
@@ -32,6 +32,8 @@
         addi 3, 3, i@got@tlsld@l
         bl __tls_get_addr(i@tlsld)
         nop
+        addis 3, 3, i@dtprel@ha
+        lwa 3, i@dtprel@l(3)
         ld 0, 16(1)
         mtlr 0
         blr
@@ -71,13 +73,17 @@
         .quad   66
         .size   k, 8
 
-// Verify that the input contains all the R_PPC64_GOT_TLSLD16* relocations.
+// Verify that the input contains all the R_PPC64_GOT_TLSLD16* relocations, as
+// well as the DTPREL relocations used in a typical medium code model
+// local-dynamic variable access.
 // InputRelocs: Relocation section '.rela.text'
-// InputRelocs:     R_PPC64_GOT_TLSLD16_HA 0000000000000000 i + 0
-// InputRelocs:     R_PPC64_GOT_TLSLD16_LO 0000000000000000 i + 0
-// InputRelocs:     R_PPC64_TLSLD          0000000000000000 i + 0
-// InputRelocs:     R_PPC64_GOT_TLSLD16_HI 0000000000000000 j + 0
-// InputRelocs:     R_PPC64_GOT_TLSLD16    0000000000000008 k + 0
+// InputRelocs:     R_PPC64_GOT_TLSLD16_HA {{[0-9a-f]+}} i + 0
+// InputRelocs:     R_PPC64_GOT_TLSLD16_LO {{[0-9a-f]+}} i + 0
+// InputRelocs:     R_PPC64_TLSLD          {{[0-9a-f]+}} i + 0
+// InputRelocs:     R_PPC64_DTPREL16_HA    {{[0-9a-f]+}} i + 0
+// InputRelocs:     R_PPC64_DTPREL16_LO_DS {{[0-9a-f]+}} i + 0
+// InputRelocs:     R_PPC64_GOT_TLSLD16_HI {{[0-9a-f]+}} j + 0
+// InputRelocs:     R_PPC64_GOT_TLSLD16    {{[0-9a-f]+}} k + 0
 
 // The local dynamic version of tls needs to use the same mechanism to look up
 // a variables address as general-dynamic. ie a call to __tls_get_addr with the
@@ -99,9 +105,19 @@
 
 // #ha(i@got@tlsld) --> (0x20108 - 0x28100 + 0x8000) >> 16 = 0
 // #lo(i@got@tlsld) --> (0x20108 - 0x28100) = -7ff8 = -32760
+// When calculating offset relative to the dynamic thread pointer we have to
+// adjust by 0x8000 since each DTV pointer points 0x8000 bytes past the start of
+// its TLS block.
+// #ha(i@dtprel) --> (0x0 -0x8000 + 0x8000) >> 16 = 0
+// #lo(i@dtprel) --> (0x0 -0x8000) = -0x8000 = -32768
 // Dis:     test:
 // Dis:        addis 3, 2, 0
 // Dis-NEXT:   addi 3, 3, -32760
+// Dis-NEXT:   bl .+67108804
+// Dis-NEXT:   ld 2, 24(1)
+// Dis-NEXT:   addis 3, 3, 0
+// Dis-NEXT:   lwa 3, -32768(3)
+
 
 // #hi(j@got@tlsld) --> (0x20108 - 0x28100 ) > 16 = -1
 // Dis: test_hi:
diff --git a/test/ELF/ppc64-local-exec-tls.s b/test/ELF/ppc64-local-exec-tls.s
new file mode 100644
index 0000000..ff8c2b9
--- /dev/null
+++ b/test/ELF/ppc64-local-exec-tls.s
@@ -0,0 +1,163 @@
+// REQUIRES: ppc
+// RUN: llvm-mc -filetype=obj -triple=powerpc64le-unknown-linux %s -o %t.o
+// RUN: ld.lld  %t.o -o %t
+// RUN: llvm-readelf -relocations --wide %t.o | FileCheck --check-prefix=InputRelocs %s
+// RUN: llvm-objdump -D %t | FileCheck --check-prefix=Dis %s
+
+	.text
+	.abiversion 2
+	.globl	test_local_exec                    # -- Begin function test_local_exec
+	.p2align	4
+	.type	test_local_exec,@function
+test_local_exec:                                   # @test_local_exec
+.Lfunc_begin0:
+# %bb.0:                                # %entry
+	li 3, 0
+	stw 3, -12(1)
+	addis 3, 13, a@tprel@ha
+	addi 3, 3, a@tprel@l
+	ld 3, 0(3)
+	mr 4, 3
+	extsw 3, 4
+	blr
+	.long	0
+	.quad	0
+.Lfunc_end0:
+	.size	test_local_exec, .Lfunc_end0-.Lfunc_begin0
+                                        # -- End function
+test_tprel:
+.Lfunc_gep1:
+  addis 2, 12, .TOC.-.Lfunc_gep1@ha
+  addi 2, 2, .TOC.-.Lfunc_gep1@l
+.Lfunc_lep1:
+  .localentry test_tprel, .Lfunc_lep1-.Lfunc_gep1
+  addi 3, 13, b@tprel
+  blr
+
+
+test_hi:
+.Lfunc_gep2:
+  addis 2, 12, .TOC.-.Lfunc_gep2@ha
+  addi  2, 2,  .TOC.-.Lfunc_gep2@l
+.Lfunc_lep2:
+  .localentry test_hi, .Lfunc_lep2-.Lfunc_gep2
+  addis 3, 13, b@tprel@h
+  blr
+
+test_ds:
+.Lfunc_gep3:
+  addis 2, 12, .TOC.-.Lfunc_gep3@ha
+  addi 2, 2, .TOC.-.Lfunc_gep3@l
+.Lfunc_lep3:
+  .localentry test_ds, .Lfunc_lep3-.Lfunc_gep3
+  ld 3, b@tprel, 13
+  blr
+
+test_lo_ds:
+.Lfunc_gep4:
+  addis 2, 12, .TOC.-.Lfunc_gep4@ha
+  addi 2, 2, .TOC.-.Lfunc_gep4@l
+.Lfunc_lep4:
+  .localentry test_lo_ds, .Lfunc_lep4-.Lfunc_gep4
+  ld 3, b@tprel@l, 13
+  blr
+
+test_highest_a:
+.Lfunc_gep5:
+  addis 2, 12, .TOC.-.Lfunc_gep5@ha
+  addi  2, 2,  .TOC.-.Lfunc_gep5@l
+.Lfunc_lep5:
+  .localentry test_highest_a, .Lfunc_lep5-.Lfunc_gep5
+  lis 4, b@tprel@highesta
+  ori 4, 4, b@tprel@highera
+  lis 5, b@tprel@ha
+  addi 5, 5, b@tprel@l
+  sldi 4, 4, 32
+  or   4, 4, 5
+  add  3, 13, 4
+  blr
+
+test_highest:
+.Lfunc_gep6:
+  addis 2, 12, .TOC.-.Lfunc_gep6@ha
+  addi  2, 2,  .TOC.-.Lfunc_gep6@l
+.Lfunc_lep6:
+  .localentry test_highest, .Lfunc_lep6-.Lfunc_gep6
+  lis 4, b@tprel@highest
+  ori 4, 4, b@tprel@higher
+  sldi 4, 4, 32
+  oris  4, 4, b@tprel@h
+  ori   4, 4, b@tprel@l
+  add  3, 13, 4
+  blr
+
+	.type	a,@object               # @a
+	.type	b,@object               # @b
+	.section	.tdata,"awT",@progbits
+	.p2align	3
+a:
+	.quad	55                      # 0x37
+	.size	a, 8
+
+b:
+	.quad	55                      # 0x37
+	.size	b, 8
+
+// Verify that the input has every initial-exec tls relocation type.
+// InputRelocs: Relocation section '.rela.text'
+// InputRelocs: R_PPC64_TPREL16_HA {{0+}} a + 0
+// InputRelocs: R_PPC64_TPREL16_LO {{0+}} a + 0
+// InputRelocs: R_PPC64_TPREL16 {{0+8}} b + 0
+// InputRelocs: R_PPC64_TPREL16_HI {{0+8}} b + 0
+// InputRelocs: R_PPC64_TPREL16_DS {{0+8}} b + 0
+// InputRelocs: R_PPC64_TPREL16_LO_DS {{0+8}} b + 0
+// InputRelocs: R_PPC64_TPREL16_HIGHESTA {{0+8}} b + 0
+// InputRelocs: R_PPC64_TPREL16_HIGHERA {{0+8}} b + 0
+// InputRelocs: R_PPC64_TPREL16_HIGHEST {{0+8}} b + 0
+// InputRelocs: R_PPC64_TPREL16_HIGHER {{0+8}} b + 0
+
+// The start of the TLS storage area is 0x7000 bytes before the thread pointer (r13).
+// We are building the address of the first TLS variable, relative to the thread pointer.
+// #ha(a@tprel) --> (0 - 0x7000 + 0x8000) >> 16 = 0
+// #lo(a@tprel)) --> (0 - 0x7000) &  0xFFFF =  -0x7000 = -28672
+// Dis: test_local_exec:
+// Dis: addis 3, 13, 0
+// Dis: addi 3, 3, -28672
+
+// We are building the offset for the second TLS variable
+// Offset within tls storage - 0x7000
+// b@tprel = 8 - 0x7000 = 28664
+// Dis: test_tprel:
+// Dis: addi 3, 13, -28664
+
+// #hi(b@tprel) --> (8 - 0x7000) >> 16 = -1
+// Dis: test_hi:
+// Dis: addis 3, 13, -1
+
+// b@tprel = 8 - 0x7000 = -28664
+// Dis: test_ds:
+// Dis: ld 3, -28664(13)
+
+// #lo(b@tprel) --> (8 - 0x7000) & 0xFFFF = -28664
+// Dis: test_lo_ds:
+// Dis: ld 3, -28664(13)
+
+// #highesta(b@tprel) --> ((0x8 - 0x7000 + 0x8000) >> 48) & 0xFFFF = 0
+// #highera(b@tprel)  --> ((0x8 - 0x7000 + 0x8000) >> 32) & 0xFFFF = 0
+// #ha(k@dtprel)       --> ((0x8 - 0x7000 + 0x8000) >> 16) & 0xFFFF = 0
+// #lo(k@dtprel)       --> ((0x8 - 0x7000) & 0xFFFF = -28664
+// Dis: test_highest_a:
+// Dis: lis 4, 0
+// Dis: ori 4, 4, 0
+// Dis: lis 5, 0
+// Dis: addi 5, 5, -28664
+
+// #highest(b@tprel) --> ((0x8 - 0x7000) >> 48) & 0xFFFF = 0xFFFF = -1
+// #higher(b@tprel)  --> ((0x8 - 0x7000) >> 32) & 0xFFFF = 0xFFFF = 65535
+// #hi(k@dtprel)      --> ((0x8 - 0x7000) >> 16) & 0xFFFF = 0xFFFF = 65535
+// #lo(k@dtprel)      --> ((0x8 - 0x7000) & 0xFFFF = 33796
+// Dis: test_highest:
+// Dis: lis 4, -1
+// Dis: ori 4, 4, 65535
+// Dis: oris 4, 4, 65535
+// Dis: ori 4, 4, 36872
diff --git a/test/ELF/ppc64-tls-gd-le.s b/test/ELF/ppc64-tls-gd-le.s
new file mode 100644
index 0000000..c55ae5f
--- /dev/null
+++ b/test/ELF/ppc64-tls-gd-le.s
@@ -0,0 +1,83 @@
+// REQUIRES: ppc
+
+// RUN: llvm-mc -filetype=obj -triple=powerpc64le-unknown-linux %s -o %t.o
+// RUN: llvm-readelf -relocations --wide %t.o | FileCheck --check-prefix=InputRelocs %s
+// RUN: ld.lld  %t.o -o %t
+// RUN: llvm-objdump -D %t | FileCheck --check-prefix=Dis %s
+// RUN: llvm-readelf -relocations --wide %t | FileCheck --check-prefix=OutputRelocs %s
+
+// RUN: llvm-mc -filetype=obj -triple=powerpc64-unknown-linux %s -o %t.o
+// RUN: llvm-readelf -relocations --wide %t.o | FileCheck --check-prefix=InputRelocs %s
+// RUN: ld.lld  %t.o -o %t
+// RUN: llvm-objdump -D %t | FileCheck --check-prefix=Dis %s
+// RUN: llvm-readelf -relocations --wide %t | FileCheck --check-prefix=OutputRelocs %s
+
+	.text
+	.abiversion 2
+	.globl	_start                    # -- Begin function _start
+	.p2align	4
+	.type	_start,@function
+_start:                                   # @_start
+.Lfunc_begin0:
+.Lfunc_gep0:
+	addis 2, 12, .TOC.-.Lfunc_gep0@ha
+	addi 2, 2, .TOC.-.Lfunc_gep0@l
+.Lfunc_lep0:
+	.localentry	_start, .Lfunc_lep0-.Lfunc_gep0
+# %bb.0:                                # %entry
+	mflr 0
+	std 31, -8(1)
+	std 0, 16(1)
+	stdu 1, -64(1)
+	mr 31, 1
+	std 30, 48(31)                  # 8-byte Folded Spill
+	li 3, 0
+	stw 3, 44(31)
+	addis 3, 2, a@got@tlsgd@ha
+	addi 3, 3, a@got@tlsgd@l
+	bl __tls_get_addr(a@tlsgd)
+	nop
+	lwz 30, 0(3)
+	extsw 3, 30
+	ld 30, 48(31)                   # 8-byte Folded Reload
+	addi 1, 1, 64
+	ld 0, 16(1)
+	ld 31, -8(1)
+	mtlr 0
+	blr
+	.long	0
+	.quad	0
+.Lfunc_end0:
+	.size	_start, .Lfunc_end0-.Lfunc_begin0
+
+.globl __tls_get_addr
+.type __tls_get_addr,@function
+__tls_get_addr:
+
+                                        # -- End function
+	.type	a,@object               # @a
+	.section	.tdata,"awT",@progbits
+	.globl	a
+	.p2align	2
+a:
+	.long	55                      # 0x37
+	.size	a, 4
+
+// Verify that the input has general-dynamic tls relocation types
+// InputRelocs:  Relocation section '.rela.text'
+// InputRelocs: R_PPC64_GOT_TLSGD16_HA  {{0+}}  a + 0
+// InputRelocs: R_PPC64_GOT_TLSGD16_LO  {{0+}}  a + 0
+// InputRelocs: R_PPC64_TLSGD           {{0+}}  a + 0
+
+// Verify that the general-dynamic sequence is  relaxed to local exec.
+// #ha(a@tprel) --> (0 - 0x7000 + 0x8000) >> 16 = 0
+// #lo(a@tprel)) --> (0 - 0x7000) &  0xFFFF =  -0x7000 = -28672
+// Dis: _start:
+// Dis: nop
+// Dis: addis 3, 13, 0
+// Dis: nop
+// Dis: addi 3, 3, -28672
+
+// Verify that no general-dynamic relocations exist for the dynamic linker.
+// OutputRelocs-NOT: R_PPC64_DTPMOD64
+// OutputRelocs-NOT: R_PPC64_DTPREL64
diff --git a/test/ELF/ppc64-tls-ld-le.s b/test/ELF/ppc64-tls-ld-le.s
new file mode 100644
index 0000000..d42d7b9
--- /dev/null
+++ b/test/ELF/ppc64-tls-ld-le.s
@@ -0,0 +1,84 @@
+// REQUIRES: ppc
+
+// RUN: llvm-mc -filetype=obj -triple=powerpc64le-unknown-linux %s -o %t.o
+// RUN: llvm-readelf -relocations --wide %t.o | FileCheck --check-prefix=InputRelocs %s
+// RUN: ld.lld  %t.o -o %t
+// RUN: llvm-objdump -D %t | FileCheck --check-prefix=Dis %s
+// RUN: llvm-readelf -relocations --wide %t | FileCheck --check-prefix=OutputRelocs %s
+
+// RUN: llvm-mc -filetype=obj -triple=powerpc64-unknown-linux %s -o %t.o
+// RUN: llvm-readelf -relocations --wide %t.o | FileCheck --check-prefix=InputRelocs %s
+// RUN: ld.lld  %t.o -o %t
+// RUN: llvm-objdump -D %t | FileCheck --check-prefix=Dis %s
+// RUN: llvm-readelf -relocations --wide %t | FileCheck --check-prefix=OutputRelocs %s
+
+	.text
+	.abiversion 2
+	.globl	_start                    # -- Begin function _start
+	.p2align	4
+	.type	_start,@function
+_start:                                   # @_start
+.Lfunc_begin0:
+.Lfunc_gep0:
+	addis 2, 12, .TOC.-.Lfunc_gep0@ha
+	addi 2, 2, .TOC.-.Lfunc_gep0@l
+.Lfunc_lep0:
+	.localentry	_start, .Lfunc_lep0-.Lfunc_gep0
+# %bb.0:                                # %entry
+	mflr 0
+	std 31, -8(1)
+	std 0, 16(1)
+	stdu 1, -64(1)
+	mr 31, 1
+	std 30, 48(31)                  # 8-byte Folded Spill
+	li 3, 0
+	stw 3, 44(31)
+	addis 3, 2, a@got@tlsld@ha
+	addi 3, 3, a@got@tlsld@l
+	bl __tls_get_addr(a@tlsld)
+	nop
+	addis 3, 3, a@dtprel@ha
+	addi 3, 3, a@dtprel@l
+	lwz 30, 0(3)
+	extsw 3, 30
+	ld 30, 48(31)                   # 8-byte Folded Reload
+	addi 1, 1, 64
+	ld 0, 16(1)
+	ld 31, -8(1)
+	mtlr 0
+	blr
+	.long	0
+	.quad	0
+.Lfunc_end0:
+	.size	_start, .Lfunc_end0-.Lfunc_begin0
+                                        # -- End function
+.globl __tls_get_addr
+.type __tls_get_addr,@function
+__tls_get_addr:
+	.type	a,@object               # @a
+	.section	.tdata,"awT",@progbits
+	.p2align	2
+a:
+	.long	2                       # 0x2
+	.size	a, 4
+
+// Verify that the input has local-dynamic tls relocation types
+// InputRelocs:  Relocation section '.rela.text'
+// InputRelocs: R_PPC64_GOT_TLSLD16_HA  {{0+}}  a + 0
+// InputRelocs: R_PPC64_GOT_TLSLD16_LO  {{0+}}  a + 0
+// InputRelocs: R_PPC64_TLSLD           {{0+}}  a + 0
+
+// Verify that the local-dynamic sequence is relaxed to local exec.
+// Dis: _start:
+// Dis: nop
+// Dis: addis 3, 13, 0
+// Dis: nop
+// Dis: addi 3, 3, 4096
+
+// #ha(a@dtprel) --> (0x0 -0x8000 + 0x8000) >> 16 = 0
+// #lo(a@dtprel) --> (0x0 -0x8000) = -0x8000 = -32768
+// Dis: addis 3, 3, 0
+// Dis: addi 3, 3, -32768
+
+// Verify that no local-dynamic relocations exist for the dynamic linker.
+// OutputRelocs-NOT: R_PPC64_DTPMOD64
diff --git a/test/ELF/pr34660.s b/test/ELF/pr34660.s
index 7c78bbc..53998ad 100644
--- a/test/ELF/pr34660.s
+++ b/test/ELF/pr34660.s
@@ -3,7 +3,7 @@
 # RUN: llvm-mc -filetype=obj -triple=aarch64-linux-none %s -o %t.o
 # RUN: ld.lld --hash-style=sysv -shared %t.o -o %t
 # RUN: llvm-objdump %t -d | FileCheck %s --check-prefix=DISASM
-# RUN: llvm-readobj -elf-output-style=GNU %t -t | FileCheck %s --check-prefix=SYM
+# RUN: llvm-readelf %t -t | FileCheck %s --check-prefix=SYM
 
 # It would be much easier to understand/read this test if llvm-objdump would print
 # the immediates in hex.
diff --git a/test/ELF/pr34872.s b/test/ELF/pr34872.s
index e459877..c6ca819 100644
--- a/test/ELF/pr34872.s
+++ b/test/ELF/pr34872.s
@@ -1,8 +1,9 @@
+# REQUIRES: x86
 # RUN: llvm-mc %s -filetype=obj -triple=x86_64-pc-linux -o %t.o
 # RUN: llvm-mc %p/Inputs/undefined-error.s -filetype=obj \
 # RUN:    -triple=x86_64-pc-linux -o %t2.o
 # RUN: ld.lld -shared %t2.o -o %t2.so
-# RUN: not ld.lld %t2.so %t.o -o %t.out 2>&1 | FileCheck %s
+# RUN: not ld.lld %t2.so %t.o -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: undefined symbol: fmod
 # Check we're not emitting other diagnostics for this symbol.
diff --git a/test/ELF/pr36475.s b/test/ELF/pr36475.s
index 2ce240c..0228974 100644
--- a/test/ELF/pr36475.s
+++ b/test/ELF/pr36475.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 # RUN: echo "PHDRS {" > %t.script
 # RUN: echo " ph_text PT_LOAD FLAGS (0x1 | 0x4);" >> %t.script
diff --git a/test/ELF/pr37735.s b/test/ELF/pr37735.s
new file mode 100644
index 0000000..7e25d1b
--- /dev/null
+++ b/test/ELF/pr37735.s
@@ -0,0 +1,12 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=i386-pc-linux-gnu %s -o %t.o
+# RUN: ld.lld -r %t.o %t.o -o %t1.o
+# RUN: llvm-objdump -s -section=.bar %t1.o | FileCheck %s
+
+.section .foo
+	.byte 0
+
+# CHECK:      Contents of section .bar:
+# CHECK-NEXT:  0000 00000000 01000000
+.section .bar
+	.dc.a .foo
diff --git a/test/ELF/pre_init_fini_array.s b/test/ELF/pre_init_fini_array.s
index 1192fd0..cf716ab 100644
--- a/test/ELF/pre_init_fini_array.s
+++ b/test/ELF/pre_init_fini_array.s
@@ -1,10 +1,10 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/shared.s -o %t2
 // RUN: ld.lld %t2 -o %t2.so -shared
 // RUN: ld.lld %t %t2.so -o %t2
 // RUN: llvm-readobj -r -symbols -sections -dynamic-table %t2 | FileCheck %s
 // RUN: llvm-objdump -d %t2 | FileCheck --check-prefix=DISASM %s
-// REQUIRES: x86
 
 .globl _start
 _start:
diff --git a/test/ELF/pre_init_fini_array_missing.s b/test/ELF/pre_init_fini_array_missing.s
index 4a6b612..d5b9984 100644
--- a/test/ELF/pre_init_fini_array_missing.s
+++ b/test/ELF/pre_init_fini_array_missing.s
@@ -1,9 +1,9 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 // RUN: ld.lld %t -o %t2
 // RUN: llvm-objdump -d %t2 | FileCheck %s
 // RUN: ld.lld -pie %t -o %t3
 // RUN: llvm-objdump -d %t3 | FileCheck --check-prefix=PIE %s
-// REQUIRES: x86
 
 .globl _start
 _start:
diff --git a/test/ELF/program-header-layout.s b/test/ELF/program-header-layout.s
index 57759c9..949a96e 100644
--- a/test/ELF/program-header-layout.s
+++ b/test/ELF/program-header-layout.s
@@ -1,7 +1,7 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 # RUN: ld.lld %t -o %t2
 # RUN: llvm-readobj -sections -program-headers %t2 | FileCheck %s
-# REQUIRES: x86
 
 # Check that different output sections with the same flags are merged into a
 # single Read/Write PT_LOAD.
diff --git a/test/ELF/relative-dynamic-reloc-ppc64.s b/test/ELF/relative-dynamic-reloc-ppc64.s
index 99c2457..83190a2 100644
--- a/test/ELF/relative-dynamic-reloc-ppc64.s
+++ b/test/ELF/relative-dynamic-reloc-ppc64.s
@@ -1,3 +1,4 @@
+// REQUIRES: ppc
 // RUN: llvm-mc -filetype=obj -triple=powerpc64le-unknown-linux %s -o %t.o
 // RUN: ld.lld -shared %t.o -o %t.so
 // RUN: llvm-readobj -t -r -dyn-symbols %t.so | FileCheck %s
@@ -6,8 +7,6 @@
 // RUN: ld.lld -shared %t.o -o %t.so
 // RUN: llvm-readobj -t -r -dyn-symbols %t.so | FileCheck %s
 
-// REQUIRES: ppc
-
 // Test that we create R_PPC64_RELATIVE relocations but don't put any
 // symbols in the dynamic symbol table.
 
diff --git a/test/ELF/relocatable-many-sections.s b/test/ELF/relocatable-many-sections.s
new file mode 100644
index 0000000..347f3f7
--- /dev/null
+++ b/test/ELF/relocatable-many-sections.s
@@ -0,0 +1,109 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o %t.o
+# RUN: ld.lld -r %t.o -o %t
+
+## Check we are able to link against relocatable file produced.
+# RUN: ld.lld %t -o %t.out
+
+## Check we emit a valid ELF header when
+## sections amount is greater than SHN_LORESERVE.
+# RUN: llvm-readobj -file-headers %t | FileCheck %s --check-prefix=HDR
+# HDR:      ElfHeader {
+# HDR:        SectionHeaderCount: 0 (65543)
+# HDR-NEXT:   StringTableSectionIndex: 65535 (65541)
+
+## Check that:
+## 1) 65541 is the index of .shstrtab section.
+## 2) .symtab_shndxr is linked with .symtab.
+## 3) .symtab_shndxr entry size and alignment == 4.
+## 4) .symtab_shndxr has size equal to
+##    (sizeof(.symtab) / entsize(.symtab)) * entsize(.symtab_shndxr) = 0x4 * 0x180048 / 0x18 == 0x04000c
+# RUN: llvm-readelf -sections -symbols %t | FileCheck %s
+##              [Nr]    Name           Type                   Address          Off    Size   ES Flg  Lk    Inf    Al
+# CHECK:        [65538] .bar
+# CHECK-NEXT:   [65539] .symtab        SYMTAB                 0000000000000000 000040 180078 18      65542 65539  8
+# CHECK-NEXT:   [65540] .symtab_shndxr SYMTAB SECTION INDICES 0000000000000000 1800b8 040014 04      65539 0      4
+# CHECK-NEXT:   [65541] .shstrtab      STRTAB                 0000000000000000 1c00cc 0f0035 00      0     0      1
+# CHECK-NEXT:   [65542] .strtab        STRTAB                 0000000000000000 2b0101 00000c 00
+# 5) Check we are able to represent symbol foo with section (.bar) index  > 0xFF00 (SHN_LORESERVE).
+# CHECK: GLOBAL DEFAULT  65538 foo
+
+.macro gen_sections4 x
+  .section a\x
+  .section b\x
+  .section c\x
+  .section d\x
+.endm
+
+.macro gen_sections8 x
+  gen_sections4 a\x
+  gen_sections4 b\x
+.endm
+
+.macro gen_sections16 x
+  gen_sections8 a\x
+  gen_sections8 b\x
+.endm
+
+.macro gen_sections32 x
+  gen_sections16 a\x
+  gen_sections16 b\x
+.endm
+
+.macro gen_sections64 x
+  gen_sections32 a\x
+  gen_sections32 b\x
+.endm
+
+.macro gen_sections128 x
+  gen_sections64 a\x
+  gen_sections64 b\x
+.endm
+
+.macro gen_sections256 x
+  gen_sections128 a\x
+  gen_sections128 b\x
+.endm
+
+.macro gen_sections512 x
+  gen_sections256 a\x
+  gen_sections256 b\x
+.endm
+
+.macro gen_sections1024 x
+  gen_sections512 a\x
+  gen_sections512 b\x
+.endm
+
+.macro gen_sections2048 x
+  gen_sections1024 a\x
+  gen_sections1024 b\x
+.endm
+
+.macro gen_sections4096 x
+  gen_sections2048 a\x
+  gen_sections2048 b\x
+.endm
+
+.macro gen_sections8192 x
+  gen_sections4096 a\x
+  gen_sections4096 b\x
+.endm
+
+.macro gen_sections16384 x
+  gen_sections8192 a\x
+  gen_sections8192 b\x
+.endm
+
+gen_sections16384 a
+gen_sections16384 b
+gen_sections16384 c
+gen_sections16384 d
+
+.section .bar
+.global foo
+foo:
+
+.section .text, "ax"
+.global _start
+_start:
diff --git a/test/ELF/relocation-absolute.s b/test/ELF/relocation-absolute.s
index 20d54ec..b882987 100644
--- a/test/ELF/relocation-absolute.s
+++ b/test/ELF/relocation-absolute.s
@@ -1,8 +1,8 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/abs.s -o %tabs
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
 // RUN: ld.lld %tabs %t -o %tout
 // RUN: llvm-objdump -d %tout | FileCheck %s
-// REQUIRES: x86
 
 .global _start
 _start:
diff --git a/test/ELF/relocation-common.s b/test/ELF/relocation-common.s
index 28276bf..71b1ac0 100644
--- a/test/ELF/relocation-common.s
+++ b/test/ELF/relocation-common.s
@@ -1,7 +1,7 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
 // RUN: ld.lld %t -o %tout
 // RUN: llvm-objdump -t -d %tout | FileCheck %s
-// REQUIRES: x86
 
 .global _start
 _start:
diff --git a/test/ELF/relocation-dtrace.test b/test/ELF/relocation-dtrace.test
index ef2cc49..9007a26 100644
--- a/test/ELF/relocation-dtrace.test
+++ b/test/ELF/relocation-dtrace.test
@@ -1,5 +1,5 @@
 # RUN: yaml2obj %s -o %t.o
-# RUN: ld.lld -shared %t.o -o %t.so
+# RUN: ld.lld -shared %t.o -o /dev/null
 
 # Test that we can handle R_X86_64_NONE as produced by dtrace.
 
diff --git a/test/ELF/relocation-i686.s b/test/ELF/relocation-i686.s
index 3986357..fdec7ca 100644
--- a/test/ELF/relocation-i686.s
+++ b/test/ELF/relocation-i686.s
@@ -1,10 +1,10 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=i686-pc-linux %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %p/Inputs/shared.s -o %t2.o
 // RUN: ld.lld -shared %t2.o -o %t2.so
 // RUN: ld.lld --hash-style=sysv %t %t2.so -o %t2
 // RUN: llvm-readobj -s %t2 | FileCheck --check-prefix=ADDR %s
 // RUN: llvm-objdump -d %t2 | FileCheck %s
-// REQUIRES: x86
 
 .global _start
 _start:
diff --git a/test/ELF/relocation-local.s b/test/ELF/relocation-local.s
index 8173dac..383c5c8 100644
--- a/test/ELF/relocation-local.s
+++ b/test/ELF/relocation-local.s
@@ -1,8 +1,8 @@
+// REQUIRES: x86
 // Test that relocation of local symbols is working.
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
 // RUN: ld.lld %t -o %t2
 // RUN: llvm-objdump -s -d %t2 | FileCheck %s
-// REQUIRES: x86
 
 
 .global _start
diff --git a/test/ELF/relocation-nocopy.s b/test/ELF/relocation-nocopy.s
index 533277d..70e9933 100644
--- a/test/ELF/relocation-nocopy.s
+++ b/test/ELF/relocation-nocopy.s
@@ -2,7 +2,7 @@
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/relocation-copy.s -o %t2.o
 // RUN: ld.lld -shared %t2.o -o %t.so
-// RUN: not ld.lld -z nocopyreloc %t.o %t.so -o %t3 2>&1 | FileCheck %s
+// RUN: not ld.lld -z nocopyreloc %t.o %t.so -o /dev/null 2>&1 | FileCheck %s
 
 // CHECK: unresolvable relocation R_X86_64_32S against symbol 'x'
 // CHECK: unresolvable relocation R_X86_64_32S against symbol 'y'
diff --git a/test/ELF/relocation-none-aarch64.test b/test/ELF/relocation-none-aarch64.test
index dd67c8c..e77989a 100644
--- a/test/ELF/relocation-none-aarch64.test
+++ b/test/ELF/relocation-none-aarch64.test
@@ -1,7 +1,7 @@
 # REQUIRES: aarch64
 
 # RUN: yaml2obj %s -o %t.o
-# RUN: ld.lld %t.o -o %t.out
+# RUN: ld.lld %t.o -o /dev/null
 
 !ELF
 FileHeader:
diff --git a/test/ELF/relocation-none-i686.test b/test/ELF/relocation-none-i686.test
index d8eed8f..82dc4e6 100644
--- a/test/ELF/relocation-none-i686.test
+++ b/test/ELF/relocation-none-i686.test
@@ -1,5 +1,5 @@
 # RUN: yaml2obj %s -o %t.o
-# RUN: ld.lld %t.o -o %t.out
+# RUN: ld.lld %t.o -o /dev/null
 
 # Test that we can handle R_386_NONE.
 
diff --git a/test/ELF/relocation-past-merge-end.s b/test/ELF/relocation-past-merge-end.s
index d08bde7..a3e7b59 100644
--- a/test/ELF/relocation-past-merge-end.s
+++ b/test/ELF/relocation-past-merge-end.s
@@ -1,6 +1,6 @@
 // REQUIRES: x86
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
-// RUN: not ld.lld %t.o -o %t.so -shared 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.o -o /dev/null -shared 2>&1 | FileCheck %s
 // CHECK: relocation-past-merge-end.s.tmp.o:(.foo): entry is past the end of the section
 
 .data
diff --git a/test/ELF/relocation-relative-absolute.s b/test/ELF/relocation-relative-absolute.s
index 2a343fd..21f5002 100644
--- a/test/ELF/relocation-relative-absolute.s
+++ b/test/ELF/relocation-relative-absolute.s
@@ -2,7 +2,7 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %tinput1.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux \
 # RUN:   %S/Inputs/relocation-relative-absolute.s -o %tinput2.o
-# RUN: not ld.lld %tinput1.o %tinput2.o -o %t -pie 2>&1 | FileCheck %s
+# RUN: not ld.lld %tinput1.o %tinput2.o -o /dev/null -pie 2>&1 | FileCheck %s
 
 .globl _start
 _start:
diff --git a/test/ELF/relocation-shared.s b/test/ELF/relocation-shared.s
index 730395a..81537ce 100644
--- a/test/ELF/relocation-shared.s
+++ b/test/ELF/relocation-shared.s
@@ -8,7 +8,7 @@
 // CHECK-NEXT: Flags [
 // CHECK-NEXT:   SHF_ALLOC
 // CHECK-NEXT: ]
-// CHECK-NEXT: Address: 0x1E1
+// CHECK-NEXT: Address: 0x20D
 // CHECK-NEXT: Offset:
 // CHECK-NEXT: Size: 8
 // CHECK-NEXT: Link: 0
@@ -16,8 +16,8 @@
 // CHECK-NEXT: AddressAlignment: 1
 // CHECK-NEXT: EntrySize: 0
 // CHECK-NEXT: SectionData (
-// CHECK-NEXT:   0000: 1F0E0000 00000000
-//                     0x1000 - 0x1E1 = 0xE1F
+// CHECK-NEXT:   0000: F30D0000 00000000
+//                     0x1000 - 0x20D = 0xDF3
 // CHECK-NEXT: )
 
 // CHECK:      Name: .text
diff --git a/test/ELF/relocation-size-err.s b/test/ELF/relocation-size-err.s
index 763ad87..8783fe9 100644
--- a/test/ELF/relocation-size-err.s
+++ b/test/ELF/relocation-size-err.s
@@ -1,6 +1,6 @@
 // REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-// RUN: not ld.lld %t.o -o %t.so -shared 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.o -o /dev/null -shared 2>&1 | FileCheck %s
 
 // CHECK:  error: can't create dynamic relocation R_X86_64_SIZE64 against symbol: foo in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output
 
diff --git a/test/ELF/relocation-size-shared.s b/test/ELF/relocation-size-shared.s
index cea9e64..f60f092 100644
--- a/test/ELF/relocation-size-shared.s
+++ b/test/ELF/relocation-size-shared.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/relocation-size-shared.s -o %tso.o
 // RUN: ld.lld -shared %tso.o -o %tso
diff --git a/test/ELF/relocation-size.s b/test/ELF/relocation-size.s
index 419b8a1..525b1e1 100644
--- a/test/ELF/relocation-size.s
+++ b/test/ELF/relocation-size.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 // RUN: ld.lld %t.o -o %t1
 // RUN: llvm-readobj -r %t1 | FileCheck --check-prefix=NORELOC %s
diff --git a/test/ELF/relocation-undefined-weak.s b/test/ELF/relocation-undefined-weak.s
index 6aa84ec..7ea247f 100644
--- a/test/ELF/relocation-undefined-weak.s
+++ b/test/ELF/relocation-undefined-weak.s
@@ -1,8 +1,8 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
 // RUN: ld.lld %t -o %tout
 // RUN: llvm-readobj -sections %tout | FileCheck %s
 // RUN: llvm-objdump -d %tout | FileCheck %s --check-prefix DISASM
-// REQUIRES: x86
 
 // Check that undefined weak symbols are treated as having a VA of 0.
 
diff --git a/test/ELF/relocation.s b/test/ELF/relocation.s
index 8205728..00b75d2 100644
--- a/test/ELF/relocation.s
+++ b/test/ELF/relocation.s
@@ -1,10 +1,10 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/shared.s -o %t2
 // RUN: ld.lld %t2 -soname fixed-length-string.so -o %t2.so -shared
 // RUN: ld.lld --hash-style=sysv %t %t2.so -o %t3
 // RUN: llvm-readobj -s  %t3 | FileCheck --check-prefix=SEC %s
 // RUN: llvm-objdump -s -d %t3 | FileCheck %s
-// REQUIRES: x86
 
 // SEC:      Name: .plt
 // SEC-NEXT: Type: SHT_PROGBITS
@@ -113,16 +113,16 @@
  .quad R_X86_64_64
 
 // CHECK:      Contents of section .R_X86_64_64:
-// CHECK-NEXT:   20024d 4d022000 00000000
+// CHECK-NEXT:   2002c0 c0022000 00000000
 
 .section .R_X86_64_GOTPCREL,"a",@progbits
 .global R_X86_64_GOTPCREL
 R_X86_64_GOTPCREL:
  .long zed@gotpcrel
 
-// 0x2020F0(.got) - 0x200255(.R_X86_64_GOTPCREL) = 0x2e9b
+// 0x2030F0(.got) - 0x2002c8(.R_X86_64_GOTPCREL) = 0x2e28
 // CHECK:      Contents of section .R_X86_64_GOTPCREL
-// CHECK-NEXT:   200255 9b2e0000
+// CHECK-NEXT:   2002c8 282e0000
 
 .section .R_X86_64_GOT32,"a",@progbits
 .global R_X86_64_GOT32
diff --git a/test/ELF/relro-non-contiguous.s b/test/ELF/relro-non-contiguous.s
index ce66808..8fdf64a 100644
--- a/test/ELF/relro-non-contiguous.s
+++ b/test/ELF/relro-non-contiguous.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/shared.s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/copy-in-shared.s -o %t2.o
 // RUN: ld.lld -shared %t.o %t2.o -o %t.so
@@ -15,7 +16,6 @@
 // RUN: not ld.lld %t3.o %t.so -z relro -o %t --script=%t.script 2>&1 | FileCheck %s
 // No error when we do not request relro.
 // RUN: ld.lld %t3.o %t.so -z norelro -o %t --script=%t.script
-// REQUIRES: x86
 
 // CHECK: error: section: .bss.rel.ro is not contiguous with other relro sections
         .section .text, "ax", @progbits
diff --git a/test/ELF/relro-omagic.s b/test/ELF/relro-omagic.s
index 3eac082..ba0ca72 100644
--- a/test/ELF/relro-omagic.s
+++ b/test/ELF/relro-omagic.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/shared.s -o %t2.o
 # RUN: ld.lld -shared %t2.o -o %t2.so -soname relro-omagic.s.tmp2.so
@@ -9,12 +10,12 @@
 # NORELRO-NEXT: Idx Name          Size      Address          Type
 # NORELRO-NEXT:   0               00000000 0000000000000000
 # NORELRO-NEXT:   1 .dynsym       00000048 0000000000200120
-# NORELRO-NEXT:   2 .dynstr       00000021 0000000000200168
-# NORELRO-NEXT:   3 .hash         00000020 000000000020018c
+# NORELRO-NEXT:   2 .hash         00000020 0000000000200168
+# NORELRO-NEXT:   3 .dynstr       00000021 0000000000200188
 # NORELRO-NEXT:   4 .rela.dyn     00000018 00000000002001b0
 # NORELRO-NEXT:   5 .rela.plt     00000018 00000000002001c8
-# NORELRO-NEXT:   6 .text         0000000a 00000000002001e0 TEXT DATA
-# NORELRO-NEXT:   7 .plt          00000020 00000000002001f0 TEXT DATA
+# NORELRO-NEXT:   6 .text         0000000a 00000000002001e0 TEXT
+# NORELRO-NEXT:   7 .plt          00000020 00000000002001f0 TEXT
 # NORELRO-NEXT:   8 .data         00000008 0000000000200210 DATA
 # NORELRO-NEXT:   9 .foo          00000004 0000000000200218 DATA
 # NORELRO-NEXT:  10 .dynamic      000000f0 0000000000200220
diff --git a/test/ELF/relro-script.s b/test/ELF/relro-script.s
index f0dca67..c71c8e5 100644
--- a/test/ELF/relro-script.s
+++ b/test/ELF/relro-script.s
@@ -16,7 +16,7 @@
 // RUN: .got.plt : { *(.got.plt) } \
 // RUN: } " > %t.script
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t3.o
-// RUN: ld.lld %t3.o %t.so -o %t --script=%t.script --print-map | FileCheck %s
+// RUN: ld.lld %t3.o %t.so -o /dev/null --script=%t.script --print-map | FileCheck %s
 
 // CHECK: .data.rel.ro
 // CHECK-NEXT: <internal>:(.bss.rel.ro)
diff --git a/test/ELF/reproduce-error.s b/test/ELF/reproduce-error.s
index e2de8a4..3a99815 100644
--- a/test/ELF/reproduce-error.s
+++ b/test/ELF/reproduce-error.s
@@ -1,5 +1,5 @@
-# Extracting the tar archive can get over the path limit on windows.
 # REQUIRES: shell
+# Extracting the tar archive can get over the path limit on windows.
 
 # RUN: rm -rf %t.dir
 # RUN: mkdir -p %t.dir
diff --git a/test/ELF/reproduce.s b/test/ELF/reproduce.s
index 69671a0..bfab2e8 100644
--- a/test/ELF/reproduce.s
+++ b/test/ELF/reproduce.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
+# REQUIRES: shell
 
 # Extracting the tar archive can get over the path limit on windows.
-# REQUIRES: shell
 
 # RUN: rm -rf %t.dir
 # RUN: mkdir -p %t.dir/build1
diff --git a/test/ELF/resolution-end.s b/test/ELF/resolution-end.s
index 2685837..c8c5a04 100644
--- a/test/ELF/resolution-end.s
+++ b/test/ELF/resolution-end.s
@@ -1,9 +1,9 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t1.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/resolution-end.s -o %t2.o
 # RUN: ld.lld -shared -o %t2.so %t2.o
 # RUN: ld.lld --hash-style=sysv %t1.o %t2.so -o %t
 # RUN: llvm-readobj -t -s -section-data  %t | FileCheck %s
-# REQUIRES: x86
 
 # Test that we resolve _end to the this executable.
 
diff --git a/test/ELF/resolution-shared.s b/test/ELF/resolution-shared.s
index e1eac07..2d61d26 100644
--- a/test/ELF/resolution-shared.s
+++ b/test/ELF/resolution-shared.s
@@ -1,9 +1,9 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/resolution-shared.s -o %t2.o
 // RUN: ld.lld %t2.o -o %t2.so -shared
 // RUN: ld.lld %t.o %t2.so -o %t3 -shared
 // RUN: llvm-readobj -t %t3 | FileCheck %s
-// REQUIRES: x86
 
         .weak foo
 foo:
diff --git a/test/ELF/resolution.s b/test/ELF/resolution.s
index 4a42d94..f72f487 100644
--- a/test/ELF/resolution.s
+++ b/test/ELF/resolution.s
@@ -1,8 +1,8 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/resolution.s -o %t2
 // RUN: ld.lld -discard-all %t %t2 -o %t3
 // RUN: llvm-readobj -t %t3 | FileCheck %s
-// REQUIRES: x86
 
 // This is an exhaustive test for checking which symbol is kept when two
 // have the same name. Each symbol has a different size which is used
diff --git a/test/ELF/riscv-branch.test b/test/ELF/riscv-branch.test
new file mode 100644
index 0000000..3af9364
--- /dev/null
+++ b/test/ELF/riscv-branch.test
@@ -0,0 +1,119 @@
+# .option norelax
+# .global _start
+# _start:
+#     beq x0, x0, _start
+#
+# .section .reloc_max, "ax", @progbits
+# L1:
+#     beq x0, x0, L1 + 0xffe
+#
+# .section .reloc_min, "ax", @progbits
+# L2:
+#     beq x0, x0, L2 - 0x1000
+#
+# REQUIRES: riscv
+# RUN: yaml2obj %s -o %t.o
+# RUN: ld.lld %t.o -o %t
+# RUN: obj2yaml %t | FileCheck %s
+#
+# CHECK: - Name:            .text
+# CHECK:   Content:         '63000000'
+# 11000:       00000063                beqz    zero,11000 <_start>
+#
+# CHECK: - Name:            .reloc_max
+# CHECK:   Content:         E30F007E
+# 11004:       7e000fe3                beqz    zero,12002
+#
+# CHECK: - Name:            .reloc_min
+# CHECK:   Content:         '63000080'
+# 11008:       80000063                beqz    zero,10008
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS32
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_RISCV
+  Flags:           [ EF_RISCV_RVC, EF_RISCV_FLOAT_ABI_SOFT ]
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000002
+    Content:         '63000000'
+  - Name:            .rela.text
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    AddressAlign:    0x0000000000000004
+    Info:            .text
+    Relocations:
+      - Offset:          0x0000000000000000
+        Symbol:          _start
+        Type:            R_RISCV_BRANCH
+  - Name:            .data
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    AddressAlign:    0x0000000000000001
+    Content:         ''
+  - Name:            .bss
+    Type:            SHT_NOBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    AddressAlign:    0x0000000000000001
+  - Name:            .reloc_max
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000001
+    Content:         E30F007E
+  - Name:            .rela.reloc_max
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    AddressAlign:    0x0000000000000004
+    Info:            .reloc_max
+    Relocations:
+      - Offset:          0x0000000000000000
+        Symbol:          L1
+        Type:            R_RISCV_BRANCH
+        Addend:          4094
+  - Name:            .reloc_min
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000001
+    Content:         '63000080'
+  - Name:            .rela.reloc_min
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    AddressAlign:    0x0000000000000004
+    Info:            .reloc_min
+    Relocations:
+      - Offset:          0x0000000000000000
+        Symbol:          L2
+        Type:            R_RISCV_BRANCH
+        Addend:          -4096
+Symbols:
+  Local:
+    - Name:            .text
+      Type:            STT_SECTION
+      Section:         .text
+    - Name:            .data
+      Type:            STT_SECTION
+      Section:         .data
+    - Name:            .bss
+      Type:            STT_SECTION
+      Section:         .bss
+    - Name:            .reloc_max
+      Type:            STT_SECTION
+      Section:         .reloc_max
+    - Name:            L1
+      Section:         .reloc_max
+    - Name:            .reloc_min
+      Type:            STT_SECTION
+      Section:         .reloc_min
+    - Name:            L2
+      Section:         .reloc_min
+  Global:
+    - Name:            _start
+      Section:         .text
+...
diff --git a/test/ELF/riscv-call.test b/test/ELF/riscv-call.test
new file mode 100644
index 0000000..d8077d4
--- /dev/null
+++ b/test/ELF/riscv-call.test
@@ -0,0 +1,95 @@
+# .option norelax
+# .global _start
+# _start:
+#     call    _start + 4
+#
+# .section .reloc_neg, "ax", @progbits
+# L1:
+#     call    L1 - 4
+#
+# REQUIRES: riscv
+# RUN: yaml2obj %s -o %t.o
+# RUN: ld.lld %t.o -o %t
+# RUN: obj2yaml %t | FileCheck %s
+#
+# CHECK:  - Name:            .text
+# CHECK:    Content:         '97000000E7804000'
+#
+# 11000:       00000097                auipc   ra,0x0
+# 11004:       004080e7                jalr    4(ra)
+#
+# CHECK:  - Name:            .reloc_neg
+# CHECK:    Content:         97000000E780C0FF
+#
+# 11008:       00000097                auipc   ra,0x0
+# 1100c:       ffc080e7                jalr    -4(ra)
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS32
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_RISCV
+  Flags:           [ EF_RISCV_RVC, EF_RISCV_FLOAT_ABI_SOFT ]
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000002
+    Content:         '97000000E7800000'
+  - Name:            .rela.text
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    AddressAlign:    0x0000000000000004
+    Info:            .text
+    Relocations:
+      - Offset:          0x0000000000000000
+        Symbol:          _start
+        Type:            R_RISCV_CALL
+        Addend:          4
+  - Name:            .data
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    AddressAlign:    0x0000000000000001
+    Content:         ''
+  - Name:            .bss
+    Type:            SHT_NOBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    AddressAlign:    0x0000000000000001
+  - Name:            .reloc_neg
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000001
+    Content:         '97000000E7800000'
+  - Name:            .rela.reloc_neg
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    AddressAlign:    0x0000000000000004
+    Info:            .reloc_neg
+    Relocations:
+      - Offset:          0x0000000000000000
+        Symbol:          L1
+        Type:            R_RISCV_CALL
+        Addend:          -4
+Symbols:
+  Local:
+    - Name:            .text
+      Type:            STT_SECTION
+      Section:         .text
+    - Name:            .data
+      Type:            STT_SECTION
+      Section:         .data
+    - Name:            .bss
+      Type:            STT_SECTION
+      Section:         .bss
+    - Name:            .reloc_neg
+      Type:            STT_SECTION
+      Section:         .reloc_neg
+    - Name:            L1
+      Section:         .reloc_neg
+  Global:
+    - Name:            _start
+      Section:         .text
+...
diff --git a/test/ELF/riscv-hi20-lo12.test b/test/ELF/riscv-hi20-lo12.test
new file mode 100644
index 0000000..8d21d33
--- /dev/null
+++ b/test/ELF/riscv-hi20-lo12.test
@@ -0,0 +1,86 @@
+# .option norelax
+# .global _start
+#
+# .section .reloc_12345678, "ax", @progbits
+# _start:
+# foo = 0x12345678
+#     lui     a0, %hi(foo)
+#     addi    a0, a0, %lo(foo)
+#     lw      a0, %lo(foo)(a0)
+#
+# .section .reloc_fedcba98, "ax", @progbits
+# foo = 0xfedcba98
+#     lui     a0, %hi(foo)
+#     addi    a0, a0, %lo(foo)
+#
+# REQUIRES: riscv
+# RUN: yaml2obj %s -o %t.o
+# RUN: ld.lld %t.o -o %t
+# RUN: obj2yaml %t | FileCheck %s
+#
+# CHECK: - Name:            .reloc_12345678
+# CHECK:   Content:         '375534121305856703258567'
+# 11000:       12345537                lui     a0,0x12345
+# 11004:       67850513                addi    a0,a0,1656 # 12345678 <__global_pointer$+0x12332e78>
+# 11008:       67852503                lw      a0,1656(a0)
+#
+# CHECK: - Name:            .reloc_fedcba98
+# CHECK:   Content:         37C5DCFE130585A9
+# 1100c:       fedcc537                lui     a0,0xfedcc
+# 11010:       a9850513                addi    a0,a0,-1384 # fedcba98 <__global_pointer$+0xfedb9298>
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS32
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_RISCV
+  Flags:           [ EF_RISCV_RVC, EF_RISCV_FLOAT_ABI_SOFT ]
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000002
+    Content:         ''
+  - Name:            .data
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    AddressAlign:    0x0000000000000001
+    Content:         ''
+  - Name:            .bss
+    Type:            SHT_NOBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    AddressAlign:    0x0000000000000001
+  - Name:            .reloc_12345678
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000001
+    Content:         '375534121305856703258567'
+  - Name:            .reloc_fedcba98
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000001
+    Content:         37C5DCFE130585A9
+Symbols:
+  Local:
+    - Name:            .text
+      Type:            STT_SECTION
+      Section:         .text
+    - Name:            .data
+      Type:            STT_SECTION
+      Section:         .data
+    - Name:            .bss
+      Type:            STT_SECTION
+      Section:         .bss
+    - Name:            .reloc_12345678
+      Type:            STT_SECTION
+      Section:         .reloc_12345678
+    - Name:            foo
+      Value:           0x00000000FEDCBA98
+    - Name:            .reloc_fedcba98
+      Type:            STT_SECTION
+      Section:         .reloc_fedcba98
+  Global:
+    - Name:            _start
+      Section:         .reloc_12345678
+...
diff --git a/test/ELF/riscv-jal-error.test b/test/ELF/riscv-jal-error.test
new file mode 100644
index 0000000..9df95c9
--- /dev/null
+++ b/test/ELF/riscv-jal-error.test
@@ -0,0 +1,93 @@
+# .option norelax
+# .global _start
+#
+# _start:
+# L1:
+#     jal x0, L1 + 0x100000
+# L2:
+#     jal x0, L2 - 0x100002
+# L3:
+#     jal x0, L3 + 1
+# L4:
+#     c.jal L4 + 1
+#
+# REQUIRES: riscv
+# RUN: yaml2obj %s -o %t.o
+# RUN: not ld.lld %t.o -o %t 2>&1 | FileCheck %s
+#
+# CHECK: {{.*}}(.text+0x0): relocation R_RISCV_JAL out of range
+# CHECK: {{.*}}(.text+0x4): relocation R_RISCV_JAL out of range
+# CHECK: {{.*}}(.text+0x8): improper alignment for relocation R_RISCV_JAL
+# CHECK: {{.*}}(.text+0xC): improper alignment for relocation R_RISCV_RVC_JUMP
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS32
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_RISCV
+  Flags:           [ EF_RISCV_RVC, EF_RISCV_FLOAT_ABI_SOFT ]
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000002
+    Content:         6F0000806FF0FF7F6F0000000120
+  - Name:            .rela.text
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    AddressAlign:    0x0000000000000004
+    Info:            .text
+    Relocations:
+      - Offset:          0x0000000000000000
+        Symbol:          L1
+        Type:            R_RISCV_JAL
+        Addend:          1048576
+      - Offset:          0x0000000000000004
+        Symbol:          L2
+        Type:            R_RISCV_JAL
+        Addend:          -1048578
+      - Offset:          0x0000000000000008
+        Symbol:          L3
+        Type:            R_RISCV_JAL
+        Addend:          1
+      - Offset:          0x000000000000000C
+        Symbol:          L4
+        Type:            R_RISCV_RVC_JUMP
+        Addend:          1
+  - Name:            .data
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    AddressAlign:    0x0000000000000001
+    Content:         ''
+  - Name:            .bss
+    Type:            SHT_NOBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    AddressAlign:    0x0000000000000001
+Symbols:
+  Local:
+    - Name:            .text
+      Type:            STT_SECTION
+      Section:         .text
+    - Name:            .data
+      Type:            STT_SECTION
+      Section:         .data
+    - Name:            .bss
+      Type:            STT_SECTION
+      Section:         .bss
+    - Name:            L1
+      Section:         .text
+    - Name:            L2
+      Section:         .text
+      Value:           0x0000000000000004
+    - Name:            L3
+      Section:         .text
+      Value:           0x0000000000000008
+    - Name:            L4
+      Section:         .text
+      Value:           0x000000000000000C
+  Global:
+    - Name:            _start
+      Section:         .text
+...
diff --git a/test/ELF/riscv-jal.test b/test/ELF/riscv-jal.test
new file mode 100644
index 0000000..cb40dc6
--- /dev/null
+++ b/test/ELF/riscv-jal.test
@@ -0,0 +1,161 @@
+# .option norelax
+# .global _start
+#
+# .section .reloc_zero, "ax", @progbits
+# _start:
+# L1:
+#     jal x0, L1
+# L2:
+#     c.jal L2
+#
+# .section .reloc_max, "ax", @progbits
+# L3:
+#     jal x0, L3 + 0xffffe
+# L4:
+#     c.jal L4 + 0x7fe
+#
+# .section .reloc_min, "ax", @progbits
+# L5:
+#     jal x0, L5 - 0x100000
+# L6:
+#     c.jal L6 - 0x800
+#
+# REQUIRES: riscv
+# RUN: yaml2obj %s -o %t.o
+# RUN: ld.lld %t.o -o %t
+# RUN: obj2yaml %t | FileCheck %s
+#
+# CHECK: - Name:            .reloc_zero
+# CHECK:   Content:         6F0000000120
+# 11000:       0000006f                j       11000
+# 11004:       2001                    jal     11004
+#
+# CHECK: - Name:            .reloc_max
+# CHECK:   Content:         6FF0FF7FFD2F
+# 11006:       7ffff06f                j       111004
+# 1100a:       2ffd                    jal     11808
+#
+# CHECK: - Name:            .reloc_min
+# CHECK:   Content:         6F0000800130
+# 1100c:       8000006f                j       fff1100c
+# 11010:       3001                    jal     10810
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS32
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_RISCV
+  Flags:           [ EF_RISCV_RVC, EF_RISCV_FLOAT_ABI_SOFT ]
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000002
+    Content:         ''
+  - Name:            .data
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    AddressAlign:    0x0000000000000001
+    Content:         ''
+  - Name:            .bss
+    Type:            SHT_NOBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    AddressAlign:    0x0000000000000001
+  - Name:            .reloc_zero
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000001
+    Content:         6F0000000120
+  - Name:            .rela.reloc_zero
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    AddressAlign:    0x0000000000000004
+    Info:            .reloc_zero
+    Relocations:
+      - Offset:          0x0000000000000000
+        Symbol:          L1
+        Type:            R_RISCV_JAL
+      - Offset:          0x0000000000000004
+        Symbol:          L2
+        Type:            R_RISCV_RVC_JUMP
+  - Name:            .reloc_max
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000001
+    Content:         6FF0FF7FFD2F
+  - Name:            .rela.reloc_max
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    AddressAlign:    0x0000000000000004
+    Info:            .reloc_max
+    Relocations:
+      - Offset:          0x0000000000000000
+        Symbol:          L3
+        Type:            R_RISCV_JAL
+        Addend:          1048574
+      - Offset:          0x0000000000000004
+        Symbol:          L4
+        Type:            R_RISCV_RVC_JUMP
+        Addend:          2046
+  - Name:            .reloc_min
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000001
+    Content:         6F0000800130
+  - Name:            .rela.reloc_min
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    AddressAlign:    0x0000000000000004
+    Info:            .reloc_min
+    Relocations:
+      - Offset:          0x0000000000000000
+        Symbol:          L5
+        Type:            R_RISCV_JAL
+        Addend:          -1048576
+      - Offset:          0x0000000000000004
+        Symbol:          L6
+        Type:            R_RISCV_RVC_JUMP
+        Addend:          -2048
+Symbols:
+  Local:
+    - Name:            .text
+      Type:            STT_SECTION
+      Section:         .text
+    - Name:            .data
+      Type:            STT_SECTION
+      Section:         .data
+    - Name:            .bss
+      Type:            STT_SECTION
+      Section:         .bss
+    - Name:            .reloc_zero
+      Type:            STT_SECTION
+      Section:         .reloc_zero
+    - Name:            L1
+      Section:         .reloc_zero
+    - Name:            L2
+      Section:         .reloc_zero
+      Value:           0x0000000000000004
+    - Name:            .reloc_max
+      Type:            STT_SECTION
+      Section:         .reloc_max
+    - Name:            L3
+      Section:         .reloc_max
+    - Name:            L4
+      Section:         .reloc_max
+      Value:           0x0000000000000004
+    - Name:            .reloc_min
+      Type:            STT_SECTION
+      Section:         .reloc_min
+    - Name:            L5
+      Section:         .reloc_min
+    - Name:            L6
+      Section:         .reloc_min
+      Value:           0x0000000000000004
+  Global:
+    - Name:            _start
+      Section:         .reloc_zero
+...
diff --git a/test/ELF/riscv-pcrel-hilo.test b/test/ELF/riscv-pcrel-hilo.test
new file mode 100644
index 0000000..9767e14
--- /dev/null
+++ b/test/ELF/riscv-pcrel-hilo.test
@@ -0,0 +1,103 @@
+# .option norelax
+# .global _start
+#
+# _start:
+#     auipc   a0, %pcrel_hi(_start + 4)
+#     addi    a0, a0, %pcrel_lo(_start)
+#
+# .section .reloc_neg, "ax", @progbits
+# L1:
+#     auipc   a0, %pcrel_hi(L1 - 2)
+#     addi    a0, a0, %pcrel_lo(L1)
+#
+#
+# REQUIRES: riscv
+# RUN: yaml2obj %s -o %t.o
+# RUN: ld.lld %t.o -o %t
+# RUN: obj2yaml %t | FileCheck %s
+#
+# CHECK: - Name:            .text
+# CHECK:   Content:         '1705000013054500'
+# 11000:       00000517                auipc   a0,0x0
+# 11004:       00450513                addi    a0,a0,4 # 11004 <_start+0x4>
+#
+# CHECK: - Name:            .reloc_neg
+# CHECK:   Content:         170500001305E5FF
+# 11008:       00000517                auipc   a0,0x0
+# 1100c:       ffe50513                addi    a0,a0,-2 # 11006 <_start+0x6>
+
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS32
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_RISCV
+  Flags:           [ EF_RISCV_RVC, EF_RISCV_FLOAT_ABI_SOFT ]
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000002
+    Content:         '1705000013050500'
+  - Name:            .rela.text
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    AddressAlign:    0x0000000000000004
+    Info:            .text
+    Relocations:
+      - Offset:          0x0000000000000000
+        Symbol:          _start
+        Type:            R_RISCV_PCREL_HI20
+        Addend:          4
+      - Offset:          0x0000000000000004
+        Symbol:          _start
+        Type:            R_RISCV_PCREL_LO12_I
+  - Name:            .data
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    AddressAlign:    0x0000000000000001
+    Content:         ''
+  - Name:            .bss
+    Type:            SHT_NOBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    AddressAlign:    0x0000000000000001
+  - Name:            .reloc_neg
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000001
+    Content:         '1705000013050500'
+  - Name:            .rela.reloc_neg
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    AddressAlign:    0x0000000000000004
+    Info:            .reloc_neg
+    Relocations:
+      - Offset:          0x0000000000000000
+        Symbol:          L1
+        Type:            R_RISCV_PCREL_HI20
+        Addend:          -2
+      - Offset:          0x0000000000000004
+        Symbol:          L1
+        Type:            R_RISCV_PCREL_LO12_I
+Symbols:
+  Local:
+    - Name:            .text
+      Type:            STT_SECTION
+      Section:         .text
+    - Name:            .data
+      Type:            STT_SECTION
+      Section:         .data
+    - Name:            .bss
+      Type:            STT_SECTION
+      Section:         .bss
+    - Name:            .reloc_neg
+      Type:            STT_SECTION
+      Section:         .reloc_neg
+    - Name:            L1
+      Section:         .reloc_neg
+  Global:
+    - Name:            _start
+      Section:         .text
+...
diff --git a/test/ELF/rodynamic.s b/test/ELF/rodynamic.s
index 441e5c3..49d59ce 100644
--- a/test/ELF/rodynamic.s
+++ b/test/ELF/rodynamic.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
 # RUN: llvm-mc %p/Inputs/rodynamic.s -o %t.so.o -filetype=obj -triple=x86_64-pc-linux
 
diff --git a/test/ELF/section-align-0.test b/test/ELF/section-align-0.test
index 35783f5..8827ecf 100644
--- a/test/ELF/section-align-0.test
+++ b/test/ELF/section-align-0.test
@@ -1,5 +1,5 @@
 # RUN: yaml2obj %s -o %t
-# RUN: ld.lld %t -o %tout
+# RUN: ld.lld %t -o /dev/null
 
 # Verify that lld can handle sections with an alignment of zero.
 
diff --git a/test/ELF/section-layout.s b/test/ELF/section-layout.s
index 7febec8..0832109 100644
--- a/test/ELF/section-layout.s
+++ b/test/ELF/section-layout.s
@@ -1,7 +1,7 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 # RUN: ld.lld %t -o %tout
 # RUN: llvm-readobj -sections %tout | FileCheck %s
-# REQUIRES: x86
 
 # Check that sections are laid out in the correct order.
 
@@ -26,9 +26,10 @@
 .section e,"awT"
 .section d,"ax",@nobits
 .section c,"ax"
-.section b,"a",@nobits
-.section a,"a"
+.section a,"a",@nobits
+.section b,"a"
 
+// For non-executable and non-writable sections, PROGBITS appear after others.
 // CHECK: Name: a
 // CHECK: Name: b
 // CHECK: Name: c
diff --git a/test/ELF/section-metadata-err.s b/test/ELF/section-metadata-err.s
index ba102be..c910430 100644
--- a/test/ELF/section-metadata-err.s
+++ b/test/ELF/section-metadata-err.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-# RUN: not ld.lld %t.o -o %t 2>&1 | FileCheck %s
+# RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: error: a section .bar with SHF_LINK_ORDER should not refer a non-regular section: {{.*}}section-metadata-err.s.tmp.o:(.foo)
 
diff --git a/test/ELF/section-metadata-err2.s b/test/ELF/section-metadata-err2.s
index b2b611f..3191c1f 100644
--- a/test/ELF/section-metadata-err2.s
+++ b/test/ELF/section-metadata-err2.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-# RUN: not ld.lld %t.o -o %t 2>&1 | FileCheck %s
+# RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
 
 ## Check we do not crash and report proper errors.
 # CHECK: error: a section .bar with SHF_LINK_ORDER should not refer a non-regular section: {{.*}}section-metadata-err2.s.tmp.o:(.foo)
diff --git a/test/ELF/section-metadata-err3.s b/test/ELF/section-metadata-err3.s
index 36666bf..5c4875b 100644
--- a/test/ELF/section-metadata-err3.s
+++ b/test/ELF/section-metadata-err3.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-# RUN: not ld.lld %t.o -o %t 2>&1 | FileCheck %s
+# RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK:      error: incompatible section flags for .bar
 # CHECK-NEXT: >>> {{.*}}section-metadata-err3.s.tmp.o:(.bar): 0x2
diff --git a/test/ELF/section-name.s b/test/ELF/section-name.s
index caf574f..4f010c8 100644
--- a/test/ELF/section-name.s
+++ b/test/ELF/section-name.s
@@ -1,7 +1,7 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 # RUN: ld.lld %t -o %tout
 # RUN: llvm-objdump --section-headers  %tout | FileCheck %s
-# REQUIRES: x86
 
 .global _start
 .text
diff --git a/test/ELF/section-symbol.s b/test/ELF/section-symbol.s
index 5cf71ac..495c38d 100644
--- a/test/ELF/section-symbol.s
+++ b/test/ELF/section-symbol.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
 // RUN: ld.lld %t -o %t.so -shared -discard-none
 // RUN: llvm-readobj -t %t.so | FileCheck %s
diff --git a/test/ELF/section-symbols.test b/test/ELF/section-symbols.test
index 2ef77a8..973b110 100644
--- a/test/ELF/section-symbols.test
+++ b/test/ELF/section-symbols.test
@@ -1,5 +1,5 @@
 # RUN: yaml2obj %s -o %t
-# RUN: ld.lld -shared %t -o %tout
+# RUN: ld.lld -shared %t -o /dev/null
 
 # Verify that lld can handle STT_SECTION symbols associated
 # with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections.
diff --git a/test/ELF/sectionstart-noallochdr.s b/test/ELF/sectionstart-noallochdr.s
index e9267a5..be8e0c5 100644
--- a/test/ELF/sectionstart-noallochdr.s
+++ b/test/ELF/sectionstart-noallochdr.s
@@ -7,7 +7,7 @@
 # CHECK:      Sections:
 # CHECK-NEXT:  Idx Name          Size      Address          Type
 # CHECK-NEXT:    0               00000000 0000000000000000
-# CHECK-NEXT:    1 .text         00000001 0000000000000010 TEXT DATA
+# CHECK-NEXT:    1 .text         00000001 0000000000000010 TEXT
 # CHECK-NEXT:    2 .data         00000004 0000000000000020 DATA
 # CHECK-NEXT:    3 .bss          00000004 0000000000000030 BSS
 
diff --git a/test/ELF/sectionstart.s b/test/ELF/sectionstart.s
index 053d41d..be8b5f0 100644
--- a/test/ELF/sectionstart.s
+++ b/test/ELF/sectionstart.s
@@ -7,7 +7,7 @@
 # CHECK:      Sections:
 # CHECK-NEXT:  Idx Name          Size      Address          Type
 # CHECK-NEXT:    0               00000000 0000000000000000
-# CHECK-NEXT:    1 .text         00000001 0000000000100000 TEXT DATA
+# CHECK-NEXT:    1 .text         00000001 0000000000100000 TEXT
 # CHECK-NEXT:    2 .data         00000004 0000000000110000 DATA
 # CHECK-NEXT:    3 .bss          00000004 0000000000200000 BSS
 
@@ -35,11 +35,11 @@
 # RUN: llvm-objdump -section-headers %t4 | FileCheck %s
 
 ## Errors:
-# RUN: not ld.lld %t.o --section-start .text100000 -o %t2 2>&1 \
+# RUN: not ld.lld %t.o --section-start .text100000 -o /dev/null 2>&1 \
 # RUN:    | FileCheck -check-prefix=ERR1 %s
 # ERR1: invalid argument: --section-start .text100000
 
-# RUN: not ld.lld %t.o --section-start .text=1Q0000 -o %t3 2>&1 \
+# RUN: not ld.lld %t.o --section-start .text=1Q0000 -o /dev/null 2>&1 \
 # RUN:    | FileCheck -check-prefix=ERR2 %s
 # ERR2: invalid argument: --section-start .text=1Q0000
 
diff --git a/test/ELF/shared-lazy.s b/test/ELF/shared-lazy.s
index bc1e61c..36b8f16 100644
--- a/test/ELF/shared-lazy.s
+++ b/test/ELF/shared-lazy.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t1.o
 // RUN: rm -f %t1.a
 // RUN: llvm-ar rc %t1.a %t1.o
diff --git a/test/ELF/shared.s b/test/ELF/shared.s
index 6d4bcd1..3a8fede 100644
--- a/test/ELF/shared.s
+++ b/test/ELF/shared.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %p/Inputs/shared.s -o %t2.o
 // RUN: ld.lld --hash-style=sysv -shared %t2.o -o %t2.so
@@ -6,7 +7,6 @@
 // RUN: llvm-readobj --program-headers --dynamic-table -t -s -dyn-symbols -section-data -hash-table %t | FileCheck %s
 // RUN: ld.lld --hash-style=sysv %t.o %t2.so %t2.so -o %t2
 // RUN: llvm-readobj -dyn-symbols %t2 | FileCheck --check-prefix=DONT_EXPORT %s
-// REQUIRES: x86
 
 // Make sure .symtab is properly aligned.
 // SO:      Name: .symtab
@@ -59,24 +59,8 @@
 // CHECK-NEXT:     0030:
 // CHECK-NEXT:   )
 // CHECK-NEXT: }
-// CHECK:        Index: [[DYNSTR]]
-// CHECK-NEXT:   Name: .dynstr
-// CHECK-NEXT:   Type: SHT_STRTAB
-// CHECK-NEXT:   Flags [
-// CHECK-NEXT:     SHF_ALLOC
-// CHECK-NEXT:   ]
-// CHECK-NEXT:   Address: [[DYNSTRADDR:.*]]
-// CHECK-NEXT:   Offset:
-// CHECK-NEXT:   Size:
-// CHECK-NEXT:   Link: 0
-// CHECK-NEXT:   Info: 0
-// CHECK-NEXT:   AddressAlignment: 1
-// CHECK-NEXT:   EntrySize: 0
-// CHECK-NEXT:   SectionData (
-// CHECK:        )
-// CHECK-NEXT: }
 // CHECK-NEXT: Section {
-// CHECK-NEXT:   Index: 4
+// CHECK-NEXT:   Index: 3
 // CHECK-NEXT:    Name: .hash
 // CHECK-NEXT:    Type: SHT_HASH
 // CHECK-NEXT:    Flags [
@@ -89,6 +73,20 @@
 // CHECK-NEXT:    Info: 0
 // CHECK-NEXT:    AddressAlignment: 4
 // CHECK-NEXT:    EntrySize: 4
+// CHECK:      Section {
+// CHECK-NEXT:   Index: [[DYNSTR]]
+// CHECK-NEXT:   Name: .dynstr
+// CHECK-NEXT:   Type: SHT_STRTAB
+// CHECK-NEXT:   Flags [
+// CHECK-NEXT:     SHF_ALLOC
+// CHECK-NEXT:   ]
+// CHECK-NEXT:   Address: [[DYNSTRADDR:.*]]
+// CHECK-NEXT:   Offset:
+// CHECK-NEXT:   Size:
+// CHECK-NEXT:   Link: 0
+// CHECK-NEXT:   Info: 0
+// CHECK-NEXT:   AddressAlignment: 1
+// CHECK-NEXT:   EntrySize: 0
 
 // CHECK:      Name: .rel.dyn
 // CHECK-NEXT: Type: SHT_REL
diff --git a/test/ELF/sht-group-gold-r.test b/test/ELF/sht-group-gold-r.test
index 3a3889e..d1757ee 100644
--- a/test/ELF/sht-group-gold-r.test
+++ b/test/ELF/sht-group-gold-r.test
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # GNU gold 1.14 (the newest version as of July 2017) seems to create
 # non-standard-compliant SHT_GROUP sections when the -r option is given.
 #
diff --git a/test/ELF/soname.s b/test/ELF/soname.s
index a26bb30..25c969d 100644
--- a/test/ELF/soname.s
+++ b/test/ELF/soname.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: ld.lld %t.o -shared -soname=bar -o %t.so
 // RUN: ld.lld %t.o -shared --soname=bar -o %t2.so
diff --git a/test/ELF/soname2.s b/test/ELF/soname2.s
index 9fb8da5..67a9c24 100644
--- a/test/ELF/soname2.s
+++ b/test/ELF/soname2.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: ld.lld %t.o -shared -soname=foo.so -o %t
 // RUN: llvm-readobj --dynamic-table %t | FileCheck %s
diff --git a/test/ELF/sort-norosegment.s b/test/ELF/sort-norosegment.s
index 6d02eee..cd4fc9e 100644
--- a/test/ELF/sort-norosegment.s
+++ b/test/ELF/sort-norosegment.s
@@ -1,13 +1,13 @@
 # REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 
-# RUN: ld.lld --hash-style=sysv -no-rosegment -o %t1  %t -shared
-# RUN: llvm-readobj -elf-output-style=GNU -s %t1 | FileCheck %s
+# RUN: ld.lld --hash-style=sysv -no-rosegment -o %t %t.o -shared
+# RUN: llvm-readelf -s %t | FileCheck %s
 
 # CHECK:      .dynsym  {{.*}}   A
+# CHECK-NEXT: .hash    {{.*}}   A
 # CHECK-NEXT: .dynstr  {{.*}}   A
 # CHECK-NEXT: .text    {{.*}}   AX
-# CHECK-NEXT: .hash    {{.*}}   A
 # CHECK-NEXT: foo      {{.*}}  WA
 # CHECK-NEXT: .dynamic {{.*}}  WA
 
diff --git a/test/ELF/splitstacks.s b/test/ELF/splitstacks.s
deleted file mode 100644
index 3b7e67e..0000000
--- a/test/ELF/splitstacks.s
+++ /dev/null
@@ -1,11 +0,0 @@
-# REQUIRES: x86
-# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
-
-# RUN: not ld.lld %t1.o -o %t 2>&1 | FileCheck %s
-# CHECK: .o: object file compiled with -fsplit-stack is not supported
-
-.globl _start
-_start:
- nop
-
-.section .note.GNU-split-stack,"",@progbits
diff --git a/test/ELF/static-with-export-dynamic.s b/test/ELF/static-with-export-dynamic.s
index 8634835..f0c67df 100644
--- a/test/ELF/static-with-export-dynamic.s
+++ b/test/ELF/static-with-export-dynamic.s
@@ -1,7 +1,7 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=i686-unknown-cloudabi %s -o %t.o
 // RUN: ld.lld --export-dynamic %t.o -o %t
 // RUN: llvm-readobj -dyn-symbols %t | FileCheck %s
-// REQUIRES: x86
 
 // Ensure that a dynamic symbol table is present when --export-dynamic
 // is passed in, even when creating statically linked executables.
diff --git a/test/ELF/string-gc.s b/test/ELF/string-gc.s
index 96c75e9..8e4ee14 100644
--- a/test/ELF/string-gc.s
+++ b/test/ELF/string-gc.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: ld.lld %t.o -o %t --gc-sections
 // RUN: llvm-readobj -symbols %t | FileCheck %s
diff --git a/test/ELF/string-table.s b/test/ELF/string-table.s
index 490c4d5..2a97550 100644
--- a/test/ELF/string-table.s
+++ b/test/ELF/string-table.s
@@ -1,7 +1,7 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 // RUN: ld.lld %t -o %t2
 // RUN: llvm-readobj -sections %t2 | FileCheck %s
-// REQUIRES: x86
 
 .global _start
 _start:
diff --git a/test/ELF/symbol-override.s b/test/ELF/symbol-override.s
index 8b62d87..2c35733 100644
--- a/test/ELF/symbol-override.s
+++ b/test/ELF/symbol-override.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/symbol-override.s -o %t2.o
 // RUN: ld.lld -shared %t2.o -o %t2.so
diff --git a/test/ELF/symbols.s b/test/ELF/symbols.s
index 54dfcbd..e87b643 100644
--- a/test/ELF/symbols.s
+++ b/test/ELF/symbols.s
@@ -1,7 +1,7 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 // RUN: ld.lld %t -o %t2
 // RUN: llvm-readobj -symbols -sections %t2 | FileCheck %s
-// REQUIRES: x86
 
 .type _start, @function
 .globl _start
diff --git a/test/ELF/symver-archive.s b/test/ELF/symver-archive.s
index be50503..3e22dd2 100644
--- a/test/ELF/symver-archive.s
+++ b/test/ELF/symver-archive.s
@@ -4,7 +4,7 @@
 # RUN: llvm-ar rcs %t.a %t1
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/symver-archive1.s -o %t2.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/symver-archive2.s -o %t3.o
-# RUN: ld.lld -o %t.out %t2.o %t3.o %t.a
+# RUN: ld.lld -o /dev/null %t2.o %t3.o %t.a
 
 .text
 .globl x
diff --git a/test/ELF/synthetic-got.s b/test/ELF/synthetic-got.s
index 8bc7fc5..375a438 100644
--- a/test/ELF/synthetic-got.s
+++ b/test/ELF/synthetic-got.s
@@ -7,13 +7,13 @@
 # RUN:   | FileCheck %s --check-prefix=GOTDATA
 
 # GOT:     Sections:
-# GOT:      8  .got.plt     00000020 00000000000000d0 DATA
-# GOT:      10 .got         00000008 00000000000001c0 DATA
+# GOT:      8  .got.plt     00000020 00000000000000e0 DATA
+# GOT:      10 .got         00000008 00000000000001d0 DATA
 # GOTDATA:     Contents of section .got.plt:
-# GOTDATA-NEXT:  00d0 f0000000 00000000 00000000 00000000
-# GOTDATA-NEXT:  00e0 00000000 00000000 c6000000 00000000
+# GOTDATA-NEXT:  00e0 00010000 00000000 00000000 00000000
+# GOTDATA-NEXT:  00f0 00000000 00000000 d6000000 00000000
 # GOTDATA-NEXT: Contents of section .got:
-# GOTDATA-NEXT:  01c0 00000000 00000000
+# GOTDATA-NEXT:  01d0 00000000 00000000
 
 # RUN: echo "SECTIONS { .mygot : { *(.got) *(.got.plt) } }" > %t1.script
 # RUN: ld.lld --hash-style=sysv -shared %t.o -o %t1.out --script %t1.script
@@ -21,12 +21,12 @@
 # RUN: llvm-objdump -s -section=.mygot %t1.out | FileCheck %s --check-prefix=MYGOTDATA
 
 # MYGOT:     Sections:
-# MYGOT:      8  .mygot     00000028 00000000000000d0 DATA
+# MYGOT:      8  .mygot     00000028 00000000000000e0 DATA
 # MYGOT-NOT:  .got
 # MYGOT-NOT:  .got.plt
-# MYGOTDATA:      00d0 00000000 00000000 f8000000 00000000
-# MYGOTDATA-NEXT: 00e0 00000000 00000000 00000000 00000000
-# MYGOTDATA-NEXT: 00f0 c6000000 00000000
+# MYGOTDATA:      00e0 00000000 00000000 08010000 00000000
+# MYGOTDATA-NEXT: 00f0 00000000 00000000 00000000 00000000
+# MYGOTDATA-NEXT: 0100 d6000000 00000000
 
 mov bar@gotpcrel(%rip), %rax
 call foo@plt
diff --git a/test/ELF/sysroot.s b/test/ELF/sysroot.s
index 6b40c7d..e9d6862 100644
--- a/test/ELF/sysroot.s
+++ b/test/ELF/sysroot.s
@@ -1,10 +1,10 @@
+// REQUIRES: x86
 // RUN: mkdir -p %t/lib
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t/m.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
 // RUN:     %p/Inputs/libsearch-st.s -o %t/st.o
 // RUN: rm -f %t/lib/libls.a
 // RUN: llvm-ar rcs %t/lib/libls.a %t/st.o
-// REQUIRES: x86
 
 // Should not link because of undefined symbol _bar
 // RUN: not ld.lld -o %t/r %t/m.o 2>&1 \
diff --git a/test/ELF/text-section-prefix.s b/test/ELF/text-section-prefix.s
index b9e9a60..e39536d 100644
--- a/test/ELF/text-section-prefix.s
+++ b/test/ELF/text-section-prefix.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 # RUN: ld.lld -z keep-text-section-prefix %t -o %t2
 # RUN: llvm-readelf -l %t2 | FileCheck %s
diff --git a/test/ELF/tls-archive.s b/test/ELF/tls-archive.s
index 9a88fdd..640e68a 100644
--- a/test/ELF/tls-archive.s
+++ b/test/ELF/tls-archive.s
@@ -3,7 +3,7 @@
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/tls-mismatch.s -o %t2
 // RUN: rm -f %t.a
 // RUN: llvm-ar cru %t.a %t2
-// RUN: ld.lld %t.a %t -o %t3
+// RUN: ld.lld %t.a %t -o /dev/null
 
 .globl _start,tlsvar
 _start:
diff --git a/test/ELF/tls-error.s b/test/ELF/tls-error.s
index b617899..989a63e 100644
--- a/test/ELF/tls-error.s
+++ b/test/ELF/tls-error.s
@@ -1,6 +1,6 @@
 // REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
-// RUN: not ld.lld %t -o %tout 2>&1 | FileCheck %s
+// RUN: not ld.lld %t -o /dev/null 2>&1 | FileCheck %s
 // CHECK: R_X86_64_TPOFF32 out of range
 
 .global _start
diff --git a/test/ELF/tls-got.s b/test/ELF/tls-got.s
index a0261ee..bedaaeb 100644
--- a/test/ELF/tls-got.s
+++ b/test/ELF/tls-got.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/tls-got.s -o %t2.o
 // RUN: ld.lld -shared %t2.o -o %t2.so
diff --git a/test/ELF/tls-in-archive.s b/test/ELF/tls-in-archive.s
index 71f60e3..5a8791d 100644
--- a/test/ELF/tls-in-archive.s
+++ b/test/ELF/tls-in-archive.s
@@ -1,8 +1,9 @@
 // REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/tls-in-archive.s -o %t1.o
+// RUN: rm -f %t.a
 // RUN: llvm-ar cru %t.a %t1.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t2.o
-// RUN: ld.lld %t2.o %t.a -o %tout
+// RUN: ld.lld %t2.o %t.a -o /dev/null
 
         .globl  _start
 _start:
diff --git a/test/ELF/tls-mismatch.s b/test/ELF/tls-mismatch.s
index 21994d1..d7ce224 100644
--- a/test/ELF/tls-mismatch.s
+++ b/test/ELF/tls-mismatch.s
@@ -1,7 +1,7 @@
 // REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/tls-mismatch.s -o %t2
-// RUN: not ld.lld %t %t2 -o %t3 2>&1 | FileCheck %s
+// RUN: not ld.lld %t %t2 -o /dev/null 2>&1 | FileCheck %s
 
 // CHECK: TLS attribute mismatch: tlsvar
 // CHECK: >>> defined in
diff --git a/test/ELF/tls-opt-gdie.s b/test/ELF/tls-opt-gdie.s
index 6e85312..6d0eb97 100644
--- a/test/ELF/tls-opt-gdie.s
+++ b/test/ELF/tls-opt-gdie.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/tls-opt-gdie.s -o %tso.o
 // RUN: ld.lld -shared %tso.o -o %t.so
diff --git a/test/ELF/tls-opt-gdiele-i686.s b/test/ELF/tls-opt-gdiele-i686.s
index 2dc3731..b39f933 100644
--- a/test/ELF/tls-opt-gdiele-i686.s
+++ b/test/ELF/tls-opt-gdiele-i686.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=i686-pc-linux %p/Inputs/tls-opt-gdiele-i686.s -o %tso.o
 // RUN: llvm-mc -filetype=obj -triple=i686-pc-linux %s -o %t.o
 // RUN: ld.lld -shared %tso.o -o %tso
diff --git a/test/ELF/tls-opt-i686.s b/test/ELF/tls-opt-i686.s
index dec45b4..d8b1d0e 100644
--- a/test/ELF/tls-opt-i686.s
+++ b/test/ELF/tls-opt-i686.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=i686-pc-linux %s -o %t.o
 // RUN: ld.lld %t.o -o %t1
 // RUN: llvm-readobj -r %t1 | FileCheck --check-prefix=NORELOC %s
@@ -11,11 +12,11 @@
 // LD -> LE:
 // DISASM-NEXT: 11000: 65 a1 00 00 00 00 movl %gs:0, %eax
 // DISASM-NEXT: 11006: 90                nop
-// DISASM-NEXT: 11007: 8d 74 26 00       leal (%esi), %esi
+// DISASM-NEXT: 11007: 8d 74 26 00       leal (%esi,%eiz), %esi
 // DISASM-NEXT: 1100b: 8d 90 f8 ff ff ff leal -8(%eax), %edx
 // DISASM-NEXT: 11011: 65 a1 00 00 00 00 movl %gs:0, %eax
 // DISASM-NEXT: 11017: 90                nop
-// DISASM-NEXT: 11018: 8d 74 26 00       leal (%esi), %esi
+// DISASM-NEXT: 11018: 8d 74 26 00       leal (%esi,%eiz), %esi
 // DISASM-NEXT: 1101c: 8d 90 fc ff ff ff leal -4(%eax), %edx
 // IE -> LE:
 // 4294967288 == 0xFFFFFFF8
diff --git a/test/ELF/tls-opt-iele-i686-nopic.s b/test/ELF/tls-opt-iele-i686-nopic.s
index 02148b5..50655e3 100644
--- a/test/ELF/tls-opt-iele-i686-nopic.s
+++ b/test/ELF/tls-opt-iele-i686-nopic.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=i686-pc-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=i686-pc-linux %p/Inputs/tls-opt-iele-i686-nopic.s -o %tso.o
 // RUN: ld.lld -shared %tso.o -o %tso
diff --git a/test/ELF/tls-opt-local.s b/test/ELF/tls-opt-local.s
index 633c22b..e937d4d 100644
--- a/test/ELF/tls-opt-local.s
+++ b/test/ELF/tls-opt-local.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 // RUN: ld.lld %t.o -o %t1
 // RUN: llvm-readobj -r %t1 | FileCheck --check-prefix=NORELOC %s
diff --git a/test/ELF/tls-opt-no-plt.s b/test/ELF/tls-opt-no-plt.s
index 53655d0..c613886 100644
--- a/test/ELF/tls-opt-no-plt.s
+++ b/test/ELF/tls-opt-no-plt.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/tls-opt-gdie.s -o %t2.o
 // RUN: ld.lld %t2.o -o %t2.so -shared
diff --git a/test/ELF/tls-opt.s b/test/ELF/tls-opt.s
index 6835e06..856e82c 100644
--- a/test/ELF/tls-opt.s
+++ b/test/ELF/tls-opt.s
@@ -1,3 +1,4 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 // RUN: ld.lld %t.o -o %t1
 // RUN: llvm-readobj -r %t1 | FileCheck --check-prefix=NORELOC %s
diff --git a/test/ELF/tls-static.s b/test/ELF/tls-static.s
index 338d95c..3e1aead 100644
--- a/test/ELF/tls-static.s
+++ b/test/ELF/tls-static.s
@@ -1,10 +1,10 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/shared.s -o %tso
 // RUN: ld.lld -static %t -o %tout
 // RUN: ld.lld %t -o %tout
 // RUN: ld.lld -shared %tso -o %tshared
 // RUN: ld.lld -static %t %tshared -o %tout
-// REQUIRES: x86
 
 .global _start
 _start:
diff --git a/test/ELF/tls-weak-undef.s b/test/ELF/tls-weak-undef.s
index 7aa6ef1..1023aeb 100644
--- a/test/ELF/tls-weak-undef.s
+++ b/test/ELF/tls-weak-undef.s
@@ -4,6 +4,7 @@
 
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux \
 // RUN:   %p/Inputs/tls-in-archive.s -o %t1.o
+// RUN: rm -f %t.a
 // RUN: llvm-ar cru %t.a %t1.o
 // RUN: ld.lld %t.o %t.a -o %t
 
diff --git a/test/ELF/trace-ar.s b/test/ELF/trace-ar.s
index 1d178dc..0a71f7e 100644
--- a/test/ELF/trace-ar.s
+++ b/test/ELF/trace-ar.s
@@ -2,6 +2,7 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.foo.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/trace-ar1.s -o %t.obj1.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/trace-ar2.s -o %t.obj2.o
+# RUN: rm -f %t.boo.a
 # RUN: llvm-ar rcs %t.boo.a %t.obj1.o %t.obj2.o
 
 ## Check how -t works with achieves
diff --git a/test/ELF/trace-symbols.s b/test/ELF/trace-symbols.s
index 2018f31..63004b7 100644
--- a/test/ELF/trace-symbols.s
+++ b/test/ELF/trace-symbols.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # Test -y symbol and -trace-symbol=symbol
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
@@ -7,7 +8,9 @@
 # RUN: %p/Inputs/trace-symbols-foo-strong.s -o %t2
 # RUN: ld.lld -shared %t1 -o %t1.so
 # RUN: ld.lld -shared %t2 -o %t2.so
+# RUN: rm -f %t1.a
 # RUN: llvm-ar rcs %t1.a %t1
+# RUN: rm -f %t2.a
 # RUN: llvm-ar rcs %t2.a %t2
 
 # RUN: ld.lld -y foo -trace-symbol common -trace-symbol=hsymbol \
@@ -28,7 +31,7 @@
 # RUN: ld.lld -y foo -trace-symbol=common -trace-symbol=hsymbol \
 # RUN:   %t %t1 %t2 -o %t3 2>&1 | FileCheck -check-prefix=OBJECTD2FOO %s
 # RUN: ld.lld -y foo -y common --trace-symbol=hsymbol \
-# RUN:   %t %t2 %t1 -o %t4 2>&1 | FileCheck -check-prefix=OBJECTD2FOO %s
+# RUN:   %t %t2 %t1 -o /dev/null 2>&1 | FileCheck -check-prefix=OBJECTD2FOO %s
 # RUN: ld.lld -y foo -y common %t %t1.so %t2 -o %t3 2>&1 | \
 # RUN:   FileCheck -check-prefix=OBJECTD2FOO %s
 # OBJECTD2FOO: trace-symbols.s.tmp2: definition of foo
@@ -75,6 +78,9 @@
 # RUN:   FileCheck -check-prefix=STARTLIB %s
 # STARTLIB: trace-symbols.s.tmp1: reference to bar
 
+## Check we do not crash when trying to trace special symbol.
+# RUN: not ld.lld -trace-symbol=_end %t -o /dev/null
+
 .hidden hsymbol
 .globl	_start
 .type	_start, @function
diff --git a/test/ELF/typed-undef.s b/test/ELF/typed-undef.s
index d00e07f..879a809 100644
--- a/test/ELF/typed-undef.s
+++ b/test/ELF/typed-undef.s
@@ -3,7 +3,7 @@
 # We used to crash on this, check that we don't
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-# RUN: ld.lld %t.o -o %t -pie --unresolved-symbols=ignore-all
+# RUN: ld.lld %t.o -o /dev/null -pie --unresolved-symbols=ignore-all
 
         .global _start
 _start:
diff --git a/test/ELF/undef-broken-debug.test b/test/ELF/undef-broken-debug.test
index 4a61bce..b93d399 100644
--- a/test/ELF/undef-broken-debug.test
+++ b/test/ELF/undef-broken-debug.test
@@ -1,6 +1,6 @@
 # REQUIRES: x86
 # RUN: yaml2obj %s -o %t.o
-# RUN: not ld.lld %t.o -o %t.exe 2>&1 | FileCheck %s
+# RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
 
 # The debug info has a broken relocation. Check that we don't crash
 # and still report the undefined symbol.
diff --git a/test/ELF/undef-shared.s b/test/ELF/undef-shared.s
index bc38b60..701f70e 100644
--- a/test/ELF/undef-shared.s
+++ b/test/ELF/undef-shared.s
@@ -1,5 +1,6 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
-# RUN: not ld.lld %t.o -o %t.so -shared 2>&1 | FileCheck %s
+# RUN: not ld.lld %t.o -o /dev/null -shared 2>&1 | FileCheck %s
 
 # CHECK: error: undefined symbol: hidden
 # CHECK: >>> referenced by {{.*}}:(.data+0x0)
diff --git a/test/ELF/undef-shared2.s b/test/ELF/undef-shared2.s
index 3c53040..cae5ea4 100644
--- a/test/ELF/undef-shared2.s
+++ b/test/ELF/undef-shared2.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/undef-shared2.s -o %t2.o
 # RUN: not ld.lld %t.o %t2.o -o %t.so -shared 2>&1 | FileCheck %s
diff --git a/test/ELF/undef-start.s b/test/ELF/undef-start.s
index d0e0147..5c591bf 100644
--- a/test/ELF/undef-start.s
+++ b/test/ELF/undef-start.s
@@ -1,5 +1,5 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
-# RUN: ld.lld %t.o -o %t 2>&1 | FileCheck %s
+# RUN: ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: warning: cannot find entry symbol _start
diff --git a/test/ELF/undef-version-script.s b/test/ELF/undef-version-script.s
index 024ac1d..712589e 100644
--- a/test/ELF/undef-version-script.s
+++ b/test/ELF/undef-version-script.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 # RUN: echo "{ local: *; };" > %t.script
 # RUN: ld.lld --version-script %t.script -shared %t.o -o %t.so
diff --git a/test/ELF/undef.s b/test/ELF/undef.s
index 07e3b68..a511723 100644
--- a/test/ELF/undef.s
+++ b/test/ELF/undef.s
@@ -3,6 +3,7 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/undef.s -o %t2.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/undef-debug.s -o %t3.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/undef-bad-debug.s -o %t4.o
+# RUN: rm -f %t2.a
 # RUN: llvm-ar rc %t2.a %t2.o
 # RUN: not ld.lld %t.o %t2.a %t3.o %t4.o -o %t.exe 2>&1 | FileCheck %s
 # RUN: not ld.lld -pie %t.o %t2.a %t3.o %t4.o -o %t.exe 2>&1 | FileCheck %s
diff --git a/test/ELF/undefined-opt.s b/test/ELF/undefined-opt.s
index d8b793d..9e93e0f 100644
--- a/test/ELF/undefined-opt.s
+++ b/test/ELF/undefined-opt.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux \
 # RUN:     %p/Inputs/abs.s -o %tabs.o
@@ -5,7 +6,6 @@
 # RUN:     %p/Inputs/shared.s -o %tshared.o
 # RUN: rm -f %tar.a
 # RUN: llvm-ar rcs %tar.a %tabs.o %tshared.o
-# REQUIRES: x86
 
 # Symbols from the archive are not in if not needed
 # RUN: ld.lld -o %t1 %t.o %tar.a
diff --git a/test/ELF/unresolved-symbols.s b/test/ELF/unresolved-symbols.s
index 97ecd50..69da3e6 100644
--- a/test/ELF/unresolved-symbols.s
+++ b/test/ELF/unresolved-symbols.s
@@ -21,7 +21,7 @@
 ## case when --no-undefined specified.
 # RUN: ld.lld %t2.o -o %t1_1 --unresolved-symbols=ignore-all
 # RUN: llvm-readobj %t1_1 > /dev/null 2>&1
-# RUN: not ld.lld %t2.o -o %t1_2 --unresolved-symbols=ignore-all --no-undefined 2>&1 | \
+# RUN: not ld.lld %t2.o -o /dev/null --unresolved-symbols=ignore-all --no-undefined 2>&1 | \
 # RUN:   FileCheck -check-prefix=ERRUND %s
 # ERRUND: error: undefined symbol: undef
 # ERRUND: >>> referenced by {{.*}}:(.text+0x1)
@@ -34,11 +34,11 @@
 # RUN: ld.lld %t1.o %t2.o -o %t2 --unresolved-symbols=ignore-in-object-files
 # RUN: llvm-readobj %t2 > /dev/null 2>&1
 ## And still should not should produce for undefines from DSOs.
-# RUN: ld.lld %t1.o %t.so -o %t2_1 --unresolved-symbols=ignore-in-object-files
+# RUN: ld.lld %t1.o %t.so -o /dev/null --unresolved-symbols=ignore-in-object-files
 # RUN: llvm-readobj %t2 > /dev/null 2>&1
 
 ## Ignoring undefines in shared should produce error for symbol from object.
-# RUN: not ld.lld %t2.o -o %t3 --unresolved-symbols=ignore-in-shared-libs 2>&1 | \
+# RUN: not ld.lld %t2.o -o /dev/null --unresolved-symbols=ignore-in-shared-libs 2>&1 | \
 # RUN:   FileCheck -check-prefix=ERRUND %s
 ## And should not produce errors for symbols from DSO.
 # RUN: ld.lld %t1.o %t.so -o %t3_1 --unresolved-symbols=ignore-in-shared-libs
@@ -60,9 +60,9 @@
 # RUN: llvm-readobj %t6 > /dev/null 2>&1
 # RUN: ld.lld -shared %t1.o %t.so -o %t6_1
 # RUN: llvm-readobj %t6_1 > /dev/null 2>&1
-# RUN: not ld.lld %t2.o -o %t7 --unresolved-symbols=report-all 2>&1 | \
+# RUN: not ld.lld %t2.o -o /dev/null --unresolved-symbols=report-all 2>&1 | \
 # RUN:   FileCheck -check-prefix=ERRUND %s
-# RUN: not ld.lld %t2.o -o %t7_1 2>&1 | FileCheck -check-prefix=ERRUND %s
+# RUN: not ld.lld %t2.o -o /dev/null 2>&1 | FileCheck -check-prefix=ERRUND %s
 
 .globl _start
 _start:
diff --git a/test/ELF/user_def_init_array_start.s b/test/ELF/user_def_init_array_start.s
index 6c33166..a06dbd8 100644
--- a/test/ELF/user_def_init_array_start.s
+++ b/test/ELF/user_def_init_array_start.s
@@ -1,5 +1,6 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-// RUN: ld.lld %t.o -o %t2.so -shared
+// RUN: ld.lld %t.o -o /dev/null -shared
 // Allow user defined __init_array_start. This is used by musl because of the
 // the bfd linker not handling these properly. We always create them as
 // hidden, musl should not have problems with lld.
diff --git a/test/ELF/verdef-defaultver.s b/test/ELF/verdef-defaultver.s
index 4f01f17..c8444c4 100644
--- a/test/ELF/verdef-defaultver.s
+++ b/test/ELF/verdef-defaultver.s
@@ -55,8 +55,8 @@
 # DSO-NEXT:  ]
 # DSO-NEXT:  Version symbols {
 # DSO-NEXT:    Section Name: .gnu.version
-# DSO-NEXT:    Address: 0x256
-# DSO-NEXT:    Offset: 0x256
+# DSO-NEXT:    Address: 0x240
+# DSO-NEXT:    Offset: 0x240
 # DSO-NEXT:    Link: 1
 # DSO-NEXT:    Symbols [
 # DSO-NEXT:      Symbol {
@@ -150,8 +150,8 @@
 # EXE-NEXT:  ]
 # EXE-NEXT:  Version symbols {
 # EXE-NEXT:    Section Name: .gnu.version
-# EXE-NEXT:    Address: 0x20023C
-# EXE-NEXT:    Offset: 0x23C
+# EXE-NEXT:    Address: 0x200228
+# EXE-NEXT:    Offset: 0x228
 # EXE-NEXT:    Link: 1
 # EXE-NEXT:    Symbols [
 # EXE-NEXT:      Symbol {
diff --git a/test/ELF/verdef.s b/test/ELF/verdef.s
index af7c2ad..b5d12ee 100644
--- a/test/ELF/verdef.s
+++ b/test/ELF/verdef.s
@@ -8,8 +8,8 @@
 
 # DSO:        Version symbols {
 # DSO-NEXT:   Section Name: .gnu.version
-# DSO-NEXT:   Address: 0x260
-# DSO-NEXT:   Offset: 0x260
+# DSO-NEXT:   Address: 0x228
+# DSO-NEXT:   Offset: 0x228
 # DSO-NEXT:   Link: 1
 # DSO-NEXT:   Symbols [
 # DSO-NEXT:     Symbol {
@@ -70,8 +70,8 @@
 
 # MAIN:      Version symbols {
 # MAIN-NEXT:   Section Name: .gnu.version
-# MAIN-NEXT:   Address: 0x200260
-# MAIN-NEXT:   Offset: 0x260
+# MAIN-NEXT:   Address: 0x200228
+# MAIN-NEXT:   Offset: 0x228
 # MAIN-NEXT:   Link: 1
 # MAIN-NEXT:   Symbols [
 # MAIN-NEXT:     Symbol {
diff --git a/test/ELF/verneed-local.s b/test/ELF/verneed-local.s
index 208d8ec..d779a48 100644
--- a/test/ELF/verneed-local.s
+++ b/test/ELF/verneed-local.s
@@ -4,7 +4,7 @@
 # RUN: ld.lld -shared %t1.o --version-script %t.script -o %t.so
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-# RUN: not ld.lld %t.o %t.so -o %t 2>&1 | FileCheck %s
+# RUN: not ld.lld %t.o %t.so -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: error: undefined symbol: f3
 # CHECK: >>> referenced by {{.*}}:(.text+0x1)
diff --git a/test/ELF/verneed.s b/test/ELF/verneed.s
index 3c92336..6e87f04 100644
--- a/test/ELF/verneed.s
+++ b/test/ELF/verneed.s
@@ -19,20 +19,49 @@
 # CHECK-NEXT:     Address: 0x2001C8
 # CHECK-NEXT:     Offset: 0x1C8
 # CHECK-NEXT:     Size: 96
-# CHECK-NEXT:     Link: 2
+# CHECK-NEXT:     Link: [[DYNSTR:.*]]
 # CHECK-NEXT:     Info: 1
 # CHECK-NEXT:     AddressAlignment: 8
 # CHECK-NEXT:     EntrySize: 24
 
-# CHECK:        Section {
-# CHECK-NEXT:     Index: 2
+# CHECK:       Section {
+# CHECK-NEXT:    Index: 2
+# CHECK-NEXT:    Name: .gnu.version
+# CHECK-NEXT:    Type: SHT_GNU_versym (0x6FFFFFFF)
+# CHECK-NEXT:    Flags [ (0x2)
+# CHECK-NEXT:      SHF_ALLOC (0x2)
+# CHECK-NEXT:    ]
+# CHECK-NEXT:    Address: [[VERSYM:.*]]
+# CHECK-NEXT:    Offset: [[VERSYM_OFFSET:.*]]
+# CHECK-NEXT:    Size: 8
+# CHECK-NEXT:    Link: 1
+# CHECK-NEXT:    Info: 0
+# CHECK-NEXT:    AddressAlignment: 2
+# CHECK-NEXT:    EntrySize: 2
+
+# CHECK:       Section {
+# CHECK-NEXT:    Index: 3
+# CHECK-NEXT:    Name: .gnu.version_r
+# CHECK-NEXT:    Type: SHT_GNU_verneed (0x6FFFFFFE)
+# CHECK-NEXT:    Flags [ (0x2)
+# CHECK-NEXT:      SHF_ALLOC (0x2)
+# CHECK-NEXT:    ]
+# CHECK-NEXT:    Address: [[VERNEED:.*]]
+# CHECK-NEXT:    Offset: 0x230
+# CHECK-NEXT:    Size: 80
+# CHECK-NEXT:    Link: 5
+# CHECK-NEXT:    Info: 2
+# CHECK-NEXT:    AddressAlignment: 4
+# CHECK-NEXT:    EntrySize: 0
+
+# CHECK:          Index: [[DYNSTR]]
 # CHECK-NEXT:     Name: .dynstr
 # CHECK-NEXT:     Type: SHT_STRTAB (0x3)
 # CHECK-NEXT:     Flags [ (0x2)
 # CHECK-NEXT:       SHF_ALLOC (0x2)
 # CHECK-NEXT:     ]
-# CHECK-NEXT:     Address: 0x200228
-# CHECK-NEXT:     Offset: 0x228
+# CHECK-NEXT:     Address: 0x2002A8
+# CHECK-NEXT:     Offset: 0x2A8
 # CHECK-NEXT:     Size: 47
 # CHECK-NEXT:     Link: 0
 # CHECK-NEXT:     Info: 0
@@ -45,36 +74,6 @@
 # CHECK-NEXT:     )
 # CHECK-NEXT:   }
 
-# CHECK:       Section {
-# CHECK-NEXT:    Index: 3
-# CHECK-NEXT:    Name: .gnu.version
-# CHECK-NEXT:    Type: SHT_GNU_versym (0x6FFFFFFF)
-# CHECK-NEXT:    Flags [ (0x2)
-# CHECK-NEXT:      SHF_ALLOC (0x2)
-# CHECK-NEXT:    ]
-# CHECK-NEXT:    Address: 0x200258
-# CHECK-NEXT:    Offset: 0x258
-# CHECK-NEXT:    Size: 8
-# CHECK-NEXT:    Link: 1
-# CHECK-NEXT:    Info: 0
-# CHECK-NEXT:    AddressAlignment: 2
-# CHECK-NEXT:    EntrySize: 2
-
-# CHECK:       Section {
-# CHECK-NEXT:    Index: 4
-# CHECK-NEXT:    Name: .gnu.version_r
-# CHECK-NEXT:    Type: SHT_GNU_verneed (0x6FFFFFFE)
-# CHECK-NEXT:    Flags [ (0x2)
-# CHECK-NEXT:      SHF_ALLOC (0x2)
-# CHECK-NEXT:    ]
-# CHECK-NEXT:    Address: 0x200260
-# CHECK-NEXT:    Offset: 0x260
-# CHECK-NEXT:    Size: 80
-# CHECK-NEXT:    Link: 2
-# CHECK-NEXT:    Info: 2
-# CHECK-NEXT:    AddressAlignment: 4
-# CHECK-NEXT:    EntrySize: 0
-
 # CHECK:      DynamicSymbols [
 # CHECK-NEXT:   Symbol {
 # CHECK-NEXT:     Name: @
@@ -114,14 +113,14 @@
 # CHECK-NEXT:   }
 # CHECK-NEXT: ]
 
-# CHECK:      0x000000006FFFFFF0 VERSYM               0x200258
-# CHECK-NEXT: 0x000000006FFFFFFE VERNEED              0x200260
+# CHECK:      0x000000006FFFFFF0 VERSYM               [[VERSYM]]
+# CHECK-NEXT: 0x000000006FFFFFFE VERNEED              [[VERNEED]]
 # CHECK-NEXT: 0x000000006FFFFFFF VERNEEDNUM           2
 
 # CHECK:      Version symbols {
 # CHECK-NEXT:    Section Name: .gnu.version
-# CHECK-NEXT:    Address: 0x200258
-# CHECK-NEXT:    Offset: 0x258
+# CHECK-NEXT:    Address: [[VERSYM]]
+# CHECK-NEXT:    Offset: [[VERSYM_OFFSET]]
 # CHECK-NEXT:    Link: 1
 # CHECK-NEXT:    Symbols [
 # CHECK-NEXT:      Symbol {
diff --git a/test/ELF/version-exclude-libs.s b/test/ELF/version-exclude-libs.s
index 7335c23..a25a08f 100644
--- a/test/ELF/version-exclude-libs.s
+++ b/test/ELF/version-exclude-libs.s
@@ -1,5 +1,6 @@
 // REQUIRES: x86
 // RUN: llvm-mc %p/Inputs/versiondef.s -o %t.o -filetype=obj -triple=x86_64-pc-linux
+// RUN: rm -f %t.a
 // RUN: llvm-ar -r %t.a %t.o
 // RUN: llvm-mc %s -o %t2.o -filetype=obj -triple=x86_64-pc-linux
 // RUN: ld.lld %t2.o %t.a --shared --exclude-libs ALL -o %t.so
diff --git a/test/ELF/version-script-complex-wildcards.s b/test/ELF/version-script-complex-wildcards.s
index 61e1069..ce001d0 100644
--- a/test/ELF/version-script-complex-wildcards.s
+++ b/test/ELF/version-script-complex-wildcards.s
@@ -46,7 +46,7 @@
 # RUN: llvm-readobj -V %t8.so | FileCheck %s --check-prefix=ABBABC
 
 # RUN: echo "FOO { global: extern \"C++\" { a[; }; };" > %t9.script
-# RUN: not ld.lld --version-script %t9.script -shared %t.o -o %t9.so 2>&1 \
+# RUN: not ld.lld --version-script %t9.script -shared %t.o -o /dev/null 2>&1 \
 # RUN:   | FileCheck %s --check-prefix=ERROR
 # ERROR: invalid glob pattern: a[
 
diff --git a/test/ELF/version-script-extern-undefined.s b/test/ELF/version-script-extern-undefined.s
new file mode 100644
index 0000000..518b122
--- /dev/null
+++ b/test/ELF/version-script-extern-undefined.s
@@ -0,0 +1,19 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "FOO { global: extern \"C++\" { \"abb(int)\"; }; };" > %t.script
+# RUN: ld.lld --version-script %t.script -shared %t.o -o %t.so
+# RUN: llvm-readobj -V %t.so | FileCheck %s
+
+# CHECK:      Symbols [
+# CHECK-NEXT:   Symbol {
+# CHECK-NEXT:     Version: 0
+# CHECK-NEXT:     Name: @
+# CHECK-NEXT:   }
+# CHECK-NEXT:   Symbol {
+# CHECK-NEXT:     Version: 1
+# CHECK-NEXT:     Name: _Z3abbi@
+# CHECK-NEXT:   }
+# CHECK-NEXT: ]
+
+.globl _Z3abbi
diff --git a/test/ELF/version-script-extern.s b/test/ELF/version-script-extern.s
index 3125f62..16f4003 100644
--- a/test/ELF/version-script-extern.s
+++ b/test/ELF/version-script-extern.s
@@ -68,8 +68,8 @@
 # DSO-NEXT:  ]
 # DSO-NEXT:  Version symbols {
 # DSO-NEXT:    Section Name: .gnu.version
-# DSO-NEXT:    Address: 0x2BA
-# DSO-NEXT:    Offset: 0x2BA
+# DSO-NEXT:    Address: 0x258
+# DSO-NEXT:    Offset: 0x258
 # DSO-NEXT:    Link: 1
 # DSO-NEXT:    Symbols [
 # DSO-NEXT:      Symbol {
diff --git a/test/ELF/version-script-extern2.s b/test/ELF/version-script-extern2.s
new file mode 100644
index 0000000..834bbe1
--- /dev/null
+++ b/test/ELF/version-script-extern2.s
@@ -0,0 +1,22 @@
+# REQUIRES: x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "FOO { global: extern \"C++\" { \"bar\"; }; };" > %t.script
+# RUN: ld.lld --version-script %t.script -shared %t.o -o %t.so
+# RUN: llvm-readobj -V %t.so | FileCheck %s
+
+# CHECK:      Symbols [
+# CHECK-NEXT:   Symbol {
+# CHECK-NEXT:     Version: 0
+# CHECK-NEXT:     Name: @
+# CHECK-NEXT:   }
+# CHECK-NEXT:   Symbol {
+# CHECK-NEXT:     Version: 2
+# CHECK-NEXT:     Name: bar@@FOO
+# CHECK-NEXT:   }
+# CHECK-NEXT: ]
+
+.globl bar
+.type bar,@function
+bar:
+retq
diff --git a/test/ELF/version-script-glob.s b/test/ELF/version-script-glob.s
index 8149ead..d9ead05 100644
--- a/test/ELF/version-script-glob.s
+++ b/test/ELF/version-script-glob.s
@@ -48,7 +48,7 @@
 # CHECK-NEXT: ]
 
 # RUN: echo "{ global : local; local: *; };" > %t1.script
-# RUN: ld.lld -shared --version-script %t1.script %t.o -o %t1.so
+# RUN: ld.lld -shared --version-script %t1.script %t.o -o /dev/null
 
 # LOCAL:      DynamicSymbols [
 # LOCAL-NEXT:   Symbol {
diff --git a/test/ELF/version-script-in-search-path.s b/test/ELF/version-script-in-search-path.s
new file mode 100644
index 0000000..948f337
--- /dev/null
+++ b/test/ELF/version-script-in-search-path.s
@@ -0,0 +1,10 @@
+# REQUIRES: x86
+# Check that we fall back to search paths if a version script was not found
+# This behaviour matches ld.bfd.
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: mkdir -p %T/searchpath
+# RUN: echo '{};' > %T/searchpath/%basename_t.script
+# RUN: ld.lld -L%T/searchpath --version-script=%basename_t.script %t.o -o /dev/null
+# RUN: not ld.lld --version-script=%basename_t.script %t.o 2>&1 | FileCheck -check-prefix ERROR %s
+# ERROR: error: cannot find version script
diff --git a/test/ELF/version-script-missing.s b/test/ELF/version-script-missing.s
index ad4786e..a82a37e 100644
--- a/test/ELF/version-script-missing.s
+++ b/test/ELF/version-script-missing.s
@@ -4,4 +4,4 @@
 
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 # RUN: echo "{ foobar; };" > %t.script
-# RUN: ld.lld --version-script %t.script -shared %t.o -o %t.so
+# RUN: ld.lld --version-script %t.script -shared %t.o -o /dev/null
diff --git a/test/ELF/version-script-no-warn.s b/test/ELF/version-script-no-warn.s
index d5ff4b5..d99b87b 100644
--- a/test/ELF/version-script-no-warn.s
+++ b/test/ELF/version-script-no-warn.s
@@ -5,7 +5,7 @@
 # RUN: ld.lld -shared %t2.o -soname shared -o %t2.so
 
 # RUN: echo "foo { global: bar;  local: *; };" > %t.script
-# RUN: ld.lld --fatal-warnings --shared --version-script %t.script %t.o %t2.so -o %t.out
+# RUN: ld.lld --fatal-warnings --shared --version-script %t.script %t.o %t2.so -o /dev/null
 
 .global bar
 bar:
diff --git a/test/ELF/version-script-no-warn2.s b/test/ELF/version-script-no-warn2.s
index 52beff3..795fbb0 100644
--- a/test/ELF/version-script-no-warn2.s
+++ b/test/ELF/version-script-no-warn2.s
@@ -1,8 +1,9 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/version-script-no-warn2.s -o %t1.o
 # RUN: ld.lld %t1.o -o %t1.so -shared
 # RUN: echo "{ global: foo; local:  *; };" > %t.script
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t2.o
-# RUN: ld.lld -shared --version-script %t.script %t2.o %t1.so -o %t2.so --fatal-warnings
+# RUN: ld.lld -shared --version-script %t.script %t2.o %t1.so -o /dev/null --fatal-warnings
 
 .global	foo
 foo:
diff --git a/test/ELF/version-script-symver.s b/test/ELF/version-script-symver.s
index 0a4eddd..b635594 100644
--- a/test/ELF/version-script-symver.s
+++ b/test/ELF/version-script-symver.s
@@ -1,6 +1,6 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
-# RUN: ld.lld %t.o -o %t
+# RUN: ld.lld %t.o -o /dev/null
 
 .global _start
 .global bar
diff --git a/test/ELF/version-script.s b/test/ELF/version-script.s
index 36942c1..75083ac 100644
--- a/test/ELF/version-script.s
+++ b/test/ELF/version-script.s
@@ -34,7 +34,7 @@
 
 # RUN: echo "VERSION_1.0 { global: foo1; local: *; };" > %t6.script
 # RUN: echo "VERSION_2.0 { global: foo1; local: *; };" >> %t6.script
-# RUN: not ld.lld --version-script %t6.script -shared %t.o %t2.so -o %t6.so 2>&1 | \
+# RUN: not ld.lld --version-script %t6.script -shared %t.o %t2.so -o /dev/null 2>&1 | \
 # RUN:   FileCheck -check-prefix=ERR3 %s
 # ERR3: duplicate symbol 'foo1' in version script
 
@@ -214,7 +214,7 @@
 # ALL-NEXT: ]
 
 # RUN: echo "VERSION_1.0 { global: foo1; foo1; local: *; };" > %t8.script
-# RUN: ld.lld --version-script %t8.script -shared %t.o -o %t8.so --fatal-warnings
+# RUN: ld.lld --version-script %t8.script -shared %t.o -o /dev/null --fatal-warnings
 
 .globl foo1
 foo1:
diff --git a/test/ELF/version-symbol-error.s b/test/ELF/version-symbol-error.s
index fb83b35..f916fe7 100644
--- a/test/ELF/version-symbol-error.s
+++ b/test/ELF/version-symbol-error.s
@@ -1,7 +1,7 @@
 // REQUIRES: x86
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
 // RUN: echo "V1 {};" > %t.script
-// RUN: not ld.lld -shared -version-script=%t.script %t.o -o %t.so 2>&1 \
+// RUN: not ld.lld -shared -version-script=%t.script %t.o -o /dev/null 2>&1 \
 // RUN:   | FileCheck %s
 
 // CHECK: .o: symbol foo@V2 has undefined version V2
diff --git a/test/ELF/version-undef-sym.s b/test/ELF/version-undef-sym.s
index 20e92e6..13a6dc4 100644
--- a/test/ELF/version-undef-sym.s
+++ b/test/ELF/version-undef-sym.s
@@ -35,7 +35,7 @@
 // CHECK: Name: bar
 
 // But now we can successfully find bar.
-// RUN: ld.lld %t.o %p/Inputs/version-undef-sym.so -o %t.exe
+// RUN: ld.lld %t.o %p/Inputs/version-undef-sym.so -o /dev/null
 
         .global _start
 _start:
diff --git a/test/ELF/visibility.s b/test/ELF/visibility.s
index 7af29c9..0582d71 100644
--- a/test/ELF/visibility.s
+++ b/test/ELF/visibility.s
@@ -1,8 +1,8 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/visibility.s -o %t2
 // RUN: ld.lld -shared %t %t2 -o %t3
 // RUN: llvm-readobj -t -dyn-symbols %t3 | FileCheck %s
-// REQUIRES: x86
 
 // CHECK:      Symbols [
 // CHECK-NEXT:   Symbol {
diff --git a/test/ELF/warn-backrefs.s b/test/ELF/warn-backrefs.s
index d51e1f5..28937e1 100644
--- a/test/ELF/warn-backrefs.s
+++ b/test/ELF/warn-backrefs.s
@@ -37,11 +37,11 @@
 
 # RUN: echo ".globl bar; bar:" | llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %t3.o
 # RUN: echo ".globl foo; foo: call bar" | llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %t4.o
-# RUN: ld.lld --fatal-warnings --warn-backrefs %t1.o --start-lib %t3.o %t4.o --end-lib -o %t.exe
+# RUN: ld.lld --fatal-warnings --warn-backrefs %t1.o --start-lib %t3.o %t4.o --end-lib -o /dev/null
 
 # We don't report backward references to weak symbols as they can be overriden later.
 # RUN: echo ".weak foo; foo:" | llvm-mc -filetype=obj -triple=x86_64-unknown-linux - -o %t5.o
-# RUN: ld.lld --fatal-warnings --warn-backrefs --start-lib %t5.o --end-lib %t1.o %t2.o -o %t.exe
+# RUN: ld.lld --fatal-warnings --warn-backrefs --start-lib %t5.o --end-lib %t1.o %t2.o -o /dev/null
 
 .globl _start, foo
 _start:
diff --git a/test/ELF/warn-unresolved-symbols-hidden.s b/test/ELF/warn-unresolved-symbols-hidden.s
index 04691f9..9e3d9e1 100644
--- a/test/ELF/warn-unresolved-symbols-hidden.s
+++ b/test/ELF/warn-unresolved-symbols-hidden.s
@@ -1,6 +1,6 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-# RUN: not ld.lld -shared %t.o -o %t.so -z defs --warn-unresolved-symbols 2>&1| FileCheck %s
+# RUN: not ld.lld -shared %t.o -o /dev/null -z defs --warn-unresolved-symbols 2>&1| FileCheck %s
 
 # CHECK: warning: undefined symbol: foo
 # CHECK: error: undefined symbol: bar
diff --git a/test/ELF/warn-unresolved-symbols.s b/test/ELF/warn-unresolved-symbols.s
index 3342c6c..608b355 100644
--- a/test/ELF/warn-unresolved-symbols.s
+++ b/test/ELF/warn-unresolved-symbols.s
@@ -2,11 +2,11 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t1.o
 
 ## The link should fail with an undef error by default
-# RUN: not ld.lld %t1.o -o %t3 2>&1 | \
+# RUN: not ld.lld %t1.o -o /dev/null 2>&1 | \
 # RUN: FileCheck -check-prefix=ERRUND %s
 
 ## --error-unresolved-symbols should generate an error
-# RUN: not ld.lld %t1.o -o %t4 --error-unresolved-symbols 2>&1 | \
+# RUN: not ld.lld %t1.o -o /dev/null --error-unresolved-symbols 2>&1 | \
 # RUN: FileCheck -check-prefix=ERRUND %s
 
 ## --warn-unresolved-symbols should generate a warning
@@ -16,19 +16,19 @@
 ## Test that the last option wins
 # RUN: ld.lld %t1.o -o %t5 --error-unresolved-symbols --warn-unresolved-symbols 2>&1 | \
 # RUN:  FileCheck -check-prefix=WARNUND %s
-# RUN: not ld.lld %t1.o -o %t6 --warn-unresolved-symbols --error-unresolved-symbols 2>&1 | \
+# RUN: not ld.lld %t1.o -o /dev/null --warn-unresolved-symbols --error-unresolved-symbols 2>&1 | \
 # RUN:  FileCheck -check-prefix=ERRUND %s
 
 ## Do not report undefines if linking relocatable or shared.
 ## And while we're at it, check that we can accept single -
 ## variants of these options.
-# RUN: ld.lld -r %t1.o -o %t7 -error-unresolved-symbols 2>&1 | \
+# RUN: ld.lld -r %t1.o -o /dev/null -error-unresolved-symbols 2>&1 | \
 # RUN:  FileCheck -allow-empty -check-prefix=NOERR %s
-# RUN: ld.lld -shared %t1.o -o %t8.so --error-unresolved-symbols 2>&1 | \
+# RUN: ld.lld -shared %t1.o -o /dev/null --error-unresolved-symbols 2>&1 | \
 # RUN:  FileCheck -allow-empty -check-prefix=NOERR %s
-# RUN: ld.lld -r %t1.o -o %t9 -warn-unresolved-symbols 2>&1 | \
+# RUN: ld.lld -r %t1.o -o /dev/null -warn-unresolved-symbols 2>&1 | \
 # RUN:  FileCheck -allow-empty -check-prefix=NOWARN %s
-# RUN: ld.lld -shared %t1.o -o %t10.so --warn-unresolved-symbols 2>&1 | \
+# RUN: ld.lld -shared %t1.o -o /dev/null --warn-unresolved-symbols 2>&1 | \
 # RUN:  FileCheck -allow-empty -check-prefix=NOWARN %s
 
 # ERRUND: error: undefined symbol: undef
diff --git a/test/ELF/weak-undef-lazy.s b/test/ELF/weak-undef-lazy.s
index 113013e..0a4188f 100644
--- a/test/ELF/weak-undef-lazy.s
+++ b/test/ELF/weak-undef-lazy.s
@@ -3,7 +3,7 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/weak-undef-lazy.s -o %t2.o
 # RUN: rm -f %t2.a
 # RUN: llvm-ar rc %t2.a %t2.o
-# RUN: ld.lld %t.o %t2.a -o %t --export-dynamic
+# RUN: ld.lld %t.o %t2.a -o /dev/null --export-dynamic
 
         .global _start
 _start:
diff --git a/test/ELF/whole-archive-name.s b/test/ELF/whole-archive-name.s
index 7b7d7e8..1cf7962 100644
--- a/test/ELF/whole-archive-name.s
+++ b/test/ELF/whole-archive-name.s
@@ -3,7 +3,7 @@
 // RUN: mkdir -p %t.dir
 // RUN: rm -f %t.dir/liba.a
 // RUN: llvm-ar rcs %t.dir/liba.a %t.o
-// RUN: ld.lld -L%t.dir --whole-archive -la -o %t -Map=- | FileCheck %s
+// RUN: ld.lld -L%t.dir --whole-archive -la -o /dev/null -Map=- | FileCheck %s
 
 .globl _start
 _start:
diff --git a/test/ELF/wrap.s b/test/ELF/wrap.s
index b96917b..a02592e 100644
--- a/test/ELF/wrap.s
+++ b/test/ELF/wrap.s
@@ -6,6 +6,8 @@
 // RUN: llvm-objdump -d -print-imm-hex %t3 | FileCheck %s
 // RUN: ld.lld -o %t3 %t %t2 --wrap foo -wrap=nosuchsym
 // RUN: llvm-objdump -d -print-imm-hex %t3 | FileCheck %s
+// RUN: ld.lld -o %t3 %t %t2 --wrap foo --wrap foo -wrap=nosuchsym
+// RUN: llvm-objdump -d -print-imm-hex %t3 | FileCheck %s
 
 // CHECK: _start:
 // CHECK-NEXT: movl $0x11010, %edx
diff --git a/test/ELF/writable-merge.s b/test/ELF/writable-merge.s
index 3006fa3..91a7e07 100644
--- a/test/ELF/writable-merge.s
+++ b/test/ELF/writable-merge.s
@@ -1,6 +1,6 @@
 // REQUIRES: x86
 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
-// RUN: not ld.lld %t.o -o %t 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
 // CHECK: writable SHF_MERGE section is not supported
 
 .section .foo,"awM",@progbits,4
diff --git a/test/ELF/x86-64-dyn-rel-error2.s b/test/ELF/x86-64-dyn-rel-error2.s
index 9980b47..b325939 100644
--- a/test/ELF/x86-64-dyn-rel-error2.s
+++ b/test/ELF/x86-64-dyn-rel-error2.s
@@ -2,7 +2,7 @@
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/shared.s -o %t2.o
 // RUN: ld.lld %t2.o -shared -o %t2.so
-// RUN: not ld.lld -shared %t.o %t2.so -o %t 2>&1 | FileCheck %s
+// RUN: not ld.lld -shared %t.o %t2.so -o /dev/null 2>&1 | FileCheck %s
 
 // CHECK: relocation R_X86_64_PC32 cannot be used against symbol zed; recompile with -fPIC
 // CHECK: >>> defined in {{.*}}.so
diff --git a/test/ELF/x86-64-dyn-rel-error3.s b/test/ELF/x86-64-dyn-rel-error3.s
index 82a580d..86cef14 100644
--- a/test/ELF/x86-64-dyn-rel-error3.s
+++ b/test/ELF/x86-64-dyn-rel-error3.s
@@ -1,6 +1,6 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-# RUN: not ld.lld %t.o -shared -o %t.so 2>&1 | FileCheck %s
+# RUN: not ld.lld %t.o -shared -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: relocation R_X86_64_8 cannot be used against symbol foo; recompile with -fPIC
 # CHECK: relocation R_X86_64_16 cannot be used against symbol foo; recompile with -fPIC
diff --git a/test/ELF/x86-64-reloc-16.s b/test/ELF/x86-64-reloc-16.s
index 4822ec7..5157c37 100644
--- a/test/ELF/x86-64-reloc-16.s
+++ b/test/ELF/x86-64-reloc-16.s
@@ -3,12 +3,12 @@
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/x86-64-reloc-16.s -o %t1
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/x86-64-reloc-16-error.s -o %t2
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
-// RUN: ld.lld -shared %t %t1 -o %t3
+// RUN: ld.lld -shared %t %t1 -o /dev/null
 
 // CHECK:      Contents of section .text:
 // CHECK-NEXT:   200000 42
 
-// RUN: not ld.lld -shared %t %t2 -o %t4 2>&1 | FileCheck --check-prefix=ERROR %s
+// RUN: not ld.lld -shared %t %t2 -o /dev/null 2>&1 | FileCheck --check-prefix=ERROR %s
 // ERROR: relocation R_X86_64_16 out of range: 65536 is not in [0, 65535]
 
 .short foo
diff --git a/test/ELF/x86-64-reloc-32-fpic.s b/test/ELF/x86-64-reloc-32-fpic.s
index b5f11e5..1c4754f 100644
--- a/test/ELF/x86-64-reloc-32-fpic.s
+++ b/test/ELF/x86-64-reloc-32-fpic.s
@@ -1,6 +1,6 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
-# RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
+# RUN: not ld.lld -shared %t.o -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: relocation R_X86_64_32 cannot be used against symbol _shared; recompile with -fPIC
 # CHECK: >>> defined in {{.*}}
diff --git a/test/ELF/x86-64-reloc-8.s b/test/ELF/x86-64-reloc-8.s
index 8f6ba5a..f71bafb 100644
--- a/test/ELF/x86-64-reloc-8.s
+++ b/test/ELF/x86-64-reloc-8.s
@@ -3,12 +3,12 @@
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/x86-64-reloc-8.s -o %t1
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/x86-64-reloc-8-error.s -o %t2
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
-// RUN: ld.lld -shared %t %t1 -o %t3
+// RUN: ld.lld -shared %t %t1 -o /dev/null
 
 // CHECK:      Contents of section .text:
 // CHECK-NEXT:   200000 42
 
-// RUN: not ld.lld -shared %t %t2 -o %t4 2>&1 | FileCheck --check-prefix=ERROR %s
+// RUN: not ld.lld -shared %t %t2 -o /dev/null 2>&1 | FileCheck --check-prefix=ERROR %s
 // ERROR: relocation R_X86_64_8 out of range: 256 is not in [0, 255]
 
 .byte foo
diff --git a/test/ELF/x86-64-reloc-debug-overflow.s b/test/ELF/x86-64-reloc-debug-overflow.s
index 364c3bf..d6e6650 100644
--- a/test/ELF/x86-64-reloc-debug-overflow.s
+++ b/test/ELF/x86-64-reloc-debug-overflow.s
@@ -1,7 +1,7 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/x86-64-reloc-error.s -o %tabs
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
-# RUN: not ld.lld -shared %tabs %t -o %t2 2>&1 | FileCheck %s
+# RUN: not ld.lld -shared %tabs %t -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: (.debug_info+0x0): relocation R_X86_64_32 out of range: 281474976710656 is not in [0, 4294967295]; consider recompiling with -fdebug-types-section to reduce size of debug sections
 
diff --git a/test/ELF/x86-64-reloc-error-reporting.s b/test/ELF/x86-64-reloc-error-reporting.s
new file mode 100644
index 0000000..bb9c8be
--- /dev/null
+++ b/test/ELF/x86-64-reloc-error-reporting.s
@@ -0,0 +1,19 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/x86-64-reloc-error.s -o %tabs
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+
+// We have some code in error reporting to check that
+// section belongs to the output section. Without that
+// check, the linker would crash, so it is useful to test it.
+// And the easy way to do that is to trigger GC. That way .text.dumb
+// be collected and mentioned check will execute.
+
+// RUN: not ld.lld -gc-sections -shared %tabs %t -o /dev/null
+
+.section .text.dumb,"ax"
+ nop
+
+.section .text,"ax"
+.globl _start
+_start:
+  movl $big, %edx
diff --git a/test/ELF/x86-64-reloc-error.s b/test/ELF/x86-64-reloc-error.s
index cb600d9..0c3bebe 100644
--- a/test/ELF/x86-64-reloc-error.s
+++ b/test/ELF/x86-64-reloc-error.s
@@ -1,7 +1,7 @@
+// REQUIRES: x86
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/x86-64-reloc-error.s -o %tabs
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
-// RUN: not ld.lld -shared %tabs %t -o %t2 2>&1 | FileCheck %s
-// REQUIRES: x86
+// RUN: not ld.lld -shared %tabs %t -o /dev/null 2>&1 | FileCheck %s
 
   movl $big, %edx
   movq $foo - 0x1000000000000, %rdx
diff --git a/test/ELF/x86-64-reloc-error2.s b/test/ELF/x86-64-reloc-error2.s
new file mode 100644
index 0000000..d49b675
--- /dev/null
+++ b/test/ELF/x86-64-reloc-error2.s
@@ -0,0 +1,14 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: not ld.lld %t.o -o /dev/null 2>&1 | FileCheck %s
+
+## Check we are able to find a function symbol that encloses
+## a given location when reporting error messages.
+# CHECK: {{.*}}.o:(function func): relocation R_X86_64_32S out of range: -281474974609408 is not in [-2147483648, 2147483647]
+
+.section .text.func, "ax", %progbits
+.globl func
+.type func,@function
+.size func, 0x10
+func:
+ movq func - 0x1000000000000, %rdx
diff --git a/test/ELF/x86-64-reloc-gotoff64.s b/test/ELF/x86-64-reloc-gotoff64.s
new file mode 100644
index 0000000..697ac17
--- /dev/null
+++ b/test/ELF/x86-64-reloc-gotoff64.s
@@ -0,0 +1,32 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+// RUN: ld.lld %t.o -shared -o %t.so
+// RUN: llvm-readelf -s %t.so | FileCheck %s -check-prefix=SECTION
+// RUN: llvm-objdump -d %t.so | FileCheck %s
+
+// SECTION: .dynamic DYNAMIC 0000000000003000
+// SECTION: .got PROGBITS 0000000000003070 003070 000000
+
+// All the _GLOBAL_OFFSET_TABLE_ occurrences below refer to the address
+// of GOT base, not the address of the symbol _GLOBAL_OFFSET_TABLE_. These
+// instructions are special and produce GOT base relative relocations. We
+// currently use .got end as the GOT base, which is not equal to
+// the address of the special symbol _GLOBAL_OFFSET_TABLE_.
+
+// The assembly is generated by
+// gcc -O2 -S -mcmodel=medium -fPIC a.c
+// This computes the pc-relative address (runtime address) of _DYNAMIC.
+//
+// extern long _DYNAMIC[] __attribute__((visibility("hidden")));
+// long* dynamic() { return _DYNAMIC; }
+
+// 0x3070 (.got end) - 0x1007 = 8297
+// 0x3000 (_DYNAMIC) - 0x3070 (.got end) = -112
+// CHECK:      1000: {{.*}} leaq 8297(%rip), %rdx
+// CHECK-NEXT: 1007: {{.*}} movabsq $-112, %rax
+.global dynamic
+dynamic:
+  leaq _GLOBAL_OFFSET_TABLE_(%rip), %rdx
+  movabsq $_DYNAMIC@GOTOFF, %rax
+  addq %rdx, %rax
+  ret
diff --git a/test/ELF/x86-64-reloc-gotpc64.s b/test/ELF/x86-64-reloc-gotpc64.s
new file mode 100644
index 0000000..f07376f
--- /dev/null
+++ b/test/ELF/x86-64-reloc-gotpc64.s
@@ -0,0 +1,14 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+// RUN: ld.lld %t.o -shared -o %t.so
+// RUN: llvm-readelf -s %t.so | FileCheck %s -check-prefix=SECTION
+// RUN: llvm-objdump -d %t.so | FileCheck %s
+
+// SECTION: .got PROGBITS 0000000000003070 003070 000000
+
+// 0x3070 (.got end) - 0x1000 = 8304
+// CHECK: gotpc64:
+// CHECK-NEXT: 1000: {{.*}} movabsq $8304, %r11
+.global gotpc64
+gotpc64:
+  movabsq $_GLOBAL_OFFSET_TABLE_-., %r11
diff --git a/test/ELF/x86-64-reloc-pc32-fpic.s b/test/ELF/x86-64-reloc-pc32-fpic.s
index 0bf5e8f..2dfd1bf 100644
--- a/test/ELF/x86-64-reloc-pc32-fpic.s
+++ b/test/ELF/x86-64-reloc-pc32-fpic.s
@@ -1,6 +1,6 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
-# RUN: not ld.lld -shared %t.o -o %t.so 2>&1 | FileCheck %s
+# RUN: not ld.lld -shared %t.o -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: relocation R_X86_64_PC32 cannot be used against symbol _shared; recompile with -fPIC
 # CHECK: >>> defined in {{.*}}
diff --git a/test/ELF/x86-64-reloc-range-debug-loc.s b/test/ELF/x86-64-reloc-range-debug-loc.s
new file mode 100644
index 0000000..8be4df3
--- /dev/null
+++ b/test/ELF/x86-64-reloc-range-debug-loc.s
@@ -0,0 +1,36 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %S/Inputs/x86-64-reloc-error.s -o %tabs
+# RUN: llvm-mc %s -o %t.o -triple x86_64-pc-linux -filetype=obj
+# RUN: not ld.lld %tabs %t.o -o /dev/null -shared 2>&1 | FileCheck %s
+
+## Check we are able to report file and location from debug information
+## when reporting such kind of errors.
+# CHECK: error: test.s:3: relocation R_X86_64_32 out of range: 68719476736 is not in [0, 4294967295]
+
+.section .text,"ax",@progbits
+foo:
+.file 1 "test.s"
+.loc 1 3
+ movl $big, %edx
+
+.section .debug_abbrev,"",@progbits
+.byte 1                       # Abbreviation Code
+.byte 17                      # DW_TAG_compile_unit
+.byte 0                       # DW_CHILDREN_no
+.byte 16                      # DW_AT_stmt_list
+.byte 23                      # DW_FORM_sec_offset
+.byte 0                       # EOM(1)
+.byte 0                       # EOM(2)
+.byte 0                       # EOM(3)
+
+.section .debug_info,"",@progbits
+.long .Lend0 - .Lbegin0       # Length of Unit
+.Lbegin0:
+.short 4                       # DWARF version number
+.long .debug_abbrev           # Offset Into Abbrev. Section
+.byte 8                       # Address Size (in bytes)
+.byte 1                       # Abbrev [1] 0xb:0x1f DW_TAG_compile_unit
+.long .debug_line             # DW_AT_stmt_list
+.Lend0:
+
+.section .debug_line,"",@progbits
diff --git a/test/ELF/x86-64-reloc-range.s b/test/ELF/x86-64-reloc-range.s
index 2913458..c58a692 100644
--- a/test/ELF/x86-64-reloc-range.s
+++ b/test/ELF/x86-64-reloc-range.s
@@ -1,5 +1,6 @@
+// REQUIRES: x86
 // RUN: llvm-mc %s -o %t.o -triple x86_64-pc-linux -filetype=obj
-// RUN: not ld.lld %t.o -o %t.so -shared 2>&1 | FileCheck %s
+// RUN: not ld.lld %t.o -o /dev/null -shared 2>&1 | FileCheck %s
 
 // CHECK: {{.*}}:(.text+0x3): relocation R_X86_64_PC32 out of range: 2147483648 is not in [-2147483648, 2147483647]
 // CHECK-NOT: relocation
diff --git a/test/ELF/x86-64-reloc-tpoff32-fpic.s b/test/ELF/x86-64-reloc-tpoff32-fpic.s
index 7ae063b..edb04c1 100644
--- a/test/ELF/x86-64-reloc-tpoff32-fpic.s
+++ b/test/ELF/x86-64-reloc-tpoff32-fpic.s
@@ -1,6 +1,6 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-# RUN: not ld.lld %t.o -shared -o %t.so 2>&1 | FileCheck %s
+# RUN: not ld.lld %t.o -shared -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: relocation R_X86_64_TPOFF32 cannot be used against symbol var; recompile with -fPIC
 # CHECK: >>> defined in {{.*}}.o
diff --git a/test/ELF/x86-64-retpoline-linkerscript.s b/test/ELF/x86-64-retpoline-linkerscript.s
index 03c3ef2..82d2ca6 100644
--- a/test/ELF/x86-64-retpoline-linkerscript.s
+++ b/test/ELF/x86-64-retpoline-linkerscript.s
@@ -14,8 +14,8 @@
 
 // CHECK:      Disassembly of section .plt:
 // CHECK-NEXT: .plt:
-// CHECK-NEXT: 10:       ff 35 ea 00 00 00       pushq   234(%rip)
-// CHECK-NEXT: 16:       4c 8b 1d eb 00 00 00    movq    235(%rip), %r11
+// CHECK-NEXT: 10:       ff 35 72 00 00 00       pushq   114(%rip)
+// CHECK-NEXT: 16:       4c 8b 1d 73 00 00 00    movq    115(%rip), %r11
 // CHECK-NEXT: 1d:       e8 0e 00 00 00  callq   14 <.plt+0x20>
 // CHECK-NEXT: 22:       f3 90   pause
 // CHECK-NEXT: 24:       0f ae e8        lfence
@@ -40,7 +40,7 @@
 // CHECK-NEXT: 3d:       cc      int3
 // CHECK-NEXT: 3e:       cc      int3
 // CHECK-NEXT: 3f:       cc      int3
-// CHECK-NEXT: 40:       4c 8b 1d c9 00 00 00    movq    201(%rip), %r11
+// CHECK-NEXT: 40:       4c 8b 1d 51 00 00 00    movq    81(%rip), %r11
 // CHECK-NEXT: 47:       e8 e4 ff ff ff  callq   -28 <.plt+0x20>
 // CHECK-NEXT: 4c:       e9 d1 ff ff ff  jmp     -47 <.plt+0x12>
 // CHECK-NEXT: 51:       68 00 00 00 00  pushq   $0
@@ -50,7 +50,7 @@
 // CHECK-NEXT: 5d:       cc      int3
 // CHECK-NEXT: 5e:       cc      int3
 // CHECK-NEXT: 5f:       cc      int3
-// CHECK-NEXT: 60:       4c 8b 1d b1 00 00 00    movq    177(%rip), %r11
+// CHECK-NEXT: 60:       4c 8b 1d 39 00 00 00    movq    57(%rip), %r11
 // CHECK-NEXT: 67:       e8 c4 ff ff ff  callq   -60 <.plt+0x20>
 // CHECK-NEXT: 6c:       e9 b1 ff ff ff  jmp     -79 <.plt+0x12>
 // CHECK-NEXT: 71:       68 01 00 00 00  pushq   $1
diff --git a/test/ELF/x86-64-retpoline-znow-linkerscript.s b/test/ELF/x86-64-retpoline-znow-linkerscript.s
index e4eed84..2019605 100644
--- a/test/ELF/x86-64-retpoline-znow-linkerscript.s
+++ b/test/ELF/x86-64-retpoline-znow-linkerscript.s
@@ -35,13 +35,13 @@
 // CHECK-NEXT: 2d:	cc 	int3
 // CHECK-NEXT: 2e:	cc 	int3
 // CHECK-NEXT: 2f:	cc 	int3
-// CHECK-NEXT: 30:	4c 8b 1d a9 00 00 00 	movq	169(%rip), %r11
+// CHECK-NEXT: 30:	4c 8b 1d 31 00 00 00 	movq	49(%rip), %r11
 // CHECK-NEXT: 37:	e9 d4 ff ff ff 	jmp	-44 <.plt>
 // CHECK-NEXT: 3c:	cc 	int3
 // CHECK-NEXT: 3d:	cc 	int3
 // CHECK-NEXT: 3e:	cc 	int3
 // CHECK-NEXT: 3f:	cc 	int3
-// CHECK-NEXT: 40:      4c 8b 1d a1 00 00 00 	movq	161(%rip), %r11
+// CHECK-NEXT: 40:      4c 8b 1d 29 00 00 00 	movq	41(%rip), %r11
 // CHECK-NEXT: 47:	e9 c4 ff ff ff 	jmp	-60 <.plt>
 // CHECK-NEXT: 4c:	cc 	int3
 // CHECK-NEXT: 4d:	cc 	int3
diff --git a/test/ELF/x86-64-split-stack-prologue-adjust-fail.s b/test/ELF/x86-64-split-stack-prologue-adjust-fail.s
new file mode 100644
index 0000000..f3d0924
--- /dev/null
+++ b/test/ELF/x86-64-split-stack-prologue-adjust-fail.s
@@ -0,0 +1,35 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/x86-64-split-stack-extra.s -o %t2.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/x86-64-split-stack-main.s -o %t3.o
+
+# RUN: not ld.lld --defsym __morestack=0x100 --defsym __more_stack_nonsplit=0x200 %t1.o %t2.o %t3.o -o %t 2>&1 | FileCheck %s
+
+# An unknown prologue gives a match failure
+# CHECK: error: {{.*}}.o:(.text): unknown_prologue (with -fsplit-stack) calls non_split (without -fsplit-stack), but couldn't adjust its prologue
+
+# RUN: not ld.lld -r --defsym __morestack=0x100 %t1.o %t2.o -o %t 2>&1 | FileCheck %s -check-prefix=RELOCATABLE
+# RELOCATABLE: Cannot mix split-stack and non-split-stack in a relocatable link
+
+# RUN: not ld.lld --defsym __morestack=0x100 --defsym _start=0x300 %t1.o %t2.o %t3.o -o %t 2>&1 | FileCheck %s -check-prefix=ERROR
+# ERROR: Mixing split-stack objects requires a definition of __morestack_non_split
+
+	.text
+
+	.global	unknown_prologue
+	.type	unknown_prologue,@function
+unknown_prologue:
+	push	%rbp
+	mov	%rsp,%rbp
+	cmp	%fs:0x70,%rsp
+	jae	1f
+	callq	__morestack
+	retq
+1:
+	callq	non_split
+	leaveq
+	retq
+
+	.size	unknown_prologue,. - unknown_prologue
+
+	.section	.note.GNU-split-stack,"",@progbits
diff --git a/test/ELF/x86-64-split-stack-prologue-adjust-silent.s b/test/ELF/x86-64-split-stack-prologue-adjust-silent.s
new file mode 100644
index 0000000..0c82943
--- /dev/null
+++ b/test/ELF/x86-64-split-stack-prologue-adjust-silent.s
@@ -0,0 +1,32 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/x86-64-split-stack-main.s -o %t2.o
+
+# RUN: ld.lld --defsym __morestack=0x100 --defsym __morestack_non_split=0x200 %t1.o %t2.o -o %t
+# RUN: llvm-objdump -d %t 2>&1 | FileCheck %s
+
+# An unknown prologue ordinarily gives a match failure, except that this
+# object file includes a .note.GNU-no-split-stack section, which tells the
+# linker to expect such prologues, and therefore not error.
+
+# CHECK: __morestack
+
+	.text
+
+	.global	unknown_prologue
+	.type	unknown_prologue,@function
+unknown_prologue:
+	push %rbp
+	mov %rsp,%rbp
+	cmp %fs:0x70,%rsp
+	jae 1f
+	callq __morestack
+	retq
+1:
+	callq non_split
+	leaveq
+	retq
+
+	.size unknown_prologue,. - unknown_prologue
+	.section .note.GNU-split-stack,"",@progbits
+	.section .note.GNU-no-split-stack,"",@progbits
diff --git a/test/ELF/x86-64-split-stack-prologue-adjust-success.s b/test/ELF/x86-64-split-stack-prologue-adjust-success.s
new file mode 100644
index 0000000..fb3493f
--- /dev/null
+++ b/test/ELF/x86-64-split-stack-prologue-adjust-success.s
@@ -0,0 +1,115 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/x86-64-split-stack-extra.s -o %t2.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/x86-64-split-stack-main.s -o %t3.o
+
+# RUN: ld.lld --defsym __morestack=0x100 --defsym __morestack_non_split=0x200 %t1.o %t2.o %t3.o -o %t -z notext
+# RUN: llvm-objdump -d %t | FileCheck %s
+
+# Avoid duplicating the prologue for every test via macros.
+
+.macro prologue1 function_to_call
+	.global	prologue1_calls_\function_to_call
+	.type	prologue1_calls_\function_to_call,@function
+prologue1_calls_\function_to_call:
+	cmp %fs:0x70,%rsp
+	jae 1f
+	callq __morestack
+	retq
+1:
+	# Various and duplicate calls to ensure every code path is taken.
+	callq \function_to_call
+	callq \function_to_call
+	callq 1b
+	callq non_function_text_symbol
+	retq
+	.size	prologue1_calls_\function_to_call,. - prologue1_calls_\function_to_call
+.endm
+
+.macro prologue2 function_to_call register compare_amount
+	.global	prologue2_calls_\function_to_call\register
+	.type	prologue2_calls_\function_to_call\register,@function
+prologue2_calls_\function_to_call\register:
+	lea	-\compare_amount(%rsp),%\register
+	cmp	%fs:0x70,%\register
+	jae	1f
+	callq	__morestack
+	retq
+1:
+	# Various and duplicate calls to ensure every code path is taken.
+	callq	\function_to_call
+	callq	\function_to_call
+	callq 1b
+	callq non_function_text_symbol
+	retq
+	.size	prologue2_calls_\function_to_call\register,. - prologue2_calls_\function_to_call\register
+.endm
+
+	.local foo
+foo:
+	.section .text,"ax",@progbits
+	.quad foo
+
+	.text
+
+# For split-stack code calling split-stack code, ensure prologue v1 still
+# calls plain __morestack, and that any raw bytes written to the prologue
+# make sense.
+# CHECK: prologue1_calls_split:
+# CHECK-NEXT: cmp{{.*}}%fs:{{[^,]*}},{{.*}}%rsp
+# CHECK: jae{{.*$}}
+# CHECK-NEXT: callq{{.*}}<__morestack>
+
+prologue1 split
+
+# For split-stack code calling split-stack code, ensure prologue v2 still
+# calls plain __morestack, that any raw bytes written to the prologue
+# make sense, and that the register number is preserved.
+# CHECK: prologue2_calls_splitr10:
+# CHECK-NEXT: lea{{.*}} -512(%rsp),{{.*}}%r10
+# CHECK: cmp{{.*}}%fs:{{[^,]*}},{{.*}}%r{{[0-9]+}}
+# CHECK: jae{{.*}}
+# CHECK-NEXT: callq{{.*}}<__morestack>
+
+prologue2 split r10 0x200
+
+# CHECK: prologue2_calls_splitr11:
+# CHECK-NEXT: lea{{.*}} -256(%rsp),{{.*}}%r11
+# CHECK: cmp{{.*}}%fs:{{[^,]*}},{{.*}}%r{{[0-9]+}}
+# CHECK: jae{{.*}}
+# CHECK-NEXT: callq{{.*}}<__morestack>
+
+prologue2 split r11 0x100
+
+# For split-stack code calling non-split-stack code, ensure prologue v1
+# calls __morestack_non_split, and that any raw bytes written to the prologue
+# make sense.
+# CHECK: prologue1_calls_non_split:
+# CHECK-NEXT: stc{{.*$}}
+# CHECK-NEXT: nopl{{.*$}}
+# CHECK: jae{{.*$}}
+# CHECK-NEXT: callq{{.*}}<__morestack_non_split>
+
+prologue1 non_split
+
+# For split-stack code calling non-split-stack code, ensure prologue v2
+# calls __morestack_non_split, that any raw bytes written to the prologue
+# make sense, and that the register number is preserved
+# CHECK: prologue2_calls_non_splitr10:
+# CHECK-NEXT: lea{{.*}} -16640(%rsp),{{.*}}%r10
+# CHECK: cmp{{.*}}%fs:{{[^,]*}},{{.*}}%r10
+# CHECK: jae{{.*$}}
+# CHECK-NEXT: callq{{.*}}<__morestack_non_split>
+
+prologue2 non_split r10 0x100
+
+# CHECK: prologue2_calls_non_splitr11:
+# CHECK-NEXT: lea{{.*}} -16896(%rsp),{{.*}}%r11
+# CHECK: cmp{{.*}}%fs:{{[^,]*}},{{.*}}%r11
+# CHECK: jae{{.*$}}
+# CHECK-NEXT: callq{{.*}}<__morestack_non_split>
+
+prologue2 non_split r11 0x200
+
+	.section	.note.GNU-stack,"",@progbits
+	.section	.note.GNU-split-stack,"",@progbits
diff --git a/test/ELF/zdefs.s b/test/ELF/zdefs.s
index 93c61e1..21bec77 100644
--- a/test/ELF/zdefs.s
+++ b/test/ELF/zdefs.s
@@ -1,3 +1,4 @@
+# REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
 # RUN: ld.lld -shared %t.o -o %t1.so
 
diff --git a/test/ELF/znotext-plt-relocations-protected.s b/test/ELF/znotext-plt-relocations-protected.s
index 439d222..4fd8065 100644
--- a/test/ELF/znotext-plt-relocations-protected.s
+++ b/test/ELF/znotext-plt-relocations-protected.s
@@ -2,7 +2,7 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/znotext-plt-relocations-protected.s -o %t2.o
 # RUN: ld.lld %t2.o -o %t2.so -shared
-# RUN: not ld.lld -z notext %t.o %t2.so -o %t 2>&1 | FileCheck %s
+# RUN: not ld.lld -z notext %t.o %t2.so -o /dev/null 2>&1 | FileCheck %s
 
 # CHECK: error: cannot preempt symbol: foo
 
diff --git a/test/MinGW/driver.test b/test/MinGW/driver.test
index e7c608a..35d3ccf 100644
--- a/test/MinGW/driver.test
+++ b/test/MinGW/driver.test
@@ -90,6 +90,11 @@
 RUN: ld.lld -### -m i386pep foo.o --strip-all | FileCheck -check-prefix STRIP %s
 STRIP-NOT: -debug:dwarf
 
+RUN: ld.lld -### -m i386pep foo.o -S | FileCheck -check-prefix STRIP-DEBUG %s
+RUN: ld.lld -### -m i386pep foo.o --strip-debug | FileCheck -check-prefix STRIP-DEBUG %s
+STRIP-DEBUG: -debug:symtab
+STRIP-DEBUG-NOT: -debug:dwarf
+
 RUN: ld.lld -### -m i386pep foo.o -pdb out.pdb | FileCheck -check-prefix PDB %s
 PDB: -debug -pdb:out.pdb
 PDB-NOT: -debug:dwarf
diff --git a/test/darwin/cmdline-lto_library.objtxt b/test/darwin/cmdline-lto_library.objtxt
new file mode 100644
index 0000000..6b91235
--- /dev/null
+++ b/test/darwin/cmdline-lto_library.objtxt
@@ -0,0 +1,11 @@
+# RUN: ld64.lld -arch x86_64 -lto_library %t -print-atoms -r %s 2>&1 | FileCheck %s
+#
+# Test that the -lto_library option does not result in an error.
+#
+
+# CHECK-NOT: -lto_library
+
+--- !native
+defined-atoms:
+    - name:              _foo
+...
diff --git a/test/lit.cfg.py b/test/lit.cfg.py
index 41b57e5..d4d9ea7 100644
--- a/test/lit.cfg.py
+++ b/test/lit.cfg.py
@@ -65,8 +65,10 @@
                           'AMDGPU': 'amdgpu',
                           'ARM': 'arm',
                           'AVR': 'avr',
+                          'Hexagon': 'hexagon',
                           'Mips': 'mips',
                           'PowerPC': 'ppc',
+                          'RISCV': 'riscv',
                           'Sparc': 'sparc',
                           'WebAssembly': 'wasm',
                           'X86': 'x86'})
diff --git a/test/mach-o/dependency_info.yaml b/test/mach-o/dependency_info.yaml
index 34d6885..06b269a 100644
--- a/test/mach-o/dependency_info.yaml
+++ b/test/mach-o/dependency_info.yaml
@@ -9,7 +9,7 @@
 # RUN:        -F/Custom/Frameworks \
 # RUN:        -framework Bar \
 # RUN:        -framework Foo
-# RUN: '%python' %p/Inputs/DependencyDump.py %t.info | FileCheck %s
+# RUN: %python %p/Inputs/DependencyDump.py %t.info | FileCheck %s
 
 
 # CHECK: linker-vers: lld
diff --git a/test/wasm/Inputs/archive2.ll b/test/wasm/Inputs/archive2.ll
index c4903cb..66bfeac 100644
--- a/test/wasm/Inputs/archive2.ll
+++ b/test/wasm/Inputs/archive2.ll
@@ -7,3 +7,8 @@
   %call = tail call i32 @foo() #2
   ret i32 %call
 }
+
+define void @archive2_symbol() local_unnamed_addr #0 {
+entry:
+  ret void
+}
diff --git a/test/wasm/Inputs/archive3.ll b/test/wasm/Inputs/archive3.ll
new file mode 100644
index 0000000..8c78a46
--- /dev/null
+++ b/test/wasm/Inputs/archive3.ll
@@ -0,0 +1,11 @@
+target triple = "wasm32-unknown-unknown"
+
+define i32 @bar() local_unnamed_addr #0 {
+entry:
+  ret i32 1
+}
+
+define void @archive3_symbol() local_unnamed_addr #0 {
+entry:
+  ret void
+}
diff --git a/test/wasm/Inputs/ret32.ll b/test/wasm/Inputs/ret32.ll
index b1ccd64..674b34b 100644
--- a/test/wasm/Inputs/ret32.ll
+++ b/test/wasm/Inputs/ret32.ll
@@ -1,8 +1,6 @@
 target triple = "wasm32-unknown-unknown"
 
-; Function Attrs: norecurse nounwind readnone
-define i32 @ret32(float %arg) #0 {
+define hidden i32 @ret32(float %arg) {
 entry:
     ret i32 0
-     ; ptrtoint (i32 (float)* @ret32 to i32)
 }
diff --git a/test/wasm/Inputs/ret64.ll b/test/wasm/Inputs/ret64.ll
index 034260d..c1dd5e5 100644
--- a/test/wasm/Inputs/ret64.ll
+++ b/test/wasm/Inputs/ret64.ll
@@ -1,6 +1,6 @@
 target triple = "wasm32-unknown-unknown"
 
-define i64 @ret64(double %arg) local_unnamed_addr #0 {
+define hidden i64 @ret64(double %arg) {
 entry:
     ret i64 1
 }
diff --git a/test/wasm/archive-export.ll b/test/wasm/archive-export.ll
new file mode 100644
index 0000000..fc70ec7
--- /dev/null
+++ b/test/wasm/archive-export.ll
@@ -0,0 +1,50 @@
+Test that --export will also fetch lazy symbols from archives
+
+RUN: llc -filetype=obj %S/Inputs/start.ll -o %t.o
+RUN: llc -filetype=obj %S/Inputs/archive1.ll -o %t.a1.o
+RUN: llc -filetype=obj %S/Inputs/archive2.ll -o %t.a2.o
+RUN: rm -f %t.a
+RUN: llvm-ar rcs %t.a %t.a1.o %t.a2.o
+RUN: wasm-ld --export=archive2_symbol -o %t.wasm %t.a %t.o
+RUN: obj2yaml %t.wasm | FileCheck %s
+RUN: wasm-ld -o %t.wasm %t.a %t.o
+RUN: obj2yaml %t.wasm | FileCheck %s -check-prefix=NOEXPORT
+
+CHECK:         Exports:
+CHECK-NEXT:       - Name:            memory
+CHECK-NEXT:         Kind:            MEMORY
+CHECK-NEXT:         Index:           0
+CHECK-NEXT:       - Name:            __heap_base
+CHECK-NEXT:         Kind:            GLOBAL
+CHECK-NEXT:         Index:           1
+CHECK-NEXT:       - Name:            __data_end
+CHECK-NEXT:         Kind:            GLOBAL
+CHECK-NEXT:         Index:           2
+CHECK-NEXT:       - Name:            foo
+CHECK-NEXT:         Kind:            FUNCTION
+CHECK-NEXT:         Index:           2
+CHECK-NEXT:       - Name:            bar
+CHECK-NEXT:         Kind:            FUNCTION
+CHECK-NEXT:         Index:           3
+CHECK-NEXT:       - Name:            archive2_symbol
+CHECK-NEXT:         Kind:            FUNCTION
+CHECK-NEXT:         Index:           4
+CHECK-NEXT:       - Name:            _start
+CHECK-NEXT:         Kind:            FUNCTION
+CHECK-NEXT:         Index:           1
+CHECK-NEXT:   - Type:            CODE
+
+NOEXPORT:         Exports:
+NOEXPORT-NEXT:       - Name:            memory
+NOEXPORT-NEXT:         Kind:            MEMORY
+NOEXPORT-NEXT:         Index:           0
+NOEXPORT-NEXT:       - Name:            __heap_base
+NOEXPORT-NEXT:         Kind:            GLOBAL
+NOEXPORT-NEXT:         Index:           1
+NOEXPORT-NEXT:       - Name:            __data_end
+NOEXPORT-NEXT:         Kind:            GLOBAL
+NOEXPORT-NEXT:         Index:           2
+NOEXPORT-NEXT:       - Name:            _start
+NOEXPORT-NEXT:         Kind:            FUNCTION
+NOEXPORT-NEXT:         Index:           1
+NOEXPORT-NEXT:   - Type:            CODE
diff --git a/test/wasm/archive.ll b/test/wasm/archive.ll
index b2499ea..beea272 100644
--- a/test/wasm/archive.ll
+++ b/test/wasm/archive.ll
@@ -1,8 +1,10 @@
 ; RUN: llc -filetype=obj %s -o %t.o
 ; RUN: llc -filetype=obj %S/Inputs/archive1.ll -o %t.a1.o
 ; RUN: llc -filetype=obj %S/Inputs/archive2.ll -o %t.a2.o
-; RUN: llc -filetype=obj %S/Inputs/hello.ll -o %t.a3.o
-; RUN: llvm-ar rcs %t.a %t.a1.o %t.a2.o %t.a3.o
+; RUN: llc -filetype=obj %S/Inputs/archive3.ll -o %t.a3.o
+; RUN: llc -filetype=obj %S/Inputs/hello.ll -o %t.hello.o
+; RUN: rm -f %t.a
+; RUN: llvm-ar rcs %t.a %t.a1.o %t.a2.o %t.a3.o %t.hello.o
 ; RUN: rm -f %t.imports
 ; RUN: not wasm-ld %t.a %t.o -o %t.wasm 2>&1 | FileCheck -check-prefix=CHECK-UNDEFINED %s
 
@@ -31,9 +33,10 @@
 ; TODO(ncw): Update LLD so that the symbol table is written out for
 ;   non-relocatable output (with an option to strip it)
 
-; CHECK:      00000003 T _start
+; CHECK:      00000004 T _start
+; CHECK-NEXT: 00000002 T archive2_symbol
 ; CHECK-NEXT: 00000001 T bar
-; CHECK-NEXT: 00000002 T foo
+; CHECK-NEXT: 00000003 T foo
 ; CHECK-NEXT:          U missing_func
 
 ; Verify that symbols from unused objects don't appear in the symbol table
@@ -41,3 +44,9 @@
 
 ; Specifying the same archive twice is allowed.
 ; RUN: wasm-ld %t.a %t.a %t.o -o %t.wasm
+
+; Verfiy errors include library name
+; RUN: not wasm-ld -u archive2_symbol -u archive3_symbol %t.a %t.o -o %t.wasm 2>&1 | FileCheck -check-prefix=CHECK-DUP %s
+; CHECK-DUP: error: duplicate symbol: bar
+; CHECK-DUP: >>> defined in {{.*}}.a({{.*}}.a2.o)
+; CHECK-DUP: >>> defined in {{.*}}.a({{.*}}.a3.o)
diff --git a/test/wasm/call-indirect.ll b/test/wasm/call-indirect.ll
index 63a6def..f5dc220 100644
--- a/test/wasm/call-indirect.ll
+++ b/test/wasm/call-indirect.ll
@@ -105,9 +105,6 @@
 ; CHECK-NEXT:       - Name:            __data_end
 ; CHECK-NEXT:         Kind:            GLOBAL
 ; CHECK-NEXT:         Index:           2
-; CHECK-NEXT:       - Name:            _start
-; CHECK-NEXT:         Kind:            FUNCTION
-; CHECK-NEXT:         Index:           4
 ; CHECK-NEXT:       - Name:            bar
 ; CHECK-NEXT:         Kind:            FUNCTION
 ; CHECK-NEXT:         Index:           1
@@ -117,6 +114,9 @@
 ; CHECK-NEXT:       - Name:            foo
 ; CHECK-NEXT:         Kind:            FUNCTION
 ; CHECK-NEXT:         Index:           3
+; CHECK-NEXT:       - Name:            _start
+; CHECK-NEXT:         Kind:            FUNCTION
+; CHECK-NEXT:         Index:           4
 ; CHECK-NEXT:       - Name:            indirect_func
 ; CHECK-NEXT:         Kind:            GLOBAL
 ; CHECK-NEXT:         Index:           3
diff --git a/test/wasm/cxx-mangling.ll b/test/wasm/cxx-mangling.ll
index 67f3594..c7f15af 100644
--- a/test/wasm/cxx-mangling.ll
+++ b/test/wasm/cxx-mangling.ll
@@ -32,12 +32,12 @@
 ; CHECK-NEXT:       - Name:            __data_end
 ; CHECK-NEXT:         Kind:            GLOBAL
 ; CHECK-NEXT:         Index:           2
-; CHECK-NEXT:       - Name:            _start
-; CHECK-NEXT:         Kind:            FUNCTION
-; CHECK-NEXT:         Index:           3
 ; CHECK-NEXT:       - Name:            _Z3fooi
 ; CHECK-NEXT:         Kind:            FUNCTION
 ; CHECK-NEXT:         Index:           2
+; CHECK-NEXT:       - Name:            _start
+; CHECK-NEXT:         Kind:            FUNCTION
+; CHECK-NEXT:         Index:           3
 ; CHECK-NEXT:   - Type:            CODE
 ; CHECK-NEXT:     Functions:
 ; CHECK-NEXT:       - Index:           0
diff --git a/test/wasm/data-segment-merging.ll b/test/wasm/data-segment-merging.ll
index d0df84d..2a48b2d 100644
--- a/test/wasm/data-segment-merging.ll
+++ b/test/wasm/data-segment-merging.ll
@@ -5,44 +5,27 @@
 @c = hidden global [9 x i8] c"whatever\00", align 1
 @d = hidden global i32 42, align 4
 
+@e = private constant [9 x i8] c"constant\00", align 1
+@f = private constant i8 43, align 4
+
 ; RUN: llc -filetype=obj %s -o %t.data-segment-merging.o
 
 ; RUN: wasm-ld -no-gc-sections --no-entry -o %t.merged.wasm %t.data-segment-merging.o
 ; RUN: obj2yaml %t.merged.wasm | FileCheck %s --check-prefix=MERGE
-; MERGE:       - Type:            DATA
-; MERGE-NEXT:    Segments:
-; MERGE-NEXT:      - SectionOffset:   7
-; MERGE-NEXT:        MemoryIndex:     0
-; MERGE-NEXT:        Offset:
-; MERGE-NEXT:          Opcode:          I32_CONST
-; MERGE-NEXT:          Value:           1024
-; MERGE-NEXT:        Content:         68656C6C6F00676F6F6462796500776861746576657200002A000000
+; MERGE:   - Type:            DATA
+; MERGE:     Segments:
+; MERGE:        Content:         68656C6C6F00676F6F6462796500776861746576657200002A000000
+; MERGE:        Content:         636F6E7374616E74000000002B
+; MERGE-NOT:    Content:
 
 ; RUN: wasm-ld -no-gc-sections --no-entry --no-merge-data-segments -o %t.separate.wasm %t.data-segment-merging.o
 ; RUN: obj2yaml %t.separate.wasm | FileCheck %s --check-prefix=SEPARATE
-; SEPARATE:       - Type:            DATA
-; SEPARATE-NEXT:    Segments:
-; SEPARATE-NEXT:      - SectionOffset:   7
-; SEPARATE-NEXT:        MemoryIndex:     0
-; SEPARATE-NEXT:        Offset:
-; SEPARATE-NEXT:          Opcode:          I32_CONST
-; SEPARATE-NEXT:          Value:           1024
-; SEPARATE-NEXT:        Content:         68656C6C6F00
-; SEPARATE-NEXT:      - SectionOffset:   19
-; SEPARATE-NEXT:        MemoryIndex:     0
-; SEPARATE-NEXT:        Offset:
-; SEPARATE-NEXT:          Opcode:          I32_CONST
-; SEPARATE-NEXT:          Value:           1030
-; SEPARATE-NEXT:        Content:         676F6F6462796500
-; SEPARATE-NEXT:      - SectionOffset:   33
-; SEPARATE-NEXT:        MemoryIndex:     0
-; SEPARATE-NEXT:        Offset:
-; SEPARATE-NEXT:          Opcode:          I32_CONST
-; SEPARATE-NEXT:          Value:           1038
-; SEPARATE-NEXT:        Content:         '776861746576657200'
-; SEPARATE-NEXT:      - SectionOffset:   48
-; SEPARATE-NEXT:        MemoryIndex:     0
-; SEPARATE-NEXT:        Offset:
-; SEPARATE-NEXT:          Opcode:          I32_CONST
-; SEPARATE-NEXT:          Value:           1048
-; SEPARATE-NEXT:        Content:         2A000000
+; SEPARATE:   - Type:            DATA
+; SEPARATE:     Segments:
+; SEPARATE:        Content:         68656C6C6F00
+; SEPARATE:        Content:         676F6F6462796500
+; SEPARATE:        Content:         '776861746576657200'
+; SEPARATE:        Content:         2A000000
+; SEPARATE:        Content:         636F6E7374616E7400
+; SEPARATE:        Content:         2B
+; SEPARATE-NOT:    Content:
diff --git a/test/wasm/demangle.ll b/test/wasm/demangle.ll
index f0416bb..64fa46a 100644
--- a/test/wasm/demangle.ll
+++ b/test/wasm/demangle.ll
@@ -1,17 +1,19 @@
 ; RUN: llc -filetype=obj %s -o %t.o
-; RUN: not wasm-ld --undefined _Z3fooi \
-; RUN:     -o %t.wasm %t.o 2>&1 | FileCheck %s
+; RUN: not wasm-ld -o %t.wasm %t.o 2>&1 | FileCheck %s
 
-; CHECK: error: undefined symbol: foo(int)
+; CHECK: error: {{.*}}.o: undefined symbol: foo(int)
 
-; RUN: not wasm-ld --no-demangle --undefined _Z3fooi \
-; RUN:     -o %t.wasm %t.o 2>&1 |  FileCheck -check-prefix=CHECK-NODEMANGLE %s
+; RUN: not wasm-ld --no-demangle \
+; RUN:     -o %t.wasm %t.o 2>&1 | FileCheck -check-prefix=CHECK-NODEMANGLE %s
 
-; CHECK-NODEMANGLE: error: undefined symbol: _Z3fooi
+; CHECK-NODEMANGLE: error: {{.*}}.o: undefined symbol: _Z3fooi
 
 target triple = "wasm32-unknown-unknown"
 
+declare void @_Z3fooi(i32);
+
 define hidden void @_start() local_unnamed_addr {
 entry:
+    call void @_Z3fooi(i32 1)
     ret void
 }
diff --git a/test/wasm/export-all.ll b/test/wasm/export-all.ll
new file mode 100644
index 0000000..f903df9
--- /dev/null
+++ b/test/wasm/export-all.ll
@@ -0,0 +1,48 @@
+; RUN: llc -O0 -filetype=obj %s -o %t.o
+
+; RUN: wasm-ld -o %t.wasm %t.o
+; RUN: obj2yaml %t.wasm | FileCheck %s
+
+; RUN: wasm-ld --export-all -o %t.wasm %t.o
+; RUN: obj2yaml %t.wasm | FileCheck %s -check-prefix=EXPORT
+
+; RUN: wasm-ld --export-all --no-gc-sections -o %t.wasm %t.o
+; RUN: obj2yaml %t.wasm | FileCheck %s -check-prefix=EXPORT
+
+; Verify the --export-all flag exports hidden symbols
+
+target triple = "wasm32-unknown-unknown"
+
+define internal void @internal_func() local_unnamed_addr {
+entry:
+  ret void
+}
+
+define hidden void @bar() local_unnamed_addr {
+entry:
+  ret void
+}
+
+define hidden void @foo() local_unnamed_addr {
+entry:
+  ret void
+}
+
+define hidden void @_start() local_unnamed_addr {
+entry:
+  call void @foo()
+  call void @internal_func()
+  ret void
+}
+
+; CHECK:      - Type:            EXPORT
+; CHECK:         - Name:            _start
+; CHECK-NOT:     - Name:            bar
+; CHECK-NOT:     - Name:            foo
+; CHECK-NOT:     - Name:            internal_func
+
+; EXPORT:     - Type:            EXPORT
+; EXPORT:        - Name:            bar
+; EXPORT:        - Name:            foo
+; EXPORT:        - Name:            _start
+; EXPORT-NOT:    - Name:            internal_func
diff --git a/test/wasm/export.ll b/test/wasm/export.ll
index 16b2b6c..519aafe 100644
--- a/test/wasm/export.ll
+++ b/test/wasm/export.ll
@@ -28,10 +28,10 @@
 ; CHECK-NEXT:       - Name:            __data_end
 ; CHECK-NEXT:         Kind:            GLOBAL
 ; CHECK-NEXT:         Index:           2
-; CHECK-NEXT:       - Name:            _start
-; CHECK-NEXT:         Kind:            FUNCTION
-; CHECK-NEXT:         Index:           2
 ; CHECK-NEXT:       - Name:            hidden_function
 ; CHECK-NEXT:         Kind:            FUNCTION
 ; CHECK-NEXT:         Index:           1
+; CHECK-NEXT:       - Name:            _start
+; CHECK-NEXT:         Kind:            FUNCTION
+; CHECK-NEXT:         Index:           2
 ; CHECK-NEXT:   - Type:            CODE
diff --git a/test/wasm/fatal-warnings.ll b/test/wasm/fatal-warnings.ll
index 9bfe95e..0007dc2 100644
--- a/test/wasm/fatal-warnings.ll
+++ b/test/wasm/fatal-warnings.ll
@@ -3,8 +3,8 @@
 ; RUN: lld -flavor wasm -o %t.wasm %t.main.o %t.ret32.o 2>&1 | FileCheck %s -check-prefix=CHECK-WARN
 ; RUN: not lld -flavor wasm --fatal-warnings -o %t.wasm %t.main.o %t.ret32.o 2>&1 | FileCheck %s -check-prefix=CHECK-FATAL
 
-; CHECK-WARN: warning: Function type mismatch: ret32
-; CHECK-FATAL: error: Function type mismatch: ret32
+; CHECK-WARN: warning: function signature mismatch: ret32
+; CHECK-FATAL: error: function signature mismatch: ret32
 
 target triple = "wasm32-unknown-unknown"
 
diff --git a/test/wasm/gc-imports.ll b/test/wasm/gc-imports.ll
index 066cd88..fbcb74d 100644
--- a/test/wasm/gc-imports.ll
+++ b/test/wasm/gc-imports.ll
@@ -1,15 +1,21 @@
 ; RUN: llc -filetype=obj %s -o %t.o
 ; RUN: yaml2obj %S/Inputs/undefined-globals.yaml -o %t_globals.o
-; RUN: wasm-ld -print-gc-sections --allow-undefined -o %t1.wasm %t.o %t_globals.o
+; RUN: wasm-ld --allow-undefined -o %t1.wasm %t.o %t_globals.o
 
 target triple = "wasm32-unknown-unknown"
 
-declare hidden i64 @unused_undef_function(i64 %arg)
+declare i64 @unused_undef_function(i64 %arg)
 
-declare hidden i32 @used_undef_function()
+declare i32 @used_undef_function()
 
 declare i64 @use_undef_global()
 
+define hidden void @foo() {
+entry:
+  call i64 @unused_undef_function(i64 0)
+  ret void
+}
+
 define hidden void @_start() {
 entry:
   call i32 @used_undef_function()
@@ -19,18 +25,7 @@
 
 ; RUN: obj2yaml %t1.wasm | FileCheck %s
 
-; CHECK:        - Type:            TYPE
-; CHECK-NEXT:     Signatures:
-; CHECK-NEXT:       - Index:           0
-; CHECK-NEXT:         ReturnType:      I32
-; CHECK-NEXT:         ParamTypes:
-; CHECK-NEXT:       - Index:           1
-; CHECK-NEXT:         ReturnType:      NORESULT
-; CHECK-NEXT:         ParamTypes:
-; CHECK-NEXT:       - Index:           2
-; CHECK-NEXT:         ReturnType:      I64
-; CHECK-NEXT:         ParamTypes:
-; CHECK-NEXT:   - Type:            IMPORT
+; CHECK:        - Type:            IMPORT
 ; CHECK-NEXT:     Imports:
 ; CHECK-NEXT:       - Module:          env
 ; CHECK-NEXT:         Field:           used_undef_function
@@ -55,33 +50,18 @@
 ; CHECK-NEXT:         Name:            use_undef_global
 ; CHECK-NEXT: ...
 
-; RUN: wasm-ld -print-gc-sections --no-gc-sections --allow-undefined \
+; RUN: wasm-ld --no-gc-sections --allow-undefined \
 ; RUN:     -o %t1.no-gc.wasm %t.o %t_globals.o
 ; RUN: obj2yaml %t1.no-gc.wasm | FileCheck %s -check-prefix=NO-GC
 
-; NO-GC:        - Type:            TYPE
-; NO-GC-NEXT:     Signatures:
-; NO-GC-NEXT:       - Index:           0
-; NO-GC-NEXT:         ReturnType:      I32
-; NO-GC-NEXT:         ParamTypes:
-; NO-GC-NEXT:       - Index:           1
-; NO-GC-NEXT:         ReturnType:      I64
-; NO-GC-NEXT:         ParamTypes:
-; NO-GC-NEXT:           - I64
-; NO-GC-NEXT:       - Index:           2
-; NO-GC-NEXT:         ReturnType:      NORESULT
-; NO-GC-NEXT:         ParamTypes:
-; NO-GC-NEXT:       - Index:           3
-; NO-GC-NEXT:         ReturnType:      I64
-; NO-GC-NEXT:         ParamTypes:
-; NO-GC-NEXT:   - Type:            IMPORT
+; NO-GC:        - Type:            IMPORT
 ; NO-GC-NEXT:     Imports:
 ; NO-GC-NEXT:       - Module:          env
-; NO-GC-NEXT:         Field:           used_undef_function
+; NO-GC-NEXT:         Field:           unused_undef_function
 ; NO-GC-NEXT:         Kind:            FUNCTION
 ; NO-GC-NEXT:         SigIndex:        0
 ; NO-GC-NEXT:       - Module:          env
-; NO-GC-NEXT:         Field:           unused_undef_function
+; NO-GC-NEXT:         Field:           used_undef_function
 ; NO-GC-NEXT:         Kind:            FUNCTION
 ; NO-GC-NEXT:         SigIndex:        1
 ; NO-GC-NEXT:       - Module:          env
@@ -99,13 +79,15 @@
 ; NO-GC-NEXT:     Name:            name
 ; NO-GC-NEXT:     FunctionNames:
 ; NO-GC-NEXT:       - Index:           0
-; NO-GC-NEXT:         Name:            used_undef_function
-; NO-GC-NEXT:       - Index:           1
 ; NO-GC-NEXT:         Name:            unused_undef_function
+; NO-GC-NEXT:       - Index:           1
+; NO-GC-NEXT:         Name:            used_undef_function
 ; NO-GC-NEXT:       - Index:           2
 ; NO-GC-NEXT:         Name:            __wasm_call_ctors
 ; NO-GC-NEXT:       - Index:           3
-; NO-GC-NEXT:         Name:            _start
+; NO-GC-NEXT:         Name:            foo
 ; NO-GC-NEXT:       - Index:           4
+; NO-GC-NEXT:         Name:            _start
+; NO-GC-NEXT:       - Index:           5
 ; NO-GC-NEXT:         Name:            use_undef_global
 ; NO-GC-NEXT: ...
diff --git a/test/wasm/load-undefined.test b/test/wasm/load-undefined.test
index 52a4a04..1b8d259 100644
--- a/test/wasm/load-undefined.test
+++ b/test/wasm/load-undefined.test
@@ -4,36 +4,35 @@
 ; RUN: llc -filetype=obj %S/Inputs/ret64.ll -o %t.o
 ; RUN: llc -filetype=obj %S/Inputs/ret32.ll -o %t2.o
 ; RUN: llc -filetype=obj %S/Inputs/start.ll -o %t.start.o
+; RUN: rm -f %t2.a
 ; RUN: llvm-ar rcs %t2.a %t2.o
-; RUN: wasm-ld %t.start.o %t2.a %t.o -o %t.wasm -u ret32 --undefined ret64
+; RUN: wasm-ld %t.start.o --no-gc-sections %t2.a %t.o -o %t.wasm -u ret32 --undefined ret64
 ; RUN: obj2yaml %t.wasm | FileCheck %s
+; RUN: wasm-ld %t.start.o --no-gc-sections %t2.a %t.o -o %t2.wasm
+; RUN: obj2yaml %t2.wasm | FileCheck %s -check-prefix=NO-LOAD
 
-; CHECK:        - Type:            EXPORT
-; CHECK-NEXT:     Exports:
-; CHECK-NEXT:       - Name:            memory
-; CHECK-NEXT:         Kind:            MEMORY
-; CHECK-NEXT:         Index:           0
-; CHECK-NEXT:       - Name:            __heap_base
-; CHECK-NEXT:         Kind:            GLOBAL
-; CHECK-NEXT:         Index:           1
-; CHECK-NEXT:       - Name:            __data_end
-; CHECK-NEXT:         Kind:            GLOBAL
-; CHECK-NEXT:         Index:           2
-; CHECK-NEXT:       - Name:            _start
-; CHECK-NEXT:         Kind:            FUNCTION
-; CHECK-NEXT:         Index:           1
-; CHECK-NEXT:       - Name:            ret32
-; CHECK-NEXT:         Kind:            FUNCTION
-; CHECK-NEXT:         Index:           2
-; CHECK-NEXT:       - Name:            ret64
-; CHECK-NEXT:         Kind:            FUNCTION
-; CHECK-NEXT:         Index:           3
-; CHECK-NEXT:   - Type:
+; CHECK:         Name:            name
+; CHECK-NEXT:    FunctionNames:   
+; CHECK-NEXT:      - Index:           0
+; CHECK-NEXT:        Name:            __wasm_call_ctors
+; CHECK-NEXT:      - Index:           1
+; CHECK-NEXT:        Name:            _start
+; CHECK-NEXT:      - Index:           2
+; CHECK-NEXT:        Name:            ret64
+; CHECK-NEXT:      - Index:           3
+; CHECK-NEXT:        Name:            ret32
+; CHECK-NEXT: ...
 
+; NO-LOAD:         Name:            name
+; NO-LOAD-NEXT:    FunctionNames:   
+; NO-LOAD-NEXT:      - Index:           0
+; NO-LOAD-NEXT:        Name:            __wasm_call_ctors
+; NO-LOAD-NEXT:      - Index:           1
+; NO-LOAD-NEXT:        Name:            _start
+; NO-LOAD-NEXT:      - Index:           2
+; NO-LOAD-NEXT:        Name:            ret64
+; NO-LOAD-NEXT: ...
 
-; Verify that referencing a symbol that doesn't exist won't work
-; RUN: not wasm-ld %t.start.o -o %t.wasm -u symboldoesnotexist 2>&1 | FileCheck -check-prefix=CHECK-UNDEFINED1 %s
-; CHECK-UNDEFINED1: error: undefined symbol: symboldoesnotexist
-
-; RUN: not wasm-ld %t.start.o -o %t.wasm --undefined symboldoesnotexist --allow-undefined 2>&1 | FileCheck -check-prefix=CHECK-UNDEFINED2 %s
-; CHECK-UNDEFINED2: symbol forced with --undefined not found: symboldoesnotexist
+; Verify that referencing a symbol that is not found doesn't result in a link
+; failure.  This matches the behaviour of the ELF linker.
+; RUN: wasm-ld %t.start.o -o %t.wasm -u symboldoesnotexist
diff --git a/test/wasm/lto/Inputs/archive.ll b/test/wasm/lto/Inputs/archive.ll
new file mode 100644
index 0000000..7598479
--- /dev/null
+++ b/test/wasm/lto/Inputs/archive.ll
@@ -0,0 +1,6 @@
+target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
+target triple = "wasm32-unknown-unknown"
+
+define void @f() {
+  ret void
+}
diff --git a/test/wasm/lto/Inputs/used.ll b/test/wasm/lto/Inputs/used.ll
new file mode 100644
index 0000000..d0250d5
--- /dev/null
+++ b/test/wasm/lto/Inputs/used.ll
@@ -0,0 +1,8 @@
+target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
+target triple = "wasm32-unknown-unknown"
+
+@foo = hidden global i32 1
+
+define hidden void @bar() {
+  ret void
+}
diff --git a/test/wasm/lto/archive.ll b/test/wasm/lto/archive.ll
new file mode 100644
index 0000000..89fa840
--- /dev/null
+++ b/test/wasm/lto/archive.ll
@@ -0,0 +1,25 @@
+; RUN: llvm-as %S/Inputs/archive.ll -o %t1.o
+; RUN: rm -f %t.a
+; RUN: llvm-ar rcs %t.a %t1.o
+; RUN: llvm-as %s -o %t2.o
+; RUN: wasm-ld %t2.o %t.a -o %t3
+; RUN: obj2yaml %t3 | FileCheck %s
+
+target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
+target triple = "wasm32-unknown-unknown"
+
+define void @_start() {
+  call void @f()
+  ret void
+}
+
+declare void @f()
+
+; CHECK:         Name:            name
+; CHECK-NEXT:    FunctionNames:
+; CHECK-NEXT:      - Index:           0
+; CHECK-NEXT:        Name:            __wasm_call_ctors
+; CHECK-NEXT:      - Index:           1
+; CHECK-NEXT:        Name:            _start
+; CHECK-NEXT:      - Index:           2
+; CHECK-NEXT:        Name:            f
diff --git a/test/wasm/lto/atomics.ll b/test/wasm/lto/atomics.ll
new file mode 100644
index 0000000..a5a82ae
--- /dev/null
+++ b/test/wasm/lto/atomics.ll
@@ -0,0 +1,14 @@
+; RUN: llvm-as %s -o %t.o
+; RUN: wasm-ld %t.o -o %t.wasm -lto-O0
+; Atomic operations with fail to compile if the ThreadModel is not
+; correctly set to Single (i.e. if atomics are not lowered to regular ops).
+
+target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
+target triple = "wasm32-unknown-unknown-wasm"
+
+@foo = hidden global i32 1
+
+define void @_start() {
+  %1 = load atomic i32, i32* @foo unordered, align 4
+  ret void
+}
diff --git a/test/wasm/lto/cache.ll b/test/wasm/lto/cache.ll
index b0a7820..9b4aa5d 100644
--- a/test/wasm/lto/cache.ll
+++ b/test/wasm/lto/cache.ll
@@ -11,7 +11,7 @@
 ; RUN: ls %t.cache | count 4
 
 ; Create a file of size 64KB.
-; RUN: "%python" -c "print(' ' * 65536)" > %t.cache/llvmcache-foo
+; RUN: %python -c "print(' ' * 65536)" > %t.cache/llvmcache-foo
 
 ; This should leave the file in place.
 ; RUN: wasm-ld --thinlto-cache-dir=%t.cache --thinlto-cache-policy cache_size_bytes=128k:prune_interval=0s -o %t.wasm %t2.o %t.o
diff --git a/test/wasm/lto/diagnostics.ll b/test/wasm/lto/diagnostics.ll
new file mode 100644
index 0000000..77f68fe
--- /dev/null
+++ b/test/wasm/lto/diagnostics.ll
@@ -0,0 +1,22 @@
+; verify that errors in the LLVM backend during LTO manifest as lld
+; errors
+
+; RUN: llvm-as %s -o %t.o
+; RUN: not wasm-ld --lto-O0 %t.o -o %t2 2>&1 | FileCheck %s
+
+target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
+target triple = "wasm32-unknown-unknown"
+
+define void @_start() {
+  call i8* @foo()
+  ret void
+}
+
+define i8* @foo() {
+  %1 = call i8* @llvm.returnaddress(i32 0)
+  ret i8* %1
+}
+
+declare i8* @llvm.returnaddress(i32)
+
+; CHECK: error: {{.*}} WebAssembly hasn't implemented __builtin_return_address
diff --git a/test/wasm/lto/export.ll b/test/wasm/lto/export.ll
new file mode 100644
index 0000000..0ff0be5
--- /dev/null
+++ b/test/wasm/lto/export.ll
@@ -0,0 +1,38 @@
+; RUN: llvm-as -o %t.bc %s
+; RUN: not wasm-ld --export=missing -o %t.wasm %t.bc 2>&1 | FileCheck -check-prefix=CHECK-ERROR %s
+; RUN: wasm-ld --export=hidden_function -o %t.wasm %t.bc
+; RUN: obj2yaml %t.wasm | FileCheck %s
+
+target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
+target triple = "wasm32-unknown-unknown"
+
+define hidden i32 @hidden_function() local_unnamed_addr {
+entry:
+  ret i32 0
+}
+
+define void @_start() local_unnamed_addr {
+entry:
+  ret void
+}
+
+; CHECK-ERROR: error: symbol exported via --export not found: missing
+
+; CHECK:        - Type:            EXPORT
+; CHECK-NEXT:     Exports:
+; CHECK-NEXT:       - Name:            memory
+; CHECK-NEXT:         Kind:            MEMORY
+; CHECK-NEXT:         Index:           0
+; CHECK-NEXT:       - Name:            __heap_base
+; CHECK-NEXT:         Kind:            GLOBAL
+; CHECK-NEXT:         Index:           1
+; CHECK-NEXT:       - Name:            __data_end
+; CHECK-NEXT:         Kind:            GLOBAL
+; CHECK-NEXT:         Index:           2
+; CHECK-NEXT:       - Name:            hidden_function
+; CHECK-NEXT:         Kind:            FUNCTION
+; CHECK-NEXT:         Index:           1
+; CHECK-NEXT:       - Name:            _start
+; CHECK-NEXT:         Kind:            FUNCTION
+; CHECK-NEXT:         Index:           2
+; CHECK-NEXT:   - Type:            CODE
diff --git a/test/wasm/lto/undef.ll b/test/wasm/lto/undef.ll
new file mode 100644
index 0000000..729007b
--- /dev/null
+++ b/test/wasm/lto/undef.ll
@@ -0,0 +1,20 @@
+; RUN: llvm-as %s -o %t.o
+; RUN: wasm-ld %t.o -o %t.wasm --allow-undefined
+; RUN: obj2yaml %t.wasm | FileCheck %s
+
+target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
+target triple = "wasm32-unknown-unknown"
+
+declare void @bar()
+
+define void @_start() {
+  call void @bar()
+  ret void
+}
+
+; CHECK:       - Type:            IMPORT
+; CHECK-NEXT:    Imports:         
+; CHECK-NEXT:      - Module:          env
+; CHECK-NEXT:        Field:           bar
+; CHECK-NEXT:        Kind:            FUNCTION
+; CHECK-NEXT:        SigIndex:        0
diff --git a/test/wasm/lto/used.ll b/test/wasm/lto/used.ll
new file mode 100644
index 0000000..8bf8403
--- /dev/null
+++ b/test/wasm/lto/used.ll
@@ -0,0 +1,45 @@
+; RUN: llc %s -o %t.o -filetype=obj
+; RUN: llvm-as %S/Inputs/used.ll -o %t1.o
+; RUN: wasm-ld %t.o %t1.o -o %t.wasm
+; RUN: obj2yaml %t.wasm | FileCheck %s
+
+; Verify that symbols references from regular objects are preserved by LTO
+
+target datalayout = "e-m:e-p:32:32-i64:64-n32:64-S128"
+target triple = "wasm32-unknown-unknown"
+
+declare void @bar()
+
+@foo = external global i32
+
+define void @_start() {
+  %val = load i32, i32* @foo, align 4
+  %tobool = icmp ne i32 %val, 0
+  br i1 %tobool, label %callbar, label %return
+
+callbar:
+  call void @bar()
+  br label %return
+
+return:
+  ret void
+}
+
+; CHECK:        - Type:            DATA
+; CHECK-NEXT:     Segments:
+; CHECK-NEXT:       - SectionOffset:   7
+; CHECK-NEXT:         MemoryIndex:     0
+; CHECK-NEXT:         Offset:
+; CHECK-NEXT:           Opcode:          I32_CONST
+; CHECK-NEXT:           Value:           1024
+; CHECK-NEXT:         Content:         '01000000'
+
+; CHECK:       - Type:            CUSTOM
+; CHECK-NEXT:    Name:            name
+; CHECK-NEXT:    FunctionNames:   
+; CHECK-NEXT:      - Index:           0
+; CHECK-NEXT:        Name:            __wasm_call_ctors
+; CHECK-NEXT:      - Index:           1
+; CHECK-NEXT:        Name:            _start
+; CHECK-NEXT:      - Index:           2
+; CHECK-NEXT:        Name:            bar
diff --git a/test/wasm/responsefile.test b/test/wasm/responsefile.test
index d5e262c..11f8059 100644
--- a/test/wasm/responsefile.test
+++ b/test/wasm/responsefile.test
@@ -1,10 +1,10 @@
 RUN: llc -filetype=obj -o %t.o %p/Inputs/ret32.ll
 
-RUN: echo "%t.o -o %t.wasm -e ret32" > %t.rsp
+RUN: echo "%/t.o -o %/t.wasm -e ret32" > %t.rsp
 RUN: wasm-ld @%t.rsp --initial-memory=655360
 RUN: llvm-readobj --sections %t.wasm | FileCheck %s
 CHECK: InitialPages: 10
 
 RUN: echo "blah\foo" > %t.rsp
 RUN: not wasm-ld @%t.rsp 2>&1 | FileCheck --check-prefix=ESCAPE %s
-ESCAPE: error: cannot open blahfoo: No such file or directory
+ESCAPE: error: cannot open blahfoo: {{[Nn]}}o such file or directory
diff --git a/test/wasm/signature-mismatch-weak.ll b/test/wasm/signature-mismatch-weak.ll
index 8123b60..dbf73d1 100644
--- a/test/wasm/signature-mismatch-weak.ll
+++ b/test/wasm/signature-mismatch-weak.ll
@@ -13,6 +13,6 @@
   ret void
 }
 
-; CHECK: warning: Function type mismatch: weakFn
+; CHECK: warning: function signature mismatch: weakFn
 ; CHECK-NEXT: >>> defined as () -> I32 in {{.*}}signature-mismatch-weak.ll.tmp.o
 ; CHECK-NEXT: >>> defined as () -> I64 in {{.*}}signature-mismatch-weak.ll.tmp.strong.o
diff --git a/test/wasm/signature-mismatch.ll b/test/wasm/signature-mismatch.ll
index 5b91c19..d750d4f 100644
--- a/test/wasm/signature-mismatch.ll
+++ b/test/wasm/signature-mismatch.ll
@@ -17,10 +17,10 @@
 
 declare i32 @ret32(i32, i64, i32) local_unnamed_addr #1
 
-; CHECK: error: Function type mismatch: ret32
+; CHECK: error: function signature mismatch: ret32
 ; CHECK-NEXT: >>> defined as (I32, I64, I32) -> I32 in {{.*}}.main.o
 ; CHECK-NEXT: >>> defined as (F32) -> I32 in {{.*}}.ret32.o
 
-; REVERSE: error: Function type mismatch: ret32
+; REVERSE: error: function signature mismatch: ret32
 ; REVERSE-NEXT: >>> defined as (F32) -> I32 in {{.*}}.ret32.o
 ; REVERSE-NEXT: >>> defined as (I32, I64, I32) -> I32 in {{.*}}.main.o
diff --git a/test/wasm/undefined-entry.test b/test/wasm/undefined-entry.test
index ffa079c..a36212f 100644
--- a/test/wasm/undefined-entry.test
+++ b/test/wasm/undefined-entry.test
@@ -1,11 +1,9 @@
 RUN: llc -filetype=obj %p/Inputs/ret32.ll -o %t.ret32.o
 RUN: not wasm-ld -o %t.wasm %t.ret32.o 2>&1 | FileCheck %s
+RUN: not wasm-ld --allow-undefined -o %t.wasm %t.ret32.o 2>&1 | FileCheck %s
 RUN: not wasm-ld -entry=foo -o %t.wasm %t.ret32.o 2>&1 | FileCheck %s -check-prefix=CHECK-CUSTOM
-RUN: not wasm-ld --allow-undefined -o %t.wasm %t.ret32.o 2>&1 | FileCheck %s -check-prefix=CHECK-ALLOW
 
-CHECK: error: undefined symbol: _start
-CHECK-CUSTOM: error: undefined symbol: foo
-CHECK-ALLOW: error: entry symbol not defined (pass --no-entry to supress):
-_start
+CHECK: error: entry symbol not defined (pass --no-entry to supress): _start
+CHECK-CUSTOM: error: entry symbol not defined (pass --no-entry to supress): foo
 
 RUN: wasm-ld --no-entry -o %t.wasm %t.ret32.o
diff --git a/test/wasm/undefined.ll b/test/wasm/undefined.ll
index 7d2161d..1da979f 100644
--- a/test/wasm/undefined.ll
+++ b/test/wasm/undefined.ll
@@ -1,10 +1,10 @@
 ; RUN: llc -filetype=obj %s -o %t.o
 ; RUN: wasm-ld --allow-undefined -o %t.wasm %t.o
 
-; Fails due to undefined 'foo' and also 'baz'
+; Fails due to undefined 'foo' 
 ; RUN: not wasm-ld --undefined=baz -o %t.wasm %t.o 2>&1 | FileCheck %s
 ; CHECK: error: {{.*}}.o: undefined symbol: foo
-; CHECK: error: undefined symbol: baz
+; CHECK-NOT: undefined symbol: baz
 
 ; Succeeds if we pass a file containing 'foo' as --allow-undefined-file.
 ; RUN: echo 'foo' > %t.txt
diff --git a/test/wasm/visibility-hidden.ll b/test/wasm/visibility-hidden.ll
index af973df..f553c08 100644
--- a/test/wasm/visibility-hidden.ll
+++ b/test/wasm/visibility-hidden.ll
@@ -1,5 +1,6 @@
 ; RUN: llc -filetype=obj -o %t.o %s
 ; RUN: llc -filetype=obj %S/Inputs/hidden.ll -o %t2.o
+; RUN: rm -f %t2.a
 ; RUN: llvm-ar rcs %t2.a %t2.o
 ; RUN: wasm-ld %t.o %t2.a -o %t.wasm
 ; RUN: obj2yaml %t.wasm | FileCheck %s
@@ -42,12 +43,12 @@
 ; CHECK-NEXT:       - Name:            __data_end
 ; CHECK-NEXT:         Kind:            GLOBAL
 ; CHECK-NEXT:         Index:           2
-; CHECK-NEXT:       - Name:            _start
-; CHECK-NEXT:         Kind:            FUNCTION
-; CHECK-NEXT:         Index:           3
 ; CHECK-NEXT:       - Name:            objectDefault
 ; CHECK-NEXT:         Kind:            FUNCTION
 ; CHECK-NEXT:         Index:           2
+; CHECK-NEXT:       - Name:            _start
+; CHECK-NEXT:         Kind:            FUNCTION
+; CHECK-NEXT:         Index:           3
 ; CHECK-NEXT:       - Name:            archiveDefault
 ; CHECK-NEXT:         Kind:            FUNCTION
 ; CHECK-NEXT:         Index:           5
diff --git a/test/wasm/weak-alias-overide.ll b/test/wasm/weak-alias-overide.ll
index 8b98f33..a6a37f2 100644
--- a/test/wasm/weak-alias-overide.ll
+++ b/test/wasm/weak-alias-overide.ll
@@ -74,12 +74,12 @@
 ; CHECK-NEXT:       - Name:            __data_end
 ; CHECK-NEXT:         Kind:            GLOBAL
 ; CHECK-NEXT:         Index:           2
-; CHECK-NEXT:       - Name:            _start
-; CHECK-NEXT:         Kind:            FUNCTION
-; CHECK-NEXT:         Index:           2
 ; CHECK-NEXT:       - Name:            alias_fn
 ; CHECK-NEXT:         Kind:            FUNCTION
 ; CHECK-NEXT:         Index:           1
+; CHECK-NEXT:       - Name:            _start
+; CHECK-NEXT:         Kind:            FUNCTION
+; CHECK-NEXT:         Index:           2
 ; CHECK-NEXT:       - Name:            direct_fn
 ; CHECK-NEXT:         Kind:            FUNCTION
 ; CHECK-NEXT:         Index:           3
diff --git a/test/wasm/weak-undefined.ll b/test/wasm/weak-undefined.ll
index 53b38bc..048b91b 100644
--- a/test/wasm/weak-undefined.ll
+++ b/test/wasm/weak-undefined.ll
@@ -81,15 +81,15 @@
 ; CHECK-NEXT:       - Name:            __data_end
 ; CHECK-NEXT:         Kind:            GLOBAL
 ; CHECK-NEXT:         Index:           2
-; CHECK-NEXT:       - Name:            _start
-; CHECK-NEXT:         Kind:            FUNCTION
-; CHECK-NEXT:         Index:           3
 ; CHECK-NEXT:       - Name:            get_address_of_foo
 ; CHECK-NEXT:         Kind:            FUNCTION
 ; CHECK-NEXT:         Index:           1
 ; CHECK-NEXT:       - Name:            get_address_of_global_var
 ; CHECK-NEXT:         Kind:            FUNCTION
 ; CHECK-NEXT:         Index:           2
+; CHECK-NEXT:       - Name:            _start
+; CHECK-NEXT:         Kind:            FUNCTION
+; CHECK-NEXT:         Index:           3
 ; CHECK-NEXT:   - Type:            CODE
 ; CHECK-NEXT:     Functions:
 ; CHECK-NEXT:       - Index:           0
diff --git a/test/wasm/whole-archive.test b/test/wasm/whole-archive.test
new file mode 100644
index 0000000..814acbf
--- /dev/null
+++ b/test/wasm/whole-archive.test
@@ -0,0 +1,34 @@
+RUN: llc -filetype=obj %p/Inputs/start.ll -o %t.o
+RUN: llc -filetype=obj %p/Inputs/ret32.ll -o %t.ret32.o
+RUN: rm -f %t.a
+RUN: llvm-ar rcs %t.a %t.ret32.o
+
+Should not add symbols from the archive by default as they are not required
+RUN: wasm-ld --no-gc-sections -o %t.wasm %t.o %t.a
+RUN: obj2yaml %t.wasm | FileCheck --check-prefix=NOTADDED %s
+NOTADDED: FunctionNames:
+NOTADDED-NOT: Name: ret32
+NOTADDED: ...
+
+Should add symbols from the archive if --whole-archive is used
+RUN: wasm-ld --no-gc-sections -o %t.wasm %t.o --whole-archive %t.a
+RUN: obj2yaml %t.wasm | FileCheck --check-prefix=ADDED %s
+ADDED: FunctionNames:
+ADDED:   Name: ret32
+ADDED: ...
+
+--no-whole-archive should restore default behaviour
+RUN: wasm-ld --no-gc-sections -o %t.wasm %t.o --whole-archive --no-whole-archive %t.a
+RUN: obj2yaml %t.wasm | FileCheck --check-prefix=NOTADDED %s
+
+--whole-archive and --no-whole-archive should affect only archives which follow them
+RUN: wasm-ld --no-gc-sections -o %t.wasm %t.o %t.a --whole-archive --no-whole-archive
+RUN: obj2yaml %t.wasm | FileCheck --check-prefix=NOTADDED %s
+RUN: wasm-ld --no-gc-sections -o %t.wasm %t.o --whole-archive %t.a --no-whole-archive
+RUN: obj2yaml %t.wasm | FileCheck --check-prefix=ADDED %s
+
+--whole-archive should also work with thin archives
+RUN: rm -f %tthin.a
+RUN: llvm-ar --format=gnu rcsT %tthin.a %t.ret32.o
+RUN: wasm-ld --no-gc-sections -o %t.wasm %t.o --whole-archive %tthin.a
+RUN: obj2yaml %t.wasm | FileCheck --check-prefix=ADDED %s
diff --git a/tools/lld/lld.cpp b/tools/lld/lld.cpp
index 2c00a13..4a8e3f7 100644
--- a/tools/lld/lld.cpp
+++ b/tools/lld/lld.cpp
@@ -7,12 +7,22 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// This is the entry point to the lld driver. This is a thin wrapper which
-// dispatches to the given platform specific driver.
+// This file contains the main function of the lld executable. The main
+// function is a thin wrapper which dispatches to the platform specific
+// driver.
 //
-// If there is -flavor option, it is dispatched according to the arguments.
-// If the flavor parameter is not present, then it is dispatched according
-// to argv[0].
+// lld is a single executable that contains four different linkers for ELF,
+// COFF, WebAssembly and Mach-O. The main function dispatches according to
+// argv[0] (i.e. command name). The most common name for each target is shown
+// below:
+//
+//  - ld.lld:    ELF (Unix)
+//  - ld64:      Mach-O (macOS)
+//  - lld-link:  COFF (Windows)
+//  - ld-wasm:   WebAssembly
+//
+// lld can be invoked as "lld" along with "-flavor" option. This is for
+// backward compatibility and not recommended.
 //
 //===----------------------------------------------------------------------===//
 
@@ -123,11 +133,12 @@
   case WinLink:
     return !coff::link(Args, canExitEarly());
   case Darwin:
-    return !mach_o::link(Args);
+    return !mach_o::link(Args, canExitEarly());
   case Wasm:
     return !wasm::link(Args, canExitEarly());
   default:
     die("lld is a generic driver.\n"
-        "Invoke ld.lld (Unix), ld64.lld (macOS) or lld-link (Windows) instead.");
+        "Invoke ld.lld (Unix), ld64.lld (macOS), lld-link (Windows), wasm-lld"
+        " (WebAssembly) instead");
   }
 }
diff --git a/unittests/DriverTests/DarwinLdDriverTest.cpp b/unittests/DriverTests/DarwinLdDriverTest.cpp
index 0c1d779..e2e634a 100644
--- a/unittests/DriverTests/DarwinLdDriverTest.cpp
+++ b/unittests/DriverTests/DarwinLdDriverTest.cpp
@@ -23,8 +23,7 @@
 
 namespace lld {
 namespace mach_o {
-bool parse(llvm::ArrayRef<const char *> args, MachOLinkingContext &ctx,
-           raw_ostream &diagnostics);
+bool parse(llvm::ArrayRef<const char *> args, MachOLinkingContext &ctx);
 }
 }
 
@@ -42,9 +41,7 @@
 
   bool parse(std::vector<const char *> args) {
     args.insert(args.begin(), "ld");
-    std::string errorMessage;
-    raw_string_ostream os(errorMessage);
-    return mach_o::parse(args, _ctx, os);
+    return mach_o::parse(args, _ctx);
   }
 
   MachOLinkingContext _ctx;
diff --git a/wasm/CMakeLists.txt b/wasm/CMakeLists.txt
index 308c4e2..1a9e09b 100644
--- a/wasm/CMakeLists.txt
+++ b/wasm/CMakeLists.txt
@@ -2,6 +2,10 @@
 tablegen(LLVM Options.inc -gen-opt-parser-defs)
 add_public_tablegen_target(WasmOptionsTableGen)
 
+if(NOT LLD_BUILT_STANDALONE)
+  set(tablegen_deps intrinsics_gen)
+endif()
+
 add_lld_library(lldWasm
   Driver.cpp
   InputChunks.cpp
@@ -27,4 +31,8 @@
 
   LINK_LIBS
   lldCommon
-  )
+
+  DEPENDS
+  WasmOptionsTableGen
+  ${tablegen_deps}
+  )
\ No newline at end of file
diff --git a/wasm/Config.h b/wasm/Config.h
index 4b11320..76a7805 100644
--- a/wasm/Config.h
+++ b/wasm/Config.h
@@ -23,6 +23,7 @@
   bool CompressRelocTargets;
   bool Demangle;
   bool DisableVerify;
+  bool ExportAll;
   bool ExportTable;
   bool GcSections;
   bool ImportMemory;
diff --git a/wasm/Driver.cpp b/wasm/Driver.cpp
index 5a9fe36..3c542ed 100644
--- a/wasm/Driver.cpp
+++ b/wasm/Driver.cpp
@@ -68,13 +68,17 @@
   void createFiles(opt::InputArgList &Args);
   void addFile(StringRef Path);
   void addLibrary(StringRef Name);
+
+  // True if we are in --whole-archive and --no-whole-archive.
+  bool InWholeArchive = false;
+
   std::vector<InputFile *> Files;
 };
 } // anonymous namespace
 
 bool lld::wasm::link(ArrayRef<const char *> Args, bool CanExitEarly,
                      raw_ostream &Error) {
-  errorHandler().LogName = Args[0];
+  errorHandler().LogName = sys::path::filename(Args[0]);
   errorHandler().ErrorOS = &Error;
   errorHandler().ColorDiagnostics = Error.has_colors();
   errorHandler().ErrorLimitExceededMsg =
@@ -180,6 +184,37 @@
       Config->AllowUndefinedSymbols.insert(Sym);
 }
 
+// Returns slices of MB by parsing MB as an archive file.
+// Each slice consists of a member file in the archive.
+std::vector<MemoryBufferRef> static getArchiveMembers(
+    MemoryBufferRef MB) {
+  std::unique_ptr<Archive> File =
+      CHECK(Archive::create(MB),
+            MB.getBufferIdentifier() + ": failed to parse archive");
+
+  std::vector<MemoryBufferRef> V;
+  Error Err = Error::success();
+  for (const ErrorOr<Archive::Child> &COrErr : File->children(Err)) {
+    Archive::Child C =
+        CHECK(COrErr, MB.getBufferIdentifier() +
+                          ": could not get the child of the archive");
+    MemoryBufferRef MBRef =
+        CHECK(C.getMemoryBufferRef(),
+              MB.getBufferIdentifier() +
+                  ": could not get the buffer for a child of the archive");
+    V.push_back(MBRef);
+  }
+  if (Err)
+    fatal(MB.getBufferIdentifier() + ": Archive::children failed: " +
+          toString(std::move(Err)));
+
+  // Take ownership of memory buffers created for members of thin archives.
+  for (std::unique_ptr<MemoryBuffer> &MB : File->takeThinBuffers())
+    make<std::unique_ptr<MemoryBuffer>>(std::move(MB));
+
+  return V;
+}
+
 void LinkerDriver::addFile(StringRef Path) {
   Optional<MemoryBufferRef> Buffer = readFile(Path);
   if (!Buffer.hasValue())
@@ -188,6 +223,13 @@
 
   switch (identify_magic(MBRef.getBuffer())) {
   case file_magic::archive: {
+    // Handle -whole-archive.
+    if (InWholeArchive) {
+      for (MemoryBufferRef &M : getArchiveMembers(MBRef))
+        Files.push_back(createObjectFile(M));
+      return;
+    }
+
     SmallString<128> ImportFile = Path;
     path::replace_extension(ImportFile, ".imports");
     if (fs::exists(ImportFile))
@@ -197,10 +239,11 @@
     return;
   }
   case file_magic::bitcode:
-    Files.push_back(make<BitcodeFile>(MBRef));
+  case file_magic::wasm_object:
+    Files.push_back(createObjectFile(MBRef));
     break;
   default:
-    Files.push_back(make<ObjFile>(MBRef));
+    error("unknown file type: " + MBRef.getBufferIdentifier());
   }
 }
 
@@ -225,6 +268,12 @@
     case OPT_INPUT:
       addFile(Arg->getValue());
       break;
+    case OPT_whole_archive:
+      InWholeArchive = true;
+      break;
+    case OPT_no_whole_archive:
+      InWholeArchive = false;
+      break;
     }
   }
 }
@@ -257,8 +306,8 @@
 
     // It is possible for undefined functions not to have a signature (eg. if
     // added via "--undefined"), but weak undefined ones do have a signature.
-    assert(FuncSym->getFunctionType());
-    const WasmSignature &Sig = *FuncSym->getFunctionType();
+    assert(FuncSym->FunctionType);
+    const WasmSignature &Sig = *FuncSym->FunctionType;
 
     // Add a synthetic dummy for weak undefined functions.  These dummies will
     // be GC'd if not used as the target of any "call" instructions.
@@ -280,14 +329,19 @@
 }
 
 // Force Sym to be entered in the output. Used for -u or equivalent.
-static Symbol *addUndefined(StringRef Name) {
-  Symbol *S = Symtab->addUndefinedFunction(Name, 0, nullptr, nullptr);
+static Symbol *handleUndefined(StringRef Name) {
+  Symbol *Sym = Symtab->find(Name);
+  if (!Sym)
+    return nullptr;
 
   // Since symbol S may not be used inside the program, LTO may
   // eliminate it. Mark the symbol as "used" to prevent it.
-  S->IsUsedInRegularObj = true;
+  Sym->IsUsedInRegularObj = true;
 
-  return S;
+  if (auto *LazySym = dyn_cast<LazySymbol>(Sym))
+    LazySym->fetch();
+
+  return Sym;
 }
 
 void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
@@ -319,6 +373,7 @@
   Config->Demangle = Args.hasFlag(OPT_demangle, OPT_no_demangle, true);
   Config->DisableVerify = Args.hasArg(OPT_disable_verify);
   Config->Entry = getEntry(Args, Args.hasArg(OPT_relocatable) ? "" : "_start");
+  Config->ExportAll = Args.hasArg(OPT_export_all);
   Config->ExportTable = Args.hasArg(OPT_export_table);
   errorHandler().FatalWarnings =
       Args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false);
@@ -389,10 +444,6 @@
 
   Symbol *EntrySym = nullptr;
   if (!Config->Relocatable) {
-    // Can't export the SP right now because it's mutable, and mutable
-    // globals aren't yet supported in the official binary format.
-    // TODO(sbc): Remove WASM_SYMBOL_VISIBILITY_HIDDEN if/when the
-    // "mutable global" proposal is accepted.
     llvm::wasm::WasmGlobal Global;
     Global.Type = {WASM_TYPE_I32, true};
     Global.InitExpr.Value.Int32 = 0;
@@ -407,21 +458,15 @@
     WasmSym::CallCtors = Symtab->addSyntheticFunction(
         "__wasm_call_ctors", WASM_SYMBOL_VISIBILITY_HIDDEN,
         make<SyntheticFunction>(NullSignature, "__wasm_call_ctors"));
+    // TODO(sbc): Remove WASM_SYMBOL_VISIBILITY_HIDDEN when the mutable global
+    // spec proposal is implemented in all major browsers.
+    // See: https://github.com/WebAssembly/mutable-global
     WasmSym::StackPointer = Symtab->addSyntheticGlobal(
         "__stack_pointer", WASM_SYMBOL_VISIBILITY_HIDDEN, StackPointer);
     WasmSym::HeapBase = Symtab->addSyntheticDataSymbol("__heap_base", 0);
     WasmSym::DsoHandle = Symtab->addSyntheticDataSymbol(
         "__dso_handle", WASM_SYMBOL_VISIBILITY_HIDDEN);
     WasmSym::DataEnd = Symtab->addSyntheticDataSymbol("__data_end", 0);
-
-    // For now, since we don't actually use the start function as the
-    // wasm start symbol, we don't need to care about it signature.
-    if (!Config->Entry.empty())
-      EntrySym = addUndefined(Config->Entry);
-
-    // Handle the `--undefined <sym>` options.
-    for (auto *Arg : Args.filtered(OPT_undefined))
-      addUndefined(Arg->getValue());
   }
 
   createFiles(Args);
@@ -435,47 +480,46 @@
   if (errorCount())
     return;
 
-  // Add synthetic dummies for weak undefined functions.
-  if (!Config->Relocatable)
+  // Handle the `--undefined <sym>` options.
+  for (auto *Arg : Args.filtered(OPT_undefined))
+    handleUndefined(Arg->getValue());
+
+  // Handle the `--export <sym>` options
+  // This works like --undefined but also exports the symbol if its found
+  for (auto *Arg : Args.filtered(OPT_export)) {
+    Symbol *Sym = handleUndefined(Arg->getValue());
+    if (Sym && Sym->isDefined())
+      Sym->ForceExport = true;
+    else if (!Config->AllowUndefined)
+      error(Twine("symbol exported via --export not found: ") +
+            Arg->getValue());
+  }
+
+  if (!Config->Relocatable) {
+    // Add synthetic dummies for weak undefined functions.
     handleWeakUndefines();
 
+    if (!Config->Entry.empty()) {
+      EntrySym = handleUndefined(Config->Entry);
+      if (!EntrySym)
+        error("entry symbol not defined (pass --no-entry to supress): " +
+              Config->Entry);
+    }
+
+    // Make sure we have resolved all symbols.
+    if (!Config->AllowUndefined)
+      Symtab->reportRemainingUndefines();
+  }
+
+  if (errorCount())
+    return;
+
   // Do link-time optimization if given files are LLVM bitcode files.
   // This compiles bitcode files into real object files.
   Symtab->addCombinedLTOObject();
   if (errorCount())
     return;
 
-  // Make sure we have resolved all symbols.
-  if (!Config->Relocatable && !Config->AllowUndefined) {
-    Symtab->reportRemainingUndefines();
-  } else {
-    // Even when using --allow-undefined we still want to report the absence of
-    // our initial set of undefined symbols (i.e. the entry point and symbols
-    // specified via --undefined).
-    // Part of the reason for this is that these function don't have signatures
-    // so which means they cannot be written as wasm function imports.
-    for (auto *Arg : Args.filtered(OPT_undefined)) {
-      Symbol *Sym = Symtab->find(Arg->getValue());
-      if (!Sym->isDefined())
-        error("symbol forced with --undefined not found: " + Sym->getName());
-    }
-    if (EntrySym && !EntrySym->isDefined())
-      error("entry symbol not defined (pass --no-entry to supress): " +
-            EntrySym->getName());
-  }
-  if (errorCount())
-    return;
-
-  // Handle --export.
-  for (auto *Arg : Args.filtered(OPT_export)) {
-    StringRef Name = Arg->getValue();
-    Symbol *Sym = Symtab->find(Name);
-    if (Sym && Sym->isDefined())
-      Sym->setHidden(false);
-    else if (!Config->AllowUndefined)
-      error("symbol exported via --export not found: " + Name);
-  }
-
   if (EntrySym)
     EntrySym->setHidden(false);
 
diff --git a/wasm/InputFiles.cpp b/wasm/InputFiles.cpp
index ee9e99b..53a24c3 100644
--- a/wasm/InputFiles.cpp
+++ b/wasm/InputFiles.cpp
@@ -42,6 +42,17 @@
   return MBRef;
 }
 
+InputFile *lld::wasm::createObjectFile(MemoryBufferRef MB) {
+  file_magic Magic = identify_magic(MB.getBuffer());
+  if (Magic == file_magic::wasm_object)
+    return make<ObjFile>(MB);
+
+  if (Magic == file_magic::bitcode)
+    return make<BitcodeFile>(MB);
+
+  fatal("unknown file type: " + MB.getBufferIdentifier());
+}
+
 void ObjFile::dumpInfo() const {
   log("info for: " + getName() +
       "\n              Symbols : " + Twine(Symbols.size()) +
@@ -360,13 +371,8 @@
             "could not get the buffer for the member defining symbol " +
                 Sym->getName());
 
-  if (identify_magic(MB.getBuffer()) != file_magic::wasm_object) {
-    error("unknown file type: " + MB.getBufferIdentifier());
-    return;
-  }
-
-  InputFile *Obj = make<ObjFile>(MB);
-  Obj->ParentName = ParentName;
+  InputFile *Obj = createObjectFile(MB);
+  Obj->ArchiveName = getName();
   Symtab->addFile(Obj);
 }
 
@@ -401,7 +407,7 @@
 
 void BitcodeFile::parse() {
   Obj = check(lto::InputFile::create(MemoryBufferRef(
-      MB.getBuffer(), Saver.save(ParentName + MB.getBufferIdentifier()))));
+      MB.getBuffer(), Saver.save(ArchiveName + MB.getBufferIdentifier()))));
   Triple T(Obj->getTargetTriple());
   if (T.getArch() != Triple::wasm32) {
     error(toString(MB.getBufferIdentifier()) + ": machine type must be wasm32");
@@ -417,8 +423,8 @@
   if (!File)
     return "<internal>";
 
-  if (File->ParentName.empty())
+  if (File->ArchiveName.empty())
     return File->getName();
 
-  return (File->ParentName + "(" + File->getName() + ")").str();
+  return (File->ArchiveName + "(" + File->getName() + ")").str();
 }
diff --git a/wasm/InputFiles.h b/wasm/InputFiles.h
index 75d20e6..ec77446 100644
--- a/wasm/InputFiles.h
+++ b/wasm/InputFiles.h
@@ -63,7 +63,7 @@
   Kind kind() const { return FileKind; }
 
   // An archive file name if this file is created from an archive.
-  StringRef ParentName;
+  StringRef ArchiveName;
 
   ArrayRef<Symbol *> getSymbols() const { return Symbols; }
 
@@ -150,6 +150,10 @@
   std::unique_ptr<llvm::lto::InputFile> Obj;
 };
 
+// Will report a fatal() error if the input buffer is not a valid bitcode
+// or was object file.
+InputFile *createObjectFile(MemoryBufferRef MB);
+
 // Opens a given file.
 llvm::Optional<MemoryBufferRef> readFile(StringRef Path);
 
diff --git a/wasm/LTO.cpp b/wasm/LTO.cpp
index 58f32aa..f15551d 100644
--- a/wasm/LTO.cpp
+++ b/wasm/LTO.cpp
@@ -45,10 +45,13 @@
   lto::Config C;
   C.Options = InitTargetOptionsFromCodeGenFlags();
 
-  // Always emit a section per function/datum with LTO.
+  // Always emit a section per function/data with LTO.
   C.Options.FunctionSections = true;
   C.Options.DataSections = true;
 
+  // Wasm currently only supports ThreadModel::Single
+  C.Options.ThreadModel = ThreadModel::Single;
+
   C.DisableVerify = Config->DisableVerify;
   C.DiagHandler = diagnosticHandler;
   C.OptLevel = Config->LTOO;
@@ -95,7 +98,8 @@
     // Once IRObjectFile is fixed to report only one symbol this hack can
     // be removed.
     R.Prevailing = !ObjSym.isUndefined() && Sym->getFile() == &F;
-    R.VisibleToRegularObj = Config->Relocatable || Sym->IsUsedInRegularObj;
+    R.VisibleToRegularObj = Config->Relocatable || Sym->IsUsedInRegularObj ||
+                            (R.Prevailing && Sym->isExported());
     if (R.Prevailing)
       undefine(Sym);
   }
diff --git a/wasm/MarkLive.cpp b/wasm/MarkLive.cpp
index 7c4670d..dfaa712 100644
--- a/wasm/MarkLive.cpp
+++ b/wasm/MarkLive.cpp
@@ -43,6 +43,7 @@
   auto Enqueue = [&](Symbol *Sym) {
     if (!Sym || Sym->isLive())
       return;
+    LLVM_DEBUG(dbgs() << "markLive: " << Sym->getName() << "\n");
     Sym->markLive();
     if (InputChunk *Chunk = Sym->getChunk())
       Q.push_back(Chunk);
@@ -53,9 +54,9 @@
     Enqueue(Symtab->find(Config->Entry));
   Enqueue(WasmSym::CallCtors);
 
-  // By default we export all non-hidden, so they are gc roots too
+  // We need to preserve any exported symbol
   for (Symbol *Sym : Symtab->getSymbols())
-    if (!Sym->isHidden())
+    if (Sym->isExported())
       Enqueue(Sym);
 
   // The ctor functions are all used in the synthetic __wasm_call_ctors
diff --git a/wasm/Options.td b/wasm/Options.td
index 4dbed35..43588a8 100644
--- a/wasm/Options.td
+++ b/wasm/Options.td
@@ -105,6 +105,9 @@
 defm export: Eq<"export">,
   HelpText<"Force a symbol to be exported">;
 
+def export_all: F<"export-all">,
+  HelpText<"Export all symbols (normally combined with --no-gc-sections)">;
+
 def export_table: F<"export-table">,
   HelpText<"Export function table to the environment">;
 
@@ -129,6 +132,10 @@
 def stack_first: F<"stack-first">,
   HelpText<"Place stack at start of linear memory rather than after data">;
 
+defm whole_archive: B<"whole-archive",
+    "Force load of all members in a static library",
+    "Do not force load of all members in a static library (default)">;
+
 // Aliases
 def alias_entry_e: JoinedOrSeparate<["-"], "e">, Alias<entry>;
 def alias_entry_entry: J<"entry=">, Alias<entry>;
diff --git a/wasm/SymbolTable.cpp b/wasm/SymbolTable.cpp
index 6cc7c36..2a4249e 100644
--- a/wasm/SymbolTable.cpp
+++ b/wasm/SymbolTable.cpp
@@ -60,7 +60,6 @@
 }
 
 void SymbolTable::reportRemainingUndefines() {
-  SetVector<Symbol *> Undefs;
   for (Symbol *Sym : SymVector) {
     if (!Sym->isUndefined() || Sym->isWeak())
       continue;
@@ -68,34 +67,26 @@
       continue;
     if (!Sym->IsUsedInRegularObj)
       continue;
-    Undefs.insert(Sym);
+    error(toString(Sym->getFile()) + ": undefined symbol: " + toString(*Sym));
   }
-
-  if (Undefs.empty())
-    return;
-
-  for (ObjFile *File : ObjectFiles)
-    for (Symbol *Sym : File->getSymbols())
-      if (Undefs.count(Sym))
-        error(toString(File) + ": undefined symbol: " + toString(*Sym));
-
-  for (Symbol *Sym : Undefs)
-    if (!Sym->getFile())
-      error("undefined symbol: " + toString(*Sym));
 }
 
 Symbol *SymbolTable::find(StringRef Name) {
   return SymMap.lookup(CachedHashStringRef(Name));
 }
 
-std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name) {
+std::pair<Symbol *, bool> SymbolTable::insert(StringRef Name, InputFile *File) {
+  bool Inserted = false;
   Symbol *&Sym = SymMap[CachedHashStringRef(Name)];
-  if (Sym)
-    return {Sym, false};
-  Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
-  Sym->IsUsedInRegularObj = false;
-  SymVector.emplace_back(Sym);
-  return {Sym, true};
+  if (!Sym) {
+    Sym = reinterpret_cast<Symbol *>(make<SymbolUnion>());
+    Sym->IsUsedInRegularObj = false;
+    SymVector.emplace_back(Sym);
+    Inserted = true;
+  }
+  if (!File || File->kind() == InputFile::ObjectKind)
+    Sym->IsUsedInRegularObj = true;
+  return {Sym, Inserted};
 }
 
 static void reportTypeError(const Symbol *Existing, const InputFile *File,
@@ -106,7 +97,7 @@
         " in " + toString(File));
 }
 
-static void checkFunctionType(const Symbol *Existing, const InputFile *File,
+static void checkFunctionType(Symbol *Existing, const InputFile *File,
                               const WasmSignature *NewSig) {
   auto ExistingFunction = dyn_cast<FunctionSymbol>(Existing);
   if (!ExistingFunction) {
@@ -114,13 +105,20 @@
     return;
   }
 
-  const WasmSignature *OldSig = ExistingFunction->getFunctionType();
-  if (OldSig && NewSig && *NewSig != *OldSig) {
-    warn("Function type mismatch: " + Existing->getName() +
+  if (!NewSig)
+    return;
+
+  const WasmSignature *OldSig = ExistingFunction->FunctionType;
+  if (!OldSig) {
+    ExistingFunction->FunctionType = NewSig;
+    return;
+  }
+
+  if (*NewSig != *OldSig)
+    warn("function signature mismatch: " + Existing->getName() +
          "\n>>> defined as " + toString(*OldSig) + " in " +
          toString(Existing->getFile()) + "\n>>> defined as " +
          toString(*NewSig) + " in " + toString(File));
-  }
 }
 
 // Check the type of new symbol matches that of the symbol is replacing.
@@ -151,15 +149,15 @@
   LLVM_DEBUG(dbgs() << "addSyntheticFunction: " << Name << "\n");
   assert(!find(Name));
   SyntheticFunctions.emplace_back(Function);
-  return replaceSymbol<DefinedFunction>(insert(Name).first, Name, Flags,
-                                        nullptr, Function);
+  return replaceSymbol<DefinedFunction>(insert(Name, nullptr).first, Name,
+                                        Flags, nullptr, Function);
 }
 
 DefinedData *SymbolTable::addSyntheticDataSymbol(StringRef Name,
                                                  uint32_t Flags) {
   LLVM_DEBUG(dbgs() << "addSyntheticDataSymbol: " << Name << "\n");
   assert(!find(Name));
-  return replaceSymbol<DefinedData>(insert(Name).first, Name, Flags);
+  return replaceSymbol<DefinedData>(insert(Name, nullptr).first, Name, Flags);
 }
 
 DefinedGlobal *SymbolTable::addSyntheticGlobal(StringRef Name, uint32_t Flags,
@@ -168,8 +166,8 @@
                     << "\n");
   assert(!find(Name));
   SyntheticGlobals.emplace_back(Global);
-  return replaceSymbol<DefinedGlobal>(insert(Name).first, Name, Flags, nullptr,
-                                      Global);
+  return replaceSymbol<DefinedGlobal>(insert(Name, nullptr).first, Name, Flags,
+                                      nullptr, Global);
 }
 
 static bool shouldReplace(const Symbol *Existing, InputFile *NewFile,
@@ -206,10 +204,7 @@
   LLVM_DEBUG(dbgs() << "addDefinedFunction: " << Name << "\n");
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(Name);
-
-  if (!File || File->kind() == InputFile::ObjectKind)
-    S->IsUsedInRegularObj = true;
+  std::tie(S, WasInserted) = insert(Name, File);
 
   if (WasInserted || S->isLazy()) {
     replaceSymbol<DefinedFunction>(S, Name, Flags, File, Function);
@@ -231,10 +226,7 @@
                     << "\n");
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(Name);
-
-  if (!File || File->kind() == InputFile::ObjectKind)
-    S->IsUsedInRegularObj = true;
+  std::tie(S, WasInserted) = insert(Name, File);
 
   if (WasInserted || S->isLazy()) {
     replaceSymbol<DefinedData>(S, Name, Flags, File, Segment, Address, Size);
@@ -251,12 +243,10 @@
 Symbol *SymbolTable::addDefinedGlobal(StringRef Name, uint32_t Flags,
                                       InputFile *File, InputGlobal *Global) {
   LLVM_DEBUG(dbgs() << "addDefinedGlobal:" << Name << "\n");
+
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(Name);
-
-  if (!File || File->kind() == InputFile::ObjectKind)
-    S->IsUsedInRegularObj = true;
+  std::tie(S, WasInserted) = insert(Name, File);
 
   if (WasInserted || S->isLazy()) {
     replaceSymbol<DefinedGlobal>(S, Name, Flags, File, Global);
@@ -277,17 +267,15 @@
 
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(Name);
-
-  if (!File || File->kind() == InputFile::ObjectKind)
-    S->IsUsedInRegularObj = true;
+  std::tie(S, WasInserted) = insert(Name, File);
 
   if (WasInserted)
     replaceSymbol<UndefinedFunction>(S, Name, Flags, File, Sig);
   else if (auto *Lazy = dyn_cast<LazySymbol>(S))
     Lazy->fetch();
-  else if (S->isDefined())
+  else
     checkFunctionType(S, File, Sig);
+
   return S;
 }
 
@@ -297,7 +285,7 @@
 
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(Name);
+  std::tie(S, WasInserted) = insert(Name, File);
 
   if (WasInserted)
     replaceSymbol<UndefinedData>(S, Name, Flags, File);
@@ -315,10 +303,7 @@
 
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(Name);
-
-  if (!File || File->kind() == InputFile::ObjectKind)
-    S->IsUsedInRegularObj = true;
+  std::tie(S, WasInserted) = insert(Name, File);
 
   if (WasInserted)
     replaceSymbol<UndefinedGlobal>(S, Name, Flags, File, Type);
@@ -335,7 +320,7 @@
 
   Symbol *S;
   bool WasInserted;
-  std::tie(S, WasInserted) = insert(Name);
+  std::tie(S, WasInserted) = insert(Name, nullptr);
 
   if (WasInserted) {
     replaceSymbol<LazySymbol>(S, Name, File, *Sym);
diff --git a/wasm/SymbolTable.h b/wasm/SymbolTable.h
index 26242e6..de4b716 100644
--- a/wasm/SymbolTable.h
+++ b/wasm/SymbolTable.h
@@ -77,7 +77,7 @@
                                         InputFunction *Function);
 
 private:
-  std::pair<Symbol *, bool> insert(StringRef Name);
+  std::pair<Symbol *, bool> insert(StringRef Name, InputFile *File);
 
   llvm::DenseMap<llvm::CachedHashStringRef, Symbol *> SymMap;
   std::vector<Symbol *> SymVector;
diff --git a/wasm/Symbols.cpp b/wasm/Symbols.cpp
index 6b43eb3..a11081c 100644
--- a/wasm/Symbols.cpp
+++ b/wasm/Symbols.cpp
@@ -98,6 +98,16 @@
     Flags |= WASM_SYMBOL_VISIBILITY_DEFAULT;
 }
 
+bool Symbol::isExported() const {
+  if (!isDefined() || isLocal())
+    return false;
+
+  if (ForceExport || Config->ExportAll)
+    return true;
+
+  return !isHidden();
+}
+
 uint32_t FunctionSymbol::getFunctionIndex() const {
   if (auto *F = dyn_cast<DefinedFunction>(this))
     return F->Function->getFunctionIndex();
diff --git a/wasm/Symbols.h b/wasm/Symbols.h
index b2ec1e8..815cc97 100644
--- a/wasm/Symbols.h
+++ b/wasm/Symbols.h
@@ -90,21 +90,23 @@
   void setOutputSymbolIndex(uint32_t Index);
 
   WasmSymbolType getWasmType() const;
+  bool isExported() const;
 
   // True if this symbol was referenced by a regular (non-bitcode) object.
   unsigned IsUsedInRegularObj : 1;
+  unsigned ForceExport : 1;
 
 protected:
   Symbol(StringRef Name, Kind K, uint32_t Flags, InputFile *F)
-      : IsUsedInRegularObj(false), Name(Name), SymbolKind(K), Flags(Flags),
-        File(F), Referenced(!Config->GcSections) {}
+      : IsUsedInRegularObj(false), ForceExport(false), Name(Name),
+        SymbolKind(K), Flags(Flags), File(F), Referenced(!Config->GcSections) {}
 
   StringRef Name;
   Kind SymbolKind;
   uint32_t Flags;
   InputFile *File;
   uint32_t OutputSymbolIndex = INVALID_INDEX;
-  bool Referenced = false;
+  bool Referenced;
 };
 
 class FunctionSymbol : public Symbol {
@@ -114,8 +116,6 @@
            S->kind() == UndefinedFunctionKind;
   }
 
-  const WasmSignature *getFunctionType() const { return FunctionType; }
-
   // Get/set the table index
   void setTableIndex(uint32_t Index);
   uint32_t getTableIndex() const;
@@ -126,6 +126,8 @@
   void setFunctionIndex(uint32_t Index);
   bool hasFunctionIndex() const;
 
+  const WasmSignature *FunctionType;
+
 protected:
   FunctionSymbol(StringRef Name, Kind K, uint32_t Flags, InputFile *F,
                  const WasmSignature *Type)
@@ -133,8 +135,6 @@
 
   uint32_t TableIndex = INVALID_INDEX;
   uint32_t FunctionIndex = INVALID_INDEX;
-
-  const WasmSignature *FunctionType;
 };
 
 class DefinedFunction : public FunctionSymbol {
@@ -340,6 +340,7 @@
 
   T *S2 = new (S) T(std::forward<ArgT>(Arg)...);
   S2->IsUsedInRegularObj = SymCopy.IsUsedInRegularObj;
+  S2->ForceExport = SymCopy.ForceExport;
   return S2;
 }
 
diff --git a/wasm/Writer.cpp b/wasm/Writer.cpp
index ee6056a..23a7b5d 100644
--- a/wasm/Writer.cpp
+++ b/wasm/Writer.cpp
@@ -175,7 +175,7 @@
     Import.Field = Sym->getName();
     if (auto *FunctionSym = dyn_cast<FunctionSymbol>(Sym)) {
       Import.Kind = WASM_EXTERNAL_FUNCTION;
-      Import.SigIndex = lookupType(*FunctionSym->getFunctionType());
+      Import.SigIndex = lookupType(*FunctionSym->FunctionType);
     } else {
       auto *GlobalSym = cast<GlobalSymbol>(Sym);
       Import.Kind = WASM_EXTERNAL_GLOBAL;
@@ -740,9 +740,7 @@
   unsigned FakeGlobalIndex = NumImportedGlobals + InputGlobals.size();
 
   for (Symbol *Sym : Symtab->getSymbols()) {
-    if (!Sym->isDefined())
-      continue;
-    if (Sym->isHidden() || Sym->isLocal())
+    if (!Sym->isExported())
       continue;
     if (!Sym->isLive())
       continue;
@@ -752,6 +750,14 @@
     if (auto *F = dyn_cast<DefinedFunction>(Sym)) {
       Export = {Name, WASM_EXTERNAL_FUNCTION, F->getFunctionIndex()};
     } else if (auto *G = dyn_cast<DefinedGlobal>(Sym)) {
+      // TODO(sbc): Remove this check once to mutable global proposal is
+      // implement in all major browsers.
+      // See: https://github.com/WebAssembly/mutable-global
+      if (G->getGlobalType()->Mutable) {
+        // Only the __stack_pointer should ever be create as mutable.
+        assert(G == WasmSym::StackPointer);
+        continue;
+      }
       Export = {Name, WASM_EXTERNAL_GLOBAL, G->getGlobalIndex()};
     } else {
       auto *D = cast<DefinedData>(Sym);
@@ -840,7 +846,7 @@
 
   for (const Symbol *Sym : ImportedSymbols)
     if (auto *F = dyn_cast<FunctionSymbol>(Sym))
-      registerType(*F->getFunctionType());
+      registerType(*F->FunctionType);
 
   for (const InputFunction *F : InputFunctions)
     registerType(F->Signature);
@@ -924,6 +930,8 @@
     return ".data";
   if (Name.startswith(".bss."))
     return ".bss";
+  if (Name.startswith(".rodata."))
+    return ".rodata";
   return Name;
 }
 
@@ -983,7 +991,7 @@
     const WasmLinkingData &L = File->getWasmObj()->linkingData();
     for (const WasmInitFunc &F : L.InitFunctions) {
       FunctionSymbol *Sym = File->getFunctionSymbol(F.Symbol);
-      if (*Sym->getFunctionType() != WasmSignature{{}, WASM_TYPE_NORESULT})
+      if (*Sym->FunctionType != WasmSignature{{}, WASM_TYPE_NORESULT})
         error("invalid signature for init func: " + toString(*Sym));
       InitFunctions.emplace_back(WasmInitEntry{Sym, F.Priority});
     }