1 /* Provide legacy r_debug and link_map support for SVR4-like native targets.
2    Copyright 2000, 2001
3    Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21 
22 #include "defs.h"
23 #include "gdbcore.h"
24 #include "solib-svr4.h"
25 
26 #ifdef HAVE_LINK_H
27 
28 #ifdef HAVE_NLIST_H
29 /* nlist.h needs to be included before link.h on some older *BSD systems. */
30 #include <nlist.h>
31 #endif
32 
33 #include <link.h>
34 
35 /* Fetch (and possibly build) an appropriate link_map_offsets structure
36    for native targets using struct definitions from link.h.  */
37 
38 static struct link_map_offsets *
legacy_svr4_fetch_link_map_offsets(void)39 legacy_svr4_fetch_link_map_offsets (void)
40 {
41   static struct link_map_offsets lmo;
42   static struct link_map_offsets *lmp = 0;
43 #if defined (HAVE_STRUCT_LINK_MAP32)
44   static struct link_map_offsets lmo32;
45   static struct link_map_offsets *lmp32 = 0;
46 #endif
47 
48 #ifndef offsetof
49 #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
50 #endif
51 #define fieldsize(TYPE, MEMBER) (sizeof (((TYPE *)0)->MEMBER))
52 
53   if (lmp == 0)
54     {
55       lmp = &lmo;
56 
57 #ifdef HAVE_STRUCT_LINK_MAP_WITH_L_MEMBERS
58       lmo.r_debug_size = sizeof (struct r_debug);
59 
60       lmo.r_map_offset = offsetof (struct r_debug, r_map);
61       lmo.r_map_size = fieldsize (struct r_debug, r_map);
62 
63       lmo.link_map_size = sizeof (struct link_map);
64 
65       lmo.l_addr_offset = offsetof (struct link_map, l_addr);
66       lmo.l_addr_size = fieldsize (struct link_map, l_addr);
67 
68       lmo.l_next_offset = offsetof (struct link_map, l_next);
69       lmo.l_next_size = fieldsize (struct link_map, l_next);
70 
71       lmo.l_prev_offset = offsetof (struct link_map, l_prev);
72       lmo.l_prev_size = fieldsize (struct link_map, l_prev);
73 
74       lmo.l_name_offset = offsetof (struct link_map, l_name);
75       lmo.l_name_size = fieldsize (struct link_map, l_name);
76 #else /* !defined(HAVE_STRUCT_LINK_MAP_WITH_L_MEMBERS) */
77 #ifdef HAVE_STRUCT_LINK_MAP_WITH_LM_MEMBERS
78       lmo.link_map_size = sizeof (struct link_map);
79 
80       lmo.l_addr_offset = offsetof (struct link_map, lm_addr);
81       lmo.l_addr_size = fieldsize (struct link_map, lm_addr);
82 
83       lmo.l_next_offset = offsetof (struct link_map, lm_next);
84       lmo.l_next_size = fieldsize (struct link_map, lm_next);
85 
86       lmo.l_name_offset = offsetof (struct link_map, lm_name);
87       lmo.l_name_size = fieldsize (struct link_map, lm_name);
88 #else /* !defined(HAVE_STRUCT_LINK_MAP_WITH_LM_MEMBERS) */
89 #if HAVE_STRUCT_SO_MAP_WITH_SOM_MEMBERS
90       lmo.link_map_size = sizeof (struct so_map);
91 
92       lmo.l_addr_offset = offsetof (struct so_map, som_addr);
93       lmo.l_addr_size = fieldsize (struct so_map, som_addr);
94 
95       lmo.l_next_offset = offsetof (struct so_map, som_next);
96       lmo.l_next_size = fieldsize (struct so_map, som_next);
97 
98       lmo.l_name_offset = offsetof (struct so_map, som_path);
99       lmo.l_name_size = fieldsize (struct so_map, som_path);
100 #endif /* HAVE_STRUCT_SO_MAP_WITH_SOM_MEMBERS */
101 #endif /* HAVE_STRUCT_LINK_MAP_WITH_LM_MEMBERS */
102 #endif /* HAVE_STRUCT_LINK_MAP_WITH_L_MEMBERS */
103     }
104 
105 #if defined (HAVE_STRUCT_LINK_MAP32)
106   if (lmp32 == 0)
107     {
108       lmp32 = &lmo32;
109 
110       lmo32.r_debug_size = sizeof (struct r_debug32);
111 
112       lmo32.r_map_offset = offsetof (struct r_debug32, r_map);
113       lmo32.r_map_size = fieldsize (struct r_debug32, r_map);
114 
115       lmo32.link_map_size = sizeof (struct link_map32);
116 
117       lmo32.l_addr_offset = offsetof (struct link_map32, l_addr);
118       lmo32.l_addr_size = fieldsize (struct link_map32, l_addr);
119 
120       lmo32.l_next_offset = offsetof (struct link_map32, l_next);
121       lmo32.l_next_size = fieldsize (struct link_map32, l_next);
122 
123       lmo32.l_prev_offset = offsetof (struct link_map32, l_prev);
124       lmo32.l_prev_size = fieldsize (struct link_map32, l_prev);
125 
126       lmo32.l_name_offset = offsetof (struct link_map32, l_name);
127       lmo32.l_name_size = fieldsize (struct link_map32, l_name);
128     }
129 #endif /* defined (HAVE_STRUCT_LINK_MAP32) */
130 
131 #if defined (HAVE_STRUCT_LINK_MAP32)
132   if (exec_bfd != NULL)
133     {
134       if (bfd_get_arch_size (exec_bfd) == 32)
135 	return lmp32;
136     }
137   if (TARGET_PTR_BIT == 32)
138     return lmp32;
139 #endif
140   return lmp;
141 }
142 
143 #endif /* HAVE_LINK_H */
144 
145 extern initialize_file_ftype _initialize_svr4_lm; /* -Wmissing-prototypes */
146 
147 void
_initialize_svr4_lm(void)148 _initialize_svr4_lm (void)
149 {
150 #ifdef HAVE_LINK_H
151   legacy_svr4_fetch_link_map_offsets_hook = legacy_svr4_fetch_link_map_offsets;
152 #endif /* HAVE_LINK_H */
153 }
154