mips-tdump.c 41 KB
Newer Older
Richard Stallman committed
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 5
   Contributed by hartzell@boulder.colorado.edu,
   Rewritten by meissner@osf.org.
Richard Stallman committed
6

7
This file is part of GCC.
Richard Stallman committed
8

9 10
GCC is free software; you can redistribute it and/or modify it under
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.
Richard Stallman committed
13

14 15 16 17
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.
Richard Stallman committed
18 19

You should have received a copy of the GNU General Public License
20 21
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
Richard Stallman committed
22

Jeff Law committed
23
#include "config.h"
24
#include "system.h"
25
#include "version.h"
26 27 28
#ifdef index
#undef index
#endif
29 30
#include <a.out.h>

31 32 33
/* Include getopt.h for the sake of getopt_long.  */
#include "getopt.h"

Michael Meissner committed
34
/* Macros for mips-tfile.c to encapsulate stabs in ECOFF, and for
35
   mips-tdump.c to print them out.
Michael Meissner committed
36 37

   These must match the corresponding definitions in gdb/mipsread.c.
Mike Stump committed
38
   Unfortunately, gcc and gdb do not currently share any directories.  */
Michael Meissner committed
39 40 41 42 43 44

#define CODE_MASK 0x8F300
#define MIPS_IS_STAB(sym) (((sym)->index & 0xFFF00) == CODE_MASK)
#define MIPS_MARK_STAB(code) ((code)+CODE_MASK)
#define MIPS_UNMARK_STAB(code) ((code)-CODE_MASK)

Richard Stallman committed
45 46 47 48 49 50
#define uchar	unsigned char
#define ushort	unsigned short
#define uint	unsigned int
#define ulong	unsigned long


Jeff Law committed
51
/* Redefinition of storage classes as an enumeration for better
Richard Stallman committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
   debugging.  */

#ifndef stStaParam
#define stStaParam	16	/* Fortran static parameters */
#endif

#ifndef btVoid
#define btVoid		26	/* void basic type */
#endif

typedef enum sc {
  sc_Nil	 = scNil,	  /* no storage class */
  sc_Text	 = scText,	  /* text symbol */
  sc_Data	 = scData,	  /* initialized data symbol */
  sc_Bss	 = scBss,	  /* un-initialized data symbol */
  sc_Register	 = scRegister,	  /* value of symbol is register number */
  sc_Abs	 = scAbs,	  /* value of symbol is absolute */
  sc_Undefined	 = scUndefined,	  /* who knows? */
  sc_CdbLocal	 = scCdbLocal,	  /* variable's value is IN se->va.?? */
  sc_Bits	 = scBits,	  /* this is a bit field */
  sc_CdbSystem	 = scCdbSystem,	  /* var's value is IN CDB's address space */
  sc_RegImage	 = scRegImage,	  /* register value saved on stack */
  sc_Info	 = scInfo,	  /* symbol contains debugger information */
  sc_UserStruct	 = scUserStruct,  /* addr in struct user for current process */
  sc_SData	 = scSData,	  /* load time only small data */
  sc_SBss	 = scSBss,	  /* load time only small common */
  sc_RData	 = scRData,	  /* load time only read only data */
  sc_Var	 = scVar,	  /* Var parameter (fortran,pascal) */
  sc_Common	 = scCommon,	  /* common variable */
  sc_SCommon	 = scSCommon,	  /* small common */
  sc_VarRegister = scVarRegister, /* Var parameter in a register */
  sc_Variant	 = scVariant,	  /* Variant record */
  sc_SUndefined	 = scSUndefined,  /* small undefined(external) data */
  sc_Init	 = scInit,	  /* .init section symbol */
  sc_Max	 = scMax	  /* Max storage class+1 */
} sc_t;

/* Redefinition of symbol type.  */

typedef enum st {
  st_Nil	= stNil,	/* Nuthin' special */
  st_Global	= stGlobal,	/* external symbol */
  st_Static	= stStatic,	/* static */
  st_Param	= stParam,	/* procedure argument */
  st_Local	= stLocal,	/* local variable */
  st_Label	= stLabel,	/* label */
  st_Proc	= stProc,	/*     "      "	 Procedure */
Charles Hannum committed
99
  st_Block	= stBlock,	/* beginning of block */
Richard Stallman committed
100 101 102 103 104 105 106 107 108
  st_End	= stEnd,	/* end (of anything) */
  st_Member	= stMember,	/* member (of anything	- struct/union/enum */
  st_Typedef	= stTypedef,	/* type definition */
  st_File	= stFile,	/* file name */
  st_RegReloc	= stRegReloc,	/* register relocation */
  st_Forward	= stForward,	/* forwarding address */
  st_StaticProc	= stStaticProc,	/* load time only static procs */
  st_StaParam	= stStaParam,	/* Fortran static parameters */
  st_Constant	= stConstant,	/* const */
109 110 111 112 113
#ifdef stStruct
  st_Struct	= stStruct,	/* struct */
  st_Union	= stUnion,	/* union */
  st_Enum	= stEnum,	/* enum */
#endif
Richard Stallman committed
114
  st_Str	= stStr,	/* string */
115
  st_Number	= stNumber,	/* pure number (i.e. 4 NOR 2+2) */
Richard Stallman committed
116
  st_Expr	= stExpr,	/* 2+2 vs. 4 */
Charles Hannum committed
117
  st_Type	= stType,	/* post-coercion SER */
Richard Stallman committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
  st_Max	= stMax		/* max type+1 */
} st_t;

/* Redefinition of type qualifiers.  */

typedef enum tq {
  tq_Nil	= tqNil,	/* bt is what you see */
  tq_Ptr	= tqPtr,	/* pointer */
  tq_Proc	= tqProc,	/* procedure */
  tq_Array	= tqArray,	/* duh */
  tq_Far	= tqFar,	/* longer addressing - 8086/8 land */
  tq_Vol	= tqVol,	/* volatile */
  tq_Max	= tqMax		/* Max type qualifier+1 */
} tq_t;

/* Redefinition of basic types.  */

typedef enum bt {
  bt_Nil	= btNil,	/* undefined */
  bt_Adr	= btAdr,	/* address - integer same size as pointer */
  bt_Char	= btChar,	/* character */
  bt_UChar	= btUChar,	/* unsigned character */
  bt_Short	= btShort,	/* short */
  bt_UShort	= btUShort,	/* unsigned short */
  bt_Int	= btInt,	/* int */
  bt_UInt	= btUInt,	/* unsigned int */
  bt_Long	= btLong,	/* long */
  bt_ULong	= btULong,	/* unsigned long */
  bt_Float	= btFloat,	/* float (real) */
  bt_Double	= btDouble,	/* Double (real) */
  bt_Struct	= btStruct,	/* Structure (Record) */
  bt_Union	= btUnion,	/* Union (variant) */
  bt_Enum	= btEnum,	/* Enumerated */
  bt_Typedef	= btTypedef,	/* defined via a typedef, isymRef points */
  bt_Range	= btRange,	/* subrange of int */
  bt_Set	= btSet,	/* pascal sets */
  bt_Complex	= btComplex,	/* fortran complex */
  bt_DComplex	= btDComplex,	/* fortran double complex */
  bt_Indirect	= btIndirect,	/* forward or unnamed typedef */
  bt_FixedDec	= btFixedDec,	/* Fixed Decimal */
  bt_FloatDec	= btFloatDec,	/* Float Decimal */
  bt_String	= btString,	/* Varying Length Character String */
  bt_Bit	= btBit,	/* Aligned Bit String */
  bt_Picture	= btPicture,	/* Picture */
  bt_Void	= btVoid,	/* void */
  bt_Max	= btMax		/* Max basic type+1 */
} bt_t;

