Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* DWARF 2 support. |
f5a3e38a | 2 | Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 |
7898deda | 3 | Free Software Foundation, Inc. |
252b5132 RH |
4 | |
5 | Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions | |
6 | (gavin@cygnus.com). | |
7 | ||
8 | From the dwarf2read.c header: | |
9 | Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology, | |
10 | Inc. with support from Florida State University (under contract | |
11 | with the Ada Joint Program Office), and Silicon Graphics, Inc. | |
12 | Initial contribution by Brent Benson, Harris Computer Systems, Inc., | |
13 | based on Fred Fish's (Cygnus Support) implementation of DWARF 1 | |
14 | support in dwarfread.c | |
15 | ||
e2f6d277 | 16 | This file is part of BFD. |
252b5132 | 17 | |
e2f6d277 NC |
18 | This program is free software; you can redistribute it and/or modify |
19 | it under the terms of the GNU General Public License as published by | |
20 | the Free Software Foundation; either version 2 of the License, or (at | |
21 | your option) any later version. | |
252b5132 | 22 | |
e2f6d277 NC |
23 | This program is distributed in the hope that it will be useful, but |
24 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
25 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
26 | General Public License for more details. | |
252b5132 | 27 | |
e2f6d277 NC |
28 | You should have received a copy of the GNU General Public License |
29 | along with this program; if not, write to the Free Software | |
30 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
252b5132 RH |
31 | |
32 | #include "bfd.h" | |
33 | #include "sysdep.h" | |
34 | #include "libiberty.h" | |
35 | #include "libbfd.h" | |
36 | #include "elf-bfd.h" | |
37 | #include "elf/dwarf2.h" | |
38 | ||
39 | /* The data in the .debug_line statement prologue looks like this. */ | |
a092b084 | 40 | |
252b5132 | 41 | struct line_head |
a092b084 | 42 | { |
d03ba2a1 | 43 | bfd_vma total_length; |
a092b084 | 44 | unsigned short version; |
f46c2da6 | 45 | bfd_vma prologue_length; |
a092b084 NC |
46 | unsigned char minimum_instruction_length; |
47 | unsigned char default_is_stmt; | |
48 | int line_base; | |
49 | unsigned char line_range; | |
50 | unsigned char opcode_base; | |
51 | unsigned char *standard_opcode_lengths; | |
52 | }; | |
53 | ||
54 | /* Attributes have a name and a value. */ | |
55 | ||
252b5132 | 56 | struct attribute |
a092b084 NC |
57 | { |
58 | enum dwarf_attribute name; | |
59 | enum dwarf_form form; | |
60 | union | |
252b5132 | 61 | { |
a092b084 NC |
62 | char *str; |
63 | struct dwarf_block *blk; | |
64 | unsigned int unsnd; | |
65 | int snd; | |
66 | bfd_vma addr; | |
67 | } | |
68 | u; | |
69 | }; | |
70 | ||
71 | /* Get at parts of an attribute structure. */ | |
252b5132 RH |
72 | |
73 | #define DW_STRING(attr) ((attr)->u.str) | |
74 | #define DW_UNSND(attr) ((attr)->u.unsnd) | |
75 | #define DW_BLOCK(attr) ((attr)->u.blk) | |
76 | #define DW_SND(attr) ((attr)->u.snd) | |
77 | #define DW_ADDR(attr) ((attr)->u.addr) | |
78 | ||
98591c73 | 79 | /* Blocks are a bunch of untyped bytes. */ |
252b5132 | 80 | struct dwarf_block |
a092b084 NC |
81 | { |
82 | unsigned int size; | |
83 | char *data; | |
84 | }; | |
252b5132 | 85 | |
a092b084 NC |
86 | struct dwarf2_debug |
87 | { | |
88 | /* A list of all previously read comp_units. */ | |
252b5132 RH |
89 | struct comp_unit* all_comp_units; |
90 | ||
91 | /* The next unread compilation unit within the .debug_info section. | |
92 | Zero indicates that the .debug_info section has not been loaded | |
a092b084 | 93 | into a buffer yet. */ |
252b5132 RH |
94 | char* info_ptr; |
95 | ||
a092b084 | 96 | /* Pointer to the end of the .debug_info section memory buffer. */ |
252b5132 RH |
97 | char* info_ptr_end; |
98 | ||
f2363ce5 AO |
99 | /* Pointer to the section and address of the beginning of the |
100 | section. */ | |
101 | asection* sec; | |
102 | char* sec_info_ptr; | |
103 | ||
104 | /* Pointer to the symbol table. */ | |
105 | asymbol** syms; | |
106 | ||
a092b084 | 107 | /* Pointer to the .debug_abbrev section loaded into memory. */ |
252b5132 RH |
108 | char* dwarf_abbrev_buffer; |
109 | ||
a092b084 | 110 | /* Length of the loaded .debug_abbrev section. */ |
252b5132 | 111 | unsigned long dwarf_abbrev_size; |
69dd2e2d RH |
112 | |
113 | /* Buffer for decode_line_info. */ | |
114 | char *dwarf_line_buffer; | |
ccdb16fc JW |
115 | |
116 | /* Length of the loaded .debug_line section. */ | |
117 | unsigned long dwarf_line_size; | |
d03ba2a1 JJ |
118 | |
119 | /* Pointer to the .debug_str section loaded into memory. */ | |
120 | char* dwarf_str_buffer; | |
121 | ||
122 | /* Length of the loaded .debug_str section. */ | |
123 | unsigned long dwarf_str_size; | |
252b5132 RH |
124 | }; |
125 | ||
a092b084 NC |
126 | struct arange |
127 | { | |
f623be2b RH |
128 | struct arange *next; |
129 | bfd_vma low; | |
130 | bfd_vma high; | |
131 | }; | |
252b5132 | 132 | |
252b5132 | 133 | /* A minimal decoding of DWARF2 compilation units. We only decode |
a092b084 | 134 | what's needed to get to the line number information. */ |
252b5132 | 135 | |
a092b084 NC |
136 | struct comp_unit |
137 | { | |
138 | /* Chain the previously read compilation units. */ | |
252b5132 RH |
139 | struct comp_unit* next_unit; |
140 | ||
a092b084 | 141 | /* Keep the bdf convenient (for memory allocation). */ |
252b5132 RH |
142 | bfd* abfd; |
143 | ||
144 | /* The lowest and higest addresses contained in this compilation | |
a092b084 | 145 | unit as specified in the compilation unit header. */ |
f623be2b | 146 | struct arange arange; |
252b5132 | 147 | |
a092b084 | 148 | /* The DW_AT_name attribute (for error messages). */ |
252b5132 RH |
149 | char* name; |
150 | ||
a092b084 | 151 | /* The abbrev hash table. */ |
252b5132 RH |
152 | struct abbrev_info** abbrevs; |
153 | ||
a092b084 | 154 | /* Note that an error was found by comp_unit_find_nearest_line. */ |
252b5132 RH |
155 | int error; |
156 | ||
a092b084 | 157 | /* The DW_AT_comp_dir attribute. */ |
252b5132 RH |
158 | char* comp_dir; |
159 | ||
a092b084 | 160 | /* True if there is a line number table associated with this comp. unit. */ |
252b5132 | 161 | int stmtlist; |
98591c73 | 162 | |
a092b084 | 163 | /* The offset into .debug_line of the line number table. */ |
252b5132 RH |
164 | unsigned long line_offset; |
165 | ||
a092b084 | 166 | /* Pointer to the first child die for the comp unit. */ |
252b5132 RH |
167 | char *first_child_die_ptr; |
168 | ||
a092b084 | 169 | /* The end of the comp unit. */ |
252b5132 RH |
170 | char *end_ptr; |
171 | ||
a092b084 | 172 | /* The decoded line number, NULL if not yet decoded. */ |
252b5132 RH |
173 | struct line_info_table* line_table; |
174 | ||
a092b084 | 175 | /* A list of the functions found in this comp. unit. */ |
98591c73 | 176 | struct funcinfo* function_table; |
252b5132 | 177 | |
d03ba2a1 JJ |
178 | /* Pointer to dwarf2_debug structure. */ |
179 | struct dwarf2_debug *stash; | |
180 | ||
a092b084 | 181 | /* Address size for this unit - from unit header. */ |
252b5132 | 182 | unsigned char addr_size; |
d03ba2a1 JJ |
183 | |
184 | /* Offset size for this unit - from unit header. */ | |
185 | unsigned char offset_size; | |
252b5132 RH |
186 | }; |
187 | ||
a7b97311 AM |
188 | /* This data structure holds the information of an abbrev. */ |
189 | struct abbrev_info | |
190 | { | |
191 | unsigned int number; /* Number identifying abbrev. */ | |
192 | enum dwarf_tag tag; /* DWARF tag. */ | |
193 | int has_children; /* Boolean. */ | |
194 | unsigned int num_attrs; /* Number of attributes. */ | |
195 | struct attr_abbrev *attrs; /* An array of attribute descriptions. */ | |
196 | struct abbrev_info *next; /* Next in chain. */ | |
197 | }; | |
198 | ||
199 | struct attr_abbrev | |
200 | { | |
201 | enum dwarf_attribute name; | |
202 | enum dwarf_form form; | |
203 | }; | |
204 | ||
205 | #ifndef ABBREV_HASH_SIZE | |
206 | #define ABBREV_HASH_SIZE 121 | |
207 | #endif | |
208 | #ifndef ATTR_ALLOC_CHUNK | |
209 | #define ATTR_ALLOC_CHUNK 4 | |
210 | #endif | |
211 | ||
212 | static unsigned int read_1_byte PARAMS ((bfd *, char *)); | |
213 | static int read_1_signed_byte PARAMS ((bfd *, char *)); | |
214 | static unsigned int read_2_bytes PARAMS ((bfd *, char *)); | |
215 | static unsigned int read_4_bytes PARAMS ((bfd *, char *)); | |
d03ba2a1 | 216 | static bfd_vma read_8_bytes PARAMS ((bfd *, char *)); |
a7b97311 AM |
217 | static char *read_n_bytes PARAMS ((bfd *, char *, unsigned int)); |
218 | static char *read_string PARAMS ((bfd *, char *, unsigned int *)); | |
d03ba2a1 | 219 | static char *read_indirect_string PARAMS ((struct comp_unit *, char *, unsigned int *)); |
a7b97311 AM |
220 | static unsigned int read_unsigned_leb128 |
221 | PARAMS ((bfd *, char *, unsigned int *)); | |
222 | static int read_signed_leb128 | |
223 | PARAMS ((bfd *, char *, unsigned int *)); | |
224 | static bfd_vma read_address PARAMS ((struct comp_unit *, char *)); | |
225 | static struct abbrev_info *lookup_abbrev | |
226 | PARAMS ((unsigned int, struct abbrev_info **)); | |
227 | static struct abbrev_info **read_abbrevs | |
f46c2da6 | 228 | PARAMS ((bfd *, bfd_vma, struct dwarf2_debug *)); |
a7b97311 AM |
229 | static char *read_attribute |
230 | PARAMS ((struct attribute *, struct attr_abbrev *, | |
231 | struct comp_unit *, char *)); | |
cf716c56 RH |
232 | static char *read_attribute_value |
233 | PARAMS ((struct attribute *, unsigned, | |
234 | struct comp_unit *, char *)); | |
a7b97311 AM |
235 | static void add_line_info |
236 | PARAMS ((struct line_info_table *, bfd_vma, char *, | |
237 | unsigned int, unsigned int, int)); | |
238 | static char *concat_filename PARAMS ((struct line_info_table *, unsigned int)); | |
239 | static void arange_add PARAMS ((struct comp_unit *, bfd_vma, bfd_vma)); | |
240 | static struct line_info_table *decode_line_info | |
241 | PARAMS ((struct comp_unit *, struct dwarf2_debug *)); | |
242 | static boolean lookup_address_in_line_info_table | |
1ee24f27 DJ |
243 | PARAMS ((struct line_info_table *, bfd_vma, struct funcinfo *, |
244 | const char **, unsigned int *)); | |
a7b97311 | 245 | static boolean lookup_address_in_function_table |
1ee24f27 | 246 | PARAMS ((struct funcinfo *, bfd_vma, struct funcinfo **, const char **)); |
a7b97311 AM |
247 | static boolean scan_unit_for_functions PARAMS ((struct comp_unit *)); |
248 | static bfd_vma find_rela_addend | |
249 | PARAMS ((bfd *, asection *, bfd_size_type, asymbol**)); | |
250 | static struct comp_unit *parse_comp_unit | |
251 | PARAMS ((bfd *, struct dwarf2_debug *, bfd_vma, unsigned int)); | |
252 | static boolean comp_unit_contains_address | |
253 | PARAMS ((struct comp_unit *, bfd_vma)); | |
254 | static boolean comp_unit_find_nearest_line | |
255 | PARAMS ((struct comp_unit *, bfd_vma, const char **, const char **, | |
256 | unsigned int *, struct dwarf2_debug *)); | |
257 | static asection *find_debug_info PARAMS ((bfd *, asection *)); | |
258 | ||
98591c73 KH |
259 | /* VERBATIM |
260 | The following function up to the END VERBATIM mark are | |
a092b084 | 261 | copied directly from dwarf2read.c. */ |
252b5132 | 262 | |
a092b084 | 263 | /* Read dwarf information from a buffer. */ |
252b5132 RH |
264 | |
265 | static unsigned int | |
266 | read_1_byte (abfd, buf) | |
7442e600 | 267 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
268 | char *buf; |
269 | { | |
270 | return bfd_get_8 (abfd, (bfd_byte *) buf); | |
271 | } | |
272 | ||
273 | static int | |
274 | read_1_signed_byte (abfd, buf) | |
7442e600 | 275 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
276 | char *buf; |
277 | { | |
278 | return bfd_get_signed_8 (abfd, (bfd_byte *) buf); | |
279 | } | |
280 | ||
281 | static unsigned int | |
282 | read_2_bytes (abfd, buf) | |
283 | bfd *abfd; | |
284 | char *buf; | |
285 | { | |
286 | return bfd_get_16 (abfd, (bfd_byte *) buf); | |
287 | } | |
288 | ||
a092b084 | 289 | #if 0 /* This is not used. */ |
252b5132 RH |
290 | |
291 | static int | |
292 | read_2_signed_bytes (abfd, buf) | |
293 | bfd *abfd; | |
294 | char *buf; | |
295 | { | |
296 | return bfd_get_signed_16 (abfd, (bfd_byte *) buf); | |
297 | } | |
298 | ||
299 | #endif | |
300 | ||
301 | static unsigned int | |
302 | read_4_bytes (abfd, buf) | |
303 | bfd *abfd; | |
304 | char *buf; | |
305 | { | |
306 | return bfd_get_32 (abfd, (bfd_byte *) buf); | |
307 | } | |
308 | ||
a092b084 | 309 | #if 0 /* This is not used. */ |
252b5132 RH |
310 | |
311 | static int | |
312 | read_4_signed_bytes (abfd, buf) | |
313 | bfd *abfd; | |
314 | char *buf; | |
315 | { | |
316 | return bfd_get_signed_32 (abfd, (bfd_byte *) buf); | |
317 | } | |
318 | ||
319 | #endif | |
320 | ||
d03ba2a1 | 321 | static bfd_vma |
252b5132 RH |
322 | read_8_bytes (abfd, buf) |
323 | bfd *abfd; | |
324 | char *buf; | |
325 | { | |
326 | return bfd_get_64 (abfd, (bfd_byte *) buf); | |
327 | } | |
328 | ||
329 | static char * | |
330 | read_n_bytes (abfd, buf, size) | |
7442e600 | 331 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 | 332 | char *buf; |
7442e600 | 333 | unsigned int size ATTRIBUTE_UNUSED; |
252b5132 RH |
334 | { |
335 | /* If the size of a host char is 8 bits, we can return a pointer | |
336 | to the buffer, otherwise we have to copy the data to a buffer | |
337 | allocated on the temporary obstack. */ | |
338 | return buf; | |
339 | } | |
340 | ||
341 | static char * | |
342 | read_string (abfd, buf, bytes_read_ptr) | |
7442e600 | 343 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
344 | char *buf; |
345 | unsigned int *bytes_read_ptr; | |
346 | { | |
d03ba2a1 | 347 | /* Return a pointer to the embedded string. */ |
252b5132 RH |
348 | if (*buf == '\0') |
349 | { | |
350 | *bytes_read_ptr = 1; | |
351 | return NULL; | |
352 | } | |
98591c73 | 353 | |
252b5132 RH |
354 | *bytes_read_ptr = strlen (buf) + 1; |
355 | return buf; | |
356 | } | |
357 | ||
d03ba2a1 JJ |
358 | static char * |
359 | read_indirect_string (unit, buf, bytes_read_ptr) | |
360 | struct comp_unit* unit; | |
361 | char *buf; | |
362 | unsigned int *bytes_read_ptr; | |
363 | { | |
364 | bfd_vma offset; | |
365 | struct dwarf2_debug *stash = unit->stash; | |
366 | ||
367 | if (unit->offset_size == 4) | |
368 | offset = read_4_bytes (unit->abfd, buf); | |
369 | else | |
370 | offset = read_8_bytes (unit->abfd, buf); | |
371 | *bytes_read_ptr = unit->offset_size; | |
372 | ||
373 | if (! stash->dwarf_str_buffer) | |
374 | { | |
375 | asection *msec; | |
376 | bfd *abfd = unit->abfd; | |
377 | ||
378 | msec = bfd_get_section_by_name (abfd, ".debug_str"); | |
379 | if (! msec) | |
380 | { | |
381 | (*_bfd_error_handler) | |
382 | (_("Dwarf Error: Can't find .debug_str section.")); | |
383 | bfd_set_error (bfd_error_bad_value); | |
384 | return NULL; | |
385 | } | |
386 | ||
387 | stash->dwarf_str_size = msec->_raw_size; | |
388 | stash->dwarf_str_buffer = (char*) bfd_alloc (abfd, msec->_raw_size); | |
389 | if (! stash->dwarf_abbrev_buffer) | |
390 | return NULL; | |
391 | ||
392 | if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer, | |
393 | (bfd_vma) 0, msec->_raw_size)) | |
394 | return NULL; | |
395 | } | |
396 | ||
397 | if (offset >= stash->dwarf_str_size) | |
398 | { | |
f46c2da6 AM |
399 | (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."), |
400 | (unsigned long) offset, stash->dwarf_str_size); | |
d03ba2a1 JJ |
401 | bfd_set_error (bfd_error_bad_value); |
402 | return NULL; | |
403 | } | |
404 | ||
e82ce529 | 405 | buf = stash->dwarf_str_buffer + offset; |
d03ba2a1 JJ |
406 | if (*buf == '\0') |
407 | return NULL; | |
408 | return buf; | |
409 | } | |
410 | ||
252b5132 RH |
411 | static unsigned int |
412 | read_unsigned_leb128 (abfd, buf, bytes_read_ptr) | |
7442e600 | 413 | bfd *abfd ATTRIBUTE_UNUSED; |
252b5132 RH |
414 | char *buf; |
415 | unsigned int *bytes_read_ptr; | |
416 | { | |
417 | unsigned int result; | |
418 | unsigned int num_read; | |
419 | int shift; | |
420 | unsigned char byte; | |
421 | ||
422 | result = 0; | |
423 | shift = 0; | |
424 | num_read = 0; | |
98591c73 | 425 | |
252b5132 RH |
426 | do |
427 | { | |
428 | byte = bfd_get_8 (abfd, (bfd_byte *) buf); | |
429 | buf ++; | |
430 | num_read ++; | |
431 | result |= ((byte & 0x7f) << shift); | |
432 | shift += 7; | |
433 | } | |
434 | while (byte & 0x80); | |
98591c73 | 435 | |
252b5132 | 436 | * bytes_read_ptr = num_read; |
98591c73 | 437 | |
252b5132 RH |
438 | return result; |
439 | } | |
440 | ||
441 | static int | |
442 | read_signed_leb128 (abfd, buf, bytes_read_ptr) | |
7442e600 ILT |
443 | bfd *abfd ATTRIBUTE_UNUSED; |
444 | char *buf; | |
252b5132 RH |
445 | unsigned int * bytes_read_ptr; |
446 | { | |
447 | int result; | |
448 | int shift; | |
449 | int num_read; | |
450 | unsigned char byte; | |
451 | ||
452 | result = 0; | |
453 | shift = 0; | |
454 | num_read = 0; | |
455 | ||
456 | do | |
457 | { | |
458 | byte = bfd_get_8 (abfd, (bfd_byte *) buf); | |
459 | buf ++; | |
460 | num_read ++; | |
461 | result |= ((byte & 0x7f) << shift); | |
462 | shift += 7; | |
463 | } | |
464 | while (byte & 0x80); | |
98591c73 | 465 | |
252b5132 RH |
466 | if ((shift < 32) && (byte & 0x40)) |
467 | result |= -(1 << shift); | |
468 | ||
469 | * bytes_read_ptr = num_read; | |
98591c73 | 470 | |
252b5132 RH |
471 | return result; |
472 | } | |
473 | ||
474 | /* END VERBATIM */ | |
475 | ||
476 | static bfd_vma | |
477 | read_address (unit, buf) | |
478 | struct comp_unit* unit; | |
479 | char *buf; | |
480 | { | |
ecb651f0 | 481 | switch (unit->addr_size) |
252b5132 | 482 | { |
ecb651f0 NC |
483 | case 8: |
484 | return bfd_get_64 (unit->abfd, (bfd_byte *) buf); | |
485 | case 4: | |
486 | return bfd_get_32 (unit->abfd, (bfd_byte *) buf); | |
487 | case 2: | |
488 | return bfd_get_16 (unit->abfd, (bfd_byte *) buf); | |
489 | default: | |
490 | abort (); | |
252b5132 | 491 | } |
252b5132 RH |
492 | } |
493 | ||
252b5132 RH |
494 | /* Lookup an abbrev_info structure in the abbrev hash table. */ |
495 | ||
496 | static struct abbrev_info * | |
497 | lookup_abbrev (number,abbrevs) | |
498 | unsigned int number; | |
499 | struct abbrev_info **abbrevs; | |
500 | { | |
501 | unsigned int hash_number; | |
502 | struct abbrev_info *abbrev; | |
503 | ||
504 | hash_number = number % ABBREV_HASH_SIZE; | |
505 | abbrev = abbrevs[hash_number]; | |
506 | ||
507 | while (abbrev) | |
508 | { | |
509 | if (abbrev->number == number) | |
510 | return abbrev; | |
511 | else | |
512 | abbrev = abbrev->next; | |
513 | } | |
98591c73 | 514 | |
252b5132 RH |
515 | return NULL; |
516 | } | |
517 | ||
518 | /* In DWARF version 2, the description of the debugging information is | |
519 | stored in a separate .debug_abbrev section. Before we read any | |
520 | dies from a section we read in all abbreviations and install them | |
521 | in a hash table. */ | |
522 | ||
523 | static struct abbrev_info** | |
51db3708 | 524 | read_abbrevs (abfd, offset, stash) |
252b5132 | 525 | bfd * abfd; |
f46c2da6 | 526 | bfd_vma offset; |
51db3708 | 527 | struct dwarf2_debug *stash; |
252b5132 RH |
528 | { |
529 | struct abbrev_info **abbrevs; | |
530 | char *abbrev_ptr; | |
531 | struct abbrev_info *cur_abbrev; | |
532 | unsigned int abbrev_number, bytes_read, abbrev_name; | |
533 | unsigned int abbrev_form, hash_number; | |
dc810e39 | 534 | bfd_size_type amt; |
252b5132 RH |
535 | |
536 | if (! stash->dwarf_abbrev_buffer) | |
537 | { | |
538 | asection *msec; | |
539 | ||
540 | msec = bfd_get_section_by_name (abfd, ".debug_abbrev"); | |
541 | if (! msec) | |
542 | { | |
543 | (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section.")); | |
544 | bfd_set_error (bfd_error_bad_value); | |
545 | return 0; | |
546 | } | |
98591c73 | 547 | |
67d83c76 | 548 | stash->dwarf_abbrev_size = msec->_raw_size; |
dc810e39 | 549 | stash->dwarf_abbrev_buffer = (char*) bfd_alloc (abfd, msec->_raw_size); |
252b5132 RH |
550 | if (! stash->dwarf_abbrev_buffer) |
551 | return 0; | |
98591c73 | 552 | |
dc810e39 AM |
553 | if (! bfd_get_section_contents (abfd, msec, stash->dwarf_abbrev_buffer, |
554 | (bfd_vma) 0, msec->_raw_size)) | |
252b5132 RH |
555 | return 0; |
556 | } | |
557 | ||
f5198f61 | 558 | if (offset >= stash->dwarf_abbrev_size) |
252b5132 | 559 | { |
f46c2da6 AM |
560 | (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."), |
561 | (unsigned long) offset, stash->dwarf_abbrev_size); | |
252b5132 RH |
562 | bfd_set_error (bfd_error_bad_value); |
563 | return 0; | |
564 | } | |
565 | ||
dc810e39 AM |
566 | amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE; |
567 | abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, amt); | |
252b5132 RH |
568 | |
569 | abbrev_ptr = stash->dwarf_abbrev_buffer + offset; | |
570 | abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
571 | abbrev_ptr += bytes_read; | |
572 | ||
a092b084 | 573 | /* Loop until we reach an abbrev number of 0. */ |
252b5132 RH |
574 | while (abbrev_number) |
575 | { | |
dc810e39 AM |
576 | amt = sizeof (struct abbrev_info); |
577 | cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt); | |
252b5132 | 578 | |
a092b084 | 579 | /* Read in abbrev header. */ |
252b5132 RH |
580 | cur_abbrev->number = abbrev_number; |
581 | cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
582 | abbrev_ptr += bytes_read; | |
583 | cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr); | |
584 | abbrev_ptr += 1; | |
585 | ||
a092b084 | 586 | /* Now read in declarations. */ |
252b5132 RH |
587 | abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); |
588 | abbrev_ptr += bytes_read; | |
589 | abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
590 | abbrev_ptr += bytes_read; | |
98591c73 | 591 | |
252b5132 RH |
592 | while (abbrev_name) |
593 | { | |
594 | if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0) | |
595 | { | |
dc810e39 AM |
596 | amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK; |
597 | amt *= sizeof (struct attr_abbrev); | |
598 | cur_abbrev->attrs = ((struct attr_abbrev *) | |
599 | bfd_realloc (cur_abbrev->attrs, amt)); | |
252b5132 RH |
600 | if (! cur_abbrev->attrs) |
601 | return 0; | |
602 | } | |
98591c73 | 603 | |
252b5132 RH |
604 | cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name; |
605 | cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form; | |
606 | abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
607 | abbrev_ptr += bytes_read; | |
608 | abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
609 | abbrev_ptr += bytes_read; | |
610 | } | |
611 | ||
612 | hash_number = abbrev_number % ABBREV_HASH_SIZE; | |
613 | cur_abbrev->next = abbrevs[hash_number]; | |
614 | abbrevs[hash_number] = cur_abbrev; | |
615 | ||
616 | /* Get next abbreviation. | |
e82ce529 | 617 | Under Irix6 the abbreviations for a compilation unit are not |
252b5132 RH |
618 | always properly terminated with an abbrev number of 0. |
619 | Exit loop if we encounter an abbreviation which we have | |
620 | already read (which means we are about to read the abbreviations | |
621 | for the next compile unit) or if the end of the abbreviation | |
622 | table is reached. */ | |
623 | if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer) | |
624 | >= stash->dwarf_abbrev_size) | |
625 | break; | |
626 | abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
627 | abbrev_ptr += bytes_read; | |
628 | if (lookup_abbrev (abbrev_number,abbrevs) != NULL) | |
629 | break; | |
630 | } | |
631 | ||
632 | return abbrevs; | |
633 | } | |
634 | ||
cf716c56 | 635 | /* Read an attribute value described by an attribute form. */ |
252b5132 RH |
636 | |
637 | static char * | |
cf716c56 | 638 | read_attribute_value (attr, form, unit, info_ptr) |
252b5132 | 639 | struct attribute *attr; |
cf716c56 | 640 | unsigned form; |
252b5132 RH |
641 | struct comp_unit *unit; |
642 | char *info_ptr; | |
643 | { | |
644 | bfd *abfd = unit->abfd; | |
645 | unsigned int bytes_read; | |
646 | struct dwarf_block *blk; | |
dc810e39 | 647 | bfd_size_type amt; |
252b5132 | 648 | |
cf716c56 | 649 | attr->form = form; |
98591c73 | 650 | |
cf716c56 | 651 | switch (form) |
252b5132 RH |
652 | { |
653 | case DW_FORM_addr: | |
d03ba2a1 | 654 | /* FIXME: DWARF3 draft sais DW_FORM_ref_addr is offset_size. */ |
252b5132 RH |
655 | case DW_FORM_ref_addr: |
656 | DW_ADDR (attr) = read_address (unit, info_ptr); | |
657 | info_ptr += unit->addr_size; | |
658 | break; | |
659 | case DW_FORM_block2: | |
dc810e39 AM |
660 | amt = sizeof (struct dwarf_block); |
661 | blk = (struct dwarf_block *) bfd_alloc (abfd, amt); | |
252b5132 RH |
662 | blk->size = read_2_bytes (abfd, info_ptr); |
663 | info_ptr += 2; | |
664 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); | |
665 | info_ptr += blk->size; | |
666 | DW_BLOCK (attr) = blk; | |
667 | break; | |
668 | case DW_FORM_block4: | |
dc810e39 AM |
669 | amt = sizeof (struct dwarf_block); |
670 | blk = (struct dwarf_block *) bfd_alloc (abfd, amt); | |
252b5132 RH |
671 | blk->size = read_4_bytes (abfd, info_ptr); |
672 | info_ptr += 4; | |
673 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); | |
674 | info_ptr += blk->size; | |
675 | DW_BLOCK (attr) = blk; | |
676 | break; | |
677 | case DW_FORM_data2: | |
678 | DW_UNSND (attr) = read_2_bytes (abfd, info_ptr); | |
679 | info_ptr += 2; | |
680 | break; | |
681 | case DW_FORM_data4: | |
682 | DW_UNSND (attr) = read_4_bytes (abfd, info_ptr); | |
683 | info_ptr += 4; | |
684 | break; | |
685 | case DW_FORM_data8: | |
686 | DW_UNSND (attr) = read_8_bytes (abfd, info_ptr); | |
687 | info_ptr += 8; | |
688 | break; | |
689 | case DW_FORM_string: | |
690 | DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read); | |
691 | info_ptr += bytes_read; | |
692 | break; | |
d03ba2a1 JJ |
693 | case DW_FORM_strp: |
694 | DW_STRING (attr) = read_indirect_string (unit, info_ptr, &bytes_read); | |
695 | info_ptr += bytes_read; | |
696 | break; | |
252b5132 | 697 | case DW_FORM_block: |
dc810e39 AM |
698 | amt = sizeof (struct dwarf_block); |
699 | blk = (struct dwarf_block *) bfd_alloc (abfd, amt); | |
252b5132 RH |
700 | blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
701 | info_ptr += bytes_read; | |
702 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); | |
703 | info_ptr += blk->size; | |
704 | DW_BLOCK (attr) = blk; | |
705 | break; | |
706 | case DW_FORM_block1: | |
dc810e39 AM |
707 | amt = sizeof (struct dwarf_block); |
708 | blk = (struct dwarf_block *) bfd_alloc (abfd, amt); | |
252b5132 RH |
709 | blk->size = read_1_byte (abfd, info_ptr); |
710 | info_ptr += 1; | |
711 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); | |
712 | info_ptr += blk->size; | |
713 | DW_BLOCK (attr) = blk; | |
714 | break; | |
715 | case DW_FORM_data1: | |
716 | DW_UNSND (attr) = read_1_byte (abfd, info_ptr); | |
717 | info_ptr += 1; | |
718 | break; | |
719 | case DW_FORM_flag: | |
720 | DW_UNSND (attr) = read_1_byte (abfd, info_ptr); | |
721 | info_ptr += 1; | |
722 | break; | |
723 | case DW_FORM_sdata: | |
724 | DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read); | |
725 | info_ptr += bytes_read; | |
726 | break; | |
727 | case DW_FORM_udata: | |
728 | DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); | |
729 | info_ptr += bytes_read; | |
730 | break; | |
731 | case DW_FORM_ref1: | |
732 | DW_UNSND (attr) = read_1_byte (abfd, info_ptr); | |
733 | info_ptr += 1; | |
734 | break; | |
735 | case DW_FORM_ref2: | |
736 | DW_UNSND (attr) = read_2_bytes (abfd, info_ptr); | |
737 | info_ptr += 2; | |
738 | break; | |
739 | case DW_FORM_ref4: | |
740 | DW_UNSND (attr) = read_4_bytes (abfd, info_ptr); | |
741 | info_ptr += 4; | |
742 | break; | |
81edd86d MM |
743 | case DW_FORM_ref8: |
744 | DW_UNSND (attr) = read_8_bytes (abfd, info_ptr); | |
745 | info_ptr += 8; | |
746 | break; | |
252b5132 RH |
747 | case DW_FORM_ref_udata: |
748 | DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); | |
749 | info_ptr += bytes_read; | |
750 | break; | |
252b5132 | 751 | case DW_FORM_indirect: |
cf716c56 RH |
752 | form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); |
753 | info_ptr += bytes_read; | |
754 | info_ptr = read_attribute_value (attr, form, unit, info_ptr); | |
755 | break; | |
252b5132 | 756 | default: |
f46c2da6 | 757 | (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."), |
cf716c56 | 758 | form); |
252b5132 RH |
759 | bfd_set_error (bfd_error_bad_value); |
760 | } | |
761 | return info_ptr; | |
762 | } | |
763 | ||
cf716c56 RH |
764 | /* Read an attribute described by an abbreviated attribute. */ |
765 | ||
766 | static char * | |
767 | read_attribute (attr, abbrev, unit, info_ptr) | |
768 | struct attribute *attr; | |
769 | struct attr_abbrev *abbrev; | |
770 | struct comp_unit *unit; | |
771 | char *info_ptr; | |
772 | { | |
773 | attr->name = abbrev->name; | |
774 | info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr); | |
775 | return info_ptr; | |
776 | } | |
777 | ||
a092b084 | 778 | /* Source line information table routines. */ |
252b5132 RH |
779 | |
780 | #define FILE_ALLOC_CHUNK 5 | |
781 | #define DIR_ALLOC_CHUNK 5 | |
782 | ||
a092b084 NC |
783 | struct line_info |
784 | { | |
252b5132 | 785 | struct line_info* prev_line; |
252b5132 RH |
786 | bfd_vma address; |
787 | char* filename; | |
788 | unsigned int line; | |
789 | unsigned int column; | |
a092b084 | 790 | int end_sequence; /* End of (sequential) code sequence. */ |
252b5132 RH |
791 | }; |
792 | ||
a092b084 NC |
793 | struct fileinfo |
794 | { | |
252b5132 RH |
795 | char *name; |
796 | unsigned int dir; | |
797 | unsigned int time; | |
798 | unsigned int size; | |
799 | }; | |
800 | ||
a092b084 NC |
801 | struct line_info_table |
802 | { | |
252b5132 | 803 | bfd* abfd; |
252b5132 RH |
804 | unsigned int num_files; |
805 | unsigned int num_dirs; | |
252b5132 RH |
806 | char* comp_dir; |
807 | char** dirs; | |
808 | struct fileinfo* files; | |
e82ce529 AM |
809 | struct line_info* last_line; /* largest VMA */ |
810 | struct line_info* lcl_head; /* local head; used in 'add_line_info' */ | |
252b5132 RH |
811 | }; |
812 | ||
1ee24f27 DJ |
813 | struct funcinfo |
814 | { | |
815 | struct funcinfo *prev_func; | |
816 | char* name; | |
817 | bfd_vma low; | |
818 | bfd_vma high; | |
819 | }; | |
820 | ||
e82ce529 AM |
821 | /* add_line_info: adds a new entry to the line_info list in the |
822 | line_info_table, ensuring that the list is sorted. Note that the | |
823 | line_info list is sorted from highest to lowest VMA (with possible | |
824 | duplicates); that is, line_info->prev_line always accesses an equal | |
825 | or smaller VMA. */ | |
98591c73 | 826 | static void |
159002ff | 827 | add_line_info (table, address, filename, line, column, end_sequence) |
252b5132 RH |
828 | struct line_info_table* table; |
829 | bfd_vma address; | |
830 | char* filename; | |
831 | unsigned int line; | |
832 | unsigned int column; | |
159002ff | 833 | int end_sequence; |
252b5132 | 834 | { |
dc810e39 AM |
835 | bfd_size_type amt = sizeof (struct line_info); |
836 | struct line_info* info = (struct line_info*) bfd_alloc (table->abfd, amt); | |
252b5132 | 837 | |
e82ce529 AM |
838 | /* Find the correct location for 'info'. Normally we will receive |
839 | new line_info data 1) in order and 2) with increasing VMAs. | |
840 | However some compilers break the rules (cf. decode_line_info) and | |
841 | so we include some heuristics for quickly finding the correct | |
842 | location for 'info'. In particular, these heuristics optimize for | |
843 | the common case in which the VMA sequence that we receive is a | |
844 | list of locally sorted VMAs such as | |
845 | p...z a...j (where a < j < p < z) | |
252b5132 | 846 | |
e82ce529 AM |
847 | Note: table->lcl_head is used to head an *actual* or *possible* |
848 | sequence within the list (such as a...j) that is not directly | |
849 | headed by table->last_line | |
850 | ||
851 | Note: we may receive duplicate entries from 'decode_line_info'. */ | |
852 | ||
853 | while (1) | |
854 | if (!table->last_line | |
855 | || address >= table->last_line->address) | |
856 | { | |
857 | /* Normal case: add 'info' to the beginning of the list */ | |
858 | info->prev_line = table->last_line; | |
859 | table->last_line = info; | |
860 | ||
861 | /* lcl_head: initialize to head a *possible* sequence at the end. */ | |
862 | if (!table->lcl_head) | |
863 | table->lcl_head = info; | |
864 | break; | |
865 | } | |
866 | else if (!table->lcl_head->prev_line | |
867 | && table->lcl_head->address > address) | |
868 | { | |
869 | /* Abnormal but easy: lcl_head is 1) at the *end* of the line | |
870 | list and 2) the head of 'info'. */ | |
871 | info->prev_line = NULL; | |
872 | table->lcl_head->prev_line = info; | |
873 | break; | |
874 | } | |
875 | else if (table->lcl_head->prev_line | |
876 | && table->lcl_head->address > address | |
877 | && address >= table->lcl_head->prev_line->address) | |
878 | { | |
879 | /* Abnormal but easy: lcl_head is 1) in the *middle* of the line | |
880 | list and 2) the head of 'info'. */ | |
881 | info->prev_line = table->lcl_head->prev_line; | |
882 | table->lcl_head->prev_line = info; | |
883 | break; | |
884 | } | |
885 | else | |
886 | { | |
887 | /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid | |
888 | heads for 'info'. Reset 'lcl_head' and repeat. */ | |
889 | struct line_info* li2 = table->last_line; /* always non-NULL */ | |
890 | struct line_info* li1 = li2->prev_line; | |
891 | ||
892 | while (li1) | |
893 | { | |
894 | if (li2->address > address && address >= li1->address) | |
895 | break; | |
896 | ||
897 | li2 = li1; /* always non-NULL */ | |
898 | li1 = li1->prev_line; | |
899 | } | |
900 | table->lcl_head = li2; | |
901 | } | |
902 | ||
903 | /* Set member data of 'info'. */ | |
252b5132 RH |
904 | info->address = address; |
905 | info->filename = filename; | |
906 | info->line = line; | |
907 | info->column = column; | |
159002ff | 908 | info->end_sequence = end_sequence; |
252b5132 RH |
909 | } |
910 | ||
a092b084 | 911 | static char * |
252b5132 RH |
912 | concat_filename (table, file) |
913 | struct line_info_table* table; | |
914 | unsigned int file; | |
915 | { | |
159002ff RH |
916 | char* filename; |
917 | ||
918 | if (file - 1 >= table->num_files) | |
919 | { | |
dcdea4f4 AM |
920 | (*_bfd_error_handler) |
921 | (_("Dwarf Error: mangled line number section (bad file number).")); | |
159002ff RH |
922 | return "<unknown>"; |
923 | } | |
924 | ||
925 | filename = table->files[file - 1].name; | |
51db3708 | 926 | if (IS_ABSOLUTE_PATH(filename)) |
252b5132 | 927 | return filename; |
252b5132 RH |
928 | else |
929 | { | |
930 | char* dirname = (table->files[file - 1].dir | |
931 | ? table->dirs[table->files[file - 1].dir - 1] | |
932 | : table->comp_dir); | |
0dafd5f6 RE |
933 | |
934 | /* Not all tools set DW_AT_comp_dir, so dirname may be unknown. The | |
935 | best we can do is return the filename part. */ | |
936 | if (dirname == NULL) | |
937 | return filename; | |
938 | else | |
939 | return (char*) concat (dirname, "/", filename, NULL); | |
252b5132 RH |
940 | } |
941 | } | |
942 | ||
f623be2b RH |
943 | static void |
944 | arange_add (unit, low_pc, high_pc) | |
945 | struct comp_unit *unit; | |
946 | bfd_vma low_pc; | |
947 | bfd_vma high_pc; | |
948 | { | |
949 | struct arange *arange; | |
950 | ||
a092b084 | 951 | /* First see if we can cheaply extend an existing range. */ |
f623be2b | 952 | arange = &unit->arange; |
98591c73 | 953 | |
f623be2b RH |
954 | do |
955 | { | |
956 | if (low_pc == arange->high) | |
957 | { | |
958 | arange->high = high_pc; | |
959 | return; | |
960 | } | |
961 | if (high_pc == arange->low) | |
962 | { | |
963 | arange->low = low_pc; | |
964 | return; | |
965 | } | |
966 | arange = arange->next; | |
967 | } | |
968 | while (arange); | |
969 | ||
970 | if (unit->arange.high == 0) | |
971 | { | |
a092b084 | 972 | /* This is the first address range: store it in unit->arange. */ |
f623be2b RH |
973 | unit->arange.next = 0; |
974 | unit->arange.low = low_pc; | |
975 | unit->arange.high = high_pc; | |
976 | return; | |
977 | } | |
978 | ||
a092b084 | 979 | /* Need to allocate a new arange and insert it into the arange list. */ |
dc810e39 | 980 | arange = bfd_zalloc (unit->abfd, (bfd_size_type) sizeof (*arange)); |
f623be2b RH |
981 | arange->low = low_pc; |
982 | arange->high = high_pc; | |
983 | ||
984 | arange->next = unit->arange.next; | |
985 | unit->arange.next = arange; | |
986 | } | |
987 | ||
a092b084 | 988 | /* Decode the line number information for UNIT. */ |
252b5132 RH |
989 | |
990 | static struct line_info_table* | |
51db3708 | 991 | decode_line_info (unit, stash) |
252b5132 | 992 | struct comp_unit *unit; |
51db3708 | 993 | struct dwarf2_debug *stash; |
252b5132 RH |
994 | { |
995 | bfd *abfd = unit->abfd; | |
252b5132 | 996 | struct line_info_table* table; |
252b5132 RH |
997 | char *line_ptr; |
998 | char *line_end; | |
999 | struct line_head lh; | |
d03ba2a1 | 1000 | unsigned int i, bytes_read, offset_size; |
252b5132 RH |
1001 | char *cur_file, *cur_dir; |
1002 | unsigned char op_code, extended_op, adj_opcode; | |
dc810e39 | 1003 | bfd_size_type amt; |
252b5132 | 1004 | |
69dd2e2d | 1005 | if (! stash->dwarf_line_buffer) |
252b5132 RH |
1006 | { |
1007 | asection *msec; | |
252b5132 RH |
1008 | |
1009 | msec = bfd_get_section_by_name (abfd, ".debug_line"); | |
1010 | if (! msec) | |
1011 | { | |
1012 | (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section.")); | |
1013 | bfd_set_error (bfd_error_bad_value); | |
1014 | return 0; | |
1015 | } | |
98591c73 | 1016 | |
ccdb16fc | 1017 | stash->dwarf_line_size = msec->_raw_size; |
dc810e39 | 1018 | stash->dwarf_line_buffer = (char *) bfd_alloc (abfd, msec->_raw_size); |
f623be2b | 1019 | if (! stash->dwarf_line_buffer) |
252b5132 RH |
1020 | return 0; |
1021 | ||
dc810e39 AM |
1022 | if (! bfd_get_section_contents (abfd, msec, stash->dwarf_line_buffer, |
1023 | (bfd_vma) 0, msec->_raw_size)) | |
252b5132 RH |
1024 | return 0; |
1025 | ||
1026 | /* FIXME: We ought to apply the relocs against this section before | |
a092b084 | 1027 | we process it... */ |
252b5132 RH |
1028 | } |
1029 | ||
ccdb16fc JW |
1030 | /* Since we are using un-relocated data, it is possible to get a bad value |
1031 | for the line_offset. Validate it here so that we won't get a segfault | |
1032 | below. */ | |
1033 | if (unit->line_offset >= stash->dwarf_line_size) | |
1034 | { | |
f46c2da6 | 1035 | (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."), |
ccdb16fc JW |
1036 | unit->line_offset, stash->dwarf_line_size); |
1037 | bfd_set_error (bfd_error_bad_value); | |
1038 | return 0; | |
1039 | } | |
1040 | ||
dc810e39 AM |
1041 | amt = sizeof (struct line_info_table); |
1042 | table = (struct line_info_table*) bfd_alloc (abfd, amt); | |
252b5132 RH |
1043 | table->abfd = abfd; |
1044 | table->comp_dir = unit->comp_dir; | |
1045 | ||
1046 | table->num_files = 0; | |
1047 | table->files = NULL; | |
1048 | ||
1049 | table->num_dirs = 0; | |
1050 | table->dirs = NULL; | |
1051 | ||
159002ff RH |
1052 | table->files = NULL; |
1053 | table->last_line = NULL; | |
e82ce529 | 1054 | table->lcl_head = NULL; |
159002ff | 1055 | |
69dd2e2d | 1056 | line_ptr = stash->dwarf_line_buffer + unit->line_offset; |
252b5132 | 1057 | |
a092b084 | 1058 | /* Read in the prologue. */ |
91a4d569 AM |
1059 | lh.total_length = read_4_bytes (abfd, line_ptr); |
1060 | line_ptr += 4; | |
1061 | offset_size = 4; | |
1062 | if (lh.total_length == 0xffffffff) | |
dae2dd0d | 1063 | { |
dae2dd0d NC |
1064 | lh.total_length = read_8_bytes (abfd, line_ptr); |
1065 | line_ptr += 8; | |
1066 | offset_size = 8; | |
1067 | } | |
91a4d569 | 1068 | else if (lh.total_length == 0 && unit->addr_size == 8) |
d03ba2a1 | 1069 | { |
91a4d569 AM |
1070 | /* Handle (non-standard) 64-bit DWARF2 formats. */ |
1071 | lh.total_length = read_4_bytes (abfd, line_ptr); | |
1072 | line_ptr += 4; | |
d03ba2a1 JJ |
1073 | offset_size = 8; |
1074 | } | |
252b5132 RH |
1075 | line_end = line_ptr + lh.total_length; |
1076 | lh.version = read_2_bytes (abfd, line_ptr); | |
1077 | line_ptr += 2; | |
d03ba2a1 JJ |
1078 | if (offset_size == 4) |
1079 | lh.prologue_length = read_4_bytes (abfd, line_ptr); | |
1080 | else | |
1081 | lh.prologue_length = read_8_bytes (abfd, line_ptr); | |
1082 | line_ptr += offset_size; | |
252b5132 RH |
1083 | lh.minimum_instruction_length = read_1_byte (abfd, line_ptr); |
1084 | line_ptr += 1; | |
1085 | lh.default_is_stmt = read_1_byte (abfd, line_ptr); | |
1086 | line_ptr += 1; | |
1087 | lh.line_base = read_1_signed_byte (abfd, line_ptr); | |
1088 | line_ptr += 1; | |
1089 | lh.line_range = read_1_byte (abfd, line_ptr); | |
1090 | line_ptr += 1; | |
1091 | lh.opcode_base = read_1_byte (abfd, line_ptr); | |
1092 | line_ptr += 1; | |
dc810e39 AM |
1093 | amt = lh.opcode_base * sizeof (unsigned char); |
1094 | lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt); | |
252b5132 RH |
1095 | |
1096 | lh.standard_opcode_lengths[0] = 1; | |
98591c73 | 1097 | |
252b5132 RH |
1098 | for (i = 1; i < lh.opcode_base; ++i) |
1099 | { | |
1100 | lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr); | |
1101 | line_ptr += 1; | |
1102 | } | |
1103 | ||
a092b084 | 1104 | /* Read directory table. */ |
252b5132 RH |
1105 | while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL) |
1106 | { | |
1107 | line_ptr += bytes_read; | |
98591c73 | 1108 | |
252b5132 RH |
1109 | if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0) |
1110 | { | |
dc810e39 AM |
1111 | amt = table->num_dirs + DIR_ALLOC_CHUNK; |
1112 | amt *= sizeof (char *); | |
1113 | table->dirs = (char **) bfd_realloc (table->dirs, amt); | |
252b5132 RH |
1114 | if (! table->dirs) |
1115 | return 0; | |
1116 | } | |
98591c73 | 1117 | |
252b5132 RH |
1118 | table->dirs[table->num_dirs++] = cur_dir; |
1119 | } | |
98591c73 | 1120 | |
252b5132 RH |
1121 | line_ptr += bytes_read; |
1122 | ||
a092b084 | 1123 | /* Read file name table. */ |
252b5132 RH |
1124 | while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL) |
1125 | { | |
1126 | line_ptr += bytes_read; | |
98591c73 | 1127 | |
252b5132 RH |
1128 | if ((table->num_files % FILE_ALLOC_CHUNK) == 0) |
1129 | { | |
dc810e39 AM |
1130 | amt = table->num_files + FILE_ALLOC_CHUNK; |
1131 | amt *= sizeof (struct fileinfo); | |
1132 | table->files = (struct fileinfo *) bfd_realloc (table->files, amt); | |
252b5132 RH |
1133 | if (! table->files) |
1134 | return 0; | |
1135 | } | |
98591c73 | 1136 | |
252b5132 RH |
1137 | table->files[table->num_files].name = cur_file; |
1138 | table->files[table->num_files].dir = | |
1139 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1140 | line_ptr += bytes_read; | |
1141 | table->files[table->num_files].time = | |
1142 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1143 | line_ptr += bytes_read; | |
1144 | table->files[table->num_files].size = | |
1145 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1146 | line_ptr += bytes_read; | |
1147 | table->num_files++; | |
1148 | } | |
98591c73 | 1149 | |
252b5132 RH |
1150 | line_ptr += bytes_read; |
1151 | ||
1152 | /* Read the statement sequences until there's nothing left. */ | |
1153 | while (line_ptr < line_end) | |
1154 | { | |
a092b084 | 1155 | /* State machine registers. */ |
252b5132 | 1156 | bfd_vma address = 0; |
e2f6d277 | 1157 | char * filename = concat_filename (table, 1); |
252b5132 RH |
1158 | unsigned int line = 1; |
1159 | unsigned int column = 0; | |
1160 | int is_stmt = lh.default_is_stmt; | |
1161 | int basic_block = 0; | |
e2f6d277 NC |
1162 | int end_sequence = 0; |
1163 | /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some | |
e82ce529 AM |
1164 | compilers generate address sequences that are wildly out of |
1165 | order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler | |
1166 | for ia64-Linux). Thus, to determine the low and high | |
1167 | address, we must compare on every DW_LNS_copy, etc. */ | |
e2f6d277 NC |
1168 | bfd_vma low_pc = 0; |
1169 | bfd_vma high_pc = 0; | |
252b5132 | 1170 | |
a092b084 | 1171 | /* Decode the table. */ |
252b5132 RH |
1172 | while (! end_sequence) |
1173 | { | |
1174 | op_code = read_1_byte (abfd, line_ptr); | |
1175 | line_ptr += 1; | |
98591c73 | 1176 | |
1a509dcc | 1177 | if (op_code >= lh.opcode_base) |
e2f6d277 NC |
1178 | { |
1179 | /* Special operand. */ | |
1a509dcc GK |
1180 | adj_opcode = op_code - lh.opcode_base; |
1181 | address += (adj_opcode / lh.line_range) | |
1182 | * lh.minimum_instruction_length; | |
1183 | line += lh.line_base + (adj_opcode % lh.line_range); | |
1184 | /* Append row to matrix using current values. */ | |
1185 | add_line_info (table, address, filename, line, column, 0); | |
1186 | basic_block = 1; | |
e2f6d277 NC |
1187 | if (low_pc == 0 || address < low_pc) |
1188 | low_pc = address; | |
1189 | if (address > high_pc) | |
1190 | high_pc = address; | |
1a509dcc GK |
1191 | } |
1192 | else switch (op_code) | |
252b5132 RH |
1193 | { |
1194 | case DW_LNS_extended_op: | |
e2f6d277 NC |
1195 | /* Ignore length. */ |
1196 | line_ptr += 1; | |
252b5132 RH |
1197 | extended_op = read_1_byte (abfd, line_ptr); |
1198 | line_ptr += 1; | |
e2f6d277 | 1199 | |
252b5132 RH |
1200 | switch (extended_op) |
1201 | { | |
1202 | case DW_LNE_end_sequence: | |
1203 | end_sequence = 1; | |
f623be2b RH |
1204 | add_line_info (table, address, filename, line, column, |
1205 | end_sequence); | |
e2f6d277 NC |
1206 | if (low_pc == 0 || address < low_pc) |
1207 | low_pc = address; | |
1208 | if (address > high_pc) | |
1209 | high_pc = address; | |
a2ce5bdc | 1210 | arange_add (unit, low_pc, high_pc); |
252b5132 RH |
1211 | break; |
1212 | case DW_LNE_set_address: | |
1213 | address = read_address (unit, line_ptr); | |
1214 | line_ptr += unit->addr_size; | |
1215 | break; | |
1216 | case DW_LNE_define_file: | |
1217 | cur_file = read_string (abfd, line_ptr, &bytes_read); | |
1218 | line_ptr += bytes_read; | |
1219 | if ((table->num_files % FILE_ALLOC_CHUNK) == 0) | |
1220 | { | |
dc810e39 AM |
1221 | amt = table->num_files + FILE_ALLOC_CHUNK; |
1222 | amt *= sizeof (struct fileinfo); | |
1223 | table->files = | |
1224 | (struct fileinfo *) bfd_realloc (table->files, amt); | |
252b5132 RH |
1225 | if (! table->files) |
1226 | return 0; | |
1227 | } | |
1228 | table->files[table->num_files].name = cur_file; | |
1229 | table->files[table->num_files].dir = | |
1230 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1231 | line_ptr += bytes_read; | |
1232 | table->files[table->num_files].time = | |
1233 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1234 | line_ptr += bytes_read; | |
1235 | table->files[table->num_files].size = | |
1236 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1237 | line_ptr += bytes_read; | |
1238 | table->num_files++; | |
1239 | break; | |
1240 | default: | |
1241 | (*_bfd_error_handler) (_("Dwarf Error: mangled line number section.")); | |
1242 | bfd_set_error (bfd_error_bad_value); | |
1243 | return 0; | |
1244 | } | |
1245 | break; | |
1246 | case DW_LNS_copy: | |
159002ff | 1247 | add_line_info (table, address, filename, line, column, 0); |
252b5132 | 1248 | basic_block = 0; |
e2f6d277 NC |
1249 | if (low_pc == 0 || address < low_pc) |
1250 | low_pc = address; | |
1251 | if (address > high_pc) | |
1252 | high_pc = address; | |
252b5132 RH |
1253 | break; |
1254 | case DW_LNS_advance_pc: | |
1255 | address += lh.minimum_instruction_length | |
1256 | * read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1257 | line_ptr += bytes_read; | |
1258 | break; | |
1259 | case DW_LNS_advance_line: | |
1260 | line += read_signed_leb128 (abfd, line_ptr, &bytes_read); | |
1261 | line_ptr += bytes_read; | |
1262 | break; | |
1263 | case DW_LNS_set_file: | |
1264 | { | |
1265 | unsigned int file; | |
1266 | ||
e2f6d277 NC |
1267 | /* The file and directory tables are 0 |
1268 | based, the references are 1 based. */ | |
252b5132 RH |
1269 | file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read); |
1270 | line_ptr += bytes_read; | |
1271 | filename = concat_filename (table, file); | |
1272 | break; | |
1273 | } | |
1274 | case DW_LNS_set_column: | |
1275 | column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1276 | line_ptr += bytes_read; | |
1277 | break; | |
1278 | case DW_LNS_negate_stmt: | |
1279 | is_stmt = (!is_stmt); | |
1280 | break; | |
1281 | case DW_LNS_set_basic_block: | |
1282 | basic_block = 1; | |
1283 | break; | |
1284 | case DW_LNS_const_add_pc: | |
159002ff RH |
1285 | address += lh.minimum_instruction_length |
1286 | * ((255 - lh.opcode_base) / lh.line_range); | |
252b5132 RH |
1287 | break; |
1288 | case DW_LNS_fixed_advance_pc: | |
1289 | address += read_2_bytes (abfd, line_ptr); | |
1290 | line_ptr += 2; | |
1291 | break; | |
1a509dcc | 1292 | default: |
e2f6d277 | 1293 | { |
1a509dcc | 1294 | int i; |
e2f6d277 | 1295 | /* Unknown standard opcode, ignore it. */ |
1a509dcc GK |
1296 | for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++) |
1297 | { | |
1298 | (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
1299 | line_ptr += bytes_read; | |
1300 | } | |
1301 | } | |
252b5132 RH |
1302 | } |
1303 | } | |
1304 | } | |
1305 | ||
1306 | return table; | |
1307 | } | |
1308 | ||
252b5132 RH |
1309 | /* If ADDR is within TABLE set the output parameters and return true, |
1310 | otherwise return false. The output parameters, FILENAME_PTR and | |
a092b084 | 1311 | LINENUMBER_PTR, are pointers to the objects to be filled in. */ |
252b5132 RH |
1312 | |
1313 | static boolean | |
e2f6d277 | 1314 | lookup_address_in_line_info_table (table, addr, function, filename_ptr, |
252b5132 RH |
1315 | linenumber_ptr) |
1316 | struct line_info_table* table; | |
1317 | bfd_vma addr; | |
1ee24f27 | 1318 | struct funcinfo *function; |
252b5132 RH |
1319 | const char **filename_ptr; |
1320 | unsigned int *linenumber_ptr; | |
1321 | { | |
e82ce529 | 1322 | /* Note: table->last_line should be a descendingly sorted list. */ |
159002ff | 1323 | struct line_info* next_line = table->last_line; |
e82ce529 AM |
1324 | struct line_info* each_line = NULL; |
1325 | *filename_ptr = NULL; | |
98591c73 | 1326 | |
159002ff RH |
1327 | if (!next_line) |
1328 | return false; | |
1329 | ||
1330 | each_line = next_line->prev_line; | |
1331 | ||
e82ce529 AM |
1332 | /* Check for large addresses */ |
1333 | if (addr > next_line->address) | |
1334 | each_line = NULL; /* ensure we skip over the normal case */ | |
1335 | ||
1336 | /* Normal case: search the list; save */ | |
159002ff | 1337 | while (each_line && next_line) |
252b5132 | 1338 | { |
e82ce529 AM |
1339 | /* If we have an address match, save this info. This allows us |
1340 | to return as good as results as possible for strange debugging | |
1341 | info. */ | |
1342 | boolean addr_match = false; | |
1343 | if (each_line->address <= addr && addr <= next_line->address) | |
252b5132 | 1344 | { |
e82ce529 AM |
1345 | addr_match = true; |
1346 | ||
1ee24f27 DJ |
1347 | /* If this line appears to span functions, and addr is in the |
1348 | later function, return the first line of that function instead | |
1349 | of the last line of the earlier one. This check is for GCC | |
1350 | 2.95, which emits the first line number for a function late. */ | |
1351 | if (function != NULL | |
1352 | && each_line->address < function->low | |
1353 | && next_line->address > function->low) | |
1354 | { | |
1355 | *filename_ptr = next_line->filename; | |
1356 | *linenumber_ptr = next_line->line; | |
1357 | } | |
1358 | else | |
1359 | { | |
1360 | *filename_ptr = each_line->filename; | |
1361 | *linenumber_ptr = each_line->line; | |
1362 | } | |
252b5132 | 1363 | } |
e82ce529 AM |
1364 | |
1365 | if (addr_match && !each_line->end_sequence) | |
1366 | return true; /* we have definitely found what we want */ | |
1367 | ||
159002ff RH |
1368 | next_line = each_line; |
1369 | each_line = each_line->prev_line; | |
252b5132 | 1370 | } |
98591c73 | 1371 | |
e82ce529 AM |
1372 | /* At this point each_line is NULL but next_line is not. If we found |
1373 | a candidate end-of-sequence point in the loop above, we can return | |
1374 | that (compatibility with a bug in the Intel compiler); otherwise, | |
1375 | assuming that we found the containing function for this address in | |
1376 | this compilation unit, return the first line we have a number for | |
1377 | (compatibility with GCC 2.95). */ | |
1378 | if (*filename_ptr == NULL && function != NULL) | |
1ee24f27 DJ |
1379 | { |
1380 | *filename_ptr = next_line->filename; | |
1381 | *linenumber_ptr = next_line->line; | |
1382 | return true; | |
1383 | } | |
1384 | ||
252b5132 RH |
1385 | return false; |
1386 | } | |
98591c73 | 1387 | |
a092b084 | 1388 | /* Function table functions. */ |
252b5132 | 1389 | |
98591c73 | 1390 | /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */ |
252b5132 RH |
1391 | |
1392 | static boolean | |
e2f6d277 | 1393 | lookup_address_in_function_table (table, addr, function_ptr, |
252b5132 RH |
1394 | functionname_ptr) |
1395 | struct funcinfo* table; | |
1396 | bfd_vma addr; | |
1ee24f27 | 1397 | struct funcinfo** function_ptr; |
252b5132 RH |
1398 | const char **functionname_ptr; |
1399 | { | |
1400 | struct funcinfo* each_func; | |
1401 | ||
1402 | for (each_func = table; | |
1403 | each_func; | |
1404 | each_func = each_func->prev_func) | |
1405 | { | |
1406 | if (addr >= each_func->low && addr < each_func->high) | |
1407 | { | |
1408 | *functionname_ptr = each_func->name; | |
1ee24f27 | 1409 | *function_ptr = each_func; |
252b5132 RH |
1410 | return true; |
1411 | } | |
1412 | } | |
98591c73 | 1413 | |
252b5132 RH |
1414 | return false; |
1415 | } | |
1416 | ||
a092b084 | 1417 | /* DWARF2 Compilation unit functions. */ |
252b5132 RH |
1418 | |
1419 | /* Scan over each die in a comp. unit looking for functions to add | |
a092b084 | 1420 | to the function table. */ |
252b5132 RH |
1421 | |
1422 | static boolean | |
1423 | scan_unit_for_functions (unit) | |
1424 | struct comp_unit *unit; | |
1425 | { | |
1426 | bfd *abfd = unit->abfd; | |
1427 | char *info_ptr = unit->first_child_die_ptr; | |
1428 | int nesting_level = 1; | |
1429 | ||
1430 | while (nesting_level) | |
1431 | { | |
1432 | unsigned int abbrev_number, bytes_read, i; | |
1433 | struct abbrev_info *abbrev; | |
1434 | struct attribute attr; | |
1435 | struct funcinfo *func; | |
1436 | char* name = 0; | |
1437 | ||
1438 | abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); | |
1439 | info_ptr += bytes_read; | |
1440 | ||
1441 | if (! abbrev_number) | |
1442 | { | |
1443 | nesting_level--; | |
1444 | continue; | |
1445 | } | |
98591c73 | 1446 | |
252b5132 RH |
1447 | abbrev = lookup_abbrev (abbrev_number,unit->abbrevs); |
1448 | if (! abbrev) | |
1449 | { | |
f46c2da6 | 1450 | (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."), |
252b5132 RH |
1451 | abbrev_number); |
1452 | bfd_set_error (bfd_error_bad_value); | |
1453 | return false; | |
1454 | } | |
98591c73 | 1455 | |
252b5132 RH |
1456 | if (abbrev->tag == DW_TAG_subprogram) |
1457 | { | |
dc810e39 AM |
1458 | bfd_size_type amt = sizeof (struct funcinfo); |
1459 | func = (struct funcinfo *) bfd_zalloc (abfd, amt); | |
252b5132 RH |
1460 | func->prev_func = unit->function_table; |
1461 | unit->function_table = func; | |
1462 | } | |
1463 | else | |
1464 | func = NULL; | |
98591c73 | 1465 | |
252b5132 RH |
1466 | for (i = 0; i < abbrev->num_attrs; ++i) |
1467 | { | |
1468 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr); | |
98591c73 | 1469 | |
252b5132 RH |
1470 | if (func) |
1471 | { | |
1472 | switch (attr.name) | |
1473 | { | |
1474 | case DW_AT_name: | |
98591c73 | 1475 | |
252b5132 RH |
1476 | name = DW_STRING (&attr); |
1477 | ||
1478 | /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */ | |
1479 | if (func->name == NULL) | |
1480 | func->name = DW_STRING (&attr); | |
1481 | break; | |
98591c73 | 1482 | |
252b5132 RH |
1483 | case DW_AT_MIPS_linkage_name: |
1484 | func->name = DW_STRING (&attr); | |
1485 | break; | |
1486 | ||
1487 | case DW_AT_low_pc: | |
1488 | func->low = DW_ADDR (&attr); | |
1489 | break; | |
1490 | ||
1491 | case DW_AT_high_pc: | |
1492 | func->high = DW_ADDR (&attr); | |
1493 | break; | |
1494 | ||
1495 | default: | |
1496 | break; | |
1497 | } | |
1498 | } | |
1499 | else | |
1500 | { | |
1501 | switch (attr.name) | |
1502 | { | |
1503 | case DW_AT_name: | |
1504 | name = DW_STRING (&attr); | |
1505 | break; | |
98591c73 | 1506 | |
252b5132 RH |
1507 | default: |
1508 | break; | |
1509 | } | |
1510 | } | |
1511 | } | |
1512 | ||
1513 | if (abbrev->has_children) | |
1514 | nesting_level++; | |
1515 | } | |
1516 | ||
1517 | return true; | |
1518 | } | |
1519 | ||
f2363ce5 AO |
1520 | /* Look for a RELA relocation to be applied on OFFSET of section SEC, |
1521 | and return the addend if such a relocation is found. Since this is | |
1522 | only used to find relocations referring to the .debug_abbrev | |
1523 | section, we make sure the relocation refers to this section, but | |
1524 | this is not strictly necessary, and it can probably be safely | |
1525 | removed if needed. However, it is important to note that this | |
1526 | function only returns the addend, it doesn't serve the purpose of | |
1527 | applying a generic relocation. | |
1528 | ||
1529 | If no suitable relocation is found, or if it is not a real RELA | |
1530 | relocation, this function returns 0. */ | |
1531 | ||
1532 | static bfd_vma | |
1533 | find_rela_addend (abfd, sec, offset, syms) | |
1534 | bfd* abfd; | |
1535 | asection* sec; | |
1536 | bfd_size_type offset; | |
1537 | asymbol** syms; | |
1538 | { | |
1539 | long reloc_size = bfd_get_reloc_upper_bound (abfd, sec); | |
1540 | arelent **relocs = NULL; | |
1541 | long reloc_count, relc; | |
1542 | ||
1543 | if (reloc_size <= 0) | |
1544 | return 0; | |
1545 | ||
dc810e39 | 1546 | relocs = (arelent **) bfd_malloc ((bfd_size_type) reloc_size); |
f2363ce5 AO |
1547 | if (relocs == NULL) |
1548 | return 0; | |
1549 | ||
1550 | reloc_count = bfd_canonicalize_reloc (abfd, sec, relocs, syms); | |
1551 | ||
1552 | if (reloc_count <= 0) | |
1553 | { | |
1554 | free (relocs); | |
1555 | return 0; | |
1556 | } | |
1557 | ||
1558 | for (relc = 0; relc < reloc_count; relc++) | |
1559 | if (relocs[relc]->address == offset | |
1560 | && (*relocs[relc]->sym_ptr_ptr)->flags & BSF_SECTION_SYM | |
1561 | && strcmp ((*relocs[relc]->sym_ptr_ptr)->name, | |
1562 | ".debug_abbrev") == 0) | |
1563 | { | |
1564 | bfd_vma addend = (relocs[relc]->howto->partial_inplace | |
1565 | ? 0 : relocs[relc]->addend); | |
1566 | free (relocs); | |
1567 | return addend; | |
1568 | } | |
dc810e39 | 1569 | |
f2363ce5 AO |
1570 | free (relocs); |
1571 | return 0; | |
1572 | } | |
1573 | ||
5e38c3b8 MM |
1574 | /* Parse a DWARF2 compilation unit starting at INFO_PTR. This |
1575 | includes the compilation unit header that proceeds the DIE's, but | |
1576 | does not include the length field that preceeds each compilation | |
1577 | unit header. END_PTR points one past the end of this comp unit. | |
d03ba2a1 | 1578 | OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes). |
252b5132 RH |
1579 | |
1580 | This routine does not read the whole compilation unit; only enough | |
1581 | to get to the line number information for the compilation unit. */ | |
1582 | ||
1583 | static struct comp_unit * | |
d03ba2a1 | 1584 | parse_comp_unit (abfd, stash, unit_length, offset_size) |
252b5132 | 1585 | bfd* abfd; |
51db3708 NC |
1586 | struct dwarf2_debug *stash; |
1587 | bfd_vma unit_length; | |
d03ba2a1 | 1588 | unsigned int offset_size; |
252b5132 RH |
1589 | { |
1590 | struct comp_unit* unit; | |
f46c2da6 AM |
1591 | unsigned int version; |
1592 | bfd_vma abbrev_offset = 0; | |
1593 | unsigned int addr_size; | |
252b5132 | 1594 | struct abbrev_info** abbrevs; |
252b5132 RH |
1595 | unsigned int abbrev_number, bytes_read, i; |
1596 | struct abbrev_info *abbrev; | |
1597 | struct attribute attr; | |
51db3708 NC |
1598 | char *info_ptr = stash->info_ptr; |
1599 | char *end_ptr = info_ptr + unit_length; | |
dc810e39 AM |
1600 | bfd_size_type amt; |
1601 | bfd_size_type off; | |
3fde5a36 | 1602 | |
252b5132 RH |
1603 | version = read_2_bytes (abfd, info_ptr); |
1604 | info_ptr += 2; | |
d03ba2a1 JJ |
1605 | BFD_ASSERT (offset_size == 4 || offset_size == 8); |
1606 | if (offset_size == 4) | |
5e38c3b8 | 1607 | abbrev_offset = read_4_bytes (abfd, info_ptr); |
d03ba2a1 | 1608 | else |
5e38c3b8 | 1609 | abbrev_offset = read_8_bytes (abfd, info_ptr); |
f2363ce5 AO |
1610 | /* The abbrev offset is generally a relocation pointing to |
1611 | .debug_abbrev+offset. On RELA targets, we have to find the | |
1612 | relocation and extract the addend to obtain the actual | |
1613 | abbrev_offset, so do it here. */ | |
dc810e39 AM |
1614 | off = info_ptr - stash->sec_info_ptr; |
1615 | abbrev_offset += find_rela_addend (abfd, stash->sec, off, stash->syms); | |
d03ba2a1 | 1616 | info_ptr += offset_size; |
252b5132 RH |
1617 | addr_size = read_1_byte (abfd, info_ptr); |
1618 | info_ptr += 1; | |
1619 | ||
1620 | if (version != 2) | |
1621 | { | |
f46c2da6 | 1622 | (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version); |
252b5132 RH |
1623 | bfd_set_error (bfd_error_bad_value); |
1624 | return 0; | |
1625 | } | |
1626 | ||
1627 | if (addr_size > sizeof (bfd_vma)) | |
1628 | { | |
1629 | (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."), | |
1630 | addr_size, | |
f46c2da6 | 1631 | (unsigned int) sizeof (bfd_vma)); |
252b5132 RH |
1632 | bfd_set_error (bfd_error_bad_value); |
1633 | return 0; | |
1634 | } | |
1635 | ||
ecb651f0 | 1636 | if (addr_size != 2 && addr_size != 4 && addr_size != 8) |
252b5132 | 1637 | { |
f5a3e38a | 1638 | (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size); |
252b5132 RH |
1639 | bfd_set_error (bfd_error_bad_value); |
1640 | return 0; | |
1641 | } | |
1642 | ||
a092b084 | 1643 | /* Read the abbrevs for this compilation unit into a table. */ |
51db3708 | 1644 | abbrevs = read_abbrevs (abfd, abbrev_offset, stash); |
252b5132 RH |
1645 | if (! abbrevs) |
1646 | return 0; | |
1647 | ||
1648 | abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); | |
1649 | info_ptr += bytes_read; | |
1650 | if (! abbrev_number) | |
1651 | { | |
f46c2da6 | 1652 | (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."), |
252b5132 RH |
1653 | abbrev_number); |
1654 | bfd_set_error (bfd_error_bad_value); | |
1655 | return 0; | |
1656 | } | |
1657 | ||
1658 | abbrev = lookup_abbrev (abbrev_number, abbrevs); | |
1659 | if (! abbrev) | |
1660 | { | |
f46c2da6 | 1661 | (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."), |
252b5132 RH |
1662 | abbrev_number); |
1663 | bfd_set_error (bfd_error_bad_value); | |
1664 | return 0; | |
1665 | } | |
98591c73 | 1666 | |
dc810e39 AM |
1667 | amt = sizeof (struct comp_unit); |
1668 | unit = (struct comp_unit*) bfd_zalloc (abfd, amt); | |
252b5132 | 1669 | unit->abfd = abfd; |
98591c73 | 1670 | unit->addr_size = addr_size; |
d03ba2a1 | 1671 | unit->offset_size = offset_size; |
252b5132 RH |
1672 | unit->abbrevs = abbrevs; |
1673 | unit->end_ptr = end_ptr; | |
d03ba2a1 | 1674 | unit->stash = stash; |
252b5132 RH |
1675 | |
1676 | for (i = 0; i < abbrev->num_attrs; ++i) | |
1677 | { | |
1678 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr); | |
1679 | ||
1680 | /* Store the data if it is of an attribute we want to keep in a | |
1681 | partial symbol table. */ | |
1682 | switch (attr.name) | |
1683 | { | |
1684 | case DW_AT_stmt_list: | |
1685 | unit->stmtlist = 1; | |
1686 | unit->line_offset = DW_UNSND (&attr); | |
1687 | break; | |
1688 | ||
1689 | case DW_AT_name: | |
1690 | unit->name = DW_STRING (&attr); | |
1691 | break; | |
1692 | ||
1693 | case DW_AT_low_pc: | |
f623be2b | 1694 | unit->arange.low = DW_ADDR (&attr); |
252b5132 RH |
1695 | break; |
1696 | ||
1697 | case DW_AT_high_pc: | |
f623be2b | 1698 | unit->arange.high = DW_ADDR (&attr); |
252b5132 RH |
1699 | break; |
1700 | ||
1701 | case DW_AT_comp_dir: | |
1702 | { | |
1703 | char* comp_dir = DW_STRING (&attr); | |
1704 | if (comp_dir) | |
1705 | { | |
1706 | /* Irix 6.2 native cc prepends <machine>.: to the compilation | |
1707 | directory, get rid of it. */ | |
1708 | char *cp = (char*) strchr (comp_dir, ':'); | |
1709 | ||
1710 | if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/') | |
1711 | comp_dir = cp + 1; | |
1712 | } | |
1713 | unit->comp_dir = comp_dir; | |
1714 | break; | |
1715 | } | |
1716 | ||
1717 | default: | |
1718 | break; | |
1719 | } | |
1720 | } | |
1721 | ||
1722 | unit->first_child_die_ptr = info_ptr; | |
1723 | return unit; | |
1724 | } | |
1725 | ||
a092b084 | 1726 | /* Return true if UNIT contains the address given by ADDR. */ |
252b5132 RH |
1727 | |
1728 | static boolean | |
1729 | comp_unit_contains_address (unit, addr) | |
1730 | struct comp_unit* unit; | |
1731 | bfd_vma addr; | |
1732 | { | |
f623be2b RH |
1733 | struct arange *arange; |
1734 | ||
1735 | if (unit->error) | |
1736 | return 0; | |
1737 | ||
1738 | arange = &unit->arange; | |
1739 | do | |
1740 | { | |
1741 | if (addr >= arange->low && addr < arange->high) | |
1742 | return 1; | |
1743 | arange = arange->next; | |
1744 | } | |
1745 | while (arange); | |
98591c73 | 1746 | |
f623be2b | 1747 | return 0; |
252b5132 RH |
1748 | } |
1749 | ||
252b5132 RH |
1750 | /* If UNIT contains ADDR, set the output parameters to the values for |
1751 | the line containing ADDR. The output parameters, FILENAME_PTR, | |
1752 | FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects | |
98591c73 | 1753 | to be filled in. |
252b5132 RH |
1754 | |
1755 | Return true of UNIT contains ADDR, and no errors were encountered; | |
1756 | false otherwise. */ | |
1757 | ||
1758 | static boolean | |
e2f6d277 NC |
1759 | comp_unit_find_nearest_line (unit, addr, filename_ptr, functionname_ptr, |
1760 | linenumber_ptr, stash) | |
252b5132 RH |
1761 | struct comp_unit* unit; |
1762 | bfd_vma addr; | |
1763 | const char **filename_ptr; | |
1764 | const char **functionname_ptr; | |
1765 | unsigned int *linenumber_ptr; | |
51db3708 | 1766 | struct dwarf2_debug *stash; |
252b5132 RH |
1767 | { |
1768 | boolean line_p; | |
1769 | boolean func_p; | |
1ee24f27 | 1770 | struct funcinfo *function; |
98591c73 | 1771 | |
252b5132 RH |
1772 | if (unit->error) |
1773 | return false; | |
1774 | ||
1775 | if (! unit->line_table) | |
1776 | { | |
1777 | if (! unit->stmtlist) | |
1778 | { | |
1779 | unit->error = 1; | |
1780 | return false; | |
1781 | } | |
98591c73 | 1782 | |
51db3708 | 1783 | unit->line_table = decode_line_info (unit, stash); |
252b5132 RH |
1784 | |
1785 | if (! unit->line_table) | |
1786 | { | |
1787 | unit->error = 1; | |
1788 | return false; | |
1789 | } | |
98591c73 | 1790 | |
3f5864e1 | 1791 | if (unit->first_child_die_ptr < unit->end_ptr |
e82ce529 | 1792 | && ! scan_unit_for_functions (unit)) |
252b5132 RH |
1793 | { |
1794 | unit->error = 1; | |
1795 | return false; | |
1796 | } | |
1797 | } | |
1798 | ||
1ee24f27 | 1799 | function = NULL; |
e2f6d277 NC |
1800 | func_p = lookup_address_in_function_table (unit->function_table, addr, |
1801 | &function, functionname_ptr); | |
1802 | line_p = lookup_address_in_line_info_table (unit->line_table, addr, | |
1803 | function, filename_ptr, | |
252b5132 | 1804 | linenumber_ptr); |
252b5132 RH |
1805 | return line_p || func_p; |
1806 | } | |
1807 | ||
e2f6d277 NC |
1808 | /* Locate a section in a BFD containing debugging info. The search starts |
1809 | from the section after AFTER_SEC, or from the first section in the BFD if | |
1810 | AFTER_SEC is NULL. The search works by examining the names of the | |
1811 | sections. There are two permissiable names. The first is .debug_info. | |
1812 | This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi. | |
1813 | This is a variation on the .debug_info section which has a checksum | |
1814 | describing the contents appended onto the name. This allows the linker to | |
1815 | identify and discard duplicate debugging sections for different | |
1816 | compilation units. */ | |
a092b084 NC |
1817 | #define DWARF2_DEBUG_INFO ".debug_info" |
1818 | #define GNU_LINKONCE_INFO ".gnu.linkonce.wi." | |
1819 | ||
1820 | static asection * | |
1821 | find_debug_info (abfd, after_sec) | |
1822 | bfd * abfd; | |
1823 | asection * after_sec; | |
1824 | { | |
1825 | asection * msec; | |
1826 | ||
1827 | if (after_sec) | |
1828 | msec = after_sec->next; | |
1829 | else | |
1830 | msec = abfd->sections; | |
1831 | ||
1832 | while (msec) | |
1833 | { | |
1834 | if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0) | |
1835 | return msec; | |
1836 | ||
1837 | if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0) | |
1838 | return msec; | |
1839 | ||
1840 | msec = msec->next; | |
1841 | } | |
1842 | ||
1843 | return NULL; | |
1844 | } | |
1845 | ||
5e38c3b8 MM |
1846 | /* The DWARF2 version of find_nearest line. Return true if the line |
1847 | is found without error. ADDR_SIZE is the number of bytes in the | |
1848 | initial .debug_info length field and in the abbreviation offset. | |
1849 | You may use zero to indicate that the default value should be | |
1850 | used. */ | |
252b5132 RH |
1851 | |
1852 | boolean | |
1853 | _bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset, | |
5e38c3b8 | 1854 | filename_ptr, functionname_ptr, |
e2f6d277 | 1855 | linenumber_ptr, addr_size, pinfo) |
252b5132 RH |
1856 | bfd *abfd; |
1857 | asection *section; | |
f2363ce5 | 1858 | asymbol **symbols; |
252b5132 RH |
1859 | bfd_vma offset; |
1860 | const char **filename_ptr; | |
1861 | const char **functionname_ptr; | |
1862 | unsigned int *linenumber_ptr; | |
5e38c3b8 | 1863 | unsigned int addr_size; |
51db3708 | 1864 | PTR *pinfo; |
252b5132 RH |
1865 | { |
1866 | /* Read each compilation unit from the section .debug_info, and check | |
1867 | to see if it contains the address we are searching for. If yes, | |
1868 | lookup the address, and return the line number info. If no, go | |
98591c73 | 1869 | on to the next compilation unit. |
252b5132 RH |
1870 | |
1871 | We keep a list of all the previously read compilation units, and | |
98591c73 | 1872 | a pointer to the next un-read compilation unit. Check the |
a092b084 | 1873 | previously read units before reading more. */ |
51db3708 | 1874 | struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo; |
252b5132 | 1875 | |
a092b084 | 1876 | /* What address are we looking for? */ |
252b5132 RH |
1877 | bfd_vma addr = offset + section->vma; |
1878 | ||
1879 | struct comp_unit* each; | |
98591c73 | 1880 | |
252b5132 RH |
1881 | *filename_ptr = NULL; |
1882 | *functionname_ptr = NULL; | |
1883 | *linenumber_ptr = 0; | |
1884 | ||
5e38c3b8 MM |
1885 | /* The DWARF2 spec says that the initial length field, and the |
1886 | offset of the abbreviation table, should both be 4-byte values. | |
1887 | However, some compilers do things differently. */ | |
1888 | if (addr_size == 0) | |
1889 | addr_size = 4; | |
1efe4b10 | 1890 | BFD_ASSERT (addr_size == 4 || addr_size == 8); |
98591c73 | 1891 | |
252b5132 RH |
1892 | if (! stash) |
1893 | { | |
dc810e39 | 1894 | bfd_size_type total_size; |
252b5132 | 1895 | asection *msec; |
dc810e39 | 1896 | bfd_size_type amt = sizeof (struct dwarf2_debug); |
a092b084 | 1897 | |
dc810e39 | 1898 | stash = (struct dwarf2_debug*) bfd_zalloc (abfd, amt); |
252b5132 RH |
1899 | if (! stash) |
1900 | return false; | |
252b5132 | 1901 | |
51db3708 | 1902 | *pinfo = (PTR) stash; |
3fde5a36 | 1903 | |
a092b084 NC |
1904 | msec = find_debug_info (abfd, NULL); |
1905 | if (! msec) | |
1906 | /* No dwarf2 info. Note that at this point the stash | |
1907 | has been allocated, but contains zeros, this lets | |
1908 | future calls to this function fail quicker. */ | |
51db3708 | 1909 | return false; |
a092b084 NC |
1910 | |
1911 | /* There can be more than one DWARF2 info section in a BFD these days. | |
e82ce529 | 1912 | Read them all in and produce one large stash. We do this in two |
a092b084 NC |
1913 | passes - in the first pass we just accumulate the section sizes. |
1914 | In the second pass we read in the section's contents. The allows | |
1915 | us to avoid reallocing the data as we add sections to the stash. */ | |
1916 | for (total_size = 0; msec; msec = find_debug_info (abfd, msec)) | |
1917 | total_size += msec->_raw_size; | |
98591c73 | 1918 | |
a092b084 NC |
1919 | stash->info_ptr = (char *) bfd_alloc (abfd, total_size); |
1920 | if (stash->info_ptr == NULL) | |
252b5132 RH |
1921 | return false; |
1922 | ||
a092b084 NC |
1923 | stash->info_ptr_end = stash->info_ptr; |
1924 | ||
1925 | for (msec = find_debug_info (abfd, NULL); | |
1926 | msec; | |
1927 | msec = find_debug_info (abfd, msec)) | |
252b5132 | 1928 | { |
dc810e39 AM |
1929 | bfd_size_type size; |
1930 | bfd_size_type start; | |
a092b084 NC |
1931 | |
1932 | size = msec->_raw_size; | |
1933 | if (size == 0) | |
1934 | continue; | |
1935 | ||
1936 | start = stash->info_ptr_end - stash->info_ptr; | |
1937 | ||
dc810e39 AM |
1938 | if (! bfd_get_section_contents (abfd, msec, stash->info_ptr + start, |
1939 | (bfd_vma) 0, size)) | |
a092b084 NC |
1940 | continue; |
1941 | ||
1942 | stash->info_ptr_end = stash->info_ptr + start + size; | |
252b5132 RH |
1943 | } |
1944 | ||
f2363ce5 AO |
1945 | BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size); |
1946 | ||
1947 | stash->sec = find_debug_info (abfd, NULL); | |
1948 | stash->sec_info_ptr = stash->info_ptr; | |
1949 | stash->syms = symbols; | |
252b5132 | 1950 | } |
a092b084 NC |
1951 | |
1952 | /* FIXME: There is a problem with the contents of the | |
1953 | .debug_info section. The 'low' and 'high' addresses of the | |
1954 | comp_units are computed by relocs against symbols in the | |
1955 | .text segment. We need these addresses in order to determine | |
1956 | the nearest line number, and so we have to resolve the | |
1957 | relocs. There is a similar problem when the .debug_line | |
1958 | section is processed as well (e.g., there may be relocs | |
1959 | against the operand of the DW_LNE_set_address operator). | |
98591c73 | 1960 | |
a092b084 | 1961 | Unfortunately getting hold of the reloc information is hard... |
98591c73 | 1962 | |
a092b084 NC |
1963 | For now, this means that disassembling object files (as |
1964 | opposed to fully executables) does not always work as well as | |
1965 | we would like. */ | |
98591c73 KH |
1966 | |
1967 | /* A null info_ptr indicates that there is no dwarf2 info | |
a092b084 | 1968 | (or that an error occured while setting up the stash). */ |
252b5132 RH |
1969 | if (! stash->info_ptr) |
1970 | return false; | |
1971 | ||
a092b084 | 1972 | /* Check the previously read comp. units first. */ |
252b5132 | 1973 | for (each = stash->all_comp_units; each; each = each->next_unit) |
f623be2b | 1974 | if (comp_unit_contains_address (each, addr)) |
98591c73 | 1975 | return comp_unit_find_nearest_line (each, addr, filename_ptr, |
51db3708 NC |
1976 | functionname_ptr, linenumber_ptr, |
1977 | stash); | |
252b5132 | 1978 | |
a092b084 | 1979 | /* Read each remaining comp. units checking each as they are read. */ |
252b5132 RH |
1980 | while (stash->info_ptr < stash->info_ptr_end) |
1981 | { | |
5e38c3b8 | 1982 | bfd_vma length; |
f623be2b | 1983 | boolean found; |
d03ba2a1 | 1984 | unsigned int offset_size = addr_size; |
252b5132 | 1985 | |
5e38c3b8 | 1986 | if (addr_size == 4) |
d03ba2a1 JJ |
1987 | { |
1988 | length = read_4_bytes (abfd, stash->info_ptr); | |
1989 | if (length == 0xffffffff) | |
1990 | { | |
1991 | offset_size = 8; | |
1992 | length = read_8_bytes (abfd, stash->info_ptr + 4); | |
1993 | stash->info_ptr += 8; | |
1994 | } | |
75936555 NC |
1995 | else if (length == 0) |
1996 | { | |
1997 | /* Handle (non-standard) 64-bit DWARF2 formats. */ | |
1998 | offset_size = 8; | |
1999 | length = read_4_bytes (abfd, stash->info_ptr + 4); | |
2000 | stash->info_ptr += 4; | |
2001 | } | |
d03ba2a1 | 2002 | } |
5e38c3b8 MM |
2003 | else |
2004 | length = read_8_bytes (abfd, stash->info_ptr); | |
2005 | stash->info_ptr += addr_size; | |
252b5132 RH |
2006 | |
2007 | if (length > 0) | |
e82ce529 | 2008 | { |
d03ba2a1 | 2009 | each = parse_comp_unit (abfd, stash, length, offset_size); |
252b5132 RH |
2010 | stash->info_ptr += length; |
2011 | ||
f2363ce5 AO |
2012 | if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr) |
2013 | == stash->sec->_raw_size) | |
2014 | { | |
2015 | stash->sec = find_debug_info (abfd, stash->sec); | |
2016 | stash->sec_info_ptr = stash->info_ptr; | |
2017 | } | |
2018 | ||
252b5132 RH |
2019 | if (each) |
2020 | { | |
2021 | each->next_unit = stash->all_comp_units; | |
2022 | stash->all_comp_units = each; | |
2023 | ||
159002ff RH |
2024 | /* DW_AT_low_pc and DW_AT_high_pc are optional for |
2025 | compilation units. If we don't have them (i.e., | |
2026 | unit->high == 0), we need to consult the line info | |
2027 | table to see if a compilation unit contains the given | |
a092b084 | 2028 | address. */ |
f623be2b | 2029 | if (each->arange.high > 0) |
159002ff RH |
2030 | { |
2031 | if (comp_unit_contains_address (each, addr)) | |
2032 | return comp_unit_find_nearest_line (each, addr, | |
2033 | filename_ptr, | |
2034 | functionname_ptr, | |
51db3708 NC |
2035 | linenumber_ptr, |
2036 | stash); | |
159002ff RH |
2037 | } |
2038 | else | |
2039 | { | |
2040 | found = comp_unit_find_nearest_line (each, addr, | |
2041 | filename_ptr, | |
2042 | functionname_ptr, | |
51db3708 NC |
2043 | linenumber_ptr, |
2044 | stash); | |
159002ff RH |
2045 | if (found) |
2046 | return true; | |
2047 | } | |
252b5132 RH |
2048 | } |
2049 | } | |
2050 | } | |
2051 | ||
2052 | return false; | |
2053 | } |