* coff-h8300.c (h8300_reloc16_extra_cases): Correct off by one
[deliverable/binutils-gdb.git] / bfd / syms.c
1 /* Generic symbol-table support for the BFD library.
2 Copyright (C) 1990, 91, 92, 93, 94, 95, 1996 Free Software Foundation, Inc.
3 Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 /*
22 SECTION
23 Symbols
24
25 BFD tries to maintain as much symbol information as it can when
26 it moves information from file to file. BFD passes information
27 to applications though the <<asymbol>> structure. When the
28 application requests the symbol table, BFD reads the table in
29 the native form and translates parts of it into the internal
30 format. To maintain more than the information passed to
31 applications, some targets keep some information ``behind the
32 scenes'' in a structure only the particular back end knows
33 about. For example, the coff back end keeps the original
34 symbol table structure as well as the canonical structure when
35 a BFD is read in. On output, the coff back end can reconstruct
36 the output symbol table so that no information is lost, even
37 information unique to coff which BFD doesn't know or
38 understand. If a coff symbol table were read, but were written
39 through an a.out back end, all the coff specific information
40 would be lost. The symbol table of a BFD
41 is not necessarily read in until a canonicalize request is
42 made. Then the BFD back end fills in a table provided by the
43 application with pointers to the canonical information. To
44 output symbols, the application provides BFD with a table of
45 pointers to pointers to <<asymbol>>s. This allows applications
46 like the linker to output a symbol as it was read, since the ``behind
47 the scenes'' information will be still available.
48 @menu
49 @* Reading Symbols::
50 @* Writing Symbols::
51 @* Mini Symbols::
52 @* typedef asymbol::
53 @* symbol handling functions::
54 @end menu
55
56 INODE
57 Reading Symbols, Writing Symbols, Symbols, Symbols
58 SUBSECTION
59 Reading symbols
60
61 There are two stages to reading a symbol table from a BFD:
62 allocating storage, and the actual reading process. This is an
63 excerpt from an application which reads the symbol table:
64
65 | long storage_needed;
66 | asymbol **symbol_table;
67 | long number_of_symbols;
68 | long i;
69 |
70 | storage_needed = bfd_get_symtab_upper_bound (abfd);
71 |
72 | if (storage_needed < 0)
73 | FAIL
74 |
75 | if (storage_needed == 0) {
76 | return ;
77 | }
78 | symbol_table = (asymbol **) xmalloc (storage_needed);
79 | ...
80 | number_of_symbols =
81 | bfd_canonicalize_symtab (abfd, symbol_table);
82 |
83 | if (number_of_symbols < 0)
84 | FAIL
85 |
86 | for (i = 0; i < number_of_symbols; i++) {
87 | process_symbol (symbol_table[i]);
88 | }
89
90 All storage for the symbols themselves is in an obstack
91 connected to the BFD; it is freed when the BFD is closed.
92
93
94 INODE
95 Writing Symbols, Mini Symbols, Reading Symbols, Symbols
96 SUBSECTION
97 Writing symbols
98
99 Writing of a symbol table is automatic when a BFD open for
100 writing is closed. The application attaches a vector of
101 pointers to pointers to symbols to the BFD being written, and
102 fills in the symbol count. The close and cleanup code reads
103 through the table provided and performs all the necessary
104 operations. The BFD output code must always be provided with an
105 ``owned'' symbol: one which has come from another BFD, or one
106 which has been created using <<bfd_make_empty_symbol>>. Here is an
107 example showing the creation of a symbol table with only one element:
108
109 | #include "bfd.h"
110 | main()
111 | {
112 | bfd *abfd;
113 | asymbol *ptrs[2];
114 | asymbol *new;
115 |
116 | abfd = bfd_openw("foo","a.out-sunos-big");
117 | bfd_set_format(abfd, bfd_object);
118 | new = bfd_make_empty_symbol(abfd);
119 | new->name = "dummy_symbol";
120 | new->section = bfd_make_section_old_way(abfd, ".text");
121 | new->flags = BSF_GLOBAL;
122 | new->value = 0x12345;
123 |
124 | ptrs[0] = new;
125 | ptrs[1] = (asymbol *)0;
126 |
127 | bfd_set_symtab(abfd, ptrs, 1);
128 | bfd_close(abfd);
129 | }
130 |
131 | ./makesym
132 | nm foo
133 | 00012345 A dummy_symbol
134
135 Many formats cannot represent arbitary symbol information; for
136 instance, the <<a.out>> object format does not allow an
137 arbitary number of sections. A symbol pointing to a section
138 which is not one of <<.text>>, <<.data>> or <<.bss>> cannot
139 be described.
140
141 INODE
142 Mini Symbols, typedef asymbol, Writing Symbols, Symbols
143 SUBSECTION
144 Mini Symbols
145
146 Mini symbols provide read-only access to the symbol table.
147 They use less memory space, but require more time to access.
148 They can be useful for tools like nm or objdump, which may
149 have to handle symbol tables of extremely large executables.
150
151 The <<bfd_read_minisymbols>> function will read the symbols
152 into memory in an internal form. It will return a <<void *>>
153 pointer to a block of memory, a symbol count, and the size of
154 each symbol. The pointer is allocated using <<malloc>>, and
155 should be freed by the caller when it is no longer needed.
156
157 The function <<bfd_minisymbol_to_symbol>> will take a pointer
158 to a minisymbol, and a pointer to a structure returned by
159 <<bfd_make_empty_symbol>>, and return a <<asymbol>> structure.
160 The return value may or may not be the same as the value from
161 <<bfd_make_empty_symbol>> which was passed in.
162
163 */
164
165
166
167 /*
168 DOCDD
169 INODE
170 typedef asymbol, symbol handling functions, Mini Symbols, Symbols
171
172 */
173 /*
174 SUBSECTION
175 typedef asymbol
176
177 An <<asymbol>> has the form:
178
179 */
180
181 /*
182 CODE_FRAGMENT
183
184 .
185 .typedef struct symbol_cache_entry
186 .{
187 . {* A pointer to the BFD which owns the symbol. This information
188 . is necessary so that a back end can work out what additional
189 . information (invisible to the application writer) is carried
190 . with the symbol.
191 .
192 . This field is *almost* redundant, since you can use section->owner
193 . instead, except that some symbols point to the global sections
194 . bfd_{abs,com,und}_section. This could be fixed by making
195 . these globals be per-bfd (or per-target-flavor). FIXME. *}
196 .
197 . struct _bfd *the_bfd; {* Use bfd_asymbol_bfd(sym) to access this field. *}
198 .
199 . {* The text of the symbol. The name is left alone, and not copied; the
200 . application may not alter it. *}
201 . CONST char *name;
202 .
203 . {* The value of the symbol. This really should be a union of a
204 . numeric value with a pointer, since some flags indicate that
205 . a pointer to another symbol is stored here. *}
206 . symvalue value;
207 .
208 . {* Attributes of a symbol: *}
209 .
210 .#define BSF_NO_FLAGS 0x00
211 .
212 . {* The symbol has local scope; <<static>> in <<C>>. The value
213 . is the offset into the section of the data. *}
214 .#define BSF_LOCAL 0x01
215 .
216 . {* The symbol has global scope; initialized data in <<C>>. The
217 . value is the offset into the section of the data. *}
218 .#define BSF_GLOBAL 0x02
219 .
220 . {* The symbol has global scope and is exported. The value is
221 . the offset into the section of the data. *}
222 .#define BSF_EXPORT BSF_GLOBAL {* no real difference *}
223 .
224 . {* A normal C symbol would be one of:
225 . <<BSF_LOCAL>>, <<BSF_FORT_COMM>>, <<BSF_UNDEFINED>> or
226 . <<BSF_GLOBAL>> *}
227 .
228 . {* The symbol is a debugging record. The value has an arbitary
229 . meaning. *}
230 .#define BSF_DEBUGGING 0x08
231 .
232 . {* The symbol denotes a function entry point. Used in ELF,
233 . perhaps others someday. *}
234 .#define BSF_FUNCTION 0x10
235 .
236 . {* Used by the linker. *}
237 .#define BSF_KEEP 0x20
238 .#define BSF_KEEP_G 0x40
239 .
240 . {* A weak global symbol, overridable without warnings by
241 . a regular global symbol of the same name. *}
242 .#define BSF_WEAK 0x80
243 .
244 . {* This symbol was created to point to a section, e.g. ELF's
245 . STT_SECTION symbols. *}
246 .#define BSF_SECTION_SYM 0x100
247 .
248 . {* The symbol used to be a common symbol, but now it is
249 . allocated. *}
250 .#define BSF_OLD_COMMON 0x200
251 .
252 . {* The default value for common data. *}
253 .#define BFD_FORT_COMM_DEFAULT_VALUE 0
254 .
255 . {* In some files the type of a symbol sometimes alters its
256 . location in an output file - ie in coff a <<ISFCN>> symbol
257 . which is also <<C_EXT>> symbol appears where it was
258 . declared and not at the end of a section. This bit is set
259 . by the target BFD part to convey this information. *}
260 .
261 .#define BSF_NOT_AT_END 0x400
262 .
263 . {* Signal that the symbol is the label of constructor section. *}
264 .#define BSF_CONSTRUCTOR 0x800
265 .
266 . {* Signal that the symbol is a warning symbol. The name is a
267 . warning. The name of the next symbol is the one to warn about;
268 . if a reference is made to a symbol with the same name as the next
269 . symbol, a warning is issued by the linker. *}
270 .#define BSF_WARNING 0x1000
271 .
272 . {* Signal that the symbol is indirect. This symbol is an indirect
273 . pointer to the symbol with the same name as the next symbol. *}
274 .#define BSF_INDIRECT 0x2000
275 .
276 . {* BSF_FILE marks symbols that contain a file name. This is used
277 . for ELF STT_FILE symbols. *}
278 .#define BSF_FILE 0x4000
279 .
280 . {* Symbol is from dynamic linking information. *}
281 .#define BSF_DYNAMIC 0x8000
282 .
283 . {* The symbol denotes a data object. Used in ELF, and perhaps
284 . others someday. *}
285 .#define BSF_OBJECT 0x10000
286 .
287 . flagword flags;
288 .
289 . {* A pointer to the section to which this symbol is
290 . relative. This will always be non NULL, there are special
291 . sections for undefined and absolute symbols. *}
292 . struct sec *section;
293 .
294 . {* Back end special data. *}
295 . union
296 . {
297 . PTR p;
298 . bfd_vma i;
299 . } udata;
300 .
301 .} asymbol;
302 */
303
304 #include "bfd.h"
305 #include "sysdep.h"
306 #include "libbfd.h"
307 #include "bfdlink.h"
308 #include "aout/stab_gnu.h"
309
310 static char coff_section_type PARAMS ((const char *));
311
312 /*
313 DOCDD
314 INODE
315 symbol handling functions, , typedef asymbol, Symbols
316 SUBSECTION
317 Symbol handling functions
318 */
319
320 /*
321 FUNCTION
322 bfd_get_symtab_upper_bound
323
324 DESCRIPTION
325 Return the number of bytes required to store a vector of pointers
326 to <<asymbols>> for all the symbols in the BFD @var{abfd},
327 including a terminal NULL pointer. If there are no symbols in
328 the BFD, then return 0. If an error occurs, return -1.
329
330 .#define bfd_get_symtab_upper_bound(abfd) \
331 . BFD_SEND (abfd, _bfd_get_symtab_upper_bound, (abfd))
332
333 */
334
335 /*
336 FUNCTION
337 bfd_is_local_label
338
339 SYNOPSIS
340 boolean bfd_is_local_label(bfd *abfd, asymbol *sym);
341
342 DESCRIPTION
343 Return true if the given symbol @var{sym} in the BFD @var{abfd} is
344 a compiler generated local label, else return false.
345 .#define bfd_is_local_label(abfd, sym) \
346 . BFD_SEND (abfd, _bfd_is_local_label,(abfd, sym))
347 */
348
349 /*
350 FUNCTION
351 bfd_canonicalize_symtab
352
353 DESCRIPTION
354 Read the symbols from the BFD @var{abfd}, and fills in
355 the vector @var{location} with pointers to the symbols and
356 a trailing NULL.
357 Return the actual number of symbol pointers, not
358 including the NULL.
359
360
361 .#define bfd_canonicalize_symtab(abfd, location) \
362 . BFD_SEND (abfd, _bfd_canonicalize_symtab,\
363 . (abfd, location))
364
365 */
366
367
368 /*
369 FUNCTION
370 bfd_set_symtab
371
372 SYNOPSIS
373 boolean bfd_set_symtab (bfd *abfd, asymbol **location, unsigned int count);
374
375 DESCRIPTION
376 Arrange that when the output BFD @var{abfd} is closed,
377 the table @var{location} of @var{count} pointers to symbols
378 will be written.
379 */
380
381 boolean
382 bfd_set_symtab (abfd, location, symcount)
383 bfd *abfd;
384 asymbol **location;
385 unsigned int symcount;
386 {
387 if ((abfd->format != bfd_object) || (bfd_read_p (abfd)))
388 {
389 bfd_set_error (bfd_error_invalid_operation);
390 return false;
391 }
392
393 bfd_get_outsymbols (abfd) = location;
394 bfd_get_symcount (abfd) = symcount;
395 return true;
396 }
397
398 /*
399 FUNCTION
400 bfd_print_symbol_vandf
401
402 SYNOPSIS
403 void bfd_print_symbol_vandf(PTR file, asymbol *symbol);
404
405 DESCRIPTION
406 Print the value and flags of the @var{symbol} supplied to the
407 stream @var{file}.
408 */
409 void
410 bfd_print_symbol_vandf (arg, symbol)
411 PTR arg;
412 asymbol *symbol;
413 {
414 FILE *file = (FILE *) arg;
415 flagword type = symbol->flags;
416 if (symbol->section != (asection *) NULL)
417 {
418 fprintf_vma (file, symbol->value + symbol->section->vma);
419 }
420 else
421 {
422 fprintf_vma (file, symbol->value);
423 }
424
425 /* This presumes that a symbol can not be both BSF_DEBUGGING and
426 BSF_DYNAMIC, nor more than one of BSF_FUNCTION, BSF_FILE, and
427 BSF_OBJECT. */
428 fprintf (file, " %c%c%c%c%c%c%c",
429 ((type & BSF_LOCAL)
430 ? (type & BSF_GLOBAL) ? '!' : 'l'
431 : (type & BSF_GLOBAL) ? 'g' : ' '),
432 (type & BSF_WEAK) ? 'w' : ' ',
433 (type & BSF_CONSTRUCTOR) ? 'C' : ' ',
434 (type & BSF_WARNING) ? 'W' : ' ',
435 (type & BSF_INDIRECT) ? 'I' : ' ',
436 (type & BSF_DEBUGGING) ? 'd' : (type & BSF_DYNAMIC) ? 'D' : ' ',
437 ((type & BSF_FUNCTION)
438 ? 'F'
439 : ((type & BSF_FILE)
440 ? 'f'
441 : ((type & BSF_OBJECT) ? 'O' : ' '))));
442 }
443
444
445 /*
446 FUNCTION
447 bfd_make_empty_symbol
448
449 DESCRIPTION
450 Create a new <<asymbol>> structure for the BFD @var{abfd}
451 and return a pointer to it.
452
453 This routine is necessary because each back end has private
454 information surrounding the <<asymbol>>. Building your own
455 <<asymbol>> and pointing to it will not create the private
456 information, and will cause problems later on.
457
458 .#define bfd_make_empty_symbol(abfd) \
459 . BFD_SEND (abfd, _bfd_make_empty_symbol, (abfd))
460 */
461
462 /*
463 FUNCTION
464 bfd_make_debug_symbol
465
466 DESCRIPTION
467 Create a new <<asymbol>> structure for the BFD @var{abfd},
468 to be used as a debugging symbol. Further details of its use have
469 yet to be worked out.
470
471 .#define bfd_make_debug_symbol(abfd,ptr,size) \
472 . BFD_SEND (abfd, _bfd_make_debug_symbol, (abfd, ptr, size))
473 */
474
475 struct section_to_type
476 {
477 CONST char *section;
478 char type;
479 };
480
481 /* Map section names to POSIX/BSD single-character symbol types.
482 This table is probably incomplete. It is sorted for convenience of
483 adding entries. Since it is so short, a linear search is used. */
484 static CONST struct section_to_type stt[] =
485 {
486 {"*DEBUG*", 'N'},
487 {".bss", 'b'},
488 {"zerovars", 'b'}, /* MRI .bss */
489 {".data", 'd'},
490 {"vars", 'd'}, /* MRI .data */
491 {".rdata", 'r'}, /* Read only data. */
492 {".rodata", 'r'}, /* Read only data. */
493 {".sbss", 's'}, /* Small BSS (uninitialized data). */
494 {".scommon", 'c'}, /* Small common. */
495 {".sdata", 'g'}, /* Small initialized data. */
496 {".text", 't'},
497 {"code", 't'}, /* MRI .text */
498 {0, 0}
499 };
500
501 /* Return the single-character symbol type corresponding to
502 section S, or '?' for an unknown COFF section.
503
504 Check for any leading string which matches, so .text5 returns
505 't' as well as .text */
506
507 static char
508 coff_section_type (s)
509 const char *s;
510 {
511 CONST struct section_to_type *t;
512
513 for (t = &stt[0]; t->section; t++)
514 if (!strncmp (s, t->section, strlen (t->section)))
515 return t->type;
516
517 return '?';
518 }
519
520 #ifndef islower
521 #define islower(c) ((c) >= 'a' && (c) <= 'z')
522 #endif
523 #ifndef toupper
524 #define toupper(c) (islower(c) ? ((c) & ~0x20) : (c))
525 #endif
526
527 /*
528 FUNCTION
529 bfd_decode_symclass
530
531 DESCRIPTION
532 Return a character corresponding to the symbol
533 class of @var{symbol}, or '?' for an unknown class.
534
535 SYNOPSIS
536 int bfd_decode_symclass(asymbol *symbol);
537 */
538 int
539 bfd_decode_symclass (symbol)
540 asymbol *symbol;
541 {
542 char c;
543
544 if (bfd_is_com_section (symbol->section))
545 return 'C';
546 if (bfd_is_und_section (symbol->section))
547 return 'U';
548 if (bfd_is_ind_section (symbol->section))
549 return 'I';
550 if (symbol->flags & BSF_WEAK)
551 return 'W';
552 if (!(symbol->flags & (BSF_GLOBAL | BSF_LOCAL)))
553 return '?';
554
555 if (bfd_is_abs_section (symbol->section))
556 c = 'a';
557 else if (symbol->section)
558 c = coff_section_type (symbol->section->name);
559 else
560 return '?';
561 if (symbol->flags & BSF_GLOBAL)
562 c = toupper (c);
563 return c;
564
565 /* We don't have to handle these cases just yet, but we will soon:
566 N_SETV: 'v';
567 N_SETA: 'l';
568 N_SETT: 'x';
569 N_SETD: 'z';
570 N_SETB: 's';
571 N_INDR: 'i';
572 */
573 }
574
575 /*
576 FUNCTION
577 bfd_symbol_info
578
579 DESCRIPTION
580 Fill in the basic info about symbol that nm needs.
581 Additional info may be added by the back-ends after
582 calling this function.
583
584 SYNOPSIS
585 void bfd_symbol_info(asymbol *symbol, symbol_info *ret);
586 */
587
588 void
589 bfd_symbol_info (symbol, ret)
590 asymbol *symbol;
591 symbol_info *ret;
592 {
593 ret->type = bfd_decode_symclass (symbol);
594 if (ret->type != 'U')
595 ret->value = symbol->value + symbol->section->vma;
596 else
597 ret->value = 0;
598 ret->name = symbol->name;
599 }
600
601 void
602 bfd_symbol_is_absolute ()
603 {
604 abort ();
605 }
606
607 /*
608 FUNCTION
609 bfd_copy_private_symbol_data
610
611 SYNOPSIS
612 boolean bfd_copy_private_symbol_data(bfd *ibfd, asymbol *isym, bfd *obfd, asymbol *osym);
613
614 DESCRIPTION
615 Copy private symbol information from @var{isym} in the BFD
616 @var{ibfd} to the symbol @var{osym} in the BFD @var{obfd}.
617 Return <<true>> on success, <<false>> on error. Possible error
618 returns are:
619
620 o <<bfd_error_no_memory>> -
621 Not enough memory exists to create private data for @var{osec}.
622
623 .#define bfd_copy_private_symbol_data(ibfd, isymbol, obfd, osymbol) \
624 . BFD_SEND (ibfd, _bfd_copy_private_symbol_data, \
625 . (ibfd, isymbol, obfd, osymbol))
626
627 */
628
629 /* The generic version of the function which returns mini symbols.
630 This is used when the backend does not provide a more efficient
631 version. It just uses BFD asymbol structures as mini symbols. */
632
633 long
634 _bfd_generic_read_minisymbols (abfd, dynamic, minisymsp, sizep)
635 bfd *abfd;
636 boolean dynamic;
637 PTR *minisymsp;
638 unsigned int *sizep;
639 {
640 long storage;
641 asymbol **syms = NULL;
642 long symcount;
643
644 if (dynamic)
645 storage = bfd_get_dynamic_symtab_upper_bound (abfd);
646 else
647 storage = bfd_get_symtab_upper_bound (abfd);
648 if (storage < 0)
649 goto error_return;
650
651 syms = (asymbol **) bfd_malloc ((size_t) storage);
652 if (syms == NULL)
653 goto error_return;
654
655 if (dynamic)
656 symcount = bfd_canonicalize_dynamic_symtab (abfd, syms);
657 else
658 symcount = bfd_canonicalize_symtab (abfd, syms);
659 if (symcount < 0)
660 goto error_return;
661
662 *minisymsp = (PTR) syms;
663 *sizep = sizeof (asymbol *);
664 return symcount;
665
666 error_return:
667 if (syms != NULL)
668 free (syms);
669 return -1;
670 }
671
672 /* The generic version of the function which converts a minisymbol to
673 an asymbol. We don't worry about the sym argument we are passed;
674 we just return the asymbol the minisymbol points to. */
675
676 /*ARGSUSED*/
677 asymbol *
678 _bfd_generic_minisymbol_to_symbol (abfd, dynamic, minisym, sym)
679 bfd *abfd;
680 boolean dynamic;
681 const PTR minisym;
682 asymbol *sym;
683 {
684 return *(asymbol **) minisym;
685 }
686
687 /* Look through stabs debugging information in .stab and .stabstr
688 sections to find the source file and line closest to a desired
689 location. This is used by COFF and ELF targets. It sets *pfound
690 to true if it finds some information. The *pinfo field is used to
691 pass cached information in and out of this routine; this first time
692 the routine is called for a BFD, *pinfo should be NULL. The value
693 placed in *pinfo should be saved with the BFD, and passed back each
694 time this function is called. */
695
696 /* A pointer to this structure is stored in *pinfo. */
697
698 struct stab_find_info
699 {
700 /* The .stab section. */
701 asection *stabsec;
702 /* The .stabstr section. */
703 asection *strsec;
704 /* The contents of the .stab section. */
705 bfd_byte *stabs;
706 /* The contents of the .stabstr section. */
707 bfd_byte *strs;
708 /* An malloc buffer to hold the file name. */
709 char *filename;
710 /* Cached values to restart quickly. */
711 bfd_vma cached_offset;
712 bfd_byte *cached_stab;
713 bfd_byte *cached_str;
714 bfd_size_type cached_stroff;
715 };
716
717 boolean
718 _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, pfound,
719 pfilename, pfnname, pline, pinfo)
720 bfd *abfd;
721 asymbol **symbols;
722 asection *section;
723 bfd_vma offset;
724 boolean *pfound;
725 const char **pfilename;
726 const char **pfnname;
727 unsigned int *pline;
728 PTR *pinfo;
729 {
730 struct stab_find_info *info;
731 bfd_size_type stabsize, strsize;
732 bfd_byte *stab, *stabend, *str;
733 bfd_size_type stroff;
734 bfd_vma fnaddr;
735 char *directory_name, *main_file_name, *current_file_name, *line_file_name;
736 char *fnname;
737 bfd_vma low_func_vma, low_line_vma;
738
739 *pfound = false;
740 *pfilename = bfd_get_filename (abfd);
741 *pfnname = NULL;
742 *pline = 0;
743
744 info = (struct stab_find_info *) *pinfo;
745 if (info != NULL)
746 {
747 if (info->stabsec == NULL || info->strsec == NULL)
748 {
749 /* No stabs debugging information. */
750 return true;
751 }
752
753 stabsize = info->stabsec->_raw_size;
754 strsize = info->strsec->_raw_size;
755 }
756 else
757 {
758 long reloc_size, reloc_count;
759 arelent **reloc_vector;
760
761 info = (struct stab_find_info *) bfd_zalloc (abfd, sizeof *info);
762 if (info == NULL)
763 return false;
764
765 /* FIXME: When using the linker --split-by-file or
766 --split-by-reloc options, it is possible for the .stab and
767 .stabstr sections to be split. We should handle that. */
768
769 info->stabsec = bfd_get_section_by_name (abfd, ".stab");
770 info->strsec = bfd_get_section_by_name (abfd, ".stabstr");
771
772 if (info->stabsec == NULL || info->strsec == NULL)
773 {
774 /* No stabs debugging information. Set *pinfo so that we
775 can return quickly in the info != NULL case above. */
776 *pinfo = (PTR) info;
777 return true;
778 }
779
780 stabsize = info->stabsec->_raw_size;
781 strsize = info->strsec->_raw_size;
782
783 info->stabs = (bfd_byte *) bfd_alloc (abfd, stabsize);
784 info->strs = (bfd_byte *) bfd_alloc (abfd, strsize);
785 if (info->stabs == NULL || info->strs == NULL)
786 return false;
787
788 if (! bfd_get_section_contents (abfd, info->stabsec, info->stabs, 0,
789 stabsize)
790 || ! bfd_get_section_contents (abfd, info->strsec, info->strs, 0,
791 strsize))
792 return false;
793
794 /* If this is a relocateable object file, we have to relocate
795 the entries in .stab. This should always be simple 32 bit
796 relocations against symbols defined in this object file, so
797 this should be no big deal. */
798 reloc_size = bfd_get_reloc_upper_bound (abfd, info->stabsec);
799 if (reloc_size < 0)
800 return false;
801 reloc_vector = (arelent **) bfd_malloc (reloc_size);
802 if (reloc_vector == NULL && reloc_size != 0)
803 return false;
804 reloc_count = bfd_canonicalize_reloc (abfd, info->stabsec, reloc_vector,
805 symbols);
806 if (reloc_count < 0)
807 {
808 if (reloc_vector != NULL)
809 free (reloc_vector);
810 return false;
811 }
812 if (reloc_count > 0)
813 {
814 arelent **pr;
815
816 for (pr = reloc_vector; *pr != NULL; pr++)
817 {
818 arelent *r;
819 unsigned long val;
820 asymbol *sym;
821
822 r = *pr;
823 if (r->howto->rightshift != 0
824 || r->howto->size != 2
825 || r->howto->bitsize != 32
826 || r->howto->pc_relative
827 || r->howto->bitpos != 0
828 || r->howto->dst_mask != 0xffffffff)
829 {
830 (*_bfd_error_handler)
831 ("Unsupported .stab relocation");
832 bfd_set_error (bfd_error_invalid_operation);
833 if (reloc_vector != NULL)
834 free (reloc_vector);
835 return false;
836 }
837
838 val = bfd_get_32 (abfd, info->stabs + r->address);
839 val &= r->howto->src_mask;
840 sym = *r->sym_ptr_ptr;
841 val += sym->value + sym->section->vma + r->addend;
842 bfd_put_32 (abfd, val, info->stabs + r->address);
843 }
844 }
845
846 if (reloc_vector != NULL)
847 free (reloc_vector);
848
849 *pinfo = (PTR) info;
850 }
851
852 /* We are passed a section relative offset. The offsets in the
853 stabs information are absolute. */
854 offset += bfd_get_section_vma (abfd, section);
855
856 /* Stabs entries use a 12 byte format:
857 4 byte string table index
858 1 byte stab type
859 1 byte stab other field
860 2 byte stab desc field
861 4 byte stab value
862 FIXME: This will have to change for a 64 bit object format.
863
864 The stabs symbols are divided into compilation units. For the
865 first entry in each unit, the type of 0, the value is the length
866 of the string table for this unit, and the desc field is the
867 number of stabs symbols for this unit. */
868
869 #define STRDXOFF (0)
870 #define TYPEOFF (4)
871 #define OTHEROFF (5)
872 #define DESCOFF (6)
873 #define VALOFF (8)
874 #define STABSIZE (12)
875
876 /* It would be nice if we could skip ahead to the stabs symbols for
877 the next compilation unit to quickly scan through the compilation
878 units. Unfortunately, since each line number gets a separate
879 stabs entry, it is entirely plausible that a large source file
880 will overflow the 16 bit count of stabs entries. */
881 fnaddr = 0;
882 directory_name = NULL;
883 main_file_name = NULL;
884 current_file_name = NULL;
885 line_file_name = NULL;
886 fnname = NULL;
887 low_func_vma = 0;
888 low_line_vma = 0;
889
890 stabend = info->stabs + stabsize;
891
892 if (info->cached_stab == NULL || offset < info->cached_offset)
893 {
894 stab = info->stabs;
895 str = info->strs;
896 stroff = 0;
897 }
898 else
899 {
900 stab = info->cached_stab;
901 str = info->cached_str;
902 stroff = info->cached_stroff;
903 }
904
905 info->cached_offset = offset;
906
907 for (; stab < stabend; stab += STABSIZE)
908 {
909 boolean done;
910 bfd_vma val;
911 char *name;
912
913 done = false;
914
915 switch (stab[TYPEOFF])
916 {
917 case 0:
918 /* This is the first entry in a compilation unit. */
919 if ((bfd_size_type) ((info->strs + strsize) - str) < stroff)
920 {
921 done = true;
922 break;
923 }
924 str += stroff;
925 stroff = bfd_get_32 (abfd, stab + VALOFF);
926 break;
927
928 case N_SO:
929 /* The main file name. */
930
931 val = bfd_get_32 (abfd, stab + VALOFF);
932 if (val > offset)
933 {
934 done = true;
935 break;
936 }
937
938 name = (char *) str + bfd_get_32 (abfd, stab + STRDXOFF);
939
940 /* An empty string indicates the end of the compilation
941 unit. */
942 if (*name == '\0')
943 {
944 /* If there are functions in different sections, they
945 may have addresses larger than val, but we don't want
946 to forget the file name. When there are functions in
947 different cases, there is supposed to be an N_FUN at
948 the end of the function indicating where it ends. */
949 if (low_func_vma < val || fnname == NULL)
950 main_file_name = NULL;
951 break;
952 }
953
954 /* We know that we have to get to at least this point in the
955 stabs entries for this offset. */
956 info->cached_stab = stab;
957 info->cached_str = str;
958 info->cached_stroff = stroff;
959
960 current_file_name = name;
961
962 /* Look ahead to the next symbol. Two consecutive N_SO
963 symbols are a directory and a file name. */
964 if (stab + STABSIZE >= stabend
965 || *(stab + STABSIZE + TYPEOFF) != N_SO)
966 directory_name = NULL;
967 else
968 {
969 stab += STABSIZE;
970 directory_name = current_file_name;
971 current_file_name = ((char *) str
972 + bfd_get_32 (abfd, stab + STRDXOFF));
973 }
974
975 main_file_name = current_file_name;
976
977 break;
978
979 case N_SOL:
980 /* The name of an include file. */
981 current_file_name = ((char *) str
982 + bfd_get_32 (abfd, stab + STRDXOFF));
983 break;
984
985 case N_SLINE:
986 case N_DSLINE:
987 case N_BSLINE:
988 /* A line number. The value is relative to the start of the
989 current function. */
990 val = fnaddr + bfd_get_32 (abfd, stab + VALOFF);
991 if (val >= low_line_vma && val <= offset)
992 {
993 *pline = bfd_get_16 (abfd, stab + DESCOFF);
994 low_line_vma = val;
995 line_file_name = current_file_name;
996 }
997 break;
998
999 case N_FUN:
1000 /* A function name. */
1001 val = bfd_get_32 (abfd, stab + VALOFF);
1002 name = (char *) str + bfd_get_32 (abfd, stab + STRDXOFF);
1003
1004 /* An empty string here indicates the end of a function, and
1005 the value is relative to fnaddr. */
1006
1007 if (*name == '\0')
1008 {
1009 val += fnaddr;
1010 if (val >= low_func_vma && val < offset)
1011 fnname = NULL;
1012 }
1013 else
1014 {
1015 if (val >= low_func_vma && val <= offset)
1016 {
1017 fnname = name;
1018 low_func_vma = val;
1019 }
1020
1021 fnaddr = val;
1022 }
1023
1024 break;
1025 }
1026
1027 if (done)
1028 break;
1029 }
1030
1031 if (main_file_name == NULL)
1032 {
1033 /* No information found. */
1034 return true;
1035 }
1036
1037 *pfound = true;
1038
1039 if (*pline != 0)
1040 main_file_name = line_file_name;
1041
1042 if (main_file_name != NULL)
1043 {
1044 if (main_file_name[0] == '/' || directory_name == NULL)
1045 *pfilename = main_file_name;
1046 else
1047 {
1048 size_t dirlen;
1049
1050 dirlen = strlen (directory_name);
1051 if (info->filename == NULL
1052 || strncmp (info->filename, directory_name, dirlen) != 0
1053 || strcmp (info->filename + dirlen, main_file_name) != 0)
1054 {
1055 if (info->filename != NULL)
1056 free (info->filename);
1057 info->filename = (char *) bfd_malloc (dirlen +
1058 strlen (main_file_name)
1059 + 1);
1060 if (info->filename == NULL)
1061 return false;
1062 strcpy (info->filename, directory_name);
1063 strcpy (info->filename + dirlen, main_file_name);
1064 }
1065
1066 *pfilename = info->filename;
1067 }
1068 }
1069
1070 if (fnname != NULL)
1071 {
1072 char *s;
1073
1074 /* This will typically be something like main:F(0,1), so we want
1075 to clobber the colon. It's OK to change the name, since the
1076 string is in our own local storage anyhow. */
1077
1078 s = strchr (fnname, ':');
1079 if (s != NULL)
1080 *s = '\0';
1081
1082 *pfnname = fnname;
1083 }
1084
1085 return true;
1086 }
This page took 0.099856 seconds and 4 git commands to generate.