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