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