home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / gdb-4.12.tar.gz / gdb-4.12.tar / gdb-4.12 / bfd / syms.c < prev    next >
C/C++ Source or Header  |  1994-02-03  |  14KB  |  531 lines

  1. /* Generic symbol-table support for the BFD library.
  2.    Copyright (C) 1990, 1991, 1992, 1993 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., 675 Mass Ave, Cambridge, MA 02139, 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. @* typedef asymbol::
  52. @* symbol handling functions::
  53. @end menu
  54.  
  55. INODE
  56. Reading Symbols, Writing Symbols, Symbols, Symbols
  57. SUBSECTION
  58.     Reading symbols
  59.  
  60.     There are two stages to reading a symbol table from a BFD:
  61.     allocating storage, and the actual reading process. This is an
  62.     excerpt from an application which reads the symbol table:
  63.  
  64. |      unsigned int storage_needed;
  65. |      asymbol **symbol_table;
  66. |      unsigned int number_of_symbols;
  67. |      unsigned int i;
  68. |    
  69. |      storage_needed = get_symtab_upper_bound (abfd);
  70. |    
  71. |      if (storage_needed == 0) {
  72. |         return ;
  73. |      }
  74. |      symbol_table = (asymbol **) bfd_xmalloc (storage_needed);
  75. |        ...
  76. |      number_of_symbols = 
  77. |         bfd_canonicalize_symtab (abfd, symbol_table); 
  78. |    
  79. |      for (i = 0; i < number_of_symbols; i++) {
  80. |         process_symbol (symbol_table[i]);
  81. |      }
  82.  
  83.     All storage for the symbols themselves is in an obstack
  84.     connected to the BFD; it is freed when the BFD is closed.
  85.  
  86.  
  87. INODE
  88. Writing Symbols, typedef asymbol, Reading Symbols, Symbols
  89. SUBSECTION
  90.     Writing symbols
  91.  
  92.     Writing of a symbol table is automatic when a BFD open for
  93.     writing is closed. The application attaches a vector of
  94.     pointers to pointers to symbols to the BFD being written, and
  95.     fills in the symbol count. The close and cleanup code reads
  96.     through the table provided and performs all the necessary
  97.     operations. The BFD output code must always be provided with an
  98.     ``owned'' symbol: one which has come from another BFD, or one
  99.     which has been created using <<bfd_make_empty_symbol>>.  Here is an
  100.     example showing the creation of a symbol table with only one element:
  101.  
  102. |    #include "bfd.h"
  103. |    main() 
  104. |    {
  105. |      bfd *abfd;
  106. |      asymbol *ptrs[2];
  107. |      asymbol *new;
  108. |    
  109. |      abfd = bfd_openw("foo","a.out-sunos-big");
  110. |      bfd_set_format(abfd, bfd_object);
  111. |      new = bfd_make_empty_symbol(abfd);
  112. |      new->name = "dummy_symbol";
  113. |      new->section = bfd_make_section_old_way(abfd, ".text");
  114. |      new->flags = BSF_GLOBAL;
  115. |      new->value = 0x12345;
  116. |    
  117. |      ptrs[0] = new;
  118. |      ptrs[1] = (asymbol *)0;
  119. |      
  120. |      bfd_set_symtab(abfd, ptrs, 1);
  121. |      bfd_close(abfd);
  122. |    }
  123. |    
  124. |    ./makesym 
  125. |    nm foo
  126. |    00012345 A dummy_symbol
  127.  
  128.     Many formats cannot represent arbitary symbol information; for
  129.      instance, the <<a.out>> object format does not allow an
  130.     arbitary number of sections. A symbol pointing to a section
  131.     which is not one  of <<.text>>, <<.data>> or <<.bss>> cannot
  132.     be described. 
  133.  
  134. */
  135.  
  136.  
  137.  
  138. /*
  139. DOCDD
  140. INODE
  141. typedef asymbol, symbol handling functions, Writing Symbols, Symbols
  142.  
  143. */
  144. /*
  145. SUBSECTION
  146.     typedef asymbol
  147.  
  148.     An <<asymbol>> has the form:
  149.  
  150. */
  151.  
  152. /*
  153. CODE_FRAGMENT
  154.  
  155. .
  156. .typedef struct symbol_cache_entry 
  157. .{
  158. .    {* A pointer to the BFD which owns the symbol. This information
  159. .       is necessary so that a back end can work out what additional
  160. .          information (invisible to the application writer) is carried
  161. .       with the symbol.
  162. .
  163. .       This field is *almost* redundant, since you can use section->owner
  164. .       instead, except that some symbols point to the global sections
  165. .       bfd_{abs,com,und}_section.  This could be fixed by making
  166. .       these globals be per-bfd (or per-target-flavor).  FIXME. *}
  167. .
  168. .  struct _bfd *the_bfd; {* Use bfd_asymbol_bfd(sym) to access this field. *}
  169. .
  170. .    {* The text of the symbol. The name is left alone, and not copied; the
  171. .       application may not alter it. *}
  172. .  CONST char *name;
  173. .
  174. .    {* The value of the symbol.  This really should be a union of a
  175. .          numeric value with a pointer, since some flags indicate that
  176. .          a pointer to another symbol is stored here.  *}
  177. .  symvalue value;
  178. .
  179. .    {* Attributes of a symbol: *}
  180. .
  181. .#define BSF_NO_FLAGS    0x00
  182. .
  183. .    {* The symbol has local scope; <<static>> in <<C>>. The value
  184. .        is the offset into the section of the data. *}
  185. .#define BSF_LOCAL    0x01
  186. .
  187. .    {* The symbol has global scope; initialized data in <<C>>. The
  188. .       value is the offset into the section of the data. *}
  189. .#define BSF_GLOBAL    0x02
  190. .
  191. .    {* The symbol has global scope and is exported. The value is
  192. .       the offset into the section of the data. *}
  193. .#define BSF_EXPORT    BSF_GLOBAL {* no real difference *}
  194. .
  195. .    {* A normal C symbol would be one of:
  196. .       <<BSF_LOCAL>>, <<BSF_FORT_COMM>>,  <<BSF_UNDEFINED>> or
  197. .       <<BSF_GLOBAL>> *}
  198. .
  199. .    {* The symbol is a debugging record. The value has an arbitary
  200. .       meaning. *}
  201. .#define BSF_DEBUGGING    0x08
  202. .
  203. .    {* The symbol denotes a function entry point.  Used in ELF,
  204. .       perhaps others someday.  *}
  205. .#define BSF_FUNCTION    0x10
  206. .
  207. .    {* Used by the linker. *}
  208. .#define BSF_KEEP        0x20
  209. .#define BSF_KEEP_G      0x40
  210. .
  211. .    {* A weak global symbol, overridable without warnings by
  212. .       a regular global symbol of the same name.  *}
  213. .#define BSF_WEAK        0x80
  214. .
  215. .       {* This symbol was created to point to a section, e.g. ELF's
  216. .       STT_SECTION symbols.  *}
  217. .#define BSF_SECTION_SYM 0x100
  218. .
  219. .    {* The symbol used to be a common symbol, but now it is
  220. .       allocated. *}
  221. .#define BSF_OLD_COMMON  0x200
  222. .
  223. .    {* The default value for common data. *}
  224. .#define BFD_FORT_COMM_DEFAULT_VALUE 0
  225. .
  226. .    {* In some files the type of a symbol sometimes alters its
  227. .       location in an output file - ie in coff a <<ISFCN>> symbol
  228. .       which is also <<C_EXT>> symbol appears where it was
  229. .       declared and not at the end of a section.  This bit is set
  230. .         by the target BFD part to convey this information. *}
  231. .
  232. .#define BSF_NOT_AT_END    0x400
  233. .
  234. .    {* Signal that the symbol is the label of constructor section. *}
  235. .#define BSF_CONSTRUCTOR   0x800
  236. .
  237. .    {* Signal that the symbol is a warning symbol. If the symbol
  238. .       is a warning symbol, then the value field (I know this is
  239. .       tacky) will point to the asymbol which when referenced will
  240. .       cause the warning. *}
  241. .#define BSF_WARNING       0x1000
  242. .
  243. .    {* Signal that the symbol is indirect. The value of the symbol
  244. .       is a pointer to an undefined asymbol which contains the
  245. .       name to use instead. *}
  246. .#define BSF_INDIRECT      0x2000
  247. .
  248. .    {* BSF_FILE marks symbols that contain a file name.  This is used
  249. .       for ELF STT_FILE symbols.  *}
  250. .#define BSF_FILE          0x4000
  251. .
  252. .  flagword flags;
  253. .
  254. .    {* A pointer to the section to which this symbol is 
  255. .       relative.  This will always be non NULL, there are special
  256. .          sections for undefined and absolute symbols *}
  257. .  struct sec *section;
  258. .
  259. .    {* Back end special data. This is being phased out in favour
  260. .       of making this a union. *}
  261. .  PTR udata;
  262. .
  263. .} asymbol;
  264. */
  265.  
  266. #include "bfd.h"
  267. #include "sysdep.h"
  268.  
  269. #include "libbfd.h"
  270. #include "aout/stab_gnu.h"
  271.  
  272. /*
  273. DOCDD
  274. INODE
  275. symbol handling functions,  , typedef asymbol, Symbols
  276. SUBSECTION
  277.     Symbol handling functions
  278. */
  279.  
  280. /*
  281. FUNCTION
  282.     get_symtab_upper_bound
  283.  
  284. DESCRIPTION
  285.     Return the number of bytes required to store a vector of pointers
  286.     to <<asymbols>> for all the symbols in the BFD @var{abfd},
  287.     including a terminal NULL pointer. If there are no symbols in
  288.     the BFD, then return 0.
  289.  
  290. .#define get_symtab_upper_bound(abfd) \
  291. .     BFD_SEND (abfd, _get_symtab_upper_bound, (abfd))
  292.  
  293. */
  294.  
  295. /*
  296. FUNCTION
  297.     bfd_canonicalize_symtab
  298.  
  299. DESCRIPTION
  300.     Read the symbols from the BFD @var{abfd}, and fills in
  301.     the vector @var{location} with pointers to the symbols and
  302.     a trailing NULL. 
  303.     Return the actual number of symbol pointers, not
  304.     including the NULL.
  305.  
  306.  
  307. .#define bfd_canonicalize_symtab(abfd, location) \
  308. .     BFD_SEND (abfd, _bfd_canonicalize_symtab,\
  309. .                  (abfd, location))
  310.  
  311. */
  312.  
  313.  
  314. /*
  315. FUNCTION
  316.     bfd_set_symtab
  317.  
  318. SYNOPSIS
  319.     boolean bfd_set_symtab (bfd *abfd, asymbol **location, unsigned int count);
  320.  
  321. DESCRIPTION
  322.     Arrange that when the output BFD @var{abfd} is closed,
  323.     the table @var{location} of @var{count} pointers to symbols
  324.     will be written.
  325. */
  326.  
  327. boolean
  328. bfd_set_symtab (abfd, location, symcount)
  329.      bfd *abfd;
  330.      asymbol **location;
  331.      unsigned int symcount;
  332. {
  333.   if ((abfd->format != bfd_object) || (bfd_read_p (abfd))) {
  334.     bfd_error = invalid_operation;
  335.     return false;
  336.   }
  337.  
  338.   bfd_get_outsymbols (abfd) = location;
  339.   bfd_get_symcount (abfd) = symcount;
  340.   return true;
  341. }
  342.  
  343. /*
  344. FUNCTION
  345.     bfd_print_symbol_vandf
  346.  
  347. SYNOPSIS
  348.     void bfd_print_symbol_vandf(PTR file, asymbol *symbol);
  349.  
  350. DESCRIPTION
  351.     Print the value and flags of the @var{symbol} supplied to the
  352.     stream @var{file}.
  353. */
  354. void
  355. DEFUN(bfd_print_symbol_vandf,(arg, symbol),
  356. PTR arg AND
  357. asymbol *symbol)
  358. {
  359.   FILE *file = (FILE *) arg;
  360.   flagword type = symbol->flags;
  361.   if (symbol->section != (asection *)NULL)
  362.       {
  363.     fprintf_vma(file, symbol->value+symbol->section->vma);
  364.       }
  365.   else 
  366.       {
  367.     fprintf_vma(file, symbol->value);
  368.       }
  369.   fprintf(file," %c%c%c%c%c%c%c",
  370.       (type & BSF_LOCAL)  ? 'l':' ',
  371.       (type & BSF_GLOBAL) ? 'g' : ' ',
  372.       (type & BSF_WEAK) ? 'w' : ' ',
  373.       (type & BSF_CONSTRUCTOR) ? 'C' : ' ',
  374.       (type & BSF_WARNING) ? 'W' : ' ',
  375.       (type & BSF_INDIRECT) ? 'I' : ' ',
  376.       (type & BSF_DEBUGGING) ? 'd' :' ');
  377.  
  378. }
  379.  
  380.  
  381. /*
  382. FUNCTION
  383.     bfd_make_empty_symbol
  384.  
  385. DESCRIPTION
  386.     Create a new <<asymbol>> structure for the BFD @var{abfd}
  387.     and return a pointer to it.
  388.  
  389.     This routine is necessary because each back end has private
  390.     information surrounding the <<asymbol>>. Building your own
  391.     <<asymbol>> and pointing to it will not create the private
  392.     information, and will cause problems later on.
  393.  
  394. .#define bfd_make_empty_symbol(abfd) \
  395. .     BFD_SEND (abfd, _bfd_make_empty_symbol, (abfd))
  396. */
  397.  
  398. /*
  399. FUNCTION
  400.     bfd_make_debug_symbol
  401.  
  402. DESCRIPTION
  403.     Create a new <<asymbol>> structure for the BFD @var{abfd},
  404.     to be used as a debugging symbol.  Further details of its use have
  405.     yet to be worked out.
  406.  
  407. .#define bfd_make_debug_symbol(abfd,ptr,size) \
  408. .        BFD_SEND (abfd, _bfd_make_debug_symbol, (abfd, ptr, size))
  409. */
  410.  
  411. struct section_to_type
  412. {
  413.   CONST char *section;
  414.   char type;
  415. };
  416.  
  417. /* Map section names to POSIX/BSD single-character symbol types.
  418.    This table is probably incomplete.  It is sorted for convenience of
  419.    adding entries.  Since it is so short, a linear search is used.  */
  420. static CONST struct section_to_type stt[] = {
  421.   {"*DEBUG*", 'N'},
  422.   {".bss", 'b'},
  423.   {".data", 'd'},
  424.   {".sbss", 's'},        /* Small BSS (uninitialized data) */
  425.   {".scommon", 'c'},        /* Small common */
  426.   {".sdata", 'g'},        /* Small initialized data */
  427.   {".text", 't'},
  428.   {0, 0}
  429. };
  430.  
  431. /* Return the single-character symbol type corresponding to
  432.    section S, or '?' for an unknown COFF section.  */
  433.  
  434. static char
  435. coff_section_type (s)
  436.      char *s;
  437. {
  438.   CONST struct section_to_type *t;
  439.  
  440.   for (t = &stt[0]; t->section; t++)
  441.     if (!strcmp (s, t->section))
  442.       return t->type;
  443.   return '?';
  444. }
  445.  
  446. #ifndef islower
  447. #define islower(c) ((c) >= 'a' && (c) <= 'z')
  448. #endif
  449. #ifndef toupper
  450. #define toupper(c) (islower(c) ? ((c) & ~0x20) : (c))
  451. #endif
  452.  
  453. /*
  454. FUNCTION
  455.     bfd_decode_symclass
  456.  
  457. DESCRIPTION
  458.     Return a character corresponding to the symbol
  459.     class of @var{symbol}, or '?' for an unknown class.
  460.  
  461. SYNOPSIS
  462.     int bfd_decode_symclass(asymbol *symbol);
  463. */
  464. int
  465. DEFUN(bfd_decode_symclass,(symbol),
  466. asymbol *symbol)
  467. {
  468.   char c;
  469.  
  470.   if (bfd_is_com_section (symbol->section))
  471.     return 'C';
  472.   if (symbol->section == &bfd_und_section)
  473.     return 'U';
  474.   if (symbol->section == &bfd_ind_section)
  475.     return 'I';
  476.   if (!(symbol->flags & (BSF_GLOBAL|BSF_LOCAL)))
  477.     return '?';
  478.  
  479.   if (symbol->section == &bfd_abs_section)
  480.     c = 'a';
  481.   else if (symbol->section)
  482.     c = coff_section_type (symbol->section->name);
  483.   else
  484.     return '?';
  485.   if (symbol->flags & BSF_GLOBAL)
  486.     c = toupper (c);
  487.   return c;
  488.  
  489.   /* We don't have to handle these cases just yet, but we will soon:
  490.      N_SETV: 'v'; 
  491.      N_SETA: 'l'; 
  492.      N_SETT: 'x';
  493.      N_SETD: 'z';
  494.      N_SETB: 's';
  495.      N_INDR: 'i';
  496.      */
  497. }
  498.  
  499. /*
  500. FUNCTION
  501.     bfd_symbol_info
  502.  
  503. DESCRIPTION
  504.     Fill in the basic info about symbol that nm needs.
  505.     Additional info may be added by the back-ends after
  506.     calling this function.
  507.  
  508. SYNOPSIS
  509.     void bfd_symbol_info(asymbol *symbol, symbol_info *ret);
  510. */
  511.  
  512. void
  513. DEFUN(bfd_symbol_info,(symbol, ret),
  514.       asymbol *symbol AND
  515.       symbol_info *ret)
  516. {
  517.   ret->type = bfd_decode_symclass (symbol);
  518.   if (ret->type != 'U')
  519.     ret->value = symbol->value+symbol->section->vma;
  520.   else
  521.     ret->value = 0;
  522.   ret->name = symbol->name;
  523. }
  524.  
  525. void
  526. bfd_symbol_is_absolute()
  527. {
  528.   abort();
  529. }
  530.  
  531.