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