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