Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Read HP PA/Risc object files for GDB. |
28e7fd62 | 2 | Copyright (C) 1991-2013 Free Software Foundation, Inc. |
c906108c SS |
3 | Written by Fred Fish at Cygnus Support. |
4 | ||
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 10 | (at your option) any later version. |
c906108c | 11 | |
c5aa993b JM |
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. | |
c906108c | 16 | |
c5aa993b | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
19 | |
20 | #include "defs.h" | |
21 | #include "bfd.h" | |
22 | #include <syms.h> | |
23 | #include "symtab.h" | |
24 | #include "symfile.h" | |
25 | #include "objfiles.h" | |
26 | #include "buildsym.h" | |
27 | #include "stabsread.h" | |
28 | #include "gdb-stabs.h" | |
29 | #include "complaints.h" | |
30 | #include "gdb_string.h" | |
31 | #include "demangle.h" | |
32 | #include "som.h" | |
33 | #include "libhppa.h" | |
ccefe4c4 | 34 | #include "psymtab.h" |
c906108c | 35 | |
17fe2d6e | 36 | #include "solib-som.h" |
c906108c | 37 | |
7f86f058 | 38 | /* Read the symbol table of a SOM file. |
c906108c | 39 | |
c5aa993b JM |
40 | Given an open bfd, a base address to relocate symbols to, and a |
41 | flag that specifies whether or not this bfd is for an executable | |
42 | or not (may be shared library for example), add all the global | |
7f86f058 | 43 | function and data symbols to the minimal symbol table. */ |
c906108c SS |
44 | |
45 | static void | |
fba45db2 KB |
46 | som_symtab_read (bfd *abfd, struct objfile *objfile, |
47 | struct section_offsets *section_offsets) | |
c906108c | 48 | { |
5e2b427d | 49 | struct gdbarch *gdbarch = get_objfile_arch (objfile); |
c906108c SS |
50 | unsigned int number_of_symbols; |
51 | int val, dynamic; | |
52 | char *stringtab; | |
53 | asection *shlib_info; | |
54 | struct symbol_dictionary_record *buf, *bufp, *endbufp; | |
55 | char *symname; | |
56 | CONST int symsize = sizeof (struct symbol_dictionary_record); | |
57 | CORE_ADDR text_offset, data_offset; | |
58 | ||
59 | ||
60 | text_offset = ANOFFSET (section_offsets, 0); | |
61 | data_offset = ANOFFSET (section_offsets, 1); | |
62 | ||
63 | number_of_symbols = bfd_get_symcount (abfd); | |
64 | ||
f31b3751 JB |
65 | /* Allocate a buffer to read in the debug info. |
66 | We avoid using alloca because the memory size could be so large | |
67 | that we could hit the stack size limit. */ | |
68 | buf = xmalloc (symsize * number_of_symbols); | |
69 | make_cleanup (xfree, buf); | |
c906108c | 70 | bfd_seek (abfd, obj_som_sym_filepos (abfd), SEEK_SET); |
3a42e9d0 | 71 | val = bfd_bread (buf, symsize * number_of_symbols, abfd); |
c906108c | 72 | if (val != symsize * number_of_symbols) |
8a3fe4f8 | 73 | error (_("Couldn't read symbol dictionary!")); |
c906108c | 74 | |
f31b3751 JB |
75 | /* Allocate a buffer to read in the som stringtab section of |
76 | the debugging info. Again, we avoid using alloca because | |
77 | the data could be so large that we could potentially hit | |
78 | the stack size limitat. */ | |
79 | stringtab = xmalloc (obj_som_stringtab_size (abfd)); | |
80 | make_cleanup (xfree, stringtab); | |
c906108c | 81 | bfd_seek (abfd, obj_som_str_filepos (abfd), SEEK_SET); |
3a42e9d0 | 82 | val = bfd_bread (stringtab, obj_som_stringtab_size (abfd), abfd); |
c906108c | 83 | if (val != obj_som_stringtab_size (abfd)) |
8a3fe4f8 | 84 | error (_("Can't read in HP string table.")); |
c906108c SS |
85 | |
86 | /* We need to determine if objfile is a dynamic executable (so we | |
87 | can do the right thing for ST_ENTRY vs ST_CODE symbols). | |
88 | ||
89 | There's nothing in the header which easily allows us to do | |
3fa41cdb JL |
90 | this. |
91 | ||
92 | This code used to rely upon the existence of a $SHLIB_INFO$ | |
93 | section to make this determination. HP claims that it is | |
94 | more accurate to check for a nonzero text offset, but they | |
95 | have not provided any information about why that test is | |
96 | more accurate. */ | |
c906108c SS |
97 | dynamic = (text_offset != 0); |
98 | ||
99 | endbufp = buf + number_of_symbols; | |
100 | for (bufp = buf; bufp < endbufp; ++bufp) | |
101 | { | |
102 | enum minimal_symbol_type ms_type; | |
103 | ||
104 | QUIT; | |
105 | ||
106 | switch (bufp->symbol_scope) | |
107 | { | |
108 | case SS_UNIVERSAL: | |
109 | case SS_EXTERNAL: | |
110 | switch (bufp->symbol_type) | |
111 | { | |
112 | case ST_SYM_EXT: | |
113 | case ST_ARG_EXT: | |
114 | continue; | |
115 | ||
116 | case ST_CODE: | |
117 | case ST_PRI_PROG: | |
118 | case ST_SEC_PROG: | |
119 | case ST_MILLICODE: | |
120 | symname = bufp->name.n_strx + stringtab; | |
121 | ms_type = mst_text; | |
122 | bufp->symbol_value += text_offset; | |
85ddcc70 | 123 | bufp->symbol_value = gdbarch_addr_bits_remove |
5e2b427d | 124 | (gdbarch, bufp->symbol_value); |
c906108c SS |
125 | break; |
126 | ||
127 | case ST_ENTRY: | |
128 | symname = bufp->name.n_strx + stringtab; | |
129 | /* For a dynamic executable, ST_ENTRY symbols are | |
c5aa993b JM |
130 | the stubs, while the ST_CODE symbol is the real |
131 | function. */ | |
c906108c SS |
132 | if (dynamic) |
133 | ms_type = mst_solib_trampoline; | |
134 | else | |
135 | ms_type = mst_text; | |
136 | bufp->symbol_value += text_offset; | |
85ddcc70 | 137 | bufp->symbol_value = gdbarch_addr_bits_remove |
5e2b427d | 138 | (gdbarch, bufp->symbol_value); |
c906108c SS |
139 | break; |
140 | ||
141 | case ST_STUB: | |
142 | symname = bufp->name.n_strx + stringtab; | |
143 | ms_type = mst_solib_trampoline; | |
144 | bufp->symbol_value += text_offset; | |
85ddcc70 | 145 | bufp->symbol_value = gdbarch_addr_bits_remove |
5e2b427d | 146 | (gdbarch, bufp->symbol_value); |
c906108c SS |
147 | break; |
148 | ||
149 | case ST_DATA: | |
150 | symname = bufp->name.n_strx + stringtab; | |
151 | bufp->symbol_value += data_offset; | |
152 | ms_type = mst_data; | |
153 | break; | |
154 | default: | |
155 | continue; | |
156 | } | |
157 | break; | |
158 | ||
159 | #if 0 | |
160 | /* SS_GLOBAL and SS_LOCAL are two names for the same thing (!). */ | |
161 | case SS_GLOBAL: | |
162 | #endif | |
163 | case SS_LOCAL: | |
164 | switch (bufp->symbol_type) | |
165 | { | |
166 | case ST_SYM_EXT: | |
167 | case ST_ARG_EXT: | |
168 | continue; | |
169 | ||
170 | case ST_CODE: | |
171 | symname = bufp->name.n_strx + stringtab; | |
172 | ms_type = mst_file_text; | |
173 | bufp->symbol_value += text_offset; | |
85ddcc70 | 174 | bufp->symbol_value = gdbarch_addr_bits_remove |
5e2b427d | 175 | (gdbarch, bufp->symbol_value); |
c906108c SS |
176 | |
177 | check_strange_names: | |
178 | /* Utah GCC 2.5, FSF GCC 2.6 and later generate correct local | |
c5aa993b JM |
179 | label prefixes for stabs, constant data, etc. So we need |
180 | only filter out L$ symbols which are left in due to | |
181 | limitations in how GAS generates SOM relocations. | |
182 | ||
183 | When linking in the HPUX C-library the HP linker has | |
184 | the nasty habit of placing section symbols from the literal | |
185 | subspaces in the middle of the program's text. Filter | |
186 | those out as best we can. Check for first and last character | |
c378eb4e | 187 | being '$'. |
c5aa993b JM |
188 | |
189 | And finally, the newer HP compilers emit crud like $PIC_foo$N | |
190 | in some circumstance (PIC code I guess). It's also claimed | |
191 | that they emit D$ symbols too. What stupidity. */ | |
c906108c | 192 | if ((symname[0] == 'L' && symname[1] == '$') |
c5aa993b | 193 | || (symname[0] == '$' && symname[strlen (symname) - 1] == '$') |
c906108c | 194 | || (symname[0] == 'D' && symname[1] == '$') |
b887c273 | 195 | || (strncmp (symname, "L0\001", 3) == 0) |
c906108c SS |
196 | || (strncmp (symname, "$PIC", 4) == 0)) |
197 | continue; | |
198 | break; | |
199 | ||
200 | case ST_PRI_PROG: | |
201 | case ST_SEC_PROG: | |
202 | case ST_MILLICODE: | |
203 | symname = bufp->name.n_strx + stringtab; | |
204 | ms_type = mst_file_text; | |
205 | bufp->symbol_value += text_offset; | |
85ddcc70 | 206 | bufp->symbol_value = gdbarch_addr_bits_remove |
5e2b427d | 207 | (gdbarch, bufp->symbol_value); |
c906108c SS |
208 | break; |
209 | ||
210 | case ST_ENTRY: | |
211 | symname = bufp->name.n_strx + stringtab; | |
3fa41cdb JL |
212 | /* SS_LOCAL symbols in a shared library do not have |
213 | export stubs, so we do not have to worry about | |
214 | using mst_file_text vs mst_solib_trampoline here like | |
215 | we do for SS_UNIVERSAL and SS_EXTERNAL symbols above. */ | |
216 | ms_type = mst_file_text; | |
c906108c | 217 | bufp->symbol_value += text_offset; |
85ddcc70 | 218 | bufp->symbol_value = gdbarch_addr_bits_remove |
5e2b427d | 219 | (gdbarch, bufp->symbol_value); |
c906108c SS |
220 | break; |
221 | ||
222 | case ST_STUB: | |
223 | symname = bufp->name.n_strx + stringtab; | |
224 | ms_type = mst_solib_trampoline; | |
225 | bufp->symbol_value += text_offset; | |
85ddcc70 | 226 | bufp->symbol_value = gdbarch_addr_bits_remove |
5e2b427d | 227 | (gdbarch, bufp->symbol_value); |
c906108c SS |
228 | break; |
229 | ||
230 | ||
231 | case ST_DATA: | |
232 | symname = bufp->name.n_strx + stringtab; | |
233 | bufp->symbol_value += data_offset; | |
234 | ms_type = mst_file_data; | |
235 | goto check_strange_names; | |
236 | ||
237 | default: | |
238 | continue; | |
239 | } | |
240 | break; | |
241 | ||
c5aa993b JM |
242 | /* This can happen for common symbols when -E is passed to the |
243 | final link. No idea _why_ that would make the linker force | |
244 | common symbols to have an SS_UNSAT scope, but it does. | |
c906108c | 245 | |
c5aa993b JM |
246 | This also happens for weak symbols, but their type is |
247 | ST_DATA. */ | |
c906108c SS |
248 | case SS_UNSAT: |
249 | switch (bufp->symbol_type) | |
250 | { | |
c5aa993b JM |
251 | case ST_STORAGE: |
252 | case ST_DATA: | |
253 | symname = bufp->name.n_strx + stringtab; | |
254 | bufp->symbol_value += data_offset; | |
255 | ms_type = mst_data; | |
256 | break; | |
257 | ||
258 | default: | |
259 | continue; | |
c906108c SS |
260 | } |
261 | break; | |
262 | ||
263 | default: | |
264 | continue; | |
265 | } | |
266 | ||
267 | if (bufp->name.n_strx > obj_som_stringtab_size (abfd)) | |
8a3fe4f8 | 268 | error (_("Invalid symbol data; bad HP string table offset: %d"), |
c906108c SS |
269 | bufp->name.n_strx); |
270 | ||
c5aa993b | 271 | prim_record_minimal_symbol (symname, bufp->symbol_value, ms_type, |
c906108c SS |
272 | objfile); |
273 | } | |
274 | } | |
275 | ||
276 | /* Scan and build partial symbols for a symbol file. | |
277 | We have been initialized by a call to som_symfile_init, which | |
278 | currently does nothing. | |
279 | ||
280 | SECTION_OFFSETS is a set of offsets to apply to relocate the symbols | |
281 | in each section. This is ignored, as it isn't needed for SOM. | |
282 | ||
c906108c SS |
283 | This function only does the minimum work necessary for letting the |
284 | user "name" things symbolically; it does not read the entire symtab. | |
285 | Instead, it reads the external and static symbols and puts them in partial | |
286 | symbol tables. When more extensive information is requested of a | |
287 | file, the corresponding partial symbol table is mutated into a full | |
288 | fledged symbol table by going back and reading the symbols | |
289 | for real. | |
290 | ||
291 | We look for sections with specific names, to tell us what debug | |
292 | format to look for: FIXME!!! | |
293 | ||
294 | somstab_build_psymtabs() handles STABS symbols. | |
295 | ||
296 | Note that SOM files have a "minimal" symbol table, which is vaguely | |
297 | reminiscent of a COFF symbol table, but has only the minimal information | |
298 | necessary for linking. We process this also, and use the information to | |
299 | build gdb's minimal symbol table. This gives us some minimal debugging | |
300 | capability even for files compiled without -g. */ | |
301 | ||
302 | static void | |
f4352531 | 303 | som_symfile_read (struct objfile *objfile, int symfile_flags) |
c906108c SS |
304 | { |
305 | bfd *abfd = objfile->obfd; | |
306 | struct cleanup *back_to; | |
307 | ||
c906108c | 308 | init_minimal_symbol_collection (); |
56e290f4 | 309 | back_to = make_cleanup_discard_minimal_symbols (); |
c906108c | 310 | |
c378eb4e | 311 | /* Process the normal SOM symbol table first. |
c906108c | 312 | This reads in the DNTT and string table, but doesn't |
c378eb4e MS |
313 | actually scan the DNTT. It does scan the linker symbol |
314 | table and thus build up a "minimal symbol table". */ | |
c5aa993b | 315 | |
96baa820 | 316 | som_symtab_read (abfd, objfile, objfile->section_offsets); |
c906108c | 317 | |
7134143f | 318 | /* Install any minimal symbols that have been collected as the current |
c378eb4e | 319 | minimal symbols for this objfile. |
7134143f | 320 | Further symbol-reading is done incrementally, file-by-file, |
c378eb4e MS |
321 | in a step known as "psymtab-to-symtab" expansion. hp-symtab-read.c |
322 | contains the code to do the actual DNTT scanning and symtab building. */ | |
7134143f DJ |
323 | install_minimal_symbols (objfile); |
324 | do_cleanups (back_to); | |
325 | ||
c906108c | 326 | /* Now read information from the stabs debug sections. |
4897bfb9 | 327 | This is emitted by gcc. */ |
c67a9c90 | 328 | stabsect_build_psymtabs (objfile, |
c906108c | 329 | "$GDB_SYMBOLS$", "$GDB_STRINGS$", "$TEXT$"); |
c906108c SS |
330 | } |
331 | ||
332 | /* Initialize anything that needs initializing when a completely new symbol | |
333 | file is specified (not just adding some symbols from another file, e.g. a | |
334 | shared library). | |
335 | ||
336 | We reinitialize buildsym, since we may be reading stabs from a SOM file. */ | |
337 | ||
338 | static void | |
fba45db2 | 339 | som_new_init (struct objfile *ignore) |
c906108c SS |
340 | { |
341 | stabsread_new_init (); | |
342 | buildsym_new_init (); | |
343 | } | |
344 | ||
345 | /* Perform any local cleanups required when we are done with a particular | |
c378eb4e | 346 | objfile. I.e, we are in the process of discarding all symbol information |
c906108c | 347 | for an objfile, freeing up all memory held for it, and unlinking the |
c378eb4e | 348 | objfile struct from the global list of known objfiles. */ |
c906108c SS |
349 | |
350 | static void | |
fba45db2 | 351 | som_symfile_finish (struct objfile *objfile) |
c906108c | 352 | { |
c906108c SS |
353 | } |
354 | ||
355 | /* SOM specific initialization routine for reading symbols. */ | |
356 | ||
357 | static void | |
fba45db2 | 358 | som_symfile_init (struct objfile *objfile) |
c906108c SS |
359 | { |
360 | /* SOM objects may be reordered, so set OBJF_REORDERED. If we | |
361 | find this causes a significant slowdown in gdb then we could | |
362 | set it in the debug symbol readers only when necessary. */ | |
363 | objfile->flags |= OBJF_REORDERED; | |
c906108c SS |
364 | } |
365 | ||
366 | /* SOM specific parsing routine for section offsets. | |
367 | ||
368 | Plain and simple for now. */ | |
369 | ||
d4f3574e | 370 | static void |
fba45db2 | 371 | som_symfile_offsets (struct objfile *objfile, struct section_addr_info *addrs) |
c906108c | 372 | { |
c906108c | 373 | int i; |
0aa9cf96 | 374 | CORE_ADDR text_addr; |
c906108c | 375 | |
a39a16c4 | 376 | objfile->num_sections = bfd_count_sections (objfile->obfd); |
d4f3574e | 377 | objfile->section_offsets = (struct section_offsets *) |
8b92e4d5 | 378 | obstack_alloc (&objfile->objfile_obstack, |
a39a16c4 | 379 | SIZEOF_N_SECTION_OFFSETS (objfile->num_sections)); |
c906108c | 380 | |
b8fbeb18 | 381 | /* FIXME: ezannoni 2000-04-20 The section names in SOM are not |
c378eb4e MS |
382 | .text, .data, etc, but $TEXT$, $DATA$,... We should initialize |
383 | SET_OFF_* from bfd. (See default_symfile_offsets()). But I don't | |
b8fbeb18 | 384 | know the correspondence between SOM sections and GDB's idea of |
c378eb4e MS |
385 | section names. So for now we default to what is was before these |
386 | changes. */ | |
b8fbeb18 EZ |
387 | objfile->sect_index_text = 0; |
388 | objfile->sect_index_data = 1; | |
389 | objfile->sect_index_bss = 2; | |
390 | objfile->sect_index_rodata = 3; | |
391 | ||
c906108c | 392 | /* First see if we're a shared library. If so, get the section |
2acceee2 | 393 | offsets from the library, else get them from addrs. */ |
d4f3574e | 394 | if (!som_solib_section_offsets (objfile, objfile->section_offsets)) |
c906108c | 395 | { |
b8fbeb18 EZ |
396 | /* Note: Here is OK to compare with ".text" because this is the |
397 | name that gdb itself gives to that section, not the SOM | |
c378eb4e | 398 | name. */ |
8d498949 | 399 | for (i = 0; i < addrs->num_sections && addrs->other[i].name; i++) |
0aa9cf96 EZ |
400 | if (strcmp (addrs->other[i].name, ".text") == 0) |
401 | break; | |
402 | text_addr = addrs->other[i].addr; | |
403 | ||
a39a16c4 | 404 | for (i = 0; i < objfile->num_sections; i++) |
f0a58b0b | 405 | (objfile->section_offsets)->offsets[i] = text_addr; |
c906108c | 406 | } |
c906108c | 407 | } |
c5aa993b | 408 | \f |
c906108c SS |
409 | |
410 | ||
c906108c SS |
411 | /* Register that we are able to handle SOM object file formats. */ |
412 | ||
00b5771c | 413 | static const struct sym_fns som_sym_fns = |
c906108c SS |
414 | { |
415 | bfd_target_som_flavour, | |
3e43a32a MS |
416 | som_new_init, /* init anything gbl to entire symtab */ |
417 | som_symfile_init, /* read initial info, setup for sym_read() */ | |
418 | som_symfile_read, /* read a symbol file into symtab */ | |
b11896a5 | 419 | NULL, /* sym_read_psymbols */ |
3e43a32a MS |
420 | som_symfile_finish, /* finished with file, cleanup */ |
421 | som_symfile_offsets, /* Translate ext. to int. relocation */ | |
422 | default_symfile_segments, /* Get segment information from a file. */ | |
423 | NULL, | |
424 | default_symfile_relocate, /* Relocate a debug section. */ | |
55aa24fb | 425 | NULL, /* sym_get_probes */ |
00b5771c | 426 | &psym_functions |
c906108c SS |
427 | }; |
428 | ||
429 | void | |
fba45db2 | 430 | _initialize_somread (void) |
c906108c SS |
431 | { |
432 | add_symtab_fns (&som_sym_fns); | |
433 | } |