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