Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 1 | //===- lib/ReaderWriter/MachO/MachONormalizedFileBinaryUtils.h ------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | |
| 11 | #include "MachONormalizedFile.h" |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 12 | #include "lld/Core/Endian.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 13 | #include "lld/Core/Error.h" |
| 14 | #include "lld/Core/LLVM.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringRef.h" |
| 16 | #include "llvm/Support/Casting.h" |
| 17 | #include "llvm/Support/ErrorHandling.h" |
| 18 | #include "llvm/Support/Host.h" |
| 19 | #include "llvm/Support/MachO.h" |
Rafael Espindola | 54427cc | 2014-06-12 17:15:58 +0000 | [diff] [blame] | 20 | #include <system_error> |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 21 | |
Rui Ueyama | 014192db | 2013-11-15 03:09:26 +0000 | [diff] [blame] | 22 | #ifndef LLD_READER_WRITER_MACHO_NORMALIZED_FILE_BINARY_UTILS_H |
| 23 | #define LLD_READER_WRITER_MACHO_NORMALIZED_FILE_BINARY_UTILS_H |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 24 | |
| 25 | namespace lld { |
| 26 | namespace mach_o { |
| 27 | namespace normalized { |
| 28 | |
Artyom Skrobov | 9b3f647 | 2014-06-14 12:14:25 +0000 | [diff] [blame] | 29 | using llvm::sys::getSwappedBytes; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 30 | |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 31 | template<typename T> |
| 32 | static inline uint16_t read16(const T *loc, bool isBig) { |
| 33 | assert((uint64_t)loc % llvm::alignOf<T>() == 0 && |
| 34 | "invalid pointer alignment"); |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 35 | return isBig ? read16be(loc) : read16le(loc); |
Nick Kledzik | de0860a | 2014-07-02 23:52:22 +0000 | [diff] [blame] | 36 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 37 | |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 38 | template<typename T> |
| 39 | static inline uint32_t read32(const T *loc, bool isBig) { |
| 40 | assert((uint64_t)loc % llvm::alignOf<T>() == 0 && |
| 41 | "invalid pointer alignment"); |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 42 | return isBig ? read32be(loc) : read32le(loc); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 45 | template<typename T> |
| 46 | static inline uint64_t read64(const T *loc, bool isBig) { |
| 47 | assert((uint64_t)loc % llvm::alignOf<T>() == 0 && |
| 48 | "invalid pointer alignment"); |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 49 | return isBig ? read64be(loc) : read64le(loc); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 52 | inline void write16(uint8_t *loc, uint16_t value, bool isBig) { |
| 53 | if (isBig) |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 54 | write16be(loc, value); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 55 | else |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 56 | write16le(loc, value); |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 57 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 58 | |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 59 | inline void write32(uint8_t *loc, uint32_t value, bool isBig) { |
| 60 | if (isBig) |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 61 | write32be(loc, value); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 62 | else |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 63 | write32le(loc, value); |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 66 | inline void write64(uint8_t *loc, uint64_t value, bool isBig) { |
| 67 | if (isBig) |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 68 | write64be(loc, value); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 69 | else |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 70 | write64le(loc, value); |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 71 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 72 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 73 | inline uint32_t |
| 74 | bitFieldExtract(uint32_t value, bool isBigEndianBigField, uint8_t firstBit, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 75 | uint8_t bitCount) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 76 | const uint32_t mask = ((1<<bitCount)-1); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 77 | const uint8_t shift = isBigEndianBigField ? (32-firstBit-bitCount) : firstBit; |
| 78 | return (value >> shift) & mask; |
| 79 | } |
| 80 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 81 | inline void |
| 82 | bitFieldSet(uint32_t &bits, bool isBigEndianBigField, uint32_t newBits, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 83 | uint8_t firstBit, uint8_t bitCount) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 84 | const uint32_t mask = ((1<<bitCount)-1); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 85 | assert((newBits & mask) == newBits); |
| 86 | const uint8_t shift = isBigEndianBigField ? (32-firstBit-bitCount) : firstBit; |
| 87 | bits &= ~(mask << shift); |
| 88 | bits |= (newBits << shift); |
| 89 | } |
| 90 | |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 91 | inline Relocation unpackRelocation(const llvm::MachO::any_relocation_info &r, |
| 92 | bool isBigEndian) { |
| 93 | uint32_t r0 = read32(&r.r_word0, isBigEndian); |
| 94 | uint32_t r1 = read32(&r.r_word1, isBigEndian); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 95 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 96 | Relocation result; |
| 97 | if (r0 & llvm::MachO::R_SCATTERED) { |
| 98 | // scattered relocation record always laid out like big endian bit field |
| 99 | result.offset = bitFieldExtract(r0, true, 8, 24); |
| 100 | result.scattered = true; |
| 101 | result.type = (RelocationInfoType) |
| 102 | bitFieldExtract(r0, true, 4, 4); |
| 103 | result.length = bitFieldExtract(r0, true, 2, 2); |
| 104 | result.pcRel = bitFieldExtract(r0, true, 1, 1); |
| 105 | result.isExtern = false; |
| 106 | result.value = r1; |
| 107 | result.symbol = 0; |
| 108 | } else { |
| 109 | result.offset = r0; |
| 110 | result.scattered = false; |
| 111 | result.type = (RelocationInfoType) |
| 112 | bitFieldExtract(r1, isBigEndian, 28, 4); |
| 113 | result.length = bitFieldExtract(r1, isBigEndian, 25, 2); |
| 114 | result.pcRel = bitFieldExtract(r1, isBigEndian, 24, 1); |
| 115 | result.isExtern = bitFieldExtract(r1, isBigEndian, 27, 1); |
| 116 | result.value = 0; |
| 117 | result.symbol = bitFieldExtract(r1, isBigEndian, 0, 24); |
| 118 | } |
| 119 | return result; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 120 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 121 | |
| 122 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 123 | inline llvm::MachO::any_relocation_info |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 124 | packRelocation(const Relocation &r, bool swap, bool isBigEndian) { |
| 125 | uint32_t r0 = 0; |
| 126 | uint32_t r1 = 0; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 127 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 128 | if (r.scattered) { |
| 129 | r1 = r.value; |
| 130 | bitFieldSet(r0, true, r.offset, 8, 24); |
| 131 | bitFieldSet(r0, true, r.type, 4, 4); |
| 132 | bitFieldSet(r0, true, r.length, 2, 2); |
| 133 | bitFieldSet(r0, true, r.pcRel, 1, 1); |
| 134 | bitFieldSet(r0, true, r.scattered, 0, 1); // R_SCATTERED |
| 135 | } else { |
| 136 | r0 = r.offset; |
| 137 | bitFieldSet(r1, isBigEndian, r.type, 28, 4); |
| 138 | bitFieldSet(r1, isBigEndian, r.isExtern, 27, 1); |
| 139 | bitFieldSet(r1, isBigEndian, r.length, 25, 2); |
| 140 | bitFieldSet(r1, isBigEndian, r.pcRel, 24, 1); |
| 141 | bitFieldSet(r1, isBigEndian, r.symbol, 0, 24); |
| 142 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 143 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 144 | llvm::MachO::any_relocation_info result; |
Artyom Skrobov | 9b3f647 | 2014-06-14 12:14:25 +0000 | [diff] [blame] | 145 | result.r_word0 = swap ? getSwappedBytes(r0) : r0; |
| 146 | result.r_word1 = swap ? getSwappedBytes(r1) : r1; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 147 | return result; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 148 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 149 | |
| 150 | inline StringRef getString16(const char s[16]) { |
| 151 | StringRef x = s; |
| 152 | if ( x.size() > 16 ) |
| 153 | return x.substr(0, 16); |
| 154 | else |
| 155 | return x; |
| 156 | } |
| 157 | |
| 158 | inline void setString16(StringRef str, char s[16]) { |
| 159 | memset(s, 0, 16); |
| 160 | memcpy(s, str.begin(), (str.size() > 16) ? 16: str.size()); |
| 161 | } |
| 162 | |
Nick Kledzik | ec14083 | 2014-06-10 01:50:00 +0000 | [diff] [blame] | 163 | // Implemented in normalizedToAtoms() and used by normalizedFromAtoms() so |
| 164 | // that the same table can be used to map mach-o sections to and from |
| 165 | // DefinedAtom::ContentType. |
| 166 | void relocatableSectionInfoForContentType(DefinedAtom::ContentType atomType, |
| 167 | StringRef &segmentName, |
| 168 | StringRef §ionName, |
| 169 | SectionType §ionType, |
| 170 | SectionAttr §ionAttrs); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 171 | |
| 172 | } // namespace normalized |
| 173 | } // namespace mach_o |
| 174 | } // namespace lld |
| 175 | |
Rui Ueyama | 014192db | 2013-11-15 03:09:26 +0000 | [diff] [blame] | 176 | #endif // LLD_READER_WRITER_MACHO_NORMALIZED_FILE_BINARY_UTILS_H |