In Pass 1 of the linking loader, the primary task is to analyze the object code files and gather information about the program's structure. The loader examines the headers of each module to build a symbol table, which records the names and addresses of all external symbols (i.e., variables or functions referenced across different modules). Pass 1 also identifies relocation information and determines the memory allocation for each module. However, it does not perform any actual memory loading or address resolution. Instead, it prepares the necessary data for Pass 2, where the final linking and loading steps will occur.
The objective of Linking Loader Pass1:
Resolve external symbol definitions and references across object modules.
Allocate memory and calculate starting addresses for each object module.
Process relocation records to identify addresses requiring modifications.
Validate external symbols to detect undefined or duplicate definitions.
Prepare data structures for efficient object code loading and linking in Pass 2.
The data structure used in Linking Loader Pass1:
The data structure used by a linking loader in its first pass is an external symbol table (ESTAB). This table stores the name and address of each external symbol in the set of control sections being loaded. The table is usually organized using a hash.
The data structure used by a linking loader in its first pass is an external symbol table (ESTAB). This table stores the name and address of each external symbol in the set of control sections being loaded. The table is usually organized using a hash.
begin
get PROOADDR from operating system
set CSADDR to PROOADDR {for first control section}
while not end of input do
begin
read next input record {Header record for control section}
set CSLTH to control section length
search ESTAB for control section name
if found then
set error flag {duplicate external symbol}
else
enter control section name into ESTAB with value CSADDR
while record type ~ 'E' do
begin
read next input record
if record type = 'D' then
for each symbol in the record do
begin
search ESTAB for symbol name
if found then
set error flag (duplicate external symbol)
else
enter symbol into ESTAB with value(CSADDR + indicated address)
end {for}
end {while ~ 'E'}
add CSLTH to CSADDR {starting address for next control section}
end {while not EOF}
end {Pass 1}
H PROGD 000000 000081
D LISTX 000050
R LISTY ENDX LISTZ ENDY
T 000024 0A 04201D 88100008 060014
T 000050 0F 110014 00000C 005051 000008 120000
M 000028 05 +LISTY
M 000050 06 +LISTZ
M 000058 06 +LISTY
M 000058 06 -LISTX
E 000024
H PROGE 000000 00009F
D LISTY 000070 ENDY 000090
R LISTX LISTZ ENDZ
T 000046 0B 04100000 882037 06100000
T 000090 0F 110000 00000C 005051 000008 120070
M 000047 05 +LISTX
M 00004E 05 -LISTX
M 000090 06 -LISTX
M 000090 06 +LISTZ
M 00009C 06 +PROGE
M 00009C 06 -LISTX
E 000000
H PROGF 000000 000067
D LISTZ 000040 ENDZ 000052
R LISTX LISTY ENDY
T 000028 0C 04100000 88100008 06100000
T 000052 0F 110040 00000C 005051 000008 120000
M 00002D 05 +LISTY
M 000031 05 -LISTX
M 000052 06 -LISTX
M 000052 06 +PROGF
M 00005E 06 +LISTY
M 00005E 06 -LISTX
E
END
#include<stdio.h>
#include<string.h>
struct estab
{
char csname[10];
char extsym[10];
int address;
int length;
}es[20];
void main()
{
char input[10],name[10],symbol[10],ch; int count=0,progaddr,csaddr,add,len;
FILE *fp1,*fp2;
fp1=fopen("INPUT.DAT","r");
fp2=fopen("ESTAB.DAT","w");
printf("\n\nEnter the address where the program has to be loaded : ");
scanf("%x",&progaddr); // TAKING THE PROGRAM ADDRESS FROM THE USER,GENERALLY IT IS DONE BY THE OS
csaddr=progaddr;
fscanf(fp1,"%s",input);
while(strcmp(input,"END")!=0)
{
if(strcmp(input,"H")==0)
{
fscanf(fp1,"%s",name);
strcpy(es[count].csname,name);
strcpy(es[count].extsym," ");
fscanf(fp1,"%x",&add);
es[count].address=add+csaddr;
fscanf(fp1,"%x",&len);
es[count].length=len;
fprintf(fp2,"%s ** %x %x\n",es[count].csname,es[count].address,es[count].length);
count++;
}
else if(strcmp(input,"D")==0)
{
fscanf(fp1,"%s",input);
while(strcmp(input,"R")!=0)
{
strcpy(es[count].csname," ");
strcpy(es[count].extsym,input);
fscanf(fp1,"%x",&add);
es[count].address=add+csaddr;
es[count].length=0;
fprintf(fp2,"** %s %x\n",es[count].extsym,es[count].address);
count++;
fscanf(fp1,"%s",input);
}
csaddr=csaddr+len;
}
else if(strcmp(input,"T")==0)
{
while(strcmp(input,"E")!=0)
fscanf(fp1,"%s",input);
}
fscanf(fp1,"%s",input);
}
fclose(fp1);
fclose(fp2);
fp2=fopen("ESTAB.DAT","r");
ch=fgetc(fp2);
while(ch!=EOF)
{
printf("%c",ch);
ch=fgetc(fp2);
}
fclose(fp2);
}
Pass 1 Output