gdb/solib-darwin.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Handle Darwin shared libraries for GDB, the GNU Debugger.

  2.    Copyright (C) 2009-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"

  15. #include "symtab.h"
  16. #include "bfd.h"
  17. #include "symfile.h"
  18. #include "objfiles.h"
  19. #include "gdbcore.h"
  20. #include "target.h"
  21. #include "inferior.h"
  22. #include "regcache.h"
  23. #include "gdbthread.h"
  24. #include "gdb_bfd.h"

  25. #include "solist.h"
  26. #include "solib.h"
  27. #include "solib-svr4.h"

  28. #include "bfd-target.h"
  29. #include "elf-bfd.h"
  30. #include "exec.h"
  31. #include "auxv.h"
  32. #include "mach-o.h"
  33. #include "mach-o/external.h"

  34. struct gdb_dyld_image_info
  35. {
  36.   /* Base address (which corresponds to the Mach-O header).  */
  37.   CORE_ADDR mach_header;
  38.   /* Image file path.  */
  39.   CORE_ADDR file_path;
  40.   /* st.m_time of image file.  */
  41.   unsigned long mtime;
  42. };

  43. /* Content of inferior dyld_all_image_infos structure.
  44.    See /usr/include/mach-o/dyld_images.h for the documentation.  */
  45. struct gdb_dyld_all_image_infos
  46. {
  47.   /* Version (1).  */
  48.   unsigned int version;
  49.   /* Number of images.  */
  50.   unsigned int count;
  51.   /* Image description.  */
  52.   CORE_ADDR info;
  53.   /* Notifier (function called when a library is added or removed).  */
  54.   CORE_ADDR notifier;
  55. };

  56. /* Current all_image_infos version.  */
  57. #define DYLD_VERSION_MIN 1
  58. #define DYLD_VERSION_MAX 14

  59. /* Per PSPACE specific data.  */
  60. struct darwin_info
  61. {
  62.   /* Address of structure dyld_all_image_infos in inferior.  */
  63.   CORE_ADDR all_image_addr;

  64.   /* Gdb copy of dyld_all_info_infos.  */
  65.   struct gdb_dyld_all_image_infos all_image;
  66. };

  67. /* Per-program-space data key.  */
  68. static const struct program_space_data *solib_darwin_pspace_data;

  69. static void
  70. darwin_pspace_data_cleanup (struct program_space *pspace, void *arg)
  71. {
  72.   xfree (arg);
  73. }

  74. /* Get the current darwin data.  If none is found yet, add it now.  This
  75.    function always returns a valid object.  */

  76. static struct darwin_info *
  77. get_darwin_info (void)
  78. {
  79.   struct darwin_info *info;

  80.   info = program_space_data (current_program_space, solib_darwin_pspace_data);
  81.   if (info != NULL)
  82.     return info;

  83.   info = XCNEW (struct darwin_info);
  84.   set_program_space_data (current_program_space,
  85.                           solib_darwin_pspace_data, info);
  86.   return info;
  87. }

  88. /* Return non-zero if the version in dyld_all_image is known.  */

  89. static int
  90. darwin_dyld_version_ok (const struct darwin_info *info)
  91. {
  92.   return info->all_image.version >= DYLD_VERSION_MIN
  93.     && info->all_image.version <= DYLD_VERSION_MAX;
  94. }

  95. /* Read dyld_all_image from inferior.  */

  96. static void
  97. darwin_load_image_infos (struct darwin_info *info)
  98. {
  99.   gdb_byte buf[24];
  100.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  101.   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  102.   int len;

  103.   /* If the structure address is not known, don't continue.  */
  104.   if (info->all_image_addr == 0)
  105.     return;

  106.   /* The structure has 4 fields: version (4 bytes), count (4 bytes),
  107.      info (pointer) and notifier (pointer).  */
  108.   len = 4 + 4 + 2 * ptr_type->length;
  109.   gdb_assert (len <= sizeof (buf));
  110.   memset (&info->all_image, 0, sizeof (info->all_image));

  111.   /* Read structure raw bytes from target.  */
  112.   if (target_read_memory (info->all_image_addr, buf, len))
  113.     return;

  114.   /* Extract the fields.  */
  115.   info->all_image.version = extract_unsigned_integer (buf, 4, byte_order);
  116.   if (!darwin_dyld_version_ok (info))
  117.     return;

  118.   info->all_image.count = extract_unsigned_integer (buf + 4, 4, byte_order);
  119.   info->all_image.info = extract_typed_address (buf + 8, ptr_type);
  120.   info->all_image.notifier = extract_typed_address
  121.     (buf + 8 + ptr_type->length, ptr_type);
  122. }

  123. /* Link map info to include in an allocated so_list entry.  */

  124. struct lm_info
  125. {
  126.   /* The target location of lm.  */
  127.   CORE_ADDR lm_addr;
  128. };

  129. struct darwin_so_list
  130. {
  131.   /* Common field.  */
  132.   struct so_list sl;
  133.   /* Darwin specific data.  */
  134.   struct lm_info li;
  135. };

  136. /* Lookup the value for a specific symbol.  */

  137. static CORE_ADDR
  138. lookup_symbol_from_bfd (bfd *abfd, char *symname)
  139. {
  140.   long storage_needed;
  141.   asymbol **symbol_table;
  142.   unsigned int number_of_symbols;
  143.   unsigned int i;
  144.   CORE_ADDR symaddr = 0;

  145.   storage_needed = bfd_get_symtab_upper_bound (abfd);

  146.   if (storage_needed <= 0)
  147.     return 0;

  148.   symbol_table = (asymbol **) xmalloc (storage_needed);
  149.   number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);

  150.   for (i = 0; i < number_of_symbols; i++)
  151.     {
  152.       asymbol *sym = symbol_table[i];

  153.       if (strcmp (sym->name, symname) == 0
  154.           && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0)
  155.         {
  156.           /* BFD symbols are section relative.  */
  157.           symaddr = sym->value + sym->section->vma;
  158.           break;
  159.         }
  160.     }
  161.   xfree (symbol_table);

  162.   return symaddr;
  163. }

  164. /* Return program interpreter string.  */

  165. static char *
  166. find_program_interpreter (void)
  167. {
  168.   char *buf = NULL;

  169.   /* If we have an exec_bfd, get the interpreter from the load commands.  */
  170.   if (exec_bfd)
  171.     {
  172.       bfd_mach_o_load_command *cmd;

  173.       if (bfd_mach_o_lookup_command (exec_bfd,
  174.                                      BFD_MACH_O_LC_LOAD_DYLINKER, &cmd) == 1)
  175.         return cmd->command.dylinker.name_str;
  176.     }

  177.   /* If we didn't find it, read from memory.
  178.      FIXME: todo.  */
  179.   return buf;
  180. }

  181. /*  Not used.  I don't see how the main symbol file can be found: the
  182.     interpreter name is needed and it is known from the executable file.
  183.     Note that darwin-nat.c implements pid_to_exec_file.  */

  184. static int
  185. open_symbol_file_object (void *from_ttyp)
  186. {
  187.   return 0;
  188. }

  189. /* Build a list of currently loaded shared objects.  See solib-svr4.c.  */

  190. static struct so_list *
  191. darwin_current_sos (void)
  192. {
  193.   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  194.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  195.   int ptr_len = TYPE_LENGTH (ptr_type);
  196.   unsigned int image_info_size;
  197.   struct so_list *head = NULL;
  198.   struct so_list *tail = NULL;
  199.   int i;
  200.   struct darwin_info *info = get_darwin_info ();

  201.   /* Be sure image infos are loaded.  */
  202.   darwin_load_image_infos (info);

  203.   if (!darwin_dyld_version_ok (info))
  204.     return NULL;

  205.   image_info_size = ptr_len * 3;

  206.   /* Read infos for each solib.
  207.      The first entry was rumored to be the executable itself, but this is not
  208.      true when a large number of shared libraries are used (table expanded ?).
  209.      We now check all entries, but discard executable images.  */
  210.   for (i = 0; i < info->all_image.count; i++)
  211.     {
  212.       CORE_ADDR iinfo = info->all_image.info + i * image_info_size;
  213.       gdb_byte buf[image_info_size];
  214.       CORE_ADDR load_addr;
  215.       CORE_ADDR path_addr;
  216.       struct mach_o_header_external hdr;
  217.       unsigned long hdr_val;
  218.       char *file_path;
  219.       int errcode;
  220.       struct darwin_so_list *dnew;
  221.       struct so_list *new;
  222.       struct cleanup *old_chain;

  223.       /* Read image info from inferior.  */
  224.       if (target_read_memory (iinfo, buf, image_info_size))
  225.         break;

  226.       load_addr = extract_typed_address (buf, ptr_type);
  227.       path_addr = extract_typed_address (buf + ptr_len, ptr_type);

  228.       /* Read Mach-O header from memory.  */
  229.       if (target_read_memory (load_addr, (gdb_byte *) &hdr, sizeof (hdr) - 4))
  230.         break;
  231.       /* Discard wrong magic numbers.  Shouldn't happen.  */
  232.       hdr_val = extract_unsigned_integer
  233.         (hdr.magic, sizeof (hdr.magic), byte_order);
  234.       if (hdr_val != BFD_MACH_O_MH_MAGIC && hdr_val != BFD_MACH_O_MH_MAGIC_64)
  235.         continue;
  236.       /* Discard executable.  Should happen only once.  */
  237.       hdr_val = extract_unsigned_integer
  238.         (hdr.filetype, sizeof (hdr.filetype), byte_order);
  239.       if (hdr_val == BFD_MACH_O_MH_EXECUTE)
  240.         continue;

  241.       target_read_string (path_addr, &file_path,
  242.                           SO_NAME_MAX_PATH_SIZE - 1, &errcode);
  243.       if (errcode)
  244.         break;

  245.       /* Create and fill the new so_list element.  */
  246.       dnew = XCNEW (struct darwin_so_list);
  247.       new = &dnew->sl;
  248.       old_chain = make_cleanup (xfree, dnew);

  249.       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);
  253.       xfree (file_path);
  254.       new->lm_info->lm_addr = load_addr;

  255.       if (head == NULL)
  256.         head = new;
  257.       else
  258.         tail->next = new;
  259.       tail = new;

  260.       discard_cleanups (old_chain);
  261.     }

  262.   return head;
  263. }

  264. /* Get the load address of the executable.  We assume that the dyld info are
  265.    correct.  */

  266. static CORE_ADDR
  267. darwin_read_exec_load_addr (struct darwin_info *info)
  268. {
  269.   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
  270.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  271.   int ptr_len = TYPE_LENGTH (ptr_type);
  272.   unsigned int image_info_size = ptr_len * 3;
  273.   int i;

  274.   /* Read infos for each solib.  One of them should be the executable.  */
  275.   for (i = 0; i < info->all_image.count; i++)
  276.     {
  277.       CORE_ADDR iinfo = info->all_image.info + i * image_info_size;
  278.       gdb_byte buf[image_info_size];
  279.       CORE_ADDR load_addr;
  280.       struct mach_o_header_external hdr;
  281.       unsigned long hdr_val;

  282.       /* Read image info from inferior.  */
  283.       if (target_read_memory (iinfo, buf, image_info_size))
  284.         break;

  285.       load_addr = extract_typed_address (buf, ptr_type);

  286.       /* Read Mach-O header from memory.  */
  287.       if (target_read_memory (load_addr, (gdb_byte *) &hdr, sizeof (hdr) - 4))
  288.         break;
  289.       /* Discard wrong magic numbers.  Shouldn't happen.  */
  290.       hdr_val = extract_unsigned_integer
  291.         (hdr.magic, sizeof (hdr.magic), byte_order);
  292.       if (hdr_val != BFD_MACH_O_MH_MAGIC && hdr_val != BFD_MACH_O_MH_MAGIC_64)
  293.         continue;
  294.       /* Check executable.  */
  295.       hdr_val = extract_unsigned_integer
  296.         (hdr.filetype, sizeof (hdr.filetype), byte_order);
  297.       if (hdr_val == BFD_MACH_O_MH_EXECUTE)
  298.         return load_addr;
  299.     }

  300.   return 0;
  301. }

  302. /* Return 1 if PC lies in the dynamic symbol resolution code of the
  303.    run time loader.  */

  304. static int
  305. darwin_in_dynsym_resolve_code (CORE_ADDR pc)
  306. {
  307.   return 0;
  308. }


  309. /* No special symbol handling.  */

  310. static void
  311. darwin_special_symbol_handling (void)
  312. {
  313. }

  314. /* A wrapper for bfd_mach_o_fat_extract that handles reference
  315.    counting properly.  This will either return NULL, or return a new
  316.    reference to a BFD.  */

  317. static bfd *
  318. gdb_bfd_mach_o_fat_extract (bfd *abfd, bfd_format format,
  319.                             const bfd_arch_info_type *arch)
  320. {
  321.   bfd *result = bfd_mach_o_fat_extract (abfd, format, arch);

  322.   if (result == NULL)
  323.     return NULL;

  324.   if (result == abfd)
  325.     gdb_bfd_ref (result);
  326.   else
  327.     gdb_bfd_mark_parent (result, abfd);

  328.   return result;
  329. }

  330. /* Extract dyld_all_image_addr when the process was just created, assuming the
  331.    current PC is at the entry of the dynamic linker.  */

  332. static void
  333. darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
  334. {
  335.   char *interp_name;
  336.   CORE_ADDR load_addr = 0;
  337.   bfd *dyld_bfd = NULL;
  338.   struct cleanup *cleanup;

  339.   /* This method doesn't work with an attached process.  */
  340.   if (current_inferior ()->attach_flag)
  341.     return;

  342.   /* Find the program interpreter.  */
  343.   interp_name = find_program_interpreter ();
  344.   if (!interp_name)
  345.     return;

  346.   cleanup = make_cleanup (null_cleanup, NULL);

  347.   /* Create a bfd for the interpreter.  */
  348.   dyld_bfd = gdb_bfd_open (interp_name, gnutarget, -1);
  349.   if (dyld_bfd)
  350.     {
  351.       bfd *sub;

  352.       make_cleanup_bfd_unref (dyld_bfd);
  353.       sub = gdb_bfd_mach_o_fat_extract (dyld_bfd, bfd_object,
  354.                                         gdbarch_bfd_arch_info (target_gdbarch ()));
  355.       if (sub)
  356.         {
  357.           dyld_bfd = sub;
  358.           make_cleanup_bfd_unref (sub);
  359.         }
  360.       else
  361.         dyld_bfd = NULL;
  362.     }
  363.   if (!dyld_bfd)
  364.     {
  365.       do_cleanups (cleanup);
  366.       return;
  367.     }

  368.   /* We find the dynamic linker's base address by examining
  369.      the current pc (which should point at the entry point for the
  370.      dynamic linker) and subtracting the offset of the entry point.  */
  371.   load_addr = (regcache_read_pc (get_current_regcache ())
  372.                - bfd_get_start_address (dyld_bfd));

  373.   /* Now try to set a breakpoint in the dynamic linker.  */
  374.   info->all_image_addr =
  375.     lookup_symbol_from_bfd (dyld_bfd, "_dyld_all_image_infos");

  376.   do_cleanups (cleanup);

  377.   if (info->all_image_addr == 0)
  378.     return;

  379.   info->all_image_addr += load_addr;
  380. }

  381. /* Extract dyld_all_image_addr reading it from
  382.    TARGET_OBJECT_DARWIN_DYLD_INFO.  */

  383. static void
  384. darwin_solib_read_all_image_info_addr (struct darwin_info *info)
  385. {
  386.   gdb_byte buf[8 + 8 + 4];
  387.   LONGEST len;
  388.   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());

  389.   len = target_read (&current_target, TARGET_OBJECT_DARWIN_DYLD_INFO, NULL,
  390.                      buf, 0, sizeof (buf));
  391.   if (len != sizeof (buf))
  392.     return;

  393.   info->all_image_addr = extract_unsigned_integer (buf, 8, byte_order);
  394. }

  395. /* Shared library startup support.  See documentation in solib-svr4.c.  */

  396. static void
  397. darwin_solib_create_inferior_hook (int from_tty)
  398. {
  399.   struct darwin_info *info = get_darwin_info ();
  400.   CORE_ADDR load_addr;

  401.   info->all_image_addr = 0;

  402.   darwin_solib_read_all_image_info_addr (info);

  403.   if (info->all_image_addr == 0)
  404.     darwin_solib_get_all_image_info_addr_at_init (info);

  405.   if (info->all_image_addr == 0)
  406.     return;

  407.   darwin_load_image_infos (info);

  408.   if (!darwin_dyld_version_ok (info))
  409.     {
  410.       warning (_("unhandled dyld version (%d)"), info->all_image.version);
  411.       return;
  412.     }

  413.   create_solib_event_breakpoint (target_gdbarch (), info->all_image.notifier);

  414.   /* Possible relocate the main executable (PIE).  */
  415.   load_addr = darwin_read_exec_load_addr (info);
  416.   if (load_addr != 0 && symfile_objfile != NULL)
  417.     {
  418.       CORE_ADDR vmaddr;

  419.       /* Find the base address of the executable.  */
  420.       vmaddr = bfd_mach_o_get_base_address (exec_bfd);

  421.       /* Relocate.  */
  422.       if (vmaddr != load_addr)
  423.         objfile_rebase (symfile_objfile, load_addr - vmaddr);
  424.     }
  425. }

  426. static void
  427. darwin_clear_solib (void)
  428. {
  429.   struct darwin_info *info = get_darwin_info ();

  430.   info->all_image_addr = 0;
  431.   info->all_image.version = 0;
  432. }

  433. static void
  434. darwin_free_so (struct so_list *so)
  435. {
  436. }

  437. /* The section table is built from bfd sections using bfd VMAs.
  438.    Relocate these VMAs according to solib info.  */

  439. static void
  440. darwin_relocate_section_addresses (struct so_list *so,
  441.                                    struct target_section *sec)
  442. {
  443.   sec->addr += so->lm_info->lm_addr;
  444.   sec->endaddr += so->lm_info->lm_addr;

  445.   /* Best effort to set addr_high/addr_low.  This is used only by
  446.      'info sharedlibary'.  */
  447.   if (so->addr_high == 0)
  448.     {
  449.       so->addr_low = sec->addr;
  450.       so->addr_high = sec->endaddr;
  451.     }
  452.   if (sec->endaddr > so->addr_high)
  453.     so->addr_high = sec->endaddr;
  454.   if (sec->addr < so->addr_low)
  455.     so->addr_low = sec->addr;
  456. }

  457. static struct symbol *
  458. darwin_lookup_lib_symbol (struct objfile *objfile,
  459.                           const char *name,
  460.                           const domain_enum domain)
  461. {
  462.   return NULL;
  463. }

  464. static bfd *
  465. darwin_bfd_open (char *pathname)
  466. {
  467.   char *found_pathname;
  468.   int found_file;
  469.   bfd *abfd;
  470.   bfd *res;

  471.   /* Search for shared library file.  */
  472.   found_pathname = solib_find (pathname, &found_file);
  473.   if (found_pathname == NULL)
  474.     perror_with_name (pathname);

  475.   /* Open bfd for shared library.  */
  476.   abfd = solib_bfd_fopen (found_pathname, found_file);

  477.   res = gdb_bfd_mach_o_fat_extract (abfd, bfd_object,
  478.                                     gdbarch_bfd_arch_info (target_gdbarch ()));
  479.   if (!res)
  480.     {
  481.       make_cleanup_bfd_unref (abfd);
  482.       error (_("`%s': not a shared-library: %s"),
  483.              bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
  484.     }

  485.   /* The current filename for fat-binary BFDs is a name generated
  486.      by BFD, usually a string containing the name of the architecture.
  487.      Reset its value to the actual filename.  */
  488.   xfree (bfd_get_filename (res));
  489.   res->filename = xstrdup (pathname);

  490.   gdb_bfd_unref (abfd);
  491.   return res;
  492. }

  493. struct target_so_ops darwin_so_ops;

  494. /* -Wmissing-prototypes */
  495. extern initialize_file_ftype _initialize_darwin_solib;

  496. void
  497. _initialize_darwin_solib (void)
  498. {
  499.   solib_darwin_pspace_data
  500.     = register_program_space_data_with_cleanup (NULL,
  501.                                                 darwin_pspace_data_cleanup);

  502.   darwin_so_ops.relocate_section_addresses = darwin_relocate_section_addresses;
  503.   darwin_so_ops.free_so = darwin_free_so;
  504.   darwin_so_ops.clear_solib = darwin_clear_solib;
  505.   darwin_so_ops.solib_create_inferior_hook = darwin_solib_create_inferior_hook;
  506.   darwin_so_ops.special_symbol_handling = darwin_special_symbol_handling;
  507.   darwin_so_ops.current_sos = darwin_current_sos;
  508.   darwin_so_ops.open_symbol_file_object = open_symbol_file_object;
  509.   darwin_so_ops.in_dynsym_resolve_code = darwin_in_dynsym_resolve_code;
  510.   darwin_so_ops.lookup_lib_global_symbol = darwin_lookup_lib_symbol;
  511.   darwin_so_ops.bfd_open = darwin_bfd_open;
  512. }