70328c9f4e6600ec65880facd197ff8778f56933
[external/binutils.git] / gdb / nlmread.c
1 /* Read NLM (NetWare Loadable Module) format executable files for GDB.
2    Copyright 1993 Free Software Foundation, Inc.
3    Written by Fred Fish at Cygnus Support (fnf@cygnus.com).
4
5 This file is part of GDB.
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 #include "defs.h"
22 #include "bfd.h"
23 #include "symtab.h"
24 #include "symfile.h"
25 #include "objfiles.h"
26 #include "gdb-stabs.h"
27 #include "buildsym.h"
28 #include "stabsread.h"
29
30 static void
31 nlm_new_init PARAMS ((struct objfile *));
32
33 static void
34 nlm_symfile_init PARAMS ((struct objfile *));
35
36 static void
37 nlm_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));
38
39 static void
40 nlm_symfile_finish PARAMS ((struct objfile *));
41
42 static void
43 nlm_symtab_read PARAMS ((bfd *,  CORE_ADDR, struct objfile *));
44
45 static struct section_offsets *
46 nlm_symfile_offsets PARAMS ((struct objfile *, CORE_ADDR));
47
48 static void
49 record_minimal_symbol PARAMS ((char *, CORE_ADDR, enum minimal_symbol_type,
50                                struct objfile *));
51
52
53 /* Initialize anything that needs initializing when a completely new symbol
54    file is specified (not just adding some symbols from another file, e.g. a
55    shared library).
56
57    We reinitialize buildsym, since gdb will be able to read stabs from an NLM
58    file at some point in the near future.  */
59
60 static void
61 nlm_new_init (ignore)
62      struct objfile *ignore;
63 {
64   stabsread_new_init ();
65   buildsym_new_init ();
66 }
67
68
69 /* NLM specific initialization routine for reading symbols.
70
71    It is passed a pointer to a struct sym_fns which contains, among other
72    things, the BFD for the file whose symbols are being read, and a slot for
73    a pointer to "private data" which we can fill with goodies.
74
75    For now at least, we have nothing in particular to do, so this function is
76    just a stub. */
77
78 static void
79 nlm_symfile_init (ignore)
80      struct objfile *ignore;
81 {
82 }
83
84 static void
85 record_minimal_symbol (name, address, ms_type, objfile)
86      char *name;
87      CORE_ADDR address;
88      enum minimal_symbol_type ms_type;
89      struct objfile *objfile;
90 {
91   name = obsavestring (name, strlen (name), &objfile -> symbol_obstack);
92   prim_record_minimal_symbol (name, address, ms_type);
93 }
94
95
96 /*
97
98 LOCAL FUNCTION
99
100         nlm_symtab_read -- read the symbol table of an NLM file
101
102 SYNOPSIS
103
104         void nlm_symtab_read (bfd *abfd, CORE_ADDR addr,
105                               struct objfile *objfile)
106
107 DESCRIPTION
108
109         Given an open bfd, a base address to relocate symbols to, and a
110         flag that specifies whether or not this bfd is for an executable
111         or not (may be shared library for example), add all the global
112         function and data symbols to the minimal symbol table.
113 */
114
115 static void
116 nlm_symtab_read (abfd, addr, objfile)
117      bfd *abfd;
118      CORE_ADDR addr;
119      struct objfile *objfile;
120 {
121   unsigned int storage_needed;
122   asymbol *sym;
123   asymbol **symbol_table;
124   unsigned int number_of_symbols;
125   unsigned int i;
126   struct cleanup *back_to;
127   CORE_ADDR symaddr;
128   enum minimal_symbol_type ms_type;
129   
130   storage_needed = get_symtab_upper_bound (abfd);
131   if (storage_needed > 0)
132     {
133       symbol_table = (asymbol **) xmalloc (storage_needed);
134       back_to = make_cleanup (free, symbol_table);
135       number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); 
136   
137       for (i = 0; i < number_of_symbols; i++)
138         {
139           sym = symbol_table[i];
140           if (sym -> flags & BSF_GLOBAL)
141             {
142               /* Bfd symbols are section relative. */
143               symaddr = sym -> value + sym -> section -> vma;
144               /* Relocate all non-absolute symbols by base address.  */
145               if (sym -> section != &bfd_abs_section)
146                 {
147                   symaddr += addr;
148                 }
149
150               /* For non-absolute symbols, use the type of the section
151                  they are relative to, to intuit text/data.  Bfd provides
152                  no way of figuring this out for absolute symbols. */
153               if (sym -> section -> flags & SEC_CODE)
154                 {
155                   ms_type = mst_text;
156                 }
157               else if (sym -> section -> flags & SEC_DATA)
158                 {
159                   ms_type = mst_data;
160                 }
161               else
162                 {
163                   ms_type = mst_unknown;
164                 }
165               record_minimal_symbol ((char *) sym -> name, symaddr, ms_type,
166                                      objfile);
167             }
168         }
169       do_cleanups (back_to);
170     }
171 }
172
173
174 /* Scan and build partial symbols for a symbol file.
175    We have been initialized by a call to nlm_symfile_init, which 
176    currently does nothing.
177
178    SECTION_OFFSETS is a set of offsets to apply to relocate the symbols
179    in each section.  We simplify it down to a single offset for all
180    symbols.  FIXME.
181
182    MAINLINE is true if we are reading the main symbol
183    table (as opposed to a shared lib or dynamically loaded file).
184
185    This function only does the minimum work necessary for letting the
186    user "name" things symbolically; it does not read the entire symtab.
187    Instead, it reads the external and static symbols and puts them in partial
188    symbol tables.  When more extensive information is requested of a
189    file, the corresponding partial symbol table is mutated into a full
190    fledged symbol table by going back and reading the symbols
191    for real.
192
193    Note that NLM files have two sets of information that is potentially
194    useful for building gdb's minimal symbol table.  The first is a list
195    of the publically exported symbols, and is currently used to build
196    bfd's canonical symbol table.  The second is an optional native debugging
197    format which contains additional symbols (and possibly duplicates of
198    the publically exported symbols).  The optional native debugging format
199    is not currently used. */
200
201 static void
202 nlm_symfile_read (objfile, section_offsets, mainline)
203      struct objfile *objfile;
204      struct section_offsets *section_offsets;
205      int mainline;
206 {
207   bfd *abfd = objfile -> obfd;
208   struct cleanup *back_to;
209   CORE_ADDR offset;
210
211   init_minimal_symbol_collection ();
212   back_to = make_cleanup (discard_minimal_symbols, 0);
213
214   /* FIXME, should take a section_offsets param, not just an offset.  */
215
216   offset = ANOFFSET (section_offsets, 0);
217
218   /* Process the NLM export records, which become the bfd's canonical symbol
219      table. */
220
221   nlm_symtab_read (abfd, offset, objfile);
222
223   /* FIXME:  We could locate and read the optional native debugging format
224      here and add the symbols to the minimal symbol table. */
225
226   if (!have_partial_symbols ())
227     {
228       wrap_here ("");
229       printf_filtered ("(no debugging symbols found)...");
230       wrap_here ("");
231     }
232
233   /* Install any minimal symbols that have been collected as the current
234      minimal symbols for this objfile. */
235
236   install_minimal_symbols (objfile);
237
238   do_cleanups (back_to);
239 }
240
241
242 /* Perform any local cleanups required when we are done with a particular
243    objfile.  I.E, we are in the process of discarding all symbol information
244    for an objfile, freeing up all memory held for it, and unlinking the
245    objfile struct from the global list of known objfiles. */
246
247 static void
248 nlm_symfile_finish (objfile)
249      struct objfile *objfile;
250 {
251   if (objfile -> sym_private != NULL)
252     {
253       mfree (objfile -> md, objfile -> sym_private);
254     }
255 }
256
257 /* NLM specific parsing routine for section offsets.
258    FIXME:  This may or may not be necessary.  All the symbol readers seem
259    to have similar code.  See if it can be generalized and moved elsewhere. */
260
261 static
262 struct section_offsets *
263 nlm_symfile_offsets (objfile, addr)
264      struct objfile *objfile;
265      CORE_ADDR addr;
266 {
267   struct section_offsets *section_offsets;
268   int i;
269  
270   section_offsets = (struct section_offsets *)
271     obstack_alloc (&objfile -> psymbol_obstack,
272                    sizeof (struct section_offsets) +
273                    sizeof (section_offsets->offsets) * (SECT_OFF_MAX-1));
274
275   for (i = 0; i < SECT_OFF_MAX; i++)
276     {
277       ANOFFSET (section_offsets, i) = addr;
278     }
279   
280   return (section_offsets);
281 }
282
283 \f
284 /*  Register that we are able to handle NLM file format. */
285
286 static struct sym_fns nlm_sym_fns =
287 {
288   "nlm",                /* sym_name: name or name prefix of BFD target type */
289   3,                    /* sym_namelen: number of significant sym_name chars */
290   nlm_new_init,         /* sym_new_init: init anything gbl to entire symtab */
291   nlm_symfile_init,     /* sym_init: read initial info, setup for sym_read() */
292   nlm_symfile_read,     /* sym_read: read a symbol file into symtab */
293   nlm_symfile_finish,   /* sym_finish: finished with file, cleanup */
294   nlm_symfile_offsets,  /* sym_offsets:  Translate ext. to int. relocation */
295   NULL                  /* next: pointer to next struct sym_fns */
296 };
297
298 void
299 _initialize_nlmread ()
300 {
301   add_symtab_fns (&nlm_sym_fns);
302 }