Move compile_instance to compile.c
[deliverable/binutils-gdb.git] / gdb / compile / compile.c
CommitLineData
bb2ec1b3
TT
1/* General Compile and inject code
2
e2882c85 3 Copyright (C) 2014-2018 Free Software Foundation, Inc.
bb2ec1b3
TT
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
cb814510 21#include "top.h"
bb2ec1b3
TT
22#include "ui-out.h"
23#include "command.h"
24#include "cli/cli-script.h"
25#include "cli/cli-utils.h"
26#include "completer.h"
27#include "gdbcmd.h"
28#include "compile.h"
29#include "compile-internal.h"
30#include "compile-object-load.h"
31#include "compile-object-run.h"
32#include "language.h"
33#include "frame.h"
34#include "source.h"
35#include "block.h"
36#include "arch-utils.h"
37#include "filestuff.h"
38#include "target.h"
39#include "osabi.h"
3ce348af 40#include "gdb_wait.h"
36de76f9 41#include "valprint.h"
b80cf838
TT
42#include "common/gdb_optional.h"
43#include "common/gdb_unlinker.h"
b4987c95 44#include "common/pathstuff.h"
bb2ec1b3
TT
45
46\f
47
48/* Initial filename for temporary files. */
49
50#define TMP_PREFIX "/tmp/gdbobj-"
51
52/* Hold "compile" commands. */
53
54static struct cmd_list_element *compile_command_list;
55
56/* Debug flag for "compile" commands. */
57
58int compile_debug;
59
946d3d10
KS
60/* Object of this type are stored in the compiler's symbol_err_map. */
61
62struct symbol_error
63{
64 /* The symbol. */
65
66 const struct symbol *sym;
67
68 /* The error message to emit. This is malloc'd and owned by the
69 hash table. */
70
71 char *message;
72};
73
74/* Hash a type_map_instance. */
75
76static hashval_t
77hash_type_map_instance (const void *p)
78{
79 const struct type_map_instance *inst = (const struct type_map_instance *) p;
80
81 return htab_hash_pointer (inst->type);
82}
83
84/* Check two type_map_instance objects for equality. */
85
86static int
87eq_type_map_instance (const void *a, const void *b)
88{
89 const struct type_map_instance *insta = (const struct type_map_instance *) a;
90 const struct type_map_instance *instb = (const struct type_map_instance *) b;
91
92 return insta->type == instb->type;
93}
94
95/* Hash function for struct symbol_error. */
96
97static hashval_t
98hash_symbol_error (const void *a)
99{
100 const struct symbol_error *se = (const struct symbol_error *) a;
101
102 return htab_hash_pointer (se->sym);
103}
104
105/* Equality function for struct symbol_error. */
106
107static int
108eq_symbol_error (const void *a, const void *b)
109{
110 const struct symbol_error *sea = (const struct symbol_error *) a;
111 const struct symbol_error *seb = (const struct symbol_error *) b;
112
113 return sea->sym == seb->sym;
114}
115
116/* Deletion function for struct symbol_error. */
117
118static void
119del_symbol_error (void *a)
120{
121 struct symbol_error *se = (struct symbol_error *) a;
122
123 xfree (se->message);
124 xfree (se);
125}
126
127/* Constructor for compile_instance. */
128
129compile_instance::compile_instance (struct gcc_base_context *gcc_fe,
130 const char *options)
131 : m_gcc_fe (gcc_fe), m_gcc_target_options (options),
132 m_symbol_err_map (NULL)
133{
134 m_type_map = htab_create_alloc (10, hash_type_map_instance,
135 eq_type_map_instance,
136 xfree, xcalloc, xfree);
137}
138
139/* See compile-internal.h. */
140
141bool
142compile_instance::get_cached_type (struct type *type, gcc_type &ret) const
143{
144 struct type_map_instance inst, *found;
145
146 inst.type = type;
147 found = (struct type_map_instance *) htab_find (m_type_map, &inst);
148 if (found != NULL)
149 {
150 ret = found->gcc_type_handle;
151 return true;
152 }
153
154 return false;
155}
156
157/* See compile-internal.h. */
158
159void
160compile_instance::insert_type (struct type *type, gcc_type gcc_type)
161{
162 struct type_map_instance inst, *add;
163 void **slot;
164
165 inst.type = type;
166 inst.gcc_type_handle = gcc_type;
167 slot = htab_find_slot (m_type_map, &inst, INSERT);
168
169 add = (struct type_map_instance *) *slot;
170 /* The type might have already been inserted in order to handle
171 recursive types. */
172 if (add != NULL && add->gcc_type_handle != gcc_type)
173 error (_("Unexpected type id from GCC, check you use recent enough GCC."));
174
175 if (add == NULL)
176 {
177 add = XNEW (struct type_map_instance);
178 *add = inst;
179 *slot = add;
180 }
181}
182
183/* See compile-internal.h. */
184
185void
186compile_instance::insert_symbol_error (const struct symbol *sym,
187 const char *text)
188{
189 struct symbol_error e;
190 void **slot;
191
192 if (m_symbol_err_map == NULL)
193 {
194 m_symbol_err_map = htab_create_alloc (10,
195 hash_symbol_error,
196 eq_symbol_error,
197 del_symbol_error,
198 xcalloc,
199 xfree);
200 }
201
202 e.sym = sym;
203 slot = htab_find_slot (m_symbol_err_map, &e, INSERT);
204 if (*slot == NULL)
205 {
206 struct symbol_error *e = XNEW (struct symbol_error);
207
208 e->sym = sym;
209 e->message = xstrdup (text);
210 *slot = e;
211 }
212}
213
214/* See compile-internal.h. */
215
216void
217compile_instance::error_symbol_once (const struct symbol *sym)
218{
219 struct symbol_error search;
220 struct symbol_error *err;
221
222 if (m_symbol_err_map == NULL)
223 return;
224
225 search.sym = sym;
226 err = (struct symbol_error *) htab_find (m_symbol_err_map, &search);
227 if (err == NULL || err->message == NULL)
228 return;
229
230 gdb::unique_xmalloc_ptr<char> message (err->message);
231 err->message = NULL;
232 error (_("%s"), message.get ());
233}
234
bb2ec1b3
TT
235/* Implement "show debug compile". */
236
237static void
238show_compile_debug (struct ui_file *file, int from_tty,
239 struct cmd_list_element *c, const char *value)
240{
241 fprintf_filtered (file, _("Compile debugging is %s.\n"), value);
242}
243
244\f
245
246/* Check *ARG for a "-raw" or "-r" argument. Return 0 if not seen.
247 Return 1 if seen and update *ARG. */
248
249static int
6663cf91 250check_raw_argument (const char **arg)
bb2ec1b3
TT
251{
252 *arg = skip_spaces (*arg);
253
254 if (arg != NULL
255 && (check_for_argument (arg, "-raw", sizeof ("-raw") - 1)
256 || check_for_argument (arg, "-r", sizeof ("-r") - 1)))
257 return 1;
258 return 0;
259}
260
261/* Handle the input from the 'compile file' command. The "compile
262 file" command is used to evaluate an expression contained in a file
263 that may contain calls to the GCC compiler. */
264
265static void
6663cf91 266compile_file_command (const char *arg, int from_tty)
bb2ec1b3
TT
267{
268 enum compile_i_scope_types scope = COMPILE_I_SIMPLE_SCOPE;
bb2ec1b3 269
b7b633e9 270 scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
bb2ec1b3
TT
271
272 /* Check the user did not just <enter> after command. */
273 if (arg == NULL)
274 error (_("You must provide a filename for this command."));
275
276 /* Check if a raw (-r|-raw) argument is provided. */
277 if (arg != NULL && check_raw_argument (&arg))
278 {
279 scope = COMPILE_I_RAW_SCOPE;
280 arg = skip_spaces (arg);
281 }
282
283 /* After processing arguments, check there is a filename at the end
284 of the command. */
285 if (arg[0] == '\0')
286 error (_("You must provide a filename with the raw option set."));
287
288 if (arg[0] == '-')
289 error (_("Unknown argument specified."));
290
291 arg = skip_spaces (arg);
e3e41d58
TT
292 gdb::unique_xmalloc_ptr<char> abspath = gdb_abspath (arg);
293 std::string buffer = string_printf ("#include \"%s\"\n", abspath.get ());
294 eval_compile_command (NULL, buffer.c_str (), scope, NULL);
bb2ec1b3
TT
295}
296
297/* Handle the input from the 'compile code' command. The
298 "compile code" command is used to evaluate an expression that may
299 contain calls to the GCC compiler. The language expected in this
300 compile command is the language currently set in GDB. */
301
302static void
6663cf91 303compile_code_command (const char *arg, int from_tty)
bb2ec1b3 304{
bb2ec1b3
TT
305 enum compile_i_scope_types scope = COMPILE_I_SIMPLE_SCOPE;
306
b7b633e9 307 scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
bb2ec1b3
TT
308
309 if (arg != NULL && check_raw_argument (&arg))
310 {
311 scope = COMPILE_I_RAW_SCOPE;
312 arg = skip_spaces (arg);
313 }
314
315 arg = skip_spaces (arg);
316
317 if (arg != NULL && !check_for_argument (&arg, "--", sizeof ("--") - 1))
318 {
319 if (arg[0] == '-')
320 error (_("Unknown argument specified."));
321 }
322
323 if (arg && *arg)
5c65b58a 324 eval_compile_command (NULL, arg, scope, NULL);
bb2ec1b3
TT
325 else
326 {
12973681 327 counted_command_line l = get_command_line (compile_control, "");
bb2ec1b3 328
bb2ec1b3 329 l->control_u.compile.scope = scope;
93921405 330 execute_control_command_untraced (l.get ());
bb2ec1b3 331 }
bb2ec1b3
TT
332}
333
36de76f9
JK
334/* Callback for compile_print_command. */
335
336void
337compile_print_value (struct value *val, void *data_voidp)
338{
9a3c8263 339 const struct format_data *fmtp = (const struct format_data *) data_voidp;
36de76f9
JK
340
341 print_value (val, fmtp);
342}
343
344/* Handle the input from the 'compile print' command. The "compile
345 print" command is used to evaluate and print an expression that may
346 contain calls to the GCC compiler. The language expected in this
347 compile command is the language currently set in GDB. */
348
349static void
6663cf91 350compile_print_command (const char *arg, int from_tty)
36de76f9 351{
36de76f9
JK
352 enum compile_i_scope_types scope = COMPILE_I_PRINT_ADDRESS_SCOPE;
353 struct format_data fmt;
354
b7b633e9 355 scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
36de76f9
JK
356
357 /* Passing &FMT as SCOPE_DATA is safe as do_module_cleanup will not
358 touch the stale pointer if compile_object_run has already quit. */
359 print_command_parse_format (&arg, "compile print", &fmt);
360
361 if (arg && *arg)
362 eval_compile_command (NULL, arg, scope, &fmt);
363 else
364 {
12973681 365 counted_command_line l = get_command_line (compile_control, "");
36de76f9 366
36de76f9
JK
367 l->control_u.compile.scope = scope;
368 l->control_u.compile.scope_data = &fmt;
93921405 369 execute_control_command_untraced (l.get ());
36de76f9 370 }
36de76f9
JK
371}
372
bb2ec1b3
TT
373/* A cleanup function to remove a directory and all its contents. */
374
375static void
376do_rmdir (void *arg)
377{
9a3c8263 378 const char *dir = (const char *) arg;
bb2ec1b3 379 char *zap;
3ce348af
CG
380 int wstat;
381
61012eef 382 gdb_assert (startswith (dir, TMP_PREFIX));
bb2ec1b3 383 zap = concat ("rm -rf ", dir, (char *) NULL);
3ce348af
CG
384 wstat = system (zap);
385 if (wstat == -1 || !WIFEXITED (wstat) || WEXITSTATUS (wstat) != 0)
386 warning (_("Could not remove temporary directory %s"), dir);
387 XDELETEVEC (zap);
bb2ec1b3
TT
388}
389
390/* Return the name of the temporary directory to use for .o files, and
391 arrange for the directory to be removed at shutdown. */
392
393static const char *
394get_compile_file_tempdir (void)
395{
396 static char *tempdir_name;
397
398#define TEMPLATE TMP_PREFIX "XXXXXX"
399 char tname[sizeof (TEMPLATE)];
400
401 if (tempdir_name != NULL)
402 return tempdir_name;
403
404 strcpy (tname, TEMPLATE);
405#undef TEMPLATE
1bc1068a 406#ifdef HAVE_MKDTEMP
bb2ec1b3 407 tempdir_name = mkdtemp (tname);
1bc1068a
JK
408#else
409 error (_("Command not supported on this host."));
410#endif
bb2ec1b3
TT
411 if (tempdir_name == NULL)
412 perror_with_name (_("Could not make temporary directory"));
413
414 tempdir_name = xstrdup (tempdir_name);
415 make_final_cleanup (do_rmdir, tempdir_name);
416 return tempdir_name;
417}
418
aaee65ae 419/* Compute the names of source and object files to use. */
bb2ec1b3 420
aaee65ae
PA
421static compile_file_names
422get_new_file_names ()
bb2ec1b3
TT
423{
424 static int seq;
425 const char *dir = get_compile_file_tempdir ();
426
427 ++seq;
aaee65ae
PA
428
429 return compile_file_names (string_printf ("%s%sout%d.c",
430 dir, SLASH_STRING, seq),
431 string_printf ("%s%sout%d.o",
432 dir, SLASH_STRING, seq));
bb2ec1b3
TT
433}
434
435/* Get the block and PC at which to evaluate an expression. */
436
437static const struct block *
438get_expr_block_and_pc (CORE_ADDR *pc)
439{
440 const struct block *block = get_selected_block (pc);
441
442 if (block == NULL)
443 {
444 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
445
446 if (cursal.symtab)
447 block = BLOCKVECTOR_BLOCK (SYMTAB_BLOCKVECTOR (cursal.symtab),
448 STATIC_BLOCK);
449 if (block != NULL)
450 *pc = BLOCK_START (block);
451 }
452 else
453 *pc = BLOCK_START (block);
454
455 return block;
456}
457
773a1edc
TT
458/* Call buildargv (via gdb_argv), set its result for S into *ARGVP but
459 calculate also the number of parsed arguments into *ARGCP. If
460 buildargv has returned NULL then *ARGCP is set to zero. */
bb2ec1b3
TT
461
462static void
463build_argc_argv (const char *s, int *argcp, char ***argvp)
464{
773a1edc
TT
465 gdb_argv args (s);
466
467 *argcp = args.count ();
468 *argvp = args.release ();
bb2ec1b3
TT
469}
470
471/* String for 'set compile-args' and 'show compile-args'. */
472static char *compile_args;
473
474/* Parsed form of COMPILE_ARGS. COMPILE_ARGS_ARGV is NULL terminated. */
475static int compile_args_argc;
476static char **compile_args_argv;
477
478/* Implement 'set compile-args'. */
479
480static void
eb4c3f4a 481set_compile_args (const char *args, int from_tty, struct cmd_list_element *c)
bb2ec1b3
TT
482{
483 freeargv (compile_args_argv);
484 build_argc_argv (compile_args, &compile_args_argc, &compile_args_argv);
485}
486
487/* Implement 'show compile-args'. */
488
489static void
490show_compile_args (struct ui_file *file, int from_tty,
491 struct cmd_list_element *c, const char *value)
492{
493 fprintf_filtered (file, _("Compile command command-line arguments "
494 "are \"%s\".\n"),
495 value);
496}
497
498/* Append ARGC and ARGV (as parsed by build_argc_argv) to *ARGCP and *ARGVP.
499 ARGCP+ARGVP can be zero+NULL and also ARGC+ARGV can be zero+NULL. */
500
501static void
502append_args (int *argcp, char ***argvp, int argc, char **argv)
503{
504 int argi;
505
8d749320 506 *argvp = XRESIZEVEC (char *, *argvp, (*argcp + argc + 1));
bb2ec1b3
TT
507
508 for (argi = 0; argi < argc; argi++)
509 (*argvp)[(*argcp)++] = xstrdup (argv[argi]);
510 (*argvp)[(*argcp)] = NULL;
511}
512
6e41ddec
JK
513/* String for 'set compile-gcc' and 'show compile-gcc'. */
514static char *compile_gcc;
515
516/* Implement 'show compile-gcc'. */
517
518static void
519show_compile_gcc (struct ui_file *file, int from_tty,
520 struct cmd_list_element *c, const char *value)
521{
522 fprintf_filtered (file, _("Compile command GCC driver filename is \"%s\".\n"),
523 value);
524}
525
bb2ec1b3
TT
526/* Return DW_AT_producer parsed for get_selected_frame () (if any).
527 Return NULL otherwise.
528
529 GCC already filters its command-line arguments only for the suitable ones to
530 put into DW_AT_producer - see GCC function gen_producer_string. */
531
532static const char *
533get_selected_pc_producer_options (void)
534{
535 CORE_ADDR pc = get_frame_pc (get_selected_frame (NULL));
536 struct compunit_symtab *symtab = find_pc_compunit_symtab (pc);
537 const char *cs;
538
539 if (symtab == NULL || symtab->producer == NULL
61012eef 540 || !startswith (symtab->producer, "GNU "))
bb2ec1b3
TT
541 return NULL;
542
543 cs = symtab->producer;
544 while (*cs != 0 && *cs != '-')
f1735a53 545 cs = skip_spaces (skip_to_space (cs));
bb2ec1b3
TT
546 if (*cs != '-')
547 return NULL;
548 return cs;
549}
550
a7606d80
JK
551/* Filter out unwanted options from *ARGCP and ARGV. */
552
553static void
554filter_args (int *argcp, char **argv)
555{
556 char **destv;
557
558 for (destv = argv; *argv != NULL; argv++)
559 {
560 /* -fpreprocessed may get in commonly from ccache. */
561 if (strcmp (*argv, "-fpreprocessed") == 0)
562 {
563 xfree (*argv);
564 (*argcp)--;
565 continue;
566 }
567 *destv++ = *argv;
568 }
569 *destv = NULL;
570}
571
e05cac70
PM
572/* Produce final vector of GCC compilation options.
573
574 The first element of the combined argument vector are arguments
575 relating to the target size ("-m64", "-m32" etc.). These are
576 sourced from the inferior's architecture.
577
578 The second element of the combined argument vector are arguments
579 stored in the inferior DW_AT_producer section. If these are stored
580 in the inferior (there is no guarantee that they are), they are
581 added to the vector.
582
583 The third element of the combined argument vector are argument
584 supplied by the language implementation provided by
585 compile-{lang}-support. These contain language specific arguments.
586
587 The final element of the combined argument vector are arguments
588 supplied by the "set compile-args" command. These are always
589 appended last so as to override any of the arguments automatically
590 generated above. */
bb2ec1b3
TT
591
592static void
9cdfd9a2 593get_args (const compile_instance *compiler, struct gdbarch *gdbarch,
bb2ec1b3
TT
594 int *argcp, char ***argvp)
595{
596 const char *cs_producer_options;
597 int argc_compiler;
598 char **argv_compiler;
599
600 build_argc_argv (gdbarch_gcc_target_options (gdbarch),
601 argcp, argvp);
602
603 cs_producer_options = get_selected_pc_producer_options ();
604 if (cs_producer_options != NULL)
605 {
606 int argc_producer;
607 char **argv_producer;
608
609 build_argc_argv (cs_producer_options, &argc_producer, &argv_producer);
a7606d80 610 filter_args (&argc_producer, argv_producer);
bb2ec1b3
TT
611 append_args (argcp, argvp, argc_producer, argv_producer);
612 freeargv (argv_producer);
613 }
614
9cdfd9a2 615 build_argc_argv (compiler->gcc_target_options ().c_str (),
bb2ec1b3
TT
616 &argc_compiler, &argv_compiler);
617 append_args (argcp, argvp, argc_compiler, argv_compiler);
618 freeargv (argv_compiler);
619
620 append_args (argcp, argvp, compile_args_argc, compile_args_argv);
621}
622
bb2ec1b3
TT
623/* A helper function suitable for use as the "print_callback" in the
624 compiler object. */
625
626static void
627print_callback (void *ignore, const char *message)
628{
629 fputs_filtered (message, gdb_stderr);
630}
631
632/* Process the compilation request. On success it returns the object
aaee65ae
PA
633 and source file names. On an error condition, error () is
634 called. */
bb2ec1b3 635
aaee65ae 636static compile_file_names
851c9091 637compile_to_object (struct command_line *cmd, const char *cmd_string,
aaee65ae 638 enum compile_i_scope_types scope)
bb2ec1b3 639{
bb2ec1b3
TT
640 const struct block *expr_block;
641 CORE_ADDR trash_pc, expr_pc;
642 int argc;
643 char **argv;
644 int ok;
bb2ec1b3 645 struct gdbarch *gdbarch = get_current_arch ();
7d937cad 646 std::string triplet_rx;
bb2ec1b3
TT
647
648 if (!target_has_execution)
649 error (_("The program must be running for the compile command to "\
650 "work."));
651
652 expr_block = get_expr_block_and_pc (&trash_pc);
653 expr_pc = get_frame_address_in_block (get_selected_frame (NULL));
654
655 /* Set up instance and context for the compiler. */
656 if (current_language->la_get_compile_instance == NULL)
cdaec3f3
LM
657 error (_("No compiler support for language %s."),
658 current_language->la_name);
bb2ec1b3 659
9cdfd9a2
KS
660 compile_instance *compiler_instance
661 = current_language->la_get_compile_instance ();
662 std::unique_ptr<compile_instance> compiler (compiler_instance);
663 compiler->set_print_callback (print_callback, NULL);
664 compiler->set_scope (scope);
665 compiler->set_block (expr_block);
bb2ec1b3
TT
666
667 /* From the provided expression, build a scope to pass to the
668 compiler. */
aaee65ae 669
d7e74731 670 string_file input_buf;
aaee65ae
PA
671 const char *input;
672
bb2ec1b3
TT
673 if (cmd != NULL)
674 {
bb2ec1b3
TT
675 struct command_line *iter;
676
12973681 677 for (iter = cmd->body_list_0.get (); iter; iter = iter->next)
bb2ec1b3 678 {
d7e74731
PA
679 input_buf.puts (iter->line);
680 input_buf.puts ("\n");
bb2ec1b3
TT
681 }
682
aaee65ae 683 input = input_buf.c_str ();
bb2ec1b3
TT
684 }
685 else if (cmd_string != NULL)
851c9091 686 input = cmd_string;
bb2ec1b3
TT
687 else
688 error (_("Neither a simple expression, or a multi-line specified."));
689
aaee65ae 690 std::string code
9cdfd9a2 691 = current_language->la_compute_program (compiler.get (), input, gdbarch,
aaee65ae 692 expr_block, expr_pc);
bb2ec1b3 693 if (compile_debug)
aaee65ae 694 fprintf_unfiltered (gdb_stdlog, "debug output:\n\n%s", code.c_str ());
bb2ec1b3 695
9cdfd9a2 696 compiler->set_verbose (compile_debug);
71b30f27 697
6e41ddec
JK
698 if (compile_gcc[0] != 0)
699 {
9cdfd9a2 700 if (compiler->version () < GCC_FE_VERSION_1)
6e41ddec
JK
701 error (_("Command 'set compile-gcc' requires GCC version 6 or higher "
702 "(libcc1 interface version 1 or higher)"));
703
9cdfd9a2 704 compiler->set_driver_filename (compile_gcc);
6e41ddec
JK
705 }
706 else
707 {
708 const char *os_rx = osabi_triplet_regexp (gdbarch_osabi (gdbarch));
709 const char *arch_rx = gdbarch_gnu_triplet_regexp (gdbarch);
710
711 /* Allow triplets with or without vendor set. */
7d937cad 712 triplet_rx = std::string (arch_rx) + "(-[^-]*)?-" + os_rx;
9cdfd9a2 713 compiler->set_triplet_regexp (triplet_rx.c_str ());
6e41ddec 714 }
bb2ec1b3
TT
715
716 /* Set compiler command-line arguments. */
9cdfd9a2 717 get_args (compiler.get (), gdbarch, &argc, &argv);
773a1edc 718 gdb_argv argv_holder (argv);
bb2ec1b3 719
9cdfd9a2
KS
720 gdb::unique_xmalloc_ptr<char> error_message;
721 error_message.reset (compiler->set_arguments (argc, argv,
722 triplet_rx.c_str ()));
723
bb2ec1b3 724 if (error_message != NULL)
9cdfd9a2 725 error ("%s", error_message.get ());
bb2ec1b3
TT
726
727 if (compile_debug)
728 {
729 int argi;
730
a4063588 731 fprintf_unfiltered (gdb_stdlog, "Passing %d compiler options:\n", argc);
bb2ec1b3 732 for (argi = 0; argi < argc; argi++)
a4063588 733 fprintf_unfiltered (gdb_stdlog, "Compiler option %d: <%s>\n",
bb2ec1b3
TT
734 argi, argv[argi]);
735 }
736
aaee65ae 737 compile_file_names fnames = get_new_file_names ();
bb2ec1b3 738
b80cf838
TT
739 gdb::optional<gdb::unlinker> source_remover;
740
d419f42d
TT
741 {
742 gdb_file_up src = gdb_fopen_cloexec (fnames.source_file (), "w");
743 if (src == NULL)
744 perror_with_name (_("Could not open source file for writing"));
b80cf838
TT
745
746 source_remover.emplace (fnames.source_file ());
747
d419f42d
TT
748 if (fputs (code.c_str (), src.get ()) == EOF)
749 perror_with_name (_("Could not write to source file"));
750 }
bb2ec1b3
TT
751
752 if (compile_debug)
a4063588 753 fprintf_unfiltered (gdb_stdlog, "source file produced: %s\n\n",
aaee65ae 754 fnames.source_file ());
bb2ec1b3
TT
755
756 /* Call the compiler and start the compilation process. */
9cdfd9a2
KS
757 compiler->set_source_file (fnames.source_file ());
758 ok = compiler->compile (fnames.object_file (), compile_debug);
e68c32d5 759 if (!ok)
bb2ec1b3
TT
760 error (_("Compilation failed."));
761
762 if (compile_debug)
a4063588 763 fprintf_unfiltered (gdb_stdlog, "object file produced: %s\n\n",
aaee65ae 764 fnames.object_file ());
bb2ec1b3 765
b80cf838
TT
766 /* Keep the source file. */
767 source_remover->keep ();
aaee65ae 768 return fnames;
bb2ec1b3
TT
769}
770
771/* The "compile" prefix command. */
772
773static void
981a3fb3 774compile_command (const char *args, int from_tty)
bb2ec1b3
TT
775{
776 /* If a sub-command is not specified to the compile prefix command,
777 assume it is a direct code compilation. */
778 compile_code_command (args, from_tty);
779}
780
781/* See compile.h. */
782
783void
851c9091 784eval_compile_command (struct command_line *cmd, const char *cmd_string,
5c65b58a 785 enum compile_i_scope_types scope, void *scope_data)
bb2ec1b3 786{
aaee65ae
PA
787 struct compile_module *compile_module;
788
789 compile_file_names fnames = compile_to_object (cmd, cmd_string, scope);
bb2ec1b3 790
b80cf838
TT
791 gdb::unlinker object_remover (fnames.object_file ());
792 gdb::unlinker source_remover (fnames.source_file ());
793
aaee65ae
PA
794 compile_module = compile_object_load (fnames, scope, scope_data);
795 if (compile_module == NULL)
bb2ec1b3 796 {
aaee65ae
PA
797 gdb_assert (scope == COMPILE_I_PRINT_ADDRESS_SCOPE);
798 eval_compile_command (cmd, cmd_string,
799 COMPILE_I_PRINT_VALUE_SCOPE, scope_data);
800 return;
bb2ec1b3 801 }
b80cf838
TT
802
803 /* Keep the files. */
804 source_remover.keep ();
805 object_remover.keep ();
806
aaee65ae 807 compile_object_run (compile_module);
bb2ec1b3
TT
808}
809
810/* See compile/compile-internal.h. */
811
8f84fb0e 812std::string
bb2ec1b3
TT
813compile_register_name_mangled (struct gdbarch *gdbarch, int regnum)
814{
815 const char *regname = gdbarch_register_name (gdbarch, regnum);
816
8f84fb0e 817 return string_printf ("__%s", regname);
bb2ec1b3
TT
818}
819
820/* See compile/compile-internal.h. */
821
822int
823compile_register_name_demangle (struct gdbarch *gdbarch,
824 const char *regname)
825{
826 int regnum;
827
828 if (regname[0] != '_' || regname[1] != '_')
829 error (_("Invalid register name \"%s\"."), regname);
830 regname += 2;
831
832 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
833 if (strcmp (regname, gdbarch_register_name (gdbarch, regnum)) == 0)
834 return regnum;
835
836 error (_("Cannot find gdbarch register \"%s\"."), regname);
837}
838
9cdfd9a2
KS
839/* Forwards to the plug-in. */
840
841#define FORWARD(OP,...) (m_gcc_fe->ops->OP (m_gcc_fe, ##__VA_ARGS__))
842
843/* See compile-internal.h. */
844
845void
846compile_instance::set_print_callback
847 (void (*print_function) (void *, const char *), void *datum)
848{
849 FORWARD (set_print_callback, print_function, datum);
850}
851
852/* See compile-internal.h. */
853
854unsigned int
855compile_instance::version () const
856{
857 return m_gcc_fe->ops->version;
858}
859
860/* See compile-internal.h. */
861
862void
863compile_instance::set_verbose (int level)
864{
865 if (version () >= GCC_FE_VERSION_1)
866 FORWARD (set_verbose, level);
867}
868
869/* See compile-internal.h. */
870
871void
872compile_instance::set_driver_filename (const char *filename)
873{
874 if (version () >= GCC_FE_VERSION_1)
875 FORWARD (set_driver_filename, filename);
876}
877
878/* See compile-internal.h. */
879
880void
881compile_instance::set_triplet_regexp (const char *regexp)
882{
883 if (version () >= GCC_FE_VERSION_1)
884 FORWARD (set_triplet_regexp, regexp);
885}
886
887/* See compile-internal.h. */
888
889char *
890compile_instance::set_arguments (int argc, char **argv, const char *regexp)
891{
892 if (version () >= GCC_FE_VERSION_1)
893 return FORWARD (set_arguments, argc, argv);
894 else
895 return FORWARD (set_arguments_v0, regexp, argc, argv);
896}
897
898/* See compile-internal.h. */
899
900void
901compile_instance::set_source_file (const char *filename)
902{
903 FORWARD (set_source_file, filename);
904}
905
906/* See compile-internal.h. */
907
908bool
909compile_instance::compile (const char *filename, int verbose_level)
910{
911 if (version () >= GCC_FE_VERSION_1)
912 return FORWARD (compile, filename);
913 else
914 return FORWARD (compile_v0, filename, verbose_level);
915}
916
917#undef FORWARD
918
919
bb2ec1b3
TT
920void
921_initialize_compile (void)
922{
923 struct cmd_list_element *c = NULL;
924
925 add_prefix_cmd ("compile", class_obscure, compile_command,
926 _("\
927Command to compile source code and inject it into the inferior."),
928 &compile_command_list, "compile ", 1, &cmdlist);
929 add_com_alias ("expression", "compile", class_obscure, 0);
930
931 add_cmd ("code", class_obscure, compile_code_command,
932 _("\
933Compile, inject, and execute code.\n\
934\n\
935Usage: compile code [-r|-raw] [--] [CODE]\n\
936-r|-raw: Suppress automatic 'void _gdb_expr () { CODE }' wrapping.\n\
937--: Do not parse any options beyond this delimiter. All text to the\n\
938 right will be treated as source code.\n\
939\n\
940The source code may be specified as a simple one line expression, e.g.:\n\
941\n\
942 compile code printf(\"Hello world\\n\");\n\
943\n\
36de76f9
JK
944Alternatively, you can type a multiline expression by invoking\n\
945this command with no argument. GDB will then prompt for the\n\
946expression interactively; type a line containing \"end\" to\n\
947indicate the end of the expression."),
bb2ec1b3
TT
948 &compile_command_list);
949
950 c = add_cmd ("file", class_obscure, compile_file_command,
951 _("\
952Evaluate a file containing source code.\n\
953\n\
954Usage: compile file [-r|-raw] [filename]\n\
955-r|-raw: Suppress automatic 'void _gdb_expr () { CODE }' wrapping."),
956 &compile_command_list);
957 set_cmd_completer (c, filename_completer);
958
36de76f9
JK
959 add_cmd ("print", class_obscure, compile_print_command,
960 _("\
961Evaluate EXPR by using the compiler and print result.\n\
962\n\
963Usage: compile print[/FMT] [EXPR]\n\
964\n\
965The expression may be specified on the same line as the command, e.g.:\n\
966\n\
967 compile print i\n\
968\n\
969Alternatively, you can type a multiline expression by invoking\n\
970this command with no argument. GDB will then prompt for the\n\
971expression interactively; type a line containing \"end\" to\n\
972indicate the end of the expression.\n\
973\n\
974EXPR may be preceded with /FMT, where FMT is a format letter\n\
975but no count or size letter (see \"x\" command)."),
976 &compile_command_list);
977
bb2ec1b3
TT
978 add_setshow_boolean_cmd ("compile", class_maintenance, &compile_debug, _("\
979Set compile command debugging."), _("\
980Show compile command debugging."), _("\
981When on, compile command debugging is enabled."),
982 NULL, show_compile_debug,
983 &setdebuglist, &showdebuglist);
984
985 add_setshow_string_cmd ("compile-args", class_support,
986 &compile_args,
987 _("Set compile command GCC command-line arguments"),
988 _("Show compile command GCC command-line arguments"),
989 _("\
990Use options like -I (include file directory) or ABI settings.\n\
991String quoting is parsed like in shell, for example:\n\
992 -mno-align-double \"-I/dir with a space/include\""),
993 set_compile_args, show_compile_args, &setlist, &showlist);
994
995 /* Override flags possibly coming from DW_AT_producer. */
996 compile_args = xstrdup ("-O0 -gdwarf-4"
4b62a76e 997 /* We use -fPIE Otherwise GDB would need to reserve space large enough for
bb2ec1b3
TT
998 any object file in the inferior in advance to get the final address when
999 to link the object file to and additionally the default system linker
1000 script would need to be modified so that one can specify there the
4b62a76e
JK
1001 absolute target address.
1002 -fPIC is not used at is would require from GDB to generate .got. */
1003 " -fPIE"
3a9558c4
JK
1004 /* We want warnings, except for some commonly happening for GDB commands. */
1005 " -Wall "
1006 " -Wno-implicit-function-declaration"
1007 " -Wno-unused-but-set-variable"
1008 " -Wno-unused-variable"
bb2ec1b3
TT
1009 /* Override CU's possible -fstack-protector-strong. */
1010 " -fno-stack-protector"
1011 );
1012 set_compile_args (compile_args, 0, NULL);
6e41ddec
JK
1013
1014 add_setshow_optional_filename_cmd ("compile-gcc", class_support,
1015 &compile_gcc,
1016 _("Set compile command "
1017 "GCC driver filename"),
1018 _("Show compile command "
1019 "GCC driver filename"),
1020 _("\
1021It should be absolute filename of the gcc executable.\n\
1022If empty the default target triplet will be searched in $PATH."),
1023 NULL, show_compile_gcc, &setlist,
1024 &showlist);
1025 compile_gcc = xstrdup ("");
bb2ec1b3 1026}
This page took 0.310887 seconds and 4 git commands to generate.