ed5ef88caa4c8da98cac7bf0d380049a9d9da113
[deliverable/binutils-gdb.git] / gdb / compile / compile-object-load.c
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 "block.h"
33 #include "arch-utils.h"
34
35 /* Helper data for setup_sections. */
36
37 struct setup_sections_data
38 {
39 /* Size of all recent sections with matching LAST_PROT. */
40 CORE_ADDR last_size;
41
42 /* First section matching LAST_PROT. */
43 asection *last_section_first;
44
45 /* Memory protection like the prot parameter of gdbarch_infcall_mmap. */
46 unsigned last_prot;
47
48 /* Maximum of alignments of all sections matching LAST_PROT.
49 This value is always at least 1. This value is always a power of 2. */
50 CORE_ADDR last_max_alignment;
51 };
52
53 /* Place all ABFD sections next to each other obeying all constraints. */
54
55 static void
56 setup_sections (bfd *abfd, asection *sect, void *data_voidp)
57 {
58 struct setup_sections_data *data = data_voidp;
59 CORE_ADDR alignment;
60 unsigned prot;
61
62 if (sect != NULL)
63 {
64 /* It is required by later bfd_get_relocated_section_contents. */
65 if (sect->output_section == NULL)
66 sect->output_section = sect;
67
68 if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0)
69 return;
70
71 /* Make the memory always readable. */
72 prot = GDB_MMAP_PROT_READ;
73 if ((bfd_get_section_flags (abfd, sect) & SEC_READONLY) == 0)
74 prot |= GDB_MMAP_PROT_WRITE;
75 if ((bfd_get_section_flags (abfd, sect) & SEC_CODE) != 0)
76 prot |= GDB_MMAP_PROT_EXEC;
77
78 if (compile_debug)
79 fprintf_unfiltered (gdb_stdout,
80 "module \"%s\" section \"%s\" size %s prot %u\n",
81 bfd_get_filename (abfd),
82 bfd_get_section_name (abfd, sect),
83 paddress (target_gdbarch (),
84 bfd_get_section_size (sect)),
85 prot);
86 }
87 else
88 prot = -1;
89
90 if (sect == NULL
91 || (data->last_prot != prot && bfd_get_section_size (sect) != 0))
92 {
93 CORE_ADDR addr;
94 asection *sect_iter;
95
96 if (data->last_size != 0)
97 {
98 addr = gdbarch_infcall_mmap (target_gdbarch (), data->last_size,
99 data->last_prot);
100 if (compile_debug)
101 fprintf_unfiltered (gdb_stdout,
102 "allocated %s bytes at %s prot %u\n",
103 paddress (target_gdbarch (), data->last_size),
104 paddress (target_gdbarch (), addr),
105 data->last_prot);
106 }
107 else
108 addr = 0;
109
110 if ((addr & (data->last_max_alignment - 1)) != 0)
111 error (_("Inferior compiled module address %s "
112 "is not aligned to BFD required %s."),
113 paddress (target_gdbarch (), addr),
114 paddress (target_gdbarch (), data->last_max_alignment));
115
116 for (sect_iter = data->last_section_first; sect_iter != sect;
117 sect_iter = sect_iter->next)
118 if ((bfd_get_section_flags (abfd, sect_iter) & SEC_ALLOC) != 0)
119 bfd_set_section_vma (abfd, sect_iter,
120 addr + bfd_get_section_vma (abfd, sect_iter));
121
122 data->last_size = 0;
123 data->last_section_first = sect;
124 data->last_prot = prot;
125 data->last_max_alignment = 1;
126 }
127
128 if (sect == NULL)
129 return;
130
131 alignment = ((CORE_ADDR) 1) << bfd_get_section_alignment (abfd, sect);
132 data->last_max_alignment = max (data->last_max_alignment, alignment);
133
134 data->last_size = (data->last_size + alignment - 1) & -alignment;
135
136 bfd_set_section_vma (abfd, sect, data->last_size);
137
138 data->last_size += bfd_get_section_size (sect);
139 data->last_size = (data->last_size + alignment - 1) & -alignment;
140 }
141
142 /* Helper for link_callbacks callbacks vector. */
143
144 static bfd_boolean
145 link_callbacks_multiple_definition (struct bfd_link_info *link_info,
146 struct bfd_link_hash_entry *h, bfd *nbfd,
147 asection *nsec, bfd_vma nval)
148 {
149 bfd *abfd = link_info->input_bfds;
150
151 if (link_info->allow_multiple_definition)
152 return TRUE;
153 warning (_("Compiled module \"%s\": multiple symbol definitions: %s"),
154 bfd_get_filename (abfd), h->root.string);
155 return FALSE;
156 }
157
158 /* Helper for link_callbacks callbacks vector. */
159
160 static bfd_boolean
161 link_callbacks_warning (struct bfd_link_info *link_info, const char *xwarning,
162 const char *symbol, bfd *abfd, asection *section,
163 bfd_vma address)
164 {
165 warning (_("Compiled module \"%s\" section \"%s\": warning: %s"),
166 bfd_get_filename (abfd), bfd_get_section_name (abfd, section),
167 xwarning);
168 /* Maybe permit running as a module? */
169 return FALSE;
170 }
171
172 /* Helper for link_callbacks callbacks vector. */
173
174 static bfd_boolean
175 link_callbacks_undefined_symbol (struct bfd_link_info *link_info,
176 const char *name, bfd *abfd, asection *section,
177 bfd_vma address, bfd_boolean is_fatal)
178 {
179 warning (_("Cannot resolve relocation to \"%s\" "
180 "from compiled module \"%s\" section \"%s\"."),
181 name, bfd_get_filename (abfd), bfd_get_section_name (abfd, section));
182 return FALSE;
183 }
184
185 /* Helper for link_callbacks callbacks vector. */
186
187 static bfd_boolean
188 link_callbacks_reloc_overflow (struct bfd_link_info *link_info,
189 struct bfd_link_hash_entry *entry,
190 const char *name, const char *reloc_name,
191 bfd_vma addend, bfd *abfd, asection *section,
192 bfd_vma address)
193 {
194 /* TRUE is required for intra-module relocations. */
195 return TRUE;
196 }
197
198 /* Helper for link_callbacks callbacks vector. */
199
200 static bfd_boolean
201 link_callbacks_reloc_dangerous (struct bfd_link_info *link_info,
202 const char *message, bfd *abfd,
203 asection *section, bfd_vma address)
204 {
205 warning (_("Compiled module \"%s\" section \"%s\": dangerous "
206 "relocation: %s\n"),
207 bfd_get_filename (abfd), bfd_get_section_name (abfd, section),
208 message);
209 return FALSE;
210 }
211
212 /* Helper for link_callbacks callbacks vector. */
213
214 static bfd_boolean
215 link_callbacks_unattached_reloc (struct bfd_link_info *link_info,
216 const char *name, bfd *abfd, asection *section,
217 bfd_vma address)
218 {
219 warning (_("Compiled module \"%s\" section \"%s\": unattached "
220 "relocation: %s\n"),
221 bfd_get_filename (abfd), bfd_get_section_name (abfd, section),
222 name);
223 return FALSE;
224 }
225
226 /* Helper for link_callbacks callbacks vector. */
227
228 static void link_callbacks_einfo (const char *fmt, ...)
229 ATTRIBUTE_PRINTF (1, 2);
230
231 static void
232 link_callbacks_einfo (const char *fmt, ...)
233 {
234 struct cleanup *cleanups;
235 va_list ap;
236 char *str;
237
238 va_start (ap, fmt);
239 str = xstrvprintf (fmt, ap);
240 va_end (ap);
241 cleanups = make_cleanup (xfree, str);
242
243 warning (_("Compile module: warning: %s"), str);
244
245 do_cleanups (cleanups);
246 }
247
248 /* Helper for bfd_get_relocated_section_contents.
249 Only these symbols are set by bfd_simple_get_relocated_section_contents
250 but bfd/ seems to use even the NULL ones without checking them first. */
251
252 static const struct bfd_link_callbacks link_callbacks =
253 {
254 NULL, /* add_archive_element */
255 link_callbacks_multiple_definition, /* multiple_definition */
256 NULL, /* multiple_common */
257 NULL, /* add_to_set */
258 NULL, /* constructor */
259 link_callbacks_warning, /* warning */
260 link_callbacks_undefined_symbol, /* undefined_symbol */
261 link_callbacks_reloc_overflow, /* reloc_overflow */
262 link_callbacks_reloc_dangerous, /* reloc_dangerous */
263 link_callbacks_unattached_reloc, /* unattached_reloc */
264 NULL, /* notice */
265 link_callbacks_einfo, /* einfo */
266 NULL, /* info */
267 NULL, /* minfo */
268 NULL, /* override_segment_assignment */
269 };
270
271 struct link_hash_table_cleanup_data
272 {
273 bfd *abfd;
274 bfd *link_next;
275 };
276
277 /* Cleanup callback for struct bfd_link_info. */
278
279 static void
280 link_hash_table_free (void *d)
281 {
282 struct link_hash_table_cleanup_data *data = d;
283
284 if (data->abfd->is_linker_output)
285 (*data->abfd->link.hash->hash_table_free) (data->abfd);
286 data->abfd->link.next = data->link_next;
287 }
288
289 /* Relocate and store into inferior memory each section SECT of ABFD. */
290
291 static void
292 copy_sections (bfd *abfd, asection *sect, void *data)
293 {
294 asymbol **symbol_table = data;
295 bfd_byte *sect_data, *sect_data_got;
296 struct cleanup *cleanups;
297 struct bfd_link_info link_info;
298 struct bfd_link_order link_order;
299 CORE_ADDR inferior_addr;
300 struct link_hash_table_cleanup_data cleanup_data;
301
302 if ((bfd_get_section_flags (abfd, sect) & (SEC_ALLOC | SEC_LOAD))
303 != (SEC_ALLOC | SEC_LOAD))
304 return;
305
306 if (bfd_get_section_size (sect) == 0)
307 return;
308
309 /* Mostly a copy of bfd_simple_get_relocated_section_contents which GDB
310 cannot use as it does not report relocations to undefined symbols. */
311 memset (&link_info, 0, sizeof (link_info));
312 link_info.output_bfd = abfd;
313 link_info.input_bfds = abfd;
314 link_info.input_bfds_tail = &abfd->link.next;
315
316 cleanup_data.abfd = abfd;
317 cleanup_data.link_next = abfd->link.next;
318
319 abfd->link.next = NULL;
320 link_info.hash = bfd_link_hash_table_create (abfd);
321
322 cleanups = make_cleanup (link_hash_table_free, &cleanup_data);
323 link_info.callbacks = &link_callbacks;
324
325 memset (&link_order, 0, sizeof (link_order));
326 link_order.next = NULL;
327 link_order.type = bfd_indirect_link_order;
328 link_order.offset = 0;
329 link_order.size = bfd_get_section_size (sect);
330 link_order.u.indirect.section = sect;
331
332 sect_data = xmalloc (bfd_get_section_size (sect));
333 make_cleanup (xfree, sect_data);
334
335 sect_data_got = bfd_get_relocated_section_contents (abfd, &link_info,
336 &link_order, sect_data,
337 FALSE, symbol_table);
338
339 if (sect_data_got == NULL)
340 error (_("Cannot map compiled module \"%s\" section \"%s\": %s"),
341 bfd_get_filename (abfd), bfd_get_section_name (abfd, sect),
342 bfd_errmsg (bfd_get_error ()));
343 gdb_assert (sect_data_got == sect_data);
344
345 inferior_addr = bfd_get_section_vma (abfd, sect);
346 if (0 != target_write_memory (inferior_addr, sect_data,
347 bfd_get_section_size (sect)))
348 error (_("Cannot write compiled module \"%s\" section \"%s\" "
349 "to inferior memory range %s-%s."),
350 bfd_get_filename (abfd), bfd_get_section_name (abfd, sect),
351 paddress (target_gdbarch (), inferior_addr),
352 paddress (target_gdbarch (),
353 inferior_addr + bfd_get_section_size (sect)));
354
355 do_cleanups (cleanups);
356 }
357
358 /* Fetch the type of COMPILE_I_EXPR_PTR_TYPE and COMPILE_I_EXPR_VAL
359 symbols in OBJFILE so we can calculate how much memory to allocate
360 for the out parameter. This avoids needing a malloc in the generated
361 code. Throw an error if anything fails.
362 GDB first tries to compile the code with COMPILE_I_PRINT_ADDRESS_SCOPE.
363 If it finds user tries to print an array type this function returns
364 NULL. Caller will then regenerate the code with
365 COMPILE_I_PRINT_VALUE_SCOPE, recompiles it again and finally runs it.
366 This is because __auto_type array-to-pointer type conversion of
367 COMPILE_I_EXPR_VAL which gets detected by COMPILE_I_EXPR_PTR_TYPE
368 preserving the array type. */
369
370 static struct type *
371 get_out_value_type (struct symbol *func_sym, struct objfile *objfile,
372 enum compile_i_scope_types scope)
373 {
374 struct symbol *gdb_ptr_type_sym, *gdb_val_sym;
375 struct type *gdb_ptr_type, *gdb_type_from_ptr, *gdb_type;
376 const struct block *block;
377 const struct blockvector *bv;
378 int nblocks = 0;
379 int block_loop = 0;
380
381 bv = SYMTAB_BLOCKVECTOR (func_sym->owner.symtab);
382 nblocks = BLOCKVECTOR_NBLOCKS (bv);
383
384 gdb_ptr_type_sym = NULL;
385 for (block_loop = 0; block_loop < nblocks; block_loop++)
386 {
387 struct symbol *function;
388 const struct block *function_block;
389
390 block = BLOCKVECTOR_BLOCK (bv, block_loop);
391 if (BLOCK_FUNCTION (block) != NULL)
392 continue;
393 gdb_val_sym = block_lookup_symbol (block, COMPILE_I_EXPR_VAL, VAR_DOMAIN);
394 if (gdb_val_sym == NULL)
395 continue;
396
397 function_block = block;
398 while (function_block != BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)
399 && function_block != BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
400 {
401 function_block = BLOCK_SUPERBLOCK (function_block);
402 function = BLOCK_FUNCTION (function_block);
403 if (function != NULL)
404 break;
405 }
406 if (function != NULL
407 && (BLOCK_SUPERBLOCK (function_block)
408 == BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK))
409 && (strcmp (SYMBOL_LINKAGE_NAME (function), GCC_FE_WRAPPER_FUNCTION)
410 == 0))
411 break;
412 }
413 if (block_loop == nblocks)
414 error (_("No \"%s\" symbol found"), COMPILE_I_EXPR_PTR_TYPE);
415
416 gdb_type = SYMBOL_TYPE (gdb_val_sym);
417 CHECK_TYPEDEF (gdb_type);
418
419 gdb_ptr_type_sym = block_lookup_symbol (block, COMPILE_I_EXPR_PTR_TYPE,
420 VAR_DOMAIN);
421 if (gdb_ptr_type_sym == NULL)
422 error (_("No \"%s\" symbol found"), COMPILE_I_EXPR_PTR_TYPE);
423 gdb_ptr_type = SYMBOL_TYPE (gdb_ptr_type_sym);
424 CHECK_TYPEDEF (gdb_ptr_type);
425 if (TYPE_CODE (gdb_ptr_type) != TYPE_CODE_PTR)
426 error (_("Type of \"%s\" is not a pointer"), COMPILE_I_EXPR_PTR_TYPE);
427 gdb_type_from_ptr = TYPE_TARGET_TYPE (gdb_ptr_type);
428
429 if (types_deeply_equal (gdb_type, gdb_type_from_ptr))
430 {
431 if (scope != COMPILE_I_PRINT_ADDRESS_SCOPE)
432 error (_("Expected address scope in compiled module \"%s\"."),
433 objfile_name (objfile));
434 return gdb_type;
435 }
436
437 if (TYPE_CODE (gdb_type) != TYPE_CODE_PTR)
438 error (_("Invalid type code %d of symbol \"%s\" "
439 "in compiled module \"%s\"."),
440 TYPE_CODE (gdb_type_from_ptr), COMPILE_I_EXPR_VAL,
441 objfile_name (objfile));
442
443 switch (TYPE_CODE (gdb_type_from_ptr))
444 {
445 case TYPE_CODE_ARRAY:
446 gdb_type_from_ptr = TYPE_TARGET_TYPE (gdb_type_from_ptr);
447 break;
448 case TYPE_CODE_FUNC:
449 break;
450 default:
451 error (_("Invalid type code %d of symbol \"%s\" "
452 "in compiled module \"%s\"."),
453 TYPE_CODE (gdb_type_from_ptr), COMPILE_I_EXPR_PTR_TYPE,
454 objfile_name (objfile));
455 }
456 if (!types_deeply_equal (gdb_type_from_ptr,
457 TYPE_TARGET_TYPE (gdb_type)))
458 error (_("Referenced types do not match for symbols \"%s\" and \"%s\" "
459 "in compiled module \"%s\"."),
460 COMPILE_I_EXPR_PTR_TYPE, COMPILE_I_EXPR_VAL,
461 objfile_name (objfile));
462 if (scope == COMPILE_I_PRINT_ADDRESS_SCOPE)
463 return NULL;
464 return gdb_type_from_ptr;
465 }
466
467 /* Fetch the type of first parameter of FUNC_SYM.
468 Return NULL if FUNC_SYM has no parameters. Throw an error otherwise. */
469
470 static struct type *
471 get_regs_type (struct symbol *func_sym, struct objfile *objfile)
472 {
473 struct type *func_type = SYMBOL_TYPE (func_sym);
474 struct type *regsp_type, *regs_type;
475
476 /* No register parameter present. */
477 if (TYPE_NFIELDS (func_type) == 0)
478 return NULL;
479
480 regsp_type = check_typedef (TYPE_FIELD_TYPE (func_type, 0));
481 if (TYPE_CODE (regsp_type) != TYPE_CODE_PTR)
482 error (_("Invalid type code %d of first parameter of function \"%s\" "
483 "in compiled module \"%s\"."),
484 TYPE_CODE (regsp_type), GCC_FE_WRAPPER_FUNCTION,
485 objfile_name (objfile));
486
487 regs_type = check_typedef (TYPE_TARGET_TYPE (regsp_type));
488 if (TYPE_CODE (regs_type) != TYPE_CODE_STRUCT)
489 error (_("Invalid type code %d of dereferenced first parameter "
490 "of function \"%s\" in compiled module \"%s\"."),
491 TYPE_CODE (regs_type), GCC_FE_WRAPPER_FUNCTION,
492 objfile_name (objfile));
493
494 return regs_type;
495 }
496
497 /* Store all inferior registers required by REGS_TYPE to inferior memory
498 starting at inferior address REGS_BASE. */
499
500 static void
501 store_regs (struct type *regs_type, CORE_ADDR regs_base)
502 {
503 struct gdbarch *gdbarch = target_gdbarch ();
504 struct regcache *regcache = get_thread_regcache (inferior_ptid);
505 int fieldno;
506
507 for (fieldno = 0; fieldno < TYPE_NFIELDS (regs_type); fieldno++)
508 {
509 const char *reg_name = TYPE_FIELD_NAME (regs_type, fieldno);
510 ULONGEST reg_bitpos = TYPE_FIELD_BITPOS (regs_type, fieldno);
511 ULONGEST reg_bitsize = TYPE_FIELD_BITSIZE (regs_type, fieldno);
512 ULONGEST reg_offset;
513 struct type *reg_type = check_typedef (TYPE_FIELD_TYPE (regs_type,
514 fieldno));
515 ULONGEST reg_size = TYPE_LENGTH (reg_type);
516 int regnum;
517 struct value *regval;
518 CORE_ADDR inferior_addr;
519
520 if (strcmp (reg_name, COMPILE_I_SIMPLE_REGISTER_DUMMY) == 0)
521 continue;
522
523 if ((reg_bitpos % 8) != 0 || reg_bitsize != 0)
524 error (_("Invalid register \"%s\" position %s bits or size %s bits"),
525 reg_name, pulongest (reg_bitpos), pulongest (reg_bitsize));
526 reg_offset = reg_bitpos / 8;
527
528 if (TYPE_CODE (reg_type) != TYPE_CODE_INT
529 && TYPE_CODE (reg_type) != TYPE_CODE_PTR)
530 error (_("Invalid register \"%s\" type code %d"), reg_name,
531 TYPE_CODE (reg_type));
532
533 regnum = compile_register_name_demangle (gdbarch, reg_name);
534
535 regval = value_from_register (reg_type, regnum, get_current_frame ());
536 if (value_optimized_out (regval))
537 error (_("Register \"%s\" is optimized out."), reg_name);
538 if (!value_entirely_available (regval))
539 error (_("Register \"%s\" is not available."), reg_name);
540
541 inferior_addr = regs_base + reg_offset;
542 if (0 != target_write_memory (inferior_addr, value_contents (regval),
543 reg_size))
544 error (_("Cannot write register \"%s\" to inferior memory at %s."),
545 reg_name, paddress (gdbarch, inferior_addr));
546 }
547 }
548
549 /* Load OBJECT_FILE into inferior memory. Throw an error otherwise.
550 Caller must fully dispose the return value by calling compile_object_run.
551 SOURCE_FILE's copy is stored into the returned object.
552 Caller should free both OBJECT_FILE and SOURCE_FILE immediatelly after this
553 function returns.
554 Function returns NULL only for COMPILE_I_PRINT_ADDRESS_SCOPE when
555 COMPILE_I_PRINT_VALUE_SCOPE should have been used instead. */
556
557 struct compile_module *
558 compile_object_load (const char *object_file, const char *source_file,
559 enum compile_i_scope_types scope, void *scope_data)
560 {
561 struct cleanup *cleanups, *cleanups_free_objfile;
562 bfd *abfd;
563 struct setup_sections_data setup_sections_data;
564 CORE_ADDR addr, regs_addr, out_value_addr = 0;
565 struct symbol *func_sym;
566 struct type *func_type;
567 struct bound_minimal_symbol bmsym;
568 long storage_needed;
569 asymbol **symbol_table, **symp;
570 long number_of_symbols, missing_symbols;
571 struct type *dptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
572 unsigned dptr_type_len = TYPE_LENGTH (dptr_type);
573 struct compile_module *retval;
574 struct type *regs_type, *out_value_type = NULL;
575 char *filename, **matching;
576 struct objfile *objfile;
577 int expect_parameters;
578 struct type *expect_return_type;
579
580 filename = tilde_expand (object_file);
581 cleanups = make_cleanup (xfree, filename);
582
583 abfd = gdb_bfd_open (filename, gnutarget, -1);
584 if (abfd == NULL)
585 error (_("\"%s\": could not open as compiled module: %s"),
586 filename, bfd_errmsg (bfd_get_error ()));
587 make_cleanup_bfd_unref (abfd);
588
589 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
590 error (_("\"%s\": not in loadable format: %s"),
591 filename, gdb_bfd_errmsg (bfd_get_error (), matching));
592
593 if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC)) != 0)
594 error (_("\"%s\": not in object format."), filename);
595
596 setup_sections_data.last_size = 0;
597 setup_sections_data.last_section_first = abfd->sections;
598 setup_sections_data.last_prot = -1;
599 setup_sections_data.last_max_alignment = 1;
600 bfd_map_over_sections (abfd, setup_sections, &setup_sections_data);
601 setup_sections (abfd, NULL, &setup_sections_data);
602
603 storage_needed = bfd_get_symtab_upper_bound (abfd);
604 if (storage_needed < 0)
605 error (_("Cannot read symbols of compiled module \"%s\": %s"),
606 filename, bfd_errmsg (bfd_get_error ()));
607
608 /* SYMFILE_VERBOSE is not passed even if FROM_TTY, user is not interested in
609 "Reading symbols from ..." message for automatically generated file. */
610 objfile = symbol_file_add_from_bfd (abfd, filename, 0, NULL, 0, NULL);
611 cleanups_free_objfile = make_cleanup_free_objfile (objfile);
612
613 func_sym = lookup_global_symbol_from_objfile (objfile,
614 GCC_FE_WRAPPER_FUNCTION,
615 VAR_DOMAIN);
616 if (func_sym == NULL)
617 error (_("Cannot find function \"%s\" in compiled module \"%s\"."),
618 GCC_FE_WRAPPER_FUNCTION, objfile_name (objfile));
619 func_type = SYMBOL_TYPE (func_sym);
620 if (TYPE_CODE (func_type) != TYPE_CODE_FUNC)
621 error (_("Invalid type code %d of function \"%s\" in compiled "
622 "module \"%s\"."),
623 TYPE_CODE (func_type), GCC_FE_WRAPPER_FUNCTION,
624 objfile_name (objfile));
625
626 switch (scope)
627 {
628 case COMPILE_I_SIMPLE_SCOPE:
629 expect_parameters = 1;
630 expect_return_type = builtin_type (target_gdbarch ())->builtin_void;
631 break;
632 case COMPILE_I_RAW_SCOPE:
633 expect_parameters = 0;
634 expect_return_type = builtin_type (target_gdbarch ())->builtin_void;
635 break;
636 case COMPILE_I_PRINT_ADDRESS_SCOPE:
637 case COMPILE_I_PRINT_VALUE_SCOPE:
638 expect_parameters = 2;
639 expect_return_type = builtin_type (target_gdbarch ())->builtin_void;
640 break;
641 default:
642 internal_error (__FILE__, __LINE__, _("invalid scope %d"), scope);
643 }
644 if (TYPE_NFIELDS (func_type) != expect_parameters)
645 error (_("Invalid %d parameters of function \"%s\" in compiled "
646 "module \"%s\"."),
647 TYPE_NFIELDS (func_type), GCC_FE_WRAPPER_FUNCTION,
648 objfile_name (objfile));
649 if (!types_deeply_equal (expect_return_type, TYPE_TARGET_TYPE (func_type)))
650 error (_("Invalid return type of function \"%s\" in compiled "
651 "module \"%s\"."),
652 GCC_FE_WRAPPER_FUNCTION, objfile_name (objfile));
653
654 /* The memory may be later needed
655 by bfd_generic_get_relocated_section_contents
656 called from default_symfile_relocate. */
657 symbol_table = obstack_alloc (&objfile->objfile_obstack, storage_needed);
658 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
659 if (number_of_symbols < 0)
660 error (_("Cannot parse symbols of compiled module \"%s\": %s"),
661 filename, bfd_errmsg (bfd_get_error ()));
662
663 missing_symbols = 0;
664 for (symp = symbol_table; symp < symbol_table + number_of_symbols; symp++)
665 {
666 asymbol *sym = *symp;
667
668 if (sym->flags != 0)
669 continue;
670 if (compile_debug)
671 fprintf_unfiltered (gdb_stdout,
672 "lookup undefined ELF symbol \"%s\"\n",
673 sym->name);
674 sym->flags = BSF_GLOBAL;
675 sym->section = bfd_abs_section_ptr;
676 if (strcmp (sym->name, "_GLOBAL_OFFSET_TABLE_") == 0)
677 {
678 sym->value = 0;
679 continue;
680 }
681 bmsym = lookup_minimal_symbol (sym->name, NULL, NULL);
682 switch (bmsym.minsym == NULL
683 ? mst_unknown : MSYMBOL_TYPE (bmsym.minsym))
684 {
685 case mst_text:
686 sym->value = BMSYMBOL_VALUE_ADDRESS (bmsym);
687 break;
688 case mst_text_gnu_ifunc:
689 sym->value = gnu_ifunc_resolve_addr (target_gdbarch (),
690 BMSYMBOL_VALUE_ADDRESS (bmsym));
691 break;
692 default:
693 warning (_("Could not find symbol \"%s\" "
694 "for compiled module \"%s\"."),
695 sym->name, filename);
696 missing_symbols++;
697 }
698 }
699 if (missing_symbols)
700 error (_("%ld symbols were missing, cannot continue."), missing_symbols);
701
702 bfd_map_over_sections (abfd, copy_sections, symbol_table);
703
704 regs_type = get_regs_type (func_sym, objfile);
705 if (regs_type == NULL)
706 regs_addr = 0;
707 else
708 {
709 /* Use read-only non-executable memory protection. */
710 regs_addr = gdbarch_infcall_mmap (target_gdbarch (),
711 TYPE_LENGTH (regs_type),
712 GDB_MMAP_PROT_READ);
713 gdb_assert (regs_addr != 0);
714 if (compile_debug)
715 fprintf_unfiltered (gdb_stdout,
716 "allocated %s bytes at %s for registers\n",
717 paddress (target_gdbarch (),
718 TYPE_LENGTH (regs_type)),
719 paddress (target_gdbarch (), regs_addr));
720 store_regs (regs_type, regs_addr);
721 }
722
723 if (scope == COMPILE_I_PRINT_ADDRESS_SCOPE
724 || scope == COMPILE_I_PRINT_VALUE_SCOPE)
725 {
726 out_value_type = get_out_value_type (func_sym, objfile, scope);
727 if (out_value_type == NULL)
728 {
729 do_cleanups (cleanups);
730 return NULL;
731 }
732 check_typedef (out_value_type);
733 out_value_addr = gdbarch_infcall_mmap (target_gdbarch (),
734 TYPE_LENGTH (out_value_type),
735 (GDB_MMAP_PROT_READ
736 | GDB_MMAP_PROT_WRITE));
737 gdb_assert (out_value_addr != 0);
738 if (compile_debug)
739 fprintf_unfiltered (gdb_stdout,
740 "allocated %s bytes at %s for printed value\n",
741 paddress (target_gdbarch (),
742 TYPE_LENGTH (out_value_type)),
743 paddress (target_gdbarch (), out_value_addr));
744 }
745
746 discard_cleanups (cleanups_free_objfile);
747 do_cleanups (cleanups);
748
749 retval = xmalloc (sizeof (*retval));
750 retval->objfile = objfile;
751 retval->source_file = xstrdup (source_file);
752 retval->func_sym = func_sym;
753 retval->regs_addr = regs_addr;
754 retval->scope = scope;
755 retval->scope_data = scope_data;
756 retval->out_value_type = out_value_type;
757 retval->out_value_addr = out_value_addr;
758 return retval;
759 }
This page took 0.046705 seconds and 4 git commands to generate.