summaryrefslogtreecommitdiff
path: root/misc/buildroot/toolchain/nxflat/readnxflat.c
blob: e19e2d6991160f0b15b6a2049fb01c0aed44a156 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
/***********************************************************************
 * toolchain/nxflat/readnxflat.c
 *
 *   Copyright (C) 2009 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * Modified from readxflat (see http://xflat.org):
 *
 *   Copyright (c) 2002, 2006, Cadenux, LLC.  All rights reserved.
 *   Copyright (c) 2002, 2006, Gregory Nutt.  All rights reserved.
 *   Author: Gregory Nutt <gnutt@nuttx.org>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name NuttX nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ***********************************************************************/

/***********************************************************************
 * Compilation Flags
 ***********************************************************************/

#define SWAP_BYTES 1

/***********************************************************************
 * Included Files
 ***********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <netinet/in.h>         /* ntohl and friends */
#include "nxflat.h"

/***********************************************************************
 * Compilation Switches
 ***********************************************************************/

/* #define RELOCS_IN_NETWORK_ORDER */

/***********************************************************************
 * Definitions
 ***********************************************************************/
 
#define NXFLAT_HDR_SIZE   sizeof(struct nxflat_hdr_s)

/***********************************************************************
 * Private Data
 ***********************************************************************/

static const char *program_name;
static const char *nxflat_filename;

static int dump_header = 0;
static int dump_relocs = 0;
static int dump_imports = 0;
static int dump_text = 0;
static int dump_data = 0;
static int verbose = 0;

static int num_errors = 0;

#ifdef ARCH_BIG_ENDIAN
static int big_endian = 1;      /* Assume big-endian */
#else
static int big_endian = 0;      /* Assume little-endian */
#endif

/***********************************************************************
 * Private Constant Data
 ***********************************************************************/

static const char unknown[] = "UNKNOWN";

static const char hdr_reloc_rel32i[]  = "RELOC_REL32I";
static const char hdr_reloc_rel32d[]  = "RELOC_REL32D";
#ifdef NXFLAT_RELOC_TYPE_REL32ID
static const char hdr_reloc_rel32id[] = "RELOC_REL32ID";
#endif

static const char *reloc_type_string[] = {
  hdr_reloc_rel32i,
  hdr_reloc_rel32d,
#ifdef NXFLAT_RELOC_TYPE_REL32ID
  hdr_reloc_rel32id,
#else
  unknown,
#endif
  unknown
};

/***********************************************************************
 * Public Function Prototypes
 ***********************************************************************/

extern void __attribute__ ((weak)) print_insn_arm(u_int32_t pc,
                                                  FILE * stream,
                                                  u_int32_t given);

/***********************************************************************
 * Private Functions
 ***********************************************************************/

/***********************************************************************
 * swap32
 ***********************************************************************/

static inline u_int32_t nxflat_swap32(u_int32_t little)
{
  u_int32_t big =
    ((little >> 24) & 0xff) |
    (((little >> 16) & 0xff) << 8) |
    (((little >> 8) & 0xff) << 16) | ((little & 0xff) << 24);
  return big;
}

/***********************************************************************
 * get_nxflat32
 ***********************************************************************/

static inline u_int32_t get_nxflat32(u_int32_t * addr32)
{
  return ntohl(*addr32);
}

/***********************************************************************
 * get_nxflat16
 ***********************************************************************/

static inline u_int16_t get_nxflat16(u_int16_t * addr16)
{
  return ntohs(*addr16);
}

/***********************************************************************
 * dump_hex_data
 ***********************************************************************/

static void dump_hex_data(FILE * in_stream, struct nxflat_hdr_s *header)
{
  u_int32_t data_start = get_nxflat32(&header->h_datastart);
  u_int32_t data_end = get_nxflat32(&header->h_dataend);
  int32_t words_left = (data_end - data_start) / sizeof(u_int32_t);
  u_int32_t addr;
  u_int32_t buffer[64];

  printf("\nXFLAT DATA SEGMENT:\n\n");

  /* Seek to the beginning of data in the file */

  if (fseek(in_stream, data_start, SEEK_SET) != 0)
    {
      fprintf(stderr,
              "ERROR: Failed to seek to data in file: offset: %08x\n",
              data_start);
      return;
    }

  /* Now dump all of the data reading 64 words at a time */

  addr = 0;
  while (words_left > 0)
    {
      size_t nread = fread(buffer, sizeof(u_int32_t), 64, in_stream);
      if (nread >= 0)
        {
          union
            {
              u_int32_t l[4];
              unsigned char b[16];
            } row;
          int32_t i, j, k;

          for (i = 0; i < nread; i += 4)
            {
              for (j = 0; j < 4; j++)
                {
                  row.l[j] = buffer[i + j];
                }

              printf("%08x: ", addr);

              for (j = 0; j < 4; j++)
                {
                  printf("%08x ", row.l[j]);
                }

              printf("  ");

              for (j = 0; j < 4 * sizeof(u_int32_t); j += sizeof(u_int32_t))
                {
                  for (k = 0; k < sizeof(u_int32_t); k++)
                    {
                      if (isprint(row.b[j + k]))
                        putchar(row.b[j + k]);
                      else
                        putchar('.');
                    }
                }

              putchar('\n');
              addr += 4 * sizeof(u_int32_t);
            }
          words_left -= nread;
        }
      else
        break;
    }
  putchar('\n');
}

/***********************************************************************
 * disassemble_text
 ***********************************************************************/

static void disassemble_text(FILE * in_stream, struct nxflat_hdr_s *header)
{
  if (print_insn_arm)
    {
      u_int32_t text_start = NXFLAT_HDR_SIZE;
      u_int32_t text_end = get_nxflat32(&header->h_datastart);
      int32_t insns_left = (text_end - text_start) / sizeof(u_int32_t);
      u_int32_t addr;
      u_int32_t buffer[64];

      printf("\nXFLAT TEXT:\n\n");

      /* Seek to the beginning of text in the file */

      if (fseek(in_stream, text_start, SEEK_SET) != 0)
        {
          fprintf(stderr,
                  "ERROR: Failed to seek to text in file: offset: %08x\n",
                  text_start);
          return;
        }

      /* Now dump all of the data reading 64 insns at a time */

      addr = text_start;
      while (insns_left > 0)
        {
          size_t nread = fread(buffer, sizeof(u_int32_t), 64, in_stream);
          if (nread > 0)
            {
              int i;
              for (i = 0; i < nread; i++)
                {
                  u_int32_t insn = buffer[i];
                  if (big_endian)
                    {
                      insn = nxflat_swap32(insn);
                    }

                  printf("%08x %08x\t", addr, insn);
                  print_insn_arm(addr, stdout, insn);
                  putchar('\n');
                  addr += sizeof(u_int32_t);
                }
              insns_left -= nread;
            }
          else
            break;
          putchar('\n');
        }
    }
}

/***********************************************************************
 * dump_imported_symbols
 ***********************************************************************/

