* hppa-tdep.c (rp_saved): Handle IMPORT stubs too.
[binutils-gdb.git] / gdb / somsolib.c
1 /* Handle HP SOM shared libraries for GDB, the GNU Debugger.
2 Copyright 1993 Free Software Foundation, Inc.
3
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
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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"
36
37 /* We don't currently use this structure, but it isn't documented anywhere,
38 and we'll likely need it in som_solib_add as more shared library support
39 is added. */
40
41 struct som_solib_mapped_entry
42 {
43 /* The name of the library. */
44 char *name;
45
46 /* Version of this structure (it is expected to change again in hpux10. */
47 unsigned char struct_version;
48
49 /* Binding mode for this library. */
50 unsigned char bind_mode;
51
52 /* Version of this library. */
53 short library_version;
54
55 /* Start of text address, link-time text location, end of text address. */
56 CORE_ADDR text_addr;
57 CORE_ADDR text_link_addr;
58 CORE_ADDR text_end;
59
60 /* Start of data, start of bss and end of data. */
61 CORE_ADDR data_start;
62 CORE_ADDR bss_start;
63 CORE_ADDR data_end;
64
65 /* Value of linkage pointer (%r19). */
66 CORE_ADDR got_value;
67
68 /* Next entry. */
69 struct som_solib_mapped_entry *next;
70
71 /* There are other fields, but I don't have information as to what is
72 contained in them. */
73 };
74
75 /* Add symbols from shared libraries into the symtab list. */
76
77 void
78 som_solib_add (arg_string, from_tty, target)
79 char *arg_string;
80 int from_tty;
81 struct target_ops *target;
82 {
83 struct minimal_symbol *msymbol;
84 CORE_ADDR addr;
85 int status;
86 unsigned int dld_flags;
87 char buf[4];
88
89 /* If we're debugging a core file, or have attached to a running
90 process, then som_solib_create_inferior_hook will not have been
91 called.
92
93 We need to examine __dld_flags to determine if the shared library
94 list is valid, and to determine if the libraries have been privately
95 mapped. */
96 msymbol = lookup_minimal_symbol ("__dld_flags", (struct objfile *) NULL);
97 if (msymbol == NULL)
98 {
99 error ("Unable to find __dld_flags symbol in object file.\n");
100 return;
101 }
102
103 addr = SYMBOL_VALUE_ADDRESS (msymbol);
104 /* Read the current contents. */
105 status = target_read_memory (addr, buf, 4);
106 if (status != 0)
107 {
108 error ("Unable to read __dld_flags\n");
109 return;
110 }
111 dld_flags = extract_unsigned_integer (buf, 4);
112
113 /* __dld_list may not be valid. If it's not valid tell the user. */
114 if ((dld_flags & 4) == 0)
115 {
116 error ("__dld_list is not valid according to __dld_flags.\n");
117 return;
118 }
119
120 /* If the libraries were not mapped private, warn the user. */
121 if ((dld_flags & 1) == 0)
122 warning ("The shared libraries were not privately mapped;\nsetting a breakpoint in a shared library will not work.\n");
123
124 msymbol = lookup_minimal_symbol ("__dld_list", (struct objfile *) NULL);
125 if (!msymbol)
126 {
127 /* Older crt0.o files (hpux8) don't have __dld_list as a symbol,
128 but the data is still available if you know where to look. */
129 msymbol = lookup_minimal_symbol ("__dld_flags", (struct objfile *)NULL);
130 if (!msymbol)
131 {
132 error ("Unable to find dynamic library list.\n");
133 return;
134 }
135 addr = SYMBOL_VALUE_ADDRESS (msymbol) - 8;
136 }
137 else
138 addr = SYMBOL_VALUE_ADDRESS (msymbol);
139
140 status = target_read_memory (addr, buf, 4);
141 if (status != 0)
142 {
143 error ("Unable to find dynamic library list.\n");
144 return;
145 }
146
147 addr = extract_unsigned_integer (buf, 4);
148
149 /* If addr is zero, then we're using an old dynamic loader which
150 doesn't maintain __dld_list. We'll have to use a completely
151 different approach to get shared library information. */
152 if (addr == 0)
153 goto old_dld;
154
155 /* Using the information in __dld_list is the preferred method
156 to get at shared library information. It doesn't depend on
157 any functions in /usr/lib/end.o and has a chance of working
158 with hpux10 when it is released. */
159 status = target_read_memory (addr, buf, 4);
160 if (status != 0)
161 {
162 error ("Unable to find dynamic library list.\n");
163 return;
164 }
165
166 /* addr now holds the address of the first entry in the dynamic
167 library list. */
168 addr = extract_unsigned_integer (buf, 4);
169
170 /* Now that we have a pointer to the dynamic library list, walk
171 through it and add the symbols for each library.
172
173 Skip the first entry since it's our executable. */
174 status = target_read_memory (addr + 36, buf, 4);
175 if (status != 0)
176 {
177 error ("Error while reading dynamic library list.\n");
178 return;
179 }
180 addr = extract_unsigned_integer (buf, 4);
181
182 while (1)
183 {
184 CORE_ADDR name_addr, text_addr;
185 unsigned int name_len;
186 char *name;
187 if (addr == 0)
188 break;
189
190 /* Get a pointer to the name of this library. */
191 status = target_read_memory (addr, buf, 4);
192 if (status != 0)
193 {
194 error ("Error while reading dynamic library list.\n");
195 return;
196 }
197 name_addr = extract_unsigned_integer (buf, 4);
198 name_len = 0;
199 while (1)
200 {
201 target_read_memory (name_addr + name_len, buf, 1);
202 if (status != 0)
203 {
204 error ("Error while reading dynamic library list.\n");
205 return;
206 }
207 name_len++;
208 if (*buf == '\0')
209 break;
210 }
211 name = alloca (name_len);
212 status = target_read_memory (name_addr, name, name_len);
213 if (status != 0)
214 {
215 error ("Error while reading dynamic library list.\n");
216 return;
217 }
218 name = obsavestring (name, name_len - 1,
219 &symfile_objfile->symbol_obstack);
220
221 status = target_read_memory (addr + 8, buf, 4);
222 if (status != 0)
223 {
224 error ("Error while reading dynamic library list.\n");
225 return;
226 }
227 text_addr = extract_unsigned_integer (buf, 4);
228
229 /* OK, we've got everything we need. Tell GDB about it. */
230 symbol_file_add (name, from_tty, text_addr, 0, 0, 0);
231
232 /* Get address of the next record. */
233 status = target_read_memory (addr + 36, buf, 4);
234 if (status != 0)
235 {
236 error ("Error while reading dynamic library list.\n");
237 return;
238 }
239 addr = extract_unsigned_integer (buf, 4);
240 }
241
242 /* Getting new symbols may change our opinion about what is
243 frameless. */
244 reinit_frame_cache ();
245 return;
246
247 old_dld:
248 error ("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported.\n");
249 return;
250 }
251
252 /* This hook gets called just before the first instruction in the
253 inferior process is executed.
254
255 This is our opportunity to set magic flags in the inferior so
256 that GDB can be notified when a shared library is mapped in and
257 to tell the dynamic linker that a private copy of the library is
258 needed (so GDB can set breakpoints in the library).
259
260 __dld_flags is the location of the magic flags; as of this implementation
261 there are 3 flags of interest:
262
263 bit 0 when set indicates that private copies of the libraries are needed
264 bit 1 when set indicates that the callback hook routine is valid
265 bit 2 when set indicates that the dynamic linker should maintain the
266 __dld_list structure when loading/unloading libraries.
267
268 Note that shared libraries are not mapped in at this time, so we have
269 run the inferior until the libraries are mapped in. Typically this
270 means running until the "_start" is called. */
271
272 void
273 som_solib_create_inferior_hook()
274 {
275 struct minimal_symbol *msymbol;
276 asection *shlib_info;
277 unsigned int dld_flags, status;
278 char shadow_contents[BREAKPOINT_MAX], buf[4];
279 CORE_ADDR anaddr;
280
281 /* First see if the objfile was dynamically linked. */
282 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
283 if (!shlib_info)
284 return;
285
286 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
287 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
288 return;
289
290 /* Get the address of __dld_flags, if no such symbol exists, then we can
291 not debug the shared code. */
292 msymbol = lookup_minimal_symbol ("__dld_flags", (struct objfile *) NULL);
293 if (msymbol == NULL)
294 {
295 error ("Unable to find __dld_flags symbol in object file.\n");
296 return;
297 }
298
299 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
300 /* Read the current contents. */
301 status = target_read_memory (anaddr, buf, 4);
302 if (status != 0)
303 {
304 error ("Unable to read __dld_flags\n");
305 return;
306 }
307 dld_flags = extract_unsigned_integer (buf, 4);
308
309 /* Turn on the flags we care about. */
310 dld_flags |= 0x5;
311 store_unsigned_integer (buf, 4, dld_flags);
312 status = target_write_memory (anaddr, buf, 4);
313 if (status != 0)
314 {
315 error ("Unable to write __dld_flags\n");
316 return;
317 }
318
319 /* Now find the address of _start and set a breakpoint there. */
320 msymbol = lookup_minimal_symbol ("_start", symfile_objfile);
321 if (msymbol == NULL)
322 {
323 error ("Unable to find _start symbol in object file.\n");
324 return;
325 }
326
327 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
328 if (target_insert_breakpoint (anaddr, shadow_contents))
329 {
330 error ("Unable to set breakpoint at _start.\n");
331 return;
332 }
333
334 /* Start the process again and wait for it to hit our breakpoint. */
335 clear_proceed_status ();
336 stop_soon_quietly = 1;
337 stop_signal = TARGET_SIGNAL_0;
338 do
339 {
340 target_resume (-1, 0, stop_signal);
341 wait_for_inferior ();
342 }
343 while (stop_signal != TARGET_SIGNAL_TRAP);
344 stop_soon_quietly = 0;
345
346 /* All the libraries should be mapped in now. Remove our breakpoint and
347 read in the symbol tables from the shared libraries. */
348 if (target_remove_breakpoint (anaddr, shadow_contents))
349 {
350 error ("Unable to remove breakpoint at _start.\n");
351 return;
352 }
353
354 som_solib_add ((char *) 0, 0, (struct target_ops *) 0);
355 }