LLVM API Documentation
00001 //===- ObjectFile.cpp - File format independent object file -----*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines a file format independent ObjectFile class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Object/COFF.h" 00015 #include "llvm/Object/MachO.h" 00016 #include "llvm/Object/ObjectFile.h" 00017 #include "llvm/Support/ErrorHandling.h" 00018 #include "llvm/Support/FileSystem.h" 00019 #include "llvm/Support/MemoryBuffer.h" 00020 #include "llvm/Support/raw_ostream.h" 00021 #include <system_error> 00022 00023 using namespace llvm; 00024 using namespace object; 00025 00026 void ObjectFile::anchor() { } 00027 00028 ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source) 00029 : SymbolicFile(Type, Source) {} 00030 00031 std::error_code ObjectFile::printSymbolName(raw_ostream &OS, 00032 DataRefImpl Symb) const { 00033 StringRef Name; 00034 if (std::error_code EC = getSymbolName(Symb, Name)) 00035 return EC; 00036 OS << Name; 00037 return object_error::success; 00038 } 00039 00040 std::error_code ObjectFile::getSymbolAlignment(DataRefImpl DRI, 00041 uint32_t &Result) const { 00042 Result = 0; 00043 return object_error::success; 00044 } 00045 00046 section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const { 00047 return section_iterator(SectionRef(Sec, this)); 00048 } 00049 00050 ErrorOr<std::unique_ptr<ObjectFile>> 00051 ObjectFile::createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type) { 00052 StringRef Data = Object.getBuffer(); 00053 if (Type == sys::fs::file_magic::unknown) 00054 Type = sys::fs::identify_magic(Data); 00055 00056 switch (Type) { 00057 case sys::fs::file_magic::unknown: 00058 case sys::fs::file_magic::bitcode: 00059 case sys::fs::file_magic::archive: 00060 case sys::fs::file_magic::macho_universal_binary: 00061 case sys::fs::file_magic::windows_resource: 00062 return object_error::invalid_file_type; 00063 case sys::fs::file_magic::elf_relocatable: 00064 case sys::fs::file_magic::elf_executable: 00065 case sys::fs::file_magic::elf_shared_object: 00066 case sys::fs::file_magic::elf_core: 00067 return createELFObjectFile(Object); 00068 case sys::fs::file_magic::macho_object: 00069 case sys::fs::file_magic::macho_executable: 00070 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib: 00071 case sys::fs::file_magic::macho_core: 00072 case sys::fs::file_magic::macho_preload_executable: 00073 case sys::fs::file_magic::macho_dynamically_linked_shared_lib: 00074 case sys::fs::file_magic::macho_dynamic_linker: 00075 case sys::fs::file_magic::macho_bundle: 00076 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub: 00077 case sys::fs::file_magic::macho_dsym_companion: 00078 return createMachOObjectFile(Object); 00079 case sys::fs::file_magic::coff_object: 00080 case sys::fs::file_magic::coff_import_library: 00081 case sys::fs::file_magic::pecoff_executable: 00082 return createCOFFObjectFile(Object); 00083 } 00084 llvm_unreachable("Unexpected Object File Type"); 00085 } 00086 00087 ErrorOr<OwningBinary<ObjectFile>> 00088 ObjectFile::createObjectFile(StringRef ObjectPath) { 00089 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 00090 MemoryBuffer::getFile(ObjectPath); 00091 if (std::error_code EC = FileOrErr.getError()) 00092 return EC; 00093 std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get()); 00094 00095 ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr = 00096 createObjectFile(Buffer->getMemBufferRef()); 00097 if (std::error_code EC = ObjOrErr.getError()) 00098 return EC; 00099 std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get()); 00100 00101 return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer)); 00102 }