summaryrefslogtreecommitdiffstats
path: root/lldb/source/Plugins/SymbolFile/DWARF/HashedNameToDIE.h
blob: 81da55a13b2c32c63137f5157f8f97adc8754b9a (plain)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//===-- HashedNameToDIE.h ---------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef SymbolFileDWARF_HashedNameToDIE_h_
#define SymbolFileDWARF_HashedNameToDIE_h_

#include <vector>
#include "lldb/lldb-defines.h"
#include "lldb/Core/dwarf.h"
#include "lldb/Core/RegularExpression.h"

class SymbolFileDWARF;

typedef std::vector<dw_offset_t> DIEArray;

class HashedNameToDIE
{
public:
    enum NameFlags
    {
        eNameFlagIsExternal         = (1u << 0),
        eNameFlagIsClassCXX         = (1u << 1),
        eNameFlagIsClassObjC        = (1u << 2),
        eNameFlagIsClassObjCMaster  = (1u << 3)
    };
    
    enum TypeFlags
    {
        eTypeFlagIsExternal         = (1u << 0)
    };
    
    enum HashFunctionType
    {
        eHashFunctionDJB        = 0u,   // Daniel J Bernstein hash function that is also used by the ELF GNU_HASH sections
    };
    
    static const uint32_t HASH_MAGIC = 0x48415348u;
    
    struct Header
	{
        uint32_t magic;              // 'HASH' magic value to allow endian detection        
        uint16_t version;            // Version number
        uint8_t  addr_bytesize;      // Size in bytes of an address
		uint8_t  hash_function;      // The hash function enumeration that was used
		uint32_t bucket_count;       // The number of buckets in this hash table
		uint32_t hashes_count;       // The total number of unique hash values and hash data offsets in this table
        uint32_t prologue_length;    // The length of the prologue
        
		Header (uint32_t _prologue_length) :
            magic (HASH_MAGIC),
            version (1),
            addr_bytesize (4),
            hash_function (eHashFunctionDJB),
            bucket_count (0),
            hashes_count (0),
            prologue_length (_prologue_length)
		{
		}
        
        virtual
        ~Header ()
        {            
        }
        
        virtual size_t
        GetByteSize() const
        {
            return  sizeof(magic) + 
            sizeof(version) + 
            sizeof(addr_bytesize) + 
            sizeof(hash_function) +
            sizeof(bucket_count) +
            sizeof(hashes_count) +
            sizeof(prologue_length) +
            prologue_length;
        }
        
        virtual void
        Dump (lldb_private::Stream &s);

        virtual uint32_t
        Read (const lldb_private::DataExtractor &data, uint32_t offset);
	};

    struct DWARF
    {
        enum AtomType
        {
            eAtomTypeNULL       = 0u,
            eAtomTypeHashString = 1u,   // String value for hash, use DW_FORM_strp (preferred) or DW_FORM_string
            eAtomTypeHashLength = 2u,   // Length of data for the previous string refered by the last eAtomTypeHashString atom
            eAtomTypeArraySize  = 3u,   // A count that specifies a number of atoms that follow this entry, the next atom defines what the atom type for the array is
            eAtomTypeDIEOffset  = 4u,   // DIE offset, check form for encoding. If DW_FORM_ref1,2,4,8 or DW_FORM_ref_udata, then this value is added to the prologue
            eAtomTypeTag        = 5u,   // DW_TAG_xxx value, should be encoded as DW_FORM_data1 (if no tags exceed 255) or DW_FORM_data2
            eAtomTypeNameFlags  = 6u,   // Flags from enum NameFlags
            eAtomTypeTypeFlags  = 7u,   // Flags from enum TypeFlags
        };
        
        struct Atom
        {
            uint16_t type;
            dw_form_t form;
            
            Atom (uint16_t t = eAtomTypeNULL, dw_form_t f = 0) :
                type (t),
                form (f)
            {
            }
        };
        
