xcoff2yaml.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //===------ xcoff2yaml.cpp - XCOFF YAMLIO implementation --------*- C++ -*-===//
  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. #include "obj2yaml.h"
  9. #include "llvm/Object/XCOFFObjectFile.h"
  10. #include "llvm/ObjectYAML/XCOFFYAML.h"
  11. #include "llvm/Support/Errc.h"
  12. #include "llvm/Support/YAMLTraits.h"
  13. using namespace llvm;
  14. using namespace llvm::object;
  15. namespace {
  16. class XCOFFDumper {
  17. const object::XCOFFObjectFile &Obj;
  18. XCOFFYAML::Object YAMLObj;
  19. void dumpHeader();
  20. Error dumpSections();
  21. Error dumpSymbols();
  22. template <typename Shdr, typename Reloc>
  23. Error dumpSections(ArrayRef<Shdr> Sections);
  24. public:
  25. XCOFFDumper(const object::XCOFFObjectFile &obj) : Obj(obj) {}
  26. Error dump();
  27. XCOFFYAML::Object &getYAMLObj() { return YAMLObj; }
  28. };
  29. } // namespace
  30. Error XCOFFDumper::dump() {
  31. dumpHeader();
  32. if (Error E = dumpSections())
  33. return E;
  34. return dumpSymbols();
  35. }
  36. void XCOFFDumper::dumpHeader() {
  37. YAMLObj.Header.Magic = Obj.getMagic();
  38. YAMLObj.Header.NumberOfSections = Obj.getNumberOfSections();
  39. YAMLObj.Header.TimeStamp = Obj.getTimeStamp();
  40. YAMLObj.Header.SymbolTableOffset = Obj.is64Bit()
  41. ? Obj.getSymbolTableOffset64()
  42. : Obj.getSymbolTableOffset32();
  43. YAMLObj.Header.NumberOfSymTableEntries =
  44. Obj.is64Bit() ? Obj.getNumberOfSymbolTableEntries64()
  45. : Obj.getRawNumberOfSymbolTableEntries32();
  46. YAMLObj.Header.AuxHeaderSize = Obj.getOptionalHeaderSize();
  47. YAMLObj.Header.Flags = Obj.getFlags();
  48. }
  49. Error XCOFFDumper::dumpSections() {
  50. if (Obj.is64Bit())
  51. return dumpSections<XCOFFSectionHeader64, XCOFFRelocation64>(
  52. Obj.sections64());
  53. return dumpSections<XCOFFSectionHeader32, XCOFFRelocation32>(
  54. Obj.sections32());
  55. }
  56. template <typename Shdr, typename Reloc>
  57. Error XCOFFDumper::dumpSections(ArrayRef<Shdr> Sections) {
  58. std::vector<XCOFFYAML::Section> &YamlSections = YAMLObj.Sections;
  59. for (const Shdr &S : Sections) {
  60. XCOFFYAML::Section YamlSec;
  61. YamlSec.SectionName = S.getName();
  62. YamlSec.Address = S.PhysicalAddress;
  63. YamlSec.Size = S.SectionSize;
  64. YamlSec.NumberOfRelocations = S.NumberOfRelocations;
  65. YamlSec.NumberOfLineNumbers = S.NumberOfLineNumbers;
  66. YamlSec.FileOffsetToData = S.FileOffsetToRawData;
  67. YamlSec.FileOffsetToRelocations = S.FileOffsetToRelocationInfo;
  68. YamlSec.FileOffsetToLineNumbers = S.FileOffsetToLineNumberInfo;
  69. YamlSec.Flags = S.Flags;
  70. // Dump section data.
  71. if (S.FileOffsetToRawData) {
  72. DataRefImpl SectionDRI;
  73. SectionDRI.p = reinterpret_cast<uintptr_t>(&S);
  74. Expected<ArrayRef<uint8_t>> SecDataRefOrErr =
  75. Obj.getSectionContents(SectionDRI);
  76. if (!SecDataRefOrErr)
  77. return SecDataRefOrErr.takeError();
  78. YamlSec.SectionData = SecDataRefOrErr.get();
  79. }
  80. // Dump relocations.
  81. if (S.NumberOfRelocations) {
  82. auto RelRefOrErr = Obj.relocations<Shdr, Reloc>(S);
  83. if (!RelRefOrErr)
  84. return RelRefOrErr.takeError();
  85. for (const Reloc &R : RelRefOrErr.get()) {
  86. XCOFFYAML::Relocation YamlRel;
  87. YamlRel.Type = R.Type;
  88. YamlRel.Info = R.Info;
  89. YamlRel.SymbolIndex = R.SymbolIndex;
  90. YamlRel.VirtualAddress = R.VirtualAddress;
  91. YamlSec.Relocations.push_back(YamlRel);
  92. }
  93. }
  94. YamlSections.push_back(YamlSec);
  95. }
  96. return Error::success();
  97. }
  98. Error XCOFFDumper::dumpSymbols() {
  99. std::vector<XCOFFYAML::Symbol> &Symbols = YAMLObj.Symbols;
  100. for (const SymbolRef &S : Obj.symbols()) {
  101. DataRefImpl SymbolDRI = S.getRawDataRefImpl();
  102. const XCOFFSymbolRef SymbolEntRef = Obj.toSymbolRef(SymbolDRI);
  103. XCOFFYAML::Symbol Sym;
  104. Expected<StringRef> SymNameRefOrErr = Obj.getSymbolName(SymbolDRI);
  105. if (!SymNameRefOrErr) {
  106. return SymNameRefOrErr.takeError();
  107. }
  108. Sym.SymbolName = SymNameRefOrErr.get();
  109. Sym.Value = SymbolEntRef.getValue();
  110. Expected<StringRef> SectionNameRefOrErr =
  111. Obj.getSymbolSectionName(SymbolEntRef);
  112. if (!SectionNameRefOrErr)
  113. return SectionNameRefOrErr.takeError();
  114. Sym.SectionName = SectionNameRefOrErr.get();
  115. Sym.Type = SymbolEntRef.getSymbolType();
  116. Sym.StorageClass = SymbolEntRef.getStorageClass();
  117. Sym.NumberOfAuxEntries = SymbolEntRef.getNumberOfAuxEntries();
  118. Symbols.push_back(std::move(Sym));
  119. }
  120. return Error::success();
  121. }
  122. Error xcoff2yaml(raw_ostream &Out, const object::XCOFFObjectFile &Obj) {
  123. XCOFFDumper Dumper(Obj);
  124. if (Error E = Dumper.dump())
  125. return E;
  126. yaml::Output Yout(Out);
  127. Yout << Dumper.getYAMLObj();
  128. return Error::success();
  129. }