rmflags.c
Go to the documentation of this file.
00001 /* rmphdr.c
00002 /*      read the elf format object file, and remove
00003 /*      program execution headers and nullify allocation flags,
00004 /*      so that the file is used to provide only relocation
00005 /*      information for a EusLisp's compiled code.
00006 /*
00007 /* May/1994     (c) Toshihiro Matsui, Electrotechnical Laboratory
00008 /*
00009 */
00010 
00011 
00012 #include <stdio.h>
00013 #include <sys/types.h>
00014 #include <libelf.h>     /* sys/elf.h is read */
00015 #include <fcntl.h>
00016 
00017 #define min(x,y) ((x)<(y)?(x):(y))
00018 
00019 extern int errno;
00020 
00021 
00022 void copy_bytes(infd, outfd, offset, nbytes)
00023 int infd, outfd, offset, nbytes;
00024 { char buf[8192];
00025   int  i, s;
00026   lseek(infd, offset, SEEK_SET);
00027   while (nbytes>0) {
00028     s=min(8192, nbytes);
00029     s=read(infd, buf, s);
00030     if (s<=0) { 
00031         fprintf(stderr, "cannot read object %d", errno);
00032         return;}
00033     write(outfd, buf, s);
00034     nbytes -= s;}
00035   }
00036 
00037 
00038 main(argc,argv)
00039 int argc;
00040 char *argv[];
00041 {
00042   char *fname;
00043   int elfd, outfd;
00044   char outfname[128];
00045   Elf *elf;
00046   Elf32_Ehdr *ehdr, newehdr;
00047   Elf_Scn *scn;
00048   Elf32_Shdr *scnhdr, scntab[40];
00049   Elf_Data *data;
00050   int scn_symt[40];
00051   int i, j, scn_num, scn_total_size, scn_offset;
00052   char *cp;
00053   
00054 
00055   fname=argv[1];
00056   sprintf(outfname,"%s.p",fname);
00057   elfd=open(fname, 0);
00058   outfd=open(outfname, O_RDWR | O_CREAT, 0777);
00059   if (elfd<=0) {
00060     fprintf(stderr, "%s cannot open %d\n", fname, errno);
00061     exit(2);}
00062   if (outfd<=0) {
00063     fprintf(stderr, "%s cannot open %d\n", outfname, errno);
00064     exit(2);}
00065   fprintf(stderr, "%s opened\n", outfname);
00066 
00067   elf_version(EV_CURRENT);
00068   elf=elf_begin(elfd, ELF_C_READ, 0);
00069   if (elf==0) printf("%s\n", elf_errmsg(elf_errno()));
00070   ehdr=elf32_getehdr(elf);
00071   memcpy(&newehdr, ehdr, sizeof(newehdr));
00072 /*  printf("elfhdr=%x\n", ehdr); */
00073 /*  printf("e_ident="); */
00074 /*  for (i=0; i<16; i++) {  printf("%2x ", ehdr->e_ident[i]);} */
00075 
00076   printf("\ne_type= %d\n", newehdr.e_type);
00077   printf("e_machine= %d\n", newehdr.e_machine);
00078   printf("e_version= %d\n", newehdr.e_version);
00079   printf("e_entry= 0x%x\n", newehdr.e_entry);
00080   printf("e_phoff= 0x%x\n", newehdr.e_phoff);
00081   printf("e_shoff= 0x%x\n", newehdr.e_shoff);
00082   printf("e_flags= 0x%x\n", newehdr.e_flags);
00083   printf("e_ehsize= 0x%x\n", newehdr.e_ehsize);
00084   printf("e_phentsize= %d\n", newehdr.e_phentsize);
00085   printf("e_phnum= %d\n", newehdr.e_phnum);
00086   printf("e_shentsize= %d\n", newehdr.e_shentsize);
00087   printf("e_shnum= %d\n", newehdr.e_shnum);
00088   printf("e_shstrndx= %d\n", newehdr.e_shstrndx);
00089 
00090   /* get all section headers, and remove flags */
00091   scn_num=newehdr.e_shnum;
00092   scn_total_size=0;
00093   for (i=0; i<scn_num; i++) {
00094     scn=elf_getscn(elf, i);
00095     scnhdr=elf32_getshdr(scn);
00096     memcpy(&scntab[i], scnhdr, sizeof(Elf32_Shdr));
00097     if (scntab[i].sh_flags) {
00098       scntab[i].sh_flags=0;     /* no write, alloc, or inst bit */
00099       if (scntab[i].sh_type==SHT_PROGBITS)
00100         scntab[i].sh_size=0;}
00101     scn_total_size +=scntab[i].sh_size; } 
00102 
00103   /* prepare a new elf header */
00104   newehdr.e_type=ET_REL;        /*relocatable*/
00105   newehdr.e_entry=0;
00106   newehdr.e_phoff=0;    /* no program execution header*/
00107   newehdr.e_shoff= sizeof(Elf32_Ehdr) + scn_total_size;
00108   newehdr.e_phentsize=0;
00109   newehdr.e_phnum=0;
00110 
00111   /* write new elf header */
00112   write(outfd, &newehdr, sizeof(Elf32_Ehdr));
00113   fprintf(stderr, "ehdr updated\n");
00114 
00115   /* ignore phdr's written */
00116   /* copy section contents */
00117   for (i=1; i<scn_num; i++) {
00118     fprintf(stderr, "%d size=%d\n", i, scntab[i].sh_size);
00119     copy_bytes(elfd, outfd, scntab[i].sh_offset, scntab[i].sh_size);
00120     }
00121 
00122   /* write updated section headers */
00123   /* write zeroth header */
00124   scn_offset=sizeof(Elf32_Ehdr);
00125   write(outfd, scntab[0], sizeof(Elf32_Shdr));
00126 
00127   /* write section headers */
00128   for (i=1; i<scn_num; i++) {
00129     scntab[i].sh_offset=scn_offset;
00130     scn_offset += scntab[i].sh_size;
00131     write(outfd, scntab[i], sizeof(Elf32_Shdr));
00132     }
00133 
00134   elf_end(elf);
00135   close(elfd);
00136   close(outfd);
00137   }
00138 
00139 


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Jun 6 2019 18:05:53