Home | History | Annotate | Download | only in Object
      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/ObjectFile.h"
     15 #include "llvm/Support/ErrorHandling.h"
     16 #include "llvm/Support/FileSystem.h"
     17 #include "llvm/Support/MemoryBuffer.h"
     18 #include "llvm/Support/raw_ostream.h"
     19 #include <system_error>
     20 
     21 using namespace llvm;
     22 using namespace object;
     23 
     24 void ObjectFile::anchor() { }
     25 
     26 ObjectFile::ObjectFile(unsigned int Type, std::unique_ptr<MemoryBuffer> Source)
     27     : SymbolicFile(Type, std::move(Source)) {}
     28 
     29 std::error_code ObjectFile::printSymbolName(raw_ostream &OS,
     30                                             DataRefImpl Symb) const {
     31   StringRef Name;
     32   if (std::error_code EC = getSymbolName(Symb, Name))
     33     return EC;
     34   OS << Name;
     35   return object_error::success;
     36 }
     37 
     38 std::error_code ObjectFile::getSymbolAlignment(DataRefImpl DRI,
     39                                                uint32_t &Result) const {
     40   Result = 0;
     41   return object_error::success;
     42 }
     43 
     44 section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
     45   return section_iterator(SectionRef(Sec, this));
     46 }
     47 
     48 ErrorOr<ObjectFile *>
     49 ObjectFile::createObjectFile(std::unique_ptr<MemoryBuffer> &Object,
     50                              sys::fs::file_magic Type) {
     51   if (Type == sys::fs::file_magic::unknown)
     52     Type = sys::fs::identify_magic(Object->getBuffer());
     53 
     54   switch (Type) {
     55   case sys::fs::file_magic::unknown:
     56   case sys::fs::file_magic::bitcode:
     57   case sys::fs::file_magic::archive:
     58   case sys::fs::file_magic::macho_universal_binary:
     59   case sys::fs::file_magic::windows_resource:
     60     return object_error::invalid_file_type;
     61   case sys::fs::file_magic::elf_relocatable:
     62   case sys::fs::file_magic::elf_executable:
     63   case sys::fs::file_magic::elf_shared_object:
     64   case sys::fs::file_magic::elf_core:
     65     return createELFObjectFile(Object);
     66   case sys::fs::file_magic::macho_object:
     67   case sys::fs::file_magic::macho_executable:
     68   case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
     69   case sys::fs::file_magic::macho_core:
     70   case sys::fs::file_magic::macho_preload_executable:
     71   case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
     72   case sys::fs::file_magic::macho_dynamic_linker:
     73   case sys::fs::file_magic::macho_bundle:
     74   case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
     75   case sys::fs::file_magic::macho_dsym_companion:
     76     return createMachOObjectFile(Object);
     77   case sys::fs::file_magic::coff_object:
     78   case sys::fs::file_magic::coff_import_library:
     79   case sys::fs::file_magic::pecoff_executable:
     80     return createCOFFObjectFile(std::move(Object));
     81   }
     82   llvm_unreachable("Unexpected Object File Type");
     83 }
     84 
     85 ErrorOr<ObjectFile *> ObjectFile::createObjectFile(StringRef ObjectPath) {
     86   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
     87       MemoryBuffer::getFile(ObjectPath);
     88   if (std::error_code EC = FileOrErr.getError())
     89     return EC;
     90   return createObjectFile(FileOrErr.get());
     91 }
     92