Automatic date update in version.in
[binutils-gdb.git] / gdb / solib-darwin.c
blob6943b2df586588972f35e8a97e6a473f7f753d3b
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/>. */
21 #include "bfd.h"
22 #include "objfiles.h"
23 #include "gdbcore.h"
24 #include "target.h"
25 #include "inferior.h"
26 #include "regcache.h"
27 #include "gdb_bfd.h"
29 #include "solist.h"
30 #include "solib-darwin.h"
32 #include "mach-o.h"
33 #include "mach-o/external.h"
35 struct gdb_dyld_image_info
37 /* Base address (which corresponds to the Mach-O header). */
38 CORE_ADDR mach_header;
39 /* Image file path. */
40 CORE_ADDR file_path;
41 /* st.m_time of image file. */
42 unsigned long mtime;
45 /* Content of inferior dyld_all_image_infos structure.
46 See /usr/include/mach-o/dyld_images.h for the documentation. */
47 struct gdb_dyld_all_image_infos
49 /* Version (1). */
50 unsigned int version;
51 /* Number of images. */
52 unsigned int count;
53 /* Image description. */
54 CORE_ADDR info;
55 /* Notifier (function called when a library is added or removed). */
56 CORE_ADDR notifier;
59 /* Current all_image_infos version. */
60 #define DYLD_VERSION_MIN 1
61 #define DYLD_VERSION_MAX 15
63 /* Per PSPACE specific data. */
64 struct darwin_info
66 /* Address of structure dyld_all_image_infos in inferior. */
67 CORE_ADDR all_image_addr = 0;
69 /* Gdb copy of dyld_all_info_infos. */
70 struct gdb_dyld_all_image_infos all_image {};
73 /* Per-program-space data key. */
74 static const registry<program_space>::key<darwin_info>
75 solib_darwin_pspace_data;
77 /* Get the darwin solib data for PSPACE. If none is found yet, add it now. This
78 function always returns a valid object. */
80 static darwin_info *
81 get_darwin_info (program_space *pspace)
83 darwin_info *info = solib_darwin_pspace_data.get (pspace);
84 if (info != nullptr)
85 return info;
87 return solib_darwin_pspace_data.emplace (pspace);
90 /* Return non-zero if the version in dyld_all_image is known. */
92 static int
93 darwin_dyld_version_ok (const struct darwin_info *info)
95 return info->all_image.version >= DYLD_VERSION_MIN
96 && info->all_image.version <= DYLD_VERSION_MAX;
99 /* Read dyld_all_image from inferior. */
101 static void
102 darwin_load_image_infos (struct darwin_info *info)
104 gdb_byte buf[24];
105 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
106 type *ptr_type
107 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
108 int len;
110 /* If the structure address is not known, don't continue. */
111 if (info->all_image_addr == 0)
112 return;
114 /* The structure has 4 fields: version (4 bytes), count (4 bytes),
115 info (pointer) and notifier (pointer). */
116 len = 4 + 4 + 2 * ptr_type->length ();
117 gdb_assert (len <= sizeof (buf));
118 memset (&info->all_image, 0, sizeof (info->all_image));
120 /* Read structure raw bytes from target. */
121 if (target_read_memory (info->all_image_addr, buf, len))
122 return;
124 /* Extract the fields. */
125 info->all_image.version = extract_unsigned_integer (buf, 4, byte_order);
126 if (!darwin_dyld_version_ok (info))
127 return;
129 info->all_image.count = extract_unsigned_integer (buf + 4, 4, byte_order);
130 info->all_image.info = extract_typed_address (buf + 8, ptr_type);
131 info->all_image.notifier = extract_typed_address
132 (buf + 8 + ptr_type->length (), ptr_type);
135 /* Link map info to include in an allocated so_list entry. */
137 struct lm_info_darwin final : public lm_info
139 /* The target location of lm. */
140 CORE_ADDR lm_addr = 0;
143 /* Lookup the value for a specific symbol. */
145 static CORE_ADDR
146 lookup_symbol_from_bfd (bfd *abfd, const char *symname)
148 long storage_needed;
149 asymbol **symbol_table;
150 unsigned int number_of_symbols;
151 unsigned int i;
152 CORE_ADDR symaddr = 0;
154 storage_needed = bfd_get_symtab_upper_bound (abfd);
156 if (storage_needed <= 0)
157 return 0;
159 symbol_table = (asymbol **) xmalloc (storage_needed);
160 number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
162 for (i = 0; i < number_of_symbols; i++)
164 asymbol *sym = symbol_table[i];
166 if (strcmp (sym->name, symname) == 0
167 && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0)
169 /* BFD symbols are section relative. */
170 symaddr = sym->value + sym->section->vma;
171 break;
174 xfree (symbol_table);
176 return symaddr;
179 /* Return program interpreter string. */
181 static char *
182 find_program_interpreter (void)
184 char *buf = NULL;
186 /* If we have an current exec_bfd, get the interpreter from the load
187 commands. */
188 if (current_program_space->exec_bfd ())
190 bfd_mach_o_load_command *cmd;
192 if (bfd_mach_o_lookup_command (current_program_space->exec_bfd (),
193 BFD_MACH_O_LC_LOAD_DYLINKER, &cmd) == 1)
194 return cmd->command.dylinker.name_str;
197 /* If we didn't find it, read from memory.
198 FIXME: todo. */
199 return buf;
202 /* Not used. I don't see how the main symbol file can be found: the
203 interpreter name is needed and it is known from the executable file.
204 Note that darwin-nat.c implements pid_to_exec_file. */
206 static int
207 open_symbol_file_object (int from_tty)
209 return 0;
212 /* Build a list of currently loaded shared objects. See solib-svr4.c. */
214 static intrusive_list<solib>
215 darwin_current_sos ()
217 type *ptr_type
218 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
219 enum bfd_endian byte_order = type_byte_order (ptr_type);
220 int ptr_len = ptr_type->length ();
221 unsigned int image_info_size;
222 darwin_info *info = get_darwin_info (current_program_space);
224 /* Be sure image infos are loaded. */
225 darwin_load_image_infos (info);
227 if (!darwin_dyld_version_ok (info))
228 return {};
230 image_info_size = ptr_len * 3;
232 intrusive_list<solib> sos;
234 /* Read infos for each solib.
235 The first entry was rumored to be the executable itself, but this is not
236 true when a large number of shared libraries are used (table expanded ?).
237 We now check all entries, but discard executable images. */
238 for (int i = 0; i < info->all_image.count; i++)
240 CORE_ADDR iinfo = info->all_image.info + i * image_info_size;
241 gdb_byte buf[image_info_size];
242 CORE_ADDR load_addr;
243 CORE_ADDR path_addr;
244 struct mach_o_header_external hdr;
245 unsigned long hdr_val;
247 /* Read image info from inferior. */
248 if (target_read_memory (iinfo, buf, image_info_size))
249 break;
251 load_addr = extract_typed_address (buf, ptr_type);
252 path_addr = extract_typed_address (buf + ptr_len, ptr_type);
254 /* Read Mach-O header from memory. */
255 if (target_read_memory (load_addr, (gdb_byte *) &hdr, sizeof (hdr) - 4))
256 break;
257 /* Discard wrong magic numbers. Shouldn't happen. */
258 hdr_val = extract_unsigned_integer
259 (hdr.magic, sizeof (hdr.magic), byte_order);
260 if (hdr_val != BFD_MACH_O_MH_MAGIC && hdr_val != BFD_MACH_O_MH_MAGIC_64)
261 continue;
262 /* Discard executable. Should happen only once. */
263 hdr_val = extract_unsigned_integer
264 (hdr.filetype, sizeof (hdr.filetype), byte_order);
265 if (hdr_val == BFD_MACH_O_MH_EXECUTE)
266 continue;
268 gdb::unique_xmalloc_ptr<char> file_path
269 = target_read_string (path_addr, SO_NAME_MAX_PATH_SIZE - 1);
270 if (file_path == nullptr)
271 break;
273 /* Create and fill the new struct solib element. */
274 solib *newobj = new solib;
276 auto li = std::make_unique<lm_info_darwin> ();
278 newobj->so_name = file_path.get ();
279 newobj->so_original_name = newobj->so_name;
280 li->lm_addr = load_addr;
282 newobj->lm_info = std::move (li);
283 sos.push_back (*newobj);
286 return sos;
289 /* Check LOAD_ADDR points to a Mach-O executable header. Return LOAD_ADDR
290 in case of success, 0 in case of failure. */
292 static CORE_ADDR
293 darwin_validate_exec_header (CORE_ADDR load_addr)
295 bfd_endian byte_order = gdbarch_byte_order (current_inferior ()->arch ());
296 struct mach_o_header_external hdr;
297 unsigned long hdr_val;
299 /* Read Mach-O header from memory. */
300 if (target_read_memory (load_addr, (gdb_byte *) &hdr, sizeof (hdr) - 4))
301 return 0;
303 /* Discard wrong magic numbers. Shouldn't happen. */
304 hdr_val = extract_unsigned_integer
305 (hdr.magic, sizeof (hdr.magic), byte_order);
306 if (hdr_val != BFD_MACH_O_MH_MAGIC && hdr_val != BFD_MACH_O_MH_MAGIC_64)
307 return 0;
309 /* Check executable. */
310 hdr_val = extract_unsigned_integer
311 (hdr.filetype, sizeof (hdr.filetype), byte_order);
312 if (hdr_val == BFD_MACH_O_MH_EXECUTE)
313 return load_addr;
315 return 0;
318 /* Get the load address of the executable using dyld list of images.
319 We assume that the dyld info are correct (which is wrong if the target
320 is stopped at the first instruction). */
322 static CORE_ADDR
323 darwin_read_exec_load_addr_from_dyld (struct darwin_info *info)
325 type *ptr_type
326 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
327 int ptr_len = ptr_type->length ();
328 unsigned int image_info_size = ptr_len * 3;
329 int i;
331 /* Read infos for each solib. One of them should be the executable. */
332 for (i = 0; i < info->all_image.count; i++)
334 CORE_ADDR iinfo = info->all_image.info + i * image_info_size;
335 gdb_byte buf[image_info_size];
336 CORE_ADDR load_addr;
338 /* Read image info from inferior. */
339 if (target_read_memory (iinfo, buf, image_info_size))
340 break;
342 load_addr = extract_typed_address (buf, ptr_type);
343 if (darwin_validate_exec_header (load_addr) == load_addr)
344 return load_addr;
347 return 0;
350 /* Get the load address of the executable when the PC is at the dyld
351 entry point using parameter passed by the kernel (at SP). */
353 static CORE_ADDR
354 darwin_read_exec_load_addr_at_init (struct darwin_info *info)
356 gdbarch *gdbarch = current_inferior ()->arch ();
357 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
358 int addr_size = gdbarch_addr_bit (gdbarch) / 8;
359 ULONGEST load_ptr_addr;
360 ULONGEST load_addr;
361 gdb_byte buf[8];
363 /* Get SP. */
364 if (regcache_cooked_read_unsigned (get_thread_regcache (inferior_thread ()),
365 gdbarch_sp_regnum (gdbarch),
366 &load_ptr_addr) != REG_VALID)
367 return 0;
369 /* Read value at SP (image load address). */
370 if (target_read_memory (load_ptr_addr, buf, addr_size))
371 return 0;
373 load_addr = extract_unsigned_integer (buf, addr_size, byte_order);
375 return darwin_validate_exec_header (load_addr);
378 /* Return 1 if PC lies in the dynamic symbol resolution code of the
379 run time loader. */
381 static int
382 darwin_in_dynsym_resolve_code (CORE_ADDR pc)
384 return 0;
387 /* A wrapper for bfd_mach_o_fat_extract that handles reference
388 counting properly. This will either return NULL, or return a new
389 reference to a BFD. */
391 static gdb_bfd_ref_ptr
392 gdb_bfd_mach_o_fat_extract (bfd *abfd, bfd_format format,
393 const bfd_arch_info_type *arch)
395 bfd *result = bfd_mach_o_fat_extract (abfd, format, arch);
397 if (result == NULL)
398 return NULL;
400 if (result == abfd)
401 gdb_bfd_ref (result);
402 else
403 gdb_bfd_mark_parent (result, abfd);
405 return gdb_bfd_ref_ptr (result);
408 /* Return the BFD for the program interpreter. */
410 static gdb_bfd_ref_ptr
411 darwin_get_dyld_bfd ()
413 char *interp_name;
415 /* This method doesn't work with an attached process. */
416 if (current_inferior ()->attach_flag)
417 return NULL;
419 /* Find the program interpreter. */
420 interp_name = find_program_interpreter ();
421 if (!interp_name)
422 return NULL;
424 /* Create a bfd for the interpreter. */
425 gdb_bfd_ref_ptr dyld_bfd (gdb_bfd_open (interp_name, gnutarget));
426 if (dyld_bfd != NULL)
428 gdb_bfd_ref_ptr sub
429 (gdb_bfd_mach_o_fat_extract
430 (dyld_bfd.get (), bfd_object,
431 gdbarch_bfd_arch_info (current_inferior ()->arch ())));
432 dyld_bfd = sub;
434 return dyld_bfd;
437 /* Extract dyld_all_image_addr when the process was just created, assuming the
438 current PC is at the entry of the dynamic linker. */
440 static void
441 darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
443 CORE_ADDR load_addr = 0;
444 gdb_bfd_ref_ptr dyld_bfd = darwin_get_dyld_bfd ();
446 if (dyld_bfd == NULL)
447 return;
449 /* We find the dynamic linker's base address by examining
450 the current pc (which should point at the entry point for the
451 dynamic linker) and subtracting the offset of the entry point. */
452 load_addr = (regcache_read_pc (get_thread_regcache (inferior_thread ()))
453 - bfd_get_start_address (dyld_bfd.get ()));
455 /* Now try to set a breakpoint in the dynamic linker. */
456 info->all_image_addr =
457 lookup_symbol_from_bfd (dyld_bfd.get (), "_dyld_all_image_infos");
459 if (info->all_image_addr == 0)
460 return;
462 info->all_image_addr += load_addr;
465 /* Extract dyld_all_image_addr reading it from
466 TARGET_OBJECT_DARWIN_DYLD_INFO. */
468 static void
469 darwin_solib_read_all_image_info_addr (struct darwin_info *info)
471 gdb_byte buf[8];
472 LONGEST len;
473 type *ptr_type
474 = builtin_type (current_inferior ()->arch ())->builtin_data_ptr;
476 /* Sanity check. */
477 if (ptr_type->length () > sizeof (buf))
478 return;
480 len = target_read (current_inferior ()->top_target (),
481 TARGET_OBJECT_DARWIN_DYLD_INFO,
482 NULL, buf, 0, ptr_type->length ());
483 if (len <= 0)
484 return;
486 /* The use of BIG endian is intended, as BUF is a raw stream of bytes. This
487 makes the support of remote protocol easier. */
488 info->all_image_addr = extract_unsigned_integer (buf, len, BFD_ENDIAN_BIG);
491 /* Shared library startup support. See documentation in solib-svr4.c. */
493 static void
494 darwin_solib_create_inferior_hook (int from_tty)
496 /* Everything below only makes sense if we have a running inferior. */
497 if (!target_has_execution ())
498 return;
500 darwin_info *info = get_darwin_info (current_program_space);
501 CORE_ADDR load_addr;
503 info->all_image_addr = 0;
505 darwin_solib_read_all_image_info_addr (info);
507 if (info->all_image_addr == 0)
508 darwin_solib_get_all_image_info_addr_at_init (info);
510 if (info->all_image_addr == 0)
511 return;
513 darwin_load_image_infos (info);
515 if (!darwin_dyld_version_ok (info))
517 warning (_("unhandled dyld version (%d)"), info->all_image.version);
518 return;
521 if (info->all_image.count != 0)
523 /* Possible relocate the main executable (PIE). */
524 load_addr = darwin_read_exec_load_addr_from_dyld (info);
526 else
528 /* Possible issue:
529 Do not break on the notifier if dyld is not initialized (deduced from
530 count == 0). In that case, dyld hasn't relocated itself and the
531 notifier may point to a wrong address. */
533 load_addr = darwin_read_exec_load_addr_at_init (info);
536 if (load_addr != 0 && current_program_space->symfile_object_file != NULL)
538 CORE_ADDR vmaddr;
540 /* Find the base address of the executable. */
541 vmaddr = bfd_mach_o_get_base_address (current_program_space->exec_bfd ());
543 /* Relocate. */
544 if (vmaddr != load_addr)
545 objfile_rebase (current_program_space->symfile_object_file,
546 load_addr - vmaddr);
549 /* Set solib notifier (to reload list of shared libraries). */
550 CORE_ADDR notifier = info->all_image.notifier;
552 if (info->all_image.count == 0)
554 /* Dyld hasn't yet relocated itself, so the notifier address may
555 be incorrect (as it has to be relocated). */
556 CORE_ADDR start
557 = bfd_get_start_address (current_program_space->exec_bfd ());
558 if (start == 0)
559 notifier = 0;
560 else
562 gdb_bfd_ref_ptr dyld_bfd = darwin_get_dyld_bfd ();
563 if (dyld_bfd != NULL)
565 CORE_ADDR dyld_bfd_start_address;
566 CORE_ADDR dyld_relocated_base_address;
567 CORE_ADDR pc;
569 dyld_bfd_start_address = bfd_get_start_address (dyld_bfd.get());
571 /* We find the dynamic linker's base address by examining
572 the current pc (which should point at the entry point
573 for the dynamic linker) and subtracting the offset of
574 the entry point. */
576 pc = regcache_read_pc (get_thread_regcache (inferior_thread ()));
577 dyld_relocated_base_address = pc - dyld_bfd_start_address;
579 /* We get the proper notifier relocated address by
580 adding the dyld relocated base address to the current
581 notifier offset value. */
583 notifier += dyld_relocated_base_address;
588 /* Add the breakpoint which is hit by dyld when the list of solib is
589 modified. */
590 if (notifier != 0)
591 create_solib_event_breakpoint (current_inferior ()->arch (), notifier);
594 static void
595 darwin_clear_solib (program_space *pspace)
597 darwin_info *info = get_darwin_info (pspace);
599 info->all_image_addr = 0;
600 info->all_image.version = 0;
603 /* The section table is built from bfd sections using bfd VMAs.
604 Relocate these VMAs according to solib info. */
606 static void
607 darwin_relocate_section_addresses (solib &so, target_section *sec)
609 auto *li = gdb::checked_static_cast<lm_info_darwin *> (so.lm_info.get ());
611 sec->addr += li->lm_addr;
612 sec->endaddr += li->lm_addr;
614 /* Best effort to set addr_high/addr_low. This is used only by
615 'info sharedlibary'. */
616 if (so.addr_high == 0)
618 so.addr_low = sec->addr;
619 so.addr_high = sec->endaddr;
621 if (sec->endaddr > so.addr_high)
622 so.addr_high = sec->endaddr;
623 if (sec->addr < so.addr_low)
624 so.addr_low = sec->addr;
627 static gdb_bfd_ref_ptr
628 darwin_bfd_open (const char *pathname)
630 int found_file;
632 /* Search for shared library file. */
633 gdb::unique_xmalloc_ptr<char> found_pathname
634 = solib_find (pathname, &found_file);
635 if (found_pathname == NULL)
636 perror_with_name (pathname);
638 /* Open bfd for shared library. */
639 gdb_bfd_ref_ptr abfd (solib_bfd_fopen (found_pathname.get (), found_file));
641 gdb_bfd_ref_ptr res
642 (gdb_bfd_mach_o_fat_extract
643 (abfd.get (), bfd_object,
644 gdbarch_bfd_arch_info (current_inferior ()->arch ())));
645 if (res == NULL)
646 error (_("`%s': not a shared-library: %s"),
647 bfd_get_filename (abfd.get ()), bfd_errmsg (bfd_get_error ()));
649 /* The current filename for fat-binary BFDs is a name generated
650 by BFD, usually a string containing the name of the architecture.
651 Reset its value to the actual filename. */
652 bfd_set_filename (res.get (), pathname);
654 return res;
657 const solib_ops darwin_so_ops =
659 darwin_relocate_section_addresses,
660 nullptr,
661 darwin_clear_solib,
662 darwin_solib_create_inferior_hook,
663 darwin_current_sos,
664 open_symbol_file_object,
665 darwin_in_dynsym_resolve_code,
666 darwin_bfd_open,