ObjectFile.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //===- ObjectFile.cpp - File format independent object file ---------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines a file format independent ObjectFile class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Object/ObjectFile.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/BinaryFormat/Magic.h"
  15. #include "llvm/Object/Binary.h"
  16. #include "llvm/Object/COFF.h"
  17. #include "llvm/Object/Error.h"
  18. #include "llvm/Object/MachO.h"
  19. #include "llvm/Object/Wasm.h"
  20. #include "llvm/Support/Error.h"
  21. #include "llvm/Support/ErrorHandling.h"
  22. #include "llvm/Support/ErrorOr.h"
  23. #include "llvm/Support/FileSystem.h"
  24. #include "llvm/Support/MemoryBuffer.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <algorithm>
  27. #include <cstdint>
  28. #include <memory>
  29. #include <system_error>
  30. using namespace llvm;
  31. using namespace object;
  32. void ObjectFile::anchor() {}
  33. ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
  34. : SymbolicFile(Type, Source) {}
  35. bool SectionRef::containsSymbol(SymbolRef S) const {
  36. Expected<section_iterator> SymSec = S.getSection();
  37. if (!SymSec) {
  38. // TODO: Actually report errors helpfully.
  39. consumeError(SymSec.takeError());
  40. return false;
  41. }
  42. return *this == **SymSec;
  43. }
  44. uint64_t ObjectFile::getSymbolValue(DataRefImpl Ref) const {
  45. uint32_t Flags = getSymbolFlags(Ref);
  46. if (Flags & SymbolRef::SF_Undefined)
  47. return 0;
  48. if (Flags & SymbolRef::SF_Common)
  49. return getCommonSymbolSize(Ref);
  50. return getSymbolValueImpl(Ref);
  51. }
  52. Error ObjectFile::printSymbolName(raw_ostream &OS, DataRefImpl Symb) const {
  53. Expected<StringRef> Name = getSymbolName(Symb);
  54. if (!Name)
  55. return Name.takeError();
  56. OS << *Name;
  57. return Error::success();
  58. }
  59. uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; }
  60. bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const {
  61. if (Expected<StringRef> NameOrErr = getSectionName(Sec))
  62. return *NameOrErr == ".llvmbc";
  63. return false;
  64. }
  65. bool ObjectFile::isSectionStripped(DataRefImpl Sec) const { return false; }
  66. bool ObjectFile::isBerkeleyText(DataRefImpl Sec) const {
  67. return isSectionText(Sec);
  68. }
  69. bool ObjectFile::isBerkeleyData(DataRefImpl Sec) const {
  70. return isSectionData(Sec);
  71. }
  72. section_iterator ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
  73. return section_iterator(SectionRef(Sec, this));
  74. }
  75. Triple ObjectFile::makeTriple() const {
  76. Triple TheTriple;
  77. auto Arch = getArch();
  78. TheTriple.setArch(Triple::ArchType(Arch));
  79. // For ARM targets, try to use the build attributes to build determine
  80. // the build target. Target features are also added, but later during
  81. // disassembly.
  82. if (Arch == Triple::arm || Arch == Triple::armeb)
  83. setARMSubArch(TheTriple);
  84. // TheTriple defaults to ELF, and COFF doesn't have an environment:
  85. // the best we can do here is indicate that it is mach-o.
  86. if (isMachO())
  87. TheTriple.setObjectFormat(Triple::MachO);
  88. if (isCOFF()) {
  89. const auto COFFObj = dyn_cast<COFFObjectFile>(this);
  90. if (COFFObj->getArch() == Triple::thumb)
  91. TheTriple.setTriple("thumbv7-windows");
  92. }
  93. return TheTriple;
  94. }
  95. Expected<std::unique_ptr<ObjectFile>>
  96. ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type) {
  97. StringRef Data = Object.getBuffer();
  98. if (Type == file_magic::unknown)
  99. Type = identify_magic(Data);
  100. switch (Type) {
  101. case file_magic::unknown:
  102. case file_magic::bitcode:
  103. case file_magic::coff_cl_gl_object:
  104. case file_magic::archive:
  105. case file_magic::macho_universal_binary:
  106. case file_magic::windows_resource:
  107. case file_magic::pdb:
  108. case file_magic::minidump:
  109. return errorCodeToError(object_error::invalid_file_type);
  110. case file_magic::elf:
  111. case file_magic::elf_relocatable:
  112. case file_magic::elf_executable:
  113. case file_magic::elf_shared_object:
  114. case file_magic::elf_core:
  115. return createELFObjectFile(Object);
  116. case file_magic::macho_object:
  117. case file_magic::macho_executable:
  118. case file_magic::macho_fixed_virtual_memory_shared_lib:
  119. case file_magic::macho_core:
  120. case file_magic::macho_preload_executable:
  121. case file_magic::macho_dynamically_linked_shared_lib:
  122. case file_magic::macho_dynamic_linker:
  123. case file_magic::macho_bundle:
  124. case file_magic::macho_dynamically_linked_shared_lib_stub:
  125. case file_magic::macho_dsym_companion:
  126. case file_magic::macho_kext_bundle:
  127. return createMachOObjectFile(Object);
  128. case file_magic::coff_object:
  129. case file_magic::coff_import_library:
  130. case file_magic::pecoff_executable:
  131. return createCOFFObjectFile(Object);
  132. case file_magic::xcoff_object_32:
  133. return createXCOFFObjectFile(Object);
  134. case file_magic::wasm_object:
  135. return createWasmObjectFile(Object);
  136. }
  137. llvm_unreachable("Unexpected Object File Type");
  138. }
  139. Expected<OwningBinary<ObjectFile>>
  140. ObjectFile::createObjectFile(StringRef ObjectPath) {
  141. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  142. MemoryBuffer::getFile(ObjectPath);
  143. if (std::error_code EC = FileOrErr.getError())
  144. return errorCodeToError(EC);
  145. std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
  146. Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
  147. createObjectFile(Buffer->getMemBufferRef());
  148. if (Error Err = ObjOrErr.takeError())
  149. return std::move(Err);
  150. std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
  151. return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
  152. }