source: trunk/src/binutils/bfd/syms.c@ 610

Last change on this file since 610 was 86, checked in by bird, 22 years ago

Applied the original 2.11.2 diff.

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