ObjectFile.cpp revision 288943
1//===- ObjectFile.cpp - File format independent object file -----*- 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// This file defines a file format independent ObjectFile class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Object/COFF.h"
15#include "llvm/Object/MachO.h"
16#include "llvm/Object/ObjectFile.h"
17#include "llvm/Support/ErrorHandling.h"
18#include "llvm/Support/FileSystem.h"
19#include "llvm/Support/MemoryBuffer.h"
20#include "llvm/Support/raw_ostream.h"
21#include <system_error>
22
23using namespace llvm;
24using namespace object;
25
26void ObjectFile::anchor() { }
27
28ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
29    : SymbolicFile(Type, Source) {}
30
31bool SectionRef::containsSymbol(SymbolRef S) const {
32  section_iterator SymSec = getObject()->section_end();
33  if (S.getSection(SymSec))
34    return false;
35  return *this == *SymSec;
36}
37
38uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref) const {
39  uint32_t Flags = getSymbolFlags(Ref);
40  if (Flags & SymbolRef::SF_Undefined)
41    return 0;
42  if (Flags & SymbolRef::SF_Common)
43    return getCommonSymbolSize(Ref);
44  return getSymbolValueImpl(Ref);
45}
46
47std::error_code ObjectFile::printSymbolName(raw_ostream &OS,
48                                            DataRefImpl Symb) const {
49  ErrorOr<StringRef> Name = getSymbolName(Symb);
50  if (std::error_code EC = Name.getError())
51    return EC;
52  OS << *Name;
53  return std::error_code();
54}
55
56uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; }
57
58section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
59  return section_iterator(SectionRef(Sec, this));
60}
61
62ErrorOr<std::unique_ptr<ObjectFile>>
63ObjectFile::createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type) {
64  StringRef Data = Object.getBuffer();
65  if (Type == sys::fs::file_magic::unknown)
66    Type = sys::fs::identify_magic(Data);
67
68  switch (Type) {
69  case sys::fs::file_magic::unknown:
70  case sys::fs::file_magic::bitcode:
71  case sys::fs::file_magic::archive:
72  case sys::fs::file_magic::macho_universal_binary:
73  case sys::fs::file_magic::windows_resource:
74    return object_error::invalid_file_type;
75  case sys::fs::file_magic::elf:
76  case sys::fs::file_magic::elf_relocatable:
77  case sys::fs::file_magic::elf_executable:
78  case sys::fs::file_magic::elf_shared_object:
79  case sys::fs::file_magic::elf_core:
80    return createELFObjectFile(Object);
81  case sys::fs::file_magic::macho_object:
82  case sys::fs::file_magic::macho_executable:
83  case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
84  case sys::fs::file_magic::macho_core:
85  case sys::fs::file_magic::macho_preload_executable:
86  case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
87  case sys::fs::file_magic::macho_dynamic_linker:
88  case sys::fs::file_magic::macho_bundle:
89  case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
90  case sys::fs::file_magic::macho_dsym_companion:
91  case sys::fs::file_magic::macho_kext_bundle:
92    return createMachOObjectFile(Object);
93  case sys::fs::file_magic::coff_object:
94  case sys::fs::file_magic::coff_import_library:
95  case sys::fs::file_magic::pecoff_executable:
96    return createCOFFObjectFile(Object);
97  }
98  llvm_unreachable("Unexpected Object File Type");
99}
100
101ErrorOr<OwningBinary<ObjectFile>>
102ObjectFile::createObjectFile(StringRef ObjectPath) {
103  ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
104      MemoryBuffer::getFile(ObjectPath);
105  if (std::error_code EC = FileOrErr.getError())
106    return EC;
107  std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
108
109  ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
110      createObjectFile(Buffer->getMemBufferRef());
111  if (std::error_code EC = ObjOrErr.getError())
112    return EC;
113  std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
114
115  return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
116}
117