llvm.org GIT mirror llvm / 825fc31
Change createObjectFile to return an ErrorOr. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199776 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 6 years ago
8 changed file(s) with 31 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
375375 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
376376 /// return true.
377377 /// @brief Create ObjectFile from path.
378 static ObjectFile *createObjectFile(StringRef ObjectPath);
379 static ObjectFile *createObjectFile(MemoryBuffer *Object);
378 static ErrorOr createObjectFile(StringRef ObjectPath);
379 static ErrorOr createObjectFile(MemoryBuffer *Object);
380380
381381 static inline bool classof(const Binary *v) {
382382 return v->isObject();
262262 sys::path::append(AbsolutePath, CompilationDir);
263263 }
264264 sys::path::append(AbsolutePath, DWOFileName);
265 object::ObjectFile *DWOFile =
265 ErrorOr DWOFile =
266266 object::ObjectFile::createObjectFile(AbsolutePath);
267267 if (!DWOFile)
268268 return false;
269269 // Reset DWOHolder.
270 DWO.reset(new DWOHolder(DWOFile));
270 DWO.reset(new DWOHolder(DWOFile.get()));
271271 DWARFUnit *DWOCU = DWO->getUnit();
272272 // Verify that compile unit in .dwo file is valid.
273273 if (DWOCU == 0 || DWOCU->getDWOId() != getDWOId()) {
4343 ObjectImageCommon(ObjectBuffer* Input)
4444 : ObjectImage(Input) // saves Input as Buffer and takes ownership
4545 {
46 ObjFile = object::ObjectFile::createObjectFile(Buffer->getMemBuffer());
46 ObjFile =
47 object::ObjectFile::createObjectFile(Buffer->getMemBuffer()).get();
4748 }
4849 ObjectImageCommon(object::ObjectFile* Input)
4950 : ObjectImage(NULL), ObjFile(Input) {}
5858
5959 // ObjectFile creation
6060 LLVMObjectFileRef LLVMCreateObjectFile(LLVMMemoryBufferRef MemBuf) {
61 return wrap(ObjectFile::createObjectFile(unwrap(MemBuf)));
61 ErrorOr ObjOrErr(ObjectFile::createObjectFile(unwrap(MemBuf)));
62 ObjectFile *Obj = ObjOrErr ? ObjOrErr.get() : 0;
63 return wrap(Obj);
6264 }
6365
6466 void LLVMDisposeObjectFile(LLVMObjectFileRef ObjectFile) {
3636 return section_iterator(SectionRef(Sec, this));
3737 }
3838
39 ObjectFile *ObjectFile::createObjectFile(MemoryBuffer *Object) {
40 if (Object->getBufferSize() < 64) {
41 delete Object;
42 return 0;
43 }
39 ErrorOr ObjectFile::createObjectFile(MemoryBuffer *Object) {
40 OwningPtr ScopedObj(Object);
41 sys::fs::file_magic Type = sys::fs::identify_magic(Object->getBuffer());
4442
45 sys::fs::file_magic Type = sys::fs::identify_magic(Object->getBuffer());
4643 switch (Type) {
4744 case sys::fs::file_magic::unknown:
4845 case sys::fs::file_magic::bitcode:
4946 case sys::fs::file_magic::archive:
5047 case sys::fs::file_magic::macho_universal_binary:
5148 case sys::fs::file_magic::windows_resource:
52 delete Object;
53 return 0;
49 return object_error::invalid_file_type;
5450 case sys::fs::file_magic::elf_relocatable:
5551 case sys::fs::file_magic::elf_executable:
5652 case sys::fs::file_magic::elf_shared_object:
5753 case sys::fs::file_magic::elf_core:
58 return createELFObjectFile(Object).get();
54 return createELFObjectFile(ScopedObj.take());
5955 case sys::fs::file_magic::macho_object:
6056 case sys::fs::file_magic::macho_executable:
6157 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
6662 case sys::fs::file_magic::macho_bundle:
6763 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
6864 case sys::fs::file_magic::macho_dsym_companion:
69 return createMachOObjectFile(Object).get();
65 return createMachOObjectFile(ScopedObj.take());
7066 case sys::fs::file_magic::coff_object:
7167 case sys::fs::file_magic::coff_import_library:
7268 case sys::fs::file_magic::pecoff_executable:
73 return createCOFFObjectFile(Object).get();
69 return createCOFFObjectFile(ScopedObj.take());
7470 }
7571 llvm_unreachable("Unexpected Object File Type");
7672 }
7773
78 ObjectFile *ObjectFile::createObjectFile(StringRef ObjectPath) {
74 ErrorOr ObjectFile::createObjectFile(StringRef ObjectPath) {
7975 OwningPtr File;
80 if (MemoryBuffer::getFile(ObjectPath, File))
81 return NULL;
76 if (error_code EC = MemoryBuffer::getFile(ObjectPath, File))
77 return EC;
8278 return createObjectFile(File.take());
8379 }
526526 }
527527
528528 for (unsigned i = 0, e = ExtraObjects.size(); i != e; ++i) {
529 object::ObjectFile *Obj = object::ObjectFile::createObjectFile(
530 ExtraObjects[i]);
529 ErrorOr Obj =
530 object::ObjectFile::createObjectFile(ExtraObjects[i]);
531531 if (!Obj) {
532532 Err.print(argv[0], errs());
533533 return 1;
534534 }
535 EE->addObjectFile(Obj);
535 EE->addObjectFile(Obj.get());
536536 }
537537
538538 for (unsigned i = 0, e = ExtraArchives.size(); i != e; ++i) {
669669 object::ObjectFile *Obj;
670670 if (I->isNewMember()) {
671671 const char *Filename = I->getNew();
672 Obj = object::ObjectFile::createObjectFile(Filename);
672 if (ErrorOr ObjOrErr =
673 object::ObjectFile::createObjectFile(Filename))
674 Obj = ObjOrErr.get();
675 else
676 Obj = NULL;
673677 } else {
674678 object::Archive::child_iterator OldMember = I->getOld();
675679 OwningPtr Binary;
9292 return;
9393 }
9494
95 OwningPtr Obj(ObjectFile::createObjectFile(Buff.take()));
96 if (!Obj) {
97 errs() << Filename << ": Unknown object file format\n";
95 ErrorOr ObjOrErr(ObjectFile::createObjectFile(Buff.take()));
96 if (error_code EC = ObjOrErr.getError()) {
97 errs() << Filename << ": " << EC.message() << '\n';
9898 return;
9999 }
100 OwningPtr Obj(ObjOrErr.get());
100101
101102 OwningPtr DICtx(DIContext::getDWARFContext(Obj.get()));
102103