* elf32-spu.c (build_stub): Fix malloc under-allocation.
[binutils.git] / gold / dwarf_reader.h
blob3f92dd3eea2dcd7ede989e79e66a6b33f86fa94c
1 // dwarf_reader.h -- parse dwarf2/3 debug information for gold -*- C++ -*-
3 // Copyright 2007, 2008, 2009, 2010 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
26 #include <vector>
27 #include <map>
28 #include <limits.h>
30 #include "elfcpp.h"
31 #include "elfcpp_swap.h"
32 #include "dwarf.h"
33 #include "reloc.h"
35 namespace gold
38 template<int size, bool big_endian>
39 class Track_relocs;
40 struct LineStateMachine;
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
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;
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
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;
69 // This class is used to read the line information from the debugging
70 // section of an object file.
72 class Dwarf_line_info
74 public:
75 Dwarf_line_info()
76 { }
78 virtual
79 ~Dwarf_line_info()
80 { }
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); }
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);
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();
108 private:
109 virtual std::string
110 do_addr2line(unsigned int shndx, off_t offset,
111 std::vector<std::string>* other_lines) = 0;
114 template<int size, bool big_endian>
115 class Sized_dwarf_line_info : public Dwarf_line_info
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);
123 private:
124 std::string
125 do_addr2line(unsigned int shndx, off_t offset,
126 std::vector<std::string>* other_lines);
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;
132 // Start processing line info, and populates the offset_map_.
133 // If SHNDX is non-negative, only store debug information that
134 // pertains to the specified section.
135 void
136 read_line_mappings(Object*, unsigned int shndx);
138 // Reads the relocation section associated with .debug_line and
139 // stores relocation information in reloc_map_.
140 void
141 read_relocs(Object*);
143 // Looks in the symtab to see what section a symbol is in.
144 unsigned int
145 symbol_section(Object*, unsigned int sym,
146 typename elfcpp::Elf_types<size>::Elf_Addr* value,
147 bool* is_ordinary);
149 // Reads the DWARF2/3 header for this line info. Each takes as input
150 // a starting buffer position, and returns the ending position.
151 const unsigned char*
152 read_header_prolog(const unsigned char* lineptr);
154 const unsigned char*
155 read_header_tables(const unsigned char* lineptr);
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.
160 const unsigned char*
161 read_lines(const unsigned char* lineptr, unsigned int shndx);
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
168 process_one_opcode(const unsigned char* start,
169 struct LineStateMachine* lsm, size_t* len);
171 // Some parts of processing differ depending on whether the input
172 // was a .o file or not.
173 bool input_is_relobj();
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_;
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.
184 struct Dwarf_line_infoHeader
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_;
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_;
201 const unsigned char* buffer_end_;
203 // This has relocations that point into buffer.
204 Track_relocs<size, big_endian> track_relocs_;
205 // The type of the reloc section in track_relocs_--SHT_REL or SHT_RELA.
206 unsigned int track_relocs_type_;
208 // This is used to figure out what section to apply a relocation to.
209 const unsigned char* symtab_buffer_;
210 section_size_type symtab_buffer_size_;
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_;
216 // The first part is an index into directories_, the second the filename.
217 std::vector<std::vector< std::pair<int, std::string> > > files_;
219 // An index into the current directories_ and files_ vectors.
220 int current_header_index_;
222 // A sorted map from offset of the relocation target to the shndx
223 // and addend for the relocation.
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_;
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;
234 Lineno_map line_number_map_;
237 } // End namespace gold.
239 #endif // !defined(GOLD_DWARF_READER_H)