CBMC
Loading...
Searching...
No Matches
ld_cmdline.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: A special command line object for the ld-like options
4
5Author: Daniel Kroening, 2013
6
7\*******************************************************************/
8
11
12#include "ld_cmdline.h"
13
14#include <util/invariant.h>
15#include <util/prefix.h>
16
17#include <iostream>
18
19// clang-format off
21{
22 "--verbosity",
23 "--native-compiler",
24 "--native-linker",
25 "--validate-goto-model",
26 nullptr
27};
28
30{
31 "-a",
32 "--architecture",
33 "-A",
34 "--format",
35 "-b",
36 "--mri-script",
37 "-c",
38 "--entry",
39 "-e",
40 "--auxiliary",
41 "-f",
42 "--filter",
43 "-F",
44 "--gpsize",
45 "-G",
46 "--soname",
47 "-h",
48 "--dynamic-linker",
49 "-I",
50 "--library",
51 "-l",
52 "--library-path",
53 "-L",
54 "--sysroot",
55 "-m",
56 "--output",
57 "-o",
58 "-O",
59 "--plugin",
60 "--plugin-opt",
61 "--flto-partition",
62 "--just-symbols",
63 "-R",
64 "--script",
65 "-T",
66 "--default-script",
67 "--dT",
68 "--undefined",
69 "-u",
70 "--trace-symbol",
71 "-y",
72 "-Y",
73 "--assert",
74 "--defsym",
75 "--file-alignment",
76 "--fini",
77 "--hash-size",
78 "--heap",
79 "--image-base",
80 "--init",
81 "--Map",
82 "--major-image-version",
83 "--major-os-version",
84 "--major-subsystem-version",
85 "--minor-image-version",
86 "--minor-os-version",
87 "--minor-subsystem-version",
88 "--oformat",
89 "--retain-symbols-file",
90 "--rpath",
91 "--rpath-link",
92 "--section-alignment",
93 "--sort-section",
94 "--spare-dynamic-tags",
95 "--stack",
96 "--subsystem",
97 "--task-link",
98 "--section-start",
99 "--Tbss",
100 "--Tdata",
101 "--Ttext",
102 "--Ttext-segment",
103 "--unresolved-symbols",
104 "--version-exports-section",
105 "--dynamic-list",
106 "--wrap",
107 "--hash-style",
108 "-z",
109 "--arch", // Apple only
110 "--ios_version_min", // Apple only
111 "--macosx_version_min", // Apple only
112 "--install_name", // Apple only
113 "--build-id",
114 nullptr
115};
116
118{
119 "--dc",
120 "-d",
121 "--dp",
122 "--export-dynamic",
123 "-E",
124 "--no-export-dynamic",
125 "--EB",
126 "--EL",
127 "-g",
128 "--print-map",
129 "-M",
130 "--nmagic",
131 "-n",
132 "--omagic",
133 "-N",
134 "--no-omagic",
135 "--flto",
136 "--Qy",
137 "--emit-relocs",
138 "-q",
139 "--relocatable",
140 "-r",
141 "-i",
142 "--strip-all",
143 "-s",
144 "--strip-debug",
145 "-S",
146 "--strip-discarded",
147 "--no-strip-discarded",
148 "--trace",
149 "-t",
150 "--unique",
151 "--Ur",
152 "--version",
153 "-v",
154 "-V",
155 "--discard-all",
156 "-x",
157 "--discard-locals",
158 "-X",
159 "--discard-none",
160 "--start-group",
161 "-(",
162 "--end-group",
163 "-)",
164 "--accept-unknown-input-arch",
165 "--no-accept-unknown-input-arch",
166 "--add-needed",
167 "--no-add-needed",
168 "--as-needed",
169 "--no-as-needed",
170 "--Bdynamic",
171 "--dy",
172 "--call_shared",
173 "--Bstatic",
174 "--dn",
175 "--non_shared",
176 "--static",
177 "--Bsymbolic",
178 "--Bsymbolic-functions",
179 "--check-sections",
180 "--no-check-sections",
181 "--copy-dt-needed-entries",
182 "--no-copy-dt-needed-entries",
183 "--cref",
184 "--demangle",
185 "--embedded-relocs",
186 "--fatal-warnings",
187 "--no-fatal-warnings",
188 "--force-exe-suffix",
189 "--gc-sections",
190 "--no-gc-sections",
191 "--print-gc-sections",
192 "--no-print-gc-sections",
193 "--help",
194 "--no-define-common",
195 "--no-demangle",
196 "--no-keep-memory",
197 "--no-undefined",
198 "--allow-shlib-undefined",
199 "--no-allow-shlib-undefined",
200 "--allow-multiple-definition",
201 "--no-undefined-version",
202 "--default-symver",
203 "--default-imported-symver",
204 "--no-warn-mismatch",
205 "--no-warn-search-mismatch",
206 "--no-whole-archive",
207 "--noinhibit-exec",
208 "--noinhibit_exec",
209 "--nostdlib",
210 "--print-output-format",
211 "--qmagic",
212 "--reduce-memory-overheads",
213 "--relax",
214 "--no-relax",
215 "--shared",
216 "--Bshareable",
217 "--pie",
218 "--pic-executable",
219 "--sort-common",
220 "--sort_common",
221 "--split-by-file",
222 "--split-by-reloc",
223 "--stats",
224 "--target-help",
225 "--traditional-format",
226 "--verbose",
227 "--dll-verbose",
228 "--version-script",
229 "--dynamic-list-data",
230 "--dynamic-list-cpp-new",
231 "--dynamic-list-cpp-typeinfo",
232 "--warn-common",
233 "--warn-constructors",
234 "--warn-multiple-gp",
235 "--warn-once",
236 "--warn-section-align",
237 "--warn-shared-textrel",
238 "--warn-alternate-em",
239 "--warn-unresolved-symbols",
240 "--error-unresolved-symbols",
241 "--whole-archive",
242 "--build-id",
243 "--eh-frame-hdr",
244 "--enable-new-dtags",
245 "--dylib", // Apple only
246 "--dylinker", // Apple only
247 "--bundle", // Apple only
248 nullptr
249};
250// clang-format on
251
255bool ld_cmdlinet::parse(int argc, const char **argv)
256{
257 PRECONDITION(argc > 0);
258 add_arg(argv[0]);
259
260 for(int i=1; i<argc; i++)
261 {
262 std::string argv_i=argv[i];
263
264 // options file?
265 if(has_prefix(argv_i, "@"))
266 {
267 // TODO
268 continue;
269 }
270
271 // file?
272 if(argv_i=="-" || !has_prefix(argv_i, "-"))
273 {
275 continue;
276 }
277
278 bool found=false;
279
280 for(const char **o=goto_ld_options_with_argument;
281 *o!=nullptr && !found;
282 ++o)
283 {
284 std::string os(*o);
285
286 // separated?
287 if(argv_i==os ||
288 (os.size()>=3 && os[0]=='-' && os[1]=='-' && "-"+argv_i==os))
289 {
290 found=true;
291 if(i!=argc-1)
292 {
293 set(os, argv[i+1]);
294 i++;
295 }
296 else
297 {
298 std::cerr << "Warning: missing argument for " << argv_i << '\n';
299 set(os, ""); // end of command line
300 }
301 }
302 else if(os.size()>2 && has_prefix(argv_i, os+"=")) // concatenated, long
303 {
304 found=true;
305 set(os, argv[i]+os.size()+1);
306 }
307 }
308
309 // goto-ld-only command line argument found
310 if(found)
311 continue;
312
313 // add to new_argv
315
316 // also store in cmdlinet
317
318 for(const char **o=ld_options_without_argument; *o!=nullptr && !found; o++)
319 {
320 std::string os(*o);
321 // ld accepts all long options also as short option
322 if(argv_i==os ||
323 (os.size()>=3 && os[0]=='-' && os[1]=='-' && "-"+argv_i==os))
324 {
325 found=true;
326 set(os); // record as long
327 }
328 }
329
330 // arguments to options can be given as follows:
331 // 1) concatenated for short options
332 // 2) concatenated with '=' for long options
333 // 3) separate
334
335 for(const char **o=ld_options_with_argument; *o!=nullptr && !found; o++)
336 {
337 std::string os(*o);
338
339 // separated?
340 if(argv_i==os ||
341 (os.size()>=3 && os[0]=='-' && os[1]=='-' && "-"+argv_i==os))
342 {
343 found=true;
344 if(i!=argc-1)
345 {
346 set(os, argv[i+1]);
347 add_arg(argv[i+1]);
348 i++;
349 }
350 else
351 {
352 std::cerr << "Warning: missing argument for " << argv_i << '\n';
353 set(os, ""); // end of command line
354 }
355 }
356 else if(os.size()==2 && has_prefix(argv_i, os)) // concatenated, short
357 {
358 found=true;
359 set(os, argv[i]+os.size());
360 }
361 else if(os.size()>2 && has_prefix(argv_i, os+"=")) // concatenated, long
362 {
363 found=true;
364 set(os, argv[i]+os.size()+1);
365 }
366 // concatenated, long as short
367 else if(os.size()>2 && has_prefix("-"+argv_i, os+"="))
368 {
369 found=true;
370 set(os, argv[i]+os.size()+1-1);
371 }
372 }
373
374 if(!found)
375 {
376 // unrecognized option
377 std::cerr << "Warning: uninterpreted ld option '" << argv_i
378 << "'\n";
379 }
380 }
381
382 return false;
383}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
void set(const std::string &opt, const char *value) override
Set option option to value.
void add_infile_arg(const std::string &arg)
void add_arg(const std::string &arg)
virtual bool parse(int, const char **)
parses the command line options into a cmdlinet
bool has_prefix(const std::string &s, const std::string &prefix)
Definition converter.cpp:13
const char * ld_options_with_argument[]
const char * ld_options_without_argument[]
const char * goto_ld_options_with_argument[]
A special command line object for the ld-like options.
#define PRECONDITION(CONDITION)
Definition invariant.h:463