llvm.org GIT mirror llvm / 8a80641
Return ErrorOr from SymbolRef::getName. This function can really fail since the string table offset can be out of bounds. Using ErrorOr makes sure the error is checked. Hopefully a lot of the boilerplate code in tools/* can go away once we have a diagnostic manager in Object. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241297 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
30 changed file(s) with 248 addition(s) and 172 deletion(s). Raw diff Collapse all Expand all
646646 }
647647 protected:
648648 void moveSymbolNext(DataRefImpl &Symb) const override;
649 std::error_code getSymbolName(DataRefImpl Symb,
650 StringRef &Res) const override;
649 ErrorOr getSymbolName(DataRefImpl Symb) const override;
651650 std::error_code getSymbolAddress(DataRefImpl Symb,
652651 uint64_t &Res) const override;
653652 uint64_t getSymbolValue(DataRefImpl Symb) const override;
194194 ELFFile EF;
195195
196196 void moveSymbolNext(DataRefImpl &Symb) const override;
197 std::error_code getSymbolName(DataRefImpl Symb,
198 StringRef &Res) const override;
197 ErrorOr getSymbolName(DataRefImpl Symb) const override;
199198 std::error_code getSymbolAddress(DataRefImpl Symb,
200199 uint64_t &Res) const override;
201200 uint64_t getSymbolValue(DataRefImpl Symb) const override;
348347 }
349348
350349 template
351 std::error_code ELFObjectFile::getSymbolName(DataRefImpl Sym,
352 StringRef &Result) const {
350 ErrorOr ELFObjectFile::getSymbolName(DataRefImpl Sym) const {
353351 const Elf_Sym *ESym = toELFSymIter(Sym);
354 ErrorOr Name = EF.getSymbolName(ESym, Sym.p & 1);
355 if (!Name)
356 return Name.getError();
357 Result = *Name;
358 return std::error_code();
352 return EF.getSymbolName(ESym, Sym.p & 1);
359353 }
360354
361355 template
198198 void moveSymbolNext(DataRefImpl &Symb) const override;
199199
200200 uint64_t getNValue(DataRefImpl Sym) const;
201 std::error_code getSymbolName(DataRefImpl Symb,
202 StringRef &Res) const override;
201 ErrorOr getSymbolName(DataRefImpl Symb) const override;
203202
204203 // MachO specific.
205204 std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const;
131131 assert(isa(BasicSymbolRef::getObject()));
132132 }
133133
134 std::error_code getName(StringRef &Result) const;
134 ErrorOr getName() const;
135135 /// Returns the symbol virtual address (i.e. address at which it will be
136136 /// mapped).
137137 std::error_code getAddress(uint64_t &Result) const;
194194 // Implementations assume that the DataRefImpl is valid and has not been
195195 // modified externally. It's UB otherwise.
196196 friend class SymbolRef;
197 virtual std::error_code getSymbolName(DataRefImpl Symb,
198 StringRef &Res) const = 0;
197 virtual ErrorOr getSymbolName(DataRefImpl Symb) const = 0;
199198 std::error_code printSymbolName(raw_ostream &OS,
200199 DataRefImpl Symb) const override;
201200 virtual std::error_code getSymbolAddress(DataRefImpl Symb,
304303 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
305304 : BasicSymbolRef(SymbolP, Owner) {}
306305
307 inline std::error_code SymbolRef::getName(StringRef &Result) const {
308 return getObject()->getSymbolName(getRawDataRefImpl(), Result);
306 inline ErrorOr SymbolRef::getName() const {
307 return getObject()->getSymbolName(getRawDataRefImpl());
309308 }
310309
311310 inline std::error_code SymbolRef::getAddress(uint64_t &Result) const {
180180 SymType == object::SymbolRef::ST_Data ||
181181 SymType == object::SymbolRef::ST_Unknown) {
182182
183 StringRef Name;
183 ErrorOr NameOrErr = I->getName();
184 Check(NameOrErr.getError());
185 StringRef Name = *NameOrErr;
184186 uint64_t SectOffset;
185 Check(I->getName(Name));
186187 Check(getOffset(*I, SectOffset));
187188 section_iterator SI = Obj.section_end();
188189 Check(I->getSection(SI));
480481 DEBUG(dbgs() << "Processing common symbols...\n");
481482
482483 for (const auto &Sym : CommonSymbols) {
483 StringRef Name;
484 Check(Sym.getName(Name));
484 ErrorOr NameOrErr = Sym.getName();
485 Check(NameOrErr.getError());
486 StringRef Name = *NameOrErr;
485487
486488 // Skip common symbols already elsewhere.
487489 if (GlobalSymbolTable.count(Name) ||
514516 // Assign the address of each symbol
515517 for (auto &Sym : SymbolsToAllocate) {
516518 uint32_t Align = Sym.getAlignment();
517 StringRef Name;
518519 uint64_t Size = Sym.getCommonSize();
519 Check(Sym.getName(Name));
520 ErrorOr NameOrErr = Sym.getName();
521 Check(NameOrErr.getError());
522 StringRef Name = *NameOrErr;
520523 if (Align) {
521524 // This symbol has an alignment requirement.
522525 uint64_t AlignOffset = OffsetToAlignment((uint64_t)Addr, Align);
10631063
10641064 // Obtain the symbol name which is referenced in the relocation
10651065 StringRef TargetName;
1066 if (Symbol != Obj.symbol_end())
1067 Symbol->getName(TargetName);
1066 if (Symbol != Obj.symbol_end()) {
1067 ErrorOr TargetNameOrErr = Symbol->getName();
1068 if (std::error_code EC = TargetNameOrErr.getError())
1069 report_fatal_error(EC.message());
1070 TargetName = *TargetNameOrErr;
1071 }
10681072 DEBUG(dbgs() << "\t\tRelType: " << RelType << " Addend: " << Addend
10691073 << " TargetName: " << TargetName << "\n");
10701074 RelocationValueRef Value;
6262 bool IsExternal = Obj.getPlainRelocationExternal(RelInfo);
6363 if (IsExternal) {
6464 symbol_iterator Symbol = RI->getSymbol();
65 StringRef TargetName;
66 Symbol->getName(TargetName);
65 ErrorOr TargetNameOrErr = Symbol->getName();
66 if (std::error_code EC = TargetNameOrErr.getError())
67 report_fatal_error(EC.message());
68 StringRef TargetName = *TargetNameOrErr;
6769 RTDyldSymbolTable::const_iterator SI =
6870 GlobalSymbolTable.find(TargetName.data());
6971 if (SI != GlobalSymbolTable.end()) {
161163 unsigned SymbolIndex =
162164 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
163165 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
164 StringRef IndirectSymbolName;
165 SI->getName(IndirectSymbolName);
166 ErrorOr IndirectSymbolNameOrErr = SI->getName();
167 if (std::error_code EC = IndirectSymbolNameOrErr.getError())
168 report_fatal_error(EC.message());
169 StringRef IndirectSymbolName = *IndirectSymbolNameOrErr;
166170 DEBUG(dbgs() << " " << IndirectSymbolName << ": index " << SymbolIndex
167171 << ", PT offset: " << PTEntryOffset << "\n");
168172 RelocationEntry RE(PTSectionID, PTEntryOffset,
154154 break;
155155 }
156156
157 StringRef TargetName;
158 Symbol->getName(TargetName);
157 ErrorOr TargetNameOrErr = Symbol->getName();
158 if (std::error_code EC = TargetNameOrErr.getError())
159 report_fatal_error(EC.message());
160 StringRef TargetName = *TargetNameOrErr;
159161
160162 DEBUG(dbgs() << "\t\tIn Section " << SectionID << " Offset " << Offset
161163 << " RelType: " << RelType << " TargetName: " << TargetName
239239 unsigned SymbolIndex =
240240 Obj.getIndirectSymbolTableEntry(DySymTabCmd, FirstIndirectSymbol + i);
241241 symbol_iterator SI = Obj.getSymbolByIndex(SymbolIndex);
242 StringRef IndirectSymbolName;
243 SI->getName(IndirectSymbolName);
242 ErrorOr IndirectSymbolName = SI->getName();
243 if (std::error_code EC = IndirectSymbolName.getError())
244 report_fatal_error(EC.message());
244245 uint8_t *JTEntryAddr = JTSectionAddr + JTEntryOffset;
245246 createStubFunction(JTEntryAddr);
246247 RelocationEntry RE(JTSectionID, JTEntryOffset + 1,
247248 MachO::GENERIC_RELOC_VANILLA, 0, true, 2);
248 addRelocationForSymbol(RE, IndirectSymbolName);
249 addRelocationForSymbol(RE, *IndirectSymbolName);
249250 JTEntryOffset += JTEntrySize;
250251 }
251252 }
144144 }
145145 }
146146
147 std::error_code COFFObjectFile::getSymbolName(DataRefImpl Ref,
148 StringRef &Result) const {
147 ErrorOr COFFObjectFile::getSymbolName(DataRefImpl Ref) const {
149148 COFFSymbolRef Symb = getCOFFSymbol(Ref);
150 return getSymbolName(Symb, Result);
149 StringRef Result;
150 std::error_code EC = getSymbolName(Symb, Result);
151 if (EC)
152 return EC;
153 return Result;
151154 }
152155
153156 uint64_t COFFObjectFile::getSymbolValue(DataRefImpl Ref) const {
326326 Symb.p += SymbolTableEntrySize;
327327 }
328328
329 std::error_code MachOObjectFile::getSymbolName(DataRefImpl Symb,
330 StringRef &Res) const {
329 ErrorOr MachOObjectFile::getSymbolName(DataRefImpl Symb) const {
331330 StringRef StringTable = getStringTableData();
332331 MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
333332 const char *Start = &StringTable.data()[Entry.n_strx];
334333 if (Start < getData().begin() || Start >= getData().end())
335334 report_fatal_error(
336335 "Symbol name entry points before beginning or past end of file.");
337 Res = StringRef(Start);
338 return std::error_code();
336 return StringRef(Start);
339337 }
340338
341339 unsigned MachOObjectFile::getSectionType(SectionRef Sec) const {
172172
173173 // SymbolRef accessors
174174 const char *LLVMGetSymbolName(LLVMSymbolIteratorRef SI) {
175 StringRef ret;
176 if (std::error_code ec = (*unwrap(SI))->getName(ret))
177 report_fatal_error(ec.message());
178 return ret.data();
175 ErrorOr Ret = (*unwrap(SI))->getName();
176 if (std::error_code EC = Ret.getError())
177 report_fatal_error(EC.message());
178 return Ret->data();
179179 }
180180
181181 uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
3636
3737 std::error_code ObjectFile::printSymbolName(raw_ostream &OS,
3838 DataRefImpl Symb) const {
39 StringRef Name;
40 if (std::error_code EC = getSymbolName(Symb, Name))
39 ErrorOr Name = getSymbolName(Symb);
40 if (std::error_code EC = Name.getError())
4141 return EC;
42 OS << Name;
42 OS << *Name;
4343 return std::error_code();
4444 }
4545
2828 uint64_t RelType = Rel.getType();
2929 elf_symbol_iterator SymI = Rel.getSymbol();
3030
31 StringRef SymName; SymI->getName(SymName);
31 ErrorOr SymNameOrErr = SymI->getName();
32 if (std::error_code EC = SymNameOrErr.getError())
33 report_fatal_error(EC.message());
34 StringRef SymName = *SymNameOrErr;
35
3236 uint64_t SymAddr; SymI->getAddress(SymAddr);
3337 uint64_t SymSize = SymI->getSize();
3438 int64_t Addend = *ELFRelocationRef(Rel).getAddend();
2929 uint64_t RelType = Rel.getType();
3030 symbol_iterator SymI = Rel.getSymbol();
3131
32 StringRef SymName; SymI->getName(SymName);
32 ErrorOr SymNameOrErr = SymI->getName();
33 if (std::error_code EC = SymNameOrErr.getError())
34 report_fatal_error(EC.message());
35 StringRef SymName = *SymNameOrErr;
3336 uint64_t SymAddr; SymI->getAddress(SymAddr);
3437
3538 any_relocation_info RE = Obj->getRelocation(Rel.getRawDataRefImpl());
8891 symbol_iterator RSymI = Rel.getSymbol();
8992 uint64_t RSymAddr;
9093 RSymI->getAddress(RSymAddr);
91 StringRef RSymName;
92 RSymI->getName(RSymName);
94 ErrorOr RSymName = RSymI->getName();
95 if (std::error_code EC = RSymName.getError())
96 report_fatal_error(EC.message());
9397
94 MCSymbol *RSym = Ctx.getOrCreateSymbol(RSymName);
98 MCSymbol *RSym = Ctx.getOrCreateSymbol(*RSymName);
9599 if (!RSym->isVariable())
96100 RSym->setVariableValue(MCConstantExpr::create(RSymAddr, Ctx));
97101
215215 // during the test, we can't hardcode the symbols addresses, so
216216 // look them up here and rewrite them.
217217 for (const auto &Sym : ErrOrObjectFile->symbols()) {
218 StringRef Name;
219218 uint64_t Address;
220 if (Sym.getName(Name) || Sym.getAddress(Address))
219 if (Sym.getAddress(Address))
221220 continue;
222 SymbolAddresses[Name] = Address;
221 ErrorOr Name = Sym.getName();
222 if (!Name)
223 continue;
224 SymbolAddresses[*Name] = Address;
223225 }
224226 }
225227
14611461
14621462 auto Sym = Reloc.getSymbol();
14631463 if (Sym != Obj.symbol_end()) {
1464 StringRef SymbolName;
1465 if (Sym->getName(SymbolName)) {
1464 ErrorOr SymbolName = Sym->getName();
1465 if (!SymbolName) {
14661466 reportWarning("error getting relocation symbol name.");
14671467 continue;
14681468 }
1469 if (const auto *Mapping = DMO.lookupSymbol(SymbolName))
1469 if (const auto *Mapping = DMO.lookupSymbol(*SymbolName))
14701470 ValidRelocs.emplace_back(Offset64, RelocSize, Addend, Mapping);
14711471 } else if (const auto *Mapping = DMO.lookupObjectAddress(Addend)) {
14721472 // Do not store the addend. The addend was the address of the
196196 CurrentObjectAddresses.clear();
197197
198198 for (auto Sym : CurrentObjectHolder.Get().symbols()) {
199 StringRef Name;
199
200200 uint64_t Addr;
201 if (Sym.getAddress(Addr) || Addr == UnknownAddress || Sym.getName(Name))
202 continue;
203 CurrentObjectAddresses[Name] = Addr;
201 if (Sym.getAddress(Addr) || Addr == UnknownAddress)
202 continue;
203 ErrorOr Name = Sym.getName();
204 if (!Name)
205 continue;
206 CurrentObjectAddresses[*Name] = Addr;
204207 }
205208 }
206209
224227 // Skip undefined and STAB entries.
225228 if ((Type & SymbolRef::ST_Debug) || (Type & SymbolRef::ST_Unknown))
226229 continue;
227 StringRef Name;
228230 uint64_t Addr;
229231 // The only symbols of interest are the global variables. These
230232 // are the only ones that need to be queried because the address
232234 // addresses should be fetched for the debug map.
233235 if (Sym.getAddress(Addr) || Addr == UnknownAddress ||
234236 !(Sym.getFlags() & SymbolRef::SF_Global) || Sym.getSection(Section) ||
235 Section->isText() || Sym.getName(Name) || Name.size() == 0 ||
236 Name[0] == '\0')
237 Section->isText())
238 continue;
239 ErrorOr NameOrErr = Sym.getName();
240 if (!NameOrErr)
241 continue;
242 StringRef Name = *NameOrErr;
243 if (Name.size() == 0 || Name[0] == '\0')
237244 continue;
238245 MainBinarySymbolAddresses[Name] = Addr;
239246 }
9696 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
9797 if (RelocSymI == Obj->symbol_end())
9898 continue;
99 StringRef RelocSymName;
100 if (error(RelocSymI->getName(RelocSymName)))
99 ErrorOr RelocSymName = RelocSymI->getName();
100 if (error(RelocSymName.getError()))
101101 return true;
102102 uint64_t Offset = Reloc.getOffset();
103103 if (Offset >= SymOffset && Offset < SymEnd) {
104 *I = RelocSymName;
104 *I = *RelocSymName;
105105 ++I;
106106 }
107107 }
120120 const object::symbol_iterator RelocSymI = Reloc.getSymbol();
121121 if (RelocSymI == Obj->symbol_end())
122122 continue;
123 StringRef RelocSymName;
124 if (error(RelocSymI->getName(RelocSymName)))
123 ErrorOr RelocSymName = RelocSymI->getName();
124 if (error(RelocSymName.getError()))
125125 return true;
126126 uint64_t Offset = Reloc.getOffset();
127127 if (Offset >= SymOffset && Offset < SymEnd)
128 Collection[std::make_pair(SymName, Offset - SymOffset)] = RelocSymName;
128 Collection[std::make_pair(SymName, Offset - SymOffset)] = *RelocSymName;
129129 }
130130 }
131131 return false;
189189 for (auto &P : SymAddr) {
190190 object::SymbolRef Sym = P.first;
191191 uint64_t SymSize = P.second;
192 StringRef SymName;
193 if (error(Sym.getName(SymName)))
192 ErrorOr SymNameOrErr = Sym.getName();
193 if (error(SymNameOrErr.getError()))
194194 return;
195 StringRef SymName = *SymNameOrErr;
195196 object::section_iterator SecI(Obj->section_begin());
196197 if (error(Sym.getSection(SecI)))
197198 return;
652652 }
653653
654654 if (SymI->getELFType() == ELF::STT_SECTION) {
655 StringRef Name;
656 if (error(SymI->getName(Name)))
655 ErrorOr Name = SymI->getName();
656 if (error(Name.getError()))
657657 return '?';
658 return StringSwitch(Name)
658 return StringSwitch(*Name)
659659 .StartsWith(".debug", 'N')
660660 .StartsWith(".note", 'n')
661661 .Default('?');
669669 // OK, this is COFF.
670670 symbol_iterator SymI(I);
671671
672 StringRef Name;
673 if (error(SymI->getName(Name)))
672 ErrorOr Name = SymI->getName();
673 if (error(Name.getError()))
674674 return '?';
675675
676 char Ret = StringSwitch(Name)
676 char Ret = StringSwitch(*Name)
677677 .StartsWith(".debug", 'N')
678678 .StartsWith(".sxdata", 'N')
679679 .Default('?');
212212 SymbolRef Sym;
213213 if (std::error_code EC = resolveSymbol(Rels, Offset, Sym))
214214 return EC;
215 if (std::error_code EC = Sym.getName(Name))
216 return EC;
215 ErrorOr NameOrErr = Sym.getName();
216 if (std::error_code EC = NameOrErr.getError())
217 return EC;
218 Name = *NameOrErr;
217219 return std::error_code();
218220 }
219221
281281 SmallVectorImpl &FoundFns,
282282 uint64_t &BaseSegmentAddress) {
283283 for (const SymbolRef &Symbol : MachOObj->symbols()) {
284 StringRef SymName;
285 Symbol.getName(SymName);
286 if (!SymName.startswith("ltmp"))
284 ErrorOr SymName = Symbol.getName();
285 if (std::error_code EC = SymName.getError())
286 report_fatal_error(EC.message());
287 if (!SymName->startswith("ltmp"))
287288 Symbols.push_back(Symbol);
288289 }
289290
360361 if (indirect_symbol < Symtab.nsyms) {
361362 symbol_iterator Sym = O->getSymbolByIndex(indirect_symbol);
362363 SymbolRef Symbol = *Sym;
363 StringRef SymName;
364 Symbol.getName(SymName);
365 outs() << SymName;
364 ErrorOr SymName = Symbol.getName();
365 if (std::error_code EC = SymName.getError())
366 report_fatal_error(EC.message());
367 outs() << *SymName;
366368 } else {
367369 outs() << "?";
368370 }
591593 ST == SymbolRef::ST_Other) {
592594 uint64_t Address;
593595 Symbol.getAddress(Address);
594 StringRef SymName;
595 Symbol.getName(SymName);
596 ErrorOr SymNameOrErr = Symbol.getName();
597 if (std::error_code EC = SymNameOrErr.getError())
598 report_fatal_error(EC.message());
599 StringRef SymName = *SymNameOrErr;
596600 if (!SymName.startswith(".objc"))
597601 (*AddrMap)[Address] = SymName;
598602 }
829833 [&](const std::pair &P) { return P.first == i; });
830834 if (Reloc != Relocs.end()) {
831835 symbol_iterator RelocSym = Reloc->second;
832 StringRef SymName;
833 RelocSym->getName(SymName);
834 outs() << "external relocation entry for symbol:" << SymName << "\n";
836 ErrorOr SymName = RelocSym->getName();
837 if (std::error_code EC = SymName.getError())
838 report_fatal_error(EC.message());
839 outs() << "external relocation entry for symbol:" << *SymName << "\n";
835840 continue;
836841 }
837842
17921797 }
17931798 }
17941799 if (reloc_found && isExtern) {
1795 StringRef SymName;
1796 Symbol.getName(SymName);
1797 const char *name = SymName.data();
1800 ErrorOr SymName = Symbol.getName();
1801 if (std::error_code EC = SymName.getError())
1802 report_fatal_error(EC.message());
1803 const char *name = SymName->data();
17981804 op_info->AddSymbol.Present = 1;
17991805 op_info->AddSymbol.Name = name;
18001806 // For i386 extern relocation entries the value in the instruction is
18581864 // is the offset from the external symbol.
18591865 if (info->O->getAnyRelocationPCRel(RE))
18601866 op_info->Value -= Pc + Offset + Size;
1861 StringRef SymName;
1862 Symbol.getName(SymName);
1863 const char *name = SymName.data();
1867 ErrorOr SymName = Symbol.getName();
1868 if (std::error_code EC = SymName.getError())
1869 report_fatal_error(EC.message());
1870 const char *name = SymName->data();
18641871 unsigned Type = info->O->getAnyRelocationType(RE);
18651872 if (Type == MachO::X86_64_RELOC_SUBTRACTOR) {
18661873 DataRefImpl RelNext = Rel;
18741881 op_info->SubtractSymbol.Name = name;
18751882 symbol_iterator RelocSymNext = info->O->getSymbolByIndex(SymbolNum);
18761883 Symbol = *RelocSymNext;
1877 StringRef SymNameNext;
1878 Symbol.getName(SymNameNext);
1879 name = SymNameNext.data();
1884 ErrorOr SymNameNext = Symbol.getName();
1885 if (std::error_code EC = SymNameNext.getError())
1886 report_fatal_error(EC.message());
1887 name = SymNameNext->data();
18801888 }
18811889 }
18821890 // TODO: add the VariantKinds to op_info->VariantKind for relocation types
19431951 }
19441952
19451953 if (isExtern) {
1946 StringRef SymName;
1947 Symbol.getName(SymName);
1948 const char *name = SymName.data();
1954 ErrorOr SymName = Symbol.getName();
1955 if (std::error_code EC = SymName.getError())
1956 report_fatal_error(EC.message());
1957 const char *name = SymName->data();
19491958 op_info->AddSymbol.Present = 1;
19501959 op_info->AddSymbol.Name = name;
19511960 switch (r_type) {
20552064 // NOTE: Scattered relocations don't exist on arm64.
20562065 if (!info->O->getPlainRelocationExternal(RE))
20572066 return 0;
2058 StringRef SymName;
2059 Reloc->getSymbol()->getName(SymName);
2060 const char *name = SymName.data();
2067 ErrorOr SymName = Reloc->getSymbol()->getName();
2068 if (std::error_code EC = SymName.getError())
2069 report_fatal_error(EC.message());
2070 const char *name = SymName->data();
20612071 op_info->AddSymbol.Present = 1;
20622072 op_info->AddSymbol.Name = name;
20632073
21842194 if (indirect_symbol < Symtab.nsyms) {
21852195 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
21862196 SymbolRef Symbol = *Sym;
2187 StringRef SymName;
2188 Symbol.getName(SymName);
2189 const char *name = SymName.data();
2197 ErrorOr SymName = Symbol.getName();
2198 if (std::error_code EC = SymName.getError())
2199 report_fatal_error(EC.message());
2200 const char *name = SymName->data();
21902201 return name;
21912202 }
21922203 }
22182229 if (indirect_symbol < Symtab.nsyms) {
22192230 symbol_iterator Sym = info->O->getSymbolByIndex(indirect_symbol);
22202231 SymbolRef Symbol = *Sym;
2221 StringRef SymName;
2222 Symbol.getName(SymName);
2223 const char *name = SymName.data();
2232 ErrorOr SymName = Symbol.getName();
2233 if (std::error_code EC = SymName.getError())
2234 report_fatal_error(EC.message());
2235 const char *name = SymName->data();
22242236 return name;
22252237 }
22262238 }
24462458 Symbol.getAddress(n_value);
24472459 if (n_value == UnknownAddress)
24482460 n_value = 0;
2449 StringRef name;
2450 Symbol.getName(name);
2451 if (!name.empty()) {
2452 SymbolName = name.data();
2461 ErrorOr NameOrError = Symbol.getName();
2462 if (std::error_code EC = NameOrError.getError())
2463 report_fatal_error(EC.message());
2464 StringRef Name = *NameOrError;
2465 if (!Name.empty()) {
2466 SymbolName = Name.data();
24532467 return SymbolName;
24542468 }
24552469 }
61186132 ST == SymbolRef::ST_Other) {
61196133 uint64_t Address;
61206134 Symbol.getAddress(Address);
6121 StringRef SymName;
6122 Symbol.getName(SymName);
6135 ErrorOr SymNameOrErr = Symbol.getName();
6136 if (std::error_code EC = SymNameOrErr.getError())
6137 report_fatal_error(EC.message());
6138 StringRef SymName = *SymNameOrErr;
61236139 AddrMap[Address] = SymName;
61246140 if (!DisSymName.empty() && DisSymName == SymName)
61256141 DisSymNameFound = true;
61586174
61596175 // Disassemble symbol by symbol.
61606176 for (unsigned SymIdx = 0; SymIdx != Symbols.size(); SymIdx++) {
6161 StringRef SymName;
6162 Symbols[SymIdx].getName(SymName);
6177 ErrorOr SymNameOrErr = Symbols[SymIdx].getName();
6178 if (std::error_code EC = SymNameOrErr.getError())
6179 report_fatal_error(EC.message());
6180 StringRef SymName = *SymNameOrErr;
61636181
61646182 SymbolRef::Type ST = Symbols[SymIdx].getType();
61656183 if (ST != SymbolRef::ST_Function)
64226440 const RelocationRef &Reloc, uint64_t Addr,
64236441 StringRef &Name, uint64_t &Addend) {
64246442 if (Reloc.getSymbol() != Obj->symbol_end()) {
6425 Reloc.getSymbol()->getName(Name);
6443 ErrorOr NameOrErr = Reloc.getSymbol()->getName();
6444 if (std::error_code EC = NameOrErr.getError())
6445 report_fatal_error(EC.message());
6446 Name = *NameOrErr;
64266447 Addend = Addr;
64276448 return;
64286449 }
64486469 Sym->second.getSection(SymSection);
64496470 if (RelocSection == *SymSection) {
64506471 // There's a valid symbol in the same section before this reference.
6451 Sym->second.getName(Name);
6472 ErrorOr NameOrErr = Sym->second.getName();
6473 if (std::error_code EC = NameOrErr.getError())
6474 report_fatal_error(EC.message());
6475 Name = *NameOrErr;
64526476 Addend = Addr - Sym->first;
64536477 return;
64546478 }
432432 const RelocationRef &Rel,
433433 SmallVectorImpl &Result) {
434434 symbol_iterator SymI = Rel.getSymbol();
435 StringRef SymName;
436 if (std::error_code EC = SymI->getName(SymName))
435 ErrorOr SymNameOrErr = SymI->getName();
436 if (std::error_code EC = SymNameOrErr.getError())
437437 return EC;
438 StringRef SymName = *SymNameOrErr;
438439 Result.append(SymName.begin(), SymName.end());
439440 return std::error_code();
440441 }
454455 for (const SymbolRef &Symbol : O->symbols()) {
455456 std::error_code ec;
456457 uint64_t Addr;
457 StringRef Name;
458 ErrorOr Name = Symbol.getName();
458459
459460 if ((ec = Symbol.getAddress(Addr)))
460461 report_fatal_error(ec.message());
461462 if (Addr != Val)
462463 continue;
463 if ((ec = Symbol.getName(Name)))
464 report_fatal_error(ec.message());
465 fmt << Name;
464 if (std::error_code EC = Name.getError())
465 report_fatal_error(EC.message());
466 fmt << *Name;
466467 return;
467468 }
468469
492493 if (isExtern) {
493494 symbol_iterator SI = O->symbol_begin();
494495 advance(SI, Val);
495 SI->getName(S);
496 ErrorOr SOrErr = SI->getName();
497 if (!error(SOrErr.getError()))
498 S = *SOrErr;
496499 } else {
497500 section_iterator SI = O->section_begin();
498501 // Adjust for the fact that sections are 1-indexed.
829832 if (Address >= SectSize)
830833 continue;
831834
832 StringRef Name;
833 if (error(Symbol.getName(Name)))
835 ErrorOr Name = Symbol.getName();
836 if (error(Name.getError()))
834837 break;
835 Symbols.push_back(std::make_pair(Address, Name));
838 Symbols.push_back(std::make_pair(Address, *Name));
836839 }
837840 }
838841
11201123 StringRef Name;
11211124 if (Type == SymbolRef::ST_Debug && Section != o->section_end()) {
11221125 Section->getName(Name);
1123 } else if (error(Symbol.getName(Name))) {
1124 continue;
1126 } else {
1127 ErrorOr NameOrErr = Symbol.getName();
1128 if (error(NameOrErr.getError()))
1129 continue;
1130 Name = *NameOrErr;
11251131 }
11261132
11271133 bool Global = Flags & SymbolRef::SF_Global;
566566 if (!Symbol)
567567 Symbol = getSymbol(COFF, Address, /*FunctionOnly=*/true);
568568
569 StringRef Name;
570 if (Symbol)
571 Symbol->getName(Name);
569 ErrorOr Name = Symbol->getName();
570 if (std::error_code EC = Name.getError())
571 report_fatal_error(EC.message());
572572
573573 ListScope EHS(SW, "ExceptionHandler");
574 SW.printString("Routine", formatSymbol(Name, Address));
574 SW.printString("Routine", formatSymbol(*Name, Address));
575575 SW.printHex("Parameter", Parameter);
576576 }
577577
600600 StringRef FunctionName;
601601 uint64_t FunctionAddress;
602602 if (Function) {
603 Function->getName(FunctionName);
603 ErrorOr FunctionNameOrErr = Function->getName();
604 if (std::error_code EC = FunctionNameOrErr.getError())
605 report_fatal_error(EC.message());
606 FunctionName = *FunctionNameOrErr;
604607 Function->getAddress(FunctionAddress);
605608 } else {
606609 const pe32_header *PEHeader;
612615 SW.printString("Function", formatSymbol(FunctionName, FunctionAddress));
613616
614617 if (XDataRecord) {
615 StringRef Name;
618 ErrorOr Name = XDataRecord->getName();
619 if (std::error_code EC = Name.getError())
620 report_fatal_error(EC.message());
621
616622 uint64_t Address;
617
618 XDataRecord->getName(Name);
619623 XDataRecord->getAddress(Address);
620624
621 SW.printString("ExceptionRecord", formatSymbol(Name, Address));
625 SW.printString("ExceptionRecord", formatSymbol(*Name, Address));
622626
623627 section_iterator SI = COFF.section_end();
624628 if (XDataRecord->getSection(SI))
657661 StringRef FunctionName;
658662 uint64_t FunctionAddress;
659663 if (Function) {
660 Function->getName(FunctionName);
664 ErrorOr FunctionNameOrErr = Function->getName();
665 if (std::error_code EC = FunctionNameOrErr.getError())
666 report_fatal_error(EC.message());
667 FunctionName = *FunctionNameOrErr;
661668 Function->getAddress(FunctionAddress);
662669 } else {
663670 const pe32_header *PEHeader;
138138 SymbolRef Symbol;
139139 if (std::error_code EC = resolveSymbol(Section, Offset, Symbol))
140140 return EC;
141 if (std::error_code EC = Symbol.getName(Name))
141 ErrorOr NameOrErr = Symbol.getName();
142 if (std::error_code EC = NameOrErr.getError())
142143 return EC;
144 Name = *NameOrErr;
143145 return std::error_code();
144146 }
145147
808810 StringRef SymbolName;
809811 Reloc.getTypeName(RelocName);
810812 symbol_iterator Symbol = Reloc.getSymbol();
811 if (Symbol != Obj->symbol_end() && error(Symbol->getName(SymbolName)))
812 return;
813 if (Symbol != Obj->symbol_end()) {
814 ErrorOr SymbolNameOrErr = Symbol->getName();
815 if (error(SymbolNameOrErr.getError()))
816 return;
817 SymbolName = *SymbolNameOrErr;
818 }
813819
814820 if (opts::ExpandRelocs) {
815821 DictScope Group(W, "Relocation");
473473 if (IsExtern) {
474474 symbol_iterator Symbol = Reloc.getSymbol();
475475 if (Symbol != Obj->symbol_end()) {
476 if (error(Symbol->getName(TargetName)))
476 ErrorOr TargetNameOrErr = Symbol->getName();
477 if (error(TargetNameOrErr.getError()))
477478 return;
479 TargetName = *TargetNameOrErr;
478480 }
479481 } else if (!IsScattered) {
480482 section_iterator SecI = Obj->getRelocationSection(DR);
537539
538540 void MachODumper::printSymbol(const SymbolRef &Symbol) {
539541 StringRef SymbolName;
540 if (Symbol.getName(SymbolName))
541 SymbolName = "";
542 if (ErrorOr SymbolNameOrErr = Symbol.getName())
543 SymbolName = *SymbolNameOrErr;
542544
543545 MachOSymbol MOSymbol;
544546 getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol);
117117 std::string Buffer;
118118 raw_string_ostream OS(Buffer);
119119
120 StringRef Name;
121120 SymbolRef Symbol;
122 if (Ctx.ResolveSymbol(Section, Offset, Symbol, Ctx.UserData) ||
123 Symbol.getName(Name)) {
124 OS << format(" (0x%" PRIX64 ")", Offset);
125 return OS.str();
126 }
127
128 OS << Name;
129 if (Displacement > 0)
130 OS << format(" +0x%X (0x%" PRIX64 ")", Displacement, Offset);
131 else
132 OS << format(" (0x%" PRIX64 ")", Offset);
121 if (!Ctx.ResolveSymbol(Section, Offset, Symbol, Ctx.UserData)) {
122 if (ErrorOr Name = Symbol.getName()) {
123 OS << *Name;
124 if (Displacement > 0)
125 OS << format(" +0x%X (0x%" PRIX64 ")", Displacement, Offset);
126 else
127 OS << format(" (0x%" PRIX64 ")", Offset);
128 return OS.str();
129 }
130 }
131
132 OS << format(" (0x%" PRIX64 ")", Offset);
133133 return OS.str();
134134 }
135135
265265 for (const auto &P : SymAddr) {
266266 object::SymbolRef Sym = P.first;
267267 if (Sym.getType() == object::SymbolRef::ST_Function) {
268 StringRef Name;
269 uint64_t Addr;
270 if (Sym.getName(Name))
268 ErrorOr Name = Sym.getName();
269 if (!Name)
271270 continue;
271 uint64_t Addr;
272272 if (Sym.getAddress(Addr))
273273 continue;
274274
287287 Addr += SectionLoadAddress - Sec->getAddress();
288288 }
289289
290 outs() << "Function: " << Name << ", Size = " << Size << ", Addr = " << Addr << "\n";
290 outs() << "Function: " << *Name << ", Size = " << Size
291 << ", Addr = " << Addr << "\n";
291292
292293 DILineInfoTable Lines = Context->getLineInfoForAddressRange(Addr, Size);
293294 DILineInfoTable::iterator Begin = Lines.begin();
9999 OpdExtractor->isValidOffsetForAddress(OpdOffset32))
100100 SymbolAddress = OpdExtractor->getAddress(&OpdOffset32);
101101 }
102 StringRef SymbolName;
103 if (error(Symbol.getName(SymbolName)))
102 ErrorOr SymbolNameOrErr = Symbol.getName();
103 if (error(SymbolNameOrErr.getError()))
104104 return;
105 StringRef SymbolName = *SymbolNameOrErr;
105106 // Mach-O symbol table names have leading underscore, skip it.
106107 if (Module->isMachO() && SymbolName.size() > 0 && SymbolName[0] == '_')
107108 SymbolName = SymbolName.drop_front();
119119 const object::coff_relocation *reloc = Obj.getCOFFRelocation(Reloc);
120120 COFFYAML::Relocation Rel;
121121 object::symbol_iterator Sym = Reloc.getSymbol();
122 Sym->getName(Rel.SymbolName);
122 ErrorOr SymbolNameOrErr = Sym->getName();
123 if (std::error_code EC = SymbolNameOrErr.getError())
124 report_fatal_error(EC.message());
125 Rel.SymbolName = *SymbolNameOrErr;
123126 Rel.VirtualAddress = reloc->VirtualAddress;
124127 Rel.Type = reloc->Type;
125128 Relocations.push_back(Rel);