/* Redefinition of the language codes.  */

typedef enum lang {
  lang_C	 = langC,
  lang_Pascal	 = langPascal,
  lang_Fortran	 = langFortran,
  lang_Assembler = langAssembler,
  lang_Machine	 = langMachine,
  lang_Nil	 = langNil,
  lang_Ada	 = langAda,
  lang_Pl1	 = langPl1,
  lang_Cobol	 = langCobol
} lang_t;

/* Redefinition of the debug level codes.  */

typedef enum glevel {
  glevel_0	= GLEVEL_0,
  glevel_1	= GLEVEL_1,
  glevel_2	= GLEVEL_2,
  glevel_3	= GLEVEL_3
} glevel_t;


/* Keep track of the active scopes.  */
typedef struct scope {
  struct scope *prev;		/* previous scope */
  ulong open_sym;		/* symbol opening scope */
  sc_t sc;			/* storage class */
  st_t st;			/* symbol type */
} scope_t;

struct filehdr global_hdr;	/* a.out header */

int	 errors		= 0;	/* # of errors */
int	 want_aux	= 0;	/* print aux table */
int	 want_line	= 0;	/* print line numbers */
int	 want_rfd	= 0;	/* print relative file desc's */
int	 want_scope	= 0;	/* print scopes for every symbol */
Michael Meissner committed
205
int	 tfile		= 0;	/* no global header file */
206 207
int	 version	= 0;    /* print version # */
int	 verbose	= 0;
Richard Stallman committed
208 209 210 211 212 213 214 215 216 217 218 219
int	 tfile_fd;		/* file descriptor of .T file */
off_t	 tfile_offset;		/* current offset in .T file */
scope_t	*cur_scope	= 0;	/* list of active scopes */
scope_t	*free_scope	= 0;	/* list of freed scopes */
HDRR	 sym_hdr;		/* symbolic header */
char	*l_strings;		/* local strings */
char	*e_strings;		/* external strings */
SYMR	*l_symbols;		/* local symbols */
EXTR	*e_symbols;		/* external symbols */
LINER	*lines;			/* line numbers */
DNR	*dense_nums;		/* dense numbers */
OPTR	*opt_symbols;		/* optimization symbols */
Charles Hannum committed
220
AUXU	*aux_symbols;		/* Auxiliary symbols */
Richard Stallman committed
221 222 223 224 225 226
char	*aux_used;		/* map of which aux syms are used */
FDR	*file_desc;		/* file tables */
ulong	*rfile_desc;		/* relative file tables */
PDR	*proc_desc;		/* procedure tables */

/* Forward reference for functions.  */
227 228 229 230 231 232 233 234 235 236 237 238 239
static void *read_seek (void *, size_t, off_t, const char *);
static void read_tfile (void);
static void print_global_hdr (struct filehdr *);
static void print_sym_hdr (HDRR *);
static void print_file_desc (FDR *, int);
static void print_symbol (SYMR *, int, const char *, AUXU *, int, FDR *);
static void print_aux (AUXU, int, int);
static void emit_aggregate (char *, AUXU, AUXU, const char *, FDR *);
static const char *st_to_string (st_t);
static const char *sc_to_string (sc_t);
static const char *glevel_to_string (glevel_t);
static const char *lang_to_string (lang_t);
static const char *type_to_string (AUXU *, int, FDR *);
Richard Stallman committed
240 241 242 243 244 245 246 247

extern char *optarg;
extern int   optind;
extern int   opterr;

/* Create a table of debugging stab-codes and corresponding names.  */

#define __define_stab(NAME, CODE, STRING) {(int)CODE, STRING},
248
const struct {const short code; const char string[10];} stab_names[]  = {
Richard Stallman committed
249 250 251 252
#include "stab.def"
#undef __define_stab
};

253 254 255 256 257 258 259 260
/* Command line options for getopt_long.  */

static const struct option options[] =
{
  { "version", 0, 0, 'V' },
  { "verbose", 0, 0, 'v' },
  { 0, 0, 0, 0 }
};
Richard Stallman committed
261

262 263 264 265
/* Read some bytes at a specified location, and return a pointer.
   Read_seek takes a pointer PTR to a buffer or NULL and reads SIZE
   bytes from offset OFFSET.  In case of errors CONTEXT is used as
   error message.  */
Richard Stallman committed
266

267
static void *
268
read_seek (void *ptr, size_t size, off_t offset,  const char *context)
Richard Stallman committed
269 270 271 272 273 274
{
  long read_size = 0;

  if (size == 0)		/* nothing to read */
    return ptr;

275 276 277 278
  if (!ptr)
    ptr = xmalloc (size);

  if ((tfile_offset != offset && lseek (tfile_fd, offset, 0) == -1)
Richard Stallman committed
279 280 281 282 283 284
      || (read_size = read (tfile_fd, ptr, size)) < 0)
    {
      perror (context);
      exit (1);
    }

285
  if (read_size != (long) size)
Richard Stallman committed
286 287 288 289 290 291 292 293 294 295 296 297 298
    {
      fprintf (stderr, "%s: read %ld bytes, expected %ld bytes\n",
	       context, read_size, (long) size);
      exit (1);
    }

  tfile_offset = offset + size;
  return ptr;
}


/* Convert language code to string format.  */

299
static const char *
300
lang_to_string (lang_t lang)
Richard Stallman committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
{
  switch (lang)
    {
    case langC:		return "C";
    case langPascal:	return "Pascal";
    case langFortran:	return "Fortran";
    case langAssembler:	return "Assembler";
    case langMachine:	return "Machine";
    case langNil:	return "Nil";
    case langAda:	return "Ada";
    case langPl1:	return "Pl1";
    case langCobol:	return "Cobol";
    }

  return "Unknown language";
}


/* Convert storage class to string.  */

321
static const char *
322
sc_to_string (sc_t storage_class)
Richard Stallman committed
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
{
  switch(storage_class)
    {
    case sc_Nil:	 return "Nil";
    case sc_Text:	 return "Text";
    case sc_Data:	 return "Data";
    case sc_Bss:	 return "Bss";
    case sc_Register:	 return "Register";
    case sc_Abs:	 return "Abs";
    case sc_Undefined:	 return "Undefined";
    case sc_CdbLocal:	 return "CdbLocal";
    case sc_Bits:	 return "Bits";
    case sc_CdbSystem:	 return "CdbSystem";
    case sc_RegImage:	 return "RegImage";
    case sc_Info:	 return "Info";
    case sc_UserStruct:	 return "UserStruct";
    case sc_SData:	 return "SData";
    case sc_SBss:	 return "SBss";
    case sc_RData:	 return "RData";
    case sc_Var:	 return "Var";
    case sc_Common:	 return "Common";
    case sc_SCommon:	 return "SCommon";
    case sc_VarRegister: return "VarRegister";
    case sc_Variant:	 return "Variant";
    case sc_SUndefined:	 return "SUndefined";
    case sc_Init:	 return "Init";
    case sc_Max:	 return "Max";
    }

  return "???";
}


/* Convert symbol type to string.  */

