FunctionInfo.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //===- FunctionInfo.cpp ---------------------------------------------------===//
  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 "llvm/DebugInfo/GSYM/FunctionInfo.h"
  9. #include "llvm/DebugInfo/GSYM/FileWriter.h"
  10. #include "llvm/DebugInfo/GSYM/LineTable.h"
  11. #include "llvm/DebugInfo/GSYM/InlineInfo.h"
  12. #include "llvm/Support/DataExtractor.h"
  13. using namespace llvm;
  14. using namespace gsym;
  15. /// FunctionInfo information type that is used to encode the optional data
  16. /// that is associated with a FunctionInfo object.
  17. enum InfoType : uint32_t {
  18. EndOfList = 0u,
  19. LineTableInfo = 1u,
  20. InlineInfo = 2u
  21. };
  22. raw_ostream &llvm::gsym::operator<<(raw_ostream &OS, const FunctionInfo &FI) {
  23. OS << '[' << HEX64(FI.Range.Start) << '-' << HEX64(FI.Range.End) << "): "
  24. << "Name=" << HEX32(FI.Name) << '\n' << FI.OptLineTable << FI.Inline;
  25. return OS;
  26. }
  27. llvm::Expected<FunctionInfo> FunctionInfo::decode(DataExtractor &Data,
  28. uint64_t BaseAddr) {
  29. FunctionInfo FI;
  30. FI.Range.Start = BaseAddr;
  31. uint64_t Offset = 0;
  32. if (!Data.isValidOffsetForDataOfSize(Offset, 4))
  33. return createStringError(std::errc::io_error,
  34. "0x%8.8" PRIx64 ": missing FunctionInfo Size", Offset);
  35. FI.Range.End = FI.Range.Start + Data.getU32(&Offset);
  36. if (!Data.isValidOffsetForDataOfSize(Offset, 4))
  37. return createStringError(std::errc::io_error,
  38. "0x%8.8" PRIx64 ": missing FunctionInfo Name", Offset);
  39. FI.Name = Data.getU32(&Offset);
  40. if (FI.Name == 0)
  41. return createStringError(std::errc::io_error,
  42. "0x%8.8" PRIx64 ": invalid FunctionInfo Name value 0x%8.8x",
  43. Offset - 4, FI.Name);
  44. bool Done = false;
  45. while (!Done) {
  46. if (!Data.isValidOffsetForDataOfSize(Offset, 4))
  47. return createStringError(std::errc::io_error,
  48. "0x%8.8" PRIx64 ": missing FunctionInfo InfoType value", Offset);
  49. const uint32_t IT = Data.getU32(&Offset);
  50. if (!Data.isValidOffsetForDataOfSize(Offset, 4))
  51. return createStringError(std::errc::io_error,
  52. "0x%8.8" PRIx64 ": missing FunctionInfo InfoType length", Offset);
  53. const uint32_t InfoLength = Data.getU32(&Offset);
  54. if (!Data.isValidOffsetForDataOfSize(Offset, InfoLength))
  55. return createStringError(std::errc::io_error,
  56. "0x%8.8" PRIx64 ": missing FunctionInfo data for InfoType %u",
  57. Offset, IT);
  58. DataExtractor InfoData(Data.getData().substr(Offset, InfoLength),
  59. Data.isLittleEndian(),
  60. Data.getAddressSize());
  61. switch (IT) {
  62. case InfoType::EndOfList:
  63. Done = true;
  64. break;
  65. case InfoType::LineTableInfo:
  66. if (Expected<LineTable> LT = LineTable::decode(InfoData, BaseAddr))
  67. FI.OptLineTable = std::move(LT.get());
  68. else
  69. return LT.takeError();
  70. break;
  71. case InfoType::InlineInfo:
  72. if (Expected<InlineInfo> II = InlineInfo::decode(InfoData, BaseAddr))
  73. FI.Inline = std::move(II.get());
  74. else
  75. return II.takeError();
  76. break;
  77. default:
  78. return createStringError(std::errc::io_error,
  79. "0x%8.8" PRIx64 ": unsupported InfoType %u",
  80. Offset-8, IT);
  81. }
  82. Offset += InfoLength;
  83. }
  84. return std::move(FI);
  85. }
  86. llvm::Expected<uint64_t> FunctionInfo::encode(FileWriter &O) const {
  87. if (!isValid())
  88. return createStringError(std::errc::invalid_argument,
  89. "attempted to encode invalid FunctionInfo object");
  90. // Align FunctionInfo data to a 4 byte alignment.
  91. O.alignTo(4);
  92. const uint64_t FuncInfoOffset = O.tell();
  93. // Write the size in bytes of this function as a uint32_t. This can be zero
  94. // if we just have a symbol from a symbol table and that symbol has no size.
  95. O.writeU32(size());
  96. // Write the name of this function as a uint32_t string table offset.
  97. O.writeU32(Name);
  98. if (OptLineTable.hasValue()) {
  99. O.writeU32(InfoType::LineTableInfo);
  100. // Write a uint32_t length as zero for now, we will fix this up after
  101. // writing the LineTable out with the number of bytes that were written.
  102. O.writeU32(0);
  103. const auto StartOffset = O.tell();
  104. llvm::Error err = OptLineTable->encode(O, Range.Start);
  105. if (err)
  106. return std::move(err);
  107. const off_t Length = O.tell() - StartOffset;
  108. if (Length > UINT32_MAX)
  109. return createStringError(std::errc::invalid_argument,
  110. "LineTable length is greater than UINT32_MAX");
  111. // Fixup the size of the LineTable data with the correct size.
  112. O.fixup32(static_cast<uint32_t>(Length), StartOffset - 4);
  113. }
  114. // Write out the inline function info if we have any and if it is valid.
  115. if (Inline.hasValue()) {
  116. O.writeU32(InfoType::InlineInfo);
  117. // Write a uint32_t length as zero for now, we will fix this up after
  118. // writing the LineTable out with the number of bytes that were written.
  119. O.writeU32(0);
  120. const auto StartOffset = O.tell();
  121. llvm::Error err = Inline->encode(O, Range.Start);
  122. if (err)
  123. return std::move(err);
  124. const off_t Length = O.tell() - StartOffset;
  125. if (Length > UINT32_MAX)
  126. return createStringError(std::errc::invalid_argument,
  127. "InlineInfo length is greater than UINT32_MAX");
  128. // Fixup the size of the InlineInfo data with the correct size.
  129. O.fixup32(static_cast<uint32_t>(Length), StartOffset - 4);
  130. }
  131. // Terminate the data chunks with and end of list with zero size
  132. O.writeU32(InfoType::EndOfList);
  133. O.writeU32(0);
  134. return FuncInfoOffset;
  135. }