Commit | Line | Data |
---|---|---|
5c2c6c95 ILT |
1 | // dwarf_reader.h -- parse dwarf2/3 debug information for gold -*- C++ -*- |
2 | ||
4dbfafcc | 3 | // Copyright 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
5c2c6c95 ILT |
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> |
71ff8986 | 28 | #include <limits.h> |
5c2c6c95 | 29 | |
4c50553d | 30 | #include "elfcpp.h" |
5c2c6c95 ILT |
31 | #include "elfcpp_swap.h" |
32 | #include "dwarf.h" | |
24badc65 | 33 | #include "reloc.h" |
5c2c6c95 ILT |
34 | |
35 | namespace gold | |
36 | { | |
37 | ||
4c50553d ILT |
38 | template<int size, bool big_endian> |
39 | class Track_relocs; | |
5c2c6c95 ILT |
40 | struct LineStateMachine; |
41 | ||
79e052ea ILT |
42 | // We can't do better than to keep the offsets in a sorted vector. |
43 | // Here, offset is the key, and file_num/line_num is the value. | |
44 | struct Offset_to_lineno_entry | |
45 | { | |
46 | off_t offset; | |
47 | int header_num; // which file-list to use (i.e. which .o file are we in) | |
71ff8986 ILT |
48 | // A pointer into files_. |
49 | unsigned int file_num : sizeof(int) * CHAR_BIT - 1; | |
50 | // True if this was the last entry for the current offset, meaning | |
51 | // it's the line that actually applies. | |
52 | unsigned int last_line_for_offset : 1; | |
53 | // The line number in the source file. -1 to indicate end-of-function. | |
54 | int line_num; | |
55 | ||
56 | // This sorts by offsets first, and then puts the correct line to | |
57 | // report for a given offset at the beginning of the run of equal | |
58 | // offsets (so that asking for 1 line gives the best answer). This | |
59 | // is not a total ordering. | |
79e052ea | 60 | bool operator<(const Offset_to_lineno_entry& that) const |
71ff8986 ILT |
61 | { |
62 | if (this->offset != that.offset) | |
63 | return this->offset < that.offset; | |
64 | // Note the '>' which makes this sort 'true' first. | |
65 | return this->last_line_for_offset > that.last_line_for_offset; | |
66 | } | |
79e052ea ILT |
67 | }; |
68 | ||
5c2c6c95 ILT |
69 | // This class is used to read the line information from the debugging |
70 | // section of an object file. | |
71 | ||
72 | class Dwarf_line_info | |
73 | { | |
74 | public: | |
a55ce7fe ILT |
75 | Dwarf_line_info() |
76 | { } | |
77 | ||
78 | virtual | |
79 | ~Dwarf_line_info() | |
80 | { } | |
5c2c6c95 ILT |
81 | |
82 | // Given a section number and an offset, returns the associated | |
83 | // file and line-number, as a string: "file:lineno". If unable | |
84 | // to do the mapping, returns the empty string. You must call | |
71ff8986 ILT |
85 | // read_line_mappings() before calling this function. If |
86 | // 'other_lines' is non-NULL, fills that in with other line | |
87 | // numbers assigned to the same offset. | |
5c2c6c95 | 88 | std::string |
71ff8986 ILT |
89 | addr2line(unsigned int shndx, off_t offset, |
90 | std::vector<std::string>* other_lines) | |
91 | { return this->do_addr2line(shndx, offset, other_lines); } | |
a55ce7fe | 92 | |
e4e5049b CS |
93 | // A helper function for a single addr2line lookup. It also keeps a |
94 | // cache of the last CACHE_SIZE Dwarf_line_info objects it created; | |
95 | // set to 0 not to cache at all. The larger CACHE_SIZE is, the more | |
96 | // chance this routine won't have to re-create a Dwarf_line_info | |
97 | // object for its addr2line computation; such creations are slow. | |
98 | // NOTE: Not thread-safe, so only call from one thread at a time. | |
a55ce7fe | 99 | static std::string |
e4e5049b | 100 | one_addr2line(Object* object, unsigned int shndx, off_t offset, |
71ff8986 | 101 | size_t cache_size, std::vector<std::string>* other_lines); |
e4e5049b CS |
102 | |
103 | // This reclaims all the memory that one_addr2line may have cached. | |
104 | // Use this when you know you will not be calling one_addr2line again. | |
105 | static void | |
106 | clear_addr2line_cache(); | |
5c2c6c95 ILT |
107 | |
108 | private: | |
a55ce7fe | 109 | virtual std::string |
71ff8986 ILT |
110 | do_addr2line(unsigned int shndx, off_t offset, |
111 | std::vector<std::string>* other_lines) = 0; | |
a55ce7fe ILT |
112 | }; |
113 | ||
114 | template<int size, bool big_endian> | |
a18f2bd6 | 115 | class Sized_dwarf_line_info : public Dwarf_line_info |
a55ce7fe ILT |
116 | { |
117 | public: | |
118 | // Initializes a .debug_line reader for a given object file. | |
9430daf8 ILT |
119 | // If SHNDX is specified and non-negative, only read the debug |
120 | // information that pertains to the specified section. | |
75aea3d0 | 121 | Sized_dwarf_line_info(Object* object, unsigned int read_shndx = -1U); |
a55ce7fe | 122 | |
a55ce7fe ILT |
123 | private: |
124 | std::string | |
71ff8986 ILT |
125 | do_addr2line(unsigned int shndx, off_t offset, |
126 | std::vector<std::string>* other_lines); | |
127 | ||
128 | // Formats a file and line number to a string like "dirname/filename:lineno". | |
129 | std::string | |
130 | format_file_lineno(const Offset_to_lineno_entry& lineno) const; | |
a55ce7fe | 131 | |
24badc65 | 132 | // Start processing line info, and populates the offset_map_. |
9430daf8 ILT |
133 | // If SHNDX is non-negative, only store debug information that |
134 | // pertains to the specified section. | |
24badc65 | 135 | void |
75aea3d0 | 136 | read_line_mappings(Object*, unsigned int shndx); |
24badc65 | 137 | |
4c50553d ILT |
138 | // Reads the relocation section associated with .debug_line and |
139 | // stores relocation information in reloc_map_. | |
140 | void | |
d491d34e | 141 | read_relocs(Object*); |
4c50553d ILT |
142 | |
143 | // Looks in the symtab to see what section a symbol is in. | |
144 | unsigned int | |
d491d34e ILT |
145 | symbol_section(Object*, unsigned int sym, |
146 | typename elfcpp::Elf_types<size>::Elf_Addr* value, | |
147 | bool* is_ordinary); | |
4c50553d | 148 | |
5c2c6c95 ILT |
149 | // Reads the DWARF2/3 header for this line info. Each takes as input |
150 | // a starting buffer position, and returns the ending position. | |
5c2c6c95 ILT |
151 | const unsigned char* |
152 | read_header_prolog(const unsigned char* lineptr); | |
153 | ||
154 | const unsigned char* | |
155 | read_header_tables(const unsigned char* lineptr); | |
156 | ||
9430daf8 ILT |
157 | // Reads the DWARF2/3 line information. If shndx is non-negative, |
158 | // discard all line information that doesn't pertain to the given | |
159 | // section. | |
5c2c6c95 | 160 | const unsigned char* |
75aea3d0 | 161 | read_lines(const unsigned char* lineptr, unsigned int shndx); |
5c2c6c95 ILT |
162 | |
163 | // Process a single line info opcode at START using the state | |
164 | // machine at LSM. Return true if we should define a line using the | |
165 | // current state of the line state machine. Place the length of the | |
166 | // opcode in LEN. | |
167 | bool | |
e43872e9 | 168 | process_one_opcode(const unsigned char* start, |
5c2c6c95 ILT |
169 | struct LineStateMachine* lsm, size_t* len); |
170 | ||
af674d1d ILT |
171 | // Some parts of processing differ depending on whether the input |
172 | // was a .o file or not. | |
173 | bool input_is_relobj(); | |
174 | ||
4c50553d ILT |
175 | // If we saw anything amiss while parsing, we set this to false. |
176 | // Then addr2line will always fail (rather than return possibly- | |
177 | // corrupt data). | |
178 | bool data_valid_; | |
179 | ||
5c2c6c95 ILT |
180 | // A DWARF2/3 line info header. This is not the same size as in the |
181 | // actual file, as the one in the file may have a 32 bit or 64 bit | |
182 | // lengths. | |
183 | ||
184 | struct Dwarf_line_infoHeader | |
185 | { | |
186 | off_t total_length; | |
187 | int version; | |
188 | off_t prologue_length; | |
189 | int min_insn_length; // insn stands for instructin | |
190 | bool default_is_stmt; // stmt stands for statement | |
191 | signed char line_base; | |
192 | int line_range; | |
193 | unsigned char opcode_base; | |
194 | std::vector<unsigned char> std_opcode_lengths; | |
195 | int offset_size; | |
196 | } header_; | |
197 | ||
198 | // buffer is the buffer for our line info, starting at exactly where | |
199 | // the line info to read is. | |
200 | const unsigned char* buffer_; | |
24badc65 | 201 | const unsigned char* buffer_end_; |
5c2c6c95 | 202 | |
4c50553d | 203 | // This has relocations that point into buffer. |
24badc65 | 204 | Track_relocs<size, big_endian> track_relocs_; |
4dbfafcc ILT |
205 | // The type of the reloc section in track_relocs_--SHT_REL or SHT_RELA. |
206 | unsigned int track_relocs_type_; | |
4c50553d ILT |
207 | |
208 | // This is used to figure out what section to apply a relocation to. | |
24badc65 | 209 | const unsigned char* symtab_buffer_; |
8383303e | 210 | section_size_type symtab_buffer_size_; |
4c50553d | 211 | |
af674d1d ILT |
212 | // Holds the directories and files as we see them. We have an array |
213 | // of directory-lists, one for each .o file we're reading (usually | |
214 | // there will just be one, but there may be more if input is a .so). | |
215 | std::vector<std::vector<std::string> > directories_; | |
5c2c6c95 | 216 | // The first part is an index into directories_, the second the filename. |
af674d1d ILT |
217 | std::vector<std::vector< std::pair<int, std::string> > > files_; |
218 | ||
219 | // An index into the current directories_ and files_ vectors. | |
220 | int current_header_index_; | |
5c2c6c95 | 221 | |
af674d1d ILT |
222 | // A sorted map from offset of the relocation target to the shndx |
223 | // and addend for the relocation. | |
4c50553d ILT |
224 | typedef std::map<typename elfcpp::Elf_types<size>::Elf_Addr, |
225 | std::pair<unsigned int, | |
226 | typename elfcpp::Elf_types<size>::Elf_Swxword> > | |
227 | Reloc_map; | |
228 | Reloc_map reloc_map_; | |
229 | ||
5c2c6c95 ILT |
230 | // We have a vector of offset->lineno entries for every input section. |
231 | typedef Unordered_map<unsigned int, std::vector<Offset_to_lineno_entry> > | |
232 | Lineno_map; | |
233 | ||
234 | Lineno_map line_number_map_; | |
235 | }; | |
236 | ||
237 | } // End namespace gold. | |
238 | ||
239 | #endif // !defined(GOLD_DWARF_READER_H) |