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 * |________________________|
101 * | TEXT | zero filled
102 * | | if not word multiple
103 * |________________________|
107 * |________________________|
111 * |________________________|
116 remtext = textbytes%wordsize ;
117 if ( remtext != 0 ) remtext = wordsize-remtext ;
119 if ((ifile = fopen(eout,"w")) == 0 )
120 fatal("can't create e.out");
122 fclose(tfile); tfile=fopen("TFILE.$$$", "r");
123 fclose(dfile); dfile=fopen("DFILE.$$$", "r");
125 tfile=frewind(tfile);
126 dfile=frewind(dfile);
128 xput16(as_magic,ifile);
129 xput16(intflags,ifile);
130 xput16(unresolved,ifile);
131 xput16(VERSION,ifile);
132 xput16(wordsize,ifile);
133 xput16(ptrsize,ifile);
136 xputa(textbytes+remtext ,ifile);
137 xputa((cons_t)datablocks,ifile);
138 xputa((cons_t)procnum,ifile);
139 xputa((cons_t)searchproc(MAIN,xprocs,oursize->n_xproc)->p_num,
141 xputa((cons_t)sourcelines,ifile);
142 xputa((cons_t)databytes,ifile);
143 xputa((cons_t)0,ifile);
144 xputa((cons_t)0,ifile);
146 textprocess(tfile,ifile);
147 while ( remtext-- ) xputc(0,ifile) ;
149 dataprocess(dfile,ifile);
150 for (i=0;i<procnum;i++) {
151 xputarb(ptrsize,proctab[i].pr_loc,ifile);
152 xputarb(ptrsize,proctab[i].pr_off,ifile);
154 if ( fclose(ifile)==EOF ) ;
157 dataprocess(f1,f2) FILE *f1,*f2; {
163 fclose(rdfile); rdfile=fopen("RDFILE.$$$", "r");
165 rdfile=frewind(rdfile) ;
167 ieof=getblk(rdfile,(char *)(&datareloc.r_off),
168 sizeof datareloc - sizeof datareloc.r_next) ;
169 for (i=0 ; i<dataoff && !ieof ; i++) {
170 if (i==datareloc.r_off) {
171 switch(datareloc.r_typ) {
173 xputa(xgeta(f1)+datareloc.r_val.rel_i,f2) ;
177 if (datareloc.r_val.rel_gp->g_status&DEF) {
179 datareloc.r_val.rel_gp->g_val.g_addr,
184 if ( unresolved == 0 )
185 fatal("Definition botch") ;
187 xputc((int)(xgetc(f1)+datareloc.r_val.rel_i),
191 fatal("Bad r_typ in dataprocess");
193 ieof=getblk(rdfile,(char *)(&datareloc.r_off),
194 sizeof datareloc - sizeof datareloc.r_next) ;
198 for ( ; i<dataoff ; i++ ) xputc(xgetc(f1),f2) ;
199 if ( !ieof && !getblk(rdfile,(char *)&datareloc,1) )
200 fatal("data relocation botch") ;
203 textprocess(f1,f2) FILE *f1,*f2; {
208 int insl ; register int ieof ;
210 register FOFFSET keep ;
213 fclose(rtfile); rtfile=fopen("RTFILE.$$$", "r");
215 rtfile=frewind(rtfile) ;
217 keep = textoff ; textoff=0 ; otfile=tfile ; tfile=f2 ;
218 /* This redirects the output of genop */
219 ieof=getblk(rtfile,(char *)(&textreloc.r_off),
220 sizeof textreloc - sizeof textreloc.r_next) ;
221 for(i=0;i<keep && !ieof ;i++) {
222 if( i == textreloc.r_off ) {
223 if (textreloc.r_typ&RELMNS) {
224 n=textreloc.r_val.rel_i;
226 if (textreloc.r_val.rel_gp->g_status&DEF) {
227 n=textreloc.r_val.rel_gp->g_val.g_addr;
230 fatal("Definition botch") ;
231 xputc(xgetc(f1),f2) ;
232 ieof=getblk(rtfile,(char *)(&textreloc.r_off),
233 sizeof textreloc-sizeof textreloc.r_next);
237 op_curr = &opchoice[textreloc.r_typ& ~RELMNS] ;
238 insl = oplength(*op_curr) ;
239 genop(op_curr, n+xgetarb(insl,f1), PAR_G);
241 ieof=getblk(rtfile,(char *)(&textreloc.r_off),
242 sizeof textreloc - sizeof textreloc.r_next) ;
244 xputc(xgetc(f1),f2) ;
247 for ( ; i<keep ; i++ ) xputc(xgetc(f1),f2) ;
248 if ( !ieof && !getblk(rtfile,(char *)&textreloc,1) )
249 fatal("text relocation botch") ;
256 register glob_t *gbp;
259 * Change reloc-tables such that for every pointer into mglobs
260 * either the corresponding pointer into xglobs or its value
263 * Use is made of the known order of mglobs and xglobs
267 while ( p= f_text ) {
268 gbp= p->r_val.rel_gp ;
269 if( gbp->g_status&DEF ) {
271 p->r_val.rel_i = gbp->g_val.g_addr;
273 p->r_val.rel_gp = gbp->g_val.g_gp;
274 putblk(rtfile,(char *)(&(p->r_off)),sizeof *p - sizeof p) ;
275 f_text= p->r_next ; freearea( (area_t) p , sizeof *p ) ;
279 if (p->r_typ == RELGLO) {
280 gbp= p->r_val.rel_gp ;
281 if(gbp->g_status&DEF) {
283 p->r_val.rel_i = gbp->g_val.g_addr;
285 p->r_val.rel_gp = gbp->g_val.g_gp;
287 putblk(rdfile,(char *)(&(p->r_off)),sizeof *p - sizeof p) ;
288 f_data= p->r_next ; freearea( (area_t) p , sizeof *p ) ;