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