4 * (C) Copyright 1989-1995
12 * - change lkparea to use a_type == 0 as "virgin area" flag
13 * 02-Apr-98 JLH: add code to link 8051 data spaces
22 * The module lkarea.c contains the functions which
23 * create and link together all area definitions read
24 * from the .rel file(s).
26 * lkarea.c contains the following functions:
32 * lkarea.c contains no global variables.
35 /*)Function VOID newarea()
37 * The function newarea() creates and/or modifies area
38 * and areax structures for each A directive read from
39 * the .rel file(s). The function lkparea() is called
40 * to find tha area structure associated with this name.
41 * If the area does not yet exist then a new area
42 * structure is created and linked to any existing
43 * linked area structures. The area flags are copied
44 * into the area flag variable. For each occurence of
45 * an A directive an areax structure is created and
46 * linked to the areax structures associated with this
47 * area. The size of this area section is placed into
48 * the areax structure. The flag value for all subsequent
49 * area definitions for the same area are compared and
50 * flagged as an error if they are not identical.
51 * The areax structure created for every occurence of
52 * an A directive is loaded with a pointer to the base
53 * area structure and a pointer to the associated
54 * head structure. And finally, a pointer to this
55 * areax structure is loaded into the list of areax
56 * structures in the head structure. Refer to lkdata.c
57 * for details of the structures and their linkage.
60 * areax **halp pointer to an array of pointers
61 * int i counter, loop variable, value
63 * int narea number of areas in this head structure
64 * areax * taxp pointer to an areax structure
68 * area *ap Pointer to the current
70 * areax *axp Pointer to the current
72 * head *hp Pointer to the current
74 * int lkerr error flag
77 * Addr_T eval() lkeval.c
78 * VOID exit() c_library
79 * int fprintf() c_library
80 * VOID getid() lklex.c
81 * VOID lkparea() lkarea.c
85 * The area and areax structures are created and
86 * linked with the appropriate head structures.
87 * Failure to allocate area or areax structure
88 * space will terminate the linker. Other internal
89 * errors most likely caused by corrupted .rel
90 * files will also terminate the linker.
94 * Create an area entry.
96 * A xxxxxx size nnnn flags mm
99 * | `------------- axp->a_size
100 * `------------------------- ap->a_id
106 register int i, narea;
120 axp->a_size = eval();
127 while (taxp->a_axp) {
135 /* if (i && (ap->a_flag != i)) { */
136 /* fprintf(stderr, "Conflicting flags in area %8s\n", id); */
141 * Place pointer in header area list
144 fprintf(stderr, "No header defined\n");
149 for (i=0; i < narea ;++i) {
150 if (halp[i] == NULL) {
155 fprintf(stderr, "Header area list overflow\n");
159 /*)Function VOID lkparea(id)
161 * char * id pointer to the area name string
163 * The function lkparea() searches the linked area structures
164 * for a name match. If the name is not found then an area
165 * structure is created. An areax structure is created and
166 * appended to the areax structures linked to the area structure.
167 * The associated base area and head structure pointers are
168 * loaded into the areax structure.
171 * area * tap pointer to an area structure
172 * areax * taxp pointer to an areax structure
175 * area *ap Pointer to the current
177 * area *areap The pointer to the first
178 * area structure of a linked list
179 * areax *axp Pointer to the current
183 * VOID * new() lksym()
184 * char * strcpy() c_library
185 * int symeq() lksym.c
188 * Area and/or areax structures are created.
189 * Failure to allocate space for created structures
190 * will terminate the linker.
196 register struct area *tap;
197 register struct areax *taxp;
200 axp = (struct areax *) new (sizeof(struct areax));
202 if (symeq(id, ap->a_id)) {
213 ap = (struct area *) new (sizeof(struct area));
225 strncpy(ap->a_id, id, NCPS);
229 /*)Function VOID lnkarea()
231 * The function lnkarea() resolves all area addresses.
232 * The function evaluates each area structure (and all
233 * the associated areax structures) in sequence. The
234 * linking process supports four (4) possible area types:
236 * ABS/OVR - All sections (each individual areax
237 * section) starts at the identical base
238 * area address overlaying all other
239 * areax sections for this area. The
240 * size of the area is largest of the area
243 * ABS/CON - All sections (each individual areax
244 * section) are concatenated with the
245 * first section starting at the base
246 * area address. The size of the area
247 * is the sum of the section sizes.
249 * NOTE: Multiple absolute (ABS) areas are
250 * never concatenated with each other,
251 * thus absolute area A and absolute area
252 * B will overlay each other if they begin
253 * at the same location (the default is
254 * always address 0 for absolute areas).
256 * REL/OVR - All sections (each individual areax
257 * section) starts at the identical base
258 * area address overlaying all other
259 * areax sections for this area. The
260 * size of the area is largest of the area
263 * REL/CON - All sections (each individual areax
264 * section) are concatenated with the
265 * first section starting at the base
266 * area address. The size of the area
267 * is the sum of the section sizes.
269 * NOTE: Relocatable (REL) areas are always concatenated
270 * with each other, thus relocatable area B
271 * (defined after area A) will follow
272 * relocatable area A independent of the
273 * starting address of area A. Within a
274 * specific area each areax section may be
275 * overlayed or concatenated with other
279 * If a base address for an area is specified then the
280 * area will start at that address. Any relocatable
281 * areas defined subsequently will be concatenated to the
282 * previous relocatable area if it does not have a base
285 * The names s_<areaname> and l_<areaname> are created to
286 * define the starting address and length of each area.
289 * Addr_T rloc ;current relocation address
290 * char temp[] ;temporary string
291 * struct symbol *sp ;symbol structure
294 * area *ap Pointer to the current
296 * area *areap The pointer to the first
297 * area structure of a linked list
300 * int fprintf() c_library
301 * VOID lnksect() lkarea.c
302 * symbol *lkpsym() lksym.c
303 * char * strncpy() c_library
304 * int symeq() lksym.c
307 * All area and areax addresses and sizes are
308 * determined and saved in their respective
313 * Resolve all area addresses.
322 /*JCF: used to save the REG_BANK_[0-3] and SBIT_BYTES area pointers*/
326 rloc[0] = rloc[1] = rloc[2] = rloc[3] = 0;
329 if (ap->a_flag&A_ABS) {
335 /* Determine memory space */
337 if (ap->a_flag & A_CODE) {
340 if (ap->a_flag & A_XDATA) {
343 if (ap->a_flag & A_BIT) {
347 * Relocatable sections
349 if (ap->a_type == 0) { /* JLH */
350 ap->a_addr = rloc[ locIndex ];
354 rloc[ locIndex ] = ap->a_addr + ap->a_size;
358 * Create symbols called:
359 * s_<areaname> the start address of the area
360 * l_<areaname> the length of the area
363 if (! symeq(ap->a_id, _abs_)) {
364 strncpy(temp+2,ap->a_id,NCPS-2);
368 sp = lkpsym(temp, 1);
369 sp->s_addr = ap->a_addr ;
370 /* sp->s_axp = ap->a_axp; JLH: was NULL; */
374 sp = lkpsym(temp, 1);
375 sp->s_addr = ap->a_size;
381 /*JCF: Since area BSEG is defined just before BSEG_BYTES, use the bit size of BSEG
382 to compute the byte size of BSEG_BYTES: */
383 if (!strcmp(ap->a_id, "BSEG")) {
384 ap->a_ap->a_axp->a_size=(ap->a_addr/8)+((ap->a_size+7)/8); /*Bits to bytes*/
386 else if (!strcmp(ap->a_id, "REG_BANK_0")) ta[0]=ap;
387 else if (!strcmp(ap->a_id, "REG_BANK_1")) ta[1]=ap;
388 else if (!strcmp(ap->a_id, "REG_BANK_2")) ta[2]=ap;
389 else if (!strcmp(ap->a_id, "REG_BANK_3")) ta[3]=ap;
390 else if (!strcmp(ap->a_id, "BSEG_BYTES"))
395 /*If upper register banks are not used roll back the relocation counter*/
396 if ( (ta[j]->a_size==0) && (ta[j-1]->a_size==0) )
407 /*)Function VOID lnksect()
409 * area * tap pointer to an area structure
411 * The function lnksect() is the function called by
412 * lnkarea() to resolve the areax addresses. Refer
413 * to the function lnkarea() for more detail. Pageing
414 * boundary and length errors will be reported by this
418 * Addr_T size size of area
419 * Addr_T addr address of area
420 * areax * taxp pointer to an areax structure
423 * int lkerr error flag
429 * All area and areax addresses and sizes area determined
430 * and linked into the structures.
434 lnksect(register struct area *tap)
436 register Addr_T size, addr;
437 register struct areax *taxp;
442 if ((tap->a_flag&A_PAG) && (addr & 0xFF)) {
444 "\n?ASlink-Warning-Paged Area %8s Boundary Error\n", tap->a_id);
449 if (tap->a_flag&A_OVR) {
455 if (taxp->a_size > size)
461 * Concatenated sections
465 addr += taxp->a_size;
466 size += taxp->a_size;
471 if ((tap->a_flag&A_PAG) && (size > 256)) {
473 "\n?ASlink-Warning-Paged Area %8s Length Error\n", tap->a_id);
476 if ((tap->a_flag&A_PAG) && (tap->a_size) &&
477 ((tap->a_addr & 0xFFFFFF00) != ((addr-1) & 0xFFFFFF00)))
480 "\n?ASlink-Warning-Paged Area %8s Boundary Error\n", tap->a_id);
485 void lnksect2 (struct area *tap, int rloc);
488 /*Modified version of the functions for packing variables in internal data memory*/
491 Addr_T rloc[4]={0, 0, 0, 0};
496 struct area *dseg_ap=NULL;
497 struct sym *sp_dseg_s=NULL, *sp_dseg_l=NULL;
499 for(j=0; j<256; j++) idatamap[j]=' ';
504 /* Determine memory space */
505 if (ap->a_flag & A_CODE) locIndex = 1;
506 else if (ap->a_flag & A_XDATA) locIndex = 2;
507 else if (ap->a_flag & A_BIT) locIndex = 3;
510 if (ap->a_flag&A_ABS) /* Absolute sections */
512 lnksect2(ap, locIndex);
514 else /* Relocatable sections */
518 ap->a_addr=rloc[locIndex];
522 lnksect2(ap, locIndex);
523 rloc[locIndex] = ap->a_addr + ap->a_size;
527 * Create symbols called:
528 * s_<areaname> the start address of the area
529 * l_<areaname> the length of the area
532 if (! symeq(ap->a_id, _abs_))
534 strncpy(temp+2,ap->a_id,NCPS-2);
538 sp = lkpsym(temp, 1);
539 sp->s_addr = ap->a_addr ;
541 if (!strcmp(ap->a_id, "DSEG")) sp_dseg_s=sp;
544 sp = lkpsym(temp, 1);
545 sp->s_addr = ap->a_size;
548 if (!strcmp(ap->a_id, "DSEG")) sp_dseg_l=sp;
551 /*Since area BSEG is defined just before BSEG_BYTES, use the bit size of BSEG
552 to compute the byte size of BSEG_BYTES: */
553 if (!strcmp(ap->a_id, "BSEG"))
555 ap->a_ap->a_axp->a_size=(ap->a_addr/8)+((ap->a_size+7)/8); /*Bits to bytes*/
557 else if (!strcmp(ap->a_id, "DSEG"))
559 dseg_ap=ap; /*Need it later to set its correct size*/
564 /*Compute the size of DSEG*/
569 for(j=0; j<0x80; j++) if(idatamap[j]!=' ') dseg_ap->a_size++;
571 if(sp_dseg_s!=NULL) sp_dseg_s->s_addr=0;
572 if(sp_dseg_l!=NULL) sp_dseg_l->s_addr=dseg_ap->a_size;
575 /*Print the memory map*/
576 fprintf(stderr, "Internal RAM layout:\n"
577 " 0 1 2 3 4 5 6 7 8 9 A B C D E F");
580 if(j%16==0) fprintf(stderr, "\n0x%02x:|", j);
581 fprintf(stderr, "%c|", idatamap[j]);
583 fprintf(stderr, "\n0-3:Reg Banks, a-z:Data, B:Bits, Q:Overlay, I:iData, S:Stack\n");
587 void lnksect2 (struct area *tap, int rloc)
589 register Addr_T size, addr;
590 register struct areax *taxp;
592 char fchar, dchar='a';
593 char ErrMsg[]="?ASlink-Error-Could not get %d consecutive byte%s"
594 " in internal RAM for area %s.\n";
598 /*Notice that only ISEG and SSEG can be in the indirectly addressable internal RAM*/
599 if( (!strcmp(tap->a_id, "ISEG")) || (!strcmp(tap->a_id, "SSEG")) )
601 if((iram_size<=0)||(iram_size>0x100))
608 if((iram_size<=0)||(iram_size>0x80))
617 if ((tap->a_flag&A_PAG) && (addr & 0xFF))
620 "\n?ASlink-Warning-Paged Area %8s Boundary Error\n", tap->a_id);
626 /*Use a letter to identify each area in the internal RAM layout map*/
629 /**/ if(!strcmp(tap->a_id, "DSEG"))
630 fchar='D'; /*It will be converted to letters 'a' to 'z' later for each areax*/
631 else if(!strcmp(tap->a_id, "ISEG"))
633 else if(!strcmp(tap->a_id, "SSEG"))
635 else if(!strcmp(tap->a_id, "OSEG"))
637 else if(!strcmp(tap->a_id, "REG_BANK_0"))
639 else if(!strcmp(tap->a_id, "REG_BANK_1"))
641 else if(!strcmp(tap->a_id, "REG_BANK_2"))
643 else if(!strcmp(tap->a_id, "REG_BANK_3"))
645 else if(!strcmp(tap->a_id, "BSEG_BYTES"))
655 if (tap->a_flag&A_OVR) /* Overlayed sections */
659 if ( (fchar=='0')||(fchar=='1')||(fchar=='2')||(fchar=='3') ) /*Reg banks*/
664 for(j=addr; (j<(int)(addr+taxp->a_size)) && (j<ramlimit); j++)
667 else if( (fchar=='S') || (fchar=='Q') ) /*Overlay and stack in internal RAM*/
669 /*Find the size of the space currently used for this areax overlay*/
670 for(j=0, size=0; j<ramlimit; j++)
671 if(idatamap[j]==fchar) size++;
673 if( (fchar=='S') && (stacksize==0) )
675 /*Search for the largest space available and use it for stack*/
676 for(j=0, k=0, taxp->a_size=0; j<ramlimit; j++)
680 if((++k)>(int)taxp->a_size) taxp->a_size=k;
687 stacksize=taxp->a_size;
690 /*If more space required, release the previously allocated areax in
691 internal RAM and search for a bigger one*/
692 if((int)taxp->a_size>size)
694 size=(int)taxp->a_size;
696 for(j=0; j<ramlimit; j++)
697 if(idatamap[j]==fchar) idatamap[j]=' ';
699 /*Search for a space large enough in data memory for this overlay areax*/
700 for(j=0, k=0; j<ramlimit; j++)
706 if(k==(int)taxp->a_size) break;
709 if(k==(int)taxp->a_size)
711 taxp->a_addr = j-k+1;
712 if(addr<(unsigned int)ramlimit)
714 for(j=ramlimit-1; (j>=0)&&(idatamap[j]==' '); j--);
719 /*Mark the memory used for overlay*/
720 if(k==(int)taxp->a_size)
722 for(j=taxp->a_addr; (j<(int)(taxp->a_addr+taxp->a_size)) && (j<ramlimit); j++)
725 /*Set the new size of the data memory area*/
728 else /*Couldn't find a chunk big enough: report the problem.*/
730 tap->a_unaloc=taxp->a_size;
731 fprintf(stderr, ErrMsg, taxp->a_size, taxp->a_size>1?"s":"", tap->a_id);
736 for(j=0; j<ramlimit; j++)
738 if (idatamap[j]==fchar)
747 else /*Overlay areas not in internal ram*/
750 if (taxp->a_size > size) size = taxp->a_size;
755 else /* Concatenated sections */
759 if( (fchar=='D') || (fchar=='I') )
763 /*Search for a space large enough in internal RAM for this areax*/
764 for(j=0, k=0; j<ramlimit; j++)
770 if(k==(int)taxp->a_size) break;
773 if(k==(int)taxp->a_size)
775 taxp->a_addr = j-k+1;
776 if(addr<(unsigned int)ramlimit)
778 for(j=ramlimit-1; (j>=0)&&(idatamap[j]==' '); j--);
783 for(j=taxp->a_addr; (j<(int)(taxp->a_addr+taxp->a_size)) && (j<ramlimit); j++)
784 idatamap[j]=(fchar=='D')?dchar:fchar;
785 if((taxp->a_size>0)&&(fchar=='D'))dchar++;
786 if((dchar<'a')||(dchar>'z')) dchar='D'; /*Ran out of letters?*/
788 else /*We are in trouble, there is not enough memory for an areax chunk*/
791 addr += taxp->a_size;
792 size += taxp->a_size;
793 tap->a_unaloc+=taxp->a_size;
794 fprintf(stderr, ErrMsg, taxp->a_size, taxp->a_size>1?"s":"", tap->a_id);
804 for(j=0x20+taxp->a_addr; j<((int)(0x20+taxp->a_addr+taxp->a_size)); j++)
809 addr += taxp->a_size;
810 size += taxp->a_size;
813 else /*For concatenated BIT, CODE, and XRAM areax's*/
815 if(!strcmp(tap->a_id, "XSTK") && (taxp->a_size == 1))
817 taxp->a_size = 256-(addr & 0xFF);
820 addr += taxp->a_size;
821 size += taxp->a_size;
828 if ((tap->a_flag&A_PAG) && (size > 256))
831 "\n?ASlink-Warning-Paged Area %8s Length Error\n", tap->a_id);
834 if ((tap->a_flag&A_PAG) && (tap->a_size) &&
835 ((tap->a_addr & 0xFFFFFF00) != ((addr-1) & 0xFFFFFF00)))
838 "\n?ASlink-Warning-Paged Area %8s Boundary Error\n", tap->a_id);