*** empty log message ***
[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),
af674d1d 122 directories_(), files_(), current_header_index_(-1)
24badc65
ILT
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".
af674d1d 138 // We expect these for relobjs (.o's) but not dynobjs (.so's).
24badc65
ILT
139 bool got_relocs = false;
140 for (unsigned int reloc_shndx = 0;
141 reloc_shndx < object->shnum();
142 ++reloc_shndx)
143 {
144 unsigned int reloc_sh_type = object->section_type(reloc_shndx);
145 if ((reloc_sh_type == elfcpp::SHT_REL
146 || reloc_sh_type == elfcpp::SHT_RELA)
147 && object->section_info(reloc_shndx) == debug_shndx)
148 {
149 got_relocs = this->track_relocs_.initialize(object, reloc_shndx,
150 reloc_sh_type);
151 break;
152 }
153 }
24badc65
ILT
154
155 // Finally, we need the symtab section to interpret the relocs.
af674d1d
ILT
156 if (got_relocs)
157 {
158 unsigned int symtab_shndx;
159 for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx)
160 if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB)
161 {
162 this->symtab_buffer_ = object->section_contents(
163 symtab_shndx, &this->symtab_buffer_size_, false);
164 break;
165 }
166 if (this->symtab_buffer_ == NULL)
167 return;
168 }
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 243{
af674d1d
ILT
244 ++this->current_header_index_;
245
246 // Create a new directories_ entry and a new files_ entry for our new
247 // header. We initialize each with a single empty element, because
248 // dwarf indexes directory and filenames starting at 1.
249 gold_assert(static_cast<int>(this->directories_.size())
250 == this->current_header_index_);
251 gold_assert(static_cast<int>(this->files_.size())
252 == this->current_header_index_);
253 this->directories_.push_back(std::vector<std::string>(1));
254 this->files_.push_back(std::vector<std::pair<int, std::string> >(1));
255
5c2c6c95
ILT
256 // It is legal for the directory entry table to be empty.
257 if (*lineptr)
258 {
259 int dirindex = 1;
260 while (*lineptr)
261 {
af674d1d
ILT
262 const char* dirname = reinterpret_cast<const char*>(lineptr);
263 gold_assert(dirindex
264 == static_cast<int>(this->directories_.back().size()));
265 this->directories_.back().push_back(dirname);
266 lineptr += this->directories_.back().back().size() + 1;
5c2c6c95
ILT
267 dirindex++;
268 }
269 }
270 lineptr++;
271
272 // It is also legal for the file entry table to be empty.
273 if (*lineptr)
274 {
275 int fileindex = 1;
276 size_t len;
277 while (*lineptr)
278 {
279 const char* filename = reinterpret_cast<const char*>(lineptr);
280 lineptr += strlen(filename) + 1;
281
282 uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
5c2c6c95
ILT
283 lineptr += len;
284
af674d1d
ILT
285 if (dirindex >= this->directories_.back().size())
286 dirindex = 0;
287 int dirindexi = static_cast<int>(dirindex);
288
5c2c6c95
ILT
289 read_unsigned_LEB_128(lineptr, &len); // mod_time
290 lineptr += len;
291
292 read_unsigned_LEB_128(lineptr, &len); // filelength
293 lineptr += len;
294
af674d1d
ILT
295 gold_assert(fileindex
296 == static_cast<int>(this->files_.back().size()));
297 this->files_.back().push_back(std::make_pair(dirindexi, filename));
5c2c6c95
ILT
298 fileindex++;
299 }
300 }
301 lineptr++;
302
303 return lineptr;
304}
305
306// Process a single opcode in the .debug.line structure.
307
308// Templating on size and big_endian would yield more efficient (and
309// simpler) code, but would bloat the binary. Speed isn't important
310// here.
311
e43872e9 312template<int size, bool big_endian>
5c2c6c95 313bool
e43872e9
ILT
314Dwarf_line_info<size, big_endian>::process_one_opcode(
315 const unsigned char* start, struct LineStateMachine* lsm, size_t* len)
5c2c6c95
ILT
316{
317 size_t oplen = 0;
318 size_t templen;
319 unsigned char opcode = *start;
320 oplen++;
321 start++;
322
323 // If the opcode is great than the opcode_base, it is a special
324 // opcode. Most line programs consist mainly of special opcodes.
325 if (opcode >= header_.opcode_base)
326 {
327 opcode -= header_.opcode_base;
328 const int advance_address = ((opcode / header_.line_range)
329 * header_.min_insn_length);
330 lsm->address += advance_address;
331
332 const int advance_line = ((opcode % header_.line_range)
333 + header_.line_base);
334 lsm->line_num += advance_line;
335 lsm->basic_block = true;
336 *len = oplen;
337 return true;
338 }
339
340 // Otherwise, we have the regular opcodes
341 switch (opcode)
342 {
343 case elfcpp::DW_LNS_copy:
344 lsm->basic_block = false;
345 *len = oplen;
346 return true;
347
348 case elfcpp::DW_LNS_advance_pc:
349 {
350 const uint64_t advance_address
351 = read_unsigned_LEB_128(start, &templen);
352 oplen += templen;
353 lsm->address += header_.min_insn_length * advance_address;
354 }
355 break;
356
357 case elfcpp::DW_LNS_advance_line:
358 {
359 const uint64_t advance_line = read_signed_LEB_128(start, &templen);
360 oplen += templen;
361 lsm->line_num += advance_line;
362 }
363 break;
364
365 case elfcpp::DW_LNS_set_file:
366 {
367 const uint64_t fileno = read_unsigned_LEB_128(start, &templen);
368 oplen += templen;
369 lsm->file_num = fileno;
370 }
371 break;
372
373 case elfcpp::DW_LNS_set_column:
374 {
375 const uint64_t colno = read_unsigned_LEB_128(start, &templen);
376 oplen += templen;
377 lsm->column_num = colno;
378 }
379 break;
380
381 case elfcpp::DW_LNS_negate_stmt:
382 lsm->is_stmt = !lsm->is_stmt;
383 break;
384
385 case elfcpp::DW_LNS_set_basic_block:
386 lsm->basic_block = true;
387 break;
388
389 case elfcpp::DW_LNS_fixed_advance_pc:
390 {
391 int advance_address;
e43872e9 392 advance_address = elfcpp::Swap<16, big_endian>::readval(start);
5c2c6c95
ILT
393 oplen += 2;
394 lsm->address += advance_address;
395 }
396 break;
397
398 case elfcpp::DW_LNS_const_add_pc:
399 {
400 const int advance_address = (header_.min_insn_length
401 * ((255 - header_.opcode_base)
402 / header_.line_range));
403 lsm->address += advance_address;
404 }
405 break;
406
407 case elfcpp::DW_LNS_extended_op:
408 {
409 const uint64_t extended_op_len
410 = read_unsigned_LEB_128(start, &templen);
411 start += templen;
412 oplen += templen + extended_op_len;
413
414 const unsigned char extended_op = *start;
415 start++;
416
417 switch (extended_op)
418 {
419 case elfcpp::DW_LNE_end_sequence:
420 lsm->end_sequence = true;
421 *len = oplen;
422 return true;
423
424 case elfcpp::DW_LNE_set_address:
4c50553d 425 {
af674d1d 426 lsm->address = elfcpp::Swap<size, big_endian>::readval(start);
4c50553d
ILT
427 typename Reloc_map::const_iterator it
428 = reloc_map_.find(start - this->buffer_);
429 if (it != reloc_map_.end())
430 {
431 // value + addend.
af674d1d 432 lsm->address += it->second.second;
4c50553d
ILT
433 lsm->shndx = it->second.first;
434 }
435 else
436 {
af674d1d
ILT
437 // If we're a normal .o file, with relocs, every
438 // set_address should have an associated relocation.
439 if (this->input_is_relobj())
440 this->data_valid_ = false;
4c50553d
ILT
441 }
442 break;
24badc65 443 }
5c2c6c95
ILT
444 case elfcpp::DW_LNE_define_file:
445 {
446 const char* filename = reinterpret_cast<const char*>(start);
447 templen = strlen(filename) + 1;
448 start += templen;
449
450 uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
5c2c6c95
ILT
451 oplen += templen;
452
af674d1d
ILT
453 if (dirindex >= this->directories_.back().size())
454 dirindex = 0;
455 int dirindexi = static_cast<int>(dirindex);
456
5c2c6c95
ILT
457 read_unsigned_LEB_128(start, &templen); // mod_time
458 oplen += templen;
459
460 read_unsigned_LEB_128(start, &templen); // filelength
461 oplen += templen;
462
af674d1d 463 this->files_.back().push_back(std::make_pair(dirindexi,
5c2c6c95
ILT
464 filename));
465 }
466 break;
467 }
468 }
469 break;
470
471 default:
472 {
473 // Ignore unknown opcode silently
474 for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++)
475 {
476 size_t templen;
477 read_unsigned_LEB_128(start, &templen);
478 start += templen;
479 oplen += templen;
480 }
481 }
482 break;
483 }
484 *len = oplen;
485 return false;
486}
487
488// Read the debug information at LINEPTR and store it in the line
489// number map.
490
e43872e9 491template<int size, bool big_endian>
5c2c6c95 492unsigned const char*
e43872e9 493Dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr)
5c2c6c95
ILT
494{
495 struct LineStateMachine lsm;
496
497 // LENGTHSTART is the place the length field is based on. It is the
498 // point in the header after the initial length field.
499 const unsigned char* lengthstart = buffer_;
500
501 // In 64 bit dwarf, the initial length is 12 bytes, because of the
502 // 0xffffffff at the start.
503 if (header_.offset_size == 8)
504 lengthstart += 12;
505 else
506 lengthstart += 4;
507
508 while (lineptr < lengthstart + header_.total_length)
509 {
510 ResetLineStateMachine(&lsm, header_.default_is_stmt);
511 while (!lsm.end_sequence)
512 {
513 size_t oplength;
e43872e9 514 bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength);
5c2c6c95
ILT
515 if (add_line)
516 {
517 Offset_to_lineno_entry entry
af674d1d
ILT
518 = { lsm.address, this->current_header_index_,
519 lsm.file_num, lsm.line_num };
5c2c6c95
ILT
520 line_number_map_[lsm.shndx].push_back(entry);
521 }
522 lineptr += oplength;
523 }
524 }
525
526 return lengthstart + header_.total_length;
527}
528
4c50553d
ILT
529// Looks in the symtab to see what section a symbol is in.
530
531template<int size, bool big_endian>
532unsigned int
533Dwarf_line_info<size, big_endian>::symbol_section(
534 unsigned int sym,
535 typename elfcpp::Elf_types<size>::Elf_Addr* value)
536{
537 const int symsize = elfcpp::Elf_sizes<size>::sym_size;
af674d1d 538 gold_assert(sym * symsize < this->symtab_buffer_size_);
4c50553d
ILT
539 elfcpp::Sym<size, big_endian> elfsym(this->symtab_buffer_ + sym * symsize);
540 *value = elfsym.get_st_value();
541 return elfsym.get_st_shndx();
542}
543
544// Read the relocations into a Reloc_map.
545
546template<int size, bool big_endian>
547void
548Dwarf_line_info<size, big_endian>::read_relocs()
549{
550 if (this->symtab_buffer_ == NULL)
551 return;
552
553 typename elfcpp::Elf_types<size>::Elf_Addr value;
554 off_t reloc_offset;
24badc65 555 while ((reloc_offset = this->track_relocs_.next_offset()) != -1)
4c50553d 556 {
24badc65 557 const unsigned int sym = this->track_relocs_.next_symndx();
4c50553d
ILT
558 const unsigned int shndx = this->symbol_section(sym, &value);
559 this->reloc_map_[reloc_offset] = std::make_pair(shndx, value);
24badc65 560 this->track_relocs_.advance(reloc_offset + 1);
4c50553d
ILT
561 }
562}
563
564// Read the line number info.
565
e43872e9 566template<int size, bool big_endian>
5c2c6c95 567void
e43872e9 568Dwarf_line_info<size, big_endian>::read_line_mappings()
5c2c6c95 569{
c261a0be 570 gold_assert(this->data_valid_ == true);
24badc65 571
4c50553d
ILT
572 read_relocs();
573 while (this->buffer_ < this->buffer_end_)
e43872e9 574 {
4c50553d 575 const unsigned char* lineptr = this->buffer_;
e43872e9
ILT
576 lineptr = this->read_header_prolog(lineptr);
577 lineptr = this->read_header_tables(lineptr);
578 lineptr = this->read_lines(lineptr);
4c50553d 579 this->buffer_ = lineptr;
e43872e9
ILT
580 }
581
582 // Sort the lines numbers, so addr2line can use binary search.
583 for (typename Lineno_map::iterator it = line_number_map_.begin();
5c2c6c95
ILT
584 it != line_number_map_.end();
585 ++it)
586 // Each vector needs to be sorted by offset.
4c50553d 587 std::sort(it->second.begin(), it->second.end());
5c2c6c95
ILT
588}
589
af674d1d
ILT
590// Some processing depends on whether the input is a .o file or not.
591// For instance, .o files have relocs, and have .debug_lines
592// information on a per section basis. .so files, on the other hand,
593// lack relocs, and offsets are unique, so we can ignore the section
594// information.
595
596template<int size, bool big_endian>
597bool
598Dwarf_line_info<size, big_endian>::input_is_relobj()
599{
600 // Only .o files have relocs and the symtab buffer that goes with them.
601 return this->symtab_buffer_ != NULL;
602}
603
604
5c2c6c95
ILT
605// Return a string for a file name and line number.
606
e43872e9 607template<int size, bool big_endian>
5c2c6c95 608std::string
e43872e9 609Dwarf_line_info<size, big_endian>::addr2line(unsigned int shndx, off_t offset)
5c2c6c95 610{
4c50553d
ILT
611 if (this->data_valid_ == false)
612 return "";
613
af674d1d
ILT
614 const Offset_to_lineno_entry lookup_key = { offset, 0, 0, 0 };
615 const std::vector<Offset_to_lineno_entry>* offsets;
616 // If we do not have reloc information, then our input is a .so or
617 // some similar data structure where all the information is held in
618 // the offset. In that case, we ignore the input shndx.
619 if (this->input_is_relobj())
620 offsets = &this->line_number_map_[shndx];
621 else
622 offsets = &this->line_number_map_[-1U];
623 if (offsets->empty())
4c50553d
ILT
624 return "";
625
e43872e9 626 typename std::vector<Offset_to_lineno_entry>::const_iterator it
af674d1d 627 = std::lower_bound(offsets->begin(), offsets->end(), lookup_key);
5c2c6c95
ILT
628
629 // If we found an exact match, great, otherwise find the last entry
630 // before the passed-in offset.
631 if (it->offset > offset)
632 {
af674d1d 633 if (it == offsets->begin())
5c2c6c95
ILT
634 return "";
635 --it;
636 gold_assert(it->offset < offset);
637 }
638
639 // Convert the file_num + line_num into a string.
640 std::string ret;
af674d1d
ILT
641
642 gold_assert(it->header_num < static_cast<int>(this->files_.size()));
643 gold_assert(it->file_num
644 < static_cast<int>(this->files_[it->header_num].size()));
645 const std::pair<int, std::string>& filename_pair
646 = this->files_[it->header_num][it->file_num];
5c2c6c95 647 const std::string& filename = filename_pair.second;
af674d1d
ILT
648
649 gold_assert(it->header_num < static_cast<int>(this->directories_.size()));
650 gold_assert(filename_pair.first
651 < static_cast<int>(this->directories_[it->header_num].size()));
652 const std::string& dirname
653 = this->directories_[it->header_num][filename_pair.first];
654
5c2c6c95
ILT
655 if (!dirname.empty())
656 {
657 ret += dirname;
658 ret += "/";
659 }
660 ret += filename;
661 if (ret.empty())
662 ret = "(unknown)";
663
664 char buffer[64]; // enough to hold a line number
665 snprintf(buffer, sizeof(buffer), "%d", it->line_num);
666 ret += ":";
667 ret += buffer;
668
669 return ret;
670}
671
672#ifdef HAVE_TARGET_32_LITTLE
673template
e43872e9 674class Dwarf_line_info<32, false>;
5c2c6c95
ILT
675#endif
676
677#ifdef HAVE_TARGET_32_BIG
678template
e43872e9 679class Dwarf_line_info<32, true>;
5c2c6c95
ILT
680#endif
681
682#ifdef HAVE_TARGET_64_LITTLE
683template
e43872e9 684class Dwarf_line_info<64, false>;
5c2c6c95
ILT
685#endif
686
687#ifdef HAVE_TARGET_64_BIG
688template
e43872e9 689class Dwarf_line_info<64, true>;
5c2c6c95
ILT
690#endif
691
692} // End namespace gold.
This page took 0.077316 seconds and 4 git commands to generate.