diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -589,13 +589,12 @@ fatal("must specify -arch"); PlatformKind platform = parsePlatformVersion(args); - config->target = - MachO::Target(MachO::getArchitectureFromName(archName), platform); + config->target = MachO::Target(getArchitectureFromName(archName), platform); - switch (MachO::getCPUTypeFromArchitecture(config->target.Arch).first) { - case MachO::CPU_TYPE_X86_64: + switch (getCPUTypeFromArchitecture(config->target.Arch).first) { + case CPU_TYPE_X86_64: return createX86_64TargetInfo(); - case MachO::CPU_TYPE_ARM64: + case CPU_TYPE_ARM64: return createARM64TargetInfo(); default: fatal("missing or unsupported -arch " + archName); diff --git a/lld/MachO/DriverUtils.cpp b/lld/MachO/DriverUtils.cpp --- a/lld/MachO/DriverUtils.cpp +++ b/lld/MachO/DriverUtils.cpp @@ -24,6 +24,7 @@ #include "llvm/Option/Option.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Path.h" +#include "llvm/TextAPI/MachO/InterfaceFile.h" #include "llvm/TextAPI/MachO/TextAPIReader.h" using namespace llvm; diff --git a/lld/MachO/InputFiles.h b/lld/MachO/InputFiles.h --- a/lld/MachO/InputFiles.h +++ b/lld/MachO/InputFiles.h @@ -19,7 +19,6 @@ #include "llvm/DebugInfo/DWARF/DWARFUnit.h" #include "llvm/Object/Archive.h" #include "llvm/Support/MemoryBuffer.h" -#include "llvm/TextAPI/MachO/InterfaceFile.h" #include "llvm/TextAPI/MachO/TextAPIReader.h" #include @@ -29,6 +28,9 @@ namespace lto { class InputFile; } // namespace lto +namespace MachO { +class InterfaceFile; +} // namespace MachO class TarWriter; } // namespace llvm @@ -78,8 +80,7 @@ InputFile(Kind kind, MemoryBufferRef mb) : mb(mb), id(idCount++), fileKind(kind), name(mb.getBufferIdentifier()) {} - InputFile(Kind kind, const llvm::MachO::InterfaceFile &interface) - : id(idCount++), fileKind(kind), name(saver.save(interface.getPath())) {} + InputFile(Kind, const llvm::MachO::InterfaceFile &); private: const Kind fileKind; diff --git a/lld/MachO/InputFiles.cpp b/lld/MachO/InputFiles.cpp --- a/lld/MachO/InputFiles.cpp +++ b/lld/MachO/InputFiles.cpp @@ -67,6 +67,7 @@ #include "llvm/Support/Path.h" #include "llvm/Support/TarWriter.h" #include "llvm/TextAPI/MachO/Architecture.h" +#include "llvm/TextAPI/MachO/InterfaceFile.h" using namespace llvm; using namespace llvm::MachO; @@ -111,9 +112,9 @@ // If this is a regular non-fat file, return it. const char *buf = mbref.getBufferStart(); - auto *hdr = reinterpret_cast(buf); + const auto *hdr = reinterpret_cast(buf); if (mbref.getBufferSize() < sizeof(uint32_t) || - read32be(&hdr->magic) != MachO::FAT_MAGIC) { + read32be(&hdr->magic) != FAT_MAGIC) { if (tar) tar->append(relativeToRoot(path), mbref.getBuffer()); return mbref; @@ -123,7 +124,7 @@ // multiple real files for different CPU ISAs. Here, we search for a // file that matches with the current link target and returns it as // a MemoryBufferRef. - auto *arch = reinterpret_cast(buf + sizeof(*hdr)); + const auto *arch = reinterpret_cast(buf + sizeof(*hdr)); for (uint32_t i = 0, n = read32be(&hdr->nfat_arch); i < n; ++i) { if (reinterpret_cast(arch + i + 1) > @@ -149,6 +150,9 @@ return None; } +InputFile::InputFile(Kind kind, const InterfaceFile &interface) + : id(idCount++), fileKind(kind), name(saver.save(interface.getPath())) {} + void ObjFile::parseSections(ArrayRef sections) { subsections.reserve(sections.size()); auto *buf = reinterpret_cast(mb.getBufferStart()); @@ -501,8 +505,7 @@ auto *buf = reinterpret_cast(mb.getBufferStart()); auto *hdr = reinterpret_cast(mb.getBufferStart()); - MachO::Architecture arch = - MachO::getArchitectureFromCpuType(hdr->cputype, hdr->cpusubtype); + Architecture arch = getArchitectureFromCpuType(hdr->cputype, hdr->cpusubtype); if (arch != config->target.Arch) { error(toString(this) + " has architecture " + getArchitectureName(arch) + " which is incompatible with target architecture " + diff --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp --- a/lld/MachO/InputSection.cpp +++ b/lld/MachO/InputSection.cpp @@ -89,11 +89,11 @@ } bool macho::isCodeSection(InputSection *isec) { - uint32_t type = isec->flags & MachO::SECTION_TYPE; + uint32_t type = isec->flags & SECTION_TYPE; if (type != S_REGULAR && type != S_COALESCED) return false; - uint32_t attr = isec->flags & MachO::SECTION_ATTRIBUTES_USR; + uint32_t attr = isec->flags & SECTION_ATTRIBUTES_USR; if (attr == S_ATTR_PURE_INSTRUCTIONS) return true; diff --git a/lld/MachO/MergedOutputSection.cpp b/lld/MachO/MergedOutputSection.cpp --- a/lld/MachO/MergedOutputSection.cpp +++ b/lld/MachO/MergedOutputSection.cpp @@ -54,19 +54,18 @@ // are actually merged. The logic presented here was written without // any form of informed research. void MergedOutputSection::mergeFlags(uint32_t inputFlags) { - uint8_t sectionFlag = MachO::SECTION_TYPE & inputFlags; - if (sectionFlag != (MachO::SECTION_TYPE & flags)) + uint8_t sectionFlag = SECTION_TYPE & inputFlags; + if (sectionFlag != (SECTION_TYPE & flags)) error("Cannot add merge section; inconsistent type flags " + Twine(sectionFlag)); - uint32_t inconsistentFlags = - MachO::S_ATTR_DEBUG | MachO::S_ATTR_STRIP_STATIC_SYMS | - MachO::S_ATTR_NO_DEAD_STRIP | MachO::S_ATTR_LIVE_SUPPORT; + uint32_t inconsistentFlags = S_ATTR_DEBUG | S_ATTR_STRIP_STATIC_SYMS | + S_ATTR_NO_DEAD_STRIP | S_ATTR_LIVE_SUPPORT; if ((inputFlags ^ flags) & inconsistentFlags) error("Cannot add merge section; cannot merge inconsistent flags"); // Negate pure instruction presence if any section isn't pure. - uint32_t pureMask = ~MachO::S_ATTR_PURE_INSTRUCTIONS | (inputFlags & flags); + uint32_t pureMask = ~S_ATTR_PURE_INSTRUCTIONS | (inputFlags & flags); // Merge the rest flags |= inputFlags; diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp --- a/lld/MachO/SyntheticSections.cpp +++ b/lld/MachO/SyntheticSections.cpp @@ -31,6 +31,7 @@ #endif using namespace llvm; +using namespace llvm::MachO; using namespace llvm::support; using namespace llvm::support::endian; using namespace lld; @@ -55,50 +56,50 @@ } 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(buf); - hdr->magic = MachO::MH_MAGIC_64; + auto *hdr = reinterpret_cast(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->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 (OutputSegment *seg : outputSegments) { for (OutputSection *osec : seg->getSections()) { if (isThreadLocalVariables(osec->flags)) { - hdr->flags |= MachO::MH_HAS_TLV_DESCRIPTORS; + hdr->flags |= MH_HAS_TLV_DESCRIPTORS; break; } } @@ -135,7 +136,6 @@ // 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(REBASE_OPCODE_DO_REBASE_IMM_TIMES | @@ -149,7 +149,6 @@ 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) { @@ -175,7 +174,6 @@ } void RebaseSection::finalizeContents() { - using namespace llvm::MachO; if (locations.empty()) return; @@ -209,7 +207,7 @@ 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) { @@ -249,7 +247,6 @@ 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) { @@ -284,12 +281,11 @@ // 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(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(BIND_OPCODE_SET_DYLIB_SPECIAL_IMM | (ordinal & BIND_IMMEDIATE_MASK)); @@ -303,7 +299,6 @@ static void encodeWeakOverride(const Defined *defined, raw_svector_ostream &os) { - using namespace llvm::MachO; os << static_cast(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM | BIND_SYMBOL_FLAGS_NON_WEAK_DEFINITION) << defined->getName() << '\0'; @@ -348,7 +343,7 @@ } } if (!bindings.empty()) - os << static_cast(MachO::BIND_OPCODE_DONE); + os << static_cast(BIND_OPCODE_DONE); } void BindingSection::writeTo(uint8_t *buf) const { @@ -382,7 +377,7 @@ } } if (!bindings.empty() || !definitions.empty()) - os << static_cast(MachO::BIND_OPCODE_DONE); + os << static_cast(BIND_OPCODE_DONE); } void WeakBindingSection::writeTo(uint8_t *buf) const { @@ -419,8 +414,7 @@ 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; @@ -448,7 +442,7 @@ 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 } @@ -496,7 +490,7 @@ 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 { @@ -554,20 +548,20 @@ uint32_t LazyBindingSection::encode(const DylibSymbol &sym) { uint32_t opstreamOffset = contents.size(); OutputSegment *dataSeg = in.lazyPointers->parent; - os << static_cast(MachO::BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | + os << static_cast(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(MachO::BIND_OPCODE_DO_BIND) - << static_cast(MachO::BIND_OPCODE_DONE); + << static_cast(BIND_OPCODE_DO_BIND) + << static_cast(BIND_OPCODE_DONE); return opstreamOffset; } @@ -621,7 +615,7 @@ } 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 @@ -634,13 +628,13 @@ } 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()); @@ -658,7 +652,7 @@ } 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(); @@ -709,11 +703,11 @@ 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)); } } @@ -800,41 +794,41 @@ // 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(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(ordinal)); + SET_LIBRARY_ORDINAL(n_desc, static_cast(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; @@ -868,7 +862,7 @@ 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 { @@ -955,7 +949,6 @@ } void CodeSignatureSection::writeTo(uint8_t *buf) const { - using namespace llvm::MachO; uint32_t signatureSize = static_cast(getSize()); auto *superBlob = reinterpret_cast(buf); write32be(&superBlob->magic, CSMAGIC_EMBEDDED_SIGNATURE); diff --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp --- a/lld/MachO/Writer.cpp +++ b/lld/MachO/Writer.cpp @@ -532,7 +532,7 @@ for (InputFile *file : inputFiles) { if (auto *dylibFile = dyn_cast(file)) { if (dylibFile->isBundleLoader) { - dylibFile->ordinal = MachO::BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE; + dylibFile->ordinal = BIND_SPECIAL_DYLIB_MAIN_EXECUTABLE; // Shortcut since bundle-loader does not re-export the symbols. dylibFile->reexport = false;