PR 48915 Update mixed-language programming documentation
[official-gcc.git] / gcc / mips-tfile.c
blobaf0b248f841f3b8211391f7f583012028e00ef46
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
15 version.
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
20 for more details.
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:
30 Symbolic Header
32 +-- Auxiliary Symbols
34 +-- Dense number table
36 +-- Optimizer Symbols
38 +-- External Strings
40 +-- External Symbols
42 +-- Relative file descriptors
44 +-- File table
46 +-- Procedure table
48 +-- Line number table
50 +-- Local Strings
52 +-- Local Symbols
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:
101 btNil -- undefined
102 btAdr -- address - integer same size as ptr
103 btChar -- character
104 btUChar -- unsigned character
105 btShort -- short
106 btUShort -- unsigned short
107 btInt -- int
108 btUInt -- unsigned int
109 btLong -- long
110 btULong -- unsigned long
111 btFloat -- float (real)
112 btDouble -- Double (real)
113 btStruct -- Structure (Record)
114 btUnion -- Union (variant)
115 btEnum -- Enumerated
116 btTypedef -- defined via a typedef isymRef
117 btRange -- subrange of int
118 btSet -- pascal sets
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
126 btPicture -- Picture
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
131 are:
133 tqNil -- no more qualifiers
134 tqPtr -- pointer
135 tqProc -- procedure
136 tqArray -- array
137 tqFar -- 8086 far pointers
138 tqVol -- volatile
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
167 tables for the file.
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
185 symbol.
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
193 stStatic -- static
194 stParam -- procedure argument
195 stLocal -- local variable
196 stLabel -- label
197 stProc -- External Procedure
198 stBlock -- beginning of block
199 stEnd -- end (of anything)
200 stMember -- member (of anything)
201 stTypedef -- type definition
202 stFile -- file name
203 stRegReloc -- register relocation
204 stForward -- forwarding address
205 stStaticProc -- Static procedure
206 stConstant -- const
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:
239 #include <stdio.h>
241 main(){
242 printf("Hello World!\n");
243 return 0;
246 Mips-tdump produces the following information:
248 Global file header:
249 magic number 0x162
250 # sections 2
251 timestamp 645311799, Wed Jun 13 17:16:39 1990
252 symbolic header offset 284
253 symbolic header size 96
254 optional header 56
255 flags 0x0
257 Symbolic header, magic number = 0x7009, vstamp = 1.31:
259 Info Offset Number Bytes
260 ==== ====== ====== =====
262 Line numbers 380 4 4 [13]
263 Dense numbers 0 0 0
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
271 Relative Files 0 0 0
272 External Symbols 1152 20 320
274 File #0, "hello2.c"
276 Name index = 1 Readin = No
277 Merge = No Endian = LITTLE
278 Debug level = G2 Language = C
279 Adr = 0x00000000
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"
294 End+1 symbol = 6
295 String index = 1
296 Storage class = Text Index = 6
297 Symbol type = File Value = 0
299 Symbol# 1: "main"
300 End+1 symbol = 5
301 Type = int
302 String index = 10
303 Storage class = Text Index = 12
304 Symbol type = Proc Value = 0
306 Symbol# 2: ""
307 End+1 symbol = 4
308 String index = 0
309 Storage class = Text Index = 4
310 Symbol type = Block Value = 8
312 Symbol# 3: ""
313 First symbol = 2
314 String index = 0
315 Storage class = Text Index = 2
316 Symbol type = End Value = 28
318 Symbol# 4: "main"
319 First symbol = 1
320 String index = 10
321 Storage class = Text Index = 1
322 Symbol type = End Value = 52
324 Symbol# 5: "hello2.c"
325 First symbol = 0
326 String index = 1
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
352 .frame $29,24,$31
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
368 Adr = 0x00000000
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
376 Procedures 1 0 0 436
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"
383 End+1 symbol = 10
384 String index = 1
385 Storage class = Text Index = 10
386 Symbol type = File Value = 0
388 Symbol# 1: "_iobuf"
389 End+1 symbol = 9
390 String index = 22
391 Storage class = Info Index = 9
392 Symbol type = Block Value = 20
394 Symbol# 2: "_cnt"
395 Type = int
396 String index = 29
397 Storage class = Info Index = 4
398 Symbol type = Member Value = 0
400 Symbol# 3: "_ptr"
401 Type = ptr to char
402 String index = 34
403 Storage class = Info Index = 15
404 Symbol type = Member Value = 32
406 Symbol# 4: "_base"
407 Type = ptr to char
408 String index = 39
409 Storage class = Info Index = 16
410 Symbol type = Member Value = 64
412 Symbol# 5: "_bufsiz"
413 Type = int
414 String index = 45
415 Storage class = Info Index = 4
416 Symbol type = Member Value = 96
418 Symbol# 6: "_flag"
419 Type = short
420 String index = 53
421 Storage class = Info Index = 3
422 Symbol type = Member Value = 128
424 Symbol# 7: "_file"
425 Type = char
426 String index = 59
427 Storage class = Info Index = 2
428 Symbol type = Member Value = 144
430 Symbol# 8: ""
431 First symbol = 1
432 String index = 0
433 Storage class = Info Index = 1
434 Symbol type = End Value = 0
436 Symbol# 9: "/usr/include/stdio.h"
437 First symbol = 0
438 String index = 1
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
474 Symbol# 0: "_iob"
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
480 Symbol# 1: "fopen"
481 String index = 5 Ifd = 1
482 Storage class = Nil Index = 1048575
483 Symbol type = Proc Value = 0
485 Symbol# 2: "fdopen"
486 String index = 11 Ifd = 1
487 Storage class = Nil Index = 1048575
488 Symbol type = Proc Value = 0
490 Symbol# 3: "freopen"
491 String index = 18 Ifd = 1
492 Storage class = Nil Index = 1048575
493 Symbol type = Proc Value = 0
495 Symbol# 4: "popen"
496 String index = 26 Ifd = 1
497 Storage class = Nil Index = 1048575
498 Symbol type = Proc Value = 0
500 Symbol# 5: "tmpfile"
501 String index = 32 Ifd = 1
502 Storage class = Nil Index = 1048575
503 Symbol type = Proc Value = 0
505 Symbol# 6: "ftell"
506 String index = 40 Ifd = 1
507 Storage class = Nil Index = 1048575
508 Symbol type = Proc Value = 0
510 Symbol# 7: "rewind"
511 String index = 46 Ifd = 1
512 Storage class = Nil Index = 1048575
513 Symbol type = Proc Value = 0
515 Symbol# 8: "setbuf"
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
530 Symbol# 11: "fgets"
531 String index = 81 Ifd = 1
532 Storage class = Nil Index = 1048575
533 Symbol type = Proc Value = 0
535 Symbol# 12: "gets"
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
555 Symbol# 16: "tmpnam"
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
565 Symbol# 18: "main"
566 Type = int
567 String index = 131 Ifd = 0
568 Storage class = Text Index = 1
569 Symbol type = Proc Value = 0
571 Symbol# 19: "printf"
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:
578 #0 0 0x00000000 void
579 #2 8 0x00000008 char
580 #3 16 0x00000010 short
581 #4 24 0x00000018 int
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 }
603 #include "config.h"
604 #include "system.h"
605 #include "version.h"
606 #include "intl.h"
608 /* Include getopt.h for the sake of getopt_long. */
609 #include "getopt.h"
611 /* Macros for mips-tfile.c to encapsulate stabs in ECOFF, and for
612 mips-tdump.c to print them out.
614 These must match the corresponding definitions in gdb/mipsread.c.
615 Unfortunately, gcc and gdb do not currently share any directories. */
617 #define CODE_MASK 0x8F300
618 #define MIPS_IS_STAB(sym) (((sym)->index & 0xFFF00) == CODE_MASK)
619 #define MIPS_MARK_STAB(code) ((code)+CODE_MASK)
620 #define MIPS_UNMARK_STAB(code) ((code)-CODE_MASK)
622 /* The following might be called from obstack or malloc,
623 so they can't be static. */
625 extern void pfatal_with_name (const char *) ATTRIBUTE_NORETURN;
626 extern void botch (const char *) ATTRIBUTE_NORETURN;
628 extern void fatal (const char *format, ...) ATTRIBUTE_PRINTF_1 ATTRIBUTE_NORETURN;
629 extern void error (const char *format, ...) ATTRIBUTE_PRINTF_1;
631 /* The local and global symbols have a field index, so undo any defines
632 of index -> strchr. */
634 #undef index
636 #ifndef CROSS_DIRECTORY_STRUCTURE
637 #include <a.out.h>
638 #else
639 #include "mips/a.out.h"
640 #endif /* CROSS_DIRECTORY_STRUCTURE */
642 #include "gstab.h"
644 #define IS_ASM_IDENT(ch) \
645 (ISIDNUM (ch) || (ch) == '.' || (ch) == '$')
648 /* Redefinition of storage classes as an enumeration for better
649 debugging. */
651 typedef enum sc {
652 sc_Nil = scNil, /* no storage class */
653 sc_Text = scText, /* text symbol */
654 sc_Data = scData, /* initialized data symbol */
655 sc_Bss = scBss, /* un-initialized data symbol */
656 sc_Register = scRegister, /* value of symbol is register number */
657 sc_Abs = scAbs, /* value of symbol is absolute */
658 sc_Undefined = scUndefined, /* who knows? */
659 sc_CdbLocal = scCdbLocal, /* variable's value is IN se->va.?? */
660 sc_Bits = scBits, /* this is a bit field */
661 sc_CdbSystem = scCdbSystem, /* value is IN CDB's address space */
662 sc_RegImage = scRegImage, /* register value saved on stack */
663 sc_Info = scInfo, /* symbol contains debugger information */
664 sc_UserStruct = scUserStruct, /* addr in struct user for current process */
665 sc_SData = scSData, /* load time only small data */
666 sc_SBss = scSBss, /* load time only small common */
667 sc_RData = scRData, /* load time only read only data */
668 sc_Var = scVar, /* Var parameter (fortran,pascal) */
669 sc_Common = scCommon, /* common variable */
670 sc_SCommon = scSCommon, /* small common */
671 sc_VarRegister = scVarRegister, /* Var parameter in a register */
672 sc_Variant = scVariant, /* Variant record */
673 sc_SUndefined = scSUndefined, /* small undefined(external) data */
674 sc_Init = scInit, /* .init section symbol */
675 sc_Max = scMax /* Max storage class+1 */
676 } sc_t;
678 /* Redefinition of symbol type. */
680 typedef enum st {
681 st_Nil = stNil, /* Nuthin' special */
682 st_Global = stGlobal, /* external symbol */
683 st_Static = stStatic, /* static */
684 st_Param = stParam, /* procedure argument */
685 st_Local = stLocal, /* local variable */
686 st_Label = stLabel, /* label */
687 st_Proc = stProc, /* " " Procedure */
688 st_Block = stBlock, /* beginning of block */
689 st_End = stEnd, /* end (of anything) */
690 st_Member = stMember, /* member (of anything - struct/union/enum */
691 st_Typedef = stTypedef, /* type definition */
692 st_File = stFile, /* file name */
693 st_RegReloc = stRegReloc, /* register relocation */
694 st_Forward = stForward, /* forwarding address */
695 st_StaticProc = stStaticProc, /* load time only static procs */
696 st_Constant = stConstant, /* const */
697 st_Str = stStr, /* string */
698 st_Number = stNumber, /* pure number (i.e. 4 NOR 2+2) */
699 st_Expr = stExpr, /* 2+2 vs. 4 */
700 st_Type = stType, /* post-coercion SER */
701 st_Max = stMax /* max type+1 */
702 } st_t;
704 /* Redefinition of type qualifiers. */
706 typedef enum tq {
707 tq_Nil = tqNil, /* bt is what you see */
708 tq_Ptr = tqPtr, /* pointer */
709 tq_Proc = tqProc, /* procedure */
710 tq_Array = tqArray, /* duh */
711 tq_Far = tqFar, /* longer addressing - 8086/8 land */
712 tq_Vol = tqVol, /* volatile */
713 tq_Max = tqMax /* Max type qualifier+1 */
714 } tq_t;
716 /* Redefinition of basic types. */
718 typedef enum bt {
719 bt_Nil = btNil, /* undefined */
720 bt_Adr = btAdr, /* address - integer same size as pointer */
721 bt_Char = btChar, /* character */
722 bt_UChar = btUChar, /* unsigned character */
723 bt_Short = btShort, /* short */
724 bt_UShort = btUShort, /* unsigned short */
725 bt_Int = btInt, /* int */
726 bt_UInt = btUInt, /* unsigned int */
727 bt_Long = btLong, /* long */
728 bt_ULong = btULong, /* unsigned long */
729 bt_Float = btFloat, /* float (real) */
730 bt_Double = btDouble, /* Double (real) */
731 bt_Struct = btStruct, /* Structure (Record) */
732 bt_Union = btUnion, /* Union (variant) */
733 bt_Enum = btEnum, /* Enumerated */
734 bt_Typedef = btTypedef, /* defined via a typedef, isymRef points */
735 bt_Range = btRange, /* subrange of int */
736 bt_Set = btSet, /* pascal sets */
737 bt_Complex = btComplex, /* fortran complex */
738 bt_DComplex = btDComplex, /* fortran double complex */
739 bt_Indirect = btIndirect, /* forward or unnamed typedef */
740 bt_FixedDec = btFixedDec, /* Fixed Decimal */
741 bt_FloatDec = btFloatDec, /* Float Decimal */
742 bt_String = btString, /* Varying Length Character String */
743 bt_Bit = btBit, /* Aligned Bit String */
744 bt_Picture = btPicture, /* Picture */
746 #ifdef btVoid
747 bt_Void = btVoid, /* Void */
748 #else
749 #define bt_Void bt_Nil
750 #endif
752 bt_Max = btMax /* Max basic type+1 */
753 } bt_t;
757 /* Basic COFF storage classes. */
758 enum coff_storage {
759 C_EFCN = -1,
760 C_NULL = 0,
761 C_AUTO = 1,
762 C_EXT = 2,
763 C_STAT = 3,
764 C_REG = 4,
765 C_EXTDEF = 5,
766 C_LABEL = 6,
767 C_ULABEL = 7,
768 C_MOS = 8,
769 C_ARG = 9,
770 C_STRTAG = 10,
771 C_MOU = 11,
772 C_UNTAG = 12,
773 C_TPDEF = 13,
774 C_USTATIC = 14,
775 C_ENTAG = 15,
776 C_MOE = 16,
777 C_REGPARM = 17,
778 C_FIELD = 18,
779 C_BLOCK = 100,
780 C_FCN = 101,
781 C_EOS = 102,
782 C_FILE = 103,
783 C_LINE = 104,
784 C_ALIAS = 105,
785 C_HIDDEN = 106,
786 C_MAX = 107
787 } coff_storage_t;
789 /* Regular COFF fundamental type. */
790 typedef enum coff_type {
791 T_NULL = 0,
792 T_ARG = 1,
793 T_CHAR = 2,
794 T_SHORT = 3,
795 T_INT = 4,
796 T_LONG = 5,
797 T_FLOAT = 6,
798 T_DOUBLE = 7,
799 T_STRUCT = 8,
800 T_UNION = 9,
801 T_ENUM = 10,
802 T_MOE = 11,
803 T_UCHAR = 12,
804 T_USHORT = 13,
805 T_UINT = 14,
806 T_ULONG = 15,
807 T_MAX = 16
808 } coff_type_t;
810 /* Regular COFF derived types. */
811 typedef enum coff_dt {
812 DT_NON = 0,
813 DT_PTR = 1,
814 DT_FCN = 2,
815 DT_ARY = 3,
816 DT_MAX = 4
817 } coff_dt_t;
819 #define N_BTMASK 017 /* bitmask to isolate basic type */
820 #define N_TMASK 003 /* bitmask to isolate derived type */
821 #define N_BT_SHIFT 4 /* # bits to shift past basic type */
822 #define N_TQ_SHIFT 2 /* # bits to shift derived types */
823 #define N_TQ 6 /* # of type qualifiers */
825 /* States for whether to hash type or not. */
826 typedef enum hash_state {
827 hash_no = 0, /* don't hash type */
828 hash_yes = 1, /* ok to hash type, or use previous hash */
829 hash_record = 2 /* ok to record hash, but don't use prev. */
830 } hash_state_t;
833 /* Types of different sized allocation requests. */
834 enum alloc_type {
835 alloc_type_none, /* dummy value */
836 alloc_type_scope, /* nested scopes linked list */
837 alloc_type_vlinks, /* glue linking pages in varray */
838 alloc_type_shash, /* string hash element */
839 alloc_type_thash, /* type hash element */
840 alloc_type_tag, /* struct/union/tag element */
841 alloc_type_forward, /* element to hold unknown tag */
842 alloc_type_thead, /* head of type hash list */
843 alloc_type_varray, /* general varray allocation */
844 alloc_type_last /* last+1 element for array bounds */
848 #define WORD_ALIGN(x) (((x) + (sizeof (long) - 1)) & ~ (sizeof (long) - 1))
849 #define DWORD_ALIGN(x) (((x) + 7) & ~7)
852 /* Structures to provide n-number of virtual arrays, each of which can
853 grow linearly, and which are written in the object file as sequential
854 pages. On systems with a BSD malloc that define USE_MALLOC, the
855 MAX_CLUSTER_PAGES should be 1 less than a power of two, since malloc
856 adds its overhead, and rounds up to the next power of 2. Pages are
857 linked together via a linked list. */
859 #ifndef PAGE_SIZE
860 #define PAGE_SIZE 32768 /* size of varray pages */
861 #endif
863 #define PAGE_USIZE ((size_t) PAGE_SIZE)
866 #ifndef MAX_CLUSTER_PAGES /* # pages to get from system */
867 #ifndef USE_MALLOC /* in one memory request */
868 #define MAX_CLUSTER_PAGES 64
869 #else
870 #define MAX_CLUSTER_PAGES 63
871 #endif
872 #endif
875 /* Linked list connecting separate page allocations. */
876 typedef struct vlinks {
877 struct vlinks *prev; /* previous set of pages */
878 struct vlinks *next; /* next set of pages */
879 union page *datum; /* start of page */
880 unsigned long start_index; /* starting index # of page */
881 } vlinks_t;
884 /* Virtual array header. */
885 typedef struct varray {
886 vlinks_t *first; /* first page link */
887 vlinks_t *last; /* last page link */
888 unsigned long num_allocated; /* # objects allocated */
889 unsigned short object_size; /* size in bytes of each object */
890 unsigned short objects_per_page; /* # objects that can fit on a page */
891 unsigned short objects_last_page; /* # objects allocated on last page */
892 } varray_t;
894 #ifndef MALLOC_CHECK
895 #define OBJECTS_PER_PAGE(type) (PAGE_SIZE / sizeof (type))
896 #else
897 #define OBJECTS_PER_PAGE(type) ((sizeof (type) > 1) ? 1 : PAGE_SIZE)
898 #endif
900 #define INIT_VARRAY(type) { /* macro to initialize a varray */ \
901 (vlinks_t *) 0, /* first */ \
902 (vlinks_t *) 0, /* last */ \
903 0, /* num_allocated */ \
904 sizeof (type), /* object_size */ \
905 OBJECTS_PER_PAGE (type), /* objects_per_page */ \
906 OBJECTS_PER_PAGE (type), /* objects_last_page */ \
909 #define INITIALIZE_VARRAY(x,type) \
910 do { \
911 (x)->object_size = sizeof (type); \
912 (x)->objects_per_page = OBJECTS_PER_PAGE (type); \
913 (x)->objects_last_page = OBJECTS_PER_PAGE (type); \
914 } while (0)
916 /* Master type for indexes within the symbol table. */
917 typedef unsigned long symint_t;
920 /* Linked list support for nested scopes (file, block, structure, etc.). */
921 typedef struct scope {
922 struct scope *prev; /* previous scope level */
923 struct scope *free; /* free list pointer */
924 SYMR *lsym; /* pointer to local symbol node */
925 symint_t lnumber; /* lsym index */
926 st_t type; /* type of the node */
927 } scope_t;
930 /* Forward reference list for tags referenced, but not yet defined. */
931 typedef struct forward {
932 struct forward *next; /* next forward reference */
933 struct forward *free; /* free list pointer */
934 AUXU *ifd_ptr; /* pointer to store file index */
935 AUXU *index_ptr; /* pointer to store symbol index */
936 AUXU *type_ptr; /* pointer to munge type info */
937 } forward_t;
940 /* Linked list support for tags. The first tag in the list is always
941 the current tag for that block. */
942 typedef struct tag {
943 struct tag *free; /* free list pointer */
944 struct shash *hash_ptr; /* pointer to the hash table head */
945 struct tag *same_name; /* tag with same name in outer scope */
946 struct tag *same_block; /* next tag defined in the same block. */
947 struct forward *forward_ref; /* list of forward references */
948 bt_t basic_type; /* bt_Struct, bt_Union, or bt_Enum */
949 symint_t ifd; /* file # tag defined in */
950 symint_t indx; /* index within file's local symbols */
951 } tag_t;
954 /* Head of a block's linked list of tags. */
955 typedef struct thead {
956 struct thead *prev; /* previous block */
957 struct thead *free; /* free list pointer */
958 struct tag *first_tag; /* first tag in block defined */
959 } thead_t;
962 /* Union containing pointers to each the small structures which are freed up. */
963 typedef union small_free {
964 scope_t *f_scope; /* scope structure */
965 thead_t *f_thead; /* tag head structure */
966 tag_t *f_tag; /* tag element structure */
967 forward_t *f_forward; /* forward tag reference */
968 } small_free_t;
971 /* String hash table support. The size of the hash table must fit
972 within a page. */
974 #define SHASH_SIZE 511
976 #define HASH_LEN_MAX ((1 << 12) - 1) /* Max length we can store */
978 typedef struct shash {
979 struct shash *next; /* next hash value */
980 char *string; /* string we are hashing */
981 symint_t len; /* string length */
982 symint_t indx; /* index within string table */
983 EXTR *esym_ptr; /* global symbol pointer */
984 SYMR *sym_ptr; /* local symbol pointer */
985 SYMR *end_ptr; /* symbol pointer to end block */
986 tag_t *tag_ptr; /* tag pointer */
987 PDR *proc_ptr; /* procedure descriptor pointer */
988 } shash_t;
991 /* Type hash table support. The size of the hash table must fit
992 within a page with the other extended file descriptor information.
993 Because unique types which are hashed are fewer in number than
994 strings, we use a smaller hash value. */
996 #define THASH_SIZE 55
998 typedef struct thash {
999 struct thash *next; /* next hash value */
1000 AUXU type; /* type we are hashing */
1001 symint_t indx; /* index within string table */
1002 } thash_t;
1005 /* Extended file descriptor that contains all of the support necessary
1006 to add things to each file separately. */
1007 typedef struct efdr {
1008 FDR fdr; /* File header to be written out */
1009 FDR *orig_fdr; /* original file header */
1010 char *name; /* filename */
1011 int name_len; /* length of the filename */
1012 symint_t void_type; /* aux. pointer to 'void' type */
1013 symint_t int_type; /* aux. pointer to 'int' type */
1014 scope_t *cur_scope; /* current nested scopes */
1015 symint_t file_index; /* current file number */
1016 int nested_scopes; /* # nested scopes */
1017 varray_t strings; /* local strings */
1018 varray_t symbols; /* local symbols */
1019 varray_t procs; /* procedures */
1020 varray_t aux_syms; /* auxiliary symbols */
1021 struct efdr *next_file; /* next file descriptor */
1022 /* string/type hash tables */
1023 shash_t **shash_head; /* string hash table */
1024 thash_t *thash_head[THASH_SIZE];
1025 } efdr_t;
1027 /* Pre-initialized extended file structure. */
1028 static int init_file_initialized = 0;
1029 static efdr_t init_file;
1031 static efdr_t *first_file; /* first file descriptor */
1032 static efdr_t **last_file_ptr = &first_file; /* file descriptor tail */
1035 /* Union of various things that are held in pages. */
1036 typedef union page {
1037 char byte [ PAGE_SIZE ];
1038 unsigned char ubyte [ PAGE_SIZE ];
1039 efdr_t file [ PAGE_SIZE / sizeof (efdr_t) ];
1040 FDR ofile [ PAGE_SIZE / sizeof (FDR) ];
1041 PDR proc [ PAGE_SIZE / sizeof (PDR) ];
1042 SYMR sym [ PAGE_SIZE / sizeof (SYMR) ];
1043 EXTR esym [ PAGE_SIZE / sizeof (EXTR) ];
1044 AUXU aux [ PAGE_SIZE / sizeof (AUXU) ];
1045 DNR dense [ PAGE_SIZE / sizeof (DNR) ];
1046 scope_t scope [ PAGE_SIZE / sizeof (scope_t) ];
1047 vlinks_t vlinks [ PAGE_SIZE / sizeof (vlinks_t) ];
1048 shash_t shash [ PAGE_SIZE / sizeof (shash_t) ];
1049 thash_t thash [ PAGE_SIZE / sizeof (thash_t) ];
1050 tag_t tag [ PAGE_SIZE / sizeof (tag_t) ];
1051 forward_t forward [ PAGE_SIZE / sizeof (forward_t) ];
1052 thead_t thead [ PAGE_SIZE / sizeof (thead_t) ];
1053 } page_t;
1056 /* Structure holding allocation information for small sized structures. */
1057 typedef struct alloc_info {
1058 const char *alloc_name; /* name of this allocation type (must be first) */
1059 page_t *cur_page; /* current page being allocated from */
1060 small_free_t free_list; /* current free list if any */
1061 int unallocated; /* number of elements unallocated on page */
1062 int total_alloc; /* total number of allocations */
1063 int total_free; /* total number of frees */
1064 int total_pages; /* total number of pages allocated */
1065 } alloc_info_t;
1067 /* Type information collected together. */
1068 typedef struct type_info {
1069 bt_t basic_type; /* basic type */
1070 coff_type_t orig_type; /* original COFF-based type */
1071 int num_tq; /* # type qualifiers */
1072 int num_dims; /* # dimensions */
1073 int num_sizes; /* # sizes */
1074 int extra_sizes; /* # extra sizes not tied with dims */
1075 tag_t * tag_ptr; /* tag pointer */
1076 int bitfield; /* symbol is a bitfield */
1077 int unknown_tag; /* this is an unknown tag */
1078 tq_t type_qualifiers[N_TQ]; /* type qualifiers (ptr, func, array)*/
1079 symint_t dimensions [N_TQ]; /* dimensions for each array */
1080 symint_t sizes [N_TQ+2]; /* sizes of each array slice + size of
1081 struct/union/enum + bitfield size */
1082 } type_info_t;
1084 /* Pre-initialized type_info struct. */
1085 static type_info_t type_info_init = {
1086 bt_Nil, /* basic type */
1087 T_NULL, /* original COFF-based type */
1088 0, /* # type qualifiers */
1089 0, /* # dimensions */
1090 0, /* # sizes */
1091 0, /* sizes not tied with dims */
1092 NULL, /* ptr to tag */
1093 0, /* bitfield */
1094 0, /* unknown tag */
1095 { /* type qualifiers */
1096 tq_Nil,
1097 tq_Nil,
1098 tq_Nil,
1099 tq_Nil,
1100 tq_Nil,
1101 tq_Nil,
1103 { /* dimensions */
1111 { /* sizes */
1124 /* Global virtual arrays & hash table for external strings as well as
1125 for the tags table and global tables for file descriptors, and
1126 dense numbers. */
1128 static varray_t file_desc = INIT_VARRAY (efdr_t);
1129 static varray_t dense_num = INIT_VARRAY (DNR);
1130 static varray_t tag_strings = INIT_VARRAY (char);
1131 static varray_t ext_strings = INIT_VARRAY (char);
1132 static varray_t ext_symbols = INIT_VARRAY (EXTR);
1134 static shash_t *orig_str_hash[SHASH_SIZE];
1135 static shash_t *ext_str_hash [SHASH_SIZE];
1136 static shash_t *tag_hash [SHASH_SIZE];
1138 /* Static types for int and void. Also, remember the last function's
1139 type (which is set up when we encounter the declaration for the
1140 function, and used when the end block for the function is emitted. */
1142 static type_info_t int_type_info;
1143 static type_info_t void_type_info;
1144 static type_info_t last_func_type_info;
1145 static EXTR *last_func_eptr;
1148 /* Convert COFF basic type to ECOFF basic type. The T_NULL type
1149 really should use bt_Void, but this causes the current ecoff GDB to
1150 issue unsupported type messages, and the Ultrix 4.00 dbx (aka MIPS
1151 2.0) doesn't understand it, even though the compiler generates it.
1152 Maybe this will be fixed in 2.10 or 2.20 of the MIPS compiler
1153 suite, but for now go with what works. */
1155 static const bt_t map_coff_types[ (int) T_MAX ] = {
1156 bt_Nil, /* T_NULL */
1157 bt_Nil, /* T_ARG */
1158 bt_Char, /* T_CHAR */
1159 bt_Short, /* T_SHORT */
1160 bt_Int, /* T_INT */
1161 bt_Long, /* T_LONG */
1162 bt_Float, /* T_FLOAT */
1163 bt_Double, /* T_DOUBLE */
1164 bt_Struct, /* T_STRUCT */
1165 bt_Union, /* T_UNION */
1166 bt_Enum, /* T_ENUM */
1167 bt_Enum, /* T_MOE */
1168 bt_UChar, /* T_UCHAR */
1169 bt_UShort, /* T_USHORT */
1170 bt_UInt, /* T_UINT */
1171 bt_ULong /* T_ULONG */
1174 /* Convert COFF storage class to ECOFF storage class. */
1175 static const sc_t map_coff_storage[ (int) C_MAX ] = {
1176 sc_Nil, /* 0: C_NULL */
1177 sc_Abs, /* 1: C_AUTO auto var */
1178 sc_Undefined, /* 2: C_EXT external */
1179 sc_Data, /* 3: C_STAT static */
1180 sc_Register, /* 4: C_REG register */
1181 sc_Undefined, /* 5: C_EXTDEF ??? */
1182 sc_Text, /* 6: C_LABEL label */
1183 sc_Text, /* 7: C_ULABEL user label */
1184 sc_Info, /* 8: C_MOS member of struct */
1185 sc_Abs, /* 9: C_ARG argument */
1186 sc_Info, /* 10: C_STRTAG struct tag */
1187 sc_Info, /* 11: C_MOU member of union */
1188 sc_Info, /* 12: C_UNTAG union tag */
1189 sc_Info, /* 13: C_TPDEF typedef */
1190 sc_Data, /* 14: C_USTATIC ??? */
1191 sc_Info, /* 15: C_ENTAG enum tag */
1192 sc_Info, /* 16: C_MOE member of enum */
1193 sc_Register, /* 17: C_REGPARM register parameter */
1194 sc_Bits, /* 18; C_FIELD bitfield */
1195 sc_Nil, /* 19 */
1196 sc_Nil, /* 20 */
1197 sc_Nil, /* 21 */
1198 sc_Nil, /* 22 */
1199 sc_Nil, /* 23 */
1200 sc_Nil, /* 24 */
1201 sc_Nil, /* 25 */
1202 sc_Nil, /* 26 */
1203 sc_Nil, /* 27 */
1204 sc_Nil, /* 28 */
1205 sc_Nil, /* 29 */
1206 sc_Nil, /* 30 */
1207 sc_Nil, /* 31 */
1208 sc_Nil, /* 32 */
1209 sc_Nil, /* 33 */
1210 sc_Nil, /* 34 */
1211 sc_Nil, /* 35 */
1212 sc_Nil, /* 36 */
1213 sc_Nil, /* 37 */
1214 sc_Nil, /* 38 */
1215 sc_Nil, /* 39 */
1216 sc_Nil, /* 40 */
1217 sc_Nil, /* 41 */
1218 sc_Nil, /* 42 */
1219 sc_Nil, /* 43 */
1220 sc_Nil, /* 44 */
1221 sc_Nil, /* 45 */
1222 sc_Nil, /* 46 */
1223 sc_Nil, /* 47 */
1224 sc_Nil, /* 48 */
1225 sc_Nil, /* 49 */
1226 sc_Nil, /* 50 */
1227 sc_Nil, /* 51 */
1228 sc_Nil, /* 52 */
1229 sc_Nil, /* 53 */
1230 sc_Nil, /* 54 */
1231 sc_Nil, /* 55 */
1232 sc_Nil, /* 56 */
1233 sc_Nil, /* 57 */
1234 sc_Nil, /* 58 */
1235 sc_Nil, /* 59 */
1236 sc_Nil, /* 60 */
1237 sc_Nil, /* 61 */
1238 sc_Nil, /* 62 */
1239 sc_Nil, /* 63 */
1240 sc_Nil, /* 64 */
1241 sc_Nil, /* 65 */
1242 sc_Nil, /* 66 */
1243 sc_Nil, /* 67 */
1244 sc_Nil, /* 68 */
1245 sc_Nil, /* 69 */
1246 sc_Nil, /* 70 */
1247 sc_Nil, /* 71 */
1248 sc_Nil, /* 72 */
1249 sc_Nil, /* 73 */
1250 sc_Nil, /* 74 */
1251 sc_Nil, /* 75 */
1252 sc_Nil, /* 76 */
1253 sc_Nil, /* 77 */
1254 sc_Nil, /* 78 */
1255 sc_Nil, /* 79 */
1256 sc_Nil, /* 80 */
1257 sc_Nil, /* 81 */
1258 sc_Nil, /* 82 */
1259 sc_Nil, /* 83 */
1260 sc_Nil, /* 84 */
1261 sc_Nil, /* 85 */
1262 sc_Nil, /* 86 */
1263 sc_Nil, /* 87 */
1264 sc_Nil, /* 88 */
1265 sc_Nil, /* 89 */
1266 sc_Nil, /* 90 */
1267 sc_Nil, /* 91 */
1268 sc_Nil, /* 92 */
1269 sc_Nil, /* 93 */
1270 sc_Nil, /* 94 */
1271 sc_Nil, /* 95 */
1272 sc_Nil, /* 96 */
1273 sc_Nil, /* 97 */
1274 sc_Nil, /* 98 */
1275 sc_Nil, /* 99 */
1276 sc_Text, /* 100: C_BLOCK block start/end */
1277 sc_Text, /* 101: C_FCN function start/end */
1278 sc_Info, /* 102: C_EOS end of struct/union/enum */
1279 sc_Nil, /* 103: C_FILE file start */
1280 sc_Nil, /* 104: C_LINE line number */
1281 sc_Nil, /* 105: C_ALIAS combined type info */
1282 sc_Nil, /* 106: C_HIDDEN ??? */
1285 /* Convert COFF storage class to ECOFF symbol type. */
1286 static const st_t map_coff_sym_type[ (int) C_MAX ] = {
1287 st_Nil, /* 0: C_NULL */
1288 st_Local, /* 1: C_AUTO auto var */
1289 st_Global, /* 2: C_EXT external */
1290 st_Static, /* 3: C_STAT static */
1291 st_Local, /* 4: C_REG register */
1292 st_Global, /* 5: C_EXTDEF ??? */
1293 st_Label, /* 6: C_LABEL label */
1294 st_Label, /* 7: C_ULABEL user label */
1295 st_Member, /* 8: C_MOS member of struct */
1296 st_Param, /* 9: C_ARG argument */
1297 st_Block, /* 10: C_STRTAG struct tag */
1298 st_Member, /* 11: C_MOU member of union */
1299 st_Block, /* 12: C_UNTAG union tag */
1300 st_Typedef, /* 13: C_TPDEF typedef */
1301 st_Static, /* 14: C_USTATIC ??? */
1302 st_Block, /* 15: C_ENTAG enum tag */
1303 st_Member, /* 16: C_MOE member of enum */
1304 st_Param, /* 17: C_REGPARM register parameter */
1305 st_Member, /* 18; C_FIELD bitfield */
1306 st_Nil, /* 19 */
1307 st_Nil, /* 20 */
1308 st_Nil, /* 21 */
1309 st_Nil, /* 22 */
1310 st_Nil, /* 23 */
1311 st_Nil, /* 24 */
1312 st_Nil, /* 25 */
1313 st_Nil, /* 26 */
1314 st_Nil, /* 27 */
1315 st_Nil, /* 28 */
1316 st_Nil, /* 29 */
1317 st_Nil, /* 30 */
1318 st_Nil, /* 31 */
1319 st_Nil, /* 32 */
1320 st_Nil, /* 33 */
1321 st_Nil, /* 34 */
1322 st_Nil, /* 35 */
1323 st_Nil, /* 36 */
1324 st_Nil, /* 37 */
1325 st_Nil, /* 38 */
1326 st_Nil, /* 39 */
1327 st_Nil, /* 40 */
1328 st_Nil, /* 41 */
1329 st_Nil, /* 42 */
1330 st_Nil, /* 43 */
1331 st_Nil, /* 44 */
1332 st_Nil, /* 45 */
1333 st_Nil, /* 46 */
1334 st_Nil, /* 47 */
1335 st_Nil, /* 48 */
1336 st_Nil, /* 49 */
1337 st_Nil, /* 50 */
1338 st_Nil, /* 51 */
1339 st_Nil, /* 52 */
1340 st_Nil, /* 53 */
1341 st_Nil, /* 54 */
1342 st_Nil, /* 55 */
1343 st_Nil, /* 56 */
1344 st_Nil, /* 57 */
1345 st_Nil, /* 58 */
1346 st_Nil, /* 59 */
1347 st_Nil, /* 60 */
1348 st_Nil, /* 61 */
1349 st_Nil, /* 62 */
1350 st_Nil, /* 63 */
1351 st_Nil, /* 64 */
1352 st_Nil, /* 65 */
1353 st_Nil, /* 66 */
1354 st_Nil, /* 67 */
1355 st_Nil, /* 68 */
1356 st_Nil, /* 69 */
1357 st_Nil, /* 70 */
1358 st_Nil, /* 71 */
1359 st_Nil, /* 72 */
1360 st_Nil, /* 73 */
1361 st_Nil, /* 74 */
1362 st_Nil, /* 75 */
1363 st_Nil, /* 76 */
1364 st_Nil, /* 77 */
1365 st_Nil, /* 78 */
1366 st_Nil, /* 79 */
1367 st_Nil, /* 80 */
1368 st_Nil, /* 81 */
1369 st_Nil, /* 82 */
1370 st_Nil, /* 83 */
1371 st_Nil, /* 84 */
1372 st_Nil, /* 85 */
1373 st_Nil, /* 86 */
1374 st_Nil, /* 87 */
1375 st_Nil, /* 88 */
1376 st_Nil, /* 89 */
1377 st_Nil, /* 90 */
1378 st_Nil, /* 91 */
1379 st_Nil, /* 92 */
1380 st_Nil, /* 93 */
1381 st_Nil, /* 94 */
1382 st_Nil, /* 95 */
1383 st_Nil, /* 96 */
1384 st_Nil, /* 97 */
1385 st_Nil, /* 98 */
1386 st_Nil, /* 99 */
1387 st_Block, /* 100: C_BLOCK block start/end */
1388 st_Proc, /* 101: C_FCN function start/end */
1389 st_End, /* 102: C_EOS end of struct/union/enum */
1390 st_File, /* 103: C_FILE file start */
1391 st_Nil, /* 104: C_LINE line number */
1392 st_Nil, /* 105: C_ALIAS combined type info */
1393 st_Nil, /* 106: C_HIDDEN ??? */
1396 /* Map COFF derived types to ECOFF type qualifiers. */
1397 static const tq_t map_coff_derived_type[ (int) DT_MAX ] = {
1398 tq_Nil, /* 0: DT_NON no more qualifiers */
1399 tq_Ptr, /* 1: DT_PTR pointer */
1400 tq_Proc, /* 2: DT_FCN function */
1401 tq_Array, /* 3: DT_ARY array */
1405 /* Keep track of different sized allocation requests. */
1406 static alloc_info_t alloc_counts[ (int) alloc_type_last ];
1409 /* Pointers and such to the original symbol table that is read in. */
1410 static struct filehdr orig_file_header; /* global object file header */
1412 static HDRR orig_sym_hdr; /* symbolic header on input */
1413 static char *orig_linenum; /* line numbers */
1414 static DNR *orig_dense; /* dense numbers */
1415 static PDR *orig_procs; /* procedures */
1416 static SYMR *orig_local_syms; /* local symbols */
1417 static OPTR *orig_opt_syms; /* optimization symbols */
1418 static AUXU *orig_aux_syms; /* auxiliary symbols */
1419 static char *orig_local_strs; /* local strings */
1420 static char *orig_ext_strs; /* external strings */
1421 static FDR *orig_files; /* file descriptors */
1422 static symint_t *orig_rfds; /* relative file desc's */
1423 static EXTR *orig_ext_syms; /* external symbols */
1425 /* Macros to convert an index into a given object within the original
1426 symbol table. */
1427 #define CHECK(num,max,str) \
1428 (((unsigned long) num > (unsigned long) max) ? out_of_bounds (num, max, str, __LINE__) : 0)
1430 #define ORIG_LINENUM(indx) (CHECK ((indx), orig_sym_hdr.cbLine, "line#"), (indx) + orig_linenum)
1431 #define ORIG_DENSE(indx) (CHECK ((indx), orig_sym_hdr.idnMax, "dense"), (indx) + orig_dense)
1432 #define ORIG_PROCS(indx) (CHECK ((indx), orig_sym_hdr.ipdMax, "procs"), (indx) + orig_procs)
1433 #define ORIG_FILES(indx) (CHECK ((indx), orig_sym_hdr.ifdMax, "funcs"), (indx) + orig_files)
1434 #define ORIG_LSYMS(indx) (CHECK ((indx), orig_sym_hdr.isymMax, "lsyms"), (indx) + orig_local_syms)
1435 #define ORIG_LSTRS(indx) (CHECK ((indx), orig_sym_hdr.issMax, "lstrs"), (indx) + orig_local_strs)
1436 #define ORIG_ESYMS(indx) (CHECK ((indx), orig_sym_hdr.iextMax, "esyms"), (indx) + orig_ext_syms)
1437 #define ORIG_ESTRS(indx) (CHECK ((indx), orig_sym_hdr.issExtMax, "estrs"), (indx) + orig_ext_strs)
1438 #define ORIG_OPT(indx) (CHECK ((indx), orig_sym_hdr.ioptMax, "opt"), (indx) + orig_opt_syms)
1439 #define ORIG_AUX(indx) (CHECK ((indx), orig_sym_hdr.iauxMax, "aux"), (indx) + orig_aux_syms)
1440 #define ORIG_RFDS(indx) (CHECK ((indx), orig_sym_hdr.crfd, "rfds"), (indx) + orig_rfds)
1442 /* Various other statics. */
1443 static HDRR symbolic_header; /* symbolic header */
1444 static efdr_t *cur_file_ptr = (efdr_t *) 0; /* current file desc. header */
1445 static PDR *cur_proc_ptr = (PDR *) 0; /* current procedure header */
1446 static SYMR *cur_oproc_begin = (SYMR *) 0; /* original proc. sym begin info */
1447 static SYMR *cur_oproc_end = (SYMR *) 0; /* original proc. sym end info */
1448 static PDR *cur_oproc_ptr = (PDR *) 0; /* current original procedure*/
1449 static thead_t *cur_tag_head = (thead_t *) 0;/* current tag head */
1450 static unsigned long file_offset = 0; /* current file offset */
1451 static unsigned long max_file_offset = 0; /* maximum file offset */
1452 static FILE *object_stream = (FILE *) 0; /* file desc. to output .o */
1453 static FILE *obj_in_stream = (FILE *) 0; /* file desc. to input .o */
1454 static const char *progname = (const char *) 0;/* program name for errors */
1455 static const char *input_name = "stdin"; /* name of input file */
1456 static char *object_name = (char *) 0; /* tmp. name of object file */
1457 static char *obj_in_name = (char *) 0; /* name of input object file */
1458 static char *cur_line_start = (char *) 0; /* current line read in */
1459 static char *cur_line_ptr = (char *) 0; /* ptr within current line */
1460 static unsigned cur_line_nbytes = 0; /* # bytes for current line */
1461 static unsigned cur_line_alloc = 0; /* # bytes total in buffer */
1462 static long line_number = 0; /* current input line number */
1463 static int debug = 0; /* trace functions */
1464 static int version = 0; /* print version # */
1465 static int verbose = 0;
1466 static int had_errors = 0; /* != 0 if errors were found */
1467 static int rename_output = 0; /* != 0 if rename output file*/
1468 static int delete_input = 0; /* != 0 if delete input after done */
1469 static int stabs_seen = 0; /* != 0 if stabs have been seen */
1472 /* Pseudo symbol to use when putting stabs into the symbol table. */
1473 #ifndef STABS_SYMBOL
1474 #define STABS_SYMBOL "@stabs"
1475 #endif
1477 static const char stabs_symbol[] = STABS_SYMBOL;
1480 /* Forward reference for functions. See the definition for more details. */
1482 static int out_of_bounds (symint_t, symint_t, const char *, int);
1483 static shash_t *hash_string (const char *, ptrdiff_t, shash_t **, symint_t *);
1484 static symint_t add_string (varray_t *, shash_t **, const char *, const char *,
1485 shash_t **);
1486 static symint_t add_local_symbol (const char *, const char *, st_t, sc_t,
1487 symint_t, symint_t);
1488 static symint_t add_ext_symbol (EXTR *, int);
1489 static symint_t add_aux_sym_symint (symint_t);
1490 static symint_t add_aux_sym_rndx (int, symint_t);
1491 static symint_t add_aux_sym_tir (type_info_t *, hash_state_t, thash_t **);
1492 static tag_t * get_tag (const char *, const char *, symint_t, bt_t);
1493 static void add_unknown_tag (tag_t *);
1494 static void add_procedure (const char *, const char *);
1495 static void initialize_init_file (void);
1496 static void add_file (const char *, const char *);
1497 static void add_bytes (varray_t *, char *, size_t);
1498 static void add_varray_page (varray_t *);
1499 static void update_headers (void);
1500 static void write_varray (varray_t *, off_t, const char *);
1501 static void write_object (void);
1502 static const char *st_to_string (st_t);
1503 static const char *sc_to_string (sc_t);
1504 static char *read_line (void);
1505 static void parse_input (void);
1506 static void mark_stabs (const char *);
1507 static void parse_begin (const char *);
1508 static void parse_bend (const char *);
1509 static void parse_def (const char *);
1510 static void parse_end (const char *);
1511 static void parse_ent (const char *);
1512 static void parse_file (const char *);
1513 static void parse_stabs_common (const char *, const char *, const char *);
1514 static void parse_stabs (const char *);
1515 static void parse_stabn (const char *);
1516 static page_t *read_seek (size_t, off_t, const char *);
1517 static void copy_object (void);
1519 static void catch_signal (int) ATTRIBUTE_NORETURN;
1520 static page_t *allocate_page (void);
1521 static page_t *allocate_multiple_pages (size_t);
1522 static void free_multiple_pages (page_t *, size_t);
1524 #ifndef MALLOC_CHECK
1525 static page_t *allocate_cluster (size_t);
1526 #endif
1528 static forward_t *allocate_forward (void);
1529 static scope_t *allocate_scope (void);
1530 static shash_t *allocate_shash (void);
1531 static tag_t *allocate_tag (void);
1532 static thash_t *allocate_thash (void);
1533 static thead_t *allocate_thead (void);
1534 static vlinks_t *allocate_vlinks (void);
1536 static void free_forward (forward_t *);
1537 static void free_scope (scope_t *);
1538 static void free_tag (tag_t *);
1539 static void free_thead (thead_t *);
1541 extern char *optarg;
1542 extern int optind;
1543 extern int opterr;
1545 /* List of assembler pseudo ops and beginning sequences that need
1546 special actions. Someday, this should be a hash table, and such,
1547 but for now a linear list of names and calls to memcmp will
1548 do...... */
1550 typedef struct _pseudo_ops {
1551 const char *const name; /* pseudo-op in ascii */
1552 const int len; /* length of name to compare */
1553 void (*const func) (const char *); /* function to handle line */
1554 } pseudo_ops_t;
1556 static const pseudo_ops_t pseudo_ops[] = {
1557 { "#.def", sizeof("#.def")-1, parse_def },
1558 { "#.begin", sizeof("#.begin")-1, parse_begin },
1559 { "#.bend", sizeof("#.bend")-1, parse_bend },
1560 { ".end", sizeof(".end")-1, parse_end },
1561 { ".ent", sizeof(".ent")-1, parse_ent },
1562 { ".file", sizeof(".file")-1, parse_file },
1563 { "#.stabs", sizeof("#.stabs")-1, parse_stabs },
1564 { "#.stabn", sizeof("#.stabn")-1, parse_stabn },
1565 { ".stabs", sizeof(".stabs")-1, parse_stabs },
1566 { ".stabn", sizeof(".stabn")-1, parse_stabn },
1567 { "#@stabs", sizeof("#@stabs")-1, mark_stabs },
1571 /* Command line options for getopt_long. */
1573 static const struct option options[] =
1575 { "version", 0, 0, 'V' },
1576 { "verbose", 0, 0, 'v' },
1577 { 0, 0, 0, 0 }
1580 /* Add a page to a varray object. */
1582 static void
1583 add_varray_page (varray_t *vp)
1585 vlinks_t *new_links = allocate_vlinks ();
1587 #ifdef MALLOC_CHECK
1588 if (vp->object_size > 1)
1589 new_links->datum = xcalloc (1, vp->object_size);
1590 else
1591 #endif
1592 new_links->datum = allocate_page ();
1594 alloc_counts[ (int) alloc_type_varray ].total_alloc++;
1595 alloc_counts[ (int) alloc_type_varray ].total_pages++;
1597 new_links->start_index = vp->num_allocated;
1598 vp->objects_last_page = 0;
1600 if (vp->first == (vlinks_t *) 0) /* first allocation? */
1601 vp->first = vp->last = new_links;
1602 else
1603 { /* 2nd or greater allocation */
1604 new_links->prev = vp->last;
1605 vp->last->next = new_links;
1606 vp->last = new_links;
1611 /* Compute hash code (from tree.c) */
1613 #define HASHBITS 30
1615 static shash_t *
1616 hash_string (const char *text, ptrdiff_t hash_len, shash_t **hash_tbl,
1617 symint_t *ret_hash_index)
1619 unsigned long hi;
1620 ptrdiff_t i;
1621 shash_t *ptr;
1622 int first_ch = *text;
1624 hi = hash_len;
1625 for (i = 0; i < hash_len; i++)
1626 hi = ((hi & 0x003fffff) * 613) + (text[i] & 0xff);
1628 hi &= (1 << HASHBITS) - 1;
1629 hi %= SHASH_SIZE;
1631 if (ret_hash_index != (symint_t *) 0)
1632 *ret_hash_index = hi;
1634 for (ptr = hash_tbl[hi]; ptr != (shash_t *) 0; ptr = ptr->next)
1635 if ((symint_t) hash_len == ptr->len
1636 && first_ch == ptr->string[0]
1637 && memcmp (text, ptr->string, hash_len) == 0)
1638 break;
1640 return ptr;
1644 /* Add a string (and null pad) to one of the string tables. A
1645 consequence of hashing strings, is that we don't let strings cross
1646 page boundaries. The extra nulls will be ignored. VP is a string
1647 virtual array, HASH_TBL a pointer to the hash table, the string
1648 starts at START and the position one byte after the string is given
1649 with END_P1, the resulting hash pointer is returned in RET_HASH. */
1651 static symint_t
1652 add_string (varray_t *vp, shash_t **hash_tbl, const char *start,
1653 const char *end_p1, shash_t **ret_hash)
1655 ptrdiff_t len = end_p1 - start;
1656 shash_t *hash_ptr;
1657 symint_t hi;
1659 if (len >= (ptrdiff_t) PAGE_USIZE)
1660 fatal ("string too big (%ld bytes)", (long) len);
1662 hash_ptr = hash_string (start, len, hash_tbl, &hi);
1663 if (hash_ptr == (shash_t *) 0)
1665 char *p;
1667 if (vp->objects_last_page + len >= (long) PAGE_USIZE)
1669 vp->num_allocated
1670 = ((vp->num_allocated + PAGE_USIZE - 1) / PAGE_USIZE) * PAGE_USIZE;
1671 add_varray_page (vp);
1674 hash_ptr = allocate_shash ();
1675 hash_ptr->next = hash_tbl[hi];
1676 hash_tbl[hi] = hash_ptr;
1678 hash_ptr->len = len;
1679 hash_ptr->indx = vp->num_allocated;
1680 hash_ptr->string = p = & vp->last->datum->byte[ vp->objects_last_page ];
1682 vp->objects_last_page += len+1;
1683 vp->num_allocated += len+1;
1685 while (len-- > 0)
1686 *p++ = *start++;
1688 *p = '\0';
1691 if (ret_hash != (shash_t **) 0)
1692 *ret_hash = hash_ptr;
1694 return hash_ptr->indx;
1698 /* Add a local symbol. The symbol string starts at STR_START and the
1699 first byte after it is marked by STR_END_P1. The symbol has type
1700 TYPE and storage class STORAGE and value VALUE. INDX is an index
1701 to local/aux. symbols. */
1703 static symint_t
1704 add_local_symbol (const char *str_start, const char *str_end_p1, st_t type,
1705 sc_t storage, symint_t value, symint_t indx)
1707 symint_t ret;
1708 SYMR *psym;
1709 scope_t *pscope;
1710 thead_t *ptag_head;
1711 tag_t *ptag;
1712 tag_t *ptag_next;
1713 varray_t *vp = &cur_file_ptr->symbols;
1714 int scope_delta = 0;
1715 shash_t *hash_ptr = (shash_t *) 0;
1717 if (vp->objects_last_page == vp->objects_per_page)
1718 add_varray_page (vp);
1720 psym = &vp->last->datum->sym[ vp->objects_last_page++ ];
1722 psym->value = value;
1723 psym->st = (unsigned) type;
1724 psym->sc = (unsigned) storage;
1725 psym->index = indx;
1726 psym->iss = (str_start == (const char *) 0)
1728 : add_string (&cur_file_ptr->strings,
1729 &cur_file_ptr->shash_head[0],
1730 str_start,
1731 str_end_p1,
1732 &hash_ptr);
1734 ret = vp->num_allocated++;
1736 if (MIPS_IS_STAB (psym))
1737 return ret;
1739 /* Save the symbol within the hash table if this is a static
1740 item, and it has a name. */
1741 if (hash_ptr != (shash_t *) 0
1742 && (type == st_Global || type == st_Static || type == st_Label
1743 || type == st_Proc || type == st_StaticProc))
1744 hash_ptr->sym_ptr = psym;
1746 /* push or pop a scope if appropriate. */
1747 switch (type)
1749 default:
1750 break;
1752 case st_File: /* beginning of file */
1753 case st_Proc: /* procedure */
1754 case st_StaticProc: /* static procedure */
1755 case st_Block: /* begin scope */
1756 pscope = allocate_scope ();
1757 pscope->prev = cur_file_ptr->cur_scope;
1758 pscope->lsym = psym;
1759 pscope->lnumber = ret;
1760 pscope->type = type;
1761 cur_file_ptr->cur_scope = pscope;
1763 if (type != st_File)
1764 scope_delta = 1;
1766 /* For every block type except file, struct, union, or
1767 enumeration blocks, push a level on the tag stack. We omit
1768 file types, so that tags can span file boundaries. */
1769 if (type != st_File && storage != sc_Info)
1771 ptag_head = allocate_thead ();
1772 ptag_head->first_tag = 0;
1773 ptag_head->prev = cur_tag_head;
1774 cur_tag_head = ptag_head;
1776 break;
1778 case st_End:
1779 pscope = cur_file_ptr->cur_scope;
1780 if (pscope == (scope_t *) 0)
1781 error ("internal error, too many st_End's");
1783 else
1785 st_t begin_type = (st_t) pscope->lsym->st;
1787 if (begin_type != st_File)
1788 scope_delta = -1;
1790 /* Except for file, structure, union, or enumeration end
1791 blocks remove all tags created within this scope. */
1792 if (begin_type != st_File && storage != sc_Info)
1794 ptag_head = cur_tag_head;
1795 cur_tag_head = ptag_head->prev;
1797 for (ptag = ptag_head->first_tag;
1798 ptag != (tag_t *) 0;
1799 ptag = ptag_next)
1801 if (ptag->forward_ref != (forward_t *) 0)
1802 add_unknown_tag (ptag);
1804 ptag_next = ptag->same_block;
1805 ptag->hash_ptr->tag_ptr = ptag->same_name;
1806 free_tag (ptag);
1809 free_thead (ptag_head);
1812 cur_file_ptr->cur_scope = pscope->prev;
1813 psym->index = pscope->lnumber; /* blk end gets begin sym # */
1815 if (storage != sc_Info)
1816 psym->iss = pscope->lsym->iss; /* blk end gets same name */
1818 if (begin_type == st_File || begin_type == st_Block)
1819 pscope->lsym->index = ret+1; /* block begin gets next sym # */
1821 /* Functions push two or more aux words as follows:
1822 1st word: index+1 of the end symbol
1823 2nd word: type of the function (plus any aux words needed).
1824 Also, tie the external pointer back to the function begin symbol. */
1825 else
1827 symint_t type;
1828 pscope->lsym->index = add_aux_sym_symint (ret+1);
1829 type = add_aux_sym_tir (&last_func_type_info,
1830 hash_no,
1831 &cur_file_ptr->thash_head[0]);
1832 if (last_func_eptr)
1834 last_func_eptr->ifd = cur_file_ptr->file_index;
1836 /* The index for an external st_Proc symbol is the index
1837 of the st_Proc symbol in the local symbol table. */
1838 last_func_eptr->asym.index = psym->index;
1842 free_scope (pscope);
1846 cur_file_ptr->nested_scopes += scope_delta;
1848 if (debug && type != st_File
1849 && (debug > 2 || type == st_Block || type == st_End
1850 || type == st_Proc || type == st_StaticProc))
1852 const char *sc_str = sc_to_string (storage);
1853 const char *st_str = st_to_string (type);
1854 int depth = cur_file_ptr->nested_scopes + (scope_delta < 0);
1856 fprintf (stderr,
1857 "\tlsym\tv= %10ld, depth= %2d, sc= %-12s",
1858 value, depth, sc_str);
1860 if (str_start && str_end_p1 - str_start > 0)
1861 fprintf (stderr, " st= %-11s name= %.*s\n",
1862 st_str, (int) (str_end_p1 - str_start), str_start);
1863 else
1865 size_t len = strlen (st_str);
1866 fprintf (stderr, " st= %.*s\n", (int) (len-1), st_str);
1870 return ret;
1874 /* Add an external symbol with symbol pointer ESYM and file index
1875 IFD. */
1877 static symint_t
1878 add_ext_symbol (EXTR *esym, int ifd)
1880 const char *str_start; /* first byte in string */
1881 const char *str_end_p1; /* first byte after string */
1882 EXTR *psym;
1883 varray_t *vp = &ext_symbols;
1884 shash_t *hash_ptr = (shash_t *) 0;
1886 str_start = ORIG_ESTRS (esym->asym.iss);
1887 str_end_p1 = str_start + strlen (str_start);
1889 if (debug > 1)
1891 long value = esym->asym.value;
1892 const char *sc_str = sc_to_string ((sc_t) esym->asym.sc);
1893 const char *st_str = st_to_string ((st_t) esym->asym.st);
1895 fprintf (stderr,
1896 "\tesym\tv= %10ld, ifd= %2d, sc= %-12s",
1897 value, ifd, sc_str);
1899 if (str_start && str_end_p1 - str_start > 0)
1900 fprintf (stderr, " st= %-11s name= %.*s\n",
1901 st_str, (int) (str_end_p1 - str_start), str_start);
1902 else
1903 fprintf (stderr, " st= %s\n", st_str);
1906 if (vp->objects_last_page == vp->objects_per_page)
1907 add_varray_page (vp);
1909 psym = &vp->last->datum->esym[ vp->objects_last_page++ ];
1911 *psym = *esym;
1912 psym->ifd = ifd;
1913 psym->asym.index = indexNil;
1914 psym->asym.iss = (str_start == (const char *) 0)
1916 : add_string (&ext_strings,
1917 &ext_str_hash[0],
1918 str_start,
1919 str_end_p1,
1920 &hash_ptr);
1922 hash_ptr->esym_ptr = psym;
1923 return vp->num_allocated++;
1927 /* Add an auxiliary symbol (passing a symint). */
1929 static symint_t
1930 add_aux_sym_symint (symint_t aux_word)
1932 AUXU *aux_ptr;
1933 efdr_t *file_ptr = cur_file_ptr;
1934 varray_t *vp = &file_ptr->aux_syms;
1936 if (vp->objects_last_page == vp->objects_per_page)
1937 add_varray_page (vp);
1939 aux_ptr = &vp->last->datum->aux[ vp->objects_last_page++ ];
1940 aux_ptr->isym = aux_word;
1942 return vp->num_allocated++;
1946 /* Add an auxiliary symbol (passing a file/symbol index combo). */
1948 static symint_t
1949 add_aux_sym_rndx (int file_index, symint_t sym_index)
1951 AUXU *aux_ptr;
1952 efdr_t *file_ptr = cur_file_ptr;
1953 varray_t *vp = &file_ptr->aux_syms;
1955 if (vp->objects_last_page == vp->objects_per_page)
1956 add_varray_page (vp);
1958 aux_ptr = &vp->last->datum->aux[ vp->objects_last_page++ ];
1959 aux_ptr->rndx.rfd = file_index;
1960 aux_ptr->rndx.index = sym_index;
1962 return vp->num_allocated++;
1966 /* Add an auxiliary symbol (passing the basic type and possibly
1967 type qualifiers). */
1969 static symint_t
1970 add_aux_sym_tir (type_info_t *t, hash_state_t state, thash_t **hash_tbl)
1972 AUXU *aux_ptr;
1973 efdr_t *file_ptr = cur_file_ptr;
1974 varray_t *vp = &file_ptr->aux_syms;
1975 static AUXU init_aux;
1976 symint_t ret;
1977 int i;
1978 AUXU aux;
1980 aux = init_aux;
1981 aux.ti.bt = (int) t->basic_type;
1982 aux.ti.continued = 0;
1983 aux.ti.fBitfield = t->bitfield;
1985 aux.ti.tq0 = (int) t->type_qualifiers[0];
1986 aux.ti.tq1 = (int) t->type_qualifiers[1];
1987 aux.ti.tq2 = (int) t->type_qualifiers[2];
1988 aux.ti.tq3 = (int) t->type_qualifiers[3];
1989 aux.ti.tq4 = (int) t->type_qualifiers[4];
1990 aux.ti.tq5 = (int) t->type_qualifiers[5];
1993 /* For anything that adds additional information, we must not hash,
1994 so check here, and reset our state. */
1996 if (state != hash_no
1997 && (t->type_qualifiers[0] == tq_Array
1998 || t->type_qualifiers[1] == tq_Array
1999 || t->type_qualifiers[2] == tq_Array
2000 || t->type_qualifiers[3] == tq_Array
2001 || t->type_qualifiers[4] == tq_Array
2002 || t->type_qualifiers[5] == tq_Array
2003 || t->basic_type == bt_Struct
2004 || t->basic_type == bt_Union
2005 || t->basic_type == bt_Enum
2006 || t->bitfield
2007 || t->num_dims > 0))
2008 state = hash_no;
2010 /* See if we can hash this type, and save some space, but some types
2011 can't be hashed (because they contain arrays or continuations),
2012 and others can be put into the hash list, but cannot use existing
2013 types because other aux entries precede this one. */
2015 if (state != hash_no)
2017 thash_t *hash_ptr;
2018 symint_t hi;
2020 hi = aux.isym & ((1 << HASHBITS) - 1);
2021 hi %= THASH_SIZE;
2023 for (hash_ptr = hash_tbl[hi];
2024 hash_ptr != (thash_t *) 0;
2025 hash_ptr = hash_ptr->next)
2027 if (aux.isym == hash_ptr->type.isym)
2028 break;
2031 if (hash_ptr != (thash_t *) 0 && state == hash_yes)
2032 return hash_ptr->indx;
2034 if (hash_ptr == (thash_t *) 0)
2036 hash_ptr = allocate_thash ();
2037 hash_ptr->next = hash_tbl[hi];
2038 hash_ptr->type = aux;
2039 hash_ptr->indx = vp->num_allocated;
2040 hash_tbl[hi] = hash_ptr;
2044 /* Everything is set up, add the aux symbol. */
2045 if (vp->objects_last_page == vp->objects_per_page)
2046 add_varray_page (vp);
2048 aux_ptr = &vp->last->datum->aux[ vp->objects_last_page++ ];
2049 *aux_ptr = aux;
2051 ret = vp->num_allocated++;
2053 /* Add bitfield length if it exists.
2055 NOTE: Mips documentation claims bitfield goes at the end of the
2056 AUX record, but the DECstation compiler emits it here.
2057 (This would only make a difference for enum bitfields.)
2059 Also note: We use the last size given since gcc may emit 2
2060 for an enum bitfield. */
2062 if (t->bitfield)
2063 (void) add_aux_sym_symint ((symint_t) t->sizes[t->num_sizes-1]);
2066 /* Add tag information if needed. Structure, union, and enum
2067 references add 2 aux symbols: a [file index, symbol index]
2068 pointer to the structure type, and the current file index. */
2070 if (t->basic_type == bt_Struct
2071 || t->basic_type == bt_Union
2072 || t->basic_type == bt_Enum)
2074 symint_t file_index = t->tag_ptr->ifd;
2075 symint_t sym_index = t->tag_ptr->indx;
2077 if (t->unknown_tag)
2079 (void) add_aux_sym_rndx (ST_RFDESCAPE, sym_index);
2080 (void) add_aux_sym_symint ((symint_t)-1);
2082 else if (sym_index != indexNil)
2084 (void) add_aux_sym_rndx (ST_RFDESCAPE, sym_index);
2085 (void) add_aux_sym_symint (file_index);
2087 else
2089 forward_t *forward_ref = allocate_forward ();
2091 forward_ref->type_ptr = aux_ptr;
2092 forward_ref->next = t->tag_ptr->forward_ref;
2093 t->tag_ptr->forward_ref = forward_ref;
2095 (void) add_aux_sym_rndx (ST_RFDESCAPE, sym_index);
2096 forward_ref->index_ptr
2097 = &vp->last->datum->aux[ vp->objects_last_page - 1];
2099 (void) add_aux_sym_symint (file_index);
2100 forward_ref->ifd_ptr
2101 = &vp->last->datum->aux[ vp->objects_last_page - 1];
2105 /* Add information about array bounds if they exist. */
2106 for (i = 0; i < t->num_dims; i++)
2108 (void) add_aux_sym_rndx (ST_RFDESCAPE,
2109 cur_file_ptr->int_type);
2111 (void) add_aux_sym_symint (cur_file_ptr->file_index); /* file index*/
2112 (void) add_aux_sym_symint ((symint_t) 0); /* low bound */
2113 (void) add_aux_sym_symint (t->dimensions[i] - 1); /* high bound*/
2114 (void) add_aux_sym_symint ((t->dimensions[i] == 0) /* stride */
2116 : (t->sizes[i] * 8) / t->dimensions[i]);
2119 /* NOTE: Mips documentation claims that the bitfield width goes here.
2120 But it needs to be emitted earlier. */
2122 return ret;
2126 /* Add a tag to the tag table (unless it already exists). */
2128 static tag_t *
2129 get_tag (const char *tag_start, /* 1st byte of tag name */
2130 const char *tag_end_p1, /* 1st byte after tag name */
2131 symint_t indx, /* index of tag start block */
2132 bt_t basic_type) /* bt_Struct, bt_Union, or bt_Enum */
2135 shash_t *hash_ptr;
2136 tag_t *tag_ptr;
2137 hash_ptr = hash_string (tag_start,
2138 tag_end_p1 - tag_start,
2139 &tag_hash[0],
2140 (symint_t *) 0);
2142 if (hash_ptr != (shash_t *) 0
2143 && hash_ptr->tag_ptr != (tag_t *) 0)
2145 tag_ptr = hash_ptr->tag_ptr;
2146 if (indx != indexNil)
2148 tag_ptr->basic_type = basic_type;
2149 tag_ptr->ifd = cur_file_ptr->file_index;
2150 tag_ptr->indx = indx;
2152 return tag_ptr;
2155 (void) add_string (&tag_strings,
2156 &tag_hash[0],
2157 tag_start,
2158 tag_end_p1,
2159 &hash_ptr);
2161 tag_ptr = allocate_tag ();
2162 tag_ptr->forward_ref = (forward_t *) 0;
2163 tag_ptr->hash_ptr = hash_ptr;
2164 tag_ptr->same_name = hash_ptr->tag_ptr;
2165 tag_ptr->basic_type = basic_type;
2166 tag_ptr->indx = indx;
2167 tag_ptr->ifd = (indx == indexNil
2168 ? (symint_t) -1 : cur_file_ptr->file_index);
2169 tag_ptr->same_block = cur_tag_head->first_tag;
2171 cur_tag_head->first_tag = tag_ptr;
2172 hash_ptr->tag_ptr = tag_ptr;
2174 return tag_ptr;
2178 /* Add an unknown {struct, union, enum} tag. */
2180 static void
2181 add_unknown_tag (tag_t *ptag)
2183 shash_t *hash_ptr = ptag->hash_ptr;
2184 char *name_start = hash_ptr->string;
2185 char *name_end_p1 = name_start + hash_ptr->len;
2186 forward_t *f_next = ptag->forward_ref;
2187 forward_t *f_cur;
2188 int sym_index;
2189 int file_index = cur_file_ptr->file_index;
2191 if (debug > 1)
2193 const char *agg_type = "{unknown aggregate type}";
2194 switch (ptag->basic_type)
2196 case bt_Struct: agg_type = "struct"; break;
2197 case bt_Union: agg_type = "union"; break;
2198 case bt_Enum: agg_type = "enum"; break;
2199 default: break;
2202 fprintf (stderr, "unknown %s %.*s found\n",
2203 agg_type, (int) hash_ptr->len, name_start);
2206 sym_index = add_local_symbol (name_start,
2207 name_end_p1,
2208 st_Block,
2209 sc_Info,
2210 (symint_t) 0,
2211 (symint_t) 0);
2213 (void) add_local_symbol (name_start,
2214 name_end_p1,
2215 st_End,
2216 sc_Info,
2217 (symint_t) 0,
2218 (symint_t) 0);
2220 while (f_next != (forward_t *) 0)
2222 f_cur = f_next;
2223 f_next = f_next->next;
2225 f_cur->ifd_ptr->isym = file_index;
2226 f_cur->index_ptr->rndx.index = sym_index;
2228 free_forward (f_cur);
2231 return;
2235 /* Add a procedure to the current file's list of procedures, and record
2236 this is the current procedure. If the assembler created a PDR for
2237 this procedure, use that to initialize the current PDR. */
2239 static void
2240 add_procedure (const char *func_start, /* 1st byte of func name */
2241 const char *func_end_p1) /* 1st byte after func name */
2243 PDR *new_proc_ptr;
2244 efdr_t *file_ptr = cur_file_ptr;
2245 varray_t *vp = &file_ptr->procs;
2246 symint_t value = 0;
2247 st_t proc_type = st_Proc;
2248 shash_t *shash_ptr = hash_string (func_start,
2249 func_end_p1 - func_start,
2250 &orig_str_hash[0],
2251 (symint_t *) 0);
2253 if (debug)
2254 fputc ('\n', stderr);
2256 if (vp->objects_last_page == vp->objects_per_page)
2257 add_varray_page (vp);
2259 cur_proc_ptr = new_proc_ptr = &vp->last->datum->proc[ vp->objects_last_page++ ];
2261 vp->num_allocated++;
2264 /* Did the assembler create this procedure? If so, get the PDR information. */
2265 cur_oproc_ptr = (PDR *) 0;
2266 if (shash_ptr != (shash_t *) 0)
2268 PDR *old_proc_ptr = shash_ptr->proc_ptr;
2269 SYMR *sym_ptr = shash_ptr->sym_ptr;
2271 if (old_proc_ptr != (PDR *) 0
2272 && sym_ptr != (SYMR *) 0
2273 && ((st_t) sym_ptr->st == st_Proc || (st_t) sym_ptr->st == st_StaticProc))
2275 cur_oproc_begin = sym_ptr;
2276 cur_oproc_end = shash_ptr->end_ptr;
2277 value = sym_ptr->value;
2279 cur_oproc_ptr = old_proc_ptr;
2280 proc_type = (st_t) sym_ptr->st;
2281 *new_proc_ptr = *old_proc_ptr; /* initialize */
2285 if (cur_oproc_ptr == (PDR *) 0)
2286 error ("did not find a PDR block for %.*s",
2287 (int) (func_end_p1 - func_start), func_start);
2289 /* Determine the start of symbols. */
2290 new_proc_ptr->isym = file_ptr->symbols.num_allocated;
2292 /* Push the start of the function. */
2293 (void) add_local_symbol (func_start, func_end_p1,
2294 proc_type, sc_Text,
2295 value,
2296 (symint_t) 0);
2300 /* Initialize the init_file structure. */
2302 static void
2303 initialize_init_file (void)
2305 union {
2306 unsigned char c[4];
2307 int i;
2308 } endian_test;
2310 memset (&init_file, 0, sizeof (init_file));
2312 init_file.fdr.lang = langC;
2313 init_file.fdr.fMerge = 1;
2314 init_file.fdr.glevel = GLEVEL_2;
2316 /* mips-tfile doesn't attempt to perform byte swapping and always writes
2317 out integers in its native ordering. For cross-compilers, this need
2318 not be the same as either the host or the target. The simplest thing
2319 to do is skip the configury and perform an introspective test. */
2320 /* ??? Despite the name, mips-tfile is currently only used on alpha/Tru64
2321 and would/may require significant work to be used in cross-compiler
2322 configurations, so we could simply admit defeat and hard code this as
2323 little-endian, i.e. init_file.fdr.fBigendian = 0. */
2324 endian_test.i = 1;
2325 if (endian_test.c[3])
2326 init_file.fdr.fBigendian = 1;
2328 INITIALIZE_VARRAY (&init_file.strings, char);
2329 INITIALIZE_VARRAY (&init_file.symbols, SYMR);
2330 INITIALIZE_VARRAY (&init_file.procs, PDR);
2331 INITIALIZE_VARRAY (&init_file.aux_syms, AUXU);
2333 init_file_initialized = 1;
2336 /* Add a new filename, and set up all of the file relative
2337 virtual arrays (strings, symbols, aux syms, etc.). Record
2338 where the current file structure lives. */
2340 static void
2341 add_file (const char *file_start, /* first byte in string */
2342 const char *file_end_p1) /* first byte after string */
2344 static char zero_bytes[2] = { '\0', '\0' };
2346 ptrdiff_t len = file_end_p1 - file_start;
2347 int first_ch = *file_start;
2348 efdr_t *file_ptr;
2350 if (debug)
2351 fprintf (stderr, "\tfile\t%.*s\n", (int) len, file_start);
2353 /* See if the file has already been created. */
2354 for (file_ptr = first_file;
2355 file_ptr != (efdr_t *) 0;
2356 file_ptr = file_ptr->next_file)
2358 if (first_ch == file_ptr->name[0]
2359 && file_ptr->name[len] == '\0'
2360 && memcmp (file_start, file_ptr->name, len) == 0)
2362 cur_file_ptr = file_ptr;
2363 break;
2367 /* If this is a new file, create it. */
2368 if (file_ptr == (efdr_t *) 0)
2370 if (file_desc.objects_last_page == file_desc.objects_per_page)
2371 add_varray_page (&file_desc);
2373 if (! init_file_initialized)
2374 initialize_init_file ();
2376 file_ptr = cur_file_ptr
2377 = &file_desc.last->datum->file[ file_desc.objects_last_page++ ];
2378 *file_ptr = init_file;
2380 file_ptr->file_index = file_desc.num_allocated++;
2382 /* Allocate the string hash table. */
2383 file_ptr->shash_head = (shash_t **) allocate_page ();
2385 /* Make sure 0 byte in string table is null */
2386 add_string (&file_ptr->strings,
2387 &file_ptr->shash_head[0],
2388 &zero_bytes[0],
2389 &zero_bytes[0],
2390 (shash_t **) 0);
2392 if (file_end_p1 - file_start > (long) PAGE_USIZE-2)
2393 fatal ("filename goes over one page boundary");
2395 /* Push the start of the filename. We assume that the filename
2396 will be stored at string offset 1. */
2397 (void) add_local_symbol (file_start, file_end_p1, st_File, sc_Text,
2398 (symint_t) 0, (symint_t) 0);
2399 file_ptr->fdr.rss = 1;
2400 file_ptr->name = &file_ptr->strings.last->datum->byte[1];
2401 file_ptr->name_len = file_end_p1 - file_start;
2403 /* Update the linked list of file descriptors. */
2404 *last_file_ptr = file_ptr;
2405 last_file_ptr = &file_ptr->next_file;
2407 /* Add void & int types to the file (void should be first to catch
2408 errant 0's within the index fields). */
2409 file_ptr->void_type = add_aux_sym_tir (&void_type_info,
2410 hash_yes,
2411 &cur_file_ptr->thash_head[0]);
2413 file_ptr->int_type = add_aux_sym_tir (&int_type_info,
2414 hash_yes,
2415 &cur_file_ptr->thash_head[0]);
2420 /* Add a stream of random bytes to a varray. */
2422 static void
2423 add_bytes (varray_t *vp, /* virtual array to add too */
2424 char *input_ptr, /* start of the bytes */
2425 size_t nitems) /* # items to move */
2427 size_t move_items;
2428 size_t move_bytes;
2429 char *ptr;
2431 while (nitems > 0)
2433 if (vp->objects_last_page >= vp->objects_per_page)
2434 add_varray_page (vp);
2436 ptr = &vp->last->datum->byte[ vp->objects_last_page * vp->object_size ];
2437 move_items = vp->objects_per_page - vp->objects_last_page;
2438 if (move_items > nitems)
2439 move_items = nitems;
2441 move_bytes = move_items * vp->object_size;
2442 nitems -= move_items;
2444 if (move_bytes >= 32)
2446 (void) memcpy (ptr, input_ptr, move_bytes);
2447 input_ptr += move_bytes;
2449 else
2451 while (move_bytes-- > 0)
2452 *ptr++ = *input_ptr++;
2458 /* Convert storage class to string. */
2460 static const char *
2461 sc_to_string (sc_t storage_class)
2463 switch (storage_class)
2465 case sc_Nil: return "Nil,";
2466 case sc_Text: return "Text,";
2467 case sc_Data: return "Data,";
2468 case sc_Bss: return "Bss,";
2469 case sc_Register: return "Register,";
2470 case sc_Abs: return "Abs,";
2471 case sc_Undefined: return "Undefined,";
2472 case sc_CdbLocal: return "CdbLocal,";
2473 case sc_Bits: return "Bits,";
2474 case sc_CdbSystem: return "CdbSystem,";
2475 case sc_RegImage: return "RegImage,";
2476 case sc_Info: return "Info,";
2477 case sc_UserStruct: return "UserStruct,";
2478 case sc_SData: return "SData,";
2479 case sc_SBss: return "SBss,";
2480 case sc_RData: return "RData,";
2481 case sc_Var: return "Var,";
2482 case sc_Common: return "Common,";
2483 case sc_SCommon: return "SCommon,";
2484 case sc_VarRegister: return "VarRegister,";
2485 case sc_Variant: return "Variant,";
2486 case sc_SUndefined: return "SUndefined,";
2487 case sc_Init: return "Init,";
2488 case sc_Max: return "Max,";
2491 return "???,";
2495 /* Convert symbol type to string. */
2497 static const char *
2498 st_to_string (st_t symbol_type)
2500 switch (symbol_type)
2502 case st_Nil: return "Nil,";
2503 case st_Global: return "Global,";
2504 case st_Static: return "Static,";
2505 case st_Param: return "Param,";
2506 case st_Local: return "Local,";
2507 case st_Label: return "Label,";
2508 case st_Proc: return "Proc,";
2509 case st_Block: return "Block,";
2510 case st_End: return "End,";
2511 case st_Member: return "Member,";
2512 case st_Typedef: return "Typedef,";
2513 case st_File: return "File,";
2514 case st_RegReloc: return "RegReloc,";
2515 case st_Forward: return "Forward,";
2516 case st_StaticProc: return "StaticProc,";
2517 case st_Constant: return "Constant,";
2518 case st_Str: return "String,";
2519 case st_Number: return "Number,";
2520 case st_Expr: return "Expr,";
2521 case st_Type: return "Type,";
2522 case st_Max: return "Max,";
2525 return "???,";
2529 /* Read a line from standard input, and return the start of the buffer
2530 (which is grows if the line is too big). We split lines at the
2531 semi-colon, and return each logical line independently. */
2533 static char *
2534 read_line (void)
2536 static int line_split_p = 0;
2537 int string_p = 0;
2538 int comment_p = 0;
2539 int ch;
2540 char *ptr;
2542 if (cur_line_start == (char *) 0)
2543 { /* allocate initial page */
2544 cur_line_start = (char *) allocate_page ();
2545 cur_line_alloc = PAGE_SIZE;
2548 if (!line_split_p)
2549 line_number++;
2551 line_split_p = 0;
2552 cur_line_nbytes = 0;
2554 for (ptr = cur_line_start; (ch = getchar ()) != EOF; *ptr++ = ch)
2556 if (++cur_line_nbytes >= cur_line_alloc-1)
2558 int num_pages = cur_line_alloc / PAGE_SIZE;
2559 char *old_buffer = cur_line_start;
2561 cur_line_alloc += PAGE_SIZE;
2562 cur_line_start = (char *) allocate_multiple_pages (num_pages+1);
2563 memcpy (cur_line_start, old_buffer, num_pages * PAGE_SIZE);
2565 ptr = cur_line_start + cur_line_nbytes - 1;
2568 if (ch == '\n')
2570 *ptr++ = '\n';
2571 *ptr = '\0';
2572 cur_line_ptr = cur_line_start;
2573 return cur_line_ptr;
2576 else if (ch == '\0')
2577 error ("null character found in input");
2579 else if (!comment_p)
2581 if (ch == '"')
2582 string_p = !string_p;
2584 else if (ch == '#')
2585 comment_p++;
2587 else if (ch == ';' && !string_p)
2589 line_split_p = 1;
2590 *ptr++ = '\n';
2591 *ptr = '\0';
2592 cur_line_ptr = cur_line_start;
2593 return cur_line_ptr;
2598 if (ferror (stdin))
2599 pfatal_with_name (input_name);
2601 cur_line_ptr = (char *) 0;
2602 return (char *) 0;
2606 /* Parse #.begin directives which have a label as the first argument
2607 which gives the location of the start of the block. */
2609 static void
2610 parse_begin (const char *start)
2612 const char *end_p1; /* end of label */
2613 int ch;
2614 shash_t *hash_ptr; /* hash pointer to lookup label */
2616 if (cur_file_ptr == (efdr_t *) 0)
2618 error ("#.begin directive without a preceding .file directive");
2619 return;
2622 if (cur_proc_ptr == (PDR *) 0)
2624 error ("#.begin directive without a preceding .ent directive");
2625 return;
2628 for (end_p1 = start; (ch = *end_p1) != '\0' && !ISSPACE (ch); end_p1++)
2631 hash_ptr = hash_string (start,
2632 end_p1 - start,
2633 &orig_str_hash[0],
2634 (symint_t *) 0);
2636 if (hash_ptr == (shash_t *) 0)
2638 error ("label %.*s not found for #.begin",
2639 (int) (end_p1 - start), start);
2640 return;
2643 if (cur_oproc_begin == (SYMR *) 0)
2645 error ("procedure table %.*s not found for #.begin",
2646 (int) (end_p1 - start), start);
2647 return;
2650 (void) add_local_symbol ((const char *) 0, (const char *) 0,
2651 st_Block, sc_Text,
2652 (symint_t) hash_ptr->sym_ptr->value - cur_oproc_begin->value,
2653 (symint_t) 0);
2657 /* Parse #.bend directives which have a label as the first argument
2658 which gives the location of the end of the block. */
2660 static void
2661 parse_bend (const char *start)
2663 const char *end_p1; /* end of label */
2664 int ch;
2665 shash_t *hash_ptr; /* hash pointer to lookup label */
2667 if (cur_file_ptr == (efdr_t *) 0)
2669 error ("#.begin directive without a preceding .file directive");
2670 return;
2673 if (cur_proc_ptr == (PDR *) 0)
2675 error ("#.bend directive without a preceding .ent directive");
2676 return;
2679 for (end_p1 = start; (ch = *end_p1) != '\0' && !ISSPACE (ch); end_p1++)
2682 hash_ptr = hash_string (start,
2683 end_p1 - start,
2684 &orig_str_hash[0],
2685 (symint_t *) 0);
2687 if (hash_ptr == (shash_t *) 0)
2689 error ("label %.*s not found for #.bend", (int) (end_p1 - start), start);
2690 return;
2693 if (cur_oproc_begin == (SYMR *) 0)
2695 error ("procedure table %.*s not found for #.bend",
2696 (int) (end_p1 - start), start);
2697 return;
2700 (void) add_local_symbol ((const char *) 0, (const char *) 0,
2701 st_End, sc_Text,
2702 (symint_t) hash_ptr->sym_ptr->value - cur_oproc_begin->value,
2703 (symint_t) 0);
2707 /* Parse #.def directives, which are contain standard COFF subdirectives
2708 to describe the debugging format. These subdirectives include:
2710 .scl specify storage class
2711 .val specify a value
2712 .endef specify end of COFF directives
2713 .type specify the type
2714 .size specify the size of an array
2715 .dim specify an array dimension
2716 .tag specify a tag for a struct, union, or enum. */
2718 static void
2719 parse_def (const char *name_start)
2721 const char *dir_start; /* start of current directive*/
2722 const char *dir_end_p1; /* end+1 of current directive*/
2723 const char *arg_start; /* start of current argument */
2724 const char *arg_end_p1; /* end+1 of current argument */
2725 const char *name_end_p1; /* end+1 of label */
2726 const char *tag_start = 0; /* start of tag name */
2727 const char *tag_end_p1 = 0; /* end+1 of tag name */
2728 sc_t storage_class = sc_Nil;
2729 st_t symbol_type = st_Nil;
2730 type_info_t t;
2731 EXTR *eptr = (EXTR *) 0; /* ext. sym equivalent to def*/
2732 int is_function = 0; /* != 0 if function */
2733 symint_t value = 0;
2734 symint_t indx = cur_file_ptr->void_type;
2735 int error_line = 0;
2736 symint_t arg_number;
2737 symint_t temp_array[ N_TQ ];
2738 int arg_was_number;
2739 int ch, i;
2740 ptrdiff_t len;
2742 static int inside_enumeration = 0; /* is this an enumeration? */
2745 /* Initialize the type information. */
2746 t = type_info_init;
2749 /* Search for the end of the name being defined. */
2750 /* Allow spaces and such in names for G++ templates, which produce stabs
2751 that look like:
2753 #.def SMANIP<long unsigned int>; .scl 10; .type 0x8; .size 8; .endef */
2755 for (name_end_p1 = name_start; (ch = *name_end_p1) != ';' && ch != '\0'; name_end_p1++)
2758 if (ch == '\0')
2760 error_line = __LINE__;
2761 goto bomb_out;
2764 /* Parse the remaining subdirectives now. */
2765 dir_start = name_end_p1+1;
2766 for (;;)
2768 while ((ch = *dir_start) == ' ' || ch == '\t')
2769 ++dir_start;
2771 if (ch != '.')
2773 error_line = __LINE__;
2774 goto bomb_out;
2777 /* Are we done? */
2778 if (dir_start[1] == 'e'
2779 && memcmp (dir_start, ".endef", sizeof (".endef")-1) == 0)
2780 break;
2782 /* Pick up the subdirective now. */
2783 for (dir_end_p1 = dir_start+1;
2784 (ch = *dir_end_p1) != ' ' && ch != '\t';
2785 dir_end_p1++)
2787 if (ch == '\0' || ISSPACE (ch))
2789 error_line = __LINE__;
2790 goto bomb_out;
2794 /* Pick up the subdirective argument now. */
2795 arg_was_number = arg_number = 0;
2796 arg_end_p1 = 0;
2797 arg_start = dir_end_p1+1;
2798 ch = *arg_start;
2799 while (ch == ' ' || ch == '\t')
2800 ch = *++arg_start;
2802 if (ISDIGIT (ch) || ch == '-' || ch == '+')
2804 int ch2;
2805 arg_number = strtol (arg_start, (char **) &arg_end_p1, 0);
2806 /* It's only a number if followed by ';' or ','. */
2807 if (arg_end_p1 != arg_start && (((ch2 = *arg_end_p1) == ';') || ch2 == ','))
2808 arg_was_number++;
2811 else if (ch == '\0' || ISSPACE (ch))
2813 error_line = __LINE__;
2814 goto bomb_out;
2817 if (!arg_was_number)
2819 /* Allow spaces and such in names for G++ templates. */
2820 for (arg_end_p1 = arg_start+1;
2821 (ch = *arg_end_p1) != ';' && ch != '\0';
2822 arg_end_p1++)
2825 if (ch == '\0')
2827 error_line = __LINE__;
2828 goto bomb_out;
2832 /* Classify the directives now. */
2833 len = dir_end_p1 - dir_start;
2834 switch (dir_start[1])
2836 default:
2837 error_line = __LINE__;
2838 goto bomb_out;
2840 case 'd':
2841 if (len == sizeof (".dim")-1
2842 && memcmp (dir_start, ".dim", sizeof (".dim")-1) == 0
2843 && arg_was_number)
2845 symint_t *t_ptr = &temp_array[ N_TQ-1 ];
2847 *t_ptr = arg_number;
2848 while (*arg_end_p1 == ',' && arg_was_number)
2850 arg_start = arg_end_p1+1;
2851 ch = *arg_start;
2852 while (ch == ' ' || ch == '\t')
2853 ch = *++arg_start;
2855 arg_was_number = 0;
2856 if (ISDIGIT (ch) || ch == '-' || ch == '+')
2858 int ch2;
2859 arg_number = strtol (arg_start, (char **) &arg_end_p1, 0);
2860 if (arg_end_p1 != arg_start && (((ch2 = *arg_end_p1) == ';') || ch2 == ','))
2861 arg_was_number++;
2863 if (t_ptr == &temp_array[0])
2865 error_line = __LINE__;
2866 goto bomb_out;
2869 *--t_ptr = arg_number;
2873 /* Reverse order of dimensions. */
2874 while (t_ptr <= &temp_array[ N_TQ-1 ])
2876 if (t.num_dims >= N_TQ-1)
2878 error_line = __LINE__;
2879 goto bomb_out;
2882 t.dimensions[ t.num_dims++ ] = *t_ptr++;
2884 break;
2886 else
2888 error_line = __LINE__;
2889 goto bomb_out;
2893 case 's':
2894 if (len == sizeof (".scl")-1
2895 && memcmp (dir_start, ".scl", sizeof (".scl")-1) == 0
2896 && arg_was_number
2897 && arg_number < ((symint_t) C_MAX))
2899 /* If the symbol is a static or external, we have
2900 already gotten the appropriate type and class, so
2901 make sure we don't override those values. This is
2902 needed because there are some type and classes that
2903 are not in COFF, such as short data, etc. */
2904 if (symbol_type == st_Nil)
2906 symbol_type = map_coff_sym_type[arg_number];
2907 storage_class = map_coff_storage [arg_number];
2909 break;
2912 else if (len == sizeof (".size")-1
2913 && memcmp (dir_start, ".size", sizeof (".size")-1) == 0
2914 && arg_was_number)
2916 symint_t *t_ptr = &temp_array[ N_TQ-1 ];
2918 *t_ptr = arg_number;
2919 while (*arg_end_p1 == ',' && arg_was_number)
2921 arg_start = arg_end_p1+1;
2922 ch = *arg_start;
2923 while (ch == ' ' || ch == '\t')
2924 ch = *++arg_start;
2926 arg_was_number = 0;
2927 if (ISDIGIT (ch) || ch == '-' || ch == '+')
2929 int ch2;
2930 arg_number = strtol (arg_start, (char **) &arg_end_p1, 0);
2931 if (arg_end_p1 != arg_start && (((ch2 = *arg_end_p1) == ';') || ch2 == ','))
2932 arg_was_number++;
2934 if (t_ptr == &temp_array[0])
2936 error_line = __LINE__;
2937 goto bomb_out;
2940 *--t_ptr = arg_number;
2944 /* Reverse order of sizes. */
2945 while (t_ptr <= &temp_array[ N_TQ-1 ])
2947 if (t.num_sizes >= N_TQ-1)
2949 error_line = __LINE__;
2950 goto bomb_out;
2953 t.sizes[ t.num_sizes++ ] = *t_ptr++;
2955 break;
2958 else
2960 error_line = __LINE__;
2961 goto bomb_out;
2965 case 't':
2966 if (len == sizeof (".type")-1
2967 && memcmp (dir_start, ".type", sizeof (".type")-1) == 0
2968 && arg_was_number)
2970 tq_t *tq_ptr = &t.type_qualifiers[0];
2972 t.orig_type = (coff_type_t) (arg_number & N_BTMASK);
2973 t.basic_type = map_coff_types [(int) t.orig_type];
2974 for (i = N_TQ-1; i >= 0; i--)
2976 int dt = (arg_number >> ((i * N_TQ_SHIFT) + N_BT_SHIFT)
2977 & N_TMASK);
2979 if (dt != (int) DT_NON)
2980 *tq_ptr++ = map_coff_derived_type [dt];
2983 /* If this is a function, ignore it, so that we don't get
2984 two entries (one from the .ent, and one for the .def
2985 that precedes it). Save the type information so that
2986 the end block can properly add it after the begin block
2987 index. For MIPS knows what reason, we must strip off
2988 the function type at this point. */
2989 if (tq_ptr != &t.type_qualifiers[0] && tq_ptr[-1] == tq_Proc)
2991 is_function = 1;
2992 tq_ptr[-1] = tq_Nil;
2995 break;
2998 else if (len == sizeof (".tag")-1
2999 && memcmp (dir_start, ".tag", sizeof (".tag")-1) == 0)
3001 tag_start = arg_start;
3002 tag_end_p1 = arg_end_p1;
3003 break;
3006 else
3008 error_line = __LINE__;
3009 goto bomb_out;
3013 case 'v':
3014 if (len == sizeof (".val")-1
3015 && memcmp (dir_start, ".val", sizeof (".val")-1) == 0)
3017 if (arg_was_number)
3018 value = arg_number;
3020 /* If the value is not an integer value, it must be the
3021 name of a static or global item. Look up the name in
3022 the original symbol table to pick up the storage
3023 class, symbol type, etc. */
3024 else
3026 shash_t *orig_hash_ptr; /* hash within orig sym table*/
3027 shash_t *ext_hash_ptr; /* hash within ext. sym table*/
3029 ext_hash_ptr = hash_string (arg_start,
3030 arg_end_p1 - arg_start,
3031 &ext_str_hash[0],
3032 (symint_t *) 0);
3034 if (ext_hash_ptr != (shash_t *) 0
3035 && ext_hash_ptr->esym_ptr != (EXTR *) 0)
3036 eptr = ext_hash_ptr->esym_ptr;
3038 orig_hash_ptr = hash_string (arg_start,
3039 arg_end_p1 - arg_start,
3040 &orig_str_hash[0],
3041 (symint_t *) 0);
3043 if ((orig_hash_ptr == (shash_t *) 0
3044 || orig_hash_ptr->sym_ptr == (SYMR *) 0)
3045 && eptr == (EXTR *) 0)
3047 fprintf (stderr, "warning, %.*s not found in original or external symbol tables, value defaults to 0\n",
3048 (int) (arg_end_p1 - arg_start),
3049 arg_start);
3050 value = 0;
3052 else
3054 SYMR *ptr = (orig_hash_ptr != (shash_t *) 0
3055 && orig_hash_ptr->sym_ptr != (SYMR *) 0)
3056 ? orig_hash_ptr->sym_ptr
3057 : &eptr->asym;
3059 symbol_type = (st_t) ptr->st;
3060 storage_class = (sc_t) ptr->sc;
3061 value = ptr->value;
3064 break;
3066 else
3068 error_line = __LINE__;
3069 goto bomb_out;
3073 /* Set up to find next directive. */
3074 dir_start = arg_end_p1 + 1;
3078 if (storage_class == sc_Bits)
3080 t.bitfield = 1;
3081 t.extra_sizes = 1;
3083 else
3084 t.extra_sizes = 0;
3086 if (t.num_dims > 0)
3088 int num_real_sizes = t.num_sizes - t.extra_sizes;
3089 int diff = t.num_dims - num_real_sizes;
3090 int i = t.num_dims - 1;
3091 int j;
3093 if (num_real_sizes != 1 || diff < 0)
3095 error_line = __LINE__;
3096 goto bomb_out;
3099 /* If this is an array, make sure the same number of dimensions
3100 and sizes were passed, creating extra sizes for multiply
3101 dimensioned arrays if not passed. */
3103 if (diff)
3105 for (j = ARRAY_SIZE (t.sizes) - 1; j >= 0; j--)
3106 t.sizes[ j ] = ((j-diff) >= 0) ? t.sizes[ j-diff ] : 0;
3108 t.num_sizes = i + 1;
3109 for ( i--; i >= 0; i-- )
3111 if (t.dimensions[ i+1 ])
3112 t.sizes[ i ] = t.sizes[ i+1 ] / t.dimensions[ i+1 ];
3113 else
3114 t.sizes[ i ] = t.sizes[ i+1 ];
3119 /* Except for enumeration members & begin/ending of scopes, put the
3120 type word in the aux. symbol table. */
3122 if (symbol_type == st_Block || symbol_type == st_End)
3123 indx = 0;
3125 else if (inside_enumeration)
3126 indx = cur_file_ptr->void_type;
3128 else
3130 if (t.basic_type == bt_Struct
3131 || t.basic_type == bt_Union
3132 || t.basic_type == bt_Enum)
3134 if (tag_start == (char *) 0)
3136 error ("no tag specified for %.*s",
3137 (int) (name_end_p1 - name_start),
3138 name_start);
3139 return;
3142 t.tag_ptr = get_tag (tag_start, tag_end_p1, (symint_t) indexNil,
3143 t.basic_type);
3146 if (is_function)
3148 last_func_type_info = t;
3149 last_func_eptr = eptr;
3150 return;
3153 indx = add_aux_sym_tir (&t,
3154 hash_yes,
3155 &cur_file_ptr->thash_head[0]);
3159 /* If this is an external or static symbol, update the appropriate
3160 external symbol. */
3162 if (eptr != (EXTR *) 0
3163 && (eptr->asym.index == indexNil || cur_proc_ptr == (PDR *) 0))
3165 eptr->ifd = cur_file_ptr->file_index;
3166 eptr->asym.index = indx;
3170 /* Do any last minute adjustments that are necessary. */
3171 switch (symbol_type)
3173 default:
3174 break;
3177 /* For the beginning of structs, unions, and enumerations, the
3178 size info needs to be passed in the value field. */
3180 case st_Block:
3181 if (t.num_sizes - t.num_dims - t.extra_sizes != 1)
3183 error_line = __LINE__;
3184 goto bomb_out;
3187 else
3188 value = t.sizes[0];
3190 inside_enumeration = (t.orig_type == T_ENUM);
3191 break;
3194 /* For the end of structs, unions, and enumerations, omit the
3195 name which is always ".eos". This needs to be done last, so
3196 that any error reporting above gives the correct name. */
3198 case st_End:
3199 name_start = name_end_p1 = 0;
3200 value = inside_enumeration = 0;
3201 break;
3204 /* Members of structures and unions that aren't bitfields, need
3205 to adjust the value from a byte offset to a bit offset.
3206 Members of enumerations do not have the value adjusted, and
3207 can be distinguished by indx == indexNil. For enumerations,
3208 update the maximum enumeration value. */
3210 case st_Member:
3211 if (!t.bitfield && !inside_enumeration)
3212 value *= 8;
3214 break;
3218 /* Add the symbol, except for global symbols outside of functions,
3219 for which the external symbol table is fine enough. */
3221 if (eptr == (EXTR *) 0
3222 || eptr->asym.st == (int) st_Nil
3223 || cur_proc_ptr != (PDR *) 0)
3225 symint_t isym = add_local_symbol (name_start, name_end_p1,
3226 symbol_type, storage_class,
3227 value,
3228 indx);
3230 /* Deal with struct, union, and enum tags. */
3231 if (symbol_type == st_Block)
3233 /* Create or update the tag information. */
3234 tag_t *tag_ptr = get_tag (name_start,
3235 name_end_p1,
3236 isym,
3237 t.basic_type);
3239 /* If there are any forward references, fill in the appropriate
3240 file and symbol indexes. */
3242 symint_t file_index = cur_file_ptr->file_index;
3243 forward_t *f_next = tag_ptr->forward_ref;
3244 forward_t *f_cur;
3246 while (f_next != (forward_t *) 0)
3248 f_cur = f_next;
3249 f_next = f_next->next;
3251 f_cur->ifd_ptr->isym = file_index;
3252 f_cur->index_ptr->rndx.index = isym;
3254 free_forward (f_cur);
3257 tag_ptr->forward_ref = (forward_t *) 0;
3261 /* Normal return */
3262 return;
3264 /* Error return, issue message. */
3265 bomb_out:
3266 if (error_line)
3267 error ("compiler error, badly formed #.def (internal line # = %d)", error_line);
3268 else
3269 error ("compiler error, badly formed #.def");
3271 return;
3275 /* Parse .end directives. */
3277 static void
3278 parse_end (const char *start)
3280 const char *start_func, *end_func_p1;
3281 int ch;
3282 symint_t value;
3283 FDR *orig_fdr;
3285 if (cur_file_ptr == (efdr_t *) 0)
3287 error (".end directive without a preceding .file directive");
3288 return;
3291 if (cur_proc_ptr == (PDR *) 0)
3293 error (".end directive without a preceding .ent directive");
3294 return;
3297 /* Get the function name, skipping whitespace. */
3298 for (start_func = start; ISSPACE ((unsigned char)*start_func); start_func++)
3301 ch = *start_func;
3302 if (!IS_ASM_IDENT (ch))
3304 error (".end directive has no name");
3305 return;
3308 for (end_func_p1 = start_func; IS_ASM_IDENT (ch); ch = *++end_func_p1)
3312 /* Get the value field for creating the end from the original object
3313 file (which we find by locating the procedure start, and using the
3314 pointer to the end+1 block and backing up. The index points to a
3315 two word aux. symbol, whose first word is the index of the end
3316 symbol, and the second word is the type of the function return
3317 value. */
3319 orig_fdr = cur_file_ptr->orig_fdr;
3320 value = 0;
3321 if (orig_fdr != (FDR *) 0 && cur_oproc_end != (SYMR *) 0)
3322 value = cur_oproc_end->value;
3324 else
3325 error ("cannot find .end block for %.*s",
3326 (int) (end_func_p1 - start_func), start_func);
3328 (void) add_local_symbol (start_func, end_func_p1,
3329 st_End, sc_Text,
3330 value,
3331 (symint_t) 0);
3333 cur_proc_ptr = cur_oproc_ptr = (PDR *) 0;
3337 /* Parse .ent directives. */
3339 static void
3340 parse_ent (const char *start)
3342 const char *start_func, *end_func_p1;
3343 int ch;
3345 if (cur_file_ptr == (efdr_t *) 0)
3347 error (".ent directive without a preceding .file directive");
3348 return;
3351 if (cur_proc_ptr != (PDR *) 0)
3353 error ("second .ent directive found before .end directive");
3354 return;
3357 for (start_func = start; ISSPACE ((unsigned char)*start_func); start_func++)
3360 ch = *start_func;
3361 if (!IS_ASM_IDENT (ch))
3363 error (".ent directive has no name");
3364 return;
3367 for (end_func_p1 = start_func; IS_ASM_IDENT (ch); ch = *++end_func_p1)
3370 (void) add_procedure (start_func, end_func_p1);
3374 /* Parse .file directives. */
3376 static void
3377 parse_file (const char *start)
3379 char *p;
3380 char *start_name, *end_name_p1;
3382 (void) strtol (start, &p, 0);
3383 if (start == p
3384 || (start_name = strchr (p, '"')) == (char *) 0
3385 || (end_name_p1 = strrchr (++start_name, '"')) == (char *) 0)
3387 error ("invalid .file directive");
3388 return;
3391 if (cur_proc_ptr != (PDR *) 0)
3393 error ("no way to handle .file within .ent/.end section");
3394 return;
3397 add_file (start_name, end_name_p1);
3401 /* Make sure the @stabs symbol is emitted. */
3403 static void
3404 mark_stabs (const char *start ATTRIBUTE_UNUSED)
3406 if (!stabs_seen)
3408 /* Add a dummy @stabs symbol. */
3409 stabs_seen = 1;
3410 (void) add_local_symbol (stabs_symbol,
3411 stabs_symbol + sizeof (stabs_symbol),
3412 (st_t) stNil, (sc_t) scInfo, -1,
3413 MIPS_MARK_STAB (0));
3419 /* Parse .stabs directives.
3421 .stabs directives have five fields:
3422 "string" a string, encoding the type information.
3423 code a numeric code, defined in <stab.h>
3424 0 a zero
3425 0 a zero or line number
3426 value a numeric value or an address.
3428 If the value is relocatable, we transform this into:
3429 iss points as an index into string space
3430 value value from lookup of the name
3431 st st from lookup of the name
3432 sc sc from lookup of the name
3433 index code|CODE_MASK
3435 If the value is not relocatable, we transform this into:
3436 iss points as an index into string space
3437 value value
3438 st st_Nil
3439 sc sc_Nil
3440 index code|CODE_MASK
3442 .stabn directives have four fields (string is null):
3443 code a numeric code, defined in <stab.h>
3444 0 a zero
3445 0 a zero or a line number
3446 value a numeric value or an address. */
3448 static void
3449 parse_stabs_common (const char *string_start, /* start of string or NULL */
3450 const char *string_end, /* end+1 of string or NULL */
3451 const char *rest) /* rest of the directive. */
3453 efdr_t *save_file_ptr = cur_file_ptr;
3454 symint_t code;
3455 symint_t value;
3456 char *p;
3457 st_t st;
3458 sc_t sc;
3459 int ch;
3461 if (stabs_seen == 0)
3462 mark_stabs ("");
3464 /* Read code from stabs. */
3465 if (!ISDIGIT (*rest))
3467 error ("invalid .stabs/.stabn directive, code is non-numeric");
3468 return;
3471 code = strtol (rest, &p, 0);
3473 /* Line number stabs are handled differently, since they have two values,
3474 the line number and the address of the label. We use the index field
3475 (aka code) to hold the line number, and the value field to hold the
3476 address. The symbol type is st_Label, which should be different from
3477 the other stabs, so that gdb can recognize it. */
3479 if (code == (int) N_SLINE)
3481 SYMR *sym_ptr, dummy_symr;
3482 shash_t *shash_ptr;
3484 /* Skip ,0, */
3485 if (p[0] != ',' || p[1] != '0' || p[2] != ',' || !ISDIGIT (p[3]))
3487 error ("invalid line number .stabs/.stabn directive");
3488 return;
3491 code = strtol (p+3, &p, 0);
3492 ch = *++p;
3493 if (p[-1] != ',' || ISDIGIT (ch) || !IS_ASM_IDENT (ch))
3495 error ("invalid line number .stabs/.stabn directive");
3496 return;
3499 dummy_symr.index = code;
3500 if (dummy_symr.index != code)
3502 error ("line number (%lu) for .stabs/.stabn directive cannot fit in index field (20 bits)",
3503 code);
3505 return;
3508 shash_ptr = hash_string (p,
3509 strlen (p) - 1,
3510 &orig_str_hash[0],
3511 (symint_t *) 0);
3513 if (shash_ptr == (shash_t *) 0
3514 || (sym_ptr = shash_ptr->sym_ptr) == (SYMR *) 0)
3516 error ("invalid .stabs/.stabn directive, value not found");
3517 return;
3520 if ((st_t) sym_ptr->st != st_Label)
3522 error ("invalid line number .stabs/.stabn directive");
3523 return;
3526 st = st_Label;
3527 sc = (sc_t) sym_ptr->sc;
3528 value = sym_ptr->value;
3530 else
3532 /* Skip ,<num>,<num>, */
3533 if (*p++ != ',')
3534 goto failure;
3535 for (; ISDIGIT (*p); p++)
3537 if (*p++ != ',')
3538 goto failure;
3539 for (; ISDIGIT (*p); p++)
3541 if (*p++ != ',')
3542 goto failure;
3543 ch = *p;
3544 if (!IS_ASM_IDENT (ch) && ch != '-')
3546 failure:
3547 error ("invalid .stabs/.stabn directive, bad character");
3548 return;
3551 if (ISDIGIT (ch) || ch == '-')
3553 st = st_Nil;
3554 sc = sc_Nil;
3555 value = strtol (p, &p, 0);
3556 if (*p != '\n')
3558 error ("invalid .stabs/.stabn directive, stuff after numeric value");
3559 return;
3562 else if (!IS_ASM_IDENT (ch))
3564 error ("invalid .stabs/.stabn directive, bad character");
3565 return;
3567 else
3569 SYMR *sym_ptr;
3570 shash_t *shash_ptr;
3571 const char *start, *end_p1;
3573 start = p;
3574 if ((end_p1 = strchr (start, '+')) == (char *) 0)
3576 if ((end_p1 = strchr (start, '-')) == (char *) 0)
3577 end_p1 = start + strlen (start) - 1;
3580 shash_ptr = hash_string (start,
3581 end_p1 - start,
3582 &orig_str_hash[0],
3583 (symint_t *) 0);
3585 if (shash_ptr == (shash_t *) 0
3586 || (sym_ptr = shash_ptr->sym_ptr) == (SYMR *) 0)
3588 shash_ptr = hash_string (start,
3589 end_p1 - start,
3590 &ext_str_hash[0],
3591 (symint_t *) 0);
3593 if (shash_ptr == (shash_t *) 0
3594 || shash_ptr->esym_ptr == (EXTR *) 0)
3596 error ("invalid .stabs/.stabn directive, value not found");
3597 return;
3599 else
3600 sym_ptr = &(shash_ptr->esym_ptr->asym);
3603 /* Traditionally, N_LBRAC and N_RBRAC are *not* relocated. */
3604 if (code == (int) N_LBRAC || code == (int) N_RBRAC)
3606 sc = (sc_t) scNil;
3607 st = (st_t) stNil;
3609 else
3611 sc = (sc_t) sym_ptr->sc;
3612 st = (st_t) sym_ptr->st;
3614 value = sym_ptr->value;
3616 ch = *end_p1++;
3617 if (ch != '\n')
3619 if (((!ISDIGIT (*end_p1)) && (*end_p1 != '-'))
3620 || ((ch != '+') && (ch != '-')))
3622 error ("invalid .stabs/.stabn directive, badly formed value");
3623 return;
3625 if (ch == '+')
3626 value += strtol (end_p1, &p, 0);
3627 else if (ch == '-')
3628 value -= strtol (end_p1, &p, 0);
3630 if (*p != '\n')
3632 error ("invalid .stabs/.stabn directive, stuff after numeric value");
3633 return;
3637 code = MIPS_MARK_STAB (code);
3640 (void) add_local_symbol (string_start, string_end, st, sc, value, code);
3641 /* Restore normal file type. */
3642 cur_file_ptr = save_file_ptr;
3646 static void
3647 parse_stabs (const char *start)
3649 const char *end = strchr (start+1, '"');
3651 if (*start != '"' || end == (const char *) 0 || end[1] != ',')
3653 error ("invalid .stabs directive, no string");
3654 return;
3657 parse_stabs_common (start+1, end, end+2);
3661 static void
3662 parse_stabn (const char *start)
3664 parse_stabs_common ((const char *) 0, (const char *) 0, start);
3668 /* Parse the input file, and write the lines to the output file
3669 if needed. */
3671 static void
3672 parse_input (void)
3674 char *p;
3675 size_t i;
3676 thead_t *ptag_head;
3677 tag_t *ptag;
3678 tag_t *ptag_next;
3680 if (debug)
3681 fprintf (stderr, "\tinput\n");
3683 /* Add a dummy scope block around the entire compilation unit for
3684 structures defined outside of blocks. */
3685 ptag_head = allocate_thead ();
3686 ptag_head->first_tag = 0;
3687 ptag_head->prev = cur_tag_head;
3688 cur_tag_head = ptag_head;
3690 while ((p = read_line ()) != (char *) 0)
3692 /* Skip leading blanks. */
3693 while (ISSPACE ((unsigned char)*p))
3694 p++;
3696 /* See if it's a directive we handle. If so, dispatch handler. */
3697 for (i = 0; i < ARRAY_SIZE (pseudo_ops); i++)
3698 if (memcmp (p, pseudo_ops[i].name, pseudo_ops[i].len) == 0
3699 && ISSPACE ((unsigned char)(p[pseudo_ops[i].len])))
3701 p += pseudo_ops[i].len; /* skip to first argument */
3702 while (ISSPACE ((unsigned char)*p))
3703 p++;
3705 (*pseudo_ops[i].func)( p );
3706 break;
3710 /* Process any tags at global level. */
3711 ptag_head = cur_tag_head;
3712 cur_tag_head = ptag_head->prev;
3714 for (ptag = ptag_head->first_tag;
3715 ptag != (tag_t *) 0;
3716 ptag = ptag_next)
3718 if (ptag->forward_ref != (forward_t *) 0)
3719 add_unknown_tag (ptag);
3721 ptag_next = ptag->same_block;
3722 ptag->hash_ptr->tag_ptr = ptag->same_name;
3723 free_tag (ptag);
3726 free_thead (ptag_head);
3731 /* Update the global headers with the final offsets in preparation
3732 to write out the .T file. */
3734 static void
3735 update_headers (void)
3737 symint_t i;
3738 efdr_t *file_ptr;
3740 /* Set up the symbolic header. */
3741 file_offset = sizeof (symbolic_header) + orig_file_header.f_symptr;
3742 symbolic_header.magic = orig_sym_hdr.magic;
3743 symbolic_header.vstamp = orig_sym_hdr.vstamp;
3745 /* Set up global counts. */
3746 symbolic_header.issExtMax = ext_strings.num_allocated;
3747 symbolic_header.idnMax = dense_num.num_allocated;
3748 symbolic_header.ifdMax = file_desc.num_allocated;
3749 symbolic_header.iextMax = ext_symbols.num_allocated;
3750 symbolic_header.ilineMax = orig_sym_hdr.ilineMax;
3751 symbolic_header.ioptMax = orig_sym_hdr.ioptMax;
3752 symbolic_header.cbLine = orig_sym_hdr.cbLine;
3753 symbolic_header.crfd = orig_sym_hdr.crfd;
3756 /* Loop through each file, figuring out how many local syms,
3757 line numbers, etc. there are. Also, put out end symbol
3758 for the filename. */
3760 for (file_ptr = first_file;
3761 file_ptr != (efdr_t *) 0;
3762 file_ptr = file_ptr->next_file)
3764 SYMR *sym_start;
3765 SYMR *sym;
3766 SYMR *sym_end_p1;
3767 FDR *fd_ptr = file_ptr->orig_fdr;
3769 cur_file_ptr = file_ptr;
3771 /* Copy st_Static symbols from the original local symbol table if
3772 they did not get added to the new local symbol table.
3773 This happens with stabs-in-ecoff or if the source file is
3774 compiled without debugging. */
3775 sym_start = ORIG_LSYMS (fd_ptr->isymBase);
3776 sym_end_p1 = sym_start + fd_ptr->csym;
3777 for (sym = sym_start; sym < sym_end_p1; sym++)
3779 if ((st_t) sym->st == st_Static)
3781 char *str = ORIG_LSTRS (fd_ptr->issBase + sym->iss);
3782 size_t len = strlen (str);
3783 shash_t *hash_ptr;
3785 /* Ignore internal labels. */
3786 if (str[0] == '$' && str[1] == 'L')
3787 continue;
3788 hash_ptr = hash_string (str,
3789 (ptrdiff_t) len,
3790 &file_ptr->shash_head[0],
3791 (symint_t *) 0);
3792 if (hash_ptr == (shash_t *) 0)
3794 (void) add_local_symbol (str, str + len,
3795 (st_t) sym->st, (sc_t) sym->sc,
3796 (symint_t) sym->value,
3797 (symint_t) indexNil);
3801 (void) add_local_symbol ((const char *) 0, (const char *) 0,
3802 st_End, sc_Text,
3803 (symint_t) 0,
3804 (symint_t) 0);
3806 file_ptr->fdr.cpd = file_ptr->procs.num_allocated;
3807 file_ptr->fdr.ipdFirst = symbolic_header.ipdMax;
3808 symbolic_header.ipdMax += file_ptr->fdr.cpd;
3810 file_ptr->fdr.csym = file_ptr->symbols.num_allocated;
3811 file_ptr->fdr.isymBase = symbolic_header.isymMax;
3812 symbolic_header.isymMax += file_ptr->fdr.csym;
3814 file_ptr->fdr.caux = file_ptr->aux_syms.num_allocated;
3815 file_ptr->fdr.iauxBase = symbolic_header.iauxMax;
3816 symbolic_header.iauxMax += file_ptr->fdr.caux;
3818 file_ptr->fdr.cbSs = file_ptr->strings.num_allocated;
3819 file_ptr->fdr.issBase = symbolic_header.issMax;
3820 symbolic_header.issMax += file_ptr->fdr.cbSs;
3823 /* Align ecoff symbol tables to avoid OSF1/1.3 nm complaints. */
3824 #define ALIGN_SYMTABLE_OFFSET(OFFSET) (((OFFSET) + 7) & ~7)
3826 file_offset = ALIGN_SYMTABLE_OFFSET (file_offset);
3827 i = WORD_ALIGN (symbolic_header.cbLine); /* line numbers */
3828 if (i > 0)
3830 symbolic_header.cbLineOffset = file_offset;
3831 file_offset += i;
3832 file_offset = ALIGN_SYMTABLE_OFFSET (file_offset);
3835 i = symbolic_header.ioptMax; /* optimization symbols */
3836 if (((long) i) > 0)
3838 symbolic_header.cbOptOffset = file_offset;
3839 file_offset += i * sizeof (OPTR);
3840 file_offset = ALIGN_SYMTABLE_OFFSET (file_offset);
3843 i = symbolic_header.idnMax; /* dense numbers */
3844 if (i > 0)
3846 symbolic_header.cbDnOffset = file_offset;
3847 file_offset += i * sizeof (DNR);
3848 file_offset = ALIGN_SYMTABLE_OFFSET (file_offset);
3851 i = symbolic_header.ipdMax; /* procedure tables */
3852 if (i > 0)
3854 symbolic_header.cbPdOffset = file_offset;
3855 file_offset += i * sizeof (PDR);
3856 file_offset = ALIGN_SYMTABLE_OFFSET (file_offset);
3859 i = symbolic_header.isymMax; /* local symbols */
3860 if (i > 0)
3862 symbolic_header.cbSymOffset = file_offset;
3863 file_offset += i * sizeof (SYMR);
3864 file_offset = ALIGN_SYMTABLE_OFFSET (file_offset);
3867 i = symbolic_header.iauxMax; /* aux syms. */
3868 if (i > 0)
3870 symbolic_header.cbAuxOffset = file_offset;
3871 file_offset += i * sizeof (TIR);
3872 file_offset = ALIGN_SYMTABLE_OFFSET (file_offset);
3875 i = WORD_ALIGN (symbolic_header.issMax); /* local strings */
3876 if (i > 0)
3878 symbolic_header.cbSsOffset = file_offset;
3879 file_offset += i;
3880 file_offset = ALIGN_SYMTABLE_OFFSET (file_offset);
3883 i = WORD_ALIGN (symbolic_header.issExtMax); /* external strings */
3884 if (i > 0)
3886 symbolic_header.cbSsExtOffset = file_offset;
3887 file_offset += i;
3888 file_offset = ALIGN_SYMTABLE_OFFSET (file_offset);
3891 i = symbolic_header.ifdMax; /* file tables */
3892 if (i > 0)
3894 symbolic_header.cbFdOffset = file_offset;
3895 file_offset += i * sizeof (FDR);
3896 file_offset = ALIGN_SYMTABLE_OFFSET (file_offset);
3899 i = symbolic_header.crfd; /* relative file descriptors */
3900 if (i > 0)
3902 symbolic_header.cbRfdOffset = file_offset;
3903 file_offset += i * sizeof (symint_t);
3904 file_offset = ALIGN_SYMTABLE_OFFSET (file_offset);
3907 i = symbolic_header.iextMax; /* external symbols */
3908 if (i > 0)
3910 symbolic_header.cbExtOffset = file_offset;
3911 file_offset += i * sizeof (EXTR);
3912 file_offset = ALIGN_SYMTABLE_OFFSET (file_offset);
3917 /* Write out a varray at a given location. */
3919 static void
3920 write_varray (varray_t *vp, /* virtual array */
3921 off_t offset, /* offset to write varray to */
3922 const char *str) /* string to print out when tracing */
3924 int num_write, sys_write;
3925 vlinks_t *ptr;
3927 if (vp->num_allocated == 0)
3928 return;
3930 if (debug)
3931 fprintf (stderr, "\twarray\tvp = " HOST_PTR_PRINTF
3932 ", offset = %7lu, size = %7lu, %s\n",
3933 (void *) vp, (unsigned long) offset,
3934 vp->num_allocated * vp->object_size, str);
3936 if (file_offset != (unsigned long) offset
3937 && fseek (object_stream, (long) offset, SEEK_SET) < 0)
3938 pfatal_with_name (object_name);
3940 for (ptr = vp->first; ptr != (vlinks_t *) 0; ptr = ptr->next)
3942 num_write = (ptr->next == (vlinks_t *) 0)
3943 ? vp->objects_last_page * vp->object_size
3944 : vp->objects_per_page * vp->object_size;
3946 sys_write = fwrite (ptr->datum, 1, num_write, object_stream);
3947 if (sys_write <= 0)
3948 pfatal_with_name (object_name);
3950 else if (sys_write != num_write)
3951 fatal ("wrote %d bytes to %s, system returned %d",
3952 num_write,
3953 object_name,
3954 sys_write);
3956 file_offset += num_write;
3961 /* Write out the symbol table in the object file. */
3963 static void
3964 write_object (void)
3966 int sys_write;
3967 efdr_t *file_ptr;
3968 off_t offset;
3970 if (debug)
3971 fprintf (stderr, "\n\twrite\tvp = " HOST_PTR_PRINTF
3972 ", offset = %7u, size = %7lu, %s\n",
3973 (void *) &symbolic_header, 0,
3974 (unsigned long) sizeof (symbolic_header), "symbolic header");
3976 sys_write = fwrite (&symbolic_header,
3978 sizeof (symbolic_header),
3979 object_stream);
3981 if (sys_write < 0)
3982 pfatal_with_name (object_name);
3984 else if (sys_write != sizeof (symbolic_header))
3985 fatal ("wrote %d bytes to %s, system returned %d",
3986 (int) sizeof (symbolic_header),
3987 object_name,
3988 sys_write);
3991 file_offset = sizeof (symbolic_header) + orig_file_header.f_symptr;
3993 if (symbolic_header.cbLine > 0) /* line numbers */
3995 long sys_write;
3997 if (file_offset != (unsigned long) symbolic_header.cbLineOffset
3998 && fseek (object_stream, symbolic_header.cbLineOffset, SEEK_SET) != 0)
3999 pfatal_with_name (object_name);
4001 if (debug)
4002 fprintf (stderr, "\twrite\tvp = " HOST_PTR_PRINTF
4003 ", offset = %7lu, size = %7lu, %s\n",
4004 (void *) &orig_linenum, (long) symbolic_header.cbLineOffset,
4005 (long) symbolic_header.cbLine, "Line numbers");
4007 sys_write = fwrite (orig_linenum,
4009 symbolic_header.cbLine,
4010 object_stream);
4012 if (sys_write <= 0)
4013 pfatal_with_name (object_name);
4015 else if (sys_write != symbolic_header.cbLine)
4016 fatal ("wrote %ld bytes to %s, system returned %ld",
4017 (long) symbolic_header.cbLine,
4018 object_name,
4019 sys_write);
4021 file_offset = symbolic_header.cbLineOffset + symbolic_header.cbLine;
4024 if (symbolic_header.ioptMax > 0) /* optimization symbols */
4026 long sys_write;
4027 long num_write = symbolic_header.ioptMax * sizeof (OPTR);
4029 if (file_offset != (unsigned long) symbolic_header.cbOptOffset
4030 && fseek (object_stream, symbolic_header.cbOptOffset, SEEK_SET) != 0)
4031 pfatal_with_name (object_name);
4033 if (debug)
4034 fprintf (stderr, "\twrite\tvp = " HOST_PTR_PRINTF
4035 ", offset = %7lu, size = %7lu, %s\n",
4036 (void *) &orig_opt_syms, (long) symbolic_header.cbOptOffset,
4037 num_write, "Optimizer symbols");
4039 sys_write = fwrite (orig_opt_syms,
4041 num_write,
4042 object_stream);
4044 if (sys_write <= 0)
4045 pfatal_with_name (object_name);
4047 else if (sys_write != num_write)
4048 fatal ("wrote %ld bytes to %s, system returned %ld",
4049 num_write,
4050 object_name,
4051 sys_write);
4053 file_offset = symbolic_header.cbOptOffset + num_write;
4056 if (symbolic_header.idnMax > 0) /* dense numbers */
4057 write_varray (&dense_num, (off_t) symbolic_header.cbDnOffset, "Dense numbers");
4059 if (symbolic_header.ipdMax > 0) /* procedure tables */
4061 offset = symbolic_header.cbPdOffset;
4062 for (file_ptr = first_file;
4063 file_ptr != (efdr_t *) 0;
4064 file_ptr = file_ptr->next_file)
4066 write_varray (&file_ptr->procs, offset, "Procedure tables");
4067 offset = file_offset;
4071 if (symbolic_header.isymMax > 0) /* local symbols */
4073 offset = symbolic_header.cbSymOffset;
4074 for (file_ptr = first_file;
4075 file_ptr != (efdr_t *) 0;
4076 file_ptr = file_ptr->next_file)
4078 write_varray (&file_ptr->symbols, offset, "Local symbols");
4079 offset = file_offset;
4083 if (symbolic_header.iauxMax > 0) /* aux symbols */
4085 offset = symbolic_header.cbAuxOffset;
4086 for (file_ptr = first_file;
4087 file_ptr != (efdr_t *) 0;
4088 file_ptr = file_ptr->next_file)
4090 write_varray (&file_ptr->aux_syms, offset, "Aux. symbols");
4091 offset = file_offset;
4095 if (symbolic_header.issMax > 0) /* local strings */
4097 offset = symbolic_header.cbSsOffset;
4098 for (file_ptr = first_file;
4099 file_ptr != (efdr_t *) 0;
4100 file_ptr = file_ptr->next_file)
4102 write_varray (&file_ptr->strings, offset, "Local strings");
4103 offset = file_offset;
4107 if (symbolic_header.issExtMax > 0) /* external strings */
4108 write_varray (&ext_strings, symbolic_header.cbSsExtOffset, "External strings");
4110 if (symbolic_header.ifdMax > 0) /* file tables */
4112 offset = symbolic_header.cbFdOffset;
4113 if (file_offset != (unsigned long) offset
4114 && fseek (object_stream, (long) offset, SEEK_SET) < 0)
4115 pfatal_with_name (object_name);
4117 file_offset = offset;
4118 for (file_ptr = first_file;
4119 file_ptr != (efdr_t *) 0;
4120 file_ptr = file_ptr->next_file)
4122 if (debug)
4123 fprintf (stderr, "\twrite\tvp = " HOST_PTR_PRINTF
4124 ", offset = %7lu, size = %7lu, %s\n",
4125 (void *) &file_ptr->fdr, file_offset,
4126 (unsigned long) sizeof (FDR), "File header");
4128 sys_write = fwrite (&file_ptr->fdr,
4130 sizeof (FDR),
4131 object_stream);
4133 if (sys_write < 0)
4134 pfatal_with_name (object_name);
4136 else if (sys_write != sizeof (FDR))
4137 fatal ("wrote %d bytes to %s, system returned %d",
4138 (int) sizeof (FDR),
4139 object_name,
4140 sys_write);
4142 file_offset = offset += sizeof (FDR);
4146 if (symbolic_header.crfd > 0) /* relative file descriptors */
4148 long sys_write;
4149 symint_t num_write = symbolic_header.crfd * sizeof (symint_t);
4151 if (file_offset != (unsigned long) symbolic_header.cbRfdOffset
4152 && fseek (object_stream, symbolic_header.cbRfdOffset, SEEK_SET) != 0)
4153 pfatal_with_name (object_name);
4155 if (debug)
4156 fprintf (stderr, "\twrite\tvp = " HOST_PTR_PRINTF
4157 ", offset = %7lu, size = %7lu, %s\n",
4158 (void *) &orig_rfds, (long) symbolic_header.cbRfdOffset,
4159 num_write, "Relative file descriptors");
4161 sys_write = fwrite (orig_rfds,
4163 num_write,
4164 object_stream);
4166 if (sys_write <= 0)
4167 pfatal_with_name (object_name);
4169 else if (sys_write != (long) num_write)
4170 fatal ("wrote %lu bytes to %s, system returned %ld",
4171 num_write,
4172 object_name,
4173 sys_write);
4175 file_offset = symbolic_header.cbRfdOffset + num_write;
4178 if (symbolic_header.issExtMax > 0) /* external symbols */
4179 write_varray (&ext_symbols, (off_t) symbolic_header.cbExtOffset, "External symbols");
4181 if (fclose (object_stream) != 0)
4182 pfatal_with_name (object_name);
4186 /* Read some bytes at a specified location, and return a pointer. */
4188 static page_t *
4189 read_seek (size_t size, /* # bytes to read */
4190 off_t offset, /* offset to read at */
4191 const char *str) /* name for tracing */
4193 page_t *ptr;
4194 long sys_read = 0;
4196 if (size == 0) /* nothing to read */
4197 return (page_t *) 0;
4199 if (debug)
4200 fprintf (stderr,
4201 "\trseek\tsize = %7lu, offset = %7lu, currently at %7lu, %s\n",
4202 (unsigned long) size, (unsigned long) offset, file_offset, str);
4204 #ifndef MALLOC_CHECK
4205 ptr = allocate_multiple_pages ((size + PAGE_USIZE - 1) / PAGE_USIZE);
4206 #else
4207 ptr = xcalloc (1, size);
4208 #endif
4210 /* If we need to seek, and the distance is nearby, just do some reads,
4211 to speed things up. */
4212 if (file_offset != (unsigned long) offset)
4214 symint_t difference = offset - file_offset;
4216 if (difference < 8)
4218 char small_buffer[8];
4220 sys_read = fread (small_buffer, 1, difference, obj_in_stream);
4221 if (sys_read <= 0)
4222 pfatal_with_name (obj_in_name);
4224 if ((symint_t) sys_read != difference)
4225 fatal ("wanted to read %lu bytes from %s, system returned %ld",
4226 (unsigned long) size,
4227 obj_in_name,
4228 sys_read);
4230 else if (fseek (obj_in_stream, offset, SEEK_SET) < 0)
4231 pfatal_with_name (obj_in_name);
4234 sys_read = fread (ptr, 1, size, obj_in_stream);
4235 if (sys_read <= 0)
4236 pfatal_with_name (obj_in_name);
4238 if (sys_read != (long) size)
4239 fatal ("wanted to read %lu bytes from %s, system returned %ld",
4240 (unsigned long) size,
4241 obj_in_name,
4242 sys_read);
4244 file_offset = offset + size;
4246 if (file_offset > max_file_offset)
4247 max_file_offset = file_offset;
4249 return ptr;
4253 /* Read the existing object file (and copy to the output object file
4254 if it is different from the input object file), and remove the old
4255 symbol table. */
4257 static void
4258 copy_object (void)
4260 char buffer[ PAGE_SIZE ];
4261 int sys_read;
4262 int remaining;
4263 int num_write;
4264 int sys_write;
4265 int fd, es;
4266 int delete_ifd = 0;
4267 int *remap_file_number;
4268 struct stat stat_buf;
4270 if (debug)
4271 fprintf (stderr, "\tcopy\n");
4273 if (fstat (fileno (obj_in_stream), &stat_buf) != 0
4274 || fseek (obj_in_stream, 0L, SEEK_SET) != 0)
4275 pfatal_with_name (obj_in_name);
4277 sys_read = fread (&orig_file_header,
4279 sizeof (struct filehdr),
4280 obj_in_stream);
4282 if (sys_read < 0)
4283 pfatal_with_name (obj_in_name);
4285 else if (sys_read == 0 && feof (obj_in_stream))
4286 return; /* create a .T file sans file header */
4288 else if (sys_read < (int) sizeof (struct filehdr))
4289 fatal ("wanted to read %d bytes from %s, system returned %d",
4290 (int) sizeof (struct filehdr),
4291 obj_in_name,
4292 sys_read);
4295 if (orig_file_header.f_nsyms != sizeof (HDRR))
4296 fatal ("%s symbolic header wrong size (%ld bytes, should be %ld)",
4297 input_name, (long) orig_file_header.f_nsyms, (long) sizeof (HDRR));
4300 /* Read in the current symbolic header. */
4301 if (fseek (obj_in_stream, (long) orig_file_header.f_symptr, SEEK_SET) != 0)
4302 pfatal_with_name (input_name);
4304 sys_read = fread (&orig_sym_hdr,
4306 sizeof (orig_sym_hdr),
4307 obj_in_stream);
4309 if (sys_read < 0)
4310 pfatal_with_name (object_name);
4312 else if (sys_read < (int) sizeof (struct filehdr))
4313 fatal ("wanted to read %d bytes from %s, system returned %d",
4314 (int) sizeof (struct filehdr),
4315 obj_in_name,
4316 sys_read);
4319 /* Read in each of the sections if they exist in the object file.
4320 We read things in the order the mips assembler creates the
4321 sections, so in theory no extra seeks are done.
4323 For simplicity sake, round each read up to a page boundary,
4324 we may want to revisit this later.... */
4326 file_offset = orig_file_header.f_symptr + sizeof (struct filehdr);
4328 if (orig_sym_hdr.cbLine > 0) /* line numbers */
4329 orig_linenum = (char *) read_seek (orig_sym_hdr.cbLine,
4330 orig_sym_hdr.cbLineOffset,
4331 "Line numbers");
4333 if (orig_sym_hdr.ipdMax > 0) /* procedure tables */
4334 orig_procs = (PDR *) read_seek (orig_sym_hdr.ipdMax * sizeof (PDR),
4335 orig_sym_hdr.cbPdOffset,
4336 "Procedure tables");
4338 if (orig_sym_hdr.isymMax > 0) /* local symbols */
4339 orig_local_syms = (SYMR *) read_seek (orig_sym_hdr.isymMax * sizeof (SYMR),
4340 orig_sym_hdr.cbSymOffset,
4341 "Local symbols");
4343 if (orig_sym_hdr.iauxMax > 0) /* aux symbols */
4344 orig_aux_syms = (AUXU *) read_seek (orig_sym_hdr.iauxMax * sizeof (AUXU),
4345 orig_sym_hdr.cbAuxOffset,
4346 "Aux. symbols");
4348 if (orig_sym_hdr.issMax > 0) /* local strings */
4349 orig_local_strs = (char *) read_seek (orig_sym_hdr.issMax,
4350 orig_sym_hdr.cbSsOffset,
4351 "Local strings");
4353 if (orig_sym_hdr.issExtMax > 0) /* external strings */
4354 orig_ext_strs = (char *) read_seek (orig_sym_hdr.issExtMax,
4355 orig_sym_hdr.cbSsExtOffset,
4356 "External strings");
4358 if (orig_sym_hdr.ifdMax > 0) /* file tables */
4359 orig_files = (FDR *) read_seek (orig_sym_hdr.ifdMax * sizeof (FDR),
4360 orig_sym_hdr.cbFdOffset,
4361 "File tables");
4363 if (orig_sym_hdr.crfd > 0) /* relative file descriptors */
4364 orig_rfds = (symint_t *) read_seek (orig_sym_hdr.crfd * sizeof (symint_t),
4365 orig_sym_hdr.cbRfdOffset,
4366 "Relative file descriptors");
4368 if (orig_sym_hdr.issExtMax > 0) /* external symbols */
4369 orig_ext_syms = (EXTR *) read_seek (orig_sym_hdr.iextMax * sizeof (EXTR),
4370 orig_sym_hdr.cbExtOffset,
4371 "External symbols");
4373 if (orig_sym_hdr.idnMax > 0) /* dense numbers */
4375 orig_dense = (DNR *) read_seek (orig_sym_hdr.idnMax * sizeof (DNR),
4376 orig_sym_hdr.cbDnOffset,
4377 "Dense numbers");
4379 add_bytes (&dense_num, (char *) orig_dense, orig_sym_hdr.idnMax);
4382 if (orig_sym_hdr.ioptMax > 0) /* opt symbols */
4383 orig_opt_syms = (OPTR *) read_seek (orig_sym_hdr.ioptMax * sizeof (OPTR),
4384 orig_sym_hdr.cbOptOffset,
4385 "Optimizer symbols");
4389 /* The symbol table should be last. */
4390 if (max_file_offset != (unsigned long) stat_buf.st_size)
4391 fatal ("symbol table is not last (symbol table ends at %ld, .o ends at %ld",
4392 max_file_offset,
4393 (long) stat_buf.st_size);
4396 /* If the first original file descriptor is a dummy which the assembler
4397 put out, but there are no symbols in it, skip it now. */
4398 if (orig_sym_hdr.ifdMax > 1
4399 && orig_files->csym == 2
4400 && orig_files->caux == 0)
4402 char *filename = orig_local_strs + (orig_files->issBase + orig_files->rss);
4403 char *suffix = strrchr (filename, '.');
4405 if (suffix != (char *) 0 && strcmp (suffix, ".s") == 0)
4406 delete_ifd = 1;
4410 /* Create array to map original file numbers to the new file numbers
4411 (in case there are duplicate filenames, we collapse them into one
4412 file section, the MIPS assembler may or may not collapse them). */
4414 remap_file_number = (int *) alloca (sizeof (int) * orig_sym_hdr.ifdMax);
4416 for (fd = delete_ifd; fd < orig_sym_hdr.ifdMax; fd++)
4418 FDR *fd_ptr = ORIG_FILES (fd);
4419 char *filename = ORIG_LSTRS (fd_ptr->issBase + fd_ptr->rss);
4421 /* file support itself. */
4422 add_file (filename, filename + strlen (filename));
4423 remap_file_number[fd] = cur_file_ptr->file_index;
4426 if (delete_ifd > 0) /* just in case */
4427 remap_file_number[0] = remap_file_number[1];
4430 /* Loop, adding each of the external symbols. These must be in
4431 order or otherwise we would have to change the relocation
4432 entries. We don't just call add_bytes, because we need to have
4433 the names put into the external hash table. We set the type to
4434 'void' for now, and parse_def will fill in the correct type if it
4435 is in the symbol table. We must add the external symbols before
4436 the locals, since the locals do lookups against the externals. */
4438 if (debug)
4439 fprintf (stderr, "\tehash\n");
4441 for (es = 0; es < orig_sym_hdr.iextMax; es++)
4443 EXTR *eptr = orig_ext_syms + es;
4444 int ifd = eptr->ifd;
4446 (void) add_ext_symbol (eptr, ((long) ifd < orig_sym_hdr.ifdMax)
4447 ? remap_file_number[ ifd ] : ifd );
4451 /* For each of the files in the object file, copy the symbols, and such
4452 into the varrays for the new object file. */
4454 for (fd = delete_ifd; fd < orig_sym_hdr.ifdMax; fd++)
4456 FDR *fd_ptr = ORIG_FILES (fd);
4457 char *filename = ORIG_LSTRS (fd_ptr->issBase + fd_ptr->rss);
4458 SYMR *sym_start;
4459 SYMR *sym;
4460 SYMR *sym_end_p1;
4461 PDR *proc_start;
4462 PDR *proc;
4463 PDR *proc_end_p1;
4465 /* file support itself. */
4466 add_file (filename, filename + strlen (filename));
4467 cur_file_ptr->orig_fdr = fd_ptr;
4469 /* Copy stuff that's just passed through (such as line #'s) */
4470 cur_file_ptr->fdr.adr = fd_ptr->adr;
4471 cur_file_ptr->fdr.ilineBase = fd_ptr->ilineBase;
4472 cur_file_ptr->fdr.cline = fd_ptr->cline;
4473 cur_file_ptr->fdr.rfdBase = fd_ptr->rfdBase;
4474 cur_file_ptr->fdr.crfd = fd_ptr->crfd;
4475 cur_file_ptr->fdr.cbLineOffset = fd_ptr->cbLineOffset;
4476 cur_file_ptr->fdr.cbLine = fd_ptr->cbLine;
4477 cur_file_ptr->fdr.fMerge = fd_ptr->fMerge;
4478 cur_file_ptr->fdr.fReadin = fd_ptr->fReadin;
4479 cur_file_ptr->fdr.glevel = fd_ptr->glevel;
4481 if (debug)
4482 fprintf (stderr, "\thash\tstart, filename %s\n", filename);
4484 /* For each of the static and global symbols defined, add them
4485 to the hash table of original symbols, so we can look up
4486 their values. */
4488 sym_start = ORIG_LSYMS (fd_ptr->isymBase);
4489 sym_end_p1 = sym_start + fd_ptr->csym;
4490 for (sym = sym_start; sym < sym_end_p1; sym++)
4492 switch ((st_t) sym->st)
4494 default:
4495 break;
4497 case st_Global:
4498 case st_Static:
4499 case st_Label:
4500 case st_Proc:
4501 case st_StaticProc:
4503 auto symint_t hash_index;
4504 char *str = ORIG_LSTRS (fd_ptr->issBase + sym->iss);
4505 size_t len = strlen (str);
4506 shash_t *shash_ptr = hash_string (str,
4507 (ptrdiff_t) len,
4508 &orig_str_hash[0],
4509 &hash_index);
4511 if (shash_ptr != (shash_t *) 0)
4512 error ("internal error, %s is already in original symbol table", str);
4514 else
4516 shash_ptr = allocate_shash ();
4517 shash_ptr->next = orig_str_hash[hash_index];
4518 orig_str_hash[hash_index] = shash_ptr;
4520 shash_ptr->len = len;
4521 shash_ptr->indx = indexNil;
4522 shash_ptr->string = str;
4523 shash_ptr->sym_ptr = sym;
4526 break;
4528 case st_End:
4529 if ((sc_t) sym->sc == sc_Text)
4531 char *str = ORIG_LSTRS (fd_ptr->issBase + sym->iss);
4533 if (*str != '\0')
4535 size_t len = strlen (str);
4536 shash_t *shash_ptr = hash_string (str,
4537 (ptrdiff_t) len,
4538 &orig_str_hash[0],
4539 (symint_t *) 0);
4541 if (shash_ptr != (shash_t *) 0)
4542 shash_ptr->end_ptr = sym;
4545 break;
4550 if (debug)
4552 fprintf (stderr, "\thash\tdone, filename %s\n", filename);
4553 fprintf (stderr, "\tproc\tstart, filename %s\n", filename);
4556 /* Go through each of the procedures in this file, and add the
4557 procedure pointer to the hash entry for the given name. */
4559 proc_start = ORIG_PROCS (fd_ptr->ipdFirst);
4560 proc_end_p1 = proc_start + fd_ptr->cpd;
4561 for (proc = proc_start; proc < proc_end_p1; proc++)
4563 SYMR *proc_sym = ORIG_LSYMS (fd_ptr->isymBase + proc->isym);
4564 char *str = ORIG_LSTRS (fd_ptr->issBase + proc_sym->iss);
4565 size_t len = strlen (str);
4566 shash_t *shash_ptr = hash_string (str,
4567 (ptrdiff_t) len,
4568 &orig_str_hash[0],
4569 (symint_t *) 0);
4571 if (shash_ptr == (shash_t *) 0)
4572 error ("internal error, function %s is not in original symbol table", str);
4574 else
4575 shash_ptr->proc_ptr = proc;
4578 if (debug)
4579 fprintf (stderr, "\tproc\tdone, filename %s\n", filename);
4582 cur_file_ptr = first_file;
4585 /* Copy all of the object file up to the symbol table. Originally
4586 we were going to use ftruncate, but that doesn't seem to work
4587 on Ultrix 3.1.... */
4589 if (fseek (obj_in_stream, (long) 0, SEEK_SET) != 0)
4590 pfatal_with_name (obj_in_name);
4592 if (fseek (object_stream, (long) 0, SEEK_SET) != 0)
4593 pfatal_with_name (object_name);
4595 for (remaining = orig_file_header.f_symptr;
4596 remaining > 0;
4597 remaining -= num_write)
4599 num_write
4600 = (remaining <= (int) sizeof (buffer))
4601 ? remaining : (int) sizeof (buffer);
4602 sys_read = fread (buffer, 1, num_write, obj_in_stream);
4603 if (sys_read <= 0)
4604 pfatal_with_name (obj_in_name);
4606 else if (sys_read != num_write)
4607 fatal ("wanted to read %d bytes from %s, system returned %d",
4608 num_write,
4609 obj_in_name,
4610 sys_read);
4612 sys_write = fwrite (buffer, 1, num_write, object_stream);
4613 if (sys_write <= 0)
4614 pfatal_with_name (object_name);
4616 else if (sys_write != num_write)
4617 fatal ("wrote %d bytes to %s, system returned %d",
4618 num_write,
4619 object_name,
4620 sys_write);
4625 /* Ye olde main program. */
4627 extern int main (int, char **);
4630 main (int argc, char **argv)
4632 int iflag = 0;
4633 char *num_end;
4634 int option;
4635 int i;
4637 progname = lbasename (argv[0]);
4639 (void) signal (SIGSEGV, catch_signal);
4640 (void) signal (SIGBUS, catch_signal);
4641 (void) signal (SIGABRT, catch_signal);
4643 #ifndef lint
4644 if (sizeof (efdr_t) > PAGE_USIZE)
4645 fatal ("efdr_t has a sizeof %d bytes, when it should be less than %d",
4646 (int) sizeof (efdr_t),
4647 (int) PAGE_USIZE);
4649 if (sizeof (page_t) != PAGE_USIZE)
4650 fatal ("page_t has a sizeof %d bytes, when it should be %d",
4651 (int) sizeof (page_t),
4652 (int) PAGE_USIZE);
4654 #endif
4656 alloc_counts[ alloc_type_none ].alloc_name = "none";
4657 alloc_counts[ alloc_type_scope ].alloc_name = "scope";
4658 alloc_counts[ alloc_type_vlinks ].alloc_name = "vlinks";
4659 alloc_counts[ alloc_type_shash ].alloc_name = "shash";
4660 alloc_counts[ alloc_type_thash ].alloc_name = "thash";
4661 alloc_counts[ alloc_type_tag ].alloc_name = "tag";
4662 alloc_counts[ alloc_type_forward ].alloc_name = "forward";
4663 alloc_counts[ alloc_type_thead ].alloc_name = "thead";
4664 alloc_counts[ alloc_type_varray ].alloc_name = "varray";
4666 int_type_info = type_info_init;
4667 int_type_info.basic_type = bt_Int;
4669 void_type_info = type_info_init;
4670 void_type_info.basic_type = bt_Void;
4672 while ((option = getopt_long (argc, argv, "d:i:I:o:v", options, NULL)) != -1)
4673 switch (option)
4675 default:
4676 had_errors++;
4677 break;
4679 case 'd':
4680 debug = strtol (optarg, &num_end, 0);
4681 if ((unsigned) debug > 4 || num_end == optarg)
4682 had_errors++;
4684 break;
4686 case 'I':
4687 if (rename_output || obj_in_name != (char *) 0)
4688 had_errors++;
4689 else
4690 rename_output = 1;
4692 /* Fall through to 'i' case. */
4694 case 'i':
4695 if (obj_in_name == (char *) 0)
4697 obj_in_name = optarg;
4698 iflag++;
4700 else
4701 had_errors++;
4702 break;
4704 case 'o':
4705 if (object_name == (char *) 0)
4706 object_name = optarg;
4707 else
4708 had_errors++;
4709 break;
4711 case 'v':
4712 verbose++;
4713 break;
4715 case 'V':
4716 version++;
4717 break;
4720 if (version)
4722 printf (_("mips-tfile %s%s\n"), pkgversion_string, version_string);
4723 fputs ("Copyright (C) 2011 Free Software Foundation, Inc.\n", stdout);
4724 fputs (_("This is free software; see the source for copying conditions. There is NO\n\
4725 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\n"),
4726 stdout);
4727 exit (0);
4730 if (obj_in_name == (char *) 0 && optind <= argc - 2)
4731 obj_in_name = argv[--argc];
4733 if (object_name == (char *) 0 && optind <= argc - 2)
4734 object_name = argv[--argc];
4736 /* If there is an output name, but no input name use
4737 the same file for both, deleting the name between
4738 opening it for input and opening it for output. */
4739 if (obj_in_name == (char *) 0 && object_name != (char *) 0)
4741 obj_in_name = object_name;
4742 delete_input = 1;
4745 if (optind != argc - 1)
4746 had_errors++;
4748 if (verbose || had_errors)
4749 fprintf (stderr, _("mips-tfile (GCC) %s\n"), version_string);
4751 if (object_name == (char *) 0 || had_errors)
4753 fprintf (stderr, _("Calling Sequence:\n"));
4754 fprintf (stderr, _("\tmips-tfile [-d <num>] [-v] [-i <o-in-file>] -o <o-out-file> <s-file> (or)\n"));
4755 fprintf (stderr, _("\tmips-tfile [-d <num>] [-v] [-I <o-in-file>] -o <o-out-file> <s-file> (or)\n"));
4756 fprintf (stderr, _("\tmips-tfile [-d <num>] [-v] <s-file> <o-in-file> <o-out-file>\n"));
4757 fprintf (stderr, "\n");
4758 fprintf (stderr, _("Debug levels are:\n"));
4759 fprintf (stderr, _(" 1\tGeneral debug + trace functions/blocks.\n"));
4760 fprintf (stderr, _(" 2\tDebug level 1 + trace externals.\n"));
4761 fprintf (stderr, _(" 3\tDebug level 2 + trace all symbols.\n"));
4762 fprintf (stderr, _(" 4\tDebug level 3 + trace memory allocations.\n"));
4763 return 1;
4766 if (obj_in_name == (char *) 0)
4767 obj_in_name = object_name;
4769 if (rename_output && rename (object_name, obj_in_name) != 0)
4771 char *buffer = (char *) allocate_multiple_pages (4);
4772 int len;
4773 int len2;
4774 int in_fd;
4775 int out_fd;
4777 /* Rename failed, copy input file */
4778 in_fd = open (object_name, O_RDONLY, 0666);
4779 if (in_fd < 0)
4780 pfatal_with_name (object_name);
4782 out_fd = open (obj_in_name, O_WRONLY | O_CREAT | O_TRUNC, 0666);
4783 if (out_fd < 0)
4784 pfatal_with_name (obj_in_name);
4786 while ((len = read (in_fd, buffer, 4*PAGE_SIZE)) > 0)
4788 len2 = write (out_fd, buffer, len);
4789 if (len2 < 0)
4790 pfatal_with_name (object_name);
4792 if (len != len2)
4793 fatal ("wrote %d bytes to %s, expected to write %d", len2, obj_in_name, len);
4796 free_multiple_pages ((page_t *) buffer, 4);
4798 if (len < 0)
4799 pfatal_with_name (object_name);
4801 if (close (in_fd) < 0)
4802 pfatal_with_name (object_name);
4804 if (close (out_fd) < 0)
4805 pfatal_with_name (obj_in_name);
4808 /* Must open input before output, since the output may be the same file, and
4809 we need to get the input handle before truncating it. */
4810 obj_in_stream = fopen (obj_in_name, "r");
4811 if (obj_in_stream == (FILE *) 0)
4812 pfatal_with_name (obj_in_name);
4814 if (delete_input && unlink (obj_in_name) != 0)
4815 pfatal_with_name (obj_in_name);
4817 object_stream = fopen (object_name, "w");
4818 if (object_stream == (FILE *) 0)
4819 pfatal_with_name (object_name);
4821 if (strcmp (argv[optind], "-") != 0)
4823 input_name = argv[optind];
4824 if (freopen (argv[optind], "r", stdin) != stdin)
4825 pfatal_with_name (argv[optind]);
4828 copy_object (); /* scan & copy object file */
4829 parse_input (); /* scan all of input */
4831 update_headers (); /* write out tfile */
4832 write_object ();
4834 if (debug)
4836 fprintf (stderr, "\n\tAllocation summary:\n\n");
4837 for (i = (int) alloc_type_none; i < (int) alloc_type_last; i++)
4838 if (alloc_counts[i].total_alloc)
4840 fprintf (stderr,
4841 "\t%s\t%5d allocation(s), %5d free(s), %2d page(s)\n",
4842 alloc_counts[i].alloc_name,
4843 alloc_counts[i].total_alloc,
4844 alloc_counts[i].total_free,
4845 alloc_counts[i].total_pages);
4849 return (had_errors) ? 1 : 0;
4853 /* Catch a signal and exit without dumping core. */
4855 static void
4856 catch_signal (int signum)
4858 (void) signal (signum, SIG_DFL); /* just in case... */
4859 fatal ("%s", strsignal (signum));
4862 /* Print a fatal error message. NAME is the text.
4863 Also include a system error message based on `errno'. */
4865 void
4866 pfatal_with_name (const char *msg)
4868 int save_errno = errno; /* just in case.... */
4869 if (line_number > 0)
4870 fprintf (stderr, "%s, %s:%ld ", progname, input_name, line_number);
4871 else
4872 fprintf (stderr, "%s:", progname);
4874 errno = save_errno;
4875 if (errno == 0)
4876 fprintf (stderr, "[errno = 0] %s\n", msg);
4877 else
4878 perror (msg);
4880 exit (1);
4884 /* Procedure to die with an out of bounds error message. It has
4885 type int, so it can be used with an ?: expression within the
4886 ORIG_xxx macros, but the function never returns. */
4888 static int
4889 out_of_bounds (symint_t indx, /* index that is out of bounds */
4890 symint_t max, /* maximum index */
4891 const char *str, /* string to print out */
4892 int prog_line) /* line number within mips-tfile.c */
4894 if (indx < max) /* just in case */
4895 return 0;
4897 fprintf (stderr, "%s, %s:%ld index %lu is out of bounds for %s, max is %lu, mips-tfile.c line# %d\n",
4898 progname, input_name, line_number, indx, str, max, prog_line);
4900 exit (1);
4901 return 0; /* turn off warning messages */
4905 /* Allocate a cluster of pages. USE_MALLOC says that malloc does not
4906 like sbrk's behind its back (or sbrk isn't available). If we use
4907 sbrk, we assume it gives us zeroed pages. */
4909 #ifndef MALLOC_CHECK
4910 #ifdef USE_MALLOC
4912 static page_t *
4913 allocate_cluster (size_t npages)
4915 page_t *value = xcalloc (npages, PAGE_USIZE);
4917 if (debug > 3)
4918 fprintf (stderr, "\talloc\tnpages = %d, value = 0x%.8x\n", npages, value);
4920 return value;
4923 #else /* USE_MALLOC */
4925 static page_t *
4926 allocate_cluster (size_t npages)
4928 page_t *ptr = (page_t *) sbrk (0); /* current sbreak */
4929 unsigned long offset = ((unsigned long) ptr) & (PAGE_SIZE - 1);
4931 if (offset != 0) /* align to a page boundary */
4933 if (sbrk (PAGE_USIZE - offset) == (char *)-1)
4934 pfatal_with_name ("allocate_cluster");
4936 ptr = (page_t *) (((char *) ptr) + PAGE_SIZE - offset);
4939 if (sbrk (npages * PAGE_USIZE) == (char *) -1)
4940 pfatal_with_name ("allocate_cluster");
4942 if (debug > 3)
4943 fprintf (stderr, "\talloc\tnpages = %lu, value = " HOST_PTR_PRINTF "\n",
4944 (unsigned long) npages, (void *) ptr);
4946 return ptr;
4949 #endif /* USE_MALLOC */
4952 static page_t *cluster_ptr = NULL;
4953 static unsigned pages_left = 0;
4955 #endif /* MALLOC_CHECK */
4958 /* Allocate some pages (which is initialized to 0). */
4960 static page_t *
4961 allocate_multiple_pages (size_t npages)
4963 #ifndef MALLOC_CHECK
4964 if (pages_left == 0 && npages < MAX_CLUSTER_PAGES)
4966 pages_left = MAX_CLUSTER_PAGES;
4967 cluster_ptr = allocate_cluster (MAX_CLUSTER_PAGES);
4970 if (npages <= pages_left)
4972 page_t *ptr = cluster_ptr;
4973 cluster_ptr += npages;
4974 pages_left -= npages;
4975 return ptr;
4978 return allocate_cluster (npages);
4980 #else /* MALLOC_CHECK */
4981 return xcalloc (npages, PAGE_SIZE);
4983 #endif /* MALLOC_CHECK */
4987 /* Release some pages. */
4989 static void
4990 free_multiple_pages (page_t *page_ptr, size_t npages)
4992 #ifndef MALLOC_CHECK
4993 if (pages_left == 0)
4995 cluster_ptr = page_ptr;
4996 pages_left = npages;
4999 else if ((page_ptr + npages) == cluster_ptr)
5001 cluster_ptr -= npages;
5002 pages_left += npages;
5005 /* otherwise the page is not freed. If more than call is
5006 done, we probably should worry about it, but at present,
5007 the free pages is done right after an allocate. */
5009 #else /* MALLOC_CHECK */
5010 free (page_ptr);
5012 #endif /* MALLOC_CHECK */
5016 /* Allocate one page (which is initialized to 0). */
5018 static page_t *
5019 allocate_page (void)
5021 #ifndef MALLOC_CHECK
5022 if (pages_left == 0)
5024 pages_left = MAX_CLUSTER_PAGES;
5025 cluster_ptr = allocate_cluster (MAX_CLUSTER_PAGES);
5028 pages_left--;
5029 return cluster_ptr++;
5031 #else /* MALLOC_CHECK */
5032 return xcalloc (1, PAGE_SIZE);
5034 #endif /* MALLOC_CHECK */
5038 /* Allocate scoping information. */
5040 static scope_t *
5041 allocate_scope (void)
5043 scope_t *ptr;
5044 static scope_t initial_scope;
5046 #ifndef MALLOC_CHECK
5047 ptr = alloc_counts[ (int) alloc_type_scope ].free_list.f_scope;
5048 if (ptr != (scope_t *) 0)
5049 alloc_counts[ (int) alloc_type_scope ].free_list.f_scope = ptr->free;
5051 else
5053 int unallocated = alloc_counts[ (int) alloc_type_scope ].unallocated;
5054 page_t *cur_page = alloc_counts[ (int) alloc_type_scope ].cur_page;
5056 if (unallocated == 0)
5058 unallocated = PAGE_SIZE / sizeof (scope_t);
5059 alloc_counts[ (int) alloc_type_scope ].cur_page = cur_page = allocate_page ();
5060 alloc_counts[ (int) alloc_type_scope ].total_pages++;
5063 ptr = &cur_page->scope[ --unallocated ];
5064 alloc_counts[ (int) alloc_type_scope ].unallocated = unallocated;
5067 #else
5068 ptr = xmalloc (sizeof (scope_t));
5070 #endif
5072 alloc_counts[ (int) alloc_type_scope ].total_alloc++;
5073 *ptr = initial_scope;
5074 return ptr;
5077 /* Free scoping information. */
5079 static void
5080 free_scope (scope_t *ptr)
5082 alloc_counts[ (int) alloc_type_scope ].total_free++;
5084 #ifndef MALLOC_CHECK
5085 ptr->free = alloc_counts[ (int) alloc_type_scope ].free_list.f_scope;
5086 alloc_counts[ (int) alloc_type_scope ].free_list.f_scope = ptr;
5088 #else
5089 free (ptr);
5090 #endif
5095 /* Allocate links for pages in a virtual array. */
5097 static vlinks_t *
5098 allocate_vlinks (void)
5100 vlinks_t *ptr;
5101 static vlinks_t initial_vlinks;
5103 #ifndef MALLOC_CHECK
5104 int unallocated = alloc_counts[ (int) alloc_type_vlinks ].unallocated;
5105 page_t *cur_page = alloc_counts[ (int) alloc_type_vlinks ].cur_page;
5107 if (unallocated == 0)
5109 unallocated = PAGE_SIZE / sizeof (vlinks_t);
5110 alloc_counts[ (int) alloc_type_vlinks ].cur_page = cur_page = allocate_page ();
5111 alloc_counts[ (int) alloc_type_vlinks ].total_pages++;
5114 ptr = &cur_page->vlinks[ --unallocated ];
5115 alloc_counts[ (int) alloc_type_vlinks ].unallocated = unallocated;
5117 #else
5118 ptr = xmalloc (sizeof (vlinks_t));
5120 #endif
5122 alloc_counts[ (int) alloc_type_vlinks ].total_alloc++;
5123 *ptr = initial_vlinks;
5124 return ptr;
5128 /* Allocate string hash buckets. */
5130 static shash_t *
5131 allocate_shash (void)
5133 shash_t *ptr;
5134 static shash_t initial_shash;
5136 #ifndef MALLOC_CHECK
5137 int unallocated = alloc_counts[ (int) alloc_type_shash ].unallocated;
5138 page_t *cur_page = alloc_counts[ (int) alloc_type_shash ].cur_page;
5140 if (unallocated == 0)
5142 unallocated = PAGE_SIZE / sizeof (shash_t);
5143 alloc_counts[ (int) alloc_type_shash ].cur_page = cur_page = allocate_page ();
5144 alloc_counts[ (int) alloc_type_shash ].total_pages++;
5147 ptr = &cur_page->shash[ --unallocated ];
5148 alloc_counts[ (int) alloc_type_shash ].unallocated = unallocated;
5150 #else
5151 ptr = xmalloc (sizeof (shash_t));
5153 #endif
5155 alloc_counts[ (int) alloc_type_shash ].total_alloc++;
5156 *ptr = initial_shash;
5157 return ptr;
5161 /* Allocate type hash buckets. */
5163 static thash_t *
5164 allocate_thash (void)
5166 thash_t *ptr;
5167 static thash_t initial_thash;
5169 #ifndef MALLOC_CHECK
5170 int unallocated = alloc_counts[ (int) alloc_type_thash ].unallocated;
5171 page_t *cur_page = alloc_counts[ (int) alloc_type_thash ].cur_page;
5173 if (unallocated == 0)
5175 unallocated = PAGE_SIZE / sizeof (thash_t);
5176 alloc_counts[ (int) alloc_type_thash ].cur_page = cur_page = allocate_page ();
5177 alloc_counts[ (int) alloc_type_thash ].total_pages++;
5180 ptr = &cur_page->thash[ --unallocated ];
5181 alloc_counts[ (int) alloc_type_thash ].unallocated = unallocated;
5183 #else
5184 ptr = xmalloc (sizeof (thash_t));
5186 #endif
5188 alloc_counts[ (int) alloc_type_thash ].total_alloc++;
5189 *ptr = initial_thash;
5190 return ptr;
5194 /* Allocate structure, union, or enum tag information. */
5196 static tag_t *
5197 allocate_tag (void)
5199 tag_t *ptr;
5200 static tag_t initial_tag;
5202 #ifndef MALLOC_CHECK
5203 ptr = alloc_counts[ (int) alloc_type_tag ].free_list.f_tag;
5204 if (ptr != (tag_t *) 0)
5205 alloc_counts[ (int) alloc_type_tag ].free_list.f_tag = ptr->free;
5207 else
5209 int unallocated = alloc_counts[ (int) alloc_type_tag ].unallocated;
5210 page_t *cur_page = alloc_counts[ (int) alloc_type_tag ].cur_page;
5212 if (unallocated == 0)
5214 unallocated = PAGE_SIZE / sizeof (tag_t);
5215 alloc_counts[ (int) alloc_type_tag ].cur_page = cur_page = allocate_page ();
5216 alloc_counts[ (int) alloc_type_tag ].total_pages++;
5219 ptr = &cur_page->tag[ --unallocated ];
5220 alloc_counts[ (int) alloc_type_tag ].unallocated = unallocated;
5223 #else
5224 ptr = xmalloc (sizeof (tag_t));
5226 #endif
5228 alloc_counts[ (int) alloc_type_tag ].total_alloc++;
5229 *ptr = initial_tag;
5230 return ptr;
5233 /* Free scoping information. */
5235 static void
5236 free_tag (tag_t *ptr)
5238 alloc_counts[ (int) alloc_type_tag ].total_free++;
5240 #ifndef MALLOC_CHECK
5241 ptr->free = alloc_counts[ (int) alloc_type_tag ].free_list.f_tag;
5242 alloc_counts[ (int) alloc_type_tag ].free_list.f_tag = ptr;
5244 #else
5245 free (ptr);
5246 #endif
5251 /* Allocate forward reference to a yet unknown tag. */
5253 static forward_t *
5254 allocate_forward (void)
5256 forward_t *ptr;
5257 static forward_t initial_forward;
5259 #ifndef MALLOC_CHECK
5260 ptr = alloc_counts[ (int) alloc_type_forward ].free_list.f_forward;
5261 if (ptr != (forward_t *) 0)
5262 alloc_counts[ (int) alloc_type_forward ].free_list.f_forward = ptr->free;
5264 else
5266 int unallocated = alloc_counts[ (int) alloc_type_forward ].unallocated;
5267 page_t *cur_page = alloc_counts[ (int) alloc_type_forward ].cur_page;
5269 if (unallocated == 0)
5271 unallocated = PAGE_SIZE / sizeof (forward_t);
5272 alloc_counts[ (int) alloc_type_forward ].cur_page = cur_page = allocate_page ();
5273 alloc_counts[ (int) alloc_type_forward ].total_pages++;
5276 ptr = &cur_page->forward[ --unallocated ];
5277 alloc_counts[ (int) alloc_type_forward ].unallocated = unallocated;
5280 #else
5281 ptr = xmalloc (sizeof (forward_t));
5283 #endif
5285 alloc_counts[ (int) alloc_type_forward ].total_alloc++;
5286 *ptr = initial_forward;
5287 return ptr;
5290 /* Free scoping information. */
5292 static void
5293 free_forward (forward_t *ptr)
5295 alloc_counts[ (int) alloc_type_forward ].total_free++;
5297 #ifndef MALLOC_CHECK
5298 ptr->free = alloc_counts[ (int) alloc_type_forward ].free_list.f_forward;
5299 alloc_counts[ (int) alloc_type_forward ].free_list.f_forward = ptr;
5301 #else
5302 free (ptr);
5303 #endif
5308 /* Allocate head of type hash list. */
5310 static thead_t *
5311 allocate_thead (void)
5313 thead_t *ptr;
5314 static thead_t initial_thead;
5316 #ifndef MALLOC_CHECK
5317 ptr = alloc_counts[ (int) alloc_type_thead ].free_list.f_thead;
5318 if (ptr != (thead_t *) 0)
5319 alloc_counts[ (int) alloc_type_thead ].free_list.f_thead = ptr->free;
5321 else
5323 int unallocated = alloc_counts[ (int) alloc_type_thead ].unallocated;
5324 page_t *cur_page = alloc_counts[ (int) alloc_type_thead ].cur_page;
5326 if (unallocated == 0)
5328 unallocated = PAGE_SIZE / sizeof (thead_t);
5329 alloc_counts[ (int) alloc_type_thead ].cur_page = cur_page = allocate_page ();
5330 alloc_counts[ (int) alloc_type_thead ].total_pages++;
5333 ptr = &cur_page->thead[ --unallocated ];
5334 alloc_counts[ (int) alloc_type_thead ].unallocated = unallocated;
5337 #else
5338 ptr = xmalloc (sizeof (thead_t));
5340 #endif
5342 alloc_counts[ (int) alloc_type_thead ].total_alloc++;
5343 *ptr = initial_thead;
5344 return ptr;
5347 /* Free scoping information. */
5349 static void
5350 free_thead (thead_t *ptr)
5352 alloc_counts[ (int) alloc_type_thead ].total_free++;
5354 #ifndef MALLOC_CHECK
5355 ptr->free = (thead_t *) alloc_counts[ (int) alloc_type_thead ].free_list.f_thead;
5356 alloc_counts[ (int) alloc_type_thead ].free_list.f_thead = ptr;
5358 #else
5359 free (ptr);
5360 #endif
5364 /* Output an error message and exit. */
5366 void
5367 fatal (const char *format, ...)
5369 va_list ap;
5371 va_start (ap, format);
5373 if (line_number > 0)
5374 fprintf (stderr, "%s, %s:%ld ", progname, input_name, line_number);
5375 else
5376 fprintf (stderr, "%s:", progname);
5378 vfprintf (stderr, format, ap);
5379 va_end (ap);
5380 fprintf (stderr, "\n");
5381 if (line_number > 0)
5382 fprintf (stderr, "line:\t%s\n", cur_line_start);
5384 exit (1);
5387 void
5388 error (const char *format, ...)
5390 va_list ap;
5392 va_start (ap, format);
5394 if (line_number > 0)
5395 fprintf (stderr, "%s, %s:%ld ", progname, input_name, line_number);
5396 else
5397 fprintf (stderr, "%s:", progname);
5399 vfprintf (stderr, format, ap);
5400 fprintf (stderr, "\n");
5401 if (line_number > 0)
5402 fprintf (stderr, "line:\t%s\n", cur_line_start);
5404 had_errors++;
5405 va_end (ap);
5408 /* More 'friendly' abort that prints the line and file. */
5410 void
5411 fancy_abort (const char *file, int line, const char *func)
5413 fatal ("abort in %s, at %s:%d", func, file, line);
5417 /* When `malloc.c' is compiled with `rcheck' defined,
5418 it calls this function to report clobberage. */
5420 void
5421 botch (const char *s)
5423 fatal ("%s", s);