Added support to generate aomf51 file. (Basic Intel support, no Keil
[fw/sdcc] / as / mcs51 / lkmain.c
1 /* lkmain.c */
2
3 /*
4  * (C) Copyright 1989-1995
5  * All Rights Reserved
6  *
7  * Alan R. Baldwin
8  * 721 Berkeley St.
9  * Kent, Ohio  44240
10  *
11  * 31-Oct-97 JLH:
12  *           - add jflag and jfp to control NoICE output file genration
13  *  3-Nov-97 JLH: 
14  *           - use a_type == 0 as "virgin area" flag: set == 1 if -b
15  */
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include "aslink.h"
21
22 /* yuck - but including unistd.h causes problems on Cygwin by redefining
23  * Addr_T.
24  */
25 extern int unlink(const char *);
26
27 /*)Module       lkmain.c
28  *
29  *      The module lkmain.c contains the functions which
30  *      (1) input the linker options, parameters, and specifications
31  *      (2) perform a two pass link
32  *      (3) produce the appropriate linked data output and/or
33  *          link map file and/or relocated listing files.
34  *
35  *      lkmain.c contains the following functions:
36  *              FILE *  afile(fn,ft,wf)
37  *              VOID    bassav()
38  *              VOID    gblsav()
39   *             VOID    link_main()
40  *              VOID    lkexit()
41  *              VOID    main(argc,argv)
42  *              VOID    map()
43  *              int     parse()
44  *              VOID    setbas()
45  *              VOID    setgbl()
46  *              VOID    usage()
47  *
48  *      lkmain.c contains the following local variables:
49  *              char *  usetext[]       array of pointers to the
50  *                                      command option tect lines
51  *
52  */
53
54 /*JCF:  Creates some of the default areas so they are allocated in the right order.*/
55 void Areas51 (void)
56 {
57         char * rel[]={
58                 "XH",
59                 "H 7 areas 0 global symbols",
60                 "A _CODE size 0 flags 0",               /*Each .rel has one, so...*/
61                 "A REG_BANK_0 size 0 flags 4",  /*Register banks are overlayable*/
62                 "A REG_BANK_1 size 0 flags 4",
63                 "A REG_BANK_2 size 0 flags 4",
64                 "A REG_BANK_3 size 0 flags 4",
65                 "A BSEG size 0 flags 80",               /*BSEG must be just before BITS*/
66                 "A BSEG_BYTES size 0 flags 0",  /*Size will be obtained from BSEG in lnkarea()*/
67                 ""
68         };
69         int j;
70
71         for (j=0; rel[j][0]!=0; j++)
72         {
73                 ip=rel[j];
74                 link_main();
75         }
76         
77         /*Set the start address of the default areas:*/
78         for(ap=areap; ap; ap=ap->a_ap)
79         {
80                 /**/ if (!strcmp(ap->a_id, "REG_BANK_0")) { ap->a_addr=0x00; ap->a_type=1; }
81                 else if (!strcmp(ap->a_id, "REG_BANK_1")) { ap->a_addr=0x08; ap->a_type=1; }
82                 else if (!strcmp(ap->a_id, "REG_BANK_2")) { ap->a_addr=0x10; ap->a_type=1; }
83                 else if (!strcmp(ap->a_id, "REG_BANK_3")) { ap->a_addr=0x18; ap->a_type=1; }
84                 else if (!strcmp(ap->a_id, "BSEG_BYTES")) { ap->a_addr=0x20; ap->a_type=1; }
85         }
86 }
87
88 /*)Function     VOID    main(argc,argv)
89  *
90  *              int     argc            number of command line arguments + 1
91  *              char *  argv[]          array of pointers to the command line
92  *                                      arguments
93  *
94  *      The function main() evaluates the command line arguments to
95  *      determine if the linker parameters are to input through 'stdin'
96  *      or read from a command file.  The functiond getline() and parse()
97  *      are to input and evaluate the linker parameters.  The linking process
98  *      proceeds by making the first pass through each .rel file in the order
99  *      presented to the linker.  At the end of the first pass the setbase(),
100  *      lnkarea(), setgbl(), and symdef() functions are called to evaluate
101  *      the base address terms, link all areas, define global variables,
102  *      and look for undefined symbols.  Following these routines a linker
103  *      map file may be produced and the linker output files may be opened.
104  *      The second pass through the .rel files will output the linked data
105  *      in one of the four supported formats.
106  *
107  *      local variables:
108  *              char *  p               pointer to an argument string
109  *              int     c               character from argument string
110  *              int     i               loop counter
111  *
112  *      global variables:
113  *                                              text line in ib[]
114  *              lfile   *cfp            The pointer *cfp points to the
115  *                                              current lfile structure
116  *              char    ctype[]         array of character types, one per
117  *                                              ASCII character
118  *              lfile   *filep                  The pointer *filep points to the
119  *                                              beginning of a linked list of
120  *                                              lfile structures.
121  *              head    *hp             Pointer to the current
122  *                                              head structure
123  *              char    ib[NINPUT]      .rel file text line
124  *              char    *ip             pointer into the .rel file
125  *              lfile   *linkp          pointer to first lfile structure
126  *                                              containing an input .rel file
127  *                                              specification
128  *              int     lkerr           error flag
129  *              int     mflag           Map output flag
130  *              int     oflag           Output file type flag
131  *              FILE    *ofp            Output file handle
132  *                                              for word formats
133  *              FILE    *ofph           Output file handle
134  *                                              for high byte format
135  *              FILE    *ofpl           Output file handle
136  *                                              for low byte format
137  *              int     pass            linker pass number
138  *              int     pflag           print linker command file flag
139  *              int     radix           current number conversion radix
140  *              FILE    *sfp            The file handle sfp points to the
141  *                                              currently open file
142  *              lfile   *startp         asmlnk startup file structure
143  *              FILE *  stdin           c_library
144  *              FILE *  stdout          c_library
145  *
146  *      functions called:
147  *              FILE *  afile()         lkmain.c
148  *              int     fclose()        c_library
149  *              int     fprintf()       c_library
150  *              int     getline()       lklex.c
151  *              VOID    library()       lklibr.c
152  *              VOID    link_main()     lkmain.c
153  *              VOID    lkexit()        lkmain.c
154  *              VOID    lnkarea()       lkarea.c
155  *              VOID    map()           lkmain.c
156  *              VOID    new()           lksym.c
157  *              int     parse()         lkmain.c
158  *              VOID    reloc()         lkreloc.c
159  *              VOID    search()        lklibr.c
160  *              VOID    setbas()        lkmain.c
161  *              VOID    setgbl()        lkmain.c
162  *              VOID    symdef()        lksym.c
163  *              VOID    usage()         lkmain.c
164  *
165  *      side effects:
166  *              Completion of main() completes the linking process
167  *              and may produce a map file (.map) and/or a linked
168  *              data files (.ihx or .s19) and/or one or more
169  *              relocated listing files (.rst).
170  */
171
172 int
173 main(argc, argv)
174 char *argv[];
175 {
176         register char *p;
177         register int c, i;
178
179         startp = (struct lfile *) new (sizeof (struct lfile));
180
181         pflag = 1;
182         for (i=1; i<argc; ++i) {
183                 p = argv[i];
184                 if (*p == '-') {
185                         while (ctype[c = *(++p)] & LETTER) {
186                                 switch(c) {
187
188                                 case 'c':
189                                 case 'C':
190                                         startp->f_type = F_STD;
191                                         break;
192
193                                 case 'f':
194                                 case 'F':
195                                         startp->f_type = F_LNK;
196                                         break;
197         
198                                 case 'n':
199                                 case 'N':
200                                         pflag = 0;
201                                         break;
202
203                                 case 'p':
204                                 case 'P':
205                                         pflag = 1;
206                                         break;
207
208                                 default:
209                                         usage();
210                                 }
211                         }
212                 } else {
213                         if (startp->f_type == F_LNK) {
214                                 startp->f_idp = p;
215                         }
216                 }
217         }
218        if (startp->f_type == 0)
219                 usage();
220         if (startp->f_type == F_LNK && startp->f_idp == NULL)
221                 usage();
222
223         cfp = NULL;
224         sfp = NULL;
225         filep = startp;
226         while (1) {
227                 ip = ib;
228                 if (getline() == 0)
229                         break;
230                 if (pflag && sfp != stdin)
231                         fprintf(stdout, "%s\n", ip);
232                if (*ip == '\0' || parse())
233                         break;
234         }
235
236         if (sfp) {
237             fclose(sfp);
238             sfp = NULL;
239         }
240
241         if (linkp == NULL)
242                 usage();
243
244         syminit();
245         
246         if (dflag){
247             //dfp = afile("temp", "cdb", 1);
248                 dfp = afile(linkp->f_idp,"cdb",1); //JCF: Nov 30, 2002
249             if (dfp == NULL) 
250                 lkexit(1);
251         }
252
253         for (pass=0; pass<2; ++pass) {
254                 cfp = NULL;
255                 sfp = NULL;
256                 filep = linkp;
257                 hp = NULL;
258                 radix = 10;
259                 
260                 Areas51(); /*JCF: Create the default 8051 areas in the right order*/
261
262                 while (getline()) {
263                         ip = ib;
264
265                         /* pass any "magic comments" to NoICE output */
266                         if ((ip[0] == ';') && (ip[1] == '!') && jfp) {
267                                 fprintf( jfp, "%s\n", &ip[2] );
268                         }
269                         link_main();
270                 }
271                 if (pass == 0) {
272                         /*
273                          * Search libraries for global symbols
274                          */
275                         search();
276                         /*
277                          * Set area base addresses.
278                          */
279                         setbas();
280                         /*
281                          * Link all area addresses.
282                          */
283                         lnkarea();
284                         /*
285                          * Process global definitions.
286                          */
287                         setgbl();
288                         /*
289                          * Check for undefined globals.
290                          */
291                         symdef(stderr);
292
293                         /* Open NoICE output file if requested */
294                         if (jflag) {
295                                 jfp = afile(linkp->f_idp, "NOI", 1);
296                                 if (jfp == NULL) {
297                                         lkexit(1);
298                                 }
299                         }
300
301                         /*
302                          * Output Link Map if requested,
303                          * or if NoICE output requested (since NoICE
304                          * file is generated in part by map() processing)
305                          */
306                         if (mflag || jflag)
307                                 map();
308
309                         if (sflag) /*JCF: memory usage summary output*/
310                                 if(summary(areap))lkexit(1);
311
312                         if (iram_size)
313                                 iramcheck();
314
315                         /*
316                          * Open output file
317                          */
318                         if (oflag == 1) {
319                                 ofp = afile(linkp->f_idp, "ihx", 1);
320                                 if (ofp == NULL) {
321                                         lkexit(1);
322                                 }
323                                 /* include NoICE command to load hex file */
324                                 if (jfp) fprintf( jfp, "LOAD %s.IHX\n", linkp->f_idp );
325
326                         } else
327                         if (oflag == 2) {
328                                 ofp = afile(linkp->f_idp, "S19", 1);
329                                 if (ofp == NULL) {
330                                         lkexit(1);
331                                 }
332                                 /* include NoICE command to load hex file */
333                                 if (jfp) fprintf( jfp, "LOAD %s.S19\n", linkp->f_idp );
334                         }
335                 } else {
336                         /*
337                          * Link in library files
338                          */
339                         library();
340                         reloc('E');
341                 }
342         }
343         //JCF:
344         CreateAOMF51();
345
346         lkexit(lkerr);
347         return 0;
348 }
349
350 /*)Function     VOID    lkexit(i)
351  *
352  *                      int     i       exit code
353  *
354  *      The function lkexit() explicitly closes all open
355  *      files and then terminates the program.
356  *
357  *      local variables:
358  *              none
359  *
360  *      global variables:
361  *              FILE *  mfp             file handle for .map
362  *              FILE *  ofp             file handle for .ihx/.s19
363  *              FILE *  rfp             file hanlde for .rst
364  *              FILE *  sfp             file handle for .rel
365  *              FILE *  tfp             file handle for .lst
366  *
367  *      functions called:
368  *              int     fclose()        c_library
369  *              VOID    exit()          c_library
370  *
371  *      side effects:
372  *              All files closed. Program terminates.
373  */
374
375 VOID
376 lkexit(i)
377 int i;
378 {
379         if (mfp != NULL) fclose(mfp);
380         if (jfp != NULL) fclose(jfp);
381         if (ofp != NULL) fclose(ofp);
382         if (rfp != NULL) fclose(rfp);
383         if (sfp != NULL) fclose(sfp);
384         if (tfp != NULL) fclose(tfp);
385         if (dfp != NULL) fclose(dfp);
386         /*if (dfp != NULL)
387             FILE *xfp = afile(linkp->f_idp,"cdb",1);
388             dfp = freopen("temp.cdb","r",dfp);
389             copyfile(xfp,dfp);
390             fclose(xfp);
391             fclose(dfp);
392             unlink("temp.cdb");
393         }*/
394         exit(i);
395 }
396
397 /*)Function     link_main()
398  *
399  *      The function link_main() evaluates the directives for each line of
400  *      text read from the .rel file(s).  The valid directives processed
401  *      are:
402  *              X, D, Q, H, M, A, S, T, R, and P.
403  *
404  *      local variables:
405  *              int     c               first non blank character of a line
406  *
407  *      global variables:
408  *              head    *headp          The pointer to the first
409  *                                              head structure of a linked list
410  *              head    *hp             Pointer to the current
411  *                                              head structure
412  *              int     pass            linker pass number
413  *              int     radix           current number conversion radix
414  *
415  *      functions called:
416  *              char    endline()       lklex.c
417  *              VOID    module()        lkhead.c
418  *              VOID    newarea()       lkarea.c
419  *              VOID    newhead()       lkhead.c
420  *              sym *   newsym()        lksym.c
421  *              VOID    reloc()         lkreloc.c
422  *
423  *      side effects:
424  *              Head, area, and symbol structures are created and
425  *              the radix is set as the .rel file(s) are read.
426  */
427
428 VOID
429 link_main()
430 {
431         register int c;
432
433         if ((c=endline()) == 0) { return; }
434         switch (c) {
435
436         case 'X':
437                 radix = 16;
438                 break;
439
440         case 'D':
441                 radix = 10;
442                 break;
443
444         case 'Q':
445                 radix = 8;
446                 break;
447
448         case 'H':
449                 if (pass == 0) {
450                         newhead();
451                 } else {
452                         if (hp == 0) {
453                                 hp = headp;
454                         } else {
455                                 hp = hp->h_hp;
456                         }
457                 }
458                 sdp.s_area = NULL;
459                 sdp.s_areax = NULL;
460                 sdp.s_addr = 0;
461                 // jwk lastExtendedAddress = -1;
462                 break;
463
464         case 'M':
465                 if (pass == 0)
466                         module();
467                 break;
468
469         case 'A':
470                 if (pass == 0)
471                         newarea();
472                 if (sdp.s_area == NULL) {
473                         sdp.s_area = areap;
474                         sdp.s_areax = areap->a_axp;
475                         sdp.s_addr = 0;
476                 }
477                 break;
478
479         case 'S':
480                 if (pass == 0)
481                         newsym();
482                 break;
483
484         case 'T':
485         case 'R':
486         case 'P':
487                 if (pass == 0)
488                         break;
489                 reloc(c);
490                 break;
491
492         default:
493                 break;
494         }
495         if (c == 'X' || c == 'D' || c == 'Q') {
496                 if ((c = get()) == 'H') {
497                         hilo = 1;
498                 } else
499                 if (c == 'L') {
500                         hilo = 0;
501                 }
502         }
503 }
504
505
506 /*)Function     VOID    map()
507  *
508  *      The function map() opens the output map file and calls the various
509  *      routines to
510  *      (1) output the variables in each area,
511  *      (2) list the files processed with module names,
512  *      (3) list the libraries file processed,
513  *      (4) list base address definitions,
514  *      (5) list global variable definitions, and
515  *      (6) list any undefined variables.
516  *
517  *      local variables:
518  *              int             i               counter
519  *              head *  hdp             pointer to head structure
520  *              lbfile *lbfh            pointer to library file structure
521  *
522  *      global variables:
523  *              area    *ap             Pointer to the current
524  *                                              area structure
525  *              area    *areap          The pointer to the first
526  *                                              area structure of a linked list
527  *              base    *basep          The pointer to the first
528  *                                              base structure
529  *              base    *bsp            Pointer to the current
530  *                                              base structure
531  *              lfile   *filep                  The pointer *filep points to the
532  *                                              beginning of a linked list of
533  *                                              lfile structures.
534  *              globl   *globlp         The pointer to the first
535  *                                              globl structure
536  *              globl   *gsp            Pointer to the current
537  *                                              globl structure
538  *              head    *headp          The pointer to the first
539  *                                              head structure of a linked list
540  *              lbfile  *lbfhead        The pointer to the first
541  *                                      lbfile structure of a linked list
542  *              lfile   *linkp          pointer to first lfile structure
543  *                                              containing an input REL file
544  *                                              specification
545  *              int     lop             current line number on page
546  *              FILE    *mfp            Map output file handle
547  *              int     page            current page number
548  *
549  *      functions called:
550  *              FILE *  afile()         lkmain.c
551  *              int     fprintf()       c_library
552  *              VOID    lkexit()        lkmain.c
553  *              VOID    lstarea()       lklist.c
554  *              VOID    newpag()        lklist.c
555  *              VOID    symdef()        lksym.c
556  *
557  *      side effects:
558  *              The map file is created.
559  */
560
561 VOID
562 map()
563 {
564         register int i;
565         register struct head *hdp;
566         register struct lbfile *lbfh;
567
568         /*
569          * Open Map File
570          */
571         mfp = afile(linkp->f_idp, "map", 1);
572         if (mfp == NULL) {
573                 lkexit(1);
574         }
575
576         /*
577          * Output Map Area Lists
578          */
579         page = 0;
580         lop  = NLPP;
581         ap = areap;
582         while (ap) {
583                 lstarea(ap);
584                 ap = ap->a_ap;
585         }
586         /*
587          * List Linked Files
588          */
589         newpag(mfp);
590         fprintf(mfp, "\nFiles Linked      [ module(s) ]\n\n");
591         hdp = headp;
592         filep = linkp;
593         while (filep) {
594                 fprintf(mfp, "%-16s", filep->f_idp);
595                 i = 0;
596                 while ((hdp != NULL) && (hdp->h_lfile == filep)) {
597                         if (i % 5) {
598                             fprintf(mfp, ", %8.8s", hdp->m_id);
599                         } else {
600                             if (i) {
601                                 fprintf(mfp, ",\n%20s%8.8s", "", hdp->m_id);
602                             } else {
603                                 fprintf(mfp, "  [ %8.8s", hdp->m_id);
604                             }
605                         }
606                         hdp = hdp->h_hp;
607                         i++;
608                 }
609                 if (i)
610                         fprintf(mfp, " ]");
611                 fprintf(mfp, "\n");
612                 filep = filep->f_flp;
613         }
614         /*
615          * List Linked Libraries
616          */
617         if (lbfhead != NULL) {
618                 fprintf(mfp,
619         "\nLibraries Linked                    [   object  file   ]\n\n");
620                 for (lbfh=lbfhead; lbfh; lbfh=lbfh->next) {
621                         fprintf(mfp, "%-32s    [ %16.16s ]\n",
622                                 lbfh->libspc, lbfh->relfil);
623                 }
624                 fprintf(mfp, "\n");
625         }
626         /*
627          * List Base Address Definitions
628          */
629         if (basep) {
630                 newpag(mfp);
631                 fprintf(mfp, "\nUser Base Address Definitions\n\n");
632                 bsp = basep;
633                 while (bsp) {
634                         fprintf(mfp, "%s\n", bsp->b_strp);
635                         bsp = bsp->b_base;
636                 }
637         }
638         /*
639          * List Global Definitions
640          */
641         if (globlp) {
642                 newpag(mfp);
643                 fprintf(mfp, "\nUser Global Definitions\n\n");
644                 gsp = globlp;
645                 while (gsp) {
646                         fprintf(mfp, "%s\n", gsp->g_strp);
647                         gsp = gsp->g_globl;
648                 }
649         }
650         fprintf(mfp, "\n\f");
651         symdef(mfp);
652 }
653
654 /*)Function     int     parse()
655  *
656  *      The function parse() evaluates all command line or file input
657  *      linker directives and updates the appropriate variables.
658  *
659  *      local variables:
660  *              int     c               character value
661  *              char    fid[]           file id string
662  *
663  *      global variables:
664  *              char    ctype[]         array of character types, one per
665  *                                              ASCII character
666  *              lfile   *lfp            pointer to current lfile structure
667  *                                              being processed by parse()
668  *              lfile   *linkp          pointer to first lfile structure
669  *                                              containing an input REL file
670  *                                              specification
671  *              int     mflag           Map output flag
672  *              int     oflag           Output file type flag
673  *              int     pflag           print linker command file flag
674  *              FILE *  stderr          c_library
675  *              int     uflag           Relocated listing flag
676  *              int     xflag           Map file radix type flag
677  *
678  *      Functions called:
679  *              VOID    addlib()        lklibr.c
680  *              VOID    addpath()       lklibr.c
681  *              VOID    bassav()        lkmain.c
682  *              int     fprintf()       c_library
683  *              VOID    gblsav()        lkmain.c
684  *              VOID    getfid()        lklex.c
685  *              char    getnb()         lklex.c
686  *              VOID    lkexit()        lkmain.c
687  *              char *  strcpy()        c_library
688  *              int     strlen()        c_library
689  *
690  *      side effects:
691  *              Various linker flags are updated and the linked
692  *              structure lfile is created.
693  */
694
695 int
696 parse()
697 {
698         register int c;
699         char fid[NINPUT];
700
701         while ((c = getnb()) != 0) {
702                 if ( c == '-') {
703                         while (ctype[c=get()] & LETTER) {
704                                 switch(c) {
705
706                                 case 'i':
707                                 case 'I':
708                                         oflag = 1;
709                                         break;
710
711                                 case 's':
712                                 case 'S':
713                                         oflag = 2;
714                                         break;
715
716                                 case 'm':
717                                 case 'M':
718                                         ++mflag;
719                                         break;
720
721                                 case 'y': /*JCF: memory usage summary output*/
722                                 case 'Y':
723                                         ++sflag;
724                                         break;
725
726                                 case 'j':
727                                 case 'J':
728                                         jflag = 1;
729                                         break;
730
731                                 case 'u':
732                                 case 'U':
733                                         uflag = 1;
734                                         break;
735                                 case 'r':
736                                 case 'R':
737                                         rflag = 1;
738                                         break;
739                                 case 'x':
740                                 case 'X':
741                                         xflag = 0;
742                                         break;
743
744                                 case 'q':
745                                 case 'Q':
746                                         xflag = 1;
747                                         break;
748
749                                 case 'd':
750                                 case 'D':
751                                         xflag = 2;
752                                         break;
753
754                                 case 'e':
755                                 case 'E':
756                                         return(1);
757
758                                 case 'n':
759                                 case 'N':
760                                         pflag = 0;
761                                         break;
762
763                                 case 'p':
764                                 case 'P':
765                                         pflag = 1;
766                                         break;
767
768                                 case 'b':
769                                 case 'B':
770                                         bassav();
771                                         return(0);
772
773                                 case 'g':
774                                 case 'G':
775                                         gblsav();
776                                         return(0);
777
778                                 case 'k':
779                                 case 'K':
780                                         addpath();
781                                         return(0);
782
783                                 case 'l':
784                                 case 'L':
785                                         addlib();
786                                         return(0);
787
788                                 case 'a':
789                                 case 'A':
790                                         iramsav();
791                                         return(0);
792
793                                 case 'z':
794                                 case 'Z':
795                                         dflag = 1;                                      
796                                         return(0);
797                                 default:
798                                         fprintf(stderr, "Invalid option\n");
799                                         lkexit(1);
800                                 }
801                         }
802                 } else
803                if (ctype[c] & ILL) {
804                        fprintf(stderr, "Invalid input");
805                        lkexit(1);
806                } else {
807                         if (linkp == NULL) {
808                                 linkp = (struct lfile *)
809                                         new (sizeof (struct lfile));
810                                 lfp = linkp;
811                         } else {
812                                 lfp->f_flp = (struct lfile *)
813                                                 new (sizeof (struct lfile));
814                                 lfp = lfp->f_flp;
815                         }
816                         getfid(fid, c);
817                         lfp->f_idp = (char *) new (strlen(fid)+1);
818                         strcpy(lfp->f_idp, fid);
819                         lfp->f_type = F_REL;
820                 }
821         }
822         return(0);
823 }
824
825 /*)Function     VOID    bassav()
826  *
827  *      The function bassav() creates a linked structure containing
828  *      the base address strings input to the linker.
829  *
830  *      local variables:
831  *              none
832  *
833  *      global variables:
834  *              base    *basep          The pointer to the first
835  *                                              base structure
836  *              base    *bsp            Pointer to the current
837  *                                              base structure
838  *              char    *ip             pointer into the REL file
839  *                                              text line in ib[]
840  *
841  *       functions called:
842  *              char    getnb()         lklex.c
843  *              VOID *  new()           lksym.c
844  *              int     strlen()        c_library
845  *              char *  strcpy()        c_library
846  *              VOID    unget()         lklex.c
847  *
848  *      side effects:
849  *              The basep structure is created.
850  */
851
852 VOID
853 bassav()
854 {
855         if (basep == NULL) {
856                 basep = (struct base *)
857                         new (sizeof (struct base));
858                 bsp = basep;
859         } else {
860                 bsp->b_base = (struct base *)
861                                 new (sizeof (struct base));
862                 bsp = bsp->b_base;
863         }
864         unget(getnb());
865         bsp->b_strp = (char *) new (strlen(ip)+1);
866         strcpy(bsp->b_strp, ip);
867 }
868
869 /*)Function     VOID    setbas()
870  *
871  *      The function setbas() scans the base address lines in hte
872  *      basep structure, evaluates the arguments, and sets beginning
873  *      address of the specified areas.
874  *
875  *      local variables:
876  *              int     v               expression value
877  *              char    id[]            base id string
878  *
879  *      global variables:
880  *              area    *ap             Pointer to the current
881  *                                              area structure
882  *              area    *areap          The pointer to the first
883  *                                              area structure of a linked list
884  *              base    *basep          The pointer to the first
885  *                                              base structure
886  *              base    *bsp            Pointer to the current
887  *                                              base structure
888  *              char    *ip             pointer into the REL file
889  *                                              text line in ib[]
890  *              int     lkerr           error flag
891  *
892  *       functions called:
893  *              Addr_T  expr()          lkeval.c
894  *              int     fprintf()       c_library
895  *              VOID    getid()         lklex.c
896  *              char    getnb()         lklex.c
897  *              int     symeq()         lksym.c
898  *
899  *      side effects:
900  *              The base address of an area is set.
901  */
902
903 VOID
904 setbas()
905 {
906         register int v;
907         char id[NCPS];
908
909         bsp = basep;
910         while (bsp) {
911                 ip = bsp->b_strp;
912                 getid(id, -1);
913                 if (getnb() == '=') {
914                         v = expr(0);
915                         for (ap = areap; ap != NULL; ap = ap->a_ap) {
916                                 if (symeq(id, ap->a_id))
917                                         break;
918                         }
919                         if (ap == NULL) {
920                                 fprintf(stderr,
921                                 "No definition of area %s\n", id);
922                                 lkerr++;
923                         } else {
924                                 ap->a_addr = v;
925                                 ap->a_type = 1; /* JLH: value set */
926                         }
927                 } else {
928                         fprintf(stderr, "No '=' in base expression");
929                         lkerr++;
930                 }
931                 bsp = bsp->b_base;
932         }
933 }
934
935 /*)Function     VOID    gblsav()
936  *
937  *      The function gblsav() creates a linked structure containing
938  *      the global variable strings input to the linker.
939  *
940  *      local variable:
941  *              none
942  *
943  *      global variables:
944  *              globl   *globlp         The pointer to the first
945  *                                              globl structure
946  *              globl   *gsp            Pointer to the current
947  *                                              globl structure
948  *              char    *ip             pointer into the REL file
949  *                                              text line in ib[]
950  *              int     lkerr           error flag
951  *
952  *      functions called:
953  *              char    getnb()         lklex.c
954  *              VOID *  new()           lksym.c
955  *              int     strlen()        c_library
956  *              char *  strcpy()        c_library
957  *              VOID    unget()         lklex.c
958  *
959  *      side effects:
960  *              The globlp structure is created.
961  */
962
963 VOID
964 gblsav()
965 {
966         if (globlp == NULL) {
967                 globlp = (struct globl *)
968                         new (sizeof (struct globl));
969                 gsp = globlp;
970         } else {
971                 gsp->g_globl = (struct globl *)
972                                 new (sizeof (struct globl));
973                 gsp = gsp->g_globl;
974         }
975         unget(getnb());
976         gsp->g_strp = (char *) new (strlen(ip)+1);
977         strcpy(gsp->g_strp, ip);
978 }
979         
980 /*)Function     VOID    setgbl()
981  *
982  *      The function setgbl() scans the global variable lines in hte
983  *      globlp structure, evaluates the arguments, and sets a variable
984  *      to this value.
985  *
986  *      local variables:
987  *              int     v               expression value
988  *              char    id[]            base id string
989  *              sym *   sp              pointer to a symbol structure
990  *
991  *      global variables:
992  *              char    *ip             pointer into the REL file
993  *                                              text line in ib[]
994  *              globl   *globlp         The pointer to the first
995  *                                              globl structure
996  *              globl   *gsp            Pointer to the current
997  *                                              globl structure
998  *              FILE *  stderr          c_library
999  *              int     lkerr           error flag
1000  *
1001  *       functions called:
1002  *              Addr_T  expr()          lkeval.c
1003  *              int     fprintf()       c_library
1004  *              VOID    getid()         lklex.c
1005  *              char    getnb()         lklex.c
1006  *              sym *   lkpsym()        lksym.c
1007  *
1008  *      side effects:
1009  *              The value of a variable is set.
1010  */
1011
1012 VOID
1013 setgbl()
1014 {
1015         register int v;
1016         register struct sym *sp;
1017         char id[NCPS];
1018
1019         gsp = globlp;
1020         while (gsp) {
1021                 ip = gsp->g_strp;
1022                 getid(id, -1);
1023                 if (getnb() == '=') {
1024                         v = expr(0);
1025                         sp = lkpsym(id, 0);
1026                         if (sp == NULL) {
1027                                 fprintf(stderr,
1028                                 "No definition of symbol %s\n", id);
1029                                 lkerr++;
1030                         } else {
1031                                 if (sp->s_flag & S_DEF) {
1032                                         fprintf(stderr,
1033                                         "Redefinition of symbol %s\n", id);
1034                                         lkerr++;
1035                                         sp->s_axp = NULL;
1036                                 }
1037                                 sp->s_addr = v;
1038                                 sp->s_type |= S_DEF;
1039                         }
1040                 } else {
1041                         fprintf(stderr, "No '=' in global expression");
1042                         lkerr++;
1043                 }
1044                 gsp = gsp->g_globl;
1045         }
1046 }
1047
1048 /*)Function     FILE *  afile(fn,, ft, wf)
1049  *
1050  *              char *  fn              file specification string
1051  *              char *  ft              file type string
1052  *              int     wf              read(0)/write(1) flag
1053  *
1054  *      The function afile() opens a file for reading or writing.
1055  *              (1)     If the file type specification string ft
1056  *                      is not NULL then a file specification is
1057  *                      constructed with the file path\name in fn
1058  *                      and the extension in ft.
1059  *              (2)     If the file type specification string ft
1060  *                      is NULL then the file specification is
1061  *                      constructed from fn.  If fn does not have
1062  *                      a file type then the default .rel file
1063  *                      type is appended to the file specification.
1064  *
1065  *      afile() returns a file handle for the opened file or aborts
1066  *      the assembler on an open error.
1067  *
1068  *      local variables:
1069  *              int     c               character value
1070  *              char    fb[]            constructed file specification string
1071  *              FILE *  fp              filehandle for opened file
1072  *              char *  p1              pointer to filespec string fn
1073  *              char *  p2              pointer to filespec string fb
1074  *              char *  p3              pointer to filetype string ft
1075  *
1076  *      global variables:
1077  *              int     lkerr           error flag
1078  *
1079  *      functions called:
1080  *              FILE *  fopen()         c_library
1081  *              int     fprintf()       c_library
1082  *
1083  *      side effects:
1084  *              File is opened for read or write.
1085  */
1086
1087 FILE *
1088 afile(fn, ft, wf)
1089 char *fn;
1090 char *ft;
1091 {
1092         register char *p1, *p2, *p3;
1093         register int c;
1094         FILE *fp;
1095         char fb[PATH_MAX];
1096         char *omode = (wf ? (wf == 2 ? "a" : "w") : "r");
1097
1098         p1 = fn;
1099         p2 = fb;
1100         p3 = ft;
1101         while ((c = *p1++) != 0 && c != FSEPX) {
1102                 if (p2 < &fb[PATH_MAX-4])
1103                         *p2++ = c;
1104         }
1105         *p2++ = FSEPX;
1106         if (*p3 == 0) {
1107                 if (c == FSEPX) {
1108                         p3 = p1;
1109                 } else {
1110                         p3 = "rel";
1111                 }
1112         }
1113         while ((c = *p3++) != 0) {
1114                 if (p2 < &fb[PATH_MAX-1])
1115                         *p2++ = c;
1116         }
1117         *p2++ = 0;      
1118         if ((fp = fopen(fb, omode)) == NULL) {
1119             if (strcmp(ft,"adb")) {
1120                 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1121                 lkerr++;
1122             }
1123         }
1124         return (fp);
1125 }
1126
1127 /*)Function     VOID    iramsav()
1128  *
1129  *      The function iramsav() stores the size of the chip's internal RAM.
1130  *      This is used after linking to check that variable assignment to this
1131  *      dataspace didn't overflow into adjoining segments.  Variables in the
1132  *      DSEG, OSEG, and ISEG are assigned to this dataspace.
1133  *
1134  *      local variables:
1135  *              none
1136  *
1137  *      global variables:
1138  *              char    *ip             pointer into the REL file
1139  *                                              text line in ib[]
1140  *              unsigned int            size of chip's internal
1141  *                      iram_size               RAM segment
1142  *
1143  *       functions called:
1144  *              char    getnb()         lklex.c
1145  *              VOID    unget()         lklex.c
1146  *              Addr_T  expr()          lkeval.c
1147  *
1148  *      side effects:
1149  *              The iram_size may be modified.
1150  */
1151
1152 VOID
1153 iramsav()
1154 {
1155   unget(getnb());
1156   if (ip && *ip)
1157     //iram_size = atoi(ip);
1158     iram_size = expr(0);        /* evaluate size expression */
1159   else
1160     iram_size = 128;            /* Default is 128 (0x80) bytes */
1161 }
1162
1163 /*)Function     VOID    iramcheck()
1164  *
1165  *      The function iramcheck() is used at the end of linking to check that
1166  *      the internal RAM area wasn't overflowed by too many variable
1167  *      assignments.  Variables in the DSEG, ISEG, and OSEG are assigned to
1168  *      the chip's internal RAM.
1169  *
1170  *      local variables:
1171  *              none
1172  *
1173  *      global variables:
1174  *              unsigned int            size of chip's internal
1175  *                      iram_size               RAM segment
1176  *              struct area             linked list of memory
1177  *                      *areap                  areas
1178  *
1179  *       functions called:
1180  *
1181  *      side effects:
1182  */
1183
1184 VOID
1185 iramcheck()
1186 {
1187   register unsigned int last_addr;
1188   register struct area *ap;
1189
1190   for (ap = areap; ap; ap=ap->a_ap) {
1191     if ((ap->a_size != 0) &&
1192         (!strcmp(ap->a_id, "DSEG") ||
1193          !strcmp(ap->a_id, "OSEG") ||
1194          !strcmp(ap->a_id, "ISEG")
1195         )
1196        )
1197     {
1198       last_addr = ap->a_addr + ap->a_size - 1;
1199       if (last_addr >= iram_size)
1200         fprintf(stderr,
1201                 "\nWARNING! Segment %s extends past the end\n"
1202                 "         of internal RAM.  Check map file.\n",
1203                 ap->a_id);
1204     }
1205   }
1206 }
1207
1208 char *usetxt[] = {
1209         "Startup:",
1210         "  -c   Command line input",
1211         "  -f   file[LNK] File input",
1212         "  -p   Prompt and echo of file[LNK] to stdout (default)",
1213         "  -n   No echo of file[LNK] to stdout",
1214 /*      "Usage: [-Options] file [file ...]", */
1215         "Libraries:",
1216         "  -k   Library path specification, one per -k",
1217         "  -l   Library file specification, one per -l",
1218         "Relocation:",
1219         "  -b   area base address = expression",
1220         "  -g   global symbol = expression",
1221         "Map format:",
1222         "  -m   Map output generated as file[MAP]",
1223         "  -x   Hexadecimal (default),  -d  Decimal,  -q  Octal",
1224         "Output:",
1225         "  -i   Intel Hex as file[IHX]",
1226         "  -s   Motorola S19 as file[S19]",
1227         "  -j   Produce NoICE debug as file[NOI]",
1228         "  -z   Produce SDCdb debug as file[cdb]",
1229 /*      "List:", */
1230         "  -u   Update listing file(s) with link data as file(s)[.RST]",
1231         "Miscellaneous:\n"
1232         "  -a   [iram-size] Check for internal RAM overflow",
1233         "End:",
1234         "  -e   or null line terminates input",
1235         0
1236 };
1237
1238 /*)Function     VOID    usage()
1239  *
1240  *      The function usage() outputs to the stderr device the
1241  *      assembler name and version and a list of valid assembler options.
1242  *
1243  *      local variables:
1244  *              char ** dp              pointer to an array of
1245  *                                      text string pointers.
1246  *
1247  *      global variables:
1248  *              FILE *  stderr          c_library
1249  *
1250  *      functions called:
1251  *              int     fprintf()       c_library
1252  *
1253  *      side effects:
1254  *              none
1255  */
1256
1257 VOID
1258 usage()
1259 {
1260         register char   **dp;
1261
1262         fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1263         for (dp = usetxt; *dp; dp++)
1264                 fprintf(stderr, "%s\n", *dp);
1265         lkexit(1);
1266 }
1267
1268 /*)Function     VOID    copyfile()
1269  *              
1270  *              FILE    *dest           destination file
1271  *              FILE    *src            source file
1272  *
1273  *      function will copy source file to destination file
1274  *
1275  *
1276  *      functions called:
1277  *              int     fgetc()         c_library
1278  *              int     fputc()         c_library
1279  *
1280  *      side effects:
1281  *              none
1282  */
1283 VOID copyfile (dest,src)
1284 FILE *src,*dest ;
1285 {    
1286     int ch;
1287     while ((ch = fgetc(src)) != EOF) {
1288
1289         fputc(ch,dest);
1290     }
1291 }