-// FIXME: Move this to llvm-symbolizer tool.
-static bool error(std::error_code ec) {
- if (!ec)
- return false;
- errs() << "LLVMSymbolizer: error reading file: " << ec.message() << ".\n";
- return true;
-}
-
-static DILineInfoSpecifier
-getDILineInfoSpecifier(const LLVMSymbolizer::Options &Opts) {
- return DILineInfoSpecifier(
- DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath,
- Opts.PrintFunctions);
-}
-
-ModuleInfo::ModuleInfo(ObjectFile *Obj, std::unique_ptr<DIContext> DICtx)
- : Module(Obj), DebugInfoContext(std::move(DICtx)) {
- std::unique_ptr<DataExtractor> OpdExtractor;
- uint64_t OpdAddress = 0;
- // Find the .opd (function descriptor) section if any, for big-endian
- // PowerPC64 ELF.
- if (Module->getArch() == Triple::ppc64) {
- for (section_iterator Section : Module->sections()) {
- StringRef Name;
- if (!error(Section->getName(Name)) && Name == ".opd") {
- StringRef Data;
- if (!error(Section->getContents(Data))) {
- OpdExtractor.reset(new DataExtractor(Data, Module->isLittleEndian(),
- Module->getBytesInAddress()));
- OpdAddress = Section->getAddress();
- }
- break;
- }
- }
- }
- std::vector<std::pair<SymbolRef, uint64_t>> Symbols =
- computeSymbolSizes(*Module);
- for (auto &P : Symbols)
- addSymbol(P.first, P.second, OpdExtractor.get(), OpdAddress);
-
- // If this is a COFF object and we didn't find any symbols, try the export
- // table.
- if (Symbols.empty()) {
- if (auto *CoffObj = dyn_cast<COFFObjectFile>(Obj))
- addCoffExportSymbols(CoffObj);
- }
-}
-
-namespace {
-struct OffsetNamePair {
- uint32_t Offset;
- StringRef Name;
- bool operator<(const OffsetNamePair &R) const {
- return Offset < R.Offset;
- }
-};
-}
-
-void ModuleInfo::addCoffExportSymbols(const COFFObjectFile *CoffObj) {
- // Get all export names and offsets.
- std::vector<OffsetNamePair> ExportSyms;
- for (const ExportDirectoryEntryRef &Ref : CoffObj->export_directories()) {
- StringRef Name;
- uint32_t Offset;
- if (error(Ref.getSymbolName(Name)) || error(Ref.getExportRVA(Offset)))
- return;
- ExportSyms.push_back(OffsetNamePair{Offset, Name});
- }
- if (ExportSyms.empty())
- return;
-
- // Sort by ascending offset.
- array_pod_sort(ExportSyms.begin(), ExportSyms.end());
-
- // Approximate the symbol sizes by assuming they run to the next symbol.
- // FIXME: This assumes all exports are functions.
- uint64_t ImageBase = CoffObj->getImageBase();
- for (auto I = ExportSyms.begin(), E = ExportSyms.end(); I != E; ++I) {
- OffsetNamePair &Export = *I;
- // FIXME: The last export has a one byte size now.
- uint32_t NextOffset = I != E ? I->Offset : Export.Offset + 1;
- uint64_t SymbolStart = ImageBase + Export.Offset;
- uint64_t SymbolSize = NextOffset - Export.Offset;
- SymbolDesc SD = {SymbolStart, SymbolSize};
- Functions.insert(std::make_pair(SD, Export.Name));
- }
-}
-
-void ModuleInfo::addSymbol(const SymbolRef &Symbol, uint64_t SymbolSize,
- DataExtractor *OpdExtractor, uint64_t OpdAddress) {
- SymbolRef::Type SymbolType = Symbol.getType();
- if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
- return;
- ErrorOr<uint64_t> SymbolAddressOrErr = Symbol.getAddress();
- if (error(SymbolAddressOrErr.getError()))
- return;
- uint64_t SymbolAddress = *SymbolAddressOrErr;
- if (OpdExtractor) {
- // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
- // function descriptors. The first word of the descriptor is a pointer to
- // the function's code.
- // For the purposes of symbolization, pretend the symbol's address is that
- // of the function's code, not the descriptor.
- uint64_t OpdOffset = SymbolAddress - OpdAddress;
- uint32_t OpdOffset32 = OpdOffset;
- if (OpdOffset == OpdOffset32 &&
- OpdExtractor->isValidOffsetForAddress(OpdOffset32))
- SymbolAddress = OpdExtractor->getAddress(&OpdOffset32);
- }
- ErrorOr<StringRef> SymbolNameOrErr = Symbol.getName();
- if (error(SymbolNameOrErr.getError()))
- return;
- StringRef SymbolName = *SymbolNameOrErr;
- // Mach-O symbol table names have leading underscore, skip it.
- if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_')
- SymbolName = SymbolName.drop_front();
- // FIXME: If a function has alias, there are two entries in symbol table
- // with same address size. Make sure we choose the correct one.
- auto &M = SymbolType == SymbolRef::ST_Function ? Functions : Objects;
- SymbolDesc SD = { SymbolAddress, SymbolSize };
- M.insert(std::make_pair(SD, SymbolName));
-}
-
-// Return true if this is a 32-bit x86 PE COFF module.
-bool ModuleInfo::isWin32Module() const {
- auto *CoffObject = dyn_cast<COFFObjectFile>(Module);
- return CoffObject && CoffObject->getMachine() == COFF::IMAGE_FILE_MACHINE_I386;
-}
-
-uint64_t ModuleInfo::getModulePreferredBase() const {
- if (auto *CoffObject = dyn_cast<COFFObjectFile>(Module))
- return CoffObject->getImageBase();
- return 0;
-}