]> git.sur5r.net Git - cc65/blob - src/od65/main.c
Allow to dump scope information.
[cc65] / src / od65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*             Main program of the od65 object file dump utility             */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2011, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <errno.h>
40
41 /* common */
42 #include "cmdline.h"
43 #include "objdefs.h"
44 #include "version.h"
45
46 /* od65 */
47 #include "dump.h"
48 #include "error.h"
49 #include "fileio.h"
50 #include "global.h"
51
52
53
54 /*****************************************************************************/
55 /*                                   Data                                    */
56 /*****************************************************************************/
57
58
59
60 static unsigned FilesProcessed = 0;
61
62
63
64 /*****************************************************************************/
65 /*                                   Code                                    */
66 /*****************************************************************************/
67
68
69
70 static void Usage (void)
71 /* Print usage information and exit */
72 {
73     fprintf (stderr,
74              "Usage: %s [options] file [options] [file]\n"
75              "Short options:\n"
76              "  -h\t\t\tHelp (this text)\n"
77              "  -H\t\t\tDump the object file header\n"
78              "  -S\t\t\tDump segments sizes\n"
79              "  -V\t\t\tPrint the version number and exit\n"
80              "\n"
81              "Long options:\n"
82              "  --dump-all\t\tDump all object file information\n"
83              "  --dump-dbgsyms\tDump debug symbols\n"
84              "  --dump-exports\tDump exported symbols\n"
85              "  --dump-files\t\tDump the source files\n"
86              "  --dump-header\t\tDump the object file header\n"
87              "  --dump-imports\tDump imported symbols\n"
88              "  --dump-lineinfo\tDump line information\n"
89              "  --dump-options\tDump object file options\n"
90              "  --dump-segments\tDump the segments in the file\n"
91              "  --dump-segsize\tDump segments sizes\n"
92              "  --help\t\tHelp (this text)\n"
93              "  --version\t\tPrint the version number and exit\n",
94              ProgName);
95 }
96
97
98
99 static void OptDumpAll (const char* Opt attribute ((unused)),
100                         const char* Arg attribute ((unused)))
101 /* Dump all object file information */
102 {
103     What |= D_ALL;
104 }
105
106
107
108 static void OptDumpDbgSyms (const char* Opt attribute ((unused)),
109                             const char* Arg attribute ((unused)))
110 /* Dump debug symbols contained in the object file */
111 {
112     What |= D_DBGSYMS;
113 }
114
115
116
117 static void OptDumpExports (const char* Opt attribute ((unused)),
118                             const char* Arg attribute ((unused)))
119 /* Dump the exported symbols */
120 {
121     What |= D_EXPORTS;
122 }
123
124
125
126 static void OptDumpFiles (const char* Opt attribute ((unused)),
127                           const char* Arg attribute ((unused)))
128 /* Dump the source files */
129 {
130     What |= D_FILES;
131 }
132
133
134
135 static void OptDumpHeader (const char* Opt attribute ((unused)),
136                            const char* Arg attribute ((unused)))
137 /* Dump the object file header */
138 {
139     What |= D_HEADER;
140 }
141
142
143
144 static void OptDumpImports (const char* Opt attribute ((unused)),
145                             const char* Arg attribute ((unused)))
146 /* Dump the imported symbols */
147 {
148     What |= D_IMPORTS;
149 }
150
151
152
153 static void OptDumpLineInfo (const char* Opt attribute ((unused)),
154                              const char* Arg attribute ((unused)))
155 /* Dump the line infos */
156 {
157     What |= D_LINEINFO;
158 }
159
160
161
162 static void OptDumpOptions (const char* Opt attribute ((unused)),
163                             const char* Arg attribute ((unused)))
164 /* Dump the object file options */
165 {
166     What |= D_OPTIONS;
167 }
168
169
170
171 static void OptDumpScopes (const char* Opt attribute ((unused)),
172                            const char* Arg attribute ((unused)))
173 /* Dump the scopes in the object file */
174 {
175     What |= D_SCOPES;
176 }
177
178
179
180 static void OptDumpSegments (const char* Opt attribute ((unused)),
181                              const char* Arg attribute ((unused)))
182 /* Dump the segments in the object file */
183 {
184     What |= D_SEGMENTS;
185 }
186
187
188
189 static void OptDumpSegSize (const char* Opt attribute ((unused)),
190                             const char* Arg attribute ((unused)))
191 /* Dump the segments in the object file */
192 {
193     What |= D_SEGSIZE;
194 }
195
196
197
198 static void OptHelp (const char* Opt attribute ((unused)),
199                      const char* Arg attribute ((unused)))
200 /* Print usage information and exit */
201 {
202     Usage ();
203     exit (EXIT_SUCCESS);
204 }
205
206
207
208 static void OptVersion (const char* Opt attribute ((unused)),
209                         const char* Arg attribute ((unused)))
210 /* Print the assembler version */
211 {
212     fprintf (stderr,
213              "%s V%s - (C) Copyright 2000-2011, Ullrich von Bassewitz\n",
214              ProgName, GetVersionAsString ());
215 }
216
217
218
219 static void DumpFile (const char* Name)
220 /* Dump information from the named file */
221 {
222     unsigned long Magic;
223
224     /* Try to open the file */
225     FILE* F = fopen (Name, "rb");
226     if (F == 0) {
227         Error ("Cannot open `%s': %s", Name, strerror (errno));
228     }
229
230     /* Read the magic word */
231     Magic = Read32 (F);
232
233     /* Do we know this type of file? */
234     if (Magic != OBJ_MAGIC) {
235
236         /* Unknown format */
237         printf ("%s: (no xo65 object file)\n", Name);
238
239     } else if (What == 0) {
240
241         /* Special handling if no info was requested */
242         printf ("%s: (no information requested)\n", Name);
243
244     } else {
245
246         /* Print the filename */
247         printf ("%s:\n", Name);
248
249         /* Check what to dump */
250         if (What & D_HEADER) {
251             DumpObjHeader (F, 0);
252         }
253         if (What & D_OPTIONS) {
254             DumpObjOptions (F, 0);
255         }
256         if (What & D_FILES) {
257             DumpObjFiles (F, 0);
258         }
259         if (What & D_SEGMENTS) {
260             DumpObjSegments (F, 0);
261         }
262         if (What & D_IMPORTS) {
263             DumpObjImports (F, 0);
264         }
265         if (What & D_EXPORTS) {
266             DumpObjExports (F, 0);
267         }
268         if (What & D_DBGSYMS) {
269             DumpObjDbgSyms (F, 0);
270         }
271         if (What & D_LINEINFO) {
272             DumpObjLineInfo (F, 0);
273         }
274         if (What & D_SCOPES) {
275             DumpObjScopes (F, 0);
276         }
277         if (What & D_SEGSIZE) {
278             DumpObjSegSize (F, 0);
279         }
280     }
281
282     /* Close the file */
283     fclose (F);
284 }
285
286
287
288 int main (int argc, char* argv [])
289 /* Assembler main program */
290 {
291     /* Program long options */
292     static const LongOpt OptTab[] = {
293         { "--dump-all",         0,      OptDumpAll              },
294         { "--dump-dbgsyms",     0,      OptDumpDbgSyms          },
295         { "--dump-exports",     0,      OptDumpExports          },
296         { "--dump-files",       0,      OptDumpFiles            },
297         { "--dump-header",      0,      OptDumpHeader           },
298         { "--dump-imports",     0,      OptDumpImports          },
299         { "--dump-lineinfo",    0,      OptDumpLineInfo         },
300         { "--dump-options",     0,      OptDumpOptions          },
301         { "--dump-scopes",      0,      OptDumpScopes           },
302         { "--dump-segments",    0,      OptDumpSegments         },
303         { "--dump-segsize",     0,      OptDumpSegSize          },
304         { "--help",             0,      OptHelp                 },
305         { "--version",          0,      OptVersion              },
306     };
307
308     unsigned I;
309
310     /* Initialize the cmdline module */
311     InitCmdLine (&argc, &argv, "od65");
312
313     /* Check the parameters */
314     I = 1;
315     while (I < ArgCount) {
316
317         /* Get the argument */
318         const char* Arg = ArgVec[I];
319
320         /* Check for an option */
321         if (Arg [0] == '-') {
322             switch (Arg [1]) {
323
324                 case '-':
325                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
326                     break;
327
328                 case 'h':
329                     OptHelp (Arg, 0);
330                     break;
331
332                 case 'H':
333                     OptDumpHeader (Arg, 0);
334                     break;
335
336                 case 'S':
337                     OptDumpSegSize (Arg, 0);
338                     break;
339
340                 case 'V':
341                     OptVersion (Arg, 0);
342                     break;
343
344                 default:
345                     UnknownOption (Arg);
346                     break;
347
348             }
349         } else {
350             /* Filename. Dump it. */
351             DumpFile (Arg);
352             ++FilesProcessed;
353         }
354
355         /* Next argument */
356         ++I;
357     }
358
359     /* Print a message if we did not process any files */
360     if (FilesProcessed == 0) {
361         fprintf (stderr, "%s: No input files\n", ProgName);
362     }
363
364     /* Success */
365     return EXIT_SUCCESS;
366 }
367
368
369