#endif
using namespace llvm;
+using namespace llvm::MachO;
using namespace llvm::support;
using namespace llvm::support::endian;
using namespace lld;
}
uint64_t MachHeaderSection::getSize() const {
- return sizeof(MachO::mach_header_64) + sizeOfCmds + config->headerPad;
+ return sizeof(mach_header_64) + sizeOfCmds + config->headerPad;
}
static uint32_t cpuSubtype() {
uint32_t subtype = target->cpuSubtype;
- if (config->outputType == MachO::MH_EXECUTE && !config->staticLink &&
- target->cpuSubtype == MachO::CPU_SUBTYPE_X86_64_ALL &&
- config->target.Platform == MachO::PlatformKind::macOS &&
+ if (config->outputType == MH_EXECUTE && !config->staticLink &&
+ target->cpuSubtype == CPU_SUBTYPE_X86_64_ALL &&
+ config->target.Platform == PlatformKind::macOS &&
config->platformInfo.minimum >= VersionTuple(10, 5))
- subtype |= MachO::CPU_SUBTYPE_LIB64;
+ subtype |= CPU_SUBTYPE_LIB64;
return subtype;
}
void MachHeaderSection::writeTo(uint8_t *buf) const {
- auto *hdr = reinterpret_cast<MachO::mach_header_64 *>(buf);
- hdr->magic = MachO::MH_MAGIC_64;
+ auto *hdr = reinterpret_cast<mach_header_64 *>(buf);
+ hdr->magic = MH_MAGIC_64;
hdr->cputype = target->cpuType;
hdr->cpusubtype = cpuSubtype();
hdr->filetype = config->outputType;
hdr->ncmds = loadCommands.size();
hdr->sizeofcmds = sizeOfCmds;
- hdr->flags = MachO::MH_DYLDLINK;
+ hdr->flags = MH_DYLDLINK;
if (config->namespaceKind == NamespaceKind::twolevel)
- hdr->flags |= MachO::MH_NOUNDEFS | MachO::MH_TWOLEVEL;
+ hdr->flags |= MH_NOUNDEFS | MH_TWOLEVEL;
- if (config->outputType == MachO::MH_DYLIB && !config->hasReexports)
- hdr->flags |= MachO::MH_NO_REEXPORTED_DYLIBS;
+ if (config->outputType == MH_DYLIB && !config->hasReexports)
+ hdr->flags |= MH_NO_REEXPORTED_DYLIBS;
if (config->markDeadStrippableDylib)
- hdr->flags |= MachO::MH_DEAD_STRIPPABLE_DYLIB;
+ hdr->flags |= MH_DEAD_STRIPPABLE_DYLIB;
- if (config->outputType == MachO::MH_EXECUTE && config->isPic)
- hdr->flags |= MachO::MH_PIE;
+ if (config->outputType == MH_EXECUTE && config->isPic)
+ hdr->flags |= MH_PIE;
if (in.exports->hasWeakSymbol || in.weakBinding->hasNonWeakDefinition())
- hdr->flags |= MachO::MH_WEAK_DEFINES;
+ hdr->flags |= MH_WEAK_DEFINES;
if (in.exports->hasWeakSymbol || in.weakBinding->hasEntry())
- hdr->flags |= MachO::MH_BINDS_TO_WEAK;
+ hdr->flags |= MH_BINDS_TO_WEAK;
for (const OutputSegment *seg : outputSegments) {
for (const OutputSection *osec : seg->getSections()) {
if (isThreadLocalVariables(osec->flags)) {
- hdr->flags |= MachO::MH_HAS_TLV_DESCRIPTORS;
+ hdr->flags |= MH_HAS_TLV_DESCRIPTORS;
break;
}
}
// using a single DO_REBASE opcode. To take advantage of it, we delay emitting
// `DO_REBASE` until we have reached the end of a contiguous sequence.
static void encodeDoRebase(Rebase &rebase, raw_svector_ostream &os) {
- using namespace llvm::MachO;
assert(rebase.consecutiveCount != 0);
if (rebase.consecutiveCount <= REBASE_IMMEDIATE_MASK) {
os << static_cast<uint8_t>(REBASE_OPCODE_DO_REBASE_IMM_TIMES |
static void encodeRebase(const OutputSection *osec, uint64_t outSecOff,
Rebase &lastRebase, raw_svector_ostream &os) {
- using namespace llvm::MachO;
OutputSegment *seg = osec->parent;
uint64_t offset = osec->getSegmentOffset() + outSecOff;
if (lastRebase.segment != seg || lastRebase.offset != offset) {
}
void RebaseSection::finalizeContents() {
- using namespace llvm::MachO;
if (locations.empty())
return;
const char *name)
: SyntheticSection(segname, name) {
align = WordSize;
- flags = MachO::S_NON_LAZY_SYMBOL_POINTERS;
+ flags = S_NON_LAZY_SYMBOL_POINTERS;
}
void NonLazyPointerSectionBase::addEntry(Symbol *sym) {
uint64_t outSecOff, int64_t addend,
bool isWeakBinding, Binding &lastBinding,
raw_svector_ostream &os) {
- using namespace llvm::MachO;
OutputSegment *seg = osec->parent;
uint64_t offset = osec->getSegmentOffset() + outSecOff;
if (lastBinding.segment != seg) {
// Non-weak bindings need to have their dylib ordinal encoded as well.
static int16_t ordinalForDylibSymbol(const DylibSymbol &dysym) {
return config->namespaceKind == NamespaceKind::flat || dysym.isDynamicLookup()
- ? (int16_t)MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP
+ ? static_cast<int16_t>(BIND_SPECIAL_DYLIB_FLAT_LOOKUP)
: dysym.getFile()->ordinal;
}
static void encodeDylibOrdinal(int16_t ordinal, raw_svector_ostream &os) {
- using namespace llvm::MachO;
if (ordinal <= 0) {
os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM |
(ordinal & BIND_IMMEDIATE_MASK));
static void encodeWeakOverride(const Defined *defined,
raw_svector_ostream &os) {
- using namespace llvm::MachO;
os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM |
BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION)
<< defined->getName() << '\0';
}
}
if (!bindings.empty())
- os << static_cast<uint8_t>(MachO::BIND_OPCODE_DONE);
+ os << static_cast<uint8_t>(BIND_OPCODE_DONE);
}
void BindingSection::writeTo(uint8_t *buf) const {
}
}
if (!bindings.empty() || !definitions.empty())
- os << static_cast<uint8_t>(MachO::BIND_OPCODE_DONE);
+ os << static_cast<uint8_t>(BIND_OPCODE_DONE);
}
void WeakBindingSection::writeTo(uint8_t *buf) const {
StubsSection::StubsSection()
: SyntheticSection(segment_names::text, "__stubs") {
- flags = MachO::S_SYMBOL_STUBS | MachO::S_ATTR_SOME_INSTRUCTIONS |
- MachO::S_ATTR_PURE_INSTRUCTIONS;
+ flags = S_SYMBOL_STUBS | S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
// The stubs section comprises machine instructions, which are aligned to
// 4 bytes on the archs we care about.
align = 4;
StubHelperSection::StubHelperSection()
: SyntheticSection(segment_names::text, "__stub_helper") {
- flags = MachO::S_ATTR_SOME_INSTRUCTIONS | MachO::S_ATTR_PURE_INSTRUCTIONS;
+ flags = S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS;
align = 4; // This section comprises machine instructions
}
LazyPointerSection::LazyPointerSection()
: SyntheticSection(segment_names::data, "__la_symbol_ptr") {
align = WordSize;
- flags = MachO::S_LAZY_SYMBOL_POINTERS;
+ flags = S_LAZY_SYMBOL_POINTERS;
}
uint64_t LazyPointerSection::getSize() const {
uint32_t LazyBindingSection::encode(const DylibSymbol &sym) {
uint32_t opstreamOffset = contents.size();
OutputSegment *dataSeg = in.lazyPointers->parent;
- os << static_cast<uint8_t>(MachO::BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
+ os << static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
dataSeg->index);
uint64_t offset = in.lazyPointers->addr - dataSeg->firstSection()->addr +
sym.stubsIndex * WordSize;
encodeULEB128(offset, os);
encodeDylibOrdinal(ordinalForDylibSymbol(sym), os);
- uint8_t flags = MachO::BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM;
+ uint8_t flags = BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM;
if (sym.isWeakRef())
- flags |= MachO::BIND_SYMBOL_FLAGS_WEAK_IMPORT;
+ flags |= BIND_SYMBOL_FLAGS_WEAK_IMPORT;
os << flags << sym.getName() << '\0'
- << static_cast<uint8_t>(MachO::BIND_OPCODE_DO_BIND)
- << static_cast<uint8_t>(MachO::BIND_OPCODE_DONE);
+ << static_cast<uint8_t>(BIND_OPCODE_DO_BIND)
+ << static_cast<uint8_t>(BIND_OPCODE_DONE);
return opstreamOffset;
}
}
void SymtabSection::emitBeginSourceStab(DWARFUnit *compileUnit) {
- StabsEntry stab(MachO::N_SO);
+ StabsEntry stab(N_SO);
SmallString<261> dir(compileUnit->getCompilationDir());
StringRef sep = sys::path::get_separator();
// We don't use `path::append` here because we want an empty `dir` to result
}
void SymtabSection::emitEndSourceStab() {
- StabsEntry stab(MachO::N_SO);
+ StabsEntry stab(N_SO);
stab.sect = 1;
stabs.emplace_back(std::move(stab));
}
void SymtabSection::emitObjectFileStab(ObjFile *file) {
- StabsEntry stab(MachO::N_OSO);
+ StabsEntry stab(N_OSO);
stab.sect = target->cpuSubtype;
SmallString<261> path(!file->archiveName.empty() ? file->archiveName
: file->getName());
}
void SymtabSection::emitEndFunStab(Defined *defined) {
- StabsEntry stab(MachO::N_FUN);
+ StabsEntry stab(N_FUN);
// FIXME this should be the size of the symbol. Using the section size in
// lieu is only correct if .subsections_via_symbols is set.
stab.value = defined->isec->getSize();
symStab.value = defined->getVA();
if (isCodeSection(isec)) {
- symStab.type = MachO::N_FUN;
+ symStab.type = N_FUN;
stabs.emplace_back(std::move(symStab));
emitEndFunStab(defined);
} else {
- symStab.type = defined->isExternal() ? MachO::N_GSYM : MachO::N_STSYM;
+ symStab.type = defined->isExternal() ? N_GSYM : N_STSYM;
stabs.emplace_back(std::move(symStab));
}
}
// Private external -- dylib scoped symbol.
// Promote to non-external at link time.
assert(defined->isExternal() && "invalid input file");
- scope = MachO::N_PEXT;
+ scope = N_PEXT;
} else if (defined->isExternal()) {
// Normal global symbol.
- scope = MachO::N_EXT;
+ scope = N_EXT;
} else {
// TU-local symbol from localSymbols.
scope = 0;
}
if (defined->isAbsolute()) {
- nList->n_type = scope | MachO::N_ABS;
- nList->n_sect = MachO::NO_SECT;
+ nList->n_type = scope | N_ABS;
+ nList->n_sect = NO_SECT;
nList->n_value = defined->value;
} else {
- nList->n_type = scope | MachO::N_SECT;
+ nList->n_type = scope | N_SECT;
nList->n_sect = defined->isec->parent->index;
// For the N_SECT symbol type, n_value is the address of the symbol
nList->n_value = defined->getVA();
}
- nList->n_desc |= defined->isExternalWeakDef() ? MachO::N_WEAK_DEF : 0;
+ nList->n_desc |= defined->isExternalWeakDef() ? N_WEAK_DEF : 0;
} else if (auto *dysym = dyn_cast<DylibSymbol>(entry.sym)) {
uint16_t n_desc = nList->n_desc;
int16_t ordinal = ordinalForDylibSymbol(*dysym);
- if (ordinal == MachO::BIND_SPECIAL_DYLIB_FLAT_LOOKUP)
- MachO::SET_LIBRARY_ORDINAL(n_desc, MachO::DYNAMIC_LOOKUP_ORDINAL);
- else if (ordinal == MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE)
- MachO::SET_LIBRARY_ORDINAL(n_desc, MachO::EXECUTABLE_ORDINAL);
+ if (ordinal == BIND_SPECIAL_DYLIB_FLAT_LOOKUP)
+ SET_LIBRARY_ORDINAL(n_desc, DYNAMIC_LOOKUP_ORDINAL);
+ else if (ordinal == BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE)
+ SET_LIBRARY_ORDINAL(n_desc, EXECUTABLE_ORDINAL);
else {
assert(ordinal > 0);
- MachO::SET_LIBRARY_ORDINAL(n_desc, static_cast<uint8_t>(ordinal));
+ SET_LIBRARY_ORDINAL(n_desc, static_cast<uint8_t>(ordinal));
}
- nList->n_type = MachO::N_EXT;
- n_desc |= dysym->isWeakDef() ? MachO::N_WEAK_DEF : 0;
- n_desc |= dysym->isWeakRef() ? MachO::N_WEAK_REF : 0;
+ nList->n_type = N_EXT;
+ n_desc |= dysym->isWeakDef() ? N_WEAK_DEF : 0;
+ n_desc |= dysym->isWeakRef() ? N_WEAK_REF : 0;
nList->n_desc = n_desc;
}
++nList;
static uint32_t indirectValue(const Symbol *sym) {
return sym->symtabIndex != UINT32_MAX ? sym->symtabIndex
- : MachO::INDIRECT_SYMBOL_LOCAL;
+ : INDIRECT_SYMBOL_LOCAL;
}
void IndirectSymtabSection::writeTo(uint8_t *buf) const {
}
void CodeSignatureSection::writeTo(uint8_t *buf) const {
- using namespace llvm::MachO;
uint32_t signatureSize = static_cast<uint32_t>(getSize());
auto *superBlob = reinterpret_cast<CS_SuperBlob *>(buf);
write32be(&superBlob->magic, CSMAGIC_EMBEDDED_SIGNATURE);