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