0baf177ffb0c4fff2f4fc818b41872f29e6922db
[external/binutils.git] / gdb / nlmread.c
1 /* Read NLM (NetWare Loadable Module) format executable files for GDB.
2    Copyright 1993, 1994, 1995, 1996, 1998, 1999, 2000
3    Free Software Foundation, Inc.
4    Written by Fred Fish at Cygnus Support (fnf@cygnus.com).
5
6    This file is part of GDB.
7
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
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
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
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 #include "defs.h"
24 #include "bfd.h"
25 #include "symtab.h"
26 #include "symfile.h"
27 #include "objfiles.h"
28 #include "buildsym.h"
29 #include "stabsread.h"
30
31 extern void _initialize_nlmread (void);
32
33 static void nlm_new_init (struct objfile *);
34
35 static void nlm_symfile_init (struct objfile *);
36
37 static void nlm_symfile_read (struct objfile *, int);
38
39 static void nlm_symfile_finish (struct objfile *);
40
41 static void nlm_symtab_read (bfd *, CORE_ADDR, struct objfile *);
42
43 /* Initialize anything that needs initializing when a completely new symbol
44    file is specified (not just adding some symbols from another file, e.g. a
45    shared library).
46
47    We reinitialize buildsym, since gdb will be able to read stabs from an NLM
48    file at some point in the near future.  */
49
50 static void
51 nlm_new_init (struct objfile *ignore)
52 {
53   stabsread_new_init ();
54   buildsym_new_init ();
55 }
56
57
58 /* NLM specific initialization routine for reading symbols.
59
60    It is passed a pointer to a struct sym_fns which contains, among other
61    things, the BFD for the file whose symbols are being read, and a slot for
62    a pointer to "private data" which we can fill with goodies.
63
64    For now at least, we have nothing in particular to do, so this function is
65    just a stub. */
66
67 static void
68 nlm_symfile_init (struct objfile *ignore)
69 {
70 }
71
72 /*
73
74    LOCAL FUNCTION
75
76    nlm_symtab_read -- read the symbol table of an NLM file
77
78    SYNOPSIS
79
80    void nlm_symtab_read (bfd *abfd, CORE_ADDR addr,
81    struct objfile *objfile)
82
83    DESCRIPTION
84
85    Given an open bfd, a base address to relocate symbols to, and a
86    flag that specifies whether or not this bfd is for an executable
87    or not (may be shared library for example), add all the global
88    function and data symbols to the minimal symbol table.
89  */
90
91 static void
92 nlm_symtab_read (bfd *abfd, CORE_ADDR addr, struct objfile *objfile)
93 {
94   long storage_needed;
95   asymbol *sym;
96   asymbol **symbol_table;
97   long number_of_symbols;
98   long i;
99   struct cleanup *back_to;
100   CORE_ADDR symaddr;
101   enum minimal_symbol_type ms_type;
102
103   storage_needed = bfd_get_symtab_upper_bound (abfd);
104   if (storage_needed < 0)
105     error ("Can't read symbols from %s: %s", bfd_get_filename (abfd),
106            bfd_errmsg (bfd_get_error ()));
107   if (storage_needed > 0)
108     {
109       symbol_table = (asymbol **) xmalloc (storage_needed);
110       back_to = make_cleanup (xfree, symbol_table);
111       number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
112       if (number_of_symbols < 0)
113         error ("Can't read symbols from %s: %s", bfd_get_filename (abfd),
114                bfd_errmsg (bfd_get_error ()));
115
116       for (i = 0; i < number_of_symbols; i++)
117         {
118           sym = symbol_table[i];
119           if ( /*sym -> flags & BSF_GLOBAL */ 1)
120             {
121               /* Bfd symbols are section relative. */
122               symaddr = sym->value + sym->section->vma;
123               /* Relocate all non-absolute symbols by base address.  */
124               if (sym->section != &bfd_abs_section)
125                 symaddr += addr;
126
127               /* For non-absolute symbols, use the type of the section
128                  they are relative to, to intuit text/data.  BFD provides
129                  no way of figuring this out for absolute symbols. */
130               if (sym->section->flags & SEC_CODE)
131                 ms_type = mst_text;
132               else if (sym->section->flags & SEC_DATA)
133                 ms_type = mst_data;
134               else
135                 ms_type = mst_unknown;
136
137               prim_record_minimal_symbol (sym->name, symaddr, ms_type,
138                                           objfile);
139             }
140         }
141       do_cleanups (back_to);
142     }
143 }
144
145
146 /* Scan and build partial symbols for a symbol file.
147    We have been initialized by a call to nlm_symfile_init, which 
148    currently does nothing.
149
150    SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
151    in each section.  We simplify it down to a single offset for all
152    symbols.  FIXME.
153
154    MAINLINE is true if we are reading the main symbol
155    table (as opposed to a shared lib or dynamically loaded file).
156
157    This function only does the minimum work necessary for letting the
158    user "name" things symbolically; it does not read the entire symtab.
159    Instead, it reads the external and static symbols and puts them in partial
160    symbol tables.  When more extensive information is requested of a
161    file, the corresponding partial symbol table is mutated into a full
162    fledged symbol table by going back and reading the symbols
163    for real.
164
165    Note that NLM files have two sets of information that is potentially
166    useful for building gdb's minimal symbol table.  The first is a list
167    of the publically exported symbols, and is currently used to build
168    bfd's canonical symbol table.  The second is an optional native debugging
169    format which contains additional symbols (and possibly duplicates of
170    the publically exported symbols).  The optional native debugging format
171    is not currently used. */
172
173 static void
174 nlm_symfile_read (struct objfile *objfile, int mainline)
175 {
176   bfd *abfd = objfile->obfd;
177   struct cleanup *back_to;
178   CORE_ADDR offset;
179   struct symbol *mainsym;
180
181   init_minimal_symbol_collection ();
182   back_to = make_cleanup_discard_minimal_symbols ();
183
184   /* FIXME, should take a section_offsets param, not just an offset.  */
185
186   offset = ANOFFSET (objfile->section_offsets, 0);
187
188   /* Process the NLM export records, which become the bfd's canonical symbol
189      table. */
190
191   nlm_symtab_read (abfd, offset, objfile);
192
193   stabsect_build_psymtabs (objfile, mainline, ".stab",
194                            ".stabstr", ".text");
195
196   mainsym = lookup_symbol ("main", NULL, VAR_NAMESPACE, NULL, NULL);
197
198   if (mainsym
199       && SYMBOL_CLASS (mainsym) == LOC_BLOCK)
200     {
201       objfile->ei.main_func_lowpc = BLOCK_START (SYMBOL_BLOCK_VALUE (mainsym));
202       objfile->ei.main_func_highpc = BLOCK_END (SYMBOL_BLOCK_VALUE (mainsym));
203     }
204
205   /* FIXME:  We could locate and read the optional native debugging format
206      here and add the symbols to the minimal symbol table. */
207
208   /* Install any minimal symbols that have been collected as the current
209      minimal symbols for this objfile. */
210
211   install_minimal_symbols (objfile);
212
213   do_cleanups (back_to);
214 }
215
216
217 /* Perform any local cleanups required when we are done with a particular
218    objfile.  I.E, we are in the process of discarding all symbol information
219    for an objfile, freeing up all memory held for it, and unlinking the
220    objfile struct from the global list of known objfiles. */
221
222 static void
223 nlm_symfile_finish (struct objfile *objfile)
224 {
225   if (objfile->sym_private != NULL)
226     {
227       mfree (objfile->md, objfile->sym_private);
228     }
229 }
230
231 /* Register that we are able to handle NLM file format. */
232
233 static struct sym_fns nlm_sym_fns =
234 {
235   bfd_target_nlm_flavour,
236   nlm_new_init,                 /* sym_new_init: init anything gbl to entire symtab */
237   nlm_symfile_init,             /* sym_init: read initial info, setup for sym_read() */
238   nlm_symfile_read,             /* sym_read: read a symbol file into symtab */
239   nlm_symfile_finish,           /* sym_finish: finished with file, cleanup */
240   default_symfile_offsets,      /* sym_offsets:  Translate ext. to int. relocation */
241   NULL                          /* next: pointer to next struct sym_fns */
242 };
243
244 void
245 _initialize_nlmread (void)
246 {
247   add_symtab_fns (&nlm_sym_fns);
248 }