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 "dwarf_reader.h"
32 // Read an unsigned LEB128 number. Each byte contains 7 bits of
33 // information, plus one bit saying whether the number continues or
37 read_unsigned_LEB_128(const unsigned char* buffer
, size_t* len
)
41 unsigned int shift
= 0;
48 result
|= (static_cast<uint64_t>(byte
& 0x7f)) << shift
;
58 // Read a signed LEB128 number. These are like regular LEB128
59 // numbers, except the last byte may have a sign bit set.
62 read_signed_LEB_128(const unsigned char* buffer
, size_t* len
)
73 result
|= (static_cast<uint64_t>(byte
& 0x7f) << shift
);
78 if ((shift
< 8 * static_cast<int>(sizeof(result
))) && (byte
& 0x40))
79 result
|= -((static_cast<int64_t>(1)) << shift
);
84 } // End anonymous namespace.
89 // This is the format of a DWARF2/3 line state machine that we process
90 // opcodes using. There is no need for anything outside the lineinfo
91 // processor to know how this works.
93 struct LineStateMachine
99 unsigned int shndx
; // the section address refers to
100 bool is_stmt
; // stmt means statement.
106 ResetLineStateMachine(struct LineStateMachine
* lsm
, bool default_is_stmt
)
113 lsm
->is_stmt
= default_is_stmt
;
114 lsm
->basic_block
= false;
115 lsm
->end_sequence
= false;
118 // Read the DWARF header.
120 template<int size
, bool big_endian
>
122 Dwarf_line_info
<size
, big_endian
>::read_header_prolog(
123 const unsigned char* lineptr
)
125 uint32_t initial_length
= elfcpp::Swap
<32, big_endian
>::readval(lineptr
);
128 // In DWARF2/3, if the initial length is all 1 bits, then the offset
129 // size is 8 and we need to read the next 8 bytes for the real length.
130 if (initial_length
== 0xffffffff)
132 header_
.offset_size
= 8;
133 initial_length
= elfcpp::Swap
<64, big_endian
>::readval(lineptr
);
137 header_
.offset_size
= 4;
139 header_
.total_length
= initial_length
;
141 gold_assert(lineptr
+ header_
.total_length
<= buffer_end_
);
143 header_
.version
= elfcpp::Swap
<16, big_endian
>::readval(lineptr
);
146 if (header_
.offset_size
== 4)
147 header_
.prologue_length
= elfcpp::Swap
<32, big_endian
>::readval(lineptr
);
149 header_
.prologue_length
= elfcpp::Swap
<64, big_endian
>::readval(lineptr
);
150 lineptr
+= header_
.offset_size
;
152 header_
.min_insn_length
= *lineptr
;
155 header_
.default_is_stmt
= *lineptr
;
158 header_
.line_base
= *reinterpret_cast<const signed char*>(lineptr
);
161 header_
.line_range
= *lineptr
;
164 header_
.opcode_base
= *lineptr
;
167 header_
.std_opcode_lengths
.reserve(header_
.opcode_base
+ 1);
168 header_
.std_opcode_lengths
[0] = 0;
169 for (int i
= 1; i
< header_
.opcode_base
; i
++)
171 header_
.std_opcode_lengths
[i
] = *lineptr
;
178 // The header for a debug_line section is mildly complicated, because
179 // the line info is very tightly encoded.
181 template<int size
, bool big_endian
>
183 Dwarf_line_info
<size
, big_endian
>::read_header_tables(
184 const unsigned char* lineptr
)
186 // It is legal for the directory entry table to be empty.
192 const unsigned char* dirname
= lineptr
;
193 gold_assert(dirindex
== static_cast<int>(directories_
.size()));
194 directories_
.push_back(reinterpret_cast<const char*>(dirname
));
195 lineptr
+= directories_
.back().size() + 1;
201 // It is also legal for the file entry table to be empty.
208 const char* filename
= reinterpret_cast<const char*>(lineptr
);
209 lineptr
+= strlen(filename
) + 1;
211 uint64_t dirindex
= read_unsigned_LEB_128(lineptr
, &len
);
212 if (dirindex
>= directories_
.size())
216 read_unsigned_LEB_128(lineptr
, &len
); // mod_time
219 read_unsigned_LEB_128(lineptr
, &len
); // filelength
222 gold_assert(fileindex
== static_cast<int>(files_
.size()));
223 files_
.push_back(std::pair
<int, std::string
>(dirindex
, filename
));
232 // Process a single opcode in the .debug.line structure.
234 // Templating on size and big_endian would yield more efficient (and
235 // simpler) code, but would bloat the binary. Speed isn't important
238 template<int size
, bool big_endian
>
240 Dwarf_line_info
<size
, big_endian
>::process_one_opcode(
241 const unsigned char* start
, struct LineStateMachine
* lsm
, size_t* len
)
245 unsigned char opcode
= *start
;
249 // If the opcode is great than the opcode_base, it is a special
250 // opcode. Most line programs consist mainly of special opcodes.
251 if (opcode
>= header_
.opcode_base
)
253 opcode
-= header_
.opcode_base
;
254 const int advance_address
= ((opcode
/ header_
.line_range
)
255 * header_
.min_insn_length
);
256 lsm
->address
+= advance_address
;
258 const int advance_line
= ((opcode
% header_
.line_range
)
259 + header_
.line_base
);
260 lsm
->line_num
+= advance_line
;
261 lsm
->basic_block
= true;
266 // Otherwise, we have the regular opcodes
269 case elfcpp::DW_LNS_copy
:
270 lsm
->basic_block
= false;
274 case elfcpp::DW_LNS_advance_pc
:
276 const uint64_t advance_address
277 = read_unsigned_LEB_128(start
, &templen
);
279 lsm
->address
+= header_
.min_insn_length
* advance_address
;
283 case elfcpp::DW_LNS_advance_line
:
285 const uint64_t advance_line
= read_signed_LEB_128(start
, &templen
);
287 lsm
->line_num
+= advance_line
;
291 case elfcpp::DW_LNS_set_file
:
293 const uint64_t fileno
= read_unsigned_LEB_128(start
, &templen
);
295 lsm
->file_num
= fileno
;
299 case elfcpp::DW_LNS_set_column
:
301 const uint64_t colno
= read_unsigned_LEB_128(start
, &templen
);
303 lsm
->column_num
= colno
;
307 case elfcpp::DW_LNS_negate_stmt
:
308 lsm
->is_stmt
= !lsm
->is_stmt
;
311 case elfcpp::DW_LNS_set_basic_block
:
312 lsm
->basic_block
= true;
315 case elfcpp::DW_LNS_fixed_advance_pc
:
318 advance_address
= elfcpp::Swap
<16, big_endian
>::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
:
352 typename
Reloc_map::const_iterator it
353 = reloc_map_
.find(start
- this->buffer_
);
354 if (it
!= reloc_map_
.end())
358 (elfcpp::Swap
<size
, big_endian
>::readval(start
)
359 + it
->second
.second
);
360 lsm
->shndx
= it
->second
.first
;
364 // Every set_address should have an associated
366 this->data_valid_
= false;
370 case elfcpp::DW_LNE_define_file
:
372 const char* filename
= reinterpret_cast<const char*>(start
);
373 templen
= strlen(filename
) + 1;
376 uint64_t dirindex
= read_unsigned_LEB_128(start
, &templen
);
377 if (dirindex
>= directories_
.size())
381 read_unsigned_LEB_128(start
, &templen
); // mod_time
384 read_unsigned_LEB_128(start
, &templen
); // filelength
387 files_
.push_back(std::pair
<int, std::string
>(dirindex
,
397 // Ignore unknown opcode silently
398 for (int i
= 0; i
< header_
.std_opcode_lengths
[opcode
]; i
++)
401 read_unsigned_LEB_128(start
, &templen
);
412 // Read the debug information at LINEPTR and store it in the line
415 template<int size
, bool big_endian
>
417 Dwarf_line_info
<size
, big_endian
>::read_lines(unsigned const char* lineptr
)
419 struct LineStateMachine lsm
;
421 // LENGTHSTART is the place the length field is based on. It is the
422 // point in the header after the initial length field.
423 const unsigned char* lengthstart
= buffer_
;
425 // In 64 bit dwarf, the initial length is 12 bytes, because of the
426 // 0xffffffff at the start.
427 if (header_
.offset_size
== 8)
432 while (lineptr
< lengthstart
+ header_
.total_length
)
434 ResetLineStateMachine(&lsm
, header_
.default_is_stmt
);
435 while (!lsm
.end_sequence
)
438 bool add_line
= this->process_one_opcode(lineptr
, &lsm
, &oplength
);
441 Offset_to_lineno_entry entry
442 = { lsm
.address
, lsm
.file_num
, lsm
.line_num
};
443 line_number_map_
[lsm
.shndx
].push_back(entry
);
449 return lengthstart
+ header_
.total_length
;
452 // Looks in the symtab to see what section a symbol is in.
454 template<int size
, bool big_endian
>
456 Dwarf_line_info
<size
, big_endian
>::symbol_section(
458 typename
elfcpp::Elf_types
<size
>::Elf_Addr
* value
)
460 const int symsize
= elfcpp::Elf_sizes
<size
>::sym_size
;
461 gold_assert(this->symtab_buffer_
+ sym
* symsize
< this->symtab_buffer_end_
);
462 elfcpp::Sym
<size
, big_endian
> elfsym(this->symtab_buffer_
+ sym
* symsize
);
463 *value
= elfsym
.get_st_value();
464 return elfsym
.get_st_shndx();
467 // Read the relocations into a Reloc_map.
469 template<int size
, bool big_endian
>
471 Dwarf_line_info
<size
, big_endian
>::read_relocs()
473 if (this->symtab_buffer_
== NULL
)
476 typename
elfcpp::Elf_types
<size
>::Elf_Addr value
;
478 while ((reloc_offset
= this->track_relocs_
->next_offset()) != -1)
480 const unsigned int sym
= this->track_relocs_
->next_symndx();
481 const unsigned int shndx
= this->symbol_section(sym
, &value
);
482 this->reloc_map_
[reloc_offset
] = std::make_pair(shndx
, value
);
483 this->track_relocs_
->advance(reloc_offset
+ 1);
487 // Read the line number info.
489 template<int size
, bool big_endian
>
491 Dwarf_line_info
<size
, big_endian
>::read_line_mappings()
494 while (this->buffer_
< this->buffer_end_
)
496 const unsigned char* lineptr
= this->buffer_
;
497 lineptr
= this->read_header_prolog(lineptr
);
498 lineptr
= this->read_header_tables(lineptr
);
499 lineptr
= this->read_lines(lineptr
);
500 this->buffer_
= lineptr
;
503 // Sort the lines numbers, so addr2line can use binary search.
504 for (typename
Lineno_map::iterator it
= line_number_map_
.begin();
505 it
!= line_number_map_
.end();
507 // Each vector needs to be sorted by offset.
508 std::sort(it
->second
.begin(), it
->second
.end());
511 // Return a string for a file name and line number.
513 template<int size
, bool big_endian
>
515 Dwarf_line_info
<size
, big_endian
>::addr2line(unsigned int shndx
, off_t offset
)
517 if (this->data_valid_
== false)
520 const Offset_to_lineno_entry lookup_key
= { offset
, 0, 0 };
521 std::vector
<Offset_to_lineno_entry
>& offsets
= this->line_number_map_
[shndx
];
525 typename
std::vector
<Offset_to_lineno_entry
>::const_iterator it
526 = std::lower_bound(offsets
.begin(), offsets
.end(), lookup_key
);
528 // If we found an exact match, great, otherwise find the last entry
529 // before the passed-in offset.
530 if (it
->offset
> offset
)
532 if (it
== offsets
.begin())
535 gold_assert(it
->offset
< offset
);
538 // Convert the file_num + line_num into a string.
540 gold_assert(it
->file_num
< static_cast<int>(files_
.size()));
541 const std::pair
<int, std::string
>& filename_pair
= files_
[it
->file_num
];
542 gold_assert(filename_pair
.first
< static_cast<int>(directories_
.size()));
543 const std::string
& dirname
= directories_
[filename_pair
.first
];
544 const std::string
& filename
= filename_pair
.second
;
545 if (!dirname
.empty())
554 char buffer
[64]; // enough to hold a line number
555 snprintf(buffer
, sizeof(buffer
), "%d", it
->line_num
);
562 #ifdef HAVE_TARGET_32_LITTLE
564 class Dwarf_line_info
<32, false>;
567 #ifdef HAVE_TARGET_32_BIG
569 class Dwarf_line_info
<32, true>;
572 #ifdef HAVE_TARGET_64_LITTLE
574 class Dwarf_line_info
<64, false>;
577 #ifdef HAVE_TARGET_64_BIG
579 class Dwarf_line_info
<64, true>;
582 } // End namespace gold.