* disasm.h (gdb_disassembly): Add GDBARCH parameter.
[deliverable/binutils-gdb.git] / gdb / solib-irix.c
1 /* Shared library support for IRIX.
2 Copyright (C) 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2004,
3 2007, 2008, 2009 Free Software Foundation, Inc.
4
5 This file was created using portions of irix5-nat.c originally
6 contributed to GDB by Ian Lance Taylor.
7
8 This file is part of GDB.
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22
23 #include "defs.h"
24
25 #include "symtab.h"
26 #include "bfd.h"
27 /* FIXME: ezannoni/2004-02-13 Verify that the include below is
28 really needed. */
29 #include "symfile.h"
30 #include "objfiles.h"
31 #include "gdbcore.h"
32 #include "target.h"
33 #include "inferior.h"
34 #include "gdbthread.h"
35
36 #include "solist.h"
37 #include "solib.h"
38 #include "solib-irix.h"
39
40
41 /* Link map info to include in an allocate so_list entry. Unlike some
42 of the other solib backends, this (Irix) backend chooses to decode
43 the link map info obtained from the target and store it as (mostly)
44 CORE_ADDRs which need no further decoding. This is more convenient
45 because there are three different link map formats to worry about.
46 We use a single routine (fetch_lm_info) to read (and decode) the target
47 specific link map data. */
48
49 struct lm_info
50 {
51 CORE_ADDR addr; /* address of obj_info or obj_list
52 struct on target (from which the
53 following information is obtained). */
54 CORE_ADDR next; /* address of next item in list. */
55 CORE_ADDR reloc_offset; /* amount to relocate by */
56 CORE_ADDR pathname_addr; /* address of pathname */
57 int pathname_len; /* length of pathname */
58 };
59
60 /* It's not desirable to use the system header files to obtain the
61 structure of the obj_list or obj_info structs. Therefore, we use a
62 platform neutral representation which has been derived from the IRIX
63 header files. */
64
65 typedef struct
66 {
67 gdb_byte b[4];
68 }
69 gdb_int32_bytes;
70 typedef struct
71 {
72 gdb_byte b[8];
73 }
74 gdb_int64_bytes;
75
76 /* The "old" obj_list struct. This is used with old (o32) binaries.
77 The ``data'' member points at a much larger and more complicated
78 struct which we will only refer to by offsets. See
79 fetch_lm_info(). */
80
81 struct irix_obj_list
82 {
83 gdb_int32_bytes data;
84 gdb_int32_bytes next;
85 gdb_int32_bytes prev;
86 };
87
88 /* The ELF32 and ELF64 versions of the above struct. The oi_magic value
89 corresponds to the ``data'' value in the "old" struct. When this value
90 is 0xffffffff, the data will be in one of the following formats. The
91 ``oi_size'' field is used to decide which one we actually have. */
92
93 struct irix_elf32_obj_info
94 {
95 gdb_int32_bytes oi_magic;
96 gdb_int32_bytes oi_size;
97 gdb_int32_bytes oi_next;
98 gdb_int32_bytes oi_prev;
99 gdb_int32_bytes oi_ehdr;
100 gdb_int32_bytes oi_orig_ehdr;
101 gdb_int32_bytes oi_pathname;
102 gdb_int32_bytes oi_pathname_len;
103 };
104
105 struct irix_elf64_obj_info
106 {
107 gdb_int32_bytes oi_magic;
108 gdb_int32_bytes oi_size;
109 gdb_int64_bytes oi_next;
110 gdb_int64_bytes oi_prev;
111 gdb_int64_bytes oi_ehdr;
112 gdb_int64_bytes oi_orig_ehdr;
113 gdb_int64_bytes oi_pathname;
114 gdb_int32_bytes oi_pathname_len;
115 gdb_int32_bytes padding;
116 };
117
118 /* Union of all of the above (plus a split out magic field). */
119
120 union irix_obj_info
121 {
122 gdb_int32_bytes magic;
123 struct irix_obj_list ol32;
124 struct irix_elf32_obj_info oi32;
125 struct irix_elf64_obj_info oi64;
126 };
127
128 /* MIPS sign extends its 32 bit addresses. We could conceivably use
129 extract_typed_address here, but to do so, we'd have to construct an
130 appropriate type. Calling extract_signed_integer seems simpler. */
131
132 static CORE_ADDR
133 extract_mips_address (void *addr, int len)
134 {
135 return extract_signed_integer (addr, len);
136 }
137
138 /* Fetch and return the link map data associated with ADDR. Note that
139 this routine automatically determines which (of three) link map
140 formats is in use by the target. */
141
142 static struct lm_info
143 fetch_lm_info (CORE_ADDR addr)
144 {
145 struct lm_info li;
146 union irix_obj_info buf;
147
148 li.addr = addr;
149
150 /* The smallest region that we'll need is for buf.ol32. We'll read
151 that first. We'll read more of the buffer later if we have to deal
152 with one of the other cases. (We don't want to incur a memory error
153 if we were to read a larger region that generates an error due to
154 being at the end of a page or the like.) */
155 read_memory (addr, (char *) &buf, sizeof (buf.ol32));
156
157 if (extract_unsigned_integer (buf.magic.b, sizeof (buf.magic)) != 0xffffffff)
158 {
159 /* Use buf.ol32... */
160 char obj_buf[432];
161 CORE_ADDR obj_addr = extract_mips_address (&buf.ol32.data,
162 sizeof (buf.ol32.data));
163 li.next = extract_mips_address (&buf.ol32.next, sizeof (buf.ol32.next));
164
165 read_memory (obj_addr, obj_buf, sizeof (obj_buf));
166
167 li.pathname_addr = extract_mips_address (&obj_buf[236], 4);
168 li.pathname_len = 0; /* unknown */
169 li.reloc_offset = extract_mips_address (&obj_buf[196], 4)
170 - extract_mips_address (&obj_buf[248], 4);
171
172 }
173 else if (extract_unsigned_integer (buf.oi32.oi_size.b,
174 sizeof (buf.oi32.oi_size))
175 == sizeof (buf.oi32))
176 {
177 /* Use buf.oi32... */
178
179 /* Read rest of buffer. */
180 read_memory (addr + sizeof (buf.ol32),
181 ((char *) &buf) + sizeof (buf.ol32),
182 sizeof (buf.oi32) - sizeof (buf.ol32));
183
184 /* Fill in fields using buffer contents. */
185 li.next = extract_mips_address (&buf.oi32.oi_next,
186 sizeof (buf.oi32.oi_next));
187 li.reloc_offset = extract_mips_address (&buf.oi32.oi_ehdr,
188 sizeof (buf.oi32.oi_ehdr))
189 - extract_mips_address (&buf.oi32.oi_orig_ehdr,
190 sizeof (buf.oi32.oi_orig_ehdr));
191 li.pathname_addr = extract_mips_address (&buf.oi32.oi_pathname,
192 sizeof (buf.oi32.oi_pathname));
193 li.pathname_len = extract_unsigned_integer (buf.oi32.oi_pathname_len.b,
194 sizeof (buf.oi32.
195 oi_pathname_len));
196 }
197 else if (extract_unsigned_integer (buf.oi64.oi_size.b,
198 sizeof (buf.oi64.oi_size))
199 == sizeof (buf.oi64))
200 {
201 /* Use buf.oi64... */
202
203 /* Read rest of buffer. */
204 read_memory (addr + sizeof (buf.ol32),
205 ((char *) &buf) + sizeof (buf.ol32),
206 sizeof (buf.oi64) - sizeof (buf.ol32));
207
208 /* Fill in fields using buffer contents. */
209 li.next = extract_mips_address (&buf.oi64.oi_next,
210 sizeof (buf.oi64.oi_next));
211 li.reloc_offset = extract_mips_address (&buf.oi64.oi_ehdr,
212 sizeof (buf.oi64.oi_ehdr))
213 - extract_mips_address (&buf.oi64.oi_orig_ehdr,
214 sizeof (buf.oi64.oi_orig_ehdr));
215 li.pathname_addr = extract_mips_address (&buf.oi64.oi_pathname,
216 sizeof (buf.oi64.oi_pathname));
217 li.pathname_len = extract_unsigned_integer (buf.oi64.oi_pathname_len.b,
218 sizeof (buf.oi64.
219 oi_pathname_len));
220 }
221 else
222 {
223 error (_("Unable to fetch shared library obj_info or obj_list info."));
224 }
225
226 return li;
227 }
228
229 /* The symbol which starts off the list of shared libraries. */
230 #define DEBUG_BASE "__rld_obj_head"
231
232 static void *base_breakpoint;
233
234 static CORE_ADDR debug_base; /* Base of dynamic linker structures */
235
236 /*
237
238 LOCAL FUNCTION
239
240 locate_base -- locate the base address of dynamic linker structs
241
242 SYNOPSIS
243
244 CORE_ADDR locate_base (void)
245
246 DESCRIPTION
247
248 For both the SunOS and SVR4 shared library implementations, if the
249 inferior executable has been linked dynamically, there is a single
250 address somewhere in the inferior's data space which is the key to
251 locating all of the dynamic linker's runtime structures. This
252 address is the value of the symbol defined by the macro DEBUG_BASE.
253 The job of this function is to find and return that address, or to
254 return 0 if there is no such address (the executable is statically
255 linked for example).
256
257 For SunOS, the job is almost trivial, since the dynamic linker and
258 all of it's structures are statically linked to the executable at
259 link time. Thus the symbol for the address we are looking for has
260 already been added to the minimal symbol table for the executable's
261 objfile at the time the symbol file's symbols were read, and all we
262 have to do is look it up there. Note that we explicitly do NOT want
263 to find the copies in the shared library.
264
265 The SVR4 version is much more complicated because the dynamic linker
266 and it's structures are located in the shared C library, which gets
267 run as the executable's "interpreter" by the kernel. We have to go
268 to a lot more work to discover the address of DEBUG_BASE. Because
269 of this complexity, we cache the value we find and return that value
270 on subsequent invocations. Note there is no copy in the executable
271 symbol tables.
272
273 Irix 5 is basically like SunOS.
274
275 Note that we can assume nothing about the process state at the time
276 we need to find this address. We may be stopped on the first instruc-
277 tion of the interpreter (C shared library), the first instruction of
278 the executable itself, or somewhere else entirely (if we attached
279 to the process for example).
280
281 */
282
283 static CORE_ADDR
284 locate_base (void)
285 {
286 struct minimal_symbol *msymbol;
287 CORE_ADDR address = 0;
288
289 msymbol = lookup_minimal_symbol (DEBUG_BASE, NULL, symfile_objfile);
290 if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
291 {
292 address = SYMBOL_VALUE_ADDRESS (msymbol);
293 }
294 return (address);
295 }
296
297 /*
298
299 LOCAL FUNCTION
300
301 disable_break -- remove the "mapping changed" breakpoint
302
303 SYNOPSIS
304
305 static int disable_break ()
306
307 DESCRIPTION
308
309 Removes the breakpoint that gets hit when the dynamic linker
310 completes a mapping change.
311
312 */
313
314 static int
315 disable_break (void)
316 {
317 int status = 1;
318
319
320 /* Note that breakpoint address and original contents are in our address
321 space, so we just need to write the original contents back. */
322
323 if (deprecated_remove_raw_breakpoint (target_gdbarch, base_breakpoint) != 0)
324 {
325 status = 0;
326 }
327
328 base_breakpoint = NULL;
329
330 /* Note that it is possible that we have stopped at a location that
331 is different from the location where we inserted our breakpoint.
332 On mips-irix, we can actually land in __dbx_init(), so we should
333 not check the PC against our breakpoint address here. See procfs.c
334 for more details. */
335
336 return (status);
337 }
338
339 /*
340
341 LOCAL FUNCTION
342
343 enable_break -- arrange for dynamic linker to hit breakpoint
344
345 SYNOPSIS
346
347 int enable_break (void)
348
349 DESCRIPTION
350
351 This functions inserts a breakpoint at the entry point of the
352 main executable, where all shared libraries are mapped in.
353 */
354
355 static int
356 enable_break (void)
357 {
358 if (symfile_objfile != NULL)
359 {
360 base_breakpoint
361 = deprecated_insert_raw_breakpoint (target_gdbarch,
362 entry_point_address ());
363
364 if (base_breakpoint != NULL)
365 return 1;
366 }
367
368 return 0;
369 }
370
371 /*
372
373 LOCAL FUNCTION
374
375 irix_solib_create_inferior_hook -- shared library startup support
376
377 SYNOPSIS
378
379 void solib_create_inferior_hook ()
380
381 DESCRIPTION
382
383 When gdb starts up the inferior, it nurses it along (through the
384 shell) until it is ready to execute it's first instruction. At this
385 point, this function gets called via expansion of the macro
386 SOLIB_CREATE_INFERIOR_HOOK.
387
388 For SunOS executables, this first instruction is typically the
389 one at "_start", or a similar text label, regardless of whether
390 the executable is statically or dynamically linked. The runtime
391 startup code takes care of dynamically linking in any shared
392 libraries, once gdb allows the inferior to continue.
393
394 For SVR4 executables, this first instruction is either the first
395 instruction in the dynamic linker (for dynamically linked
396 executables) or the instruction at "start" for statically linked
397 executables. For dynamically linked executables, the system
398 first exec's /lib/libc.so.N, which contains the dynamic linker,
399 and starts it running. The dynamic linker maps in any needed
400 shared libraries, maps in the actual user executable, and then
401 jumps to "start" in the user executable.
402
403 For both SunOS shared libraries, and SVR4 shared libraries, we
404 can arrange to cooperate with the dynamic linker to discover the
405 names of shared libraries that are dynamically linked, and the
406 base addresses to which they are linked.
407
408 This function is responsible for discovering those names and
409 addresses, and saving sufficient information about them to allow
410 their symbols to be read at a later time.
411
412 FIXME
413
414 Between enable_break() and disable_break(), this code does not
415 properly handle hitting breakpoints which the user might have
416 set in the startup code or in the dynamic linker itself. Proper
417 handling will probably have to wait until the implementation is
418 changed to use the "breakpoint handler function" method.
419
420 Also, what if child has exit()ed? Must exit loop somehow.
421 */
422
423 static void
424 irix_solib_create_inferior_hook (void)
425 {
426 struct inferior *inf;
427 struct thread_info *tp;
428
429 if (!enable_break ())
430 {
431 warning (_("shared library handler failed to enable breakpoint"));
432 return;
433 }
434
435 /* Now run the target. It will eventually hit the breakpoint, at
436 which point all of the libraries will have been mapped in and we
437 can go groveling around in the dynamic linker structures to find
438 out what we need to know about them. */
439
440 inf = current_inferior ();
441 tp = inferior_thread ();
442
443 clear_proceed_status ();
444
445 inf->stop_soon = STOP_QUIETLY;
446 tp->stop_signal = TARGET_SIGNAL_0;
447
448 do
449 {
450 target_resume (pid_to_ptid (-1), 0, tp->stop_signal);
451 wait_for_inferior (0);
452 }
453 while (tp->stop_signal != TARGET_SIGNAL_TRAP);
454
455 /* We are now either at the "mapping complete" breakpoint (or somewhere
456 else, a condition we aren't prepared to deal with anyway), so adjust
457 the PC as necessary after a breakpoint, disable the breakpoint, and
458 add any shared libraries that were mapped in. */
459
460 if (!disable_break ())
461 {
462 warning (_("shared library handler failed to disable breakpoint"));
463 }
464
465 /* solib_add will call reinit_frame_cache.
466 But we are stopped in the startup code and we might not have symbols
467 for the startup code, so heuristic_proc_start could be called
468 and will put out an annoying warning.
469 Delaying the resetting of stop_soon until after symbol loading
470 suppresses the warning. */
471 solib_add ((char *) 0, 0, (struct target_ops *) 0, auto_solib_add);
472 inf->stop_soon = NO_STOP_QUIETLY;
473 }
474
475 /* LOCAL FUNCTION
476
477 current_sos -- build a list of currently loaded shared objects
478
479 SYNOPSIS
480
481 struct so_list *current_sos ()
482
483 DESCRIPTION
484
485 Build a list of `struct so_list' objects describing the shared
486 objects currently loaded in the inferior. This list does not
487 include an entry for the main executable file.
488
489 Note that we only gather information directly available from the
490 inferior --- we don't examine any of the shared library files
491 themselves. The declaration of `struct so_list' says which fields
492 we provide values for. */
493
494 static struct so_list *
495 irix_current_sos (void)
496 {
497 CORE_ADDR lma;
498 char addr_buf[8];
499 struct so_list *head = 0;
500 struct so_list **link_ptr = &head;
501 int is_first = 1;
502 struct lm_info lm;
503
504 /* Make sure we've looked up the inferior's dynamic linker's base
505 structure. */
506 if (!debug_base)
507 {
508 debug_base = locate_base ();
509
510 /* If we can't find the dynamic linker's base structure, this
511 must not be a dynamically linked executable. Hmm. */
512 if (!debug_base)
513 return 0;
514 }
515
516 read_memory (debug_base,
517 addr_buf,
518 gdbarch_addr_bit (target_gdbarch) / TARGET_CHAR_BIT);
519 lma = extract_mips_address (addr_buf,
520 gdbarch_addr_bit (target_gdbarch)
521 / TARGET_CHAR_BIT);
522
523 while (lma)
524 {
525 lm = fetch_lm_info (lma);
526 if (!is_first)
527 {
528 int errcode;
529 char *name_buf;
530 int name_size;
531 struct so_list *new
532 = (struct so_list *) xmalloc (sizeof (struct so_list));
533 struct cleanup *old_chain = make_cleanup (xfree, new);
534
535 memset (new, 0, sizeof (*new));
536
537 new->lm_info = xmalloc (sizeof (struct lm_info));
538 make_cleanup (xfree, new->lm_info);
539
540 *new->lm_info = lm;
541
542 /* Extract this shared object's name. */
543 name_size = lm.pathname_len;
544 if (name_size == 0)
545 name_size = SO_NAME_MAX_PATH_SIZE - 1;
546
547 if (name_size >= SO_NAME_MAX_PATH_SIZE)
548 {
549 name_size = SO_NAME_MAX_PATH_SIZE - 1;
550 warning
551 ("current_sos: truncating name of %d characters to only %d characters",
552 lm.pathname_len, name_size);
553 }
554
555 target_read_string (lm.pathname_addr, &name_buf,
556 name_size, &errcode);
557 if (errcode != 0)
558 warning (_("Can't read pathname for load map: %s."),
559 safe_strerror (errcode));
560 else
561 {
562 strncpy (new->so_name, name_buf, name_size);
563 new->so_name[name_size] = '\0';
564 xfree (name_buf);
565 strcpy (new->so_original_name, new->so_name);
566 }
567
568 new->next = 0;
569 *link_ptr = new;
570 link_ptr = &new->next;
571
572 discard_cleanups (old_chain);
573 }
574 is_first = 0;
575 lma = lm.next;
576 }
577
578 return head;
579 }
580
581 /*
582
583 LOCAL FUNCTION
584
585 irix_open_symbol_file_object
586
587 SYNOPSIS
588
589 void irix_open_symbol_file_object (void *from_tty)
590
591 DESCRIPTION
592
593 If no open symbol file, attempt to locate and open the main symbol
594 file. On IRIX, this is the first link map entry. If its name is
595 here, we can open it. Useful when attaching to a process without
596 first loading its symbol file.
597
598 If FROM_TTYP dereferences to a non-zero integer, allow messages to
599 be printed. This parameter is a pointer rather than an int because
600 open_symbol_file_object() is called via catch_errors() and
601 catch_errors() requires a pointer argument. */
602
603 static int
604 irix_open_symbol_file_object (void *from_ttyp)
605 {
606 CORE_ADDR lma;
607 char addr_buf[8];
608 struct lm_info lm;
609 struct cleanup *cleanups;
610 int errcode;
611 int from_tty = *(int *) from_ttyp;
612 char *filename;
613
614 if (symfile_objfile)
615 if (!query (_("Attempt to reload symbols from process? ")))
616 return 0;
617
618 if ((debug_base = locate_base ()) == 0)
619 return 0; /* failed somehow... */
620
621 /* First link map member should be the executable. */
622 read_memory (debug_base,
623 addr_buf,
624 gdbarch_addr_bit (target_gdbarch) / TARGET_CHAR_BIT);
625 lma = extract_mips_address (addr_buf,
626 gdbarch_addr_bit (target_gdbarch)
627 / TARGET_CHAR_BIT);
628 if (lma == 0)
629 return 0; /* failed somehow... */
630
631 lm = fetch_lm_info (lma);
632
633 if (lm.pathname_addr == 0)
634 return 0; /* No filename. */
635
636 /* Now fetch the filename from target memory. */
637 target_read_string (lm.pathname_addr, &filename, SO_NAME_MAX_PATH_SIZE - 1,
638 &errcode);
639
640 if (errcode)
641 {
642 warning (_("failed to read exec filename from attached file: %s"),
643 safe_strerror (errcode));
644 return 0;
645 }
646
647 cleanups = make_cleanup (xfree, filename);
648 /* Have a pathname: read the symbol file. */
649 symbol_file_add_main (filename, from_tty);
650
651 do_cleanups (cleanups);
652
653 return 1;
654 }
655
656
657 /*
658
659 LOCAL FUNCTION
660
661 irix_special_symbol_handling -- additional shared library symbol handling
662
663 SYNOPSIS
664
665 void irix_special_symbol_handling ()
666
667 DESCRIPTION
668
669 Once the symbols from a shared object have been loaded in the usual
670 way, we are called to do any system specific symbol handling that
671 is needed.
672
673 For SunOS4, this consisted of grunging around in the dynamic
674 linkers structures to find symbol definitions for "common" symbols
675 and adding them to the minimal symbol table for the runtime common
676 objfile.
677
678 However, for IRIX, there's nothing to do.
679
680 */
681
682 static void
683 irix_special_symbol_handling (void)
684 {
685 }
686
687 /* Using the solist entry SO, relocate the addresses in SEC. */
688
689 static void
690 irix_relocate_section_addresses (struct so_list *so,
691 struct target_section *sec)
692 {
693 sec->addr += so->lm_info->reloc_offset;
694 sec->endaddr += so->lm_info->reloc_offset;
695 }
696
697 /* Free the lm_info struct. */
698
699 static void
700 irix_free_so (struct so_list *so)
701 {
702 xfree (so->lm_info);
703 }
704
705 /* Clear backend specific state. */
706
707 static void
708 irix_clear_solib (void)
709 {
710 debug_base = 0;
711 }
712
713 /* Return 1 if PC lies in the dynamic symbol resolution code of the
714 run time loader. */
715 static int
716 irix_in_dynsym_resolve_code (CORE_ADDR pc)
717 {
718 return 0;
719 }
720
721 struct target_so_ops irix_so_ops;
722
723 /* Provide a prototype to silence -Wmissing-prototypes. */
724 extern initialize_file_ftype _initialize_irix_solib;
725
726 void
727 _initialize_irix_solib (void)
728 {
729 irix_so_ops.relocate_section_addresses = irix_relocate_section_addresses;
730 irix_so_ops.free_so = irix_free_so;
731 irix_so_ops.clear_solib = irix_clear_solib;
732 irix_so_ops.solib_create_inferior_hook = irix_solib_create_inferior_hook;
733 irix_so_ops.special_symbol_handling = irix_special_symbol_handling;
734 irix_so_ops.current_sos = irix_current_sos;
735 irix_so_ops.open_symbol_file_object = irix_open_symbol_file_object;
736 irix_so_ops.in_dynsym_resolve_code = irix_in_dynsym_resolve_code;
737 }
This page took 0.05395 seconds and 4 git commands to generate.