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