gdb/machoread.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* Darwin support for GDB, the GNU debugger.
  2.    Copyright (C) 2008-2015 Free Software Foundation, Inc.

  3.    Contributed by AdaCore.

  4.    This file is part of GDB.

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

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

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

  15. #include "defs.h"
  16. #include "symtab.h"
  17. #include "gdbtypes.h"
  18. #include "bfd.h"
  19. #include "symfile.h"
  20. #include "objfiles.h"
  21. #include "buildsym.h"
  22. #include "gdbcmd.h"
  23. #include "gdbcore.h"
  24. #include "mach-o.h"
  25. #include "aout/stab_gnu.h"
  26. #include "vec.h"
  27. #include "psympriv.h"
  28. #include "complaints.h"
  29. #include "gdb_bfd.h"

  30. /* If non-zero displays debugging message.  */
  31. static unsigned int mach_o_debug_level = 0;

  32. /* Dwarf debugging information are never in the final executable.  They stay
  33.    in object files and the executable contains the list of object files read
  34.    during the link.
  35.    Each time an oso (other source) is found in the executable, the reader
  36.    creates such a structure.  They are read after the processing of the
  37.    executable.  */

  38. typedef struct oso_el
  39. {
  40.   /* Object file name.  Can also be a member name.  */
  41.   const char *name;

  42.   /* Associated time stamp.  */
  43.   unsigned long mtime;

  44.   /* Stab symbols range for this OSO.  */
  45.   asymbol **oso_sym;
  46.   asymbol **end_sym;

  47.   /* Number of interesting stabs in the range.  */
  48.   unsigned int nbr_syms;
  49. }
  50. oso_el;

  51. /* Vector of object files to be read after the executable.  */
  52. DEF_VEC_O (oso_el);

  53. static void
  54. macho_new_init (struct objfile *objfile)
  55. {
  56. }

  57. static void
  58. macho_symfile_init (struct objfile *objfile)
  59. {
  60.   objfile->flags |= OBJF_REORDERED;
  61. }

  62. /*  Add a new OSO to the vector of OSO to load.  */

  63. static void
  64. macho_register_oso (VEC (oso_el) **oso_vector_ptr,
  65.                     struct objfile *objfile,
  66.                     asymbol **oso_sym, asymbol **end_sym,
  67.                     unsigned int nbr_syms)
  68. {
  69.   oso_el el;

  70.   el.name = (*oso_sym)->name;
  71.   el.mtime = (*oso_sym)->value;
  72.   el.oso_sym = oso_sym;
  73.   el.end_sym = end_sym;
  74.   el.nbr_syms = nbr_syms;
  75.   VEC_safe_push (oso_el, *oso_vector_ptr, &el);
  76. }

  77. /* Add symbol SYM to the minimal symbol table of OBJFILE.  */

  78. static void
  79. macho_symtab_add_minsym (struct objfile *objfile, const asymbol *sym)
  80. {
  81.   if (sym->name == NULL || *sym->name == '\0')
  82.     {
  83.       /* Skip names that don't exist (shouldn't happen), or names
  84.          that are null strings (may happen).  */
  85.       return;
  86.     }

  87.   if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK))
  88.     {
  89.       CORE_ADDR symaddr;
  90.       enum minimal_symbol_type ms_type;

  91.       /* Bfd symbols are section relative.  */
  92.       symaddr = sym->value + sym->section->vma;

  93.       if (sym->section == bfd_abs_section_ptr)
  94.         ms_type = mst_abs;
  95.       else if (sym->section->flags & SEC_CODE)
  96.         {
  97.           if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
  98.             ms_type = mst_text;
  99.           else
  100.             ms_type = mst_file_text;
  101.         }
  102.       else if (sym->section->flags & SEC_ALLOC)
  103.         {
  104.           if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
  105.             {
  106.               if (sym->section->flags & SEC_LOAD)
  107.                 ms_type = mst_data;
  108.               else
  109.                 ms_type = mst_bss;
  110.             }
  111.           else if (sym->flags & BSF_LOCAL)
  112.             {
  113.               /* Not a special stabs-in-elf symbol, do regular
  114.                  symbol processing.  */
  115.               if (sym->section->flags & SEC_LOAD)
  116.                 ms_type = mst_file_data;
  117.               else
  118.                 ms_type = mst_file_bss;
  119.             }
  120.           else
  121.             ms_type = mst_unknown;
  122.         }
  123.       else
  124.         return;        /* Skip this symbol.  */

  125.       prim_record_minimal_symbol_and_info
  126.         (sym->name, symaddr, ms_type,
  127.          gdb_bfd_section_index (objfile->obfd, sym->section),
  128.          objfile);
  129.     }
  130. }

  131. /* Build the minimal symbol table from SYMBOL_TABLE of length
  132.    NUMBER_OF_SYMBOLS for OBJFILE.  Registers OSO filenames found.  */

  133. static void
  134. macho_symtab_read (struct objfile *objfile,
  135.                    long number_of_symbols, asymbol **symbol_table,
  136.                    VEC (oso_el) **oso_vector_ptr)
  137. {
  138.   long i;
  139.   const asymbol *dir_so = NULL;
  140.   const asymbol *file_so = NULL;
  141.   asymbol **oso_file = NULL;
  142.   unsigned int nbr_syms = 0;

  143.   /* Current state while reading stabs.  */
  144.   enum
  145.   {
  146.     /* Not within an SO part.  Only non-debugging symbols should be present,
  147.        and will be added to the minimal symbols table.  */
  148.     S_NO_SO,

  149.     /* First SO read.  Introduce an SO section, and may be followed by a second
  150.        SO.  The SO section should contain onl debugging symbols.  */
  151.     S_FIRST_SO,

  152.     /* Second non-null SO found, just after the first one.  Means that the first
  153.        is in fact a directory name.  */
  154.     S_SECOND_SO,

  155.     /* Non-null OSO found.  Debugging info are DWARF in this OSO file.  */
  156.     S_DWARF_FILE,

  157.     S_STAB_FILE
  158.   } state = S_NO_SO;

  159.   for (i = 0; i < number_of_symbols; i++)
  160.     {
  161.       const asymbol *sym = symbol_table[i];
  162.       bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;

  163.       switch (state)
  164.         {
  165.         case S_NO_SO:
  166.           if (mach_o_sym->n_type == N_SO)
  167.             {
  168.               /* Start of object stab.  */
  169.               if (sym->name == NULL || sym->name[0] == 0)
  170.                 {
  171.                   /* Unexpected empty N_SO.  */
  172.                   complaint (&symfile_complaints,
  173.                              _("Unexpected empty N_SO stab"));
  174.                 }
  175.               else
  176.                 {
  177.                   file_so = sym;
  178.                   dir_so = NULL;
  179.                   state = S_FIRST_SO;
  180.                 }
  181.             }
  182.           else if (sym->flags & BSF_DEBUGGING)
  183.             {
  184.               if (mach_o_sym->n_type == N_OPT)
  185.                 {
  186.                   /* No complaint for OPT.  */
  187.                   break;
  188.                 }

  189.               /* Debugging symbols are not expected here.  */
  190.               complaint (&symfile_complaints,
  191.                          _("%s: Unexpected debug stab outside SO markers"),
  192.                          objfile_name (objfile));
  193.             }
  194.           else
  195.             {
  196.               /* Non-debugging symbols go to the minimal symbol table.  */
  197.               macho_symtab_add_minsym (objfile, sym);
  198.             }
  199.           break;

  200.         case S_FIRST_SO:
  201.         case S_SECOND_SO:
  202.           if (mach_o_sym->n_type == N_SO)
  203.             {
  204.               if (sym->name == NULL || sym->name[0] == 0)
  205.                 {
  206.                   /* Unexpected empty N_SO.  */
  207.                   complaint (&symfile_complaints, _("Empty SO section"));
  208.                   state = S_NO_SO;
  209.                 }
  210.               else if (state == S_FIRST_SO)
  211.                 {
  212.                   /* Second SO stab for the file name.  */
  213.                   dir_so = file_so;
  214.                   file_so = sym;
  215.                   state = S_SECOND_SO;
  216.                 }
  217.               else
  218.                 complaint (&symfile_complaints, _("Three SO in a raw"));
  219.             }
  220.           else if (mach_o_sym->n_type == N_OSO)
  221.             {
  222.               if (sym->name == NULL || sym->name[0] == 0)
  223.                 {
  224.                   /* Empty OSO.  Means that this file was compiled with
  225.                      stabs.  */
  226.                   state = S_STAB_FILE;
  227.                   warning (_("stabs debugging not supported for %s"),
  228.                            file_so->name);
  229.                 }
  230.               else
  231.                 {
  232.                   /* Non-empty OSO for a Dwarf file.  */
  233.                   oso_file = symbol_table + i;
  234.                   nbr_syms = 0;
  235.                   state = S_DWARF_FILE;
  236.                 }
  237.             }
  238.           else
  239.             complaint (&symfile_complaints,
  240.                        _("Unexpected stab after SO"));
  241.           break;

  242.         case S_STAB_FILE:
  243.         case S_DWARF_FILE:
  244.           if (mach_o_sym->n_type == N_SO)
  245.             {
  246.               if (sym->name == NULL || sym->name[0] == 0)
  247.                 {
  248.                   /* End of file.  */
  249.                   if (state == S_DWARF_FILE)
  250.                     macho_register_oso (oso_vector_ptr, objfile,
  251.                                         oso_file, symbol_table + i,
  252.                                         nbr_syms);
  253.                   state = S_NO_SO;
  254.                 }
  255.               else
  256.                 {
  257.                   complaint (&symfile_complaints, _("Missing nul SO"));
  258.                   file_so = sym;
  259.                   dir_so = NULL;
  260.                   state = S_FIRST_SO;
  261.                 }
  262.             }
  263.           else if (sym->flags & BSF_DEBUGGING)
  264.             {
  265.               if (state == S_STAB_FILE)
  266.                 {
  267.                   /* FIXME: to be implemented.  */
  268.                 }
  269.               else
  270.                 {
  271.                   switch (mach_o_sym->n_type)
  272.                     {
  273.                     case N_FUN:
  274.                       if (sym->name == NULL || sym->name[0] == 0)
  275.                         break;
  276.                       /* Fall through.  */
  277.                     case N_STSYM:
  278.                       /* Interesting symbol.  */
  279.                       nbr_syms++;
  280.                       break;
  281.                     case N_ENSYM:
  282.                     case N_BNSYM:
  283.                     case N_GSYM:
  284.                       break;
  285.                     default:
  286.                       complaint (&symfile_complaints,
  287.                                  _("unhandled stab for dwarf OSO file"));
  288.                       break;
  289.                     }
  290.                 }
  291.             }
  292.           else
  293.             complaint (&symfile_complaints,
  294.                        _("non-debugging symbol within SO"));
  295.           break;
  296.         }
  297.     }

  298.   if (state != S_NO_SO)
  299.     complaint (&symfile_complaints, _("missing nul SO"));
  300. }

  301. /* If NAME describes an archive member (ie: ARCHIVE '(' MEMBER ')'),
  302.    returns the length of the archive name.
  303.    Returns -1 otherwise.  */

  304. static int
  305. get_archive_prefix_len (const char *name)
  306. {
  307.   char *lparen;
  308.   int name_len = strlen (name);

  309.   if (name_len == 0 || name[name_len - 1] != ')')
  310.     return -1;

  311.   lparen = strrchr (name, '(');
  312.   if (lparen == NULL || lparen == name)
  313.     return -1;
  314.   return lparen - name;
  315. }

  316. /* Compare function to qsort OSOs, so that members of a library are
  317.    gathered.  */

  318. static int
  319. oso_el_compare_name (const void *vl, const void *vr)
  320. {
  321.   const oso_el *l = (const oso_el *)vl;
  322.   const oso_el *r = (const oso_el *)vr;

  323.   return strcmp (l->name, r->name);
  324. }

  325. /* Hash table entry structure for the stabs symbols in the main object file.
  326.    This is used to speed up lookup for symbols in the OSO.  */

  327. struct macho_sym_hash_entry
  328. {
  329.   struct bfd_hash_entry base;
  330.   const asymbol *sym;
  331. };

  332. /* Routine to create an entry in the hash table.  */

  333. static struct bfd_hash_entry *
  334. macho_sym_hash_newfunc (struct bfd_hash_entry *entry,
  335.                         struct bfd_hash_table *table,
  336.                         const char *string)
  337. {
  338.   struct macho_sym_hash_entry *ret = (struct macho_sym_hash_entry *) entry;

  339.   /* Allocate the structure if it has not already been allocated by a
  340.      subclass.  */
  341.   if (ret == NULL)
  342.     ret = (struct macho_sym_hash_entry *) bfd_hash_allocate (table,
  343.                                                              sizeof (* ret));
  344.   if (ret == NULL)
  345.     return NULL;

  346.   /* Call the allocation method of the superclass.  */
  347.   ret = (struct macho_sym_hash_entry *)
  348.          bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);

  349.   if (ret)
  350.     {
  351.       /* Initialize the local fields.  */
  352.       ret->sym = NULL;
  353.     }

  354.   return (struct bfd_hash_entry *) ret;
  355. }

  356. /* Get the value of SYM from the minimal symtab of MAIN_OBJFILE.  This is used
  357.    to get the value of global and common symbols.  */

  358. static CORE_ADDR
  359. macho_resolve_oso_sym_with_minsym (struct objfile *main_objfile, asymbol *sym)
  360. {
  361.   /* For common symbol and global symbols, use the min symtab.  */
  362.   struct bound_minimal_symbol msym;
  363.   const char *name = sym->name;

  364.   if (name[0] == bfd_get_symbol_leading_char (main_objfile->obfd))
  365.     ++name;
  366.   msym = lookup_minimal_symbol (name, NULL, main_objfile);
  367.   if (msym.minsym == NULL)
  368.     {
  369.       warning (_("can't find symbol '%s' in minsymtab"), name);
  370.       return 0;
  371.     }
  372.   else
  373.     return BMSYMBOL_VALUE_ADDRESS (msym);
  374. }

  375. /* Add oso file OSO/ABFD as a symbol file.  */

  376. static void
  377. macho_add_oso_symfile (oso_el *oso, bfd *abfd, const char *name,
  378.                        struct objfile *main_objfile, int symfile_flags)
  379. {
  380.   int storage;
  381.   int i;
  382.   asymbol **symbol_table;
  383.   asymbol **symp;
  384.   struct bfd_hash_table table;
  385.   int nbr_sections;
  386.   struct cleanup *cleanup;

  387.   /* Per section flag to mark which section have been rebased.  */
  388.   unsigned char *sections_rebased;

  389.   if (mach_o_debug_level > 0)
  390.     printf_unfiltered
  391.       (_("Loading debugging symbols from oso: %s\n"), oso->name);

  392.   if (!bfd_check_format (abfd, bfd_object))
  393.     {
  394.       warning (_("`%s': can't read symbols: %s."), oso->name,
  395.                bfd_errmsg (bfd_get_error ()));
  396.       gdb_bfd_unref (abfd);
  397.       return;
  398.     }

  399.   if (abfd->my_archive == NULL && oso->mtime != bfd_get_mtime (abfd))
  400.     {
  401.       warning (_("`%s': file time stamp mismatch."), oso->name);
  402.       gdb_bfd_unref (abfd);
  403.       return;
  404.     }

  405.   if (!bfd_hash_table_init_n (&table, macho_sym_hash_newfunc,
  406.                               sizeof (struct macho_sym_hash_entry),
  407.                               oso->nbr_syms))
  408.     {
  409.       warning (_("`%s': can't create hash table"), oso->name);
  410.       gdb_bfd_unref (abfd);
  411.       return;
  412.     }

  413.   bfd_set_cacheable (abfd, 1);

  414.   /* Read symbols table.  */
  415.   storage = bfd_get_symtab_upper_bound (abfd);
  416.   symbol_table = (asymbol **) xmalloc (storage);
  417.   bfd_canonicalize_symtab (abfd, symbol_table);

  418.   /* Init section flags.  */
  419.   nbr_sections = bfd_count_sections (abfd);
  420.   sections_rebased = (unsigned char *) alloca (nbr_sections);
  421.   for (i = 0; i < nbr_sections; i++)
  422.     sections_rebased[i] = 0;

  423.   /* Put symbols for the OSO file in the hash table.  */
  424.   for (symp = oso->oso_sym; symp != oso->end_sym; symp++)
  425.     {
  426.       const asymbol *sym = *symp;
  427.       bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;

  428.       switch (mach_o_sym->n_type)
  429.         {
  430.         case N_ENSYM:
  431.         case N_BNSYM:
  432.         case N_GSYM:
  433.           sym = NULL;
  434.           break;
  435.         case N_FUN:
  436.           if (sym->name == NULL || sym->name[0] == 0)
  437.             sym = NULL;
  438.           break;
  439.         case N_STSYM:
  440.           break;
  441.         default:
  442.           sym = NULL;
  443.           break;
  444.         }
  445.       if (sym != NULL)
  446.         {
  447.           struct macho_sym_hash_entry *ent;

  448.           ent = (struct macho_sym_hash_entry *)
  449.             bfd_hash_lookup (&table, sym->name, TRUE, FALSE);
  450.           if (ent->sym != NULL)
  451.             complaint (&symfile_complaints,
  452.                        _("Duplicated symbol %s in symbol table"), sym->name);
  453.           else
  454.             {
  455.               if (mach_o_debug_level > 4)
  456.                 {
  457.                   struct gdbarch *arch = get_objfile_arch (main_objfile);
  458.                   printf_unfiltered
  459.                     (_("Adding symbol %s (addr: %s)\n"),
  460.                      sym->name, paddress (arch, sym->value));
  461.                 }
  462.               ent->sym = sym;
  463.             }
  464.         }
  465.     }

  466.   /* Relocate symbols of the OSO.  */
  467.   for (i = 0; symbol_table[i]; i++)
  468.     {
  469.       asymbol *sym = symbol_table[i];
  470.       bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;

  471.       if (mach_o_sym->n_type & BFD_MACH_O_N_STAB)
  472.         continue;
  473.       if ((mach_o_sym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_UNDF
  474.            && sym->value != 0)
  475.         {
  476.           /* For common symbol use the min symtab and modify the OSO
  477.              symbol table.  */
  478.           CORE_ADDR res;

  479.           res = macho_resolve_oso_sym_with_minsym (main_objfile, sym);
  480.           if (res != 0)
  481.             {
  482.               sym->section = bfd_com_section_ptr;
  483.               sym->value = res;
  484.             }
  485.         }
  486.       else if ((mach_o_sym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
  487.         {
  488.           /* Normal symbol.  */
  489.           asection *sec = sym->section;
  490.           bfd_mach_o_section *msec;
  491.           unsigned int sec_type;

  492.           /* Skip buggy ones.  */
  493.           if (sec == NULL || sections_rebased[sec->index] != 0)
  494.             continue;

  495.           /* Only consider regular, non-debugging sections.  */
  496.           msec = bfd_mach_o_get_mach_o_section (sec);
  497.           sec_type = msec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
  498.           if ((sec_type == BFD_MACH_O_S_REGULAR
  499.                || sec_type == BFD_MACH_O_S_ZEROFILL)
  500.               && (msec->flags & BFD_MACH_O_S_ATTR_DEBUG) == 0)
  501.             {
  502.               CORE_ADDR addr = 0;

  503.               if ((mach_o_sym->n_type & BFD_MACH_O_N_EXT) != 0)
  504.                 {
  505.                   /* Use the min symtab for global symbols.  */
  506.                   addr = macho_resolve_oso_sym_with_minsym (main_objfile, sym);
  507.                 }
  508.               else
  509.                 {
  510.                   struct macho_sym_hash_entry *ent;

  511.                   ent = (struct macho_sym_hash_entry *)
  512.                     bfd_hash_lookup (&table, sym->name, FALSE, FALSE);
  513.                   if (ent != NULL)
  514.                     addr = bfd_asymbol_value (ent->sym);
  515.                 }

  516.               /* Adjust the section.  */
  517.               if (addr != 0)
  518.                 {
  519.                   CORE_ADDR res = addr - sym->value;

  520.                   if (mach_o_debug_level > 3)
  521.                     {
  522.                       struct gdbarch *arch = get_objfile_arch (main_objfile);
  523.                       printf_unfiltered
  524.                         (_("resolve sect %s with %s (set to %s)\n"),
  525.                          sec->name, sym->name,
  526.                          paddress (arch, res));
  527.                     }
  528.                   bfd_set_section_vma (abfd, sec, res);
  529.                   sections_rebased[sec->index] = 1;
  530.                 }
  531.             }
  532.           else
  533.             {
  534.               /* Mark the section as never rebased.  */
  535.               sections_rebased[sec->index] = 2;
  536.             }
  537.         }
  538.     }

  539.   bfd_hash_table_free (&table);

  540.   /* We need to clear SYMFILE_MAINLINE to avoid interractive question
  541.      from symfile.c:symbol_file_add_with_addrs_or_offsets.  */
  542.   cleanup = make_cleanup_bfd_unref (abfd);
  543.   symbol_file_add_from_bfd
  544.     (abfd, name, symfile_flags & ~(SYMFILE_MAINLINE | SYMFILE_VERBOSE), NULL,
  545.      main_objfile->flags & (OBJF_REORDERED | OBJF_SHARED
  546.                             | OBJF_READNOW | OBJF_USERLOADED),
  547.      main_objfile);
  548.   do_cleanups (cleanup);
  549. }

  550. /* Read symbols from the vector of oso files.

  551.    Note that this function sorts OSO_VECTOR_PTR.  */

  552. static void
  553. macho_symfile_read_all_oso (VEC (oso_el) **oso_vector_ptr,
  554.                             struct objfile *main_objfile,
  555.                             int symfile_flags)
  556. {
  557.   int ix;
  558.   VEC (oso_el) *vec = *oso_vector_ptr;
  559.   oso_el *oso;
  560.   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);

  561.   /* Sort oso by name so that files from libraries are gathered.  */
  562.   qsort (VEC_address (oso_el, vec), VEC_length (oso_el, vec),
  563.          sizeof (oso_el), oso_el_compare_name);

  564.   for (ix = 0; VEC_iterate (oso_el, vec, ix, oso);)
  565.     {
  566.       int pfx_len;

  567.       /* Check if this is a library name.  */
  568.       pfx_len = get_archive_prefix_len (oso->name);
  569.       if (pfx_len > 0)
  570.         {
  571.           bfd *archive_bfd;
  572.           bfd *member_bfd;
  573.           char *archive_name = XNEWVEC (char, pfx_len + 1);
  574.           int last_ix;
  575.           oso_el *oso2;
  576.           int ix2;

  577.           memcpy (archive_name, oso->name, pfx_len);
  578.           archive_name[pfx_len] = '\0';

  579.           make_cleanup (xfree, archive_name);

  580.           /* Compute number of oso for this archive.  */
  581.           for (last_ix = ix;
  582.                VEC_iterate (oso_el, vec, last_ix, oso2); last_ix++)
  583.             {
  584.               if (strncmp (oso2->name, archive_name, pfx_len) != 0)
  585.                 break;
  586.             }

  587.           /* Open the archive and check the format.  */
  588.           archive_bfd = gdb_bfd_open (archive_name, gnutarget, -1);
  589.           if (archive_bfd == NULL)
  590.             {
  591.               warning (_("Could not open OSO archive file \"%s\""),
  592.                        archive_name);
  593.               ix = last_ix;
  594.               continue;
  595.             }
  596.           if (!bfd_check_format (archive_bfd, bfd_archive))
  597.             {
  598.               warning (_("OSO archive file \"%s\" not an archive."),
  599.                        archive_name);
  600.               gdb_bfd_unref (archive_bfd);
  601.               ix = last_ix;
  602.               continue;
  603.             }

  604.           member_bfd = gdb_bfd_openr_next_archived_file (archive_bfd, NULL);

  605.           if (member_bfd == NULL)
  606.             {
  607.               warning (_("Could not read archive members out of "
  608.                          "OSO archive \"%s\""), archive_name);
  609.               gdb_bfd_unref (archive_bfd);
  610.               ix = last_ix;
  611.               continue;
  612.             }

  613.           /* Load all oso in this library.  */
  614.           while (member_bfd != NULL)
  615.             {
  616.               bfd *prev;
  617.               const char *member_name = member_bfd->filename;
  618.               int member_len = strlen (member_name);

  619.               /* If this member is referenced, add it as a symfile.  */
  620.               for (ix2 = ix; ix2 < last_ix; ix2++)
  621.                 {
  622.                   oso2 = VEC_index (oso_el, vec, ix2);

  623.                   if (oso2->name
  624.                       && strlen (oso2->name) == pfx_len + member_len + 2
  625.                       && !memcmp (member_name, oso2->name + pfx_len + 1,
  626.                                   member_len))
  627.                     {
  628.                       macho_add_oso_symfile (oso2, member_bfd,
  629.                                              bfd_get_filename (member_bfd),
  630.                                              main_objfile, symfile_flags);
  631.                       oso2->name = NULL;
  632.                       break;
  633.                     }
  634.                 }

  635.               prev = member_bfd;
  636.               member_bfd = gdb_bfd_openr_next_archived_file (archive_bfd,
  637.                                                              member_bfd);

  638.               /* Free previous member if not referenced by an oso.  */
  639.               if (ix2 >= last_ix)
  640.                 gdb_bfd_unref (prev);
  641.             }
  642.           for (ix2 = ix; ix2 < last_ix; ix2++)
  643.             {
  644.               oso_el *oso2 = VEC_index (oso_el, vec, ix2);

  645.               if (oso2->name != NULL)
  646.                 warning (_("Could not find specified archive member "
  647.                            "for OSO name \"%s\""), oso->name);
  648.             }
  649.           ix = last_ix;
  650.         }
  651.       else
  652.         {
  653.           bfd *abfd;

  654.           abfd = gdb_bfd_open (oso->name, gnutarget, -1);
  655.           if (!abfd)
  656.             warning (_("`%s': can't open to read symbols: %s."), oso->name,
  657.                      bfd_errmsg (bfd_get_error ()));
  658.           else
  659.             macho_add_oso_symfile (oso, abfd, oso->name, main_objfile,
  660.                                    symfile_flags);

  661.           ix++;
  662.         }
  663.     }

  664.   do_cleanups (cleanup);
  665. }

  666. /* DSYM (debug symbols) files contain the debug info of an executable.
  667.    This is a separate file created by dsymutil(1) and is similar to debug
  668.    link feature on ELF.
  669.    DSYM files are located in a subdirectory.  Append DSYM_SUFFIX to the
  670.    executable name and the executable base name to get the DSYM file name.  */
  671. #define DSYM_SUFFIX ".dSYM/Contents/Resources/DWARF/"

  672. /* Check if a dsym file exists for OBJFILE.  If so, returns a bfd for it
  673.    and return *FILENAMEP with its original xmalloc-ated filename.
  674.    Return NULL if no valid dsym file is found (FILENAMEP is not used in
  675.    such case).  */

  676. static bfd *
  677. macho_check_dsym (struct objfile *objfile, char **filenamep)
  678. {
  679.   size_t name_len = strlen (objfile_name (objfile));
  680.   size_t dsym_len = strlen (DSYM_SUFFIX);
  681.   const char *base_name = lbasename (objfile_name (objfile));
  682.   size_t base_len = strlen (base_name);
  683.   char *dsym_filename = alloca (name_len + dsym_len + base_len + 1);
  684.   bfd *dsym_bfd;
  685.   bfd_mach_o_load_command *main_uuid;
  686.   bfd_mach_o_load_command *dsym_uuid;

  687.   strcpy (dsym_filename, objfile_name (objfile));
  688.   strcpy (dsym_filename + name_len, DSYM_SUFFIX);
  689.   strcpy (dsym_filename + name_len + dsym_len, base_name);

  690.   if (access (dsym_filename, R_OK) != 0)
  691.     return NULL;

  692.   if (bfd_mach_o_lookup_command (objfile->obfd,
  693.                                  BFD_MACH_O_LC_UUID, &main_uuid) == 0)
  694.     {
  695.       warning (_("can't find UUID in %s"), objfile_name (objfile));
  696.       return NULL;
  697.     }
  698.   dsym_bfd = gdb_bfd_openr (dsym_filename, gnutarget);
  699.   if (dsym_bfd == NULL)
  700.     {
  701.       warning (_("can't open dsym file %s"), dsym_filename);
  702.       return NULL;
  703.     }

  704.   if (!bfd_check_format (dsym_bfd, bfd_object))
  705.     {
  706.       gdb_bfd_unref (dsym_bfd);
  707.       warning (_("bad dsym file format: %s"), bfd_errmsg (bfd_get_error ()));
  708.       return NULL;
  709.     }

  710.   if (bfd_mach_o_lookup_command (dsym_bfd,
  711.                                  BFD_MACH_O_LC_UUID, &dsym_uuid) == 0)
  712.     {
  713.       warning (_("can't find UUID in %s"), dsym_filename);
  714.       gdb_bfd_unref (dsym_bfd);
  715.       return NULL;
  716.     }
  717.   if (memcmp (dsym_uuid->command.uuid.uuid, main_uuid->command.uuid.uuid,
  718.               sizeof (main_uuid->command.uuid.uuid)))
  719.     {
  720.       warning (_("dsym file UUID doesn't match the one in %s"),
  721.                objfile_name (objfile));
  722.       gdb_bfd_unref (dsym_bfd);
  723.       return NULL;
  724.     }
  725.   *filenamep = xstrdup (dsym_filename);
  726.   return dsym_bfd;
  727. }

  728. static void
  729. macho_symfile_read (struct objfile *objfile, int symfile_flags)
  730. {
  731.   bfd *abfd = objfile->obfd;
  732.   CORE_ADDR offset;
  733.   long storage_needed;
  734.   bfd *dsym_bfd;
  735.   VEC (oso_el) *oso_vector = NULL;
  736.   struct cleanup *old_chain = make_cleanup (VEC_cleanup (oso_el), &oso_vector);

  737.   /* Get symbols from the symbol table only if the file is an executable.
  738.      The symbol table of object files is not relocated and is expected to
  739.      be in the executable.  */
  740.   if (bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
  741.     {
  742.       char *dsym_filename;

  743.       /* Process the normal symbol table first.  */
  744.       storage_needed = bfd_get_symtab_upper_bound (objfile->obfd);
  745.       if (storage_needed < 0)
  746.         error (_("Can't read symbols from %s: %s"),
  747.                bfd_get_filename (objfile->obfd),
  748.                bfd_errmsg (bfd_get_error ()));

  749.       if (storage_needed > 0)
  750.         {
  751.           asymbol **symbol_table;
  752.           long symcount;

  753.           symbol_table = (asymbol **) xmalloc (storage_needed);
  754.           make_cleanup (xfree, symbol_table);

  755.           init_minimal_symbol_collection ();
  756.           make_cleanup_discard_minimal_symbols ();

  757.           symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);

  758.           if (symcount < 0)
  759.             error (_("Can't read symbols from %s: %s"),
  760.                    bfd_get_filename (objfile->obfd),
  761.                    bfd_errmsg (bfd_get_error ()));

  762.           macho_symtab_read (objfile, symcount, symbol_table, &oso_vector);

  763.           install_minimal_symbols (objfile);
  764.         }

  765.       /* Try to read .eh_frame / .debug_frame.  */
  766.       /* First, locate these sections.  We ignore the result status
  767.          as it only checks for debug info.  */
  768.       dwarf2_has_info (objfile, NULL);
  769.       dwarf2_build_frame_info (objfile);

  770.       /* Check for DSYM file.  */
  771.       dsym_bfd = macho_check_dsym (objfile, &dsym_filename);
  772.       if (dsym_bfd != NULL)
  773.         {
  774.           int ix;
  775.           oso_el *oso;
  776.           struct bfd_section *asect, *dsect;

  777.           make_cleanup (xfree, dsym_filename);

  778.           if (mach_o_debug_level > 0)
  779.             printf_unfiltered (_("dsym file found\n"));

  780.           /* Set dsym section size.  */
  781.           for (asect = objfile->obfd->sections, dsect = dsym_bfd->sections;
  782.                asect && dsect;
  783.                asect = asect->next, dsect = dsect->next)
  784.             {
  785.               if (strcmp (asect->name, dsect->name) != 0)
  786.                 break;
  787.               bfd_set_section_size (dsym_bfd, dsect,
  788.                                     bfd_get_section_size (asect));
  789.             }

  790.           /* Add the dsym file as a separate file.  */
  791.           make_cleanup_bfd_unref (dsym_bfd);
  792.           symbol_file_add_separate (dsym_bfd, dsym_filename, symfile_flags,
  793.                                     objfile);

  794.           /* Don't try to read dwarf2 from main file or shared libraries.  */
  795.           do_cleanups (old_chain);
  796.           return;
  797.         }
  798.     }

  799.   if (dwarf2_has_info (objfile, NULL))
  800.     {
  801.       /* DWARF 2 sections */
  802.       dwarf2_build_psymtabs (objfile);
  803.     }

  804.   /* Then the oso.  */
  805.   if (oso_vector != NULL)
  806.     macho_symfile_read_all_oso (&oso_vector, objfile, symfile_flags);

  807.   do_cleanups (old_chain);
  808. }

  809. static bfd_byte *
  810. macho_symfile_relocate (struct objfile *objfile, asection *sectp,
  811.                         bfd_byte *buf)
  812. {
  813.   bfd *abfd = objfile->obfd;

  814.   /* We're only interested in sections with relocation
  815.      information.  */
  816.   if ((sectp->flags & SEC_RELOC) == 0)
  817.     return NULL;

  818.   if (mach_o_debug_level > 0)
  819.     printf_unfiltered (_("Relocate section '%s' of %s\n"),
  820.                        sectp->name, objfile_name (objfile));

  821.   return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
  822. }

  823. static void
  824. macho_symfile_finish (struct objfile *objfile)
  825. {
  826. }

  827. static void
  828. macho_symfile_offsets (struct objfile *objfile,
  829.                        const struct section_addr_info *addrs)
  830. {
  831.   unsigned int i;
  832.   unsigned int num_sections;
  833.   struct obj_section *osect;

  834.   /* Allocate section_offsets.  */
  835.   objfile->num_sections = bfd_count_sections (objfile->obfd);
  836.   objfile->section_offsets = (struct section_offsets *)
  837.     obstack_alloc (&objfile->objfile_obstack,
  838.                    SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
  839.   memset (objfile->section_offsets, 0,
  840.           SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));

  841.   /* This code is run when we first add the objfile with
  842.      symfile_add_with_addrs_or_offsets, when "addrs" not "offsets" are
  843.      passed in.  The place in symfile.c where the addrs are applied
  844.      depends on the addrs having section names.  But in the dyld code
  845.      we build an anonymous array of addrs, so that code is a no-op.
  846.      Because of that, we have to apply the addrs to the sections here.
  847.      N.B. if an objfile slides after we've already created it, then it
  848.      goes through objfile_relocate.  */

  849.   for (i = 0; i < addrs->num_sections; i++)
  850.     {
  851.       ALL_OBJFILE_OSECTIONS (objfile, osect)
  852.         {
  853.           const char *bfd_sect_name = osect->the_bfd_section->name;

  854.           if (strcmp (bfd_sect_name, addrs->other[i].name) == 0)
  855.             {
  856.               obj_section_offset (osect) = addrs->other[i].addr;
  857.               break;
  858.             }
  859.         }
  860.     }

  861.   objfile->sect_index_text = 0;

  862.   ALL_OBJFILE_OSECTIONS (objfile, osect)
  863.     {
  864.       const char *bfd_sect_name = osect->the_bfd_section->name;
  865.       int sect_index = osect - objfile->sections;;

  866.       if (strncmp (bfd_sect_name, "LC_SEGMENT.", 11) == 0)
  867.         bfd_sect_name += 11;
  868.       if (strcmp (bfd_sect_name, "__TEXT") == 0
  869.           || strcmp (bfd_sect_name, "__TEXT.__text") == 0)
  870.         objfile->sect_index_text = sect_index;
  871.     }
  872. }

  873. static const struct sym_fns macho_sym_fns = {
  874.   macho_new_init,               /* init anything gbl to entire symtab */
  875.   macho_symfile_init,           /* read initial info, setup for sym_read() */
  876.   macho_symfile_read,           /* read a symbol file into symtab */
  877.   NULL,                                /* sym_read_psymbols */
  878.   macho_symfile_finish,         /* finished with file, cleanup */
  879.   macho_symfile_offsets,        /* xlate external to internal form */
  880.   default_symfile_segments,        /* Get segment information from a file.  */
  881.   NULL,
  882.   macho_symfile_relocate,        /* Relocate a debug section.  */
  883.   NULL,                                /* sym_get_probes */
  884.   &psym_functions
  885. };

  886. /* -Wmissing-prototypes */
  887. extern initialize_file_ftype _initialize_machoread;

  888. void
  889. _initialize_machoread (void)
  890. {
  891.   add_symtab_fns (bfd_target_mach_o_flavour, &macho_sym_fns);

  892.   add_setshow_zuinteger_cmd ("mach-o", class_obscure,
  893.                              &mach_o_debug_level,
  894.                              _("Set if printing Mach-O symbols processing."),
  895.                              _("Show if printing Mach-O symbols processing."),
  896.                              NULL, NULL, NULL,
  897.                              &setdebuglist, &showdebuglist);
  898. }