libcc1.cc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* The library used by gdb.
  2. Copyright (C) 2014-2022 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <cc1plugin-config.h>
  16. #include <vector>
  17. #include <string>
  18. #include <sys/socket.h>
  19. #include <sys/types.h>
  20. #include <unistd.h>
  21. #include <sys/wait.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <sys/stat.h>
  25. #include <stdlib.h>
  26. #include "marshall.hh"
  27. #include "rpc.hh"
  28. #include "connection.hh"
  29. #include "names.hh"
  30. #include "callbacks.hh"
  31. #include "libiberty.h"
  32. #include "compiler-name.hh"
  33. #include "gcc-c-interface.h"
  34. #include "compiler.hh"
  35. #include "gdbctx.hh"
  36. // The C compiler context that we hand back to our caller.
  37. struct libcc1 : public cc1_plugin::base_gdb_plugin<gcc_c_context>
  38. {
  39. explicit libcc1 (const gcc_c_fe_vtable *);
  40. void add_callbacks () override;
  41. gcc_c_oracle_function *binding_oracle = nullptr;
  42. gcc_c_symbol_address_function *address_oracle = nullptr;
  43. void *oracle_datum = nullptr;
  44. };
  45. libcc1::libcc1 (const gcc_c_fe_vtable *cv)
  46. : cc1_plugin::base_gdb_plugin<gcc_c_context> ("libcc1plugin",
  47. C_COMPILER_NAME,
  48. GCC_C_FE_VERSION_1)
  49. {
  50. c_ops = cv;
  51. }
  52. // Enclose these functions in an anonymous namespace because they
  53. // shouldn't be exported, but they can't be static because they're
  54. // used as template arguments.
  55. namespace {
  56. // This is a wrapper function that is called by the RPC system and
  57. // that then forwards the call to the library user. Note that the
  58. // return value is not used; the type cannot be 'void' due to
  59. // limitations in our simple RPC.
  60. int
  61. c_call_binding_oracle (cc1_plugin::connection *conn,
  62. enum gcc_c_oracle_request request,
  63. const char *identifier)
  64. {
  65. libcc1 *self = (libcc1 *) (((libcc1::local_connection *) conn)->back_ptr);
  66. self->binding_oracle (self->oracle_datum, self, request, identifier);
  67. return 1;
  68. }
  69. // This is a wrapper function that is called by the RPC system and
  70. // that then forwards the call to the library user.
  71. gcc_address
  72. c_call_symbol_address (cc1_plugin::connection *conn, const char *identifier)
  73. {
  74. libcc1 *self = (libcc1 *) (((libcc1::local_connection *) conn)->back_ptr);
  75. return self->address_oracle (self->oracle_datum, self, identifier);
  76. }
  77. } /* anonymous namespace */
  78. static void
  79. set_callbacks (struct gcc_c_context *s,
  80. gcc_c_oracle_function *binding_oracle,
  81. gcc_c_symbol_address_function *address_oracle,
  82. void *datum)
  83. {
  84. libcc1 *self = (libcc1 *) s;
  85. self->binding_oracle = binding_oracle;
  86. self->address_oracle = address_oracle;
  87. self->oracle_datum = datum;
  88. }
  89. static const struct gcc_c_fe_vtable c_vtable =
  90. {
  91. GCC_C_FE_VERSION_0,
  92. set_callbacks,
  93. #define GCC_METHOD0(R, N) \
  94. cc1_plugin::rpc<gcc_c_context, R, cc1_plugin::c::N>,
  95. #define GCC_METHOD1(R, N, A) \
  96. cc1_plugin::rpc<gcc_c_context, R, cc1_plugin::c::N, A>,
  97. #define GCC_METHOD2(R, N, A, B) \
  98. cc1_plugin::rpc<gcc_c_context, R, cc1_plugin::c::N, A, B>,
  99. #define GCC_METHOD3(R, N, A, B, C) \
  100. cc1_plugin::rpc<gcc_c_context, R, cc1_plugin::c::N, A, B, C>,
  101. #define GCC_METHOD4(R, N, A, B, C, D) \
  102. cc1_plugin::rpc<gcc_c_context, R, cc1_plugin::c::N, A, B, C, D>,
  103. #define GCC_METHOD5(R, N, A, B, C, D, E) \
  104. cc1_plugin::rpc<gcc_c_context, R, cc1_plugin::c::N, A, B, C, D, E>,
  105. #define GCC_METHOD7(R, N, A, B, C, D, E, F, G) \
  106. cc1_plugin::rpc<gcc_c_context, R, cc1_plugin::c::N, A, B, C, D, E, F, G>,
  107. #include "gcc-c-fe.def"
  108. #undef GCC_METHOD0
  109. #undef GCC_METHOD1
  110. #undef GCC_METHOD2
  111. #undef GCC_METHOD3
  112. #undef GCC_METHOD4
  113. #undef GCC_METHOD5
  114. #undef GCC_METHOD7
  115. };
  116. void
  117. libcc1::add_callbacks ()
  118. {
  119. cc1_plugin::callback_ftype *fun
  120. = cc1_plugin::invoker<int,
  121. enum gcc_c_oracle_request,
  122. const char *>::invoke<c_call_binding_oracle>;
  123. connection->add_callback ("binding_oracle", fun);
  124. fun = cc1_plugin::invoker<gcc_address,
  125. const char *>::invoke<c_call_symbol_address>;
  126. connection->add_callback ("address_oracle", fun);
  127. }
  128. extern "C" gcc_c_fe_context_function gcc_c_fe_context;
  129. #ifdef __GNUC__
  130. #pragma GCC visibility push(default)
  131. #endif
  132. extern "C"
  133. struct gcc_c_context *
  134. gcc_c_fe_context (enum gcc_base_api_version base_version,
  135. enum gcc_c_api_version c_version)
  136. {
  137. if ((base_version != GCC_FE_VERSION_0 && base_version != GCC_FE_VERSION_1)
  138. || (c_version != GCC_C_FE_VERSION_0 && c_version != GCC_C_FE_VERSION_1))
  139. return NULL;
  140. return new libcc1 (&c_vtable);
  141. }