1 // dwarf_reader.cc -- parse dwarf2/3 debug information
3 // Copyright 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
25 #include "elfcpp_swap.h"
28 #include "parameters.h"
30 #include "dwarf_reader.h"
34 // Read an unsigned LEB128 number. Each byte contains 7 bits of
35 // information, plus one bit saying whether the number continues or
39 read_unsigned_LEB_128(const unsigned char* buffer, size_t* len)
43 unsigned int shift = 0;
50 result |= (static_cast<uint64_t>(byte & 0x7f)) << shift;
60 // Read a signed LEB128 number. These are like regular LEB128
61 // numbers, except the last byte may have a sign bit set.
64 read_signed_LEB_128(const unsigned char* buffer, size_t* len)
75 result |= (static_cast<uint64_t>(byte & 0x7f) << shift);
80 if ((shift < 8 * static_cast<int>(sizeof(result))) && (byte & 0x40))
81 result |= -((static_cast<int64_t>(1)) << shift);
86 } // End anonymous namespace.
91 // This is the format of a DWARF2/3 line state machine that we process
92 // opcodes using. There is no need for anything outside the lineinfo
93 // processor to know how this works.
95 struct LineStateMachine
101 unsigned int shndx; // the section address refers to
102 bool is_stmt; // stmt means statement.
108 ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt)
115 lsm->is_stmt = default_is_stmt;
116 lsm->basic_block = false;
117 lsm->end_sequence = false;
120 template<int size, bool big_endian>
121 Sized_dwarf_line_info<size, big_endian>::Sized_dwarf_line_info(Object* object)
122 : data_valid_(false), buffer_(NULL), symtab_buffer_(NULL),
123 directories_(), files_(), current_header_index_(-1)
125 unsigned int debug_shndx;
126 for (debug_shndx = 0; debug_shndx < object->shnum(); ++debug_shndx)
127 if (object->section_name(debug_shndx) == ".debug_line")
130 this->buffer_ = object->section_contents(
131 debug_shndx, &buffer_size, false);
132 this->buffer_end_ = this->buffer_ + buffer_size;
135 if (this->buffer_ == NULL)
138 // Find the relocation section for ".debug_line".
139 // We expect these for relobjs (.o's) but not dynobjs (.so's).
140 bool got_relocs = false;
141 for (unsigned int reloc_shndx = 0;
142 reloc_shndx < object->shnum();
145 unsigned int reloc_sh_type = object->section_type(reloc_shndx);
146 if ((reloc_sh_type == elfcpp::SHT_REL
147 || reloc_sh_type == elfcpp::SHT_RELA)
148 && object->section_info(reloc_shndx) == debug_shndx)
150 got_relocs = this->track_relocs_.initialize(object, reloc_shndx,
156 // Finally, we need the symtab section to interpret the relocs.
159 unsigned int symtab_shndx;
160 for (symtab_shndx = 0; symtab_shndx < object->shnum(); ++symtab_shndx)
161 if (object->section_type(symtab_shndx) == elfcpp::SHT_SYMTAB)
163 this->symtab_buffer_ = object->section_contents(
164 symtab_shndx, &this->symtab_buffer_size_, false);
167 if (this->symtab_buffer_ == NULL)
171 // Now that we have successfully read all the data, parse the debug
173 this->data_valid_ = true;
174 this->read_line_mappings();
177 // Read the DWARF header.
179 template<int size, bool big_endian>
181 Sized_dwarf_line_info<size, big_endian>::read_header_prolog(
182 const unsigned char* lineptr)
184 uint32_t initial_length = elfcpp::Swap<32, big_endian>::readval(lineptr);
187 // In DWARF2/3, if the initial length is all 1 bits, then the offset
188 // size is 8 and we need to read the next 8 bytes for the real length.
189 if (initial_length == 0xffffffff)
191 header_.offset_size = 8;
192 initial_length = elfcpp::Swap<64, big_endian>::readval(lineptr);
196 header_.offset_size = 4;
198 header_.total_length = initial_length;
200 gold_assert(lineptr + header_.total_length <= buffer_end_);
202 header_.version = elfcpp::Swap<16, big_endian>::readval(lineptr);
205 if (header_.offset_size == 4)
206 header_.prologue_length = elfcpp::Swap<32, big_endian>::readval(lineptr);
208 header_.prologue_length = elfcpp::Swap<64, big_endian>::readval(lineptr);
209 lineptr += header_.offset_size;
211 header_.min_insn_length = *lineptr;
214 header_.default_is_stmt = *lineptr;
217 header_.line_base = *reinterpret_cast<const signed char*>(lineptr);
220 header_.line_range = *lineptr;
223 header_.opcode_base = *lineptr;
226 header_.std_opcode_lengths.reserve(header_.opcode_base + 1);
227 header_.std_opcode_lengths[0] = 0;
228 for (int i = 1; i < header_.opcode_base; i++)
230 header_.std_opcode_lengths[i] = *lineptr;
237 // The header for a debug_line section is mildly complicated, because
238 // the line info is very tightly encoded.
240 template<int size, bool big_endian>
242 Sized_dwarf_line_info<size, big_endian>::read_header_tables(
243 const unsigned char* lineptr)
245 ++this->current_header_index_;
247 // Create a new directories_ entry and a new files_ entry for our new
248 // header. We initialize each with a single empty element, because
249 // dwarf indexes directory and filenames starting at 1.
250 gold_assert(static_cast<int>(this->directories_.size())
251 == this->current_header_index_);
252 gold_assert(static_cast<int>(this->files_.size())
253 == this->current_header_index_);
254 this->directories_.push_back(std::vector<std::string>(1));
255 this->files_.push_back(std::vector<std::pair<int, std::string> >(1));
257 // It is legal for the directory entry table to be empty.
263 const char* dirname = reinterpret_cast<const char*>(lineptr);
265 == static_cast<int>(this->directories_.back().size()));
266 this->directories_.back().push_back(dirname);
267 lineptr += this->directories_.back().back().size() + 1;
273 // It is also legal for the file entry table to be empty.
280 const char* filename = reinterpret_cast<const char*>(lineptr);
281 lineptr += strlen(filename) + 1;
283 uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
286 if (dirindex >= this->directories_.back().size())
288 int dirindexi = static_cast<int>(dirindex);
290 read_unsigned_LEB_128(lineptr, &len); // mod_time
293 read_unsigned_LEB_128(lineptr, &len); // filelength
296 gold_assert(fileindex
297 == static_cast<int>(this->files_.back().size()));
298 this->files_.back().push_back(std::make_pair(dirindexi, filename));
307 // Process a single opcode in the .debug.line structure.
309 // Templating on size and big_endian would yield more efficient (and
310 // simpler) code, but would bloat the binary. Speed isn't important
313 template<int size, bool big_endian>
315 Sized_dwarf_line_info<size, big_endian>::process_one_opcode(
316 const unsigned char* start, struct LineStateMachine* lsm, size_t* len)
320 unsigned char opcode = *start;
324 // If the opcode is great than the opcode_base, it is a special
325 // opcode. Most line programs consist mainly of special opcodes.
326 if (opcode >= header_.opcode_base)
328 opcode -= header_.opcode_base;
329 const int advance_address = ((opcode / header_.line_range)
330 * header_.min_insn_length);
331 lsm->address += advance_address;
333 const int advance_line = ((opcode % header_.line_range)
334 + header_.line_base);
335 lsm->line_num += advance_line;
336 lsm->basic_block = true;
341 // Otherwise, we have the regular opcodes
344 case elfcpp::DW_LNS_copy:
345 lsm->basic_block = false;
349 case elfcpp::DW_LNS_advance_pc:
351 const uint64_t advance_address
352 = read_unsigned_LEB_128(start, &templen);
354 lsm->address += header_.min_insn_length * advance_address;
358 case elfcpp::DW_LNS_advance_line:
360 const uint64_t advance_line = read_signed_LEB_128(start, &templen);
362 lsm->line_num += advance_line;
366 case elfcpp::DW_LNS_set_file:
368 const uint64_t fileno = read_unsigned_LEB_128(start, &templen);
370 lsm->file_num = fileno;
374 case elfcpp::DW_LNS_set_column:
376 const uint64_t colno = read_unsigned_LEB_128(start, &templen);
378 lsm->column_num = colno;
382 case elfcpp::DW_LNS_negate_stmt:
383 lsm->is_stmt = !lsm->is_stmt;
386 case elfcpp::DW_LNS_set_basic_block:
387 lsm->basic_block = true;
390 case elfcpp::DW_LNS_fixed_advance_pc:
393 advance_address = elfcpp::Swap<16, big_endian>::readval(start);
395 lsm->address += advance_address;
399 case elfcpp::DW_LNS_const_add_pc:
401 const int advance_address = (header_.min_insn_length
402 * ((255 - header_.opcode_base)
403 / header_.line_range));
404 lsm->address += advance_address;
408 case elfcpp::DW_LNS_extended_op:
410 const uint64_t extended_op_len
411 = read_unsigned_LEB_128(start, &templen);
413 oplen += templen + extended_op_len;
415 const unsigned char extended_op = *start;
420 case elfcpp::DW_LNE_end_sequence:
421 lsm->end_sequence = true;
425 case elfcpp::DW_LNE_set_address:
427 lsm->address = elfcpp::Swap<size, big_endian>::readval(start);
428 typename Reloc_map::const_iterator it
429 = reloc_map_.find(start - this->buffer_);
430 if (it != reloc_map_.end())
433 lsm->address += it->second.second;
434 lsm->shndx = it->second.first;
438 // If we're a normal .o file, with relocs, every
439 // set_address should have an associated relocation.
440 if (this->input_is_relobj())
441 this->data_valid_ = false;
445 case elfcpp::DW_LNE_define_file:
447 const char* filename = reinterpret_cast<const char*>(start);
448 templen = strlen(filename) + 1;
451 uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
454 if (dirindex >= this->directories_.back().size())
456 int dirindexi = static_cast<int>(dirindex);
458 read_unsigned_LEB_128(start, &templen); // mod_time
461 read_unsigned_LEB_128(start, &templen); // filelength
464 this->files_.back().push_back(std::make_pair(dirindexi,
474 // Ignore unknown opcode silently
475 for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++)
478 read_unsigned_LEB_128(start, &templen);
489 // Read the debug information at LINEPTR and store it in the line
492 template<int size, bool big_endian>
494 Sized_dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr)
496 struct LineStateMachine lsm;
498 // LENGTHSTART is the place the length field is based on. It is the
499 // point in the header after the initial length field.
500 const unsigned char* lengthstart = buffer_;
502 // In 64 bit dwarf, the initial length is 12 bytes, because of the
503 // 0xffffffff at the start.
504 if (header_.offset_size == 8)
509 while (lineptr < lengthstart + header_.total_length)
511 ResetLineStateMachine(&lsm, header_.default_is_stmt);
512 while (!lsm.end_sequence)
515 bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength);
518 Offset_to_lineno_entry entry
519 = { lsm.address, this->current_header_index_,
520 lsm.file_num, lsm.line_num };
521 line_number_map_[lsm.shndx].push_back(entry);
527 return lengthstart + header_.total_length;
530 // Looks in the symtab to see what section a symbol is in.
532 template<int size, bool big_endian>
534 Sized_dwarf_line_info<size, big_endian>::symbol_section(
536 typename elfcpp::Elf_types<size>::Elf_Addr* value)
538 const int symsize = elfcpp::Elf_sizes<size>::sym_size;
539 gold_assert(sym * symsize < this->symtab_buffer_size_);
540 elfcpp::Sym<size, big_endian> elfsym(this->symtab_buffer_ + sym * symsize);
541 *value = elfsym.get_st_value();
542 return elfsym.get_st_shndx();
545 // Read the relocations into a Reloc_map.
547 template<int size, bool big_endian>
549 Sized_dwarf_line_info<size, big_endian>::read_relocs()
551 if (this->symtab_buffer_ == NULL)
554 typename elfcpp::Elf_types<size>::Elf_Addr value;
556 while ((reloc_offset = this->track_relocs_.next_offset()) != -1)
558 const unsigned int sym = this->track_relocs_.next_symndx();
559 const unsigned int shndx = this->symbol_section(sym, &value);
560 this->reloc_map_[reloc_offset] = std::make_pair(shndx, value);
561 this->track_relocs_.advance(reloc_offset + 1);
565 // Read the line number info.
567 template<int size, bool big_endian>
569 Sized_dwarf_line_info<size, big_endian>::read_line_mappings()
571 gold_assert(this->data_valid_ == true);
574 while (this->buffer_ < this->buffer_end_)
576 const unsigned char* lineptr = this->buffer_;
577 lineptr = this->read_header_prolog(lineptr);
578 lineptr = this->read_header_tables(lineptr);
579 lineptr = this->read_lines(lineptr);
580 this->buffer_ = lineptr;
583 // Sort the lines numbers, so addr2line can use binary search.
584 for (typename Lineno_map::iterator it = line_number_map_.begin();
585 it != line_number_map_.end();
587 // Each vector needs to be sorted by offset.
588 std::sort(it->second.begin(), it->second.end());
591 // Some processing depends on whether the input is a .o file or not.
592 // For instance, .o files have relocs, and have .debug_lines
593 // information on a per section basis. .so files, on the other hand,
594 // lack relocs, and offsets are unique, so we can ignore the section
597 template<int size, bool big_endian>
599 Sized_dwarf_line_info<size, big_endian>::input_is_relobj()
601 // Only .o files have relocs and the symtab buffer that goes with them.
602 return this->symtab_buffer_ != NULL;
606 // Return a string for a file name and line number.
608 template<int size, bool big_endian>
610 Sized_dwarf_line_info<size, big_endian>::do_addr2line(unsigned int shndx,
613 if (this->data_valid_ == false)
616 const Offset_to_lineno_entry lookup_key = { offset, 0, 0, 0 };
617 const std::vector<Offset_to_lineno_entry>* offsets;
618 // If we do not have reloc information, then our input is a .so or
619 // some similar data structure where all the information is held in
620 // the offset. In that case, we ignore the input shndx.
621 if (this->input_is_relobj())
622 offsets = &this->line_number_map_[shndx];
624 offsets = &this->line_number_map_[-1U];
625 if (offsets->empty())
628 typename std::vector<Offset_to_lineno_entry>::const_iterator it
629 = std::lower_bound(offsets->begin(), offsets->end(), lookup_key);
631 // If we found an exact match, great, otherwise find the last entry
632 // before the passed-in offset.
633 if (it == offsets->end() || it->offset > offset)
635 if (it == offsets->begin())
638 gold_assert(it->offset < offset);
641 // Convert the file_num + line_num into a string.
644 gold_assert(it->header_num < static_cast<int>(this->files_.size()));
645 gold_assert(it->file_num
646 < static_cast<int>(this->files_[it->header_num].size()));
647 const std::pair<int, std::string>& filename_pair
648 = this->files_[it->header_num][it->file_num];
649 const std::string& filename = filename_pair.second;
651 gold_assert(it->header_num < static_cast<int>(this->directories_.size()));
652 gold_assert(filename_pair.first
653 < static_cast<int>(this->directories_[it->header_num].size()));
654 const std::string& dirname
655 = this->directories_[it->header_num][filename_pair.first];
657 if (!dirname.empty())
666 char buffer[64]; // enough to hold a line number
667 snprintf(buffer, sizeof(buffer), "%d", it->line_num);
674 // Dwarf_line_info routines.
677 Dwarf_line_info::one_addr2line(Object* object,
678 unsigned int shndx, off_t offset)
680 if (parameters->get_size() == 32 && !parameters->is_big_endian())
681 #ifdef HAVE_TARGET_32_LITTLE
682 return Sized_dwarf_line_info<32, false>(object).addr2line(shndx, offset);
686 else if (parameters->get_size() == 32 && parameters->is_big_endian())
687 #ifdef HAVE_TARGET_32_BIG
688 return Sized_dwarf_line_info<32, true>(object).addr2line(shndx, offset);
692 else if (parameters->get_size() == 64 && !parameters->is_big_endian())
693 #ifdef HAVE_TARGET_64_LITTLE
694 return Sized_dwarf_line_info<64, false>(object).addr2line(shndx, offset);
698 else if (parameters->get_size() == 64 && parameters->is_big_endian())
699 #ifdef HAVE_TARGET_64_BIT
700 return Sized_dwarf_line_info<64, true>(object).addr2line(shndx, offset);
708 #ifdef HAVE_TARGET_32_LITTLE
710 class Sized_dwarf_line_info<32, false>;
713 #ifdef HAVE_TARGET_32_BIG
715 class Sized_dwarf_line_info<32, true>;
718 #ifdef HAVE_TARGET_64_LITTLE
720 class Sized_dwarf_line_info<64, false>;
723 #ifdef HAVE_TARGET_64_BIG
725 class Sized_dwarf_line_info<64, true>;
728 } // End namespace gold.