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"
27 #include "dwarf_reader.h"
31 // Read an unsigned LEB128 number. Each byte contains 7 bits of
32 // information, plus one bit saying whether the number continues or
36 read_unsigned_LEB_128(const unsigned char* buffer, size_t* len)
40 unsigned int shift = 0;
47 result |= (static_cast<uint64_t>(byte & 0x7f)) << shift;
57 // Read a signed LEB128 number. These are like regular LEB128
58 // numbers, except the last byte may have a sign bit set.
61 read_signed_LEB_128(const unsigned char* buffer, size_t* len)
72 result |= (static_cast<uint64_t>(byte & 0x7f) << shift);
77 if ((shift < 8 * static_cast<int>(sizeof(result))) && (byte & 0x40))
78 result |= -((static_cast<int64_t>(1)) << shift);
83 } // End anonymous namespace.
88 // This is the format of a DWARF2/3 line state machine that we process
89 // opcodes using. There is no need for anything outside the lineinfo
90 // processor to know how this works.
92 struct LineStateMachine
98 unsigned int shndx; // the section address refers to
99 bool is_stmt; // stmt means statement.
105 ResetLineStateMachine(struct LineStateMachine* lsm, bool default_is_stmt)
112 lsm->is_stmt = default_is_stmt;
113 lsm->basic_block = false;
114 lsm->end_sequence = false;
117 // Read the DWARF header.
119 template<int size, bool big_endian>
121 Dwarf_line_info<size, big_endian>::read_header_prolog(
122 const unsigned char* lineptr)
124 uint32_t initial_length = elfcpp::Swap<32, big_endian>::readval(lineptr);
127 // In DWARF2/3, if the initial length is all 1 bits, then the offset
128 // size is 8 and we need to read the next 8 bytes for the real length.
129 if (initial_length == 0xffffffff)
131 header_.offset_size = 8;
132 initial_length = elfcpp::Swap<64, big_endian>::readval(lineptr);
136 header_.offset_size = 4;
138 header_.total_length = initial_length;
140 gold_assert(lineptr + header_.total_length <= buffer_end_);
142 header_.version = elfcpp::Swap<16, big_endian>::readval(lineptr);
145 if (header_.offset_size == 4)
146 header_.prologue_length = elfcpp::Swap<32, big_endian>::readval(lineptr);
148 header_.prologue_length = elfcpp::Swap<64, big_endian>::readval(lineptr);
149 lineptr += header_.offset_size;
151 header_.min_insn_length = *lineptr;
154 header_.default_is_stmt = *lineptr;
157 header_.line_base = *reinterpret_cast<const signed char*>(lineptr);
160 header_.line_range = *lineptr;
163 header_.opcode_base = *lineptr;
166 header_.std_opcode_lengths.reserve(header_.opcode_base + 1);
167 header_.std_opcode_lengths[0] = 0;
168 for (int i = 1; i < header_.opcode_base; i++)
170 header_.std_opcode_lengths[i] = *lineptr;
177 // The header for a debug_line section is mildly complicated, because
178 // the line info is very tightly encoded.
180 template<int size, bool big_endian>
182 Dwarf_line_info<size, big_endian>::read_header_tables(
183 const unsigned char* lineptr)
185 // It is legal for the directory entry table to be empty.
191 const unsigned char* dirname = lineptr;
192 gold_assert(dirindex == static_cast<int>(directories_.size()));
193 directories_.push_back(reinterpret_cast<const char*>(dirname));
194 lineptr += directories_.back().size() + 1;
200 // It is also legal for the file entry table to be empty.
207 const char* filename = reinterpret_cast<const char*>(lineptr);
208 lineptr += strlen(filename) + 1;
210 uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
211 if (dirindex >= directories_.size())
215 read_unsigned_LEB_128(lineptr, &len); // mod_time
218 read_unsigned_LEB_128(lineptr, &len); // filelength
221 gold_assert(fileindex == static_cast<int>(files_.size()));
222 files_.push_back(std::pair<int, std::string>(dirindex, filename));
231 // Process a single opcode in the .debug.line structure.
233 // Templating on size and big_endian would yield more efficient (and
234 // simpler) code, but would bloat the binary. Speed isn't important
237 template<int size, bool big_endian>
239 Dwarf_line_info<size, big_endian>::process_one_opcode(
240 const unsigned char* start, struct LineStateMachine* lsm, size_t* len)
244 unsigned char opcode = *start;
248 // If the opcode is great than the opcode_base, it is a special
249 // opcode. Most line programs consist mainly of special opcodes.
250 if (opcode >= header_.opcode_base)
252 opcode -= header_.opcode_base;
253 const int advance_address = ((opcode / header_.line_range)
254 * header_.min_insn_length);
255 lsm->address += advance_address;
257 const int advance_line = ((opcode % header_.line_range)
258 + header_.line_base);
259 lsm->line_num += advance_line;
260 lsm->basic_block = true;
265 // Otherwise, we have the regular opcodes
268 case elfcpp::DW_LNS_copy:
269 lsm->basic_block = false;
273 case elfcpp::DW_LNS_advance_pc:
275 const uint64_t advance_address
276 = read_unsigned_LEB_128(start, &templen);
278 lsm->address += header_.min_insn_length * advance_address;
282 case elfcpp::DW_LNS_advance_line:
284 const uint64_t advance_line = read_signed_LEB_128(start, &templen);
286 lsm->line_num += advance_line;
290 case elfcpp::DW_LNS_set_file:
292 const uint64_t fileno = read_unsigned_LEB_128(start, &templen);
294 lsm->file_num = fileno;
298 case elfcpp::DW_LNS_set_column:
300 const uint64_t colno = read_unsigned_LEB_128(start, &templen);
302 lsm->column_num = colno;
306 case elfcpp::DW_LNS_negate_stmt:
307 lsm->is_stmt = !lsm->is_stmt;
310 case elfcpp::DW_LNS_set_basic_block:
311 lsm->basic_block = true;
314 case elfcpp::DW_LNS_fixed_advance_pc:
317 advance_address = elfcpp::Swap<16, big_endian>::readval(start);
319 lsm->address += advance_address;
323 case elfcpp::DW_LNS_const_add_pc:
325 const int advance_address = (header_.min_insn_length
326 * ((255 - header_.opcode_base)
327 / header_.line_range));
328 lsm->address += advance_address;
332 case elfcpp::DW_LNS_extended_op:
334 const uint64_t extended_op_len
335 = read_unsigned_LEB_128(start, &templen);
337 oplen += templen + extended_op_len;
339 const unsigned char extended_op = *start;
344 case elfcpp::DW_LNE_end_sequence:
345 lsm->end_sequence = true;
349 case elfcpp::DW_LNE_set_address:
350 // FIXME: modify the address based on the reloc
351 lsm->address = elfcpp::Swap<size, big_endian>::readval(start);
352 // FIXME: set lsm->shndx from the reloc
356 case elfcpp::DW_LNE_define_file:
358 const char* filename = reinterpret_cast<const char*>(start);
359 templen = strlen(filename) + 1;
362 uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
363 if (dirindex >= directories_.size())
367 read_unsigned_LEB_128(start, &templen); // mod_time
370 read_unsigned_LEB_128(start, &templen); // filelength
373 files_.push_back(std::pair<int, std::string>(dirindex,
383 // Ignore unknown opcode silently
384 for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++)
387 read_unsigned_LEB_128(start, &templen);
398 // Read the debug information at LINEPTR and store it in the line
401 template<int size, bool big_endian>
403 Dwarf_line_info<size, big_endian>::read_lines(unsigned const char* lineptr)
405 struct LineStateMachine lsm;
407 // LENGTHSTART is the place the length field is based on. It is the
408 // point in the header after the initial length field.
409 const unsigned char* lengthstart = buffer_;
411 // In 64 bit dwarf, the initial length is 12 bytes, because of the
412 // 0xffffffff at the start.
413 if (header_.offset_size == 8)
418 while (lineptr < lengthstart + header_.total_length)
420 ResetLineStateMachine(&lsm, header_.default_is_stmt);
421 while (!lsm.end_sequence)
424 bool add_line = this->process_one_opcode(lineptr, &lsm, &oplength);
427 Offset_to_lineno_entry entry
428 = { lsm.address, lsm.file_num, lsm.line_num };
429 line_number_map_[lsm.shndx].push_back(entry);
435 return lengthstart + header_.total_length;
438 template<int size, bool big_endian>
440 Dwarf_line_info<size, big_endian>::read_line_mappings()
442 while (buffer_ < buffer_end_)
444 const unsigned char* lineptr = buffer_;
445 lineptr = this->read_header_prolog(lineptr);
446 lineptr = this->read_header_tables(lineptr);
447 lineptr = this->read_lines(lineptr);
451 // Sort the lines numbers, so addr2line can use binary search.
452 for (typename Lineno_map::iterator it = line_number_map_.begin();
453 it != line_number_map_.end();
455 // Each vector needs to be sorted by offset.
456 sort(it->second.begin(), it->second.end());
459 // Return a string for a file name and line number.
461 template<int size, bool big_endian>
463 Dwarf_line_info<size, big_endian>::addr2line(unsigned int shndx, off_t offset)
465 const Offset_to_lineno_entry lookup_key = { offset, 0, 0 };
466 std::vector<Offset_to_lineno_entry>& offsets = line_number_map_[shndx];
467 typename std::vector<Offset_to_lineno_entry>::const_iterator it
468 = std::lower_bound(offsets.begin(), offsets.end(), lookup_key);
470 // If we found an exact match, great, otherwise find the last entry
471 // before the passed-in offset.
472 if (it->offset > offset)
474 if (it == offsets.begin())
477 gold_assert(it->offset < offset);
480 // Convert the file_num + line_num into a string.
482 gold_assert(it->file_num < static_cast<int>(files_.size()));
483 const std::pair<int, std::string>& filename_pair = files_[it->file_num];
484 gold_assert(filename_pair.first < static_cast<int>(directories_.size()));
485 const std::string& dirname = directories_[filename_pair.first];
486 const std::string& filename = filename_pair.second;
487 if (!dirname.empty())
496 char buffer[64]; // enough to hold a line number
497 snprintf(buffer, sizeof(buffer), "%d", it->line_num);
504 #ifdef HAVE_TARGET_32_LITTLE
506 class Dwarf_line_info<32, false>;
509 #ifdef HAVE_TARGET_32_BIG
511 class Dwarf_line_info<32, true>;
514 #ifdef HAVE_TARGET_64_LITTLE
516 class Dwarf_line_info<64, false>;
519 #ifdef HAVE_TARGET_64_BIG
521 class Dwarf_line_info<64, true>;
524 } // End namespace gold.