2013-11-07 05:36:55 +08:00
|
|
|
//===- lib/ReaderWriter/MachO/MachONormalizedFileBinaryUtils.h ------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2013-11-07 05:36:55 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-02-09 05:50:45 +08:00
|
|
|
#ifndef LLD_READER_WRITER_MACHO_NORMALIZED_FILE_BINARY_UTILS_H
|
|
|
|
#define LLD_READER_WRITER_MACHO_NORMALIZED_FILE_BINARY_UTILS_H
|
2013-11-07 05:36:55 +08:00
|
|
|
|
|
|
|
#include "MachONormalizedFile.h"
|
2017-10-03 05:00:41 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
2013-11-07 05:36:55 +08:00
|
|
|
#include "lld/Core/Error.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/MachO.h"
|
2013-11-07 05:36:55 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2015-03-03 04:31:43 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2013-11-07 05:36:55 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
#include "llvm/Support/Host.h"
|
2016-02-09 09:38:13 +08:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2014-06-13 01:15:58 +08:00
|
|
|
#include <system_error>
|
2013-11-07 05:36:55 +08:00
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace mach_o {
|
|
|
|
namespace normalized {
|
|
|
|
|
2016-02-09 09:38:13 +08:00
|
|
|
class ByteBuffer {
|
|
|
|
public:
|
|
|
|
ByteBuffer() : _ostream(_bytes) { }
|
|
|
|
|
|
|
|
void append_byte(uint8_t b) {
|
|
|
|
_ostream << b;
|
|
|
|
}
|
|
|
|
void append_uleb128(uint64_t value) {
|
|
|
|
llvm::encodeULEB128(value, _ostream);
|
|
|
|
}
|
|
|
|
void append_uleb128Fixed(uint64_t value, unsigned byteCount) {
|
|
|
|
unsigned min = llvm::getULEB128Size(value);
|
|
|
|
assert(min <= byteCount);
|
|
|
|
unsigned pad = byteCount - min;
|
|
|
|
llvm::encodeULEB128(value, _ostream, pad);
|
|
|
|
}
|
|
|
|
void append_sleb128(int64_t value) {
|
|
|
|
llvm::encodeSLEB128(value, _ostream);
|
|
|
|
}
|
|
|
|
void append_string(StringRef str) {
|
|
|
|
_ostream << str;
|
|
|
|
append_byte(0);
|
|
|
|
}
|
|
|
|
void align(unsigned alignment) {
|
|
|
|
while ( (_ostream.tell() % alignment) != 0 )
|
|
|
|
append_byte(0);
|
|
|
|
}
|
|
|
|
size_t size() {
|
|
|
|
return _ostream.tell();
|
|
|
|
}
|
|
|
|
const uint8_t *bytes() {
|
|
|
|
return reinterpret_cast<const uint8_t*>(_ostream.str().data());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SmallVector<char, 128> _bytes;
|
|
|
|
// Stream ivar must be after SmallVector ivar to construct properly.
|
|
|
|
llvm::raw_svector_ostream _ostream;
|
|
|
|
};
|
|
|
|
|
2015-03-03 04:31:43 +08:00
|
|
|
using namespace llvm::support::endian;
|
2014-06-14 20:14:25 +08:00
|
|
|
using llvm::sys::getSwappedBytes;
|
2013-11-07 05:36:55 +08:00
|
|
|
|
2014-10-28 06:48:35 +08:00
|
|
|
template<typename T>
|
|
|
|
static inline uint16_t read16(const T *loc, bool isBig) {
|
2016-10-20 23:30:02 +08:00
|
|
|
assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment");
|
2015-02-27 12:21:40 +08:00
|
|
|
return isBig ? read16be(loc) : read16le(loc);
|
2014-07-03 07:52:22 +08:00
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
|
2014-10-28 06:48:35 +08:00
|
|
|
template<typename T>
|
|
|
|
static inline uint32_t read32(const T *loc, bool isBig) {
|
2016-10-20 23:30:02 +08:00
|
|
|
assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment");
|
2015-02-27 12:21:40 +08:00
|
|
|
return isBig ? read32be(loc) : read32le(loc);
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
2014-10-28 06:48:35 +08:00
|
|
|
template<typename T>
|
|
|
|
static inline uint64_t read64(const T *loc, bool isBig) {
|
2016-10-20 23:30:02 +08:00
|
|
|
assert((uint64_t)loc % alignof(T) == 0 && "invalid pointer alignment");
|
2015-02-27 12:21:40 +08:00
|
|
|
return isBig ? read64be(loc) : read64le(loc);
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
2014-10-28 06:48:35 +08:00
|
|
|
inline void write16(uint8_t *loc, uint16_t value, bool isBig) {
|
|
|
|
if (isBig)
|
2015-02-27 12:21:40 +08:00
|
|
|
write16be(loc, value);
|
2014-10-28 06:48:35 +08:00
|
|
|
else
|
2015-02-27 12:21:40 +08:00
|
|
|
write16le(loc, value);
|
2014-07-17 03:49:02 +08:00
|
|
|
}
|
|
|
|
|
2014-10-28 06:48:35 +08:00
|
|
|
inline void write32(uint8_t *loc, uint32_t value, bool isBig) {
|
|
|
|
if (isBig)
|
2015-02-27 12:21:40 +08:00
|
|
|
write32be(loc, value);
|
2014-10-28 06:48:35 +08:00
|
|
|
else
|
2015-02-27 12:21:40 +08:00
|
|
|
write32le(loc, value);
|
2014-07-17 03:49:02 +08:00
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
|
2014-10-28 06:48:35 +08:00
|
|
|
inline void write64(uint8_t *loc, uint64_t value, bool isBig) {
|
|
|
|
if (isBig)
|
2015-02-27 12:21:40 +08:00
|
|
|
write64be(loc, value);
|
2014-10-28 06:48:35 +08:00
|
|
|
else
|
2015-02-27 12:21:40 +08:00
|
|
|
write64le(loc, value);
|
2014-07-17 03:49:02 +08:00
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
|
2014-01-27 11:09:26 +08:00
|
|
|
inline uint32_t
|
|
|
|
bitFieldExtract(uint32_t value, bool isBigEndianBigField, uint8_t firstBit,
|
2013-11-07 05:36:55 +08:00
|
|
|
uint8_t bitCount) {
|
2014-01-27 11:09:26 +08:00
|
|
|
const uint32_t mask = ((1<<bitCount)-1);
|
2013-11-07 05:36:55 +08:00
|
|
|
const uint8_t shift = isBigEndianBigField ? (32-firstBit-bitCount) : firstBit;
|
|
|
|
return (value >> shift) & mask;
|
|
|
|
}
|
|
|
|
|
2014-01-27 11:09:26 +08:00
|
|
|
inline void
|
|
|
|
bitFieldSet(uint32_t &bits, bool isBigEndianBigField, uint32_t newBits,
|
2013-11-07 05:36:55 +08:00
|
|
|
uint8_t firstBit, uint8_t bitCount) {
|
2014-01-27 11:09:26 +08:00
|
|
|
const uint32_t mask = ((1<<bitCount)-1);
|
2013-11-07 05:36:55 +08:00
|
|
|
assert((newBits & mask) == newBits);
|
|
|
|
const uint8_t shift = isBigEndianBigField ? (32-firstBit-bitCount) : firstBit;
|
|
|
|
bits &= ~(mask << shift);
|
|
|
|
bits |= (newBits << shift);
|
|
|
|
}
|
|
|
|
|
2014-10-28 06:48:35 +08:00
|
|
|
inline Relocation unpackRelocation(const llvm::MachO::any_relocation_info &r,
|
|
|
|
bool isBigEndian) {
|
|
|
|
uint32_t r0 = read32(&r.r_word0, isBigEndian);
|
|
|
|
uint32_t r1 = read32(&r.r_word1, isBigEndian);
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
Relocation result;
|
|
|
|
if (r0 & llvm::MachO::R_SCATTERED) {
|
|
|
|
// scattered relocation record always laid out like big endian bit field
|
|
|
|
result.offset = bitFieldExtract(r0, true, 8, 24);
|
|
|
|
result.scattered = true;
|
|
|
|
result.type = (RelocationInfoType)
|
|
|
|
bitFieldExtract(r0, true, 4, 4);
|
|
|
|
result.length = bitFieldExtract(r0, true, 2, 2);
|
|
|
|
result.pcRel = bitFieldExtract(r0, true, 1, 1);
|
|
|
|
result.isExtern = false;
|
|
|
|
result.value = r1;
|
|
|
|
result.symbol = 0;
|
|
|
|
} else {
|
|
|
|
result.offset = r0;
|
|
|
|
result.scattered = false;
|
|
|
|
result.type = (RelocationInfoType)
|
|
|
|
bitFieldExtract(r1, isBigEndian, 28, 4);
|
|
|
|
result.length = bitFieldExtract(r1, isBigEndian, 25, 2);
|
|
|
|
result.pcRel = bitFieldExtract(r1, isBigEndian, 24, 1);
|
|
|
|
result.isExtern = bitFieldExtract(r1, isBigEndian, 27, 1);
|
|
|
|
result.value = 0;
|
|
|
|
result.symbol = bitFieldExtract(r1, isBigEndian, 0, 24);
|
|
|
|
}
|
|
|
|
return result;
|
2014-01-27 11:09:26 +08:00
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
|
|
|
|
|
2014-01-27 11:09:26 +08:00
|
|
|
inline llvm::MachO::any_relocation_info
|
2013-11-07 05:36:55 +08:00
|
|
|
packRelocation(const Relocation &r, bool swap, bool isBigEndian) {
|
|
|
|
uint32_t r0 = 0;
|
|
|
|
uint32_t r1 = 0;
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
if (r.scattered) {
|
|
|
|
r1 = r.value;
|
|
|
|
bitFieldSet(r0, true, r.offset, 8, 24);
|
|
|
|
bitFieldSet(r0, true, r.type, 4, 4);
|
|
|
|
bitFieldSet(r0, true, r.length, 2, 2);
|
|
|
|
bitFieldSet(r0, true, r.pcRel, 1, 1);
|
|
|
|
bitFieldSet(r0, true, r.scattered, 0, 1); // R_SCATTERED
|
|
|
|
} else {
|
|
|
|
r0 = r.offset;
|
|
|
|
bitFieldSet(r1, isBigEndian, r.type, 28, 4);
|
|
|
|
bitFieldSet(r1, isBigEndian, r.isExtern, 27, 1);
|
|
|
|
bitFieldSet(r1, isBigEndian, r.length, 25, 2);
|
|
|
|
bitFieldSet(r1, isBigEndian, r.pcRel, 24, 1);
|
|
|
|
bitFieldSet(r1, isBigEndian, r.symbol, 0, 24);
|
|
|
|
}
|
2014-01-27 11:09:26 +08:00
|
|
|
|
2013-11-07 05:36:55 +08:00
|
|
|
llvm::MachO::any_relocation_info result;
|
2014-06-14 20:14:25 +08:00
|
|
|
result.r_word0 = swap ? getSwappedBytes(r0) : r0;
|
|
|
|
result.r_word1 = swap ? getSwappedBytes(r1) : r1;
|
2013-11-07 05:36:55 +08:00
|
|
|
return result;
|
2014-01-27 11:09:26 +08:00
|
|
|
}
|
2013-11-07 05:36:55 +08:00
|
|
|
|
2018-09-07 23:51:52 +08:00
|
|
|
inline StringRef getString16(const char s[16]) {
|
2018-09-07 23:42:01 +08:00
|
|
|
// The StringRef(const char *) constructor passes the const char * to
|
|
|
|
// strlen(), so we can't use this constructor here, because if there is no
|
|
|
|
// null terminator in s, then strlen() will read past the end of the array.
|
|
|
|
return StringRef(s, strnlen(s, 16));
|
2013-11-07 05:36:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void setString16(StringRef str, char s[16]) {
|
|
|
|
memset(s, 0, 16);
|
|
|
|
memcpy(s, str.begin(), (str.size() > 16) ? 16: str.size());
|
|
|
|
}
|
|
|
|
|
2014-06-10 09:50:00 +08:00
|
|
|
// Implemented in normalizedToAtoms() and used by normalizedFromAtoms() so
|
|
|
|
// that the same table can be used to map mach-o sections to and from
|
|
|
|
// DefinedAtom::ContentType.
|
|
|
|
void relocatableSectionInfoForContentType(DefinedAtom::ContentType atomType,
|
|
|
|
StringRef &segmentName,
|
|
|
|
StringRef §ionName,
|
|
|
|
SectionType §ionType,
|
2016-01-08 05:07:26 +08:00
|
|
|
SectionAttr §ionAttrs,
|
|
|
|
bool &relocsToDefinedCanBeImplicit);
|
2013-11-07 05:36:55 +08:00
|
|
|
|
|
|
|
} // namespace normalized
|
|
|
|
} // namespace mach_o
|
|
|
|
} // namespace lld
|
|
|
|
|
2013-11-15 11:09:26 +08:00
|
|
|
#endif // LLD_READER_WRITER_MACHO_NORMALIZED_FILE_BINARY_UTILS_H
|