XCOFFDump.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. Optional<XCOFF::StorageMappingClass>
  38. objdump::getXCOFFSymbolCsectSMC(const XCOFFObjectFile *Obj,
  39. const SymbolRef &Sym) {
  40. XCOFFSymbolRef SymRef(Sym.getRawDataRefImpl(), Obj);
  41. if (SymRef.hasCsectAuxEnt())
  42. return SymRef.getXCOFFCsectAuxEnt32()->StorageMappingClass;
  43. return None;
  44. }
  45. bool objdump::isLabel(const XCOFFObjectFile *Obj, const SymbolRef &Sym) {
  46. XCOFFSymbolRef SymRef(Sym.getRawDataRefImpl(), Obj);
  47. if (SymRef.hasCsectAuxEnt())
  48. return SymRef.getXCOFFCsectAuxEnt32()->isLabel();
  49. return false;
  50. }
  51. std::string objdump::getXCOFFSymbolDescription(const SymbolInfoTy &SymbolInfo,
  52. StringRef SymbolName) {
  53. assert(SymbolInfo.isXCOFF() && "Must be a XCOFFSymInfo.");
  54. std::string Result;
  55. // Dummy symbols have no symbol index.
  56. if (SymbolInfo.XCOFFSymInfo.Index)
  57. Result = ("(idx: " + Twine(SymbolInfo.XCOFFSymInfo.Index.getValue()) +
  58. ") " + SymbolName)
  59. .str();
  60. else
  61. Result.append(SymbolName.begin(), SymbolName.end());
  62. if (SymbolInfo.XCOFFSymInfo.StorageMappingClass &&
  63. !SymbolInfo.XCOFFSymInfo.IsLabel) {
  64. const XCOFF::StorageMappingClass Smc =
  65. SymbolInfo.XCOFFSymInfo.StorageMappingClass.getValue();
  66. Result.append(("[" + XCOFF::getMappingClassString(Smc) + "]").str());
  67. }
  68. return Result;
  69. }