1 //===- lib/ReaderWriter/MachO/MachONormalizedFileBinaryUtils.h ------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_READER_WRITER_MACHO_NORMALIZED_FILE_BINARY_UTILS_H
11 #define LLD_READER_WRITER_MACHO_NORMALIZED_FILE_BINARY_UTILS_H
13 #include "MachONormalizedFile.h"
14 #include "lld/Core/Error.h"
15 #include "lld/Core/LLVM.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/Casting.h"
18 #include "llvm/Support/Endian.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/Host.h"
21 #include "llvm/Support/LEB128.h"
22 #include "llvm/Support/MachO.h"
23 #include <system_error>
27 namespace normalized {
31 ByteBuffer() : _ostream(_bytes) { }
33 void append_byte(uint8_t b) {
36 void append_uleb128(uint64_t value) {
37 llvm::encodeULEB128(value, _ostream);
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);
45 void append_sleb128(int64_t value) {
46 llvm::encodeSLEB128(value, _ostream);
48 void append_string(StringRef str) {
52 void align(unsigned alignment) {
53 while ( (_ostream.tell() % alignment) != 0 )
57 return _ostream.tell();
59 const uint8_t *bytes() {
60 return reinterpret_cast<const uint8_t*>(_ostream.str().data());
64 SmallVector<char, 128> _bytes;
65 // Stream ivar must be after SmallVector ivar to construct properly.
66 llvm::raw_svector_ostream _ostream;
69 using namespace llvm::support::endian;
70 using llvm::sys::getSwappedBytes;
73 static inline uint16_t read16(const T *loc, bool isBig) {
74 assert((uint64_t)loc % llvm::alignOf<T>() == 0 &&
75 "invalid pointer alignment");
76 return isBig ? read16be(loc) : read16le(loc);
80 static inline uint32_t read32(const T *loc, bool isBig) {
81 assert((uint64_t)loc % llvm::alignOf<T>() == 0 &&
82 "invalid pointer alignment");
83 return isBig ? read32be(loc) : read32le(loc);
87 static inline uint64_t read64(const T *loc, bool isBig) {
88 assert((uint64_t)loc % llvm::alignOf<T>() == 0 &&
89 "invalid pointer alignment");
90 return isBig ? read64be(loc) : read64le(loc);
93 inline void write16(uint8_t *loc, uint16_t value, bool isBig) {
95 write16be(loc, value);
97 write16le(loc, value);
100 inline void write32(uint8_t *loc, uint32_t value, bool isBig) {
102 write32be(loc, value);
104 write32le(loc, value);
107 inline void write64(uint8_t *loc, uint64_t value, bool isBig) {
109 write64be(loc, value);
111 write64le(loc, value);
115 bitFieldExtract(uint32_t value, bool isBigEndianBigField, uint8_t firstBit,
117 const uint32_t mask = ((1<<bitCount)-1);
118 const uint8_t shift = isBigEndianBigField ? (32-firstBit-bitCount) : firstBit;
119 return (value >> shift) & mask;
123 bitFieldSet(uint32_t &bits, bool isBigEndianBigField, uint32_t newBits,
124 uint8_t firstBit, uint8_t bitCount) {
125 const uint32_t mask = ((1<<bitCount)-1);
126 assert((newBits & mask) == newBits);
127 const uint8_t shift = isBigEndianBigField ? (32-firstBit-bitCount) : firstBit;
128 bits &= ~(mask << shift);
129 bits |= (newBits << shift);
132 inline Relocation unpackRelocation(const llvm::MachO::any_relocation_info &r,
134 uint32_t r0 = read32(&r.r_word0, isBigEndian);
135 uint32_t r1 = read32(&r.r_word1, isBigEndian);
138 if (r0 & llvm::MachO::R_SCATTERED) {
139 // scattered relocation record always laid out like big endian bit field
140 result.offset = bitFieldExtract(r0, true, 8, 24);
141 result.scattered = true;
142 result.type = (RelocationInfoType)
143 bitFieldExtract(r0, true, 4, 4);
144 result.length = bitFieldExtract(r0, true, 2, 2);
145 result.pcRel = bitFieldExtract(r0, true, 1, 1);
146 result.isExtern = false;
151 result.scattered = false;
152 result.type = (RelocationInfoType)
153 bitFieldExtract(r1, isBigEndian, 28, 4);
154 result.length = bitFieldExtract(r1, isBigEndian, 25, 2);
155 result.pcRel = bitFieldExtract(r1, isBigEndian, 24, 1);
156 result.isExtern = bitFieldExtract(r1, isBigEndian, 27, 1);
158 result.symbol = bitFieldExtract(r1, isBigEndian, 0, 24);
164 inline llvm::MachO::any_relocation_info
165 packRelocation(const Relocation &r, bool swap, bool isBigEndian) {
171 bitFieldSet(r0, true, r.offset, 8, 24);
172 bitFieldSet(r0, true, r.type, 4, 4);
173 bitFieldSet(r0, true, r.length, 2, 2);
174 bitFieldSet(r0, true, r.pcRel, 1, 1);
175 bitFieldSet(r0, true, r.scattered, 0, 1); // R_SCATTERED
178 bitFieldSet(r1, isBigEndian, r.type, 28, 4);
179 bitFieldSet(r1, isBigEndian, r.isExtern, 27, 1);
180 bitFieldSet(r1, isBigEndian, r.length, 25, 2);
181 bitFieldSet(r1, isBigEndian, r.pcRel, 24, 1);
182 bitFieldSet(r1, isBigEndian, r.symbol, 0, 24);
185 llvm::MachO::any_relocation_info result;
186 result.r_word0 = swap ? getSwappedBytes(r0) : r0;
187 result.r_word1 = swap ? getSwappedBytes(r1) : r1;
191 inline StringRef getString16(const char s[16]) {
194 return x.substr(0, 16);
199 inline void setString16(StringRef str, char s[16]) {
201 memcpy(s, str.begin(), (str.size() > 16) ? 16: str.size());
204 // Implemented in normalizedToAtoms() and used by normalizedFromAtoms() so
205 // that the same table can be used to map mach-o sections to and from
206 // DefinedAtom::ContentType.
207 void relocatableSectionInfoForContentType(DefinedAtom::ContentType atomType,
208 StringRef &segmentName,
209 StringRef §ionName,
210 SectionType §ionType,
211 SectionAttr §ionAttrs,
212 bool &relocsToDefinedCanBeImplicit);
214 } // namespace normalized
215 } // namespace mach_o
218 #endif // LLD_READER_WRITER_MACHO_NORMALIZED_FILE_BINARY_UTILS_H