OSDN Git Service

When we converted ld-elf2flt from the shell script to C, one small nuance
[uclinux-h8/elf2flt.git] / flthdr.c
index f3c54ab..dfc9107 100644 (file)
--- a/flthdr.c
+++ b/flthdr.c
 #include <time.h>
 #include <stdlib.h>   /* exit() */
 #include <string.h>   /* strcat(), strcpy() */
+#include <inttypes.h>
 #include <assert.h>
 
-/* macros for conversion between host and (internet) network byte order */
-#ifndef WIN32
-#include <netinet/in.h> /* Consts and structs defined by the internet system */
-#define        BINARY_FILE_OPTS
-#else
-#include <winsock2.h>
-#define        BINARY_FILE_OPTS "b"
-#endif
-
 #include "compress.h"
 #include <libiberty.h>
 
+#include "stubs.h"
+const char *elf2flt_progname;
+
 /* from uClinux-x.x.x/include/linux */
 #include "flat.h"     /* Binary flat header description                      */
 
 
 #endif
 
-/****************************************************************************/
+#if defined TARGET_bfin
+# define flat_get_relocate_addr(addr) (addr & 0x03ffffff)
+#else
+# define flat_get_relocate_addr(addr) (addr)
+#endif
 
-char *program_name;
+/****************************************************************************/
 
-static int print = 0, docompress = 0, ramload = 0, stacksize = 0, ktrace = 0;
+static int print = 0, print_relocs = 0, docompress = 0, ramload = 0,
+           stacksize = 0, ktrace = 0, l1stack = 0;
 
 /****************************************************************************/
 
@@ -65,11 +65,13 @@ process_file(char *ifile, char *ofile)
 
        if (fread_stream(&old_hdr, sizeof(old_hdr), 1, &ifp) != 1) {
                fprintf(stderr, "Cannot read header of %s\n", ifile);
+               fclose_stream(&ifp);
                return;
        }
 
        if (strncmp(old_hdr.magic, "bFLT", 4) != 0) {
                fprintf(stderr, "Cannot read header of %s\n", ifile);
+               fclose_stream(&ifp);
                return;
        }
 
@@ -96,11 +98,17 @@ process_file(char *ifile, char *ofile)
        else if (ktrace < 0)
                new_flags &= ~FLAT_FLAG_KTRACE;
        
+       if (l1stack > 0)
+               new_flags |= FLAT_FLAG_L1STK;
+       else if (l1stack < 0)
+               new_flags &= ~FLAT_FLAG_L1STK;
+
        if (stacksize)
                new_stack = stacksize;
 
        if (print == 1) {
                time_t t;
+               uint32_t reloc_count, reloc_start;
 
                printf("%s\n", ifile);
                printf("    Magic:        %4.4s\n", old_hdr.magic);
@@ -112,8 +120,10 @@ process_file(char *ifile, char *ofile)
                printf("    Data End:     0x%x\n",  ntohl(old_hdr.data_end));
                printf("    BSS End:      0x%x\n",  ntohl(old_hdr.bss_end));
                printf("    Stack Size:   0x%x\n",  ntohl(old_hdr.stack_size));
-               printf("    Reloc Start:  0x%x\n",  ntohl(old_hdr.reloc_start));
-               printf("    Reloc Count:  0x%x\n",  ntohl(old_hdr.reloc_count));
+               reloc_start = ntohl(old_hdr.reloc_start);
+               printf("    Reloc Start:  0x%x\n",  reloc_start);
+               reloc_count = ntohl(old_hdr.reloc_count);
+               printf("    Reloc Count:  0x%x\n",  reloc_count);
                printf("    Flags:        0x%x ( ",  ntohl(old_hdr.flags));
                if (old_flags) {
                        if (old_flags & FLAT_FLAG_RAM)
@@ -126,8 +136,45 @@ process_file(char *ifile, char *ofile)
                                printf("Gzip-Data-Compressed ");
                        if (old_flags & FLAT_FLAG_KTRACE)
                                printf("Kernel-Traced-Load ");
+                       if (old_flags & FLAT_FLAG_L1STK)
+                               printf("L1-Scratch-Stack ");
                        printf(")\n");
                }
+
+               if (print_relocs) {
+                       uint32_t *relocs = xcalloc(reloc_count, sizeof(uint32_t));
+                       uint32_t i;
+                       unsigned long r;
+
+                       printf("    Relocs:\n");
+                       printf("    #\treloc      (  address )\tdata\n");
+
+                       if (old_flags & FLAT_FLAG_GZIP)
+                               reopen_stream_compressed(&ifp);
+                       if (fseek_stream(&ifp, reloc_start, SEEK_SET)) {
+                               fprintf(stderr, "Cannot seek to relocs of %s\n", ifile);
+                               fclose_stream(&ifp);
+                               return;
+                       }
+                       if (fread_stream(relocs, sizeof(uint32_t), reloc_count, &ifp) == -1) {
+                               fprintf(stderr, "Cannot read relocs of %s\n", ifile);
+                               fclose_stream(&ifp);
+                               return;
+                       }
+
+                       for (i = 0; i < reloc_count; ++i) {
+                               uint32_t raddr, addr;
+                               r = ntohl(relocs[i]);
+                               raddr = flat_get_relocate_addr(r);
+                               printf("    %u\t0x%08lx (0x%08"PRIx32")\t", i, r, raddr);
+                               fseek_stream(&ifp, sizeof(old_hdr) + raddr, SEEK_SET);
+                               fread_stream(&addr, sizeof(addr), 1, &ifp);
+                               printf("%"PRIx32"\n", addr);
+                       }
+
+                       /* reset file position for below */
+                       fseek_stream(&ifp, sizeof(old_hdr), SEEK_SET);
+               }
        } else if (print > 1) {
                static int first = 1;
                unsigned int text, data, bss, stk, rel, tot;
@@ -173,25 +220,25 @@ process_file(char *ifile, char *ofile)
        }
 
        /* if there is nothing else to do, leave */