static void dump_imported_symbols(FILE *in_stream, struct nxflat_hdr_s *header)
{
  struct nxflat_import_s import;
  u_int32_t import_offset;
  u_int32_t data_start;
  u_int32_t struct_offset;
  u_int32_t name_offset;
  char imported_symbol_name[NXFLAT_MAX_STRING_SIZE];
  int status;
  int i;

  printf("\nIMPORTED SYMBOLS:\n");
  printf("      OFFSET   ADDRESS  SYMBOL NAME\n\n");

  import_offset = get_nxflat32(&header->h_importsymbols);
  data_start    = get_nxflat32(&header->h_datastart);

  for (i = 0; i < get_nxflat16(&header->h_importcount); i++)
    {
      /* Seek to the next imported symbol */

      struct_offset = i * sizeof(struct nxflat_import_s) + import_offset;

      if (fseek(in_stream, struct_offset, SEEK_SET) != 0)
        {
          fprintf(stderr, "ERROR: fseek to imported symbol %d struct failed\n",
                  i);
          fprintf(stderr, "       struct_offset: %d: %s\n",
                  struct_offset, strerror(errno));
          exit(1);
        }

      /* Read the next import entry. */

      status = fread((void *)&import,
                     sizeof(struct nxflat_import_s), 1, in_stream);
      if (status != 1)
        {
          if (ferror(in_stream))
            {
              fprintf(stderr, "ERROR: Read imported symbol %d struct failed: %s\n",
                      i + 1, strerror(errno));
            }
          else
            {
              fprintf(stderr, "ERROR: Read imported symbol %d struct: End-of-file after %d\n",
                      i + 1, struct_offset);
            }

          exit(1);
        }

      if (big_endian)
        {
          import.i_funcname    = nxflat_swap32(import.i_funcname);
          import.i_funcaddress = nxflat_swap32(import.i_funcaddress);
        }

      if (verbose)
        {
          /* Print the raw info */

          printf("[Import: %4d Offset: %08x Name: %08x Address: %08x]\n",
                 i + 1, struct_offset, import.i_funcname, import.i_funcaddress);
        }

      /* Seek to the function name in the file */

      name_offset = import.i_funcname + NXFLAT_HDR_SIZE;

      if (fseek(in_stream, name_offset, SEEK_SET) != 0)
        {
          fprintf(stderr, "ERROR: fseek to imported symbol %d name failed\n",
                  i);
          fprintf(stderr, "       name_offset: %d %s\n",
                  name_offset, strerror(errno));
          exit(1);
        }

      /* Then, read the imported symbol name (assuming it is less than
       * NXFLAT_MAX_STRING_SIZE in length). */

      status = fread((void *)imported_symbol_name, NXFLAT_MAX_STRING_SIZE,
                     1, in_stream);

      if (status != 1)
        {
          if (ferror(in_stream))
            {
              fprintf(stderr, "ERROR: Read imported symbol %d name failed: %s\n",
                      i + 1, strerror(errno));
            }
          else
            {
              fprintf(stderr, "ERROR: Read imported symbol %d name: End-of-file after offset=%d\n",
                      i + 1, name_offset);
            }
          exit(1);
        }

      imported_symbol_name[NXFLAT_MAX_STRING_SIZE - 1] = '\0';

      /* And print it */

      printf("%5d %08x ", i + 1, (int)struct_offset - data_start);

      if (import.i_funcaddress)
        {
          printf("%08x ", import.i_funcaddress);
        }
      else
        {
          printf("UNKNOWN  ");
        }

      printf("%s\n", imported_symbol_name);
    }
}

/***********************************************************************
 * dump_relocation_entries
 ***********************************************************************/

