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