File: rmflags.c

package info (click to toggle)
euslisp 9.31%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,448 kB
  • sloc: ansic: 41,610; lisp: 3,339; makefile: 286; sh: 238; asm: 138; python: 53
file content (139 lines) | stat: -rw-r--r-- 3,791 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* rmphdr.c
/*	read the elf format object file, and remove
/*	program execution headers and nullify allocation flags,
/*	so that the file is used to provide only relocation
/*	information for a EusLisp's compiled code.
/*
/* May/1994	(c) Toshihiro Matsui, Electrotechnical Laboratory
/*
*/


#include <stdio.h>
#include <sys/types.h>
#include <libelf.h>	/* sys/elf.h is read */
#include <fcntl.h>

#define min(x,y) ((x)<(y)?(x):(y))

extern int errno;


void copy_bytes(infd, outfd, offset, nbytes)
int infd, outfd, offset, nbytes;
{ char buf[8192];
  int  i, s;
  lseek(infd, offset, SEEK_SET);
  while (nbytes>0) {
    s=min(8192, nbytes);
    s=read(infd, buf, s);
    if (s<=0) { 
	fprintf(stderr, "cannot read object %d", errno);
	return;}
    write(outfd, buf, s);
    nbytes -= s;}
  }


main(argc,argv)
int argc;
char *argv[];
{
  char *fname;
  int elfd, outfd;
  char outfname[128];
  Elf *elf;
  Elf32_Ehdr *ehdr, newehdr;
  Elf_Scn *scn;
  Elf32_Shdr *scnhdr, scntab[40];
  Elf_Data *data;
  int scn_symt[40];
  int i, j, scn_num, scn_total_size, scn_offset;
  char *cp;
  

  fname=argv[1];
  sprintf(outfname,"%s.p",fname);
  elfd=open(fname, 0);
  outfd=open(outfname, O_RDWR | O_CREAT, 0777);
  if (elfd<=0) {
    fprintf(stderr, "%s cannot open %d\n", fname, errno);
    exit(2);}
  if (outfd<=0) {
    fprintf(stderr, "%s cannot open %d\n", outfname, errno);
    exit(2);}
  fprintf(stderr, "%s opened\n", outfname);

  elf_version(EV_CURRENT);
  elf=elf_begin(elfd, ELF_C_READ, 0);
  if (elf==0) printf("%s\n", elf_errmsg(elf_errno()));
  ehdr=elf32_getehdr(elf);
  memcpy(&newehdr, ehdr, sizeof(newehdr));
/*  printf("elfhdr=%x\n", ehdr); */
/*  printf("e_ident="); */
/*  for (i=0; i<16; i++) {  printf("%2x ", ehdr->e_ident[i]);} */

  printf("\ne_type= %d\n", newehdr.e_type);
  printf("e_machine= %d\n", newehdr.e_machine);
  printf("e_version= %d\n", newehdr.e_version);
  printf("e_entry= 0x%x\n", newehdr.e_entry);
  printf("e_phoff= 0x%x\n", newehdr.e_phoff);
  printf("e_shoff= 0x%x\n", newehdr.e_shoff);
  printf("e_flags= 0x%x\n", newehdr.e_flags);
  printf("e_ehsize= 0x%x\n", newehdr.e_ehsize);
  printf("e_phentsize= %d\n", newehdr.e_phentsize);
  printf("e_phnum= %d\n", newehdr.e_phnum);
  printf("e_shentsize= %d\n", newehdr.e_shentsize);
  printf("e_shnum= %d\n", newehdr.e_shnum);
  printf("e_shstrndx= %d\n", newehdr.e_shstrndx);

  /* get all section headers, and remove flags */
  scn_num=newehdr.e_shnum;
  scn_total_size=0;
  for (i=0; i<scn_num; i++) {
    scn=elf_getscn(elf, i);
    scnhdr=elf32_getshdr(scn);
    memcpy(&scntab[i], scnhdr, sizeof(Elf32_Shdr));
    if (scntab[i].sh_flags) {
      scntab[i].sh_flags=0;	/* no write, alloc, or inst bit */
      if (scntab[i].sh_type==SHT_PROGBITS)
	scntab[i].sh_size=0;}
    scn_total_size +=scntab[i].sh_size; } 

  /* prepare a new elf header */
  newehdr.e_type=ET_REL;	/*relocatable*/
  newehdr.e_entry=0;
  newehdr.e_phoff=0;	/* no program execution header*/
  newehdr.e_shoff= sizeof(Elf32_Ehdr) + scn_total_size;
  newehdr.e_phentsize=0;
  newehdr.e_phnum=0;

  /* write new elf header */
  write(outfd, &newehdr, sizeof(Elf32_Ehdr));
  fprintf(stderr, "ehdr updated\n");

  /* ignore phdr's written */
  /* copy section contents */
  for (i=1; i<scn_num; i++) {
    fprintf(stderr, "%d size=%d\n", i, scntab[i].sh_size);
    copy_bytes(elfd, outfd, scntab[i].sh_offset, scntab[i].sh_size);
    }

  /* write updated section headers */
  /* write zeroth header */
  scn_offset=sizeof(Elf32_Ehdr);
  write(outfd, scntab[0], sizeof(Elf32_Shdr));

  /* write section headers */
  for (i=1; i<scn_num; i++) {
    scntab[i].sh_offset=scn_offset;
    scn_offset += scntab[i].sh_size;
    write(outfd, scntab[i], sizeof(Elf32_Shdr));
    }

  elf_end(elf);
  close(elfd);
  close(outfd);
  }