OSDN Git Service

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