* Beginnings of SOM shared library support. Breakpoints and
[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 char buf[4];
87
88 msymbol = lookup_minimal_symbol ("__dld_list", (struct objfile *) NULL);
89 if (!msymbol)
90 {
91 /* Older crt0.o files (hpux8) don't have __dld_list as a symbol,
92 but the data is still available if you know where to look. */
93 msymbol = lookup_minimal_symbol ("__dld_flags", (struct objfile *)NULL);
94 if (!msymbol)
95 {
96 error ("Unable to find dynamic library list.\n");
97 return;
98 }
99 addr = SYMBOL_VALUE_ADDRESS (msymbol) - 8;
100 }
101 else
102 addr = SYMBOL_VALUE_ADDRESS (msymbol);
103
104 status = target_read_memory (addr, buf, 4);
105 if (status != 0)
106 {
107 error ("Unable to find dynamic library list.\n");
108 return;
109 }
110
111 addr = extract_unsigned_integer (buf, 4);
112
113 /* If addr is zero, then we're using an old dynamic loader which
114 doesn't maintain __dld_list. We'll have to use a completely
115 different approach to get shared library information. */
116 if (addr == 0)
117 goto old_dld;
118
119 /* Using the information in __dld_list is the preferred method
120 to get at shared library information. It doesn't depend on
121 any functions in /usr/lib/end.o and has a chance of working
122 with hpux10 when it is released. */
123 status = target_read_memory (addr, buf, 4);
124 if (status != 0)
125 {
126 error ("Unable to find dynamic library list.\n");
127 return;
128 }
129
130 /* addr now holds the address of the first entry in the dynamic
131 library list. */
132 addr = extract_unsigned_integer (buf, 4);
133
134 /* Now that we have a pointer to the dynamic library list, walk
135 through it and add the symbols for each library.
136
137 Skip the first entry since it's our executable. */
138 status = target_read_memory (addr + 36, buf, 4);
139 if (status != 0)
140 {
141 error ("Error while reading dynamic library list.\n");
142 return;
143 }
144 addr = extract_unsigned_integer (buf, 4);
145
146 while (1)
147 {
148 CORE_ADDR name_addr, text_addr;
149 unsigned int name_len;
150 char *name;
151 if (addr == 0)
152 break;
153
154 /* Get a pointer to the name of this library. */
155 status = target_read_memory (addr, buf, 4);
156 if (status != 0)
157 {
158 error ("Error while reading dynamic library list.\n");
159 return;
160 }
161 name_addr = extract_unsigned_integer (buf, 4);
162 name_len = 0;
163 while (1)
164 {
165 target_read_memory (name_addr + name_len, buf, 1);
166 if (status != 0)
167 {
168 error ("Error while reading dynamic library list.\n");
169 return;
170 }
171 name_len++;
172 if (*buf == '\0')
173 break;
174 }
175 name = alloca (name_len);
176 status = target_read_memory (name_addr, name, name_len);
177 if (status != 0)
178 {
179 error ("Error while reading dynamic library list.\n");
180 return;
181 }
182 name = obsavestring (name, name_len - 1,
183 &symfile_objfile->symbol_obstack);
184
185 status = target_read_memory (addr + 8, buf, 4);
186 if (status != 0)
187 {
188 error ("Error while reading dynamic library list.\n");
189 return;
190 }
191 text_addr = extract_unsigned_integer (buf, 4);
192
193 /* OK, we've got everything we need. Tell GDB about it. */
194 symbol_file_add (name, from_tty, text_addr, 0, 0, 0);
195
196 /* Get address of the next record. */
197 status = target_read_memory (addr + 36, buf, 4);
198 if (status != 0)
199 {
200 error ("Error while reading dynamic library list.\n");
201 return;
202 }
203 addr = extract_unsigned_integer (buf, 4);
204 }
205
206 /* Getting new symbols may change our opinion about what is
207 frameless. */
208 reinit_frame_cache ();
209 return;
210
211 old_dld:
212 error ("Debugging dynamic executables loaded via the hpux8 dld.sl is not supported.\n");
213 return;
214 }
215
216 /* This hook gets called just before the first instruction in the
217 inferior process is executed.
218
219 This is our opportunity to set magic flags in the inferior so
220 that GDB can be notified when a shared library is mapped in and
221 to tell the dynamic linker that a private copy of the library is
222 needed (so GDB can set breakpoints in the library).
223
224 __dld_flags is the location of the magic flags; as of this implementation
225 there are 3 flags of interest:
226
227 bit 0 when set indicates that private copies of the libraries are needed
228 bit 1 when set indicates that the callback hook routine is valid
229 bit 2 when set indicates that the dynamic linker should maintain the
230 __dld_list structure when loading/unloading libraries.
231
232 Note that shared libraries are not mapped in at this time, so we have
233 run the inferior until the libraries are mapped in. Typically this
234 means running until the "_start" is called. */
235
236 void
237 som_solib_create_inferior_hook()
238 {
239 struct minimal_symbol *msymbol;
240 asection *shlib_info;
241 unsigned int dld_flags, status;
242 char shadow_contents[BREAKPOINT_MAX], buf[4];
243 CORE_ADDR anaddr;
244
245 /* First see if the objfile was dynamically linked. */
246 shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
247 if (!shlib_info)
248 return;
249
250 /* It's got a $SHLIB_INFO$ section, make sure it's not empty. */
251 if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
252 return;
253
254 /* Get the address of __dld_flags, if no such symbol exists, then we can
255 not debug the shared code. */
256 msymbol = lookup_minimal_symbol ("__dld_flags", (struct objfile *) NULL);
257 if (msymbol == NULL)
258 {
259 error ("Unable to find __dld_flags symbol in object file.\n");
260 return;
261 }
262
263 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
264 /* Read the current contents. */
265 status = target_read_memory (anaddr, buf, 4);
266 if (status != 0)
267 {
268 error ("Unable to read __dld_flags\n");
269 return;
270 }
271 dld_flags = extract_unsigned_integer (buf, 4);
272
273 /* Turn on the flags we care about. */
274 dld_flags |= 0x5;
275 store_unsigned_integer (buf, 4, dld_flags);
276 status = target_write_memory (anaddr, buf, 4);
277 if (status != 0)
278 {
279 error ("Unable to write __dld_flags\n");
280 return;
281 }
282
283 /* Now find the address of _start and set a breakpoint there. */
284 msymbol = lookup_minimal_symbol ("_start", symfile_objfile);
285 if (msymbol == NULL)
286 {
287 error ("Unable to find _start symbol in object file.\n");
288 return;
289 }
290
291 anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
292 if (target_insert_breakpoint (anaddr, shadow_contents))
293 {
294 error ("Unable to set breakpoint at _start.\n");
295 return;
296 }
297
298 /* Start the process again and wait for it to hit our breakpoint. */
299 clear_proceed_status ();
300 stop_soon_quietly = 1;
301 stop_signal = TARGET_SIGNAL_0;
302 do
303 {
304 target_resume (-1, 0, stop_signal);
305 wait_for_inferior ();
306 }
307 while (stop_signal != TARGET_SIGNAL_TRAP);
308 stop_soon_quietly = 0;
309
310 /* All the libraries should be mapped in now. Remove our breakpoint and
311 read in the symbol tables from the shared libraries. */
312 if (target_remove_breakpoint (anaddr, shadow_contents))
313 {
314 error ("Unable to remove breakpoint at _start.\n");
315 return;
316 }
317
318 som_solib_add ((char *) 0, 0, (struct target_ops *) 0);
319 }