summaryrefslogtreecommitdiff
path: root/misc/pascal/insn32/plist/plist.c
blob: d57e7deb790024e7b3f8d5808805abaf55f4c85a (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
/**********************************************************************
 * plist.c
 * POFF file lister
 *
 *   Copyright (C) 2008-2009 Gregory Nutt. All rights reserved.
 *   Author: Gregory Nutt <spudmonkey@racsa.co.cr>
 *
 * 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.
 *
 **********************************************************************/

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

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>

#define _GNU_SOURCE
#include <getopt.h>

#include "keywords.h"
#include "pdefs.h"
#include "podefs.h"
#include "pinsn32.h"
#include "pedefs.h"

#include "pofflib.h"

#include "paslib.h"
#include "pinsn.h"

/**********************************************************************
 * Definitions
 **********************************************************************/

#define MAX_STRING 80

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

static char *poffFileName      = NULL;
static int  showFileHeader     = 0;
static int  showSectionHeaders = 0;
static int  showSymbols        = 0;
static int  showRelocs         = 0;
static int  disassemble        = 0;

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

static const struct option long_options[] =
{
  {"all",              0, NULL, 'a'},
  {"file-header",      0, NULL, 'h'},
  {"section-headers",  0, NULL, 'S'},
  {"symbols",          0, NULL, 's'},
  {"relocs",           0, NULL, 'r'},
  {"disassemble",      0, NULL, 'd'},
  {"help",             0, NULL, 'H'},
  {NULL,               0, NULL, 0}
};

/**********************************************************************
 * Private Function Prototypes
 **********************************************************************/

static void showUsage       (const char *progname);
static void parseArgs       (int argc, char **argv);
static void dumpProgramData (poffHandle_t poffHandle);

/**********************************************************************
 * Global Functions
 **********************************************************************/

int main (int argc, char *argv[], char *envp[])
{
  FILE    *object;                 /* Object file pointer */
  poffHandle_t poffHandle;         /* Handle for POFF object */
  char     fileName[FNAME_SIZE+1]; /* Object file name */
  uint16_t errCode;                /* See pedefs.h */

  /* Parse the command line arguments */

  parseArgs(argc, argv);

  /* Open source POFF file -- Use .o or command line extension, if supplied */

  (void) extension (poffFileName, "o", fileName, 0);
  if (!(object = fopen (fileName, "rb")))
    {
      printf ("Error opening %s\n", fileName);
      exit (1);
    } /* end if */

  /* Read the POFF file */

  poffHandle = poffCreateHandle();
  if (poffHandle == NULL)
    {
      printf ("Could not get POFF handler\n");
      exit (1);
    }

  errCode = poffReadFile(poffHandle, object);
  if (errCode != eNOERROR)
    {
      printf ("Could not read POFF file\n");
      exit (1);
    }

  /* Dump the File Header */

  if (showFileHeader)
    {
      poffDumpFileHeader(poffHandle, stdout);
    }

  /* Dump the Section Headers */

  if (showSectionHeaders)
    {
      poffDumpSectionHeaders(poffHandle, stdout);
    }

  /* Dump the symbol table */

  if (showSymbols)
    {
      poffDumpSymbolTable(poffHandle, stdout);
    }

  /* Dump the relocation table */

  if (showRelocs)
    {
      poffDumpRelocTable(poffHandle, stdout);
    }

  /* Dump the program data section -- Main Loop */

  if (disassemble)
    {
      dumpProgramData(poffHandle);
    }

  /* Close files and release objects */

  poffDestroyHandle(poffHandle);
  (void)fclose(object);
  return 0;
} /* end main */

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

static void showUsage(const char *progname)
{
  fprintf(stderr, "Usage:\n");
  fprintf(stderr, "  %s [options] <poff-filename>\n",
          progname);
  fprintf(stderr, "options:\n");
  fprintf(stderr, "  -a --all              Equivalent to: -h -S -s -r -d\n");
  fprintf(stderr, "  -h --file-header      Display the POFF file header\n");
  fprintf(stderr, "  -S --section-headers  Display the sections' header\n");
  fprintf(stderr, "  -s --symbols          Display the symbol table\n");
  fprintf(stderr, "  -r --relocs           Display the relocations\n");
  fprintf(stderr, "  -d --disassemble      Display disassembled text\n");
  fprintf(stderr, "  -H --help             Display this information\n");
  exit(1);
}

/***********************************************************************/

static void parseArgs(int argc, char **argv)
{
  int option_index;
  int c;

  /* Check for existence of filename argument */

  if (argc < 2)
    {
      fprintf(stderr, "ERROR: POFF filename required\n");
      showUsage(argv[0]);
    } /* end if */

  /* Parse the command line options */

  do
    {
      c = getopt_long (argc, argv, "ahSsrdH",
                       long_options, &option_index);
      if (c != -1)
        {
          switch (c)
            {
            case 'a' :
              showFileHeader     = 1;
              showSectionHeaders = 1;
              showSymbols        = 1;
              showRelocs         = 1;
              disassemble        = 1;
              break;

            case 'h' :
              showFileHeader     = 1;
              break;

            case 'S' :
              showSectionHeaders = 1;
              break;

            case 's' :
              showSymbols        = 1;
              break;

            case 'r' :
              showRelocs         = 1;
              break;

            case 'd' :
              disassemble        = 1;
              break;

            case 'H' :
              showUsage(argv[0]);
              break;

            default:
              /* Shouldn't happen */

              fprintf(stderr, "ERROR: Unrecognized option\n");
              showUsage(argv[0]);
            }
        }
    }
  while (c != -1);

  /* Get the name of the p-code file(s) from the last argument(s) */

  if (optind != argc-1)
    {
      fprintf(stderr, "ERROR: POFF filename required as final argument\n");
      showUsage(argv[0]);
    }

  /* Save the POFF file name */

  poffFileName = argv[argc-1];
}

/***********************************************************************/

static void dumpProgramData(poffHandle_t poffHandle)
{
  poffLibLineNumber_t *lastln;     /* Previous line number reference */
  poffLibLineNumber_t *ln;         /* Current line number reference */
  poffLibDebugFuncInfo_t *dfi;     /* Current line debug info */
  uint32_t pc;                      /* Program counter */
  int     opSize;                  /* Size of the opcode */
  int     inch;                    /* Input char */
  OPTYPE  op;                      /* opcode */

  /* Read the line number entries from the POFF file */

  poffReadLineNumberTable(poffHandle);

  /* Read the debug function information from the POFF file */

  poffReadDebugFuncInfoTable(poffHandle);

  /* Dump the program data section -- DumpProgramData Loop */

  pc     = 0;
  lastln = NULL;

  while ((inch = poffGetProgByte(poffHandle)) != EOF)
    {
      /* Get opcode arguments (if any) */

      op.op  = inch;
      opSize = 1;

      if (inch & o32)
        {
          uint32_t arg;

          /* Handle 32-bits in big endian byte stream */

          arg  = poffGetProgByte(poffHandle) << 24;
          arg |= poffGetProgByte(poffHandle) << 16;
          arg |= poffGetProgByte(poffHandle) <<  8;
          arg |= poffGetProgByte(poffHandle);

          op.arg  = arg;
          opSize += 4;
        }

      /* Check for debug information associated with this line */

      dfi = poffFindDebugFuncInfo(pc);
      if (dfi)
        {
          int i;
          if (dfi->retsize)
            {
              printf("\nFUNCTION ENTRY:  return size=%ld nparms=%ld\n",
                     dfi->retsize, dfi->nparms);
            }
          else
            {
              printf("\nPROCEDURE ENTRY: nparms=%ld\n", dfi->nparms);
            }

          for (i = 0; i < dfi->nparms; i++)
            {
              printf("Argument %2d:     size=%ld\n", i, dfi->argsize[i]);
            }
        }

      /* Find the line number associated with this line */

      ln = poffFindLineNumber(pc);
      if ((ln) && (ln != lastln))
        {
          /* Print the line number line */

          printf("\n%s:%ld\n", ln->filename, ln->lineno);

          /* This will suppress reporting the same line number
           * repeatedly.
           */

          lastln = ln;
        }

      /* Print the address then the opcode on stdout */

      fprintf(stdout, "%08lx ", pc);
      insn_DisassemblePCode(stdout, &op);

      /* Bump the PC to the next address */

      pc += opSize;

    } /* end while */

  /* Release buffers associated with line number and debug information */

  poffReleaseLineNumberTable();
  poffReleaseDebugFuncInfoTable();

} /* end dumpProgramData */

/***********************************************************************/