emmettneyman created this revision.
emmettneyman added reviewers: morehouse, kcc.
Herald added subscribers: cfe-commits, mgorny.
Herald added a reviewer: alexshap.

Made changes to the llvm-proto-fuzzer

- Added loop vectorizer optimization pass in order to have two IR versions
- Updated old fuzz target to handle two different IR versions
- Wrote code to execute both versions in memory


Repository:
  rC Clang

https://reviews.llvm.org/D49526

Files:
  clang/tools/clang-fuzzer/CMakeLists.txt
  clang/tools/clang-fuzzer/ExampleClangLLVMProtoFuzzer.cpp
  clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  clang/tools/clang-fuzzer/handle-llvm/Object.cpp
  clang/tools/clang-fuzzer/handle-llvm/Object.h
  clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.cpp
  clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.h
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.h
  clang/tools/clang-fuzzer/handle-llvm/llvm-objcopy.h

Index: clang/tools/clang-fuzzer/handle-llvm/llvm-objcopy.h
===================================================================
--- /dev/null
+++ clang/tools/clang-fuzzer/handle-llvm/llvm-objcopy.h
@@ -0,0 +1,42 @@
+//===- llvm-objcopy.h -------------------------------------------*- C++ -*-===//
+//
+//                      The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//      Modified version of llvm/tools/llvm-objcopy/llvm-objcopy.h
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TOOLS_OBJCOPY_OBJCOPY_H
+#define LLVM_TOOLS_OBJCOPY_OBJCOPY_H
+
+#include "llvm/ADT/Twine.h"
+#include "llvm/Support/Compiler.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/raw_ostream.h"
+#include <string>
+
+namespace llvm {
+
+LLVM_ATTRIBUTE_NORETURN extern void error(Twine Message);
+LLVM_ATTRIBUTE_NORETURN extern void reportError(StringRef File, Error E);
+LLVM_ATTRIBUTE_NORETURN extern void reportError(StringRef File,
+                                                std::error_code EC);
+
+// This is taken from llvm-readobj.
+// [see here](llvm/tools/llvm-readobj/llvm-readobj.h:38)
+template <class T> T unwrapOrError(Expected<T> EO) {
+  if (EO)
+    return *EO;
+  std::string Buf;
+  raw_string_ostream OS(Buf);
+  logAllUnhandledErrors(EO.takeError(), OS, "");
+  OS.flush();
+  error(Buf);
+}
+
+} // end namespace llvm
+
+#endif // LLVM_TOOLS_OBJCOPY_OBJCOPY_H
Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===================================================================
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -7,33 +7,56 @@
 //
 //===----------------------------------------------------------------------===//
 //
-// Implements HandleLLVM for use by the Clang fuzzers. Mimics the llc tool to
-// compile an LLVM IR file to X86_64 assembly.
+// Implements HandleLLVM for use by the Clang fuzzers. First runs an loop
+// vectorizer optimization pass over the given IR code. Then mimics llc on both
+// versions of the IR code to genereate machine code for each version. Inserts
+// both versions of the code into memory and executes both functions on dummy
+// inputs.
 //
 //===----------------------------------------------------------------------===//
 
 #include "handle_llvm.h"
+#include "Object.h"
 
 #include "llvm/ADT/Triple.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
+#include "llvm/BinaryFormat/ELF.h"
 #include "llvm/CodeGen/CommandFlags.inc"
 #include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/LegacyPassNameParser.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Object/Binary.h"
+#include "llvm/Object/ELFObjectFile.h"
+#include "llvm/Object/ELFTypes.h"
+#include "llvm/Pass.h"
 #include "llvm/PassRegistry.h"
-#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+#include "llvm/Transforms/IPO.h"
 
 #include <cstdlib>
 
 using namespace llvm;
+using namespace object;
+using namespace ELF;
 
+static cl::list<const PassInfo*, bool, PassNameParser>
+PassList(cl::desc("Optimizations available:"));
+
+static std::string OptIR;
+
+// Helper function to parse command line args and find the optimization level
 static void getOptLevel(const std::vector<const char *> &ExtraArgs,
                               CodeGenOpt::Level &OLvl) {
   // Find the optimization level from the command line args
@@ -53,23 +76,243 @@
   }
 }
 
