GDB (API)
/home/stan/gdb/src/gdb/solib-darwin.c
Go to the documentation of this file.
00001 /* Handle Darwin shared libraries for GDB, the GNU Debugger.
00002 
00003    Copyright (C) 2009-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 
00022 #include "symtab.h"
00023 #include "bfd.h"
00024 #include "symfile.h"
00025 #include "objfiles.h"
00026 #include "gdbcore.h"
00027 #include "target.h"
00028 #include "inferior.h"
00029 #include "regcache.h"
00030 #include "gdbthread.h"
00031 #include "gdb_bfd.h"
00032 
00033 #include "gdb_assert.h"
00034 
00035 #include "solist.h"
00036 #include "solib.h"
00037 #include "solib-svr4.h"
00038 
00039 #include "bfd-target.h"
00040 #include "elf-bfd.h"
00041 #include "exec.h"
00042 #include "auxv.h"
00043 #include "exceptions.h"
00044 #include "mach-o.h"
00045 #include "mach-o/external.h"
00046 
00047 struct gdb_dyld_image_info
00048 {
00049   /* Base address (which corresponds to the Mach-O header).  */
00050   CORE_ADDR mach_header;
00051   /* Image file path.  */
00052   CORE_ADDR file_path;
00053   /* st.m_time of image file.  */
00054   unsigned long mtime;
00055 };
00056 
00057 /* Content of inferior dyld_all_image_infos structure.
00058    See /usr/include/mach-o/dyld_images.h for the documentation.  */
00059 struct gdb_dyld_all_image_infos
00060 {
00061   /* Version (1).  */
00062   unsigned int version;
00063   /* Number of images.  */
00064   unsigned int count;
00065   /* Image description.  */
00066   CORE_ADDR info;
00067   /* Notifier (function called when a library is added or removed).  */
00068   CORE_ADDR notifier;
00069 };
00070 
00071 /* Current all_image_infos version.  */
00072 #define DYLD_VERSION_MIN 1
00073 #define DYLD_VERSION_MAX 12
00074 
00075 /* Per PSPACE specific data.  */
00076 struct darwin_info
00077 {
00078   /* Address of structure dyld_all_image_infos in inferior.  */
00079   CORE_ADDR all_image_addr;
00080 
00081   /* Gdb copy of dyld_all_info_infos.  */
00082   struct gdb_dyld_all_image_infos all_image;
00083 };
00084 
00085 /* Per-program-space data key.  */
00086 static const struct program_space_data *solib_darwin_pspace_data;
00087 
00088 static void
00089 darwin_pspace_data_cleanup (struct program_space *pspace, void *arg)
00090 {
00091   struct darwin_info *info;
00092 
00093   info = program_space_data (pspace, solib_darwin_pspace_data);
00094   xfree (info);
00095 }
00096 
00097 /* Get the current darwin data.  If none is found yet, add it now.  This
00098    function always returns a valid object.  */
00099 
00100 static struct darwin_info *
00101 get_darwin_info (void)
00102 {
00103   struct darwin_info *info;
00104 
00105   info = program_space_data (current_program_space, solib_darwin_pspace_data);
00106   if (info != NULL)
00107     return info;
00108 
00109   info = XZALLOC (struct darwin_info);
00110   set_program_space_data (current_program_space,
00111                           solib_darwin_pspace_data, info);
00112   return info;
00113 }
00114 
00115 /* Return non-zero if the version in dyld_all_image is known.  */
00116 
00117 static int
00118 darwin_dyld_version_ok (const struct darwin_info *info)
00119 {
00120   return info->all_image.version >= DYLD_VERSION_MIN
00121     && info->all_image.version <= DYLD_VERSION_MAX;
00122 }
00123 
00124 /* Read dyld_all_image from inferior.  */
00125 
00126 static void
00127 darwin_load_image_infos (struct darwin_info *info)
00128 {
00129   gdb_byte buf[24];
00130   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
00131   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
00132   int len;
00133 
00134   /* If the structure address is not known, don't continue.  */
00135   if (info->all_image_addr == 0)
00136     return;
00137 
00138   /* The structure has 4 fields: version (4 bytes), count (4 bytes),
00139      info (pointer) and notifier (pointer).  */
00140   len = 4 + 4 + 2 * ptr_type->length;
00141   gdb_assert (len <= sizeof (buf));
00142   memset (&info->all_image, 0, sizeof (info->all_image));
00143 
00144   /* Read structure raw bytes from target.  */
00145   if (target_read_memory (info->all_image_addr, buf, len))
00146     return;
00147 
00148   /* Extract the fields.  */
00149   info->all_image.version = extract_unsigned_integer (buf, 4, byte_order);
00150   if (!darwin_dyld_version_ok (info))
00151     return;
00152 
00153   info->all_image.count = extract_unsigned_integer (buf + 4, 4, byte_order);
00154   info->all_image.info = extract_typed_address (buf + 8, ptr_type);
00155   info->all_image.notifier = extract_typed_address
00156     (buf + 8 + ptr_type->length, ptr_type);
00157 }
00158 
00159 /* Link map info to include in an allocated so_list entry.  */
00160 
00161 struct lm_info
00162 {
00163   /* The target location of lm.  */
00164   CORE_ADDR lm_addr;
00165 };
00166 
00167 struct darwin_so_list
00168 {
00169   /* Common field.  */
00170   struct so_list sl;
00171   /* Darwin specific data.  */
00172   struct lm_info li;
00173 };
00174 
00175 /* Lookup the value for a specific symbol.  */
00176 
00177 static CORE_ADDR
00178 lookup_symbol_from_bfd (bfd *abfd, char *symname)
00179 {
00180   long storage_needed;
00181   asymbol **symbol_table;
00182   unsigned int number_of_symbols;
00183   unsigned int i;
00184   CORE_ADDR symaddr = 0;
00185 
00186   storage_needed = bfd_get_symtab_upper_bound (abfd);
00187 
00188   if (storage_needed <= 0)
00189     return 0;
00190 
00191   symbol_table = (asymbol **) xmalloc (storage_needed);
00192   number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);
00193 
00194   for (i = 0; i < number_of_symbols; i++)
00195     {
00196       asymbol *sym = symbol_table[i];
00197 
00198       if (strcmp (sym->name, symname) == 0
00199           && (sym->section->flags & (SEC_CODE | SEC_DATA)) != 0)
00200         {
00201           /* BFD symbols are section relative.  */
00202           symaddr = sym->value + sym->section->vma;
00203           break;
00204         }
00205     }
00206   xfree (symbol_table);
00207 
00208   return symaddr;
00209 }
00210 
00211 /* Return program interpreter string.  */
00212 
00213 static char *
00214 find_program_interpreter (void)
00215 {
00216   char *buf = NULL;
00217 
00218   /* If we have an exec_bfd, get the interpreter from the load commands.  */
00219   if (exec_bfd)
00220     {
00221       bfd_mach_o_load_command *cmd;
00222 
00223       if (bfd_mach_o_lookup_command (exec_bfd,
00224                                      BFD_MACH_O_LC_LOAD_DYLINKER, &cmd) == 1)
00225         return cmd->command.dylinker.name_str;
00226     }
00227 
00228   /* If we didn't find it, read from memory.
00229      FIXME: todo.  */
00230   return buf;
00231 }
00232 
00233 /*  Not used.  I don't see how the main symbol file can be found: the
00234     interpreter name is needed and it is known from the executable file.
00235     Note that darwin-nat.c implements pid_to_exec_file.  */
00236 
00237 static int
00238 open_symbol_file_object (void *from_ttyp)
00239 {
00240   return 0;
00241 }
00242 
00243 /* Build a list of currently loaded shared objects.  See solib-svr4.c.  */
00244 
00245 static struct so_list *
00246 darwin_current_sos (void)
00247 {
00248   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
00249   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
00250   int ptr_len = TYPE_LENGTH (ptr_type);
00251   unsigned int image_info_size;
00252   struct so_list *head = NULL;
00253   struct so_list *tail = NULL;
00254   int i;
00255   struct darwin_info *info = get_darwin_info ();
00256 
00257   /* Be sure image infos are loaded.  */
00258   darwin_load_image_infos (info);
00259 
00260   if (!darwin_dyld_version_ok (info))
00261     return NULL;
00262 
00263   image_info_size = ptr_len * 3;
00264 
00265   /* Read infos for each solib.
00266      The first entry was rumored to be the executable itself, but this is not
00267      true when a large number of shared libraries are used (table expanded ?).
00268      We now check all entries, but discard executable images.  */
00269   for (i = 0; i < info->all_image.count; i++)
00270     {
00271       CORE_ADDR iinfo = info->all_image.info + i * image_info_size;
00272       gdb_byte buf[image_info_size];
00273       CORE_ADDR load_addr;
00274       CORE_ADDR path_addr;
00275       struct mach_o_header_external hdr;
00276       unsigned long hdr_val;
00277       char *file_path;
00278       int errcode;
00279       struct darwin_so_list *dnew;
00280       struct so_list *new;
00281       struct cleanup *old_chain;
00282 
00283       /* Read image info from inferior.  */
00284       if (target_read_memory (iinfo, buf, image_info_size))
00285         break;
00286 
00287       load_addr = extract_typed_address (buf, ptr_type);
00288       path_addr = extract_typed_address (buf + ptr_len, ptr_type);
00289 
00290       /* Read Mach-O header from memory.  */
00291       if (target_read_memory (load_addr, (gdb_byte *) &hdr, sizeof (hdr) - 4))
00292         break;
00293       /* Discard wrong magic numbers.  Shouldn't happen.  */
00294       hdr_val = extract_unsigned_integer
00295         (hdr.magic, sizeof (hdr.magic), byte_order);
00296       if (hdr_val != BFD_MACH_O_MH_MAGIC && hdr_val != BFD_MACH_O_MH_MAGIC_64)
00297         continue;
00298       /* Discard executable.  Should happen only once.  */
00299       hdr_val = extract_unsigned_integer
00300         (hdr.filetype, sizeof (hdr.filetype), byte_order);
00301       if (hdr_val == BFD_MACH_O_MH_EXECUTE)
00302         continue;
00303 
00304       target_read_string (path_addr, &file_path,
00305                           SO_NAME_MAX_PATH_SIZE - 1, &errcode);
00306       if (errcode)
00307         break;
00308 
00309       /* Create and fill the new so_list element.  */
00310       dnew = XZALLOC (struct darwin_so_list);
00311       new = &dnew->sl;
00312       old_chain = make_cleanup (xfree, dnew);
00313 
00314       new->lm_info = &dnew->li;
00315 
00316       strncpy (new->so_name, file_path, SO_NAME_MAX_PATH_SIZE - 1);
00317       new->so_name[SO_NAME_MAX_PATH_SIZE - 1] = '\0';
00318       strcpy (new->so_original_name, new->so_name);
00319       xfree (file_path);
00320       new->lm_info->lm_addr = load_addr;
00321 
00322       if (head == NULL)
00323         head = new;
00324       else
00325         tail->next = new;
00326       tail = new;
00327 
00328       discard_cleanups (old_chain);
00329     }
00330 
00331   return head;
00332 }
00333 
00334 /* Get the load address of the executable.  We assume that the dyld info are
00335    correct.  */
00336 
00337 static CORE_ADDR
00338 darwin_read_exec_load_addr (struct darwin_info *info)
00339 {
00340   struct type *ptr_type = builtin_type (target_gdbarch ())->builtin_data_ptr;
00341   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
00342   int ptr_len = TYPE_LENGTH (ptr_type);
00343   unsigned int image_info_size = ptr_len * 3;
00344   int i;
00345 
00346   /* Read infos for each solib.  One of them should be the executable.  */
00347   for (i = 0; i < info->all_image.count; i++)
00348     {
00349       CORE_ADDR iinfo = info->all_image.info + i * image_info_size;
00350       gdb_byte buf[image_info_size];
00351       CORE_ADDR load_addr;
00352       struct mach_o_header_external hdr;
00353       unsigned long hdr_val;
00354 
00355       /* Read image info from inferior.  */
00356       if (target_read_memory (iinfo, buf, image_info_size))
00357         break;
00358 
00359       load_addr = extract_typed_address (buf, ptr_type);
00360 
00361       /* Read Mach-O header from memory.  */
00362       if (target_read_memory (load_addr, (gdb_byte *) &hdr, sizeof (hdr) - 4))
00363         break;
00364       /* Discard wrong magic numbers.  Shouldn't happen.  */
00365       hdr_val = extract_unsigned_integer
00366         (hdr.magic, sizeof (hdr.magic), byte_order);
00367       if (hdr_val != BFD_MACH_O_MH_MAGIC && hdr_val != BFD_MACH_O_MH_MAGIC_64)
00368         continue;
00369       /* Check executable.  */
00370       hdr_val = extract_unsigned_integer
00371         (hdr.filetype, sizeof (hdr.filetype), byte_order);
00372       if (hdr_val == BFD_MACH_O_MH_EXECUTE)
00373         return load_addr;
00374     }
00375 
00376   return 0;
00377 }
00378 
00379 /* Return 1 if PC lies in the dynamic symbol resolution code of the
00380    run time loader.  */
00381 
00382 static int
00383 darwin_in_dynsym_resolve_code (CORE_ADDR pc)
00384 {
00385   return 0;
00386 }
00387 
00388 
00389 /* No special symbol handling.  */
00390 
00391 static void
00392 darwin_special_symbol_handling (void)
00393 {
00394 }
00395 
00396 /* A wrapper for bfd_mach_o_fat_extract that handles reference
00397    counting properly.  This will either return NULL, or return a new
00398    reference to a BFD.  */
00399 
00400 static bfd *
00401 gdb_bfd_mach_o_fat_extract (bfd *abfd, bfd_format format,
00402                             const bfd_arch_info_type *arch)
00403 {
00404   bfd *result = bfd_mach_o_fat_extract (abfd, format, arch);
00405 
00406   if (result == NULL)
00407     return NULL;
00408 
00409   if (result == abfd)
00410     gdb_bfd_ref (result);
00411   else
00412     gdb_bfd_mark_parent (result, abfd);
00413 
00414   return result;
00415 }
00416 
00417 /* Extract dyld_all_image_addr when the process was just created, assuming the
00418    current PC is at the entry of the dynamic linker.  */
00419 
00420 static void
00421 darwin_solib_get_all_image_info_addr_at_init (struct darwin_info *info)
00422 {
00423   char *interp_name;
00424   CORE_ADDR load_addr = 0;
00425   bfd *dyld_bfd = NULL;
00426   struct cleanup *cleanup;
00427 
00428   /* This method doesn't work with an attached process.  */
00429   if (current_inferior ()->attach_flag)
00430     return;
00431 
00432   /* Find the program interpreter.  */
00433   interp_name = find_program_interpreter ();
00434   if (!interp_name)
00435     return;
00436 
00437   cleanup = make_cleanup (null_cleanup, NULL);
00438 
00439   /* Create a bfd for the interpreter.  */
00440   dyld_bfd = gdb_bfd_open (interp_name, gnutarget, -1);
00441   if (dyld_bfd)
00442     {
00443       bfd *sub;
00444 
00445       make_cleanup_bfd_unref (dyld_bfd);
00446       sub = gdb_bfd_mach_o_fat_extract (dyld_bfd, bfd_object,
00447                                         gdbarch_bfd_arch_info (target_gdbarch ()));
00448       if (sub)
00449         {
00450           dyld_bfd = sub;
00451           make_cleanup_bfd_unref (sub);
00452         }
00453       else
00454         dyld_bfd = NULL;
00455     }
00456   if (!dyld_bfd)
00457     {
00458       do_cleanups (cleanup);
00459       return;
00460     }
00461 
00462   /* We find the dynamic linker's base address by examining
00463      the current pc (which should point at the entry point for the
00464      dynamic linker) and subtracting the offset of the entry point.  */
00465   load_addr = (regcache_read_pc (get_current_regcache ())
00466                - bfd_get_start_address (dyld_bfd));
00467 
00468   /* Now try to set a breakpoint in the dynamic linker.  */
00469   info->all_image_addr =
00470     lookup_symbol_from_bfd (dyld_bfd, "_dyld_all_image_infos");
00471 
00472   do_cleanups (cleanup);
00473 
00474   if (info->all_image_addr == 0)
00475     return;
00476 
00477   info->all_image_addr += load_addr;
00478 }
00479 
00480 /* Extract dyld_all_image_addr reading it from 
00481    TARGET_OBJECT_DARWIN_DYLD_INFO.  */
00482 
00483 static void
00484 darwin_solib_read_all_image_info_addr (struct darwin_info *info)
00485 {
00486   gdb_byte buf[8 + 8 + 4];
00487   LONGEST len;
00488   enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
00489 
00490   len = target_read (&current_target, TARGET_OBJECT_DARWIN_DYLD_INFO, NULL,
00491                      buf, 0, sizeof (buf));
00492   if (len != sizeof (buf))
00493     return;
00494 
00495   info->all_image_addr = extract_unsigned_integer (buf, 8, byte_order);
00496 }
00497 
00498 /* Shared library startup support.  See documentation in solib-svr4.c.  */
00499 
00500 static void
00501 darwin_solib_create_inferior_hook (int from_tty)
00502 {
00503   struct darwin_info *info = get_darwin_info ();
00504   CORE_ADDR load_addr;
00505 
00506   info->all_image_addr = 0;
00507 
00508   darwin_solib_read_all_image_info_addr (info);
00509 
00510   if (info->all_image_addr == 0)
00511     darwin_solib_get_all_image_info_addr_at_init (info);
00512 
00513   if (info->all_image_addr == 0)
00514     return;
00515 
00516   darwin_load_image_infos (info);
00517 
00518   if (!darwin_dyld_version_ok (info))
00519     return;
00520 
00521   create_solib_event_breakpoint (target_gdbarch (), info->all_image.notifier);
00522 
00523   /* Possible relocate the main executable (PIE).  */
00524   load_addr = darwin_read_exec_load_addr (info);
00525   if (load_addr != 0 && symfile_objfile != NULL)
00526     {
00527       CORE_ADDR vmaddr = 0;
00528       struct mach_o_data_struct *md = bfd_mach_o_get_data (exec_bfd);
00529       unsigned int i, num;
00530 
00531       /* Find the base address of the executable.  */
00532       for (i = 0; i < md->header.ncmds; i++)
00533         {
00534           struct bfd_mach_o_load_command *cmd = &md->commands[i];
00535 
00536           if (cmd->type != BFD_MACH_O_LC_SEGMENT
00537               && cmd->type != BFD_MACH_O_LC_SEGMENT_64)
00538             continue;
00539           if (cmd->command.segment.fileoff == 0
00540               && cmd->command.segment.vmaddr != 0
00541               && cmd->command.segment.filesize != 0)
00542             {
00543               vmaddr = cmd->command.segment.vmaddr;
00544               break;
00545             }
00546         }
00547 
00548       /* Relocate.  */
00549       if (vmaddr != load_addr)
00550         objfile_rebase (symfile_objfile, load_addr - vmaddr);
00551     }
00552 }
00553 
00554 static void
00555 darwin_clear_solib (void)
00556 {
00557   struct darwin_info *info = get_darwin_info ();
00558 
00559   info->all_image_addr = 0;
00560   info->all_image.version = 0;
00561 }
00562 
00563 static void
00564 darwin_free_so (struct so_list *so)
00565 {
00566 }
00567 
00568 /* The section table is built from bfd sections using bfd VMAs.
00569    Relocate these VMAs according to solib info.  */
00570 
00571 static void
00572 darwin_relocate_section_addresses (struct so_list *so,
00573                                    struct target_section *sec)
00574 {
00575   sec->addr += so->lm_info->lm_addr;
00576   sec->endaddr += so->lm_info->lm_addr;
00577 
00578   /* Best effort to set addr_high/addr_low.  This is used only by
00579      'info sharedlibary'.  */
00580   if (so->addr_high == 0)
00581     {
00582       so->addr_low = sec->addr;
00583       so->addr_high = sec->endaddr;
00584     }
00585   if (sec->endaddr > so->addr_high)
00586     so->addr_high = sec->endaddr;
00587   if (sec->addr < so->addr_low)
00588     so->addr_low = sec->addr;
00589 }
00590 
00591 static struct symbol *
00592 darwin_lookup_lib_symbol (const struct objfile *objfile,
00593                           const char *name,
00594                           const domain_enum domain)
00595 {
00596   return NULL;
00597 }
00598 
00599 static bfd *
00600 darwin_bfd_open (char *pathname)
00601 {
00602   char *found_pathname;
00603   int found_file;
00604   bfd *abfd;
00605   bfd *res;
00606 
00607   /* Search for shared library file.  */
00608   found_pathname = solib_find (pathname, &found_file);
00609   if (found_pathname == NULL)
00610     perror_with_name (pathname);
00611 
00612   /* Open bfd for shared library.  */
00613   abfd = solib_bfd_fopen (found_pathname, found_file);
00614 
00615   res = gdb_bfd_mach_o_fat_extract (abfd, bfd_object,
00616                                     gdbarch_bfd_arch_info (target_gdbarch ()));
00617   if (!res)
00618     {
00619       make_cleanup_bfd_unref (abfd);
00620       error (_("`%s': not a shared-library: %s"),
00621              bfd_get_filename (abfd), bfd_errmsg (bfd_get_error ()));
00622     }
00623 
00624   gdb_bfd_unref (abfd);
00625   return res;
00626 }
00627 
00628 struct target_so_ops darwin_so_ops;
00629 
00630 /* -Wmissing-prototypes */
00631 extern initialize_file_ftype _initialize_darwin_solib;
00632 
00633 void
00634 _initialize_darwin_solib (void)
00635 {
00636   solib_darwin_pspace_data
00637     = register_program_space_data_with_cleanup (NULL,
00638                                                 darwin_pspace_data_cleanup);
00639 
00640   darwin_so_ops.relocate_section_addresses = darwin_relocate_section_addresses;
00641   darwin_so_ops.free_so = darwin_free_so;
00642   darwin_so_ops.clear_solib = darwin_clear_solib;
00643   darwin_so_ops.solib_create_inferior_hook = darwin_solib_create_inferior_hook;
00644   darwin_so_ops.special_symbol_handling = darwin_special_symbol_handling;
00645   darwin_so_ops.current_sos = darwin_current_sos;
00646   darwin_so_ops.open_symbol_file_object = open_symbol_file_object;
00647   darwin_so_ops.in_dynsym_resolve_code = darwin_in_dynsym_resolve_code;
00648   darwin_so_ops.lookup_lib_global_symbol = darwin_lookup_lib_symbol;
00649   darwin_so_ops.bfd_open = darwin_bfd_open;
00650 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines