OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [gold/] [dwarf_reader.cc] - Blame information for rev 167

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 khays
// dwarf_reader.cc -- parse dwarf2/3 debug information
2
 
3 166 khays
// Copyright 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 27 khays
// Written by Ian Lance Taylor <iant@google.com>.
5
 
6
// This file is part of gold.
7
 
8
// This program is free software; you can redistribute it and/or modify
9
// it under the terms of the GNU General Public License as published by
10
// the Free Software Foundation; either version 3 of the License, or
11
// (at your option) any later version.
12
 
13
// This program is distributed in the hope that it will be useful,
14
// but WITHOUT ANY WARRANTY; without even the implied warranty of
15
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
// GNU General Public License for more details.
17
 
18
// You should have received a copy of the GNU General Public License
19
// along with this program; if not, write to the Free Software
20
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21
// MA 02110-1301, USA.
22
 
23
#include "gold.h"
24
 
25
#include <algorithm>
26
#include <vector>
27
 
28
#include "elfcpp_swap.h"
29
#include "dwarf.h"
30
#include "object.h"
31
#include "parameters.h"
32
#include "reloc.h"
33
#include "dwarf_reader.h"
34
#include "int_encoding.h"
35
#include "compressed_output.h"
36
 
37
namespace gold {
38
 
39
struct LineStateMachine
40
{
41
  int file_num;
42
  uint64_t address;
43
  int line_num;
44
  int column_num;
45
  unsigned int shndx;    // the section address refers to
46
  bool is_stmt;          // stmt means statement.
47
  bool basic_block;
48
  bool end_sequence;
49
};
50
 
51
static void
52
ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt)
53
{
54
  lsm->file_num = 1;
55
  lsm->address = 0;
56
  lsm->line_num = 1;
57
  lsm->column_num = 0;
58
  lsm->shndx = -1U;
59
  lsm->is_stmt = default_is_stmt;
60
  lsm->basic_block = false;
61
  lsm->end_sequence = false;
62
}
63
 
64
template<int size, bool big_endian>
65 166 khays
Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(
66
    Object* object,
67
    unsigned int read_shndx)
68
  : data_valid_(false), buffer_(NULL), buffer_start_(NULL),
69
    symtab_buffer_(NULL), directories_(), files_(), current_header_index_(-1)
70 27 khays
{
71
  unsigned int debug_shndx;
72 166 khays
 
73 27 khays
  for (debug_shndx = 1; debug_shndx < object->shnum(); ++debug_shndx)
74
    {
75
      // FIXME: do this more efficiently: section_name() isn't super-fast
76
      std::string name = object->section_name(debug_shndx);
77
      if (name == ".debug_line" || name == ".zdebug_line")
78
        {
79
          section_size_type buffer_size;
80 166 khays
          bool is_new = false;
81
          this->buffer_ = object->decompressed_section_contents(debug_shndx,
82
                                                                &buffer_size,
83
                                                                &is_new);
84
          if (is_new)
85
            this->buffer_start_ = this->buffer_;
86 27 khays
          this->buffer_end_ = this->buffer_ + buffer_size;
87
          break;
88
        }
89
    }
90
  if (this->buffer_ == NULL)
91
    return;
92
 
93
  // Find the relocation section for ".debug_line".
94
  // We expect these for relobjs (.o's) but not dynobjs (.so's).
95
  bool got_relocs = false;
96
  for (unsigned int reloc_shndx = 0;
97
       reloc_shndx < object->shnum();
98
       ++reloc_shndx)
99
    {
100
      unsigned int reloc_sh_type = object->section_type(reloc_shndx);
101
      if ((reloc_sh_type == elfcpp::SHT_REL
102
           || reloc_sh_type == elfcpp::SHT_RELA)
103
          && object->section_info(reloc_shndx) == debug_shndx)
104
        {
105
          got_relocs = this->track_relocs_.initialize(object, reloc_shndx,
106
                                                      reloc_sh_type);
107
          this->track_relocs_type_ = reloc_sh_type;
108
          break;
109
        }
110
    }
111
 
112
  // Finally, we need the symtab section to interpret the relocs.
113
  if (got_relocs)
114
    {
115
      unsigned int symtab_shndx;
116
      for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx)
117
        if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB)
118
          {
119
            this->symtab_buffer_ = object->section_contents(
120
                symtab_shndx, &this->symtab_buffer_size_, false);
121
            break;
122
          }
123
      if (this->symtab_buffer_ == NULL)
124
        return;
125
    }
126
 
127
  // Now that we have successfully read all the data, parse the debug
