Commit | Line | Data |
---|---|---|
ab31aa69 | 1 | /* Handle SunOS shared libraries for GDB, the GNU Debugger. |
8dcef9cf | 2 | |
28e7fd62 | 3 | Copyright (C) 1990-2013 Free Software Foundation, Inc. |
ab31aa69 KB |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
ab31aa69 KB |
10 | (at your option) any later version. |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
ab31aa69 KB |
19 | |
20 | #include "defs.h" | |
21 | ||
22 | #include <sys/types.h> | |
23 | #include <signal.h> | |
24 | #include "gdb_string.h" | |
25 | #include <sys/param.h> | |
26 | #include <fcntl.h> | |
27 | ||
8dcef9cf | 28 | /* SunOS shared libs need the nlist structure. */ |
ab31aa69 KB |
29 | #include <a.out.h> |
30 | #include <link.h> | |
31 | ||
32 | #include "symtab.h" | |
33 | #include "bfd.h" | |
34 | #include "symfile.h" | |
35 | #include "objfiles.h" | |
36 | #include "gdbcore.h" | |
37 | #include "inferior.h" | |
2020b7ab | 38 | #include "gdbthread.h" |
ab31aa69 | 39 | #include "solist.h" |
03cc47f7 MK |
40 | #include "bcache.h" |
41 | #include "regcache.h" | |
ab31aa69 | 42 | |
8dcef9cf MK |
43 | /* The shared library implementation found on BSD a.out systems is |
44 | very similar to the SunOS implementation. However, the data | |
45 | structures defined in <link.h> are named very differently. Make up | |
46 | for those differences here. */ | |
47 | ||
48 | #ifdef HAVE_STRUCT_SO_MAP_WITH_SOM_MEMBERS | |
49 | ||
50 | /* FIXME: Temporary until the equivalent defines have been removed | |
51 | from all nm-*bsd*.h files. */ | |
52 | #ifndef link_dynamic | |
53 | ||
54 | /* Map `struct link_map' and its members. */ | |
55 | #define link_map so_map | |
56 | #define lm_addr som_addr | |
57 | #define lm_name som_path | |
58 | #define lm_next som_next | |
59 | ||
60 | /* Map `struct link_dynamic_2' and its members. */ | |
61 | #define link_dynamic_2 section_dispatch_table | |
62 | #define ld_loaded sdt_loaded | |
63 | ||
64 | /* Map `struct rtc_symb' and its members. */ | |
65 | #define rtc_symb rt_symbol | |
66 | #define rtc_sp rt_sp | |
67 | #define rtc_next rt_next | |
68 | ||
69 | /* Map `struct ld_debug' and its members. */ | |
70 | #define ld_debug so_debug | |
71 | #define ldd_in_debugger dd_in_debugger | |
72 | #define ldd_bp_addr dd_bpt_addr | |
73 | #define ldd_bp_inst dd_bpt_shadow | |
74 | #define ldd_cp dd_cc | |
75 | ||
76 | /* Map `struct link_dynamic' and its members. */ | |
77 | #define link_dynamic _dynamic | |
78 | #define ld_version d_version | |
79 | #define ldd d_debug | |
80 | #define ld_un d_un | |
81 | #define ld_2 d_sdt | |
82 | ||
83 | #endif | |
84 | ||
85 | #endif | |
86 | ||
c378eb4e | 87 | /* Link map info to include in an allocated so_list entry. */ |
ab31aa69 KB |
88 | |
89 | struct lm_info | |
90 | { | |
91 | /* Pointer to copy of link map from inferior. The type is char * | |
92 | rather than void *, so that we may use byte offsets to find the | |
93 | various fields without the need for a cast. */ | |
94 | char *lm; | |
95 | }; | |
96 | ||
97 | ||
c378eb4e | 98 | /* Symbols which are used to locate the base of the link map structures. */ |
ab31aa69 KB |
99 | |
100 | static char *debug_base_symbols[] = | |
101 | { | |
102 | "_DYNAMIC", | |
103 | "_DYNAMIC__MGC", | |
104 | NULL | |
105 | }; | |
106 | ||
107 | static char *main_name_list[] = | |
108 | { | |
109 | "main_$main", | |
110 | NULL | |
111 | }; | |
112 | ||
ae0167b9 AC |
113 | /* Macro to extract an address from a solib structure. When GDB is |
114 | configured for some 32-bit targets (e.g. Solaris 2.7 sparc), BFD is | |
115 | configured to handle 64-bit targets, so CORE_ADDR is 64 bits. We | |
116 | have to extract only the significant bits of addresses to get the | |
117 | right address when accessing the core file BFD. | |
118 | ||
119 | Assume that the address is unsigned. */ | |
ab31aa69 KB |
120 | |
121 | #define SOLIB_EXTRACT_ADDRESS(MEMBER) \ | |
e17a4113 | 122 | extract_unsigned_integer (&(MEMBER), sizeof (MEMBER), \ |
f5656ead | 123 | gdbarch_byte_order (target_gdbarch ())) |
ab31aa69 KB |
124 | |
125 | /* local data declarations */ | |
126 | ||
127 | static struct link_dynamic dynamic_copy; | |
128 | static struct link_dynamic_2 ld_2_copy; | |
129 | static struct ld_debug debug_copy; | |
130 | static CORE_ADDR debug_addr; | |
131 | static CORE_ADDR flag_addr; | |
132 | ||
133 | #ifndef offsetof | |
134 | #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER) | |
135 | #endif | |
136 | #define fieldsize(TYPE, MEMBER) (sizeof (((TYPE *)0)->MEMBER)) | |
137 | ||
138 | /* link map access functions */ | |
139 | ||
140 | static CORE_ADDR | |
b23518f0 | 141 | lm_addr (struct so_list *so) |
ab31aa69 | 142 | { |
f5656ead | 143 | enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ()); |
ab31aa69 KB |
144 | int lm_addr_offset = offsetof (struct link_map, lm_addr); |
145 | int lm_addr_size = fieldsize (struct link_map, lm_addr); | |
146 | ||
147 | return (CORE_ADDR) extract_signed_integer (so->lm_info->lm + lm_addr_offset, | |
e17a4113 | 148 | lm_addr_size, byte_order); |
ab31aa69 KB |
149 | } |
150 | ||
151 | static CORE_ADDR | |
b23518f0 | 152 | lm_next (struct so_list *so) |
ab31aa69 | 153 | { |
f5656ead | 154 | enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ()); |
ab31aa69 KB |
155 | int lm_next_offset = offsetof (struct link_map, lm_next); |
156 | int lm_next_size = fieldsize (struct link_map, lm_next); | |
157 | ||
ae0167b9 AC |
158 | /* Assume that the address is unsigned. */ |
159 | return extract_unsigned_integer (so->lm_info->lm + lm_next_offset, | |
e17a4113 | 160 | lm_next_size, byte_order); |
ab31aa69 KB |
161 | } |
162 | ||
163 | static CORE_ADDR | |
b23518f0 | 164 | lm_name (struct so_list *so) |
ab31aa69 | 165 | { |
f5656ead | 166 | enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ()); |
ab31aa69 KB |
167 | int lm_name_offset = offsetof (struct link_map, lm_name); |
168 | int lm_name_size = fieldsize (struct link_map, lm_name); | |
169 | ||
ae0167b9 AC |
170 | /* Assume that the address is unsigned. */ |
171 | return extract_unsigned_integer (so->lm_info->lm + lm_name_offset, | |
e17a4113 | 172 | lm_name_size, byte_order); |
ab31aa69 KB |
173 | } |
174 | ||
c378eb4e | 175 | static CORE_ADDR debug_base; /* Base of dynamic linker structures. */ |
ab31aa69 KB |
176 | |
177 | /* Local function prototypes */ | |
178 | ||
179 | static int match_main (char *); | |
180 | ||
181 | /* Allocate the runtime common object file. */ | |
182 | ||
183 | static void | |
184 | allocate_rt_common_objfile (void) | |
185 | { | |
186 | struct objfile *objfile; | |
187 | struct objfile *last_one; | |
188 | ||
189 | objfile = (struct objfile *) xmalloc (sizeof (struct objfile)); | |
190 | memset (objfile, 0, sizeof (struct objfile)); | |
5068b8e8 | 191 | objfile->psymbol_cache = psymbol_bcache_init (); |
192 | objfile->macro_cache = bcache_xmalloc (NULL, NULL); | |
193 | objfile->filename_cache = bcache_xmalloc (NULL, NULL); | |
1ab21617 | 194 | obstack_init (&objfile->objfile_obstack); |
982526a1 | 195 | objfile->name = xstrdup ("rt_common"); |
ab31aa69 | 196 | |
c378eb4e | 197 | /* Add this file onto the tail of the linked list of other such files. */ |
ab31aa69 KB |
198 | |
199 | objfile->next = NULL; | |
200 | if (object_files == NULL) | |
201 | object_files = objfile; | |
202 | else | |
203 | { | |
204 | for (last_one = object_files; | |
205 | last_one->next; | |
206 | last_one = last_one->next); | |
207 | last_one->next = objfile; | |
208 | } | |
209 | ||
210 | rt_common_objfile = objfile; | |
211 | } | |
212 | ||
213 | /* Read all dynamically loaded common symbol definitions from the inferior | |
214 | and put them into the minimal symbol table for the runtime common | |
215 | objfile. */ | |
216 | ||
217 | static void | |
218 | solib_add_common_symbols (CORE_ADDR rtc_symp) | |
219 | { | |
220 | struct rtc_symb inferior_rtc_symb; | |
221 | struct nlist inferior_rtc_nlist; | |
222 | int len; | |
223 | char *name; | |
224 | ||
225 | /* Remove any runtime common symbols from previous runs. */ | |
226 | ||
227 | if (rt_common_objfile != NULL && rt_common_objfile->minimal_symbol_count) | |
228 | { | |
4a146b47 | 229 | obstack_free (&rt_common_objfile->objfile_obstack, 0); |
1ab21617 | 230 | obstack_init (&rt_common_objfile->objfile_obstack); |
ab31aa69 KB |
231 | rt_common_objfile->minimal_symbol_count = 0; |
232 | rt_common_objfile->msymbols = NULL; | |
15831452 | 233 | terminate_minimal_symbol_table (rt_common_objfile); |
ab31aa69 KB |
234 | } |
235 | ||
236 | init_minimal_symbol_collection (); | |
237 | make_cleanup_discard_minimal_symbols (); | |
238 | ||
239 | while (rtc_symp) | |
240 | { | |
241 | read_memory (rtc_symp, | |
242 | (char *) &inferior_rtc_symb, | |
243 | sizeof (inferior_rtc_symb)); | |
244 | read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_sp), | |
245 | (char *) &inferior_rtc_nlist, | |
246 | sizeof (inferior_rtc_nlist)); | |
247 | if (inferior_rtc_nlist.n_type == N_COMM) | |
248 | { | |
249 | /* FIXME: The length of the symbol name is not available, but in the | |
250 | current implementation the common symbol is allocated immediately | |
c378eb4e | 251 | behind the name of the symbol. */ |
ab31aa69 KB |
252 | len = inferior_rtc_nlist.n_value - inferior_rtc_nlist.n_un.n_strx; |
253 | ||
254 | name = xmalloc (len); | |
255 | read_memory (SOLIB_EXTRACT_ADDRESS (inferior_rtc_nlist.n_un.n_name), | |
256 | name, len); | |
257 | ||
c378eb4e | 258 | /* Allocate the runtime common objfile if necessary. */ |
ab31aa69 KB |
259 | if (rt_common_objfile == NULL) |
260 | allocate_rt_common_objfile (); | |
261 | ||
262 | prim_record_minimal_symbol (name, inferior_rtc_nlist.n_value, | |
263 | mst_bss, rt_common_objfile); | |
264 | xfree (name); | |
265 | } | |
266 | rtc_symp = SOLIB_EXTRACT_ADDRESS (inferior_rtc_symb.rtc_next); | |
267 | } | |
268 | ||
269 | /* Install any minimal symbols that have been collected as the current | |
270 | minimal symbols for the runtime common objfile. */ | |
271 | ||
272 | install_minimal_symbols (rt_common_objfile); | |
273 | } | |
274 | ||
275 | ||
7f86f058 | 276 | /* Locate the base address of dynamic linker structs. |
ab31aa69 KB |
277 | |
278 | For both the SunOS and SVR4 shared library implementations, if the | |
279 | inferior executable has been linked dynamically, there is a single | |
280 | address somewhere in the inferior's data space which is the key to | |
281 | locating all of the dynamic linker's runtime structures. This | |
282 | address is the value of the debug base symbol. The job of this | |
283 | function is to find and return that address, or to return 0 if there | |
284 | is no such address (the executable is statically linked for example). | |
285 | ||
286 | For SunOS, the job is almost trivial, since the dynamic linker and | |
287 | all of it's structures are statically linked to the executable at | |
288 | link time. Thus the symbol for the address we are looking for has | |
289 | already been added to the minimal symbol table for the executable's | |
290 | objfile at the time the symbol file's symbols were read, and all we | |
291 | have to do is look it up there. Note that we explicitly do NOT want | |
292 | to find the copies in the shared library. | |
293 | ||
294 | The SVR4 version is a bit more complicated because the address | |
295 | is contained somewhere in the dynamic info section. We have to go | |
296 | to a lot more work to discover the address of the debug base symbol. | |
297 | Because of this complexity, we cache the value we find and return that | |
298 | value on subsequent invocations. Note there is no copy in the | |
7f86f058 | 299 | executable symbol tables. */ |
ab31aa69 KB |
300 | |
301 | static CORE_ADDR | |
302 | locate_base (void) | |
303 | { | |
304 | struct minimal_symbol *msymbol; | |
305 | CORE_ADDR address = 0; | |
306 | char **symbolp; | |
307 | ||
308 | /* For SunOS, we want to limit the search for the debug base symbol to the | |
309 | executable being debugged, since there is a duplicate named symbol in the | |
c378eb4e | 310 | shared library. We don't want the shared library versions. */ |
ab31aa69 KB |
311 | |
312 | for (symbolp = debug_base_symbols; *symbolp != NULL; symbolp++) | |
313 | { | |
314 | msymbol = lookup_minimal_symbol (*symbolp, NULL, symfile_objfile); | |
315 | if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0)) | |
316 | { | |
317 | address = SYMBOL_VALUE_ADDRESS (msymbol); | |
318 | return (address); | |
319 | } | |
320 | } | |
321 | return (0); | |
322 | } | |
323 | ||
7f86f058 | 324 | /* Locate first member in dynamic linker's map. |
ab31aa69 KB |
325 | |
326 | Find the first element in the inferior's dynamic link map, and | |
327 | return its address in the inferior. This function doesn't copy the | |
328 | link map entry itself into our address space; current_sos actually | |
329 | does the reading. */ | |
330 | ||
331 | static CORE_ADDR | |
332 | first_link_map_member (void) | |
333 | { | |
334 | CORE_ADDR lm = 0; | |
335 | ||
336 | read_memory (debug_base, (char *) &dynamic_copy, sizeof (dynamic_copy)); | |
337 | if (dynamic_copy.ld_version >= 2) | |
338 | { | |
339 | /* It is a version that we can deal with, so read in the secondary | |
c378eb4e | 340 | structure and find the address of the link map list from it. */ |
ab31aa69 KB |
341 | read_memory (SOLIB_EXTRACT_ADDRESS (dynamic_copy.ld_un.ld_2), |
342 | (char *) &ld_2_copy, sizeof (struct link_dynamic_2)); | |
343 | lm = SOLIB_EXTRACT_ADDRESS (ld_2_copy.ld_loaded); | |
344 | } | |
345 | return (lm); | |
346 | } | |
347 | ||
348 | static int | |
349 | open_symbol_file_object (void *from_ttyp) | |
350 | { | |
351 | return 1; | |
352 | } | |
353 | ||
354 | ||
7f86f058 | 355 | /* Implement the "current_sos" target_so_ops method. */ |
ab31aa69 KB |
356 | |
357 | static struct so_list * | |
358 | sunos_current_sos (void) | |
359 | { | |
360 | CORE_ADDR lm; | |
361 | struct so_list *head = 0; | |
362 | struct so_list **link_ptr = &head; | |
363 | int errcode; | |
364 | char *buffer; | |
365 | ||
366 | /* Make sure we've looked up the inferior's dynamic linker's base | |
367 | structure. */ | |
368 | if (! debug_base) | |
369 | { | |
370 | debug_base = locate_base (); | |
371 | ||
372 | /* If we can't find the dynamic linker's base structure, this | |
373 | must not be a dynamically linked executable. Hmm. */ | |
374 | if (! debug_base) | |
375 | return 0; | |
376 | } | |
377 | ||
378 | /* Walk the inferior's link map list, and build our list of | |
379 | `struct so_list' nodes. */ | |
380 | lm = first_link_map_member (); | |
381 | while (lm) | |
382 | { | |
383 | struct so_list *new | |
384 | = (struct so_list *) xmalloc (sizeof (struct so_list)); | |
385 | struct cleanup *old_chain = make_cleanup (xfree, new); | |
386 | ||
387 | memset (new, 0, sizeof (*new)); | |
388 | ||
389 | new->lm_info = xmalloc (sizeof (struct lm_info)); | |
390 | make_cleanup (xfree, new->lm_info); | |
391 | ||
392 | new->lm_info->lm = xmalloc (sizeof (struct link_map)); | |
393 | make_cleanup (xfree, new->lm_info->lm); | |
394 | memset (new->lm_info->lm, 0, sizeof (struct link_map)); | |
395 | ||
396 | read_memory (lm, new->lm_info->lm, sizeof (struct link_map)); | |
397 | ||
b23518f0 | 398 | lm = lm_next (new); |
ab31aa69 KB |
399 | |
400 | /* Extract this shared object's name. */ | |
b23518f0 | 401 | target_read_string (lm_name (new), &buffer, |
ab31aa69 KB |
402 | SO_NAME_MAX_PATH_SIZE - 1, &errcode); |
403 | if (errcode != 0) | |
8a3fe4f8 AC |
404 | warning (_("Can't read pathname for load map: %s."), |
405 | safe_strerror (errcode)); | |
ab31aa69 KB |
406 | else |
407 | { | |
408 | strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1); | |
409 | new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0'; | |
410 | xfree (buffer); | |
411 | strcpy (new->so_original_name, new->so_name); | |
412 | } | |
413 | ||
414 | /* If this entry has no name, or its name matches the name | |
415 | for the main executable, don't include it in the list. */ | |
416 | if (! new->so_name[0] | |
417 | || match_main (new->so_name)) | |
418 | free_so (new); | |
419 | else | |
420 | { | |
421 | new->next = 0; | |
422 | *link_ptr = new; | |
423 | link_ptr = &new->next; | |
424 | } | |
425 | ||
426 | discard_cleanups (old_chain); | |
427 | } | |
428 | ||
429 | return head; | |
430 | } | |
431 | ||
432 | ||
433 | /* On some systems, the only way to recognize the link map entry for | |
434 | the main executable file is by looking at its name. Return | |
435 | non-zero iff SONAME matches one of the known main executable names. */ | |
436 | ||
437 | static int | |
438 | match_main (char *soname) | |
439 | { | |
440 | char **mainp; | |
441 | ||
442 | for (mainp = main_name_list; *mainp != NULL; mainp++) | |
443 | { | |
444 | if (strcmp (soname, *mainp) == 0) | |
445 | return (1); | |
446 | } | |
447 | ||
448 | return (0); | |
449 | } | |
450 | ||
451 | ||
452 | static int | |
453 | sunos_in_dynsym_resolve_code (CORE_ADDR pc) | |
454 | { | |
455 | return 0; | |
456 | } | |
457 | ||
7f86f058 | 458 | /* Remove the "mapping changed" breakpoint. |
ab31aa69 KB |
459 | |
460 | Removes the breakpoint that gets hit when the dynamic linker | |
7f86f058 | 461 | completes a mapping change. */ |
ab31aa69 KB |
462 | |
463 | static int | |
464 | disable_break (void) | |
465 | { | |
c378eb4e | 466 | CORE_ADDR breakpoint_addr; /* Address where end bkpt is set. */ |
ab31aa69 KB |
467 | |
468 | int in_debugger = 0; | |
469 | ||
470 | /* Read the debugger structure from the inferior to retrieve the | |
471 | address of the breakpoint and the original contents of the | |
472 | breakpoint address. Remove the breakpoint by writing the original | |
c378eb4e | 473 | contents back. */ |
ab31aa69 KB |
474 | |
475 | read_memory (debug_addr, (char *) &debug_copy, sizeof (debug_copy)); | |
476 | ||
c378eb4e | 477 | /* Set `in_debugger' to zero now. */ |
ab31aa69 KB |
478 | |
479 | write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger)); | |
480 | ||
481 | breakpoint_addr = SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_bp_addr); | |
482 | write_memory (breakpoint_addr, (char *) &debug_copy.ldd_bp_inst, | |
483 | sizeof (debug_copy.ldd_bp_inst)); | |
484 | ||
485 | /* For the SVR4 version, we always know the breakpoint address. For the | |
486 | SunOS version we don't know it until the above code is executed. | |
c378eb4e | 487 | Grumble if we are stopped anywhere besides the breakpoint address. */ |
ab31aa69 KB |
488 | |
489 | if (stop_pc != breakpoint_addr) | |
490 | { | |
3e43a32a MS |
491 | warning (_("stopped at unknown breakpoint " |
492 | "while handling shared libraries")); | |
ab31aa69 KB |
493 | } |
494 | ||
495 | return 1; | |
496 | } | |
497 | ||
7f86f058 | 498 | /* Arrange for dynamic linker to hit breakpoint. |
ab31aa69 KB |
499 | |
500 | Both the SunOS and the SVR4 dynamic linkers have, as part of their | |
501 | debugger interface, support for arranging for the inferior to hit | |
502 | a breakpoint after mapping in the shared libraries. This function | |
503 | enables that breakpoint. | |
504 | ||
505 | For SunOS, there is a special flag location (in_debugger) which we | |
506 | set to 1. When the dynamic linker sees this flag set, it will set | |
507 | a breakpoint at a location known only to itself, after saving the | |
508 | original contents of that place and the breakpoint address itself, | |
509 | in it's own internal structures. When we resume the inferior, it | |
510 | will eventually take a SIGTRAP when it runs into the breakpoint. | |
511 | We handle this (in a different place) by restoring the contents of | |
512 | the breakpointed location (which is only known after it stops), | |
513 | chasing around to locate the shared libraries that have been | |
514 | loaded, then resuming. | |
515 | ||
516 | For SVR4, the debugger interface structure contains a member (r_brk) | |
517 | which is statically initialized at the time the shared library is | |
518 | built, to the offset of a function (_r_debug_state) which is guaran- | |
519 | teed to be called once before mapping in a library, and again when | |
520 | the mapping is complete. At the time we are examining this member, | |
521 | it contains only the unrelocated offset of the function, so we have | |
522 | to do our own relocation. Later, when the dynamic linker actually | |
523 | runs, it relocates r_brk to be the actual address of _r_debug_state(). | |
524 | ||
525 | The debugger interface structure also contains an enumeration which | |
526 | is set to either RT_ADD or RT_DELETE prior to changing the mapping, | |
7f86f058 PA |
527 | depending upon whether or not the library is being mapped or |
528 | unmapped, and then set to RT_CONSISTENT after the library is | |
529 | mapped/unmapped. */ | |
ab31aa69 KB |
530 | |
531 | static int | |
532 | enable_break (void) | |
533 | { | |
534 | int success = 0; | |
535 | int j; | |
536 | int in_debugger; | |
537 | ||
c378eb4e | 538 | /* Get link_dynamic structure. */ |
ab31aa69 KB |
539 | |
540 | j = target_read_memory (debug_base, (char *) &dynamic_copy, | |
541 | sizeof (dynamic_copy)); | |
542 | if (j) | |
543 | { | |
544 | /* unreadable */ | |
545 | return (0); | |
546 | } | |
547 | ||
c378eb4e | 548 | /* Calc address of debugger interface structure. */ |
ab31aa69 KB |
549 | |
550 | debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd); | |
551 | ||
c378eb4e | 552 | /* Calc address of `in_debugger' member of debugger interface structure. */ |
ab31aa69 KB |
553 | |
554 | flag_addr = debug_addr + (CORE_ADDR) ((char *) &debug_copy.ldd_in_debugger - | |
555 | (char *) &debug_copy); | |
556 | ||
557 | /* Write a value of 1 to this member. */ | |
558 | ||
559 | in_debugger = 1; | |
560 | write_memory (flag_addr, (char *) &in_debugger, sizeof (in_debugger)); | |
561 | success = 1; | |
562 | ||
563 | return (success); | |
564 | } | |
565 | ||
7f86f058 | 566 | /* Implement the "special_symbol_handling" target_so_ops method. |
ab31aa69 KB |
567 | |
568 | For SunOS4, this consists of grunging around in the dynamic | |
569 | linkers structures to find symbol definitions for "common" symbols | |
570 | and adding them to the minimal symbol table for the runtime common | |
7f86f058 | 571 | objfile. */ |
ab31aa69 KB |
572 | |
573 | static void | |
574 | sunos_special_symbol_handling (void) | |
575 | { | |
576 | int j; | |
577 | ||
578 | if (debug_addr == 0) | |
579 | { | |
c378eb4e | 580 | /* Get link_dynamic structure. */ |
ab31aa69 KB |
581 | |
582 | j = target_read_memory (debug_base, (char *) &dynamic_copy, | |
583 | sizeof (dynamic_copy)); | |
584 | if (j) | |
585 | { | |
586 | /* unreadable */ | |
587 | return; | |
588 | } | |
589 | ||
c378eb4e | 590 | /* Calc address of debugger interface structure. */ |
ab31aa69 KB |
591 | /* FIXME, this needs work for cross-debugging of core files |
592 | (byteorder, size, alignment, etc). */ | |
593 | ||
594 | debug_addr = SOLIB_EXTRACT_ADDRESS (dynamic_copy.ldd); | |
595 | } | |
596 | ||
597 | /* Read the debugger structure from the inferior, just to make sure | |
c378eb4e | 598 | we have a current copy. */ |
ab31aa69 KB |
599 | |
600 | j = target_read_memory (debug_addr, (char *) &debug_copy, | |
601 | sizeof (debug_copy)); | |
602 | if (j) | |
603 | return; /* unreadable */ | |
604 | ||
c378eb4e | 605 | /* Get common symbol definitions for the loaded object. */ |
ab31aa69 KB |
606 | |
607 | if (debug_copy.ldd_cp) | |
608 | { | |
609 | solib_add_common_symbols (SOLIB_EXTRACT_ADDRESS (debug_copy.ldd_cp)); | |
610 | } | |
611 | } | |
612 | ||
7f86f058 | 613 | /* Implement the "create_inferior_hook" target_solib_ops method. |
ab31aa69 KB |
614 | |
615 | For SunOS executables, this first instruction is typically the | |
616 | one at "_start", or a similar text label, regardless of whether | |
617 | the executable is statically or dynamically linked. The runtime | |
618 | startup code takes care of dynamically linking in any shared | |
619 | libraries, once gdb allows the inferior to continue. | |
620 | ||
7f86f058 PA |
621 | We can arrange to cooperate with the dynamic linker to discover the |
622 | names of shared libraries that are dynamically linked, and the base | |
623 | addresses to which they are linked. | |
ab31aa69 KB |
624 | |
625 | This function is responsible for discovering those names and | |
626 | addresses, and saving sufficient information about them to allow | |
627 | their symbols to be read at a later time. | |
628 | ||
629 | FIXME | |
630 | ||
631 | Between enable_break() and disable_break(), this code does not | |
632 | properly handle hitting breakpoints which the user might have | |
633 | set in the startup code or in the dynamic linker itself. Proper | |
634 | handling will probably have to wait until the implementation is | |
635 | changed to use the "breakpoint handler function" method. | |
636 | ||
7f86f058 | 637 | Also, what if child has exit()ed? Must exit loop somehow. */ |
ab31aa69 KB |
638 | |
639 | static void | |
268a4a75 | 640 | sunos_solib_create_inferior_hook (int from_tty) |
ab31aa69 | 641 | { |
2020b7ab | 642 | struct thread_info *tp; |
d6b48e9c | 643 | struct inferior *inf; |
2020b7ab | 644 | |
ab31aa69 KB |
645 | if ((debug_base = locate_base ()) == 0) |
646 | { | |
c378eb4e | 647 | /* Can't find the symbol or the executable is statically linked. */ |
ab31aa69 KB |
648 | return; |
649 | } | |
650 | ||
651 | if (!enable_break ()) | |
652 | { | |
8a3fe4f8 | 653 | warning (_("shared library handler failed to enable breakpoint")); |
ab31aa69 KB |
654 | return; |
655 | } | |
656 | ||
657 | /* SCO and SunOS need the loop below, other systems should be using the | |
658 | special shared library breakpoints and the shared library breakpoint | |
659 | service routine. | |
660 | ||
661 | Now run the target. It will eventually hit the breakpoint, at | |
662 | which point all of the libraries will have been mapped in and we | |
663 | can go groveling around in the dynamic linker structures to find | |
c378eb4e | 664 | out what we need to know about them. */ |
ab31aa69 | 665 | |
d6b48e9c | 666 | inf = current_inferior (); |
2020b7ab | 667 | tp = inferior_thread (); |
d6b48e9c | 668 | |
ab31aa69 | 669 | clear_proceed_status (); |
d6b48e9c | 670 | |
16c381f0 | 671 | inf->control.stop_soon = STOP_QUIETLY; |
a493e3e2 | 672 | tp->suspend.stop_signal = GDB_SIGNAL_0; |
ab31aa69 KB |
673 | do |
674 | { | |
16c381f0 | 675 | target_resume (pid_to_ptid (-1), 0, tp->suspend.stop_signal); |
e4c8541f | 676 | wait_for_inferior (); |
ab31aa69 | 677 | } |
a493e3e2 | 678 | while (tp->suspend.stop_signal != GDB_SIGNAL_TRAP); |
16c381f0 | 679 | inf->control.stop_soon = NO_STOP_QUIETLY; |
ab31aa69 KB |
680 | |
681 | /* We are now either at the "mapping complete" breakpoint (or somewhere | |
682 | else, a condition we aren't prepared to deal with anyway), so adjust | |
683 | the PC as necessary after a breakpoint, disable the breakpoint, and | |
b4397864 UW |
684 | add any shared libraries that were mapped in. |
685 | ||
686 | Note that adjust_pc_after_break did not perform any PC adjustment, | |
687 | as the breakpoint the inferior just hit was not inserted by GDB, | |
688 | but by the dynamic loader itself, and is therefore not found on | |
689 | the GDB software break point list. Thus we have to adjust the | |
690 | PC here. */ | |
ab31aa69 | 691 | |
f5656ead | 692 | if (gdbarch_decr_pc_after_break (target_gdbarch ())) |
ab31aa69 | 693 | { |
f5656ead | 694 | stop_pc -= gdbarch_decr_pc_after_break (target_gdbarch ()); |
fb14de7b | 695 | regcache_write_pc (get_current_regcache (), stop_pc); |
ab31aa69 KB |
696 | } |
697 | ||
698 | if (!disable_break ()) | |
699 | { | |
8a3fe4f8 | 700 | warning (_("shared library handler failed to disable breakpoint")); |
ab31aa69 KB |
701 | } |
702 | ||
990f9fe3 | 703 | solib_add ((char *) 0, 0, (struct target_ops *) 0, auto_solib_add); |
ab31aa69 KB |
704 | } |
705 | ||
706 | static void | |
707 | sunos_clear_solib (void) | |
708 | { | |
709 | debug_base = 0; | |
710 | } | |
711 | ||
712 | static void | |
713 | sunos_free_so (struct so_list *so) | |
714 | { | |
715 | xfree (so->lm_info->lm); | |
716 | xfree (so->lm_info); | |
717 | } | |
718 | ||
719 | static void | |
720 | sunos_relocate_section_addresses (struct so_list *so, | |
0542c86d | 721 | struct target_section *sec) |
ab31aa69 | 722 | { |
b23518f0 PM |
723 | sec->addr += lm_addr (so); |
724 | sec->endaddr += lm_addr (so); | |
ab31aa69 KB |
725 | } |
726 | ||
727 | static struct target_so_ops sunos_so_ops; | |
728 | ||
729 | void | |
730 | _initialize_sunos_solib (void) | |
731 | { | |
732 | sunos_so_ops.relocate_section_addresses = sunos_relocate_section_addresses; | |
733 | sunos_so_ops.free_so = sunos_free_so; | |
734 | sunos_so_ops.clear_solib = sunos_clear_solib; | |
735 | sunos_so_ops.solib_create_inferior_hook = sunos_solib_create_inferior_hook; | |
736 | sunos_so_ops.special_symbol_handling = sunos_special_symbol_handling; | |
737 | sunos_so_ops.current_sos = sunos_current_sos; | |
738 | sunos_so_ops.open_symbol_file_object = open_symbol_file_object; | |
739 | sunos_so_ops.in_dynsym_resolve_code = sunos_in_dynsym_resolve_code; | |
831a0c44 | 740 | sunos_so_ops.bfd_open = solib_bfd_open; |
ab31aa69 | 741 | |
c378eb4e | 742 | /* FIXME: Don't do this here. *_gdbarch_init() should set so_ops. */ |
ab31aa69 KB |
743 | current_target_so_ops = &sunos_so_ops; |
744 | } |