File: irepdie.h

package info (click to toggle)
dwarfutils 20201201-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,868 kB
  • sloc: ansic: 104,667; sh: 5,947; cpp: 4,675; python: 878; makefile: 646; awk: 11
file content (354 lines) | stat: -rw-r--r-- 11,805 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
  Copyright (C) 2010-2018 David Anderson.  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
  * Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
  * Neither the name of the example nor the
    names of its contributors may be used to endorse or promote products
    derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY David Anderson ''AS IS'' AND ANY
  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL David Anderson BE LIABLE FOR ANY
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/


//
// irepdie.h
//
//
class IRCUdata;
class IRDie;

class IRAttr {
public:
    IRAttr():attr_(0),finalform_(0),initialform_(0),
        formclass_(DW_FORM_CLASS_UNKNOWN),formdata_(0) {
        };
    IRAttr(Dwarf_Half attr,Dwarf_Half finalform, Dwarf_Half initialform):
        attr_(attr),finalform_(finalform),initialform_(initialform),
        formclass_(DW_FORM_CLASS_UNKNOWN),formdata_(0) {
    };
    IRAttr(const IRAttr &r) { // copy constructor
        attr_ = r.attr_;
        finalform_ = r.finalform_;
        initialform_ = r.initialform_;
        formclass_ = r.formclass_;
        if(r.formdata_) {
            formdata_ =  r.formdata_->clone();
        } else {
            formdata_ = 0;
        }
    };
    ~IRAttr() {
        delete formdata_; };
    IRAttr & operator=( const IRAttr &r) {
        if(this == &r) {
            return *this;
        }
        attr_ = r.attr_;
        finalform_ = r.finalform_;
        initialform_ = r.initialform_;
        formclass_ = r.formclass_;
        if(formdata_) {
            delete formdata_;
            formdata_ =  r.formdata_->clone();
        } else {
            formdata_ = r.formdata_->clone();
        }
        return *this;
    }
    void setBaseData(Dwarf_Half attr, Dwarf_Half finalform,
        Dwarf_Half initialform){
        attr_ = attr;
        finalform_ = finalform;
        initialform_ = initialform;
    };
    void setFormClass(enum Dwarf_Form_Class cl) {
        formclass_ = cl;
    };
    enum Dwarf_Form_Class getFormClass() const {return formclass_; };
    void dropFormData() {
        if(formdata_)
        {delete formdata_; formdata_ = 0; };
        }
    void setFormData(IRForm *f) {
        if (formdata_) {
            delete formdata_;
            formdata_ = 0;
        }
        formdata_ = f;
    };
    Dwarf_Half getFinalForm() const { return initialform_; };
    Dwarf_Half getDirectForm() const { return finalform_; };
    Dwarf_Half getAttrNum() const { return attr_; };
    IRForm * getFormData() { return formdata_;};
private:
    Dwarf_Half attr_;
    Dwarf_Half finalform_;
    // In most cases finalform == initialform form.
    // Otherwise, initialform == DW_FORM_indirect.
    Dwarf_Half initialform_;
    enum Dwarf_Form_Class formclass_;
    IRForm *formdata_;
};

class IRDie {
public:
    IRDie():tag_(0),globalOffset_(0), cuRelativeOffset_(0),
        generatedDie_(0) {};
    ~IRDie() {};
    void addChild(const IRDie & newdie ) {
        children_.push_back(newdie);
    };
    std::string  getName() {
        std::list<IRAttr>::iterator it = attrs_.begin();
        for( ; it != attrs_.end() ; ++it) {
            if (it->getAttrNum() == DW_AT_name) {
                IRForm *f = it->getFormData();
                const IRFormString * isv =
                    dynamic_cast<const IRFormString *>(f);
                if(isv) {
                    return isv->getString();
                }
            }
        }
        return "";
    };
    std::list<IRAttr> & getAttributes() {return attrs_; };
    std::list<IRDie> & getChildren() {return children_; };
    bool hasNewestChild(IRDie **lastch) { size_t N = children_.size();
        if(N < 1) {
            return false;
        }
        *lastch = &children_.back();
        return true;
    };
    // lastChild and lastAttr will throw if no entry exists.
    IRDie &lastChild() { return children_.back(); };
    IRAttr &lastAttr() { return attrs_.back(); };
    void setBaseData(Dwarf_Half tag,Dwarf_Unsigned goff,
        Dwarf_Unsigned cuoff) {
        tag_ = tag;
        globalOffset_=goff;
        cuRelativeOffset_ = cuoff;
    };
    Dwarf_Unsigned getGlobalOffset() const { return globalOffset_;};
    Dwarf_Unsigned getCURelativeOffset() const { return cuRelativeOffset_;};
    void setGeneratedDie(Dwarf_P_Die p_die) {
        generatedDie_ = p_die;};
    Dwarf_P_Die getGeneratedDie() const { return generatedDie_;};
    unsigned getTag() {return tag_; }

private:
   // We rely on the IRDie container being one which does not
   // invalidate pointers with addition/deletion.
   std::list<IRDie>  children_;

