[lld] r230725 - Add {read,write}{16,32,64}{le,be} functions.

Rui Ueyama ruiu at google.com
Thu Feb 26 19:18:46 PST 2015


Author: ruiu
Date: Thu Feb 26 21:18:46 2015
New Revision: 230725

URL: http://llvm.org/viewvc/llvm-project?rev=230725&view=rev
Log:
Add {read,write}{16,32,64}{le,be} functions.

Nothing wrong with reinterpret_cast<llvm::support::ulittle32_t *>(loc),
but that's redundant and not great from readability point of view.
The new functions are wrappers for that kind of reinterpet_casts.

Surprisingly or unsurprisingly, there was no use of big endian read
and write. {read,write}{16,32,64}be have no user. But I think they
still worth to be there in the header for completeness.

http://reviews.llvm.org/D7927

Added:
    lld/trunk/include/lld/Core/Endian.h
Modified:
    lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64RelocationHandler.cpp
    lld/trunk/lib/ReaderWriter/ELF/ARM/ARMRelocationHandler.cpp
    lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonRelocationHandler.cpp
    lld/trunk/lib/ReaderWriter/ELF/X86/X86RelocationHandler.cpp
    lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64RelocationHandler.cpp
    lld/trunk/lib/ReaderWriter/PECOFF/IdataPass.cpp
    lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp
    lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp

Added: lld/trunk/include/lld/Core/Endian.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/Endian.h?rev=230725&view=auto
==============================================================================
--- lld/trunk/include/lld/Core/Endian.h (added)
+++ lld/trunk/include/lld/Core/Endian.h Thu Feb 26 21:18:46 2015
@@ -0,0 +1,67 @@
+//===--- Endian.h - utility functions for endian-aware reads/writes -----*-===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLD_CORE_ENDIAN_H
+#define LLD_CORE_ENDIAN_H
+
+#include "llvm/Support/Endian.h"
+
+namespace lld {
+
+inline uint16_t read16le(const void *p) {
+  return *reinterpret_cast<const llvm::support::ulittle16_t *>(p);
+}
+
+inline uint32_t read32le(const void *p) {
+  return *reinterpret_cast<const llvm::support::ulittle32_t *>(p);
+}
+
+inline uint64_t read64le(const void *p) {
+  return *reinterpret_cast<const llvm::support::ulittle64_t *>(p);
+}
+
+inline void write16le(void *p, uint16_t v) {
+  *reinterpret_cast<llvm::support::ulittle16_t *>(p) = v;
+}
+
+inline void write32le(void *p, uint32_t v) {
+  *reinterpret_cast<llvm::support::ulittle32_t *>(p) = v;
+}
+
+inline void write64le(void *p, uint64_t v) {
+  *reinterpret_cast<llvm::support::ulittle64_t *>(p) = v;
+}
+
+inline uint16_t read16be(const void *p) {
+  return *reinterpret_cast<const llvm::support::ubig16_t *>(p);
+}
+
+inline uint32_t read32be(const void *p) {
+  return *reinterpret_cast<const llvm::support::ubig32_t *>(p);
+}
+
+inline uint64_t read64be(const void *p) {
+  return *reinterpret_cast<const llvm::support::ubig64_t *>(p);
+}
+
+inline void write16be(void *p, uint16_t v) {
+  *reinterpret_cast<llvm::support::ubig16_t *>(p) = v;
+}
+
+inline void write32be(void *p, uint32_t v) {
+  *reinterpret_cast<llvm::support::ubig32_t *>(p) = v;
+}
+
+inline void write64be(void *p, uint64_t v) {
+  *reinterpret_cast<llvm::support::ubig64_t *>(p) = v;
+}
+
+} // namespace lld
+
+#endif

Modified: lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64RelocationHandler.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64RelocationHandler.cpp?rev=230725&r1=230724&r2=230725&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64RelocationHandler.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/AArch64/AArch64RelocationHandler.cpp Thu Feb 26 21:18:46 2015
@@ -9,6 +9,7 @@
 
 #include "AArch64TargetHandler.h"
 #include "AArch64LinkingContext.h"
+#include "lld/Core/Endian.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/MathExtras.h"
 
