llvm.org GIT mirror llvm / 281c1b0
[llvm-objdump] Call exit(1) on error, i.e. fail early. Previously we kept going on partly corrupted input, which might result in garbage being printed, or even worse, random crashes. Rafael mentioned that this is the GNU behavior as well, but after some discussion we both agreed it's probably better to emit a reasonable error message and exit. As a side-effect of this commit, now we don't rely on global state for error codes anymore. objdump was the last tool in the toolchain which needed to be converted. Hopefully the old behavior won't sneak into the tree again. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@244019 91177308-0d34-0410-b5e6-96231b3b80d8 Davide Italiano 5 years ago
4 changed file(s) with 43 addition(s) and 90 deletion(s). Raw diff Collapse all Expand all
240240 return;
241241
242242 const pe32_header *PE32Header;
243 if (error(Obj->getPE32Header(PE32Header)))
244 return;
243 error(Obj->getPE32Header(PE32Header));
245244 uint32_t ImageBase = PE32Header->ImageBase;
246245 uintptr_t IntPtr = 0;
247 if (error(Obj->getVaPtr(TableVA, IntPtr)))
248 return;
246 error(Obj->getVaPtr(TableVA, IntPtr));
249247 const support::ulittle32_t *P = (const support::ulittle32_t *)IntPtr;
250248 outs() << "SEH Table:";
251249 for (int I = 0; I < Count; ++I)
256254 static void printLoadConfiguration(const COFFObjectFile *Obj) {
257255 // Skip if it's not executable.
258256 const pe32_header *PE32Header;
259 if (error(Obj->getPE32Header(PE32Header)))
260 return;
257 error(Obj->getPE32Header(PE32Header));
261258 if (!PE32Header)
262259 return;
263260
266263 return;
267264
268265 const data_directory *DataDir;
269 if (error(Obj->getDataDirectory(COFF::LOAD_CONFIG_TABLE, DataDir)))
270 return;
266 error(Obj->getDataDirectory(COFF::LOAD_CONFIG_TABLE, DataDir));
271267 uintptr_t IntPtr = 0;
272268 if (DataDir->RelativeVirtualAddress == 0)
273269 return;
274 if (error(Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr)))
275 return;
270 error(Obj->getRvaPtr(DataDir->RelativeVirtualAddress, IntPtr));
276271
277272 auto *LoadConf = reinterpret_cast(IntPtr);
278273 outs() << "Load configuration:"
380375 const RuntimeFunction *&RFStart, int &NumRFs) {
381376 for (const SectionRef &Section : Obj->sections()) {
382377 StringRef Name;
383 if (error(Section.getName(Name)))
384 continue;
378 error(Section.getName(Name));
385379 if (Name != ".pdata")
386380 continue;
387381
393387 std::sort(Rels.begin(), Rels.end(), RelocAddressLess);
394388
395389 ArrayRef Contents;
396 if (error(Obj->getSectionContents(Pdata, Contents)))
397 continue;
390 error(Obj->getSectionContents(Pdata, Contents));
398391 if (Contents.empty())
399392 continue;
400393
498491
499492 ArrayRef XContents;
500493 uint64_t UnwindInfoOffset = 0;
501 if (error(getSectionContents(
494 error(getSectionContents(
502495 Obj, Rels, SectionOffset +
503496 /*offsetof(RuntimeFunction, UnwindInfoOffset)*/ 8,
504 XContents, UnwindInfoOffset)))
505 return;
497 XContents, UnwindInfoOffset));
506498 if (XContents.empty())
507499 return;
508500
85538553 uint64_t CurSegAddress;
85548554 for (const SectionRef &Section : Obj->sections()) {
85558555 SectionInfo Info;
8556 if (error(Section.getName(Info.SectionName)))
8557 return;
8556 error(Section.getName(Info.SectionName));
85588557 Info.Address = Section.getAddress();
85598558 Info.Size = Section.getSize();
85608559 Info.SegmentName =
176176 cl::desc("Display contents of faultmap section"));
177177
178178 static StringRef ToolName;
179 static int ReturnValue = EXIT_SUCCESS;
180179
181180 namespace {
182181 typedef std::function FilterPredicate;
243242 }
244243 }
245244
246 bool llvm::error(std::error_code EC) {
245 void llvm::error(std::error_code EC) {
247246 if (!EC)
248 return false;
247 return;
249248
250249 outs() << ToolName << ": error reading file: " << EC.message() << ".\n";
251250 outs().flush();
252 ReturnValue = EXIT_FAILURE;
253 return true;
254251 }
255252
256253 static void report_error(StringRef File, std::error_code EC) {
257254 assert(EC);
258255 errs() << ToolName << ": '" << File << "': " << EC.message() << ".\n";
259 ReturnValue = EXIT_FAILURE;
256 exit(1);
260257 }
261258
262259 static const Target *getTarget(const ObjectFile *Obj = nullptr) {
574571 symbol_iterator SI = O->symbol_begin();
575572 advance(SI, Val);
576573 ErrorOr SOrErr = SI->getName();
577 if (!error(SOrErr.getError()))
578 S = *SOrErr;
574 error(SOrErr.getError());
575 S = *SOrErr;
579576 } else {
580577 section_iterator SI = O->section_begin();
581578 // Adjust for the fact that sections are 1-indexed.
899896 continue;
900897
901898 ErrorOr AddressOrErr = Symbol.getAddress();
902 if (error(AddressOrErr.getError()))
903 break;
899 error(AddressOrErr.getError());
904900 uint64_t Address = *AddressOrErr;
905901
906902 ErrorOr Name = Symbol.getName();
907 if (error(Name.getError()))
908 break;
903 error(Name.getError());
909904 if (Name->empty())
910905 continue;
911906 AllSymbols.push_back(std::make_pair(Address, *Name));
928923 for (const SymbolRef &Symbol : Obj->symbols()) {
929924 if (Section.containsSymbol(Symbol)) {
930925 ErrorOr AddressOrErr = Symbol.getAddress();
931 if (error(AddressOrErr.getError()))
932 break;
926 error(AddressOrErr.getError());
933927 uint64_t Address = *AddressOrErr;
934928 Address -= SectionAddr;
935929 if (Address >= SectSize)
936930 continue;
937931
938932 ErrorOr Name = Symbol.getName();
939 if (error(Name.getError()))
940 break;
933 error(Name.getError());
941934 Symbols.push_back(std::make_pair(Address, *Name));
942935 }
943936 }
964957 SegmentName = MachO->getSectionFinalSegmentName(DR);
965958 }
966959 StringRef name;
967 if (error(Section.getName(name)))
968 break;
960 error(Section.getName(name));
969961 outs() << "Disassembly of section ";
970962 if (!SegmentName.empty())
971963 outs() << SegmentName << ",";
979971 raw_svector_ostream CommentStream(Comments);
980972
981973 StringRef BytesStr;
982 if (error(Section.getContents(BytesStr)))
983 break;
974 error(Section.getContents(BytesStr));
984975 ArrayRef Bytes(reinterpret_cast(BytesStr.data()),
985976 BytesStr.size());
986977
10611052 // Stop when rel_cur's address is past the current instruction.
10621053 if (addr >= Index + Size) break;
10631054 rel_cur->getTypeName(name);
1064 if (error(getRelocationValueString(*rel_cur, val)))
1065 goto skip_print_rel;
1055 error(getRelocationValueString(*rel_cur, val));
10661056 outs() << format(Fmt.data(), SectionAddr + addr) << name
10671057 << "\t" << val << "\n";
10681058
10861076 if (Section.relocation_begin() == Section.relocation_end())
10871077 continue;
10881078 StringRef secname;
1089 if (error(Section.getName(secname)))
1090 continue;
1079 error(Section.getName(secname));
10911080 outs() << "RELOCATION RECORDS FOR [" << secname << "]:\n";
10921081 for (const RelocationRef &Reloc : Section.relocations()) {
10931082 bool hidden = getHidden(Reloc);
10971086 if (hidden)
10981087 continue;
10991088 Reloc.getTypeName(relocname);
1100 if (error(getRelocationValueString(Reloc, valuestr)))
1101 continue;
1089 error(getRelocationValueString(Reloc, valuestr));
11021090 outs() << format(Fmt.data(), address) << " " << relocname << " "
11031091 << valuestr << "\n";
11041092 }
11121100 unsigned i = 0;
11131101 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
11141102 StringRef Name;
1115 if (error(Section.getName(Name)))
1116 return;
1103 error(Section.getName(Name));
11171104 uint64_t Address = Section.getAddress();
11181105 uint64_t Size = Section.getSize();
11191106 bool Text = Section.isText();
11321119 for (const SectionRef &Section : ToolSectionFilter(*Obj)) {
11331120 StringRef Name;
11341121 StringRef Contents;
1135 if (error(Section.getName(Name)))
1136 continue;
1122 error(Section.getName(Name));
11371123 uint64_t BaseAddr = Section.getAddress();
11381124 uint64_t Size = Section.getSize();
11391125 if (!Size)
11471133 continue;
11481134 }
11491135
1150 if (error(Section.getContents(Contents)))
1151 continue;
1136 error(Section.getContents(Contents));
11521137
11531138 // Dump out the content as hex and printable ascii characters.
11541139 for (std::size_t addr = 0, end = Contents.size(); addr < end; addr += 16) {
11801165 for (unsigned SI = 0, SE = coff->getNumberOfSymbols(); SI != SE; ++SI) {
11811166 ErrorOr Symbol = coff->getSymbol(SI);
11821167 StringRef Name;
1183 if (error(Symbol.getError()))
1184 return;
1185
1186 if (error(coff->getSymbolName(*Symbol, Name)))
1187 return;
1168 error(Symbol.getError());
1169 error(coff->getSymbolName(*Symbol, Name));
11881170
11891171 outs() << "[" << format("%2d", SI) << "]"
11901172 << "(sec " << format("%2d", int(Symbol->getSectionNumber())) << ")"
11981180 for (unsigned AI = 0, AE = Symbol->getNumberOfAuxSymbols(); AI < AE; ++AI, ++SI) {
11991181 if (Symbol->isSectionDefinition()) {
12001182 const coff_aux_section_definition *asd;
1201 if (error(coff->getAuxSymbol(SI + 1, asd)))
1202 return;
1183 error(coff->getAuxSymbol(SI + 1, asd));
12031184
12041185 int32_t AuxNumber = asd->getNumber(Symbol->isBigObj());
12051186
12141195 , unsigned(asd->Selection));
12151196 } else if (Symbol->isFileRecord()) {
12161197 const char *FileName;
1217 if (error(coff->getAuxSymbol(SI + 1, FileName)))
1218 return;
1198 error(coff->getAuxSymbol(SI + 1, FileName));
12191199
12201200 StringRef Name(FileName, Symbol->getNumberOfAuxSymbols() *
12211201 coff->getSymbolTableEntrySize());
12391219 }
12401220 for (const SymbolRef &Symbol : o->symbols()) {
12411221 ErrorOr AddressOrError = Symbol.getAddress();
1242 if (error(AddressOrError.getError()))
1243 continue;
1222 error(AddressOrError.getError());
12441223 uint64_t Address = *AddressOrError;
12451224 SymbolRef::Type Type = Symbol.getType();
12461225 uint32_t Flags = Symbol.getFlags();
12471226 section_iterator Section = o->section_end();
1248 if (error(Symbol.getSection(Section)))
1249 continue;
1227 error(Symbol.getSection(Section));
12501228 StringRef Name;
12511229 if (Type == SymbolRef::ST_Debug && Section != o->section_end()) {
12521230 Section->getName(Name);
12531231 } else {
12541232 ErrorOr NameOrErr = Symbol.getName();
1255 if (error(NameOrErr.getError()))
1256 continue;
1233 error(NameOrErr.getError());
12571234 Name = *NameOrErr;
12581235 }
12591236
13001277 outs() << SegmentName << ",";
13011278 }
13021279 StringRef SectionName;
1303 if (error(Section->getName(SectionName)))
1304 SectionName = "";
1280 error(Section->getName(SectionName));
13051281 outs() << SectionName;
13061282 }
13071283
14191395 return;
14201396
14211397 StringRef ClangASTContents;
1422 if (error(ClangASTSection.getValue().getContents(ClangASTContents))) {
1423 errs() << "Could not read the " << ClangASTSectionName << " section!\n";
1424 return;
1425 }
1426
1398 error(ClangASTSection.getValue().getContents(ClangASTContents));
14271399 outs().write(ClangASTContents.data(), ClangASTContents.size());
14281400 }
14291401
14591431 }
14601432
14611433 StringRef FaultMapContents;
1462 if (error(FaultMapSection.getValue().getContents(FaultMapContents))) {
1463 errs() << "Could not read the " << FaultMapContents << " section!\n";
1464 return;
1465 }
1434 error(FaultMapSection.getValue().getContents(FaultMapContents));
14661435
14671436 FaultMapParser FMP(FaultMapContents.bytes_begin(),
14681437 FaultMapContents.bytes_end());
15221491 static void DumpArchive(const Archive *a) {
15231492 for (const Archive::Child &C : a->children()) {
15241493 ErrorOr> ChildOrErr = C.getAsBinary();
1525 if (std::error_code EC = ChildOrErr.getError()) {
1526 // Ignore non-object files.
1494 if (std::error_code EC = ChildOrErr.getError())
15271495 if (EC != object_error::invalid_file_type)
15281496 report_error(a->getFileName(), EC);
1529 continue;
1530 }
15311497 if (ObjectFile *o = dyn_cast(&*ChildOrErr.get()))
15321498 DumpObject(o);
15331499 else
15381504 /// @brief Open file and figure out how to dump it.
15391505 static void DumpInput(StringRef file) {
15401506 // If file isn't stdin, check that it exists.
1541 if (file != "-" && !sys::fs::exists(file)) {
1507 if (file != "-" && !sys::fs::exists(file))
15421508 report_error(file, errc::no_such_file_or_directory);
1543 return;
1544 }
15451509
15461510 // If we are using the Mach-O specific object file parser, then let it parse
15471511 // the file and process the command line options. So the -arch flags can
15531517
15541518 // Attempt to open the binary.
15551519 ErrorOr> BinaryOrErr = createBinary(file);
1556 if (std::error_code EC = BinaryOrErr.getError()) {
1520 if (std::error_code EC = BinaryOrErr.getError())
15571521 report_error(file, EC);
1558 return;
1559 }
15601522 Binary &Binary = *BinaryOrErr.get().getBinary();
15611523
15621524 if (Archive *a = dyn_cast(&Binary))
16241586 std::for_each(InputFilenames.begin(), InputFilenames.end(),
16251587 DumpInput);
16261588
1627 return ReturnValue;
1628 }
1589 return EXIT_SUCCESS;
1590 }
5454 extern cl::opt PrintImmHex;
5555
5656 // Various helper functions.
57 bool error(std::error_code ec);
57 void error(std::error_code ec);
5858 bool RelocAddressLess(object::RelocationRef a, object::RelocationRef b);
5959 void ParseInputMachO(StringRef Filename);
6060 void printCOFFUnwindInfo(const object::COFFObjectFile* o);