        typedef std::vector<Atom> AtomArray;
        
        
        static const char *
        GetAtomTypeName (uint16_t atom)
        {
            switch (atom)
            {
                case eAtomTypeNULL:         return "NULL";
                case eAtomTypeHashString:   return "hash-string";
                case eAtomTypeHashLength:   return "hash-data-length";
                case eAtomTypeArraySize:    return "array-size";
                case eAtomTypeDIEOffset:    return "die-offset";
                case eAtomTypeTag:          return "die-tag";
                case eAtomTypeNameFlags:    return "name-flags";
                case eAtomTypeTypeFlags:    return "type-flags";
            }
            return "<invalid>";
        }
        struct Prologue
        {
            // DIE offset base so die offsets in hash_data can be CU relative
            dw_offset_t die_base_offset;
            AtomArray atoms;
            
            Prologue (dw_offset_t _die_base_offset = 0) :
                die_base_offset (_die_base_offset)
            {
                // Define an array of DIE offsets by first defining an array, 
                // and then define the atom type for the array, in this case
                // we have an array of DIE offsets
                atoms.push_back (Atom(eAtomTypeArraySize, DW_FORM_data4));
                atoms.push_back (Atom(eAtomTypeDIEOffset, DW_FORM_data4));
            }

            virtual
            ~Prologue ()
            {            
            }
            
            
            virtual void
            Clear ()
            {
                die_base_offset = 0;
                atoms.clear();
            }
            
            virtual void
            Dump (lldb_private::Stream &s);        
            
            virtual uint32_t
            Read (const lldb_private::DataExtractor &data, uint32_t offset);
            
            size_t
            GetByteSize () const
            {
                // Add an extra count to the atoms size for the zero termination Atom that gets
                // written to disk
                return sizeof(die_base_offset) + ((atoms.size() + 1) * sizeof(Atom));
            }
        };
        
        struct Header : public HashedNameToDIE::Header
        {
            Header (dw_offset_t _die_base_offset = 0) :
                HashedNameToDIE::Header (sizeof(Prologue)),
                dwarf_prologue (_die_base_offset)
            {
            }

            virtual
            ~Header ()
            {            
            }

            Prologue dwarf_prologue;
            
            virtual void
            Dump (lldb_private::Stream &s);        
            
            virtual uint32_t
            Read (const lldb_private::DataExtractor &data, uint32_t offset);
        };
        
    };
    

    class MemoryTable
    {
    public:
        
        MemoryTable (SymbolFileDWARF *dwarf, 
                     const lldb_private::DataExtractor &data,
                     bool is_apple_names);
        
        ~MemoryTable ()
        {
        }

        bool
        Initialize ();

        bool
        IsValid () const
        {
            return m_header.version > 0;
        }
        
        uint32_t
        GetOffsetOfBucketEntry (uint32_t idx) const
        {
            if (idx < m_header.bucket_count)
                return m_header.GetByteSize() + 4 * idx;
            return UINT32_MAX;
        }
        
        uint32_t
        GetOffsetOfHashValue (uint32_t idx) const
        {
            if (idx < m_header.hashes_count)
                return  m_header.GetByteSize() + 
                4 * m_header.bucket_count + 
                4 * idx;
            return UINT32_MAX;
        }
        
        uint32_t
        GetOffsetOfHashDataOffset (uint32_t idx) const
        {
            if (idx < m_header.hashes_count)
            {
                return  m_header.GetByteSize() +
                4 * m_header.bucket_count +
                4 * m_header.hashes_count +
                4 * idx;
            }
            return UINT32_MAX;
        }
        
        void
        Dump (lldb_private::Stream &s);
        
        size_t
        Find (const char *name, DIEArray &die_ofsets) const;
        
    protected:
        const lldb_private::DataExtractor &m_data;
        const lldb_private::DataExtractor &m_string_table;
        bool m_is_apple_names; // true => .apple_names, false => .apple_types
        DWARF::Header m_header;
    };    
};

#include "lldb/Core/MappedHash.h"

struct DWARFMappedHash
{
    typedef std::vector<uint32_t> DIEArray;
    
