]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/solib-spu.c
[binutils, ARM, 4/16] BF insns infrastructure with array of relocs in struct arm_it
[thirdparty/binutils-gdb.git] / gdb / solib-spu.c
CommitLineData
85e747d2 1/* Cell SPU GNU/Linux support -- shared library handling.
42a4f53d 2 Copyright (C) 2009-2019 Free Software Foundation, Inc.
85e747d2
UW
3
4 Contributed by Ulrich Weigand <uweigand@de.ibm.com>.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
dcf7800b 10 the Free Software Foundation; either version 3 of the License, or
85e747d2
UW
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
dcf7800b 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
85e747d2
UW
20
21#include "defs.h"
693be288 22#include "solib-spu.h"
85e747d2 23#include "gdbcore.h"
53ce3c39 24#include <sys/stat.h>
85e747d2
UW
25#include "arch-utils.h"
26#include "bfd.h"
27#include "symtab.h"
28#include "solib.h"
29#include "solib-svr4.h"
30#include "solist.h"
31#include "inferior.h"
32#include "objfiles.h"
76727919 33#include "observable.h"
85e747d2
UW
34#include "breakpoint.h"
35#include "gdbthread.h"
cbb099e8 36#include "gdb_bfd.h"
85e747d2
UW
37
38#include "spu-tdep.h"
39
40/* Highest SPE id (file handle) the inferior may have. */
41#define MAX_SPE_FD 1024
42
43/* Stand-alone SPE executable? */
44#define spu_standalone_p() \
45 (symfile_objfile && symfile_objfile->obfd \
46 && bfd_get_arch (symfile_objfile->obfd) == bfd_arch_spu)
47
48
49/* Relocate main SPE executable. */
50static void
51spu_relocate_main_executable (int spufs_fd)
52{
85e747d2
UW
53 struct section_offsets *new_offsets;
54 int i;
55
567995e1
JK
56 if (symfile_objfile == NULL)
57 return;
85e747d2 58
224c3ddb
SM
59 new_offsets = XALLOCAVEC (struct section_offsets,
60 symfile_objfile->num_sections);
85e747d2 61
567995e1
JK
62 for (i = 0; i < symfile_objfile->num_sections; i++)
63 new_offsets->offsets[i] = SPUADDR (spufs_fd, 0);
64
65 objfile_relocate (symfile_objfile, new_offsets);
85e747d2
UW
66}
67
68/* When running a stand-alone SPE executable, we may need to skip one more
69 exec event on startup, to get past the binfmt_misc loader. */
70static void
71spu_skip_standalone_loader (void)
72{
73 if (target_has_execution && !current_inferior ()->attach_flag)
74 {
75 struct target_waitstatus ws;
76
77 /* Only some kernels report an extra SIGTRAP with the binfmt_misc
78 loader; others do not. In addition, if we have attached to an
79 already running inferior instead of starting a new one, we will
80 not see the extra SIGTRAP -- and we cannot readily distinguish
81 the two cases, in particular with the extended-remote target.
82
83 Thus we issue a single-step here. If no extra SIGTRAP was pending,
84 this will step past the first instruction of the stand-alone SPE
85 executable loader, but we don't care about that. */
86
16c381f0 87 inferior_thread ()->control.in_infcall = 1; /* Suppress MI messages. */
85e747d2 88
a493e3e2 89 target_resume (inferior_ptid, 1, GDB_SIGNAL_0);
85e747d2
UW
90 target_wait (minus_one_ptid, &ws, 0);
91 set_executing (minus_one_ptid, 0);
92
16c381f0 93 inferior_thread ()->control.in_infcall = 0;
85e747d2
UW
94 }
95}
96
f1d8ee66
UW
97static const struct objfile_data *ocl_program_data_key;
98
99/* Appends OpenCL programs to the list of `struct so_list' objects. */
100static void
101append_ocl_sos (struct so_list **link_ptr)
102{
103 CORE_ADDR *ocl_program_addr_base;
f1d8ee66 104
2030c079 105 for (objfile *objfile : current_program_space->objfiles ())
f1d8ee66 106 {
19ba03f4
SM
107 ocl_program_addr_base
108 = (CORE_ADDR *) objfile_data (objfile, ocl_program_data_key);
f1d8ee66
UW
109 if (ocl_program_addr_base != NULL)
110 {
111 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd)?
112 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
a70b8144 113 try
f1d8ee66
UW
114 {
115 CORE_ADDR data =
116 read_memory_unsigned_integer (*ocl_program_addr_base,
117 sizeof (CORE_ADDR),
118 byte_order);
119 if (data != 0x0)
120 {
fe978cb0 121 struct so_list *newobj;
f1d8ee66
UW
122
123 /* Allocate so_list structure. */
fe978cb0 124 newobj = XCNEW (struct so_list);
f1d8ee66
UW
125
126 /* Encode FD and object ID in path name. */
fe978cb0 127 xsnprintf (newobj->so_name, sizeof newobj->so_name, "@%s <%d>",
070c8028
UW
128 hex_string (data),
129 SPUADDR_SPU (*ocl_program_addr_base));
fe978cb0 130 strcpy (newobj->so_original_name, newobj->so_name);
f1d8ee66 131
fe978cb0
PA
132 *link_ptr = newobj;
133 link_ptr = &newobj->next;
f1d8ee66
UW
134 }
135 }
230d2906 136 catch (const gdb_exception &ex)
f1d8ee66
UW
137 {
138 /* Ignore memory errors. */
139 switch (ex.error)
140 {
141 case MEMORY_ERROR:
142 break;
143 default:
eedc3f4f 144 throw;
f1d8ee66
UW
145 break;
146 }
147 }
148 }
149 }
150}
151
85e747d2
UW
152/* Build a list of `struct so_list' objects describing the shared
153 objects currently loaded in the inferior. */
154static struct so_list *
155spu_current_sos (void)
156{
f5656ead 157 enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
85e747d2
UW
158 struct so_list *head;
159 struct so_list **link_ptr;
160
e362b510 161 gdb_byte buf[MAX_SPE_FD * 4];
85e747d2
UW
162 int i, size;
163
164 /* First, retrieve the SVR4 shared library list. */
165 head = svr4_so_ops.current_sos ();
166
167 /* Append our libraries to the end of the list. */
168 for (link_ptr = &head; *link_ptr; link_ptr = &(*link_ptr)->next)
169 ;
170
171 /* Determine list of SPU ids. */
8b88a78e 172 size = target_read (current_top_target (), TARGET_OBJECT_SPU, NULL,
85e747d2
UW
173 buf, 0, sizeof buf);
174
175 /* Do not add stand-alone SPE executable context as shared library,
176 but relocate main SPE executable objfile. */
177 if (spu_standalone_p ())
178 {
179 if (size == 4)
180 {
181 int fd = extract_unsigned_integer (buf, 4, byte_order);
433759f7 182
85e747d2
UW
183 spu_relocate_main_executable (fd);
184
185 /* Re-enable breakpoints after main SPU context was established;
186 see also comments in spu_solib_create_inferior_hook. */
187 enable_breakpoints_after_startup ();
188 }
189
190 return head;
191 }
192
193 /* Create an so_list entry for each SPU id. */
194 for (i = 0; i < size; i += 4)
195 {
196 int fd = extract_unsigned_integer (buf + i, 4, byte_order);
fe978cb0 197 struct so_list *newobj;
85e747d2
UW
198
199 unsigned long long addr;
200 char annex[32], id[100];
201 int len;
202
203 /* Read object ID. There's a race window where the inferior may have
204 already created the SPE context, but not installed the object-id
205 yet. Skip such entries; we'll be back for them later. */
206 xsnprintf (annex, sizeof annex, "%d/object-id", fd);
8b88a78e 207 len = target_read (current_top_target (), TARGET_OBJECT_SPU, annex,
001f13d8 208 (gdb_byte *) id, 0, sizeof id);
85e747d2
UW
209 if (len <= 0 || len >= sizeof id)
210 continue;
211 id[len] = 0;
212 if (sscanf (id, "0x%llx", &addr) != 1 || !addr)
213 continue;
214
215 /* Allocate so_list structure. */
fe978cb0 216 newobj = XCNEW (struct so_list);
85e747d2
UW
217
218 /* Encode FD and object ID in path name. Choose the name so as not
219 to conflict with any (normal) SVR4 library path name. */
fe978cb0 220 xsnprintf (newobj->so_name, sizeof newobj->so_name, "@%s <%d>",
2244ba2e 221 hex_string (addr), fd);
fe978cb0 222 strcpy (newobj->so_original_name, newobj->so_name);
85e747d2 223
fe978cb0
PA
224 *link_ptr = newobj;
225 link_ptr = &newobj->next;
85e747d2
UW
226 }
227
c378eb4e 228 /* Append OpenCL sos. */
f1d8ee66
UW
229 append_ocl_sos (link_ptr);
230
85e747d2
UW
231 return head;
232}
233
234/* Free so_list information. */
235static void
236spu_free_so (struct so_list *so)
237{
238 if (so->so_original_name[0] != '@')
239 svr4_so_ops.free_so (so);
240}
241
242/* Relocate section addresses. */
243static void
244spu_relocate_section_addresses (struct so_list *so,
245 struct target_section *sec)
246{
247 if (so->so_original_name[0] != '@')
248 svr4_so_ops.relocate_section_addresses (so, sec);
249 else
250 {
251 unsigned long long addr;
252 int fd;
253
254 /* Set addr_low/high to just LS offset for display. */
255 if (so->addr_low == 0 && so->addr_high == 0
256 && strcmp (sec->the_bfd_section->name, ".text") == 0)
257 {
258 so->addr_low = sec->addr;
259 so->addr_high = sec->endaddr;
260 }
261
262 /* Decode object ID. */
263 if (sscanf (so->so_original_name, "@0x%llx <%d>", &addr, &fd) != 2)
264 internal_error (__FILE__, __LINE__, "bad object ID");
265
266 sec->addr = SPUADDR (fd, sec->addr);
267 sec->endaddr = SPUADDR (fd, sec->endaddr);
268 }
269}
270
271
272/* Inferior memory should contain an SPE executable image at location ADDR.
273 Allocate a BFD representing that executable. Return NULL on error. */
274
275static void *
276spu_bfd_iovec_open (bfd *nbfd, void *open_closure)
277{
278 return open_closure;
279}
280
281static int
282spu_bfd_iovec_close (bfd *nbfd, void *stream)
283{
284 xfree (stream);
39ed5604
JK
285
286 /* Zero means success. */
287 return 0;
85e747d2
UW
288}
289
290static file_ptr
291spu_bfd_iovec_pread (bfd *abfd, void *stream, void *buf,
292 file_ptr nbytes, file_ptr offset)
293{
294 CORE_ADDR addr = *(CORE_ADDR *)stream;
295 int ret;
296
5af9928d 297 ret = target_read_memory (addr + offset, (gdb_byte *) buf, nbytes);
85e747d2
UW
298 if (ret != 0)
299 {
300 bfd_set_error (bfd_error_invalid_operation);
301 return -1;
302 }
303
304 return nbytes;
305}
306
307static int
308spu_bfd_iovec_stat (bfd *abfd, void *stream, struct stat *sb)
309{
310 /* We don't have an easy way of finding the size of embedded spu
311 images. We could parse the in-memory ELF header and section
312 table to find the extent of the last section but that seems
313 pointless when the size is needed only for checks of other
314 parsed values in dbxread.c. */
326a5c7e 315 memset (sb, 0, sizeof (struct stat));
85e747d2
UW
316 sb->st_size = INT_MAX;
317 return 0;
318}
319
192b62ce 320static gdb_bfd_ref_ptr
692d6f97 321spu_bfd_fopen (const char *name, CORE_ADDR addr)
85e747d2 322{
8d749320 323 CORE_ADDR *open_closure = XNEW (CORE_ADDR);
85e747d2 324
85e747d2
UW
325 *open_closure = addr;
326
192b62ce
TT
327 gdb_bfd_ref_ptr nbfd (gdb_bfd_openr_iovec (name, "elf32-spu",
328 spu_bfd_iovec_open, open_closure,
329 spu_bfd_iovec_pread,
330 spu_bfd_iovec_close,
331 spu_bfd_iovec_stat));
332 if (nbfd == NULL)
85e747d2
UW
333 return NULL;
334
192b62ce
TT
335 if (!bfd_check_format (nbfd.get (), bfd_object))
336 return NULL;
85e747d2
UW
337
338 return nbfd;
339}
340
341/* Open shared library BFD. */
192b62ce 342static gdb_bfd_ref_ptr
692d6f97 343spu_bfd_open (const char *pathname)
85e747d2 344{
692d6f97 345 const char *original_name = strrchr (pathname, '@');
85e747d2
UW
346 asection *spu_name;
347 unsigned long long addr;
348 int fd;
349
350 /* Handle regular SVR4 libraries. */
351 if (!original_name)
352 return svr4_so_ops.bfd_open (pathname);
353
354 /* Decode object ID. */
355 if (sscanf (original_name, "@0x%llx <%d>", &addr, &fd) != 2)
356 internal_error (__FILE__, __LINE__, "bad object ID");
357
358 /* Open BFD representing SPE executable. */
192b62ce
TT
359 gdb_bfd_ref_ptr abfd (spu_bfd_fopen (original_name, (CORE_ADDR) addr));
360 if (abfd == NULL)
85e747d2
UW
361 error (_("Cannot read SPE executable at %s"), original_name);
362
363 /* Retrieve SPU name note. */
192b62ce 364 spu_name = bfd_get_section_by_name (abfd.get (), ".note.spu_name");
85e747d2
UW
365 if (spu_name)
366 {
192b62ce 367 int sect_size = bfd_section_size (abfd.get (), spu_name);
433759f7 368
85e747d2
UW
369 if (sect_size > 20)
370 {
224c3ddb
SM
371 char *buf
372 = (char *) alloca (sect_size - 20 + strlen (original_name) + 1);
433759f7 373
192b62ce
TT
374 bfd_get_section_contents (abfd.get (), spu_name, buf, 20,
375 sect_size - 20);
85e747d2
UW
376 buf[sect_size - 20] = '\0';
377
378 strcat (buf, original_name);
379
380 xfree ((char *)abfd->filename);
381 abfd->filename = xstrdup (buf);
382 }
383 }
384
385 return abfd;
386}
387
388/* Lookup global symbol in a SPE executable. */
d12307c1 389static struct block_symbol
efad9b6a 390spu_lookup_lib_symbol (struct objfile *objfile,
85e747d2 391 const char *name,
85e747d2
UW
392 const domain_enum domain)
393{
394 if (bfd_get_arch (objfile->obfd) == bfd_arch_spu)
94af9270 395 return lookup_global_symbol_from_objfile (objfile, name, domain);
85e747d2
UW
396
397 if (svr4_so_ops.lookup_lib_global_symbol != NULL)
94af9270 398 return svr4_so_ops.lookup_lib_global_symbol (objfile, name, domain);
6640a367 399 return {};
85e747d2
UW
400}
401
402/* Enable shared library breakpoint. */
403static int
404spu_enable_break (struct objfile *objfile)
405{
3b7344d5 406 struct bound_minimal_symbol spe_event_sym;
85e747d2
UW
407
408 /* The libspe library will call __spe_context_update_event whenever any
409 SPE context is allocated or destroyed. */
410 spe_event_sym = lookup_minimal_symbol ("__spe_context_update_event",
411 NULL, objfile);
412
413 /* Place a solib_event breakpoint on the symbol. */
3b7344d5 414 if (spe_event_sym.minsym)
85e747d2 415 {
77e371c0 416 CORE_ADDR addr = BMSYMBOL_VALUE_ADDRESS (spe_event_sym);
433759f7 417
f5656ead 418 addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch (), addr,
8b88a78e 419 current_top_target ());
f5656ead 420 create_solib_event_breakpoint (target_gdbarch (), addr);
85e747d2
UW
421 return 1;
422 }
423
424 return 0;
425}
426
f1d8ee66
UW
427/* Enable shared library breakpoint for the
428 OpenCL runtime running on the SPU. */
429static void
430ocl_enable_break (struct objfile *objfile)
431{
3b7344d5
TT
432 struct bound_minimal_symbol event_sym;
433 struct bound_minimal_symbol addr_sym;
f1d8ee66
UW
434
435 /* The OpenCL runtime on the SPU will call __opencl_program_update_event
436 whenever an OpenCL program is loaded. */
437 event_sym = lookup_minimal_symbol ("__opencl_program_update_event", NULL,
438 objfile);
439 /* The PPU address of the OpenCL program can be found
440 at opencl_elf_image_address. */
441 addr_sym = lookup_minimal_symbol ("opencl_elf_image_address", NULL, objfile);
442
3b7344d5 443 if (event_sym.minsym && addr_sym.minsym)
f1d8ee66
UW
444 {
445 /* Place a solib_event breakpoint on the symbol. */
77e371c0 446 CORE_ADDR event_addr = BMSYMBOL_VALUE_ADDRESS (event_sym);
f1d8ee66
UW
447 create_solib_event_breakpoint (get_objfile_arch (objfile), event_addr);
448
449 /* Store the address of the symbol that will point to OpenCL program
450 using the per-objfile private data mechanism. */
451 if (objfile_data (objfile, ocl_program_data_key) == NULL)
452 {
453 CORE_ADDR *ocl_program_addr_base = OBSTACK_CALLOC (
454 &objfile->objfile_obstack,
455 objfile->sections_end - objfile->sections,
456 CORE_ADDR);
77e371c0 457 *ocl_program_addr_base = BMSYMBOL_VALUE_ADDRESS (addr_sym);
f1d8ee66
UW
458 set_objfile_data (objfile, ocl_program_data_key,
459 ocl_program_addr_base);
460 }
461 }
462}
463
85e747d2
UW
464/* Create inferior hook. */
465static void
268a4a75 466spu_solib_create_inferior_hook (int from_tty)
85e747d2 467{
85e747d2
UW
468 /* Handle SPE stand-alone executables. */
469 if (spu_standalone_p ())
470 {
471 /* After an SPE stand-alone executable was loaded, we'll receive
472 an additional trap due to the binfmt_misc handler. Make sure
473 to skip that trap. */
474 spu_skip_standalone_loader ();
475
476 /* If the user established breakpoints before starting the inferior, GDB
477 would attempt to insert those now. This would fail because the SPU
478 context has not yet been created and the SPU executable has not yet
479 been loaded. To prevent such failures, we disable all user-created
480 breakpoints now; they will be re-enabled in spu_current_sos once the
481 main SPU context has been detected. */
482 disable_breakpoints_before_startup ();
483
484 /* A special case arises when re-starting an executable, because at
485 this point it still resides at the relocated address range that was
486 determined during its last execution. We need to undo the relocation
487 so that that multi-architecture target recognizes the stand-alone
488 initialization special case. */
489 spu_relocate_main_executable (-1);
490 }
491
492 /* Call SVR4 hook -- this will re-insert the SVR4 solib breakpoints. */
268a4a75 493 svr4_so_ops.solib_create_inferior_hook (from_tty);
85e747d2
UW
494
495 /* If the inferior is statically linked against libspe, we need to install
496 our own solib breakpoint right now. Otherwise, it will be installed by
497 the solib_loaded observer below as soon as libspe is loaded. */
498 spu_enable_break (NULL);
499}
500
501/* Install SPE "shared library" handling. This is called by -tdep code
502 that wants to support SPU as a secondary architecture. */
503void
504set_spu_solib_ops (struct gdbarch *gdbarch)
505{
506 static struct target_so_ops spu_so_ops;
507
508 /* Initialize this lazily, to avoid an initialization order
509 dependency on solib-svr4.c's _initialize routine. */
510 if (spu_so_ops.current_sos == NULL)
511 {
512 spu_so_ops = svr4_so_ops;
513 spu_so_ops.solib_create_inferior_hook = spu_solib_create_inferior_hook;
514 spu_so_ops.relocate_section_addresses = spu_relocate_section_addresses;
515 spu_so_ops.free_so = spu_free_so;
516 spu_so_ops.current_sos = spu_current_sos;
517 spu_so_ops.bfd_open = spu_bfd_open;
518 spu_so_ops.lookup_lib_global_symbol = spu_lookup_lib_symbol;
519 }
520
521 set_solib_ops (gdbarch, &spu_so_ops);
522}
523
524/* Observer for the solib_loaded event. Used to install our breakpoint
525 if libspe is a shared library. */
526static void
527spu_solib_loaded (struct so_list *so)
528{
529 if (strstr (so->so_original_name, "/libspe") != NULL)
530 {
7e559477 531 solib_read_symbols (so, 0);
85e747d2
UW
532 spu_enable_break (so->objfile);
533 }
f1d8ee66
UW
534 /* In case the OpenCL runtime is loaded we install a breakpoint
535 to get notified whenever an OpenCL program gets loaded. */
536 if (strstr (so->so_name, "CLRuntimeAccelCellSPU@") != NULL)
537 {
538 solib_read_symbols (so, 0);
539 ocl_enable_break (so->objfile);
540 }
85e747d2
UW
541}
542
543void
544_initialize_spu_solib (void)
545{
76727919 546 gdb::observers::solib_loaded.attach (spu_solib_loaded);
f1d8ee66 547 ocl_program_data_key = register_objfile_data ();
85e747d2
UW
548}
549