* solib-target.c (solib_target_relocate_section_addresses): Add
[external/binutils.git] / gdb / solib-target.c
1 /* Definitions for targets which report shared library events.
2
3    Copyright (C) 2007
4    Free Software Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor,
21    Boston, MA 02110-1301, USA.  */
22
23 #include "defs.h"
24 #include "objfiles.h"
25 #include "solist.h"
26 #include "symtab.h"
27 #include "symfile.h"
28 #include "target.h"
29 #include "vec.h"
30
31 #include "gdb_string.h"
32
33 DEF_VEC_O(CORE_ADDR);
34
35 /* Private data for each loaded library.  */
36 struct lm_info
37 {
38   /* The library's name.  The name is normally kept in the struct
39      so_list; it is only here during XML parsing.  */
40   char *name;
41
42   /* The base addresses for each independently relocatable segment of
43      this shared library.  */
44   VEC(CORE_ADDR) *segment_bases;
45
46   /* The cached offsets for each section of this shared library,
47      determined from SEGMENT_BASES.  */
48   struct section_offsets *offsets;
49 };
50
51 typedef struct lm_info *lm_info_p;
52 DEF_VEC_P(lm_info_p);
53
54 #if !defined(HAVE_LIBEXPAT)
55
56 static VEC(lm_info_p)
57 solib_target_parse_libraries (const char *library)
58 {
59   static int have_warned;
60
61   if (!have_warned)
62     {
63       have_warned = 1;
64       warning (_("Can not parse XML library list; XML support was disabled "
65                  "at compile time"));
66     }
67
68   return NULL;
69 }
70
71 #else /* HAVE_LIBEXPAT */
72
73 #include "xml-support.h"
74
75 /* Handle the start of a <segment> element.  */
76
77 static void
78 library_list_start_segment (struct gdb_xml_parser *parser,
79                             const struct gdb_xml_element *element,
80                             void *user_data, VEC(gdb_xml_value_s) *attributes)
81 {
82   VEC(lm_info_p) **list = user_data;
83   struct lm_info *last = VEC_last (lm_info_p, *list);
84   ULONGEST *address_p = VEC_index (gdb_xml_value_s, attributes, 0)->value;
85   CORE_ADDR address = (CORE_ADDR) *address_p;
86
87   VEC_safe_push (CORE_ADDR, last->segment_bases, &address);
88 }
89
90 /* Handle the start of a <library> element.  */
91
92 static void
93 library_list_start_library (struct gdb_xml_parser *parser,
94                             const struct gdb_xml_element *element,
95                             void *user_data, VEC(gdb_xml_value_s) *attributes)
96 {
97   VEC(lm_info_p) **list = user_data;
98   struct lm_info *item = XZALLOC (struct lm_info);
99   const char *name = VEC_index (gdb_xml_value_s, attributes, 0)->value;
100
101   item->name = xstrdup (name);
102   VEC_safe_push (lm_info_p, *list, item);
103 }
104
105 /* Handle the start of a <library-list> element.  */
106
107 static void
108 library_list_start_list (struct gdb_xml_parser *parser,
109                          const struct gdb_xml_element *element,
110                          void *user_data, VEC(gdb_xml_value_s) *attributes)
111 {
112   char *version = VEC_index (gdb_xml_value_s, attributes, 0)->value;
113
114   if (strcmp (version, "1.0") != 0)
115     gdb_xml_error (parser,
116                    _("Library list has unsupported version \"%s\""),
117                    version);
118 }
119
120 /* Discard the constructed library list.  */
121
122 static void
123 solib_target_free_library_list (void *p)
124 {
125   VEC(lm_info_p) **result = p;
126   struct lm_info *info;
127   int ix;
128
129   for (ix = 0; VEC_iterate (lm_info_p, *result, ix, info); ix++)
130     {
131       xfree (info->name);
132       VEC_free (CORE_ADDR, info->segment_bases);
133       xfree (info);
134     }
135   VEC_free (lm_info_p, *result);
136   *result = NULL;
137 }
138
139 /* The allowed elements and attributes for an XML library list.
140    The root element is a <library-list>.  */
141
142 const struct gdb_xml_attribute segment_attributes[] = {
143   { "address", GDB_XML_AF_NONE, gdb_xml_parse_attr_ulongest, NULL },
144   { NULL, GDB_XML_AF_NONE, NULL, NULL }
145 };
146
147 const struct gdb_xml_element library_children[] = {
148   { "segment", segment_attributes, NULL, GDB_XML_EF_REPEATABLE,
149     library_list_start_segment, NULL },
150   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
151 };
152
153 const struct gdb_xml_attribute library_attributes[] = {
154   { "name", GDB_XML_AF_NONE, NULL, NULL },
155   { NULL, GDB_XML_AF_NONE, NULL, NULL }
156 };
157
158 const struct gdb_xml_element library_list_children[] = {
159   { "library", library_attributes, library_children,
160     GDB_XML_EF_REPEATABLE | GDB_XML_EF_OPTIONAL,
161     library_list_start_library, NULL },
162   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
163 };
164
165 const struct gdb_xml_attribute library_list_attributes[] = {
166   { "version", GDB_XML_AF_NONE, NULL, NULL },
167   { NULL, GDB_XML_AF_NONE, NULL, NULL }
168 };
169
170 const struct gdb_xml_element library_list_elements[] = {
171   { "library-list", library_list_attributes, library_list_children,
172     GDB_XML_EF_NONE, library_list_start_list, NULL },
173   { NULL, NULL, NULL, GDB_XML_EF_NONE, NULL, NULL }
174 };
175
176 static VEC(lm_info_p) *
177 solib_target_parse_libraries (const char *library)
178 {
179   struct gdb_xml_parser *parser;
180   VEC(lm_info_p) *result = NULL;
181   struct cleanup *before_deleting_result, *back_to;
182
183   back_to = make_cleanup (null_cleanup, NULL);
184   parser = gdb_xml_create_parser_and_cleanup (_("target library list"),
185                                               library_list_elements, &result);
186   gdb_xml_use_dtd (parser, "library-list.dtd");
187
188   before_deleting_result = make_cleanup (solib_target_free_library_list,
189                                          &result);
190
191   if (gdb_xml_parse (parser, library) == 0)
192     /* Parsed successfully, don't need to delete the result.  */
193     discard_cleanups (before_deleting_result);
194
195   do_cleanups (back_to);
196   return result;
197 }
198 #endif
199
200 static struct so_list *
201 solib_target_current_sos (void)
202 {
203   struct so_list *new_solib, *start = NULL, *last = NULL;
204   const char *library_document;
205   VEC(lm_info_p) *library_list;
206   struct lm_info *info;
207   int ix;
208
209   /* Fetch the list of shared libraries.  */
210   library_document = target_read_stralloc (&current_target,
211                                            TARGET_OBJECT_LIBRARIES,
212                                            NULL);
213   if (library_document == NULL)
214     return NULL;
215
216   /* Parse the list.  */
217   library_list = solib_target_parse_libraries (library_document);
218   if (library_list == NULL)
219     return NULL;
220
221   /* Build a struct so_list for each entry on the list.  */
222   for (ix = 0; VEC_iterate (lm_info_p, library_list, ix, info); ix++)
223     {
224       new_solib = XZALLOC (struct so_list);
225       strncpy (new_solib->so_name, info->name, SO_NAME_MAX_PATH_SIZE - 1);
226       new_solib->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
227       strncpy (new_solib->so_original_name, info->name,
228                SO_NAME_MAX_PATH_SIZE - 1);
229       new_solib->so_original_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
230       new_solib->lm_info = info;
231
232       /* We no longer need this copy of the name.  */
233       xfree (info->name);
234       info->name = NULL;
235
236       /* Add it to the list.  */
237       if (!start)
238         last = start = new_solib;
239       else
240         {
241           last->next = new_solib;
242           last = new_solib;
243         }
244     }
245
246   /* Free the library list, but not its members.  */
247   VEC_free (lm_info_p, library_list);
248
249   return start;
250 }
251
252 static void
253 solib_target_special_symbol_handling (void)
254 {
255   /* Nothing needed.  */
256 }
257
258 static void
259 solib_target_solib_create_inferior_hook (void)
260 {
261   /* Nothing needed.  */
262 }
263
264 static void
265 solib_target_clear_solib (void)
266 {
267   /* Nothing needed.  */
268 }
269
270 static void
271 solib_target_free_so (struct so_list *so)
272 {
273   gdb_assert (so->lm_info->name == NULL);
274   xfree (so->lm_info->offsets);
275   VEC_free (CORE_ADDR, so->lm_info->segment_bases);
276   xfree (so->lm_info);
277 }
278
279 static void
280 solib_target_relocate_section_addresses (struct so_list *so,
281                                          struct section_table *sec)
282 {
283   int flags = bfd_get_section_flags (sec->bfd, sec->the_bfd_section);
284   CORE_ADDR offset;
285
286   /* Build the offset table only once per object file.  We can not do
287      it any earlier, since we need to open the file first.  */
288   if (so->lm_info->offsets == NULL)
289     {
290       struct symfile_segment_data *data;
291       int num_sections = bfd_count_sections (so->abfd);
292
293       so->lm_info->offsets = xzalloc (SIZEOF_N_SECTION_OFFSETS (num_sections));
294
295       data = get_symfile_segment_data (so->abfd);
296       if (data == NULL)
297         warning (_("Could not relocate shared library \"%s\": no segments"),
298                  so->so_name);
299       else
300         {
301           ULONGEST orig_delta;
302           int i;
303           int num_bases = VEC_length (CORE_ADDR, so->lm_info->segment_bases);
304           CORE_ADDR *segment_bases = VEC_address (CORE_ADDR,
305                                                   so->lm_info->segment_bases);
306
307           if (!symfile_map_offsets_to_segments (so->abfd, data,
308                                                 so->lm_info->offsets,
309                                                 num_bases, segment_bases))
310             warning (_("Could not relocate shared library \"%s\": bad offsets"),
311                      so->so_name);
312
313           /* Find the range of addresses to report for this library in
314              "info sharedlibrary".  Report any consecutive segments
315              which were relocated as a single unit.  */
316           gdb_assert (num_bases > 0);
317           orig_delta = segment_bases[0] - data->segment_bases[0];
318
319           for (i = 1; i < data->num_segments; i++)
320             {
321               /* If we have run out of offsets, assume all remaining segments
322                  have the same offset.  */
323               if (i >= num_bases)
324                 continue;
325
326               /* If this segment does not have the same offset, do not include
327                  it in the library's range.  */
328               if (segment_bases[i] - data->segment_bases[i] != orig_delta)
329                 break;
330             }
331
332           so->addr_low = segment_bases[0];
333           so->addr_high = (data->segment_bases[i - 1]
334                            + data->segment_sizes[i - 1]
335                            + orig_delta);
336           gdb_assert (so->addr_low <= so->addr_high);
337
338           free_symfile_segment_data (data);
339         }
340     }
341
342   offset = so->lm_info->offsets->offsets[sec->the_bfd_section->index];
343   sec->addr += offset;
344   sec->endaddr += offset;
345 }
346
347 static int
348 solib_target_open_symbol_file_object (void *from_ttyp)
349 {
350   /* We can't locate the main symbol file based on the target's
351      knowledge; the user has to specify it.  */
352   return 0;
353 }
354
355 static int
356 solib_target_in_dynsym_resolve_code (CORE_ADDR pc)
357 {
358   /* We don't have a range of addresses for the dynamic linker; there
359      may not be one in the program's address space.  So only report
360      PLT entries (which may be import stubs).  */
361   return in_plt_section (pc, NULL);
362 }
363
364 static struct target_so_ops solib_target_so_ops;
365
366 extern initialize_file_ftype _initialize_solib_target; /* -Wmissing-prototypes */
367
368 void
369 _initialize_solib_target (void)
370 {
371   solib_target_so_ops.relocate_section_addresses
372     = solib_target_relocate_section_addresses;
373   solib_target_so_ops.free_so = solib_target_free_so;
374   solib_target_so_ops.clear_solib = solib_target_clear_solib;
375   solib_target_so_ops.solib_create_inferior_hook
376     = solib_target_solib_create_inferior_hook;
377   solib_target_so_ops.special_symbol_handling
378     = solib_target_special_symbol_handling;
379   solib_target_so_ops.current_sos = solib_target_current_sos;
380   solib_target_so_ops.open_symbol_file_object
381     = solib_target_open_symbol_file_object;
382   solib_target_so_ops.in_dynsym_resolve_code
383     = solib_target_in_dynsym_resolve_code;
384
385   current_target_so_ops = &solib_target_so_ops;
386 }