@@ -32,18 +33,14 @@ static void relocR_AARCH64_ABS64(uint8_t
       llvm::dbgs() << " A: 0x" << Twine::utohexstr(A);
       llvm::dbgs() << " P: 0x" << Twine::utohexstr(P);
       llvm::dbgs() << " result: 0x" << Twine::utohexstr(result) << "\n");
-  *reinterpret_cast<llvm::support::ulittle64_t *>(location) =
-      result |
-      (int64_t) * reinterpret_cast<llvm::support::little64_t *>(location);
+  write64le(location, result | read64le(location));
 }
 
 /// \brief R_AARCH64_PREL32 - word32: S + A - P
 static void relocR_AARCH64_PREL32(uint8_t *location, uint64_t P, uint64_t S,
                                   int64_t A) {
   int32_t result = (int32_t)((S + A) - P);
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      result +
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, result + (int32_t)read32le(location));
 }
 
 /// \brief R_AARCH64_ABS32 - word32:  S + A
@@ -58,9 +55,7 @@ static std::error_code relocR_AARCH64_AB
       llvm::dbgs() << " A: 0x" << Twine::utohexstr(A);
       llvm::dbgs() << " P: 0x" << Twine::utohexstr(P);
       llvm::dbgs() << " result: 0x" << Twine::utohexstr(result) << "\n");
-  *reinterpret_cast<llvm::support::ulittle32_t *>(location) =
-      result |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, result | read32le(location));
   return std::error_code();
 }
 
@@ -81,9 +76,7 @@ static void relocR_AARCH64_ADR_PREL_PG_H
       llvm::dbgs() << " immhi: " << Twine::utohexstr(immhi);
       llvm::dbgs() << " immlo: " << Twine::utohexstr(immlo);
       llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      immlo | immhi |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, immlo | immhi | read32le(location));
   // TODO: Make sure this is correct!
 }
 
@@ -103,9 +96,7 @@ static void relocR_AARCH64_ADR_PREL_LO21
       llvm::dbgs() << " immhi: " << Twine::utohexstr(immhi);
       llvm::dbgs() << " immlo: " << Twine::utohexstr(immlo);
       llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      immlo | immhi |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, immlo | immhi | read32le(location));
   // TODO: Make sure this is correct!
 }
 
@@ -120,9 +111,7 @@ static void relocR_AARCH64_ADD_ABS_LO12_
       llvm::dbgs() << " A: " << Twine::utohexstr(A);
       llvm::dbgs() << " P: " << Twine::utohexstr(P);
       llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      result |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, result | read32le(location));
 }
 
 static void relocJump26(uint8_t *location, uint64_t P, uint64_t S, int64_t A) {
@@ -135,9 +124,7 @@ static void relocJump26(uint8_t *locatio
       llvm::dbgs() << " A: " << Twine::utohexstr(A);
       llvm::dbgs() << " P: " << Twine::utohexstr(P);
       llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      result |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, result | read32le(location));
 }
 
 /// \brief R_AARCH64_CONDBR19
@@ -152,9 +139,7 @@ static void relocR_AARCH64_CONDBR19(uint
       llvm::dbgs() << " A: " << Twine::utohexstr(A);
       llvm::dbgs() << " P: " << Twine::utohexstr(P);
       llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      result |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, result | read32le(location));
 }
 
 /// \brief R_AARCH64_LDST8_ABS_LO12_NC - S + A
@@ -168,9 +153,7 @@ static void relocR_AARCH64_LDST8_ABS_LO1
       llvm::dbgs() << " A: " << Twine::utohexstr(A);
       llvm::dbgs() << " P: " << Twine::utohexstr(P);
       llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      result |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, result | read32le(location));
 }
 
 /// \brief R_AARCH64_LDST16_ABS_LO12_NC
@@ -185,9 +168,7 @@ static void relocR_AARCH64_LDST16_ABS_LO
       llvm::dbgs() << " A: " << Twine::utohexstr(A);
       llvm::dbgs() << " P: " << Twine::utohexstr(P);
       llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      result |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, result | read32le(location));
 }
 
 /// \brief R_AARCH64_LDST32_ABS_LO12_NC
@@ -202,9 +183,7 @@ static void relocR_AARCH64_LDST32_ABS_LO
       llvm::dbgs() << " A: " << Twine::utohexstr(A);
       llvm::dbgs() << " P: " << Twine::utohexstr(P);
       llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      result |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, result | read32le(location));
 }
 
 /// \brief R_AARCH64_LDST64_ABS_LO12_NC