358
static const char *
359
st_to_string (st_t symbol_type)
Richard Stallman committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
{
  switch(symbol_type)
    {
    case st_Nil:	return "Nil";
    case st_Global:	return "Global";
    case st_Static:	return "Static";
    case st_Param:	return "Param";
    case st_Local:	return "Local";
    case st_Label:	return "Label";
    case st_Proc:	return "Proc";
    case st_Block:	return "Block";
    case st_End:	return "End";
    case st_Member:	return "Member";
    case st_Typedef:	return "Typedef";
    case st_File:	return "File";
    case st_RegReloc:	return "RegReloc";
    case st_Forward:	return "Forward";
    case st_StaticProc:	return "StaticProc";
    case st_Constant:	return "Constant";
    case st_StaParam:	return "StaticParam";
380 381 382 383 384
#ifdef stStruct
    case st_Struct:	return "Struct";
    case st_Union:	return "Union";
    case st_Enum:	return "Enum";
#endif
Richard Stallman committed
385 386 387 388 389 390 391 392 393 394 395 396 397
    case st_Str:	return "String";
    case st_Number:	return "Number";
    case st_Expr:	return "Expr";
    case st_Type:	return "Type";
    case st_Max:	return "Max";
    }

  return "???";
}


/* Convert debug level to string.  */

398
static const char *
399
glevel_to_string (glevel_t g_level)
Richard Stallman committed
400 401 402 403 404 405 406 407 408 409 410
{
  switch(g_level)
    {
    case GLEVEL_0: return "G0";
    case GLEVEL_1: return "G1";
    case GLEVEL_2: return "G2";
    case GLEVEL_3: return "G3";
    }

  return "??";
}
411

Richard Stallman committed
412 413 414

/* Convert the type information to string format.  */

415
static const char *
416
type_to_string (AUXU *aux_ptr, int index, FDR *fdp)
Richard Stallman committed
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
{
  AUXU u;
  struct qual {
    tq_t type;
    int  low_bound;
    int  high_bound;
    int  stride;
  } qualifiers[7];

  bt_t basic_type;
  int i;
  static char buffer1[1024];
  static char buffer2[1024];
  char *p1 = buffer1;
  char *p2 = buffer2;
  char *used_ptr = aux_used + (aux_ptr - aux_symbols);

  for (i = 0; i < 7; i++)
    {
      qualifiers[i].low_bound = 0;
      qualifiers[i].high_bound = 0;
      qualifiers[i].stride = 0;
    }

  used_ptr[index] = 1;
  u = aux_ptr[index++];
  if (u.isym == -1)
    return "-1 (no type)";

  basic_type = (bt_t) u.ti.bt;
  qualifiers[0].type = (tq_t) u.ti.tq0;
  qualifiers[1].type = (tq_t) u.ti.tq1;
  qualifiers[2].type = (tq_t) u.ti.tq2;
  qualifiers[3].type = (tq_t) u.ti.tq3;
  qualifiers[4].type = (tq_t) u.ti.tq4;
  qualifiers[5].type = (tq_t) u.ti.tq5;
  qualifiers[6].type = tq_Nil;

  /*
   * Go get the basic type.
   */
  switch (basic_type)
    {
    case bt_Nil:		/* undefined */
      strcpy (p1, "nil");
      break;

    case bt_Adr:		/* address - integer same size as pointer */
      strcpy (p1, "address");
      break;

    case bt_Char:		/* character */
      strcpy (p1, "char");
      break;

    case bt_UChar:		/* unsigned character */
      strcpy (p1, "unsigned char");
      break;

    case bt_Short:		/* short */
      strcpy (p1, "short");
      break;

    case bt_UShort:		/* unsigned short */
      strcpy (p1, "unsigned short");
      break;

    case bt_Int:		/* int */
      strcpy (p1, "int");
      break;

    case bt_UInt:		/* unsigned int */
      strcpy (p1, "unsigned int");
      break;

    case bt_Long:		/* long */
      strcpy (p1, "long");
      break;

    case bt_ULong:		/* unsigned long */
      strcpy (p1, "unsigned long");
      break;

    case bt_Float:		/* float (real) */
      strcpy (p1, "float");
      break;

    case bt_Double:		/* Double (real) */
      strcpy (p1, "double");
      break;

      /* Structures add 1-2 aux words:
	 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
	 2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */

    case bt_Struct:		/* Structure (Record) */
513
      emit_aggregate (p1, aux_ptr[index], aux_ptr[index+1], "struct", fdp);
Richard Stallman committed
514 515 516 517 518 519 520 521 522 523 524 525
      used_ptr[index] = 1;
      if (aux_ptr[index].rndx.rfd == ST_RFDESCAPE)
	used_ptr[++index] = 1;

      index++;			/* skip aux words */
      break;

      /* Unions add 1-2 aux words:
	 1st word is [ST_RFDESCAPE, offset] pointer to union def;
	 2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */

    case bt_Union:		/* Union */
526
      emit_aggregate (p1, aux_ptr[index], aux_ptr[index+1], "union", fdp);
Richard Stallman committed
527 528 529 530 531 532 533 534 535 536 537 538
      used_ptr[index] = 1;
      if (aux_ptr[index].rndx.rfd == ST_RFDESCAPE)
	used_ptr[++index] = 1;

      index++;			/* skip aux words */
      break;

      /* Enumerations add 1-2 aux words:
	 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
	 2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */

    case bt_Enum:		/* Enumeration */
539
      emit_aggregate (p1, aux_ptr[index], aux_ptr[index+1], "enum", fdp);
Richard Stallman committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
      used_ptr[index] = 1;
      if (aux_ptr[index].rndx.rfd == ST_RFDESCAPE)
	used_ptr[++index] = 1;

      index++;			/* skip aux words */
      break;

    case bt_Typedef:		/* defined via a typedef, isymRef points */
      strcpy (p1, "typedef");
      break;

    case bt_Range:		/* subrange of int */
      strcpy (p1, "subrange");
      break;

    case bt_Set:		/* pascal sets */
      strcpy (p1, "set");
      break;

    case bt_Complex:		/* fortran complex */
      strcpy (p1, "complex");
      break;

    case bt_DComplex:		/* fortran double complex */
      strcpy (p1, "double complex");
      break;

    case bt_Indirect:		/* forward or unnamed typedef */
568
      strcpy (p1, "forward/unnamed typedef");
Richard Stallman committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
      break;

    case bt_FixedDec:		/* Fixed Decimal */
      strcpy (p1, "fixed decimal");
      break;

    case bt_FloatDec:		/* Float Decimal */
      strcpy (p1, "float decimal");
      break;

    case bt_String:		/* Varying Length Character String */
      strcpy (p1, "string");
      break;

    case bt_Bit:		/* Aligned Bit String */
      strcpy (p1, "bit");
      break;

    case bt_Picture:		/* Picture */
      strcpy (p1, "picture");
      break;

    case bt_Void:		/* Void */
      strcpy (p1, "void");
      break;

    default:
      sprintf (p1, "Unknown basic type %d", (int) basic_type);
      break;
    }

  p1 += strlen (buffer1);

  /*
   * If this is a bitfield, get the bitsize.
   */
  if (u.ti.fBitfield)
    {
      int bitsize;

      used_ptr[index] = 1;
      bitsize = aux_ptr[index++].width;
      sprintf (p1, " : %d", bitsize);
      p1 += strlen (buffer1);
    }


  /*
   * Deal with any qualifiers.
   */
  if (qualifiers[0].type != tq_Nil)
    {
      /*
       * Snarf up any array bounds in the correct order.  Arrays
Charles Hannum committed
623
       * store 5 successive words in the aux. table:
624
       *	word 0	RNDXR to type of the bounds (i.e., int)
Richard Stallman committed
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
       *	word 1	Current file descriptor index
       *	word 2	low bound
       *	word 3	high bound (or -1 if [])
       *	word 4	stride size in bits
       */
      for (i = 0; i < 7; i++)
	{
	  if (qualifiers[i].type == tq_Array)
	    {
	      qualifiers[i].low_bound  = aux_ptr[index+2].dnLow;
	      qualifiers[i].high_bound = aux_ptr[index+3].dnHigh;
	      qualifiers[i].stride     = aux_ptr[index+4].width;
	      used_ptr[index] = 1;
	      used_ptr[index+1] = 1;
	      used_ptr[index+2] = 1;
	      used_ptr[index+3] = 1;
	      used_ptr[index+4] = 1;
	      index += 5;
	    }
	}

      /*
       * Now print out the qualifiers.
       */
      for (i = 0; i < 6; i++)
	{
	  switch (qualifiers[i].type)
	    {
	    case tq_Nil:
	    case tq_Max:
	      break;

	    case tq_Ptr:
	      strcpy (p2, "ptr to ");
	      p2 += sizeof ("ptr to ")-1;
	      break;

	    case tq_Vol:
	      strcpy (p2, "volatile ");
	      p2 += sizeof ("volatile ")-1;
	      break;

	    case tq_Far:
	      strcpy (p2, "far ");
	      p2 += sizeof ("far ")-1;
	      break;

	    case tq_Proc:
	      strcpy (p2, "func. ret. ");
	      p2 += sizeof ("func. ret. ");
	      break;

	    case tq_Array:
	      {
		int first_array = i;
		int j;

682
		/* Print array bounds reversed (i.e., in the order the C
Mike Stump committed
683
		   programmer writes them).  C is such a fun language....  */
Richard Stallman committed
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724

		while (i < 5 && qualifiers[i+1].type == tq_Array)
		  i++;

		for (j = i; j >= first_array; j--)
		  {
		    strcpy (p2, "array [");
		    p2 += sizeof ("array [")-1;
		    if (qualifiers[j].low_bound != 0)
		      sprintf (p2,
			       "%ld:%ld {%ld bits}",
			       (long) qualifiers[j].low_bound,
			       (long) qualifiers[j].high_bound,
			       (long) qualifiers[j].stride);

		    else if (qualifiers[j].high_bound != -1)
		      sprintf (p2,
			       "%ld {%ld bits}",
			       (long) (qualifiers[j].high_bound + 1),
			       (long) (qualifiers[j].stride));

		    else
		      sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));

		    p2 += strlen (p2);
		    strcpy (p2, "] of ");
		    p2 += sizeof ("] of ")-1;
		  }
	      }
	      break;
	    }
	}
    }

  strcpy (p2, buffer1);
  return buffer2;
}


