ObjectFile.cpp 6.5 KB

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