   std::list<IRAttr> attrs_;
   unsigned tag_;
   // The following are data from input.
   Dwarf_Unsigned globalOffset_;
   Dwarf_Unsigned cuRelativeOffset_;

   // the following is generated during output.
   Dwarf_P_Die generatedDie_;
};


struct OffsetFormEntry {
    OffsetFormEntry(): off_(0),form_(0){};
    OffsetFormEntry(Dwarf_Unsigned o,
        IRFormReference* f): off_(o),form_(f){};
    ~OffsetFormEntry(){};

    Dwarf_Unsigned  off_;
    IRFormReference *form_;
};
struct ClassReferenceFixupData {
    ClassReferenceFixupData():
        dbg_(0),
        attrnum_(0),
        sourcedie_(0),
        target_(0) {}
    ~ClassReferenceFixupData(){};
    ClassReferenceFixupData(
        Dwarf_P_Debug dbg,
        Dwarf_Half attrnum,
        Dwarf_P_Die sourcedie,
        IRDie *d):
        dbg_(dbg),
        attrnum_(attrnum),
        sourcedie_(sourcedie),
        target_(d) {};
    Dwarf_P_Debug dbg_;
    //  The source die and attrnum suffice because the definition of
    //  DWARF guarantees only one attribute of any given attribute number
    //  can exist on a given DIE.
    Dwarf_Half attrnum_;
    Dwarf_P_Die sourcedie_;
    IRDie *target_;
};

class IRCUdata {
public:
    IRCUdata():
        cu_header_length_(0),
        abbrev_offset_(0),
        next_cu_header_offset_(0),
        version_stamp_(0),
        address_size_(0),
        length_size_(0),
        extension_size_(0),
        has_macrodata_(false),
        macrodata_offset_(0),
        has_linedata_(false),
        linedata_offset_(0),
        cudie_offset_(0)
        {};
    IRCUdata(Dwarf_Unsigned len,Dwarf_Half version,
        Dwarf_Unsigned abbrev_offset,
        Dwarf_Half addr_size,
        Dwarf_Half length_size,
        Dwarf_Half extension_size,
        Dwarf_Unsigned next_cu_header UNUSEDARG):
            cu_header_length_(len),
            abbrev_offset_(abbrev_offset),
            next_cu_header_offset_(addr_size),
            version_stamp_(version),
            address_size_(addr_size),
            length_size_(length_size),
            extension_size_(extension_size),
            has_macrodata_(false),
            macrodata_offset_(0),
            has_linedata_(false),
            linedata_offset_(0),
            cudie_offset_(0) {};
    ~IRCUdata() { };
    bool hasMacroData(Dwarf_Unsigned *offset_out,Dwarf_Unsigned *cudie_off) {
        *offset_out = macrodata_offset_;
        *cudie_off = cudie_offset_;
        return has_macrodata_;
    }
    bool hasLineData(Dwarf_Unsigned *offset_out,Dwarf_Unsigned *cudie_off) {
        *offset_out = linedata_offset_;
        *cudie_off = cudie_offset_;
        return has_linedata_;
    }
    void setMacroData(Dwarf_Unsigned offset,Dwarf_Unsigned cudieoff) {
        has_macrodata_ = true;
        macrodata_offset_ = offset;
        cudie_offset_ = cudieoff;
    };
    void setLineData(Dwarf_Unsigned offset,Dwarf_Unsigned cudieoff) {
        has_linedata_ = true;
        linedata_offset_ = offset;
        cudie_offset_ = cudieoff;
    };
    IRDie & baseDie() { return cudie_; };
    Dwarf_Half getVersionStamp() { return version_stamp_; };
    Dwarf_Half getOffsetSize() { return length_size_; };
    Dwarf_Unsigned getCUdieOffset() { return cudie_offset_; };
    IRCULineData & getCULines() { return cu_lines_; };