/* Print out the global file header for object files.  */

725
static void
726
print_global_hdr (struct filehdr *ptr)
Richard Stallman committed
727
{
728
  char *time = ctime ((time_t *)&ptr->f_timdat);
Richard Stallman committed
729 730 731 732 733 734 735 736 737
  ushort flags = ptr->f_flags;

  printf("Global file header:\n");
  printf("    %-*s 0x%x\n",    24, "magic number",	     (ushort) ptr->f_magic);
  printf("    %-*s %d\n",      24, "# sections",	     (int)    ptr->f_nscns);
  printf("    %-*s %ld, %s",   24, "timestamp",		     (long)   ptr->f_timdat, time);
  printf("    %-*s %ld\n",     24, "symbolic header offset", (long)   ptr->f_symptr);
  printf("    %-*s %ld\n",     24, "symbolic header size",   (long)   ptr->f_nsyms);
  printf("    %-*s %ld\n",     24, "optional header",	     (long)   ptr->f_opthdr);
738
  printf("    %-*s 0x%x",     24, "flags",		     (ushort) flags);
Richard Stallman committed
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778

  if ((flags & F_RELFLG) != 0)
    printf (", F_RELFLG");

  if ((flags & F_EXEC) != 0)
    printf (", F_EXEC");

  if ((flags & F_LNNO) != 0)
    printf (", F_LNNO");

  if ((flags & F_LSYMS) != 0)
    printf (", F_LSYMS");

  if ((flags & F_MINMAL) != 0)
    printf (", F_MINMAL");

  if ((flags & F_UPDATE) != 0)
    printf (", F_UPDATE");

  if ((flags & F_SWABD) != 0)
    printf (", F_SWABD");

  if ((flags & F_AR16WR) != 0)
    printf (", F_AR16WR");

  if ((flags & F_AR32WR) != 0)
    printf (", F_AR32WR");

  if ((flags & F_AR32W) != 0)
    printf (", F_AR32W");

  if ((flags & F_PATCH) != 0)
    printf (", F_PATCH/F_NODF");

  printf ("\n\n");
}


/* Print out the symbolic header.  */

779
static void
780
print_sym_hdr (HDRR *sym_ptr)
Richard Stallman committed
781 782 783 784 785 786 787 788 789 790 791
{
  int width = 20;

  printf("Symbolic header, magic number = 0x%04x, vstamp = %d.%d:\n\n",
	 sym_ptr->magic & 0xffff,
	 (sym_ptr->vstamp & 0xffff) >> 8,
	 sym_ptr->vstamp & 0xff);

  printf("    %-*s %11s %11s %11s\n", width, "Info", "Offset", "Number", "Bytes");
  printf("    %-*s %11s %11s %11s\n", width, "====", "======", "======", "=====\n");

792
  printf("    %-*s %11ld %11ld %11ld [%d]\n", width, "Line numbers",
Mike Stump committed
793 794 795 796
	 (long) sym_ptr->cbLineOffset,
	 (long) sym_ptr->cbLine,
	 (long) sym_ptr->cbLine,
	 (int) sym_ptr->ilineMax);
Richard Stallman committed
797

798
  printf("    %-*s %11ld %11ld %11ld\n", width, "Dense numbers",
Mike Stump committed
799 800 801
	 (long) sym_ptr->cbDnOffset,
	 (long) sym_ptr->idnMax,
	 (long) (sym_ptr->idnMax * sizeof (DNR)));
Richard Stallman committed
802

803
  printf("    %-*s %11ld %11ld %11ld\n", width, "Procedures Tables",
Mike Stump committed
804 805 806
	 (long) sym_ptr->cbPdOffset,
	 (long) sym_ptr->ipdMax,
	 (long) (sym_ptr->ipdMax * sizeof (PDR)));
Richard Stallman committed
807

808
  printf("    %-*s %11ld %11ld %11ld\n", width, "Local Symbols",
Mike Stump committed
809 810 811
	 (long) sym_ptr->cbSymOffset,
	 (long) sym_ptr->isymMax,
	 (long) (sym_ptr->isymMax * sizeof (SYMR)));
Richard Stallman committed
812

813
  printf("    %-*s %11ld %11ld %11ld\n", width, "Optimization Symbols",
Mike Stump committed
814 815 816
	 (long) sym_ptr->cbOptOffset,
	 (long) sym_ptr->ioptMax,
	 (long) (sym_ptr->ioptMax * sizeof (OPTR)));
Richard Stallman committed
817

818
  printf("    %-*s %11ld %11ld %11ld\n", width, "Auxiliary Symbols",
Mike Stump committed
819 820 821
	 (long) sym_ptr->cbAuxOffset,
	 (long) sym_ptr->iauxMax,
	 (long) (sym_ptr->iauxMax * sizeof (AUXU)));
Richard Stallman committed
822

823
  printf("    %-*s %11ld %11ld %11ld\n", width, "Local Strings",
Mike Stump committed
824 825 826
	 (long) sym_ptr->cbSsOffset,
	 (long) sym_ptr->issMax,
	 (long) sym_ptr->issMax);
Richard Stallman committed
827

828
  printf("    %-*s %11ld %11ld %11ld\n", width, "External Strings",
Mike Stump committed
829 830 831
	 (long) sym_ptr->cbSsExtOffset,
	 (long) sym_ptr->issExtMax,
	 (long) sym_ptr->issExtMax);
Richard Stallman committed
832

833
  printf("    %-*s %11ld %11ld %11ld\n", width, "File Tables",
Mike Stump committed
834 835 836
	 (long) sym_ptr->cbFdOffset,
	 (long) sym_ptr->ifdMax,
	 (long) (sym_ptr->ifdMax * sizeof (FDR)));
Richard Stallman committed
837

838
  printf("    %-*s %11ld %11ld %11ld\n", width, "Relative Files",
Mike Stump committed
839 840 841
	 (long) sym_ptr->cbRfdOffset,
	 (long) sym_ptr->crfd,
	 (long) (sym_ptr->crfd * sizeof (ulong)));
Richard Stallman committed
842

843
  printf("    %-*s %11ld %11ld %11ld\n", width, "External Symbols",
Mike Stump committed
844 845 846
	 (long) sym_ptr->cbExtOffset,
	 (long) sym_ptr->iextMax,
	 (long) (sym_ptr->iextMax * sizeof (EXTR)));
Richard Stallman committed
847 848 849 850 851
}