@@ -219,9 +198,7 @@ static void relocR_AARCH64_LDST64_ABS_LO
       llvm::dbgs() << " A: " << Twine::utohexstr(A);
       llvm::dbgs() << " P: " << Twine::utohexstr(P);
       llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      result |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, result | read32le(location));
 }
 
 /// \brief R_AARCH64_LDST128_ABS_LO12_NC
@@ -236,9 +213,7 @@ static void relocR_AARCH64_LDST128_ABS_L
       llvm::dbgs() << " A: " << Twine::utohexstr(A);
       llvm::dbgs() << " P: " << Twine::utohexstr(P);
       llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      result |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, result | read32le(location));
 }
 
 static void relocR_AARCH64_ADR_GOT_PAGE(uint8_t *location, uint64_t P,
@@ -257,9 +232,7 @@ static void relocR_AARCH64_ADR_GOT_PAGE(
       llvm::dbgs() << " immhi: " << Twine::utohexstr(immhi);
       llvm::dbgs() << " immlo: " << Twine::utohexstr(immlo);
       llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      immlo | immhi |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, result | read32le(location));
 }
 
 // R_AARCH64_LD64_GOT_LO12_NC
@@ -274,9 +247,7 @@ static void relocR_AARCH64_LD64_GOT_LO12
       llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
   result &= 0xFF8;
   result <<= 7;
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      result |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, result | read32le(location));
 }
 
 // ADD_AARCH64_GOTRELINDEX
@@ -291,9 +262,7 @@ static void relocADD_AARCH64_GOTRELINDEX
       llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
   result &= 0xFFF;
   result <<= 10;
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      result |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, result | read32le(location));
 }
 
 // R_AARCH64_TLSIE_ADR_GOTTPREL_PAGE21
@@ -314,9 +283,7 @@ static void relocR_AARCH64_TLSIE_ADR_GOT
       llvm::dbgs() << " immhi: " << Twine::utohexstr(immhi);
       llvm::dbgs() << " immlo: " << Twine::utohexstr(immlo);
       llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      immlo | immhi |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, immlo | immhi | read32le(location));
 }
 
 // R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC
@@ -332,9 +299,7 @@ static void relocR_AARCH64_TLSIE_LD64_GO
       llvm::dbgs() << " A: " << Twine::utohexstr(A);
       llvm::dbgs() << " P: " << Twine::utohexstr(P);
       llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      result |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, result | read32le(location));
 }
 
 /// \brief R_AARCH64_TLSLE_ADD_TPREL_HI12
@@ -349,9 +314,7 @@ static void relocR_AARCH64_TLSLE_ADD_TPR
       llvm::dbgs() << " A: " << Twine::utohexstr(A);
       llvm::dbgs() << " P: " << Twine::utohexstr(P);
       llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      result |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, result | read32le(location));
 }
 
 /// \brief R_AARCH64_TLSLE_ADD_TPREL_LO12_NC