    void insertLocalDieOffset(Dwarf_Unsigned localoff,IRDie* dieptr) {
        cuOffInLocalToIRDie_[localoff] = dieptr;
    };
    void insertLocalReferenceAttrTargetRef(Dwarf_Unsigned localoff,
        IRFormReference* attrptr) {

        cuOffInLocalToIRFormRef_.push_back(OffsetFormEntry(localoff,
            attrptr));
    };
    IRDie * getLocalDie(Dwarf_Unsigned localoff) {
        std::map<Dwarf_Unsigned,IRDie*>::iterator pos;
        pos = cuOffInLocalToIRDie_.find(localoff);
        if(pos != cuOffInLocalToIRDie_.end()) {
            return pos->second;
        }
        return NULL;
    };
    void insertClassReferenceFixupData(ClassReferenceFixupData &c) {
        classReferenceFixupList_.push_back(c);
    }
    void updateClassReferenceTargets();
    std::string  getCUName() {
        return cudie_.getName();
    };
    // Use  cuOffInLocalToIRDie_ and
    // cuOffInLocalToIRAttr_ to update attr targets.
    void updateReferenceAttrDieTargets() {
        for(std::list<OffsetFormEntry>::iterator it =
            cuOffInLocalToIRFormRef_.begin();
            it != cuOffInLocalToIRFormRef_.end();
            ++it) {
            IRFormReference* r = it->form_;
            IRDie * tdie = getLocalDie(it->off_);
            if(tdie) {
                r->setTargetInDie(tdie);
            } else {
                // Missing die in r
                // Should be impossible.
            }
        }
    }

private:
    Dwarf_Unsigned cu_header_length_;
    Dwarf_Unsigned abbrev_offset_;
    Dwarf_Unsigned next_cu_header_offset_;
    Dwarf_Half  version_stamp_;
    Dwarf_Half address_size_;
    Dwarf_Half length_size_;
    Dwarf_Half extension_size_;
    bool has_macrodata_;
    Dwarf_Unsigned macrodata_offset_;
    bool has_linedata_;
    Dwarf_Unsigned linedata_offset_;
    Dwarf_Unsigned cudie_offset_;
    IRCULineData      cu_lines_;

    // If true, is 32bit dwarf,else 64bit. Gives the size of a reference.
    bool dwarf32bit_;

    IRDie   cudie_;

    // Refers to cu-local offsets in the input CU and which DIE
    // in the input they target for this CU.
    // Used to find the target DIE for the IRAttrs
    // referenced by  cuOffInLocalToIRAttr_
    std::map<Dwarf_Unsigned,IRDie*> cuOffInLocalToIRDie_;

    // Refers to IRAttrs which make a CU local reference
    // meaning CLASS_REFERENCE IRFormReference to a cu-local die
    // Once Input dies read in this and cuOffInLocalToIRDie_
    // are used to update the IRAttr itself.
    std::list<OffsetFormEntry> cuOffInLocalToIRFormRef_;

    // The data needed to get the Dwarf_P_Die  set for
    // some class reference instances.
    std::list<ClassReferenceFixupData> classReferenceFixupList_;
};

class IRDInfo {
public:
   IRDInfo() {};
   ~IRDInfo() {};
   IRCUdata &lastCU() { return cudata_.back(); }
   std::list<IRCUdata>& getCUData() {return cudata_; };
private:
   std::list<IRCUdata>  cudata_;
};