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