[ARM] Commit approaved testcases missed in previous commit
[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)
a4063588 79 fprintf_unfiltered (gdb_stdlog,
bb2ec1b3
TT
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)
a4063588 101 fprintf_unfiltered (gdb_stdlog,
bb2ec1b3
TT
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{
d976bace
JK
374 struct symbol *gdb_ptr_type_sym;
375 /* Initialize it just to avoid a GCC false warning. */
376 struct symbol *gdb_val_sym = NULL;
4d18dfad 377 struct type *gdb_ptr_type, *gdb_type_from_ptr, *gdb_type, *retval;
d976bace
JK
378 /* Initialize it just to avoid a GCC false warning. */
379 const struct block *block = NULL;
36de76f9
JK
380 const struct blockvector *bv;
381 int nblocks = 0;
382 int block_loop = 0;
383
384 bv = SYMTAB_BLOCKVECTOR (func_sym->owner.symtab);
385 nblocks = BLOCKVECTOR_NBLOCKS (bv);
386
387 gdb_ptr_type_sym = NULL;
388 for (block_loop = 0; block_loop < nblocks; block_loop++)
389 {
d976bace 390 struct symbol *function = NULL;
36de76f9
JK
391 const struct block *function_block;
392
393 block = BLOCKVECTOR_BLOCK (bv, block_loop);
394 if (BLOCK_FUNCTION (block) != NULL)
395 continue;
396 gdb_val_sym = block_lookup_symbol (block, COMPILE_I_EXPR_VAL, VAR_DOMAIN);
397 if (gdb_val_sym == NULL)
398 continue;
399
400 function_block = block;
401 while (function_block != BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK)
402 && function_block != BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK))
403 {
404 function_block = BLOCK_SUPERBLOCK (function_block);
405 function = BLOCK_FUNCTION (function_block);
406 if (function != NULL)
407 break;
408 }
409 if (function != NULL
410 && (BLOCK_SUPERBLOCK (function_block)
411 == BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK))
412 && (strcmp (SYMBOL_LINKAGE_NAME (function), GCC_FE_WRAPPER_FUNCTION)
413 == 0))
414 break;
415 }
416 if (block_loop == nblocks)
417 error (_("No \"%s\" symbol found"), COMPILE_I_EXPR_PTR_TYPE);
418
419 gdb_type = SYMBOL_TYPE (gdb_val_sym);
420 CHECK_TYPEDEF (gdb_type);
421
422 gdb_ptr_type_sym = block_lookup_symbol (block, COMPILE_I_EXPR_PTR_TYPE,
423 VAR_DOMAIN);
424 if (gdb_ptr_type_sym == NULL)
425 error (_("No \"%s\" symbol found"), COMPILE_I_EXPR_PTR_TYPE);
426 gdb_ptr_type = SYMBOL_TYPE (gdb_ptr_type_sym);
427 CHECK_TYPEDEF (gdb_ptr_type);
428 if (TYPE_CODE (gdb_ptr_type) != TYPE_CODE_PTR)
429 error (_("Type of \"%s\" is not a pointer"), COMPILE_I_EXPR_PTR_TYPE);
430 gdb_type_from_ptr = TYPE_TARGET_TYPE (gdb_ptr_type);
431
432 if (types_deeply_equal (gdb_type, gdb_type_from_ptr))
433 {
434 if (scope != COMPILE_I_PRINT_ADDRESS_SCOPE)
435 error (_("Expected address scope in compiled module \"%s\"."),
436 objfile_name (objfile));
437 return gdb_type;
438 }
439
440 if (TYPE_CODE (gdb_type) != TYPE_CODE_PTR)
441 error (_("Invalid type code %d of symbol \"%s\" "
442 "in compiled module \"%s\"."),
443 TYPE_CODE (gdb_type_from_ptr), COMPILE_I_EXPR_VAL,
444 objfile_name (objfile));
445
4d18dfad 446 retval = gdb_type_from_ptr;
36de76f9
JK
447 switch (TYPE_CODE (gdb_type_from_ptr))
448 {
449 case TYPE_CODE_ARRAY:
450 gdb_type_from_ptr = TYPE_TARGET_TYPE (gdb_type_from_ptr);
451 break;
452 case TYPE_CODE_FUNC:
453 break;
454 default:
455 error (_("Invalid type code %d of symbol \"%s\" "
456 "in compiled module \"%s\"."),
457 TYPE_CODE (gdb_type_from_ptr), COMPILE_I_EXPR_PTR_TYPE,
458 objfile_name (objfile));
459 }
460 if (!types_deeply_equal (gdb_type_from_ptr,
461 TYPE_TARGET_TYPE (gdb_type)))
462 error (_("Referenced types do not match for symbols \"%s\" and \"%s\" "
463 "in compiled module \"%s\"."),
464 COMPILE_I_EXPR_PTR_TYPE, COMPILE_I_EXPR_VAL,
465 objfile_name (objfile));
466 if (scope == COMPILE_I_PRINT_ADDRESS_SCOPE)
467 return NULL;
4d18dfad 468 return retval;
36de76f9
JK
469}
470
83d3415e
JK
471/* Fetch the type of first parameter of FUNC_SYM.
472 Return NULL if FUNC_SYM has no parameters. Throw an error otherwise. */
bb2ec1b3
TT
473
474static struct type *
83d3415e 475get_regs_type (struct symbol *func_sym, struct objfile *objfile)
bb2ec1b3 476{
83d3415e
JK
477 struct type *func_type = SYMBOL_TYPE (func_sym);
478 struct type *regsp_type, *regs_type;
bb2ec1b3
TT
479
480 /* No register parameter present. */
481 if (TYPE_NFIELDS (func_type) == 0)
482 return NULL;
483
bb2ec1b3
TT
484 regsp_type = check_typedef (TYPE_FIELD_TYPE (func_type, 0));
485 if (TYPE_CODE (regsp_type) != TYPE_CODE_PTR)
486 error (_("Invalid type code %d of first parameter of function \"%s\" "
487 "in compiled module \"%s\"."),
488 TYPE_CODE (regsp_type), GCC_FE_WRAPPER_FUNCTION,
489 objfile_name (objfile));
490
491 regs_type = check_typedef (TYPE_TARGET_TYPE (regsp_type));
492 if (TYPE_CODE (regs_type) != TYPE_CODE_STRUCT)
493 error (_("Invalid type code %d of dereferenced first parameter "
494 "of function \"%s\" in compiled module \"%s\"."),
495 TYPE_CODE (regs_type), GCC_FE_WRAPPER_FUNCTION,
496 objfile_name (objfile));
497
498 return regs_type;
499}
500
501/* Store all inferior registers required by REGS_TYPE to inferior memory
502 starting at inferior address REGS_BASE. */
503
504static void
505store_regs (struct type *regs_type, CORE_ADDR regs_base)
506{
507 struct gdbarch *gdbarch = target_gdbarch ();
508 struct regcache *regcache = get_thread_regcache (inferior_ptid);
509 int fieldno;
510
511 for (fieldno = 0; fieldno < TYPE_NFIELDS (regs_type); fieldno++)
512 {
513 const char *reg_name = TYPE_FIELD_NAME (regs_type, fieldno);
514 ULONGEST reg_bitpos = TYPE_FIELD_BITPOS (regs_type, fieldno);
515 ULONGEST reg_bitsize = TYPE_FIELD_BITSIZE (regs_type, fieldno);
516 ULONGEST reg_offset;
517 struct type *reg_type = check_typedef (TYPE_FIELD_TYPE (regs_type,
518 fieldno));
519 ULONGEST reg_size = TYPE_LENGTH (reg_type);
520 int regnum;
521 struct value *regval;
522 CORE_ADDR inferior_addr;
523
524 if (strcmp (reg_name, COMPILE_I_SIMPLE_REGISTER_DUMMY) == 0)
525 continue;
526
527 if ((reg_bitpos % 8) != 0 || reg_bitsize != 0)
528 error (_("Invalid register \"%s\" position %s bits or size %s bits"),
529 reg_name, pulongest (reg_bitpos), pulongest (reg_bitsize));
530 reg_offset = reg_bitpos / 8;
531
532 if (TYPE_CODE (reg_type) != TYPE_CODE_INT
533 && TYPE_CODE (reg_type) != TYPE_CODE_PTR)
534 error (_("Invalid register \"%s\" type code %d"), reg_name,
535 TYPE_CODE (reg_type));
536
537 regnum = compile_register_name_demangle (gdbarch, reg_name);
538
539 regval = value_from_register (reg_type, regnum, get_current_frame ());
540 if (value_optimized_out (regval))
541 error (_("Register \"%s\" is optimized out."), reg_name);
542 if (!value_entirely_available (regval))
543 error (_("Register \"%s\" is not available."), reg_name);
544
545 inferior_addr = regs_base + reg_offset;
546 if (0 != target_write_memory (inferior_addr, value_contents (regval),
547 reg_size))
548 error (_("Cannot write register \"%s\" to inferior memory at %s."),
549 reg_name, paddress (gdbarch, inferior_addr));
550 }
551}
552
553/* Load OBJECT_FILE into inferior memory. Throw an error otherwise.
554 Caller must fully dispose the return value by calling compile_object_run.
555 SOURCE_FILE's copy is stored into the returned object.
556 Caller should free both OBJECT_FILE and SOURCE_FILE immediatelly after this
36de76f9
JK
557 function returns.
558 Function returns NULL only for COMPILE_I_PRINT_ADDRESS_SCOPE when
559 COMPILE_I_PRINT_VALUE_SCOPE should have been used instead. */
bb2ec1b3
TT
560
561struct compile_module *
5c65b58a
JK
562compile_object_load (const char *object_file, const char *source_file,
563 enum compile_i_scope_types scope, void *scope_data)
bb2ec1b3
TT
564{
565 struct cleanup *cleanups, *cleanups_free_objfile;
566 bfd *abfd;
567 struct setup_sections_data setup_sections_data;
36de76f9 568 CORE_ADDR addr, regs_addr, out_value_addr = 0;
83d3415e
JK
569 struct symbol *func_sym;
570 struct type *func_type;
bb2ec1b3
TT
571 struct bound_minimal_symbol bmsym;
572 long storage_needed;
573 asymbol **symbol_table, **symp;
574 long number_of_symbols, missing_symbols;
575 struct type *dptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
576 unsigned dptr_type_len = TYPE_LENGTH (dptr_type);
577 struct compile_module *retval;
36de76f9 578 struct type *regs_type, *out_value_type = NULL;
bb2ec1b3
TT
579 char *filename, **matching;
580 struct objfile *objfile;
83d3415e
JK
581 int expect_parameters;
582 struct type *expect_return_type;
bb2ec1b3
TT
583
584 filename = tilde_expand (object_file);
585 cleanups = make_cleanup (xfree, filename);
586
587 abfd = gdb_bfd_open (filename, gnutarget, -1);
588 if (abfd == NULL)
589 error (_("\"%s\": could not open as compiled module: %s"),
590 filename, bfd_errmsg (bfd_get_error ()));
591 make_cleanup_bfd_unref (abfd);
592
593 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
594 error (_("\"%s\": not in loadable format: %s"),
595 filename, gdb_bfd_errmsg (bfd_get_error (), matching));
596
597 if ((bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC)) != 0)
598 error (_("\"%s\": not in object format."), filename);
599
600 setup_sections_data.last_size = 0;
601 setup_sections_data.last_section_first = abfd->sections;
602 setup_sections_data.last_prot = -1;
603 setup_sections_data.last_max_alignment = 1;
604 bfd_map_over_sections (abfd, setup_sections, &setup_sections_data);
605 setup_sections (abfd, NULL, &setup_sections_data);
606
607 storage_needed = bfd_get_symtab_upper_bound (abfd);
608 if (storage_needed < 0)
609 error (_("Cannot read symbols of compiled module \"%s\": %s"),
610 filename, bfd_errmsg (bfd_get_error ()));
611
612 /* SYMFILE_VERBOSE is not passed even if FROM_TTY, user is not interested in
613 "Reading symbols from ..." message for automatically generated file. */
614 objfile = symbol_file_add_from_bfd (abfd, filename, 0, NULL, 0, NULL);
615 cleanups_free_objfile = make_cleanup_free_objfile (objfile);
616
83d3415e
JK
617 func_sym = lookup_global_symbol_from_objfile (objfile,
618 GCC_FE_WRAPPER_FUNCTION,
619 VAR_DOMAIN);
620 if (func_sym == NULL)
621 error (_("Cannot find function \"%s\" in compiled module \"%s\"."),
622 GCC_FE_WRAPPER_FUNCTION, objfile_name (objfile));
623 func_type = SYMBOL_TYPE (func_sym);
624 if (TYPE_CODE (func_type) != TYPE_CODE_FUNC)
625 error (_("Invalid type code %d of function \"%s\" in compiled "
626 "module \"%s\"."),
627 TYPE_CODE (func_type), GCC_FE_WRAPPER_FUNCTION,
628 objfile_name (objfile));
629
630 switch (scope)
631 {
632 case COMPILE_I_SIMPLE_SCOPE:
633 expect_parameters = 1;
634 expect_return_type = builtin_type (target_gdbarch ())->builtin_void;
635 break;
636 case COMPILE_I_RAW_SCOPE:
637 expect_parameters = 0;
638 expect_return_type = builtin_type (target_gdbarch ())->builtin_void;
639 break;
36de76f9
JK
640 case COMPILE_I_PRINT_ADDRESS_SCOPE:
641 case COMPILE_I_PRINT_VALUE_SCOPE:
642 expect_parameters = 2;
643 expect_return_type = builtin_type (target_gdbarch ())->builtin_void;
644 break;
83d3415e
JK
645 default:
646 internal_error (__FILE__, __LINE__, _("invalid scope %d"), scope);
647 }
648 if (TYPE_NFIELDS (func_type) != expect_parameters)
649 error (_("Invalid %d parameters of function \"%s\" in compiled "
650 "module \"%s\"."),
651 TYPE_NFIELDS (func_type), GCC_FE_WRAPPER_FUNCTION,
652 objfile_name (objfile));
653 if (!types_deeply_equal (expect_return_type, TYPE_TARGET_TYPE (func_type)))
654 error (_("Invalid return type of function \"%s\" in compiled "
655 "module \"%s\"."),
656 GCC_FE_WRAPPER_FUNCTION, objfile_name (objfile));
bb2ec1b3
TT
657
658 /* The memory may be later needed
659 by bfd_generic_get_relocated_section_contents
660 called from default_symfile_relocate. */
661 symbol_table = obstack_alloc (&objfile->objfile_obstack, storage_needed);
662 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
663 if (number_of_symbols < 0)
664 error (_("Cannot parse symbols of compiled module \"%s\": %s"),
665 filename, bfd_errmsg (bfd_get_error ()));
666
667 missing_symbols = 0;
668 for (symp = symbol_table; symp < symbol_table + number_of_symbols; symp++)
669 {
670 asymbol *sym = *symp;
671
672 if (sym->flags != 0)
673 continue;
674 if (compile_debug)
a4063588 675 fprintf_unfiltered (gdb_stdlog,
bb2ec1b3
TT
676 "lookup undefined ELF symbol \"%s\"\n",
677 sym->name);
678 sym->flags = BSF_GLOBAL;
679 sym->section = bfd_abs_section_ptr;
680 if (strcmp (sym->name, "_GLOBAL_OFFSET_TABLE_") == 0)
681 {
682 sym->value = 0;
683 continue;
684 }
685 bmsym = lookup_minimal_symbol (sym->name, NULL, NULL);
686 switch (bmsym.minsym == NULL
687 ? mst_unknown : MSYMBOL_TYPE (bmsym.minsym))
688 {
689 case mst_text:
690 sym->value = BMSYMBOL_VALUE_ADDRESS (bmsym);
691 break;
e26efa40
JK
692 case mst_text_gnu_ifunc:
693 sym->value = gnu_ifunc_resolve_addr (target_gdbarch (),
694 BMSYMBOL_VALUE_ADDRESS (bmsym));
695 break;
bb2ec1b3
TT
696 default:
697 warning (_("Could not find symbol \"%s\" "
698 "for compiled module \"%s\"."),
699 sym->name, filename);
700 missing_symbols++;
701 }
702 }
703 if (missing_symbols)
704 error (_("%ld symbols were missing, cannot continue."), missing_symbols);
705
706 bfd_map_over_sections (abfd, copy_sections, symbol_table);
707
83d3415e 708 regs_type = get_regs_type (func_sym, objfile);
bb2ec1b3
TT
709 if (regs_type == NULL)
710 regs_addr = 0;
711 else
712 {
713 /* Use read-only non-executable memory protection. */
714 regs_addr = gdbarch_infcall_mmap (target_gdbarch (),
715 TYPE_LENGTH (regs_type),
716 GDB_MMAP_PROT_READ);
717 gdb_assert (regs_addr != 0);
b6de3f96 718 if (compile_debug)
a4063588 719 fprintf_unfiltered (gdb_stdlog,
b6de3f96
JK
720 "allocated %s bytes at %s for registers\n",
721 paddress (target_gdbarch (),
722 TYPE_LENGTH (regs_type)),
723 paddress (target_gdbarch (), regs_addr));
bb2ec1b3
TT
724 store_regs (regs_type, regs_addr);
725 }
726
36de76f9
JK
727 if (scope == COMPILE_I_PRINT_ADDRESS_SCOPE
728 || scope == COMPILE_I_PRINT_VALUE_SCOPE)
729 {
730 out_value_type = get_out_value_type (func_sym, objfile, scope);
731 if (out_value_type == NULL)
732 {
733 do_cleanups (cleanups);
734 return NULL;
735 }
736 check_typedef (out_value_type);
737 out_value_addr = gdbarch_infcall_mmap (target_gdbarch (),
738 TYPE_LENGTH (out_value_type),
739 (GDB_MMAP_PROT_READ
740 | GDB_MMAP_PROT_WRITE));
741 gdb_assert (out_value_addr != 0);
742 if (compile_debug)
a4063588 743 fprintf_unfiltered (gdb_stdlog,
36de76f9
JK
744 "allocated %s bytes at %s for printed value\n",
745 paddress (target_gdbarch (),
746 TYPE_LENGTH (out_value_type)),
747 paddress (target_gdbarch (), out_value_addr));
748 }
749
bb2ec1b3
TT
750 discard_cleanups (cleanups_free_objfile);
751 do_cleanups (cleanups);
752
753 retval = xmalloc (sizeof (*retval));
754 retval->objfile = objfile;
755 retval->source_file = xstrdup (source_file);
83d3415e 756 retval->func_sym = func_sym;
bb2ec1b3 757 retval->regs_addr = regs_addr;
5c65b58a
JK
758 retval->scope = scope;
759 retval->scope_data = scope_data;
36de76f9
JK
760 retval->out_value_type = out_value_type;
761 retval->out_value_addr = out_value_addr;
bb2ec1b3
TT
762 return retval;
763}
This page took 0.08917 seconds and 4 git commands to generate.