1 |
27 |
khays |
// dwarf_reader.h -- parse dwarf2/3 debug information for gold -*- C++ -*-
|
2 |
|
|
|
3 |
|
|
// Copyright 2007, 2008, 2009, 2010 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>
|
27 |
|
|
#include <map>
|
28 |
|
|
#include <limits.h>
|
29 |
|
|
|
30 |
|
|
#include "elfcpp.h"
|
31 |
|
|
#include "elfcpp_swap.h"
|
32 |
|
|
#include "dwarf.h"
|
33 |
|
|
#include "reloc.h"
|
34 |
|
|
|
35 |
|
|
namespace gold
|
36 |
|
|
{
|
37 |
|
|
|
38 |
|
|
template<int size, bool big_endian>
|
39 |
|
|
class Track_relocs;
|
40 |
|
|
struct LineStateMachine;
|
41 |
|
|
|
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)
|
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.
|
60 |
|
|
bool operator<(const Offset_to_lineno_entry& that) const
|
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 |
|
|
}
|
67 |
|
|
};
|
68 |
|
|
|
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:
|
75 |
|
|
Dwarf_line_info()
|
76 |
|
|
{ }
|
77 |
|
|
|
78 |
|
|
virtual
|
79 |
|
|
~Dwarf_line_info()
|
80 |
|
|
{ }
|
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
|
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.
|
88 |
|
|
std::string
|
89 |
|
|
addr2line(unsigned int shndx, off_t offset,
|
90 |
|
|
std::vector<std::string>* other_lines)
|
91 |
|
|
{ return this->do_addr2line(shndx, offset, other_lines); }
|
92 |
|
|
|
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.
|
99 |
|
|
static std::string
|
100 |
|
|
one_addr2line(Object* object, unsigned int shndx, off_t offset,
|
101 |
|
|
size_t cache_size, std::vector<std::string>* other_lines);
|
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();
|
107 |
|
|
|
108 |
|
|
private:
|
109 |
|
|
virtual std::string
|
110 |
|
|
do_addr2line(unsigned int shndx, off_t offset,
|
111 |
|
|
std::vector<std::string>* other_lines) = 0;
|
112 |
|
|
};
|
113 |
|
|
|
114 |
|
|
template<int size, bool big_endian>
|
115 |
|
|
class Sized_dwarf_line_info : public Dwarf_line_info
|
116 |
|
|
{
|
117 |
|
|
public:
|
118 |
|
|
// Initializes a .debug_line reader for a given object file.
|
119 |
|
|
// If SHNDX is specified and non-negative, only read the debug
|
120 |
|
|
// information that pertains to the specified section.
|
121 |
|
|
Sized_dwarf_line_info(Object* object, unsigned int read_shndx = -1U);
|
122 |
|
|
|
123 |
166 |
khays |
virtual
|
124 |
|
|
~Sized_dwarf_line_info()
|
125 |
|
|
{
|
126 |
|
|
if (this->buffer_start_ != NULL)
|
127 |
|
|
delete[] this->buffer_start_;
|
128 |
|
|
}
|
129 |
|
|
|
130 |
27 |
khays |
private:
|
131 |
|
|
std::string
|
132 |
|
|
do_addr2line(unsigned int shndx, off_t offset,
|
133 |
|
|
std::vector<std::string>* other_lines);
|
134 |
|
|
|
135 |
|
|
// Formats a file and line number to a string like "dirname/filename:lineno".
|
136 |
|
|
std::string
|
137 |
|
|
format_file_lineno(const Offset_to_lineno_entry& lineno) const;
|
138 |
|
|
|
139 |
|
|
// Start processing line info, and populates the offset_map_.
|
140 |
|
|
// If SHNDX is non-negative, only store debug information that
|
141 |
|
|
// pertains to the specified section.
|
142 |
|
|
void
|
143 |
|
|
read_line_mappings(Object*, unsigned int shndx);
|
144 |
|
|
|
145 |
|
|
// Reads the relocation section associated with .debug_line and
|
146 |
|
|
// stores relocation information in reloc_map_.
|
147 |
|
|
void
|
148 |
|
|
read_relocs(Object*);
|
149 |
|
|
|
150 |
|
|
// Looks in the symtab to see what section a symbol is in.
|
151 |
|
|
unsigned int
|
152 |
|
|
symbol_section(Object*, unsigned int sym,
|
153 |
|
|
typename elfcpp::Elf_types<size>::Elf_Addr* value,
|
154 |
|
|
bool* is_ordinary);
|
155 |
|
|
|
156 |
|
|
// Reads the DWARF2/3 header for this line info. Each takes as input
|
157 |
|
|
// a starting buffer position, and returns the ending position.
|
158 |
|
|
const unsigned char*
|
159 |
|
|
read_header_prolog(const unsigned char* lineptr);
|
160 |
|
|
|
161 |
|
|
const unsigned char*
|
162 |
|
|
read_header_tables(const unsigned char* lineptr);
|
163 |
|
|
|
164 |
|
|
// Reads the DWARF2/3 line information. If shndx is non-negative,
|
165 |
|
|
// discard all line information that doesn't pertain to the given
|
166 |
|
|
// section.
|
167 |
|
|
const unsigned char*
|
168 |
|
|
read_lines(const unsigned char* lineptr, unsigned int shndx);
|
169 |
|
|
|
170 |
|
|
// Process a single line info opcode at START using the state
|
171 |
|
|
// machine at LSM. Return true if we should define a line using the
|
172 |
|
|
// current state of the line state machine. Place the length of the
|
173 |
|
|
// opcode in LEN.
|
174 |
|
|
bool
|
175 |
|
|
process_one_opcode(const unsigned char* start,
|
176 |
|
|
struct LineStateMachine* lsm, size_t* len);
|
177 |
|
|
|
178 |
|
|
// Some parts of processing differ depending on whether the input
|
179 |
|
|
// was a .o file or not.
|
180 |
|
|
bool input_is_relobj();
|
181 |
|
|
|
182 |
|
|
// If we saw anything amiss while parsing, we set this to false.
|
183 |
|
|
// Then addr2line will always fail (rather than return possibly-
|
184 |
|
|
// corrupt data).
|
185 |
|
|
bool data_valid_;
|
186 |
|
|
|
187 |
|
|
// A DWARF2/3 line info header. This is not the same size as in the
|
188 |
|
|
// actual file, as the one in the file may have a 32 bit or 64 bit
|
189 |
|
|
// lengths.
|
190 |
|
|
|
191 |
|
|
struct Dwarf_line_infoHeader
|
192 |
|
|
{
|
193 |
|
|
off_t total_length;
|
194 |
|
|
int version;
|
195 |
|
|
off_t prologue_length;
|
196 |
|
|
int min_insn_length; // insn stands for instructin
|
197 |
|
|
bool default_is_stmt; // stmt stands for statement
|
198 |
|
|
signed char line_base;
|
199 |
|
|
int line_range;
|
200 |
|
|
unsigned char opcode_base;
|
201 |
|
|
std::vector<unsigned char> std_opcode_lengths;
|
202 |
|
|
int offset_size;
|
203 |
|
|
} header_;
|
204 |
|
|
|
205 |
|
|
// buffer is the buffer for our line info, starting at exactly where
|
206 |
|
|
// the line info to read is.
|
207 |
|
|
const unsigned char* buffer_;
|
208 |
|
|
const unsigned char* buffer_end_;
|
209 |
166 |
khays |
// If the buffer was allocated temporarily, and therefore must be
|
210 |
|
|
// deallocated in the dtor, this contains a pointer to the start
|
211 |
|
|
// of the buffer.
|
212 |
|
|
const unsigned char* buffer_start_;
|
213 |
27 |
khays |
|
214 |
|
|
// This has relocations that point into buffer.
|
215 |
|
|
Track_relocs<size, big_endian> track_relocs_;
|
216 |
|
|
// The type of the reloc section in track_relocs_--SHT_REL or SHT_RELA.
|
217 |
|
|
unsigned int track_relocs_type_;
|
218 |
|
|
|
219 |
|
|
// This is used to figure out what section to apply a relocation to.
|
220 |
|
|
const unsigned char* symtab_buffer_;
|
221 |
|
|
section_size_type symtab_buffer_size_;
|
222 |
|
|
|
223 |
|
|
// Holds the directories and files as we see them. We have an array
|
224 |
|
|
// of directory-lists, one for each .o file we're reading (usually
|
225 |
|
|
// there will just be one, but there may be more if input is a .so).
|
226 |
|
|
std::vector<std::vector<std::string> > directories_;
|
227 |
|
|
// The first part is an index into directories_, the second the filename.
|
228 |
|
|
std::vector<std::vector< std::pair<int, std::string> > > files_;
|
229 |
|
|
|
230 |
|
|
// An index into the current directories_ and files_ vectors.
|
231 |
|
|
int current_header_index_;
|
232 |
|
|
|
233 |
|
|
// A sorted map from offset of the relocation target to the shndx
|
234 |
|
|
// and addend for the relocation.
|
235 |
|
|
typedef std::map<typename elfcpp::Elf_types<size>::Elf_Addr,
|
236 |
|
|
std::pair<unsigned int,
|
237 |
|
|
typename elfcpp::Elf_types<size>::Elf_Swxword> >
|
238 |
|
|
Reloc_map;
|
239 |
|
|
Reloc_map reloc_map_;
|
240 |
|
|
|
241 |
|
|
// We have a vector of offset->lineno entries for every input section.
|
242 |
|
|
typedef Unordered_map<unsigned int, std::vector<Offset_to_lineno_entry> >
|
243 |
|
|
Lineno_map;
|
244 |
|
|
|
245 |
|
|
Lineno_map line_number_map_;
|
246 |
|
|
};
|
247 |
|
|
|
248 |
|
|
} // End namespace gold.
|
249 |
|
|
|
250 |
|
|
#endif // !defined(GOLD_DWARF_READER_H)
|