    enum AtomType
    {
        eAtomTypeNULL       = 0u,
        eAtomTypeHashString = 1u,   // String value for hash, use DW_FORM_strp (preferred) or DW_FORM_string
        eAtomTypeHashLength = 2u,   // Length of data for the previous string refered by the last eAtomTypeHashString atom
        eAtomTypeArraySize  = 3u,   // A count that specifies a number of atoms that follow this entry, the next atom defines what the atom type for the array is
        eAtomTypeDIEOffset  = 4u,   // DIE offset, check form for encoding. If DW_FORM_ref1,2,4,8 or DW_FORM_ref_udata, then this value is added to the prologue
        eAtomTypeTag        = 5u,   // DW_TAG_xxx value, should be encoded as DW_FORM_data1 (if no tags exceed 255) or DW_FORM_data2
        eAtomTypeNameFlags  = 6u,   // Flags from enum NameFlags
        eAtomTypeTypeFlags  = 7u,   // Flags from enum TypeFlags
    };
    
    struct Atom
    {
        uint16_t type;
        dw_form_t form;
        
        Atom (uint16_t t = eAtomTypeNULL, dw_form_t f = 0) :
        type (t),
        form (f)
        {
        }
    };
    
    typedef std::vector<Atom> AtomArray;
    
    
    static const char *
    GetAtomTypeName (uint16_t atom)
    {
        switch (atom)
        {
            case eAtomTypeNULL:         return "NULL";
            case eAtomTypeHashString:   return "hash-string";
            case eAtomTypeHashLength:   return "hash-data-length";
            case eAtomTypeArraySize:    return "array-size";
            case eAtomTypeDIEOffset:    return "die-offset";
            case eAtomTypeTag:          return "die-tag";
            case eAtomTypeNameFlags:    return "name-flags";
            case eAtomTypeTypeFlags:    return "type-flags";
        }
        return "<invalid>";
    }
    struct Prologue
    {
        // DIE offset base so die offsets in hash_data can be CU relative
        dw_offset_t die_base_offset;
        AtomArray atoms;
        
        Prologue (dw_offset_t _die_base_offset = 0) :
            die_base_offset (_die_base_offset)
        {
            // Define an array of DIE offsets by first defining an array, 
            // and then define the atom type for the array, in this case
            // we have an array of DIE offsets
            atoms.push_back (Atom(eAtomTypeArraySize, DW_FORM_data4));
            atoms.push_back (Atom(eAtomTypeDIEOffset, DW_FORM_data4));
        }
        
        virtual ~Prologue()
        {
        }

        virtual void
        Clear ()
        {
            die_base_offset = 0;
            atoms.clear();
        }
        
//        void
//        Dump (std::ostream* ostrm_ptr);        
        
        uint32_t
        Read (const lldb_private::DataExtractor &data, uint32_t offset)
        {
            die_base_offset = data.GetU32 (&offset);
            Atom atom;
            while (offset != UINT32_MAX)
            {
                atom.type = data.GetU16 (&offset);
                atom.form = data.GetU16 (&offset);
                if (atom.type == eAtomTypeNULL)
                    break;
                atoms.push_back(atom);
            }
            return offset;
        }
        
//        virtual void
//        Write (BinaryStreamBuf &s);
        
        size_t
        GetByteSize () const
        {
            // Add an extra count to the atoms size for the zero termination Atom that gets
            // written to disk
            return sizeof(die_base_offset) + ((atoms.size() + 1) * sizeof(Atom));
        }
    };
    
    struct Header : public MappedHash::Header<Prologue>
    {
        Header (dw_offset_t _die_base_offset = 0)
        {
        }
        
        virtual 
        ~Header()
        {
        }

