merge from gcc
[deliverable/binutils-gdb.git] / gold / dwarf_reader.cc
CommitLineData
5c2c6c95
ILT
1// dwarf_reader.cc -- parse dwarf2/3 debug information
2
3// Copyright 2007 Free Software Foundation, Inc.
4// 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 "elfcpp_swap.h"
26#include "dwarf.h"
24badc65 27#include "object.h"
4c50553d 28#include "reloc.h"
5c2c6c95
ILT
29#include "dwarf_reader.h"
30
31namespace {
32
33// Read an unsigned LEB128 number. Each byte contains 7 bits of
34// information, plus one bit saying whether the number continues or
35// not.
36
37uint64_t
38read_unsigned_LEB_128(const unsigned char* buffer, size_t* len)
39{
40 uint64_t result = 0;
41 size_t num_read = 0;
42 unsigned int shift = 0;
43 unsigned char byte;
44
45 do
46 {
47 byte = *buffer++;
48 num_read++;
49 result |= (static_cast<uint64_t>(byte & 0x7f)) << shift;
50 shift += 7;
51 }
52 while (byte & 0x80);
53
54 *len = num_read;
55
56 return result;
57}
58
59// Read a signed LEB128 number. These are like regular LEB128
60// numbers, except the last byte may have a sign bit set.
61
62int64_t
63read_signed_LEB_128(const unsigned char* buffer, size_t* len)
64{
65 int64_t result = 0;
66 int shift = 0;
67 size_t num_read = 0;
68 unsigned char byte;
69
70 do
71 {
72 byte = *buffer++;
73 num_read++;
74 result |= (static_cast<uint64_t>(byte & 0x7f) << shift);
75 shift += 7;
76 }
77 while (byte & 0x80);
78
79 if ((shift < 8 * static_cast<int>(sizeof(result))) && (byte & 0x40))
80 result |= -((static_cast<int64_t>(1)) << shift);
81 *len = num_read;
82 return result;
83}
84
85} // End anonymous namespace.
86
87
88namespace gold {
89
90// This is the format of a DWARF2/3 line state machine that we process
91// opcodes using. There is no need for anything outside the lineinfo
92// processor to know how this works.
93
94struct LineStateMachine
95{
96 int file_num;
97 uint64_t address;
98 int line_num;
99 int column_num;
100 unsigned int shndx; // the section address refers to
101 bool is_stmt; // stmt means statement.
102 bool basic_block;
103 bool end_sequence;
104};
105
106static void
107ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt)
108{
109 lsm->file_num = 1;
110 lsm->address = 0;
111 lsm->line_num = 1;
112 lsm->column_num = 0;
338f2eba 113 lsm->shndx = -1U;
5c2c6c95
ILT
114 lsm->is_stmt = default_is_stmt;
115 lsm->basic_block = false;
116 lsm->end_sequence = false;
117}
118
24badc65 119template<int size, bool big_endian>
c261a0be
ILT
120Dwarf_line_info<size, big_endian>::Dwarf_line_info(Object* object)
121 : data_valid_(false), buffer_(NULL), symtab_buffer_(NULL),
24badc65
ILT
122 directories_(1), files_(1)
123{
124 unsigned int debug_shndx;
125 for (debug_shndx = 0; debug_shndx < object->shnum(); ++debug_shndx)
126 if (object->section_name(debug_shndx) == ".debug_line")
127 {
128 off_t buffer_size;
129 this->buffer_ = object->section_contents(
130 debug_shndx, &buffer_size, false);
131 this->buffer_end_ = this->buffer_ + buffer_size;
132 break;
133 }
134 if (this->buffer_ == NULL)
c261a0be 135 return;
24badc65
ILT
136
137 // Find the relocation section for ".debug_line".
138 bool got_relocs = false;
139 for (unsigned int reloc_shndx = 0;
140 reloc_shndx < object->shnum();
141 ++reloc_shndx)
142 {
143 unsigned int reloc_sh_type = object->section_type(reloc_shndx);
144 if ((reloc_sh_type == elfcpp::SHT_REL
145 || reloc_sh_type == elfcpp::SHT_RELA)
146 && object->section_info(reloc_shndx) == debug_shndx)
147 {
148 got_relocs = this->track_relocs_.initialize(object, reloc_shndx,
149 reloc_sh_type);
150 break;
151 }
152 }
153 if (!got_relocs)
c261a0be 154 return;
24badc65
ILT
155
156 // Finally, we need the symtab section to interpret the relocs.
157 unsigned int symtab_shndx;
158 for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx)
159 if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB)
160 {
161 off_t symtab_size;
162 this->symtab_buffer_ = object->section_contents(
163 symtab_shndx, &symtab_size, false);
164 this->symtab_buffer_end_ = this->symtab_buffer_ + symtab_size;
165 break;
166 }
167 if (this->symtab_buffer_ == NULL)
c261a0be 168 return;
24badc65
ILT
169
170 // Now that we have successfully read all the data, parse the debug
171 // info.
c261a0be 172 this->data_valid_ = true;
24badc65
ILT
173 this->read_line_mappings();
174}
175
5c2c6c95
ILT
176// Read the DWARF header.
177
178template<int size, bool big_endian>
179const unsigned char*
e43872e9
ILT
180Dwarf_line_info<size, big_endian>::read_header_prolog(
181 const unsigned char* lineptr)
5c2c6c95
ILT
182{
183 uint32_t initial_length = elfcpp::Swap<32, big_endian>::readval(lineptr);
184 lineptr += 4;
185
186 // In DWARF2/3, if the initial length is all 1 bits, then the offset
187 // size is 8 and we need to read the next 8 bytes for the real length.
188 if (initial_length == 0xffffffff)
189 {
190 header_.offset_size = 8;
191 initial_length = elfcpp::Swap<64, big_endian>::readval(lineptr);
192 lineptr += 8;
193 }
194 else
195 header_.offset_size = 4;
196
197 header_.total_length = initial_length;
198
199 gold_assert(lineptr + header_.total_length <= buffer_end_);
200
201 header_.version = elfcpp::Swap<16, big_endian>::readval(lineptr);
202 lineptr += 2;
203
204 if (header_.offset_size == 4)
205 header_.prologue_length = elfcpp::Swap<32, big_endian>::readval(lineptr);
206 else
207 header_.prologue_length = elfcpp::Swap<64, big_endian>::readval(lineptr);
208 lineptr += header_.offset_size;
209
210 header_.min_insn_length = *lineptr;
211 lineptr += 1;
212
213 header_.default_is_stmt = *lineptr;
214 lineptr += 1;
215
216 header_.line_base = *reinterpret_cast<const signed char*>(lineptr);
217 lineptr += 1;
218
219 header_.line_range = *lineptr;
220 lineptr += 1;
221
222 header_.opcode_base = *lineptr;
223 lineptr += 1;
224
225 header_.std_opcode_lengths.reserve(header_.opcode_base + 1);
226 header_.std_opcode_lengths[0] = 0;
227 for (int i = 1; i < header_.opcode_base; i++)
228 {
229 header_.std_opcode_lengths[i] = *lineptr;
230 lineptr += 1;
231 }
232
233 return lineptr;
234}
235
236// The header for a debug_line section is mildly complicated, because
237// the line info is very tightly encoded.
238
e43872e9 239template<int size, bool big_endian>
5c2c6c95 240const unsigned char*
e43872e9
ILT
241Dwarf_line_info<size, big_endian>::read_header_tables(
242 const unsigned char* lineptr)
5c2c6c95
ILT
243{
244 // It is legal for the directory entry table to be empty.
245 if (*lineptr)
246 {
247 int dirindex = 1;
248 while (*lineptr)
249 {
250 const unsigned char* dirname = lineptr;
251 gold_assert(dirindex == static_cast<int>(directories_.size()));
252 directories_.push_back(reinterpret_cast<const char*>(dirname));
253 lineptr += directories_.back().size() + 1;
254 dirindex++;
255 }
256 }
257 lineptr++;
258
259 // It is also legal for the file entry table to be empty.
260 if (*lineptr)
261 {
262 int fileindex = 1;
263 size_t len;
264 while (*lineptr)
265 {
266 const char* filename = reinterpret_cast<const char*>(lineptr);
267 lineptr += strlen(filename) + 1;
268
269 uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
270 if (dirindex >= directories_.size())
271 dirindex = 0;
272 lineptr += len;
273
274 read_unsigned_LEB_128(lineptr, &len); // mod_time
275 lineptr += len;
276
277 read_unsigned_LEB_128(lineptr, &len); // filelength
278 lineptr += len;
279
280 gold_assert(fileindex == static_cast<int>(files_.size()));
281 files_.push_back(std::pair<int, std::string>(dirindex, filename));
282 fileindex++;
283 }
284 }
285 lineptr++;
286
287 return lineptr;
288}
289
290// Process a single opcode in the .debug.line structure.
291
292// Templating on size and big_endian would yield more efficient (and
293// simpler) code, but would bloat the binary. Speed isn't important
294// here.
295
e43872e9 296template<int size, bool big_endian>
5c2c6c95 297bool
e43872e9
ILT
298Dwarf_line_info<size, big_endian>::process_one_opcode(
299 const unsigned char* start, struct LineStateMachine* lsm, size_t* len)
5c2c6c95
ILT
300{
301 size_t oplen = 0;
302 size_t templen;
303 unsigned char opcode = *start;
304 oplen++;
305 start++;
306
307 // If the opcode is great than the opcode_base, it is a special
308 // opcode. Most line programs consist mainly of special opcodes.
309 if (opcode >= header_.opcode_base)
310 {
311 opcode -= header_.opcode_base;
312 const int advance_address = ((opcode / header_.line_range)
313 * header_.min_insn_length);
314 lsm->address += advance_address;
315
316 const int advance_line = ((opcode % header_.line_range)
317 + header_.line_base);
318 lsm->line_num += advance_line;
319 lsm->basic_block = true;
320 *len = oplen;
321 return true;
322 }
323
324 // Otherwise, we have the regular opcodes
325 switch (opcode)
326 {
327 case elfcpp::DW_LNS_copy:
328 lsm->basic_block = false;
329 *len = oplen;
330 return true;
331
332 case elfcpp::DW_LNS_advance_pc:
333 {
334 const uint64_t advance_address
335 = read_unsigned_LEB_128(start, &templen);
336 oplen += templen;
337 lsm->address += header_.min_insn_length * advance_address;
338 }
339 break;
340
341 case elfcpp::DW_LNS_advance_line:
342 {
343 const uint64_t advance_line = read_signed_LEB_128(start, &templen);
344 oplen += templen;
345 lsm->line_num += advance_line;
346 }
347 break;
348
349 case elfcpp::DW_LNS_set_file:
350 {
351 const uint64_t fileno = read_unsigned_LEB_128(start, &templen);
352 oplen += templen;
353 lsm->file_num = fileno;
354 }
355 break;
356
357 case elfcpp::DW_LNS_set_column:
358 {
359 const uint64_t colno = read_unsigned_LEB_128(start, &templen);
360 oplen += templen;
361 lsm->column_num = colno;
362 }
363 break;
364
365 case elfcpp::DW_LNS_negate_stmt:
366 lsm->is_stmt = !lsm->is_stmt;
367 break;
368
369 case elfcpp::DW_LNS_set_basic_block:
370 lsm->basic_block = true;
371 break;
372
373 case elfcpp::DW_LNS_fixed_advance_pc:
374 {
375 int advance_address;
e43872e9 376 advance_address = elfcpp::Swap<16, big_endian>::readval(start);
5c2c6c95
ILT
377 oplen += 2;
378 lsm->address += advance_address;
379 }
380 break;
381
382 case elfcpp::DW_LNS_const_add_pc:
383 {
384 const int advance_address = (header_.min_insn_length
385 * ((255 - header_.opcode_base)
386 / header_.line_range));
387 lsm->address += advance_address;
388 }
389 break;
390
391 case elfcpp::DW_LNS_extended_op:
392 {
393 const uint64_t extended_op_len
394 = read_unsigned_LEB_128(start, &templen);
395 start += templen;
396 oplen += templen + extended_op_len;
397
398 const unsigned char extended_op = *start;
399 start++;
400
401 switch (extended_op)
402 {
403 case elfcpp::DW_LNE_end_sequence:
404 lsm->end_sequence = true;
405 *len = oplen;
406 return true;
407
408 case elfcpp::DW_LNE_set_address:
4c50553d
ILT
409 {
410 typename Reloc_map::const_iterator it
411 = reloc_map_.find(start - this->buffer_);
412 if (it != reloc_map_.end())
413 {
414 // value + addend.
415 lsm->address =
416 (elfcpp::Swap<size, big_endian>::readval(start)
417 + it->second.second);
418 lsm->shndx = it->second.first;
419 }
420 else
421 {
422 // Every set_address should have an associated
423 // relocation.
424 this->data_valid_ = false;
425 }
426 break;
24badc65 427 }
5c2c6c95
ILT
428 case elfcpp::DW_LNE_define_file:
429 {
430 const char* filename = reinterpret_cast<const char*>(start);
431 templen = strlen(filename) + 1;
432 start += templen;
433
434 uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
435 if (dirindex >= directories_.size())
436 dirindex = 0;
437 oplen += templen;
438
439 read_unsigned_LEB_128(start, &templen); // mod_time
440 oplen += templen;
441
442 read_unsigned_LEB_128(start, &templen); // filelength
443 oplen += templen;
444
445 files_.push_back(std::pair<int, std::string>(dirindex,
446 filename));
447 }
448 break;
449 }
450 }
451 break;
452
453 default:
454 {
455 // Ignore unknown opcode silently
456 for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++)
457 {
458 size_t templen;
459 read_unsigned_LEB_128(start, &templen);
460 start += templen;
461 oplen += templen;
462 }
463 }
464 break;
465 }
466 *len = oplen;
467 return false;
468}
469
470// Read the debug information at LINEPTR and store it in the line
471// number map.
472
e43872e9 473template<int size, bool big_endian>
5c2c6c95 474unsigned const char*
e43872e9 475Dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr)
5c2c6c95
ILT
476{
477 struct LineStateMachine lsm;
478
479 // LENGTHSTART is the place the length field is based on. It is the
480 // point in the header after the initial length field.
481 const unsigned char* lengthstart = buffer_;
482
483 // In 64 bit dwarf, the initial length is 12 bytes, because of the
484 // 0xffffffff at the start.
485 if (header_.offset_size == 8)
486 lengthstart += 12;
487 else
488 lengthstart += 4;
489
490 while (lineptr < lengthstart + header_.total_length)
491 {
492 ResetLineStateMachine(&lsm, header_.default_is_stmt);
493 while (!lsm.end_sequence)
494 {
495 size_t oplength;
e43872e9 496 bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength);
5c2c6c95
ILT
497 if (add_line)
498 {
499 Offset_to_lineno_entry entry
500 = { lsm.address, lsm.file_num, lsm.line_num };
501 line_number_map_[lsm.shndx].push_back(entry);
502 }
503 lineptr += oplength;
504 }
505 }
506
507 return lengthstart + header_.total_length;
508}
509
4c50553d
ILT
510// Looks in the symtab to see what section a symbol is in.
511
512template<int size, bool big_endian>
513unsigned int
514Dwarf_line_info<size, big_endian>::symbol_section(
515 unsigned int sym,
516 typename elfcpp::Elf_types<size>::Elf_Addr* value)
517{
518 const int symsize = elfcpp::Elf_sizes<size>::sym_size;
519 gold_assert(this->symtab_buffer_ + sym * symsize < this->symtab_buffer_end_);
520 elfcpp::Sym<size, big_endian> elfsym(this->symtab_buffer_ + sym * symsize);
521 *value = elfsym.get_st_value();
522 return elfsym.get_st_shndx();
523}
524
525// Read the relocations into a Reloc_map.
526
527template<int size, bool big_endian>
528void
529Dwarf_line_info<size, big_endian>::read_relocs()
530{
531 if (this->symtab_buffer_ == NULL)
532 return;
533
534 typename elfcpp::Elf_types<size>::Elf_Addr value;
535 off_t reloc_offset;
24badc65 536 while ((reloc_offset = this->track_relocs_.next_offset()) != -1)
4c50553d 537 {
24badc65 538 const unsigned int sym = this->track_relocs_.next_symndx();
4c50553d
ILT
539 const unsigned int shndx = this->symbol_section(sym, &value);
540 this->reloc_map_[reloc_offset] = std::make_pair(shndx, value);
24badc65 541 this->track_relocs_.advance(reloc_offset + 1);
4c50553d
ILT
542 }
543}
544
545// Read the line number info.
546
e43872e9 547template<int size, bool big_endian>
5c2c6c95 548void
e43872e9 549Dwarf_line_info<size, big_endian>::read_line_mappings()
5c2c6c95 550{
c261a0be 551 gold_assert(this->data_valid_ == true);
24badc65 552
4c50553d
ILT
553 read_relocs();
554 while (this->buffer_ < this->buffer_end_)
e43872e9 555 {
4c50553d 556 const unsigned char* lineptr = this->buffer_;
e43872e9
ILT
557 lineptr = this->read_header_prolog(lineptr);
558 lineptr = this->read_header_tables(lineptr);
559 lineptr = this->read_lines(lineptr);
4c50553d 560 this->buffer_ = lineptr;
e43872e9
ILT
561 }
562
563 // Sort the lines numbers, so addr2line can use binary search.
564 for (typename Lineno_map::iterator it = line_number_map_.begin();
5c2c6c95
ILT
565 it != line_number_map_.end();
566 ++it)
567 // Each vector needs to be sorted by offset.
4c50553d 568 std::sort(it->second.begin(), it->second.end());
5c2c6c95
ILT
569}
570
571// Return a string for a file name and line number.
572
e43872e9 573template<int size, bool big_endian>
5c2c6c95 574std::string
e43872e9 575Dwarf_line_info<size, big_endian>::addr2line(unsigned int shndx, off_t offset)
5c2c6c95 576{
4c50553d
ILT
577 if (this->data_valid_ == false)
578 return "";
579
5c2c6c95 580 const Offset_to_lineno_entry lookup_key = { offset, 0, 0 };
4c50553d
ILT
581 std::vector<Offset_to_lineno_entry>& offsets = this->line_number_map_[shndx];
582 if (offsets.empty())
583 return "";
584
e43872e9 585 typename std::vector<Offset_to_lineno_entry>::const_iterator it
5c2c6c95
ILT
586 = std::lower_bound(offsets.begin(), offsets.end(), lookup_key);
587
588 // If we found an exact match, great, otherwise find the last entry
589 // before the passed-in offset.
590 if (it->offset > offset)
591 {
592 if (it == offsets.begin())
593 return "";
594 --it;
595 gold_assert(it->offset < offset);
596 }
597
598 // Convert the file_num + line_num into a string.
599 std::string ret;
600 gold_assert(it->file_num < static_cast<int>(files_.size()));
601 const std::pair<int, std::string>& filename_pair = files_[it->file_num];
602 gold_assert(filename_pair.first < static_cast<int>(directories_.size()));
603 const std::string& dirname = directories_[filename_pair.first];
604 const std::string& filename = filename_pair.second;
605 if (!dirname.empty())
606 {
607 ret += dirname;
608 ret += "/";
609 }
610 ret += filename;
611 if (ret.empty())
612 ret = "(unknown)";
613
614 char buffer[64]; // enough to hold a line number
615 snprintf(buffer, sizeof(buffer), "%d", it->line_num);
616 ret += ":";
617 ret += buffer;
618
619 return ret;
620}
621
622#ifdef HAVE_TARGET_32_LITTLE
623template
e43872e9 624class Dwarf_line_info<32, false>;
5c2c6c95
ILT
625#endif
626
627#ifdef HAVE_TARGET_32_BIG
628template
e43872e9 629class Dwarf_line_info<32, true>;
5c2c6c95
ILT
630#endif
631
632#ifdef HAVE_TARGET_64_LITTLE
633template
e43872e9 634class Dwarf_line_info<64, false>;
5c2c6c95
ILT
635#endif
636
637#ifdef HAVE_TARGET_64_BIG
638template
e43872e9 639class Dwarf_line_info<64, true>;
5c2c6c95
ILT
640#endif
641
642} // End namespace gold.
This page took 0.047462 seconds and 4 git commands to generate.