128
  // info.
129
  this->data_valid_ = true;
130
  this->read_line_mappings(object, read_shndx);
131
}
132
 
133
// Read the DWARF header.
134
 
135
template<int size, bool big_endian>
136
const unsigned char*
137
Sized_dwarf_line_info<size, big_endian>::read_header_prolog(
138
    const unsigned char* lineptr)
139
{
140
  uint32_t initial_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
141
  lineptr += 4;
142
 
143
  // In DWARF2/3, if the initial length is all 1 bits, then the offset
144
  // size is 8 and we need to read the next 8 bytes for the real length.
145
  if (initial_length == 0xffffffff)
146
    {
147
      header_.offset_size = 8;
148
      initial_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
149
      lineptr += 8;
150
    }
151
  else
152
    header_.offset_size = 4;
153
 
154
  header_.total_length = initial_length;
155
 
156
  gold_assert(lineptr + header_.total_length <= buffer_end_);
157
 
158
  header_.version = elfcpp::Swap_unaligned<16, big_endian>::readval(lineptr);
159
  lineptr += 2;
160
 
161
  if (header_.offset_size == 4)
162
    header_.prologue_length = elfcpp::Swap_unaligned<32, big_endian>::readval(lineptr);
163
  else
164
    header_.prologue_length = elfcpp::Swap_unaligned<64, big_endian>::readval(lineptr);
165
  lineptr += header_.offset_size;
166
 
167
  header_.min_insn_length = *lineptr;
168
  lineptr += 1;
169
 
170
  header_.default_is_stmt = *lineptr;
171
  lineptr += 1;
172
 
173
  header_.line_base = *reinterpret_cast<const signed char*>(lineptr);
174
  lineptr += 1;
175
 
176
  header_.line_range = *lineptr;
177
  lineptr += 1;
178
 
179
  header_.opcode_base = *lineptr;
180
  lineptr += 1;
181
 
182
  header_.std_opcode_lengths.resize(header_.opcode_base + 1);
183
  header_.std_opcode_lengths[0] = 0;
184
  for (int i = 1; i < header_.opcode_base; i++)
185
    {
186
      header_.std_opcode_lengths[i] = *lineptr;
187
      lineptr += 1;
188
    }
189
 
190
  return lineptr;
191
}
192
 
193
// The header for a debug_line section is mildly complicated, because
194
// the line info is very tightly encoded.
195
 
196
template<int size, bool big_endian>
197
const unsigned char*
198
Sized_dwarf_line_info<size, big_endian>::read_header_tables(
199
    const unsigned char* lineptr)
200
{
201
  ++this->current_header_index_;
202
 
203
  // Create a new directories_ entry and a new files_ entry for our new
204
  // header.  We initialize each with a single empty element, because
205
  // dwarf indexes directory and filenames starting at 1.
206
  gold_assert(static_cast<int>(this->directories_.size())
207
              == this->current_header_index_);
208
  gold_assert(static_cast<int>(this->files_.size())
209
              == this->current_header_index_);
210
  this->directories_.push_back(std::vector<std::string>(1));
211
  this->files_.push_back(std::vector<std::pair<int, std::string> >(1));
212
 
213
  // It is legal for the directory entry table to be empty.
214
  if (*lineptr)
215
    {
216
      int dirindex = 1;
217
      while (*lineptr)
218
        {
219
          const char* dirname = reinterpret_cast<const char*>(lineptr);
220
          gold_assert(dirindex
221
                      == static_cast<int>(this->directories_.back().size()));
222
          this->directories_.back().push_back(dirname);
223
          lineptr += this->directories_.back().back().size() + 1;
224
          dirindex++;
225
        }
226
    }
227
  lineptr++;
228
 
229
  // It is also legal for the file entry table to be empty.
230
  if (*lineptr)
231
    {
232
      int fileindex = 1;
233
      size_t len;
234
      while (*lineptr)
235
        {
236
          const char* filename = reinterpret_cast<const char*>(lineptr);
237
          lineptr += strlen(filename) + 1;
238
 
239
          uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
240
          lineptr += len;
241
 
242
          if (dirindex >= this->directories_.back().size())
243
            dirindex = 0;
244
          int dirindexi = static_cast<int>(dirindex);
245
 
246
          read_unsigned_LEB_128(lineptr, &len);   // mod_time
247
          lineptr += len;
248
 
249
          read_unsigned_LEB_128(lineptr, &len);   // filelength
250
          lineptr += len;
251
 
252
          gold_assert(fileindex
253
                      == static_cast<int>(this->files_.back().size()));
254
          this->files_.back().push_back(std::make_pair(dirindexi, filename));
255
          fileindex++;
256
        }
257
    }
258
  lineptr++;
259
 
260
  return lineptr;
261
}
262
 