        virtual size_t
        GetByteSize (const HeaderData &header_data)
        {
            return header_data.GetByteSize();
        }
        
        
        //        virtual void
        //        Dump (std::ostream* ostrm_ptr);        
        //        
        virtual uint32_t
        Read (lldb_private::DataExtractor &data, uint32_t offset)
        {
            offset = MappedHash::Header<Prologue>::Read (data, offset);
            if (offset != UINT32_MAX)
            {
                offset = header_data.Read (data, offset);
            }
            return offset;
        }
        //        
        //        virtual void
        //        Write (BinaryStreamBuf &s);
    };
    
//    class ExportTable
//    {
//    public:
//        ExportTable ();
//        
//        void
//        AppendNames (DWARFDebugPubnamesSet &pubnames_set,
//                     StringTable &string_table);
//        
//        void
//        AppendNamesEntry (SymbolFileDWARF *dwarf2Data,
//                          const DWARFCompileUnit* cu,
//                          const DWARFDebugInfoEntry* die,
//                          StringTable &string_table);
//        
//        void
//        AppendTypesEntry (DWARFData *dwarf2Data,
//                          const DWARFCompileUnit* cu,
//                          const DWARFDebugInfoEntry* die,
//                          StringTable &string_table);
//        
//        size_t
//        Save (BinaryStreamBuf &names_data, const StringTable &string_table);
//        
//        void
//        AppendName (const char *name, 
//                    uint32_t die_offset, 
//                    StringTable &string_table,
//                    dw_offset_t name_debug_str_offset = DW_INVALID_OFFSET); // If "name" has already been looked up, then it can be supplied
//        void
//        AppendType (const char *name, 
//                    uint32_t die_offset, 
//                    StringTable &string_table);
//        
//        
//    protected:
//        struct Entry
//        {
//            uint32_t hash;
//            uint32_t str_offset;
//            uint32_t die_offset;
//        };
//        
//        // Map uniqued .debug_str offset to the corresponding DIE offsets
//        typedef std::map<uint32_t, DIEArray> NameInfo;
//        // Map a name hash to one or more name infos
//        typedef std::map<uint32_t, NameInfo> BucketEntry;
//        
//        static uint32_t
//        GetByteSize (const NameInfo &name_info);
//        
//        typedef std::vector<BucketEntry> BucketEntryColl;
//        typedef std::vector<Entry> EntryColl;
//        EntryColl m_entries;
//        
//    };
    
    
    // A class for reading and using a saved hash table from a block of data
    // in memory
    class MemoryTable : public MappedHash::MemoryTable<uint32_t, DWARFMappedHash::Header, DIEArray>
    {
    public:
        
        MemoryTable (lldb_private::DataExtractor &table_data, 
                     const lldb_private::DataExtractor &string_table,
                     bool is_apple_names) :
            MappedHash::MemoryTable<uint32_t, Header, DIEArray> (table_data),
            m_data (table_data),
            m_string_table (string_table),
            m_is_apple_names (is_apple_names)
        {
        }
    
        virtual 
        ~MemoryTable ()
        {
        }

        virtual const char *
        GetStringForKeyType (KeyType key) const
        {
            // The key in the DWARF table is the .debug_str offset for the string
            return m_string_table.PeekCStr (key);
        }
        
        virtual Result
        GetHashDataForName (const char *name,
                            uint32_t* hash_data_offset_ptr, 
                            Pair &pair) const
        {
            pair.key = m_data.GetU32 (hash_data_offset_ptr);
            // If the key is zero, this terminates our chain of HashData objects
            // for this hash value.
            if (pair.key == 0)
                return eResultEndOfHashData;

            // There definitely should be a string for this string offset, if
            // there isn't, there is something wrong, return and error
            const char *strp_cstr = m_string_table.PeekCStr (pair.key);
            if (strp_cstr == NULL)
                return eResultError;

            const uint32_t count = m_data.GetU32 (hash_data_offset_ptr);
            const uint32_t data_size = count * sizeof(uint32_t);
            if (count > 0 && m_data.ValidOffsetForDataOfSize (*hash_data_offset_ptr, data_size))
            {
                if (strcmp (name, strp_cstr) == 0)
                {
                    pair.value.clear();
                    for (uint32_t i=0; i<count; ++i)
                        pair.value.push_back (m_data.GetU32 (hash_data_offset_ptr));
                    return eResultKeyMatch;
                }
                else
                {
                    // Skip the data so we are ready to parse another HashData
                    // for this hash value
                    *hash_data_offset_ptr += data_size;
                    // The key doesn't match
                    return eResultKeyMismatch;
                }
            }
            else
            {
                *hash_data_offset_ptr = UINT32_MAX;
                return eResultError;
            }
        }

