1/* ECOFF debugging support.
2   Copyright (C) 1993-2020 Free Software Foundation, Inc.
3   Contributed by Cygnus Support.
4   This file was put together by Ian Lance Taylor <ian@cygnus.com>.  A
5   good deal of it comes directly from mips-tfile.c, by Michael
6   Meissner <meissner@osf.org>.
7
8   This file is part of GAS.
9
10   GAS is free software; you can redistribute it and/or modify
11   it under the terms of the GNU General Public License as published by
12   the Free Software Foundation; either version 3, or (at your option)
13   any later version.
14
15   GAS is distributed in the hope that it will be useful,
16   but WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   GNU General Public License for more details.
19
20   You should have received a copy of the GNU General Public License
21   along with GAS; see the file COPYING.  If not, write to the Free
22   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23   02110-1301, USA.  */
24
25#include "as.h"
26
27/* This file is compiled conditionally for those targets which use
28   ECOFF debugging information (e.g., MIPS ELF, Alpha ECOFF).  */
29
30#include "ecoff.h"
31
32#ifdef ECOFF_DEBUGGING
33
34#include "coff/internal.h"
35#include "coff/symconst.h"
36#include "aout/stab_gnu.h"
37#include "filenames.h"
38#include "safe-ctype.h"
39
40/* Why isn't this in coff/sym.h?  */
41#define ST_RFDESCAPE 0xfff
42
43/* This file constructs the information used by the ECOFF debugging
44   format.  It just builds a large block of data.
45
46   We support both ECOFF style debugging and stabs debugging (the
47   stabs symbols are encapsulated in ECOFF symbols).  This should let
48   us handle anything the compiler might throw at us.  */
49
50/* Here is a brief description of the MIPS ECOFF symbol table, by
51   Michael Meissner.  The MIPS symbol table has the following pieces:
52
53	Symbolic Header
54	    |
55	    +--	Auxiliary Symbols
56	    |
57	    +--	Dense number table
58	    |
59	    +--	Optimizer Symbols
60	    |
61	    +--	External Strings
62	    |
63	    +--	External Symbols
64	    |
65	    +--	Relative file descriptors
66	    |
67	    +--	File table
68		    |
69		    +--	Procedure table
70		    |
71		    +--	Line number table
72		    |
73		    +--	Local Strings
74		    |
75		    +--	Local Symbols
76
77   The symbolic header points to each of the other tables, and also
78   contains the number of entries.  It also contains a magic number
79   and MIPS compiler version number, such as 2.0.
80
81   The auxiliary table is a series of 32 bit integers, that are
82   referenced as needed from the local symbol table.  Unlike standard
83   COFF, the aux.  information does not follow the symbol that uses
84   it, but rather is a separate table.  In theory, this would allow
85   the MIPS compilers to collapse duplicate aux. entries, but I've not
86   noticed this happening with the 1.31 compiler suite.  The different
87   types of aux. entries are:
88
89    1)	dnLow: Low bound on array dimension.
90
91    2)	dnHigh: High bound on array dimension.
92
93    3)	isym: Index to the local symbol which is the start of the
94	function for the end of function first aux. entry.
95
96    4)	width: Width of structures and bitfields.
97
98    5)	count: Count of ranges for variant part.
99
100    6)	rndx: A relative index into the symbol table.  The relative
101	index field has two parts: rfd which is a pointer into the
102	relative file index table or ST_RFDESCAPE which says the next
103	aux. entry is the file number, and index: which is the pointer
104	into the local symbol within a given file table.  This is for
105	things like references to types defined in another file.
106
107    7)	Type information: This is like the COFF type bits, except it
108	is 32 bits instead of 16; they still have room to add new
109	basic types; and they can handle more than 6 levels of array,
110	pointer, function, etc.  Each type information field contains
111	the following structure members:
112
113	    a)	fBitfield: a bit that says this is a bitfield, and the
114		size in bits follows as the next aux. entry.
115
116	    b)	continued: a bit that says the next aux. entry is a
117		continuation of the current type information (in case
118		there are more than 6 levels of array/ptr/function).
119
120	    c)	bt: an integer containing the base type before adding
121		array, pointer, function, etc. qualifiers.  The
122		current base types that I have documentation for are:
123
124			btNil		-- undefined
125			btAdr		-- address - integer same size as ptr
126			btChar		-- character
127			btUChar		-- unsigned character
128			btShort		-- short
129			btUShort	-- unsigned short
130			btInt		-- int
131			btUInt		-- unsigned int
132			btLong		-- long
133			btULong		-- unsigned long
134			btFloat		-- float (real)
135			btDouble	-- Double (real)
136			btStruct	-- Structure (Record)
137			btUnion		-- Union (variant)
138			btEnum		-- Enumerated
139			btTypedef	-- defined via a typedef isymRef
140			btRange		-- subrange of int
141			btSet		-- pascal sets
142			btComplex	-- fortran complex
143			btDComplex	-- fortran double complex
144			btIndirect	-- forward or unnamed typedef
145			btFixedDec	-- Fixed Decimal
146			btFloatDec	-- Float Decimal
147			btString	-- Varying Length Character String
148			btBit		-- Aligned Bit String
149			btPicture	-- Picture
150			btVoid		-- Void (MIPS cc revision >= 2.00)
151
152	    d)	tq0 - tq5: type qualifier fields as needed.  The
153		current type qualifier fields I have documentation for
154		are:
155
156			tqNil		-- no more qualifiers
157			tqPtr		-- pointer
158			tqProc		-- procedure
159			tqArray		-- array
160			tqFar		-- 8086 far pointers
161			tqVol		-- volatile
162
163   The dense number table is used in the front ends, and disappears by
164   the time the .o is created.
165
166   With the 1.31 compiler suite, the optimization symbols don't seem
167   to be used as far as I can tell.
168
169   The linker is the first entity that creates the relative file
170   descriptor table, and I believe it is used so that the individual
171   file table pointers don't have to be rewritten when the objects are
172   merged together into the program file.
173
174   Unlike COFF, the basic symbol & string tables are split into
175   external and local symbols/strings.  The relocation information
176   only goes off of the external symbol table, and the debug
177   information only goes off of the internal symbol table.  The
178   external symbols can have links to an appropriate file index and
179   symbol within the file to give it the appropriate type information.
180   Because of this, the external symbols are actually larger than the
181   internal symbols (to contain the link information), and contain the
182   local symbol structure as a member, though this member is not the
183   first member of the external symbol structure (!).  I suspect this
184   split is to make strip easier to deal with.
185
186   Each file table has offsets for where the line numbers, local
187   strings, local symbols, and procedure table starts from within the
188   global tables, and the indices are reset to 0 for each of those
189   tables for the file.
190
191   The procedure table contains the binary equivalents of the .ent
192   (start of the function address), .frame (what register is the
193   virtual frame pointer, constant offset from the register to obtain
194   the VFP, and what register holds the return address), .mask/.fmask
195   (bitmask of saved registers, and where the first register is stored
196   relative to the VFP) assembler directives.  It also contains the
197   low and high bounds of the line numbers if debugging is turned on.
198
199   The line number table is a compressed form of the normal COFF line
200   table.  Each line number entry is either 1 or 3 bytes long, and
201   contains a signed delta from the previous line, and an unsigned
202   count of the number of instructions this statement takes.
203
204   The local symbol table contains the following fields:
205
206    1)	iss: index to the local string table giving the name of the
207	symbol.
208
209    2)	value: value of the symbol (address, register number, etc.).
210
211    3)	st: symbol type.  The current symbol types are:
212
213	    stNil	  -- Nuthin' special
214	    stGlobal	  -- external symbol
215	    stStatic	  -- static
216	    stParam	  -- procedure argument
217	    stLocal	  -- local variable
218	    stLabel	  -- label
219	    stProc	  -- External Procedure
220	    stBlock	  -- beginning of block
221	    stEnd	  -- end (of anything)
222	    stMember	  -- member (of anything)
223	    stTypedef	  -- type definition
224	    stFile	  -- file name
225	    stRegReloc	  -- register relocation
226	    stForward	  -- forwarding address
227	    stStaticProc  -- Static procedure
228	    stConstant	  -- const
229
230    4)	sc: storage class.  The current storage classes are:
231
232	    scText	  -- text symbol
233	    scData	  -- initialized data symbol
234	    scBss	  -- un-initialized data symbol
235	    scRegister	  -- value of symbol is register number
236	    scAbs	  -- value of symbol is absolute
237	    scUndefined   -- who knows?
238	    scCdbLocal	  -- variable's value is IN se->va.??
239	    scBits	  -- this is a bit field
240	    scCdbSystem	  -- value is IN debugger's address space
241	    scRegImage	  -- register value saved on stack
242	    scInfo	  -- symbol contains debugger information
243	    scUserStruct  -- addr in struct user for current process
244	    scSData	  -- load time only small data
245	    scSBss	  -- load time only small common
246	    scRData	  -- load time only read only data
247	    scVar	  -- Var parameter (fortranpascal)
248	    scCommon	  -- common variable
249	    scSCommon	  -- small common
250	    scVarRegister -- Var parameter in a register
251	    scVariant	  -- Variant record
252	    scSUndefined  -- small undefined(external) data
253	    scInit	  -- .init section symbol
254
255    5)	index: pointer to a local symbol or aux. entry.
256
257   For the following program:
258
259	#include <stdio.h>
260
261	main(){
262		printf("Hello World!\n");
263		return 0;
264	}
265
266   Mips-tdump produces the following information:
267
268   Global file header:
269       magic number             0x162
270       # sections               2
271       timestamp                645311799, Wed Jun 13 17:16:39 1990
272       symbolic header offset   284
273       symbolic header size     96
274       optional header          56
275       flags                    0x0
276
277   Symbolic header, magic number = 0x7009, vstamp = 1.31:
278
279       Info                      Offset      Number       Bytes
280       ====                      ======      ======      =====
281
282       Line numbers                 380           4           4 [13]
283       Dense numbers                  0           0           0
284       Procedures Tables            384           1          52
285       Local Symbols                436          16         192
286       Optimization Symbols           0           0           0
287       Auxiliary Symbols            628          39         156
288       Local Strings                784          80          80
289       External Strings             864         144         144
290       File Tables                 1008           2         144
291       Relative Files                 0           0           0
292       External Symbols            1152          20         320
293
294   File #0, "hello2.c"
295
296       Name index  = 1          Readin      = No
297       Merge       = No         Endian      = LITTLE
298       Debug level = G2         Language    = C
299       Adr         = 0x00000000
300
301       Info                       Start      Number        Size      Offset
302       ====                       =====      ======        ====      ======
303       Local strings                  0          15          15         784
304       Local symbols                  0           6          72         436
305       Line numbers                   0          13          13         380
306       Optimization symbols           0           0           0           0
307       Procedures                     0           1          52         384
308       Auxiliary symbols              0          14          56         628
309       Relative Files                 0           0           0           0
310
311    There are 6 local symbols, starting at 436
312
313	Symbol# 0: "hello2.c"
314	    End+1 symbol  = 6
315	    String index  = 1
316	    Storage class = Text        Index  = 6
317	    Symbol type   = File        Value  = 0
318
319	Symbol# 1: "main"
320	    End+1 symbol  = 5
321	    Type          = int
322	    String index  = 10
323	    Storage class = Text        Index  = 12
324	    Symbol type   = Proc        Value  = 0
325
326	Symbol# 2: ""
327	    End+1 symbol  = 4
328	    String index  = 0
329	    Storage class = Text        Index  = 4
330	    Symbol type   = Block       Value  = 8
331
332	Symbol# 3: ""
333	    First symbol  = 2
334	    String index  = 0
335	    Storage class = Text        Index  = 2
336	    Symbol type   = End         Value  = 28
337
338	Symbol# 4: "main"
339	    First symbol  = 1
340	    String index  = 10
341	    Storage class = Text        Index  = 1
342	    Symbol type   = End         Value  = 52
343
344	Symbol# 5: "hello2.c"
345	    First symbol  = 0
346	    String index  = 1
347	    Storage class = Text        Index  = 0
348	    Symbol type   = End         Value  = 0
349
350    There are 14 auxiliary table entries, starting at 628.
351
352	* #0               0, [   0/      0], [ 0 0:0 0:0:0:0:0:0]
353	* #1              24, [  24/      0], [ 6 0:0 0:0:0:0:0:0]
354	* #2               8, [   8/      0], [ 2 0:0 0:0:0:0:0:0]
355	* #3              16, [  16/      0], [ 4 0:0 0:0:0:0:0:0]
356	* #4              24, [  24/      0], [ 6 0:0 0:0:0:0:0:0]
357	* #5              32, [  32/      0], [ 8 0:0 0:0:0:0:0:0]
358	* #6              40, [  40/      0], [10 0:0 0:0:0:0:0:0]
359	* #7              44, [  44/      0], [11 0:0 0:0:0:0:0:0]
360	* #8              12, [  12/      0], [ 3 0:0 0:0:0:0:0:0]
361	* #9              20, [  20/      0], [ 5 0:0 0:0:0:0:0:0]
362	* #10             28, [  28/      0], [ 7 0:0 0:0:0:0:0:0]
363	* #11             36, [  36/      0], [ 9 0:0 0:0:0:0:0:0]
364	  #12              5, [   5/      0], [ 1 1:0 0:0:0:0:0:0]
365	  #13             24, [  24/      0], [ 6 0:0 0:0:0:0:0:0]
366
367    There are 1 procedure descriptor entries, starting at 0.
368
369	Procedure descriptor 0:
370	    Name index   = 10          Name          = "main"
371	    .mask 0x80000000,-4        .fmask 0x00000000,0
372	    .frame $29,24,$31
373	    Opt. start   = -1          Symbols start = 1
374	    First line # = 3           Last line #   = 6
375	    Line Offset  = 0           Address       = 0x00000000
376
377	There are 4 bytes holding line numbers, starting at 380.
378	    Line           3,   delta     0,   count  2
379	    Line           4,   delta     1,   count  3
380	    Line           5,   delta     1,   count  2
381	    Line           6,   delta     1,   count  6
382
383   File #1, "/usr/include/stdio.h"
384
385    Name index  = 1          Readin      = No
386    Merge       = Yes        Endian      = LITTLE
387    Debug level = G2         Language    = C
388    Adr         = 0x00000000
389
390    Info                       Start      Number        Size      Offset
391    ====                       =====      ======        ====      ======
392    Local strings                 15          65          65         799
393    Local symbols                  6          10         120         508
394    Line numbers                   0           0           0         380
395    Optimization symbols           0           0           0           0
396    Procedures                     1           0           0         436
397    Auxiliary symbols             14          25         100         684
398    Relative Files                 0           0           0           0
399
400    There are 10 local symbols, starting at 442
401
402	Symbol# 0: "/usr/include/stdio.h"
403	    End+1 symbol  = 10
404	    String index  = 1
405	    Storage class = Text        Index  = 10
406	    Symbol type   = File        Value  = 0
407
408	Symbol# 1: "_iobuf"
409	    End+1 symbol  = 9
410	    String index  = 22
411	    Storage class = Info        Index  = 9
412	    Symbol type   = Block       Value  = 20
413
414	Symbol# 2: "_cnt"
415	    Type          = int
416	    String index  = 29
417	    Storage class = Info        Index  = 4
418	    Symbol type   = Member      Value  = 0
419
420	Symbol# 3: "_ptr"
421	    Type          = ptr to char
422	    String index  = 34
423	    Storage class = Info        Index  = 15
424	    Symbol type   = Member      Value  = 32
425
426	Symbol# 4: "_base"
427	    Type          = ptr to char
428	    String index  = 39
429	    Storage class = Info        Index  = 16
430	    Symbol type   = Member      Value  = 64
431
432	Symbol# 5: "_bufsiz"
433	    Type          = int
434	    String index  = 45
435	    Storage class = Info        Index  = 4
436	    Symbol type   = Member      Value  = 96
437
438	Symbol# 6: "_flag"
439	    Type          = short
440	    String index  = 53
441	    Storage class = Info        Index  = 3
442	    Symbol type   = Member      Value  = 128
443
444	Symbol# 7: "_file"
445	    Type          = char
446	    String index  = 59
447	    Storage class = Info        Index  = 2
448	    Symbol type   = Member      Value  = 144
449
450	Symbol# 8: ""
451	    First symbol  = 1
452	    String index  = 0
453	    Storage class = Info        Index  = 1
454	    Symbol type   = End         Value  = 0
455
456	Symbol# 9: "/usr/include/stdio.h"
457	    First symbol  = 0
458	    String index  = 1
459	    Storage class = Text        Index  = 0
460	    Symbol type   = End         Value  = 0
461
462    There are 25 auxiliary table entries, starting at 642.
463
464	* #14             -1, [4095/1048575], [63 1:1 f:f:f:f:f:f]
465	  #15          65544, [   8/     16], [ 2 0:0 1:0:0:0:0:0]
466	  #16          65544, [   8/     16], [ 2 0:0 1:0:0:0:0:0]
467	* #17         196656, [  48/     48], [12 0:0 3:0:0:0:0:0]
468	* #18           8191, [4095/      1], [63 1:1 0:0:0:0:f:1]
469	* #19              1, [   1/      0], [ 0 1:0 0:0:0:0:0:0]
470	* #20          20479, [4095/      4], [63 1:1 0:0:0:0:f:4]
471	* #21              1, [   1/      0], [ 0 1:0 0:0:0:0:0:0]
472	* #22              0, [   0/      0], [ 0 0:0 0:0:0:0:0:0]
473	* #23              2, [   2/      0], [ 0 0:1 0:0:0:0:0:0]
474	* #24            160, [ 160/      0], [40 0:0 0:0:0:0:0:0]
475	* #25              0, [   0/      0], [ 0 0:0 0:0:0:0:0:0]
476	* #26              0, [   0/      0], [ 0 0:0 0:0:0:0:0:0]
477	* #27              0, [   0/      0], [ 0 0:0 0:0:0:0:0:0]
478	* #28              0, [   0/      0], [ 0 0:0 0:0:0:0:0:0]
479	* #29              0, [   0/      0], [ 0 0:0 0:0:0:0:0:0]
480	* #30              0, [   0/      0], [ 0 0:0 0:0:0:0:0:0]
481	* #31              0, [   0/      0], [ 0 0:0 0:0:0:0:0:0]
482	* #32              0, [   0/      0], [ 0 0:0 0:0:0:0:0:0]
483	* #33              0, [   0/      0], [ 0 0:0 0:0:0:0:0:0]
484	* #34              0, [   0/      0], [ 0 0:0 0:0:0:0:0:0]
485	* #35              0, [   0/      0], [ 0 0:0 0:0:0:0:0:0]
486	* #36              0, [   0/      0], [ 0 0:0 0:0:0:0:0:0]
487	* #37              0, [   0/      0], [ 0 0:0 0:0:0:0:0:0]
488	* #38              0, [   0/      0], [ 0 0:0 0:0:0:0:0:0]
489
490    There are 0 procedure descriptor entries, starting at 1.
491
492   There are 20 external symbols, starting at 1152
493
494	Symbol# 0: "_iob"
495	    Type          = array [3 {160}] of struct _iobuf { ifd = 1, index = 1 }
496	    String index  = 0           Ifd    = 1
497	    Storage class = Nil         Index  = 17
498	    Symbol type   = Global      Value  = 60
499
500	Symbol# 1: "fopen"
501	    String index  = 5           Ifd    = 1
502	    Storage class = Nil         Index  = 1048575
503	    Symbol type   = Proc        Value  = 0
504
505	Symbol# 2: "fdopen"
506	    String index  = 11          Ifd    = 1
507	    Storage class = Nil         Index  = 1048575
508	    Symbol type   = Proc        Value  = 0
509
510	Symbol# 3: "freopen"
511	    String index  = 18          Ifd    = 1
512	    Storage class = Nil         Index  = 1048575
513	    Symbol type   = Proc        Value  = 0
514
515	Symbol# 4: "popen"
516	    String index  = 26          Ifd    = 1
517	    Storage class = Nil         Index  = 1048575
518	    Symbol type   = Proc        Value  = 0
519
520	Symbol# 5: "tmpfile"
521	    String index  = 32          Ifd    = 1
522	    Storage class = Nil         Index  = 1048575
523	    Symbol type   = Proc        Value  = 0
524
525	Symbol# 6: "ftell"
526	    String index  = 40          Ifd    = 1
527	    Storage class = Nil         Index  = 1048575
528	    Symbol type   = Proc        Value  = 0
529
530	Symbol# 7: "rewind"
531	    String index  = 46          Ifd    = 1
532	    Storage class = Nil         Index  = 1048575
533	    Symbol type   = Proc        Value  = 0
534
535	Symbol# 8: "setbuf"
536	    String index  = 53          Ifd    = 1
537	    Storage class = Nil         Index  = 1048575
538	    Symbol type   = Proc        Value  = 0
539
540	Symbol# 9: "setbuffer"
541	    String index  = 60          Ifd    = 1
542	    Storage class = Nil         Index  = 1048575
543	    Symbol type   = Proc        Value  = 0
544
545	Symbol# 10: "setlinebuf"
546	    String index  = 70          Ifd    = 1
547	    Storage class = Nil         Index  = 1048575
548	    Symbol type   = Proc        Value  = 0
549
550	Symbol# 11: "fgets"
551	    String index  = 81          Ifd    = 1
552	    Storage class = Nil         Index  = 1048575
553	    Symbol type   = Proc        Value  = 0
554
555	Symbol# 12: "gets"
556	    String index  = 87          Ifd    = 1
557	    Storage class = Nil         Index  = 1048575
558	    Symbol type   = Proc        Value  = 0
559
560	Symbol# 13: "ctermid"
561	    String index  = 92          Ifd    = 1
562	    Storage class = Nil         Index  = 1048575
563	    Symbol type   = Proc        Value  = 0
564
565	Symbol# 14: "cuserid"
566	    String index  = 100         Ifd    = 1
567	    Storage class = Nil         Index  = 1048575
568	    Symbol type   = Proc        Value  = 0
569
570	Symbol# 15: "tempnam"
571	    String index  = 108         Ifd    = 1
572	    Storage class = Nil         Index  = 1048575
573	    Symbol type   = Proc        Value  = 0
574
575	Symbol# 16: "tmpnam"
576	    String index  = 116         Ifd    = 1
577	    Storage class = Nil         Index  = 1048575
578	    Symbol type   = Proc        Value  = 0
579
580	Symbol# 17: "sprintf"
581	    String index  = 123         Ifd    = 1
582	    Storage class = Nil         Index  = 1048575
583	    Symbol type   = Proc        Value  = 0
584
585	Symbol# 18: "main"
586	    Type          = int
587	    String index  = 131         Ifd    = 0
588	    Storage class = Text        Index  = 1
589	    Symbol type   = Proc        Value  = 0
590
591	Symbol# 19: "printf"
592	    String index  = 136         Ifd    = 0
593	    Storage class = Undefined   Index  = 1048575
594	    Symbol type   = Proc        Value  = 0
595
596   The following auxiliary table entries were unused:
597
598    #0               0  0x00000000  void
599    #2               8  0x00000008  char
600    #3              16  0x00000010  short
601    #4              24  0x00000018  int
602    #5              32  0x00000020  long
603    #6              40  0x00000028  float
604    #7              44  0x0000002c  double
605    #8              12  0x0000000c  unsigned char
606    #9              20  0x00000014  unsigned short
607    #10             28  0x0000001c  unsigned int
608    #11             36  0x00000024  unsigned long
609    #14              0  0x00000000  void
610    #15             24  0x00000018  int
611    #19             32  0x00000020  long
612    #20             40  0x00000028  float
613    #21             44  0x0000002c  double
614    #22             12  0x0000000c  unsigned char
615    #23             20  0x00000014  unsigned short
616    #24             28  0x0000001c  unsigned int
617    #25             36  0x00000024  unsigned long
618    #26             48  0x00000030  struct no name { ifd = -1, index = 1048575 }
619*/
620
621/* Redefinition of storage classes as an enumeration for better
622   debugging.  */
623
624typedef enum sc {
625  sc_Nil	 = scNil,	  /* no storage class */
626  sc_Text	 = scText,	  /* text symbol */
627  sc_Data	 = scData,	  /* initialized data symbol */
628  sc_Bss	 = scBss,	  /* un-initialized data symbol */
629  sc_Register	 = scRegister,	  /* value of symbol is register number */
630  sc_Abs	 = scAbs,	  /* value of symbol is absolute */
631  sc_Undefined	 = scUndefined,	  /* who knows? */
632  sc_CdbLocal	 = scCdbLocal,	  /* variable's value is IN se->va.?? */
633  sc_Bits	 = scBits,	  /* this is a bit field */
634  sc_CdbSystem	 = scCdbSystem,	  /* value is IN CDB's address space */
635  sc_RegImage	 = scRegImage,	  /* register value saved on stack */
636  sc_Info	 = scInfo,	  /* symbol contains debugger information */
637  sc_UserStruct	 = scUserStruct,  /* addr in struct user for current process */
638  sc_SData	 = scSData,	  /* load time only small data */
639  sc_SBss	 = scSBss,	  /* load time only small common */
640  sc_RData	 = scRData,	  /* load time only read only data */
641  sc_Var	 = scVar,	  /* Var parameter (fortran,pascal) */
642  sc_Common	 = scCommon,	  /* common variable */
643  sc_SCommon	 = scSCommon,	  /* small common */
644  sc_VarRegister = scVarRegister, /* Var parameter in a register */
645  sc_Variant	 = scVariant,	  /* Variant record */
646  sc_SUndefined	 = scSUndefined,  /* small undefined(external) data */
647  sc_Init	 = scInit,	  /* .init section symbol */
648  sc_Max	 = scMax	  /* Max storage class+1 */
649} sc_t;
650
651/* Redefinition of symbol type.  */
652
653typedef enum st {
654  st_Nil	= stNil,	/* Nuthin' special */
655  st_Global	= stGlobal,	/* external symbol */
656  st_Static	= stStatic,	/* static */
657  st_Param	= stParam,	/* procedure argument */
658  st_Local	= stLocal,	/* local variable */
659  st_Label	= stLabel,	/* label */
660  st_Proc	= stProc,	/*     "      "	 Procedure */
661  st_Block	= stBlock,	/* beginning of block */
662  st_End	= stEnd,	/* end (of anything) */
663  st_Member	= stMember,	/* member (of anything	- struct/union/enum */
664  st_Typedef	= stTypedef,	/* type definition */
665  st_File	= stFile,	/* file name */
666  st_RegReloc	= stRegReloc,	/* register relocation */
667  st_Forward	= stForward,	/* forwarding address */
668  st_StaticProc	= stStaticProc,	/* load time only static procs */
669  st_Constant	= stConstant,	/* const */
670  st_Str	= stStr,	/* string */
671  st_Number	= stNumber,	/* pure number (ie. 4 NOR 2+2) */
672  st_Expr	= stExpr,	/* 2+2 vs. 4 */
673  st_Type	= stType,	/* post-coercion SER */
674  st_Max	= stMax		/* max type+1 */
675} st_t;
676
677/* Redefinition of type qualifiers.  */
678
679typedef enum tq {
680  tq_Nil	= tqNil,	/* bt is what you see */
681  tq_Ptr	= tqPtr,	/* pointer */
682  tq_Proc	= tqProc,	/* procedure */
683  tq_Array	= tqArray,	/* duh */
684  tq_Far	= tqFar,	/* longer addressing - 8086/8 land */
685  tq_Vol	= tqVol,	/* volatile */
686  tq_Max	= tqMax		/* Max type qualifier+1 */
687} tq_t;
688
689/* Redefinition of basic types.  */
690
691typedef enum bt {
692  bt_Nil	= btNil,	/* undefined */
693  bt_Adr	= btAdr,	/* address - integer same size as pointer */
694  bt_Char	= btChar,	/* character */
695  bt_UChar	= btUChar,	/* unsigned character */
696  bt_Short	= btShort,	/* short */
697  bt_UShort	= btUShort,	/* unsigned short */
698  bt_Int	= btInt,	/* int */
699  bt_UInt	= btUInt,	/* unsigned int */
700  bt_Long	= btLong,	/* long */
701  bt_ULong	= btULong,	/* unsigned long */
702  bt_Float	= btFloat,	/* float (real) */
703  bt_Double	= btDouble,	/* Double (real) */
704  bt_Struct	= btStruct,	/* Structure (Record) */
705  bt_Union	= btUnion,	/* Union (variant) */
706  bt_Enum	= btEnum,	/* Enumerated */
707  bt_Typedef	= btTypedef,	/* defined via a typedef, isymRef points */
708  bt_Range	= btRange,	/* subrange of int */
709  bt_Set	= btSet,	/* pascal sets */
710  bt_Complex	= btComplex,	/* fortran complex */
711  bt_DComplex	= btDComplex,	/* fortran double complex */
712  bt_Indirect	= btIndirect,	/* forward or unnamed typedef */
713  bt_FixedDec	= btFixedDec,	/* Fixed Decimal */
714  bt_FloatDec	= btFloatDec,	/* Float Decimal */
715  bt_String	= btString,	/* Varying Length Character String */
716  bt_Bit	= btBit,	/* Aligned Bit String */
717  bt_Picture	= btPicture,	/* Picture */
718  bt_Void	= btVoid,	/* Void */
719  bt_Max	= btMax		/* Max basic type+1 */
720} bt_t;
721
722#define N_TQ itqMax
723
724/* States for whether to hash type or not.  */
725typedef enum hash_state {
726  hash_no	= 0,		/* Don't hash type */
727  hash_yes	= 1,		/* OK to hash type, or use previous hash */
728  hash_record	= 2		/* OK to record hash, but don't use prev.  */
729} hash_state_t;
730
731/* Types of different sized allocation requests.  */
732enum alloc_type {
733  alloc_type_none,		/* dummy value */
734  alloc_type_scope,		/* nested scopes linked list */
735  alloc_type_vlinks,		/* glue linking pages in varray */
736  alloc_type_shash,		/* string hash element */
737  alloc_type_thash,		/* type hash element */
738  alloc_type_tag,		/* struct/union/tag element */
739  alloc_type_forward,		/* element to hold unknown tag */
740  alloc_type_thead,		/* head of type hash list */
741  alloc_type_varray,		/* general varray allocation */
742  alloc_type_lineno,		/* line number list */
743  alloc_type_last		/* last+1 element for array bounds */
744};
745
746/* Types of auxiliary type information.  */
747enum aux_type {
748  aux_tir,			/* TIR type information */
749  aux_rndx,			/* relative index into symbol table */
750  aux_dnLow,			/* low dimension */
751  aux_dnHigh,			/* high dimension */
752  aux_isym,			/* symbol table index (end of proc) */
753  aux_iss,			/* index into string space (not used) */
754  aux_width,			/* width for non-default sized struc fields */
755  aux_count			/* count of ranges for variant arm */
756};
757
758/* Structures to provide n-number of virtual arrays, each of which can
759   grow linearly, and which are written in the object file as
760   sequential pages.  On systems with a BSD malloc, the
761   MAX_CLUSTER_PAGES should be 1 less than a power of two, since
762   malloc adds it's overhead, and rounds up to the next power of 2.
763   Pages are linked together via a linked list.
764
765   If PAGE_SIZE is > 4096, the string length in the shash_t structure
766   can't be represented (assuming there are strings > 4096 bytes).  */
767
768/* FIXME: Yes, there can be such strings while emitting C++ class debug
769   info.  Templates are the offender here, the test case in question
770   having a mangled class name of
771
772     t7rb_tree4Z4xkeyZt4pair2ZC4xkeyZt7xsocket1Z4UserZt9select1st2Zt4pair\
773     2ZC4xkeyZt7xsocket1Z4UserZ4xkeyZt4less1Z4xkey
774
775   Repeat that a couple dozen times while listing the class members and
776   you've got strings over 4k.  Hack around this for now by increasing
777   the page size.  A proper solution would abandon this structure scheme
778   certainly for very large strings, and possibly entirely.  */
779
780#ifndef PAGE_SIZE
781#define PAGE_SIZE (8*1024)	/* size of varray pages */
782#endif
783
784#define PAGE_USIZE ((unsigned long) PAGE_SIZE)
785
786#ifndef MAX_CLUSTER_PAGES	/* # pages to get from system */
787#define MAX_CLUSTER_PAGES 63
788#endif
789
790/* Linked list connecting separate page allocations.  */
791typedef struct vlinks {
792  struct vlinks	*prev;		/* previous set of pages */
793  struct vlinks *next;		/* next set of pages */
794  union  page   *datum;		/* start of page */
795  unsigned long	 start_index;	/* starting index # of page */
796} vlinks_t;
797
798/* Virtual array header.  */
799typedef struct varray {
800  vlinks_t	*first;			/* first page link */
801  vlinks_t	*last;			/* last page link */
802  unsigned long	 num_allocated;		/* # objects allocated */
803  unsigned short object_size;		/* size in bytes of each object */
804  unsigned short objects_per_page;	/* # objects that can fit on a page */
805  unsigned short objects_last_page;	/* # objects allocated on last page */
806} varray_t;
807
808#ifndef MALLOC_CHECK
809#define OBJECTS_PER_PAGE(type) (PAGE_SIZE / sizeof (type))
810#else
811#define OBJECTS_PER_PAGE(type) ((sizeof (type) > 1) ? 1 : PAGE_SIZE)
812#endif
813
814#define INIT_VARRAY(type) {	/* macro to initialize a varray */	\
815  (vlinks_t *)0,		/* first */				\
816  (vlinks_t *)0,		/* last */				\
817  0,				/* num_allocated */			\
818  sizeof (type),		/* object_size */			\
819  OBJECTS_PER_PAGE (type),	/* objects_per_page */			\
820  OBJECTS_PER_PAGE (type),	/* objects_last_page */			\
821}
822
823/* Master type for indexes within the symbol table.  */
824typedef unsigned long symint_t;
825
826/* Linked list support for nested scopes (file, block, structure, etc.).  */
827typedef struct scope {
828  struct scope	*prev;		/* previous scope level */
829  struct scope	*free;		/* free list pointer */
830  struct localsym *lsym;	/* pointer to local symbol node */
831  st_t		 type;		/* type of the node */
832} scope_t;
833
834/* For a local symbol we store a gas symbol as well as the debugging
835   information we generate.  The gas symbol will be NULL if this is
836   only a debugging symbol.  */
837typedef struct localsym {
838  const char *name;		/* symbol name */
839  symbolS *as_sym;		/* symbol as seen by gas */
840  bfd_vma addend;		/* addend to as_sym value */
841  struct efdr *file_ptr;	/* file pointer */
842  struct ecoff_proc *proc_ptr;	/* proc pointer */
843  struct localsym *begin_ptr;	/* symbol at start of block */
844  struct ecoff_aux *index_ptr;	/* index value to be filled in */
845  struct forward *forward_ref;	/* forward references to this symbol */
846  long sym_index;		/* final symbol index */
847  EXTR ecoff_sym;		/* ECOFF debugging symbol */
848} localsym_t;
849
850/* For aux information we keep the type and the data.  */
851typedef struct ecoff_aux {
852  enum aux_type type;		/* aux type */
853  AUXU data;			/* aux data */
854} aux_t;
855
856/* For a procedure we store the gas symbol as well as the PDR
857   debugging information.  */
858typedef struct ecoff_proc {
859  localsym_t *sym;		/* associated symbol */
860  PDR pdr;			/* ECOFF debugging info */
861} proc_t;
862
863/* Number of proc_t structures allocated.  */
864static unsigned long proc_cnt;
865
866/* Forward reference list for tags referenced, but not yet defined.  */
867typedef struct forward {
868  struct forward *next;		/* next forward reference */
869  struct forward *free;		/* free list pointer */
870  aux_t		 *ifd_ptr;	/* pointer to store file index */
871  aux_t		 *index_ptr;	/* pointer to store symbol index */
872} forward_t;
873
874/* Linked list support for tags.  The first tag in the list is always
875   the current tag for that block.  */
876typedef struct tag {
877  struct tag	 *free;		/* free list pointer */
878  struct shash	 *hash_ptr;	/* pointer to the hash table head */
879  struct tag	 *same_name;	/* tag with same name in outer scope */
880  struct tag	 *same_block;	/* next tag defined in the same block.  */
881  struct forward *forward_ref;	/* list of forward references */
882  bt_t		  basic_type;	/* bt_Struct, bt_Union, or bt_Enum */
883  symint_t	  ifd;		/* file # tag defined in */
884  localsym_t	 *sym;		/* file's local symbols */
885} tag_t;
886
887/* Head of a block's linked list of tags.  */
888typedef struct thead {
889  struct thead	*prev;		/* previous block */
890  struct thead	*free;		/* free list pointer */
891  struct tag	*first_tag;	/* first tag in block defined */
892} thead_t;
893
894/* Union containing pointers to each the small structures which are freed up.  */
895typedef union small_free {
896  scope_t	*f_scope;	/* scope structure */
897  thead_t	*f_thead;	/* tag head structure */
898  tag_t		*f_tag;		/* tag element structure */
899  forward_t	*f_forward;	/* forward tag reference */
900} small_free_t;
901
902/* String hash table entry.  */
903
904typedef struct shash {
905  char		*string;	/* string we are hashing */
906  symint_t	 indx;		/* index within string table */
907  EXTR		*esym_ptr;	/* global symbol pointer */
908  localsym_t	*sym_ptr;	/* local symbol pointer */
909  localsym_t	*end_ptr;	/* symbol pointer to end block */
910  tag_t		*tag_ptr;	/* tag pointer */
911  proc_t	*proc_ptr;	/* procedure descriptor pointer */
912} shash_t;
913
914/* Type hash table support.  The size of the hash table must fit
915   within a page with the other extended file descriptor information.
916   Because unique types which are hashed are fewer in number than
917   strings, we use a smaller hash value.  */
918
919#define HASHBITS 30
920
921#ifndef THASH_SIZE
922#define THASH_SIZE 113
923#endif
924
925typedef struct thash {
926  struct thash	*next;		/* next hash value */
927  AUXU		 type;		/* type we are hashing */
928  symint_t	 indx;		/* index within string table */
929} thash_t;
930
931/* Extended file descriptor that contains all of the support necessary
932   to add things to each file separately.  */
933typedef struct efdr {
934  FDR		 fdr;		/* File header to be written out */
935  FDR		*orig_fdr;	/* original file header */
936  char		*name;		/* filename */
937  int		 fake;		/* whether this is faked .file */
938  symint_t	 void_type;	/* aux. pointer to 'void' type */
939  symint_t	 int_type;	/* aux. pointer to 'int' type */
940  scope_t	*cur_scope;	/* current nested scopes */
941  symint_t	 file_index;	/* current file number */
942  int		 nested_scopes;	/* # nested scopes */
943  varray_t	 strings;	/* local strings */
944  varray_t	 symbols;	/* local symbols */
945  varray_t	 procs;		/* procedures */
946  varray_t	 aux_syms;	/* auxiliary symbols */
947  struct efdr	*next_file;	/* next file descriptor */
948				/* string/type hash tables */
949  struct hash_control *str_hash;	/* string hash table */
950  thash_t	*thash_head[THASH_SIZE];
951} efdr_t;
952
953/* Pre-initialized extended file structure.  */
954static const efdr_t init_file = {
955  {			/* FDR structure */
956    0,			/* adr:		memory address of beginning of file */
957    0,			/* rss:		file name (of source, if known) */
958    0,			/* issBase:	file's string space */
959    0,			/* cbSs:	number of bytes in the ss */
960    0,			/* isymBase:	beginning of symbols */
961    0,			/* csym:	count file's of symbols */
962    0,			/* ilineBase:	file's line symbols */
963    0,			/* cline:	count of file's line symbols */
964    0,			/* ioptBase:	file's optimization entries */
965    0,			/* copt:	count of file's optimization entries */
966    0,			/* ipdFirst:	start of procedures for this file */
967    0,			/* cpd:		count of procedures for this file */
968    0,			/* iauxBase:	file's auxiliary entries */
969    0,			/* caux:	count of file's auxiliary entries */
970    0,			/* rfdBase:	index into the file indirect table */
971    0,			/* crfd:	count file indirect entries */
972    langC,		/* lang:	language for this file */
973    1,			/* fMerge:	whether this file can be merged */
974    0,			/* fReadin:	true if read in (not just created) */
975    TARGET_BYTES_BIG_ENDIAN,  /* fBigendian:	if 1, compiled on big endian machine */
976    GLEVEL_2,		/* glevel:	level this file was compiled with */
977    0,			/* reserved:	reserved for future use */
978    0,			/* cbLineOffset: byte offset from header for this file ln's */
979    0,			/* cbLine:	size of lines for this file */
980  },
981
982  (FDR *)0,		/* orig_fdr:	original file header pointer */
983  (char *)0,		/* name:	pointer to filename */
984  0,			/* fake:	whether this is a faked .file */
985  0,			/* void_type:	ptr to aux node for void type */
986  0,			/* int_type:	ptr to aux node for int type */
987  (scope_t *)0,		/* cur_scope:	current scope being processed */
988  0,			/* file_index:	current file # */
989  0,			/* nested_scopes: # nested scopes */
990  INIT_VARRAY (char),	/* strings:	local string varray */
991  INIT_VARRAY (localsym_t),	/* symbols:	local symbols varray */
992  INIT_VARRAY (proc_t),	/* procs:	procedure varray */
993  INIT_VARRAY (aux_t),	/* aux_syms:	auxiliary symbols varray */
994
995  (struct efdr *)0,	/* next_file:	next file structure */
996
997  (struct hash_control *)0,	/* str_hash:	string hash table */
998  { 0 },		/* thash_head:	type hash table */
999};
1000
1001static efdr_t *first_file;			/* first file descriptor */
1002static efdr_t **last_file_ptr = &first_file;	/* file descriptor tail */
1003
1004/* Line number information is kept in a list until the assembly is
1005   finished.  */
1006typedef struct lineno_list {
1007  struct lineno_list *next;	/* next element in list */
1008  efdr_t *file;			/* file this line is in */
1009  proc_t *proc;			/* procedure this line is in */
1010  fragS *frag;			/* fragment this line number is in */
1011  unsigned long paddr;		/* offset within fragment */
1012  long lineno;			/* actual line number */
1013} lineno_list_t;
1014
1015static lineno_list_t *first_lineno;
1016static lineno_list_t *last_lineno;
1017static lineno_list_t **last_lineno_ptr = &first_lineno;
1018
1019/* Sometimes there will be some .loc statements before a .ent.  We
1020   keep them in this list so that we can fill in the procedure pointer
1021   after we see the .ent.  */
1022static lineno_list_t *noproc_lineno;
1023
1024/* Union of various things that are held in pages.  */
1025typedef union page {
1026  char		byte	[ PAGE_SIZE ];
1027  unsigned char	ubyte	[ PAGE_SIZE ];
1028  efdr_t	file	[ PAGE_SIZE / sizeof (efdr_t)	     ];
1029  FDR		ofile	[ PAGE_SIZE / sizeof (FDR)	     ];
1030  proc_t	proc	[ PAGE_SIZE / sizeof (proc_t)	     ];
1031  localsym_t	sym	[ PAGE_SIZE / sizeof (localsym_t)    ];
1032  aux_t		aux	[ PAGE_SIZE / sizeof (aux_t)	     ];
1033  DNR		dense	[ PAGE_SIZE / sizeof (DNR)	     ];
1034  scope_t	scope	[ PAGE_SIZE / sizeof (scope_t)	     ];
1035  vlinks_t	vlinks	[ PAGE_SIZE / sizeof (vlinks_t)	     ];
1036  shash_t	shash	[ PAGE_SIZE / sizeof (shash_t)	     ];
1037  thash_t	thash	[ PAGE_SIZE / sizeof (thash_t)	     ];
1038  tag_t		tag	[ PAGE_SIZE / sizeof (tag_t)	     ];
1039  forward_t	forward	[ PAGE_SIZE / sizeof (forward_t)     ];
1040  thead_t	thead	[ PAGE_SIZE / sizeof (thead_t)	     ];
1041  lineno_list_t	lineno	[ PAGE_SIZE / sizeof (lineno_list_t) ];
1042} page_type;
1043
1044/* Structure holding allocation information for small sized structures.  */
1045typedef struct alloc_info {
1046  char		*alloc_name;	/* name of this allocation type (must be first) */
1047  page_type	*cur_page;	/* current page being allocated from */
1048  small_free_t	 free_list;	/* current free list if any */
1049  int		 unallocated;	/* number of elements unallocated on page */
1050  int		 total_alloc;	/* total number of allocations */
1051  int		 total_free;	/* total number of frees */
1052  int		 total_pages;	/* total number of pages allocated */
1053} alloc_info_t;
1054
1055/* Type information collected together.  */
1056typedef struct type_info {
1057  bt_t	      basic_type;		/* basic type */
1058  int	      orig_type;		/* original COFF-based type */
1059  int	      num_tq;			/* # type qualifiers */
1060  int	      num_dims;			/* # dimensions */
1061  int	      num_sizes;		/* # sizes */
1062  int	      extra_sizes;		/* # extra sizes not tied with dims */
1063  tag_t *     tag_ptr;			/* tag pointer */
1064  int	      bitfield;			/* symbol is a bitfield */
1065  tq_t	      type_qualifiers[N_TQ];	/* type qualifiers (ptr, func, array)*/
1066  symint_t    dimensions     [N_TQ];	/* dimensions for each array */
1067  symint_t    sizes	     [N_TQ+2];	/* sizes of each array slice + size of
1068					   struct/union/enum + bitfield size */
1069} type_info_t;
1070
1071/* Pre-initialized type_info struct.  */
1072static const type_info_t type_info_init = {
1073  bt_Nil,				/* basic type */
1074  T_NULL,				/* original COFF-based type */
1075  0,					/* # type qualifiers */
1076  0,					/* # dimensions */
1077  0,					/* # sizes */
1078  0,					/* sizes not tied with dims */
1079  NULL,					/* ptr to tag */
1080  0,					/* bitfield */
1081  {					/* type qualifiers */
1082    tq_Nil,
1083    tq_Nil,
1084    tq_Nil,
1085    tq_Nil,
1086    tq_Nil,
1087    tq_Nil,
1088  },
1089  {					/* dimensions */
1090    0,
1091    0,
1092    0,
1093    0,
1094    0,
1095    0
1096  },
1097  {					/* sizes */
1098    0,
1099    0,
1100    0,
1101    0,
1102    0,
1103    0,
1104    0,
1105    0,
1106  },
1107};
1108
1109/* Global hash table for the tags table and global table for file
1110   descriptors.  */
1111
1112static varray_t file_desc = INIT_VARRAY (efdr_t);
1113
1114static struct hash_control *tag_hash;
1115
1116/* Static types for int and void.  Also, remember the last function's
1117   type (which is set up when we encounter the declaration for the
1118   function, and used when the end block for the function is emitted.  */
1119
1120static type_info_t int_type_info;
1121static type_info_t void_type_info;
1122static type_info_t last_func_type_info;
1123static symbolS *last_func_sym_value;
1124
1125/* Convert COFF basic type to ECOFF basic type.  The T_NULL type
1126   really should use bt_Void, but this causes the current ecoff GDB to
1127   issue unsupported type messages, and the Ultrix 4.00 dbx (aka MIPS
1128   2.0) doesn't understand it, even though the compiler generates it.
1129   Maybe this will be fixed in 2.10 or 2.20 of the MIPS compiler
1130   suite, but for now go with what works.
1131
1132   It would make sense for the .type and .scl directives to use the
1133   ECOFF numbers directly, rather than using the COFF numbers and
1134   mapping them.  Unfortunately, this is historically what mips-tfile
1135   expects, and changing gcc now would be a considerable pain (the
1136   native compiler generates debugging information internally, rather
1137   than via the assembler, so it will never use .type or .scl).  */
1138
1139static const bt_t map_coff_types[] = {
1140  bt_Nil,			/* T_NULL */
1141  bt_Nil,			/* T_ARG */
1142  bt_Char,			/* T_CHAR */
1143  bt_Short,			/* T_SHORT */
1144  bt_Int,			/* T_INT */
1145  bt_Long,			/* T_LONG */
1146  bt_Float,			/* T_FLOAT */
1147  bt_Double,			/* T_DOUBLE */
1148  bt_Struct,			/* T_STRUCT */
1149  bt_Union,			/* T_UNION */
1150  bt_Enum,			/* T_ENUM */
1151  bt_Enum,			/* T_MOE */
1152  bt_UChar,			/* T_UCHAR */
1153  bt_UShort,			/* T_USHORT */
1154  bt_UInt,			/* T_UINT */
1155  bt_ULong			/* T_ULONG */
1156};
1157
1158/* Convert COFF storage class to ECOFF storage class.  */
1159static const sc_t map_coff_storage[] = {
1160  sc_Nil,			/*   0: C_NULL */
1161  sc_Abs,			/*   1: C_AUTO	  auto var */
1162  sc_Undefined,			/*   2: C_EXT	  external */
1163  sc_Data,			/*   3: C_STAT	  static */
1164  sc_Register,			/*   4: C_REG	  register */
1165  sc_Undefined,			/*   5: C_EXTDEF  ??? */
1166  sc_Text,			/*   6: C_LABEL	  label */
1167  sc_Text,			/*   7: C_ULABEL  user label */
1168  sc_Info,			/*   8: C_MOS	  member of struct */
1169  sc_Abs,			/*   9: C_ARG	  argument */
1170  sc_Info,			/*  10: C_STRTAG  struct tag */
1171  sc_Info,			/*  11: C_MOU	  member of union */
1172  sc_Info,			/*  12: C_UNTAG   union tag */
1173  sc_Info,			/*  13: C_TPDEF	  typedef */
1174  sc_Data,			/*  14: C_USTATIC ??? */
1175  sc_Info,			/*  15: C_ENTAG	  enum tag */
1176  sc_Info,			/*  16: C_MOE	  member of enum */
1177  sc_Register,			/*  17: C_REGPARM register parameter */
1178  sc_Bits,			/*  18; C_FIELD	  bitfield */
1179  sc_Nil,			/*  19 */
1180  sc_Nil,			/*  20 */
1181  sc_Nil,			/*  21 */
1182  sc_Nil,			/*  22 */
1183  sc_Nil,			/*  23 */
1184  sc_Nil,			/*  24 */
1185  sc_Nil,			/*  25 */
1186  sc_Nil,			/*  26 */
1187  sc_Nil,			/*  27 */
1188  sc_Nil,			/*  28 */
1189  sc_Nil,			/*  29 */
1190  sc_Nil,			/*  30 */
1191  sc_Nil,			/*  31 */
1192  sc_Nil,			/*  32 */
1193  sc_Nil,			/*  33 */
1194  sc_Nil,			/*  34 */
1195  sc_Nil,			/*  35 */
1196  sc_Nil,			/*  36 */
1197  sc_Nil,			/*  37 */
1198  sc_Nil,			/*  38 */
1199  sc_Nil,			/*  39 */
1200  sc_Nil,			/*  40 */
1201  sc_Nil,			/*  41 */
1202  sc_Nil,			/*  42 */
1203  sc_Nil,			/*  43 */
1204  sc_Nil,			/*  44 */
1205  sc_Nil,			/*  45 */
1206  sc_Nil,			/*  46 */
1207  sc_Nil,			/*  47 */
1208  sc_Nil,			/*  48 */
1209  sc_Nil,			/*  49 */
1210  sc_Nil,			/*  50 */
1211  sc_Nil,			/*  51 */
1212  sc_Nil,			/*  52 */
1213  sc_Nil,			/*  53 */
1214  sc_Nil,			/*  54 */
1215  sc_Nil,			/*  55 */
1216  sc_Nil,			/*  56 */
1217  sc_Nil,			/*  57 */
1218  sc_Nil,			/*  58 */
1219  sc_Nil,			/*  59 */
1220  sc_Nil,			/*  60 */
1221  sc_Nil,			/*  61 */
1222  sc_Nil,			/*  62 */
1223  sc_Nil,			/*  63 */
1224  sc_Nil,			/*  64 */
1225  sc_Nil,			/*  65 */
1226  sc_Nil,			/*  66 */
1227  sc_Nil,			/*  67 */
1228  sc_Nil,			/*  68 */
1229  sc_Nil,			/*  69 */
1230  sc_Nil,			/*  70 */
1231  sc_Nil,			/*  71 */
1232  sc_Nil,			/*  72 */
1233  sc_Nil,			/*  73 */
1234  sc_Nil,			/*  74 */
1235  sc_Nil,			/*  75 */
1236  sc_Nil,			/*  76 */
1237  sc_Nil,			/*  77 */
1238  sc_Nil,			/*  78 */
1239  sc_Nil,			/*  79 */
1240  sc_Nil,			/*  80 */
1241  sc_Nil,			/*  81 */
1242  sc_Nil,			/*  82 */
1243  sc_Nil,			/*  83 */
1244  sc_Nil,			/*  84 */
1245  sc_Nil,			/*  85 */
1246  sc_Nil,			/*  86 */
1247  sc_Nil,			/*  87 */
1248  sc_Nil,			/*  88 */
1249  sc_Nil,			/*  89 */
1250  sc_Nil,			/*  90 */
1251  sc_Nil,			/*  91 */
1252  sc_Nil,			/*  92 */
1253  sc_Nil,			/*  93 */
1254  sc_Nil,			/*  94 */
1255  sc_Nil,			/*  95 */
1256  sc_Nil,			/*  96 */
1257  sc_Nil,			/*  97 */
1258  sc_Nil,			/*  98 */
1259  sc_Nil,			/*  99 */
1260  sc_Text,			/* 100: C_BLOCK  block start/end */
1261  sc_Text,			/* 101: C_FCN	 function start/end */
1262  sc_Info,			/* 102: C_EOS	 end of struct/union/enum */
1263  sc_Nil,			/* 103: C_FILE	 file start */
1264  sc_Nil,			/* 104: C_LINE	 line number */
1265  sc_Nil,			/* 105: C_ALIAS	 combined type info */
1266  sc_Nil,			/* 106: C_HIDDEN ??? */
1267};
1268
1269/* Convert COFF storage class to ECOFF symbol type.  */
1270static const st_t map_coff_sym_type[] = {
1271  st_Nil,			/*   0: C_NULL */
1272  st_Local,			/*   1: C_AUTO	  auto var */
1273  st_Global,			/*   2: C_EXT	  external */
1274  st_Static,			/*   3: C_STAT	  static */
1275  st_Local,			/*   4: C_REG	  register */
1276  st_Global,			/*   5: C_EXTDEF  ??? */
1277  st_Label,			/*   6: C_LABEL	  label */
1278  st_Label,			/*   7: C_ULABEL  user label */
1279  st_Member,			/*   8: C_MOS	  member of struct */
1280  st_Param,			/*   9: C_ARG	  argument */
1281  st_Block,			/*  10: C_STRTAG  struct tag */
1282  st_Member,			/*  11: C_MOU	  member of union */
1283  st_Block,			/*  12: C_UNTAG   union tag */
1284  st_Typedef,			/*  13: C_TPDEF	  typedef */
1285  st_Static,			/*  14: C_USTATIC ??? */
1286  st_Block,			/*  15: C_ENTAG	  enum tag */
1287  st_Member,			/*  16: C_MOE	  member of enum */
1288  st_Param,			/*  17: C_REGPARM register parameter */
1289  st_Member,			/*  18; C_FIELD	  bitfield */
1290  st_Nil,			/*  19 */
1291  st_Nil,			/*  20 */
1292  st_Nil,			/*  21 */
1293  st_Nil,			/*  22 */
1294  st_Nil,			/*  23 */
1295  st_Nil,			/*  24 */
1296  st_Nil,			/*  25 */
1297  st_Nil,			/*  26 */
1298  st_Nil,			/*  27 */
1299  st_Nil,			/*  28 */
1300  st_Nil,			/*  29 */
1301  st_Nil,			/*  30 */
1302  st_Nil,			/*  31 */
1303  st_Nil,			/*  32 */
1304  st_Nil,			/*  33 */
1305  st_Nil,			/*  34 */
1306  st_Nil,			/*  35 */
1307  st_Nil,			/*  36 */
1308  st_Nil,			/*  37 */
1309  st_Nil,			/*  38 */
1310  st_Nil,			/*  39 */
1311  st_Nil,			/*  40 */
1312  st_Nil,			/*  41 */
1313  st_Nil,			/*  42 */
1314  st_Nil,			/*  43 */
1315  st_Nil,			/*  44 */
1316  st_Nil,			/*  45 */
1317  st_Nil,			/*  46 */
1318  st_Nil,			/*  47 */
1319  st_Nil,			/*  48 */
1320  st_Nil,			/*  49 */
1321  st_Nil,			/*  50 */
1322  st_Nil,			/*  51 */
1323  st_Nil,			/*  52 */
1324  st_Nil,			/*  53 */
1325  st_Nil,			/*  54 */
1326  st_Nil,			/*  55 */
1327  st_Nil,			/*  56 */
1328  st_Nil,			/*  57 */
1329  st_Nil,			/*  58 */
1330  st_Nil,			/*  59 */
1331  st_Nil,			/*  60 */
1332  st_Nil,			/*  61 */
1333  st_Nil,			/*  62 */
1334  st_Nil,			/*  63 */
1335  st_Nil,			/*  64 */
1336  st_Nil,			/*  65 */
1337  st_Nil,			/*  66 */
1338  st_Nil,			/*  67 */
1339  st_Nil,			/*  68 */
1340  st_Nil,			/*  69 */
1341  st_Nil,			/*  70 */
1342  st_Nil,			/*  71 */
1343  st_Nil,			/*  72 */
1344  st_Nil,			/*  73 */
1345  st_Nil,			/*  74 */
1346  st_Nil,			/*  75 */
1347  st_Nil,			/*  76 */
1348  st_Nil,			/*  77 */
1349  st_Nil,			/*  78 */
1350  st_Nil,			/*  79 */
1351  st_Nil,			/*  80 */
1352  st_Nil,			/*  81 */
1353  st_Nil,			/*  82 */
1354  st_Nil,			/*  83 */
1355  st_Nil,			/*  84 */
1356  st_Nil,			/*  85 */
1357  st_Nil,			/*  86 */
1358  st_Nil,			/*  87 */
1359  st_Nil,			/*  88 */
1360  st_Nil,			/*  89 */
1361  st_Nil,			/*  90 */
1362  st_Nil,			/*  91 */
1363  st_Nil,			/*  92 */
1364  st_Nil,			/*  93 */
1365  st_Nil,			/*  94 */
1366  st_Nil,			/*  95 */
1367  st_Nil,			/*  96 */
1368  st_Nil,			/*  97 */
1369  st_Nil,			/*  98 */
1370  st_Nil,			/*  99 */
1371  st_Block,			/* 100: C_BLOCK  block start/end */
1372  st_Proc,			/* 101: C_FCN	 function start/end */
1373  st_End,			/* 102: C_EOS	 end of struct/union/enum */
1374  st_File,			/* 103: C_FILE	 file start */
1375  st_Nil,			/* 104: C_LINE	 line number */
1376  st_Nil,			/* 105: C_ALIAS	 combined type info */
1377  st_Nil,			/* 106: C_HIDDEN ??? */
1378};
1379
1380/* Keep track of different sized allocation requests.  */
1381static alloc_info_t alloc_counts[(int) alloc_type_last];
1382
1383/* Record whether we have seen any debugging information.  */
1384int ecoff_debugging_seen = 0;
1385
1386/* Various statics.  */
1387static efdr_t  *cur_file_ptr	= (efdr_t *) 0;	/* current file desc. header */
1388static proc_t  *cur_proc_ptr	= (proc_t *) 0;	/* current procedure header */
1389static proc_t  *first_proc_ptr  = (proc_t *) 0; /* first procedure header */
1390static thead_t *top_tag_head	= (thead_t *) 0; /* top level tag head */
1391static thead_t *cur_tag_head	= (thead_t *) 0; /* current tag head */
1392#ifdef ECOFF_DEBUG
1393static int	debug		= 0; 		/* trace functions */
1394#endif
1395static int	stabs_seen	= 0;		/* != 0 if stabs have been seen */
1396
1397static int current_file_idx;
1398static const char *current_stabs_filename;
1399
1400/* Pseudo symbol to use when putting stabs into the symbol table.  */
1401#ifndef STABS_SYMBOL
1402#define STABS_SYMBOL "@stabs"
1403#endif
1404
1405static char stabs_symbol[] = STABS_SYMBOL;
1406
1407/* Prototypes for functions defined in this file.  */
1408
1409static void add_varray_page (varray_t *vp);
1410static symint_t add_string (varray_t *vp,
1411			    struct hash_control *hash_tbl,
1412			    const char *str,
1413			    shash_t **ret_hash);
1414static localsym_t *add_ecoff_symbol (const char *str, st_t type,
1415				     sc_t storage, symbolS *sym,
1416				     bfd_vma addend, symint_t value,
1417				     symint_t indx);
1418static symint_t add_aux_sym_symint (symint_t aux_word);
1419static symint_t add_aux_sym_rndx (int file_index, symint_t sym_index);
1420static symint_t add_aux_sym_tir (type_info_t *t,
1421				 hash_state_t state,
1422				 thash_t **hash_tbl);
1423static tag_t *get_tag (const char *tag, localsym_t *sym, bt_t basic_type);
1424static void add_unknown_tag (tag_t *ptag);
1425static void add_procedure (char *func, int aent);
1426static void add_file (const char *file_name, int indx, int fake);
1427#ifdef ECOFF_DEBUG
1428static char *sc_to_string (sc_t storage_class);
1429static char *st_to_string (st_t symbol_type);
1430#endif
1431static void mark_stabs (int);
1432static char *ecoff_add_bytes (char **buf, char **bufend,
1433			      char *bufptr, unsigned long need);
1434static unsigned long ecoff_padding_adjust
1435  (const struct ecoff_debug_swap *backend, char **buf, char **bufend,
1436   unsigned long offset, char **bufptrptr);
1437static unsigned long ecoff_build_lineno
1438  (const struct ecoff_debug_swap *backend, char **buf, char **bufend,
1439   unsigned long offset, long *linecntptr);
1440static unsigned long ecoff_build_symbols
1441  (const struct ecoff_debug_swap *backend, char **buf, char **bufend,
1442   unsigned long offset);
1443static unsigned long ecoff_build_procs
1444  (const struct ecoff_debug_swap *backend, char **buf, char **bufend,
1445   unsigned long offset);
1446static unsigned long ecoff_build_aux
1447  (const struct ecoff_debug_swap *backend, char **buf, char **bufend,
1448   unsigned long offset);
1449static unsigned long ecoff_build_strings (char **buf, char **bufend,
1450					  unsigned long offset,
1451					  varray_t *vp);
1452static unsigned long ecoff_build_ss
1453  (const struct ecoff_debug_swap *backend, char **buf, char **bufend,
1454   unsigned long offset);
1455static unsigned long ecoff_build_fdr
1456  (const struct ecoff_debug_swap *backend, char **buf, char **bufend,
1457   unsigned long offset);
1458static void ecoff_setup_ext (void);
1459static page_type *allocate_cluster (unsigned long npages);
1460static page_type *allocate_page (void);
1461static scope_t *allocate_scope (void);
1462static void free_scope (scope_t *ptr);
1463static vlinks_t *allocate_vlinks (void);
1464static shash_t *allocate_shash (void);
1465static thash_t *allocate_thash (void);
1466static tag_t *allocate_tag (void);
1467static void free_tag (tag_t *ptr);
1468static forward_t *allocate_forward (void);
1469static thead_t *allocate_thead (void);
1470static void free_thead (thead_t *ptr);
1471static lineno_list_t *allocate_lineno_list (void);
1472
1473/* This function should be called when the assembler starts up.  */
1474
1475void
1476ecoff_read_begin_hook (void)
1477{
1478  tag_hash = hash_new ();
1479  top_tag_head = allocate_thead ();
1480  top_tag_head->first_tag = (tag_t *) NULL;
1481  top_tag_head->free = (thead_t *) NULL;
1482  top_tag_head->prev = cur_tag_head;
1483  cur_tag_head = top_tag_head;
1484}
1485
1486/* This function should be called when a symbol is created.  */
1487
1488void
1489ecoff_symbol_new_hook (symbolS *symbolP)
1490{
1491  OBJ_SYMFIELD_TYPE *obj;
1492
1493  /* Make sure that we have a file pointer, but only if we have seen a
1494     file.  If we haven't seen a file, then this is a probably special
1495     symbol created by md_begin which may required special handling at
1496     some point.  Creating a dummy file with a dummy name is certainly
1497     wrong.  */
1498  if (cur_file_ptr == (efdr_t *) NULL
1499      && seen_at_least_1_file ())
1500    add_file ((const char *) NULL, 0, 1);
1501  obj = symbol_get_obj (symbolP);
1502  obj->ecoff_file = cur_file_ptr;
1503  obj->ecoff_symbol = NULL;
1504  obj->ecoff_extern_size = 0;
1505}
1506
1507void
1508ecoff_symbol_clone_hook (symbolS *newsymP, symbolS *orgsymP)
1509{
1510  OBJ_SYMFIELD_TYPE *n, *o;
1511
1512  n = symbol_get_obj (newsymP);
1513  o = symbol_get_obj (orgsymP);
1514  memcpy (n, o, sizeof *n);
1515}
1516
1517/* Add a page to a varray object.  */
1518
1519static void
1520add_varray_page (varray_t *vp /* varray to add page to */)
1521{
1522  vlinks_t *new_links = allocate_vlinks ();
1523
1524#ifdef MALLOC_CHECK
1525  if (vp->object_size > 1)
1526    new_links->datum = (page_type *) xcalloc (1, vp->object_size);
1527  else
1528#endif
1529    new_links->datum = allocate_page ();
1530
1531  alloc_counts[(int) alloc_type_varray].total_alloc++;
1532  alloc_counts[(int) alloc_type_varray].total_pages++;
1533
1534  new_links->start_index = vp->num_allocated;
1535  vp->objects_last_page = 0;
1536
1537  if (vp->first == (vlinks_t *) NULL)		/* first allocation? */
1538    vp->first = vp->last = new_links;
1539  else
1540    {						/* 2nd or greater allocation */
1541      new_links->prev = vp->last;
1542      vp->last->next = new_links;
1543      vp->last = new_links;
1544    }
1545}
1546
1547/* Add a string (and null pad) to one of the string tables.  */
1548
1549static symint_t
1550add_string (varray_t *vp,			/* string obstack */
1551	    struct hash_control *hash_tbl,	/* ptr to hash table */
1552	    const char *str,			/* string */
1553	    shash_t **ret_hash			/* return hash pointer */)
1554{
1555  unsigned long len = strlen (str);
1556  shash_t *hash_ptr;
1557
1558  if (len >= PAGE_USIZE)
1559    as_fatal (_("string too big (%lu bytes)"), len);
1560
1561  hash_ptr = (shash_t *) hash_find (hash_tbl, str);
1562  if (hash_ptr == (shash_t *) NULL)
1563    {
1564      const char *err;
1565
1566      if (vp->objects_last_page + len >= PAGE_USIZE)
1567	{
1568	  vp->num_allocated =
1569	    ((vp->num_allocated + PAGE_USIZE - 1) / PAGE_USIZE) * PAGE_USIZE;
1570	  add_varray_page (vp);
1571	}
1572
1573      hash_ptr = allocate_shash ();
1574      hash_ptr->indx = vp->num_allocated;
1575
1576      hash_ptr->string = &vp->last->datum->byte[vp->objects_last_page];
1577
1578      vp->objects_last_page += len + 1;
1579      vp->num_allocated += len + 1;
1580
1581      strcpy (hash_ptr->string, str);
1582
1583      err = hash_insert (hash_tbl, str, (char *) hash_ptr);
1584      if (err)
1585	as_fatal (_("inserting \"%s\" into string hash table: %s"),
1586		  str, err);
1587    }
1588
1589  if (ret_hash != (shash_t **) NULL)
1590    *ret_hash = hash_ptr;
1591
1592  return hash_ptr->indx;
1593}
1594
1595/* Add debugging information for a symbol.  */
1596
1597static localsym_t *
1598add_ecoff_symbol (const char *str,	/* symbol name */
1599		  st_t type,		/* symbol type */
1600		  sc_t storage,		/* storage class */
1601		  symbolS *sym_value,	/* associated symbol.  */
1602		  bfd_vma addend,	/* addend to sym_value.  */
1603		  symint_t value,	/* value of symbol */
1604		  symint_t indx		/* index to local/aux. syms */)
1605{
1606  localsym_t *psym;
1607  scope_t *pscope;
1608  thead_t *ptag_head;
1609  tag_t *ptag;
1610  tag_t *ptag_next;
1611  varray_t *vp;
1612  int scope_delta = 0;
1613  shash_t *hash_ptr = (shash_t *) NULL;
1614
1615  if (cur_file_ptr == (efdr_t *) NULL)
1616    as_fatal (_("no current file pointer"));
1617
1618  vp = &cur_file_ptr->symbols;
1619
1620  if (vp->objects_last_page == vp->objects_per_page)
1621    add_varray_page (vp);
1622
1623  psym = &vp->last->datum->sym[vp->objects_last_page++];
1624
1625  if (str == (const char *) NULL && sym_value != (symbolS *) NULL)
1626    psym->name = S_GET_NAME (sym_value);
1627  else
1628    psym->name = str;
1629  psym->as_sym = sym_value;
1630  if (sym_value != (symbolS *) NULL)
1631    symbol_get_obj (sym_value)->ecoff_symbol = psym;
1632  psym->addend = addend;
1633  psym->file_ptr = cur_file_ptr;
1634  psym->proc_ptr = cur_proc_ptr;
1635  psym->begin_ptr = (localsym_t *) NULL;
1636  psym->index_ptr = (aux_t *) NULL;
1637  psym->forward_ref = (forward_t *) NULL;
1638  psym->sym_index = -1;
1639  memset (&psym->ecoff_sym, 0, sizeof (EXTR));
1640  psym->ecoff_sym.asym.value = value;
1641  psym->ecoff_sym.asym.st = (unsigned) type;
1642  psym->ecoff_sym.asym.sc = (unsigned) storage;
1643  psym->ecoff_sym.asym.index = indx;
1644
1645  /* If there is an associated symbol, we wait until the end of the
1646     assembly before deciding where to put the name (it may be just an
1647     external symbol).  Otherwise, this is just a debugging symbol and
1648     the name should go with the current file.  */
1649  if (sym_value == (symbolS *) NULL)
1650    psym->ecoff_sym.asym.iss = ((str == (const char *) NULL)
1651				? 0
1652				: add_string (&cur_file_ptr->strings,
1653					      cur_file_ptr->str_hash,
1654					      str,
1655					      &hash_ptr));
1656
1657  ++vp->num_allocated;
1658
1659  if (ECOFF_IS_STAB (&psym->ecoff_sym.asym))
1660    return psym;
1661
1662  /* Save the symbol within the hash table if this is a static
1663     item, and it has a name.  */
1664  if (hash_ptr != (shash_t *) NULL
1665      && (type == st_Global || type == st_Static || type == st_Label
1666	  || type == st_Proc || type == st_StaticProc))
1667    hash_ptr->sym_ptr = psym;
1668
1669  /* push or pop a scope if appropriate.  */
1670  switch (type)
1671    {
1672    default:
1673      break;
1674
1675    case st_File:			/* beginning of file */
1676    case st_Proc:			/* procedure */
1677    case st_StaticProc:			/* static procedure */
1678    case st_Block:			/* begin scope */
1679      pscope = allocate_scope ();
1680      pscope->prev = cur_file_ptr->cur_scope;
1681      pscope->lsym = psym;
1682      pscope->type = type;
1683      cur_file_ptr->cur_scope = pscope;
1684
1685      if (type != st_File)
1686	scope_delta = 1;
1687
1688      /* For every block type except file, struct, union, or
1689         enumeration blocks, push a level on the tag stack.  We omit
1690         file types, so that tags can span file boundaries.  */
1691      if (type != st_File && storage != sc_Info)
1692	{
1693	  ptag_head = allocate_thead ();
1694	  ptag_head->first_tag = 0;
1695	  ptag_head->prev = cur_tag_head;
1696	  cur_tag_head = ptag_head;
1697	}
1698      break;
1699
1700    case st_End:
1701      pscope = cur_file_ptr->cur_scope;
1702      if (pscope == (scope_t *) NULL)
1703	as_fatal (_("too many st_End's"));
1704      else
1705	{
1706	  st_t begin_type = (st_t) pscope->lsym->ecoff_sym.asym.st;
1707
1708	  psym->begin_ptr = pscope->lsym;
1709
1710	  if (begin_type != st_File)
1711	    scope_delta = -1;
1712
1713	  /* Except for file, structure, union, or enumeration end
1714	     blocks remove all tags created within this scope.  */
1715	  if (begin_type != st_File && storage != sc_Info)
1716	    {
1717	      ptag_head = cur_tag_head;
1718	      cur_tag_head = ptag_head->prev;
1719
1720	      for (ptag = ptag_head->first_tag;
1721		   ptag != (tag_t *) NULL;
1722		   ptag = ptag_next)
1723		{
1724		  if (ptag->forward_ref != (forward_t *) NULL)
1725		    add_unknown_tag (ptag);
1726
1727		  ptag_next = ptag->same_block;
1728		  ptag->hash_ptr->tag_ptr = ptag->same_name;
1729		  free_tag (ptag);
1730		}
1731
1732	      free_thead (ptag_head);
1733	    }
1734
1735	  cur_file_ptr->cur_scope = pscope->prev;
1736
1737	  /* block begin gets next sym #.  This is set when we know
1738	     the symbol index value.  */
1739
1740	  /* Functions push two or more aux words as follows:
1741	     1st word: index+1 of the end symbol (filled in later).
1742	     2nd word: type of the function (plus any aux words needed).
1743	     Also, tie the external pointer back to the function begin symbol.  */
1744	  if (begin_type != st_File && begin_type != st_Block)
1745	    {
1746	      symint_t ty;
1747	      varray_t *svp = &cur_file_ptr->aux_syms;
1748
1749	      pscope->lsym->ecoff_sym.asym.index = add_aux_sym_symint (0);
1750	      pscope->lsym->index_ptr =
1751		&svp->last->datum->aux[svp->objects_last_page - 1];
1752	      ty = add_aux_sym_tir (&last_func_type_info,
1753				    hash_no,
1754				    &cur_file_ptr->thash_head[0]);
1755	      (void) ty;
1756/* This seems to be unnecessary.  I'm not even sure what it is
1757 * intended to do.  It's from mips-tfile.
1758 *	      if (last_func_sym_value != (symbolS *) NULL)
1759 *		{
1760 *		  last_func_sym_value->ifd = cur_file_ptr->file_index;
1761 *		  last_func_sym_value->index = ty;
1762 *		}
1763 */
1764	    }
1765
1766	  free_scope (pscope);
1767	}
1768    }
1769
1770  cur_file_ptr->nested_scopes += scope_delta;
1771
1772#ifdef ECOFF_DEBUG
1773  if (debug && type != st_File
1774      && (debug > 2 || type == st_Block || type == st_End
1775	  || type == st_Proc || type == st_StaticProc))
1776    {
1777      char *sc_str = sc_to_string (storage);
1778      char *st_str = st_to_string (type);
1779      int depth = cur_file_ptr->nested_scopes + (scope_delta < 0);
1780
1781      fprintf (stderr,
1782	       "\tlsym\tv= %10ld, depth= %2d, sc= %-12s",
1783	       value, depth, sc_str);
1784
1785      if (str_start && str_end_p1 - str_start > 0)
1786	fprintf (stderr, " st= %-11s name= %.*s\n",
1787		 st_str, str_end_p1 - str_start, str_start);
1788      else
1789	{
1790	  unsigned long len = strlen (st_str);
1791	  fprintf (stderr, " st= %.*s\n", len - 1, st_str);
1792	}
1793    }
1794#endif
1795
1796  return psym;
1797}
1798
1799/* Add an auxiliary symbol (passing a symint).  This is actually used
1800   for integral aux types, not just symints.  */
1801
1802static symint_t
1803add_aux_sym_symint (symint_t aux_word /* auxiliary information word */)
1804{
1805  varray_t *vp;
1806  aux_t *aux_ptr;
1807
1808  if (cur_file_ptr == (efdr_t *) NULL)
1809    as_fatal (_("no current file pointer"));
1810
1811  vp = &cur_file_ptr->aux_syms;
1812
1813  if (vp->objects_last_page == vp->objects_per_page)
1814    add_varray_page (vp);
1815
1816  aux_ptr = &vp->last->datum->aux[vp->objects_last_page++];
1817  aux_ptr->type = aux_isym;
1818  aux_ptr->data.isym = aux_word;
1819
1820  return vp->num_allocated++;
1821}
1822
1823/* Add an auxiliary symbol (passing a file/symbol index combo).  */
1824
1825static symint_t
1826add_aux_sym_rndx (int file_index, symint_t sym_index)
1827{
1828  varray_t *vp;
1829  aux_t *aux_ptr;
1830
1831  if (cur_file_ptr == (efdr_t *) NULL)
1832    as_fatal (_("no current file pointer"));
1833
1834  vp = &cur_file_ptr->aux_syms;
1835
1836  if (vp->objects_last_page == vp->objects_per_page)
1837    add_varray_page (vp);
1838
1839  aux_ptr = &vp->last->datum->aux[vp->objects_last_page++];
1840  aux_ptr->type = aux_rndx;
1841  aux_ptr->data.rndx.rfd   = file_index;
1842  aux_ptr->data.rndx.index = sym_index;
1843
1844  return vp->num_allocated++;
1845}
1846
1847/* Add an auxiliary symbol (passing the basic type and possibly
1848   type qualifiers).  */
1849
1850static symint_t
1851add_aux_sym_tir (type_info_t *t,	/* current type information */
1852		 hash_state_t state,	/* whether to hash type or not */
1853		 thash_t **hash_tbl	/* pointer to hash table to use */)
1854{
1855  varray_t *vp;
1856  aux_t *aux_ptr;
1857  static AUXU init_aux;
1858  symint_t ret;
1859  int i;
1860  AUXU aux;
1861
1862  if (cur_file_ptr == (efdr_t *) NULL)
1863    as_fatal (_("no current file pointer"));
1864
1865  vp = &cur_file_ptr->aux_syms;
1866
1867  aux = init_aux;
1868  aux.ti.bt = (int) t->basic_type;
1869  aux.ti.continued = 0;
1870  aux.ti.fBitfield = t->bitfield;
1871
1872  aux.ti.tq0 = (int) t->type_qualifiers[0];
1873  aux.ti.tq1 = (int) t->type_qualifiers[1];
1874  aux.ti.tq2 = (int) t->type_qualifiers[2];
1875  aux.ti.tq3 = (int) t->type_qualifiers[3];
1876  aux.ti.tq4 = (int) t->type_qualifiers[4];
1877  aux.ti.tq5 = (int) t->type_qualifiers[5];
1878
1879  /* For anything that adds additional information, we must not hash,
1880     so check here, and reset our state.  */
1881
1882  if (state != hash_no
1883      && (t->type_qualifiers[0] == tq_Array
1884	  || t->type_qualifiers[1] == tq_Array
1885	  || t->type_qualifiers[2] == tq_Array
1886	  || t->type_qualifiers[3] == tq_Array
1887	  || t->type_qualifiers[4] == tq_Array
1888	  || t->type_qualifiers[5] == tq_Array
1889	  || t->basic_type == bt_Struct
1890	  || t->basic_type == bt_Union
1891	  || t->basic_type == bt_Enum
1892	  || t->bitfield
1893	  || t->num_dims > 0))
1894    state = hash_no;
1895
1896  /* See if we can hash this type, and save some space, but some types
1897     can't be hashed (because they contain arrays or continuations),
1898     and others can be put into the hash list, but cannot use existing
1899     types because other aux entries precede this one.  */
1900
1901  if (state != hash_no)
1902    {
1903      thash_t *hash_ptr;
1904      symint_t hi;
1905
1906      hi = aux.isym & ((1 << HASHBITS) - 1);
1907      hi %= THASH_SIZE;
1908
1909      for (hash_ptr = hash_tbl[hi];
1910	   hash_ptr != (thash_t *)0;
1911	   hash_ptr = hash_ptr->next)
1912	{
1913	  if (aux.isym == hash_ptr->type.isym)
1914	    break;
1915	}
1916
1917      if (hash_ptr != (thash_t *) NULL && state == hash_yes)
1918	return hash_ptr->indx;
1919
1920      if (hash_ptr == (thash_t *) NULL)
1921	{
1922	  hash_ptr = allocate_thash ();
1923	  hash_ptr->next = hash_tbl[hi];
1924	  hash_ptr->type = aux;
1925	  hash_ptr->indx = vp->num_allocated;
1926	  hash_tbl[hi] = hash_ptr;
1927	}
1928    }
1929
1930  /* Everything is set up, add the aux symbol.  */
1931  if (vp->objects_last_page == vp->objects_per_page)
1932    add_varray_page (vp);
1933
1934  aux_ptr = &vp->last->datum->aux[vp->objects_last_page++];
1935  aux_ptr->type = aux_tir;
1936  aux_ptr->data = aux;
1937
1938  ret = vp->num_allocated++;
1939
1940  /* Add bitfield length if it exists.
1941
1942     NOTE:  Mips documentation claims bitfield goes at the end of the
1943     AUX record, but the DECstation compiler emits it here.
1944     (This would only make a difference for enum bitfields.)
1945
1946     Also note:  We use the last size given since gcc may emit 2
1947     for an enum bitfield.  */
1948
1949  if (t->bitfield)
1950    (void) add_aux_sym_symint ((symint_t) t->sizes[t->num_sizes - 1]);
1951
1952  /* Add tag information if needed.  Structure, union, and enum
1953     references add 2 aux symbols: a [file index, symbol index]
1954     pointer to the structure type, and the current file index.  */
1955
1956  if (t->basic_type == bt_Struct
1957      || t->basic_type == bt_Union
1958      || t->basic_type == bt_Enum)
1959    {
1960      symint_t file_index = t->tag_ptr->ifd;
1961      localsym_t *sym = t->tag_ptr->sym;
1962      forward_t *forward_ref = allocate_forward ();
1963
1964      if (sym != (localsym_t *) NULL)
1965	{
1966	  forward_ref->next = sym->forward_ref;
1967	  sym->forward_ref = forward_ref;
1968	}
1969      else
1970	{
1971	  forward_ref->next = t->tag_ptr->forward_ref;
1972	  t->tag_ptr->forward_ref = forward_ref;
1973	}
1974
1975      (void) add_aux_sym_rndx (ST_RFDESCAPE, indexNil);
1976      forward_ref->index_ptr
1977	= &vp->last->datum->aux[vp->objects_last_page - 1];
1978
1979      (void) add_aux_sym_symint (file_index);
1980      forward_ref->ifd_ptr
1981	= &vp->last->datum->aux[vp->objects_last_page - 1];
1982    }
1983
1984  /* Add information about array bounds if they exist.  */
1985  for (i = 0; i < t->num_dims; i++)
1986    {
1987      (void) add_aux_sym_rndx (ST_RFDESCAPE,
1988			       cur_file_ptr->int_type);
1989
1990      (void) add_aux_sym_symint (cur_file_ptr->file_index);	/* file index*/
1991      (void) add_aux_sym_symint ((symint_t) 0);			/* low bound */
1992      (void) add_aux_sym_symint (t->dimensions[i] - 1);		/* high bound*/
1993      (void) add_aux_sym_symint ((t->dimensions[i] == 0)	/* stride */
1994				 ? 0
1995				 : (t->sizes[i] * 8) / t->dimensions[i]);
1996    };
1997
1998  /* NOTE:  Mips documentation claims that the bitfield width goes here.
1999     But it needs to be emitted earlier.  */
2000
2001  return ret;
2002}
2003
2004/* Add a tag to the tag table (unless it already exists).  */
2005
2006static tag_t *
2007get_tag (const char *tag,	/* tag name */
2008	 localsym_t *sym,	/* tag start block */
2009	 bt_t basic_type	/* bt_Struct, bt_Union, or bt_Enum */)
2010{
2011  shash_t *hash_ptr;
2012  const char *err;
2013  tag_t *tag_ptr;
2014
2015  if (cur_file_ptr == (efdr_t *) NULL)
2016    as_fatal (_("no current file pointer"));
2017
2018  hash_ptr = (shash_t *) hash_find (tag_hash, tag);
2019
2020  if (hash_ptr != (shash_t *) NULL
2021      && hash_ptr->tag_ptr != (tag_t *) NULL)
2022    {
2023      tag_ptr = hash_ptr->tag_ptr;
2024      if (sym != (localsym_t *) NULL)
2025	{
2026	  tag_ptr->basic_type = basic_type;
2027	  tag_ptr->ifd        = cur_file_ptr->file_index;
2028	  tag_ptr->sym        = sym;
2029	}
2030      return tag_ptr;
2031    }
2032
2033  if (hash_ptr == (shash_t *) NULL)
2034    {
2035      char *perm;
2036
2037      perm = xstrdup (tag);
2038      hash_ptr = allocate_shash ();
2039      err = hash_insert (tag_hash, perm, (char *) hash_ptr);
2040      if (err)
2041	as_fatal (_("inserting \"%s\" into tag hash table: %s"),
2042		  tag, err);
2043      hash_ptr->string = perm;
2044    }
2045
2046  tag_ptr = allocate_tag ();
2047  tag_ptr->forward_ref	= (forward_t *) NULL;
2048  tag_ptr->hash_ptr	= hash_ptr;
2049  tag_ptr->same_name	= hash_ptr->tag_ptr;
2050  tag_ptr->basic_type	= basic_type;
2051  tag_ptr->sym		= sym;
2052  tag_ptr->ifd		= ((sym == (localsym_t *) NULL)
2053			   ? (symint_t) -1
2054			   : cur_file_ptr->file_index);
2055  tag_ptr->same_block	= cur_tag_head->first_tag;
2056
2057  cur_tag_head->first_tag = tag_ptr;
2058  hash_ptr->tag_ptr	  = tag_ptr;
2059
2060  return tag_ptr;
2061}
2062
2063/* Add an unknown {struct, union, enum} tag.  */
2064
2065static void
2066add_unknown_tag (tag_t *ptag /* pointer to tag information */)
2067{
2068  shash_t *hash_ptr	= ptag->hash_ptr;
2069  char *name		= hash_ptr->string;
2070  localsym_t *sym;
2071  forward_t **pf;
2072
2073#ifdef ECOFF_DEBUG
2074  if (debug > 1)
2075    {
2076      char *agg_type = "{unknown aggregate type}";
2077      switch (ptag->basic_type)
2078	{
2079	case bt_Struct:	agg_type = "struct";	break;
2080	case bt_Union:	agg_type = "union";	break;
2081	case bt_Enum:	agg_type = "enum";	break;
2082	default:				break;
2083	}
2084
2085      fprintf (stderr, "unknown %s %.*s found\n", agg_type,
2086	       hash_ptr->len, name_start);
2087    }
2088#endif
2089
2090  sym = add_ecoff_symbol (name,
2091			  st_Block,
2092			  sc_Info,
2093			  (symbolS *) NULL,
2094			  (bfd_vma) 0,
2095			  (symint_t) 0,
2096			  (symint_t) 0);
2097
2098  (void) add_ecoff_symbol (name,
2099			   st_End,
2100			   sc_Info,
2101			   (symbolS *) NULL,
2102			   (bfd_vma) 0,
2103			   (symint_t) 0,
2104			   (symint_t) 0);
2105
2106  for (pf = &sym->forward_ref; *pf != (forward_t *) NULL; pf = &(*pf)->next)
2107    ;
2108  *pf = ptag->forward_ref;
2109}
2110
2111/* Add a procedure to the current file's list of procedures, and record
2112   this is the current procedure.  If AENT, then only set the requested
2113   symbol's function type.  */
2114
2115static void
2116add_procedure (char *func /* func name */, int aent)
2117{
2118  varray_t *vp;
2119  proc_t *new_proc_ptr;
2120  symbolS *sym;
2121
2122#ifdef ECOFF_DEBUG
2123  if (debug)
2124    fputc ('\n', stderr);
2125#endif
2126
2127  /* Set the BSF_FUNCTION flag for the symbol.  */
2128  sym = symbol_find_or_make (func);
2129  symbol_get_bfdsym (sym)->flags |= BSF_FUNCTION;
2130
2131  if (aent)
2132    return;
2133
2134  if (cur_file_ptr == (efdr_t *) NULL)
2135    as_fatal (_("no current file pointer"));
2136
2137  vp = &cur_file_ptr->procs;
2138
2139  if (vp->objects_last_page == vp->objects_per_page)
2140    add_varray_page (vp);
2141
2142  cur_proc_ptr = new_proc_ptr = &vp->last->datum->proc[vp->objects_last_page++];
2143
2144  if (first_proc_ptr == (proc_t *) NULL)
2145    first_proc_ptr = new_proc_ptr;
2146
2147  vp->num_allocated++;
2148
2149  new_proc_ptr->pdr.isym = -1;
2150  new_proc_ptr->pdr.iline = -1;
2151  new_proc_ptr->pdr.lnLow = -1;
2152  new_proc_ptr->pdr.lnHigh = -1;
2153
2154  /* Push the start of the function.  */
2155  new_proc_ptr->sym = add_ecoff_symbol ((const char *) NULL, st_Proc, sc_Text,
2156					sym, (bfd_vma) 0, (symint_t) 0,
2157					(symint_t) 0);
2158
2159  ++proc_cnt;
2160
2161  /* Fill in the linenos preceding the .ent, if any.  */
2162  if (noproc_lineno != (lineno_list_t *) NULL)
2163    {
2164      lineno_list_t *l;
2165
2166      for (l = noproc_lineno; l != (lineno_list_t *) NULL; l = l->next)
2167	l->proc = new_proc_ptr;
2168      *last_lineno_ptr = noproc_lineno;
2169      while (*last_lineno_ptr != NULL)
2170	{
2171	  last_lineno = *last_lineno_ptr;
2172	  last_lineno_ptr = &last_lineno->next;
2173	}
2174      noproc_lineno = (lineno_list_t *) NULL;
2175    }
2176}
2177
2178symbolS *
2179ecoff_get_cur_proc_sym (void)
2180{
2181  return (cur_proc_ptr ? cur_proc_ptr->sym->as_sym : NULL);
2182}
2183
2184/* Add a new filename, and set up all of the file relative
2185   virtual arrays (strings, symbols, aux syms, etc.).  Record
2186   where the current file structure lives.  */
2187
2188static void
2189add_file (const char *file_name, int indx ATTRIBUTE_UNUSED, int fake)
2190{
2191  int first_ch;
2192  efdr_t *fil_ptr;
2193
2194#ifdef ECOFF_DEBUG
2195  if (debug)
2196    fprintf (stderr, "\tfile\t%.*s\n", len, file_start);
2197#endif
2198
2199  /* If the file name is NULL, then no .file symbol appeared, and we
2200     want to use the actual file name.  */
2201  if (file_name == (const char *) NULL)
2202    {
2203      if (first_file != (efdr_t *) NULL)
2204	as_fatal (_("fake .file after real one"));
2205      file_name = as_where ((unsigned int *) NULL);
2206
2207      /* Automatically generate ECOFF debugging information, since I
2208         think that's what other ECOFF assemblers do.  We don't do
2209         this if we see a .file directive with a string, since that
2210         implies that some sort of debugging information is being
2211         provided.  */
2212      if (! symbol_table_frozen && debug_type == DEBUG_UNSPECIFIED)
2213	debug_type = DEBUG_ECOFF;
2214    }
2215  else if (debug_type == DEBUG_UNSPECIFIED)
2216    debug_type = DEBUG_NONE;
2217
2218#ifndef NO_LISTING
2219  if (listing)
2220    listing_source_file (file_name);
2221#endif
2222
2223  current_stabs_filename = file_name;
2224
2225  /* If we're creating stabs, then we don't actually make a new FDR.
2226     Instead, we just create a stabs symbol.  */
2227  if (stabs_seen)
2228    {
2229      (void) add_ecoff_symbol (file_name, st_Nil, sc_Nil,
2230			       symbol_new (FAKE_LABEL_NAME, now_seg,
2231					   (valueT) frag_now_fix (),
2232					   frag_now),
2233			       (bfd_vma) 0, 0, ECOFF_MARK_STAB (N_SOL));
2234      return;
2235    }
2236
2237  first_ch = *file_name;
2238
2239  /* FIXME: We can't safely merge files which have line number
2240     information (fMerge will be zero in this case).  Otherwise, we
2241     get incorrect line number debugging info.  See for instance
2242     ecoff_build_lineno, which will end up setting all file->fdr.*
2243     fields multiple times, resulting in incorrect debug info.  In
2244     order to make this work right, all line number and symbol info
2245     for the same source file has to be adjacent in the object file,
2246     so that a single file descriptor can be used to point to them.
2247     This would require maintaining file specific lists of line
2248     numbers and symbols for each file, so that they can be merged
2249     together (or output together) when two .file pseudo-ops are
2250     merged into one file descriptor.  */
2251
2252  /* See if the file has already been created.  */
2253  for (fil_ptr = first_file;
2254       fil_ptr != (efdr_t *) NULL;
2255       fil_ptr = fil_ptr->next_file)
2256    {
2257      if (first_ch == fil_ptr->name[0]
2258	  && filename_cmp (file_name, fil_ptr->name) == 0
2259	  && fil_ptr->fdr.fMerge)
2260	{
2261	  cur_file_ptr = fil_ptr;
2262	  if (! fake)
2263	    cur_file_ptr->fake = 0;
2264	  break;
2265	}
2266    }
2267
2268  /* If this is a new file, create it.  */
2269  if (fil_ptr == (efdr_t *) NULL)
2270    {
2271      if (file_desc.objects_last_page == file_desc.objects_per_page)
2272	add_varray_page (&file_desc);
2273
2274      fil_ptr = cur_file_ptr =
2275	&file_desc.last->datum->file[file_desc.objects_last_page++];
2276      *fil_ptr = init_file;
2277
2278      fil_ptr->file_index = current_file_idx++;
2279      ++file_desc.num_allocated;
2280
2281      fil_ptr->fake = fake;
2282
2283      /* Allocate the string hash table.  */
2284      fil_ptr->str_hash = hash_new ();
2285
2286      /* Make sure 0 byte in string table is null  */
2287      add_string (&fil_ptr->strings,
2288		  fil_ptr->str_hash,
2289		  "",
2290		  (shash_t **)0);
2291
2292      if (strlen (file_name) > PAGE_USIZE - 2)
2293	as_fatal (_("filename goes over one page boundary"));
2294
2295      /* Push the start of the filename. We assume that the filename
2296         will be stored at string offset 1.  */
2297      (void) add_ecoff_symbol (file_name, st_File, sc_Text,
2298			       (symbolS *) NULL, (bfd_vma) 0,
2299			       (symint_t) 0, (symint_t) 0);
2300      fil_ptr->fdr.rss = 1;
2301      fil_ptr->name = &fil_ptr->strings.last->datum->byte[1];
2302
2303      /* Update the linked list of file descriptors.  */
2304      *last_file_ptr = fil_ptr;
2305      last_file_ptr = &fil_ptr->next_file;
2306
2307      /* Add void & int types to the file (void should be first to catch
2308         errant 0's within the index fields).  */
2309      fil_ptr->void_type = add_aux_sym_tir (&void_type_info,
2310					    hash_yes,
2311					    &cur_file_ptr->thash_head[0]);
2312
2313      fil_ptr->int_type = add_aux_sym_tir (&int_type_info,
2314					   hash_yes,
2315					   &cur_file_ptr->thash_head[0]);
2316    }
2317}
2318
2319/* This function is called when the assembler notices a preprocessor
2320   directive switching to a new file.  This will not happen in
2321   compiler output, only in hand coded assembler.  */
2322
2323void
2324ecoff_new_file (const char *name, int appfile ATTRIBUTE_UNUSED)
2325{
2326  if (cur_file_ptr != NULL && filename_cmp (cur_file_ptr->name, name) == 0)
2327    return;
2328  add_file (name, 0, 0);
2329
2330  /* This is a hand coded assembler file, so automatically turn on
2331     debugging information.  */
2332  if (debug_type == DEBUG_UNSPECIFIED)
2333    debug_type = DEBUG_ECOFF;
2334}
2335
2336#ifdef ECOFF_DEBUG
2337
2338/* Convert storage class to string.  */
2339
2340static char *
2341sc_to_string (storage_class)
2342     sc_t storage_class;
2343{
2344  switch (storage_class)
2345    {
2346    case sc_Nil:	 return "Nil,";
2347    case sc_Text:	 return "Text,";
2348    case sc_Data:	 return "Data,";
2349    case sc_Bss:	 return "Bss,";
2350    case sc_Register:	 return "Register,";
2351    case sc_Abs:	 return "Abs,";
2352    case sc_Undefined:	 return "Undefined,";
2353    case sc_CdbLocal:	 return "CdbLocal,";
2354    case sc_Bits:	 return "Bits,";
2355    case sc_CdbSystem:	 return "CdbSystem,";
2356    case sc_RegImage:	 return "RegImage,";
2357    case sc_Info:	 return "Info,";
2358    case sc_UserStruct:	 return "UserStruct,";
2359    case sc_SData:	 return "SData,";
2360    case sc_SBss:	 return "SBss,";
2361    case sc_RData:	 return "RData,";
2362    case sc_Var:	 return "Var,";
2363    case sc_Common:	 return "Common,";
2364    case sc_SCommon:	 return "SCommon,";
2365    case sc_VarRegister: return "VarRegister,";
2366    case sc_Variant:	 return "Variant,";
2367    case sc_SUndefined:	 return "SUndefined,";
2368    case sc_Init:	 return "Init,";
2369    case sc_Max:	 return "Max,";
2370    }
2371
2372  return "???,";
2373}
2374
2375#endif /* DEBUG */
2376
2377#ifdef ECOFF_DEBUG
2378
2379/* Convert symbol type to string.  */
2380
2381static char *
2382st_to_string (symbol_type)
2383     st_t symbol_type;
2384{
2385  switch (symbol_type)
2386    {
2387    case st_Nil:	return "Nil,";
2388    case st_Global:	return "Global,";
2389    case st_Static:	return "Static,";
2390    case st_Param:	return "Param,";
2391    case st_Local:	return "Local,";
2392    case st_Label:	return "Label,";
2393    case st_Proc:	return "Proc,";
2394    case st_Block:	return "Block,";
2395    case st_End:	return "End,";
2396    case st_Member:	return "Member,";
2397    case st_Typedef:	return "Typedef,";
2398    case st_File:	return "File,";
2399    case st_RegReloc:	return "RegReloc,";
2400    case st_Forward:	return "Forward,";
2401    case st_StaticProc:	return "StaticProc,";
2402    case st_Constant:	return "Constant,";
2403    case st_Str:	return "String,";
2404    case st_Number:	return "Number,";
2405    case st_Expr:	return "Expr,";
2406    case st_Type:	return "Type,";
2407    case st_Max:	return "Max,";
2408    }
2409
2410  return "???,";
2411}
2412
2413#endif /* DEBUG */
2414
2415/* Parse .begin directives which have a label as the first argument
2416   which gives the location of the start of the block.  */
2417
2418void
2419ecoff_directive_begin (int ignore ATTRIBUTE_UNUSED)
2420{
2421  char *name;
2422  char name_end;
2423
2424  if (cur_file_ptr == (efdr_t *) NULL)
2425    {
2426      as_warn (_(".begin directive without a preceding .file directive"));
2427      demand_empty_rest_of_line ();
2428      return;
2429    }
2430
2431  if (cur_proc_ptr == (proc_t *) NULL)
2432    {
2433      as_warn (_(".begin directive without a preceding .ent directive"));
2434      demand_empty_rest_of_line ();
2435      return;
2436    }
2437
2438  name_end = get_symbol_name (&name);
2439
2440  (void) add_ecoff_symbol ((const char *) NULL, st_Block, sc_Text,
2441			   symbol_find_or_make (name),
2442			   (bfd_vma) 0, (symint_t) 0, (symint_t) 0);
2443
2444  (void) restore_line_pointer (name_end);
2445
2446  /* The line number follows, but we don't use it.  */
2447  (void) get_absolute_expression ();
2448  demand_empty_rest_of_line ();
2449}
2450
2451/* Parse .bend directives which have a label as the first argument
2452   which gives the location of the end of the block.  */
2453
2454void
2455ecoff_directive_bend (int ignore ATTRIBUTE_UNUSED)
2456{
2457  char *name;
2458  char name_end;
2459  symbolS *endsym;
2460
2461  if (cur_file_ptr == (efdr_t *) NULL)
2462    {
2463      as_warn (_(".bend directive without a preceding .file directive"));
2464      demand_empty_rest_of_line ();
2465      return;
2466    }
2467
2468  if (cur_proc_ptr == (proc_t *) NULL)
2469    {
2470      as_warn (_(".bend directive without a preceding .ent directive"));
2471      demand_empty_rest_of_line ();
2472      return;
2473    }
2474
2475  name_end = get_symbol_name (&name);
2476
2477  /* The value is the distance between the .bend directive and the
2478     corresponding symbol.  We fill in the offset when we write out
2479     the symbol.  */
2480  endsym = symbol_find (name);
2481  if (endsym == (symbolS *) NULL)
2482    as_warn (_(".bend directive names unknown symbol"));
2483  else
2484    (void) add_ecoff_symbol ((const char *) NULL, st_End, sc_Text, endsym,
2485			     (bfd_vma) 0, (symint_t) 0, (symint_t) 0);
2486
2487  restore_line_pointer (name_end);
2488
2489  /* The line number follows, but we don't use it.  */
2490  (void) get_absolute_expression ();
2491  demand_empty_rest_of_line ();
2492}
2493
2494/* COFF debugging information is provided as a series of directives
2495   (.def, .scl, etc.).  We build up information as we read the
2496   directives in the following static variables, and file it away when
2497   we reach the .endef directive.  */
2498static char *coff_sym_name;
2499static type_info_t coff_type;
2500static sc_t coff_storage_class;
2501static st_t coff_symbol_typ;
2502static int coff_is_function;
2503static char *coff_tag;
2504static valueT coff_value;
2505static symbolS *coff_sym_value;
2506static bfd_vma coff_sym_addend;
2507static int coff_inside_enumeration;
2508
2509/* Handle a .def directive: start defining a symbol.  */
2510
2511void
2512ecoff_directive_def (int ignore ATTRIBUTE_UNUSED)
2513{
2514  char *name;
2515  char name_end;
2516
2517  ecoff_debugging_seen = 1;
2518
2519  SKIP_WHITESPACE ();
2520
2521  name_end = get_symbol_name (&name);
2522
2523  if (coff_sym_name != (char *) NULL)
2524    as_warn (_(".def pseudo-op used inside of .def/.endef; ignored"));
2525  else if (*name == '\0')
2526    as_warn (_("empty symbol name in .def; ignored"));
2527  else
2528    {
2529      if (coff_sym_name != (char *) NULL)
2530	free (coff_sym_name);
2531      if (coff_tag != (char *) NULL)
2532	free (coff_tag);
2533
2534      coff_sym_name = xstrdup (name);
2535      coff_type = type_info_init;
2536      coff_storage_class = sc_Nil;
2537      coff_symbol_typ = st_Nil;
2538      coff_is_function = 0;
2539      coff_tag = (char *) NULL;
2540      coff_value = 0;
2541      coff_sym_value = (symbolS *) NULL;
2542      coff_sym_addend = 0;
2543    }
2544
2545  restore_line_pointer (name_end);
2546
2547  demand_empty_rest_of_line ();
2548}
2549
2550/* Handle a .dim directive, used to give dimensions for an array.  The
2551   arguments are comma separated numbers.  mips-tfile assumes that
2552   there will not be more than 6 dimensions, and gdb won't read any
2553   more than that anyhow, so I will also make that assumption.  */
2554
2555void
2556ecoff_directive_dim (int ignore ATTRIBUTE_UNUSED)
2557{
2558  int dimens[N_TQ];
2559  int i;
2560
2561  if (coff_sym_name == (char *) NULL)
2562    {
2563      as_warn (_(".dim pseudo-op used outside of .def/.endef; ignored"));
2564      demand_empty_rest_of_line ();
2565      return;
2566    }
2567
2568  for (i = 0; i < N_TQ; i++)
2569    {
2570      SKIP_WHITESPACE ();
2571      dimens[i] = get_absolute_expression ();
2572      if (*input_line_pointer == ',')
2573	++input_line_pointer;
2574      else
2575	{
2576	  if (*input_line_pointer != '\n'
2577	      && *input_line_pointer != ';')
2578	    as_warn (_("badly formed .dim directive"));
2579	  break;
2580	}
2581    }
2582
2583  if (i == N_TQ)
2584    --i;
2585
2586  /* The dimensions are stored away in reverse order.  */
2587  for (; i >= 0; i--)
2588    {
2589      if (coff_type.num_dims >= N_TQ)
2590	{
2591	  as_warn (_("too many .dim entries"));
2592	  break;
2593	}
2594      coff_type.dimensions[coff_type.num_dims] = dimens[i];
2595      ++coff_type.num_dims;
2596    }
2597
2598  demand_empty_rest_of_line ();
2599}
2600
2601/* Handle a .scl directive, which sets the COFF storage class of the
2602   symbol.  */
2603
2604void
2605ecoff_directive_scl (int ignore ATTRIBUTE_UNUSED)
2606{
2607  long val;
2608
2609  if (coff_sym_name == (char *) NULL)
2610    {
2611      as_warn (_(".scl pseudo-op used outside of .def/.endef; ignored"));
2612      demand_empty_rest_of_line ();
2613      return;
2614    }
2615
2616  val = get_absolute_expression ();
2617
2618  coff_symbol_typ = map_coff_sym_type[val];
2619  coff_storage_class = map_coff_storage[val];
2620
2621  demand_empty_rest_of_line ();
2622}
2623
2624/* Handle a .size directive.  For some reason mips-tfile.c thinks that
2625   .size can have multiple arguments.  We humor it, although gcc will
2626   never generate more than one argument.  */
2627
2628void
2629ecoff_directive_size (int ignore ATTRIBUTE_UNUSED)
2630{
2631  int sizes[N_TQ];
2632  int i;
2633
2634  if (coff_sym_name == (char *) NULL)
2635    {
2636      as_warn (_(".size pseudo-op used outside of .def/.endef; ignored"));
2637      demand_empty_rest_of_line ();
2638      return;
2639    }
2640
2641  for (i = 0; i < N_TQ; i++)
2642    {
2643      SKIP_WHITESPACE ();
2644      sizes[i] = get_absolute_expression ();
2645      if (*input_line_pointer == ',')
2646	++input_line_pointer;
2647      else
2648	{
2649	  if (*input_line_pointer != '\n'
2650	      && *input_line_pointer != ';')
2651	    as_warn (_("badly formed .size directive"));
2652	  break;
2653	}
2654    }
2655
2656  if (i == N_TQ)
2657    --i;
2658
2659  /* The sizes are stored away in reverse order.  */
2660  for (; i >= 0; i--)
2661    {
2662      if (coff_type.num_sizes >= N_TQ)
2663	{
2664	  as_warn (_("too many .size entries"));
2665	  break;
2666	}
2667      coff_type.sizes[coff_type.num_sizes] = sizes[i];
2668      ++coff_type.num_sizes;
2669    }
2670
2671  demand_empty_rest_of_line ();
2672}
2673
2674/* Handle the .type directive, which gives the COFF type of the
2675   symbol.  */
2676
2677void
2678ecoff_directive_type (int ignore ATTRIBUTE_UNUSED)
2679{
2680  long val;
2681  tq_t *tq_ptr;
2682  tq_t *tq_shft;
2683
2684  if (coff_sym_name == (char *) NULL)
2685    {
2686      as_warn (_(".type pseudo-op used outside of .def/.endef; ignored"));
2687      demand_empty_rest_of_line ();
2688      return;
2689    }
2690
2691  val = get_absolute_expression ();
2692
2693  coff_type.orig_type = BTYPE (val);
2694  coff_type.basic_type = map_coff_types[coff_type.orig_type];
2695
2696  tq_ptr = &coff_type.type_qualifiers[N_TQ];
2697  while (val & ~N_BTMASK)
2698    {
2699      if (tq_ptr == &coff_type.type_qualifiers[0])
2700	{
2701	  /* FIXME: We could handle this by setting the continued bit.
2702	     There would still be a limit: the .type argument can not
2703	     be infinite.  */
2704	  as_warn (_("the type of %s is too complex; it will be simplified"),
2705		   coff_sym_name);
2706	  break;
2707	}
2708      if (ISPTR (val))
2709	*--tq_ptr = tq_Ptr;
2710      else if (ISFCN (val))
2711	*--tq_ptr = tq_Proc;
2712      else if (ISARY (val))
2713	*--tq_ptr = tq_Array;
2714      else
2715	as_fatal (_("Unrecognized .type argument"));
2716
2717      val = DECREF (val);
2718    }
2719
2720  tq_shft = &coff_type.type_qualifiers[0];
2721  while (tq_ptr != &coff_type.type_qualifiers[N_TQ])
2722    *tq_shft++ = *tq_ptr++;
2723
2724  if (tq_shft != &coff_type.type_qualifiers[0] && tq_shft[-1] == tq_Proc)
2725    {
2726      /* If this is a function, ignore it, so that we don't get two
2727         entries (one from the .ent, and one for the .def that
2728         precedes it).  Save the type information so that the end
2729         block can properly add it after the begin block index.  For
2730         MIPS knows what reason, we must strip off the function type
2731         at this point.  */
2732      coff_is_function = 1;
2733      tq_shft[-1] = tq_Nil;
2734    }
2735
2736  while (tq_shft != &coff_type.type_qualifiers[N_TQ])
2737    *tq_shft++ = tq_Nil;
2738
2739  demand_empty_rest_of_line ();
2740}
2741
2742/* Handle the .tag directive, which gives the name of a structure,
2743   union or enum.  */
2744
2745void
2746ecoff_directive_tag (int ignore ATTRIBUTE_UNUSED)
2747{
2748  char *name;
2749  char name_end;
2750
2751  if (coff_sym_name == (char *) NULL)
2752    {
2753      as_warn (_(".tag pseudo-op used outside of .def/.endef; ignored"));
2754      demand_empty_rest_of_line ();
2755      return;
2756    }
2757
2758  name_end = get_symbol_name (&name);
2759
2760  coff_tag = xstrdup (name);
2761
2762  (void) restore_line_pointer (name_end);
2763
2764  demand_empty_rest_of_line ();
2765}
2766
2767/* Handle the .val directive, which gives the value of the symbol.  It
2768   may be the name of a static or global symbol.  */
2769
2770void
2771ecoff_directive_val (int ignore ATTRIBUTE_UNUSED)
2772{
2773  expressionS exp;
2774
2775  if (coff_sym_name == (char *) NULL)
2776    {
2777      as_warn (_(".val pseudo-op used outside of .def/.endef; ignored"));
2778      demand_empty_rest_of_line ();
2779      return;
2780    }
2781
2782  expression (&exp);
2783  if (exp.X_op != O_constant && exp.X_op != O_symbol)
2784    {
2785      as_bad (_(".val expression is too complex"));
2786      demand_empty_rest_of_line ();
2787      return;
2788    }
2789
2790  if (exp.X_op == O_constant)
2791    coff_value = exp.X_add_number;
2792  else
2793    {
2794      coff_sym_value = exp.X_add_symbol;
2795      coff_sym_addend = exp.X_add_number;
2796    }
2797
2798  demand_empty_rest_of_line ();
2799}
2800
2801/* Handle the .endef directive, which terminates processing of COFF
2802   debugging information for a symbol.  */
2803
2804void
2805ecoff_directive_endef (int ignore ATTRIBUTE_UNUSED)
2806{
2807  char *name;
2808  symint_t indx;
2809  localsym_t *sym;
2810
2811  demand_empty_rest_of_line ();
2812
2813  if (coff_sym_name == (char *) NULL)
2814    {
2815      as_warn (_(".endef pseudo-op used before .def; ignored"));
2816      return;
2817    }
2818
2819  name = coff_sym_name;
2820  coff_sym_name = (char *) NULL;
2821
2822  /* If the symbol is a static or external, we have already gotten the
2823     appropriate type and class, so make sure we don't override those
2824     values.  This is needed because there are some type and classes
2825     that are not in COFF, such as short data, etc.  */
2826  if (coff_sym_value != (symbolS *) NULL)
2827    {
2828      coff_symbol_typ = st_Nil;
2829      coff_storage_class = sc_Nil;
2830    }
2831
2832  coff_type.extra_sizes = coff_tag != (char *) NULL;
2833  if (coff_type.num_dims > 0)
2834    {
2835      int diff = coff_type.num_dims - coff_type.num_sizes;
2836      int i = coff_type.num_dims - 1;
2837      int j;
2838
2839      if (coff_type.num_sizes != 1 || diff < 0)
2840	{
2841	  as_warn (_("bad COFF debugging information"));
2842	  return;
2843	}
2844
2845      /* If this is an array, make sure the same number of dimensions
2846         and sizes were passed, creating extra sizes for multiply
2847         dimensioned arrays if not passed.  */
2848      coff_type.extra_sizes = 0;
2849      if (diff)
2850	{
2851	  j = (sizeof (coff_type.sizes) / sizeof (coff_type.sizes[0])) - 1;
2852	  while (j >= 0)
2853	    {
2854	      coff_type.sizes[j] = (((j - diff) >= 0)
2855				    ? coff_type.sizes[j - diff]
2856				    : 0);
2857	      j--;
2858	    }
2859
2860	  coff_type.num_sizes = i + 1;
2861	  for (i--; i >= 0; i--)
2862	    coff_type.sizes[i] = (coff_type.dimensions[i + 1] == 0
2863				  ? 0
2864				  : (coff_type.sizes[i + 1]
2865				     / coff_type.dimensions[i + 1]));
2866	}
2867    }
2868  else if (coff_symbol_typ == st_Member
2869	   && coff_type.num_sizes - coff_type.extra_sizes == 1)
2870    {
2871      /* Is this a bitfield?  This is indicated by a structure member
2872         having a size field that isn't an array.  */
2873      coff_type.bitfield = 1;
2874    }
2875
2876  /* Except for enumeration members & begin/ending of scopes, put the
2877     type word in the aux. symbol table.  */
2878  if (coff_symbol_typ == st_Block || coff_symbol_typ == st_End)
2879    indx = 0;
2880  else if (coff_inside_enumeration)
2881    indx = cur_file_ptr->void_type;
2882  else
2883    {
2884      if (coff_type.basic_type == bt_Struct
2885	  || coff_type.basic_type == bt_Union
2886	  || coff_type.basic_type == bt_Enum)
2887	{
2888	  if (coff_tag == (char *) NULL)
2889	    {
2890	      as_warn (_("no tag specified for %s"), name);
2891	      return;
2892	    }
2893
2894	  coff_type.tag_ptr = get_tag (coff_tag, (localsym_t *) NULL,
2895				       coff_type.basic_type);
2896	}
2897
2898      if (coff_is_function)
2899	{
2900	  last_func_type_info = coff_type;
2901	  last_func_sym_value = coff_sym_value;
2902	  return;
2903	}
2904
2905      indx = add_aux_sym_tir (&coff_type,
2906			      hash_yes,
2907			      &cur_file_ptr->thash_head[0]);
2908    }
2909
2910  /* Do any last minute adjustments that are necessary.  */
2911  switch (coff_symbol_typ)
2912    {
2913    default:
2914      break;
2915
2916      /* For the beginning of structs, unions, and enumerations, the
2917         size info needs to be passed in the value field.  */
2918    case st_Block:
2919      if (coff_type.num_sizes - coff_type.num_dims - coff_type.extra_sizes
2920	  != 1)
2921	{
2922	  as_warn (_("bad COFF debugging information"));
2923	  return;
2924	}
2925      else
2926	coff_value = coff_type.sizes[0];
2927
2928      coff_inside_enumeration = (coff_type.orig_type == T_ENUM);
2929      break;
2930
2931      /* For the end of structs, unions, and enumerations, omit the
2932         name which is always ".eos".  This needs to be done last, so
2933         that any error reporting above gives the correct name.  */
2934    case st_End:
2935      free (name);
2936      name = (char *) NULL;
2937      coff_value = 0;
2938      coff_inside_enumeration = 0;
2939      break;
2940
2941      /* Members of structures and unions that aren't bitfields, need
2942         to adjust the value from a byte offset to a bit offset.
2943         Members of enumerations do not have the value adjusted, and
2944         can be distinguished by indx == indexNil.  For enumerations,
2945         update the maximum enumeration value.  */
2946    case st_Member:
2947      if (! coff_type.bitfield && ! coff_inside_enumeration)
2948	coff_value *= 8;
2949
2950      break;
2951    }
2952
2953  /* Add the symbol.  */
2954  sym = add_ecoff_symbol (name,
2955			  coff_symbol_typ,
2956			  coff_storage_class,
2957			  coff_sym_value,
2958			  coff_sym_addend,
2959			  (symint_t) coff_value,
2960			  indx);
2961
2962  /* deal with struct, union, and enum tags.  */
2963  if (coff_symbol_typ == st_Block)
2964    {
2965      /* Create or update the tag information.  */
2966      tag_t *tag_ptr = get_tag (name,
2967				sym,
2968				coff_type.basic_type);
2969      forward_t **pf;
2970
2971      /* Remember any forward references.  */
2972      for (pf = &sym->forward_ref;
2973	   *pf != (forward_t *) NULL;
2974	   pf = &(*pf)->next)
2975	;
2976      *pf = tag_ptr->forward_ref;
2977      tag_ptr->forward_ref = (forward_t *) NULL;
2978    }
2979}
2980
2981/* Parse .end directives.  */
2982
2983void
2984ecoff_directive_end (int ignore ATTRIBUTE_UNUSED)
2985{
2986  char *name;
2987  char name_end;
2988  symbolS *ent;
2989
2990  if (cur_file_ptr == (efdr_t *) NULL)
2991    {
2992      as_warn (_(".end directive without a preceding .file directive"));
2993      demand_empty_rest_of_line ();
2994      return;
2995    }
2996
2997  if (cur_proc_ptr == (proc_t *) NULL)
2998    {
2999      as_warn (_(".end directive without a preceding .ent directive"));
3000      demand_empty_rest_of_line ();
3001      return;
3002    }
3003
3004  name_end = get_symbol_name (&name);
3005
3006  if (name == input_line_pointer)
3007    {
3008      as_warn (_(".end directive has no name"));
3009      (void) restore_line_pointer (name_end);
3010      demand_empty_rest_of_line ();
3011      return;
3012    }
3013
3014  /* The value is the distance between the .end directive and the
3015     corresponding symbol.  We create a fake symbol to hold the
3016     current location, and put in the offset when we write out the
3017     symbol.  */
3018  ent = symbol_find (name);
3019  if (ent == (symbolS *) NULL)
3020    as_warn (_(".end directive names unknown symbol"));
3021  else
3022    (void) add_ecoff_symbol ((const char *) NULL, st_End, sc_Text,
3023			     symbol_new (FAKE_LABEL_NAME, now_seg,
3024					 (valueT) frag_now_fix (),
3025					 frag_now),
3026			     (bfd_vma) 0, (symint_t) 0, (symint_t) 0);
3027
3028#ifdef md_flush_pending_output
3029  md_flush_pending_output ();
3030#endif
3031
3032  cur_proc_ptr = (proc_t *) NULL;
3033
3034  (void) restore_line_pointer (name_end);
3035  demand_empty_rest_of_line ();
3036}
3037
3038/* Parse .ent directives.  */
3039
3040void
3041ecoff_directive_ent (int aent)
3042{
3043  char *name;
3044  char name_end;
3045
3046  if (cur_file_ptr == (efdr_t *) NULL)
3047    add_file ((const char *) NULL, 0, 1);
3048
3049  if (!aent && cur_proc_ptr != (proc_t *) NULL)
3050    {
3051      as_warn (_("second .ent directive found before .end directive"));
3052      demand_empty_rest_of_line ();
3053      return;
3054    }
3055
3056  name_end = get_symbol_name (&name);
3057
3058  if (name == input_line_pointer)
3059    {
3060      as_warn (_("%s directive has no name"), aent ? ".aent" : ".ent");
3061      (void) restore_line_pointer (name_end);
3062      demand_empty_rest_of_line ();
3063      return;
3064    }
3065
3066  add_procedure (name, aent);
3067
3068  (void) restore_line_pointer (name_end);
3069
3070  /* The .ent directive is sometimes followed by a number.  I'm not
3071     really sure what the number means.  I don't see any way to store
3072     the information in the PDR.  The Irix 4 assembler seems to ignore
3073     the information.  */
3074  SKIP_WHITESPACE ();
3075  if (*input_line_pointer == ',')
3076    {
3077      ++input_line_pointer;
3078      SKIP_WHITESPACE ();
3079    }
3080  if (ISDIGIT (*input_line_pointer)
3081      || *input_line_pointer == '-')
3082    (void) get_absolute_expression ();
3083
3084  demand_empty_rest_of_line ();
3085}
3086
3087/* Parse .extern directives.  */
3088
3089void
3090ecoff_directive_extern (int ignore ATTRIBUTE_UNUSED)
3091{
3092  char *name;
3093  int c;
3094  symbolS *symbolp;
3095  valueT size;
3096
3097  c = get_symbol_name (&name);
3098  symbolp = symbol_find_or_make (name);
3099  (void) restore_line_pointer (c);
3100
3101  S_SET_EXTERNAL (symbolp);
3102
3103  if (*input_line_pointer == ',')
3104    ++input_line_pointer;
3105  size = get_absolute_expression ();
3106
3107  symbol_get_obj (symbolp)->ecoff_extern_size = size;
3108}
3109
3110/* Parse .file directives.  */
3111
3112void
3113ecoff_directive_file (int ignore ATTRIBUTE_UNUSED)
3114{
3115  int indx;
3116  char *name;
3117  int len;
3118
3119  if (cur_proc_ptr != (proc_t *) NULL)
3120    {
3121      as_warn (_("no way to handle .file within .ent/.end section"));
3122      demand_empty_rest_of_line ();
3123      return;
3124    }
3125
3126  indx = (int) get_absolute_expression ();
3127
3128  /* FIXME: we don't have to save the name here.  */
3129  name = demand_copy_C_string (&len);
3130
3131  add_file (name, indx - 1, 0);
3132
3133  demand_empty_rest_of_line ();
3134}
3135
3136/* Parse .fmask directives.  */
3137
3138void
3139ecoff_directive_fmask (int ignore ATTRIBUTE_UNUSED)
3140{
3141  long val;
3142
3143  if (cur_proc_ptr == (proc_t *) NULL)
3144    {
3145      as_warn (_(".fmask outside of .ent"));
3146      demand_empty_rest_of_line ();
3147      return;
3148    }
3149
3150  if (get_absolute_expression_and_terminator (&val) != ',')
3151    {
3152      as_warn (_("bad .fmask directive"));
3153      --input_line_pointer;
3154      demand_empty_rest_of_line ();
3155      return;
3156    }
3157
3158  cur_proc_ptr->pdr.fregmask = val;
3159  cur_proc_ptr->pdr.fregoffset = get_absolute_expression ();
3160
3161  demand_empty_rest_of_line ();
3162}
3163
3164/* Parse .frame directives.  */
3165
3166void
3167ecoff_directive_frame (int ignore ATTRIBUTE_UNUSED)
3168{
3169  long val;
3170
3171  if (cur_proc_ptr == (proc_t *) NULL)
3172    {
3173      as_warn (_(".frame outside of .ent"));
3174      demand_empty_rest_of_line ();
3175      return;
3176    }
3177
3178  cur_proc_ptr->pdr.framereg = tc_get_register (1);
3179
3180  SKIP_WHITESPACE ();
3181  if (*input_line_pointer++ != ','
3182      || get_absolute_expression_and_terminator (&val) != ',')
3183    {
3184      as_warn (_("bad .frame directive"));
3185      --input_line_pointer;
3186      demand_empty_rest_of_line ();
3187      return;
3188    }
3189
3190  cur_proc_ptr->pdr.frameoffset = val;
3191
3192  cur_proc_ptr->pdr.pcreg = tc_get_register (0);
3193
3194  /* Alpha-OSF1 adds "the offset of saved $a0 from $sp", according to
3195     Sandro.  I don't yet know where this value should be stored, if
3196     anywhere.  Don't call demand_empty_rest_of_line ().  */
3197  s_ignore (42);
3198}
3199
3200/* Parse .mask directives.  */
3201
3202void
3203ecoff_directive_mask (int ignore ATTRIBUTE_UNUSED)
3204{
3205  long val;
3206
3207  if (cur_proc_ptr == (proc_t *) NULL)
3208    {
3209      as_warn (_(".mask outside of .ent"));
3210      demand_empty_rest_of_line ();
3211      return;
3212    }
3213
3214  if (get_absolute_expression_and_terminator (&val) != ',')
3215    {
3216      as_warn (_("bad .mask directive"));
3217      --input_line_pointer;
3218      demand_empty_rest_of_line ();
3219      return;
3220    }
3221
3222  cur_proc_ptr->pdr.regmask = val;
3223  cur_proc_ptr->pdr.regoffset = get_absolute_expression ();
3224
3225  demand_empty_rest_of_line ();
3226}
3227
3228/* Parse .loc directives.  */
3229
3230void
3231ecoff_directive_loc (int ignore ATTRIBUTE_UNUSED)
3232{
3233  lineno_list_t *list;
3234  symint_t lineno;
3235
3236  if (cur_file_ptr == (efdr_t *) NULL)
3237    {
3238      as_warn (_(".loc before .file"));
3239      demand_empty_rest_of_line ();
3240      return;
3241    }
3242
3243  if (now_seg != text_section)
3244    {
3245      as_warn (_(".loc outside of .text"));
3246      demand_empty_rest_of_line ();
3247      return;
3248    }
3249
3250  /* Skip the file number.  */
3251  SKIP_WHITESPACE ();
3252  get_absolute_expression ();
3253  SKIP_WHITESPACE ();
3254
3255  lineno = get_absolute_expression ();
3256
3257#ifndef NO_LISTING
3258  if (listing)
3259    listing_source_line (lineno);
3260#endif
3261
3262  /* If we're building stabs, then output a special label rather than
3263     ECOFF line number info.  */
3264  if (stabs_seen)
3265    {
3266      (void) add_ecoff_symbol ((char *) NULL, st_Label, sc_Text,
3267			       symbol_new (FAKE_LABEL_NAME, now_seg,
3268					   (valueT) frag_now_fix (),
3269					   frag_now),
3270			       (bfd_vma) 0, 0, lineno);
3271      return;
3272    }
3273
3274  list = allocate_lineno_list ();
3275
3276  list->next = (lineno_list_t *) NULL;
3277  list->file = cur_file_ptr;
3278  list->proc = cur_proc_ptr;
3279  list->frag = frag_now;
3280  list->paddr = frag_now_fix ();
3281  list->lineno = lineno;
3282
3283  /* We don't want to merge files which have line numbers.  */
3284  cur_file_ptr->fdr.fMerge = 0;
3285
3286  /* A .loc directive will sometimes appear before a .ent directive,
3287     which means that cur_proc_ptr will be NULL here.  Arrange to
3288     patch this up.  */
3289  if (cur_proc_ptr == (proc_t *) NULL)
3290    {
3291      lineno_list_t **pl;
3292
3293      pl = &noproc_lineno;
3294      while (*pl != (lineno_list_t *) NULL)
3295	pl = &(*pl)->next;
3296      *pl = list;
3297    }
3298  else
3299    {
3300      last_lineno = list;
3301      *last_lineno_ptr = list;
3302      last_lineno_ptr = &list->next;
3303    }
3304}
3305
3306/* The MIPS assembler sometimes inserts nop instructions in the
3307   instruction stream.  When this happens, we must patch up the .loc
3308   information so that it points to the instruction after the nop.  */
3309
3310void
3311ecoff_fix_loc (fragS *old_frag, unsigned long old_frag_offset)
3312{
3313  if (last_lineno != NULL
3314      && last_lineno->frag == old_frag
3315      && last_lineno->paddr == old_frag_offset)
3316    {
3317      last_lineno->frag = frag_now;
3318      last_lineno->paddr = frag_now_fix ();
3319    }
3320}
3321
3322/* Make sure the @stabs symbol is emitted.  */
3323
3324static void
3325mark_stabs (int ignore ATTRIBUTE_UNUSED)
3326{
3327  if (! stabs_seen)
3328    {
3329      /* Add a dummy @stabs symbol.  */
3330      stabs_seen = 1;
3331      (void) add_ecoff_symbol (stabs_symbol, st_Nil, sc_Info,
3332			       (symbolS *) NULL,
3333			       (bfd_vma) 0, (symint_t) -1,
3334			       ECOFF_MARK_STAB (0));
3335    }
3336}
3337
3338/* Parse .weakext directives.  */
3339#ifndef TC_MIPS
3340/* For TC_MIPS use the version in tc-mips.c.  */
3341void
3342ecoff_directive_weakext (int ignore ATTRIBUTE_UNUSED)
3343{
3344  char *name;
3345  int c;
3346  symbolS *symbolP;
3347  expressionS exp;
3348
3349  c = get_symbol_name (&name);
3350  symbolP = symbol_find_or_make (name);
3351  (void) restore_line_pointer (c);
3352
3353  SKIP_WHITESPACE ();
3354
3355  if (*input_line_pointer == ',')
3356    {
3357      if (S_IS_DEFINED (symbolP))
3358	{
3359	  as_bad (_("symbol `%s' is already defined"),
3360		  S_GET_NAME (symbolP));
3361	  ignore_rest_of_line ();
3362	  return;
3363	}
3364
3365      ++input_line_pointer;
3366      SKIP_WHITESPACE ();
3367      if (! is_end_of_line[(unsigned char) *input_line_pointer])
3368	{
3369	  expression (&exp);
3370	  if (exp.X_op != O_symbol)
3371	    {
3372	      as_bad (_("bad .weakext directive"));
3373	      ignore_rest_of_line ();
3374	      return;
3375	    }
3376	  symbol_set_value_expression (symbolP, &exp);
3377	}
3378    }
3379
3380  S_SET_WEAK (symbolP);
3381
3382  demand_empty_rest_of_line ();
3383}
3384#endif /* not TC_MIPS */
3385
3386/* Handle .stabs directives.  The actual parsing routine is done by a
3387   generic routine.  This routine is called via OBJ_PROCESS_STAB.
3388   When this is called, input_line_pointer will be pointing at the
3389   value field of the stab.
3390
3391   .stabs directives have five fields:
3392	"string"	a string, encoding the type information.
3393	code		a numeric code, defined in <stab.h>
3394	0		a zero
3395	desc		a zero or line number
3396	value		a numeric value or an address.
3397
3398    If the value is relocatable, we transform this into:
3399	iss		points as an index into string space
3400	value		value from lookup of the name
3401	st		st from lookup of the name
3402	sc		sc from lookup of the name
3403	index		code|CODE_MASK
3404
3405    If the value is not relocatable, we transform this into:
3406	iss		points as an index into string space
3407	value		value
3408	st		st_Nil
3409	sc		sc_Nil
3410	index		code|CODE_MASK
3411
3412    .stabn directives have four fields (string is null):
3413	code		a numeric code, defined in <stab.h>
3414	0		a zero
3415	desc		a zero or a line number
3416	value		a numeric value or an address.  */
3417
3418void
3419ecoff_stab (segT sec ATTRIBUTE_UNUSED,
3420	    int what,
3421	    const char *string,
3422	    int type,
3423	    int other,
3424	    int desc)
3425{
3426  efdr_t *save_file_ptr = cur_file_ptr;
3427  symbolS *sym;
3428  symint_t value;
3429  bfd_vma addend;
3430  st_t st;
3431  sc_t sc;
3432  symint_t indx;
3433  localsym_t *hold = NULL;
3434
3435  ecoff_debugging_seen = 1;
3436
3437  /* We don't handle .stabd.  */
3438  if (what != 's' && what != 'n')
3439    {
3440      as_bad (_(".stab%c is not supported"), what);
3441      return;
3442    }
3443
3444  /* A .stabn uses a null name, not an empty string.  */
3445  if (what == 'n')
3446    string = NULL;
3447
3448  /* We ignore the other field.  */
3449  if (other != 0)
3450    as_warn (_(".stab%c: ignoring non-zero other field"), what);
3451
3452  /* Make sure we have a current file.  */
3453  if (cur_file_ptr == (efdr_t *) NULL)
3454    {
3455      add_file ((const char *) NULL, 0, 1);
3456      save_file_ptr = cur_file_ptr;
3457    }
3458
3459  /* For stabs in ECOFF, the first symbol must be @stabs.  This is a
3460     signal to gdb.  */
3461  if (stabs_seen == 0)
3462    mark_stabs (0);
3463
3464  /* Line number stabs are handled differently, since they have two
3465     values, the line number and the address of the label.  We use the
3466     index field (aka desc) to hold the line number, and the value
3467     field to hold the address.  The symbol type is st_Label, which
3468     should be different from the other stabs, so that gdb can
3469     recognize it.  */
3470  if (type == N_SLINE)
3471    {
3472      SYMR dummy_symr;
3473      char *name;
3474      char name_end;
3475
3476#ifndef NO_LISTING
3477      if (listing)
3478	listing_source_line ((unsigned int) desc);
3479#endif
3480
3481      dummy_symr.index = desc;
3482      if (dummy_symr.index != desc)
3483	{
3484	  as_warn (_("line number (%d) for .stab%c directive cannot fit in index field (20 bits)"),
3485		   desc, what);
3486	  return;
3487	}
3488
3489      name_end = get_symbol_name (&name);
3490      sym = symbol_find_or_make (name);
3491      (void) restore_line_pointer (name_end);
3492
3493      value = 0;
3494      addend = 0;
3495      st = st_Label;
3496      sc = sc_Text;
3497      indx = desc;
3498    }
3499  else
3500    {
3501#ifndef NO_LISTING
3502      if (listing && (type == N_SO || type == N_SOL))
3503	listing_source_file (string);
3504#endif
3505
3506      if (ISDIGIT (*input_line_pointer)
3507	  || *input_line_pointer == '-'
3508	  || *input_line_pointer == '+')
3509	{
3510	  st = st_Nil;
3511	  sc = sc_Nil;
3512	  sym = (symbolS *) NULL;
3513	  value = get_absolute_expression ();
3514	  addend = 0;
3515	}
3516      else if (! is_name_beginner ((unsigned char) *input_line_pointer))
3517	{
3518	  as_warn (_("illegal .stab%c directive, bad character"), what);
3519	  return;
3520	}
3521      else
3522	{
3523	  expressionS exp;
3524
3525	  sc = sc_Nil;
3526	  st = st_Nil;
3527
3528	  expression (&exp);
3529	  if (exp.X_op == O_constant)
3530	    {
3531	      sym = NULL;
3532	      value = exp.X_add_number;
3533	      addend = 0;
3534	    }
3535	  else if (exp.X_op == O_symbol)
3536	    {
3537	      sym = exp.X_add_symbol;
3538	      value = 0;
3539	      addend = exp.X_add_number;
3540	    }
3541	  else
3542	    {
3543	      sym = make_expr_symbol (&exp);
3544	      value = 0;
3545	      addend = 0;
3546	    }
3547	}
3548
3549      indx = ECOFF_MARK_STAB (type);
3550    }
3551
3552  /* Don't store the stabs symbol we are creating as the type of the
3553     ECOFF symbol.  We want to compute the type of the ECOFF symbol
3554     independently.  */
3555  if (sym != (symbolS *) NULL)
3556    hold = symbol_get_obj (sym)->ecoff_symbol;
3557
3558  (void) add_ecoff_symbol (string, st, sc, sym, addend, value, indx);
3559
3560  if (sym != (symbolS *) NULL)
3561    symbol_get_obj (sym)->ecoff_symbol = hold;
3562
3563  /* Restore normal file type.  */
3564  cur_file_ptr = save_file_ptr;
3565}
3566
3567/* Frob an ECOFF symbol.  Small common symbols go into a special
3568   .scommon section rather than bfd_com_section.  */
3569
3570void
3571ecoff_frob_symbol (symbolS *sym)
3572{
3573  if (S_IS_COMMON (sym)
3574      && S_GET_VALUE (sym) > 0
3575      && S_GET_VALUE (sym) <= bfd_get_gp_size (stdoutput))
3576    {
3577      static asection scom_section;
3578      static asymbol scom_symbol;
3579
3580      /* We must construct a fake section similar to bfd_com_section
3581         but with the name .scommon.  */
3582      if (scom_section.name == NULL)
3583	{
3584	  scom_section = *bfd_com_section_ptr;
3585	  scom_section.name = ".scommon";
3586	  scom_section.output_section = &scom_section;
3587	  scom_section.symbol = &scom_symbol;
3588	  scom_section.symbol_ptr_ptr = &scom_section.symbol;
3589	  scom_symbol = *bfd_com_section_ptr->symbol;
3590	  scom_symbol.name = ".scommon";
3591	  scom_symbol.section = &scom_section;
3592	}
3593      S_SET_SEGMENT (sym, &scom_section);
3594    }
3595
3596  /* Double check weak symbols.  */
3597  if (S_IS_WEAK (sym))
3598    {
3599      if (S_IS_COMMON (sym))
3600	as_bad (_("symbol `%s' can not be both weak and common"),
3601		S_GET_NAME (sym));
3602    }
3603}
3604
3605/* Add bytes to the symbolic information buffer.  */
3606
3607static char *
3608ecoff_add_bytes (char **buf,
3609		 char **bufend,
3610		 char *bufptr,
3611		 unsigned long need)
3612{
3613  unsigned long at;
3614  unsigned long want;
3615
3616  at = bufptr - *buf;
3617  need -= *bufend - bufptr;
3618  if (need < PAGE_SIZE)
3619    need = PAGE_SIZE;
3620  want = (*bufend - *buf) + need;
3621  *buf = XRESIZEVEC (char, *buf, want);
3622  *bufend = *buf + want;
3623  return *buf + at;
3624}
3625
3626/* Adjust the symbolic information buffer to the alignment required
3627   for the ECOFF target debugging information.  */
3628
3629static unsigned long
3630ecoff_padding_adjust (const struct ecoff_debug_swap *backend,
3631		      char **buf,
3632		      char **bufend,
3633		      unsigned long offset,
3634		      char **bufptrptr)
3635{
3636  bfd_size_type align;
3637
3638  align = backend->debug_align;
3639  if ((offset & (align - 1)) != 0)
3640    {
3641      unsigned long add;
3642
3643      add = align - (offset & (align - 1));
3644      if ((unsigned long) (*bufend - (*buf + offset)) < add)
3645	(void) ecoff_add_bytes (buf, bufend, *buf + offset, add);
3646      memset (*buf + offset, 0, add);
3647      offset += add;
3648      if (bufptrptr != (char **) NULL)
3649	*bufptrptr = *buf + offset;
3650    }
3651
3652  return offset;
3653}
3654
3655/* Build the line number information.  */
3656
3657static unsigned long
3658ecoff_build_lineno (const struct ecoff_debug_swap *backend,
3659		    char **buf,
3660		    char **bufend,
3661		    unsigned long offset,
3662		    long *linecntptr)
3663{
3664  char *bufptr;
3665  lineno_list_t *l;
3666  lineno_list_t *last;
3667  efdr_t *file;
3668  proc_t *proc;
3669  unsigned long c;
3670  long iline;
3671  long totcount;
3672  lineno_list_t first;
3673  lineno_list_t *local_first_lineno = first_lineno;
3674
3675  if (linecntptr != (long *) NULL)
3676    *linecntptr = 0;
3677
3678  bufptr = *buf + offset;
3679
3680  file = (efdr_t *) NULL;
3681  proc = (proc_t *) NULL;
3682  last = (lineno_list_t *) NULL;
3683  c = offset;
3684  iline = 0;
3685  totcount = 0;
3686
3687  /* FIXME?  Now that MIPS embedded-PIC is gone, it may be safe to
3688     remove this code.  */
3689  /* For some reason the address of the first procedure is ignored
3690     when reading line numbers.  This doesn't matter if the address of
3691     the first procedure is 0, but when gcc is generating MIPS
3692     embedded PIC code, it will put strings in the .text section
3693     before the first procedure.  We cope by inserting a dummy line if
3694     the address of the first procedure is not 0.  Hopefully this
3695     won't screw things up too badly.
3696
3697     Don't do this for ECOFF assembly source line numbers.  They work
3698     without this extra attention.  */
3699  if (debug_type != DEBUG_ECOFF
3700      && first_proc_ptr != (proc_t *) NULL
3701      && local_first_lineno != (lineno_list_t *) NULL
3702      && ((S_GET_VALUE (first_proc_ptr->sym->as_sym)
3703	   + bfd_section_vma (S_GET_SEGMENT (first_proc_ptr->sym->as_sym)))
3704	  != 0))
3705    {
3706      first.file = local_first_lineno->file;
3707      first.proc = local_first_lineno->proc;
3708      first.frag = &zero_address_frag;
3709      first.paddr = 0;
3710      first.lineno = 0;
3711
3712      first.next = local_first_lineno;
3713      local_first_lineno = &first;
3714    }
3715
3716  for (l = local_first_lineno; l != (lineno_list_t *) NULL; l = l->next)
3717    {
3718      long count;
3719      long delta;
3720
3721      /* Get the offset to the memory address of the next line number
3722         (in words).  Do this first, so that we can skip ahead to the
3723         next useful line number entry.  */
3724      if (l->next == (lineno_list_t *) NULL)
3725	{
3726	  /* We want a count of zero, but it will be decremented
3727	     before it is used.  */
3728	  count = 1;
3729	}
3730      else if (l->next->frag->fr_address + l->next->paddr
3731	       > l->frag->fr_address + l->paddr)
3732	{
3733	  count = ((l->next->frag->fr_address + l->next->paddr
3734		    - (l->frag->fr_address + l->paddr))
3735		   >> 2);
3736	}
3737      else
3738	{
3739	  /* Don't change last, so we still get the right delta.  */
3740	  continue;
3741	}
3742
3743      if (l->file != file || l->proc != proc)
3744	{
3745	  if (l->proc != proc && proc != (proc_t *) NULL)
3746	    proc->pdr.lnHigh = last->lineno;
3747	  if (l->file != file && file != (efdr_t *) NULL)
3748	    {
3749	      file->fdr.cbLine = c - file->fdr.cbLineOffset;
3750	      file->fdr.cline = totcount + count;
3751	      if (linecntptr != (long *) NULL)
3752		*linecntptr += totcount + count;
3753	      totcount = 0;
3754	    }
3755
3756	  if (l->file != file)
3757	    {
3758	      efdr_t *last_file = file;
3759
3760	      file = l->file;
3761	      if (last_file != (efdr_t *) NULL)
3762		file->fdr.ilineBase
3763		  = last_file->fdr.ilineBase + last_file->fdr.cline;
3764	      else
3765		file->fdr.ilineBase = 0;
3766	      file->fdr.cbLineOffset = c;
3767	    }
3768	  if (l->proc != proc)
3769	    {
3770	      proc = l->proc;
3771	      if (proc != (proc_t *) NULL)
3772		{
3773		  proc->pdr.lnLow = l->lineno;
3774		  proc->pdr.cbLineOffset = c - file->fdr.cbLineOffset;
3775		  proc->pdr.iline = totcount;
3776		}
3777	    }
3778
3779	  last = (lineno_list_t *) NULL;
3780	}
3781
3782      totcount += count;
3783
3784      /* Get the offset to this line number.  */
3785      if (last == (lineno_list_t *) NULL)
3786	delta = 0;
3787      else
3788	delta = l->lineno - last->lineno;
3789
3790      /* Put in the offset to this line number.  */
3791      while (delta != 0)
3792	{
3793	  int setcount;
3794
3795	  /* 1 is added to each count read.  */
3796	  --count;
3797	  /* We can only adjust the word count by up to 15 words at a
3798	     time.  */
3799	  if (count <= 0x0f)
3800	    {
3801	      setcount = count;
3802	      count = 0;
3803	    }
3804	  else
3805	    {
3806	      setcount = 0x0f;
3807	      count -= 0x0f;
3808	    }
3809	  if (delta >= -7 && delta <= 7)
3810	    {
3811	      if (bufptr >= *bufend)
3812		bufptr = ecoff_add_bytes (buf, bufend, bufptr, (long) 1);
3813	      *bufptr++ = setcount + (delta << 4);
3814	      delta = 0;
3815	      ++c;
3816	    }
3817	  else
3818	    {
3819	      int set;
3820
3821	      if (*bufend - bufptr < 3)
3822		bufptr = ecoff_add_bytes (buf, bufend, bufptr, (long) 3);
3823	      *bufptr++ = setcount + (8 << 4);
3824	      if (delta < -0x8000)
3825		{
3826		  set = -0x8000;
3827		  delta += 0x8000;
3828		}
3829	      else if (delta > 0x7fff)
3830		{
3831		  set = 0x7fff;
3832		  delta -= 0x7fff;
3833		}
3834	      else
3835		{
3836		  set = delta;
3837		  delta = 0;
3838		}
3839	      *bufptr++ = set >> 8;
3840	      *bufptr++ = set & 0xffff;
3841	      c += 3;
3842	    }
3843	}
3844
3845      /* Finish adjusting the count.  */
3846      while (count > 0)
3847	{
3848	  if (bufptr >= *bufend)
3849	    bufptr = ecoff_add_bytes (buf, bufend, bufptr, (long) 1);
3850	  /* 1 is added to each count read.  */
3851	  --count;
3852	  if (count > 0x0f)
3853	    {
3854	      *bufptr++ = 0x0f;
3855	      count -= 0x0f;
3856	    }
3857	  else
3858	    {
3859	      *bufptr++ = count;
3860	      count = 0;
3861	    }
3862	  ++c;
3863	}
3864
3865      ++iline;
3866      last = l;
3867    }
3868
3869  if (proc != (proc_t *) NULL)
3870    proc->pdr.lnHigh = last->lineno;
3871  if (file != (efdr_t *) NULL)
3872    {
3873      file->fdr.cbLine = c - file->fdr.cbLineOffset;
3874      file->fdr.cline = totcount;
3875    }
3876
3877  if (linecntptr != (long *) NULL)
3878    *linecntptr += totcount;
3879
3880  c = ecoff_padding_adjust (backend, buf, bufend, c, &bufptr);
3881
3882  return c;
3883}
3884
3885/* Build and swap out the symbols.  */
3886
3887static unsigned long
3888ecoff_build_symbols (const struct ecoff_debug_swap *backend,
3889		     char **buf,
3890		     char **bufend,
3891		     unsigned long offset)
3892{
3893  const bfd_size_type external_sym_size = backend->external_sym_size;
3894  void (* const swap_sym_out) (bfd *, const SYMR *, void *)
3895    = backend->swap_sym_out;
3896  char *sym_out;
3897  long isym;
3898  vlinks_t *file_link;
3899
3900  sym_out = *buf + offset;
3901
3902  isym = 0;
3903
3904  /* The symbols are stored by file.  */
3905  for (file_link = file_desc.first;
3906       file_link != (vlinks_t *) NULL;
3907       file_link = file_link->next)
3908    {
3909      int ifilesym;
3910      int fil_cnt;
3911      efdr_t *fil_ptr;
3912      efdr_t *fil_end;
3913
3914      if (file_link->next == (vlinks_t *) NULL)
3915	fil_cnt = file_desc.objects_last_page;
3916      else
3917	fil_cnt = file_desc.objects_per_page;
3918      fil_ptr = file_link->datum->file;
3919      fil_end = fil_ptr + fil_cnt;
3920      for (; fil_ptr < fil_end; fil_ptr++)
3921	{
3922	  vlinks_t *sym_link;
3923
3924	  fil_ptr->fdr.isymBase = isym;
3925	  ifilesym = isym;
3926	  for (sym_link = fil_ptr->symbols.first;
3927	       sym_link != (vlinks_t *) NULL;
3928	       sym_link = sym_link->next)
3929	    {
3930	      int sym_cnt;
3931	      localsym_t *sym_ptr;
3932	      localsym_t *sym_end;
3933
3934	      if (sym_link->next == (vlinks_t *) NULL)
3935		sym_cnt = fil_ptr->symbols.objects_last_page;
3936	      else
3937		sym_cnt = fil_ptr->symbols.objects_per_page;
3938	      sym_ptr = sym_link->datum->sym;
3939	      sym_end = sym_ptr + sym_cnt;
3940	      for (; sym_ptr < sym_end; sym_ptr++)
3941		{
3942		  int local;
3943		  symbolS *as_sym;
3944		  forward_t *f;
3945
3946		  know (sym_ptr->file_ptr == fil_ptr);
3947
3948		  /* If there is no associated gas symbol, then this
3949		     is a pure debugging symbol.  We have already
3950		     added the name (if any) to fil_ptr->strings.
3951		     Otherwise we must decide whether this is an
3952		     external or a local symbol (actually, it may be
3953		     both if the local provides additional debugging
3954		     information for the external).  */
3955		  local = 1;
3956		  as_sym = sym_ptr->as_sym;
3957		  if (as_sym != (symbolS *) NULL)
3958		    {
3959		      symint_t indx;
3960
3961		      /* The value of a block start symbol is the
3962		         offset from the start of the procedure.  For
3963		         other symbols we just use the gas value (but
3964		         we must offset it by the vma of the section,
3965		         just as BFD does, because BFD will not see
3966		         this value).  */
3967		      if (sym_ptr->ecoff_sym.asym.st == (int) st_Block
3968			  && sym_ptr->ecoff_sym.asym.sc == (int) sc_Text)
3969			{
3970			  symbolS *begin_sym;
3971
3972			  know (sym_ptr->proc_ptr != (proc_t *) NULL);
3973			  begin_sym = sym_ptr->proc_ptr->sym->as_sym;
3974			  if (S_GET_SEGMENT (as_sym)
3975			      != S_GET_SEGMENT (begin_sym))
3976			    as_warn (_(".begin/.bend in different segments"));
3977			  sym_ptr->ecoff_sym.asym.value =
3978			    S_GET_VALUE (as_sym) - S_GET_VALUE (begin_sym);
3979			}
3980		      else
3981			sym_ptr->ecoff_sym.asym.value =
3982			  (S_GET_VALUE (as_sym)
3983			   + bfd_section_vma (S_GET_SEGMENT (as_sym))
3984			   + sym_ptr->addend);
3985
3986		      sym_ptr->ecoff_sym.weakext = S_IS_WEAK (as_sym);
3987
3988		      /* Set st_Proc to st_StaticProc for local
3989			 functions.  */
3990		      if (sym_ptr->ecoff_sym.asym.st == st_Proc
3991			  && S_IS_DEFINED (as_sym)
3992			  && ! S_IS_EXTERNAL (as_sym)
3993			  && ! S_IS_WEAK (as_sym))
3994			sym_ptr->ecoff_sym.asym.st = st_StaticProc;
3995
3996		      /* Get the type and storage class based on where
3997		         the symbol actually wound up.  Traditionally,
3998		         N_LBRAC and N_RBRAC are *not* relocated.  */
3999		      indx = sym_ptr->ecoff_sym.asym.index;
4000		      if (sym_ptr->ecoff_sym.asym.st == st_Nil
4001			  && sym_ptr->ecoff_sym.asym.sc == sc_Nil
4002			  && (! ECOFF_IS_STAB (&sym_ptr->ecoff_sym.asym)
4003			      || ((ECOFF_UNMARK_STAB (indx) != N_LBRAC)
4004				  && (ECOFF_UNMARK_STAB (indx) != N_RBRAC))))
4005			{
4006			  segT seg;
4007			  const char *segname;
4008			  st_t st;
4009			  sc_t sc;
4010
4011			  seg = S_GET_SEGMENT (as_sym);
4012			  segname = segment_name (seg);
4013
4014			  if (! ECOFF_IS_STAB (&sym_ptr->ecoff_sym.asym)
4015			      && (S_IS_EXTERNAL (as_sym)
4016				  || S_IS_WEAK (as_sym)
4017				  || ! S_IS_DEFINED (as_sym)))
4018			    {
4019			      if ((symbol_get_bfdsym (as_sym)->flags
4020				   & BSF_FUNCTION) != 0)
4021				st = st_Proc;
4022			      else
4023				st = st_Global;
4024			    }
4025			  else if (seg == text_section)
4026			    st = st_Label;
4027			  else
4028			    st = st_Static;
4029
4030			  if (! S_IS_DEFINED (as_sym))
4031			    {
4032			      valueT s;
4033
4034			      s = symbol_get_obj (as_sym)->ecoff_extern_size;
4035			      if (s == 0
4036				  || s > bfd_get_gp_size (stdoutput))
4037				sc = sc_Undefined;
4038			      else
4039				{
4040				  sc = sc_SUndefined;
4041				  sym_ptr->ecoff_sym.asym.value = s;
4042				}
4043#ifdef S_SET_SIZE
4044			      S_SET_SIZE (as_sym, s);
4045#endif
4046			    }
4047			  else if (S_IS_COMMON (as_sym))
4048			    {
4049			      if (S_GET_VALUE (as_sym) > 0
4050				  && (S_GET_VALUE (as_sym)
4051				      <= bfd_get_gp_size (stdoutput)))
4052				sc = sc_SCommon;
4053			      else
4054				sc = sc_Common;
4055			    }
4056			  else if (seg == text_section)
4057			    sc = sc_Text;
4058			  else if (seg == data_section)
4059			    sc = sc_Data;
4060			  else if (strcmp (segname, ".rdata") == 0
4061				   || strcmp (segname, ".rodata") == 0)
4062			    sc = sc_RData;
4063			  else if (strcmp (segname, ".sdata") == 0)
4064			    sc = sc_SData;
4065			  else if (seg == bss_section)
4066			    sc = sc_Bss;
4067			  else if (strcmp (segname, ".sbss") == 0)
4068			    sc = sc_SBss;
4069			  else if (seg == bfd_abs_section_ptr)
4070			    sc = sc_Abs;
4071			  else
4072			    {
4073			      /* This must be a user named section.
4074			         This is not possible in ECOFF, but it
4075			         is in ELF.  */
4076			      sc = sc_Data;
4077			    }
4078
4079			  sym_ptr->ecoff_sym.asym.st = (int) st;
4080			  sym_ptr->ecoff_sym.asym.sc = (int) sc;
4081			}
4082
4083		      /* This is just an external symbol if it is
4084		         outside a procedure and it has a type.
4085		         FIXME: g++ will generate symbols which have
4086		         different names in the debugging information
4087		         than the actual symbol.  Should we handle
4088		         them here?  */
4089		      if ((S_IS_EXTERNAL (as_sym)
4090			   || S_IS_WEAK (as_sym)
4091			   || ! S_IS_DEFINED (as_sym))
4092			  && sym_ptr->proc_ptr == (proc_t *) NULL
4093			  && sym_ptr->ecoff_sym.asym.st != (int) st_Nil
4094			  && ! ECOFF_IS_STAB (&sym_ptr->ecoff_sym.asym))
4095			local = 0;
4096
4097		      /* This is just an external symbol if it is a
4098		         common symbol.  */
4099		      if (S_IS_COMMON (as_sym))
4100			local = 0;
4101
4102		      /* If an st_end symbol has an associated gas
4103		         symbol, then it is a local label created for
4104		         a .bend or .end directive.  Stabs line
4105		         numbers will have FAKE_LABEL_CHAR in the names.  */
4106		      if (local
4107			  && sym_ptr->ecoff_sym.asym.st != st_End
4108			  && strchr (sym_ptr->name, FAKE_LABEL_CHAR) == 0)
4109			sym_ptr->ecoff_sym.asym.iss =
4110			  add_string (&fil_ptr->strings,
4111				      fil_ptr->str_hash,
4112				      sym_ptr->name,
4113				      (shash_t **) NULL);
4114		    }
4115
4116		  /* We now know the index of this symbol; fill in
4117		     locations that have been waiting for that
4118		     information.  */
4119		  if (sym_ptr->begin_ptr != (localsym_t *) NULL)
4120		    {
4121		      localsym_t *begin_ptr;
4122		      st_t begin_type;
4123
4124		      know (local);
4125		      begin_ptr = sym_ptr->begin_ptr;
4126		      know (begin_ptr->sym_index != -1);
4127		      sym_ptr->ecoff_sym.asym.index = begin_ptr->sym_index;
4128		      if (sym_ptr->ecoff_sym.asym.sc != (int) sc_Info)
4129			sym_ptr->ecoff_sym.asym.iss =
4130			  begin_ptr->ecoff_sym.asym.iss;
4131
4132		      begin_type = (st_t) begin_ptr->ecoff_sym.asym.st;
4133		      if (begin_type == st_File
4134			  || begin_type == st_Block)
4135			{
4136			  begin_ptr->ecoff_sym.asym.index =
4137			    isym - ifilesym + 1;
4138			  (*swap_sym_out) (stdoutput,
4139					   &begin_ptr->ecoff_sym.asym,
4140					   (*buf
4141					    + offset
4142					    + (begin_ptr->sym_index
4143					       * external_sym_size)));
4144			}
4145		      else
4146			{
4147			  know (begin_ptr->index_ptr != (aux_t *) NULL);
4148			  begin_ptr->index_ptr->data.isym =
4149			    isym - ifilesym + 1;
4150			}
4151
4152		      /* The value of the symbol marking the end of a
4153		         procedure is the size of the procedure.  The
4154		         value of the symbol marking the end of a
4155		         block is the offset from the start of the
4156		         procedure to the block.  */
4157		      if (begin_type == st_Proc
4158			  || begin_type == st_StaticProc)
4159			{
4160			  know (as_sym != (symbolS *) NULL);
4161			  know (begin_ptr->as_sym != (symbolS *) NULL);
4162			  if (S_GET_SEGMENT (as_sym)
4163			      != S_GET_SEGMENT (begin_ptr->as_sym))
4164			    as_warn (_(".begin/.bend in different segments"));
4165			  sym_ptr->ecoff_sym.asym.value =
4166			    (S_GET_VALUE (as_sym)
4167			     - S_GET_VALUE (begin_ptr->as_sym));
4168
4169			  /* If the size is odd, this is probably a
4170			     mips16 function; force it to be even.  */
4171			  if ((sym_ptr->ecoff_sym.asym.value & 1) != 0)
4172			    ++sym_ptr->ecoff_sym.asym.value;
4173
4174#ifdef S_SET_SIZE
4175			  S_SET_SIZE (begin_ptr->as_sym,
4176				      sym_ptr->ecoff_sym.asym.value);
4177#endif
4178			}
4179		      else if (begin_type == st_Block
4180			       && sym_ptr->ecoff_sym.asym.sc != (int) sc_Info)
4181			{
4182			  symbolS *begin_sym;
4183
4184			  know (as_sym != (symbolS *) NULL);
4185			  know (sym_ptr->proc_ptr != (proc_t *) NULL);
4186			  begin_sym = sym_ptr->proc_ptr->sym->as_sym;
4187			  if (S_GET_SEGMENT (as_sym)
4188			      != S_GET_SEGMENT (begin_sym))
4189			    as_warn (_(".begin/.bend in different segments"));
4190			  sym_ptr->ecoff_sym.asym.value =
4191			    S_GET_VALUE (as_sym) - S_GET_VALUE (begin_sym);
4192			}
4193		    }
4194
4195		  for (f = sym_ptr->forward_ref;
4196		       f != (forward_t *) NULL;
4197		       f = f->next)
4198		    {
4199		      know (local);
4200		      f->ifd_ptr->data.isym = fil_ptr->file_index;
4201		      f->index_ptr->data.rndx.index = isym - ifilesym;
4202		    }
4203
4204		  if (local)
4205		    {
4206		      if ((bfd_size_type)(*bufend - sym_out) < external_sym_size)
4207			sym_out = ecoff_add_bytes (buf, bufend,
4208						   sym_out,
4209						   external_sym_size);
4210		      (*swap_sym_out) (stdoutput, &sym_ptr->ecoff_sym.asym,
4211				       sym_out);
4212		      sym_out += external_sym_size;
4213
4214		      sym_ptr->sym_index = isym;
4215
4216		      if (sym_ptr->proc_ptr != (proc_t *) NULL
4217			  && sym_ptr->proc_ptr->sym == sym_ptr)
4218			sym_ptr->proc_ptr->pdr.isym = isym - ifilesym;
4219
4220		      ++isym;
4221		    }
4222
4223		  /* Record the local symbol index and file number in
4224		     case this is an external symbol.  Note that this
4225		     destroys the asym.index field.  */
4226		  if (as_sym != (symbolS *) NULL
4227		      && symbol_get_obj (as_sym)->ecoff_symbol == sym_ptr)
4228		    {
4229		      if ((sym_ptr->ecoff_sym.asym.st == st_Proc
4230			   || sym_ptr->ecoff_sym.asym.st == st_StaticProc)
4231			  && local)
4232			sym_ptr->ecoff_sym.asym.index = isym - ifilesym - 1;
4233		      sym_ptr->ecoff_sym.ifd = fil_ptr->file_index;
4234
4235		      /* Don't try to merge an FDR which has an
4236		         external symbol attached to it.  */
4237		      if (S_IS_EXTERNAL (as_sym) || S_IS_WEAK (as_sym))
4238			fil_ptr->fdr.fMerge = 0;
4239		    }
4240		}
4241	    }
4242	  fil_ptr->fdr.csym = isym - fil_ptr->fdr.isymBase;
4243	}
4244    }
4245
4246  return offset + isym * external_sym_size;
4247}
4248
4249/* Swap out the procedure information.  */
4250
4251static unsigned long
4252ecoff_build_procs (const struct ecoff_debug_swap *backend,
4253		   char **buf,
4254		   char **bufend,
4255		   unsigned long offset)
4256{
4257  const bfd_size_type external_pdr_size = backend->external_pdr_size;
4258  void (* const swap_pdr_out) (bfd *, const PDR *, void *)
4259    = backend->swap_pdr_out;
4260  char *pdr_out;
4261  long iproc;
4262  vlinks_t *file_link;
4263
4264  pdr_out = *buf + offset;
4265
4266  iproc = 0;
4267
4268  /* The procedures are stored by file.  */
4269  for (file_link = file_desc.first;
4270       file_link != (vlinks_t *) NULL;
4271       file_link = file_link->next)
4272    {
4273      int fil_cnt;
4274      efdr_t *fil_ptr;
4275      efdr_t *fil_end;
4276
4277      if (file_link->next == (vlinks_t *) NULL)
4278	fil_cnt = file_desc.objects_last_page;
4279      else
4280	fil_cnt = file_desc.objects_per_page;
4281      fil_ptr = file_link->datum->file;
4282      fil_end = fil_ptr + fil_cnt;
4283      for (; fil_ptr < fil_end; fil_ptr++)
4284	{
4285	  vlinks_t *proc_link;
4286	  int first;
4287
4288	  fil_ptr->fdr.ipdFirst = iproc;
4289	  first = 1;
4290	  for (proc_link = fil_ptr->procs.first;
4291	       proc_link != (vlinks_t *) NULL;
4292	       proc_link = proc_link->next)
4293	    {
4294	      int prc_cnt;
4295	      proc_t *proc_ptr;
4296	      proc_t *proc_end;
4297
4298	      if (proc_link->next == (vlinks_t *) NULL)
4299		prc_cnt = fil_ptr->procs.objects_last_page;
4300	      else
4301		prc_cnt = fil_ptr->procs.objects_per_page;
4302	      proc_ptr = proc_link->datum->proc;
4303	      proc_end = proc_ptr + prc_cnt;
4304	      for (; proc_ptr < proc_end; proc_ptr++)
4305		{
4306		  symbolS *adr_sym;
4307		  unsigned long adr;
4308
4309		  adr_sym = proc_ptr->sym->as_sym;
4310		  adr = (S_GET_VALUE (adr_sym)
4311			 + bfd_section_vma (S_GET_SEGMENT (adr_sym)));
4312		  if (first)
4313		    {
4314		      /* This code used to force the adr of the very
4315		         first fdr to be 0.  However, the native tools
4316		         don't do that, and I can't remember why it
4317		         used to work that way, so I took it out.  */
4318		      fil_ptr->fdr.adr = adr;
4319		      first = 0;
4320		    }
4321		  proc_ptr->pdr.adr = adr - fil_ptr->fdr.adr;
4322		  if ((bfd_size_type)(*bufend - pdr_out) < external_pdr_size)
4323		    pdr_out = ecoff_add_bytes (buf, bufend,
4324					       pdr_out,
4325					       external_pdr_size);
4326		  (*swap_pdr_out) (stdoutput, &proc_ptr->pdr, pdr_out);
4327		  pdr_out += external_pdr_size;
4328		  ++iproc;
4329		}
4330	    }
4331	  fil_ptr->fdr.cpd = iproc - fil_ptr->fdr.ipdFirst;
4332	}
4333    }
4334
4335  return offset + iproc * external_pdr_size;
4336}
4337
4338/* Swap out the aux information.  */
4339
4340static unsigned long
4341ecoff_build_aux (const struct ecoff_debug_swap *backend,
4342		 char **buf,
4343		 char **bufend,
4344		 unsigned long offset)
4345{
4346  int bigendian;
4347  union aux_ext *aux_out;
4348  long iaux;
4349  vlinks_t *file_link;
4350
4351  bigendian = bfd_big_endian (stdoutput);
4352
4353  aux_out = (union aux_ext *) (*buf + offset);
4354
4355  iaux = 0;
4356
4357  /* The aux entries are stored by file.  */
4358  for (file_link = file_desc.first;
4359       file_link != (vlinks_t *) NULL;
4360       file_link = file_link->next)
4361    {
4362      int fil_cnt;
4363      efdr_t *fil_ptr;
4364      efdr_t *fil_end;
4365
4366      if (file_link->next == (vlinks_t *) NULL)
4367	fil_cnt = file_desc.objects_last_page;
4368      else
4369	fil_cnt = file_desc.objects_per_page;
4370      fil_ptr = file_link->datum->file;
4371      fil_end = fil_ptr + fil_cnt;
4372      for (; fil_ptr < fil_end; fil_ptr++)
4373	{
4374	  vlinks_t *aux_link;
4375
4376	  fil_ptr->fdr.fBigendian = bigendian;
4377	  fil_ptr->fdr.iauxBase = iaux;
4378	  for (aux_link = fil_ptr->aux_syms.first;
4379	       aux_link != (vlinks_t *) NULL;
4380	       aux_link = aux_link->next)
4381	    {
4382	      int aux_cnt;
4383	      aux_t *aux_ptr;
4384	      aux_t *aux_end;
4385
4386	      if (aux_link->next == (vlinks_t *) NULL)
4387		aux_cnt = fil_ptr->aux_syms.objects_last_page;
4388	      else
4389		aux_cnt = fil_ptr->aux_syms.objects_per_page;
4390	      aux_ptr = aux_link->datum->aux;
4391	      aux_end = aux_ptr + aux_cnt;
4392	      for (; aux_ptr < aux_end; aux_ptr++)
4393		{
4394		  if ((unsigned long) (*bufend - (char *) aux_out)
4395		      < sizeof (union aux_ext))
4396		    aux_out = ((union aux_ext *)
4397			       ecoff_add_bytes (buf, bufend,
4398						(char *) aux_out,
4399						sizeof (union aux_ext)));
4400		  switch (aux_ptr->type)
4401		    {
4402		    case aux_tir:
4403		      (*backend->swap_tir_out) (bigendian,
4404						&aux_ptr->data.ti,
4405						&aux_out->a_ti);
4406		      break;
4407		    case aux_rndx:
4408		      (*backend->swap_rndx_out) (bigendian,
4409						 &aux_ptr->data.rndx,
4410						 &aux_out->a_rndx);
4411		      break;
4412		    case aux_dnLow:
4413		      AUX_PUT_DNLOW (bigendian, aux_ptr->data.dnLow,
4414				     aux_out);
4415		      break;
4416		    case aux_dnHigh:
4417		      AUX_PUT_DNHIGH (bigendian, aux_ptr->data.dnHigh,
4418				      aux_out);
4419		      break;
4420		    case aux_isym:
4421		      AUX_PUT_ISYM (bigendian, aux_ptr->data.isym,
4422				    aux_out);
4423		      break;
4424		    case aux_iss:
4425		      AUX_PUT_ISS (bigendian, aux_ptr->data.iss,
4426				   aux_out);
4427		      break;
4428		    case aux_width:
4429		      AUX_PUT_WIDTH (bigendian, aux_ptr->data.width,
4430				     aux_out);
4431		      break;
4432		    case aux_count:
4433		      AUX_PUT_COUNT (bigendian, aux_ptr->data.count,
4434				     aux_out);
4435		      break;
4436		    }
4437
4438		  ++aux_out;
4439		  ++iaux;
4440		}
4441	    }
4442	  fil_ptr->fdr.caux = iaux - fil_ptr->fdr.iauxBase;
4443	}
4444    }
4445
4446  return ecoff_padding_adjust (backend, buf, bufend,
4447			       offset + iaux * sizeof (union aux_ext),
4448			       (char **) NULL);
4449}
4450
4451/* Copy out the strings from a varray_t.  This returns the number of
4452   bytes copied, rather than the new offset.  */
4453
4454static unsigned long
4455ecoff_build_strings (char **buf,
4456		     char **bufend,
4457		     unsigned long offset,
4458		     varray_t *vp)
4459{
4460  unsigned long istr;
4461  char *str_out;
4462  vlinks_t *str_link;
4463
4464  str_out = *buf + offset;
4465
4466  istr = 0;
4467
4468  for (str_link = vp->first;
4469       str_link != (vlinks_t *) NULL;
4470       str_link = str_link->next)
4471    {
4472      unsigned long str_cnt;
4473
4474      if (str_link->next == (vlinks_t *) NULL)
4475	str_cnt = vp->objects_last_page;
4476      else
4477	str_cnt = vp->objects_per_page;
4478
4479      if ((unsigned long)(*bufend - str_out) < str_cnt)
4480	str_out = ecoff_add_bytes (buf, bufend, str_out, str_cnt);
4481
4482      memcpy (str_out, str_link->datum->byte, str_cnt);
4483      str_out += str_cnt;
4484      istr += str_cnt;
4485    }
4486
4487  return istr;
4488}
4489
4490/* Dump out the local strings.  */
4491
4492static unsigned long
4493ecoff_build_ss (const struct ecoff_debug_swap *backend,
4494		char **buf,
4495		char **bufend,
4496		unsigned long offset)
4497{
4498  long iss;
4499  vlinks_t *file_link;
4500
4501  iss = 0;
4502
4503  for (file_link = file_desc.first;
4504       file_link != (vlinks_t *) NULL;
4505       file_link = file_link->next)
4506    {
4507      int fil_cnt;
4508      efdr_t *fil_ptr;
4509      efdr_t *fil_end;
4510
4511      if (file_link->next == (vlinks_t *) NULL)
4512	fil_cnt = file_desc.objects_last_page;
4513      else
4514	fil_cnt = file_desc.objects_per_page;
4515      fil_ptr = file_link->datum->file;
4516      fil_end = fil_ptr + fil_cnt;
4517      for (; fil_ptr < fil_end; fil_ptr++)
4518	{
4519	  long ss_cnt;
4520
4521	  fil_ptr->fdr.issBase = iss;
4522	  ss_cnt = ecoff_build_strings (buf, bufend, offset + iss,
4523					&fil_ptr->strings);
4524	  fil_ptr->fdr.cbSs = ss_cnt;
4525	  iss += ss_cnt;
4526	}
4527    }
4528
4529  return ecoff_padding_adjust (backend, buf, bufend, offset + iss,
4530			       (char **) NULL);
4531}
4532
4533/* Swap out the file descriptors.  */
4534
4535static unsigned long
4536ecoff_build_fdr (const struct ecoff_debug_swap *backend,
4537		 char **buf,
4538		 char **bufend,
4539		 unsigned long offset)
4540{
4541  const bfd_size_type external_fdr_size = backend->external_fdr_size;
4542  void (* const swap_fdr_out) (bfd *, const FDR *, void *)
4543    = backend->swap_fdr_out;
4544  long ifile;
4545  char *fdr_out;
4546  vlinks_t *file_link;
4547
4548  ifile = 0;
4549
4550  fdr_out = *buf + offset;
4551
4552  for (file_link = file_desc.first;
4553       file_link != (vlinks_t *) NULL;
4554       file_link = file_link->next)
4555    {
4556      int fil_cnt;
4557      efdr_t *fil_ptr;
4558      efdr_t *fil_end;
4559
4560      if (file_link->next == (vlinks_t *) NULL)
4561	fil_cnt = file_desc.objects_last_page;
4562      else
4563	fil_cnt = file_desc.objects_per_page;
4564      fil_ptr = file_link->datum->file;
4565      fil_end = fil_ptr + fil_cnt;
4566      for (; fil_ptr < fil_end; fil_ptr++)
4567	{
4568	  if ((bfd_size_type)(*bufend - fdr_out) < external_fdr_size)
4569	    fdr_out = ecoff_add_bytes (buf, bufend, fdr_out,
4570				       external_fdr_size);
4571	  (*swap_fdr_out) (stdoutput, &fil_ptr->fdr, fdr_out);
4572	  fdr_out += external_fdr_size;
4573	  ++ifile;
4574	}
4575    }
4576
4577  return offset + ifile * external_fdr_size;
4578}
4579
4580/* Set up the external symbols.  These are supposed to be handled by
4581   the backend.  This routine just gets the right information and
4582   calls a backend function to deal with it.  */
4583
4584static void
4585ecoff_setup_ext (void)
4586{
4587  symbolS *sym;
4588
4589  for (sym = symbol_rootP; sym != (symbolS *) NULL; sym = symbol_next (sym))
4590    {
4591      if (symbol_get_obj (sym)->ecoff_symbol == NULL)
4592	continue;
4593
4594      /* If this is a local symbol, then force the fields to zero.  */
4595      if (! S_IS_EXTERNAL (sym)
4596	  && ! S_IS_WEAK (sym)
4597	  && S_IS_DEFINED (sym))
4598	{
4599	  struct localsym *lsym;
4600
4601	  lsym = symbol_get_obj (sym)->ecoff_symbol;
4602	  lsym->ecoff_sym.asym.value = 0;
4603	  lsym->ecoff_sym.asym.st = (int) st_Nil;
4604	  lsym->ecoff_sym.asym.sc = (int) sc_Nil;
4605	  lsym->ecoff_sym.asym.index = indexNil;
4606	}
4607
4608      obj_ecoff_set_ext (sym, &symbol_get_obj (sym)->ecoff_symbol->ecoff_sym);
4609    }
4610}
4611
4612/* Build the ECOFF debugging information.  */
4613
4614unsigned long
4615ecoff_build_debug (HDRR *hdr,
4616		   char **bufp,
4617		   const struct ecoff_debug_swap *backend)
4618{
4619  const bfd_size_type external_pdr_size = backend->external_pdr_size;
4620  tag_t *ptag;
4621  tag_t *ptag_next;
4622  efdr_t *fil_ptr;
4623  int end_warning;
4624  efdr_t *hold_file_ptr;
4625  proc_t *hold_proc_ptr;
4626  symbolS *sym;
4627  char *buf;
4628  char *bufend;
4629  unsigned long offset;
4630
4631  /* Make sure we have a file.  */
4632  if (first_file == (efdr_t *) NULL)
4633    add_file ((const char *) NULL, 0, 1);
4634
4635  /* Handle any top level tags.  */
4636  for (ptag = top_tag_head->first_tag;
4637       ptag != (tag_t *) NULL;
4638       ptag = ptag_next)
4639    {
4640      if (ptag->forward_ref != (forward_t *) NULL)
4641	add_unknown_tag (ptag);
4642
4643      ptag_next = ptag->same_block;
4644      ptag->hash_ptr->tag_ptr = ptag->same_name;
4645      free_tag (ptag);
4646    }
4647
4648  free_thead (top_tag_head);
4649
4650  /* Look through the symbols.  Add debugging information for each
4651     symbol that has not already received it.  */
4652  hold_file_ptr = cur_file_ptr;
4653  hold_proc_ptr = cur_proc_ptr;
4654  cur_proc_ptr = (proc_t *) NULL;
4655  for (sym = symbol_rootP; sym != (symbolS *) NULL; sym = symbol_next (sym))
4656    {
4657      if (symbol_get_obj (sym)->ecoff_symbol != NULL
4658	  || symbol_get_obj (sym)->ecoff_file == (efdr_t *) NULL
4659	  || (symbol_get_bfdsym (sym)->flags & BSF_SECTION_SYM) != 0)
4660	continue;
4661
4662      cur_file_ptr = symbol_get_obj (sym)->ecoff_file;
4663      add_ecoff_symbol ((const char *) NULL, st_Nil, sc_Nil, sym,
4664			(bfd_vma) 0, S_GET_VALUE (sym), indexNil);
4665    }
4666  cur_proc_ptr = hold_proc_ptr;
4667  cur_file_ptr = hold_file_ptr;
4668
4669  /* Output an ending symbol for all the files.  We have to do this
4670     here for the last file, so we may as well do it for all of the
4671     files.  */
4672  end_warning = 0;
4673  for (fil_ptr = first_file;
4674       fil_ptr != (efdr_t *) NULL;
4675       fil_ptr = fil_ptr->next_file)
4676    {
4677      cur_file_ptr = fil_ptr;
4678      while (cur_file_ptr->cur_scope != (scope_t *) NULL
4679	     && cur_file_ptr->cur_scope->prev != (scope_t *) NULL)
4680	{
4681	  cur_file_ptr->cur_scope = cur_file_ptr->cur_scope->prev;
4682	  if (! end_warning && ! cur_file_ptr->fake)
4683	    {
4684	      as_warn (_("missing .end or .bend at end of file"));
4685	      end_warning = 1;
4686	    }
4687	}
4688      if (cur_file_ptr->cur_scope != (scope_t *) NULL)
4689	(void) add_ecoff_symbol ((const char *) NULL,
4690				 st_End, sc_Text,
4691				 (symbolS *) NULL,
4692				 (bfd_vma) 0,
4693				 (symint_t) 0,
4694				 (symint_t) 0);
4695    }
4696
4697  /* Build the symbolic information.  */
4698  offset = 0;
4699  buf = XNEWVEC (char, PAGE_SIZE);
4700  bufend = buf + PAGE_SIZE;
4701
4702  /* Build the line number information.  */
4703  hdr->cbLineOffset = offset;
4704  offset = ecoff_build_lineno (backend, &buf, &bufend, offset,
4705			       &hdr->ilineMax);
4706  hdr->cbLine = offset - hdr->cbLineOffset;
4707
4708  /* We don't use dense numbers at all.  */
4709  hdr->idnMax = 0;
4710  hdr->cbDnOffset = 0;
4711
4712  /* We can't build the PDR table until we have built the symbols,
4713     because a PDR contains a symbol index.  However, we set aside
4714     space at this point.  */
4715  hdr->ipdMax = proc_cnt;
4716  hdr->cbPdOffset = offset;
4717  if ((bfd_size_type)(bufend - (buf + offset)) < proc_cnt * external_pdr_size)
4718    (void) ecoff_add_bytes (&buf, &bufend, buf + offset,
4719			    proc_cnt * external_pdr_size);
4720  offset += proc_cnt * external_pdr_size;
4721
4722  /* Build the local symbols.  */
4723  hdr->cbSymOffset = offset;
4724  offset = ecoff_build_symbols (backend, &buf, &bufend, offset);
4725  hdr->isymMax = (offset - hdr->cbSymOffset) / backend->external_sym_size;
4726
4727  /* Building the symbols initializes the symbol index in the PDR's.
4728     Now we can swap out the PDR's.  */
4729  (void) ecoff_build_procs (backend, &buf, &bufend, hdr->cbPdOffset);
4730
4731  /* We don't use optimization symbols.  */
4732  hdr->ioptMax = 0;
4733  hdr->cbOptOffset = 0;
4734
4735  /* Swap out the auxiliary type information.  */
4736  hdr->cbAuxOffset = offset;
4737  offset = ecoff_build_aux (backend, &buf, &bufend, offset);
4738  hdr->iauxMax = (offset - hdr->cbAuxOffset) / sizeof (union aux_ext);
4739
4740  /* Copy out the local strings.  */
4741  hdr->cbSsOffset = offset;
4742  offset = ecoff_build_ss (backend, &buf, &bufend, offset);
4743  hdr->issMax = offset - hdr->cbSsOffset;
4744
4745  /* We don't use relative file descriptors.  */
4746  hdr->crfd = 0;
4747  hdr->cbRfdOffset = 0;
4748
4749  /* Swap out the file descriptors.  */
4750  hdr->cbFdOffset = offset;
4751  offset = ecoff_build_fdr (backend, &buf, &bufend, offset);
4752  hdr->ifdMax = (offset - hdr->cbFdOffset) / backend->external_fdr_size;
4753
4754  /* Set up the external symbols, which are handled by the BFD back
4755     end.  */
4756  hdr->issExtMax = 0;
4757  hdr->cbSsExtOffset = 0;
4758  hdr->iextMax = 0;
4759  hdr->cbExtOffset = 0;
4760  ecoff_setup_ext ();
4761
4762  know ((offset & (backend->debug_align - 1)) == 0);
4763
4764  /* FIXME: This value should be determined from the .verstamp directive,
4765     with reasonable defaults in config files.  */
4766#ifdef TC_ALPHA
4767  hdr->vstamp = 0x030b;
4768#else
4769  hdr->vstamp = 0x020b;
4770#endif
4771
4772  *bufp = buf;
4773  return offset;
4774}
4775
4776/* Allocate a cluster of pages.  */
4777
4778#ifndef MALLOC_CHECK
4779
4780static page_type *
4781allocate_cluster (unsigned long npages)
4782{
4783  page_type *value = (page_type *) xmalloc (npages * PAGE_USIZE);
4784
4785#ifdef ECOFF_DEBUG
4786  if (debug > 3)
4787    fprintf (stderr, "\talloc\tnpages = %d, value = 0x%.8x\n", npages, value);
4788#endif
4789
4790  memset (value, 0, npages * PAGE_USIZE);
4791
4792  return value;
4793}
4794
4795static page_type *cluster_ptr = NULL;
4796static unsigned long pages_left = 0;
4797
4798#endif /* MALLOC_CHECK */
4799
4800/* Allocate one page (which is initialized to 0).  */
4801
4802static page_type *
4803allocate_page (void)
4804{
4805#ifndef MALLOC_CHECK
4806
4807  if (pages_left == 0)
4808    {
4809      pages_left = MAX_CLUSTER_PAGES;
4810      cluster_ptr = allocate_cluster (pages_left);
4811    }
4812
4813  pages_left--;
4814  return cluster_ptr++;
4815
4816#else /* MALLOC_CHECK */
4817
4818  page_type *ptr;
4819
4820  ptr = xmalloc (PAGE_USIZE);
4821  memset (ptr, 0, PAGE_USIZE);
4822  return ptr;
4823
4824#endif /* MALLOC_CHECK */
4825}
4826
4827/* Allocate scoping information.  */
4828
4829static scope_t *
4830allocate_scope (void)
4831{
4832  scope_t *ptr;
4833  static scope_t initial_scope;
4834
4835#ifndef MALLOC_CHECK
4836
4837  ptr = alloc_counts[(int) alloc_type_scope].free_list.f_scope;
4838  if (ptr != (scope_t *) NULL)
4839    alloc_counts[(int) alloc_type_scope].free_list.f_scope = ptr->free;
4840  else
4841    {
4842      int unallocated	= alloc_counts[(int) alloc_type_scope].unallocated;
4843      page_type *cur_page	= alloc_counts[(int) alloc_type_scope].cur_page;
4844
4845      if (unallocated == 0)
4846	{
4847	  unallocated = PAGE_SIZE / sizeof (scope_t);
4848	  alloc_counts[(int) alloc_type_scope].cur_page = cur_page = allocate_page ();
4849	  alloc_counts[(int) alloc_type_scope].total_pages++;
4850	}
4851
4852      ptr = &cur_page->scope[--unallocated];
4853      alloc_counts[(int) alloc_type_scope].unallocated = unallocated;
4854    }
4855
4856#else
4857
4858  ptr = XNEW (scope_t);
4859
4860#endif
4861
4862  alloc_counts[(int) alloc_type_scope].total_alloc++;
4863  *ptr = initial_scope;
4864  return ptr;
4865}
4866
4867/* Free scoping information.  */
4868
4869static void
4870free_scope (scope_t *ptr)
4871{
4872  alloc_counts[(int) alloc_type_scope].total_free++;
4873
4874#ifndef MALLOC_CHECK
4875  ptr->free = alloc_counts[(int) alloc_type_scope].free_list.f_scope;
4876  alloc_counts[(int) alloc_type_scope].free_list.f_scope = ptr;
4877#else
4878  free ((void *) ptr);
4879#endif
4880}
4881
4882/* Allocate links for pages in a virtual array.  */
4883
4884static vlinks_t *
4885allocate_vlinks (void)
4886{
4887  vlinks_t *ptr;
4888  static vlinks_t initial_vlinks;
4889
4890#ifndef MALLOC_CHECK
4891
4892  int unallocated = alloc_counts[(int) alloc_type_vlinks].unallocated;
4893  page_type *cur_page = alloc_counts[(int) alloc_type_vlinks].cur_page;
4894
4895  if (unallocated == 0)
4896    {
4897      unallocated = PAGE_SIZE / sizeof (vlinks_t);
4898      alloc_counts[(int) alloc_type_vlinks].cur_page = cur_page = allocate_page ();
4899      alloc_counts[(int) alloc_type_vlinks].total_pages++;
4900    }
4901
4902  ptr = &cur_page->vlinks[--unallocated];
4903  alloc_counts[(int) alloc_type_vlinks].unallocated = unallocated;
4904
4905#else
4906
4907  ptr = XNEW (vlinks_t);
4908
4909#endif
4910
4911  alloc_counts[(int) alloc_type_vlinks].total_alloc++;
4912  *ptr = initial_vlinks;
4913  return ptr;
4914}
4915
4916/* Allocate string hash buckets.  */
4917
4918static shash_t *
4919allocate_shash (void)
4920{
4921  shash_t *ptr;
4922  static shash_t initial_shash;
4923
4924#ifndef MALLOC_CHECK
4925
4926  int unallocated = alloc_counts[(int) alloc_type_shash].unallocated;
4927  page_type *cur_page = alloc_counts[(int) alloc_type_shash].cur_page;
4928
4929  if (unallocated == 0)
4930    {
4931      unallocated = PAGE_SIZE / sizeof (shash_t);
4932      alloc_counts[(int) alloc_type_shash].cur_page = cur_page = allocate_page ();
4933      alloc_counts[(int) alloc_type_shash].total_pages++;
4934    }
4935
4936  ptr = &cur_page->shash[--unallocated];
4937  alloc_counts[(int) alloc_type_shash].unallocated = unallocated;
4938
4939#else
4940
4941  ptr = XNEW (shash_t);
4942
4943#endif
4944
4945  alloc_counts[(int) alloc_type_shash].total_alloc++;
4946  *ptr = initial_shash;
4947  return ptr;
4948}
4949
4950/* Allocate type hash buckets.  */
4951
4952static thash_t *
4953allocate_thash (void)
4954{
4955  thash_t *ptr;
4956  static thash_t initial_thash;
4957
4958#ifndef MALLOC_CHECK
4959
4960  int unallocated = alloc_counts[(int) alloc_type_thash].unallocated;
4961  page_type *cur_page = alloc_counts[(int) alloc_type_thash].cur_page;
4962
4963  if (unallocated == 0)
4964    {
4965      unallocated = PAGE_SIZE / sizeof (thash_t);
4966      alloc_counts[(int) alloc_type_thash].cur_page = cur_page = allocate_page ();
4967      alloc_counts[(int) alloc_type_thash].total_pages++;
4968    }
4969
4970  ptr = &cur_page->thash[--unallocated];
4971  alloc_counts[(int) alloc_type_thash].unallocated = unallocated;
4972
4973#else
4974
4975  ptr = XNEW (thash_t);
4976
4977#endif
4978
4979  alloc_counts[(int) alloc_type_thash].total_alloc++;
4980  *ptr = initial_thash;
4981  return ptr;
4982}
4983
4984/* Allocate structure, union, or enum tag information.  */
4985
4986static tag_t *
4987allocate_tag (void)
4988{
4989  tag_t *ptr;
4990  static tag_t initial_tag;
4991
4992#ifndef MALLOC_CHECK
4993
4994  ptr = alloc_counts[(int) alloc_type_tag].free_list.f_tag;
4995  if (ptr != (tag_t *) NULL)
4996    alloc_counts[(int) alloc_type_tag].free_list.f_tag = ptr->free;
4997  else
4998    {
4999      int unallocated = alloc_counts[(int) alloc_type_tag].unallocated;
5000      page_type *cur_page = alloc_counts[(int) alloc_type_tag].cur_page;
5001
5002      if (unallocated == 0)
5003	{
5004	  unallocated = PAGE_SIZE / sizeof (tag_t);
5005	  alloc_counts[(int) alloc_type_tag].cur_page = cur_page = allocate_page ();
5006	  alloc_counts[(int) alloc_type_tag].total_pages++;
5007	}
5008
5009      ptr = &cur_page->tag[--unallocated];
5010      alloc_counts[(int) alloc_type_tag].unallocated = unallocated;
5011    }
5012
5013#else
5014
5015  ptr = XNEW (tag_t);
5016
5017#endif
5018
5019  alloc_counts[(int) alloc_type_tag].total_alloc++;
5020  *ptr = initial_tag;
5021  return ptr;
5022}
5023
5024/* Free scoping information.  */
5025
5026static void
5027free_tag (tag_t *ptr)
5028{
5029  alloc_counts[(int) alloc_type_tag].total_free++;
5030
5031#ifndef MALLOC_CHECK
5032  ptr->free = alloc_counts[(int) alloc_type_tag].free_list.f_tag;
5033  alloc_counts[(int) alloc_type_tag].free_list.f_tag = ptr;
5034#else
5035  free ((PTR_T) ptr);
5036#endif
5037}
5038
5039/* Allocate forward reference to a yet unknown tag.  */
5040
5041static forward_t *
5042allocate_forward (void)
5043{
5044  forward_t *ptr;
5045  static forward_t initial_forward;
5046
5047#ifndef MALLOC_CHECK
5048
5049  int unallocated = alloc_counts[(int) alloc_type_forward].unallocated;
5050  page_type *cur_page = alloc_counts[(int) alloc_type_forward].cur_page;
5051
5052  if (unallocated == 0)
5053    {
5054      unallocated = PAGE_SIZE / sizeof (forward_t);
5055      alloc_counts[(int) alloc_type_forward].cur_page = cur_page = allocate_page ();
5056      alloc_counts[(int) alloc_type_forward].total_pages++;
5057    }
5058
5059  ptr = &cur_page->forward[--unallocated];
5060  alloc_counts[(int) alloc_type_forward].unallocated = unallocated;
5061
5062#else
5063
5064  ptr = XNEW (forward_t);
5065
5066#endif
5067
5068  alloc_counts[(int) alloc_type_forward].total_alloc++;
5069  *ptr = initial_forward;
5070  return ptr;
5071}
5072
5073/* Allocate head of type hash list.  */
5074
5075static thead_t *
5076allocate_thead (void)
5077{
5078  thead_t *ptr;
5079  static thead_t initial_thead;
5080
5081#ifndef MALLOC_CHECK
5082
5083  ptr = alloc_counts[(int) alloc_type_thead].free_list.f_thead;
5084  if (ptr != (thead_t *) NULL)
5085    alloc_counts[(int) alloc_type_thead].free_list.f_thead = ptr->free;
5086  else
5087    {
5088      int unallocated = alloc_counts[(int) alloc_type_thead].unallocated;
5089      page_type *cur_page = alloc_counts[(int) alloc_type_thead].cur_page;
5090
5091      if (unallocated == 0)
5092	{
5093	  unallocated = PAGE_SIZE / sizeof (thead_t);
5094	  alloc_counts[(int) alloc_type_thead].cur_page = cur_page = allocate_page ();
5095	  alloc_counts[(int) alloc_type_thead].total_pages++;
5096	}
5097
5098      ptr = &cur_page->thead[--unallocated];
5099      alloc_counts[(int) alloc_type_thead].unallocated = unallocated;
5100    }
5101
5102#else
5103
5104  ptr = XNEW (thead_t);
5105
5106#endif
5107
5108  alloc_counts[(int) alloc_type_thead].total_alloc++;
5109  *ptr = initial_thead;
5110  return ptr;
5111}
5112
5113/* Free scoping information.  */
5114
5115static void
5116free_thead (thead_t *ptr)
5117{
5118  alloc_counts[(int) alloc_type_thead].total_free++;
5119
5120#ifndef MALLOC_CHECK
5121  ptr->free = (thead_t *) alloc_counts[(int) alloc_type_thead].free_list.f_thead;
5122  alloc_counts[(int) alloc_type_thead].free_list.f_thead = ptr;
5123#else
5124  free ((PTR_T) ptr);
5125#endif
5126}
5127
5128static lineno_list_t *
5129allocate_lineno_list (void)
5130{
5131  lineno_list_t *ptr;
5132  static lineno_list_t initial_lineno_list;
5133
5134#ifndef MALLOC_CHECK
5135
5136  int unallocated = alloc_counts[(int) alloc_type_lineno].unallocated;
5137  page_type *cur_page = alloc_counts[(int) alloc_type_lineno].cur_page;
5138
5139  if (unallocated == 0)
5140    {
5141      unallocated = PAGE_SIZE / sizeof (lineno_list_t);
5142      alloc_counts[(int) alloc_type_lineno].cur_page = cur_page = allocate_page ();
5143      alloc_counts[(int) alloc_type_lineno].total_pages++;
5144    }
5145
5146  ptr = &cur_page->lineno[--unallocated];
5147  alloc_counts[(int) alloc_type_lineno].unallocated = unallocated;
5148
5149#else
5150
5151  ptr = XNEW (lineno_list_t);
5152
5153#endif
5154
5155  alloc_counts[(int) alloc_type_lineno].total_alloc++;
5156  *ptr = initial_lineno_list;
5157  return ptr;
5158}
5159
5160void
5161ecoff_set_gp_prolog_size (int sz)
5162{
5163  if (cur_proc_ptr == 0)
5164    return;
5165
5166  cur_proc_ptr->pdr.gp_prologue = sz;
5167  if (cur_proc_ptr->pdr.gp_prologue != sz)
5168    {
5169      as_warn (_("GP prologue size exceeds field size, using 0 instead"));
5170      cur_proc_ptr->pdr.gp_prologue = 0;
5171    }
5172
5173  cur_proc_ptr->pdr.gp_used = 1;
5174}
5175
5176int
5177ecoff_no_current_file (void)
5178{
5179  return cur_file_ptr == (efdr_t *) NULL;
5180}
5181
5182void
5183ecoff_generate_asm_lineno (void)
5184{
5185  unsigned int lineno;
5186  const char *filename;
5187  lineno_list_t *list;
5188
5189  filename = as_where (&lineno);
5190
5191  if (current_stabs_filename == (char *) NULL
5192      || filename_cmp (current_stabs_filename, filename))
5193    add_file (filename, 0, 1);
5194
5195  list = allocate_lineno_list ();
5196
5197  list->next = (lineno_list_t *) NULL;
5198  list->file = cur_file_ptr;
5199  list->proc = cur_proc_ptr;
5200  list->frag = frag_now;
5201  list->paddr = frag_now_fix ();
5202  list->lineno = lineno;
5203
5204  /* We don't want to merge files which have line numbers.  */
5205  cur_file_ptr->fdr.fMerge = 0;
5206
5207  /* A .loc directive will sometimes appear before a .ent directive,
5208     which means that cur_proc_ptr will be NULL here.  Arrange to
5209     patch this up.  */
5210  if (cur_proc_ptr == (proc_t *) NULL)
5211    {
5212      lineno_list_t **pl;
5213
5214      pl = &noproc_lineno;
5215      while (*pl != (lineno_list_t *) NULL)
5216	pl = &(*pl)->next;
5217      *pl = list;
5218    }
5219  else
5220    {
5221      last_lineno = list;
5222      *last_lineno_ptr = list;
5223      last_lineno_ptr = &list->next;
5224    }
5225}
5226
5227#else
5228
5229void
5230ecoff_generate_asm_lineno (void)
5231{
5232}
5233
5234#endif /* ECOFF_DEBUGGING */
5235