2 * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
3 * See the copyright notice in the ACK home directory, in the file "Copyright".
11 static char rcs_id[] = "$Id: assrl.c,v 2.4 1994/06/24 10:15:39 ceriel Exp $" ;
18 * collection of routines to deal with relocation business
24 text_reloc(glosym,off,typ) glob_t *glosym; FOFFSET off ; int typ ; {
27 * prepare the relocation that has to be done at text-offset off
28 * according to global symbol glosym.
29 * NOTE: The pointer glosym will point into mglobs[], while at
30 * the time copyout() is called all the symbols here
31 * will have disappeared.
32 * The procedure upd_reloc() will change this pointer
33 * into the one in xglobs[] later.
36 register relc_t *nxtextreloc ;
38 nxtextreloc= rlp_cast getarea(sizeof *nxtextreloc) ;
42 l_text->r_next= nxtextreloc ;
44 nxtextreloc->r_next= rlp_cast 0 ;
46 nxtextreloc->r_off = off;
47 nxtextreloc->r_val.rel_gp = glosym;
48 nxtextreloc->r_typ = typ; /* flags of instruction */
53 data_reloc(arg,off,typ) char *arg ; FOFFSET off ; int typ ; {
59 register relc_t *nxdatareloc ;
61 nxdatareloc= rlp_cast getarea(sizeof *nxdatareloc) ;
65 l_data->r_next= nxdatareloc ;
67 nxdatareloc->r_next= rlp_cast 0 ;
69 nxdatareloc->r_off = off;
70 nxdatareloc->r_val.rel_lp = lbp_cast arg;
71 nxdatareloc->r_typ = typ;
80 * Make the e.out file that looks as follows:
82 * __________________________
86 * | VERSION | | 8*(2-byte word) header
87 * | WORDSIZE | | for interpreter selection
94 * | ENTRY-POINT | | 8*(wordsize-word) header
95 * | NLINES | | for interpreter proper
99 * |________________________|
103 * |_ _ _ _ _ _ _ _ _ _ _ _ |
105 * | TEXT | zero filled
106 * | | if not word multiple
107 * |________________________|
111 * |________________________|
116 procbytes = procnum * 3 * ptrsize;
117 remtext = textbytes%wordsize ;
118 if ( remtext != 0 ) remtext = wordsize-remtext ;
120 if ((ifile = fopen(eout,"w")) == 0 )
121 fatal("can't create e.out");
123 fclose(tfile); tfile=fopen("TFILE.$$$", "r");
124 fclose(dfile); dfile=fopen("DFILE.$$$", "r");
126 tfile=frewind(tfile);
127 dfile=frewind(dfile);
129 xput16(as_magic,ifile);
130 xput16(intflags,ifile);
131 xput16(unresolved,ifile);
132 xput16(VERSION,ifile);
133 xput16(wordsize,ifile);
134 xput16(ptrsize,ifile);
137 xputa(procbytes+textbytes+remtext,ifile);
138 xputa((cons_t)datablocks,ifile);
139 xputa((cons_t)procnum,ifile);
140 xputa((cons_t)searchproc(MAIN,xprocs,oursize->n_xproc)->p_num,
142 xputa((cons_t)sourcelines,ifile);
143 xputa((cons_t)databytes,ifile);
144 xputa((cons_t)0,ifile);
145 xputa((cons_t)0,ifile);
147 for (i=0;i<procnum;i++) {
148 xputarb(ptrsize,proctab[i].pr_loc,ifile);
149 xputarb(ptrsize,proctab[i].pr_off+procbytes,ifile);
150 xputarb(ptrsize,proctab[i].pr_len,ifile);
153 textprocess(tfile,ifile);
154 while ( remtext-- ) xputc(0,ifile) ;
156 dataprocess(dfile,ifile);
157 if ( fclose(ifile)==EOF ) ;
160 dataprocess(f1,f2) FILE *f1,*f2; {
166 fclose(rdfile); rdfile=fopen("RDFILE.$$$", "r");
168 rdfile=frewind(rdfile) ;
170 ieof=getblk(rdfile,(char *)(&datareloc.r_off),
171 sizeof datareloc - sizeof datareloc.r_next) ;
172 for (i=0 ; i<dataoff && !ieof ; i++) {
173 if (i==datareloc.r_off) {
174 switch(datareloc.r_typ) {
176 datareloc.r_val.rel_i += procbytes;
179 xputa(xgeta(f1)+datareloc.r_val.rel_i,f2) ;
183 if (datareloc.r_val.rel_gp->g_status&DEF) {
185 datareloc.r_val.rel_gp->g_val.g_addr,
190 if ( unresolved == 0 )
191 fatal("Definition botch") ;
193 xputc((int)(xgetc(f1)+datareloc.r_val.rel_i),
197 fatal("Bad r_typ in dataprocess");
199 ieof=getblk(rdfile,(char *)(&datareloc.r_off),
200 sizeof datareloc - sizeof datareloc.r_next) ;
204 for ( ; i<dataoff ; i++ ) xputc(xgetc(f1),f2) ;
205 if ( !ieof && !getblk(rdfile,(char *)&datareloc,1) )
206 fatal("data relocation botch") ;
209 textprocess(f1,f2) FILE *f1,*f2; {
214 int insl ; register int ieof ;
216 register FOFFSET keep ;
219 fclose(rtfile); rtfile=fopen("RTFILE.$$$", "r");
221 rtfile=frewind(rtfile) ;
223 keep = textoff ; textoff=0 ; otfile=tfile ; tfile=f2 ;
224 /* This redirects the output of genop */
225 ieof=getblk(rtfile,(char *)(&textreloc.r_off),
226 sizeof textreloc - sizeof textreloc.r_next) ;
227 for(i=0;i<keep && !ieof ;i++) {
228 if( i == textreloc.r_off ) {
229 if (textreloc.r_typ&RELMNS) {
230 n=textreloc.r_val.rel_i;
232 if (textreloc.r_val.rel_gp->g_status&DEF) {
233 n=textreloc.r_val.rel_gp->g_val.g_addr;
236 fatal("Definition botch") ;
237 xputc(xgetc(f1),f2) ;
238 ieof=getblk(rtfile,(char *)(&textreloc.r_off),
239 sizeof textreloc-sizeof textreloc.r_next);
243 op_curr = &opchoice[textreloc.r_typ& ~RELMNS] ;
244 insl = oplength(*op_curr) ;
245 genop(op_curr, n+xgetarb(insl,f1), PAR_G);
247 ieof=getblk(rtfile,(char *)(&textreloc.r_off),
248 sizeof textreloc - sizeof textreloc.r_next) ;
250 xputc(xgetc(f1),f2) ;
253 for ( ; i<keep ; i++ ) xputc(xgetc(f1),f2) ;
254 if ( !ieof && !getblk(rtfile,(char *)&textreloc,1) )
255 fatal("text relocation botch") ;
262 register glob_t *gbp;
265 * Change reloc-tables such that for every pointer into mglobs
266 * either the corresponding pointer into xglobs or its value
269 * Use is made of the known order of mglobs and xglobs
273 while ( p= f_text ) {
274 gbp= p->r_val.rel_gp ;
275 if( gbp->g_status&DEF ) {
277 p->r_val.rel_i = gbp->g_val.g_addr;
279 p->r_val.rel_gp = gbp->g_val.g_gp;
280 putblk(rtfile,(char *)(&(p->r_off)),sizeof *p - sizeof p) ;
281 f_text= p->r_next ; freearea( (area_t) p , sizeof *p ) ;
285 if (p->r_typ == RELGLO) {
286 gbp= p->r_val.rel_gp ;
287 if(gbp->g_status&DEF) {
289 p->r_val.rel_i = gbp->g_val.g_addr;
291 p->r_val.rel_gp = gbp->g_val.g_gp;
293 putblk(rdfile,(char *)(&(p->r_off)),sizeof *p - sizeof p) ;
294 f_data= p->r_next ; freearea( (area_t) p , sizeof *p ) ;