[dsymutil, llvm-objcopy] Fix some Clang-tidy modernize and Include What You Use warnings; other minor fixes (NFC).

llvm-svn: 317123
diff --git a/llvm/tools/llvm-objcopy/Object.cpp b/llvm/tools/llvm-objcopy/Object.cpp
index f9acf00..22ae47f 100644
--- a/llvm/tools/llvm-objcopy/Object.cpp
+++ b/llvm/tools/llvm-objcopy/Object.cpp
@@ -1,4 +1,4 @@
-//===- Object.cpp -----------------------------------------------*- C++ -*-===//
+//===- Object.cpp ---------------------------------------------------------===//
 //
 //                      The LLVM Compiler Infrastructure
 //
@@ -6,16 +6,32 @@
 // License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
+
 #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 <algorithm>
+#include <cstddef>
+#include <cstdint>
+#include <iterator>
+#include <utility>
+#include <vector>
 
 using namespace llvm;
 using namespace object;
 using namespace ELF;
 
 template <class ELFT> void Segment::writeHeader(FileOutputBuffer &Out) const {
-  typedef typename ELFT::Ehdr Elf_Ehdr;
-  typedef typename ELFT::Phdr Elf_Phdr;
+  using Elf_Ehdr = typename ELFT::Ehdr;
+  using Elf_Phdr = typename ELFT::Phdr;
 
   uint8_t *Buf = Out.getBufferStart();
   Buf += sizeof(Elf_Ehdr) + Index * sizeof(Elf_Phdr);
@@ -191,8 +207,7 @@
 }
 
 template <class ELFT>
-void SymbolTableSectionImpl<ELFT>::writeSection(
-    llvm::FileOutputBuffer &Out) const {
+void SymbolTableSectionImpl<ELFT>::writeSection(FileOutputBuffer &Out) const {
   uint8_t *Buf = Out.getBufferStart();
   Buf += Offset;
   typename ELFT::Sym *Sym = reinterpret_cast<typename ELFT::Sym *>(Buf);
@@ -212,9 +227,10 @@
 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 " +
+    error("Symbol table " + Symbols->Name +
+          " cannot be removed because it is "
+          "referenced by the relocation "
+          "section " +
           this->Name);
   }
 }
@@ -229,9 +245,9 @@
           " is not a symbol table"));
 
   if (Info != SHN_UNDEF)
-    setSection(SecTable.getSection(Info,
-                                   "Info field value " + Twine(Info) +
-                                       " in section " + Name + " is invalid"));
+    setSection(SecTable.getSection(Info, "Info field value " + Twine(Info) +
+                                             " in section " + Name +
+                                             " is invalid"));
   else
     setSection(nullptr);
 }
@@ -263,7 +279,7 @@
 }
 
 template <class ELFT>
