[PATCH] LLD: ELF: Simplify CreateELF.

Rui Ueyama ruiu at google.com
Thu Apr 2 16:25:33 PDT 2015


Hi Bigcheese,

CreateELF was a combination of templates and C preprocessor macros.
This patch removes uses of macros.

Previously, we checked alignments based on the host environment.
It doesn't seem to make sense to me because we can use the linker
for cross compilation. So I relaxed the check in this patch.

REPOSITORY
  rL LLVM

http://reviews.llvm.org/D8810

Files:
  lib/ReaderWriter/ELF/CreateELF.h

Index: lib/ReaderWriter/ELF/CreateELF.h
===================================================================
--- lib/ReaderWriter/ELF/CreateELF.h
+++ lib/ReaderWriter/ELF/CreateELF.h
@@ -35,48 +35,37 @@
 /// \param maxAlignment the maximum alignment of the file.
 /// \param args arguments forwarded to CreateELFTraits<T>::create.
 
-#define LLVM_CREATE_ELF_CreateELFTraits(endian, align, is64, ...) \
-  Traits::template create<ELFType<llvm::support::endian, align, is64>>( \
-      __VA_ARGS__);
-
-#if !LLVM_IS_UNALIGNED_ACCESS_FAST
-# define LLVM_CREATE_ELF_Create(normal, low, endian, is64, ...) \
-  if (maxAlignment >= normal) \
-    return LLVM_CREATE_ELF_CreateELFTraits(endian, normal, is64, __VA_ARGS__) \
-  else if (maxAlignment >= low) \
-    return LLVM_CREATE_ELF_CreateELFTraits(endian, low, is64, __VA_ARGS__) \
-  else \
-    llvm_unreachable("Invalid alignment for ELF file!");
-#else
-# define LLVM_CREATE_ELF_Create(normal, low, endian, is64, ...) \
-  if (maxAlignment >= low) \
-    return LLVM_CREATE_ELF_CreateELFTraits(endian, low, is64, __VA_ARGS__) \
-  else \
-    llvm_unreachable("Invalid alignment for ELF file!");
-#endif
-
 template <class Traits, class... Args>
 llvm::ErrorOr<std::unique_ptr<lld::File>>
 createELF(std::pair<unsigned char, unsigned char> ident,
           std::size_t maxAlignment, Args &&... args) {
-  if (ident.first == llvm::ELF::ELFCLASS32 &&
-      ident.second == llvm::ELF::ELFDATA2LSB) {
-    LLVM_CREATE_ELF_Create(4, 2, little, false, std::forward<Args>(args)...)
-  } else if (ident.first == llvm::ELF::ELFCLASS32 &&
-             ident.second == llvm::ELF::ELFDATA2MSB) {
-    LLVM_CREATE_ELF_Create(4, 2, big, false, std::forward<Args>(args)...)
-  } else if (ident.first == llvm::ELF::ELFCLASS64 &&
-             ident.second == llvm::ELF::ELFDATA2MSB) {
-    LLVM_CREATE_ELF_Create(8, 2, big, true, std::forward<Args>(args)...)
-  } else if (ident.first == llvm::ELF::ELFCLASS64 &&
-             ident.second == llvm::ELF::ELFDATA2LSB) {
-    LLVM_CREATE_ELF_Create(8, 2, little, true, std::forward<Args>(args)...)
+  using namespace llvm::ELF;
+  using namespace llvm::support;
+  unsigned char size = ident.first;
+  unsigned char endian = ident.second;
+
+  if (maxAlignment < 2)
+    llvm_unreachable("Invalid alignment for ELF file!");
+
+  if (size == ELFCLASS32 && endian == ELFDATA2LSB) {
+    return Traits::template create<ELFType<little, 2, false>>(
+        std::forward<Args>(args)...);
+  }
+  if (size == ELFCLASS32 && endian == ELFDATA2MSB) {
+    return Traits::template create<ELFType<big, 2, false>>(
+        std::forward<Args>(args)...);
+  }
+  if (size == ELFCLASS64 && endian == ELFDATA2LSB) {
+    return Traits::template create<ELFType<little, 2, true>>(
+        std::forward<Args>(args)...);
+  }
+  if (size == ELFCLASS64 && endian == ELFDATA2MSB) {
+    return Traits::template create<ELFType<big, 2, true>>(
+        std::forward<Args>(args)...);
   }
   llvm_unreachable("Invalid ELF type!");
 }
-} // end anon namespace
 
-#undef LLVM_CREATE_ELF_CreateELFTraits
-#undef LLVM_CREATE_ELF_Create
+} // end anon namespace
 
 #endif

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8810.23185.patch
Type: text/x-patch
Size: 3130 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150402/1059df9a/attachment.bin>


More information about the llvm-commits mailing list