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 // 10 // Part of the ELFObjectFile class implementation. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/Object/ELF.h" 15 16 namespace llvm { 17 18 using namespace object; 19 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]); 27 } 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 } 49 50 } // end namespace llvm 51