All pastes #806939 Raw Edit

Mine

public text v1 · immutable
·published 2007-12-06 01:19 UTC
rendered paste body
/*
 * Code to manipulate the relocation table during the link
 */
#include "../include/genutil.h"
#include "../include/bacicnty.h"
#include "ld.h"
#include "ldutils.h"
#include "ldreloc.h"

/*
 *>>>>>>>>>>>>  Change this to your login id  <<<<<<<<<<<<
 *>>>>>>>>>>>>  (code won't run until you do) <<<<<<<<<<<<
 */ 
char loginid[] = {"mxbusu"};

void append_reloc(ALL_TABLES* tfinal, ALL_TABLES *t1, ALL_TABLES* t2)
    /* appends t2->reloc[] to tfinal->reloc[],
       updating the code addresses of the reloc items to the
       value required in tfinal->reloc[], and editing
       tfinal->code[] as necessary,
       */
{
	int last_reloc;
	int i;
	ORDER code[CSIZE];
	relocation reloc;	
	int t2_coffset = t1->last_code+1 - t2->first_code;
	int t2_doffset = t1->last_dseg+1 - t2->first_dseg;
	
	for(i = 0; i <= last_reloc; i++){
		reloc_fields r = t2->reloc[i];
		if(r.r.ref == CODEREF){
			r.r.adr += t2_coffset;
			tfinal->code[r.r.adr].u.y += t2_coffset;
		}//if
		if(r.r.ref == EXTDSEGREF){
			r.r.adr += t2_coffset;
			if(tfinal->code[r.r.adr].u.y != 0xffff){
				tfinal->code[r.r.adr].u.y += t2_doffset;
			}//if
		}//if
		if(r.r.ref == DSEGREF){
			if(r.r.tabix == NO_MOD){
				r.r.tabix = 0;
			}//if
			else{
				r.r.adr +=t2_coffset;
				tfinal->code[r.r.adr].u.y += t2_doffset;
			}//else
		}//if
		if(r.r.ref == MARKSTACKREF){
			unsigned tix = t2->code[r.r.adr].u.y;
			int tixf = search_tab(tfinal,t2->tab[tix].name);
			r.r.adr += t2_coffset;
			tfinal->code[r.r.adr].u.y = tixf;
		}//if
		tfinal->last_reloc++;
		tfinal->reloc[tfinal->last_reloc] = r;		
	}//for
}/*end append_reloc */

void update_ext_reloc(ALL_TABLES* tfinal, ALL_TABLES* t, int ex_tabix, 
   int new_ref, int new_tabix, int modify_code , int t_code_offset, 
   int new_dseg_adr)
   /* Called when new information shows up about an external dseg reference.
    * Search the relocation table t->reloc[] for an extdsegref ref field
    * having a tabix field == ex_tabix
    * When found at t->reloc[i], 
    *   update the ref field of t->reloc[i] to new_ref
    *   update the tabix field of t->reloc[i] to new_tabix
    *   If (modify_code),
    *      modify tfinal->code[adr+t_code_offset].u.y to new_dseg_adr
    *      If new_tabix == NO_MOD, then the adr field of the relocation
    *      item must be modified now (append_reloc will zero the tabix
    *      field when it sees such an item and leave the adr field 
    *      unmolested)
    */
{
	int last_reloc;
	int i;

	for(i = 0; i <= last_reloc; i++){
		if(t->reloc[i].r.tabix == ex_tabix && t->reloc[i].r.ref == EXTDSEGREF){
			t->reloc[i].r.ref = new_ref;
			t->reloc[i].r.tabix = new_tabix;
		}//if
		if(modify_code){
			tfinal->code[t->reloc[i].r.adr+t_code_offset].u.y = new_dseg_adr;
			if(new_tabix == NO_MOD){
				t->reloc[i].r.adr += t_code_offset;
			}//if		
		}//if
	}//for
}  /* update_ext_reloc */

void perform_reloc(ALL_TABLES* t, int first_code, int first_dseg)
   /* user has requested relocation of the final link file 
    * so that t->first_code = first_code and t->first_dseg = first_dseg.
    * Move code & dseg tables, then 
    * Use reloc table to update the code array
    * Change code addresses in the tab and dbg tables
    */
{
	t->first_code = first_code;
	t->first_dseg = first_dseg;
	int code_delta = first_code - t->first_code;
	int dseg_delta = first_dseg - t->first_dseg;
	move_code(t, first_code);
	move_dseg(t, first_dseg);
	int last_tab = t->last_tab;
	int i;
	int last_reloc = t->last_reloc;
	for(i = 0; i <= last_reloc; i++){
		reloc_fields r = t->reloc[i];
		r.r.adr += code_delta;
		if(r.r.ref == CODEREF){
			t->code[r.r.adr].u.y += code_delta;
		}//if
		if(r.r.ref == DSEGREF){
			t->code[r.r.adr].u.y += dseg_delta;
		}//if
		if(r.r.ref == EXTDSEGREF && t->code[r.r.ref].u.y != 0xfff){
			t->code[r.r.adr].u.y += dseg_delta;
		}//if
	}//for
	
	int ix = t->btab[0].last;
   	while (ix) {
		if(t->tab[ix].obj == variable){
			t->tab[ix].adr += dseg_delta;
		}//if
		else{
			t->tab[ix].adr += code_delta;
		}//else
		ix = t->tab[ix].link;
	}//while

	update_dbg(t, code_delta);
}  /* perform_reloc */

/*
 * $Log: ldreloc-handout.c,v $
 * Revision 1.2  2005/04/13 18:59:36  bynum
 * Fall 2004 version
 *
 * Revision 1.1  2002/11/27 13:26:01  bynum
 * Initial revision
 *
 */