a01634238ab50f76ad72f601a20f7f9be2483c25
[platform/upstream/binutils.git] / gold / dwarf_reader.h
1 // dwarf_reader.h -- parse dwarf2/3 debug information for gold  -*- C++ -*-
2
3 // Copyright 2007 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
28 #include "elfcpp_swap.h"
29 #include "dwarf.h"
30
31 namespace gold
32 {
33
34 struct LineStateMachine;
35
36 // This class is used to read the line information from the debugging
37 // section of an object file.
38
39 template<int size, bool big_endian>
40 class Dwarf_line_info
41 {
42  public:
43   // Initializes a .debug_line reader. Buffer and buffer length point
44   // to the beginning and length of the line information to read.
45   // Reader is a ByteReader class that has the endianness set
46   // properly.
47   Dwarf_line_info(const unsigned char* buffer, off_t buffer_length)
48     : buffer_(buffer), buffer_end_(buffer + buffer_length),
49       directories_(1), files_(1)
50   { }
51
52   // Start processing line info, and populates the offset_map_.
53   void
54   read_line_mappings();
55
56   // Given a section number and an offset, returns the associated
57   // file and line-number, as a string: "file:lineno".  If unable
58   // to do the mapping, returns the empty string.  You must call
59   // read_line_mappings() before calling this function.
60   std::string
61   addr2line(unsigned int shndx, off_t offset);
62
63  private:
64   // Reads the DWARF2/3 header for this line info.  Each takes as input
65   // a starting buffer position, and returns the ending position.
66   const unsigned char*
67   read_header_prolog(const unsigned char* lineptr);
68
69   const unsigned char*
70   read_header_tables(const unsigned char* lineptr);
71
72   // Reads the DWARF2/3 line information.
73   const unsigned char*
74   read_lines(const unsigned char* lineptr);
75
76   // Process a single line info opcode at START using the state
77   // machine at LSM.  Return true if we should define a line using the
78   // current state of the line state machine.  Place the length of the
79   // opcode in LEN.
80   bool
81   process_one_opcode(const unsigned char* start,
82                      struct LineStateMachine* lsm, size_t* len);
83
84   // A DWARF2/3 line info header.  This is not the same size as in the
85   // actual file, as the one in the file may have a 32 bit or 64 bit
86   // lengths.
87
88   struct Dwarf_line_infoHeader
89   {
90     off_t total_length;
91     int version;
92     off_t prologue_length;
93     int min_insn_length; // insn stands for instructin
94     bool default_is_stmt; // stmt stands for statement
95     signed char line_base;
96     int line_range;
97     unsigned char opcode_base;
98     std::vector<unsigned char> std_opcode_lengths;
99     int offset_size;
100   } header_;
101
102   // buffer is the buffer for our line info, starting at exactly where
103   // the line info to read is.
104   const unsigned char* buffer_;
105   const unsigned char* const buffer_end_;
106
107   // Holds the directories and files as we see them.
108   std::vector<std::string> directories_;
109   // The first part is an index into directories_, the second the filename.
110   std::vector< std::pair<int, std::string> > files_;
111
112   // We can't do better than to keep the offsets in a sorted vector.
113   // Here, offset is the key, and file_num/line_num is the value.
114   struct Offset_to_lineno_entry
115   {
116     off_t offset;
117     int file_num;    // a pointer into files_
118     int line_num;
119     // Offsets are unique within a section, so that's a sufficient sort key.
120     bool operator<(const Offset_to_lineno_entry& that) const
121     { return this->offset < that.offset; }
122   };
123   // We have a vector of offset->lineno entries for every input section.
124   typedef Unordered_map<unsigned int, std::vector<Offset_to_lineno_entry> >
125   Lineno_map;
126
127   Lineno_map line_number_map_;
128 };
129
130 } // End namespace gold.
131
132 #endif // !defined(GOLD_DWARF_READER_H)