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 | |
Pete Cooper | d75b718 | 2016-02-08 21:50:45 +0000 | [diff] [blame] | 10 | #ifndef LLD_READER_WRITER_MACHO_NORMALIZED_FILE_BINARY_UTILS_H |
| 11 | #define LLD_READER_WRITER_MACHO_NORMALIZED_FILE_BINARY_UTILS_H |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 12 | |
| 13 | #include "MachONormalizedFile.h" |
Rui Ueyama | 3f85170 | 2017-10-02 21:00:41 +0000 | [diff] [blame] | 14 | #include "lld/Common/LLVM.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 15 | #include "lld/Core/Error.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringRef.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 17 | #include "llvm/BinaryFormat/MachO.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Casting.h" |
Rui Ueyama | 25b87a4 | 2015-03-02 20:31:43 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Endian.h" |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 20 | #include "llvm/Support/ErrorHandling.h" |
| 21 | #include "llvm/Support/Host.h" |
Pete Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 22 | #include "llvm/Support/LEB128.h" |
Rafael Espindola | 54427cc | 2014-06-12 17:15:58 +0000 | [diff] [blame] | 23 | #include <system_error> |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 24 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 25 | namespace lld { |
| 26 | namespace mach_o { |
| 27 | namespace normalized { |
| 28 | |
Pete Cooper | 41f3e8e | 2016-02-09 01:38:13 +0000 | [diff] [blame] | 29 | class ByteBuffer { |
| 30 | public: |
| 31 | ByteBuffer() : _ostream(_bytes) { } |
| 32 | |
| 33 | void append_byte(uint8_t b) { |
| 34 | _ostream << b; |
| 35 | } |
| 36 | void append_uleb128(uint64_t value) { |
| 37 | llvm::encodeULEB128(value, _ostream); |
| 38 | } |
| 39 | void append_uleb128Fixed(uint64_t value, unsigned byteCount) { |
| 40 | unsigned min = llvm::getULEB128Size(value); |
| 41 | assert(min <= byteCount); |
| 42 | unsigned pad = byteCount - min; |
| 43 | llvm::encodeULEB128(value, _ostream, pad); |
| 44 | } |
| 45 | void append_sleb128(int64_t value) { |
| 46 | llvm::encodeSLEB128(value, _ostream); |
| 47 | } |
| 48 | void append_string(StringRef str) { |
| 49 | _ostream << str; |
| 50 | append_byte(0); |
| 51 | } |
| 52 | void align(unsigned alignment) { |
| 53 | while ( (_ostream.tell() % alignment) != 0 ) |
| 54 | append_byte(0); |
| 55 | } |
| 56 | size_t size() { |
| 57 | return _ostream.tell(); |
| 58 | } |
| 59 | const uint8_t *bytes() { |
| 60 | return reinterpret_cast<const uint8_t*>(_ostream.str().data()); |
| 61 | } |
| 62 | |
| 63 | private: |
| 64 | SmallVector<char, 128> _bytes; |
| 65 | // Stream ivar must be after SmallVector ivar to construct properly. |
| 66 | llvm::raw_svector_ostream _ostream; |
| 67 | }; |
| 68 | |
Rui Ueyama | 25b87a4 | 2015-03-02 20:31:43 +0000 | [diff] [blame] | 69 | using namespace llvm::support::endian; |
Artyom Skrobov | 9b3f647 | 2014-06-14 12:14:25 +0000 | [diff] [blame] | 70 | using llvm::sys::getSwappedBytes; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 71 | |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 72 | template<typename T> |
| 73 | static inline uint16_t read16(const T *loc, bool isBig) { |
Benjamin Kramer | 3ad3f50 | 2016-10-20 15:30:02 +0000 | [diff] [blame] | 74 | assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment"); |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 75 | return isBig ? read16be(loc) : read16le(loc); |
Nick Kledzik | de0860a | 2014-07-02 23:52:22 +0000 | [diff] [blame] | 76 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 77 | |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 78 | template<typename T> |
| 79 | static inline uint32_t read32(const T *loc, bool isBig) { |
Benjamin Kramer | 3ad3f50 | 2016-10-20 15:30:02 +0000 | [diff] [blame] | 80 | assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment"); |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 81 | return isBig ? read32be(loc) : read32le(loc); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 84 | template<typename T> |
| 85 | static inline uint64_t read64(const T *loc, bool isBig) { |
Benjamin Kramer | 3ad3f50 | 2016-10-20 15:30:02 +0000 | [diff] [blame] | 86 | assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment"); |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 87 | return isBig ? read64be(loc) : read64le(loc); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 90 | inline void write16(uint8_t *loc, uint16_t value, bool isBig) { |
| 91 | if (isBig) |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 92 | write16be(loc, value); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 93 | else |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 94 | write16le(loc, value); |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 95 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 96 | |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 97 | inline void write32(uint8_t *loc, uint32_t value, bool isBig) { |
| 98 | if (isBig) |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 99 | write32be(loc, value); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 100 | else |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 101 | write32le(loc, value); |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 104 | inline void write64(uint8_t *loc, uint64_t value, bool isBig) { |
| 105 | if (isBig) |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 106 | write64be(loc, value); |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 107 | else |
Rui Ueyama | e088a0f | 2015-02-27 04:21:40 +0000 | [diff] [blame] | 108 | write64le(loc, value); |
Nick Kledzik | 2458bec | 2014-07-16 19:49:02 +0000 | [diff] [blame] | 109 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 110 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 111 | inline uint32_t |
| 112 | bitFieldExtract(uint32_t value, bool isBigEndianBigField, uint8_t firstBit, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 113 | uint8_t bitCount) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 114 | const uint32_t mask = ((1<<bitCount)-1); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 115 | const uint8_t shift = isBigEndianBigField ? (32-firstBit-bitCount) : firstBit; |
| 116 | return (value >> shift) & mask; |
| 117 | } |
| 118 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 119 | inline void |
| 120 | bitFieldSet(uint32_t &bits, bool isBigEndianBigField, uint32_t newBits, |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 121 | uint8_t firstBit, uint8_t bitCount) { |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 122 | const uint32_t mask = ((1<<bitCount)-1); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 123 | assert((newBits & mask) == newBits); |
| 124 | const uint8_t shift = isBigEndianBigField ? (32-firstBit-bitCount) : firstBit; |
| 125 | bits &= ~(mask << shift); |
| 126 | bits |= (newBits << shift); |
| 127 | } |
| 128 | |
Tim Northover | 40d3ad3 | 2014-10-27 22:48:35 +0000 | [diff] [blame] | 129 | inline Relocation unpackRelocation(const llvm::MachO::any_relocation_info &r, |
| 130 | bool isBigEndian) { |
| 131 | uint32_t r0 = read32(&r.r_word0, isBigEndian); |
| 132 | uint32_t r1 = read32(&r.r_word1, isBigEndian); |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 133 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 134 | Relocation result; |
| 135 | if (r0 & llvm::MachO::R_SCATTERED) { |
| 136 | // scattered relocation record always laid out like big endian bit field |
| 137 | result.offset = bitFieldExtract(r0, true, 8, 24); |
| 138 | result.scattered = true; |
| 139 | result.type = (RelocationInfoType) |
| 140 | bitFieldExtract(r0, true, 4, 4); |
| 141 | result.length = bitFieldExtract(r0, true, 2, 2); |
| 142 | result.pcRel = bitFieldExtract(r0, true, 1, 1); |
| 143 | result.isExtern = false; |
| 144 | result.value = r1; |
| 145 | result.symbol = 0; |
| 146 | } else { |
| 147 | result.offset = r0; |
| 148 | result.scattered = false; |
| 149 | result.type = (RelocationInfoType) |
| 150 | bitFieldExtract(r1, isBigEndian, 28, 4); |
| 151 | result.length = bitFieldExtract(r1, isBigEndian, 25, 2); |
| 152 | result.pcRel = bitFieldExtract(r1, isBigEndian, 24, 1); |
| 153 | result.isExtern = bitFieldExtract(r1, isBigEndian, 27, 1); |
| 154 | result.value = 0; |
| 155 | result.symbol = bitFieldExtract(r1, isBigEndian, 0, 24); |
| 156 | } |
| 157 | return result; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 158 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 159 | |
| 160 | |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 161 | inline llvm::MachO::any_relocation_info |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 162 | packRelocation(const Relocation &r, bool swap, bool isBigEndian) { |
| 163 | uint32_t r0 = 0; |
| 164 | uint32_t r1 = 0; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 165 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 166 | if (r.scattered) { |
| 167 | r1 = r.value; |
| 168 | bitFieldSet(r0, true, r.offset, 8, 24); |
| 169 | bitFieldSet(r0, true, r.type, 4, 4); |
| 170 | bitFieldSet(r0, true, r.length, 2, 2); |
| 171 | bitFieldSet(r0, true, r.pcRel, 1, 1); |
| 172 | bitFieldSet(r0, true, r.scattered, 0, 1); // R_SCATTERED |
| 173 | } else { |
| 174 | r0 = r.offset; |
| 175 | bitFieldSet(r1, isBigEndian, r.type, 28, 4); |
| 176 | bitFieldSet(r1, isBigEndian, r.isExtern, 27, 1); |
| 177 | bitFieldSet(r1, isBigEndian, r.length, 25, 2); |
| 178 | bitFieldSet(r1, isBigEndian, r.pcRel, 24, 1); |
| 179 | bitFieldSet(r1, isBigEndian, r.symbol, 0, 24); |
| 180 | } |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 181 | |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 182 | llvm::MachO::any_relocation_info result; |
Artyom Skrobov | 9b3f647 | 2014-06-14 12:14:25 +0000 | [diff] [blame] | 183 | result.r_word0 = swap ? getSwappedBytes(r0) : r0; |
| 184 | result.r_word1 = swap ? getSwappedBytes(r1) : r1; |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 185 | return result; |
Shankar Easwaran | 3d8de47 | 2014-01-27 03:09:26 +0000 | [diff] [blame] | 186 | } |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 187 | |
Tom Stellard | a674913 | 2018-09-07 15:51:52 +0000 | [diff] [blame] | 188 | inline StringRef getString16(const char s[16]) { |
Tom Stellard | 493f3ba | 2018-09-07 15:42:01 +0000 | [diff] [blame] | 189 | // The StringRef(const char *) constructor passes the const char * to |
| 190 | // strlen(), so we can't use this constructor here, because if there is no |
| 191 | // null terminator in s, then strlen() will read past the end of the array. |
| 192 | return StringRef(s, strnlen(s, 16)); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | inline void setString16(StringRef str, char s[16]) { |
| 196 | memset(s, 0, 16); |
| 197 | memcpy(s, str.begin(), (str.size() > 16) ? 16: str.size()); |
| 198 | } |
| 199 | |
Nick Kledzik | ec14083 | 2014-06-10 01:50:00 +0000 | [diff] [blame] | 200 | // Implemented in normalizedToAtoms() and used by normalizedFromAtoms() so |
| 201 | // that the same table can be used to map mach-o sections to and from |
| 202 | // DefinedAtom::ContentType. |
| 203 | void relocatableSectionInfoForContentType(DefinedAtom::ContentType atomType, |
| 204 | StringRef &segmentName, |
| 205 | StringRef §ionName, |
| 206 | SectionType §ionType, |
Pete Cooper | ac03979 | 2016-01-07 21:07:26 +0000 | [diff] [blame] | 207 | SectionAttr §ionAttrs, |
| 208 | bool &relocsToDefinedCanBeImplicit); |
Nick Kledzik | e34182f | 2013-11-06 21:36:55 +0000 | [diff] [blame] | 209 | |
| 210 | } // namespace normalized |
| 211 | } // namespace mach_o |
| 212 | } // namespace lld |
| 213 | |
Rui Ueyama | 014192db | 2013-11-15 03:09:26 +0000 | [diff] [blame] | 214 | #endif // LLD_READER_WRITER_MACHO_NORMALIZED_FILE_BINARY_UTILS_H |