/* Print out a symbol.  */

852
static void
853 854
print_symbol (SYMR *sym_ptr, int number, const char *strbase, AUXU *aux_base,
	      int ifd, FDR *fdp)
Richard Stallman committed
855 856 857 858 859 860 861 862 863
{
  sc_t storage_class = (sc_t) sym_ptr->sc;
  st_t symbol_type   = (st_t) sym_ptr->st;
  ulong index	     = sym_ptr->index;
  char *used_ptr     = aux_used + (aux_base - aux_symbols);
  scope_t *scope_ptr;

  printf ("\n    Symbol# %d: \"%s\"\n", number, sym_ptr->iss + strbase);

Mike Stump committed
864
  if (aux_base != (AUXU *) 0 && index != indexNil)
Richard Stallman committed
865 866 867 868 869 870 871 872 873 874 875
    switch (symbol_type)
      {
      case st_Nil:
      case st_Label:
	break;

      case st_File:
      case st_Block:
	printf ("      End+1 symbol: %ld\n", index);
	if (want_scope)
	  {
Mike Stump committed
876
	    if (free_scope == (scope_t *) 0)
877
	      scope_ptr = (scope_t *) xmalloc (sizeof (scope_t));
Richard Stallman committed
878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
	    else
	      {
		scope_ptr = free_scope;
		free_scope = scope_ptr->prev;
	      }
	    scope_ptr->open_sym = number;
	    scope_ptr->st = symbol_type;
	    scope_ptr->sc = storage_class;
	    scope_ptr->prev = cur_scope;
	    cur_scope = scope_ptr;
	  }
	break;

      case st_End:
	if (storage_class == sc_Text || storage_class == sc_Info)
	  printf ("      First symbol: %ld\n", index);
	else
	  {
	    used_ptr[index] = 1;
Kaveh R. Ghazi committed
897
	    printf ("      First symbol: %ld\n", (long) aux_base[index].isym);
Richard Stallman committed
898 899 900 901
	  }

	if (want_scope)
	  {
Mike Stump committed
902
	    if (cur_scope == (scope_t *) 0)
Richard Stallman committed
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
	      printf ("      Can't pop end scope\n");
	    else
	      {
		scope_ptr = cur_scope;
		cur_scope = scope_ptr->prev;
		scope_ptr->prev = free_scope;
		free_scope = scope_ptr;
	      }
	  }
	break;

      case st_Proc:
      case st_StaticProc:
	if (MIPS_IS_STAB(sym_ptr))
	  ;
	else if (ifd == -1)		/* local symbol */
	  {
	    used_ptr[index] = used_ptr[index+1] = 1;
	    printf ("      End+1 symbol: %-7ld   Type:  %s\n",
Kaveh R. Ghazi committed
922
		    (long) aux_base[index].isym,
923
		    type_to_string (aux_base, index+1, fdp));
Richard Stallman committed
924 925
	  }
	else			/* global symbol */
926
	  printf ("      Local symbol: %ld\n", index);
Richard Stallman committed
927 928 929

	if (want_scope)
	  {
Mike Stump committed
930
	    if (free_scope == (scope_t *) 0)
931
	      scope_ptr = (scope_t *) xmalloc (sizeof (scope_t));
Richard Stallman committed
932 933 934 935 936 937 938 939 940 941 942 943 944
	    else
	      {
		scope_ptr = free_scope;
		free_scope = scope_ptr->prev;
	      }
	    scope_ptr->open_sym = number;
	    scope_ptr->st = symbol_type;
	    scope_ptr->sc = storage_class;
	    scope_ptr->prev = cur_scope;
	    cur_scope = scope_ptr;
	  }
	break;

945 946 947 948 949 950 951 952
#ifdef stStruct
      case st_Struct:
      case st_Union:
      case st_Enum:
	printf ("      End+1 symbol: %lu\n", index);
	break;
#endif

Richard Stallman committed
953 954 955 956 957
      default:
	if (!MIPS_IS_STAB (sym_ptr))
	  {
	    used_ptr[index] = 1;
	    printf ("      Type: %s\n",
958
		    type_to_string (aux_base, index, fdp));
Richard Stallman committed
959 960 961 962 963 964 965
	  }
	break;
      }

  if (want_scope)
    {
      printf ("      Scopes:  ");
Mike Stump committed
966
      if (cur_scope == (scope_t *) 0)
Richard Stallman committed
967 968 969 970
	printf (" none\n");
      else
	{
	  for (scope_ptr = cur_scope;
Mike Stump committed
971
	       scope_ptr != (scope_t *) 0;
Richard Stallman committed
972 973
	       scope_ptr = scope_ptr->prev)
	    {
974
	      const char *sclass;
Richard Stallman committed
975
	      if (scope_ptr->st == st_Proc || scope_ptr->st == st_StaticProc)
976
		sclass = "func.";
Richard Stallman committed
977
	      else if (scope_ptr->st == st_File)
978
		sclass = "file";
Richard Stallman committed
979
	      else if (scope_ptr->st == st_Block && scope_ptr->sc == sc_Text)
980
		sclass = "block";
Richard Stallman committed
981
	      else if (scope_ptr->st == st_Block && scope_ptr->sc == sc_Info)
982
		sclass = "type";
Richard Stallman committed
983
	      else
984
		sclass = "???";
Richard Stallman committed
985

986
	      printf (" %ld [%s]", scope_ptr->open_sym, sclass);
Richard Stallman committed
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
	    }
	  printf ("\n");
	}
    }

  printf ("      Value: %-13ld    ",
	  (long)sym_ptr->value);
  if (ifd == -1)
    printf ("String index: %ld\n", (long)sym_ptr->iss);
  else
    printf ("String index: %-11ld Ifd: %d\n",
	    (long)sym_ptr->iss, ifd);

  printf ("      Symbol type: %-11sStorage class: %-11s",
	  st_to_string (symbol_type), sc_to_string (storage_class));

  if (MIPS_IS_STAB(sym_ptr))
    {
1005
      int i = ARRAY_SIZE (stab_names);
Kaveh R. Ghazi committed
1006
      const char *stab_name = "stab";
Richard Stallman committed
1007
      short code = MIPS_UNMARK_STAB(sym_ptr->index);
1008

Richard Stallman committed
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
      while (--i >= 0)
	if (stab_names[i].code == code)
	  {
	    stab_name = stab_names[i].string;
	    break;
	  }
      printf ("Index: 0x%lx (%s)\n", (long)sym_ptr->index, stab_name);
    }
  else if (sym_ptr->st == stLabel && sym_ptr->index != indexNil)
    printf ("Index: %ld (line#)\n", (long)sym_ptr->index);
  else
    printf ("Index: %ld\n", (long)sym_ptr->index);

}


