llvm.org GIT mirror llvm / 029dd82
[llvm-objdump] Migrate some functions from std::error_code to Error git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@357965 91177308-0d34-0410-b5e6-96231b3b80d8 Fangrui Song 7 months ago
4 changed file(s) with 28 addition(s) and 38 deletion(s). Raw diff Collapse all Expand all
154154 }
155155
156156 // Given a symbol sym this functions returns the address and section of it.
157 static std::error_code
158 resolveSectionAndAddress(const COFFObjectFile *Obj, const SymbolRef &Sym,
159 const coff_section *&ResolvedSection,
160 uint64_t &ResolvedAddr) {
157 static Error resolveSectionAndAddress(const COFFObjectFile *Obj,
158 const SymbolRef &Sym,
159 const coff_section *&ResolvedSection,
160 uint64_t &ResolvedAddr) {
161161 Expected ResolvedAddrOrErr = Sym.getAddress();
162162 if (!ResolvedAddrOrErr)
163 return errorToErrorCode(ResolvedAddrOrErr.takeError());
163 return ResolvedAddrOrErr.takeError();
164164 ResolvedAddr = *ResolvedAddrOrErr;
165165 Expected Iter = Sym.getSection();
166166 if (!Iter)
167 return errorToErrorCode(Iter.takeError());
167 return Iter.takeError();
168168 ResolvedSection = Obj->getCOFFSection(**Iter);
169 return std::error_code();
169 return Error::success();
170170 }
171171
172172 // Given a vector of relocations for a section and an offset into this section
173173 // the function returns the symbol used for the relocation at the offset.
174 static std::error_code resolveSymbol(const std::vector &Rels,
174 static Error resolveSymbol(const std::vector &Rels,
175175 uint64_t Offset, SymbolRef &Sym) {
176176 for (auto &R : Rels) {
177177 uint64_t Ofs = R.getOffset();
178178 if (Ofs == Offset) {
179179 Sym = *R.getSymbol();
180 return std::error_code();
180 return Error::success();
181181 }
182182 }
183 return object_error::parse_failed;
183 return make_error();
184184 }
185185
186186 // Given a vector of relocations for a section and an offset into this section
187187 // the function resolves the symbol used for the relocation at the offset and
188188 // returns the section content and the address inside the content pointed to
189189 // by the symbol.
190 static std::error_code
190 static Error
191191 getSectionContents(const COFFObjectFile *Obj,
192192 const std::vector &Rels, uint64_t Offset,
193193 ArrayRef &Contents, uint64_t &Addr) {
194194 SymbolRef Sym;
195 if (std::error_code EC = resolveSymbol(Rels, Offset, Sym))
196 return EC;
195 if (Error E = resolveSymbol(Rels, Offset, Sym))
196 return E;
197197 const coff_section *Section;
198 if (std::error_code EC = resolveSectionAndAddress(Obj, Sym, Section, Addr))
199 return EC;
198 if (Error E = resolveSectionAndAddress(Obj, Sym, Section, Addr))
199 return E;
200200 if (std::error_code EC = Obj->getSectionContents(Section, Contents))
201 return EC;
202 return std::error_code();
201 return errorCodeToError(EC);
202 return Error::success();
203203 }
204204
205205 // Given a vector of relocations for a section and an offset into this section
206206 // the function returns the name of the symbol used for the relocation at the
207207 // offset.
208 static std::error_code resolveSymbolName(const std::vector &Rels,
209 uint64_t Offset, StringRef &Name) {
208 static Error resolveSymbolName(const std::vector &Rels,
209 uint64_t Offset, StringRef &Name) {
210210 SymbolRef Sym;
211 if (std::error_code EC = resolveSymbol(Rels, Offset, Sym))
211 if (Error EC = resolveSymbol(Rels, Offset, Sym))
212212 return EC;
213213 Expected NameOrErr = Sym.getName();
214214 if (!NameOrErr)
215 return errorToErrorCode(NameOrErr.takeError());
215 return NameOrErr.takeError();
216216 Name = *NameOrErr;
217 return std::error_code();
217 return Error::success();
218218 }
219219
220220 static void printCOFFSymbolAddress(llvm::raw_ostream &Out,
652652 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
653653 Expected Symbol = coff->getSymbol(SI);
654654 StringRef Name;
655 error(errorToErrorCode(Symbol.takeError()));
655 error(Symbol.takeError());
656656 error(coff->getSymbolName(*Symbol, Name));
657657
658658 outs() << "[" << format("%2d", SI) << "]"
361361 // If we couldn't find a symbol that this relocation refers to, try
362362 // to find a section beginning instead.
363363 for (const SectionRef &Section : ToolSectionFilter(*O)) {
364 std::error_code ec;
365
366364 StringRef Name;
367365 uint64_t Addr = Section.getAddress();
368366 if (Addr != Val)
369367 continue;
370 if ((ec = Section.getName(Name)))
371 report_error(O->getFileName(), ec);
368 if (std::error_code EC = Section.getName(Name))
369 report_error(errorCodeToError(EC), O->getFileName());
372370 Fmt << Name;
373371 return;
374372 }
354354 exit(1);
355355 }
356356
357 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(StringRef File,
358 std::error_code EC) {
359 assert(EC);
360 WithColor::error(errs(), ToolName)
361 << "'" << File << "': " << EC.message() << ".\n";
362 exit(1);
363 }
364
365357 LLVM_ATTRIBUTE_NORETURN void llvm::report_error(Error E, StringRef File) {
366358 assert(E);
367359 std::string Buf;
20122004 else if (COFFImportFile *I = dyn_cast(&*ChildOrErr.get()))
20132005 dumpObject(I, A, &C);
20142006 else
2015 report_error(A->getFileName(), object_error::invalid_file_type);
2007 report_error(errorCodeToError(object_error::invalid_file_type),
2008 A->getFileName());
20162009 }
20172010 if (Err)
20182011 report_error(std::move(Err), A->getFileName());
20392032 else if (MachOUniversalBinary *UB = dyn_cast(&Binary))
20402033 parseInputMachO(UB);
20412034 else
2042 report_error(file, object_error::invalid_file_type);
2035 report_error(errorCodeToError(object_error::invalid_file_type), file);
20432036 }
20442037
20452038 int main(int argc, char **argv) {
171171 void warn(StringRef Message);
172172 LLVM_ATTRIBUTE_NORETURN void error(Twine Message);
173173 LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, Twine Message);
174 LLVM_ATTRIBUTE_NORETURN void report_error(StringRef File, std::error_code EC);
175174 LLVM_ATTRIBUTE_NORETURN void report_error(Error E, StringRef File);
176175 LLVM_ATTRIBUTE_NORETURN void
177176 report_error(Error E, StringRef FileName, StringRef ArchiveName,