IA64: Migrate from 'regset_from_core_section' to 'iterate_over_regset_sections'
[platform/upstream/binutils.git] / gdb / solib-irix.c
1 /* Shared library support for IRIX.
2    Copyright (C) 1993-2014 Free Software Foundation, Inc.
3
4    This file was created using portions of irix5-nat.c originally
5    contributed to GDB by Ian Lance Taylor.
6
7    This file is part of GDB.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
22 #include "defs.h"
23
24 #include "symtab.h"
25 #include "bfd.h"
26 /* FIXME: ezannoni/2004-02-13 Verify that the include below is
27    really needed.  */
28 #include "symfile.h"
29 #include "objfiles.h"
30 #include "gdbcore.h"
31 #include "target.h"
32 #include "inferior.h"
33 #include "infrun.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, enum bfd_endian byte_order)
134 {
135   return extract_signed_integer (addr, len, byte_order);
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   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
146   struct lm_info li;
147   union irix_obj_info buf;
148
149   li.addr = addr;
150
151   /* The smallest region that we'll need is for buf.ol32.  We'll read
152      that first.  We'll read more of the buffer later if we have to deal
153      with one of the other cases.  (We don't want to incur a memory error
154      if we were to read a larger region that generates an error due to
155      being at the end of a page or the like.)  */
156   read_memory (addr, (gdb_byte *) &buf, sizeof (buf.ol32));
157
158   if (extract_unsigned_integer (buf.magic.b, sizeof (buf.magic), byte_order)
159       != 0xffffffff)
160     {
161       /* Use buf.ol32...  */
162       gdb_byte obj_buf[432];
163       CORE_ADDR obj_addr = extract_mips_address (&buf.ol32.data,
164                                                  sizeof (buf.ol32.data),
165                                                  byte_order);
166
167       li.next = extract_mips_address (&buf.ol32.next,
168                                       sizeof (buf.ol32.next), byte_order);
169
170       read_memory (obj_addr, obj_buf, sizeof (obj_buf));
171
172       li.pathname_addr = extract_mips_address (&obj_buf[236], 4, byte_order);
173       li.pathname_len = 0;      /* unknown */
174       li.reloc_offset = extract_mips_address (&obj_buf[196], 4, byte_order)
175         - extract_mips_address (&obj_buf[248], 4, byte_order);
176
177     }
178   else if (extract_unsigned_integer (buf.oi32.oi_size.b,
179                                      sizeof (buf.oi32.oi_size), byte_order)
180            == sizeof (buf.oi32))
181     {
182       /* Use buf.oi32...  */
183
184       /* Read rest of buffer.  */
185       read_memory (addr + sizeof (buf.ol32),
186                    ((gdb_byte *) &buf) + sizeof (buf.ol32),
187                    sizeof (buf.oi32) - sizeof (buf.ol32));
188
189       /* Fill in fields using buffer contents.  */
190       li.next = extract_mips_address (&buf.oi32.oi_next,
191                                       sizeof (buf.oi32.oi_next), byte_order);
192       li.reloc_offset = extract_mips_address (&buf.oi32.oi_ehdr,
193                                               sizeof (buf.oi32.oi_ehdr),
194                                               byte_order)
195         - extract_mips_address (&buf.oi32.oi_orig_ehdr,
196                                 sizeof (buf.oi32.oi_orig_ehdr), byte_order);
197       li.pathname_addr = extract_mips_address (&buf.oi32.oi_pathname,
198                                                sizeof (buf.oi32.oi_pathname),
199                                                byte_order);
200       li.pathname_len = extract_unsigned_integer (buf.oi32.oi_pathname_len.b,
201                                                   sizeof (buf.oi32.
202                                                           oi_pathname_len),
203                                                   byte_order);
204     }
205   else if (extract_unsigned_integer (buf.oi64.oi_size.b,
206                                      sizeof (buf.oi64.oi_size), byte_order)
207            == sizeof (buf.oi64))
208     {
209       /* Use buf.oi64...  */
210
211       /* Read rest of buffer.  */
212       read_memory (addr + sizeof (buf.ol32),
213                    ((gdb_byte *) &buf) + sizeof (buf.ol32),
214                    sizeof (buf.oi64) - sizeof (buf.ol32));
215
216       /* Fill in fields using buffer contents.  */
217       li.next = extract_mips_address (&buf.oi64.oi_next,
218                                       sizeof (buf.oi64.oi_next), byte_order);
219       li.reloc_offset = extract_mips_address (&buf.oi64.oi_ehdr,
220                                               sizeof (buf.oi64.oi_ehdr),
221                                               byte_order)
222         - extract_mips_address (&buf.oi64.oi_orig_ehdr,
223                                 sizeof (buf.oi64.oi_orig_ehdr), byte_order);
224       li.pathname_addr = extract_mips_address (&buf.oi64.oi_pathname,
225                                                sizeof (buf.oi64.oi_pathname),
226                                                byte_order);
227       li.pathname_len = extract_unsigned_integer (buf.oi64.oi_pathname_len.b,
228                                                   sizeof (buf.oi64.
229                                                           oi_pathname_len),
230                                                   byte_order);
231     }
232   else
233     {
234       error (_("Unable to fetch shared library obj_info or obj_list info."));
235     }
236
237   return li;
238 }
239
240 /* The symbol which starts off the list of shared libraries.  */
241 #define DEBUG_BASE "__rld_obj_head"
242
243 static CORE_ADDR debug_base;    /* Base of dynamic linker structures.  */
244
245 /* Locate the base address of dynamic linker structs.
246
247    For both the SunOS and SVR4 shared library implementations, if the
248    inferior executable has been linked dynamically, there is a single
249    address somewhere in the inferior's data space which is the key to
250    locating all of the dynamic linker's runtime structures.  This
251    address is the value of the symbol defined by the macro DEBUG_BASE.
252    The job of this function is to find and return that address, or to
253    return 0 if there is no such address (the executable is statically
254    linked for example).
255
256    For SunOS, the job is almost trivial, since the dynamic linker and
257    all of it's structures are statically linked to the executable at
258    link time.  Thus the symbol for the address we are looking for has
259    already been added to the minimal symbol table for the executable's
260    objfile at the time the symbol file's symbols were read, and all we
261    have to do is look it up there.  Note that we explicitly do NOT want
262    to find the copies in the shared library.
263
264    The SVR4 version is much more complicated because the dynamic linker
265    and it's structures are located in the shared C library, which gets
266    run as the executable's "interpreter" by the kernel.  We have to go
267    to a lot more work to discover the address of DEBUG_BASE.  Because
268    of this complexity, we cache the value we find and return that value
269    on subsequent invocations.  Note there is no copy in the executable
270    symbol tables.
271
272    Irix 5 is basically like SunOS.
273
274    Note that we can assume nothing about the process state at the time
275    we need to find this address.  We may be stopped on the first instruc-
276    tion of the interpreter (C shared library), the first instruction of
277    the executable itself, or somewhere else entirely (if we attached
278    to the process for example).  */
279
280 static CORE_ADDR
281 locate_base (void)
282 {
283   struct bound_minimal_symbol msymbol;
284   CORE_ADDR address = 0;
285
286   msymbol = lookup_minimal_symbol (DEBUG_BASE, NULL, symfile_objfile);
287   if ((msymbol.minsym != NULL) && (BMSYMBOL_VALUE_ADDRESS (msymbol) != 0))
288     {
289       address = BMSYMBOL_VALUE_ADDRESS (msymbol);
290     }
291   return (address);
292 }
293
294
295 /* Arrange for dynamic linker to hit breakpoint.
296
297    This functions inserts a breakpoint at the entry point of the
298    main executable, where all shared libraries are mapped in.  */
299
300 static int
301 enable_break (void)
302 {
303   if (symfile_objfile != NULL && has_stack_frames ())
304     {
305       CORE_ADDR entry_point;
306
307       if (entry_point_address_query (&entry_point))
308         {
309           create_solib_event_breakpoint (target_gdbarch (), entry_point);
310           return 1;
311         }
312     }
313
314   return 0;
315 }
316
317 /* Implement the "handle_event" target_solib_ops method.  */
318
319 static void
320 irix_solib_handle_event (void)
321 {
322   /* We are now at the "mapping complete" breakpoint, we no longer
323      need it.  Note that it is possible that we have stopped at a
324      location that is different from the location where we inserted
325      our breakpoint: On mips-irix, we can actually land in
326      __dbx_link(), so we should not check the PC against our
327      breakpoint address here.  See procfs.c for more details.  Note
328      we're being called by the bpstat handling code, and so can't
329      delete the breakpoint immediately.  Mark it for later deletion,
330      which has the same effect (it'll be removed before we next resume
331      or if we're stopping).  */
332   remove_solib_event_breakpoints_at_next_stop ();
333
334   /* The caller calls solib_add, which will add any shared libraries
335      that were mapped in.  */
336 }
337
338 /* Implement the "create_inferior_hook" target_solib_ops method.
339
340    For SunOS executables, this first instruction is typically the
341    one at "_start", or a similar text label, regardless of whether
342    the executable is statically or dynamically linked.  The runtime
343    startup code takes care of dynamically linking in any shared
344    libraries, once gdb allows the inferior to continue.
345
346    For SVR4 executables, this first instruction is either the first
347    instruction in the dynamic linker (for dynamically linked
348    executables) or the instruction at "start" for statically linked
349    executables.  For dynamically linked executables, the system
350    first exec's /lib/libc.so.N, which contains the dynamic linker,
351    and starts it running.  The dynamic linker maps in any needed
352    shared libraries, maps in the actual user executable, and then
353    jumps to "start" in the user executable.
354
355    For both SunOS shared libraries, and SVR4 shared libraries, we
356    can arrange to cooperate with the dynamic linker to discover the
357    names of shared libraries that are dynamically linked, and the
358    base addresses to which they are linked.
359
360    This function is responsible for discovering those names and
361    addresses, and saving sufficient information about them to allow
362    their symbols to be read at a later time.
363
364    FIXME
365
366    Between enable_break() and disable_break(), this code does not
367    properly handle hitting breakpoints which the user might have
368    set in the startup code or in the dynamic linker itself.  Proper
369    handling will probably have to wait until the implementation is
370    changed to use the "breakpoint handler function" method.
371
372    Also, what if child has exit()ed?  Must exit loop somehow.  */
373
374 static void
375 irix_solib_create_inferior_hook (int from_tty)
376 {
377   struct inferior *inf;
378   struct thread_info *tp;
379
380   inf = current_inferior ();
381
382   /* If we are attaching to the inferior, the shared libraries
383      have already been mapped, so nothing more to do.  */
384   if (inf->attach_flag)
385     return;
386
387   /* Likewise when debugging from a core file, the shared libraries
388      have already been mapped, so nothing more to do.  */
389   if (!target_can_run (&current_target))
390     return;
391
392   if (!enable_break ())
393     {
394       warning (_("shared library handler failed to enable breakpoint"));
395       return;
396     }
397
398   /* The target will eventually hit the breakpoint, at which point all
399      of the libraries will have been mapped in and we can go groveling
400      around in the dynamic linker structures to find out what we need
401      to know about them.  */
402 }
403
404 /* Implement the "current_sos" target_so_ops method.  */
405
406 static struct so_list *
407 irix_current_sos (void)
408 {
409   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
410   int addr_size = gdbarch_addr_bit (target_gdbarch ()) / TARGET_CHAR_BIT;
411   CORE_ADDR lma;
412   gdb_byte addr_buf[8];
413   struct so_list *head = 0;
414   struct so_list **link_ptr = &head;
415   int is_first = 1;
416   struct lm_info lm;
417
418   /* Make sure we've looked up the inferior's dynamic linker's base
419      structure.  */
420   if (!debug_base)
421     {
422       debug_base = locate_base ();
423
424       /* If we can't find the dynamic linker's base structure, this
425          must not be a dynamically linked executable.  Hmm.  */
426       if (!debug_base)
427         return 0;
428     }
429
430   read_memory (debug_base, addr_buf, addr_size);
431   lma = extract_mips_address (addr_buf, addr_size, byte_order);
432
433   while (lma)
434     {
435       lm = fetch_lm_info (lma);
436       if (!is_first)
437         {
438           int errcode;
439           char *name_buf;
440           int name_size;
441           struct so_list *new
442             = (struct so_list *) xmalloc (sizeof (struct so_list));
443           struct cleanup *old_chain = make_cleanup (xfree, new);
444
445           memset (new, 0, sizeof (*new));
446
447           new->lm_info = xmalloc (sizeof (struct lm_info));
448           make_cleanup (xfree, new->lm_info);
449
450           *new->lm_info = lm;
451
452           /* Extract this shared object's name.  */
453           name_size = lm.pathname_len;
454           if (name_size == 0)
455             name_size = SO_NAME_MAX_PATH_SIZE - 1;
456
457           if (name_size >= SO_NAME_MAX_PATH_SIZE)
458             {
459               name_size = SO_NAME_MAX_PATH_SIZE - 1;
460               warning (_("current_sos: truncating name of "
461                          "%d characters to only %d characters"),
462                        lm.pathname_len, name_size);
463             }
464
465           target_read_string (lm.pathname_addr, &name_buf,
466                               name_size, &errcode);
467           if (errcode != 0)
468             warning (_("Can't read pathname for load map: %s."),
469                        safe_strerror (errcode));
470           else
471             {
472               strncpy (new->so_name, name_buf, name_size);
473               new->so_name[name_size] = '\0';
474               xfree (name_buf);
475               strcpy (new->so_original_name, new->so_name);
476             }
477
478           new->next = 0;
479           *link_ptr = new;
480           link_ptr = &new->next;
481
482           discard_cleanups (old_chain);
483         }
484       is_first = 0;
485       lma = lm.next;
486     }
487
488   return head;
489 }
490
491 /* Implement the "open_symbol_file_object" target_so_ops method.
492
493    If no open symbol file, attempt to locate and open the main symbol
494    file.  On IRIX, this is the first link map entry.  If its name is
495    here, we can open it.  Useful when attaching to a process without
496    first loading its symbol file.  */
497
498 static int
499 irix_open_symbol_file_object (void *from_ttyp)
500 {
501   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
502   int addr_size = gdbarch_addr_bit (target_gdbarch ()) / TARGET_CHAR_BIT;
503   CORE_ADDR lma;
504   gdb_byte addr_buf[8];
505   struct lm_info lm;
506   struct cleanup *cleanups;
507   int errcode;
508   int from_tty = *(int *) from_ttyp;
509   char *filename;
510
511   if (symfile_objfile)
512     if (!query (_("Attempt to reload symbols from process? ")))
513       return 0;
514
515   if ((debug_base = locate_base ()) == 0)
516     return 0;                   /* failed somehow...  */
517
518   /* First link map member should be the executable.  */
519   read_memory (debug_base, addr_buf, addr_size);
520   lma = extract_mips_address (addr_buf, addr_size, byte_order);
521   if (lma == 0)
522     return 0;                   /* failed somehow...  */
523
524   lm = fetch_lm_info (lma);
525
526   if (lm.pathname_addr == 0)
527     return 0;                   /* No filename.  */
528
529   /* Now fetch the filename from target memory.  */
530   target_read_string (lm.pathname_addr, &filename, SO_NAME_MAX_PATH_SIZE - 1,
531                       &errcode);
532
533   if (errcode)
534     {
535       warning (_("failed to read exec filename from attached file: %s"),
536                safe_strerror (errcode));
537       return 0;
538     }
539
540   cleanups = make_cleanup (xfree, filename);
541   /* Have a pathname: read the symbol file.  */
542   symbol_file_add_main (filename, from_tty);
543
544   do_cleanups (cleanups);
545
546   return 1;
547 }
548
549 /* Implement the "special_symbol_handling" target_so_ops method.
550
551    For IRIX, there's nothing to do.  */
552
553 static void
554 irix_special_symbol_handling (void)
555 {
556 }
557
558 /* Using the solist entry SO, relocate the addresses in SEC.  */
559
560 static void
561 irix_relocate_section_addresses (struct so_list *so,
562                                  struct target_section *sec)
563 {
564   sec->addr += so->lm_info->reloc_offset;
565   sec->endaddr += so->lm_info->reloc_offset;
566 }
567
568 /* Free the lm_info struct.  */
569
570 static void
571 irix_free_so (struct so_list *so)
572 {
573   xfree (so->lm_info);
574 }
575
576 /* Clear backend specific state.  */
577
578 static void
579 irix_clear_solib (void)
580 {
581   debug_base = 0;
582 }
583
584 /* Return 1 if PC lies in the dynamic symbol resolution code of the
585    run time loader.  */
586 static int
587 irix_in_dynsym_resolve_code (CORE_ADDR pc)
588 {
589   return 0;
590 }
591
592 struct target_so_ops irix_so_ops;
593
594 /* Provide a prototype to silence -Wmissing-prototypes.  */
595 extern initialize_file_ftype _initialize_irix_solib;
596
597 void
598 _initialize_irix_solib (void)
599 {
600   irix_so_ops.relocate_section_addresses = irix_relocate_section_addresses;
601   irix_so_ops.free_so = irix_free_so;
602   irix_so_ops.clear_solib = irix_clear_solib;
603   irix_so_ops.solib_create_inferior_hook = irix_solib_create_inferior_hook;
604   irix_so_ops.special_symbol_handling = irix_special_symbol_handling;
605   irix_so_ops.current_sos = irix_current_sos;
606   irix_so_ops.open_symbol_file_object = irix_open_symbol_file_object;
607   irix_so_ops.in_dynsym_resolve_code = irix_in_dynsym_resolve_code;
608   irix_so_ops.bfd_open = solib_bfd_open;
609   irix_so_ops.handle_event = irix_solib_handle_event;
610 }