/* Print out a word from the aux. table in various formats.  */

1027
static void
1028
print_aux (AUXU u, int auxi, int used)
Richard Stallman committed
1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049
{
  printf ("\t%s#%-5d %11ld, [%4ld/%7ld], [%2d %1d:%1d %1x:%1x:%1x:%1x:%1x:%1x]\n",
	  (used) ? "  " : "* ",
	  auxi,
	  (long) u.isym,
	  (long) u.rndx.rfd,
	  (long) u.rndx.index,
	  u.ti.bt,
	  u.ti.fBitfield,
	  u.ti.continued,
	  u.ti.tq0,
	  u.ti.tq1,
	  u.ti.tq2,
	  u.ti.tq3,
	  u.ti.tq4,
	  u.ti.tq5);
}


/* Write aggregate information to a string.  */

1050
static void
1051
emit_aggregate (char *string, AUXU u, AUXU u2, const char *which, FDR *fdp)
Richard Stallman committed
1052
{
1053 1054 1055
  unsigned int ifd = u.rndx.rfd;
  unsigned int index = u.rndx.index;
  const char *name;
1056

Richard Stallman committed
1057 1058
  if (ifd == ST_RFDESCAPE)
    ifd = u2.isym;
1059

1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
  /* An ifd of -1 is an opaque type.  An escaped index of 0 is a
     struct return type of a procedure compiled without -g.  */
  if (ifd == 0xffffffff
      || (u.rndx.rfd == ST_RFDESCAPE && index == 0))
    name = "<undefined>";
  else if (index == indexNil)
    name = "<no name>";
  else
    {
      if (fdp == 0 || sym_hdr.crfd == 0)
	fdp = &file_desc[ifd];
      else
	fdp = &file_desc[rfile_desc[fdp->rfdBase + ifd]];
      name = &l_strings[fdp->issBase + l_symbols[index + fdp->isymBase].iss];
    }
1075

Richard Stallman committed
1076
  sprintf (string,
1077 1078
	   "%s %s { ifd = %u, index = %u }",
	   which, name, ifd, index);
Richard Stallman committed
1079 1080 1081 1082 1083 1084
}


/* Print out information about a file descriptor, and the symbols,
   procedures, and line numbers within it.  */

