compile: Fix detected inferior type
[deliverable/binutils-gdb.git] / gdb / compile / compile-object-load.c
CommitLineData
bb2ec1b3
TT
1/* Load module for 'compile' command.
2
32d0add0 3 Copyright (C) 2014-2015 Free Software Foundation, Inc.
bb2ec1b3
TT
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"
36de76f9 32#include "block.h"
bb2ec1b3
TT
33#include "arch-utils.h"
34
35/* Helper data for setup_sections. */
36
37struct 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
55static void
56setup_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
bb2b33b9 71 /* Make the memory always readable. */
bb2ec1b3
TT
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
144static bfd_boolean
145link_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;
8e8347b8 153 warning (_("Compiled module \"%s\": multiple symbol definitions: %s"),
bb2ec1b3
TT
154 bfd_get_filename (abfd), h->root.string);
155 return FALSE;
156}
157
158/* Helper for link_callbacks callbacks vector. */
159
160static bfd_boolean
161link_callbacks_warning (struct bfd_link_info *link_info, const char *xwarning,
162 const char *symbol, bfd *abfd, asection *section,
163 bfd_vma address)
164{
8e8347b8 165 warning (_("Compiled module \"%s\" section \"%s\": warning: %s"),
bb2ec1b3
TT
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
174static bfd_boolean
175link_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
187static bfd_boolean
188link_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
200static bfd_boolean
201link_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
214static bfd_boolean
215link_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
77b64a49
PA
228static void link_callbacks_einfo (const char *fmt, ...)
229 ATTRIBUTE_PRINTF (1, 2);
230
bb2ec1b3
TT
231static void
232link_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
8e8347b8 243 warning (_("Compile module: warning: %s"), str);
bb2ec1b3
TT
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
252static 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
271struct link_hash_table_cleanup_data
272{
273 bfd *abfd;
274 bfd *link_next;
275};
276
277/* Cleanup callback for struct bfd_link_info. */
278
279static void
280link_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
291static void
292copy_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
36de76f9
JK
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
370static struct type *
371get_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;
4d18dfad 375 struct type *gdb_ptr_type, *gdb_type_from_ptr, *gdb_type, *retval;
36de76f9
JK
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
4d18dfad 443 retval = gdb_type_from_ptr;
36de76f9
JK
444 switch (TYPE_CODE (gdb_type_from_ptr))
445 {
446 case TYPE_CODE_ARRAY:
447 gdb_type_from_ptr = TYPE_TARGET_TYPE (gdb_type_from_ptr);
448 break;
449 case TYPE_CODE_FUNC:
450 break;
451 default:
452 error (_("Invalid type code %d of symbol \"%s\" "
453 "in compiled module \"%s\"."),
454 TYPE_CODE (gdb_type_from_ptr), COMPILE_I_EXPR_PTR_TYPE,
455 objfile_name (objfile));
456 }
457 if (!types_deeply_equal (gdb_type_from_ptr,
458 TYPE_TARGET_TYPE (gdb_type)))
459 error (_("Referenced types do not match for symbols \"%s\" and \"%s\" "
460 "in compiled module \"%s\"."),
461 COMPILE_I_EXPR_PTR_TYPE, COMPILE_I_EXPR_VAL,
462 objfile_name (objfile));
463 if (scope == COMPILE_I_PRINT_ADDRESS_SCOPE)
464 return NULL;
4d18dfad 465 return retval;
36de76f9
JK
466}
467
83d3415e
JK
468/* Fetch the type of first parameter of FUNC_SYM.
469 Return NULL if FUNC_SYM has no parameters. Throw an error otherwise. */
bb2ec1b3
TT
470
471static struct type *
83d3415e 472get_regs_type (struct symbol *func_sym, struct objfile *objfile)
bb2ec1b3 473{
83d3415e
JK
474 struct type *func_type = SYMBOL_TYPE (func_sym);
475 struct type *regsp_type, *regs_type;
bb2ec1b3
TT
476
477 /* No register parameter present. */
478 if (TYPE_NFIELDS (func_type) == 0)
479 return NULL;
480
bb2ec1b3
TT
481 regsp_type = check_typedef (TYPE_FIELD_TYPE (func_type, 0));
482 if (TYPE_CODE (regsp_type) != TYPE_CODE_PTR)
483 error (_("Invalid type code %d of first parameter of function \"%s\" "
484 "in compiled module \"%s\"."),
485 TYPE_CODE (regsp_type), GCC_FE_WRAPPER_FUNCTION,
486 objfile_name (objfile));
487
488 regs_type = check_typedef (TYPE_TARGET_TYPE (regsp_type));
489 if (TYPE_CODE (regs_type) != TYPE_CODE_STRUCT)
490 error (_("Invalid type code %d of dereferenced first parameter "
491 "of function \"%s\" in compiled module \"%s\"."),
492 TYPE_CODE (regs_type), GCC_FE_WRAPPER_FUNCTION,
493 objfile_name (objfile));
494
495 return regs_type;
496}
497
498/* Store all inferior registers required by REGS_TYPE to inferior memory
499 starting at inferior address REGS_BASE. */
500
501static void
502store_regs (struct type *regs_type, CORE_ADDR regs_base)
503{
504 struct gdbarch *gdbarch = target_gdbarch ();
505 struct regcache *regcache = get_thread_regcache (inferior_ptid);
506 int fieldno;
507
508 for (fieldno = 0; fieldno < TYPE_NFIELDS (regs_type); fieldno++)
509 {
510 const char *reg_name = TYPE_FIELD_NAME (regs_type, fieldno);
511 ULONGEST reg_bitpos = TYPE_FIELD_BITPOS (regs_type, fieldno);
512 ULONGEST reg_bitsize = TYPE_FIELD_BITSIZE (regs_type, fieldno);
513 ULONGEST reg_offset;
514 struct type *reg_type = check_typedef (TYPE_FIELD_TYPE (regs_type,
515 fieldno));
516 ULONGEST reg_size = TYPE_LENGTH (reg_type);
517 int regnum;
518 struct value *regval;
519 CORE_ADDR inferior_addr;
520
521 if (strcmp (reg_name, COMPILE_I_SIMPLE_REGISTER_DUMMY) == 0)
522 continue;
523
524 if ((reg_bitpos % 8) != 0 || reg_bitsize != 0)
525 error (_("Invalid register \"%s\" position %s bits or size %s bits"),
526 reg_name, pulongest (reg_bitpos), pulongest (reg_bitsize));
527 reg_offset = reg_bitpos / 8;
528
529 if (TYPE_CODE (reg_type) != TYPE_CODE_INT
530 && TYPE_CODE (reg_type) != TYPE_CODE_PTR)
531 error (_("Invalid register \"%s\" type code %d"), reg_name,
532 TYPE_CODE (reg_type));
533
534 regnum = compile_register_name_demangle (gdbarch, reg_name);
535
536 regval = value_from_register (reg_type, regnum, get_current_frame ());
537 if (value_optimized_out (regval))
538 error (_("Register \"%s\" is optimized out."), reg_name);
539 if (!value_entirely_available (regval))
540 error (_("Register \"%s\" is not available."), reg_name);
541
542 inferior_addr = regs_base + reg_offset;
543 if (0 != target_write_memory (inferior_addr, value_contents (regval),
544 reg_size))
545 error (_("Cannot write register \"%s\" to inferior memory at %s."),
546 reg_name, paddress (gdbarch, inferior_addr));
547 }
548}
549
550/* Load OBJECT_FILE into inferior memory. Throw an error otherwise.
551 Caller must fully dispose the return value by calling compile_object_run.
552 SOURCE_FILE's copy is stored into the returned object.
553 Caller should free both OBJECT_FILE and SOURCE_FILE immediatelly after this
36de76f9
JK
554 function returns.
555 Function returns NULL only for COMPILE_I_PRINT_ADDRESS_SCOPE when
556 COMPILE_I_PRINT_VALUE_SCOPE should have been used instead. */
bb2ec1b3
TT
557
558struct compile_module *
5c65b58a
JK
559compile_object_load (const char *object_file, const char *source_file,
560 enum compile_i_scope_types scope, void *scope_data)
bb2ec1b3
TT
561{
562 struct cleanup *cleanups, *cleanups_free_objfile;
563 bfd *abfd;
564 struct setup_sections_data setup_sections_data;
36de76f9 565 CORE_ADDR addr, regs_addr, out_value_addr = 0;
83d3415e
JK
566 struct symbol *func_sym;
567 struct type *func_type;
bb2ec1b3
TT
568 struct bound_minimal_symbol bmsym;
569 long storage_needed;
570 asymbol **symbol_table, **symp;
571 long number_of_symbols, missing_symbols;
572 struct type *dptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
573 unsigned dptr_type_len = TYPE_LENGTH (dptr_type);
574 struct compile_module *retval;
36de76f9 575 struct type *regs_type, *out_value_type = NULL;
bb2ec1b3
TT
576 char *filename, **matching;
577 struct objfile *objfile;
83d3415e
JK
578 int expect_parameters;
579 struct type *expect_return_type;
bb2ec1b3
TT
580
581 filename = tilde_expand (object_file);
582 cleanups = make_cleanup (xfree, filename);
583
584 abfd = gdb_bfd_open (filename, gnutarget, -1);
585 if (abfd == NULL)
586 error (_("\"%s\": could not open as compiled module: %s"),
587 filename, bfd_errmsg (bfd_get_error ()));
588 make_cleanup_bfd_unref (abfd);
589
590 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
591 error (_("\"%s\": not in loadable format: %s"),
592 filename, gdb_bfd_errmsg (bfd_get_error (), matching));
593
594 if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC)) != 0)
595 error (_("\"%s\": not in object format."), filename);
596
597 setup_sections_data.last_size = 0;
598 setup_sections_data.last_section_first = abfd->sections;
599 setup_sections_data.last_prot = -1;
600 setup_sections_data.last_max_alignment = 1;
601 bfd_map_over_sections (abfd, setup_sections, &setup_sections_data);
602 setup_sections (abfd, NULL, &setup_sections_data);
603
604 storage_needed = bfd_get_symtab_upper_bound (abfd);
605 if (storage_needed < 0)
606 error (_("Cannot read symbols of compiled module \"%s\": %s"),
607 filename, bfd_errmsg (bfd_get_error ()));
608
609 /* SYMFILE_VERBOSE is not passed even if FROM_TTY, user is not interested in
610 "Reading symbols from ..." message for automatically generated file. */
611 objfile = symbol_file_add_from_bfd (abfd, filename, 0, NULL, 0, NULL);
612 cleanups_free_objfile = make_cleanup_free_objfile (objfile);
613
83d3415e
JK
614 func_sym = lookup_global_symbol_from_objfile (objfile,
615 GCC_FE_WRAPPER_FUNCTION,
616 VAR_DOMAIN);
617 if (func_sym == NULL)
618 error (_("Cannot find function \"%s\" in compiled module \"%s\"."),
619 GCC_FE_WRAPPER_FUNCTION, objfile_name (objfile));
620 func_type = SYMBOL_TYPE (func_sym);
621 if (TYPE_CODE (func_type) != TYPE_CODE_FUNC)
622 error (_("Invalid type code %d of function \"%s\" in compiled "
623 "module \"%s\"."),
624 TYPE_CODE (func_type), GCC_FE_WRAPPER_FUNCTION,
625 objfile_name (objfile));
626
627 switch (scope)
628 {
629 case COMPILE_I_SIMPLE_SCOPE:
630 expect_parameters = 1;
631 expect_return_type = builtin_type (target_gdbarch ())->builtin_void;
632 break;
633 case COMPILE_I_RAW_SCOPE:
634 expect_parameters = 0;
635 expect_return_type = builtin_type (target_gdbarch ())->builtin_void;
636 break;
36de76f9
JK
637 case COMPILE_I_PRINT_ADDRESS_SCOPE:
638 case COMPILE_I_PRINT_VALUE_SCOPE:
639 expect_parameters = 2;
640 expect_return_type = builtin_type (target_gdbarch ())->builtin_void;
641 break;
83d3415e
JK
642 default:
643 internal_error (__FILE__, __LINE__, _("invalid scope %d"), scope);
644 }
645 if (TYPE_NFIELDS (func_type) != expect_parameters)
646 error (_("Invalid %d parameters of function \"%s\" in compiled "
647 "module \"%s\"."),
648 TYPE_NFIELDS (func_type), GCC_FE_WRAPPER_FUNCTION,
649 objfile_name (objfile));
650 if (!types_deeply_equal (expect_return_type, TYPE_TARGET_TYPE (func_type)))
651 error (_("Invalid return type of function \"%s\" in compiled "
652 "module \"%s\"."),
653 GCC_FE_WRAPPER_FUNCTION, objfile_name (objfile));
bb2ec1b3
TT
654
655 /* The memory may be later needed
656 by bfd_generic_get_relocated_section_contents
657 called from default_symfile_relocate. */
658 symbol_table = obstack_alloc (&objfile->objfile_obstack, storage_needed);
659 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
660 if (number_of_symbols < 0)
661 error (_("Cannot parse symbols of compiled module \"%s\": %s"),
662 filename, bfd_errmsg (bfd_get_error ()));
663
664 missing_symbols = 0;
665 for (symp = symbol_table; symp < symbol_table + number_of_symbols; symp++)
666 {
667 asymbol *sym = *symp;
668
669 if (sym->flags != 0)
670 continue;
671 if (compile_debug)
672 fprintf_unfiltered (gdb_stdout,
673 "lookup undefined ELF symbol \"%s\"\n",
674 sym->name);
675 sym->flags = BSF_GLOBAL;
676 sym->section = bfd_abs_section_ptr;
677 if (strcmp (sym->name, "_GLOBAL_OFFSET_TABLE_") == 0)
678 {
679 sym->value = 0;
680 continue;
681 }
682 bmsym = lookup_minimal_symbol (sym->name, NULL, NULL);
683 switch (bmsym.minsym == NULL
684 ? mst_unknown : MSYMBOL_TYPE (bmsym.minsym))
685 {
686 case mst_text:
687 sym->value = BMSYMBOL_VALUE_ADDRESS (bmsym);
688 break;
e26efa40
JK
689 case mst_text_gnu_ifunc:
690 sym->value = gnu_ifunc_resolve_addr (target_gdbarch (),
691 BMSYMBOL_VALUE_ADDRESS (bmsym));
692 break;
bb2ec1b3
TT
693 default:
694 warning (_("Could not find symbol \"%s\" "
695 "for compiled module \"%s\"."),
696 sym->name, filename);
697 missing_symbols++;
698 }
699 }
700 if (missing_symbols)
701 error (_("%ld symbols were missing, cannot continue."), missing_symbols);
702
703 bfd_map_over_sections (abfd, copy_sections, symbol_table);
704
83d3415e 705 regs_type = get_regs_type (func_sym, objfile);
bb2ec1b3
TT
706 if (regs_type == NULL)
707 regs_addr = 0;
708 else
709 {
710 /* Use read-only non-executable memory protection. */
711 regs_addr = gdbarch_infcall_mmap (target_gdbarch (),
712 TYPE_LENGTH (regs_type),
713 GDB_MMAP_PROT_READ);
714 gdb_assert (regs_addr != 0);
b6de3f96
JK
715 if (compile_debug)
716 fprintf_unfiltered (gdb_stdout,
717 "allocated %s bytes at %s for registers\n",
718 paddress (target_gdbarch (),
719 TYPE_LENGTH (regs_type)),
720 paddress (target_gdbarch (), regs_addr));
bb2ec1b3
TT
721 store_regs (regs_type, regs_addr);
722 }
723
36de76f9
JK
724 if (scope == COMPILE_I_PRINT_ADDRESS_SCOPE
725 || scope == COMPILE_I_PRINT_VALUE_SCOPE)
726 {
727 out_value_type = get_out_value_type (func_sym, objfile, scope);
728 if (out_value_type == NULL)
729 {
730 do_cleanups (cleanups);
731 return NULL;
732 }
733 check_typedef (out_value_type);
734 out_value_addr = gdbarch_infcall_mmap (target_gdbarch (),
735 TYPE_LENGTH (out_value_type),
736 (GDB_MMAP_PROT_READ
737 | GDB_MMAP_PROT_WRITE));
738 gdb_assert (out_value_addr != 0);
739 if (compile_debug)
740 fprintf_unfiltered (gdb_stdout,
741 "allocated %s bytes at %s for printed value\n",
742 paddress (target_gdbarch (),
743 TYPE_LENGTH (out_value_type)),
744 paddress (target_gdbarch (), out_value_addr));
745 }
746
bb2ec1b3
TT
747 discard_cleanups (cleanups_free_objfile);
748 do_cleanups (cleanups);
749
750 retval = xmalloc (sizeof (*retval));
751 retval->objfile = objfile;
752 retval->source_file = xstrdup (source_file);
83d3415e 753 retval->func_sym = func_sym;
bb2ec1b3 754 retval->regs_addr = regs_addr;
5c65b58a
JK
755 retval->scope = scope;
756 retval->scope_data = scope_data;
36de76f9
JK
757 retval->out_value_type = out_value_type;
758 retval->out_value_addr = out_value_addr;
bb2ec1b3
TT
759 return retval;
760}
This page took 0.085217 seconds and 4 git commands to generate.