Elf32_Shdr *libsh = NULL,*tmpsh = NULL,*objsh = NULL;
Elf32_Phdr *ph = NULL,*tmpph = NULL;
Elf32_Rela *obj_rela_text = NULL;
Elf32_Sym *objsym = NULL,*libsym = NULL,*tmpsym = NULL;
struct stat objst,libst;
unsigned char *libdata = NULL,*objdata = NULL,*tmpdata = NULL;
char *libsn = NULL,*objstr = NULL,*objsn = NULL,*libstr = NULL;
int i=0,objoff = 0,objlen = 0,fd,nobj_rela_text = 0;
int lib_got_ndx = 0,lib_plt_ndx = 0,lib_dynsym_ndx = 0,
lib_dynstr_ndx = 0,lib_hash_ndx = 0;
int obj_rela_text_ndx = 0,obj_symtab_ndx = 0,obj_strtab_ndx = 0,
obj_text_ndx = 0;
char *p = NULL;
unsigned long idx = 0;
if (argc < 3) {
fprintf(stderr,"Usage: %s objfile libfile\n",argv[0]);
fprintf(stderr," objfile : code will be inserted into libfile\n"
;
fprintf(stderr," libfile : Library file name\n"
;
return -1;
}
/* Open & mmap object file */
objfile = argv[1];
if ((fd=open(objfile,O_RDONLY)) < 0) {
perror("Open object file"
;
return 1;
}
fstat(fd,&objst);
if ((objdata=mmap(NULL,objst.st_size,PROT_READ,MAP_SHARED,fd,0)) == NULL){
perror("mmap"
;
return -1;
}
close(fd);
/* Open & mmap library file */
libfile = argv[2];
if ((fd=open(libfile,O_RDONLY)) < 0) {
perror("Open Library file"
;
return 1;
}
fstat(fd,&libst);
if ((libdata=mmap(NULL,libst.st_size,PROT_READ,MAP_SHARED,fd,0)) == NULL){
perror("mmap library file"
;
return -1;
}
close(fd);
/* Now get objfile infomation */
fprintf(stderr,"\nNow collecting obj file infomation .....\n"
;
objeh = (Elf32_Ehdr*)objdata;
#ifdef DEBUG
printf("obj: e_shoff:%#x,e_shnum:%d,e_shentsize:%d\n",
objeh->e_shoff,objeh->e_shnum,objeh->e_shentsize);
printf("obj: e_shstrndx:%d,e_phoff:%#x\n",
objeh->e_shstrndx,objeh->e_phoff);
#endif
objsh = (Elf32_Shdr*)(objdata + objeh->e_shoff);
objsn = (char*)(objdata + objsh[objeh->e_shstrndx].sh_offset);
{
Elf32_Shdr *tmp = objsh;
for (i=0; i<objeh->e_shnum; i++,tmp++) {
printf("%d off:%#10x size:%#10x entsize:%#10x %s\n",
i,tmp->sh_offset,tmp->sh_size,
tmp->sh_entsize,objsn + tmp->sh_name);
#ifdef sun
if (!strncmp(objsn + tmp->sh_name,".rela.text",10)) {
#else
if (!strncmp(objsn + tmp->sh_name,".rel.text",9)) {
#endif
obj_rela_text_ndx = i;
}
else if (!strncmp(objsn + tmp->sh_name,".symtab",7)) {
obj_symtab_ndx = i;
}
else if (!strncmp(objsn + tmp->sh_name,".strtab",7)) {
obj_strtab_ndx = i;
}
else if (!strncmp(objsn + tmp->sh_name,".text",5)) {
obj_text_ndx = i;
}
}
}
if (!obj_rela_text_ndx || !obj_symtab_ndx ||