packaging: Add python3-base dependency
[platform/upstream/gdb.git] / gdb / solib-darwin.c
1 /* Handle Darwin shared libraries for GDB, the GNU Debugger.
2
3    Copyright (C) 2009-2023 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
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.
11
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.
16
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/>.  */
19
20 #include "defs.h"
21
22 #include "symtab.h"
23 #include "bfd.h"
24 #include "symfile.h"
25 #include "objfiles.h"
26 #include "gdbcore.h"
27 #include "target.h"
28 #include "inferior.h"
29 #include "regcache.h"
30 #include "gdbthread.h"
31 #include "gdb_bfd.h"
32
33 #include "solist.h"
34 #include "solib.h"
35 #include "solib-svr4.h"
36 #include "solib-darwin.h"
37
38 #include "bfd-target.h"
39 #include "elf-bfd.h"
40 #include "exec.h"
41 #include "auxv.h"
42 #include "mach-o.h"
43 #include "mach-o/external.h"
44
45 struct gdb_dyld_image_info
46 {
47   /* Base address (which corresponds to the Mach-O header).  */
48   CORE_ADDR mach_header;
49   /* Image file path.  */
50   CORE_ADDR file_path;
51   /* st.m_time of image file.  */
52   unsigned long mtime;
53 };
54
55 /* Content of inferior dyld_all_image_infos structure.
56    See /usr/include/mach-o/dyld_images.h for the documentation.  */
57 struct gdb_dyld_all_image_infos
58 {
59   /* Version (1).  */
60   unsigned int version;
61   /* Number of images.  */
62   unsigned int count;
63   /* Image description.  */
64   CORE_ADDR info;
65   /* Notifier (function called when a library is added or removed).  */
66   CORE_ADDR notifier;
67 };
68
69 /* Current all_image_infos version.  */
70 #define DYLD_VERSION_MIN 1
71 #define DYLD_VERSION_MAX 15
72
73 /* Per PSPACE specific data.  */
74 struct darwin_info
75 {
76   /* Address of structure dyld_all_image_infos in inferior.  */
77   CORE_ADDR all_image_addr = 0;
78
79   /* Gdb copy of dyld_all_info_infos.  */
80   struct gdb_dyld_all_image_infos all_image {};
81 };
82
83 /* Per-program-space data key.  */
84 static const registry<program_space>::key<darwin_info>
85   solib_darwin_pspace_data;
86
87 /* Get the current darwin data.  If none is found yet, add it now.  This
88    function always returns a valid object.  */
89
90 static struct darwin_info *
91 get_darwin_info (void)
92 {
93   struct darwin_info *info;
94
95   info = solib_darwin_pspace_data.get (current_program_space);
96   if (info != NULL)
97     return info;
98
99   return solib_darwin_pspace_data.emplace (current_program_space);
100 }
101
102 /* Return non-zero if the version in dyld_all_image is known.  */
103
104 static int
105 darwin_dyld_version_ok (const struct darwin_info *info)
106 {
107   return info->all_image.version >= DYLD_VERSION_MIN
108     && info->all_image.version <= DYLD_VERSION_MAX;
109 }
110
111 /* Read dyld_all_image from inferior.  */
112
113 static void
114 darwin_load_image_infos (struct darwin_info *info)
115 {
116   gdb_byte buf[24];
117   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
118   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
119   int len;
120
121   /* If the structure address is not known, don't continue.  */
122   if (info->all_image_addr == 0)
123     return;
124
125   /* The structure has 4 fields: version (4 bytes), count (4 bytes),
126      info (pointer) and notifier (pointer).  */
127   len = 4 + 4 + 2 * ptr_type->length ();
128   gdb_assert (len <= sizeof (buf));
129   memset (&info->all_image, 0, sizeof (info->all_image));
130
131   /* Read structure raw bytes from target.  */
132   if (target_read_memory (info->all_image_addr, buf, len))
133     return;
134
135   /* Extract the fields.  */
136   info->all_image.version = extract_unsigned_integer (buf, 4, byte_order);
137   if (!darwin_dyld_version_ok (info))
138     return;
139
140   info->all_image.count = extract_unsigned_integer (buf + 4, 4, byte_order);
141   info->all_image.info = extract_typed_address (buf + 8, ptr_type);
142   info->all_image.notifier = extract_typed_address
143     (buf + 8 + ptr_type->length (), ptr_type);
144 }
145
146 /* Link map info to include in an allocated so_list entry.  */
147
148 struct lm_info_darwin : public lm_info_base
149 {
150   /* The target location of lm.  */
151   CORE_ADDR lm_addr = 0;
152 };
153
154 /* Lookup the value for a specific symbol.  */
155
156 static CORE_ADDR
157 lookup_symbol_from_bfd (bfd *abfd, const char *symname)
158 {
159   long storage_needed;
160   asymbol **symbol_table;
161   unsigned int number_of_symbols;
162   unsigned int i;
163   CORE_ADDR symaddr = 0;
164
165   storage_needed = bfd_get_symtab_upper_bound (abfd);
166
167   if (storage_needed <= 0)
168     return 0;
169
170   symbol_table = (asymbol **) xmalloc (storage_needed);
171   number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
172
173   for (i = 0; i < number_of_symbols; i++)
174     {
175       asymbol *sym = symbol_table[i];
176
177       if (strcmp (sym->name, symname) == 0
178           && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0)
179         {
180           /* BFD symbols are section relative.  */
181           symaddr = sym->value + sym->section->vma;
182           break;
183         }
184     }
185   xfree (symbol_table);
186
187   return symaddr;
188 }
189
190 /* Return program interpreter string.  */
191
192 static char *
193 find_program_interpreter (void)
194 {
195   char *buf = NULL;
196
197   /* If we have an current exec_bfd, get the interpreter from the load
198      commands.  */
199   if (current_program_space->exec_bfd ())
200     {
201       bfd_mach_o_load_command *cmd;
202
203       if (bfd_mach_o_lookup_command (current_program_space->exec_bfd (),
204                                      BFD_MACH_O_LC_LOAD_DYLINKER, &cmd) == 1)
205         return cmd->command.dylinker.name_str;
206     }
207
208   /* If we didn't find it, read from memory.
209      FIXME: todo.  */
210   return buf;
211 }
212
213 /*  Not used.  I don't see how the main symbol file can be found: the
214     interpreter name is needed and it is known from the executable file.
215     Note that darwin-nat.c implements pid_to_exec_file.  */
216
217 static int
218 open_symbol_file_object (int from_tty)
219 {
220   return 0;
221 }
222
223 /* Build a list of currently loaded shared objects.  See solib-svr4.c.  */
224
225 static struct so_list *
226 darwin_current_sos (void)
227 {
228   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
229   enum bfd_endian byte_order = type_byte_order (ptr_type);
230   int ptr_len = ptr_type->length ();
231   unsigned int image_info_size;
232   struct so_list *head = NULL;
233   struct so_list *tail = NULL;
234   int i;
235   struct darwin_info *info = get_darwin_info ();
236
237   /* Be sure image infos are loaded.  */
238   darwin_load_image_infos (info);
239
240   if (!darwin_dyld_version_ok (info))
241     return NULL;
242
243   image_info_size = ptr_len * 3;
244
245   /* Read infos for each solib.
246      The first entry was rumored to be the executable itself, but this is not
247      true when a large number of shared libraries are used (table expanded ?).
248      We now check all entries, but discard executable images.  */
249   for (i = 0; i < info->all_image.count; i++)
250     {
251       CORE_ADDR iinfo = info->all_image.info + i * image_info_size;
252       gdb_byte buf[image_info_size];
253       CORE_ADDR load_addr;
254       CORE_ADDR path_addr;
255       struct mach_o_header_external hdr;
256       unsigned long hdr_val;
257
258       /* Read image info from inferior.  */
259       if (target_read_memory (iinfo, buf, image_info_size))
260         break;
261
262       load_addr = extract_typed_address (buf, ptr_type);
263       path_addr = extract_typed_address (buf + ptr_len, ptr_type);
264
265       /* Read Mach-O header from memory.  */
266       if (target_read_memory (load_addr, (gdb_byte *) &hdr, sizeof (hdr) - 4))
267         break;
268       /* Discard wrong magic numbers.  Shouldn't happen.  */
269       hdr_val = extract_unsigned_integer
270         (hdr.magic, sizeof (hdr.magic), byte_order);
271       if (hdr_val != BFD_MACH_O_MH_MAGIC && hdr_val != BFD_MACH_O_MH_MAGIC_64)
272         continue;
273       /* Discard executable.  Should happen only once.  */
274       hdr_val = extract_unsigned_integer
275         (hdr.filetype, sizeof (hdr.filetype), byte_order);
276       if (hdr_val == BFD_MACH_O_MH_EXECUTE)
277         continue;
278
279       gdb::unique_xmalloc_ptr<char> file_path
280         = target_read_string (path_addr, SO_NAME_MAX_PATH_SIZE - 1);
281       if (file_path == nullptr)
282         break;
283
284       /* Create and fill the new so_list element.  */
285       gdb::unique_xmalloc_ptr<struct so_list> newobj (XCNEW (struct so_list));
286
287       lm_info_darwin *li = new lm_info_darwin;
288       newobj->lm_info = li;
289
290       strncpy (newobj->so_name, file_path.get (), SO_NAME_MAX_PATH_SIZE - 1);
291       newobj->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
292       strcpy (newobj->so_original_name, newobj->so_name);
293       li->lm_addr = load_addr;
294
295       if (head == NULL)
296         head = newobj.get ();
297       else
298         tail->next = newobj.get ();
299       tail = newobj.release ();
300     }
301
302   return head;
303 }
304
305 /* Check LOAD_ADDR points to a Mach-O executable header.  Return LOAD_ADDR
306    in case of success, 0 in case of failure.  */
307
308 static CORE_ADDR
309 darwin_validate_exec_header (CORE_ADDR load_addr)
310 {
311   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
312   struct mach_o_header_external hdr;
313   unsigned long hdr_val;
314
315   /* Read Mach-O header from memory.  */
316   if (target_read_memory (load_addr, (gdb_byte *) &hdr, sizeof (hdr) - 4))
317     return 0;
318
319   /* Discard wrong magic numbers.  Shouldn't happen.  */
320   hdr_val = extract_unsigned_integer
321     (hdr.magic, sizeof (hdr.magic), byte_order);
322   if (hdr_val != BFD_MACH_O_MH_MAGIC && hdr_val != BFD_MACH_O_MH_MAGIC_64)
323     return 0;
324
325   /* Check executable.  */
326   hdr_val = extract_unsigned_integer
327     (hdr.filetype, sizeof (hdr.filetype), byte_order);
328   if (hdr_val == BFD_MACH_O_MH_EXECUTE)
329     return load_addr;
330
331   return 0;
332 }
333
334 /* Get the load address of the executable using dyld list of images.
335    We assume that the dyld info are correct (which is wrong if the target
336    is stopped at the first instruction).  */
337
338 static CORE_ADDR
339 darwin_read_exec_load_addr_from_dyld (struct darwin_info *info)
340 {
341   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
342   int ptr_len = ptr_type->length ();
343   unsigned int image_info_size = ptr_len * 3;
344   int i;
345
346   /* Read infos for each solib.  One of them should be the executable.  */
347   for (i = 0; i < info->all_image.count; i++)
348     {
349       CORE_ADDR iinfo = info->all_image.info + i * image_info_size;
350       gdb_byte buf[image_info_size];
351       CORE_ADDR load_addr;
352
353       /* Read image info from inferior.  */
354       if (target_read_memory (iinfo, buf, image_info_size))
355         break;
356
357       load_addr = extract_typed_address (buf, ptr_type);
358       if (darwin_validate_exec_header (load_addr) == load_addr)
359         return load_addr;
360     }
361
362   return 0;
363 }
364
365 /* Get the load address of the executable when the PC is at the dyld
366    entry point using parameter passed by the kernel (at SP). */
367
368 static CORE_ADDR
369 darwin_read_exec_load_addr_at_init (struct darwin_info *info)
370 {
371   struct gdbarch *gdbarch = target_gdbarch ();
372   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
373   int addr_size = gdbarch_addr_bit (gdbarch) / 8;
374   ULONGEST load_ptr_addr;
375   ULONGEST load_addr;
376   gdb_byte buf[8];
377
378   /* Get SP.  */
379   if (regcache_cooked_read_unsigned (get_current_regcache (),
380                                      gdbarch_sp_regnum (gdbarch),
381                                      &load_ptr_addr) != REG_VALID)
382     return 0;
383
384   /* Read value at SP (image load address).  */
385   if (target_read_memory (load_ptr_addr, buf, addr_size))
386     return 0;
387
388   load_addr = extract_unsigned_integer (buf, addr_size, byte_order);
389
390   return darwin_validate_exec_header (load_addr);
391 }
392
393 /* Return 1 if PC lies in the dynamic symbol resolution code of the
394    run time loader.  */
395
396 static int
397 darwin_in_dynsym_resolve_code (CORE_ADDR pc)
398 {
399   return 0;
400 }
401
402 /* A wrapper for bfd_mach_o_fat_extract that handles reference
403    counting properly.  This will either return NULL, or return a new
404    reference to a BFD.  */
405
406 static gdb_bfd_ref_ptr
407 gdb_bfd_mach_o_fat_extract (bfd *abfd, bfd_format format,
408                             const bfd_arch_info_type *arch)
409 {
410   bfd *result = bfd_mach_o_fat_extract (abfd, format, arch);
411
412   if (result == NULL)
413     return NULL;
414
415   if (result == abfd)
416     gdb_bfd_ref (result);
417   else
418     gdb_bfd_mark_parent (result, abfd);
419
420   return gdb_bfd_ref_ptr (result);
421 }
422
423 /* Return the BFD for the program interpreter.  */
424
425 static gdb_bfd_ref_ptr
426 darwin_get_dyld_bfd ()
427 {
428   char *interp_name;
429
430   /* This method doesn't work with an attached process.  */
431   if (current_inferior ()->attach_flag)
432     return NULL;
433
434   /* Find the program interpreter.  */
435   interp_name = find_program_interpreter ();
436   if (!interp_name)
437     return NULL;
438
439   /* Create a bfd for the interpreter.  */
440   gdb_bfd_ref_ptr dyld_bfd (gdb_bfd_open (interp_name, gnutarget));
441   if (dyld_bfd != NULL)
442     {
443       gdb_bfd_ref_ptr sub
444         (gdb_bfd_mach_o_fat_extract (dyld_bfd.get (), bfd_object,
445                                      gdbarch_bfd_arch_info (target_gdbarch ())));
446       dyld_bfd = sub;
447     }
448   return dyld_bfd;
449 }
450
451 /* Extract dyld_all_image_addr when the process was just created, assuming the
452    current PC is at the entry of the dynamic linker.  */
453
454 static void
455 darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
456 {
457   CORE_ADDR load_addr = 0;
458   gdb_bfd_ref_ptr dyld_bfd = darwin_get_dyld_bfd ();
459
460   if (dyld_bfd == NULL)
461     return;
462
463   /* We find the dynamic linker's base address by examining
464      the current pc (which should point at the entry point for the
465      dynamic linker) and subtracting the offset of the entry point.  */
466   load_addr = (regcache_read_pc (get_current_regcache ())
467                - bfd_get_start_address (dyld_bfd.get ()));
468
469   /* Now try to set a breakpoint in the dynamic linker.  */
470   info->all_image_addr =
471     lookup_symbol_from_bfd (dyld_bfd.get (), "_dyld_all_image_infos");
472
473   if (info->all_image_addr == 0)
474     return;
475
476   info->all_image_addr += load_addr;
477 }
478
479 /* Extract dyld_all_image_addr reading it from
480    TARGET_OBJECT_DARWIN_DYLD_INFO.  */
481
482 static void
483 darwin_solib_read_all_image_info_addr (struct darwin_info *info)
484 {
485   gdb_byte buf[8];
486   LONGEST len;
487   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
488
489   /* Sanity check.  */
490   if (ptr_type->length () > sizeof (buf))
491     return;
492
493   len = target_read (current_inferior ()->top_target (),
494                      TARGET_OBJECT_DARWIN_DYLD_INFO,
495                      NULL, buf, 0, ptr_type->length ());
496   if (len <= 0)
497     return;
498
499   /* The use of BIG endian is intended, as BUF is a raw stream of bytes.  This
500       makes the support of remote protocol easier.  */
501   info->all_image_addr = extract_unsigned_integer (buf, len, BFD_ENDIAN_BIG);
502 }
503
504 /* Shared library startup support.  See documentation in solib-svr4.c.  */
505
506 static void
507 darwin_solib_create_inferior_hook (int from_tty)
508 {
509   /* Everything below only makes sense if we have a running inferior.  */
510   if (!target_has_execution ())
511     return;
512
513   struct darwin_info *info = get_darwin_info ();
514   CORE_ADDR load_addr;
515
516   info->all_image_addr = 0;
517
518   darwin_solib_read_all_image_info_addr (info);
519
520   if (info->all_image_addr == 0)
521     darwin_solib_get_all_image_info_addr_at_init (info);
522
523   if (info->all_image_addr == 0)
524     return;
525
526   darwin_load_image_infos (info);
527
528   if (!darwin_dyld_version_ok (info))
529     {
530       warning (_("unhandled dyld version (%d)"), info->all_image.version);
531       return;
532     }
533
534   if (info->all_image.count != 0)
535     {
536       /* Possible relocate the main executable (PIE).  */
537       load_addr = darwin_read_exec_load_addr_from_dyld (info);
538     }
539   else
540     {
541       /* Possible issue:
542          Do not break on the notifier if dyld is not initialized (deduced from
543          count == 0).  In that case, dyld hasn't relocated itself and the
544          notifier may point to a wrong address.  */
545
546       load_addr = darwin_read_exec_load_addr_at_init (info);
547     }
548
549   if (load_addr != 0 && current_program_space->symfile_object_file != NULL)
550     {
551       CORE_ADDR vmaddr;
552
553       /* Find the base address of the executable.  */
554       vmaddr = bfd_mach_o_get_base_address (current_program_space->exec_bfd ());
555
556       /* Relocate.  */
557       if (vmaddr != load_addr)
558         objfile_rebase (current_program_space->symfile_object_file,
559                         load_addr - vmaddr);
560     }
561
562   /* Set solib notifier (to reload list of shared libraries).  */
563   CORE_ADDR notifier = info->all_image.notifier;
564
565   if (info->all_image.count == 0)
566     {
567       /* Dyld hasn't yet relocated itself, so the notifier address may
568          be incorrect (as it has to be relocated).  */
569       CORE_ADDR start
570         = bfd_get_start_address (current_program_space->exec_bfd ());
571       if (start == 0)
572         notifier = 0;
573       else
574         {
575           gdb_bfd_ref_ptr dyld_bfd = darwin_get_dyld_bfd ();
576           if (dyld_bfd != NULL)
577             {
578               CORE_ADDR dyld_bfd_start_address;
579               CORE_ADDR dyld_relocated_base_address;
580               CORE_ADDR pc;
581
582               dyld_bfd_start_address = bfd_get_start_address (dyld_bfd.get());
583
584               /* We find the dynamic linker's base address by examining
585                  the current pc (which should point at the entry point
586                  for the dynamic linker) and subtracting the offset of
587                  the entry point.  */
588
589               pc = regcache_read_pc (get_current_regcache ());
590               dyld_relocated_base_address = pc - dyld_bfd_start_address;
591
592               /* We get the proper notifier relocated address by
593                  adding the dyld relocated base address to the current
594                  notifier offset value.  */
595
596               notifier += dyld_relocated_base_address;
597             }
598         }
599     }
600
601   /* Add the breakpoint which is hit by dyld when the list of solib is
602      modified.  */
603   if (notifier != 0)
604     create_solib_event_breakpoint (target_gdbarch (), notifier);
605 }
606
607 static void
608 darwin_clear_solib (void)
609 {
610   struct darwin_info *info = get_darwin_info ();
611
612   info->all_image_addr = 0;
613   info->all_image.version = 0;
614 }
615
616 static void
617 darwin_free_so (struct so_list *so)
618 {
619   lm_info_darwin *li = (lm_info_darwin *) so->lm_info;
620
621   delete li;
622 }
623
624 /* The section table is built from bfd sections using bfd VMAs.
625    Relocate these VMAs according to solib info.  */
626
627 static void
628 darwin_relocate_section_addresses (struct so_list *so,
629                                    struct target_section *sec)
630 {
631   lm_info_darwin *li = (lm_info_darwin *) so->lm_info;
632
633   sec->addr += li->lm_addr;
634   sec->endaddr += li->lm_addr;
635
636   /* Best effort to set addr_high/addr_low.  This is used only by
637      'info sharedlibary'.  */
638   if (so->addr_high == 0)
639     {
640       so->addr_low = sec->addr;
641       so->addr_high = sec->endaddr;
642     }
643   if (sec->endaddr > so->addr_high)
644     so->addr_high = sec->endaddr;
645   if (sec->addr < so->addr_low)
646     so->addr_low = sec->addr;
647 }
648 \f
649 static gdb_bfd_ref_ptr
650 darwin_bfd_open (const char *pathname)
651 {
652   int found_file;
653
654   /* Search for shared library file.  */
655   gdb::unique_xmalloc_ptr<char> found_pathname
656     = solib_find (pathname, &found_file);
657   if (found_pathname == NULL)
658     perror_with_name (pathname);
659
660   /* Open bfd for shared library.  */
661   gdb_bfd_ref_ptr abfd (solib_bfd_fopen (found_pathname.get (), found_file));
662
663   gdb_bfd_ref_ptr res
664     (gdb_bfd_mach_o_fat_extract (abfd.get (), bfd_object,
665                                  gdbarch_bfd_arch_info (target_gdbarch ())));
666   if (res == NULL)
667     error (_("`%s': not a shared-library: %s"),
668            bfd_get_filename (abfd.get ()), bfd_errmsg (bfd_get_error ()));
669
670   /* The current filename for fat-binary BFDs is a name generated
671      by BFD, usually a string containing the name of the architecture.
672      Reset its value to the actual filename.  */
673   bfd_set_filename (res.get (), pathname);
674
675   return res;
676 }
677
678 const struct target_so_ops darwin_so_ops =
679 {
680   darwin_relocate_section_addresses,
681   darwin_free_so,
682   nullptr,
683   darwin_clear_solib,
684   darwin_solib_create_inferior_hook,
685   darwin_current_sos,
686   open_symbol_file_object,
687   darwin_in_dynsym_resolve_code,
688   darwin_bfd_open,
689 };