10 #include <sys/types.h>
14 #define min(x,y) ((x)<(y)?(x):(y))
23 lseek(
infd, offset, SEEK_SET);
28 fprintf(stderr,
"cannot read object %d",
errno);
39 Elf32_Sym symbuf[16384];
43 if (
s>
sizeof(symbuf)) { fprintf(stderr,
"symtab too big %d\n",
s); exit(5);}
44 lseek(
infd, symtab->sh_offset, SEEK_SET);
45 ss=read(
infd, symbuf,
s);
46 if (ss!=
s) { fprintf(stderr,
"cannt read symtab %d\n", ss); exit(6);}
47 k=ss/
sizeof(Elf32_Sym);
48 for (i=symtab->sh_info; i<k; i++) {
49 info=symbuf[i].st_info;
50 if (ELF32_ST_TYPE(info)==STT_OBJECT) {
51 fprintf(stderr,
"symbol[%d] is OBJECT\n", i);
52 symbuf[i].st_info += 1;}}
53 write(
outfd, symbuf, ss);}
63 Elf32_Ehdr *ehdr, newehdr;
65 Elf32_Shdr *scnhdr, scntab[40], *symtab;
68 int i, j, scn_num, scn_count, scn_total_size, scn_offset;
72 sprintf(outfname,
"%s.r",fname);
74 outfd=open(outfname, O_RDWR | O_CREAT, 0777);
76 fprintf(stderr,
"%s cannot open %d\n", fname,
errno);
79 fprintf(stderr,
"%s cannot open %d\n", outfname,
errno);
81 fprintf(stderr,
"%s opened\n", outfname);
83 elf_version(EV_CURRENT);
84 elf=elf_begin(elfd, ELF_C_READ, 0);
85 if (elf==0) printf(
"%s\n", elf_errmsg(elf_errno()));
86 ehdr=elf32_getehdr(elf);
87 memcpy(&newehdr, ehdr,
sizeof(newehdr));
92 printf(
"\ne_type= %d\n", newehdr.e_type);
93 printf(
"e_machine= %d\n", newehdr.e_machine);
94 printf(
"e_version= %d\n", newehdr.e_version);
95 printf(
"e_entry= 0x%x\n", newehdr.e_entry);
96 printf(
"e_phoff= 0x%x\n", newehdr.e_phoff);
97 printf(
"e_shoff= 0x%x\n", newehdr.e_shoff);
98 printf(
"e_flags= 0x%x\n", newehdr.e_flags);
99 printf(
"e_ehsize= 0x%x\n", newehdr.e_ehsize);
100 printf(
"e_phentsize= %d\n", newehdr.e_phentsize);
101 printf(
"e_phnum= %d\n", newehdr.e_phnum);
102 printf(
"e_shentsize= %d\n", newehdr.e_shentsize);
103 printf(
"e_shnum= %d\n", newehdr.e_shnum);
104 printf(
"e_shstrndx= %d\n", newehdr.e_shstrndx);
107 scn_num=newehdr.e_shnum;
110 for (i=0; i<scn_num; i++) {
111 scn=elf_getscn(elf, i);
112 scnhdr=elf32_getshdr(scn);
113 memcpy(&scntab[i], scnhdr,
sizeof(Elf32_Shdr));
115 if (scntab[i].sh_flags==0) {
116 scn_symt[i]=scn_count++;
117 if (scntab[i].sh_type == SHT_SYMTAB) symtab=&scntab[i];
118 scn_total_size +=scntab[i].sh_size; } }
120 printf(
"ehdrsize=%d; %d sections are needed.\n",
121 sizeof(Elf32_Ehdr), scn_count);
124 newehdr.e_type=ET_REL;
127 newehdr.e_shoff=
sizeof(Elf32_Ehdr) + scn_total_size;
128 newehdr.e_phentsize=0;
130 newehdr.e_shstrndx=scn_symt[newehdr.e_shstrndx];
131 newehdr.e_shnum=scn_count;
133 fprintf(stderr,
"ehdr updated\n");
136 write(
outfd, &newehdr,
sizeof(Elf32_Ehdr));
140 for (i=1; i<scn_num; i++) {
141 if (scn_symt[i]!=0 && scntab[i].sh_type != SHT_NOBITS) {
142 fprintf(stderr,
"%d size=%d\n", i, scntab[i].sh_size);
143 if (scntab[i].sh_type==SHT_SYMTAB) {
151 scn_offset=
sizeof(Elf32_Ehdr);
152 write(
outfd, scntab[0],
sizeof(Elf32_Shdr));
153 for (i=1; i<scn_num; i++) {
154 if (scn_symt[i]!=0) {
156 if (j!=0) scntab[i].sh_link=scn_symt[j];
157 scntab[i].sh_offset=scn_offset;
158 scn_offset += scntab[i].sh_size;
159 write(
outfd, scntab[i],
sizeof(Elf32_Shdr));