263
// Process a single opcode in the .debug.line structure.
264
 
265
template<int size, bool big_endian>
266
bool
267
Sized_dwarf_line_info<size, big_endian>::process_one_opcode(
268
    const unsigned char* start, struct LineStateMachine* lsm, size_t* len)
269
{
270
  size_t oplen = 0;
271
  size_t templen;
272
  unsigned char opcode = *start;
273
  oplen++;
274
  start++;
275
 
276
  // If the opcode is great than the opcode_base, it is a special
277
  // opcode. Most line programs consist mainly of special opcodes.
278
  if (opcode >= header_.opcode_base)
279
    {
280
      opcode -= header_.opcode_base;
281
      const int advance_address = ((opcode / header_.line_range)
282
                                   * header_.min_insn_length);
283
      lsm->address += advance_address;
284
 
285
      const int advance_line = ((opcode % header_.line_range)
286
                                + header_.line_base);
287
      lsm->line_num += advance_line;
288
      lsm->basic_block = true;
289
      *len = oplen;
290
      return true;
291
    }
292
 
293
  // Otherwise, we have the regular opcodes
294
  switch (opcode)
295
    {
296
    case elfcpp::DW_LNS_copy:
297
      lsm->basic_block = false;
298
      *len = oplen;
299
      return true;
300
 
301
    case elfcpp::DW_LNS_advance_pc:
302
      {
303
        const uint64_t advance_address
304
            = read_unsigned_LEB_128(start, &templen);
305
        oplen += templen;
306
        lsm->address += header_.min_insn_length * advance_address;
307
      }
308
      break;
309
 
310
    case elfcpp::DW_LNS_advance_line:
311
      {
312
        const uint64_t advance_line = read_signed_LEB_128(start, &templen);
313
        oplen += templen;
314
        lsm->line_num += advance_line;
315
      }
316
      break;
317
 
318
    case elfcpp::DW_LNS_set_file:
319
      {
320
        const uint64_t fileno = read_unsigned_LEB_128(start, &templen);
321
        oplen += templen;
322
        lsm->file_num = fileno;
323
      }
324
      break;
325
 
326
    case elfcpp::DW_LNS_set_column:
327
      {
328
        const uint64_t colno = read_unsigned_LEB_128(start, &templen);
329
        oplen += templen;
330
        lsm->column_num = colno;
331
      }
332
      break;
333
 
334
    case elfcpp::DW_LNS_negate_stmt:
335
      lsm->is_stmt = !lsm->is_stmt;
336
      break;
337
 
338
    case elfcpp::DW_LNS_set_basic_block:
339
      lsm->basic_block = true;
340
      break;
341
 
342
    case elfcpp::DW_LNS_fixed_advance_pc:
343
      {
344
        int advance_address;
345
        advance_address = elfcpp::Swap_unaligned<16, big_endian>::readval(start);
346
        oplen += 2;
347
        lsm->address += advance_address;
348
      }
349
      break;
350
 
351
    case elfcpp::DW_LNS_const_add_pc:
352
      {
353
        const int advance_address = (header_.min_insn_length
354
                                     * ((255 - header_.opcode_base)
355
                                        / header_.line_range));
356
        lsm->address += advance_address;
357
      }
358
      break;
359
 
360
    case elfcpp::DW_LNS_extended_op:
361
      {
362
        const uint64_t extended_op_len
363
            = read_unsigned_LEB_128(start, &templen);
364
        start += templen;
365
        oplen += templen + extended_op_len;
366
 
367
        const unsigned char extended_op = *start;
368
        start++;
369
 
370
        switch (extended_op)
371
          {
372
          case elfcpp::DW_LNE_end_sequence:
373
            // This means that the current byte is the one immediately
374
            // after a set of instructions.  Record the current line
375
            // for up to one less than the current address.
376
            lsm->line_num = -1;
377
            lsm->end_sequence = true;
378
            *len = oplen;
379
            return true;
380
 
381
          case elfcpp::DW_LNE_set_address:
382
            {
383
              lsm->address =
384
                elfcpp::Swap_unaligned<size, big_endian>::readval(start);
385
              typename Reloc_map::const_iterator it
386
                  = this->reloc_map_.find(start - this->buffer_);
387
              if (it != reloc_map_.end())
388
                {
389
                  // If this is a SHT_RELA section, then ignore the
390
                  // section contents.  This assumes that this is a
391
                  // straight reloc which just uses the reloc addend.
392
                  // The reloc addend has already been included in the
393
                  // symbol value.
394
                  if (this->track_relocs_type_ == elfcpp::SHT_RELA)
395
                    lsm->address = 0;
396
                  // Add in the symbol value.
397
                  lsm->address += it->second.second;
398
                  lsm->shndx = it->second.first;
399
                }
400
              else
401
                {
402
                  // If we're a normal .o file, with relocs, every
403
                  // set_address should have an associated relocation.
404
                  if (this->input_is_relobj())
405
                    this->data_valid_ = false;
406
                }
407
              break;
408
            }
409
          case elfcpp::DW_LNE_define_file:
410
            {
411
              const char* filename  = reinterpret_cast<const char*>(start);
412
              templen = strlen(filename) + 1;
413
              start += templen;
414
 
415
              uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
416
              oplen += templen;
417
 
418
              if (dirindex >= this->directories_.back().size())
419
                dirindex = 0;
420
              int dirindexi = static_cast<int>(dirindex);
421
 
422
              read_unsigned_LEB_128(start, &templen);   // mod_time
423
              oplen += templen;
424
 
425
              read_unsigned_LEB_128(start, &templen);   // filelength
426
              oplen += templen;
427
 
428
              this->files_.back().push_back(std::make_pair(dirindexi,
429
                                                           filename));
430
            }
431
            break;
432
          }
433
      }
434
      break;
435
 
436
    default:
437
      {
438
        // Ignore unknown opcode  silently
439
        for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++)
440
          {
441
            size_t templen;
442
            read_unsigned_LEB_128(start, &templen);
443
            start += templen;
444
            oplen += templen;
445
          }
446
      }
447
      break;
448
  }