static void dump_relocation_entries(FILE * in_stream, struct nxflat_hdr_s *header)
{
  struct nxflat_reloc_s reloc;
  int status;
  int i;

  /* Seek to the beginning of the relocation records. */

  if (0 != fseek(in_stream, get_nxflat32(&header->h_relocstart), SEEK_SET))
    {
      fprintf(stderr, "ERROR: fseek to reloc records failed: %s\n",
              strerror(errno));
      exit(1);
    }

  printf("\nRELOCATION ENTRIES:\n");
  printf("      OFFSET   RELOC TYPE\n\n");

  for (i = 0; i < get_nxflat16(&header->h_reloccount); i++)
    {
      /* Read the next reloction entry. */

      status = fread((void *)&reloc, sizeof(struct nxflat_reloc_s), 1, in_stream);
      if (status != 1)
        {
          if (ferror(in_stream))
            {
              fprintf(stderr, "ERROR: Read reloc record %d: %s\n",
                      i + 1, strerror(errno));
            }
          else
            {
              fprintf(stderr, "ERROR: Read reloc record %d: End-of-file\n",
                      i + 1);
            }
          exit(1);
        }
 
#ifdef RELOCS_IN_NETWORK_ORDER
      {
        u_int32_t *ptmp;
        ptmp = (u_int32_t *) & reloc;
        *ptmp = get_nxflat32(ptmp);
      }
#endif

      if (NXFLAT_RELOC_TYPE(reloc.r_info) >= NXFLAT_RELOC_TYPE_NUM)
        {
          printf("%5d %08x UNKNOWN(%d)\n", i + 1,
                 NXFLAT_RELOC_OFFSET(reloc.r_info), NXFLAT_RELOC_TYPE(reloc.r_info));
          fprintf(stderr, "Error eloc type out of range(%d)\n",
                  NXFLAT_RELOC_TYPE(reloc.r_info));
          num_errors++;
        }
      else
        {
          printf("%5d %08x %-13s\n",
                 i + 1, NXFLAT_RELOC_OFFSET(reloc.r_info),
                 reloc_type_string[NXFLAT_RELOC_TYPE(reloc.r_info)]);
        }
    }
}

/***********************************************************************
 * dump_hdr
 ***********************************************************************/

static void dump_hdr(struct nxflat_hdr_s *header)
{
  /* Print the contents of the FLT header */

  printf("\nXFLAT HEADER:\n");
  printf("\nMagic           %c%c%c%c\n",
         header->h_magic[0], header->h_magic[1],
         header->h_magic[2], header->h_magic[3]);

  printf("\nMEMORY MAP:\n");
  printf("  Text start    %08lx\n", (long)NXFLAT_HDR_SIZE);
  printf("  Entry point   %08x\n", get_nxflat32(&header->h_entry));
  printf("  Data start    %08x\n", get_nxflat32(&header->h_datastart));
  printf("  Data end      %08x\n", get_nxflat32(&header->h_dataend) - 1);
  printf("  Bss start     %08x\n", get_nxflat32(&header->h_dataend));
  printf("  Bss end       %08x\n", get_nxflat32(&header->h_bssend) - 1);
  printf("TOTAL SIZE      %08x\n\n", get_nxflat32(&header->h_bssend));
  printf("Stack size      %08x\n", get_nxflat32(&header->h_stacksize));
  printf("\nRELOCATIONS:\n");
  printf("  Reloc start   %08x\n", get_nxflat32(&header->h_relocstart));
  printf("  reloc count   %d\n", get_nxflat16(&header->h_reloccount));
  printf("\nIMPORTED SYMBOLS:\n");
  printf("  Import start  %08x\n", get_nxflat32(&header->h_importsymbols));
  printf("  Import count  %d\n", get_nxflat16(&header->h_importcount));
}

/***********************************************************************
 * show_usage
 ***********************************************************************/

static void show_usage(void)
{
  fprintf(stderr, "Usage: %s [options] <flat-filename>\n\n", program_name);
#if 1
  fprintf(stderr, "Where options are one or more of the following:\n\n");
#else
  fprintf(stderr, "Where options are one or more of the following.  Note\n");
  fprintf(stderr, "that a space is always required between the option and\n");
  fprintf(stderr, "any following arguments\n\n");
#endif
  fprintf(stderr, "  -h Dump the XFLAT file header     [not dumped]\n");
  fprintf(stderr, "  -r Dump relocation entries        [not dumped]\n");
  fprintf(stderr, "  -i Dump the imported symbol table [not dumped]\n");
  fprintf(stderr, "  -x Dump xFLT loader pathname      [not dumped]\n");
  fprintf(stderr, "  -c Disassemble the text section   [not dumped]\n");
  fprintf(stderr, "  -d Dump data section (hex)        [not dumped]\n");
  fprintf(stderr, "  -a Dump all of the above          [not dumped]\n");
#ifdef ARCH_BIG_ENDIAN
  fprintf(stderr, "  -b Assume little-endian byteorder [big endian]\n");
#else
  fprintf(stderr, "  -b Assume big-endian byteorder    [little endian]\n");
#endif
  fprintf(stderr, "  -v Output verbose debug info      [no output]\n");
  fprintf(stderr, "\n");
  exit(1);
}

