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