1085
static void
1086
print_file_desc (FDR *fdp, int number)
Richard Stallman committed
1087 1088 1089 1090 1091 1092
{
  char *str_base;
  AUXU *aux_base;
  int symi, pdi;
  int width = 20;
  char *used_base;
1093 1094

  str_base = l_strings + fdp->issBase;
Richard Stallman committed
1095 1096 1097
  aux_base = aux_symbols + fdp->iauxBase;
  used_base = aux_used + (aux_base - aux_symbols);

1098 1099 1100
  printf ("\nFile #%d, \"%s\"\n\n",
	  number,
	  fdp->rss != issNil ? str_base + fdp->rss : "<unknown>");
1101

1102
  printf ("    Name index  = %-10ld Readin      = %s\n",
Richard Stallman committed
1103 1104 1105 1106 1107 1108 1109
	  (long) fdp->rss, (fdp->fReadin) ? "Yes" : "No");

  printf ("    Merge       = %-10s Endian      = %s\n",
	  (fdp->fMerge)  ? "Yes" : "No",
	  (fdp->fBigendian) ? "BIG" : "LITTLE");

  printf ("    Debug level = %-10s Language    = %s\n",
1110
	  glevel_to_string ((glevel_t) fdp->glevel),
Richard Stallman committed
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	  lang_to_string((lang_t) fdp->lang));

  printf ("    Adr         = 0x%08lx\n\n", (long) fdp->adr);

  printf("    %-*s %11s %11s %11s %11s\n", width, "Info", "Start", "Number", "Size", "Offset");
  printf("    %-*s %11s %11s %11s %11s\n", width, "====", "=====", "======", "====", "======");

  printf("    %-*s %11lu %11lu %11lu %11lu\n",
	 width, "Local strings",
	 (ulong) fdp->issBase,
	 (ulong) fdp->cbSs,
	 (ulong) fdp->cbSs,
	 (ulong) (fdp->issBase + sym_hdr.cbSsOffset));

1125
  printf("    %-*s %11lu %11lu %11lu %11lu\n",
Richard Stallman committed
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
	 width, "Local symbols",
	 (ulong) fdp->isymBase,
	 (ulong) fdp->csym,
	 (ulong) (fdp->csym * sizeof (SYMR)),
	 (ulong) (fdp->isymBase * sizeof (SYMR) + sym_hdr.cbSymOffset));

  printf("    %-*s %11lu %11lu %11lu %11lu\n",
	 width, "Line numbers",
	 (ulong) fdp->cbLineOffset,
	 (ulong) fdp->cline,
1136
	 (ulong) fdp->cbLine,
Richard Stallman committed
1137 1138 1139 1140 1141 1142 1143 1144 1145
	 (ulong) (fdp->cbLineOffset + sym_hdr.cbLineOffset));

  printf("    %-*s %11lu %11lu %11lu %11lu\n",
	 width, "Optimization symbols",
	 (ulong) fdp->ioptBase,
	 (ulong) fdp->copt,
	 (ulong) (fdp->copt * sizeof (OPTR)),
	 (ulong) (fdp->ioptBase * sizeof (OPTR) + sym_hdr.cbOptOffset));

1146
  printf("    %-*s %11lu %11lu %11lu %11lu\n",
Richard Stallman committed
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
	 width, "Procedures",
	 (ulong) fdp->ipdFirst,
	 (ulong) fdp->cpd,
	 (ulong) (fdp->cpd * sizeof (PDR)),
	 (ulong) (fdp->ipdFirst * sizeof (PDR) + sym_hdr.cbPdOffset));

  printf("    %-*s %11lu %11lu %11lu %11lu\n",
	 width, "Auxiliary symbols",
	 (ulong) fdp->iauxBase,
	 (ulong) fdp->caux,
	 (ulong) (fdp->caux * sizeof (AUXU)),
	 (ulong) (fdp->iauxBase * sizeof(AUXU) + sym_hdr.cbAuxOffset));

  printf("    %-*s %11lu %11lu %11lu %11lu\n",
	 width, "Relative Files",
	 (ulong) fdp->rfdBase,
	 (ulong) fdp->crfd,
	 (ulong) (fdp->crfd * sizeof (ulong)),
	 (ulong) (fdp->rfdBase * sizeof(ulong) + sym_hdr.cbRfdOffset));


Mike Stump committed
1168
  if (want_scope && cur_scope != (scope_t *) 0)
Richard Stallman committed
1169 1170
    printf ("\n    Warning scope does not start at 0!\n");

1171
  /*
Richard Stallman committed
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182
   * print the info about the symbol table.
   */
  printf ("\n    There are %lu local symbols, starting at %lu\n",
	  (ulong) fdp->csym,
	  (ulong) (fdp->isymBase + sym_hdr.cbSymOffset));

  for(symi = fdp->isymBase; symi < (fdp->csym + fdp->isymBase); symi++)
    print_symbol (&l_symbols[symi],
		  symi - fdp->isymBase,
		  str_base,
		  aux_base,
1183 1184
		  -1,
		  fdp);
Richard Stallman committed
1185

Mike Stump committed
1186
  if (want_scope && cur_scope != (scope_t *) 0)
Richard Stallman committed
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
    printf ("\n    Warning scope does not end at 0!\n");

  /*
   * print the aux. table if desired.
   */

  if (want_aux && fdp->caux != 0)
    {
      int auxi;

      printf ("\n    There are %lu auxiliary table entries, starting at %lu.\n\n",
	      (ulong) fdp->caux,
	      (ulong) (fdp->iauxBase + sym_hdr.cbAuxOffset));

      for (auxi = fdp->iauxBase; auxi < (fdp->caux + fdp->iauxBase); auxi++)
	print_aux (aux_base[auxi], auxi, used_base[auxi]);
    }

  /*
   * print the relative file descriptors.
   */
  if (want_rfd && fdp->crfd != 0)
    {
      ulong *rfd_ptr, i;

      printf ("\n    There are %lu relative file descriptors, starting at %lu.\n",
	      (ulong) fdp->crfd,
	      (ulong) fdp->rfdBase);

1216
      rfd_ptr = rfile_desc + fdp->rfdBase;
Kaveh R. Ghazi committed
1217
      for (i = 0; i < (ulong) fdp->crfd; i++)
Richard Stallman committed
1218 1219 1220 1221 1222 1223
	{
	  printf ("\t#%-5ld %11ld, 0x%08lx\n", i, *rfd_ptr, *rfd_ptr);
	  rfd_ptr++;
	}
    }

1224
  /*
Richard Stallman committed
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
   * do the procedure descriptors.
   */
  printf ("\n    There are %lu procedure descriptor entries, ", (ulong) fdp->cpd);
  printf ("starting at %lu.\n", (ulong) fdp->ipdFirst);

  for (pdi = fdp->ipdFirst; pdi < (fdp->cpd + fdp->ipdFirst); pdi++)
    {
      PDR *proc_ptr = &proc_desc[pdi];
      printf ("\n\tProcedure descriptor %d:\n", (pdi - fdp->ipdFirst));

1235 1236 1237 1238
      if (l_symbols != 0)
	printf ("\t    Name index   = %-11ld Name          = \"%s\"\n",
		(long) l_symbols[proc_ptr->isym + fdp->isymBase].iss,
		l_symbols[proc_ptr->isym + fdp->isymBase].iss + str_base);
Richard Stallman committed
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270

      printf ("\t    .mask 0x%08lx,%-9ld .fmask 0x%08lx,%ld\n",
	      (long) proc_ptr->regmask,
	      (long) proc_ptr->regoffset,
	      (long) proc_ptr->fregmask,
	      (long) proc_ptr->fregoffset);

      printf ("\t    .frame $%d,%ld,$%d\n",
	      (int)  proc_ptr->framereg,
	      (long) proc_ptr->frameoffset,
	      (int)  proc_ptr->pcreg);

      printf ("\t    Opt. start   = %-11ld Symbols start = %ld\n",
	      (long) proc_ptr->iopt,
	      (long) proc_ptr->isym);

      printf ("\t    First line # = %-11ld Last line #   = %ld\n",
	      (long) proc_ptr->lnLow,
	      (long) proc_ptr->lnHigh);

      printf ("\t    Line Offset  = %-11ld Address       = 0x%08lx\n",
	      (long) proc_ptr->cbLineOffset,
	      (long) proc_ptr->adr);

      /*
       * print the line number entries.
       */

      if (want_line && fdp->cline != 0)
	{
	  int delta, count;
	  long cur_line = proc_ptr->lnLow;
1271 1272
	  uchar *line_ptr = (((uchar *)lines) + proc_ptr->cbLineOffset
			     + fdp->cbLineOffset);
Richard Stallman committed
1273 1274 1275
	  uchar *line_end;

	  if (pdi == fdp->cpd + fdp->ipdFirst - 1)	/* last procedure */
1276
	    line_end = ((uchar *)lines) + fdp->cbLine + fdp->cbLineOffset;
Mike Stump committed
1277
	  else						/* not last proc.  */
1278 1279
	    line_end = (((uchar *)lines) + proc_desc[pdi+1].cbLineOffset
			+ fdp->cbLineOffset);
Richard Stallman committed
1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310

	  printf ("\n\tThere are %lu bytes holding line numbers, starting at %lu.\n",
		  (ulong) (line_end - line_ptr),
		  (ulong) (fdp->ilineBase + sym_hdr.cbLineOffset));

	  while (line_ptr < line_end)
	    {						/* sign extend nibble */
	      delta = ((*line_ptr >> 4) ^ 0x8) - 0x8;
	      count = (*line_ptr & 0xf) + 1;
	      if (delta != -8)
		line_ptr++;
	      else
		{
		  delta = (((line_ptr[1]) & 0xff) << 8) + ((line_ptr[2]) & 0xff);
		  delta = (delta ^ 0x8000) - 0x8000;
		  line_ptr += 3;
		}

	      cur_line += delta;
	      printf ("\t    Line %11ld,   delta %5d,   count %2d\n",
		      cur_line,
		      delta,
		      count);
	    }
	}
    }
}


/* Read in the portions of the .T file that we will print out.  */

