ObjectFile.cpp 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include "llvm/Object/ObjectFile.h"
  14. #include "llvm/ADT/OwningPtr.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 "llvm/Support/system_error.h"
  20. using namespace llvm;
  21. using namespace object;
  22. void ObjectFile::anchor() { }
  23. ObjectFile::ObjectFile(unsigned int Type, MemoryBuffer *Source,
  24. bool BufferOwned)
  25. : SymbolicFile(Type, Source, BufferOwned) {}
  26. error_code ObjectFile::printSymbolName(raw_ostream &OS,
  27. DataRefImpl Symb) const {
  28. StringRef Name;
  29. if (error_code EC = getSymbolName(Symb, Name))
  30. return EC;
  31. OS << Name;
  32. return object_error::success;
  33. }
  34. error_code ObjectFile::getSymbolAlignment(DataRefImpl DRI,
  35. uint32_t &Result) const {
  36. Result = 0;
  37. return object_error::success;
  38. }
  39. section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
  40. return section_iterator(SectionRef(Sec, this));
  41. }
  42. ErrorOr<ObjectFile *> ObjectFile::createObjectFile(MemoryBuffer *Object,
  43. bool BufferOwned,
  44. sys::fs::file_magic Type) {
  45. if (Type == sys::fs::file_magic::unknown)
  46. Type = sys::fs::identify_magic(Object->getBuffer());
  47. switch (Type) {
  48. case sys::fs::file_magic::unknown:
  49. case sys::fs::file_magic::bitcode:
  50. case sys::fs::file_magic::archive:
  51. case sys::fs::file_magic::macho_universal_binary:
  52. case sys::fs::file_magic::windows_resource:
  53. if (BufferOwned)
  54. delete Object;
  55. return object_error::invalid_file_type;
  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. return createELFObjectFile(Object, BufferOwned);
  61. case sys::fs::file_magic::macho_object:
  62. case sys::fs::file_magic::macho_executable:
  63. case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
  64. case sys::fs::file_magic::macho_core:
  65. case sys::fs::file_magic::macho_preload_executable:
  66. case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
  67. case sys::fs::file_magic::macho_dynamic_linker:
  68. case sys::fs::file_magic::macho_bundle:
  69. case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
  70. case sys::fs::file_magic::macho_dsym_companion:
  71. return createMachOObjectFile(Object, BufferOwned);
  72. case sys::fs::file_magic::coff_object:
  73. case sys::fs::file_magic::coff_import_library:
  74. case sys::fs::file_magic::pecoff_executable:
  75. return createCOFFObjectFile(Object, BufferOwned);
  76. }
  77. llvm_unreachable("Unexpected Object File Type");
  78. }
  79. ErrorOr<ObjectFile *> ObjectFile::createObjectFile(StringRef ObjectPath) {
  80. OwningPtr<MemoryBuffer> File;
  81. if (error_code EC = MemoryBuffer::getFile(ObjectPath, File))
  82. return EC;
  83. return createObjectFile(File.take());
  84. }