ObjectFile.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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/Format.h"
  24. #include "llvm/Support/MemoryBuffer.h"
  25. #include "llvm/Support/raw_ostream.h"
  26. #include <cstdint>
  27. #include <memory>
  28. #include <system_error>
  29. using namespace llvm;
  30. using namespace object;
  31. raw_ostream &object::operator<<(raw_ostream &OS, const SectionedAddress &Addr) {
  32. OS << "SectionedAddress{" << format_hex(Addr.Address, 10);
  33. if (Addr.SectionIndex != SectionedAddress::UndefSection)
  34. OS << ", " << Addr.SectionIndex;
  35. return OS << "}";
  36. }
  37. void ObjectFile::anchor() {}
  38. ObjectFile::ObjectFile(unsigned int Type, MemoryBufferRef Source)
  39. : SymbolicFile(Type, Source) {}
  40. bool SectionRef::containsSymbol(SymbolRef S) const {
  41. Expected<section_iterator> SymSec = S.getSection();
  42. if (!SymSec) {
  43. // TODO: Actually report errors helpfully.
  44. consumeError(SymSec.takeError());
  45. return false;
  46. }
  47. return *this == **SymSec;
  48. }
  49. Expected<uint64_t> ObjectFile::getSymbolValue(DataRefImpl Ref) const {
  50. uint32_t Flags;
  51. if (Error E = getSymbolFlags(Ref).moveInto(Flags))
  52. // TODO: Test this error.
  53. return std::move(E);
  54. if (Flags & SymbolRef::SF_Undefined)
  55. return 0;
  56. if (Flags & SymbolRef::SF_Common)
  57. return getCommonSymbolSize(Ref);
  58. return getSymbolValueImpl(Ref);
  59. }
  60. Error ObjectFile::printSymbolName(raw_ostream &OS, DataRefImpl Symb) const {
  61. Expected<StringRef> Name = getSymbolName(Symb);
  62. if (!Name)
  63. return Name.takeError();
  64. OS << *Name;
  65. return Error::success();
  66. }
  67. uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; }
  68. bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const {
  69. Expected<StringRef> NameOrErr = getSectionName(Sec);
  70. if (NameOrErr)
  71. return *NameOrErr == ".llvmbc";
  72. consumeError(NameOrErr.takeError());
  73. return false;
  74. }
  75. bool ObjectFile::isSectionStripped(DataRefImpl Sec) const { return false; }
  76. bool ObjectFile::isBerkeleyText(DataRefImpl Sec) const {
  77. return isSectionText(Sec);
  78. }
  79. bool ObjectFile::isBerkeleyData(DataRefImpl Sec) const {
  80. return isSectionData(Sec);
  81. }
  82. bool ObjectFile::isDebugSection(DataRefImpl Sec) const { return false; }
  83. bool ObjectFile::hasDebugInfo() const {
  84. return any_of(sections(),
  85. [](SectionRef Sec) { return Sec.isDebugSection(); });
  86. }
  87. Expected<section_iterator>
  88. ObjectFile::getRelocatedSection(DataRefImpl Sec) const {
  89. return section_iterator(SectionRef(Sec, this));
  90. }
  91. Triple ObjectFile::makeTriple() const {
  92. Triple TheTriple;
  93. auto Arch = getArch();
  94. TheTriple.setArch(Triple::ArchType(Arch));
  95. // For ARM targets, try to use the build attributes to build determine
  96. // the build target. Target features are also added, but later during
  97. // disassembly.
  98. if (Arch == Triple::arm || Arch == Triple::armeb)
  99. setARMSubArch(TheTriple);
  100. // TheTriple defaults to ELF, and COFF doesn't have an environment:
  101. // something we can do here is indicate that it is mach-o.
  102. if (isMachO()) {
  103. TheTriple.setObjectFormat(Triple::MachO);
  104. } else if (isCOFF()) {
  105. const auto COFFObj = cast<COFFObjectFile>(this);
  106. if (COFFObj->getArch() == Triple::thumb)
  107. TheTriple.setTriple("thumbv7-windows");
  108. } else if (isXCOFF()) {
  109. // XCOFF implies AIX.
  110. TheTriple.setOS(Triple::AIX);
  111. TheTriple.setObjectFormat(Triple::XCOFF);
  112. }
  113. return TheTriple;
  114. }
  115. Expected<std::unique_ptr<ObjectFile>>
  116. ObjectFile::createObjectFile(MemoryBufferRef Object, file_magic Type,
  117. bool InitContent) {
  118. StringRef Data = Object.getBuffer();
  119. if (Type == file_magic::unknown)
  120. Type = identify_magic(Data);
  121. switch (Type) {
  122. case file_magic::unknown:
  123. case file_magic::bitcode:
  124. case file_magic::coff_cl_gl_object:
  125. case file_magic::archive:
  126. case file_magic::macho_universal_binary:
  127. case file_magic::windows_resource:
  128. case file_magic::pdb:
  129. case file_magic::minidump:
  130. case file_magic::goff_object:
  131. case file_magic::cuda_fatbinary:
  132. case file_magic::offload_binary:
  133. case file_magic::dxcontainer_object:
  134. return errorCodeToError(object_error::invalid_file_type);
  135. case file_magic::tapi_file:
  136. return errorCodeToError(object_error::invalid_file_type);
  137. case file_magic::elf:
  138. case file_magic::elf_relocatable:
  139. case file_magic::elf_executable:
  140. case file_magic::elf_shared_object:
  141. case file_magic::elf_core:
  142. return createELFObjectFile(Object, InitContent);
  143. case file_magic::macho_object:
  144. case file_magic::macho_executable:
  145. case file_magic::macho_fixed_virtual_memory_shared_lib:
  146. case file_magic::macho_core:
  147. case file_magic::macho_preload_executable:
  148. case file_magic::macho_dynamically_linked_shared_lib:
  149. case file_magic::macho_dynamic_linker:
  150. case file_magic::macho_bundle:
  151. case file_magic::macho_dynamically_linked_shared_lib_stub:
  152. case file_magic::macho_dsym_companion:
  153. case file_magic::macho_kext_bundle:
  154. case file_magic::macho_file_set:
  155. return createMachOObjectFile(Object);
  156. case file_magic::coff_object:
  157. case file_magic::coff_import_library:
  158. case file_magic::pecoff_executable:
  159. return createCOFFObjectFile(Object);
  160. case file_magic::xcoff_object_32:
  161. return createXCOFFObjectFile(Object, Binary::ID_XCOFF32);
  162. case file_magic::xcoff_object_64:
  163. return createXCOFFObjectFile(Object, Binary::ID_XCOFF64);
  164. case file_magic::wasm_object:
  165. return createWasmObjectFile(Object);
  166. }
  167. llvm_unreachable("Unexpected Object File Type");
  168. }
  169. Expected<OwningBinary<ObjectFile>>
  170. ObjectFile::createObjectFile(StringRef ObjectPath) {
  171. ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
  172. MemoryBuffer::getFile(ObjectPath);
  173. if (std::error_code EC = FileOrErr.getError())
  174. return errorCodeToError(EC);
  175. std::unique_ptr<MemoryBuffer> Buffer = std::move(FileOrErr.get());
  176. Expected<std::unique_ptr<ObjectFile>> ObjOrErr =
  177. createObjectFile(Buffer->getMemBufferRef());
  178. if (Error Err = ObjOrErr.takeError())
  179. return std::move(Err);
  180. std::unique_ptr<ObjectFile> Obj = std::move(ObjOrErr.get());
  181. return OwningBinary<ObjectFile>(std::move(Obj), std::move(Buffer));
  182. }
  183. bool ObjectFile::isReflectionSectionStrippable(
  184. llvm::binaryformat::Swift5ReflectionSectionKind ReflectionSectionKind)
  185. const {
  186. using llvm::binaryformat::Swift5ReflectionSectionKind;
  187. return ReflectionSectionKind == Swift5ReflectionSectionKind::fieldmd ||
  188. ReflectionSectionKind == Swift5ReflectionSectionKind::reflstr ||
  189. ReflectionSectionKind == Swift5ReflectionSectionKind::assocty;
  190. }