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" | |
24badc65 | 32 | #include "reloc.h" |
5c2c6c95 ILT |
33 | |
34 | namespace gold | |
35 | { | |
36 | ||
4c50553d ILT |
37 | template<int size, bool big_endian> |
38 | class Track_relocs; | |
5c2c6c95 ILT |
39 | struct LineStateMachine; |
40 | ||
41 | // This class is used to read the line information from the debugging | |
42 | // section of an object file. | |
43 | ||
e43872e9 | 44 | template<int size, bool big_endian> |
5c2c6c95 ILT |
45 | class Dwarf_line_info |
46 | { | |
47 | public: | |
24badc65 | 48 | // Initializes a .debug_line reader for a given object file. |
c261a0be | 49 | Dwarf_line_info(Object* object); |
5c2c6c95 ILT |
50 | |
51 | // Given a section number and an offset, returns the associated | |
52 | // file and line-number, as a string: "file:lineno". If unable | |
53 | // to do the mapping, returns the empty string. You must call | |
54 | // read_line_mappings() before calling this function. | |
55 | std::string | |
56 | addr2line(unsigned int shndx, off_t offset); | |
57 | ||
58 | private: | |
24badc65 ILT |
59 | // Start processing line info, and populates the offset_map_. |
60 | void | |
61 | read_line_mappings(); | |
62 | ||
4c50553d ILT |
63 | // Reads the relocation section associated with .debug_line and |
64 | // stores relocation information in reloc_map_. | |
65 | void | |
66 | read_relocs(); | |
67 | ||
68 | // Looks in the symtab to see what section a symbol is in. | |
69 | unsigned int | |
70 | symbol_section(unsigned int sym, | |
71 | typename elfcpp::Elf_types<size>::Elf_Addr* value); | |
72 | ||
5c2c6c95 ILT |
73 | // Reads the DWARF2/3 header for this line info. Each takes as input |
74 | // a starting buffer position, and returns the ending position. | |
5c2c6c95 ILT |
75 | const unsigned char* |
76 | read_header_prolog(const unsigned char* lineptr); | |
77 | ||
78 | const unsigned char* | |
79 | read_header_tables(const unsigned char* lineptr); | |
80 | ||
81 | // Reads the DWARF2/3 line information. | |
82 | const unsigned char* | |
e43872e9 | 83 | read_lines(const unsigned char* lineptr); |
5c2c6c95 ILT |
84 | |
85 | // Process a single line info opcode at START using the state | |
86 | // machine at LSM. Return true if we should define a line using the | |
87 | // current state of the line state machine. Place the length of the | |
88 | // opcode in LEN. | |
89 | bool | |
e43872e9 | 90 | process_one_opcode(const unsigned char* start, |
5c2c6c95 ILT |
91 | struct LineStateMachine* lsm, size_t* len); |
92 | ||
af674d1d ILT |
93 | // Some parts of processing differ depending on whether the input |
94 | // was a .o file or not. | |
95 | bool input_is_relobj(); | |
96 | ||
4c50553d ILT |
97 | // If we saw anything amiss while parsing, we set this to false. |
98 | // Then addr2line will always fail (rather than return possibly- | |
99 | // corrupt data). | |
100 | bool data_valid_; | |
101 | ||
5c2c6c95 ILT |
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 | |
104 | // lengths. | |
105 | ||
106 | struct Dwarf_line_infoHeader | |
107 | { | |
108 | off_t total_length; | |
109 | int version; | |
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; | |
114 | int line_range; | |
115 | unsigned char opcode_base; | |
116 | std::vector<unsigned char> std_opcode_lengths; | |
117 | int offset_size; | |
118 | } header_; | |
119 | ||
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_; | |
24badc65 | 123 | const unsigned char* buffer_end_; |
5c2c6c95 | 124 | |
4c50553d | 125 | // This has relocations that point into buffer. |
24badc65 | 126 | Track_relocs<size, big_endian> track_relocs_; |
4c50553d ILT |
127 | |
128 | // This is used to figure out what section to apply a relocation to. | |
24badc65 | 129 | const unsigned char* symtab_buffer_; |
af674d1d | 130 | off_t symtab_buffer_size_; |
4c50553d | 131 | |
af674d1d ILT |
132 | // Holds the directories and files as we see them. We have an array |
133 | // of directory-lists, one for each .o file we're reading (usually | |
134 | // there will just be one, but there may be more if input is a .so). | |
135 | std::vector<std::vector<std::string> > directories_; | |
5c2c6c95 | 136 | // The first part is an index into directories_, the second the filename. |
af674d1d ILT |
137 | std::vector<std::vector< std::pair<int, std::string> > > files_; |
138 | ||
139 | // An index into the current directories_ and files_ vectors. | |
140 | int current_header_index_; | |
5c2c6c95 | 141 | |
af674d1d ILT |
142 | // A sorted map from offset of the relocation target to the shndx |
143 | // and addend for the relocation. | |
4c50553d ILT |
144 | typedef std::map<typename elfcpp::Elf_types<size>::Elf_Addr, |
145 | std::pair<unsigned int, | |
146 | typename elfcpp::Elf_types<size>::Elf_Swxword> > | |
147 | Reloc_map; | |
148 | Reloc_map reloc_map_; | |
149 | ||
5c2c6c95 ILT |
150 | // We can't do better than to keep the offsets in a sorted vector. |
151 | // Here, offset is the key, and file_num/line_num is the value. | |
152 | struct Offset_to_lineno_entry | |
153 | { | |
154 | off_t offset; | |
af674d1d | 155 | int header_num; // which file-list to use (i.e. which .o file are we in) |
5c2c6c95 | 156 | int file_num; // a pointer into files_ |
af674d1d | 157 | int line_num; // the line number in the source file |
5c2c6c95 ILT |
158 | // Offsets are unique within a section, so that's a sufficient sort key. |
159 | bool operator<(const Offset_to_lineno_entry& that) const | |
160 | { return this->offset < that.offset; } | |
161 | }; | |
162 | // We have a vector of offset->lineno entries for every input section. | |
163 | typedef Unordered_map<unsigned int, std::vector<Offset_to_lineno_entry> > | |
164 | Lineno_map; | |
165 | ||
166 | Lineno_map line_number_map_; | |
167 | }; | |
168 | ||
169 | } // End namespace gold. | |
170 | ||
171 | #endif // !defined(GOLD_DWARF_READER_H) |