Michael J. Spencer | b60a18d | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 1 | //===- ELFObjectFile.cpp - ELF object file implementation -------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Eli Bendersky | c7d23dd | 2012-02-12 06:12:10 +0000 | [diff] [blame] | 10 | // Part of the ELFObjectFile class implementation. |
Michael J. Spencer | b60a18d | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Michael J. Spencer | 126973b | 2013-08-08 22:27:13 +0000 | [diff] [blame] | 14 | #include "llvm/Object/ELFObjectFile.h" |
Michael J. Spencer | bae14ce | 2013-01-04 20:36:28 +0000 | [diff] [blame] | 15 | #include "llvm/Support/MathExtras.h" |
Eli Bendersky | c3c80f0 | 2012-01-22 09:01:03 +0000 | [diff] [blame] | 16 | |
Michael J. Spencer | b60a18d | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 17 | namespace llvm { |
Eli Bendersky | c7d23dd | 2012-02-12 06:12:10 +0000 | [diff] [blame] | 18 | using namespace object; |
Eli Bendersky | c3c80f0 | 2012-01-22 09:01:03 +0000 | [diff] [blame] | 19 | |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 20 | ELFObjectFileBase::ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source) |
| 21 | : ObjectFile(Type, Source) {} |
Rafael Espindola | ab73774 | 2014-08-17 17:52:10 +0000 | [diff] [blame] | 22 | |
Rafael Espindola | 437b0d5 | 2014-07-31 03:12:45 +0000 | [diff] [blame] | 23 | ErrorOr<std::unique_ptr<ObjectFile>> |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 24 | ObjectFile::createELFObjectFile(MemoryBufferRef Obj) { |
Rafael Espindola | d5a8efe | 2014-07-05 11:38:52 +0000 | [diff] [blame] | 25 | std::pair<unsigned char, unsigned char> Ident = |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 26 | getElfArchType(Obj.getBuffer()); |
Michael J. Spencer | bae14ce | 2013-01-04 20:36:28 +0000 | [diff] [blame] | 27 | std::size_t MaxAlignment = |
Rafael Espindola | 48af1c2 | 2014-08-19 18:44:46 +0000 | [diff] [blame] | 28 | 1ULL << countTrailingZeros(uintptr_t(Obj.getBufferStart())); |
Michael J. Spencer | bae14ce | 2013-01-04 20:36:28 +0000 | [diff] [blame] | 29 | |
Rafael Espindola | ac729b4 | 2015-06-02 12:05:27 +0000 | [diff] [blame] | 30 | if (MaxAlignment < 2) |
| 31 | return object_error::parse_failed; |
| 32 | |
Rafael Espindola | db4ed0b | 2014-06-13 02:24:39 +0000 | [diff] [blame] | 33 | std::error_code EC; |
Ahmed Charles | 56440fd | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 34 | std::unique_ptr<ObjectFile> R; |
Rafael Espindola | ac729b4 | 2015-06-02 12:05:27 +0000 | [diff] [blame] | 35 | if (Ident.first == ELF::ELFCLASS32) { |
| 36 | if (Ident.second == ELF::ELFDATA2LSB) |
| 37 | R.reset(new ELFObjectFile<ELFType<support::little, false>>(Obj, EC)); |
| 38 | else if (Ident.second == ELF::ELFDATA2MSB) |
| 39 | R.reset(new ELFObjectFile<ELFType<support::big, false>>(Obj, EC)); |
Michael J. Spencer | 39678d8 | 2013-02-03 10:48:31 +0000 | [diff] [blame] | 40 | else |
Alexey Samsonov | 18ad2e5 | 2015-06-04 22:58:25 +0000 | [diff] [blame] | 41 | return object_error::parse_failed; |
Alexey Samsonov | 49179dd | 2015-06-04 23:14:43 +0000 | [diff] [blame^] | 42 | } else if (Ident.first == ELF::ELFCLASS64) { |
Rafael Espindola | ac729b4 | 2015-06-02 12:05:27 +0000 | [diff] [blame] | 43 | if (Ident.second == ELF::ELFDATA2LSB) |
| 44 | R.reset(new ELFObjectFile<ELFType<support::little, true>>(Obj, EC)); |
| 45 | else if (Ident.second == ELF::ELFDATA2MSB) |
| 46 | R.reset(new ELFObjectFile<ELFType<support::big, true>>(Obj, EC)); |
Michael J. Spencer | bae14ce | 2013-01-04 20:36:28 +0000 | [diff] [blame] | 47 | else |
Alexey Samsonov | 18ad2e5 | 2015-06-04 22:58:25 +0000 | [diff] [blame] | 48 | return object_error::parse_failed; |
Alexey Samsonov | 49179dd | 2015-06-04 23:14:43 +0000 | [diff] [blame^] | 49 | } else { |
| 50 | return object_error::parse_failed; |
Eli Bendersky | c7d23dd | 2012-02-12 06:12:10 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Rafael Espindola | 692410e | 2014-01-21 23:06:54 +0000 | [diff] [blame] | 53 | if (EC) |
| 54 | return EC; |
Rafael Espindola | 437b0d5 | 2014-07-31 03:12:45 +0000 | [diff] [blame] | 55 | return std::move(R); |
Eli Bendersky | c7d23dd | 2012-02-12 06:12:10 +0000 | [diff] [blame] | 56 | } |
Michael J. Spencer | b60a18d | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 57 | |
Michael J. Spencer | b60a18d | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 58 | } // end namespace llvm |