sstrip.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /* http://www.muppetlabs.com/~breadbox/software/elfkickers.html */
  2. /* sstrip: Copyright (C) 1999-2001 by Brian Raiter, under the GNU
  3. * General Public License. No warranty. See COPYING for details.
  4. *
  5. * Aug 23, 2004 Hacked by Manuel Novoa III <mjn3@codepoet.org> to
  6. * handle targets of different endianness and/or elf class, making
  7. * it more useful in a cross-devel environment.
  8. */
  9. /* ============== original README ===================
  10. *
  11. * sstrip is a small utility that removes the contents at the end of an
  12. * ELF file that are not part of the program's memory image.
  13. *
  14. * Most ELF executables are built with both a program header table and a
  15. * section header table. However, only the former is required in order
  16. * for the OS to load, link and execute a program. sstrip attempts to
  17. * extract the ELF header, the program header table, and its contents,
  18. * leaving everything else in the bit bucket. It can only remove parts of
  19. * the file that occur at the end, after the parts to be saved. However,
  20. * this almost always includes the section header table, and occasionally
  21. * a few random sections that are not used when running a program.
  22. *
  23. * It should be noted that the GNU bfd library is (understandably)
  24. * dependent on the section header table as an index to the file's
  25. * contents. Thus, an executable file that has no section header table
  26. * cannot be used with gdb, objdump, or any other program based upon the
  27. * bfd library, at all. In fact, the program will not even recognize the
  28. * file as a valid executable. (This limitation is noted in the source
  29. * code comments for bfd, and is marked "FIXME", so this may change at
  30. * some future date. However, I would imagine that it is a pretty
  31. * low-priority item, as executables without a section header table are
  32. * rare in the extreme.) This probably also explains why strip doesn't
  33. * offer the option to do this.
  34. *
  35. * Shared library files may also have their section header table removed.
  36. * Such a library will still function; however, it will no longer be
  37. * possible for a compiler to link a new program against it.
  38. *
  39. * As an added bonus, sstrip also tries to removes trailing zero bytes
  40. * from the end of the file. (This normally cannot be done with an
  41. * executable that has a section header table.)
  42. *
  43. * sstrip is a very simplistic program. It depends upon the common
  44. * practice of putting the parts of the file that contribute to the
  45. * memory image at the front, and the remaining material at the end. This
  46. * permits it to discard the latter material without affecting file
  47. * offsets and memory addresses in what remains. Of course, the ELF
  48. * standard permits files to be organized in almost any order, so if a
  49. * pathological linker decided to put its section headers at the top,
  50. * sstrip would be useless on such executables.
  51. */
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <errno.h>
  56. #include <unistd.h>
  57. #include <fcntl.h>
  58. #include <elf.h>
  59. #include <endian.h>
  60. #include <byteswap.h>
  61. #ifndef TRUE
  62. #define TRUE 1
  63. #define FALSE 0
  64. #endif
  65. /* The name of the program.
  66. */
  67. static char const *progname;
  68. /* The name of the current file.
  69. */
  70. static char const *filename;
  71. /* A simple error-handling function. FALSE is always returned for the
  72. * convenience of the caller.
  73. */
  74. static int err(char const *errmsg)
  75. {
  76. fprintf(stderr, "%s: %s: %s\n", progname, filename, errmsg);
  77. return FALSE;
  78. }
  79. /* A flag to signal the need for endian reversal.
  80. */
  81. static int do_reverse_endian;
  82. /* Get a value from the elf header, compensating for endianness.
  83. */
  84. #define EGET(X) \
  85. (__extension__ ({ \
  86. uint64_t __res; \
  87. if (!do_reverse_endian) { \
  88. __res = (X); \
  89. } else if (sizeof(X) == 1) { \
  90. __res = (X); \
  91. } else if (sizeof(X) == 2) { \
  92. __res = bswap_16((X)); \
  93. } else if (sizeof(X) == 4) { \
  94. __res = bswap_32((X)); \
  95. } else if (sizeof(X) == 8) { \
  96. __res = bswap_64((X)); \
  97. } else { \
  98. fprintf(stderr, "%s: %s: EGET failed for size %d\n", \
  99. progname, filename, sizeof(X)); \
  100. exit(EXIT_FAILURE); \
  101. } \
  102. __res; \
  103. }))
  104. /* Set a value 'Y' in the elf header to 'X', compensating for endianness.
  105. */
  106. #define ESET(Y,X) \
  107. do if (!do_reverse_endian) { \
  108. Y = (X); \
  109. } else if (sizeof(Y) == 1) { \
  110. Y = (X); \
  111. } else if (sizeof(Y) == 2) { \
  112. Y = bswap_16((uint16_t)(X)); \
  113. } else if (sizeof(Y) == 4) { \
  114. Y = bswap_32((uint32_t)(X)); \
  115. } else if (sizeof(Y) == 8) { \
  116. Y = bswap_64((uint64_t)(X)); \
  117. } else { \
  118. fprintf(stderr, "%s: %s: ESET failed for size %d\n", \
  119. progname, filename, sizeof(Y)); \
  120. exit(EXIT_FAILURE); \
  121. } while (0)
  122. /* A macro for I/O errors: The given error message is used only when
  123. * errno is not set.
  124. */
  125. #define ferr(msg) (err(errno ? strerror(errno) : (msg)))
  126. #define HEADER_FUNCTIONS(CLASS) \
  127. \
  128. /* readelfheader() reads the ELF header into our global variable, and \
  129. * checks to make sure that this is in fact a file that we should be \
  130. * munging. \
  131. */ \
  132. static int readelfheader ## CLASS (int fd, Elf ## CLASS ## _Ehdr *ehdr) \
  133. { \
  134. if (read(fd, ((char *)ehdr)+EI_NIDENT, sizeof(*ehdr) - EI_NIDENT) \
  135. != sizeof(*ehdr) - EI_NIDENT) \
  136. return ferr("missing or incomplete ELF header."); \
  137. \
  138. /* Verify the sizes of the ELF header and the program segment \
  139. * header table entries. \
  140. */ \
  141. if (EGET(ehdr->e_ehsize) != sizeof(Elf ## CLASS ## _Ehdr)) \
  142. return err("unrecognized ELF header size."); \
  143. if (EGET(ehdr->e_phentsize) != sizeof(Elf ## CLASS ## _Phdr)) \
  144. return err("unrecognized program segment header size."); \
  145. \
  146. /* Finally, check the file type. \
  147. */ \
  148. if (EGET(ehdr->e_type) != ET_EXEC && EGET(ehdr->e_type) != ET_DYN) \
  149. return err("not an executable or shared-object library."); \
  150. \
  151. return TRUE; \
  152. } \
  153. \
  154. /* readphdrtable() loads the program segment header table into memory. \
  155. */ \
  156. static int readphdrtable ## CLASS (int fd, Elf ## CLASS ## _Ehdr const *ehdr, \
  157. Elf ## CLASS ## _Phdr **phdrs) \
  158. { \
  159. size_t size; \
  160. \
  161. if (!EGET(ehdr->e_phoff) || !EGET(ehdr->e_phnum) \
  162. ) return err("ELF file has no program header table."); \
  163. \
  164. size = EGET(ehdr->e_phnum) * sizeof **phdrs; \
  165. if (!(*phdrs = malloc(size))) \
  166. return err("Out of memory!"); \
  167. \
  168. errno = 0; \
  169. if (read(fd, *phdrs, size) != (ssize_t)size) \
  170. return ferr("missing or incomplete program segment header table."); \
  171. \
  172. return TRUE; \
  173. } \
  174. \
  175. /* getmemorysize() determines the offset of the last byte of the file \
  176. * that is referenced by an entry in the program segment header table. \
  177. * (Anything in the file after that point is not used when the program \
  178. * is executing, and thus can be safely discarded.) \
  179. */ \
  180. static int getmemorysize ## CLASS (Elf ## CLASS ## _Ehdr const *ehdr, \
  181. Elf ## CLASS ## _Phdr const *phdrs, \
  182. unsigned long *newsize) \
  183. { \
  184. Elf ## CLASS ## _Phdr const *phdr; \
  185. unsigned long size, n; \
  186. int i; \
  187. \
  188. /* Start by setting the size to include the ELF header and the \
  189. * complete program segment header table. \
  190. */ \
  191. size = EGET(ehdr->e_phoff) + EGET(ehdr->e_phnum) * sizeof *phdrs; \
  192. if (size < sizeof *ehdr) \
  193. size = sizeof *ehdr; \
  194. \
  195. /* Then keep extending the size to include whatever data the \
  196. * program segment header table references. \
  197. */ \
  198. for (i = 0, phdr = phdrs ; i < EGET(ehdr->e_phnum) ; ++i, ++phdr) { \
  199. if (EGET(phdr->p_type) != PT_NULL) { \
  200. n = EGET(phdr->p_offset) + EGET(phdr->p_filesz); \
  201. if (n > size) \
  202. size = n; \
  203. } \
  204. } \
  205. \
  206. *newsize = size; \
  207. return TRUE; \
  208. } \
  209. \
  210. /* modifyheaders() removes references to the section header table if \
  211. * it was stripped, and reduces program header table entries that \
  212. * included truncated bytes at the end of the file. \
  213. */ \
  214. static int modifyheaders ## CLASS (Elf ## CLASS ## _Ehdr *ehdr, \
  215. Elf ## CLASS ## _Phdr *phdrs, \
  216. unsigned long newsize) \
  217. { \
  218. Elf ## CLASS ## _Phdr *phdr; \
  219. int i; \
  220. \
  221. /* If the section header table is gone, then remove all references \
  222. * to it in the ELF header. \
  223. */ \
  224. if (EGET(ehdr->e_shoff) >= newsize) { \
  225. ESET(ehdr->e_shoff,0); \
  226. ESET(ehdr->e_shnum,0); \
  227. ESET(ehdr->e_shentsize,0); \
  228. ESET(ehdr->e_shstrndx,0); \
  229. } \
  230. \
  231. /* The program adjusts the file size of any segment that was \
  232. * truncated. The case of a segment being completely stripped out \
  233. * is handled separately. \
  234. */ \
  235. for (i = 0, phdr = phdrs ; i < EGET(ehdr->e_phnum) ; ++i, ++phdr) { \
  236. if (EGET(phdr->p_offset) >= newsize) { \
  237. ESET(phdr->p_offset,newsize); \
  238. ESET(phdr->p_filesz,0); \
  239. } else if (EGET(phdr->p_offset) + EGET(phdr->p_filesz) > newsize) { \
  240. newsize -= EGET(phdr->p_offset); \
  241. ESET(phdr->p_filesz, newsize); \
  242. } \
  243. } \
  244. \
  245. return TRUE; \
  246. } \
  247. \
  248. /* commitchanges() writes the new headers back to the original file \
  249. * and sets the file to its new size. \
  250. */ \
  251. static int commitchanges ## CLASS (int fd, Elf ## CLASS ## _Ehdr const *ehdr, \
  252. Elf ## CLASS ## _Phdr *phdrs, \
  253. unsigned long newsize) \
  254. { \
  255. size_t n; \
  256. \
  257. /* Save the changes to the ELF header, if any. \
  258. */ \
  259. if (lseek(fd, 0, SEEK_SET)) \
  260. return ferr("could not rewind file"); \
  261. errno = 0; \
  262. if (write(fd, ehdr, sizeof *ehdr) != sizeof *ehdr) \
  263. return err("could not modify file"); \
  264. \
  265. /* Save the changes to the program segment header table, if any. \
  266. */ \
  267. if (lseek(fd, EGET(ehdr->e_phoff), SEEK_SET) == (off_t)-1) { \
  268. err("could not seek in file."); \
  269. goto warning; \
  270. } \
  271. n = EGET(ehdr->e_phnum) * sizeof *phdrs; \
  272. if (write(fd, phdrs, n) != (ssize_t)n) { \
  273. err("could not write to file"); \
  274. goto warning; \
  275. } \
  276. \
  277. /* Eleventh-hour sanity check: don't truncate before the end of \
  278. * the program segment header table. \
  279. */ \
  280. if (newsize < EGET(ehdr->e_phoff) + n) \
  281. newsize = EGET(ehdr->e_phoff) + n; \
  282. \
  283. /* Chop off the end of the file. \
  284. */ \
  285. if (ftruncate(fd, newsize)) { \
  286. err("could not resize file"); \
  287. goto warning; \
  288. } \
  289. \
  290. return TRUE; \
  291. \
  292. warning: \
  293. return err("ELF file may have been corrupted!"); \
  294. }
  295. /* First elements of Elf32_Ehdr and Elf64_Ehdr are common.
  296. */
  297. static int readelfheaderident(int fd, Elf32_Ehdr *ehdr)
  298. {
  299. errno = 0;
  300. if (read(fd, ehdr, EI_NIDENT) != EI_NIDENT)
  301. return ferr("missing or incomplete ELF header.");
  302. /* Check the ELF signature.
  303. */
  304. if (!(ehdr->e_ident[EI_MAG0] == ELFMAG0 &&
  305. ehdr->e_ident[EI_MAG1] == ELFMAG1 &&
  306. ehdr->e_ident[EI_MAG2] == ELFMAG2 &&
  307. ehdr->e_ident[EI_MAG3] == ELFMAG3))
  308. {
  309. err("missing ELF signature.");
  310. return -1;
  311. }
  312. /* Compare the file's class and endianness with the program's.
  313. */
  314. #if __BYTE_ORDER == __LITTLE_ENDIAN
  315. if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
  316. do_reverse_endian = 0;
  317. } else if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
  318. /* fprintf(stderr, "ELF file has different endianness.\n"); */
  319. do_reverse_endian = 1;
  320. }
  321. #elif __BYTE_ORDER == __BIG_ENDIAN
  322. if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
  323. /* fprintf(stderr, "ELF file has different endianness.\n"); */
  324. do_reverse_endian = 1;
  325. } else if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
  326. do_reverse_endian = 0;
  327. }
  328. #else
  329. #error unkown endianness
  330. #endif
  331. else {
  332. err("Unsupported endianness");
  333. return -1;
  334. }
  335. /* Check the target architecture.
  336. */
  337. /* if (EGET(ehdr->e_machine) != ELF_ARCH) { */
  338. /* /\* return err("ELF file created for different architecture."); *\/ */
  339. /* fprintf(stderr, "ELF file created for different architecture.\n"); */
  340. /* } */
  341. return ehdr->e_ident[EI_CLASS];
  342. }
  343. HEADER_FUNCTIONS(32)
  344. HEADER_FUNCTIONS(64)
  345. /* truncatezeros() examines the bytes at the end of the file's
  346. * size-to-be, and reduces the size to exclude any trailing zero
  347. * bytes.
  348. */
  349. static int truncatezeros(int fd, unsigned long *newsize)
  350. {
  351. unsigned char contents[1024];
  352. unsigned long size, n;
  353. size = *newsize;
  354. do {
  355. n = sizeof contents;
  356. if (n > size)
  357. n = size;
  358. if (lseek(fd, size - n, SEEK_SET) == (off_t)-1)
  359. return ferr("cannot seek in file.");
  360. if (read(fd, contents, n) != (ssize_t)n)
  361. return ferr("cannot read file contents");
  362. while (n && !contents[--n])
  363. --size;
  364. } while (size && !n);
  365. /* Sanity check.
  366. */
  367. if (!size)
  368. return err("ELF file is completely blank!");
  369. *newsize = size;
  370. return TRUE;
  371. }
  372. /* main() loops over the cmdline arguments, leaving all the real work
  373. * to the other functions.
  374. */
  375. int main(int argc, char *argv[])
  376. {
  377. int fd;
  378. union {
  379. Elf32_Ehdr ehdr32;
  380. Elf64_Ehdr ehdr64;
  381. } e;
  382. union {
  383. Elf32_Phdr *phdrs32;
  384. Elf64_Phdr *phdrs64;
  385. } p;
  386. unsigned long newsize;
  387. char **arg;
  388. int failures = 0;
  389. if (argc < 2 || argv[1][0] == '-') {
  390. printf("Usage: sstrip FILE...\n"
  391. "sstrip discards all nonessential bytes from an executable.\n\n"
  392. "Version 2.0-X Copyright (C) 2000,2001 Brian Raiter.\n"
  393. "Cross-devel hacks Copyright (C) 2004 Manuel Novoa III.\n"
  394. "This program is free software, licensed under the GNU\n"
  395. "General Public License. There is absolutely no warranty.\n");
  396. return EXIT_SUCCESS;
  397. }
  398. progname = argv[0];
  399. for (arg = argv + 1 ; *arg != NULL ; ++arg) {
  400. filename = *arg;
  401. fd = open(*arg, O_RDWR);
  402. if (fd < 0) {
  403. ferr("can't open");
  404. ++failures;
  405. continue;
  406. }
  407. switch (readelfheaderident(fd, &e.ehdr32)) {
  408. case ELFCLASS32:
  409. if (!(readelfheader32(fd, &e.ehdr32) &&
  410. readphdrtable32(fd, &e.ehdr32, &p.phdrs32) &&
  411. getmemorysize32(&e.ehdr32, p.phdrs32, &newsize) &&
  412. truncatezeros(fd, &newsize) &&
  413. modifyheaders32(&e.ehdr32, p.phdrs32, newsize) &&
  414. commitchanges32(fd, &e.ehdr32, p.phdrs32, newsize)))
  415. ++failures;
  416. break;
  417. case ELFCLASS64:
  418. if (!(readelfheader64(fd, &e.ehdr64) &&
  419. readphdrtable64(fd, &e.ehdr64, &p.phdrs64) &&
  420. getmemorysize64(&e.ehdr64, p.phdrs64, &newsize) &&
  421. truncatezeros(fd, &newsize) &&
  422. modifyheaders64(&e.ehdr64, p.phdrs64, newsize) &&
  423. commitchanges64(fd, &e.ehdr64, p.phdrs64, newsize)))
  424. ++failures;
  425. break;
  426. default:
  427. ++failures;
  428. break;
  429. }
  430. close(fd);
  431. }
  432. return failures ? EXIT_FAILURE : EXIT_SUCCESS;
  433. }