llvm.org GIT mirror llvm / e84d8c1
Convert getSymbolSection to return an ErrorOr. This function can actually fail since the symbol contains an index to the section and that can be invalid. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@244375 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
22 changed file(s) with 84 addition(s) and 92 deletion(s). Raw diff Collapse all Expand all
652652 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
653653 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
654654 SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
655 std::error_code getSymbolSection(DataRefImpl Symb,
656 section_iterator &Res) const override;
655 ErrorOr getSymbolSection(DataRefImpl Symb) const override;
657656 void moveSectionNext(DataRefImpl &Sec) const override;
658657 std::error_code getSectionName(DataRefImpl Sec,
659658 StringRef &Res) const override;
203203 uint8_t getSymbolOther(DataRefImpl Symb) const override;
204204 uint8_t getSymbolELFType(DataRefImpl Symb) const override;
205205 SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
206 section_iterator getSymbolSection(const Elf_Sym *Symb) const;
207 std::error_code getSymbolSection(DataRefImpl Symb,
208 section_iterator &Res) const override;
206 ErrorOr getSymbolSection(const Elf_Sym *Symb) const;
207 ErrorOr getSymbolSection(DataRefImpl Symb) const override;
209208
210209 void moveSectionNext(DataRefImpl &Sec) const override;
211210 std::error_code getSectionName(DataRefImpl Sec,
504503 }
505504
506505 template
507 section_iterator
506 ErrorOr
508507 ELFObjectFile::getSymbolSection(const Elf_Sym *ESym) const {
509508 ErrorOr ESecOrErr = EF.getSection(ESym);
510509 if (std::error_code EC = ESecOrErr.getError())
511 report_fatal_error(EC.message());
510 return EC;
512511
513512 const Elf_Shdr *ESec = *ESecOrErr;
514513 if (!ESec)
520519 }
521520
522521 template
523 std::error_code
524 ELFObjectFile::getSymbolSection(DataRefImpl Symb,
525 section_iterator &Res) const {
526 Res = getSymbolSection(getSymbol(Symb));
527 return std::error_code();
522 ErrorOr
523 ELFObjectFile::getSymbolSection(DataRefImpl Symb) const {
524 return getSymbolSection(getSymbol(Symb));
528525 }
529526
530527 template
209209 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
210210 SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
211211 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
212 std::error_code getSymbolSection(DataRefImpl Symb,
213 section_iterator &Res) const override;
212 ErrorOr getSymbolSection(DataRefImpl Symb) const override;
214213 unsigned getSymbolSectionID(SymbolRef Symb) const;
215214 unsigned getSectionID(SectionRef Sec) const;
216215
146146
147147 /// @brief Get section this symbol is defined in reference to. Result is
148148 /// end_sections() if it is undefined or is an absolute symbol.
149 std::error_code getSection(section_iterator &Result) const;
149 ErrorOr getSection() const;
150150
151151 const ObjectFile *getObject() const;
152152 };
201201 virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
202202 virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
203203 virtual SymbolRef::Type getSymbolType(DataRefImpl Symb) const = 0;
204 virtual std::error_code getSymbolSection(DataRefImpl Symb,
205 section_iterator &Res) const = 0;
204 virtual ErrorOr
205 getSymbolSection(DataRefImpl Symb) const = 0;
206206
207207 // Same as above for SectionRef.
208208 friend class SectionRef;
322322 return getObject()->getCommonSymbolSize(getRawDataRefImpl());
323323 }
324324
325 inline std::error_code SymbolRef::getSection(section_iterator &Result) const {
326 return getObject()->getSymbolSection(getRawDataRefImpl(), Result);
325 inline ErrorOr SymbolRef::getSection() const {
326 return getObject()->getSymbolSection(getRawDataRefImpl());
327327 }
328328
329329 inline SymbolRef::Type SymbolRef::getType() const {
684684 }
685685 SymAddr = *SymAddrOrErr;
686686 // Also remember what section this symbol is in for later
687 Sym->getSection(RSec);
687 RSec = *Sym->getSection();
688688 } else if (auto *MObj = dyn_cast(&Obj)) {
689689 // MachO also has relocations that point to sections and
690690 // scattered relocations.
163163 ErrorOr NameOrErr = I->getName();
164164 Check(NameOrErr.getError());
165165 StringRef Name = *NameOrErr;
166 section_iterator SI = Obj.section_end();
167 Check(I->getSection(SI));
166 ErrorOr SIOrErr = I->getSection();
167 Check(SIOrErr.getError());
168 section_iterator SI = *SIOrErr;
168169 if (SI == Obj.section_end())
169170 continue;
170171 uint64_t SectOffset;
844844 if (Rel.Addend != (int64_t)TargetSymbolOffset)
845845 continue;
846846
847 section_iterator tsi(Obj.section_end());
848 check(TargetSymbol->getSection(tsi));
847 ErrorOr TSIOrErr = TargetSymbol->getSection();
848 check(TSIOrErr.getError());
849 section_iterator tsi = *TSIOrErr;
849850 bool IsCode = tsi->isText();
850851 Rel.SectionID = findOrEmitSection(Obj, (*tsi), IsCode, LocalSections);
851852 Rel.Addend = (intptr_t)Addend;
11611162 // TODO: Now ELF SymbolRef::ST_Debug = STT_SECTION, it's not obviously
11621163 // and can be changed by another developers. Maybe best way is add
11631164 // a new symbol type ST_Section to SymbolRef and use it.
1164 section_iterator si(Obj.section_end());
1165 Symbol->getSection(si);
1165 section_iterator si = *Symbol->getSection();
11661166 if (si == Obj.section_end())
11671167 llvm_unreachable("Symbol section not found, bad object file format!");
11681168 DEBUG(dbgs() << "\t\tThis is section symbol\n");
118118 symbol_iterator Symbol = RelI->getSymbol();
119119 if (Symbol == Obj.symbol_end())
120120 report_fatal_error("Unknown symbol in relocation");
121 section_iterator SecI(Obj.section_end());
122 Symbol->getSection(SecI);
121 section_iterator SecI = *Symbol->getSection();
123122 // If there is no section, this must be an external reference.
124123 const bool IsExtern = SecI == Obj.section_end();
125124
237237 return Symb.getValue();
238238 }
239239
240 std::error_code
241 COFFObjectFile::getSymbolSection(DataRefImpl Ref,
242 section_iterator &Result) const {
240 ErrorOr
241 COFFObjectFile::getSymbolSection(DataRefImpl Ref) const {
243242 COFFSymbolRef Symb = getCOFFSymbol(Ref);
244 if (COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
245 Result = section_end();
246 } else {
247 const coff_section *Sec = nullptr;
248 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
249 return EC;
250 DataRefImpl Ref;
251 Ref.p = reinterpret_cast(Sec);
252 Result = section_iterator(SectionRef(Ref, this));
253 }
254 return std::error_code();
243 if (COFF::isReservedSectionNumber(Symb.getSectionNumber()))
244 return section_end();
245 const coff_section *Sec = nullptr;
246 if (std::error_code EC = getSection(Symb.getSectionNumber(), Sec))
247 return EC;
248 DataRefImpl Ret;
249 Ret.p = reinterpret_cast(Sec);
250 return section_iterator(SectionRef(Ret, this));
255251 }
256252
257253 unsigned COFFObjectFile::getSymbolSectionID(SymbolRef Sym) const {
444444 return Result;
445445 }
446446
447 std::error_code MachOObjectFile::getSymbolSection(DataRefImpl Symb,
448 section_iterator &Res) const {
447 ErrorOr
448 MachOObjectFile::getSymbolSection(DataRefImpl Symb) const {
449449 MachO::nlist_base Entry = getSymbolTableEntryBase(this, Symb);
450450 uint8_t index = Entry.n_sect;
451451
452 if (index == 0) {
453 Res = section_end();
454 } else {
455 DataRefImpl DRI;
456 DRI.d.a = index - 1;
457 if (DRI.d.a >= Sections.size())
458 report_fatal_error("getSymbolSection: Invalid section index.");
459 Res = section_iterator(SectionRef(DRI, this));
460 }
461
462 return std::error_code();
452 if (index == 0)
453 return section_end();
454 DataRefImpl DRI;
455 DRI.d.a = index - 1;
456 if (DRI.d.a >= Sections.size())
457 report_fatal_error("getSymbolSection: Invalid section index.");
458 return section_iterator(SectionRef(DRI, this));
463459 }
464460
465461 unsigned MachOObjectFile::getSymbolSectionID(SymbolRef Sym) const {
9797
9898 void LLVMMoveToContainingSection(LLVMSectionIteratorRef Sect,
9999 LLVMSymbolIteratorRef Sym) {
100 if (std::error_code ec = (*unwrap(Sym))->getSection(*unwrap(Sect)))
100 ErrorOr SecOrErr = (*unwrap(Sym))->getSection();
101 if (std::error_code ec = SecOrErr.getError())
101102 report_fatal_error(ec.message());
103 *unwrap(Sect) = *SecOrErr;
102104 }
103105
104106 // ObjectFile Symbol iterators
2828 : SymbolicFile(Type, Source) {}
2929
3030 bool SectionRef::containsSymbol(SymbolRef S) const {
31 section_iterator SymSec = getObject()->section_end();
32 if (S.getSection(SymSec))
31 ErrorOr SymSec = S.getSection();
32 if (!SymSec)
3333 return false;
34 return *this == *SymSec;
34 return *this == **SymSec;
3535 }
3636
3737 uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref) const {
264264 // are the only ones that need to be queried because the address
265265 // of common data won't be described in the debug map. All other
266266 // addresses should be fetched for the debug map.
267 if (!(Sym.getFlags() & SymbolRef::SF_Global) || Sym.getSection(Section) ||
268 Section == MainBinary.section_end() || Section->isText())
267 if (!(Sym.getFlags() & SymbolRef::SF_Global))
268 continue;
269 ErrorOr SectionOrErr = Sym.getSection();
270 if (!SectionOrErr)
271 continue;
272 Section = *SectionOrErr;
273 if (Section == MainBinary.section_end() || Section->isText())
269274 continue;
270275 uint64_t Addr = Sym.getValue();
271276 ErrorOr NameOrErr = Sym.getName();
183183 ErrorOr SymNameOrErr = Sym.getName();
184184 error(SymNameOrErr.getError());
185185 StringRef SymName = *SymNameOrErr;
186 object::section_iterator SecI(Obj->section_begin());
187 error(Sym.getSection(SecI));
186 ErrorOr SecIOrErr = Sym.getSection();
187 error(SecIOrErr.getError());
188 object::section_iterator SecI = *SecIOrErr;
188189 // Skip external symbols.
189190 if (SecI == Obj->section_end())
190191 continue;
313313 outs() << "(indirect) ";
314314 break;
315315 case MachO::N_SECT: {
316 section_iterator Sec = MachO->section_end();
317 MachO->getSymbolSection(I->Sym.getRawDataRefImpl(), Sec);
316 section_iterator Sec = *MachO->getSymbolSection(I->Sym.getRawDataRefImpl());
318317 DataRefImpl Ref = Sec->getRawDataRefImpl();
319318 StringRef SectionName;
320319 MachO->getSectionName(Ref, SectionName);
593592 // OK, this is ELF
594593 elf_symbol_iterator SymI(I);
595594
596 elf_section_iterator SecI = Obj.section_end();
597 if (error(SymI->getSection(SecI)))
595 ErrorOr SecIOrErr = SymI->getSection();
596 if (error(SecIOrErr.getError()))
598597 return '?';
599598
599 elf_section_iterator SecI = *SecIOrErr;
600600 if (SecI != Obj.section_end()) {
601601 switch (SecI->getType()) {
602602 case ELF::SHT_PROGBITS:
650650
651651 uint32_t Characteristics = 0;
652652 if (!COFF::isReservedSectionNumber(Symb.getSectionNumber())) {
653 section_iterator SecI = Obj.section_end();
654 if (error(SymI->getSection(SecI)))
653 ErrorOr SecIOrErr = SymI->getSection();
654 if (error(SecIOrErr.getError()))
655655 return '?';
656 section_iterator SecI = *SecIOrErr;
656657 const coff_section *Section = Obj.getCOFFSection(*SecI);
657658 Characteristics = Section->Characteristics;
658659 }
700701 case MachO::N_INDR:
701702 return 'i';
702703 case MachO::N_SECT: {
703 section_iterator Sec = Obj.section_end();
704 Obj.getSymbolSection(Symb, Sec);
704 section_iterator Sec = *Obj.getSymbolSection(Symb);
705705 DataRefImpl Ref = Sec->getRawDataRefImpl();
706706 StringRef SectionName;
707707 Obj.getSectionName(Ref, SectionName);
164164 if (std::error_code EC = ResolvedAddrOrErr.getError())
165165 return EC;
166166 ResolvedAddr = *ResolvedAddrOrErr;
167 section_iterator iter(Obj->section_begin());
168 if (std::error_code EC = Sym.getSection(iter))
169 return EC;
170 ResolvedSection = Obj->getCOFFSection(*iter);
167 ErrorOr Iter = Sym.getSection();
168 if (std::error_code EC = Iter.getError())
169 return EC;
170 ResolvedSection = Obj->getCOFFSection(**Iter);
171171 return std::error_code();
172172 }
173173
64356435 // Go back one so that SymbolAddress <= Addr.
64366436 --Sym;
64376437
6438 section_iterator SymSection = Obj->section_end();
6439 Sym->second.getSection(SymSection);
6438 section_iterator SymSection = *Sym->second.getSection();
64406439 if (RelocSection == *SymSection) {
64416440 // There's a valid symbol in the same section before this reference.
64426441 ErrorOr NameOrErr = Sym->second.getName();
67796778 for (const SymbolRef &SymRef : Obj->symbols()) {
67806779 // Discard any undefined or absolute symbols. They're not going to take part
67816780 // in the convenience lookup for unwind info and just take up resources.
6782 section_iterator Section = Obj->section_end();
6783 SymRef.getSection(Section);
6781 section_iterator Section = *SymRef.getSection();
67846782 if (Section == Obj->section_end())
67856783 continue;
67866784
12241224 uint64_t Address = *AddressOrError;
12251225 SymbolRef::Type Type = Symbol.getType();
12261226 uint32_t Flags = Symbol.getFlags();
1227 section_iterator Section = o->section_end();
1228 error(Symbol.getSection(Section));
1227 ErrorOr SectionOrErr = Symbol.getSection();
1228 error(SectionOrErr.getError());
1229 section_iterator Section = *SectionOrErr;
12291230 StringRef Name;
12301231 if (Type == SymbolRef::ST_Debug && Section != o->section_end()) {
12311232 Section->getName(Name);
629629
630630 SW.printString("ExceptionRecord", formatSymbol(*Name, Address));
631631
632 section_iterator SI = COFF.section_end();
633 if (XDataRecord->getSection(SI))
632 ErrorOr SIOrErr = XDataRecord->getSection();
633 if (!SIOrErr)
634634 return false;
635 section_iterator SI = *SIOrErr;
635636
636637 return dumpXDataRecord(COFF, *SI, FunctionAddress, Address);
637638 } else {
541541 getSymbol(Obj, Symbol.getRawDataRefImpl(), MOSymbol);
542542
543543 StringRef SectionName = "";
544 section_iterator SecI(Obj->section_begin());
545 error(Symbol.getSection(SecI));
544 ErrorOr SecIOrErr = Symbol.getSection();
545 error(SecIOrErr.getError());
546 section_iterator SecI = *SecIOrErr;
546547 if (SecI != Obj->section_end())
547548 error(SecI->getName(SectionName));
548549
148148 return EC;
149149 ResolvedAddress = *ResolvedAddressOrErr;
150150
151 section_iterator SI = Ctx.COFF.section_begin();
152 if (std::error_code EC = Symbol.getSection(SI))
153 return EC;
154
155 ResolvedSection = Ctx.COFF.getCOFFSection(*SI);
151 ErrorOr SI = Symbol.getSection();
152 ResolvedSection = Ctx.COFF.getCOFFSection(**SI);
156153 return std::error_code();
157154 }
158155
303303 // symbol in memory (rather than that in the unrelocated object file)
304304 // and use that to query the DWARFContext.
305305 if (!UseDebugObj && LoadObjects) {
306 object::section_iterator Sec(SymbolObj->section_end());
307 Sym.getSection(Sec);
306 object::section_iterator Sec = *Sym.getSection();
308307 StringRef SecName;
309308 Sec->getName(SecName);
310309 uint64_t SectionLoadAddress =