1 // dwarf_reader.h -- parse dwarf2/3 debug information for gold -*- C++ -*-
3 // Copyright 2007, 2008 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
30 #include "elfcpp_swap.h"
37 template<int size
, bool big_endian
>
39 struct LineStateMachine
;
41 // We can't do better than to keep the offsets in a sorted vector.
42 // Here, offset is the key, and file_num/line_num is the value.
43 struct Offset_to_lineno_entry
46 int header_num
; // which file-list to use (i.e. which .o file are we in)
47 int file_num
; // a pointer into files_
48 int line_num
; // the line number in the source file
49 // Offsets are unique within a section, so that's a sufficient sort key.
50 bool operator<(const Offset_to_lineno_entry
& that
) const
51 { return this->offset
< that
.offset
; }
54 // This class is used to read the line information from the debugging
55 // section of an object file.
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.
72 addr2line(unsigned int shndx
, off_t offset
)
73 { return do_addr2line(shndx
, offset
); }
75 // A helper function for a single addr2line lookup. It also keeps a
76 // cache of the last CACHE_SIZE Dwarf_line_info objects it created;
77 // set to 0 not to cache at all. The larger CACHE_SIZE is, the more
78 // chance this routine won't have to re-create a Dwarf_line_info
79 // object for its addr2line computation; such creations are slow.
80 // NOTE: Not thread-safe, so only call from one thread at a time.
82 one_addr2line(Object
* object
, unsigned int shndx
, off_t offset
,
85 // This reclaims all the memory that one_addr2line may have cached.
86 // Use this when you know you will not be calling one_addr2line again.
88 clear_addr2line_cache();
92 do_addr2line(unsigned int shndx
, off_t offset
) = 0;
95 template<int size
, bool big_endian
>
96 class Sized_dwarf_line_info
: public Dwarf_line_info
99 // Initializes a .debug_line reader for a given object file.
100 // If SHNDX is specified and non-negative, only read the debug
101 // information that pertains to the specified section.
102 Sized_dwarf_line_info(Object
* object
, off_t read_shndx
= -1U);
106 do_addr2line(unsigned int shndx
, off_t offset
);
108 // Start processing line info, and populates the offset_map_.
109 // If SHNDX is non-negative, only store debug information that
110 // pertains to the specified section.
112 read_line_mappings(Object
*, off_t shndx
);
114 // Reads the relocation section associated with .debug_line and
115 // stores relocation information in reloc_map_.
117 read_relocs(Object
*);
119 // Looks in the symtab to see what section a symbol is in.
121 symbol_section(Object
*, unsigned int sym
,
122 typename
elfcpp::Elf_types
<size
>::Elf_Addr
* value
,
125 // Reads the DWARF2/3 header for this line info. Each takes as input
126 // a starting buffer position, and returns the ending position.
128 read_header_prolog(const unsigned char* lineptr
);
131 read_header_tables(const unsigned char* lineptr
);
133 // Reads the DWARF2/3 line information. If shndx is non-negative,
134 // discard all line information that doesn't pertain to the given
137 read_lines(const unsigned char* lineptr
, off_t shndx
);
139 // Process a single line info opcode at START using the state
140 // machine at LSM. Return true if we should define a line using the
141 // current state of the line state machine. Place the length of the
144 process_one_opcode(const unsigned char* start
,
145 struct LineStateMachine
* lsm
, size_t* len
);
147 // Some parts of processing differ depending on whether the input
148 // was a .o file or not.
149 bool input_is_relobj();
151 // If we saw anything amiss while parsing, we set this to false.
152 // Then addr2line will always fail (rather than return possibly-
156 // A DWARF2/3 line info header. This is not the same size as in the
157 // actual file, as the one in the file may have a 32 bit or 64 bit
160 struct Dwarf_line_infoHeader
164 off_t prologue_length
;
165 int min_insn_length
; // insn stands for instructin
166 bool default_is_stmt
; // stmt stands for statement
167 signed char line_base
;
169 unsigned char opcode_base
;
170 std::vector
<unsigned char> std_opcode_lengths
;
174 // buffer is the buffer for our line info, starting at exactly where
175 // the line info to read is.
176 const unsigned char* buffer_
;
177 const unsigned char* buffer_end_
;
179 // This has relocations that point into buffer.
180 Track_relocs
<size
, big_endian
> track_relocs_
;
182 // This is used to figure out what section to apply a relocation to.
183 const unsigned char* symtab_buffer_
;
184 section_size_type symtab_buffer_size_
;
186 // Holds the directories and files as we see them. We have an array
187 // of directory-lists, one for each .o file we're reading (usually
188 // there will just be one, but there may be more if input is a .so).
189 std::vector
<std::vector
<std::string
> > directories_
;
190 // The first part is an index into directories_, the second the filename.
191 std::vector
<std::vector
< std::pair
<int, std::string
> > > files_
;
193 // An index into the current directories_ and files_ vectors.
194 int current_header_index_
;
196 // A sorted map from offset of the relocation target to the shndx
197 // and addend for the relocation.
198 typedef std::map
<typename
elfcpp::Elf_types
<size
>::Elf_Addr
,
199 std::pair
<unsigned int,
200 typename
elfcpp::Elf_types
<size
>::Elf_Swxword
> >
202 Reloc_map reloc_map_
;
204 // We have a vector of offset->lineno entries for every input section.
205 typedef Unordered_map
<unsigned int, std::vector
<Offset_to_lineno_entry
> >
208 Lineno_map line_number_map_
;
211 } // End namespace gold.
213 #endif // !defined(GOLD_DWARF_READER_H)