OSDN Git Service

gcc 3.1 and 3.1.1 create a .jcr ELF section, which apparently is intended to
[uclinux-h8/elf2flt.git] / elf2flt.c
index 324ef3d..a1e8e9a 100644 (file)
--- a/elf2flt.c
+++ b/elf2flt.c
  * krab@california.daimi.aau.dk
  */
  
-#include <stdio.h>
-#include <fcntl.h>
-#include <stdlib.h>
-#include <sys/types.h>
-#include <netinet/in.h>
-#include <getopt.h>
-#include <stdarg.h>
-#include <bfd.h>
-#include <elf.h>
-#include "flat.h"
+#include <stdio.h>    /* Userland pieces of the ANSI C standard I/O package  */
+#include <stdlib.h>   /* Userland prototypes of the ANSI C std lib functions */
+#include <stdarg.h>   /* Allows va_list to exist in the these namespaces     */
+#include <string.h>   /* Userland prototypes of the string handling funcs    */
+#include <strings.h>
+#include <unistd.h>   /* Userland prototypes of the Unix std system calls    */
+#include <fcntl.h>    /* Flag value for file handling functions              */
+#include <time.h>
+
+#include <netinet/in.h> /* Consts and structs defined by the internet system */
+
+/* from $(INSTALLDIR)/include       */
+#include <bfd.h>      /* Main header file for the BFD library                */
+
+#include <elf.h>      /* TARGET_* ELF support for the BFD library            */
+
+/* from uClinux-x.x.x/include/linux */
+#include "flat.h"     /* Binary flat header description                      */
+
+
+#ifdef TARGET_v850e
+#define TARGET_v850
+#endif
 
 #if defined(TARGET_m68k)
 #define        ARCH    "m68k/coldfire"
@@ -51,7 +64,7 @@
 #elif defined(TARGET_v850)
 #define        ARCH    "v850"
 #else
-#error "Don't know how to support you CPU archiecture??"
+#error "Don't know how to support your CPU architecture??"
 #endif
 
 /*
@@ -73,11 +86,11 @@ int pic_with_got = 0; /* do elf/got processing with PIC code */
 int load_to_ram = 0;  /* instruct loader to allocate everything into RAM */
 int compress = 0;     /* 1 = compress everything, 2 = compress data only */
 int use_resolved = 0; /* If true, get the value of symbol references from */
-                      /* the program contents, not from the relocation table. */
-                                         /* In this case, the input ELF file must be already */
-                                         /* fully resolved (using the `-q' flag with recent */
-                      /* versions of GNU ld will give you a fully resolved */
-                                         /* output file with relocation entries).  */
+                     /* the program contents, not from the relocation table. */
+                     /* In this case, the input ELF file must be already */
+                     /* fully resolved (using the `-q' flag with recent */
+                     /* versions of GNU ld will give you a fully resolved */
+                     /* output file with relocation entries).  */
 
 const char *progname, *filename;
 int lineno;
@@ -134,9 +147,9 @@ einfo (int type, const char *format, ...) {
   if (type & E_WARNING) {
     fprintf (stderr, "warning: ");
     nwarnings++;
-  }
-  else
+  } else {
     nerrors++;
+  }
 
   va_start (args, format);
   vfprintf (stderr, format, args);
@@ -161,9 +174,8 @@ get_symbols (bfd *abfd, long *num)
   if (storage_needed < 0)
     abort ();
       
-  if (storage_needed == 0) {
+  if (storage_needed == 0)
     return NULL;
-  }
 
   symbol_table = (asymbol **) malloc (storage_needed);
 
@@ -476,6 +488,10 @@ dump_symbols(symbols, number_of_symbols);
                                        sym_vma = bfd_section_vma(abs_bfd, sym_section);
                                        sym_addr += sym_vma + q->addend;
                                        break;
