rmflags.c
Go to the documentation of this file.
1 /* rmphdr.c
2 /* read the elf format object file, and remove
3 /* program execution headers and nullify allocation flags,
4 /* so that the file is used to provide only relocation
5 /* information for a EusLisp's compiled code.
6 /*
7 /* May/1994 (c) Toshihiro Matsui, Electrotechnical Laboratory
8 /*
9 */
10 
11 
12 #include <stdio.h>
13 #include <sys/types.h>
14 #include <libelf.h> /* sys/elf.h is read */
15 #include <fcntl.h>
16 
17 #define min(x,y) ((x)<(y)?(x):(y))
18 
19 extern int errno;
20 
21 
22 void copy_bytes(infd, outfd, offset, nbytes)
23 int infd, outfd, offset, nbytes;
24 { char buf[8192];
25  int i, s;
26  lseek(infd, offset, SEEK_SET);
27  while (nbytes>0) {
28  s=min(8192, nbytes);
29  s=read(infd, buf, s);
30  if (s<=0) {
31  fprintf(stderr, "cannot read object %d", errno);
32  return;}
33  write(outfd, buf, s);
34  nbytes -= s;}
35  }
36 
37 
38 main(argc,argv)
39 int argc;
40 char *argv[];
41 {
42  char *fname;
43  int elfd, outfd;
44  char outfname[128];
45  Elf *elf;
46  Elf32_Ehdr *ehdr, newehdr;
47  Elf_Scn *scn;
48  Elf32_Shdr *scnhdr, scntab[40];
49  Elf_Data *data;
50  int scn_symt[40];
51  int i, j, scn_num, scn_total_size, scn_offset;
52  char *cp;
53 
54 
55  fname=argv[1];
56  sprintf(outfname,"%s.p",fname);
57  elfd=open(fname, 0);
58  outfd=open(outfname, O_RDWR | O_CREAT, 0777);
59  if (elfd<=0) {
60  fprintf(stderr, "%s cannot open %d\n", fname, errno);
61  exit(2);}
62  if (outfd<=0) {
63  fprintf(stderr, "%s cannot open %d\n", outfname, errno);
64  exit(2);}
65  fprintf(stderr, "%s opened\n", outfname);
66 
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));
72 /* printf("elfhdr=%x\n", ehdr); */
73 /* printf("e_ident="); */
74 /* for (i=0; i<16; i++) { printf("%2x ", ehdr->e_ident[i]);} */
75 
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);
89 
90  /* get all section headers, and remove flags */
91  scn_num=newehdr.e_shnum;
92  scn_total_size=0;
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) {
98  scntab[i].sh_flags=0; /* no write, alloc, or inst bit */
99  if (scntab[i].sh_type==SHT_PROGBITS)
100  scntab[i].sh_size=0;}
101  scn_total_size +=scntab[i].sh_size; }
102 
103  /* prepare a new elf header */
104  newehdr.e_type=ET_REL; /*relocatable*/
105  newehdr.e_entry=0;
106  newehdr.e_phoff=0; /* no program execution header*/
107  newehdr.e_shoff= sizeof(Elf32_Ehdr) + scn_total_size;
108  newehdr.e_phentsize=0;
109  newehdr.e_phnum=0;
110 
111  /* write new elf header */
112  write(outfd, &newehdr, sizeof(Elf32_Ehdr));
113  fprintf(stderr, "ehdr updated\n");
114 
115  /* ignore phdr's written */
116  /* copy section contents */
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);
120  }
121 
122  /* write updated section headers */
123  /* write zeroth header */
124  scn_offset=sizeof(Elf32_Ehdr);
125  write(outfd, scntab[0], sizeof(Elf32_Shdr));
126 
127  /* write section headers */
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));
132  }
133 
134  elf_end(elf);
135  close(elfd);
136  close(outfd);
137  }
138 
139 
int infd
Definition: exsym.c:17
main(int argc, argv)
Definition: rmflags.c:38
static int argc
Definition: transargv.c:56
#define min(x, y)
Definition: rmflags.c:17
int outfd
Definition: exsym.c:17
int errno
short s
Definition: structsize.c:2
void copy_bytes(int infd, int outfd, int offset, int nbytes)
Definition: rmflags.c:22
static char buf[CHAR_SIZE]
Definition: helpsub.c:23


euslisp
Author(s): Toshihiro Matsui
autogenerated on Mon Feb 28 2022 22:18:28