2020-04-03 02:54:05 +08:00
|
|
|
//===- Target.h -------------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_MACHO_TARGET_H
|
|
|
|
#define LLD_MACHO_TARGET_H
|
|
|
|
|
2021-04-03 06:46:18 +08:00
|
|
|
#include "MachOStructs.h"
|
2021-03-12 02:28:09 +08:00
|
|
|
#include "Relocations.h"
|
|
|
|
|
2021-01-19 23:44:42 +08:00
|
|
|
#include "llvm/ADT/BitmaskEnum.h"
|
2020-05-16 04:42:28 +08:00
|
|
|
#include "llvm/BinaryFormat/MachO.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
|
[lld-macho] Support calls to functions in dylibs
Summary:
This diff implements lazy symbol binding -- very similar to the PLT
mechanism in ELF.
ELF's .plt section is broken up into two sections in Mach-O:
StubsSection and StubHelperSection. Calls to functions in dylibs will
end up calling into StubsSection, which contains indirect jumps to
addresses stored in the LazyPointerSection (the counterpart to ELF's
.plt.got).
Initially, the LazyPointerSection contains addresses that point into one
of the entry points in the middle of the StubHelperSection. The code in
StubHelperSection will push on the stack an offset into the
LazyBindingSection. The push is followed by a jump to the beginning of
the StubHelperSection (similar to PLT0), which then calls into
dyld_stub_binder. dyld_stub_binder is a non-lazily bound symbol, so this
call looks it up in the GOT.
The stub binder will look up the bind opcodes in the LazyBindingSection
at the given offset. The bind opcodes will tell the binder to update the
address in the LazyPointerSection to point to the symbol, so that
subsequent calls don't have to redo the symbol resolution. The binder
will then jump to the resolved symbol.
Depends on D78269.
Reviewers: ruiu, pcc, MaskRay, smeenai, alexshap, gkm, Ktwu, christylee
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78270
2020-05-06 08:38:10 +08:00
|
|
|
#include <cstddef>
|
2020-04-03 02:54:05 +08:00
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace macho {
|
2021-01-19 23:44:42 +08:00
|
|
|
LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();
|
2020-04-03 02:54:05 +08:00
|
|
|
|
2020-06-14 11:00:06 +08:00
|
|
|
class Symbol;
|
2021-03-30 08:33:48 +08:00
|
|
|
class Defined;
|
[lld-macho] Support calls to functions in dylibs
Summary:
This diff implements lazy symbol binding -- very similar to the PLT
mechanism in ELF.
ELF's .plt section is broken up into two sections in Mach-O:
StubsSection and StubHelperSection. Calls to functions in dylibs will
end up calling into StubsSection, which contains indirect jumps to
addresses stored in the LazyPointerSection (the counterpart to ELF's
.plt.got).
Initially, the LazyPointerSection contains addresses that point into one
of the entry points in the middle of the StubHelperSection. The code in
StubHelperSection will push on the stack an offset into the
LazyBindingSection. The push is followed by a jump to the beginning of
the StubHelperSection (similar to PLT0), which then calls into
dyld_stub_binder. dyld_stub_binder is a non-lazily bound symbol, so this
call looks it up in the GOT.
The stub binder will look up the bind opcodes in the LazyBindingSection
at the given offset. The bind opcodes will tell the binder to update the
address in the LazyPointerSection to point to the symbol, so that
subsequent calls don't have to redo the symbol resolution. The binder
will then jump to the resolved symbol.
Depends on D78269.
Reviewers: ruiu, pcc, MaskRay, smeenai, alexshap, gkm, Ktwu, christylee
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78270
2020-05-06 08:38:10 +08:00
|
|
|
class DylibSymbol;
|
2020-05-16 04:42:28 +08:00
|
|
|
class InputSection;
|
[lld-macho] Support calls to functions in dylibs
Summary:
This diff implements lazy symbol binding -- very similar to the PLT
mechanism in ELF.
ELF's .plt section is broken up into two sections in Mach-O:
StubsSection and StubHelperSection. Calls to functions in dylibs will
end up calling into StubsSection, which contains indirect jumps to
addresses stored in the LazyPointerSection (the counterpart to ELF's
.plt.got).
Initially, the LazyPointerSection contains addresses that point into one
of the entry points in the middle of the StubHelperSection. The code in
StubHelperSection will push on the stack an offset into the
LazyBindingSection. The push is followed by a jump to the beginning of
the StubHelperSection (similar to PLT0), which then calls into
dyld_stub_binder. dyld_stub_binder is a non-lazily bound symbol, so this
call looks it up in the GOT.
The stub binder will look up the bind opcodes in the LazyBindingSection
at the given offset. The bind opcodes will tell the binder to update the
address in the LazyPointerSection to point to the symbol, so that
subsequent calls don't have to redo the symbol resolution. The binder
will then jump to the resolved symbol.
Depends on D78269.
Reviewers: ruiu, pcc, MaskRay, smeenai, alexshap, gkm, Ktwu, christylee
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78270
2020-05-06 08:38:10 +08:00
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
class TargetInfo {
|
|
|
|
public:
|
2021-04-03 06:46:18 +08:00
|
|
|
template <class LP> TargetInfo(LP) {
|
|
|
|
// Having these values available in TargetInfo allows us to access them
|
|
|
|
// without having to resort to templates.
|
2021-05-04 06:31:23 +08:00
|
|
|
magic = LP::magic;
|
2021-04-03 06:46:18 +08:00
|
|
|
pageZeroSize = LP::pageZeroSize;
|
2021-05-04 06:31:23 +08:00
|
|
|
headerSize = sizeof(typename LP::mach_header);
|
2021-04-03 06:46:18 +08:00
|
|
|
wordSize = LP::wordSize;
|
|
|
|
}
|
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
virtual ~TargetInfo() = default;
|
2020-05-19 23:53:53 +08:00
|
|
|
|
2020-05-16 04:42:28 +08:00
|
|
|
// Validate the relocation structure and get its addend.
|
2021-03-13 06:26:11 +08:00
|
|
|
virtual int64_t
|
2021-04-03 06:46:18 +08:00
|
|
|
getEmbeddedAddend(llvm::MemoryBufferRef, uint64_t offset,
|
2021-01-19 23:44:42 +08:00
|
|
|
const llvm::MachO::relocation_info) const = 0;
|
|
|
|
virtual void relocateOne(uint8_t *loc, const Reloc &, uint64_t va,
|
2021-03-12 02:28:11 +08:00
|
|
|
uint64_t relocVA) const = 0;
|
2020-04-03 02:54:05 +08:00
|
|
|
|
[lld-macho] Support calls to functions in dylibs
Summary:
This diff implements lazy symbol binding -- very similar to the PLT
mechanism in ELF.
ELF's .plt section is broken up into two sections in Mach-O:
StubsSection and StubHelperSection. Calls to functions in dylibs will
end up calling into StubsSection, which contains indirect jumps to
addresses stored in the LazyPointerSection (the counterpart to ELF's
.plt.got).
Initially, the LazyPointerSection contains addresses that point into one
of the entry points in the middle of the StubHelperSection. The code in
StubHelperSection will push on the stack an offset into the
LazyBindingSection. The push is followed by a jump to the beginning of
the StubHelperSection (similar to PLT0), which then calls into
dyld_stub_binder. dyld_stub_binder is a non-lazily bound symbol, so this
call looks it up in the GOT.
The stub binder will look up the bind opcodes in the LazyBindingSection
at the given offset. The bind opcodes will tell the binder to update the
address in the LazyPointerSection to point to the symbol, so that
subsequent calls don't have to redo the symbol resolution. The binder
will then jump to the resolved symbol.
Depends on D78269.
Reviewers: ruiu, pcc, MaskRay, smeenai, alexshap, gkm, Ktwu, christylee
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78270
2020-05-06 08:38:10 +08:00
|
|
|
// Write code for lazy binding. See the comments on StubsSection for more
|
|
|
|
// details.
|
2020-08-28 06:54:42 +08:00
|
|
|
virtual void writeStub(uint8_t *buf, const Symbol &) const = 0;
|
[lld-macho] Support calls to functions in dylibs
Summary:
This diff implements lazy symbol binding -- very similar to the PLT
mechanism in ELF.
ELF's .plt section is broken up into two sections in Mach-O:
StubsSection and StubHelperSection. Calls to functions in dylibs will
end up calling into StubsSection, which contains indirect jumps to
addresses stored in the LazyPointerSection (the counterpart to ELF's
.plt.got).
Initially, the LazyPointerSection contains addresses that point into one
of the entry points in the middle of the StubHelperSection. The code in
StubHelperSection will push on the stack an offset into the
LazyBindingSection. The push is followed by a jump to the beginning of
the StubHelperSection (similar to PLT0), which then calls into
dyld_stub_binder. dyld_stub_binder is a non-lazily bound symbol, so this
call looks it up in the GOT.
The stub binder will look up the bind opcodes in the LazyBindingSection
at the given offset. The bind opcodes will tell the binder to update the
address in the LazyPointerSection to point to the symbol, so that
subsequent calls don't have to redo the symbol resolution. The binder
will then jump to the resolved symbol.
Depends on D78269.
Reviewers: ruiu, pcc, MaskRay, smeenai, alexshap, gkm, Ktwu, christylee
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78270
2020-05-06 08:38:10 +08:00
|
|
|
virtual void writeStubHelperHeader(uint8_t *buf) const = 0;
|
|
|
|
virtual void writeStubHelperEntry(uint8_t *buf, const DylibSymbol &,
|
|
|
|
uint64_t entryAddr) const = 0;
|
|
|
|
|
2020-06-14 11:00:06 +08:00
|
|
|
// Symbols may be referenced via either the GOT or the stubs section,
|
|
|
|
// depending on the relocation type. prepareSymbolRelocation() will set up the
|
2020-08-08 02:04:52 +08:00
|
|
|
// GOT/stubs entries, and resolveSymbolVA() will return the addresses of those
|
|
|
|
// entries. resolveSymbolVA() may also relax the target instructions to save
|
|
|
|
// on a level of address indirection.
|
2021-01-19 23:44:42 +08:00
|
|
|
virtual void relaxGotLoad(uint8_t *loc, uint8_t type) const = 0;
|
|
|
|
|
|
|
|
virtual const RelocAttrs &getRelocAttrs(uint8_t type) const = 0;
|
|
|
|
|
2020-09-27 04:00:22 +08:00
|
|
|
virtual uint64_t getPageSize() const = 0;
|
|
|
|
|
2021-03-30 08:33:48 +08:00
|
|
|
virtual void populateThunk(InputSection *thunk, Symbol *funcSym) {
|
|
|
|
llvm_unreachable("target does not use thunks");
|
|
|
|
}
|
|
|
|
|
2021-01-19 23:44:42 +08:00
|
|
|
bool hasAttr(uint8_t type, RelocAttrBits bit) const {
|
|
|
|
return getRelocAttrs(type).hasAttr(bit);
|
|
|
|
}
|
|
|
|
|
2021-03-30 08:33:48 +08:00
|
|
|
bool usesThunks() const { return thunkSize > 0; }
|
|
|
|
|
2021-05-04 06:31:23 +08:00
|
|
|
uint32_t magic;
|
2021-05-11 03:45:20 +08:00
|
|
|
llvm::MachO::CPUType cpuType;
|
2020-04-03 02:54:05 +08:00
|
|
|
uint32_t cpuSubtype;
|
[lld-macho] Support calls to functions in dylibs
Summary:
This diff implements lazy symbol binding -- very similar to the PLT
mechanism in ELF.
ELF's .plt section is broken up into two sections in Mach-O:
StubsSection and StubHelperSection. Calls to functions in dylibs will
end up calling into StubsSection, which contains indirect jumps to
addresses stored in the LazyPointerSection (the counterpart to ELF's
.plt.got).
Initially, the LazyPointerSection contains addresses that point into one
of the entry points in the middle of the StubHelperSection. The code in
StubHelperSection will push on the stack an offset into the
LazyBindingSection. The push is followed by a jump to the beginning of
the StubHelperSection (similar to PLT0), which then calls into
dyld_stub_binder. dyld_stub_binder is a non-lazily bound symbol, so this
call looks it up in the GOT.
The stub binder will look up the bind opcodes in the LazyBindingSection
at the given offset. The bind opcodes will tell the binder to update the
address in the LazyPointerSection to point to the symbol, so that
subsequent calls don't have to redo the symbol resolution. The binder
will then jump to the resolved symbol.
Depends on D78269.
Reviewers: ruiu, pcc, MaskRay, smeenai, alexshap, gkm, Ktwu, christylee
Subscribers: llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D78270
2020-05-06 08:38:10 +08:00
|
|
|
|
2021-04-03 23:10:45 +08:00
|
|
|
uint64_t pageZeroSize;
|
2021-05-04 06:31:23 +08:00
|
|
|
size_t headerSize;
|
2021-04-03 23:58:23 +08:00
|
|
|
size_t stubSize;
|
|
|
|
size_t stubHelperHeaderSize;
|
|
|
|
size_t stubHelperEntrySize;
|
|
|
|
size_t wordSize;
|
2021-03-30 08:33:48 +08:00
|
|
|
|
|
|
|
size_t thunkSize = 0;
|
2021-08-30 03:19:19 +08:00
|
|
|
uint64_t forwardBranchRange = 0;
|
|
|
|
uint64_t backwardBranchRange = 0;
|
2021-03-30 08:33:48 +08:00
|
|
|
|
|
|
|
// We contrive this value as sufficiently far from any valid address that it
|
|
|
|
// will always be out-of-range for any architecture. UINT64_MAX is not a
|
|
|
|
// good choice because it is (a) only 1 away from wrapping to 0, and (b) the
|
|
|
|
// tombstone value for DenseMap<> and caused weird assertions for me.
|
|
|
|
static constexpr uint64_t outOfRangeVA = 0xfull << 60;
|
2020-04-03 02:54:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TargetInfo *createX86_64TargetInfo();
|
2020-09-27 04:00:22 +08:00
|
|
|
TargetInfo *createARM64TargetInfo();
|
2021-04-16 09:14:32 +08:00
|
|
|
TargetInfo *createARM64_32TargetInfo();
|
2021-05-01 04:17:25 +08:00
|
|
|
TargetInfo *createARMTargetInfo(uint32_t cpuSubtype);
|
2020-04-03 02:54:05 +08:00
|
|
|
|
2021-04-03 06:46:18 +08:00
|
|
|
struct LP64 {
|
|
|
|
using mach_header = llvm::MachO::mach_header_64;
|
|
|
|
using nlist = structs::nlist_64;
|
|
|
|
using segment_command = llvm::MachO::segment_command_64;
|
|
|
|
using section = llvm::MachO::section_64;
|
2021-04-22 01:35:12 +08:00
|
|
|
using encryption_info_command = llvm::MachO::encryption_info_command_64;
|
2021-04-03 06:46:18 +08:00
|
|
|
|
|
|
|
static constexpr uint32_t magic = llvm::MachO::MH_MAGIC_64;
|
|
|
|
static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT_64;
|
2021-04-22 01:35:12 +08:00
|
|
|
static constexpr uint32_t encryptionInfoLCType =
|
|
|
|
llvm::MachO::LC_ENCRYPTION_INFO_64;
|
2021-04-03 06:46:18 +08:00
|
|
|
|
2021-04-03 23:10:45 +08:00
|
|
|
static constexpr uint64_t pageZeroSize = 1ull << 32;
|
2021-04-03 23:58:23 +08:00
|
|
|
static constexpr size_t wordSize = 8;
|
2021-04-03 06:46:18 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct ILP32 {
|
|
|
|
using mach_header = llvm::MachO::mach_header;
|
|
|
|
using nlist = structs::nlist;
|
|
|
|
using segment_command = llvm::MachO::segment_command;
|
|
|
|
using section = llvm::MachO::section;
|
2021-04-22 01:35:12 +08:00
|
|
|
using encryption_info_command = llvm::MachO::encryption_info_command;
|
2021-04-03 06:46:18 +08:00
|
|
|
|
|
|
|
static constexpr uint32_t magic = llvm::MachO::MH_MAGIC;
|
|
|
|
static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT;
|
2021-04-22 01:35:12 +08:00
|
|
|
static constexpr uint32_t encryptionInfoLCType =
|
|
|
|
llvm::MachO::LC_ENCRYPTION_INFO;
|
2021-04-03 06:46:18 +08:00
|
|
|
|
2021-04-03 23:10:45 +08:00
|
|
|
static constexpr uint64_t pageZeroSize = 1ull << 12;
|
2021-04-03 23:58:23 +08:00
|
|
|
static constexpr size_t wordSize = 4;
|
2021-04-03 06:46:18 +08:00
|
|
|
};
|
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
extern TargetInfo *target;
|
|
|
|
|
|
|
|
} // namespace macho
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|