-       if (new_flags == old_flags && new_stack == old_stack)
+       if (new_flags == old_flags && new_stack == old_stack) {
+               fclose_stream(&ifp);
                return;
-       
+       }
+
        new_hdr.flags = htonl(new_flags);
        new_hdr.stack_size = htonl(new_stack);
 
        tfile = make_temp_file("flthdr");
 
        if (fopen_stream_u(&ofp, tfile, "w" BINARY_FILE_OPTS)) {
-               fprintf(stderr, "Failed to open %s for writing\n", tfile);
                unlink(tfile);
-               exit(1);
+               fatal("Failed to open %s for writing\n", tfile);
        }
 
        /* Copy header (always uncompressed).  */
        if (fwrite_stream(&new_hdr, sizeof(new_hdr), 1, &ofp) != 1) {
-               fprintf(stderr, "Failed to write to  %s\n", tfile);
                unlink(tfile);
-               exit(1);
+               fatal("Failed to write to  %s\n", tfile);
        }
 
        /* Whole input file (including text) is compressed: start decompressing
@@ -226,11 +273,10 @@ process_file(char *ifile, char *ofile)
        output_error = ferror_stream(&ofp);
 
        if (input_error || output_error) {
-               fprintf(stderr, "Error on file pointer%s%s\n",
+               unlink(tfile);
+               fatal("Error on file pointer%s%s\n",
                                input_error ? " input" : "",
                                output_error ? " output" : "");
-               unlink(tfile);
-               exit(1);
        }
 
        fclose_stream(&ifp);
@@ -256,9 +302,10 @@ usage(char *s)
 {
        if (s)
                fprintf(stderr, "%s\n", s);
-       fprintf(stderr, "usage: %s [options] flat-file\n", program_name);
+       fprintf(stderr, "usage: %s [options] flat-file\n", elf2flt_progname);
        fprintf(stderr, "       Allows you to change an existing flat file\n\n");
        fprintf(stderr, "       -p      : print current settings\n");
+       fprintf(stderr, "       -P      : print relocations\n");
        fprintf(stderr, "       -z      : compressed flat file\n");
        fprintf(stderr, "       -d      : compressed data-only flat file\n");
        fprintf(stderr, "       -Z      : un-compressed flat file\n");
@@ -266,6 +313,8 @@ usage(char *s)
        fprintf(stderr, "       -R      : do not RAM load\n");
        fprintf(stderr, "       -k      : kernel traced load (for debug)\n");
        fprintf(stderr, "       -K      : normal non-kernel traced load\n");
+       fprintf(stderr, "       -u      : place stack in L1 scratchpad memory\n");
+       fprintf(stderr, "       -U      : place stack in normal SDRAM memory\n");
        fprintf(stderr, "       -s size : stack size\n");
        fprintf(stderr, "       -o file : output-file\n"
                        "                 (default is to modify input file)\n");
@@ -277,14 +326,16 @@ usage(char *s)
 int
 main(int argc, char *argv[])
 {
-       int c;
+       int c, noargs;
        char *ofile = NULL, *ifile;
 
-       program_name = argv[0];
+       elf2flt_progname = argv[0];
 
-       while ((c = getopt(argc, argv, "pdzZrRkKs:o:")) != EOF) {
+       noargs = 1;
+       while ((c = getopt(argc, argv, "pPdzZrRuUkKs:o:")) != EOF) {
                switch (c) {
                case 'p': print = 1;                break;
+               case 'P': print_relocs = 1;         break;
                case 'z': docompress = 1;           break;
                case 'd': docompress = 2;           break;
                case 'Z': docompress = -1;          break;
@@ -292,6 +343,8 @@ main(int argc, char *argv[])
                case 'R': ramload = -1;             break;
                case 'k': ktrace = 1;               break;
                case 'K': ktrace = -1;              break;
+               case 'u': l1stack = 1;              break;
+               case 'U': l1stack = -1;             break;
                case 'o': ofile = optarg;           break;
                case 's':
                        if (sscanf(optarg, "%i", &stacksize) != 1)
@@ -301,6 +354,7 @@ main(int argc, char *argv[])
                        usage("invalid option");
                        break;
                }
+               noargs = 0;
        }
 
        if (optind >= argc)
@@ -309,9 +363,9 @@ main(int argc, char *argv[])
        if (ofile && argc - optind > 1)
                usage("-o can only be used with a single file");
 
-       if (!print && !docompress && !ramload && !stacksize) /* no args == print */
+       if (!print && noargs) /* no args == print */
                print = argc - optind; /* greater than 1 is short format */
-       
+
        for (c = optind; c < argc; c++) {
                ifile = argv[c];
                if (!ofile)