1 /* Cell SPU GNU/Linux support -- shared library handling.
2 Copyright (C) 2009-2012 Free Software Foundation, Inc.
4 Contributed by Ulrich Weigand <uweigand@de.ibm.com>.
6 This file is part of GDB.
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
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "solib-spu.h"
24 #include "gdb_string.h"
25 #include "gdb_assert.h"
27 #include "arch-utils.h"
31 #include "solib-svr4.h"
36 #include "breakpoint.h"
37 #include "gdbthread.h"
38 #include "exceptions.h"
43 /* Highest SPE id (file handle) the inferior may have. */
44 #define MAX_SPE_FD 1024
46 /* Stand-alone SPE executable? */
47 #define spu_standalone_p() \
48 (symfile_objfile && symfile_objfile->obfd \
49 && bfd_get_arch (symfile_objfile->obfd) == bfd_arch_spu)
52 /* Relocate main SPE executable. */
54 spu_relocate_main_executable (int spufs_fd
)
56 struct section_offsets
*new_offsets
;
59 if (symfile_objfile
== NULL
)
62 new_offsets
= alloca (symfile_objfile
->num_sections
63 * sizeof (struct section_offsets
));
65 for (i
= 0; i
< symfile_objfile
->num_sections
; i
++)
66 new_offsets
->offsets
[i
] = SPUADDR (spufs_fd
, 0);
68 objfile_relocate (symfile_objfile
, new_offsets
);
71 /* When running a stand-alone SPE executable, we may need to skip one more
72 exec event on startup, to get past the binfmt_misc loader. */
74 spu_skip_standalone_loader (void)
76 if (target_has_execution
&& !current_inferior ()->attach_flag
)
78 struct target_waitstatus ws
;
80 /* Only some kernels report an extra SIGTRAP with the binfmt_misc
81 loader; others do not. In addition, if we have attached to an
82 already running inferior instead of starting a new one, we will
83 not see the extra SIGTRAP -- and we cannot readily distinguish
84 the two cases, in particular with the extended-remote target.
86 Thus we issue a single-step here. If no extra SIGTRAP was pending,
87 this will step past the first instruction of the stand-alone SPE
88 executable loader, but we don't care about that. */
90 inferior_thread ()->control
.in_infcall
= 1; /* Suppress MI messages. */
92 target_resume (inferior_ptid
, 1, GDB_SIGNAL_0
);
93 target_wait (minus_one_ptid
, &ws
, 0);
94 set_executing (minus_one_ptid
, 0);
96 inferior_thread ()->control
.in_infcall
= 0;
100 static const struct objfile_data
*ocl_program_data_key
;
102 /* Appends OpenCL programs to the list of `struct so_list' objects. */
104 append_ocl_sos (struct so_list
**link_ptr
)
106 CORE_ADDR
*ocl_program_addr_base
;
107 struct objfile
*objfile
;
109 ALL_OBJFILES (objfile
)
111 ocl_program_addr_base
= objfile_data (objfile
, ocl_program_data_key
);
112 if (ocl_program_addr_base
!= NULL
)
114 enum bfd_endian byte_order
= bfd_big_endian (objfile
->obfd
)?
115 BFD_ENDIAN_BIG
: BFD_ENDIAN_LITTLE
;
116 volatile struct gdb_exception ex
;
117 TRY_CATCH (ex
, RETURN_MASK_ALL
)
120 read_memory_unsigned_integer (*ocl_program_addr_base
,
127 /* Allocate so_list structure. */
128 new = XZALLOC (struct so_list
);
130 /* Encode FD and object ID in path name. */
131 xsnprintf (new->so_name
, sizeof new->so_name
, "@%s <%d>",
133 SPUADDR_SPU (*ocl_program_addr_base
));
134 strcpy (new->so_original_name
, new->so_name
);
137 link_ptr
= &new->next
;
142 /* Ignore memory errors. */
148 throw_exception (ex
);
156 /* Build a list of `struct so_list' objects describing the shared
157 objects currently loaded in the inferior. */
158 static struct so_list
*
159 spu_current_sos (void)
161 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
162 struct so_list
*head
;
163 struct so_list
**link_ptr
;
165 char buf
[MAX_SPE_FD
* 4];
168 /* First, retrieve the SVR4 shared library list. */
169 head
= svr4_so_ops
.current_sos ();
171 /* Append our libraries to the end of the list. */
172 for (link_ptr
= &head
; *link_ptr
; link_ptr
= &(*link_ptr
)->next
)
175 /* Determine list of SPU ids. */
176 size
= target_read (¤t_target
, TARGET_OBJECT_SPU
, NULL
,
179 /* Do not add stand-alone SPE executable context as shared library,
180 but relocate main SPE executable objfile. */
181 if (spu_standalone_p ())
185 int fd
= extract_unsigned_integer (buf
, 4, byte_order
);
187 spu_relocate_main_executable (fd
);
189 /* Re-enable breakpoints after main SPU context was established;
190 see also comments in spu_solib_create_inferior_hook. */
191 enable_breakpoints_after_startup ();
197 /* Create an so_list entry for each SPU id. */
198 for (i
= 0; i
< size
; i
+= 4)
200 int fd
= extract_unsigned_integer (buf
+ i
, 4, byte_order
);
203 unsigned long long addr
;
204 char annex
[32], id
[100];
207 /* Read object ID. There's a race window where the inferior may have
208 already created the SPE context, but not installed the object-id
209 yet. Skip such entries; we'll be back for them later. */
210 xsnprintf (annex
, sizeof annex
, "%d/object-id", fd
);
211 len
= target_read (¤t_target
, TARGET_OBJECT_SPU
, annex
,
213 if (len
<= 0 || len
>= sizeof id
)
216 if (sscanf (id
, "0x%llx", &addr
) != 1 || !addr
)
219 /* Allocate so_list structure. */
220 new = XZALLOC (struct so_list
);
222 /* Encode FD and object ID in path name. Choose the name so as not
223 to conflict with any (normal) SVR4 library path name. */
224 xsnprintf (new->so_name
, sizeof new->so_name
, "@%s <%d>",
225 hex_string (addr
), fd
);
226 strcpy (new->so_original_name
, new->so_name
);
229 link_ptr
= &new->next
;
232 /* Append OpenCL sos. */
233 append_ocl_sos (link_ptr
);
238 /* Free so_list information. */
240 spu_free_so (struct so_list
*so
)
242 if (so
->so_original_name
[0] != '@')
243 svr4_so_ops
.free_so (so
);
246 /* Relocate section addresses. */
248 spu_relocate_section_addresses (struct so_list
*so
,
249 struct target_section
*sec
)
251 if (so
->so_original_name
[0] != '@')
252 svr4_so_ops
.relocate_section_addresses (so
, sec
);
255 unsigned long long addr
;
258 /* Set addr_low/high to just LS offset for display. */
259 if (so
->addr_low
== 0 && so
->addr_high
== 0
260 && strcmp (sec
->the_bfd_section
->name
, ".text") == 0)
262 so
->addr_low
= sec
->addr
;
263 so
->addr_high
= sec
->endaddr
;
266 /* Decode object ID. */
267 if (sscanf (so
->so_original_name
, "@0x%llx <%d>", &addr
, &fd
) != 2)
268 internal_error (__FILE__
, __LINE__
, "bad object ID");
270 sec
->addr
= SPUADDR (fd
, sec
->addr
);
271 sec
->endaddr
= SPUADDR (fd
, sec
->endaddr
);
276 /* Inferior memory should contain an SPE executable image at location ADDR.
277 Allocate a BFD representing that executable. Return NULL on error. */
280 spu_bfd_iovec_open (bfd
*nbfd
, void *open_closure
)
286 spu_bfd_iovec_close (bfd
*nbfd
, void *stream
)
293 spu_bfd_iovec_pread (bfd
*abfd
, void *stream
, void *buf
,
294 file_ptr nbytes
, file_ptr offset
)
296 CORE_ADDR addr
= *(CORE_ADDR
*)stream
;
299 ret
= target_read_memory (addr
+ offset
, buf
, nbytes
);
302 bfd_set_error (bfd_error_invalid_operation
);
310 spu_bfd_iovec_stat (bfd
*abfd
, void *stream
, struct stat
*sb
)
312 /* We don't have an easy way of finding the size of embedded spu
313 images. We could parse the in-memory ELF header and section
314 table to find the extent of the last section but that seems
315 pointless when the size is needed only for checks of other
316 parsed values in dbxread.c. */
317 sb
->st_size
= INT_MAX
;
322 spu_bfd_fopen (char *name
, CORE_ADDR addr
)
326 CORE_ADDR
*open_closure
= xmalloc (sizeof (CORE_ADDR
));
327 *open_closure
= addr
;
329 nbfd
= gdb_bfd_ref (bfd_openr_iovec (name
, "elf32-spu",
330 spu_bfd_iovec_open
, open_closure
,
331 spu_bfd_iovec_pread
, spu_bfd_iovec_close
,
332 spu_bfd_iovec_stat
));
336 if (!bfd_check_format (nbfd
, bfd_object
))
338 gdb_bfd_unref (nbfd
);
342 gdb_bfd_stash_filename (nbfd
);
346 /* Open shared library BFD. */
348 spu_bfd_open (char *pathname
)
350 char *original_name
= strrchr (pathname
, '@');
353 unsigned long long addr
;
356 /* Handle regular SVR4 libraries. */
358 return svr4_so_ops
.bfd_open (pathname
);
360 /* Decode object ID. */
361 if (sscanf (original_name
, "@0x%llx <%d>", &addr
, &fd
) != 2)
362 internal_error (__FILE__
, __LINE__
, "bad object ID");
364 /* Open BFD representing SPE executable. */
365 abfd
= spu_bfd_fopen (original_name
, (CORE_ADDR
) addr
);
367 error (_("Cannot read SPE executable at %s"), original_name
);
369 /* Retrieve SPU name note. */
370 spu_name
= bfd_get_section_by_name (abfd
, ".note.spu_name");
373 int sect_size
= bfd_section_size (abfd
, spu_name
);
377 char *buf
= alloca (sect_size
- 20 + strlen (original_name
) + 1);
379 bfd_get_section_contents (abfd
, spu_name
, buf
, 20, sect_size
- 20);
380 buf
[sect_size
- 20] = '\0';
382 strcat (buf
, original_name
);
384 xfree ((char *)abfd
->filename
);
385 abfd
->filename
= xstrdup (buf
);
392 /* Lookup global symbol in a SPE executable. */
393 static struct symbol
*
394 spu_lookup_lib_symbol (const struct objfile
*objfile
,
396 const domain_enum domain
)
398 if (bfd_get_arch (objfile
->obfd
) == bfd_arch_spu
)
399 return lookup_global_symbol_from_objfile (objfile
, name
, domain
);
401 if (svr4_so_ops
.lookup_lib_global_symbol
!= NULL
)
402 return svr4_so_ops
.lookup_lib_global_symbol (objfile
, name
, domain
);
406 /* Enable shared library breakpoint. */
408 spu_enable_break (struct objfile
*objfile
)
410 struct minimal_symbol
*spe_event_sym
= NULL
;
412 /* The libspe library will call __spe_context_update_event whenever any
413 SPE context is allocated or destroyed. */
414 spe_event_sym
= lookup_minimal_symbol ("__spe_context_update_event",
417 /* Place a solib_event breakpoint on the symbol. */
420 CORE_ADDR addr
= SYMBOL_VALUE_ADDRESS (spe_event_sym
);
422 addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch
, addr
,
424 create_solib_event_breakpoint (target_gdbarch
, addr
);
431 /* Enable shared library breakpoint for the
432 OpenCL runtime running on the SPU. */
434 ocl_enable_break (struct objfile
*objfile
)
436 struct minimal_symbol
*event_sym
= NULL
;
437 struct minimal_symbol
*addr_sym
= NULL
;
439 /* The OpenCL runtime on the SPU will call __opencl_program_update_event
440 whenever an OpenCL program is loaded. */
441 event_sym
= lookup_minimal_symbol ("__opencl_program_update_event", NULL
,
443 /* The PPU address of the OpenCL program can be found
444 at opencl_elf_image_address. */
445 addr_sym
= lookup_minimal_symbol ("opencl_elf_image_address", NULL
, objfile
);
447 if (event_sym
&& addr_sym
)
449 /* Place a solib_event breakpoint on the symbol. */
450 CORE_ADDR event_addr
= SYMBOL_VALUE_ADDRESS (event_sym
);
451 create_solib_event_breakpoint (get_objfile_arch (objfile
), event_addr
);
453 /* Store the address of the symbol that will point to OpenCL program
454 using the per-objfile private data mechanism. */
455 if (objfile_data (objfile
, ocl_program_data_key
) == NULL
)
457 CORE_ADDR
*ocl_program_addr_base
= OBSTACK_CALLOC (
458 &objfile
->objfile_obstack
,
459 objfile
->sections_end
- objfile
->sections
,
461 *ocl_program_addr_base
= SYMBOL_VALUE_ADDRESS (addr_sym
);
462 set_objfile_data (objfile
, ocl_program_data_key
,
463 ocl_program_addr_base
);
468 /* Create inferior hook. */
470 spu_solib_create_inferior_hook (int from_tty
)
472 /* Handle SPE stand-alone executables. */
473 if (spu_standalone_p ())
475 /* After an SPE stand-alone executable was loaded, we'll receive
476 an additional trap due to the binfmt_misc handler. Make sure
477 to skip that trap. */
478 spu_skip_standalone_loader ();
480 /* If the user established breakpoints before starting the inferior, GDB
481 would attempt to insert those now. This would fail because the SPU
482 context has not yet been created and the SPU executable has not yet
483 been loaded. To prevent such failures, we disable all user-created
484 breakpoints now; they will be re-enabled in spu_current_sos once the
485 main SPU context has been detected. */
486 disable_breakpoints_before_startup ();
488 /* A special case arises when re-starting an executable, because at
489 this point it still resides at the relocated address range that was
490 determined during its last execution. We need to undo the relocation
491 so that that multi-architecture target recognizes the stand-alone
492 initialization special case. */
493 spu_relocate_main_executable (-1);
496 /* Call SVR4 hook -- this will re-insert the SVR4 solib breakpoints. */
497 svr4_so_ops
.solib_create_inferior_hook (from_tty
);
499 /* If the inferior is statically linked against libspe, we need to install
500 our own solib breakpoint right now. Otherwise, it will be installed by
501 the solib_loaded observer below as soon as libspe is loaded. */
502 spu_enable_break (NULL
);
505 /* Install SPE "shared library" handling. This is called by -tdep code
506 that wants to support SPU as a secondary architecture. */
508 set_spu_solib_ops (struct gdbarch
*gdbarch
)
510 static struct target_so_ops spu_so_ops
;
512 /* Initialize this lazily, to avoid an initialization order
513 dependency on solib-svr4.c's _initialize routine. */
514 if (spu_so_ops
.current_sos
== NULL
)
516 spu_so_ops
= svr4_so_ops
;
517 spu_so_ops
.solib_create_inferior_hook
= spu_solib_create_inferior_hook
;
518 spu_so_ops
.relocate_section_addresses
= spu_relocate_section_addresses
;
519 spu_so_ops
.free_so
= spu_free_so
;
520 spu_so_ops
.current_sos
= spu_current_sos
;
521 spu_so_ops
.bfd_open
= spu_bfd_open
;
522 spu_so_ops
.lookup_lib_global_symbol
= spu_lookup_lib_symbol
;
525 set_solib_ops (gdbarch
, &spu_so_ops
);
528 /* Observer for the solib_loaded event. Used to install our breakpoint
529 if libspe is a shared library. */
531 spu_solib_loaded (struct so_list
*so
)
533 if (strstr (so
->so_original_name
, "/libspe") != NULL
)
535 solib_read_symbols (so
, 0);
536 spu_enable_break (so
->objfile
);
538 /* In case the OpenCL runtime is loaded we install a breakpoint
539 to get notified whenever an OpenCL program gets loaded. */
540 if (strstr (so
->so_name
, "CLRuntimeAccelCellSPU@") != NULL
)
542 solib_read_symbols (so
, 0);
543 ocl_enable_break (so
->objfile
);
547 /* -Wmissing-prototypes */
548 extern initialize_file_ftype _initialize_spu_solib
;
551 _initialize_spu_solib (void)
553 observer_attach_solib_loaded (spu_solib_loaded
);
554 ocl_program_data_key
= register_objfile_data ();