1 using System;
2 using System.Collections;
3 using System.IO;
4 using cmsx.object;
5
6 public nothrow const char* Version()
7 {
8 return "4.0.0";
9 }
10
11 void PrintHelp()
12 {
13 Console.Out() << "Cmajor System X Dumper version " << Version() << endl();
14 Console.Out() << "Usage: cmsxdump [options] { FILE.o | FILE.a | FILE }" << endl();
15 Console.Out() << "Dump contents of an object file FILE.o, a library file FILE.a or an executable file FILE to standard output." << endl();
16 Console.Out() << "Options:" << endl();
17 Console.Out() << "--help (-h)" << endl();
18 Console.Out() << " print help" << endl();
19 Console.Out() << "--verbose (-v)" << endl();
20 Console.Out() << " be verbose" << endl();
21 }
22
23 int main(int argc, const char** argv)
24 {
25 try
26 {
27 bool verbose = false;
28 List<string> fileNames;
29 for (int i = 1; i < argc; ++i;)
30 {
31 string arg = argv[i];
32 if (arg.StartsWith("--"))
33 {
34 if (arg == "--help")
35 {
36 PrintHelp();
37 return 1;
38 }
39 else if (arg == "--verbose")
40 {
41 verbose = true;
42 }
43 else
44 {
45 throw Exception("unknown option '" + arg + "'");
46 }
47 }
48 else if (arg.StartsWith("-"))
49 {
50 string options = arg.Substring(1);
51 if (options.IsEmpty())
52 {
53 throw Exception("unknown option '" + arg + "'");
54 }
55 else
56 {
57 for (char o : options)
58 {
59 if (o == 'h')
60 {
61 PrintHelp();
62 return 1;
63 }
64 else if (o == 'v')
65 {
66 verbose = true;
67 }
68 else
69 {
70 throw Exception("unknown option '-" + string(o) + "'");
71 }
72 }
73 }
74 }
75 else
76 {
77 fileNames.Add(GetFullPath(arg));
78 }
79 }
80 if (verbose)
81 {
82 Console.Out() << "Cmajor System X Dumper version " << Version() << endl();
83 }
84 if (fileNames.IsEmpty())
85 {
86 PrintHelp();
87 throw Exception("no files given");
88 }
89 for (const string& fileName : fileNames)
90 {
91 Console.Out() << fileName << ":" << endl();
92 UniquePtr<BinaryFile> binaryFile = ReadBinaryFile(fileName);
93 binaryFile->AddSymbolsToAddressMap();
94 binaryFile->Dump(Console.Out());
95 }
96 }
97 catch (const Exception& ex)
98 {
99 Console.Error() << ex.Message() << endl();
100 return 1;
101 }
102 return 0;
103 }