1 /* Cell SPU GNU/Linux support -- shared library handling.
2 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4 Contributed by Ulrich Weigand <uweigand@de.ibm.com>.
6 This file is part of GDB.
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.
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.
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/>. */
23 #include "gdb_string.h"
24 #include "gdb_assert.h"
26 #include "arch-utils.h"
30 #include "solib-svr4.h"
35 #include "breakpoint.h"
36 #include "gdbthread.h"
40 /* Highest SPE id (file handle) the inferior may have. */
41 #define MAX_SPE_FD 1024
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)
49 /* Relocate main SPE executable. */
51 spu_relocate_main_executable (int spufs_fd
)
53 struct section_offsets
*new_offsets
;
56 if (symfile_objfile
== NULL
)
59 new_offsets
= alloca (symfile_objfile
->num_sections
60 * sizeof (struct section_offsets
));
62 for (i
= 0; i
< symfile_objfile
->num_sections
; i
++)
63 new_offsets
->offsets
[i
] = SPUADDR (spufs_fd
, 0);
65 objfile_relocate (symfile_objfile
, new_offsets
);
68 /* When running a stand-alone SPE executable, we may need to skip one more
69 exec event on startup, to get past the binfmt_misc loader. */
71 spu_skip_standalone_loader (void)
73 if (target_has_execution
&& !current_inferior ()->attach_flag
)
75 struct target_waitstatus ws
;
77 /* Only some kernels report an extra SIGTRAP with the binfmt_misc
78 loader; others do not. In addition, if we have attached to an
79 already running inferior instead of starting a new one, we will
80 not see the extra SIGTRAP -- and we cannot readily distinguish
81 the two cases, in particular with the extended-remote target.
83 Thus we issue a single-step here. If no extra SIGTRAP was pending,
84 this will step past the first instruction of the stand-alone SPE
85 executable loader, but we don't care about that. */
87 inferior_thread ()->in_infcall
= 1; /* Suppress MI messages. */
89 target_resume (inferior_ptid
, 1, TARGET_SIGNAL_0
);
90 target_wait (minus_one_ptid
, &ws
, 0);
91 set_executing (minus_one_ptid
, 0);
93 inferior_thread ()->in_infcall
= 0;
97 /* Build a list of `struct so_list' objects describing the shared
98 objects currently loaded in the inferior. */
99 static struct so_list
*
100 spu_current_sos (void)
102 enum bfd_endian byte_order
= gdbarch_byte_order (target_gdbarch
);
103 struct so_list
*head
;
104 struct so_list
**link_ptr
;
106 char buf
[MAX_SPE_FD
* 4];
109 /* First, retrieve the SVR4 shared library list. */
110 head
= svr4_so_ops
.current_sos ();
112 /* Append our libraries to the end of the list. */
113 for (link_ptr
= &head
; *link_ptr
; link_ptr
= &(*link_ptr
)->next
)
116 /* Determine list of SPU ids. */
117 size
= target_read (¤t_target
, TARGET_OBJECT_SPU
, NULL
,
120 /* Do not add stand-alone SPE executable context as shared library,
121 but relocate main SPE executable objfile. */
122 if (spu_standalone_p ())
126 int fd
= extract_unsigned_integer (buf
, 4, byte_order
);
128 spu_relocate_main_executable (fd
);
130 /* Re-enable breakpoints after main SPU context was established;
131 see also comments in spu_solib_create_inferior_hook. */
132 enable_breakpoints_after_startup ();
138 /* Create an so_list entry for each SPU id. */
139 for (i
= 0; i
< size
; i
+= 4)
141 int fd
= extract_unsigned_integer (buf
+ i
, 4, byte_order
);
144 unsigned long long addr
;
145 char annex
[32], id
[100];
148 /* Read object ID. There's a race window where the inferior may have
149 already created the SPE context, but not installed the object-id
150 yet. Skip such entries; we'll be back for them later. */
151 xsnprintf (annex
, sizeof annex
, "%d/object-id", fd
);
152 len
= target_read (¤t_target
, TARGET_OBJECT_SPU
, annex
,
154 if (len
<= 0 || len
>= sizeof id
)
157 if (sscanf (id
, "0x%llx", &addr
) != 1 || !addr
)
160 /* Allocate so_list structure. */
161 new = XZALLOC (struct so_list
);
163 /* Encode FD and object ID in path name. Choose the name so as not
164 to conflict with any (normal) SVR4 library path name. */
165 xsnprintf (new->so_name
, sizeof new->so_name
, "@%s <%d>",
166 hex_string (addr
), fd
);
167 strcpy (new->so_original_name
, new->so_name
);
170 link_ptr
= &new->next
;
176 /* Free so_list information. */
178 spu_free_so (struct so_list
*so
)
180 if (so
->so_original_name
[0] != '@')
181 svr4_so_ops
.free_so (so
);
184 /* Relocate section addresses. */
186 spu_relocate_section_addresses (struct so_list
*so
,
187 struct target_section
*sec
)
189 if (so
->so_original_name
[0] != '@')
190 svr4_so_ops
.relocate_section_addresses (so
, sec
);
193 unsigned long long addr
;
196 /* Set addr_low/high to just LS offset for display. */
197 if (so
->addr_low
== 0 && so
->addr_high
== 0
198 && strcmp (sec
->the_bfd_section
->name
, ".text") == 0)
200 so
->addr_low
= sec
->addr
;
201 so
->addr_high
= sec
->endaddr
;
204 /* Decode object ID. */
205 if (sscanf (so
->so_original_name
, "@0x%llx <%d>", &addr
, &fd
) != 2)
206 internal_error (__FILE__
, __LINE__
, "bad object ID");
208 sec
->addr
= SPUADDR (fd
, sec
->addr
);
209 sec
->endaddr
= SPUADDR (fd
, sec
->endaddr
);
214 /* Inferior memory should contain an SPE executable image at location ADDR.
215 Allocate a BFD representing that executable. Return NULL on error. */
218 spu_bfd_iovec_open (bfd
*nbfd
, void *open_closure
)
224 spu_bfd_iovec_close (bfd
*nbfd
, void *stream
)
231 spu_bfd_iovec_pread (bfd
*abfd
, void *stream
, void *buf
,
232 file_ptr nbytes
, file_ptr offset
)
234 CORE_ADDR addr
= *(CORE_ADDR
*)stream
;
237 ret
= target_read_memory (addr
+ offset
, buf
, nbytes
);
240 bfd_set_error (bfd_error_invalid_operation
);
248 spu_bfd_iovec_stat (bfd
*abfd
, void *stream
, struct stat
*sb
)
250 /* We don't have an easy way of finding the size of embedded spu
251 images. We could parse the in-memory ELF header and section
252 table to find the extent of the last section but that seems
253 pointless when the size is needed only for checks of other
254 parsed values in dbxread.c. */
255 sb
->st_size
= INT_MAX
;
260 spu_bfd_fopen (char *name
, CORE_ADDR addr
)
264 CORE_ADDR
*open_closure
= xmalloc (sizeof (CORE_ADDR
));
265 *open_closure
= addr
;
267 nbfd
= bfd_openr_iovec (xstrdup (name
), "elf32-spu",
268 spu_bfd_iovec_open
, open_closure
,
269 spu_bfd_iovec_pread
, spu_bfd_iovec_close
,
274 if (!bfd_check_format (nbfd
, bfd_object
))
283 /* Open shared library BFD. */
285 spu_bfd_open (char *pathname
)
287 char *original_name
= strrchr (pathname
, '@');
290 unsigned long long addr
;
293 /* Handle regular SVR4 libraries. */
295 return svr4_so_ops
.bfd_open (pathname
);
297 /* Decode object ID. */
298 if (sscanf (original_name
, "@0x%llx <%d>", &addr
, &fd
) != 2)
299 internal_error (__FILE__
, __LINE__
, "bad object ID");
301 /* Open BFD representing SPE executable. */
302 abfd
= spu_bfd_fopen (original_name
, (CORE_ADDR
) addr
);
304 error (_("Cannot read SPE executable at %s"), original_name
);
306 /* Retrieve SPU name note. */
307 spu_name
= bfd_get_section_by_name (abfd
, ".note.spu_name");
310 int sect_size
= bfd_section_size (abfd
, spu_name
);
314 char *buf
= alloca (sect_size
- 20 + strlen (original_name
) + 1);
316 bfd_get_section_contents (abfd
, spu_name
, buf
, 20, sect_size
- 20);
317 buf
[sect_size
- 20] = '\0';
319 strcat (buf
, original_name
);
321 xfree ((char *)abfd
->filename
);
322 abfd
->filename
= xstrdup (buf
);
329 /* Lookup global symbol in a SPE executable. */
330 static struct symbol
*
331 spu_lookup_lib_symbol (const struct objfile
*objfile
,
333 const domain_enum domain
)
335 if (bfd_get_arch (objfile
->obfd
) == bfd_arch_spu
)
336 return lookup_global_symbol_from_objfile (objfile
, name
, domain
);
338 if (svr4_so_ops
.lookup_lib_global_symbol
!= NULL
)
339 return svr4_so_ops
.lookup_lib_global_symbol (objfile
, name
, domain
);
343 /* Enable shared library breakpoint. */
345 spu_enable_break (struct objfile
*objfile
)
347 struct minimal_symbol
*spe_event_sym
= NULL
;
349 /* The libspe library will call __spe_context_update_event whenever any
350 SPE context is allocated or destroyed. */
351 spe_event_sym
= lookup_minimal_symbol ("__spe_context_update_event",
354 /* Place a solib_event breakpoint on the symbol. */
357 CORE_ADDR addr
= SYMBOL_VALUE_ADDRESS (spe_event_sym
);
359 addr
= gdbarch_convert_from_func_ptr_addr (target_gdbarch
, addr
,
361 create_solib_event_breakpoint (target_gdbarch
, addr
);
368 /* Create inferior hook. */
370 spu_solib_create_inferior_hook (int from_tty
)
372 /* Handle SPE stand-alone executables. */
373 if (spu_standalone_p ())
375 /* After an SPE stand-alone executable was loaded, we'll receive
376 an additional trap due to the binfmt_misc handler. Make sure
377 to skip that trap. */
378 spu_skip_standalone_loader ();
380 /* If the user established breakpoints before starting the inferior, GDB
381 would attempt to insert those now. This would fail because the SPU
382 context has not yet been created and the SPU executable has not yet
383 been loaded. To prevent such failures, we disable all user-created
384 breakpoints now; they will be re-enabled in spu_current_sos once the
385 main SPU context has been detected. */
386 disable_breakpoints_before_startup ();
388 /* A special case arises when re-starting an executable, because at
389 this point it still resides at the relocated address range that was
390 determined during its last execution. We need to undo the relocation
391 so that that multi-architecture target recognizes the stand-alone
392 initialization special case. */
393 spu_relocate_main_executable (-1);
396 /* Call SVR4 hook -- this will re-insert the SVR4 solib breakpoints. */
397 svr4_so_ops
.solib_create_inferior_hook (from_tty
);
399 /* If the inferior is statically linked against libspe, we need to install
400 our own solib breakpoint right now. Otherwise, it will be installed by
401 the solib_loaded observer below as soon as libspe is loaded. */
402 spu_enable_break (NULL
);
405 /* Install SPE "shared library" handling. This is called by -tdep code
406 that wants to support SPU as a secondary architecture. */
408 set_spu_solib_ops (struct gdbarch
*gdbarch
)
410 static struct target_so_ops spu_so_ops
;
412 /* Initialize this lazily, to avoid an initialization order
413 dependency on solib-svr4.c's _initialize routine. */
414 if (spu_so_ops
.current_sos
== NULL
)
416 spu_so_ops
= svr4_so_ops
;
417 spu_so_ops
.solib_create_inferior_hook
= spu_solib_create_inferior_hook
;
418 spu_so_ops
.relocate_section_addresses
= spu_relocate_section_addresses
;
419 spu_so_ops
.free_so
= spu_free_so
;
420 spu_so_ops
.current_sos
= spu_current_sos
;
421 spu_so_ops
.bfd_open
= spu_bfd_open
;
422 spu_so_ops
.lookup_lib_global_symbol
= spu_lookup_lib_symbol
;
425 set_solib_ops (gdbarch
, &spu_so_ops
);
428 /* Observer for the solib_loaded event. Used to install our breakpoint
429 if libspe is a shared library. */
431 spu_solib_loaded (struct so_list
*so
)
433 if (strstr (so
->so_original_name
, "/libspe") != NULL
)
435 solib_read_symbols (so
, 0);
436 spu_enable_break (so
->objfile
);
441 _initialize_spu_solib (void)
443 observer_attach_solib_loaded (spu_solib_loaded
);