dwarf2.c revision 1.9 1 1.1 christos /* DWARF 2 support.
2 1.9 christos Copyright (C) 1994-2020 Free Software Foundation, Inc.
3 1.1 christos
4 1.1 christos Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
5 1.1 christos (gavin (at) cygnus.com).
6 1.1 christos
7 1.1 christos From the dwarf2read.c header:
8 1.1 christos Adapted by Gary Funck (gary (at) intrepid.com), Intrepid Technology,
9 1.1 christos Inc. with support from Florida State University (under contract
10 1.1 christos with the Ada Joint Program Office), and Silicon Graphics, Inc.
11 1.1 christos Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
12 1.1 christos based on Fred Fish's (Cygnus Support) implementation of DWARF 1
13 1.1 christos support in dwarfread.c
14 1.1 christos
15 1.1 christos This file is part of BFD.
16 1.1 christos
17 1.1 christos This program is free software; you can redistribute it and/or modify
18 1.1 christos it under the terms of the GNU General Public License as published by
19 1.1 christos the Free Software Foundation; either version 3 of the License, or (at
20 1.1 christos your option) any later version.
21 1.1 christos
22 1.1 christos This program is distributed in the hope that it will be useful, but
23 1.1 christos WITHOUT ANY WARRANTY; without even the implied warranty of
24 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 1.1 christos General Public License for more details.
26 1.1 christos
27 1.1 christos You should have received a copy of the GNU General Public License
28 1.1 christos along with this program; if not, write to the Free Software
29 1.1 christos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
30 1.1 christos MA 02110-1301, USA. */
31 1.1 christos
32 1.1 christos #include "sysdep.h"
33 1.1 christos #include "bfd.h"
34 1.1 christos #include "libiberty.h"
35 1.1 christos #include "libbfd.h"
36 1.1 christos #include "elf-bfd.h"
37 1.1 christos #include "dwarf2.h"
38 1.9 christos #include "hashtab.h"
39 1.1 christos
40 1.1 christos /* The data in the .debug_line statement prologue looks like this. */
41 1.1 christos
42 1.1 christos struct line_head
43 1.1 christos {
44 1.1 christos bfd_vma total_length;
45 1.1 christos unsigned short version;
46 1.1 christos bfd_vma prologue_length;
47 1.1 christos unsigned char minimum_instruction_length;
48 1.1 christos unsigned char maximum_ops_per_insn;
49 1.1 christos unsigned char default_is_stmt;
50 1.1 christos int line_base;
51 1.1 christos unsigned char line_range;
52 1.1 christos unsigned char opcode_base;
53 1.1 christos unsigned char *standard_opcode_lengths;
54 1.1 christos };
55 1.1 christos
56 1.1 christos /* Attributes have a name and a value. */
57 1.1 christos
58 1.1 christos struct attribute
59 1.1 christos {
60 1.1 christos enum dwarf_attribute name;
61 1.1 christos enum dwarf_form form;
62 1.1 christos union
63 1.1 christos {
64 1.1 christos char *str;
65 1.1 christos struct dwarf_block *blk;
66 1.1 christos bfd_uint64_t val;
67 1.1 christos bfd_int64_t sval;
68 1.1 christos }
69 1.1 christos u;
70 1.1 christos };
71 1.1 christos
72 1.1 christos /* Blocks are a bunch of untyped bytes. */
73 1.1 christos struct dwarf_block
74 1.1 christos {
75 1.1 christos unsigned int size;
76 1.1 christos bfd_byte *data;
77 1.1 christos };
78 1.1 christos
79 1.1 christos struct adjusted_section
80 1.1 christos {
81 1.1 christos asection *section;
82 1.1 christos bfd_vma adj_vma;
83 1.1 christos };
84 1.1 christos
85 1.9 christos struct dwarf2_debug_file
86 1.1 christos {
87 1.9 christos /* The actual bfd from which debug info was loaded. Might be
88 1.9 christos different to orig_bfd because of gnu_debuglink sections. */
89 1.9 christos bfd *bfd_ptr;
90 1.1 christos
91 1.9 christos /* Pointer to the symbol table. */
92 1.9 christos asymbol **syms;
93 1.1 christos
94 1.9 christos /* The current info pointer for the .debug_info section being parsed. */
95 1.1 christos bfd_byte *info_ptr;
96 1.1 christos
97 1.9 christos /* A pointer to the memory block allocated for .debug_info sections. */
98 1.9 christos bfd_byte *dwarf_info_buffer;
99 1.1 christos
100 1.9 christos /* Length of the loaded .debug_info sections. */
101 1.9 christos bfd_size_type dwarf_info_size;
102 1.1 christos
103 1.1 christos /* Pointer to the .debug_abbrev section loaded into memory. */
104 1.1 christos bfd_byte *dwarf_abbrev_buffer;
105 1.1 christos
106 1.1 christos /* Length of the loaded .debug_abbrev section. */
107 1.1 christos bfd_size_type dwarf_abbrev_size;
108 1.1 christos
109 1.1 christos /* Buffer for decode_line_info. */
110 1.1 christos bfd_byte *dwarf_line_buffer;
111 1.1 christos
112 1.1 christos /* Length of the loaded .debug_line section. */
113 1.1 christos bfd_size_type dwarf_line_size;
114 1.1 christos
115 1.1 christos /* Pointer to the .debug_str section loaded into memory. */
116 1.1 christos bfd_byte *dwarf_str_buffer;
117 1.1 christos
118 1.1 christos /* Length of the loaded .debug_str section. */
119 1.1 christos bfd_size_type dwarf_str_size;
120 1.1 christos
121 1.8 christos /* Pointer to the .debug_line_str section loaded into memory. */
122 1.8 christos bfd_byte *dwarf_line_str_buffer;
123 1.8 christos
124 1.8 christos /* Length of the loaded .debug_line_str section. */
125 1.8 christos bfd_size_type dwarf_line_str_size;
126 1.8 christos
127 1.7 christos /* Pointer to the .debug_ranges section loaded into memory. */
128 1.1 christos bfd_byte *dwarf_ranges_buffer;
129 1.1 christos
130 1.7 christos /* Length of the loaded .debug_ranges section. */
131 1.1 christos bfd_size_type dwarf_ranges_size;
132 1.1 christos
133 1.9 christos /* Pointer to the .debug_rnglists section loaded into memory. */
134 1.9 christos bfd_byte *dwarf_rnglists_buffer;
135 1.9 christos
136 1.9 christos /* Length of the loaded .debug_rnglists section. */
137 1.9 christos bfd_size_type dwarf_rnglists_size;
138 1.9 christos
139 1.9 christos /* A list of all previously read comp_units. */
140 1.9 christos struct comp_unit *all_comp_units;
141 1.9 christos
142 1.9 christos /* Last comp unit in list above. */
143 1.9 christos struct comp_unit *last_comp_unit;
144 1.9 christos
145 1.9 christos /* Line table at line_offset zero. */
146 1.9 christos struct line_info_table *line_table;
147 1.9 christos
148 1.9 christos /* Hash table to map offsets to decoded abbrevs. */
149 1.9 christos htab_t abbrev_offsets;
150 1.9 christos };
151 1.9 christos
152 1.9 christos struct dwarf2_debug
153 1.9 christos {
154 1.9 christos /* Names of the debug sections. */
155 1.9 christos const struct dwarf_debug_section *debug_sections;
156 1.9 christos
157 1.9 christos /* Per-file stuff. */
158 1.9 christos struct dwarf2_debug_file f, alt;
159 1.9 christos
160 1.9 christos /* Pointer to the original bfd for which debug was loaded. This is what
161 1.9 christos we use to compare and so check that the cached debug data is still
162 1.9 christos valid - it saves having to possibly dereference the gnu_debuglink each
163 1.9 christos time. */
164 1.9 christos bfd *orig_bfd;
165 1.9 christos
166 1.1 christos /* If the most recent call to bfd_find_nearest_line was given an
167 1.1 christos address in an inlined function, preserve a pointer into the
168 1.1 christos calling chain for subsequent calls to bfd_find_inliner_info to
169 1.7 christos use. */
170 1.1 christos struct funcinfo *inliner_chain;
171 1.1 christos
172 1.3 christos /* Section VMAs at the time the stash was built. */
173 1.3 christos bfd_vma *sec_vma;
174 1.9 christos /* Number of sections in the SEC_VMA table. */
175 1.9 christos unsigned int sec_vma_count;
176 1.3 christos
177 1.1 christos /* Number of sections whose VMA we must adjust. */
178 1.3 christos int adjusted_section_count;
179 1.1 christos
180 1.1 christos /* Array of sections with adjusted VMA. */
181 1.1 christos struct adjusted_section *adjusted_sections;
182 1.1 christos
183 1.1 christos /* Number of times find_line is called. This is used in
184 1.1 christos the heuristic for enabling the info hash tables. */
185 1.1 christos int info_hash_count;
186 1.1 christos
187 1.1 christos #define STASH_INFO_HASH_TRIGGER 100
188 1.1 christos
189 1.1 christos /* Hash table mapping symbol names to function infos. */
190 1.1 christos struct info_hash_table *funcinfo_hash_table;
191 1.1 christos
192 1.1 christos /* Hash table mapping symbol names to variable infos. */
193 1.1 christos struct info_hash_table *varinfo_hash_table;
194 1.1 christos
195 1.1 christos /* Head of comp_unit list in the last hash table update. */
196 1.1 christos struct comp_unit *hash_units_head;
197 1.1 christos
198 1.1 christos /* Status of info hash. */
199 1.1 christos int info_hash_status;
200 1.8 christos #define STASH_INFO_HASH_OFF 0
201 1.8 christos #define STASH_INFO_HASH_ON 1
202 1.1 christos #define STASH_INFO_HASH_DISABLED 2
203 1.1 christos
204 1.1 christos /* True if we opened bfd_ptr. */
205 1.1 christos bfd_boolean close_on_cleanup;
206 1.1 christos };
207 1.1 christos
208 1.1 christos struct arange
209 1.1 christos {
210 1.1 christos struct arange *next;
211 1.1 christos bfd_vma low;
212 1.1 christos bfd_vma high;
213 1.1 christos };
214 1.1 christos
215 1.1 christos /* A minimal decoding of DWARF2 compilation units. We only decode
216 1.1 christos what's needed to get to the line number information. */
217 1.1 christos
218 1.1 christos struct comp_unit
219 1.1 christos {
220 1.1 christos /* Chain the previously read compilation units. */
221 1.1 christos struct comp_unit *next_unit;
222 1.1 christos
223 1.1 christos /* Likewise, chain the compilation unit read after this one.
224 1.1 christos The comp units are stored in reversed reading order. */
225 1.1 christos struct comp_unit *prev_unit;
226 1.1 christos
227 1.1 christos /* Keep the bfd convenient (for memory allocation). */
228 1.1 christos bfd *abfd;
229 1.1 christos
230 1.1 christos /* The lowest and highest addresses contained in this compilation
231 1.1 christos unit as specified in the compilation unit header. */
232 1.1 christos struct arange arange;
233 1.1 christos
234 1.1 christos /* The DW_AT_name attribute (for error messages). */
235 1.1 christos char *name;
236 1.1 christos
237 1.1 christos /* The abbrev hash table. */
238 1.1 christos struct abbrev_info **abbrevs;
239 1.1 christos
240 1.3 christos /* DW_AT_language. */
241 1.3 christos int lang;
242 1.3 christos
243 1.1 christos /* Note that an error was found by comp_unit_find_nearest_line. */
244 1.1 christos int error;
245 1.1 christos
246 1.1 christos /* The DW_AT_comp_dir attribute. */
247 1.1 christos char *comp_dir;
248 1.1 christos
249 1.1 christos /* TRUE if there is a line number table associated with this comp. unit. */
250 1.1 christos int stmtlist;
251 1.1 christos
252 1.1 christos /* Pointer to the current comp_unit so that we can find a given entry
253 1.1 christos by its reference. */
254 1.1 christos bfd_byte *info_ptr_unit;
255 1.1 christos
256 1.1 christos /* The offset into .debug_line of the line number table. */
257 1.1 christos unsigned long line_offset;
258 1.1 christos
259 1.1 christos /* Pointer to the first child die for the comp unit. */
260 1.1 christos bfd_byte *first_child_die_ptr;
261 1.1 christos
262 1.1 christos /* The end of the comp unit. */
263 1.1 christos bfd_byte *end_ptr;
264 1.1 christos
265 1.1 christos /* The decoded line number, NULL if not yet decoded. */
266 1.1 christos struct line_info_table *line_table;
267 1.1 christos
268 1.1 christos /* A list of the functions found in this comp. unit. */
269 1.1 christos struct funcinfo *function_table;
270 1.1 christos
271 1.7 christos /* A table of function information references searchable by address. */
272 1.7 christos struct lookup_funcinfo *lookup_funcinfo_table;
273 1.7 christos
274 1.7 christos /* Number of functions in the function_table and sorted_function_table. */
275 1.7 christos bfd_size_type number_of_functions;
276 1.7 christos
277 1.1 christos /* A list of the variables found in this comp. unit. */
278 1.1 christos struct varinfo *variable_table;
279 1.1 christos
280 1.9 christos /* Pointers to dwarf2_debug structures. */
281 1.1 christos struct dwarf2_debug *stash;
282 1.9 christos struct dwarf2_debug_file *file;
283 1.1 christos
284 1.1 christos /* DWARF format version for this unit - from unit header. */
285 1.1 christos int version;
286 1.1 christos
287 1.1 christos /* Address size for this unit - from unit header. */
288 1.1 christos unsigned char addr_size;
289 1.1 christos
290 1.1 christos /* Offset size for this unit - from unit header. */
291 1.1 christos unsigned char offset_size;
292 1.1 christos
293 1.1 christos /* Base address for this unit - from DW_AT_low_pc attribute of
294 1.1 christos DW_TAG_compile_unit DIE */
295 1.1 christos bfd_vma base_address;
296 1.1 christos
297 1.1 christos /* TRUE if symbols are cached in hash table for faster lookup by name. */
298 1.1 christos bfd_boolean cached;
299 1.1 christos };
300 1.1 christos
301 1.1 christos /* This data structure holds the information of an abbrev. */
302 1.1 christos struct abbrev_info
303 1.1 christos {
304 1.9 christos unsigned int number; /* Number identifying abbrev. */
305 1.9 christos enum dwarf_tag tag; /* DWARF tag. */
306 1.9 christos bfd_boolean has_children; /* TRUE if the abbrev has children. */
307 1.9 christos unsigned int num_attrs; /* Number of attributes. */
308 1.9 christos struct attr_abbrev * attrs; /* An array of attribute descriptions. */
309 1.9 christos struct abbrev_info * next; /* Next in chain. */
310 1.1 christos };
311 1.1 christos
312 1.1 christos struct attr_abbrev
313 1.1 christos {
314 1.1 christos enum dwarf_attribute name;
315 1.1 christos enum dwarf_form form;
316 1.8 christos bfd_vma implicit_const;
317 1.1 christos };
318 1.1 christos
319 1.1 christos /* Map of uncompressed DWARF debug section name to compressed one. It
320 1.1 christos is terminated by NULL uncompressed_name. */
321 1.1 christos
322 1.1 christos const struct dwarf_debug_section dwarf_debug_sections[] =
323 1.1 christos {
324 1.1 christos { ".debug_abbrev", ".zdebug_abbrev" },
325 1.1 christos { ".debug_aranges", ".zdebug_aranges" },
326 1.1 christos { ".debug_frame", ".zdebug_frame" },
327 1.1 christos { ".debug_info", ".zdebug_info" },
328 1.1 christos { ".debug_info", ".zdebug_info" },
329 1.1 christos { ".debug_line", ".zdebug_line" },
330 1.1 christos { ".debug_loc", ".zdebug_loc" },
331 1.1 christos { ".debug_macinfo", ".zdebug_macinfo" },
332 1.1 christos { ".debug_macro", ".zdebug_macro" },
333 1.1 christos { ".debug_pubnames", ".zdebug_pubnames" },
334 1.1 christos { ".debug_pubtypes", ".zdebug_pubtypes" },
335 1.1 christos { ".debug_ranges", ".zdebug_ranges" },
336 1.9 christos { ".debug_rnglists", ".zdebug_rnglist" },
337 1.1 christos { ".debug_static_func", ".zdebug_static_func" },
338 1.1 christos { ".debug_static_vars", ".zdebug_static_vars" },
339 1.1 christos { ".debug_str", ".zdebug_str", },
340 1.1 christos { ".debug_str", ".zdebug_str", },
341 1.8 christos { ".debug_line_str", ".zdebug_line_str", },
342 1.1 christos { ".debug_types", ".zdebug_types" },
343 1.1 christos /* GNU DWARF 1 extensions */
344 1.1 christos { ".debug_sfnames", ".zdebug_sfnames" },
345 1.1 christos { ".debug_srcinfo", ".zebug_srcinfo" },
346 1.1 christos /* SGI/MIPS DWARF 2 extensions */
347 1.1 christos { ".debug_funcnames", ".zdebug_funcnames" },
348 1.1 christos { ".debug_typenames", ".zdebug_typenames" },
349 1.1 christos { ".debug_varnames", ".zdebug_varnames" },
350 1.1 christos { ".debug_weaknames", ".zdebug_weaknames" },
351 1.1 christos { NULL, NULL },
352 1.1 christos };
353 1.1 christos
354 1.8 christos /* NB/ Numbers in this enum must match up with indices
355 1.1 christos into the dwarf_debug_sections[] array above. */
356 1.1 christos enum dwarf_debug_section_enum
357 1.1 christos {
358 1.1 christos debug_abbrev = 0,
359 1.1 christos debug_aranges,
360 1.1 christos debug_frame,
361 1.1 christos debug_info,
362 1.1 christos debug_info_alt,
363 1.1 christos debug_line,
364 1.1 christos debug_loc,
365 1.1 christos debug_macinfo,
366 1.1 christos debug_macro,
367 1.1 christos debug_pubnames,
368 1.1 christos debug_pubtypes,
369 1.1 christos debug_ranges,
370 1.9 christos debug_rnglists,
371 1.1 christos debug_static_func,
372 1.1 christos debug_static_vars,
373 1.1 christos debug_str,
374 1.1 christos debug_str_alt,
375 1.8 christos debug_line_str,
376 1.1 christos debug_types,
377 1.1 christos debug_sfnames,
378 1.1 christos debug_srcinfo,
379 1.1 christos debug_funcnames,
380 1.1 christos debug_typenames,
381 1.1 christos debug_varnames,
382 1.8 christos debug_weaknames,
383 1.8 christos debug_max
384 1.1 christos };
385 1.1 christos
386 1.8 christos /* A static assertion. */
387 1.8 christos extern int dwarf_debug_section_assert[ARRAY_SIZE (dwarf_debug_sections)
388 1.8 christos == debug_max + 1 ? 1 : -1];
389 1.8 christos
390 1.1 christos #ifndef ABBREV_HASH_SIZE
391 1.1 christos #define ABBREV_HASH_SIZE 121
392 1.1 christos #endif
393 1.1 christos #ifndef ATTR_ALLOC_CHUNK
394 1.1 christos #define ATTR_ALLOC_CHUNK 4
395 1.1 christos #endif
396 1.1 christos
397 1.1 christos /* Variable and function hash tables. This is used to speed up look-up
398 1.1 christos in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
399 1.1 christos In order to share code between variable and function infos, we use
400 1.1 christos a list of untyped pointer for all variable/function info associated with
401 1.1 christos a symbol. We waste a bit of memory for list with one node but that
402 1.1 christos simplifies the code. */
403 1.1 christos
404 1.1 christos struct info_list_node
405 1.1 christos {
406 1.1 christos struct info_list_node *next;
407 1.1 christos void *info;
408 1.1 christos };
409 1.1 christos
410 1.1 christos /* Info hash entry. */
411 1.1 christos struct info_hash_entry
412 1.1 christos {
413 1.1 christos struct bfd_hash_entry root;
414 1.1 christos struct info_list_node *head;
415 1.1 christos };
416 1.1 christos
417 1.1 christos struct info_hash_table
418 1.1 christos {
419 1.1 christos struct bfd_hash_table base;
420 1.1 christos };
421 1.1 christos
422 1.7 christos /* Function to create a new entry in info hash table. */
423 1.1 christos
424 1.1 christos static struct bfd_hash_entry *
425 1.1 christos info_hash_table_newfunc (struct bfd_hash_entry *entry,
426 1.1 christos struct bfd_hash_table *table,
427 1.1 christos const char *string)
428 1.1 christos {
429 1.1 christos struct info_hash_entry *ret = (struct info_hash_entry *) entry;
430 1.1 christos
431 1.1 christos /* Allocate the structure if it has not already been allocated by a
432 1.1 christos derived class. */
433 1.1 christos if (ret == NULL)
434 1.1 christos {
435 1.1 christos ret = (struct info_hash_entry *) bfd_hash_allocate (table,
436 1.3 christos sizeof (* ret));
437 1.1 christos if (ret == NULL)
438 1.1 christos return NULL;
439 1.1 christos }
440 1.1 christos
441 1.1 christos /* Call the allocation method of the base class. */
442 1.1 christos ret = ((struct info_hash_entry *)
443 1.1 christos bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
444 1.1 christos
445 1.1 christos /* Initialize the local fields here. */
446 1.1 christos if (ret)
447 1.1 christos ret->head = NULL;
448 1.1 christos
449 1.1 christos return (struct bfd_hash_entry *) ret;
450 1.1 christos }
451 1.1 christos
452 1.1 christos /* Function to create a new info hash table. It returns a pointer to the
453 1.1 christos newly created table or NULL if there is any error. We need abfd
454 1.1 christos solely for memory allocation. */
455 1.1 christos
456 1.1 christos static struct info_hash_table *
457 1.1 christos create_info_hash_table (bfd *abfd)
458 1.1 christos {
459 1.1 christos struct info_hash_table *hash_table;
460 1.1 christos
461 1.1 christos hash_table = ((struct info_hash_table *)
462 1.1 christos bfd_alloc (abfd, sizeof (struct info_hash_table)));
463 1.1 christos if (!hash_table)
464 1.1 christos return hash_table;
465 1.1 christos
466 1.1 christos if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
467 1.1 christos sizeof (struct info_hash_entry)))
468 1.1 christos {
469 1.1 christos bfd_release (abfd, hash_table);
470 1.1 christos return NULL;
471 1.1 christos }
472 1.1 christos
473 1.1 christos return hash_table;
474 1.1 christos }
475 1.1 christos
476 1.1 christos /* Insert an info entry into an info hash table. We do not check of
477 1.1 christos duplicate entries. Also, the caller need to guarantee that the
478 1.1 christos right type of info in inserted as info is passed as a void* pointer.
479 1.1 christos This function returns true if there is no error. */
480 1.1 christos
481 1.1 christos static bfd_boolean
482 1.1 christos insert_info_hash_table (struct info_hash_table *hash_table,
483 1.1 christos const char *key,
484 1.1 christos void *info,
485 1.1 christos bfd_boolean copy_p)
486 1.1 christos {
487 1.1 christos struct info_hash_entry *entry;
488 1.1 christos struct info_list_node *node;
489 1.1 christos
490 1.1 christos entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
491 1.1 christos key, TRUE, copy_p);
492 1.1 christos if (!entry)
493 1.1 christos return FALSE;
494 1.1 christos
495 1.1 christos node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
496 1.3 christos sizeof (*node));
497 1.1 christos if (!node)
498 1.1 christos return FALSE;
499 1.1 christos
500 1.1 christos node->info = info;
501 1.1 christos node->next = entry->head;
502 1.1 christos entry->head = node;
503 1.1 christos
504 1.1 christos return TRUE;
505 1.1 christos }
506 1.1 christos
507 1.1 christos /* Look up an info entry list from an info hash table. Return NULL
508 1.7 christos if there is none. */
509 1.1 christos
510 1.1 christos static struct info_list_node *
511 1.1 christos lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
512 1.1 christos {
513 1.1 christos struct info_hash_entry *entry;
514 1.1 christos
515 1.1 christos entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
516 1.1 christos FALSE, FALSE);
517 1.1 christos return entry ? entry->head : NULL;
518 1.1 christos }
519 1.1 christos
520 1.1 christos /* Read a section into its appropriate place in the dwarf2_debug
521 1.1 christos struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
522 1.1 christos not NULL, use bfd_simple_get_relocated_section_contents to read the
523 1.1 christos section contents, otherwise use bfd_get_section_contents. Fail if
524 1.1 christos the located section does not contain at least OFFSET bytes. */
525 1.1 christos
526 1.1 christos static bfd_boolean
527 1.8 christos read_section (bfd * abfd,
528 1.1 christos const struct dwarf_debug_section *sec,
529 1.1 christos asymbol ** syms,
530 1.1 christos bfd_uint64_t offset,
531 1.1 christos bfd_byte ** section_buffer,
532 1.1 christos bfd_size_type * section_size)
533 1.1 christos {
534 1.1 christos asection *msec;
535 1.1 christos const char *section_name = sec->uncompressed_name;
536 1.8 christos bfd_byte *contents = *section_buffer;
537 1.8 christos bfd_size_type amt;
538 1.1 christos
539 1.1 christos /* The section may have already been read. */
540 1.8 christos if (contents == NULL)
541 1.1 christos {
542 1.1 christos msec = bfd_get_section_by_name (abfd, section_name);
543 1.1 christos if (! msec)
544 1.1 christos {
545 1.1 christos section_name = sec->compressed_name;
546 1.3 christos if (section_name != NULL)
547 1.3 christos msec = bfd_get_section_by_name (abfd, section_name);
548 1.1 christos }
549 1.1 christos if (! msec)
550 1.1 christos {
551 1.8 christos _bfd_error_handler (_("DWARF error: can't find %s section."),
552 1.7 christos sec->uncompressed_name);
553 1.1 christos bfd_set_error (bfd_error_bad_value);
554 1.1 christos return FALSE;
555 1.1 christos }
556 1.1 christos
557 1.1 christos *section_size = msec->rawsize ? msec->rawsize : msec->size;
558 1.8 christos /* Paranoia - alloc one extra so that we can make sure a string
559 1.8 christos section is NUL terminated. */
560 1.8 christos amt = *section_size + 1;
561 1.8 christos if (amt == 0)
562 1.1 christos {
563 1.8 christos bfd_set_error (bfd_error_no_memory);
564 1.8 christos return FALSE;
565 1.1 christos }
566 1.8 christos contents = (bfd_byte *) bfd_malloc (amt);
567 1.8 christos if (contents == NULL)
568 1.8 christos return FALSE;
569 1.8 christos if (syms
570 1.8 christos ? !bfd_simple_get_relocated_section_contents (abfd, msec, contents,
571 1.8 christos syms)
572 1.8 christos : !bfd_get_section_contents (abfd, msec, contents, 0, *section_size))
573 1.1 christos {
574 1.8 christos free (contents);
575 1.8 christos return FALSE;
576 1.1 christos }
577 1.8 christos contents[*section_size] = 0;
578 1.8 christos *section_buffer = contents;
579 1.1 christos }
580 1.1 christos
581 1.1 christos /* It is possible to get a bad value for the offset into the section
582 1.1 christos that the client wants. Validate it here to avoid trouble later. */
583 1.1 christos if (offset != 0 && offset >= *section_size)
584 1.1 christos {
585 1.7 christos /* xgettext: c-format */
586 1.8 christos _bfd_error_handler (_("DWARF error: offset (%" PRIu64 ")"
587 1.8 christos " greater than or equal to %s size (%" PRIu64 ")"),
588 1.8 christos (uint64_t) offset, section_name,
589 1.8 christos (uint64_t) *section_size);
590 1.1 christos bfd_set_error (bfd_error_bad_value);
591 1.1 christos return FALSE;
592 1.1 christos }
593 1.1 christos
594 1.1 christos return TRUE;
595 1.1 christos }
596 1.1 christos
597 1.1 christos /* Read dwarf information from a buffer. */
598 1.1 christos
599 1.1 christos static unsigned int
600 1.5 christos read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf, bfd_byte *end)
601 1.1 christos {
602 1.5 christos if (buf + 1 > end)
603 1.5 christos return 0;
604 1.1 christos return bfd_get_8 (abfd, buf);
605 1.1 christos }
606 1.1 christos
607 1.1 christos static int
608 1.5 christos read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf, bfd_byte *end)
609 1.1 christos {
610 1.5 christos if (buf + 1 > end)
611 1.5 christos return 0;
612 1.1 christos return bfd_get_signed_8 (abfd, buf);
613 1.1 christos }
614 1.1 christos
615 1.1 christos static unsigned int
616 1.5 christos read_2_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
617 1.1 christos {
618 1.5 christos if (buf + 2 > end)
619 1.5 christos return 0;
620 1.1 christos return bfd_get_16 (abfd, buf);
621 1.1 christos }
622 1.1 christos
623 1.1 christos static unsigned int
624 1.5 christos read_4_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
625 1.1 christos {
626 1.5 christos if (buf + 4 > end)
627 1.5 christos return 0;
628 1.1 christos return bfd_get_32 (abfd, buf);
629 1.1 christos }
630 1.1 christos
631 1.1 christos static bfd_uint64_t
632 1.5 christos read_8_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
633 1.1 christos {
634 1.5 christos if (buf + 8 > end)
635 1.5 christos return 0;
636 1.1 christos return bfd_get_64 (abfd, buf);
637 1.1 christos }
638 1.1 christos
639 1.1 christos static bfd_byte *
640 1.8 christos read_n_bytes (bfd_byte * buf,
641 1.8 christos bfd_byte * end,
642 1.8 christos struct dwarf_block * block)
643 1.8 christos {
644 1.8 christos unsigned int size = block->size;
645 1.8 christos bfd_byte * block_end = buf + size;
646 1.8 christos
647 1.8 christos if (block_end > end || block_end < buf)
648 1.8 christos {
649 1.8 christos block->data = NULL;
650 1.8 christos block->size = 0;
651 1.8 christos return end;
652 1.8 christos }
653 1.8 christos else
654 1.8 christos {
655 1.8 christos block->data = buf;
656 1.8 christos return block_end;
657 1.8 christos }
658 1.1 christos }
659 1.1 christos
660 1.5 christos /* Scans a NUL terminated string starting at BUF, returning a pointer to it.
661 1.5 christos Returns the number of characters in the string, *including* the NUL byte,
662 1.5 christos in BYTES_READ_PTR. This value is set even if the function fails. Bytes
663 1.5 christos at or beyond BUF_END will not be read. Returns NULL if there was a
664 1.5 christos problem, or if the string is empty. */
665 1.5 christos
666 1.1 christos static char *
667 1.8 christos read_string (bfd * abfd ATTRIBUTE_UNUSED,
668 1.8 christos bfd_byte * buf,
669 1.8 christos bfd_byte * buf_end,
670 1.5 christos unsigned int * bytes_read_ptr)
671 1.1 christos {
672 1.5 christos bfd_byte *str = buf;
673 1.5 christos
674 1.5 christos if (buf >= buf_end)
675 1.5 christos {
676 1.5 christos * bytes_read_ptr = 0;
677 1.5 christos return NULL;
678 1.5 christos }
679 1.1 christos
680 1.1 christos if (*str == '\0')
681 1.1 christos {
682 1.5 christos * bytes_read_ptr = 1;
683 1.1 christos return NULL;
684 1.1 christos }
685 1.1 christos
686 1.5 christos while (buf < buf_end)
687 1.5 christos if (* buf ++ == 0)
688 1.5 christos {
689 1.5 christos * bytes_read_ptr = buf - str;
690 1.5 christos return (char *) str;
691 1.5 christos }
692 1.5 christos
693 1.5 christos * bytes_read_ptr = buf - str;
694 1.5 christos return NULL;
695 1.1 christos }
696 1.1 christos
697 1.5 christos /* Reads an offset from BUF and then locates the string at this offset
698 1.5 christos inside the debug string section. Returns a pointer to the string.
699 1.5 christos Returns the number of bytes read from BUF, *not* the length of the string,
700 1.5 christos in BYTES_READ_PTR. This value is set even if the function fails. Bytes
701 1.5 christos at or beyond BUF_END will not be read from BUF. Returns NULL if there was
702 1.5 christos a problem, or if the string is empty. Does not check for NUL termination
703 1.5 christos of the string. */
704 1.1 christos
705 1.1 christos static char *
706 1.1 christos read_indirect_string (struct comp_unit * unit,
707 1.8 christos bfd_byte * buf,
708 1.8 christos bfd_byte * buf_end,
709 1.8 christos unsigned int * bytes_read_ptr)
710 1.1 christos {
711 1.1 christos bfd_uint64_t offset;
712 1.1 christos struct dwarf2_debug *stash = unit->stash;
713 1.9 christos struct dwarf2_debug_file *file = unit->file;
714 1.1 christos char *str;
715 1.1 christos
716 1.5 christos if (buf + unit->offset_size > buf_end)
717 1.5 christos {
718 1.5 christos * bytes_read_ptr = 0;
719 1.5 christos return NULL;
720 1.5 christos }
721 1.5 christos
722 1.1 christos if (unit->offset_size == 4)
723 1.5 christos offset = read_4_bytes (unit->abfd, buf, buf_end);
724 1.1 christos else
725 1.5 christos offset = read_8_bytes (unit->abfd, buf, buf_end);
726 1.1 christos
727 1.1 christos *bytes_read_ptr = unit->offset_size;
728 1.1 christos
729 1.1 christos if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
730 1.9 christos file->syms, offset,
731 1.9 christos &file->dwarf_str_buffer, &file->dwarf_str_size))
732 1.1 christos return NULL;
733 1.1 christos
734 1.9 christos str = (char *) file->dwarf_str_buffer + offset;
735 1.1 christos if (*str == '\0')
736 1.1 christos return NULL;
737 1.1 christos return str;
738 1.1 christos }
739 1.1 christos
740 1.8 christos /* Like read_indirect_string but from .debug_line_str section. */
741 1.8 christos
742 1.8 christos static char *
743 1.8 christos read_indirect_line_string (struct comp_unit * unit,
744 1.8 christos bfd_byte * buf,
745 1.8 christos bfd_byte * buf_end,
746 1.8 christos unsigned int * bytes_read_ptr)
747 1.8 christos {
748 1.8 christos bfd_uint64_t offset;
749 1.8 christos struct dwarf2_debug *stash = unit->stash;
750 1.9 christos struct dwarf2_debug_file *file = unit->file;
751 1.8 christos char *str;
752 1.8 christos
753 1.8 christos if (buf + unit->offset_size > buf_end)
754 1.8 christos {
755 1.8 christos * bytes_read_ptr = 0;
756 1.8 christos return NULL;
757 1.8 christos }
758 1.8 christos
759 1.8 christos if (unit->offset_size == 4)
760 1.8 christos offset = read_4_bytes (unit->abfd, buf, buf_end);
761 1.8 christos else
762 1.8 christos offset = read_8_bytes (unit->abfd, buf, buf_end);
763 1.8 christos
764 1.8 christos *bytes_read_ptr = unit->offset_size;
765 1.8 christos
766 1.8 christos if (! read_section (unit->abfd, &stash->debug_sections[debug_line_str],
767 1.9 christos file->syms, offset,
768 1.9 christos &file->dwarf_line_str_buffer,
769 1.9 christos &file->dwarf_line_str_size))
770 1.8 christos return NULL;
771 1.8 christos
772 1.9 christos str = (char *) file->dwarf_line_str_buffer + offset;
773 1.8 christos if (*str == '\0')
774 1.8 christos return NULL;
775 1.8 christos return str;
776 1.8 christos }
777 1.8 christos
778 1.1 christos /* Like read_indirect_string but uses a .debug_str located in
779 1.3 christos an alternate file pointed to by the .gnu_debugaltlink section.
780 1.1 christos Used to impement DW_FORM_GNU_strp_alt. */
781 1.1 christos
782 1.1 christos static char *
783 1.1 christos read_alt_indirect_string (struct comp_unit * unit,
784 1.8 christos bfd_byte * buf,
785 1.8 christos bfd_byte * buf_end,
786 1.1 christos unsigned int * bytes_read_ptr)
787 1.1 christos {
788 1.1 christos bfd_uint64_t offset;
789 1.1 christos struct dwarf2_debug *stash = unit->stash;
790 1.1 christos char *str;
791 1.1 christos
792 1.5 christos if (buf + unit->offset_size > buf_end)
793 1.5 christos {
794 1.5 christos * bytes_read_ptr = 0;
795 1.5 christos return NULL;
796 1.5 christos }
797 1.5 christos
798 1.1 christos if (unit->offset_size == 4)
799 1.5 christos offset = read_4_bytes (unit->abfd, buf, buf_end);
800 1.1 christos else
801 1.5 christos offset = read_8_bytes (unit->abfd, buf, buf_end);
802 1.1 christos
803 1.1 christos *bytes_read_ptr = unit->offset_size;
804 1.1 christos
805 1.9 christos if (stash->alt.bfd_ptr == NULL)
806 1.1 christos {
807 1.9 christos bfd *debug_bfd;
808 1.9 christos char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
809 1.1 christos
810 1.1 christos if (debug_filename == NULL)
811 1.1 christos return NULL;
812 1.1 christos
813 1.9 christos debug_bfd = bfd_openr (debug_filename, NULL);
814 1.9 christos free (debug_filename);
815 1.9 christos if (debug_bfd == NULL)
816 1.9 christos /* FIXME: Should we report our failure to follow the debuglink ? */
817 1.9 christos return NULL;
818 1.9 christos
819 1.9 christos if (!bfd_check_format (debug_bfd, bfd_object))
820 1.1 christos {
821 1.9 christos bfd_close (debug_bfd);
822 1.1 christos return NULL;
823 1.1 christos }
824 1.9 christos stash->alt.bfd_ptr = debug_bfd;
825 1.1 christos }
826 1.5 christos
827 1.9 christos if (! read_section (unit->stash->alt.bfd_ptr,
828 1.1 christos stash->debug_sections + debug_str_alt,
829 1.9 christos stash->alt.syms, offset,
830 1.9 christos &stash->alt.dwarf_str_buffer,
831 1.9 christos &stash->alt.dwarf_str_size))
832 1.1 christos return NULL;
833 1.1 christos
834 1.9 christos str = (char *) stash->alt.dwarf_str_buffer + offset;
835 1.1 christos if (*str == '\0')
836 1.1 christos return NULL;
837 1.1 christos
838 1.1 christos return str;
839 1.1 christos }
840 1.1 christos
841 1.1 christos /* Resolve an alternate reference from UNIT at OFFSET.
842 1.1 christos Returns a pointer into the loaded alternate CU upon success
843 1.1 christos or NULL upon failure. */
844 1.1 christos
845 1.1 christos static bfd_byte *
846 1.1 christos read_alt_indirect_ref (struct comp_unit * unit,
847 1.1 christos bfd_uint64_t offset)
848 1.1 christos {
849 1.1 christos struct dwarf2_debug *stash = unit->stash;
850 1.1 christos
851 1.9 christos if (stash->alt.bfd_ptr == NULL)
852 1.1 christos {
853 1.9 christos bfd *debug_bfd;
854 1.9 christos char *debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
855 1.1 christos
856 1.1 christos if (debug_filename == NULL)
857 1.9 christos return NULL;
858 1.9 christos
859 1.9 christos debug_bfd = bfd_openr (debug_filename, NULL);
860 1.9 christos free (debug_filename);
861 1.9 christos if (debug_bfd == NULL)
862 1.9 christos /* FIXME: Should we report our failure to follow the debuglink ? */
863 1.9 christos return NULL;
864 1.1 christos
865 1.9 christos if (!bfd_check_format (debug_bfd, bfd_object))
866 1.1 christos {
867 1.9 christos bfd_close (debug_bfd);
868 1.1 christos return NULL;
869 1.1 christos }
870 1.9 christos stash->alt.bfd_ptr = debug_bfd;
871 1.1 christos }
872 1.5 christos
873 1.9 christos if (! read_section (unit->stash->alt.bfd_ptr,
874 1.1 christos stash->debug_sections + debug_info_alt,
875 1.9 christos stash->alt.syms, offset,
876 1.9 christos &stash->alt.dwarf_info_buffer,
877 1.9 christos &stash->alt.dwarf_info_size))
878 1.1 christos return NULL;
879 1.1 christos
880 1.9 christos return stash->alt.dwarf_info_buffer + offset;
881 1.1 christos }
882 1.1 christos
883 1.1 christos static bfd_uint64_t
884 1.5 christos read_address (struct comp_unit *unit, bfd_byte *buf, bfd_byte * buf_end)
885 1.1 christos {
886 1.3 christos int signed_vma = 0;
887 1.3 christos
888 1.3 christos if (bfd_get_flavour (unit->abfd) == bfd_target_elf_flavour)
889 1.3 christos signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
890 1.1 christos
891 1.5 christos if (buf + unit->addr_size > buf_end)
892 1.5 christos return 0;
893 1.5 christos
894 1.1 christos if (signed_vma)
895 1.1 christos {
896 1.1 christos switch (unit->addr_size)
897 1.1 christos {
898 1.1 christos case 8:
899 1.1 christos return bfd_get_signed_64 (unit->abfd, buf);
900 1.1 christos case 4:
901 1.1 christos return bfd_get_signed_32 (unit->abfd, buf);
902 1.1 christos case 2:
903 1.1 christos return bfd_get_signed_16 (unit->abfd, buf);
904 1.1 christos default:
905 1.1 christos abort ();
906 1.1 christos }
907 1.1 christos }
908 1.1 christos else
909 1.1 christos {
910 1.1 christos switch (unit->addr_size)
911 1.1 christos {
912 1.1 christos case 8:
913 1.1 christos return bfd_get_64 (unit->abfd, buf);
914 1.1 christos case 4:
915 1.1 christos return bfd_get_32 (unit->abfd, buf);
916 1.1 christos case 2:
917 1.1 christos return bfd_get_16 (unit->abfd, buf);
918 1.1 christos default:
919 1.1 christos abort ();
920 1.1 christos }
921 1.1 christos }
922 1.1 christos }
923 1.1 christos
924 1.1 christos /* Lookup an abbrev_info structure in the abbrev hash table. */
925 1.1 christos
926 1.1 christos static struct abbrev_info *
927 1.1 christos lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
928 1.1 christos {
929 1.1 christos unsigned int hash_number;
930 1.1 christos struct abbrev_info *abbrev;
931 1.1 christos
932 1.1 christos hash_number = number % ABBREV_HASH_SIZE;
933 1.1 christos abbrev = abbrevs[hash_number];
934 1.1 christos
935 1.1 christos while (abbrev)
936 1.1 christos {
937 1.1 christos if (abbrev->number == number)
938 1.1 christos return abbrev;
939 1.1 christos else
940 1.1 christos abbrev = abbrev->next;
941 1.1 christos }
942 1.1 christos
943 1.1 christos return NULL;
944 1.1 christos }
945 1.1 christos
946 1.9 christos /* We keep a hash table to map .debug_abbrev section offsets to the
947 1.9 christos array of abbrevs, so that compilation units using the same set of
948 1.9 christos abbrevs do not waste memory. */
949 1.9 christos
950 1.9 christos struct abbrev_offset_entry
951 1.9 christos {
952 1.9 christos size_t offset;
953 1.9 christos struct abbrev_info **abbrevs;
954 1.9 christos };
955 1.9 christos
956 1.9 christos static hashval_t
957 1.9 christos hash_abbrev (const void *p)
958 1.9 christos {
959 1.9 christos const struct abbrev_offset_entry *ent = p;
960 1.9 christos return htab_hash_pointer ((void *) ent->offset);
961 1.9 christos }
962 1.9 christos
963 1.9 christos static int
964 1.9 christos eq_abbrev (const void *pa, const void *pb)
965 1.9 christos {
966 1.9 christos const struct abbrev_offset_entry *a = pa;
967 1.9 christos const struct abbrev_offset_entry *b = pb;
968 1.9 christos return a->offset == b->offset;
969 1.9 christos }
970 1.9 christos
971 1.9 christos static void
972 1.9 christos del_abbrev (void *p)
973 1.9 christos {
974 1.9 christos struct abbrev_offset_entry *ent = p;
975 1.9 christos struct abbrev_info **abbrevs = ent->abbrevs;
976 1.9 christos size_t i;
977 1.9 christos
978 1.9 christos for (i = 0; i < ABBREV_HASH_SIZE; i++)
979 1.9 christos {
980 1.9 christos struct abbrev_info *abbrev = abbrevs[i];
981 1.9 christos
982 1.9 christos while (abbrev)
983 1.9 christos {
984 1.9 christos free (abbrev->attrs);
985 1.9 christos abbrev = abbrev->next;
986 1.9 christos }
987 1.9 christos }
988 1.9 christos free (ent);
989 1.9 christos }
990 1.9 christos
991 1.1 christos /* In DWARF version 2, the description of the debugging information is
992 1.1 christos stored in a separate .debug_abbrev section. Before we read any
993 1.1 christos dies from a section we read in all abbreviations and install them
994 1.1 christos in a hash table. */
995 1.1 christos
996 1.1 christos static struct abbrev_info**
997 1.9 christos read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash,
998 1.9 christos struct dwarf2_debug_file *file)
999 1.1 christos {
1000 1.1 christos struct abbrev_info **abbrevs;
1001 1.1 christos bfd_byte *abbrev_ptr;
1002 1.5 christos bfd_byte *abbrev_end;
1003 1.1 christos struct abbrev_info *cur_abbrev;
1004 1.1 christos unsigned int abbrev_number, bytes_read, abbrev_name;
1005 1.1 christos unsigned int abbrev_form, hash_number;
1006 1.9 christos size_t amt;
1007 1.9 christos void **slot;
1008 1.9 christos struct abbrev_offset_entry ent = { offset, NULL };
1009 1.9 christos
1010 1.9 christos if (ent.offset != offset)
1011 1.9 christos return NULL;
1012 1.1 christos
1013 1.9 christos slot = htab_find_slot (file->abbrev_offsets, &ent, INSERT);
1014 1.9 christos if (slot == NULL)
1015 1.1 christos return NULL;
1016 1.9 christos if (*slot != NULL)
1017 1.9 christos return ((struct abbrev_offset_entry *) (*slot))->abbrevs;
1018 1.1 christos
1019 1.9 christos if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
1020 1.9 christos file->syms, offset,
1021 1.9 christos &file->dwarf_abbrev_buffer,
1022 1.9 christos &file->dwarf_abbrev_size))
1023 1.5 christos return NULL;
1024 1.5 christos
1025 1.1 christos amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
1026 1.1 christos abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
1027 1.1 christos if (abbrevs == NULL)
1028 1.1 christos return NULL;
1029 1.1 christos
1030 1.9 christos abbrev_ptr = file->dwarf_abbrev_buffer + offset;
1031 1.9 christos abbrev_end = file->dwarf_abbrev_buffer + file->dwarf_abbrev_size;
1032 1.7 christos abbrev_number = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
1033 1.7 christos FALSE, abbrev_end);
1034 1.1 christos abbrev_ptr += bytes_read;
1035 1.1 christos
1036 1.1 christos /* Loop until we reach an abbrev number of 0. */
1037 1.1 christos while (abbrev_number)
1038 1.1 christos {
1039 1.1 christos amt = sizeof (struct abbrev_info);
1040 1.1 christos cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
1041 1.1 christos if (cur_abbrev == NULL)
1042 1.9 christos goto fail;
1043 1.1 christos
1044 1.1 christos /* Read in abbrev header. */
1045 1.1 christos cur_abbrev->number = abbrev_number;
1046 1.1 christos cur_abbrev->tag = (enum dwarf_tag)
1047 1.7 christos _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
1048 1.7 christos FALSE, abbrev_end);
1049 1.1 christos abbrev_ptr += bytes_read;
1050 1.5 christos cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr, abbrev_end);
1051 1.1 christos abbrev_ptr += 1;
1052 1.1 christos
1053 1.1 christos /* Now read in declarations. */
1054 1.8 christos for (;;)
1055 1.8 christos {
1056 1.8 christos /* Initialize it just to avoid a GCC false warning. */
1057 1.8 christos bfd_vma implicit_const = -1;
1058 1.8 christos
1059 1.8 christos abbrev_name = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
1060 1.8 christos FALSE, abbrev_end);
1061 1.8 christos abbrev_ptr += bytes_read;
1062 1.8 christos abbrev_form = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
1063 1.8 christos FALSE, abbrev_end);
1064 1.8 christos abbrev_ptr += bytes_read;
1065 1.8 christos if (abbrev_form == DW_FORM_implicit_const)
1066 1.8 christos {
1067 1.8 christos implicit_const = _bfd_safe_read_leb128 (abfd, abbrev_ptr,
1068 1.8 christos &bytes_read, TRUE,
1069 1.8 christos abbrev_end);
1070 1.8 christos abbrev_ptr += bytes_read;
1071 1.8 christos }
1072 1.8 christos
1073 1.8 christos if (abbrev_name == 0)
1074 1.8 christos break;
1075 1.1 christos
1076 1.1 christos if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
1077 1.1 christos {
1078 1.1 christos struct attr_abbrev *tmp;
1079 1.1 christos
1080 1.1 christos amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
1081 1.1 christos amt *= sizeof (struct attr_abbrev);
1082 1.1 christos tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
1083 1.1 christos if (tmp == NULL)
1084 1.9 christos goto fail;
1085 1.1 christos cur_abbrev->attrs = tmp;
1086 1.1 christos }
1087 1.1 christos
1088 1.1 christos cur_abbrev->attrs[cur_abbrev->num_attrs].name
1089 1.1 christos = (enum dwarf_attribute) abbrev_name;
1090 1.8 christos cur_abbrev->attrs[cur_abbrev->num_attrs].form
1091 1.1 christos = (enum dwarf_form) abbrev_form;
1092 1.8 christos cur_abbrev->attrs[cur_abbrev->num_attrs].implicit_const
1093 1.8 christos = implicit_const;
1094 1.8 christos ++cur_abbrev->num_attrs;
1095 1.1 christos }
1096 1.1 christos
1097 1.1 christos hash_number = abbrev_number % ABBREV_HASH_SIZE;
1098 1.1 christos cur_abbrev->next = abbrevs[hash_number];
1099 1.1 christos abbrevs[hash_number] = cur_abbrev;
1100 1.1 christos
1101 1.1 christos /* Get next abbreviation.
1102 1.1 christos Under Irix6 the abbreviations for a compilation unit are not
1103 1.1 christos always properly terminated with an abbrev number of 0.
1104 1.1 christos Exit loop if we encounter an abbreviation which we have
1105 1.1 christos already read (which means we are about to read the abbreviations
1106 1.1 christos for the next compile unit) or if the end of the abbreviation
1107 1.1 christos table is reached. */
1108 1.9 christos if ((size_t) (abbrev_ptr - file->dwarf_abbrev_buffer)
1109 1.9 christos >= file->dwarf_abbrev_size)
1110 1.1 christos break;
1111 1.7 christos abbrev_number = _bfd_safe_read_leb128 (abfd, abbrev_ptr,
1112 1.7 christos &bytes_read, FALSE, abbrev_end);
1113 1.1 christos abbrev_ptr += bytes_read;
1114 1.5 christos if (lookup_abbrev (abbrev_number, abbrevs) != NULL)
1115 1.1 christos break;
1116 1.1 christos }
1117 1.1 christos
1118 1.9 christos *slot = bfd_malloc (sizeof ent);
1119 1.9 christos if (!*slot)
1120 1.9 christos goto fail;
1121 1.9 christos ent.abbrevs = abbrevs;
1122 1.9 christos memcpy (*slot, &ent, sizeof ent);
1123 1.1 christos return abbrevs;
1124 1.9 christos
1125 1.9 christos fail:
1126 1.9 christos if (abbrevs != NULL)
1127 1.9 christos {
1128 1.9 christos size_t i;
1129 1.9 christos
1130 1.9 christos for (i = 0; i < ABBREV_HASH_SIZE; i++)
1131 1.9 christos {
1132 1.9 christos struct abbrev_info *abbrev = abbrevs[i];
1133 1.9 christos
1134 1.9 christos while (abbrev)
1135 1.9 christos {
1136 1.9 christos free (abbrev->attrs);
1137 1.9 christos abbrev = abbrev->next;
1138 1.9 christos }
1139 1.9 christos }
1140 1.9 christos free (abbrevs);
1141 1.9 christos }
1142 1.9 christos return NULL;
1143 1.1 christos }
1144 1.1 christos
1145 1.3 christos /* Returns true if the form is one which has a string value. */
1146 1.3 christos
1147 1.3 christos static inline bfd_boolean
1148 1.3 christos is_str_attr (enum dwarf_form form)
1149 1.3 christos {
1150 1.8 christos return (form == DW_FORM_string || form == DW_FORM_strp
1151 1.8 christos || form == DW_FORM_line_strp || form == DW_FORM_GNU_strp_alt);
1152 1.3 christos }
1153 1.3 christos
1154 1.5 christos /* Read and fill in the value of attribute ATTR as described by FORM.
1155 1.5 christos Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1156 1.5 christos Returns an updated INFO_PTR taking into account the amount of data read. */
1157 1.1 christos
1158 1.1 christos static bfd_byte *
1159 1.5 christos read_attribute_value (struct attribute * attr,
1160 1.8 christos unsigned form,
1161 1.8 christos bfd_vma implicit_const,
1162 1.5 christos struct comp_unit * unit,
1163 1.8 christos bfd_byte * info_ptr,
1164 1.8 christos bfd_byte * info_ptr_end)
1165 1.1 christos {
1166 1.1 christos bfd *abfd = unit->abfd;
1167 1.1 christos unsigned int bytes_read;
1168 1.1 christos struct dwarf_block *blk;
1169 1.9 christos size_t amt;
1170 1.1 christos
1171 1.6 christos if (info_ptr >= info_ptr_end && form != DW_FORM_flag_present)
1172 1.5 christos {
1173 1.8 christos _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1174 1.5 christos bfd_set_error (bfd_error_bad_value);
1175 1.5 christos return info_ptr;
1176 1.5 christos }
1177 1.5 christos
1178 1.1 christos attr->form = (enum dwarf_form) form;
1179 1.1 christos
1180 1.1 christos switch (form)
1181 1.1 christos {
1182 1.1 christos case DW_FORM_ref_addr:
1183 1.1 christos /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1184 1.1 christos DWARF3. */
1185 1.1 christos if (unit->version == 3 || unit->version == 4)
1186 1.1 christos {
1187 1.1 christos if (unit->offset_size == 4)
1188 1.5 christos attr->u.val = read_4_bytes (unit->abfd, info_ptr, info_ptr_end);
1189 1.1 christos else
1190 1.5 christos attr->u.val = read_8_bytes (unit->abfd, info_ptr, info_ptr_end);
1191 1.1 christos info_ptr += unit->offset_size;
1192 1.1 christos break;
1193 1.1 christos }
1194 1.1 christos /* FALLTHROUGH */
1195 1.1 christos case DW_FORM_addr:
1196 1.5 christos attr->u.val = read_address (unit, info_ptr, info_ptr_end);
1197 1.1 christos info_ptr += unit->addr_size;
1198 1.1 christos break;
1199 1.1 christos case DW_FORM_GNU_ref_alt:
1200 1.1 christos case DW_FORM_sec_offset:
1201 1.1 christos if (unit->offset_size == 4)
1202 1.5 christos attr->u.val = read_4_bytes (unit->abfd, info_ptr, info_ptr_end);
1203 1.1 christos else
1204 1.5 christos attr->u.val = read_8_bytes (unit->abfd, info_ptr, info_ptr_end);
1205 1.1 christos info_ptr += unit->offset_size;
1206 1.1 christos break;
1207 1.1 christos case DW_FORM_block2:
1208 1.1 christos amt = sizeof (struct dwarf_block);
1209 1.1 christos blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1210 1.1 christos if (blk == NULL)
1211 1.1 christos return NULL;
1212 1.5 christos blk->size = read_2_bytes (abfd, info_ptr, info_ptr_end);
1213 1.1 christos info_ptr += 2;
1214 1.8 christos info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
1215 1.1 christos attr->u.blk = blk;
1216 1.1 christos break;
1217 1.1 christos case DW_FORM_block4:
1218 1.1 christos amt = sizeof (struct dwarf_block);
1219 1.1 christos blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1220 1.1 christos if (blk == NULL)
1221 1.1 christos return NULL;
1222 1.5 christos blk->size = read_4_bytes (abfd, info_ptr, info_ptr_end);
1223 1.1 christos info_ptr += 4;
1224 1.8 christos info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
1225 1.1 christos attr->u.blk = blk;
1226 1.1 christos break;
1227 1.1 christos case DW_FORM_data2:
1228 1.5 christos attr->u.val = read_2_bytes (abfd, info_ptr, info_ptr_end);
1229 1.1 christos info_ptr += 2;
1230 1.1 christos break;
1231 1.1 christos case DW_FORM_data4:
1232 1.5 christos attr->u.val = read_4_bytes (abfd, info_ptr, info_ptr_end);
1233 1.1 christos info_ptr += 4;
1234 1.1 christos break;
1235 1.1 christos case DW_FORM_data8:
1236 1.5 christos attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
1237 1.1 christos info_ptr += 8;
1238 1.1 christos break;
1239 1.1 christos case DW_FORM_string:
1240 1.5 christos attr->u.str = read_string (abfd, info_ptr, info_ptr_end, &bytes_read);
1241 1.1 christos info_ptr += bytes_read;
1242 1.1 christos break;
1243 1.1 christos case DW_FORM_strp:
1244 1.5 christos attr->u.str = read_indirect_string (unit, info_ptr, info_ptr_end, &bytes_read);
1245 1.1 christos info_ptr += bytes_read;
1246 1.1 christos break;
1247 1.8 christos case DW_FORM_line_strp:
1248 1.8 christos attr->u.str = read_indirect_line_string (unit, info_ptr, info_ptr_end, &bytes_read);
1249 1.8 christos info_ptr += bytes_read;
1250 1.8 christos break;
1251 1.1 christos case DW_FORM_GNU_strp_alt:
1252 1.5 christos attr->u.str = read_alt_indirect_string (unit, info_ptr, info_ptr_end, &bytes_read);
1253 1.1 christos info_ptr += bytes_read;
1254 1.1 christos break;
1255 1.1 christos case DW_FORM_exprloc:
1256 1.1 christos case DW_FORM_block:
1257 1.1 christos amt = sizeof (struct dwarf_block);
1258 1.1 christos blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1259 1.1 christos if (blk == NULL)
1260 1.1 christos return NULL;
1261 1.7 christos blk->size = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1262 1.7 christos FALSE, info_ptr_end);
1263 1.1 christos info_ptr += bytes_read;
1264 1.8 christos info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
1265 1.1 christos attr->u.blk = blk;
1266 1.1 christos break;
1267 1.1 christos case DW_FORM_block1:
1268 1.1 christos amt = sizeof (struct dwarf_block);
1269 1.1 christos blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1270 1.1 christos if (blk == NULL)
1271 1.1 christos return NULL;
1272 1.5 christos blk->size = read_1_byte (abfd, info_ptr, info_ptr_end);
1273 1.1 christos info_ptr += 1;
1274 1.8 christos info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
1275 1.1 christos attr->u.blk = blk;
1276 1.1 christos break;
1277 1.1 christos case DW_FORM_data1:
1278 1.5 christos attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
1279 1.1 christos info_ptr += 1;
1280 1.1 christos break;
1281 1.1 christos case DW_FORM_flag:
1282 1.5 christos attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
1283 1.1 christos info_ptr += 1;
1284 1.1 christos break;
1285 1.1 christos case DW_FORM_flag_present:
1286 1.1 christos attr->u.val = 1;
1287 1.1 christos break;
1288 1.1 christos case DW_FORM_sdata:
1289 1.7 christos attr->u.sval = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1290 1.7 christos TRUE, info_ptr_end);
1291 1.1 christos info_ptr += bytes_read;
1292 1.1 christos break;
1293 1.1 christos case DW_FORM_udata:
1294 1.7 christos attr->u.val = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1295 1.7 christos FALSE, info_ptr_end);
1296 1.1 christos info_ptr += bytes_read;
1297 1.1 christos break;
1298 1.1 christos case DW_FORM_ref1:
1299 1.5 christos attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
1300 1.1 christos info_ptr += 1;
1301 1.1 christos break;
1302 1.1 christos case DW_FORM_ref2:
1303 1.5 christos attr->u.val = read_2_bytes (abfd, info_ptr, info_ptr_end);
1304 1.1 christos info_ptr += 2;
1305 1.1 christos break;
1306 1.1 christos case DW_FORM_ref4:
1307 1.5 christos attr->u.val = read_4_bytes (abfd, info_ptr, info_ptr_end);
1308 1.1 christos info_ptr += 4;
1309 1.1 christos break;
1310 1.1 christos case DW_FORM_ref8:
1311 1.5 christos attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
1312 1.1 christos info_ptr += 8;
1313 1.1 christos break;
1314 1.1 christos case DW_FORM_ref_sig8:
1315 1.5 christos attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
1316 1.1 christos info_ptr += 8;
1317 1.1 christos break;
1318 1.1 christos case DW_FORM_ref_udata:
1319 1.7 christos attr->u.val = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1320 1.7 christos FALSE, info_ptr_end);
1321 1.1 christos info_ptr += bytes_read;
1322 1.1 christos break;
1323 1.1 christos case DW_FORM_indirect:
1324 1.7 christos form = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1325 1.7 christos FALSE, info_ptr_end);
1326 1.1 christos info_ptr += bytes_read;
1327 1.8 christos if (form == DW_FORM_implicit_const)
1328 1.8 christos {
1329 1.8 christos implicit_const = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1330 1.8 christos TRUE, info_ptr_end);
1331 1.8 christos info_ptr += bytes_read;
1332 1.8 christos }
1333 1.8 christos info_ptr = read_attribute_value (attr, form, implicit_const, unit,
1334 1.8 christos info_ptr, info_ptr_end);
1335 1.8 christos break;
1336 1.8 christos case DW_FORM_implicit_const:
1337 1.8 christos attr->form = DW_FORM_sdata;
1338 1.8 christos attr->u.sval = implicit_const;
1339 1.1 christos break;
1340 1.9 christos case DW_FORM_data16:
1341 1.9 christos /* This is really a "constant", but there is no way to store that
1342 1.9 christos so pretend it is a 16 byte block instead. */
1343 1.9 christos amt = sizeof (struct dwarf_block);
1344 1.9 christos blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1345 1.9 christos if (blk == NULL)
1346 1.9 christos return NULL;
1347 1.9 christos blk->size = 16;
1348 1.9 christos info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
1349 1.9 christos attr->u.blk = blk;
1350 1.9 christos break;
1351 1.1 christos default:
1352 1.8 christos _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1353 1.7 christos form);
1354 1.1 christos bfd_set_error (bfd_error_bad_value);
1355 1.1 christos return NULL;
1356 1.1 christos }
1357 1.1 christos return info_ptr;
1358 1.1 christos }
1359 1.1 christos
1360 1.1 christos /* Read an attribute described by an abbreviated attribute. */
1361 1.1 christos
1362 1.1 christos static bfd_byte *
1363 1.5 christos read_attribute (struct attribute * attr,
1364 1.5 christos struct attr_abbrev * abbrev,
1365 1.5 christos struct comp_unit * unit,
1366 1.8 christos bfd_byte * info_ptr,
1367 1.8 christos bfd_byte * info_ptr_end)
1368 1.1 christos {
1369 1.1 christos attr->name = abbrev->name;
1370 1.8 christos info_ptr = read_attribute_value (attr, abbrev->form, abbrev->implicit_const,
1371 1.8 christos unit, info_ptr, info_ptr_end);
1372 1.1 christos return info_ptr;
1373 1.1 christos }
1374 1.1 christos
1375 1.3 christos /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1376 1.3 christos for a function. */
1377 1.3 christos
1378 1.3 christos static bfd_boolean
1379 1.3 christos non_mangled (int lang)
1380 1.3 christos {
1381 1.3 christos switch (lang)
1382 1.3 christos {
1383 1.3 christos default:
1384 1.3 christos return FALSE;
1385 1.3 christos
1386 1.3 christos case DW_LANG_C89:
1387 1.3 christos case DW_LANG_C:
1388 1.3 christos case DW_LANG_Ada83:
1389 1.3 christos case DW_LANG_Cobol74:
1390 1.3 christos case DW_LANG_Cobol85:
1391 1.3 christos case DW_LANG_Fortran77:
1392 1.3 christos case DW_LANG_Pascal83:
1393 1.3 christos case DW_LANG_C99:
1394 1.3 christos case DW_LANG_Ada95:
1395 1.3 christos case DW_LANG_PLI:
1396 1.3 christos case DW_LANG_UPC:
1397 1.3 christos case DW_LANG_C11:
1398 1.3 christos return TRUE;
1399 1.3 christos }
1400 1.3 christos }
1401 1.3 christos
1402 1.1 christos /* Source line information table routines. */
1403 1.1 christos
1404 1.1 christos #define FILE_ALLOC_CHUNK 5
1405 1.1 christos #define DIR_ALLOC_CHUNK 5
1406 1.1 christos
1407 1.1 christos struct line_info
1408 1.1 christos {
1409 1.7 christos struct line_info * prev_line;
1410 1.7 christos bfd_vma address;
1411 1.7 christos char * filename;
1412 1.7 christos unsigned int line;
1413 1.7 christos unsigned int column;
1414 1.7 christos unsigned int discriminator;
1415 1.7 christos unsigned char op_index;
1416 1.7 christos unsigned char end_sequence; /* End of (sequential) code sequence. */
1417 1.1 christos };
1418 1.1 christos
1419 1.1 christos struct fileinfo
1420 1.1 christos {
1421 1.7 christos char * name;
1422 1.7 christos unsigned int dir;
1423 1.7 christos unsigned int time;
1424 1.7 christos unsigned int size;
1425 1.1 christos };
1426 1.1 christos
1427 1.1 christos struct line_sequence
1428 1.1 christos {
1429 1.8 christos bfd_vma low_pc;
1430 1.1 christos struct line_sequence* prev_sequence;
1431 1.8 christos struct line_info* last_line; /* Largest VMA. */
1432 1.8 christos struct line_info** line_info_lookup;
1433 1.7 christos bfd_size_type num_lines;
1434 1.1 christos };
1435 1.1 christos
1436 1.1 christos struct line_info_table
1437 1.1 christos {
1438 1.8 christos bfd * abfd;
1439 1.8 christos unsigned int num_files;
1440 1.8 christos unsigned int num_dirs;
1441 1.8 christos unsigned int num_sequences;
1442 1.8 christos char * comp_dir;
1443 1.8 christos char ** dirs;
1444 1.8 christos struct fileinfo* files;
1445 1.1 christos struct line_sequence* sequences;
1446 1.8 christos struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
1447 1.1 christos };
1448 1.1 christos
1449 1.1 christos /* Remember some information about each function. If the function is
1450 1.1 christos inlined (DW_TAG_inlined_subroutine) it may have two additional
1451 1.1 christos attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1452 1.1 christos source code location where this function was inlined. */
1453 1.1 christos
1454 1.1 christos struct funcinfo
1455 1.1 christos {
1456 1.1 christos /* Pointer to previous function in list of all functions. */
1457 1.7 christos struct funcinfo * prev_func;
1458 1.1 christos /* Pointer to function one scope higher. */
1459 1.7 christos struct funcinfo * caller_func;
1460 1.1 christos /* Source location file name where caller_func inlines this func. */
1461 1.7 christos char * caller_file;
1462 1.3 christos /* Source location file name. */
1463 1.7 christos char * file;
1464 1.1 christos /* Source location line number where caller_func inlines this func. */
1465 1.7 christos int caller_line;
1466 1.1 christos /* Source location line number. */
1467 1.7 christos int line;
1468 1.7 christos int tag;
1469 1.7 christos bfd_boolean is_linkage;
1470 1.7 christos const char * name;
1471 1.7 christos struct arange arange;
1472 1.1 christos /* Where the symbol is defined. */
1473 1.7 christos asection * sec;
1474 1.7 christos };
1475 1.7 christos
1476 1.7 christos struct lookup_funcinfo
1477 1.7 christos {
1478 1.7 christos /* Function information corresponding to this lookup table entry. */
1479 1.7 christos struct funcinfo * funcinfo;
1480 1.7 christos
1481 1.7 christos /* The lowest address for this specific function. */
1482 1.8 christos bfd_vma low_addr;
1483 1.7 christos
1484 1.7 christos /* The highest address of this function before the lookup table is sorted.
1485 1.7 christos The highest address of all prior functions after the lookup table is
1486 1.7 christos sorted, which is used for binary search. */
1487 1.8 christos bfd_vma high_addr;
1488 1.9 christos /* Index of this function, used to ensure qsort is stable. */
1489 1.9 christos unsigned int idx;
1490 1.1 christos };
1491 1.1 christos
1492 1.1 christos struct varinfo
1493 1.1 christos {
1494 1.9 christos /* Pointer to previous variable in list of all variables. */
1495 1.1 christos struct varinfo *prev_var;
1496 1.9 christos /* The offset of the varinfo from the start of the unit. */
1497 1.9 christos bfd_uint64_t unit_offset;
1498 1.9 christos /* Source location file name. */
1499 1.1 christos char *file;
1500 1.9 christos /* Source location line number. */
1501 1.1 christos int line;
1502 1.9 christos /* The type of this variable. */
1503 1.1 christos int tag;
1504 1.9 christos /* The name of the variable, if it has one. */
1505 1.1 christos char *name;
1506 1.9 christos /* The address of the variable. */
1507 1.1 christos bfd_vma addr;
1508 1.9 christos /* Where the symbol is defined. */
1509 1.1 christos asection *sec;
1510 1.9 christos /* Is this a stack variable? */
1511 1.9 christos bfd_boolean stack;
1512 1.1 christos };
1513 1.1 christos
1514 1.1 christos /* Return TRUE if NEW_LINE should sort after LINE. */
1515 1.1 christos
1516 1.1 christos static inline bfd_boolean
1517 1.1 christos new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1518 1.1 christos {
1519 1.1 christos return (new_line->address > line->address
1520 1.1 christos || (new_line->address == line->address
1521 1.8 christos && new_line->op_index > line->op_index));
1522 1.1 christos }
1523 1.1 christos
1524 1.1 christos
1525 1.1 christos /* Adds a new entry to the line_info list in the line_info_table, ensuring
1526 1.1 christos that the list is sorted. Note that the line_info list is sorted from
1527 1.1 christos highest to lowest VMA (with possible duplicates); that is,
1528 1.1 christos line_info->prev_line always accesses an equal or smaller VMA. */
1529 1.1 christos
1530 1.1 christos static bfd_boolean
1531 1.1 christos add_line_info (struct line_info_table *table,
1532 1.1 christos bfd_vma address,
1533 1.1 christos unsigned char op_index,
1534 1.1 christos char *filename,
1535 1.1 christos unsigned int line,
1536 1.1 christos unsigned int column,
1537 1.1 christos unsigned int discriminator,
1538 1.1 christos int end_sequence)
1539 1.1 christos {
1540 1.9 christos size_t amt = sizeof (struct line_info);
1541 1.1 christos struct line_sequence* seq = table->sequences;
1542 1.1 christos struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
1543 1.1 christos
1544 1.1 christos if (info == NULL)
1545 1.1 christos return FALSE;
1546 1.1 christos
1547 1.1 christos /* Set member data of 'info'. */
1548 1.1 christos info->prev_line = NULL;
1549 1.1 christos info->address = address;
1550 1.1 christos info->op_index = op_index;
1551 1.1 christos info->line = line;
1552 1.1 christos info->column = column;
1553 1.1 christos info->discriminator = discriminator;
1554 1.1 christos info->end_sequence = end_sequence;
1555 1.1 christos
1556 1.1 christos if (filename && filename[0])
1557 1.1 christos {
1558 1.1 christos info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1559 1.1 christos if (info->filename == NULL)
1560 1.1 christos return FALSE;
1561 1.1 christos strcpy (info->filename, filename);
1562 1.1 christos }
1563 1.1 christos else
1564 1.1 christos info->filename = NULL;
1565 1.1 christos
1566 1.1 christos /* Find the correct location for 'info'. Normally we will receive
1567 1.1 christos new line_info data 1) in order and 2) with increasing VMAs.
1568 1.1 christos However some compilers break the rules (cf. decode_line_info) and
1569 1.1 christos so we include some heuristics for quickly finding the correct
1570 1.1 christos location for 'info'. In particular, these heuristics optimize for
1571 1.1 christos the common case in which the VMA sequence that we receive is a
1572 1.1 christos list of locally sorted VMAs such as
1573 1.1 christos p...z a...j (where a < j < p < z)
1574 1.1 christos
1575 1.1 christos Note: table->lcl_head is used to head an *actual* or *possible*
1576 1.1 christos sub-sequence within the list (such as a...j) that is not directly
1577 1.1 christos headed by table->last_line
1578 1.1 christos
1579 1.1 christos Note: we may receive duplicate entries from 'decode_line_info'. */
1580 1.1 christos
1581 1.1 christos if (seq
1582 1.1 christos && seq->last_line->address == address
1583 1.1 christos && seq->last_line->op_index == op_index
1584 1.1 christos && seq->last_line->end_sequence == end_sequence)
1585 1.1 christos {
1586 1.1 christos /* We only keep the last entry with the same address and end
1587 1.1 christos sequence. See PR ld/4986. */
1588 1.1 christos if (table->lcl_head == seq->last_line)
1589 1.1 christos table->lcl_head = info;
1590 1.1 christos info->prev_line = seq->last_line->prev_line;
1591 1.1 christos seq->last_line = info;
1592 1.1 christos }
1593 1.1 christos else if (!seq || seq->last_line->end_sequence)
1594 1.1 christos {
1595 1.1 christos /* Start a new line sequence. */
1596 1.1 christos amt = sizeof (struct line_sequence);
1597 1.1 christos seq = (struct line_sequence *) bfd_malloc (amt);
1598 1.1 christos if (seq == NULL)
1599 1.1 christos return FALSE;
1600 1.1 christos seq->low_pc = address;
1601 1.1 christos seq->prev_sequence = table->sequences;
1602 1.1 christos seq->last_line = info;
1603 1.1 christos table->lcl_head = info;
1604 1.1 christos table->sequences = seq;
1605 1.1 christos table->num_sequences++;
1606 1.1 christos }
1607 1.8 christos else if (info->end_sequence
1608 1.8 christos || new_line_sorts_after (info, seq->last_line))
1609 1.1 christos {
1610 1.1 christos /* Normal case: add 'info' to the beginning of the current sequence. */
1611 1.1 christos info->prev_line = seq->last_line;
1612 1.1 christos seq->last_line = info;
1613 1.1 christos
1614 1.1 christos /* lcl_head: initialize to head a *possible* sequence at the end. */
1615 1.1 christos if (!table->lcl_head)
1616 1.1 christos table->lcl_head = info;
1617 1.1 christos }
1618 1.1 christos else if (!new_line_sorts_after (info, table->lcl_head)
1619 1.1 christos && (!table->lcl_head->prev_line
1620 1.1 christos || new_line_sorts_after (info, table->lcl_head->prev_line)))
1621 1.1 christos {
1622 1.1 christos /* Abnormal but easy: lcl_head is the head of 'info'. */
1623 1.1 christos info->prev_line = table->lcl_head->prev_line;
1624 1.1 christos table->lcl_head->prev_line = info;
1625 1.1 christos }
1626 1.1 christos else
1627 1.1 christos {
1628 1.1 christos /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1629 1.1 christos are valid heads for 'info'. Reset 'lcl_head'. */
1630 1.1 christos struct line_info* li2 = seq->last_line; /* Always non-NULL. */
1631 1.1 christos struct line_info* li1 = li2->prev_line;
1632 1.1 christos
1633 1.1 christos while (li1)
1634 1.1 christos {
1635 1.1 christos if (!new_line_sorts_after (info, li2)
1636 1.1 christos && new_line_sorts_after (info, li1))
1637 1.1 christos break;
1638 1.1 christos
1639 1.1 christos li2 = li1; /* always non-NULL */
1640 1.1 christos li1 = li1->prev_line;
1641 1.1 christos }
1642 1.1 christos table->lcl_head = li2;
1643 1.1 christos info->prev_line = table->lcl_head->prev_line;
1644 1.1 christos table->lcl_head->prev_line = info;
1645 1.1 christos if (address < seq->low_pc)
1646 1.3 christos seq->low_pc = address;
1647 1.1 christos }
1648 1.1 christos return TRUE;
1649 1.1 christos }
1650 1.1 christos
1651 1.1 christos /* Extract a fully qualified filename from a line info table.
1652 1.1 christos The returned string has been malloc'ed and it is the caller's
1653 1.1 christos responsibility to free it. */
1654 1.1 christos
1655 1.1 christos static char *
1656 1.1 christos concat_filename (struct line_info_table *table, unsigned int file)
1657 1.1 christos {
1658 1.1 christos char *filename;
1659 1.1 christos
1660 1.8 christos if (table == NULL || file - 1 >= table->num_files)
1661 1.1 christos {
1662 1.1 christos /* FILE == 0 means unknown. */
1663 1.1 christos if (file)
1664 1.7 christos _bfd_error_handler
1665 1.8 christos (_("DWARF error: mangled line number section (bad file number)"));
1666 1.1 christos return strdup ("<unknown>");
1667 1.1 christos }
1668 1.1 christos
1669 1.1 christos filename = table->files[file - 1].name;
1670 1.8 christos if (filename == NULL)
1671 1.8 christos return strdup ("<unknown>");
1672 1.1 christos
1673 1.1 christos if (!IS_ABSOLUTE_PATH (filename))
1674 1.1 christos {
1675 1.1 christos char *dir_name = NULL;
1676 1.1 christos char *subdir_name = NULL;
1677 1.1 christos char *name;
1678 1.1 christos size_t len;
1679 1.1 christos
1680 1.5 christos if (table->files[file - 1].dir
1681 1.5 christos /* PR 17512: file: 0317e960. */
1682 1.5 christos && table->files[file - 1].dir <= table->num_dirs
1683 1.5 christos /* PR 17512: file: 7f3d2e4b. */
1684 1.5 christos && table->dirs != NULL)
1685 1.1 christos subdir_name = table->dirs[table->files[file - 1].dir - 1];
1686 1.1 christos
1687 1.1 christos if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
1688 1.1 christos dir_name = table->comp_dir;
1689 1.1 christos
1690 1.1 christos if (!dir_name)
1691 1.1 christos {
1692 1.1 christos dir_name = subdir_name;
1693 1.1 christos subdir_name = NULL;
1694 1.1 christos }
1695 1.1 christos
1696 1.1 christos if (!dir_name)
1697 1.1 christos return strdup (filename);
1698 1.1 christos
1699 1.1 christos len = strlen (dir_name) + strlen (filename) + 2;
1700 1.1 christos
1701 1.1 christos if (subdir_name)
1702 1.1 christos {
1703 1.1 christos len += strlen (subdir_name) + 1;
1704 1.1 christos name = (char *) bfd_malloc (len);
1705 1.1 christos if (name)
1706 1.1 christos sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
1707 1.1 christos }
1708 1.1 christos else
1709 1.1 christos {
1710 1.1 christos name = (char *) bfd_malloc (len);
1711 1.1 christos if (name)
1712 1.1 christos sprintf (name, "%s/%s", dir_name, filename);
1713 1.1 christos }
1714 1.1 christos
1715 1.1 christos return name;
1716 1.1 christos }
1717 1.1 christos
1718 1.1 christos return strdup (filename);
1719 1.1 christos }
1720 1.1 christos
1721 1.1 christos static bfd_boolean
1722 1.1 christos arange_add (const struct comp_unit *unit, struct arange *first_arange,
1723 1.1 christos bfd_vma low_pc, bfd_vma high_pc)
1724 1.1 christos {
1725 1.1 christos struct arange *arange;
1726 1.1 christos
1727 1.1 christos /* Ignore empty ranges. */
1728 1.1 christos if (low_pc == high_pc)
1729 1.1 christos return TRUE;
1730 1.1 christos
1731 1.1 christos /* If the first arange is empty, use it. */
1732 1.1 christos if (first_arange->high == 0)
1733 1.1 christos {
1734 1.1 christos first_arange->low = low_pc;
1735 1.1 christos first_arange->high = high_pc;
1736 1.1 christos return TRUE;
1737 1.1 christos }
1738 1.1 christos
1739 1.1 christos /* Next see if we can cheaply extend an existing range. */
1740 1.1 christos arange = first_arange;
1741 1.1 christos do
1742 1.1 christos {
1743 1.1 christos if (low_pc == arange->high)
1744 1.1 christos {
1745 1.1 christos arange->high = high_pc;
1746 1.1 christos return TRUE;
1747 1.1 christos }
1748 1.1 christos if (high_pc == arange->low)
1749 1.1 christos {
1750 1.1 christos arange->low = low_pc;
1751 1.1 christos return TRUE;
1752 1.1 christos }
1753 1.1 christos arange = arange->next;
1754 1.1 christos }
1755 1.1 christos while (arange);
1756 1.1 christos
1757 1.1 christos /* Need to allocate a new arange and insert it into the arange list.
1758 1.7 christos Order isn't significant, so just insert after the first arange. */
1759 1.1 christos arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
1760 1.1 christos if (arange == NULL)
1761 1.1 christos return FALSE;
1762 1.1 christos arange->low = low_pc;
1763 1.1 christos arange->high = high_pc;
1764 1.1 christos arange->next = first_arange->next;
1765 1.1 christos first_arange->next = arange;
1766 1.1 christos return TRUE;
1767 1.1 christos }
1768 1.1 christos
1769 1.1 christos /* Compare function for line sequences. */
1770 1.1 christos
1771 1.1 christos static int
1772 1.1 christos compare_sequences (const void* a, const void* b)
1773 1.1 christos {
1774 1.1 christos const struct line_sequence* seq1 = a;
1775 1.1 christos const struct line_sequence* seq2 = b;
1776 1.1 christos
1777 1.1 christos /* Sort by low_pc as the primary key. */
1778 1.1 christos if (seq1->low_pc < seq2->low_pc)
1779 1.1 christos return -1;
1780 1.1 christos if (seq1->low_pc > seq2->low_pc)
1781 1.1 christos return 1;
1782 1.1 christos
1783 1.1 christos /* If low_pc values are equal, sort in reverse order of
1784 1.1 christos high_pc, so that the largest region comes first. */
1785 1.1 christos if (seq1->last_line->address < seq2->last_line->address)
1786 1.1 christos return 1;
1787 1.1 christos if (seq1->last_line->address > seq2->last_line->address)
1788 1.1 christos return -1;
1789 1.1 christos
1790 1.1 christos if (seq1->last_line->op_index < seq2->last_line->op_index)
1791 1.1 christos return 1;
1792 1.1 christos if (seq1->last_line->op_index > seq2->last_line->op_index)
1793 1.1 christos return -1;
1794 1.1 christos
1795 1.9 christos /* num_lines is initially an index, to make the sort stable. */
1796 1.9 christos if (seq1->num_lines < seq2->num_lines)
1797 1.9 christos return -1;
1798 1.9 christos if (seq1->num_lines > seq2->num_lines)
1799 1.9 christos return 1;
1800 1.1 christos return 0;
1801 1.1 christos }
1802 1.1 christos
1803 1.7 christos /* Construct the line information table for quick lookup. */
1804 1.7 christos
1805 1.7 christos static bfd_boolean
1806 1.7 christos build_line_info_table (struct line_info_table * table,
1807 1.7 christos struct line_sequence * seq)
1808 1.7 christos {
1809 1.9 christos size_t amt;
1810 1.9 christos struct line_info **line_info_lookup;
1811 1.9 christos struct line_info *each_line;
1812 1.9 christos unsigned int num_lines;
1813 1.9 christos unsigned int line_index;
1814 1.7 christos
1815 1.7 christos if (seq->line_info_lookup != NULL)
1816 1.7 christos return TRUE;
1817 1.7 christos
1818 1.7 christos /* Count the number of line information entries. We could do this while
1819 1.7 christos scanning the debug information, but some entries may be added via
1820 1.7 christos lcl_head without having a sequence handy to increment the number of
1821 1.7 christos lines. */
1822 1.7 christos num_lines = 0;
1823 1.7 christos for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
1824 1.7 christos num_lines++;
1825 1.7 christos
1826 1.9 christos seq->num_lines = num_lines;
1827 1.7 christos if (num_lines == 0)
1828 1.7 christos return TRUE;
1829 1.7 christos
1830 1.7 christos /* Allocate space for the line information lookup table. */
1831 1.7 christos amt = sizeof (struct line_info*) * num_lines;
1832 1.7 christos line_info_lookup = (struct line_info**) bfd_alloc (table->abfd, amt);
1833 1.9 christos seq->line_info_lookup = line_info_lookup;
1834 1.7 christos if (line_info_lookup == NULL)
1835 1.7 christos return FALSE;
1836 1.7 christos
1837 1.7 christos /* Create the line information lookup table. */
1838 1.7 christos line_index = num_lines;
1839 1.7 christos for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
1840 1.7 christos line_info_lookup[--line_index] = each_line;
1841 1.7 christos
1842 1.7 christos BFD_ASSERT (line_index == 0);
1843 1.7 christos return TRUE;
1844 1.7 christos }
1845 1.7 christos
1846 1.1 christos /* Sort the line sequences for quick lookup. */
1847 1.1 christos
1848 1.1 christos static bfd_boolean
1849 1.1 christos sort_line_sequences (struct line_info_table* table)
1850 1.1 christos {
1851 1.9 christos size_t amt;
1852 1.9 christos struct line_sequence *sequences;
1853 1.9 christos struct line_sequence *seq;
1854 1.9 christos unsigned int n = 0;
1855 1.9 christos unsigned int num_sequences = table->num_sequences;
1856 1.9 christos bfd_vma last_high_pc;
1857 1.1 christos
1858 1.1 christos if (num_sequences == 0)
1859 1.1 christos return TRUE;
1860 1.1 christos
1861 1.1 christos /* Allocate space for an array of sequences. */
1862 1.1 christos amt = sizeof (struct line_sequence) * num_sequences;
1863 1.1 christos sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
1864 1.1 christos if (sequences == NULL)
1865 1.1 christos return FALSE;
1866 1.1 christos
1867 1.1 christos /* Copy the linked list into the array, freeing the original nodes. */
1868 1.1 christos seq = table->sequences;
1869 1.1 christos for (n = 0; n < num_sequences; n++)
1870 1.1 christos {
1871 1.1 christos struct line_sequence* last_seq = seq;
1872 1.1 christos
1873 1.1 christos BFD_ASSERT (seq);
1874 1.1 christos sequences[n].low_pc = seq->low_pc;
1875 1.1 christos sequences[n].prev_sequence = NULL;
1876 1.1 christos sequences[n].last_line = seq->last_line;
1877 1.7 christos sequences[n].line_info_lookup = NULL;
1878 1.9 christos sequences[n].num_lines = n;
1879 1.1 christos seq = seq->prev_sequence;
1880 1.1 christos free (last_seq);
1881 1.1 christos }
1882 1.1 christos BFD_ASSERT (seq == NULL);
1883 1.1 christos
1884 1.1 christos qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
1885 1.1 christos
1886 1.1 christos /* Make the list binary-searchable by trimming overlapping entries
1887 1.1 christos and removing nested entries. */
1888 1.1 christos num_sequences = 1;
1889 1.1 christos last_high_pc = sequences[0].last_line->address;
1890 1.1 christos for (n = 1; n < table->num_sequences; n++)
1891 1.1 christos {
1892 1.1 christos if (sequences[n].low_pc < last_high_pc)
1893 1.3 christos {
1894 1.1 christos if (sequences[n].last_line->address <= last_high_pc)
1895 1.1 christos /* Skip nested entries. */
1896 1.1 christos continue;
1897 1.1 christos
1898 1.1 christos /* Trim overlapping entries. */
1899 1.1 christos sequences[n].low_pc = last_high_pc;
1900 1.3 christos }
1901 1.1 christos last_high_pc = sequences[n].last_line->address;
1902 1.1 christos if (n > num_sequences)
1903 1.3 christos {
1904 1.3 christos /* Close up the gap. */
1905 1.3 christos sequences[num_sequences].low_pc = sequences[n].low_pc;
1906 1.3 christos sequences[num_sequences].last_line = sequences[n].last_line;
1907 1.3 christos }
1908 1.1 christos num_sequences++;
1909 1.1 christos }
1910 1.1 christos
1911 1.1 christos table->sequences = sequences;
1912 1.1 christos table->num_sequences = num_sequences;
1913 1.1 christos return TRUE;
1914 1.1 christos }
1915 1.1 christos
1916 1.8 christos /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
1917 1.8 christos
1918 1.8 christos static bfd_boolean
1919 1.8 christos line_info_add_include_dir (struct line_info_table *table, char *cur_dir)
1920 1.8 christos {
1921 1.8 christos if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1922 1.8 christos {
1923 1.8 christos char **tmp;
1924 1.9 christos size_t amt;
1925 1.8 christos
1926 1.8 christos amt = table->num_dirs + DIR_ALLOC_CHUNK;
1927 1.8 christos amt *= sizeof (char *);
1928 1.8 christos
1929 1.8 christos tmp = (char **) bfd_realloc (table->dirs, amt);
1930 1.8 christos if (tmp == NULL)
1931 1.8 christos return FALSE;
1932 1.8 christos table->dirs = tmp;
1933 1.8 christos }
1934 1.8 christos
1935 1.8 christos table->dirs[table->num_dirs++] = cur_dir;
1936 1.8 christos return TRUE;
1937 1.8 christos }
1938 1.8 christos
1939 1.8 christos static bfd_boolean
1940 1.8 christos line_info_add_include_dir_stub (struct line_info_table *table, char *cur_dir,
1941 1.8 christos unsigned int dir ATTRIBUTE_UNUSED,
1942 1.8 christos unsigned int xtime ATTRIBUTE_UNUSED,
1943 1.8 christos unsigned int size ATTRIBUTE_UNUSED)
1944 1.8 christos {
1945 1.8 christos return line_info_add_include_dir (table, cur_dir);
1946 1.8 christos }
1947 1.8 christos
1948 1.8 christos /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
1949 1.8 christos
1950 1.8 christos static bfd_boolean
1951 1.8 christos line_info_add_file_name (struct line_info_table *table, char *cur_file,
1952 1.8 christos unsigned int dir, unsigned int xtime,
1953 1.8 christos unsigned int size)
1954 1.8 christos {
1955 1.8 christos if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1956 1.8 christos {
1957 1.8 christos struct fileinfo *tmp;
1958 1.9 christos size_t amt;
1959 1.8 christos
1960 1.8 christos amt = table->num_files + FILE_ALLOC_CHUNK;
1961 1.8 christos amt *= sizeof (struct fileinfo);
1962 1.8 christos
1963 1.8 christos tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1964 1.8 christos if (tmp == NULL)
1965 1.8 christos return FALSE;
1966 1.8 christos table->files = tmp;
1967 1.8 christos }
1968 1.8 christos
1969 1.8 christos table->files[table->num_files].name = cur_file;
1970 1.8 christos table->files[table->num_files].dir = dir;
1971 1.8 christos table->files[table->num_files].time = xtime;
1972 1.8 christos table->files[table->num_files].size = size;
1973 1.8 christos table->num_files++;
1974 1.8 christos return TRUE;
1975 1.8 christos }
1976 1.8 christos
1977 1.8 christos /* Read directory or file name entry format, starting with byte of
1978 1.8 christos format count entries, ULEB128 pairs of entry formats, ULEB128 of
1979 1.8 christos entries count and the entries themselves in the described entry
1980 1.8 christos format. */
1981 1.8 christos
1982 1.8 christos static bfd_boolean
1983 1.8 christos read_formatted_entries (struct comp_unit *unit, bfd_byte **bufp,
1984 1.8 christos bfd_byte *buf_end, struct line_info_table *table,
1985 1.8 christos bfd_boolean (*callback) (struct line_info_table *table,
1986 1.8 christos char *cur_file,
1987 1.8 christos unsigned int dir,
1988 1.8 christos unsigned int time,
1989 1.8 christos unsigned int size))
1990 1.8 christos {
1991 1.8 christos bfd *abfd = unit->abfd;
1992 1.8 christos bfd_byte format_count, formati;
1993 1.8 christos bfd_vma data_count, datai;
1994 1.8 christos bfd_byte *buf = *bufp;
1995 1.8 christos bfd_byte *format_header_data;
1996 1.8 christos unsigned int bytes_read;
1997 1.8 christos
1998 1.8 christos format_count = read_1_byte (abfd, buf, buf_end);
1999 1.8 christos buf += 1;
2000 1.8 christos format_header_data = buf;
2001 1.8 christos for (formati = 0; formati < format_count; formati++)
2002 1.8 christos {
2003 1.8 christos _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
2004 1.8 christos buf += bytes_read;
2005 1.8 christos _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
2006 1.8 christos buf += bytes_read;
2007 1.8 christos }
2008 1.8 christos
2009 1.8 christos data_count = _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
2010 1.8 christos buf += bytes_read;
2011 1.8 christos if (format_count == 0 && data_count != 0)
2012 1.8 christos {
2013 1.8 christos _bfd_error_handler (_("DWARF error: zero format count"));
2014 1.8 christos bfd_set_error (bfd_error_bad_value);
2015 1.8 christos return FALSE;
2016 1.8 christos }
2017 1.8 christos
2018 1.8 christos /* PR 22210. Paranoia check. Don't bother running the loop
2019 1.8 christos if we know that we are going to run out of buffer. */
2020 1.8 christos if (data_count > (bfd_vma) (buf_end - buf))
2021 1.8 christos {
2022 1.8 christos _bfd_error_handler
2023 1.8 christos (_("DWARF error: data count (%" PRIx64 ") larger than buffer size"),
2024 1.8 christos (uint64_t) data_count);
2025 1.8 christos bfd_set_error (bfd_error_bad_value);
2026 1.8 christos return FALSE;
2027 1.8 christos }
2028 1.8 christos
2029 1.8 christos for (datai = 0; datai < data_count; datai++)
2030 1.8 christos {
2031 1.8 christos bfd_byte *format = format_header_data;
2032 1.8 christos struct fileinfo fe;
2033 1.8 christos
2034 1.8 christos memset (&fe, 0, sizeof fe);
2035 1.8 christos for (formati = 0; formati < format_count; formati++)
2036 1.8 christos {
2037 1.8 christos bfd_vma content_type, form;
2038 1.8 christos char *string_trash;
2039 1.8 christos char **stringp = &string_trash;
2040 1.8 christos unsigned int uint_trash, *uintp = &uint_trash;
2041 1.8 christos struct attribute attr;
2042 1.8 christos
2043 1.8 christos content_type = _bfd_safe_read_leb128 (abfd, format, &bytes_read,
2044 1.8 christos FALSE, buf_end);
2045 1.8 christos format += bytes_read;
2046 1.8 christos switch (content_type)
2047 1.8 christos {
2048 1.8 christos case DW_LNCT_path:
2049 1.8 christos stringp = &fe.name;
2050 1.8 christos break;
2051 1.8 christos case DW_LNCT_directory_index:
2052 1.8 christos uintp = &fe.dir;
2053 1.8 christos break;
2054 1.8 christos case DW_LNCT_timestamp:
2055 1.8 christos uintp = &fe.time;
2056 1.8 christos break;
2057 1.8 christos case DW_LNCT_size:
2058 1.8 christos uintp = &fe.size;
2059 1.8 christos break;
2060 1.8 christos case DW_LNCT_MD5:
2061 1.8 christos break;
2062 1.8 christos default:
2063 1.8 christos _bfd_error_handler
2064 1.8 christos (_("DWARF error: unknown format content type %" PRIu64),
2065 1.8 christos (uint64_t) content_type);
2066 1.8 christos bfd_set_error (bfd_error_bad_value);
2067 1.8 christos return FALSE;
2068 1.8 christos }
2069 1.8 christos
2070 1.8 christos form = _bfd_safe_read_leb128 (abfd, format, &bytes_read, FALSE,
2071 1.8 christos buf_end);
2072 1.8 christos format += bytes_read;
2073 1.8 christos
2074 1.8 christos buf = read_attribute_value (&attr, form, 0, unit, buf, buf_end);
2075 1.8 christos if (buf == NULL)
2076 1.8 christos return FALSE;
2077 1.8 christos switch (form)
2078 1.8 christos {
2079 1.8 christos case DW_FORM_string:
2080 1.8 christos case DW_FORM_line_strp:
2081 1.8 christos *stringp = attr.u.str;
2082 1.8 christos break;
2083 1.8 christos
2084 1.8 christos case DW_FORM_data1:
2085 1.8 christos case DW_FORM_data2:
2086 1.8 christos case DW_FORM_data4:
2087 1.8 christos case DW_FORM_data8:
2088 1.8 christos case DW_FORM_udata:
2089 1.8 christos *uintp = attr.u.val;
2090 1.8 christos break;
2091 1.9 christos
2092 1.9 christos case DW_FORM_data16:
2093 1.9 christos /* MD5 data is in the attr.blk, but we are ignoring those. */
2094 1.9 christos break;
2095 1.8 christos }
2096 1.8 christos }
2097 1.8 christos
2098 1.9 christos /* Skip the first "zero entry", which is the compilation dir/file. */
2099 1.9 christos if (datai != 0)
2100 1.9 christos if (!callback (table, fe.name, fe.dir, fe.time, fe.size))
2101 1.9 christos return FALSE;
2102 1.8 christos }
2103 1.8 christos
2104 1.8 christos *bufp = buf;
2105 1.8 christos return TRUE;
2106 1.8 christos }
2107 1.8 christos
2108 1.1 christos /* Decode the line number information for UNIT. */
2109 1.1 christos
2110 1.1 christos static struct line_info_table*
2111 1.9 christos decode_line_info (struct comp_unit *unit)
2112 1.1 christos {
2113 1.1 christos bfd *abfd = unit->abfd;
2114 1.9 christos struct dwarf2_debug *stash = unit->stash;
2115 1.9 christos struct dwarf2_debug_file *file = unit->file;
2116 1.1 christos struct line_info_table* table;
2117 1.1 christos bfd_byte *line_ptr;
2118 1.1 christos bfd_byte *line_end;
2119 1.1 christos struct line_head lh;
2120 1.1 christos unsigned int i, bytes_read, offset_size;
2121 1.1 christos char *cur_file, *cur_dir;
2122 1.1 christos unsigned char op_code, extended_op, adj_opcode;
2123 1.1 christos unsigned int exop_len;
2124 1.9 christos size_t amt;
2125 1.9 christos
2126 1.9 christos if (unit->line_offset == 0 && file->line_table)
2127 1.9 christos return file->line_table;
2128 1.1 christos
2129 1.1 christos if (! read_section (abfd, &stash->debug_sections[debug_line],
2130 1.9 christos file->syms, unit->line_offset,
2131 1.9 christos &file->dwarf_line_buffer, &file->dwarf_line_size))
2132 1.1 christos return NULL;
2133 1.1 christos
2134 1.9 christos if (file->dwarf_line_size < 16)
2135 1.5 christos {
2136 1.7 christos _bfd_error_handler
2137 1.8 christos (_("DWARF error: line info section is too small (%" PRId64 ")"),
2138 1.9 christos (int64_t) file->dwarf_line_size);
2139 1.5 christos bfd_set_error (bfd_error_bad_value);
2140 1.5 christos return NULL;
2141 1.5 christos }
2142 1.9 christos line_ptr = file->dwarf_line_buffer + unit->line_offset;
2143 1.9 christos line_end = file->dwarf_line_buffer + file->dwarf_line_size;
2144 1.1 christos
2145 1.1 christos /* Read in the prologue. */
2146 1.5 christos lh.total_length = read_4_bytes (abfd, line_ptr, line_end);
2147 1.1 christos line_ptr += 4;
2148 1.1 christos offset_size = 4;
2149 1.1 christos if (lh.total_length == 0xffffffff)
2150 1.1 christos {
2151 1.5 christos lh.total_length = read_8_bytes (abfd, line_ptr, line_end);
2152 1.1 christos line_ptr += 8;
2153 1.1 christos offset_size = 8;
2154 1.1 christos }
2155 1.1 christos else if (lh.total_length == 0 && unit->addr_size == 8)
2156 1.1 christos {
2157 1.1 christos /* Handle (non-standard) 64-bit DWARF2 formats. */
2158 1.5 christos lh.total_length = read_4_bytes (abfd, line_ptr, line_end);
2159 1.1 christos line_ptr += 4;
2160 1.1 christos offset_size = 8;
2161 1.1 christos }
2162 1.5 christos
2163 1.8 christos if (lh.total_length > (size_t) (line_end - line_ptr))
2164 1.5 christos {
2165 1.7 christos _bfd_error_handler
2166 1.7 christos /* xgettext: c-format */
2167 1.8 christos (_("DWARF error: line info data is bigger (%#" PRIx64 ")"
2168 1.8 christos " than the space remaining in the section (%#lx)"),
2169 1.8 christos (uint64_t) lh.total_length, (unsigned long) (line_end - line_ptr));
2170 1.5 christos bfd_set_error (bfd_error_bad_value);
2171 1.5 christos return NULL;
2172 1.5 christos }
2173 1.5 christos
2174 1.1 christos line_end = line_ptr + lh.total_length;
2175 1.5 christos
2176 1.5 christos lh.version = read_2_bytes (abfd, line_ptr, line_end);
2177 1.8 christos if (lh.version < 2 || lh.version > 5)
2178 1.1 christos {
2179 1.7 christos _bfd_error_handler
2180 1.8 christos (_("DWARF error: unhandled .debug_line version %d"), lh.version);
2181 1.1 christos bfd_set_error (bfd_error_bad_value);
2182 1.1 christos return NULL;
2183 1.1 christos }
2184 1.1 christos line_ptr += 2;
2185 1.5 christos
2186 1.8 christos if (line_ptr + offset_size + (lh.version >= 5 ? 8 : (lh.version >= 4 ? 6 : 5))
2187 1.8 christos >= line_end)
2188 1.5 christos {
2189 1.7 christos _bfd_error_handler
2190 1.8 christos (_("DWARF error: ran out of room reading prologue"));
2191 1.5 christos bfd_set_error (bfd_error_bad_value);
2192 1.5 christos return NULL;
2193 1.5 christos }
2194 1.5 christos
2195 1.8 christos if (lh.version >= 5)
2196 1.8 christos {
2197 1.8 christos unsigned int segment_selector_size;
2198 1.8 christos
2199 1.8 christos /* Skip address size. */
2200 1.8 christos read_1_byte (abfd, line_ptr, line_end);
2201 1.8 christos line_ptr += 1;
2202 1.8 christos
2203 1.8 christos segment_selector_size = read_1_byte (abfd, line_ptr, line_end);
2204 1.8 christos line_ptr += 1;
2205 1.8 christos if (segment_selector_size != 0)
2206 1.8 christos {
2207 1.8 christos _bfd_error_handler
2208 1.8 christos (_("DWARF error: line info unsupported segment selector size %u"),
2209 1.8 christos segment_selector_size);
2210 1.8 christos bfd_set_error (bfd_error_bad_value);
2211 1.8 christos return NULL;
2212 1.8 christos }
2213 1.8 christos }
2214 1.8 christos
2215 1.1 christos if (offset_size == 4)
2216 1.5 christos lh.prologue_length = read_4_bytes (abfd, line_ptr, line_end);
2217 1.1 christos else
2218 1.5 christos lh.prologue_length = read_8_bytes (abfd, line_ptr, line_end);
2219 1.1 christos line_ptr += offset_size;
2220 1.5 christos
2221 1.5 christos lh.minimum_instruction_length = read_1_byte (abfd, line_ptr, line_end);
2222 1.1 christos line_ptr += 1;
2223 1.5 christos
2224 1.1 christos if (lh.version >= 4)
2225 1.1 christos {
2226 1.5 christos lh.maximum_ops_per_insn = read_1_byte (abfd, line_ptr, line_end);
2227 1.1 christos line_ptr += 1;
2228 1.1 christos }
2229 1.1 christos else
2230 1.1 christos lh.maximum_ops_per_insn = 1;
2231 1.5 christos
2232 1.1 christos if (lh.maximum_ops_per_insn == 0)
2233 1.1 christos {
2234 1.7 christos _bfd_error_handler
2235 1.8 christos (_("DWARF error: invalid maximum operations per instruction"));
2236 1.1 christos bfd_set_error (bfd_error_bad_value);
2237 1.1 christos return NULL;
2238 1.1 christos }
2239 1.5 christos
2240 1.5 christos lh.default_is_stmt = read_1_byte (abfd, line_ptr, line_end);
2241 1.1 christos line_ptr += 1;
2242 1.5 christos
2243 1.5 christos lh.line_base = read_1_signed_byte (abfd, line_ptr, line_end);
2244 1.1 christos line_ptr += 1;
2245 1.5 christos
2246 1.5 christos lh.line_range = read_1_byte (abfd, line_ptr, line_end);
2247 1.1 christos line_ptr += 1;
2248 1.5 christos
2249 1.5 christos lh.opcode_base = read_1_byte (abfd, line_ptr, line_end);
2250 1.1 christos line_ptr += 1;
2251 1.5 christos
2252 1.5 christos if (line_ptr + (lh.opcode_base - 1) >= line_end)
2253 1.5 christos {
2254 1.8 christos _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2255 1.5 christos bfd_set_error (bfd_error_bad_value);
2256 1.5 christos return NULL;
2257 1.5 christos }
2258 1.5 christos
2259 1.1 christos amt = lh.opcode_base * sizeof (unsigned char);
2260 1.1 christos lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
2261 1.1 christos
2262 1.1 christos lh.standard_opcode_lengths[0] = 1;
2263 1.1 christos
2264 1.1 christos for (i = 1; i < lh.opcode_base; ++i)
2265 1.1 christos {
2266 1.5 christos lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr, line_end);
2267 1.1 christos line_ptr += 1;
2268 1.1 christos }
2269 1.1 christos
2270 1.9 christos amt = sizeof (struct line_info_table);
2271 1.9 christos table = (struct line_info_table *) bfd_alloc (abfd, amt);
2272 1.9 christos if (table == NULL)
2273 1.9 christos return NULL;
2274 1.9 christos table->abfd = abfd;
2275 1.9 christos table->comp_dir = unit->comp_dir;
2276 1.9 christos
2277 1.9 christos table->num_files = 0;
2278 1.9 christos table->files = NULL;
2279 1.9 christos
2280 1.9 christos table->num_dirs = 0;
2281 1.9 christos table->dirs = NULL;
2282 1.9 christos
2283 1.9 christos table->num_sequences = 0;
2284 1.9 christos table->sequences = NULL;
2285 1.9 christos
2286 1.9 christos table->lcl_head = NULL;
2287 1.9 christos
2288 1.8 christos if (lh.version >= 5)
2289 1.1 christos {
2290 1.8 christos /* Read directory table. */
2291 1.8 christos if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2292 1.8 christos line_info_add_include_dir_stub))
2293 1.8 christos goto fail;
2294 1.1 christos
2295 1.8 christos /* Read file name table. */
2296 1.8 christos if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2297 1.8 christos line_info_add_file_name))
2298 1.8 christos goto fail;
2299 1.8 christos }
2300 1.8 christos else
2301 1.8 christos {
2302 1.8 christos /* Read directory table. */
2303 1.8 christos while ((cur_dir = read_string (abfd, line_ptr, line_end, &bytes_read)) != NULL)
2304 1.1 christos {
2305 1.8 christos line_ptr += bytes_read;
2306 1.1 christos
2307 1.8 christos if (!line_info_add_include_dir (table, cur_dir))
2308 1.1 christos goto fail;
2309 1.1 christos }
2310 1.1 christos
2311 1.1 christos line_ptr += bytes_read;
2312 1.1 christos
2313 1.8 christos /* Read file name table. */
2314 1.8 christos while ((cur_file = read_string (abfd, line_ptr, line_end, &bytes_read)) != NULL)
2315 1.1 christos {
2316 1.8 christos unsigned int dir, xtime, size;
2317 1.1 christos
2318 1.8 christos line_ptr += bytes_read;
2319 1.1 christos
2320 1.8 christos dir = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
2321 1.8 christos line_ptr += bytes_read;
2322 1.8 christos xtime = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
2323 1.8 christos line_ptr += bytes_read;
2324 1.8 christos size = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
2325 1.8 christos line_ptr += bytes_read;
2326 1.8 christos
2327 1.8 christos if (!line_info_add_file_name (table, cur_file, dir, xtime, size))
2328 1.1 christos goto fail;
2329 1.1 christos }
2330 1.1 christos
2331 1.1 christos line_ptr += bytes_read;
2332 1.1 christos }
2333 1.1 christos
2334 1.1 christos /* Read the statement sequences until there's nothing left. */
2335 1.1 christos while (line_ptr < line_end)
2336 1.1 christos {
2337 1.1 christos /* State machine registers. */
2338 1.1 christos bfd_vma address = 0;
2339 1.1 christos unsigned char op_index = 0;
2340 1.1 christos char * filename = table->num_files ? concat_filename (table, 1) : NULL;
2341 1.1 christos unsigned int line = 1;
2342 1.1 christos unsigned int column = 0;
2343 1.1 christos unsigned int discriminator = 0;
2344 1.1 christos int is_stmt = lh.default_is_stmt;
2345 1.1 christos int end_sequence = 0;
2346 1.8 christos unsigned int dir, xtime, size;
2347 1.1 christos /* eraxxon (at) alumni.rice.edu: Against the DWARF2 specs, some
2348 1.1 christos compilers generate address sequences that are wildly out of
2349 1.1 christos order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2350 1.1 christos for ia64-Linux). Thus, to determine the low and high
2351 1.1 christos address, we must compare on every DW_LNS_copy, etc. */
2352 1.1 christos bfd_vma low_pc = (bfd_vma) -1;
2353 1.1 christos bfd_vma high_pc = 0;
2354 1.1 christos
2355 1.1 christos /* Decode the table. */
2356 1.8 christos while (!end_sequence && line_ptr < line_end)
2357 1.1 christos {
2358 1.5 christos op_code = read_1_byte (abfd, line_ptr, line_end);
2359 1.1 christos line_ptr += 1;
2360 1.1 christos
2361 1.1 christos if (op_code >= lh.opcode_base)
2362 1.1 christos {
2363 1.1 christos /* Special operand. */
2364 1.1 christos adj_opcode = op_code - lh.opcode_base;
2365 1.5 christos if (lh.line_range == 0)
2366 1.5 christos goto line_fail;
2367 1.1 christos if (lh.maximum_ops_per_insn == 1)
2368 1.1 christos address += (adj_opcode / lh.line_range
2369 1.1 christos * lh.minimum_instruction_length);
2370 1.1 christos else
2371 1.1 christos {
2372 1.1 christos address += ((op_index + adj_opcode / lh.line_range)
2373 1.1 christos / lh.maximum_ops_per_insn
2374 1.1 christos * lh.minimum_instruction_length);
2375 1.1 christos op_index = ((op_index + adj_opcode / lh.line_range)
2376 1.1 christos % lh.maximum_ops_per_insn);
2377 1.1 christos }
2378 1.1 christos line += lh.line_base + (adj_opcode % lh.line_range);
2379 1.1 christos /* Append row to matrix using current values. */
2380 1.1 christos if (!add_line_info (table, address, op_index, filename,
2381 1.1 christos line, column, discriminator, 0))
2382 1.1 christos goto line_fail;
2383 1.3 christos discriminator = 0;
2384 1.1 christos if (address < low_pc)
2385 1.1 christos low_pc = address;
2386 1.1 christos if (address > high_pc)
2387 1.1 christos high_pc = address;
2388 1.1 christos }
2389 1.1 christos else switch (op_code)
2390 1.1 christos {
2391 1.1 christos case DW_LNS_extended_op:
2392 1.7 christos exop_len = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2393 1.7 christos FALSE, line_end);
2394 1.1 christos line_ptr += bytes_read;
2395 1.5 christos extended_op = read_1_byte (abfd, line_ptr, line_end);
2396 1.1 christos line_ptr += 1;
2397 1.1 christos
2398 1.1 christos switch (extended_op)
2399 1.1 christos {
2400 1.1 christos case DW_LNE_end_sequence:
2401 1.1 christos end_sequence = 1;
2402 1.1 christos if (!add_line_info (table, address, op_index, filename, line,
2403 1.1 christos column, discriminator, end_sequence))
2404 1.1 christos goto line_fail;
2405 1.3 christos discriminator = 0;
2406 1.1 christos if (address < low_pc)
2407 1.1 christos low_pc = address;
2408 1.1 christos if (address > high_pc)
2409 1.1 christos high_pc = address;
2410 1.1 christos if (!arange_add (unit, &unit->arange, low_pc, high_pc))
2411 1.1 christos goto line_fail;
2412 1.1 christos break;
2413 1.1 christos case DW_LNE_set_address:
2414 1.5 christos address = read_address (unit, line_ptr, line_end);
2415 1.1 christos op_index = 0;
2416 1.1 christos line_ptr += unit->addr_size;
2417 1.1 christos break;
2418 1.1 christos case DW_LNE_define_file:
2419 1.5 christos cur_file = read_string (abfd, line_ptr, line_end, &bytes_read);
2420 1.1 christos line_ptr += bytes_read;
2421 1.8 christos dir = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2422 1.8 christos FALSE, line_end);
2423 1.1 christos line_ptr += bytes_read;
2424 1.8 christos xtime = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2425 1.8 christos FALSE, line_end);
2426 1.1 christos line_ptr += bytes_read;
2427 1.8 christos size = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2428 1.8 christos FALSE, line_end);
2429 1.1 christos line_ptr += bytes_read;
2430 1.8 christos if (!line_info_add_file_name (table, cur_file, dir,
2431 1.8 christos xtime, size))
2432 1.8 christos goto line_fail;
2433 1.1 christos break;
2434 1.1 christos case DW_LNE_set_discriminator:
2435 1.1 christos discriminator =
2436 1.7 christos _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2437 1.7 christos FALSE, line_end);
2438 1.1 christos line_ptr += bytes_read;
2439 1.1 christos break;
2440 1.1 christos case DW_LNE_HP_source_file_correlation:
2441 1.1 christos line_ptr += exop_len - 1;
2442 1.1 christos break;
2443 1.1 christos default:
2444 1.7 christos _bfd_error_handler
2445 1.8 christos (_("DWARF error: mangled line number section"));
2446 1.1 christos bfd_set_error (bfd_error_bad_value);
2447 1.1 christos line_fail:
2448 1.9 christos free (filename);
2449 1.1 christos goto fail;
2450 1.1 christos }
2451 1.1 christos break;
2452 1.1 christos case DW_LNS_copy:
2453 1.1 christos if (!add_line_info (table, address, op_index,
2454 1.1 christos filename, line, column, discriminator, 0))
2455 1.1 christos goto line_fail;
2456 1.3 christos discriminator = 0;
2457 1.1 christos if (address < low_pc)
2458 1.1 christos low_pc = address;
2459 1.1 christos if (address > high_pc)
2460 1.1 christos high_pc = address;
2461 1.1 christos break;
2462 1.1 christos case DW_LNS_advance_pc:
2463 1.1 christos if (lh.maximum_ops_per_insn == 1)
2464 1.1 christos address += (lh.minimum_instruction_length
2465 1.7 christos * _bfd_safe_read_leb128 (abfd, line_ptr,
2466 1.7 christos &bytes_read,
2467 1.7 christos FALSE, line_end));
2468 1.1 christos else
2469 1.1 christos {
2470 1.7 christos bfd_vma adjust = _bfd_safe_read_leb128 (abfd, line_ptr,
2471 1.7 christos &bytes_read,
2472 1.7 christos FALSE, line_end);
2473 1.1 christos address = ((op_index + adjust) / lh.maximum_ops_per_insn
2474 1.1 christos * lh.minimum_instruction_length);
2475 1.1 christos op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
2476 1.1 christos }
2477 1.1 christos line_ptr += bytes_read;
2478 1.1 christos break;
2479 1.1 christos case DW_LNS_advance_line:
2480 1.7 christos line += _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2481 1.7 christos TRUE, line_end);
2482 1.1 christos line_ptr += bytes_read;
2483 1.1 christos break;
2484 1.1 christos case DW_LNS_set_file:
2485 1.1 christos {
2486 1.9 christos unsigned int filenum;
2487 1.1 christos
2488 1.1 christos /* The file and directory tables are 0
2489 1.1 christos based, the references are 1 based. */
2490 1.9 christos filenum = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2491 1.9 christos FALSE, line_end);
2492 1.1 christos line_ptr += bytes_read;
2493 1.9 christos free (filename);
2494 1.9 christos filename = concat_filename (table, filenum);
2495 1.1 christos break;
2496 1.1 christos }
2497 1.1 christos case DW_LNS_set_column:
2498 1.7 christos column = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2499 1.7 christos FALSE, line_end);
2500 1.1 christos line_ptr += bytes_read;
2501 1.1 christos break;
2502 1.1 christos case DW_LNS_negate_stmt:
2503 1.1 christos is_stmt = (!is_stmt);
2504 1.1 christos break;
2505 1.1 christos case DW_LNS_set_basic_block:
2506 1.1 christos break;
2507 1.1 christos case DW_LNS_const_add_pc:
2508 1.8 christos if (lh.line_range == 0)
2509 1.8 christos goto line_fail;
2510 1.1 christos if (lh.maximum_ops_per_insn == 1)
2511 1.1 christos address += (lh.minimum_instruction_length
2512 1.1 christos * ((255 - lh.opcode_base) / lh.line_range));
2513 1.1 christos else
2514 1.1 christos {
2515 1.1 christos bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
2516 1.1 christos address += (lh.minimum_instruction_length
2517 1.1 christos * ((op_index + adjust)
2518 1.1 christos / lh.maximum_ops_per_insn));
2519 1.1 christos op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
2520 1.1 christos }
2521 1.1 christos break;
2522 1.1 christos case DW_LNS_fixed_advance_pc:
2523 1.5 christos address += read_2_bytes (abfd, line_ptr, line_end);
2524 1.1 christos op_index = 0;
2525 1.1 christos line_ptr += 2;
2526 1.1 christos break;
2527 1.1 christos default:
2528 1.1 christos /* Unknown standard opcode, ignore it. */
2529 1.1 christos for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
2530 1.1 christos {
2531 1.7 christos (void) _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2532 1.7 christos FALSE, line_end);
2533 1.1 christos line_ptr += bytes_read;
2534 1.1 christos }
2535 1.1 christos break;
2536 1.1 christos }
2537 1.1 christos }
2538 1.1 christos
2539 1.9 christos free (filename);
2540 1.1 christos }
2541 1.1 christos
2542 1.9 christos if (unit->line_offset == 0)
2543 1.9 christos file->line_table = table;
2544 1.1 christos if (sort_line_sequences (table))
2545 1.1 christos return table;
2546 1.1 christos
2547 1.1 christos fail:
2548 1.8 christos while (table->sequences != NULL)
2549 1.8 christos {
2550 1.8 christos struct line_sequence* seq = table->sequences;
2551 1.8 christos table->sequences = table->sequences->prev_sequence;
2552 1.8 christos free (seq);
2553 1.8 christos }
2554 1.9 christos free (table->files);
2555 1.9 christos free (table->dirs);
2556 1.1 christos return NULL;
2557 1.1 christos }
2558 1.1 christos
2559 1.1 christos /* If ADDR is within TABLE set the output parameters and return the
2560 1.1 christos range of addresses covered by the entry used to fill them out.
2561 1.1 christos Otherwise set * FILENAME_PTR to NULL and return 0.
2562 1.1 christos The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2563 1.1 christos are pointers to the objects to be filled in. */
2564 1.1 christos
2565 1.1 christos static bfd_vma
2566 1.1 christos lookup_address_in_line_info_table (struct line_info_table *table,
2567 1.1 christos bfd_vma addr,
2568 1.1 christos const char **filename_ptr,
2569 1.1 christos unsigned int *linenumber_ptr,
2570 1.1 christos unsigned int *discriminator_ptr)
2571 1.1 christos {
2572 1.1 christos struct line_sequence *seq = NULL;
2573 1.7 christos struct line_info *info;
2574 1.1 christos int low, high, mid;
2575 1.1 christos
2576 1.1 christos /* Binary search the array of sequences. */
2577 1.1 christos low = 0;
2578 1.1 christos high = table->num_sequences;
2579 1.1 christos while (low < high)
2580 1.1 christos {
2581 1.1 christos mid = (low + high) / 2;
2582 1.1 christos seq = &table->sequences[mid];
2583 1.1 christos if (addr < seq->low_pc)
2584 1.1 christos high = mid;
2585 1.1 christos else if (addr >= seq->last_line->address)
2586 1.1 christos low = mid + 1;
2587 1.1 christos else
2588 1.1 christos break;
2589 1.1 christos }
2590 1.1 christos
2591 1.7 christos /* Check for a valid sequence. */
2592 1.7 christos if (!seq || addr < seq->low_pc || addr >= seq->last_line->address)
2593 1.7 christos goto fail;
2594 1.7 christos
2595 1.7 christos if (!build_line_info_table (table, seq))
2596 1.7 christos goto fail;
2597 1.7 christos
2598 1.7 christos /* Binary search the array of line information. */
2599 1.7 christos low = 0;
2600 1.7 christos high = seq->num_lines;
2601 1.7 christos info = NULL;
2602 1.7 christos while (low < high)
2603 1.1 christos {
2604 1.7 christos mid = (low + high) / 2;
2605 1.7 christos info = seq->line_info_lookup[mid];
2606 1.7 christos if (addr < info->address)
2607 1.7 christos high = mid;
2608 1.7 christos else if (addr >= seq->line_info_lookup[mid + 1]->address)
2609 1.7 christos low = mid + 1;
2610 1.7 christos else
2611 1.7 christos break;
2612 1.7 christos }
2613 1.1 christos
2614 1.7 christos /* Check for a valid line information entry. */
2615 1.7 christos if (info
2616 1.7 christos && addr >= info->address
2617 1.7 christos && addr < seq->line_info_lookup[mid + 1]->address
2618 1.7 christos && !(info->end_sequence || info == seq->last_line))
2619 1.7 christos {
2620 1.7 christos *filename_ptr = info->filename;
2621 1.7 christos *linenumber_ptr = info->line;
2622 1.7 christos if (discriminator_ptr)
2623 1.7 christos *discriminator_ptr = info->discriminator;
2624 1.7 christos return seq->last_line->address - seq->low_pc;
2625 1.1 christos }
2626 1.1 christos
2627 1.9 christos fail:
2628 1.1 christos *filename_ptr = NULL;
2629 1.1 christos return 0;
2630 1.1 christos }
2631 1.1 christos
2632 1.1 christos /* Read in the .debug_ranges section for future reference. */
2633 1.1 christos
2634 1.1 christos static bfd_boolean
2635 1.7 christos read_debug_ranges (struct comp_unit * unit)
2636 1.1 christos {
2637 1.9 christos struct dwarf2_debug *stash = unit->stash;
2638 1.9 christos struct dwarf2_debug_file *file = unit->file;
2639 1.7 christos
2640 1.1 christos return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
2641 1.9 christos file->syms, 0,
2642 1.9 christos &file->dwarf_ranges_buffer, &file->dwarf_ranges_size);
2643 1.9 christos }
2644 1.9 christos
2645 1.9 christos /* Read in the .debug_rnglists section for future reference. */
2646 1.9 christos
2647 1.9 christos static bfd_boolean
2648 1.9 christos read_debug_rnglists (struct comp_unit * unit)
2649 1.9 christos {
2650 1.9 christos struct dwarf2_debug *stash = unit->stash;
2651 1.9 christos struct dwarf2_debug_file *file = unit->file;
2652 1.9 christos
2653 1.9 christos return read_section (unit->abfd, &stash->debug_sections[debug_rnglists],
2654 1.9 christos file->syms, 0,
2655 1.9 christos &file->dwarf_rnglists_buffer, &file->dwarf_rnglists_size);
2656 1.1 christos }
2657 1.1 christos
2658 1.1 christos /* Function table functions. */
2659 1.1 christos
2660 1.7 christos static int
2661 1.7 christos compare_lookup_funcinfos (const void * a, const void * b)
2662 1.7 christos {
2663 1.7 christos const struct lookup_funcinfo * lookup1 = a;
2664 1.7 christos const struct lookup_funcinfo * lookup2 = b;
2665 1.7 christos
2666 1.7 christos if (lookup1->low_addr < lookup2->low_addr)
2667 1.7 christos return -1;
2668 1.7 christos if (lookup1->low_addr > lookup2->low_addr)
2669 1.7 christos return 1;
2670 1.7 christos if (lookup1->high_addr < lookup2->high_addr)
2671 1.7 christos return -1;
2672 1.7 christos if (lookup1->high_addr > lookup2->high_addr)
2673 1.7 christos return 1;
2674 1.7 christos
2675 1.9 christos if (lookup1->idx < lookup2->idx)
2676 1.9 christos return -1;
2677 1.9 christos if (lookup1->idx > lookup2->idx)
2678 1.9 christos return 1;
2679 1.7 christos return 0;
2680 1.7 christos }
2681 1.7 christos
2682 1.7 christos static bfd_boolean
2683 1.7 christos build_lookup_funcinfo_table (struct comp_unit * unit)
2684 1.7 christos {
2685 1.7 christos struct lookup_funcinfo *lookup_funcinfo_table = unit->lookup_funcinfo_table;
2686 1.7 christos unsigned int number_of_functions = unit->number_of_functions;
2687 1.7 christos struct funcinfo *each;
2688 1.7 christos struct lookup_funcinfo *entry;
2689 1.7 christos size_t func_index;
2690 1.7 christos struct arange *range;
2691 1.7 christos bfd_vma low_addr, high_addr;
2692 1.7 christos
2693 1.7 christos if (lookup_funcinfo_table || number_of_functions == 0)
2694 1.7 christos return TRUE;
2695 1.7 christos
2696 1.7 christos /* Create the function info lookup table. */
2697 1.7 christos lookup_funcinfo_table = (struct lookup_funcinfo *)
2698 1.7 christos bfd_malloc (number_of_functions * sizeof (struct lookup_funcinfo));
2699 1.7 christos if (lookup_funcinfo_table == NULL)
2700 1.7 christos return FALSE;
2701 1.7 christos
2702 1.7 christos /* Populate the function info lookup table. */
2703 1.7 christos func_index = number_of_functions;
2704 1.7 christos for (each = unit->function_table; each; each = each->prev_func)
2705 1.7 christos {
2706 1.7 christos entry = &lookup_funcinfo_table[--func_index];
2707 1.7 christos entry->funcinfo = each;
2708 1.9 christos entry->idx = func_index;
2709 1.7 christos
2710 1.7 christos /* Calculate the lowest and highest address for this function entry. */
2711 1.7 christos low_addr = entry->funcinfo->arange.low;
2712 1.7 christos high_addr = entry->funcinfo->arange.high;
2713 1.7 christos
2714 1.7 christos for (range = entry->funcinfo->arange.next; range; range = range->next)
2715 1.7 christos {
2716 1.7 christos if (range->low < low_addr)
2717 1.7 christos low_addr = range->low;
2718 1.7 christos if (range->high > high_addr)
2719 1.7 christos high_addr = range->high;
2720 1.7 christos }
2721 1.7 christos
2722 1.7 christos entry->low_addr = low_addr;
2723 1.7 christos entry->high_addr = high_addr;
2724 1.7 christos }
2725 1.7 christos
2726 1.7 christos BFD_ASSERT (func_index == 0);
2727 1.7 christos
2728 1.7 christos /* Sort the function by address. */
2729 1.7 christos qsort (lookup_funcinfo_table,
2730 1.7 christos number_of_functions,
2731 1.7 christos sizeof (struct lookup_funcinfo),
2732 1.7 christos compare_lookup_funcinfos);
2733 1.7 christos
2734 1.7 christos /* Calculate the high watermark for each function in the lookup table. */
2735 1.7 christos high_addr = lookup_funcinfo_table[0].high_addr;
2736 1.7 christos for (func_index = 1; func_index < number_of_functions; func_index++)
2737 1.7 christos {
2738 1.7 christos entry = &lookup_funcinfo_table[func_index];
2739 1.7 christos if (entry->high_addr > high_addr)
2740 1.7 christos high_addr = entry->high_addr;
2741 1.7 christos else
2742 1.7 christos entry->high_addr = high_addr;
2743 1.7 christos }
2744 1.7 christos
2745 1.7 christos unit->lookup_funcinfo_table = lookup_funcinfo_table;
2746 1.7 christos return TRUE;
2747 1.7 christos }
2748 1.7 christos
2749 1.3 christos /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
2750 1.1 christos TRUE. Note that we need to find the function that has the smallest range
2751 1.1 christos that contains ADDR, to handle inlined functions without depending upon
2752 1.1 christos them being ordered in TABLE by increasing range. */
2753 1.1 christos
2754 1.1 christos static bfd_boolean
2755 1.1 christos lookup_address_in_function_table (struct comp_unit *unit,
2756 1.1 christos bfd_vma addr,
2757 1.3 christos struct funcinfo **function_ptr)
2758 1.1 christos {
2759 1.7 christos unsigned int number_of_functions = unit->number_of_functions;
2760 1.7 christos struct lookup_funcinfo* lookup_funcinfo = NULL;
2761 1.7 christos struct funcinfo* funcinfo = NULL;
2762 1.1 christos struct funcinfo* best_fit = NULL;
2763 1.3 christos bfd_vma best_fit_len = 0;
2764 1.7 christos bfd_size_type low, high, mid, first;
2765 1.1 christos struct arange *arange;
2766 1.1 christos
2767 1.7 christos if (number_of_functions == 0)
2768 1.7 christos return FALSE;
2769 1.7 christos
2770 1.7 christos if (!build_lookup_funcinfo_table (unit))
2771 1.7 christos return FALSE;
2772 1.7 christos
2773 1.7 christos if (unit->lookup_funcinfo_table[number_of_functions - 1].high_addr < addr)
2774 1.7 christos return FALSE;
2775 1.8 christos
2776 1.7 christos /* Find the first function in the lookup table which may contain the
2777 1.7 christos specified address. */
2778 1.7 christos low = 0;
2779 1.7 christos high = number_of_functions;
2780 1.7 christos first = high;
2781 1.7 christos while (low < high)
2782 1.7 christos {
2783 1.7 christos mid = (low + high) / 2;
2784 1.7 christos lookup_funcinfo = &unit->lookup_funcinfo_table[mid];
2785 1.7 christos if (addr < lookup_funcinfo->low_addr)
2786 1.7 christos high = mid;
2787 1.7 christos else if (addr >= lookup_funcinfo->high_addr)
2788 1.7 christos low = mid + 1;
2789 1.7 christos else
2790 1.7 christos high = first = mid;
2791 1.7 christos }
2792 1.7 christos
2793 1.7 christos /* Find the 'best' match for the address. The prior algorithm defined the
2794 1.7 christos best match as the function with the smallest address range containing
2795 1.7 christos the specified address. This definition should probably be changed to the
2796 1.7 christos innermost inline routine containing the address, but right now we want
2797 1.7 christos to get the same results we did before. */
2798 1.7 christos while (first < number_of_functions)
2799 1.1 christos {
2800 1.7 christos if (addr < unit->lookup_funcinfo_table[first].low_addr)
2801 1.7 christos break;
2802 1.7 christos funcinfo = unit->lookup_funcinfo_table[first].funcinfo;
2803 1.7 christos
2804 1.7 christos for (arange = &funcinfo->arange; arange; arange = arange->next)
2805 1.1 christos {
2806 1.7 christos if (addr < arange->low || addr >= arange->high)
2807 1.7 christos continue;
2808 1.7 christos
2809 1.7 christos if (!best_fit
2810 1.7 christos || arange->high - arange->low < best_fit_len
2811 1.7 christos /* The following comparison is designed to return the same
2812 1.7 christos match as the previous algorithm for routines which have the
2813 1.7 christos same best fit length. */
2814 1.7 christos || (arange->high - arange->low == best_fit_len
2815 1.7 christos && funcinfo > best_fit))
2816 1.1 christos {
2817 1.7 christos best_fit = funcinfo;
2818 1.7 christos best_fit_len = arange->high - arange->low;
2819 1.1 christos }
2820 1.1 christos }
2821 1.7 christos
2822 1.7 christos first++;
2823 1.1 christos }
2824 1.1 christos
2825 1.7 christos if (!best_fit)
2826 1.7 christos return FALSE;
2827 1.7 christos
2828 1.7 christos *function_ptr = best_fit;
2829 1.7 christos return TRUE;
2830 1.1 christos }
2831 1.1 christos
2832 1.1 christos /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
2833 1.1 christos and LINENUMBER_PTR, and return TRUE. */
2834 1.1 christos
2835 1.1 christos static bfd_boolean
2836 1.1 christos lookup_symbol_in_function_table (struct comp_unit *unit,
2837 1.1 christos asymbol *sym,
2838 1.1 christos bfd_vma addr,
2839 1.1 christos const char **filename_ptr,
2840 1.1 christos unsigned int *linenumber_ptr)
2841 1.1 christos {
2842 1.1 christos struct funcinfo* each_func;
2843 1.1 christos struct funcinfo* best_fit = NULL;
2844 1.3 christos bfd_vma best_fit_len = 0;
2845 1.1 christos struct arange *arange;
2846 1.1 christos const char *name = bfd_asymbol_name (sym);
2847 1.9 christos asection *sec = bfd_asymbol_section (sym);
2848 1.1 christos
2849 1.1 christos for (each_func = unit->function_table;
2850 1.1 christos each_func;
2851 1.1 christos each_func = each_func->prev_func)
2852 1.1 christos {
2853 1.1 christos for (arange = &each_func->arange;
2854 1.1 christos arange;
2855 1.1 christos arange = arange->next)
2856 1.1 christos {
2857 1.1 christos if ((!each_func->sec || each_func->sec == sec)
2858 1.1 christos && addr >= arange->low
2859 1.1 christos && addr < arange->high
2860 1.1 christos && each_func->name
2861 1.1 christos && strcmp (name, each_func->name) == 0
2862 1.1 christos && (!best_fit
2863 1.3 christos || arange->high - arange->low < best_fit_len))
2864 1.3 christos {
2865 1.3 christos best_fit = each_func;
2866 1.3 christos best_fit_len = arange->high - arange->low;
2867 1.3 christos }
2868 1.1 christos }
2869 1.1 christos }
2870 1.1 christos
2871 1.1 christos if (best_fit)
2872 1.1 christos {
2873 1.1 christos best_fit->sec = sec;
2874 1.1 christos *filename_ptr = best_fit->file;
2875 1.1 christos *linenumber_ptr = best_fit->line;
2876 1.1 christos return TRUE;
2877 1.1 christos }
2878 1.1 christos else
2879 1.1 christos return FALSE;
2880 1.1 christos }
2881 1.1 christos
2882 1.1 christos /* Variable table functions. */
2883 1.1 christos
2884 1.1 christos /* If SYM is within variable table of UNIT, set FILENAME_PTR and
2885 1.1 christos LINENUMBER_PTR, and return TRUE. */
2886 1.1 christos
2887 1.1 christos static bfd_boolean
2888 1.1 christos lookup_symbol_in_variable_table (struct comp_unit *unit,
2889 1.1 christos asymbol *sym,
2890 1.1 christos bfd_vma addr,
2891 1.1 christos const char **filename_ptr,
2892 1.1 christos unsigned int *linenumber_ptr)
2893 1.1 christos {
2894 1.1 christos const char *name = bfd_asymbol_name (sym);
2895 1.9 christos asection *sec = bfd_asymbol_section (sym);
2896 1.1 christos struct varinfo* each;
2897 1.1 christos
2898 1.1 christos for (each = unit->variable_table; each; each = each->prev_var)
2899 1.9 christos if (! each->stack
2900 1.1 christos && each->file != NULL
2901 1.1 christos && each->name != NULL
2902 1.1 christos && each->addr == addr
2903 1.1 christos && (!each->sec || each->sec == sec)
2904 1.1 christos && strcmp (name, each->name) == 0)
2905 1.1 christos break;
2906 1.1 christos
2907 1.1 christos if (each)
2908 1.1 christos {
2909 1.1 christos each->sec = sec;
2910 1.1 christos *filename_ptr = each->file;
2911 1.1 christos *linenumber_ptr = each->line;
2912 1.1 christos return TRUE;
2913 1.1 christos }
2914 1.7 christos
2915 1.7 christos return FALSE;
2916 1.1 christos }
2917 1.1 christos
2918 1.9 christos static struct comp_unit *stash_comp_unit (struct dwarf2_debug *,
2919 1.9 christos struct dwarf2_debug_file *);
2920 1.9 christos static bfd_boolean comp_unit_maybe_decode_line_info (struct comp_unit *);
2921 1.9 christos
2922 1.8 christos static bfd_boolean
2923 1.9 christos find_abstract_instance (struct comp_unit *unit,
2924 1.9 christos struct attribute *attr_ptr,
2925 1.9 christos unsigned int recur_count,
2926 1.9 christos const char **pname,
2927 1.9 christos bfd_boolean *is_linkage,
2928 1.9 christos char **filename_ptr,
2929 1.9 christos int *linenumber_ptr)
2930 1.1 christos {
2931 1.1 christos bfd *abfd = unit->abfd;
2932 1.9 christos bfd_byte *info_ptr = NULL;
2933 1.5 christos bfd_byte *info_ptr_end;
2934 1.1 christos unsigned int abbrev_number, bytes_read, i;
2935 1.1 christos struct abbrev_info *abbrev;
2936 1.1 christos bfd_uint64_t die_ref = attr_ptr->u.val;
2937 1.1 christos struct attribute attr;
2938 1.8 christos const char *name = NULL;
2939 1.1 christos
2940 1.9 christos if (recur_count == 100)
2941 1.9 christos {
2942 1.9 christos _bfd_error_handler
2943 1.9 christos (_("DWARF error: abstract instance recursion detected"));
2944 1.9 christos bfd_set_error (bfd_error_bad_value);
2945 1.9 christos return FALSE;
2946 1.9 christos }
2947 1.9 christos
2948 1.1 christos /* DW_FORM_ref_addr can reference an entry in a different CU. It
2949 1.1 christos is an offset from the .debug_info section, not the current CU. */
2950 1.1 christos if (attr_ptr->form == DW_FORM_ref_addr)
2951 1.1 christos {
2952 1.1 christos /* We only support DW_FORM_ref_addr within the same file, so
2953 1.8 christos any relocations should be resolved already. Check this by
2954 1.8 christos testing for a zero die_ref; There can't be a valid reference
2955 1.8 christos to the header of a .debug_info section.
2956 1.8 christos DW_FORM_ref_addr is an offset relative to .debug_info.
2957 1.8 christos Normally when using the GNU linker this is accomplished by
2958 1.8 christos emitting a symbolic reference to a label, because .debug_info
2959 1.8 christos sections are linked at zero. When there are multiple section
2960 1.8 christos groups containing .debug_info, as there might be in a
2961 1.8 christos relocatable object file, it would be reasonable to assume that
2962 1.8 christos a symbolic reference to a label in any .debug_info section
2963 1.8 christos might be used. Since we lay out multiple .debug_info
2964 1.8 christos sections at non-zero VMAs (see place_sections), and read
2965 1.9 christos them contiguously into dwarf_info_buffer, that means the
2966 1.9 christos reference is relative to dwarf_info_buffer. */
2967 1.8 christos size_t total;
2968 1.8 christos
2969 1.9 christos info_ptr = unit->file->dwarf_info_buffer;
2970 1.9 christos info_ptr_end = info_ptr + unit->file->dwarf_info_size;
2971 1.8 christos total = info_ptr_end - info_ptr;
2972 1.1 christos if (!die_ref)
2973 1.8 christos return TRUE;
2974 1.8 christos else if (die_ref >= total)
2975 1.8 christos {
2976 1.8 christos _bfd_error_handler
2977 1.8 christos (_("DWARF error: invalid abstract instance DIE ref"));
2978 1.8 christos bfd_set_error (bfd_error_bad_value);
2979 1.8 christos return FALSE;
2980 1.8 christos }
2981 1.8 christos info_ptr += die_ref;
2982 1.9 christos }
2983 1.9 christos else if (attr_ptr->form == DW_FORM_GNU_ref_alt)
2984 1.9 christos {
2985 1.9 christos bfd_boolean first_time = unit->stash->alt.dwarf_info_buffer == NULL;
2986 1.9 christos
2987 1.9 christos info_ptr = read_alt_indirect_ref (unit, die_ref);
2988 1.9 christos if (first_time)
2989 1.9 christos unit->stash->alt.info_ptr = unit->stash->alt.dwarf_info_buffer;
2990 1.9 christos if (info_ptr == NULL)
2991 1.9 christos {
2992 1.9 christos _bfd_error_handler
2993 1.9 christos (_("DWARF error: unable to read alt ref %" PRIu64),
2994 1.9 christos (uint64_t) die_ref);
2995 1.9 christos bfd_set_error (bfd_error_bad_value);
2996 1.9 christos return FALSE;
2997 1.9 christos }
2998 1.9 christos info_ptr_end = (unit->stash->alt.dwarf_info_buffer
2999 1.9 christos + unit->stash->alt.dwarf_info_size);
3000 1.9 christos if (unit->stash->alt.all_comp_units)
3001 1.9 christos unit = unit->stash->alt.all_comp_units;
3002 1.9 christos }
3003 1.3 christos
3004 1.9 christos if (attr_ptr->form == DW_FORM_ref_addr
3005 1.9 christos || attr_ptr->form == DW_FORM_GNU_ref_alt)
3006 1.9 christos {
3007 1.3 christos /* Now find the CU containing this pointer. */
3008 1.3 christos if (info_ptr >= unit->info_ptr_unit && info_ptr < unit->end_ptr)
3009 1.8 christos info_ptr_end = unit->end_ptr;
3010 1.3 christos else
3011 1.3 christos {
3012 1.3 christos /* Check other CUs to see if they contain the abbrev. */
3013 1.9 christos struct comp_unit *u;
3014 1.3 christos
3015 1.3 christos for (u = unit->prev_unit; u != NULL; u = u->prev_unit)
3016 1.3 christos if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3017 1.3 christos break;
3018 1.3 christos
3019 1.3 christos if (u == NULL)
3020 1.3 christos for (u = unit->next_unit; u != NULL; u = u->next_unit)
3021 1.3 christos if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3022 1.3 christos break;
3023 1.3 christos
3024 1.9 christos if (attr_ptr->form == DW_FORM_ref_addr)
3025 1.9 christos while (u == NULL)
3026 1.9 christos {
3027 1.9 christos u = stash_comp_unit (unit->stash, &unit->stash->f);
3028 1.9 christos if (u == NULL)
3029 1.9 christos break;
3030 1.9 christos if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3031 1.9 christos break;
3032 1.9 christos u = NULL;
3033 1.9 christos }
3034 1.9 christos
3035 1.9 christos if (attr_ptr->form == DW_FORM_GNU_ref_alt)
3036 1.9 christos while (u == NULL)
3037 1.9 christos {
3038 1.9 christos u = stash_comp_unit (unit->stash, &unit->stash->alt);
3039 1.9 christos if (u == NULL)
3040 1.9 christos break;
3041 1.9 christos if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
3042 1.9 christos break;
3043 1.9 christos u = NULL;
3044 1.9 christos }
3045 1.9 christos
3046 1.9 christos if (u == NULL)
3047 1.8 christos {
3048 1.9 christos _bfd_error_handler
3049 1.9 christos (_("DWARF error: unable to locate abstract instance DIE ref %"
3050 1.9 christos PRIu64), (uint64_t) die_ref);
3051 1.9 christos bfd_set_error (bfd_error_bad_value);
3052 1.9 christos return FALSE;
3053 1.8 christos }
3054 1.9 christos unit = u;
3055 1.9 christos info_ptr_end = unit->end_ptr;
3056 1.3 christos }
3057 1.1 christos }
3058 1.1 christos else
3059 1.5 christos {
3060 1.8 christos /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
3061 1.8 christos DW_FORM_ref_udata. These are all references relative to the
3062 1.8 christos start of the current CU. */
3063 1.8 christos size_t total;
3064 1.8 christos
3065 1.8 christos info_ptr = unit->info_ptr_unit;
3066 1.5 christos info_ptr_end = unit->end_ptr;
3067 1.8 christos total = info_ptr_end - info_ptr;
3068 1.8 christos if (!die_ref || die_ref >= total)
3069 1.8 christos {
3070 1.8 christos _bfd_error_handler
3071 1.8 christos (_("DWARF error: invalid abstract instance DIE ref"));
3072 1.8 christos bfd_set_error (bfd_error_bad_value);
3073 1.8 christos return FALSE;
3074 1.8 christos }
3075 1.8 christos info_ptr += die_ref;
3076 1.5 christos }
3077 1.1 christos
3078 1.7 christos abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
3079 1.7 christos FALSE, info_ptr_end);
3080 1.1 christos info_ptr += bytes_read;
3081 1.1 christos
3082 1.1 christos if (abbrev_number)
3083 1.1 christos {
3084 1.1 christos abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3085 1.1 christos if (! abbrev)
3086 1.1 christos {
3087 1.7 christos _bfd_error_handler
3088 1.8 christos (_("DWARF error: could not find abbrev number %u"), abbrev_number);
3089 1.1 christos bfd_set_error (bfd_error_bad_value);
3090 1.8 christos return FALSE;
3091 1.1 christos }
3092 1.1 christos else
3093 1.1 christos {
3094 1.1 christos for (i = 0; i < abbrev->num_attrs; ++i)
3095 1.1 christos {
3096 1.1 christos info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
3097 1.5 christos info_ptr, info_ptr_end);
3098 1.1 christos if (info_ptr == NULL)
3099 1.1 christos break;
3100 1.1 christos switch (attr.name)
3101 1.1 christos {
3102 1.1 christos case DW_AT_name:
3103 1.1 christos /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3104 1.1 christos over DW_AT_name. */
3105 1.3 christos if (name == NULL && is_str_attr (attr.form))
3106 1.3 christos {
3107 1.3 christos name = attr.u.str;
3108 1.3 christos if (non_mangled (unit->lang))
3109 1.3 christos *is_linkage = TRUE;
3110 1.3 christos }
3111 1.1 christos break;
3112 1.1 christos case DW_AT_specification:
3113 1.9 christos if (!find_abstract_instance (unit, &attr, recur_count + 1,
3114 1.8 christos &name, is_linkage,
3115 1.8 christos filename_ptr, linenumber_ptr))
3116 1.8 christos return FALSE;
3117 1.1 christos break;
3118 1.1 christos case DW_AT_linkage_name:
3119 1.1 christos case DW_AT_MIPS_linkage_name:
3120 1.3 christos /* PR 16949: Corrupt debug info can place
3121 1.3 christos non-string forms into these attributes. */
3122 1.3 christos if (is_str_attr (attr.form))
3123 1.3 christos {
3124 1.3 christos name = attr.u.str;
3125 1.3 christos *is_linkage = TRUE;
3126 1.3 christos }
3127 1.1 christos break;
3128 1.8 christos case DW_AT_decl_file:
3129 1.9 christos if (!comp_unit_maybe_decode_line_info (unit))
3130 1.9 christos return FALSE;
3131 1.8 christos *filename_ptr = concat_filename (unit->line_table,
3132 1.8 christos attr.u.val);
3133 1.8 christos break;
3134 1.8 christos case DW_AT_decl_line:
3135 1.8 christos *linenumber_ptr = attr.u.val;
3136 1.8 christos break;
3137 1.1 christos default:
3138 1.1 christos break;
3139 1.1 christos }
3140 1.1 christos }
3141 1.1 christos }
3142 1.1 christos }
3143 1.8 christos *pname = name;
3144 1.8 christos return TRUE;
3145 1.1 christos }
3146 1.1 christos
3147 1.1 christos static bfd_boolean
3148 1.9 christos read_ranges (struct comp_unit *unit, struct arange *arange,
3149 1.9 christos bfd_uint64_t offset)
3150 1.1 christos {
3151 1.1 christos bfd_byte *ranges_ptr;
3152 1.5 christos bfd_byte *ranges_end;
3153 1.1 christos bfd_vma base_address = unit->base_address;
3154 1.1 christos
3155 1.9 christos if (! unit->file->dwarf_ranges_buffer)
3156 1.1 christos {
3157 1.1 christos if (! read_debug_ranges (unit))
3158 1.1 christos return FALSE;
3159 1.1 christos }
3160 1.5 christos
3161 1.9 christos ranges_ptr = unit->file->dwarf_ranges_buffer + offset;
3162 1.9 christos if (ranges_ptr < unit->file->dwarf_ranges_buffer)
3163 1.5 christos return FALSE;
3164 1.9 christos ranges_end = unit->file->dwarf_ranges_buffer + unit->file->dwarf_ranges_size;
3165 1.1 christos
3166 1.1 christos for (;;)
3167 1.1 christos {
3168 1.1 christos bfd_vma low_pc;
3169 1.1 christos bfd_vma high_pc;
3170 1.1 christos
3171 1.5 christos /* PR 17512: file: 62cada7d. */
3172 1.5 christos if (ranges_ptr + 2 * unit->addr_size > ranges_end)
3173 1.5 christos return FALSE;
3174 1.5 christos
3175 1.5 christos low_pc = read_address (unit, ranges_ptr, ranges_end);
3176 1.1 christos ranges_ptr += unit->addr_size;
3177 1.5 christos high_pc = read_address (unit, ranges_ptr, ranges_end);
3178 1.1 christos ranges_ptr += unit->addr_size;
3179 1.1 christos
3180 1.1 christos if (low_pc == 0 && high_pc == 0)
3181 1.1 christos break;
3182 1.1 christos if (low_pc == -1UL && high_pc != -1UL)
3183 1.1 christos base_address = high_pc;
3184 1.1 christos else
3185 1.1 christos {
3186 1.1 christos if (!arange_add (unit, arange,
3187 1.1 christos base_address + low_pc, base_address + high_pc))
3188 1.1 christos return FALSE;
3189 1.1 christos }
3190 1.1 christos }
3191 1.1 christos return TRUE;
3192 1.1 christos }
3193 1.1 christos
3194 1.9 christos static bfd_boolean
3195 1.9 christos read_rnglists (struct comp_unit *unit, struct arange *arange,
3196 1.9 christos bfd_uint64_t offset)
3197 1.9 christos {
3198 1.9 christos bfd_byte *rngs_ptr;
3199 1.9 christos bfd_byte *rngs_end;
3200 1.9 christos bfd_vma base_address = unit->base_address;
3201 1.9 christos bfd_vma low_pc;
3202 1.9 christos bfd_vma high_pc;
3203 1.9 christos bfd *abfd = unit->abfd;
3204 1.9 christos
3205 1.9 christos if (! unit->file->dwarf_rnglists_buffer)
3206 1.9 christos {
3207 1.9 christos if (! read_debug_rnglists (unit))
3208 1.9 christos return FALSE;
3209 1.9 christos }
3210 1.9 christos
3211 1.9 christos rngs_ptr = unit->file->dwarf_rnglists_buffer + offset;
3212 1.9 christos if (rngs_ptr < unit->file->dwarf_rnglists_buffer)
3213 1.9 christos return FALSE;
3214 1.9 christos rngs_end = unit->file->dwarf_rnglists_buffer;
3215 1.9 christos rngs_end += unit->file->dwarf_rnglists_size;
3216 1.9 christos
3217 1.9 christos for (;;)
3218 1.9 christos {
3219 1.9 christos enum dwarf_range_list_entry rlet;
3220 1.9 christos unsigned int bytes_read;
3221 1.9 christos
3222 1.9 christos if (rngs_ptr + 1 > rngs_end)
3223 1.9 christos return FALSE;
3224 1.9 christos
3225 1.9 christos rlet = read_1_byte (abfd, rngs_ptr, rngs_end);
3226 1.9 christos rngs_ptr++;
3227 1.9 christos
3228 1.9 christos switch (rlet)
3229 1.9 christos {
3230 1.9 christos case DW_RLE_end_of_list:
3231 1.9 christos return TRUE;
3232 1.9 christos
3233 1.9 christos case DW_RLE_base_address:
3234 1.9 christos if (rngs_ptr + unit->addr_size > rngs_end)
3235 1.9 christos return FALSE;
3236 1.9 christos base_address = read_address (unit, rngs_ptr, rngs_end);
3237 1.9 christos rngs_ptr += unit->addr_size;
3238 1.9 christos continue;
3239 1.9 christos
3240 1.9 christos case DW_RLE_start_length:
3241 1.9 christos if (rngs_ptr + unit->addr_size > rngs_end)
3242 1.9 christos return FALSE;
3243 1.9 christos low_pc = read_address (unit, rngs_ptr, rngs_end);
3244 1.9 christos rngs_ptr += unit->addr_size;
3245 1.9 christos high_pc = low_pc;
3246 1.9 christos high_pc += _bfd_safe_read_leb128 (abfd, rngs_ptr, &bytes_read,
3247 1.9 christos FALSE, rngs_end);
3248 1.9 christos rngs_ptr += bytes_read;
3249 1.9 christos break;
3250 1.9 christos
3251 1.9 christos case DW_RLE_offset_pair:
3252 1.9 christos low_pc = base_address;
3253 1.9 christos low_pc += _bfd_safe_read_leb128 (abfd, rngs_ptr, &bytes_read,
3254 1.9 christos FALSE, rngs_end);
3255 1.9 christos high_pc = base_address;
3256 1.9 christos high_pc += _bfd_safe_read_leb128 (abfd, rngs_ptr, &bytes_read,
3257 1.9 christos FALSE, rngs_end);
3258 1.9 christos break;
3259 1.9 christos
3260 1.9 christos case DW_RLE_start_end:
3261 1.9 christos if (rngs_ptr + 2 * unit->addr_size > rngs_end)
3262 1.9 christos return FALSE;
3263 1.9 christos low_pc = read_address (unit, rngs_ptr, rngs_end);
3264 1.9 christos rngs_ptr += unit->addr_size;
3265 1.9 christos high_pc = read_address (unit, rngs_ptr, rngs_end);
3266 1.9 christos rngs_ptr += unit->addr_size;
3267 1.9 christos break;
3268 1.9 christos
3269 1.9 christos /* TODO x-variants need .debug_addr support used for split-dwarf. */
3270 1.9 christos case DW_RLE_base_addressx:
3271 1.9 christos case DW_RLE_startx_endx:
3272 1.9 christos case DW_RLE_startx_length:
3273 1.9 christos default:
3274 1.9 christos return FALSE;
3275 1.9 christos }
3276 1.9 christos
3277 1.9 christos if ((low_pc == 0 && high_pc == 0) || low_pc == high_pc)
3278 1.9 christos return FALSE;
3279 1.9 christos
3280 1.9 christos if (!arange_add (unit, arange, low_pc, high_pc))
3281 1.9 christos return FALSE;
3282 1.9 christos }
3283 1.9 christos }
3284 1.9 christos
3285 1.9 christos static bfd_boolean
3286 1.9 christos read_rangelist (struct comp_unit *unit, struct arange *arange,
3287 1.9 christos bfd_uint64_t offset)
3288 1.9 christos {
3289 1.9 christos if (unit->version <= 4)
3290 1.9 christos return read_ranges (unit, arange, offset);
3291 1.9 christos else
3292 1.9 christos return read_rnglists (unit, arange, offset);
3293 1.9 christos }
3294 1.9 christos
3295 1.9 christos static struct varinfo *
3296 1.9 christos lookup_var_by_offset (bfd_uint64_t offset, struct varinfo * table)
3297 1.9 christos {
3298 1.9 christos while (table)
3299 1.9 christos {
3300 1.9 christos if (table->unit_offset == offset)
3301 1.9 christos return table;
3302 1.9 christos table = table->prev_var;
3303 1.9 christos }
3304 1.9 christos
3305 1.9 christos return NULL;
3306 1.9 christos }
3307 1.9 christos
3308 1.9 christos
3309 1.1 christos /* DWARF2 Compilation unit functions. */
3310 1.1 christos
3311 1.1 christos /* Scan over each die in a comp. unit looking for functions to add
3312 1.1 christos to the function table and variables to the variable table. */
3313 1.1 christos
3314 1.1 christos static bfd_boolean
3315 1.1 christos scan_unit_for_symbols (struct comp_unit *unit)
3316 1.1 christos {
3317 1.1 christos bfd *abfd = unit->abfd;
3318 1.1 christos bfd_byte *info_ptr = unit->first_child_die_ptr;
3319 1.9 christos bfd_byte *info_ptr_end = unit->end_ptr;
3320 1.8 christos int nesting_level = 0;
3321 1.8 christos struct nest_funcinfo {
3322 1.8 christos struct funcinfo *func;
3323 1.8 christos } *nested_funcs;
3324 1.1 christos int nested_funcs_size;
3325 1.1 christos
3326 1.1 christos /* Maintain a stack of in-scope functions and inlined functions, which we
3327 1.1 christos can use to set the caller_func field. */
3328 1.1 christos nested_funcs_size = 32;
3329 1.8 christos nested_funcs = (struct nest_funcinfo *)
3330 1.8 christos bfd_malloc (nested_funcs_size * sizeof (*nested_funcs));
3331 1.1 christos if (nested_funcs == NULL)
3332 1.1 christos return FALSE;
3333 1.8 christos nested_funcs[nesting_level].func = 0;
3334 1.1 christos
3335 1.8 christos while (nesting_level >= 0)
3336 1.1 christos {
3337 1.1 christos unsigned int abbrev_number, bytes_read, i;
3338 1.1 christos struct abbrev_info *abbrev;
3339 1.1 christos struct attribute attr;
3340 1.1 christos struct funcinfo *func;
3341 1.1 christos struct varinfo *var;
3342 1.1 christos bfd_vma low_pc = 0;
3343 1.1 christos bfd_vma high_pc = 0;
3344 1.1 christos bfd_boolean high_pc_relative = FALSE;
3345 1.9 christos bfd_uint64_t current_offset;
3346 1.1 christos
3347 1.5 christos /* PR 17512: file: 9f405d9d. */
3348 1.5 christos if (info_ptr >= info_ptr_end)
3349 1.5 christos goto fail;
3350 1.5 christos
3351 1.9 christos current_offset = info_ptr - unit->info_ptr_unit;
3352 1.7 christos abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
3353 1.7 christos FALSE, info_ptr_end);
3354 1.1 christos info_ptr += bytes_read;
3355 1.1 christos
3356 1.1 christos if (! abbrev_number)
3357 1.1 christos {
3358 1.1 christos nesting_level--;
3359 1.1 christos continue;
3360 1.1 christos }
3361 1.1 christos
3362 1.7 christos abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3363 1.1 christos if (! abbrev)
3364 1.1 christos {
3365 1.7 christos static unsigned int previous_failed_abbrev = -1U;
3366 1.7 christos
3367 1.7 christos /* Avoid multiple reports of the same missing abbrev. */
3368 1.7 christos if (abbrev_number != previous_failed_abbrev)
3369 1.7 christos {
3370 1.7 christos _bfd_error_handler
3371 1.8 christos (_("DWARF error: could not find abbrev number %u"),
3372 1.7 christos abbrev_number);
3373 1.7 christos previous_failed_abbrev = abbrev_number;
3374 1.7 christos }
3375 1.1 christos bfd_set_error (bfd_error_bad_value);
3376 1.1 christos goto fail;
3377 1.1 christos }
3378 1.1 christos
3379 1.1 christos if (abbrev->tag == DW_TAG_subprogram
3380 1.1 christos || abbrev->tag == DW_TAG_entry_point
3381 1.1 christos || abbrev->tag == DW_TAG_inlined_subroutine)
3382 1.1 christos {
3383 1.9 christos size_t amt = sizeof (struct funcinfo);
3384 1.9 christos
3385 1.9 christos var = NULL;
3386 1.1 christos func = (struct funcinfo *) bfd_zalloc (abfd, amt);
3387 1.1 christos if (func == NULL)
3388 1.1 christos goto fail;
3389 1.1 christos func->tag = abbrev->tag;
3390 1.1 christos func->prev_func = unit->function_table;
3391 1.1 christos unit->function_table = func;
3392 1.7 christos unit->number_of_functions++;
3393 1.1 christos BFD_ASSERT (!unit->cached);
3394 1.1 christos
3395 1.1 christos if (func->tag == DW_TAG_inlined_subroutine)
3396 1.8 christos for (i = nesting_level; i-- != 0; )
3397 1.8 christos if (nested_funcs[i].func)
3398 1.1 christos {
3399 1.8 christos func->caller_func = nested_funcs[i].func;
3400 1.1 christos break;
3401 1.1 christos }
3402 1.8 christos nested_funcs[nesting_level].func = func;
3403 1.1 christos }
3404 1.1 christos else
3405 1.1 christos {
3406 1.1 christos func = NULL;
3407 1.9 christos if (abbrev->tag == DW_TAG_variable
3408 1.9 christos || abbrev->tag == DW_TAG_member)
3409 1.1 christos {
3410 1.9 christos size_t amt = sizeof (struct varinfo);
3411 1.1 christos var = (struct varinfo *) bfd_zalloc (abfd, amt);
3412 1.1 christos if (var == NULL)
3413 1.1 christos goto fail;
3414 1.1 christos var->tag = abbrev->tag;
3415 1.9 christos var->stack = TRUE;
3416 1.1 christos var->prev_var = unit->variable_table;
3417 1.1 christos unit->variable_table = var;
3418 1.9 christos var->unit_offset = current_offset;
3419 1.7 christos /* PR 18205: Missing debug information can cause this
3420 1.7 christos var to be attached to an already cached unit. */
3421 1.1 christos }
3422 1.9 christos else
3423 1.9 christos var = NULL;
3424 1.1 christos
3425 1.1 christos /* No inline function in scope at this nesting level. */
3426 1.8 christos nested_funcs[nesting_level].func = 0;
3427 1.1 christos }
3428 1.1 christos
3429 1.1 christos for (i = 0; i < abbrev->num_attrs; ++i)
3430 1.1 christos {
3431 1.8 christos info_ptr = read_attribute (&attr, &abbrev->attrs[i],
3432 1.8 christos unit, info_ptr, info_ptr_end);
3433 1.1 christos if (info_ptr == NULL)
3434 1.1 christos goto fail;
3435 1.1 christos
3436 1.1 christos if (func)
3437 1.1 christos {
3438 1.1 christos switch (attr.name)
3439 1.1 christos {
3440 1.1 christos case DW_AT_call_file:
3441 1.1 christos func->caller_file = concat_filename (unit->line_table,
3442 1.1 christos attr.u.val);
3443 1.1 christos break;
3444 1.1 christos
3445 1.1 christos case DW_AT_call_line:
3446 1.1 christos func->caller_line = attr.u.val;
3447 1.1 christos break;
3448 1.1 christos
3449 1.1 christos case DW_AT_abstract_origin:
3450 1.1 christos case DW_AT_specification:
3451 1.9 christos if (!find_abstract_instance (unit, &attr, 0,
3452 1.8 christos &func->name,
3453 1.8 christos &func->is_linkage,
3454 1.8 christos &func->file,
3455 1.8 christos &func->line))
3456 1.8 christos goto fail;
3457 1.1 christos break;
3458 1.1 christos
3459 1.1 christos case DW_AT_name:
3460 1.1 christos /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3461 1.1 christos over DW_AT_name. */
3462 1.3 christos if (func->name == NULL && is_str_attr (attr.form))
3463 1.3 christos {
3464 1.3 christos func->name = attr.u.str;
3465 1.3 christos if (non_mangled (unit->lang))
3466 1.3 christos func->is_linkage = TRUE;
3467 1.3 christos }
3468 1.1 christos break;
3469 1.1 christos
3470 1.1 christos case DW_AT_linkage_name:
3471 1.1 christos case DW_AT_MIPS_linkage_name:
3472 1.3 christos /* PR 16949: Corrupt debug info can place
3473 1.3 christos non-string forms into these attributes. */
3474 1.3 christos if (is_str_attr (attr.form))
3475 1.3 christos {
3476 1.3 christos func->name = attr.u.str;
3477 1.3 christos func->is_linkage = TRUE;
3478 1.3 christos }
3479 1.1 christos break;
3480 1.1 christos
3481 1.1 christos case DW_AT_low_pc:
3482 1.1 christos low_pc = attr.u.val;
3483 1.1 christos break;
3484 1.1 christos
3485 1.1 christos case DW_AT_high_pc:
3486 1.1 christos high_pc = attr.u.val;
3487 1.1 christos high_pc_relative = attr.form != DW_FORM_addr;
3488 1.1 christos break;
3489 1.1 christos
3490 1.1 christos case DW_AT_ranges:
3491 1.1 christos if (!read_rangelist (unit, &func->arange, attr.u.val))
3492 1.1 christos goto fail;
3493 1.1 christos break;
3494 1.1 christos
3495 1.1 christos case DW_AT_decl_file:
3496 1.1 christos func->file = concat_filename (unit->line_table,
3497 1.1 christos attr.u.val);
3498 1.1 christos break;
3499 1.1 christos
3500 1.1 christos case DW_AT_decl_line:
3501 1.1 christos func->line = attr.u.val;
3502 1.1 christos break;
3503 1.1 christos
3504 1.1 christos default:
3505 1.1 christos break;
3506 1.1 christos }
3507 1.1 christos }
3508 1.1 christos else if (var)
3509 1.1 christos {
3510 1.1 christos switch (attr.name)
3511 1.1 christos {
3512 1.9 christos case DW_AT_specification:
3513 1.9 christos if (attr.u.val)
3514 1.9 christos {
3515 1.9 christos struct varinfo * spec_var;
3516 1.9 christos
3517 1.9 christos spec_var = lookup_var_by_offset (attr.u.val,
3518 1.9 christos unit->variable_table);
3519 1.9 christos if (spec_var == NULL)
3520 1.9 christos {
3521 1.9 christos _bfd_error_handler (_("DWARF error: could not find "
3522 1.9 christos "variable specification "
3523 1.9 christos "at offset %lx"),
3524 1.9 christos (unsigned long) attr.u.val);
3525 1.9 christos break;
3526 1.9 christos }
3527 1.9 christos
3528 1.9 christos if (var->name == NULL)
3529 1.9 christos var->name = spec_var->name;
3530 1.9 christos if (var->file == NULL && spec_var->file != NULL)
3531 1.9 christos var->file = strdup (spec_var->file);
3532 1.9 christos if (var->line == 0)
3533 1.9 christos var->line = spec_var->line;
3534 1.9 christos if (var->sec == NULL)
3535 1.9 christos var->sec = spec_var->sec;
3536 1.9 christos }
3537 1.9 christos break;
3538 1.9 christos
3539 1.1 christos case DW_AT_name:
3540 1.8 christos if (is_str_attr (attr.form))
3541 1.8 christos var->name = attr.u.str;
3542 1.1 christos break;
3543 1.1 christos
3544 1.1 christos case DW_AT_decl_file:
3545 1.1 christos var->file = concat_filename (unit->line_table,
3546 1.1 christos attr.u.val);
3547 1.1 christos break;
3548 1.1 christos
3549 1.1 christos case DW_AT_decl_line:
3550 1.1 christos var->line = attr.u.val;
3551 1.1 christos break;
3552 1.1 christos
3553 1.1 christos case DW_AT_external:
3554 1.1 christos if (attr.u.val != 0)
3555 1.9 christos var->stack = FALSE;
3556 1.1 christos break;
3557 1.1 christos
3558 1.1 christos case DW_AT_location:
3559 1.1 christos switch (attr.form)
3560 1.1 christos {
3561 1.1 christos case DW_FORM_block:
3562 1.1 christos case DW_FORM_block1:
3563 1.1 christos case DW_FORM_block2:
3564 1.1 christos case DW_FORM_block4:
3565 1.1 christos case DW_FORM_exprloc:
3566 1.8 christos if (attr.u.blk->data != NULL
3567 1.8 christos && *attr.u.blk->data == DW_OP_addr)
3568 1.1 christos {
3569 1.9 christos var->stack = FALSE;
3570 1.1 christos
3571 1.1 christos /* Verify that DW_OP_addr is the only opcode in the
3572 1.1 christos location, in which case the block size will be 1
3573 1.1 christos plus the address size. */
3574 1.1 christos /* ??? For TLS variables, gcc can emit
3575 1.1 christos DW_OP_addr <addr> DW_OP_GNU_push_tls_address
3576 1.1 christos which we don't handle here yet. */
3577 1.1 christos if (attr.u.blk->size == unit->addr_size + 1U)
3578 1.1 christos var->addr = bfd_get (unit->addr_size * 8,
3579 1.1 christos unit->abfd,
3580 1.1 christos attr.u.blk->data + 1);
3581 1.1 christos }
3582 1.1 christos break;
3583 1.1 christos
3584 1.1 christos default:
3585 1.1 christos break;
3586 1.1 christos }
3587 1.1 christos break;
3588 1.1 christos
3589 1.1 christos default:
3590 1.1 christos break;
3591 1.1 christos }
3592 1.1 christos }
3593 1.1 christos }
3594 1.1 christos
3595 1.1 christos if (high_pc_relative)
3596 1.1 christos high_pc += low_pc;
3597 1.1 christos
3598 1.1 christos if (func && high_pc != 0)
3599 1.1 christos {
3600 1.1 christos if (!arange_add (unit, &func->arange, low_pc, high_pc))
3601 1.1 christos goto fail;
3602 1.1 christos }
3603 1.1 christos
3604 1.1 christos if (abbrev->has_children)
3605 1.1 christos {
3606 1.1 christos nesting_level++;
3607 1.1 christos
3608 1.1 christos if (nesting_level >= nested_funcs_size)
3609 1.1 christos {
3610 1.8 christos struct nest_funcinfo *tmp;
3611 1.1 christos
3612 1.1 christos nested_funcs_size *= 2;
3613 1.8 christos tmp = (struct nest_funcinfo *)
3614 1.1 christos bfd_realloc (nested_funcs,
3615 1.8 christos nested_funcs_size * sizeof (*nested_funcs));
3616 1.1 christos if (tmp == NULL)
3617 1.1 christos goto fail;
3618 1.1 christos nested_funcs = tmp;
3619 1.1 christos }
3620 1.8 christos nested_funcs[nesting_level].func = 0;
3621 1.1 christos }
3622 1.1 christos }
3623 1.1 christos
3624 1.1 christos free (nested_funcs);
3625 1.1 christos return TRUE;
3626 1.1 christos
3627 1.1 christos fail:
3628 1.1 christos free (nested_funcs);
3629 1.1 christos return FALSE;
3630 1.1 christos }
3631 1.1 christos
3632 1.9 christos /* Parse a DWARF2 compilation unit starting at INFO_PTR. UNIT_LENGTH
3633 1.1 christos includes the compilation unit header that proceeds the DIE's, but
3634 1.1 christos does not include the length field that precedes each compilation
3635 1.1 christos unit header. END_PTR points one past the end of this comp unit.
3636 1.1 christos OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
3637 1.1 christos
3638 1.1 christos This routine does not read the whole compilation unit; only enough
3639 1.1 christos to get to the line number information for the compilation unit. */
3640 1.1 christos
3641 1.1 christos static struct comp_unit *
3642 1.1 christos parse_comp_unit (struct dwarf2_debug *stash,
3643 1.9 christos struct dwarf2_debug_file *file,
3644 1.9 christos bfd_byte *info_ptr,
3645 1.1 christos bfd_vma unit_length,
3646 1.1 christos bfd_byte *info_ptr_unit,
3647 1.1 christos unsigned int offset_size)
3648 1.1 christos {
3649 1.1 christos struct comp_unit* unit;
3650 1.1 christos unsigned int version;
3651 1.1 christos bfd_uint64_t abbrev_offset = 0;
3652 1.8 christos /* Initialize it just to avoid a GCC false warning. */
3653 1.8 christos unsigned int addr_size = -1;
3654 1.1 christos struct abbrev_info** abbrevs;
3655 1.1 christos unsigned int abbrev_number, bytes_read, i;
3656 1.1 christos struct abbrev_info *abbrev;
3657 1.1 christos struct attribute attr;
3658 1.1 christos bfd_byte *end_ptr = info_ptr + unit_length;
3659 1.9 christos size_t amt;
3660 1.1 christos bfd_vma low_pc = 0;
3661 1.1 christos bfd_vma high_pc = 0;
3662 1.9 christos bfd *abfd = file->bfd_ptr;
3663 1.1 christos bfd_boolean high_pc_relative = FALSE;
3664 1.8 christos enum dwarf_unit_type unit_type;
3665 1.1 christos
3666 1.5 christos version = read_2_bytes (abfd, info_ptr, end_ptr);
3667 1.1 christos info_ptr += 2;
3668 1.8 christos if (version < 2 || version > 5)
3669 1.1 christos {
3670 1.6 christos /* PR 19872: A version number of 0 probably means that there is padding
3671 1.6 christos at the end of the .debug_info section. Gold puts it there when
3672 1.6 christos performing an incremental link, for example. So do not generate
3673 1.6 christos an error, just return a NULL. */
3674 1.6 christos if (version)
3675 1.6 christos {
3676 1.7 christos _bfd_error_handler
3677 1.8 christos (_("DWARF error: found dwarf version '%u', this reader"
3678 1.8 christos " only handles version 2, 3, 4 and 5 information"), version);
3679 1.6 christos bfd_set_error (bfd_error_bad_value);
3680 1.6 christos }
3681 1.6 christos return NULL;
3682 1.1 christos }
3683 1.1 christos
3684 1.8 christos if (version < 5)
3685 1.8 christos unit_type = DW_UT_compile;
3686 1.8 christos else
3687 1.8 christos {
3688 1.8 christos unit_type = read_1_byte (abfd, info_ptr, end_ptr);
3689 1.8 christos info_ptr += 1;
3690 1.8 christos
3691 1.8 christos addr_size = read_1_byte (abfd, info_ptr, end_ptr);
3692 1.8 christos info_ptr += 1;
3693 1.8 christos }
3694 1.8 christos
3695 1.8 christos BFD_ASSERT (offset_size == 4 || offset_size == 8);
3696 1.8 christos if (offset_size == 4)
3697 1.8 christos abbrev_offset = read_4_bytes (abfd, info_ptr, end_ptr);
3698 1.8 christos else
3699 1.8 christos abbrev_offset = read_8_bytes (abfd, info_ptr, end_ptr);
3700 1.8 christos info_ptr += offset_size;
3701 1.8 christos
3702 1.8 christos if (version < 5)
3703 1.8 christos {
3704 1.8 christos addr_size = read_1_byte (abfd, info_ptr, end_ptr);
3705 1.8 christos info_ptr += 1;
3706 1.8 christos }
3707 1.8 christos
3708 1.8 christos if (unit_type == DW_UT_type)
3709 1.8 christos {
3710 1.8 christos /* Skip type signature. */
3711 1.8 christos info_ptr += 8;
3712 1.8 christos
3713 1.8 christos /* Skip type offset. */
3714 1.8 christos info_ptr += offset_size;
3715 1.8 christos }
3716 1.8 christos
3717 1.1 christos if (addr_size > sizeof (bfd_vma))
3718 1.1 christos {
3719 1.7 christos _bfd_error_handler
3720 1.7 christos /* xgettext: c-format */
3721 1.8 christos (_("DWARF error: found address size '%u', this reader"
3722 1.8 christos " can not handle sizes greater than '%u'"),
3723 1.1 christos addr_size,
3724 1.1 christos (unsigned int) sizeof (bfd_vma));
3725 1.1 christos bfd_set_error (bfd_error_bad_value);
3726 1.6 christos return NULL;
3727 1.1 christos }
3728 1.1 christos
3729 1.1 christos if (addr_size != 2 && addr_size != 4 && addr_size != 8)
3730 1.1 christos {
3731 1.7 christos _bfd_error_handler
3732 1.8 christos ("DWARF error: found address size '%u', this reader"
3733 1.8 christos " can only handle address sizes '2', '4' and '8'", addr_size);
3734 1.1 christos bfd_set_error (bfd_error_bad_value);
3735 1.6 christos return NULL;
3736 1.1 christos }
3737 1.1 christos
3738 1.1 christos /* Read the abbrevs for this compilation unit into a table. */
3739 1.9 christos abbrevs = read_abbrevs (abfd, abbrev_offset, stash, file);
3740 1.1 christos if (! abbrevs)
3741 1.6 christos return NULL;
3742 1.1 christos
3743 1.7 christos abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
3744 1.7 christos FALSE, end_ptr);
3745 1.1 christos info_ptr += bytes_read;
3746 1.1 christos if (! abbrev_number)
3747 1.1 christos {
3748 1.6 christos /* PR 19872: An abbrev number of 0 probably means that there is padding
3749 1.6 christos at the end of the .debug_abbrev section. Gold puts it there when
3750 1.6 christos performing an incremental link, for example. So do not generate
3751 1.6 christos an error, just return a NULL. */
3752 1.6 christos return NULL;
3753 1.1 christos }
3754 1.1 christos
3755 1.1 christos abbrev = lookup_abbrev (abbrev_number, abbrevs);
3756 1.1 christos if (! abbrev)
3757 1.1 christos {
3758 1.8 christos _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
3759 1.7 christos abbrev_number);
3760 1.1 christos bfd_set_error (bfd_error_bad_value);
3761 1.6 christos return NULL;
3762 1.1 christos }
3763 1.1 christos
3764 1.1 christos amt = sizeof (struct comp_unit);
3765 1.1 christos unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
3766 1.1 christos if (unit == NULL)
3767 1.1 christos return NULL;
3768 1.1 christos unit->abfd = abfd;
3769 1.1 christos unit->version = version;
3770 1.1 christos unit->addr_size = addr_size;
3771 1.1 christos unit->offset_size = offset_size;
3772 1.1 christos unit->abbrevs = abbrevs;
3773 1.1 christos unit->end_ptr = end_ptr;
3774 1.1 christos unit->stash = stash;
3775 1.9 christos unit->file = file;
3776 1.1 christos unit->info_ptr_unit = info_ptr_unit;
3777 1.1 christos
3778 1.1 christos for (i = 0; i < abbrev->num_attrs; ++i)
3779 1.1 christos {
3780 1.5 christos info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, end_ptr);
3781 1.1 christos if (info_ptr == NULL)
3782 1.1 christos return NULL;
3783 1.1 christos
3784 1.1 christos /* Store the data if it is of an attribute we want to keep in a
3785 1.1 christos partial symbol table. */
3786 1.1 christos switch (attr.name)
3787 1.1 christos {
3788 1.1 christos case DW_AT_stmt_list:
3789 1.1 christos unit->stmtlist = 1;
3790 1.1 christos unit->line_offset = attr.u.val;
3791 1.1 christos break;
3792 1.1 christos
3793 1.1 christos case DW_AT_name:
3794 1.8 christos if (is_str_attr (attr.form))
3795 1.8 christos unit->name = attr.u.str;
3796 1.1 christos break;
3797 1.1 christos
3798 1.1 christos case DW_AT_low_pc:
3799 1.1 christos low_pc = attr.u.val;
3800 1.1 christos /* If the compilation unit DIE has a DW_AT_low_pc attribute,
3801 1.1 christos this is the base address to use when reading location
3802 1.7 christos lists or range lists. */
3803 1.1 christos if (abbrev->tag == DW_TAG_compile_unit)
3804 1.1 christos unit->base_address = low_pc;
3805 1.1 christos break;
3806 1.1 christos
3807 1.1 christos case DW_AT_high_pc:
3808 1.1 christos high_pc = attr.u.val;
3809 1.1 christos high_pc_relative = attr.form != DW_FORM_addr;
3810 1.1 christos break;
3811 1.1 christos
3812 1.1 christos case DW_AT_ranges:
3813 1.1 christos if (!read_rangelist (unit, &unit->arange, attr.u.val))
3814 1.1 christos return NULL;
3815 1.1 christos break;
3816 1.1 christos
3817 1.1 christos case DW_AT_comp_dir:
3818 1.1 christos {
3819 1.1 christos char *comp_dir = attr.u.str;
3820 1.5 christos
3821 1.5 christos /* PR 17512: file: 1fe726be. */
3822 1.5 christos if (! is_str_attr (attr.form))
3823 1.5 christos {
3824 1.7 christos _bfd_error_handler
3825 1.8 christos (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
3826 1.5 christos comp_dir = NULL;
3827 1.5 christos }
3828 1.5 christos
3829 1.1 christos if (comp_dir)
3830 1.1 christos {
3831 1.1 christos /* Irix 6.2 native cc prepends <machine>.: to the compilation
3832 1.1 christos directory, get rid of it. */
3833 1.1 christos char *cp = strchr (comp_dir, ':');
3834 1.1 christos
3835 1.1 christos if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
3836 1.1 christos comp_dir = cp + 1;
3837 1.1 christos }
3838 1.1 christos unit->comp_dir = comp_dir;
3839 1.1 christos break;
3840 1.1 christos }
3841 1.1 christos
3842 1.3 christos case DW_AT_language:
3843 1.3 christos unit->lang = attr.u.val;
3844 1.3 christos break;
3845 1.3 christos
3846 1.1 christos default:
3847 1.1 christos break;
3848 1.1 christos }
3849 1.1 christos }
3850 1.1 christos if (high_pc_relative)
3851 1.1 christos high_pc += low_pc;
3852 1.1 christos if (high_pc != 0)
3853 1.1 christos {
3854 1.1 christos if (!arange_add (unit, &unit->arange, low_pc, high_pc))
3855 1.1 christos return NULL;
3856 1.1 christos }
3857 1.1 christos
3858 1.1 christos unit->first_child_die_ptr = info_ptr;
3859 1.1 christos return unit;
3860 1.1 christos }
3861 1.1 christos
3862 1.1 christos /* Return TRUE if UNIT may contain the address given by ADDR. When
3863 1.1 christos there are functions written entirely with inline asm statements, the
3864 1.1 christos range info in the compilation unit header may not be correct. We
3865 1.1 christos need to consult the line info table to see if a compilation unit
3866 1.1 christos really contains the given address. */
3867 1.1 christos
3868 1.1 christos static bfd_boolean
3869 1.1 christos comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
3870 1.1 christos {
3871 1.1 christos struct arange *arange;
3872 1.1 christos
3873 1.1 christos if (unit->error)
3874 1.1 christos return FALSE;
3875 1.1 christos
3876 1.1 christos arange = &unit->arange;
3877 1.1 christos do
3878 1.1 christos {
3879 1.1 christos if (addr >= arange->low && addr < arange->high)
3880 1.1 christos return TRUE;
3881 1.1 christos arange = arange->next;
3882 1.1 christos }
3883 1.1 christos while (arange);
3884 1.1 christos
3885 1.1 christos return FALSE;
3886 1.1 christos }
3887 1.1 christos
3888 1.1 christos /* If UNIT contains ADDR, set the output parameters to the values for
3889 1.1 christos the line containing ADDR. The output parameters, FILENAME_PTR,
3890 1.3 christos FUNCTION_PTR, and LINENUMBER_PTR, are pointers to the objects
3891 1.1 christos to be filled in.
3892 1.1 christos
3893 1.1 christos Returns the range of addresses covered by the entry that was used
3894 1.1 christos to fill in *LINENUMBER_PTR or 0 if it was not filled in. */
3895 1.1 christos
3896 1.1 christos static bfd_vma
3897 1.1 christos comp_unit_find_nearest_line (struct comp_unit *unit,
3898 1.1 christos bfd_vma addr,
3899 1.1 christos const char **filename_ptr,
3900 1.3 christos struct funcinfo **function_ptr,
3901 1.1 christos unsigned int *linenumber_ptr,
3902 1.9 christos unsigned int *discriminator_ptr)
3903 1.1 christos {
3904 1.1 christos bfd_boolean func_p;
3905 1.1 christos
3906 1.9 christos if (!comp_unit_maybe_decode_line_info (unit))
3907 1.1 christos return FALSE;
3908 1.1 christos
3909 1.3 christos *function_ptr = NULL;
3910 1.3 christos func_p = lookup_address_in_function_table (unit, addr, function_ptr);
3911 1.3 christos if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine)
3912 1.9 christos unit->stash->inliner_chain = *function_ptr;
3913 1.1 christos
3914 1.1 christos return lookup_address_in_line_info_table (unit->line_table, addr,
3915 1.1 christos filename_ptr,
3916 1.1 christos linenumber_ptr,
3917 1.1 christos discriminator_ptr);
3918 1.1 christos }
3919 1.1 christos
3920 1.1 christos /* Check to see if line info is already decoded in a comp_unit.
3921 1.1 christos If not, decode it. Returns TRUE if no errors were encountered;
3922 1.1 christos FALSE otherwise. */
3923 1.1 christos
3924 1.1 christos static bfd_boolean
3925 1.9 christos comp_unit_maybe_decode_line_info (struct comp_unit *unit)
3926 1.1 christos {
3927 1.1 christos if (unit->error)
3928 1.1 christos return FALSE;
3929 1.1 christos
3930 1.1 christos if (! unit->line_table)
3931 1.1 christos {
3932 1.1 christos if (! unit->stmtlist)
3933 1.1 christos {
3934 1.1 christos unit->error = 1;
3935 1.1 christos return FALSE;
3936 1.1 christos }
3937 1.1 christos
3938 1.9 christos unit->line_table = decode_line_info (unit);
3939 1.1 christos
3940 1.1 christos if (! unit->line_table)
3941 1.1 christos {
3942 1.1 christos unit->error = 1;
3943 1.1 christos return FALSE;
3944 1.1 christos }
3945 1.1 christos
3946 1.1 christos if (unit->first_child_die_ptr < unit->end_ptr
3947 1.1 christos && ! scan_unit_for_symbols (unit))
3948 1.1 christos {
3949 1.1 christos unit->error = 1;
3950 1.1 christos return FALSE;
3951 1.1 christos }
3952 1.1 christos }
3953 1.1 christos
3954 1.1 christos return TRUE;
3955 1.1 christos }
3956 1.1 christos
3957 1.1 christos /* If UNIT contains SYM at ADDR, set the output parameters to the
3958 1.1 christos values for the line containing SYM. The output parameters,
3959 1.1 christos FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
3960 1.1 christos filled in.
3961 1.1 christos
3962 1.1 christos Return TRUE if UNIT contains SYM, and no errors were encountered;
3963 1.1 christos FALSE otherwise. */
3964 1.1 christos
3965 1.1 christos static bfd_boolean
3966 1.1 christos comp_unit_find_line (struct comp_unit *unit,
3967 1.1 christos asymbol *sym,
3968 1.1 christos bfd_vma addr,
3969 1.1 christos const char **filename_ptr,
3970 1.9 christos unsigned int *linenumber_ptr)
3971 1.1 christos {
3972 1.9 christos if (!comp_unit_maybe_decode_line_info (unit))
3973 1.1 christos return FALSE;
3974 1.1 christos
3975 1.1 christos if (sym->flags & BSF_FUNCTION)
3976 1.1 christos return lookup_symbol_in_function_table (unit, sym, addr,
3977 1.1 christos filename_ptr,
3978 1.1 christos linenumber_ptr);
3979 1.1 christos
3980 1.1 christos return lookup_symbol_in_variable_table (unit, sym, addr,
3981 1.1 christos filename_ptr,
3982 1.1 christos linenumber_ptr);
3983 1.1 christos }
3984 1.1 christos
3985 1.1 christos static struct funcinfo *
3986 1.1 christos reverse_funcinfo_list (struct funcinfo *head)
3987 1.1 christos {
3988 1.1 christos struct funcinfo *rhead;
3989 1.1 christos struct funcinfo *temp;
3990 1.1 christos
3991 1.1 christos for (rhead = NULL; head; head = temp)
3992 1.1 christos {
3993 1.1 christos temp = head->prev_func;
3994 1.1 christos head->prev_func = rhead;
3995 1.1 christos rhead = head;
3996 1.1 christos }
3997 1.1 christos return rhead;
3998 1.1 christos }
3999 1.1 christos
4000 1.1 christos static struct varinfo *
4001 1.1 christos reverse_varinfo_list (struct varinfo *head)
4002 1.1 christos {
4003 1.1 christos struct varinfo *rhead;
4004 1.1 christos struct varinfo *temp;
4005 1.1 christos
4006 1.1 christos for (rhead = NULL; head; head = temp)
4007 1.1 christos {
4008 1.1 christos temp = head->prev_var;
4009 1.1 christos head->prev_var = rhead;
4010 1.1 christos rhead = head;
4011 1.1 christos }
4012 1.1 christos return rhead;
4013 1.1 christos }
4014 1.1 christos
4015 1.1 christos /* Extract all interesting funcinfos and varinfos of a compilation
4016 1.1 christos unit into hash tables for faster lookup. Returns TRUE if no
4017 1.1 christos errors were enountered; FALSE otherwise. */
4018 1.1 christos
4019 1.1 christos static bfd_boolean
4020 1.1 christos comp_unit_hash_info (struct dwarf2_debug *stash,
4021 1.1 christos struct comp_unit *unit,
4022 1.1 christos struct info_hash_table *funcinfo_hash_table,
4023 1.1 christos struct info_hash_table *varinfo_hash_table)
4024 1.1 christos {
4025 1.1 christos struct funcinfo* each_func;
4026 1.1 christos struct varinfo* each_var;
4027 1.1 christos bfd_boolean okay = TRUE;
4028 1.1 christos
4029 1.1 christos BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
4030 1.1 christos
4031 1.9 christos if (!comp_unit_maybe_decode_line_info (unit))
4032 1.1 christos return FALSE;
4033 1.1 christos
4034 1.1 christos BFD_ASSERT (!unit->cached);
4035 1.1 christos
4036 1.1 christos /* To preserve the original search order, we went to visit the function
4037 1.1 christos infos in the reversed order of the list. However, making the list
4038 1.1 christos bi-directional use quite a bit of extra memory. So we reverse
4039 1.1 christos the list first, traverse the list in the now reversed order and
4040 1.1 christos finally reverse the list again to get back the original order. */
4041 1.1 christos unit->function_table = reverse_funcinfo_list (unit->function_table);
4042 1.1 christos for (each_func = unit->function_table;
4043 1.1 christos each_func && okay;
4044 1.1 christos each_func = each_func->prev_func)
4045 1.1 christos {
4046 1.7 christos /* Skip nameless functions. */
4047 1.1 christos if (each_func->name)
4048 1.1 christos /* There is no need to copy name string into hash table as
4049 1.1 christos name string is either in the dwarf string buffer or
4050 1.1 christos info in the stash. */
4051 1.1 christos okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
4052 1.1 christos (void*) each_func, FALSE);
4053 1.1 christos }
4054 1.1 christos unit->function_table = reverse_funcinfo_list (unit->function_table);
4055 1.1 christos if (!okay)
4056 1.1 christos return FALSE;
4057 1.1 christos
4058 1.1 christos /* We do the same for variable infos. */
4059 1.1 christos unit->variable_table = reverse_varinfo_list (unit->variable_table);
4060 1.1 christos for (each_var = unit->variable_table;
4061 1.1 christos each_var && okay;
4062 1.1 christos each_var = each_var->prev_var)
4063 1.1 christos {
4064 1.1 christos /* Skip stack vars and vars with no files or names. */
4065 1.9 christos if (! each_var->stack
4066 1.1 christos && each_var->file != NULL
4067 1.1 christos && each_var->name != NULL)
4068 1.1 christos /* There is no need to copy name string into hash table as
4069 1.1 christos name string is either in the dwarf string buffer or
4070 1.1 christos info in the stash. */
4071 1.1 christos okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
4072 1.1 christos (void*) each_var, FALSE);
4073 1.1 christos }
4074 1.1 christos
4075 1.1 christos unit->variable_table = reverse_varinfo_list (unit->variable_table);
4076 1.1 christos unit->cached = TRUE;
4077 1.1 christos return okay;
4078 1.1 christos }
4079 1.1 christos
4080 1.1 christos /* Locate a section in a BFD containing debugging info. The search starts
4081 1.1 christos from the section after AFTER_SEC, or from the first section in the BFD if
4082 1.1 christos AFTER_SEC is NULL. The search works by examining the names of the
4083 1.1 christos sections. There are three permissiable names. The first two are given
4084 1.1 christos by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
4085 1.1 christos and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
4086 1.1 christos This is a variation on the .debug_info section which has a checksum
4087 1.1 christos describing the contents appended onto the name. This allows the linker to
4088 1.1 christos identify and discard duplicate debugging sections for different
4089 1.1 christos compilation units. */
4090 1.1 christos #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
4091 1.1 christos
4092 1.1 christos static asection *
4093 1.1 christos find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
4094 1.3 christos asection *after_sec)
4095 1.1 christos {
4096 1.1 christos asection *msec;
4097 1.1 christos const char *look;
4098 1.1 christos
4099 1.1 christos if (after_sec == NULL)
4100 1.1 christos {
4101 1.1 christos look = debug_sections[debug_info].uncompressed_name;
4102 1.1 christos msec = bfd_get_section_by_name (abfd, look);
4103 1.1 christos if (msec != NULL)
4104 1.1 christos return msec;
4105 1.1 christos
4106 1.1 christos look = debug_sections[debug_info].compressed_name;
4107 1.1 christos if (look != NULL)
4108 1.1 christos {
4109 1.1 christos msec = bfd_get_section_by_name (abfd, look);
4110 1.1 christos if (msec != NULL)
4111 1.1 christos return msec;
4112 1.1 christos }
4113 1.1 christos
4114 1.1 christos for (msec = abfd->sections; msec != NULL; msec = msec->next)
4115 1.1 christos if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
4116 1.1 christos return msec;
4117 1.1 christos
4118 1.1 christos return NULL;
4119 1.1 christos }
4120 1.1 christos
4121 1.1 christos for (msec = after_sec->next; msec != NULL; msec = msec->next)
4122 1.1 christos {
4123 1.1 christos look = debug_sections[debug_info].uncompressed_name;
4124 1.1 christos if (strcmp (msec->name, look) == 0)
4125 1.1 christos return msec;
4126 1.1 christos
4127 1.1 christos look = debug_sections[debug_info].compressed_name;
4128 1.1 christos if (look != NULL && strcmp (msec->name, look) == 0)
4129 1.1 christos return msec;
4130 1.1 christos
4131 1.1 christos if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
4132 1.1 christos return msec;
4133 1.1 christos }
4134 1.1 christos
4135 1.1 christos return NULL;
4136 1.1 christos }
4137 1.1 christos
4138 1.3 christos /* Transfer VMAs from object file to separate debug file. */
4139 1.3 christos
4140 1.3 christos static void
4141 1.3 christos set_debug_vma (bfd *orig_bfd, bfd *debug_bfd)
4142 1.3 christos {
4143 1.3 christos asection *s, *d;
4144 1.3 christos
4145 1.3 christos for (s = orig_bfd->sections, d = debug_bfd->sections;
4146 1.3 christos s != NULL && d != NULL;
4147 1.3 christos s = s->next, d = d->next)
4148 1.3 christos {
4149 1.3 christos if ((d->flags & SEC_DEBUGGING) != 0)
4150 1.3 christos break;
4151 1.3 christos /* ??? Assumes 1-1 correspondence between sections in the
4152 1.3 christos two files. */
4153 1.3 christos if (strcmp (s->name, d->name) == 0)
4154 1.3 christos {
4155 1.3 christos d->output_section = s->output_section;
4156 1.3 christos d->output_offset = s->output_offset;
4157 1.3 christos d->vma = s->vma;
4158 1.3 christos }
4159 1.3 christos }
4160 1.3 christos }
4161 1.3 christos
4162 1.9 christos /* If the dwarf2 info was found in a separate debug file, return the
4163 1.9 christos debug file section corresponding to the section in the original file
4164 1.9 christos and the debug file symbols. */
4165 1.9 christos
4166 1.9 christos static void
4167 1.9 christos _bfd_dwarf2_stash_syms (struct dwarf2_debug *stash, bfd *abfd,
4168 1.9 christos asection **sec, asymbol ***syms)
4169 1.9 christos {
4170 1.9 christos if (stash->f.bfd_ptr != abfd)
4171 1.9 christos {
4172 1.9 christos asection *s, *d;
4173 1.9 christos
4174 1.9 christos if (*sec == NULL)
4175 1.9 christos {
4176 1.9 christos *syms = stash->f.syms;
4177 1.9 christos return;
4178 1.9 christos }
4179 1.9 christos
4180 1.9 christos for (s = abfd->sections, d = stash->f.bfd_ptr->sections;
4181 1.9 christos s != NULL && d != NULL;
4182 1.9 christos s = s->next, d = d->next)
4183 1.9 christos {
4184 1.9 christos if ((d->flags & SEC_DEBUGGING) != 0)
4185 1.9 christos break;
4186 1.9 christos if (s == *sec
4187 1.9 christos && strcmp (s->name, d->name) == 0)
4188 1.9 christos {
4189 1.9 christos *sec = d;
4190 1.9 christos *syms = stash->f.syms;
4191 1.9 christos break;
4192 1.9 christos }
4193 1.9 christos }
4194 1.9 christos }
4195 1.9 christos }
4196 1.9 christos
4197 1.1 christos /* Unset vmas for adjusted sections in STASH. */
4198 1.1 christos
4199 1.1 christos static void
4200 1.1 christos unset_sections (struct dwarf2_debug *stash)
4201 1.1 christos {
4202 1.3 christos int i;
4203 1.1 christos struct adjusted_section *p;
4204 1.1 christos
4205 1.1 christos i = stash->adjusted_section_count;
4206 1.1 christos p = stash->adjusted_sections;
4207 1.1 christos for (; i > 0; i--, p++)
4208 1.1 christos p->section->vma = 0;
4209 1.1 christos }
4210 1.1 christos
4211 1.3 christos /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
4212 1.3 christos relocatable object file. VMAs are normally all zero in relocatable
4213 1.3 christos object files, so if we want to distinguish locations in sections by
4214 1.3 christos address we need to set VMAs so the sections do not overlap. We
4215 1.3 christos also set VMA on .debug_info so that when we have multiple
4216 1.3 christos .debug_info sections (or the linkonce variant) they also do not
4217 1.3 christos overlap. The multiple .debug_info sections make up a single
4218 1.3 christos logical section. ??? We should probably do the same for other
4219 1.3 christos debug sections. */
4220 1.1 christos
4221 1.1 christos static bfd_boolean
4222 1.3 christos place_sections (bfd *orig_bfd, struct dwarf2_debug *stash)
4223 1.1 christos {
4224 1.3 christos bfd *abfd;
4225 1.1 christos struct adjusted_section *p;
4226 1.3 christos int i;
4227 1.3 christos const char *debug_info_name;
4228 1.1 christos
4229 1.1 christos if (stash->adjusted_section_count != 0)
4230 1.1 christos {
4231 1.1 christos i = stash->adjusted_section_count;
4232 1.1 christos p = stash->adjusted_sections;
4233 1.1 christos for (; i > 0; i--, p++)
4234 1.1 christos p->section->vma = p->adj_vma;
4235 1.3 christos return TRUE;
4236 1.1 christos }
4237 1.3 christos
4238 1.3 christos debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
4239 1.3 christos i = 0;
4240 1.3 christos abfd = orig_bfd;
4241 1.3 christos while (1)
4242 1.1 christos {
4243 1.1 christos asection *sect;
4244 1.1 christos
4245 1.1 christos for (sect = abfd->sections; sect != NULL; sect = sect->next)
4246 1.1 christos {
4247 1.1 christos int is_debug_info;
4248 1.1 christos
4249 1.3 christos if ((sect->output_section != NULL
4250 1.3 christos && sect->output_section != sect
4251 1.3 christos && (sect->flags & SEC_DEBUGGING) == 0)
4252 1.3 christos || sect->vma != 0)
4253 1.1 christos continue;
4254 1.1 christos
4255 1.3 christos is_debug_info = (strcmp (sect->name, debug_info_name) == 0
4256 1.3 christos || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO));
4257 1.1 christos
4258 1.3 christos if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
4259 1.3 christos && !is_debug_info)
4260 1.1 christos continue;
4261 1.1 christos
4262 1.1 christos i++;
4263 1.1 christos }
4264 1.9 christos if (abfd == stash->f.bfd_ptr)
4265 1.3 christos break;
4266 1.9 christos abfd = stash->f.bfd_ptr;
4267 1.3 christos }
4268 1.1 christos
4269 1.3 christos if (i <= 1)
4270 1.3 christos stash->adjusted_section_count = -1;
4271 1.3 christos else
4272 1.3 christos {
4273 1.3 christos bfd_vma last_vma = 0, last_dwarf = 0;
4274 1.9 christos size_t amt = i * sizeof (struct adjusted_section);
4275 1.3 christos
4276 1.3 christos p = (struct adjusted_section *) bfd_malloc (amt);
4277 1.3 christos if (p == NULL)
4278 1.1 christos return FALSE;
4279 1.1 christos
4280 1.1 christos stash->adjusted_sections = p;
4281 1.1 christos stash->adjusted_section_count = i;
4282 1.1 christos
4283 1.3 christos abfd = orig_bfd;
4284 1.3 christos while (1)
4285 1.1 christos {
4286 1.3 christos asection *sect;
4287 1.1 christos
4288 1.3 christos for (sect = abfd->sections; sect != NULL; sect = sect->next)
4289 1.3 christos {
4290 1.3 christos bfd_size_type sz;
4291 1.3 christos int is_debug_info;
4292 1.1 christos
4293 1.3 christos if ((sect->output_section != NULL
4294 1.3 christos && sect->output_section != sect
4295 1.3 christos && (sect->flags & SEC_DEBUGGING) == 0)
4296 1.3 christos || sect->vma != 0)
4297 1.3 christos continue;
4298 1.1 christos
4299 1.3 christos is_debug_info = (strcmp (sect->name, debug_info_name) == 0
4300 1.3 christos || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO));
4301 1.1 christos
4302 1.3 christos if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
4303 1.3 christos && !is_debug_info)
4304 1.3 christos continue;
4305 1.1 christos
4306 1.3 christos sz = sect->rawsize ? sect->rawsize : sect->size;
4307 1.1 christos
4308 1.3 christos if (is_debug_info)
4309 1.3 christos {
4310 1.3 christos BFD_ASSERT (sect->alignment_power == 0);
4311 1.3 christos sect->vma = last_dwarf;
4312 1.3 christos last_dwarf += sz;
4313 1.3 christos }
4314 1.3 christos else
4315 1.3 christos {
4316 1.3 christos /* Align the new address to the current section
4317 1.3 christos alignment. */
4318 1.3 christos last_vma = ((last_vma
4319 1.6 christos + ~(-((bfd_vma) 1 << sect->alignment_power)))
4320 1.6 christos & (-((bfd_vma) 1 << sect->alignment_power)));
4321 1.3 christos sect->vma = last_vma;
4322 1.3 christos last_vma += sz;
4323 1.3 christos }
4324 1.1 christos
4325 1.3 christos p->section = sect;
4326 1.3 christos p->adj_vma = sect->vma;
4327 1.3 christos p++;
4328 1.3 christos }
4329 1.9 christos if (abfd == stash->f.bfd_ptr)
4330 1.3 christos break;
4331 1.9 christos abfd = stash->f.bfd_ptr;
4332 1.1 christos }
4333 1.1 christos }
4334 1.1 christos
4335 1.9 christos if (orig_bfd != stash->f.bfd_ptr)
4336 1.9 christos set_debug_vma (orig_bfd, stash->f.bfd_ptr);
4337 1.3 christos
4338 1.1 christos return TRUE;
4339 1.1 christos }
4340 1.1 christos
4341 1.1 christos /* Look up a funcinfo by name using the given info hash table. If found,
4342 1.1 christos also update the locations pointed to by filename_ptr and linenumber_ptr.
4343 1.1 christos
4344 1.1 christos This function returns TRUE if a funcinfo that matches the given symbol
4345 1.1 christos and address is found with any error; otherwise it returns FALSE. */
4346 1.1 christos
4347 1.1 christos static bfd_boolean
4348 1.1 christos info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
4349 1.1 christos asymbol *sym,
4350 1.1 christos bfd_vma addr,
4351 1.1 christos const char **filename_ptr,
4352 1.1 christos unsigned int *linenumber_ptr)
4353 1.1 christos {
4354 1.1 christos struct funcinfo* each_func;
4355 1.1 christos struct funcinfo* best_fit = NULL;
4356 1.3 christos bfd_vma best_fit_len = 0;
4357 1.1 christos struct info_list_node *node;
4358 1.1 christos struct arange *arange;
4359 1.1 christos const char *name = bfd_asymbol_name (sym);
4360 1.9 christos asection *sec = bfd_asymbol_section (sym);
4361 1.1 christos
4362 1.1 christos for (node = lookup_info_hash_table (hash_table, name);
4363 1.1 christos node;
4364 1.1 christos node = node->next)
4365 1.1 christos {
4366 1.1 christos each_func = (struct funcinfo *) node->info;
4367 1.1 christos for (arange = &each_func->arange;
4368 1.1 christos arange;
4369 1.1 christos arange = arange->next)
4370 1.1 christos {
4371 1.1 christos if ((!each_func->sec || each_func->sec == sec)
4372 1.1 christos && addr >= arange->low
4373 1.1 christos && addr < arange->high
4374 1.1 christos && (!best_fit
4375 1.3 christos || arange->high - arange->low < best_fit_len))
4376 1.3 christos {
4377 1.3 christos best_fit = each_func;
4378 1.3 christos best_fit_len = arange->high - arange->low;
4379 1.3 christos }
4380 1.1 christos }
4381 1.1 christos }
4382 1.1 christos
4383 1.1 christos if (best_fit)
4384 1.1 christos {
4385 1.1 christos best_fit->sec = sec;
4386 1.1 christos *filename_ptr = best_fit->file;
4387 1.1 christos *linenumber_ptr = best_fit->line;
4388 1.1 christos return TRUE;
4389 1.1 christos }
4390 1.1 christos
4391 1.1 christos return FALSE;
4392 1.1 christos }
4393 1.1 christos
4394 1.1 christos /* Look up a varinfo by name using the given info hash table. If found,
4395 1.1 christos also update the locations pointed to by filename_ptr and linenumber_ptr.
4396 1.1 christos
4397 1.1 christos This function returns TRUE if a varinfo that matches the given symbol
4398 1.1 christos and address is found with any error; otherwise it returns FALSE. */
4399 1.1 christos
4400 1.1 christos static bfd_boolean
4401 1.1 christos info_hash_lookup_varinfo (struct info_hash_table *hash_table,
4402 1.1 christos asymbol *sym,
4403 1.1 christos bfd_vma addr,
4404 1.1 christos const char **filename_ptr,
4405 1.1 christos unsigned int *linenumber_ptr)
4406 1.1 christos {
4407 1.1 christos const char *name = bfd_asymbol_name (sym);
4408 1.9 christos asection *sec = bfd_asymbol_section (sym);
4409 1.1 christos struct varinfo* each;
4410 1.1 christos struct info_list_node *node;
4411 1.1 christos
4412 1.1 christos for (node = lookup_info_hash_table (hash_table, name);
4413 1.1 christos node;
4414 1.1 christos node = node->next)
4415 1.1 christos {
4416 1.1 christos each = (struct varinfo *) node->info;
4417 1.1 christos if (each->addr == addr
4418 1.1 christos && (!each->sec || each->sec == sec))
4419 1.1 christos {
4420 1.1 christos each->sec = sec;
4421 1.1 christos *filename_ptr = each->file;
4422 1.1 christos *linenumber_ptr = each->line;
4423 1.1 christos return TRUE;
4424 1.1 christos }
4425 1.1 christos }
4426 1.1 christos
4427 1.1 christos return FALSE;
4428 1.1 christos }
4429 1.1 christos
4430 1.1 christos /* Update the funcinfo and varinfo info hash tables if they are
4431 1.1 christos not up to date. Returns TRUE if there is no error; otherwise
4432 1.1 christos returns FALSE and disable the info hash tables. */
4433 1.1 christos
4434 1.1 christos static bfd_boolean
4435 1.1 christos stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
4436 1.1 christos {
4437 1.1 christos struct comp_unit *each;
4438 1.1 christos
4439 1.1 christos /* Exit if hash tables are up-to-date. */
4440 1.9 christos if (stash->f.all_comp_units == stash->hash_units_head)
4441 1.1 christos return TRUE;
4442 1.1 christos
4443 1.1 christos if (stash->hash_units_head)
4444 1.1 christos each = stash->hash_units_head->prev_unit;
4445 1.1 christos else
4446 1.9 christos each = stash->f.last_comp_unit;
4447 1.1 christos
4448 1.1 christos while (each)
4449 1.1 christos {
4450 1.1 christos if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
4451 1.1 christos stash->varinfo_hash_table))
4452 1.1 christos {
4453 1.1 christos stash->info_hash_status = STASH_INFO_HASH_DISABLED;
4454 1.1 christos return FALSE;
4455 1.1 christos }
4456 1.1 christos each = each->prev_unit;
4457 1.1 christos }
4458 1.1 christos
4459 1.9 christos stash->hash_units_head = stash->f.all_comp_units;
4460 1.1 christos return TRUE;
4461 1.1 christos }
4462 1.1 christos
4463 1.7 christos /* Check consistency of info hash tables. This is for debugging only. */
4464 1.1 christos
4465 1.1 christos static void ATTRIBUTE_UNUSED
4466 1.1 christos stash_verify_info_hash_table (struct dwarf2_debug *stash)
4467 1.1 christos {
4468 1.1 christos struct comp_unit *each_unit;
4469 1.1 christos struct funcinfo *each_func;
4470 1.1 christos struct varinfo *each_var;
4471 1.1 christos struct info_list_node *node;
4472 1.1 christos bfd_boolean found;
4473 1.1 christos
4474 1.9 christos for (each_unit = stash->f.all_comp_units;
4475 1.1 christos each_unit;
4476 1.1 christos each_unit = each_unit->next_unit)
4477 1.1 christos {
4478 1.1 christos for (each_func = each_unit->function_table;
4479 1.1 christos each_func;
4480 1.1 christos each_func = each_func->prev_func)
4481 1.1 christos {
4482 1.1 christos if (!each_func->name)
4483 1.1 christos continue;
4484 1.1 christos node = lookup_info_hash_table (stash->funcinfo_hash_table,
4485 1.1 christos each_func->name);
4486 1.1 christos BFD_ASSERT (node);
4487 1.1 christos found = FALSE;
4488 1.1 christos while (node && !found)
4489 1.1 christos {
4490 1.1 christos found = node->info == each_func;
4491 1.1 christos node = node->next;
4492 1.1 christos }
4493 1.1 christos BFD_ASSERT (found);
4494 1.1 christos }
4495 1.1 christos
4496 1.1 christos for (each_var = each_unit->variable_table;
4497 1.1 christos each_var;
4498 1.1 christos each_var = each_var->prev_var)
4499 1.1 christos {
4500 1.1 christos if (!each_var->name || !each_var->file || each_var->stack)
4501 1.1 christos continue;
4502 1.1 christos node = lookup_info_hash_table (stash->varinfo_hash_table,
4503 1.1 christos each_var->name);
4504 1.1 christos BFD_ASSERT (node);
4505 1.1 christos found = FALSE;
4506 1.1 christos while (node && !found)
4507 1.1 christos {
4508 1.1 christos found = node->info == each_var;
4509 1.1 christos node = node->next;
4510 1.1 christos }
4511 1.1 christos BFD_ASSERT (found);
4512 1.1 christos }
4513 1.1 christos }
4514 1.1 christos }
4515 1.1 christos
4516 1.1 christos /* Check to see if we want to enable the info hash tables, which consume
4517 1.1 christos quite a bit of memory. Currently we only check the number times
4518 1.1 christos bfd_dwarf2_find_line is called. In the future, we may also want to
4519 1.1 christos take the number of symbols into account. */
4520 1.1 christos
4521 1.1 christos static void
4522 1.1 christos stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
4523 1.1 christos {
4524 1.1 christos BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
4525 1.1 christos
4526 1.1 christos if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
4527 1.1 christos return;
4528 1.1 christos
4529 1.1 christos /* FIXME: Maybe we should check the reduce_memory_overheads
4530 1.1 christos and optimize fields in the bfd_link_info structure ? */
4531 1.1 christos
4532 1.1 christos /* Create hash tables. */
4533 1.1 christos stash->funcinfo_hash_table = create_info_hash_table (abfd);
4534 1.1 christos stash->varinfo_hash_table = create_info_hash_table (abfd);
4535 1.1 christos if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
4536 1.1 christos {
4537 1.1 christos /* Turn off info hashes if any allocation above fails. */
4538 1.1 christos stash->info_hash_status = STASH_INFO_HASH_DISABLED;
4539 1.1 christos return;
4540 1.1 christos }
4541 1.1 christos /* We need a forced update so that the info hash tables will
4542 1.1 christos be created even though there is no compilation unit. That
4543 1.1 christos happens if STASH_INFO_HASH_TRIGGER is 0. */
4544 1.9 christos if (stash_maybe_update_info_hash_tables (stash))
4545 1.9 christos stash->info_hash_status = STASH_INFO_HASH_ON;
4546 1.1 christos }
4547 1.1 christos
4548 1.1 christos /* Find the file and line associated with a symbol and address using the
4549 1.1 christos info hash tables of a stash. If there is a match, the function returns
4550 1.1 christos TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
4551 1.1 christos otherwise it returns FALSE. */
4552 1.1 christos
4553 1.1 christos static bfd_boolean
4554 1.1 christos stash_find_line_fast (struct dwarf2_debug *stash,
4555 1.1 christos asymbol *sym,
4556 1.1 christos bfd_vma addr,
4557 1.1 christos const char **filename_ptr,
4558 1.1 christos unsigned int *linenumber_ptr)
4559 1.1 christos {
4560 1.1 christos BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
4561 1.1 christos
4562 1.1 christos if (sym->flags & BSF_FUNCTION)
4563 1.1 christos return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
4564 1.1 christos filename_ptr, linenumber_ptr);
4565 1.1 christos return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
4566 1.1 christos filename_ptr, linenumber_ptr);
4567 1.1 christos }
4568 1.1 christos
4569 1.3 christos /* Save current section VMAs. */
4570 1.3 christos
4571 1.3 christos static bfd_boolean
4572 1.3 christos save_section_vma (const bfd *abfd, struct dwarf2_debug *stash)
4573 1.3 christos {
4574 1.3 christos asection *s;
4575 1.3 christos unsigned int i;
4576 1.3 christos
4577 1.3 christos if (abfd->section_count == 0)
4578 1.3 christos return TRUE;
4579 1.3 christos stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count);
4580 1.3 christos if (stash->sec_vma == NULL)
4581 1.3 christos return FALSE;
4582 1.9 christos stash->sec_vma_count = abfd->section_count;
4583 1.9 christos for (i = 0, s = abfd->sections;
4584 1.9 christos s != NULL && i < abfd->section_count;
4585 1.9 christos i++, s = s->next)
4586 1.3 christos {
4587 1.3 christos if (s->output_section != NULL)
4588 1.3 christos stash->sec_vma[i] = s->output_section->vma + s->output_offset;
4589 1.3 christos else
4590 1.3 christos stash->sec_vma[i] = s->vma;
4591 1.3 christos }
4592 1.3 christos return TRUE;
4593 1.3 christos }
4594 1.3 christos
4595 1.3 christos /* Compare current section VMAs against those at the time the stash
4596 1.3 christos was created. If find_nearest_line is used in linker warnings or
4597 1.3 christos errors early in the link process, the debug info stash will be
4598 1.3 christos invalid for later calls. This is because we relocate debug info
4599 1.3 christos sections, so the stashed section contents depend on symbol values,
4600 1.3 christos which in turn depend on section VMAs. */
4601 1.3 christos
4602 1.3 christos static bfd_boolean
4603 1.3 christos section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash)
4604 1.3 christos {
4605 1.3 christos asection *s;
4606 1.3 christos unsigned int i;
4607 1.3 christos
4608 1.9 christos /* PR 24334: If the number of sections in ABFD has changed between
4609 1.9 christos when the stash was created and now, then we cannot trust the
4610 1.9 christos stashed vma information. */
4611 1.9 christos if (abfd->section_count != stash->sec_vma_count)
4612 1.9 christos return FALSE;
4613 1.9 christos
4614 1.9 christos for (i = 0, s = abfd->sections;
4615 1.9 christos s != NULL && i < abfd->section_count;
4616 1.9 christos i++, s = s->next)
4617 1.3 christos {
4618 1.3 christos bfd_vma vma;
4619 1.3 christos
4620 1.3 christos if (s->output_section != NULL)
4621 1.3 christos vma = s->output_section->vma + s->output_offset;
4622 1.3 christos else
4623 1.3 christos vma = s->vma;
4624 1.3 christos if (vma != stash->sec_vma[i])
4625 1.3 christos return FALSE;
4626 1.3 christos }
4627 1.3 christos return TRUE;
4628 1.3 christos }
4629 1.3 christos
4630 1.1 christos /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
4631 1.1 christos If DEBUG_BFD is not specified, we read debug information from ABFD
4632 1.1 christos or its gnu_debuglink. The results will be stored in PINFO.
4633 1.1 christos The function returns TRUE iff debug information is ready. */
4634 1.1 christos
4635 1.1 christos bfd_boolean
4636 1.1 christos _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
4637 1.3 christos const struct dwarf_debug_section *debug_sections,
4638 1.3 christos asymbol **symbols,
4639 1.3 christos void **pinfo,
4640 1.3 christos bfd_boolean do_place)
4641 1.1 christos {
4642 1.9 christos size_t amt = sizeof (struct dwarf2_debug);
4643 1.1 christos bfd_size_type total_size;
4644 1.1 christos asection *msec;
4645 1.1 christos struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
4646 1.1 christos
4647 1.1 christos if (stash != NULL)
4648 1.3 christos {
4649 1.7 christos if (stash->orig_bfd == abfd
4650 1.8 christos && section_vma_same (abfd, stash))
4651 1.8 christos {
4652 1.8 christos /* Check that we did previously find some debug information
4653 1.8 christos before attempting to make use of it. */
4654 1.9 christos if (stash->f.bfd_ptr != NULL)
4655 1.8 christos {
4656 1.8 christos if (do_place && !place_sections (abfd, stash))
4657 1.8 christos return FALSE;
4658 1.8 christos return TRUE;
4659 1.8 christos }
4660 1.7 christos
4661 1.8 christos return FALSE;
4662 1.8 christos }
4663 1.3 christos _bfd_dwarf2_cleanup_debug_info (abfd, pinfo);
4664 1.3 christos memset (stash, 0, amt);
4665 1.3 christos }
4666 1.3 christos else
4667 1.3 christos {
4668 1.3 christos stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
4669 1.3 christos if (! stash)
4670 1.3 christos return FALSE;
4671 1.3 christos }
4672 1.7 christos stash->orig_bfd = abfd;
4673 1.1 christos stash->debug_sections = debug_sections;
4674 1.9 christos stash->f.syms = symbols;
4675 1.3 christos if (!save_section_vma (abfd, stash))
4676 1.3 christos return FALSE;
4677 1.1 christos
4678 1.9 christos stash->f.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
4679 1.9 christos del_abbrev, calloc, free);
4680 1.9 christos if (!stash->f.abbrev_offsets)
4681 1.9 christos return FALSE;
4682 1.9 christos
4683 1.9 christos stash->alt.abbrev_offsets = htab_create_alloc (10, hash_abbrev, eq_abbrev,
4684 1.9 christos del_abbrev, calloc, free);
4685 1.9 christos if (!stash->alt.abbrev_offsets)
4686 1.9 christos return FALSE;
4687 1.9 christos
4688 1.1 christos *pinfo = stash;
4689 1.1 christos
4690 1.1 christos if (debug_bfd == NULL)
4691 1.1 christos debug_bfd = abfd;
4692 1.1 christos
4693 1.1 christos msec = find_debug_info (debug_bfd, debug_sections, NULL);
4694 1.1 christos if (msec == NULL && abfd == debug_bfd)
4695 1.1 christos {
4696 1.7 christos char * debug_filename;
4697 1.7 christos
4698 1.7 christos debug_filename = bfd_follow_build_id_debuglink (abfd, DEBUGDIR);
4699 1.7 christos if (debug_filename == NULL)
4700 1.7 christos debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
4701 1.1 christos
4702 1.1 christos if (debug_filename == NULL)
4703 1.1 christos /* No dwarf2 info, and no gnu_debuglink to follow.
4704 1.1 christos Note that at this point the stash has been allocated, but
4705 1.1 christos contains zeros. This lets future calls to this function
4706 1.1 christos fail more quickly. */
4707 1.1 christos return FALSE;
4708 1.1 christos
4709 1.9 christos debug_bfd = bfd_openr (debug_filename, NULL);
4710 1.9 christos free (debug_filename);
4711 1.9 christos if (debug_bfd == NULL)
4712 1.9 christos /* FIXME: Should we report our failure to follow the debuglink ? */
4713 1.9 christos return FALSE;
4714 1.9 christos
4715 1.6 christos /* Set BFD_DECOMPRESS to decompress debug sections. */
4716 1.9 christos debug_bfd->flags |= BFD_DECOMPRESS;
4717 1.9 christos if (!bfd_check_format (debug_bfd, bfd_object)
4718 1.1 christos || (msec = find_debug_info (debug_bfd,
4719 1.3 christos debug_sections, NULL)) == NULL
4720 1.3 christos || !bfd_generic_link_read_symbols (debug_bfd))
4721 1.1 christos {
4722 1.9 christos bfd_close (debug_bfd);
4723 1.1 christos return FALSE;
4724 1.1 christos }
4725 1.3 christos
4726 1.3 christos symbols = bfd_get_outsymbols (debug_bfd);
4727 1.9 christos stash->f.syms = symbols;
4728 1.1 christos stash->close_on_cleanup = TRUE;
4729 1.1 christos }
4730 1.9 christos stash->f.bfd_ptr = debug_bfd;
4731 1.1 christos
4732 1.3 christos if (do_place
4733 1.3 christos && !place_sections (abfd, stash))
4734 1.3 christos return FALSE;
4735 1.3 christos
4736 1.1 christos /* There can be more than one DWARF2 info section in a BFD these
4737 1.1 christos days. First handle the easy case when there's only one. If
4738 1.1 christos there's more than one, try case two: none of the sections is
4739 1.1 christos compressed. In that case, read them all in and produce one
4740 1.1 christos large stash. We do this in two passes - in the first pass we
4741 1.1 christos just accumulate the section sizes, and in the second pass we
4742 1.1 christos read in the section's contents. (The allows us to avoid
4743 1.1 christos reallocing the data as we add sections to the stash.) If
4744 1.1 christos some or all sections are compressed, then do things the slow
4745 1.1 christos way, with a bunch of reallocs. */
4746 1.1 christos
4747 1.1 christos if (! find_debug_info (debug_bfd, debug_sections, msec))
4748 1.1 christos {
4749 1.1 christos /* Case 1: only one info section. */
4750 1.1 christos total_size = msec->size;
4751 1.1 christos if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
4752 1.1 christos symbols, 0,
4753 1.9 christos &stash->f.dwarf_info_buffer, &total_size))
4754 1.1 christos return FALSE;
4755 1.1 christos }
4756 1.1 christos else
4757 1.1 christos {
4758 1.1 christos /* Case 2: multiple sections. */
4759 1.1 christos for (total_size = 0;
4760 1.1 christos msec;
4761 1.1 christos msec = find_debug_info (debug_bfd, debug_sections, msec))
4762 1.9 christos {
4763 1.9 christos /* Catch PR25070 testcase overflowing size calculation here. */
4764 1.9 christos if (total_size + msec->size < total_size
4765 1.9 christos || total_size + msec->size < msec->size)
4766 1.9 christos {
4767 1.9 christos bfd_set_error (bfd_error_no_memory);
4768 1.9 christos return FALSE;
4769 1.9 christos }
4770 1.9 christos total_size += msec->size;
4771 1.9 christos }
4772 1.1 christos
4773 1.9 christos stash->f.dwarf_info_buffer = (bfd_byte *) bfd_malloc (total_size);
4774 1.9 christos if (stash->f.dwarf_info_buffer == NULL)
4775 1.1 christos return FALSE;
4776 1.1 christos
4777 1.1 christos total_size = 0;
4778 1.1 christos for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
4779 1.1 christos msec;
4780 1.1 christos msec = find_debug_info (debug_bfd, debug_sections, msec))
4781 1.1 christos {
4782 1.1 christos bfd_size_type size;
4783 1.1 christos
4784 1.1 christos size = msec->size;
4785 1.1 christos if (size == 0)
4786 1.1 christos continue;
4787 1.1 christos
4788 1.1 christos if (!(bfd_simple_get_relocated_section_contents
4789 1.9 christos (debug_bfd, msec, stash->f.dwarf_info_buffer + total_size,
4790 1.1 christos symbols)))
4791 1.1 christos return FALSE;
4792 1.1 christos
4793 1.1 christos total_size += size;
4794 1.1 christos }
4795 1.1 christos }
4796 1.1 christos
4797 1.9 christos stash->f.info_ptr = stash->f.dwarf_info_buffer;
4798 1.9 christos stash->f.dwarf_info_size = total_size;
4799 1.1 christos return TRUE;
4800 1.1 christos }
4801 1.1 christos
4802 1.9 christos /* Parse the next DWARF2 compilation unit at FILE->INFO_PTR. */
4803 1.9 christos
4804 1.9 christos static struct comp_unit *
4805 1.9 christos stash_comp_unit (struct dwarf2_debug *stash, struct dwarf2_debug_file *file)
4806 1.9 christos {
4807 1.9 christos bfd_size_type length;
4808 1.9 christos unsigned int offset_size;
4809 1.9 christos bfd_byte *info_ptr_unit = file->info_ptr;
4810 1.9 christos bfd_byte *info_ptr_end = file->dwarf_info_buffer + file->dwarf_info_size;
4811 1.9 christos
4812 1.9 christos if (file->info_ptr >= info_ptr_end)
4813 1.9 christos return NULL;
4814 1.9 christos
4815 1.9 christos length = read_4_bytes (file->bfd_ptr, file->info_ptr, info_ptr_end);
4816 1.9 christos /* A 0xffffff length is the DWARF3 way of indicating
4817 1.9 christos we use 64-bit offsets, instead of 32-bit offsets. */
4818 1.9 christos if (length == 0xffffffff)
4819 1.9 christos {
4820 1.9 christos offset_size = 8;
4821 1.9 christos length = read_8_bytes (file->bfd_ptr, file->info_ptr + 4,
4822 1.9 christos info_ptr_end);
4823 1.9 christos file->info_ptr += 12;
4824 1.9 christos }
4825 1.9 christos /* A zero length is the IRIX way of indicating 64-bit offsets,
4826 1.9 christos mostly because the 64-bit length will generally fit in 32
4827 1.9 christos bits, and the endianness helps. */
4828 1.9 christos else if (length == 0)
4829 1.9 christos {
4830 1.9 christos offset_size = 8;
4831 1.9 christos length = read_4_bytes (file->bfd_ptr, file->info_ptr + 4,
4832 1.9 christos info_ptr_end);
4833 1.9 christos file->info_ptr += 8;
4834 1.9 christos }
4835 1.9 christos /* In the absence of the hints above, we assume 32-bit DWARF2
4836 1.9 christos offsets even for targets with 64-bit addresses, because:
4837 1.9 christos a) most of the time these targets will not have generated
4838 1.9 christos more than 2Gb of debug info and so will not need 64-bit
4839 1.9 christos offsets,
4840 1.9 christos and
4841 1.9 christos b) if they do use 64-bit offsets but they are not using
4842 1.9 christos the size hints that are tested for above then they are
4843 1.9 christos not conforming to the DWARF3 standard anyway. */
4844 1.9 christos else
4845 1.9 christos {
4846 1.9 christos offset_size = 4;
4847 1.9 christos file->info_ptr += 4;
4848 1.9 christos }
4849 1.9 christos
4850 1.9 christos if (length != 0
4851 1.9 christos && file->info_ptr + length <= info_ptr_end
4852 1.9 christos && file->info_ptr + length > file->info_ptr)
4853 1.9 christos {
4854 1.9 christos struct comp_unit *each = parse_comp_unit (stash, file,
4855 1.9 christos file->info_ptr, length,
4856 1.9 christos info_ptr_unit, offset_size);
4857 1.9 christos if (each)
4858 1.9 christos {
4859 1.9 christos if (file->all_comp_units)
4860 1.9 christos file->all_comp_units->prev_unit = each;
4861 1.9 christos else
4862 1.9 christos file->last_comp_unit = each;
4863 1.9 christos
4864 1.9 christos each->next_unit = file->all_comp_units;
4865 1.9 christos file->all_comp_units = each;
4866 1.9 christos
4867 1.9 christos file->info_ptr += length;
4868 1.9 christos return each;
4869 1.9 christos }
4870 1.9 christos }
4871 1.9 christos
4872 1.9 christos /* Don't trust any of the DWARF info after a corrupted length or
4873 1.9 christos parse error. */
4874 1.9 christos file->info_ptr = info_ptr_end;
4875 1.9 christos return NULL;
4876 1.9 christos }
4877 1.9 christos
4878 1.9 christos /* Hash function for an asymbol. */
4879 1.9 christos
4880 1.9 christos static hashval_t
4881 1.9 christos hash_asymbol (const void *sym)
4882 1.9 christos {
4883 1.9 christos const asymbol *asym = sym;
4884 1.9 christos return htab_hash_string (asym->name);
4885 1.9 christos }
4886 1.9 christos
4887 1.9 christos /* Equality function for asymbols. */
4888 1.9 christos
4889 1.9 christos static int
4890 1.9 christos eq_asymbol (const void *a, const void *b)
4891 1.9 christos {
4892 1.9 christos const asymbol *sa = a;
4893 1.9 christos const asymbol *sb = b;
4894 1.9 christos return strcmp (sa->name, sb->name) == 0;
4895 1.9 christos }
4896 1.9 christos
4897 1.5 christos /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
4898 1.5 christos abbrev with a DW_AT_low_pc attached to it. Then lookup that same
4899 1.5 christos symbol in SYMBOLS and return the difference between the low_pc and
4900 1.5 christos the symbol's address. Returns 0 if no suitable symbol could be found. */
4901 1.5 christos
4902 1.5 christos bfd_signed_vma
4903 1.5 christos _bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo)
4904 1.5 christos {
4905 1.5 christos struct dwarf2_debug *stash;
4906 1.5 christos struct comp_unit * unit;
4907 1.9 christos htab_t sym_hash;
4908 1.9 christos bfd_signed_vma result = 0;
4909 1.9 christos asymbol ** psym;
4910 1.5 christos
4911 1.5 christos stash = (struct dwarf2_debug *) *pinfo;
4912 1.5 christos
4913 1.9 christos if (stash == NULL || symbols == NULL)
4914 1.5 christos return 0;
4915 1.5 christos
4916 1.9 christos sym_hash = htab_create_alloc (10, hash_asymbol, eq_asymbol,
4917 1.9 christos NULL, xcalloc, free);
4918 1.9 christos for (psym = symbols; * psym != NULL; psym++)
4919 1.5 christos {
4920 1.9 christos asymbol * sym = * psym;
4921 1.5 christos
4922 1.9 christos if (sym->flags & BSF_FUNCTION && sym->section != NULL)
4923 1.5 christos {
4924 1.9 christos void **slot = htab_find_slot (sym_hash, sym, INSERT);
4925 1.9 christos *slot = sym;
4926 1.5 christos }
4927 1.9 christos }
4928 1.9 christos
4929 1.9 christos for (unit = stash->f.all_comp_units; unit; unit = unit->next_unit)
4930 1.9 christos {
4931 1.9 christos struct funcinfo * func;
4932 1.9 christos
4933 1.9 christos comp_unit_maybe_decode_line_info (unit);
4934 1.5 christos
4935 1.5 christos for (func = unit->function_table; func != NULL; func = func->prev_func)
4936 1.5 christos if (func->name && func->arange.low)
4937 1.5 christos {
4938 1.9 christos asymbol search, *sym;
4939 1.5 christos
4940 1.5 christos /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
4941 1.5 christos
4942 1.9 christos search.name = func->name;
4943 1.9 christos sym = htab_find (sym_hash, &search);
4944 1.9 christos if (sym != NULL)
4945 1.5 christos {
4946 1.9 christos result = ((bfd_signed_vma) func->arange.low) -
4947 1.9 christos ((bfd_signed_vma) (sym->value + sym->section->vma));
4948 1.9 christos goto done;
4949 1.5 christos }
4950 1.5 christos }
4951 1.5 christos }
4952 1.5 christos
4953 1.9 christos done:
4954 1.9 christos htab_delete (sym_hash);
4955 1.9 christos return result;
4956 1.5 christos }
4957 1.5 christos
4958 1.1 christos /* Find the source code location of SYMBOL. If SYMBOL is NULL
4959 1.1 christos then find the nearest source code location corresponding to
4960 1.1 christos the address SECTION + OFFSET.
4961 1.9 christos Returns 1 if the line is found without error and fills in
4962 1.1 christos FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
4963 1.1 christos NULL the FUNCTIONNAME_PTR is also filled in.
4964 1.9 christos Returns 2 if partial information from _bfd_elf_find_function is
4965 1.9 christos returned (function and maybe file) by looking at symbols. DWARF2
4966 1.9 christos info is present but not regarding the requested code location.
4967 1.9 christos Returns 0 otherwise.
4968 1.1 christos SYMBOLS contains the symbol table for ABFD.
4969 1.9 christos DEBUG_SECTIONS contains the name of the dwarf debug sections. */
4970 1.1 christos
4971 1.9 christos int
4972 1.3 christos _bfd_dwarf2_find_nearest_line (bfd *abfd,
4973 1.3 christos asymbol **symbols,
4974 1.3 christos asymbol *symbol,
4975 1.3 christos asection *section,
4976 1.3 christos bfd_vma offset,
4977 1.3 christos const char **filename_ptr,
4978 1.3 christos const char **functionname_ptr,
4979 1.3 christos unsigned int *linenumber_ptr,
4980 1.3 christos unsigned int *discriminator_ptr,
4981 1.3 christos const struct dwarf_debug_section *debug_sections,
4982 1.3 christos void **pinfo)
4983 1.1 christos {
4984 1.1 christos /* Read each compilation unit from the section .debug_info, and check
4985 1.1 christos to see if it contains the address we are searching for. If yes,
4986 1.1 christos lookup the address, and return the line number info. If no, go
4987 1.1 christos on to the next compilation unit.
4988 1.1 christos
4989 1.1 christos We keep a list of all the previously read compilation units, and
4990 1.1 christos a pointer to the next un-read compilation unit. Check the
4991 1.1 christos previously read units before reading more. */
4992 1.1 christos struct dwarf2_debug *stash;
4993 1.1 christos /* What address are we looking for? */
4994 1.1 christos bfd_vma addr;
4995 1.1 christos struct comp_unit* each;
4996 1.3 christos struct funcinfo *function = NULL;
4997 1.9 christos int found = FALSE;
4998 1.1 christos bfd_boolean do_line;
4999 1.1 christos
5000 1.1 christos *filename_ptr = NULL;
5001 1.1 christos if (functionname_ptr != NULL)
5002 1.1 christos *functionname_ptr = NULL;
5003 1.1 christos *linenumber_ptr = 0;
5004 1.1 christos if (discriminator_ptr)
5005 1.1 christos *discriminator_ptr = 0;
5006 1.1 christos
5007 1.3 christos if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL, debug_sections,
5008 1.3 christos symbols, pinfo,
5009 1.3 christos (abfd->flags & (EXEC_P | DYNAMIC)) == 0))
5010 1.1 christos return FALSE;
5011 1.1 christos
5012 1.1 christos stash = (struct dwarf2_debug *) *pinfo;
5013 1.1 christos
5014 1.3 christos do_line = symbol != NULL;
5015 1.1 christos if (do_line)
5016 1.1 christos {
5017 1.3 christos BFD_ASSERT (section == NULL && offset == 0 && functionname_ptr == NULL);
5018 1.9 christos section = bfd_asymbol_section (symbol);
5019 1.1 christos addr = symbol->value;
5020 1.1 christos }
5021 1.1 christos else
5022 1.3 christos {
5023 1.3 christos BFD_ASSERT (section != NULL && functionname_ptr != NULL);
5024 1.3 christos addr = offset;
5025 1.7 christos
5026 1.7 christos /* If we have no SYMBOL but the section we're looking at is not a
5027 1.8 christos code section, then take a look through the list of symbols to see
5028 1.8 christos if we have a symbol at the address we're looking for. If we do
5029 1.8 christos then use this to look up line information. This will allow us to
5030 1.8 christos give file and line results for data symbols. We exclude code
5031 1.8 christos symbols here, if we look up a function symbol and then look up the
5032 1.8 christos line information we'll actually return the line number for the
5033 1.8 christos opening '{' rather than the function definition line. This is
5034 1.8 christos because looking up by symbol uses the line table, in which the
5035 1.8 christos first line for a function is usually the opening '{', while
5036 1.8 christos looking up the function by section + offset uses the
5037 1.8 christos DW_AT_decl_line from the function DW_TAG_subprogram for the line,
5038 1.8 christos which will be the line of the function name. */
5039 1.8 christos if (symbols != NULL && (section->flags & SEC_CODE) == 0)
5040 1.7 christos {
5041 1.7 christos asymbol **tmp;
5042 1.7 christos
5043 1.7 christos for (tmp = symbols; (*tmp) != NULL; ++tmp)
5044 1.7 christos if ((*tmp)->the_bfd == abfd
5045 1.7 christos && (*tmp)->section == section
5046 1.7 christos && (*tmp)->value == offset
5047 1.7 christos && ((*tmp)->flags & BSF_SECTION_SYM) == 0)
5048 1.7 christos {
5049 1.7 christos symbol = *tmp;
5050 1.7 christos do_line = TRUE;
5051 1.8 christos /* For local symbols, keep going in the hope we find a
5052 1.8 christos global. */
5053 1.8 christos if ((symbol->flags & BSF_GLOBAL) != 0)
5054 1.8 christos break;
5055 1.7 christos }
5056 1.7 christos }
5057 1.3 christos }
5058 1.1 christos
5059 1.1 christos if (section->output_section)
5060 1.1 christos addr += section->output_section->vma + section->output_offset;
5061 1.1 christos else
5062 1.1 christos addr += section->vma;
5063 1.1 christos
5064 1.1 christos /* A null info_ptr indicates that there is no dwarf2 info
5065 1.1 christos (or that an error occured while setting up the stash). */
5066 1.9 christos if (! stash->f.info_ptr)
5067 1.1 christos return FALSE;
5068 1.1 christos
5069 1.1 christos stash->inliner_chain = NULL;
5070 1.1 christos
5071 1.1 christos /* Check the previously read comp. units first. */
5072 1.1 christos if (do_line)
5073 1.1 christos {
5074 1.1 christos /* The info hash tables use quite a bit of memory. We may not want to
5075 1.1 christos always use them. We use some heuristics to decide if and when to
5076 1.1 christos turn it on. */
5077 1.1 christos if (stash->info_hash_status == STASH_INFO_HASH_OFF)
5078 1.1 christos stash_maybe_enable_info_hash_tables (abfd, stash);
5079 1.1 christos
5080 1.1 christos /* Keep info hash table up to date if they are available. Note that we
5081 1.7 christos may disable the hash tables if there is any error duing update. */
5082 1.1 christos if (stash->info_hash_status == STASH_INFO_HASH_ON)
5083 1.1 christos stash_maybe_update_info_hash_tables (stash);
5084 1.1 christos
5085 1.1 christos if (stash->info_hash_status == STASH_INFO_HASH_ON)
5086 1.1 christos {
5087 1.1 christos found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
5088 1.1 christos linenumber_ptr);
5089 1.1 christos if (found)
5090 1.1 christos goto done;
5091 1.1 christos }
5092 1.1 christos else
5093 1.1 christos {
5094 1.1 christos /* Check the previously read comp. units first. */
5095 1.9 christos for (each = stash->f.all_comp_units; each; each = each->next_unit)
5096 1.1 christos if ((symbol->flags & BSF_FUNCTION) == 0
5097 1.1 christos || each->arange.high == 0
5098 1.1 christos || comp_unit_contains_address (each, addr))
5099 1.1 christos {
5100 1.1 christos found = comp_unit_find_line (each, symbol, addr, filename_ptr,
5101 1.9 christos linenumber_ptr);
5102 1.1 christos if (found)
5103 1.1 christos goto done;
5104 1.1 christos }
5105 1.1 christos }
5106 1.1 christos }
5107 1.1 christos else
5108 1.1 christos {
5109 1.1 christos bfd_vma min_range = (bfd_vma) -1;
5110 1.1 christos const char * local_filename = NULL;
5111 1.3 christos struct funcinfo *local_function = NULL;
5112 1.1 christos unsigned int local_linenumber = 0;
5113 1.1 christos unsigned int local_discriminator = 0;
5114 1.1 christos
5115 1.9 christos for (each = stash->f.all_comp_units; each; each = each->next_unit)
5116 1.1 christos {
5117 1.1 christos bfd_vma range = (bfd_vma) -1;
5118 1.1 christos
5119 1.1 christos found = ((each->arange.high == 0
5120 1.1 christos || comp_unit_contains_address (each, addr))
5121 1.9 christos && (range = (comp_unit_find_nearest_line
5122 1.9 christos (each, addr, &local_filename,
5123 1.9 christos &local_function, &local_linenumber,
5124 1.9 christos &local_discriminator))) != 0);
5125 1.1 christos if (found)
5126 1.1 christos {
5127 1.1 christos /* PRs 15935 15994: Bogus debug information may have provided us
5128 1.1 christos with an erroneous match. We attempt to counter this by
5129 1.1 christos selecting the match that has the smallest address range
5130 1.1 christos associated with it. (We are assuming that corrupt debug info
5131 1.1 christos will tend to result in extra large address ranges rather than
5132 1.1 christos extra small ranges).
5133 1.1 christos
5134 1.1 christos This does mean that we scan through all of the CUs associated
5135 1.1 christos with the bfd each time this function is called. But this does
5136 1.1 christos have the benefit of producing consistent results every time the
5137 1.1 christos function is called. */
5138 1.1 christos if (range <= min_range)
5139 1.1 christos {
5140 1.1 christos if (filename_ptr && local_filename)
5141 1.1 christos * filename_ptr = local_filename;
5142 1.3 christos if (local_function)
5143 1.3 christos function = local_function;
5144 1.1 christos if (discriminator_ptr && local_discriminator)
5145 1.1 christos * discriminator_ptr = local_discriminator;
5146 1.1 christos if (local_linenumber)
5147 1.1 christos * linenumber_ptr = local_linenumber;
5148 1.1 christos min_range = range;
5149 1.1 christos }
5150 1.1 christos }
5151 1.1 christos }
5152 1.1 christos
5153 1.1 christos if (* linenumber_ptr)
5154 1.1 christos {
5155 1.1 christos found = TRUE;
5156 1.1 christos goto done;
5157 1.1 christos }
5158 1.1 christos }
5159 1.1 christos
5160 1.1 christos /* Read each remaining comp. units checking each as they are read. */
5161 1.9 christos while ((each = stash_comp_unit (stash, &stash->f)) != NULL)
5162 1.1 christos {
5163 1.9 christos /* DW_AT_low_pc and DW_AT_high_pc are optional for
5164 1.9 christos compilation units. If we don't have them (i.e.,
5165 1.9 christos unit->high == 0), we need to consult the line info table
5166 1.9 christos to see if a compilation unit contains the given
5167 1.9 christos address. */
5168 1.9 christos if (do_line)
5169 1.9 christos found = (((symbol->flags & BSF_FUNCTION) == 0
5170 1.9 christos || each->arange.high == 0
5171 1.9 christos || comp_unit_contains_address (each, addr))
5172 1.9 christos && comp_unit_find_line (each, symbol, addr,
5173 1.9 christos filename_ptr, linenumber_ptr));
5174 1.1 christos else
5175 1.9 christos found = ((each->arange.high == 0
5176 1.9 christos || comp_unit_contains_address (each, addr))
5177 1.9 christos && comp_unit_find_nearest_line (each, addr,
5178 1.9 christos filename_ptr,
5179 1.9 christos &function,
5180 1.9 christos linenumber_ptr,
5181 1.9 christos discriminator_ptr) != 0);
5182 1.1 christos
5183 1.9 christos if (found)
5184 1.9 christos break;
5185 1.9 christos }
5186 1.5 christos
5187 1.9 christos done:
5188 1.9 christos if (functionname_ptr && function && function->is_linkage)
5189 1.9 christos *functionname_ptr = function->name;
5190 1.9 christos else if (functionname_ptr
5191 1.9 christos && (!*functionname_ptr
5192 1.9 christos || (function && !function->is_linkage)))
5193 1.9 christos {
5194 1.9 christos asymbol *fun;
5195 1.9 christos asymbol **syms = symbols;
5196 1.9 christos asection *sec = section;
5197 1.9 christos
5198 1.9 christos _bfd_dwarf2_stash_syms (stash, abfd, &sec, &syms);
5199 1.9 christos fun = _bfd_elf_find_function (abfd, syms, sec, offset,
5200 1.9 christos *filename_ptr ? NULL : filename_ptr,
5201 1.9 christos functionname_ptr);
5202 1.7 christos
5203 1.9 christos if (!found && fun != NULL)
5204 1.9 christos found = 2;
5205 1.1 christos
5206 1.9 christos if (function && !function->is_linkage)
5207 1.3 christos {
5208 1.6 christos bfd_vma sec_vma;
5209 1.6 christos
5210 1.6 christos sec_vma = section->vma;
5211 1.6 christos if (section->output_section != NULL)
5212 1.6 christos sec_vma = section->output_section->vma + section->output_offset;
5213 1.6 christos if (fun != NULL
5214 1.6 christos && fun->value + sec_vma == function->arange.low)
5215 1.6 christos function->name = *functionname_ptr;
5216 1.6 christos /* Even if we didn't find a linkage name, say that we have
5217 1.6 christos to stop a repeated search of symbols. */
5218 1.3 christos function->is_linkage = TRUE;
5219 1.3 christos }
5220 1.3 christos }
5221 1.9 christos
5222 1.1 christos if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
5223 1.1 christos unset_sections (stash);
5224 1.1 christos
5225 1.1 christos return found;
5226 1.1 christos }
5227 1.1 christos
5228 1.1 christos bfd_boolean
5229 1.1 christos _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
5230 1.1 christos const char **filename_ptr,
5231 1.1 christos const char **functionname_ptr,
5232 1.1 christos unsigned int *linenumber_ptr,
5233 1.1 christos void **pinfo)
5234 1.1 christos {
5235 1.1 christos struct dwarf2_debug *stash;
5236 1.1 christos
5237 1.1 christos stash = (struct dwarf2_debug *) *pinfo;
5238 1.1 christos if (stash)
5239 1.1 christos {
5240 1.1 christos struct funcinfo *func = stash->inliner_chain;
5241 1.1 christos
5242 1.1 christos if (func && func->caller_func)
5243 1.1 christos {
5244 1.1 christos *filename_ptr = func->caller_file;
5245 1.1 christos *functionname_ptr = func->caller_func->name;
5246 1.1 christos *linenumber_ptr = func->caller_line;
5247 1.1 christos stash->inliner_chain = func->caller_func;
5248 1.1 christos return TRUE;
5249 1.1 christos }
5250 1.1 christos }
5251 1.1 christos
5252 1.1 christos return FALSE;
5253 1.1 christos }
5254 1.1 christos
5255 1.1 christos void
5256 1.1 christos _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
5257 1.1 christos {
5258 1.1 christos struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
5259 1.1 christos struct comp_unit *each;
5260 1.9 christos struct dwarf2_debug_file *file;
5261 1.1 christos
5262 1.1 christos if (abfd == NULL || stash == NULL)
5263 1.1 christos return;
5264 1.1 christos
5265 1.9 christos if (stash->varinfo_hash_table)
5266 1.9 christos bfd_hash_table_free (&stash->varinfo_hash_table->base);
5267 1.9 christos if (stash->funcinfo_hash_table)
5268 1.9 christos bfd_hash_table_free (&stash->funcinfo_hash_table->base);
5269 1.9 christos
5270 1.9 christos file = &stash->f;
5271 1.9 christos while (1)
5272 1.1 christos {
5273 1.9 christos for (each = file->all_comp_units; each; each = each->next_unit)
5274 1.1 christos {
5275 1.9 christos struct funcinfo *function_table = each->function_table;
5276 1.9 christos struct varinfo *variable_table = each->variable_table;
5277 1.1 christos
5278 1.9 christos if (each->line_table && each->line_table != file->line_table)
5279 1.1 christos {
5280 1.9 christos free (each->line_table->files);
5281 1.9 christos free (each->line_table->dirs);
5282 1.1 christos }
5283 1.1 christos
5284 1.9 christos free (each->lookup_funcinfo_table);
5285 1.9 christos each->lookup_funcinfo_table = NULL;
5286 1.1 christos
5287 1.9 christos while (function_table)
5288 1.1 christos {
5289 1.1 christos free (function_table->file);
5290 1.1 christos function_table->file = NULL;
5291 1.1 christos free (function_table->caller_file);
5292 1.1 christos function_table->caller_file = NULL;
5293 1.9 christos function_table = function_table->prev_func;
5294 1.1 christos }
5295 1.1 christos
5296 1.9 christos while (variable_table)
5297 1.1 christos {
5298 1.1 christos free (variable_table->file);
5299 1.1 christos variable_table->file = NULL;
5300 1.9 christos variable_table = variable_table->prev_var;
5301 1.1 christos }
5302 1.9 christos }
5303 1.1 christos
5304 1.9 christos if (file->line_table)
5305 1.9 christos {
5306 1.9 christos free (file->line_table->files);
5307 1.9 christos free (file->line_table->dirs);
5308 1.1 christos }
5309 1.9 christos htab_delete (file->abbrev_offsets);
5310 1.9 christos
5311 1.9 christos free (file->dwarf_line_str_buffer);
5312 1.9 christos free (file->dwarf_str_buffer);
5313 1.9 christos free (file->dwarf_ranges_buffer);
5314 1.9 christos free (file->dwarf_line_buffer);
5315 1.9 christos free (file->dwarf_abbrev_buffer);
5316 1.9 christos free (file->dwarf_info_buffer);
5317 1.9 christos if (file == &stash->alt)
5318 1.9 christos break;
5319 1.9 christos file = &stash->alt;
5320 1.1 christos }
5321 1.9 christos free (stash->sec_vma);
5322 1.9 christos free (stash->adjusted_sections);
5323 1.1 christos if (stash->close_on_cleanup)
5324 1.9 christos bfd_close (stash->f.bfd_ptr);
5325 1.9 christos if (stash->alt.bfd_ptr)
5326 1.9 christos bfd_close (stash->alt.bfd_ptr);
5327 1.1 christos }
5328 1.3 christos
5329 1.3 christos /* Find the function to a particular section and offset,
5330 1.3 christos for error reporting. */
5331 1.3 christos
5332 1.6 christos asymbol *
5333 1.3 christos _bfd_elf_find_function (bfd *abfd,
5334 1.3 christos asymbol **symbols,
5335 1.3 christos asection *section,
5336 1.3 christos bfd_vma offset,
5337 1.3 christos const char **filename_ptr,
5338 1.3 christos const char **functionname_ptr)
5339 1.3 christos {
5340 1.3 christos struct elf_find_function_cache
5341 1.3 christos {
5342 1.3 christos asection *last_section;
5343 1.3 christos asymbol *func;
5344 1.3 christos const char *filename;
5345 1.3 christos bfd_size_type func_size;
5346 1.3 christos } *cache;
5347 1.3 christos
5348 1.3 christos if (symbols == NULL)
5349 1.6 christos return NULL;
5350 1.3 christos
5351 1.3 christos if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
5352 1.6 christos return NULL;
5353 1.3 christos
5354 1.3 christos cache = elf_tdata (abfd)->elf_find_function_cache;
5355 1.3 christos if (cache == NULL)
5356 1.3 christos {
5357 1.3 christos cache = bfd_zalloc (abfd, sizeof (*cache));
5358 1.3 christos elf_tdata (abfd)->elf_find_function_cache = cache;
5359 1.3 christos if (cache == NULL)
5360 1.6 christos return NULL;
5361 1.3 christos }
5362 1.3 christos if (cache->last_section != section
5363 1.3 christos || cache->func == NULL
5364 1.3 christos || offset < cache->func->value
5365 1.3 christos || offset >= cache->func->value + cache->func_size)
5366 1.3 christos {
5367 1.3 christos asymbol *file;
5368 1.3 christos bfd_vma low_func;
5369 1.3 christos asymbol **p;
5370 1.3 christos /* ??? Given multiple file symbols, it is impossible to reliably
5371 1.3 christos choose the right file name for global symbols. File symbols are
5372 1.3 christos local symbols, and thus all file symbols must sort before any
5373 1.3 christos global symbols. The ELF spec may be interpreted to say that a
5374 1.3 christos file symbol must sort before other local symbols, but currently
5375 1.3 christos ld -r doesn't do this. So, for ld -r output, it is possible to
5376 1.3 christos make a better choice of file name for local symbols by ignoring
5377 1.3 christos file symbols appearing after a given local symbol. */
5378 1.3 christos enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
5379 1.3 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5380 1.3 christos
5381 1.3 christos file = NULL;
5382 1.3 christos low_func = 0;
5383 1.3 christos state = nothing_seen;
5384 1.3 christos cache->filename = NULL;
5385 1.3 christos cache->func = NULL;
5386 1.3 christos cache->func_size = 0;
5387 1.3 christos cache->last_section = section;
5388 1.3 christos
5389 1.3 christos for (p = symbols; *p != NULL; p++)
5390 1.3 christos {
5391 1.3 christos asymbol *sym = *p;
5392 1.3 christos bfd_vma code_off;
5393 1.3 christos bfd_size_type size;
5394 1.3 christos
5395 1.3 christos if ((sym->flags & BSF_FILE) != 0)
5396 1.3 christos {
5397 1.3 christos file = sym;
5398 1.3 christos if (state == symbol_seen)
5399 1.3 christos state = file_after_symbol_seen;
5400 1.3 christos continue;
5401 1.3 christos }
5402 1.3 christos
5403 1.3 christos size = bed->maybe_function_sym (sym, section, &code_off);
5404 1.3 christos if (size != 0
5405 1.3 christos && code_off <= offset
5406 1.3 christos && (code_off > low_func
5407 1.3 christos || (code_off == low_func
5408 1.3 christos && size > cache->func_size)))
5409 1.3 christos {
5410 1.3 christos cache->func = sym;
5411 1.3 christos cache->func_size = size;
5412 1.3 christos cache->filename = NULL;
5413 1.3 christos low_func = code_off;
5414 1.3 christos if (file != NULL
5415 1.3 christos && ((sym->flags & BSF_LOCAL) != 0
5416 1.3 christos || state != file_after_symbol_seen))
5417 1.3 christos cache->filename = bfd_asymbol_name (file);
5418 1.3 christos }
5419 1.3 christos if (state == nothing_seen)
5420 1.3 christos state = symbol_seen;
5421 1.3 christos }
5422 1.3 christos }
5423 1.3 christos
5424 1.3 christos if (cache->func == NULL)
5425 1.6 christos return NULL;
5426 1.3 christos
5427 1.3 christos if (filename_ptr)
5428 1.3 christos *filename_ptr = cache->filename;
5429 1.3 christos if (functionname_ptr)
5430 1.3 christos *functionname_ptr = bfd_asymbol_name (cache->func);
5431 1.3 christos
5432 1.6 christos return cache->func;
5433 1.3 christos }
5434