Expected<Optional<Archive::Child>> Archive::findSym(StringRef name) const { Archive::symbol_iterator bs = symbol_begin(); Archive::symbol_iterator es = symbol_end(); for (; bs != es; ++bs) { StringRef SymName = bs->getName(); if (SymName == name) { if (auto MemberOrErr = bs->getMember()) return Child(*MemberOrErr); else return errorCodeToError(MemberOrErr.getError()); } } return Optional<Child>(); }
Archive::child_iterator Archive::findSym(StringRef name) const { Archive::symbol_iterator bs = symbol_begin(); Archive::symbol_iterator es = symbol_end(); for (; bs != es; ++bs) { StringRef SymName = bs->getName(); if (SymName == name) { ErrorOr<Archive::child_iterator> ResultOrErr = bs->getMember(); // FIXME: Should we really eat the error? if (ResultOrErr.getError()) return child_end(); return ResultOrErr.get(); } } return child_end(); }
Archive::child_iterator Archive::findSym(StringRef name) const { Archive::symbol_iterator bs = begin_symbols(); Archive::symbol_iterator es = end_symbols(); Archive::child_iterator result; StringRef symname; for (; bs != es; ++bs) { if (bs->getName(symname)) return end_children(); if (symname == name) { if (bs->getMember(result)) return end_children(); return result; } } return end_children(); }
static void dumpSymbolNamesFromFile(std::string &Filename) { ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr = MemoryBuffer::getFileOrSTDIN(Filename); if (error(BufferOrErr.getError(), Filename)) return; std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrErr.get()); LLVMContext &Context = getGlobalContext(); ErrorOr<Binary *> BinaryOrErr = createBinary(Buffer, &Context); if (error(BinaryOrErr.getError(), Filename)) return; Buffer.release(); std::unique_ptr<Binary> Bin(BinaryOrErr.get()); if (Archive *A = dyn_cast<Archive>(Bin.get())) { if (ArchiveMap) { Archive::symbol_iterator I = A->symbol_begin(); Archive::symbol_iterator E = A->symbol_end(); if (I != E) { outs() << "Archive map\n"; for (; I != E; ++I) { ErrorOr<Archive::child_iterator> C = I->getMember(); if (error(C.getError())) return; ErrorOr<StringRef> FileNameOrErr = C.get()->getName(); if (error(FileNameOrErr.getError())) return; StringRef SymName = I->getName(); outs() << SymName << " in " << FileNameOrErr.get() << "\n"; } outs() << "\n"; } } for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); I != E; ++I) { ErrorOr<std::unique_ptr<Binary>> ChildOrErr = I->getAsBinary(&Context); if (ChildOrErr.getError()) continue; if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) { if (!checkMachOAndArchFlags(O, Filename)) return; outs() << "\n"; if (isa<MachOObjectFile>(O)) { outs() << Filename << "(" << O->getFileName() << ")"; } else outs() << O->getFileName(); outs() << ":\n"; dumpSymbolNamesFromObject(O, false); } } return; } if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin.get())) { // If we have a list of architecture flags specified dump only those. if (!ArchAll && ArchFlags.size() != 0) { // Look for a slice in the universal binary that matches each ArchFlag. bool ArchFound; for (unsigned i = 0; i < ArchFlags.size(); ++i) { ArchFound = false; for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), E = UB->end_objects(); I != E; ++I) { if (ArchFlags[i] == I->getArchTypeName()) { ArchFound = true; ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile(); std::unique_ptr<Archive> A; if (ObjOrErr) { std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get()); if (ArchFlags.size() > 1) { outs() << "\n" << Obj->getFileName() << " (for architecture " << I->getArchTypeName() << ")" << ":\n"; } dumpSymbolNamesFromObject(Obj.get(), false); } else if (!I->getAsArchive(A)) { for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end(); AI != AE; ++AI) { ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary(&Context); if (ChildOrErr.getError()) continue; if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) { outs() << "\n" << A->getFileName(); outs() << "(" << O->getFileName() << ")"; if (ArchFlags.size() > 1) { outs() << " (for architecture " << I->getArchTypeName() << ")"; } outs() << ":\n"; dumpSymbolNamesFromObject(O, false); } } } } } if (!ArchFound) { error(ArchFlags[i], "file: " + Filename + " does not contain architecture"); return; } } return; } // No architecture flags were specified so if this contains a slice that // matches the host architecture dump only that. if (!ArchAll) { StringRef HostArchName = MachOObjectFile::getHostArch().getArchName(); for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), E = UB->end_objects(); I != E; ++I) { if (HostArchName == I->getArchTypeName()) { ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile(); std::unique_ptr<Archive> A; if (ObjOrErr) { std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get()); dumpSymbolNamesFromObject(Obj.get(), false); } else if (!I->getAsArchive(A)) { for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end(); AI != AE; ++AI) { ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary(&Context); if (ChildOrErr.getError()) continue; if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) { outs() << "\n" << A->getFileName() << "(" << O->getFileName() << ")" << ":\n"; dumpSymbolNamesFromObject(O, false); } } } return; } } } // Either all architectures have been specified or none have been specified // and this does not contain the host architecture so dump all the slices. bool moreThanOneArch = UB->getNumberOfObjects() > 1; for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), E = UB->end_objects(); I != E; ++I) { ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = I->getAsObjectFile(); std::unique_ptr<Archive> A; if (ObjOrErr) { std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get()); if (moreThanOneArch) outs() << "\n"; outs() << Obj->getFileName(); if (isa<MachOObjectFile>(Obj.get()) && moreThanOneArch) outs() << " (for architecture " << I->getArchTypeName() << ")"; outs() << ":\n"; dumpSymbolNamesFromObject(Obj.get(), false); } else if (!I->getAsArchive(A)) { for (Archive::child_iterator AI = A->child_begin(), AE = A->child_end(); AI != AE; ++AI) { ErrorOr<std::unique_ptr<Binary>> ChildOrErr = AI->getAsBinary(&Context); if (ChildOrErr.getError()) continue; if (SymbolicFile *O = dyn_cast<SymbolicFile>(&*ChildOrErr.get())) { outs() << "\n" << A->getFileName(); if (isa<MachOObjectFile>(O)) { outs() << "(" << O->getFileName() << ")"; if (moreThanOneArch) outs() << " (for architecture " << I->getArchTypeName() << ")"; } else outs() << ":" << O->getFileName(); outs() << ":\n"; dumpSymbolNamesFromObject(O, false); } } } } return; } if (SymbolicFile *O = dyn_cast<SymbolicFile>(Bin.get())) { if (!checkMachOAndArchFlags(O, Filename)) return; dumpSymbolNamesFromObject(O, true); return; } error("unrecognizable file type", Filename); return; }
static void dumpSymbolNamesFromFile(std::string &Filename) { std::unique_ptr<MemoryBuffer> Buffer; if (error(MemoryBuffer::getFileOrSTDIN(Filename, Buffer), Filename)) return; LLVMContext &Context = getGlobalContext(); ErrorOr<Binary *> BinaryOrErr = createBinary(Buffer.release(), &Context); if (error(BinaryOrErr.getError(), Filename)) return; std::unique_ptr<Binary> Bin(BinaryOrErr.get()); if (Archive *A = dyn_cast<Archive>(Bin.get())) { if (ArchiveMap) { Archive::symbol_iterator I = A->symbol_begin(); Archive::symbol_iterator E = A->symbol_end(); if (I != E) { outs() << "Archive map\n"; for (; I != E; ++I) { Archive::child_iterator C; StringRef SymName; StringRef FileName; if (error(I->getMember(C))) return; if (error(I->getName(SymName))) return; if (error(C->getName(FileName))) return; outs() << SymName << " in " << FileName << "\n"; } outs() << "\n"; } } for (Archive::child_iterator I = A->child_begin(), E = A->child_end(); I != E; ++I) { std::unique_ptr<Binary> Child; if (I->getAsBinary(Child, &Context)) continue; if (SymbolicFile *O = dyn_cast<SymbolicFile>(Child.get())) { outs() << O->getFileName() << ":\n"; dumpSymbolNamesFromObject(O); } } return; } if (MachOUniversalBinary *UB = dyn_cast<MachOUniversalBinary>(Bin.get())) { for (MachOUniversalBinary::object_iterator I = UB->begin_objects(), E = UB->end_objects(); I != E; ++I) { std::unique_ptr<ObjectFile> Obj; if (!I->getAsObjectFile(Obj)) { outs() << Obj->getFileName() << ":\n"; dumpSymbolNamesFromObject(Obj.get()); } } return; } if (SymbolicFile *O = dyn_cast<SymbolicFile>(Bin.get())) { dumpSymbolNamesFromObject(O); return; } error("unrecognizable file type", Filename); return; }