-void clang_fuzzer::HandleLLVM(const std::string &S,
-                              const std::vector<const char *> &ExtraArgs) {
+// Helper function to call pass initialization functions
+void InitEverything() {
+  PassRegistry &Registry = *PassRegistry::getPassRegistry();
+  initializeCore(Registry);
+  initializeScalarOpts(Registry);
+  initializeVectorization(Registry);
+  initializeIPO(Registry);
+  initializeAnalysis(Registry);
+  initializeTransformUtils(Registry);
+  initializeInstCombine(Registry);
+  initializeAggressiveInstCombine(Registry);
+  initializeInstrumentation(Registry);
+  initializeTarget(Registry);
+}
+
+// Helper function to add optimization passes to the TargetMachine at the 
+// specified optimization level, OptLevel
+static void AddOptimizationPasses(legacy::PassManagerBase &MPM,
+                                  legacy::FunctionPassManager &FPM,
+                                  unsigned OptLevel, unsigned SizeLevel) {
+  // Verify that input is correct by adding a verifier pass
+  FPM.add(createVerifierPass());
+
+  // Create and initializa a PassManagerBuilder
+  PassManagerBuilder Builder;
+  Builder.OptLevel = OptLevel;
+  Builder.SizeLevel = SizeLevel;
+  Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
+  Builder.LoopVectorize = true;
+  Builder.populateFunctionPassManager(FPM);
+  Builder.populateModulePassManager(MPM);
+}
+
+// Mimics the opt tool to run an optimization pass over the provided IR
+void OptLLVM(const std::string IR, CodeGenOpt::Level &OLvl) {
+  InitEverything();
+ 
+  // Mimic argc and argv and pass them to ParseCommandLineOptions to initilize
+  // PassList, ie which optimizations we want to run on the IR
+  // TODO: Find a better way of doing this
+  char *args[2];
+  char t[18] = "llvm-proto-fuzzer";
+  char s[16] = "-loop-vectorize";
+  args[0] = t;
+  args[1] = s;
+  cl::ParseCommandLineOptions(2, args, "");
+
+  // Create a module that will run the optimization passes
+  SMDiagnostic Err;
+  LLVMContext Context;
+  std::unique_ptr<Module> M = parseIR(MemoryBufferRef(IR, "IR"), Err, Context);
+  if (!M || verifyModule(*M, &errs())) {
+    errs() << "error: could not parse IR!\n";
+    std::exit(1);
+  }
+
+  // 
+  Triple ModuleTriple(M->getTargetTriple());
+  TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
+  std::string CPUStr;
+  std::string FeaturesStr;
+  TargetMachine *Machine = nullptr;
+  std::unique_ptr<TargetMachine> TM(Machine);
+  setFunctionAttributes(CPUStr, FeaturesStr, *M);
+  
+  legacy::PassManager Passes;
+  TargetLibraryInfoImpl TLII(ModuleTriple);
+  Passes.add(new TargetLibraryInfoWrapperPass(TLII));
+  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  
+  std::unique_ptr<legacy::FunctionPassManager> FPasses;
+  FPasses.reset(new legacy::FunctionPassManager(M.get()));
+  FPasses->add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+
+  AddOptimizationPasses(Passes, *FPasses, 3, 0);
+  const PassInfo *PassInf = PassList[0];
+  Pass *P = nullptr;
+  
+  if (PassInf->getNormalCtor())
+    P = PassInf->getNormalCtor()();
+  else {
+    errs() << "cannot create pass: " << PassInf->getPassName() << "\n";
+    std::exit(1);
+  }
+
+  if (P)
+    Passes.add(P);
+
+  if (FPasses) {
+    FPasses->doInitialization();
+    for (Function &F : *M)
+      FPasses->run(F);
+    FPasses->doFinalization();
+  }
+  Passes.add(createVerifierPass());
+  
+  // Add a pass that writes the optimized IR to an output stream
+  std::string outString;
+  raw_string_ostream OS(outString);
+  Passes.add(createPrintModulePass(OS, "", false));
+
+  Passes.run(*M);
+
+  // Save the resulting IR to OptIR
+  OptIR = outString;
+}
+
+// Helper function that defines which sections of the ELF file should be kept
+// We only want to keep the ".text" section
+// Modified version of HandleArgs() function in llvm-objcopy.cpp
+void HandleArgs(Object &Obj) {
+  Obj.removeSymbols([&](const Symbol &Sym) {
+    return false;
+  });
+  std::function<bool(const SectionBase &Sec)> RemovePred =
+      [](const SectionBase &) { return false; };
+  RemovePred = [RemovePred, &Obj](const SectionBase &Sec) {
+    if (Sec.Name == ".text")
+      return false;
+    if (RemovePred(Sec))
+      return true;
+    if (Obj.SectionNames == &Sec)
+      return false;
+    if (Obj.SymbolTable == &Sec || Obj.SymbolTable->getStrTab() == &Sec)
+      return false;
+    return true;
+  };
+  Obj.removeSections(RemovePred);
+}
+
+// Helper function that converts ELF relocatable into raw machine code that
+// can be executed in memory. Returns size of machine code.
+// Mimics the llvm-objcopy tool
+int StripHeader(StringRef &str, MemBuffer &buf) {
+  // Create a Binary object from the input ELF string
+  Expected<std::unique_ptr<Binary>> BinaryOrErr = 
+      createBinary(MemoryBufferRef(str, "code"));
+  if (!BinaryOrErr) {
+    errs() << "error: could not create binary\n";
+    exit(1);
+  }
+  std::unique_ptr<Binary> &bptr = BinaryOrErr.get();
+  Binary *binary = std::move(bptr).get();
+  
+  // Read the binary code into an Object
+  ELFReader Reader(binary);
+  std::unique_ptr<Object> Obj = Reader.create();
+  if (!Obj) {
+    errs() << "error: could not create Object from binary\n";
+    std::exit(1);
+  }
+
+  // Pass the Obj to a helper function that defines which ELF sections to keep
+  HandleArgs(*Obj);
+
+  // Create a BinaryWriter that will write the machine code into buf
+  std::unique_ptr<Writer> Writer = llvm::make_unique<BinaryWriter>(*Obj.get(),
+                                                                   buf);
+  if (!Writer) {
+    errs() << "error: could not create binary writer\n";
+    std::exit(1);
+  }
+
+  // Writer->finalize() returns the size of the machine code
+  // i.e. how many bytes should be written into buf
+  int s = Writer->finalize();
+  Writer->write();
+  
+  return s;
+}
+
+// Function that inserts machine code into memory and executes it
+// TODO: Call each function on a suite of test inputs instead of dummy inputs
+void InsertCodeAndExec(char *m1, char *m2,
+                       char *str1, char *str2,
+                       int s1, int s2) {
+  // First, check that the memory regions are non-null
+  if (!m1 || !m2) {
+    errs() << "error: could not create exec memory region\n";
+    std::exit(1);
+  }
+  
+  // Fill both memory regions with trap calls
+  memset((void *) m1, 0xCC, 1000000);
+  memset((void *) m2, 0xCC, 1000000);
+
+  // Insert the machine code into both memory regions
+  memcpy((void *) m1, (const void *) str1, s1);
+  memcpy((void *) m2, (const void *) str2, s2);
+  
+  // Define some dummy arrays to use an input for now
+  int a[] = {1};
+  int b[] = {1};
+  int c[] = {1};
+
+  typedef void (*func)(int*, int*, int*, int);
+
+  // Call each function
+  func f1 = (func)(m1);
+  (*f1)(a, b, c, 1);
+  
+  func f2 = (func)(m2);
+  (*f2)(a, b, c, 1);
+  
+  return;
+}
+
+// Main fuzz target called by ExampleClangLLVMProtoFuzzer.cpp
+// Mimics the llc tool to generate machine code from provided IR
+void clang_fuzzer::HandleLLVM(const std::string &IR,
+                              const std::vector<const char *> &ExtraArgs,
+                              char *m1, char *m2) {
   // Parse ExtraArgs to set the optimization level
   CodeGenOpt::Level OLvl;
   getOptLevel(ExtraArgs, OLvl);
+  
+  // First we optimize the IR by running a loop vectorizer pass
+  OptLLVM(IR, OLvl);
 
-  // Set the Module to include the the IR code to be compiled
-  SMDiagnostic Err;
+  // If the optimized IR contains a call to an external function, just return
+  // We can't handle external function calls since we don't do any linking
+  if (OptIR.find("call") != std::string::npos)
+    return;
 
+  // Create and initialize two modules, one for the optimized code and one
+  // for the unoptimized code
+  SMDiagnostic Err;
   LLVMContext Context;
-  std::unique_ptr<Module> M = parseIR(MemoryBufferRef(S, "IR"), Err, Context);
-  if (!M) {
+  std::unique_ptr<Module> M1 = parseIR(MemoryBufferRef(OptIR, "IR"), Err,
+                                       Context);
+  std::unique_ptr<Module> M2 = parseIR(MemoryBufferRef(IR, "IR"), Err, Context);
+  if (!M1 || !M2) {
     errs() << "error: could not parse IR!\n";
     std::exit(1);
   }
 
-  // Create a new Target
+  // Create a new Target to be used by the two machines created below
   std::string Error;
   const Target *TheTarget = TargetRegistry::lookupTarget(
       sys::getDefaultTargetTriple(), Error);
@@ -80,31 +323,84 @@
 
   TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
 
-  // Create a new Machine
+  // Create and initialize two machines, T1 and T2
   std::string CPUStr = getCPUStr();
   std::string FeaturesStr = getFeaturesStr();
-  std::unique_ptr<TargetMachine> Target(TheTarget->createTargetMachine(
+  std::unique_ptr<TargetMachine> T1(TheTarget->createTargetMachine(
       sys::getDefaultTargetTriple(), CPUStr, FeaturesStr, Options,
       getRelocModel(), getCodeModel(), OLvl));
+  std::unique_ptr<TargetMachine> T2(TheTarget->createTargetMachine(
+      sys::getDefaultTargetTriple(), CPUStr, FeaturesStr, Options,
+      getRelocModel(), getCodeModel(), CodeGenOpt::None));
 
-  // Create a new PassManager
-  legacy::PassManager PM;
-  TargetLibraryInfoImpl TLII(Triple(M->getTargetTriple()));
-  PM.add(new TargetLibraryInfoWrapperPass(TLII));
-  M->setDataLayout(Target->createDataLayout());
+  if (!T1 || !T2) {
+    errs() << "error: could not create target machine\n";
+    std::exit(1);
+  }
+
+  // Create and initialize two pass managers, PM1 and PM2
+  legacy::PassManager PM1;
+  TargetLibraryInfoImpl TLII1(Triple(M1->getTargetTriple()));
+  PM1.add(new TargetLibraryInfoWrapperPass(TLII1));
+  M1->setDataLayout(T1->createDataLayout());
+ 
+  legacy::PassManager PM2;
+  TargetLibraryInfoImpl TLII2(Triple(M2->getTargetTriple()));
+  PM2.add(new TargetLibraryInfoWrapperPass(TLII2));
+  M2->setDataLayout(T2->createDataLayout());
  
-  // Make sure the Module has no errors
-  if (verifyModule(*M, &errs())) {
+  // Make sure the Modules have no errors
+  if (verifyModule(*M1, &errs())) {
+    errs() << "error: input module is broken!\n";
+    std::exit(1);
+  } 
+  if (verifyModule(*M2, &errs())) {
     errs() << "error: input module is broken!\n";
     std::exit(1);
   } 
 
-  setFunctionAttributes(CPUStr, FeaturesStr, *M);
+  setFunctionAttributes(CPUStr, FeaturesStr, *M1);
+  setFunctionAttributes(CPUStr, FeaturesStr, *M2);
+  
+  // Create the streams that will be written to during code generation
+  std::string s1;
+  raw_string_ostream RSO1(s1);
+  buffer_ostream BOS1(RSO1);
+  std::string s2;
+  raw_string_ostream RSO2(s2);
+  buffer_ostream BOS2(RSO2);
   
-  raw_null_ostream OS;
-  Target->addPassesToEmitFile(PM, OS, nullptr, TargetMachine::CGFT_ObjectFile,
+  // Add passes to each module to emit the generated binary code
+  T1->addPassesToEmitFile(PM1, BOS1, nullptr, TargetMachine::CGFT_ObjectFile,
                               false);
-  PM.run(*M);
+  T2->addPassesToEmitFile(PM2, BOS2, nullptr, TargetMachine::CGFT_ObjectFile,
+                              false);
+  PM1.run(*M1);
+  PM2.run(*M2);
+
+  // Extract strings from the streams
+  StringRef str1(BOS1.str());
+  StringRef str2(BOS2.str());
+  
+  MemBuffer buf1("buf1");
+  MemBuffer buf2("buf2");
+
+  // Strip the ELF headers from the object files created above
+  // Copy resulting machine code into buf1 and buf2
+  // StripHeader returns the size of the resulting buffer
+  int size1 = StripHeader(str1, buf1);
+  char *optcode = (char *) buf1.getBufferStart();
+
+  int size2 = StripHeader(str2, buf2);
+  char *unoptcode = (char *) buf2.getBufferStart();
+  
+  if (!optcode || !unoptcode) {
+    errs() << "error: could not create executable binary\n";
+    std::exit(1);
+  }
+
+  // Insert the generated machine code into memory and execute it
+  InsertCodeAndExec(m1, m2, optcode, unoptcode, size1, size2);
 
   return;
 }
Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.h
===================================================================
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.h
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.h
@@ -19,7 +19,8 @@
 
 namespace clang_fuzzer {
 void HandleLLVM(const std::string &S,
-               const std::vector<const char *> &ExtraArgs);
+               const std::vector<const char *> &ExtraArgs,
+               char *m1, char *m2);
 } // namespace clang_fuzzer
 
 #endif
Index: clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.h
===================================================================
--- clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.h
+++ clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.h
@@ -1,25 +1,21 @@
-//==-- handle_llvm.h - Helper function for Clang fuzzers -------------------==//
+//==-- fuzzer_initialize.h - Fuzz Clang ------------------------------------==//
 //
 //                     The LLVM Compiler Infrastructure
 //
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
-// Defines HandleLLVM for use by the Clang fuzzers.
+// Defines a function that returns the command line arguments for a specific
+// call to the fuzz target.
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_CLANG_TOOLS_CLANG_FUZZER_HANDLE_LLVM_HANDLELLVM_H
-#define LLVM_CLANG_TOOLS_CLANG_FUZZER_HANDLE_LLVM_HANDLELLVM_H
-
-#include <string>
 #include <vector>
 
 namespace clang_fuzzer {
-void HandleLLVM(const std::string &S,
-               const std::vector<const char *> &ExtraArgs);
-} // namespace clang_fuzzer
-
-#endif
+const std::vector<const char *>& GetCLArgs();
+char *GetRegion1();
+char *GetRegion2();
+}
Index: clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.cpp
===================================================================
--- /dev/null
+++ clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.cpp
@@ -0,0 +1,69 @@
+//===-- fuzzer_initialize.cpp - Fuzz Clang --------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// This file implements a few functions: one that returns the command line
+/// arguments for a given call to the fuzz target and one that initializes
+/// the fuzzer with the correct command line arguments. It also initializes two
+/// regions in memory for the fuzzer to use to insert and execute generated
+/// code. Getter methods for those two regions are also included.
+///
+//===----------------------------------------------------------------------===//
+
+#include "fuzzer_initialize.h"
+
+#include "llvm/Support/TargetSelect.h"
+#include <cstring>
+#include <sys/mman.h>
+
+using namespace clang_fuzzer;
+
+
+namespace clang_fuzzer {
+
+static std::vector<const char *> CLArgs;
+static char *m1;
+static char *m2;
+
+const std::vector<const char *>& GetCLArgs() {
+  return CLArgs;
+}
+
+char *GetRegion1() {
+  return m1;
+}
+
+char *GetRegion2() {
+  return m2;
+}
+
+}
+
+extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) {
+  llvm::InitializeAllTargets();
+  llvm::InitializeAllTargetMCs();
+  llvm::InitializeAllAsmPrinters();
+  llvm::InitializeAllAsmParsers();
+
+  CLArgs.push_back("-O2");
+  for (int I = 1; I < *argc; I++) {
+    if (strcmp((*argv)[I], "-ignore_remaining_args=1") == 0) {
+      for (I++; I < *argc; I++)
+        CLArgs.push_back((*argv)[I]);
+      break;
+    }
+  }
+
+  m1 = (char *) mmap(NULL, 1000000, PROT_WRITE | PROT_EXEC,
+                     MAP_SHARED | MAP_ANON, -1, 0);
+  m2 = (char *) mmap(NULL, 1000000, PROT_WRITE | PROT_EXEC,
+                     MAP_SHARED | MAP_ANON, -1, 0);
+
+  return 0;
+}
Index: clang/tools/clang-fuzzer/handle-llvm/Object.h
===================================================================
--- /dev/null
+++ clang/tools/clang-fuzzer/handle-llvm/Object.h
@@ -0,0 +1,640 @@
+//===- Object.h -------------------------------------------------*- C++ -*-===//
+//
+//                      The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//        Modified version of llvm/tools/llvm-objcopy/Object.h
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_TOOLS_OBJCOPY_OBJECT_H
+#define LLVM_TOOLS_OBJCOPY_OBJECT_H
+
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/Twine.h"
+#include "llvm/BinaryFormat/ELF.h"
+#include "llvm/MC/StringTableBuilder.h"
+#include "llvm/Object/ELFObjectFile.h"
+#include "llvm/Support/FileOutputBuffer.h"
+#include "llvm/Support/JamCRC.h"
+#include <cstddef>
+#include <cstdint>
+#include <functional>
+#include <memory>
+#include <set>
+#include <vector>
+
+namespace llvm {
+
+class Buffer;
+class SectionBase;
+class Section;
+class OwnedDataSection;
+class StringTableSection;
+class SymbolTableSection;
+class RelocationSection;
+class DynamicRelocationSection;
+class GnuDebugLinkSection;
+class GroupSection;
+class Segment;
+class Object;
+struct Symbol;
+
+class SectionTableRef {
+  MutableArrayRef<std::unique_ptr<SectionBase>> Sections;
+
+public:
+  using iterator = pointee_iterator<std::unique_ptr<SectionBase> *>;
+
+  explicit SectionTableRef(MutableArrayRef<std::unique_ptr<SectionBase>> Secs)
+      : Sections(Secs) {}
+  SectionTableRef(const SectionTableRef &) = default;
+
+  iterator begin() { return iterator(Sections.data()); }
+  iterator end() { return iterator(Sections.data() + Sections.size()); }
+
+  SectionBase *getSection(uint16_t Index, Twine ErrMsg);
+
+  template <class T>
+  T *getSectionOfType(uint16_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
+};
+
+enum ElfType { ELFT_ELF32LE, ELFT_ELF64LE, ELFT_ELF32BE, ELFT_ELF64BE };
+
+class SectionVisitor {
+public:
+  virtual ~SectionVisitor();
+
+  virtual void visit(const Section &Sec) = 0;
+  virtual void visit(const OwnedDataSection &Sec) = 0;
+  virtual void visit(const StringTableSection &Sec) = 0;
+  virtual void visit(const SymbolTableSection &Sec) = 0;
+  virtual void visit(const RelocationSection &Sec) = 0;
+  virtual void visit(const DynamicRelocationSection &Sec) = 0;
+  virtual void visit(const GnuDebugLinkSection &Sec) = 0;
+  virtual void visit(const GroupSection &Sec) = 0;
+};
+
+class SectionWriter : public SectionVisitor {
+protected:
+  Buffer &Out;
+
+public:
+  virtual ~SectionWriter(){};
+
+  void visit(const Section &Sec) override;
+  void visit(const OwnedDataSection &Sec) override;
+  void visit(const StringTableSection &Sec) override;
+  void visit(const DynamicRelocationSection &Sec) override;
+  virtual void visit(const SymbolTableSection &Sec) override = 0;
+  virtual void visit(const RelocationSection &Sec) override = 0;
+  virtual void visit(const GnuDebugLinkSection &Sec) override = 0;
+  virtual void visit(const GroupSection &Sec) override = 0;
+
+  explicit SectionWriter(Buffer &Buf) : Out(Buf) {}
+};
+
+template <class ELFT> class ELFSectionWriter : public SectionWriter {
+private:
+  using Elf_Word = typename ELFT::Word;
+  using Elf_Rel = typename ELFT::Rel;
+  using Elf_Rela = typename ELFT::Rela;
+
+public:
+  virtual ~ELFSectionWriter() {}
+  void visit(const SymbolTableSection &Sec) override;
+  void visit(const RelocationSection &Sec) override;
+  void visit(const GnuDebugLinkSection &Sec) override;
+  void visit(const GroupSection &Sec) override;
+
+  explicit ELFSectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
+};
+
+#define MAKE_SEC_WRITER_FRIEND                                                 \
+  friend class SectionWriter;                                                  \
+  template <class ELFT> friend class ELFSectionWriter;
+
+class BinarySectionWriter : public SectionWriter {
+public:
+  virtual ~BinarySectionWriter() {}
+
+  void visit(const SymbolTableSection &Sec) override;
+  void visit(const RelocationSection &Sec) override;
+  void visit(const GnuDebugLinkSection &Sec) override;
+  void visit(const GroupSection &Sec) override;
+
+  explicit BinarySectionWriter(Buffer &Buf) : SectionWriter(Buf) {}
+};
+
+// The class Buffer abstracts out the common interface of FileOutputBuffer and
+// WritableMemoryBuffer so that the hierarchy of Writers depends on this
+// abstract interface and doesn't depend on a particular implementation.
+// TODO: refactor the buffer classes in LLVM to enable us to use them here
+// directly.
+class Buffer {
+  StringRef Name;
+
+public:
+  virtual ~Buffer();
+  virtual void allocate(size_t Size) = 0;
+  virtual uint8_t *getBufferStart() = 0;
+  virtual Error commit() = 0;
+
+  explicit Buffer(StringRef Name) : Name(Name) {}
+  StringRef getName() const { return Name; }
+};
+
+class MemBuffer : public Buffer {
+  std::unique_ptr<WritableMemoryBuffer> Buf;
+
+public:
+  void allocate(size_t Size) override;
+  uint8_t *getBufferStart() override;
+  Error commit() override;
+
+  explicit MemBuffer(StringRef Name) : Buffer(Name) {}
+
+  std::unique_ptr<WritableMemoryBuffer> releaseMemoryBuffer();
+};
+
+class Writer {
+protected:
+  Object &Obj;
+  Buffer &Buf;
+
+public:
+  virtual ~Writer();
+  virtual int finalize() = 0;
+  virtual void write() = 0;
+
+  Writer(Object &O, Buffer &B) : Obj(O), Buf(B) {}
+};
+
+class BinaryWriter : public Writer {
+private:
+  std::unique_ptr<BinarySectionWriter> SecWriter;
+
+  uint64_t TotalSize;
+
+public:
+  ~BinaryWriter() {}
+  int finalize() override;
+  void write() override;
+  BinaryWriter(Object &Obj, Buffer &Buf) : Writer(Obj, Buf) {}
+};
+
+class SectionBase {
+public:
+  StringRef Name;
+  Segment *ParentSegment = nullptr;
+  uint64_t HeaderOffset;
+  uint64_t OriginalOffset;
+  uint32_t Index;
+
+  uint64_t Addr = 0;
+  uint64_t Align = 1;
+  uint32_t EntrySize = 0;
+  uint64_t Flags = 0;
+  uint64_t Info = 0;
+  uint64_t Link = ELF::SHN_UNDEF;
+  uint64_t NameIndex = 0;
+  uint64_t Offset = 0;
+  uint64_t Size = 0;
+  uint64_t Type = ELF::SHT_NULL;
+
+  virtual ~SectionBase() = default;
+
+  virtual void initialize(SectionTableRef SecTable);
+  virtual void finalize();
+  virtual void removeSectionReferences(const SectionBase *Sec);
+  virtual void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
+  virtual void accept(SectionVisitor &Visitor) const = 0;
+  virtual void markSymbols();
+};
+
+class Segment {
+private:
+  struct SectionCompare {
+    bool operator()(const SectionBase *Lhs, const SectionBase *Rhs) const {
+      // Some sections might have the same address if one of them is empty. To
+      // fix this we can use the lexicographic ordering on ->Addr and the
+      // address of the actully stored section.
+      if (Lhs->OriginalOffset == Rhs->OriginalOffset)
+        return Lhs < Rhs;
+      return Lhs->OriginalOffset < Rhs->OriginalOffset;
+    }
+  };
+
+  std::set<const SectionBase *, SectionCompare> Sections;
+  ArrayRef<uint8_t> Contents;
+
+public:
+  uint64_t Align;
+  uint64_t FileSize;
+  uint32_t Flags;
+  uint32_t Index;
+  uint64_t MemSize;
+  uint64_t Offset;
+  uint64_t PAddr;
+  uint64_t Type;
+  uint64_t VAddr;
+
+  uint64_t OriginalOffset;
+  Segment *ParentSegment = nullptr;
+
+  explicit Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
+  Segment() {}
+
+  const SectionBase *firstSection() const {
+    if (!Sections.empty())
+      return *Sections.begin();
+    return nullptr;
+  }
+
+  void removeSection(const SectionBase *Sec) { Sections.erase(Sec); }
+  void addSection(const SectionBase *Sec) { Sections.insert(Sec); }
+};
+
+class Section : public SectionBase {
+  MAKE_SEC_WRITER_FRIEND
+
+  ArrayRef<uint8_t> Contents;
+  SectionBase *LinkSection = nullptr;
+
+public:
+  explicit Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
+
+  void accept(SectionVisitor &Visitor) const override;
+  void removeSectionReferences(const SectionBase *Sec) override;
+  void initialize(SectionTableRef SecTable) override;
+  void finalize() override;
+};
+
+class OwnedDataSection : public SectionBase {
+  MAKE_SEC_WRITER_FRIEND
+
+  std::vector<uint8_t> Data;
+
+public:
+  OwnedDataSection(StringRef SecName, ArrayRef<uint8_t> Data)
+      : Data(std::begin(Data), std::end(Data)) {
+    Name = SecName;
+    Type = ELF::SHT_PROGBITS;
+    Size = Data.size();
+    OriginalOffset = std::numeric_limits<uint64_t>::max();
+  }
+
+  void accept(SectionVisitor &Sec) const override;
+};
+
+// There are two types of string tables that can exist, dynamic and not dynamic.
+// In the dynamic case the string table is allocated. Changing a dynamic string
+// table would mean altering virtual addresses and thus the memory image. So
+// dynamic string tables should not have an interface to modify them or
+// reconstruct them. This type lets us reconstruct a string table. To avoid
+// this class being used for dynamic string tables (which has happened) the
+// classof method checks that the particular instance is not allocated. This
+// then agrees with the makeSection method used to construct most sections.
+class StringTableSection : public SectionBase {
+  MAKE_SEC_WRITER_FRIEND
+
+  StringTableBuilder StrTabBuilder;
+
+public:
+  StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
+    Type = ELF::SHT_STRTAB;
+  }
+
+  void addString(StringRef Name);
+  uint32_t findIndex(StringRef Name) const;
+  void finalize() override;
+  void accept(SectionVisitor &Visitor) const override;
+
+  static bool classof(const SectionBase *S) {
+    if (S->Flags & ELF::SHF_ALLOC)
+      return false;
+    return S->Type == ELF::SHT_STRTAB;
+  }
+};
+
+// Symbols have a st_shndx field that normally stores an index but occasionally
+// stores a different special value. This enum keeps track of what the st_shndx
+// field means. Most of the values are just copies of the special SHN_* values.
+// SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
+enum SymbolShndxType {
+  SYMBOL_SIMPLE_INDEX = 0,
+  SYMBOL_ABS = ELF::SHN_ABS,
+  SYMBOL_COMMON = ELF::SHN_COMMON,
+  SYMBOL_HEXAGON_SCOMMON = ELF::SHN_HEXAGON_SCOMMON,
+  SYMBOL_HEXAGON_SCOMMON_2 = ELF::SHN_HEXAGON_SCOMMON_2,
+  SYMBOL_HEXAGON_SCOMMON_4 = ELF::SHN_HEXAGON_SCOMMON_4,
+  SYMBOL_HEXAGON_SCOMMON_8 = ELF::SHN_HEXAGON_SCOMMON_8,
+};
+
+struct Symbol {
+  uint8_t Binding;
+  SectionBase *DefinedIn = nullptr;
+  SymbolShndxType ShndxType;
+  uint32_t Index;
+  StringRef Name;
+  uint32_t NameIndex;
+  uint64_t Size;
+  uint8_t Type;
+  uint64_t Value;
+  uint8_t Visibility;
+  bool Referenced = false;
+
+  uint16_t getShndx() const;
+};
+
+class SymbolTableSection : public SectionBase {
+  MAKE_SEC_WRITER_FRIEND
+
+  void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
+  void assignIndices();
+
+protected:
+  std::vector<std::unique_ptr<Symbol>> Symbols;
+  StringTableSection *SymbolNames = nullptr;
+
+  using SymPtr = std::unique_ptr<Symbol>;
+
+public:
+  void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
+                 SectionBase *DefinedIn, uint64_t Value, uint8_t Visibility,
+                 uint16_t Shndx, uint64_t Sz);
+  void addSymbolNames();
+  // An 'empty' symbol table still contains a null symbol.
+  bool empty() const { return Symbols.size() == 1; }
+  const SectionBase *getStrTab() const { return SymbolNames; }
+  const Symbol *getSymbolByIndex(uint32_t Index) const;
+  Symbol *getSymbolByIndex(uint32_t Index);
+  void updateSymbols(function_ref<void(Symbol &)> Callable);
+
+  void removeSectionReferences(const SectionBase *Sec) override;
+  void initialize(SectionTableRef SecTable) override;
+  void finalize() override;
+  void accept(SectionVisitor &Visitor) const override;
+  void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
+
+  static bool classof(const SectionBase *S) {
+    return S->Type == ELF::SHT_SYMTAB;
+  }
+};
+
+struct Relocation {
+  Symbol *RelocSymbol = nullptr;
+  uint64_t Offset;
+  uint64_t Addend;
+  uint32_t Type;
+};
+
+// All relocation sections denote relocations to apply to another section.
+// However, some relocation sections use a dynamic symbol table and others use
+// a regular symbol table. Because the types of the two symbol tables differ in
+// our system (because they should behave differently) we can't uniformly
+// represent all relocations with the same base class if we expose an interface
+// that mentions the symbol table type. So we split the two base types into two
+// different classes, one which handles the section the relocation is applied to
+// and another which handles the symbol table type. The symbol table type is
+// taken as a type parameter to the class (see RelocSectionWithSymtabBase).
+class RelocationSectionBase : public SectionBase {
+protected:
+  SectionBase *SecToApplyRel = nullptr;
+
+public:
+  const SectionBase *getSection() const { return SecToApplyRel; }
+  void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
+
+  static bool classof(const SectionBase *S) {
+    return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
+  }
+};
+
+// Takes the symbol table type to use as a parameter so that we can deduplicate
+// that code between the two symbol table types.
+template <class SymTabType>
+class RelocSectionWithSymtabBase : public RelocationSectionBase {
+  SymTabType *Symbols = nullptr;
+  void setSymTab(SymTabType *SymTab) { Symbols = SymTab; }
+
+protected:
+  RelocSectionWithSymtabBase() = default;
+
+public:
+  void removeSectionReferences(const SectionBase *Sec) override;
+  void initialize(SectionTableRef SecTable) override;
+  void finalize() override;
+};
+
+class RelocationSection
+    : public RelocSectionWithSymtabBase<SymbolTableSection> {
+  MAKE_SEC_WRITER_FRIEND
+
+  std::vector<Relocation> Relocations;
+
+public:
+  void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
+  void accept(SectionVisitor &Visitor) const override;
+  void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
+  void markSymbols() override;
+
+  static bool classof(const SectionBase *S) {
+    if (S->Flags & ELF::SHF_ALLOC)
+      return false;
+    return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
+  }
+};
+
+// TODO: The way stripping and groups interact is complicated
+// and still needs to be worked on.
+
+class GroupSection : public SectionBase {
+  MAKE_SEC_WRITER_FRIEND
+  const SymbolTableSection *SymTab = nullptr;
+  Symbol *Sym = nullptr;
+  ELF::Elf32_Word FlagWord;
+  SmallVector<SectionBase *, 3> GroupMembers;
+
+public:
+  // TODO: Contents is present in several classes of the hierarchy.
+  // This needs to be refactored to avoid duplication.
+  ArrayRef<uint8_t> Contents;
+
+  explicit GroupSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
+
+  void setSymTab(const SymbolTableSection *SymTabSec) { SymTab = SymTabSec; }
+  void setSymbol(Symbol *S) { Sym = S; }
+  void setFlagWord(ELF::Elf32_Word W) { FlagWord = W; }
+  void addMember(SectionBase *Sec) { GroupMembers.push_back(Sec); }
+
+  void initialize(SectionTableRef SecTable) override{};
+  void accept(SectionVisitor &) const override;
+  void finalize() override;
+  void removeSymbols(function_ref<bool(const Symbol &)> ToRemove) override;
+  void markSymbols() override;
+
+  static bool classof(const SectionBase *S) {
+    return S->Type == ELF::SHT_GROUP;
+  }
+};
+
+class DynamicSymbolTableSection : public Section {
+public:
+  explicit DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : Section(Data) {}
+
+  static bool classof(const SectionBase *S) {
+    return S->Type == ELF::SHT_DYNSYM;
+  }
+};
+
+class DynamicSection : public Section {
+public:
+  explicit DynamicSection(ArrayRef<uint8_t> Data) : Section(Data) {}
+
+  static bool classof(const SectionBase *S) {
+    return S->Type == ELF::SHT_DYNAMIC;
+  }
+};
+
+class DynamicRelocationSection
+    : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
+  MAKE_SEC_WRITER_FRIEND
+
+private:
+  ArrayRef<uint8_t> Contents;
+
+public:
+  explicit DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
+
+  void accept(SectionVisitor &) const override;
+
+  static bool classof(const SectionBase *S) {
+    if (!(S->Flags & ELF::SHF_ALLOC))
+      return false;
+    return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
+  }
+};
+
+class GnuDebugLinkSection : public SectionBase {
+  MAKE_SEC_WRITER_FRIEND
+
+private:
+  StringRef FileName;
+  uint32_t CRC32;
+
+  void init(StringRef File, StringRef Data);
+
+public:
+  // If we add this section from an external source we can use this ctor.
+  explicit GnuDebugLinkSection(StringRef File);
+  void accept(SectionVisitor &Visitor) const override;
+};
+
+class Reader {
+public:
+  virtual ~Reader();
+  virtual std::unique_ptr<Object> create() const = 0;
+};
+
+using object::Binary;
+using object::ELFFile;
+using object::ELFObjectFile;
+using object::OwningBinary;
+
+template <class ELFT> class ELFBuilder {
+private:
+  using Elf_Addr = typename ELFT::Addr;
+  using Elf_Shdr = typename ELFT::Shdr;
+  using Elf_Ehdr = typename ELFT::Ehdr;
+
+  const ELFFile<ELFT> &ElfFile;
+  Object &Obj;
+
+  void setParentSegment(Segment &Child);
+  void readProgramHeaders();
+  void initGroupSection(GroupSection *GroupSec);
+  void initSymbolTable(SymbolTableSection *SymTab);
+  void readSectionHeaders();
+  SectionBase &makeSection(const Elf_Shdr &Shdr);
+
+public:
+  ELFBuilder(const ELFObjectFile<ELFT> &ElfObj, Object &Obj)
+      : ElfFile(*ElfObj.getELFFile()), Obj(Obj) {}
+
+  void build();
+};
+
+class ELFReader : public Reader {
+  Binary *Bin;
+
+public:
+  ElfType getElfType() const;
+  std::unique_ptr<Object> create() const override;
+  explicit ELFReader(Binary *B) : Bin(B){};
+};
+
+class Object {
+private:
+  using SecPtr = std::unique_ptr<SectionBase>;
+  using SegPtr = std::unique_ptr<Segment>;
+
+  std::vector<SecPtr> Sections;
+  std::vector<SegPtr> Segments;
+
+public:
+  template <class T>
+  using Range = iterator_range<
+      pointee_iterator<typename std::vector<std::unique_ptr<T>>::iterator>>;
+
+  template <class T>
+  using ConstRange = iterator_range<pointee_iterator<
+      typename std::vector<std::unique_ptr<T>>::const_iterator>>;
+
+  // It is often the case that the ELF header and the program header table are
+  // not present in any segment. This could be a problem during file layout,
+  // because other segments may get assigned an offset where either of the
+  // two should reside, which will effectively corrupt the resulting binary.
+  // Other than that we use these segments to track program header offsets
+  // when they may not follow the ELF header.
+  Segment ElfHdrSegment;
+  Segment ProgramHdrSegment;
+
+  uint8_t Ident[16];
+  uint64_t Entry;
+  uint64_t SHOffset;
+  uint32_t Type;
+  uint32_t Machine;
+  uint32_t Version;
+  uint32_t Flags;
+
+  StringTableSection *SectionNames = nullptr;
+  SymbolTableSection *SymbolTable = nullptr;
+
+  void sortSections();
+  SectionTableRef sections() { return SectionTableRef(Sections); }
+  ConstRange<SectionBase> sections() const {
+    return make_pointee_range(Sections);
+  }
+  Range<Segment> segments() { return make_pointee_range(Segments); }
+  ConstRange<Segment> segments() const { return make_pointee_range(Segments); }
+
+  void removeSections(std::function<bool(const SectionBase &)> ToRemove);
+  void removeSymbols(function_ref<bool(const Symbol &)> ToRemove);
+  template <class T, class... Ts> T &addSection(Ts &&... Args) {
+    auto Sec = llvm::make_unique<T>(std::forward<Ts>(Args)...);
+    auto Ptr = Sec.get();
+    Sections.emplace_back(std::move(Sec));
+    return *Ptr;
+  }
+  Segment &addSegment(ArrayRef<uint8_t> Data) {
+    Segments.emplace_back(llvm::make_unique<Segment>(Data));
+    return *Segments.back();
+  }
+};
+} // end namespace llvm
+
+#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H
Index: clang/tools/clang-fuzzer/handle-llvm/Object.cpp
===================================================================
--- /dev/null
+++ clang/tools/clang-fuzzer/handle-llvm/Object.cpp
@@ -0,0 +1,1033 @@
+//===- Object.cpp ---------------------------------------------------------===//
+//
+//                      The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//        Modified version of llvm/tools/llvm-objcopy/Object.cpp
+//
+//===----------------------------------------------------------------------===//
+
+#include "Object.h"
+#include "llvm-objcopy.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/Twine.h"
+#include "llvm/ADT/iterator_range.h"
+#include "llvm/BinaryFormat/ELF.h"
+#include "llvm/Object/ELFObjectFile.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/FileOutputBuffer.h"
+#include "llvm/Support/Path.h"
+#include <algorithm>
+#include <cstddef>
+#include <cstdint>
+#include <iterator>
+#include <utility>
+#include <vector>
+
+using namespace llvm;
+using namespace object;
+using namespace ELF;
+
+static StringRef ToolName;
+
+namespace llvm {
+
+LLVM_ATTRIBUTE_NORETURN void error(Twine Message) {
+  errs() << ToolName << ": " << Message << ".\n";
+  errs().flush();
+  exit(1);
+}
+
+LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC) {
+  assert(EC);
+  errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n";
+  exit(1);
+}
+
+LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) {
+  assert(E);
+  std::string Buf;
+  raw_string_ostream OS(Buf);
+  logAllUnhandledErrors(std::move(E), OS, "");
+  OS.flush();
+  errs() << ToolName << ": '" << File << "': " << Buf;
+  exit(1);
+}
+}
+
+Buffer::~Buffer() {}
+
+void MemBuffer::allocate(size_t Size) {
+  Buf = WritableMemoryBuffer::getNewMemBuffer(Size, getName());
+}
+
+Error MemBuffer::commit() { return Error::success(); }
+
+uint8_t *MemBuffer::getBufferStart() {
+  return reinterpret_cast<uint8_t *>(Buf->getBufferStart());
+}
+
+std::unique_ptr<WritableMemoryBuffer> MemBuffer::releaseMemoryBuffer() {
+  return std::move(Buf);
+}
+
+void SectionBase::removeSectionReferences(const SectionBase *Sec) {}
+void SectionBase::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {}
+void SectionBase::initialize(SectionTableRef SecTable) {}
+void SectionBase::finalize() {}
+void SectionBase::markSymbols() {}
+
+SectionVisitor::~SectionVisitor() {}
+
+void BinarySectionWriter::visit(const SymbolTableSection &Sec) {
+  error("Cannot write symbol table '" + Sec.Name + "' out to binary");
+}
+
+void BinarySectionWriter::visit(const RelocationSection &Sec) {
+  error("Cannot write relocation section '" + Sec.Name + "' out to binary");
+}
+
+void BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) {
+  error("Cannot write '" + Sec.Name + "' out to binary");
+}
+
+void BinarySectionWriter::visit(const GroupSection &Sec) {
+  error("Cannot write '" + Sec.Name + "' out to binary");
+}
+
+void SectionWriter::visit(const Section &Sec) {
+  if (Sec.Type == SHT_NOBITS)
+    return;
+  uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
+  std::copy(std::begin(Sec.Contents), std::end(Sec.Contents), Buf);
+}
+
+void Section::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); }
+
+void SectionWriter::visit(const OwnedDataSection &Sec) {
+  uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
+  std::copy(std::begin(Sec.Data), std::end(Sec.Data), Buf);
+}
+
+void OwnedDataSection::accept(SectionVisitor &Visitor) const {
+  Visitor.visit(*this);
+}
+
+void StringTableSection::addString(StringRef Name) {
+  StrTabBuilder.add(Name);
+  Size = StrTabBuilder.getSize();
+}
+
+uint32_t StringTableSection::findIndex(StringRef Name) const {
+  return StrTabBuilder.getOffset(Name);
+}
+
+void StringTableSection::finalize() { StrTabBuilder.finalize(); }
+
+void SectionWriter::visit(const StringTableSection &Sec) {
+  Sec.StrTabBuilder.write(Out.getBufferStart() + Sec.Offset);
+}
+
+void StringTableSection::accept(SectionVisitor &Visitor) const {
+  Visitor.visit(*this);
+}
+
+static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
+  switch (Index) {
+  case SHN_ABS:
+  case SHN_COMMON:
+    return true;
+  }
+  if (Machine == EM_HEXAGON) {
+    switch (Index) {
+    case SHN_HEXAGON_SCOMMON:
+    case SHN_HEXAGON_SCOMMON_2:
+    case SHN_HEXAGON_SCOMMON_4:
+    case SHN_HEXAGON_SCOMMON_8:
+      return true;
+    }
+  }
+  return false;
+}
+
+uint16_t Symbol::getShndx() const {
+  if (DefinedIn != nullptr) {
+    return DefinedIn->Index;
+  }
+  switch (ShndxType) {
+  // This means that we don't have a defined section but we do need to
+  // output a legitimate section index.
+  case SYMBOL_SIMPLE_INDEX:
+    return SHN_UNDEF;
+  case SYMBOL_ABS:
+  case SYMBOL_COMMON:
+  case SYMBOL_HEXAGON_SCOMMON:
+  case SYMBOL_HEXAGON_SCOMMON_2:
+  case SYMBOL_HEXAGON_SCOMMON_4:
+  case SYMBOL_HEXAGON_SCOMMON_8:
+    return static_cast<uint16_t>(ShndxType);
+  }
+  llvm_unreachable("Symbol with invalid ShndxType encountered");
+}
+
+void SymbolTableSection::assignIndices() {
+  uint32_t Index = 0;
+  for (auto &Sym : Symbols)
+    Sym->Index = Index++;
+}
+
+void SymbolTableSection::addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
+                                   SectionBase *DefinedIn, uint64_t Value,
+                                   uint8_t Visibility, uint16_t Shndx,
+                                   uint64_t Sz) {
+  Symbol Sym;
+  Sym.Name = Name;
+  Sym.Binding = Bind;
+  Sym.Type = Type;
+  Sym.DefinedIn = DefinedIn;
+  if (DefinedIn == nullptr) {
+    if (Shndx >= SHN_LORESERVE)
+      Sym.ShndxType = static_cast<SymbolShndxType>(Shndx);
+    else
+      Sym.ShndxType = SYMBOL_SIMPLE_INDEX;
+  }
+  Sym.Value = Value;
+  Sym.Visibility = Visibility;
+  Sym.Size = Sz;
+  Sym.Index = Symbols.size();
+  Symbols.emplace_back(llvm::make_unique<Symbol>(Sym));
+  Size += this->EntrySize;
+}
+
+void SymbolTableSection::removeSectionReferences(const SectionBase *Sec) {
+  if (SymbolNames == Sec) {
+    error("String table " + SymbolNames->Name +
+          " cannot be removed because it is referenced by the symbol table " +
+          this->Name);
+  }
+  removeSymbols([Sec](const Symbol &Sym) { return Sym.DefinedIn == Sec; });
+}
+
+void SymbolTableSection::updateSymbols(function_ref<void(Symbol &)> Callable) {
+  std::for_each(std::begin(Symbols) + 1, std::end(Symbols),
+                [Callable](SymPtr &Sym) { Callable(*Sym); });
+  std::stable_partition(
+      std::begin(Symbols), std::end(Symbols),
+      [](const SymPtr &Sym) { return Sym->Binding == STB_LOCAL; });
+  assignIndices();
+}
+
+void SymbolTableSection::removeSymbols(
+    function_ref<bool(const Symbol &)> ToRemove) {
+  Symbols.erase(
+      std::remove_if(std::begin(Symbols) + 1, std::end(Symbols),
+                     [ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }),
+      std::end(Symbols));
+  Size = Symbols.size() * EntrySize;
+  assignIndices();
+}
+
+void SymbolTableSection::initialize(SectionTableRef SecTable) {
+  Size = 0;
+  setStrTab(SecTable.getSectionOfType<StringTableSection>(
+      Link,
+      "Symbol table has link index of " + Twine(Link) +
+          " which is not a valid index",
+      "Symbol table has link index of " + Twine(Link) +
+          " which is not a string table"));
+}
+
+void SymbolTableSection::finalize() {
+  // Make sure SymbolNames is finalized before getting name indexes.
+  SymbolNames->finalize();
+
+  uint32_t MaxLocalIndex = 0;
+  for (auto &Sym : Symbols) {
+    Sym->NameIndex = SymbolNames->findIndex(Sym->Name);
+    if (Sym->Binding == STB_LOCAL)
+      MaxLocalIndex = std::max(MaxLocalIndex, Sym->Index);
+  }
+  // Now we need to set the Link and Info fields.
+  Link = SymbolNames->Index;
+  Info = MaxLocalIndex + 1;
+}
+
+void SymbolTableSection::addSymbolNames() {
+  // Add all of our strings to SymbolNames so that SymbolNames has the right
+  // size before layout is decided.
+  for (auto &Sym : Symbols)
+    SymbolNames->addString(Sym->Name);
+}
+
+const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const {
+  if (Symbols.size() <= Index)
+    error("Invalid symbol index: " + Twine(Index));
+  return Symbols[Index].get();
+}
+
+Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) {
+  return const_cast<Symbol *>(
+      static_cast<const SymbolTableSection *>(this)->getSymbolByIndex(Index));
+}
+
+template <class ELFT>
+void ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) {
+  uint8_t *Buf = Out.getBufferStart();
+  Buf += Sec.Offset;
+  typename ELFT::Sym *Sym = reinterpret_cast<typename ELFT::Sym *>(Buf);
+  // Loop though symbols setting each entry of the symbol table.
+  for (auto &Symbol : Sec.Symbols) {
+    Sym->st_name = Symbol->NameIndex;
+    Sym->st_value = Symbol->Value;
+    Sym->st_size = Symbol->Size;
+    Sym->st_other = Symbol->Visibility;
+    Sym->setBinding(Symbol->Binding);
+    Sym->setType(Symbol->Type);
+    Sym->st_shndx = Symbol->getShndx();
+    ++Sym;
+  }
+}
+
+void SymbolTableSection::accept(SectionVisitor &Visitor) const {
+  Visitor.visit(*this);
+}
+
+template <class SymTabType>
+void RelocSectionWithSymtabBase<SymTabType>::removeSectionReferences(
+    const SectionBase *Sec) {
+  if (Symbols == Sec) {
+    error("Symbol table " + Symbols->Name +
+          " cannot be removed because it is "
+          "referenced by the relocation "
+          "section " +
+          this->Name);
+  }
+}
+
+template <class SymTabType>
+void RelocSectionWithSymtabBase<SymTabType>::initialize(
+    SectionTableRef SecTable) {
+  setSymTab(SecTable.getSectionOfType<SymTabType>(
+      Link,
+      "Link field value " + Twine(Link) + " in section " + Name + " is invalid",
+      "Link field value " + Twine(Link) + " in section " + Name +
+          " is not a symbol table"));
+
+  if (Info != SHN_UNDEF)
+    setSection(SecTable.getSection(Info, "Info field value " + Twine(Info) +
+                                             " in section " + Name +
+                                             " is invalid"));
+  else
+    setSection(nullptr);
+}
+
+template <class SymTabType>
+void RelocSectionWithSymtabBase<SymTabType>::finalize() {
+  this->Link = Symbols->Index;
+  if (SecToApplyRel != nullptr)
+    this->Info = SecToApplyRel->Index;
+}
+
+template <class ELFT>
+void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {}
+
+template <class ELFT>
+void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) {
+  Rela.r_addend = Addend;
+}
+
+template <class RelRange, class T>
+void writeRel(const RelRange &Relocations, T *Buf) {
+  for (const auto &Reloc : Relocations) {
+    Buf->r_offset = Reloc.Offset;
+    setAddend(*Buf, Reloc.Addend);
+    Buf->setSymbolAndType(Reloc.RelocSymbol->Index, Reloc.Type, false);
+    ++Buf;
+  }
+}
+
+template <class ELFT>
+void ELFSectionWriter<ELFT>::visit(const RelocationSection &Sec) {
+  uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
+  if (Sec.Type == SHT_REL)
+    writeRel(Sec.Relocations, reinterpret_cast<Elf_Rel *>(Buf));
+  else
+    writeRel(Sec.Relocations, reinterpret_cast<Elf_Rela *>(Buf));
+}
+
+void RelocationSection::accept(SectionVisitor &Visitor) const {
+  Visitor.visit(*this);
+}
+
+void RelocationSection::removeSymbols(
+    function_ref<bool(const Symbol &)> ToRemove) {
+  for (const Relocation &Reloc : Relocations)
+    if (ToRemove(*Reloc.RelocSymbol))
+      error("not stripping symbol `" + Reloc.RelocSymbol->Name +
+            "' because it is named in a relocation");
+}
+
+void RelocationSection::markSymbols() {
+  for (const Relocation &Reloc : Relocations)
+    Reloc.RelocSymbol->Referenced = true;
+}
+
+void SectionWriter::visit(const DynamicRelocationSection &Sec) {
+  std::copy(std::begin(Sec.Contents), std::end(Sec.Contents),
+            Out.getBufferStart() + Sec.Offset);
+}
+
+void DynamicRelocationSection::accept(SectionVisitor &Visitor) const {
+  Visitor.visit(*this);
+}
+
+void Section::removeSectionReferences(const SectionBase *Sec) {
+  if (LinkSection == Sec) {
+    error("Section " + LinkSection->Name +
+          " cannot be removed because it is "
+          "referenced by the section " +
+          this->Name);
+  }
+}
+
+void GroupSection::finalize() {
+  this->Info = Sym->Index;
+  this->Link = SymTab->Index;
+}
+
+void GroupSection::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
+  if (ToRemove(*Sym)) {
+    error("Symbol " + Sym->Name +
+          " cannot be removed because it is "
+          "referenced by the section " +
+          this->Name + "[" + Twine(this->Index) + "]");
+  }
+}
+
+void GroupSection::markSymbols() {
+  if (Sym)
+    Sym->Referenced = true;
+}
+
+void Section::initialize(SectionTableRef SecTable) {
+  if (Link != ELF::SHN_UNDEF) {
+    LinkSection =
+        SecTable.getSection(Link, "Link field value " + Twine(Link) +
+                                      " in section " + Name + " is invalid");
+    if (LinkSection->Type == ELF::SHT_SYMTAB)
+      LinkSection = nullptr;
+  }
+}
+
+void Section::finalize() { this->Link = LinkSection ? LinkSection->Index : 0; }
+
+void GnuDebugLinkSection::init(StringRef File, StringRef Data) {
+  FileName = sys::path::filename(File);
+  // The format for the .gnu_debuglink starts with the file name and is
+  // followed by a null terminator and then the CRC32 of the file. The CRC32
+  // should be 4 byte aligned. So we add the FileName size, a 1 for the null
+  // byte, and then finally push the size to alignment and add 4.
+  Size = alignTo(FileName.size() + 1, 4) + 4;
+  // The CRC32 will only be aligned if we align the whole section.
+  Align = 4;
+  Type = ELF::SHT_PROGBITS;
+  Name = ".gnu_debuglink";
+  // For sections not found in segments, OriginalOffset is only used to
+  // establish the order that sections should go in. By using the maximum
+  // possible offset we cause this section to wind up at the end.
+  OriginalOffset = std::numeric_limits<uint64_t>::max();
+  JamCRC crc;
+  crc.update(ArrayRef<char>(Data.data(), Data.size()));
+  // The CRC32 value needs to be complemented because the JamCRC dosn't
+  // finalize the CRC32 value. It also dosn't negate the initial CRC32 value
+  // but it starts by default at 0xFFFFFFFF which is the complement of zero.
+  CRC32 = ~crc.getCRC();
+}
+
+GnuDebugLinkSection::GnuDebugLinkSection(StringRef File) : FileName(File) {
+  // Read in the file to compute the CRC of it.
+  auto DebugOrErr = MemoryBuffer::getFile(File);
+  if (!DebugOrErr)
+    error("'" + File + "': " + DebugOrErr.getError().message());
+  auto Debug = std::move(*DebugOrErr);
+  init(File, Debug->getBuffer());
+}
+
+template <class ELFT>
+void ELFSectionWriter<ELFT>::visit(const GnuDebugLinkSection &Sec) {
+  auto Buf = Out.getBufferStart() + Sec.Offset;
+  char *File = reinterpret_cast<char *>(Buf);
+  Elf_Word *CRC =
+      reinterpret_cast<Elf_Word *>(Buf + Sec.Size - sizeof(Elf_Word));
+  *CRC = Sec.CRC32;
+  std::copy(std::begin(Sec.FileName), std::end(Sec.FileName), File);
+}
+
+void GnuDebugLinkSection::accept(SectionVisitor &Visitor) const {
+  Visitor.visit(*this);
+}
+
+template <class ELFT>
+void ELFSectionWriter<ELFT>::visit(const GroupSection &Sec) {
+  ELF::Elf32_Word *Buf =
+      reinterpret_cast<ELF::Elf32_Word *>(Out.getBufferStart() + Sec.Offset);
+  *Buf++ = Sec.FlagWord;
+  for (const auto *S : Sec.GroupMembers)
+    support::endian::write32<ELFT::TargetEndianness>(Buf++, S->Index);
+}
+
+void GroupSection::accept(SectionVisitor &Visitor) const {
+  Visitor.visit(*this);
+}
+
+// Returns true IFF a section is wholly inside the range of a segment
+static bool sectionWithinSegment(const SectionBase &Section,
+                                 const Segment &Segment) {
+  // If a section is empty it should be treated like it has a size of 1. This is
+  // to clarify the case when an empty section lies on a boundary between two
+  // segments and ensures that the section "belongs" to the second segment and
+  // not the first.
+  uint64_t SecSize = Section.Size ? Section.Size : 1;
+  return Segment.Offset <= Section.OriginalOffset &&
+         Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize;
+}
+
+// Returns true IFF a segment's original offset is inside of another segment's
+// range.
+static bool segmentOverlapsSegment(const Segment &Child,
+                                   const Segment &Parent) {
+
+  return Parent.OriginalOffset <= Child.OriginalOffset &&
+         Parent.OriginalOffset + Parent.FileSize > Child.OriginalOffset;
+}
+
+static bool compareSegmentsByOffset(const Segment *A, const Segment *B) {
+  // Any segment without a parent segment should come before a segment
+  // that has a parent segment.
+  if (A->OriginalOffset < B->OriginalOffset)
+    return true;
+  if (A->OriginalOffset > B->OriginalOffset)
+    return false;
+  return A->Index < B->Index;
+}
+
+static bool compareSegmentsByPAddr(const Segment *A, const Segment *B) {
+  if (A->PAddr < B->PAddr)
+    return true;
+  if (A->PAddr > B->PAddr)
+    return false;
+  return A->Index < B->Index;
+}
+
+template <class ELFT> void ELFBuilder<ELFT>::setParentSegment(Segment &Child) {
+  for (auto &Parent : Obj.segments()) {
+    // Every segment will overlap with itself but we don't want a segment to
+    // be it's own parent so we avoid that situation.
+    if (&Child != &Parent && segmentOverlapsSegment(Child, Parent)) {
+      // We want a canonical "most parental" segment but this requires
+      // inspecting the ParentSegment.
+      if (compareSegmentsByOffset(&Parent, &Child))
+        if (Child.ParentSegment == nullptr ||
+            compareSegmentsByOffset(&Parent, Child.ParentSegment)) {
+          Child.ParentSegment = &Parent;
+        }
+    }
+  }
+}
+
+template <class ELFT> void ELFBuilder<ELFT>::readProgramHeaders() {
+  uint32_t Index = 0;
+  for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) {
+    ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset,
+                           (size_t)Phdr.p_filesz};
+    Segment &Seg = Obj.addSegment(Data);
+    Seg.Type = Phdr.p_type;
+    Seg.Flags = Phdr.p_flags;
+    Seg.OriginalOffset = Phdr.p_offset;
+    Seg.Offset = Phdr.p_offset;
+    Seg.VAddr = Phdr.p_vaddr;
+    Seg.PAddr = Phdr.p_paddr;
+    Seg.FileSize = Phdr.p_filesz;
+    Seg.MemSize = Phdr.p_memsz;
+    Seg.Align = Phdr.p_align;
+    Seg.Index = Index++;
+    for (auto &Section : Obj.sections()) {
+      if (sectionWithinSegment(Section, Seg)) {
+        Seg.addSection(&Section);
+        if (!Section.ParentSegment ||
+            Section.ParentSegment->Offset > Seg.Offset) {
+          Section.ParentSegment = &Seg;
+        }
+      }
+    }
+  }
+
+  auto &ElfHdr = Obj.ElfHdrSegment;
+  // Creating multiple PT_PHDR segments technically is not valid, but PT_LOAD
+  // segments must not overlap, and other types fit even less.
+  ElfHdr.Type = PT_PHDR;
+  ElfHdr.Flags = 0;
+  ElfHdr.OriginalOffset = ElfHdr.Offset = 0;
+  ElfHdr.VAddr = 0;
+  ElfHdr.PAddr = 0;
+  ElfHdr.FileSize = ElfHdr.MemSize = sizeof(Elf_Ehdr);
+  ElfHdr.Align = 0;
+  ElfHdr.Index = Index++;
+
+  const auto &Ehdr = *ElfFile.getHeader();
+  auto &PrHdr = Obj.ProgramHdrSegment;
+  PrHdr.Type = PT_PHDR;
+  PrHdr.Flags = 0;
+  // The spec requires us to have p_vaddr % p_align == p_offset % p_align.
+  // Whereas this works automatically for ElfHdr, here OriginalOffset is
+  // always non-zero and to ensure the equation we assign the same value to
+  // VAddr as well.
+  PrHdr.OriginalOffset = PrHdr.Offset = PrHdr.VAddr = Ehdr.e_phoff;
+  PrHdr.PAddr = 0;
+  PrHdr.FileSize = PrHdr.MemSize = Ehdr.e_phentsize * Ehdr.e_phnum;
+  // The spec requires us to naturally align all the fields.
+  PrHdr.Align = sizeof(Elf_Addr);
+  PrHdr.Index = Index++;
+
+  // Now we do an O(n^2) loop through the segments in order to match up
+  // segments.
+  for (auto &Child : Obj.segments())
+    setParentSegment(Child);
+  setParentSegment(ElfHdr);
+  setParentSegment(PrHdr);
+}
+
+template <class ELFT>
+void ELFBuilder<ELFT>::initGroupSection(GroupSection *GroupSec) {
+  auto SecTable = Obj.sections();
+  auto SymTab = SecTable.template getSectionOfType<SymbolTableSection>(
+      GroupSec->Link,
+      "Link field value " + Twine(GroupSec->Link) + " in section " +
+          GroupSec->Name + " is invalid",
+      "Link field value " + Twine(GroupSec->Link) + " in section " +
+          GroupSec->Name + " is not a symbol table");
+  auto Sym = SymTab->getSymbolByIndex(GroupSec->Info);
+  if (!Sym)
+    error("Info field value " + Twine(GroupSec->Info) + " in section " +
+          GroupSec->Name + " is not a valid symbol index");
+  GroupSec->setSymTab(SymTab);
+  GroupSec->setSymbol(Sym);
+  if (GroupSec->Contents.size() % sizeof(ELF::Elf32_Word) ||
+      GroupSec->Contents.empty())
+    error("The content of the section " + GroupSec->Name + " is malformed");
+  const ELF::Elf32_Word *Word =
+      reinterpret_cast<const ELF::Elf32_Word *>(GroupSec->Contents.data());
+  const ELF::Elf32_Word *End =
+      Word + GroupSec->Contents.size() / sizeof(ELF::Elf32_Word);
+  GroupSec->setFlagWord(*Word++);
+  for (; Word != End; ++Word) {
+    uint32_t Index = support::endian::read32<ELFT::TargetEndianness>(Word);
+    GroupSec->addMember(SecTable.getSection(
+        Index, "Group member index " + Twine(Index) + " in section " +
+                   GroupSec->Name + " is invalid"));
+  }
+}
+
+template <class ELFT>
+void ELFBuilder<ELFT>::initSymbolTable(SymbolTableSection *SymTab) {
+  const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
+  StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
+
+  for (const auto &Sym : unwrapOrError(ElfFile.symbols(&Shdr))) {
+    SectionBase *DefSection = nullptr;
+    StringRef Name = unwrapOrError(Sym.getName(StrTabData));
+
+    if (Sym.st_shndx >= SHN_LORESERVE) {
+      if (!isValidReservedSectionIndex(Sym.st_shndx, Obj.Machine)) {
+        error(
+            "Symbol '" + Name +
+            "' has unsupported value greater than or equal to SHN_LORESERVE: " +
+            Twine(Sym.st_shndx));
+      }
+    } else if (Sym.st_shndx != SHN_UNDEF) {
+      DefSection = Obj.sections().getSection(
+          Sym.st_shndx, "Symbol '" + Name +
+                            "' is defined in invalid section with index " +
+                            Twine(Sym.st_shndx));
+    }
+
+    SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection,
+                      Sym.getValue(), Sym.st_other, Sym.st_shndx, Sym.st_size);
+  }
+}
+
+template <class ELFT>
+static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, false> &Rel) {}
+
+template <class ELFT>
+static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) {
+  ToSet = Rela.r_addend;
+}
+
+template <class T>
+void initRelocations(RelocationSection *Relocs, SymbolTableSection *SymbolTable,
+                     T RelRange) {
+  for (const auto &Rel : RelRange) {
+    Relocation ToAdd;
+    ToAdd.Offset = Rel.r_offset;
+    getAddend(ToAdd.Addend, Rel);
+    ToAdd.Type = Rel.getType(false);
+    ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Rel.getSymbol(false));
+    Relocs->addRelocation(ToAdd);
+  }
+}
+
+SectionBase *SectionTableRef::getSection(uint16_t Index, Twine ErrMsg) {
+  if (Index == SHN_UNDEF || Index > Sections.size())
+    error(ErrMsg);
+  return Sections[Index - 1].get();
+}
+
+template <class T>
+T *SectionTableRef::getSectionOfType(uint16_t Index, Twine IndexErrMsg,
+                                     Twine TypeErrMsg) {
+  if (T *Sec = dyn_cast<T>(getSection(Index, IndexErrMsg)))
+    return Sec;
+  error(TypeErrMsg);
+}
+
+template <class ELFT>
+SectionBase &ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
+  ArrayRef<uint8_t> Data;
+  switch (Shdr.sh_type) {
+  case SHT_REL:
+  case SHT_RELA:
+    if (Shdr.sh_flags & SHF_ALLOC) {
+      Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
+      return Obj.addSection<DynamicRelocationSection>(Data);
+    }
+    return Obj.addSection<RelocationSection>();
+  case SHT_STRTAB:
+    // If a string table is allocated we don't want to mess with it. That would
+    // mean altering the memory image. There are no special link types or
+    // anything so we can just use a Section.
+    if (Shdr.sh_flags & SHF_ALLOC) {
+      Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
+      return Obj.addSection<Section>(Data);
+    }
+    return Obj.addSection<StringTableSection>();
+  case SHT_HASH:
+  case SHT_GNU_HASH:
+    // Hash tables should refer to SHT_DYNSYM which we're not going to change.
+    // Because of this we don't need to mess with the hash tables either.
+    Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
+    return Obj.addSection<Section>(Data);
+  case SHT_GROUP:
+    Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
+    return Obj.addSection<GroupSection>(Data);
+  case SHT_DYNSYM:
+    Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
+    return Obj.addSection<DynamicSymbolTableSection>(Data);
+  case SHT_DYNAMIC:
+    Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
+    return Obj.addSection<DynamicSection>(Data);
+  case SHT_SYMTAB: {
+    auto &SymTab = Obj.addSection<SymbolTableSection>();
+    Obj.SymbolTable = &SymTab;
+    return SymTab;
+  }
+  case SHT_NOBITS:
+    return Obj.addSection<Section>(Data);
+  default:
+    Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
+    return Obj.addSection<Section>(Data);
+  }
+}
+
+template <class ELFT> void ELFBuilder<ELFT>::readSectionHeaders() {
+  uint32_t Index = 0;
+  for (const auto &Shdr : unwrapOrError(ElfFile.sections())) {
+    if (Index == 0) {
+      ++Index;
+      continue;
+    }
+    auto &Sec = makeSection(Shdr);
+    Sec.Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
+    Sec.Type = Shdr.sh_type;
+    Sec.Flags = Shdr.sh_flags;
+    Sec.Addr = Shdr.sh_addr;
+    Sec.Offset = Shdr.sh_offset;
+    Sec.OriginalOffset = Shdr.sh_offset;
+    Sec.Size = Shdr.sh_size;
+    Sec.Link = Shdr.sh_link;
+    Sec.Info = Shdr.sh_info;
+    Sec.Align = Shdr.sh_addralign;
+    Sec.EntrySize = Shdr.sh_entsize;
+    Sec.Index = Index++;
+  }
+
+  // Now that all of the sections have been added we can fill out some extra
+  // details about symbol tables. We need the symbol table filled out before
+  // any relocations.
+  if (Obj.SymbolTable) {
+    Obj.SymbolTable->initialize(Obj.sections());
+    initSymbolTable(Obj.SymbolTable);
+  }
+
+  // Now that all sections and symbols have been added we can add
+  // relocations that reference symbols and set the link and info fields for
+  // relocation sections.
+  for (auto &Section : Obj.sections()) {
+    if (&Section == Obj.SymbolTable)
+      continue;
+    Section.initialize(Obj.sections());
+    if (auto RelSec = dyn_cast<RelocationSection>(&Section)) {
+      auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
+      if (RelSec->Type == SHT_REL)
+        initRelocations(RelSec, Obj.SymbolTable,
+                        unwrapOrError(ElfFile.rels(Shdr)));
+      else
+        initRelocations(RelSec, Obj.SymbolTable,
+                        unwrapOrError(ElfFile.relas(Shdr)));
+    } else if (auto GroupSec = dyn_cast<GroupSection>(&Section)) {
+      initGroupSection(GroupSec);
+    }
+  }
+}
+
+template <class ELFT> void ELFBuilder<ELFT>::build() {
+  const auto &Ehdr = *ElfFile.getHeader();
+
+  std::copy(Ehdr.e_ident, Ehdr.e_ident + 16, Obj.Ident);
+  Obj.Type = Ehdr.e_type;
+  Obj.Machine = Ehdr.e_machine;
+  Obj.Version = Ehdr.e_version;
+  Obj.Entry = Ehdr.e_entry;
+  Obj.Flags = Ehdr.e_flags;
+
+  readSectionHeaders();
+  readProgramHeaders();
+
+  Obj.SectionNames =
+      Obj.sections().template getSectionOfType<StringTableSection>(
+          Ehdr.e_shstrndx,
+          "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
+              " in elf header " + " is invalid",
+          "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
+              " in elf header " + " is not a string table");
+}
+
+// A generic size function which computes sizes of any random access range.
+template <class R> size_t size(R &&Range) {
+  return static_cast<size_t>(std::end(Range) - std::begin(Range));
+}
+
+Writer::~Writer() {}
+
+Reader::~Reader() {}
+
+ElfType ELFReader::getElfType() const {
+  if (isa<ELFObjectFile<ELF32LE>>(Bin))
+    return ELFT_ELF32LE;
+  if (isa<ELFObjectFile<ELF64LE>>(Bin))
+    return ELFT_ELF64LE;
+  if (isa<ELFObjectFile<ELF32BE>>(Bin))
+    return ELFT_ELF32BE;
+  if (isa<ELFObjectFile<ELF64BE>>(Bin))
+    return ELFT_ELF64BE;
+  llvm_unreachable("Invalid ELFType");
+}
+
+std::unique_ptr<Object> ELFReader::create() const {
+  auto Obj = llvm::make_unique<Object>();
+  if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(Bin)) {
+    ELFBuilder<ELF32LE> Builder(*o, *Obj);
+    Builder.build();
+    return Obj;
+  } else if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(Bin)) {
+    ELFBuilder<ELF64LE> Builder(*o, *Obj);
+    Builder.build();
+    return Obj;
+  } else if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(Bin)) {
+    ELFBuilder<ELF32BE> Builder(*o, *Obj);
+    Builder.build();
+    return Obj;
+  } else if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(Bin)) {
+    ELFBuilder<ELF64BE> Builder(*o, *Obj);
+    Builder.build();
+    return Obj;
+  }
+  error("Invalid file type");
+}
+
+void Object::removeSections(std::function<bool(const SectionBase &)> ToRemove) {
+
+  auto Iter = std::stable_partition(
+      std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
+        if (ToRemove(*Sec))
+          return false;
+        if (auto RelSec = dyn_cast<RelocationSectionBase>(Sec.get())) {
+          if (auto ToRelSec = RelSec->getSection())
+            return !ToRemove(*ToRelSec);
+        }
+        return true;
+      });
+  if (SymbolTable != nullptr && ToRemove(*SymbolTable))
+    SymbolTable = nullptr;
+  if (SectionNames != nullptr && ToRemove(*SectionNames)) {
+    SectionNames = nullptr;
+  }
+  // Now make sure there are no remaining references to the sections that will
+  // be removed. Sometimes it is impossible to remove a reference so we emit
+  // an error here instead.
+  for (auto &RemoveSec : make_range(Iter, std::end(Sections))) {
+    for (auto &Segment : Segments)
+      Segment->removeSection(RemoveSec.get());
+    for (auto &KeepSec : make_range(std::begin(Sections), Iter))
+      KeepSec->removeSectionReferences(RemoveSec.get());
+  }
+  // Now finally get rid of them all togethor.
+  Sections.erase(Iter, std::end(Sections));
+}
+
+void Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
+  if (!SymbolTable)
+    return;
+
+  for (const SecPtr &Sec : Sections)
+    Sec->removeSymbols(ToRemove);
+}
+
+void Object::sortSections() {
+  // Put all sections in offset order. Maintain the ordering as closely as
+  // possible while meeting that demand however.
+  auto CompareSections = [](const SecPtr &A, const SecPtr &B) {
+    return A->OriginalOffset < B->OriginalOffset;
+  };
+  std::stable_sort(std::begin(this->Sections), std::end(this->Sections),
+                   CompareSections);
+}
+
+// This function finds a consistent layout for a list of sections. It assumes
+// that the ->ParentSegment of each section has already been laid out. The
+// supplied starting Offset is used for the starting offset of any section that
+// does not have a ParentSegment. It returns either the offset given if all
+// sections had a ParentSegment or an offset one past the last section if there
+// was a section that didn't have a ParentSegment.
+template <class Range>
+static uint64_t LayoutSections(Range Sections, uint64_t Offset) {
+  // Now the offset of every segment has been set we can assign the offsets
+  // of each section. For sections that are covered by a segment we should use
+  // the segment's original offset and the section's original offset to compute
+  // the offset from the start of the segment. Using the offset from the start
+  // of the segment we can assign a new offset to the section. For sections not
+  // covered by segments we can just bump Offset to the next valid location.
+  uint32_t Index = 1;
+  for (auto &Section : Sections) {
+    Section.Index = Index++;
+    if (Section.ParentSegment != nullptr) {
+      auto Segment = *Section.ParentSegment;
+      Section.Offset =
+          Segment.Offset + (Section.OriginalOffset - Segment.OriginalOffset);
+    } else {
+      Offset = alignTo(Offset, Section.Align == 0 ? 1 : Section.Align);
+      Section.Offset = Offset;
+      if (Section.Type != SHT_NOBITS)
+        Offset += Section.Size;
+    }
+  }
+  return Offset;
+}
+
+void BinaryWriter::write() {
+  for (auto &Section : Obj.sections()) {
+    if ((Section.Flags & SHF_ALLOC) == 0)
+      continue;
+    Section.accept(*SecWriter);
+  }
+  if (auto E = Buf.commit())
+    reportError(Buf.getName(), errorToErrorCode(std::move(E)));
+}
+
+int BinaryWriter::finalize() {
+  // TODO: Create a filter range to construct OrderedSegments from so that this
+  // code can be deduped with assignOffsets above. This should also solve the
+  // todo below for LayoutSections.
+  // We need a temporary list of segments that has a special order to it
+  // so that we know that anytime ->ParentSegment is set that segment has
+  // already had it's offset properly set. We only want to consider the segments
+  // that will affect layout of allocated sections so we only add those.
+  std::vector<Segment *> OrderedSegments;
+  for (auto &Section : Obj.sections()) {
+    if ((Section.Flags & SHF_ALLOC) != 0 && Section.ParentSegment != nullptr) {
+      OrderedSegments.push_back(Section.ParentSegment);
+    }
+  }
+
+  // For binary output, we're going to use physical addresses instead of
+  // virtual addresses, since a binary output is used for cases like ROM
+  // loading and physical addresses are intended for ROM loading.
+  // However, if no segment has a physical address, we'll fallback to using
+  // virtual addresses for all.
+  if (std::all_of(std::begin(OrderedSegments), std::end(OrderedSegments),
+                  [](const Segment *Segment) { return Segment->PAddr == 0; }))
+    for (const auto &Segment : OrderedSegments)
+      Segment->PAddr = Segment->VAddr;
+
+  std::stable_sort(std::begin(OrderedSegments), std::end(OrderedSegments),
+                   compareSegmentsByPAddr);
+
+  // Because we add a ParentSegment for each section we might have duplicate
+  // segments in OrderedSegments. If there were duplicates then LayoutSegments
+  // would do very strange things.
+  auto End =
+      std::unique(std::begin(OrderedSegments), std::end(OrderedSegments));
+  OrderedSegments.erase(End, std::end(OrderedSegments));
+
+  uint64_t Offset = 0;
+
+  // Modify the first segment so that there is no gap at the start. This allows
+  // our layout algorithm to proceed as expected while not out writing out the
+  // gap at the start.
+  if (!OrderedSegments.empty()) {
+    auto Seg = OrderedSegments[0];
+    auto Sec = Seg->firstSection();
+    auto Diff = Sec->OriginalOffset - Seg->OriginalOffset;
+    Seg->OriginalOffset += Diff;
+    // The size needs to be shrunk as well.
+    Seg->FileSize -= Diff;
+    // The PAddr needs to be increased to remove the gap before the first
+    // section.
+    Seg->PAddr += Diff;
+    uint64_t LowestPAddr = Seg->PAddr;
+    for (auto &Segment : OrderedSegments) {
+      Segment->Offset = Segment->PAddr - LowestPAddr;
+      Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
+    }
+  }
+
+  // TODO: generalize LayoutSections to take a range. Pass a special range
+  // constructed from an iterator that skips values for which a predicate does
+  // not hold. Then pass such a range to LayoutSections instead of constructing
+  // AllocatedSections here.
+  std::vector<SectionBase *> AllocatedSections;
+  for (auto &Section : Obj.sections()) {
+    if ((Section.Flags & SHF_ALLOC) == 0)
+      continue;
+    AllocatedSections.push_back(&Section);
+  }
+  LayoutSections(make_pointee_range(AllocatedSections), Offset);
+
+  // Now that every section has been laid out we just need to compute the total
+  // file size. This might not be the same as the offset returned by
+  // LayoutSections, because we want to truncate the last segment to the end of
+  // its last section, to match GNU objcopy's behaviour.
+  TotalSize = 0;
+  for (const auto &Section : AllocatedSections) {
+    if (Section->Type != SHT_NOBITS)
+      TotalSize = std::max(TotalSize, Section->Offset + Section->Size);
+  }
+
+  Buf.allocate(TotalSize);
+  SecWriter = llvm::make_unique<BinarySectionWriter>(Buf);
+  return TotalSize;
+}
Index: clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
===================================================================
--- clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
+++ clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
@@ -1,5 +1,22 @@
-set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} Support)
+set(LLVM_LINK_COMPONENTS
+  Core
+  IRReader
+  MC
+  Support
+  Analysis
+)
+
+# Depend on LLVM IR instrinsic generation.
+set(handle_llvm_deps intrinsics_gen)
+if (CLANG_BUILT_STANDALONE)
+  set(handle_llvm_deps)
+endif()
 
 add_clang_library(clangHandleLLVM
   handle_llvm.cpp
+  Object.cpp
+  fuzzer_initialize.cpp
+  
+  DEPENDS
+  ${handle_llvm_deps}
   )