449
  *len = oplen;
450
  return false;
451
}
452
 
453
// Read the debug information at LINEPTR and store it in the line
454
// number map.
455
 
456
template<int size, bool big_endian>
457
unsigned const char*
458
Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr,
459
                                                    unsigned int shndx)
460
{
461
  struct LineStateMachine lsm;
462
 
463
  // LENGTHSTART is the place the length field is based on.  It is the
464
  // point in the header after the initial length field.
465
  const unsigned char* lengthstart = buffer_;
466
 
467
  // In 64 bit dwarf, the initial length is 12 bytes, because of the
468
  // 0xffffffff at the start.
469
  if (header_.offset_size == 8)
470
    lengthstart += 12;
471
  else
472
    lengthstart += 4;
473
 
474
  while (lineptr < lengthstart + header_.total_length)
475
    {
476
      ResetLineStateMachine(&lsm, header_.default_is_stmt);
477
      while (!lsm.end_sequence)
478
        {
479
          size_t oplength;
480
          bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength);
481
          if (add_line
482
              && (shndx == -1U || lsm.shndx == -1U || shndx == lsm.shndx))
483
            {
484
              Offset_to_lineno_entry entry
485 166 khays
                  = { static_cast<off_t>(lsm.address),
486
                      this->current_header_index_,
487
                      static_cast<unsigned int>(lsm.file_num),
488
                      true, lsm.line_num };
489 27 khays
              std::vector<Offset_to_lineno_entry>&
490
                map(this->line_number_map_[lsm.shndx]);
491
              // If we see two consecutive entries with the same
492
              // offset and a real line number, then mark the first
493
              // one as non-canonical.
494
              if (!map.empty()
495
                  && (map.back().offset == static_cast<off_t>(lsm.address))
496
                  && lsm.line_num != -1
497
                  && map.back().line_num != -1)
498
                map.back().last_line_for_offset = false;
499
              map.push_back(entry);
500
            }
501
          lineptr += oplength;
502
        }
503
    }
504
 
505
  return lengthstart + header_.total_length;
506
}
507
 
508
// Looks in the symtab to see what section a symbol is in.
509
 
510
template<int size, bool big_endian>
511
unsigned int
512
Sized_dwarf_line_info<size, big_endian>::symbol_section(
513
    Object* object,
514
    unsigned int sym,
515
    typename elfcpp::Elf_types<size>::Elf_Addr* value,
516
    bool* is_ordinary)
517
{
518
  const int symsize = elfcpp::Elf_sizes<size>::sym_size;
519
  gold_assert(sym * symsize < this->symtab_buffer_size_);
520
  elfcpp::Sym<size, big_endian> elfsym(this->symtab_buffer_ + sym * symsize);
521
  *value = elfsym.get_st_value();
522
  return object->adjust_sym_shndx(sym, elfsym.get_st_shndx(), is_ordinary);
523
}
524
 
