NOBITS expansion, for .bss etc
[centaur.git] / src / options.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <getopt.h>
5
6 #include "options.h"
7
8
9 static void printUsage(char *progname)
10 {
11   printf("Usage: %s [OPTIONS] <elf-file>\n", progname);
12   printf("\n"
13           "Options:\n"
14           "  -h, --help                     Print this help message\n"
15           "  -o, --output                   Where to write the modified ELF file to\n"
16           "\n"
17           "ELF dump:\n"
18           "      --print-header             Print ELF header\n"
19           "      --print-segments           Print program headers\n"
20           "      --print-sections           Print sections\n"
21           "\n"
22           "Space insertion:\n"
23           "    off: File offset, not within any structure (headers or sections).\n"
24           "    sz:  A multiple of the maximum alignment of all PHDRs.\n"
25           "\n"
26           "      --expand-nobits off        Expand virtual areas (NOBITS sections and similar PHDRs).\n"
27           "      --insert-before off,sz     Insert spacing at given offset,\n"
28           "                                 mapping everything before it to lower mem addresses.\n"
29           "      --insert-after  off,sz     Insert spacing at given offset,\n"
30           "                                 mapping everything after it to higher mem addresses.\n"
31           "\n");
32 }
33
34
35
36 void parseOptions(CLIOpts *opts, int argc, char **argv)
37 {
38   char *progname = argv[0];
39   int c;
40   int option_index = 0;
41   char *endptr;
42
43   static struct option long_options[] = {
44     {"help", 0, 0, 'h'},
45     {"output", 1, 0, 'o'},
46     {"print-header", 0, 0, 10001},
47     {"print-segments", 0, 0, 10002},
48     {"print-sections", 0, 0, 10003},
49     {"insert-before", 1, 0, 10004},
50     {"insert-after", 1, 0, 10005},
51     {"expand-nobits", 1, 0, 10006},
52     {NULL, 0, NULL, 0}
53   };
54
55   while ((c = getopt_long(argc, argv, "e:ho:",
56                           long_options, &option_index)) != -1) {
57     switch (c) {
58       case 'h':
59         printUsage(progname);
60         exit(EXIT_SUCCESS);
61       case 'o':
62         opts->fnOutput = optarg;
63         break;
64       case 10001:
65         opts->printHeader = 1;
66         break;
67       case 10002:
68         opts->printSegments = 1;
69         break;
70       case 10003:
71         opts->printSections = 1;
72         break;
73       case 10004:
74         opts->insertBeforeOffs = strtoul(optarg, &endptr, 0);
75         if (endptr[0] != ',') {
76           goto USAGE;
77         }
78         opts->insertBeforeSz = strtoul(endptr + 1, &endptr, 0);
79         if (endptr[0] != '\0' || opts->insertBeforeSz == 0) {
80           goto USAGE;
81         }
82         break;
83       case 10005:
84         opts->insertAfterOffs = strtoul(optarg, &endptr, 0);
85         if (endptr[0] != ',') {
86           goto USAGE;
87         }
88         opts->insertAfterSz = strtoul(endptr + 1, &endptr, 0);
89         if (endptr[0] != '\0' || opts->insertAfterSz == 0) {
90           goto USAGE;
91         }
92         break;
93       case 10006:
94         opts->expandNobitsOffs = strtoul(optarg, &endptr, 0);
95         if (endptr[0] != '\0' || opts->expandNobitsOffs < 1) {
96           goto USAGE;
97         }
98         break;
99       case '?':
100       default:
101         goto USAGE;
102     }
103   }
104
105   while (optind < argc) {
106     if (!opts->fnInput) {
107       opts->fnInput = argv[optind];
108     }
109     optind++;
110   }
111
112   if (!opts->fnInput) {
113     fprintf(stderr, "Error: No input file specified.\n\n");
114     goto USAGE;
115   }
116
117   return;
118
119 USAGE:
120   printUsage(progname);
121   exit(1);
122 }