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