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;
|
[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.
|
|
|
|
pageZeroSize = LP::pageZeroSize;
|
|
|
|
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-01-19 23:44:42 +08:00
|
|
|
bool hasAttr(uint8_t type, RelocAttrBits bit) const {
|
|
|
|
return getRelocAttrs(type).hasAttr(bit);
|
|
|
|
}
|
|
|
|
|
2020-04-03 02:54:05 +08:00
|
|
|
uint32_t cpuType;
|
|
|
|
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;
|
|
|
|
uint64_t stubSize;
|
|
|
|
uint64_t stubHelperHeaderSize;
|
|
|
|
uint64_t stubHelperEntrySize;
|
|
|
|
uint64_t wordSize;
|
2020-04-03 02:54:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
TargetInfo *createX86_64TargetInfo();
|
2020-09-27 04:00:22 +08:00
|
|
|
TargetInfo *createARM64TargetInfo();
|
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;
|
|
|
|
|
|
|
|
static constexpr uint32_t magic = llvm::MachO::MH_MAGIC_64;
|
|
|
|
static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT_64;
|
|
|
|
|
2021-04-03 23:10:45 +08:00
|
|
|
static constexpr uint64_t pageZeroSize = 1ull << 32;
|
|
|
|
static constexpr uint64_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;
|
|
|
|
|
|
|
|
static constexpr uint32_t magic = llvm::MachO::MH_MAGIC;
|
|
|
|
static constexpr uint32_t segmentLCType = llvm::MachO::LC_SEGMENT;
|
|
|
|
|
2021-04-03 23:10:45 +08:00
|
|
|
static constexpr uint64_t pageZeroSize = 1ull << 12;
|
|
|
|
static constexpr uint64_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
|