Commit | Line | Data |
---|---|---|
d02731be GRK |
1 | /* DWARF 2 support. |
2 | Copyright 1994, 1995, 1996, 1997, 1998 Free Software Foundation, Inc. | |
3 | ||
4 | Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions | |
5 | (gavin@cygnus.com). | |
6 | ||
7 | From the dwarf2read.c header: | |
8 | Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology, | |
9 | Inc. with support from Florida State University (under contract | |
10 | with the Ada Joint Program Office), and Silicon Graphics, Inc. | |
11 | Initial contribution by Brent Benson, Harris Computer Systems, Inc., | |
12 | based on Fred Fish's (Cygnus Support) implementation of DWARF 1 | |
13 | support in dwarfread.c | |
14 | ||
15 | This file is part of BFD. | |
16 | ||
17 | This program is free software; you can redistribute it and/or modify | |
18 | it under the terms of the GNU General Public License as published by | |
19 | the Free Software Foundation; either version 2 of the License, or (at | |
20 | your option) any later version. | |
21 | ||
22 | This program is distributed in the hope that it will be useful, but | |
23 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
25 | General Public License for more details. | |
26 | ||
27 | You should have received a copy of the GNU General Public License | |
28 | along with this program; if not, write to the Free Software | |
29 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
30 | ||
31 | #include "bfd.h" | |
32 | #include "sysdep.h" | |
33 | #include "libiberty.h" | |
34 | #include "libbfd.h" | |
35 | #include "elf-bfd.h" | |
36 | #include "elf/dwarf2.h" | |
37 | ||
38 | /* The data in the .debug_line statement prologue looks like this. */ | |
39 | struct line_head | |
40 | { | |
41 | unsigned int total_length; | |
42 | unsigned short version; | |
43 | unsigned int prologue_length; | |
44 | unsigned char minimum_instruction_length; | |
45 | unsigned char default_is_stmt; | |
46 | int line_base; | |
47 | unsigned char line_range; | |
48 | unsigned char opcode_base; | |
49 | unsigned char *standard_opcode_lengths; | |
50 | }; | |
51 | ||
52 | /* Attributes have a name and a value */ | |
53 | struct attribute | |
54 | { | |
55 | enum dwarf_attribute name; | |
56 | enum dwarf_form form; | |
57 | union | |
58 | { | |
59 | char *str; | |
60 | struct dwarf_block *blk; | |
61 | unsigned int unsnd; | |
62 | int snd; | |
63 | bfd_vma addr; | |
64 | } | |
65 | u; | |
66 | }; | |
67 | ||
68 | /* Get at parts of an attribute structure */ | |
69 | ||
70 | #define DW_STRING(attr) ((attr)->u.str) | |
71 | #define DW_UNSND(attr) ((attr)->u.unsnd) | |
72 | #define DW_BLOCK(attr) ((attr)->u.blk) | |
73 | #define DW_SND(attr) ((attr)->u.snd) | |
74 | #define DW_ADDR(attr) ((attr)->u.addr) | |
75 | ||
76 | /* Blocks are a bunch of untyped bytes. */ | |
77 | struct dwarf_block | |
78 | { | |
79 | unsigned int size; | |
80 | char *data; | |
81 | }; | |
82 | ||
83 | ||
84 | struct dwarf2_debug { | |
85 | ||
86 | /* A list of all previously read comp_units. */ | |
87 | struct comp_unit* all_comp_units; | |
88 | ||
89 | /* The next unread compilation unit within the .debug_info section. | |
90 | Zero indicates that the .debug_info section has not been loaded | |
91 | into a buffer yet.*/ | |
92 | char* info_ptr; | |
93 | ||
94 | /* Pointer to the end of the .debug_info section memory buffer. */ | |
95 | char* info_ptr_end; | |
96 | ||
97 | /* Pointer to the .debug_abbrev section loaded into memory. */ | |
98 | char* dwarf_abbrev_buffer; | |
99 | ||
100 | /* Length of the loaded .debug_abbrev section. */ | |
101 | unsigned long dwarf_abbrev_size; | |
102 | }; | |
103 | ||
104 | ||
105 | /* VERBATUM | |
106 | The following function up to the END VERBATUM mark are | |
107 | copied directly from dwarf2read.c. */ | |
108 | ||
109 | /* read dwarf information from a buffer */ | |
110 | ||
111 | static unsigned int | |
112 | read_1_byte (abfd, buf) | |
113 | bfd *abfd; | |
114 | char *buf; | |
115 | { | |
116 | return bfd_get_8 (abfd, (bfd_byte *) buf); | |
117 | } | |
118 | ||
119 | static int | |
120 | read_1_signed_byte (abfd, buf) | |
121 | bfd *abfd; | |
122 | char *buf; | |
123 | { | |
124 | return bfd_get_signed_8 (abfd, (bfd_byte *) buf); | |
125 | } | |
126 | ||
127 | static unsigned int | |
128 | read_2_bytes (abfd, buf) | |
129 | bfd *abfd; | |
130 | char *buf; | |
131 | { | |
132 | return bfd_get_16 (abfd, (bfd_byte *) buf); | |
133 | } | |
134 | ||
135 | #if 0 | |
136 | ||
137 | /* This is not used. */ | |
138 | ||
139 | static int | |
140 | read_2_signed_bytes (abfd, buf) | |
141 | bfd *abfd; | |
142 | char *buf; | |
143 | { | |
144 | return bfd_get_signed_16 (abfd, (bfd_byte *) buf); | |
145 | } | |
146 | ||
147 | #endif | |
148 | ||
149 | static unsigned int | |
150 | read_4_bytes (abfd, buf) | |
151 | bfd *abfd; | |
152 | char *buf; | |
153 | { | |
154 | return bfd_get_32 (abfd, (bfd_byte *) buf); | |
155 | } | |
156 | ||
157 | #if 0 | |
158 | ||
159 | /* This is not used. */ | |
160 | ||
161 | static int | |
162 | read_4_signed_bytes (abfd, buf) | |
163 | bfd *abfd; | |
164 | char *buf; | |
165 | { | |
166 | return bfd_get_signed_32 (abfd, (bfd_byte *) buf); | |
167 | } | |
168 | ||
169 | #endif | |
170 | ||
171 | static unsigned int | |
172 | read_8_bytes (abfd, buf) | |
173 | bfd *abfd; | |
174 | char *buf; | |
175 | { | |
176 | return bfd_get_64 (abfd, (bfd_byte *) buf); | |
177 | } | |
178 | ||
179 | static char * | |
180 | read_n_bytes (abfd, buf, size) | |
181 | bfd * abfd; | |
182 | char *buf; | |
183 | unsigned int size; | |
184 | { | |
185 | /* If the size of a host char is 8 bits, we can return a pointer | |
186 | to the buffer, otherwise we have to copy the data to a buffer | |
187 | allocated on the temporary obstack. */ | |
188 | return buf; | |
189 | } | |
190 | ||
191 | static char * | |
192 | read_string (abfd, buf, bytes_read_ptr) | |
193 | bfd *abfd; | |
194 | char *buf; | |
195 | unsigned int *bytes_read_ptr; | |
196 | { | |
197 | /* If the size of a host char is 8 bits, we can return a pointer | |
198 | to the string, otherwise we have to copy the string to a buffer | |
199 | allocated on the temporary obstack. */ | |
200 | if (*buf == '\0') | |
201 | { | |
202 | *bytes_read_ptr = 1; | |
203 | return NULL; | |
204 | } | |
205 | *bytes_read_ptr = strlen (buf) + 1; | |
206 | return buf; | |
207 | } | |
208 | ||
209 | static unsigned int | |
210 | read_unsigned_leb128 (abfd, buf, bytes_read_ptr) | |
211 | bfd *abfd; | |
212 | char *buf; | |
213 | unsigned int *bytes_read_ptr; | |
214 | { | |
215 | unsigned int result, num_read; | |
216 | int i, shift; | |
217 | unsigned char byte; | |
218 | ||
219 | result = 0; | |
220 | shift = 0; | |
221 | num_read = 0; | |
222 | i = 0; | |
223 | while (1) | |
224 | { | |
225 | byte = bfd_get_8 (abfd, (bfd_byte *) buf); | |
226 | buf++; | |
227 | num_read++; | |
228 | result |= ((byte & 127) << shift); | |
229 | if ((byte & 128) == 0) | |
230 | { | |
231 | break; | |
232 | } | |
233 | shift += 7; | |
234 | } | |
235 | *bytes_read_ptr = num_read; | |
236 | return result; | |
237 | } | |
238 | ||
239 | static int | |
240 | read_signed_leb128 (abfd, buf, bytes_read_ptr) | |
241 | bfd *abfd; | |
242 | char *buf; | |
243 | unsigned int *bytes_read_ptr; | |
244 | { | |
245 | int result; | |
246 | int i, shift, size, num_read; | |
247 | unsigned char byte; | |
248 | ||
249 | result = 0; | |
250 | shift = 0; | |
251 | size = 32; | |
252 | num_read = 0; | |
253 | i = 0; | |
254 | while (1) | |
255 | { | |
256 | byte = bfd_get_8 (abfd, (bfd_byte *) buf); | |
257 | buf++; | |
258 | num_read++; | |
259 | result |= ((byte & 127) << shift); | |
260 | shift += 7; | |
261 | if ((byte & 128) == 0) | |
262 | { | |
263 | break; | |
264 | } | |
265 | } | |
266 | if ((shift < size) && (byte & 0x40)) | |
267 | { | |
268 | result |= -(1 << shift); | |
269 | } | |
270 | *bytes_read_ptr = num_read; | |
271 | return result; | |
272 | } | |
273 | ||
274 | /* END VERBATUM */ | |
275 | ||
276 | ||
277 | static bfd_vma | |
278 | read_address (abfd, buf) | |
279 | bfd *abfd; | |
280 | char *buf; | |
281 | { | |
282 | bfd_vma retval = 0; | |
283 | ||
284 | if (sizeof(retval) == 4) | |
285 | { | |
286 | retval = bfd_get_32 (abfd, (bfd_byte *) buf); | |
287 | } else { | |
288 | retval = bfd_get_64 (abfd, (bfd_byte *) buf); | |
289 | } | |
290 | return retval; | |
291 | } | |
292 | ||
293 | ||
294 | ||
295 | ||
296 | ||
297 | /* This data structure holds the information of an abbrev. */ | |
298 | struct abbrev_info | |
299 | { | |
300 | unsigned int number; /* number identifying abbrev */ | |
301 | enum dwarf_tag tag; /* dwarf tag */ | |
302 | int has_children; /* boolean */ | |
303 | unsigned int num_attrs; /* number of attributes */ | |
304 | struct attr_abbrev *attrs; /* an array of attribute descriptions */ | |
305 | struct abbrev_info *next; /* next in chain */ | |
306 | }; | |
307 | ||
308 | struct attr_abbrev | |
309 | { | |
310 | enum dwarf_attribute name; | |
311 | enum dwarf_form form; | |
312 | }; | |
313 | ||
314 | #ifndef ABBREV_HASH_SIZE | |
315 | #define ABBREV_HASH_SIZE 121 | |
316 | #endif | |
317 | #ifndef ATTR_ALLOC_CHUNK | |
318 | #define ATTR_ALLOC_CHUNK 4 | |
319 | #endif | |
320 | ||
321 | /* Lookup an abbrev_info structure in the abbrev hash table. */ | |
322 | ||
323 | static struct abbrev_info * | |
324 | lookup_abbrev (number,abbrevs) | |
325 | unsigned int number; | |
326 | struct abbrev_info **abbrevs; | |
327 | { | |
328 | unsigned int hash_number; | |
329 | struct abbrev_info *abbrev; | |
330 | ||
331 | hash_number = number % ABBREV_HASH_SIZE; | |
332 | abbrev = abbrevs[hash_number]; | |
333 | ||
334 | while (abbrev) | |
335 | { | |
336 | if (abbrev->number == number) | |
337 | return abbrev; | |
338 | else | |
339 | abbrev = abbrev->next; | |
340 | } | |
341 | return NULL; | |
342 | } | |
343 | ||
344 | /* In DWARF version 2, the description of the debugging information is | |
345 | stored in a separate .debug_abbrev section. Before we read any | |
346 | dies from a section we read in all abbreviations and install them | |
347 | in a hash table. */ | |
348 | ||
349 | static struct abbrev_info** | |
350 | read_abbrevs (abfd, offset) | |
351 | bfd * abfd; | |
352 | unsigned int offset; | |
353 | { | |
354 | struct abbrev_info **abbrevs; | |
355 | char *abbrev_ptr; | |
356 | struct abbrev_info *cur_abbrev; | |
357 | unsigned int abbrev_number, bytes_read, abbrev_name; | |
358 | unsigned int abbrev_form, hash_number; | |
359 | struct dwarf2_debug *stash; | |
360 | ||
361 | stash = elf_tdata(abfd)->dwarf2_find_line_info; | |
362 | ||
363 | if (! stash->dwarf_abbrev_buffer) | |
364 | { | |
365 | asection *msec; | |
366 | ||
367 | msec = bfd_get_section_by_name (abfd, ".debug_abbrev"); | |
368 | if (! msec) | |
369 | { | |
370 | (*_bfd_error_handler) ("Dwarf Error: Can't find .debug_abbrev section."); | |
371 | bfd_set_error (bfd_error_bad_value); | |
372 | return 0; | |
373 | } | |
374 | ||
375 | stash->dwarf_abbrev_size = bfd_get_section_size_before_reloc (msec); | |
376 | stash->dwarf_abbrev_buffer = (unsigned char*) bfd_alloc (abfd, stash->dwarf_abbrev_size); | |
377 | if (! stash->dwarf_abbrev_buffer) | |
378 | return 0; | |
379 | ||
380 | if (! bfd_get_section_contents (abfd, msec, | |
381 | stash->dwarf_abbrev_buffer, 0, | |
382 | stash->dwarf_abbrev_size)) | |
383 | return 0; | |
384 | } | |
385 | ||
386 | if (offset > stash->dwarf_abbrev_size) | |
387 | { | |
388 | (*_bfd_error_handler) ("Dwarf Error: Abbrev offset (%u) bigger than abbrev size (%u).", | |
389 | offset, stash->dwarf_abbrev_size ); | |
390 | bfd_set_error (bfd_error_bad_value); | |
391 | return 0; | |
392 | } | |
393 | ||
394 | abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, sizeof(struct abbrev_info*) * ABBREV_HASH_SIZE); | |
395 | ||
396 | abbrev_ptr = stash->dwarf_abbrev_buffer + offset; | |
397 | abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
398 | abbrev_ptr += bytes_read; | |
399 | ||
400 | /* loop until we reach an abbrev number of 0 */ | |
401 | while (abbrev_number) | |
402 | { | |
403 | cur_abbrev = (struct abbrev_info*)bfd_zalloc (abfd, sizeof (struct abbrev_info)); | |
404 | ||
405 | /* read in abbrev header */ | |
406 | cur_abbrev->number = abbrev_number; | |
407 | cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
408 | abbrev_ptr += bytes_read; | |
409 | cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr); | |
410 | abbrev_ptr += 1; | |
411 | ||
412 | /* now read in declarations */ | |
413 | abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
414 | abbrev_ptr += bytes_read; | |
415 | abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
416 | abbrev_ptr += bytes_read; | |
417 | while (abbrev_name) | |
418 | { | |
419 | if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0) | |
420 | { | |
421 | cur_abbrev->attrs = (struct attr_abbrev *) | |
422 | bfd_realloc (cur_abbrev->attrs, | |
423 | (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK) | |
424 | * sizeof (struct attr_abbrev)); | |
425 | if (! cur_abbrev->attrs) | |
426 | return 0; | |
427 | } | |
428 | cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name; | |
429 | cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form; | |
430 | abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
431 | abbrev_ptr += bytes_read; | |
432 | abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
433 | abbrev_ptr += bytes_read; | |
434 | } | |
435 | ||
436 | hash_number = abbrev_number % ABBREV_HASH_SIZE; | |
437 | cur_abbrev->next = abbrevs[hash_number]; | |
438 | abbrevs[hash_number] = cur_abbrev; | |
439 | ||
440 | /* Get next abbreviation. | |
441 | Under Irix6 the abbreviations for a compilation unit are not | |
442 | always properly terminated with an abbrev number of 0. | |
443 | Exit loop if we encounter an abbreviation which we have | |
444 | already read (which means we are about to read the abbreviations | |
445 | for the next compile unit) or if the end of the abbreviation | |
446 | table is reached. */ | |
447 | if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer) | |
448 | >= stash->dwarf_abbrev_size) | |
449 | break; | |
450 | abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read); | |
451 | abbrev_ptr += bytes_read; | |
452 | if (lookup_abbrev (abbrev_number,abbrevs) != NULL) | |
453 | break; | |
454 | } | |
455 | ||
456 | return abbrevs; | |
457 | } | |
458 | ||
459 | /* Read an attribute described by an abbreviated attribute. */ | |
460 | ||
461 | static char * | |
462 | read_attribute (attr, abbrev, abfd, info_ptr) | |
463 | struct attribute *attr; | |
464 | struct attr_abbrev *abbrev; | |
465 | bfd *abfd; | |
466 | char *info_ptr; | |
467 | { | |
468 | unsigned int bytes_read; | |
469 | struct dwarf_block *blk; | |
470 | ||
471 | attr->name = abbrev->name; | |
472 | attr->form = abbrev->form; | |
473 | switch (abbrev->form) | |
474 | { | |
475 | case DW_FORM_addr: | |
476 | case DW_FORM_ref_addr: | |
477 | DW_ADDR (attr) = read_address (abfd, info_ptr); | |
478 | info_ptr += sizeof(bfd_vma); | |
479 | break; | |
480 | case DW_FORM_block2: | |
481 | blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block)); | |
482 | blk->size = read_2_bytes (abfd, info_ptr); | |
483 | info_ptr += 2; | |
484 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); | |
485 | info_ptr += blk->size; | |
486 | DW_BLOCK (attr) = blk; | |
487 | break; | |
488 | case DW_FORM_block4: | |
489 | blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block)); | |
490 | blk->size = read_4_bytes (abfd, info_ptr); | |
491 | info_ptr += 4; | |
492 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); | |
493 | info_ptr += blk->size; | |
494 | DW_BLOCK (attr) = blk; | |
495 | break; | |
496 | case DW_FORM_data2: | |
497 | DW_UNSND (attr) = read_2_bytes (abfd, info_ptr); | |
498 | info_ptr += 2; | |
499 | break; | |
500 | case DW_FORM_data4: | |
501 | DW_UNSND (attr) = read_4_bytes (abfd, info_ptr); | |
502 | info_ptr += 4; | |
503 | break; | |
504 | case DW_FORM_data8: | |
505 | DW_UNSND (attr) = read_8_bytes (abfd, info_ptr); | |
506 | info_ptr += 8; | |
507 | break; | |
508 | case DW_FORM_string: | |
509 | DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read); | |
510 | info_ptr += bytes_read; | |
511 | break; | |
512 | case DW_FORM_block: | |
513 | blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block)); | |
514 | blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); | |
515 | info_ptr += bytes_read; | |
516 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); | |
517 | info_ptr += blk->size; | |
518 | DW_BLOCK (attr) = blk; | |
519 | break; | |
520 | case DW_FORM_block1: | |
521 | blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block)); | |
522 | blk->size = read_1_byte (abfd, info_ptr); | |
523 | info_ptr += 1; | |
524 | blk->data = read_n_bytes (abfd, info_ptr, blk->size); | |
525 | info_ptr += blk->size; | |
526 | DW_BLOCK (attr) = blk; | |
527 | break; | |
528 | case DW_FORM_data1: | |
529 | DW_UNSND (attr) = read_1_byte (abfd, info_ptr); | |
530 | info_ptr += 1; | |
531 | break; | |
532 | case DW_FORM_flag: | |
533 | DW_UNSND (attr) = read_1_byte (abfd, info_ptr); | |
534 | info_ptr += 1; | |
535 | break; | |
536 | case DW_FORM_sdata: | |
537 | DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read); | |
538 | info_ptr += bytes_read; | |
539 | break; | |
540 | case DW_FORM_udata: | |
541 | DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); | |
542 | info_ptr += bytes_read; | |
543 | break; | |
544 | case DW_FORM_ref1: | |
545 | DW_UNSND (attr) = read_1_byte (abfd, info_ptr); | |
546 | info_ptr += 1; | |
547 | break; | |
548 | case DW_FORM_ref2: | |
549 | DW_UNSND (attr) = read_2_bytes (abfd, info_ptr); | |
550 | info_ptr += 2; | |
551 | break; | |
552 | case DW_FORM_ref4: | |
553 | DW_UNSND (attr) = read_4_bytes (abfd, info_ptr); | |
554 | info_ptr += 4; | |
555 | break; | |
556 | case DW_FORM_ref_udata: | |
557 | DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); | |
558 | info_ptr += bytes_read; | |
559 | break; | |
560 | case DW_FORM_strp: | |
561 | case DW_FORM_indirect: | |
562 | default: | |
563 | (*_bfd_error_handler) ("Dwarf Error: Invalid or unhandled FORM value: %d.", | |
564 | abbrev->form); | |
565 | bfd_set_error (bfd_error_bad_value); | |
566 | } | |
567 | return info_ptr; | |
568 | } | |
569 | ||
570 | ||
571 | /* Source line information table routines. */ | |
572 | ||
573 | #define FILE_ALLOC_CHUNK 5 | |
574 | #define DIR_ALLOC_CHUNK 5 | |
575 | ||
576 | struct line_info { | |
577 | struct line_info* prev_line; | |
578 | ||
579 | bfd_vma address; | |
580 | char* filename; | |
581 | unsigned int line; | |
582 | unsigned int column; | |
583 | }; | |
584 | ||
585 | struct fileinfo { | |
586 | char *name; | |
587 | unsigned int dir; | |
588 | unsigned int time; | |
589 | unsigned int size; | |
590 | }; | |
591 | ||
592 | struct line_info_table { | |
593 | bfd* abfd; | |
594 | ||
595 | unsigned int num_files; | |
596 | unsigned int num_dirs; | |
597 | ||
598 | char* comp_dir; | |
599 | char** dirs; | |
600 | struct fileinfo* files; | |
601 | struct line_info* last_line; | |
602 | }; | |
603 | ||
604 | static void | |
605 | add_line_info (table, address, filename, line, column) | |
606 | struct line_info_table* table; | |
607 | bfd_vma address; | |
608 | char* filename; | |
609 | unsigned int line; | |
610 | unsigned int column; | |
611 | { | |
612 | struct line_info* info = (struct line_info*) | |
613 | bfd_alloc (table->abfd, sizeof (struct line_info)); | |
614 | ||
615 | info->prev_line = table->last_line; | |
616 | table->last_line = info; | |
617 | ||
618 | info->address = address; | |
619 | info->filename = filename; | |
620 | info->line = line; | |
621 | info->column = column; | |
622 | } | |
623 | ||
624 | static char* | |
625 | concat_filename (table, file) | |
626 | struct line_info_table* table; | |
627 | unsigned int file; | |
628 | { | |
629 | char* filename = table->files[file - 1].name; | |
630 | if (*filename == '/') | |
631 | return filename; | |
632 | ||
633 | else | |
634 | { | |
635 | char* dirname = (table->files[file - 1].dir | |
636 | ? table->dirs[table->files[file - 1].dir - 1] | |
637 | : table->comp_dir); | |
638 | return (char*) concat (dirname, "/", filename, NULL); | |
639 | } | |
640 | } | |
641 | ||
642 | /* Decode the line number information for the compilation unit whose | |
643 | line number info is at OFFSET in the .debug_line section. | |
644 | The compilation directory of the file is passed in COMP_DIR. */ | |
645 | ||
646 | static struct line_info_table* | |
647 | decode_line_info (abfd, offset, comp_dir) | |
648 | bfd *abfd; | |
649 | unsigned int offset; | |
650 | char *comp_dir; | |
651 | { | |
652 | static char* dwarf_line_buffer = 0; | |
653 | ||
654 | struct line_info_table* table; | |
655 | ||
656 | char *line_ptr; | |
657 | char *line_end; | |
658 | struct line_head lh; | |
659 | unsigned int i, bytes_read; | |
660 | char *cur_file, *cur_dir; | |
661 | unsigned char op_code, extended_op, adj_opcode; | |
662 | ||
663 | if (! dwarf_line_buffer) | |
664 | { | |
665 | asection *msec; | |
666 | unsigned long size; | |
667 | ||
668 | msec = bfd_get_section_by_name (abfd, ".debug_line"); | |
669 | if (! msec) | |
670 | { | |
671 | (*_bfd_error_handler) ("Dwarf Error: Can't find .debug_line section."); | |
672 | bfd_set_error (bfd_error_bad_value); | |
673 | return 0; | |
674 | } | |
675 | ||
676 | size = bfd_get_section_size_before_reloc (msec); | |
677 | dwarf_line_buffer = (unsigned char*) bfd_alloc (abfd, size); | |
678 | if (! dwarf_line_buffer) | |
679 | return 0; | |
680 | ||
681 | if (! bfd_get_section_contents (abfd, msec, | |
682 | dwarf_line_buffer, 0, | |
683 | size)) | |
684 | return 0; | |
685 | } | |
686 | ||
687 | table = (struct line_info_table*) bfd_alloc (abfd, | |
688 | sizeof (struct line_info_table)); | |
689 | table->abfd = abfd; | |
690 | table->comp_dir = comp_dir; | |
691 | ||
692 | table->num_files = 0; | |
693 | table->files = NULL; | |
694 | ||
695 | table->num_dirs = 0; | |
696 | table->dirs = NULL; | |
697 | ||
698 | line_ptr = dwarf_line_buffer + offset; | |
699 | ||
700 | /* read in the prologue */ | |
701 | lh.total_length = read_4_bytes (abfd, line_ptr); | |
702 | line_ptr += 4; | |
703 | line_end = line_ptr + lh.total_length; | |
704 | lh.version = read_2_bytes (abfd, line_ptr); | |
705 | line_ptr += 2; | |
706 | lh.prologue_length = read_4_bytes (abfd, line_ptr); | |
707 | line_ptr += 4; | |
708 | lh.minimum_instruction_length = read_1_byte (abfd, line_ptr); | |
709 | line_ptr += 1; | |
710 | lh.default_is_stmt = read_1_byte (abfd, line_ptr); | |
711 | line_ptr += 1; | |
712 | lh.line_base = read_1_signed_byte (abfd, line_ptr); | |
713 | line_ptr += 1; | |
714 | lh.line_range = read_1_byte (abfd, line_ptr); | |
715 | line_ptr += 1; | |
716 | lh.opcode_base = read_1_byte (abfd, line_ptr); | |
717 | line_ptr += 1; | |
718 | lh.standard_opcode_lengths = (unsigned char *) | |
719 | bfd_alloc (abfd, lh.opcode_base * sizeof (unsigned char)); | |
720 | ||
721 | lh.standard_opcode_lengths[0] = 1; | |
722 | for (i = 1; i < lh.opcode_base; ++i) | |
723 | { | |
724 | lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr); | |
725 | line_ptr += 1; | |
726 | } | |
727 | ||
728 | /* Read directory table */ | |
729 | while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL) | |
730 | { | |
731 | line_ptr += bytes_read; | |
732 | if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0) | |
733 | { | |
734 | table->dirs = (char **) | |
735 | bfd_realloc (table->dirs, | |
736 | (table->num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *)); | |
737 | if (! table->dirs) | |
738 | return 0; | |
739 | } | |
740 | table->dirs[table->num_dirs++] = cur_dir; | |
741 | } | |
742 | line_ptr += bytes_read; | |
743 | ||
744 | /* Read file name table */ | |
745 | while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL) | |
746 | { | |
747 | line_ptr += bytes_read; | |
748 | if ((table->num_files % FILE_ALLOC_CHUNK) == 0) | |
749 | { | |
750 | table->files = (struct fileinfo *) | |
751 | bfd_realloc (table->files, | |
752 | (table->num_files + FILE_ALLOC_CHUNK) | |
753 | * sizeof (struct fileinfo)); | |
754 | if (! table->files) | |
755 | return 0; | |
756 | } | |
757 | table->files[table->num_files].name = cur_file; | |
758 | table->files[table->num_files].dir = | |
759 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
760 | line_ptr += bytes_read; | |
761 | table->files[table->num_files].time = | |
762 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
763 | line_ptr += bytes_read; | |
764 | table->files[table->num_files].size = | |
765 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
766 | line_ptr += bytes_read; | |
767 | table->num_files++; | |
768 | } | |
769 | line_ptr += bytes_read; | |
770 | ||
771 | /* Read the statement sequences until there's nothing left. */ | |
772 | while (line_ptr < line_end) | |
773 | { | |
774 | /* state machine registers */ | |
775 | bfd_vma address = 0; | |
776 | char* filename = concat_filename (table, 1); | |
777 | unsigned int line = 1; | |
778 | unsigned int column = 0; | |
779 | int is_stmt = lh.default_is_stmt; | |
780 | int basic_block = 0; | |
781 | int end_sequence = 0; | |
782 | ||
783 | /* Decode the table. */ | |
784 | while (! end_sequence) | |
785 | { | |
786 | op_code = read_1_byte (abfd, line_ptr); | |
787 | line_ptr += 1; | |
788 | switch (op_code) | |
789 | { | |
790 | case DW_LNS_extended_op: | |
791 | line_ptr += 1; /* ignore length */ | |
792 | extended_op = read_1_byte (abfd, line_ptr); | |
793 | line_ptr += 1; | |
794 | switch (extended_op) | |
795 | { | |
796 | case DW_LNE_end_sequence: | |
797 | end_sequence = 1; | |
798 | add_line_info (table, address, filename, line, column); | |
799 | break; | |
800 | case DW_LNE_set_address: | |
801 | address = read_address (abfd, line_ptr); | |
802 | address &= 0xffffffff; | |
803 | line_ptr += sizeof (bfd_vma); | |
804 | break; | |
805 | case DW_LNE_define_file: | |
806 | cur_file = read_string (abfd, line_ptr, &bytes_read); | |
807 | line_ptr += bytes_read; | |
808 | if ((table->num_files % FILE_ALLOC_CHUNK) == 0) | |
809 | { | |
810 | table->files = (struct fileinfo *) | |
811 | bfd_realloc (table->files, | |
812 | (table->num_files + FILE_ALLOC_CHUNK) | |
813 | * sizeof (struct fileinfo)); | |
814 | if (! table->files) | |
815 | return 0; | |
816 | } | |
817 | table->files[table->num_files].name = cur_file; | |
818 | table->files[table->num_files].dir = | |
819 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
820 | line_ptr += bytes_read; | |
821 | table->files[table->num_files].time = | |
822 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
823 | line_ptr += bytes_read; | |
824 | table->files[table->num_files].size = | |
825 | read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
826 | line_ptr += bytes_read; | |
827 | table->num_files++; | |
828 | break; | |
829 | default: | |
830 | (*_bfd_error_handler) ("Dwarf Error: mangled line number section."); | |
831 | bfd_set_error (bfd_error_bad_value); | |
832 | return 0; | |
833 | } | |
834 | break; | |
835 | case DW_LNS_copy: | |
836 | add_line_info (table, address, filename, line, column); | |
837 | basic_block = 0; | |
838 | break; | |
839 | case DW_LNS_advance_pc: | |
840 | address += lh.minimum_instruction_length | |
841 | * read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
842 | line_ptr += bytes_read; | |
843 | break; | |
844 | case DW_LNS_advance_line: | |
845 | line += read_signed_leb128 (abfd, line_ptr, &bytes_read); | |
846 | line_ptr += bytes_read; | |
847 | break; | |
848 | case DW_LNS_set_file: | |
849 | { | |
850 | unsigned int file; | |
851 | ||
852 | /* The file and directory tables are 0 based, the references | |
853 | are 1 based. */ | |
854 | file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
855 | line_ptr += bytes_read; | |
856 | filename = concat_filename (table, file); | |
857 | break; | |
858 | } | |
859 | case DW_LNS_set_column: | |
860 | column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read); | |
861 | line_ptr += bytes_read; | |
862 | break; | |
863 | case DW_LNS_negate_stmt: | |
864 | is_stmt = (!is_stmt); | |
865 | break; | |
866 | case DW_LNS_set_basic_block: | |
867 | basic_block = 1; | |
868 | break; | |
869 | case DW_LNS_const_add_pc: | |
870 | address += (255 - lh.opcode_base) / lh.line_range; | |
871 | break; | |
872 | case DW_LNS_fixed_advance_pc: | |
873 | address += read_2_bytes (abfd, line_ptr); | |
874 | line_ptr += 2; | |
875 | break; | |
876 | default: /* special operand */ | |
877 | adj_opcode = op_code - lh.opcode_base; | |
878 | address += (adj_opcode / lh.line_range) | |
879 | * lh.minimum_instruction_length; | |
880 | line += lh.line_base + (adj_opcode % lh.line_range); | |
881 | /* append row to matrix using current values */ | |
882 | add_line_info (table, address, filename, line, column); | |
883 | basic_block = 1; | |
884 | } | |
885 | } | |
886 | } | |
887 | ||
888 | return table; | |
889 | } | |
890 | ||
891 | ||
892 | /* If ADDR is within TABLE set the output parameters and return true, | |
893 | otherwise return false. The output parameters, FILENAME_PTR and | |
894 | LINENUMBER_PTR, are pointers to the objects to be filled in. */ | |
895 | ||
896 | static boolean | |
897 | lookup_address_in_line_info_table (table, | |
898 | addr, | |
899 | filename_ptr, | |
900 | linenumber_ptr) | |
901 | struct line_info_table* table; | |
902 | bfd_vma addr; | |
903 | const char **filename_ptr; | |
904 | unsigned int *linenumber_ptr; | |
905 | { | |
906 | struct line_info* each_line; | |
907 | struct line_info* next_line; | |
908 | ||
909 | for (next_line = 0, each_line = table->last_line; | |
910 | each_line; | |
911 | next_line = each_line, each_line = each_line->prev_line) | |
912 | { | |
913 | if (addr >= each_line->address | |
914 | && (next_line == 0 | |
915 | || addr < next_line->address)) | |
916 | { | |
917 | *filename_ptr = each_line->filename; | |
918 | *linenumber_ptr = each_line->line; | |
919 | return true; | |
920 | } | |
921 | } | |
922 | ||
923 | return false; | |
924 | } | |
925 | ||
926 | ||
927 | ||
928 | ||
929 | /* Function table functions. */ | |
930 | ||
931 | struct funcinfo { | |
932 | struct funcinfo *prev_func; | |
933 | ||
934 | char* name; | |
935 | bfd_vma low; | |
936 | bfd_vma high; | |
937 | }; | |
938 | ||
939 | ||
940 | /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */ | |
941 | ||
942 | static boolean | |
943 | lookup_address_in_function_table (table, | |
944 | addr, | |
945 | functionname_ptr) | |
946 | struct funcinfo* table; | |
947 | bfd_vma addr; | |
948 | const char **functionname_ptr; | |
949 | { | |
950 | struct funcinfo* each_func; | |
951 | ||
952 | for (each_func = table; | |
953 | each_func; | |
954 | each_func = each_func->prev_func) | |
955 | { | |
956 | if (addr >= (each_func->low & 0xffffffff) | |
957 | && addr < (each_func->high & 0xffffffff)) | |
958 | { | |
959 | *functionname_ptr = each_func->name; | |
960 | return true; | |
961 | } | |
962 | } | |
963 | ||
964 | return false; | |
965 | } | |
966 | ||
967 | ||
968 | ||
969 | ||
970 | /* DWARF2 Compilation unit functions. */ | |
971 | ||
972 | ||
973 | /* A minimal decoding of DWARF2 compilation units. We only decode | |
974 | what's needed to get to the line number information. */ | |
975 | ||
976 | struct comp_unit { | |
977 | ||
978 | /* Chain the previously read compilation units. */ | |
979 | struct comp_unit* next_unit; | |
980 | ||
981 | /* Keep the bdf convenient (for memory allocation). */ | |
982 | bfd* abfd; | |
983 | ||
984 | /* The lowest and higest addresses contained in this compilation | |
985 | unit as specified in the compilation unit header. */ | |
986 | bfd_vma low; | |
987 | bfd_vma high; | |
988 | ||
989 | /* The DW_AT_name attribute (for error messages). */ | |
990 | char* name; | |
991 | ||
992 | /* The abbrev hash table. */ | |
993 | struct abbrev_info** abbrevs; | |
994 | ||
995 | /* Note that an error was found by comp_unit_find_nearest_line. */ | |
996 | int error; | |
997 | ||
998 | /* The DW_AT_comp_dir attribute */ | |
999 | char* comp_dir; | |
1000 | ||
1001 | /* True if there is a line number table associated with this comp. unit. */ | |
1002 | int stmtlist; | |
1003 | ||
1004 | /* The offset into .debug_line of the line number table. */ | |
1005 | unsigned long line_offset; | |
1006 | ||
1007 | /* Pointer to the first child die for the comp unit. */ | |
1008 | char *first_child_die_ptr; | |
1009 | ||
1010 | /* The end of the comp unit. */ | |
1011 | char *end_ptr; | |
1012 | ||
1013 | /* The decoded line number, NULL if not yet decoded. */ | |
1014 | struct line_info_table* line_table; | |
1015 | ||
1016 | /* A list of the functions found in this comp. unit. */ | |
1017 | struct funcinfo* function_table; | |
1018 | ||
1019 | }; | |
1020 | ||
1021 | ||
1022 | ||
1023 | /* Scan over each die in a comp. unit looking for functions to add | |
1024 | to the function table. */ | |
1025 | ||
1026 | static boolean | |
1027 | scan_unit_for_functions (unit) | |
1028 | struct comp_unit *unit; | |
1029 | { | |
1030 | bfd *abfd = unit->abfd; | |
1031 | char *info_ptr = unit->first_child_die_ptr; | |
1032 | int nesting_level = 1; | |
1033 | ||
1034 | while (nesting_level) | |
1035 | { | |
1036 | unsigned int abbrev_number, bytes_read, i; | |
1037 | struct abbrev_info *abbrev; | |
1038 | struct attribute attr; | |
1039 | struct funcinfo *func; | |
1040 | char* name = 0; | |
1041 | ||
1042 | abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); | |
1043 | info_ptr += bytes_read; | |
1044 | ||
1045 | if (! abbrev_number) | |
1046 | { | |
1047 | nesting_level--; | |
1048 | continue; | |
1049 | } | |
1050 | ||
1051 | abbrev = lookup_abbrev (abbrev_number,unit->abbrevs); | |
1052 | if (! abbrev) | |
1053 | { | |
1054 | (*_bfd_error_handler) ("Dwarf Error: Could not find abbrev number %d.", | |
1055 | abbrev_number); | |
1056 | bfd_set_error (bfd_error_bad_value); | |
1057 | return false; | |
1058 | } | |
1059 | ||
1060 | if (abbrev->tag == DW_TAG_subprogram) | |
1061 | { | |
1062 | func = (struct funcinfo*) bfd_zalloc (abfd, sizeof (struct funcinfo)); | |
1063 | func->prev_func = unit->function_table; | |
1064 | unit->function_table = func; | |
1065 | } | |
1066 | else | |
1067 | func = NULL; | |
1068 | ||
1069 | for (i = 0; i < abbrev->num_attrs; ++i) | |
1070 | { | |
1071 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr); | |
1072 | ||
1073 | if (func) | |
1074 | { | |
1075 | switch (attr.name) | |
1076 | { | |
1077 | case DW_AT_name: | |
1078 | ||
1079 | name = DW_STRING (&attr); | |
1080 | ||
1081 | /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */ | |
1082 | if (func->name == NULL) | |
1083 | func->name = DW_STRING (&attr); | |
1084 | break; | |
1085 | ||
1086 | case DW_AT_MIPS_linkage_name: | |
1087 | func->name = DW_STRING (&attr); | |
1088 | break; | |
1089 | ||
1090 | case DW_AT_low_pc: | |
1091 | func->low = DW_ADDR (&attr); | |
1092 | break; | |
1093 | ||
1094 | case DW_AT_high_pc: | |
1095 | func->high = DW_ADDR (&attr); | |
1096 | break; | |
1097 | ||
1098 | default: | |
1099 | break; | |
1100 | } | |
1101 | } | |
1102 | else | |
1103 | { | |
1104 | switch (attr.name) | |
1105 | { | |
1106 | case DW_AT_name: | |
1107 | name = DW_STRING (&attr); | |
1108 | break; | |
1109 | ||
1110 | default: | |
1111 | break; | |
1112 | } | |
1113 | } | |
1114 | } | |
1115 | ||
1116 | if (abbrev->has_children) | |
1117 | nesting_level++; | |
1118 | } | |
1119 | ||
1120 | return true; | |
1121 | } | |
1122 | ||
1123 | ||
1124 | ||
1125 | ||
1126 | ||
1127 | ||
1128 | /* Parse a DWARF2 compilation unit starting at INFO_PTR. This includes | |
1129 | the compilation unit header that proceeds the DIE's, but does not | |
1130 | include the length field that preceeds each compilation unit header. | |
1131 | END_PTR points one past the end of this comp unit. | |
1132 | ||
1133 | This routine does not read the whole compilation unit; only enough | |
1134 | to get to the line number information for the compilation unit. | |
1135 | */ | |
1136 | ||
1137 | static struct comp_unit* | |
1138 | parse_comp_unit (abfd, info_ptr, end_ptr) | |
1139 | bfd* abfd; | |
1140 | char* info_ptr; | |
1141 | char* end_ptr; | |
1142 | { | |
1143 | struct comp_unit* unit; | |
1144 | ||
1145 | unsigned short version; | |
1146 | unsigned int abbrev_offset; | |
1147 | unsigned char addr_size; | |
1148 | struct abbrev_info** abbrevs; | |
1149 | ||
1150 | unsigned int abbrev_number, bytes_read, i; | |
1151 | struct abbrev_info *abbrev; | |
1152 | struct attribute attr; | |
1153 | ||
1154 | version = read_2_bytes (abfd, info_ptr); | |
1155 | info_ptr += 2; | |
1156 | abbrev_offset = read_4_bytes (abfd, info_ptr); | |
1157 | info_ptr += 4; | |
1158 | addr_size = read_1_byte (abfd, info_ptr); | |
1159 | info_ptr += 1; | |
1160 | ||
1161 | if (version != 2) | |
1162 | { | |
1163 | (*_bfd_error_handler) ("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information.", version ); | |
1164 | bfd_set_error (bfd_error_bad_value); | |
1165 | return 0; | |
1166 | } | |
1167 | ||
1168 | if (addr_size != sizeof (bfd_vma)) | |
1169 | { | |
1170 | (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this readers only handles address size '%u'.", | |
1171 | addr_size, | |
1172 | sizeof (bfd_vma)); | |
1173 | bfd_set_error (bfd_error_bad_value); | |
1174 | return 0; | |
1175 | } | |
1176 | ||
1177 | /* Read the abbrevs for this compilation unit into a table */ | |
1178 | abbrevs = read_abbrevs (abfd, abbrev_offset); | |
1179 | if (! abbrevs) | |
1180 | return 0; | |
1181 | ||
1182 | abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read); | |
1183 | info_ptr += bytes_read; | |
1184 | if (! abbrev_number) | |
1185 | { | |
1186 | (*_bfd_error_handler) ("Dwarf Error: Bad abbrev number: %d.", | |
1187 | abbrev_number); | |
1188 | bfd_set_error (bfd_error_bad_value); | |
1189 | return 0; | |
1190 | } | |
1191 | ||
1192 | abbrev = lookup_abbrev (abbrev_number, abbrevs); | |
1193 | if (! abbrev) | |
1194 | { | |
1195 | (*_bfd_error_handler) ("Dwarf Error: Could not find abbrev number %d.", | |
1196 | abbrev_number); | |
1197 | bfd_set_error (bfd_error_bad_value); | |
1198 | return 0; | |
1199 | } | |
1200 | ||
1201 | unit = (struct comp_unit*) bfd_zalloc (abfd, sizeof (struct comp_unit)); | |
1202 | unit->abfd = abfd; | |
1203 | unit->abbrevs = abbrevs; | |
1204 | unit->end_ptr = end_ptr; | |
1205 | ||
1206 | for (i = 0; i < abbrev->num_attrs; ++i) | |
1207 | { | |
1208 | info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr); | |
1209 | ||
1210 | /* Store the data if it is of an attribute we want to keep in a | |
1211 | partial symbol table. */ | |
1212 | switch (attr.name) | |
1213 | { | |
1214 | case DW_AT_stmt_list: | |
1215 | unit->stmtlist = 1; | |
1216 | unit->line_offset = DW_UNSND (&attr); | |
1217 | break; | |
1218 | ||
1219 | case DW_AT_name: | |
1220 | unit->name = DW_STRING (&attr); | |
1221 | break; | |
1222 | ||
1223 | case DW_AT_low_pc: | |
1224 | unit->low = DW_ADDR (&attr); | |
1225 | break; | |
1226 | ||
1227 | case DW_AT_high_pc: | |
1228 | unit->high = DW_ADDR (&attr); | |
1229 | break; | |
1230 | ||
1231 | case DW_AT_comp_dir: | |
1232 | { | |
1233 | char* comp_dir = DW_STRING (&attr); | |
1234 | if (comp_dir) | |
1235 | { | |
1236 | /* Irix 6.2 native cc prepends <machine>.: to the compilation | |
1237 | directory, get rid of it. */ | |
1238 | char *cp = (char*) strchr (comp_dir, ':'); | |
1239 | ||
1240 | if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/') | |
1241 | comp_dir = cp + 1; | |
1242 | } | |
1243 | unit->comp_dir = comp_dir; | |
1244 | break; | |
1245 | } | |
1246 | ||
1247 | default: | |
1248 | break; | |
1249 | } | |
1250 | } | |
1251 | ||
1252 | unit->first_child_die_ptr = info_ptr; | |
1253 | return unit; | |
1254 | } | |
1255 | ||
1256 | ||
1257 | ||
1258 | ||
1259 | ||
1260 | /* Return true if UNIT contains the address given by ADDR. */ | |
1261 | ||
1262 | static boolean | |
1263 | comp_unit_contains_address (unit, addr) | |
1264 | struct comp_unit* unit; | |
1265 | bfd_vma addr; | |
1266 | { | |
1267 | return ! unit->error | |
1268 | && ( addr >= (unit->low & 0xffffffff) | |
1269 | && addr <= (unit->high & 0xffffffff)); | |
1270 | } | |
1271 | ||
1272 | ||
1273 | /* If UNIT contains ADDR, set the output parameters to the values for | |
1274 | the line containing ADDR. The output parameters, FILENAME_PTR, | |
1275 | FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects | |
1276 | to be filled in. | |
1277 | ||
1278 | Return true of UNIT contains ADDR, and no errors were encountered; | |
1279 | false otherwise. */ | |
1280 | ||
1281 | static boolean | |
1282 | comp_unit_find_nearest_line (unit, addr, | |
1283 | filename_ptr, functionname_ptr, linenumber_ptr) | |
1284 | struct comp_unit* unit; | |
1285 | bfd_vma addr; | |
1286 | const char **filename_ptr; | |
1287 | const char **functionname_ptr; | |
1288 | unsigned int *linenumber_ptr; | |
1289 | { | |
1290 | boolean line_p; | |
1291 | boolean func_p; | |
1292 | ||
1293 | if (unit->error) | |
1294 | return false; | |
1295 | ||
1296 | if (! unit->line_table) | |
1297 | { | |
1298 | if (! unit->stmtlist) | |
1299 | { | |
1300 | unit->error = 1; | |
1301 | return false; | |
1302 | } | |
1303 | ||
1304 | unit->line_table = decode_line_info (unit->abfd, | |
1305 | unit->line_offset, | |
1306 | unit->comp_dir); | |
1307 | ||
1308 | if (! unit->line_table) | |
1309 | { | |
1310 | unit->error = 1; | |
1311 | return false; | |
1312 | } | |
1313 | ||
1314 | if (! scan_unit_for_functions (unit)) | |
1315 | { | |
1316 | unit->error = 1; | |
1317 | return false; | |
1318 | } | |
1319 | } | |
1320 | ||
1321 | line_p = lookup_address_in_line_info_table (unit->line_table, | |
1322 | addr, | |
1323 | filename_ptr, | |
1324 | linenumber_ptr); | |
1325 | func_p = lookup_address_in_function_table (unit->function_table, | |
1326 | addr, | |
1327 | functionname_ptr); | |
1328 | return line_p || func_p; | |
1329 | } | |
1330 | ||
1331 | /* The DWARF2 version of find_nearest line. | |
1332 | Return true if the line is found without error. */ | |
1333 | ||
1334 | boolean | |
1335 | _bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset, | |
1336 | filename_ptr, functionname_ptr, linenumber_ptr) | |
1337 | bfd *abfd; | |
1338 | asection *section; | |
1339 | asymbol **symbols; | |
1340 | bfd_vma offset; | |
1341 | const char **filename_ptr; | |
1342 | const char **functionname_ptr; | |
1343 | unsigned int *linenumber_ptr; | |
1344 | { | |
1345 | /* Read each compilation unit from the section .debug_info, and check | |
1346 | to see if it contains the address we are searching for. If yes, | |
1347 | lookup the address, and return the line number info. If no, go | |
1348 | on to the next compilation unit. | |
1349 | ||
1350 | We keep a list of all the previously read compilation units, and | |
1351 | a pointer to the next un-read compilation unit. Check the | |
1352 | previously read units before reading more. | |
1353 | */ | |
1354 | ||
1355 | struct dwarf2_debug *stash = elf_tdata (abfd)->dwarf2_find_line_info; | |
1356 | ||
1357 | /* What address are we looking for? */ | |
1358 | bfd_vma addr = offset + section->vma; | |
1359 | ||
1360 | struct comp_unit* each; | |
1361 | ||
1362 | *filename_ptr = NULL; | |
1363 | *functionname_ptr = NULL; | |
1364 | *linenumber_ptr = 0; | |
1365 | ||
1366 | if (! stash) | |
1367 | { | |
1368 | asection *msec; | |
1369 | unsigned long size; | |
1370 | ||
1371 | stash = elf_tdata (abfd)->dwarf2_find_line_info = | |
1372 | (struct dwarf2_debug*) bfd_zalloc (abfd, sizeof (struct dwarf2_debug)); | |
1373 | ||
1374 | if (! stash) | |
1375 | return false; | |
1376 | ||
1377 | msec = bfd_get_section_by_name (abfd, ".debug_info"); | |
1378 | if (! msec) | |
1379 | { | |
1380 | /* No dwarf2 info. Note that at this point the stash | |
1381 | has been allocated, but contains zeros, this lets | |
1382 | future calls to this function fail quicker. */ | |
1383 | return false; | |
1384 | } | |
1385 | ||
1386 | size = bfd_get_section_size_before_reloc (msec); | |
1387 | stash->info_ptr = (unsigned char*) bfd_alloc (abfd, size); | |
1388 | ||
1389 | if (! stash->info_ptr) | |
1390 | return false; | |
1391 | ||
1392 | if (! bfd_get_section_contents (abfd, msec, stash->info_ptr, 0, size)) | |
1393 | { | |
1394 | stash->info_ptr = 0; | |
1395 | return false; | |
1396 | } | |
1397 | ||
1398 | stash->info_ptr_end = stash->info_ptr + size; | |
1399 | } | |
1400 | ||
1401 | ||
1402 | /* A null info_ptr indicates that there is no dwarf2 info | |
1403 | (or that an error occured while setting up the stash). */ | |
1404 | ||
1405 | if (! stash->info_ptr) | |
1406 | return false; | |
1407 | ||
1408 | ||
1409 | ||
1410 | /* Check the previously read comp. units first. */ | |
1411 | ||
1412 | for (each = stash->all_comp_units; each; each = each->next_unit) | |
1413 | { | |
1414 | if (comp_unit_contains_address (each, addr)) | |
1415 | return comp_unit_find_nearest_line (each, addr, | |
1416 | filename_ptr, | |
1417 | functionname_ptr, | |
1418 | linenumber_ptr); | |
1419 | } | |
1420 | ||
1421 | ||
1422 | /* Read each remaining comp. units checking each as they are read. */ | |
1423 | while (stash->info_ptr < stash->info_ptr_end) | |
1424 | { | |
1425 | struct comp_unit* each; | |
1426 | unsigned int length; | |
1427 | ||
1428 | length = read_4_bytes (abfd, stash->info_ptr); | |
1429 | stash->info_ptr += 4; | |
1430 | ||
1431 | if (length > 0) | |
1432 | { | |
1433 | each = parse_comp_unit (abfd, stash->info_ptr, | |
1434 | stash->info_ptr + length); | |
1435 | stash->info_ptr += length; | |
1436 | ||
1437 | if (each) | |
1438 | { | |
1439 | each->next_unit = stash->all_comp_units; | |
1440 | stash->all_comp_units = each; | |
1441 | ||
1442 | if (comp_unit_contains_address (each, addr)) | |
1443 | return comp_unit_find_nearest_line (each, addr, | |
1444 | filename_ptr, | |
1445 | functionname_ptr, | |
1446 | linenumber_ptr); | |
1447 | } | |
1448 | } | |
1449 | } | |
1450 | ||
1451 | return false; | |
1452 | } | |
1453 | ||
1454 | /* end of file */ |