Relocate symbols using an array of section_offsets, rather than a
[binutils-gdb.git] / gdb / partial-stab.h
1 /* Shared code to pre-read a stab (dbx-style), when building a psymtab.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992 Free Software Foundation,
3 Inc.
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 /* The following need to be defined:
22 SET_NAMESTRING() --Set namestring to name of symbol.
23 CUR_SYMBOL_TYPE --Type code of current symbol.
24 CUR_SYMBOL_VALUE --Value field of current symbol. May be adjusted here.
25 */
26
27 /* End of macro definitions, now let's handle them symbols! */
28
29 switch (CUR_SYMBOL_TYPE)
30 {
31 char *p;
32 /*
33 * Standard, external, non-debugger, symbols
34 */
35
36 case N_TEXT | N_EXT:
37 case N_NBTEXT | N_EXT:
38 CUR_SYMBOL_VALUE += ANOFFSET (section_offsets, SECT_OFF_TEXT);
39 goto record_it;
40
41 case N_DATA | N_EXT:
42 case N_NBDATA | N_EXT:
43 CUR_SYMBOL_VALUE += ANOFFSET (section_offsets, SECT_OFF_DATA);
44 goto record_it;
45
46 case N_BSS | N_EXT:
47 case N_NBBSS | N_EXT:
48 case N_SETV | N_EXT: /* FIXME, is this in BSS? */
49 CUR_SYMBOL_VALUE += ANOFFSET (section_offsets, SECT_OFF_BSS);
50 goto record_it;
51
52 case N_ABS | N_EXT:
53 record_it:
54 #ifdef DBXREAD_ONLY
55 SET_NAMESTRING();
56
57 bss_ext_symbol:
58 record_minimal_symbol (namestring, CUR_SYMBOL_VALUE,
59 CUR_SYMBOL_TYPE, objfile); /* Always */
60 #endif /* DBXREAD_ONLY */
61 continue;
62
63 /* Standard, local, non-debugger, symbols */
64
65 case N_NBTEXT:
66
67 /* We need to be able to deal with both N_FN or N_TEXT,
68 because we have no way of knowing whether the sys-supplied ld
69 or GNU ld was used to make the executable. Sequents throw
70 in another wrinkle -- they renumbered N_FN. */
71
72 case N_FN:
73 case N_FN_SEQ:
74 case N_TEXT:
75 #ifdef DBXREAD_ONLY
76 CUR_SYMBOL_VALUE += ANOFFSET (section_offsets, SECT_OFF_TEXT);
77 SET_NAMESTRING();
78 if ((namestring[0] == '-' && namestring[1] == 'l')
79 || (namestring [(nsl = strlen (namestring)) - 1] == 'o'
80 && namestring [nsl - 2] == '.')
81 #ifdef hp9000s800
82 /* some cooperation from gcc to get around ld stupidity */
83 || (namestring[0] == 'e' && !strcmp (namestring, "end_file."))
84 #endif
85 )
86 {
87 #ifndef hp9000s800
88 if (objfile -> ei.entry_point < CUR_SYMBOL_VALUE &&
89 objfile -> ei.entry_point >= last_o_file_start)
90 {
91 objfile -> ei.entry_file_lowpc = last_o_file_start;
92 objfile -> ei.entry_file_highpc = CUR_SYMBOL_VALUE;
93 }
94 #endif
95 if (past_first_source_file && pst
96 /* The gould NP1 uses low values for .o and -l symbols
97 which are not the address. */
98 && CUR_SYMBOL_VALUE >= pst->textlow)
99 {
100 END_PSYMTAB (pst, psymtab_include_list, includes_used,
101 symnum * symbol_size, CUR_SYMBOL_VALUE,
102 dependency_list, dependencies_used);
103 pst = (struct partial_symtab *) 0;
104 includes_used = 0;
105 dependencies_used = 0;
106 }
107 else
108 past_first_source_file = 1;
109 last_o_file_start = CUR_SYMBOL_VALUE;
110 }
111 #endif /* DBXREAD_ONLY */
112 continue;
113
114 case N_DATA:
115 #ifdef DBXREAD_ONLY
116 CUR_SYMBOL_VALUE += ANOFFSET (section_offsets, SECT_OFF_DATA);
117 SET_NAMESTRING ();
118 /* Check for __DYNAMIC, which is used by Sun shared libraries.
119 Record it even if it's local, not global, so we can find it.
120 Same with virtual function tables, both global and static. */
121 if ((namestring[8] == 'C' && (strcmp ("__DYNAMIC", namestring) == 0))
122 || VTBL_PREFIX_P ((namestring+HASH_OFFSET)))
123 {
124 record_minimal_symbol (namestring, CUR_SYMBOL_VALUE,
125 CUR_SYMBOL_TYPE, objfile); /* Always */
126 }
127 #endif /* DBXREAD_ONLY */
128 continue;
129
130 case N_UNDF | N_EXT:
131 #ifdef DBXREAD_ONLY
132 if (CUR_SYMBOL_VALUE != 0) {
133 /* This is a "Fortran COMMON" symbol. See if the target
134 environment knows where it has been relocated to. */
135
136 CORE_ADDR reladdr;
137
138 SET_NAMESTRING();
139 if (target_lookup_symbol (namestring, &reladdr)) {
140 continue; /* Error in lookup; ignore symbol for now. */
141 }
142 CUR_SYMBOL_TYPE ^= (N_BSS^N_UNDF); /* Define it as a bss-symbol */
143 CUR_SYMBOL_VALUE = reladdr;
144 goto bss_ext_symbol;
145 }
146 #endif /* DBXREAD_ONLY */
147 continue; /* Just undefined, not COMMON */
148
149 case N_UNDF:
150 #ifdef DBXREAD_ONLY
151 if (processing_acc_compilation && bufp->n_strx == 1) {
152 /* Deal with relative offsets in the string table
153 used in ELF+STAB under Solaris. If we want to use the
154 n_strx field, which contains the name of the file,
155 we must adjust file_string_table_offset *before* calling
156 SET_NAMESTRING(). */
157 past_first_source_file = 1;
158 file_string_table_offset = next_file_string_table_offset;
159 next_file_string_table_offset =
160 file_string_table_offset + bufp->n_value;
161 if (next_file_string_table_offset < file_string_table_offset)
162 error ("string table offset backs up at %d", symnum);
163 /* FIXME -- replace error() with complaint. */
164 continue;
165 }
166 #endif /* DBXREAD_ONLY */
167 continue;
168
169 /* Lots of symbol types we can just ignore. */
170
171 case N_ABS:
172 case N_BSS:
173 case N_NBDATA:
174 case N_NBBSS:
175 continue;
176
177 /* Keep going . . .*/
178
179 /*
180 * Special symbol types for GNU
181 */
182 case N_INDR:
183 case N_INDR | N_EXT:
184 case N_SETA:
185 case N_SETA | N_EXT:
186 case N_SETT:
187 case N_SETT | N_EXT:
188 case N_SETD:
189 case N_SETD | N_EXT:
190 case N_SETB:
191 case N_SETB | N_EXT:
192 case N_SETV:
193 continue;
194
195 /*
196 * Debugger symbols
197 */
198
199 case N_SO: {
200 unsigned long valu = CUR_SYMBOL_VALUE;
201 static int last_so_symnum = -10;
202 static int dir_so_symnum = -10;
203 int tmp;
204 char *p;
205
206 /* End the current partial symtab and start a new one */
207
208 SET_NAMESTRING();
209
210 valu += ANOFFSET (section_offsets, SECT_OFF_TEXT);
211
212 if (pst)
213 {
214 END_PSYMTAB (pst, psymtab_include_list, includes_used,
215 symnum * symbol_size, valu,
216 dependency_list, dependencies_used);
217 pst = (struct partial_symtab *) 0;
218 includes_used = 0;
219 dependencies_used = 0;
220 }
221
222 /* Some compilers (including gcc) emit a pair of initial N_SOs.
223 The first one is a directory name; the second the file name.
224 If pst exists, is empty, and has a filename ending in '/',
225 we assume the previous N_SO was a directory name. */
226
227 p = strrchr (namestring, '/');
228 if (p && *(p+1) == '\000')
229 {
230 dir_so_symnum = symnum;
231 continue; /* Simply ignore directory name SOs */
232 }
233
234 /* Some other compilers (C++ ones in particular) emit useless
235 SOs for non-existant .c files. */
236
237 if (last_so_symnum == symnum - 1)
238 continue; /* Ignore repeated SOs */
239 last_so_symnum = symnum;
240
241 past_first_source_file = 1;
242
243 if (dir_so_symnum == symnum - 1) /* Was prev. SO a directory? */
244 tmp = dir_so_symnum;
245 else
246 tmp = symnum;
247 pst = START_PSYMTAB (objfile, section_offsets,
248 namestring, valu,
249 tmp * symbol_size,
250 objfile -> global_psymbols.next,
251 objfile -> static_psymbols.next);
252 dir_so_symnum = -10;
253 continue;
254 }
255
256 case N_BINCL:
257 #ifdef DBXREAD_ONLY
258 /* Add this bincl to the bincl_list for future EXCLs. No
259 need to save the string; it'll be around until
260 read_dbx_symtab function returns */
261
262 SET_NAMESTRING();
263
264 add_bincl_to_list (pst, namestring, CUR_SYMBOL_VALUE);
265
266 /* Mark down an include file in the current psymtab */
267
268 psymtab_include_list[includes_used++] = namestring;
269 if (includes_used >= includes_allocated)
270 {
271 char **orig = psymtab_include_list;
272
273 psymtab_include_list = (char **)
274 alloca ((includes_allocated *= 2) *
275 sizeof (char *));
276 memcpy ((PTR)psymtab_include_list, (PTR)orig,
277 includes_used * sizeof (char *));
278 }
279
280 #endif /* DBXREAD_ONLY */
281 continue;
282
283 case N_SOL:
284 /* Mark down an include file in the current psymtab */
285
286 SET_NAMESTRING();
287
288 /* In C++, one may expect the same filename to come round many
289 times, when code is coming alternately from the main file
290 and from inline functions in other files. So I check to see
291 if this is a file we've seen before -- either the main
292 source file, or a previously included file.
293
294 This seems to be a lot of time to be spending on N_SOL, but
295 things like "break c-exp.y:435" need to work (I
296 suppose the psymtab_include_list could be hashed or put
297 in a binary tree, if profiling shows this is a major hog). */
298 if (pst && !strcmp (namestring, pst->filename))
299 continue;
300 {
301 register int i;
302 for (i = 0; i < includes_used; i++)
303 if (!strcmp (namestring, psymtab_include_list[i]))
304 {
305 i = -1;
306 break;
307 }
308 if (i == -1)
309 continue;
310 }
311
312 psymtab_include_list[includes_used++] = namestring;
313 if (includes_used >= includes_allocated)
314 {
315 char **orig = psymtab_include_list;
316
317 psymtab_include_list = (char **)
318 alloca ((includes_allocated *= 2) *
319 sizeof (char *));
320 memcpy ((PTR)psymtab_include_list, (PTR)orig,
321 includes_used * sizeof (char *));
322 }
323 continue;
324
325 case N_LSYM: /* Typedef or automatic variable. */
326 case N_STSYM: /* Data seg var -- static */
327 case N_LCSYM: /* BSS " */
328 case N_ROSYM: /* Read-only data seg var -- static. */
329 case N_NBSTS: /* Gould nobase. */
330 case N_NBLCS: /* symbols. */
331
332 SET_NAMESTRING();
333
334 p = (char *) strchr (namestring, ':');
335
336 /* Skip if there is no :. */
337 if (!p) continue;
338
339 switch (p[1])
340 {
341 case 'T':
342 if (p != namestring) /* a name is there, not just :T... */
343 {
344 ADD_PSYMBOL_TO_LIST (namestring, p - namestring,
345 STRUCT_NAMESPACE, LOC_TYPEDEF,
346 objfile->static_psymbols, CUR_SYMBOL_VALUE);
347 if (p[2] == 't')
348 {
349 /* Also a typedef with the same name. */
350 ADD_PSYMBOL_TO_LIST (namestring, p - namestring,
351 VAR_NAMESPACE, LOC_TYPEDEF,
352 objfile->static_psymbols, CUR_SYMBOL_VALUE);
353 p += 1;
354 }
355 }
356 goto check_enum;
357 case 't':
358 if (p != namestring) /* a name is there, not just :T... */
359 {
360 ADD_PSYMBOL_TO_LIST (namestring, p - namestring,
361 VAR_NAMESPACE, LOC_TYPEDEF,
362 objfile->static_psymbols, CUR_SYMBOL_VALUE);
363 }
364 check_enum:
365 /* If this is an enumerated type, we need to
366 add all the enum constants to the partial symbol
367 table. This does not cover enums without names, e.g.
368 "enum {a, b} c;" in C, but fortunately those are
369 rare. There is no way for GDB to find those from the
370 enum type without spending too much time on it. Thus
371 to solve this problem, the compiler needs to put out separate
372 constant symbols ('c' N_LSYMS) for enum constants in
373 enums without names, or put out a dummy type. */
374
375 /* We are looking for something of the form
376 <name> ":" ("t" | "T") [<number> "="] "e"
377 {<constant> ":" <value> ","} ";". */
378
379 /* Skip over the colon and the 't' or 'T'. */
380 p += 2;
381 /* This type may be given a number. Also, numbers can come
382 in pairs like (0,26). Skip over it. */
383 while ((*p >= '0' && *p <= '9')
384 || *p == '(' || *p == ',' || *p == ')'
385 || *p == '=')
386 p++;
387
388 if (*p++ == 'e')
389 {
390 /* We have found an enumerated type. */
391 /* According to comments in read_enum_type
392 a comma could end it instead of a semicolon.
393 I don't know where that happens.
394 Accept either. */
395 while (*p && *p != ';' && *p != ',')
396 {
397 char *q;
398
399 /* Check for and handle cretinous dbx symbol name
400 continuation! */
401 if (*p == '\\')
402 p = next_symbol_text ();
403
404 /* Point to the character after the name
405 of the enum constant. */
406 for (q = p; *q && *q != ':'; q++)
407 ;
408 /* Note that the value doesn't matter for
409 enum constants in psymtabs, just in symtabs. */
410 ADD_PSYMBOL_TO_LIST (p, q - p,
411 VAR_NAMESPACE, LOC_CONST,
412 objfile->static_psymbols, 0);
413 /* Point past the name. */
414 p = q;
415 /* Skip over the value. */
416 while (*p && *p != ',')
417 p++;
418 /* Advance past the comma. */
419 if (*p)
420 p++;
421 }
422 }
423 continue;
424 case 'c':
425 /* Constant, e.g. from "const" in Pascal. */
426 ADD_PSYMBOL_TO_LIST (namestring, p - namestring,
427 VAR_NAMESPACE, LOC_CONST,
428 objfile->static_psymbols, CUR_SYMBOL_VALUE);
429 continue;
430 default:
431 /* Skip if the thing following the : is
432 not a letter (which indicates declaration of a local
433 variable, which we aren't interested in). */
434 continue;
435 }
436
437 case N_FUN:
438 case N_GSYM: /* Global (extern) variable; can be
439 data or bss (sigh FIXME). */
440
441 /* Following may probably be ignored; I'll leave them here
442 for now (until I do Pascal and Modula 2 extensions). */
443
444 case N_PC: /* I may or may not need this; I
445 suspect not. */
446 case N_M2C: /* I suspect that I can ignore this here. */
447 case N_SCOPE: /* Same. */
448
449 SET_NAMESTRING();
450
451 p = (char *) strchr (namestring, ':');
452 if (!p)
453 continue; /* Not a debugging symbol. */
454
455
456
457 /* Main processing section for debugging symbols which
458 the initial read through the symbol tables needs to worry
459 about. If we reach this point, the symbol which we are
460 considering is definitely one we are interested in.
461 p must also contain the (valid) index into the namestring
462 which indicates the debugging type symbol. */
463
464 switch (p[1])
465 {
466 case 'c':
467 ADD_PSYMBOL_TO_LIST (namestring, p - namestring,
468 VAR_NAMESPACE, LOC_CONST,
469 objfile->static_psymbols, CUR_SYMBOL_VALUE);
470 continue;
471 case 'S':
472 CUR_SYMBOL_VALUE += ANOFFSET (section_offsets, SECT_OFF_DATA);
473 ADD_PSYMBOL_ADDR_TO_LIST (namestring, p - namestring,
474 VAR_NAMESPACE, LOC_STATIC,
475 objfile->static_psymbols, CUR_SYMBOL_VALUE);
476 continue;
477 case 'G':
478 CUR_SYMBOL_VALUE += ANOFFSET (section_offsets, SECT_OFF_DATA);
479 /* The addresses in these entries are reported to be
480 wrong. See the code that reads 'G's for symtabs. */
481 ADD_PSYMBOL_ADDR_TO_LIST (namestring, p - namestring,
482 VAR_NAMESPACE, LOC_STATIC,
483 objfile->global_psymbols, CUR_SYMBOL_VALUE);
484 continue;
485
486 case 't':
487 ADD_PSYMBOL_TO_LIST (namestring, p - namestring,
488 VAR_NAMESPACE, LOC_TYPEDEF,
489 objfile->static_psymbols, CUR_SYMBOL_VALUE);
490 continue;
491
492 case 'f':
493 #ifdef DBXREAD_ONLY
494 /* Kludges for ELF/STABS with Sun ACC */
495 last_function_name = namestring;
496 if (pst->textlow == 0)
497 pst->textlow = CUR_SYMBOL_VALUE;
498 #if 0
499 if (startup_file_end == 0)
500 startup_file_end = CUR_SYMBOL_VALUE;
501 #endif
502 /* End kludge. */
503 #endif /* DBXREAD_ONLY */
504 ADD_PSYMBOL_TO_LIST (namestring, p - namestring,
505 VAR_NAMESPACE, LOC_BLOCK,
506 objfile->static_psymbols, CUR_SYMBOL_VALUE);
507 continue;
508
509 /* Global functions were ignored here, but now they
510 are put into the global psymtab like one would expect.
511 They're also in the misc fn vector...
512 FIXME, why did it used to ignore these? That broke
513 "i fun" on these functions. */
514 case 'F':
515 #ifdef DBXREAD_ONLY
516 /* Kludges for ELF/STABS with Sun ACC */
517 last_function_name = namestring;
518 if (pst->textlow == 0)
519 pst->textlow = CUR_SYMBOL_VALUE;
520 #if 0
521 if (startup_file_end == 0)
522 startup_file_end = CUR_SYMBOL_VALUE;
523 #endif
524 /* End kludge. */
525 #endif /* DBXREAD_ONLY */
526 ADD_PSYMBOL_TO_LIST (namestring, p - namestring,
527 VAR_NAMESPACE, LOC_BLOCK,
528 objfile->global_psymbols, CUR_SYMBOL_VALUE);
529 continue;
530
531 /* Two things show up here (hopefully); static symbols of
532 local scope (static used inside braces) or extensions
533 of structure symbols. We can ignore both. */
534 case 'V':
535 case '(':
536 case '0':
537 case '1':
538 case '2':
539 case '3':
540 case '4':
541 case '5':
542 case '6':
543 case '7':
544 case '8':
545 case '9':
546 continue;
547
548 default:
549 /* Unexpected symbol. Ignore it; perhaps it is an extension
550 that we don't know about.
551
552 Someone says sun cc puts out symbols like
553 /foo/baz/maclib::/usr/local/bin/maclib,
554 which would get here with a symbol type of ':'. */
555 continue;
556 }
557
558 case N_EXCL:
559 #ifdef DBXREAD_ONLY
560
561 SET_NAMESTRING();
562
563 /* Find the corresponding bincl and mark that psymtab on the
564 psymtab dependency list */
565 {
566 struct partial_symtab *needed_pst =
567 find_corresponding_bincl_psymtab (namestring, CUR_SYMBOL_VALUE);
568
569 /* If this include file was defined earlier in this file,
570 leave it alone. */
571 if (needed_pst == pst) continue;
572
573 if (needed_pst)
574 {
575 int i;
576 int found = 0;
577
578 for (i = 0; i < dependencies_used; i++)
579 if (dependency_list[i] == needed_pst)
580 {
581 found = 1;
582 break;
583 }
584
585 /* If it's already in the list, skip the rest. */
586 if (found) continue;
587
588 dependency_list[dependencies_used++] = needed_pst;
589 if (dependencies_used >= dependencies_allocated)
590 {
591 struct partial_symtab **orig = dependency_list;
592 dependency_list =
593 (struct partial_symtab **)
594 alloca ((dependencies_allocated *= 2)
595 * sizeof (struct partial_symtab *));
596 memcpy ((PTR)dependency_list, (PTR)orig,
597 (dependencies_used
598 * sizeof (struct partial_symtab *)));
599 #ifdef DEBUG_INFO
600 fprintf (stderr, "Had to reallocate dependency list.\n");
601 fprintf (stderr, "New dependencies allocated: %d\n",
602 dependencies_allocated);
603 #endif
604 }
605 }
606 else
607 error ("Invalid symbol data: \"repeated\" header file not previously seen, at symtab pos %d.",
608 symnum);
609 }
610 #endif /* DBXREAD_ONLY */
611 continue;
612
613 case N_RBRAC:
614 #ifdef HANDLE_RBRAC
615 HANDLE_RBRAC(CUR_SYMBOL_VALUE);
616 continue;
617 #endif
618 case N_EINCL:
619 case N_DSLINE:
620 case N_BSLINE:
621 case N_SSYM: /* Claim: Structure or union element.
622 Hopefully, I can ignore this. */
623 case N_ENTRY: /* Alternate entry point; can ignore. */
624 case N_MAIN: /* Can definitely ignore this. */
625 case N_CATCH: /* These are GNU C++ extensions */
626 case N_EHDECL: /* that can safely be ignored here. */
627 case N_LENG:
628 case N_BCOMM:
629 case N_ECOMM:
630 case N_ECOML:
631 case N_FNAME:
632 case N_SLINE:
633 case N_RSYM:
634 case N_PSYM:
635 case N_LBRAC:
636 case N_NSYMS: /* Ultrix 4.0: symbol count */
637 case N_DEFD: /* GNU Modula-2 */
638
639 case N_OBJ: /* useless types from Solaris */
640 case N_OPT:
641 case N_ENDM:
642 /* These symbols aren't interesting; don't worry about them */
643
644 continue;
645
646 default:
647 /* If we haven't found it yet, ignore it. It's probably some
648 new type we don't know about yet. */
649 complain (&unknown_symtype_complaint, local_hex_string(CUR_SYMBOL_TYPE));
650 continue;
651 }