[lld] r285740 - Create SyntheticSections.cpp.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 1 13:28:22 PDT 2016


Author: ruiu
Date: Tue Nov  1 15:28:21 2016
New Revision: 285740

URL: http://llvm.org/viewvc/llvm-project?rev=285740&view=rev
Log:
Create SyntheticSections.cpp.

We are going to have many more classes for linker-synthesized
input sections, so it's worth to be added to a separate file
than to the file for regular input sections.

Added:
    lld/trunk/ELF/SyntheticSections.cpp
    lld/trunk/ELF/SyntheticSections.h
Modified:
    lld/trunk/ELF/CMakeLists.txt
    lld/trunk/ELF/InputSection.cpp
    lld/trunk/ELF/InputSection.h
    lld/trunk/ELF/OutputSections.h
    lld/trunk/ELF/Writer.cpp

Modified: lld/trunk/ELF/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/CMakeLists.txt?rev=285740&r1=285739&r2=285740&view=diff
==============================================================================
--- lld/trunk/ELF/CMakeLists.txt (original)
+++ lld/trunk/ELF/CMakeLists.txt Tue Nov  1 15:28:21 2016
@@ -23,6 +23,7 @@ add_lld_library(lldELF
   SymbolListFile.cpp
   SymbolTable.cpp
   Symbols.cpp
+  SyntheticSections.cpp
   Target.cpp
   Thunks.cpp
   Writer.cpp

Modified: lld/trunk/ELF/InputSection.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.cpp?rev=285740&r1=285739&r2=285740&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.cpp (original)
+++ lld/trunk/ELF/InputSection.cpp Tue Nov  1 15:28:21 2016
@@ -19,8 +19,6 @@
 
 #include "llvm/Support/Compression.h"
 #include "llvm/Support/Endian.h"
-#include "llvm/Support/RandomNumberGenerator.h"
-#include "llvm/Support/xxhash.h"
 
 using namespace llvm;
 using namespace llvm::ELF;
@@ -852,65 +850,6 @@ InputSection<ELFT> InputSection<ELFT>::c
   return Ret;
 }
 
-template <class ELFT>
-BuildIdSection<ELFT>::BuildIdSection(size_t HashSize)
-    : InputSection<ELFT>(SHF_ALLOC, SHT_NOTE, 1, ArrayRef<uint8_t>(),
-                         ".note.gnu.build-id") {
-  Buf.resize(16 + HashSize);
-  const endianness E = ELFT::TargetEndianness;
-  write32<E>(Buf.data(), 4);                   // Name size
-  write32<E>(Buf.data() + 4, HashSize);        // Content size
-  write32<E>(Buf.data() + 8, NT_GNU_BUILD_ID); // Type
-  memcpy(Buf.data() + 12, "GNU", 4);           // Name string
-  this->Data = ArrayRef<uint8_t>(Buf);
-}
-
-template <class ELFT>
-uint8_t *BuildIdSection<ELFT>::getOutputLoc(uint8_t *Start) const {
-  return Start + this->OutSec->getFileOffset() + this->OutSecOff;
-}
-
-template <class ELFT>
-void BuildIdFastHash<ELFT>::writeBuildId(MutableArrayRef<uint8_t> Buf) {
-  const endianness E = ELFT::TargetEndianness;
-
-  // 64-bit xxhash
-  uint64_t Hash = xxHash64(toStringRef(Buf));
-  write64<E>(this->getOutputLoc(Buf.begin()) + 16, Hash);
-}
-
-template <class ELFT>
-void BuildIdMd5<ELFT>::writeBuildId(MutableArrayRef<uint8_t> Buf) {
-  MD5 Hash;
-  Hash.update(Buf);
-  MD5::MD5Result Res;
-  Hash.final(Res);
-  memcpy(this->getOutputLoc(Buf.begin()) + 16, Res, 16);
-}
-
-template <class ELFT>
-void BuildIdSha1<ELFT>::writeBuildId(MutableArrayRef<uint8_t> Buf) {
-  SHA1 Hash;
-  Hash.update(Buf);
-  memcpy(this->getOutputLoc(Buf.begin()) + 16, Hash.final().data(), 20);
-}
-
-template <class ELFT>
-void BuildIdUuid<ELFT>::writeBuildId(MutableArrayRef<uint8_t> Buf) {
-  if (getRandomBytes(this->getOutputLoc(Buf.begin()) + 16, 16))
-    error("entropy source failure");
-}
-
-template <class ELFT>
-BuildIdHexstring<ELFT>::BuildIdHexstring()
-    : BuildIdSection<ELFT>(Config->BuildIdVector.size()) {}
-
-template <class ELFT>
-void BuildIdHexstring<ELFT>::writeBuildId(MutableArrayRef<uint8_t> Buf) {
-  memcpy(this->getOutputLoc(Buf.begin()) + 16, Config->BuildIdVector.data(),
-         Config->BuildIdVector.size());
-}
-
 template class elf::InputSectionBase<ELF32LE>;
 template class elf::InputSectionBase<ELF32BE>;
 template class elf::InputSectionBase<ELF64LE>;
