1 //===- Binary.cpp - A generic binary file -----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Binary class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Object/Binary.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Support/FileSystem.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/Path.h"
20 // Include headers for createBinary.
21 #include "llvm/Object/Archive.h"
22 #include "llvm/Object/COFF.h"
23 #include "llvm/Object/MachOUniversal.h"
24 #include "llvm/Object/ObjectFile.h"
27 using namespace object;
33 Binary::Binary(unsigned int Type, MemoryBuffer *Source)
37 StringRef Binary::getData() const {
38 return Data->getBuffer();
41 StringRef Binary::getFileName() const {
42 return Data->getBufferIdentifier();
45 ErrorOr<Binary *> object::createBinary(MemoryBuffer *Source) {
46 OwningPtr<MemoryBuffer> scopedSource(Source);
47 sys::fs::file_magic type = sys::fs::identify_magic(Source->getBuffer());
50 case sys::fs::file_magic::archive: {
51 OwningPtr<Binary> Ret(new Archive(scopedSource.take(), EC));
56 case sys::fs::file_magic::elf_relocatable:
57 case sys::fs::file_magic::elf_executable:
58 case sys::fs::file_magic::elf_shared_object:
59 case sys::fs::file_magic::elf_core: {
60 OwningPtr<Binary> Ret(
61 ObjectFile::createELFObjectFile(scopedSource.take()));
63 return object_error::invalid_file_type;
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 OwningPtr<Binary> Ret(
77 ObjectFile::createMachOObjectFile(scopedSource.take()));
79 return object_error::invalid_file_type;
82 case sys::fs::file_magic::macho_universal_binary: {
83 OwningPtr<Binary> Ret(new MachOUniversalBinary(scopedSource.take(), EC));
88 case sys::fs::file_magic::coff_object:
89 case sys::fs::file_magic::coff_import_library:
90 case sys::fs::file_magic::pecoff_executable: {
91 OwningPtr<Binary> Ret(
92 ObjectFile::createCOFFObjectFile(scopedSource.take()));
94 return object_error::invalid_file_type;
97 case sys::fs::file_magic::unknown:
98 case sys::fs::file_magic::bitcode:
99 case sys::fs::file_magic::windows_resource: {
100 // Unrecognized object file format.
101 return object_error::invalid_file_type;
104 llvm_unreachable("Unexpected Binary File Type");
107 ErrorOr<Binary *> object::createBinary(StringRef Path) {
108 OwningPtr<MemoryBuffer> File;
109 if (error_code EC = MemoryBuffer::getFileOrSTDIN(Path, File))
111 return createBinary(File.take());