1 /* C language support for compilation. 2 3 Copyright (C) 2014-2017 Free Software Foundation, Inc. 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" 21 #include "compile-internal.h" 22 #include "compile.h" 23 #include "gdb-dlfcn.h" 24 #include "c-lang.h" 25 #include "macrotab.h" 26 #include "macroscope.h" 27 #include "regcache.h" 28 #include "common/function-view.h" 29 30 /* See compile-internal.h. */ 31 32 const char * 33 c_get_mode_for_size (int size) 34 { 35 const char *mode = NULL; 36 37 switch (size) 38 { 39 case 1: 40 mode = "QI"; 41 break; 42 case 2: 43 mode = "HI"; 44 break; 45 case 4: 46 mode = "SI"; 47 break; 48 case 8: 49 mode = "DI"; 50 break; 51 default: 52 internal_error (__FILE__, __LINE__, _("Invalid GCC mode size %d."), size); 53 } 54 55 return mode; 56 } 57 58 /* See compile-internal.h. */ 59 60 char * 61 c_get_range_decl_name (const struct dynamic_prop *prop) 62 { 63 return xstrprintf ("__gdb_prop_%s", host_address_to_string (prop)); 64 } 65 66 67 68 #define STR(x) #x 69 #define STRINGIFY(x) STR(x) 70 71 /* Helper function for c_get_compile_context. Open the GCC front-end 72 shared library and return the symbol specified by the current 73 GCC_C_FE_CONTEXT. */ 74 75 static gcc_c_fe_context_function * 76 load_libcc (void) 77 { 78 gcc_c_fe_context_function *func; 79 80 /* gdb_dlopen will call error () on an error, so no need to check 81 value. */ 82 gdb_dlhandle_up handle = gdb_dlopen (STRINGIFY (GCC_C_FE_LIBCC)); 83 func = (gcc_c_fe_context_function *) gdb_dlsym (handle, 84 STRINGIFY (GCC_C_FE_CONTEXT)); 85 86 if (func == NULL) 87 error (_("could not find symbol %s in library %s"), 88 STRINGIFY (GCC_C_FE_CONTEXT), 89 STRINGIFY (GCC_C_FE_LIBCC)); 90 91 /* Leave the library open. */ 92 handle.release (); 93 return func; 94 } 95 96 /* Return the compile instance associated with the current context. 97 This function calls the symbol returned from the load_libcc 98 function. This will provide the gcc_c_context. */ 99 100 struct compile_instance * 101 c_get_compile_context (void) 102 { 103 static gcc_c_fe_context_function *func; 104 105 struct gcc_c_context *context; 106 107 if (func == NULL) 108 { 109 func = load_libcc (); 110 gdb_assert (func != NULL); 111 } 112 113 context = (*func) (GCC_FE_VERSION_0, GCC_C_FE_VERSION_0); 114 if (context == NULL) 115 error (_("The loaded version of GCC does not support the required version " 116 "of the API.")); 117 118 return new_compile_instance (context); 119 } 120 121 122 123 /* Write one macro definition. */ 124 125 static void 126 print_one_macro (const char *name, const struct macro_definition *macro, 127 struct macro_source_file *source, int line, 128 ui_file *file) 129 { 130 /* Don't print command-line defines. They will be supplied another 131 way. */ 132 if (line == 0) 133 return; 134 135 /* None of -Wno-builtin-macro-redefined, #undef first 136 or plain #define of the same value would avoid a warning. */ 137 fprintf_filtered (file, "#ifndef %s\n# define %s", name, name); 138 139 if (macro->kind == macro_function_like) 140 { 141 int i; 142 143 fputs_filtered ("(", file); 144 for (i = 0; i < macro->argc; i++) 145 { 146 fputs_filtered (macro->argv[i], file); 147 if (i + 1 < macro->argc) 148 fputs_filtered (", ", file); 149 } 150 fputs_filtered (")", file); 151 } 152 153 fprintf_filtered (file, " %s\n#endif\n", macro->replacement); 154 } 155 156 /* Write macro definitions at PC to FILE. */ 157 158 static void 159 write_macro_definitions (const struct block *block, CORE_ADDR pc, 160 struct ui_file *file) 161 { 162 struct macro_scope *scope; 163 164 if (block != NULL) 165 scope = sal_macro_scope (find_pc_line (pc, 0)); 166 else 167 scope = default_macro_scope (); 168 if (scope == NULL) 169 scope = user_macro_scope (); 170 171 if (scope != NULL && scope->file != NULL && scope->file->table != NULL) 172 { 173 macro_for_each_in_scope (scope->file, scope->line, 174 [&] (const char *name, 175 const macro_definition *macro, 176 macro_source_file *source, 177 int line) 178 { 179 print_one_macro (name, macro, source, line, file); 180 }); 181 } 182 } 183 184 /* Helper function to construct a header scope for a block of code. 185 Takes a scope argument which selects the correct header to 186 insert into BUF. */ 187 188 static void 189 add_code_header (enum compile_i_scope_types type, struct ui_file *buf) 190 { 191 switch (type) 192 { 193 case COMPILE_I_SIMPLE_SCOPE: 194 fputs_unfiltered ("void " 195 GCC_FE_WRAPPER_FUNCTION 196 " (struct " 197 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG 198 " *" 199 COMPILE_I_SIMPLE_REGISTER_ARG_NAME 200 ") {\n", 201 buf); 202 break; 203 case COMPILE_I_PRINT_ADDRESS_SCOPE: 204 case COMPILE_I_PRINT_VALUE_SCOPE: 205 /* <string.h> is needed for a memcpy call below. */ 206 fputs_unfiltered ("#include <string.h>\n" 207 "void " 208 GCC_FE_WRAPPER_FUNCTION 209 " (struct " 210 COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG 211 " *" 212 COMPILE_I_SIMPLE_REGISTER_ARG_NAME 213 ", " 214 COMPILE_I_PRINT_OUT_ARG_TYPE 215 " " 216 COMPILE_I_PRINT_OUT_ARG 217 ") {\n", 218 buf); 219 break; 220 221 case COMPILE_I_RAW_SCOPE: 222 break; 223 default: 224 gdb_assert_not_reached (_("Unknown compiler scope reached.")); 225 } 226 } 227 228 /* Helper function to construct a footer scope for a block of code. 229 Takes a scope argument which selects the correct footer to 230 insert into BUF. */ 231 232 static void 233 add_code_footer (enum compile_i_scope_types type, struct ui_file *buf) 234 { 235 switch (type) 236 { 237 case COMPILE_I_SIMPLE_SCOPE: 238 case COMPILE_I_PRINT_ADDRESS_SCOPE: 239 case COMPILE_I_PRINT_VALUE_SCOPE: 240 fputs_unfiltered ("}\n", buf); 241 break; 242 case COMPILE_I_RAW_SCOPE: 243 break; 244 default: 245 gdb_assert_not_reached (_("Unknown compiler scope reached.")); 246 } 247 } 248 249 /* Generate a structure holding all the registers used by the function 250 we're generating. */ 251 252 static void 253 generate_register_struct (struct ui_file *stream, struct gdbarch *gdbarch, 254 const unsigned char *registers_used) 255 { 256 int i; 257 int seen = 0; 258 259 fputs_unfiltered ("struct " COMPILE_I_SIMPLE_REGISTER_STRUCT_TAG " {\n", 260 stream); 261 262 if (registers_used != NULL) 263 for (i = 0; i < gdbarch_num_regs (gdbarch); ++i) 264 { 265 if (registers_used[i]) 266 { 267 struct type *regtype = check_typedef (register_type (gdbarch, i)); 268 char *regname = compile_register_name_mangled (gdbarch, i); 269 struct cleanup *cleanups = make_cleanup (xfree, regname); 270 271 seen = 1; 272 273 /* You might think we could use type_print here. However, 274 target descriptions often use types with names like 275 "int64_t", which may not be defined in the inferior 276 (and in any case would not be looked up due to the 277 #pragma business). So, we take a much simpler 278 approach: for pointer- or integer-typed registers, emit 279 the field in the most direct way; and for other 280 register types (typically flags or vectors), emit a 281 maximally-aligned array of the correct size. */ 282 283 fputs_unfiltered (" ", stream); 284 switch (TYPE_CODE (regtype)) 285 { 286 case TYPE_CODE_PTR: 287 fprintf_filtered (stream, "__gdb_uintptr %s", regname); 288 break; 289 290 case TYPE_CODE_INT: 291 { 292 const char *mode 293 = c_get_mode_for_size (TYPE_LENGTH (regtype)); 294 295 if (mode != NULL) 296 { 297 if (TYPE_UNSIGNED (regtype)) 298 fputs_unfiltered ("unsigned ", stream); 299 fprintf_unfiltered (stream, 300 "int %s" 301 " __attribute__ ((__mode__(__%s__)))", 302 regname, 303 mode); 304 break; 305 } 306 } 307 308 /* Fall through. */ 309 310 default: 311 fprintf_unfiltered (stream, 312 " unsigned char %s[%d]" 313 " __attribute__((__aligned__(" 314 "__BIGGEST_ALIGNMENT__)))", 315 regname, 316 TYPE_LENGTH (regtype)); 317 } 318 fputs_unfiltered (";\n", stream); 319 320 do_cleanups (cleanups); 321 } 322 } 323 324 if (!seen) 325 fputs_unfiltered (" char " COMPILE_I_SIMPLE_REGISTER_DUMMY ";\n", 326 stream); 327 328 fputs_unfiltered ("};\n\n", stream); 329 } 330 331 /* Take the source code provided by the user with the 'compile' 332 command, and compute the additional wrapping, macro, variable and 333 register operations needed. INPUT is the source code derived from 334 the 'compile' command, GDBARCH is the architecture to use when 335 computing above, EXPR_BLOCK denotes the block relevant contextually 336 to the inferior when the expression was created, and EXPR_PC 337 indicates the value of $PC. */ 338 339 std::string 340 c_compute_program (struct compile_instance *inst, 341 const char *input, 342 struct gdbarch *gdbarch, 343 const struct block *expr_block, 344 CORE_ADDR expr_pc) 345 { 346 struct compile_c_instance *context = (struct compile_c_instance *) inst; 347 348 string_file buf; 349 string_file var_stream; 350 351 write_macro_definitions (expr_block, expr_pc, &buf); 352 353 /* Do not generate local variable information for "raw" 354 compilations. In this case we aren't emitting our own function 355 and the user's code may only refer to globals. */ 356 if (inst->scope != COMPILE_I_RAW_SCOPE) 357 { 358 unsigned char *registers_used; 359 int i; 360 361 /* Generate the code to compute variable locations, but do it 362 before generating the function header, so we can define the 363 register struct before the function body. This requires a 364 temporary stream. */ 365 registers_used = generate_c_for_variable_locations (context, 366 var_stream, gdbarch, 367 expr_block, expr_pc); 368 make_cleanup (xfree, registers_used); 369 370 buf.puts ("typedef unsigned int" 371 " __attribute__ ((__mode__(__pointer__)))" 372 " __gdb_uintptr;\n"); 373 buf.puts ("typedef int" 374 " __attribute__ ((__mode__(__pointer__)))" 375 " __gdb_intptr;\n"); 376 377 /* Iterate all log2 sizes in bytes supported by c_get_mode_for_size. */ 378 for (i = 0; i < 4; ++i) 379 { 380 const char *mode = c_get_mode_for_size (1 << i); 381 382 gdb_assert (mode != NULL); 383 buf.printf ("typedef int" 384 " __attribute__ ((__mode__(__%s__)))" 385 " __gdb_int_%s;\n", 386 mode, mode); 387 } 388 389 generate_register_struct (&buf, gdbarch, registers_used); 390 } 391 392 add_code_header (inst->scope, &buf); 393 394 if (inst->scope == COMPILE_I_SIMPLE_SCOPE 395 || inst->scope == COMPILE_I_PRINT_ADDRESS_SCOPE 396 || inst->scope == COMPILE_I_PRINT_VALUE_SCOPE) 397 { 398 buf.write (var_stream.c_str (), var_stream.size ()); 399 buf.puts ("#pragma GCC user_expression\n"); 400 } 401 402 /* The user expression has to be in its own scope, so that "extern" 403 works properly. Otherwise gcc thinks that the "extern" 404 declaration is in the same scope as the declaration provided by 405 gdb. */ 406 if (inst->scope != COMPILE_I_RAW_SCOPE) 407 buf.puts ("{\n"); 408 409 buf.puts ("#line 1 \"gdb command line\"\n"); 410 411 switch (inst->scope) 412 { 413 case COMPILE_I_PRINT_ADDRESS_SCOPE: 414 case COMPILE_I_PRINT_VALUE_SCOPE: 415 buf.printf ( 416 "__auto_type " COMPILE_I_EXPR_VAL " = %s;\n" 417 "typeof (%s) *" COMPILE_I_EXPR_PTR_TYPE ";\n" 418 "memcpy (" COMPILE_I_PRINT_OUT_ARG ", %s" COMPILE_I_EXPR_VAL ",\n" 419 "sizeof (*" COMPILE_I_EXPR_PTR_TYPE "));\n" 420 , input, input, 421 (inst->scope == COMPILE_I_PRINT_ADDRESS_SCOPE 422 ? "&" : "")); 423 break; 424 default: 425 buf.puts (input); 426 break; 427 } 428 429 buf.puts ("\n"); 430 431 /* For larger user expressions the automatic semicolons may be 432 confusing. */ 433 if (strchr (input, '\n') == NULL) 434 buf.puts (";\n"); 435 436 if (inst->scope != COMPILE_I_RAW_SCOPE) 437 buf.puts ("}\n"); 438 439 add_code_footer (inst->scope, &buf); 440 return std::move (buf.string ()); 441 } 442