@@ -945,33 +884,3 @@ template class elf::MipsAbiFlagsInputSec
 template class elf::MipsAbiFlagsInputSection<ELF32BE>;
 template class elf::MipsAbiFlagsInputSection<ELF64LE>;
 template class elf::MipsAbiFlagsInputSection<ELF64BE>;
-
-template class elf::BuildIdSection<ELF32LE>;
-template class elf::BuildIdSection<ELF32BE>;
-template class elf::BuildIdSection<ELF64LE>;
-template class elf::BuildIdSection<ELF64BE>;
-
-template class elf::BuildIdFastHash<ELF32LE>;
-template class elf::BuildIdFastHash<ELF32BE>;
-template class elf::BuildIdFastHash<ELF64LE>;
-template class elf::BuildIdFastHash<ELF64BE>;
-
-template class elf::BuildIdMd5<ELF32LE>;
-template class elf::BuildIdMd5<ELF32BE>;
-template class elf::BuildIdMd5<ELF64LE>;
-template class elf::BuildIdMd5<ELF64BE>;
-
-template class elf::BuildIdSha1<ELF32LE>;
-template class elf::BuildIdSha1<ELF32BE>;
-template class elf::BuildIdSha1<ELF64LE>;
-template class elf::BuildIdSha1<ELF64BE>;
-
-template class elf::BuildIdUuid<ELF32LE>;
-template class elf::BuildIdUuid<ELF32BE>;
-template class elf::BuildIdUuid<ELF64LE>;
-template class elf::BuildIdUuid<ELF64BE>;
-
-template class elf::BuildIdHexstring<ELF32LE>;
-template class elf::BuildIdHexstring<ELF32BE>;
-template class elf::BuildIdHexstring<ELF64LE>;
-template class elf::BuildIdHexstring<ELF64BE>;

Modified: lld/trunk/ELF/InputSection.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputSection.h?rev=285740&r1=285739&r2=285740&view=diff
==============================================================================
--- lld/trunk/ELF/InputSection.h (original)
+++ lld/trunk/ELF/InputSection.h Tue Nov  1 15:28:21 2016
@@ -339,58 +339,6 @@ public:
   const llvm::object::Elf_Mips_ABIFlags<ELFT> *Flags = nullptr;
 };
 
-template <class ELFT> class BuildIdSection : public InputSection<ELFT> {
-public:
-  virtual void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) = 0;
-  virtual ~BuildIdSection() = default;
-
-  uint8_t *getOutputLoc(uint8_t *Start) const;
-
-protected:
-  BuildIdSection(size_t HashSize);
-  std::vector<uint8_t> Buf;
-};
-
-template <class ELFT>
-class BuildIdFastHash final : public BuildIdSection<ELFT> {
-public:
-  BuildIdFastHash() : BuildIdSection<ELFT>(8) {}
-  void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) override;
-};
-
-template <class ELFT> class BuildIdMd5 final : public BuildIdSection<ELFT> {
-public:
-  BuildIdMd5() : BuildIdSection<ELFT>(16) {}
-  void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) override;
-};
-
-template <class ELFT> class BuildIdSha1 final : public BuildIdSection<ELFT> {
-public:
-  BuildIdSha1() : BuildIdSection<ELFT>(20) {}
-  void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) override;
-};
-
-template <class ELFT> class BuildIdUuid final : public BuildIdSection<ELFT> {
-public:
-  BuildIdUuid() : BuildIdSection<ELFT>(16) {}
-  void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) override;
-};
-
-template <class ELFT>
-class BuildIdHexstring final : public BuildIdSection<ELFT> {
-public:
-  BuildIdHexstring();
-  void writeBuildId(llvm::MutableArrayRef<uint8_t>) override;
-};
-
-// Linker generated sections which can be used as inputs.
-template <class ELFT> struct In {
-  static BuildIdSection<ELFT> *BuildId;
-  static std::vector<InputSection<ELFT> *> Sections;
-};
-
-template <class ELFT> BuildIdSection<ELFT> *In<ELFT>::BuildId;
-template <class ELFT> std::vector<InputSection<ELFT> *> In<ELFT>::Sections;
 } // namespace elf
 } // namespace lld
 

