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