llvm.org GIT mirror llvm / 317de7c
Thread Expected<...> up from libObject’s getSymbolAddress() for symbols to allow a good error message to be produced. This is nearly the last libObject interface that used ErrorOr and the last one that appears in llvm/include/llvm/Object/MachO.h . For Mach-O objects this is just a clean up because it’s version of getSymbolAddress() can’t return an error. I will leave it to the experts on COFF and ELF to actually add meaning full error messages in their tests if they wish. And also leave it to these experts to change the last two ErrorOr interfaces in llvm/include/llvm/Object/ObjectFile.h for createCOFFObjectFile() and createELFObjectFile() if they wish. Since there are no test cases for COFF and ELF error cases with respect to getSymbolAddress() in the test suite this is no functional change (NFC). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273701 91177308-0d34-0410-b5e6-96231b3b80d8 Kevin Enderby 3 years ago
18 changed file(s) with 92 addition(s) and 55 deletion(s). Raw diff Collapse all Expand all
721721 protected:
722722 void moveSymbolNext(DataRefImpl &Symb) const override;
723723 Expected getSymbolName(DataRefImpl Symb) const override;
724 ErrorOr getSymbolAddress(DataRefImpl Symb) const override;
724 Expected getSymbolAddress(DataRefImpl Symb) const override;
725725 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
726726 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
727727 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
206206
207207 void moveSymbolNext(DataRefImpl &Symb) const override;
208208 Expected getSymbolName(DataRefImpl Symb) const override;
209 ErrorOr getSymbolAddress(DataRefImpl Symb) const override;
209 Expected getSymbolAddress(DataRefImpl Symb) const override;
210210 uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
211211 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
212212 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
396396 }
397397
398398 template
399 ErrorOr
399 Expected
400400 ELFObjectFile::getSymbolAddress(DataRefImpl Symb) const {
401401 uint64_t Result = getSymbolValue(Symb);
402402 const Elf_Sym *ESym = getSymbol(Symb);
414414 ErrorOr SectionOrErr =
415415 EF.getSection(ESym, SymTab, ShndxTable);
416416 if (std::error_code EC = SectionOrErr.getError())
417 return EC;
417 return errorCodeToError(EC);
418418 const Elf_Shdr *Section = *SectionOrErr;
419419 if (Section)
420420 Result += Section->sh_addr;
204204 std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const;
205205 unsigned getSectionType(SectionRef Sec) const;
206206
207 ErrorOr getSymbolAddress(DataRefImpl Symb) const override;
207 Expected getSymbolAddress(DataRefImpl Symb) const override;
208208 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
209209 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
210210 Expected getSymbolType(DataRefImpl Symb) const override;
134134 Expected getName() const;
135135 /// Returns the symbol virtual address (i.e. address at which it will be
136136 /// mapped).
137 ErrorOr getAddress() const;
137 Expected getAddress() const;
138138
139139 /// Return the value of the symbol depending on the object this can be an
140140 /// offset or a virtual address.
197197 virtual Expected getSymbolName(DataRefImpl Symb) const = 0;
198198 std::error_code printSymbolName(raw_ostream &OS,
199199 DataRefImpl Symb) const override;
200 virtual ErrorOr getSymbolAddress(DataRefImpl Symb) const = 0;
200 virtual Expected getSymbolAddress(DataRefImpl Symb) const = 0;
201201 virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0;
202202 virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
203203 virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
311311 return getObject()->getSymbolName(getRawDataRefImpl());
312312 }
313313
314 inline ErrorOr SymbolRef::getAddress() const {
314 inline Expected SymbolRef::getAddress() const {
315315 return getObject()->getSymbolAddress(getRawDataRefImpl());
316316 }
317317
792792 // First calculate the address of the symbol or section as it appears
793793 // in the objct file
794794 if (Sym != Obj.symbol_end()) {
795 ErrorOr SymAddrOrErr = Sym->getAddress();
796 if (std::error_code EC = SymAddrOrErr.getError()) {
795 Expected SymAddrOrErr = Sym->getAddress();
796 if (!SymAddrOrErr) {
797 std::string Buf;
798 raw_string_ostream OS(Buf);
799 logAllUnhandledErrors(SymAddrOrErr.takeError(), OS, "");
800 OS.flush();
797801 errs() << "error: failed to compute symbol address: "
798 << EC.message() << '\n';
802 << Buf << '\n';
799803 continue;
800804 }
801805 SymAddr = *SymAddrOrErr;
124124 SymbolRef::Type SymbolType = *SymbolTypeOrErr;
125125 if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
126126 return std::error_code();
127 ErrorOr SymbolAddressOrErr = Symbol.getAddress();
128 if (auto EC = SymbolAddressOrErr.getError())
129 return EC;
127 Expected SymbolAddressOrErr = Symbol.getAddress();
128 if (!SymbolAddressOrErr)
129 return errorToErrorCode(SymbolAddressOrErr.takeError());
130130 uint64_t SymbolAddress = *SymbolAddressOrErr;
131131 if (OpdExtractor) {
132132 // For big-endian PowerPC64 ELF, symbols in the .opd section refer to
162162
163163 static Error getOffset(const SymbolRef &Sym, SectionRef Sec,
164164 uint64_t &Result) {
165 ErrorOr AddressOrErr = Sym.getAddress();
166 if (std::error_code EC = AddressOrErr.getError())
167 return errorCodeToError(EC);
165 Expected AddressOrErr = Sym.getAddress();
166 if (!AddressOrErr)
167 return AddressOrErr.takeError();
168168 Result = *AddressOrErr - Sec.getAddress();
169169 return Error::success();
170170 }
235235 if (auto AddrOrErr = I->getAddress())
236236 Addr = *AddrOrErr;
237237 else
238 return errorCodeToError(AddrOrErr.getError());
238 return AddrOrErr.takeError();
239239
240240 unsigned SectionID = AbsoluteSymbolSection;
241241
156156 return getCOFFSymbol(Ref).getValue();
157157 }
158158
159 ErrorOr COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
159 Expected COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
160160 uint64_t Result = getSymbolValue(Ref);
161161 COFFSymbolRef Symb = getCOFFSymbol(Ref);
162162 int32_t SectionNumber = Symb.getSectionNumber();
167167
168168 const coff_section *Section = nullptr;
169169 if (std::error_code EC = getSection(SectionNumber, Section))
170 return EC;
170 return errorCodeToError(EC);
171171 Result += Section->VirtualAddress;
172172
173173 // The section VirtualAddress does not include ImageBase, and we want to
472472 return getNValue(Sym);
473473 }
474474
475 ErrorOr MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const {
475 Expected MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const {
476476 return getSymbolValue(Sym);
477477 }
478478
194194 }
195195
196196 uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
197 ErrorOr Ret = (*unwrap(SI))->getAddress();
198 if (std::error_code EC = Ret.getError())
199 report_fatal_error(EC.message());
197 Expected Ret = (*unwrap(SI))->getAddress();
198 if (!Ret) {
199 std::string Buf;
200 raw_string_ostream OS(Buf);
201 logAllUnhandledErrors(Ret.takeError(), OS, "");
202 OS.flush();
203 report_fatal_error(Buf);
204 }
200205 return *Ret;
201206 }
202207
189189 continue;
190190 StringRef SecContents;
191191 error(Sec.getContents(SecContents));
192 ErrorOr SymAddressOrErr = Sym.getAddress();
193 error(SymAddressOrErr.getError());
192 Expected SymAddressOrErr = Sym.getAddress();
193 error(errorToErrorCode(SymAddressOrErr.takeError()));
194194 uint64_t SymAddress = *SymAddressOrErr;
195195 uint64_t SecAddress = Sec.getAddress();
196196 uint64_t SecSize = Sec.getSize();
10141014 }
10151015 if (PrintAddress && isa(Obj)) {
10161016 SymbolRef SymRef(Sym);
1017 ErrorOr AddressOrErr = SymRef.getAddress();
1018 if (error(AddressOrErr.getError()))
1017 Expected AddressOrErr = SymRef.getAddress();
1018 if (!AddressOrErr) {
1019 consumeError(AddressOrErr.takeError());
10191020 break;
1021 }
10201022 S.Address = *AddressOrErr;
10211023 }
10221024 S.TypeChar = getNMTypeChar(Obj, Sym);
160160 resolveSectionAndAddress(const COFFObjectFile *Obj, const SymbolRef &Sym,
161161 const coff_section *&ResolvedSection,
162162 uint64_t &ResolvedAddr) {
163 ErrorOr ResolvedAddrOrErr = Sym.getAddress();
164 if (std::error_code EC = ResolvedAddrOrErr.getError())
165 return EC;
163 Expected ResolvedAddrOrErr = Sym.getAddress();
164 if (!ResolvedAddrOrErr)
165 return errorToErrorCode(ResolvedAddrOrErr.takeError());
166166 ResolvedAddr = *ResolvedAddrOrErr;
167167 Expected Iter = Sym.getSection();
168168 if (!Iter)
648648
649649 for (const SymbolRef &Symbol : O->symbols()) {
650650 std::error_code ec;
651 ErrorOr Addr = Symbol.getAddress();
652 if ((ec = Addr.getError()))
653 report_fatal_error(ec.message());
651 Expected Addr = Symbol.getAddress();
652 if (!Addr) {
653 std::string Buf;
654 raw_string_ostream OS(Buf);
655 logAllUnhandledErrors(Addr.takeError(), OS, "");
656 OS.flush();
657 report_fatal_error(Buf);
658 }
654659 if (*Addr != Val)
655660 continue;
656661 Expected Name = Symbol.getName();
991996 typedef std::vector> SectionSymbolsTy;
992997 std::map AllSymbols;
993998 for (const SymbolRef &Symbol : Obj->symbols()) {
994 ErrorOr AddressOrErr = Symbol.getAddress();
995 error(AddressOrErr.getError());
999 Expected AddressOrErr = Symbol.getAddress();
1000 error(errorToErrorCode(AddressOrErr.takeError()));
9961001 uint64_t Address = *AddressOrErr;
9971002
9981003 Expected Name = Symbol.getName();
13881393 return;
13891394 }
13901395 for (const SymbolRef &Symbol : o->symbols()) {
1391 ErrorOr AddressOrError = Symbol.getAddress();
1392 error(AddressOrError.getError());
1396 Expected AddressOrError = Symbol.getAddress();
1397 if (!AddressOrError)
1398 report_error(ArchiveName, o->getFileName(), AddressOrError.takeError());
13931399 uint64_t Address = *AddressOrError;
13941400 Expected TypeOrError = Symbol.getType();
13951401 if (!TypeOrError)
203203 if (FunctionOnly && *Type != SymbolRef::ST_Function)
204204 continue;
205205
206 ErrorOr Address = Symbol.getAddress();
207 if (std::error_code EC = Address.getError())
208 return EC;
206 Expected Address = Symbol.getAddress();
207 if (!Address)
208 return errorToErrorCode(Address.takeError());
209209 if (*Address == VA)
210210 return Symbol;
211211 }
617617 report_fatal_error(Buf);
618618 }
619619 FunctionName = *FunctionNameOrErr;
620 ErrorOr FunctionAddressOrErr = Function->getAddress();
621 if (std::error_code EC = FunctionAddressOrErr.getError())
622 report_fatal_error(EC.message());
620 Expected FunctionAddressOrErr = Function->getAddress();
621 if (!FunctionAddressOrErr) {
622 std::string Buf;
623 llvm::raw_string_ostream OS(Buf);
624 logAllUnhandledErrors(FunctionAddressOrErr.takeError(), OS, "");
625 OS.flush();
626 report_fatal_error(Buf);
627 }
623628 FunctionAddress = *FunctionAddressOrErr;
624629 } else {
625630 const pe32_header *PEHeader;
640645 report_fatal_error(Buf);
641646 }
642647
643 ErrorOr AddressOrErr = XDataRecord->getAddress();
644 if (std::error_code EC = AddressOrErr.getError())
645 report_fatal_error(EC.message());
648 Expected AddressOrErr = XDataRecord->getAddress();
649 if (!AddressOrErr) {
650 std::string Buf;
651 llvm::raw_string_ostream OS(Buf);
652 logAllUnhandledErrors(AddressOrErr.takeError(), OS, "");
653 OS.flush();
654 report_fatal_error(Buf);
655 }
646656 uint64_t Address = *AddressOrErr;
647657
648658 SW.printString("ExceptionRecord", formatSymbol(*Name, Address));
697707 report_fatal_error(Buf);
698708 }
699709 FunctionName = *FunctionNameOrErr;
700 ErrorOr FunctionAddressOrErr = Function->getAddress();
710 Expected FunctionAddressOrErr = Function->getAddress();
711 if (!FunctionAddressOrErr) {
712 std::string Buf;
713 llvm::raw_string_ostream OS(Buf);
714 logAllUnhandledErrors(FunctionAddressOrErr.takeError(), OS, "");
715 OS.flush();
716 report_fatal_error(Buf);
717 }
701718 FunctionAddress = *FunctionAddressOrErr;
702719 } else {
703720 const pe32_header *PEHeader;
147147 Ctx.ResolveSymbol(Section, Offset, Symbol, Ctx.UserData))
148148 return EC;
149149
150 ErrorOr ResolvedAddressOrErr = Symbol.getAddress();
151 if (std::error_code EC = ResolvedAddressOrErr.getError())
152 return EC;
150 Expected ResolvedAddressOrErr = Symbol.getAddress();
151 if (!ResolvedAddressOrErr)
152 return errorToErrorCode(ResolvedAddressOrErr.takeError());
153153 ResolvedAddress = *ResolvedAddressOrErr;
154154
155155 Expected SI = Symbol.getSection();
348348 consumeError(Name.takeError());
349349 continue;
350350 }
351 ErrorOr AddrOrErr = Sym.getAddress();
352 if (!AddrOrErr)
351 Expected AddrOrErr = Sym.getAddress();
352 if (!AddrOrErr) {
353 // TODO: Actually report errors helpfully.
354 consumeError(AddrOrErr.takeError());
353355 continue;
356 }
354357 uint64_t Addr = *AddrOrErr;
355358
356359 uint64_t Size = P.second;
320320 std::set Result;
321321
322322 for (const object::SymbolRef &Symbol : O.symbols()) {
323 ErrorOr AddressOrErr = Symbol.getAddress();
324 FailIfError(AddressOrErr);
323 Expected AddressOrErr = Symbol.getAddress();
324 FailIfError(errorToErrorCode(AddressOrErr.takeError()));
325325
326326 Expected NameOrErr = Symbol.getName();
327327 FailIfError(errorToErrorCode(NameOrErr.takeError()));