• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
23 using namespace llvm;
24 using namespace object;
25 
anchor()26 void ObjectFile::anchor() { }
27 
ObjectFile(unsigned int Type,MemoryBufferRef Source)28 ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
29     : SymbolicFile(Type, Source) {}
30 
containsSymbol(SymbolRef S) const31 bool SectionRef::containsSymbol(SymbolRef S) const {
32   Expected<section_iterator> SymSec = S.getSection();
33   if (!SymSec) {
34     // TODO: Actually report errors helpfully.
35     consumeError(SymSec.takeError());
36     return false;
37   }
38   return *this == **SymSec;
39 }
40 
getSymbolValue(DataRefImpl Ref) const41 uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref) const {
42   uint32_t Flags = getSymbolFlags(Ref);
43   if (Flags & SymbolRef::SF_Undefined)
44     return 0;
45   if (Flags & SymbolRef::SF_Common)
46     return getCommonSymbolSize(Ref);
47   return getSymbolValueImpl(Ref);
48 }
49 
printSymbolName(raw_ostream & OS,DataRefImpl Symb) const50 std::error_code ObjectFile::printSymbolName(raw_ostream &OS,
51                                             DataRefImpl Symb) const {
52   Expected<StringRef> Name = getSymbolName(Symb);
53   if (!Name)
54     return errorToErrorCode(Name.takeError());
55   OS << *Name;
56   return std::error_code();
57 }
58 
getSymbolAlignment(DataRefImpl DRI) const59 uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; }
60 
isSectionBitcode(DataRefImpl Sec) const61 bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const {
62   StringRef SectName;
63   if (!getSectionName(Sec, SectName))
64     return SectName == ".llvmbc";
65   return false;
66 }
67 
getRelocatedSection(DataRefImpl Sec) const68 section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
69   return section_iterator(SectionRef(Sec, this));
70 }
71 
72 Expected<std::unique_ptr<ObjectFile>>
createObjectFile(MemoryBufferRef Object,sys::fs::file_magic Type)73 ObjectFile::createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type) {
74   StringRef Data = Object.getBuffer();
75   if (Type == sys::fs::file_magic::unknown)
76     Type = sys::fs::identify_magic(Data);
77 
78   switch (Type) {
79   case sys::fs::file_magic::unknown:
80   case sys::fs::file_magic::bitcode:
81   case sys::fs::file_magic::archive:
82   case sys::fs::file_magic::macho_universal_binary:
83   case sys::fs::file_magic::windows_resource:
84     return errorCodeToError(object_error::invalid_file_type);
85   case sys::fs::file_magic::elf:
86   case sys::fs::file_magic::elf_relocatable:
87   case sys::fs::file_magic::elf_executable:
88   case sys::fs::file_magic::elf_shared_object:
89   case sys::fs::file_magic::elf_core:
90     return errorOrToExpected(createELFObjectFile(Object));
91   case sys::fs::file_magic::macho_object:
92   case sys::fs::file_magic::macho_executable:
93   case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
94   case sys::fs::file_magic::macho_core:
95   case sys::fs::file_magic::macho_preload_executable:
96   case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
97   case sys::fs::file_magic::macho_dynamic_linker:
98   case sys::fs::file_magic::macho_bundle:
99   case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
100   case sys::fs::file_magic::macho_dsym_companion:
101   case sys::fs::file_magic::macho_kext_bundle:
102     return createMachOObjectFile(Object);
103   case sys::fs::file_magic::coff_object:
104   case sys::fs::file_magic::coff_import_library:
105   case sys::fs::file_magic::pecoff_executable:
106     return errorOrToExpected(createCOFFObjectFile(Object));
107   }
108   llvm_unreachable("Unexpected Object File Type");
109 }
110 
111 Expected<OwningBinary<ObjectFile>>
createObjectFile(StringRef ObjectPath)112 ObjectFile::createObjectFile(StringRef ObjectPath) {
113   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
114       MemoryBuffer::getFile(ObjectPath);
115   if (std::error_code EC = FileOrErr.getError())
116     return errorCodeToError(EC);
117   std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
118 
119   Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
120       createObjectFile(Buffer->getMemBufferRef());
121   if (!ObjOrErr)
122     ObjOrErr.takeError();
123   std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
124 
125   return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
126 }
127