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