llvm.org GIT mirror llvm / 133aacf
[C++11] Introduce ObjectFile::symbols() to use range-based loops. Reviewers: rafael Reviewed By: rafael CC: llvm-commits Differential Revision: http://llvm-reviews.chandlerc.com/D3081 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204031 91177308-0d34-0410-b5e6-96231b3b80d8 Alexey Samsonov 6 years ago
10 changed file(s) with 94 addition(s) and 94 deletion(s). Raw diff Collapse all Expand all
285285 virtual error_code getLibraryPath(DataRefImpl Lib, StringRef &Res) const = 0;
286286
287287 public:
288 typedef iterator_range symbol_iterator_range;
289 symbol_iterator_range symbols() const {
290 return symbol_iterator_range(symbol_begin(), symbol_end());
291 }
288292
289293 virtual section_iterator section_begin() const = 0;
290294 virtual section_iterator section_end() const = 0;
3636 : Obj(Obj), Dis(Dis), MIA(MIA), MOS(0) {}
3737
3838 uint64_t MCObjectDisassembler::getEntrypoint() {
39 for (symbol_iterator SI = Obj.symbol_begin(), SE = Obj.symbol_end();
40 SI != SE; ++SI) {
39 for (const SymbolRef &Symbol : Obj.symbols()) {
4140 StringRef Name;
42 SI->getName(Name);
41 Symbol.getName(Name);
4342 if (Name == "main" || Name == "_main") {
4443 uint64_t Entrypoint;
45 SI->getAddress(Entrypoint);
44 Symbol.getAddress(Entrypoint);
4645 return getEffectiveLoadAddr(Entrypoint);
4746 }
4847 }
180179 AddressSetTy Splits;
181180 AddressSetTy Calls;
182181
183 for (symbol_iterator SI = Obj.symbol_begin(), SE = Obj.symbol_end();
184 SI != SE; ++SI) {
182 for (const SymbolRef &Symbol : Obj.symbols()) {
185183 SymbolRef::Type SymType;
186 SI->getType(SymType);
184 Symbol.getType(SymType);
187185 if (SymType == SymbolRef::ST_Function) {
188186 uint64_t SymAddr;
189 SI->getAddress(SymAddr);
187 Symbol.getAddress(SymAddr);
190188 SymAddr = getEffectiveLoadAddr(SymAddr);
191189 Calls.push_back(SymAddr);
192190 Splits.push_back(SymAddr);
152152 return false;
153153 uint64_t UValue = Value;
154154 // FIXME: map instead of looping each time?
155 for (symbol_iterator SI = Obj->symbol_begin(), SE = Obj->symbol_end();
156 SI != SE; ++SI) {
157 uint64_t SymAddr; SI->getAddress(SymAddr);
158 uint64_t SymSize; SI->getSize(SymSize);
159 StringRef SymName; SI->getName(SymName);
160 SymbolRef::Type SymType; SI->getType(SymType);
161 if (SymAddr == UnknownAddressOrSize || SymSize == UnknownAddressOrSize
162 || SymName.empty() || SymType != SymbolRef::ST_Function)
155 for (const SymbolRef &Symbol : Obj->symbols()) {
156 uint64_t SymAddr;
157 Symbol.getAddress(SymAddr);
158 uint64_t SymSize;
159 Symbol.getSize(SymSize);
160 StringRef SymName;
161 Symbol.getName(SymName);
162 SymbolRef::Type SymType;
163 Symbol.getType(SymType);
164 if (SymAddr == UnknownAddressOrSize || SymSize == UnknownAddressOrSize ||
165 SymName.empty() || SymType != SymbolRef::ST_Function)
163166 continue;
164167
165168 if ( SymAddr == UValue ||
295295 if (IsScattered) {
296296 uint32_t Val = O->getPlainRelocationSymbolNum(RE);
297297
298 for (symbol_iterator SI = O->symbol_begin(), SE = O->symbol_end();
299 SI != SE; ++SI) {
298 for (const SymbolRef &Symbol : O->symbols()) {
300299 error_code ec;
301300 uint64_t Addr;
302301 StringRef Name;
303302
304 if ((ec = SI->getAddress(Addr)))
303 if ((ec = Symbol.getAddress(Addr)))
305304 report_fatal_error(ec.message());
306 if (Addr != Val) continue;
307 if ((ec = SI->getName(Name)))
305 if (Addr != Val)
306 continue;
307 if ((ec = Symbol.getName(Name)))
308308 report_fatal_error(ec.message());
309309 fmt << Name;
310310 return;
527527 }
528528 // Unfortunately symbols are unsorted so we need to touch all
529529 // symbols from load command
530 for (symbol_iterator I = symbol_begin(), E = symbol_end(); I != E; ++I) {
531 DataRefImpl DRI = I->getRawDataRefImpl();
530 for (const SymbolRef &Symbol : symbols()) {
531 DataRefImpl DRI = Symbol.getRawDataRefImpl();
532532 Entry = getSymbolTableEntryBase(this, DRI);
533533 getSymbolAddress(DRI, Value);
534534 if (Entry.n_sect == SectionIndex && Value > BeginOffset)
145145 }
146146 }
147147
148 static void
149 getSectionsAndSymbols(const MachO::mach_header Header,
150 MachOObjectFile *MachOObj,
151 std::vector &Sections,
152 std::vector &Symbols,
153 SmallVectorImpl &FoundFns,
154 uint64_t &BaseSegmentAddress) {
155 for (symbol_iterator SI = MachOObj->symbol_begin(),
156 SE = MachOObj->symbol_end();
157 SI != SE; ++SI)
158 Symbols.push_back(*SI);
148 static void getSectionsAndSymbols(const MachO::mach_header Header,
149 MachOObjectFile *MachOObj,
150 std::vector &Sections,
151 std::vector &Symbols,
152 SmallVectorImpl &FoundFns,
153 uint64_t &BaseSegmentAddress) {
154 for (const SymbolRef &Symbol : MachOObj->symbols())
155 Symbols.push_back(Symbol);
159156
160157 for (const SectionRef &Section : MachOObj->sections()) {
161158 StringRef SectName;
407407 break;
408408
409409 // Make a list of all the symbols in this section.
410 std::vector > Symbols;
411 for (symbol_iterator SI = Obj->symbol_begin(), SE = Obj->symbol_end();
412 SI != SE; ++SI) {
410 std::vector> Symbols;
411 for (const SymbolRef &Symbol : Obj->symbols()) {
413412 bool contains;
414 if (!error(Section.containsSymbol(*SI, contains)) && contains) {
413 if (!error(Section.containsSymbol(Symbol, contains)) && contains) {
415414 uint64_t Address;
416 if (error(SI->getAddress(Address)))
415 if (error(Symbol.getAddress(Address)))
417416 break;
418417 if (Address == UnknownAddressOrSize)
419418 continue;
422421 continue;
423422
424423 StringRef Name;
425 if (error(SI->getName(Name)))
424 if (error(Symbol.getName(Name)))
426425 break;
427426 Symbols.push_back(std::make_pair(Address, Name));
428427 }
702701 if (const COFFObjectFile *coff = dyn_cast(o))
703702 PrintCOFFSymbolTable(coff);
704703 else {
705 for (symbol_iterator si = o->symbol_begin(), se = o->symbol_end();
706 si != se; ++si) {
704 for (const SymbolRef &Symbol : o->symbols()) {
707705 StringRef Name;
708706 uint64_t Address;
709707 SymbolRef::Type Type;
710708 uint64_t Size;
711 uint32_t Flags = si->getFlags();
709 uint32_t Flags = Symbol.getFlags();
712710 section_iterator Section = o->section_end();
713 if (error(si->getName(Name))) continue;
714 if (error(si->getAddress(Address))) continue;
715 if (error(si->getType(Type))) continue;
716 if (error(si->getSize(Size))) continue;
717 if (error(si->getSection(Section))) continue;
711 if (error(Symbol.getName(Name)))
712 continue;
713 if (error(Symbol.getAddress(Address)))
714 continue;
715 if (error(Symbol.getType(Type)))
716 continue;
717 if (error(Symbol.getSize(Size)))
718 continue;
719 if (error(Symbol.getSection(Section)))
720 continue;
718721
719722 bool Global = Flags & SymbolRef::SF_Global;
720723 bool Weak = Flags & SymbolRef::SF_Weak;
3737 virtual void printUnwindInfo() override;
3838
3939 private:
40 void printSymbol(symbol_iterator SymI);
40 void printSymbol(const SymbolRef &Symbol);
4141
4242 void printRelocation(const RelocationRef &Reloc);
4343
254254
255255 if (opts::SectionSymbols) {
256256 ListScope D(W, "Symbols");
257 for (symbol_iterator SymI = Obj->symbol_begin(), SymE = Obj->symbol_end();
258 SymI != SymE; ++SymI) {
257 for (const SymbolRef &Symbol : Obj->symbols()) {
259258 bool Contained = false;
260 if (Section.containsSymbol(*SymI, Contained) || !Contained)
259 if (Section.containsSymbol(Symbol, Contained) || !Contained)
261260 continue;
262261
263 printSymbol(SymI);
262 printSymbol(Symbol);
264263 }
265264 }
266265
353352 void MachODumper::printSymbols() {
354353 ListScope Group(W, "Symbols");
355354
356 for (symbol_iterator SymI = Obj->symbol_begin(), SymE = Obj->symbol_end();
357 SymI != SymE; ++SymI) {
358 printSymbol(SymI);
355 for (const SymbolRef &Symbol : Obj->symbols()) {
356 printSymbol(Symbol);
359357 }
360358 }
361359
363361 ListScope Group(W, "DynamicSymbols");
364362 }
365363
366 void MachODumper::printSymbol(symbol_iterator SymI) {
364 void MachODumper::printSymbol(const SymbolRef &Symbol) {
367365 StringRef SymbolName;
368 if (SymI->getName(SymbolName))
366 if (Symbol.getName(SymbolName))
369367 SymbolName = "";
370368
371 MachOSymbol Symbol;
372 getSymbol(Obj, SymI->getRawDataRefImpl(), Symbol);
369 MachOSymbol MOSymbol;
370 getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol);
373371
374372 StringRef SectionName = "";
375373 section_iterator SecI(Obj->section_begin());
376 if (!error(SymI->getSection(SecI)) &&
377 SecI != Obj->section_end())
378 error(SecI->getName(SectionName));
374 if (!error(Symbol.getSection(SecI)) && SecI != Obj->section_end())
375 error(SecI->getName(SectionName));
379376
380377 DictScope D(W, "Symbol");
381 W.printNumber("Name", SymbolName, Symbol.StringIndex);
382 if (Symbol.Type & MachO::N_STAB) {
383 W.printHex ("Type", "SymDebugTable", Symbol.Type);
378 W.printNumber("Name", SymbolName, MOSymbol.StringIndex);
379 if (MOSymbol.Type & MachO::N_STAB) {
380 W.printHex("Type", "SymDebugTable", MOSymbol.Type);
384381 } else {
385 if (Symbol.Type & MachO::N_PEXT)
382 if (MOSymbol.Type & MachO::N_PEXT)
386383 W.startLine() << "PrivateExtern\n";
387 if (Symbol.Type & MachO::N_EXT)
384 if (MOSymbol.Type & MachO::N_EXT)
388385 W.startLine() << "Extern\n";
389 W.printEnum("Type", uint8_t(Symbol.Type & MachO::N_TYPE),
386 W.printEnum("Type", uint8_t(MOSymbol.Type & MachO::N_TYPE),
390387 makeArrayRef(MachOSymbolTypes));
391388 }
392 W.printHex ("Section", SectionName, Symbol.SectionIndex);
393 W.printEnum ("RefType", static_cast(Symbol.Flags & 0xF),
394 makeArrayRef(MachOSymbolRefTypes));
395 W.printFlags ("Flags", static_cast(Symbol.Flags & ~0xF),
396 makeArrayRef(MachOSymbolFlags));
397 W.printHex ("Value", Symbol.Value);
389 W.printHex("Section", SectionName, MOSymbol.SectionIndex);
390 W.printEnum("RefType", static_cast(MOSymbol.Flags & 0xF),
391 makeArrayRef(MachOSymbolRefTypes));
392 W.printFlags("Flags", static_cast(MOSymbol.Flags & ~0xF),
393 makeArrayRef(MachOSymbolFlags));
394 W.printHex("Value", MOSymbol.Value);
398395 }
399396
400397 void MachODumper::printUnwindInfo() {
5252
5353 ModuleInfo::ModuleInfo(ObjectFile *Obj, DIContext *DICtx)
5454 : Module(Obj), DebugInfoContext(DICtx) {
55 for (symbol_iterator si = Module->symbol_begin(), se = Module->symbol_end();
56 si != se; ++si) {
57 addSymbol(si);
55 for (const SymbolRef &Symbol : Module->symbols()) {
56 addSymbol(Symbol);
5857 }
5958 bool NoSymbolTable = (Module->symbol_begin() == Module->symbol_end());
6059 if (NoSymbolTable && Module->isELF()) {
6261 std::pair IDyn =
6362 getELFDynamicSymbolIterators(Module);
6463 for (symbol_iterator si = IDyn.first, se = IDyn.second; si != se; ++si) {
65 addSymbol(si);
66 }
67 }
68 }
69
70 void ModuleInfo::addSymbol(const symbol_iterator &Sym) {
64 addSymbol(*si);
65 }
66 }
67 }
68
69 void ModuleInfo::addSymbol(const SymbolRef &Symbol) {
7170 SymbolRef::Type SymbolType;
72 if (error(Sym->getType(SymbolType)))
71 if (error(Symbol.getType(SymbolType)))
7372 return;
74 if (SymbolType != SymbolRef::ST_Function &&
75 SymbolType != SymbolRef::ST_Data)
73 if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
7674 return;
7775 uint64_t SymbolAddress;
78 if (error(Sym->getAddress(SymbolAddress)) ||
76 if (error(Symbol.getAddress(SymbolAddress)) ||
7977 SymbolAddress == UnknownAddressOrSize)
8078 return;
8179 uint64_t SymbolSize;
8381 // occupies the memory range up to the following symbol.
8482 if (isa(Module))
8583 SymbolSize = 0;
86 else if (error(Sym->getSize(SymbolSize)) ||
84 else if (error(Symbol.getSize(SymbolSize)) ||
8785 SymbolSize == UnknownAddressOrSize)
8886 return;
8987 StringRef SymbolName;
90 if (error(Sym->getName(SymbolName)))
88 if (error(Symbol.getName(SymbolName)))
9189 return;
9290 // Mach-O symbol table names have leading underscore, skip it.
9391 if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_')
100100 bool getNameFromSymbolTable(SymbolRef::Type Type, uint64_t Address,
101101 std::string &Name, uint64_t &Addr,
102102 uint64_t &Size) const;
103 void addSymbol(const symbol_iterator &Sym);
103 void addSymbol(const SymbolRef &Symbol);
104104 ObjectFile *Module;
105105 std::unique_ptr DebugInfoContext;
106106
201201 // Dump the symbol table.
202202 outs() << " ('_symbols', [\n";
203203 unsigned SymNum = 0;
204 for (symbol_iterator I = Obj.symbol_begin(), E = Obj.symbol_end(); I != E;
205 ++I, ++SymNum) {
206 DataRefImpl DRI = I->getRawDataRefImpl();
204 for (const SymbolRef &Symbol : Obj.symbols()) {
205 DataRefImpl DRI = Symbol.getRawDataRefImpl();
207206 if (Obj.is64Bit()) {
208207 MachO::nlist_64 STE = Obj.getSymbol64TableEntry(DRI);
209208 DumpSymbolTableEntryData(Obj, SymNum, STE.n_strx, STE.n_type,
215214 STE.n_sect, STE.n_desc, STE.n_value,
216215 StringTable);
217216 }
217 SymNum++;
218218 }
219219 outs() << " ])\n";
220220