explicit symbol(const SymbolRef& sym) { StringRef name; if(error_code err = sym.getName(name)) llvm_binary_fail(err); this->name_ = name.str(); if (error_code err = sym.getType(this->kind_)) llvm_binary_fail(err); if (error_code err = sym.getAddress(this->addr_)) llvm_binary_fail(err); if (error_code err = sym.getSize(this->size_)) llvm_binary_fail(err); uint32_t flags; if (error_code err = sym.getFlags(flags)) llvm_binary_fail(err); if (flags & SymbolRef::SF_Undefined) { uint64_t addr; if (error_code err = sym.getValue(addr)) llvm_binary_fail(err); // This will not work for x86-64, since they usually zero // the value. BFD library uses index correspondence // between plt entry and relocation, to name the plt // entry. We can't afford this. if (addr) { addr_ = addr; size_ = 8; } } }
std::vector<std::pair<SymbolRef, uint64_t>> llvm::object::computeSymbolSizes(const ObjectFile &O) { std::vector<std::pair<SymbolRef, uint64_t>> Ret; if (const auto *E = dyn_cast<ELFObjectFileBase>(&O)) { auto Syms = E->symbols(); if (Syms.begin() == Syms.end()) Syms = E->getDynamicSymbolIterators(); for (ELFSymbolRef Sym : Syms) Ret.push_back({Sym, Sym.getSize()}); return Ret; } // Collect sorted symbol addresses. Include dummy addresses for the end // of each section. std::vector<SymEntry> Addresses; unsigned SymNum = 0; for (symbol_iterator I = O.symbol_begin(), E = O.symbol_end(); I != E; ++I) { SymbolRef Sym = *I; uint64_t Value = Sym.getValue(); Addresses.push_back({I, Value, SymNum, getSymbolSectionID(O, Sym)}); ++SymNum; } for (SectionRef Sec : O.sections()) { uint64_t Address = Sec.getAddress(); uint64_t Size = Sec.getSize(); Addresses.push_back( {O.symbol_end(), Address + Size, 0, getSectionID(O, Sec)}); } array_pod_sort(Addresses.begin(), Addresses.end(), compareAddress); // Compute the size as the gap to the next symbol for (unsigned I = 0, N = Addresses.size() - 1; I < N; ++I) { auto &P = Addresses[I]; if (P.I == O.symbol_end()) continue; // If multiple symbol have the same address, give both the same size. unsigned NextI = I + 1; while (NextI < N && Addresses[NextI].Address == P.Address) ++NextI; uint64_t Size = Addresses[NextI].Address - P.Address; P.Address = Size; } // Assign the sorted symbols in the original order. Ret.resize(SymNum); for (SymEntry &P : Addresses) { if (P.I == O.symbol_end()) continue; Ret[P.Number] = {*P.I, P.Address}; } return Ret; }
uint64_t RuntimeDyldCOFF::getSymbolOffset(const SymbolRef &Sym) { // The value in a relocatable COFF object is the offset. return Sym.getValue(); }