1311
static void
1312
read_tfile (void)
Richard Stallman committed
1313 1314 1315 1316
{
  short magic;
  off_t sym_hdr_offset = 0;

1317
  read_seek (&magic, sizeof (magic), 0, "Magic number");
Michael Meissner committed
1318
  if (!tfile)
Richard Stallman committed
1319
    {
Michael Meissner committed
1320 1321
      /* Print out the global header, since this is not a T-file.  */

1322
      read_seek (&global_hdr, sizeof (global_hdr), 0, "Global file header");
Richard Stallman committed
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334

      print_global_hdr (&global_hdr);

      if (global_hdr.f_symptr == 0)
	{
	  printf ("No symbolic header, Goodbye!\n");
	  exit (1);
	}

      sym_hdr_offset = global_hdr.f_symptr;
    }

1335
  read_seek (&sym_hdr, sizeof (sym_hdr), sym_hdr_offset, "Symbolic header");
Richard Stallman committed
1336 1337 1338

  print_sym_hdr (&sym_hdr);

1339 1340
  lines = (LINER *) read_seek (NULL, sym_hdr.cbLine, sym_hdr.cbLineOffset,
			       "Line numbers");
Richard Stallman committed
1341

1342 1343
  dense_nums = (DNR *) read_seek (NULL, sym_hdr.idnMax * sizeof (DNR),
				  sym_hdr.cbDnOffset, "Dense numbers");
Richard Stallman committed
1344

1345 1346
  proc_desc = (PDR *) read_seek (NULL, sym_hdr.ipdMax * sizeof (PDR),
				 sym_hdr.cbPdOffset, "Procedure tables");
Richard Stallman committed
1347

1348 1349
  l_symbols = (SYMR *) read_seek (NULL, sym_hdr.isymMax * sizeof (SYMR),
				  sym_hdr.cbSymOffset, "Local symbols");
Richard Stallman committed
1350

1351 1352 1353
  opt_symbols = (OPTR *) read_seek (NULL, sym_hdr.ioptMax * sizeof (OPTR),
				    sym_hdr.cbOptOffset,
				    "Optimization symbols");
Richard Stallman committed
1354

1355 1356
  aux_symbols = (AUXU *) read_seek (NULL, sym_hdr.iauxMax * sizeof (AUXU),
				    sym_hdr.cbAuxOffset, "Auxiliary symbols");
Richard Stallman committed
1357 1358

  if (sym_hdr.iauxMax > 0)
1359
    aux_used = (char *) xcalloc (sym_hdr.iauxMax, 1);
Richard Stallman committed
1360

1361 1362
  l_strings = (char *) read_seek (NULL, sym_hdr.issMax,
				  sym_hdr.cbSsOffset, "Local string table");
1363

1364 1365 1366
  e_strings = (char *) read_seek (NULL, sym_hdr.issExtMax,
				  sym_hdr.cbSsExtOffset,
				  "External string table");
1367

1368 1369
  file_desc = (FDR *) read_seek (NULL, sym_hdr.ifdMax * sizeof (FDR),
				 sym_hdr.cbFdOffset, "File tables");
1370

1371 1372 1373
  rfile_desc = (ulong *) read_seek (NULL, sym_hdr.crfd * sizeof (ulong),
				    sym_hdr.cbRfdOffset,
				    "Relative file tables");
1374

1375 1376
  e_symbols = (EXTR *) read_seek (NULL, sym_hdr.iextMax * sizeof (EXTR),
				  sym_hdr.cbExtOffset, "External symbols");
Richard Stallman committed
1377 1378 1379 1380
}



1381
extern int main (int, char **);
1382

Richard Stallman committed
1383
int
1384
main (int argc, char **argv)
Richard Stallman committed
1385 1386 1387 1388 1389 1390
{
  int i, opt;

  /*
   * Process arguments
   */
1391
  while ((opt = getopt_long (argc, argv, "alrsvt", options, NULL)) != -1)
Richard Stallman committed
1392 1393 1394 1395 1396 1397 1398
    switch (opt)
      {
      default:	errors++;	break;
      case 'a': want_aux++;	break;	/* print aux table */
      case 'l': want_line++;	break;	/* print line numbers */
      case 'r': want_rfd++;	break;	/* print relative fd's */
      case 's':	want_scope++;	break;	/* print scope info */
1399 1400 1401 1402
      case 'v': verbose++;	break;  /* print version # */
      case 'V': version++;	break;  /* print version # */
      case 't': tfile++;	break;	/* this is a tfile (without header),
					   and not a .o */
Richard Stallman committed
1403 1404
      }

1405 1406
  if (version)
    {
1407
      printf ("mips-tdump %s%s\n", pkgversion_string, version_string);
1408
      fputs ("Copyright (C) 2011 Free Software Foundation, Inc.\n", stdout);
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
      fputs ("This is free software; see the source for copying conditions.  There is NO\n\
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n",
             stdout);
      exit (0);
    }

  if (optind != argc - 1)
    errors++;

  if (verbose || errors)
1419
    fprintf (stderr, "mips-tdump (GCC) %s\n", version_string);
1420 1421

  if (errors)
Richard Stallman committed
1422 1423
    {
      fprintf (stderr, "Calling Sequence:\n");
1424
      fprintf (stderr, "\t%s [-alrst] <object-or-T-file>\n", argv[0]);
Richard Stallman committed
1425 1426 1427 1428 1429 1430
      fprintf (stderr, "\n");
      fprintf (stderr, "switches:\n");
      fprintf (stderr, "\t-a Print out auxiliary table.\n");
      fprintf (stderr, "\t-l Print out line numbers.\n");
      fprintf (stderr, "\t-r Print out relative file descriptors.\n");
      fprintf (stderr, "\t-s Print out the current scopes for an item.\n");
Michael Meissner committed
1431
      fprintf (stderr, "\t-t Assume there is no global header (ie, a T-file).\n");
1432
      fprintf (stderr, "\t-v Print program version.\n");
Richard Stallman committed
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464
      return 1;
    }

  /*
   * Open and process the input file.
   */
  tfile_fd = open (argv[optind], O_RDONLY);
  if (tfile_fd < 0)
    {
      perror (argv[optind]);
      return 1;
    }

  read_tfile ();

  /*
   * Print any global aux words if any.
   */
  if (want_aux)
    {
      long last_aux_in_use;

      if (sym_hdr.ifdMax != 0 && file_desc[0].iauxBase != 0)
	{
	  printf ("\nGlobal auxiliary entries before first file:\n");
	  for (i = 0; i < file_desc[0].iauxBase; i++)
	    print_aux (aux_symbols[i], 0, aux_used[i]);
	}

      if (sym_hdr.ifdMax == 0)
	last_aux_in_use = 0;
      else
1465 1466 1467
	last_aux_in_use
	  = (file_desc[sym_hdr.ifdMax-1].iauxBase
	     + file_desc[sym_hdr.ifdMax-1].caux - 1);
Richard Stallman committed
1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482

      if (last_aux_in_use < sym_hdr.iauxMax-1)
	{
	  printf ("\nGlobal auxiliary entries after last file:\n");
	  for (i = last_aux_in_use; i < sym_hdr.iauxMax; i++)
	    print_aux (aux_symbols[i], i - last_aux_in_use, aux_used[i]);
	}
    }

  /*
   * Print the information for each file.
   */
  for (i = 0; i < sym_hdr.ifdMax; i++)
    print_file_desc (&file_desc[i], i);

1483
  /*
Richard Stallman committed
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
   * Print the external symbols.
   */
  want_scope = 0;		/* scope info is meaning for extern symbols */
  printf ("\nThere are %lu external symbols, starting at %lu\n",
	  (ulong) sym_hdr.iextMax,
	  (ulong) sym_hdr.cbExtOffset);

  for(i = 0; i < sym_hdr.iextMax; i++)
    print_symbol (&e_symbols[i].asym, i, e_strings,
		  aux_symbols + file_desc[e_symbols[i].ifd].iauxBase,
1494 1495
		  e_symbols[i].ifd,
		  &file_desc[e_symbols[i].ifd]);
Richard Stallman committed
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518

  /*
   * Print unused aux symbols now.
   */

  if (want_aux)
    {
      int first_time = 1;

      for (i = 0; i < sym_hdr.iauxMax; i++)
	{
	  if (! aux_used[i])
	    {
	      if (first_time)
		{
		  printf ("\nThe following auxiliary table entries were unused:\n\n");
		  first_time = 0;
		}

	      printf ("    #%-5d %11ld  0x%08lx  %s\n",
		      i,
		      (long) aux_symbols[i].isym,
		      (long) aux_symbols[i].isym,
1519
		      type_to_string (aux_symbols, i, (FDR *) 0));
Richard Stallman committed
1520 1521 1522 1523 1524 1525
	    }
	}
    }

  return 0;
}