100% found this document useful (2 votes)
1K views

Source Code Program in C For Relocation Loader

The document describes a C program for relocating object code. It includes functions to open input and output files, read records with headers (H) and text (T) segments, parse bitmasks to determine if addresses need relocation, relocate addresses by adding a start address as needed, and write the relocated object code to the output file. Key steps include getting the start address, reading records, determining if bits require relocation, adding the start address, and writing the relocated records to the output file.

Uploaded by

Sridevi Sridhar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
1K views

Source Code Program in C For Relocation Loader

The document describes a C program for relocating object code. It includes functions to open input and output files, read records with headers (H) and text (T) segments, parse bitmasks to determine if addresses need relocation, relocate addresses by adding a start address as needed, and write the relocated object code to the output file. Key steps include getting the start address, reading records, determining if bits require relocation, adding the start address, and writing the relocated records to the output file.

Uploaded by

Sridevi Sridhar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Source code program in c for Relocation Loader

# include <stdio.h>
# include <conio.h>
# include <string.h>
# include <stdlib.h>
void main()
{
char add[6],length[10],input[10],binary[12],bitmask[12],relocbit;
int start,inp,len,i,address,opcode,addr,actualadd;
FILE *fp1,*fp2;
clrscr();
printf("Enter the actual starting address : ");
scanf("%d",&start);
fp1=fopen("relinput.dat","r");
fp2=fopen("reloutput.dat","w");
fscanf(fp1,"%s",input);
while(strcmp(input,"E")!=0)
{
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%s",add);
fscanf(fp1,"%s",length);
fscanf(fp1,"%s",input);
}
if(strcmp(input,"T")==0)
{
fscanf(fp1,"%d",&address);
fscanf(fp1,"%s",bitmask);
address+=start;
len=strlen(bitmask);
for(i=0;i<len;i++)
{
fscanf(fp1,"%d",&opcode);
fscanf(fp1,"%d",&addr);
relocbit=bitmask[i];
if(relocbit=='0')
actualadd=addr;
else
actualadd=addr+start;
fprintf(fp2,"%d\t%d%d\n",address,opcode,actualadd);
address+=3;
}
fscanf(fp1,"%s",input);
}
}
fclose(fp1);
fclose(fp2);
printf("FINISHED");
getch();
}
INPUT : RELINPUT.DAT
H 1000 200
T 1000 11001 14 1033 48 1039 90 1776 92 1765 57 1765
T 2011 11110 23 1838 43 1979 89 1060 66 1849 99 1477
E 1000

OUTPUT :
Enter the actual starting address :4000

RELOUTPUT.DAT
4000 144033
4003 484039
4006 901776
4009 921765
4012 574765
5011 234838
5014 434979
5017 894060
5020 664849
5023 991477

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<stdlib.h>

struct object_code

{          int locctr;

char add[10];

}obcode[300];

void main()

{          char input[100][16], output[100][16], binary[20], address[20], stloc[10];

int len, bitmask, loc, tlen=0, tloc, textloc, i=0, location, j, k, count=0, start, n, num=0,
inc=0;

FILE *fp1, *fp2;

clrscr();
fp1=fopen(“linput.dat”, “r”);

fp2=fopen(“loadout.dat”, “w”);

printf(“Enter the location where the program has to be loaded:”);

scanf(“%s”, stloc);

start = atoi(stloc);

location=start;

tloc=start;

fscanf(fp1, “%s”, input[i]);

while(strcmp(input[i], “T”)!=0)

strcpy(output[i], input[i]);

i++;

fscanf(fp1, “%s”, input[i]);

strcpy(output[i], input[i]);

itoa(start, output[2],10);

while(strcmp(input[i], “E”)!=0)

strcpy(output[i], input[i]);

if(strcmp(input[i], “T”)==0)

for(j=0; j<3; j++)

{          i++;

fscanf(fp1, “%s”, input[i]);

strcpy(output[i], input[i]);
}

bitmask = atoi(output[i]);

itoa(bitmask, binary, 2);

strcpy(output[i], NULL);

textloc=textloc+start;

itoa(textloc, output[i-2], 10);

for(n=0; n<(textloc-(tloc+tlen)); n++)

strcpy(obcode[inc].add, “xx”);

obcode[inc++].locctr=location++;

tlen=atoi(output[i-1]);

tloc=textloc;

k=0;

else

if(binary[k]==’1′)

num=0;

len=strlen(output[i]);

strcpy(address, NULL);

for(j=2; j<len; j++)

address[num]=output[i][j];
output[i][j]=”;

num++;

loc=atoi(address);

loc=loc+start;

itoa(loc, address, 10);

strcat(output[i], address);

k++;

len=strlen(output[i]);

num=0;

for(n=0; n<len; n++)

obcode[inc].add[num++]=output[i][n];

if(num>1)

obcode[inc++].locctr=location++;

num=0;

i++;

fscanf(fp1, “%s”, input[i]);

strcpy(output[i], input[i]);
i++;

fscanf(fp1, “%s”, input[i]);

loc=atoi(input[i]);

loc=loc+start;

strcpy(output[i], itoa(loc, address, 10));

count=0;

i=0;

n=0;

fprintf(fp2, “%d\t”,obcode[n].locctr);

for(n=0; n<inc; n++)

{          fprintf(fp2, “%s”, obcode[n].add);

i++;

if(i>3)

{          fprintf(fp2, “\t”);

i=0;

count++;

if(count>3)

{          fprintf(fp2, “\n%d\t”, obcode[n+1].locctr);

count=0;

printf(“\nProgram is relocated…”);

getch();

You might also like