]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/compile/compile-object-load.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / compile / compile-object-load.c
1 /* Load module for 'compile' command.
2
3 Copyright (C) 2014-2024 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 "gdbthread.h"
32 #include "compile.h"
33 #include "block.h"
34 #include "arch-utils.h"
35 #include <algorithm>
36
37 /* Add inferior mmap memory range ADDR..ADDR+SIZE (exclusive) to the
38 list. */
39
40 void
41 munmap_list::add (CORE_ADDR addr, CORE_ADDR size)
42 {
43 struct munmap_item item = { addr, size };
44 items.push_back (item);
45 }
46
47 /* Destroy an munmap_list. */
48
49 munmap_list::~munmap_list ()
50 {
51 for (auto &item : items)
52 {
53 try
54 {
55 gdbarch_infcall_munmap (current_inferior ()->arch (),
56 item.addr, item.size);
57 }
58 catch (const gdb_exception_error &ex)
59 {
60 /* There's not much the user can do, so just ignore
61 this. */
62 }
63 }
64 }
65
66 /* A data structure that is used to lay out sections of our objfile in
67 inferior memory. */
68
69 struct setup_sections_data
70 {
71 explicit setup_sections_data (bfd *abfd)
72 : m_bfd (abfd),
73 m_last_section_first (abfd->sections)
74 {
75 }
76
77 /* Place all ABFD sections next to each other obeying all
78 constraints. */
79 void setup_one_section (asection *sect);
80
81 /* List of inferior mmap ranges where setup_sections should add its
82 next range. */
83 struct munmap_list munmap_list;
84
85 private:
86
87 /* The BFD. */
88 bfd *m_bfd;
89
90 /* Size of all recent sections with matching LAST_PROT. */
91 CORE_ADDR m_last_size = 0;
92
93 /* First section matching LAST_PROT. */
94 asection *m_last_section_first;
95
96 /* Memory protection like the prot parameter of gdbarch_infcall_mmap. */
97 unsigned m_last_prot = -1;
98
99 /* Maximum of alignments of all sections matching LAST_PROT.
100 This value is always at least 1. This value is always a power of 2. */
101 CORE_ADDR m_last_max_alignment = -1;
102
103 };
104
105 /* See setup_sections_data. */
106
107 void
108 setup_sections_data::setup_one_section (asection *sect)
109 {
110 CORE_ADDR alignment;
111 unsigned prot;
112
113 if (sect != NULL)
114 {
115 /* It is required by later bfd_get_relocated_section_contents. */
116 if (sect->output_section == NULL)
117 sect->output_section = sect;
118
119 if ((bfd_section_flags (sect) & SEC_ALLOC) == 0)
120 return;
121
122 /* Make the memory always readable. */
123 prot = GDB_MMAP_PROT_READ;
124 if ((bfd_section_flags (sect) & SEC_READONLY) == 0)
125 prot |= GDB_MMAP_PROT_WRITE;
126 if ((bfd_section_flags (sect) & SEC_CODE) != 0)
127 prot |= GDB_MMAP_PROT_EXEC;
128
129 if (compile_debug)
130 gdb_printf (gdb_stdlog,
131 "module \"%s\" section \"%s\" size %s prot %u\n",
132 bfd_get_filename (m_bfd),
133 bfd_section_name (sect),
134 paddress (current_inferior ()->arch (),
135 bfd_section_size (sect)),
136 prot);
137 }
138 else
139 prot = -1;
140
141 if (sect == NULL
142 || (m_last_prot != prot && bfd_section_size (sect) != 0))
143 {
144 CORE_ADDR addr;
145 asection *sect_iter;
146
147 if (m_last_size != 0)
148 {
149 addr = gdbarch_infcall_mmap (current_inferior ()->arch (),
150 m_last_size, m_last_prot);
151 munmap_list.add (addr, m_last_size);
152 if (compile_debug)
153 gdb_printf (gdb_stdlog,
154 "allocated %s bytes at %s prot %u\n",
155 paddress (current_inferior ()->arch (), m_last_size),
156 paddress (current_inferior ()->arch (), addr),
157 m_last_prot);
158 }
159 else
160 addr = 0;
161
162 if ((addr & (m_last_max_alignment - 1)) != 0)
163 error (_("Inferior compiled module address %s "
164 "is not aligned to BFD required %s."),
165 paddress (current_inferior ()->arch (), addr),
166 paddress (current_inferior ()->arch (), m_last_max_alignment));
167
168 for (sect_iter = m_last_section_first; sect_iter != sect;
169 sect_iter = sect_iter->next)
170 if ((bfd_section_flags (sect_iter) & SEC_ALLOC) != 0)
171 bfd_set_section_vma (sect_iter, addr + bfd_section_vma (sect_iter));
172
173 m_last_size = 0;
174 m_last_section_first = sect;
175 m_last_prot = prot;
176 m_last_max_alignment = 1;
177 }
178
179 if (sect == NULL)
180 return;
181
182 alignment = ((CORE_ADDR) 1) << bfd_section_alignment (sect);
183 m_last_max_alignment = std::max (m_last_max_alignment, alignment);
184
185 m_last_size = (m_last_size + alignment - 1) & -alignment;
186
187 bfd_set_section_vma (sect, m_last_size);
188
189 m_last_size += bfd_section_size (sect);
190 m_last_size = (m_last_size + alignment - 1) & -alignment;
191 }
192
193 /* Helper for link_callbacks callbacks vector. */
194
195 static void
196 link_callbacks_multiple_definition (struct bfd_link_info *link_info,
197 struct bfd_link_hash_entry *h, bfd *nbfd,
198 asection *nsec, bfd_vma nval)
199 {
200 bfd *abfd = link_info->input_bfds;
201
202 if (link_info->allow_multiple_definition)
203 return;
204 warning (_("Compiled module \"%s\": multiple symbol definitions: %s"),
205 bfd_get_filename (abfd), h->root.string);
206 }
207
208 /* Helper for link_callbacks callbacks vector. */
209
210 static void
211 link_callbacks_warning (struct bfd_link_info *link_info, const char *xwarning,
212 const char *symbol, bfd *abfd, asection *section,
213 bfd_vma address)
214 {
215 warning (_("Compiled module \"%s\" section \"%s\": warning: %s"),
216 bfd_get_filename (abfd), bfd_section_name (section),
217 xwarning);
218 }
219
220 /* Helper for link_callbacks callbacks vector. */
221
222 static void
223 link_callbacks_undefined_symbol (struct bfd_link_info *link_info,
224 const char *name, bfd *abfd, asection *section,
225 bfd_vma address, bfd_boolean is_fatal)
226 {
227 warning (_("Cannot resolve relocation to \"%s\" "
228 "from compiled module \"%s\" section \"%s\"."),
229 name, bfd_get_filename (abfd), bfd_section_name (section));
230 }
231
232 /* Helper for link_callbacks callbacks vector. */
233
234 static void
235 link_callbacks_reloc_overflow (struct bfd_link_info *link_info,
236 struct bfd_link_hash_entry *entry,
237 const char *name, const char *reloc_name,
238 bfd_vma addend, bfd *abfd, asection *section,
239 bfd_vma address)
240 {
241 }
242
243 /* Helper for link_callbacks callbacks vector. */
244
245 static void
246 link_callbacks_reloc_dangerous (struct bfd_link_info *link_info,
247 const char *message, bfd *abfd,
248 asection *section, bfd_vma address)
249 {
250 warning (_("Compiled module \"%s\" section \"%s\": dangerous "
251 "relocation: %s\n"),
252 bfd_get_filename (abfd), bfd_section_name (section),
253 message);
254 }
255
256 /* Helper for link_callbacks callbacks vector. */
257
258 static void
259 link_callbacks_unattached_reloc (struct bfd_link_info *link_info,
260 const char *name, bfd *abfd, asection *section,
261 bfd_vma address)
262 {
263 warning (_("Compiled module \"%s\" section \"%s\": unattached "
264 "relocation: %s\n"),
265 bfd_get_filename (abfd), bfd_section_name (section),
266 name);
267 }
268
269 /* Helper for link_callbacks callbacks vector. */
270
271 static void link_callbacks_einfo (const char *fmt, ...)
272 ATTRIBUTE_PRINTF (1, 2);
273
274 static void
275 link_callbacks_einfo (const char *fmt, ...)
276 {
277 va_list ap;
278
279 va_start (ap, fmt);
280 std::string str = string_vprintf (fmt, ap);
281 va_end (ap);
282
283 warning (_("Compile module: warning: %s"), str.c_str ());
284 }
285
286 /* Helper for bfd_get_relocated_section_contents.
287 Only these symbols are set by bfd_simple_get_relocated_section_contents
288 but bfd/ seems to use even the NULL ones without checking them first. */
289
290 static const struct bfd_link_callbacks link_callbacks =
291 {
292 NULL, /* add_archive_element */
293 link_callbacks_multiple_definition, /* multiple_definition */
294 NULL, /* multiple_common */
295 NULL, /* add_to_set */
296 NULL, /* constructor */
297 link_callbacks_warning, /* warning */
298 link_callbacks_undefined_symbol, /* undefined_symbol */
299 link_callbacks_reloc_overflow, /* reloc_overflow */
300 link_callbacks_reloc_dangerous, /* reloc_dangerous */
301 link_callbacks_unattached_reloc, /* unattached_reloc */
302 NULL, /* notice */
303 link_callbacks_einfo, /* einfo */
304 NULL, /* info */
305 NULL, /* minfo */
306 NULL, /* override_segment_assignment */
307 };
308
309 struct link_hash_table_cleanup_data
310 {
311 explicit link_hash_table_cleanup_data (bfd *abfd_)
312 : abfd (abfd_),
313 link_next (abfd->link.next)
314 {
315 }
316
317 ~link_hash_table_cleanup_data ()
318 {
319 if (abfd->is_linker_output)
320 (*abfd->link.hash->hash_table_free) (abfd);
321 abfd->link.next = link_next;
322 }
323
324 DISABLE_COPY_AND_ASSIGN (link_hash_table_cleanup_data);
325
326 private:
327
328 bfd *abfd;
329 bfd *link_next;
330 };
331
332 /* Relocate and store into inferior memory each section SECT of ABFD. */
333
334 static void
335 copy_sections (bfd *abfd, asection *sect, void *data)
336 {
337 asymbol **symbol_table = (asymbol **) data;
338 bfd_byte *sect_data_got;
339 struct bfd_link_info link_info;
340 struct bfd_link_order link_order;
341 CORE_ADDR inferior_addr;
342
343 if ((bfd_section_flags (sect) & (SEC_ALLOC | SEC_LOAD))
344 != (SEC_ALLOC | SEC_LOAD))
345 return;
346
347 if (bfd_section_size (sect) == 0)
348 return;
349
350 /* Mostly a copy of bfd_simple_get_relocated_section_contents which GDB
351 cannot use as it does not report relocations to undefined symbols. */
352 memset (&link_info, 0, sizeof (link_info));
353 link_info.output_bfd = abfd;
354 link_info.input_bfds = abfd;
355 link_info.input_bfds_tail = &abfd->link.next;
356
357 struct link_hash_table_cleanup_data cleanup_data (abfd);
358
359 abfd->link.next = NULL;
360 link_info.hash = bfd_link_hash_table_create (abfd);
361
362 link_info.callbacks = &link_callbacks;
363
364 memset (&link_order, 0, sizeof (link_order));
365 link_order.next = NULL;
366 link_order.type = bfd_indirect_link_order;
367 link_order.offset = 0;
368 link_order.size = bfd_section_size (sect);
369 link_order.u.indirect.section = sect;
370
371 gdb::unique_xmalloc_ptr<gdb_byte> sect_data
372 ((bfd_byte *) xmalloc (bfd_section_size (sect)));
373
374 sect_data_got = bfd_get_relocated_section_contents (abfd, &link_info,
375 &link_order,
376 sect_data.get (),
377 FALSE, symbol_table);
378
379 if (sect_data_got == NULL)
380 error (_("Cannot map compiled module \"%s\" section \"%s\": %s"),
381 bfd_get_filename (abfd), bfd_section_name (sect),
382 bfd_errmsg (bfd_get_error ()));
383 gdb_assert (sect_data_got == sect_data.get ());
384
385 inferior_addr = bfd_section_vma (sect);
386 if (0 != target_write_memory (inferior_addr, sect_data.get (),
387 bfd_section_size (sect)))
388 error (_("Cannot write compiled module \"%s\" section \"%s\" "
389 "to inferior memory range %s-%s."),
390 bfd_get_filename (abfd), bfd_section_name (sect),
391 paddress (current_inferior ()->arch (), inferior_addr),
392 paddress (current_inferior ()->arch (),
393 inferior_addr + bfd_section_size (sect)));
394 }
395
396 /* Fetch the type of COMPILE_I_EXPR_PTR_TYPE and COMPILE_I_EXPR_VAL
397 symbols in OBJFILE so we can calculate how much memory to allocate
398 for the out parameter. This avoids needing a malloc in the generated
399 code. Throw an error if anything fails.
400 GDB first tries to compile the code with COMPILE_I_PRINT_ADDRESS_SCOPE.
401 If it finds user tries to print an array type this function returns
402 NULL. Caller will then regenerate the code with
403 COMPILE_I_PRINT_VALUE_SCOPE, recompiles it again and finally runs it.
404 This is because __auto_type array-to-pointer type conversion of
405 COMPILE_I_EXPR_VAL which gets detected by COMPILE_I_EXPR_PTR_TYPE
406 preserving the array type. */
407
408 static struct type *
409 get_out_value_type (struct symbol *func_sym, struct objfile *objfile,
410 enum compile_i_scope_types scope)
411 {
412 struct symbol *gdb_ptr_type_sym;
413 /* Initialize it just to avoid a GCC false warning. */
414 struct symbol *gdb_val_sym = NULL;
415 struct type *gdb_ptr_type, *gdb_type_from_ptr, *gdb_type, *retval;
416 /* Initialize it just to avoid a GCC false warning. */
417 const struct block *block = NULL;
418 const struct blockvector *bv;
419 int nblocks = 0;
420 int block_loop = 0;
421
422 lookup_name_info func_matcher (GCC_FE_WRAPPER_FUNCTION,
423 symbol_name_match_type::SEARCH_NAME);
424
425 bv = func_sym->symtab ()->compunit ()->blockvector ();
426 nblocks = bv->num_blocks ();
427
428 gdb_ptr_type_sym = NULL;
429 for (block_loop = 0; block_loop < nblocks; block_loop++)
430 {
431 struct symbol *function = NULL;
432 const struct block *function_block;
433
434 block = bv->block (block_loop);
435 if (block->function () != NULL)
436 continue;
437 gdb_val_sym = block_lookup_symbol (block,
438 COMPILE_I_EXPR_VAL,
439 symbol_name_match_type::SEARCH_NAME,
440 VAR_DOMAIN);
441 if (gdb_val_sym == NULL)
442 continue;
443
444 function_block = block;
445 while (function_block != bv->static_block ()
446 && function_block != bv->global_block ())
447 {
448 function_block = function_block->superblock ();
449 function = function_block->function ();
450 if (function != NULL)
451 break;
452 }
453 if (function != NULL
454 && function_block->superblock () == bv->static_block ()
455 && symbol_matches_search_name (function, func_matcher))
456 break;
457 }
458 if (block_loop == nblocks)
459 error (_("No \"%s\" symbol found"), COMPILE_I_EXPR_VAL);
460
461 gdb_type = gdb_val_sym->type ();
462 gdb_type = check_typedef (gdb_type);
463
464 gdb_ptr_type_sym = block_lookup_symbol (block, COMPILE_I_EXPR_PTR_TYPE,
465 symbol_name_match_type::SEARCH_NAME,
466 VAR_DOMAIN);
467 if (gdb_ptr_type_sym == NULL)
468 error (_("No \"%s\" symbol found"), COMPILE_I_EXPR_PTR_TYPE);
469 gdb_ptr_type = gdb_ptr_type_sym->type ();
470 gdb_ptr_type = check_typedef (gdb_ptr_type);
471 if (gdb_ptr_type->code () != TYPE_CODE_PTR)
472 error (_("Type of \"%s\" is not a pointer"), COMPILE_I_EXPR_PTR_TYPE);
473 gdb_type_from_ptr = check_typedef (gdb_ptr_type->target_type ());
474
475 if (types_deeply_equal (gdb_type, gdb_type_from_ptr))
476 {
477 if (scope != COMPILE_I_PRINT_ADDRESS_SCOPE)
478 error (_("Expected address scope in compiled module \"%s\"."),
479 objfile_name (objfile));
480 return gdb_type;
481 }
482
483 if (gdb_type->code () != TYPE_CODE_PTR)
484 error (_("Invalid type code %d of symbol \"%s\" "
485 "in compiled module \"%s\"."),
486 gdb_type_from_ptr->code (), COMPILE_I_EXPR_VAL,
487 objfile_name (objfile));
488
489 retval = gdb_type_from_ptr;
490 switch (gdb_type_from_ptr->code ())
491 {
492 case TYPE_CODE_ARRAY:
493 gdb_type_from_ptr = gdb_type_from_ptr->target_type ();
494 break;
495 case TYPE_CODE_FUNC:
496 break;
497 default:
498 error (_("Invalid type code %d of symbol \"%s\" "
499 "in compiled module \"%s\"."),
500 gdb_type_from_ptr->code (), COMPILE_I_EXPR_PTR_TYPE,
501 objfile_name (objfile));
502 }
503 if (!types_deeply_equal (gdb_type_from_ptr,
504 gdb_type->target_type ()))
505 error (_("Referenced types do not match for symbols \"%s\" and \"%s\" "
506 "in compiled module \"%s\"."),
507 COMPILE_I_EXPR_PTR_TYPE, COMPILE_I_EXPR_VAL,
508 objfile_name (objfile));
509 if (scope == COMPILE_I_PRINT_ADDRESS_SCOPE)
510 return NULL;
511 return retval;
512 }
513
514 /* Fetch the type of first parameter of FUNC_SYM.
515 Return NULL if FUNC_SYM has no parameters. Throw an error otherwise. */
516
517 static struct type *
518 get_regs_type (struct symbol *func_sym, struct objfile *objfile)
519 {
520 struct type *func_type = func_sym->type ();
521 struct type *regsp_type, *regs_type;
522
523 /* No register parameter present. */
524 if (func_type->num_fields () == 0)
525 return NULL;
526
527 regsp_type = check_typedef (func_type->field (0).type ());
528 if (regsp_type->code () != TYPE_CODE_PTR)
529 error (_("Invalid type code %d of first parameter of function \"%s\" "
530 "in compiled module \"%s\"."),
531 regsp_type->code (), GCC_FE_WRAPPER_FUNCTION,
532 objfile_name (objfile));
533
534 regs_type = check_typedef (regsp_type->target_type ());
535 if (regs_type->code () != TYPE_CODE_STRUCT)
536 error (_("Invalid type code %d of dereferenced first parameter "
537 "of function \"%s\" in compiled module \"%s\"."),
538 regs_type->code (), GCC_FE_WRAPPER_FUNCTION,
539 objfile_name (objfile));
540
541 return regs_type;
542 }
543
544 /* Store all inferior registers required by REGS_TYPE to inferior memory
545 starting at inferior address REGS_BASE. */
546
547 static void
548 store_regs (struct type *regs_type, CORE_ADDR regs_base)
549 {
550 gdbarch *gdbarch = current_inferior ()->arch ();
551 int fieldno;
552
553 for (fieldno = 0; fieldno < regs_type->num_fields (); fieldno++)
554 {
555 const char *reg_name = regs_type->field (fieldno).name ();
556 ULONGEST reg_bitpos = regs_type->field (fieldno).loc_bitpos ();
557 ULONGEST reg_bitsize = regs_type->field (fieldno).bitsize ();
558 ULONGEST reg_offset;
559 struct type *reg_type
560 = check_typedef (regs_type->field (fieldno).type ());
561 ULONGEST reg_size = reg_type->length ();
562 int regnum;
563 struct value *regval;
564 CORE_ADDR inferior_addr;
565
566 if (strcmp (reg_name, COMPILE_I_SIMPLE_REGISTER_DUMMY) == 0)
567 continue;
568
569 if ((reg_bitpos % 8) != 0 || reg_bitsize != 0)
570 error (_("Invalid register \"%s\" position %s bits or size %s bits"),
571 reg_name, pulongest (reg_bitpos), pulongest (reg_bitsize));
572 reg_offset = reg_bitpos / 8;
573
574 if (reg_type->code () != TYPE_CODE_INT
575 && reg_type->code () != TYPE_CODE_PTR)
576 error (_("Invalid register \"%s\" type code %d"), reg_name,
577 reg_type->code ());
578
579 regnum = compile_register_name_demangle (gdbarch, reg_name);
580
581 regval = value_from_register (reg_type, regnum, get_current_frame ());
582 if (regval->optimized_out ())
583 error (_("Register \"%s\" is optimized out."), reg_name);
584 if (!regval->entirely_available ())
585 error (_("Register \"%s\" is not available."), reg_name);
586
587 inferior_addr = regs_base + reg_offset;
588 if (0 != target_write_memory (inferior_addr,
589 regval->contents ().data (),
590 reg_size))
591 error (_("Cannot write register \"%s\" to inferior memory at %s."),
592 reg_name, paddress (gdbarch, inferior_addr));
593 }
594 }
595
596 /* Load the object file specified in FILE_NAMES into inferior memory.
597 Throw an error otherwise. Caller must fully dispose the return
598 value by calling compile_object_run. Returns NULL only for
599 COMPILE_I_PRINT_ADDRESS_SCOPE when COMPILE_I_PRINT_VALUE_SCOPE
600 should have been used instead. */
601
602 compile_module_up
603 compile_object_load (const compile_file_names &file_names,
604 enum compile_i_scope_types scope, void *scope_data)
605 {
606 CORE_ADDR regs_addr, out_value_addr = 0;
607 struct symbol *func_sym;
608 struct type *func_type;
609 struct bound_minimal_symbol bmsym;
610 long storage_needed;
611 asymbol **symbol_table, **symp;
612 long number_of_symbols, missing_symbols;
613 struct type *regs_type, *out_value_type = NULL;
614 char **matching;
615 struct objfile *objfile;
616 int expect_parameters;
617 struct type *expect_return_type;
618
619 gdb::unique_xmalloc_ptr<char> filename
620 (tilde_expand (file_names.object_file ()));
621
622 gdb_bfd_ref_ptr abfd (gdb_bfd_open (filename.get (), gnutarget));
623 if (abfd == NULL)
624 error (_("\"%s\": could not open as compiled module: %s"),
625 filename.get (), bfd_errmsg (bfd_get_error ()));
626
627 if (!bfd_check_format_matches (abfd.get (), bfd_object, &matching))
628 error (_("\"%s\": not in loadable format: %s"),
629 filename.get (),
630 gdb_bfd_errmsg (bfd_get_error (), matching).c_str ());
631
632 if ((bfd_get_file_flags (abfd.get ()) & (EXEC_P | DYNAMIC)) != 0)
633 error (_("\"%s\": not in object format."), filename.get ());
634
635 struct setup_sections_data setup_sections_data (abfd.get ());
636 for (asection *sect = abfd->sections; sect != nullptr; sect = sect->next)
637 setup_sections_data.setup_one_section (sect);
638 setup_sections_data.setup_one_section (nullptr);
639
640 storage_needed = bfd_get_symtab_upper_bound (abfd.get ());
641 if (storage_needed < 0)
642 error (_("Cannot read symbols of compiled module \"%s\": %s"),
643 filename.get (), bfd_errmsg (bfd_get_error ()));
644
645 /* SYMFILE_VERBOSE is not passed even if FROM_TTY, user is not interested in
646 "Reading symbols from ..." message for automatically generated file. */
647 objfile_up objfile_holder (symbol_file_add_from_bfd (abfd,
648 filename.get (),
649 0, NULL, 0, NULL));
650 objfile = objfile_holder.get ();
651
652 func_sym = lookup_global_symbol_from_objfile (objfile,
653 GLOBAL_BLOCK,
654 GCC_FE_WRAPPER_FUNCTION,
655 VAR_DOMAIN).symbol;
656 if (func_sym == NULL)
657 error (_("Cannot find function \"%s\" in compiled module \"%s\"."),
658 GCC_FE_WRAPPER_FUNCTION, objfile_name (objfile));
659 func_type = func_sym->type ();
660 if (func_type->code () != TYPE_CODE_FUNC)
661 error (_("Invalid type code %d of function \"%s\" in compiled "
662 "module \"%s\"."),
663 func_type->code (), GCC_FE_WRAPPER_FUNCTION,
664 objfile_name (objfile));
665
666 switch (scope)
667 {
668 case COMPILE_I_SIMPLE_SCOPE:
669 expect_parameters = 1;
670 expect_return_type
671 = builtin_type (current_inferior ()->arch ())->builtin_void;
672 break;
673 case COMPILE_I_RAW_SCOPE:
674 expect_parameters = 0;
675 expect_return_type
676 = builtin_type (current_inferior ()->arch ())->builtin_void;
677 break;
678 case COMPILE_I_PRINT_ADDRESS_SCOPE:
679 case COMPILE_I_PRINT_VALUE_SCOPE:
680 expect_parameters = 2;
681 expect_return_type
682 = builtin_type (current_inferior ()->arch ())->builtin_void;
683 break;
684 default:
685 internal_error (_("invalid scope %d"), scope);
686 }
687 if (func_type->num_fields () != expect_parameters)
688 error (_("Invalid %d parameters of function \"%s\" in compiled "
689 "module \"%s\"."),
690 func_type->num_fields (), GCC_FE_WRAPPER_FUNCTION,
691 objfile_name (objfile));
692 if (!types_deeply_equal (expect_return_type, func_type->target_type ()))
693 error (_("Invalid return type of function \"%s\" in compiled "
694 "module \"%s\"."),
695 GCC_FE_WRAPPER_FUNCTION, objfile_name (objfile));
696
697 /* The memory may be later needed
698 by bfd_generic_get_relocated_section_contents
699 called from default_symfile_relocate. */
700 symbol_table = (asymbol **) obstack_alloc (&objfile->objfile_obstack,
701 storage_needed);
702 number_of_symbols = bfd_canonicalize_symtab (abfd.get (), symbol_table);
703 if (number_of_symbols < 0)
704 error (_("Cannot parse symbols of compiled module \"%s\": %s"),
705 filename.get (), bfd_errmsg (bfd_get_error ()));
706
707 missing_symbols = 0;
708 for (symp = symbol_table; symp < symbol_table + number_of_symbols; symp++)
709 {
710 asymbol *sym = *symp;
711
712 if (sym->flags != 0)
713 continue;
714 sym->flags = BSF_GLOBAL;
715 sym->section = bfd_abs_section_ptr;
716 if (strcmp (sym->name, "_GLOBAL_OFFSET_TABLE_") == 0)
717 {
718 if (compile_debug)
719 gdb_printf (gdb_stdlog,
720 "ELF symbol \"%s\" relocated to zero\n",
721 sym->name);
722
723 /* It seems to be a GCC bug, with -mcmodel=large there should be no
724 need for _GLOBAL_OFFSET_TABLE_. Together with -fPIE the data
725 remain PC-relative even with _GLOBAL_OFFSET_TABLE_ as zero. */
726 sym->value = 0;
727 continue;
728 }
729 if (strcmp (sym->name, ".TOC.") == 0)
730 {
731 /* Handle the .TOC. symbol as the linker would do. Set the .TOC.
732 sections value to 0x8000 (see bfd/elf64-ppc.c TOC_BASE_OFF);
733 point the symbol section at the ".toc" section;
734 and pass the toc->vma value into bfd_set_gp_value().
735 If the .toc section is not found, use the first section
736 with the SEC_ALLOC flag set. In the unlikely case that
737 we still do not have a section identified, fall back to using
738 the "*ABS*" section. */
739 asection *toc_fallback = bfd_get_section_by_name(abfd.get(), ".toc");
740 if (toc_fallback == NULL)
741 {
742 for (asection *tsect = abfd->sections; tsect != nullptr;
743 tsect = tsect->next)
744 {
745 if (bfd_section_flags (tsect) & SEC_ALLOC)
746 {
747 toc_fallback = tsect;
748 break;
749 }
750 }
751 }
752
753 if (toc_fallback == NULL)
754 /* If we've gotten here, we have not found a section usable
755 as a backup for the .toc section. In this case, use the
756 absolute (*ABS*) section. */
757 toc_fallback = bfd_abs_section_ptr;
758
759 sym->section = toc_fallback;
760 sym->value = 0x8000;
761 bfd_set_gp_value(abfd.get(), toc_fallback->vma);
762 if (compile_debug)
763 gdb_printf (gdb_stdlog,
764 "Connecting ELF symbol \"%s\" to the .toc section (%s)\n",
765 sym->name,
766 paddress (current_inferior ()->arch (), sym->value));
767 continue;
768 }
769
770 bmsym = lookup_minimal_symbol (sym->name, NULL, NULL);
771 switch (bmsym.minsym == NULL
772 ? mst_unknown : bmsym.minsym->type ())
773 {
774 case mst_text:
775 case mst_bss:
776 case mst_data:
777 sym->value = bmsym.value_address ();
778 if (compile_debug)
779 gdb_printf (gdb_stdlog,
780 "ELF mst_text symbol \"%s\" relocated to %s\n",
781 sym->name,
782 paddress (current_inferior ()->arch (), sym->value));
783 break;
784 case mst_text_gnu_ifunc:
785 sym->value = gnu_ifunc_resolve_addr (current_inferior ()->arch (),
786 bmsym.value_address ());
787 if (compile_debug)
788 gdb_printf (gdb_stdlog,
789 "ELF mst_text_gnu_ifunc symbol \"%s\" "
790 "relocated to %s\n",
791 sym->name,
792 paddress (current_inferior ()->arch (), sym->value));
793 break;
794 default:
795 warning (_("Could not find symbol \"%s\" "
796 "for compiled module \"%s\"."),
797 sym->name, filename.get ());
798 missing_symbols++;
799 }
800 }
801 if (missing_symbols)
802 error (_("%ld symbols were missing, cannot continue."), missing_symbols);
803
804 bfd_map_over_sections (abfd.get (), copy_sections, symbol_table);
805
806 regs_type = get_regs_type (func_sym, objfile);
807 if (regs_type == NULL)
808 regs_addr = 0;
809 else
810 {
811 /* Use read-only non-executable memory protection. */
812 regs_addr = gdbarch_infcall_mmap (current_inferior ()->arch (),
813 regs_type->length (),
814 GDB_MMAP_PROT_READ);
815 gdb_assert (regs_addr != 0);
816 setup_sections_data.munmap_list.add (regs_addr, regs_type->length ());
817 if (compile_debug)
818 gdb_printf (gdb_stdlog,
819 "allocated %s bytes at %s for registers\n",
820 paddress (current_inferior ()->arch (),
821 regs_type->length ()),
822 paddress (current_inferior ()->arch (), regs_addr));
823 store_regs (regs_type, regs_addr);
824 }
825
826 if (scope == COMPILE_I_PRINT_ADDRESS_SCOPE
827 || scope == COMPILE_I_PRINT_VALUE_SCOPE)
828 {
829 out_value_type = get_out_value_type (func_sym, objfile, scope);
830 if (out_value_type == NULL)
831 return NULL;
832 check_typedef (out_value_type);
833 out_value_addr = gdbarch_infcall_mmap (current_inferior ()->arch (),
834 out_value_type->length (),
835 (GDB_MMAP_PROT_READ
836 | GDB_MMAP_PROT_WRITE));
837 gdb_assert (out_value_addr != 0);
838 setup_sections_data.munmap_list.add (out_value_addr,
839 out_value_type->length ());
840 if (compile_debug)
841 gdb_printf (gdb_stdlog,
842 "allocated %s bytes at %s for printed value\n",
843 paddress (current_inferior ()->arch (),
844 out_value_type->length ()),
845 paddress (current_inferior ()->arch (), out_value_addr));
846 }
847
848 compile_module_up retval (new struct compile_module);
849 retval->objfile = objfile_holder.release ();
850 retval->source_file = file_names.source_file ();
851 retval->func_sym = func_sym;
852 retval->regs_addr = regs_addr;
853 retval->scope = scope;
854 retval->scope_data = scope_data;
855 retval->out_value_type = out_value_type;
856 retval->out_value_addr = out_value_addr;
857 retval->munmap_list = std::move (setup_sections_data.munmap_list);
858
859 return retval;
860 }