/***********************************************************************
 * parse_args
 ***********************************************************************/

static void parse_args(int argc, char **argv)
{
  int opt;

  /* Save our name (for show_usage) */

  program_name = argv[0];

  /* At least three things must appear on the program line: the program name,
   * the BFD filname, and at least one option. */

  if (argc < 3)
    {
      fprintf(stderr, "ERROR: Missing required arguments\n\n");
      show_usage();
    }

  /* Get miscellaneous options from the command line. */

  while ((opt = getopt(argc, argv, "hrieLlxcbdav")) != -1)
    {
      switch (opt)
        {

        case 'h':              /* Dump the flat file header */
          dump_header++;
          break;

        case 'r':              /* Dump the flat file header */
          dump_relocs++;
          break;

        case 'i':              /* Dump the imported symbol table */
          dump_imports++;
          break;

        case 'c':              /* Disassembly text */
          if (print_insn_arm)
            {
              dump_text++;
            }
          else
            {
              printf("-c ignored: No disassembler available\n");
            }
          break;

        case 'b':              /* other-endian */
#ifdef ARCH_BIG_ENDIAN
          big_endian = 0;
#else
          big_endian++;
#endif
          break;

        case 'd':              /* Dump data */
          dump_data++;
          break;

        case 'a':              /* Dump everying */
          dump_header++;
          dump_relocs++;
          dump_imports++;
          dump_text++;
          dump_data++;
          break;

        case 'v':              /* Output verbose debug information */
          verbose++;
          break;

        default:
          fprintf(stderr, "%s Unknown option\n\n", argv[0]);
          show_usage();
          break;
        }
    }

  /* Get the name of the input BFD file. */

  nxflat_filename = argv[argc - 1];
}

/***********************************************************************
 * Public Functions
 ***********************************************************************/

/***********************************************************************
 * main
 ***********************************************************************/

int main(int argc, char **argv, char **envp)
{
  FILE *in_stream;
  struct nxflat_hdr_s header;
  int status;

  /* Get the input parameters */

  parse_args(argc, argv);

  /* Open the FLT file */

  in_stream = fopen(nxflat_filename, "rb");
  if (NULL == in_stream)
    {
      fprintf(stderr, "Cannot open file %s for reading\n", nxflat_filename);
      exit(1);
    }

  /* Read the FLT header */

  status = fread((void *)&header, sizeof(struct nxflat_hdr_s), 1, in_stream);
  if (status != 1)
    {
      if (ferror(in_stream))
        {
          fprintf(stderr, "ERROR: Read flat header: %s\n", strerror(errno));
        }
      else
        {
          fprintf(stderr, "ERROR: Read flat header: End-of-file\n");
        }
      exit(1);
    }

  printf("Dumping Flat Binary File: %s\n", nxflat_filename);

  /* Dump the contents of the FLT header */

  if (dump_header)
    {
      dump_hdr(&header);
    }

  /* Dump the relocation entries */

  if (dump_relocs)
    {
      dump_relocation_entries(in_stream, &header);
    }

  /* Dump all imported symbols */

  if (dump_imports)
    {
      dump_imported_symbols(in_stream, &header);
    }

   if (dump_text)
    {
      disassemble_text(in_stream, &header);
    }

  if (dump_data)
    {
      dump_hex_data(in_stream, &header);
    }

  fclose(in_stream);

  if (num_errors > 0)
    {
      fprintf(stderr, "Finished with %d errors\n", num_errors);
    }

  return 0;
}