diff --git a/src/stub/util/sstrip/sstrip.c b/src/stub/util/sstrip/sstrip.c index e109e8eb..68fdd3c5 100644 --- a/src/stub/util/sstrip/sstrip.c +++ b/src/stub/util/sstrip/sstrip.c @@ -2,32 +2,32 @@ * GNU General Public License. No warranty. See COPYING for details. */ -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include #ifndef TRUE -#define TRUE 1 -#define FALSE 0 +#define TRUE 1 +#define FALSE 0 #endif /* The memory-allocation macro. */ -#define alloc(p, n) (((p) = realloc(p, n)) \ - || (fputs("Out of memory.\n", stderr), \ - exit(EXIT_FAILURE), 0)) +#define alloc(p, n) (((p) = realloc(p, n)) \ + || (fputs("Out of memory.\n", stderr), \ + exit(EXIT_FAILURE), 0)) -static char const *thefilename; /* the current file name */ -static FILE *thefile; /* the current file handle */ +static char const *thefilename; /* the current file name */ +static FILE *thefile; /* the current file handle */ -static Elf32_Ehdr elfhdr; /* original ELF header */ -static Elf32_Phdr *phdrs = NULL; /* original program header tbl */ -static unsigned long phdrsize; /* size of program header tbl */ -static unsigned long newsize; /* size of the new file */ +static Elf32_Ehdr elfhdr; /* original ELF header */ +static Elf32_Phdr *phdrs = NULL; /* original program header tbl */ +static unsigned long phdrsize; /* size of program header tbl */ +static unsigned long newsize; /* size of the new file */ /* An error-handling function. The given error message is used only * when errno is not set. @@ -35,9 +35,9 @@ static unsigned long newsize; /* size of the new file */ static int err(char const *errmsg) { if (errno) - perror(thefilename); + perror(thefilename); else - fprintf(stderr, "%s: %s\n", thefilename, errmsg); + fprintf(stderr, "%s: %s\n", thefilename, errmsg); return FALSE; } @@ -47,44 +47,44 @@ static int err(char const *errmsg) */ static int readheaders(void) { - int bigend; + int bigend; errno = 0; if (fread(&elfhdr, sizeof elfhdr, 1, thefile) != 1) - return err("not an ELF file."); + return err("not an ELF file."); if (elfhdr.e_ident[EI_MAG0] != ELFMAG0 - || elfhdr.e_ident[EI_MAG1] != ELFMAG1 - || elfhdr.e_ident[EI_MAG2] != ELFMAG2 - || elfhdr.e_ident[EI_MAG3] != ELFMAG3) - return err("not an ELF file."); + || elfhdr.e_ident[EI_MAG1] != ELFMAG1 + || elfhdr.e_ident[EI_MAG2] != ELFMAG2 + || elfhdr.e_ident[EI_MAG3] != ELFMAG3) + return err("not an ELF file."); bigend = TRUE; *(char*)&bigend = 0; if (elfhdr.e_ident[EI_DATA] != (bigend ? ELFDATA2MSB : ELFDATA2LSB)) { - fprintf(stderr, "%s: not %s-endian.\n", - thefilename, bigend ? "big" : "little"); - return FALSE; + fprintf(stderr, "%s: not %s-endian.\n", + thefilename, bigend ? "big" : "little"); + return FALSE; } if (elfhdr.e_ehsize != sizeof(Elf32_Ehdr)) { - fprintf(stderr, "%s: unrecognized ELF header size " - "(size = %u instead of %u).\n", - thefilename, elfhdr.e_ehsize, sizeof(Elf32_Ehdr)); - return FALSE; + fprintf(stderr, "%s: unrecognized ELF header size " + "(size = %u instead of %u).\n", + thefilename, elfhdr.e_ehsize, sizeof(Elf32_Ehdr)); + return FALSE; } if (!elfhdr.e_phoff) - return err("no program header table."); + return err("no program header table."); if (elfhdr.e_phentsize != sizeof(Elf32_Phdr)) { - fprintf(stderr, "%s: unrecognized program header size " - "(size = %u instead of %u).\n", - thefilename, elfhdr.e_phentsize, sizeof(Elf32_Ehdr)); - return FALSE; + fprintf(stderr, "%s: unrecognized program header size " + "(size = %u instead of %u).\n", + thefilename, elfhdr.e_phentsize, sizeof(Elf32_Ehdr)); + return FALSE; } phdrsize = elfhdr.e_phnum * elfhdr.e_phentsize; alloc(phdrs, phdrsize); errno = 0; if (fread(phdrs, phdrsize, 1, thefile) != 1) - return err("invalid program header table."); + return err("invalid program header table."); return TRUE; } @@ -95,24 +95,24 @@ static int readheaders(void) */ static int getloadsize(void) { - Elf32_Phdr *phdr; - unsigned long n; - int i; + Elf32_Phdr *phdr; + unsigned long n; + int i; newsize = elfhdr.e_phoff + phdrsize; phdr = phdrs; for (i = 0 ; i < elfhdr.e_phnum ; ++i) { - if (phdr->p_type == PT_NULL || phdr->p_type == PT_NOTE) - continue; - n = phdr->p_offset + phdr->p_filesz; - if (n > newsize) - newsize = n; - phdr = (Elf32_Phdr*)((char*)phdr + elfhdr.e_phentsize); + if (phdr->p_type == PT_NULL || phdr->p_type == PT_NOTE) + continue; + n = phdr->p_offset + phdr->p_filesz; + if (n > newsize) + newsize = n; + phdr = (Elf32_Phdr*)((char*)phdr + elfhdr.e_phentsize); } for (i = 0 ; i < elfhdr.e_phnum ; ++i) - if (phdr->p_filesz > 0 && phdr->p_offset >= newsize) - memset(phdr, 0, elfhdr.e_phentsize); + if (phdr->p_filesz > 0 && phdr->p_offset >= newsize) + memset(phdr, 0, elfhdr.e_phentsize); return TRUE; } @@ -122,18 +122,18 @@ static int getloadsize(void) */ static int truncatezeros(void) { - char contents[1024]; - unsigned long n; + char contents[1024]; + unsigned long n; do { - n = sizeof contents; - if (n > newsize) - n = newsize; - if (fseek(thefile, newsize - n, SEEK_SET) - || fread(contents, n, 1, thefile) != 1) - return err("cannot read file contents"); - while (n && !contents[--n]) - --newsize; + n = sizeof contents; + if (n > newsize) + n = newsize; + if (fseek(thefile, newsize - n, SEEK_SET) + || fread(contents, n, 1, thefile) != 1) + return err("cannot read file contents"); + while (n && !contents[--n]) + --newsize; } while (newsize && !n); return TRUE; @@ -146,24 +146,24 @@ static int truncatezeros(void) static int modifyheaders(void) { Elf32_Phdr *phdr; - int i; + int i; if (elfhdr.e_shoff >= newsize) { - elfhdr.e_shoff = 0; - elfhdr.e_shnum = 0; - elfhdr.e_shentsize = 0; - elfhdr.e_shstrndx = 0; + elfhdr.e_shoff = 0; + elfhdr.e_shnum = 0; + elfhdr.e_shentsize = 0; + elfhdr.e_shstrndx = 0; } phdr = phdrs; for (i = 0 ; i < elfhdr.e_phnum ; ++i) { - if (phdr->p_offset + phdr->p_filesz > newsize) { - if (phdr->p_offset >= newsize) - phdr->p_filesz = 0; - else - phdr->p_filesz = newsize - phdr->p_offset; - } - phdr = (Elf32_Phdr*)((char*)phdr + elfhdr.e_phentsize); + if (phdr->p_offset + phdr->p_filesz > newsize) { + if (phdr->p_offset >= newsize) + phdr->p_filesz = 0; + else + phdr->p_filesz = newsize - phdr->p_offset; + } + phdr = (Elf32_Phdr*)((char*)phdr + elfhdr.e_phentsize); } return TRUE; @@ -178,11 +178,11 @@ static int savestripped(void) errno = 0; if (fwrite(&elfhdr, sizeof elfhdr, 1, thefile) != 1 - || fwrite(phdrs, phdrsize, 1, thefile) != 1 - || ftruncate(fileno(thefile), newsize)) { - err("could not write contents"); - fprintf(stderr, "WARNING: %s may be corrupted!\n", thefilename); - return FALSE; + || fwrite(phdrs, phdrsize, 1, thefile) != 1 + || ftruncate(fileno(thefile), newsize)) { + err("could not write contents"); + fprintf(stderr, "WARNING: %s may be corrupted!\n", thefilename); + return FALSE; } return TRUE; @@ -193,26 +193,32 @@ static int savestripped(void) */ int main(int argc, char *argv[]) { - char **arg; - int ret = 0; + char **arg; + int ret = 0; if (argc < 2 || !strcmp(argv[1], "-h")) { - printf("sstrip, version 2.0: Copyright (C) 1999 Brian Raiter\n" - "Usage: sstrip FILE...\n"); - return 0; + printf("sstrip, version 2.0: Copyright (C) 1999 Brian Raiter\n" + "Usage: sstrip FILE...\n"); + return 0; } for (arg = argv + 1 ; (thefilename = *arg) != NULL ; ++arg) { - if (!(thefile = fopen(thefilename, "rb+"))) { - err("unable to open."); - ++ret; - continue; - } - if (!readheaders() || !getloadsize() || !truncatezeros() - || !modifyheaders() || !savestripped()) - ++ret; - fclose(thefile); + if (!(thefile = fopen(thefilename, "rb+"))) { + err("unable to open."); + ++ret; + continue; + } + if (!readheaders() || !getloadsize() || !truncatezeros() + || !modifyheaders() || !savestripped()) + ++ret; + fclose(thefile); } return ret; } + + +/* +vi:ts=8:et:nowrap +*/ +