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