| 1 | /* Convert symbols from GDB to GCC |
| 2 | |
| 3 | Copyright (C) 2014-2018 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 | |
| 21 | #include "defs.h" |
| 22 | #include "compile-internal.h" |
| 23 | #include "compile-c.h" |
| 24 | #include "symtab.h" |
| 25 | #include "parser-defs.h" |
| 26 | #include "block.h" |
| 27 | #include "objfiles.h" |
| 28 | #include "compile.h" |
| 29 | #include "value.h" |
| 30 | #include "exceptions.h" |
| 31 | #include "gdbtypes.h" |
| 32 | #include "dwarf2loc.h" |
| 33 | |
| 34 | \f |
| 35 | |
| 36 | /* Object of this type are stored in the compiler's symbol_err_map. */ |
| 37 | |
| 38 | struct symbol_error |
| 39 | { |
| 40 | /* The symbol. */ |
| 41 | |
| 42 | const struct symbol *sym; |
| 43 | |
| 44 | /* The error message to emit. This is malloc'd and owned by the |
| 45 | hash table. */ |
| 46 | |
| 47 | char *message; |
| 48 | }; |
| 49 | |
| 50 | /* Hash function for struct symbol_error. */ |
| 51 | |
| 52 | static hashval_t |
| 53 | hash_symbol_error (const void *a) |
| 54 | { |
| 55 | const struct symbol_error *se = (const struct symbol_error *) a; |
| 56 | |
| 57 | return htab_hash_pointer (se->sym); |
| 58 | } |
| 59 | |
| 60 | /* Equality function for struct symbol_error. */ |
| 61 | |
| 62 | static int |
| 63 | eq_symbol_error (const void *a, const void *b) |
| 64 | { |
| 65 | const struct symbol_error *sea = (const struct symbol_error *) a; |
| 66 | const struct symbol_error *seb = (const struct symbol_error *) b; |
| 67 | |
| 68 | return sea->sym == seb->sym; |
| 69 | } |
| 70 | |
| 71 | /* Deletion function for struct symbol_error. */ |
| 72 | |
| 73 | static void |
| 74 | del_symbol_error (void *a) |
| 75 | { |
| 76 | struct symbol_error *se = (struct symbol_error *) a; |
| 77 | |
| 78 | xfree (se->message); |
| 79 | xfree (se); |
| 80 | } |
| 81 | |
| 82 | /* Associate SYMBOL with some error text. */ |
| 83 | |
| 84 | static void |
| 85 | insert_symbol_error (htab_t hash, const struct symbol *sym, const char *text) |
| 86 | { |
| 87 | struct symbol_error e; |
| 88 | void **slot; |
| 89 | |
| 90 | e.sym = sym; |
| 91 | slot = htab_find_slot (hash, &e, INSERT); |
| 92 | if (*slot == NULL) |
| 93 | { |
| 94 | struct symbol_error *e = XNEW (struct symbol_error); |
| 95 | |
| 96 | e->sym = sym; |
| 97 | e->message = xstrdup (text); |
| 98 | *slot = e; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /* Emit the error message corresponding to SYM, if one exists, and |
| 103 | arrange for it not to be emitted again. */ |
| 104 | |
| 105 | static void |
| 106 | error_symbol_once (struct compile_c_instance *context, |
| 107 | const struct symbol *sym) |
| 108 | { |
| 109 | struct symbol_error search; |
| 110 | struct symbol_error *err; |
| 111 | |
| 112 | if (context->symbol_err_map == NULL) |
| 113 | return; |
| 114 | |
| 115 | search.sym = sym; |
| 116 | err = (struct symbol_error *) htab_find (context->symbol_err_map, &search); |
| 117 | if (err == NULL || err->message == NULL) |
| 118 | return; |
| 119 | |
| 120 | gdb::unique_xmalloc_ptr<char> message (err->message); |
| 121 | err->message = NULL; |
| 122 | error (_("%s"), message.get ()); |
| 123 | } |
| 124 | |
| 125 | \f |
| 126 | |
| 127 | /* Compute the name of the pointer representing a local symbol's |
| 128 | address. */ |
| 129 | |
| 130 | static gdb::unique_xmalloc_ptr<char> |
| 131 | c_symbol_substitution_name (struct symbol *sym) |
| 132 | { |
| 133 | return gdb::unique_xmalloc_ptr<char> |
| 134 | (concat ("__", SYMBOL_NATURAL_NAME (sym), "_ptr", (char *) NULL)); |
| 135 | } |
| 136 | |
| 137 | /* Convert a given symbol, SYM, to the compiler's representation. |
| 138 | CONTEXT is the compiler instance. IS_GLOBAL is true if the |
| 139 | symbol came from the global scope. IS_LOCAL is true if the symbol |
| 140 | came from a local scope. (Note that the two are not strictly |
| 141 | inverses because the symbol might have come from the static |
| 142 | scope.) */ |
| 143 | |
| 144 | static void |
| 145 | convert_one_symbol (struct compile_c_instance *context, |
| 146 | struct block_symbol sym, |
| 147 | int is_global, |
| 148 | int is_local) |
| 149 | { |
| 150 | gcc_type sym_type; |
| 151 | const char *filename = symbol_symtab (sym.symbol)->filename; |
| 152 | unsigned short line = SYMBOL_LINE (sym.symbol); |
| 153 | |
| 154 | error_symbol_once (context, sym.symbol); |
| 155 | |
| 156 | if (SYMBOL_CLASS (sym.symbol) == LOC_LABEL) |
| 157 | sym_type = 0; |
| 158 | else |
| 159 | sym_type = convert_type (context, SYMBOL_TYPE (sym.symbol)); |
| 160 | |
| 161 | if (SYMBOL_DOMAIN (sym.symbol) == STRUCT_DOMAIN) |
| 162 | { |
| 163 | /* Binding a tag, so we don't need to build a decl. */ |
| 164 | C_CTX (context)->c_ops->tagbind (C_CTX (context), |
| 165 | SYMBOL_NATURAL_NAME (sym.symbol), |
| 166 | sym_type, filename, line); |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | gcc_decl decl; |
| 171 | enum gcc_c_symbol_kind kind; |
| 172 | CORE_ADDR addr = 0; |
| 173 | gdb::unique_xmalloc_ptr<char> symbol_name; |
| 174 | |
| 175 | switch (SYMBOL_CLASS (sym.symbol)) |
| 176 | { |
| 177 | case LOC_TYPEDEF: |
| 178 | kind = GCC_C_SYMBOL_TYPEDEF; |
| 179 | break; |
| 180 | |
| 181 | case LOC_LABEL: |
| 182 | kind = GCC_C_SYMBOL_LABEL; |
| 183 | addr = SYMBOL_VALUE_ADDRESS (sym.symbol); |
| 184 | break; |
| 185 | |
| 186 | case LOC_BLOCK: |
| 187 | kind = GCC_C_SYMBOL_FUNCTION; |
| 188 | addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym.symbol)); |
| 189 | if (is_global && TYPE_GNU_IFUNC (SYMBOL_TYPE (sym.symbol))) |
| 190 | addr = gnu_ifunc_resolve_addr (target_gdbarch (), addr); |
| 191 | break; |
| 192 | |
| 193 | case LOC_CONST: |
| 194 | if (TYPE_CODE (SYMBOL_TYPE (sym.symbol)) == TYPE_CODE_ENUM) |
| 195 | { |
| 196 | /* Already handled by convert_enum. */ |
| 197 | return; |
| 198 | } |
| 199 | C_CTX (context)->c_ops->build_constant |
| 200 | (C_CTX (context), |
| 201 | sym_type, SYMBOL_NATURAL_NAME (sym.symbol), |
| 202 | SYMBOL_VALUE (sym.symbol), |
| 203 | filename, line); |
| 204 | return; |
| 205 | |
| 206 | case LOC_CONST_BYTES: |
| 207 | error (_("Unsupported LOC_CONST_BYTES for symbol \"%s\"."), |
| 208 | SYMBOL_PRINT_NAME (sym.symbol)); |
| 209 | |
| 210 | case LOC_UNDEF: |
| 211 | internal_error (__FILE__, __LINE__, _("LOC_UNDEF found for \"%s\"."), |
| 212 | SYMBOL_PRINT_NAME (sym.symbol)); |
| 213 | |
| 214 | case LOC_COMMON_BLOCK: |
| 215 | error (_("Fortran common block is unsupported for compilation " |
| 216 | "evaluaton of symbol \"%s\"."), |
| 217 | SYMBOL_PRINT_NAME (sym.symbol)); |
| 218 | |
| 219 | case LOC_OPTIMIZED_OUT: |
| 220 | error (_("Symbol \"%s\" cannot be used for compilation evaluation " |
| 221 | "as it is optimized out."), |
| 222 | SYMBOL_PRINT_NAME (sym.symbol)); |
| 223 | |
| 224 | case LOC_COMPUTED: |
| 225 | if (is_local) |
| 226 | goto substitution; |
| 227 | /* Probably TLS here. */ |
| 228 | warning (_("Symbol \"%s\" is thread-local and currently can only " |
| 229 | "be referenced from the current thread in " |
| 230 | "compiled code."), |
| 231 | SYMBOL_PRINT_NAME (sym.symbol)); |
| 232 | /* FALLTHROUGH */ |
| 233 | case LOC_UNRESOLVED: |
| 234 | /* 'symbol_name' cannot be used here as that one is used only for |
| 235 | local variables from compile_dwarf_expr_to_c. |
| 236 | Global variables can be accessed by GCC only by their address, not |
| 237 | by their name. */ |
| 238 | { |
| 239 | struct value *val; |
| 240 | struct frame_info *frame = NULL; |
| 241 | |
| 242 | if (symbol_read_needs_frame (sym.symbol)) |
| 243 | { |
| 244 | frame = get_selected_frame (NULL); |
| 245 | if (frame == NULL) |
| 246 | error (_("Symbol \"%s\" cannot be used because " |
| 247 | "there is no selected frame"), |
| 248 | SYMBOL_PRINT_NAME (sym.symbol)); |
| 249 | } |
| 250 | |
| 251 | val = read_var_value (sym.symbol, sym.block, frame); |
| 252 | if (VALUE_LVAL (val) != lval_memory) |
| 253 | error (_("Symbol \"%s\" cannot be used for compilation " |
| 254 | "evaluation as its address has not been found."), |
| 255 | SYMBOL_PRINT_NAME (sym.symbol)); |
| 256 | |
| 257 | kind = GCC_C_SYMBOL_VARIABLE; |
| 258 | addr = value_address (val); |
| 259 | } |
| 260 | break; |
| 261 | |
| 262 | |
| 263 | case LOC_REGISTER: |
| 264 | case LOC_ARG: |
| 265 | case LOC_REF_ARG: |
| 266 | case LOC_REGPARM_ADDR: |
| 267 | case LOC_LOCAL: |
| 268 | substitution: |
| 269 | kind = GCC_C_SYMBOL_VARIABLE; |
| 270 | symbol_name = c_symbol_substitution_name (sym.symbol); |
| 271 | break; |
| 272 | |
| 273 | case LOC_STATIC: |
| 274 | kind = GCC_C_SYMBOL_VARIABLE; |
| 275 | addr = SYMBOL_VALUE_ADDRESS (sym.symbol); |
| 276 | break; |
| 277 | |
| 278 | case LOC_FINAL_VALUE: |
| 279 | default: |
| 280 | gdb_assert_not_reached ("Unreachable case in convert_one_symbol."); |
| 281 | |
| 282 | } |
| 283 | |
| 284 | /* Don't emit local variable decls for a raw expression. */ |
| 285 | if (context->base.scope != COMPILE_I_RAW_SCOPE |
| 286 | || symbol_name == NULL) |
| 287 | { |
| 288 | decl = C_CTX (context)->c_ops->build_decl |
| 289 | (C_CTX (context), |
| 290 | SYMBOL_NATURAL_NAME (sym.symbol), |
| 291 | kind, |
| 292 | sym_type, |
| 293 | symbol_name.get (), addr, |
| 294 | filename, line); |
| 295 | |
| 296 | C_CTX (context)->c_ops->bind (C_CTX (context), decl, is_global); |
| 297 | } |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | /* Convert a full symbol to its gcc form. CONTEXT is the compiler to |
| 302 | use, IDENTIFIER is the name of the symbol, SYM is the symbol |
| 303 | itself, and DOMAIN is the domain which was searched. */ |
| 304 | |
| 305 | static void |
| 306 | convert_symbol_sym (struct compile_c_instance *context, const char *identifier, |
| 307 | struct block_symbol sym, domain_enum domain) |
| 308 | { |
| 309 | const struct block *static_block; |
| 310 | int is_local_symbol; |
| 311 | |
| 312 | /* If we found a symbol and it is not in the static or global |
| 313 | scope, then we should first convert any static or global scope |
| 314 | symbol of the same name. This lets this unusual case work: |
| 315 | |
| 316 | int x; // Global. |
| 317 | int func(void) |
| 318 | { |
| 319 | int x; |
| 320 | // At this spot, evaluate "extern int x; x" |
| 321 | } |
| 322 | */ |
| 323 | |
| 324 | static_block = block_static_block (sym.block); |
| 325 | /* STATIC_BLOCK is NULL if FOUND_BLOCK is the global block. */ |
| 326 | is_local_symbol = (sym.block != static_block && static_block != NULL); |
| 327 | if (is_local_symbol) |
| 328 | { |
| 329 | struct block_symbol global_sym; |
| 330 | |
| 331 | global_sym = lookup_symbol (identifier, NULL, domain, NULL); |
| 332 | /* If the outer symbol is in the static block, we ignore it, as |
| 333 | it cannot be referenced. */ |
| 334 | if (global_sym.symbol != NULL |
| 335 | && global_sym.block != block_static_block (global_sym.block)) |
| 336 | { |
| 337 | if (compile_debug) |
| 338 | fprintf_unfiltered (gdb_stdlog, |
| 339 | "gcc_convert_symbol \"%s\": global symbol\n", |
| 340 | identifier); |
| 341 | convert_one_symbol (context, global_sym, 1, 0); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | if (compile_debug) |
| 346 | fprintf_unfiltered (gdb_stdlog, |
| 347 | "gcc_convert_symbol \"%s\": local symbol\n", |
| 348 | identifier); |
| 349 | convert_one_symbol (context, sym, 0, is_local_symbol); |
| 350 | } |
| 351 | |
| 352 | /* Convert a minimal symbol to its gcc form. CONTEXT is the compiler |
| 353 | to use and BMSYM is the minimal symbol to convert. */ |
| 354 | |
| 355 | static void |
| 356 | convert_symbol_bmsym (struct compile_c_instance *context, |
| 357 | struct bound_minimal_symbol bmsym) |
| 358 | { |
| 359 | struct minimal_symbol *msym = bmsym.minsym; |
| 360 | struct objfile *objfile = bmsym.objfile; |
| 361 | struct type *type; |
| 362 | enum gcc_c_symbol_kind kind; |
| 363 | gcc_type sym_type; |
| 364 | gcc_decl decl; |
| 365 | CORE_ADDR addr; |
| 366 | |
| 367 | addr = MSYMBOL_VALUE_ADDRESS (objfile, msym); |
| 368 | |
| 369 | /* Conversion copied from write_exp_msymbol. */ |
| 370 | switch (MSYMBOL_TYPE (msym)) |
| 371 | { |
| 372 | case mst_text: |
| 373 | case mst_file_text: |
| 374 | case mst_solib_trampoline: |
| 375 | type = objfile_type (objfile)->nodebug_text_symbol; |
| 376 | kind = GCC_C_SYMBOL_FUNCTION; |
| 377 | break; |
| 378 | |
| 379 | case mst_text_gnu_ifunc: |
| 380 | type = objfile_type (objfile)->nodebug_text_gnu_ifunc_symbol; |
| 381 | kind = GCC_C_SYMBOL_FUNCTION; |
| 382 | addr = gnu_ifunc_resolve_addr (target_gdbarch (), addr); |
| 383 | break; |
| 384 | |
| 385 | case mst_data: |
| 386 | case mst_file_data: |
| 387 | case mst_bss: |
| 388 | case mst_file_bss: |
| 389 | type = objfile_type (objfile)->nodebug_data_symbol; |
| 390 | kind = GCC_C_SYMBOL_VARIABLE; |
| 391 | break; |
| 392 | |
| 393 | case mst_slot_got_plt: |
| 394 | type = objfile_type (objfile)->nodebug_got_plt_symbol; |
| 395 | kind = GCC_C_SYMBOL_FUNCTION; |
| 396 | break; |
| 397 | |
| 398 | default: |
| 399 | type = objfile_type (objfile)->nodebug_unknown_symbol; |
| 400 | kind = GCC_C_SYMBOL_VARIABLE; |
| 401 | break; |
| 402 | } |
| 403 | |
| 404 | sym_type = convert_type (context, type); |
| 405 | decl = C_CTX (context)->c_ops->build_decl (C_CTX (context), |
| 406 | MSYMBOL_NATURAL_NAME (msym), |
| 407 | kind, sym_type, NULL, addr, |
| 408 | NULL, 0); |
| 409 | C_CTX (context)->c_ops->bind (C_CTX (context), decl, 1 /* is_global */); |
| 410 | } |
| 411 | |
| 412 | /* See compile-internal.h. */ |
| 413 | |
| 414 | void |
| 415 | gcc_convert_symbol (void *datum, |
| 416 | struct gcc_c_context *gcc_context, |
| 417 | enum gcc_c_oracle_request request, |
| 418 | const char *identifier) |
| 419 | { |
| 420 | struct compile_c_instance *context = (struct compile_c_instance *) datum; |
| 421 | domain_enum domain; |
| 422 | int found = 0; |
| 423 | |
| 424 | switch (request) |
| 425 | { |
| 426 | case GCC_C_ORACLE_SYMBOL: |
| 427 | domain = VAR_DOMAIN; |
| 428 | break; |
| 429 | case GCC_C_ORACLE_TAG: |
| 430 | domain = STRUCT_DOMAIN; |
| 431 | break; |
| 432 | case GCC_C_ORACLE_LABEL: |
| 433 | domain = LABEL_DOMAIN; |
| 434 | break; |
| 435 | default: |
| 436 | gdb_assert_not_reached ("Unrecognized oracle request."); |
| 437 | } |
| 438 | |
| 439 | /* We can't allow exceptions to escape out of this callback. Safest |
| 440 | is to simply emit a gcc error. */ |
| 441 | TRY |
| 442 | { |
| 443 | struct block_symbol sym; |
| 444 | |
| 445 | sym = lookup_symbol (identifier, context->base.block, domain, NULL); |
| 446 | if (sym.symbol != NULL) |
| 447 | { |
| 448 | convert_symbol_sym (context, identifier, sym, domain); |
| 449 | found = 1; |
| 450 | } |
| 451 | else if (domain == VAR_DOMAIN) |
| 452 | { |
| 453 | struct bound_minimal_symbol bmsym; |
| 454 | |
| 455 | bmsym = lookup_minimal_symbol (identifier, NULL, NULL); |
| 456 | if (bmsym.minsym != NULL) |
| 457 | { |
| 458 | convert_symbol_bmsym (context, bmsym); |
| 459 | found = 1; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | CATCH (e, RETURN_MASK_ALL) |
| 465 | { |
| 466 | C_CTX (context)->c_ops->error (C_CTX (context), e.message); |
| 467 | } |
| 468 | END_CATCH |
| 469 | |
| 470 | if (compile_debug && !found) |
| 471 | fprintf_unfiltered (gdb_stdlog, |
| 472 | "gcc_convert_symbol \"%s\": lookup_symbol failed\n", |
| 473 | identifier); |
| 474 | return; |
| 475 | } |
| 476 | |
| 477 | /* See compile-internal.h. */ |
| 478 | |
| 479 | gcc_address |
| 480 | gcc_symbol_address (void *datum, struct gcc_c_context *gcc_context, |
| 481 | const char *identifier) |
| 482 | { |
| 483 | struct compile_c_instance *context = (struct compile_c_instance *) datum; |
| 484 | gcc_address result = 0; |
| 485 | int found = 0; |
| 486 | |
| 487 | /* We can't allow exceptions to escape out of this callback. Safest |
| 488 | is to simply emit a gcc error. */ |
| 489 | TRY |
| 490 | { |
| 491 | struct symbol *sym; |
| 492 | |
| 493 | /* We only need global functions here. */ |
| 494 | sym = lookup_symbol (identifier, NULL, VAR_DOMAIN, NULL).symbol; |
| 495 | if (sym != NULL && SYMBOL_CLASS (sym) == LOC_BLOCK) |
| 496 | { |
| 497 | if (compile_debug) |
| 498 | fprintf_unfiltered (gdb_stdlog, |
| 499 | "gcc_symbol_address \"%s\": full symbol\n", |
| 500 | identifier); |
| 501 | result = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)); |
| 502 | if (TYPE_GNU_IFUNC (SYMBOL_TYPE (sym))) |
| 503 | result = gnu_ifunc_resolve_addr (target_gdbarch (), result); |
| 504 | found = 1; |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | struct bound_minimal_symbol msym; |
| 509 | |
| 510 | msym = lookup_bound_minimal_symbol (identifier); |
| 511 | if (msym.minsym != NULL) |
| 512 | { |
| 513 | if (compile_debug) |
| 514 | fprintf_unfiltered (gdb_stdlog, |
| 515 | "gcc_symbol_address \"%s\": minimal " |
| 516 | "symbol\n", |
| 517 | identifier); |
| 518 | result = BMSYMBOL_VALUE_ADDRESS (msym); |
| 519 | if (MSYMBOL_TYPE (msym.minsym) == mst_text_gnu_ifunc) |
| 520 | result = gnu_ifunc_resolve_addr (target_gdbarch (), result); |
| 521 | found = 1; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | CATCH (e, RETURN_MASK_ERROR) |
| 527 | { |
| 528 | C_CTX (context)->c_ops->error (C_CTX (context), e.message); |
| 529 | } |
| 530 | END_CATCH |
| 531 | |
| 532 | if (compile_debug && !found) |
| 533 | fprintf_unfiltered (gdb_stdlog, |
| 534 | "gcc_symbol_address \"%s\": failed\n", |
| 535 | identifier); |
| 536 | return result; |
| 537 | } |
| 538 | |
| 539 | \f |
| 540 | |
| 541 | /* A hash function for symbol names. */ |
| 542 | |
| 543 | static hashval_t |
| 544 | hash_symname (const void *a) |
| 545 | { |
| 546 | const struct symbol *sym = (const struct symbol *) a; |
| 547 | |
| 548 | return htab_hash_string (SYMBOL_NATURAL_NAME (sym)); |
| 549 | } |
| 550 | |
| 551 | /* A comparison function for hash tables that just looks at symbol |
| 552 | names. */ |
| 553 | |
| 554 | static int |
| 555 | eq_symname (const void *a, const void *b) |
| 556 | { |
| 557 | const struct symbol *syma = (const struct symbol *) a; |
| 558 | const struct symbol *symb = (const struct symbol *) b; |
| 559 | |
| 560 | return strcmp (SYMBOL_NATURAL_NAME (syma), SYMBOL_NATURAL_NAME (symb)) == 0; |
| 561 | } |
| 562 | |
| 563 | /* If a symbol with the same name as SYM is already in HASHTAB, return |
| 564 | 1. Otherwise, add SYM to HASHTAB and return 0. */ |
| 565 | |
| 566 | static int |
| 567 | symbol_seen (htab_t hashtab, struct symbol *sym) |
| 568 | { |
| 569 | void **slot; |
| 570 | |
| 571 | slot = htab_find_slot (hashtab, sym, INSERT); |
| 572 | if (*slot != NULL) |
| 573 | return 1; |
| 574 | |
| 575 | *slot = sym; |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | /* Generate C code to compute the length of a VLA. */ |
| 580 | |
| 581 | static void |
| 582 | generate_vla_size (struct compile_c_instance *compiler, |
| 583 | string_file &stream, |
| 584 | struct gdbarch *gdbarch, |
| 585 | unsigned char *registers_used, |
| 586 | CORE_ADDR pc, |
| 587 | struct type *type, |
| 588 | struct symbol *sym) |
| 589 | { |
| 590 | type = check_typedef (type); |
| 591 | |
| 592 | if (TYPE_IS_REFERENCE (type)) |
| 593 | type = check_typedef (TYPE_TARGET_TYPE (type)); |
| 594 | |
| 595 | switch (TYPE_CODE (type)) |
| 596 | { |
| 597 | case TYPE_CODE_RANGE: |
| 598 | { |
| 599 | if (TYPE_HIGH_BOUND_KIND (type) == PROP_LOCEXPR |
| 600 | || TYPE_HIGH_BOUND_KIND (type) == PROP_LOCLIST) |
| 601 | { |
| 602 | const struct dynamic_prop *prop = &TYPE_RANGE_DATA (type)->high; |
| 603 | std::string name = c_get_range_decl_name (prop); |
| 604 | |
| 605 | dwarf2_compile_property_to_c (stream, name.c_str (), |
| 606 | gdbarch, registers_used, |
| 607 | prop, pc, sym); |
| 608 | } |
| 609 | } |
| 610 | break; |
| 611 | |
| 612 | case TYPE_CODE_ARRAY: |
| 613 | generate_vla_size (compiler, stream, gdbarch, registers_used, pc, |
| 614 | TYPE_INDEX_TYPE (type), sym); |
| 615 | generate_vla_size (compiler, stream, gdbarch, registers_used, pc, |
| 616 | TYPE_TARGET_TYPE (type), sym); |
| 617 | break; |
| 618 | |
| 619 | case TYPE_CODE_UNION: |
| 620 | case TYPE_CODE_STRUCT: |
| 621 | { |
| 622 | int i; |
| 623 | |
| 624 | for (i = 0; i < TYPE_NFIELDS (type); ++i) |
| 625 | if (!field_is_static (&TYPE_FIELD (type, i))) |
| 626 | generate_vla_size (compiler, stream, gdbarch, registers_used, pc, |
| 627 | TYPE_FIELD_TYPE (type, i), sym); |
| 628 | } |
| 629 | break; |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | /* Generate C code to compute the address of SYM. */ |
| 634 | |
| 635 | static void |
| 636 | generate_c_for_for_one_variable (struct compile_c_instance *compiler, |
| 637 | string_file &stream, |
| 638 | struct gdbarch *gdbarch, |
| 639 | unsigned char *registers_used, |
| 640 | CORE_ADDR pc, |
| 641 | struct symbol *sym) |
| 642 | { |
| 643 | |
| 644 | TRY |
| 645 | { |
| 646 | if (is_dynamic_type (SYMBOL_TYPE (sym))) |
| 647 | { |
| 648 | /* We need to emit to a temporary buffer in case an error |
| 649 | occurs in the middle. */ |
| 650 | string_file local_file; |
| 651 | |
| 652 | generate_vla_size (compiler, local_file, gdbarch, registers_used, pc, |
| 653 | SYMBOL_TYPE (sym), sym); |
| 654 | |
| 655 | stream.write (local_file.c_str (), local_file.size ()); |
| 656 | } |
| 657 | |
| 658 | if (SYMBOL_COMPUTED_OPS (sym) != NULL) |
| 659 | { |
| 660 | gdb::unique_xmalloc_ptr<char> generated_name |
| 661 | = c_symbol_substitution_name (sym); |
| 662 | /* We need to emit to a temporary buffer in case an error |
| 663 | occurs in the middle. */ |
| 664 | string_file local_file; |
| 665 | |
| 666 | SYMBOL_COMPUTED_OPS (sym)->generate_c_location (sym, local_file, |
| 667 | gdbarch, |
| 668 | registers_used, |
| 669 | pc, |
| 670 | generated_name.get ()); |
| 671 | stream.write (local_file.c_str (), local_file.size ()); |
| 672 | } |
| 673 | else |
| 674 | { |
| 675 | switch (SYMBOL_CLASS (sym)) |
| 676 | { |
| 677 | case LOC_REGISTER: |
| 678 | case LOC_ARG: |
| 679 | case LOC_REF_ARG: |
| 680 | case LOC_REGPARM_ADDR: |
| 681 | case LOC_LOCAL: |
| 682 | error (_("Local symbol unhandled when generating C code.")); |
| 683 | |
| 684 | case LOC_COMPUTED: |
| 685 | gdb_assert_not_reached (_("LOC_COMPUTED variable " |
| 686 | "missing a method.")); |
| 687 | |
| 688 | default: |
| 689 | /* Nothing to do for all other cases, as they don't represent |
| 690 | local variables. */ |
| 691 | break; |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
| 696 | CATCH (e, RETURN_MASK_ERROR) |
| 697 | { |
| 698 | if (compiler->symbol_err_map == NULL) |
| 699 | compiler->symbol_err_map = htab_create_alloc (10, |
| 700 | hash_symbol_error, |
| 701 | eq_symbol_error, |
| 702 | del_symbol_error, |
| 703 | xcalloc, |
| 704 | xfree); |
| 705 | insert_symbol_error (compiler->symbol_err_map, sym, e.message); |
| 706 | } |
| 707 | END_CATCH |
| 708 | } |
| 709 | |
| 710 | /* See compile-c.h. */ |
| 711 | |
| 712 | gdb::unique_xmalloc_ptr<unsigned char> |
| 713 | generate_c_for_variable_locations (struct compile_c_instance *compiler, |
| 714 | string_file &stream, |
| 715 | struct gdbarch *gdbarch, |
| 716 | const struct block *block, |
| 717 | CORE_ADDR pc) |
| 718 | { |
| 719 | const struct block *static_block = block_static_block (block); |
| 720 | |
| 721 | /* If we're already in the static or global block, there is nothing |
| 722 | to write. */ |
| 723 | if (static_block == NULL || block == static_block) |
| 724 | return NULL; |
| 725 | |
| 726 | gdb::unique_xmalloc_ptr<unsigned char> registers_used |
| 727 | (XCNEWVEC (unsigned char, gdbarch_num_regs (gdbarch))); |
| 728 | |
| 729 | /* Ensure that a given name is only entered once. This reflects the |
| 730 | reality of shadowing. */ |
| 731 | htab_up symhash (htab_create_alloc (1, hash_symname, eq_symname, NULL, |
| 732 | xcalloc, xfree)); |
| 733 | |
| 734 | while (1) |
| 735 | { |
| 736 | struct symbol *sym; |
| 737 | struct block_iterator iter; |
| 738 | |
| 739 | /* Iterate over symbols in this block, generating code to |
| 740 | compute the location of each local variable. */ |
| 741 | for (sym = block_iterator_first (block, &iter); |
| 742 | sym != NULL; |
| 743 | sym = block_iterator_next (&iter)) |
| 744 | { |
| 745 | if (!symbol_seen (symhash.get (), sym)) |
| 746 | generate_c_for_for_one_variable (compiler, stream, gdbarch, |
| 747 | registers_used.get (), pc, sym); |
| 748 | } |
| 749 | |
| 750 | /* If we just finished the outermost block of a function, we're |
| 751 | done. */ |
| 752 | if (BLOCK_FUNCTION (block) != NULL) |
| 753 | break; |
| 754 | block = BLOCK_SUPERBLOCK (block); |
| 755 | } |
| 756 | |
| 757 | return registers_used; |
| 758 | } |