+ for (const SymbolRef &Symbol : Module->symbols()) {
+ addSymbol(Symbol, OpdExtractor.get(), OpdAddress);
+ }
+ bool NoSymbolTable = (Module->symbol_begin() == Module->symbol_end());
+ if (NoSymbolTable && Module->isELF()) {
+ // Fallback to dynamic symbol table, if regular symbol table is stripped.
+ auto IDyn = cast<ELFObjectFileBase>(Module)->getDynamicSymbolIterators();
+ for (SymbolRef Sym : IDyn) {
+ addSymbol(Sym, OpdExtractor.get(), OpdAddress);
+ }
+ }
+}
+
+void ModuleInfo::addSymbol(const SymbolRef &Symbol, DataExtractor *OpdExtractor,
+ uint64_t OpdAddress) {
+ SymbolRef::Type SymbolType;
+ if (error(Symbol.getType(SymbolType)))
+ return;
+ if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
+ return;
+ uint64_t SymbolAddress;
+ if (error(Symbol.getAddress(SymbolAddress)) ||
+ SymbolAddress == UnknownAddress)
+ return;
+ 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);
+ }
+ uint64_t SymbolSize;
+ // Onyl ELF has a size for every symbol so assume that symbol occupies the
+ // memory range up to the following symbol.
+ if (auto *E = dyn_cast<ELFObjectFileBase>(Module))
+ SymbolSize = E->getSymbolSize(Symbol);
+ else
+ SymbolSize = 0;
+ StringRef SymbolName;
+ if (error(Symbol.getName(SymbolName)))
+ return;
+ // 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));