OSDN Git Service

Tweaked Makefile.in so that post-configured Makefile tests for a Cygwin build
[uclinux-h8/elf2flt.git] / flthdr.c
1 /****************************************************************************/
2 /*
3  *      A simple program to manipulate flat files
4  *
5  *      Copyright (C) 2001-2003 SnapGear Inc, davidm@snapgear.com
6  *      Copyright (C) 2001 Lineo, davidm@lineo.com
7  *
8  * This is Free Software, under the GNU Public Licence v2 or greater.
9  *
10  */
11 /****************************************************************************/
12
13 #include <stdio.h>    /* Userland pieces of the ANSI C standard I/O package  */
14 #include <unistd.h>   /* Userland prototypes of the Unix std system calls    */
15 #include <time.h>
16 #include <stdlib.h>   /* exit() */
17 #include <string.h>   /* strcat(), strcpy() */
18
19 /* macros for conversion between host and (internet) network byte order */
20 #include <netinet/in.h> /* Consts and structs defined by the internet system */
21
22 /* from uClinux-x.x.x/include/linux */
23 #include "flat.h"     /* Binary flat header description                      */
24
25 /****************************************************************************/
26
27 char *program_name;
28
29 static char cmd[1024];
30 static int print = 0, compress = 0, ramload = 0, stacksize = 0, ktrace = 0;
31 static int short_format = 0;
32
33 /****************************************************************************/
34
35 void
36 transferr(FILE *ifp, FILE *ofp, int count)
37 {
38         int n, num;
39
40         while (count == -1 || count > 0) {
41                 if (count == -1 || count > sizeof(cmd))
42                         num = sizeof(cmd);
43                 else
44                         num = count;
45                 n = fread(cmd, 1, num, ifp);
46                 if (n == 0)
47                         break;
48                 if (fwrite(cmd, n, 1, ofp) != 1) {
49                         fprintf(stderr, "Write failed :-(\n");
50                         exit(1);
51                 }
52                 if (count != -1)
53                         count -= n;
54         }
55         if (count > 0) {
56                 fprintf(stderr, "Failed to transferr %d bytes\n", count);
57                 exit(1);
58         }
59 }
60         
61 /****************************************************************************/
62
63 void
64 process_file(char *ifile, char *ofile)
65 {
66         int old_flags, old_stack, new_flags, new_stack;
67         FILE *ifp, *ofp;
68         int ofp_is_pipe = 0;
69         struct flat_hdr old_hdr, new_hdr;
70         char tfile[256];
71         char tfile2[256];
72
73         *tfile = *tfile2 = '\0';
74
75         if ((ifp = fopen(ifile, "rb")) == NULL) {
76                 fprintf(stderr, "Cannot open %s\n", ifile);
77                 return;
78         }
79
80         if (fread(&old_hdr, sizeof(old_hdr), 1, ifp) != 1) {
81                 fprintf(stderr, "Cannot read header of %s\n", ifile);
82                 return;
83         }
84
85         if (strncmp(old_hdr.magic, "bFLT", 4) != 0) {
86                 fprintf(stderr, "Cannot read header of %s\n", ifile);
87                 return;
88         }
89
90         new_flags = old_flags = ntohl(old_hdr.flags);
91         new_stack = old_stack = ntohl(old_hdr.stack_size);
92         new_hdr = old_hdr;
93
94         if (compress == 1) {
95                 new_flags |= FLAT_FLAG_GZIP;
96                 new_flags &= ~FLAT_FLAG_GZDATA;
97         } else if (compress == 2) {
98                 new_flags |= FLAT_FLAG_GZDATA;
99                 new_flags &= ~FLAT_FLAG_GZIP;
100         } else if (compress < 0)
101                 new_flags &= ~(FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA);
102         
103         if (ramload > 0)
104                 new_flags |= FLAT_FLAG_RAM;
105         else if (ramload < 0)
106                 new_flags &= ~FLAT_FLAG_RAM;
107         
108         if (ktrace > 0)
109                 new_flags |= FLAT_FLAG_KTRACE;
110         else if (ktrace < 0)
111                 new_flags &= ~FLAT_FLAG_KTRACE;
112         
113         if (stacksize)
114                 new_stack = stacksize;
115
116         if (print == 1) {
117                 time_t t;
118
119                 printf("%s\n", ifile);
120                 printf("    Magic:        %4.4s\n", old_hdr.magic);
121                 printf("    Rev:          %d\n",    ntohl(old_hdr.rev));
122                 t = (time_t) htonl(old_hdr.build_date);
123                 printf("    Build Date:   %s",      t?ctime(&t):"not specified\n");
124                 printf("    Entry:        0x%x\n",  ntohl(old_hdr.entry));
125                 printf("    Data Start:   0x%x\n",  ntohl(old_hdr.data_start));
126                 printf("    Data End:     0x%x\n",  ntohl(old_hdr.data_end));
127                 printf("    BSS End:      0x%x\n",  ntohl(old_hdr.bss_end));
128                 printf("    Stack Size:   0x%x\n",  ntohl(old_hdr.stack_size));
129                 printf("    Reloc Start:  0x%x\n",  ntohl(old_hdr.reloc_start));
130                 printf("    Reloc Count:  0x%x\n",  ntohl(old_hdr.reloc_count));
131                 printf("    Flags:        0x%x ( ",  ntohl(old_hdr.flags));
132                 if (old_flags) {
133                         if (old_flags & FLAT_FLAG_RAM)
134                                 printf("Load-to-Ram ");
135                         if (old_flags & FLAT_FLAG_GOTPIC)
136                                 printf("Has-PIC-GOT ");
137                         if (old_flags & FLAT_FLAG_GZIP)
138                                 printf("Gzip-Compressed ");
139                         if (old_flags & FLAT_FLAG_GZDATA)
140                                 printf("Gzip-Data-Compressed ");
141                         if (old_flags & FLAT_FLAG_KTRACE)
142                                 printf("Kernel-Traced-Load ");
143                         printf(")\n");
144                 }
145         } else if (print > 1) {
146                 static int first = 1;
147                 unsigned int text, data, bss, stk, rel, tot;
148
149                 if (first) {
150                         printf("Flag Rev   Text   Data    BSS  Stack Relocs    RAM Filename\n");
151                         printf("-----------------------------------------------------------\n");
152                         first = 0;
153                 }
154                 *tfile = '\0';
155                 strcat(tfile, (old_flags & FLAT_FLAG_KTRACE) ? "k" : "");
156                 strcat(tfile, (old_flags & FLAT_FLAG_RAM) ? "r" : "");
157                 strcat(tfile, (old_flags & FLAT_FLAG_GOTPIC) ? "p" : "");
158                 strcat(tfile, (old_flags & FLAT_FLAG_GZIP) ? "z" :
159                                         ((old_flags & FLAT_FLAG_GZDATA) ? "d" : ""));
160                 printf("-%-3.3s ", tfile);
161                 printf("%3d ", ntohl(old_hdr.rev));
162                 printf("%6d ", text=ntohl(old_hdr.data_start)-sizeof(struct flat_hdr));
163                 printf("%6d ", data=ntohl(old_hdr.data_end)-ntohl(old_hdr.data_start));
164                 printf("%6d ", bss=ntohl(old_hdr.bss_end)-ntohl(old_hdr.data_end));
165                 printf("%6d ", stk=ntohl(old_hdr.stack_size));
166                 printf("%6d ", rel=ntohl(old_hdr.reloc_count) * 4);
167                 /*
168                  * work out how much RAM is needed per invocation, this
169                  * calculation is dependent on the binfmt_flat implementation
170                  */
171                 tot = data; /* always need data */
172
173                 if (old_flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))
174                         tot += text + sizeof(struct flat_hdr);
175                 
176                 if (bss + stk > rel) /* which is bigger ? */
177                         tot += bss + stk;
178                 else
179                         tot += rel;
180
181                 printf("%6d ", tot);
182                 /*
183                  * the total depends on whether the relocs are smaller/bigger than
184                  * the BSS
185                  */
186                 printf("%s\n", ifile);
187         }
188
189         /* if there is nothing else to do, leave */
190         if (new_flags == old_flags && new_stack == old_stack)
191                 return;
192         
193         new_hdr.flags = htonl(new_flags);
194         new_hdr.stack_size = htonl(new_stack);
195
196         strcpy(tfile, "/tmp/flatXXXXXX");
197         mkstemp(tfile);
198         if ((ofp = fopen(tfile, "wb")) == NULL) {
199                 fprintf(stderr, "Failed to open %s for writing\n", tfile);
200                 unlink(tfile);
201                 unlink(tfile2);
202                 exit(1);
203         }
204
205         if (fwrite(&new_hdr, sizeof(new_hdr), 1, ofp) != 1) {
206                 fprintf(stderr, "Failed to write to  %s\n", tfile);
207                 unlink(tfile);
208                 unlink(tfile2);
209                 exit(1);
210         }
211
212         /*
213          * get ourselves a fully uncompressed copy of the text/data/relocs
214          * so that we can manipulate it more easily
215          */
216         if (old_flags & (FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA)) {
217                 FILE *tfp;
218
219                 strcpy(tfile2, "/tmp/flat2XXXXXX");
220                 mkstemp(tfile2);
221                 
222                 if (old_flags & FLAT_FLAG_GZDATA) {
223                         tfp = fopen(tfile2, "wb");
224                         if (!tfp) {
225                                 fprintf(stderr, "Failed to open %s for writing\n", tfile2);
226                                 exit(1);
227                         }
228                         transferr(ifp, tfp, ntohl(old_hdr.data_start) -
229                                         sizeof(struct flat_hdr));
230                         fclose(tfp);
231                 }
232
233                 sprintf(cmd, "gunzip >> %s", tfile2);
234                 tfp = popen(cmd, "wb");
235                 if(!tfp) {
236                         perror("popen");
237                         exit(1);
238                 }
239                 transferr(ifp, tfp, -1);
240                 pclose(tfp);
241
242                 fclose(ifp);
243                 ifp = fopen(tfile2, "rb");
244                 if (!ifp) {
245                         fprintf(stderr, "Failed to open %s for reading\n", tfile2);
246                         unlink(tfile);
247                         unlink(tfile2);
248                         exit(1);
249                 }
250         }
251
252         if (new_flags & FLAT_FLAG_GZIP) {
253                 printf("zflat %s --> %s\n", ifile, ofile);
254                 fclose(ofp);
255                 sprintf(cmd, "gzip -9 -f >> %s", tfile);
256                 ofp = popen(cmd, "wb");
257                 ofp_is_pipe = 1;
258         } else if (new_flags & FLAT_FLAG_GZDATA) {
259                 printf("zflat-data %s --> %s\n", ifile, ofile);
260                 transferr(ifp, ofp, ntohl(new_hdr.data_start) -
261                                 sizeof(struct flat_hdr));
262                 fclose(ofp);
263                 sprintf(cmd, "gzip -9 -f >> %s", tfile);
264                 ofp = popen(cmd, "wb");
265                 ofp_is_pipe = 1;
266         }
267
268         if (!ofp) { /* can only happen if using gzip/gunzip */
269                 fprintf(stderr, "Can't run cmd %s\n", cmd);
270                 unlink(tfile);
271                 unlink(tfile2);
272                 exit(1);
273         }
274
275         transferr(ifp, ofp, -1);
276         
277         if (ferror(ifp) || ferror(ofp)) {
278                 fprintf(stderr, "Error on file pointer%s%s\n",
279                                 ferror(ifp) ? " input" : "", ferror(ofp) ? " output" : "");
280                 unlink(tfile);
281                 unlink(tfile2);
282                 exit(1);
283         }
284
285         fclose(ifp);
286         if (ofp_is_pipe)
287                 pclose(ofp);
288         else
289                 fclose(ofp);
290
291         /* cheat a little here to preserve file permissions */
292         sprintf(cmd, "cp %s %s", tfile, ofile);
293         system(cmd);
294         unlink(tfile);
295         unlink(tfile2);
296 }
297
298 /****************************************************************************/
299
300 void
301 usage(char *s)
302 {
303         if (s)
304                 fprintf(stderr, "%s\n", s);
305         fprintf(stderr, "usage: %s [options] flat-file\n", program_name);
306         fprintf(stderr, "       Allows you to change an existing flat file\n\n");
307         fprintf(stderr, "       -p      : print current settings\n");
308         fprintf(stderr, "       -z      : compressed flat file\n");
309         fprintf(stderr, "       -d      : compressed data-only flat file\n");
310         fprintf(stderr, "       -Z      : un-compressed flat file\n");
311         fprintf(stderr, "       -r      : ram load\n");
312         fprintf(stderr, "       -R      : do not RAM load\n");
313         fprintf(stderr, "       -k      : kernel traced load (for debug)\n");
314         fprintf(stderr, "       -K      : normal non-kernel traced load\n");
315         fprintf(stderr, "       -s size : stack size\n");
316         fprintf(stderr, "       -o file : output-file\n"
317                         "                 (default is to modify input file)\n");
318         exit(1);
319 }
320
321 /****************************************************************************/
322
323 int
324 main(int argc, char *argv[])
325 {
326         int c;
327         char *ofile = NULL, *ifile;
328
329         program_name = argv[0];
330
331         while ((c = getopt(argc, argv, "pdzZrRkKs:o:")) != EOF) {
332                 switch (c) {
333                 case 'p': print = 1;                break;
334                 case 'z': compress = 1;             break;
335                 case 'd': compress = 2;             break;
336                 case 'Z': compress = -1;            break;
337                 case 'r': ramload = 1;              break;
338                 case 'R': ramload = -1;             break;
339                 case 'k': ktrace = 1;               break;
340                 case 'K': ktrace = -1;              break;
341                 case 's': stacksize = atoi(optarg); break;
342                 case 'o': ofile = optarg;           break;
343                 default:
344                         usage("invalid option");
345                         break;
346                 }
347         }
348
349         if (optind >= argc)
350                 usage("No input files provided");
351
352         if (ofile && argc - optind > 1)
353                 usage("-o can only be used with a single file");
354         
355         if (!print && !compress && !ramload && !stacksize) /* no args == print */
356                 print = argc - optind; /* greater than 1 is short format */
357         
358         for (c = optind; c < argc; c++) {
359                 ifile = argv[c];
360                 if (!ofile)
361                         ofile = ifile;
362                 process_file(ifile, ofile);
363                 ofile = NULL;
364         }
365         
366         exit(0);
367 }
368
369 /****************************************************************************/