Debian Codegraph @

src/bin/dwarfgen/irepresentation.h

raw
1 /*
2 Copyright (C) 2010-2016 David Anderson. All rights reserved.
3
4 Redistribution and use in source and binary forms, with
5 or without modification, are permitted provided that the
6 following conditions are met:
7
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above
11 copyright notice, this list of conditions and the
12 following disclaimer in the
13 documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of the example nor the
16 names of its contributors may be used to endorse
17 or promote products
18 derived from this software without specific prior
19 written permission.
20
21 THIS SOFTWARE IS PROVIDED BY David Anderson ''AS IS''
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
23 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
25 NO EVENT SHALL David Anderson BE LIABLE FOR ANY DIRECT,
26 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
28 OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
33 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
35 */
36
37 //
38 // irepresentation.h
39 // The internal (to dwarfgen) representation of debug information.
40 // All the various components (info, frame, etc)
41 // will be stored here in an internal-to-dwarfgen form.
42 //
43 //
44 #include "irepform.h"
45 #include "irepline.h"
46 #include "irepdie.h"
47 #include "irepmacro.h"
48 #include "irepframe.h"
49 #include "ireppubnames.h"
50 #include "strtabdata.h"
51
52 // The elf symbols are used to tie relocations to values.
53 // We do relocations ourselves in dwarfgen so the data is not needed
54 // once the dwarf .debug_* sections created in elf.
55 // We don't write the symbols out as an elf section.
56 // The position in the vector of symbols is the 'elf symbol index'
57 // we create.
58 // Symbol 0 is 'no symbol'.
59 // Symbol 1 is .text
60 class ElfSymbol {
61 public:
62 ElfSymbol():symbolValue_(0),
63 nameIndex_(0) {};
64 ElfSymbol(Dwarf_Unsigned val, const std::string&name,
65 strtabdata&stab):
66 symbolValue_(val),name_(name) {
67 nameIndex_ = stab.addString(name);
68 };
69 ~ElfSymbol() {};
70 Dwarf_Unsigned getSymbolValue() const { return symbolValue_;}
71 private:
72 Dwarf_Unsigned symbolValue_;
73 std::string name_;
74 // The offset in the string table.
75 unsigned nameIndex_;
76 };
77
78 // It's very easy to confuse the section number in an elf file
79 // with an array index in dwarfgen.
80 // So this class hold an elf section number
81 // and gives those a recognizable type.
82 // As of 2023 the ElfSectIndex is an index into
83 // <vector>SectionForDwarf dwsectab
84 // whereas the libdwarfp arrays are fixed values
85 // (see NUM_DEBUG_SECTIONS)
86 class ElfSectIndex {
87 public:
88 ElfSectIndex():elfsect_(0) {};
89 ~ElfSectIndex() {};
90 ElfSectIndex(unsigned v):elfsect_(v) {};
91 unsigned getSectIndex() const { return elfsect_; }
92 void setSectIndex(unsigned v) { elfsect_ = v; }
93 private:
94 unsigned elfsect_;
95 };
96
97 // It's very easy to confuse the symbol number in an elf file
98 // with a symbol number in dwarfgen.
99 // So this class hold an elf symbol number number
100 // and gives those a recognizable type.
101 class ElfSymIndex {
102 public:
103 ElfSymIndex():elfsym_(0) {};
104 ~ElfSymIndex() {};
105 ElfSymIndex(unsigned v):elfsym_(v) {};
106 Dwarf_Unsigned getSymIndex() const { return elfsym_; }
107 void setSymIndex(unsigned v) { elfsym_ = v; }
108 private:
109 Dwarf_Unsigned elfsym_;
110 };
111
112 class ElfSymbols {
113 public:
114 ElfSymbols() {
115 // The initial symbol is 'no symbol'.
116 std::string emptyname("");
117 elfSymbols_.push_back(ElfSymbol(0,emptyname,symstrtab_));
118
119 // We arbitrarily make this symbol .text now, though
120 // not needed yet.
121 std::string textname(".text");
122 elfSymbols_.push_back(ElfSymbol(0,textname,symstrtab_));
123 baseTextAddressSymbol_.setSymIndex(elfSymbols_.size()-1);
124 }
125 ~ElfSymbols() {};
126 ElfSymIndex getBaseTextSymbol() const {
127 return baseTextAddressSymbol_;};
128 ElfSymIndex addElfSymbol(Dwarf_Unsigned val,
129 const std::string&name) {
130 elfSymbols_.push_back(ElfSymbol(val,name,symstrtab_));
131 ElfSymIndex indx(elfSymbols_.size()-1);
132 return indx;
133
134 };
135 ElfSymbol & getElfSymbol(ElfSymIndex symi) {
136 size_t i = symi.getSymIndex();
137 if (i >= elfSymbols_.size()) {
138 std::cerr << "Error, sym index " << i <<
139 " too big for symtab size " <<
140 elfSymbols_.size() << std::endl;
141 exit(1);
142 }
143 return elfSymbols_[i];
144 }
145 private:
146 std::vector<ElfSymbol> elfSymbols_;
147 strtabdata symstrtab_;
148 ElfSymIndex baseTextAddressSymbol_;
149 };
150
151 class IRepresentation {
152 public:
153 IRepresentation() {};
154 ~IRepresentation(){};
155 IRFrame &framedata() { return framedata_; };
156 IRFrame &ehframedata() { return ehframedata_; };
157 IRMacro &macrodata() { return macrodata_; };
158 IRDInfo &infodata() { return debuginfodata_; };
159 IRPubsData &pubnamedata() {return pubnamedata_;};
160 ElfSymbols &getElfSymbols() { return elfSymbols_;};
161 unsigned getBaseTextSymbol() {
162 return elfSymbols_.getBaseTextSymbol().getSymIndex();};
163 private:
164 // The Elf symbols data to use for relocations
165 ElfSymbols elfSymbols_;
166
167 IRFrame framedata_;
168 IRFrame ehframedata_;
169 IRMacro macrodata_;
170 IRPubsData pubnamedata_;
171
172 // .debug_info and its line data are inside debuginfodata_;
173 IRDInfo debuginfodata_;
174 // .debug_types and its line data are in debugtypesdata_.
175 IRDInfo debugtypesdata_;
176 };