        virtual Result
        AppendHashDataForRegularExpression (const lldb_private::RegularExpression& regex,
                                            uint32_t* hash_data_offset_ptr, 
                                            Pair &pair) const
        {
            pair.key = m_data.GetU32 (hash_data_offset_ptr);
            // If the key is zero, this terminates our chain of HashData objects
            // for this hash value.
            if (pair.key == 0)
                return eResultEndOfHashData;
            
            // There definitely should be a string for this string offset, if
            // there isn't, there is something wrong, return and error
            const char *strp_cstr = m_string_table.PeekCStr (pair.key);
            if (strp_cstr == NULL)
                return eResultError;
            
            const uint32_t count = m_data.GetU32 (hash_data_offset_ptr);
            const uint32_t data_size = count * sizeof(uint32_t);
            if (count > 0 && m_data.ValidOffsetForDataOfSize (*hash_data_offset_ptr, data_size))
            {
                if (regex.Execute(strp_cstr))
                {
                    for (uint32_t i=0; i<count; ++i)
                        pair.value.push_back (m_data.GetU32 (hash_data_offset_ptr));
                    return eResultKeyMatch;
                }
                else
                {
                    // Skip the data so we are ready to parse another HashData
                    // for this hash value
                    *hash_data_offset_ptr += data_size;
                    // The key doesn't match
                    return eResultKeyMismatch;
                }
            }
            else
            {
                *hash_data_offset_ptr = UINT32_MAX;
                return eResultError;
            }
        }

        size_t
        AppendAllDIEsThatMatchingRegex (const lldb_private::RegularExpression& regex, 
                                        DIEArray &die_offsets) const
        {
            const uint32_t hash_count = m_header.hashes_count;
            Pair pair;
            for (uint32_t offset_idx=0; offset_idx<hash_count; ++offset_idx)
            {
                uint32_t hash_data_offset = GetHashDataOffset (offset_idx);
                while (hash_data_offset != UINT32_MAX)
                {
                    const uint32_t prev_hash_data_offset = hash_data_offset;
                    Result hash_result = AppendHashDataForRegularExpression (regex, &hash_data_offset, pair);
                    if (prev_hash_data_offset == hash_data_offset)
                        break;

                    // Check the result of getting our hash data
                    switch (hash_result)
                    {
                        case eResultKeyMatch:
                        case eResultKeyMismatch:
                            // Whether we matches or not, it doesn't matter, we
                            // keep looking.
                            break;
                            
                        case eResultEndOfHashData:
                        case eResultError:
                            hash_data_offset = UINT32_MAX;
                            break;
                    }
                }
            }
            die_offsets.swap (pair.value);
            return die_offsets.size();
        }
        
        size_t
        AppendAllDIEsInRange (const uint32_t die_offset_start, 
                              const uint32_t die_offset_end, 
                              DIEArray &die_offsets) const
        {
            const uint32_t hash_count = m_header.hashes_count;
            for (uint32_t offset_idx=0; offset_idx<hash_count; ++offset_idx)
            {
                bool done = false;
                uint32_t hash_data_offset = GetHashDataOffset (offset_idx);
                while (!done && hash_data_offset != UINT32_MAX)
                {
                    KeyType key = m_data.GetU32 (&hash_data_offset);
                    // If the key is zero, this terminates our chain of HashData objects
                    // for this hash value.
                    if (key == 0)
                        break;
                    
                    const uint32_t count = m_data.GetU32 (&hash_data_offset);
                    for (uint32_t i=0; i<count; ++i)
                    {
                        uint32_t die_offset = m_data.GetU32 (&hash_data_offset);
                        if (die_offset == 0)
                            done = true;
                        if (die_offset_start <= die_offset && die_offset < die_offset_end)
                            die_offsets.push_back(die_offset);
                    }
                }
            }
            return die_offsets.size();
        }

        size_t 
        FindByName (const char *name, DIEArray &die_offsets)
        {
            Pair kv_pair;
            size_t old_size = die_offsets.size();
            if (Find (name, kv_pair))
            {
                die_offsets.swap(kv_pair.value);
                return die_offsets.size() - old_size;
            }
            return 0;
        }
        
    protected:
        const lldb_private::DataExtractor &m_data;
        const lldb_private::DataExtractor &m_string_table;
        bool m_is_apple_names; // true => .apple_names, false => .apple_types
    };
};


#endif  // SymbolFileDWARF_HashedNameToDIE_h_
OpenPOWER on IntegriCloud