6f02cb8593b427c2ac91435f5d63c08585687ee8
[deliverable/binutils-gdb.git] / bfd / dwarf1.c
1 /* DWARF 1 find nearest line (_bfd_dwarf1_find_nearest_line).
2 Copyright 1998, 1999, 2000, 2001, 2002, 2004, 2005, 2007
3 Free Software Foundation, Inc.
4
5 Written by Gavin Romig-Koch of Cygnus Solutions (gavin@cygnus.com).
6
7 This file is part of BFD.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or (at
12 your option) any later version.
13
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
23
24 #include "sysdep.h"
25 #include "bfd.h"
26 #include "libiberty.h"
27 #include "libbfd.h"
28 #include "elf-bfd.h"
29 #include "elf/dwarf.h"
30
31 /* dwarf1_debug is the starting point for all dwarf1 info. */
32
33 struct dwarf1_debug
34 {
35 /* The bfd we are working with. */
36 bfd* abfd;
37
38 /* List of already parsed compilation units. */
39 struct dwarf1_unit* lastUnit;
40
41 /* The buffer for the .debug section.
42 Zero indicates that the .debug section failed to load. */
43 char* debug_section;
44
45 /* Pointer to the end of the .debug_info section memory buffer. */
46 char* debug_section_end;
47
48 /* The buffer for the .line section. */
49 char* line_section;
50
51 /* End of that buffer. */
52 char* line_section_end;
53
54 /* The current or next unread die within the .debug section. */
55 char* currentDie;
56 };
57
58 /* One dwarf1_unit for each parsed compilation unit die. */
59
60 struct dwarf1_unit
61 {
62 /* Linked starting from stash->lastUnit. */
63 struct dwarf1_unit* prev;
64
65 /* Name of the compilation unit. */
66 char* name;
67
68 /* The highest and lowest address used in the compilation unit. */
69 unsigned long low_pc;
70 unsigned long high_pc;
71
72 /* Does this unit have a statement list? */
73 int has_stmt_list;
74
75 /* If any, the offset of the line number table in the .line section. */
76 unsigned long stmt_list_offset;
77
78 /* If non-zero, a pointer to the first child of this unit. */
79 char* first_child;
80
81 /* How many line entries? */
82 unsigned long line_count;
83
84 /* The decoded line number table (line_count entries). */
85 struct linenumber* linenumber_table;
86
87 /* The list of functions in this unit. */
88 struct dwarf1_func* func_list;
89 };
90
91 /* One dwarf1_func for each parsed function die. */
92
93 struct dwarf1_func
94 {
95 /* Linked starting from aUnit->func_list. */
96 struct dwarf1_func* prev;
97
98 /* Name of function. */
99 char* name;
100
101 /* The highest and lowest address used in the compilation unit. */
102 unsigned long low_pc;
103 unsigned long high_pc;
104 };
105
106 /* Used to return info about a parsed die. */
107 struct die_info
108 {
109 unsigned long length;
110 unsigned long sibling;
111 unsigned long low_pc;
112 unsigned long high_pc;
113 unsigned long stmt_list_offset;
114
115 char* name;
116
117 int has_stmt_list;
118
119 unsigned short tag;
120 };
121
122 /* Parsed line number information. */
123 struct linenumber
124 {
125 /* First address in the line. */
126 unsigned long addr;
127
128 /* The line number. */
129 unsigned long linenumber;
130 };
131
132 /* Find the form of an attr, from the attr field. */
133 #define FORM_FROM_ATTR(attr) ((attr) & 0xF) /* Implicitly specified. */
134
135 /* Return a newly allocated dwarf1_unit. It should be cleared and
136 then attached into the 'stash' at 'stash->lastUnit'. */
137
138 static struct dwarf1_unit*
139 alloc_dwarf1_unit (struct dwarf1_debug* stash)
140 {
141 bfd_size_type amt = sizeof (struct dwarf1_unit);
142
143 struct dwarf1_unit* x = bfd_zalloc (stash->abfd, amt);
144 x->prev = stash->lastUnit;
145 stash->lastUnit = x;
146
147 return x;
148 }
149
150 /* Return a newly allocated dwarf1_func. It must be cleared and
151 attached into 'aUnit' at 'aUnit->func_list'. */
152
153 static struct dwarf1_func *
154 alloc_dwarf1_func (struct dwarf1_debug* stash, struct dwarf1_unit* aUnit)
155 {
156 bfd_size_type amt = sizeof (struct dwarf1_func);
157
158 struct dwarf1_func* x = bfd_zalloc (stash->abfd, amt);
159 x->prev = aUnit->func_list;
160 aUnit->func_list = x;
161
162 return x;
163 }
164
165 /* parse_die - parse a Dwarf1 die.
166 Parse the die starting at 'aDiePtr' into 'aDieInfo'.
167 'abfd' must be the bfd from which the section that 'aDiePtr'
168 points to was pulled from.
169
170 Return FALSE if the die is invalidly formatted; TRUE otherwise. */
171
172 static bfd_boolean
173 parse_die (bfd * abfd,
174 struct die_info * aDieInfo,
175 char * aDiePtr,
176 char * aDiePtrEnd)
177 {
178 char* this_die = aDiePtr;
179 char* xptr = this_die;
180
181 memset (aDieInfo, 0, sizeof (* aDieInfo));
182
183 /* First comes the length. */
184 aDieInfo->length = bfd_get_32 (abfd, (bfd_byte *) xptr);
185 xptr += 4;
186 if (aDieInfo->length == 0
187 || (this_die + aDieInfo->length) >= aDiePtrEnd)
188 return FALSE;
189 if (aDieInfo->length < 6)
190 {
191 /* Just padding bytes. */
192 aDieInfo->tag = TAG_padding;
193 return TRUE;
194 }
195
196 /* Then the tag. */
197 aDieInfo->tag = bfd_get_16 (abfd, (bfd_byte *) xptr);
198 xptr += 2;
199
200 /* Then the attributes. */
201 while (xptr < (this_die + aDieInfo->length))
202 {
203 unsigned short attr;
204
205 /* Parse the attribute based on its form. This section
206 must handle all dwarf1 forms, but need only handle the
207 actual attributes that we care about. */
208 attr = bfd_get_16 (abfd, (bfd_byte *) xptr);
209 xptr += 2;
210
211 switch (FORM_FROM_ATTR (attr))
212 {
213 case FORM_DATA2:
214 xptr += 2;
215 break;
216 case FORM_DATA4:
217 case FORM_REF:
218 if (attr == AT_sibling)
219 aDieInfo->sibling = bfd_get_32 (abfd, (bfd_byte *) xptr);
220 else if (attr == AT_stmt_list)
221 {
222 aDieInfo->stmt_list_offset = bfd_get_32 (abfd, (bfd_byte *) xptr);
223 aDieInfo->has_stmt_list = 1;
224 }
225 xptr += 4;
226 break;
227 case FORM_DATA8:
228 xptr += 8;
229 break;
230 case FORM_ADDR:
231 if (attr == AT_low_pc)
232 aDieInfo->low_pc = bfd_get_32 (abfd, (bfd_byte *) xptr);
233 else if (attr == AT_high_pc)
234 aDieInfo->high_pc = bfd_get_32 (abfd, (bfd_byte *) xptr);
235 xptr += 4;
236 break;
237 case FORM_BLOCK2:
238 xptr += 2 + bfd_get_16 (abfd, (bfd_byte *) xptr);
239 break;
240 case FORM_BLOCK4:
241 xptr += 4 + bfd_get_32 (abfd, (bfd_byte *) xptr);
242 break;
243 case FORM_STRING:
244 if (attr == AT_name)
245 aDieInfo->name = xptr;
246 xptr += strlen (xptr) + 1;
247 break;
248 }
249 }
250
251 return TRUE;
252 }
253
254 /* Parse a dwarf1 line number table for 'aUnit->stmt_list_offset'
255 into 'aUnit->linenumber_table'. Return FALSE if an error
256 occurs; TRUE otherwise. */
257
258 static bfd_boolean
259 parse_line_table (struct dwarf1_debug* stash, struct dwarf1_unit* aUnit)
260 {
261 char* xptr;
262
263 /* Load the ".line" section from the bfd if we haven't already. */
264 if (stash->line_section == 0)
265 {
266 asection *msec;
267 bfd_size_type size;
268
269 msec = bfd_get_section_by_name (stash->abfd, ".line");
270 if (! msec)
271 return FALSE;
272
273 size = msec->rawsize ? msec->rawsize : msec->size;
274 stash->line_section = bfd_alloc (stash->abfd, size);
275
276 if (! stash->line_section)
277 return FALSE;
278
279 if (! bfd_get_section_contents (stash->abfd, msec, stash->line_section,
280 0, size))
281 {
282 stash->line_section = 0;
283 return FALSE;
284 }
285
286 stash->line_section_end = stash->line_section + size;
287 }
288
289 xptr = stash->line_section + aUnit->stmt_list_offset;
290 if (xptr < stash->line_section_end)
291 {
292 unsigned long eachLine;
293 char *tblend;
294 unsigned long base;
295 bfd_size_type amt;
296
297 /* First comes the length. */
298 tblend = bfd_get_32 (stash->abfd, (bfd_byte *) xptr) + xptr;
299 xptr += 4;
300
301 /* Then the base address for each address in the table. */
302 base = bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
303 xptr += 4;
304
305 /* How many line entrys?
306 10 = 4 (line number) + 2 (pos in line) + 4 (address in line). */
307 aUnit->line_count = (tblend - xptr) / 10;
308
309 /* Allocate an array for the entries. */
310 amt = sizeof (struct linenumber) * aUnit->line_count;
311 aUnit->linenumber_table = bfd_alloc (stash->abfd, amt);
312
313 for (eachLine = 0; eachLine < aUnit->line_count; eachLine++)
314 {
315 /* A line number. */
316 aUnit->linenumber_table[eachLine].linenumber
317 = bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
318 xptr += 4;
319
320 /* Skip the position within the line. */
321 xptr += 2;
322
323 /* And finally the address. */
324 aUnit->linenumber_table[eachLine].addr
325 = base + bfd_get_32 (stash->abfd, (bfd_byte *) xptr);
326 xptr += 4;
327 }
328 }
329
330 return TRUE;
331 }
332
333 /* Parse each function die in a compilation unit 'aUnit'.
334 The first child die of 'aUnit' should be in 'aUnit->first_child',
335 the result is placed in 'aUnit->func_list'.
336 Return FALSE if error; TRUE otherwise. */
337
338 static bfd_boolean
339 parse_functions_in_unit (struct dwarf1_debug* stash, struct dwarf1_unit* aUnit)
340 {
341 char* eachDie;
342
343 if (aUnit->first_child)
344 for (eachDie = aUnit->first_child;
345 eachDie < stash->debug_section_end;
346 )
347 {
348 struct die_info eachDieInfo;
349
350 if (! parse_die (stash->abfd, &eachDieInfo, eachDie,
351 stash->debug_section_end))
352 return FALSE;
353
354 if (eachDieInfo.tag == TAG_global_subroutine
355 || eachDieInfo.tag == TAG_subroutine
356 || eachDieInfo.tag == TAG_inlined_subroutine
357 || eachDieInfo.tag == TAG_entry_point)
358 {
359 struct dwarf1_func* aFunc = alloc_dwarf1_func (stash,aUnit);
360
361 aFunc->name = eachDieInfo.name;
362 aFunc->low_pc = eachDieInfo.low_pc;
363 aFunc->high_pc = eachDieInfo.high_pc;
364 }
365
366 /* Move to next sibling, if none, end loop */
367 if (eachDieInfo.sibling)
368 eachDie = stash->debug_section + eachDieInfo.sibling;
369 else
370 break;
371 }
372
373 return TRUE;
374 }
375
376 /* Find the nearest line to 'addr' in 'aUnit'.
377 Return whether we found the line (or a function) without error. */
378
379 static bfd_boolean
380 dwarf1_unit_find_nearest_line (struct dwarf1_debug* stash,
381 struct dwarf1_unit* aUnit,
382 unsigned long addr,
383 const char **filename_ptr,
384 const char **functionname_ptr,
385 unsigned int *linenumber_ptr)
386 {
387 int line_p = FALSE;
388 int func_p = FALSE;
389
390 if (aUnit->low_pc <= addr && addr < aUnit->high_pc)
391 {
392 if (aUnit->has_stmt_list)
393 {
394 unsigned long i;
395 struct dwarf1_func* eachFunc;
396
397 if (! aUnit->linenumber_table)
398 {
399 if (! parse_line_table (stash, aUnit))
400 return FALSE;
401 }
402
403 if (! aUnit->func_list)
404 {
405 if (! parse_functions_in_unit (stash, aUnit))
406 return FALSE;
407 }
408
409 for (i = 0; i < aUnit->line_count; i++)
410 {
411 if (aUnit->linenumber_table[i].addr <= addr
412 && addr < aUnit->linenumber_table[i+1].addr)
413 {
414 *filename_ptr = aUnit->name;
415 *linenumber_ptr = aUnit->linenumber_table[i].linenumber;
416 line_p = TRUE;
417 break;
418 }
419 }
420
421 for (eachFunc = aUnit->func_list;
422 eachFunc;
423 eachFunc = eachFunc->prev)
424 {
425 if (eachFunc->low_pc <= addr
426 && addr < eachFunc->high_pc)
427 {
428 *functionname_ptr = eachFunc->name;
429 func_p = TRUE;
430 break;
431 }
432 }
433 }
434 }
435
436 return line_p || func_p;
437 }
438
439 /* The DWARF 1 version of find_nearest line.
440 Return TRUE if the line is found without error. */
441
442 bfd_boolean
443 _bfd_dwarf1_find_nearest_line (bfd *abfd,
444 asection *section,
445 asymbol **symbols ATTRIBUTE_UNUSED,
446 bfd_vma offset,
447 const char **filename_ptr,
448 const char **functionname_ptr,
449 unsigned int *linenumber_ptr)
450 {
451 struct dwarf1_debug *stash = elf_tdata (abfd)->dwarf1_find_line_info;
452
453 struct dwarf1_unit* eachUnit;
454
455 /* What address are we looking for? */
456 unsigned long addr = (unsigned long)(offset + section->vma);
457
458 *filename_ptr = NULL;
459 *functionname_ptr = NULL;
460 *linenumber_ptr = 0;
461
462 if (! stash)
463 {
464 asection *msec;
465 bfd_size_type size = sizeof (struct dwarf1_debug);
466
467 stash = elf_tdata (abfd)->dwarf1_find_line_info
468 = bfd_zalloc (abfd, size);
469
470 if (! stash)
471 return FALSE;
472
473 msec = bfd_get_section_by_name (abfd, ".debug");
474 if (! msec)
475 /* No dwarf1 info. Note that at this point the stash
476 has been allocated, but contains zeros, this lets
477 future calls to this function fail quicker. */
478 return FALSE;
479
480 size = msec->rawsize ? msec->rawsize : msec->size;
481 stash->debug_section = bfd_alloc (abfd, size);
482
483 if (! stash->debug_section)
484 return FALSE;
485
486 if (! bfd_get_section_contents (abfd, msec, stash->debug_section,
487 0, size))
488 {
489 stash->debug_section = 0;
490 return FALSE;
491 }
492
493 stash->debug_section_end = stash->debug_section + size;
494 stash->currentDie = stash->debug_section;
495 stash->abfd = abfd;
496 }
497
498 /* A null debug_section indicates that there was no dwarf1 info
499 or that an error occured while setting up the stash. */
500
501 if (! stash->debug_section)
502 return FALSE;
503
504 /* Look at the previously parsed units to see if any contain
505 the addr. */
506 for (eachUnit = stash->lastUnit; eachUnit; eachUnit = eachUnit->prev)
507 if (eachUnit->low_pc <= addr && addr < eachUnit->high_pc)
508 return dwarf1_unit_find_nearest_line (stash, eachUnit, addr,
509 filename_ptr,
510 functionname_ptr,
511 linenumber_ptr);
512
513 while (stash->currentDie < stash->debug_section_end)
514 {
515 struct die_info aDieInfo;
516
517 if (! parse_die (stash->abfd, &aDieInfo, stash->currentDie,
518 stash->debug_section_end))
519 return FALSE;
520
521 if (aDieInfo.tag == TAG_compile_unit)
522 {
523 struct dwarf1_unit* aUnit
524 = alloc_dwarf1_unit (stash);
525
526 aUnit->name = aDieInfo.name;
527 aUnit->low_pc = aDieInfo.low_pc;
528 aUnit->high_pc = aDieInfo.high_pc;
529 aUnit->has_stmt_list = aDieInfo.has_stmt_list;
530 aUnit->stmt_list_offset = aDieInfo.stmt_list_offset;
531
532 /* A die has a child if it's followed by a die that is
533 not it's sibling. */
534 if (aDieInfo.sibling
535 && stash->currentDie + aDieInfo.length
536 < stash->debug_section_end
537 && stash->currentDie + aDieInfo.length
538 != stash->debug_section + aDieInfo.sibling)
539 aUnit->first_child = stash->currentDie + aDieInfo.length;
540 else
541 aUnit->first_child = 0;
542
543 if (aUnit->low_pc <= addr && addr < aUnit->high_pc)
544 return dwarf1_unit_find_nearest_line (stash, aUnit, addr,
545 filename_ptr,
546 functionname_ptr,
547 linenumber_ptr);
548 }
549
550 if (aDieInfo.sibling != 0)
551 stash->currentDie = stash->debug_section + aDieInfo.sibling;
552 else
553 stash->currentDie += aDieInfo.length;
554 }
555
556 return FALSE;
557 }
This page took 0.040855 seconds and 4 git commands to generate.