Commit 40bc9953 authored by Fangrui Song's avatar Fangrui Song Committed by Fangrui Song
Browse files

[lld-macho] Remove using namespace llvm::MachO

llvm/include/llvm/TextAPI/MachO/ inappropriately uses the llvm::MachO namespace (this is for BinaryFormat and Object) and causes conflicts in some MSVC builds::

http://lab.llvm.org:8011/builders/sanitizer-windows/builds/65324/steps/stage%201%20build/logs/stdio

Removing `using namespace llvm::MachO` should decrease name collisions.
parent 81b2d1d1
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#include "llvm/Support/LEB128.h" #include "llvm/Support/LEB128.h"
using namespace llvm; using namespace llvm;
using namespace llvm::MachO;
using namespace llvm::support; using namespace llvm::support;
using namespace llvm::support::endian; using namespace llvm::support::endian;
using namespace lld; using namespace lld;
...@@ -47,20 +46,20 @@ void MachHeaderSection::addLoadCommand(LoadCommand *lc) { ...@@ -47,20 +46,20 @@ void MachHeaderSection::addLoadCommand(LoadCommand *lc) {
} }
uint64_t MachHeaderSection::getSize() const { uint64_t MachHeaderSection::getSize() const {
return sizeof(mach_header_64) + sizeOfCmds; return sizeof(MachO::mach_header_64) + sizeOfCmds;
} }
void MachHeaderSection::writeTo(uint8_t *buf) const { void MachHeaderSection::writeTo(uint8_t *buf) const {
auto *hdr = reinterpret_cast<mach_header_64 *>(buf); auto *hdr = reinterpret_cast<MachO::mach_header_64 *>(buf);
hdr->magic = MH_MAGIC_64; hdr->magic = MachO::MH_MAGIC_64;
hdr->cputype = CPU_TYPE_X86_64; hdr->cputype = MachO::CPU_TYPE_X86_64;
hdr->cpusubtype = CPU_SUBTYPE_X86_64_ALL | CPU_SUBTYPE_LIB64; hdr->cpusubtype = MachO::CPU_SUBTYPE_X86_64_ALL | MachO::CPU_SUBTYPE_LIB64;
hdr->filetype = config->outputType; hdr->filetype = config->outputType;
hdr->ncmds = loadCommands.size(); hdr->ncmds = loadCommands.size();
hdr->sizeofcmds = sizeOfCmds; hdr->sizeofcmds = sizeOfCmds;
hdr->flags = MH_NOUNDEFS | MH_DYLDLINK | MH_TWOLEVEL; hdr->flags = MachO::MH_NOUNDEFS | MachO::MH_DYLDLINK | MachO::MH_TWOLEVEL;
if (config->outputType == MH_DYLIB && !config->hasReexports) if (config->outputType == MachO::MH_DYLIB && !config->hasReexports)
hdr->flags |= MH_NO_REEXPORTED_DYLIBS; hdr->flags |= MachO::MH_NO_REEXPORTED_DYLIBS;
uint8_t *p = reinterpret_cast<uint8_t *>(hdr + 1); uint8_t *p = reinterpret_cast<uint8_t *>(hdr + 1);
for (LoadCommand *lc : loadCommands) { for (LoadCommand *lc : loadCommands) {
...@@ -75,7 +74,7 @@ PageZeroSection::PageZeroSection() ...@@ -75,7 +74,7 @@ PageZeroSection::PageZeroSection()
GotSection::GotSection() GotSection::GotSection()
: SyntheticSection(segment_names::dataConst, section_names::got) { : SyntheticSection(segment_names::dataConst, section_names::got) {
align = 8; align = 8;
flags = S_NON_LAZY_SYMBOL_POINTERS; flags = MachO::S_NON_LAZY_SYMBOL_POINTERS;
// TODO: section_64::reserved1 should be an index into the indirect symbol // TODO: section_64::reserved1 should be an index into the indirect symbol
// table, which we do not currently emit // table, which we do not currently emit
...@@ -116,38 +115,40 @@ void BindingSection::finalizeContents() { ...@@ -116,38 +115,40 @@ void BindingSection::finalizeContents() {
return; return;
raw_svector_ostream os{contents}; raw_svector_ostream os{contents};
os << static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | os << static_cast<uint8_t>(MachO::BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
in.got->parent->index); in.got->parent->index);
encodeULEB128(in.got->getSegmentOffset(), os); encodeULEB128(in.got->getSegmentOffset(), os);
uint32_t entries_to_skip = 0; uint32_t entries_to_skip = 0;
for (const Symbol *sym : in.got->getEntries()) { for (const Symbol *sym : in.got->getEntries()) {
if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) { if (const auto *dysym = dyn_cast<DylibSymbol>(sym)) {
if (entries_to_skip != 0) { if (entries_to_skip != 0) {
os << static_cast<uint8_t>(BIND_OPCODE_ADD_ADDR_ULEB); os << static_cast<uint8_t>(MachO::BIND_OPCODE_ADD_ADDR_ULEB);
encodeULEB128(WordSize * entries_to_skip, os); encodeULEB128(WordSize * entries_to_skip, os);
entries_to_skip = 0; entries_to_skip = 0;
} }
// TODO: Implement compact encoding -- we only need to encode the // TODO: Implement compact encoding -- we only need to encode the
// differences between consecutive symbol entries. // differences between consecutive symbol entries.
if (dysym->file->ordinal <= BIND_IMMEDIATE_MASK) { if (dysym->file->ordinal <= MachO::BIND_IMMEDIATE_MASK) {
os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | os << static_cast<uint8_t>(MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_IMM |
dysym->file->ordinal); dysym->file->ordinal);
} else { } else {
error("TODO: Support larger dylib symbol ordinals"); error("TODO: Support larger dylib symbol ordinals");
continue; continue;
} }
os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM) os << static_cast<uint8_t>(
MachO::BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM)
<< dysym->getName() << '\0' << dysym->getName() << '\0'
<< static_cast<uint8_t>(BIND_OPCODE_SET_TYPE_IMM | BIND_TYPE_POINTER) << static_cast<uint8_t>(MachO::BIND_OPCODE_SET_TYPE_IMM |
<< static_cast<uint8_t>(BIND_OPCODE_DO_BIND); MachO::BIND_TYPE_POINTER)
<< static_cast<uint8_t>(MachO::BIND_OPCODE_DO_BIND);
} else { } else {
// We have a defined symbol with a pre-populated address; skip over it. // We have a defined symbol with a pre-populated address; skip over it.
++entries_to_skip; ++entries_to_skip;
} }
} }
os << static_cast<uint8_t>(BIND_OPCODE_DONE); os << static_cast<uint8_t>(MachO::BIND_OPCODE_DONE);
} }
void BindingSection::writeTo(uint8_t *buf) const { void BindingSection::writeTo(uint8_t *buf) const {
...@@ -219,7 +220,7 @@ ImageLoaderCacheSection::ImageLoaderCacheSection() { ...@@ -219,7 +220,7 @@ ImageLoaderCacheSection::ImageLoaderCacheSection() {
LazyPointerSection::LazyPointerSection() LazyPointerSection::LazyPointerSection()
: SyntheticSection(segment_names::data, "__la_symbol_ptr") { : SyntheticSection(segment_names::data, "__la_symbol_ptr") {
align = 8; align = 8;
flags = S_LAZY_SYMBOL_POINTERS; flags = MachO::S_LAZY_SYMBOL_POINTERS;
} }
uint64_t LazyPointerSection::getSize() const { uint64_t LazyPointerSection::getSize() const {
...@@ -265,20 +266,21 @@ void LazyBindingSection::writeTo(uint8_t *buf) const { ...@@ -265,20 +266,21 @@ void LazyBindingSection::writeTo(uint8_t *buf) const {
uint32_t LazyBindingSection::encode(const DylibSymbol &sym) { uint32_t LazyBindingSection::encode(const DylibSymbol &sym) {
uint32_t opstreamOffset = contents.size(); uint32_t opstreamOffset = contents.size();
OutputSegment *dataSeg = in.lazyPointers->parent; OutputSegment *dataSeg = in.lazyPointers->parent;
os << static_cast<uint8_t>(BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB | os << static_cast<uint8_t>(MachO::BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB |
dataSeg->index); dataSeg->index);
uint64_t offset = in.lazyPointers->addr - dataSeg->firstSection()->addr + uint64_t offset = in.lazyPointers->addr - dataSeg->firstSection()->addr +
sym.stubsIndex * WordSize; sym.stubsIndex * WordSize;
encodeULEB128(offset, os); encodeULEB128(offset, os);
if (sym.file->ordinal <= BIND_IMMEDIATE_MASK) if (sym.file->ordinal <= MachO::BIND_IMMEDIATE_MASK)
os << static_cast<uint8_t>(BIND_OPCODE_SET_DYLIB_ORDINAL_IMM | os << static_cast<uint8_t>(MachO::BIND_OPCODE_SET_DYLIB_ORDINAL_IMM |
sym.file->ordinal); sym.file->ordinal);
else else
fatal("TODO: Support larger dylib symbol ordinals"); fatal("TODO: Support larger dylib symbol ordinals");
os << static_cast<uint8_t>(BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM) os << static_cast<uint8_t>(MachO::BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM)
<< sym.getName() << '\0' << static_cast<uint8_t>(BIND_OPCODE_DO_BIND) << sym.getName() << '\0'
<< static_cast<uint8_t>(BIND_OPCODE_DONE); << static_cast<uint8_t>(MachO::BIND_OPCODE_DO_BIND)
<< static_cast<uint8_t>(MachO::BIND_OPCODE_DONE);
return opstreamOffset; return opstreamOffset;
} }
...@@ -321,7 +323,7 @@ void SymtabSection::writeTo(uint8_t *buf) const { ...@@ -321,7 +323,7 @@ void SymtabSection::writeTo(uint8_t *buf) const {
// TODO support other symbol types // TODO support other symbol types
// TODO populate n_desc // TODO populate n_desc
if (auto *defined = dyn_cast<Defined>(entry.sym)) { if (auto *defined = dyn_cast<Defined>(entry.sym)) {
nList->n_type = N_EXT | N_SECT; nList->n_type = MachO::N_EXT | MachO::N_SECT;
nList->n_sect = defined->isec->parent->index; nList->n_sect = defined->isec->parent->index;
// For the N_SECT symbol type, n_value is the address of the symbol // For the N_SECT symbol type, n_value is the address of the symbol
nList->n_value = defined->value + defined->isec->getVA(); nList->n_value = defined->value + defined->isec->getVA();
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment