diff --git a/include/llvm/Object/MachO.h b/include/llvm/Object/MachO.h index 07c8fc5ca2b..8c2e268dae5 100644 --- a/include/llvm/Object/MachO.h +++ b/include/llvm/Object/MachO.h @@ -71,6 +71,13 @@ namespace MachOFormat { support::ulittle32_t Word1; }; + struct SymbolTableEntryBase { + support::ulittle32_t StringIndex; + uint8_t Type; + uint8_t SectionIndex; + support::ulittle16_t Flags; + }; + template struct SymbolTableEntry; @@ -263,12 +270,11 @@ private: void moveToNextSection(DataRefImpl &DRI) const; - const MachOFormat::SymbolTableEntry * - getSymbolTableEntry(DataRefImpl DRI, - const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const; + const MachOFormat::SymbolTableEntryBase * + getSymbolTableEntryBase(DataRefImpl DRI) const; - const MachOFormat::SymbolTableEntry * - getSymbol64TableEntry(DataRefImpl DRI, + const MachOFormat::SymbolTableEntryBase * + getSymbolTableEntryBase(DataRefImpl DRI, const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const; void moveToNextSymbol(DataRefImpl &DRI) const; diff --git a/lib/Object/MachOObjectFile.cpp b/lib/Object/MachOObjectFile.cpp index 3931c53ac83..ae30105a93c 100644 --- a/lib/Object/MachOObjectFile.cpp +++ b/lib/Object/MachOObjectFile.cpp @@ -116,47 +116,40 @@ void MachOObjectFile::moveToNextSymbol(DataRefImpl &DRI) const { } } -const MachOFormat::SymbolTableEntry * -MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const { +const MachOFormat::SymbolTableEntryBase * +MachOObjectFile::getSymbolTableEntryBase(DataRefImpl DRI) const { const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a); const MachOFormat::SymtabLoadCommand *SymtabLoadCmd = reinterpret_cast(Command); - - return getSymbolTableEntry(DRI, SymtabLoadCmd); + return getSymbolTableEntryBase(DRI, SymtabLoadCmd); } const MachOFormat::SymbolTableEntry * -MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI, +MachOObjectFile::getSymbolTableEntry(DataRefImpl DRI) const { + const MachOFormat::SymbolTableEntryBase *Base = getSymbolTableEntryBase(DRI); + return reinterpret_cast*>(Base); +} + +const MachOFormat::SymbolTableEntryBase * +MachOObjectFile::getSymbolTableEntryBase(DataRefImpl DRI, const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const { uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset; unsigned Index = DRI.d.b; - uint64_t Offset = (SymbolTableOffset + - Index * sizeof(MachOFormat::SymbolTableEntry)); - StringRef Data = - getData(Offset, sizeof(MachOFormat::SymbolTableEntry)); + + unsigned SymbolTableEntrySize = is64Bit() ? + sizeof(MachOFormat::SymbolTableEntry) : + sizeof(MachOFormat::SymbolTableEntry); + + uint64_t Offset = SymbolTableOffset + Index * SymbolTableEntrySize; + StringRef Data = getData(Offset, SymbolTableEntrySize); return - reinterpret_cast*>(Data.data()); + reinterpret_cast(Data.data()); } const MachOFormat::SymbolTableEntry* MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI) const { - const MachOFormat::LoadCommand *Command = getLoadCommandInfo(DRI.d.a); - const MachOFormat::SymtabLoadCommand *SymtabLoadCmd = - reinterpret_cast(Command); - - return getSymbol64TableEntry(DRI, SymtabLoadCmd); -} - -const MachOFormat::SymbolTableEntry* -MachOObjectFile::getSymbol64TableEntry(DataRefImpl DRI, - const MachOFormat::SymtabLoadCommand *SymtabLoadCmd) const { - uint64_t SymbolTableOffset = SymtabLoadCmd->SymbolTableOffset; - unsigned Index = DRI.d.b; - uint64_t Offset = (SymbolTableOffset + - Index * sizeof(MachOFormat::SymbolTableEntry)); - StringRef Data = getData(Offset, sizeof(MachOFormat::SymbolTableEntry)); - return - reinterpret_cast*>(Data.data()); + const MachOFormat::SymbolTableEntryBase *Base = getSymbolTableEntryBase(DRI); + return reinterpret_cast*>(Base); } error_code MachOObjectFile::getSymbolNext(DataRefImpl DRI, @@ -176,16 +169,9 @@ error_code MachOObjectFile::getSymbolName(DataRefImpl DRI, StringRef StringTable = getData(SymtabLoadCmd->StringTableOffset, SymtabLoadCmd->StringTableSize); - uint32_t StringIndex; - if (is64Bit()) { - const MachOFormat::SymbolTableEntry *Entry = - getSymbol64TableEntry(DRI, SymtabLoadCmd); - StringIndex = Entry->StringIndex; - } else { - const MachOFormat::SymbolTableEntry *Entry = - getSymbolTableEntry(DRI, SymtabLoadCmd); - StringIndex = Entry->StringIndex; - } + const MachOFormat::SymbolTableEntryBase *Entry = + getSymbolTableEntryBase(DRI, SymtabLoadCmd); + uint32_t StringIndex = Entry->StringIndex; const char *Start = &StringTable.data()[StringIndex]; Result = StringRef(Start); @@ -403,16 +389,10 @@ error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb, error_code MachOObjectFile::getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const { - uint8_t n_type; - if (is64Bit()) { - const MachOFormat::SymbolTableEntry *Entry = - getSymbol64TableEntry(Symb); - n_type = Entry->Type; - } else { - const MachOFormat::SymbolTableEntry *Entry = - getSymbolTableEntry(Symb); - n_type = Entry->Type; - } + const MachOFormat::SymbolTableEntryBase *Entry = + getSymbolTableEntryBase(Symb); + uint8_t n_type = Entry->Type; + Res = SymbolRef::ST_Other; // If this is a STAB debugging symbol, we can do nothing more.