diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2014-01-21 23:06:54 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2014-01-21 23:06:54 +0000 |
commit | 692410efcb1330fc69aa75a042bea98c4eab80ba (patch) | |
tree | 41e211e52e88df872eab5a337196fc37dfc8ae72 /llvm/lib/Object/ELFObjectFile.cpp | |
parent | 902efc61be719775ccb15cb95ce6c2e79566dd5e (diff) | |
download | bcm5719-llvm-692410efcb1330fc69aa75a042bea98c4eab80ba.tar.gz bcm5719-llvm-692410efcb1330fc69aa75a042bea98c4eab80ba.zip |
Be a bit more consistent about using ErrorOr when constructing Binary objects.
The constructors of classes deriving from Binary normally take an error_code
as an argument to the constructor. My original intent was to change them
to have a trivial constructor and move the initial parsing logic to a static
method returning an ErrorOr. I changed my mind because:
* A constructor with an error_code out parameter is extremely convenient from
the implementation side. We can incrementally construct the object and give
up when we find an error.
* It is very efficient when constructing on the stack or when there is no
error. The only inefficient case is where heap allocating and an error is
found (we have to free the memory).
The result is that this is a much smaller patch. It just standardizes the
create* helpers to return an ErrorOr.
Almost no functionality change: The only difference is that this found that
we were trying to read past the end of COFF import library but ignoring the
error.
llvm-svn: 199770
Diffstat (limited to 'llvm/lib/Object/ELFObjectFile.cpp')
-rw-r--r-- | llvm/lib/Object/ELFObjectFile.cpp | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/llvm/lib/Object/ELFObjectFile.cpp b/llvm/lib/Object/ELFObjectFile.cpp index 15bc6be0b46..2736bc42f2e 100644 --- a/llvm/lib/Object/ELFObjectFile.cpp +++ b/llvm/lib/Object/ELFObjectFile.cpp @@ -17,57 +17,60 @@ namespace llvm { using namespace object; -// Creates an in-memory object-file by default: createELFObjectFile(Buffer) -ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) { - std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object); - error_code ec; - +ErrorOr<ObjectFile *> ObjectFile::createELFObjectFile(MemoryBuffer *Obj) { + std::pair<unsigned char, unsigned char> Ident = getElfArchType(Obj); std::size_t MaxAlignment = - 1ULL << countTrailingZeros(uintptr_t(Object->getBufferStart())); + 1ULL << countTrailingZeros(uintptr_t(Obj->getBufferStart())); + error_code EC; + OwningPtr<ObjectFile> R; if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) #if !LLVM_IS_UNALIGNED_ACCESS_FAST if (MaxAlignment >= 4) - return new ELFObjectFile<ELFType<support::little, 4, false> >(Object, ec); + R.reset(new ELFObjectFile<ELFType<support::little, 4, false> >(Obj, EC)); else #endif if (MaxAlignment >= 2) - return new ELFObjectFile<ELFType<support::little, 2, false> >(Object, ec); + R.reset(new ELFObjectFile<ELFType<support::little, 2, false> >(Obj, EC)); else llvm_unreachable("Invalid alignment for ELF file!"); else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) #if !LLVM_IS_UNALIGNED_ACCESS_FAST if (MaxAlignment >= 4) - return new ELFObjectFile<ELFType<support::big, 4, false> >(Object, ec); + R.reset(new ELFObjectFile<ELFType<support::big, 4, false> >(Obj, EC)); else #endif if (MaxAlignment >= 2) - return new ELFObjectFile<ELFType<support::big, 2, false> >(Object, ec); + R.reset(new ELFObjectFile<ELFType<support::big, 2, false> >(Obj, EC)); else llvm_unreachable("Invalid alignment for ELF file!"); else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) #if !LLVM_IS_UNALIGNED_ACCESS_FAST if (MaxAlignment >= 8) - return new ELFObjectFile<ELFType<support::big, 8, true> >(Object, ec); + R.reset(new ELFObjectFile<ELFType<support::big, 8, true> >(Obj, EC)); else #endif if (MaxAlignment >= 2) - return new ELFObjectFile<ELFType<support::big, 2, true> >(Object, ec); + R.reset(new ELFObjectFile<ELFType<support::big, 2, true> >(Obj, EC)); else llvm_unreachable("Invalid alignment for ELF file!"); else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) { #if !LLVM_IS_UNALIGNED_ACCESS_FAST if (MaxAlignment >= 8) - return new ELFObjectFile<ELFType<support::little, 8, true> >(Object, ec); + R.reset(new ELFObjectFile<ELFType<support::little, 8, true> >(Obj, EC)); else #endif if (MaxAlignment >= 2) - return new ELFObjectFile<ELFType<support::little, 2, true> >(Object, ec); + R.reset(new ELFObjectFile<ELFType<support::little, 2, true> >(Obj, EC)); else llvm_unreachable("Invalid alignment for ELF file!"); } + else + report_fatal_error("Buffer is not an ELF object file!"); - report_fatal_error("Buffer is not an ELF object file!"); + if (EC) + return EC; + return R.take(); } } // end namespace llvm |