1 /* 2 * Copyright (c) 1982 Regents of the University of California 3 */ 4 #ifndef lint 5 static char sccsid[] = "@(#)asmain.c 4.9 02/14/82"; 6 #endif not lint 7 8 #include <stdio.h> 9 #include <ctype.h> 10 #include <signal.h> 11 12 #include "as.h" 13 #include "assyms.h" 14 #include "asscan.h" 15 #include "asexpr.h" 16 17 #ifdef UNIX 18 #define unix_lang_name "VAX/UNIX Assembler V02/14/82 4.9" 19 #endif 20 21 #ifdef VMS 22 #define vms_lang_name "VAX/VMS C Assembler V1.00" 23 #endif VMS 24 25 /* 26 * variables to manage reading the assembly source files 27 */ 28 char *dotsname; /*the current file name; managed by the parser*/ 29 int lineno; /*current line number; managed by the parser*/ 30 char **innames; /*names of the files being assembled*/ 31 int ninfiles; /*how many interesting files there are*/ 32 /* 33 * Flags settable from the argv process argument list 34 */ 35 int silent = 0; /*don't complain about any errors*/ 36 int savelabels = 0; /*write the labels to the a.out file*/ 37 int d124 = 4; /*default allocate 4 bytes for unknown pointers*/ 38 int anyerrs = 0; /*no errors yet*/ 39 int anywarnings=0; /*no warnings yet*/ 40 int orgwarn = 0; /*Bad origins*/ 41 int passno = 1; /* current pass*/ 42 int jxxxJUMP = 0; /* in jxxxes that branch too far, use jmp instead of brw */ 43 int readonlydata = 0; /* initialzed data -> text space */ 44 45 int nGHnumbers = 0; /* GH numbers used */ 46 int nGHopcodes = 0; /* GH opcodes used */ 47 int nnewopcodes = 0; /* new opcodes used */ 48 49 #ifdef DEBUG 50 int debug = 0; 51 int toktrace = 0; 52 #endif 53 54 int useVM = /*put the temp file in virtual memory*/ 55 #ifdef VMS 56 1; /*VMS has virtual memory (duh)*/ 57 #endif VMS 58 #ifdef UNIX 59 0; 60 #endif 61 62 char *endcore; /*where to get more symbol space*/ 63 64 /* 65 * Managers of the a.out file. 66 */ 67 struct exec hdr; 68 #define MAGIC 0407 69 u_long tsize; /* total text size */ 70 u_long dsize; /* total data size */ 71 u_long datbase; /* base of the data segment */ 72 u_long trsize; /* total text relocation size */ 73 u_long drsize; /* total data relocation size */ 74 75 /* 76 * Information about the current segment is accumulated in 77 * usedot; the most important information stored is the 78 * accumulated size of each of the text and data segments 79 * 80 * dotp points to the correct usedot expression for the current segment 81 */ 82 struct exp usedot[NLOC+NLOC]; /* info about all segments */ 83 struct exp *dotp; /* data/text location pointer */ 84 /* 85 * The inter pass temporary file is opened and closed by stdio, but 86 * is written to using direct read/write, as the temporary file 87 * is composed of buffers exactly BUFSIZ long. 88 */ 89 FILE *tmpfil; /* interpass communication file */ 90 /* 91 * a.out is created during the second pass. 92 * It is opened by stdio, but is filled with the parallel 93 * block I/O library 94 */ 95 char *outfile = "a.out"; 96 FILE *a_out_file; 97 off_t a_out_off; /* cumulative offsets for segments */ 98 /* 99 * The logical files containing the assembled data for each of 100 * the text and data segments are 101 * managed by the parallel block I/O library. 102 * a.out is logically opened in many places at once to 103 * receive the assembled data from the various segments as 104 * it all trickles in, but is physically opened only once 105 * to minimize file overhead. 106 */ 107 BFILE *usefile[NLOC+NLOC]; /* text/data files */ 108 BFILE *txtfil; /* current text/data file */ 109 /* 110 * Relocation information is accumulated seperately for each 111 * segment. This is required by the old loader (from BTL), 112 * but not by the new loader (Bill Joy). 113 * 114 * However, the size of the relocation information can not be computed 115 * during or after the 1st pass because the ''absoluteness' of values 116 * is unknown until all locally declared symbols have been seen. 117 * Thus, the size of the relocation information is only 118 * known after the second pass is finished. 119 * This obviates the use of the block I/O 120 * library, which requires knowing the exact offsets in a.out. 121 * 122 * So, we save the relocation information internally (we don't 123 * go to internal files to minimize overhead). 124 * 125 * Empirically, we studied 259 files composing the system, 126 * two compilers and a compiler generator: (all of which have 127 * fairly large source files) 128 * 129 * Number of files = 259 130 * Number of non zero text reloc files: 233 131 * Number of non zero data reloc files: 53 132 * Average text relocation = 889 133 * Average data relocation = 346 134 * Number of files > BUFSIZ text relocation = 71 135 * Number of files > BUFSIZ data relocation = 6 136 * 137 * For compiled C code, there is usually one text segment and two 138 * data segments; we see that allocating our own buffers and 139 * doing our internal handling of relocation information will, 140 * on the average, not use more memory than taken up by the buffers 141 * allocated for doing file I/O in parallel to a number of file. 142 * 143 * If we are assembling with the -V option, we 144 * use the left over token buffers from the 2nd pass, 145 * otherwise, we create our own. 146 * 147 * When the 2nd pass is complete, closeoutrel flushes the token 148 * buffers out to a BFILE. 149 * 150 * The internals to relbufdesc are known only in assyms.c 151 * 152 * outrel constructs the relocation information. 153 * closeoutrel flushes the relocation information to relfil. 154 */ 155 struct relbufdesc *rusefile[NLOC+NLOC]; 156 struct relbufdesc *relfil; /* un concatnated relocation info */ 157 BFILE *relocfile; /* concatnated relocation info */ 158 /* 159 * Once the relocation information has been written, 160 * we can write out the symbol table using the Block I/O 161 * mechanisms, as we once again know the offsets into 162 * the a.out file. 163 * 164 * We use relfil to output the symbol table information. 165 */ 166 167 char *tmpdirprefix = 168 #ifdef UNIX 169 "/tmp/"; 170 #else VMS 171 "/usr/tmp/"; 172 #endif 173 174 #define TMP_SUFFIX "asXXXXXX" 175 char tmpn1[TNAMESIZE]; 176 177 int delexit(); 178 179 main(argc, argv) 180 int argc; 181 char **argv; 182 { 183 char *sbrk(); 184 185 tmpn1[0] = 0; 186 endcore = sbrk(0); 187 188 argprocess(argc, argv); /* process argument lists */ 189 if (anyerrs) exit(1); 190 191 initialize(); 192 zeroorigins(); /* set origins to zero */ 193 zerolocals(); /* fix local label counters */ 194 195 i_pass1(); /* open temp files, etc */ 196 pass1(); /* first pass through .s files */ 197 testlocals(); /* check for undefined locals */ 198 if (anyerrs) delexit(); 199 200 pass1_5(); /* resolve jxxx */ 201 if (anyerrs) delexit(); 202 203 open_a_out(); /* open a.out */ 204 roundsegments(); /* round segments to FW */ 205 build_hdr(); /* build initial header, and output */ 206 207 i_pass2(); /* reopen temporary file, etc */ 208 pass2(); /* second pass through the virtual .s */ 209 if (anyerrs) delexit(); 210 211 fillsegments(); /* fill segments with 0 to FW */ 212 reloc_syms(); /* dump relocation and symbol table */ 213 214 delete(); /* remove tmp file */ 215 bflush(); /* close off block I/O view of a.out */ 216 fix_a_out(); /* add in text and data reloc counts */ 217 218 if (anyerrs == 0 && orgwarn) 219 yyerror("Caution: absolute origins.\n"); 220 221 if (nGHnumbers) 222 yywarning("Caution: G or H format floating point numbers"); 223 if (nGHopcodes) 224 yywarning("Caution: G or H format floating point operators"); 225 if (nnewopcodes) 226 yywarning("Caution: New Opcodes"); 227 if (nGHnumbers || nGHopcodes || nnewopcodes) 228 yywarning("These are not defined for all implementations of the VAX architecture.\n"); 229 230 exit(anyerrs != 0); 231 } /*end of UNIX main*/ 232 233 argprocess(argc, argv) 234 int argc; 235 char *argv[]; 236 { 237 register char *cp; 238 239 ninfiles = 0; 240 silent = 0; 241 #ifdef DEBUG 242 debug = 0; 243 #endif 244 innames = (char **)ClearCalloc(argc+1, sizeof (innames[0])); 245 dotsname = "<argv error>"; 246 while (argc > 1) { 247 if (argv[1][0] != '-') 248 innames[ninfiles++] = argv[1]; 249 else { 250 cp = argv[1] + 1; 251 /* 252 * We can throw away single minus signs, so 253 * that make scripts for the PDP 11 assembler work 254 * on this assembler too 255 */ 256 while (*cp){ 257 switch(*cp++){ 258 default: 259 yyerror("Unknown flag: %c", *--cp); 260 cp++; 261 break; 262 case 'd': 263 d124 = *cp++ - '0'; 264 if ( (d124 != 1) && (d124 != 2) && 265 (d124 != 4)){ 266 yyerror("-d[124] only"); 267 exit(1); 268 } 269 break; 270 case 'o': 271 if (argc < 3){ 272 yyerror("-o what???"); 273 exit(1); 274 } 275 outfile = argv[2]; 276 bumpone: 277 argc -= 2; 278 argv += 2; 279 goto nextarg; 280 281 case 't': 282 if (argc < 3){ 283 yyerror("-t what???"); 284 exit(1); 285 } 286 tmpdirprefix = argv[2]; 287 goto bumpone; 288 289 case 'V': 290 useVM = 1; 291 break; 292 case 'W': 293 silent = 1; 294 break; 295 case 'L': 296 savelabels = 1; 297 break; 298 case 'J': 299 jxxxJUMP = 1; 300 break; 301 #ifdef DEBUG 302 case 'D': 303 debug = 1; 304 break; 305 case 'T': 306 toktrace = 1; 307 break; 308 #endif 309 case 'R': 310 readonlydata = 1; 311 break; 312 } /*end of the switch*/ 313 } /*end of pulling out all arguments*/ 314 } /*end of a flag argument*/ 315 --argc; ++argv; 316 nextarg:; 317 } 318 /* innames[ninfiles] = 0; */ 319 } 320 321 initialize() 322 { 323 if (signal(SIGINT, SIG_IGN) != SIG_IGN) 324 signal(SIGINT, delexit); 325 /* 326 * Install symbols in the table 327 */ 328 symtabinit(); 329 syminstall(); 330 /* 331 * Build the expression parser accelerator token sets 332 */ 333 buildtokensets(); 334 } 335 336 zeroorigins() 337 { 338 register int locindex; 339 /* 340 * Mark usedot: the first NLOC slots are for named text segments, 341 * the next for named data segments. 342 */ 343 for (locindex = 0; locindex < NLOC; locindex++){ 344 usedot[locindex].e_xtype = XTEXT; 345 usedot[NLOC + locindex].e_xtype = XDATA; 346 usedot[locindex].e_xvalue = 0; 347 usedot[NLOC + locindex].e_xvalue = 0; 348 } 349 } 350 351 zerolocals() 352 { 353 register int i; 354 355 for (i = 0; i <= 9; i++) { 356 lgensym[i] = 1; 357 genref[i] = 0; 358 } 359 } 360 361 i_pass1() 362 { 363 if (useVM == 0){ 364 strcat(tmpn1, tmpdirprefix); 365 if (tmpdirprefix[strlen(tmpdirprefix)-1] != '/') 366 strcat(tmpn1, "/"); 367 (void)strcat(tmpn1, TMP_SUFFIX); 368 (void)mktemp(tmpn1); 369 tmpfil = fopen(tmpn1, "w"); 370 if (tmpfil==NULL) { 371 yyerror("Bad pass 1 temporary file for writing %s", tmpn1); 372 delexit(); 373 } 374 } 375 376 inittmpfile(); 377 initijxxx(); 378 } 379 380 pass1() 381 { 382 register int i; 383 384 passno = 1; 385 dotp = &usedot[0]; 386 txtfil = (BFILE *)0; 387 relfil = (struct relbufdesc *)0; 388 389 if (ninfiles == 0){ /*take the input from stdin directly*/ 390 lineno = 1; 391 dotsname = "<stdin>"; 392 393 yyparse(); 394 } else { /*we have the names tanked*/ 395 for (i = 0; i < ninfiles; i++){ 396 new_dot_s(innames[i]); 397 if (freopen(innames[i], "r", stdin) == NULL) { 398 yyerror( "Can't open source file %s\n", 399 innames[i]); 400 exit(2); 401 } 402 /* stdio is NOT used to read the input characters */ 403 /* we use read directly, into our own buffers */ 404 yyparse(); 405 } 406 } 407 408 closetmpfile(); /*kick out the last buffered intermediate text*/ 409 } 410 411 testlocals() 412 { 413 register int i; 414 for (i = 0; i <= 9; i++) { 415 if (genref[i]) 416 yyerror("Reference to undefined local label %df", i); 417 lgensym[i] = 1; 418 genref[i] = 0; 419 } 420 } 421 422 pass1_5() 423 { 424 sortsymtab(); 425 #ifdef DEBUG 426 if (debug) dumpsymtab(); 427 #endif 428 jxxxfix(); 429 #ifdef DEBUG 430 if (debug) dumpsymtab(); 431 #endif 432 } 433 434 open_a_out() 435 { 436 /* 437 * Open up the a.out file now, and get set to build 438 * up offsets into it for all of the various text,data 439 * text relocation and data relocation segments. 440 */ 441 a_out_file = fopen(outfile, "w"); 442 if (a_out_file == NULL) { 443 yyerror("Cannot create %s", outfile); 444 delexit(); 445 } 446 biofd = a_out_file->_file; 447 a_out_off = 0; 448 } 449 450 roundsegments() 451 { 452 register int locindex; 453 register long v; 454 /* 455 * round and assign text segment origins 456 * the exec header always goes in usefile[0] 457 */ 458 tsize = 0; 459 for (locindex=0; locindex<NLOC; locindex++) { 460 v = round(usedot[locindex].e_xvalue, FW); 461 usedot[locindex].e_xvalue = tsize; 462 if ((locindex == 0) || (v != 0) ){ 463 usefile[locindex] = (BFILE *)Calloc(1, sizeof(BFILE)); 464 bopen(usefile[locindex], a_out_off); 465 if (locindex == 0) 466 a_out_off = sizeof (struct exec); 467 } else { 468 usefile[locindex] = (BFILE *)-1; 469 } 470 tsize += v; 471 a_out_off += v; 472 } 473 /* 474 * Round and assign data segment origins. 475 */ 476 datbase = round(tsize, FW); 477 for (locindex=0; locindex<NLOC; locindex++) { 478 v = round(usedot[NLOC+locindex].e_xvalue, FW); 479 usedot[NLOC+locindex].e_xvalue = datbase + dsize; 480 if (v != 0){ 481 usefile[NLOC + locindex] = (BFILE *)Calloc(1,sizeof(BFILE)); 482 bopen(usefile[NLOC + locindex], a_out_off); 483 } else { 484 usefile[NLOC + locindex] = (BFILE *)-1; 485 } 486 dsize += v; 487 a_out_off += v; 488 } 489 /* 490 * Assign final values to symbols 491 */ 492 hdr.a_bss = dsize; 493 freezesymtab(); /* this touches hdr.a_bss */ 494 stabfix(); 495 /* 496 * Set up the relocation information "files" to 497 * be zero; outrel takes care of the rest 498 */ 499 for (locindex = 0; locindex < NLOC + NLOC; locindex++){ 500 rusefile[locindex] = (struct relbufdesc *)0; 501 } 502 } 503 504 build_hdr() 505 { 506 /* 507 * Except for the text and data relocation sizes, 508 * calculate the final values for the header 509 * 510 * Write out the initial copy; we to come 511 * back later and patch up a_trsize and a_drsize, 512 * and overwrite this first version of the header. 513 */ 514 hdr.a_magic = MAGIC; 515 hdr.a_text = tsize; 516 hdr.a_data = dsize; 517 hdr.a_bss -= dsize; 518 hdr.a_syms = sizesymtab(); /* Does not include string pool length */ 519 hdr.a_entry = 0; 520 hdr.a_trsize = 0; 521 hdr.a_drsize = 0; 522 523 bwrite((char *)&hdr, sizeof(hdr), usefile[0]); 524 } 525 526 i_pass2() 527 { 528 if (useVM == 0) { 529 fclose(tmpfil); 530 tmpfil = fopen(tmpn1, "r"); 531 if (tmpfil==NULL) { 532 yyerror("Bad pass 2 temporary file for reading %s", tmpn1); 533 delexit(); 534 } 535 } 536 } 537 538 pass2() 539 { 540 #ifdef DEBUG 541 if (debug) 542 printf("\n\n\n\t\tPASS 2\n\n\n\n"); 543 #endif DEBUG 544 passno = 2; 545 lineno = 1; 546 dotp = &usedot[0]; 547 txtfil = usefile[0]; /* already opened (always!) */ 548 relfil = 0; /* outrel takes care of the rest */ 549 initoutrel(); 550 551 inittmpfile(); 552 553 yyparse(); 554 555 closetmpfile(); 556 } 557 558 fillsegments() 559 { 560 int locindex; 561 /* 562 * Round text and data segments to FW by appending zeros 563 */ 564 for (locindex = 0; locindex < NLOC + NLOC; locindex++) { 565 if (usefile[locindex]) { 566 txtfil = usefile[locindex]; 567 dotp = &usedot[locindex]; 568 while (usedot[locindex].e_xvalue & FW) 569 outb(0); 570 } 571 } 572 } 573 574 reloc_syms() 575 { 576 u_long closerelfil(); 577 /* 578 * Move the relocation information to a.out 579 * a_out_off is the offset so far: 580 * exec + text segments + data segments 581 */ 582 relocfile = (BFILE *)Calloc(1,sizeof(BFILE)); 583 bopen(relocfile, a_out_off); 584 a_out_off += closeoutrel(relocfile); 585 586 hdr.a_trsize = trsize; 587 hdr.a_drsize = drsize; 588 if (readonlydata) { 589 hdr.a_text += hdr.a_data; 590 hdr.a_data = 0; 591 hdr.a_trsize += hdr.a_drsize; 592 hdr.a_drsize = 0; 593 } 594 /* 595 * Output the symbol table 596 * and if FLEXNAMES is set, the string pool 597 */ 598 symwrite(relocfile); 599 } 600 601 fix_a_out() 602 { 603 if (lseek(a_out_file->_file, 0L, 0) < 0L) 604 yyerror("Reposition for header rewrite fails"); 605 if (write(a_out_file->_file, (char *)&hdr, sizeof (struct exec)) < 0) 606 yyerror("Rewrite of header fails"); 607 } 608 609 delexit() 610 { 611 delete(); 612 if (passno == 2){ 613 unlink(outfile); 614 } 615 exit(1); 616 } 617 618 delete() 619 { 620 if (useVM == 0 || tmpn1[0]) 621 unlink(tmpn1); 622 } 623 624 sawabort() 625 { 626 char *fillinbuffer(); 627 while (fillinbuffer() != (char *)0) 628 continue; 629 delete(); 630 exit(1); /*although the previous pass will also exit non zero*/ 631 } 632 633 panic(fmt, a1, a2, a3, a4) 634 char *fmt; 635 /*VARARGS 1*/ 636 { 637 yyerror("Assembler panic: bad internal data structure."); 638 yyerror(fmt, a1, a2, a3, a4); 639 delete(); 640 abort(); 641 } 642