OSDN Git Service

Since the nios2 port is the only one to use the get_gp_value() function,
[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;
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 (stacksize)
110                 new_stack = stacksize;
111
112         if (print == 1) {
113                 time_t t;
114                 uint32_t reloc_count, reloc_start;
115
116                 printf("%s\n", ifile);
117                 printf("    Magic:        %4.4s\n", old_hdr.magic);
118                 printf("    Rev:          %d\n",    ntohl(old_hdr.rev));
119                 t = (time_t) htonl(old_hdr.build_date);
120                 printf("    Build Date:   %s",      t?ctime(&t):"not specified\n");
121                 printf("    Entry:        0x%x\n",  ntohl(old_hdr.entry));
122                 printf("    Data Start:   0x%x\n",  ntohl(old_hdr.data_start));
123                 printf("    Data End:     0x%x\n",  ntohl(old_hdr.data_end));
124                 printf("    BSS End:      0x%x\n",  ntohl(old_hdr.bss_end));
125                 printf("    Stack Size:   0x%x\n",  ntohl(old_hdr.stack_size));
126                 reloc_start = ntohl(old_hdr.reloc_start);
127                 printf("    Reloc Start:  0x%x\n",  reloc_start);
128                 reloc_count = ntohl(old_hdr.reloc_count);
129                 printf("    Reloc Count:  0x%x\n",  reloc_count);
130                 printf("    Flags:        0x%x ( ",  ntohl(old_hdr.flags));
131                 if (old_flags) {
132                         if (old_flags & FLAT_FLAG_RAM)
133                                 printf("Load-to-Ram ");
134                         if (old_flags & FLAT_FLAG_GOTPIC)
135                                 printf("Has-PIC-GOT ");
136                         if (old_flags & FLAT_FLAG_GZIP)
137                                 printf("Gzip-Compressed ");
138                         if (old_flags & FLAT_FLAG_GZDATA)
139                                 printf("Gzip-Data-Compressed ");
140                         if (old_flags & FLAT_FLAG_KTRACE)
141                                 printf("Kernel-Traced-Load ");
142                         printf(")\n");
143                 }
144
145                 if (print_relocs) {
146                         uint32_t *relocs = xcalloc(reloc_count, sizeof(uint32_t));
147                         uint32_t i;
148                         unsigned long r;
149
150                         printf("    Relocs:\n");
151                         printf("    #\treloc      (  address )\tdata\n");
152
153                         if (old_flags & FLAT_FLAG_GZIP)
154                                 reopen_stream_compressed(&ifp);
155                         if (fseek_stream(&ifp, reloc_start, SEEK_SET)) {
156                                 fprintf(stderr, "Cannot seek to relocs of %s\n", ifile);
157                                 fclose_stream(&ifp);
158                                 return;
159                         }
160                         if (fread_stream(relocs, sizeof(uint32_t), reloc_count, &ifp) == -1) {
161                                 fprintf(stderr, "Cannot read relocs of %s\n", ifile);
162                                 fclose_stream(&ifp);
163                                 return;
164                         }
165
166                         for (i = 0; i < reloc_count; ++i) {
167                                 uint32_t raddr, addr;
168                                 r = ntohl(relocs[i]);
169                                 raddr = flat_get_relocate_addr(r);
170                                 printf("    %u\t0x%08lx (0x%08"PRIx32")\t", i, r, raddr);
171                                 fseek_stream(&ifp, sizeof(old_hdr) + raddr, SEEK_SET);
172                                 fread_stream(&addr, sizeof(addr), 1, &ifp);
173                                 printf("%"PRIx32"\n", addr);
174                         }
175
176                         /* reset file position for below */
177                         fseek_stream(&ifp, sizeof(old_hdr), SEEK_SET);
178                 }
179         } else if (print > 1) {
180                 static int first = 1;
181                 unsigned int text, data, bss, stk, rel, tot;
182
183                 if (first) {
184                         printf("Flag Rev   Text   Data    BSS  Stack Relocs    RAM Filename\n");
185                         printf("-----------------------------------------------------------\n");
186                         first = 0;
187                 }
188                 *tmpbuf = '\0';
189                 strcat(tmpbuf, (old_flags & FLAT_FLAG_KTRACE) ? "k" : "");
190                 strcat(tmpbuf, (old_flags & FLAT_FLAG_RAM) ? "r" : "");
191                 strcat(tmpbuf, (old_flags & FLAT_FLAG_GOTPIC) ? "p" : "");
192                 strcat(tmpbuf, (old_flags & FLAT_FLAG_GZIP) ? "z" :
193                                         ((old_flags & FLAT_FLAG_GZDATA) ? "d" : ""));
194                 printf("-%-3.3s ", tmpbuf);
195                 printf("%3d ", ntohl(old_hdr.rev));
196                 printf("%6d ", text=ntohl(old_hdr.data_start)-sizeof(struct flat_hdr));
197                 printf("%6d ", data=ntohl(old_hdr.data_end)-ntohl(old_hdr.data_start));
198                 printf("%6d ", bss=ntohl(old_hdr.bss_end)-ntohl(old_hdr.data_end));
199                 printf("%6d ", stk=ntohl(old_hdr.stack_size));
200                 printf("%6d ", rel=ntohl(old_hdr.reloc_count) * 4);
201                 /*
202                  * work out how much RAM is needed per invocation, this
203                  * calculation is dependent on the binfmt_flat implementation
204                  */
205                 tot = data; /* always need data */
206
207                 if (old_flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))
208                         tot += text + sizeof(struct flat_hdr);
209                 
210                 if (bss + stk > rel) /* which is bigger ? */
211                         tot += bss + stk;
212                 else
213                         tot += rel;
214
215                 printf("%6d ", tot);
216                 /*
217                  * the total depends on whether the relocs are smaller/bigger than
218                  * the BSS
219                  */
220                 printf("%s\n", ifile);
221         }
222
223         /* if there is nothing else to do, leave */
224         if (new_flags == old_flags && new_stack == old_stack) {
225                 fclose_stream(&ifp);
226                 return;
227         }
228
229         new_hdr.flags = htonl(new_flags);
230         new_hdr.stack_size = htonl(new_stack);
231
232         tfile = make_temp_file("flthdr");
233
234         if (fopen_stream_u(&ofp, tfile, "w" BINARY_FILE_OPTS)) {
235                 fprintf(stderr, "Failed to open %s for writing\n", tfile);
236                 unlink(tfile);
237                 exit(1);
238         }
239
240         /* Copy header (always uncompressed).  */
241         if (fwrite_stream(&new_hdr, sizeof(new_hdr), 1, &ofp) != 1) {
242                 fprintf(stderr, "Failed to write to  %s\n", tfile);
243                 unlink(tfile);
244                 exit(1);
245         }
246
247         /* Whole input file (including text) is compressed: start decompressing
248            now.  */
249         if (old_flags & FLAT_FLAG_GZIP)
250                 reopen_stream_compressed(&ifp);
251
252         /* Likewise, output file is compressed. Start compressing now.  */
253         if (new_flags & FLAT_FLAG_GZIP) {
254                 printf("zflat %s --> %s\n", ifile, ofile);
255                 reopen_stream_compressed(&ofp);
256         }
257
258         transfer(&ifp, &ofp,
259                   ntohl(old_hdr.data_start) - sizeof(struct flat_hdr));
260
261         /* Only data and relocs were compressed in input.  Start decompressing
262            from here.  */
263         if (old_flags & FLAT_FLAG_GZDATA)
264                 reopen_stream_compressed(&ifp);
265
266         /* Only data/relocs to be compressed in output.  Start compressing
267            from here.  */
268         if (new_flags & FLAT_FLAG_GZDATA) {
269                 printf("zflat-data %s --> %s\n", ifile, ofile);
270                 reopen_stream_compressed(&ofp);
271         }
272
273         transfer(&ifp, &ofp, -1);
274
275         input_error = ferror_stream(&ifp);
276         output_error = ferror_stream(&ofp);
277
278         if (input_error || output_error) {
279                 fprintf(stderr, "Error on file pointer%s%s\n",
280                                 input_error ? " input" : "",
281                                 output_error ? " output" : "");
282                 unlink(tfile);
283                 exit(1);
284         }
285
286         fclose_stream(&ifp);
287         fclose_stream(&ofp);
288
289         /* Copy temporary file to output location.  */
290         fopen_stream_u(&ifp, tfile, "r" BINARY_FILE_OPTS);
291         fopen_stream_u(&ofp, ofile, "w" BINARY_FILE_OPTS);
292
293         transfer(&ifp, &ofp, -1);
294
295         fclose_stream(&ifp);
296         fclose_stream(&ofp);
297
298         unlink(tfile);
299         free(tfile);
300 }
301
302 /****************************************************************************/
303
304 void
305 usage(char *s)
306 {
307         if (s)
308                 fprintf(stderr, "%s\n", s);
309         fprintf(stderr, "usage: %s [options] flat-file\n", program_name);
310         fprintf(stderr, "       Allows you to change an existing flat file\n\n");
311         fprintf(stderr, "       -p      : print current settings\n");
312         fprintf(stderr, "       -P      : print relocations\n");
313         fprintf(stderr, "       -z      : compressed flat file\n");
314         fprintf(stderr, "       -d      : compressed data-only flat file\n");
315         fprintf(stderr, "       -Z      : un-compressed flat file\n");
316         fprintf(stderr, "       -r      : ram load\n");
317         fprintf(stderr, "       -R      : do not RAM load\n");
318         fprintf(stderr, "       -k      : kernel traced load (for debug)\n");
319         fprintf(stderr, "       -K      : normal non-kernel traced load\n");
320         fprintf(stderr, "       -s size : stack size\n");
321         fprintf(stderr, "       -o file : output-file\n"
322                         "                 (default is to modify input file)\n");
323         exit(1);
324 }
325
326 /****************************************************************************/
327
328 int
329 main(int argc, char *argv[])
330 {
331         int c;
332         char *ofile = NULL, *ifile;
333
334         program_name = argv[0];
335
336         while ((c = getopt(argc, argv, "pPdzZrRkKs:o:")) != EOF) {
337                 switch (c) {
338                 case 'p': print = 1;                break;
339                 case 'P': print_relocs = 1;         break;
340                 case 'z': docompress = 1;           break;
341                 case 'd': docompress = 2;           break;
342                 case 'Z': docompress = -1;          break;
343                 case 'r': ramload = 1;              break;
344                 case 'R': ramload = -1;             break;
345                 case 'k': ktrace = 1;               break;
346                 case 'K': ktrace = -1;              break;
347                 case 'o': ofile = optarg;           break;
348                 case 's':
349                         if (sscanf(optarg, "%i", &stacksize) != 1)
350                                 usage("invalid stack size");
351                         break;
352                 default:
353                         usage("invalid option");
354                         break;
355                 }
356         }
357
358         if (optind >= argc)
359                 usage("No input files provided");
360
361         if (ofile && argc - optind > 1)
362                 usage("-o can only be used with a single file");
363
364         if (!print && !docompress && !ramload && !stacksize) /* no args == print */
365                 print = argc - optind; /* greater than 1 is short format */
366         
367         for (c = optind; c < argc; c++) {
368                 ifile = argv[c];
369                 if (!ofile)
370                         ofile = ifile;
371                 process_file(ifile, ofile);
372                 ofile = NULL;
373         }
374         
375         exit(0);
376 }
377
378 /****************************************************************************/