Modified: lld/trunk/ELF/OutputSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.h?rev=285740&r1=285739&r2=285740&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.h (original)
+++ lld/trunk/ELF/OutputSections.h Tue Nov  1 15:28:21 2016
@@ -18,8 +18,6 @@
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/MC/StringTableBuilder.h"
 #include "llvm/Object/ELF.h"
-#include "llvm/Support/MD5.h"
-#include "llvm/Support/SHA1.h"
 
 namespace lld {
 namespace elf {

Added: lld/trunk/ELF/SyntheticSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.cpp?rev=285740&view=auto
==============================================================================
--- lld/trunk/ELF/SyntheticSections.cpp (added)
+++ lld/trunk/ELF/SyntheticSections.cpp Tue Nov  1 15:28:21 2016
@@ -0,0 +1,126 @@
+//===- SyntheticSections.cpp ----------------------------------------------===//
+//
+//                             The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file contains linker-synthesized sections. Currently,
+// synthetic sections are created either output sections or input sections,
+// but we are rewriting code so that all synthetic sections are created as
+// input sections.
+//
+//===----------------------------------------------------------------------===//
+
+#include "SyntheticSections.h"
+#include "Config.h"
+#include "Error.h"
+#include "InputFiles.h"
+#include "OutputSections.h"
+#include "Strings.h"
+
+#include "llvm/Support/Endian.h"
+#include "llvm/Support/MD5.h"
+#include "llvm/Support/RandomNumberGenerator.h"
+#include "llvm/Support/SHA1.h"
+#include "llvm/Support/xxhash.h"
+
+using namespace llvm;
+using namespace llvm::ELF;
+using namespace llvm::object;
+using namespace llvm::support;
+using namespace llvm::support::endian;
+
+using namespace lld;
+using namespace lld::elf;
+
+template <class ELFT>
+BuildIdSection<ELFT>::BuildIdSection(size_t HashSize)
+    : InputSection<ELFT>(SHF_ALLOC, SHT_NOTE, 1, ArrayRef<uint8_t>(),
+                         ".note.gnu.build-id") {
+  Buf.resize(16 + HashSize);
+  const endianness E = ELFT::TargetEndianness;
+  write32<E>(Buf.data(), 4);                   // Name size
+  write32<E>(Buf.data() + 4, HashSize);        // Content size
+  write32<E>(Buf.data() + 8, NT_GNU_BUILD_ID); // Type
+  memcpy(Buf.data() + 12, "GNU", 4);           // Name string
+  this->Data = ArrayRef<uint8_t>(Buf);
+}
+
+template <class ELFT>
+uint8_t *BuildIdSection<ELFT>::getOutputLoc(uint8_t *Start) const {
+  return Start + this->OutSec->getFileOffset() + this->OutSecOff;
+}
+
+template <class ELFT>
+void BuildIdFastHash<ELFT>::writeBuildId(MutableArrayRef<uint8_t> Buf) {
+  const endianness E = ELFT::TargetEndianness;
+
+  // 64-bit xxhash
+  uint64_t Hash = xxHash64(toStringRef(Buf));
+  write64<E>(this->getOutputLoc(Buf.begin()) + 16, Hash);
+}
+
+template <class ELFT>
+void BuildIdMd5<ELFT>::writeBuildId(MutableArrayRef<uint8_t> Buf) {
+  MD5 Hash;
+  Hash.update(Buf);
+  MD5::MD5Result Res;
+  Hash.final(Res);
+  memcpy(this->getOutputLoc(Buf.begin()) + 16, Res, 16);
+}
+
+template <class ELFT>
+void BuildIdSha1<ELFT>::writeBuildId(MutableArrayRef<uint8_t> Buf) {
+  SHA1 Hash;
+  Hash.update(Buf);
+  memcpy(this->getOutputLoc(Buf.begin()) + 16, Hash.final().data(), 20);
+}
+
+template <class ELFT>
+void BuildIdUuid<ELFT>::writeBuildId(MutableArrayRef<uint8_t> Buf) {
+  if (getRandomBytes(this->getOutputLoc(Buf.begin()) + 16, 16))
+    error("entropy source failure");
+}
+
+template <class ELFT>
+BuildIdHexstring<ELFT>::BuildIdHexstring()
+    : BuildIdSection<ELFT>(Config->BuildIdVector.size()) {}
+
+template <class ELFT>
+void BuildIdHexstring<ELFT>::writeBuildId(MutableArrayRef<uint8_t> Buf) {
+  memcpy(this->getOutputLoc(Buf.begin()) + 16, Config->BuildIdVector.data(),
+         Config->BuildIdVector.size());
+}
+
+template class elf::BuildIdSection<ELF32LE>;
+template class elf::BuildIdSection<ELF32BE>;
+template class elf::BuildIdSection<ELF64LE>;
+template class elf::BuildIdSection<ELF64BE>;
+
+template class elf::BuildIdFastHash<ELF32LE>;
+template class elf::BuildIdFastHash<ELF32BE>;
+template class elf::BuildIdFastHash<ELF64LE>;
+template class elf::BuildIdFastHash<ELF64BE>;
+
+template class elf::BuildIdMd5<ELF32LE>;
+template class elf::BuildIdMd5<ELF32BE>;
+template class elf::BuildIdMd5<ELF64LE>;
+template class elf::BuildIdMd5<ELF64BE>;
+
+template class elf::BuildIdSha1<ELF32LE>;
+template class elf::BuildIdSha1<ELF32BE>;
+template class elf::BuildIdSha1<ELF64LE>;
+template class elf::BuildIdSha1<ELF64BE>;
+
+template class elf::BuildIdUuid<ELF32LE>;
+template class elf::BuildIdUuid<ELF32BE>;
+template class elf::BuildIdUuid<ELF64LE>;
+template class elf::BuildIdUuid<ELF64BE>;
+
+template class elf::BuildIdHexstring<ELF32LE>;
+template class elf::BuildIdHexstring<ELF32BE>;
+template class elf::BuildIdHexstring<ELF64LE>;
+template class elf::BuildIdHexstring<ELF64BE>;

Added: lld/trunk/ELF/SyntheticSections.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.h?rev=285740&view=auto
==============================================================================
--- lld/trunk/ELF/SyntheticSections.h (added)
+++ lld/trunk/ELF/SyntheticSections.h Tue Nov  1 15:28:21 2016
@@ -0,0 +1,74 @@
+//===- SyntheticSection.h ---------------------------------------*- C++ -*-===//
+//
+//                             The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLD_ELF_SYNTHETIC_SECTION_H
+#define LLD_ELF_SYNTHETIC_SECTION_H
+
+#include "InputSection.h"
+
+namespace lld {
+namespace elf {
+
+template <class ELFT> class BuildIdSection : public InputSection<ELFT> {
+public:
+  virtual void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) = 0;
+  virtual ~BuildIdSection() = default;
+
+  uint8_t *getOutputLoc(uint8_t *Start) const;
+
+protected:
+  BuildIdSection(size_t HashSize);
+  std::vector<uint8_t> Buf;
+};
+
+template <class ELFT>
+class BuildIdFastHash final : public BuildIdSection<ELFT> {
+public:
+  BuildIdFastHash() : BuildIdSection<ELFT>(8) {}
+  void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) override;
+};
+
+template <class ELFT> class BuildIdMd5 final : public BuildIdSection<ELFT> {
+public:
+  BuildIdMd5() : BuildIdSection<ELFT>(16) {}
+  void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) override;
+};
+
+template <class ELFT> class BuildIdSha1 final : public BuildIdSection<ELFT> {
+public:
+  BuildIdSha1() : BuildIdSection<ELFT>(20) {}
+  void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) override;
+};
+
+template <class ELFT> class BuildIdUuid final : public BuildIdSection<ELFT> {
+public:
+  BuildIdUuid() : BuildIdSection<ELFT>(16) {}
+  void writeBuildId(llvm::MutableArrayRef<uint8_t> Buf) override;
+};
+
+template <class ELFT>
+class BuildIdHexstring final : public BuildIdSection<ELFT> {
+public:
+  BuildIdHexstring();
+  void writeBuildId(llvm::MutableArrayRef<uint8_t>) override;
+};
+
+// Linker generated sections which can be used as inputs.
+template <class ELFT> struct In {
+  static BuildIdSection<ELFT> *BuildId;
+  static std::vector<InputSection<ELFT> *> Sections;
+};
+
+template <class ELFT> BuildIdSection<ELFT> *In<ELFT>::BuildId;
+template <class ELFT> std::vector<InputSection<ELFT> *> In<ELFT>::Sections;
+
+} // namespace elf
+} // namespace lld
+
+#endif

Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=285740&r1=285739&r2=285740&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Tue Nov  1 15:28:21 2016
@@ -15,6 +15,7 @@
 #include "Relocations.h"
 #include "Strings.h"
 #include "SymbolTable.h"
+#include "SyntheticSections.h"
 #include "Target.h"
 
 #include "llvm/ADT/StringMap.h"




More information about the llvm-commits mailing list