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