1 /* Handle Darwin shared libraries for GDB, the GNU Debugger.
3 Copyright (C) 2009-2024 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/>. */
22 #include "extract-store-integer.h"
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 darwin solib data for PSPACE. If none is found yet, add it now. This
79 function always returns a valid object. */
82 get_darwin_info (program_space
*pspace
)
84 darwin_info
*info
= solib_darwin_pspace_data
.get (pspace
);
88 return solib_darwin_pspace_data
.emplace (pspace
);
91 /* Return non-zero if the version in dyld_all_image is known. */
94 darwin_dyld_version_ok (const struct darwin_info
*info
)
96 return info
->all_image
.version
>= DYLD_VERSION_MIN
97 && info
->all_image
.version
<= DYLD_VERSION_MAX
;
100 /* Read dyld_all_image from inferior. */
103 darwin_load_image_infos (struct darwin_info
*info
)
106 bfd_endian byte_order
= gdbarch_byte_order (current_inferior ()->arch ());
108 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr
;
111 /* If the structure address is not known, don't continue. */
112 if (info
->all_image_addr
== 0)
115 /* The structure has 4 fields: version (4 bytes), count (4 bytes),
116 info (pointer) and notifier (pointer). */
117 len
= 4 + 4 + 2 * ptr_type
->length ();
118 gdb_assert (len
<= sizeof (buf
));
119 memset (&info
->all_image
, 0, sizeof (info
->all_image
));
121 /* Read structure raw bytes from target. */
122 if (target_read_memory (info
->all_image_addr
, buf
, len
))
125 /* Extract the fields. */
126 info
->all_image
.version
= extract_unsigned_integer (buf
, 4, byte_order
);
127 if (!darwin_dyld_version_ok (info
))
130 info
->all_image
.count
= extract_unsigned_integer (buf
+ 4, 4, byte_order
);
131 info
->all_image
.info
= extract_typed_address (buf
+ 8, ptr_type
);
132 info
->all_image
.notifier
= extract_typed_address
133 (buf
+ 8 + ptr_type
->length (), ptr_type
);
136 /* Link map info to include in an allocated so_list entry. */
138 struct lm_info_darwin final
: public lm_info
140 /* The target location of lm. */
141 CORE_ADDR lm_addr
= 0;
144 /* Lookup the value for a specific symbol. */
147 lookup_symbol_from_bfd (bfd
*abfd
, const char *symname
)
150 asymbol
**symbol_table
;
151 unsigned int number_of_symbols
;
153 CORE_ADDR symaddr
= 0;
155 storage_needed
= bfd_get_symtab_upper_bound (abfd
);
157 if (storage_needed
<= 0)
160 symbol_table
= (asymbol
**) xmalloc (storage_needed
);
161 number_of_symbols
= bfd_canonicalize_symtab (abfd
, symbol_table
);
163 for (i
= 0; i
< number_of_symbols
; i
++)
165 asymbol
*sym
= symbol_table
[i
];
167 if (strcmp (sym
->name
, symname
) == 0
168 && (sym
->section
->flags
& (SEC_CODE
| SEC_DATA
)) != 0)
170 /* BFD symbols are section relative. */
171 symaddr
= sym
->value
+ sym
->section
->vma
;
175 xfree (symbol_table
);
180 /* Return program interpreter string. */
183 find_program_interpreter (void)
187 /* If we have an current exec_bfd, get the interpreter from the load
189 if (current_program_space
->exec_bfd ())
191 bfd_mach_o_load_command
*cmd
;
193 if (bfd_mach_o_lookup_command (current_program_space
->exec_bfd (),
194 BFD_MACH_O_LC_LOAD_DYLINKER
, &cmd
) == 1)
195 return cmd
->command
.dylinker
.name_str
;
198 /* If we didn't find it, read from memory.
203 /* Not used. I don't see how the main symbol file can be found: the
204 interpreter name is needed and it is known from the executable file.
205 Note that darwin-nat.c implements pid_to_exec_file. */
208 open_symbol_file_object (int from_tty
)
213 /* Build a list of currently loaded shared objects. See solib-svr4.c. */
215 static intrusive_list
<solib
>
216 darwin_current_sos ()
219 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr
;
220 enum bfd_endian byte_order
= type_byte_order (ptr_type
);
221 int ptr_len
= ptr_type
->length ();
222 unsigned int image_info_size
;
223 darwin_info
*info
= get_darwin_info (current_program_space
);
225 /* Be sure image infos are loaded. */
226 darwin_load_image_infos (info
);
228 if (!darwin_dyld_version_ok (info
))
231 image_info_size
= ptr_len
* 3;
233 intrusive_list
<solib
> sos
;
235 /* Read infos for each solib.
236 The first entry was rumored to be the executable itself, but this is not
237 true when a large number of shared libraries are used (table expanded ?).
238 We now check all entries, but discard executable images. */
239 for (int i
= 0; i
< info
->all_image
.count
; i
++)
241 CORE_ADDR iinfo
= info
->all_image
.info
+ i
* image_info_size
;
242 gdb_byte buf
[image_info_size
];
245 struct mach_o_header_external hdr
;
246 unsigned long hdr_val
;
248 /* Read image info from inferior. */
249 if (target_read_memory (iinfo
, buf
, image_info_size
))
252 load_addr
= extract_typed_address (buf
, ptr_type
);
253 path_addr
= extract_typed_address (buf
+ ptr_len
, ptr_type
);
255 /* Read Mach-O header from memory. */
256 if (target_read_memory (load_addr
, (gdb_byte
*) &hdr
, sizeof (hdr
) - 4))
258 /* Discard wrong magic numbers. Shouldn't happen. */
259 hdr_val
= extract_unsigned_integer
260 (hdr
.magic
, sizeof (hdr
.magic
), byte_order
);
261 if (hdr_val
!= BFD_MACH_O_MH_MAGIC
&& hdr_val
!= BFD_MACH_O_MH_MAGIC_64
)
263 /* Discard executable. Should happen only once. */
264 hdr_val
= extract_unsigned_integer
265 (hdr
.filetype
, sizeof (hdr
.filetype
), byte_order
);
266 if (hdr_val
== BFD_MACH_O_MH_EXECUTE
)
269 gdb::unique_xmalloc_ptr
<char> file_path
270 = target_read_string (path_addr
, SO_NAME_MAX_PATH_SIZE
- 1);
271 if (file_path
== nullptr)
274 /* Create and fill the new struct solib element. */
275 solib
*newobj
= new solib
;
277 auto li
= std::make_unique
<lm_info_darwin
> ();
279 newobj
->so_name
= file_path
.get ();
280 newobj
->so_original_name
= newobj
->so_name
;
281 li
->lm_addr
= load_addr
;
283 newobj
->lm_info
= std::move (li
);
284 sos
.push_back (*newobj
);
290 /* Check LOAD_ADDR points to a Mach-O executable header. Return LOAD_ADDR
291 in case of success, 0 in case of failure. */
294 darwin_validate_exec_header (CORE_ADDR load_addr
)
296 bfd_endian byte_order
= gdbarch_byte_order (current_inferior ()->arch ());
297 struct mach_o_header_external hdr
;
298 unsigned long hdr_val
;
300 /* Read Mach-O header from memory. */
301 if (target_read_memory (load_addr
, (gdb_byte
*) &hdr
, sizeof (hdr
) - 4))
304 /* Discard wrong magic numbers. Shouldn't happen. */
305 hdr_val
= extract_unsigned_integer
306 (hdr
.magic
, sizeof (hdr
.magic
), byte_order
);
307 if (hdr_val
!= BFD_MACH_O_MH_MAGIC
&& hdr_val
!= BFD_MACH_O_MH_MAGIC_64
)
310 /* Check executable. */
311 hdr_val
= extract_unsigned_integer
312 (hdr
.filetype
, sizeof (hdr
.filetype
), byte_order
);
313 if (hdr_val
== BFD_MACH_O_MH_EXECUTE
)
319 /* Get the load address of the executable using dyld list of images.
320 We assume that the dyld info are correct (which is wrong if the target
321 is stopped at the first instruction). */
324 darwin_read_exec_load_addr_from_dyld (struct darwin_info
*info
)
327 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr
;
328 int ptr_len
= ptr_type
->length ();
329 unsigned int image_info_size
= ptr_len
* 3;
332 /* Read infos for each solib. One of them should be the executable. */
333 for (i
= 0; i
< info
->all_image
.count
; i
++)
335 CORE_ADDR iinfo
= info
->all_image
.info
+ i
* image_info_size
;
336 gdb_byte buf
[image_info_size
];
339 /* Read image info from inferior. */
340 if (target_read_memory (iinfo
, buf
, image_info_size
))
343 load_addr
= extract_typed_address (buf
, ptr_type
);
344 if (darwin_validate_exec_header (load_addr
) == load_addr
)
351 /* Get the load address of the executable when the PC is at the dyld
352 entry point using parameter passed by the kernel (at SP). */
355 darwin_read_exec_load_addr_at_init (struct darwin_info
*info
)
357 gdbarch
*gdbarch
= current_inferior ()->arch ();
358 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
359 int addr_size
= gdbarch_addr_bit (gdbarch
) / 8;
360 ULONGEST load_ptr_addr
;
365 if (regcache_cooked_read_unsigned (get_thread_regcache (inferior_thread ()),
366 gdbarch_sp_regnum (gdbarch
),
367 &load_ptr_addr
) != REG_VALID
)
370 /* Read value at SP (image load address). */
371 if (target_read_memory (load_ptr_addr
, buf
, addr_size
))
374 load_addr
= extract_unsigned_integer (buf
, addr_size
, byte_order
);
376 return darwin_validate_exec_header (load_addr
);
379 /* Return 1 if PC lies in the dynamic symbol resolution code of the
383 darwin_in_dynsym_resolve_code (CORE_ADDR pc
)
388 /* A wrapper for bfd_mach_o_fat_extract that handles reference
389 counting properly. This will either return NULL, or return a new
390 reference to a BFD. */
392 static gdb_bfd_ref_ptr
393 gdb_bfd_mach_o_fat_extract (bfd
*abfd
, bfd_format format
,
394 const bfd_arch_info_type
*arch
)
396 bfd
*result
= bfd_mach_o_fat_extract (abfd
, format
, arch
);
402 gdb_bfd_ref (result
);
404 gdb_bfd_mark_parent (result
, abfd
);
406 return gdb_bfd_ref_ptr (result
);
409 /* Return the BFD for the program interpreter. */
411 static gdb_bfd_ref_ptr
412 darwin_get_dyld_bfd ()
416 /* This method doesn't work with an attached process. */
417 if (current_inferior ()->attach_flag
)
420 /* Find the program interpreter. */
421 interp_name
= find_program_interpreter ();
425 /* Create a bfd for the interpreter. */
426 gdb_bfd_ref_ptr
dyld_bfd (gdb_bfd_open (interp_name
, gnutarget
));
427 if (dyld_bfd
!= NULL
)
430 (gdb_bfd_mach_o_fat_extract
431 (dyld_bfd
.get (), bfd_object
,
432 gdbarch_bfd_arch_info (current_inferior ()->arch ())));
438 /* Extract dyld_all_image_addr when the process was just created, assuming the
439 current PC is at the entry of the dynamic linker. */
442 darwin_solib_get_all_image_info_addr_at_init (struct darwin_info
*info
)
444 CORE_ADDR load_addr
= 0;
445 gdb_bfd_ref_ptr dyld_bfd
= darwin_get_dyld_bfd ();
447 if (dyld_bfd
== NULL
)
450 /* We find the dynamic linker's base address by examining
451 the current pc (which should point at the entry point for the
452 dynamic linker) and subtracting the offset of the entry point. */
453 load_addr
= (regcache_read_pc (get_thread_regcache (inferior_thread ()))
454 - bfd_get_start_address (dyld_bfd
.get ()));
456 /* Now try to set a breakpoint in the dynamic linker. */
457 info
->all_image_addr
=
458 lookup_symbol_from_bfd (dyld_bfd
.get (), "_dyld_all_image_infos");
460 if (info
->all_image_addr
== 0)
463 info
->all_image_addr
+= load_addr
;
466 /* Extract dyld_all_image_addr reading it from
467 TARGET_OBJECT_DARWIN_DYLD_INFO. */
470 darwin_solib_read_all_image_info_addr (struct darwin_info
*info
)
475 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr
;
478 if (ptr_type
->length () > sizeof (buf
))
481 len
= target_read (current_inferior ()->top_target (),
482 TARGET_OBJECT_DARWIN_DYLD_INFO
,
483 NULL
, buf
, 0, ptr_type
->length ());
487 /* The use of BIG endian is intended, as BUF is a raw stream of bytes. This
488 makes the support of remote protocol easier. */
489 info
->all_image_addr
= extract_unsigned_integer (buf
, len
, BFD_ENDIAN_BIG
);
492 /* Shared library startup support. See documentation in solib-svr4.c. */
495 darwin_solib_create_inferior_hook (int from_tty
)
497 /* Everything below only makes sense if we have a running inferior. */
498 if (!target_has_execution ())
501 darwin_info
*info
= get_darwin_info (current_program_space
);
504 info
->all_image_addr
= 0;
506 darwin_solib_read_all_image_info_addr (info
);
508 if (info
->all_image_addr
== 0)
509 darwin_solib_get_all_image_info_addr_at_init (info
);
511 if (info
->all_image_addr
== 0)
514 darwin_load_image_infos (info
);
516 if (!darwin_dyld_version_ok (info
))
518 warning (_("unhandled dyld version (%d)"), info
->all_image
.version
);
522 if (info
->all_image
.count
!= 0)
524 /* Possible relocate the main executable (PIE). */
525 load_addr
= darwin_read_exec_load_addr_from_dyld (info
);
530 Do not break on the notifier if dyld is not initialized (deduced from
531 count == 0). In that case, dyld hasn't relocated itself and the
532 notifier may point to a wrong address. */
534 load_addr
= darwin_read_exec_load_addr_at_init (info
);
537 if (load_addr
!= 0 && current_program_space
->symfile_object_file
!= NULL
)
541 /* Find the base address of the executable. */
542 vmaddr
= bfd_mach_o_get_base_address (current_program_space
->exec_bfd ());
545 if (vmaddr
!= load_addr
)
546 objfile_rebase (current_program_space
->symfile_object_file
,
550 /* Set solib notifier (to reload list of shared libraries). */
551 CORE_ADDR notifier
= info
->all_image
.notifier
;
553 if (info
->all_image
.count
== 0)
555 /* Dyld hasn't yet relocated itself, so the notifier address may
556 be incorrect (as it has to be relocated). */
558 = bfd_get_start_address (current_program_space
->exec_bfd ());
563 gdb_bfd_ref_ptr dyld_bfd
= darwin_get_dyld_bfd ();
564 if (dyld_bfd
!= NULL
)
566 CORE_ADDR dyld_bfd_start_address
;
567 CORE_ADDR dyld_relocated_base_address
;
570 dyld_bfd_start_address
= bfd_get_start_address (dyld_bfd
.get());
572 /* We find the dynamic linker's base address by examining
573 the current pc (which should point at the entry point
574 for the dynamic linker) and subtracting the offset of
577 pc
= regcache_read_pc (get_thread_regcache (inferior_thread ()));
578 dyld_relocated_base_address
= pc
- dyld_bfd_start_address
;
580 /* We get the proper notifier relocated address by
581 adding the dyld relocated base address to the current
582 notifier offset value. */
584 notifier
+= dyld_relocated_base_address
;
589 /* Add the breakpoint which is hit by dyld when the list of solib is
592 create_solib_event_breakpoint (current_inferior ()->arch (), notifier
);
596 darwin_clear_solib (program_space
*pspace
)
598 darwin_info
*info
= get_darwin_info (pspace
);
600 info
->all_image_addr
= 0;
601 info
->all_image
.version
= 0;
604 /* The section table is built from bfd sections using bfd VMAs.
605 Relocate these VMAs according to solib info. */
608 darwin_relocate_section_addresses (solib
&so
, target_section
*sec
)
610 auto *li
= gdb::checked_static_cast
<lm_info_darwin
*> (so
.lm_info
.get ());
612 sec
->addr
+= li
->lm_addr
;
613 sec
->endaddr
+= li
->lm_addr
;
615 /* Best effort to set addr_high/addr_low. This is used only by
616 'info sharedlibary'. */
617 if (so
.addr_high
== 0)
619 so
.addr_low
= sec
->addr
;
620 so
.addr_high
= sec
->endaddr
;
622 if (sec
->endaddr
> so
.addr_high
)
623 so
.addr_high
= sec
->endaddr
;
624 if (sec
->addr
< so
.addr_low
)
625 so
.addr_low
= sec
->addr
;
628 static gdb_bfd_ref_ptr
629 darwin_bfd_open (const char *pathname
)
633 /* Search for shared library file. */
634 gdb::unique_xmalloc_ptr
<char> found_pathname
635 = solib_find (pathname
, &found_file
);
636 if (found_pathname
== NULL
)
637 perror_with_name (pathname
);
639 /* Open bfd for shared library. */
640 gdb_bfd_ref_ptr
abfd (solib_bfd_fopen (found_pathname
.get (), found_file
));
643 (gdb_bfd_mach_o_fat_extract
644 (abfd
.get (), bfd_object
,
645 gdbarch_bfd_arch_info (current_inferior ()->arch ())));
647 error (_("`%s': not a shared-library: %s"),
648 bfd_get_filename (abfd
.get ()), bfd_errmsg (bfd_get_error ()));
650 /* The current filename for fat-binary BFDs is a name generated
651 by BFD, usually a string containing the name of the architecture.
652 Reset its value to the actual filename. */
653 bfd_set_filename (res
.get (), pathname
);
658 const solib_ops darwin_so_ops
=
660 darwin_relocate_section_addresses
,
663 darwin_solib_create_inferior_hook
,
665 open_symbol_file_object
,
666 darwin_in_dynsym_resolve_code
,