525
// Read the relocations into a Reloc_map.
526
 
527
template<int size, bool big_endian>
528
void
529
Sized_dwarf_line_info<size, big_endian>::read_relocs(Object* object)
530
{
531
  if (this->symtab_buffer_ == NULL)
532
    return;
533
 
534
  typename elfcpp::Elf_types<size>::Elf_Addr value;
535
  off_t reloc_offset;
536
  while ((reloc_offset = this->track_relocs_.next_offset()) != -1)
537
    {
538
      const unsigned int sym = this->track_relocs_.next_symndx();
539
 
540
      bool is_ordinary;
541
      const unsigned int shndx = this->symbol_section(object, sym, &value,
542
                                                      &is_ordinary);
543
 
544
      // There is no reason to record non-ordinary section indexes, or
545
      // SHN_UNDEF, because they will never match the real section.
546
      if (is_ordinary && shndx != elfcpp::SHN_UNDEF)
547
        {
548
          value += this->track_relocs_.next_addend();
549
          this->reloc_map_[reloc_offset] = std::make_pair(shndx, value);
550
        }
551
 
552
      this->track_relocs_.advance(reloc_offset + 1);
553
    }
554
}
555
 
556
// Read the line number info.
557
 
558
template<int size, bool big_endian>
559
void
560
Sized_dwarf_line_info<size, big_endian>::read_line_mappings(Object* object,
561
                                                            unsigned int shndx)
562
{
563
  gold_assert(this->data_valid_ == true);
564
 
565
  this->read_relocs(object);
566
  while (this->buffer_ < this->buffer_end_)
567
    {
568
      const unsigned char* lineptr = this->buffer_;
569
      lineptr = this->read_header_prolog(lineptr);
570
      lineptr = this->read_header_tables(lineptr);
571
      lineptr = this->read_lines(lineptr, shndx);
572
      this->buffer_ = lineptr;
573
    }
574
 
575
  // Sort the lines numbers, so addr2line can use binary search.
576
  for (typename Lineno_map::iterator it = line_number_map_.begin();
577
       it != line_number_map_.end();
578
       ++it)
579
    // Each vector needs to be sorted by offset.
580
    std::sort(it->second.begin(), it->second.end());
581
}
582
 
583
// Some processing depends on whether the input is a .o file or not.
584
// For instance, .o files have relocs, and have .debug_lines
585
// information on a per section basis.  .so files, on the other hand,
586
// lack relocs, and offsets are unique, so we can ignore the section
587
// information.
588
 
589
template<int size, bool big_endian>
590
bool
591
Sized_dwarf_line_info<size, big_endian>::input_is_relobj()
592
{
593
  // Only .o files have relocs and the symtab buffer that goes with them.
594
  return this->symtab_buffer_ != NULL;
595
}
596
 
597
// Given an Offset_to_lineno_entry vector, and an offset, figure out
598
// if the offset points into a function according to the vector (see
599
// comments below for the algorithm).  If it does, return an iterator
600
// into the vector that points to the line-number that contains that
601
// offset.  If not, it returns vector::end().
602
 
603
static std::vector<Offset_to_lineno_entry>::const_iterator
604
offset_to_iterator(const std::vector<Offset_to_lineno_entry>* offsets,
605
                   off_t offset)
