Xv6 with picoc & Linkage editor
v1.0
The project delineate mutual cohesion between c library, linkage editor ( linker), interpreter and operating system by porting the same on xv6 kernel
|
00001 /* 00002 http://em386.blogspot.com 00003 00004 You may not use this code in a product, 00005 but feel free to study it and rewrite it 00006 in your own way 00007 00008 This code is an example of how to use the 00009 libelf library for reading ELF objects. 00010 00011 gcc -o libelf-howto libelf-howto.c -lelf 00012 */ 00013 00014 //#include <stdio.h> 00015 //#include <string.h> 00016 //#include <stdlib.h> 00017 //#include <unistd.h> 00018 #include "fcntl.h" 00019 00020 #include <libelf/libelf.h> 00021 #include <libelf/gelf.h> 00022 #include "ld.h" 00023 #include"types.h" 00024 #include"stat.h" 00025 #include"user.h" 00026 00027 #define TEXT_INDEX 1 00028 #define DATA_INDEX 2 00029 #define BSS_INDEX 3 00030 #define ERR -1 00031 #define ALIGN(x) 32 - (x % 32) + x 00032 00033 char prasann[512]; 00034 char buf[512]; 00035 int argtextsize[3]; 00036 int argdatasize[3]; 00037 char header_buf[] = { 00038 0x7f,0x45, 0x4c,0x46, 0x01,0x01, 0x01,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 00039 0x02,0x00, 0x03,0x00, 0x01,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x34,0x00, 0x00,0x00, 00040 0xd4,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x34,0x00, 0x20,0x00, 0x01,0x00, 0x28,0x00, 00041 0x07,0x00, 0x04,0x00, 0x01,0x00, 0x00,0x00, 0x60,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 00042 0x00,0x00, 0x00,0x00, 0x48,0x00, 0x00,0x00, 0x68,0x00, 0x00,0x00, 0x07,0x00, 0x00,0x00, 00043 0x20,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00, 0x00,0x00 00044 }; 00045 00046 int definition_not_found(Node *undefined_object_file){ 00047 Node *p; 00048 p = undefined_object_file; 00049 printf("............................\n"); 00050 traverse(undefined_object_file); 00051 p = p->next; 00052 while(p != NULL){ 00053 if(p->argindex >= 0){ 00054 printf("%s: missing definition\n",p->name); 00055 return 1; 00056 } 00057 p = p->next; 00058 } 00059 return 0; 00060 } 00061 void get_undefined_entries(Elf *elf,Node **undefined_object_file1,Node **defined_object_file1,int argindex){ 00062 00063 /* segregate symbol table entries into defined and undefined 00064 argument : Elf *elf, from which symbol are to be separated out 00065 requirements: Global pointers from undefined symbols and defined symbols of type GElf_Sym ** should be declared 00066 return values: It returns none, but will malloc above mentioned arrays */ 00067 int i,j,k,symbol_count; 00068 Elf_Scn *scn1 = NULL; 00069 GElf_Shdr shdr; 00070 Elf_Data *edata = NULL; 00071 GElf_Sym *sym ; 00072 i = j = k = 0; 00073 printf("before loop\n"); 00074 while((scn1 = elf_nextscn(elf, scn1)) != NULL) 00075 { 00076 printf("in loop\n"); 00077 gelf_getshdr(scn1, &shdr); 00078 00079 // When we find a section header marked SHT_SYMTAB stop and get symbols 00080 if(shdr.sh_type == SHT_SYMTAB) 00081 { 00082 // edata points to our symbol table 00083 edata = elf_getdata(scn1, edata); 00084 symbol_count = (int ) shdr.sh_size / (int ) shdr.sh_entsize; 00085 int counter = (int)sizeof(Elf32_Rel); 00086 00087 /* how many symbols are there? 00088 his number comes from the size of // the section divided by the entry size 00089 libelf grabs the symbol data using gelf_getsym() 00090 */ 00091 00092 for(i = 0 ; i < symbol_count ; i++){ 00093 00094 sym = (GElf_Sym *)malloc(sizeof(GElf_Sym)); 00095 gelf_getsym(edata, i, sym); 00096 00097 if(sym->st_shndx == SHN_UNDEF){ 00098 // if(ELF32_ST_BIND(sym->st_info) == 1)//this checks for local declaration 00099 insert(undefined_object_file1,sym,elf_strptr(elf,shdr.sh_link,sym->st_name),argindex); 00100 }else{ 00101 if(ELF32_ST_TYPE(sym->st_info) != STT_FILE) 00102 insert(defined_object_file1,sym,elf_strptr(elf,shdr.sh_link,sym->st_name),argindex); 00103 } 00104 } 00105 } 00106 } 00107 } 00108 char *get_section_content(Elf *elf,int *size,int index){ 00109 printf("In function get section content\n"); 00110 Elf_Scn *scn; 00111 Elf_Data *edata = NULL; 00112 Elf32_Shdr *shdr; 00113 char *data; 00114 int i = 0; 00115 00116 if(!(scn = elf_getscn(elf,index))){ 00117 printf("error getting text section\n"); 00118 return NULL; 00119 } 00120 if(!(shdr = elf32_getshdr(scn))){ 00121 printf("error getting text section header\n"); 00122 return NULL; 00123 } 00124 00125 if(!(edata = elf_getdata(scn,edata))){ 00126 printf("error getting text data\n"); 00127 return NULL; 00128 } 00129 data = (char *)malloc(sizeof(char) * (int)edata->d_size); 00130 00131 for( i = 0 ; i < (int)edata->d_size ; i++){ 00132 *(data + i) = (char)*((char *)edata->d_buf + i); 00133 printf(" %x ", *(data + i)); 00134 } 00135 printf("\n\n"); 00136 *size = i; 00137 return data; 00138 } 00139 int resolve_undefined_reference(Node **undefined1, Node **defined1,Node **defined2 , Node **undefined2){ 00140 00141 Node *u = *undefined1; 00142 Node *tmp; 00143 GElf_Sym *sym; 00144 while(u){ 00145 if(tmp = (Node *)ispresent(*defined2,u->name)){ 00146 if( !(ispresent(*undefined2,u->name))){ 00147 sym = (GElf_Sym *)malloc(sizeof(GElf_Sym)); 00148 memcpy(sym,tmp->symbol,sizeof(GElf_Sym)); 00149 insert(defined1,sym,tmp->name,tmp->argindex); 00150 u->argindex = -1; 00151 00152 } 00153 } 00154 u = u->next; 00155 } 00156 } 00157 int update(Node **undefined1,Node **defined1){ 00158 Node *u = *undefined1; 00159 Node *p = *defined1; 00160 /* 00161 printf("before update function defined\n"); 00162 traverse(*defined1); 00163 printf("before update function undefined\n"); 00164 traverse(*undefined1); 00165 */ 00166 int text = 0; 00167 int data = 0; 00168 int bss = 0; 00169 int i; 00170 // while(u){ 00171 p = *defined1; 00172 while(p){ 00173 00174 // if(!strcmp(u->name,p->name)){ 00175 00176 text = data = 0; 00177 for(i = 0 ; i < p->argindex;i++) 00178 text += ALIGN(argtextsize[i]); 00179 for(i = 0 ; i < p->argindex;i++) 00180 data += ALIGN(argdatasize[i]); 00181 // printf("symbol %s t:%d d:%d\n",p->name, text,data); 00182 00183 if(p->symbol->st_shndx == 1) 00184 p->symbol->st_value += text; 00185 else if(p->symbol->st_shndx == 2) 00186 p->symbol->st_value += data; 00187 else if(p->symbol->st_shndx == 3) 00188 p->symbol->st_value += bss; 00189 00190 p = p->next; 00191 // break; 00192 } 00193 // p = p->next; 00194 // } 00195 // u = u->next; 00196 // } 00197 } 00198 char *get_symbol_name(Elf *elf,int index){ 00199 00200 Elf_Scn *scn = NULL; 00201 Elf_Data *edata = NULL; 00202 GElf_Shdr shdr; /* Section Header */ 00203 char *data; 00204 GElf_Sym sym; 00205 while((scn = elf_nextscn(elf, scn)) != NULL) 00206 { 00207 gelf_getshdr(scn, &shdr); 00208 // When we find a section header marked SHT_SYMTAB stop and get symbols 00209 if(shdr.sh_type == SHT_SYMTAB) 00210 { 00211 // edata points to our symbol table 00212 edata = elf_getdata(scn, edata); 00213 // libelf grabs the symbol data using gelf_getsym() 00214 gelf_getsym(edata,index, &sym); 00215 // print out the value and size 00216 return elf_strptr(elf, shdr.sh_link, sym.st_name); 00217 } 00218 } 00219 00220 } 00221 void do_relocation(Elf *elf,Node *defined1,char *text,char * data,int textsize, int start_text , int start_data){ 00222 int symbol_count; 00223 int i; 00224 Elf_Scn *scn1 = NULL; 00225 GElf_Shdr shdr; 00226 Elf_Data *edata = NULL; 00227 GElf_Rel *rel,rel_mem; 00228 char *sym_name; 00229 Node *symbol_entry = NULL; 00230 char *refptr = NULL; 00231 int tmp; 00232 00233 while((scn1 = elf_nextscn(elf, scn1)) != NULL) 00234 { 00235 gelf_getshdr(scn1, &shdr); 00236 00237 if(shdr.sh_type == SHT_REL){ 00238 // edata points to our symbol table 00239 edata = elf_getdata(scn1, edata); 00240 symbol_count = (int)shdr.sh_size / (int)shdr.sh_entsize; 00241 int counter = (int)sizeof(Elf32_Rel); 00242 for(i = 0 ; i < symbol_count ; i++){ 00243 /*get every entry from relocation table*/ 00244 rel = gelf_getrel(edata,i,&rel_mem); 00245 00246 /*get it's newly modified address from defined1 symlist*/ 00247 sym_name = get_symbol_name(elf,(int)GELF_R_SYM(rel->r_info)); 00248 symbol_entry = (Node *)ispresent(defined1,sym_name); 00249 printf("symbol is %s and symbol value %d",sym_name ,(int) symbol_entry->symbol->st_value); 00250 if(symbol_entry == NULL){ 00251 printf("\nUndefined reference to symbol %s\n",sym_name); 00252 return ; 00253 } 00254 00255 /*check relocation table entries type from relocation table*/ 00256 if(ELF32_ST_TYPE(rel->r_info) == R_386_PC32){ 00257 /*if it is a function*/ 00258 refptr = text + (int)rel->r_offset + start_text; 00259 if(symbol_entry->symbol->st_shndx == 1){ 00260 /* it from text section */ 00261 00262 tmp = (int)(( symbol_entry->symbol->st_value) - (int)rel->r_offset - start_text + *(int*)refptr ); 00263 //printf("value in text pc32 tmp = %d and rel offset = %d\n",(int)tmp,(int)rel->r_offset); 00264 *(int *)refptr = tmp; 00265 }else if(symbol_entry->symbol->st_shndx == 2){ 00266 tmp = (int)( textsize - symbol_entry->symbol->st_value -(int)rel->r_offset +*(int*)refptr); 00267 *(int *)refptr = tmp; 00268 } 00269 }else if(ELF32_ST_TYPE(rel->r_info) == R_386_32){ 00270 /*if it is a variable*/ 00271 refptr = text + (int)rel->r_offset + start_text; 00272 if(symbol_entry->symbol->st_shndx == 1){ 00273 /* it from text section */ 00274 // refptr = text + (int)rel->r_offset; 00275 tmp = (int)( symbol_entry->symbol->st_value); 00276 *(int *)refptr = tmp; 00277 }else if(symbol_entry->symbol->st_shndx == 2){ 00278 // refptr = data + (int)rel->r_offset; 00279 tmp = (int)( textsize + (int)symbol_entry->symbol->st_value); 00280 *(int *)refptr = tmp; 00281 } 00282 00283 }else{ 00284 printf("\n%d type not supported\n",(int)ELF32_ST_TYPE(rel->r_info)); 00285 exit(1); 00286 } 00287 printf(" After relocation its value is 0x%x \n", tmp); 00288 } 00289 } 00290 } 00291 } 00292 00293 void main(int argc, char *argv[]){ 00294 00295 Node *undefined_object_file1, *undefined_object_file2 , *undefined_object_file3; 00296 Node *defined_object_file1, *defined_object_file2 ,*defined_object_file3; 00297 00298 init(&undefined_object_file1); 00299 init(&defined_object_file1); 00300 init(&undefined_object_file2); 00301 init(&defined_object_file2); 00302 init(&undefined_object_file3); 00303 init(&defined_object_file3); 00304 00305 int fd1,fd2,fd3, type,bind,fsize; // File Descriptor 00306 char *base_ptr1, *base_ptr2, *base_ptr3; // ptr to our object in memory 00307 char *file = argv[1]; // filename 00308 struct stat elf_stats; // fstat struct 00309 int i,j,size; char *name; 00310 unsigned int ret; 00311 Elf32_Rel rel; 00312 00313 00314 00315 Elf *elf1, *elf2, *elf3; // Our Elf pointer for libelf 00316 Elf32_Ehdr *elf_header1, *elf_header2 , *elf_header3; // ELF header 00317 GElf_Shdr shdr; // Section Header 00318 Elf_Scn *scn; // Section Descriptor 00319 00320 if((fd1 = open(argv[1], 0)) < 0) //open first elf file 00321 { 00322 printf("couldnt open %s\n", argv[1]); 00323 exit(1); 00324 } 00325 00326 if((fd2 = open(argv[2], 0)) < 0) //open second file 00327 { 00328 printf("couldnt open %s\n", argv[2]); 00329 exit(1); 00330 } 00331 if((fd3 = open(argv[3], 0)) < 0 ) //open second file 00332 { 00333 printf("couldnt open %s\n", argv[3]); 00334 exit(1); 00335 } 00336 00337 if((fsize = fsstat(fd1, &elf_stats)) == -1 ) 00338 { 00339 printf("could not fstat %sand %d fd = %d\n", argv[1],fsize,fd1); 00340 close(fd1); 00341 exit(1); 00342 } 00343 00344 if((base_ptr1 = (char *) malloc(fsize+2)) == (void*)0) 00345 { 00346 printf("could not malloc\n"); 00347 close(fd1); 00348 exit(1); 00349 } 00350 if((read(fd1, base_ptr1, fsize)) == -3) 00351 { 00352 printf("could not read %s and size is %d\n", argv[1],fd1); 00353 free(base_ptr1); 00354 close(fd1); 00355 exit(1); 00356 } 00357 00358 if((fsize = fsstat(fd2, &elf_stats)) == -1) 00359 { 00360 printf("could not fstat %s\n", argv[2]); 00361 close(fd2); 00362 exit(1); 00363 } 00364 00365 if((base_ptr2 = (char *) malloc(fsize+2)) == (void*)0) 00366 { 00367 printf("could not malloc\n"); 00368 close(fd2); 00369 exit(1); 00370 } 00371 00372 00373 if((read(fd2, base_ptr2, 20)) == -3) 00374 { 00375 printf("could not read %s\n", argv[2]); 00376 free(base_ptr2); 00377 close(fd2); 00378 exit(1); 00379 } 00380 if((fsize = fsstat(fd3, &elf_stats)) == -1) 00381 { 00382 printf("could not fstat %s\n", argv[3]); 00383 close(fd3); 00384 exit(1); 00385 } 00386 00387 if((base_ptr3 = (char *) malloc(fsize+2)) == (void*)0) 00388 { 00389 printf("could not malloc\n"); 00390 close(fd3); 00391 exit(1); 00392 } 00393 00394 00395 if((read(fd3, base_ptr3, fsize)) == -3) 00396 { 00397 printf("could not read %s\n", argv[3]); 00398 free(base_ptr3); 00399 close(fd3); 00400 exit(1); 00401 } 00402 /* Check libelf version first */ 00403 if(elf_version(EV_CURRENT) == EV_NONE) 00404 { 00405 printf("WARNING Elf Library is out of date!\n"); 00406 } 00407 // READ text and data sections 00408 char *text1,*text2,*text3,*data1,*data2,*data3,*bss1,*bss2; 00409 int textsize,datasize,bsssize; 00410 00411 elf_header1 = (Elf32_Ehdr *) base_ptr1; // point elf_header at our object in memory 00412 elf_header2 = (Elf32_Ehdr *) base_ptr2; // point elf_header at our object in memory 00413 elf_header3 = (Elf32_Ehdr *) base_ptr3; // point elf_header at our object in memory 00414 00415 00416 printf("going to begin 1\n"); 00417 elf1 = elf_begin(fd1,ELF_C_READ,NULL); 00418 if(!elf1) 00419 printf("problem in elf_begin\n"); 00420 printf("return from begin 1\n"); 00421 elf2 = elf_begin(fd2,ELF_C_READ,NULL); 00422 printf("return from begin 2\n"); 00423 elf3 = elf_begin(fd3,ELF_C_READ,NULL); 00424 printf("return from begin 3\n"); 00425 00426 printf("going to undefined symbols \n"); 00427 get_undefined_entries(elf1,&undefined_object_file1,&defined_object_file1,0); 00428 printf("return from undefiend 1\n"); 00429 get_undefined_entries(elf2,&undefined_object_file2,&defined_object_file2,1); 00430 printf("return from undefiend 2\n"); 00431 get_undefined_entries(elf3,&undefined_object_file3,&defined_object_file3,2); 00432 printf("return from undefiend 3\n"); 00433 00434 printf("\nDefined Symbol list of object file 1\n"); 00435 traverse(defined_object_file1); 00436 printf("\nDefined Symbol list of object file 2\n"); 00437 traverse(defined_object_file2); 00438 printf("\nDefined Symbol list of object file 3\n"); 00439 traverse(defined_object_file3); 00440 00441 printf("\nUndefined Symbol list of object file 1\n"); 00442 traverse(undefined_object_file1); 00443 printf("\nUndefined Symbol list of object file 2\n"); 00444 traverse(undefined_object_file2); 00445 printf("\nUndefined Symbol list of object file 2\n"); 00446 traverse(undefined_object_file2); 00447 00448 /*resolve all undefined entries */ 00449 resolve_undefined_reference(&undefined_object_file1,&defined_object_file1,&defined_object_file2,&undefined_object_file2); 00450 resolve_undefined_reference(&undefined_object_file1,&defined_object_file1,&defined_object_file3,&undefined_object_file3); 00451 resolve_undefined_reference(&undefined_object_file2,&defined_object_file2,&defined_object_file1,&undefined_object_file1); 00452 resolve_undefined_reference(&undefined_object_file2,&defined_object_file2,&defined_object_file3,&undefined_object_file3); 00453 00454 text1 = get_section_content(elf1,&textsize,TEXT_INDEX); 00455 argtextsize[0] = textsize; 00456 text2 = get_section_content(elf2,&textsize,TEXT_INDEX); 00457 argtextsize[1] = textsize; 00458 text3 = get_section_content(elf3,&textsize,TEXT_INDEX); 00459 argtextsize[2] = textsize; 00460 00461 data1 = get_section_content(elf1,&datasize,DATA_INDEX); 00462 argdatasize[0] = datasize; 00463 printf("datasize is for 1 %d\n",datasize); 00464 data2 = get_section_content(elf2,&datasize,DATA_INDEX); 00465 argdatasize[1] = datasize; 00466 printf("datasize is for 2 %d\n",datasize); 00467 data3 = get_section_content(elf3,&datasize,DATA_INDEX); 00468 argdatasize[2] = datasize; 00469 printf("datasize is for 3 %d\n",datasize); 00470 00471 for(j = 0 ; j < argdatasize[0] ; j++) 00472 printf("%x ",(char)*(data1 + j)); 00473 printf("\n\n\n"); 00474 for(j = 0 ; j < argdatasize[1] ; j++) 00475 printf("%x ",(char)*(data2 + j)); 00476 printf("\n\n\n"); 00477 for(j = 0 ; j < argdatasize[2] ; j++) 00478 printf("%x ",(char)*(data3 + j)); 00479 printf("\n\n\n"); 00480 00481 00482 00483 char *finaltext = (char *)malloc(sizeof(char) * (ALIGN(argtextsize[0]) + ALIGN(argtextsize[1]) + ALIGN(argtextsize[2]))); 00484 char *finaldata = (char *)malloc(sizeof(char) * (ALIGN(argdatasize[0]) + ALIGN(argdatasize[1]) + ALIGN(argdatasize[2]))); 00485 00486 update(&undefined_object_file1,&defined_object_file1); 00487 update(&undefined_object_file2,&defined_object_file2); 00488 printf("After update\n"); 00489 for(j = 0 ; j < argdatasize[0] ; j++) 00490 printf("%x ",(char)*(data1 + j)); 00491 printf("\n\n\n"); 00492 for(j = 0 ; j < argdatasize[1] ; j++) 00493 printf("%x ",(char)*(data2 + j)); 00494 printf("\n\n\n"); 00495 for(j = 0 ; j < argdatasize[2] ; j++) 00496 printf("%x ",(char)*(data3 + j)); 00497 printf("\n\n\n"); 00498 00499 if(definition_not_found(undefined_object_file1) || definition_not_found(undefined_object_file2)) 00500 exit(1); 00501 00502 for( i = 0 ; i < argdatasize[0] ; i++) 00503 *(finaldata + i ) = *(data1 + i); 00504 for( i = argdatasize[0] ; i < ALIGN(argdatasize[0]) ; i++) 00505 *(finaldata + i) = 0; 00506 00507 for( i = 0 ; i < argdatasize[1] ; i++) 00508 *(finaldata + ALIGN(argdatasize[0]) + i ) = *(data2 + i); 00509 for( i = argdatasize[1] ; i < ALIGN(argdatasize[0]) + ALIGN(argdatasize[1]) ; i++) 00510 *(finaldata + ALIGN(argdatasize[0]) + i) = 0; 00511 00512 for( i = 0 ; i < argdatasize[2] ; i++) 00513 *(finaldata + ALIGN(argdatasize[0])+ALIGN(argdatasize[1]) + i) = *(data3 + i); 00514 00515 printf("prashant\n"); 00516 for(j = 0 ; j < argdatasize[0] ; j++) 00517 printf("%x ",(char)*(data1 + j)); 00518 printf("\n\n\n"); 00519 for(j = 0 ; j < argdatasize[1] ; j++) 00520 printf("%x ",(char)*(data2 + j)); 00521 printf("\n\n\n"); 00522 for(j = 0 ; j < argdatasize[2] ; j++) 00523 printf("%x ",(char)*(data3 + j)); 00524 printf("\n\n\n"); 00525 00526 00527 for( i = 0 ; i < argtextsize[0] ; i++) 00528 *(finaltext + i ) = *(text1 + i); 00529 for( i = argtextsize[0] ; i < ALIGN(argtextsize[0]) ; i++) 00530 *(finaltext + i) = 0x90; 00531 00532 for( i = 0 ; i < argtextsize[1] ; i++) 00533 *(finaltext + ALIGN(argtextsize[0]) + i) = *(text2 + i); 00534 for( i = argtextsize[1] ; i < (ALIGN(argtextsize[0]) + ALIGN(argtextsize[1])) ; i++) 00535 *(finaltext + ALIGN(argtextsize[0]) + i) = 0x90; 00536 00537 for( i = 0 ; i < argtextsize[2] ; i++) 00538 *(finaltext + ALIGN(argtextsize[0]) + ALIGN(argtextsize[1])+ i) = *(text3 + i); 00539 for( i = argtextsize[2] ; i < (ALIGN(argtextsize[0]) + ALIGN(argtextsize[1]) + ALIGN(argtextsize[2])) ; i++) 00540 *(finaltext + ALIGN(argtextsize[0])+ALIGN(argtextsize[1]) + i) = 0; 00541 /* 00542 printf("prashant\n"); 00543 for(j = 0 ; j < argdatasize[0] ; j++) 00544 printf("%x ",(char)*(data1 + j)); 00545 printf("\n\n\n"); 00546 for(j = 0 ; j < argdatasize[1] ; j++) 00547 printf("%x ",(char)*(data2 + j)); 00548 printf("\n\n\n"); 00549 for(j = 0 ; j < argdatasize[2] ; j++) 00550 printf("%x ",(char)*(data3 + j)); 00551 printf("\n\n\n"); 00552 00553 00554 for( i = 0 ; i < argdatasize[0] ; i++) 00555 *(finaldata + i ) = *(data1 + i); 00556 for( i = argdatasize[0] ; i < ALIGN(argdatasize[0]) ; i++) 00557 *(finaldata + i) = 0; 00558 00559 for( i = 0 ; i < argdatasize[1] ; i++) 00560 *(finaldata + ALIGN(argdatasize[0]) + i ) = *(data2 + i); 00561 for( i = argdatasize[1] ; i < ALIGN(argdatasize[0]) + ALIGN(argdatasize[1]) ; i++) 00562 *(finaldata + ALIGN(argdatasize[0]) + i) = 0; 00563 00564 for( i = 0 ; i < argdatasize[2] ; i++) 00565 *(finaldata + ALIGN(argdatasize[0])+ALIGN(argdatasize[1]) + i) = *(data3 + i); 00566 */ 00567 for(j = 0 ; j < argdatasize[0] ; j++) 00568 printf("%x ",(char)*(data1 + j)); 00569 printf("\n\n\n"); 00570 for(j = 0 ; j < argdatasize[1] ; j++) 00571 printf("%x ",(char)*(data2 + j)); 00572 printf("\n\n\n"); 00573 for(j = 0 ; j < argdatasize[2] ; j++) 00574 printf("%x ",(char)*(data3 + j)); 00575 printf("\n\n\n"); 00576 00577 printf("\nRelocation of symbols in object file1\n"); 00578 00579 do_relocation(elf1,defined_object_file1,finaltext,finaldata,ALIGN(argtextsize[0]) + ALIGN(argtextsize[1]) + ALIGN(argtextsize[2]),0,0); 00580 printf("\nRelocation of symbols in object file2\n"); 00581 00582 do_relocation(elf2,defined_object_file2,finaltext,finaldata,ALIGN(argtextsize[0]) + ALIGN(argtextsize[1]) + ALIGN(argtextsize[2]),ALIGN(argtextsize[0]),ALIGN(argdatasize[0])); 00583 00584 00585 /* do_relocation(elf2,defined_object_file2,finaltext,finaldata,ALIGN(argtextsize[0]) + ALIGN(argtextsize[1]) + ALIGN(argtextsize[2]),ALIGN(argtextsize[0]) + ALIGN(argtextsize[2]), ALIGN(argdatasize[0]) + ALIGN(argdatasize[2])); 00586 */ 00587 00588 00589 int pfdr , pfdw; 00590 char pbuf[96]; 00591 pfdr = open(argv[4],O_RDONLY ); 00592 pfdw = open(argv[5],O_WRONLY|O_CREATE|O_RDONLY); 00593 read(pfdr,pbuf,96); 00594 00595 *(int*)(header_buf+0x44) = ALIGN(argtextsize[0]) + ALIGN(argtextsize[1]) +ALIGN(argtextsize[2])+ ALIGN(argdatasize[0]) +ALIGN(argdatasize[1]) + argdatasize[2];//filesz 00596 *(int*)(header_buf+0x48) = ALIGN(argtextsize[0]) + ALIGN(argtextsize[1]) +ALIGN(argtextsize[2])+ ALIGN(argdatasize[0]) +ALIGN(argdatasize[1]) + argdatasize[2];//memsz 00597 00598 printf("\n"); 00599 00600 00601 printf("\n"); 00602 write(pfdw,header_buf,96); 00603 write(pfdw,finaltext,ALIGN(argtextsize[0])+ALIGN(argtextsize[1])+ALIGN(argtextsize[2])); 00604 write(pfdw,finaldata,ALIGN(argdatasize[0])+ALIGN(argdatasize[1])+ALIGN(argdatasize[2])); 00605 close(pfdw); 00606 exit(1); 00607 }