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