1 /* The library used by gdb.
2 Copyright (C) 2014-2020 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include <cc1plugin-config.h>
23 #include <sys/socket.h>
24 #include <sys/types.h>
32 #include "marshall-c.hh"
34 #include "connection.hh"
36 #include "callbacks.hh"
37 #include "libiberty.h"
39 #include "findcomp.hh"
40 #include "compiler-name.hh"
45 class libcc1_connection
;
47 // The C compiler context that we hand back to our caller.
48 struct libcc1
: public gcc_c_context
50 libcc1 (const gcc_base_vtable
*, const gcc_c_fe_vtable
*);
53 // A convenience function to print something.
54 void print (const char *str
)
56 this->print_function (this->print_datum
, str
);
59 libcc1_connection
*connection
;
61 gcc_c_oracle_function
*binding_oracle
;
62 gcc_c_symbol_address_function
*address_oracle
;
65 void (*print_function
) (void *datum
, const char *message
);
68 std::vector
<std::string
> args
;
69 std::string source_file
;
71 /* Non-zero as an equivalent to gcc driver option "-v". */
74 /* Compiler to set by set_triplet_regexp or set_driver_filename. */
80 compiler (libcc1
*self
) : self_ (self
)
83 virtual char *find (std::string
&compiler
) const;
89 /* Compiler to set by set_triplet_regexp. */
90 class compiler_triplet_regexp
: public compiler
93 std::string triplet_regexp_
;
95 virtual char *find (std::string
&compiler
) const;
96 compiler_triplet_regexp (libcc1
*self
, std::string triplet_regexp
)
97 : compiler (self
), triplet_regexp_ (triplet_regexp
)
100 virtual ~compiler_triplet_regexp ()
105 /* Compiler to set by set_driver_filename. */
106 class compiler_driver_filename
: public compiler
109 std::string driver_filename_
;
111 virtual char *find (std::string
&compiler
) const;
112 compiler_driver_filename (libcc1
*self
, std::string driver_filename
)
113 : compiler (self
), driver_filename_ (driver_filename
)
116 virtual ~compiler_driver_filename ()
122 // A local subclass of connection that holds a back-pointer to the
123 // gcc_c_context object that we provide to our caller.
124 class libcc1_connection
: public cc1_plugin::connection
128 libcc1_connection (int fd
, int aux_fd
, libcc1
*b
)
129 : connection (fd
, aux_fd
),
134 virtual void print (const char *buf
)
136 back_ptr
->print (buf
);
142 libcc1::libcc1 (const gcc_base_vtable
*v
,
143 const gcc_c_fe_vtable
*cv
)
145 binding_oracle (NULL
),
146 address_oracle (NULL
),
148 print_function (NULL
),
153 compilerp (new libcc1::compiler (this))
167 // Enclose these functions in an anonymous namespace because they
168 // shouldn't be exported, but they can't be static because they're
169 // used as template arguments.
171 // This is a wrapper function that is called by the RPC system and
172 // that then forwards the call to the library user. Note that the
173 // return value is not used; the type cannot be 'void' due to
174 // limitations in our simple RPC.
176 c_call_binding_oracle (cc1_plugin::connection
*conn
,
177 enum gcc_c_oracle_request request
,
178 const char *identifier
)
180 libcc1
*self
= ((libcc1_connection
*) conn
)->back_ptr
;
182 self
->binding_oracle (self
->oracle_datum
, self
, request
, identifier
);
186 // This is a wrapper function that is called by the RPC system and
187 // that then forwards the call to the library user.
189 c_call_symbol_address (cc1_plugin::connection
*conn
, const char *identifier
)
191 libcc1
*self
= ((libcc1_connection
*) conn
)->back_ptr
;
193 return self
->address_oracle (self
->oracle_datum
, self
, identifier
);
195 } /* anonymous namespace */
200 set_callbacks (struct gcc_c_context
*s
,
201 gcc_c_oracle_function
*binding_oracle
,
202 gcc_c_symbol_address_function
*address_oracle
,
205 libcc1
*self
= (libcc1
*) s
;
207 self
->binding_oracle
= binding_oracle
;
208 self
->address_oracle
= address_oracle
;
209 self
->oracle_datum
= datum
;
212 // Instances of these rpc<> template functions are installed into the
213 // "c_vtable". These functions are parameterized by type and method
214 // name and forward the call via the connection.
216 template<typename R
, const char *&NAME
>
217 R
rpc (struct gcc_c_context
*s
)
219 libcc1
*self
= (libcc1
*) s
;
222 if (!cc1_plugin::call (self
->connection
, NAME
, &result
))
227 template<typename R
, const char *&NAME
, typename A
>
228 R
rpc (struct gcc_c_context
*s
, A arg
)
230 libcc1
*self
= (libcc1
*) s
;
233 if (!cc1_plugin::call (self
->connection
, NAME
, &result
, arg
))
238 template<typename R
, const char *&NAME
, typename A1
, typename A2
>
239 R
rpc (struct gcc_c_context
*s
, A1 arg1
, A2 arg2
)
241 libcc1
*self
= (libcc1
*) s
;
244 if (!cc1_plugin::call (self
->connection
, NAME
, &result
, arg1
, arg2
))
249 template<typename R
, const char *&NAME
, typename A1
, typename A2
, typename A3
>
250 R
rpc (struct gcc_c_context
*s
, A1 arg1
, A2 arg2
, A3 arg3
)
252 libcc1
*self
= (libcc1
*) s
;
255 if (!cc1_plugin::call (self
->connection
, NAME
, &result
, arg1
, arg2
, arg3
))
260 template<typename R
, const char *&NAME
, typename A1
, typename A2
, typename A3
,
262 R
rpc (struct gcc_c_context
*s
, A1 arg1
, A2 arg2
, A3 arg3
, A4 arg4
)
264 libcc1
*self
= (libcc1
*) s
;
267 if (!cc1_plugin::call (self
->connection
, NAME
, &result
, arg1
, arg2
, arg3
,
273 template<typename R
, const char *&NAME
, typename A1
, typename A2
, typename A3
,
274 typename A4
, typename A5
>
275 R
rpc (struct gcc_c_context
*s
, A1 arg1
, A2 arg2
, A3 arg3
, A4 arg4
, A5 arg5
)
277 libcc1
*self
= (libcc1
*) s
;
280 if (!cc1_plugin::call (self
->connection
, NAME
, &result
, arg1
, arg2
, arg3
,
286 template<typename R
, const char *&NAME
, typename A1
, typename A2
, typename A3
,
287 typename A4
, typename A5
, typename A6
, typename A7
>
288 R
rpc (struct gcc_c_context
*s
, A1 arg1
, A2 arg2
, A3 arg3
, A4 arg4
, A5 arg5
,
291 libcc1
*self
= (libcc1
*) s
;
294 if (!cc1_plugin::call (self
->connection
, NAME
, &result
, arg1
, arg2
, arg3
,
295 arg4
, arg5
, arg6
, arg7
))
300 static const struct gcc_c_fe_vtable c_vtable
=
305 #define GCC_METHOD0(R, N) \
306 rpc<R, cc1_plugin::c::N>,
307 #define GCC_METHOD1(R, N, A) \
308 rpc<R, cc1_plugin::c::N, A>,
309 #define GCC_METHOD2(R, N, A, B) \
310 rpc<R, cc1_plugin::c::N, A, B>,
311 #define GCC_METHOD3(R, N, A, B, C) \
312 rpc<R, cc1_plugin::c::N, A, B, C>,
313 #define GCC_METHOD4(R, N, A, B, C, D) \
314 rpc<R, cc1_plugin::c::N, A, B, C, D>,
315 #define GCC_METHOD5(R, N, A, B, C, D, E) \
316 rpc<R, cc1_plugin::c::N, A, B, C, D, E>,
317 #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
318 rpc<R, cc1_plugin::c::N, A, B, C, D, E, F, G>,
320 #include "gcc-c-fe.def"
333 // Construct an appropriate regexp to match the compiler name.
335 make_regexp (const char *triplet_regexp
, const char *compiler
)
337 std::stringstream buf
;
339 buf
<< "^" << triplet_regexp
<< "-";
341 // Quote the compiler name in case it has something funny in it.
342 for (const char *p
= compiler
; *p
; ++p
)
369 libcc1_set_verbose (struct gcc_base_context
*s
, int /* bool */ verbose
)
371 libcc1
*self
= (libcc1
*) s
;
373 self
->verbose
= verbose
!= 0;
377 libcc1::compiler::find (std::string
&compiler ATTRIBUTE_UNUSED
) const
379 return xstrdup (_("Compiler has not been specified"));
383 libcc1::compiler_triplet_regexp::find (std::string
&compiler
) const
385 std::string rx
= make_regexp (triplet_regexp_
.c_str (), C_COMPILER_NAME
);
387 fprintf (stderr
, _("searching for compiler matching regex %s\n"),
390 int code
= regcomp (&triplet
, rx
.c_str (), REG_EXTENDED
| REG_NOSUB
);
393 size_t len
= regerror (code
, &triplet
, NULL
, 0);
396 regerror (code
, &triplet
, err
, len
);
398 return concat ("Could not compile regexp \"",
405 if (!find_compiler (triplet
, &compiler
))
408 return concat ("Could not find a compiler matching \"",
415 fprintf (stderr
, _("found compiler %s\n"), compiler
.c_str());
420 libcc1::compiler_driver_filename::find (std::string
&compiler
) const
422 // Simulate fnotice by fprintf.
424 fprintf (stderr
, _("using explicit compiler filename %s\n"),
425 driver_filename_
.c_str());
426 compiler
= driver_filename_
;
431 libcc1_set_arguments (struct gcc_base_context
*s
,
432 int argc
, char **argv
)
434 libcc1
*self
= (libcc1
*) s
;
436 std::string compiler
;
437 char *errmsg
= self
->compilerp
->find (compiler
);
441 self
->args
.push_back (compiler
);
443 for (int i
= 0; i
< argc
; ++i
)
444 self
->args
.push_back (argv
[i
]);
450 libcc1_set_triplet_regexp (struct gcc_base_context
*s
,
451 const char *triplet_regexp
)
453 libcc1
*self
= (libcc1
*) s
;
455 delete self
->compilerp
;
456 self
->compilerp
= new libcc1::compiler_triplet_regexp (self
, triplet_regexp
);
461 libcc1_set_driver_filename (struct gcc_base_context
*s
,
462 const char *driver_filename
)
464 libcc1
*self
= (libcc1
*) s
;
466 delete self
->compilerp
;
467 self
->compilerp
= new libcc1::compiler_driver_filename (self
,
473 libcc1_set_arguments_v0 (struct gcc_base_context
*s
,
474 const char *triplet_regexp
,
475 int argc
, char **argv
)
477 char *errmsg
= libcc1_set_triplet_regexp (s
, triplet_regexp
);
481 return libcc1_set_arguments (s
, argc
, argv
);
485 libcc1_set_source_file (struct gcc_base_context
*s
,
488 libcc1
*self
= (libcc1
*) s
;
490 self
->source_file
= file
;
494 libcc1_set_print_callback (struct gcc_base_context
*s
,
495 void (*print_function
) (void *datum
,
496 const char *message
),
499 libcc1
*self
= (libcc1
*) s
;
501 self
->print_function
= print_function
;
502 self
->print_datum
= datum
;
506 fork_exec (libcc1
*self
, char **argv
, int spair_fds
[2], int stderr_fds
[2])
508 pid_t child_pid
= fork ();
512 close (spair_fds
[0]);
513 close (spair_fds
[1]);
514 close (stderr_fds
[0]);
515 close (stderr_fds
[1]);
522 dup2 (stderr_fds
[1], 1);
523 dup2 (stderr_fds
[1], 2);
524 close (stderr_fds
[0]);
525 close (stderr_fds
[1]);
526 close (spair_fds
[0]);
528 execvp (argv
[0], argv
);
534 close (spair_fds
[1]);
535 close (stderr_fds
[1]);
537 cc1_plugin::status result
= cc1_plugin::FAIL
;
538 if (self
->connection
->send ('H')
539 && ::cc1_plugin::marshall (self
->connection
, GCC_C_FE_VERSION_1
))
540 result
= self
->connection
->wait_for_query ();
542 close (spair_fds
[0]);
543 close (stderr_fds
[0]);
549 if (waitpid (child_pid
, &status
, 0) == -1)
555 if (!WIFEXITED (status
) || WEXITSTATUS (status
) != 0)
567 libcc1_compile (struct gcc_base_context
*s
,
568 const char *filename
)
570 libcc1
*self
= (libcc1
*) s
;
573 if (socketpair (AF_UNIX
, SOCK_STREAM
, 0, fds
) != 0)
575 self
->print ("could not create socketpair\n");
580 if (pipe (stderr_fds
) != 0)
582 self
->print ("could not create pipe\n");
588 self
->args
.push_back ("-fplugin=libcc1plugin");
590 if (snprintf (buf
, sizeof (buf
), "-fplugin-arg-libcc1plugin-fd=%d", fds
[1])
591 >= (long) sizeof (buf
))
593 self
->args
.push_back (buf
);
595 self
->args
.push_back (self
->source_file
);
596 self
->args
.push_back ("-c");
597 self
->args
.push_back ("-o");
598 self
->args
.push_back (filename
);
600 self
->args
.push_back ("-v");
602 self
->connection
= new libcc1_connection (fds
[0], stderr_fds
[0], self
);
604 cc1_plugin::callback_ftype
*fun
605 = cc1_plugin::callback
<int,
606 enum gcc_c_oracle_request
,
608 c_call_binding_oracle
>;
609 self
->connection
->add_callback ("binding_oracle", fun
);
611 fun
= cc1_plugin::callback
<gcc_address
,
613 c_call_symbol_address
>;
614 self
->connection
->add_callback ("address_oracle", fun
);
616 char **argv
= new (std::nothrow
) char *[self
->args
.size () + 1];
620 for (unsigned int i
= 0; i
< self
->args
.size (); ++i
)
621 argv
[i
] = const_cast<char *> (self
->args
[i
].c_str ());
622 argv
[self
->args
.size ()] = NULL
;
624 return fork_exec (self
, argv
, fds
, stderr_fds
);
628 libcc1_compile_v0 (struct gcc_base_context
*s
, const char *filename
,
631 libcc1_set_verbose (s
, verbose
);
632 return libcc1_compile (s
, filename
);
636 libcc1_destroy (struct gcc_base_context
*s
)
638 libcc1
*self
= (libcc1
*) s
;
643 static const struct gcc_base_vtable vtable
=
646 libcc1_set_arguments_v0
,
647 libcc1_set_source_file
,
648 libcc1_set_print_callback
,
653 libcc1_set_arguments
,
654 libcc1_set_triplet_regexp
,
655 libcc1_set_driver_filename
,
658 extern "C" gcc_c_fe_context_function gcc_c_fe_context
;
661 #pragma GCC visibility push(default)
665 struct gcc_c_context
*
666 gcc_c_fe_context (enum gcc_base_api_version base_version
,
667 enum gcc_c_api_version c_version
)
669 if ((base_version
!= GCC_FE_VERSION_0
&& base_version
!= GCC_FE_VERSION_1
)
670 || (c_version
!= GCC_C_FE_VERSION_0
&& c_version
!= GCC_C_FE_VERSION_1
))
673 return new libcc1 (&vtable
, &c_vtable
);