xref: /openbsd/gnu/usr.bin/binutils/gdb/solib-svr4.c (revision 5af055cd)
1 /* Handle SVR4 shared libraries for GDB, the GNU Debugger.
2 
3    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999,
4    2000, 2001, 2003, 2004
5    Free Software Foundation, Inc.
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 2 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, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23 
24 #include "defs.h"
25 
26 #include "elf/external.h"
27 #include "elf/common.h"
28 #include "elf/mips.h"
29 
30 #include "auxv.h"
31 #include "symtab.h"
32 #include "bfd.h"
33 #include "symfile.h"
34 #include "objfiles.h"
35 #include "gdbcore.h"
36 #include "target.h"
37 #include "inferior.h"
38 #include "command.h"
39 
40 #include "solist.h"
41 #include "solib-svr4.h"
42 
43 #include "bfd-target.h"
44 #include "exec.h"
45 
46 #ifndef SVR4_FETCH_LINK_MAP_OFFSETS
47 #define SVR4_FETCH_LINK_MAP_OFFSETS() svr4_fetch_link_map_offsets ()
48 #endif
49 
50 static struct link_map_offsets *svr4_fetch_link_map_offsets (void);
51 static struct link_map_offsets *legacy_fetch_link_map_offsets (void);
52 static int svr4_have_link_map_offsets (void);
53 
54 /* fetch_link_map_offsets_gdbarch_data is a handle used to obtain the
55    architecture specific link map offsets fetching function.  */
56 
57 static struct gdbarch_data *fetch_link_map_offsets_gdbarch_data;
58 
59 /* legacy_svr4_fetch_link_map_offsets_hook is a pointer to a function
60    which is used to fetch link map offsets.  It will only be set
61    by solib-legacy.c, if at all. */
62 
63 struct link_map_offsets *(*legacy_svr4_fetch_link_map_offsets_hook)(void) = 0;
64 
65 /* Link map info to include in an allocated so_list entry */
66 
67 struct lm_info
68   {
69     /* Pointer to copy of link map from inferior.  The type is char *
70        rather than void *, so that we may use byte offsets to find the
71        various fields without the need for a cast.  */
72     char *lm;
73   };
74 
75 /* On SVR4 systems, a list of symbols in the dynamic linker where
76    GDB can try to place a breakpoint to monitor shared library
77    events.
78 
79    If none of these symbols are found, or other errors occur, then
80    SVR4 systems will fall back to using a symbol as the "startup
81    mapping complete" breakpoint address.  */
82 
83 static char *solib_break_names[] =
84 {
85   "r_debug_state",
86   "_r_debug_state",
87   "_dl_debug_state",
88   "rtld_db_dlactivity",
89   "_rtld_debug_state",
90 
91   /* On the 64-bit PowerPC, the linker symbol with the same name as
92      the C function points to a function descriptor, not to the entry
93      point.  The linker symbol whose name is the C function name
94      prefixed with a '.' points to the function's entry point.  So
95      when we look through this table, we ignore symbols that point
96      into the data section (thus skipping the descriptor's symbol),
97      and eventually try this one, giving us the real entry point
98      address.  */
99   "._dl_debug_state",
100 
101   NULL
102 };
103 
104 #define BKPT_AT_SYMBOL 1
105 
106 #if defined (BKPT_AT_SYMBOL)
107 static char *bkpt_names[] =
108 {
109 #ifdef SOLIB_BKPT_NAME
110   SOLIB_BKPT_NAME,		/* Prefer configured name if it exists. */
111 #endif
112   "_start",
113   "__start",
114   "main",
115   NULL
116 };
117 #endif
118 
119 static char *main_name_list[] =
120 {
121   "main_$main",
122   NULL
123 };
124 
125 /* Macro to extract an address from a solib structure.  When GDB is
126    configured for some 32-bit targets (e.g. Solaris 2.7 sparc), BFD is
127    configured to handle 64-bit targets, so CORE_ADDR is 64 bits.  We
128    have to extract only the significant bits of addresses to get the
129    right address when accessing the core file BFD.
130 
131    Assume that the address is unsigned.  */
132 
133 #define SOLIB_EXTRACT_ADDRESS(MEMBER) \
134 	extract_unsigned_integer (&(MEMBER), sizeof (MEMBER))
135 
136 /* local data declarations */
137 
138 /* link map access functions */
139 
140 static CORE_ADDR
141 LM_ADDR (struct so_list *so)
142 {
143   struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
144 
145   return (CORE_ADDR) extract_signed_integer (so->lm_info->lm + lmo->l_addr_offset,
146 					     lmo->l_addr_size);
147 }
148 
149 static CORE_ADDR
150 LM_NEXT (struct so_list *so)
151 {
152   struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
153 
154   /* Assume that the address is unsigned.  */
155   return extract_unsigned_integer (so->lm_info->lm + lmo->l_next_offset,
156 				   lmo->l_next_size);
157 }
158 
159 static CORE_ADDR
160 LM_NAME (struct so_list *so)
161 {
162   struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
163 
164   /* Assume that the address is unsigned.  */
165   return extract_unsigned_integer (so->lm_info->lm + lmo->l_name_offset,
166 				   lmo->l_name_size);
167 }
168 
169 static int
170 IGNORE_FIRST_LINK_MAP_ENTRY (struct so_list *so)
171 {
172   struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
173 
174   /* Assume that the address is unsigned.  */
175   return extract_unsigned_integer (so->lm_info->lm + lmo->l_prev_offset,
176 				   lmo->l_prev_size) == 0;
177 }
178 
179 static CORE_ADDR debug_base;	/* Base of dynamic linker structures */
180 static CORE_ADDR breakpoint_addr;	/* Address where end bkpt is set */
181 
182 /* Local function prototypes */
183 
184 #if 0
185 static int match_main (char *);
186 #endif
187 
188 static CORE_ADDR bfd_lookup_symbol (bfd *, char *, flagword);
189 
190 /*
191 
192    LOCAL FUNCTION
193 
194    bfd_lookup_symbol -- lookup the value for a specific symbol
195 
196    SYNOPSIS
197 
198    CORE_ADDR bfd_lookup_symbol (bfd *abfd, char *symname, flagword sect_flags)
199 
200    DESCRIPTION
201 
202    An expensive way to lookup the value of a single symbol for
203    bfd's that are only temporary anyway.  This is used by the
204    shared library support to find the address of the debugger
205    interface structures in the shared library.
206 
207    If SECT_FLAGS is non-zero, only match symbols in sections whose
208    flags include all those in SECT_FLAGS.
209 
210    Note that 0 is specifically allowed as an error return (no
211    such symbol).
212  */
213 
214 static CORE_ADDR
215 bfd_lookup_symbol (bfd *abfd, char *symname, flagword sect_flags)
216 {
217   long storage_needed;
218   asymbol *sym;
219   asymbol **symbol_table;
220   unsigned int number_of_symbols;
221   unsigned int i;
222   struct cleanup *back_to;
223   CORE_ADDR symaddr = 0;
224 
225   storage_needed = bfd_get_symtab_upper_bound (abfd);
226 
227   if (storage_needed > 0)
228     {
229       symbol_table = (asymbol **) xmalloc (storage_needed);
230       back_to = make_cleanup (xfree, symbol_table);
231       number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
232 
233       for (i = 0; i < number_of_symbols; i++)
234 	{
235 	  sym = *symbol_table++;
236 	  if (strcmp (sym->name, symname) == 0
237               && (sym->section->flags & sect_flags) == sect_flags)
238 	    {
239 	      /* Bfd symbols are section relative. */
240 	      symaddr = sym->value + sym->section->vma;
241 	      break;
242 	    }
243 	}
244       do_cleanups (back_to);
245     }
246 
247   if (symaddr)
248     return symaddr;
249 
250   /* On FreeBSD, the dynamic linker is stripped by default.  So we'll
251      have to check the dynamic string table too.  */
252 
253   storage_needed = bfd_get_dynamic_symtab_upper_bound (abfd);
254 
255   if (storage_needed > 0)
256     {
257       symbol_table = (asymbol **) xmalloc (storage_needed);
258       back_to = make_cleanup (xfree, symbol_table);
259       number_of_symbols = bfd_canonicalize_dynamic_symtab (abfd, symbol_table);
260 
261       for (i = 0; i < number_of_symbols; i++)
262 	{
263 	  sym = *symbol_table++;
264 
265 	  if (strcmp (sym->name, symname) == 0
266               && (sym->section->flags & sect_flags) == sect_flags)
267 	    {
268 	      /* Bfd symbols are section relative. */
269 	      symaddr = sym->value + sym->section->vma;
270 	      break;
271 	    }
272 	}
273       do_cleanups (back_to);
274     }
275 
276   return symaddr;
277 }
278 
279 /*
280 
281    LOCAL FUNCTION
282 
283    elf_locate_base -- locate the base address of dynamic linker structs
284    for SVR4 elf targets.
285 
286    SYNOPSIS
287 
288    CORE_ADDR elf_locate_base (void)
289 
290    DESCRIPTION
291 
292    For SVR4 elf targets the address of the dynamic linker's runtime
293    structure is contained within the dynamic info section in the
294    executable file.  The dynamic section is also mapped into the
295    inferior address space.  Because the runtime loader fills in the
296    real address before starting the inferior, we have to read in the
297    dynamic info section from the inferior address space.
298    If there are any errors while trying to find the address, we
299    silently return 0, otherwise the found address is returned.
300 
301  */
302 
303 static CORE_ADDR
304 elf_locate_base (void)
305 {
306   struct bfd_section *dyninfo_sect;
307   int dyninfo_sect_size;
308   CORE_ADDR dyninfo_addr, relocated_dyninfo_addr, entry_addr;
309   char *buf;
310   char *bufend;
311   int arch_size;
312 
313   /* Find the address of the entry point of the program from the
314      auxv vector.  */
315   if (target_auxv_search (&current_target, AT_ENTRY, &entry_addr) != 1)
316     {
317       /* No auxv info, maybe an older kernel. Fake our way through.  */
318       entry_addr = bfd_get_start_address (exec_bfd);
319     }
320 
321   /* Find the start address of the .dynamic section.  */
322   dyninfo_sect = bfd_get_section_by_name (exec_bfd, ".dynamic");
323   if (dyninfo_sect == NULL)
324     return 0;
325   dyninfo_addr = bfd_section_vma (exec_bfd, dyninfo_sect);
326 
327   relocated_dyninfo_addr = dyninfo_addr
328     + entry_addr - bfd_get_start_address(exec_bfd);
329 
330   /* Read in .dynamic section, silently ignore errors.  */
331   dyninfo_sect_size = bfd_section_size (exec_bfd, dyninfo_sect);
332   buf = alloca (dyninfo_sect_size);
333   if (target_read_memory (relocated_dyninfo_addr, buf, dyninfo_sect_size))
334     return 0;
335 
336   /* Find the DT_DEBUG entry in the the .dynamic section.
337      For mips elf we look for DT_MIPS_RLD_MAP, mips elf apparently has
338      no DT_DEBUG entries.  */
339 
340   arch_size = bfd_get_arch_size (exec_bfd);
341   if (arch_size == -1)	/* failure */
342     return 0;
343 
344   if (arch_size == 32)
345     { /* 32-bit elf */
346       for (bufend = buf + dyninfo_sect_size;
347 	   buf < bufend;
348 	   buf += sizeof (Elf32_External_Dyn))
349 	{
350 	  Elf32_External_Dyn *x_dynp = (Elf32_External_Dyn *) buf;
351 	  long dyn_tag;
352 	  CORE_ADDR dyn_ptr;
353 
354 	  dyn_tag = bfd_h_get_32 (exec_bfd, (bfd_byte *) x_dynp->d_tag);
355 	  if (dyn_tag == DT_NULL)
356 	    break;
357 	  else if (dyn_tag == DT_DEBUG)
358 	    {
359 	      dyn_ptr = bfd_h_get_32 (exec_bfd,
360 				      (bfd_byte *) x_dynp->d_un.d_ptr);
361 	      return dyn_ptr;
362 	    }
363 	  else if (dyn_tag == DT_MIPS_RLD_MAP)
364 	    {
365 	      char *pbuf;
366 	      int pbuf_size = TARGET_PTR_BIT / HOST_CHAR_BIT;
367 
368 	      pbuf = alloca (pbuf_size);
369 	      /* DT_MIPS_RLD_MAP contains a pointer to the address
370 		 of the dynamic link structure.  */
371 	      dyn_ptr = bfd_h_get_32 (exec_bfd,
372 				      (bfd_byte *) x_dynp->d_un.d_ptr);
373 	      if (target_read_memory (dyn_ptr, pbuf, pbuf_size))
374 		return 0;
375 	      return extract_unsigned_integer (pbuf, pbuf_size);
376 	    }
377 	}
378     }
379   else /* 64-bit elf */
380     {
381       for (bufend = buf + dyninfo_sect_size;
382 	   buf < bufend;
383 	   buf += sizeof (Elf64_External_Dyn))
384 	{
385 	  Elf64_External_Dyn *x_dynp = (Elf64_External_Dyn *) buf;
386 	  long dyn_tag;
387 	  CORE_ADDR dyn_ptr;
388 
389 	  dyn_tag = bfd_h_get_64 (exec_bfd, (bfd_byte *) x_dynp->d_tag);
390 	  if (dyn_tag == DT_NULL)
391 	    break;
392 	  else if (dyn_tag == DT_DEBUG)
393 	    {
394 	      dyn_ptr = bfd_h_get_64 (exec_bfd,
395 				      (bfd_byte *) x_dynp->d_un.d_ptr);
396 	      return dyn_ptr;
397 	    }
398 	  else if (dyn_tag == DT_MIPS_RLD_MAP)
399 	    {
400 	      char *pbuf;
401 	      int pbuf_size = TARGET_PTR_BIT / HOST_CHAR_BIT;
402 
403 	      pbuf = alloca (pbuf_size);
404 	      /* DT_MIPS_RLD_MAP contains a pointer to the address
405 		 of the dynamic link structure.  */
406 	      dyn_ptr = bfd_h_get_64 (exec_bfd,
407 				      (bfd_byte *) x_dynp->d_un.d_ptr);
408 	      if (target_read_memory (dyn_ptr, pbuf, pbuf_size))
409 		return 0;
410 	      return extract_unsigned_integer (pbuf, pbuf_size);
411 	    }
412 	}
413     }
414 
415   /* DT_DEBUG entry not found.  */
416   return 0;
417 }
418 
419 /*
420 
421    LOCAL FUNCTION
422 
423    locate_base -- locate the base address of dynamic linker structs
424 
425    SYNOPSIS
426 
427    CORE_ADDR locate_base (void)
428 
429    DESCRIPTION
430 
431    For both the SunOS and SVR4 shared library implementations, if the
432    inferior executable has been linked dynamically, there is a single
433    address somewhere in the inferior's data space which is the key to
434    locating all of the dynamic linker's runtime structures.  This
435    address is the value of the debug base symbol.  The job of this
436    function is to find and return that address, or to return 0 if there
437    is no such address (the executable is statically linked for example).
438 
439    For SunOS, the job is almost trivial, since the dynamic linker and
440    all of it's structures are statically linked to the executable at
441    link time.  Thus the symbol for the address we are looking for has
442    already been added to the minimal symbol table for the executable's
443    objfile at the time the symbol file's symbols were read, and all we
444    have to do is look it up there.  Note that we explicitly do NOT want
445    to find the copies in the shared library.
446 
447    The SVR4 version is a bit more complicated because the address
448    is contained somewhere in the dynamic info section.  We have to go
449    to a lot more work to discover the address of the debug base symbol.
450    Because of this complexity, we cache the value we find and return that
451    value on subsequent invocations.  Note there is no copy in the
452    executable symbol tables.
453 
454  */
455 
456 static CORE_ADDR
457 locate_base (void)
458 {
459   /* Check to see if we have a currently valid address, and if so, avoid
460      doing all this work again and just return the cached address.  If
461      we have no cached address, try to locate it in the dynamic info
462      section for ELF executables.  There's no point in doing any of this
463      though if we don't have some link map offsets to work with.  */
464 
465   if (debug_base == 0 && svr4_have_link_map_offsets ())
466     {
467       if (exec_bfd != NULL
468 	  && bfd_get_flavour (exec_bfd) == bfd_target_elf_flavour)
469 	debug_base = elf_locate_base ();
470     }
471   return (debug_base);
472 }
473 
474 /*
475 
476    LOCAL FUNCTION
477 
478    first_link_map_member -- locate first member in dynamic linker's map
479 
480    SYNOPSIS
481 
482    static CORE_ADDR first_link_map_member (void)
483 
484    DESCRIPTION
485 
486    Find the first element in the inferior's dynamic link map, and
487    return its address in the inferior.  This function doesn't copy the
488    link map entry itself into our address space; current_sos actually
489    does the reading.  */
490 
491 static CORE_ADDR
492 first_link_map_member (void)
493 {
494   CORE_ADDR lm = 0;
495   struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
496   char *r_map_buf = xmalloc (lmo->r_map_size);
497   struct cleanup *cleanups = make_cleanup (xfree, r_map_buf);
498 
499   read_memory (debug_base + lmo->r_map_offset, r_map_buf, lmo->r_map_size);
500 
501   /* Assume that the address is unsigned.  */
502   lm = extract_unsigned_integer (r_map_buf, lmo->r_map_size);
503 
504   /* FIXME:  Perhaps we should validate the info somehow, perhaps by
505      checking r_version for a known version number, or r_state for
506      RT_CONSISTENT. */
507 
508   do_cleanups (cleanups);
509 
510   return (lm);
511 }
512 
513 /*
514 
515   LOCAL FUNCTION
516 
517   open_symbol_file_object
518 
519   SYNOPSIS
520 
521   void open_symbol_file_object (void *from_tty)
522 
523   DESCRIPTION
524 
525   If no open symbol file, attempt to locate and open the main symbol
526   file.  On SVR4 systems, this is the first link map entry.  If its
527   name is here, we can open it.  Useful when attaching to a process
528   without first loading its symbol file.
529 
530   If FROM_TTYP dereferences to a non-zero integer, allow messages to
531   be printed.  This parameter is a pointer rather than an int because
532   open_symbol_file_object() is called via catch_errors() and
533   catch_errors() requires a pointer argument. */
534 
535 static int
536 open_symbol_file_object (void *from_ttyp)
537 {
538   CORE_ADDR lm, l_name;
539   char *filename;
540   int errcode;
541   int from_tty = *(int *)from_ttyp;
542   struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
543   char *l_name_buf = xmalloc (lmo->l_name_size);
544   struct cleanup *cleanups = make_cleanup (xfree, l_name_buf);
545 
546   if (symfile_objfile)
547     if (!query ("Attempt to reload symbols from process? "))
548       return 0;
549 
550   if ((debug_base = locate_base ()) == 0)
551     return 0;	/* failed somehow... */
552 
553   /* First link map member should be the executable.  */
554   if ((lm = first_link_map_member ()) == 0)
555     return 0;	/* failed somehow... */
556 
557   /* Read address of name from target memory to GDB.  */
558   read_memory (lm + lmo->l_name_offset, l_name_buf, lmo->l_name_size);
559 
560   /* Convert the address to host format.  Assume that the address is
561      unsigned.  */
562   l_name = extract_unsigned_integer (l_name_buf, lmo->l_name_size);
563 
564   /* Free l_name_buf.  */
565   do_cleanups (cleanups);
566 
567   if (l_name == 0)
568     return 0;		/* No filename.  */
569 
570   /* Now fetch the filename from target memory.  */
571   target_read_string (l_name, &filename, SO_NAME_MAX_PATH_SIZE - 1, &errcode);
572 
573   if (errcode)
574     {
575       warning ("failed to read exec filename from attached file: %s",
576 	       safe_strerror (errcode));
577       return 0;
578     }
579 
580   make_cleanup (xfree, filename);
581   /* Have a pathname: read the symbol file.  */
582   symbol_file_add_main (filename, from_tty);
583 
584   return 1;
585 }
586 
587 /* LOCAL FUNCTION
588 
589    current_sos -- build a list of currently loaded shared objects
590 
591    SYNOPSIS
592 
593    struct so_list *current_sos ()
594 
595    DESCRIPTION
596 
597    Build a list of `struct so_list' objects describing the shared
598    objects currently loaded in the inferior.  This list does not
599    include an entry for the main executable file.
600 
601    Note that we only gather information directly available from the
602    inferior --- we don't examine any of the shared library files
603    themselves.  The declaration of `struct so_list' says which fields
604    we provide values for.  */
605 
606 static struct so_list *
607 svr4_current_sos (void)
608 {
609   CORE_ADDR lm;
610   struct so_list *head = 0;
611   struct so_list **link_ptr = &head;
612 
613   /* Make sure we've looked up the inferior's dynamic linker's base
614      structure.  */
615   if (! debug_base)
616     {
617       debug_base = locate_base ();
618 
619       /* If we can't find the dynamic linker's base structure, this
620 	 must not be a dynamically linked executable.  Hmm.  */
621       if (! debug_base)
622 	return 0;
623     }
624 
625   /* Walk the inferior's link map list, and build our list of
626      `struct so_list' nodes.  */
627   lm = first_link_map_member ();
628   while (lm)
629     {
630       struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
631       struct so_list *new
632 	= (struct so_list *) xmalloc (sizeof (struct so_list));
633       struct cleanup *old_chain = make_cleanup (xfree, new);
634 
635       memset (new, 0, sizeof (*new));
636 
637       new->lm_info = xmalloc (sizeof (struct lm_info));
638       make_cleanup (xfree, new->lm_info);
639 
640       new->lm_info->lm = xmalloc (lmo->link_map_size);
641       make_cleanup (xfree, new->lm_info->lm);
642       memset (new->lm_info->lm, 0, lmo->link_map_size);
643 
644       read_memory (lm, new->lm_info->lm, lmo->link_map_size);
645 
646       lm = LM_NEXT (new);
647 
648       /* For SVR4 versions, the first entry in the link map is for the
649          inferior executable, so we must ignore it.  For some versions of
650          SVR4, it has no name.  For others (Solaris 2.3 for example), it
651          does have a name, so we can no longer use a missing name to
652          decide when to ignore it. */
653       if (IGNORE_FIRST_LINK_MAP_ENTRY (new))
654 	{
655           /* It is the first link map entry, i.e. it is the main executable.  */
656 
657 	  if (bfd_get_start_address (exec_bfd) == entry_point_address ())
658 	    {
659               /* Non-pie case, main executable has not been relocated.  */
660 	      free_so (new);
661 	    }
662 	  else
663 	    {
664               /* Pie case, main executable has been relocated.  */
665 	      struct so_list *gdb_solib;
666 
667 	      strncpy (new->so_name, exec_bfd->filename,
668 		       SO_NAME_MAX_PATH_SIZE - 1);
669 	      new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
670 	      strcpy (new->so_original_name, new->so_name);
671 	      new->main_relocated = 0;
672 
673 	      for (gdb_solib = master_so_list ();
674                    gdb_solib;
675                    gdb_solib = gdb_solib->next)
676 		{
677 		  if (strcmp (gdb_solib->so_name, new->so_name) == 0)
678 		    if (gdb_solib->main_relocated)
679 		      break;
680 		}
681 
682 	      if ((gdb_solib && !gdb_solib->main_relocated) || (!gdb_solib))
683 		{
684 		  add_to_target_sections (0 /*from_tty*/, &current_target, new);
685 		  new->main = 1;
686 		}
687 
688 	      /* We need this in the list of shared libs we return because
689 		 solib_add_stub will loop through it and add the symbol file.  */
690 	      new->next = 0;
691 	      *link_ptr = new;
692 	      link_ptr = &new->next;
693 	    }
694 	} /* End of IGNORE_FIRST_LINK_MAP_ENTRY  */
695       else
696 	{
697 	  int errcode;
698 	  char *buffer;
699 
700 	  /* Extract this shared object's name.  */
701 	  target_read_string (LM_NAME (new), &buffer,
702 			      SO_NAME_MAX_PATH_SIZE - 1, &errcode);
703 	  if (errcode != 0)
704 	    {
705 	      warning ("current_sos: Can't read pathname for load map: %s\n",
706 		       safe_strerror (errcode));
707 	    }
708 	  else
709 	    {
710 	      strncpy (new->so_name, buffer, SO_NAME_MAX_PATH_SIZE - 1);
711 	      new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
712 	      xfree (buffer);
713 	      strcpy (new->so_original_name, new->so_name);
714 	    }
715 
716 	  new->next = 0;
717 	  *link_ptr = new;
718 	  link_ptr = &new->next;
719 
720 	}
721 
722       discard_cleanups (old_chain);
723     }
724 
725   return head;
726 }
727 
728 /* Get the address of the link_map for a given OBJFILE.  Loop through
729    the link maps, and return the address of the one corresponding to
730    the given objfile.  Note that this function takes into account that
731    objfile can be the main executable, not just a shared library.  The
732    main executable has always an empty name field in the linkmap.  */
733 
734 CORE_ADDR
735 svr4_fetch_objfile_link_map (struct objfile *objfile)
736 {
737   CORE_ADDR lm;
738 
739   if ((debug_base = locate_base ()) == 0)
740     return 0;   /* failed somehow... */
741 
742   /* Position ourselves on the first link map.  */
743   lm = first_link_map_member ();
744   while (lm)
745     {
746       /* Get info on the layout of the r_debug and link_map structures. */
747       struct link_map_offsets *lmo = SVR4_FETCH_LINK_MAP_OFFSETS ();
748       int errcode;
749       char *buffer;
750       struct lm_info objfile_lm_info;
751       struct cleanup *old_chain;
752       CORE_ADDR name_address;
753       char *l_name_buf = xmalloc (lmo->l_name_size);
754       old_chain = make_cleanup (xfree, l_name_buf);
755 
756       /* Set up the buffer to contain the portion of the link_map
757          structure that gdb cares about.  Note that this is not the
758          whole link_map structure.  */
759       objfile_lm_info.lm = xmalloc (lmo->link_map_size);
760       make_cleanup (xfree, objfile_lm_info.lm);
761       memset (objfile_lm_info.lm, 0, lmo->link_map_size);
762 
763       /* Read the link map into our internal structure.  */
764       read_memory (lm, objfile_lm_info.lm, lmo->link_map_size);
765 
766       /* Read address of name from target memory to GDB.  */
767       read_memory (lm + lmo->l_name_offset, l_name_buf, lmo->l_name_size);
768 
769       /* Extract this object's name.  Assume that the address is
770          unsigned.  */
771       name_address = extract_unsigned_integer (l_name_buf, lmo->l_name_size);
772       target_read_string (name_address, &buffer,
773       			  SO_NAME_MAX_PATH_SIZE - 1, &errcode);
774       make_cleanup (xfree, buffer);
775       if (errcode != 0)
776     	{
777 	  warning ("svr4_fetch_objfile_link_map: Can't read pathname for load map: %s\n",
778   		   safe_strerror (errcode));
779   	}
780       else
781   	{
782 	  /* Is this the linkmap for the file we want?  */
783 	  /* If the file is not a shared library and has no name,
784 	     we are sure it is the main executable, so we return that.  */
785 	  if ((buffer && strcmp (buffer, objfile->name) == 0)
786               || (!(objfile->flags & OBJF_SHARED) && (strcmp (buffer, "") == 0)))
787   	    {
788     	      do_cleanups (old_chain);
789     	      return lm;
790       	    }
791   	}
792       /* Not the file we wanted, continue checking.  Assume that the
793          address is unsigned.  */
794       lm = extract_unsigned_integer (objfile_lm_info.lm + lmo->l_next_offset,
795 				     lmo->l_next_size);
796       do_cleanups (old_chain);
797     }
798   return 0;
799 }
800 
801 /* On some systems, the only way to recognize the link map entry for
802    the main executable file is by looking at its name.  Return
803    non-zero iff SONAME matches one of the known main executable names.  */
804 
805 #if 0
806 static int
807 match_main (char *soname)
808 {
809   char **mainp;
810 
811   for (mainp = main_name_list; *mainp != NULL; mainp++)
812     {
813       if (strcmp (soname, *mainp) == 0)
814 	return (1);
815     }
816 
817   return (0);
818 }
819 #endif
820 
821 /* Return 1 if PC lies in the dynamic symbol resolution code of the
822    SVR4 run time loader.  */
823 static CORE_ADDR interp_text_sect_low;
824 static CORE_ADDR interp_text_sect_high;
825 static CORE_ADDR interp_plt_sect_low;
826 static CORE_ADDR interp_plt_sect_high;
827 
828 static int
829 svr4_in_dynsym_resolve_code (CORE_ADDR pc)
830 {
831   return ((pc >= interp_text_sect_low && pc < interp_text_sect_high)
832 	  || (pc >= interp_plt_sect_low && pc < interp_plt_sect_high)
833 	  || in_plt_section (pc, NULL));
834 }
835 
836 /* Given an executable's ABFD and target, compute the entry-point
837    address.  */
838 
839 static CORE_ADDR
840 exec_entry_point (struct bfd *abfd, struct target_ops *targ)
841 {
842   /* KevinB wrote ... for most targets, the address returned by
843      bfd_get_start_address() is the entry point for the start
844      function.  But, for some targets, bfd_get_start_address() returns
845      the address of a function descriptor from which the entry point
846      address may be extracted.  This address is extracted by
847      gdbarch_convert_from_func_ptr_addr().  The method
848      gdbarch_convert_from_func_ptr_addr() is the merely the identify
849      function for targets which don't use function descriptors.  */
850   return gdbarch_convert_from_func_ptr_addr (current_gdbarch,
851 					     bfd_get_start_address (abfd),
852 					     targ);
853 }
854 
855 /*
856 
857    LOCAL FUNCTION
858 
859    enable_break -- arrange for dynamic linker to hit breakpoint
860 
861    SYNOPSIS
862 
863    int enable_break (void)
864 
865    DESCRIPTION
866 
867    Both the SunOS and the SVR4 dynamic linkers have, as part of their
868    debugger interface, support for arranging for the inferior to hit
869    a breakpoint after mapping in the shared libraries.  This function
870    enables that breakpoint.
871 
872    For SunOS, there is a special flag location (in_debugger) which we
873    set to 1.  When the dynamic linker sees this flag set, it will set
874    a breakpoint at a location known only to itself, after saving the
875    original contents of that place and the breakpoint address itself,
876    in it's own internal structures.  When we resume the inferior, it
877    will eventually take a SIGTRAP when it runs into the breakpoint.
878    We handle this (in a different place) by restoring the contents of
879    the breakpointed location (which is only known after it stops),
880    chasing around to locate the shared libraries that have been
881    loaded, then resuming.
882 
883    For SVR4, the debugger interface structure contains a member (r_brk)
884    which is statically initialized at the time the shared library is
885    built, to the offset of a function (_r_debug_state) which is guaran-
886    teed to be called once before mapping in a library, and again when
887    the mapping is complete.  At the time we are examining this member,
888    it contains only the unrelocated offset of the function, so we have
889    to do our own relocation.  Later, when the dynamic linker actually
890    runs, it relocates r_brk to be the actual address of _r_debug_state().
891 
892    The debugger interface structure also contains an enumeration which
893    is set to either RT_ADD or RT_DELETE prior to changing the mapping,
894    depending upon whether or not the library is being mapped or unmapped,
895    and then set to RT_CONSISTENT after the library is mapped/unmapped.
896  */
897 
898 static int
899 enable_break (void)
900 {
901   int success = 0;
902 
903 #ifdef BKPT_AT_SYMBOL
904 
905   struct minimal_symbol *msymbol;
906   char **bkpt_namep;
907   asection *interp_sect;
908 
909   /* First, remove all the solib event breakpoints.  Their addresses
910      may have changed since the last time we ran the program.  */
911   remove_solib_event_breakpoints ();
912 
913   interp_text_sect_low = interp_text_sect_high = 0;
914   interp_plt_sect_low = interp_plt_sect_high = 0;
915 
916   /* Find the .interp section; if not found, warn the user and drop
917      into the old breakpoint at symbol code.  */
918   interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
919   if (interp_sect)
920     {
921       unsigned int interp_sect_size;
922       char *buf;
923       CORE_ADDR load_addr = 0;
924       int load_addr_found = 0;
925       struct so_list *so;
926       bfd *tmp_bfd = NULL;
927       struct target_ops *tmp_bfd_target;
928       int tmp_fd = -1;
929       char *tmp_pathname = NULL;
930       CORE_ADDR sym_addr = 0;
931 
932       /* Read the contents of the .interp section into a local buffer;
933          the contents specify the dynamic linker this program uses.  */
934       interp_sect_size = bfd_section_size (exec_bfd, interp_sect);
935       buf = alloca (interp_sect_size);
936       bfd_get_section_contents (exec_bfd, interp_sect,
937 				buf, 0, interp_sect_size);
938 
939       /* Now we need to figure out where the dynamic linker was
940          loaded so that we can load its symbols and place a breakpoint
941          in the dynamic linker itself.
942 
943          This address is stored on the stack.  However, I've been unable
944          to find any magic formula to find it for Solaris (appears to
945          be trivial on GNU/Linux).  Therefore, we have to try an alternate
946          mechanism to find the dynamic linker's base address.  */
947 
948       tmp_fd  = solib_open (buf, &tmp_pathname);
949       if (tmp_fd >= 0)
950 	tmp_bfd = bfd_fdopenr (tmp_pathname, gnutarget, tmp_fd);
951 
952       if (tmp_bfd == NULL)
953 	goto bkpt_at_symbol;
954 
955       /* Make sure the dynamic linker's really a useful object.  */
956       if (!bfd_check_format (tmp_bfd, bfd_object))
957 	{
958 	  warning ("Unable to grok dynamic linker %s as an object file", buf);
959 	  bfd_close (tmp_bfd);
960 	  goto bkpt_at_symbol;
961 	}
962 
963       /* Now convert the TMP_BFD into a target.  That way target, as
964          well as BFD operations can be used.  Note that closing the
965          target will also close the underlying bfd.  */
966       tmp_bfd_target = target_bfd_reopen (tmp_bfd);
967 
968       /* On a running target, we can get the dynamic linker's base
969          address from the shared library table.  */
970       solib_add (NULL, 0, NULL, auto_solib_add);
971       so = master_so_list ();
972       while (so)
973 	{
974 	  if (strcmp (buf, so->so_original_name) == 0)
975 	    {
976 	      load_addr_found = 1;
977 	      load_addr = LM_ADDR (so);
978 	      break;
979 	    }
980 	  so = so->next;
981 	}
982 
983       /* Otherwise we find the dynamic linker's base address by examining
984 	 the current pc (which should point at the entry point for the
985 	 dynamic linker) and subtracting the offset of the entry point.  */
986       if (!load_addr_found)
987 	load_addr = (read_pc ()
988 		     - exec_entry_point (tmp_bfd, tmp_bfd_target));
989 
990       /* Record the relocated start and end address of the dynamic linker
991          text and plt section for svr4_in_dynsym_resolve_code.  */
992       interp_sect = bfd_get_section_by_name (tmp_bfd, ".text");
993       if (interp_sect)
994 	{
995 	  interp_text_sect_low =
996 	    bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
997 	  interp_text_sect_high =
998 	    interp_text_sect_low + bfd_section_size (tmp_bfd, interp_sect);
999 	}
1000       interp_sect = bfd_get_section_by_name (tmp_bfd, ".plt");
1001       if (interp_sect)
1002 	{
1003 	  interp_plt_sect_low =
1004 	    bfd_section_vma (tmp_bfd, interp_sect) + load_addr;
1005 	  interp_plt_sect_high =
1006 	    interp_plt_sect_low + bfd_section_size (tmp_bfd, interp_sect);
1007 	}
1008 
1009       /* Now try to set a breakpoint in the dynamic linker.  */
1010       for (bkpt_namep = solib_break_names; *bkpt_namep != NULL; bkpt_namep++)
1011 	{
1012           /* On ABI's that use function descriptors, there are usually
1013              two linker symbols associated with each C function: one
1014              pointing at the actual entry point of the machine code,
1015              and one pointing at the function's descriptor.  The
1016              latter symbol has the same name as the C function.
1017 
1018              What we're looking for here is the machine code entry
1019              point, so we are only interested in symbols in code
1020              sections.  */
1021 	  sym_addr = bfd_lookup_symbol (tmp_bfd, *bkpt_namep, SEC_CODE);
1022 	  if (sym_addr != 0)
1023 	    break;
1024 	}
1025 
1026       /* We're done with both the temporary bfd and target.  Remember,
1027          closing the target closes the underlying bfd.  */
1028       target_close (tmp_bfd_target, 0);
1029 
1030       if (sym_addr != 0)
1031 	{
1032 	  create_solib_event_breakpoint (load_addr + sym_addr);
1033 	  return 1;
1034 	}
1035 
1036       /* For whatever reason we couldn't set a breakpoint in the dynamic
1037          linker.  Warn and drop into the old code.  */
1038     bkpt_at_symbol:
1039       warning ("Unable to find dynamic linker breakpoint function.\nGDB will be unable to debug shared library initializers\nand track explicitly loaded dynamic code.");
1040     }
1041 
1042   /* Scan through the list of symbols, trying to look up the symbol and
1043      set a breakpoint there.  Terminate loop when we/if we succeed. */
1044 
1045   breakpoint_addr = 0;
1046   for (bkpt_namep = bkpt_names; *bkpt_namep != NULL; bkpt_namep++)
1047     {
1048       msymbol = lookup_minimal_symbol (*bkpt_namep, NULL, symfile_objfile);
1049       if ((msymbol != NULL) && (SYMBOL_VALUE_ADDRESS (msymbol) != 0))
1050 	{
1051 	  create_solib_event_breakpoint (SYMBOL_VALUE_ADDRESS (msymbol));
1052 	  return 1;
1053 	}
1054     }
1055 
1056   /* Nothing good happened.  */
1057   success = 0;
1058 
1059 #endif /* BKPT_AT_SYMBOL */
1060 
1061   return (success);
1062 }
1063 
1064 /*
1065 
1066    LOCAL FUNCTION
1067 
1068    special_symbol_handling -- additional shared library symbol handling
1069 
1070    SYNOPSIS
1071 
1072    void special_symbol_handling ()
1073 
1074    DESCRIPTION
1075 
1076    Once the symbols from a shared object have been loaded in the usual
1077    way, we are called to do any system specific symbol handling that
1078    is needed.
1079 
1080    For SunOS4, this consisted of grunging around in the dynamic
1081    linkers structures to find symbol definitions for "common" symbols
1082    and adding them to the minimal symbol table for the runtime common
1083    objfile.
1084 
1085    However, for SVR4, there's nothing to do.
1086 
1087  */
1088 
1089 static void
1090 svr4_special_symbol_handling (void)
1091 {
1092 }
1093 
1094 /* Relocate the main executable.  This function should be called upon
1095    stopping the inferior process at the entry point to the program.
1096    The entry point from BFD is compared to the PC and if they are
1097    different, the main executable is relocated by the proper amount.
1098 
1099    As written it will only attempt to relocate executables which
1100    lack interpreter sections.  It seems likely that only dynamic
1101    linker executables will get relocated, though it should work
1102    properly for a position-independent static executable as well.  */
1103 
1104 static void
1105 svr4_relocate_main_executable (void)
1106 {
1107   asection *interp_sect;
1108   CORE_ADDR pc = read_pc ();
1109 
1110   /* Decide if the objfile needs to be relocated.  As indicated above,
1111      we will only be here when execution is stopped at the beginning
1112      of the program.  Relocation is necessary if the address at which
1113      we are presently stopped differs from the start address stored in
1114      the executable AND there's no interpreter section.  The condition
1115      regarding the interpreter section is very important because if
1116      there *is* an interpreter section, execution will begin there
1117      instead.  When there is an interpreter section, the start address
1118      is (presumably) used by the interpreter at some point to start
1119      execution of the program.
1120 
1121      If there is an interpreter, it is normal for it to be set to an
1122      arbitrary address at the outset.  The job of finding it is
1123      handled in enable_break().
1124 
1125      So, to summarize, relocations are necessary when there is no
1126      interpreter section and the start address obtained from the
1127      executable is different from the address at which GDB is
1128      currently stopped.
1129 
1130      [ The astute reader will note that we also test to make sure that
1131        the executable in question has the DYNAMIC flag set.  It is my
1132        opinion that this test is unnecessary (undesirable even).  It
1133        was added to avoid inadvertent relocation of an executable
1134        whose e_type member in the ELF header is not ET_DYN.  There may
1135        be a time in the future when it is desirable to do relocations
1136        on other types of files as well in which case this condition
1137        should either be removed or modified to accomodate the new file
1138        type.  (E.g, an ET_EXEC executable which has been built to be
1139        position-independent could safely be relocated by the OS if
1140        desired.  It is true that this violates the ABI, but the ABI
1141        has been known to be bent from time to time.)  - Kevin, Nov 2000. ]
1142      */
1143 
1144   interp_sect = bfd_get_section_by_name (exec_bfd, ".interp");
1145   if (interp_sect == NULL
1146       && (bfd_get_file_flags (exec_bfd) & DYNAMIC) != 0
1147       && (exec_entry_point (exec_bfd, &exec_ops) != pc))
1148     {
1149       struct cleanup *old_chain;
1150       struct section_offsets *new_offsets;
1151       int i, changed;
1152       CORE_ADDR displacement;
1153 
1154       /* It is necessary to relocate the objfile.  The amount to
1155 	 relocate by is simply the address at which we are stopped
1156 	 minus the starting address from the executable.
1157 
1158 	 We relocate all of the sections by the same amount.  This
1159 	 behavior is mandated by recent editions of the System V ABI.
1160 	 According to the System V Application Binary Interface,
1161 	 Edition 4.1, page 5-5:
1162 
1163 	   ...  Though the system chooses virtual addresses for
1164 	   individual processes, it maintains the segments' relative
1165 	   positions.  Because position-independent code uses relative
1166 	   addressesing between segments, the difference between
1167 	   virtual addresses in memory must match the difference
1168 	   between virtual addresses in the file.  The difference
1169 	   between the virtual address of any segment in memory and
1170 	   the corresponding virtual address in the file is thus a
1171 	   single constant value for any one executable or shared
1172 	   object in a given process.  This difference is the base
1173 	   address.  One use of the base address is to relocate the
1174 	   memory image of the program during dynamic linking.
1175 
1176 	 The same language also appears in Edition 4.0 of the System V
1177 	 ABI and is left unspecified in some of the earlier editions.  */
1178 
1179       displacement = pc - exec_entry_point (exec_bfd, &exec_ops);
1180       changed = 0;
1181 
1182       new_offsets = xcalloc (symfile_objfile->num_sections,
1183 			     sizeof (struct section_offsets));
1184       old_chain = make_cleanup (xfree, new_offsets);
1185 
1186       for (i = 0; i < symfile_objfile->num_sections; i++)
1187 	{
1188 	  if (displacement != ANOFFSET (symfile_objfile->section_offsets, i))
1189 	    changed = 1;
1190 	  new_offsets->offsets[i] = displacement;
1191 	}
1192 
1193       if (changed)
1194 	objfile_relocate (symfile_objfile, new_offsets);
1195 
1196       do_cleanups (old_chain);
1197     }
1198 }
1199 
1200 /*
1201 
1202    GLOBAL FUNCTION
1203 
1204    svr4_solib_create_inferior_hook -- shared library startup support
1205 
1206    SYNOPSIS
1207 
1208    void svr4_solib_create_inferior_hook()
1209 
1210    DESCRIPTION
1211 
1212    When gdb starts up the inferior, it nurses it along (through the
1213    shell) until it is ready to execute it's first instruction.  At this
1214    point, this function gets called via expansion of the macro
1215    SOLIB_CREATE_INFERIOR_HOOK.
1216 
1217    For SunOS executables, this first instruction is typically the
1218    one at "_start", or a similar text label, regardless of whether
1219    the executable is statically or dynamically linked.  The runtime
1220    startup code takes care of dynamically linking in any shared
1221    libraries, once gdb allows the inferior to continue.
1222 
1223    For SVR4 executables, this first instruction is either the first
1224    instruction in the dynamic linker (for dynamically linked
1225    executables) or the instruction at "start" for statically linked
1226    executables.  For dynamically linked executables, the system
1227    first exec's /lib/libc.so.N, which contains the dynamic linker,
1228    and starts it running.  The dynamic linker maps in any needed
1229    shared libraries, maps in the actual user executable, and then
1230    jumps to "start" in the user executable.
1231 
1232    For both SunOS shared libraries, and SVR4 shared libraries, we
1233    can arrange to cooperate with the dynamic linker to discover the
1234    names of shared libraries that are dynamically linked, and the
1235    base addresses to which they are linked.
1236 
1237    This function is responsible for discovering those names and
1238    addresses, and saving sufficient information about them to allow
1239    their symbols to be read at a later time.
1240 
1241    FIXME
1242 
1243    Between enable_break() and disable_break(), this code does not
1244    properly handle hitting breakpoints which the user might have
1245    set in the startup code or in the dynamic linker itself.  Proper
1246    handling will probably have to wait until the implementation is
1247    changed to use the "breakpoint handler function" method.
1248 
1249    Also, what if child has exit()ed?  Must exit loop somehow.
1250  */
1251 
1252 static void
1253 svr4_solib_create_inferior_hook (void)
1254 {
1255   /* Relocate the main executable if necessary.  */
1256   svr4_relocate_main_executable ();
1257 
1258   if (!svr4_have_link_map_offsets ())
1259     {
1260       warning ("no shared library support for this OS / ABI");
1261       return;
1262 
1263     }
1264 
1265   if (!enable_break ())
1266     {
1267       warning ("shared library handler failed to enable breakpoint");
1268       return;
1269     }
1270 
1271 #if defined(_SCO_DS)
1272   /* SCO needs the loop below, other systems should be using the
1273      special shared library breakpoints and the shared library breakpoint
1274      service routine.
1275 
1276      Now run the target.  It will eventually hit the breakpoint, at
1277      which point all of the libraries will have been mapped in and we
1278      can go groveling around in the dynamic linker structures to find
1279      out what we need to know about them. */
1280 
1281   clear_proceed_status ();
1282   stop_soon = STOP_QUIETLY;
1283   stop_signal = TARGET_SIGNAL_0;
1284   do
1285     {
1286       target_resume (pid_to_ptid (-1), 0, stop_signal);
1287       wait_for_inferior ();
1288     }
1289   while (stop_signal != TARGET_SIGNAL_TRAP);
1290   stop_soon = NO_STOP_QUIETLY;
1291 #endif /* defined(_SCO_DS) */
1292 
1293    disable_breakpoints_at_startup (1);
1294 }
1295 
1296 static void
1297 svr4_clear_solib (void)
1298 {
1299   debug_base = 0;
1300 }
1301 
1302 static void
1303 svr4_free_so (struct so_list *so)
1304 {
1305   xfree (so->lm_info->lm);
1306   xfree (so->lm_info);
1307 }
1308 
1309 
1310 /* Clear any bits of ADDR that wouldn't fit in a target-format
1311    data pointer.  "Data pointer" here refers to whatever sort of
1312    address the dynamic linker uses to manage its sections.  At the
1313    moment, we don't support shared libraries on any processors where
1314    code and data pointers are different sizes.
1315 
1316    This isn't really the right solution.  What we really need here is
1317    a way to do arithmetic on CORE_ADDR values that respects the
1318    natural pointer/address correspondence.  (For example, on the MIPS,
1319    converting a 32-bit pointer to a 64-bit CORE_ADDR requires you to
1320    sign-extend the value.  There, simply truncating the bits above
1321    TARGET_PTR_BIT, as we do below, is no good.)  This should probably
1322    be a new gdbarch method or something.  */
1323 static CORE_ADDR
1324 svr4_truncate_ptr (CORE_ADDR addr)
1325 {
1326   if (TARGET_PTR_BIT == sizeof (CORE_ADDR) * 8)
1327     /* We don't need to truncate anything, and the bit twiddling below
1328        will fail due to overflow problems.  */
1329     return addr;
1330   else
1331     return addr & (((CORE_ADDR) 1 << TARGET_PTR_BIT) - 1);
1332 }
1333 
1334 
1335 static void
1336 svr4_relocate_section_addresses (struct so_list *so,
1337                                  struct section_table *sec)
1338 {
1339   sec->addr    = svr4_truncate_ptr (sec->addr    + LM_ADDR (so));
1340   sec->endaddr = svr4_truncate_ptr (sec->endaddr + LM_ADDR (so));
1341 }
1342 
1343 
1344 /* Fetch a link_map_offsets structure for native targets using struct
1345    definitions from link.h.  See solib-legacy.c for the function
1346    which does the actual work.
1347 
1348    Note: For non-native targets (i.e. cross-debugging situations),
1349    a target specific fetch_link_map_offsets() function should be
1350    defined and registered via set_solib_svr4_fetch_link_map_offsets().  */
1351 
1352 static struct link_map_offsets *
1353 legacy_fetch_link_map_offsets (void)
1354 {
1355   if (legacy_svr4_fetch_link_map_offsets_hook)
1356     return legacy_svr4_fetch_link_map_offsets_hook ();
1357   else
1358     {
1359       internal_error (__FILE__, __LINE__,
1360                       "legacy_fetch_link_map_offsets called without legacy "
1361 		      "link_map support enabled.");
1362       return 0;
1363     }
1364 }
1365 
1366 /* Fetch a link_map_offsets structure using the method registered in the
1367    architecture vector.  */
1368 
1369 static struct link_map_offsets *
1370 svr4_fetch_link_map_offsets (void)
1371 {
1372   struct link_map_offsets *(*flmo)(void) =
1373     gdbarch_data (current_gdbarch, fetch_link_map_offsets_gdbarch_data);
1374 
1375   if (flmo == NULL)
1376     {
1377       internal_error (__FILE__, __LINE__,
1378                       "svr4_fetch_link_map_offsets: fetch_link_map_offsets "
1379 		      "method not defined for this architecture.");
1380       return 0;
1381     }
1382   else
1383     return (flmo ());
1384 }
1385 
1386 /* Return 1 if a link map offset fetcher has been defined, 0 otherwise.  */
1387 static int
1388 svr4_have_link_map_offsets (void)
1389 {
1390   struct link_map_offsets *(*flmo)(void) =
1391     gdbarch_data (current_gdbarch, fetch_link_map_offsets_gdbarch_data);
1392   if (flmo == NULL
1393       || (flmo == legacy_fetch_link_map_offsets
1394           && legacy_svr4_fetch_link_map_offsets_hook == NULL))
1395     return 0;
1396   else
1397     return 1;
1398 }
1399 
1400 /* set_solib_svr4_fetch_link_map_offsets() is intended to be called by
1401    a <arch>_gdbarch_init() function.  It is used to establish an
1402    architecture specific link_map_offsets fetcher for the architecture
1403    being defined.  */
1404 
1405 void
1406 set_solib_svr4_fetch_link_map_offsets (struct gdbarch *gdbarch,
1407                                        struct link_map_offsets *(*flmo) (void))
1408 {
1409   deprecated_set_gdbarch_data (gdbarch, fetch_link_map_offsets_gdbarch_data, flmo);
1410 }
1411 
1412 /* Initialize the architecture-specific link_map_offsets fetcher.
1413    This is called after <arch>_gdbarch_init() has set up its `struct
1414    gdbarch' for the new architecture, and is only called if the
1415    link_map_offsets fetcher isn't already initialized (which is
1416    usually done by calling set_solib_svr4_fetch_link_map_offsets()
1417    above in <arch>_gdbarch_init()).  Therefore we attempt to provide a
1418    reasonable alternative (for native targets anyway) if the
1419    <arch>_gdbarch_init() fails to call
1420    set_solib_svr4_fetch_link_map_offsets().  */
1421 
1422 static void *
1423 init_fetch_link_map_offsets (struct gdbarch *gdbarch)
1424 {
1425   return legacy_fetch_link_map_offsets;
1426 }
1427 
1428 /* Most OS'es that have SVR4-style ELF dynamic libraries define a
1429    `struct r_debug' and a `struct link_map' that are binary compatible
1430    with the origional SVR4 implementation.  */
1431 
1432 /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
1433    for an ILP32 SVR4 system.  */
1434 
1435 struct link_map_offsets *
1436 svr4_ilp32_fetch_link_map_offsets (void)
1437 {
1438   static struct link_map_offsets lmo;
1439   static struct link_map_offsets *lmp = NULL;
1440 
1441   if (lmp == NULL)
1442     {
1443       lmp = &lmo;
1444 
1445       /* Everything we need is in the first 8 bytes.  */
1446       lmo.r_debug_size = 8;
1447       lmo.r_map_offset = 4;
1448       lmo.r_map_size   = 4;
1449 
1450       /* Everything we need is in the first 20 bytes.  */
1451       lmo.link_map_size = 20;
1452       lmo.l_addr_offset = 0;
1453       lmo.l_addr_size   = 4;
1454       lmo.l_name_offset = 4;
1455       lmo.l_name_size   = 4;
1456       lmo.l_next_offset = 12;
1457       lmo.l_next_size   = 4;
1458       lmo.l_prev_offset = 16;
1459       lmo.l_prev_size   = 4;
1460     }
1461 
1462   return lmp;
1463 }
1464 
1465 /* Fetch (and possibly build) an appropriate `struct link_map_offsets'
1466    for an LP64 SVR4 system.  */
1467 
1468 struct link_map_offsets *
1469 svr4_lp64_fetch_link_map_offsets (void)
1470 {
1471   static struct link_map_offsets lmo;
1472   static struct link_map_offsets *lmp = NULL;
1473 
1474   if (lmp == NULL)
1475     {
1476       lmp = &lmo;
1477 
1478       /* Everything we need is in the first 16 bytes.  */
1479       lmo.r_debug_size = 16;
1480       lmo.r_map_offset = 8;
1481       lmo.r_map_size   = 8;
1482 
1483       /* Everything we need is in the first 40 bytes.  */
1484       lmo.link_map_size = 40;
1485       lmo.l_addr_offset = 0;
1486       lmo.l_addr_size   = 8;
1487       lmo.l_name_offset = 8;
1488       lmo.l_name_size   = 8;
1489       lmo.l_next_offset = 24;
1490       lmo.l_next_size   = 8;
1491       lmo.l_prev_offset = 32;
1492       lmo.l_prev_size   = 8;
1493     }
1494 
1495   return lmp;
1496 }
1497 
1498 
1499 static struct target_so_ops svr4_so_ops;
1500 
1501 extern initialize_file_ftype _initialize_svr4_solib; /* -Wmissing-prototypes */
1502 
1503 void
1504 _initialize_svr4_solib (void)
1505 {
1506   fetch_link_map_offsets_gdbarch_data =
1507     gdbarch_data_register_post_init (init_fetch_link_map_offsets);
1508 
1509   svr4_so_ops.relocate_section_addresses = svr4_relocate_section_addresses;
1510   svr4_so_ops.free_so = svr4_free_so;
1511   svr4_so_ops.clear_solib = svr4_clear_solib;
1512   svr4_so_ops.solib_create_inferior_hook = svr4_solib_create_inferior_hook;
1513   svr4_so_ops.special_symbol_handling = svr4_special_symbol_handling;
1514   svr4_so_ops.current_sos = svr4_current_sos;
1515   svr4_so_ops.open_symbol_file_object = open_symbol_file_object;
1516   svr4_so_ops.in_dynsym_resolve_code = svr4_in_dynsym_resolve_code;
1517 
1518   /* FIXME: Don't do this here.  *_gdbarch_init() should set so_ops. */
1519   current_target_so_ops = &svr4_so_ops;
1520 }
1521