[PATCH] D30935: [ELF] - Detemplate LinkerScript<ELFT>::writeDataBytes

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 14 06:31:50 PDT 2017


grimar created this revision.

Some places in linker uses ELFT::TargetEndianness.
Patch adds getEndian() method to Config. It uses it to
detemplate writeDataBytes method.

getEndian() can be used for detemplation of many other places,
for example BuildIdSection synthetic section should be detemplated trivially
after that.


https://reviews.llvm.org/D30935

Files:
  ELF/Config.h
  ELF/LinkerScript.cpp
  ELF/LinkerScript.h


Index: ELF/LinkerScript.h
===================================================================
--- ELF/LinkerScript.h
+++ ELF/LinkerScript.h
@@ -267,6 +267,7 @@
   uint64_t getDot() { return Dot; }
   OutputSection *getOutputSection(const Twine &Loc, StringRef S);
   uint64_t getOutputSectionSize(StringRef S);
+  void writeDataBytes(StringRef Name, uint8_t *Buf);
 
   virtual uint64_t getSymbolValue(const Twine &Loc, StringRef S) = 0;
   virtual bool isDefined(StringRef S) = 0;
@@ -297,7 +298,6 @@
   LinkerScript();
   ~LinkerScript();
 
-  void writeDataBytes(StringRef Name, uint8_t *Buf);
   void addSymbol(SymbolAssignment *Cmd);
   void discard(ArrayRef<InputSectionBase *> V);
   void processCommands(OutputSectionFactory &Factory);
Index: ELF/LinkerScript.cpp
===================================================================
--- ELF/LinkerScript.cpp
+++ ELF/LinkerScript.cpp
@@ -863,38 +863,36 @@
   return 0;
 }
 
-template <class ELFT>
 static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
-  const endianness E = ELFT::TargetEndianness;
+  const endianness E = Config->getEndian();
 
   switch (Size) {
   case 1:
     *Buf = (uint8_t)Data;
     break;
   case 2:
-    write16<E>(Buf, Data);
+    write16(Buf, Data, E);
     break;
   case 4:
-    write32<E>(Buf, Data);
+    write32(Buf, Data, E);
     break;
   case 8:
-    write64<E>(Buf, Data);
+    write64(Buf, Data, E);
     break;
   default:
     llvm_unreachable("unsupported Size argument");
   }
 }
 
-template <class ELFT>
-void LinkerScript<ELFT>::writeDataBytes(StringRef Name, uint8_t *Buf) {
+void LinkerScriptBase::writeDataBytes(StringRef Name, uint8_t *Buf) {
   int I = getSectionIndex(Name);
   if (I == INT_MAX)
     return;
 
   auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get());
   for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands)
     if (auto *Data = dyn_cast<BytesDataCommand>(Base.get()))
-      writeInt<ELFT>(Buf + Data->Offset, Data->Expression(), Data->Size);
+      writeInt(Buf + Data->Offset, Data->Expression(), Data->Size);
 }
 
 bool LinkerScriptBase::hasLMA(StringRef Name) {
Index: ELF/Config.h
===================================================================
--- ELF/Config.h
+++ ELF/Config.h
@@ -15,6 +15,7 @@
 #include "llvm/ADT/StringSet.h"
 #include "llvm/Support/CodeGen.h"
 #include "llvm/Support/ELF.h"
+#include "llvm/Support/Endian.h"
 
 #include <vector>
 
@@ -204,6 +205,12 @@
   bool isMips64EL() const {
     return EMachine == llvm::ELF::EM_MIPS && EKind == ELF64LEKind;
   }
+
+  llvm::support::endianness getEndian() const {
+    return (EKind == ELF32LEKind || EKind == ELF64LEKind)
+               ? llvm::support::endianness::little
+               : llvm::support::endianness::big;
+  }
 };
 
 // The only instance of Configuration struct.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30935.91705.patch
Type: text/x-patch
Size: 2818 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170314/23240178/attachment.bin>


More information about the llvm-commits mailing list