1 /* 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef PANDA_DWARF_BUILDER_H 17 #define PANDA_DWARF_BUILDER_H 18 19 #include "utils/arch.h" 20 #include "utils/span.h" 21 22 #ifdef PANDA_COMPILER_DEBUG_INFO 23 #include <libdwarf/libdwarf.h> 24 #endif 25 26 #include "elfio/elfio.hpp" 27 28 #include <string> 29 #include <unordered_map> 30 31 namespace ark::irtoc { 32 class Function; 33 34 class DwarfBuilder { 35 public: 36 struct Section { 37 std::string name; 38 unsigned type; 39 unsigned flags; 40 unsigned link; 41 unsigned info; 42 std::vector<uint8_t> data; 43 }; 44 45 enum class SectionKind { DEBUG_LINE = 1, REL_DEBUG_LINE }; 46 47 DwarfBuilder(Arch arch, ELFIO::elfio *elf); 48 49 bool BuildGraphNestedFunction(unsigned symbol, const Function *func, Dwarf_Error &error, Dwarf_P_Die &die); 50 51 bool BuildGraph(const Function *func, uint32_t codeOffset, unsigned symbol); 52 53 bool FinalizeNestedFunction(Dwarf_Error &error); 54 55 bool Finalize(uint32_t codeSize); 56 57 Dwarf_Unsigned AddFile(const std::string &fname, Dwarf_Unsigned dirIndex); 58 59 Dwarf_Unsigned AddDir(const std::string &dname); 60 GetArch()61 Arch GetArch() 62 { 63 return arch_; 64 } 65 GetElfBuilder()66 ELFIO::elfio *GetElfBuilder() 67 { 68 return elfBuilder_; 69 } 70 71 // CC-OFFNXT(G.FUN.01-CPP) depend on Dwarf_Callback_Func by DWARF Producer Interface. Should be suppress 72 static int CreateSectionCallback([[maybe_unused]] char *name, [[maybe_unused]] int size, 73 [[maybe_unused]] Dwarf_Unsigned type, [[maybe_unused]] Dwarf_Unsigned flags, 74 [[maybe_unused]] Dwarf_Unsigned link, [[maybe_unused]] Dwarf_Unsigned info, 75 [[maybe_unused]] Dwarf_Unsigned *sectNameIndex, [[maybe_unused]] void *userData, 76 [[maybe_unused]] int *error); 77 78 private: 79 std::vector<ELFIO::section *> sections_; 80 std::unordered_map<std::string, Dwarf_Unsigned> sourceFilesMap_; 81 std::unordered_map<std::string, Dwarf_Unsigned> dirsMap_; 82 std::unordered_map<unsigned, unsigned> indexMap_; 83 std::unordered_map<unsigned, ELFIO::relocation_section_accessor> relMap_; 84 85 ELFIO::elfio *elfBuilder_ {nullptr}; 86 Dwarf_P_Debug dwarf_ {nullptr}; 87 Dwarf_P_Die compileUnitDie_ {nullptr}; 88 Dwarf_P_Die prevProgramDie_ {nullptr}; 89 uint32_t codeSize_ {0}; 90 Arch arch_; 91 }; 92 } // namespace ark::irtoc 93 94 #endif // PANDA_DWARF_BUILDER_H 95