ObjectFile.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/COFF.h"
  14. #include "llvm/Object/MachO.h"
  15. #include "llvm/Object/ObjectFile.h"
  16. #include "llvm/Support/ErrorHandling.h"
  17. #include "llvm/Support/FileSystem.h"
  18. #include "llvm/Support/MemoryBuffer.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. #include <system_error>
  21. using namespace llvm;
  22. using namespace object;
  23. void ObjectFile::anchor() { }
  24. ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
  25. : SymbolicFile(Type, Source) {}
  26. bool SectionRef::containsSymbol(SymbolRef S) const {
  27. section_iterator SymSec = getObject()->section_end();
  28. if (S.getSection(SymSec))
  29. return false;
  30. return *this == *SymSec;
  31. }
  32. uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref) const {
  33. uint32_t Flags = getSymbolFlags(Ref);
  34. if (Flags & SymbolRef::SF_Undefined)
  35. return 0;
  36. if (Flags & SymbolRef::SF_Common)
  37. return getCommonSymbolSize(Ref);
  38. return getSymbolValueImpl(Ref);
  39. }
  40. std::error_code ObjectFile::printSymbolName(raw_ostream &OS,
  41. DataRefImpl Symb) const {
  42. ErrorOr<StringRef> Name = getSymbolName(Symb);
  43. if (std::error_code EC = Name.getError())
  44. return EC;
  45. OS << *Name;
  46. return std::error_code();
  47. }
  48. uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; }
  49. section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
  50. return section_iterator(SectionRef(Sec, this));
  51. }
  52. ErrorOr<std::unique_ptr<ObjectFile>>
  53. ObjectFile::createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type) {
  54. StringRef Data = Object.getBuffer();
  55. if (Type == sys::fs::file_magic::unknown)
  56. Type = sys::fs::identify_magic(Data);
  57. switch (Type) {
  58. case sys::fs::file_magic::unknown:
  59. case sys::fs::file_magic::bitcode:
  60. case sys::fs::file_magic::archive:
  61. case sys::fs::file_magic::macho_universal_binary:
  62. case sys::fs::file_magic::windows_resource:
  63. return object_error::invalid_file_type;
  64. case sys::fs::file_magic::elf:
  65. case sys::fs::file_magic::elf_relocatable:
  66. case sys::fs::file_magic::elf_executable:
  67. case sys::fs::file_magic::elf_shared_object:
  68. case sys::fs::file_magic::elf_core:
  69. // return createELFObjectFile(Object); // HLSL Change - remove support for ELF files
  70. case sys::fs::file_magic::macho_object:
  71. case sys::fs::file_magic::macho_executable:
  72. case sys::fs::file_magic::macho_fixed_virtual_memory_shared_lib:
  73. case sys::fs::file_magic::macho_core:
  74. case sys::fs::file_magic::macho_preload_executable:
  75. case sys::fs::file_magic::macho_dynamically_linked_shared_lib:
  76. case sys::fs::file_magic::macho_dynamic_linker:
  77. case sys::fs::file_magic::macho_bundle:
  78. case sys::fs::file_magic::macho_dynamically_linked_shared_lib_stub:
  79. case sys::fs::file_magic::macho_dsym_companion:
  80. case sys::fs::file_magic::macho_kext_bundle:
  81. // return createMachOObjectFile(Object); // HLSL Change - remove support for Mach-O files
  82. return object_error::invalid_file_type; // HLSL Change
  83. case sys::fs::file_magic::coff_object:
  84. case sys::fs::file_magic::coff_import_library:
  85. case sys::fs::file_magic::pecoff_executable:
  86. return createCOFFObjectFile(Object);
  87. }
  88. llvm_unreachable("Unexpected Object File Type");
  89. }
  90. ErrorOr<OwningBinary<ObjectFile>>
  91. ObjectFile::createObjectFile(StringRef ObjectPath) {
  92. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  93. MemoryBuffer::getFile(ObjectPath);
  94. if (std::error_code EC = FileOrErr.getError())
  95. return EC;
  96. std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
  97. ErrorOr<std::unique_ptr<ObjectFile>> ObjOrErr =
  98. createObjectFile(Buffer->getMemBufferRef());
  99. if (std::error_code EC = ObjOrErr.getError())
  100. return EC;
  101. std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
  102. return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
  103. }