2010-06-25 Ken Werner <ken.werner@de.ibm.com>
[external/binutils.git] / gdb / solib-spu.c
1 /* Cell SPU GNU/Linux support -- shared library handling.
2    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
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
10    the Free Software Foundation; either version 3 of the License, or
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
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20
21 #include "defs.h"
22 #include "gdbcore.h"
23 #include "gdb_string.h"
24 #include "gdb_assert.h"
25 #include "gdb_stat.h"
26 #include "arch-utils.h"
27 #include "bfd.h"
28 #include "symtab.h"
29 #include "solib.h"
30 #include "solib-svr4.h"
31 #include "solist.h"
32 #include "inferior.h"
33 #include "objfiles.h"
34 #include "observer.h"
35 #include "breakpoint.h"
36 #include "gdbthread.h"
37 #include "exceptions.h"
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.  */
51 static void
52 spu_relocate_main_executable (int spufs_fd)
53 {
54   struct section_offsets *new_offsets;
55   int i;
56
57   if (symfile_objfile == NULL)
58     return;
59
60   new_offsets = alloca (symfile_objfile->num_sections
61                         * sizeof (struct section_offsets));
62
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);
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.  */
71 static void
72 spu_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
88       inferior_thread ()->in_infcall = 1;   /* Suppress MI messages.  */
89
90       target_resume (inferior_ptid, 1, TARGET_SIGNAL_0);
91       target_wait (minus_one_ptid, &ws, 0);
92       set_executing (minus_one_ptid, 0);
93
94       inferior_thread ()->in_infcall = 0;
95     }
96 }
97
98 static const struct objfile_data *ocl_program_data_key;
99
100 /* Appends OpenCL programs to the list of `struct so_list' objects.  */
101 static void
102 append_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.  */
126                   new = XZALLOC (struct so_list);
127
128                   /* Encode FD and object ID in path name.  */
129                   xsnprintf (new->so_name, sizeof new->so_name, "@0x%lx <%d>",
130                              data, SPUADDR_SPU (*ocl_program_addr_base));
131                   strcpy (new->so_original_name, new->so_name);
132
133                   *link_ptr = new;
134                   link_ptr = &new->next;
135                 }
136             }
137           if (ex.reason < 0)
138             {
139               /* Ignore memory errors.  */
140               switch (ex.error)
141                 {
142                 case MEMORY_ERROR:
143                   break;
144                 default:
145                   throw_exception (ex);
146                   break;
147                 }
148             }
149         }
150     }
151 }
152
153 /* Build a list of `struct so_list' objects describing the shared
154    objects currently loaded in the inferior.  */
155 static struct so_list *
156 spu_current_sos (void)
157 {
158   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
159   struct so_list *head;
160   struct so_list **link_ptr;
161
162   char buf[MAX_SPE_FD * 4];
163   int i, size;
164
165   /* First, retrieve the SVR4 shared library list.  */
166   head = svr4_so_ops.current_sos ();
167
168   /* Append our libraries to the end of the list.  */
169   for (link_ptr = &head; *link_ptr; link_ptr = &(*link_ptr)->next)
170     ;
171
172   /* Determine list of SPU ids.  */
173   size = target_read (&current_target, TARGET_OBJECT_SPU, NULL,
174                       buf, 0, sizeof buf);
175
176   /* Do not add stand-alone SPE executable context as shared library,
177      but relocate main SPE executable objfile.  */
178   if (spu_standalone_p ())
179     {
180       if (size == 4)
181         {
182           int fd = extract_unsigned_integer (buf, 4, byte_order);
183
184           spu_relocate_main_executable (fd);
185
186           /* Re-enable breakpoints after main SPU context was established;
187              see also comments in spu_solib_create_inferior_hook.  */
188           enable_breakpoints_after_startup ();
189         }
190
191       return head;
192     }
193
194   /* Create an so_list entry for each SPU id.  */
195   for (i = 0; i < size; i += 4)
196     {
197       int fd = extract_unsigned_integer (buf + i, 4, byte_order);
198       struct so_list *new;
199
200       unsigned long long addr;
201       char annex[32], id[100];
202       int len;
203
204       /* Read object ID.  There's a race window where the inferior may have
205          already created the SPE context, but not installed the object-id
206          yet.  Skip such entries; we'll be back for them later.  */
207       xsnprintf (annex, sizeof annex, "%d/object-id", fd);
208       len = target_read (&current_target, TARGET_OBJECT_SPU, annex,
209                          id, 0, sizeof id);
210       if (len <= 0 || len >= sizeof id)
211         continue;
212       id[len] = 0;
213       if (sscanf (id, "0x%llx", &addr) != 1 || !addr)
214         continue;
215
216       /* Allocate so_list structure.  */
217       new = XZALLOC (struct so_list);
218
219       /* Encode FD and object ID in path name.  Choose the name so as not
220          to conflict with any (normal) SVR4 library path name.  */
221       xsnprintf (new->so_name, sizeof new->so_name, "@%s <%d>",
222                  hex_string (addr), fd);
223       strcpy (new->so_original_name, new->so_name);
224
225       *link_ptr = new;
226       link_ptr = &new->next;
227     }
228
229   /* Append OpenCL sos. */
230   append_ocl_sos (link_ptr);
231
232   return head;
233 }
234
235 /* Free so_list information.  */
236 static void
237 spu_free_so (struct so_list *so)
238 {
239   if (so->so_original_name[0] != '@')
240     svr4_so_ops.free_so (so);
241 }
242
243 /* Relocate section addresses.  */
244 static void
245 spu_relocate_section_addresses (struct so_list *so,
246                                 struct target_section *sec)
247 {
248   if (so->so_original_name[0] != '@')
249     svr4_so_ops.relocate_section_addresses (so, sec);
250   else
251     {
252       unsigned long long addr;
253       int fd;
254
255       /* Set addr_low/high to just LS offset for display.  */
256       if (so->addr_low == 0 && so->addr_high == 0
257           && strcmp (sec->the_bfd_section->name, ".text") == 0)
258         {
259           so->addr_low = sec->addr;
260           so->addr_high = sec->endaddr;
261         }
262
263       /* Decode object ID.  */
264       if (sscanf (so->so_original_name, "@0x%llx <%d>", &addr, &fd) != 2)
265         internal_error (__FILE__, __LINE__, "bad object ID");
266
267       sec->addr = SPUADDR (fd, sec->addr);
268       sec->endaddr = SPUADDR (fd, sec->endaddr);
269     }
270 }
271
272
273 /* Inferior memory should contain an SPE executable image at location ADDR.
274    Allocate a BFD representing that executable.  Return NULL on error.  */
275
276 static void *
277 spu_bfd_iovec_open (bfd *nbfd, void *open_closure)
278 {
279   return open_closure;
280 }
281
282 static int
283 spu_bfd_iovec_close (bfd *nbfd, void *stream)
284 {
285   xfree (stream);
286   return 1;
287 }
288
289 static file_ptr
290 spu_bfd_iovec_pread (bfd *abfd, void *stream, void *buf,
291                      file_ptr nbytes, file_ptr offset)
292 {
293   CORE_ADDR addr = *(CORE_ADDR *)stream;
294   int ret;
295
296   ret = target_read_memory (addr + offset, buf, nbytes);
297   if (ret != 0)
298     {
299       bfd_set_error (bfd_error_invalid_operation);
300       return -1;
301     }
302
303   return nbytes;
304 }
305
306 static int
307 spu_bfd_iovec_stat (bfd *abfd, void *stream, struct stat *sb)
308 {
309   /* We don't have an easy way of finding the size of embedded spu
310      images.  We could parse the in-memory ELF header and section
311      table to find the extent of the last section but that seems
312      pointless when the size is needed only for checks of other
313      parsed values in dbxread.c.  */
314   sb->st_size = INT_MAX;
315   return 0;
316 }
317
318 static bfd *
319 spu_bfd_fopen (char *name, CORE_ADDR addr)
320 {
321   bfd *nbfd;
322
323   CORE_ADDR *open_closure = xmalloc (sizeof (CORE_ADDR));
324   *open_closure = addr;
325
326   nbfd = bfd_openr_iovec (xstrdup (name), "elf32-spu",
327                           spu_bfd_iovec_open, open_closure,
328                           spu_bfd_iovec_pread, spu_bfd_iovec_close,
329                           spu_bfd_iovec_stat);
330   if (!nbfd)
331     return NULL;
332
333   if (!bfd_check_format (nbfd, bfd_object))
334     {
335       bfd_close (nbfd);
336       return NULL;
337     }
338
339   return nbfd;
340 }
341
342 /* Open shared library BFD.  */
343 static bfd *
344 spu_bfd_open (char *pathname)
345 {
346   char *original_name = strrchr (pathname, '@');
347   bfd *abfd;
348   asection *spu_name;
349   unsigned long long addr;
350   int fd;
351
352   /* Handle regular SVR4 libraries.  */
353   if (!original_name)
354     return svr4_so_ops.bfd_open (pathname);
355
356   /* Decode object ID.  */
357   if (sscanf (original_name, "@0x%llx <%d>", &addr, &fd) != 2)
358     internal_error (__FILE__, __LINE__, "bad object ID");
359
360   /* Open BFD representing SPE executable.  */
361   abfd = spu_bfd_fopen (original_name, (CORE_ADDR) addr);
362   if (!abfd)
363     error (_("Cannot read SPE executable at %s"), original_name);
364
365   /* Retrieve SPU name note.  */
366   spu_name = bfd_get_section_by_name (abfd, ".note.spu_name");
367   if (spu_name)
368     {
369       int sect_size = bfd_section_size (abfd, spu_name);
370
371       if (sect_size > 20)
372         {
373           char *buf = alloca (sect_size - 20 + strlen (original_name) + 1);
374
375           bfd_get_section_contents (abfd, spu_name, buf, 20, sect_size - 20);
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.  */
389 static struct symbol *
390 spu_lookup_lib_symbol (const struct objfile *objfile,
391                        const char *name,
392                        const domain_enum domain)
393 {
394   if (bfd_get_arch (objfile->obfd) == bfd_arch_spu)
395     return lookup_global_symbol_from_objfile (objfile, name, domain);
396
397   if (svr4_so_ops.lookup_lib_global_symbol != NULL)
398     return svr4_so_ops.lookup_lib_global_symbol (objfile, name, domain);
399   return NULL;
400 }
401
402 /* Enable shared library breakpoint.  */
403 static int
404 spu_enable_break (struct objfile *objfile)
405 {
406   struct minimal_symbol *spe_event_sym = NULL;
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.  */
414   if (spe_event_sym)
415     {
416       CORE_ADDR addr = SYMBOL_VALUE_ADDRESS (spe_event_sym);
417
418       addr = gdbarch_convert_from_func_ptr_addr (target_gdbarch, addr,
419                                                  &current_target);
420       create_solib_event_breakpoint (target_gdbarch, addr);
421       return 1;
422     }
423
424   return 0;
425 }
426
427 /* Enable shared library breakpoint for the
428    OpenCL runtime running on the SPU.  */
429 static void
430 ocl_enable_break (struct objfile *objfile)
431 {
432   struct minimal_symbol *event_sym = NULL;
433   struct minimal_symbol *addr_sym = NULL;
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
443   if (event_sym && addr_sym)
444     {
445       /* Place a solib_event breakpoint on the symbol.  */
446       CORE_ADDR event_addr = SYMBOL_VALUE_ADDRESS (event_sym);
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);
457           *ocl_program_addr_base = SYMBOL_VALUE_ADDRESS (addr_sym);
458           set_objfile_data (objfile, ocl_program_data_key,
459                             ocl_program_addr_base);
460         }
461     }
462 }
463
464 /* Create inferior hook.  */
465 static void
466 spu_solib_create_inferior_hook (int from_tty)
467 {
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.  */
493   svr4_so_ops.solib_create_inferior_hook (from_tty);
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.  */
503 void
504 set_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.  */
526 static void
527 spu_solib_loaded (struct so_list *so)
528 {
529   if (strstr (so->so_original_name, "/libspe") != NULL)
530     {
531       solib_read_symbols (so, 0);
532       spu_enable_break (so->objfile);
533     }
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     }
541 }
542
543 void
544 _initialize_spu_solib (void)
545 {
546   observer_attach_solib_loaded (spu_solib_loaded);
547   ocl_program_data_key = register_objfile_data ();
548 }
549