1 // dwarf_reader.h -- parse dwarf2/3 debug information for gold -*- C++ -*-
3 // Copyright 2007 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 // This class is used to read the line information from the debugging
42 // section of an object file.
54 // Given a section number and an offset, returns the associated
55 // file and line-number, as a string: "file:lineno". If unable
56 // to do the mapping, returns the empty string. You must call
57 // read_line_mappings() before calling this function.
59 addr2line(unsigned int shndx, off_t offset)
60 { return do_addr2line(shndx, offset); }
62 // A helper function for a single addr2line lookup. It uses
63 // parameters() to figure out the size and endianness. This is less
64 // efficient than using the templatized size and endianness, so only
65 // call this from an un-templatized context.
67 one_addr2line(Object* object, unsigned int shndx, off_t offset);
71 do_addr2line(unsigned int shndx, off_t offset) = 0;
74 template<int size, bool big_endian>
75 class Sized_dwarf_line_info : public Dwarf_line_info
78 // Initializes a .debug_line reader for a given object file.
79 Sized_dwarf_line_info(Object* object);
83 do_addr2line(unsigned int shndx, off_t offset);
85 // Start processing line info, and populates the offset_map_.
89 // Reads the relocation section associated with .debug_line and
90 // stores relocation information in reloc_map_.
94 // Looks in the symtab to see what section a symbol is in.
96 symbol_section(unsigned int sym,
97 typename elfcpp::Elf_types<size>::Elf_Addr* value);
99 // Reads the DWARF2/3 header for this line info. Each takes as input
100 // a starting buffer position, and returns the ending position.
102 read_header_prolog(const unsigned char* lineptr);
105 read_header_tables(const unsigned char* lineptr);
107 // Reads the DWARF2/3 line information.
109 read_lines(const unsigned char* lineptr);
111 // Process a single line info opcode at START using the state
112 // machine at LSM. Return true if we should define a line using the
113 // current state of the line state machine. Place the length of the
116 process_one_opcode(const unsigned char* start,
117 struct LineStateMachine* lsm, size_t* len);
119 // Some parts of processing differ depending on whether the input
120 // was a .o file or not.
121 bool input_is_relobj();
123 // If we saw anything amiss while parsing, we set this to false.
124 // Then addr2line will always fail (rather than return possibly-
128 // A DWARF2/3 line info header. This is not the same size as in the
129 // actual file, as the one in the file may have a 32 bit or 64 bit
132 struct Dwarf_line_infoHeader
136 off_t prologue_length;
137 int min_insn_length; // insn stands for instructin
138 bool default_is_stmt; // stmt stands for statement
139 signed char line_base;
141 unsigned char opcode_base;
142 std::vector<unsigned char> std_opcode_lengths;
146 // buffer is the buffer for our line info, starting at exactly where
147 // the line info to read is.
148 const unsigned char* buffer_;
149 const unsigned char* buffer_end_;
151 // This has relocations that point into buffer.
152 Track_relocs<size, big_endian> track_relocs_;
154 // This is used to figure out what section to apply a relocation to.
155 const unsigned char* symtab_buffer_;
156 off_t symtab_buffer_size_;
158 // Holds the directories and files as we see them. We have an array
159 // of directory-lists, one for each .o file we're reading (usually
160 // there will just be one, but there may be more if input is a .so).
161 std::vector<std::vector<std::string> > directories_;
162 // The first part is an index into directories_, the second the filename.
163 std::vector<std::vector< std::pair<int, std::string> > > files_;
165 // An index into the current directories_ and files_ vectors.
166 int current_header_index_;
168 // A sorted map from offset of the relocation target to the shndx
169 // and addend for the relocation.
170 typedef std::map<typename elfcpp::Elf_types<size>::Elf_Addr,
171 std::pair<unsigned int,
172 typename elfcpp::Elf_types<size>::Elf_Swxword> >
174 Reloc_map reloc_map_;
176 // We can't do better than to keep the offsets in a sorted vector.
177 // Here, offset is the key, and file_num/line_num is the value.
178 struct Offset_to_lineno_entry
181 int header_num; // which file-list to use (i.e. which .o file are we in)
182 int file_num; // a pointer into files_
183 int line_num; // the line number in the source file
184 // Offsets are unique within a section, so that's a sufficient sort key.
185 bool operator<(const Offset_to_lineno_entry& that) const
186 { return this->offset < that.offset; }
188 // We have a vector of offset->lineno entries for every input section.
189 typedef Unordered_map<unsigned int, std::vector<Offset_to_lineno_entry> >
192 Lineno_map line_number_map_;
195 } // End namespace gold.
197 #endif // !defined(GOLD_DWARF_READER_H)