OSDN Git Service

PR preprocessor/48677
[pf3gnuchains/gcc-fork.git] / gcc / mips-tdump.c
1 /* Read and manage MIPS symbol tables from object modules.
2    Copyright (C) 1991, 1994, 1995, 1997, 1998, 1999, 2000, 2001, 2003, 2004,
3    2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4    Contributed by hartzell@boulder.colorado.edu,
5    Rewritten by meissner@osf.org.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
13
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "version.h"
26 #ifdef index
27 #undef index
28 #endif
29 #ifndef CROSS_DIRECTORY_STRUCTURE
30 #include <a.out.h>
31 #else
32 #include "mips/a.out.h"
33 #endif /* CROSS_DIRECTORY_STRUCTURE */
34
35 /* Include getopt.h for the sake of getopt_long.  */
36 #include "getopt.h"
37
38 /* Macros for mips-tfile.c to encapsulate stabs in ECOFF, and for
39    mips-tdump.c to print them out.
40
41    These must match the corresponding definitions in gdb/mipsread.c.
42    Unfortunately, gcc and gdb do not currently share any directories.  */
43
44 #define CODE_MASK 0x8F300
45 #define MIPS_IS_STAB(sym) (((sym)->index & 0xFFF00) == CODE_MASK)
46 #define MIPS_MARK_STAB(code) ((code)+CODE_MASK)
47 #define MIPS_UNMARK_STAB(code) ((code)-CODE_MASK)
48
49 #define uchar   unsigned char
50 #define ushort  unsigned short
51 #define uint    unsigned int
52 #define ulong   unsigned long
53
54 \f
55 /* Redefinition of storage classes as an enumeration for better
56    debugging.  */
57
58 #ifndef stStaParam
59 #define stStaParam      16      /* Fortran static parameters */
60 #endif
61
62 #ifndef btVoid
63 #define btVoid          26      /* void basic type */
64 #endif
65
66 typedef enum sc {
67   sc_Nil         = scNil,         /* no storage class */
68   sc_Text        = scText,        /* text symbol */
69   sc_Data        = scData,        /* initialized data symbol */
70   sc_Bss         = scBss,         /* un-initialized data symbol */
71   sc_Register    = scRegister,    /* value of symbol is register number */
72   sc_Abs         = scAbs,         /* value of symbol is absolute */
73   sc_Undefined   = scUndefined,   /* who knows? */
74   sc_CdbLocal    = scCdbLocal,    /* variable's value is IN se->va.?? */
75   sc_Bits        = scBits,        /* this is a bit field */
76   sc_CdbSystem   = scCdbSystem,   /* var's value is IN CDB's address space */
77   sc_RegImage    = scRegImage,    /* register value saved on stack */
78   sc_Info        = scInfo,        /* symbol contains debugger information */
79   sc_UserStruct  = scUserStruct,  /* addr in struct user for current process */
80   sc_SData       = scSData,       /* load time only small data */
81   sc_SBss        = scSBss,        /* load time only small common */
82   sc_RData       = scRData,       /* load time only read only data */
83   sc_Var         = scVar,         /* Var parameter (fortran,pascal) */
84   sc_Common      = scCommon,      /* common variable */
85   sc_SCommon     = scSCommon,     /* small common */
86   sc_VarRegister = scVarRegister, /* Var parameter in a register */
87   sc_Variant     = scVariant,     /* Variant record */
88   sc_SUndefined  = scSUndefined,  /* small undefined(external) data */
89   sc_Init        = scInit,        /* .init section symbol */
90   sc_Max         = scMax          /* Max storage class+1 */
91 } sc_t;
92
93 /* Redefinition of symbol type.  */
94
95 typedef enum st {
96   st_Nil        = stNil,        /* Nuthin' special */
97   st_Global     = stGlobal,     /* external symbol */
98   st_Static     = stStatic,     /* static */
99   st_Param      = stParam,      /* procedure argument */
100   st_Local      = stLocal,      /* local variable */
101   st_Label      = stLabel,      /* label */
102   st_Proc       = stProc,       /*     "      "  Procedure */
103   st_Block      = stBlock,      /* beginning of block */
104   st_End        = stEnd,        /* end (of anything) */
105   st_Member     = stMember,     /* member (of anything  - struct/union/enum */
106   st_Typedef    = stTypedef,    /* type definition */
107   st_File       = stFile,       /* file name */
108   st_RegReloc   = stRegReloc,   /* register relocation */
109   st_Forward    = stForward,    /* forwarding address */
110   st_StaticProc = stStaticProc, /* load time only static procs */
111   st_StaParam   = stStaParam,   /* Fortran static parameters */
112   st_Constant   = stConstant,   /* const */
113 #ifdef stStruct
114   st_Struct     = stStruct,     /* struct */
115   st_Union      = stUnion,      /* union */
116   st_Enum       = stEnum,       /* enum */
117 #endif
118   st_Str        = stStr,        /* string */
119   st_Number     = stNumber,     /* pure number (i.e. 4 NOR 2+2) */
120   st_Expr       = stExpr,       /* 2+2 vs. 4 */
121   st_Type       = stType,       /* post-coercion SER */
122   st_Max        = stMax         /* max type+1 */
123 } st_t;
124
125 /* Redefinition of type qualifiers.  */
126
127 typedef enum tq {
128   tq_Nil        = tqNil,        /* bt is what you see */
129   tq_Ptr        = tqPtr,        /* pointer */
130   tq_Proc       = tqProc,       /* procedure */
131   tq_Array      = tqArray,      /* duh */
132   tq_Far        = tqFar,        /* longer addressing - 8086/8 land */
133   tq_Vol        = tqVol,        /* volatile */
134   tq_Max        = tqMax         /* Max type qualifier+1 */
135 } tq_t;
136
137 /* Redefinition of basic types.  */
138
139 typedef enum bt {
140   bt_Nil        = btNil,        /* undefined */
141   bt_Adr        = btAdr,        /* address - integer same size as pointer */
142   bt_Char       = btChar,       /* character */
143   bt_UChar      = btUChar,      /* unsigned character */
144   bt_Short      = btShort,      /* short */
145   bt_UShort     = btUShort,     /* unsigned short */
146   bt_Int        = btInt,        /* int */
147   bt_UInt       = btUInt,       /* unsigned int */
148   bt_Long       = btLong,       /* long */
149   bt_ULong      = btULong,      /* unsigned long */
150   bt_Float      = btFloat,      /* float (real) */
151   bt_Double     = btDouble,     /* Double (real) */
152   bt_Struct     = btStruct,     /* Structure (Record) */
153   bt_Union      = btUnion,      /* Union (variant) */
154   bt_Enum       = btEnum,       /* Enumerated */
155   bt_Typedef    = btTypedef,    /* defined via a typedef, isymRef points */
156   bt_Range      = btRange,      /* subrange of int */
157   bt_Set        = btSet,        /* pascal sets */
158   bt_Complex    = btComplex,    /* fortran complex */
159   bt_DComplex   = btDComplex,   /* fortran double complex */
160   bt_Indirect   = btIndirect,   /* forward or unnamed typedef */
161   bt_FixedDec   = btFixedDec,   /* Fixed Decimal */
162   bt_FloatDec   = btFloatDec,   /* Float Decimal */
163   bt_String     = btString,     /* Varying Length Character String */
164   bt_Bit        = btBit,        /* Aligned Bit String */
165   bt_Picture    = btPicture,    /* Picture */
166   bt_Void       = btVoid,       /* void */
167   bt_Max        = btMax         /* Max basic type+1 */
168 } bt_t;
169
170 /* Redefinition of the language codes.  */
171
172 typedef enum lang {
173   lang_C         = langC,
174   lang_Pascal    = langPascal,
175   lang_Fortran   = langFortran,
176   lang_Assembler = langAssembler,
177   lang_Machine   = langMachine,
178   lang_Nil       = langNil,
179   lang_Ada       = langAda,
180   lang_Pl1       = langPl1,
181   lang_Cobol     = langCobol
182 } lang_t;
183
184 /* Redefinition of the debug level codes.  */
185
186 typedef enum glevel {
187   glevel_0      = GLEVEL_0,
188   glevel_1      = GLEVEL_1,
189   glevel_2      = GLEVEL_2,
190   glevel_3      = GLEVEL_3
191 } glevel_t;
192
193 \f
194 /* Keep track of the active scopes.  */
195 typedef struct scope {
196   struct scope *prev;           /* previous scope */
197   ulong open_sym;               /* symbol opening scope */
198   sc_t sc;                      /* storage class */
199   st_t st;                      /* symbol type */
200 } scope_t;
201
202 struct filehdr global_hdr;      /* a.out header */
203
204 int      errors         = 0;    /* # of errors */
205 int      want_aux       = 0;    /* print aux table */
206 int      want_line      = 0;    /* print line numbers */
207 int      want_rfd       = 0;    /* print relative file desc's */
208 int      want_scope     = 0;    /* print scopes for every symbol */
209 int      tfile          = 0;    /* no global header file */
210 int      version        = 0;    /* print version # */
211 int      verbose        = 0;
212 int      tfile_fd;              /* file descriptor of .T file */
213 off_t    tfile_offset;          /* current offset in .T file */
214 scope_t *cur_scope      = 0;    /* list of active scopes */
215 scope_t *free_scope     = 0;    /* list of freed scopes */
216 HDRR     sym_hdr;               /* symbolic header */
217 char    *l_strings;             /* local strings */
218 char    *e_strings;             /* external strings */
219 SYMR    *l_symbols;             /* local symbols */
220 EXTR    *e_symbols;             /* external symbols */
221 LINER   *lines;                 /* line numbers */
222 DNR     *dense_nums;            /* dense numbers */
223 OPTR    *opt_symbols;           /* optimization symbols */
224 AUXU    *aux_symbols;           /* Auxiliary symbols */
225 char    *aux_used;              /* map of which aux syms are used */
226 FDR     *file_desc;             /* file tables */
227 ulong   *rfile_desc;            /* relative file tables */
228 PDR     *proc_desc;             /* procedure tables */
229
230 /* Forward reference for functions.  */
231 static void *read_seek (void *, size_t, off_t, const char *);
232 static void read_tfile (void);
233 static void print_global_hdr (struct filehdr *);
234 static void print_sym_hdr (HDRR *);
235 static void print_file_desc (FDR *, int);
236 static void print_symbol (SYMR *, int, const char *, AUXU *, int, FDR *);
237 static void print_aux (AUXU, int, int);
238 static void emit_aggregate (char *, AUXU, AUXU, const char *, FDR *);
239 static const char *st_to_string (st_t);
240 static const char *sc_to_string (sc_t);
241 static const char *glevel_to_string (glevel_t);
242 static const char *lang_to_string (lang_t);
243 static const char *type_to_string (AUXU *, int, FDR *);
244
245 extern char *optarg;
246 extern int   optind;
247 extern int   opterr;
248
249 /* Create a table of debugging stab-codes and corresponding names.  */
250
251 #define __define_stab(NAME, CODE, STRING) {(int)CODE, STRING},
252 const struct {const short code; const char string[10];} stab_names[]  = {
253 #include "stab.def"
254 #undef __define_stab
255 };
256
257 /* Command line options for getopt_long.  */
258
259 static const struct option options[] =
260 {
261   { "version", 0, 0, 'V' },
262   { "verbose", 0, 0, 'v' },
263   { 0, 0, 0, 0 }
264 };
265 \f
266 /* Read some bytes at a specified location, and return a pointer.
267    Read_seek takes a pointer PTR to a buffer or NULL and reads SIZE
268    bytes from offset OFFSET.  In case of errors CONTEXT is used as
269    error message.  */
270
271 static void *
272 read_seek (void *ptr, size_t size, off_t offset,  const char *context)
273 {
274   long read_size = 0;
275
276   if (size == 0)                /* nothing to read */
277     return ptr;
278
279   if (!ptr)
280     ptr = xmalloc (size);
281
282   if ((tfile_offset != offset && lseek (tfile_fd, offset, 0) == -1)
283       || (read_size = read (tfile_fd, ptr, size)) < 0)
284     {
285       perror (context);
286       exit (1);
287     }
288
289   if (read_size != (long) size)
290     {
291       fprintf (stderr, "%s: read %ld bytes, expected %ld bytes\n",
292                context, read_size, (long) size);
293       exit (1);
294     }
295
296   tfile_offset = offset + size;
297   return ptr;
298 }
299
300 \f
301 /* Convert language code to string format.  */
302
303 static const char *
304 lang_to_string (lang_t lang)
305 {
306   switch (lang)
307     {
308     case langC:         return "C";
309     case langPascal:    return "Pascal";
310     case langFortran:   return "Fortran";
311     case langAssembler: return "Assembler";
312     case langMachine:   return "Machine";
313     case langNil:       return "Nil";
314     case langAda:       return "Ada";
315     case langPl1:       return "Pl1";
316     case langCobol:     return "Cobol";
317     }
318
319   return "Unknown language";
320 }
321
322 \f
323 /* Convert storage class to string.  */
324
325 static const char *
326 sc_to_string (sc_t storage_class)
327 {
328   switch(storage_class)
329     {
330     case sc_Nil:         return "Nil";
331     case sc_Text:        return "Text";
332     case sc_Data:        return "Data";
333     case sc_Bss:         return "Bss";
334     case sc_Register:    return "Register";
335     case sc_Abs:         return "Abs";
336     case sc_Undefined:   return "Undefined";
337     case sc_CdbLocal:    return "CdbLocal";
338     case sc_Bits:        return "Bits";
339     case sc_CdbSystem:   return "CdbSystem";
340     case sc_RegImage:    return "RegImage";
341     case sc_Info:        return "Info";
342     case sc_UserStruct:  return "UserStruct";
343     case sc_SData:       return "SData";
344     case sc_SBss:        return "SBss";
345     case sc_RData:       return "RData";
346     case sc_Var:         return "Var";
347     case sc_Common:      return "Common";
348     case sc_SCommon:     return "SCommon";
349     case sc_VarRegister: return "VarRegister";
350     case sc_Variant:     return "Variant";
351     case sc_SUndefined:  return "SUndefined";
352     case sc_Init:        return "Init";
353     case sc_Max:         return "Max";
354     }
355
356   return "???";
357 }
358
359 \f
360 /* Convert symbol type to string.  */
361
362 static const char *
363 st_to_string (st_t symbol_type)
364 {
365   switch(symbol_type)
366     {
367     case st_Nil:        return "Nil";
368     case st_Global:     return "Global";
369     case st_Static:     return "Static";
370     case st_Param:      return "Param";
371     case st_Local:      return "Local";
372     case st_Label:      return "Label";
373     case st_Proc:       return "Proc";
374     case st_Block:      return "Block";
375     case st_End:        return "End";
376     case st_Member:     return "Member";
377     case st_Typedef:    return "Typedef";
378     case st_File:       return "File";
379     case st_RegReloc:   return "RegReloc";
380     case st_Forward:    return "Forward";
381     case st_StaticProc: return "StaticProc";
382     case st_Constant:   return "Constant";
383     case st_StaParam:   return "StaticParam";
384 #ifdef stStruct
385     case st_Struct:     return "Struct";
386     case st_Union:      return "Union";
387     case st_Enum:       return "Enum";
388 #endif
389     case st_Str:        return "String";
390     case st_Number:     return "Number";
391     case st_Expr:       return "Expr";
392     case st_Type:       return "Type";
393     case st_Max:        return "Max";
394     }
395
396   return "???";
397 }
398
399 \f
400 /* Convert debug level to string.  */
401
402 static const char *
403 glevel_to_string (glevel_t g_level)
404 {
405   switch(g_level)
406     {
407     case GLEVEL_0: return "G0";
408     case GLEVEL_1: return "G1";
409     case GLEVEL_2: return "G2";
410     case GLEVEL_3: return "G3";
411     }
412
413   return "??";
414 }
415
416 \f
417 /* Convert the type information to string format.  */
418
419 static const char *
420 type_to_string (AUXU *aux_ptr, int index, FDR *fdp)
421 {
422   AUXU u;
423   struct qual {
424     tq_t type;
425     int  low_bound;
426     int  high_bound;
427     int  stride;
428   } qualifiers[7];
429
430   bt_t basic_type;
431   int i;
432   static char buffer1[1024];
433   static char buffer2[1024];
434   char *p1 = buffer1;
435   char *p2 = buffer2;
436   char *used_ptr = aux_used + (aux_ptr - aux_symbols);
437
438   for (i = 0; i < 7; i++)
439     {
440       qualifiers[i].low_bound = 0;
441       qualifiers[i].high_bound = 0;
442       qualifiers[i].stride = 0;
443     }
444
445   used_ptr[index] = 1;
446   u = aux_ptr[index++];
447   if (u.isym == -1)
448     return "-1 (no type)";
449
450   basic_type = (bt_t) u.ti.bt;
451   qualifiers[0].type = (tq_t) u.ti.tq0;
452   qualifiers[1].type = (tq_t) u.ti.tq1;
453   qualifiers[2].type = (tq_t) u.ti.tq2;
454   qualifiers[3].type = (tq_t) u.ti.tq3;
455   qualifiers[4].type = (tq_t) u.ti.tq4;
456   qualifiers[5].type = (tq_t) u.ti.tq5;
457   qualifiers[6].type = tq_Nil;
458
459   /*
460    * Go get the basic type.
461    */
462   switch (basic_type)
463     {
464     case bt_Nil:                /* undefined */
465       strcpy (p1, "nil");
466       break;
467
468     case bt_Adr:                /* address - integer same size as pointer */
469       strcpy (p1, "address");
470       break;
471
472     case bt_Char:               /* character */
473       strcpy (p1, "char");
474       break;
475
476     case bt_UChar:              /* unsigned character */
477       strcpy (p1, "unsigned char");
478       break;
479
480     case bt_Short:              /* short */
481       strcpy (p1, "short");
482       break;
483
484     case bt_UShort:             /* unsigned short */
485       strcpy (p1, "unsigned short");
486       break;
487
488     case bt_Int:                /* int */
489       strcpy (p1, "int");
490       break;
491
492     case bt_UInt:               /* unsigned int */
493       strcpy (p1, "unsigned int");
494       break;
495
496     case bt_Long:               /* long */
497       strcpy (p1, "long");
498       break;
499
500     case bt_ULong:              /* unsigned long */
501       strcpy (p1, "unsigned long");
502       break;
503
504     case bt_Float:              /* float (real) */
505       strcpy (p1, "float");
506       break;
507
508     case bt_Double:             /* Double (real) */
509       strcpy (p1, "double");
510       break;
511
512       /* Structures add 1-2 aux words:
513          1st word is [ST_RFDESCAPE, offset] pointer to struct def;
514          2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
515
516     case bt_Struct:             /* Structure (Record) */
517       emit_aggregate (p1, aux_ptr[index], aux_ptr[index+1], "struct", fdp);
518       used_ptr[index] = 1;
519       if (aux_ptr[index].rndx.rfd == ST_RFDESCAPE)
520         used_ptr[++index] = 1;
521
522       index++;                  /* skip aux words */
523       break;
524
525       /* Unions add 1-2 aux words:
526          1st word is [ST_RFDESCAPE, offset] pointer to union def;
527          2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
528
529     case bt_Union:              /* Union */
530       emit_aggregate (p1, aux_ptr[index], aux_ptr[index+1], "union", fdp);
531       used_ptr[index] = 1;
532       if (aux_ptr[index].rndx.rfd == ST_RFDESCAPE)
533         used_ptr[++index] = 1;
534
535       index++;                  /* skip aux words */
536       break;
537
538       /* Enumerations add 1-2 aux words:
539          1st word is [ST_RFDESCAPE, offset] pointer to enum def;
540          2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
541
542     case bt_Enum:               /* Enumeration */
543       emit_aggregate (p1, aux_ptr[index], aux_ptr[index+1], "enum", fdp);
544       used_ptr[index] = 1;
545       if (aux_ptr[index].rndx.rfd == ST_RFDESCAPE)
546         used_ptr[++index] = 1;
547
548       index++;                  /* skip aux words */
549       break;
550
551     case bt_Typedef:            /* defined via a typedef, isymRef points */
552       strcpy (p1, "typedef");
553       break;
554
555     case bt_Range:              /* subrange of int */
556       strcpy (p1, "subrange");
557       break;
558
559     case bt_Set:                /* pascal sets */
560       strcpy (p1, "set");
561       break;
562
563     case bt_Complex:            /* fortran complex */
564       strcpy (p1, "complex");
565       break;
566
567     case bt_DComplex:           /* fortran double complex */
568       strcpy (p1, "double complex");
569       break;
570
571     case bt_Indirect:           /* forward or unnamed typedef */
572       strcpy (p1, "forward/unnamed typedef");
573       break;
574
575     case bt_FixedDec:           /* Fixed Decimal */
576       strcpy (p1, "fixed decimal");
577       break;
578
579     case bt_FloatDec:           /* Float Decimal */
580       strcpy (p1, "float decimal");
581       break;
582
583     case bt_String:             /* Varying Length Character String */
584       strcpy (p1, "string");
585       break;
586
587     case bt_Bit:                /* Aligned Bit String */
588       strcpy (p1, "bit");
589       break;
590
591     case bt_Picture:            /* Picture */
592       strcpy (p1, "picture");
593       break;
594
595     case bt_Void:               /* Void */
596       strcpy (p1, "void");
597       break;
598
599     default:
600       sprintf (p1, "Unknown basic type %d", (int) basic_type);
601       break;
602     }
603
604   p1 += strlen (buffer1);
605
606   /*
607    * If this is a bitfield, get the bitsize.
608    */
609   if (u.ti.fBitfield)
610     {
611       int bitsize;
612
613       used_ptr[index] = 1;
614       bitsize = aux_ptr[index++].width;
615       sprintf (p1, " : %d", bitsize);
616       p1 += strlen (buffer1);
617     }
618
619
620   /*
621    * Deal with any qualifiers.
622    */
623   if (qualifiers[0].type != tq_Nil)
624     {
625       /*
626        * Snarf up any array bounds in the correct order.  Arrays
627        * store 5 successive words in the aux. table:
628        *        word 0  RNDXR to type of the bounds (i.e., int)
629        *        word 1  Current file descriptor index
630        *        word 2  low bound
631        *        word 3  high bound (or -1 if [])
632        *        word 4  stride size in bits
633        */
634       for (i = 0; i < 7; i++)
635         {
636           if (qualifiers[i].type == tq_Array)
637             {
638               qualifiers[i].low_bound  = aux_ptr[index+2].dnLow;
639               qualifiers[i].high_bound = aux_ptr[index+3].dnHigh;
640               qualifiers[i].stride     = aux_ptr[index+4].width;
641               used_ptr[index] = 1;
642               used_ptr[index+1] = 1;
643               used_ptr[index+2] = 1;
644               used_ptr[index+3] = 1;
645               used_ptr[index+4] = 1;
646               index += 5;
647             }
648         }
649
650       /*
651        * Now print out the qualifiers.
652        */
653       for (i = 0; i < 6; i++)
654         {
655           switch (qualifiers[i].type)
656             {
657             case tq_Nil:
658             case tq_Max:
659               break;
660
661             case tq_Ptr:
662               strcpy (p2, "ptr to ");
663               p2 += sizeof ("ptr to ")-1;
664               break;
665
666             case tq_Vol:
667               strcpy (p2, "volatile ");
668               p2 += sizeof ("volatile ")-1;
669               break;
670
671             case tq_Far:
672               strcpy (p2, "far ");
673               p2 += sizeof ("far ")-1;
674               break;
675
676             case tq_Proc:
677               strcpy (p2, "func. ret. ");
678               p2 += sizeof ("func. ret. ");
679               break;
680
681             case tq_Array:
682               {
683                 int first_array = i;
684                 int j;
685
686                 /* Print array bounds reversed (i.e., in the order the C
687                    programmer writes them).  C is such a fun language....  */
688
689                 while (i < 5 && qualifiers[i+1].type == tq_Array)
690                   i++;
691
692                 for (j = i; j >= first_array; j--)
693                   {
694                     strcpy (p2, "array [");
695                     p2 += sizeof ("array [")-1;
696                     if (qualifiers[j].low_bound != 0)
697                       sprintf (p2,
698                                "%ld:%ld {%ld bits}",
699                                (long) qualifiers[j].low_bound,
700                                (long) qualifiers[j].high_bound,
701                                (long) qualifiers[j].stride);
702
703                     else if (qualifiers[j].high_bound != -1)
704                       sprintf (p2,
705                                "%ld {%ld bits}",
706                                (long) (qualifiers[j].high_bound + 1),
707                                (long) (qualifiers[j].stride));
708
709                     else
710                       sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
711
712                     p2 += strlen (p2);
713                     strcpy (p2, "] of ");
714                     p2 += sizeof ("] of ")-1;
715                   }
716               }
717               break;
718             }
719         }
720     }
721
722   strcpy (p2, buffer1);
723   return buffer2;
724 }
725
726 \f
727 /* Print out the global file header for object files.  */
728
729 static void
730 print_global_hdr (struct filehdr *ptr)
731 {
732   char *time = ctime ((time_t *)&ptr->f_timdat);
733   ushort flags = ptr->f_flags;
734
735   printf("Global file header:\n");
736   printf("    %-*s 0x%x\n",    24, "magic number",           (ushort) ptr->f_magic);
737   printf("    %-*s %d\n",      24, "# sections",             (int)    ptr->f_nscns);
738   printf("    %-*s %ld, %s",   24, "timestamp",              (long)   ptr->f_timdat, time);
739   printf("    %-*s %ld\n",     24, "symbolic header offset", (long)   ptr->f_symptr);
740   printf("    %-*s %ld\n",     24, "symbolic header size",   (long)   ptr->f_nsyms);
741   printf("    %-*s %ld\n",     24, "optional header",        (long)   ptr->f_opthdr);
742   printf("    %-*s 0x%x",     24, "flags",                   (ushort) flags);
743
744   if ((flags & F_RELFLG) != 0)
745     printf (", F_RELFLG");
746
747   if ((flags & F_EXEC) != 0)
748     printf (", F_EXEC");
749
750   if ((flags & F_LNNO) != 0)
751     printf (", F_LNNO");
752
753   if ((flags & F_LSYMS) != 0)
754     printf (", F_LSYMS");
755
756   if ((flags & F_MINMAL) != 0)
757     printf (", F_MINMAL");
758
759   if ((flags & F_UPDATE) != 0)
760     printf (", F_UPDATE");
761
762   if ((flags & F_SWABD) != 0)
763     printf (", F_SWABD");
764
765   if ((flags & F_AR16WR) != 0)
766     printf (", F_AR16WR");
767
768   if ((flags & F_AR32WR) != 0)
769     printf (", F_AR32WR");
770
771   if ((flags & F_AR32W) != 0)
772     printf (", F_AR32W");
773
774   if ((flags & F_PATCH) != 0)
775     printf (", F_PATCH/F_NODF");
776
777   printf ("\n\n");
778 }
779
780 \f
781 /* Print out the symbolic header.  */
782
783 static void
784 print_sym_hdr (HDRR *sym_ptr)
785 {
786   int width = 20;
787
788   printf("Symbolic header, magic number = 0x%04x, vstamp = %d.%d:\n\n",
789          sym_ptr->magic & 0xffff,
790          (sym_ptr->vstamp & 0xffff) >> 8,
791          sym_ptr->vstamp & 0xff);
792
793   printf("    %-*s %11s %11s %11s\n", width, "Info", "Offset", "Number", "Bytes");
794   printf("    %-*s %11s %11s %11s\n", width, "====", "======", "======", "=====\n");
795
796   printf("    %-*s %11ld %11ld %11ld [%d]\n", width, "Line numbers",
797          (long) sym_ptr->cbLineOffset,
798          (long) sym_ptr->cbLine,
799          (long) sym_ptr->cbLine,
800          (int) sym_ptr->ilineMax);
801
802   printf("    %-*s %11ld %11ld %11ld\n", width, "Dense numbers",
803          (long) sym_ptr->cbDnOffset,
804          (long) sym_ptr->idnMax,
805          (long) (sym_ptr->idnMax * sizeof (DNR)));
806
807   printf("    %-*s %11ld %11ld %11ld\n", width, "Procedures Tables",
808          (long) sym_ptr->cbPdOffset,
809          (long) sym_ptr->ipdMax,
810          (long) (sym_ptr->ipdMax * sizeof (PDR)));
811
812   printf("    %-*s %11ld %11ld %11ld\n", width, "Local Symbols",
813          (long) sym_ptr->cbSymOffset,
814          (long) sym_ptr->isymMax,
815          (long) (sym_ptr->isymMax * sizeof (SYMR)));
816
817   printf("    %-*s %11ld %11ld %11ld\n", width, "Optimization Symbols",
818          (long) sym_ptr->cbOptOffset,
819          (long) sym_ptr->ioptMax,
820          (long) (sym_ptr->ioptMax * sizeof (OPTR)));
821
822   printf("    %-*s %11ld %11ld %11ld\n", width, "Auxiliary Symbols",
823          (long) sym_ptr->cbAuxOffset,
824          (long) sym_ptr->iauxMax,
825          (long) (sym_ptr->iauxMax * sizeof (AUXU)));
826
827   printf("    %-*s %11ld %11ld %11ld\n", width, "Local Strings",
828          (long) sym_ptr->cbSsOffset,
829          (long) sym_ptr->issMax,
830          (long) sym_ptr->issMax);
831
832   printf("    %-*s %11ld %11ld %11ld\n", width, "External Strings",
833          (long) sym_ptr->cbSsExtOffset,
834          (long) sym_ptr->issExtMax,
835          (long) sym_ptr->issExtMax);
836
837   printf("    %-*s %11ld %11ld %11ld\n", width, "File Tables",
838          (long) sym_ptr->cbFdOffset,
839          (long) sym_ptr->ifdMax,
840          (long) (sym_ptr->ifdMax * sizeof (FDR)));
841
842   printf("    %-*s %11ld %11ld %11ld\n", width, "Relative Files",
843          (long) sym_ptr->cbRfdOffset,
844          (long) sym_ptr->crfd,
845          (long) (sym_ptr->crfd * sizeof (ulong)));
846
847   printf("    %-*s %11ld %11ld %11ld\n", width, "External Symbols",
848          (long) sym_ptr->cbExtOffset,
849          (long) sym_ptr->iextMax,
850          (long) (sym_ptr->iextMax * sizeof (EXTR)));
851 }
852
853 \f
854 /* Print out a symbol.  */
855
856 static void
857 print_symbol (SYMR *sym_ptr, int number, const char *strbase, AUXU *aux_base,
858               int ifd, FDR *fdp)
859 {
860   sc_t storage_class = (sc_t) sym_ptr->sc;
861   st_t symbol_type   = (st_t) sym_ptr->st;
862   ulong index        = sym_ptr->index;
863   char *used_ptr     = aux_used + (aux_base - aux_symbols);
864   scope_t *scope_ptr;
865
866   printf ("\n    Symbol# %d: \"%s\"\n", number, sym_ptr->iss + strbase);
867
868   if (aux_base != (AUXU *) 0 && index != indexNil)
869     switch (symbol_type)
870       {
871       case st_Nil:
872       case st_Label:
873         break;
874
875       case st_File:
876       case st_Block:
877         printf ("      End+1 symbol: %ld\n", index);
878         if (want_scope)
879           {
880             if (free_scope == (scope_t *) 0)
881               scope_ptr = (scope_t *) xmalloc (sizeof (scope_t));
882             else
883               {
884                 scope_ptr = free_scope;
885                 free_scope = scope_ptr->prev;
886               }
887             scope_ptr->open_sym = number;
888             scope_ptr->st = symbol_type;
889             scope_ptr->sc = storage_class;
890             scope_ptr->prev = cur_scope;
891             cur_scope = scope_ptr;
892           }
893         break;
894
895       case st_End:
896         if (storage_class == sc_Text || storage_class == sc_Info)
897           printf ("      First symbol: %ld\n", index);
898         else
899           {
900             used_ptr[index] = 1;
901             printf ("      First symbol: %ld\n", (long) aux_base[index].isym);
902           }
903
904         if (want_scope)
905           {
906             if (cur_scope == (scope_t *) 0)
907               printf ("      Can't pop end scope\n");
908             else
909               {
910                 scope_ptr = cur_scope;
911                 cur_scope = scope_ptr->prev;
912                 scope_ptr->prev = free_scope;
913                 free_scope = scope_ptr;
914               }
915           }
916         break;
917
918       case st_Proc:
919       case st_StaticProc:
920         if (MIPS_IS_STAB(sym_ptr))
921           ;
922         else if (ifd == -1)             /* local symbol */
923           {
924             used_ptr[index] = used_ptr[index+1] = 1;
925             printf ("      End+1 symbol: %-7ld   Type:  %s\n",
926                     (long) aux_base[index].isym,
927                     type_to_string (aux_base, index+1, fdp));
928           }
929         else                    /* global symbol */
930           printf ("      Local symbol: %ld\n", index);
931
932         if (want_scope)
933           {
934             if (free_scope == (scope_t *) 0)
935               scope_ptr = (scope_t *) xmalloc (sizeof (scope_t));
936             else
937               {
938                 scope_ptr = free_scope;
939                 free_scope = scope_ptr->prev;
940               }
941             scope_ptr->open_sym = number;
942             scope_ptr->st = symbol_type;
943             scope_ptr->sc = storage_class;
944             scope_ptr->prev = cur_scope;
945             cur_scope = scope_ptr;
946           }
947         break;
948
949 #ifdef stStruct
950       case st_Struct:
951       case st_Union:
952       case st_Enum:
953         printf ("      End+1 symbol: %lu\n", index);
954         break;
955 #endif
956
957       default:
958         if (!MIPS_IS_STAB (sym_ptr))
959           {
960             used_ptr[index] = 1;
961             printf ("      Type: %s\n",
962                     type_to_string (aux_base, index, fdp));
963           }
964         break;
965       }
966
967   if (want_scope)
968     {
969       printf ("      Scopes:  ");
970       if (cur_scope == (scope_t *) 0)
971         printf (" none\n");
972       else
973         {
974           for (scope_ptr = cur_scope;
975                scope_ptr != (scope_t *) 0;
976                scope_ptr = scope_ptr->prev)
977             {
978               const char *sclass;
979               if (scope_ptr->st == st_Proc || scope_ptr->st == st_StaticProc)
980                 sclass = "func.";
981               else if (scope_ptr->st == st_File)
982                 sclass = "file";
983               else if (scope_ptr->st == st_Block && scope_ptr->sc == sc_Text)
984                 sclass = "block";
985               else if (scope_ptr->st == st_Block && scope_ptr->sc == sc_Info)
986                 sclass = "type";
987               else
988                 sclass = "???";
989
990               printf (" %ld [%s]", scope_ptr->open_sym, sclass);
991             }
992           printf ("\n");
993         }
994     }
995
996   printf ("      Value: %-13ld    ",
997           (long)sym_ptr->value);
998   if (ifd == -1)
999     printf ("String index: %ld\n", (long)sym_ptr->iss);
1000   else
1001     printf ("String index: %-11ld Ifd: %d\n",
1002             (long)sym_ptr->iss, ifd);
1003
1004   printf ("      Symbol type: %-11sStorage class: %-11s",
1005           st_to_string (symbol_type), sc_to_string (storage_class));
1006
1007   if (MIPS_IS_STAB(sym_ptr))
1008     {
1009       int i = ARRAY_SIZE (stab_names);
1010       const char *stab_name = "stab";
1011       short code = MIPS_UNMARK_STAB(sym_ptr->index);
1012
1013       while (--i >= 0)
1014         if (stab_names[i].code == code)
1015           {
1016             stab_name = stab_names[i].string;
1017             break;
1018           }
1019       printf ("Index: 0x%lx (%s)\n", (long)sym_ptr->index, stab_name);
1020     }
1021   else if (sym_ptr->st == stLabel && sym_ptr->index != indexNil)
1022     printf ("Index: %ld (line#)\n", (long)sym_ptr->index);
1023   else
1024     printf ("Index: %ld\n", (long)sym_ptr->index);
1025
1026 }
1027
1028 \f
1029 /* Print out a word from the aux. table in various formats.  */
1030
1031 static void
1032 print_aux (AUXU u, int auxi, int used)
1033 {
1034   printf ("\t%s#%-5d %11ld, [%4ld/%7ld], [%2d %1d:%1d %1x:%1x:%1x:%1x:%1x:%1x]\n",
1035           (used) ? "  " : "* ",
1036           auxi,
1037           (long) u.isym,
1038           (long) u.rndx.rfd,
1039           (long) u.rndx.index,
1040           u.ti.bt,
1041           u.ti.fBitfield,
1042           u.ti.continued,
1043           u.ti.tq0,
1044           u.ti.tq1,
1045           u.ti.tq2,
1046           u.ti.tq3,
1047           u.ti.tq4,
1048           u.ti.tq5);
1049 }
1050
1051 \f
1052 /* Write aggregate information to a string.  */
1053
1054 static void
1055 emit_aggregate (char *string, AUXU u, AUXU u2, const char *which, FDR *fdp)
1056 {
1057   unsigned int ifd = u.rndx.rfd;
1058   unsigned int index = u.rndx.index;
1059   const char *name;
1060
1061   if (ifd == ST_RFDESCAPE)
1062     ifd = u2.isym;
1063
1064   /* An ifd of -1 is an opaque type.  An escaped index of 0 is a
1065      struct return type of a procedure compiled without -g.  */
1066   if (ifd == 0xffffffff
1067       || (u.rndx.rfd == ST_RFDESCAPE && index == 0))
1068     name = "<undefined>";
1069   else if (index == indexNil)
1070     name = "<no name>";
1071   else
1072     {
1073       if (fdp == 0 || sym_hdr.crfd == 0)
1074         fdp = &file_desc[ifd];
1075       else
1076         fdp = &file_desc[rfile_desc[fdp->rfdBase + ifd]];
1077       name = &l_strings[fdp->issBase + l_symbols[index + fdp->isymBase].iss];
1078     }
1079
1080   sprintf (string,
1081            "%s %s { ifd = %u, index = %u }",
1082            which, name, ifd, index);
1083 }
1084
1085 \f
1086 /* Print out information about a file descriptor, and the symbols,
1087    procedures, and line numbers within it.  */
1088
1089 static void
1090 print_file_desc (FDR *fdp, int number)
1091 {
1092   char *str_base;
1093   AUXU *aux_base;
1094   int symi, pdi;
1095   int width = 20;
1096   char *used_base;
1097
1098   str_base = l_strings + fdp->issBase;
1099   aux_base = aux_symbols + fdp->iauxBase;
1100   used_base = aux_used + (aux_base - aux_symbols);
1101
1102   printf ("\nFile #%d, \"%s\"\n\n",
1103           number,
1104           fdp->rss != issNil ? str_base + fdp->rss : "<unknown>");
1105
1106   printf ("    Name index  = %-10ld Readin      = %s\n",
1107           (long) fdp->rss, (fdp->fReadin) ? "Yes" : "No");
1108
1109   printf ("    Merge       = %-10s Endian      = %s\n",
1110           (fdp->fMerge)  ? "Yes" : "No",
1111           (fdp->fBigendian) ? "BIG" : "LITTLE");
1112
1113   printf ("    Debug level = %-10s Language    = %s\n",
1114           glevel_to_string ((glevel_t) fdp->glevel),
1115           lang_to_string((lang_t) fdp->lang));
1116
1117   printf ("    Adr         = 0x%08lx\n\n", (long) fdp->adr);
1118
1119   printf("    %-*s %11s %11s %11s %11s\n", width, "Info", "Start", "Number", "Size", "Offset");
1120   printf("    %-*s %11s %11s %11s %11s\n", width, "====", "=====", "======", "====", "======");
1121
1122   printf("    %-*s %11lu %11lu %11lu %11lu\n",
1123          width, "Local strings",
1124          (ulong) fdp->issBase,
1125          (ulong) fdp->cbSs,
1126          (ulong) fdp->cbSs,
1127          (ulong) (fdp->issBase + sym_hdr.cbSsOffset));
1128
1129   printf("    %-*s %11lu %11lu %11lu %11lu\n",
1130          width, "Local symbols",
1131          (ulong) fdp->isymBase,
1132          (ulong) fdp->csym,
1133          (ulong) (fdp->csym * sizeof (SYMR)),
1134          (ulong) (fdp->isymBase * sizeof (SYMR) + sym_hdr.cbSymOffset));
1135
1136   printf("    %-*s %11lu %11lu %11lu %11lu\n",
1137          width, "Line numbers",
1138          (ulong) fdp->cbLineOffset,
1139          (ulong) fdp->cline,
1140          (ulong) fdp->cbLine,
1141          (ulong) (fdp->cbLineOffset + sym_hdr.cbLineOffset));
1142
1143   printf("    %-*s %11lu %11lu %11lu %11lu\n",
1144          width, "Optimization symbols",
1145          (ulong) fdp->ioptBase,
1146          (ulong) fdp->copt,
1147          (ulong) (fdp->copt * sizeof (OPTR)),
1148          (ulong) (fdp->ioptBase * sizeof (OPTR) + sym_hdr.cbOptOffset));
1149
1150   printf("    %-*s %11lu %11lu %11lu %11lu\n",
1151          width, "Procedures",
1152          (ulong) fdp->ipdFirst,
1153          (ulong) fdp->cpd,
1154          (ulong) (fdp->cpd * sizeof (PDR)),
1155          (ulong) (fdp->ipdFirst * sizeof (PDR) + sym_hdr.cbPdOffset));
1156
1157   printf("    %-*s %11lu %11lu %11lu %11lu\n",
1158          width, "Auxiliary symbols",
1159          (ulong) fdp->iauxBase,
1160          (ulong) fdp->caux,
1161          (ulong) (fdp->caux * sizeof (AUXU)),
1162          (ulong) (fdp->iauxBase * sizeof(AUXU) + sym_hdr.cbAuxOffset));
1163
1164   printf("    %-*s %11lu %11lu %11lu %11lu\n",
1165          width, "Relative Files",
1166          (ulong) fdp->rfdBase,
1167          (ulong) fdp->crfd,
1168          (ulong) (fdp->crfd * sizeof (ulong)),
1169          (ulong) (fdp->rfdBase * sizeof(ulong) + sym_hdr.cbRfdOffset));
1170
1171
1172   if (want_scope && cur_scope != (scope_t *) 0)
1173     printf ("\n    Warning scope does not start at 0!\n");
1174
1175   /*
1176    * print the info about the symbol table.
1177    */
1178   printf ("\n    There are %lu local symbols, starting at %lu\n",
1179           (ulong) fdp->csym,
1180           (ulong) (fdp->isymBase + sym_hdr.cbSymOffset));
1181
1182   for(symi = fdp->isymBase; symi < (fdp->csym + fdp->isymBase); symi++)
1183     print_symbol (&l_symbols[symi],
1184                   symi - fdp->isymBase,
1185                   str_base,
1186                   aux_base,
1187                   -1,
1188                   fdp);
1189
1190   if (want_scope && cur_scope != (scope_t *) 0)
1191     printf ("\n    Warning scope does not end at 0!\n");
1192
1193   /*
1194    * print the aux. table if desired.
1195    */
1196
1197   if (want_aux && fdp->caux != 0)
1198     {
1199       int auxi;
1200
1201       printf ("\n    There are %lu auxiliary table entries, starting at %lu.\n\n",
1202               (ulong) fdp->caux,
1203               (ulong) (fdp->iauxBase + sym_hdr.cbAuxOffset));
1204
1205       for (auxi = fdp->iauxBase; auxi < (fdp->caux + fdp->iauxBase); auxi++)
1206         print_aux (aux_base[auxi], auxi, used_base[auxi]);
1207     }
1208
1209   /*
1210    * print the relative file descriptors.
1211    */
1212   if (want_rfd && fdp->crfd != 0)
1213     {
1214       ulong *rfd_ptr, i;
1215
1216       printf ("\n    There are %lu relative file descriptors, starting at %lu.\n",
1217               (ulong) fdp->crfd,
1218               (ulong) fdp->rfdBase);
1219
1220       rfd_ptr = rfile_desc + fdp->rfdBase;
1221       for (i = 0; i < (ulong) fdp->crfd; i++)
1222         {
1223           printf ("\t#%-5ld %11ld, 0x%08lx\n", i, *rfd_ptr, *rfd_ptr);
1224           rfd_ptr++;
1225         }
1226     }
1227
1228   /*
1229    * do the procedure descriptors.
1230    */
1231   printf ("\n    There are %lu procedure descriptor entries, ", (ulong) fdp->cpd);
1232   printf ("starting at %lu.\n", (ulong) fdp->ipdFirst);
1233
1234   for (pdi = fdp->ipdFirst; pdi < (fdp->cpd + fdp->ipdFirst); pdi++)
1235     {
1236       PDR *proc_ptr = &proc_desc[pdi];
1237       printf ("\n\tProcedure descriptor %d:\n", (pdi - fdp->ipdFirst));
1238
1239       if (l_symbols != 0)
1240         printf ("\t    Name index   = %-11ld Name          = \"%s\"\n",
1241                 (long) l_symbols[proc_ptr->isym + fdp->isymBase].iss,
1242                 l_symbols[proc_ptr->isym + fdp->isymBase].iss + str_base);
1243
1244       printf ("\t    .mask 0x%08lx,%-9ld .fmask 0x%08lx,%ld\n",
1245               (long) proc_ptr->regmask,
1246               (long) proc_ptr->regoffset,
1247               (long) proc_ptr->fregmask,
1248               (long) proc_ptr->fregoffset);
1249
1250       printf ("\t    .frame $%d,%ld,$%d\n",
1251               (int)  proc_ptr->framereg,
1252               (long) proc_ptr->frameoffset,
1253               (int)  proc_ptr->pcreg);
1254
1255       printf ("\t    Opt. start   = %-11ld Symbols start = %ld\n",
1256               (long) proc_ptr->iopt,
1257               (long) proc_ptr->isym);
1258
1259       printf ("\t    First line # = %-11ld Last line #   = %ld\n",
1260               (long) proc_ptr->lnLow,
1261               (long) proc_ptr->lnHigh);
1262
1263       printf ("\t    Line Offset  = %-11ld Address       = 0x%08lx\n",
1264               (long) proc_ptr->cbLineOffset,
1265               (long) proc_ptr->adr);
1266
1267       /*
1268        * print the line number entries.
1269        */
1270
1271       if (want_line && fdp->cline != 0)
1272         {
1273           int delta, count;
1274           long cur_line = proc_ptr->lnLow;
1275           uchar *line_ptr = (((uchar *)lines) + proc_ptr->cbLineOffset
1276                              + fdp->cbLineOffset);
1277           uchar *line_end;
1278
1279           if (pdi == fdp->cpd + fdp->ipdFirst - 1)      /* last procedure */
1280             line_end = ((uchar *)lines) + fdp->cbLine + fdp->cbLineOffset;
1281           else                                          /* not last proc.  */
1282             line_end = (((uchar *)lines) + proc_desc[pdi+1].cbLineOffset
1283                         + fdp->cbLineOffset);
1284
1285           printf ("\n\tThere are %lu bytes holding line numbers, starting at %lu.\n",
1286                   (ulong) (line_end - line_ptr),
1287                   (ulong) (fdp->ilineBase + sym_hdr.cbLineOffset));
1288
1289           while (line_ptr < line_end)
1290             {                                           /* sign extend nibble */
1291               delta = ((*line_ptr >> 4) ^ 0x8) - 0x8;
1292               count = (*line_ptr & 0xf) + 1;
1293               if (delta != -8)
1294                 line_ptr++;
1295               else
1296                 {
1297                   delta = (((line_ptr[1]) & 0xff) << 8) + ((line_ptr[2]) & 0xff);
1298                   delta = (delta ^ 0x8000) - 0x8000;
1299                   line_ptr += 3;
1300                 }
1301
1302               cur_line += delta;
1303               printf ("\t    Line %11ld,   delta %5d,   count %2d\n",
1304                       cur_line,
1305                       delta,
1306                       count);
1307             }
1308         }
1309     }
1310 }
1311
1312 \f
1313 /* Read in the portions of the .T file that we will print out.  */
1314
1315 static void
1316 read_tfile (void)
1317 {
1318   short magic;
1319   off_t sym_hdr_offset = 0;
1320
1321   read_seek (&magic, sizeof (magic), 0, "Magic number");
1322   if (!tfile)
1323     {
1324       /* Print out the global header, since this is not a T-file.  */
1325
1326       read_seek (&global_hdr, sizeof (global_hdr), 0, "Global file header");
1327
1328       print_global_hdr (&global_hdr);
1329
1330       if (global_hdr.f_symptr == 0)
1331         {
1332           printf ("No symbolic header, Goodbye!\n");
1333           exit (1);
1334         }
1335
1336       sym_hdr_offset = global_hdr.f_symptr;
1337     }
1338
1339   read_seek (&sym_hdr, sizeof (sym_hdr), sym_hdr_offset, "Symbolic header");
1340
1341   print_sym_hdr (&sym_hdr);
1342
1343   lines = (LINER *) read_seek (NULL, sym_hdr.cbLine, sym_hdr.cbLineOffset,
1344                                "Line numbers");
1345
1346   dense_nums = (DNR *) read_seek (NULL, sym_hdr.idnMax * sizeof (DNR),
1347                                   sym_hdr.cbDnOffset, "Dense numbers");
1348
1349   proc_desc = (PDR *) read_seek (NULL, sym_hdr.ipdMax * sizeof (PDR),
1350                                  sym_hdr.cbPdOffset, "Procedure tables");
1351
1352   l_symbols = (SYMR *) read_seek (NULL, sym_hdr.isymMax * sizeof (SYMR),
1353                                   sym_hdr.cbSymOffset, "Local symbols");
1354
1355   opt_symbols = (OPTR *) read_seek (NULL, sym_hdr.ioptMax * sizeof (OPTR),
1356                                     sym_hdr.cbOptOffset,
1357                                     "Optimization symbols");
1358
1359   aux_symbols = (AUXU *) read_seek (NULL, sym_hdr.iauxMax * sizeof (AUXU),
1360                                     sym_hdr.cbAuxOffset, "Auxiliary symbols");
1361
1362   if (sym_hdr.iauxMax > 0)
1363     aux_used = (char *) xcalloc (sym_hdr.iauxMax, 1);
1364
1365   l_strings = (char *) read_seek (NULL, sym_hdr.issMax,
1366                                   sym_hdr.cbSsOffset, "Local string table");
1367
1368   e_strings = (char *) read_seek (NULL, sym_hdr.issExtMax,
1369                                   sym_hdr.cbSsExtOffset,
1370                                   "External string table");
1371
1372   file_desc = (FDR *) read_seek (NULL, sym_hdr.ifdMax * sizeof (FDR),
1373                                  sym_hdr.cbFdOffset, "File tables");
1374
1375   rfile_desc = (ulong *) read_seek (NULL, sym_hdr.crfd * sizeof (ulong),
1376                                     sym_hdr.cbRfdOffset,
1377                                     "Relative file tables");
1378
1379   e_symbols = (EXTR *) read_seek (NULL, sym_hdr.iextMax * sizeof (EXTR),
1380                                   sym_hdr.cbExtOffset, "External symbols");
1381 }
1382
1383 \f
1384
1385 extern int main (int, char **);
1386
1387 int
1388 main (int argc, char **argv)
1389 {
1390   int i, opt;
1391
1392   /*
1393    * Process arguments
1394    */
1395   while ((opt = getopt_long (argc, argv, "alrsvt", options, NULL)) != -1)
1396     switch (opt)
1397       {
1398       default:  errors++;       break;
1399       case 'a': want_aux++;     break;  /* print aux table */
1400       case 'l': want_line++;    break;  /* print line numbers */
1401       case 'r': want_rfd++;     break;  /* print relative fd's */
1402       case 's': want_scope++;   break;  /* print scope info */
1403       case 'v': verbose++;      break;  /* print version # */
1404       case 'V': version++;      break;  /* print version # */
1405       case 't': tfile++;        break;  /* this is a tfile (without header),
1406                                            and not a .o */
1407       }
1408
1409   if (version)
1410     {
1411       printf ("mips-tdump %s%s\n", pkgversion_string, version_string);
1412       fputs ("Copyright (C) 2011 Free Software Foundation, Inc.\n", stdout);
1413       fputs ("This is free software; see the source for copying conditions.  There is NO\n\
1414 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n",
1415              stdout);
1416       exit (0);
1417     }
1418
1419   if (optind != argc - 1)
1420     errors++;
1421
1422   if (verbose || errors)
1423     fprintf (stderr, "mips-tdump (GCC) %s\n", version_string);
1424
1425   if (errors)
1426     {
1427       fprintf (stderr, "Calling Sequence:\n");
1428       fprintf (stderr, "\t%s [-alrst] <object-or-T-file>\n", argv[0]);
1429       fprintf (stderr, "\n");
1430       fprintf (stderr, "switches:\n");
1431       fprintf (stderr, "\t-a Print out auxiliary table.\n");
1432       fprintf (stderr, "\t-l Print out line numbers.\n");
1433       fprintf (stderr, "\t-r Print out relative file descriptors.\n");
1434       fprintf (stderr, "\t-s Print out the current scopes for an item.\n");
1435       fprintf (stderr, "\t-t Assume there is no global header (ie, a T-file).\n");
1436       fprintf (stderr, "\t-v Print program version.\n");
1437       return 1;
1438     }
1439
1440   /*
1441    * Open and process the input file.
1442    */
1443   tfile_fd = open (argv[optind], O_RDONLY);
1444   if (tfile_fd < 0)
1445     {
1446       perror (argv[optind]);
1447       return 1;
1448     }
1449
1450   read_tfile ();
1451
1452   /*
1453    * Print any global aux words if any.
1454    */
1455   if (want_aux)
1456     {
1457       long last_aux_in_use;
1458
1459       if (sym_hdr.ifdMax != 0 && file_desc[0].iauxBase != 0)
1460         {
1461           printf ("\nGlobal auxiliary entries before first file:\n");
1462           for (i = 0; i < file_desc[0].iauxBase; i++)
1463             print_aux (aux_symbols[i], 0, aux_used[i]);
1464         }
1465
1466       if (sym_hdr.ifdMax == 0)
1467         last_aux_in_use = 0;
1468       else
1469         last_aux_in_use
1470           = (file_desc[sym_hdr.ifdMax-1].iauxBase
1471              + file_desc[sym_hdr.ifdMax-1].caux - 1);
1472
1473       if (last_aux_in_use < sym_hdr.iauxMax-1)
1474         {
1475           printf ("\nGlobal auxiliary entries after last file:\n");
1476           for (i = last_aux_in_use; i < sym_hdr.iauxMax; i++)
1477             print_aux (aux_symbols[i], i - last_aux_in_use, aux_used[i]);
1478         }
1479     }
1480
1481   /*
1482    * Print the information for each file.
1483    */
1484   for (i = 0; i < sym_hdr.ifdMax; i++)
1485     print_file_desc (&file_desc[i], i);
1486
1487   /*
1488    * Print the external symbols.
1489    */
1490   want_scope = 0;               /* scope info is meaning for extern symbols */
1491   printf ("\nThere are %lu external symbols, starting at %lu\n",
1492           (ulong) sym_hdr.iextMax,
1493           (ulong) sym_hdr.cbExtOffset);
1494
1495   for(i = 0; i < sym_hdr.iextMax; i++)
1496     print_symbol (&e_symbols[i].asym, i, e_strings,
1497                   aux_symbols + file_desc[e_symbols[i].ifd].iauxBase,
1498                   e_symbols[i].ifd,
1499                   &file_desc[e_symbols[i].ifd]);
1500
1501   /*
1502    * Print unused aux symbols now.
1503    */
1504
1505   if (want_aux)
1506     {
1507       int first_time = 1;
1508
1509       for (i = 0; i < sym_hdr.iauxMax; i++)
1510         {
1511           if (! aux_used[i])
1512             {
1513               if (first_time)
1514                 {
1515                   printf ("\nThe following auxiliary table entries were unused:\n\n");
1516                   first_time = 0;
1517                 }
1518
1519               printf ("    #%-5d %11ld  0x%08lx  %s\n",
1520                       i,
1521                       (long) aux_symbols[i].isym,
1522                       (long) aux_symbols[i].isym,
1523                       type_to_string (aux_symbols, i, (FDR *) 0));
1524             }
1525         }
1526     }
1527
1528   return 0;
1529 }