OSDN Git Service

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