Index: clang/tools/clang-fuzzer/ExampleClangLLVMProtoFuzzer.cpp
===================================================================
--- clang/tools/clang-fuzzer/ExampleClangLLVMProtoFuzzer.cpp
+++ clang/tools/clang-fuzzer/ExampleClangLLVMProtoFuzzer.cpp
@@ -15,14 +15,14 @@
 //===----------------------------------------------------------------------===//
 
 #include "cxx_loop_proto.pb.h"
-#include "fuzzer-initialize/fuzzer_initialize.h"
+#include "handle-llvm/fuzzer_initialize.h"
 #include "handle-llvm/handle_llvm.h"
 #include "proto-to-llvm/loop_proto_to_llvm.h"
 #include "src/libfuzzer/libfuzzer_macro.h"
 
 using namespace clang_fuzzer;
 
 DEFINE_BINARY_PROTO_FUZZER(const LoopFunction &input) {
   auto S = LoopFunctionToLLVMString(input);
-  HandleLLVM(S, GetCLArgs());
+  HandleLLVM(S, GetCLArgs(), GetRegion1(), GetRegion2());
 }
Index: clang/tools/clang-fuzzer/CMakeLists.txt
===================================================================
--- clang/tools/clang-fuzzer/CMakeLists.txt
+++ clang/tools/clang-fuzzer/CMakeLists.txt
@@ -79,19 +79,20 @@
     ${ProtobufMutator_LIBRARIES}
     ${PROTOBUF_LIBRARIES}
     ${LLVM_LIB_FUZZING_ENGINE}
-    clangFuzzerInitialize
     )
 
   target_link_libraries(clang-proto-fuzzer
     PRIVATE
     ${COMMON_PROTO_FUZZ_LIBRARIES}
+    clangFuzzerInitialize
     clangHandleCXX
     clangCXXProto
     clangProtoToCXX
     )
   target_link_libraries(clang-loop-proto-fuzzer
     PRIVATE
     ${COMMON_PROTO_FUZZ_LIBRARIES}
+    clangFuzzerInitialize
     clangHandleCXX
     clangCXXLoopProto
     clangLoopProtoToCXX
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to