Pristine Ack-5.5
[Ack-5.5.git] / util / led / output.c
1 /*
2  * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3  * See the copyright notice in the ACK home directory, in the file "Copyright".
4  */
5 #ifndef lint
6 static char rcsid[] = "$Id: output.c,v 3.6 1994/06/24 10:35:02 ceriel Exp $";
7 #endif
8
9 #include <out.h>
10 #include "const.h"
11 #include "memory.h"
12
13 static                  generate_section_names();
14
15 extern struct outhead   outhead;
16 extern bool             incore;
17 extern int              flagword;
18
19 /*
20  * We have counted all relocation structs but we know only now if
21  * these must be emitted.We add all names here,unless the -s(trip)
22  * flag was given.
23  * If this flag is given we don't need the string table either.
24  */
25 beginoutput()
26 {
27         extern unsigned short   NLocals, NGlobals;
28         extern long     NLChars, NGChars;
29         extern char     *outputname;
30
31         if (! wr_open(outputname)) {
32                 fatal("can't create %s", outputname);
33         }
34         if (incore)
35                 generate_section_names();
36
37         if (!(flagword & (CFLAG|RFLAG)))
38                 outhead.oh_nrelo = (unsigned short)0;
39         if (flagword & SFLAG) {
40                 outhead.oh_nname = (unsigned short)0;
41                 outhead.oh_nchar = (long)0;
42         } else {
43                 outhead.oh_nname = NLocals + NGlobals + outhead.oh_nsect;
44                 outhead.oh_nchar = NLChars + NGChars;
45         }
46         if (!incore)
47                 begin_write();
48 }
49
50 /*
51  * Generate names for all sections and put them after the global names.
52  * Section names are used for relocation.
53  */
54 static
55 generate_section_names()
56 {
57         register struct outname *name;
58         register int            sectindex;
59         register long           size;
60         extern struct outsect   outsect[];
61         extern char             *core_alloc();
62
63         size = (long)outhead.oh_nsect * sizeof(struct outname); 
64         name = (struct outname *)core_alloc(ALLOGLOB, size);
65         if (name == (struct outname *)0)
66                 return;
67
68         for (sectindex = 0; sectindex < outhead.oh_nsect; sectindex++, name++) {
69                 name->on_foff = (long)0;        /* No string name. */
70                 name->on_type = (S_MIN + sectindex) | S_SCT;
71                 name->on_desc = (unsigned short)0;
72                 name->on_valu = outsect[sectindex].os_base;
73         }
74 }
75
76 /*
77  * If we didn't keep the whole output file in core, most of it has been
78  * written out, and we just finish that.
79  * If we did, we write out our pieces of core.
80  */
81 endoutput()
82 {
83         if (!incore) {
84                 if (!(flagword & SFLAG))
85                         end_write();
86         } else {
87                 write_bytes();
88         }
89         wr_close();
90 }