@@ -367,9 +330,7 @@ static void relocR_AARCH64_TLSLE_ADD_TPR
       llvm::dbgs() << " A: " << Twine::utohexstr(A);
       llvm::dbgs() << " P: " << Twine::utohexstr(P);
       llvm::dbgs() << " result: " << Twine::utohexstr(result) << "\n");
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      result |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, result | read32le(location));
 }
 
 std::error_code AArch64TargetRelocationHandler::applyRelocation(

Modified: lld/trunk/lib/ReaderWriter/ELF/ARM/ARMRelocationHandler.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/ARM/ARMRelocationHandler.cpp?rev=230725&r1=230724&r2=230725&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/ARM/ARMRelocationHandler.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/ARM/ARMRelocationHandler.cpp Thu Feb 26 21:18:46 2015
@@ -10,6 +10,7 @@
 #include "ARMTargetHandler.h"
 #include "ARMLinkingContext.h"
 
+#include "lld/Core/Endian.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/MathExtras.h"
 
@@ -17,10 +18,8 @@ using namespace lld;
 using namespace elf;
 
 static Reference::Addend readAddend_THM_MOV(const uint8_t *location) {
-  const auto halfHi = uint16_t(
-      *reinterpret_cast<const llvm::support::ulittle16_t *>(location));
-  const auto halfLo = uint16_t(
-      *reinterpret_cast<const llvm::support::ulittle16_t *>(location + 2));
+  const uint16_t halfHi = read16le(location);
+  const uint16_t halfLo = read16le(location + 2);
 
   const uint16_t imm8 = halfLo & 0xFF;
   const uint16_t imm3 = (halfLo >> 12) & 0x7;
@@ -33,8 +32,7 @@ static Reference::Addend readAddend_THM_
 }
 
 static Reference::Addend readAddend_ARM_MOV(const uint8_t *location) {
-  const auto value = uint32_t(
-      *reinterpret_cast<const llvm::support::ulittle32_t *>(location));
+  const uint32_t value = read32le(location);
 
   const uint32_t imm12 = value & 0xFFF;
   const uint32_t imm4 = (value >> 16) & 0xF;
@@ -44,10 +42,8 @@ static Reference::Addend readAddend_ARM_
 }
 
 static Reference::Addend readAddend_THM_CALL(const uint8_t *location) {
-  const auto halfHi = uint16_t(
-      *reinterpret_cast<const llvm::support::ulittle16_t *>(location));
-  const auto halfLo = uint16_t(
-      *reinterpret_cast<const llvm::support::ulittle16_t *>(location + 2));
+  const uint16_t halfHi = read16le(location);
+  const uint16_t halfLo = read16le(location + 2);
 
   const uint16_t imm10 = halfHi & 0x3FF;
   const uint16_t bitS = (halfHi >> 10) & 0x1;
@@ -64,8 +60,7 @@ static Reference::Addend readAddend_THM_
 }
 
 static Reference::Addend readAddend_ARM_CALL(const uint8_t *location) {
-  const auto value = uint32_t(
-      *reinterpret_cast<const llvm::support::ulittle32_t *>(location));
+  const uint32_t value = read32le(location);
 
   const bool isBLX = (value & 0xF0000000) == 0xF0000000;
   const uint32_t bitH = isBLX ? ((value & 0x1000000) >> 24) : 0;
@@ -78,8 +73,7 @@ static Reference::Addend readAddend(cons
                                     Reference::KindValue kindValue) {
   switch (kindValue) {
   case R_ARM_ABS32:
-    return int32_t(
-        *reinterpret_cast<const llvm::support::little32_t *>(location));
+    return (int32_t)read32le(location);
   case R_ARM_THM_CALL:
   case R_ARM_THM_JUMP24:
     return readAddend_THM_CALL(location);
@@ -100,22 +94,16 @@ static Reference::Addend readAddend(cons
 static inline void applyArmReloc(uint8_t *location, uint32_t result,
                                  uint32_t mask = 0xFFFFFFFF) {
   assert(!(result & ~mask));
-  *reinterpret_cast<llvm::support::ulittle32_t *>(location) =
-      (uint32_t(*reinterpret_cast<llvm::support::ulittle32_t *>(location)) &
-       ~mask) | (result & mask);
+  write32le(location, (read32le(location) & ~mask) | (result & mask));
 }
 
 static inline void applyThmReloc(uint8_t *location, uint16_t resHi,
                                  uint16_t resLo, uint16_t maskHi,
                                  uint16_t maskLo = 0xFFFF) {
   assert(!(resHi & ~maskHi) && !(resLo & ~maskLo));
-  *reinterpret_cast<llvm::support::ulittle16_t *>(location) =
-      (uint16_t(*reinterpret_cast<llvm::support::ulittle16_t *>(location)) &
-       ~maskHi) | (resHi & maskHi);
+  write16le(location, (read16le(location) & ~maskHi) | (resHi & maskHi));
   location += 2;
-  *reinterpret_cast<llvm::support::ulittle16_t *>(location) =
-      (uint16_t(*reinterpret_cast<llvm::support::ulittle16_t *>(location)) &
-       ~maskLo) | (resLo & maskLo);
+  write16le(location, (read16le(location) & ~maskLo) | (resLo & maskLo));
 }
 
 /// \brief R_ARM_ABS32 - (S + A) | T

Modified: lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonRelocationHandler.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonRelocationHandler.cpp?rev=230725&r1=230724&r2=230725&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonRelocationHandler.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/Hexagon/HexagonRelocationHandler.cpp Thu Feb 26 21:18:46 2015
@@ -7,10 +7,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "HexagonTargetHandler.h"
 #include "HexagonLinkingContext.h"
 #include "HexagonRelocationFunctions.h"
+#include "HexagonTargetHandler.h"
 #include "HexagonRelocationHandler.h"
+#include "lld/Core/Endian.h"
 
 using namespace lld;
 using namespace elf;
@@ -18,9 +19,7 @@ using namespace elf;
 using namespace llvm::ELF;
 
 #define APPLY_RELOC(result)                                                    \
-  *reinterpret_cast<llvm::support::ulittle32_t *>(location) =                  \
-      result |                                                                 \
-      (uint32_t) * reinterpret_cast<llvm::support::ulittle32_t *>(location);
+  write32le(location, result | read32le(location));
 
 static int relocBNPCREL(uint8_t *location, uint64_t P, uint64_t S, uint64_t A,
                         int32_t nBits) {
@@ -349,5 +348,3 @@ std::error_code HexagonTargetRelocationH
 
   return std::error_code();
 }
-
-

Modified: lld/trunk/lib/ReaderWriter/ELF/X86/X86RelocationHandler.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/X86/X86RelocationHandler.cpp?rev=230725&r1=230724&r2=230725&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/X86/X86RelocationHandler.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/X86/X86RelocationHandler.cpp Thu Feb 26 21:18:46 2015
@@ -9,6 +9,7 @@
 
 #include "X86LinkingContext.h"
 #include "X86TargetHandler.h"
+#include "lld/Core/Endian.h"
 
 using namespace lld;
 using namespace elf;
@@ -17,18 +18,14 @@ namespace {
 /// \brief R_386_32 - word32:  S + A
 static int reloc32(uint8_t *location, uint64_t P, uint64_t S, uint64_t A) {
   int32_t result = (uint32_t)(S + A);
-  *reinterpret_cast<llvm::support::ulittle32_t *>(location) =
-      result |
-      (uint32_t) * reinterpret_cast<llvm::support::ulittle32_t *>(location);
+  write32le(location, result | read32le(location));
   return 0;
 }
 
 /// \brief R_386_PC32 - word32: S + A - P
 static int relocPC32(uint8_t *location, uint64_t P, uint64_t S, uint64_t A) {
   uint32_t result = (uint32_t)((S + A) - P);
-  *reinterpret_cast<llvm::support::ulittle32_t *>(location) =
-      result +
-      (uint32_t) * reinterpret_cast<llvm::support::ulittle32_t *>(location);
+  write32le(location, result + read32le(location));
   return 0;
 }
 }

Modified: lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64RelocationHandler.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64RelocationHandler.cpp?rev=230725&r1=230724&r2=230725&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64RelocationHandler.cpp (original)
+++ lld/trunk/lib/ReaderWriter/ELF/X86_64/X86_64RelocationHandler.cpp Thu Feb 26 21:18:46 2015
@@ -9,6 +9,7 @@
 
 #include "X86_64LinkingContext.h"
 #include "X86_64TargetHandler.h"
+#include "lld/Core/Endian.h"
 
 using namespace lld;
 using namespace elf;
@@ -16,52 +17,40 @@ using namespace elf;
 /// \brief R_X86_64_64 - word64: S + A
 static void reloc64(uint8_t *location, uint64_t P, uint64_t S, int64_t A) {
   uint64_t result = S + A;
-  *reinterpret_cast<llvm::support::ulittle64_t *>(location) =
-      result |
-      (uint64_t) * reinterpret_cast<llvm::support::ulittle64_t *>(location);
+  write64le(location, result | read64le(location));
 }
 
 /// \brief R_X86_64_PC32 - word32: S + A - P
 static void relocPC32(uint8_t *location, uint64_t P, uint64_t S, int64_t A) {
   uint32_t result = (uint32_t)((S + A) - P);
-  *reinterpret_cast<llvm::support::ulittle32_t *>(location) =
-      result +
-      (uint32_t) * reinterpret_cast<llvm::support::ulittle32_t *>(location);
+  write32le(location, result + read32le(location));
 }
 
 /// \brief R_X86_64_32 - word32:  S + A
 static void reloc32(uint8_t *location, uint64_t P, uint64_t S, int64_t A) {
   int32_t result = (uint32_t)(S + A);
-  *reinterpret_cast<llvm::support::ulittle32_t *>(location) =
-      result |
-      (uint32_t) * reinterpret_cast<llvm::support::ulittle32_t *>(location);
+  write32le(location, result | read32le(location));
   // TODO: Make sure that the result zero extends to the 64bit value.
 }
 
 /// \brief R_X86_64_32S - word32:  S + A
 static void reloc32S(uint8_t *location, uint64_t P, uint64_t S, int64_t A) {
   int32_t result = (int32_t)(S + A);
-  *reinterpret_cast<llvm::support::little32_t *>(location) =
-      result |
-      (int32_t) * reinterpret_cast<llvm::support::little32_t *>(location);
+  write32le(location, result | read32le(location));
   // TODO: Make sure that the result sign extends to the 64bit value.
 }
 
 /// \brief R_X86_64_16 - word16:  S + A
 static void reloc16(uint8_t *location, uint64_t P, uint64_t S, int64_t A) {
   uint16_t result = (uint16_t)(S + A);
-  *reinterpret_cast<llvm::support::ulittle16_t *>(location) =
-      result |
-      (uint16_t) * reinterpret_cast<llvm::support::ulittle16_t *>(location);
+  write16le(location, result | read16le(location));
   // TODO: Check for overflow.
 }
 
 /// \brief R_X86_64_PC64 - word64: S + A - P
 static void relocPC64(uint8_t *location, uint64_t P, uint64_t S, uint64_t A) {
   int64_t result = (uint64_t)((S + A) - P);
-  *reinterpret_cast<llvm::support::ulittle64_t *>(location) =
-      result |
-      (uint64_t) * reinterpret_cast<llvm::support::ulittle64_t *>(location);
+  write64le(location, result | read64le(location));
 }
 
 std::error_code X86_64TargetRelocationHandler::applyRelocation(
@@ -100,11 +89,9 @@ std::error_code X86_64TargetRelocationHa
     _tlsSize = _x86_64Layout.getTLSSize();
     if (ref.kindValue() == R_X86_64_TPOFF32 ||
         ref.kindValue() == R_X86_64_DTPOFF32) {
-      int32_t result = (int32_t)(targetVAddress - _tlsSize);
-      *reinterpret_cast<llvm::support::little32_t *>(location) = result;
+      write32le(location, targetVAddress - _tlsSize);
     } else {
-      int64_t result = (int64_t)(targetVAddress - _tlsSize);
-      *reinterpret_cast<llvm::support::little64_t *>(location) = result;
+      write64le(location, targetVAddress - _tlsSize);
     }
     break;
   }

Modified: lld/trunk/lib/ReaderWriter/PECOFF/IdataPass.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/IdataPass.cpp?rev=230725&r1=230724&r2=230725&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/IdataPass.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/IdataPass.cpp Thu Feb 26 21:18:46 2015
@@ -9,6 +9,7 @@
 
 #include "IdataPass.h"
 #include "Pass.h"
+#include "lld/Core/Endian.h"
 #include "lld/Core/File.h"
 #include "lld/Core/Pass.h"
 #include "lld/Core/Simple.h"
@@ -45,7 +46,7 @@ std::vector<uint8_t> HintNameAtom::assem
   std::vector<uint8_t> ret(size);
   ret[importName.size()] = 0;
   ret[importName.size() - 1] = 0;
-  *reinterpret_cast<llvm::support::ulittle16_t *>(&ret[0]) = hint;
+  write16le(&ret[0], hint);
   std::memcpy(&ret[2], importName.data(), importName.size());
   return ret;
 }
@@ -56,11 +57,11 @@ ImportTableEntryAtom::assembleRawContent
   // in PE+. In PE+, bits 62-31 are filled with zero.
   if (is64) {
     std::vector<uint8_t> ret(8);
-    *reinterpret_cast<llvm::support::ulittle64_t *>(&ret[0]) = rva;
+    write64le(&ret[0], rva);
     return ret;
   }
   std::vector<uint8_t> ret(4);
-  *reinterpret_cast<llvm::support::ulittle32_t *>(&ret[0]) = rva;
+  write32le(&ret[0], rva);
   return ret;
 }
 

Modified: lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp?rev=230725&r1=230724&r2=230725&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/ReaderImportHeader.cpp Thu Feb 26 21:18:46 2015
@@ -119,6 +119,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "Atoms.h"
+#include "lld/Core/Endian.h"
 #include "lld/Core/Error.h"
 #include "lld/Core/File.h"
 #include "lld/Core/SharedLibraryAtom.h"
@@ -245,22 +246,20 @@ public:
     const char *end = _mb->getBufferEnd();
 
     // The size of the string that follows the header.
-    uint32_t dataSize = *reinterpret_cast<const support::ulittle32_t *>(
-        buf + offsetof(COFF::ImportHeader, SizeOfData));
+    uint32_t dataSize
+        = read32le(buf + offsetof(COFF::ImportHeader, SizeOfData));
 
     // Check if the total size is valid.
     if (std::size_t(end - buf) != sizeof(COFF::ImportHeader) + dataSize)
       return make_error_code(NativeReaderError::unknown_file_format);
 
-    uint16_t hint = *reinterpret_cast<const support::ulittle16_t *>(
-        buf + offsetof(COFF::ImportHeader, OrdinalHint));
+    uint16_t hint = read16le(buf + offsetof(COFF::ImportHeader, OrdinalHint));
     StringRef symbolName(buf + sizeof(COFF::ImportHeader));
     StringRef dllName(buf + sizeof(COFF::ImportHeader) + symbolName.size() + 1);
 
     // TypeInfo is a bitfield. The least significant 2 bits are import
     // type, followed by 3 bit import name type.
-    uint16_t typeInfo = *reinterpret_cast<const support::ulittle16_t *>(
-        buf + offsetof(COFF::ImportHeader, TypeInfo));
+    uint16_t typeInfo = read16le(buf + offsetof(COFF::ImportHeader, TypeInfo));
     int type = typeInfo & 0x3;
     int nameType = (typeInfo >> 2) & 0x7;
 

Modified: lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp?rev=230725&r1=230724&r2=230725&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp Thu Feb 26 21:18:46 2015
@@ -22,6 +22,7 @@
 #include "Atoms.h"
 #include "WriterImportLibrary.h"
 #include "lld/Core/DefinedAtom.h"
+#include "lld/Core/Endian.h"
 #include "lld/Core/File.h"
 #include "lld/Core/Writer.h"
 #include "lld/ReaderWriter/AtomLayout.h"
@@ -198,7 +199,7 @@ public:
       // Set the name of the dummy symbol to the first string table entry.
       // It's better than letting dumpbin print out a garabage as a symbol name.
       char *off = _stringTable.data() + 4;
-      *reinterpret_cast<ulittle32_t *>(off) = 4;
+      write32le(off, 4);
     }
     uint32_t offset = _stringTable.size();
     _stringTable.insert(_stringTable.end(), sectionName.begin(),
@@ -213,7 +214,7 @@ public:
     if (_stringTable.empty())
       return;
     char *off = _stringTable.data() + sizeof(llvm::object::coff_symbol16);
-    *reinterpret_cast<ulittle32_t *>(off) = _stringTable.size();
+    write32le(off, _stringTable.size());
     std::memcpy(buffer, _stringTable.data(), _stringTable.size());
   }
 
@@ -1005,17 +1006,17 @@ BaseRelocChunk::createBaseRelocBlock(uin
   uint8_t *ptr = &contents[0];
 
   // The first four bytes is the page RVA.
-  *reinterpret_cast<ulittle32_t *>(ptr) = pageAddr;
+  write32le(ptr, pageAddr);
   ptr += sizeof(ulittle32_t);
 
   // The second four bytes is the size of the block, including the the page
   // RVA and this size field.
-  *reinterpret_cast<ulittle32_t *>(ptr) = size;
+  write32le(ptr, size);
   ptr += sizeof(ulittle32_t);
 
   for (const auto &reloc : relocs) {
     assert(reloc.first < _ctx.getPageSize());
-    *reinterpret_cast<ulittle16_t *>(ptr) = (reloc.second << 12) | reloc.first;
+    write16le(ptr, (reloc.second << 12) | reloc.first);
     ptr += sizeof(ulittle16_t);
   }
   return contents;





More information about the llvm-commits mailing list