line-header.h revision 1.1 1 1.1 christos /* DWARF 2 debugging format support for GDB.
2 1.1 christos
3 1.1 christos Copyright (C) 1994-2020 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This file is part of GDB.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 3 of the License, or
10 1.1 christos (at your option) any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
19 1.1 christos
20 1.1 christos #ifndef DWARF2_LINE_HEADER_H
21 1.1 christos #define DWARF2_LINE_HEADER_H
22 1.1 christos
23 1.1 christos #include "gdbtypes.h"
24 1.1 christos
25 1.1 christos /* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
26 1.1 christos later. */
27 1.1 christos typedef int dir_index;
28 1.1 christos
29 1.1 christos /* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
30 1.1 christos and later. */
31 1.1 christos typedef int file_name_index;
32 1.1 christos
33 1.1 christos struct line_header;
34 1.1 christos
35 1.1 christos struct file_entry
36 1.1 christos {
37 1.1 christos file_entry () = default;
38 1.1 christos
39 1.1 christos file_entry (const char *name_, dir_index d_index_,
40 1.1 christos unsigned int mod_time_, unsigned int length_)
41 1.1 christos : name (name_),
42 1.1 christos d_index (d_index_),
43 1.1 christos mod_time (mod_time_),
44 1.1 christos length (length_)
45 1.1 christos {}
46 1.1 christos
47 1.1 christos /* Return the include directory at D_INDEX stored in LH. Returns
48 1.1 christos NULL if D_INDEX is out of bounds. */
49 1.1 christos const char *include_dir (const line_header *lh) const;
50 1.1 christos
51 1.1 christos /* The file name. Note this is an observing pointer. The memory is
52 1.1 christos owned by debug_line_buffer. */
53 1.1 christos const char *name {};
54 1.1 christos
55 1.1 christos /* The directory index (1-based). */
56 1.1 christos dir_index d_index {};
57 1.1 christos
58 1.1 christos unsigned int mod_time {};
59 1.1 christos
60 1.1 christos unsigned int length {};
61 1.1 christos
62 1.1 christos /* True if referenced by the Line Number Program. */
63 1.1 christos bool included_p {};
64 1.1 christos
65 1.1 christos /* The associated symbol table, if any. */
66 1.1 christos struct symtab *symtab {};
67 1.1 christos };
68 1.1 christos
69 1.1 christos /* The line number information for a compilation unit (found in the
70 1.1 christos .debug_line section) begins with a "statement program header",
71 1.1 christos which contains the following information. */
72 1.1 christos struct line_header
73 1.1 christos {
74 1.1 christos line_header ()
75 1.1 christos : offset_in_dwz {}
76 1.1 christos {}
77 1.1 christos
78 1.1 christos /* Add an entry to the include directory table. */
79 1.1 christos void add_include_dir (const char *include_dir);
80 1.1 christos
81 1.1 christos /* Add an entry to the file name table. */
82 1.1 christos void add_file_name (const char *name, dir_index d_index,
83 1.1 christos unsigned int mod_time, unsigned int length);
84 1.1 christos
85 1.1 christos /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
86 1.1 christos Returns NULL if INDEX is out of bounds. */
87 1.1 christos const char *include_dir_at (dir_index index) const
88 1.1 christos {
89 1.1 christos int vec_index;
90 1.1 christos if (version >= 5)
91 1.1 christos vec_index = index;
92 1.1 christos else
93 1.1 christos vec_index = index - 1;
94 1.1 christos if (vec_index < 0 || vec_index >= m_include_dirs.size ())
95 1.1 christos return NULL;
96 1.1 christos return m_include_dirs[vec_index];
97 1.1 christos }
98 1.1 christos
99 1.1 christos bool is_valid_file_index (int file_index) const
100 1.1 christos {
101 1.1 christos if (version >= 5)
102 1.1 christos return 0 <= file_index && file_index < file_names_size ();
103 1.1 christos return 1 <= file_index && file_index <= file_names_size ();
104 1.1 christos }
105 1.1 christos
106 1.1 christos /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
107 1.1 christos Returns NULL if INDEX is out of bounds. */
108 1.1 christos file_entry *file_name_at (file_name_index index)
109 1.1 christos {
110 1.1 christos int vec_index;
111 1.1 christos if (version >= 5)
112 1.1 christos vec_index = index;
113 1.1 christos else
114 1.1 christos vec_index = index - 1;
115 1.1 christos if (vec_index < 0 || vec_index >= m_file_names.size ())
116 1.1 christos return NULL;
117 1.1 christos return &m_file_names[vec_index];
118 1.1 christos }
119 1.1 christos
120 1.1 christos /* A const overload of the same. */
121 1.1 christos const file_entry *file_name_at (file_name_index index) const
122 1.1 christos {
123 1.1 christos line_header *lh = const_cast<line_header *> (this);
124 1.1 christos return lh->file_name_at (index);
125 1.1 christos }
126 1.1 christos
127 1.1 christos /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
128 1.1 christos this method should only be used to iterate through all file entries in an
129 1.1 christos index-agnostic manner. */
130 1.1 christos std::vector<file_entry> &file_names ()
131 1.1 christos { return m_file_names; }
132 1.1 christos /* A const overload of the same. */
133 1.1 christos const std::vector<file_entry> &file_names () const
134 1.1 christos { return m_file_names; }
135 1.1 christos
136 1.1 christos /* Offset of line number information in .debug_line section. */
137 1.1 christos sect_offset sect_off {};
138 1.1 christos
139 1.1 christos /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
140 1.1 christos unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
141 1.1 christos
142 1.1 christos unsigned int total_length {};
143 1.1 christos unsigned short version {};
144 1.1 christos unsigned int header_length {};
145 1.1 christos unsigned char minimum_instruction_length {};
146 1.1 christos unsigned char maximum_ops_per_instruction {};
147 1.1 christos unsigned char default_is_stmt {};
148 1.1 christos int line_base {};
149 1.1 christos unsigned char line_range {};
150 1.1 christos unsigned char opcode_base {};
151 1.1 christos
152 1.1 christos /* standard_opcode_lengths[i] is the number of operands for the
153 1.1 christos standard opcode whose value is i. This means that
154 1.1 christos standard_opcode_lengths[0] is unused, and the last meaningful
155 1.1 christos element is standard_opcode_lengths[opcode_base - 1]. */
156 1.1 christos std::unique_ptr<unsigned char[]> standard_opcode_lengths;
157 1.1 christos
158 1.1 christos int file_names_size () const
159 1.1 christos { return m_file_names.size(); }
160 1.1 christos
161 1.1 christos /* The start and end of the statement program following this
162 1.1 christos header. These point into dwarf2_per_objfile->line_buffer. */
163 1.1 christos const gdb_byte *statement_program_start {}, *statement_program_end {};
164 1.1 christos
165 1.1 christos /* Return the full name of file number I in this object's file name
166 1.1 christos table. Use COMP_DIR as the name of the current directory of the
167 1.1 christos compilation. The result is allocated using xmalloc; the caller
168 1.1 christos is responsible for freeing it. */
169 1.1 christos gdb::unique_xmalloc_ptr<char> file_full_name (int file,
170 1.1 christos const char *comp_dir) const;
171 1.1 christos
172 1.1 christos /* Return file name relative to the compilation directory of file
173 1.1 christos number I in this object's file name table. The result is
174 1.1 christos allocated using xmalloc; the caller is responsible for freeing
175 1.1 christos it. */
176 1.1 christos gdb::unique_xmalloc_ptr<char> file_file_name (int file) const;
177 1.1 christos
178 1.1 christos private:
179 1.1 christos /* The include_directories table. Note these are observing
180 1.1 christos pointers. The memory is owned by debug_line_buffer. */
181 1.1 christos std::vector<const char *> m_include_dirs;
182 1.1 christos
183 1.1 christos /* The file_names table. This is private because the meaning of indexes
184 1.1 christos differs among DWARF versions (The first valid index is 1 in DWARF 4 and
185 1.1 christos before, and is 0 in DWARF 5 and later). So the client should use
186 1.1 christos file_name_at method for access. */
187 1.1 christos std::vector<file_entry> m_file_names;
188 1.1 christos };
189 1.1 christos
190 1.1 christos typedef std::unique_ptr<line_header> line_header_up;
191 1.1 christos
192 1.1 christos inline const char *
193 1.1 christos file_entry::include_dir (const line_header *lh) const
194 1.1 christos {
195 1.1 christos return lh->include_dir_at (d_index);
196 1.1 christos }
197 1.1 christos
198 1.1 christos /* Read the statement program header starting at SECT_OFF in SECTION.
199 1.1 christos Return line_header. Returns nullptr if there is a problem reading
200 1.1 christos the header, e.g., if it has a version we don't understand.
201 1.1 christos
202 1.1 christos NOTE: the strings in the include directory and file name tables of
203 1.1 christos the returned object point into the dwarf line section buffer,
204 1.1 christos and must not be freed. */
205 1.1 christos
206 1.1 christos extern line_header_up dwarf_decode_line_header
207 1.1 christos (sect_offset sect_off, bool is_dwz, dwarf2_per_objfile *per_objfile,
208 1.1 christos struct dwarf2_section_info *section, const struct comp_unit_head *cu_header);
209 1.1 christos
210 1.1 christos #endif /* DWARF2_LINE_HEADER_H */
211