OSDN Git Service

We renamed all the Blackfin relocs in the toolchain to match other ports
[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 #include <inttypes.h>
19 #include <assert.h>
20
21 /* macros for conversion between host and (internet) network byte order */
22 #ifndef WIN32
23 #include <netinet/in.h> /* Consts and structs defined by the internet system */
24 #define BINARY_FILE_OPTS
25 #else
26 #include <winsock2.h>
27 #define BINARY_FILE_OPTS "b"
28 #endif
29
30 #include "compress.h"
31 #include <libiberty.h>
32
33 /* from uClinux-x.x.x/include/linux */
34 #include "flat.h"     /* Binary flat header description                      */
35
36 #if defined(__MINGW32__)
37 #include <getopt.h>
38
39 #define mkstemp(p) mktemp(p)
40
41 #endif
42
43 #if defined TARGET_bfin
44 # define flat_get_relocate_addr(addr) (addr & 0x03ffffff)
45 #else
46 # define flat_get_relocate_addr(addr) (addr)
47 #endif
48
49 /****************************************************************************/
50
51 char *program_name;
52
53 static int print = 0, print_relocs = 0, docompress = 0, ramload = 0,
54            stacksize = 0, ktrace = 0, l1stack = 0;
55
56 /****************************************************************************/
57
58 void
59 process_file(char *ifile, char *ofile)
60 {
61         int old_flags, old_stack, new_flags, new_stack;
62         stream ifp, ofp;
63         struct flat_hdr old_hdr, new_hdr;
64         char *tfile, tmpbuf[256];
65         int input_error, output_error;
66
67         *tmpbuf = '\0';
68
69         if (fopen_stream_u(&ifp, ifile, "r" BINARY_FILE_OPTS)) {
70                 fprintf(stderr, "Cannot open %s\n", ifile);
71                 return;
72         }
73
74         if (fread_stream(&old_hdr, sizeof(old_hdr), 1, &ifp) != 1) {
75                 fprintf(stderr, "Cannot read header of %s\n", ifile);
76                 fclose_stream(&ifp);
77                 return;
78         }
79
80         if (strncmp(old_hdr.magic, "bFLT", 4) != 0) {
81                 fprintf(stderr, "Cannot read header of %s\n", ifile);
82                 fclose_stream(&ifp);
83                 return;
84         }
85
86         new_flags = old_flags = ntohl(old_hdr.flags);
87         new_stack = old_stack = ntohl(old_hdr.stack_size);
88         new_hdr = old_hdr;
89
90         if (docompress == 1) {
91                 new_flags |= FLAT_FLAG_GZIP;
92                 new_flags &= ~FLAT_FLAG_GZDATA;
93         } else if (docompress == 2) {
94                 new_flags |= FLAT_FLAG_GZDATA;
95                 new_flags &= ~FLAT_FLAG_GZIP;
96         } else if (docompress < 0)
97                 new_flags &= ~(FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA);
98         
99         if (ramload > 0)
100                 new_flags |= FLAT_FLAG_RAM;
101         else if (ramload < 0)
102                 new_flags &= ~FLAT_FLAG_RAM;
103         
104         if (ktrace > 0)
105                 new_flags |= FLAT_FLAG_KTRACE;
106         else if (ktrace < 0)
107                 new_flags &= ~FLAT_FLAG_KTRACE;
108         
109         if (l1stack > 0)
110                 new_flags |= FLAT_FLAG_L1STK;
111         else if (l1stack < 0)
112                 new_flags &= ~FLAT_FLAG_L1STK;
113
114         if (stacksize)
115                 new_stack = stacksize;
116
117         if (print == 1) {
118                 time_t t;
119                 uint32_t reloc_count, reloc_start;
120
121                 printf("%s\n", ifile);
122                 printf("    Magic:        %4.4s\n", old_hdr.magic);
123                 printf("    Rev:          %d\n",    ntohl(old_hdr.rev));
124                 t = (time_t) htonl(old_hdr.build_date);
125                 printf("    Build Date:   %s",      t?ctime(&t):"not specified\n");
126                 printf("    Entry:        0x%x\n",  ntohl(old_hdr.entry));
127                 printf("    Data Start:   0x%x\n",  ntohl(old_hdr.data_start));
128                 printf("    Data End:     0x%x\n",  ntohl(old_hdr.data_end));
129                 printf("    BSS End:      0x%x\n",  ntohl(old_hdr.bss_end));
130                 printf("    Stack Size:   0x%x\n",  ntohl(old_hdr.stack_size));
131                 reloc_start = ntohl(old_hdr.reloc_start);
132                 printf("    Reloc Start:  0x%x\n",  reloc_start);
133                 reloc_count = ntohl(old_hdr.reloc_count);
134                 printf("    Reloc Count:  0x%x\n",  reloc_count);
135                 printf("    Flags:        0x%x ( ",  ntohl(old_hdr.flags));
136                 if (old_flags) {
137                         if (old_flags & FLAT_FLAG_RAM)
138                                 printf("Load-to-Ram ");
139                         if (old_flags & FLAT_FLAG_GOTPIC)
140                                 printf("Has-PIC-GOT ");
141                         if (old_flags & FLAT_FLAG_GZIP)
142                                 printf("Gzip-Compressed ");
143                         if (old_flags & FLAT_FLAG_GZDATA)
144                                 printf("Gzip-Data-Compressed ");
145                         if (old_flags & FLAT_FLAG_KTRACE)
146                                 printf("Kernel-Traced-Load ");
147                         if (old_flags & FLAT_FLAG_L1STK)
148                                 printf("L1-Scratch-Stack ");
149                         printf(")\n");
150                 }
151
152                 if (print_relocs) {
153                         uint32_t *relocs = xcalloc(reloc_count, sizeof(uint32_t));
154                         uint32_t i;
155                         unsigned long r;
156
157                         printf("    Relocs:\n");
158                         printf("    #\treloc      (  address )\tdata\n");
159
160                         if (old_flags & FLAT_FLAG_GZIP)
161                                 reopen_stream_compressed(&ifp);
162                         if (fseek_stream(&ifp, reloc_start, SEEK_SET)) {
163                                 fprintf(stderr, "Cannot seek to relocs of %s\n", ifile);
164                                 fclose_stream(&ifp);
165                                 return;
166                         }
167                         if (fread_stream(relocs, sizeof(uint32_t), reloc_count, &ifp) == -1) {
168                                 fprintf(stderr, "Cannot read relocs of %s\n", ifile);
169                                 fclose_stream(&ifp);
170                                 return;
171                         }
172
173                         for (i = 0; i < reloc_count; ++i) {
174                                 uint32_t raddr, addr;
175                                 r = ntohl(relocs[i]);
176                                 raddr = flat_get_relocate_addr(r);
177                                 printf("    %u\t0x%08lx (0x%08"PRIx32")\t", i, r, raddr);
178                                 fseek_stream(&ifp, sizeof(old_hdr) + raddr, SEEK_SET);
179                                 fread_stream(&addr, sizeof(addr), 1, &ifp);
180                                 printf("%"PRIx32"\n", addr);
181                         }
182
183                         /* reset file position for below */
184                         fseek_stream(&ifp, sizeof(old_hdr), SEEK_SET);
185                 }
186         } else if (print > 1) {
187                 static int first = 1;
188                 unsigned int text, data, bss, stk, rel, tot;
189
190                 if (first) {
191                         printf("Flag Rev   Text   Data    BSS  Stack Relocs    RAM Filename\n");
192                         printf("-----------------------------------------------------------\n");
193                         first = 0;
194                 }
195                 *tmpbuf = '\0';
196                 strcat(tmpbuf, (old_flags & FLAT_FLAG_KTRACE) ? "k" : "");
197                 strcat(tmpbuf, (old_flags & FLAT_FLAG_RAM) ? "r" : "");
198                 strcat(tmpbuf, (old_flags & FLAT_FLAG_GOTPIC) ? "p" : "");
199                 strcat(tmpbuf, (old_flags & FLAT_FLAG_GZIP) ? "z" :
200                                         ((old_flags & FLAT_FLAG_GZDATA) ? "d" : ""));
201                 printf("-%-3.3s ", tmpbuf);
202                 printf("%3d ", ntohl(old_hdr.rev));
203                 printf("%6d ", text=ntohl(old_hdr.data_start)-sizeof(struct flat_hdr));
204                 printf("%6d ", data=ntohl(old_hdr.data_end)-ntohl(old_hdr.data_start));
205                 printf("%6d ", bss=ntohl(old_hdr.bss_end)-ntohl(old_hdr.data_end));
206                 printf("%6d ", stk=ntohl(old_hdr.stack_size));
207                 printf("%6d ", rel=ntohl(old_hdr.reloc_count) * 4);
208                 /*
209                  * work out how much RAM is needed per invocation, this
210                  * calculation is dependent on the binfmt_flat implementation
211                  */
212                 tot = data; /* always need data */
213
214                 if (old_flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))
215                         tot += text + sizeof(struct flat_hdr);
216                 
217                 if (bss + stk > rel) /* which is bigger ? */
218                         tot += bss + stk;
219                 else
220                         tot += rel;
221
222                 printf("%6d ", tot);
223                 /*
224                  * the total depends on whether the relocs are smaller/bigger than
225                  * the BSS
226                  */
227                 printf("%s\n", ifile);
228         }
229
230         /* if there is nothing else to do, leave */
231         if (new_flags == old_flags && new_stack == old_stack) {
232                 fclose_stream(&ifp);
233                 return;
234         }
235
236         new_hdr.flags = htonl(new_flags);
237         new_hdr.stack_size = htonl(new_stack);
238
239         tfile = make_temp_file("flthdr");
240
241         if (fopen_stream_u(&ofp, tfile, "w" BINARY_FILE_OPTS)) {
242                 fprintf(stderr, "Failed to open %s for writing\n", tfile);
243                 unlink(tfile);
244                 exit(1);
245         }
246
247         /* Copy header (always uncompressed).  */
248         if (fwrite_stream(&new_hdr, sizeof(new_hdr), 1, &ofp) != 1) {
249                 fprintf(stderr, "Failed to write to  %s\n", tfile);
250                 unlink(tfile);
251                 exit(1);
252         }
253
254         /* Whole input file (including text) is compressed: start decompressing
255            now.  */
256         if (old_flags & FLAT_FLAG_GZIP)
257                 reopen_stream_compressed(&ifp);
258
259         /* Likewise, output file is compressed. Start compressing now.  */
260         if (new_flags & FLAT_FLAG_GZIP) {
261                 printf("zflat %s --> %s\n", ifile, ofile);
262                 reopen_stream_compressed(&ofp);
263         }
264
265         transfer(&ifp, &ofp,
266                   ntohl(old_hdr.data_start) - sizeof(struct flat_hdr));
267
268         /* Only data and relocs were compressed in input.  Start decompressing
269            from here.  */
270         if (old_flags & FLAT_FLAG_GZDATA)
271                 reopen_stream_compressed(&ifp);
272
273         /* Only data/relocs to be compressed in output.  Start compressing
274            from here.  */
275         if (new_flags & FLAT_FLAG_GZDATA) {
276                 printf("zflat-data %s --> %s\n", ifile, ofile);
277                 reopen_stream_compressed(&ofp);
278         }
279
280         transfer(&ifp, &ofp, -1);
281
282         input_error = ferror_stream(&ifp);
283         output_error = ferror_stream(&ofp);
284
285         if (input_error || output_error) {
286                 fprintf(stderr, "Error on file pointer%s%s\n",
287                                 input_error ? " input" : "",
288                                 output_error ? " output" : "");
289                 unlink(tfile);
290                 exit(1);
291         }
292
293         fclose_stream(&ifp);
294         fclose_stream(&ofp);
295
296         /* Copy temporary file to output location.  */
297         fopen_stream_u(&ifp, tfile, "r" BINARY_FILE_OPTS);
298         fopen_stream_u(&ofp, ofile, "w" BINARY_FILE_OPTS);
299
300         transfer(&ifp, &ofp, -1);
301
302         fclose_stream(&ifp);
303         fclose_stream(&ofp);
304
305         unlink(tfile);
306         free(tfile);
307 }
308
309 /****************************************************************************/
310
311 void
312 usage(char *s)
313 {
314         if (s)
315                 fprintf(stderr, "%s\n", s);
316         fprintf(stderr, "usage: %s [options] flat-file\n", program_name);
317         fprintf(stderr, "       Allows you to change an existing flat file\n\n");
318         fprintf(stderr, "       -p      : print current settings\n");
319         fprintf(stderr, "       -P      : print relocations\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, "       -u      : place stack in L1 scratchpad memory\n");
328         fprintf(stderr, "       -U      : place stack in normal SDRAM memory\n");
329         fprintf(stderr, "       -s size : stack size\n");
330         fprintf(stderr, "       -o file : output-file\n"
331                         "                 (default is to modify input file)\n");
332         exit(1);
333 }
334
335 /****************************************************************************/
336
337 int
338 main(int argc, char *argv[])
339 {
340         int c;
341         char *ofile = NULL, *ifile;
342
343         program_name = argv[0];
344
345         while ((c = getopt(argc, argv, "pPdzZrRuUkKs:o:")) != EOF) {
346                 switch (c) {
347                 case 'p': print = 1;                break;
348                 case 'P': print_relocs = 1;         break;
349                 case 'z': docompress = 1;           break;
350                 case 'd': docompress = 2;           break;
351                 case 'Z': docompress = -1;          break;
352                 case 'r': ramload = 1;              break;
353                 case 'R': ramload = -1;             break;
354                 case 'k': ktrace = 1;               break;
355                 case 'K': ktrace = -1;              break;
356                 case 'u': l1stack = 1;              break;
357                 case 'U': l1stack = -1;             break;
358                 case 'o': ofile = optarg;           break;
359                 case 's':
360                         if (sscanf(optarg, "%i", &stacksize) != 1)
361                                 usage("invalid stack size");
362                         break;
363                 default:
364                         usage("invalid option");
365                         break;
366                 }
367         }
368
369         if (optind >= argc)
370                 usage("No input files provided");
371
372         if (ofile && argc - optind > 1)
373                 usage("-o can only be used with a single file");
374
375         if (!print && !docompress && !ramload && !stacksize) /* no args == print */
376                 print = argc - optind; /* greater than 1 is short format */
377         
378         for (c = optind; c < argc; c++) {
379                 ifile = argv[c];
380                 if (!ofile)
381                         ofile = ifile;
382                 process_file(ifile, ofile);
383                 ofile = NULL;
384         }
385         
386         exit(0);
387 }
388
389 /****************************************************************************/