📄 asmain.c
字号:
for (cp = (char *)&environ, ub = sbrk(0); cp < ub; cp++){ if (cp[0] != '@') continue; if (cp[1] != '(') continue; if (cp[2] != '#') continue; if (cp[3] != ')') continue; fputc('\t', place); for (cp += 4; cp < ub; cp++){ if (*cp == 0) break; if (*cp == '>') break; if (*cp == '\n') break; fputc(*cp, place); } fputc('\n', place); }}initialize(){ if (signal(SIGINT, SIG_IGN) != SIG_IGN) signal(SIGINT, delexit); /* * Install symbols in the table */ symtabinit(); syminstall(); /* * Build the expression parser accelerator token sets */ buildtokensets(); }zeroorigins(){ register int locindex; /* * Mark usedot: the first NLOC slots are for named text segments, * the next for named data segments. */ for (locindex = 0; locindex < NLOC; locindex++){ usedot[locindex].e_xtype = XTEXT; usedot[NLOC + locindex].e_xtype = XDATA; usedot[locindex].e_xvalue = 0; usedot[NLOC + locindex].e_xvalue = 0; }}zerolocals(){ register int i; for (i = 0; i <= 9; i++) { lgensym[i] = 1; genref[i] = 0; }}i_pass1(){ FILE *tempopen(); if (useVM == 0) tokfile = tempopen(tokfilename, "T"); strfile = tempopen(strfilename, "S"); /* * write out the string length. * This will be overwritten when the * strings are tacked onto the growing a.out file */ strfilepos = sizeof(int); fwrite(&strfilepos, sizeof(int), 1, strfile); inittokfile(); initijxxx();}FILE *tempopen(tname, part) char *tname; char *part;{ FILE *file; sprintf(tname, "%s%sas%s%05d", tmpdirprefix, (tmpdirprefix[strlen(tmpdirprefix)-1] != '/') ? "/" : 0, part, getpid()); file = fopen(tname, "w"); if (file == NULL) { yyerror("Bad pass 1 temporary file for writing %s", tname); delexit(); } return(file);}pass1(){ register int i; passno = 1; dotp = &usedot[0]; txtfil = (BFILE *)0; relfil = (struct relbufdesc *)0; if (ninfiles == 0){ /*take the input from stdin directly*/ lineno = 1; dotsname = "<stdin>"; yyparse(); } else { /*we have the names tanked*/ for (i = 0; i < ninfiles; i++){ new_dot_s(innames[i]); if (freopen(innames[i], "r", stdin) == NULL) { yyerror( "Can't open source file %s\n", innames[i]); exit(2); } /* stdio is NOT used to read the input characters */ /* we use read directly, into our own buffers */ yyparse(); } } closetokfile(); /*kick out the last buffered intermediate text*/}testlocals(){ register int i; for (i = 0; i <= 9; i++) { if (genref[i]) yyerror("Reference to undefined local label %df", i); lgensym[i] = 1; genref[i] = 0; }}pass1_5(){ sortsymtab();#ifdef DEBUG if (debug) dumpsymtab();#endif jxxxfix();#ifdef DEBUG if (debug) dumpsymtab();#endif}open_a_out(){ struct stat stb; /* * Open up the a.out file now, and get set to build * up offsets into it for all of the various text,data * text relocation and data relocation segments. */ a_out_file = fopen(outfile, "w"); if (a_out_file == NULL) { yyerror("Cannot create %s", outfile); delexit(); } biofd = a_out_file->_file; fstat(biofd,&stb); biobufsize = stb.st_blksize; a_out_off = 0;}roundsegments(){ register int locindex; register long v; /* * round and assign text segment origins * the exec header always goes in usefile[0] */ tsize = 0; for (locindex=0; locindex<NLOC; locindex++) { v = round(usedot[locindex].e_xvalue, FW); usedot[locindex].e_xvalue = tsize; if ((locindex == 0) || (v != 0) ){ usefile[locindex] = (BFILE *)Calloc(1, sizeof(BFILE)); bopen(usefile[locindex], a_out_off); if (locindex == 0) a_out_off = sizeof (struct exec); } else { usefile[locindex] = (BFILE *)-1; } tsize += v; a_out_off += v; } /* * Round and assign data segment origins. */ datbase = round(tsize, FW); for (locindex=0; locindex<NLOC; locindex++) { v = round(usedot[NLOC+locindex].e_xvalue, FW); usedot[NLOC+locindex].e_xvalue = datbase + dsize; if (v != 0){ usefile[NLOC + locindex] = (BFILE *)Calloc(1,sizeof(BFILE)); bopen(usefile[NLOC + locindex], a_out_off); } else { usefile[NLOC + locindex] = (BFILE *)-1; } dsize += v; a_out_off += v; } /* * Assign final values to symbols */ hdr.a_bss = dsize; freezesymtab(); /* this touches hdr.a_bss */ stabfix(); /* * Set up the relocation information "files" to * be zero; outrel takes care of the rest */ for (locindex = 0; locindex < NLOC + NLOC; locindex++){ rusefile[locindex] = (struct relbufdesc *)0; }}build_hdr(){ /* * Except for the text and data relocation sizes, * calculate the final values for the header * * Write out the initial copy; we to come * back later and patch up a_trsize and a_drsize, * and overwrite this first version of the header. */ hdr.a_magic = MAGIC; hdr.a_text = tsize; hdr.a_data = dsize; hdr.a_bss -= dsize; hdr.a_syms = sizesymtab(); /* Does not include string pool length */ hdr.a_entry = 0; hdr.a_trsize = 0; hdr.a_drsize = 0; bwrite((char *)&hdr, sizeof(hdr), usefile[0]);}i_pass2(){ if (useVM == 0) { fclose(tokfile); tokfile = fopen(tokfilename, "r"); if (tokfile==NULL) { yyerror("Bad pass 2 temporary file for reading %s", tokfilename); delexit(); } } fclose(strfile); strfile = fopen(strfilename, "r");}pass2(){#ifdef DEBUG if (debug) printf("\n\n\n\t\tPASS 2\n\n\n\n");#endif DEBUG passno = 2; lineno = 1; dotp = &usedot[0]; txtfil = usefile[0]; /* already opened (always!) */ relfil = 0; /* outrel takes care of the rest */ initoutrel(); inittokfile(); yyparse(); closetokfile();}fillsegments(){ int locindex; /* * Round text and data segments to FW by appending zeros */ for (locindex = 0; locindex < NLOC + NLOC; locindex++) { if (usefile[locindex]) { txtfil = usefile[locindex]; dotp = &usedot[locindex]; while (usedot[locindex].e_xvalue & FW) outb(0); } }}reloc_syms(){ u_long closerelfil(); /* * Move the relocation information to a.out * a_out_off is the offset so far: * exec + text segments + data segments */ relocfile = (BFILE *)Calloc(1,sizeof(BFILE)); bopen(relocfile, a_out_off); a_out_off += closeoutrel(relocfile); hdr.a_trsize = trsize; hdr.a_drsize = drsize; if (readonlydata) { hdr.a_text += hdr.a_data; hdr.a_data = 0; hdr.a_trsize += hdr.a_drsize; hdr.a_drsize = 0; } /* * Output the symbol table and the string pool * * We must first rewind the string pool file to its beginning, * in case it was seek'ed into for fetching ascii and asciz * strings. */ fseek(strfile, 0, 0); symwrite(relocfile);}fix_a_out(){ if (lseek(a_out_file->_file, 0L, 0) < 0L) yyerror("Reposition for header rewrite fails"); if (write(a_out_file->_file, (char *)&hdr, sizeof (struct exec)) < 0) yyerror("Rewrite of header fails");}delexit(){ delete(); if (passno == 2){ unlink(outfile); } exit(1);}delete(){ if (useVM == 0 || tokfilename[0]) unlink(tokfilename); if (strfilename[0]) unlink(strfilename);}sawabort(){ char *fillinbuffer(); while (fillinbuffer() != (char *)0) continue; delete(); exit(1); /*although the previous pass will also exit non zero*/}panic(fmt, a1, a2, a3, a4) char *fmt; /*VARARGS 1*/{ yyerror("Assembler panic: bad internal data structure."); yyerror(fmt, a1, a2, a3, a4); delete(); abort();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -