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