1 /* Update the symbol table (the .T file) in a MIPS object to
2 contain debugging information specified by the GNU compiler
3 in the form of comments (the mips assembler does not support
4 assembly access to debug information).
5 Copyright (C) 1991, 1993, 1994, 1995, 1997, 1998, 1999, 2000, 2001,
6 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
7 Free Software Foundation, Inc.
8 Contributed by Michael Meissner (meissner@cygnus.com).
10 This file is part of GCC.
12 GCC is free software; you can redistribute it and/or modify it under
13 the terms of the GNU General Public License as published by the Free
14 Software Foundation; either version 3, or (at your option) any later
17 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
18 WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 You should have received a copy of the GNU General Public License
23 along with GCC; see the file COPYING3. If not see
24 <http://www.gnu.org/licenses/>. */
27 /* Here is a brief description of the MIPS ECOFF symbol table. The
28 MIPS symbol table has the following pieces:
34 +-- Dense number table
42 +-- Relative file descriptors
54 The symbolic header points to each of the other tables, and also
55 contains the number of entries. It also contains a magic number
56 and MIPS compiler version number, such as 2.0.
58 The auxiliary table is a series of 32 bit integers, that are
59 referenced as needed from the local symbol table. Unlike standard
60 COFF, the aux. information does not follow the symbol that uses
61 it, but rather is a separate table. In theory, this would allow
62 the MIPS compilers to collapse duplicate aux. entries, but I've not
63 noticed this happening with the 1.31 compiler suite. The different
64 types of aux. entries are:
66 1) dnLow: Low bound on array dimension.
68 2) dnHigh: High bound on array dimension.
70 3) isym: Index to the local symbol which is the start of the
71 function for the end of function first aux. entry.
73 4) width: Width of structures and bitfields.
75 5) count: Count of ranges for variant part.
77 6) rndx: A relative index into the symbol table. The relative
78 index field has two parts: rfd which is a pointer into the
79 relative file index table or ST_RFDESCAPE which says the next
80 aux. entry is the file number, and index: which is the pointer
81 into the local symbol within a given file table. This is for
82 things like references to types defined in another file.
84 7) Type information: This is like the COFF type bits, except it
85 is 32 bits instead of 16; they still have room to add new
86 basic types; and they can handle more than 6 levels of array,
87 pointer, function, etc. Each type information field contains
88 the following structure members:
90 a) fBitfield: a bit that says this is a bitfield, and the
91 size in bits follows as the next aux. entry.
93 b) continued: a bit that says the next aux. entry is a
94 continuation of the current type information (in case
95 there are more than 6 levels of array/ptr/function).
97 c) bt: an integer containing the base type before adding
98 array, pointer, function, etc. qualifiers. The
99 current base types that I have documentation for are:
102 btAdr -- address - integer same size as ptr
104 btUChar -- unsigned character
106 btUShort -- unsigned short
108 btUInt -- unsigned int
110 btULong -- unsigned long
111 btFloat -- float (real)
112 btDouble -- Double (real)
113 btStruct -- Structure (Record)
114 btUnion -- Union (variant)
116 btTypedef -- defined via a typedef isymRef
117 btRange -- subrange of int
119 btComplex -- fortran complex
120 btDComplex -- fortran double complex
121 btIndirect -- forward or unnamed typedef
122 btFixedDec -- Fixed Decimal
123 btFloatDec -- Float Decimal
124 btString -- Varying Length Character String
125 btBit -- Aligned Bit String
127 btVoid -- Void (MIPS cc revision >= 2.00)
129 d) tq0 - tq5: type qualifier fields as needed. The
130 current type qualifier fields I have documentation for
133 tqNil -- no more qualifiers
137 tqFar -- 8086 far pointers
141 The dense number table is used in the front ends, and disappears by
142 the time the .o is created.
144 With the 1.31 compiler suite, the optimization symbols don't seem
145 to be used as far as I can tell.
147 The linker is the first entity that creates the relative file
148 descriptor table, and I believe it is used so that the individual
149 file table pointers don't have to be rewritten when the objects are
150 merged together into the program file.
152 Unlike COFF, the basic symbol & string tables are split into
153 external and local symbols/strings. The relocation information
154 only goes off of the external symbol table, and the debug
155 information only goes off of the internal symbol table. The
156 external symbols can have links to an appropriate file index and
157 symbol within the file to give it the appropriate type information.
158 Because of this, the external symbols are actually larger than the
159 internal symbols (to contain the link information), and contain the
160 local symbol structure as a member, though this member is not the
161 first member of the external symbol structure (!). I suspect this
162 split is to make strip easier to deal with.
164 Each file table has offsets for where the line numbers, local
165 strings, local symbols, and procedure table starts from within the
166 global tables, and the indices are reset to 0 for each of those
169 The procedure table contains the binary equivalents of the .ent
170 (start of the function address), .frame (what register is the
171 virtual frame pointer, constant offset from the register to obtain
172 the VFP, and what register holds the return address), .mask/.fmask
173 (bitmask of saved registers, and where the first register is stored
174 relative to the VFP) assembler directives. It also contains the
175 low and high bounds of the line numbers if debugging is turned on.
177 The line number table is a compressed form of the normal COFF line
178 table. Each line number entry is either 1 or 3 bytes long, and
179 contains a signed delta from the previous line, and an unsigned
180 count of the number of instructions this statement takes.
182 The local symbol table contains the following fields:
184 1) iss: index to the local string table giving the name of the
187 2) value: value of the symbol (address, register number, etc.).
189 3) st: symbol type. The current symbol types are:
191 stNil -- Nuthin' special
192 stGlobal -- external symbol
194 stParam -- procedure argument
195 stLocal -- local variable
197 stProc -- External Procedure
198 stBlock -- beginning of block
199 stEnd -- end (of anything)
200 stMember -- member (of anything)
201 stTypedef -- type definition
203 stRegReloc -- register relocation
204 stForward -- forwarding address
205 stStaticProc -- Static procedure
208 4) sc: storage class. The current storage classes are:
210 scText -- text symbol
211 scData -- initialized data symbol
212 scBss -- un-initialized data symbol
213 scRegister -- value of symbol is register number
214 scAbs -- value of symbol is absolute
215 scUndefined -- who knows?
216 scCdbLocal -- variable's value is IN se->va.??
217 scBits -- this is a bit field
218 scCdbSystem -- value is IN debugger's address space
219 scRegImage -- register value saved on stack
220 scInfo -- symbol contains debugger information
221 scUserStruct -- addr in struct user for current process
222 scSData -- load time only small data
223 scSBss -- load time only small common
224 scRData -- load time only read only data
225 scVar -- Var parameter (fortranpascal)
226 scCommon -- common variable
227 scSCommon -- small common
228 scVarRegister -- Var parameter in a register
229 scVariant -- Variant record
230 scSUndefined -- small undefined(external) data
231 scInit -- .init section symbol
233 5) index: pointer to a local symbol or aux. entry.
237 For the following program:
242 printf("Hello World!\n");
246 Mips-tdump produces the following information:
251 timestamp 645311799, Wed Jun 13 17:16:39 1990
252 symbolic header offset 284
253 symbolic header size 96
257 Symbolic header, magic number = 0x7009, vstamp = 1.31:
259 Info Offset Number Bytes
260 ==== ====== ====== =====
262 Line numbers 380 4 4 [13]
264 Procedures Tables 384 1 52
265 Local Symbols 436 16 192
266 Optimization Symbols 0 0 0
267 Auxiliary Symbols 628 39 156
268 Local Strings 784 80 80
269 External Strings 864 144 144
270 File Tables 1008 2 144
272 External Symbols 1152 20 320
276 Name index = 1 Readin = No
277 Merge = No Endian = LITTLE
278 Debug level = G2 Language = C
281 Info Start Number Size Offset
282 ==== ===== ====== ==== ======
283 Local strings 0 15 15 784
284 Local symbols 0 6 72 436
285 Line numbers 0 13 13 380
286 Optimization symbols 0 0 0 0
287 Procedures 0 1 52 384
288 Auxiliary symbols 0 14 56 628
289 Relative Files 0 0 0 0
291 There are 6 local symbols, starting at 436
293 Symbol# 0: "hello2.c"
296 Storage class = Text Index = 6
297 Symbol type = File Value = 0
303 Storage class = Text Index = 12
304 Symbol type = Proc Value = 0
309 Storage class = Text Index = 4
310 Symbol type = Block Value = 8
315 Storage class = Text Index = 2
316 Symbol type = End Value = 28
321 Storage class = Text Index = 1
322 Symbol type = End Value = 52
324 Symbol# 5: "hello2.c"
327 Storage class = Text Index = 0
328 Symbol type = End Value = 0
330 There are 14 auxiliary table entries, starting at 628.
332 * #0 0, [ 0/ 0], [ 0 0:0 0:0:0:0:0:0]
333 * #1 24, [ 24/ 0], [ 6 0:0 0:0:0:0:0:0]
334 * #2 8, [ 8/ 0], [ 2 0:0 0:0:0:0:0:0]
335 * #3 16, [ 16/ 0], [ 4 0:0 0:0:0:0:0:0]
336 * #4 24, [ 24/ 0], [ 6 0:0 0:0:0:0:0:0]
337 * #5 32, [ 32/ 0], [ 8 0:0 0:0:0:0:0:0]
338 * #6 40, [ 40/ 0], [10 0:0 0:0:0:0:0:0]
339 * #7 44, [ 44/ 0], [11 0:0 0:0:0:0:0:0]
340 * #8 12, [ 12/ 0], [ 3 0:0 0:0:0:0:0:0]
341 * #9 20, [ 20/ 0], [ 5 0:0 0:0:0:0:0:0]
342 * #10 28, [ 28/ 0], [ 7 0:0 0:0:0:0:0:0]
343 * #11 36, [ 36/ 0], [ 9 0:0 0:0:0:0:0:0]
344 #12 5, [ 5/ 0], [ 1 1:0 0:0:0:0:0:0]
345 #13 24, [ 24/ 0], [ 6 0:0 0:0:0:0:0:0]
347 There are 1 procedure descriptor entries, starting at 0.
349 Procedure descriptor 0:
350 Name index = 10 Name = "main"
351 .mask 0x80000000,-4 .fmask 0x00000000,0
353 Opt. start = -1 Symbols start = 1
354 First line # = 3 Last line # = 6
355 Line Offset = 0 Address = 0x00000000
357 There are 4 bytes holding line numbers, starting at 380.
358 Line 3, delta 0, count 2
359 Line 4, delta 1, count 3
360 Line 5, delta 1, count 2
361 Line 6, delta 1, count 6
363 File #1, "/usr/include/stdio.h"
365 Name index = 1 Readin = No
366 Merge = Yes Endian = LITTLE
367 Debug level = G2 Language = C
370 Info Start Number Size Offset
371 ==== ===== ====== ==== ======
372 Local strings 15 65 65 799
373 Local symbols 6 10 120 508
374 Line numbers 0 0 0 380
375 Optimization symbols 0 0 0 0
377 Auxiliary symbols 14 25 100 684
378 Relative Files 0 0 0 0
380 There are 10 local symbols, starting at 442
382 Symbol# 0: "/usr/include/stdio.h"
385 Storage class = Text Index = 10
386 Symbol type = File Value = 0
391 Storage class = Info Index = 9
392 Symbol type = Block Value = 20
397 Storage class = Info Index = 4
398 Symbol type = Member Value = 0
403 Storage class = Info Index = 15
404 Symbol type = Member Value = 32
409 Storage class = Info Index = 16
410 Symbol type = Member Value = 64
415 Storage class = Info Index = 4
416 Symbol type = Member Value = 96
421 Storage class = Info Index = 3
422 Symbol type = Member Value = 128
427 Storage class = Info Index = 2
428 Symbol type = Member Value = 144
433 Storage class = Info Index = 1
434 Symbol type = End Value = 0
436 Symbol# 9: "/usr/include/stdio.h"
439 Storage class = Text Index = 0
440 Symbol type = End Value = 0
442 There are 25 auxiliary table entries, starting at 642.
444 * #14 -1, [4095/1048575], [63 1:1 f:f:f:f:f:f]
445 #15 65544, [ 8/ 16], [ 2 0:0 1:0:0:0:0:0]
446 #16 65544, [ 8/ 16], [ 2 0:0 1:0:0:0:0:0]
447 * #17 196656, [ 48/ 48], [12 0:0 3:0:0:0:0:0]
448 * #18 8191, [4095/ 1], [63 1:1 0:0:0:0:f:1]
449 * #19 1, [ 1/ 0], [ 0 1:0 0:0:0:0:0:0]
450 * #20 20479, [4095/ 4], [63 1:1 0:0:0:0:f:4]
451 * #21 1, [ 1/ 0], [ 0 1:0 0:0:0:0:0:0]
452 * #22 0, [ 0/ 0], [ 0 0:0 0:0:0:0:0:0]
453 * #23 2, [ 2/ 0], [ 0 0:1 0:0:0:0:0:0]
454 * #24 160, [ 160/ 0], [40 0:0 0:0:0:0:0:0]
455 * #25 0, [ 0/ 0], [ 0 0:0 0:0:0:0:0:0]
456 * #26 0, [ 0/ 0], [ 0 0:0 0:0:0:0:0:0]
457 * #27 0, [ 0/ 0], [ 0 0:0 0:0:0:0:0:0]
458 * #28 0, [ 0/ 0], [ 0 0:0 0:0:0:0:0:0]
459 * #29 0, [ 0/ 0], [ 0 0:0 0:0:0:0:0:0]
460 * #30 0, [ 0/ 0], [ 0 0:0 0:0:0:0:0:0]
461 * #31 0, [ 0/ 0], [ 0 0:0 0:0:0:0:0:0]
462 * #32 0, [ 0/ 0], [ 0 0:0 0:0:0:0:0:0]
463 * #33 0, [ 0/ 0], [ 0 0:0 0:0:0:0:0:0]
464 * #34 0, [ 0/ 0], [ 0 0:0 0:0:0:0:0:0]
465 * #35 0, [ 0/ 0], [ 0 0:0 0:0:0:0:0:0]
466 * #36 0, [ 0/ 0], [ 0 0:0 0:0:0:0:0:0]
467 * #37 0, [ 0/ 0], [ 0 0:0 0:0:0:0:0:0]
468 * #38 0, [ 0/ 0], [ 0 0:0 0:0:0:0:0:0]
470 There are 0 procedure descriptor entries, starting at 1.
472 There are 20 external symbols, starting at 1152
475 Type = array [3 {160}] of struct _iobuf { ifd = 1, index = 1 }
476 String index = 0 Ifd = 1
477 Storage class = Nil Index = 17
478 Symbol type = Global Value = 60
481 String index = 5 Ifd = 1
482 Storage class = Nil Index = 1048575
483 Symbol type = Proc Value = 0
486 String index = 11 Ifd = 1
487 Storage class = Nil Index = 1048575
488 Symbol type = Proc Value = 0
491 String index = 18 Ifd = 1
492 Storage class = Nil Index = 1048575
493 Symbol type = Proc Value = 0
496 String index = 26 Ifd = 1
497 Storage class = Nil Index = 1048575
498 Symbol type = Proc Value = 0
501 String index = 32 Ifd = 1
502 Storage class = Nil Index = 1048575
503 Symbol type = Proc Value = 0
506 String index = 40 Ifd = 1
507 Storage class = Nil Index = 1048575
508 Symbol type = Proc Value = 0
511 String index = 46 Ifd = 1
512 Storage class = Nil Index = 1048575
513 Symbol type = Proc Value = 0
516 String index = 53 Ifd = 1
517 Storage class = Nil Index = 1048575
518 Symbol type = Proc Value = 0
520 Symbol# 9: "setbuffer"
521 String index = 60 Ifd = 1
522 Storage class = Nil Index = 1048575
523 Symbol type = Proc Value = 0
525 Symbol# 10: "setlinebuf"
526 String index = 70 Ifd = 1
527 Storage class = Nil Index = 1048575
528 Symbol type = Proc Value = 0
531 String index = 81 Ifd = 1
532 Storage class = Nil Index = 1048575
533 Symbol type = Proc Value = 0
536 String index = 87 Ifd = 1
537 Storage class = Nil Index = 1048575
538 Symbol type = Proc Value = 0
540 Symbol# 13: "ctermid"
541 String index = 92 Ifd = 1
542 Storage class = Nil Index = 1048575
543 Symbol type = Proc Value = 0
545 Symbol# 14: "cuserid"
546 String index = 100 Ifd = 1
547 Storage class = Nil Index = 1048575
548 Symbol type = Proc Value = 0
550 Symbol# 15: "tempnam"
551 String index = 108 Ifd = 1
552 Storage class = Nil Index = 1048575
553 Symbol type = Proc Value = 0
556 String index = 116 Ifd = 1
557 Storage class = Nil Index = 1048575
558 Symbol type = Proc Value = 0
560 Symbol# 17: "sprintf"
561 String index = 123 Ifd = 1
562 Storage class = Nil Index = 1048575
563 Symbol type = Proc Value = 0
567 String index = 131 Ifd = 0
568 Storage class = Text Index = 1
569 Symbol type = Proc Value = 0
572 String index = 136 Ifd = 0
573 Storage class = Undefined Index = 1048575
574 Symbol type = Proc Value = 0
576 The following auxiliary table entries were unused:
580 #3 16 0x00000010 short
582 #5 32 0x00000020 long
583 #6 40 0x00000028 float
584 #7 44 0x0000002c double
585 #8 12 0x0000000c unsigned char
586 #9 20 0x00000014 unsigned short
587 #10 28 0x0000001c unsigned int
588 #11 36 0x00000024 unsigned long
589 #14 0 0x00000000 void
590 #15 24 0x00000018 int
591 #19 32 0x00000020 long
592 #20 40 0x00000028 float
593 #21 44 0x0000002c double
594 #22 12 0x0000000c unsigned char
595 #23 20 0x00000014 unsigned short
596 #24 28 0x0000001c unsigned int
597 #25 36 0x00000024 unsigned long
598 #26 48 0x00000030 struct no name { ifd = -1, index = 1048575 }
605 #include "coretypes.h"
609 #include "filenames.h"
615 /* Include getopt.h for the sake of getopt_long. */
622 /* Due to size_t being defined in sys/types.h and different
623 in stddef.h, we have to do this by hand..... Note, these
624 types are correct for MIPS based systems, and may not be
625 correct for other systems. Ultrix 4.0 and Silicon Graphics
626 have this fixed, but since the following is correct, and
627 the fact that including stddef.h gets you GCC's version
628 instead of the standard one it's not worth it to fix it. */
630 #if defined(__OSF1__) || defined(__OSF__) || defined(__osf__)
631 #define Size_t long unsigned int
633 #define Size_t unsigned int
635 #define Ptrdiff_t long
637 /* The following might be called from obstack or malloc,
638 so they can't be static. */
640 extern void pfatal_with_name (const char *) ATTRIBUTE_NORETURN
;
641 extern void botch (const char *) ATTRIBUTE_NORETURN
;
643 extern void fatal (const char *format
, ...) ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN
;
644 extern void error (const char *format
, ...) ATTRIBUTE_PRINTF_1
;
646 #ifndef MIPS_DEBUGGING_INFO
648 static int line_number
;
649 static int cur_line_start
;
651 static int had_errors
;
652 static const char *progname
;
653 static const char *input_name
;
658 fprintf (stderr
, "Mips-tfile should only be run on a MIPS computer!\n");
662 #else /* MIPS_DEBUGGING defined */
664 /* The local and global symbols have a field index, so undo any defines
665 of index -> strchr. */
669 #ifndef CROSS_DIRECTORY_STRUCTURE
672 #include "mips/a.out.h"
673 #endif /* CROSS_DIRECTORY_STRUCTURE */
683 #define IS_ASM_IDENT(ch) \
684 (ISIDNUM (ch) || (ch) == '.' || (ch) == '$')
687 /* Redefinition of storage classes as an enumeration for better
691 sc_Nil
= scNil
, /* no storage class */
692 sc_Text
= scText
, /* text symbol */
693 sc_Data
= scData
, /* initialized data symbol */
694 sc_Bss
= scBss
, /* un-initialized data symbol */
695 sc_Register
= scRegister
, /* value of symbol is register number */
696 sc_Abs
= scAbs
, /* value of symbol is absolute */
697 sc_Undefined
= scUndefined
, /* who knows? */
698 sc_CdbLocal
= scCdbLocal
, /* variable's value is IN se->va.?? */
699 sc_Bits
= scBits
, /* this is a bit field */
700 sc_CdbSystem
= scCdbSystem
, /* value is IN CDB's address space */
701 sc_RegImage
= scRegImage
, /* register value saved on stack */
702 sc_Info
= scInfo
, /* symbol contains debugger information */
703 sc_UserStruct
= scUserStruct
, /* addr in struct user for current process */
704 sc_SData
= scSData
, /* load time only small data */
705 sc_SBss
= scSBss
, /* load time only small common */
706 sc_RData
= scRData
, /* load time only read only data */
707 sc_Var
= scVar
, /* Var parameter (fortran,pascal) */
708 sc_Common
= scCommon
, /* common variable */
709 sc_SCommon
= scSCommon
, /* small common */
710 sc_VarRegister
= scVarRegister
, /* Var parameter in a register */
711 sc_Variant
= scVariant
, /* Variant record */
712 sc_SUndefined
= scSUndefined
, /* small undefined(external) data */
713 sc_Init
= scInit
, /* .init section symbol */
714 sc_Max
= scMax
/* Max storage class+1 */
717 /* Redefinition of symbol type. */
720 st_Nil
= stNil
, /* Nuthin' special */
721 st_Global
= stGlobal
, /* external symbol */
722 st_Static
= stStatic
, /* static */
723 st_Param
= stParam
, /* procedure argument */
724 st_Local
= stLocal
, /* local variable */
725 st_Label
= stLabel
, /* label */
726 st_Proc
= stProc
, /* " " Procedure */
727 st_Block
= stBlock
, /* beginning of block */
728 st_End
= stEnd
, /* end (of anything) */
729 st_Member
= stMember
, /* member (of anything - struct/union/enum */
730 st_Typedef
= stTypedef
, /* type definition */
731 st_File
= stFile
, /* file name */
732 st_RegReloc
= stRegReloc
, /* register relocation */
733 st_Forward
= stForward
, /* forwarding address */
734 st_StaticProc
= stStaticProc
, /* load time only static procs */
735 st_Constant
= stConstant
, /* const */
736 st_Str
= stStr
, /* string */
737 st_Number
= stNumber
, /* pure number (i.e. 4 NOR 2+2) */
738 st_Expr
= stExpr
, /* 2+2 vs. 4 */
739 st_Type
= stType
, /* post-coercion SER */
740 st_Max
= stMax
/* max type+1 */
743 /* Redefinition of type qualifiers. */
746 tq_Nil
= tqNil
, /* bt is what you see */
747 tq_Ptr
= tqPtr
, /* pointer */
748 tq_Proc
= tqProc
, /* procedure */
749 tq_Array
= tqArray
, /* duh */
750 tq_Far
= tqFar
, /* longer addressing - 8086/8 land */
751 tq_Vol
= tqVol
, /* volatile */
752 tq_Max
= tqMax
/* Max type qualifier+1 */
755 /* Redefinition of basic types. */
758 bt_Nil
= btNil
, /* undefined */
759 bt_Adr
= btAdr
, /* address - integer same size as pointer */
760 bt_Char
= btChar
, /* character */
761 bt_UChar
= btUChar
, /* unsigned character */
762 bt_Short
= btShort
, /* short */
763 bt_UShort
= btUShort
, /* unsigned short */
764 bt_Int
= btInt
, /* int */
765 bt_UInt
= btUInt
, /* unsigned int */
766 bt_Long
= btLong
, /* long */
767 bt_ULong
= btULong
, /* unsigned long */
768 bt_Float
= btFloat
, /* float (real) */
769 bt_Double
= btDouble
, /* Double (real) */
770 bt_Struct
= btStruct
, /* Structure (Record) */
771 bt_Union
= btUnion
, /* Union (variant) */
772 bt_Enum
= btEnum
, /* Enumerated */
773 bt_Typedef
= btTypedef
, /* defined via a typedef, isymRef points */
774 bt_Range
= btRange
, /* subrange of int */
775 bt_Set
= btSet
, /* pascal sets */
776 bt_Complex
= btComplex
, /* fortran complex */
777 bt_DComplex
= btDComplex
, /* fortran double complex */
778 bt_Indirect
= btIndirect
, /* forward or unnamed typedef */
779 bt_FixedDec
= btFixedDec
, /* Fixed Decimal */
780 bt_FloatDec
= btFloatDec
, /* Float Decimal */
781 bt_String
= btString
, /* Varying Length Character String */
782 bt_Bit
= btBit
, /* Aligned Bit String */
783 bt_Picture
= btPicture
, /* Picture */
786 bt_Void
= btVoid
, /* Void */
788 #define bt_Void bt_Nil
791 bt_Max
= btMax
/* Max basic type+1 */
796 /* Basic COFF storage classes. */
828 /* Regular COFF fundamental type. */
829 typedef enum coff_type
{
849 /* Regular COFF derived types. */
850 typedef enum coff_dt
{
858 #define N_BTMASK 017 /* bitmask to isolate basic type */
859 #define N_TMASK 003 /* bitmask to isolate derived type */
860 #define N_BT_SHIFT 4 /* # bits to shift past basic type */
861 #define N_TQ_SHIFT 2 /* # bits to shift derived types */
862 #define N_TQ 6 /* # of type qualifiers */
864 /* States for whether to hash type or not. */
865 typedef enum hash_state
{
866 hash_no
= 0, /* don't hash type */
867 hash_yes
= 1, /* ok to hash type, or use previous hash */
868 hash_record
= 2 /* ok to record hash, but don't use prev. */
872 /* Types of different sized allocation requests. */
874 alloc_type_none
, /* dummy value */
875 alloc_type_scope
, /* nested scopes linked list */
876 alloc_type_vlinks
, /* glue linking pages in varray */
877 alloc_type_shash
, /* string hash element */
878 alloc_type_thash
, /* type hash element */
879 alloc_type_tag
, /* struct/union/tag element */
880 alloc_type_forward
, /* element to hold unknown tag */
881 alloc_type_thead
, /* head of type hash list */
882 alloc_type_varray
, /* general varray allocation */
883 alloc_type_last
/* last+1 element for array bounds */
887 #define WORD_ALIGN(x) (((x) + (sizeof (long) - 1)) & ~ (sizeof (long) - 1))
888 #define DWORD_ALIGN(x) (((x) + 7) & ~7)
891 /* Structures to provide n-number of virtual arrays, each of which can
892 grow linearly, and which are written in the object file as sequential
893 pages. On systems with a BSD malloc that define USE_MALLOC, the
894 MAX_CLUSTER_PAGES should be 1 less than a power of two, since malloc
895 adds its overhead, and rounds up to the next power of 2. Pages are
896 linked together via a linked list. */
899 #define PAGE_SIZE 32768 /* size of varray pages */
902 #define PAGE_USIZE ((Size_t) PAGE_SIZE)
905 #ifndef MAX_CLUSTER_PAGES /* # pages to get from system */
906 #ifndef USE_MALLOC /* in one memory request */
907 #define MAX_CLUSTER_PAGES 64
909 #define MAX_CLUSTER_PAGES 63
914 /* Linked list connecting separate page allocations. */
915 typedef struct vlinks
{
916 struct vlinks
*prev
; /* previous set of pages */
917 struct vlinks
*next
; /* next set of pages */
918 union page
*datum
; /* start of page */
919 unsigned long start_index
; /* starting index # of page */
923 /* Virtual array header. */
924 typedef struct varray
{
925 vlinks_t
*first
; /* first page link */
926 vlinks_t
*last
; /* last page link */
927 unsigned long num_allocated
; /* # objects allocated */
928 unsigned short object_size
; /* size in bytes of each object */
929 unsigned short objects_per_page
; /* # objects that can fit on a page */
930 unsigned short objects_last_page
; /* # objects allocated on last page */
934 #define OBJECTS_PER_PAGE(type) (PAGE_SIZE / sizeof (type))
936 #define OBJECTS_PER_PAGE(type) ((sizeof (type) > 1) ? 1 : PAGE_SIZE)
939 #define INIT_VARRAY(type) { /* macro to initialize a varray */ \
940 (vlinks_t *) 0, /* first */ \
941 (vlinks_t *) 0, /* last */ \
942 0, /* num_allocated */ \
943 sizeof (type), /* object_size */ \
944 OBJECTS_PER_PAGE (type), /* objects_per_page */ \
945 OBJECTS_PER_PAGE (type), /* objects_last_page */ \
948 #define INITIALIZE_VARRAY(x,type) \
950 (x)->object_size = sizeof (type); \
951 (x)->objects_per_page = OBJECTS_PER_PAGE (type); \
952 (x)->objects_last_page = OBJECTS_PER_PAGE (type); \
955 /* Master type for indexes within the symbol table. */
956 typedef unsigned long symint_t
;
959 /* Linked list support for nested scopes (file, block, structure, etc.). */
960 typedef struct scope
{
961 struct scope
*prev
; /* previous scope level */
962 struct scope
*free
; /* free list pointer */
963 SYMR
*lsym
; /* pointer to local symbol node */
964 symint_t lnumber
; /* lsym index */
965 st_t type
; /* type of the node */
969 /* Forward reference list for tags referenced, but not yet defined. */
970 typedef struct forward
{
971 struct forward
*next
; /* next forward reference */
972 struct forward
*free
; /* free list pointer */
973 AUXU
*ifd_ptr
; /* pointer to store file index */
974 AUXU
*index_ptr
; /* pointer to store symbol index */
975 AUXU
*type_ptr
; /* pointer to munge type info */
979 /* Linked list support for tags. The first tag in the list is always
980 the current tag for that block. */
982 struct tag
*free
; /* free list pointer */
983 struct shash
*hash_ptr
; /* pointer to the hash table head */
984 struct tag
*same_name
; /* tag with same name in outer scope */
985 struct tag
*same_block
; /* next tag defined in the same block. */
986 struct forward
*forward_ref
; /* list of forward references */
987 bt_t basic_type
; /* bt_Struct, bt_Union, or bt_Enum */
988 symint_t ifd
; /* file # tag defined in */
989 symint_t indx
; /* index within file's local symbols */
993 /* Head of a block's linked list of tags. */
994 typedef struct thead
{
995 struct thead
*prev
; /* previous block */
996 struct thead
*free
; /* free list pointer */
997 struct tag
*first_tag
; /* first tag in block defined */
1001 /* Union containing pointers to each the small structures which are freed up. */
1002 typedef union small_free
{
1003 scope_t
*f_scope
; /* scope structure */
1004 thead_t
*f_thead
; /* tag head structure */
1005 tag_t
*f_tag
; /* tag element structure */
1006 forward_t
*f_forward
; /* forward tag reference */
1010 /* String hash table support. The size of the hash table must fit
1014 #define SHASH_SIZE 1009
1017 #define HASH_LEN_MAX ((1 << 12) - 1) /* Max length we can store */
1019 typedef struct shash
{
1020 struct shash
*next
; /* next hash value */
1021 char *string
; /* string we are hashing */
1022 symint_t len
; /* string length */
1023 symint_t indx
; /* index within string table */
1024 EXTR
*esym_ptr
; /* global symbol pointer */
1025 SYMR
*sym_ptr
; /* local symbol pointer */
1026 SYMR
*end_ptr
; /* symbol pointer to end block */
1027 tag_t
*tag_ptr
; /* tag pointer */
1028 PDR
*proc_ptr
; /* procedure descriptor pointer */
1032 /* Type hash table support. The size of the hash table must fit
1033 within a page with the other extended file descriptor information.
1034 Because unique types which are hashed are fewer in number than
1035 strings, we use a smaller hash value. */
1038 #define THASH_SIZE 113
1041 typedef struct thash
{
1042 struct thash
*next
; /* next hash value */
1043 AUXU type
; /* type we are hashing */
1044 symint_t indx
; /* index within string table */
1048 /* Extended file descriptor that contains all of the support necessary
1049 to add things to each file separately. */
1050 typedef struct efdr
{
1051 FDR fdr
; /* File header to be written out */
1052 FDR
*orig_fdr
; /* original file header */
1053 char *name
; /* filename */
1054 int name_len
; /* length of the filename */
1055 symint_t void_type
; /* aux. pointer to 'void' type */
1056 symint_t int_type
; /* aux. pointer to 'int' type */
1057 scope_t
*cur_scope
; /* current nested scopes */
1058 symint_t file_index
; /* current file number */
1059 int nested_scopes
; /* # nested scopes */
1060 varray_t strings
; /* local strings */
1061 varray_t symbols
; /* local symbols */
1062 varray_t procs
; /* procedures */
1063 varray_t aux_syms
; /* auxiliary symbols */
1064 struct efdr
*next_file
; /* next file descriptor */
1065 /* string/type hash tables */
1066 shash_t
**shash_head
; /* string hash table */
1067 thash_t
*thash_head
[THASH_SIZE
];
1070 /* Pre-initialized extended file structure. */
1071 static int init_file_initialized
= 0;
1072 static efdr_t init_file
;
1074 static efdr_t
*first_file
; /* first file descriptor */
1075 static efdr_t
**last_file_ptr
= &first_file
; /* file descriptor tail */
1078 /* Union of various things that are held in pages. */
1079 typedef union page
{
1080 char byte
[ PAGE_SIZE
];
1081 unsigned char ubyte
[ PAGE_SIZE
];
1082 efdr_t file
[ PAGE_SIZE
/ sizeof (efdr_t
) ];
1083 FDR ofile
[ PAGE_SIZE
/ sizeof (FDR
) ];
1084 PDR proc
[ PAGE_SIZE
/ sizeof (PDR
) ];
1085 SYMR sym
[ PAGE_SIZE
/ sizeof (SYMR
) ];
1086 EXTR esym
[ PAGE_SIZE
/ sizeof (EXTR
) ];
1087 AUXU aux
[ PAGE_SIZE
/ sizeof (AUXU
) ];
1088 DNR dense
[ PAGE_SIZE
/ sizeof (DNR
) ];
1089 scope_t scope
[ PAGE_SIZE
/ sizeof (scope_t
) ];
1090 vlinks_t vlinks
[ PAGE_SIZE
/ sizeof (vlinks_t
) ];
1091 shash_t shash
[ PAGE_SIZE
/ sizeof (shash_t
) ];
1092 thash_t thash
[ PAGE_SIZE
/ sizeof (thash_t
) ];
1093 tag_t tag
[ PAGE_SIZE
/ sizeof (tag_t
) ];
1094 forward_t forward
[ PAGE_SIZE
/ sizeof (forward_t
) ];
1095 thead_t thead
[ PAGE_SIZE
/ sizeof (thead_t
) ];
1099 /* Structure holding allocation information for small sized structures. */
1100 typedef struct alloc_info
{
1101 const char *alloc_name
; /* name of this allocation type (must be first) */
1102 page_t
*cur_page
; /* current page being allocated from */
1103 small_free_t free_list
; /* current free list if any */
1104 int unallocated
; /* number of elements unallocated on page */
1105 int total_alloc
; /* total number of allocations */
1106 int total_free
; /* total number of frees */
1107 int total_pages
; /* total number of pages allocated */
1110 /* Type information collected together. */
1111 typedef struct type_info
{
1112 bt_t basic_type
; /* basic type */
1113 coff_type_t orig_type
; /* original COFF-based type */
1114 int num_tq
; /* # type qualifiers */
1115 int num_dims
; /* # dimensions */
1116 int num_sizes
; /* # sizes */
1117 int extra_sizes
; /* # extra sizes not tied with dims */
1118 tag_t
* tag_ptr
; /* tag pointer */
1119 int bitfield
; /* symbol is a bitfield */
1120 int unknown_tag
; /* this is an unknown tag */
1121 tq_t type_qualifiers
[N_TQ
]; /* type qualifiers (ptr, func, array)*/
1122 symint_t dimensions
[N_TQ
]; /* dimensions for each array */
1123 symint_t sizes
[N_TQ
+2]; /* sizes of each array slice + size of
1124 struct/union/enum + bitfield size */
1127 /* Pre-initialized type_info struct. */
1128 static type_info_t type_info_init
= {
1129 bt_Nil
, /* basic type */
1130 T_NULL
, /* original COFF-based type */
1131 0, /* # type qualifiers */
1132 0, /* # dimensions */
1134 0, /* sizes not tied with dims */
1135 NULL
, /* ptr to tag */
1137 0, /* unknown tag */
1138 { /* type qualifiers */
1167 /* Global virtual arrays & hash table for external strings as well as
1168 for the tags table and global tables for file descriptors, and
1171 static varray_t file_desc
= INIT_VARRAY (efdr_t
);
1172 static varray_t dense_num
= INIT_VARRAY (DNR
);
1173 static varray_t tag_strings
= INIT_VARRAY (char);
1174 static varray_t ext_strings
= INIT_VARRAY (char);
1175 static varray_t ext_symbols
= INIT_VARRAY (EXTR
);
1177 static shash_t
*orig_str_hash
[SHASH_SIZE
];
1178 static shash_t
*ext_str_hash
[SHASH_SIZE
];
1179 static shash_t
*tag_hash
[SHASH_SIZE
];
1181 /* Static types for int and void. Also, remember the last function's
1182 type (which is set up when we encounter the declaration for the
1183 function, and used when the end block for the function is emitted. */
1185 static type_info_t int_type_info
;
1186 static type_info_t void_type_info
;
1187 static type_info_t last_func_type_info
;
1188 static EXTR
*last_func_eptr
;
1191 /* Convert COFF basic type to ECOFF basic type. The T_NULL type
1192 really should use bt_Void, but this causes the current ecoff GDB to
1193 issue unsupported type messages, and the Ultrix 4.00 dbx (aka MIPS
1194 2.0) doesn't understand it, even though the compiler generates it.
1195 Maybe this will be fixed in 2.10 or 2.20 of the MIPS compiler
1196 suite, but for now go with what works. */
1198 static const bt_t map_coff_types
[ (int) T_MAX
] = {
1199 bt_Nil
, /* T_NULL */
1201 bt_Char
, /* T_CHAR */
1202 bt_Short
, /* T_SHORT */
1204 bt_Long
, /* T_LONG */
1205 bt_Float
, /* T_FLOAT */
1206 bt_Double
, /* T_DOUBLE */
1207 bt_Struct
, /* T_STRUCT */
1208 bt_Union
, /* T_UNION */
1209 bt_Enum
, /* T_ENUM */
1210 bt_Enum
, /* T_MOE */
1211 bt_UChar
, /* T_UCHAR */
1212 bt_UShort
, /* T_USHORT */
1213 bt_UInt
, /* T_UINT */
1214 bt_ULong
/* T_ULONG */
1217 /* Convert COFF storage class to ECOFF storage class. */
1218 static const sc_t map_coff_storage
[ (int) C_MAX
] = {
1219 sc_Nil
, /* 0: C_NULL */
1220 sc_Abs
, /* 1: C_AUTO auto var */
1221 sc_Undefined
, /* 2: C_EXT external */
1222 sc_Data
, /* 3: C_STAT static */
1223 sc_Register
, /* 4: C_REG register */
1224 sc_Undefined
, /* 5: C_EXTDEF ??? */
1225 sc_Text
, /* 6: C_LABEL label */
1226 sc_Text
, /* 7: C_ULABEL user label */
1227 sc_Info
, /* 8: C_MOS member of struct */
1228 sc_Abs
, /* 9: C_ARG argument */
1229 sc_Info
, /* 10: C_STRTAG struct tag */
1230 sc_Info
, /* 11: C_MOU member of union */
1231 sc_Info
, /* 12: C_UNTAG union tag */
1232 sc_Info
, /* 13: C_TPDEF typedef */
1233 sc_Data
, /* 14: C_USTATIC ??? */
1234 sc_Info
, /* 15: C_ENTAG enum tag */
1235 sc_Info
, /* 16: C_MOE member of enum */
1236 sc_Register
, /* 17: C_REGPARM register parameter */
1237 sc_Bits
, /* 18; C_FIELD bitfield */
1319 sc_Text
, /* 100: C_BLOCK block start/end */
1320 sc_Text
, /* 101: C_FCN function start/end */
1321 sc_Info
, /* 102: C_EOS end of struct/union/enum */
1322 sc_Nil
, /* 103: C_FILE file start */
1323 sc_Nil
, /* 104: C_LINE line number */
1324 sc_Nil
, /* 105: C_ALIAS combined type info */
1325 sc_Nil
, /* 106: C_HIDDEN ??? */
1328 /* Convert COFF storage class to ECOFF symbol type. */
1329 static const st_t map_coff_sym_type
[ (int) C_MAX
] = {
1330 st_Nil
, /* 0: C_NULL */
1331 st_Local
, /* 1: C_AUTO auto var */
1332 st_Global
, /* 2: C_EXT external */
1333 st_Static
, /* 3: C_STAT static */
1334 st_Local
, /* 4: C_REG register */
1335 st_Global
, /* 5: C_EXTDEF ??? */
1336 st_Label
, /* 6: C_LABEL label */
1337 st_Label
, /* 7: C_ULABEL user label */
1338 st_Member
, /* 8: C_MOS member of struct */
1339 st_Param
, /* 9: C_ARG argument */
1340 st_Block
, /* 10: C_STRTAG struct tag */
1341 st_Member
, /* 11: C_MOU member of union */
1342 st_Block
, /* 12: C_UNTAG union tag */
1343 st_Typedef
, /* 13: C_TPDEF typedef */
1344 st_Static
, /* 14: C_USTATIC ??? */
1345 st_Block
, /* 15: C_ENTAG enum tag */
1346 st_Member
, /* 16: C_MOE member of enum */
1347 st_Param
, /* 17: C_REGPARM register parameter */
1348 st_Member
, /* 18; C_FIELD bitfield */
1430 st_Block
, /* 100: C_BLOCK block start/end */
1431 st_Proc
, /* 101: C_FCN function start/end */
1432 st_End
, /* 102: C_EOS end of struct/union/enum */
1433 st_File
, /* 103: C_FILE file start */
1434 st_Nil
, /* 104: C_LINE line number */
1435 st_Nil
, /* 105: C_ALIAS combined type info */
1436 st_Nil
, /* 106: C_HIDDEN ??? */
1439 /* Map COFF derived types to ECOFF type qualifiers. */
1440 static const tq_t map_coff_derived_type
[ (int) DT_MAX
] = {
1441 tq_Nil
, /* 0: DT_NON no more qualifiers */
1442 tq_Ptr
, /* 1: DT_PTR pointer */
1443 tq_Proc
, /* 2: DT_FCN function */
1444 tq_Array
, /* 3: DT_ARY array */
1448 /* Keep track of different sized allocation requests. */
1449 static alloc_info_t alloc_counts
[ (int) alloc_type_last
];
1452 /* Pointers and such to the original symbol table that is read in. */
1453 static struct filehdr orig_file_header
; /* global object file header */
1455 static HDRR orig_sym_hdr
; /* symbolic header on input */
1456 static char *orig_linenum
; /* line numbers */
1457 static DNR
*orig_dense
; /* dense numbers */
1458 static PDR
*orig_procs
; /* procedures */
1459 static SYMR
*orig_local_syms
; /* local symbols */
1460 static OPTR
*orig_opt_syms
; /* optimization symbols */
1461 static AUXU
*orig_aux_syms
; /* auxiliary symbols */
1462 static char *orig_local_strs
; /* local strings */
1463 static char *orig_ext_strs
; /* external strings */
1464 static FDR
*orig_files
; /* file descriptors */
1465 static symint_t
*orig_rfds
; /* relative file desc's */
1466 static EXTR
*orig_ext_syms
; /* external symbols */
1468 /* Macros to convert an index into a given object within the original
1470 #define CHECK(num,max,str) \
1471 (((unsigned long) num > (unsigned long) max) ? out_of_bounds (num, max, str, __LINE__) : 0)
1473 #define ORIG_LINENUM(indx) (CHECK ((indx), orig_sym_hdr.cbLine, "line#"), (indx) + orig_linenum)
1474 #define ORIG_DENSE(indx) (CHECK ((indx), orig_sym_hdr.idnMax, "dense"), (indx) + orig_dense)
1475 #define ORIG_PROCS(indx) (CHECK ((indx), orig_sym_hdr.ipdMax, "procs"), (indx) + orig_procs)
1476 #define ORIG_FILES(indx) (CHECK ((indx), orig_sym_hdr.ifdMax, "funcs"), (indx) + orig_files)
1477 #define ORIG_LSYMS(indx) (CHECK ((indx), orig_sym_hdr.isymMax, "lsyms"), (indx) + orig_local_syms)
1478 #define ORIG_LSTRS(indx) (CHECK ((indx), orig_sym_hdr.issMax, "lstrs"), (indx) + orig_local_strs)
1479 #define ORIG_ESYMS(indx) (CHECK ((indx), orig_sym_hdr.iextMax, "esyms"), (indx) + orig_ext_syms)
1480 #define ORIG_ESTRS(indx) (CHECK ((indx), orig_sym_hdr.issExtMax, "estrs"), (indx) + orig_ext_strs)
1481 #define ORIG_OPT(indx) (CHECK ((indx), orig_sym_hdr.ioptMax, "opt"), (indx) + orig_opt_syms)
1482 #define ORIG_AUX(indx) (CHECK ((indx), orig_sym_hdr.iauxMax, "aux"), (indx) + orig_aux_syms)
1483 #define ORIG_RFDS(indx) (CHECK ((indx), orig_sym_hdr.crfd, "rfds"), (indx) + orig_rfds)
1485 /* Various other statics. */
1486 static HDRR symbolic_header
; /* symbolic header */
1487 static efdr_t
*cur_file_ptr
= (efdr_t
*) 0; /* current file desc. header */
1488 static PDR
*cur_proc_ptr
= (PDR
*) 0; /* current procedure header */
1489 static SYMR
*cur_oproc_begin
= (SYMR
*) 0; /* original proc. sym begin info */
1490 static SYMR
*cur_oproc_end
= (SYMR
*) 0; /* original proc. sym end info */
1491 static PDR
*cur_oproc_ptr
= (PDR
*) 0; /* current original procedure*/
1492 static thead_t
*cur_tag_head
= (thead_t
*) 0;/* current tag head */
1493 static unsigned long file_offset
= 0; /* current file offset */
1494 static unsigned long max_file_offset
= 0; /* maximum file offset */
1495 static FILE *object_stream
= (FILE *) 0; /* file desc. to output .o */
1496 static FILE *obj_in_stream
= (FILE *) 0; /* file desc. to input .o */
1497 static char *progname
= (char *) 0; /* program name for errors */
1498 static const char *input_name
= "stdin"; /* name of input file */
1499 static char *object_name
= (char *) 0; /* tmp. name of object file */
1500 static char *obj_in_name
= (char *) 0; /* name of input object file */
1501 static char *cur_line_start
= (char *) 0; /* current line read in */
1502 static char *cur_line_ptr
= (char *) 0; /* ptr within current line */
1503 static unsigned cur_line_nbytes
= 0; /* # bytes for current line */
1504 static unsigned cur_line_alloc
= 0; /* # bytes total in buffer */
1505 static long line_number
= 0; /* current input line number */
1506 static int debug
= 0; /* trace functions */
1507 static int version
= 0; /* print version # */
1508 static int verbose
= 0;
1509 static int had_errors
= 0; /* != 0 if errors were found */
1510 static int rename_output
= 0; /* != 0 if rename output file*/
1511 static int delete_input
= 0; /* != 0 if delete input after done */
1512 static int stabs_seen
= 0; /* != 0 if stabs have been seen */
1515 /* Pseudo symbol to use when putting stabs into the symbol table. */
1516 #ifndef STABS_SYMBOL
1517 #define STABS_SYMBOL "@stabs"
1520 static const char stabs_symbol
[] = STABS_SYMBOL
;
1523 /* Forward reference for functions. See the definition for more details. */
1526 #define STATIC static
1529 STATIC
int out_of_bounds (symint_t
, symint_t
, const char *, int);
1530 STATIC shash_t
*hash_string (const char *, Ptrdiff_t
, shash_t
**, symint_t
*);
1531 STATIC symint_t
add_string (varray_t
*, shash_t
**, const char *, const char *,
1533 STATIC symint_t
add_local_symbol (const char *, const char *, st_t
, sc_t
,
1534 symint_t
, symint_t
);
1535 STATIC symint_t
add_ext_symbol (EXTR
*, int);
1536 STATIC symint_t
add_aux_sym_symint (symint_t
);
1537 STATIC symint_t
add_aux_sym_rndx (int, symint_t
);
1538 STATIC symint_t
add_aux_sym_tir (type_info_t
*, hash_state_t
, thash_t
**);
1539 STATIC tag_t
* get_tag (const char *, const char *, symint_t
, bt_t
);
1540 STATIC
void add_unknown_tag (tag_t
*);
1541 STATIC
void add_procedure (const char *, const char *);
1542 STATIC
void initialize_init_file (void);
1543 STATIC
void add_file (const char *, const char *);
1544 STATIC
void add_bytes (varray_t
*, char *, Size_t
);
1545 STATIC
void add_varray_page (varray_t
*);
1546 STATIC
void update_headers (void);
1547 STATIC
void write_varray (varray_t
*, off_t
, const char *);
1548 STATIC
void write_object (void);
1549 STATIC
const char *st_to_string (st_t
);
1550 STATIC
const char *sc_to_string (sc_t
);
1551 STATIC
char *read_line (void);
1552 STATIC
void parse_input (void);
1553 STATIC
void mark_stabs (const char *);
1554 STATIC
void parse_begin (const char *);
1555 STATIC
void parse_bend (const char *);
1556 STATIC
void parse_def (const char *);
1557 STATIC
void parse_end (const char *);
1558 STATIC
void parse_ent (const char *);
1559 STATIC
void parse_file (const char *);
1560 STATIC
void parse_stabs_common (const char *, const char *, const char *);
1561 STATIC
void parse_stabs (const char *);
1562 STATIC
void parse_stabn (const char *);
1563 STATIC page_t
*read_seek (Size_t
, off_t
, const char *);
1564 STATIC
void copy_object (void);
1566 STATIC
void catch_signal (int) ATTRIBUTE_NORETURN
;
1567 STATIC page_t
*allocate_page (void);
1568 STATIC page_t
*allocate_multiple_pages (Size_t
);
1569 STATIC
void free_multiple_pages (page_t
*, Size_t
);
1571 #ifndef MALLOC_CHECK
1572 STATIC page_t
*allocate_cluster (Size_t
);
1575 STATIC forward_t
*allocate_forward (void);
1576 STATIC scope_t
*allocate_scope (void);
1577 STATIC shash_t
*allocate_shash (void);
1578 STATIC tag_t
*allocate_tag (void);
1579 STATIC thash_t
*allocate_thash (void);
1580 STATIC thead_t
*allocate_thead (void);
1581 STATIC vlinks_t
*allocate_vlinks (void);
1583 STATIC
void free_forward (forward_t
*);
1584 STATIC
void free_scope (scope_t
*);
1585 STATIC
void free_tag (tag_t
*);
1586 STATIC
void free_thead (thead_t
*);
1588 extern char *optarg
;
1592 /* List of assembler pseudo ops and beginning sequences that need
1593 special actions. Someday, this should be a hash table, and such,
1594 but for now a linear list of names and calls to memcmp will
1597 typedef struct _pseudo_ops
{
1598 const char *const name
; /* pseudo-op in ascii */
1599 const int len
; /* length of name to compare */
1600 void (*const func
) (const char *); /* function to handle line */
1603 static const pseudo_ops_t pseudo_ops
[] = {
1604 { "#.def", sizeof("#.def")-1, parse_def
},
1605 { "#.begin", sizeof("#.begin")-1, parse_begin
},
1606 { "#.bend", sizeof("#.bend")-1, parse_bend
},
1607 { ".end", sizeof(".end")-1, parse_end
},
1608 { ".ent", sizeof(".ent")-1, parse_ent
},
1609 { ".file", sizeof(".file")-1, parse_file
},
1610 { "#.stabs", sizeof("#.stabs")-1, parse_stabs
},
1611 { "#.stabn", sizeof("#.stabn")-1, parse_stabn
},
1612 { ".stabs", sizeof(".stabs")-1, parse_stabs
},
1613 { ".stabn", sizeof(".stabn")-1, parse_stabn
},
1614 { "#@stabs", sizeof("#@stabs")-1, mark_stabs
},
1618 /* Command line options for getopt_long. */
1620 static const struct option options
[] =
1622 { "version", 0, 0, 'V' },
1623 { "verbose", 0, 0, 'v' },
1627 /* Add a page to a varray object. */
1630 add_varray_page (varray_t
*vp
)
1632 vlinks_t
*new_links
= allocate_vlinks ();
1635 if (vp
->object_size
> 1)
1636 new_links
->datum
= xcalloc (1, vp
->object_size
);
1639 new_links
->datum
= allocate_page ();
1641 alloc_counts
[ (int) alloc_type_varray
].total_alloc
++;
1642 alloc_counts
[ (int) alloc_type_varray
].total_pages
++;
1644 new_links
->start_index
= vp
->num_allocated
;
1645 vp
->objects_last_page
= 0;
1647 if (vp
->first
== (vlinks_t
*) 0) /* first allocation? */
1648 vp
->first
= vp
->last
= new_links
;
1650 { /* 2nd or greater allocation */
1651 new_links
->prev
= vp
->last
;
1652 vp
->last
->next
= new_links
;
1653 vp
->last
= new_links
;
1658 /* Compute hash code (from tree.c) */
1663 hash_string (const char *text
, Ptrdiff_t hash_len
, shash_t
**hash_tbl
,
1664 symint_t
*ret_hash_index
)
1669 int first_ch
= *text
;
1672 for (i
= 0; i
< hash_len
; i
++)
1673 hi
= ((hi
& 0x003fffff) * 613) + (text
[i
] & 0xff);
1675 hi
&= (1 << HASHBITS
) - 1;
1678 if (ret_hash_index
!= (symint_t
*) 0)
1679 *ret_hash_index
= hi
;
1681 for (ptr
= hash_tbl
[hi
]; ptr
!= (shash_t
*) 0; ptr
= ptr
->next
)
1682 if ((symint_t
) hash_len
== ptr
->len
1683 && first_ch
== ptr
->string
[0]
1684 && memcmp (text
, ptr
->string
, hash_len
) == 0)
1691 /* Add a string (and null pad) to one of the string tables. A
1692 consequence of hashing strings, is that we don't let strings cross
1693 page boundaries. The extra nulls will be ignored. VP is a string
1694 virtual array, HASH_TBL a pointer to the hash table, the string
1695 starts at START and the position one byte after the string is given
1696 with END_P1, the resulting hash pointer is returned in RET_HASH. */
1699 add_string (varray_t
*vp
, shash_t
**hash_tbl
, const char *start
,
1700 const char *end_p1
, shash_t
**ret_hash
)
1702 Ptrdiff_t len
= end_p1
- start
;
1706 if (len
>= (Ptrdiff_t
) PAGE_USIZE
)
1707 fatal ("string too big (%ld bytes)", (long) len
);
1709 hash_ptr
= hash_string (start
, len
, hash_tbl
, &hi
);
1710 if (hash_ptr
== (shash_t
*) 0)
1714 if (vp
->objects_last_page
+ len
>= (long) PAGE_USIZE
)
1717 = ((vp
->num_allocated
+ PAGE_USIZE
- 1) / PAGE_USIZE
) * PAGE_USIZE
;
1718 add_varray_page (vp
);
1721 hash_ptr
= allocate_shash ();
1722 hash_ptr
->next
= hash_tbl
[hi
];
1723 hash_tbl
[hi
] = hash_ptr
;
1725 hash_ptr
->len
= len
;
1726 hash_ptr
->indx
= vp
->num_allocated
;
1727 hash_ptr
->string
= p
= & vp
->last
->datum
->byte
[ vp
->objects_last_page
];
1729 vp
->objects_last_page
+= len
+1;
1730 vp
->num_allocated
+= len
+1;
1738 if (ret_hash
!= (shash_t
**) 0)
1739 *ret_hash
= hash_ptr
;
1741 return hash_ptr
->indx
;
1745 /* Add a local symbol. The symbol string starts at STR_START and the
1746 first byte after it is marked by STR_END_P1. The symbol has type
1747 TYPE and storage class STORAGE and value VALUE. INDX is an index
1748 to local/aux. symbols. */
1751 add_local_symbol (const char *str_start
, const char *str_end_p1
, st_t type
,
1752 sc_t storage
, symint_t value
, symint_t indx
)
1760 varray_t
*vp
= &cur_file_ptr
->symbols
;
1761 int scope_delta
= 0;
1762 shash_t
*hash_ptr
= (shash_t
*) 0;
1764 if (vp
->objects_last_page
== vp
->objects_per_page
)
1765 add_varray_page (vp
);
1767 psym
= &vp
->last
->datum
->sym
[ vp
->objects_last_page
++ ];
1769 psym
->value
= value
;
1770 psym
->st
= (unsigned) type
;
1771 psym
->sc
= (unsigned) storage
;
1773 psym
->iss
= (str_start
== (const char *) 0)
1775 : add_string (&cur_file_ptr
->strings
,
1776 &cur_file_ptr
->shash_head
[0],
1781 ret
= vp
->num_allocated
++;
1783 if (MIPS_IS_STAB (psym
))
1786 /* Save the symbol within the hash table if this is a static
1787 item, and it has a name. */
1788 if (hash_ptr
!= (shash_t
*) 0
1789 && (type
== st_Global
|| type
== st_Static
|| type
== st_Label
1790 || type
== st_Proc
|| type
== st_StaticProc
))
1791 hash_ptr
->sym_ptr
= psym
;
1793 /* push or pop a scope if appropriate. */
1799 case st_File
: /* beginning of file */
1800 case st_Proc
: /* procedure */
1801 case st_StaticProc
: /* static procedure */
1802 case st_Block
: /* begin scope */
1803 pscope
= allocate_scope ();
1804 pscope
->prev
= cur_file_ptr
->cur_scope
;
1805 pscope
->lsym
= psym
;
1806 pscope
->lnumber
= ret
;
1807 pscope
->type
= type
;
1808 cur_file_ptr
->cur_scope
= pscope
;
1810 if (type
!= st_File
)
1813 /* For every block type except file, struct, union, or
1814 enumeration blocks, push a level on the tag stack. We omit
1815 file types, so that tags can span file boundaries. */
1816 if (type
!= st_File
&& storage
!= sc_Info
)
1818 ptag_head
= allocate_thead ();
1819 ptag_head
->first_tag
= 0;
1820 ptag_head
->prev
= cur_tag_head
;
1821 cur_tag_head
= ptag_head
;
1826 pscope
= cur_file_ptr
->cur_scope
;
1827 if (pscope
== (scope_t
*) 0)
1828 error ("internal error, too many st_End's");
1832 st_t begin_type
= (st_t
) pscope
->lsym
->st
;
1834 if (begin_type
!= st_File
)
1837 /* Except for file, structure, union, or enumeration end
1838 blocks remove all tags created within this scope. */
1839 if (begin_type
!= st_File
&& storage
!= sc_Info
)
1841 ptag_head
= cur_tag_head
;
1842 cur_tag_head
= ptag_head
->prev
;
1844 for (ptag
= ptag_head
->first_tag
;
1845 ptag
!= (tag_t
*) 0;
1848 if (ptag
->forward_ref
!= (forward_t
*) 0)
1849 add_unknown_tag (ptag
);
1851 ptag_next
= ptag
->same_block
;
1852 ptag
->hash_ptr
->tag_ptr
= ptag
->same_name
;
1856 free_thead (ptag_head
);
1859 cur_file_ptr
->cur_scope
= pscope
->prev
;
1860 psym
->index
= pscope
->lnumber
; /* blk end gets begin sym # */
1862 if (storage
!= sc_Info
)
1863 psym
->iss
= pscope
->lsym
->iss
; /* blk end gets same name */
1865 if (begin_type
== st_File
|| begin_type
== st_Block
)
1866 pscope
->lsym
->index
= ret
+1; /* block begin gets next sym # */
1868 /* Functions push two or more aux words as follows:
1869 1st word: index+1 of the end symbol
1870 2nd word: type of the function (plus any aux words needed).
1871 Also, tie the external pointer back to the function begin symbol. */
1875 pscope
->lsym
->index
= add_aux_sym_symint (ret
+1);
1876 type
= add_aux_sym_tir (&last_func_type_info
,
1878 &cur_file_ptr
->thash_head
[0]);
1881 last_func_eptr
->ifd
= cur_file_ptr
->file_index
;
1883 /* The index for an external st_Proc symbol is the index
1884 of the st_Proc symbol in the local symbol table. */
1885 last_func_eptr
->asym
.index
= psym
->index
;
1889 free_scope (pscope
);
1893 cur_file_ptr
->nested_scopes
+= scope_delta
;
1895 if (debug
&& type
!= st_File
1896 && (debug
> 2 || type
== st_Block
|| type
== st_End
1897 || type
== st_Proc
|| type
== st_StaticProc
))
1899 const char *sc_str
= sc_to_string (storage
);
1900 const char *st_str
= st_to_string (type
);
1901 int depth
= cur_file_ptr
->nested_scopes
+ (scope_delta
< 0);
1904 "\tlsym\tv= %10ld, depth= %2d, sc= %-12s",
1905 value
, depth
, sc_str
);
1907 if (str_start
&& str_end_p1
- str_start
> 0)
1908 fprintf (stderr
, " st= %-11s name= %.*s\n",
1909 st_str
, (int) (str_end_p1
- str_start
), str_start
);
1912 Size_t len
= strlen (st_str
);
1913 fprintf (stderr
, " st= %.*s\n", (int) (len
-1), st_str
);
1921 /* Add an external symbol with symbol pointer ESYM and file index
1925 add_ext_symbol (EXTR
*esym
, int ifd
)
1927 const char *str_start
; /* first byte in string */
1928 const char *str_end_p1
; /* first byte after string */
1930 varray_t
*vp
= &ext_symbols
;
1931 shash_t
*hash_ptr
= (shash_t
*) 0;
1933 str_start
= ORIG_ESTRS (esym
->asym
.iss
);
1934 str_end_p1
= str_start
+ strlen (str_start
);
1938 long value
= esym
->asym
.value
;
1939 const char *sc_str
= sc_to_string ((sc_t
) esym
->asym
.sc
);
1940 const char *st_str
= st_to_string ((st_t
) esym
->asym
.st
);
1943 "\tesym\tv= %10ld, ifd= %2d, sc= %-12s",
1944 value
, ifd
, sc_str
);
1946 if (str_start
&& str_end_p1
- str_start
> 0)
1947 fprintf (stderr
, " st= %-11s name= %.*s\n",
1948 st_str
, (int) (str_end_p1
- str_start
), str_start
);
1950 fprintf (stderr
, " st= %s\n", st_str
);
1953 if (vp
->objects_last_page
== vp
->objects_per_page
)
1954 add_varray_page (vp
);
1956 psym
= &vp
->last
->datum
->esym
[ vp
->objects_last_page
++ ];
1960 psym
->asym
.index
= indexNil
;
1961 psym
->asym
.iss
= (str_start
== (const char *) 0)
1963 : add_string (&ext_strings
,
1969 hash_ptr
->esym_ptr
= psym
;
1970 return vp
->num_allocated
++;
1974 /* Add an auxiliary symbol (passing a symint). */
1977 add_aux_sym_symint (symint_t aux_word
)
1980 efdr_t
*file_ptr
= cur_file_ptr
;
1981 varray_t
*vp
= &file_ptr
->aux_syms
;
1983 if (vp
->objects_last_page
== vp
->objects_per_page
)
1984 add_varray_page (vp
);
1986 aux_ptr
= &vp
->last
->datum
->aux
[ vp
->objects_last_page
++ ];
1987 aux_ptr
->isym
= aux_word
;
1989 return vp
->num_allocated
++;
1993 /* Add an auxiliary symbol (passing a file/symbol index combo). */
1996 add_aux_sym_rndx (int file_index
, symint_t sym_index
)
1999 efdr_t
*file_ptr
= cur_file_ptr
;
2000 varray_t
*vp
= &file_ptr
->aux_syms
;
2002 if (vp
->objects_last_page
== vp
->objects_per_page
)
2003 add_varray_page (vp
);
2005 aux_ptr
= &vp
->last
->datum
->aux
[ vp
->objects_last_page
++ ];
2006 aux_ptr
->rndx
.rfd
= file_index
;
2007 aux_ptr
->rndx
.index
= sym_index
;
2009 return vp
->num_allocated
++;
2013 /* Add an auxiliary symbol (passing the basic type and possibly
2014 type qualifiers). */
2017 add_aux_sym_tir (type_info_t
*t
, hash_state_t state
, thash_t
**hash_tbl
)
2020 efdr_t
*file_ptr
= cur_file_ptr
;
2021 varray_t
*vp
= &file_ptr
->aux_syms
;
2022 static AUXU init_aux
;
2028 aux
.ti
.bt
= (int) t
->basic_type
;
2029 aux
.ti
.continued
= 0;
2030 aux
.ti
.fBitfield
= t
->bitfield
;
2032 aux
.ti
.tq0
= (int) t
->type_qualifiers
[0];
2033 aux
.ti
.tq1
= (int) t
->type_qualifiers
[1];
2034 aux
.ti
.tq2
= (int) t
->type_qualifiers
[2];
2035 aux
.ti
.tq3
= (int) t
->type_qualifiers
[3];
2036 aux
.ti
.tq4
= (int) t
->type_qualifiers
[4];
2037 aux
.ti
.tq5
= (int) t
->type_qualifiers
[5];
2040 /* For anything that adds additional information, we must not hash,
2041 so check here, and reset our state. */
2043 if (state
!= hash_no
2044 && (t
->type_qualifiers
[0] == tq_Array
2045 || t
->type_qualifiers
[1] == tq_Array
2046 || t
->type_qualifiers
[2] == tq_Array
2047 || t
->type_qualifiers
[3] == tq_Array
2048 || t
->type_qualifiers
[4] == tq_Array
2049 || t
->type_qualifiers
[5] == tq_Array
2050 || t
->basic_type
== bt_Struct
2051 || t
->basic_type
== bt_Union
2052 || t
->basic_type
== bt_Enum
2054 || t
->num_dims
> 0))
2057 /* See if we can hash this type, and save some space, but some types
2058 can't be hashed (because they contain arrays or continuations),
2059 and others can be put into the hash list, but cannot use existing
2060 types because other aux entries precede this one. */
2062 if (state
!= hash_no
)
2067 hi
= aux
.isym
& ((1 << HASHBITS
) - 1);
2070 for (hash_ptr
= hash_tbl
[hi
];
2071 hash_ptr
!= (thash_t
*) 0;
2072 hash_ptr
= hash_ptr
->next
)
2074 if (aux
.isym
== hash_ptr
->type
.isym
)
2078 if (hash_ptr
!= (thash_t
*) 0 && state
== hash_yes
)
2079 return hash_ptr
->indx
;
2081 if (hash_ptr
== (thash_t
*) 0)
2083 hash_ptr
= allocate_thash ();
2084 hash_ptr
->next
= hash_tbl
[hi
];
2085 hash_ptr
->type
= aux
;
2086 hash_ptr
->indx
= vp
->num_allocated
;
2087 hash_tbl
[hi
] = hash_ptr
;
2091 /* Everything is set up, add the aux symbol. */
2092 if (vp
->objects_last_page
== vp
->objects_per_page
)
2093 add_varray_page (vp
);
2095 aux_ptr
= &vp
->last
->datum
->aux
[ vp
->objects_last_page
++ ];
2098 ret
= vp
->num_allocated
++;
2100 /* Add bitfield length if it exists.
2102 NOTE: Mips documentation claims bitfield goes at the end of the
2103 AUX record, but the DECstation compiler emits it here.
2104 (This would only make a difference for enum bitfields.)
2106 Also note: We use the last size given since gcc may emit 2
2107 for an enum bitfield. */
2110 (void) add_aux_sym_symint ((symint_t
) t
->sizes
[t
->num_sizes
-1]);
2113 /* Add tag information if needed. Structure, union, and enum
2114 references add 2 aux symbols: a [file index, symbol index]
2115 pointer to the structure type, and the current file index. */
2117 if (t
->basic_type
== bt_Struct
2118 || t
->basic_type
== bt_Union
2119 || t
->basic_type
== bt_Enum
)
2121 symint_t file_index
= t
->tag_ptr
->ifd
;
2122 symint_t sym_index
= t
->tag_ptr
->indx
;
2126 (void) add_aux_sym_rndx (ST_RFDESCAPE
, sym_index
);
2127 (void) add_aux_sym_symint ((symint_t
)-1);
2129 else if (sym_index
!= indexNil
)
2131 (void) add_aux_sym_rndx (ST_RFDESCAPE
, sym_index
);
2132 (void) add_aux_sym_symint (file_index
);
2136 forward_t
*forward_ref
= allocate_forward ();
2138 forward_ref
->type_ptr
= aux_ptr
;
2139 forward_ref
->next
= t
->tag_ptr
->forward_ref
;
2140 t
->tag_ptr
->forward_ref
= forward_ref
;
2142 (void) add_aux_sym_rndx (ST_RFDESCAPE
, sym_index
);
2143 forward_ref
->index_ptr
2144 = &vp
->last
->datum
->aux
[ vp
->objects_last_page
- 1];
2146 (void) add_aux_sym_symint (file_index
);
2147 forward_ref
->ifd_ptr
2148 = &vp
->last
->datum
->aux
[ vp
->objects_last_page
- 1];
2152 /* Add information about array bounds if they exist. */
2153 for (i
= 0; i
< t
->num_dims
; i
++)
2155 (void) add_aux_sym_rndx (ST_RFDESCAPE
,
2156 cur_file_ptr
->int_type
);
2158 (void) add_aux_sym_symint (cur_file_ptr
->file_index
); /* file index*/
2159 (void) add_aux_sym_symint ((symint_t
) 0); /* low bound */
2160 (void) add_aux_sym_symint (t
->dimensions
[i
] - 1); /* high bound*/
2161 (void) add_aux_sym_symint ((t
->dimensions
[i
] == 0) /* stride */
2163 : (t
->sizes
[i
] * 8) / t
->dimensions
[i
]);
2166 /* NOTE: Mips documentation claims that the bitfield width goes here.
2167 But it needs to be emitted earlier. */
2173 /* Add a tag to the tag table (unless it already exists). */
2176 get_tag (const char *tag_start
, /* 1st byte of tag name */
2177 const char *tag_end_p1
, /* 1st byte after tag name */
2178 symint_t indx
, /* index of tag start block */
2179 bt_t basic_type
) /* bt_Struct, bt_Union, or bt_Enum */
2184 hash_ptr
= hash_string (tag_start
,
2185 tag_end_p1
- tag_start
,
2189 if (hash_ptr
!= (shash_t
*) 0
2190 && hash_ptr
->tag_ptr
!= (tag_t
*) 0)
2192 tag_ptr
= hash_ptr
->tag_ptr
;
2193 if (indx
!= indexNil
)
2195 tag_ptr
->basic_type
= basic_type
;
2196 tag_ptr
->ifd
= cur_file_ptr
->file_index
;
2197 tag_ptr
->indx
= indx
;
2202 (void) add_string (&tag_strings
,
2208 tag_ptr
= allocate_tag ();
2209 tag_ptr
->forward_ref
= (forward_t
*) 0;
2210 tag_ptr
->hash_ptr
= hash_ptr
;
2211 tag_ptr
->same_name
= hash_ptr
->tag_ptr
;
2212 tag_ptr
->basic_type
= basic_type
;
2213 tag_ptr
->indx
= indx
;
2214 tag_ptr
->ifd
= (indx
== indexNil
2215 ? (symint_t
) -1 : cur_file_ptr
->file_index
);
2216 tag_ptr
->same_block
= cur_tag_head
->first_tag
;
2218 cur_tag_head
->first_tag
= tag_ptr
;
2219 hash_ptr
->tag_ptr
= tag_ptr
;
2225 /* Add an unknown {struct, union, enum} tag. */
2228 add_unknown_tag (tag_t
*ptag
)
2230 shash_t
*hash_ptr
= ptag
->hash_ptr
;
2231 char *name_start
= hash_ptr
->string
;
2232 char *name_end_p1
= name_start
+ hash_ptr
->len
;
2233 forward_t
*f_next
= ptag
->forward_ref
;
2236 int file_index
= cur_file_ptr
->file_index
;
2240 const char *agg_type
= "{unknown aggregate type}";
2241 switch (ptag
->basic_type
)
2243 case bt_Struct
: agg_type
= "struct"; break;
2244 case bt_Union
: agg_type
= "union"; break;
2245 case bt_Enum
: agg_type
= "enum"; break;
2249 fprintf (stderr
, "unknown %s %.*s found\n",
2250 agg_type
, (int) hash_ptr
->len
, name_start
);
2253 sym_index
= add_local_symbol (name_start
,
2260 (void) add_local_symbol (name_start
,
2267 while (f_next
!= (forward_t
*) 0)
2270 f_next
= f_next
->next
;
2272 f_cur
->ifd_ptr
->isym
= file_index
;
2273 f_cur
->index_ptr
->rndx
.index
= sym_index
;
2275 free_forward (f_cur
);
2282 /* Add a procedure to the current file's list of procedures, and record
2283 this is the current procedure. If the assembler created a PDR for
2284 this procedure, use that to initialize the current PDR. */
2287 add_procedure (const char *func_start
, /* 1st byte of func name */
2288 const char *func_end_p1
) /* 1st byte after func name */
2291 efdr_t
*file_ptr
= cur_file_ptr
;
2292 varray_t
*vp
= &file_ptr
->procs
;
2294 st_t proc_type
= st_Proc
;
2295 shash_t
*shash_ptr
= hash_string (func_start
,
2296 func_end_p1
- func_start
,
2301 fputc ('\n', stderr
);
2303 if (vp
->objects_last_page
== vp
->objects_per_page
)
2304 add_varray_page (vp
);
2306 cur_proc_ptr
= new_proc_ptr
= &vp
->last
->datum
->proc
[ vp
->objects_last_page
++ ];
2308 vp
->num_allocated
++;
2311 /* Did the assembler create this procedure? If so, get the PDR information. */
2312 cur_oproc_ptr
= (PDR
*) 0;
2313 if (shash_ptr
!= (shash_t
*) 0)
2315 PDR
*old_proc_ptr
= shash_ptr
->proc_ptr
;
2316 SYMR
*sym_ptr
= shash_ptr
->sym_ptr
;
2318 if (old_proc_ptr
!= (PDR
*) 0
2319 && sym_ptr
!= (SYMR
*) 0
2320 && ((st_t
) sym_ptr
->st
== st_Proc
|| (st_t
) sym_ptr
->st
== st_StaticProc
))
2322 cur_oproc_begin
= sym_ptr
;
2323 cur_oproc_end
= shash_ptr
->end_ptr
;
2324 value
= sym_ptr
->value
;
2326 cur_oproc_ptr
= old_proc_ptr
;
2327 proc_type
= (st_t
) sym_ptr
->st
;
2328 *new_proc_ptr
= *old_proc_ptr
; /* initialize */
2332 if (cur_oproc_ptr
== (PDR
*) 0)
2333 error ("did not find a PDR block for %.*s",
2334 (int) (func_end_p1
- func_start
), func_start
);
2336 /* Determine the start of symbols. */
2337 new_proc_ptr
->isym
= file_ptr
->symbols
.num_allocated
;
2339 /* Push the start of the function. */
2340 (void) add_local_symbol (func_start
, func_end_p1
,
2347 /* Initialize the init_file structure. */
2350 initialize_init_file (void)
2357 memset (&init_file
, 0, sizeof (init_file
));
2359 init_file
.fdr
.lang
= langC
;
2360 init_file
.fdr
.fMerge
= 1;
2361 init_file
.fdr
.glevel
= GLEVEL_2
;
2363 /* mips-tfile doesn't attempt to perform byte swapping and always writes
2364 out integers in its native ordering. For cross-compilers, this need
2365 not be the same as either the host or the target. The simplest thing
2366 to do is skip the configury and perform an introspective test. */
2367 /* ??? Despite the name, mips-tfile is currently only used on alpha/Tru64
2368 and would/may require significant work to be used in cross-compiler
2369 configurations, so we could simply admit defeat and hard code this as
2370 little-endian, i.e. init_file.fdr.fBigendian = 0. */
2372 if (endian_test
.c
[3])
2373 init_file
.fdr
.fBigendian
= 1;
2375 INITIALIZE_VARRAY (&init_file
.strings
, char);
2376 INITIALIZE_VARRAY (&init_file
.symbols
, SYMR
);
2377 INITIALIZE_VARRAY (&init_file
.procs
, PDR
);
2378 INITIALIZE_VARRAY (&init_file
.aux_syms
, AUXU
);
2380 init_file_initialized
= 1;
2383 /* Add a new filename, and set up all of the file relative
2384 virtual arrays (strings, symbols, aux syms, etc.). Record
2385 where the current file structure lives. */
2388 add_file (const char *file_start
, /* first byte in string */
2389 const char *file_end_p1
) /* first byte after string */
2391 static char zero_bytes
[2] = { '\0', '\0' };
2393 Ptrdiff_t len
= file_end_p1
- file_start
;
2394 int first_ch
= *file_start
;
2398 fprintf (stderr
, "\tfile\t%.*s\n", (int) len
, file_start
);
2400 /* See if the file has already been created. */
2401 for (file_ptr
= first_file
;
2402 file_ptr
!= (efdr_t
*) 0;
2403 file_ptr
= file_ptr
->next_file
)
2405 if (first_ch
== file_ptr
->name
[0]
2406 && file_ptr
->name
[len
] == '\0'
2407 && memcmp (file_start
, file_ptr
->name
, len
) == 0)
2409 cur_file_ptr
= file_ptr
;
2414 /* If this is a new file, create it. */
2415 if (file_ptr
== (efdr_t
*) 0)
2417 if (file_desc
.objects_last_page
== file_desc
.objects_per_page
)
2418 add_varray_page (&file_desc
);
2420 if (! init_file_initialized
)
2421 initialize_init_file ();
2423 file_ptr
= cur_file_ptr
2424 = &file_desc
.last
->datum
->file
[ file_desc
.objects_last_page
++ ];
2425 *file_ptr
= init_file
;
2427 file_ptr
->file_index
= file_desc
.num_allocated
++;
2429 /* Allocate the string hash table. */
2430 file_ptr
->shash_head
= (shash_t
**) allocate_page ();
2432 /* Make sure 0 byte in string table is null */
2433 add_string (&file_ptr
->strings
,
2434 &file_ptr
->shash_head
[0],
2439 if (file_end_p1
- file_start
> (long) PAGE_USIZE
-2)
2440 fatal ("filename goes over one page boundary");
2442 /* Push the start of the filename. We assume that the filename
2443 will be stored at string offset 1. */
2444 (void) add_local_symbol (file_start
, file_end_p1
, st_File
, sc_Text
,
2445 (symint_t
) 0, (symint_t
) 0);
2446 file_ptr
->fdr
.rss
= 1;
2447 file_ptr
->name
= &file_ptr
->strings
.last
->datum
->byte
[1];
2448 file_ptr
->name_len
= file_end_p1
- file_start
;
2450 /* Update the linked list of file descriptors. */
2451 *last_file_ptr
= file_ptr
;
2452 last_file_ptr
= &file_ptr
->next_file
;
2454 /* Add void & int types to the file (void should be first to catch
2455 errant 0's within the index fields). */
2456 file_ptr
->void_type
= add_aux_sym_tir (&void_type_info
,
2458 &cur_file_ptr
->thash_head
[0]);
2460 file_ptr
->int_type
= add_aux_sym_tir (&int_type_info
,
2462 &cur_file_ptr
->thash_head
[0]);
2467 /* Add a stream of random bytes to a varray. */
2470 add_bytes (varray_t
*vp
, /* virtual array to add too */
2471 char *input_ptr
, /* start of the bytes */
2472 Size_t nitems
) /* # items to move */
2480 if (vp
->objects_last_page
>= vp
->objects_per_page
)
2481 add_varray_page (vp
);
2483 ptr
= &vp
->last
->datum
->byte
[ vp
->objects_last_page
* vp
->object_size
];
2484 move_items
= vp
->objects_per_page
- vp
->objects_last_page
;
2485 if (move_items
> nitems
)
2486 move_items
= nitems
;
2488 move_bytes
= move_items
* vp
->object_size
;
2489 nitems
-= move_items
;
2491 if (move_bytes
>= 32)
2493 (void) memcpy (ptr
, input_ptr
, move_bytes
);
2494 input_ptr
+= move_bytes
;
2498 while (move_bytes
-- > 0)
2499 *ptr
++ = *input_ptr
++;
2505 /* Convert storage class to string. */
2508 sc_to_string (sc_t storage_class
)
2510 switch (storage_class
)
2512 case sc_Nil
: return "Nil,";
2513 case sc_Text
: return "Text,";
2514 case sc_Data
: return "Data,";
2515 case sc_Bss
: return "Bss,";
2516 case sc_Register
: return "Register,";
2517 case sc_Abs
: return "Abs,";
2518 case sc_Undefined
: return "Undefined,";
2519 case sc_CdbLocal
: return "CdbLocal,";
2520 case sc_Bits
: return "Bits,";
2521 case sc_CdbSystem
: return "CdbSystem,";
2522 case sc_RegImage
: return "RegImage,";
2523 case sc_Info
: return "Info,";
2524 case sc_UserStruct
: return "UserStruct,";
2525 case sc_SData
: return "SData,";
2526 case sc_SBss
: return "SBss,";
2527 case sc_RData
: return "RData,";
2528 case sc_Var
: return "Var,";
2529 case sc_Common
: return "Common,";
2530 case sc_SCommon
: return "SCommon,";
2531 case sc_VarRegister
: return "VarRegister,";
2532 case sc_Variant
: return "Variant,";
2533 case sc_SUndefined
: return "SUndefined,";
2534 case sc_Init
: return "Init,";
2535 case sc_Max
: return "Max,";
2542 /* Convert symbol type to string. */
2545 st_to_string (st_t symbol_type
)
2547 switch (symbol_type
)
2549 case st_Nil
: return "Nil,";
2550 case st_Global
: return "Global,";
2551 case st_Static
: return "Static,";
2552 case st_Param
: return "Param,";
2553 case st_Local
: return "Local,";
2554 case st_Label
: return "Label,";
2555 case st_Proc
: return "Proc,";
2556 case st_Block
: return "Block,";
2557 case st_End
: return "End,";
2558 case st_Member
: return "Member,";
2559 case st_Typedef
: return "Typedef,";
2560 case st_File
: return "File,";
2561 case st_RegReloc
: return "RegReloc,";
2562 case st_Forward
: return "Forward,";
2563 case st_StaticProc
: return "StaticProc,";
2564 case st_Constant
: return "Constant,";
2565 case st_Str
: return "String,";
2566 case st_Number
: return "Number,";
2567 case st_Expr
: return "Expr,";
2568 case st_Type
: return "Type,";
2569 case st_Max
: return "Max,";
2576 /* Read a line from standard input, and return the start of the buffer
2577 (which is grows if the line is too big). We split lines at the
2578 semi-colon, and return each logical line independently. */
2583 static int line_split_p
= 0;
2589 if (cur_line_start
== (char *) 0)
2590 { /* allocate initial page */
2591 cur_line_start
= (char *) allocate_page ();
2592 cur_line_alloc
= PAGE_SIZE
;
2599 cur_line_nbytes
= 0;
2601 for (ptr
= cur_line_start
; (ch
= getchar ()) != EOF
; *ptr
++ = ch
)
2603 if (++cur_line_nbytes
>= cur_line_alloc
-1)
2605 int num_pages
= cur_line_alloc
/ PAGE_SIZE
;
2606 char *old_buffer
= cur_line_start
;
2608 cur_line_alloc
+= PAGE_SIZE
;
2609 cur_line_start
= (char *) allocate_multiple_pages (num_pages
+1);
2610 memcpy (cur_line_start
, old_buffer
, num_pages
* PAGE_SIZE
);
2612 ptr
= cur_line_start
+ cur_line_nbytes
- 1;
2619 cur_line_ptr
= cur_line_start
;
2620 return cur_line_ptr
;
2623 else if (ch
== '\0')
2624 error ("null character found in input");
2626 else if (!comment_p
)
2629 string_p
= !string_p
;
2634 else if (ch
== ';' && !string_p
)
2639 cur_line_ptr
= cur_line_start
;
2640 return cur_line_ptr
;
2646 pfatal_with_name (input_name
);
2648 cur_line_ptr
= (char *) 0;
2653 /* Parse #.begin directives which have a label as the first argument
2654 which gives the location of the start of the block. */
2657 parse_begin (const char *start
)
2659 const char *end_p1
; /* end of label */
2661 shash_t
*hash_ptr
; /* hash pointer to lookup label */
2663 if (cur_file_ptr
== (efdr_t
*) 0)
2665 error ("#.begin directive without a preceding .file directive");
2669 if (cur_proc_ptr
== (PDR
*) 0)
2671 error ("#.begin directive without a preceding .ent directive");
2675 for (end_p1
= start
; (ch
= *end_p1
) != '\0' && !ISSPACE (ch
); end_p1
++)
2678 hash_ptr
= hash_string (start
,
2683 if (hash_ptr
== (shash_t
*) 0)
2685 error ("label %.*s not found for #.begin",
2686 (int) (end_p1
- start
), start
);
2690 if (cur_oproc_begin
== (SYMR
*) 0)
2692 error ("procedure table %.*s not found for #.begin",
2693 (int) (end_p1
- start
), start
);
2697 (void) add_local_symbol ((const char *) 0, (const char *) 0,
2699 (symint_t
) hash_ptr
->sym_ptr
->value
- cur_oproc_begin
->value
,
2704 /* Parse #.bend directives which have a label as the first argument
2705 which gives the location of the end of the block. */
2708 parse_bend (const char *start
)
2710 const char *end_p1
; /* end of label */
2712 shash_t
*hash_ptr
; /* hash pointer to lookup label */
2714 if (cur_file_ptr
== (efdr_t
*) 0)
2716 error ("#.begin directive without a preceding .file directive");
2720 if (cur_proc_ptr
== (PDR
*) 0)
2722 error ("#.bend directive without a preceding .ent directive");
2726 for (end_p1
= start
; (ch
= *end_p1
) != '\0' && !ISSPACE (ch
); end_p1
++)
2729 hash_ptr
= hash_string (start
,
2734 if (hash_ptr
== (shash_t
*) 0)
2736 error ("label %.*s not found for #.bend", (int) (end_p1
- start
), start
);
2740 if (cur_oproc_begin
== (SYMR
*) 0)
2742 error ("procedure table %.*s not found for #.bend",
2743 (int) (end_p1
- start
), start
);
2747 (void) add_local_symbol ((const char *) 0, (const char *) 0,
2749 (symint_t
) hash_ptr
->sym_ptr
->value
- cur_oproc_begin
->value
,
2754 /* Parse #.def directives, which are contain standard COFF subdirectives
2755 to describe the debugging format. These subdirectives include:
2757 .scl specify storage class
2758 .val specify a value
2759 .endef specify end of COFF directives
2760 .type specify the type
2761 .size specify the size of an array
2762 .dim specify an array dimension
2763 .tag specify a tag for a struct, union, or enum. */
2766 parse_def (const char *name_start
)
2768 const char *dir_start
; /* start of current directive*/
2769 const char *dir_end_p1
; /* end+1 of current directive*/
2770 const char *arg_start
; /* start of current argument */
2771 const char *arg_end_p1
; /* end+1 of current argument */
2772 const char *name_end_p1
; /* end+1 of label */
2773 const char *tag_start
= 0; /* start of tag name */
2774 const char *tag_end_p1
= 0; /* end+1 of tag name */
2775 sc_t storage_class
= sc_Nil
;
2776 st_t symbol_type
= st_Nil
;
2778 EXTR
*eptr
= (EXTR
*) 0; /* ext. sym equivalent to def*/
2779 int is_function
= 0; /* != 0 if function */
2781 symint_t indx
= cur_file_ptr
->void_type
;
2783 symint_t arg_number
;
2784 symint_t temp_array
[ N_TQ
];
2789 static int inside_enumeration
= 0; /* is this an enumeration? */
2792 /* Initialize the type information. */
2796 /* Search for the end of the name being defined. */
2797 /* Allow spaces and such in names for G++ templates, which produce stabs
2800 #.def SMANIP<long unsigned int>; .scl 10; .type 0x8; .size 8; .endef */
2802 for (name_end_p1
= name_start
; (ch
= *name_end_p1
) != ';' && ch
!= '\0'; name_end_p1
++)
2807 error_line
= __LINE__
;
2812 /* Parse the remaining subdirectives now. */
2813 dir_start
= name_end_p1
+1;
2816 while ((ch
= *dir_start
) == ' ' || ch
== '\t')
2821 error_line
= __LINE__
;
2827 if (dir_start
[1] == 'e'
2828 && memcmp (dir_start
, ".endef", sizeof (".endef")-1) == 0)
2831 /* Pick up the subdirective now. */
2832 for (dir_end_p1
= dir_start
+1;
2833 (ch
= *dir_end_p1
) != ' ' && ch
!= '\t';
2836 if (ch
== '\0' || ISSPACE (ch
))
2838 error_line
= __LINE__
;
2844 /* Pick up the subdirective argument now. */
2845 arg_was_number
= arg_number
= 0;
2847 arg_start
= dir_end_p1
+1;
2849 while (ch
== ' ' || ch
== '\t')
2852 if (ISDIGIT (ch
) || ch
== '-' || ch
== '+')
2855 arg_number
= strtol (arg_start
, (char **) &arg_end_p1
, 0);
2856 /* It's only a number if followed by ';' or ','. */
2857 if (arg_end_p1
!= arg_start
&& (((ch2
= *arg_end_p1
) == ';') || ch2
== ','))
2861 else if (ch
== '\0' || ISSPACE (ch
))
2863 error_line
= __LINE__
;
2868 if (!arg_was_number
)
2870 /* Allow spaces and such in names for G++ templates. */
2871 for (arg_end_p1
= arg_start
+1;
2872 (ch
= *arg_end_p1
) != ';' && ch
!= '\0';
2878 error_line
= __LINE__
;
2884 /* Classify the directives now. */
2885 len
= dir_end_p1
- dir_start
;
2886 switch (dir_start
[1])
2889 error_line
= __LINE__
;
2894 if (len
== sizeof (".dim")-1
2895 && memcmp (dir_start
, ".dim", sizeof (".dim")-1) == 0
2898 symint_t
*t_ptr
= &temp_array
[ N_TQ
-1 ];
2900 *t_ptr
= arg_number
;
2901 while (*arg_end_p1
== ',' && arg_was_number
)
2903 arg_start
= arg_end_p1
+1;
2905 while (ch
== ' ' || ch
== '\t')
2909 if (ISDIGIT (ch
) || ch
== '-' || ch
== '+')
2912 arg_number
= strtol (arg_start
, (char **) &arg_end_p1
, 0);
2913 if (arg_end_p1
!= arg_start
&& (((ch2
= *arg_end_p1
) == ';') || ch2
== ','))
2916 if (t_ptr
== &temp_array
[0])
2918 error_line
= __LINE__
;
2923 *--t_ptr
= arg_number
;
2927 /* Reverse order of dimensions. */
2928 while (t_ptr
<= &temp_array
[ N_TQ
-1 ])
2930 if (t
.num_dims
>= N_TQ
-1)
2932 error_line
= __LINE__
;
2937 t
.dimensions
[ t
.num_dims
++ ] = *t_ptr
++;
2943 error_line
= __LINE__
;
2950 if (len
== sizeof (".scl")-1
2951 && memcmp (dir_start
, ".scl", sizeof (".scl")-1) == 0
2953 && arg_number
< ((symint_t
) C_MAX
))
2955 /* If the symbol is a static or external, we have
2956 already gotten the appropriate type and class, so
2957 make sure we don't override those values. This is
2958 needed because there are some type and classes that
2959 are not in COFF, such as short data, etc. */
2960 if (symbol_type
== st_Nil
)
2962 symbol_type
= map_coff_sym_type
[arg_number
];
2963 storage_class
= map_coff_storage
[arg_number
];
2968 else if (len
== sizeof (".size")-1
2969 && memcmp (dir_start
, ".size", sizeof (".size")-1) == 0
2972 symint_t
*t_ptr
= &temp_array
[ N_TQ
-1 ];
2974 *t_ptr
= arg_number
;
2975 while (*arg_end_p1
== ',' && arg_was_number
)
2977 arg_start
= arg_end_p1
+1;
2979 while (ch
== ' ' || ch
== '\t')
2983 if (ISDIGIT (ch
) || ch
== '-' || ch
== '+')
2986 arg_number
= strtol (arg_start
, (char **) &arg_end_p1
, 0);
2987 if (arg_end_p1
!= arg_start
&& (((ch2
= *arg_end_p1
) == ';') || ch2
== ','))
2990 if (t_ptr
== &temp_array
[0])
2992 error_line
= __LINE__
;
2997 *--t_ptr
= arg_number
;
3001 /* Reverse order of sizes. */
3002 while (t_ptr
<= &temp_array
[ N_TQ
-1 ])
3004 if (t
.num_sizes
>= N_TQ
-1)
3006 error_line
= __LINE__
;
3011 t
.sizes
[ t
.num_sizes
++ ] = *t_ptr
++;
3018 error_line
= __LINE__
;
3025 if (len
== sizeof (".type")-1
3026 && memcmp (dir_start
, ".type", sizeof (".type")-1) == 0
3029 tq_t
*tq_ptr
= &t
.type_qualifiers
[0];
3031 t
.orig_type
= (coff_type_t
) (arg_number
& N_BTMASK
);
3032 t
.basic_type
= map_coff_types
[(int) t
.orig_type
];
3033 for (i
= N_TQ
-1; i
>= 0; i
--)
3035 int dt
= (arg_number
>> ((i
* N_TQ_SHIFT
) + N_BT_SHIFT
)
3038 if (dt
!= (int) DT_NON
)
3039 *tq_ptr
++ = map_coff_derived_type
[dt
];
3042 /* If this is a function, ignore it, so that we don't get
3043 two entries (one from the .ent, and one for the .def
3044 that precedes it). Save the type information so that
3045 the end block can properly add it after the begin block
3046 index. For MIPS knows what reason, we must strip off
3047 the function type at this point. */
3048 if (tq_ptr
!= &t
.type_qualifiers
[0] && tq_ptr
[-1] == tq_Proc
)
3051 tq_ptr
[-1] = tq_Nil
;
3057 else if (len
== sizeof (".tag")-1
3058 && memcmp (dir_start
, ".tag", sizeof (".tag")-1) == 0)
3060 tag_start
= arg_start
;
3061 tag_end_p1
= arg_end_p1
;
3067 error_line
= __LINE__
;
3074 if (len
== sizeof (".val")-1
3075 && memcmp (dir_start
, ".val", sizeof (".val")-1) == 0)
3080 /* If the value is not an integer value, it must be the
3081 name of a static or global item. Look up the name in
3082 the original symbol table to pick up the storage
3083 class, symbol type, etc. */
3086 shash_t
*orig_hash_ptr
; /* hash within orig sym table*/
3087 shash_t
*ext_hash_ptr
; /* hash within ext. sym table*/
3089 ext_hash_ptr
= hash_string (arg_start
,
3090 arg_end_p1
- arg_start
,
3094 if (ext_hash_ptr
!= (shash_t
*) 0
3095 && ext_hash_ptr
->esym_ptr
!= (EXTR
*) 0)
3096 eptr
= ext_hash_ptr
->esym_ptr
;
3098 orig_hash_ptr
= hash_string (arg_start
,
3099 arg_end_p1
- arg_start
,
3103 if ((orig_hash_ptr
== (shash_t
*) 0
3104 || orig_hash_ptr
->sym_ptr
== (SYMR
*) 0)
3105 && eptr
== (EXTR
*) 0)
3107 fprintf (stderr
, "warning, %.*s not found in original or external symbol tables, value defaults to 0\n",
3108 (int) (arg_end_p1
- arg_start
),
3114 SYMR
*ptr
= (orig_hash_ptr
!= (shash_t
*) 0
3115 && orig_hash_ptr
->sym_ptr
!= (SYMR
*) 0)
3116 ? orig_hash_ptr
->sym_ptr
3119 symbol_type
= (st_t
) ptr
->st
;
3120 storage_class
= (sc_t
) ptr
->sc
;
3128 error_line
= __LINE__
;
3134 /* Set up to find next directive. */
3135 dir_start
= arg_end_p1
+ 1;
3139 if (storage_class
== sc_Bits
)
3149 int num_real_sizes
= t
.num_sizes
- t
.extra_sizes
;
3150 int diff
= t
.num_dims
- num_real_sizes
;
3151 int i
= t
.num_dims
- 1;
3154 if (num_real_sizes
!= 1 || diff
< 0)
3156 error_line
= __LINE__
;
3161 /* If this is an array, make sure the same number of dimensions
3162 and sizes were passed, creating extra sizes for multiply
3163 dimensioned arrays if not passed. */
3167 for (j
= ARRAY_SIZE (t
.sizes
) - 1; j
>= 0; j
--)
3168 t
.sizes
[ j
] = ((j
-diff
) >= 0) ? t
.sizes
[ j
-diff
] : 0;
3170 t
.num_sizes
= i
+ 1;
3171 for ( i
--; i
>= 0; i
-- )
3173 if (t
.dimensions
[ i
+1 ])
3174 t
.sizes
[ i
] = t
.sizes
[ i
+1 ] / t
.dimensions
[ i
+1 ];
3176 t
.sizes
[ i
] = t
.sizes
[ i
+1 ];
3181 /* Except for enumeration members & begin/ending of scopes, put the
3182 type word in the aux. symbol table. */
3184 if (symbol_type
== st_Block
|| symbol_type
== st_End
)
3187 else if (inside_enumeration
)
3188 indx
= cur_file_ptr
->void_type
;
3192 if (t
.basic_type
== bt_Struct
3193 || t
.basic_type
== bt_Union
3194 || t
.basic_type
== bt_Enum
)
3196 if (tag_start
== (char *) 0)
3198 error ("no tag specified for %.*s",
3199 (int) (name_end_p1
- name_start
),
3204 t
.tag_ptr
= get_tag (tag_start
, tag_end_p1
, (symint_t
) indexNil
,
3210 last_func_type_info
= t
;
3211 last_func_eptr
= eptr
;
3215 indx
= add_aux_sym_tir (&t
,
3217 &cur_file_ptr
->thash_head
[0]);
3221 /* If this is an external or static symbol, update the appropriate
3224 if (eptr
!= (EXTR
*) 0
3225 && (eptr
->asym
.index
== indexNil
|| cur_proc_ptr
== (PDR
*) 0))
3227 eptr
->ifd
= cur_file_ptr
->file_index
;
3228 eptr
->asym
.index
= indx
;
3232 /* Do any last minute adjustments that are necessary. */
3233 switch (symbol_type
)
3239 /* For the beginning of structs, unions, and enumerations, the
3240 size info needs to be passed in the value field. */
3243 if (t
.num_sizes
- t
.num_dims
- t
.extra_sizes
!= 1)
3245 error_line
= __LINE__
;
3253 inside_enumeration
= (t
.orig_type
== T_ENUM
);
3257 /* For the end of structs, unions, and enumerations, omit the
3258 name which is always ".eos". This needs to be done last, so
3259 that any error reporting above gives the correct name. */
3262 name_start
= name_end_p1
= 0;
3263 value
= inside_enumeration
= 0;
3267 /* Members of structures and unions that aren't bitfields, need
3268 to adjust the value from a byte offset to a bit offset.
3269 Members of enumerations do not have the value adjusted, and
3270 can be distinguished by indx == indexNil. For enumerations,
3271 update the maximum enumeration value. */
3274 if (!t
.bitfield
&& !inside_enumeration
)
3281 /* Add the symbol, except for global symbols outside of functions,
3282 for which the external symbol table is fine enough. */
3284 if (eptr
== (EXTR
*) 0
3285 || eptr
->asym
.st
== (int) st_Nil
3286 || cur_proc_ptr
!= (PDR
*) 0)
3288 symint_t isym
= add_local_symbol (name_start
, name_end_p1
,
3289 symbol_type
, storage_class
,
3293 /* Deal with struct, union, and enum tags. */
3294 if (symbol_type
== st_Block
)
3296 /* Create or update the tag information. */
3297 tag_t
*tag_ptr
= get_tag (name_start
,
3302 /* If there are any forward references, fill in the appropriate
3303 file and symbol indexes. */
3305 symint_t file_index
= cur_file_ptr
->file_index
;
3306 forward_t
*f_next
= tag_ptr
->forward_ref
;
3309 while (f_next
!= (forward_t
*) 0)
3312 f_next
= f_next
->next
;
3314 f_cur
->ifd_ptr
->isym
= file_index
;
3315 f_cur
->index_ptr
->rndx
.index
= isym
;
3317 free_forward (f_cur
);
3320 tag_ptr
->forward_ref
= (forward_t
*) 0;
3327 /* Error return, issue message. */
3330 error ("compiler error, badly formed #.def (internal line # = %d)", error_line
);
3332 error ("compiler error, badly formed #.def");
3338 /* Parse .end directives. */
3341 parse_end (const char *start
)
3343 const char *start_func
, *end_func_p1
;
3348 if (cur_file_ptr
== (efdr_t
*) 0)
3350 error (".end directive without a preceding .file directive");
3354 if (cur_proc_ptr
== (PDR
*) 0)
3356 error (".end directive without a preceding .ent directive");
3360 /* Get the function name, skipping whitespace. */
3361 for (start_func
= start
; ISSPACE ((unsigned char)*start_func
); start_func
++)
3365 if (!IS_ASM_IDENT (ch
))
3367 error (".end directive has no name");
3371 for (end_func_p1
= start_func
; IS_ASM_IDENT (ch
); ch
= *++end_func_p1
)
3375 /* Get the value field for creating the end from the original object
3376 file (which we find by locating the procedure start, and using the
3377 pointer to the end+1 block and backing up. The index points to a
3378 two word aux. symbol, whose first word is the index of the end
3379 symbol, and the second word is the type of the function return
3382 orig_fdr
= cur_file_ptr
->orig_fdr
;
3384 if (orig_fdr
!= (FDR
*) 0 && cur_oproc_end
!= (SYMR
*) 0)
3385 value
= cur_oproc_end
->value
;
3388 error ("cannot find .end block for %.*s",
3389 (int) (end_func_p1
- start_func
), start_func
);
3391 (void) add_local_symbol (start_func
, end_func_p1
,
3396 cur_proc_ptr
= cur_oproc_ptr
= (PDR
*) 0;
3400 /* Parse .ent directives. */
3403 parse_ent (const char *start
)
3405 const char *start_func
, *end_func_p1
;
3408 if (cur_file_ptr
== (efdr_t
*) 0)
3410 error (".ent directive without a preceding .file directive");
3414 if (cur_proc_ptr
!= (PDR
*) 0)
3416 error ("second .ent directive found before .end directive");
3420 for (start_func
= start
; ISSPACE ((unsigned char)*start_func
); start_func
++)
3424 if (!IS_ASM_IDENT (ch
))
3426 error (".ent directive has no name");
3430 for (end_func_p1
= start_func
; IS_ASM_IDENT (ch
); ch
= *++end_func_p1
)
3433 (void) add_procedure (start_func
, end_func_p1
);
3437 /* Parse .file directives. */
3440 parse_file (const char *start
)
3443 char *start_name
, *end_name_p1
;
3445 (void) strtol (start
, &p
, 0);
3447 || (start_name
= strchr (p
, '"')) == (char *) 0
3448 || (end_name_p1
= strrchr (++start_name
, '"')) == (char *) 0)
3450 error ("invalid .file directive");
3454 if (cur_proc_ptr
!= (PDR
*) 0)
3456 error ("no way to handle .file within .ent/.end section");
3460 add_file (start_name
, end_name_p1
);
3464 /* Make sure the @stabs symbol is emitted. */
3467 mark_stabs (const char *start ATTRIBUTE_UNUSED
)
3471 /* Add a dummy @stabs symbol. */
3473 (void) add_local_symbol (stabs_symbol
,
3474 stabs_symbol
+ sizeof (stabs_symbol
),
3475 (st_t
) stNil
, (sc_t
) scInfo
, -1,
3476 MIPS_MARK_STAB (0));
3482 /* Parse .stabs directives.
3484 .stabs directives have five fields:
3485 "string" a string, encoding the type information.
3486 code a numeric code, defined in <stab.h>
3488 0 a zero or line number
3489 value a numeric value or an address.
3491 If the value is relocatable, we transform this into:
3492 iss points as an index into string space
3493 value value from lookup of the name
3494 st st from lookup of the name
3495 sc sc from lookup of the name
3496 index code|CODE_MASK
3498 If the value is not relocatable, we transform this into:
3499 iss points as an index into string space
3503 index code|CODE_MASK
3505 .stabn directives have four fields (string is null):
3506 code a numeric code, defined in <stab.h>
3508 0 a zero or a line number
3509 value a numeric value or an address. */
3512 parse_stabs_common (const char *string_start
, /* start of string or NULL */
3513 const char *string_end
, /* end+1 of string or NULL */
3514 const char *rest
) /* rest of the directive. */
3516 efdr_t
*save_file_ptr
= cur_file_ptr
;
3524 if (stabs_seen
== 0)
3527 /* Read code from stabs. */
3528 if (!ISDIGIT (*rest
))
3530 error ("invalid .stabs/.stabn directive, code is non-numeric");
3534 code
= strtol (rest
, &p
, 0);
3536 /* Line number stabs are handled differently, since they have two values,
3537 the line number and the address of the label. We use the index field
3538 (aka code) to hold the line number, and the value field to hold the
3539 address. The symbol type is st_Label, which should be different from
3540 the other stabs, so that gdb can recognize it. */
3542 if (code
== (int) N_SLINE
)
3544 SYMR
*sym_ptr
, dummy_symr
;
3548 if (p
[0] != ',' || p
[1] != '0' || p
[2] != ',' || !ISDIGIT (p
[3]))
3550 error ("invalid line number .stabs/.stabn directive");
3554 code
= strtol (p
+3, &p
, 0);
3556 if (p
[-1] != ',' || ISDIGIT (ch
) || !IS_ASM_IDENT (ch
))
3558 error ("invalid line number .stabs/.stabn directive");
3562 dummy_symr
.index
= code
;
3563 if (dummy_symr
.index
!= code
)
3565 error ("line number (%lu) for .stabs/.stabn directive cannot fit in index field (20 bits)",
3571 shash_ptr
= hash_string (p
,
3576 if (shash_ptr
== (shash_t
*) 0
3577 || (sym_ptr
= shash_ptr
->sym_ptr
) == (SYMR
*) 0)
3579 error ("invalid .stabs/.stabn directive, value not found");
3583 if ((st_t
) sym_ptr
->st
!= st_Label
)
3585 error ("invalid line number .stabs/.stabn directive");
3590 sc
= (sc_t
) sym_ptr
->sc
;
3591 value
= sym_ptr
->value
;
3595 /* Skip ,<num>,<num>, */
3598 for (; ISDIGIT (*p
); p
++)
3602 for (; ISDIGIT (*p
); p
++)
3607 if (!IS_ASM_IDENT (ch
) && ch
!= '-')
3610 error ("invalid .stabs/.stabn directive, bad character");
3614 if (ISDIGIT (ch
) || ch
== '-')
3618 value
= strtol (p
, &p
, 0);
3621 error ("invalid .stabs/.stabn directive, stuff after numeric value");
3625 else if (!IS_ASM_IDENT (ch
))
3627 error ("invalid .stabs/.stabn directive, bad character");
3634 const char *start
, *end_p1
;
3637 if ((end_p1
= strchr (start
, '+')) == (char *) 0)
3639 if ((end_p1
= strchr (start
, '-')) == (char *) 0)
3640 end_p1
= start
+ strlen (start
) - 1;
3643 shash_ptr
= hash_string (start
,
3648 if (shash_ptr
== (shash_t
*) 0
3649 || (sym_ptr
= shash_ptr
->sym_ptr
) == (SYMR
*) 0)
3651 shash_ptr
= hash_string (start
,
3656 if (shash_ptr
== (shash_t
*) 0
3657 || shash_ptr
->esym_ptr
== (EXTR
*) 0)
3659 error ("invalid .stabs/.stabn directive, value not found");
3663 sym_ptr
= &(shash_ptr
->esym_ptr
->asym
);
3666 /* Traditionally, N_LBRAC and N_RBRAC are *not* relocated. */
3667 if (code
== (int) N_LBRAC
|| code
== (int) N_RBRAC
)
3674 sc
= (sc_t
) sym_ptr
->sc
;
3675 st
= (st_t
) sym_ptr
->st
;
3677 value
= sym_ptr
->value
;
3682 if (((!ISDIGIT (*end_p1
)) && (*end_p1
!= '-'))
3683 || ((ch
!= '+') && (ch
!= '-')))
3685 error ("invalid .stabs/.stabn directive, badly formed value");
3689 value
+= strtol (end_p1
, &p
, 0);
3691 value
-= strtol (end_p1
, &p
, 0);
3695 error ("invalid .stabs/.stabn directive, stuff after numeric value");
3700 code
= MIPS_MARK_STAB (code
);
3703 (void) add_local_symbol (string_start
, string_end
, st
, sc
, value
, code
);
3704 /* Restore normal file type. */
3705 cur_file_ptr
= save_file_ptr
;
3710 parse_stabs (const char *start
)
3712 const char *end
= strchr (start
+1, '"');
3714 if (*start
!= '"' || end
== (const char *) 0 || end
[1] != ',')
3716 error ("invalid .stabs directive, no string");
3720 parse_stabs_common (start
+1, end
, end
+2);
3725 parse_stabn (const char *start
)
3727 parse_stabs_common ((const char *) 0, (const char *) 0, start
);
3731 /* Parse the input file, and write the lines to the output file
3744 fprintf (stderr
, "\tinput\n");
3746 /* Add a dummy scope block around the entire compilation unit for
3747 structures defined outside of blocks. */
3748 ptag_head
= allocate_thead ();
3749 ptag_head
->first_tag
= 0;
3750 ptag_head
->prev
= cur_tag_head
;
3751 cur_tag_head
= ptag_head
;
3753 while ((p
= read_line ()) != (char *) 0)
3755 /* Skip leading blanks. */
3756 while (ISSPACE ((unsigned char)*p
))
3759 /* See if it's a directive we handle. If so, dispatch handler. */
3760 for (i
= 0; i
< ARRAY_SIZE (pseudo_ops
); i
++)
3761 if (memcmp (p
, pseudo_ops
[i
].name
, pseudo_ops
[i
].len
) == 0
3762 && ISSPACE ((unsigned char)(p
[pseudo_ops
[i
].len
])))
3764 p
+= pseudo_ops
[i
].len
; /* skip to first argument */
3765 while (ISSPACE ((unsigned char)*p
))
3768 (*pseudo_ops
[i
].func
)( p
);
3773 /* Process any tags at global level. */
3774 ptag_head
= cur_tag_head
;
3775 cur_tag_head
= ptag_head
->prev
;
3777 for (ptag
= ptag_head
->first_tag
;
3778 ptag
!= (tag_t
*) 0;
3781 if (ptag
->forward_ref
!= (forward_t
*) 0)
3782 add_unknown_tag (ptag
);
3784 ptag_next
= ptag
->same_block
;
3785 ptag
->hash_ptr
->tag_ptr
= ptag
->same_name
;
3789 free_thead (ptag_head
);
3794 /* Update the global headers with the final offsets in preparation
3795 to write out the .T file. */
3798 update_headers (void)
3803 /* Set up the symbolic header. */
3804 file_offset
= sizeof (symbolic_header
) + orig_file_header
.f_symptr
;
3805 symbolic_header
.magic
= orig_sym_hdr
.magic
;
3806 symbolic_header
.vstamp
= orig_sym_hdr
.vstamp
;
3808 /* Set up global counts. */
3809 symbolic_header
.issExtMax
= ext_strings
.num_allocated
;
3810 symbolic_header
.idnMax
= dense_num
.num_allocated
;
3811 symbolic_header
.ifdMax
= file_desc
.num_allocated
;
3812 symbolic_header
.iextMax
= ext_symbols
.num_allocated
;
3813 symbolic_header
.ilineMax
= orig_sym_hdr
.ilineMax
;
3814 symbolic_header
.ioptMax
= orig_sym_hdr
.ioptMax
;
3815 symbolic_header
.cbLine
= orig_sym_hdr
.cbLine
;
3816 symbolic_header
.crfd
= orig_sym_hdr
.crfd
;
3819 /* Loop through each file, figuring out how many local syms,
3820 line numbers, etc. there are. Also, put out end symbol
3821 for the filename. */
3823 for (file_ptr
= first_file
;
3824 file_ptr
!= (efdr_t
*) 0;
3825 file_ptr
= file_ptr
->next_file
)
3830 FDR
*fd_ptr
= file_ptr
->orig_fdr
;
3832 cur_file_ptr
= file_ptr
;
3834 /* Copy st_Static symbols from the original local symbol table if
3835 they did not get added to the new local symbol table.
3836 This happens with stabs-in-ecoff or if the source file is
3837 compiled without debugging. */
3838 sym_start
= ORIG_LSYMS (fd_ptr
->isymBase
);
3839 sym_end_p1
= sym_start
+ fd_ptr
->csym
;
3840 for (sym
= sym_start
; sym
< sym_end_p1
; sym
++)
3842 if ((st_t
) sym
->st
== st_Static
)
3844 char *str
= ORIG_LSTRS (fd_ptr
->issBase
+ sym
->iss
);
3845 Size_t len
= strlen (str
);
3848 /* Ignore internal labels. */
3849 if (str
[0] == '$' && str
[1] == 'L')
3851 hash_ptr
= hash_string (str
,
3853 &file_ptr
->shash_head
[0],
3855 if (hash_ptr
== (shash_t
*) 0)
3857 (void) add_local_symbol (str
, str
+ len
,
3858 (st_t
) sym
->st
, (sc_t
) sym
->sc
,
3859 (symint_t
) sym
->value
,
3860 (symint_t
) indexNil
);
3864 (void) add_local_symbol ((const char *) 0, (const char *) 0,
3869 file_ptr
->fdr
.cpd
= file_ptr
->procs
.num_allocated
;
3870 file_ptr
->fdr
.ipdFirst
= symbolic_header
.ipdMax
;
3871 symbolic_header
.ipdMax
+= file_ptr
->fdr
.cpd
;
3873 file_ptr
->fdr
.csym
= file_ptr
->symbols
.num_allocated
;
3874 file_ptr
->fdr
.isymBase
= symbolic_header
.isymMax
;
3875 symbolic_header
.isymMax
+= file_ptr
->fdr
.csym
;
3877 file_ptr
->fdr
.caux
= file_ptr
->aux_syms
.num_allocated
;
3878 file_ptr
->fdr
.iauxBase
= symbolic_header
.iauxMax
;
3879 symbolic_header
.iauxMax
+= file_ptr
->fdr
.caux
;
3881 file_ptr
->fdr
.cbSs
= file_ptr
->strings
.num_allocated
;
3882 file_ptr
->fdr
.issBase
= symbolic_header
.issMax
;
3883 symbolic_header
.issMax
+= file_ptr
->fdr
.cbSs
;
3886 #ifndef ALIGN_SYMTABLE_OFFSET
3887 #define ALIGN_SYMTABLE_OFFSET(OFFSET) (OFFSET)
3890 file_offset
= ALIGN_SYMTABLE_OFFSET (file_offset
);
3891 i
= WORD_ALIGN (symbolic_header
.cbLine
); /* line numbers */
3894 symbolic_header
.cbLineOffset
= file_offset
;
3896 file_offset
= ALIGN_SYMTABLE_OFFSET (file_offset
);
3899 i
= symbolic_header
.ioptMax
; /* optimization symbols */
3902 symbolic_header
.cbOptOffset
= file_offset
;
3903 file_offset
+= i
* sizeof (OPTR
);
3904 file_offset
= ALIGN_SYMTABLE_OFFSET (file_offset
);
3907 i
= symbolic_header
.idnMax
; /* dense numbers */
3910 symbolic_header
.cbDnOffset
= file_offset
;
3911 file_offset
+= i
* sizeof (DNR
);
3912 file_offset
= ALIGN_SYMTABLE_OFFSET (file_offset
);
3915 i
= symbolic_header
.ipdMax
; /* procedure tables */
3918 symbolic_header
.cbPdOffset
= file_offset
;
3919 file_offset
+= i
* sizeof (PDR
);
3920 file_offset
= ALIGN_SYMTABLE_OFFSET (file_offset
);
3923 i
= symbolic_header
.isymMax
; /* local symbols */
3926 symbolic_header
.cbSymOffset
= file_offset
;
3927 file_offset
+= i
* sizeof (SYMR
);
3928 file_offset
= ALIGN_SYMTABLE_OFFSET (file_offset
);
3931 i
= symbolic_header
.iauxMax
; /* aux syms. */
3934 symbolic_header
.cbAuxOffset
= file_offset
;
3935 file_offset
+= i
* sizeof (TIR
);
3936 file_offset
= ALIGN_SYMTABLE_OFFSET (file_offset
);
3939 i
= WORD_ALIGN (symbolic_header
.issMax
); /* local strings */
3942 symbolic_header
.cbSsOffset
= file_offset
;
3944 file_offset
= ALIGN_SYMTABLE_OFFSET (file_offset
);
3947 i
= WORD_ALIGN (symbolic_header
.issExtMax
); /* external strings */
3950 symbolic_header
.cbSsExtOffset
= file_offset
;
3952 file_offset
= ALIGN_SYMTABLE_OFFSET (file_offset
);
3955 i
= symbolic_header
.ifdMax
; /* file tables */
3958 symbolic_header
.cbFdOffset
= file_offset
;
3959 file_offset
+= i
* sizeof (FDR
);
3960 file_offset
= ALIGN_SYMTABLE_OFFSET (file_offset
);
3963 i
= symbolic_header
.crfd
; /* relative file descriptors */
3966 symbolic_header
.cbRfdOffset
= file_offset
;
3967 file_offset
+= i
* sizeof (symint_t
);
3968 file_offset
= ALIGN_SYMTABLE_OFFSET (file_offset
);
3971 i
= symbolic_header
.iextMax
; /* external symbols */
3974 symbolic_header
.cbExtOffset
= file_offset
;
3975 file_offset
+= i
* sizeof (EXTR
);
3976 file_offset
= ALIGN_SYMTABLE_OFFSET (file_offset
);
3981 /* Write out a varray at a given location. */
3984 write_varray (varray_t
*vp
, /* virtual array */
3985 off_t offset
, /* offset to write varray to */
3986 const char *str
) /* string to print out when tracing */
3988 int num_write
, sys_write
;
3991 if (vp
->num_allocated
== 0)
3995 fprintf (stderr
, "\twarray\tvp = " HOST_PTR_PRINTF
3996 ", offset = %7lu, size = %7lu, %s\n",
3997 (void *) vp
, (unsigned long) offset
,
3998 vp
->num_allocated
* vp
->object_size
, str
);
4000 if (file_offset
!= (unsigned long) offset
4001 && fseek (object_stream
, (long) offset
, SEEK_SET
) < 0)
4002 pfatal_with_name (object_name
);
4004 for (ptr
= vp
->first
; ptr
!= (vlinks_t
*) 0; ptr
= ptr
->next
)
4006 num_write
= (ptr
->next
== (vlinks_t
*) 0)
4007 ? vp
->objects_last_page
* vp
->object_size
4008 : vp
->objects_per_page
* vp
->object_size
;
4010 sys_write
= fwrite (ptr
->datum
, 1, num_write
, object_stream
);
4012 pfatal_with_name (object_name
);
4014 else if (sys_write
!= num_write
)
4015 fatal ("wrote %d bytes to %s, system returned %d",
4020 file_offset
+= num_write
;
4025 /* Write out the symbol table in the object file. */
4035 fprintf (stderr
, "\n\twrite\tvp = " HOST_PTR_PRINTF
4036 ", offset = %7u, size = %7lu, %s\n",
4037 (void *) &symbolic_header
, 0,
4038 (unsigned long) sizeof (symbolic_header
), "symbolic header");
4040 sys_write
= fwrite (&symbolic_header
,
4042 sizeof (symbolic_header
),
4046 pfatal_with_name (object_name
);
4048 else if (sys_write
!= sizeof (symbolic_header
))
4049 fatal ("wrote %d bytes to %s, system returned %d",
4050 (int) sizeof (symbolic_header
),
4055 file_offset
= sizeof (symbolic_header
) + orig_file_header
.f_symptr
;
4057 if (symbolic_header
.cbLine
> 0) /* line numbers */
4061 if (file_offset
!= (unsigned long) symbolic_header
.cbLineOffset
4062 && fseek (object_stream
, symbolic_header
.cbLineOffset
, SEEK_SET
) != 0)
4063 pfatal_with_name (object_name
);
4066 fprintf (stderr
, "\twrite\tvp = " HOST_PTR_PRINTF
4067 ", offset = %7lu, size = %7lu, %s\n",
4068 (void *) &orig_linenum
, (long) symbolic_header
.cbLineOffset
,
4069 (long) symbolic_header
.cbLine
, "Line numbers");
4071 sys_write
= fwrite (orig_linenum
,
4073 symbolic_header
.cbLine
,
4077 pfatal_with_name (object_name
);
4079 else if (sys_write
!= symbolic_header
.cbLine
)
4080 fatal ("wrote %ld bytes to %s, system returned %ld",
4081 (long) symbolic_header
.cbLine
,
4085 file_offset
= symbolic_header
.cbLineOffset
+ symbolic_header
.cbLine
;
4088 if (symbolic_header
.ioptMax
> 0) /* optimization symbols */
4091 long num_write
= symbolic_header
.ioptMax
* sizeof (OPTR
);
4093 if (file_offset
!= (unsigned long) symbolic_header
.cbOptOffset
4094 && fseek (object_stream
, symbolic_header
.cbOptOffset
, SEEK_SET
) != 0)
4095 pfatal_with_name (object_name
);
4098 fprintf (stderr
, "\twrite\tvp = " HOST_PTR_PRINTF
4099 ", offset = %7lu, size = %7lu, %s\n",
4100 (void *) &orig_opt_syms
, (long) symbolic_header
.cbOptOffset
,
4101 num_write
, "Optimizer symbols");
4103 sys_write
= fwrite (orig_opt_syms
,
4109 pfatal_with_name (object_name
);
4111 else if (sys_write
!= num_write
)
4112 fatal ("wrote %ld bytes to %s, system returned %ld",
4117 file_offset
= symbolic_header
.cbOptOffset
+ num_write
;
4120 if (symbolic_header
.idnMax
> 0) /* dense numbers */
4121 write_varray (&dense_num
, (off_t
) symbolic_header
.cbDnOffset
, "Dense numbers");
4123 if (symbolic_header
.ipdMax
> 0) /* procedure tables */
4125 offset
= symbolic_header
.cbPdOffset
;
4126 for (file_ptr
= first_file
;
4127 file_ptr
!= (efdr_t
*) 0;
4128 file_ptr
= file_ptr
->next_file
)
4130 write_varray (&file_ptr
->procs
, offset
, "Procedure tables");
4131 offset
= file_offset
;
4135 if (symbolic_header
.isymMax
> 0) /* local symbols */
4137 offset
= symbolic_header
.cbSymOffset
;
4138 for (file_ptr
= first_file
;
4139 file_ptr
!= (efdr_t
*) 0;
4140 file_ptr
= file_ptr
->next_file
)
4142 write_varray (&file_ptr
->symbols
, offset
, "Local symbols");
4143 offset
= file_offset
;
4147 if (symbolic_header
.iauxMax
> 0) /* aux symbols */
4149 offset
= symbolic_header
.cbAuxOffset
;
4150 for (file_ptr
= first_file
;
4151 file_ptr
!= (efdr_t
*) 0;
4152 file_ptr
= file_ptr
->next_file
)
4154 write_varray (&file_ptr
->aux_syms
, offset
, "Aux. symbols");
4155 offset
= file_offset
;
4159 if (symbolic_header
.issMax
> 0) /* local strings */
4161 offset
= symbolic_header
.cbSsOffset
;
4162 for (file_ptr
= first_file
;
4163 file_ptr
!= (efdr_t
*) 0;
4164 file_ptr
= file_ptr
->next_file
)
4166 write_varray (&file_ptr
->strings
, offset
, "Local strings");
4167 offset
= file_offset
;
4171 if (symbolic_header
.issExtMax
> 0) /* external strings */
4172 write_varray (&ext_strings
, symbolic_header
.cbSsExtOffset
, "External strings");
4174 if (symbolic_header
.ifdMax
> 0) /* file tables */
4176 offset
= symbolic_header
.cbFdOffset
;
4177 if (file_offset
!= (unsigned long) offset
4178 && fseek (object_stream
, (long) offset
, SEEK_SET
) < 0)
4179 pfatal_with_name (object_name
);
4181 file_offset
= offset
;
4182 for (file_ptr
= first_file
;
4183 file_ptr
!= (efdr_t
*) 0;
4184 file_ptr
= file_ptr
->next_file
)
4187 fprintf (stderr
, "\twrite\tvp = " HOST_PTR_PRINTF
4188 ", offset = %7lu, size = %7lu, %s\n",
4189 (void *) &file_ptr
->fdr
, file_offset
,
4190 (unsigned long) sizeof (FDR
), "File header");
4192 sys_write
= fwrite (&file_ptr
->fdr
,
4198 pfatal_with_name (object_name
);
4200 else if (sys_write
!= sizeof (FDR
))
4201 fatal ("wrote %d bytes to %s, system returned %d",
4206 file_offset
= offset
+= sizeof (FDR
);
4210 if (symbolic_header
.crfd
> 0) /* relative file descriptors */
4213 symint_t num_write
= symbolic_header
.crfd
* sizeof (symint_t
);
4215 if (file_offset
!= (unsigned long) symbolic_header
.cbRfdOffset
4216 && fseek (object_stream
, symbolic_header
.cbRfdOffset
, SEEK_SET
) != 0)
4217 pfatal_with_name (object_name
);
4220 fprintf (stderr
, "\twrite\tvp = " HOST_PTR_PRINTF
4221 ", offset = %7lu, size = %7lu, %s\n",
4222 (void *) &orig_rfds
, (long) symbolic_header
.cbRfdOffset
,
4223 num_write
, "Relative file descriptors");
4225 sys_write
= fwrite (orig_rfds
,
4231 pfatal_with_name (object_name
);
4233 else if (sys_write
!= (long) num_write
)
4234 fatal ("wrote %lu bytes to %s, system returned %ld",
4239 file_offset
= symbolic_header
.cbRfdOffset
+ num_write
;
4242 if (symbolic_header
.issExtMax
> 0) /* external symbols */
4243 write_varray (&ext_symbols
, (off_t
) symbolic_header
.cbExtOffset
, "External symbols");
4245 if (fclose (object_stream
) != 0)
4246 pfatal_with_name (object_name
);
4250 /* Read some bytes at a specified location, and return a pointer. */
4253 read_seek (Size_t size
, /* # bytes to read */
4254 off_t offset
, /* offset to read at */
4255 const char *str
) /* name for tracing */
4260 if (size
== 0) /* nothing to read */
4261 return (page_t
*) 0;
4265 "\trseek\tsize = %7lu, offset = %7lu, currently at %7lu, %s\n",
4266 (unsigned long) size
, (unsigned long) offset
, file_offset
, str
);
4268 #ifndef MALLOC_CHECK
4269 ptr
= allocate_multiple_pages ((size
+ PAGE_USIZE
- 1) / PAGE_USIZE
);
4271 ptr
= xcalloc (1, size
);
4274 /* If we need to seek, and the distance is nearby, just do some reads,
4275 to speed things up. */
4276 if (file_offset
!= (unsigned long) offset
)
4278 symint_t difference
= offset
- file_offset
;
4282 char small_buffer
[8];
4284 sys_read
= fread (small_buffer
, 1, difference
, obj_in_stream
);
4286 pfatal_with_name (obj_in_name
);
4288 if ((symint_t
) sys_read
!= difference
)
4289 fatal ("wanted to read %lu bytes from %s, system returned %ld",
4290 (unsigned long) size
,
4294 else if (fseek (obj_in_stream
, offset
, SEEK_SET
) < 0)
4295 pfatal_with_name (obj_in_name
);
4298 sys_read
= fread (ptr
, 1, size
, obj_in_stream
);
4300 pfatal_with_name (obj_in_name
);
4302 if (sys_read
!= (long) size
)
4303 fatal ("wanted to read %lu bytes from %s, system returned %ld",
4304 (unsigned long) size
,
4308 file_offset
= offset
+ size
;
4310 if (file_offset
> max_file_offset
)
4311 max_file_offset
= file_offset
;
4317 /* Read the existing object file (and copy to the output object file
4318 if it is different from the input object file), and remove the old
4324 char buffer
[ PAGE_SIZE
];
4331 int *remap_file_number
;
4332 struct stat stat_buf
;
4335 fprintf (stderr
, "\tcopy\n");
4337 if (fstat (fileno (obj_in_stream
), &stat_buf
) != 0
4338 || fseek (obj_in_stream
, 0L, SEEK_SET
) != 0)
4339 pfatal_with_name (obj_in_name
);
4341 sys_read
= fread (&orig_file_header
,
4343 sizeof (struct filehdr
),
4347 pfatal_with_name (obj_in_name
);
4349 else if (sys_read
== 0 && feof (obj_in_stream
))
4350 return; /* create a .T file sans file header */
4352 else if (sys_read
< (int) sizeof (struct filehdr
))
4353 fatal ("wanted to read %d bytes from %s, system returned %d",
4354 (int) sizeof (struct filehdr
),
4359 if (orig_file_header
.f_nsyms
!= sizeof (HDRR
))
4360 fatal ("%s symbolic header wrong size (%ld bytes, should be %ld)",
4361 input_name
, (long) orig_file_header
.f_nsyms
, (long) sizeof (HDRR
));
4364 /* Read in the current symbolic header. */
4365 if (fseek (obj_in_stream
, (long) orig_file_header
.f_symptr
, SEEK_SET
) != 0)
4366 pfatal_with_name (input_name
);
4368 sys_read
= fread (&orig_sym_hdr
,
4370 sizeof (orig_sym_hdr
),
4374 pfatal_with_name (object_name
);
4376 else if (sys_read
< (int) sizeof (struct filehdr
))
4377 fatal ("wanted to read %d bytes from %s, system returned %d",
4378 (int) sizeof (struct filehdr
),
4383 /* Read in each of the sections if they exist in the object file.
4384 We read things in the order the mips assembler creates the
4385 sections, so in theory no extra seeks are done.
4387 For simplicity sake, round each read up to a page boundary,
4388 we may want to revisit this later.... */
4390 file_offset
= orig_file_header
.f_symptr
+ sizeof (struct filehdr
);
4392 if (orig_sym_hdr
.cbLine
> 0) /* line numbers */
4393 orig_linenum
= (char *) read_seek (orig_sym_hdr
.cbLine
,
4394 orig_sym_hdr
.cbLineOffset
,
4397 if (orig_sym_hdr
.ipdMax
> 0) /* procedure tables */
4398 orig_procs
= (PDR
*) read_seek (orig_sym_hdr
.ipdMax
* sizeof (PDR
),
4399 orig_sym_hdr
.cbPdOffset
,
4400 "Procedure tables");
4402 if (orig_sym_hdr
.isymMax
> 0) /* local symbols */
4403 orig_local_syms
= (SYMR
*) read_seek (orig_sym_hdr
.isymMax
* sizeof (SYMR
),
4404 orig_sym_hdr
.cbSymOffset
,
4407 if (orig_sym_hdr
.iauxMax
> 0) /* aux symbols */
4408 orig_aux_syms
= (AUXU
*) read_seek (orig_sym_hdr
.iauxMax
* sizeof (AUXU
),
4409 orig_sym_hdr
.cbAuxOffset
,
4412 if (orig_sym_hdr
.issMax
> 0) /* local strings */
4413 orig_local_strs
= (char *) read_seek (orig_sym_hdr
.issMax
,
4414 orig_sym_hdr
.cbSsOffset
,
4417 if (orig_sym_hdr
.issExtMax
> 0) /* external strings */
4418 orig_ext_strs
= (char *) read_seek (orig_sym_hdr
.issExtMax
,
4419 orig_sym_hdr
.cbSsExtOffset
,
4420 "External strings");
4422 if (orig_sym_hdr
.ifdMax
> 0) /* file tables */
4423 orig_files
= (FDR
*) read_seek (orig_sym_hdr
.ifdMax
* sizeof (FDR
),
4424 orig_sym_hdr
.cbFdOffset
,
4427 if (orig_sym_hdr
.crfd
> 0) /* relative file descriptors */
4428 orig_rfds
= (symint_t
*) read_seek (orig_sym_hdr
.crfd
* sizeof (symint_t
),
4429 orig_sym_hdr
.cbRfdOffset
,
4430 "Relative file descriptors");
4432 if (orig_sym_hdr
.issExtMax
> 0) /* external symbols */
4433 orig_ext_syms
= (EXTR
*) read_seek (orig_sym_hdr
.iextMax
* sizeof (EXTR
),
4434 orig_sym_hdr
.cbExtOffset
,
4435 "External symbols");
4437 if (orig_sym_hdr
.idnMax
> 0) /* dense numbers */
4439 orig_dense
= (DNR
*) read_seek (orig_sym_hdr
.idnMax
* sizeof (DNR
),
4440 orig_sym_hdr
.cbDnOffset
,
4443 add_bytes (&dense_num
, (char *) orig_dense
, orig_sym_hdr
.idnMax
);
4446 if (orig_sym_hdr
.ioptMax
> 0) /* opt symbols */
4447 orig_opt_syms
= (OPTR
*) read_seek (orig_sym_hdr
.ioptMax
* sizeof (OPTR
),
4448 orig_sym_hdr
.cbOptOffset
,
4449 "Optimizer symbols");
4453 /* The symbol table should be last. */
4454 if (max_file_offset
!= (unsigned long) stat_buf
.st_size
)
4455 fatal ("symbol table is not last (symbol table ends at %ld, .o ends at %ld",
4457 (long) stat_buf
.st_size
);
4460 /* If the first original file descriptor is a dummy which the assembler
4461 put out, but there are no symbols in it, skip it now. */
4462 if (orig_sym_hdr
.ifdMax
> 1
4463 && orig_files
->csym
== 2
4464 && orig_files
->caux
== 0)
4466 char *filename
= orig_local_strs
+ (orig_files
->issBase
+ orig_files
->rss
);
4467 char *suffix
= strrchr (filename
, '.');
4469 if (suffix
!= (char *) 0 && strcmp (suffix
, ".s") == 0)
4474 /* Create array to map original file numbers to the new file numbers
4475 (in case there are duplicate filenames, we collapse them into one
4476 file section, the MIPS assembler may or may not collapse them). */
4478 remap_file_number
= (int *) alloca (sizeof (int) * orig_sym_hdr
.ifdMax
);
4480 for (fd
= delete_ifd
; fd
< orig_sym_hdr
.ifdMax
; fd
++)
4482 FDR
*fd_ptr
= ORIG_FILES (fd
);
4483 char *filename
= ORIG_LSTRS (fd_ptr
->issBase
+ fd_ptr
->rss
);
4485 /* file support itself. */
4486 add_file (filename
, filename
+ strlen (filename
));
4487 remap_file_number
[fd
] = cur_file_ptr
->file_index
;
4490 if (delete_ifd
> 0) /* just in case */
4491 remap_file_number
[0] = remap_file_number
[1];
4494 /* Loop, adding each of the external symbols. These must be in
4495 order or otherwise we would have to change the relocation
4496 entries. We don't just call add_bytes, because we need to have
4497 the names put into the external hash table. We set the type to
4498 'void' for now, and parse_def will fill in the correct type if it
4499 is in the symbol table. We must add the external symbols before
4500 the locals, since the locals do lookups against the externals. */
4503 fprintf (stderr
, "\tehash\n");
4505 for (es
= 0; es
< orig_sym_hdr
.iextMax
; es
++)
4507 EXTR
*eptr
= orig_ext_syms
+ es
;
4508 int ifd
= eptr
->ifd
;
4510 (void) add_ext_symbol (eptr
, ((long) ifd
< orig_sym_hdr
.ifdMax
)
4511 ? remap_file_number
[ ifd
] : ifd
);
4515 /* For each of the files in the object file, copy the symbols, and such
4516 into the varrays for the new object file. */
4518 for (fd
= delete_ifd
; fd
< orig_sym_hdr
.ifdMax
; fd
++)
4520 FDR
*fd_ptr
= ORIG_FILES (fd
);
4521 char *filename
= ORIG_LSTRS (fd_ptr
->issBase
+ fd_ptr
->rss
);
4529 /* file support itself. */
4530 add_file (filename
, filename
+ strlen (filename
));
4531 cur_file_ptr
->orig_fdr
= fd_ptr
;
4533 /* Copy stuff that's just passed through (such as line #'s) */
4534 cur_file_ptr
->fdr
.adr
= fd_ptr
->adr
;
4535 cur_file_ptr
->fdr
.ilineBase
= fd_ptr
->ilineBase
;
4536 cur_file_ptr
->fdr
.cline
= fd_ptr
->cline
;
4537 cur_file_ptr
->fdr
.rfdBase
= fd_ptr
->rfdBase
;
4538 cur_file_ptr
->fdr
.crfd
= fd_ptr
->crfd
;
4539 cur_file_ptr
->fdr
.cbLineOffset
= fd_ptr
->cbLineOffset
;
4540 cur_file_ptr
->fdr
.cbLine
= fd_ptr
->cbLine
;
4541 cur_file_ptr
->fdr
.fMerge
= fd_ptr
->fMerge
;
4542 cur_file_ptr
->fdr
.fReadin
= fd_ptr
->fReadin
;
4543 cur_file_ptr
->fdr
.glevel
= fd_ptr
->glevel
;
4546 fprintf (stderr
, "\thash\tstart, filename %s\n", filename
);
4548 /* For each of the static and global symbols defined, add them
4549 to the hash table of original symbols, so we can look up
4552 sym_start
= ORIG_LSYMS (fd_ptr
->isymBase
);
4553 sym_end_p1
= sym_start
+ fd_ptr
->csym
;
4554 for (sym
= sym_start
; sym
< sym_end_p1
; sym
++)
4556 switch ((st_t
) sym
->st
)
4567 auto symint_t hash_index
;
4568 char *str
= ORIG_LSTRS (fd_ptr
->issBase
+ sym
->iss
);
4569 Size_t len
= strlen (str
);
4570 shash_t
*shash_ptr
= hash_string (str
,
4575 if (shash_ptr
!= (shash_t
*) 0)
4576 error ("internal error, %s is already in original symbol table", str
);
4580 shash_ptr
= allocate_shash ();
4581 shash_ptr
->next
= orig_str_hash
[hash_index
];
4582 orig_str_hash
[hash_index
] = shash_ptr
;
4584 shash_ptr
->len
= len
;
4585 shash_ptr
->indx
= indexNil
;
4586 shash_ptr
->string
= str
;
4587 shash_ptr
->sym_ptr
= sym
;
4593 if ((sc_t
) sym
->sc
== sc_Text
)
4595 char *str
= ORIG_LSTRS (fd_ptr
->issBase
+ sym
->iss
);
4599 Size_t len
= strlen (str
);
4600 shash_t
*shash_ptr
= hash_string (str
,
4605 if (shash_ptr
!= (shash_t
*) 0)
4606 shash_ptr
->end_ptr
= sym
;
4616 fprintf (stderr
, "\thash\tdone, filename %s\n", filename
);
4617 fprintf (stderr
, "\tproc\tstart, filename %s\n", filename
);
4620 /* Go through each of the procedures in this file, and add the
4621 procedure pointer to the hash entry for the given name. */
4623 proc_start
= ORIG_PROCS (fd_ptr
->ipdFirst
);
4624 proc_end_p1
= proc_start
+ fd_ptr
->cpd
;
4625 for (proc
= proc_start
; proc
< proc_end_p1
; proc
++)
4627 SYMR
*proc_sym
= ORIG_LSYMS (fd_ptr
->isymBase
+ proc
->isym
);
4628 char *str
= ORIG_LSTRS (fd_ptr
->issBase
+ proc_sym
->iss
);
4629 Size_t len
= strlen (str
);
4630 shash_t
*shash_ptr
= hash_string (str
,
4635 if (shash_ptr
== (shash_t
*) 0)
4636 error ("internal error, function %s is not in original symbol table", str
);
4639 shash_ptr
->proc_ptr
= proc
;
4643 fprintf (stderr
, "\tproc\tdone, filename %s\n", filename
);
4646 cur_file_ptr
= first_file
;
4649 /* Copy all of the object file up to the symbol table. Originally
4650 we were going to use ftruncate, but that doesn't seem to work
4651 on Ultrix 3.1.... */
4653 if (fseek (obj_in_stream
, (long) 0, SEEK_SET
) != 0)
4654 pfatal_with_name (obj_in_name
);
4656 if (fseek (object_stream
, (long) 0, SEEK_SET
) != 0)
4657 pfatal_with_name (object_name
);
4659 for (remaining
= orig_file_header
.f_symptr
;
4661 remaining
-= num_write
)
4664 = (remaining
<= (int) sizeof (buffer
))
4665 ? remaining
: (int) sizeof (buffer
);
4666 sys_read
= fread (buffer
, 1, num_write
, obj_in_stream
);
4668 pfatal_with_name (obj_in_name
);
4670 else if (sys_read
!= num_write
)
4671 fatal ("wanted to read %d bytes from %s, system returned %d",
4676 sys_write
= fwrite (buffer
, 1, num_write
, object_stream
);
4678 pfatal_with_name (object_name
);
4680 else if (sys_write
!= num_write
)
4681 fatal ("wrote %d bytes to %s, system returned %d",
4689 /* Ye olde main program. */
4691 extern int main (int, char **);
4694 main (int argc
, char **argv
)
4701 progname
= lbasename (argv
[0]);
4703 (void) signal (SIGSEGV
, catch_signal
);
4704 (void) signal (SIGBUS
, catch_signal
);
4705 (void) signal (SIGABRT
, catch_signal
);
4707 #if !defined(__SABER__) && !defined(lint)
4708 if (sizeof (efdr_t
) > PAGE_USIZE
)
4709 fatal ("efdr_t has a sizeof %d bytes, when it should be less than %d",
4710 (int) sizeof (efdr_t
),
4713 if (sizeof (page_t
) != PAGE_USIZE
)
4714 fatal ("page_t has a sizeof %d bytes, when it should be %d",
4715 (int) sizeof (page_t
),
4720 alloc_counts
[ alloc_type_none
].alloc_name
= "none";
4721 alloc_counts
[ alloc_type_scope
].alloc_name
= "scope";
4722 alloc_counts
[ alloc_type_vlinks
].alloc_name
= "vlinks";
4723 alloc_counts
[ alloc_type_shash
].alloc_name
= "shash";
4724 alloc_counts
[ alloc_type_thash
].alloc_name
= "thash";
4725 alloc_counts
[ alloc_type_tag
].alloc_name
= "tag";
4726 alloc_counts
[ alloc_type_forward
].alloc_name
= "forward";
4727 alloc_counts
[ alloc_type_thead
].alloc_name
= "thead";
4728 alloc_counts
[ alloc_type_varray
].alloc_name
= "varray";
4730 int_type_info
= type_info_init
;
4731 int_type_info
.basic_type
= bt_Int
;
4733 void_type_info
= type_info_init
;
4734 void_type_info
.basic_type
= bt_Void
;
4736 while ((option
= getopt_long (argc
, argv
, "d:i:I:o:v", options
, NULL
)) != -1)
4744 debug
= strtol (optarg
, &num_end
, 0);
4745 if ((unsigned) debug
> 4 || num_end
== optarg
)
4751 if (rename_output
|| obj_in_name
!= (char *) 0)
4756 /* Fall through to 'i' case. */
4759 if (obj_in_name
== (char *) 0)
4761 obj_in_name
= optarg
;
4769 if (object_name
== (char *) 0)
4770 object_name
= optarg
;
4786 printf (_("mips-tfile %s%s\n"), pkgversion_string
, version_string
);
4787 fputs ("Copyright (C) 2011 Free Software Foundation, Inc.\n", stdout
);
4788 fputs (_("This is free software; see the source for copying conditions. There is NO\n\
4789 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"),
4794 if (obj_in_name
== (char *) 0 && optind
<= argc
- 2)
4795 obj_in_name
= argv
[--argc
];
4797 if (object_name
== (char *) 0 && optind
<= argc
- 2)
4798 object_name
= argv
[--argc
];
4800 /* If there is an output name, but no input name use
4801 the same file for both, deleting the name between
4802 opening it for input and opening it for output. */
4803 if (obj_in_name
== (char *) 0 && object_name
!= (char *) 0)
4805 obj_in_name
= object_name
;
4809 if (optind
!= argc
- 1)
4812 if (verbose
|| had_errors
)
4813 fprintf (stderr
, _("mips-tfile (GCC) %s\n"), version_string
);
4815 if (object_name
== (char *) 0 || had_errors
)
4817 fprintf (stderr
, _("Calling Sequence:\n"));
4818 fprintf (stderr
, _("\tmips-tfile [-d <num>] [-v] [-i <o-in-file>] -o <o-out-file> <s-file> (or)\n"));
4819 fprintf (stderr
, _("\tmips-tfile [-d <num>] [-v] [-I <o-in-file>] -o <o-out-file> <s-file> (or)\n"));
4820 fprintf (stderr
, _("\tmips-tfile [-d <num>] [-v] <s-file> <o-in-file> <o-out-file>\n"));
4821 fprintf (stderr
, "\n");
4822 fprintf (stderr
, _("Debug levels are:\n"));
4823 fprintf (stderr
, _(" 1\tGeneral debug + trace functions/blocks.\n"));
4824 fprintf (stderr
, _(" 2\tDebug level 1 + trace externals.\n"));
4825 fprintf (stderr
, _(" 3\tDebug level 2 + trace all symbols.\n"));
4826 fprintf (stderr
, _(" 4\tDebug level 3 + trace memory allocations.\n"));
4830 if (obj_in_name
== (char *) 0)
4831 obj_in_name
= object_name
;
4833 if (rename_output
&& rename (object_name
, obj_in_name
) != 0)
4835 char *buffer
= (char *) allocate_multiple_pages (4);
4841 /* Rename failed, copy input file */
4842 in_fd
= open (object_name
, O_RDONLY
, 0666);
4844 pfatal_with_name (object_name
);
4846 out_fd
= open (obj_in_name
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
4848 pfatal_with_name (obj_in_name
);
4850 while ((len
= read (in_fd
, buffer
, 4*PAGE_SIZE
)) > 0)
4852 len2
= write (out_fd
, buffer
, len
);
4854 pfatal_with_name (object_name
);
4857 fatal ("wrote %d bytes to %s, expected to write %d", len2
, obj_in_name
, len
);
4860 free_multiple_pages ((page_t
*) buffer
, 4);
4863 pfatal_with_name (object_name
);
4865 if (close (in_fd
) < 0)
4866 pfatal_with_name (object_name
);
4868 if (close (out_fd
) < 0)
4869 pfatal_with_name (obj_in_name
);
4872 /* Must open input before output, since the output may be the same file, and
4873 we need to get the input handle before truncating it. */
4874 obj_in_stream
= fopen (obj_in_name
, "r");
4875 if (obj_in_stream
== (FILE *) 0)
4876 pfatal_with_name (obj_in_name
);
4878 if (delete_input
&& unlink (obj_in_name
) != 0)
4879 pfatal_with_name (obj_in_name
);
4881 object_stream
= fopen (object_name
, "w");
4882 if (object_stream
== (FILE *) 0)
4883 pfatal_with_name (object_name
);
4885 if (strcmp (argv
[optind
], "-") != 0)
4887 input_name
= argv
[optind
];
4888 if (freopen (argv
[optind
], "r", stdin
) != stdin
)
4889 pfatal_with_name (argv
[optind
]);
4892 copy_object (); /* scan & copy object file */
4893 parse_input (); /* scan all of input */
4895 update_headers (); /* write out tfile */
4900 fprintf (stderr
, "\n\tAllocation summary:\n\n");
4901 for (i
= (int) alloc_type_none
; i
< (int) alloc_type_last
; i
++)
4902 if (alloc_counts
[i
].total_alloc
)
4905 "\t%s\t%5d allocation(s), %5d free(s), %2d page(s)\n",
4906 alloc_counts
[i
].alloc_name
,
4907 alloc_counts
[i
].total_alloc
,
4908 alloc_counts
[i
].total_free
,
4909 alloc_counts
[i
].total_pages
);
4913 return (had_errors
) ? 1 : 0;
4917 /* Catch a signal and exit without dumping core. */
4920 catch_signal (int signum
)
4922 (void) signal (signum
, SIG_DFL
); /* just in case... */
4923 fatal ("%s", strsignal (signum
));
4926 /* Print a fatal error message. NAME is the text.
4927 Also include a system error message based on `errno'. */
4930 pfatal_with_name (const char *msg
)
4932 int save_errno
= errno
; /* just in case.... */
4933 if (line_number
> 0)
4934 fprintf (stderr
, "%s, %s:%ld ", progname
, input_name
, line_number
);
4936 fprintf (stderr
, "%s:", progname
);
4940 fprintf (stderr
, "[errno = 0] %s\n", msg
);
4948 /* Procedure to die with an out of bounds error message. It has
4949 type int, so it can be used with an ?: expression within the
4950 ORIG_xxx macros, but the function never returns. */
4953 out_of_bounds (symint_t indx
, /* index that is out of bounds */
4954 symint_t max
, /* maximum index */
4955 const char *str
, /* string to print out */
4956 int prog_line
) /* line number within mips-tfile.c */
4958 if (indx
< max
) /* just in case */
4961 fprintf (stderr
, "%s, %s:%ld index %lu is out of bounds for %s, max is %lu, mips-tfile.c line# %d\n",
4962 progname
, input_name
, line_number
, indx
, str
, max
, prog_line
);
4965 return 0; /* turn off warning messages */
4969 /* Allocate a cluster of pages. USE_MALLOC says that malloc does not
4970 like sbrk's behind its back (or sbrk isn't available). If we use
4971 sbrk, we assume it gives us zeroed pages. */
4973 #ifndef MALLOC_CHECK
4977 allocate_cluster (Size_t npages
)
4979 page_t
*value
= xcalloc (npages
, PAGE_USIZE
);
4982 fprintf (stderr
, "\talloc\tnpages = %d, value = 0x%.8x\n", npages
, value
);
4987 #else /* USE_MALLOC */
4990 allocate_cluster (Size_t npages
)
4992 page_t
*ptr
= (page_t
*) sbrk (0); /* current sbreak */
4993 unsigned long offset
= ((unsigned long) ptr
) & (PAGE_SIZE
- 1);
4995 if (offset
!= 0) /* align to a page boundary */
4997 if (sbrk (PAGE_USIZE
- offset
) == (char *)-1)
4998 pfatal_with_name ("allocate_cluster");
5000 ptr
= (page_t
*) (((char *) ptr
) + PAGE_SIZE
- offset
);
5003 if (sbrk (npages
* PAGE_USIZE
) == (char *) -1)
5004 pfatal_with_name ("allocate_cluster");
5007 fprintf (stderr
, "\talloc\tnpages = %lu, value = " HOST_PTR_PRINTF
"\n",
5008 (unsigned long) npages
, (void *) ptr
);
5013 #endif /* USE_MALLOC */
5016 static page_t
*cluster_ptr
= NULL
;
5017 static unsigned pages_left
= 0;
5019 #endif /* MALLOC_CHECK */
5022 /* Allocate some pages (which is initialized to 0). */
5025 allocate_multiple_pages (Size_t npages
)
5027 #ifndef MALLOC_CHECK
5028 if (pages_left
== 0 && npages
< MAX_CLUSTER_PAGES
)
5030 pages_left
= MAX_CLUSTER_PAGES
;
5031 cluster_ptr
= allocate_cluster (MAX_CLUSTER_PAGES
);
5034 if (npages
<= pages_left
)
5036 page_t
*ptr
= cluster_ptr
;
5037 cluster_ptr
+= npages
;
5038 pages_left
-= npages
;
5042 return allocate_cluster (npages
);
5044 #else /* MALLOC_CHECK */
5045 return xcalloc (npages
, PAGE_SIZE
);
5047 #endif /* MALLOC_CHECK */
5051 /* Release some pages. */
5054 free_multiple_pages (page_t
*page_ptr
, Size_t npages
)
5056 #ifndef MALLOC_CHECK
5057 if (pages_left
== 0)
5059 cluster_ptr
= page_ptr
;
5060 pages_left
= npages
;
5063 else if ((page_ptr
+ npages
) == cluster_ptr
)
5065 cluster_ptr
-= npages
;
5066 pages_left
+= npages
;
5069 /* otherwise the page is not freed. If more than call is
5070 done, we probably should worry about it, but at present,
5071 the free pages is done right after an allocate. */
5073 #else /* MALLOC_CHECK */
5076 #endif /* MALLOC_CHECK */
5080 /* Allocate one page (which is initialized to 0). */
5083 allocate_page (void)
5085 #ifndef MALLOC_CHECK
5086 if (pages_left
== 0)
5088 pages_left
= MAX_CLUSTER_PAGES
;
5089 cluster_ptr
= allocate_cluster (MAX_CLUSTER_PAGES
);
5093 return cluster_ptr
++;
5095 #else /* MALLOC_CHECK */
5096 return xcalloc (1, PAGE_SIZE
);
5098 #endif /* MALLOC_CHECK */
5102 /* Allocate scoping information. */
5105 allocate_scope (void)
5108 static scope_t initial_scope
;
5110 #ifndef MALLOC_CHECK
5111 ptr
= alloc_counts
[ (int) alloc_type_scope
].free_list
.f_scope
;
5112 if (ptr
!= (scope_t
*) 0)
5113 alloc_counts
[ (int) alloc_type_scope
].free_list
.f_scope
= ptr
->free
;
5117 int unallocated
= alloc_counts
[ (int) alloc_type_scope
].unallocated
;
5118 page_t
*cur_page
= alloc_counts
[ (int) alloc_type_scope
].cur_page
;
5120 if (unallocated
== 0)
5122 unallocated
= PAGE_SIZE
/ sizeof (scope_t
);
5123 alloc_counts
[ (int) alloc_type_scope
].cur_page
= cur_page
= allocate_page ();
5124 alloc_counts
[ (int) alloc_type_scope
].total_pages
++;
5127 ptr
= &cur_page
->scope
[ --unallocated
];
5128 alloc_counts
[ (int) alloc_type_scope
].unallocated
= unallocated
;
5132 ptr
= xmalloc (sizeof (scope_t
));
5136 alloc_counts
[ (int) alloc_type_scope
].total_alloc
++;
5137 *ptr
= initial_scope
;
5141 /* Free scoping information. */
5144 free_scope (scope_t
*ptr
)
5146 alloc_counts
[ (int) alloc_type_scope
].total_free
++;
5148 #ifndef MALLOC_CHECK
5149 ptr
->free
= alloc_counts
[ (int) alloc_type_scope
].free_list
.f_scope
;
5150 alloc_counts
[ (int) alloc_type_scope
].free_list
.f_scope
= ptr
;
5159 /* Allocate links for pages in a virtual array. */
5162 allocate_vlinks (void)
5165 static vlinks_t initial_vlinks
;
5167 #ifndef MALLOC_CHECK
5168 int unallocated
= alloc_counts
[ (int) alloc_type_vlinks
].unallocated
;
5169 page_t
*cur_page
= alloc_counts
[ (int) alloc_type_vlinks
].cur_page
;
5171 if (unallocated
== 0)
5173 unallocated
= PAGE_SIZE
/ sizeof (vlinks_t
);
5174 alloc_counts
[ (int) alloc_type_vlinks
].cur_page
= cur_page
= allocate_page ();
5175 alloc_counts
[ (int) alloc_type_vlinks
].total_pages
++;
5178 ptr
= &cur_page
->vlinks
[ --unallocated
];
5179 alloc_counts
[ (int) alloc_type_vlinks
].unallocated
= unallocated
;
5182 ptr
= xmalloc (sizeof (vlinks_t
));
5186 alloc_counts
[ (int) alloc_type_vlinks
].total_alloc
++;
5187 *ptr
= initial_vlinks
;
5192 /* Allocate string hash buckets. */
5195 allocate_shash (void)
5198 static shash_t initial_shash
;
5200 #ifndef MALLOC_CHECK
5201 int unallocated
= alloc_counts
[ (int) alloc_type_shash
].unallocated
;
5202 page_t
*cur_page
= alloc_counts
[ (int) alloc_type_shash
].cur_page
;
5204 if (unallocated
== 0)
5206 unallocated
= PAGE_SIZE
/ sizeof (shash_t
);
5207 alloc_counts
[ (int) alloc_type_shash
].cur_page
= cur_page
= allocate_page ();
5208 alloc_counts
[ (int) alloc_type_shash
].total_pages
++;
5211 ptr
= &cur_page
->shash
[ --unallocated
];
5212 alloc_counts
[ (int) alloc_type_shash
].unallocated
= unallocated
;
5215 ptr
= xmalloc (sizeof (shash_t
));
5219 alloc_counts
[ (int) alloc_type_shash
].total_alloc
++;
5220 *ptr
= initial_shash
;
5225 /* Allocate type hash buckets. */
5228 allocate_thash (void)
5231 static thash_t initial_thash
;
5233 #ifndef MALLOC_CHECK
5234 int unallocated
= alloc_counts
[ (int) alloc_type_thash
].unallocated
;
5235 page_t
*cur_page
= alloc_counts
[ (int) alloc_type_thash
].cur_page
;
5237 if (unallocated
== 0)
5239 unallocated
= PAGE_SIZE
/ sizeof (thash_t
);
5240 alloc_counts
[ (int) alloc_type_thash
].cur_page
= cur_page
= allocate_page ();
5241 alloc_counts
[ (int) alloc_type_thash
].total_pages
++;
5244 ptr
= &cur_page
->thash
[ --unallocated
];
5245 alloc_counts
[ (int) alloc_type_thash
].unallocated
= unallocated
;
5248 ptr
= xmalloc (sizeof (thash_t
));
5252 alloc_counts
[ (int) alloc_type_thash
].total_alloc
++;
5253 *ptr
= initial_thash
;
5258 /* Allocate structure, union, or enum tag information. */
5264 static tag_t initial_tag
;
5266 #ifndef MALLOC_CHECK
5267 ptr
= alloc_counts
[ (int) alloc_type_tag
].free_list
.f_tag
;
5268 if (ptr
!= (tag_t
*) 0)
5269 alloc_counts
[ (int) alloc_type_tag
].free_list
.f_tag
= ptr
->free
;
5273 int unallocated
= alloc_counts
[ (int) alloc_type_tag
].unallocated
;
5274 page_t
*cur_page
= alloc_counts
[ (int) alloc_type_tag
].cur_page
;
5276 if (unallocated
== 0)
5278 unallocated
= PAGE_SIZE
/ sizeof (tag_t
);
5279 alloc_counts
[ (int) alloc_type_tag
].cur_page
= cur_page
= allocate_page ();
5280 alloc_counts
[ (int) alloc_type_tag
].total_pages
++;
5283 ptr
= &cur_page
->tag
[ --unallocated
];
5284 alloc_counts
[ (int) alloc_type_tag
].unallocated
= unallocated
;
5288 ptr
= xmalloc (sizeof (tag_t
));
5292 alloc_counts
[ (int) alloc_type_tag
].total_alloc
++;
5297 /* Free scoping information. */
5300 free_tag (tag_t
*ptr
)
5302 alloc_counts
[ (int) alloc_type_tag
].total_free
++;
5304 #ifndef MALLOC_CHECK
5305 ptr
->free
= alloc_counts
[ (int) alloc_type_tag
].free_list
.f_tag
;
5306 alloc_counts
[ (int) alloc_type_tag
].free_list
.f_tag
= ptr
;
5315 /* Allocate forward reference to a yet unknown tag. */
5318 allocate_forward (void)
5321 static forward_t initial_forward
;
5323 #ifndef MALLOC_CHECK
5324 ptr
= alloc_counts
[ (int) alloc_type_forward
].free_list
.f_forward
;
5325 if (ptr
!= (forward_t
*) 0)
5326 alloc_counts
[ (int) alloc_type_forward
].free_list
.f_forward
= ptr
->free
;
5330 int unallocated
= alloc_counts
[ (int) alloc_type_forward
].unallocated
;
5331 page_t
*cur_page
= alloc_counts
[ (int) alloc_type_forward
].cur_page
;
5333 if (unallocated
== 0)
5335 unallocated
= PAGE_SIZE
/ sizeof (forward_t
);
5336 alloc_counts
[ (int) alloc_type_forward
].cur_page
= cur_page
= allocate_page ();
5337 alloc_counts
[ (int) alloc_type_forward
].total_pages
++;
5340 ptr
= &cur_page
->forward
[ --unallocated
];
5341 alloc_counts
[ (int) alloc_type_forward
].unallocated
= unallocated
;
5345 ptr
= xmalloc (sizeof (forward_t
));
5349 alloc_counts
[ (int) alloc_type_forward
].total_alloc
++;
5350 *ptr
= initial_forward
;
5354 /* Free scoping information. */
5357 free_forward (forward_t
*ptr
)
5359 alloc_counts
[ (int) alloc_type_forward
].total_free
++;
5361 #ifndef MALLOC_CHECK
5362 ptr
->free
= alloc_counts
[ (int) alloc_type_forward
].free_list
.f_forward
;
5363 alloc_counts
[ (int) alloc_type_forward
].free_list
.f_forward
= ptr
;
5372 /* Allocate head of type hash list. */
5375 allocate_thead (void)
5378 static thead_t initial_thead
;
5380 #ifndef MALLOC_CHECK
5381 ptr
= alloc_counts
[ (int) alloc_type_thead
].free_list
.f_thead
;
5382 if (ptr
!= (thead_t
*) 0)
5383 alloc_counts
[ (int) alloc_type_thead
].free_list
.f_thead
= ptr
->free
;
5387 int unallocated
= alloc_counts
[ (int) alloc_type_thead
].unallocated
;
5388 page_t
*cur_page
= alloc_counts
[ (int) alloc_type_thead
].cur_page
;
5390 if (unallocated
== 0)
5392 unallocated
= PAGE_SIZE
/ sizeof (thead_t
);
5393 alloc_counts
[ (int) alloc_type_thead
].cur_page
= cur_page
= allocate_page ();
5394 alloc_counts
[ (int) alloc_type_thead
].total_pages
++;
5397 ptr
= &cur_page
->thead
[ --unallocated
];
5398 alloc_counts
[ (int) alloc_type_thead
].unallocated
= unallocated
;
5402 ptr
= xmalloc (sizeof (thead_t
));
5406 alloc_counts
[ (int) alloc_type_thead
].total_alloc
++;
5407 *ptr
= initial_thead
;
5411 /* Free scoping information. */
5414 free_thead (thead_t
*ptr
)
5416 alloc_counts
[ (int) alloc_type_thead
].total_free
++;
5418 #ifndef MALLOC_CHECK
5419 ptr
->free
= (thead_t
*) alloc_counts
[ (int) alloc_type_thead
].free_list
.f_thead
;
5420 alloc_counts
[ (int) alloc_type_thead
].free_list
.f_thead
= ptr
;
5428 #endif /* MIPS_DEBUGGING_INFO */
5431 /* Output an error message and exit. */
5434 fatal (const char *format
, ...)
5438 va_start (ap
, format
);
5440 if (line_number
> 0)
5441 fprintf (stderr
, "%s, %s:%ld ", progname
, input_name
, line_number
);
5443 fprintf (stderr
, "%s:", progname
);
5445 vfprintf (stderr
, format
, ap
);
5447 fprintf (stderr
, "\n");
5448 if (line_number
> 0)
5449 fprintf (stderr
, "line:\t%s\n", cur_line_start
);
5456 error (const char *format
, ...)
5460 va_start (ap
, format
);
5462 if (line_number
> 0)
5463 fprintf (stderr
, "%s, %s:%ld ", progname
, input_name
, line_number
);
5465 fprintf (stderr
, "%s:", progname
);
5467 vfprintf (stderr
, format
, ap
);
5468 fprintf (stderr
, "\n");
5469 if (line_number
> 0)
5470 fprintf (stderr
, "line:\t%s\n", cur_line_start
);
5478 /* More 'friendly' abort that prints the line and file. */
5481 fancy_abort (const char *file
, int line
, const char *func
)
5483 fatal ("abort in %s, at %s:%d", func
, file
, line
);
5487 /* When `malloc.c' is compiled with `rcheck' defined,
5488 it calls this function to report clobberage. */
5491 botch (const char *s
)