Refactor duplicated code. NFC.

llvm-svn: 240563
This commit is contained in:
Rafael Espindola 2015-06-24 18:14:41 +00:00
parent f549598796
commit 59128921f6
2 changed files with 21 additions and 27 deletions

View File

@ -197,6 +197,8 @@ public:
std::error_code &EC); std::error_code &EC);
void moveSymbolNext(DataRefImpl &Symb) const override; void moveSymbolNext(DataRefImpl &Symb) const override;
uint64_t getNValue(DataRefImpl Sym) const;
std::error_code getSymbolName(DataRefImpl Symb, std::error_code getSymbolName(DataRefImpl Symb,
StringRef &Res) const override; StringRef &Res) const override;

View File

@ -345,23 +345,24 @@ unsigned MachOObjectFile::getSectionType(SectionRef Sec) const {
return Flags & MachO::SECTION_TYPE; return Flags & MachO::SECTION_TYPE;
} }
uint64_t MachOObjectFile::getNValue(DataRefImpl Sym) const {
if (is64Bit()) {
MachO::nlist_64 Entry = getSymbol64TableEntry(Sym);
return Entry.n_value;
}
MachO::nlist Entry = getSymbolTableEntry(Sym);
return Entry.n_value;
}
// getIndirectName() returns the name of the alias'ed symbol who's string table // getIndirectName() returns the name of the alias'ed symbol who's string table
// index is in the n_value field. // index is in the n_value field.
std::error_code MachOObjectFile::getIndirectName(DataRefImpl Symb, std::error_code MachOObjectFile::getIndirectName(DataRefImpl Symb,
StringRef &Res) const { StringRef &Res) const {
StringRef StringTable = getStringTableData(); StringRef StringTable = getStringTableData();
uint64_t NValue; MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
if (is64Bit()) { if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR)
MachO::nlist_64 Entry = getSymbol64TableEntry(Symb); return object_error::parse_failed;
NValue = Entry.n_value; uint64_t NValue = getNValue(Symb);
if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR)
return object_error::parse_failed;
} else {
MachO::nlist Entry = getSymbolTableEntry(Symb);
NValue = Entry.n_value;
if ((Entry.n_type & MachO::N_TYPE) != MachO::N_INDR)
return object_error::parse_failed;
}
if (NValue >= StringTable.size()) if (NValue >= StringTable.size())
return object_error::parse_failed; return object_error::parse_failed;
const char *Start = &StringTable.data()[NValue]; const char *Start = &StringTable.data()[NValue];
@ -371,21 +372,12 @@ std::error_code MachOObjectFile::getIndirectName(DataRefImpl Symb,
std::error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb, std::error_code MachOObjectFile::getSymbolAddress(DataRefImpl Symb,
uint64_t &Res) const { uint64_t &Res) const {
if (is64Bit()) { uint64_t NValue = getNValue(Symb);
MachO::nlist_64 Entry = getSymbol64TableEntry(Symb); MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF && if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF && NValue == 0)
Entry.n_value == 0) Res = UnknownAddress;
Res = UnknownAddress; else
else Res = NValue;
Res = Entry.n_value;
} else {
MachO::nlist Entry = getSymbolTableEntry(Symb);
if ((Entry.n_type & MachO::N_TYPE) == MachO::N_UNDF &&
Entry.n_value == 0)
Res = UnknownAddress;
else
Res = Entry.n_value;
}
return std::error_code(); return std::error_code();
} }