Simplify the handling of iterators in ObjectFile.

None of the object file formats reported error on iterator increment. In
retrospect, that is not too surprising: no object format stores symbols or
sections in a linked list or other structure that requires chasing pointers.
As a consequence, all error checking can be done on begin() and end().

This reduces the text segment of bin/llvm-readobj in my machine from 521233 to
518526 bytes.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@200442 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola
2014-01-30 02:49:50 +00:00
parent 6bf3966f7f
commit efdbec8b0a
25 changed files with 147 additions and 365 deletions

View File

@ -275,18 +275,9 @@ static StringRef parseSegmentOrSectionName(const char *P) {
// Helper to advance a section or symbol iterator multiple increments at a time.
template<class T>
static error_code advance(T &it, size_t Val) {
error_code ec;
while (Val--) {
it.increment(ec);
}
return ec;
}
template<class T>
static void advanceTo(T &it, size_t Val) {
if (error_code ec = advance(it, Val))
report_fatal_error(ec.message());
static void advance(T &it, size_t Val) {
while (Val--)
++it;
}
static unsigned getCPUType(const MachOObjectFile *O) {
@ -305,11 +296,9 @@ static void printRelocationTargetName(const MachOObjectFile *O,
if (IsScattered) {
uint32_t Val = O->getPlainRelocationSymbolNum(RE);
error_code ec;
for (symbol_iterator SI = O->begin_symbols(), SE = O->end_symbols();
SI != SE; SI.increment(ec)) {
if (ec) report_fatal_error(ec.message());
SI != SE; ++SI) {
error_code ec;
uint64_t Addr;
StringRef Name;
@ -325,9 +314,8 @@ static void printRelocationTargetName(const MachOObjectFile *O,
// If we couldn't find a symbol that this relocation refers to, try
// to find a section beginning instead.
for (section_iterator SI = O->begin_sections(), SE = O->end_sections();
SI != SE; SI.increment(ec)) {
if (ec) report_fatal_error(ec.message());
SI != SE; ++SI) {
error_code ec;
uint64_t Addr;
StringRef Name;
@ -350,12 +338,12 @@ static void printRelocationTargetName(const MachOObjectFile *O,
if (isExtern) {
symbol_iterator SI = O->begin_symbols();
advanceTo(SI, Val);
advance(SI, Val);
SI->getName(S);
} else {
section_iterator SI = O->begin_sections();
// Adjust for the fact that sections are 1-indexed.
advanceTo(SI, Val - 1);
advance(SI, Val - 1);
SI->getName(S);
}
@ -454,14 +442,11 @@ MachOObjectFile::MachOObjectFile(MemoryBuffer *Object, bool IsLittleEndian,
}
}
error_code MachOObjectFile::getSymbolNext(DataRefImpl Symb,
SymbolRef &Res) const {
void MachOObjectFile::moveSymbolNext(DataRefImpl &Symb) const {
unsigned SymbolTableEntrySize = is64Bit() ?
sizeof(MachO::nlist_64) :
sizeof(MachO::nlist);
Symb.p += SymbolTableEntrySize;
Res = SymbolRef(Symb, this);
return object_error::success;
}
error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
@ -545,9 +530,7 @@ error_code MachOObjectFile::getSymbolSize(DataRefImpl DRI,
}
// Unfortunately symbols are unsorted so we need to touch all
// symbols from load command
error_code ec;
for (symbol_iterator I = begin_symbols(), E = end_symbols(); I != E;
I.increment(ec)) {
for (symbol_iterator I = begin_symbols(), E = end_symbols(); I != E; ++I) {
DataRefImpl DRI = I->getRawDataRefImpl();
Entry = getSymbolTableEntryBase(this, DRI);
getSymbolAddress(DRI, Value);
@ -648,11 +631,8 @@ error_code MachOObjectFile::getSymbolValue(DataRefImpl Symb,
report_fatal_error("getSymbolValue unimplemented in MachOObjectFile");
}
error_code MachOObjectFile::getSectionNext(DataRefImpl Sec,
SectionRef &Res) const {
void MachOObjectFile::moveSectionNext(DataRefImpl &Sec) const {
Sec.d.a++;
Res = SectionRef(Sec, this);
return object_error::success;
}
error_code
@ -834,13 +814,10 @@ MachOObjectFile::section_rel_end(DataRefImpl Sec) const {
return relocation_iterator(RelocationRef(Ret, this));
}
error_code MachOObjectFile::getRelocationNext(DataRefImpl Rel,
RelocationRef &Res) const {
void MachOObjectFile::moveRelocationNext(DataRefImpl &Rel) const {
const MachO::any_relocation_info *P =
reinterpret_cast<const MachO::any_relocation_info *>(Rel.p);
Rel.p = reinterpret_cast<uintptr_t>(P + 1);
Res = RelocationRef(Rel, this);
return object_error::success;
}
error_code