| [9] | 1 | /* strip - remove symbols. Author: Dick van Veen */
|
|---|
| 2 |
|
|---|
| 3 | #include <sys/types.h>
|
|---|
| 4 | #include <sys/stat.h>
|
|---|
| 5 | #include <fcntl.h>
|
|---|
| 6 | #include <a.out.h>
|
|---|
| 7 | #include <unistd.h>
|
|---|
| 8 | #include <string.h>
|
|---|
| 9 | #include <stdlib.h>
|
|---|
| 10 | #include <stdio.h>
|
|---|
| 11 |
|
|---|
| 12 | /* Strip [file] ...
|
|---|
| 13 | *
|
|---|
| 14 | * - when no file is present, a.out is assumed.
|
|---|
| 15 | *
|
|---|
| 16 | */
|
|---|
| 17 |
|
|---|
| 18 | #define A_OUT "a.out"
|
|---|
| 19 | #define NAME_LENGTH 128 /* max file path name */
|
|---|
| 20 |
|
|---|
| 21 | char buffer[BUFSIZ]; /* used to copy executable */
|
|---|
| 22 | char new_file[NAME_LENGTH]; /* contains name of temporary */
|
|---|
| 23 | struct exec header;
|
|---|
| 24 |
|
|---|
| 25 | _PROTOTYPE(int main, (int argc, char **argv));
|
|---|
| 26 | _PROTOTYPE(void strip, (char *file));
|
|---|
| 27 | _PROTOTYPE(int read_header, (int fd));
|
|---|
| 28 | _PROTOTYPE(int write_header, (int fd));
|
|---|
| 29 | _PROTOTYPE(int make_tmp, (char *new_name, char *name));
|
|---|
| 30 | _PROTOTYPE(int copy_file, (int fd1, int fd2, long size));
|
|---|
| 31 |
|
|---|
| 32 | int main(argc, argv)
|
|---|
| 33 | int argc;
|
|---|
| 34 | char **argv;
|
|---|
| 35 | {
|
|---|
| 36 | argv++;
|
|---|
| 37 | if (*argv == NULL)
|
|---|
| 38 | strip(A_OUT);
|
|---|
| 39 | else
|
|---|
| 40 | while (*argv != NULL) {
|
|---|
| 41 | strip(*argv);
|
|---|
| 42 | argv++;
|
|---|
| 43 | }
|
|---|
| 44 | return(0);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | void strip(file)
|
|---|
| 48 | char *file;
|
|---|
| 49 | {
|
|---|
| 50 | int fd, new_fd;
|
|---|
| 51 | struct stat buf;
|
|---|
| 52 | long symb_size, relo_size;
|
|---|
| 53 |
|
|---|
| 54 | fd = open(file, O_RDONLY);
|
|---|
| 55 | if (fd == -1) {
|
|---|
| 56 | fprintf(stderr, "can't open %s\n", file);
|
|---|
| 57 | close(fd);
|
|---|
| 58 | return;
|
|---|
| 59 | }
|
|---|
| 60 | if (read_header(fd)) {
|
|---|
| 61 | fprintf(stderr, "%s: not an executable file\n", file);
|
|---|
| 62 | close(fd);
|
|---|
| 63 | return;
|
|---|
| 64 | }
|
|---|
| 65 | if (header.a_syms == 0L) {
|
|---|
| 66 | close(fd); /* no symbol table present */
|
|---|
| 67 | return;
|
|---|
| 68 | }
|
|---|
| 69 | symb_size = header.a_syms;
|
|---|
| 70 | header.a_syms = 0L; /* remove table size */
|
|---|
| 71 | fstat(fd, &buf);
|
|---|
| 72 | relo_size = buf.st_size - (A_MINHDR + header.a_text + header.a_data + symb_size);
|
|---|
| 73 | new_fd = make_tmp(new_file, file);
|
|---|
| 74 | if (new_fd == -1) {
|
|---|
| 75 | fprintf(stderr, "can't create temporary file\n");
|
|---|
| 76 | close(fd);
|
|---|
| 77 | return;
|
|---|
| 78 | }
|
|---|
| 79 | if (write_header(new_fd)) {
|
|---|
| 80 | fprintf(stderr, "can't write temporary file\n");
|
|---|
| 81 | unlink(new_file);
|
|---|
| 82 | close(fd);
|
|---|
| 83 | close(new_fd);
|
|---|
| 84 | return;
|
|---|
| 85 | }
|
|---|
| 86 | if (copy_file(fd, new_fd, header.a_text + header.a_data)) {
|
|---|
| 87 | fprintf(stderr, "can't copy %s\n", file);
|
|---|
| 88 | unlink(new_file);
|
|---|
| 89 | close(fd);
|
|---|
| 90 | close(new_fd);
|
|---|
| 91 | return;
|
|---|
| 92 | }
|
|---|
| 93 | if (relo_size != 0) {
|
|---|
| 94 | lseek(fd, symb_size, 1);
|
|---|
| 95 | if (copy_file(fd, new_fd, relo_size)) {
|
|---|
| 96 | fprintf(stderr, "can't copy %s\n", file);
|
|---|
| 97 | unlink(new_file);
|
|---|
| 98 | close(fd);
|
|---|
| 99 | close(new_fd);
|
|---|
| 100 | return;
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 | close(fd);
|
|---|
| 104 | close(new_fd);
|
|---|
| 105 | if (unlink(file) == -1) {
|
|---|
| 106 | fprintf(stderr, "can't unlink %s\n", file);
|
|---|
| 107 | unlink(new_file);
|
|---|
| 108 | return;
|
|---|
| 109 | }
|
|---|
| 110 | link(new_file, file);
|
|---|
| 111 | unlink(new_file);
|
|---|
| 112 | chmod(file, buf.st_mode);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | int read_header(fd)
|
|---|
| 116 | int fd;
|
|---|
| 117 | {
|
|---|
| 118 | if (read(fd, (char *) &header, A_MINHDR) != A_MINHDR) return(1);
|
|---|
| 119 | if (BADMAG(header)) return (1);
|
|---|
| 120 | if (header.a_hdrlen > sizeof(struct exec)) return (1);
|
|---|
| 121 | lseek(fd, 0L, SEEK_SET); /* variable size header */
|
|---|
| 122 | if (read(fd, (char *)&header, (int)header.a_hdrlen) != (int) header.a_hdrlen)
|
|---|
| 123 | return(1);
|
|---|
| 124 | return(0);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | int write_header(fd)
|
|---|
| 128 | int fd;
|
|---|
| 129 | {
|
|---|
| 130 | lseek(fd, 0L, SEEK_SET);
|
|---|
| 131 | if (write(fd, (char *)&header, (int)header.a_hdrlen) != (int)header.a_hdrlen)
|
|---|
| 132 | return(1);
|
|---|
| 133 | return(0);
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | int make_tmp(new_name, name)
|
|---|
| 137 | char *new_name, *name;
|
|---|
| 138 | {
|
|---|
| 139 | int len;
|
|---|
| 140 | char *nameptr;
|
|---|
| 141 |
|
|---|
| 142 | len = strlen(name);
|
|---|
| 143 | if (len + 1 > NAME_LENGTH) return(-1);
|
|---|
| 144 | strcpy(new_name, name);
|
|---|
| 145 | nameptr = strrchr(new_name, '/');
|
|---|
| 146 | if (nameptr == NULL) nameptr = new_name - 1;
|
|---|
| 147 | if (nameptr - new_name + 6 + 1 > NAME_LENGTH) return (-1);
|
|---|
| 148 | strcpy(nameptr + 1, "XXXXXX");
|
|---|
| 149 | mktemp(new_name);
|
|---|
| 150 | return(creat(new_name, 0777));
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | int copy_file(fd1, fd2, size)
|
|---|
| 154 | int fd1, fd2;
|
|---|
| 155 | long size;
|
|---|
| 156 | {
|
|---|
| 157 | int length;
|
|---|
| 158 |
|
|---|
| 159 | while (size > 0) {
|
|---|
| 160 | if (size < sizeof(buffer))
|
|---|
| 161 | length = size;
|
|---|
| 162 | else
|
|---|
| 163 | length = sizeof(buffer);
|
|---|
| 164 | if (read(fd1, buffer, length) != length) return(1);
|
|---|
| 165 | if (write(fd2, buffer, length) != length) return (1);
|
|---|
| 166 | size -= length;
|
|---|
| 167 | }
|
|---|
| 168 | return(0);
|
|---|
| 169 | }
|
|---|