]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/solib-som.c
Updated copyright notices for most files.
[thirdparty/binutils-gdb.git] / gdb / solib-som.c
1 /* Handle SOM shared libraries.
2
3 Copyright (C) 2004, 2005, 2007, 2008, 2009 Free Software Foundation, Inc.
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
9 the Free Software Foundation; either version 3 of the License, or
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "symtab.h"
22 #include "bfd.h"
23 #include "symfile.h"
24 #include "objfiles.h"
25 #include "gdbcore.h"
26 #include "target.h"
27 #include "inferior.h"
28
29 #include "hppa-tdep.h"
30 #include "solist.h"
31 #include "solib.h"
32
33 #include <sys/utsname.h>
34 #include <string.h>
35
36 #undef SOLIB_SOM_DBG
37
38 /* These ought to be defined in some public interface, but aren't. They
39 define the meaning of the various bits in the distinguished __dld_flags
40 variable that is declared in every debuggable a.out on HP-UX, and that
41 is shared between the debugger and the dynamic linker.
42 */
43 #define DLD_FLAGS_MAPPRIVATE 0x1
44 #define DLD_FLAGS_HOOKVALID 0x2
45 #define DLD_FLAGS_LISTVALID 0x4
46 #define DLD_FLAGS_BOR_ENABLE 0x8
47
48 struct lm_info
49 {
50 /* Version of this structure (it is expected to change again in hpux10). */
51 unsigned char struct_version;
52
53 /* Binding mode for this library. */
54 unsigned char bind_mode;
55
56 /* Version of this library. */
57 short library_version;
58
59 /* Start of text address,
60 link-time text location (length of text area),
61 end of text address. */
62 CORE_ADDR text_addr;
63 CORE_ADDR text_link_addr;
64 CORE_ADDR text_end;
65
66 /* Start of data, start of bss and end of data. */
67 CORE_ADDR data_start;
68 CORE_ADDR bss_start;
69 CORE_ADDR data_end;
70
71 /* Value of linkage pointer (%r19). */
72 CORE_ADDR got_value;
73
74 /* Address in target of offset from thread-local register of
75 start of this thread's data. I.e., the first thread-local
76 variable in this shared library starts at *(tsd_start_addr)
77 from that area pointed to by cr27 (mpsfu_hi).
78
79 We do the indirection as soon as we read it, so from then
80 on it's the offset itself. */
81 CORE_ADDR tsd_start_addr;
82
83 /* Address of the link map entry in the loader. */
84 CORE_ADDR lm_addr;
85 };
86
87 /* These addresses should be filled in by som_solib_create_inferior_hook.
88 They are also used elsewhere in this module.
89 */
90 typedef struct
91 {
92 CORE_ADDR address;
93 struct unwind_table_entry *unwind;
94 }
95 addr_and_unwind_t;
96
97 /* When adding fields, be sure to clear them in _initialize_som_solib. */
98 static struct
99 {
100 int is_valid;
101 addr_and_unwind_t hook;
102 addr_and_unwind_t hook_stub;
103 addr_and_unwind_t load;
104 addr_and_unwind_t load_stub;
105 addr_and_unwind_t unload;
106 addr_and_unwind_t unload2;
107 addr_and_unwind_t unload_stub;
108 }
109 dld_cache;
110
111 static void
112 som_relocate_section_addresses (struct so_list *so,
113 struct section_table *sec)
114 {
115 flagword aflag = bfd_get_section_flags(so->abfd, sec->the_bfd_section);
116
117 if (aflag & SEC_CODE)
118 {
119 sec->addr += so->lm_info->text_addr - so->lm_info->text_link_addr;
120 sec->endaddr += so->lm_info->text_addr - so->lm_info->text_link_addr;
121 }
122 else if (aflag & SEC_DATA)
123 {
124 sec->addr += so->lm_info->data_start;
125 sec->endaddr += so->lm_info->data_start;
126 }
127 else
128 ;
129 }
130
131 /* Get HP-UX major release number. Returns zero if the
132 release is not known. */
133
134 static int
135 get_hpux_major_release (void)
136 {
137 static int hpux_major_release = -1;
138
139 if (hpux_major_release == -1)
140 {
141 struct utsname x;
142 char *p;
143
144 uname (&x);
145 p = strchr (x.release, '.');
146 hpux_major_release = p ? atoi (p + 1) : 0;
147 }
148
149 return hpux_major_release;
150 }
151
152 /* DL header flag defines. */
153 #define SHLIB_TEXT_PRIVATE_ENABLE 0x4000
154
155 /* The DL header is documented in <shl.h>. We are only interested
156 in the flags field to determine whether the executable wants shared
157 libraries mapped private. */
158 struct {
159 short junk[37];
160 short flags;
161 } dl_header;
162
163 /* This hook gets called just before the first instruction in the
164 inferior process is executed.
165
166 This is our opportunity to set magic flags in the inferior so
167 that GDB can be notified when a shared library is mapped in and
168 to tell the dynamic linker that a private copy of the library is
169 needed (so GDB can set breakpoints in the library).
170
171 __dld_flags is the location of the magic flags; as of this implementation
172 there are 3 flags of interest:
173
174 bit 0 when set indicates that private copies of the libraries are needed
175 bit 1 when set indicates that the callback hook routine is valid
176 bit 2 when set indicates that the dynamic linker should maintain the
177 __dld_list structure when loading/unloading libraries.
178
179 Note that shared libraries are not mapped in at this time, so we have
180 run the inferior until the libraries are mapped in. Typically this
181 means running until the "_start" is called. */
182
183 static void
184 som_solib_create_inferior_hook (void)
185 {
186 struct minimal_symbol *msymbol;
187 unsigned int dld_flags, status, have_endo;
188 asection *shlib_info;
189 char buf[4];
190 CORE_ADDR anaddr;
191
192 /* First, remove all the solib event breakpoints. Their addresses
193 may have changed since the last time we ran the program. */
194 remove_solib_event_breakpoints ();
195
196 if (symfile_objfile == NULL)
197 return;
198
199 /* First see if the objfile was dynamically linked. */
200 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
201 if (!shlib_info)
202 return;
203
204 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
205 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
206 return;
207
208 /* Read the DL header. */
209 bfd_get_section_contents (symfile_objfile->obfd, shlib_info,
210 (char *) &dl_header, 0, sizeof (dl_header));
211
212 have_endo = 0;
213 /* Slam the pid of the process into __d_pid.
214
215 We used to warn when this failed, but that warning is only useful
216 on very old HP systems (hpux9 and older). The warnings are an
217 annoyance to users of modern systems and foul up the testsuite as
218 well. As a result, the warnings have been disabled. */
219 msymbol = lookup_minimal_symbol ("__d_pid", NULL, symfile_objfile);
220 if (msymbol == NULL)
221 goto keep_going;
222
223 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
224 store_unsigned_integer (buf, 4, PIDGET (inferior_ptid));
225 status = target_write_memory (anaddr, buf, 4);
226 if (status != 0)
227 {
228 warning (_("\
229 Unable to write __d_pid.\n\
230 Suggest linking with /opt/langtools/lib/end.o.\n\
231 GDB will be unable to track shl_load/shl_unload calls"));
232 goto keep_going;
233 }
234
235 /* Get the value of _DLD_HOOK (an export stub) and put it in __dld_hook;
236 This will force the dynamic linker to call __d_trap when significant
237 events occur.
238
239 Note that the above is the pre-HP-UX 9.0 behaviour. At 9.0 and above,
240 the dld provides an export stub named "__d_trap" as well as the
241 function named "__d_trap" itself, but doesn't provide "_DLD_HOOK".
242 We'll look first for the old flavor and then the new.
243 */
244 msymbol = lookup_minimal_symbol ("_DLD_HOOK", NULL, symfile_objfile);
245 if (msymbol == NULL)
246 msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile);
247 if (msymbol == NULL)
248 {
249 warning (_("\
250 Unable to find _DLD_HOOK symbol in object file.\n\
251 Suggest linking with /opt/langtools/lib/end.o.\n\
252 GDB will be unable to track shl_load/shl_unload calls"));
253 goto keep_going;
254 }
255 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
256 dld_cache.hook.address = anaddr;
257
258 /* Grrr, this might not be an export symbol! We have to find the
259 export stub. */
260 msymbol = hppa_lookup_stub_minimal_symbol (SYMBOL_LINKAGE_NAME (msymbol),
261 EXPORT);
262 if (msymbol != NULL)
263 {
264 anaddr = SYMBOL_VALUE (msymbol);
265 dld_cache.hook_stub.address = anaddr;
266 }
267 store_unsigned_integer (buf, 4, anaddr);
268
269 msymbol = lookup_minimal_symbol ("__dld_hook", NULL, symfile_objfile);
270 if (msymbol == NULL)
271 {
272 warning (_("\
273 Unable to find __dld_hook symbol in object file.\n\
274 Suggest linking with /opt/langtools/lib/end.o.\n\
275 GDB will be unable to track shl_load/shl_unload calls"));
276 goto keep_going;
277 }
278 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
279 status = target_write_memory (anaddr, buf, 4);
280
281 /* Now set a shlib_event breakpoint at __d_trap so we can track
282 significant shared library events. */
283 msymbol = lookup_minimal_symbol ("__d_trap", NULL, symfile_objfile);
284 if (msymbol == NULL)
285 {
286 warning (_("\
287 Unable to find __dld_d_trap symbol in object file.\n\
288 Suggest linking with /opt/langtools/lib/end.o.\n\
289 GDB will be unable to track shl_load/shl_unload calls"));
290 goto keep_going;
291 }
292 create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
293
294 /* We have all the support usually found in end.o, so we can track
295 shl_load and shl_unload calls. */
296 have_endo = 1;
297
298 keep_going:
299
300 /* Get the address of __dld_flags, if no such symbol exists, then we can
301 not debug the shared code. */
302 msymbol = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
303 if (msymbol == NULL)
304 {
305 error (_("Unable to find __dld_flags symbol in object file."));
306 }
307
308 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
309
310 /* Read the current contents. */
311 status = target_read_memory (anaddr, buf, 4);
312 if (status != 0)
313 error (_("Unable to read __dld_flags."));
314 dld_flags = extract_unsigned_integer (buf, 4);
315
316 /* If the libraries were not mapped private on HP-UX 11 and later, warn
317 the user. On HP-UX 10 and earlier, there is no easy way to specify
318 that shared libraries should be privately mapped. So, we just force
319 private mapping. */
320 if (get_hpux_major_release () >= 11
321 && (dl_header.flags & SHLIB_TEXT_PRIVATE_ENABLE) == 0
322 && (dld_flags & DLD_FLAGS_MAPPRIVATE) == 0)
323 warning
324 (_("Private mapping of shared library text was not specified\n"
325 "by the executable; setting a breakpoint in a shared library which\n"
326 "is not privately mapped will not work. See the HP-UX 11i v3 chatr\n"
327 "manpage for methods to privately map shared library text."));
328
329 /* Turn on the flags we care about. */
330 if (get_hpux_major_release () < 11)
331 dld_flags |= DLD_FLAGS_MAPPRIVATE;
332 if (have_endo)
333 dld_flags |= DLD_FLAGS_HOOKVALID;
334 store_unsigned_integer (buf, 4, dld_flags);
335 status = target_write_memory (anaddr, buf, 4);
336 if (status != 0)
337 error (_("Unable to write __dld_flags."));
338
339 /* Now find the address of _start and set a breakpoint there.
340 We still need this code for two reasons:
341
342 * Not all sites have /opt/langtools/lib/end.o, so it's not always
343 possible to track the dynamic linker's events.
344
345 * At this time no events are triggered for shared libraries
346 loaded at startup time (what a crock). */
347
348 msymbol = lookup_minimal_symbol ("_start", NULL, symfile_objfile);
349 if (msymbol == NULL)
350 error (_("Unable to find _start symbol in object file."));
351
352 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
353
354 /* Make the breakpoint at "_start" a shared library event breakpoint. */
355 create_solib_event_breakpoint (anaddr);
356
357 clear_symtab_users ();
358 }
359
360 static void
361 som_special_symbol_handling (void)
362 {
363 }
364
365 static void
366 som_solib_desire_dynamic_linker_symbols (void)
367 {
368 struct objfile *objfile;
369 struct unwind_table_entry *u;
370 struct minimal_symbol *dld_msymbol;
371
372 /* Do we already know the value of these symbols? If so, then
373 we've no work to do.
374
375 (If you add clauses to this test, be sure to likewise update the
376 test within the loop.)
377 */
378 if (dld_cache.is_valid)
379 return;
380
381 ALL_OBJFILES (objfile)
382 {
383 dld_msymbol = lookup_minimal_symbol ("shl_load", NULL, objfile);
384 if (dld_msymbol != NULL)
385 {
386 dld_cache.load.address = SYMBOL_VALUE (dld_msymbol);
387 dld_cache.load.unwind = find_unwind_entry (dld_cache.load.address);
388 }
389
390 dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_load",
391 objfile);
392 if (dld_msymbol != NULL)
393 {
394 if (MSYMBOL_TYPE (dld_msymbol) == mst_solib_trampoline)
395 {
396 u = find_unwind_entry (SYMBOL_VALUE (dld_msymbol));
397 if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT))
398 {
399 dld_cache.load_stub.address = SYMBOL_VALUE (dld_msymbol);
400 dld_cache.load_stub.unwind = u;
401 }
402 }
403 }
404
405 dld_msymbol = lookup_minimal_symbol ("shl_unload", NULL, objfile);
406 if (dld_msymbol != NULL)
407 {
408 dld_cache.unload.address = SYMBOL_VALUE (dld_msymbol);
409 dld_cache.unload.unwind = find_unwind_entry (dld_cache.unload.address);
410
411 /* ??rehrauer: I'm not sure exactly what this is, but it appears
412 that on some HPUX 10.x versions, there's two unwind regions to
413 cover the body of "shl_unload", the second being 4 bytes past
414 the end of the first. This is a large hack to handle that
415 case, but since I don't seem to have any legitimate way to
416 look for this thing via the symbol table...
417 */
418 if (dld_cache.unload.unwind != NULL)
419 {
420 u = find_unwind_entry (dld_cache.unload.unwind->region_end + 4);
421 if (u != NULL)
422 {
423 dld_cache.unload2.address = u->region_start;
424 dld_cache.unload2.unwind = u;
425 }
426 }
427 }
428
429 dld_msymbol = lookup_minimal_symbol_solib_trampoline ("shl_unload",
430 objfile);
431 if (dld_msymbol != NULL)
432 {
433 if (MSYMBOL_TYPE (dld_msymbol) == mst_solib_trampoline)
434 {
435 u = find_unwind_entry (SYMBOL_VALUE (dld_msymbol));
436 if ((u != NULL) && (u->stub_unwind.stub_type == EXPORT))
437 {
438 dld_cache.unload_stub.address = SYMBOL_VALUE (dld_msymbol);
439 dld_cache.unload_stub.unwind = u;
440 }
441 }
442 }
443
444 /* Did we find everything we were looking for? If so, stop. */
445 if ((dld_cache.load.address != 0)
446 && (dld_cache.load_stub.address != 0)
447 && (dld_cache.unload.address != 0)
448 && (dld_cache.unload_stub.address != 0))
449 {
450 dld_cache.is_valid = 1;
451 break;
452 }
453 }
454
455 dld_cache.hook.unwind = find_unwind_entry (dld_cache.hook.address);
456 dld_cache.hook_stub.unwind = find_unwind_entry (dld_cache.hook_stub.address);
457
458 /* We're prepared not to find some of these symbols, which is why
459 this function is a "desire" operation, and not a "require".
460 */
461 }
462
463 static int
464 som_in_dynsym_resolve_code (CORE_ADDR pc)
465 {
466 struct unwind_table_entry *u_pc;
467
468 /* Are we in the dld itself?
469
470 ??rehrauer: Large hack -- We'll assume that any address in a
471 shared text region is the dld's text. This would obviously
472 fall down if the user attached to a process, whose shlibs
473 weren't mapped to a (writeable) private region. However, in
474 that case the debugger probably isn't able to set the fundamental
475 breakpoint in the dld callback anyways, so this hack should be
476 safe.
477 */
478 if ((pc & (CORE_ADDR) 0xc0000000) == (CORE_ADDR) 0xc0000000)
479 return 1;
480
481 /* Cache the address of some symbols that are part of the dynamic
482 linker, if not already known.
483 */
484 som_solib_desire_dynamic_linker_symbols ();
485
486 /* Are we in the dld callback? Or its export stub? */
487 u_pc = find_unwind_entry (pc);
488 if (u_pc == NULL)
489 return 0;
490
491 if ((u_pc == dld_cache.hook.unwind) || (u_pc == dld_cache.hook_stub.unwind))
492 return 1;
493
494 /* Or the interface of the dld (i.e., "shl_load" or friends)? */
495 if ((u_pc == dld_cache.load.unwind)
496 || (u_pc == dld_cache.unload.unwind)
497 || (u_pc == dld_cache.unload2.unwind)
498 || (u_pc == dld_cache.load_stub.unwind)
499 || (u_pc == dld_cache.unload_stub.unwind))
500 return 1;
501
502 /* Apparently this address isn't part of the dld's text. */
503 return 0;
504 }
505
506 static void
507 som_clear_solib (void)
508 {
509 }
510
511 struct dld_list {
512 char name[4];
513 char info[4];
514 char text_addr[4];
515 char text_link_addr[4];
516 char text_end[4];
517 char data_start[4];
518 char bss_start[4];
519 char data_end[4];
520 char got_value[4];
521 char next[4];
522 char tsd_start_addr_ptr[4];
523 };
524
525 static CORE_ADDR
526 link_map_start (void)
527 {
528 struct minimal_symbol *sym;
529 CORE_ADDR addr;
530 char buf[4];
531 unsigned int dld_flags;
532
533 sym = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
534 if (!sym)
535 error (_("Unable to find __dld_flags symbol in object file."));
536 addr = SYMBOL_VALUE_ADDRESS (sym);
537 read_memory (addr, buf, 4);
538 dld_flags = extract_unsigned_integer (buf, 4);
539 if ((dld_flags & DLD_FLAGS_LISTVALID) == 0)
540 error (_("__dld_list is not valid according to __dld_flags."));
541
542 sym = lookup_minimal_symbol ("__dld_list", NULL, NULL);
543 if (!sym)
544 {
545 /* Older crt0.o files (hpux8) don't have __dld_list as a symbol,
546 but the data is still available if you know where to look. */
547 sym = lookup_minimal_symbol ("__dld_flags", NULL, NULL);
548 if (!sym)
549 {
550 error (_("Unable to find dynamic library list."));
551 return 0;
552 }
553 addr = SYMBOL_VALUE_ADDRESS (sym) - 8;
554 }
555 else
556 addr = SYMBOL_VALUE_ADDRESS (sym);
557
558 read_memory (addr, buf, 4);
559 addr = extract_unsigned_integer (buf, 4);
560 if (addr == 0)
561 return 0;
562
563 read_memory (addr, buf, 4);
564 return extract_unsigned_integer (buf, 4);
565 }
566
567 /* Does this so's name match the main binary? */
568 static int
569 match_main (const char *name)
570 {
571 return strcmp (name, symfile_objfile->name) == 0;
572 }
573
574 static struct so_list *
575 som_current_sos (void)
576 {
577 CORE_ADDR lm;
578 struct so_list *head = 0;
579 struct so_list **link_ptr = &head;
580
581 for (lm = link_map_start (); lm; )
582 {
583 char *namebuf;
584 CORE_ADDR addr;
585 struct so_list *new;
586 struct cleanup *old_chain;
587 int errcode;
588 struct dld_list dbuf;
589 char tsdbuf[4];
590
591 new = (struct so_list *) xmalloc (sizeof (struct so_list));
592 old_chain = make_cleanup (xfree, new);
593
594 memset (new, 0, sizeof (*new));
595 new->lm_info = xmalloc (sizeof (struct lm_info));
596 make_cleanup (xfree, new->lm_info);
597
598 read_memory (lm, (gdb_byte *)&dbuf, sizeof (struct dld_list));
599
600 addr = extract_unsigned_integer ((gdb_byte *)&dbuf.name,
601 sizeof (dbuf.name));
602 target_read_string (addr, &namebuf, SO_NAME_MAX_PATH_SIZE - 1, &errcode);
603 if (errcode != 0)
604 warning (_("Can't read pathname for load map: %s."),
605 safe_strerror (errcode));
606 else
607 {
608 strncpy (new->so_name, namebuf, SO_NAME_MAX_PATH_SIZE - 1);
609 new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
610 xfree (namebuf);
611 strcpy (new->so_original_name, new->so_name);
612 }
613
614 if (new->so_name[0] && !match_main (new->so_name))
615 {
616 struct lm_info *lmi = new->lm_info;
617 unsigned int tmp;
618
619 lmi->lm_addr = lm;
620
621 #define EXTRACT(_fld) \
622 extract_unsigned_integer ((gdb_byte *)&dbuf._fld, sizeof (dbuf._fld));
623
624 lmi->text_addr = EXTRACT (text_addr);
625 tmp = EXTRACT (info);
626 lmi->library_version = (tmp >> 16) & 0xffff;
627 lmi->bind_mode = (tmp >> 8) & 0xff;
628 lmi->struct_version = tmp & 0xff;
629 lmi->text_link_addr = EXTRACT (text_link_addr);
630 lmi->text_end = EXTRACT (text_end);
631 lmi->data_start = EXTRACT (data_start);
632 lmi->bss_start = EXTRACT (bss_start);
633 lmi->data_end = EXTRACT (data_end);
634 lmi->got_value = EXTRACT (got_value);
635 tmp = EXTRACT (tsd_start_addr_ptr);
636 read_memory (tmp, tsdbuf, 4);
637 lmi->tsd_start_addr = extract_unsigned_integer (tsdbuf, 4);
638
639 #ifdef SOLIB_SOM_DBG
640 printf ("\n+ library \"%s\" is described at 0x%s\n", new->so_name,
641 paddr_nz (lm));
642 printf (" 'version' is %d\n", new->lm_info->struct_version);
643 printf (" 'bind_mode' is %d\n", new->lm_info->bind_mode);
644 printf (" 'library_version' is %d\n",
645 new->lm_info->library_version);
646 printf (" 'text_addr' is 0x%s\n",
647 paddr_nz (new->lm_info->text_addr));
648 printf (" 'text_link_addr' is 0x%s\n",
649 paddr_nz (new->lm_info->text_link_addr));
650 printf (" 'text_end' is 0x%s\n",
651 paddr_nz (new->lm_info->text_end));
652 printf (" 'data_start' is 0x%s\n",
653 paddr_nz (new->lm_info->data_start));
654 printf (" 'bss_start' is 0x%s\n",
655 paddr_nz (new->lm_info->bss_start));
656 printf (" 'data_end' is 0x%s\n",
657 paddr_nz (new->lm_info->data_end));
658 printf (" 'got_value' is %s\n",
659 paddr_nz (new->lm_info->got_value));
660 printf (" 'tsd_start_addr' is 0x%s\n",
661 paddr_nz (new->lm_info->tsd_start_addr));
662 #endif
663
664 new->addr_low = lmi->text_addr;
665 new->addr_high = lmi->text_end;
666
667 /* Link the new object onto the list. */
668 new->next = NULL;
669 *link_ptr = new;
670 link_ptr = &new->next;
671 }
672 else
673 {
674 free_so (new);
675 }
676
677 lm = EXTRACT (next);
678 discard_cleanups (old_chain);
679 #undef EXTRACT
680 }
681
682 /* TODO: The original somsolib code has logic to detect and eliminate
683 duplicate entries. Do we need that? */
684
685 return head;
686 }
687
688 static int
689 som_open_symbol_file_object (void *from_ttyp)
690 {
691 CORE_ADDR lm, l_name;
692 char *filename;
693 int errcode;
694 int from_tty = *(int *)from_ttyp;
695 char buf[4];
696
697 if (symfile_objfile)
698 if (!query ("Attempt to reload symbols from process? "))
699 return 0;
700
701 /* First link map member should be the executable. */
702 if ((lm = link_map_start ()) == 0)
703 return 0; /* failed somehow... */
704
705 /* Read address of name from target memory to GDB. */
706 read_memory (lm + offsetof (struct dld_list, name), buf, 4);
707
708 /* Convert the address to host format. Assume that the address is
709 unsigned. */
710 l_name = extract_unsigned_integer (buf, 4);
711
712 if (l_name == 0)
713 return 0; /* No filename. */
714
715 /* Now fetch the filename from target memory. */
716 target_read_string (l_name, &filename, SO_NAME_MAX_PATH_SIZE - 1, &errcode);
717
718 if (errcode)
719 {
720 warning (_("failed to read exec filename from attached file: %s"),
721 safe_strerror (errcode));
722 return 0;
723 }
724
725 make_cleanup (xfree, filename);
726 /* Have a pathname: read the symbol file. */
727 symbol_file_add_main (filename, from_tty);
728
729 return 1;
730 }
731
732 static void
733 som_free_so (struct so_list *so)
734 {
735 xfree (so->lm_info);
736 }
737
738 static CORE_ADDR
739 som_solib_thread_start_addr (struct so_list *so)
740 {
741 return so->lm_info->tsd_start_addr;
742 }
743
744 /* Return the GOT value for the shared library in which ADDR belongs. If
745 ADDR isn't in any known shared library, return zero. */
746
747 static CORE_ADDR
748 som_solib_get_got_by_pc (CORE_ADDR addr)
749 {
750 struct so_list *so_list = master_so_list ();
751 CORE_ADDR got_value = 0;
752
753 while (so_list)
754 {
755 if (so_list->lm_info->text_addr <= addr
756 && so_list->lm_info->text_end > addr)
757 {
758 got_value = so_list->lm_info->got_value;
759 break;
760 }
761 so_list = so_list->next;
762 }
763 return got_value;
764 }
765
766 /* Return the address of the handle of the shared library in which ADDR belongs.
767 If ADDR isn't in any known shared library, return zero. */
768 /* this function is used in initialize_hp_cxx_exception_support in
769 hppa-hpux-tdep.c */
770
771 static CORE_ADDR
772 som_solib_get_solib_by_pc (CORE_ADDR addr)
773 {
774 struct so_list *so_list = master_so_list ();
775
776 while (so_list)
777 {
778 if (so_list->lm_info->text_addr <= addr
779 && so_list->lm_info->text_end > addr)
780 {
781 break;
782 }
783 so_list = so_list->next;
784 }
785 if (so_list)
786 return so_list->lm_info->lm_addr;
787 else
788 return 0;
789 }
790
791
792 static struct target_so_ops som_so_ops;
793
794 extern initialize_file_ftype _initialize_som_solib; /* -Wmissing-prototypes */
795
796 void
797 _initialize_som_solib (void)
798 {
799 som_so_ops.relocate_section_addresses = som_relocate_section_addresses;
800 som_so_ops.free_so = som_free_so;
801 som_so_ops.clear_solib = som_clear_solib;
802 som_so_ops.solib_create_inferior_hook = som_solib_create_inferior_hook;
803 som_so_ops.special_symbol_handling = som_special_symbol_handling;
804 som_so_ops.current_sos = som_current_sos;
805 som_so_ops.open_symbol_file_object = som_open_symbol_file_object;
806 som_so_ops.in_dynsym_resolve_code = som_in_dynsym_resolve_code;
807 }
808
809 void som_solib_select (struct gdbarch *gdbarch)
810 {
811 struct gdbarch_tdep *tdep = gdbarch_tdep (gdbarch);
812 set_solib_ops (gdbarch, &som_so_ops);
813
814 tdep->solib_thread_start_addr = som_solib_thread_start_addr;
815 tdep->solib_get_got_by_pc = som_solib_get_got_by_pc;
816 tdep->solib_get_solib_by_pc = som_solib_get_solib_by_pc;
817 }
818
819 /* The rest of these functions are not part of the solib interface; they
820 are used by somread.c or hppa-hpux-tdep.c */
821
822 int
823 som_solib_section_offsets (struct objfile *objfile,
824 struct section_offsets *offsets)
825 {
826 struct so_list *so_list = master_so_list ();
827
828 while (so_list)
829 {
830 /* Oh what a pain! We need the offsets before so_list->objfile
831 is valid. The BFDs will never match. Make a best guess. */
832 if (strstr (objfile->name, so_list->so_name))
833 {
834 asection *private_section;
835
836 /* The text offset is easy. */
837 offsets->offsets[SECT_OFF_TEXT (objfile)]
838 = (so_list->lm_info->text_addr
839 - so_list->lm_info->text_link_addr);
840 offsets->offsets[SECT_OFF_RODATA (objfile)]
841 = ANOFFSET (offsets, SECT_OFF_TEXT (objfile));
842
843 /* We should look at presumed_dp in the SOM header, but
844 that's not easily available. This should be OK though. */
845 private_section = bfd_get_section_by_name (objfile->obfd,
846 "$PRIVATE$");
847 if (!private_section)
848 {
849 warning (_("Unable to find $PRIVATE$ in shared library!"));
850 offsets->offsets[SECT_OFF_DATA (objfile)] = 0;
851 offsets->offsets[SECT_OFF_BSS (objfile)] = 0;
852 return 1;
853 }
854 offsets->offsets[SECT_OFF_DATA (objfile)]
855 = (so_list->lm_info->data_start - private_section->vma);
856 offsets->offsets[SECT_OFF_BSS (objfile)]
857 = ANOFFSET (offsets, SECT_OFF_DATA (objfile));
858 return 1;
859 }
860 so_list = so_list->next;
861 }
862 return 0;
863 }