| 1 | // dwarf_reader.h -- parse dwarf2/3 debug information for gold -*- C++ -*- |
| 2 | |
| 3 | // Copyright 2007, 2008 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 | |
| 29 | #include "elfcpp.h" |
| 30 | #include "elfcpp_swap.h" |
| 31 | #include "dwarf.h" |
| 32 | #include "reloc.h" |
| 33 | |
| 34 | namespace gold |
| 35 | { |
| 36 | |
| 37 | template<int size, bool big_endian> |
| 38 | class Track_relocs; |
| 39 | struct LineStateMachine; |
| 40 | |
| 41 | uint64_t |
| 42 | read_unsigned_LEB_128(const unsigned char* buffer, size_t* len); |
| 43 | |
| 44 | int64_t |
| 45 | read_signed_LEB_128(const unsigned char* buffer, size_t* len); |
| 46 | |
| 47 | // We can't do better than to keep the offsets in a sorted vector. |
| 48 | // Here, offset is the key, and file_num/line_num is the value. |
| 49 | struct Offset_to_lineno_entry |
| 50 | { |
| 51 | off_t offset; |
| 52 | int header_num; // which file-list to use (i.e. which .o file are we in) |
| 53 | int file_num; // a pointer into files_ |
| 54 | int line_num; // the line number in the source file |
| 55 | // Offsets are unique within a section, so that's a sufficient sort key. |
| 56 | bool operator<(const Offset_to_lineno_entry& that) const |
| 57 | { return this->offset < that.offset; } |
| 58 | }; |
| 59 | |
| 60 | // This class is used to read the line information from the debugging |
| 61 | // section of an object file. |
| 62 | |
| 63 | class Dwarf_line_info |
| 64 | { |
| 65 | public: |
| 66 | Dwarf_line_info() |
| 67 | { } |
| 68 | |
| 69 | virtual |
| 70 | ~Dwarf_line_info() |
| 71 | { } |
| 72 | |
| 73 | // Given a section number and an offset, returns the associated |
| 74 | // file and line-number, as a string: "file:lineno". If unable |
| 75 | // to do the mapping, returns the empty string. You must call |
| 76 | // read_line_mappings() before calling this function. |
| 77 | std::string |
| 78 | addr2line(unsigned int shndx, off_t offset) |
| 79 | { return do_addr2line(shndx, offset); } |
| 80 | |
| 81 | // A helper function for a single addr2line lookup. It also keeps a |
| 82 | // cache of the last CACHE_SIZE Dwarf_line_info objects it created; |
| 83 | // set to 0 not to cache at all. The larger CACHE_SIZE is, the more |
| 84 | // chance this routine won't have to re-create a Dwarf_line_info |
| 85 | // object for its addr2line computation; such creations are slow. |
| 86 | // NOTE: Not thread-safe, so only call from one thread at a time. |
| 87 | static std::string |
| 88 | one_addr2line(Object* object, unsigned int shndx, off_t offset, |
| 89 | size_t cache_size); |
| 90 | |
| 91 | // This reclaims all the memory that one_addr2line may have cached. |
| 92 | // Use this when you know you will not be calling one_addr2line again. |
| 93 | static void |
| 94 | clear_addr2line_cache(); |
| 95 | |
| 96 | private: |
| 97 | virtual std::string |
| 98 | do_addr2line(unsigned int shndx, off_t offset) = 0; |
| 99 | }; |
| 100 | |
| 101 | template<int size, bool big_endian> |
| 102 | class Sized_dwarf_line_info : public Dwarf_line_info |
| 103 | { |
| 104 | public: |
| 105 | // Initializes a .debug_line reader for a given object file. |
| 106 | // If SHNDX is specified and non-negative, only read the debug |
| 107 | // information that pertains to the specified section. |
| 108 | Sized_dwarf_line_info(Object* object, off_t read_shndx = -1U); |
| 109 | |
| 110 | private: |
| 111 | std::string |
| 112 | do_addr2line(unsigned int shndx, off_t offset); |
| 113 | |
| 114 | // Start processing line info, and populates the offset_map_. |
| 115 | // If SHNDX is non-negative, only store debug information that |
| 116 | // pertains to the specified section. |
| 117 | void |
| 118 | read_line_mappings(Object*, off_t shndx); |
| 119 | |
| 120 | // Reads the relocation section associated with .debug_line and |
| 121 | // stores relocation information in reloc_map_. |
| 122 | void |
| 123 | read_relocs(Object*); |
| 124 | |
| 125 | // Looks in the symtab to see what section a symbol is in. |
| 126 | unsigned int |
| 127 | symbol_section(Object*, unsigned int sym, |
| 128 | typename elfcpp::Elf_types<size>::Elf_Addr* value, |
| 129 | bool* is_ordinary); |
| 130 | |
| 131 | // Reads the DWARF2/3 header for this line info. Each takes as input |
| 132 | // a starting buffer position, and returns the ending position. |
| 133 | const unsigned char* |
| 134 | read_header_prolog(const unsigned char* lineptr); |
| 135 | |
| 136 | const unsigned char* |
| 137 | read_header_tables(const unsigned char* lineptr); |
| 138 | |
| 139 | // Reads the DWARF2/3 line information. If shndx is non-negative, |
| 140 | // discard all line information that doesn't pertain to the given |
| 141 | // section. |
| 142 | const unsigned char* |
| 143 | read_lines(const unsigned char* lineptr, off_t shndx); |
| 144 | |
| 145 | // Process a single line info opcode at START using the state |
| 146 | // machine at LSM. Return true if we should define a line using the |
| 147 | // current state of the line state machine. Place the length of the |
| 148 | // opcode in LEN. |
| 149 | bool |
| 150 | process_one_opcode(const unsigned char* start, |
| 151 | struct LineStateMachine* lsm, size_t* len); |
| 152 | |
| 153 | // Some parts of processing differ depending on whether the input |
| 154 | // was a .o file or not. |
| 155 | bool input_is_relobj(); |
| 156 | |
| 157 | // If we saw anything amiss while parsing, we set this to false. |
| 158 | // Then addr2line will always fail (rather than return possibly- |
| 159 | // corrupt data). |
| 160 | bool data_valid_; |
| 161 | |
| 162 | // A DWARF2/3 line info header. This is not the same size as in the |
| 163 | // actual file, as the one in the file may have a 32 bit or 64 bit |
| 164 | // lengths. |
| 165 | |
| 166 | struct Dwarf_line_infoHeader |
| 167 | { |
| 168 | off_t total_length; |
| 169 | int version; |
| 170 | off_t prologue_length; |
| 171 | int min_insn_length; // insn stands for instructin |
| 172 | bool default_is_stmt; // stmt stands for statement |
| 173 | signed char line_base; |
| 174 | int line_range; |
| 175 | unsigned char opcode_base; |
| 176 | std::vector<unsigned char> std_opcode_lengths; |
| 177 | int offset_size; |
| 178 | } header_; |
| 179 | |
| 180 | // buffer is the buffer for our line info, starting at exactly where |
| 181 | // the line info to read is. |
| 182 | const unsigned char* buffer_; |
| 183 | const unsigned char* buffer_end_; |
| 184 | |
| 185 | // This has relocations that point into buffer. |
| 186 | Track_relocs<size, big_endian> track_relocs_; |
| 187 | |
| 188 | // This is used to figure out what section to apply a relocation to. |
| 189 | const unsigned char* symtab_buffer_; |
| 190 | section_size_type symtab_buffer_size_; |
| 191 | |
| 192 | // Holds the directories and files as we see them. We have an array |
| 193 | // of directory-lists, one for each .o file we're reading (usually |
| 194 | // there will just be one, but there may be more if input is a .so). |
| 195 | std::vector<std::vector<std::string> > directories_; |
| 196 | // The first part is an index into directories_, the second the filename. |
| 197 | std::vector<std::vector< std::pair<int, std::string> > > files_; |
| 198 | |
| 199 | // An index into the current directories_ and files_ vectors. |
| 200 | int current_header_index_; |
| 201 | |
| 202 | // A sorted map from offset of the relocation target to the shndx |
| 203 | // and addend for the relocation. |
| 204 | typedef std::map<typename elfcpp::Elf_types<size>::Elf_Addr, |
| 205 | std::pair<unsigned int, |
| 206 | typename elfcpp::Elf_types<size>::Elf_Swxword> > |
| 207 | Reloc_map; |
| 208 | Reloc_map reloc_map_; |
| 209 | |
| 210 | // We have a vector of offset->lineno entries for every input section. |
| 211 | typedef Unordered_map<unsigned int, std::vector<Offset_to_lineno_entry> > |
| 212 | Lineno_map; |
| 213 | |
| 214 | Lineno_map line_number_map_; |
| 215 | }; |
| 216 | |
| 217 | } // End namespace gold. |
| 218 | |
| 219 | #endif // !defined(GOLD_DWARF_READER_H) |