+                               case R_ARM_GOT32:
+                               case R_ARM_GOTPC:
+                                       /* Should be fine as is */
+                                       break;
                                case R_ARM_PLT32:
                                        if (verbose)
                                                fprintf(stderr,
@@ -587,7 +603,8 @@ dump_symbols(symbols, number_of_symbols);
 
                                tmp.l = *((unsigned long *) (sectionp + q->address));
                                hl = tmp.c[i0] | (tmp.c[i1] << 8) | (tmp.c[i2] << 16);
-                               if ((*p)->howto->type != R_ARM_PC24)
+                               if (((*p)->howto->type != R_ARM_PC24) &&
+                                   ((*p)->howto->type != R_ARM_PLT32))
                                        hl |= (tmp.c[i3] << 24);
                                else if (tmp.c[i2] & 0x80)
                                        hl |= 0xff000000; /* sign extend */
@@ -595,7 +612,8 @@ dump_symbols(symbols, number_of_symbols);
                                tmp.c[i0] = hl & 0xff;
                                tmp.c[i1] = (hl >> 8) & 0xff;
                                tmp.c[i2] = (hl >> 16) & 0xff;
-                               if ((*p)->howto->type != R_ARM_PC24)
+                               if (((*p)->howto->type != R_ARM_PC24) &&
+                                   ((*p)->howto->type != R_ARM_PLT32))
                                        tmp.c[i3] = (hl >> 24) & 0xff;
                                if ((*p)->howto->type == R_ARM_ABS32)
                                        *((unsigned long *) (sectionp + q->address)) = htonl(hl);
@@ -745,7 +763,7 @@ static void usage(void)
        "       -d              : compress data/relocs\n"
        "       -a              : use existing symbol references\n"
        "                         instead of recalculating from\n"
-       "                         relocation info\n");
+       "                         relocation info\n"
        "       -p abs-pic-file : GOT/PIC processing with files\n"
        "       -s stacksize    : set application stack size\n"
        "       -o output-file  : output file name\n\n",
@@ -913,15 +931,15 @@ int main(int argc, char *argv[])
                        s->lma, s->_cooked_size, s->output_offset,
                        s->alignment_power, s->filepos);
   }
-  if (text_len != data_vma) {
-    if (text_len > data_vma) {
+  if ((text_vma + text_len) != data_vma) {
+    if ((text_vma + text_len) > data_vma) {
       printf("ERROR: text=%x overlaps data=%x ?\n", text_len, data_vma);
       exit(1);
     }
     if (verbose)
       printf("WARNING: data=%x does not directly follow text=%x\n",
                        data_vma, text_len);
-    text_len = data_vma;
+    text_len = data_vma - text_vma;
   }
 
   if (bfd_get_section_contents(abs_bfd,
@@ -945,8 +963,8 @@ int main(int argc, char *argv[])
                        s->alignment_power, s->filepos);
   }
 
-  if ((text_len + data_len) != bss_vma) {
-    if ((text_len + data_len) > bss_vma) {
+  if ((text_vma + text_len + data_len) != bss_vma) {
+    if ((text_vma + text_len + data_len) > bss_vma) {
       printf("ERROR: text=%x + data=%x overlaps bss=%x ?\n", text_len,
                        data_len, bss_vma);
       exit(1);
@@ -954,7 +972,7 @@ int main(int argc, char *argv[])
     if (verbose)
       printf("WARNING: bss=%x does not directly follow text=%x + data=%x(%x)\n",
                bss_vma, text_len, data_len, text_len + data_len);
-      data_len = bss_vma - text_len;
+      data_len = bss_vma - data_vma;
   }
 
   reloc = (unsigned long *) output_relocs (abs_bfd, symbol_table,
@@ -978,6 +996,7 @@ int main(int argc, char *argv[])
          | (pic_with_got ? FLAT_FLAG_GOTPIC : 0)
          | (compress ? (compress == 2 ? FLAT_FLAG_GZDATA : FLAT_FLAG_GZIP) : 0)
          );
+  hdr.build_date = htonl((unsigned long)time(NULL));
   bzero(hdr.filler, sizeof(hdr.filler));
 
   for (i=0; i<reloc_len; i++) reloc[i] = htonl(reloc[i]);