rmphdr.c
Go to the documentation of this file.
1 /* rmphdr.c
2 /* remove program execution header and substantial segments
3 /* from an elf format object file.
4 /* May/1994 (c) Toshihiro Matsui, Electrotechnical Laboratory
5 /*
6 */
7 
8 
9 #include <stdio.h>
10 #include <sys/types.h>
11 #include <libelf.h> /* sys/elf.h is read */
12 #include <fcntl.h>
13 
14 #define min(x,y) ((x)<(y)?(x):(y))
15 
16 extern int errno;
17 
18 
19 void copy_bytes(infd, outfd, offset, nbytes)
20 int infd, outfd, offset, nbytes;
21 { char buf[8192];
22  int i, s;
23  lseek(infd, offset, SEEK_SET);
24  while (nbytes>0) {
25  s=min(8192, nbytes);
26  s=read(infd, buf, s);
27  if (s<=0) {
28  fprintf(stderr, "cannot read object %d", errno);
29  return;}
30  write(outfd, buf, s);
31  nbytes -= s;}
32  }
33 
35 int infd, outfd;
36 Elf32_Shdr *symtab;
37 { register int i;
38  int s, ss, k;
39  Elf32_Sym symbuf[16384]; /* 256KB */
40  unsigned char info;
41 
42  s=symtab->sh_size;
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); /*number of symtab entries*/
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);}
54 
55 main(argc,argv)
56 int argc;
57 char *argv[];
58 {
59  char *fname;
60  int elfd, outfd;
61  char outfname[128];
62  Elf *elf;
63  Elf32_Ehdr *ehdr, newehdr;
64  Elf_Scn *scn;
65  Elf32_Shdr *scnhdr, scntab[40], *symtab;
66  Elf_Data *data;
67  int scn_symt[40];
68  int i, j, scn_num, scn_count, scn_total_size, scn_offset;
69  char *cp;
70 
71  fname=argv[1];
72  sprintf(outfname,"%s.r",fname);
73  elfd=open(fname, 0);
74  outfd=open(outfname, O_RDWR | O_CREAT, 0777);
75  if (elfd<=0) {
76  fprintf(stderr, "%s cannot open %d\n", fname, errno);
77  exit(2);}
78  if (outfd<=0) {
79  fprintf(stderr, "%s cannot open %d\n", outfname, errno);
80  exit(2);}
81  fprintf(stderr, "%s opened\n", outfname);
82 
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));
88 /* printf("elfhdr=%x\n", ehdr); */
89 /* printf("e_ident="); */
90 /* for (i=0; i<16; i++) { printf("%2x ", ehdr->e_ident[i]);} */
91 
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);
105 
106  /* get all section headers and reindex section headers */
107  scn_num=newehdr.e_shnum;
108  scn_count=0;
109  scn_total_size=0;
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));
114 /* printf("sh_flags[%d]=%d\n", i, scntab[i].sh_flags); */
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; } }
119 
120  printf("ehdrsize=%d; %d sections are needed.\n",
121  sizeof(Elf32_Ehdr), scn_count);
122 
123  /* prepare a new elf header */
124  newehdr.e_type=ET_REL; /*relocatable*/
125  newehdr.e_entry=0;
126  newehdr.e_phoff=0; /* no program execution header*/
127  newehdr.e_shoff= sizeof(Elf32_Ehdr) + scn_total_size;
128  newehdr.e_phentsize=0;
129  newehdr.e_phnum=0;
130  newehdr.e_shstrndx=scn_symt[newehdr.e_shstrndx];
131  newehdr.e_shnum=scn_count; /* number of symbol table sections */
132 
133  fprintf(stderr, "ehdr updated\n");
134 
135  /* write new elf header */
136  write(outfd, &newehdr, sizeof(Elf32_Ehdr));
137 
138  /* no phdr written */
139  /* copy section contents */
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) {
144  copy_new_symtab(elfd, outfd, symtab);}
145  else
146  copy_bytes(elfd, outfd, scntab[i].sh_offset, scntab[i].sh_size);
147  }}
148 
149  /* write updated section headers */
150  /* write zeroth header */
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) {
155  j=scntab[i].sh_link;
156  if (j!=0) scntab[i].sh_link=scn_symt[j]; /*rewrite link*/
157  scntab[i].sh_offset=scn_offset;
158  scn_offset += scntab[i].sh_size;
159  write(outfd, scntab[i], sizeof(Elf32_Shdr));
160  } }
161 
162  elf_end(elf);
163  close(elfd);
164  close(outfd);
165  }
166 
167 
int infd
Definition: exsym.c:17
static int argc
Definition: transargv.c:56
int outfd
Definition: exsym.c:17
int errno
short s
Definition: structsize.c:2
#define min(x, y)
Definition: rmphdr.c:14
void copy_bytes(int infd, int outfd, int offset, int nbytes)
Definition: rmphdr.c:19
void copy_new_symtab(int infd, int outfd, Elf32_Shdr *symtab)
Definition: rmphdr.c:34
main(int argc, argv)
Definition: rmphdr.c:55
static char buf[CHAR_SIZE]
Definition: helpsub.c:23


euslisp
Author(s): Toshihiro Matsui
autogenerated on Fri Feb 21 2020 03:20:54