• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 panda::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 BuildGraph(const Function *func, uint32_t codeOffset, unsigned symbol);
50 
51     bool Finalize(uint32_t codeSize);
52 
53     Dwarf_Unsigned AddFile(const std::string &fname, Dwarf_Unsigned dirIndex);
54 
55     Dwarf_Unsigned AddDir(const std::string &dname);
56 
GetArch()57     Arch GetArch()
58     {
59         return arch_;
60     }
61 
GetElfBuilder()62     ELFIO::elfio *GetElfBuilder()
63     {
64         return elfBuilder_;
65     }
66 
67     static int CreateSectionCallback([[maybe_unused]] char *name, [[maybe_unused]] int size,
68                                      [[maybe_unused]] Dwarf_Unsigned type, [[maybe_unused]] Dwarf_Unsigned flags,
69                                      [[maybe_unused]] Dwarf_Unsigned link, [[maybe_unused]] Dwarf_Unsigned info,
70                                      [[maybe_unused]] Dwarf_Unsigned *sectNameIndex, [[maybe_unused]] void *userData,
71                                      [[maybe_unused]] int *error);
72 
73 private:
74     std::vector<ELFIO::section *> sections_;
75     std::unordered_map<std::string, Dwarf_Unsigned> sourceFilesMap_;
76     std::unordered_map<std::string, Dwarf_Unsigned> dirsMap_;
77     std::unordered_map<unsigned, unsigned> indexMap_;
78     std::unordered_map<unsigned, ELFIO::relocation_section_accessor> relMap_;
79 
80     ELFIO::elfio *elfBuilder_ {nullptr};
81     Dwarf_P_Debug dwarf_ {nullptr};
82     Dwarf_P_Die compileUnitDie_ {nullptr};
83     Dwarf_P_Die prevProgramDie_ {nullptr};
84     uint32_t codeSize_ {0};
85     Arch arch_;
86 };
87 }  // namespace panda::irtoc
88 
89 #endif  // PANDA_DWARF_BUILDER_H
90