Commit | Line | Data |
---|---|---|
5c2c6c95 ILT |
1 | // dwarf_reader.h -- parse dwarf2/3 debug information for gold -*- C++ -*- |
2 | ||
3 | // Copyright 2007 Free Software Foundation, Inc. | |
4 | // Written by Ian Lance Taylor <iant@google.com>. | |
5 | ||
6 | // This file is part of gold. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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. | |
22 | ||
23 | #ifndef GOLD_DWARF_READER_H | |
24 | #define GOLD_DWARF_READER_H | |
25 | ||
26 | #include <vector> | |
4c50553d | 27 | #include <map> |
5c2c6c95 | 28 | |
4c50553d | 29 | #include "elfcpp.h" |
5c2c6c95 ILT |
30 | #include "elfcpp_swap.h" |
31 | #include "dwarf.h" | |
32 | ||
33 | namespace gold | |
34 | { | |
35 | ||
4c50553d ILT |
36 | template<int size, bool big_endian> |
37 | class Track_relocs; | |
5c2c6c95 ILT |
38 | struct LineStateMachine; |
39 | ||
40 | // This class is used to read the line information from the debugging | |
41 | // section of an object file. | |
42 | ||
e43872e9 | 43 | template<int size, bool big_endian> |
5c2c6c95 ILT |
44 | class Dwarf_line_info |
45 | { | |
46 | public: | |
47 | // Initializes a .debug_line reader. Buffer and buffer length point | |
48 | // to the beginning and length of the line information to read. | |
49 | // Reader is a ByteReader class that has the endianness set | |
50 | // properly. | |
4c50553d ILT |
51 | Dwarf_line_info(const unsigned char* buffer, off_t buffer_length, |
52 | Track_relocs<size, big_endian>* track_relocs, | |
53 | const unsigned char* symtab_buffer, | |
54 | off_t symtab_buffer_length) | |
55 | : data_valid_(true), | |
56 | buffer_(buffer), buffer_end_(buffer + buffer_length), | |
57 | track_relocs_(track_relocs), | |
58 | symtab_buffer_(symtab_buffer), | |
59 | symtab_buffer_end_(symtab_buffer + symtab_buffer_length), | |
5c2c6c95 ILT |
60 | directories_(1), files_(1) |
61 | { } | |
62 | ||
63 | // Start processing line info, and populates the offset_map_. | |
5c2c6c95 | 64 | void |
e43872e9 | 65 | read_line_mappings(); |
5c2c6c95 ILT |
66 | |
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. | |
71 | std::string | |
72 | addr2line(unsigned int shndx, off_t offset); | |
73 | ||
74 | private: | |
4c50553d ILT |
75 | // Reads the relocation section associated with .debug_line and |
76 | // stores relocation information in reloc_map_. | |
77 | void | |
78 | read_relocs(); | |
79 | ||
80 | // Looks in the symtab to see what section a symbol is in. | |
81 | unsigned int | |
82 | symbol_section(unsigned int sym, | |
83 | typename elfcpp::Elf_types<size>::Elf_Addr* value); | |
84 | ||
5c2c6c95 ILT |
85 | // Reads the DWARF2/3 header for this line info. Each takes as input |
86 | // a starting buffer position, and returns the ending position. | |
5c2c6c95 ILT |
87 | const unsigned char* |
88 | read_header_prolog(const unsigned char* lineptr); | |
89 | ||
90 | const unsigned char* | |
91 | read_header_tables(const unsigned char* lineptr); | |
92 | ||
93 | // Reads the DWARF2/3 line information. | |
94 | const unsigned char* | |
e43872e9 | 95 | read_lines(const unsigned char* lineptr); |
5c2c6c95 ILT |
96 | |
97 | // Process a single line info opcode at START using the state | |
98 | // machine at LSM. Return true if we should define a line using the | |
99 | // current state of the line state machine. Place the length of the | |
100 | // opcode in LEN. | |
101 | bool | |
e43872e9 | 102 | process_one_opcode(const unsigned char* start, |
5c2c6c95 ILT |
103 | struct LineStateMachine* lsm, size_t* len); |
104 | ||
4c50553d ILT |
105 | // If we saw anything amiss while parsing, we set this to false. |
106 | // Then addr2line will always fail (rather than return possibly- | |
107 | // corrupt data). | |
108 | bool data_valid_; | |
109 | ||
5c2c6c95 ILT |
110 | // A DWARF2/3 line info header. This is not the same size as in the |
111 | // actual file, as the one in the file may have a 32 bit or 64 bit | |
112 | // lengths. | |
113 | ||
114 | struct Dwarf_line_infoHeader | |
115 | { | |
116 | off_t total_length; | |
117 | int version; | |
118 | off_t prologue_length; | |
119 | int min_insn_length; // insn stands for instructin | |
120 | bool default_is_stmt; // stmt stands for statement | |
121 | signed char line_base; | |
122 | int line_range; | |
123 | unsigned char opcode_base; | |
124 | std::vector<unsigned char> std_opcode_lengths; | |
125 | int offset_size; | |
126 | } header_; | |
127 | ||
128 | // buffer is the buffer for our line info, starting at exactly where | |
129 | // the line info to read is. | |
130 | const unsigned char* buffer_; | |
131 | const unsigned char* const buffer_end_; | |
132 | ||
4c50553d ILT |
133 | // This has relocations that point into buffer. |
134 | Track_relocs<size, big_endian>* track_relocs_; | |
135 | ||
136 | // This is used to figure out what section to apply a relocation to. | |
137 | const unsigned char* const symtab_buffer_; | |
138 | const unsigned char* const symtab_buffer_end_; | |
139 | ||
5c2c6c95 ILT |
140 | // Holds the directories and files as we see them. |
141 | std::vector<std::string> directories_; | |
142 | // The first part is an index into directories_, the second the filename. | |
143 | std::vector< std::pair<int, std::string> > files_; | |
144 | ||
4c50553d ILT |
145 | // A map from offset of the relocation target to the shndx and |
146 | // addend for the relocation. | |
147 | typedef std::map<typename elfcpp::Elf_types<size>::Elf_Addr, | |
148 | std::pair<unsigned int, | |
149 | typename elfcpp::Elf_types<size>::Elf_Swxword> > | |
150 | Reloc_map; | |
151 | Reloc_map reloc_map_; | |
152 | ||
5c2c6c95 ILT |
153 | // We can't do better than to keep the offsets in a sorted vector. |
154 | // Here, offset is the key, and file_num/line_num is the value. | |
155 | struct Offset_to_lineno_entry | |
156 | { | |
157 | off_t offset; | |
158 | int file_num; // a pointer into files_ | |
159 | int line_num; | |
160 | // Offsets are unique within a section, so that's a sufficient sort key. | |
161 | bool operator<(const Offset_to_lineno_entry& that) const | |
162 | { return this->offset < that.offset; } | |
163 | }; | |
164 | // We have a vector of offset->lineno entries for every input section. | |
165 | typedef Unordered_map<unsigned int, std::vector<Offset_to_lineno_entry> > | |
166 | Lineno_map; | |
167 | ||
168 | Lineno_map line_number_map_; | |
169 | }; | |
170 | ||
171 | } // End namespace gold. | |
172 | ||
173 | #endif // !defined(GOLD_DWARF_READER_H) |