1 /* 2 * Copyright (c) 1982 Regents of the University of California 3 */ 4 #ifndef lint 5 static char sccsid[] = "@(#)assyms.c 4.13 07/01/83"; 6 #endif not lint 7 8 #include <stdio.h> 9 #include <ctype.h> 10 #include "as.h" 11 #include "asscan.h" 12 #include "assyms.h" 13 14 /* 15 * Managers for chunks of symbols allocated from calloc() 16 * We maintain a linked list of such chunks. 17 * 18 */ 19 struct allocbox *allochead; /*head of chunk list*/ 20 struct allocbox *alloctail; /*tail*/ 21 struct allocbox *newbox; /*for creating a new chunk*/ 22 struct symtab *nextsym; /*next symbol free*/ 23 int symsleft; /*slots left in current chunk*/ 24 25 struct symtab **symptrs; 26 struct symtab **symdelim[NLOC + NLOC +1]; 27 struct symtab **symptrub; 28 /* 29 * Managers for the dynamically extendable hash table 30 */ 31 struct hashdallop *htab; 32 33 Iptr *itab[NINST]; /*maps opcodes to instructions*/ 34 /* 35 * Counts what went into the symbol table, so that the 36 * size of the symbol table can be computed. 37 */ 38 int nsyms; /* total number in the symbol table */ 39 int njxxx; /* number of jxxx entrys */ 40 int nforgotten; /* number of symbols erroneously entered */ 41 int nlabels; /* number of label entries */ 42 43 /* 44 * Managers of the symbol literal storage. 45 */ 46 struct strpool *strplhead = 0; 47 48 symtabinit() 49 { 50 allochead = 0; 51 alloctail = 0; 52 nextsym = 0; 53 symsleft = 0; 54 strpoolalloc(); /* get the first strpool storage area */ 55 htab = 0; 56 htaballoc(); /* get the first part of the hash table */ 57 } 58 59 /* 60 * Install all known instructions in the symbol table 61 */ 62 syminstall() 63 { 64 register Iptr ip; 65 register struct symtab **hp; 66 register char *p1, *p2; 67 register int i; 68 69 for (i = 0; i < NINST; i++) 70 itab[i] = (Iptr*)BADPOINT; 71 72 for (ip = (Iptr)instab; FETCHNAME(ip)[0]; ip++) { 73 p1 = FETCHNAME(ip); 74 p2 = yytext; 75 while (*p2++ = *p1++); 76 hp = lookup(0); /* 0 => don't install this*/ 77 if (*hp==NULL) { 78 *hp = (struct symtab *)ip; 79 if ( (ip->s_tag!=INSTn) 80 && (ip->s_tag!=INST0) 81 && (ip->s_tag!=0)) 82 continue; /* was pseudo-op */ 83 if (itab[ip->i_eopcode] == (Iptr*)BADPOINT){ 84 itab[ip->i_eopcode] = 85 (Iptr*)ClearCalloc(256, sizeof(Iptr)); 86 for (i = 0; i < 256; i++) 87 itab[ip->i_eopcode][i] = 88 (Iptr)BADPOINT; 89 } 90 itab[ip->i_eopcode][ip->i_popcode] = ip; 91 } 92 } 93 } /*end of syminstall*/ 94 95 96 /* 97 * Assign final values to symbols, 98 * and overwrite the index field with its relative position in 99 * the symbol table we give to the loader. 100 */ 101 extern struct exec hdr; 102 103 freezesymtab() 104 { 105 register struct symtab *sp; 106 long bs; 107 register int relpos = 0; 108 register struct symtab *ubsp; 109 register struct allocbox *allocwalk; 110 111 DECLITERATE(allocwalk, sp, ubsp) 112 { 113 if (sp->s_tag >= IGNOREBOUND) 114 continue; /*totally ignore jxxx entries */ 115 /* 116 * Ignore stabs, but give them a symbol table index 117 */ 118 if (sp->s_type & STABFLAG) 119 goto assignindex; 120 if ((sp->s_type&XTYPE)==XUNDEF) 121 sp->s_type = XXTRN+XUNDEF; 122 else if ((sp->s_type&XTYPE)==XDATA) 123 sp->s_value += usedot[sp->s_index].e_xvalue; 124 else if ((sp->s_type&XTYPE)==XTEXT) 125 sp->s_value += usedot[sp->s_index].e_xvalue; 126 else if ((sp->s_type&XTYPE)==XBSS) { 127 bs = sp->s_value; 128 sp->s_value = hdr.a_bss + datbase; 129 hdr.a_bss += bs; 130 } 131 assignindex: 132 if ( (FETCHNAME(sp)[0] != 'L') 133 || (sp->s_tag != LABELID) 134 || savelabels 135 ) /*then, we will write it later on*/ 136 sp->s_index = relpos++; 137 } 138 } 139 140 141 142 /* 143 * For all of the stabs that had their final value undefined during pass 1 144 * and during pass 2 assign a final value. 145 * We have already given stab entrys a initial approximation 146 * when we constsructed the sorted symbol table. 147 * Iteration order doesn't matter. 148 */ 149 150 stabfix() 151 { 152 register struct symtab *sp, **cosp; 153 register struct symtab *p; 154 155 SYMITERATE(cosp, sp){ 156 if(sp->s_ptype && (sp->s_type & STABFLAG)) { 157 p = sp->s_dest; 158 /* 159 * STABFLOATING indicates that the offset has been saved in s_desc, s_other 160 */ 161 if(sp->s_tag == STABFLOATING) { 162 sp->s_value = ( ( ((unsigned char) sp->s_other) << 16) | ( (unsigned short) sp->s_desc ) ); 163 sp->s_value = sp->s_value + p->s_value; 164 } 165 else sp->s_value = p->s_value; 166 sp->s_index = p->s_index; 167 sp->s_type = p->s_type; 168 169 170 } 171 } 172 } 173 174 char *Calloc(number, size) 175 int number, size; 176 { 177 register char *newstuff; 178 char *sbrk(); 179 newstuff = sbrk(number*size); 180 if ((int)newstuff == -1){ 181 yyerror("Ran out of Memory"); 182 delexit(); 183 } 184 return(newstuff); 185 } 186 187 char *ClearCalloc(number, size) 188 int number, size; 189 { 190 register char *newstuff; /* r11 */ 191 register int length = number * size; /* r10 */ 192 #ifdef lint 193 length = length; 194 #endif length 195 newstuff = Calloc(number, size); 196 asm("movc5 $0, (r0), $0, r10, (r11)"); 197 return(newstuff); 198 } 199 200 struct symtab *symalloc() 201 { 202 if (symsleft == 0){ 203 newbox = (struct allocbox *)ClearCalloc(1,ALLOCQTY); 204 symsleft = SYMDALLOP; 205 nextsym = &newbox->symslots[0]; 206 if (alloctail == 0){ 207 allochead = alloctail = newbox; 208 } else { 209 alloctail->nextalloc = newbox; 210 alloctail = newbox; 211 } 212 } 213 --symsleft; 214 ++nsyms; 215 return(nextsym++); 216 } 217 218 strpoolalloc() 219 { 220 register struct strpool *new; 221 222 new = (struct strpool *)Calloc(1, sizeof (struct strpool)); 223 new->str_nalloc = 0; 224 new->str_next = strplhead; 225 strplhead = new; 226 } 227 228 symcmp(Pptr, Qptr) 229 struct symtab **Pptr, **Qptr; 230 { 231 register struct symtab *p = *Pptr; 232 register struct symtab *q = *Qptr; 233 if (p->s_index < q->s_index) 234 return(-1); 235 if (p->s_index > q->s_index) 236 return(1); 237 if (p->s_value < q->s_value) 238 return(-1); 239 if (p->s_value > q->s_value) 240 return(1); 241 /* 242 * Force jxxx entries to virtually preceed labels defined 243 * to follow the jxxxx instruction, so that bumping the 244 * jxxx instruction correctly fixes up the following labels 245 */ 246 if (p->s_tag >= IGNOREBOUND) /*p points to a jxxx*/ 247 return(-1); 248 if (q->s_tag >= IGNOREBOUND) 249 return(1); 250 /* 251 * both are now just plain labels; the relative order doesn't 252 * matter. Both can't be jxxxes, as they would have different 253 * values. 254 */ 255 return(0); 256 } /*end of symcmp*/ 257 258 /* 259 * We construct the auxiliary table of pointers, symptrs and 260 * symdelim 261 * We also assign preliminary values to stab entries that did not yet 262 * have an absolute value (because they initially referred to 263 * forward references). We don't worry about .stabds, as they 264 * already have an estimated final value 265 */ 266 267 sortsymtab() 268 { 269 register struct symtab *sp; 270 register struct symtab **cowalk; 271 register struct allocbox *allocwalk; 272 struct symtab *ubsp; 273 int segno; 274 int slotno; 275 int symsin; /*number put into symptrs*/ 276 277 symptrs = (struct symtab **)Calloc(nsyms + 2, sizeof *symptrs); 278 /* 279 * Allocate one word at the beginning of the symptr array 280 * so that backwards scans through the symptr array will 281 * work correctly while scanning through the zeroth segment 282 */ 283 *symptrs++ = 0; 284 cowalk = symptrs; 285 symsin = 0; 286 DECLITERATE(allocwalk, sp, ubsp) { 287 if (sp->s_ptype && (sp->s_type &STABFLAG)){ 288 sp->s_value = sp->s_dest->s_value; 289 sp->s_index = sp->s_dest->s_index; 290 } 291 if (symsin >= nsyms) 292 yyerror("INTERNAL ERROR: overfilled symbol table indirection table"); 293 *cowalk++ = sp; 294 symsin++; 295 } 296 if (symsin != nsyms) 297 yyerror("INTERNAL ERROR: installed %d syms, should have installed %d", 298 symsin, nsyms); 299 symptrub = &symptrs[nsyms ]; 300 qsort(symptrs, nsyms, sizeof *symptrs, symcmp); 301 symdelim[0] = symptrs; 302 for (cowalk = symptrs, sp = *cowalk, segno = 0, slotno = 1; 303 segno < NLOC + NLOC; 304 segno++, slotno++){ 305 for (; sp && sp->s_index == segno; sp = *++cowalk); 306 symdelim[slotno] = cowalk; /*forms the ub delimeter*/ 307 } 308 } /*end of sortsymtab*/ 309 310 #ifdef DEBUG 311 dumpsymtab() 312 { 313 register int segno; 314 register struct symtab *sp, **cosp, *ub; 315 char *tagstring(); 316 317 printf("Symbol Table dump:\n"); 318 for (segno = 0; segno < NLOC + NLOC; segno++){ 319 printf("Segment number: %d\n", segno); 320 SEGITERATE(segno, 0, 0, cosp, sp, ub, ++){ 321 printf("\tSeg: %d \"%s\" value: %d index: %d tag %s\n", 322 segno, FETCHNAME(sp), 323 sp->s_value, sp->s_index, 324 tagstring(sp->s_tag)); 325 printf("\t\ttype: %d jxbump %d jxfear: %d\n", 326 sp->s_type, sp->s_jxbump, sp->s_jxfear); 327 } 328 printf("\n\n"); 329 } 330 } 331 332 static char tagbuff[4]; 333 334 char *tagstring(tag) 335 unsigned char tag; 336 { 337 switch(tag){ 338 case JXACTIVE: return("active"); 339 case JXNOTYET: return("notyet"); 340 case JXALIGN: return("align"); 341 case JXQUESTIONABLE: return("jxquestionable"); 342 case JXINACTIVE: return("inactive"); 343 case JXTUNNEL: return("tunnel"); 344 case OBSOLETE: return("obsolete"); 345 case IGNOREBOUND: return("ignorebound"); 346 case STABFLOATING: return("stabfloating"); 347 case STABFIXED: return("stabfixed"); 348 case LABELID: return("labelid"); 349 case OKTOBUMP: return("oktobump"); 350 case ISET: return("iset"); 351 case ILSYM: return("ilsym"); 352 default: sprintf(tagbuff,"%d", tag); 353 return(tagbuff); 354 } 355 } 356 #endif DEBUG 357 358 htaballoc() 359 { 360 register struct hashdallop *new; 361 new = (struct hashdallop *)ClearCalloc(1, sizeof (struct hashdallop)); 362 if (htab == 0) 363 htab = new; 364 else { /* add AFTER the 1st slot */ 365 new->h_next = htab->h_next; 366 htab->h_next = new; 367 } 368 } 369 370 #define HASHCLOGGED (NHASH / 2) 371 372 /* 373 * Lookup a symbol stored in extern yytext. 374 * All strings passed in via extern yytext had better have 375 * a trailing null. Strings are placed in yytext for hashing by 376 * syminstall() and by yylex(); 377 * 378 * We take pains to avoid function calls; this functdion 379 * is called quite frequently, and the calls overhead 380 * in the vax contributes significantly to the overall 381 * execution speed of as. 382 */ 383 struct symtab **lookup(instflg) 384 int instflg; /* 0: don't install */ 385 { 386 static int initialprobe; 387 register struct symtab **hp; 388 register char *from; 389 register char *to; 390 register int len; 391 register int nprobes; 392 static struct hashdallop *hdallop; 393 static struct symtab **emptyslot; 394 static struct hashdallop *emptyhd; 395 static struct symtab **hp_ub; 396 397 emptyslot = 0; 398 for (nprobes = 0, from = yytext; 399 *from; 400 nprobes <<= 2, nprobes += *from++) 401 continue; 402 nprobes += from[-1] << 5; 403 nprobes %= NHASH; 404 if (nprobes < 0) 405 nprobes += NHASH; 406 407 initialprobe = nprobes; 408 for (hdallop = htab; hdallop != 0; hdallop = hdallop->h_next){ 409 for (hp = &(hdallop->h_htab[initialprobe]), 410 nprobes = 1, 411 hp_ub = &(hdallop->h_htab[NHASH]); 412 (*hp) && (nprobes < NHASH); 413 hp += nprobes, 414 hp -= (hp >= hp_ub) ? NHASH:0, 415 nprobes += 2) 416 { 417 from = yytext; 418 to = FETCHNAME(*hp); 419 while (*from && *to) 420 if (*from++ != *to++) 421 goto nextprobe; 422 if (*to == *from) /*assert both are == 0*/ 423 return(hp); 424 nextprobe: ; 425 } 426 if (*hp == 0 && emptyslot == 0 && 427 hdallop->h_nused < HASHCLOGGED) { 428 emptyslot = hp; 429 emptyhd = hdallop; 430 } 431 } 432 if (emptyslot == 0) { 433 htaballoc(); 434 hdallop = htab->h_next; /* aren't we smart! */ 435 hp = &hdallop->h_htab[initialprobe]; 436 } else { 437 hdallop = emptyhd; 438 hp = emptyslot; 439 } 440 if (instflg) { 441 *hp = symalloc(); 442 hdallop->h_nused++; 443 for (from = yytext, len = 0; *from++; len++) 444 continue; 445 (*hp)->s_name = (char *)savestr(yytext, len + 1, STR_BOTH); 446 } 447 return(hp); 448 } /*end of lookup*/ 449 /* 450 * save a string str with len in the places indicated by place 451 */ 452 struct strdesc *savestr(str, len, place) 453 char *str; 454 int len; 455 int place; 456 { 457 reg struct strdesc *res; 458 int tlen; 459 /* 460 * Compute the total length of the record to live in core 461 */ 462 tlen = sizeof(struct strdesc) - sizeof(res->sd_string); 463 if (place & STR_CORE) 464 tlen += len; 465 /* 466 * See if there is enough space for the record, 467 * and allocate the record. 468 */ 469 if (tlen >= (STRPOOLDALLOP - strplhead->str_nalloc)) 470 strpoolalloc(); 471 res = (struct strdesc *)(strplhead->str_names + strplhead->str_nalloc); 472 /* 473 * Save the string information that is always present 474 */ 475 res->sd_stroff = strfilepos; 476 res->sd_strlen = len; 477 res->sd_place = place; 478 /* 479 * Now, save the string itself. If str is null, then 480 * the characters have already been dumped to the file 481 */ 482 if ((place & STR_CORE) && str) 483 movestr(res[0].sd_string, str, len); 484 if (place & STR_FILE){ 485 if (str){ 486 fwrite(str, 1, len, strfile); 487 } 488 strfilepos += len; 489 } 490 /* 491 * Adjust the in core string pool size 492 */ 493 strplhead->str_nalloc += tlen; 494 return(res); 495 } 496 /* 497 * The relocation information is saved internally in an array of 498 * lists of relocation buffers. The relocation buffers are 499 * exactly the same size as a token buffer; if we use VM for the 500 * temporary file we reclaim this storage, otherwise we create 501 * them by mallocing. 502 */ 503 #define RELBUFLG TOKBUFLG 504 #define NRELOC ((TOKBUFLG - \ 505 (sizeof (int) + sizeof (struct relbufdesc *)) \ 506 ) / (sizeof (struct relocation_info))) 507 508 struct relbufdesc{ 509 int rel_count; 510 struct relbufdesc *rel_next; 511 struct relocation_info rel_reloc[NRELOC]; 512 }; 513 extern struct relbufdesc *tok_free; 514 #define rel_free tok_free 515 static struct relbufdesc *rel_temp; 516 struct relocation_info r_can_1PC; 517 struct relocation_info r_can_0PC; 518 519 initoutrel() 520 { 521 r_can_0PC.r_address = 0; 522 r_can_0PC.r_symbolnum = 0; 523 r_can_0PC.r_pcrel = 0; 524 r_can_0PC.r_length = 0; 525 r_can_0PC.r_extern = 0; 526 527 r_can_1PC = r_can_0PC; 528 r_can_1PC.r_pcrel = 1; 529 } 530 531 outrel(xp, reloc_how) 532 register struct exp *xp; 533 int reloc_how; /* TYPB..TYPH + (possibly)RELOC_PCREL */ 534 { 535 struct relocation_info reloc; 536 register int x_type_mask; 537 int pcrel; 538 539 x_type_mask = xp->e_xtype & ~XFORW; 540 pcrel = reloc_how & RELOC_PCREL; 541 reloc_how &= ~RELOC_PCREL; 542 543 if (bitoff&07) 544 yyerror("Padding error"); 545 if (x_type_mask == XUNDEF) 546 yyerror("Undefined reference"); 547 548 if ( (x_type_mask != XABS) || pcrel ) { 549 if (ty_NORELOC[reloc_how]) 550 yyerror("Illegal Relocation of floating or large int number."); 551 reloc = pcrel ? r_can_1PC : r_can_0PC; 552 reloc.r_address = dotp->e_xvalue - 553 ( (dotp < &usedot[NLOC] || readonlydata) ? 0 : datbase ); 554 reloc.r_length = ty_nlg[reloc_how]; 555 switch(x_type_mask){ 556 case XXTRN | XUNDEF: 557 reloc.r_symbolnum = xp->e_xname->s_index; 558 reloc.r_extern = 1; 559 break; 560 default: 561 if (readonlydata && (x_type_mask&~XXTRN) == XDATA) 562 x_type_mask = XTEXT | (x_type_mask&XXTRN); 563 reloc.r_symbolnum = x_type_mask; 564 break; 565 } 566 if ( (relfil == 0) || (relfil->rel_count >= NRELOC) ){ 567 if (rel_free){ 568 rel_temp = rel_free; 569 rel_free = rel_temp->rel_next; 570 } else { 571 rel_temp = (struct relbufdesc *) 572 Calloc(1,sizeof (struct relbufdesc)); 573 } 574 rel_temp->rel_count = 0; 575 rel_temp->rel_next = relfil; 576 relfil = rusefile[dotp - &usedot[0]] = rel_temp; 577 } 578 relfil->rel_reloc[relfil->rel_count++] = reloc; 579 } 580 /* 581 * write the unrelocated value to the text file 582 */ 583 dotp->e_xvalue += ty_nbyte[reloc_how]; 584 if (pcrel) 585 xp->e_xvalue -= dotp->e_xvalue; 586 switch(reloc_how){ 587 case TYPO: 588 case TYPQ: 589 590 case TYPF: 591 case TYPD: 592 case TYPG: 593 case TYPH: 594 bignumwrite(xp->e_number, reloc_how); 595 break; 596 597 default: 598 bwrite((char *)&(xp->e_xvalue), ty_nbyte[reloc_how], txtfil); 599 break; 600 } 601 } 602 /* 603 * Flush out all of the relocation information. 604 * Note that the individual lists of buffers are in 605 * reverse order, so we must reverse them 606 */ 607 off_t closeoutrel(relocfile) 608 BFILE *relocfile; 609 { 610 int locindex; 611 u_long Closeoutrel(); 612 613 trsize = 0; 614 for (locindex = 0; locindex < NLOC; locindex++){ 615 trsize += Closeoutrel(rusefile[locindex], relocfile); 616 } 617 drsize = 0; 618 for (locindex = 0; locindex < NLOC; locindex++){ 619 drsize += Closeoutrel(rusefile[NLOC + locindex], relocfile); 620 } 621 return(trsize + drsize); 622 } 623 624 u_long Closeoutrel(relfil, relocfile) 625 struct relbufdesc *relfil; 626 BFILE *relocfile; 627 { 628 u_long tail; 629 if (relfil == 0) 630 return(0L); 631 tail = Closeoutrel(relfil->rel_next, relocfile); 632 bwrite((char *)&relfil->rel_reloc[0], 633 relfil->rel_count * sizeof (struct relocation_info), 634 relocfile); 635 return(tail + relfil->rel_count * sizeof (struct relocation_info)); 636 } 637 638 #define NOUTSYMS (nsyms - njxxx - nforgotten - (savelabels ? 0 : nlabels)) 639 int sizesymtab() 640 { 641 return (sizeof (struct nlist) * NOUTSYMS); 642 } 643 /* 644 * Write out n symbols to file f, beginning at p 645 * ignoring symbols that are obsolete, jxxx instructions, and 646 * possibly, labels 647 */ 648 int symwrite(symfile) 649 BFILE *symfile; 650 { 651 int symsout; /*those actually written*/ 652 int symsdesired = NOUTSYMS; 653 reg struct symtab *sp, *ub; 654 char *name; /* temp to save the name */ 655 int nread; 656 char rbuf[2048]; 657 int i; 658 /* 659 * We use sp->s_index to hold the length of the 660 * name; it isn't used for anything else 661 */ 662 register struct allocbox *allocwalk; 663 664 symsout = 0; 665 DECLITERATE(allocwalk, sp, ub) 666 { 667 if (sp->s_tag >= IGNOREBOUND) 668 continue; 669 if ((FETCHNAME(sp)[0] == 'L') && (sp->s_tag == LABELID) && !savelabels) 670 continue; 671 symsout++; 672 673 name = sp->s_name; /* save pointer */ 674 /* 675 * the length of the symbol table string 676 * always includes the trailing null 677 */ 678 if (sp->s_name && (sp->s_index = STRLEN(sp))){ 679 sp->s_nmx = STROFF(sp); /* clobber */ 680 } else { 681 sp->s_nmx = 0; 682 } 683 sp->s_type = (sp->s_ptype != 0) ? sp->s_ptype : (sp->s_type & (~XFORW)); 684 if (readonlydata && (sp->s_type&~N_EXT) == N_DATA) 685 sp->s_type = N_TEXT | (sp->s_type & N_EXT); 686 bwrite((char *)&sp->s_nm, sizeof (struct nlist), symfile); 687 sp->s_name = name; /* restore pointer */ 688 } 689 if (symsout != symsdesired) 690 yyerror("INTERNAL ERROR: Wrote %d symbols, wanted to write %d symbols\n", 691 symsout, symsdesired); 692 /* 693 * Copy the string temporary file to the symbol file, 694 * copying all the strings and symbols we ever saw, 695 * including labels, stabs strings, ascii strings, etc. 696 * This is slightly wasteful. 697 */ 698 i = 0; 699 while((nread = read(strfile->_file, rbuf, sizeof(rbuf))) > 0){ 700 if (i == 0){ 701 ((int *)rbuf)[0] = strfilepos; 702 } 703 bwrite(rbuf, nread, symfile); 704 i++; 705 } 706 } 707