]>
Commit | Line | Data |
---|---|---|
bb140953 | 1 | /* Handle HP SOM shared libraries for GDB, the GNU Debugger. |
87273c71 | 2 | Copyright 1993, 1996 Free Software Foundation, Inc. |
b1c6705a | 3 | |
bb140953 JL |
4 | This file is part of GDB. |
5 | ||
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
6c9638b4 | 18 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
bb140953 JL |
19 | |
20 | Written by the Center for Software Science at the Univerity of Utah | |
21 | and by Cygnus Support. */ | |
22 | ||
23 | ||
24 | #include "defs.h" | |
25 | ||
26 | #include "frame.h" | |
27 | #include "bfd.h" | |
28 | #include "som.h" | |
29 | #include "libhppa.h" | |
30 | #include "gdbcore.h" | |
31 | #include "symtab.h" | |
32 | #include "breakpoint.h" | |
33 | #include "symfile.h" | |
34 | #include "objfiles.h" | |
35 | #include "inferior.h" | |
99e0981c | 36 | #include "gdb-stabs.h" |
2e977a3a | 37 | #include "gdbcmd.h" |
3f550b59 | 38 | #include "language.h" |
bb140953 | 39 | |
b1c6705a JL |
40 | /* TODO: |
41 | ||
b1c6705a JL |
42 | * Most of this code should work for hp300 shared libraries. Does |
43 | anyone care enough to weed out any SOM-isms. | |
44 | ||
87273c71 | 45 | * Support for hpux8 dynamic linker. */ |
b1c6705a JL |
46 | |
47 | /* The basic structure which describes a dynamically loaded object. This | |
48 | data structure is private to the dynamic linker and isn't found in | |
49 | any HPUX include file. */ | |
bb140953 JL |
50 | |
51 | struct som_solib_mapped_entry | |
52 | { | |
53 | /* The name of the library. */ | |
54 | char *name; | |
55 | ||
b1c6705a | 56 | /* Version of this structure (it is expected to change again in hpux10). */ |
bb140953 JL |
57 | unsigned char struct_version; |
58 | ||
59 | /* Binding mode for this library. */ | |
60 | unsigned char bind_mode; | |
61 | ||
62 | /* Version of this library. */ | |
63 | short library_version; | |
64 | ||
65 | /* Start of text address, link-time text location, end of text address. */ | |
66 | CORE_ADDR text_addr; | |
67 | CORE_ADDR text_link_addr; | |
68 | CORE_ADDR text_end; | |
69 | ||
70 | /* Start of data, start of bss and end of data. */ | |
71 | CORE_ADDR data_start; | |
72 | CORE_ADDR bss_start; | |
73 | CORE_ADDR data_end; | |
74 | ||
75 | /* Value of linkage pointer (%r19). */ | |
76 | CORE_ADDR got_value; | |
77 | ||
78 | /* Next entry. */ | |
79 | struct som_solib_mapped_entry *next; | |
80 | ||
81 | /* There are other fields, but I don't have information as to what is | |
82 | contained in them. */ | |
83 | }; | |
84 | ||
b1c6705a JL |
85 | /* A structure to keep track of all the known shared objects. */ |
86 | struct so_list | |
87 | { | |
88 | struct som_solib_mapped_entry som_solib; | |
89 | struct objfile *objfile; | |
90 | bfd *abfd; | |
91 | struct section_table *sections; | |
92 | struct section_table *sections_end; | |
93 | struct so_list *next; | |
94 | }; | |
95 | ||
96 | static struct so_list *so_list_head; | |
97 | ||
98 | static void som_sharedlibrary_info_command PARAMS ((char *, int)); | |
99 | ||
3f550b59 FF |
100 | static void som_solib_sharedlibrary_command PARAMS ((char *, int)); |
101 | ||
bb140953 JL |
102 | /* Add symbols from shared libraries into the symtab list. */ |
103 | ||
104 | void | |
105 | som_solib_add (arg_string, from_tty, target) | |
106 | char *arg_string; | |
107 | int from_tty; | |
108 | struct target_ops *target; | |
b1c6705a | 109 | { |
bb140953 | 110 | struct minimal_symbol *msymbol; |
b1c6705a | 111 | struct so_list *so_list_tail; |
bb140953 | 112 | CORE_ADDR addr; |
b1c6705a | 113 | asection *shlib_info; |
bb140953 | 114 | int status; |
c2e00af6 | 115 | unsigned int dld_flags; |
156285aa JL |
116 | char buf[4], *re_err; |
117 | ||
118 | /* First validate our arguments. */ | |
119 | if ((re_err = re_comp (arg_string ? arg_string : ".")) != NULL) | |
120 | { | |
121 | error ("Invalid regexp: %s", re_err); | |
122 | } | |
bb140953 | 123 | |
c2e00af6 JL |
124 | /* If we're debugging a core file, or have attached to a running |
125 | process, then som_solib_create_inferior_hook will not have been | |
126 | called. | |
127 | ||
b1c6705a JL |
128 | We need to first determine if we're dealing with a dynamically |
129 | linked executable. If not, then return without an error or warning. | |
130 | ||
131 | We also need to examine __dld_flags to determine if the shared library | |
132 | list is valid and to determine if the libraries have been privately | |
c2e00af6 | 133 | mapped. */ |
f0c9e9e5 JL |
134 | if (symfile_objfile == NULL) |
135 | return; | |
136 | ||
b1c6705a JL |
137 | /* First see if the objfile was dynamically linked. */ |
138 | shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$"); | |
139 | if (!shlib_info) | |
140 | return; | |
141 | ||
142 | /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */ | |
143 | if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0) | |
144 | return; | |
145 | ||
2d336b1b | 146 | msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL); |
c2e00af6 JL |
147 | if (msymbol == NULL) |
148 | { | |
149 | error ("Unable to find __dld_flags symbol in object file.\n"); | |
150 | return; | |
151 | } | |
152 | ||
153 | addr = SYMBOL_VALUE_ADDRESS (msymbol); | |
154 | /* Read the current contents. */ | |
155 | status = target_read_memory (addr, buf, 4); | |
156 | if (status != 0) | |
157 | { | |
158 | error ("Unable to read __dld_flags\n"); | |
159 | return; | |
160 | } | |
161 | dld_flags = extract_unsigned_integer (buf, 4); | |
162 | ||
163 | /* __dld_list may not be valid. If it's not valid tell the user. */ | |
164 | if ((dld_flags & 4) == 0) | |
165 | { | |
166 | error ("__dld_list is not valid according to __dld_flags.\n"); | |
167 | return; | |
168 | } | |
169 | ||
170 | /* If the libraries were not mapped private, warn the user. */ | |
171 | if ((dld_flags & 1) == 0) | |
b1c6705a | 172 | warning ("The shared libraries were not privately mapped; setting a\nbreakpoint in a shared library will not work until you rerun the program.\n"); |
c2e00af6 | 173 | |
2d336b1b | 174 | msymbol = lookup_minimal_symbol ("__dld_list", NULL, NULL); |
bb140953 JL |
175 | if (!msymbol) |
176 | { | |
177 | /* Older crt0.o files (hpux8) don't have __dld_list as a symbol, | |
178 | but the data is still available if you know where to look. */ | |
2d336b1b | 179 | msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL); |
bb140953 JL |
180 | if (!msymbol) |
181 | { | |
182 | error ("Unable to find dynamic library list.\n"); | |
183 | return; | |
184 | } | |
185 | addr = SYMBOL_VALUE_ADDRESS (msymbol) - 8; | |
186 | } | |
187 | else | |
188 | addr = SYMBOL_VALUE_ADDRESS (msymbol); | |
189 | ||
190 | status = target_read_memory (addr, buf, 4); | |
191 | if (status != 0) | |
192 | { | |
193 | error ("Unable to find dynamic library list.\n"); | |
194 | return; | |
195 | } | |
196 | ||
197 | addr = extract_unsigned_integer (buf, 4); | |
198 | ||
199 | /* If addr is zero, then we're using an old dynamic loader which | |
200 | doesn't maintain __dld_list. We'll have to use a completely | |
201 | different approach to get shared library information. */ | |
202 | if (addr == 0) | |
203 | goto old_dld; | |
204 | ||
205 | /* Using the information in __dld_list is the preferred method | |
206 | to get at shared library information. It doesn't depend on | |
207 | any functions in /usr/lib/end.o and has a chance of working | |
208 | with hpux10 when it is released. */ | |
209 | status = target_read_memory (addr, buf, 4); | |
210 | if (status != 0) | |
211 | { | |
212 | error ("Unable to find dynamic library list.\n"); | |
213 | return; | |
214 | } | |
215 | ||
216 | /* addr now holds the address of the first entry in the dynamic | |
217 | library list. */ | |
218 | addr = extract_unsigned_integer (buf, 4); | |
219 | ||
220 | /* Now that we have a pointer to the dynamic library list, walk | |
156285aa | 221 | through it and add the symbols for each library. */ |
bb140953 | 222 | |
b1c6705a JL |
223 | so_list_tail = so_list_head; |
224 | /* Find the end of the list of shared objects. */ | |
225 | while (so_list_tail && so_list_tail->next) | |
226 | so_list_tail = so_list_tail->next; | |
227 | ||
bb140953 JL |
228 | while (1) |
229 | { | |
230 | CORE_ADDR name_addr, text_addr; | |
231 | unsigned int name_len; | |
232 | char *name; | |
b1c6705a | 233 | struct so_list *new_so; |
156285aa | 234 | struct so_list *so_list = so_list_head; |
b1c6705a | 235 | struct section_table *p; |
95037710 | 236 | struct stat statbuf; |
b1c6705a | 237 | |
bb140953 JL |
238 | if (addr == 0) |
239 | break; | |
240 | ||
241 | /* Get a pointer to the name of this library. */ | |
242 | status = target_read_memory (addr, buf, 4); | |
243 | if (status != 0) | |
b1c6705a JL |
244 | goto err; |
245 | ||
bb140953 JL |
246 | name_addr = extract_unsigned_integer (buf, 4); |
247 | name_len = 0; | |
248 | while (1) | |
249 | { | |
250 | target_read_memory (name_addr + name_len, buf, 1); | |
251 | if (status != 0) | |
b1c6705a JL |
252 | goto err; |
253 | ||
bb140953 JL |
254 | name_len++; |
255 | if (*buf == '\0') | |
256 | break; | |
257 | } | |
258 | name = alloca (name_len); | |
259 | status = target_read_memory (name_addr, name, name_len); | |
260 | if (status != 0) | |
b1c6705a JL |
261 | goto err; |
262 | ||
156285aa JL |
263 | /* See if we've already loaded something with this name. */ |
264 | while (so_list) | |
265 | { | |
266 | if (!strcmp (so_list->som_solib.name, name)) | |
267 | break; | |
268 | so_list = so_list->next; | |
269 | } | |
270 | ||
95037710 JL |
271 | /* See if the file exists. If not, give a warning, but don't |
272 | die. */ | |
273 | status = stat (name, &statbuf); | |
274 | if (status == -1) | |
275 | { | |
276 | warning ("Can't find file %s referenced in dld_list.", name); | |
277 | ||
278 | status = target_read_memory (addr + 36, buf, 4); | |
279 | if (status != 0) | |
280 | goto err; | |
281 | ||
282 | addr = (CORE_ADDR) extract_unsigned_integer (buf, 4); | |
283 | continue; | |
284 | } | |
285 | ||
286 | /* If we've already loaded this one or it's the main program, skip it. */ | |
156285aa JL |
287 | if (so_list || !strcmp (name, symfile_objfile->name)) |
288 | { | |
289 | status = target_read_memory (addr + 36, buf, 4); | |
290 | if (status != 0) | |
291 | goto err; | |
292 | ||
293 | addr = (CORE_ADDR) extract_unsigned_integer (buf, 4); | |
294 | continue; | |
295 | } | |
296 | ||
bb140953 JL |
297 | name = obsavestring (name, name_len - 1, |
298 | &symfile_objfile->symbol_obstack); | |
299 | ||
300 | status = target_read_memory (addr + 8, buf, 4); | |
301 | if (status != 0) | |
b1c6705a JL |
302 | goto err; |
303 | ||
304 | text_addr = extract_unsigned_integer (buf, 4); | |
305 | ||
306 | ||
c37c7c6c | 307 | new_so = (struct so_list *) xmalloc (sizeof (struct so_list)); |
b1c6705a JL |
308 | memset ((char *)new_so, 0, sizeof (struct so_list)); |
309 | if (so_list_head == NULL) | |
bb140953 | 310 | { |
b1c6705a JL |
311 | so_list_head = new_so; |
312 | so_list_tail = new_so; | |
bb140953 | 313 | } |
b1c6705a JL |
314 | else |
315 | { | |
316 | so_list_tail->next = new_so; | |
317 | so_list_tail = new_so; | |
318 | } | |
319 | ||
320 | /* Fill in all the entries in GDB's shared library list. */ | |
321 | new_so->som_solib.name = name; | |
322 | status = target_read_memory (addr + 4, buf, 4); | |
323 | if (status != 0) | |
324 | goto err; | |
325 | ||
326 | new_so->som_solib.struct_version = extract_unsigned_integer (buf + 3, 1); | |
327 | new_so->som_solib.bind_mode = extract_unsigned_integer (buf + 2, 1); | |
328 | new_so->som_solib.library_version = extract_unsigned_integer (buf, 2); | |
329 | new_so->som_solib.text_addr = text_addr; | |
330 | ||
331 | status = target_read_memory (addr + 12, buf, 4); | |
332 | if (status != 0) | |
333 | goto err; | |
334 | ||
335 | new_so->som_solib.text_link_addr = extract_unsigned_integer (buf, 4); | |
bb140953 | 336 | |
b1c6705a JL |
337 | status = target_read_memory (addr + 16, buf, 4); |
338 | if (status != 0) | |
339 | goto err; | |
340 | ||
341 | new_so->som_solib.text_end = extract_unsigned_integer (buf, 4); | |
342 | ||
343 | status = target_read_memory (addr + 20, buf, 4); | |
344 | if (status != 0) | |
345 | goto err; | |
346 | ||
347 | new_so->som_solib.data_start = extract_unsigned_integer (buf, 4); | |
348 | ||
349 | status = target_read_memory (addr + 24, buf, 4); | |
350 | if (status != 0) | |
351 | goto err; | |
352 | ||
353 | new_so->som_solib.bss_start = extract_unsigned_integer (buf, 4); | |
354 | ||
355 | status = target_read_memory (addr + 28, buf, 4); | |
356 | if (status != 0) | |
357 | goto err; | |
358 | ||
359 | new_so->som_solib.data_end = extract_unsigned_integer (buf, 4); | |
360 | ||
361 | status = target_read_memory (addr + 32, buf, 4); | |
362 | if (status != 0) | |
363 | goto err; | |
364 | ||
365 | new_so->som_solib.got_value = extract_unsigned_integer (buf, 4); | |
bb140953 | 366 | |
bb140953 JL |
367 | status = target_read_memory (addr + 36, buf, 4); |
368 | if (status != 0) | |
b1c6705a JL |
369 | goto err; |
370 | ||
371 | new_so->som_solib.next = (void *)extract_unsigned_integer (buf, 4); | |
372 | addr = (CORE_ADDR)new_so->som_solib.next; | |
373 | ||
374 | new_so->objfile = symbol_file_add (name, from_tty, text_addr, 0, 0, 0); | |
506af7a7 | 375 | new_so->abfd = new_so->objfile->obfd; |
b1c6705a JL |
376 | |
377 | if (!bfd_check_format (new_so->abfd, bfd_object)) | |
378 | { | |
379 | error ("\"%s\": not in executable format: %s.", | |
380 | name, bfd_errmsg (bfd_get_error ())); | |
381 | } | |
382 | ||
383 | /* Now we need to build a section table for this library since | |
384 | we might be debugging a core file from a dynamically linked | |
385 | executable in which the libraries were not privately mapped. */ | |
386 | if (build_section_table (new_so->abfd, | |
387 | &new_so->sections, | |
388 | &new_so->sections_end)) | |
bb140953 | 389 | { |
b1c6705a | 390 | error ("Unable to build section table for shared library\n."); |
bb140953 JL |
391 | return; |
392 | } | |
b1c6705a JL |
393 | |
394 | /* Relocate all the sections based on where they got loaded. */ | |
395 | for (p = new_so->sections; p < new_so->sections_end; p++) | |
396 | { | |
397 | if (p->the_bfd_section->flags & SEC_CODE) | |
398 | { | |
399 | p->addr += text_addr - new_so->som_solib.text_link_addr; | |
400 | p->endaddr += text_addr - new_so->som_solib.text_link_addr; | |
401 | } | |
402 | else if (p->the_bfd_section->flags & SEC_DATA) | |
403 | { | |
404 | p->addr += new_so->som_solib.data_start; | |
405 | p->endaddr += new_so->som_solib.data_start; | |
406 | } | |
407 | } | |
408 | ||
409 | /* Now see if we need to map in the text and data for this shared | |
410 | library (for example debugging a core file which does not use | |
411 | private shared libraries.). | |
412 | ||
413 | Carefully peek at the first text address in the library. If the | |
414 | read succeeds, then the libraries were privately mapped and were | |
415 | included in the core dump file. | |
416 | ||
417 | If the peek failed, then the libraries were not privately mapped | |
418 | and are not in the core file, we'll have to read them in ourselves. */ | |
419 | status = target_read_memory (text_addr, buf, 4); | |
420 | if (status != 0) | |
421 | { | |
9bfed1ee | 422 | int old, new; |
148070cc JL |
423 | int update_coreops; |
424 | ||
425 | /* We must update the to_sections field in the core_ops structure | |
426 | here, otherwise we dereference a potential dangling pointer | |
427 | for each call to target_read/write_memory within this routine. */ | |
428 | update_coreops = core_ops.to_sections == target->to_sections; | |
b1c6705a | 429 | |
9bfed1ee | 430 | new = new_so->sections_end - new_so->sections; |
b1c6705a JL |
431 | /* Add sections from the shared library to the core target. */ |
432 | if (target->to_sections) | |
433 | { | |
434 | old = target->to_sections_end - target->to_sections; | |
435 | target->to_sections = (struct section_table *) | |
436 | xrealloc ((char *)target->to_sections, | |
9bfed1ee | 437 | ((sizeof (struct section_table)) * (old + new))); |
b1c6705a JL |
438 | } |
439 | else | |
440 | { | |
441 | old = 0; | |
442 | target->to_sections = (struct section_table *) | |
9bfed1ee | 443 | xmalloc ((sizeof (struct section_table)) * new); |
b1c6705a | 444 | } |
9bfed1ee | 445 | target->to_sections_end = (target->to_sections + old + new); |
148070cc JL |
446 | |
447 | /* Update the to_sections field in the core_ops structure | |
448 | if needed. */ | |
449 | if (update_coreops) | |
450 | { | |
451 | core_ops.to_sections = target->to_sections; | |
452 | core_ops.to_sections_end = target->to_sections_end; | |
453 | } | |
454 | ||
455 | /* Copy over the old data before it gets clobbered. */ | |
b1c6705a JL |
456 | memcpy ((char *)(target->to_sections + old), |
457 | new_so->sections, | |
9bfed1ee | 458 | ((sizeof (struct section_table)) * new)); |
b1c6705a | 459 | } |
bb140953 JL |
460 | } |
461 | ||
462 | /* Getting new symbols may change our opinion about what is | |
463 | frameless. */ | |
464 | reinit_frame_cache (); | |
465 | return; | |
466 | ||
467 | old_dld: | |
468 | error ("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported.\n"); | |
469 | return; | |
b1c6705a JL |
470 | |
471 | err: | |
472 | error ("Error while reading dynamic library list.\n"); | |
473 | return; | |
bb140953 JL |
474 | } |
475 | ||
b1c6705a | 476 | |
bb140953 JL |
477 | /* This hook gets called just before the first instruction in the |
478 | inferior process is executed. | |
479 | ||
480 | This is our opportunity to set magic flags in the inferior so | |
481 | that GDB can be notified when a shared library is mapped in and | |
482 | to tell the dynamic linker that a private copy of the library is | |
483 | needed (so GDB can set breakpoints in the library). | |
484 | ||
485 | __dld_flags is the location of the magic flags; as of this implementation | |
486 | there are 3 flags of interest: | |
487 | ||
488 | bit 0 when set indicates that private copies of the libraries are needed | |
489 | bit 1 when set indicates that the callback hook routine is valid | |
490 | bit 2 when set indicates that the dynamic linker should maintain the | |
491 | __dld_list structure when loading/unloading libraries. | |
492 | ||
493 | Note that shared libraries are not mapped in at this time, so we have | |
494 | run the inferior until the libraries are mapped in. Typically this | |
495 | means running until the "_start" is called. */ | |
b1c6705a JL |
496 | |
497 | void | |
bb140953 JL |
498 | som_solib_create_inferior_hook() |
499 | { | |
500 | struct minimal_symbol *msymbol; | |
87273c71 | 501 | unsigned int dld_flags, status, have_endo; |
b1c6705a | 502 | asection *shlib_info; |
3f550b59 | 503 | char buf[4]; |
87273c71 | 504 | struct objfile *objfile; |
bb140953 JL |
505 | CORE_ADDR anaddr; |
506 | ||
ff7f8f2d JL |
507 | /* First, remove all the solib event breakpoints. Their addresses |
508 | may have changed since the last time we ran the program. */ | |
509 | remove_solib_event_breakpoints (); | |
510 | ||
f0c9e9e5 JL |
511 | if (symfile_objfile == NULL) |
512 | return; | |
513 | ||
bb140953 JL |
514 | /* First see if the objfile was dynamically linked. */ |
515 | shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$"); | |
516 | if (!shlib_info) | |
517 | return; | |
518 | ||
519 | /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */ | |
520 | if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0) | |
521 | return; | |
522 | ||
87273c71 | 523 | have_endo = 0; |
5499f9a1 JL |
524 | /* If __d_pid is present, then put the inferior's pid into __d_pid. hpux9 |
525 | requires __d_pid to be set. hpux10 doesn't require __d_pid to be set | |
6f359895 JL |
526 | and the symbol may not be available. |
527 | ||
528 | Never warn about __d_pid. */ | |
87273c71 | 529 | msymbol = lookup_minimal_symbol ("__d_pid", NULL, symfile_objfile); |
6f359895 | 530 | if (msymbol != NULL) |
87273c71 | 531 | { |
5499f9a1 JL |
532 | anaddr = SYMBOL_VALUE_ADDRESS (msymbol); |
533 | store_unsigned_integer (buf, 4, inferior_pid); | |
534 | status = target_write_memory (anaddr, buf, 4); | |
535 | if (status != 0) | |
536 | { | |
537 | warning ("Unable to write __d_pid"); | |
538 | goto keep_going; | |
539 | } | |
87273c71 JL |
540 | } |
541 | ||
5499f9a1 JL |
542 | /* If __d_trap_fptr exists, then load whatever's at that address |
543 | and put it into __dld_hook. */ | |
544 | msymbol = lookup_minimal_symbol ("__d_trap_fptr", NULL, symfile_objfile); | |
545 | if (msymbol != NULL) | |
87273c71 | 546 | { |
5499f9a1 JL |
547 | anaddr = SYMBOL_VALUE_ADDRESS (msymbol); |
548 | status = target_read_memory (anaddr, buf, 4); | |
549 | anaddr = extract_unsigned_integer (buf, 4); | |
550 | ||
551 | /* If it's a plabel, then get the address of the real function. | |
552 | Egad. This is just the opposite of how hpux9 and _DLD_HOOK | |
553 | works. */ | |
554 | if (anaddr | 0x2) | |
555 | { | |
556 | status = target_read_memory (anaddr & ~0x2, buf, 4); | |
557 | anaddr = extract_unsigned_integer (buf, 4); | |
558 | } | |
87273c71 | 559 | } |
5499f9a1 | 560 | else |
87273c71 | 561 | { |
5499f9a1 JL |
562 | /* Get the value of _DLD_HOOK (an export stub) and put it in __dld_hook; |
563 | This will force the dynamic linker to call __d_trap when significant | |
564 | events occur. */ | |
565 | msymbol = lookup_minimal_symbol ("_DLD_HOOK", NULL, symfile_objfile); | |
566 | if (msymbol == NULL) | |
87273c71 | 567 | { |
5499f9a1 JL |
568 | warning ("Unable to find _DLD_HOOK symbol in object file."); |
569 | warning ("Suggest linking with /usr/lib/end.o."); | |
570 | warning ("GDB will be unable to track shl_load/shl_unload calls"); | |
571 | goto keep_going; | |
87273c71 | 572 | } |
5499f9a1 JL |
573 | anaddr = SYMBOL_VALUE_ADDRESS (msymbol); |
574 | ||
575 | /* Grrr, this might not be an export symbol! We have to find the | |
576 | export stub. */ | |
577 | ALL_OBJFILES (objfile) | |
578 | { | |
5499f9a1 JL |
579 | extern struct unwind_table_entry *find_unwind_entry PARAMS ((CORE_ADDR pc)); |
580 | ||
581 | /* What a crock. */ | |
582 | msymbol | |
583 | = lookup_minimal_symbol_solib_trampoline (SYMBOL_NAME (msymbol), | |
584 | NULL, objfile); | |
585 | /* Found a symbol with the right name. */ | |
586 | if (msymbol) | |
587 | { | |
588 | struct unwind_table_entry *u; | |
589 | /* It must be a shared library trampoline. */ | |
e5718bee | 590 | if (MSYMBOL_TYPE (msymbol) != mst_solib_trampoline) |
5499f9a1 JL |
591 | continue; |
592 | ||
593 | /* It must also be an export stub. */ | |
594 | u = find_unwind_entry (SYMBOL_VALUE (msymbol)); | |
595 | if (!u || u->stub_type != EXPORT) | |
596 | continue; | |
597 | ||
598 | /* OK. Looks like the correct import stub. */ | |
599 | anaddr = SYMBOL_VALUE (msymbol); | |
600 | break; | |
601 | } | |
602 | } | |
87273c71 JL |
603 | } |
604 | store_unsigned_integer (buf, 4, anaddr); | |
605 | ||
606 | msymbol = lookup_minimal_symbol ("__dld_hook", NULL, symfile_objfile); | |
607 | if (msymbol == NULL) | |
608 | { | |
ff7f8f2d JL |
609 | warning ("Unable to find __dld_hook symbol in object file."); |
610 | warning ("Suggest linking with /usr/lib/end.o."); | |
611 | warning ("GDB will be unable to track shl_load/shl_unload calls"); | |
87273c71 JL |
612 | goto keep_going; |
613 | } | |
614 | anaddr = SYMBOL_VALUE_ADDRESS (msymbol); | |
615 | status = target_write_memory (anaddr, buf, 4); | |
616 | ||
617 | /* Now set a shlib_event breakpoint at __d_trap so we can track | |
618 | significant shared library events. */ | |
619 | msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile); | |
620 | if (msymbol == NULL) | |
621 | { | |
ff7f8f2d JL |
622 | warning ("Unable to find __dld_d_trap symbol in object file."); |
623 | warning ("Suggest linking with /usr/lib/end.o."); | |
624 | warning ("GDB will be unable to track shl_load/shl_unload calls"); | |
87273c71 JL |
625 | goto keep_going; |
626 | } | |
627 | create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol)); | |
628 | ||
629 | /* We have all the support usually found in end.o, so we can track | |
630 | shl_load and shl_unload calls. */ | |
631 | have_endo = 1; | |
632 | ||
633 | keep_going: | |
634 | ||
bb140953 JL |
635 | /* Get the address of __dld_flags, if no such symbol exists, then we can |
636 | not debug the shared code. */ | |
2d336b1b | 637 | msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL); |
bb140953 JL |
638 | if (msymbol == NULL) |
639 | { | |
640 | error ("Unable to find __dld_flags symbol in object file.\n"); | |
87273c71 | 641 | goto keep_going; |
bb140953 JL |
642 | return; |
643 | } | |
644 | ||
645 | anaddr = SYMBOL_VALUE_ADDRESS (msymbol); | |
646 | /* Read the current contents. */ | |
647 | status = target_read_memory (anaddr, buf, 4); | |
648 | if (status != 0) | |
649 | { | |
650 | error ("Unable to read __dld_flags\n"); | |
651 | return; | |
652 | } | |
653 | dld_flags = extract_unsigned_integer (buf, 4); | |
654 | ||
655 | /* Turn on the flags we care about. */ | |
87273c71 | 656 | dld_flags |= (0x5 | (have_endo << 1)); |
bb140953 JL |
657 | store_unsigned_integer (buf, 4, dld_flags); |
658 | status = target_write_memory (anaddr, buf, 4); | |
659 | if (status != 0) | |
660 | { | |
661 | error ("Unable to write __dld_flags\n"); | |
662 | return; | |
663 | } | |
664 | ||
87273c71 JL |
665 | /* Now find the address of _start and set a breakpoint there. |
666 | We still need this code for two reasons: | |
667 | ||
668 | * Not all sites have /usr/lib/end.o, so it's not always | |
669 | possible to track the dynamic linker's events. | |
670 | ||
671 | * At this time no events are triggered for shared libraries | |
672 | loaded at startup time (what a crock). */ | |
673 | ||
2d336b1b | 674 | msymbol = lookup_minimal_symbol ("_start", NULL, symfile_objfile); |
bb140953 JL |
675 | if (msymbol == NULL) |
676 | { | |
677 | error ("Unable to find _start symbol in object file.\n"); | |
678 | return; | |
679 | } | |
680 | ||
681 | anaddr = SYMBOL_VALUE_ADDRESS (msymbol); | |
ff7f8f2d JL |
682 | |
683 | /* Make the breakpoint at "_start" a shared library event breakpoint. */ | |
684 | create_solib_event_breakpoint (anaddr); | |
bb140953 | 685 | |
b1c6705a JL |
686 | /* Wipe out all knowledge of old shared libraries since their |
687 | mapping can change from one exec to another! */ | |
688 | while (so_list_head) | |
689 | { | |
690 | struct so_list *temp; | |
691 | ||
692 | free_objfile (so_list_head->objfile); | |
693 | temp = so_list_head; | |
694 | free (so_list_head); | |
695 | so_list_head = temp->next; | |
696 | } | |
a4f39c06 | 697 | clear_symtab_users (); |
bb140953 | 698 | } |
b1c6705a | 699 | |
b1bbe38b JL |
700 | /* Return the GOT value for the shared library in which ADDR belongs. If |
701 | ADDR isn't in any known shared library, return zero. */ | |
702 | ||
703 | CORE_ADDR | |
704 | som_solib_get_got_by_pc (addr) | |
705 | CORE_ADDR addr; | |
706 | { | |
707 | struct so_list *so_list = so_list_head; | |
708 | CORE_ADDR got_value = 0; | |
709 | ||
710 | while (so_list) | |
711 | { | |
712 | if (so_list->som_solib.text_addr <= addr | |
713 | && so_list->som_solib.text_end > addr) | |
714 | { | |
715 | got_value = so_list->som_solib.got_value; | |
716 | break; | |
717 | } | |
718 | so_list = so_list->next; | |
719 | } | |
720 | return got_value; | |
721 | } | |
722 | ||
506af7a7 JL |
723 | int |
724 | som_solib_section_offsets (objfile, offsets) | |
725 | struct objfile *objfile; | |
726 | struct section_offsets *offsets; | |
727 | { | |
728 | struct so_list *so_list = so_list_head; | |
729 | ||
730 | while (so_list) | |
731 | { | |
732 | /* Oh what a pain! We need the offsets before so_list->objfile | |
733 | is valid. The BFDs will never match. Make a best guess. */ | |
fbf8d7e1 | 734 | if (strstr (objfile->name, so_list->som_solib.name)) |
506af7a7 JL |
735 | { |
736 | asection *private_section; | |
737 | ||
738 | /* The text offset is easy. */ | |
99e0981c JL |
739 | ANOFFSET (offsets, SECT_OFF_TEXT) |
740 | = (so_list->som_solib.text_addr | |
741 | - so_list->som_solib.text_link_addr); | |
742 | ANOFFSET (offsets, SECT_OFF_RODATA) | |
743 | = ANOFFSET (offsets, SECT_OFF_TEXT); | |
506af7a7 JL |
744 | |
745 | /* We should look at presumed_dp in the SOM header, but | |
746 | that's not easily available. This should be OK though. */ | |
747 | private_section = bfd_get_section_by_name (objfile->obfd, | |
748 | "$PRIVATE$"); | |
749 | if (!private_section) | |
750 | { | |
751 | warning ("Unable to find $PRIVATE$ in shared library!"); | |
99e0981c JL |
752 | ANOFFSET (offsets, SECT_OFF_DATA) = 0; |
753 | ANOFFSET (offsets, SECT_OFF_BSS) = 0; | |
506af7a7 JL |
754 | return 1; |
755 | } | |
99e0981c JL |
756 | ANOFFSET (offsets, SECT_OFF_DATA) |
757 | = (so_list->som_solib.data_start - private_section->vma); | |
758 | ANOFFSET (offsets, SECT_OFF_BSS) | |
759 | = ANOFFSET (offsets, SECT_OFF_DATA); | |
506af7a7 JL |
760 | return 1; |
761 | } | |
762 | so_list = so_list->next; | |
763 | } | |
764 | return 0; | |
765 | } | |
766 | ||
b1c6705a JL |
767 | /* Dump information about all the currently loaded shared libraries. */ |
768 | ||
769 | static void | |
770 | som_sharedlibrary_info_command (ignore, from_tty) | |
771 | char *ignore; | |
772 | int from_tty; | |
773 | { | |
774 | struct so_list *so_list = so_list_head; | |
775 | ||
776 | if (exec_bfd == NULL) | |
777 | { | |
778 | printf_unfiltered ("no exec file.\n"); | |
779 | return; | |
780 | } | |
781 | ||
782 | if (so_list == NULL) | |
783 | { | |
784 | printf_unfiltered ("No shared libraries loaded at this time.\n"); | |
785 | return; | |
786 | } | |
787 | ||
788 | printf_unfiltered ("Shared Object Libraries\n"); | |
789 | printf_unfiltered (" %-12s%-12s%-12s%-12s%-12s%-12s\n", | |
790 | " flags", " tstart", " tend", " dstart", " dend", " dlt"); | |
791 | while (so_list) | |
792 | { | |
793 | unsigned int flags; | |
794 | ||
795 | flags = so_list->som_solib.struct_version << 24; | |
796 | flags |= so_list->som_solib.bind_mode << 16; | |
797 | flags |= so_list->som_solib.library_version; | |
798 | printf_unfiltered ("%s\n", so_list->som_solib.name); | |
799 | printf_unfiltered (" %-12s", local_hex_string_custom (flags, "08l")); | |
800 | printf_unfiltered ("%-12s", | |
801 | local_hex_string_custom (so_list->som_solib.text_addr, "08l")); | |
802 | printf_unfiltered ("%-12s", | |
803 | local_hex_string_custom (so_list->som_solib.text_end, "08l")); | |
804 | printf_unfiltered ("%-12s", | |
805 | local_hex_string_custom (so_list->som_solib.data_start, "08l")); | |
806 | printf_unfiltered ("%-12s", | |
807 | local_hex_string_custom (so_list->som_solib.data_end, "08l")); | |
808 | printf_unfiltered ("%-12s\n", | |
809 | local_hex_string_custom (so_list->som_solib.got_value, "08l")); | |
810 | so_list = so_list->next; | |
811 | } | |
812 | } | |
813 | ||
156285aa JL |
814 | static void |
815 | som_solib_sharedlibrary_command (args, from_tty) | |
816 | char *args; | |
817 | int from_tty; | |
818 | { | |
819 | dont_repeat (); | |
820 | som_solib_add (args, from_tty, (struct target_ops *) 0); | |
821 | } | |
822 | ||
b1c6705a JL |
823 | void |
824 | _initialize_som_solib () | |
825 | { | |
156285aa JL |
826 | add_com ("sharedlibrary", class_files, som_solib_sharedlibrary_command, |
827 | "Load shared object library symbols for files matching REGEXP."); | |
b1c6705a JL |
828 | add_info ("sharedlibrary", som_sharedlibrary_info_command, |
829 | "Status of loaded shared object libraries."); | |
2e977a3a JL |
830 | add_show_from_set |
831 | (add_set_cmd ("auto-solib-add", class_support, var_zinteger, | |
87273c71 | 832 | (char *) &auto_solib_add, |
2e977a3a JL |
833 | "Set autoloading of shared library symbols at startup.\n\ |
834 | If nonzero, symbols from all shared object libraries will be loaded\n\ | |
87273c71 JL |
835 | automatically when the inferior begins execution or when the dynamic linker\n\ |
836 | informs gdb that a new library has been loaded. Otherwise, symbols\n\ | |
2e977a3a JL |
837 | must be loaded manually, using `sharedlibrary'.", |
838 | &setlist), | |
839 | &showlist); | |
840 | ||
b1c6705a | 841 | } |