606
{
607
  const Offset_to_lineno_entry lookup_key = { offset, 0, 0, true, 0 };
608
 
609
  // lower_bound() returns the smallest offset which is >= lookup_key.
610
  // If no offset in offsets is >= lookup_key, returns end().
611
  std::vector<Offset_to_lineno_entry>::const_iterator it
612
      = std::lower_bound(offsets->begin(), offsets->end(), lookup_key);
613
 
614
  // This code is easiest to understand with a concrete example.
615
  // Here's a possible offsets array:
616
  // {{offset = 3211, header_num = 0, file_num = 1, last, line_num = 16},  // 0
617
  //  {offset = 3224, header_num = 0, file_num = 1, last, line_num = 20},  // 1
618
  //  {offset = 3226, header_num = 0, file_num = 1, last, line_num = 22},  // 2
619
  //  {offset = 3231, header_num = 0, file_num = 1, last, line_num = 25},  // 3
620
  //  {offset = 3232, header_num = 0, file_num = 1, last, line_num = -1},  // 4
621
  //  {offset = 3232, header_num = 0, file_num = 1, last, line_num = 65},  // 5
622
  //  {offset = 3235, header_num = 0, file_num = 1, last, line_num = 66},  // 6
623
  //  {offset = 3236, header_num = 0, file_num = 1, last, line_num = -1},  // 7
624
  //  {offset = 5764, header_num = 0, file_num = 1, last, line_num = 48},  // 8
625
  //  {offset = 5764, header_num = 0, file_num = 1,!last, line_num = 47},  // 9
626
  //  {offset = 5765, header_num = 0, file_num = 1, last, line_num = 49},  // 10
627
  //  {offset = 5767, header_num = 0, file_num = 1, last, line_num = 50},  // 11
628
  //  {offset = 5768, header_num = 0, file_num = 1, last, line_num = 51},  // 12
629
  //  {offset = 5773, header_num = 0, file_num = 1, last, line_num = -1},  // 13
630
  //  {offset = 5787, header_num = 1, file_num = 1, last, line_num = 19},  // 14
631
  //  {offset = 5790, header_num = 1, file_num = 1, last, line_num = 20},  // 15
632
  //  {offset = 5793, header_num = 1, file_num = 1, last, line_num = 67},  // 16
633
  //  {offset = 5793, header_num = 1, file_num = 1, last, line_num = -1},  // 17
634
  //  {offset = 5793, header_num = 1, file_num = 1,!last, line_num = 66},  // 18
635
  //  {offset = 5795, header_num = 1, file_num = 1, last, line_num = 68},  // 19
636
  //  {offset = 5798, header_num = 1, file_num = 1, last, line_num = -1},  // 20
637
  // The entries with line_num == -1 mark the end of a function: the
638
  // associated offset is one past the last instruction in the
639
  // function.  This can correspond to the beginning of the next
640
  // function (as is true for offset 3232); alternately, there can be
641
  // a gap between the end of one function and the start of the next
642
  // (as is true for some others, most obviously from 3236->5764).
643
  //
644
  // Case 1: lookup_key has offset == 10.  lower_bound returns
645
  //         offsets[0].  Since it's not an exact match and we're
646
  //         at the beginning of offsets, we return end() (invalid).
647
  // Case 2: lookup_key has offset 10000.  lower_bound returns
648
  //         offset[21] (end()).  We return end() (invalid).
649
  // Case 3: lookup_key has offset == 3211.  lower_bound matches
650
  //         offsets[0] exactly, and that's the entry we return.
651
  // Case 4: lookup_key has offset == 3232.  lower_bound returns
652
  //         offsets[4].  That's an exact match, but indicates
653
  //         end-of-function.  We check if offsets[5] is also an
654
  //         exact match but not end-of-function.  It is, so we
655
  //         return offsets[5].
656
  // Case 5: lookup_key has offset == 3214.  lower_bound returns
657
  //         offsets[1].  Since it's not an exact match, we back
658
  //         up to the offset that's < lookup_key, offsets[0].
659
  //         We note offsets[0] is a valid entry (not end-of-function),
660
  //         so that's the entry we return.
661
  // Case 6: lookup_key has offset == 4000.  lower_bound returns
662
  //         offsets[8].  Since it's not an exact match, we back
663
  //         up to offsets[7].  Since offsets[7] indicates
664
  //         end-of-function, we know lookup_key is between
665
  //         functions, so we return end() (not a valid offset).
666
  // Case 7: lookup_key has offset == 5794.  lower_bound returns
667
  //         offsets[19].  Since it's not an exact match, we back
668
  //         up to offsets[16].  Note we back up to the *first*
669
  //         entry with offset 5793, not just offsets[19-1].
670
  //         We note offsets[16] is a valid entry, so we return it.
671
  //         If offsets[16] had had line_num == -1, we would have
672
  //         checked offsets[17].  The reason for this is that
673
  //         16 and 17 can be in an arbitrary order, since we sort
674
  //         only by offset and last_line_for_offset.  (Note it
675
  //         doesn't help to use line_number as a tertiary sort key,
676
  //         since sometimes we want the -1 to be first and sometimes
677
  //         we want it to be last.)
678
 
679
  // This deals with cases (1) and (2).
680
  if ((it == offsets->begin() && offset < it->offset)
681
      || it == offsets->end())
682
    return offsets->end();
683
 
684
  // This deals with cases (3) and (4).
685
  if (offset == it->offset)
686
    {
687
      while (it != offsets->end()
688
             && it->offset == offset
689
             && it->line_num == -1)
690
        ++it;
691
      if (it == offsets->end() || it->offset != offset)
692
        return offsets->end();
693
      else
694
        return it;
695
    }
696
 
697
  // This handles the first part of case (7) -- we back up to the
698
  // *first* entry that has the offset that's behind us.
699
  gold_assert(it != offsets->begin());
700
  std::vector<Offset_to_lineno_entry>::const_iterator range_end = it;
701
  --it;
702
  const off_t range_value = it->offset;
703
  while (it != offsets->begin() && (it-1)->offset == range_value)
704
    --it;
705
 
706
  // This handles cases (5), (6), and (7): if any entry in the
707
  // equal_range [it, range_end) has a line_num != -1, it's a valid
708
  // match.  If not, we're not in a function.  The line number we saw
709
  // last for an offset will be sorted first, so it'll get returned if
710
  // it's present.
711
  for (; it != range_end; ++it)
712
    if (it->line_num != -1)
713
      return it;
714
  return offsets->end();
715
}
716
 
