1 /* Handle Darwin shared libraries for GDB, the GNU Debugger.
3 Copyright (C) 2009-2023 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/>. */
31 #include "solib-darwin.h"
34 #include "mach-o/external.h"
36 struct gdb_dyld_image_info
38 /* Base address (which corresponds to the Mach-O header). */
39 CORE_ADDR mach_header
;
40 /* Image file path. */
42 /* st.m_time of image file. */
46 /* Content of inferior dyld_all_image_infos structure.
47 See /usr/include/mach-o/dyld_images.h for the documentation. */
48 struct gdb_dyld_all_image_infos
52 /* Number of images. */
54 /* Image description. */
56 /* Notifier (function called when a library is added or removed). */
60 /* Current all_image_infos version. */
61 #define DYLD_VERSION_MIN 1
62 #define DYLD_VERSION_MAX 15
64 /* Per PSPACE specific data. */
67 /* Address of structure dyld_all_image_infos in inferior. */
68 CORE_ADDR all_image_addr
= 0;
70 /* Gdb copy of dyld_all_info_infos. */
71 struct gdb_dyld_all_image_infos all_image
{};
74 /* Per-program-space data key. */
75 static const registry
<program_space
>::key
<darwin_info
>
76 solib_darwin_pspace_data
;
78 /* Get the current darwin data. If none is found yet, add it now. This
79 function always returns a valid object. */
81 static struct darwin_info
*
82 get_darwin_info (void)
84 struct darwin_info
*info
;
86 info
= solib_darwin_pspace_data
.get (current_program_space
);
90 return solib_darwin_pspace_data
.emplace (current_program_space
);
93 /* Return non-zero if the version in dyld_all_image is known. */
96 darwin_dyld_version_ok (const struct darwin_info
*info
)
98 return info
->all_image
.version
>= DYLD_VERSION_MIN
99 && info
->all_image
.version
<= DYLD_VERSION_MAX
;
102 /* Read dyld_all_image from inferior. */
105 darwin_load_image_infos (struct darwin_info
*info
)
108 bfd_endian byte_order
= gdbarch_byte_order (current_inferior ()->arch ());
110 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr
;
113 /* If the structure address is not known, don't continue. */
114 if (info
->all_image_addr
== 0)
117 /* The structure has 4 fields: version (4 bytes), count (4 bytes),
118 info (pointer) and notifier (pointer). */
119 len
= 4 + 4 + 2 * ptr_type
->length ();
120 gdb_assert (len
<= sizeof (buf
));
121 memset (&info
->all_image
, 0, sizeof (info
->all_image
));
123 /* Read structure raw bytes from target. */
124 if (target_read_memory (info
->all_image_addr
, buf
, len
))
127 /* Extract the fields. */
128 info
->all_image
.version
= extract_unsigned_integer (buf
, 4, byte_order
);
129 if (!darwin_dyld_version_ok (info
))
132 info
->all_image
.count
= extract_unsigned_integer (buf
+ 4, 4, byte_order
);
133 info
->all_image
.info
= extract_typed_address (buf
+ 8, ptr_type
);
134 info
->all_image
.notifier
= extract_typed_address
135 (buf
+ 8 + ptr_type
->length (), ptr_type
);
138 /* Link map info to include in an allocated so_list entry. */
140 struct lm_info_darwin
: public lm_info_base
142 /* The target location of lm. */
143 CORE_ADDR lm_addr
= 0;
146 /* Lookup the value for a specific symbol. */
149 lookup_symbol_from_bfd (bfd
*abfd
, const char *symname
)
152 asymbol
**symbol_table
;
153 unsigned int number_of_symbols
;
155 CORE_ADDR symaddr
= 0;
157 storage_needed
= bfd_get_symtab_upper_bound (abfd
);
159 if (storage_needed
<= 0)
162 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
163 number_of_symbols
= bfd_canonicalize_symtab (abfd
, symbol_table
);
165 for (i
= 0; i
< number_of_symbols
; i
++)
167 asymbol
*sym
= symbol_table
[i
];
169 if (strcmp (sym
->name
, symname
) == 0
170 && (sym
->section
->flags
& (SEC_CODE
| SEC_DATA
)) != 0)
172 /* BFD symbols are section relative. */
173 symaddr
= sym
->value
+ sym
->section
->vma
;
177 xfree (symbol_table
);
182 /* Return program interpreter string. */
185 find_program_interpreter (void)
189 /* If we have an current exec_bfd, get the interpreter from the load
191 if (current_program_space
->exec_bfd ())
193 bfd_mach_o_load_command
*cmd
;
195 if (bfd_mach_o_lookup_command (current_program_space
->exec_bfd (),
196 BFD_MACH_O_LC_LOAD_DYLINKER
, &cmd
) == 1)
197 return cmd
->command
.dylinker
.name_str
;
200 /* If we didn't find it, read from memory.
205 /* Not used. I don't see how the main symbol file can be found: the
206 interpreter name is needed and it is known from the executable file.
207 Note that darwin-nat.c implements pid_to_exec_file. */
210 open_symbol_file_object (int from_tty
)
215 /* Build a list of currently loaded shared objects. See solib-svr4.c. */
217 static struct so_list
*
218 darwin_current_sos (void)
221 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr
;
222 enum bfd_endian byte_order
= type_byte_order (ptr_type
);
223 int ptr_len
= ptr_type
->length ();
224 unsigned int image_info_size
;
225 struct so_list
*head
= NULL
;
226 struct so_list
*tail
= NULL
;
228 struct darwin_info
*info
= get_darwin_info ();
230 /* Be sure image infos are loaded. */
231 darwin_load_image_infos (info
);
233 if (!darwin_dyld_version_ok (info
))
236 image_info_size
= ptr_len
* 3;
238 /* Read infos for each solib.
239 The first entry was rumored to be the executable itself, but this is not
240 true when a large number of shared libraries are used (table expanded ?).
241 We now check all entries, but discard executable images. */
242 for (i
= 0; i
< info
->all_image
.count
; i
++)
244 CORE_ADDR iinfo
= info
->all_image
.info
+ i
* image_info_size
;
245 gdb_byte buf
[image_info_size
];
248 struct mach_o_header_external hdr
;
249 unsigned long hdr_val
;
251 /* Read image info from inferior. */
252 if (target_read_memory (iinfo
, buf
, image_info_size
))
255 load_addr
= extract_typed_address (buf
, ptr_type
);
256 path_addr
= extract_typed_address (buf
+ ptr_len
, ptr_type
);
258 /* Read Mach-O header from memory. */
259 if (target_read_memory (load_addr
, (gdb_byte
*) &hdr
, sizeof (hdr
) - 4))
261 /* Discard wrong magic numbers. Shouldn't happen. */
262 hdr_val
= extract_unsigned_integer
263 (hdr
.magic
, sizeof (hdr
.magic
), byte_order
);
264 if (hdr_val
!= BFD_MACH_O_MH_MAGIC
&& hdr_val
!= BFD_MACH_O_MH_MAGIC_64
)
266 /* Discard executable. Should happen only once. */
267 hdr_val
= extract_unsigned_integer
268 (hdr
.filetype
, sizeof (hdr
.filetype
), byte_order
);
269 if (hdr_val
== BFD_MACH_O_MH_EXECUTE
)
272 gdb::unique_xmalloc_ptr
<char> file_path
273 = target_read_string (path_addr
, SO_NAME_MAX_PATH_SIZE
- 1);
274 if (file_path
== nullptr)
277 /* Create and fill the new so_list element. */
278 gdb::unique_xmalloc_ptr
<struct so_list
> newobj (XCNEW (struct so_list
));
280 lm_info_darwin
*li
= new lm_info_darwin
;
281 newobj
->lm_info
= li
;
283 strncpy (newobj
->so_name
, file_path
.get (), SO_NAME_MAX_PATH_SIZE
- 1);
284 newobj
->so_name
[SO_NAME_MAX_PATH_SIZE
- 1] = '\0';
285 strcpy (newobj
->so_original_name
, newobj
->so_name
);
286 li
->lm_addr
= load_addr
;
289 head
= newobj
.get ();
291 tail
->next
= newobj
.get ();
292 tail
= newobj
.release ();
298 /* Check LOAD_ADDR points to a Mach-O executable header. Return LOAD_ADDR
299 in case of success, 0 in case of failure. */
302 darwin_validate_exec_header (CORE_ADDR load_addr
)
304 bfd_endian byte_order
= gdbarch_byte_order (current_inferior ()->arch ());
305 struct mach_o_header_external hdr
;
306 unsigned long hdr_val
;
308 /* Read Mach-O header from memory. */
309 if (target_read_memory (load_addr
, (gdb_byte
*) &hdr
, sizeof (hdr
) - 4))
312 /* Discard wrong magic numbers. Shouldn't happen. */
313 hdr_val
= extract_unsigned_integer
314 (hdr
.magic
, sizeof (hdr
.magic
), byte_order
);
315 if (hdr_val
!= BFD_MACH_O_MH_MAGIC
&& hdr_val
!= BFD_MACH_O_MH_MAGIC_64
)
318 /* Check executable. */
319 hdr_val
= extract_unsigned_integer
320 (hdr
.filetype
, sizeof (hdr
.filetype
), byte_order
);
321 if (hdr_val
== BFD_MACH_O_MH_EXECUTE
)
327 /* Get the load address of the executable using dyld list of images.
328 We assume that the dyld info are correct (which is wrong if the target
329 is stopped at the first instruction). */
332 darwin_read_exec_load_addr_from_dyld (struct darwin_info
*info
)
335 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr
;
336 int ptr_len
= ptr_type
->length ();
337 unsigned int image_info_size
= ptr_len
* 3;
340 /* Read infos for each solib. One of them should be the executable. */
341 for (i
= 0; i
< info
->all_image
.count
; i
++)
343 CORE_ADDR iinfo
= info
->all_image
.info
+ i
* image_info_size
;
344 gdb_byte buf
[image_info_size
];
347 /* Read image info from inferior. */
348 if (target_read_memory (iinfo
, buf
, image_info_size
))
351 load_addr
= extract_typed_address (buf
, ptr_type
);
352 if (darwin_validate_exec_header (load_addr
) == load_addr
)
359 /* Get the load address of the executable when the PC is at the dyld
360 entry point using parameter passed by the kernel (at SP). */
363 darwin_read_exec_load_addr_at_init (struct darwin_info
*info
)
365 gdbarch
*gdbarch
= current_inferior ()->arch ();
366 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
367 int addr_size
= gdbarch_addr_bit (gdbarch
) / 8;
368 ULONGEST load_ptr_addr
;
373 if (regcache_cooked_read_unsigned (get_current_regcache (),
374 gdbarch_sp_regnum (gdbarch
),
375 &load_ptr_addr
) != REG_VALID
)
378 /* Read value at SP (image load address). */
379 if (target_read_memory (load_ptr_addr
, buf
, addr_size
))
382 load_addr
= extract_unsigned_integer (buf
, addr_size
, byte_order
);
384 return darwin_validate_exec_header (load_addr
);
387 /* Return 1 if PC lies in the dynamic symbol resolution code of the
391 darwin_in_dynsym_resolve_code (CORE_ADDR pc
)
396 /* A wrapper for bfd_mach_o_fat_extract that handles reference
397 counting properly. This will either return NULL, or return a new
398 reference to a BFD. */
400 static gdb_bfd_ref_ptr
401 gdb_bfd_mach_o_fat_extract (bfd
*abfd
, bfd_format format
,
402 const bfd_arch_info_type
*arch
)
404 bfd
*result
= bfd_mach_o_fat_extract (abfd
, format
, arch
);
410 gdb_bfd_ref (result
);
412 gdb_bfd_mark_parent (result
, abfd
);
414 return gdb_bfd_ref_ptr (result
);
417 /* Return the BFD for the program interpreter. */
419 static gdb_bfd_ref_ptr
420 darwin_get_dyld_bfd ()
424 /* This method doesn't work with an attached process. */
425 if (current_inferior ()->attach_flag
)
428 /* Find the program interpreter. */
429 interp_name
= find_program_interpreter ();
433 /* Create a bfd for the interpreter. */
434 gdb_bfd_ref_ptr
dyld_bfd (gdb_bfd_open (interp_name
, gnutarget
));
435 if (dyld_bfd
!= NULL
)
438 (gdb_bfd_mach_o_fat_extract
439 (dyld_bfd
.get (), bfd_object
,
440 gdbarch_bfd_arch_info (current_inferior ()->arch ())));
446 /* Extract dyld_all_image_addr when the process was just created, assuming the
447 current PC is at the entry of the dynamic linker. */
450 darwin_solib_get_all_image_info_addr_at_init (struct darwin_info
*info
)
452 CORE_ADDR load_addr
= 0;
453 gdb_bfd_ref_ptr dyld_bfd
= darwin_get_dyld_bfd ();
455 if (dyld_bfd
== NULL
)
458 /* We find the dynamic linker's base address by examining
459 the current pc (which should point at the entry point for the
460 dynamic linker) and subtracting the offset of the entry point. */
461 load_addr
= (regcache_read_pc (get_current_regcache ())
462 - bfd_get_start_address (dyld_bfd
.get ()));
464 /* Now try to set a breakpoint in the dynamic linker. */
465 info
->all_image_addr
=
466 lookup_symbol_from_bfd (dyld_bfd
.get (), "_dyld_all_image_infos");
468 if (info
->all_image_addr
== 0)
471 info
->all_image_addr
+= load_addr
;
474 /* Extract dyld_all_image_addr reading it from
475 TARGET_OBJECT_DARWIN_DYLD_INFO. */
478 darwin_solib_read_all_image_info_addr (struct darwin_info
*info
)
483 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr
;
486 if (ptr_type
->length () > sizeof (buf
))
489 len
= target_read (current_inferior ()->top_target (),
490 TARGET_OBJECT_DARWIN_DYLD_INFO
,
491 NULL
, buf
, 0, ptr_type
->length ());
495 /* The use of BIG endian is intended, as BUF is a raw stream of bytes. This
496 makes the support of remote protocol easier. */
497 info
->all_image_addr
= extract_unsigned_integer (buf
, len
, BFD_ENDIAN_BIG
);
500 /* Shared library startup support. See documentation in solib-svr4.c. */
503 darwin_solib_create_inferior_hook (int from_tty
)
505 /* Everything below only makes sense if we have a running inferior. */
506 if (!target_has_execution ())
509 struct darwin_info
*info
= get_darwin_info ();
512 info
->all_image_addr
= 0;
514 darwin_solib_read_all_image_info_addr (info
);
516 if (info
->all_image_addr
== 0)
517 darwin_solib_get_all_image_info_addr_at_init (info
);
519 if (info
->all_image_addr
== 0)
522 darwin_load_image_infos (info
);
524 if (!darwin_dyld_version_ok (info
))
526 warning (_("unhandled dyld version (%d)"), info
->all_image
.version
);
530 if (info
->all_image
.count
!= 0)
532 /* Possible relocate the main executable (PIE). */
533 load_addr
= darwin_read_exec_load_addr_from_dyld (info
);
538 Do not break on the notifier if dyld is not initialized (deduced from
539 count == 0). In that case, dyld hasn't relocated itself and the
540 notifier may point to a wrong address. */
542 load_addr
= darwin_read_exec_load_addr_at_init (info
);
545 if (load_addr
!= 0 && current_program_space
->symfile_object_file
!= NULL
)
549 /* Find the base address of the executable. */
550 vmaddr
= bfd_mach_o_get_base_address (current_program_space
->exec_bfd ());
553 if (vmaddr
!= load_addr
)
554 objfile_rebase (current_program_space
->symfile_object_file
,
558 /* Set solib notifier (to reload list of shared libraries). */
559 CORE_ADDR notifier
= info
->all_image
.notifier
;
561 if (info
->all_image
.count
== 0)
563 /* Dyld hasn't yet relocated itself, so the notifier address may
564 be incorrect (as it has to be relocated). */
566 = bfd_get_start_address (current_program_space
->exec_bfd ());
571 gdb_bfd_ref_ptr dyld_bfd
= darwin_get_dyld_bfd ();
572 if (dyld_bfd
!= NULL
)
574 CORE_ADDR dyld_bfd_start_address
;
575 CORE_ADDR dyld_relocated_base_address
;
578 dyld_bfd_start_address
= bfd_get_start_address (dyld_bfd
.get());
580 /* We find the dynamic linker's base address by examining
581 the current pc (which should point at the entry point
582 for the dynamic linker) and subtracting the offset of
585 pc
= regcache_read_pc (get_current_regcache ());
586 dyld_relocated_base_address
= pc
- dyld_bfd_start_address
;
588 /* We get the proper notifier relocated address by
589 adding the dyld relocated base address to the current
590 notifier offset value. */
592 notifier
+= dyld_relocated_base_address
;
597 /* Add the breakpoint which is hit by dyld when the list of solib is
600 create_solib_event_breakpoint (current_inferior ()->arch (), notifier
);
604 darwin_clear_solib (void)
606 struct darwin_info
*info
= get_darwin_info ();
608 info
->all_image_addr
= 0;
609 info
->all_image
.version
= 0;
613 darwin_free_so (struct so_list
*so
)
615 lm_info_darwin
*li
= (lm_info_darwin
*) so
->lm_info
;
620 /* The section table is built from bfd sections using bfd VMAs.
621 Relocate these VMAs according to solib info. */
624 darwin_relocate_section_addresses (struct so_list
*so
,
625 struct target_section
*sec
)
627 lm_info_darwin
*li
= (lm_info_darwin
*) so
->lm_info
;
629 sec
->addr
+= li
->lm_addr
;
630 sec
->endaddr
+= li
->lm_addr
;
632 /* Best effort to set addr_high/addr_low. This is used only by
633 'info sharedlibary'. */
634 if (so
->addr_high
== 0)
636 so
->addr_low
= sec
->addr
;
637 so
->addr_high
= sec
->endaddr
;
639 if (sec
->endaddr
> so
->addr_high
)
640 so
->addr_high
= sec
->endaddr
;
641 if (sec
->addr
< so
->addr_low
)
642 so
->addr_low
= sec
->addr
;
645 static gdb_bfd_ref_ptr
646 darwin_bfd_open (const char *pathname
)
650 /* Search for shared library file. */
651 gdb::unique_xmalloc_ptr
<char> found_pathname
652 = solib_find (pathname
, &found_file
);
653 if (found_pathname
== NULL
)
654 perror_with_name (pathname
);
656 /* Open bfd for shared library. */
657 gdb_bfd_ref_ptr
abfd (solib_bfd_fopen (found_pathname
.get (), found_file
));
660 (gdb_bfd_mach_o_fat_extract
661 (abfd
.get (), bfd_object
,
662 gdbarch_bfd_arch_info (current_inferior ()->arch ())));
664 error (_("`%s': not a shared-library: %s"),
665 bfd_get_filename (abfd
.get ()), bfd_errmsg (bfd_get_error ()));
667 /* The current filename for fat-binary BFDs is a name generated
668 by BFD, usually a string containing the name of the architecture.
669 Reset its value to the actual filename. */
670 bfd_set_filename (res
.get (), pathname
);
675 const struct target_so_ops darwin_so_ops
=
677 darwin_relocate_section_addresses
,
681 darwin_solib_create_inferior_hook
,
683 open_symbol_file_object
,
684 darwin_in_dynsym_resolve_code
,