1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
//===- SyntheticSections.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_SYNTHETIC_SECTIONS_H
#define LLD_MACHO_SYNTHETIC_SECTIONS_H
#include "Config.h"
#include "ExportTrie.h"
#include "InputSection.h"
#include "OutputSection.h"
#include "Target.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/Support/raw_ostream.h"
namespace lld {
namespace macho {
namespace section_names {
constexpr const char pageZero[] = "__pagezero";
constexpr const char header[] = "__mach_header";
constexpr const char binding[] = "__binding";
constexpr const char lazyBinding[] = "__lazy_binding";
constexpr const char export_[] = "__export";
constexpr const char symbolTable[] = "__symbol_table";
constexpr const char stringTable[] = "__string_table";
constexpr const char got[] = "__got";
} // namespace section_names
class DylibSymbol;
class LoadCommand;
class SyntheticSection : public OutputSection {
public:
SyntheticSection(const char *segname, const char *name);
virtual ~SyntheticSection() = default;
static bool classof(const OutputSection *sec) {
return sec->kind() == SyntheticKind;
}
const StringRef segname;
};
// The header of the Mach-O file, which must have a file offset of zero.
class MachHeaderSection : public SyntheticSection {
public:
MachHeaderSection();
void addLoadCommand(LoadCommand *);
bool isHidden() const override { return true; }
uint64_t getSize() const override;
void writeTo(uint8_t *buf) const override;
private:
std::vector<LoadCommand *> loadCommands;
uint32_t sizeOfCmds = 0;
};
// A hidden section that exists solely for the purpose of creating the
// __PAGEZERO segment, which is used to catch null pointer dereferences.
class PageZeroSection : public SyntheticSection {
public:
PageZeroSection();
bool isHidden() const override { return true; }
uint64_t getSize() const override { return PageZeroSize; }
uint64_t getFileSize() const override { return 0; }
void writeTo(uint8_t *buf) const override {}
};
// This section will be populated by dyld with addresses to non-lazily-loaded
// dylib symbols.
class GotSection : public SyntheticSection {
public:
GotSection();
const llvm::SetVector<const Symbol *> &getEntries() const { return entries; }
bool isNeeded() const override { return !entries.empty(); }
uint64_t getSize() const override { return entries.size() * WordSize; }
void writeTo(uint8_t *buf) const override;
void addEntry(Symbol &sym);
private:
llvm::SetVector<const Symbol *> entries;
};
struct BindingEntry {
const DylibSymbol *dysym;
const InputSection *isec;
uint64_t offset;
int64_t addend;
BindingEntry(const DylibSymbol *dysym, const InputSection *isec,
uint64_t offset, int64_t addend)
: dysym(dysym), isec(isec), offset(offset), addend(addend) {}
};
// Stores bind opcodes for telling dyld which symbols to load non-lazily.
class BindingSection : public SyntheticSection {
public:
BindingSection();
void finalizeContents();
uint64_t getSize() const override { return contents.size(); }
// Like other sections in __LINKEDIT, the binding section is special: its
// offsets are recorded in the LC_DYLD_INFO_ONLY load command, instead of in
// section headers.
bool isHidden() const override { return true; }
bool isNeeded() const override;
void writeTo(uint8_t *buf) const override;
void addEntry(const DylibSymbol *dysym, const InputSection *isec,
uint64_t offset, int64_t addend) {
bindings.emplace_back(dysym, isec, offset, addend);
}
private:
std::vector<BindingEntry> bindings;
SmallVector<char, 128> contents;
};
// The following sections implement 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.
class StubsSection : public SyntheticSection {
public:
StubsSection();
uint64_t getSize() const override;
bool isNeeded() const override { return !entries.empty(); }
void writeTo(uint8_t *buf) const override;
const llvm::SetVector<DylibSymbol *> &getEntries() const { return entries; }
void addEntry(DylibSymbol &sym);
private:
llvm::SetVector<DylibSymbol *> entries;
};
class StubHelperSection : public SyntheticSection {
public:
StubHelperSection();
uint64_t getSize() const override;
bool isNeeded() const override;
void writeTo(uint8_t *buf) const override;
void setup();
DylibSymbol *stubBinder = nullptr;
};
// This section contains space for just a single word, and will be used by dyld
// to cache an address to the image loader it uses. Note that unlike the other
// synthetic sections, which are OutputSections, the ImageLoaderCacheSection is
// an InputSection that gets merged into the __data OutputSection.
class ImageLoaderCacheSection : public InputSection {
public:
ImageLoaderCacheSection();
uint64_t getSize() const override { return WordSize; }
};
class LazyPointerSection : public SyntheticSection {
public:
LazyPointerSection();
uint64_t getSize() const override;
bool isNeeded() const override;
void writeTo(uint8_t *buf) const override;
};
class LazyBindingSection : public SyntheticSection {
public:
LazyBindingSection();
void finalizeContents();
uint64_t getSize() const override { return contents.size(); }
uint32_t encode(const DylibSymbol &);
// Like other sections in __LINKEDIT, the lazy binding section is special: its
// offsets are recorded in the LC_DYLD_INFO_ONLY load command, instead of in
// section headers.
bool isHidden() const override { return true; }
bool isNeeded() const override;
void writeTo(uint8_t *buf) const override;
private:
SmallVector<char, 128> contents;
llvm::raw_svector_ostream os{contents};
};
// Stores a trie that describes the set of exported symbols.
class ExportSection : public SyntheticSection {
public:
ExportSection();
void finalizeContents();
uint64_t getSize() const override { return size; }
// Like other sections in __LINKEDIT, the export section is special: its
// offsets are recorded in the LC_DYLD_INFO_ONLY load command, instead of in
// section headers.
bool isHidden() const override { return true; }
void writeTo(uint8_t *buf) const override;
private:
TrieBuilder trieBuilder;
size_t size = 0;
};
// Stores the strings referenced by the symbol table.
class StringTableSection : public SyntheticSection {
public:
StringTableSection();
// Returns the start offset of the added string.
uint32_t addString(StringRef);
uint64_t getSize() const override { return size; }
// Like other sections in __LINKEDIT, the string table section is special: its
// offsets are recorded in the LC_SYMTAB load command, instead of in section
// headers.
bool isHidden() const override { return true; }
void writeTo(uint8_t *buf) const override;
private:
// An n_strx value of 0 always indicates the empty string, so we must locate
// our non-empty string values at positive offsets in the string table.
// Therefore we insert a dummy value at position zero.
std::vector<StringRef> strings{"\0"};
size_t size = 1;
};
struct SymtabEntry {
Symbol *sym;
size_t strx;
};
class SymtabSection : public SyntheticSection {
public:
SymtabSection(StringTableSection &);
void finalizeContents();
size_t getNumSymbols() const { return symbols.size(); }
uint64_t getSize() const override;
// Like other sections in __LINKEDIT, the symtab section is special: its
// offsets are recorded in the LC_SYMTAB load command, instead of in section
// headers.
bool isHidden() const override { return true; }
void writeTo(uint8_t *buf) const override;
private:
StringTableSection &stringTableSection;
std::vector<SymtabEntry> symbols;
};
struct InStruct {
BindingSection *binding = nullptr;
GotSection *got = nullptr;
LazyPointerSection *lazyPointers = nullptr;
StubsSection *stubs = nullptr;
StubHelperSection *stubHelper = nullptr;
ImageLoaderCacheSection *imageLoaderCache = nullptr;
};
extern InStruct in;
extern std::vector<SyntheticSection *> syntheticSections;
} // namespace macho
} // namespace lld
#endif
|