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