717
// Returns the canonical filename:lineno for the address passed in.
718
// If other_lines is not NULL, appends the non-canonical lines
719
// assigned to the same address.
720
 
721
template<int size, bool big_endian>
722
std::string
723
Sized_dwarf_line_info<size, big_endian>::do_addr2line(
724
    unsigned int shndx,
725
    off_t offset,
726
    std::vector<std::string>* other_lines)
727
{
728
  if (this->data_valid_ == false)
729
    return "";
730
 
731
  const std::vector<Offset_to_lineno_entry>* offsets;
732
  // If we do not have reloc information, then our input is a .so or
733
  // some similar data structure where all the information is held in
734
  // the offset.  In that case, we ignore the input shndx.
735
  if (this->input_is_relobj())
736
    offsets = &this->line_number_map_[shndx];
737
  else
738
    offsets = &this->line_number_map_[-1U];
739
  if (offsets->empty())
740
    return "";
741
 
742
  typename std::vector<Offset_to_lineno_entry>::const_iterator it
743
      = offset_to_iterator(offsets, offset);
744
  if (it == offsets->end())
745
    return "";
746
 
747
  std::string result = this->format_file_lineno(*it);
748
  if (other_lines != NULL)
749
    for (++it; it != offsets->end() && it->offset == offset; ++it)
750
      {
751
        if (it->line_num == -1)
752
          continue;  // The end of a previous function.
753
        other_lines->push_back(this->format_file_lineno(*it));
754
      }
755
  return result;
756
}
757
 
758
// Convert the file_num + line_num into a string.
759
 
760
template<int size, bool big_endian>
761
std::string
762
Sized_dwarf_line_info<size, big_endian>::format_file_lineno(
763
    const Offset_to_lineno_entry& loc) const
764
{
765
  std::string ret;
766
 
767
  gold_assert(loc.header_num < static_cast<int>(this->files_.size()));
768
  gold_assert(loc.file_num
769
              < static_cast<int>(this->files_[loc.header_num].size()));
770
  const std::pair<int, std::string>& filename_pair
771
      = this->files_[loc.header_num][loc.file_num];
772
  const std::string& filename = filename_pair.second;
773
 
774
  gold_assert(loc.header_num < static_cast<int>(this->directories_.size()));
775
  gold_assert(filename_pair.first
776
              < static_cast<int>(this->directories_[loc.header_num].size()));
777
  const std::string& dirname
778
      = this->directories_[loc.header_num][filename_pair.first];
779
 
780
  if (!dirname.empty())
781
    {
782
      ret += dirname;
783
      ret += "/";
784
    }
785
  ret += filename;
786
  if (ret.empty())
787
    ret = "(unknown)";
788
 
789
  char buffer[64];   // enough to hold a line number
790
  snprintf(buffer, sizeof(buffer), "%d", loc.line_num);
791
  ret += ":";
792
  ret += buffer;
793
 
794
  return ret;
795
}
796
 
797
// Dwarf_line_info routines.
798
 
799
static unsigned int next_generation_count = 0;
800
 
801
struct Addr2line_cache_entry
802
{
803
  Object* object;
804
  unsigned int shndx;
805
  Dwarf_line_info* dwarf_line_info;
806
  unsigned int generation_count;
807
  unsigned int access_count;
808
 
809
  Addr2line_cache_entry(Object* o, unsigned int s, Dwarf_line_info* d)
810
      : object(o), shndx(s), dwarf_line_info(d),
811
        generation_count(next_generation_count), access_count(0)
812
  {
813
    if (next_generation_count < (1U << 31))
814
      ++next_generation_count;
815
  }
816
};
817
// We expect this cache to be small, so don't bother with a hashtable
818
// or priority queue or anything: just use a simple vector.
819
static std::vector<Addr2line_cache_entry> addr2line_cache;
820
 
