|
17 | 17 | namespace llvm {
|
18 | 18 | using namespace object;
|
19 | 19 |
|
20 |
| -// Creates an in-memory object-file by default: createELFObjectFile(Buffer) |
21 |
| -ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) { |
22 |
| - std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object); |
23 |
| - error_code ec; |
24 |
| - |
| 20 | +ErrorOr<ObjectFile *> ObjectFile::createELFObjectFile(MemoryBuffer *Obj) { |
| 21 | + std::pair<unsigned char, unsigned char> Ident = getElfArchType(Obj); |
25 | 22 | std::size_t MaxAlignment =
|
26 |
| - 1ULL << countTrailingZeros(uintptr_t(Object->getBufferStart())); |
| 23 | + 1ULL << countTrailingZeros(uintptr_t(Obj->getBufferStart())); |
27 | 24 |
|
| 25 | + error_code EC; |
| 26 | + OwningPtr<ObjectFile> R; |
28 | 27 | if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
|
29 | 28 | #if !LLVM_IS_UNALIGNED_ACCESS_FAST
|
30 | 29 | if (MaxAlignment >= 4)
|
31 |
| - return new ELFObjectFile<ELFType<support::little, 4, false> >(Object, ec); |
| 30 | + R.reset(new ELFObjectFile<ELFType<support::little, 4, false> >(Obj, EC)); |
32 | 31 | else
|
33 | 32 | #endif
|
34 | 33 | if (MaxAlignment >= 2)
|
35 |
| - return new ELFObjectFile<ELFType<support::little, 2, false> >(Object, ec); |
| 34 | + R.reset(new ELFObjectFile<ELFType<support::little, 2, false> >(Obj, EC)); |
36 | 35 | else
|
37 | 36 | llvm_unreachable("Invalid alignment for ELF file!");
|
38 | 37 | else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
|
39 | 38 | #if !LLVM_IS_UNALIGNED_ACCESS_FAST
|
40 | 39 | if (MaxAlignment >= 4)
|
41 |
| - return new ELFObjectFile<ELFType<support::big, 4, false> >(Object, ec); |
| 40 | + R.reset(new ELFObjectFile<ELFType<support::big, 4, false> >(Obj, EC)); |
42 | 41 | else
|
43 | 42 | #endif
|
44 | 43 | if (MaxAlignment >= 2)
|
45 |
| - return new ELFObjectFile<ELFType<support::big, 2, false> >(Object, ec); |
| 44 | + R.reset(new ELFObjectFile<ELFType<support::big, 2, false> >(Obj, EC)); |
46 | 45 | else
|
47 | 46 | llvm_unreachable("Invalid alignment for ELF file!");
|
48 | 47 | else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
|
49 | 48 | #if !LLVM_IS_UNALIGNED_ACCESS_FAST
|
50 | 49 | if (MaxAlignment >= 8)
|
51 |
| - return new ELFObjectFile<ELFType<support::big, 8, true> >(Object, ec); |
| 50 | + R.reset(new ELFObjectFile<ELFType<support::big, 8, true> >(Obj, EC)); |
52 | 51 | else
|
53 | 52 | #endif
|
54 | 53 | if (MaxAlignment >= 2)
|
55 |
| - return new ELFObjectFile<ELFType<support::big, 2, true> >(Object, ec); |
| 54 | + R.reset(new ELFObjectFile<ELFType<support::big, 2, true> >(Obj, EC)); |
56 | 55 | else
|
57 | 56 | llvm_unreachable("Invalid alignment for ELF file!");
|
58 | 57 | else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
|
59 | 58 | #if !LLVM_IS_UNALIGNED_ACCESS_FAST
|
60 | 59 | if (MaxAlignment >= 8)
|
61 |
| - return new ELFObjectFile<ELFType<support::little, 8, true> >(Object, ec); |
| 60 | + R.reset(new ELFObjectFile<ELFType<support::little, 8, true> >(Obj, EC)); |
62 | 61 | else
|
63 | 62 | #endif
|
64 | 63 | if (MaxAlignment >= 2)
|
65 |
| - return new ELFObjectFile<ELFType<support::little, 2, true> >(Object, ec); |
| 64 | + R.reset(new ELFObjectFile<ELFType<support::little, 2, true> >(Obj, EC)); |
66 | 65 | else
|
67 | 66 | llvm_unreachable("Invalid alignment for ELF file!");
|
68 | 67 | }
|
| 68 | + else |
| 69 | + report_fatal_error("Buffer is not an ELF object file!"); |
69 | 70 |
|
70 |
| - report_fatal_error("Buffer is not an ELF object file!"); |
| 71 | + if (EC) |
| 72 | + return EC; |
| 73 | + return R.take(); |
71 | 74 | }
|
72 | 75 |
|
73 | 76 | } // end namespace llvm
|
0 commit comments