blob: 1d1dafdc8f42c9d93d3973165de7918ddd70c38e [file] [log] [blame]
Michael J. Spencer68b3f0c2010-11-15 03:21:41 +00001//===- 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"
Michael J. Spencer3ff95632010-12-16 03:29:14 +000015#include "llvm/ADT/OwningPtr.h"
Michael J. Spencer68b3f0c2010-11-15 03:21:41 +000016#include "llvm/Support/ErrorHandling.h"
Rafael Espindolad27a9782013-06-11 15:19:04 +000017#include "llvm/Support/FileSystem.h"
Michael J. Spencer68b3f0c2010-11-15 03:21:41 +000018#include "llvm/Support/MemoryBuffer.h"
Michael J. Spencer333fb042010-12-09 17:36:48 +000019#include "llvm/Support/system_error.h"
Michael J. Spencer68b3f0c2010-11-15 03:21:41 +000020
Michael J. Spencer88af6b92010-11-16 01:06:51 +000021using namespace llvm;
22using namespace object;
Michael J. Spencer68b3f0c2010-11-15 03:21:41 +000023
David Blaikie2d24e2a2011-12-20 02:50:00 +000024void ObjectFile::anchor() { }
25
Rafael Espindola2c6f9972013-04-07 16:40:00 +000026ObjectFile::ObjectFile(unsigned int Type, MemoryBuffer *source)
Michael J. Spencer001c9202011-06-25 17:54:50 +000027 : Binary(Type, source) {
Michael J. Spencer68b3f0c2010-11-15 03:21:41 +000028}
29
Rafael Espindola59a0e792013-04-29 22:24:22 +000030error_code ObjectFile::getSymbolAlignment(DataRefImpl DRI,
31 uint32_t &Result) const {
32 Result = 0;
33 return object_error::success;
34}
35
Rafael Espindola7486d922013-05-30 03:05:14 +000036section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
37 return section_iterator(SectionRef(Sec, this));
38}
39
Michael J. Spencer68b3f0c2010-11-15 03:21:41 +000040ObjectFile *ObjectFile::createObjectFile(MemoryBuffer *Object) {
Rafael Espindola79768422013-07-24 14:00:26 +000041 if (Object->getBufferSize() < 64) {
42 delete Object;
Michael J. Spencer68b3f0c2010-11-15 03:21:41 +000043 return 0;
Rafael Espindola79768422013-07-24 14:00:26 +000044 }
Rafael Espindolad27a9782013-06-11 15:19:04 +000045
46 sys::fs::file_magic Type = sys::fs::identify_magic(Object->getBuffer());
47 switch (Type) {
48 case sys::fs::file_magic::unknown:
Rafael Espindola4fbf6632013-06-11 15:29:10 +000049 case sys::fs::file_magic::bitcode:
50 case sys::fs::file_magic::archive:
Alexey Samsonov9c22f872013-06-18 15:03:28 +000051 case sys::fs::file_magic::macho_universal_binary:
Rafael Espindola79768422013-07-24 14:00:26 +000052 delete Object;
Rafael Espindolad27a9782013-06-11 15:19:04 +000053 return 0;
54 case sys::fs::file_magic::elf_relocatable:
55 case sys::fs::file_magic::elf_executable:
56 case sys::fs::file_magic::elf_shared_object:
57 case sys::fs::file_magic::elf_core:
58 return createELFObjectFile(Object);
59 case sys::fs::file_magic::macho_object:
60 case sys::fs::file_magic::macho_executable:
61 case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
62 case sys::fs::file_magic::macho_core:
63 case sys::fs::file_magic::macho_preload_executable:
64 case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
65 case sys::fs::file_magic::macho_dynamic_linker:
66 case sys::fs::file_magic::macho_bundle:
67 case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
68 case sys::fs::file_magic::macho_dsym_companion:
69 return createMachOObjectFile(Object);
70 case sys::fs::file_magic::coff_object:
71 case sys::fs::file_magic::pecoff_executable:
72 return createCOFFObjectFile(Object);
Michael J. Spencer68b3f0c2010-11-15 03:21:41 +000073 }
Rafael Espindolad27a9782013-06-11 15:19:04 +000074 llvm_unreachable("Unexpected Object File Type");
Michael J. Spencer68b3f0c2010-11-15 03:21:41 +000075}
76
Michael J. Spencer88af6b92010-11-16 01:06:51 +000077ObjectFile *ObjectFile::createObjectFile(StringRef ObjectPath) {
Michael J. Spencer3ff95632010-12-16 03:29:14 +000078 OwningPtr<MemoryBuffer> File;
Rafael Espindolaaba65b02011-12-25 01:20:19 +000079 if (MemoryBuffer::getFile(ObjectPath, File))
Michael J. Spencer3ff95632010-12-16 03:29:14 +000080 return NULL;
81 return createObjectFile(File.take());
Michael J. Spencer68b3f0c2010-11-15 03:21:41 +000082}