13 #include <sys/types.h> 17 #define min(x,y) ((x)<(y)?(x):(y)) 26 lseek(infd, offset, SEEK_SET);
31 fprintf(stderr,
"cannot read object %d",
errno);
46 Elf32_Ehdr *ehdr, newehdr;
48 Elf32_Shdr *scnhdr, scntab[40];
51 int i, j, scn_num, scn_total_size, scn_offset;
56 sprintf(outfname,
"%s.p",fname);
58 outfd=open(outfname, O_RDWR | O_CREAT, 0777);
60 fprintf(stderr,
"%s cannot open %d\n", fname,
errno);
63 fprintf(stderr,
"%s cannot open %d\n", outfname,
errno);
65 fprintf(stderr,
"%s opened\n", outfname);
67 elf_version(EV_CURRENT);
68 elf=elf_begin(elfd, ELF_C_READ, 0);
69 if (elf==0) printf(
"%s\n", elf_errmsg(elf_errno()));
70 ehdr=elf32_getehdr(elf);
71 memcpy(&newehdr, ehdr,
sizeof(newehdr));
76 printf(
"\ne_type= %d\n", newehdr.e_type);
77 printf(
"e_machine= %d\n", newehdr.e_machine);
78 printf(
"e_version= %d\n", newehdr.e_version);
79 printf(
"e_entry= 0x%x\n", newehdr.e_entry);
80 printf(
"e_phoff= 0x%x\n", newehdr.e_phoff);
81 printf(
"e_shoff= 0x%x\n", newehdr.e_shoff);
82 printf(
"e_flags= 0x%x\n", newehdr.e_flags);
83 printf(
"e_ehsize= 0x%x\n", newehdr.e_ehsize);
84 printf(
"e_phentsize= %d\n", newehdr.e_phentsize);
85 printf(
"e_phnum= %d\n", newehdr.e_phnum);
86 printf(
"e_shentsize= %d\n", newehdr.e_shentsize);
87 printf(
"e_shnum= %d\n", newehdr.e_shnum);
88 printf(
"e_shstrndx= %d\n", newehdr.e_shstrndx);
91 scn_num=newehdr.e_shnum;
93 for (i=0; i<scn_num; i++) {
94 scn=elf_getscn(elf, i);
95 scnhdr=elf32_getshdr(scn);
96 memcpy(&scntab[i], scnhdr,
sizeof(Elf32_Shdr));
97 if (scntab[i].sh_flags) {
99 if (scntab[i].sh_type==SHT_PROGBITS)
100 scntab[i].sh_size=0;}
101 scn_total_size +=scntab[i].sh_size; }
104 newehdr.e_type=ET_REL;
107 newehdr.e_shoff=
sizeof(Elf32_Ehdr) + scn_total_size;
108 newehdr.e_phentsize=0;
112 write(outfd, &newehdr,
sizeof(Elf32_Ehdr));
113 fprintf(stderr,
"ehdr updated\n");
117 for (i=1; i<scn_num; i++) {
118 fprintf(stderr,
"%d size=%d\n", i, scntab[i].sh_size);
119 copy_bytes(elfd, outfd, scntab[i].sh_offset, scntab[i].sh_size);
124 scn_offset=
sizeof(Elf32_Ehdr);
125 write(outfd, scntab[0],
sizeof(Elf32_Shdr));
128 for (i=1; i<scn_num; i++) {
129 scntab[i].sh_offset=scn_offset;
130 scn_offset += scntab[i].sh_size;
131 write(outfd, scntab[i],
sizeof(Elf32_Shdr));
static char buf[CHAR_SIZE]
void copy_bytes(int infd, int outfd, int offset, int nbytes)