| 1 | /* Load module for 'compile' command. |
| 2 | |
| 3 | Copyright (C) 2014-2015 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-object-load.h" |
| 22 | #include "compile-internal.h" |
| 23 | #include "command.h" |
| 24 | #include "objfiles.h" |
| 25 | #include "gdbcore.h" |
| 26 | #include "readline/tilde.h" |
| 27 | #include "bfdlink.h" |
| 28 | #include "gdbcmd.h" |
| 29 | #include "regcache.h" |
| 30 | #include "inferior.h" |
| 31 | #include "compile.h" |
| 32 | #include "arch-utils.h" |
| 33 | |
| 34 | /* Helper data for setup_sections. */ |
| 35 | |
| 36 | struct setup_sections_data |
| 37 | { |
| 38 | /* Size of all recent sections with matching LAST_PROT. */ |
| 39 | CORE_ADDR last_size; |
| 40 | |
| 41 | /* First section matching LAST_PROT. */ |
| 42 | asection *last_section_first; |
| 43 | |
| 44 | /* Memory protection like the prot parameter of gdbarch_infcall_mmap. */ |
| 45 | unsigned last_prot; |
| 46 | |
| 47 | /* Maximum of alignments of all sections matching LAST_PROT. |
| 48 | This value is always at least 1. This value is always a power of 2. */ |
| 49 | CORE_ADDR last_max_alignment; |
| 50 | }; |
| 51 | |
| 52 | /* Place all ABFD sections next to each other obeying all constraints. */ |
| 53 | |
| 54 | static void |
| 55 | setup_sections (bfd *abfd, asection *sect, void *data_voidp) |
| 56 | { |
| 57 | struct setup_sections_data *data = data_voidp; |
| 58 | CORE_ADDR alignment; |
| 59 | unsigned prot; |
| 60 | |
| 61 | if (sect != NULL) |
| 62 | { |
| 63 | /* It is required by later bfd_get_relocated_section_contents. */ |
| 64 | if (sect->output_section == NULL) |
| 65 | sect->output_section = sect; |
| 66 | |
| 67 | if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0) |
| 68 | return; |
| 69 | |
| 70 | /* Make the memory always readable. */ |
| 71 | prot = GDB_MMAP_PROT_READ; |
| 72 | if ((bfd_get_section_flags (abfd, sect) & SEC_READONLY) == 0) |
| 73 | prot |= GDB_MMAP_PROT_WRITE; |
| 74 | if ((bfd_get_section_flags (abfd, sect) & SEC_CODE) != 0) |
| 75 | prot |= GDB_MMAP_PROT_EXEC; |
| 76 | |
| 77 | if (compile_debug) |
| 78 | fprintf_unfiltered (gdb_stdout, |
| 79 | "module \"%s\" section \"%s\" size %s prot %u\n", |
| 80 | bfd_get_filename (abfd), |
| 81 | bfd_get_section_name (abfd, sect), |
| 82 | paddress (target_gdbarch (), |
| 83 | bfd_get_section_size (sect)), |
| 84 | prot); |
| 85 | } |
| 86 | else |
| 87 | prot = -1; |
| 88 | |
| 89 | if (sect == NULL |
| 90 | || (data->last_prot != prot && bfd_get_section_size (sect) != 0)) |
| 91 | { |
| 92 | CORE_ADDR addr; |
| 93 | asection *sect_iter; |
| 94 | |
| 95 | if (data->last_size != 0) |
| 96 | { |
| 97 | addr = gdbarch_infcall_mmap (target_gdbarch (), data->last_size, |
| 98 | data->last_prot); |
| 99 | if (compile_debug) |
| 100 | fprintf_unfiltered (gdb_stdout, |
| 101 | "allocated %s bytes at %s prot %u\n", |
| 102 | paddress (target_gdbarch (), data->last_size), |
| 103 | paddress (target_gdbarch (), addr), |
| 104 | data->last_prot); |
| 105 | } |
| 106 | else |
| 107 | addr = 0; |
| 108 | |
| 109 | if ((addr & (data->last_max_alignment - 1)) != 0) |
| 110 | error (_("Inferior compiled module address %s " |
| 111 | "is not aligned to BFD required %s."), |
| 112 | paddress (target_gdbarch (), addr), |
| 113 | paddress (target_gdbarch (), data->last_max_alignment)); |
| 114 | |
| 115 | for (sect_iter = data->last_section_first; sect_iter != sect; |
| 116 | sect_iter = sect_iter->next) |
| 117 | if ((bfd_get_section_flags (abfd, sect_iter) & SEC_ALLOC) != 0) |
| 118 | bfd_set_section_vma (abfd, sect_iter, |
| 119 | addr + bfd_get_section_vma (abfd, sect_iter)); |
| 120 | |
| 121 | data->last_size = 0; |
| 122 | data->last_section_first = sect; |
| 123 | data->last_prot = prot; |
| 124 | data->last_max_alignment = 1; |
| 125 | } |
| 126 | |
| 127 | if (sect == NULL) |
| 128 | return; |
| 129 | |
| 130 | alignment = ((CORE_ADDR) 1) << bfd_get_section_alignment (abfd, sect); |
| 131 | data->last_max_alignment = max (data->last_max_alignment, alignment); |
| 132 | |
| 133 | data->last_size = (data->last_size + alignment - 1) & -alignment; |
| 134 | |
| 135 | bfd_set_section_vma (abfd, sect, data->last_size); |
| 136 | |
| 137 | data->last_size += bfd_get_section_size (sect); |
| 138 | data->last_size = (data->last_size + alignment - 1) & -alignment; |
| 139 | } |
| 140 | |
| 141 | /* Helper for link_callbacks callbacks vector. */ |
| 142 | |
| 143 | static bfd_boolean |
| 144 | link_callbacks_multiple_definition (struct bfd_link_info *link_info, |
| 145 | struct bfd_link_hash_entry *h, bfd *nbfd, |
| 146 | asection *nsec, bfd_vma nval) |
| 147 | { |
| 148 | bfd *abfd = link_info->input_bfds; |
| 149 | |
| 150 | if (link_info->allow_multiple_definition) |
| 151 | return TRUE; |
| 152 | warning (_("Compiled module \"%s\": multiple symbol definitions: %s"), |
| 153 | bfd_get_filename (abfd), h->root.string); |
| 154 | return FALSE; |
| 155 | } |
| 156 | |
| 157 | /* Helper for link_callbacks callbacks vector. */ |
| 158 | |
| 159 | static bfd_boolean |
| 160 | link_callbacks_warning (struct bfd_link_info *link_info, const char *xwarning, |
| 161 | const char *symbol, bfd *abfd, asection *section, |
| 162 | bfd_vma address) |
| 163 | { |
| 164 | warning (_("Compiled module \"%s\" section \"%s\": warning: %s"), |
| 165 | bfd_get_filename (abfd), bfd_get_section_name (abfd, section), |
| 166 | xwarning); |
| 167 | /* Maybe permit running as a module? */ |
| 168 | return FALSE; |
| 169 | } |
| 170 | |
| 171 | /* Helper for link_callbacks callbacks vector. */ |
| 172 | |
| 173 | static bfd_boolean |
| 174 | link_callbacks_undefined_symbol (struct bfd_link_info *link_info, |
| 175 | const char *name, bfd *abfd, asection *section, |
| 176 | bfd_vma address, bfd_boolean is_fatal) |
| 177 | { |
| 178 | warning (_("Cannot resolve relocation to \"%s\" " |
| 179 | "from compiled module \"%s\" section \"%s\"."), |
| 180 | name, bfd_get_filename (abfd), bfd_get_section_name (abfd, section)); |
| 181 | return FALSE; |
| 182 | } |
| 183 | |
| 184 | /* Helper for link_callbacks callbacks vector. */ |
| 185 | |
| 186 | static bfd_boolean |
| 187 | link_callbacks_reloc_overflow (struct bfd_link_info *link_info, |
| 188 | struct bfd_link_hash_entry *entry, |
| 189 | const char *name, const char *reloc_name, |
| 190 | bfd_vma addend, bfd *abfd, asection *section, |
| 191 | bfd_vma address) |
| 192 | { |
| 193 | /* TRUE is required for intra-module relocations. */ |
| 194 | return TRUE; |
| 195 | } |
| 196 | |
| 197 | /* Helper for link_callbacks callbacks vector. */ |
| 198 | |
| 199 | static bfd_boolean |
| 200 | link_callbacks_reloc_dangerous (struct bfd_link_info *link_info, |
| 201 | const char *message, bfd *abfd, |
| 202 | asection *section, bfd_vma address) |
| 203 | { |
| 204 | warning (_("Compiled module \"%s\" section \"%s\": dangerous " |
| 205 | "relocation: %s\n"), |
| 206 | bfd_get_filename (abfd), bfd_get_section_name (abfd, section), |
| 207 | message); |
| 208 | return FALSE; |
| 209 | } |
| 210 | |
| 211 | /* Helper for link_callbacks callbacks vector. */ |
| 212 | |
| 213 | static bfd_boolean |
| 214 | link_callbacks_unattached_reloc (struct bfd_link_info *link_info, |
| 215 | const char *name, bfd *abfd, asection *section, |
| 216 | bfd_vma address) |
| 217 | { |
| 218 | warning (_("Compiled module \"%s\" section \"%s\": unattached " |
| 219 | "relocation: %s\n"), |
| 220 | bfd_get_filename (abfd), bfd_get_section_name (abfd, section), |
| 221 | name); |
| 222 | return FALSE; |
| 223 | } |
| 224 | |
| 225 | /* Helper for link_callbacks callbacks vector. */ |
| 226 | |
| 227 | static void |
| 228 | link_callbacks_einfo (const char *fmt, ...) |
| 229 | { |
| 230 | struct cleanup *cleanups; |
| 231 | va_list ap; |
| 232 | char *str; |
| 233 | |
| 234 | va_start (ap, fmt); |
| 235 | str = xstrvprintf (fmt, ap); |
| 236 | va_end (ap); |
| 237 | cleanups = make_cleanup (xfree, str); |
| 238 | |
| 239 | warning (_("Compile module: warning: %s"), str); |
| 240 | |
| 241 | do_cleanups (cleanups); |
| 242 | } |
| 243 | |
| 244 | /* Helper for bfd_get_relocated_section_contents. |
| 245 | Only these symbols are set by bfd_simple_get_relocated_section_contents |
| 246 | but bfd/ seems to use even the NULL ones without checking them first. */ |
| 247 | |
| 248 | static const struct bfd_link_callbacks link_callbacks = |
| 249 | { |
| 250 | NULL, /* add_archive_element */ |
| 251 | link_callbacks_multiple_definition, /* multiple_definition */ |
| 252 | NULL, /* multiple_common */ |
| 253 | NULL, /* add_to_set */ |
| 254 | NULL, /* constructor */ |
| 255 | link_callbacks_warning, /* warning */ |
| 256 | link_callbacks_undefined_symbol, /* undefined_symbol */ |
| 257 | link_callbacks_reloc_overflow, /* reloc_overflow */ |
| 258 | link_callbacks_reloc_dangerous, /* reloc_dangerous */ |
| 259 | link_callbacks_unattached_reloc, /* unattached_reloc */ |
| 260 | NULL, /* notice */ |
| 261 | link_callbacks_einfo, /* einfo */ |
| 262 | NULL, /* info */ |
| 263 | NULL, /* minfo */ |
| 264 | NULL, /* override_segment_assignment */ |
| 265 | }; |
| 266 | |
| 267 | struct link_hash_table_cleanup_data |
| 268 | { |
| 269 | bfd *abfd; |
| 270 | bfd *link_next; |
| 271 | }; |
| 272 | |
| 273 | /* Cleanup callback for struct bfd_link_info. */ |
| 274 | |
| 275 | static void |
| 276 | link_hash_table_free (void *d) |
| 277 | { |
| 278 | struct link_hash_table_cleanup_data *data = d; |
| 279 | |
| 280 | if (data->abfd->is_linker_output) |
| 281 | (*data->abfd->link.hash->hash_table_free) (data->abfd); |
| 282 | data->abfd->link.next = data->link_next; |
| 283 | } |
| 284 | |
| 285 | /* Relocate and store into inferior memory each section SECT of ABFD. */ |
| 286 | |
| 287 | static void |
| 288 | copy_sections (bfd *abfd, asection *sect, void *data) |
| 289 | { |
| 290 | asymbol **symbol_table = data; |
| 291 | bfd_byte *sect_data, *sect_data_got; |
| 292 | struct cleanup *cleanups; |
| 293 | struct bfd_link_info link_info; |
| 294 | struct bfd_link_order link_order; |
| 295 | CORE_ADDR inferior_addr; |
| 296 | struct link_hash_table_cleanup_data cleanup_data; |
| 297 | |
| 298 | if ((bfd_get_section_flags (abfd, sect) & (SEC_ALLOC | SEC_LOAD)) |
| 299 | != (SEC_ALLOC | SEC_LOAD)) |
| 300 | return; |
| 301 | |
| 302 | if (bfd_get_section_size (sect) == 0) |
| 303 | return; |
| 304 | |
| 305 | /* Mostly a copy of bfd_simple_get_relocated_section_contents which GDB |
| 306 | cannot use as it does not report relocations to undefined symbols. */ |
| 307 | memset (&link_info, 0, sizeof (link_info)); |
| 308 | link_info.output_bfd = abfd; |
| 309 | link_info.input_bfds = abfd; |
| 310 | link_info.input_bfds_tail = &abfd->link.next; |
| 311 | |
| 312 | cleanup_data.abfd = abfd; |
| 313 | cleanup_data.link_next = abfd->link.next; |
| 314 | |
| 315 | abfd->link.next = NULL; |
| 316 | link_info.hash = bfd_link_hash_table_create (abfd); |
| 317 | |
| 318 | cleanups = make_cleanup (link_hash_table_free, &cleanup_data); |
| 319 | link_info.callbacks = &link_callbacks; |
| 320 | |
| 321 | memset (&link_order, 0, sizeof (link_order)); |
| 322 | link_order.next = NULL; |
| 323 | link_order.type = bfd_indirect_link_order; |
| 324 | link_order.offset = 0; |
| 325 | link_order.size = bfd_get_section_size (sect); |
| 326 | link_order.u.indirect.section = sect; |
| 327 | |
| 328 | sect_data = xmalloc (bfd_get_section_size (sect)); |
| 329 | make_cleanup (xfree, sect_data); |
| 330 | |
| 331 | sect_data_got = bfd_get_relocated_section_contents (abfd, &link_info, |
| 332 | &link_order, sect_data, |
| 333 | FALSE, symbol_table); |
| 334 | |
| 335 | if (sect_data_got == NULL) |
| 336 | error (_("Cannot map compiled module \"%s\" section \"%s\": %s"), |
| 337 | bfd_get_filename (abfd), bfd_get_section_name (abfd, sect), |
| 338 | bfd_errmsg (bfd_get_error ())); |
| 339 | gdb_assert (sect_data_got == sect_data); |
| 340 | |
| 341 | inferior_addr = bfd_get_section_vma (abfd, sect); |
| 342 | if (0 != target_write_memory (inferior_addr, sect_data, |
| 343 | bfd_get_section_size (sect))) |
| 344 | error (_("Cannot write compiled module \"%s\" section \"%s\" " |
| 345 | "to inferior memory range %s-%s."), |
| 346 | bfd_get_filename (abfd), bfd_get_section_name (abfd, sect), |
| 347 | paddress (target_gdbarch (), inferior_addr), |
| 348 | paddress (target_gdbarch (), |
| 349 | inferior_addr + bfd_get_section_size (sect))); |
| 350 | |
| 351 | do_cleanups (cleanups); |
| 352 | } |
| 353 | |
| 354 | /* Fetch the type of first parameter of GCC_FE_WRAPPER_FUNCTION. |
| 355 | Return NULL if GCC_FE_WRAPPER_FUNCTION has no parameters. |
| 356 | Throw an error otherwise. */ |
| 357 | |
| 358 | static struct type * |
| 359 | get_regs_type (struct objfile *objfile) |
| 360 | { |
| 361 | struct symbol *func_sym; |
| 362 | struct type *func_type, *regsp_type, *regs_type; |
| 363 | |
| 364 | func_sym = lookup_global_symbol_from_objfile (objfile, |
| 365 | GCC_FE_WRAPPER_FUNCTION, |
| 366 | VAR_DOMAIN); |
| 367 | if (func_sym == NULL) |
| 368 | error (_("Cannot find function \"%s\" in compiled module \"%s\"."), |
| 369 | GCC_FE_WRAPPER_FUNCTION, objfile_name (objfile)); |
| 370 | |
| 371 | func_type = SYMBOL_TYPE (func_sym); |
| 372 | if (TYPE_CODE (func_type) != TYPE_CODE_FUNC) |
| 373 | error (_("Invalid type code %d of function \"%s\" in compiled " |
| 374 | "module \"%s\"."), |
| 375 | TYPE_CODE (func_type), GCC_FE_WRAPPER_FUNCTION, |
| 376 | objfile_name (objfile)); |
| 377 | |
| 378 | /* No register parameter present. */ |
| 379 | if (TYPE_NFIELDS (func_type) == 0) |
| 380 | return NULL; |
| 381 | |
| 382 | if (TYPE_NFIELDS (func_type) != 1) |
| 383 | error (_("Invalid %d parameters of function \"%s\" in compiled " |
| 384 | "module \"%s\"."), |
| 385 | TYPE_NFIELDS (func_type), GCC_FE_WRAPPER_FUNCTION, |
| 386 | objfile_name (objfile)); |
| 387 | |
| 388 | regsp_type = check_typedef (TYPE_FIELD_TYPE (func_type, 0)); |
| 389 | if (TYPE_CODE (regsp_type) != TYPE_CODE_PTR) |
| 390 | error (_("Invalid type code %d of first parameter of function \"%s\" " |
| 391 | "in compiled module \"%s\"."), |
| 392 | TYPE_CODE (regsp_type), GCC_FE_WRAPPER_FUNCTION, |
| 393 | objfile_name (objfile)); |
| 394 | |
| 395 | regs_type = check_typedef (TYPE_TARGET_TYPE (regsp_type)); |
| 396 | if (TYPE_CODE (regs_type) != TYPE_CODE_STRUCT) |
| 397 | error (_("Invalid type code %d of dereferenced first parameter " |
| 398 | "of function \"%s\" in compiled module \"%s\"."), |
| 399 | TYPE_CODE (regs_type), GCC_FE_WRAPPER_FUNCTION, |
| 400 | objfile_name (objfile)); |
| 401 | |
| 402 | return regs_type; |
| 403 | } |
| 404 | |
| 405 | /* Store all inferior registers required by REGS_TYPE to inferior memory |
| 406 | starting at inferior address REGS_BASE. */ |
| 407 | |
| 408 | static void |
| 409 | store_regs (struct type *regs_type, CORE_ADDR regs_base) |
| 410 | { |
| 411 | struct gdbarch *gdbarch = target_gdbarch (); |
| 412 | struct regcache *regcache = get_thread_regcache (inferior_ptid); |
| 413 | int fieldno; |
| 414 | |
| 415 | for (fieldno = 0; fieldno < TYPE_NFIELDS (regs_type); fieldno++) |
| 416 | { |
| 417 | const char *reg_name = TYPE_FIELD_NAME (regs_type, fieldno); |
| 418 | ULONGEST reg_bitpos = TYPE_FIELD_BITPOS (regs_type, fieldno); |
| 419 | ULONGEST reg_bitsize = TYPE_FIELD_BITSIZE (regs_type, fieldno); |
| 420 | ULONGEST reg_offset; |
| 421 | struct type *reg_type = check_typedef (TYPE_FIELD_TYPE (regs_type, |
| 422 | fieldno)); |
| 423 | ULONGEST reg_size = TYPE_LENGTH (reg_type); |
| 424 | int regnum; |
| 425 | struct value *regval; |
| 426 | CORE_ADDR inferior_addr; |
| 427 | |
| 428 | if (strcmp (reg_name, COMPILE_I_SIMPLE_REGISTER_DUMMY) == 0) |
| 429 | continue; |
| 430 | |
| 431 | if ((reg_bitpos % 8) != 0 || reg_bitsize != 0) |
| 432 | error (_("Invalid register \"%s\" position %s bits or size %s bits"), |
| 433 | reg_name, pulongest (reg_bitpos), pulongest (reg_bitsize)); |
| 434 | reg_offset = reg_bitpos / 8; |
| 435 | |
| 436 | if (TYPE_CODE (reg_type) != TYPE_CODE_INT |
| 437 | && TYPE_CODE (reg_type) != TYPE_CODE_PTR) |
| 438 | error (_("Invalid register \"%s\" type code %d"), reg_name, |
| 439 | TYPE_CODE (reg_type)); |
| 440 | |
| 441 | regnum = compile_register_name_demangle (gdbarch, reg_name); |
| 442 | |
| 443 | regval = value_from_register (reg_type, regnum, get_current_frame ()); |
| 444 | if (value_optimized_out (regval)) |
| 445 | error (_("Register \"%s\" is optimized out."), reg_name); |
| 446 | if (!value_entirely_available (regval)) |
| 447 | error (_("Register \"%s\" is not available."), reg_name); |
| 448 | |
| 449 | inferior_addr = regs_base + reg_offset; |
| 450 | if (0 != target_write_memory (inferior_addr, value_contents (regval), |
| 451 | reg_size)) |
| 452 | error (_("Cannot write register \"%s\" to inferior memory at %s."), |
| 453 | reg_name, paddress (gdbarch, inferior_addr)); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | /* Load OBJECT_FILE into inferior memory. Throw an error otherwise. |
| 458 | Caller must fully dispose the return value by calling compile_object_run. |
| 459 | SOURCE_FILE's copy is stored into the returned object. |
| 460 | Caller should free both OBJECT_FILE and SOURCE_FILE immediatelly after this |
| 461 | function returns. */ |
| 462 | |
| 463 | struct compile_module * |
| 464 | compile_object_load (const char *object_file, const char *source_file) |
| 465 | { |
| 466 | struct cleanup *cleanups, *cleanups_free_objfile; |
| 467 | bfd *abfd; |
| 468 | struct setup_sections_data setup_sections_data; |
| 469 | CORE_ADDR addr, func_addr, regs_addr; |
| 470 | struct bound_minimal_symbol bmsym; |
| 471 | long storage_needed; |
| 472 | asymbol **symbol_table, **symp; |
| 473 | long number_of_symbols, missing_symbols; |
| 474 | struct type *dptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr; |
| 475 | unsigned dptr_type_len = TYPE_LENGTH (dptr_type); |
| 476 | struct compile_module *retval; |
| 477 | struct type *regs_type; |
| 478 | char *filename, **matching; |
| 479 | struct objfile *objfile; |
| 480 | |
| 481 | filename = tilde_expand (object_file); |
| 482 | cleanups = make_cleanup (xfree, filename); |
| 483 | |
| 484 | abfd = gdb_bfd_open (filename, gnutarget, -1); |
| 485 | if (abfd == NULL) |
| 486 | error (_("\"%s\": could not open as compiled module: %s"), |
| 487 | filename, bfd_errmsg (bfd_get_error ())); |
| 488 | make_cleanup_bfd_unref (abfd); |
| 489 | |
| 490 | if (!bfd_check_format_matches (abfd, bfd_object, &matching)) |
| 491 | error (_("\"%s\": not in loadable format: %s"), |
| 492 | filename, gdb_bfd_errmsg (bfd_get_error (), matching)); |
| 493 | |
| 494 | if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC)) != 0) |
| 495 | error (_("\"%s\": not in object format."), filename); |
| 496 | |
| 497 | setup_sections_data.last_size = 0; |
| 498 | setup_sections_data.last_section_first = abfd->sections; |
| 499 | setup_sections_data.last_prot = -1; |
| 500 | setup_sections_data.last_max_alignment = 1; |
| 501 | bfd_map_over_sections (abfd, setup_sections, &setup_sections_data); |
| 502 | setup_sections (abfd, NULL, &setup_sections_data); |
| 503 | |
| 504 | storage_needed = bfd_get_symtab_upper_bound (abfd); |
| 505 | if (storage_needed < 0) |
| 506 | error (_("Cannot read symbols of compiled module \"%s\": %s"), |
| 507 | filename, bfd_errmsg (bfd_get_error ())); |
| 508 | |
| 509 | /* SYMFILE_VERBOSE is not passed even if FROM_TTY, user is not interested in |
| 510 | "Reading symbols from ..." message for automatically generated file. */ |
| 511 | objfile = symbol_file_add_from_bfd (abfd, filename, 0, NULL, 0, NULL); |
| 512 | cleanups_free_objfile = make_cleanup_free_objfile (objfile); |
| 513 | |
| 514 | bmsym = lookup_minimal_symbol_text (GCC_FE_WRAPPER_FUNCTION, objfile); |
| 515 | if (bmsym.minsym == NULL || MSYMBOL_TYPE (bmsym.minsym) == mst_file_text) |
| 516 | error (_("Could not find symbol \"%s\" of compiled module \"%s\"."), |
| 517 | GCC_FE_WRAPPER_FUNCTION, filename); |
| 518 | func_addr = BMSYMBOL_VALUE_ADDRESS (bmsym); |
| 519 | |
| 520 | /* The memory may be later needed |
| 521 | by bfd_generic_get_relocated_section_contents |
| 522 | called from default_symfile_relocate. */ |
| 523 | symbol_table = obstack_alloc (&objfile->objfile_obstack, storage_needed); |
| 524 | number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table); |
| 525 | if (number_of_symbols < 0) |
| 526 | error (_("Cannot parse symbols of compiled module \"%s\": %s"), |
| 527 | filename, bfd_errmsg (bfd_get_error ())); |
| 528 | |
| 529 | missing_symbols = 0; |
| 530 | for (symp = symbol_table; symp < symbol_table + number_of_symbols; symp++) |
| 531 | { |
| 532 | asymbol *sym = *symp; |
| 533 | |
| 534 | if (sym->flags != 0) |
| 535 | continue; |
| 536 | if (compile_debug) |
| 537 | fprintf_unfiltered (gdb_stdout, |
| 538 | "lookup undefined ELF symbol \"%s\"\n", |
| 539 | sym->name); |
| 540 | sym->flags = BSF_GLOBAL; |
| 541 | sym->section = bfd_abs_section_ptr; |
| 542 | if (strcmp (sym->name, "_GLOBAL_OFFSET_TABLE_") == 0) |
| 543 | { |
| 544 | sym->value = 0; |
| 545 | continue; |
| 546 | } |
| 547 | bmsym = lookup_minimal_symbol (sym->name, NULL, NULL); |
| 548 | switch (bmsym.minsym == NULL |
| 549 | ? mst_unknown : MSYMBOL_TYPE (bmsym.minsym)) |
| 550 | { |
| 551 | case mst_text: |
| 552 | sym->value = BMSYMBOL_VALUE_ADDRESS (bmsym); |
| 553 | break; |
| 554 | default: |
| 555 | warning (_("Could not find symbol \"%s\" " |
| 556 | "for compiled module \"%s\"."), |
| 557 | sym->name, filename); |
| 558 | missing_symbols++; |
| 559 | } |
| 560 | } |
| 561 | if (missing_symbols) |
| 562 | error (_("%ld symbols were missing, cannot continue."), missing_symbols); |
| 563 | |
| 564 | bfd_map_over_sections (abfd, copy_sections, symbol_table); |
| 565 | |
| 566 | regs_type = get_regs_type (objfile); |
| 567 | if (regs_type == NULL) |
| 568 | regs_addr = 0; |
| 569 | else |
| 570 | { |
| 571 | /* Use read-only non-executable memory protection. */ |
| 572 | regs_addr = gdbarch_infcall_mmap (target_gdbarch (), |
| 573 | TYPE_LENGTH (regs_type), |
| 574 | GDB_MMAP_PROT_READ); |
| 575 | gdb_assert (regs_addr != 0); |
| 576 | store_regs (regs_type, regs_addr); |
| 577 | } |
| 578 | |
| 579 | discard_cleanups (cleanups_free_objfile); |
| 580 | do_cleanups (cleanups); |
| 581 | |
| 582 | retval = xmalloc (sizeof (*retval)); |
| 583 | retval->objfile = objfile; |
| 584 | retval->source_file = xstrdup (source_file); |
| 585 | retval->func_addr = func_addr; |
| 586 | retval->regs_addr = regs_addr; |
| 587 | return retval; |
| 588 | } |