DWARFCallFrameInfo.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //===-- DWARFCallFrameInfo.h ------------------------------------*- 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. #ifndef LLDB_SYMBOL_DWARFCALLFRAMEINFO_H
  9. #define LLDB_SYMBOL_DWARFCALLFRAMEINFO_H
  10. #include <map>
  11. #include <mutex>
  12. #include "lldb/Core/AddressRange.h"
  13. #include "lldb/Core/dwarf.h"
  14. #include "lldb/Symbol/ObjectFile.h"
  15. #include "lldb/Symbol/UnwindPlan.h"
  16. #include "lldb/Utility/Flags.h"
  17. #include "lldb/Utility/RangeMap.h"
  18. #include "lldb/Utility/VMRange.h"
  19. #include "lldb/lldb-private.h"
  20. namespace lldb_private {
  21. // DWARFCallFrameInfo is a class which can read eh_frame and DWARF Call Frame
  22. // Information FDEs. It stores little information internally. Only two APIs
  23. // are exported - one to find the high/low pc values of a function given a text
  24. // address via the information in the eh_frame / debug_frame, and one to
  25. // generate an UnwindPlan based on the FDE in the eh_frame / debug_frame
  26. // section.
  27. class DWARFCallFrameInfo {
  28. public:
  29. enum Type { EH, DWARF };
  30. DWARFCallFrameInfo(ObjectFile &objfile, lldb::SectionSP &section, Type type);
  31. ~DWARFCallFrameInfo() = default;
  32. // Locate an AddressRange that includes the provided Address in this object's
  33. // eh_frame/debug_info Returns true if a range is found to cover that
  34. // address.
  35. bool GetAddressRange(Address addr, AddressRange &range);
  36. /// Return an UnwindPlan based on the call frame information encoded in the
  37. /// FDE of this DWARFCallFrameInfo section. The returned plan will be valid
  38. /// (at least) for the given address.
  39. bool GetUnwindPlan(const Address &addr, UnwindPlan &unwind_plan);
  40. /// Return an UnwindPlan based on the call frame information encoded in the
  41. /// FDE of this DWARFCallFrameInfo section. The returned plan will be valid
  42. /// (at least) for some address in the given range.
  43. bool GetUnwindPlan(const AddressRange &range, UnwindPlan &unwind_plan);
  44. typedef RangeVector<lldb::addr_t, uint32_t> FunctionAddressAndSizeVector;
  45. // Build a vector of file address and size for all functions in this Module
  46. // based on the eh_frame FDE entries.
  47. //
  48. // The eh_frame information can be a useful source of file address and size
  49. // of the functions in a Module. Often a binary's non-exported symbols are
  50. // stripped before shipping so lldb won't know the start addr / size of many
  51. // functions in the Module. But the eh_frame can help to give the addresses
  52. // of these stripped symbols, at least.
  53. //
  54. // \param[out] function_info
  55. // A vector provided by the caller is filled out. May be empty if no
  56. // FDEs/no eh_frame
  57. // is present in this Module.
  58. void
  59. GetFunctionAddressAndSizeVector(FunctionAddressAndSizeVector &function_info);
  60. void ForEachFDEEntries(
  61. const std::function<bool(lldb::addr_t, uint32_t, dw_offset_t)> &callback);
  62. private:
  63. enum { CFI_AUG_MAX_SIZE = 8, CFI_HEADER_SIZE = 8 };
  64. enum CFIVersion {
  65. CFI_VERSION1 = 1, // DWARF v.2
  66. CFI_VERSION3 = 3, // DWARF v.3
  67. CFI_VERSION4 = 4 // DWARF v.4, v.5
  68. };
  69. struct CIE {
  70. dw_offset_t cie_offset;
  71. uint8_t version;
  72. char augmentation[CFI_AUG_MAX_SIZE]; // This is typically empty or very
  73. // short.
  74. uint8_t address_size = sizeof(uint32_t); // The size of a target address.
  75. uint8_t segment_size = 0; // The size of a segment selector.
  76. uint32_t code_align;
  77. int32_t data_align;
  78. uint32_t return_addr_reg_num;
  79. dw_offset_t inst_offset; // offset of CIE instructions in mCFIData
  80. uint32_t inst_length; // length of CIE instructions in mCFIData
  81. uint8_t ptr_encoding;
  82. uint8_t lsda_addr_encoding; // The encoding of the LSDA address in the FDE
  83. // augmentation data
  84. lldb::addr_t personality_loc; // (file) address of the pointer to the
  85. // personality routine
  86. lldb_private::UnwindPlan::Row initial_row;
  87. CIE(dw_offset_t offset)
  88. : cie_offset(offset), version(-1), code_align(0), data_align(0),
  89. return_addr_reg_num(LLDB_INVALID_REGNUM), inst_offset(0),
  90. inst_length(0), ptr_encoding(0), lsda_addr_encoding(DW_EH_PE_omit),
  91. personality_loc(LLDB_INVALID_ADDRESS), initial_row() {}
  92. };
  93. typedef std::shared_ptr<CIE> CIESP;
  94. typedef std::map<dw_offset_t, CIESP> cie_map_t;
  95. // Start address (file address), size, offset of FDE location used for
  96. // finding an FDE for a given File address; the start address field is an
  97. // offset into an individual Module.
  98. typedef RangeDataVector<lldb::addr_t, uint32_t, dw_offset_t> FDEEntryMap;
  99. bool IsEHFrame() const;
  100. llvm::Optional<FDEEntryMap::Entry>
  101. GetFirstFDEEntryInRange(const AddressRange &range);
  102. void GetFDEIndex();
  103. bool FDEToUnwindPlan(uint32_t offset, Address startaddr,
  104. UnwindPlan &unwind_plan);
  105. const CIE *GetCIE(dw_offset_t cie_offset);
  106. void GetCFIData();
  107. // Applies the specified DWARF opcode to the given row. This function handle
  108. // the commands operates only on a single row (these are the ones what can
  109. // appear both in
  110. // CIE and in FDE).
  111. // Returns true if the opcode is handled and false otherwise.
  112. bool HandleCommonDwarfOpcode(uint8_t primary_opcode, uint8_t extended_opcode,
  113. int32_t data_align, lldb::offset_t &offset,
  114. UnwindPlan::Row &row);
  115. ObjectFile &m_objfile;
  116. lldb::SectionSP m_section_sp;
  117. Flags m_flags = 0;
  118. cie_map_t m_cie_map;
  119. DataExtractor m_cfi_data;
  120. bool m_cfi_data_initialized = false; // only copy the section into the DE once
  121. FDEEntryMap m_fde_index;
  122. bool m_fde_index_initialized = false; // only scan the section for FDEs once
  123. std::mutex m_fde_index_mutex; // and isolate the thread that does it
  124. Type m_type;
  125. CIESP
  126. ParseCIE(const uint32_t cie_offset);
  127. lldb::RegisterKind GetRegisterKind() const {
  128. return m_type == EH ? lldb::eRegisterKindEHFrame : lldb::eRegisterKindDWARF;
  129. }
  130. };
  131. } // namespace lldb_private
  132. #endif // LLDB_SYMBOL_DWARFCALLFRAMEINFO_H