-void RelocationSection<ELFT>::writeSection(llvm::FileOutputBuffer &Out) const {
+void RelocationSection<ELFT>::writeSection(FileOutputBuffer &Out) const {
   uint8_t *Buf = Out.getBufferStart() + Offset;
   if (Type == SHT_REL)
     writeRel(reinterpret_cast<Elf_Rel *>(Buf));
@@ -271,15 +287,16 @@
     writeRel(reinterpret_cast<Elf_Rela *>(Buf));
 }
 
-void DynamicRelocationSection::writeSection(llvm::FileOutputBuffer &Out) const {
+void DynamicRelocationSection::writeSection(FileOutputBuffer &Out) const {
   std::copy(std::begin(Contents), std::end(Contents),
             Out.getBufferStart() + Offset);
 }
 
 void SectionWithStrTab::removeSectionReferences(const SectionBase *Sec) {
   if (StrTab == Sec) {
-    error("String table " + StrTab->Name + " cannot be removed because it is "
-                                           "referenced by the section " +
+    error("String table " + StrTab->Name +
+          " cannot be removed because it is "
+          "referenced by the section " +
           this->Name);
   }
 }
@@ -289,9 +306,9 @@
 }
 
 void SectionWithStrTab::initialize(SectionTableRef SecTable) {
-  auto StrTab = SecTable.getSection(Link,
-                                    "Link field value " + Twine(Link) +
-                                        " in section " + Name + " is invalid");
+  auto StrTab =
+      SecTable.getSection(Link, "Link field value " + Twine(Link) +
+                                    " in section " + Name + " is invalid");
   if (StrTab->Type != SHT_STRTAB) {
     error("Link field value " + Twine(Link) + " in section " + Name +
           " is not a string table");
@@ -377,10 +394,9 @@
 }
 
 template <class ELFT>
-void Object<ELFT>::initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
+void Object<ELFT>::initSymbolTable(const object::ELFFile<ELFT> &ElfFile,
                                    SymbolTableSection *SymTab,
                                    SectionTableRef SecTable) {
-
   const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
   StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
 
@@ -397,9 +413,9 @@
       }
     } else if (Sym.st_shndx != SHN_UNDEF) {
       DefSection = SecTable.getSection(
-          Sym.st_shndx,
-          "Symbol '" + Name + "' is defined in invalid section with index " +
-              Twine(Sym.st_shndx));
+          Sym.st_shndx, "Symbol '" + Name +
+                            "' is defined in invalid section with index " +
+                            Twine(Sym.st_shndx));
     }
 
     SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection,
@@ -437,14 +453,14 @@
 template <class T>
 T *SectionTableRef::getSectionOfType(uint16_t Index, Twine IndexErrMsg,
                                      Twine TypeErrMsg) {
-  if (T *Sec = llvm::dyn_cast<T>(getSection(Index, IndexErrMsg)))
+  if (T *Sec = dyn_cast<T>(getSection(Index, IndexErrMsg)))
     return Sec;
   error(TypeErrMsg);
 }
 
 template <class ELFT>
 std::unique_ptr<SectionBase>
-Object<ELFT>::makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
+Object<ELFT>::makeSection(const object::ELFFile<ELFT> &ElfFile,
                           const Elf_Shdr &Shdr) {
   ArrayRef<uint8_t> Data;
   switch (Shdr.sh_type) {
@@ -621,7 +637,7 @@
 template <class ELFT>
 void Object<ELFT>::writeSectionData(FileOutputBuffer &Out) const {
   for (auto &Section : Sections)
-      Section->writeSection(Out);
+    Section->writeSection(Out);
 }
 
 template <class ELFT>
@@ -797,15 +813,13 @@
   for (auto &Segment : this->Segments) {
     // GNU objcopy does not output segments that do not cover a section. Such
     // segments can sometimes be produced by LLD due to how LLD handles PT_PHDR.
-    if (Segment->Type == llvm::ELF::PT_LOAD &&
-        Segment->firstSection() != nullptr) {
+    if (Segment->Type == PT_LOAD && Segment->firstSection() != nullptr) {
       Segment->writeSegment(Out);
     }
   }
 }
 
 template <class ELFT> void BinaryObject<ELFT>::finalize() {
-
   // Put all segments in offset order.
   auto CompareSegments = [](const SegPtr &A, const SegPtr &B) {
     return A->Offset < B->Offset;
@@ -815,8 +829,7 @@
 
   uint64_t Offset = 0;
   for (auto &Segment : this->Segments) {
-    if (Segment->Type == llvm::ELF::PT_LOAD &&
-        Segment->firstSection() != nullptr) {
+    if (Segment->Type == PT_LOAD && Segment->firstSection() != nullptr) {
       Offset = alignTo(Offset, Segment->Align);
       Segment->Offset = Offset;
       Offset += Segment->FileSize;
@@ -825,6 +838,8 @@
   TotalSize = Offset;
 }
 
+namespace llvm {
+
 template class Object<ELF64LE>;
 template class Object<ELF64BE>;
 template class Object<ELF32LE>;
@@ -839,3 +854,5 @@
 template class BinaryObject<ELF64BE>;
 template class BinaryObject<ELF32LE>;
 template class BinaryObject<ELF32BE>;
+
+} // end namespace llvm
diff --git a/llvm/tools/llvm-objcopy/Object.h b/llvm/tools/llvm-objcopy/Object.h
index f608843..9c77f59 100644
--- a/llvm/tools/llvm-objcopy/Object.h
+++ b/llvm/tools/llvm-objcopy/Object.h
@@ -7,38 +7,46 @@
 //
 //===----------------------------------------------------------------------===//
 
-#ifndef LLVM_OBJCOPY_OBJECT_H
-#define 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 <cstddef>
+#include <cstdint>
+#include <functional>
 #include <memory>
 #include <set>
+#include <vector>
 
-class Segment;
+namespace llvm {
+
+class FileOutputBuffer;
 class SectionBase;
+class Segment;
 
 class SectionTableRef {
 private:
-  llvm::ArrayRef<std::unique_ptr<SectionBase>> Sections;
+  ArrayRef<std::unique_ptr<SectionBase>> Sections;
 
 public:
-  SectionTableRef(llvm::ArrayRef<std::unique_ptr<SectionBase>> Secs)
+  SectionTableRef(ArrayRef<std::unique_ptr<SectionBase>> Secs)
       : Sections(Secs) {}
   SectionTableRef(const SectionTableRef &) = default;
 
-  SectionBase *getSection(uint16_t Index, llvm::Twine ErrMsg);
+  SectionBase *getSection(uint16_t Index, Twine ErrMsg);
 
   template <class T>
-  T *getSectionOfType(uint16_t Index, llvm::Twine IndexErrMsg,
-                      llvm::Twine TypeErrMsg);
+  T *getSectionOfType(uint16_t Index, Twine IndexErrMsg, Twine TypeErrMsg);
 };
 
 class SectionBase {
 public:
-  llvm::StringRef Name;
+  StringRef Name;
   Segment *ParentSegment = nullptr;
   uint64_t HeaderOffset;
   uint64_t OriginalOffset;
@@ -49,18 +57,19 @@
   uint32_t EntrySize = 0;
   uint64_t Flags = 0;
   uint64_t Info = 0;
-  uint64_t Link = llvm::ELF::SHN_UNDEF;
+  uint64_t Link = ELF::SHN_UNDEF;
   uint64_t NameIndex = 0;
   uint64_t Offset = 0;
   uint64_t Size = 0;
-  uint64_t Type = llvm::ELF::SHT_NULL;
+  uint64_t Type = ELF::SHT_NULL;
 
-  virtual ~SectionBase() {}
+  virtual ~SectionBase() = default;
+
   virtual void initialize(SectionTableRef SecTable);
   virtual void finalize();
   virtual void removeSectionReferences(const SectionBase *Sec);
-  template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const;
-  virtual void writeSection(llvm::FileOutputBuffer &Out) const = 0;
+  template <class ELFT> void writeHeader(FileOutputBuffer &Out) const;
+  virtual void writeSection(FileOutputBuffer &Out) const = 0;
 };
 
 class Segment {
@@ -77,7 +86,7 @@
   };
 
   std::set<const SectionBase *, SectionCompare> Sections;
-  llvm::ArrayRef<uint8_t> Contents;
+  ArrayRef<uint8_t> Contents;
 
 public:
   uint64_t Align;
@@ -93,25 +102,28 @@
   uint64_t OriginalOffset;
   Segment *ParentSegment = nullptr;
 
-  Segment(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
+  Segment(ArrayRef<uint8_t> Data) : Contents(Data) {}
+
   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); }
-  template <class ELFT> void writeHeader(llvm::FileOutputBuffer &Out) const;
-  void writeSegment(llvm::FileOutputBuffer &Out) const;
+  template <class ELFT> void writeHeader(FileOutputBuffer &Out) const;
+  void writeSegment(FileOutputBuffer &Out) const;
 };
 
 class Section : public SectionBase {
 private:
-  llvm::ArrayRef<uint8_t> Contents;
+  ArrayRef<uint8_t> Contents;
 
 public:
-  Section(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
-  void writeSection(llvm::FileOutputBuffer &Out) const override;
+  Section(ArrayRef<uint8_t> Data) : Contents(Data) {}
+
+  void writeSection(FileOutputBuffer &Out) const override;
 };
 
 // There are two types of string tables that can exist, dynamic and not dynamic.
@@ -124,21 +136,22 @@
 // then agrees with the makeSection method used to construct most sections.
 class StringTableSection : public SectionBase {
 private:
-  llvm::StringTableBuilder StrTabBuilder;
+  StringTableBuilder StrTabBuilder;
 
 public:
-  StringTableSection() : StrTabBuilder(llvm::StringTableBuilder::ELF) {
-    Type = llvm::ELF::SHT_STRTAB;
+  StringTableSection() : StrTabBuilder(StringTableBuilder::ELF) {
+    Type = ELF::SHT_STRTAB;
   }
 
-  void addString(llvm::StringRef Name);
-  uint32_t findIndex(llvm::StringRef Name) const;
+  void addString(StringRef Name);
+  uint32_t findIndex(StringRef Name) const;
   void finalize() override;
-  void writeSection(llvm::FileOutputBuffer &Out) const override;
+  void writeSection(FileOutputBuffer &Out) const override;
+
   static bool classof(const SectionBase *S) {
-    if (S->Flags & llvm::ELF::SHF_ALLOC)
+    if (S->Flags & ELF::SHF_ALLOC)
       return false;
-    return S->Type == llvm::ELF::SHT_STRTAB;
+    return S->Type == ELF::SHT_STRTAB;
   }
 };
 
@@ -148,12 +161,12 @@
 // SYMBOL_SIMPLE_INDEX means that the st_shndx is just an index of a section.
 enum SymbolShndxType {
   SYMBOL_SIMPLE_INDEX = 0,
-  SYMBOL_ABS = llvm::ELF::SHN_ABS,
-  SYMBOL_COMMON = llvm::ELF::SHN_COMMON,
-  SYMBOL_HEXAGON_SCOMMON = llvm::ELF::SHN_HEXAGON_SCOMMON,
-  SYMBOL_HEXAGON_SCOMMON_2 = llvm::ELF::SHN_HEXAGON_SCOMMON_2,
-  SYMBOL_HEXAGON_SCOMMON_4 = llvm::ELF::SHN_HEXAGON_SCOMMON_4,
-  SYMBOL_HEXAGON_SCOMMON_8 = llvm::ELF::SHN_HEXAGON_SCOMMON_8,
+  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 {
@@ -161,7 +174,7 @@
   SectionBase *DefinedIn = nullptr;
   SymbolShndxType ShndxType;
   uint32_t Index;
-  llvm::StringRef Name;
+  StringRef Name;
   uint32_t NameIndex;
   uint64_t Size;
   uint8_t Type;
@@ -175,11 +188,11 @@
   std::vector<std::unique_ptr<Symbol>> Symbols;
   StringTableSection *SymbolNames = nullptr;
 
-  typedef std::unique_ptr<Symbol> SymPtr;
+  using SymPtr = std::unique_ptr<Symbol>;
 
 public:
   void setStrTab(StringTableSection *StrTab) { SymbolNames = StrTab; }
-  void addSymbol(llvm::StringRef Name, uint8_t Bind, uint8_t Type,
+  void addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
                  SectionBase *DefinedIn, uint64_t Value, uint16_t Shndx,
                  uint64_t Sz);
   void addSymbolNames();
@@ -187,14 +200,15 @@
   void removeSectionReferences(const SectionBase *Sec) override;
   void initialize(SectionTableRef SecTable) override;
   void finalize() override;
+
   static bool classof(const SectionBase *S) {
-    return S->Type == llvm::ELF::SHT_SYMTAB;
+    return S->Type == ELF::SHT_SYMTAB;
   }
 };
 
 // Only writeSection depends on the ELF type so we implement it in a subclass.
 template <class ELFT> class SymbolTableSectionImpl : public SymbolTableSection {
-  void writeSection(llvm::FileOutputBuffer &Out) const override;
+  void writeSection(FileOutputBuffer &Out) const override;
 };
 
 struct Relocation {
@@ -222,7 +236,7 @@
   void setSection(SectionBase *Sec) { SecToApplyRel = Sec; }
 
   static bool classof(const SectionBase *S) {
-    return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA;
+    return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
   }
 };
 
@@ -234,11 +248,10 @@
   SymTabType *Symbols = nullptr;
 
 protected:
-  RelocSectionWithSymtabBase() {}
+  RelocSectionWithSymtabBase() = default;
 
 public:
   void setSymTab(SymTabType *StrTab) { Symbols = StrTab; }
-
   void removeSectionReferences(const SectionBase *Sec) override;
   void initialize(SectionTableRef SecTable) override;
   void finalize() override;
@@ -248,8 +261,8 @@
 class RelocationSection
     : public RelocSectionWithSymtabBase<SymbolTableSection> {
 private:
-  typedef typename ELFT::Rel Elf_Rel;
-  typedef typename ELFT::Rela Elf_Rela;
+  using Elf_Rel = typename ELFT::Rel;
+  using Elf_Rela = typename ELFT::Rela;
 
   std::vector<Relocation> Relocations;
 
@@ -257,12 +270,12 @@
 
 public:
   void addRelocation(Relocation Rel) { Relocations.push_back(Rel); }
-  void writeSection(llvm::FileOutputBuffer &Out) const override;
+  void writeSection(FileOutputBuffer &Out) const override;
 
   static bool classof(const SectionBase *S) {
-    if (S->Flags & llvm::ELF::SHF_ALLOC)
+    if (S->Flags & ELF::SHF_ALLOC)
       return false;
-    return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA;
+    return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
   }
 };
 
@@ -271,7 +284,8 @@
   const SectionBase *StrTab = nullptr;
 
 public:
-  SectionWithStrTab(llvm::ArrayRef<uint8_t> Data) : Section(Data) {}
+  SectionWithStrTab(ArrayRef<uint8_t> Data) : Section(Data) {}
+
   void setStrTab(const SectionBase *StringTable) { StrTab = StringTable; }
   void removeSectionReferences(const SectionBase *Sec) override;
   void initialize(SectionTableRef SecTable) override;
@@ -281,51 +295,54 @@
 
 class DynamicSymbolTableSection : public SectionWithStrTab {
 public:
-  DynamicSymbolTableSection(llvm::ArrayRef<uint8_t> Data)
-      : SectionWithStrTab(Data) {}
+  DynamicSymbolTableSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
+
   static bool classof(const SectionBase *S) {
-    return S->Type == llvm::ELF::SHT_DYNSYM;
+    return S->Type == ELF::SHT_DYNSYM;
   }
 };
 
 class DynamicSection : public SectionWithStrTab {
 public:
-  DynamicSection(llvm::ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
+  DynamicSection(ArrayRef<uint8_t> Data) : SectionWithStrTab(Data) {}
+
   static bool classof(const SectionBase *S) {
-    return S->Type == llvm::ELF::SHT_DYNAMIC;
+    return S->Type == ELF::SHT_DYNAMIC;
   }
 };
 
 class DynamicRelocationSection
     : public RelocSectionWithSymtabBase<DynamicSymbolTableSection> {
 private:
-  llvm::ArrayRef<uint8_t> Contents;
+  ArrayRef<uint8_t> Contents;
 
 public:
-  DynamicRelocationSection(llvm::ArrayRef<uint8_t> Data) : Contents(Data) {}
-  void writeSection(llvm::FileOutputBuffer &Out) const override;
+  DynamicRelocationSection(ArrayRef<uint8_t> Data) : Contents(Data) {}
+
+  void writeSection(FileOutputBuffer &Out) const override;
+
   static bool classof(const SectionBase *S) {
-    if (!(S->Flags & llvm::ELF::SHF_ALLOC))
+    if (!(S->Flags & ELF::SHF_ALLOC))
       return false;
-    return S->Type == llvm::ELF::SHT_REL || S->Type == llvm::ELF::SHT_RELA;
+    return S->Type == ELF::SHT_REL || S->Type == ELF::SHT_RELA;
   }
 };
 
 template <class ELFT> class Object {
 private:
-  typedef std::unique_ptr<SectionBase> SecPtr;
-  typedef std::unique_ptr<Segment> SegPtr;
+  using SecPtr = std::unique_ptr<SectionBase>;
+  using SegPtr = std::unique_ptr<Segment>;
 
-  typedef typename ELFT::Shdr Elf_Shdr;
-  typedef typename ELFT::Ehdr Elf_Ehdr;
-  typedef typename ELFT::Phdr Elf_Phdr;
+  using Elf_Shdr = typename ELFT::Shdr;
+  using Elf_Ehdr = typename ELFT::Ehdr;
+  using Elf_Phdr = typename ELFT::Phdr;
 
-  void initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
+  void initSymbolTable(const object::ELFFile<ELFT> &ElfFile,
                        SymbolTableSection *SymTab, SectionTableRef SecTable);
-  SecPtr makeSection(const llvm::object::ELFFile<ELFT> &ElfFile,
+  SecPtr makeSection(const object::ELFFile<ELFT> &ElfFile,
                      const Elf_Shdr &Shdr);
-  void readProgramHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
-  SectionTableRef readSectionHeaders(const llvm::object::ELFFile<ELFT> &ElfFile);
+  void readProgramHeaders(const object::ELFFile<ELFT> &ElfFile);
+  SectionTableRef readSectionHeaders(const object::ELFFile<ELFT> &ElfFile);
 
 protected:
   StringTableSection *SectionNames = nullptr;
@@ -333,10 +350,10 @@
   std::vector<SecPtr> Sections;
   std::vector<SegPtr> Segments;
 
-  void writeHeader(llvm::FileOutputBuffer &Out) const;
-  void writeProgramHeaders(llvm::FileOutputBuffer &Out) const;
-  void writeSectionData(llvm::FileOutputBuffer &Out) const;
-  void writeSectionHeaders(llvm::FileOutputBuffer &Out) const;
+  void writeHeader(FileOutputBuffer &Out) const;
+  void writeProgramHeaders(FileOutputBuffer &Out) const;
+  void writeSectionData(FileOutputBuffer &Out) const;
+  void writeSectionHeaders(FileOutputBuffer &Out) const;
 
 public:
   uint8_t Ident[16];
@@ -348,45 +365,50 @@
   uint32_t Flags;
   bool WriteSectionHeaders = true;
 
-  Object(const llvm::object::ELFObjectFile<ELFT> &Obj);
+  Object(const object::ELFObjectFile<ELFT> &Obj);
+  virtual ~Object() = default;
+
   void removeSections(std::function<bool(const SectionBase &)> ToRemove);
   virtual size_t totalSize() const = 0;
   virtual void finalize() = 0;
-  virtual void write(llvm::FileOutputBuffer &Out) const = 0;
-  virtual ~Object() = default;
+  virtual void write(FileOutputBuffer &Out) const = 0;
 };
 
 template <class ELFT> class ELFObject : public Object<ELFT> {
 private:
-  typedef std::unique_ptr<SectionBase> SecPtr;
-  typedef std::unique_ptr<Segment> SegPtr;
+  using SecPtr = std::unique_ptr<SectionBase>;
+  using SegPtr = std::unique_ptr<Segment>;
 
-  typedef typename ELFT::Shdr Elf_Shdr;
-  typedef typename ELFT::Ehdr Elf_Ehdr;
-  typedef typename ELFT::Phdr Elf_Phdr;
+  using Elf_Shdr = typename ELFT::Shdr;
+  using Elf_Ehdr = typename ELFT::Ehdr;
+  using Elf_Phdr = typename ELFT::Phdr;
 
   void sortSections();
   void assignOffsets();
 
 public:
-  ELFObject(const llvm::object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
+  ELFObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
+
   void finalize() override;
   size_t totalSize() const override;
-  void write(llvm::FileOutputBuffer &Out) const override;
+  void write(FileOutputBuffer &Out) const override;
 };
 
 template <class ELFT> class BinaryObject : public Object<ELFT> {
 private:
-  typedef std::unique_ptr<SectionBase> SecPtr;
-  typedef std::unique_ptr<Segment> SegPtr;
+  using SecPtr = std::unique_ptr<SectionBase>;
+  using SegPtr = std::unique_ptr<Segment>;
 
   uint64_t TotalSize;
 
 public:
-  BinaryObject(const llvm::object::ELFObjectFile<ELFT> &Obj)
-      : Object<ELFT>(Obj) {}
+  BinaryObject(const object::ELFObjectFile<ELFT> &Obj) : Object<ELFT>(Obj) {}
+
   void finalize() override;
   size_t totalSize() const override;
-  void write(llvm::FileOutputBuffer &Out) const override;
+  void write(FileOutputBuffer &Out) const override;
 };
-#endif
+
+} // end namespace llvm
+
+#endif // LLVM_TOOLS_OBJCOPY_OBJECT_H
diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
index 7f55a43..f3e9c77 100644
--- a/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
+++ b/llvm/tools/llvm-objcopy/llvm-objcopy.cpp
@@ -1,4 +1,4 @@
-//===- llvm-objcopy.cpp -----------------------------------------*- C++ -*-===//
+//===- llvm-objcopy.cpp ---------------------------------------------------===//
 //
 //                      The LLVM Compiler Infrastructure
 //
@@ -6,17 +6,37 @@
 // License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
+
 #include "llvm-objcopy.h"
 #include "Object.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/Twine.h"
+#include "llvm/BinaryFormat/ELF.h"
+#include "llvm/Object/Binary.h"
+#include "llvm/Object/ELFObjectFile.h"
+#include "llvm/Object/ELFTypes.h"
+#include "llvm/Object/Error.h"
+#include "llvm/Support/Casting.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/Compiler.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/ErrorOr.h"
 #include "llvm/Support/FileOutputBuffer.h"
+#include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/PrettyStackTrace.h"
 #include "llvm/Support/Signals.h"
-#include "llvm/Support/ToolOutputFile.h"
-
+#include "llvm/Support/raw_ostream.h"
+#include <algorithm>
+#include <cassert>
+#include <cstdlib>
+#include <functional>
+#include <iterator>
 #include <memory>
 #include <string>
 #include <system_error>
+#include <utility>
 
 using namespace llvm;
 using namespace object;
@@ -39,7 +59,7 @@
   exit(1);
 }
 
-LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, llvm::Error E) {
+LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) {
   assert(E);
   std::string Buf;
   raw_string_ostream OS(Buf);
@@ -48,22 +68,23 @@
   errs() << ToolName << ": '" << File << "': " << Buf;
   exit(1);
 }
-}
 
-cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input>"));
-cl::opt<std::string> OutputFilename(cl::Positional, cl::desc("<output>"),
+} // end namespace llvm
+
+static cl::opt<std::string> InputFilename(cl::Positional, cl::desc("<input>"));
+static cl::opt<std::string> OutputFilename(cl::Positional, cl::desc("<output>"),
                                     cl::init("-"));
-cl::opt<std::string>
+static cl::opt<std::string>
     OutputFormat("O", cl::desc("set output format to one of the following:"
                                "\n\tbinary"));
-cl::list<std::string> ToRemove("remove-section",
-                               cl::desc("Remove a specific section"));
-cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"),
-                    cl::aliasopt(ToRemove));
-cl::opt<bool> StripSections("strip-sections",
-                            cl::desc("Remove all section headers"));
+static cl::list<std::string> ToRemove("remove-section",
+                                      cl::desc("Remove a specific section"));
+static cl::alias ToRemoveA("R", cl::desc("Alias for remove-section"),
+                           cl::aliasopt(ToRemove));
+static cl::opt<bool> StripSections("strip-sections",
+                                   cl::desc("Remove all section headers"));
 
-typedef std::function<bool(const SectionBase &Sec)> SectionPred;
+using SectionPred = std::function<bool(const SectionBase &Sec)>;
 
 void CopyBinary(const ELFObjectFile<ELF64LE> &ObjFile) {
   std::unique_ptr<FileOutputBuffer> Buffer;
diff --git a/llvm/tools/llvm-objcopy/llvm-objcopy.h b/llvm/tools/llvm-objcopy/llvm-objcopy.h
index de7bf36..6732e41 100644
--- a/llvm/tools/llvm-objcopy/llvm-objcopy.h
+++ b/llvm/tools/llvm-objcopy/llvm-objcopy.h
@@ -6,11 +6,15 @@
 // License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
-#ifndef LLVM_OBJCOPY_H
-#define 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 {
 
@@ -27,6 +31,7 @@
   OS.flush();
   error(Buf);
 }
-}
 
-#endif
+} // end namespace llvm
+
+#endif // LLVM_TOOLS_OBJCOPY_OBJCOPY_H