cleaned up the mess I left behind
[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                 SaveLinkedFilePath(linkp->f_idp); //Must be the first one... 
249                 dfp = afile(linkp->f_idp,"cdb",1); //JCF: Nov 30, 2002
250             if (dfp == NULL) 
251                 lkexit(1);
252         }
253
254         for (pass=0; pass<2; ++pass) {
255                 cfp = NULL;
256                 sfp = NULL;
257                 filep = linkp;
258                 hp = NULL;
259                 radix = 10;
260                 
261                 Areas51(); /*JCF: Create the default 8051 areas in the right order*/
262
263                 while (getline()) {
264                         ip = ib;
265
266                         /* pass any "magic comments" to NoICE output */
267                         if ((ip[0] == ';') && (ip[1] == '!') && jfp) {
268                                 fprintf( jfp, "%s\n", &ip[2] );
269                         }
270                         link_main();
271                 }
272                 if (pass == 0) {
273                         /*
274                          * Search libraries for global symbols
275                          */
276                         search();
277                         /*
278                          * Set area base addresses.
279                          */
280                         setbas();
281                         /*
282                          * Link all area addresses.
283                          */
284                         lnkarea();
285                         /*
286                          * Process global definitions.
287                          */
288                         setgbl();
289                         /*
290                          * Check for undefined globals.
291                          */
292                         symdef(stderr);
293
294                         /* Open NoICE output file if requested */
295                         if (jflag) {
296                                 jfp = afile(linkp->f_idp, "NOI", 1);
297                                 if (jfp == NULL) {
298                                         lkexit(1);
299                                 }
300                         }
301
302                         /*
303                          * Output Link Map if requested,
304                          * or if NoICE output requested (since NoICE
305                          * file is generated in part by map() processing)
306                          */
307                         if (mflag || jflag)
308                                 map();
309
310                         if (sflag) /*JCF: memory usage summary output*/
311                                 if(summary(areap))lkexit(1);
312
313                         if (iram_size)
314                                 iramcheck();
315
316                         /*
317                          * Open output file
318                          */
319                         if (oflag == 1) {
320                                 ofp = afile(linkp->f_idp, "ihx", 1);
321                                 if (ofp == NULL) {
322                                         lkexit(1);
323                                 }
324                                 /* include NoICE command to load hex file */
325                                 if (jfp) fprintf( jfp, "LOAD %s.IHX\n", linkp->f_idp );
326
327                         } else
328                         if (oflag == 2) {
329                                 ofp = afile(linkp->f_idp, "S19", 1);
330                                 if (ofp == NULL) {
331                                         lkexit(1);
332                                 }
333                                 /* include NoICE command to load hex file */
334                                 if (jfp) fprintf( jfp, "LOAD %s.S19\n", linkp->f_idp );
335                         }
336                 } else {
337                         /*
338                          * Link in library files
339                          */
340                         library();
341                         reloc('E');
342                 }
343         }
344         //JCF:
345         CreateAOMF51();
346
347         lkexit(lkerr);
348         return 0;
349 }
350
351 /*)Function     VOID    lkexit(i)
352  *
353  *                      int     i       exit code
354  *
355  *      The function lkexit() explicitly closes all open
356  *      files and then terminates the program.
357  *
358  *      local variables:
359  *              none
360  *
361  *      global variables:
362  *              FILE *  mfp             file handle for .map
363  *              FILE *  ofp             file handle for .ihx/.s19
364  *              FILE *  rfp             file hanlde for .rst
365  *              FILE *  sfp             file handle for .rel
366  *              FILE *  tfp             file handle for .lst
367  *
368  *      functions called:
369  *              int     fclose()        c_library
370  *              VOID    exit()          c_library
371  *
372  *      side effects:
373  *              All files closed. Program terminates.
374  */
375
376 VOID
377 lkexit(i)
378 int i;
379 {
380         if (mfp != NULL) fclose(mfp);
381         if (jfp != NULL) fclose(jfp);
382         if (ofp != NULL) fclose(ofp);
383         if (rfp != NULL) fclose(rfp);
384         if (sfp != NULL) fclose(sfp);
385         if (tfp != NULL) fclose(tfp);
386         if (dfp != NULL) fclose(dfp);
387         /*if (dfp != NULL)
388             FILE *xfp = afile(linkp->f_idp,"cdb",1);
389             dfp = freopen("temp.cdb","r",dfp);
390             copyfile(xfp,dfp);
391             fclose(xfp);
392             fclose(dfp);
393             unlink("temp.cdb");
394         }*/
395         exit(i);
396 }
397
398 /*)Function     link_main()
399  *
400  *      The function link_main() evaluates the directives for each line of
401  *      text read from the .rel file(s).  The valid directives processed
402  *      are:
403  *              X, D, Q, H, M, A, S, T, R, and P.
404  *
405  *      local variables:
406  *              int     c               first non blank character of a line
407  *
408  *      global variables:
409  *              head    *headp          The pointer to the first
410  *                                              head structure of a linked list
411  *              head    *hp             Pointer to the current
412  *                                              head structure
413  *              int     pass            linker pass number
414  *              int     radix           current number conversion radix
415  *
416  *      functions called:
417  *              char    endline()       lklex.c
418  *              VOID    module()        lkhead.c
419  *              VOID    newarea()       lkarea.c
420  *              VOID    newhead()       lkhead.c
421  *              sym *   newsym()        lksym.c
422  *              VOID    reloc()         lkreloc.c
423  *
424  *      side effects:
425  *              Head, area, and symbol structures are created and
426  *              the radix is set as the .rel file(s) are read.
427  */
428
429 VOID
430 link_main()
431 {
432         register int c;
433
434         if ((c=endline()) == 0) { return; }
435         switch (c) {
436
437         case 'X':
438                 radix = 16;
439                 break;
440
441         case 'D':
442                 radix = 10;
443                 break;
444
445         case 'Q':
446                 radix = 8;
447                 break;
448
449         case 'H':
450                 if (pass == 0) {
451                         newhead();
452                 } else {
453                         if (hp == 0) {
454                                 hp = headp;
455                         } else {
456                                 hp = hp->h_hp;
457                         }
458                 }
459                 sdp.s_area = NULL;
460                 sdp.s_areax = NULL;
461                 sdp.s_addr = 0;
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 'v':
794                                 case 'V':
795                                         xramsav();
796                                         return(0);
797
798                                 case 'w':
799                                 case 'W':
800                                         codesav();
801                                         return(0);
802
803                                 case 'z':
804                                 case 'Z':
805                                         dflag = 1;                                      
806                                         return(0);
807                                 default:
808                                         fprintf(stderr, "Invalid option\n");
809                                         lkexit(1);
810                                 }
811                         }
812                 } else
813                if (ctype[c] & ILL) {
814                        fprintf(stderr, "Invalid input");
815                        lkexit(1);
816                } else {
817                         if (linkp == NULL) {
818                                 linkp = (struct lfile *)
819                                         new (sizeof (struct lfile));
820                                 lfp = linkp;
821                         } else {
822                                 lfp->f_flp = (struct lfile *)
823                                                 new (sizeof (struct lfile));
824                                 lfp = lfp->f_flp;
825                         }
826                         getfid(fid, c);
827                         lfp->f_idp = (char *) new (strlen(fid)+1);
828                         strcpy(lfp->f_idp, fid);
829                         lfp->f_type = F_REL;
830                 }
831         }
832         return(0);
833 }
834
835 /*)Function     VOID    bassav()
836  *
837  *      The function bassav() creates a linked structure containing
838  *      the base address strings input to the linker.
839  *
840  *      local variables:
841  *              none
842  *
843  *      global variables:
844  *              base    *basep          The pointer to the first
845  *                                              base structure
846  *              base    *bsp            Pointer to the current
847  *                                              base structure
848  *              char    *ip             pointer into the REL file
849  *                                              text line in ib[]
850  *
851  *       functions called:
852  *              char    getnb()         lklex.c
853  *              VOID *  new()           lksym.c
854  *              int     strlen()        c_library
855  *              char *  strcpy()        c_library
856  *              VOID    unget()         lklex.c
857  *
858  *      side effects:
859  *              The basep structure is created.
860  */
861
862 VOID
863 bassav()
864 {
865         if (basep == NULL) {
866                 basep = (struct base *)
867                         new (sizeof (struct base));
868                 bsp = basep;
869         } else {
870                 bsp->b_base = (struct base *)
871                                 new (sizeof (struct base));
872                 bsp = bsp->b_base;
873         }
874         unget(getnb());
875         bsp->b_strp = (char *) new (strlen(ip)+1);
876         strcpy(bsp->b_strp, ip);
877 }
878
879 /*)Function     VOID    setbas()
880  *
881  *      The function setbas() scans the base address lines in hte
882  *      basep structure, evaluates the arguments, and sets beginning
883  *      address of the specified areas.
884  *
885  *      local variables:
886  *              int     v               expression value
887  *              char    id[]            base id string
888  *
889  *      global variables:
890  *              area    *ap             Pointer to the current
891  *                                              area structure
892  *              area    *areap          The pointer to the first
893  *                                              area structure of a linked list
894  *              base    *basep          The pointer to the first
895  *                                              base structure
896  *              base    *bsp            Pointer to the current
897  *                                              base structure
898  *              char    *ip             pointer into the REL file
899  *                                              text line in ib[]
900  *              int     lkerr           error flag
901  *
902  *       functions called:
903  *              Addr_T  expr()          lkeval.c
904  *              int     fprintf()       c_library
905  *              VOID    getid()         lklex.c
906  *              char    getnb()         lklex.c
907  *              int     symeq()         lksym.c
908  *
909  *      side effects:
910  *              The base address of an area is set.
911  */
912
913 VOID
914 setbas()
915 {
916         register int v;
917         char id[NCPS];
918
919         bsp = basep;
920         while (bsp) {
921                 ip = bsp->b_strp;
922                 getid(id, -1);
923                 if (getnb() == '=') {
924                         v = expr(0);
925                         for (ap = areap; ap != NULL; ap = ap->a_ap) {
926                                 if (symeq(id, ap->a_id))
927                                         break;
928                         }
929                         if (ap == NULL) {
930                                 fprintf(stderr,
931                                 "ASlink-Warning-No definition of area %s\n", id);
932                                 lkerr++;
933                         } else {
934                                 ap->a_addr = v;
935                                 ap->a_type = 1; /* JLH: value set */
936                         }
937                 } else {
938                         fprintf(stderr, "ASlink-Warning-No '=' in base expression");
939                         lkerr++;
940                 }
941                 bsp = bsp->b_base;
942         }
943 }
944
945 /*)Function     VOID    gblsav()
946  *
947  *      The function gblsav() creates a linked structure containing
948  *      the global variable strings input to the linker.
949  *
950  *      local variable:
951  *              none
952  *
953  *      global variables:
954  *              globl   *globlp         The pointer to the first
955  *                                              globl structure
956  *              globl   *gsp            Pointer to the current
957  *                                              globl structure
958  *              char    *ip             pointer into the REL file
959  *                                              text line in ib[]
960  *              int     lkerr           error flag
961  *
962  *      functions called:
963  *              char    getnb()         lklex.c
964  *              VOID *  new()           lksym.c
965  *              int     strlen()        c_library
966  *              char *  strcpy()        c_library
967  *              VOID    unget()         lklex.c
968  *
969  *      side effects:
970  *              The globlp structure is created.
971  */
972
973 VOID
974 gblsav()
975 {
976         if (globlp == NULL) {
977                 globlp = (struct globl *)
978                         new (sizeof (struct globl));
979                 gsp = globlp;
980         } else {
981                 gsp->g_globl = (struct globl *)
982                                 new (sizeof (struct globl));
983                 gsp = gsp->g_globl;
984         }
985         unget(getnb());
986         gsp->g_strp = (char *) new (strlen(ip)+1);
987         strcpy(gsp->g_strp, ip);
988 }
989         
990 /*)Function     VOID    setgbl()
991  *
992  *      The function setgbl() scans the global variable lines in hte
993  *      globlp structure, evaluates the arguments, and sets a variable
994  *      to this value.
995  *
996  *      local variables:
997  *              int     v               expression value
998  *              char    id[]            base id string
999  *              sym *   sp              pointer to a symbol structure
1000  *
1001  *      global variables:
1002  *              char    *ip             pointer into the REL file
1003  *                                              text line in ib[]
1004  *              globl   *globlp         The pointer to the first
1005  *                                              globl structure
1006  *              globl   *gsp            Pointer to the current
1007  *                                              globl structure
1008  *              FILE *  stderr          c_library
1009  *              int     lkerr           error flag
1010  *
1011  *       functions called:
1012  *              Addr_T  expr()          lkeval.c
1013  *              int     fprintf()       c_library
1014  *              VOID    getid()         lklex.c
1015  *              char    getnb()         lklex.c
1016  *              sym *   lkpsym()        lksym.c
1017  *
1018  *      side effects:
1019  *              The value of a variable is set.
1020  */
1021
1022 VOID
1023 setgbl()
1024 {
1025         register int v;
1026         register struct sym *sp;
1027         char id[NCPS];
1028
1029         gsp = globlp;
1030         while (gsp) {
1031                 ip = gsp->g_strp;
1032                 getid(id, -1);
1033                 if (getnb() == '=') {
1034                         v = expr(0);
1035                         sp = lkpsym(id, 0);
1036                         if (sp == NULL) {
1037                                 fprintf(stderr,
1038                                 "No definition of symbol %s\n", id);
1039                                 lkerr++;
1040                         } else {
1041                                 if (sp->s_flag & S_DEF) {
1042                                         fprintf(stderr,
1043                                         "Redefinition of symbol %s\n", id);
1044                                         lkerr++;
1045                                         sp->s_axp = NULL;
1046                                 }
1047                                 sp->s_addr = v;
1048                                 sp->s_type |= S_DEF;
1049                         }
1050                 } else {
1051                         fprintf(stderr, "No '=' in global expression");
1052                         lkerr++;
1053                 }
1054                 gsp = gsp->g_globl;
1055         }
1056 }
1057
1058 /*)Function     FILE *  afile(fn,, ft, wf)
1059  *
1060  *              char *  fn              file specification string
1061  *              char *  ft              file type string
1062  *              int     wf              read(0)/write(1) flag
1063  *
1064  *      The function afile() opens a file for reading or writing.
1065  *              (1)     If the file type specification string ft
1066  *                      is not NULL then a file specification is
1067  *                      constructed with the file path\name in fn
1068  *                      and the extension in ft.
1069  *              (2)     If the file type specification string ft
1070  *                      is NULL then the file specification is
1071  *                      constructed from fn.  If fn does not have
1072  *                      a file type then the default .rel file
1073  *                      type is appended to the file specification.
1074  *
1075  *      afile() returns a file handle for the opened file or aborts
1076  *      the assembler on an open error.
1077  *
1078  *      local variables:
1079  *              int     c               character value
1080  *              char    fb[]            constructed file specification string
1081  *              FILE *  fp              filehandle for opened file
1082  *              char *  p1              pointer to filespec string fn
1083  *              char *  p2              pointer to filespec string fb
1084  *              char *  p3              pointer to filetype string ft
1085  *
1086  *      global variables:
1087  *              int     lkerr           error flag
1088  *
1089  *      functions called:
1090  *              FILE *  fopen()         c_library
1091  *              int     fprintf()       c_library
1092  *
1093  *      side effects:
1094  *              File is opened for read or write.
1095  */
1096
1097 FILE *
1098 afile(fn, ft, wf)
1099 char *fn;
1100 char *ft;
1101 {
1102         register char *p1, *p2, *p3;
1103         register int c;
1104         FILE *fp;
1105         char fb[PATH_MAX];
1106         char *omode = (wf ? (wf == 2 ? "a" : "w") : "r");
1107
1108         p1 = fn;
1109         p2 = fb;
1110         p3 = ft;
1111         while ((c = *p1++) != 0 && c != FSEPX) {
1112                 if (p2 < &fb[PATH_MAX-4])
1113                         *p2++ = c;
1114         }
1115         *p2++ = FSEPX;
1116         if (*p3 == 0) {
1117                 if (c == FSEPX) {
1118                         p3 = p1;
1119                 } else {
1120                         p3 = "rel";
1121                 }
1122         }
1123         while ((c = *p3++) != 0) {
1124                 if (p2 < &fb[PATH_MAX-1])
1125                         *p2++ = c;
1126         }
1127         *p2++ = 0;      
1128         if ((fp = fopen(fb, omode)) == NULL) {
1129             if (strcmp(ft,"adb")) {
1130                 fprintf(stderr, "%s: cannot %s.\n", fb, wf?"create":"open");
1131                 lkerr++;
1132             }
1133         }
1134         return (fp);
1135 }
1136
1137 /*)Function     VOID    iramsav()
1138  *
1139  *      The function iramsav() stores the size of the chip's internal RAM.
1140  *      This is used after linking to check that variable assignment to this
1141  *      dataspace didn't overflow into adjoining segments.  Variables in the
1142  *      DSEG, OSEG, and ISEG are assigned to this dataspace.
1143  *
1144  *      local variables:
1145  *              none
1146  *
1147  *      global variables:
1148  *              char    *ip             pointer into the REL file
1149  *                                              text line in ib[]
1150  *              unsigned int            size of chip's internal
1151  *                      iram_size               RAM segment
1152  *
1153  *       functions called:
1154  *              char    getnb()         lklex.c
1155  *              VOID    unget()         lklex.c
1156  *              Addr_T  expr()          lkeval.c
1157  *
1158  *      side effects:
1159  *              The iram_size may be modified.
1160  */
1161
1162 VOID
1163 iramsav()
1164 {
1165   unget(getnb());
1166   if (ip && *ip)
1167     //iram_size = atoi(ip);
1168     iram_size = expr(0);        /* evaluate size expression */
1169   else
1170     iram_size = 128;            /* Default is 128 (0x80) bytes */
1171 }
1172
1173 /*Similar to iramsav but for xram memory*/
1174 VOID
1175 xramsav()
1176 {
1177   unget(getnb());
1178   if (ip && *ip)
1179     xram_size = expr(0);        /* evaluate size expression */
1180   else
1181         xram_size = rflag?0x1000000:0x10000;
1182 }
1183
1184 /*Similar to iramsav but for code memory*/
1185 VOID
1186 codesav()
1187 {
1188   unget(getnb());
1189   if (ip && *ip)
1190     code_size = expr(0);        /* evaluate size expression */
1191   else
1192         code_size = rflag?0x1000000:0x10000;
1193 }
1194
1195
1196 /*)Function     VOID    iramcheck()
1197  *
1198  *      The function iramcheck() is used at the end of linking to check that
1199  *      the internal RAM area wasn't overflowed by too many variable
1200  *      assignments.  Variables in the DSEG, ISEG, and OSEG are assigned to
1201  *      the chip's internal RAM.
1202  *
1203  *      local variables:
1204  *              none
1205  *
1206  *      global variables:
1207  *              unsigned int            size of chip's internal
1208  *                      iram_size               RAM segment
1209  *              struct area             linked list of memory
1210  *                      *areap                  areas
1211  *
1212  *       functions called:
1213  *
1214  *      side effects:
1215  */
1216
1217 VOID
1218 iramcheck()
1219 {
1220   register unsigned int last_addr;
1221   register struct area *ap;
1222
1223   for (ap = areap; ap; ap=ap->a_ap) {
1224     if ((ap->a_size != 0) &&
1225         (!strcmp(ap->a_id, "DSEG") ||
1226          !strcmp(ap->a_id, "OSEG") ||
1227          !strcmp(ap->a_id, "ISEG")
1228         )
1229        )
1230     {
1231       last_addr = ap->a_addr + ap->a_size - 1;
1232       if (last_addr >= iram_size)
1233         fprintf(stderr,
1234                 "\nWARNING! Segment %s extends past the end\n"
1235                 "         of internal RAM.  Check map file.\n",
1236                 ap->a_id);
1237     }
1238   }
1239 }
1240
1241 char *usetxt[] = {
1242         "Startup:",
1243         "  -c   Command line input",
1244         "  -f   file[LNK] File input",
1245         "  -p   Prompt and echo of file[LNK] to stdout (default)",
1246         "  -n   No echo of file[LNK] to stdout",
1247 /*      "Usage: [-Options] file [file ...]", */
1248         "Libraries:",
1249         "  -k   Library path specification, one per -k",
1250         "  -l   Library file specification, one per -l",
1251         "Relocation:",
1252         "  -b   area base address = expression",
1253         "  -g   global symbol = expression",
1254         "Map format:",
1255         "  -m   Map output generated as file[MAP]",
1256         "  -x   Hexadecimal (default),  -d  Decimal,  -q  Octal",
1257         "Output:",
1258         "  -i   Intel Hex as file[IHX]",
1259         "  -s   Motorola S19 as file[S19]",
1260         "  -j   Produce NoICE debug as file[NOI]",
1261         "  -z   Produce SDCdb debug as file[cdb]",
1262 /*      "List:", */
1263         "  -u   Update listing file(s) with link data as file(s)[.RST]",
1264         "Miscellaneous:\n"
1265         "  -a   [iram-size] Check for internal RAM overflow",
1266         "  -v   [xram-size] Check for external RAM overflow",
1267         "  -w   [code-size] Check for code overflow",
1268         "End:",
1269         "  -e   or null line terminates input",
1270         0
1271 };
1272
1273 /*)Function     VOID    usage()
1274  *
1275  *      The function usage() outputs to the stderr device the
1276  *      assembler name and version and a list of valid assembler options.
1277  *
1278  *      local variables:
1279  *              char ** dp              pointer to an array of
1280  *                                      text string pointers.
1281  *
1282  *      global variables:
1283  *              FILE *  stderr          c_library
1284  *
1285  *      functions called:
1286  *              int     fprintf()       c_library
1287  *
1288  *      side effects:
1289  *              none
1290  */
1291
1292 VOID
1293 usage()
1294 {
1295         register char   **dp;
1296
1297         fprintf(stderr, "\nASxxxx Linker %s\n\n", VERSION);
1298         for (dp = usetxt; *dp; dp++)
1299                 fprintf(stderr, "%s\n", *dp);
1300         lkexit(1);
1301 }
1302
1303 /*)Function     VOID    copyfile()
1304  *              
1305  *              FILE    *dest           destination file
1306  *              FILE    *src            source file
1307  *
1308  *      function will copy source file to destination file
1309  *
1310  *
1311  *      functions called:
1312  *              int     fgetc()         c_library
1313  *              int     fputc()         c_library
1314  *
1315  *      side effects:
1316  *              none
1317  */
1318 VOID copyfile (dest,src)
1319 FILE *src,*dest ;
1320 {    
1321     int ch;
1322     while ((ch = fgetc(src)) != EOF) {
1323
1324         fputc(ch,dest);
1325     }
1326 }