Michael J. Spencer | b84551a | 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 | f4eff4b | 2012-02-12 06:12:10 +0000 | [diff] [blame^] | 10 | // Part of the ELFObjectFile class implementation. |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Eli Bendersky | f4eff4b | 2012-02-12 06:12:10 +0000 | [diff] [blame^] | 14 | #include "llvm/Object/ELF.h" |
Eli Bendersky | 24973c1 | 2012-01-22 09:01:03 +0000 | [diff] [blame] | 15 | |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 16 | namespace llvm { |
| 17 | |
Eli Bendersky | f4eff4b | 2012-02-12 06:12:10 +0000 | [diff] [blame^] | 18 | using namespace object; |
Eli Bendersky | 24973c1 | 2012-01-22 09:01:03 +0000 | [diff] [blame] | 19 | |
Eli Bendersky | f4eff4b | 2012-02-12 06:12:10 +0000 | [diff] [blame^] | 20 | namespace { |
| 21 | std::pair<unsigned char, unsigned char> |
| 22 | getElfArchType(MemoryBuffer *Object) { |
| 23 | if (Object->getBufferSize() < ELF::EI_NIDENT) |
| 24 | return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE); |
| 25 | return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS] |
| 26 | , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]); |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 27 | } |
Eli Bendersky | f4eff4b | 2012-02-12 06:12:10 +0000 | [diff] [blame^] | 28 | } |
| 29 | |
| 30 | // Creates an in-memory object-file by default: createELFObjectFile(Buffer) |
| 31 | ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) { |
| 32 | std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object); |
| 33 | error_code ec; |
| 34 | |
| 35 | if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) |
| 36 | return new ELFObjectFile<support::little, false>(Object, ec); |
| 37 | else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) |
| 38 | return new ELFObjectFile<support::big, false>(Object, ec); |
| 39 | else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) |
| 40 | return new ELFObjectFile<support::big, true>(Object, ec); |
| 41 | else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) { |
| 42 | ELFObjectFile<support::little, true> *result = |
| 43 | new ELFObjectFile<support::little, true>(Object, ec); |
| 44 | return result; |
| 45 | } |
| 46 | |
| 47 | report_fatal_error("Buffer is not an ELF object file!"); |
| 48 | } |
Michael J. Spencer | b84551a | 2011-01-20 06:38:47 +0000 | [diff] [blame] | 49 | |
| 50 | } // end namespace llvm |