12754fe60SDimitry Andric //===- ELFObjectFile.cpp - ELF object file implementation -------*- C++ -*-===// 22754fe60SDimitry Andric // 32754fe60SDimitry Andric // The LLVM Compiler Infrastructure 42754fe60SDimitry Andric // 52754fe60SDimitry Andric // This file is distributed under the University of Illinois Open Source 62754fe60SDimitry Andric // License. See LICENSE.TXT for details. 72754fe60SDimitry Andric // 82754fe60SDimitry Andric //===----------------------------------------------------------------------===// 92754fe60SDimitry Andric // 10dff0c46cSDimitry Andric // Part of the ELFObjectFile class implementation. 112754fe60SDimitry Andric // 122754fe60SDimitry Andric //===----------------------------------------------------------------------===// 132754fe60SDimitry Andric 14dff0c46cSDimitry Andric #include "llvm/Object/ELF.h" 152754fe60SDimitry Andric 16dff0c46cSDimitry Andric namespace llvm { 17dff0c46cSDimitry Andric 182754fe60SDimitry Andric using namespace object; 192754fe60SDimitry Andric 202754fe60SDimitry Andric namespace { 21dff0c46cSDimitry Andric std::pair<unsigned char, unsigned char> 222754fe60SDimitry Andric getElfArchType(MemoryBuffer *Object) { 232754fe60SDimitry Andric if (Object->getBufferSize() < ELF::EI_NIDENT) 242754fe60SDimitry Andric return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE); 252754fe60SDimitry Andric return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS] 262754fe60SDimitry Andric , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]); 272754fe60SDimitry Andric } 28dff0c46cSDimitry Andric } 292754fe60SDimitry Andric 30dff0c46cSDimitry Andric // Creates an in-memory object-file by default: createELFObjectFile(Buffer) 312754fe60SDimitry Andric ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) { 322754fe60SDimitry Andric std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object); 3317a519f9SDimitry Andric error_code ec; 34dff0c46cSDimitry Andric 352754fe60SDimitry Andric if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB) 3617a519f9SDimitry Andric return new ELFObjectFile<support::little, false>(Object, ec); 372754fe60SDimitry Andric else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB) 3817a519f9SDimitry Andric return new ELFObjectFile<support::big, false>(Object, ec); 392754fe60SDimitry Andric else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB) 4017a519f9SDimitry Andric return new ELFObjectFile<support::big, true>(Object, ec); 41dff0c46cSDimitry Andric else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) { 42dff0c46cSDimitry Andric ELFObjectFile<support::little, true> *result = 43dff0c46cSDimitry Andric new ELFObjectFile<support::little, true>(Object, ec); 44dff0c46cSDimitry Andric return result; 45dff0c46cSDimitry Andric } 46dff0c46cSDimitry Andric 47dff0c46cSDimitry Andric report_fatal_error("Buffer is not an ELF object file!"); 482754fe60SDimitry Andric } 492754fe60SDimitry Andric 502754fe60SDimitry Andric } // end namespace llvm 51