--- upx-2.02-src/src/stub/util/sstrip/sstrip.c.orig 2006-08-13 17:53:00.000000000 +0200 +++ upx-2.02-src/src/stub/util/sstrip/sstrip.c 2006-10-20 11:58:55.000000000 +0200 @@ -6,6 +6,10 @@ * Aug 23, 2004 Hacked by Manuel Novoa III to * handle targets of different endianness and/or elf class, making * it more useful in a cross-devel environment. + * + * Oct 2006, Frank Bergmann + * - set e_shentsize (default value of 40 on i386/32bit) + * - don't use *printf and stdio: smaller code and dietlibc "friendly" */ /* ============== original README =================== @@ -52,7 +56,7 @@ * sstrip would be useless on such executables. */ -#include +/* #include */ #include #include #include @@ -60,7 +64,6 @@ #include #include #include -#include #ifndef TRUE #define TRUE 1 @@ -75,13 +78,38 @@ */ static char const *filename; +/** + * bswap_* taken from dietlibc + * is GNUish and not portable + */ +static inline unsigned short bswap_16(unsigned short x) { + return (x>>8) | (x<<8); +} +static inline unsigned int bswap_32(unsigned int x) { + return (bswap_16(x&0xffff)<<16) | (bswap_16(x>>16)); +} +static inline unsigned long long bswap_64(unsigned long long x) { + return (((unsigned long long)bswap_32(x&0xffffffffull))<<32) | (bswap_32(x>>32)); +} + +int str_len(const char* in) { + register const char* t=in; + for (;;) { + if (!*t) break; ++t; + if (!*t) break; ++t; + if (!*t) break; ++t; + if (!*t) break; ++t; + } + return (int)(t-in); +} /* A simple error-handling function. FALSE is always returned for the * convenience of the caller. */ static int err(char const *errmsg) { - fprintf(stderr, "%s: %s: %s\n", progname, filename, errmsg); + write(2, errmsg, str_len(errmsg)); + /* fprintf(stderr, "%s: %s: %s\n", progname, filename, errmsg); */ return FALSE; } @@ -105,8 +133,7 @@ } else if (sizeof(X) == 8) { \ __res = bswap_64((X)); \ } else { \ - fprintf(stderr, "%s: %s: EGET failed for size %ld\n", \ - progname, filename, (long) sizeof(X)); \ + err("EGET failed for size\n"); \ exit(EXIT_FAILURE); \ } \ __res; \ @@ -126,8 +153,7 @@ } else if (sizeof(Y) == 8) { \ Y = bswap_64((uint64_t)(X)); \ } else { \ - fprintf(stderr, "%s: %s: ESET failed for size %ld\n", \ - progname, filename, (long) sizeof(Y)); \ + err("ESET failed for size\n"); \ exit(EXIT_FAILURE); \ } while (0) @@ -149,20 +175,20 @@ { \ if (read(fd, ((char *)ehdr)+EI_NIDENT, sizeof(*ehdr) - EI_NIDENT) \ != (ssize_t)sizeof(*ehdr) - EI_NIDENT) \ - return ferr("missing or incomplete ELF header."); \ + return ferr("missing or incomplete ELF header.\n"); \ \ /* Verify the sizes of the ELF header and the program segment \ * header table entries. \ */ \ if (EGET(ehdr->e_ehsize) != sizeof(Elf ## CLASS ## _Ehdr)) \ - return err("unrecognized ELF header size."); \ + return err("unrecognized ELF header size.\n"); \ if (EGET(ehdr->e_phentsize) != sizeof(Elf ## CLASS ## _Phdr)) \ - return err("unrecognized program segment header size."); \ + return err("unrecognized program segment header size.\n"); \ \ /* Finally, check the file type. \ */ \ if (EGET(ehdr->e_type) != ET_EXEC && EGET(ehdr->e_type) != ET_DYN) \ - return err("not an executable or shared-object library."); \ + return err("not an executable or shared-object library.\n"); \ \ return TRUE; \ } \ @@ -175,15 +201,15 @@ size_t size; \ \ if (!EGET(ehdr->e_phoff) || !EGET(ehdr->e_phnum) \ -) return err("ELF file has no program header table."); \ +) return err("ELF file has no program header table.\n"); \ \ size = EGET(ehdr->e_phnum) * sizeof **phdrs; \ if (!(*phdrs = malloc(size))) \ - return err("Out of memory!"); \ + return err("Out of memory!\n"); \ \ errno = 0; \ if (read(fd, *phdrs, size) != (ssize_t)size) \ - return ferr("missing or incomplete program segment header table."); \ + return ferr("missing or incomplete program segment header table.\n"); \ \ return TRUE; \ } \ @@ -240,7 +266,7 @@ if (EGET(ehdr->e_shoff) >= newsize) { \ ESET(ehdr->e_shoff,0); \ ESET(ehdr->e_shnum,0); \ - ESET(ehdr->e_shentsize,0); \ + ESET(ehdr->e_shentsize,sizeof(Elf ## CLASS ## _Shdr)); \ ESET(ehdr->e_shstrndx,0); \ } \ \ @@ -273,20 +299,20 @@ /* Save the changes to the ELF header, if any. \ */ \ if (lseek(fd, 0, SEEK_SET)) \ - return ferr("could not rewind file"); \ + return ferr("could not rewind file\n"); \ errno = 0; \ if (write(fd, ehdr, sizeof *ehdr) != (ssize_t)sizeof *ehdr) \ - return err("could not modify file"); \ + return err("could not modify file\n"); \ \ /* Save the changes to the program segment header table, if any. \ */ \ if (lseek(fd, EGET(ehdr->e_phoff), SEEK_SET) == (off_t)-1) { \ - err("could not seek in file."); \ + err("could not seek in file.\n"); \ goto warning; \ } \ n = EGET(ehdr->e_phnum) * sizeof *phdrs; \ if (write(fd, phdrs, n) != (ssize_t)n) { \ - err("could not write to file"); \ + err("could not write to file\n"); \ goto warning; \ } \ \ @@ -299,24 +325,23 @@ /* Chop off the end of the file. \ */ \ if (ftruncate(fd, newsize)) { \ - err("could not resize file"); \ + err("could not resize file\n"); \ goto warning; \ } \ \ return TRUE; \ \ warning: \ - return err("ELF file may have been corrupted!"); \ + return err("ELF file may have been corrupted!\n"); \ } - /* First elements of Elf32_Ehdr and Elf64_Ehdr are common. */ static int readelfheaderident(int fd, Elf32_Ehdr *ehdr) { errno = 0; if (read(fd, ehdr, EI_NIDENT) != EI_NIDENT) - return ferr("missing or incomplete ELF header."); + return ferr("missing or incomplete ELF header.\n"); /* Check the ELF signature. */ @@ -325,7 +350,7 @@ ehdr->e_ident[EI_MAG2] == ELFMAG2 && ehdr->e_ident[EI_MAG3] == ELFMAG3)) { - err("missing ELF signature."); + err("missing ELF signature.\n"); return -1; } @@ -349,7 +374,7 @@ #error unkown endianness #endif else { - err("Unsupported endianness"); + err("Unsupported endianness\n"); return -1; } @@ -382,9 +407,9 @@ if (n > size) n = size; if (lseek(fd, size - n, SEEK_SET) == (off_t)-1) - return ferr("cannot seek in file."); + return ferr("cannot seek in file.\n"); if (read(fd, contents, n) != (ssize_t)n) - return ferr("cannot read file contents"); + return ferr("cannot read file contents\n"); while (n && !contents[--n]) --size; } while (size && !n); @@ -392,7 +417,7 @@ /* Sanity check. */ if (!size) - return err("ELF file is completely blank!"); + return err("ELF file is completely blank!\n"); *newsize = size; return TRUE; @@ -417,7 +442,7 @@ int failures = 0; if (argc < 2 || argv[1][0] == '-') { - printf("Usage: sstrip FILE...\n" + err("Usage: sstrip FILE...\n" "sstrip discards all nonessential bytes from an executable.\n\n" "Version 2.0-X Copyright (C) 2000,2001 Brian Raiter.\n" "Cross-devel hacks Copyright (C) 2004 Manuel Novoa III.\n" @@ -433,7 +458,7 @@ fd = open(*arg, O_RDWR); if (fd < 0) { - ferr("can't open"); + ferr("can't open\n"); ++failures; continue; }