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