1 /* Handle Darwin shared libraries for GDB, the GNU Debugger.
3 Copyright (C) 2009 Free Software Foundation, Inc.
5 This file is part of GDB.
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "gdbthread.h"
31 #include "gdb_assert.h"
35 #include "solib-svr4.h"
37 #include "bfd-target.h"
41 #include "exceptions.h"
44 struct gdb_dyld_image_info
46 /* Base address (which corresponds to the Mach-O header). */
47 CORE_ADDR mach_header;
48 /* Image file path. */
50 /* st.m_time of image file. */
54 /* Content of inferior dyld_all_image_infos structure. */
55 struct gdb_dyld_all_image_infos
59 /* Number of images. */
61 /* Image description. */
63 /* Notifier (function called when a library is added or removed). */
67 /* Current all_image_infos version. */
68 #define DYLD_VERSION 1
70 /* Address of structure dyld_all_image_infos in inferior. */
71 static CORE_ADDR dyld_all_image_addr;
73 /* Gdb copy of dyld_all_info_infos. */
74 static struct gdb_dyld_all_image_infos dyld_all_image;
76 /* Read dyld_all_image from inferior. */
78 darwin_load_image_infos (void)
81 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
84 /* If the structure address is not known, don't continue. */
85 if (dyld_all_image_addr == 0)
88 /* The structure has 4 fields: version (4 bytes), count (4 bytes),
89 info (pointer) and notifier (pointer). */
90 len = 4 + 4 + 2 * ptr_type->length;
91 gdb_assert (len <= sizeof (buf));
92 memset (&dyld_all_image, 0, sizeof (dyld_all_image));
94 /* Read structure raw bytes from target. */
95 if (target_read_memory (dyld_all_image_addr, buf, len))
98 /* Extract the fields. */
99 dyld_all_image.version = extract_unsigned_integer (buf, 4);
100 if (dyld_all_image.version != DYLD_VERSION)
103 dyld_all_image.count = extract_unsigned_integer (buf + 4, 4);
104 dyld_all_image.info = extract_typed_address (buf + 8, ptr_type);
105 dyld_all_image.notifier = extract_typed_address
106 (buf + 8 + ptr_type->length, ptr_type);
109 /* Link map info to include in an allocated so_list entry. */
113 /* The target location of lm. */
117 struct darwin_so_list
121 /* Darwin specific data. */
125 /* Lookup the value for a specific symbol. */
127 lookup_symbol_from_bfd (bfd *abfd, char *symname)
130 asymbol **symbol_table;
131 unsigned int number_of_symbols;
133 CORE_ADDR symaddr = 0;
135 storage_needed = bfd_get_symtab_upper_bound (abfd);
137 if (storage_needed <= 0)
140 symbol_table = (asymbol **) xmalloc (storage_needed);
141 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
143 for (i = 0; i < number_of_symbols; i++)
145 asymbol *sym = symbol_table[i];
146 if (strcmp (sym->name, symname) == 0
147 && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0)
149 /* BFD symbols are section relative. */
150 symaddr = sym->value + sym->section->vma;
154 xfree (symbol_table);
159 /* Return program interpreter string. */
161 find_program_interpreter (void)
163 gdb_byte *buf = NULL;
165 /* If we have an exec_bfd, use its section table. */
168 struct bfd_section *dylinker_sect;
170 dylinker_sect = bfd_get_section_by_name (exec_bfd, "LC_LOAD_DYLINKER");
171 if (dylinker_sect != NULL)
173 int sect_size = bfd_section_size (exec_bfd, dylinker_sect);
175 buf = xmalloc (sect_size);
176 if (bfd_get_section_contents (exec_bfd, dylinker_sect,
183 /* If we didn't find it, read from memory.
188 /* Not used. I don't see how the main symbol file can be found: the
189 interpreter name is needed and it is known from the executable file.
190 Note that darwin-nat.c implements pid_to_exec_file. */
192 open_symbol_file_object (void *from_ttyp)
197 /* Build a list of currently loaded shared objects. See solib-svr4.c */
198 static struct so_list *
199 darwin_current_sos (void)
201 struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
202 int ptr_len = TYPE_LENGTH (ptr_type);
203 unsigned int image_info_size;
205 struct so_list *head = NULL;
206 struct so_list *tail = NULL;
209 /* Be sure image infos are loaded. */
210 darwin_load_image_infos ();
212 if (dyld_all_image.version != DYLD_VERSION)
215 image_info_size = ptr_len * 3;
217 /* Read infos for each solib.
218 This first entry is ignored as this is the executable itself. */
219 for (i = 1; i < dyld_all_image.count; i++)
221 CORE_ADDR info = dyld_all_image.info + i * image_info_size;
222 char buf[image_info_size];
227 struct darwin_so_list *dnew;
229 struct cleanup *old_chain;
231 /* Read image info from inferior. */
232 if (target_read_memory (info, buf, image_info_size))
235 load_addr = extract_typed_address (buf, ptr_type);
236 path_addr = extract_typed_address (buf + ptr_len, ptr_type);
238 target_read_string (path_addr, &file_path,
239 SO_NAME_MAX_PATH_SIZE - 1, &errcode);
243 /* Create and fill the new so_list element. */
244 dnew = XZALLOC (struct darwin_so_list);
246 old_chain = make_cleanup (xfree, dnew);
248 new->lm_info = &dnew->li;
250 strncpy (new->so_name, file_path, SO_NAME_MAX_PATH_SIZE - 1);
251 new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
252 strcpy (new->so_original_name, new->so_name);
254 new->lm_info->lm_addr = load_addr;
262 discard_cleanups (old_chain);
268 /* Return 1 if PC lies in the dynamic symbol resolution code of the
271 darwin_in_dynsym_resolve_code (CORE_ADDR pc)
277 /* No special symbol handling. */
279 darwin_special_symbol_handling (void)
283 /* Shared library startup support. See documentation in solib-svr4.c */
285 darwin_solib_create_inferior_hook (void)
287 struct minimal_symbol *msymbol;
289 asection *interp_sect;
290 gdb_byte *interp_name;
292 CORE_ADDR load_addr = 0;
293 int load_addr_found = 0;
294 int loader_found_in_list = 0;
296 bfd *dyld_bfd = NULL;
297 struct inferior *inf = current_inferior ();
299 /* First, remove all the solib event breakpoints. Their addresses
300 may have changed since the last time we ran the program. */
301 remove_solib_event_breakpoints ();
303 /* Find the program interpreter. */
304 interp_name = find_program_interpreter ();
308 /* Create a bfd for the interpreter. */
310 dyld_bfd = bfd_openr (interp_name, gnutarget);
314 sub = bfd_mach_o_fat_extract (dyld_bfd, bfd_object,
315 gdbarch_bfd_arch_info (current_gdbarch));
320 bfd_close (dyld_bfd);
330 if (!inf->attach_flag)
332 /* We find the dynamic linker's base address by examining
333 the current pc (which should point at the entry point for the
334 dynamic linker) and subtracting the offset of the entry point. */
335 load_addr = (read_pc () - bfd_get_start_address (dyld_bfd));
340 Get address of __DATA.__dyld in exec_bfd, read address at offset 0
346 /* Now try to set a breakpoint in the dynamic linker. */
347 dyld_all_image_addr =
348 lookup_symbol_from_bfd (dyld_bfd, "_dyld_all_image_infos");
350 bfd_close (dyld_bfd);
353 if (dyld_all_image_addr == 0)
356 dyld_all_image_addr += load_addr;
358 darwin_load_image_infos ();
360 if (dyld_all_image.version == DYLD_VERSION)
361 create_solib_event_breakpoint (dyld_all_image.notifier);
365 darwin_clear_solib (void)
367 dyld_all_image_addr = 0;
368 dyld_all_image.version = 0;
372 darwin_free_so (struct so_list *so)
376 /* The section table is built from bfd sections using bfd VMAs.
377 Relocate these VMAs according to solib info. */
379 darwin_relocate_section_addresses (struct so_list *so,
380 struct section_table *sec)
382 sec->addr += so->lm_info->lm_addr;
383 sec->endaddr += so->lm_info->lm_addr;
385 /* Best effort to set addr_high/addr_low. This is used only by
386 'info sharedlibary'. */
387 if (so->addr_high == 0)
389 so->addr_low = sec->addr;
390 so->addr_high = sec->endaddr;
392 if (sec->endaddr > so->addr_high)
393 so->addr_high = sec->endaddr;
394 if (sec->addr < so->addr_low)
395 so->addr_low = sec->addr;
398 static struct symbol *
399 darwin_lookup_lib_symbol (const struct objfile *objfile,
401 const char *linkage_name,
402 const domain_enum domain)
408 darwin_bfd_open (char *pathname)
410 char *found_pathname;
415 /* Search for shared library file. */
416 found_pathname = solib_find (pathname, &found_file);
417 if (found_pathname == NULL)
418 perror_with_name (pathname);
420 /* Open bfd for shared library. */
421 abfd = solib_bfd_fopen (found_pathname, found_file);
423 res = bfd_mach_o_fat_extract (abfd, bfd_object,
424 gdbarch_bfd_arch_info (current_gdbarch));
428 make_cleanup (xfree, found_pathname);
429 error (_("`%s': not a shared-library: %s"),
430 found_pathname, bfd_errmsg (bfd_get_error ()));
435 struct target_so_ops darwin_so_ops;
438 _initialize_darwin_solib (void)
440 darwin_so_ops.relocate_section_addresses = darwin_relocate_section_addresses;
441 darwin_so_ops.free_so = darwin_free_so;
442 darwin_so_ops.clear_solib = darwin_clear_solib;
443 darwin_so_ops.solib_create_inferior_hook = darwin_solib_create_inferior_hook;
444 darwin_so_ops.special_symbol_handling = darwin_special_symbol_handling;
445 darwin_so_ops.current_sos = darwin_current_sos;
446 darwin_so_ops.open_symbol_file_object = open_symbol_file_object;
447 darwin_so_ops.in_dynsym_resolve_code = darwin_in_dynsym_resolve_code;
448 darwin_so_ops.lookup_lib_global_symbol = darwin_lookup_lib_symbol;
449 darwin_so_ops.bfd_open = darwin_bfd_open;