llvm.org GIT mirror llvm / 1fca78a
Return an ErrorOr<Binary *> from createBinary. I did write a version returning ErrorOr<OwningPtr<Binary> >, but it is too cumbersome to use without std::move. I will keep the patch locally and submit when we switch to c++11. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199326 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
9 changed file(s) with 71 addition(s) and 68 deletion(s). Raw diff Collapse all Expand all
1313 #ifndef LLVM_OBJECT_BINARY_H
1414 #define LLVM_OBJECT_BINARY_H
1515
16 #include "llvm/ADT/OwningPtr.h"
1716 #include "llvm/Object/Error.h"
17 #include "llvm/Support/ErrorOr.h"
1818
1919 namespace llvm {
2020
112112 /// @brief Create a Binary from Source, autodetecting the file type.
113113 ///
114114 /// @param Source The data to create the Binary from. Ownership is transferred
115 /// to Result if successful. If an error is returned, Source is destroyed
116 /// by createBinary before returning.
117 /// @param Result A pointer to the resulting Binary if no error occured.
118 error_code createBinary(MemoryBuffer *Source, OwningPtr &Result);
115 /// to the Binary if successful. If an error is returned,
116 /// Source is destroyed by createBinary before returning.
117 ErrorOr createBinary(MemoryBuffer *Source);
119118
120 error_code createBinary(StringRef Path, OwningPtr &Result);
121
119 ErrorOr createBinary(StringRef Path);
122120 }
123121 }
124122
186186 OwningPtr Buff;
187187 if (error_code ec = getMemoryBuffer(Buff))
188188 return ec;
189 if (error_code ec = createBinary(Buff.take(), ret))
190 return ec;
191 Result.swap(ret);
189 ErrorOr BinaryOrErr = createBinary(Buff.take());
190 if (error_code EC = BinaryOrErr.getError())
191 return EC;
192 Result.reset(BinaryOrErr.get());
192193 return object_error::success;
193194 }
194195
4141 return Data->getBufferIdentifier();
4242 }
4343
44 error_code object::createBinary(MemoryBuffer *Source,
45 OwningPtr &Result) {
44 ErrorOr object::createBinary(MemoryBuffer *Source) {
4645 OwningPtr scopedSource(Source);
47 if (!Source)
48 return make_error_code(errc::invalid_argument);
4946 sys::fs::file_magic type = sys::fs::identify_magic(Source->getBuffer());
50 error_code ec;
47 error_code EC;
5148 switch (type) {
5249 case sys::fs::file_magic::archive: {
53 OwningPtr ret(new Archive(scopedSource.take(), ec));
54 if (ec) return ec;
55 Result.swap(ret);
56 return object_error::success;
50 OwningPtr Ret(new Archive(scopedSource.take(), EC));
51 if (EC)
52 return EC;
53 return Ret.take();
5754 }
5855 case sys::fs::file_magic::elf_relocatable:
5956 case sys::fs::file_magic::elf_executable:
6057 case sys::fs::file_magic::elf_shared_object:
6158 case sys::fs::file_magic::elf_core: {
62 OwningPtr ret(
63 ObjectFile::createELFObjectFile(scopedSource.take()));
64 if (!ret)
59 OwningPtr Ret(
60 ObjectFile::createELFObjectFile(scopedSource.take()));
61 if (!Ret)
6562 return object_error::invalid_file_type;
66 Result.swap(ret);
67 return object_error::success;
63 return Ret.take();
6864 }
6965 case sys::fs::file_magic::macho_object:
7066 case sys::fs::file_magic::macho_executable:
7672 case sys::fs::file_magic::macho_bundle:
7773 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
7874 case sys::fs::file_magic::macho_dsym_companion: {
79 OwningPtr ret(
80 ObjectFile::createMachOObjectFile(scopedSource.take()));
81 if (!ret)
75 OwningPtr Ret(
76 ObjectFile::createMachOObjectFile(scopedSource.take()));
77 if (!Ret)
8278 return object_error::invalid_file_type;
83 Result.swap(ret);
84 return object_error::success;
79 return Ret.take();
8580 }
8681 case sys::fs::file_magic::macho_universal_binary: {
87 OwningPtr ret(new MachOUniversalBinary(scopedSource.take(), ec));
88 if (ec) return ec;
89 Result.swap(ret);
90 return object_error::success;
82 OwningPtr Ret(new MachOUniversalBinary(scopedSource.take(), EC));
83 if (EC)
84 return EC;
85 return Ret.take();
9186 }
9287 case sys::fs::file_magic::coff_object:
9388 case sys::fs::file_magic::coff_import_library:
9489 case sys::fs::file_magic::pecoff_executable: {
95 OwningPtr ret(
90 OwningPtr Ret(
9691 ObjectFile::createCOFFObjectFile(scopedSource.take()));
97 if (!ret)
92 if (!Ret)
9893 return object_error::invalid_file_type;
99 Result.swap(ret);
100 return object_error::success;
94 return Ret.take();
10195 }
10296 case sys::fs::file_magic::unknown:
10397 case sys::fs::file_magic::bitcode:
109103 llvm_unreachable("Unexpected Binary File Type");
110104 }
111105
112 error_code object::createBinary(StringRef Path, OwningPtr &Result) {
106 ErrorOr object::createBinary(StringRef Path) {
113107 OwningPtr File;
114 if (error_code ec = MemoryBuffer::getFileOrSTDIN(Path, File))
115 return ec;
116 return createBinary(File.take(), Result);
108 if (error_code EC = MemoryBuffer::getFileOrSTDIN(Path, File))
109 return EC;
110 return createBinary(File.take());
117111 }
579579 delete Result;
580580 }
581581 } else if (magic == sys::fs::file_magic::archive) {
582 OwningPtr arch;
583 if (error(object::createBinary(Buffer.take(), arch), Filename))
582 ErrorOr BinaryOrErr = object::createBinary(Buffer.take());
583 if (error(BinaryOrErr.getError(), Filename))
584584 return;
585 OwningPtr arch(BinaryOrErr.get());
585586
586587 if (object::Archive *a = dyn_cast(arch.get())) {
587588 if (ArchiveMap) {
629630 }
630631 }
631632 } else if (magic == sys::fs::file_magic::macho_universal_binary) {
632 OwningPtr Bin;
633 if (error(object::createBinary(Buffer.take(), Bin), Filename))
633 ErrorOr BinaryOrErr = object::createBinary(Buffer.take());
634 if (error(BinaryOrErr.getError(), Filename))
634635 return;
636 OwningPtr Bin(BinaryOrErr.get());
635637
636638 object::MachOUniversalBinary *UB =
637639 cast(Bin.get());
646648 }
647649 }
648650 } else if (magic.is_object()) {
649 OwningPtr obj;
650 if (error(object::createBinary(Buffer.take(), obj), Filename))
651 ErrorOr BinaryOrErr = object::createBinary(Buffer.take());
652 if (error(BinaryOrErr.getError(), Filename))
651653 return;
654 OwningPtr obj(BinaryOrErr.get());
652655 if (object::ObjectFile *o = dyn_cast(obj.get()))
653656 DumpSymbolNamesFromObject(o);
654657 } else {
832832 }
833833
834834 // Attempt to open the binary.
835 OwningPtr binary;
836 if (error_code ec = createBinary(file, binary)) {
837 errs() << ToolName << ": '" << file << "': " << ec.message() << ".\n";
838 return;
839 }
835 ErrorOr BinaryOrErr = createBinary(file);
836 if (error_code EC = BinaryOrErr.getError()) {
837 errs() << ToolName << ": '" << file << "': " << EC.message() << ".\n";
838 return;
839 }
840 OwningPtr binary(BinaryOrErr.get());
840841
841842 if (Archive *a = dyn_cast(binary.get()))
842843 DumpArchive(a);
259259 }
260260
261261 // Attempt to open the binary.
262 OwningPtr Binary;
263 if (error_code EC = createBinary(File, Binary)) {
262 ErrorOr BinaryOrErr = createBinary(File);
263 if (error_code EC = BinaryOrErr.getError()) {
264264 reportError(File, EC);
265265 return;
266266 }
267 OwningPtr Binary(BinaryOrErr.get());
267268
268269 if (Archive *Arc = dyn_cast(Binary.get()))
269270 dumpArchive(Arc);
243243 }
244244
245245 // Attempt to open the binary.
246 OwningPtr binary;
247 if (error_code ec = createBinary(file, binary)) {
248 errs() << ToolName << ": " << file << ": " << ec.message() << ".\n";
246 ErrorOr BinaryOrErr = createBinary(file);
247 if (error_code EC = BinaryOrErr.getError()) {
248 errs() << ToolName << ": " << file << ": " << EC.message() << ".\n";
249249 return;
250250 }
251 OwningPtr binary(BinaryOrErr.get());
251252
252253 if (Archive *a = dyn_cast(binary.get())) {
253254 // This is an archive. Iterate over each member and display its sizes.
300300 return I->second;
301301 Binary *Bin = 0;
302302 Binary *DbgBin = 0;
303 OwningPtr ParsedBinary;
304 OwningPtr ParsedDbgBinary;
305 if (!error(createBinary(Path, ParsedBinary))) {
303 ErrorOr BinaryOrErr = createBinary(Path);
304 if (!error(BinaryOrErr.getError())) {
305 OwningPtr ParsedBinary(BinaryOrErr.get());
306306 // Check if it's a universal binary.
307307 Bin = ParsedBinary.take();
308308 ParsedBinariesAndObjects.push_back(Bin);
311311 // resource directory.
312312 const std::string &ResourcePath =
313313 getDarwinDWARFResourceForPath(Path);
314 error_code EC = createBinary(ResourcePath, ParsedDbgBinary);
314 BinaryOrErr = createBinary(ResourcePath);
315 error_code EC = BinaryOrErr.getError();
315316 if (EC != errc::no_such_file_or_directory && !error(EC)) {
316 DbgBin = ParsedDbgBinary.take();
317 DbgBin = BinaryOrErr.get();
317318 ParsedBinariesAndObjects.push_back(DbgBin);
318319 }
319320 }
323324 uint32_t CRCHash;
324325 std::string DebugBinaryPath;
325326 if (getGNUDebuglinkContents(Bin, DebuglinkName, CRCHash) &&
326 findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath) &&
327 !error(createBinary(DebugBinaryPath, ParsedDbgBinary))) {
328 DbgBin = ParsedDbgBinary.take();
329 ParsedBinariesAndObjects.push_back(DbgBin);
327 findDebugBinary(Path, DebuglinkName, CRCHash, DebugBinaryPath)) {
328 BinaryOrErr = createBinary(DebugBinaryPath);
329 if (!error(BinaryOrErr.getError())) {
330 DbgBin = BinaryOrErr.get();
331 ParsedBinariesAndObjects.push_back(DbgBin);
332 }
330333 }
331334 }
332335 }
378378
379379 cl::ParseCommandLineOptions(argc, argv, "llvm Mach-O dumping tool\n");
380380
381 OwningPtr Binary;
382 if (error_code EC = createBinary(InputFile, Binary))
381 ErrorOr BinaryOrErr = createBinary(InputFile);
382 if (error_code EC = BinaryOrErr.getError())
383383 return Error("unable to read input: '" + EC.message() + "'");
384 OwningPtr Binary(BinaryOrErr.get());
384385
385386 const MachOObjectFile *InputObject = dyn_cast(Binary.get());
386387 if (!InputObject)