1 // dwarf_reader.h -- parse dwarf2/3 debug information for gold -*- C++ -*-
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.
23 #ifndef GOLD_DWARF_READER_H
24 #define GOLD_DWARF_READER_H
28 #include "elfcpp_swap.h"
34 struct LineStateMachine;
36 // This class is used to read the line information from the debugging
37 // section of an object file.
42 // Initializes a .debug_line reader. Buffer and buffer length point
43 // to the beginning and length of the line information to read.
44 // Reader is a ByteReader class that has the endianness set
46 Dwarf_line_info(const unsigned char* buffer, off_t buffer_length)
47 : buffer_(buffer), buffer_end_(buffer + buffer_length),
48 directories_(1), files_(1)
51 // Start processing line info, and populates the offset_map_.
52 template<int size, bool big_endian>
56 while (buffer_ < buffer_end_)
58 const unsigned char* lineptr = buffer_;
59 lineptr = this->read_header_prolog<size, big_endian>(lineptr);
60 lineptr = this->read_header_tables(lineptr);
61 lineptr = this->read_lines(size, big_endian, lineptr);
64 finalize_line_number_map();
67 // Given a section number and an offset, returns the associated
68 // file and line-number, as a string: "file:lineno". If unable
69 // to do the mapping, returns the empty string. You must call
70 // read_line_mappings() before calling this function.
72 addr2line(unsigned int shndx, off_t offset);
75 // Reads the DWARF2/3 header for this line info. Each takes as input
76 // a starting buffer position, and returns the ending position.
77 template<int size, bool big_endian>
79 read_header_prolog(const unsigned char* lineptr);
82 read_header_tables(const unsigned char* lineptr);
84 // Reads the DWARF2/3 line information.
86 read_lines(int size, bool big_endian, const unsigned char* lineptr);
88 // Process a single line info opcode at START using the state
89 // machine at LSM. Return true if we should define a line using the
90 // current state of the line state machine. Place the length of the
93 process_one_opcode(int size, bool big_endian,
94 const unsigned char* start,
95 struct LineStateMachine* lsm, size_t* len);
97 // Called after all line number have been read, to ready
98 // line_number_map_ for calls to addr2line().
100 finalize_line_number_map();
102 // A DWARF2/3 line info header. This is not the same size as in the
103 // actual file, as the one in the file may have a 32 bit or 64 bit
106 struct Dwarf_line_infoHeader
110 off_t prologue_length;
111 int min_insn_length; // insn stands for instructin
112 bool default_is_stmt; // stmt stands for statement
113 signed char line_base;
115 unsigned char opcode_base;
116 std::vector<unsigned char> std_opcode_lengths;
120 // buffer is the buffer for our line info, starting at exactly where
121 // the line info to read is.
122 const unsigned char* buffer_;
123 const unsigned char* const buffer_end_;
125 // Holds the directories and files as we see them.
126 std::vector<std::string> directories_;
127 // The first part is an index into directories_, the second the filename.
128 std::vector< std::pair<int, std::string> > files_;
130 // We can't do better than to keep the offsets in a sorted vector.
131 // Here, offset is the key, and file_num/line_num is the value.
132 struct Offset_to_lineno_entry
135 int file_num; // a pointer into files_
137 // Offsets are unique within a section, so that's a sufficient sort key.
138 bool operator<(const Offset_to_lineno_entry& that) const
139 { return this->offset < that.offset; }
141 // We have a vector of offset->lineno entries for every input section.
142 typedef Unordered_map<unsigned int, std::vector<Offset_to_lineno_entry> >
145 Lineno_map line_number_map_;
148 } // End namespace gold.
150 #endif // !defined(GOLD_DWARF_READER_H)