XCOFFDump.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //===-- XCOFFDump.cpp - XCOFF-specific dumper -----------------------------===//
  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. /// \file
  10. /// This file implements the XCOFF-specific dumper for llvm-objdump.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "XCOFFDump.h"
  14. #include "llvm-objdump.h"
  15. #include "llvm/Demangle/Demangle.h"
  16. using namespace llvm;
  17. using namespace llvm::object;
  18. Error objdump::getXCOFFRelocationValueString(const XCOFFObjectFile &Obj,
  19. const RelocationRef &Rel,
  20. SmallVectorImpl<char> &Result) {
  21. symbol_iterator SymI = Rel.getSymbol();
  22. if (SymI == Obj.symbol_end())
  23. return make_error<GenericBinaryError>(
  24. "invalid symbol reference in relocation entry",
  25. object_error::parse_failed);
  26. Expected<StringRef> SymNameOrErr = SymI->getName();
  27. if (!SymNameOrErr)
  28. return SymNameOrErr.takeError();
  29. std::string SymName = (*SymNameOrErr).str();
  30. if (Demangle)
  31. SymName = demangle(SymName);
  32. if (SymbolDescription)
  33. SymName = getXCOFFSymbolDescription(createSymbolInfo(Obj, *SymI), SymName);
  34. Result.append(SymName.begin(), SymName.end());
  35. return Error::success();
  36. }
  37. std::optional<XCOFF::StorageMappingClass>
  38. objdump::getXCOFFSymbolCsectSMC(const XCOFFObjectFile &Obj,
  39. const SymbolRef &Sym) {
  40. const XCOFFSymbolRef SymRef = Obj.toSymbolRef(Sym.getRawDataRefImpl());
  41. if (!SymRef.isCsectSymbol())
  42. return std::nullopt;
  43. auto CsectAuxEntOrErr = SymRef.getXCOFFCsectAuxRef();
  44. if (!CsectAuxEntOrErr)
  45. return std::nullopt;
  46. return CsectAuxEntOrErr.get().getStorageMappingClass();
  47. }
  48. std::optional<object::SymbolRef>
  49. objdump::getXCOFFSymbolContainingSymbolRef(const XCOFFObjectFile &Obj,
  50. const SymbolRef &Sym) {
  51. const XCOFFSymbolRef SymRef = Obj.toSymbolRef(Sym.getRawDataRefImpl());
  52. if (!SymRef.isCsectSymbol())
  53. return std::nullopt;
  54. Expected<XCOFFCsectAuxRef> CsectAuxEntOrErr = SymRef.getXCOFFCsectAuxRef();
  55. if (!CsectAuxEntOrErr || !CsectAuxEntOrErr.get().isLabel())
  56. return std::nullopt;
  57. uint32_t Idx =
  58. static_cast<uint32_t>(CsectAuxEntOrErr.get().getSectionOrLength());
  59. DataRefImpl DRI;
  60. DRI.p = Obj.getSymbolByIndex(Idx);
  61. return SymbolRef(DRI, &Obj);
  62. }
  63. bool objdump::isLabel(const XCOFFObjectFile &Obj, const SymbolRef &Sym) {
  64. const XCOFFSymbolRef SymRef = Obj.toSymbolRef(Sym.getRawDataRefImpl());
  65. if (!SymRef.isCsectSymbol())
  66. return false;
  67. auto CsectAuxEntOrErr = SymRef.getXCOFFCsectAuxRef();
  68. if (!CsectAuxEntOrErr)
  69. return false;
  70. return CsectAuxEntOrErr.get().isLabel();
  71. }
  72. std::string objdump::getXCOFFSymbolDescription(const SymbolInfoTy &SymbolInfo,
  73. StringRef SymbolName) {
  74. assert(SymbolInfo.isXCOFF() && "Must be a XCOFFSymInfo.");
  75. std::string Result;
  76. // Dummy symbols have no symbol index.
  77. if (SymbolInfo.XCOFFSymInfo.Index)
  78. Result =
  79. ("(idx: " + Twine(*SymbolInfo.XCOFFSymInfo.Index) + ") " + SymbolName)
  80. .str();
  81. else
  82. Result.append(SymbolName.begin(), SymbolName.end());
  83. if (SymbolInfo.XCOFFSymInfo.StorageMappingClass &&
  84. !SymbolInfo.XCOFFSymInfo.IsLabel) {
  85. const XCOFF::StorageMappingClass Smc =
  86. *SymbolInfo.XCOFFSymInfo.StorageMappingClass;
  87. Result.append(("[" + XCOFF::getMappingClassString(Smc) + "]").str());
  88. }
  89. return Result;
  90. }