821
std::string
822
Dwarf_line_info::one_addr2line(Object* object,
823
                               unsigned int shndx, off_t offset,
824
                               size_t cache_size,
825
                               std::vector<std::string>* other_lines)
826
{
827
  Dwarf_line_info* lineinfo = NULL;
828
  std::vector<Addr2line_cache_entry>::iterator it;
829
 
830
  // First, check the cache.  If we hit, update the counts.
831
  for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
832
    {
833
      if (it->object == object && it->shndx == shndx)
834
        {
835
          lineinfo = it->dwarf_line_info;
836
          it->generation_count = next_generation_count;
837
          // We cap generation_count at 2^31 -1 to avoid overflow.
838
          if (next_generation_count < (1U << 31))
839
            ++next_generation_count;
840
          // We cap access_count at 31 so 2^access_count doesn't overflow
841
          if (it->access_count < 31)
842
            ++it->access_count;
843
          break;
844
        }
845
    }
846
 
847
  // If we don't hit the cache, create a new object and insert into the
848
  // cache.
849
  if (lineinfo == NULL)
850
  {
851
    switch (parameters->size_and_endianness())
852
      {
853
#ifdef HAVE_TARGET_32_LITTLE
854
        case Parameters::TARGET_32_LITTLE:
855
          lineinfo = new Sized_dwarf_line_info<32, false>(object, shndx); break;
856
#endif
857
#ifdef HAVE_TARGET_32_BIG
858
        case Parameters::TARGET_32_BIG:
859
          lineinfo = new Sized_dwarf_line_info<32, true>(object, shndx); break;
860
#endif
861
#ifdef HAVE_TARGET_64_LITTLE
862
        case Parameters::TARGET_64_LITTLE:
863
          lineinfo = new Sized_dwarf_line_info<64, false>(object, shndx); break;
864
#endif
865
#ifdef HAVE_TARGET_64_BIG
866
        case Parameters::TARGET_64_BIG:
867
          lineinfo = new Sized_dwarf_line_info<64, true>(object, shndx); break;
868
#endif
869
        default:
870
          gold_unreachable();
871
      }
872
    addr2line_cache.push_back(Addr2line_cache_entry(object, shndx, lineinfo));
873
  }
874
 
875
  // Now that we have our object, figure out the answer
876
  std::string retval = lineinfo->addr2line(shndx, offset, other_lines);
877
 
878
  // Finally, if our cache has grown too big, delete old objects.  We
879
  // assume the common (probably only) case is deleting only one object.
880
  // We use a pretty simple scheme to evict: function of LRU and MFU.
881
  while (addr2line_cache.size() > cache_size)
882
    {
883
      unsigned int lowest_score = ~0U;
884
      std::vector<Addr2line_cache_entry>::iterator lowest
885
          = addr2line_cache.end();
886
      for (it = addr2line_cache.begin(); it != addr2line_cache.end(); ++it)
887
        {
888
          const unsigned int score = (it->generation_count
889
                                      + (1U << it->access_count));
890
          if (score < lowest_score)
891
            {
892
              lowest_score = score;
893
              lowest = it;
894
            }
895
        }
896
      if (lowest != addr2line_cache.end())
897
        {
898
          delete lowest->dwarf_line_info;
899
          addr2line_cache.erase(lowest);
900
        }
901
    }
902
 
903
  return retval;
904
}
905
 
906
void
907
Dwarf_line_info::clear_addr2line_cache()
908
{
909
  for (std::vector<Addr2line_cache_entry>::iterator it = addr2line_cache.begin();
910
       it != addr2line_cache.end();
911
       ++it)
912
    delete it->dwarf_line_info;
913
  addr2line_cache.clear();
914
}
915
 
916
#ifdef HAVE_TARGET_32_LITTLE
917
template
918
class Sized_dwarf_line_info<32, false>;
919
#endif
920
 
921
#ifdef HAVE_TARGET_32_BIG
922
template
923
class Sized_dwarf_line_info<32, true>;
924
#endif
925
 
926
#ifdef HAVE_TARGET_64_LITTLE
927
template
928
class Sized_dwarf_line_info<64, false>;
929
#endif
930
 
931
#ifdef HAVE_TARGET_64_BIG
932
template
933
class Sized_dwarf_line_info<64, true>;
934
#endif
935
 
936
} // End namespace gold.

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.