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::read_header_prolog(const unsigned char* lineptr)
123 uint32_t initial_length = elfcpp::Swap<32, big_endian>::readval(lineptr);
126 // In DWARF2/3, if the initial length is all 1 bits, then the offset
127 // size is 8 and we need to read the next 8 bytes for the real length.
128 if (initial_length == 0xffffffff)
130 header_.offset_size = 8;
131 initial_length = elfcpp::Swap<64, big_endian>::readval(lineptr);
135 header_.offset_size = 4;
137 header_.total_length = initial_length;
139 gold_assert(lineptr + header_.total_length <= buffer_end_);
141 header_.version = elfcpp::Swap<16, big_endian>::readval(lineptr);
144 if (header_.offset_size == 4)
145 header_.prologue_length = elfcpp::Swap<32, big_endian>::readval(lineptr);
147 header_.prologue_length = elfcpp::Swap<64, big_endian>::readval(lineptr);
148 lineptr += header_.offset_size;
150 header_.min_insn_length = *lineptr;
153 header_.default_is_stmt = *lineptr;
156 header_.line_base = *reinterpret_cast<const signed char*>(lineptr);
159 header_.line_range = *lineptr;
162 header_.opcode_base = *lineptr;
165 header_.std_opcode_lengths.reserve(header_.opcode_base + 1);
166 header_.std_opcode_lengths[0] = 0;
167 for (int i = 1; i < header_.opcode_base; i++)
169 header_.std_opcode_lengths[i] = *lineptr;
176 // The header for a debug_line section is mildly complicated, because
177 // the line info is very tightly encoded.
180 Dwarf_line_info::read_header_tables(const unsigned char* lineptr)
182 // It is legal for the directory entry table to be empty.
188 const unsigned char* dirname = lineptr;
189 gold_assert(dirindex == static_cast<int>(directories_.size()));
190 directories_.push_back(reinterpret_cast<const char*>(dirname));
191 lineptr += directories_.back().size() + 1;
197 // It is also legal for the file entry table to be empty.
204 const char* filename = reinterpret_cast<const char*>(lineptr);
205 lineptr += strlen(filename) + 1;
207 uint64_t dirindex = read_unsigned_LEB_128(lineptr, &len);
208 if (dirindex >= directories_.size())
212 read_unsigned_LEB_128(lineptr, &len); // mod_time
215 read_unsigned_LEB_128(lineptr, &len); // filelength
218 gold_assert(fileindex == static_cast<int>(files_.size()));
219 files_.push_back(std::pair<int, std::string>(dirindex, filename));
228 // Process a single opcode in the .debug.line structure.
230 // Templating on size and big_endian would yield more efficient (and
231 // simpler) code, but would bloat the binary. Speed isn't important
235 Dwarf_line_info::process_one_opcode(int size, bool big_endian,
236 const unsigned char* start,
237 struct LineStateMachine* lsm,
242 unsigned char opcode = *start;
246 // If the opcode is great than the opcode_base, it is a special
247 // opcode. Most line programs consist mainly of special opcodes.
248 if (opcode >= header_.opcode_base)
250 opcode -= header_.opcode_base;
251 const int advance_address = ((opcode / header_.line_range)
252 * header_.min_insn_length);
253 lsm->address += advance_address;
255 const int advance_line = ((opcode % header_.line_range)
256 + header_.line_base);
257 lsm->line_num += advance_line;
258 lsm->basic_block = true;
263 // Otherwise, we have the regular opcodes
266 case elfcpp::DW_LNS_copy:
267 lsm->basic_block = false;
271 case elfcpp::DW_LNS_advance_pc:
273 const uint64_t advance_address
274 = read_unsigned_LEB_128(start, &templen);
276 lsm->address += header_.min_insn_length * advance_address;
280 case elfcpp::DW_LNS_advance_line:
282 const uint64_t advance_line = read_signed_LEB_128(start, &templen);
284 lsm->line_num += advance_line;
288 case elfcpp::DW_LNS_set_file:
290 const uint64_t fileno = read_unsigned_LEB_128(start, &templen);
292 lsm->file_num = fileno;
296 case elfcpp::DW_LNS_set_column:
298 const uint64_t colno = read_unsigned_LEB_128(start, &templen);
300 lsm->column_num = colno;
304 case elfcpp::DW_LNS_negate_stmt:
305 lsm->is_stmt = !lsm->is_stmt;
308 case elfcpp::DW_LNS_set_basic_block:
309 lsm->basic_block = true;
312 case elfcpp::DW_LNS_fixed_advance_pc:
316 advance_address = elfcpp::Swap<16, true>::readval(start);
318 advance_address = elfcpp::Swap<16, false>::readval(start);
320 lsm->address += advance_address;
324 case elfcpp::DW_LNS_const_add_pc:
326 const int advance_address = (header_.min_insn_length
327 * ((255 - header_.opcode_base)
328 / header_.line_range));
329 lsm->address += advance_address;
333 case elfcpp::DW_LNS_extended_op:
335 const uint64_t extended_op_len
336 = read_unsigned_LEB_128(start, &templen);
338 oplen += templen + extended_op_len;
340 const unsigned char extended_op = *start;
345 case elfcpp::DW_LNE_end_sequence:
346 lsm->end_sequence = true;
350 case elfcpp::DW_LNE_set_address:
351 // FIXME: modify the address based on the reloc
352 if (size == 32 && big_endian == false)
353 lsm->address = elfcpp::Swap<32, false>::readval(start);
354 else if (size == 32 && big_endian == true)
355 lsm->address = elfcpp::Swap<32, true>::readval(start);
356 else if (size == 64 && big_endian == false)
357 lsm->address = elfcpp::Swap<64, false>::readval(start);
358 else if (size == 64 && big_endian == true)
359 lsm->address = elfcpp::Swap<64, true>::readval(start);
361 gold_assert(false); // We need to implement more cases, then.
362 // FIXME: set lsm->shndx from the reloc
366 case elfcpp::DW_LNE_define_file:
368 const char* filename = reinterpret_cast<const char*>(start);
369 templen = strlen(filename) + 1;
372 uint64_t dirindex = read_unsigned_LEB_128(start, &templen);
373 if (dirindex >= directories_.size())
377 read_unsigned_LEB_128(start, &templen); // mod_time
380 read_unsigned_LEB_128(start, &templen); // filelength
383 files_.push_back(std::pair<int, std::string>(dirindex,
393 // Ignore unknown opcode silently
394 for (int i = 0; i < header_.std_opcode_lengths[opcode]; i++)
397 read_unsigned_LEB_128(start, &templen);
408 // Read the debug information at LINEPTR and store it in the line
412 Dwarf_line_info::read_lines(int size, bool big_endian,
413 unsigned const char* lineptr)
415 struct LineStateMachine lsm;
417 // LENGTHSTART is the place the length field is based on. It is the
418 // point in the header after the initial length field.
419 const unsigned char* lengthstart = buffer_;
421 // In 64 bit dwarf, the initial length is 12 bytes, because of the
422 // 0xffffffff at the start.
423 if (header_.offset_size == 8)
428 while (lineptr < lengthstart + header_.total_length)
430 ResetLineStateMachine(&lsm, header_.default_is_stmt);
431 while (!lsm.end_sequence)
434 bool add_line = this->process_one_opcode(size, big_endian,
435 lineptr, &lsm, &oplength);
438 Offset_to_lineno_entry entry
439 = { lsm.address, lsm.file_num, lsm.line_num };
440 line_number_map_[lsm.shndx].push_back(entry);
446 return lengthstart + header_.total_length;
449 // Called after all line numbers have been read.
452 Dwarf_line_info::finalize_line_number_map()
454 for (Lineno_map::iterator it = line_number_map_.begin();
455 it != line_number_map_.end();
457 // Each vector needs to be sorted by offset.
458 sort(it->second.begin(), it->second.end());
461 // Return a string for a file name and line number.
464 Dwarf_line_info::addr2line(unsigned int shndx, off_t offset)
466 const Offset_to_lineno_entry lookup_key = { offset, 0, 0 };
467 std::vector<Offset_to_lineno_entry>& offsets = line_number_map_[shndx];
468 std::vector<Offset_to_lineno_entry>::const_iterator it
469 = std::lower_bound(offsets.begin(), offsets.end(), lookup_key);
471 // If we found an exact match, great, otherwise find the last entry
472 // before the passed-in offset.
473 if (it->offset > offset)
475 if (it == offsets.begin())
478 gold_assert(it->offset < offset);
481 // Convert the file_num + line_num into a string.
483 gold_assert(it->file_num < static_cast<int>(files_.size()));
484 const std::pair<int, std::string>& filename_pair = files_[it->file_num];
485 gold_assert(filename_pair.first < static_cast<int>(directories_.size()));
486 const std::string& dirname = directories_[filename_pair.first];
487 const std::string& filename = filename_pair.second;
488 if (!dirname.empty())
497 char buffer[64]; // enough to hold a line number
498 snprintf(buffer, sizeof(buffer), "%d", it->line_num);
505 #ifdef HAVE_TARGET_32_LITTLE
508 Dwarf_line_info::read_header_prolog<32, false>(const unsigned char* lineptr);
511 #ifdef HAVE_TARGET_32_BIG
514 Dwarf_line_info::read_header_prolog<32, true>(const unsigned char* lineptr);
517 #ifdef HAVE_TARGET_64_LITTLE
520 Dwarf_line_info::read_header_prolog<64, false>(const unsigned char* lineptr);
523 #ifdef HAVE_TARGET_64_BIG
526 Dwarf_line_info::read_header_prolog<64, true>(const unsigned char* lineptr);
529 } // End namespace gold.