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