xcoff2yaml.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/YAMLTraits.h"
  12. using namespace llvm;
  13. using namespace llvm::object;
  14. namespace {
  15. class XCOFFDumper {
  16. const object::XCOFFObjectFile &Obj;
  17. XCOFFYAML::Object YAMLObj;
  18. void dumpHeader();
  19. std::error_code dumpSymbols();
  20. public:
  21. XCOFFDumper(const object::XCOFFObjectFile &obj) : Obj(obj) {}
  22. std::error_code dump();
  23. XCOFFYAML::Object &getYAMLObj() { return YAMLObj; }
  24. };
  25. } // namespace
  26. std::error_code XCOFFDumper::dump() {
  27. dumpHeader();
  28. return dumpSymbols();
  29. }
  30. void XCOFFDumper::dumpHeader() {
  31. YAMLObj.Header.Magic = Obj.getMagic();
  32. YAMLObj.Header.NumberOfSections = Obj.getNumberOfSections();
  33. YAMLObj.Header.TimeStamp = Obj.getTimeStamp();
  34. // TODO FIXME only dump 32 bit header for now.
  35. if (Obj.is64Bit())
  36. report_fatal_error("64-bit XCOFF files not supported yet.");
  37. YAMLObj.Header.SymbolTableOffset = Obj.getSymbolTableOffset32();
  38. YAMLObj.Header.NumberOfSymTableEntries =
  39. Obj.getRawNumberOfSymbolTableEntries32();
  40. YAMLObj.Header.AuxHeaderSize = Obj.getOptionalHeaderSize();
  41. YAMLObj.Header.Flags = Obj.getFlags();
  42. }
  43. std::error_code XCOFFDumper::dumpSymbols() {
  44. std::vector<XCOFFYAML::Symbol> &Symbols = YAMLObj.Symbols;
  45. for (const SymbolRef &S : Obj.symbols()) {
  46. DataRefImpl SymbolDRI = S.getRawDataRefImpl();
  47. const XCOFFSymbolEntry *SymbolEntPtr = Obj.toSymbolEntry(SymbolDRI);
  48. XCOFFYAML::Symbol Sym;
  49. Expected<StringRef> SymNameRefOrErr = Obj.getSymbolName(SymbolDRI);
  50. if (!SymNameRefOrErr) {
  51. return errorToErrorCode(SymNameRefOrErr.takeError());
  52. }
  53. Sym.SymbolName = SymNameRefOrErr.get();
  54. Sym.Value = SymbolEntPtr->Value;
  55. Expected<StringRef> SectionNameRefOrErr =
  56. Obj.getSymbolSectionName(SymbolEntPtr);
  57. if (!SectionNameRefOrErr)
  58. return errorToErrorCode(SectionNameRefOrErr.takeError());
  59. Sym.SectionName = SectionNameRefOrErr.get();
  60. Sym.Type = SymbolEntPtr->SymbolType;
  61. Sym.StorageClass = SymbolEntPtr->StorageClass;
  62. Sym.NumberOfAuxEntries = SymbolEntPtr->NumberOfAuxEntries;
  63. Symbols.push_back(Sym);
  64. }
  65. return std::error_code();
  66. }
  67. std::error_code xcoff2yaml(raw_ostream &Out,
  68. const object::XCOFFObjectFile &Obj) {
  69. XCOFFDumper Dumper(Obj);
  70. if (std::error_code EC = Dumper.dump())
  71. return EC;
  72. yaml::Output Yout(Out);
  73. Yout << Dumper.getYAMLObj();
  74. return std::error_code();
  75. }