llvm.org GIT mirror llvm / 5954faa
Return ErrorOr from getSymbolAddress. It can fail trying to get the section on ELF and COFF. This makes sure the error is handled. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241366 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
18 changed file(s) with 82 addition(s) and 63 deletion(s). Raw diff Collapse all Expand all
647647 protected:
648648 void moveSymbolNext(DataRefImpl &Symb) const override;
649649 ErrorOr getSymbolName(DataRefImpl Symb) const override;
650 std::error_code getSymbolAddress(DataRefImpl Symb,
651 uint64_t &Res) const override;
650 ErrorOr getSymbolAddress(DataRefImpl Symb) const override;
652651 uint64_t getSymbolValue(DataRefImpl Symb) const override;
653652 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
654653 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
195195
196196 void moveSymbolNext(DataRefImpl &Symb) const override;
197197 ErrorOr getSymbolName(DataRefImpl Symb) const override;
198 std::error_code getSymbolAddress(DataRefImpl Symb,
199 uint64_t &Res) const override;
198 ErrorOr getSymbolAddress(DataRefImpl Symb) const override;
200199 uint64_t getSymbolValue(DataRefImpl Symb) const override;
201200 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
202201 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
399398 }
400399
401400 template
402 std::error_code ELFObjectFile::getSymbolAddress(DataRefImpl Symb,
403 uint64_t &Result) const {
404 Result = getSymbolValue(Symb);
401 ErrorOr
402 ELFObjectFile::getSymbolAddress(DataRefImpl Symb) const {
403 uint64_t Result = getSymbolValue(Symb);
405404 const Elf_Sym *ESym = getSymbol(Symb);
406405 switch (ESym->st_shndx) {
407406 case ELF::SHN_COMMON:
408407 case ELF::SHN_UNDEF:
409408 case ELF::SHN_ABS:
410 return std::error_code();
409 return Result;
411410 }
412411
413412 const Elf_Ehdr *Header = EF.getHeader();
421420 Result += Section->sh_addr;
422421 }
423422
424 return std::error_code();
423 return Result;
425424 }
426425
427426 template
204204 std::error_code getIndirectName(DataRefImpl Symb, StringRef &Res) const;
205205 unsigned getSectionType(SectionRef Sec) const;
206206
207 std::error_code getSymbolAddress(DataRefImpl Symb,
208 uint64_t &Res) const override;
207 ErrorOr getSymbolAddress(DataRefImpl Symb) const override;
209208 uint64_t getSymbolValue(DataRefImpl Symb) const override;
210209 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
211210 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
134134 ErrorOr getName() const;
135135 /// Returns the symbol virtual address (i.e. address at which it will be
136136 /// mapped).
137 std::error_code getAddress(uint64_t &Result) const;
137 ErrorOr 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 ErrorOr getSymbolName(DataRefImpl Symb) const = 0;
198198 std::error_code printSymbolName(raw_ostream &OS,
199199 DataRefImpl Symb) const override;
200 virtual std::error_code getSymbolAddress(DataRefImpl Symb,
201 uint64_t &Res) const = 0;
200 virtual ErrorOr getSymbolAddress(DataRefImpl Symb) const = 0;
202201 virtual uint64_t getSymbolValue(DataRefImpl Symb) const = 0;
203202 virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
204203 virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
307306 return getObject()->getSymbolName(getRawDataRefImpl());
308307 }
309308
310 inline std::error_code SymbolRef::getAddress(uint64_t &Result) const {
311 return getObject()->getSymbolAddress(getRawDataRefImpl(), Result);
309 inline ErrorOr SymbolRef::getAddress() const {
310 return getObject()->getSymbolAddress(getRawDataRefImpl());
312311 }
313312
314313 inline uint64_t SymbolRef::getValue() const {
676676 // First calculate the address of the symbol or section as it appears
677677 // in the objct file
678678 if (Sym != Obj.symbol_end()) {
679 Sym->getAddress(SymAddr);
679 ErrorOr SymAddrOrErr = Sym->getAddress();
680 if (std::error_code EC = SymAddrOrErr.getError()) {
681 errs() << "error: failed to compute symbol address: "
682 << EC.message() << '\n';
683 continue;
684 }
685 SymAddr = *SymAddrOrErr;
680686 // Also remember what section this symbol is in for later
681687 Sym->getSection(RSec);
682688 } else if (auto *MObj = dyn_cast(&Obj)) {
113113 }
114114
115115 static std::error_code getOffset(const SymbolRef &Sym, uint64_t &Result) {
116 uint64_t Address;
117 if (std::error_code EC = Sym.getAddress(Address))
116 ErrorOr AddressOrErr = Sym.getAddress();
117 if (std::error_code EC = AddressOrErr.getError())
118118 return EC;
119 uint64_t Address = *AddressOrErr;
119120
120121 if (Address == UnknownAddress) {
121122 Result = UnknownAddress;
162162 return Sym.getValue();
163163 }
164164
165 std::error_code COFFObjectFile::getSymbolAddress(DataRefImpl Ref,
166 uint64_t &Result) const {
167 Result = getSymbolValue(Ref);
165 ErrorOr COFFObjectFile::getSymbolAddress(DataRefImpl Ref) const {
166 uint64_t Result = getSymbolValue(Ref);
168167 COFFSymbolRef Symb = getCOFFSymbol(Ref);
169168 int32_t SectionNumber = Symb.getSectionNumber();
170169
171170 if (Symb.isAnyUndefined() || Symb.isCommon() ||
172171 COFF::isReservedSectionNumber(SectionNumber))
173 return std::error_code();
172 return Result;
174173
175174 const coff_section *Section = nullptr;
176175 if (std::error_code EC = getSection(SectionNumber, Section))
177176 return EC;
178177 Result += Section->VirtualAddress;
179 return std::error_code();
178 return Result;
180179 }
181180
182181 SymbolRef::Type COFFObjectFile::getSymbolType(DataRefImpl Ref) const {
375375 return NValue;
376376 }
377377
378 std::error_code MachOObjectFile::getSymbolAddress(DataRefImpl Sym,
379 uint64_t &Res) const {
380 Res = getSymbolValue(Sym);
381 return std::error_code();
378 ErrorOr MachOObjectFile::getSymbolAddress(DataRefImpl Sym) const {
379 return getSymbolValue(Sym);
382380 }
383381
384382 uint32_t MachOObjectFile::getSymbolAlignment(DataRefImpl DRI) const {
179179 }
180180
181181 uint64_t LLVMGetSymbolAddress(LLVMSymbolIteratorRef SI) {
182 uint64_t ret;
183 if (std::error_code ec = (*unwrap(SI))->getAddress(ret))
184 report_fatal_error(ec.message());
185 return ret;
182 ErrorOr Ret = (*unwrap(SI))->getAddress();
183 if (std::error_code EC = Ret.getError())
184 report_fatal_error(EC.message());
185 return *Ret;
186186 }
187187
188188 uint64_t LLVMGetSymbolSize(LLVMSymbolIteratorRef SI) {
3333 report_fatal_error(EC.message());
3434 StringRef SymName = *SymNameOrErr;
3535
36 uint64_t SymAddr; SymI->getAddress(SymAddr);
36 ErrorOr SymAddr = SymI->getAddress();
37 if (std::error_code EC = SymAddr.getError())
38 report_fatal_error(EC.message());
3739 uint64_t SymSize = SymI->getSize();
3840 int64_t Addend = *ELFRelocationRef(Rel).getAddend();
3941
4042 MCSymbol *Sym = Ctx.getOrCreateSymbol(SymName);
4143 // FIXME: check that the value is actually the same.
4244 if (!Sym->isVariable())
43 Sym->setVariableValue(MCConstantExpr::create(SymAddr, Ctx));
45 Sym->setVariableValue(MCConstantExpr::create(*SymAddr, Ctx));
4446
4547 const MCExpr *Expr = nullptr;
4648 // If hasAddend is true, then we need to add Addend (r_addend) to Expr.
206206 StringRef SecContents;
207207 if (error(Sec.getContents(SecContents)))
208208 return;
209 uint64_t SymAddress;
210 if (error(Sym.getAddress(SymAddress)))
209 ErrorOr SymAddressOrErr = Sym.getAddress();
210 if (error(SymAddressOrErr.getError()))
211211 return;
212 uint64_t SymAddress = *SymAddressOrErr;
212213 uint64_t SecAddress = Sec.getAddress();
213214 uint64_t SecSize = Sec.getSize();
214215 uint64_t SymOffset = SymAddress - SecAddress;
900900 S.Size = ELFSymbolRef(Sym).getSize();
901901 }
902902 if (PrintAddress && isa(Obj)) {
903 if (error(SymbolRef(Sym).getAddress(S.Address)))
903 ErrorOr AddressOrErr = SymbolRef(Sym).getAddress();
904 if (error(AddressOrErr.getError()))
904905 break;
906 S.Address = *AddressOrErr;
905907 }
906908 S.TypeChar = getNMTypeChar(Obj, Sym);
907909 if (error(Sym.printName(OS)))
160160 resolveSectionAndAddress(const COFFObjectFile *Obj, const SymbolRef &Sym,
161161 const coff_section *&ResolvedSection,
162162 uint64_t &ResolvedAddr) {
163 if (std::error_code EC = Sym.getAddress(ResolvedAddr))
164 return EC;
163 ErrorOr ResolvedAddrOrErr = Sym.getAddress();
164 if (std::error_code EC = ResolvedAddrOrErr.getError())
165 return EC;
166 ResolvedAddr = *ResolvedAddrOrErr;
165167 section_iterator iter(Obj->section_begin());
166168 if (std::error_code EC = Sym.getSection(iter))
167169 return EC;
454454
455455 for (const SymbolRef &Symbol : O->symbols()) {
456456 std::error_code ec;
457 uint64_t Addr;
457 ErrorOr Addr = Symbol.getAddress();
458 if ((ec = Addr.getError()))
459 report_fatal_error(ec.message());
460 if (*Addr != Val)
461 continue;
458462 ErrorOr Name = Symbol.getName();
459
460 if ((ec = Symbol.getAddress(Addr)))
461 report_fatal_error(ec.message());
462 if (Addr != Val)
463 continue;
464463 if (std::error_code EC = Name.getError())
465464 report_fatal_error(EC.message());
466465 fmt << *Name;
823822 std::vector> Symbols;
824823 for (const SymbolRef &Symbol : Obj->symbols()) {
825824 if (Section.containsSymbol(Symbol)) {
826 uint64_t Address;
827 if (error(Symbol.getAddress(Address)))
825 ErrorOr AddressOrErr = Symbol.getAddress();
826 if (error(AddressOrErr.getError()))
828827 break;
828 uint64_t Address = *AddressOrErr;
829829 if (Address == UnknownAddress)
830830 continue;
831831 Address -= SectionAddr;
11121112 return;
11131113 }
11141114 for (const SymbolRef &Symbol : o->symbols()) {
1115 uint64_t Address;
1115 ErrorOr AddressOrError = Symbol.getAddress();
1116 if (error(AddressOrError.getError()))
1117 continue;
1118 uint64_t Address = *AddressOrError;
11161119 SymbolRef::Type Type = Symbol.getType();
11171120 uint32_t Flags = Symbol.getFlags();
11181121 section_iterator Section = o->section_end();
1119 if (error(Symbol.getAddress(Address)))
1120 continue;
11211122 if (error(Symbol.getSection(Section)))
11221123 continue;
11231124 StringRef Name;
200200 if (FunctionOnly && Symbol.getType() != SymbolRef::ST_Function)
201201 continue;
202202
203 uint64_t Address;
204 if (std::error_code EC = Symbol.getAddress(Address))
203 ErrorOr Address = Symbol.getAddress();
204 if (std::error_code EC = Address.getError())
205205 return EC;
206 if (Address == VA)
206 if (*Address == VA)
207207 return Symbol;
208208 }
209209 return readobj_error::unknown_symbol;
604604 if (std::error_code EC = FunctionNameOrErr.getError())
605605 report_fatal_error(EC.message());
606606 FunctionName = *FunctionNameOrErr;
607 Function->getAddress(FunctionAddress);
607 ErrorOr FunctionAddressOrErr = Function->getAddress();
608 if (std::error_code EC = FunctionAddressOrErr.getError())
609 report_fatal_error(EC.message());
610 FunctionAddress = *FunctionAddressOrErr;
608611 } else {
609612 const pe32_header *PEHeader;
610613 if (COFF.getPE32Header(PEHeader))
619622 if (std::error_code EC = Name.getError())
620623 report_fatal_error(EC.message());
621624
622 uint64_t Address;
623 XDataRecord->getAddress(Address);
625 ErrorOr AddressOrErr = XDataRecord->getAddress();
626 if (std::error_code EC = AddressOrErr.getError())
627 report_fatal_error(EC.message());
628 uint64_t Address = *AddressOrErr;
624629
625630 SW.printString("ExceptionRecord", formatSymbol(*Name, Address));
626631
665670 if (std::error_code EC = FunctionNameOrErr.getError())
666671 report_fatal_error(EC.message());
667672 FunctionName = *FunctionNameOrErr;
668 Function->getAddress(FunctionAddress);
673 ErrorOr FunctionAddressOrErr = Function->getAddress();
674 FunctionAddress = *FunctionAddressOrErr;
669675 } else {
670676 const pe32_header *PEHeader;
671677 if (COFF.getPE32Header(PEHeader))
143143 Ctx.ResolveSymbol(Section, Offset, Symbol, Ctx.UserData))
144144 return EC;
145145
146 if (std::error_code EC = Symbol.getAddress(ResolvedAddress))
146 ErrorOr ResolvedAddressOrErr = Symbol.getAddress();
147 if (std::error_code EC = ResolvedAddressOrErr.getError())
147148 return EC;
149 ResolvedAddress = *ResolvedAddressOrErr;
148150
149151 section_iterator SI = Ctx.COFF.section_begin();
150152 if (std::error_code EC = Symbol.getSection(SI))
268268 ErrorOr Name = Sym.getName();
269269 if (!Name)
270270 continue;
271 uint64_t Addr;
272 if (Sym.getAddress(Addr))
271 ErrorOr AddrOrErr = Sym.getAddress();
272 if (!AddrOrErr)
273273 continue;
274 uint64_t Addr = *AddrOrErr;
274275
275276 uint64_t Size = P.second;
276277 // If we're not using the debug object, compute the address of the
8383 SymbolRef::Type SymbolType = Symbol.getType();
8484 if (SymbolType != SymbolRef::ST_Function && SymbolType != SymbolRef::ST_Data)
8585 return;
86 uint64_t SymbolAddress;
87 if (error(Symbol.getAddress(SymbolAddress)) ||
88 SymbolAddress == UnknownAddress)
86 ErrorOr SymbolAddressOrErr = Symbol.getAddress();
87 if (error(SymbolAddressOrErr.getError()))
88 return;
89 uint64_t SymbolAddress = *SymbolAddressOrErr;
90 if (SymbolAddress == UnknownAddress)
8991 return;
9092 if (OpdExtractor) {
9193 // For big-endian PowerPC64 ELF, symbols in the .opd section refer to