summaryrefslogtreecommitdiff
path: root/lib/Object/ELFObjectFile.cpp
blob: ab5f8108af13a4edde11e06aa8235f60d15df098 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//===- ELFObjectFile.cpp - ELF object file implementation -------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Part of the ELFObjectFile class implementation.
//
//===----------------------------------------------------------------------===//

#include "llvm/Object/ELF.h"

namespace llvm {

using namespace object;

namespace {
  std::pair<unsigned char, unsigned char>
  getElfArchType(MemoryBuffer *Object) {
    if (Object->getBufferSize() < ELF::EI_NIDENT)
      return std::make_pair((uint8_t)ELF::ELFCLASSNONE,(uint8_t)ELF::ELFDATANONE);
    return std::make_pair( (uint8_t)Object->getBufferStart()[ELF::EI_CLASS]
                         , (uint8_t)Object->getBufferStart()[ELF::EI_DATA]);
  }
}

// Creates an in-memory object-file by default: createELFObjectFile(Buffer)
ObjectFile *ObjectFile::createELFObjectFile(MemoryBuffer *Object) {
  std::pair<unsigned char, unsigned char> Ident = getElfArchType(Object);
  error_code ec;

  if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2LSB)
    return new ELFObjectFile<support::little, false>(Object, ec);
  else if (Ident.first == ELF::ELFCLASS32 && Ident.second == ELF::ELFDATA2MSB)
    return new ELFObjectFile<support::big, false>(Object, ec);
  else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2MSB)
    return new ELFObjectFile<support::big, true>(Object, ec);
  else if (Ident.first == ELF::ELFCLASS64 && Ident.second == ELF::ELFDATA2LSB) {
    ELFObjectFile<support::little, true> *result =
          new ELFObjectFile<support::little, true>(Object, ec);
    return result;
  }

  report_fatal_error("Buffer is not an ELF object file!");
}

} // end namespace llvm