GDB (API)
/home/stan/gdb/src/gdb/machoread.c
Go to the documentation of this file.
00001 /* Darwin support for GDB, the GNU debugger.
00002    Copyright (C) 2008-2013 Free Software Foundation, Inc.
00003 
00004    Contributed by AdaCore.
00005 
00006    This file is part of GDB.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 3 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00020 
00021 #include "defs.h"
00022 #include "symtab.h"
00023 #include "gdbtypes.h"
00024 #include "bfd.h"
00025 #include "symfile.h"
00026 #include "objfiles.h"
00027 #include "buildsym.h"
00028 #include "gdbcmd.h"
00029 #include "gdbcore.h"
00030 #include "mach-o.h"
00031 #include "gdb_assert.h"
00032 #include "aout/stab_gnu.h"
00033 #include "vec.h"
00034 #include "psympriv.h"
00035 #include "complaints.h"
00036 #include "gdb_bfd.h"
00037 
00038 #include <string.h>
00039 
00040 /* If non-zero displays debugging message.  */
00041 static unsigned int mach_o_debug_level = 0;
00042 
00043 /* Dwarf debugging information are never in the final executable.  They stay
00044    in object files and the executable contains the list of object files read
00045    during the link.
00046    Each time an oso (other source) is found in the executable, the reader
00047    creates such a structure.  They are read after the processing of the
00048    executable.  */
00049 
00050 typedef struct oso_el
00051 {
00052   /* Object file name.  Can also be a member name.  */
00053   const char *name;
00054 
00055   /* Associated time stamp.  */
00056   unsigned long mtime;
00057 
00058   /* Stab symbols range for this OSO.  */
00059   asymbol **oso_sym;
00060   asymbol **end_sym;
00061 
00062   /* Number of interesting stabs in the range.  */
00063   unsigned int nbr_syms;
00064 }
00065 oso_el;
00066 
00067 /* Vector of object files to be read after the executable.  */
00068 DEF_VEC_O (oso_el);
00069 
00070 static void
00071 macho_new_init (struct objfile *objfile)
00072 {
00073 }
00074 
00075 static void
00076 macho_symfile_init (struct objfile *objfile)
00077 {
00078   objfile->flags |= OBJF_REORDERED;
00079 }
00080 
00081 /*  Add a new OSO to the vector of OSO to load.  */
00082 
00083 static void
00084 macho_register_oso (VEC (oso_el) **oso_vector_ptr,
00085                     struct objfile *objfile,
00086                     asymbol **oso_sym, asymbol **end_sym,
00087                     unsigned int nbr_syms)
00088 {
00089   oso_el el;
00090 
00091   el.name = (*oso_sym)->name;
00092   el.mtime = (*oso_sym)->value;
00093   el.oso_sym = oso_sym;
00094   el.end_sym = end_sym;
00095   el.nbr_syms = nbr_syms;
00096   VEC_safe_push (oso_el, *oso_vector_ptr, &el);
00097 }
00098 
00099 /* Add symbol SYM to the minimal symbol table of OBJFILE.  */
00100 
00101 static void
00102 macho_symtab_add_minsym (struct objfile *objfile, const asymbol *sym)
00103 {
00104   if (sym->name == NULL || *sym->name == '\0')
00105     {
00106       /* Skip names that don't exist (shouldn't happen), or names
00107          that are null strings (may happen).  */
00108       return;
00109     }
00110 
00111   if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK))
00112     {
00113       CORE_ADDR symaddr;
00114       CORE_ADDR offset;
00115       enum minimal_symbol_type ms_type;
00116 
00117       offset = ANOFFSET (objfile->section_offsets,
00118                          gdb_bfd_section_index (objfile->obfd, sym->section));
00119 
00120       /* Bfd symbols are section relative.  */
00121       symaddr = sym->value + sym->section->vma;
00122 
00123       /* Select global/local/weak symbols.  Note that bfd puts abs
00124          symbols in their own section, so all symbols we are
00125          interested in will have a section.  */
00126       /* Relocate all non-absolute and non-TLS symbols by the
00127          section offset.  */
00128       if (sym->section != bfd_abs_section_ptr
00129           && !(sym->section->flags & SEC_THREAD_LOCAL))
00130         symaddr += offset;
00131 
00132       if (sym->section == bfd_abs_section_ptr)
00133         ms_type = mst_abs;
00134       else if (sym->section->flags & SEC_CODE)
00135         {
00136           if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
00137             ms_type = mst_text;
00138           else
00139             ms_type = mst_file_text;
00140         }
00141       else if (sym->section->flags & SEC_ALLOC)
00142         {
00143           if (sym->flags & (BSF_GLOBAL | BSF_WEAK))
00144             {
00145               if (sym->section->flags & SEC_LOAD)
00146                 ms_type = mst_data;
00147               else
00148                 ms_type = mst_bss;
00149             }
00150           else if (sym->flags & BSF_LOCAL)
00151             {
00152               /* Not a special stabs-in-elf symbol, do regular
00153                  symbol processing.  */
00154               if (sym->section->flags & SEC_LOAD)
00155                 ms_type = mst_file_data;
00156               else
00157                 ms_type = mst_file_bss;
00158             }
00159           else
00160             ms_type = mst_unknown;
00161         }
00162       else
00163         return; /* Skip this symbol.  */
00164 
00165       prim_record_minimal_symbol_and_info
00166         (sym->name, symaddr, ms_type,
00167          gdb_bfd_section_index (objfile->obfd, sym->section),
00168          objfile);
00169     }
00170 }
00171 
00172 /* Build the minimal symbol table from SYMBOL_TABLE of length
00173    NUMBER_OF_SYMBOLS for OBJFILE.  Registers OSO filenames found.  */
00174 
00175 static void
00176 macho_symtab_read (struct objfile *objfile,
00177                    long number_of_symbols, asymbol **symbol_table,
00178                    VEC (oso_el) **oso_vector_ptr)
00179 {
00180   long i;
00181   const asymbol *dir_so = NULL;
00182   const asymbol *file_so = NULL;
00183   asymbol **oso_file = NULL;
00184   unsigned int nbr_syms = 0;
00185 
00186   /* Current state while reading stabs.  */
00187   enum
00188   {
00189     /* Not within an SO part.  Only non-debugging symbols should be present,
00190        and will be added to the minimal symbols table.  */
00191     S_NO_SO,
00192 
00193     /* First SO read.  Introduce an SO section, and may be followed by a second
00194        SO.  The SO section should contain onl debugging symbols.  */
00195     S_FIRST_SO,
00196 
00197     /* Second non-null SO found, just after the first one.  Means that the first
00198        is in fact a directory name.  */
00199     S_SECOND_SO,
00200 
00201     /* Non-null OSO found.  Debugging info are DWARF in this OSO file.  */
00202     S_DWARF_FILE,
00203 
00204     S_STAB_FILE
00205   } state = S_NO_SO;
00206 
00207   for (i = 0; i < number_of_symbols; i++)
00208     {
00209       const asymbol *sym = symbol_table[i];
00210       bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
00211 
00212       switch (state)
00213         {
00214         case S_NO_SO:
00215           if (mach_o_sym->n_type == N_SO)
00216             {
00217               /* Start of object stab.  */
00218               if (sym->name == NULL || sym->name[0] == 0)
00219                 {
00220                   /* Unexpected empty N_SO.  */
00221                   complaint (&symfile_complaints,
00222                              _("Unexpected empty N_SO stab"));
00223                 }
00224               else
00225                 {
00226                   file_so = sym;
00227                   dir_so = NULL;
00228                   state = S_FIRST_SO;
00229                 }
00230             }
00231           else if (sym->flags & BSF_DEBUGGING)
00232             {
00233               if (mach_o_sym->n_type == N_OPT)
00234                 {
00235                   /* No complaint for OPT.  */
00236                   break;
00237                 }
00238 
00239               /* Debugging symbols are not expected here.  */
00240               complaint (&symfile_complaints,
00241                          _("%s: Unexpected debug stab outside SO markers"),
00242                          objfile_name (objfile));
00243             }
00244           else
00245             {
00246               /* Non-debugging symbols go to the minimal symbol table.  */
00247               macho_symtab_add_minsym (objfile, sym);
00248             }
00249           break;
00250 
00251         case S_FIRST_SO:
00252         case S_SECOND_SO:
00253           if (mach_o_sym->n_type == N_SO)
00254             {
00255               if (sym->name == NULL || sym->name[0] == 0)
00256                 {
00257                   /* Unexpected empty N_SO.  */
00258                   complaint (&symfile_complaints, _("Empty SO section"));
00259                   state = S_NO_SO;
00260                 }
00261               else if (state == S_FIRST_SO)
00262                 {
00263                   /* Second SO stab for the file name.  */
00264                   dir_so = file_so;
00265                   file_so = sym;
00266                   state = S_SECOND_SO;
00267                 }
00268               else
00269                 complaint (&symfile_complaints, _("Three SO in a raw"));
00270             }
00271           else if (mach_o_sym->n_type == N_OSO)
00272             {
00273               if (sym->name == NULL || sym->name[0] == 0)
00274                 {
00275                   /* Empty OSO.  Means that this file was compiled with
00276                      stabs.  */
00277                   state = S_STAB_FILE;
00278                   warning (_("stabs debugging not supported for %s"),
00279                            file_so->name);
00280                 }
00281               else
00282                 {
00283                   /* Non-empty OSO for a Dwarf file.  */
00284                   oso_file = symbol_table + i;
00285                   nbr_syms = 0;
00286                   state = S_DWARF_FILE;
00287                 }
00288             }
00289           else
00290             complaint (&symfile_complaints,
00291                        _("Unexpected stab after SO"));
00292           break;
00293 
00294         case S_STAB_FILE:
00295         case S_DWARF_FILE:
00296           if (mach_o_sym->n_type == N_SO)
00297             {
00298               if (sym->name == NULL || sym->name[0] == 0)
00299                 {
00300                   /* End of file.  */
00301                   if (state == S_DWARF_FILE)
00302                     macho_register_oso (oso_vector_ptr, objfile,
00303                                         oso_file, symbol_table + i,
00304                                         nbr_syms);
00305                   state = S_NO_SO;
00306                 }
00307               else
00308                 {
00309                   complaint (&symfile_complaints, _("Missing nul SO"));
00310                   file_so = sym;
00311                   dir_so = NULL;
00312                   state = S_FIRST_SO;
00313                 }
00314             }
00315           else if (sym->flags & BSF_DEBUGGING)
00316             {
00317               if (state == S_STAB_FILE)
00318                 {
00319                   /* FIXME: to be implemented.  */
00320                 }
00321               else
00322                 {
00323                   switch (mach_o_sym->n_type)
00324                     {
00325                     case N_FUN:
00326                       if (sym->name == NULL || sym->name[0] == 0)
00327                         break;
00328                       /* Fall through.  */
00329                     case N_STSYM:
00330                       /* Interesting symbol.  */
00331                       nbr_syms++;
00332                       break;
00333                     case N_ENSYM:
00334                     case N_BNSYM:
00335                     case N_GSYM:
00336                       break;
00337                     default:
00338                       complaint (&symfile_complaints,
00339                                  _("unhandled stab for dwarf OSO file"));
00340                       break;
00341                     }
00342                 }
00343             }
00344           else
00345             complaint (&symfile_complaints,
00346                        _("non-debugging symbol within SO"));
00347           break;
00348         }
00349     }
00350 
00351   if (state != S_NO_SO)
00352     complaint (&symfile_complaints, _("missing nul SO"));
00353 }
00354 
00355 /* If NAME describes an archive member (ie: ARCHIVE '(' MEMBER ')'),
00356    returns the length of the archive name.
00357    Returns -1 otherwise.  */
00358 
00359 static int
00360 get_archive_prefix_len (const char *name)
00361 {
00362   char *lparen;
00363   int name_len = strlen (name);
00364 
00365   if (name_len == 0 || name[name_len - 1] != ')')
00366     return -1;
00367 
00368   lparen = strrchr (name, '(');
00369   if (lparen == NULL || lparen == name)
00370     return -1;
00371   return lparen - name;
00372 }
00373 
00374 /* Compare function to qsort OSOs, so that members of a library are
00375    gathered.  */
00376 
00377 static int
00378 oso_el_compare_name (const void *vl, const void *vr)
00379 {
00380   const oso_el *l = (const oso_el *)vl;
00381   const oso_el *r = (const oso_el *)vr;
00382 
00383   return strcmp (l->name, r->name);
00384 }
00385 
00386 /* Hash table entry structure for the stabs symbols in the main object file.
00387    This is used to speed up lookup for symbols in the OSO.  */
00388 
00389 struct macho_sym_hash_entry
00390 {
00391   struct bfd_hash_entry base;
00392   const asymbol *sym;
00393 };
00394 
00395 /* Routine to create an entry in the hash table.  */
00396 
00397 static struct bfd_hash_entry *
00398 macho_sym_hash_newfunc (struct bfd_hash_entry *entry,
00399                         struct bfd_hash_table *table,
00400                         const char *string)
00401 {
00402   struct macho_sym_hash_entry *ret = (struct macho_sym_hash_entry *) entry;
00403 
00404   /* Allocate the structure if it has not already been allocated by a
00405      subclass.  */
00406   if (ret == NULL)
00407     ret = (struct macho_sym_hash_entry *) bfd_hash_allocate (table,
00408                                                              sizeof (* ret));
00409   if (ret == NULL)
00410     return NULL;
00411 
00412   /* Call the allocation method of the superclass.  */
00413   ret = (struct macho_sym_hash_entry *)
00414          bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
00415 
00416   if (ret)
00417     {
00418       /* Initialize the local fields.  */
00419       ret->sym = NULL;
00420     }
00421 
00422   return (struct bfd_hash_entry *) ret;
00423 }
00424 
00425 /* Get the value of SYM from the minimal symtab of MAIN_OBJFILE.  This is used
00426    to get the value of global and common symbols.  */
00427 
00428 static CORE_ADDR
00429 macho_resolve_oso_sym_with_minsym (struct objfile *main_objfile, asymbol *sym)
00430 {
00431   /* For common symbol and global symbols, use the min symtab.  */
00432   struct minimal_symbol *msym;
00433   const char *name = sym->name;
00434 
00435   if (name[0] == bfd_get_symbol_leading_char (main_objfile->obfd))
00436     ++name;
00437   msym = lookup_minimal_symbol (name, NULL, main_objfile);
00438   if (msym == NULL)
00439     {
00440       warning (_("can't find symbol '%s' in minsymtab"), name);
00441       return 0;
00442     }
00443   else
00444     return SYMBOL_VALUE_ADDRESS (msym);
00445 }
00446 
00447 /* Add oso file OSO/ABFD as a symbol file.  */
00448 
00449 static void
00450 macho_add_oso_symfile (oso_el *oso, bfd *abfd, const char *name,
00451                        struct objfile *main_objfile, int symfile_flags)
00452 {
00453   int storage;
00454   int i;
00455   asymbol **symbol_table;
00456   asymbol **symp;
00457   struct bfd_hash_table table;
00458   int nbr_sections;
00459   struct cleanup *cleanup;
00460 
00461   /* Per section flag to mark which section have been rebased.  */
00462   unsigned char *sections_rebased;
00463 
00464   if (mach_o_debug_level > 0)
00465     printf_unfiltered
00466       (_("Loading debugging symbols from oso: %s\n"), oso->name);
00467 
00468   if (!bfd_check_format (abfd, bfd_object))
00469     {
00470       warning (_("`%s': can't read symbols: %s."), oso->name,
00471                bfd_errmsg (bfd_get_error ()));
00472       gdb_bfd_unref (abfd);
00473       return;
00474     }
00475 
00476   if (abfd->my_archive == NULL && oso->mtime != bfd_get_mtime (abfd))
00477     {
00478       warning (_("`%s': file time stamp mismatch."), oso->name);
00479       gdb_bfd_unref (abfd);
00480       return;
00481     }
00482 
00483   if (!bfd_hash_table_init_n (&table, macho_sym_hash_newfunc,
00484                               sizeof (struct macho_sym_hash_entry),
00485                               oso->nbr_syms))
00486     {
00487       warning (_("`%s': can't create hash table"), oso->name);
00488       gdb_bfd_unref (abfd);
00489       return;
00490     }
00491 
00492   bfd_set_cacheable (abfd, 1);
00493 
00494   /* Read symbols table.  */
00495   storage = bfd_get_symtab_upper_bound (abfd);
00496   symbol_table = (asymbol **) xmalloc (storage);
00497   bfd_canonicalize_symtab (abfd, symbol_table);
00498 
00499   /* Init section flags.  */
00500   nbr_sections = bfd_count_sections (abfd);
00501   sections_rebased = (unsigned char *) alloca (nbr_sections);
00502   for (i = 0; i < nbr_sections; i++)
00503     sections_rebased[i] = 0;
00504 
00505   /* Put symbols for the OSO file in the hash table.  */
00506   for (symp = oso->oso_sym; symp != oso->end_sym; symp++)
00507     {
00508       const asymbol *sym = *symp;
00509       bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
00510 
00511       switch (mach_o_sym->n_type)
00512         {
00513         case N_ENSYM:
00514         case N_BNSYM:
00515         case N_GSYM:
00516           sym = NULL;
00517           break;
00518         case N_FUN:
00519           if (sym->name == NULL || sym->name[0] == 0)
00520             sym = NULL;
00521           break;
00522         case N_STSYM:
00523           break;
00524         default:
00525           sym = NULL;
00526           break;
00527         }
00528       if (sym != NULL)
00529         {
00530           struct macho_sym_hash_entry *ent;
00531 
00532           ent = (struct macho_sym_hash_entry *)
00533             bfd_hash_lookup (&table, sym->name, TRUE, FALSE);
00534           if (ent->sym != NULL)
00535             complaint (&symfile_complaints,
00536                        _("Duplicated symbol %s in symbol table"), sym->name);
00537           else
00538             {
00539               if (mach_o_debug_level > 4)
00540                 {
00541                   struct gdbarch *arch = get_objfile_arch (main_objfile);
00542                   printf_unfiltered
00543                     (_("Adding symbol %s (addr: %s)\n"),
00544                      sym->name, paddress (arch, sym->value));
00545                 }
00546               ent->sym = sym;
00547             }
00548         }
00549     }
00550 
00551   /* Relocate symbols of the OSO.  */
00552   for (i = 0; symbol_table[i]; i++)
00553     {
00554       asymbol *sym = symbol_table[i];
00555       bfd_mach_o_asymbol *mach_o_sym = (bfd_mach_o_asymbol *)sym;
00556 
00557       if (mach_o_sym->n_type & BFD_MACH_O_N_STAB)
00558         continue;
00559       if ((mach_o_sym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_UNDF
00560            && sym->value != 0)
00561         {
00562           /* For common symbol use the min symtab and modify the OSO
00563              symbol table.  */
00564           CORE_ADDR res;
00565 
00566           res = macho_resolve_oso_sym_with_minsym (main_objfile, sym);
00567           if (res != 0)
00568             {
00569               sym->section = bfd_com_section_ptr;
00570               sym->value = res;
00571             }
00572         }
00573       else if ((mach_o_sym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
00574         {
00575           /* Normal symbol.  */
00576           asection *sec = sym->section;
00577           bfd_mach_o_section *msec;
00578           unsigned int sec_type;
00579 
00580           /* Skip buggy ones.  */
00581           if (sec == NULL || sections_rebased[sec->index] != 0)
00582             continue;
00583 
00584           /* Only consider regular, non-debugging sections.  */
00585           msec = bfd_mach_o_get_mach_o_section (sec);
00586           sec_type = msec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
00587           if ((sec_type == BFD_MACH_O_S_REGULAR
00588                || sec_type == BFD_MACH_O_S_ZEROFILL)
00589               && (msec->flags & BFD_MACH_O_S_ATTR_DEBUG) == 0)
00590             {
00591               CORE_ADDR addr = 0;
00592 
00593               if ((mach_o_sym->n_type & BFD_MACH_O_N_EXT) != 0)
00594                 {
00595                   /* Use the min symtab for global symbols.  */
00596                   addr = macho_resolve_oso_sym_with_minsym (main_objfile, sym);
00597                 }
00598               else
00599                 {
00600                   struct macho_sym_hash_entry *ent;
00601 
00602                   ent = (struct macho_sym_hash_entry *)
00603                     bfd_hash_lookup (&table, sym->name, FALSE, FALSE);
00604                   if (ent != NULL)
00605                     addr = bfd_asymbol_value (ent->sym);
00606                 }
00607 
00608               /* Adjust the section.  */
00609               if (addr != 0)
00610                 {
00611                   CORE_ADDR res = addr - sym->value;
00612 
00613                   if (mach_o_debug_level > 3)
00614                     {
00615                       struct gdbarch *arch = get_objfile_arch (main_objfile);
00616                       printf_unfiltered
00617                         (_("resolve sect %s with %s (set to %s)\n"),
00618                          sec->name, sym->name,
00619                          paddress (arch, res));
00620                     }
00621                   bfd_set_section_vma (abfd, sec, res);
00622                   sections_rebased[sec->index] = 1;
00623                 }
00624             }
00625           else
00626             {
00627               /* Mark the section as never rebased.  */
00628               sections_rebased[sec->index] = 2;
00629             }
00630         }
00631     }
00632 
00633   bfd_hash_table_free (&table);
00634 
00635   /* We need to clear SYMFILE_MAINLINE to avoid interractive question
00636      from symfile.c:symbol_file_add_with_addrs_or_offsets.  */
00637   cleanup = make_cleanup_bfd_unref (abfd);
00638   symbol_file_add_from_bfd
00639     (abfd, name, symfile_flags & ~(SYMFILE_MAINLINE | SYMFILE_VERBOSE), NULL,
00640      main_objfile->flags & (OBJF_REORDERED | OBJF_SHARED
00641                             | OBJF_READNOW | OBJF_USERLOADED),
00642      main_objfile);
00643   do_cleanups (cleanup);
00644 }
00645 
00646 /* Read symbols from the vector of oso files.
00647 
00648    Note that this function sorts OSO_VECTOR_PTR.  */
00649 
00650 static void
00651 macho_symfile_read_all_oso (VEC (oso_el) **oso_vector_ptr,
00652                             struct objfile *main_objfile,
00653                             int symfile_flags)
00654 {
00655   int ix;
00656   VEC (oso_el) *vec = *oso_vector_ptr;
00657   oso_el *oso;
00658   struct cleanup *cleanup = make_cleanup (null_cleanup, NULL);
00659 
00660   /* Sort oso by name so that files from libraries are gathered.  */
00661   qsort (VEC_address (oso_el, vec), VEC_length (oso_el, vec),
00662          sizeof (oso_el), oso_el_compare_name);
00663 
00664   for (ix = 0; VEC_iterate (oso_el, vec, ix, oso);)
00665     {
00666       int pfx_len;
00667 
00668       /* Check if this is a library name.  */
00669       pfx_len = get_archive_prefix_len (oso->name);
00670       if (pfx_len > 0)
00671         {
00672           bfd *archive_bfd;
00673           bfd *member_bfd;
00674           char *archive_name = XNEWVEC (char, pfx_len + 1);
00675           int last_ix;
00676           oso_el *oso2;
00677           int ix2;
00678 
00679           memcpy (archive_name, oso->name, pfx_len);
00680           archive_name[pfx_len] = '\0';
00681 
00682           make_cleanup (xfree, archive_name);
00683 
00684           /* Compute number of oso for this archive.  */
00685           for (last_ix = ix;
00686                VEC_iterate (oso_el, vec, last_ix, oso2); last_ix++)
00687             {
00688               if (strncmp (oso2->name, archive_name, pfx_len) != 0)
00689                 break;
00690             }
00691 
00692           /* Open the archive and check the format.  */
00693           archive_bfd = gdb_bfd_open (archive_name, gnutarget, -1);
00694           if (archive_bfd == NULL)
00695             {
00696               warning (_("Could not open OSO archive file \"%s\""),
00697                        archive_name);
00698               ix = last_ix;
00699               continue;
00700             }
00701           if (!bfd_check_format (archive_bfd, bfd_archive))
00702             {
00703               warning (_("OSO archive file \"%s\" not an archive."),
00704                        archive_name);
00705               gdb_bfd_unref (archive_bfd);
00706               ix = last_ix;
00707               continue;
00708             }
00709 
00710           member_bfd = gdb_bfd_openr_next_archived_file (archive_bfd, NULL);
00711 
00712           if (member_bfd == NULL)
00713             {
00714               warning (_("Could not read archive members out of "
00715                          "OSO archive \"%s\""), archive_name);
00716               gdb_bfd_unref (archive_bfd);
00717               ix = last_ix;
00718               continue;
00719             }
00720 
00721           /* Load all oso in this library.  */
00722           while (member_bfd != NULL)
00723             {
00724               bfd *prev;
00725               const char *member_name = member_bfd->filename;
00726               int member_len = strlen (member_name);
00727 
00728               /* If this member is referenced, add it as a symfile.  */
00729               for (ix2 = ix; ix2 < last_ix; ix2++)
00730                 {
00731                   oso2 = VEC_index (oso_el, vec, ix2);
00732 
00733                   if (oso2->name
00734                       && strlen (oso2->name) == pfx_len + member_len + 2
00735                       && !memcmp (member_name, oso2->name + pfx_len + 1,
00736                                   member_len))
00737                     {
00738                       macho_add_oso_symfile (oso2, member_bfd,
00739                                              bfd_get_filename (member_bfd),
00740                                              main_objfile, symfile_flags);
00741                       oso2->name = NULL;
00742                       break;
00743                     }
00744                 }
00745 
00746               prev = member_bfd;
00747               member_bfd = gdb_bfd_openr_next_archived_file (archive_bfd,
00748                                                              member_bfd);
00749 
00750               /* Free previous member if not referenced by an oso.  */
00751               if (ix2 >= last_ix)
00752                 gdb_bfd_unref (prev);
00753             }
00754           for (ix2 = ix; ix2 < last_ix; ix2++)
00755             {
00756               oso_el *oso2 = VEC_index (oso_el, vec, ix2);
00757 
00758               if (oso2->name != NULL)
00759                 warning (_("Could not find specified archive member "
00760                            "for OSO name \"%s\""), oso->name);
00761             }
00762           ix = last_ix;
00763         }
00764       else
00765         {
00766           bfd *abfd;
00767 
00768           abfd = gdb_bfd_open (oso->name, gnutarget, -1);
00769           if (!abfd)
00770             warning (_("`%s': can't open to read symbols: %s."), oso->name,
00771                      bfd_errmsg (bfd_get_error ()));
00772           else
00773             macho_add_oso_symfile (oso, abfd, oso->name, main_objfile,
00774                                    symfile_flags);
00775 
00776           ix++;
00777         }
00778     }
00779 
00780   do_cleanups (cleanup);
00781 }
00782 
00783 /* DSYM (debug symbols) files contain the debug info of an executable.
00784    This is a separate file created by dsymutil(1) and is similar to debug
00785    link feature on ELF.
00786    DSYM files are located in a subdirectory.  Append DSYM_SUFFIX to the
00787    executable name and the executable base name to get the DSYM file name.  */
00788 #define DSYM_SUFFIX ".dSYM/Contents/Resources/DWARF/"
00789 
00790 /* Check if a dsym file exists for OBJFILE.  If so, returns a bfd for it
00791    and return *FILENAMEP with its original xmalloc-ated filename.
00792    Return NULL if no valid dsym file is found (FILENAMEP is not used in
00793    such case).  */
00794 
00795 static bfd *
00796 macho_check_dsym (struct objfile *objfile, char **filenamep)
00797 {
00798   size_t name_len = strlen (objfile_name (objfile));
00799   size_t dsym_len = strlen (DSYM_SUFFIX);
00800   const char *base_name = lbasename (objfile_name (objfile));
00801   size_t base_len = strlen (base_name);
00802   char *dsym_filename = alloca (name_len + dsym_len + base_len + 1);
00803   bfd *dsym_bfd;
00804   bfd_mach_o_load_command *main_uuid;
00805   bfd_mach_o_load_command *dsym_uuid;
00806 
00807   strcpy (dsym_filename, objfile_name (objfile));
00808   strcpy (dsym_filename + name_len, DSYM_SUFFIX);
00809   strcpy (dsym_filename + name_len + dsym_len, base_name);
00810 
00811   if (access (dsym_filename, R_OK) != 0)
00812     return NULL;
00813 
00814   if (bfd_mach_o_lookup_command (objfile->obfd,
00815                                  BFD_MACH_O_LC_UUID, &main_uuid) == 0)
00816     {
00817       warning (_("can't find UUID in %s"), objfile_name (objfile));
00818       return NULL;
00819     }
00820   dsym_bfd = gdb_bfd_openr (dsym_filename, gnutarget);
00821   if (dsym_bfd == NULL)
00822     {
00823       warning (_("can't open dsym file %s"), dsym_filename);
00824       return NULL;
00825     }
00826 
00827   if (!bfd_check_format (dsym_bfd, bfd_object))
00828     {
00829       gdb_bfd_unref (dsym_bfd);
00830       warning (_("bad dsym file format: %s"), bfd_errmsg (bfd_get_error ()));
00831       return NULL;
00832     }
00833 
00834   if (bfd_mach_o_lookup_command (dsym_bfd,
00835                                  BFD_MACH_O_LC_UUID, &dsym_uuid) == 0)
00836     {
00837       warning (_("can't find UUID in %s"), dsym_filename);
00838       gdb_bfd_unref (dsym_bfd);
00839       return NULL;
00840     }
00841   if (memcmp (dsym_uuid->command.uuid.uuid, main_uuid->command.uuid.uuid,
00842               sizeof (main_uuid->command.uuid.uuid)))
00843     {
00844       warning (_("dsym file UUID doesn't match the one in %s"),
00845                objfile_name (objfile));
00846       gdb_bfd_unref (dsym_bfd);
00847       return NULL;
00848     }
00849   *filenamep = xstrdup (dsym_filename);
00850   return dsym_bfd;
00851 }
00852 
00853 static void
00854 macho_symfile_read (struct objfile *objfile, int symfile_flags)
00855 {
00856   bfd *abfd = objfile->obfd;
00857   CORE_ADDR offset;
00858   long storage_needed;
00859   bfd *dsym_bfd;
00860   VEC (oso_el) *oso_vector = NULL;
00861   struct cleanup *old_chain = make_cleanup (VEC_cleanup (oso_el), &oso_vector);
00862 
00863   /* Get symbols from the symbol table only if the file is an executable.
00864      The symbol table of object files is not relocated and is expected to
00865      be in the executable.  */
00866   if (bfd_get_file_flags (abfd) & (EXEC_P | DYNAMIC))
00867     {
00868       char *dsym_filename;
00869 
00870       /* Process the normal symbol table first.  */
00871       storage_needed = bfd_get_symtab_upper_bound (objfile->obfd);
00872       if (storage_needed < 0)
00873         error (_("Can't read symbols from %s: %s"),
00874                bfd_get_filename (objfile->obfd),
00875                bfd_errmsg (bfd_get_error ()));
00876 
00877       if (storage_needed > 0)
00878         {
00879           asymbol **symbol_table;
00880           long symcount;
00881 
00882           symbol_table = (asymbol **) xmalloc (storage_needed);
00883           make_cleanup (xfree, symbol_table);
00884 
00885           init_minimal_symbol_collection ();
00886           make_cleanup_discard_minimal_symbols ();
00887 
00888           symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table);
00889 
00890           if (symcount < 0)
00891             error (_("Can't read symbols from %s: %s"),
00892                    bfd_get_filename (objfile->obfd),
00893                    bfd_errmsg (bfd_get_error ()));
00894 
00895           macho_symtab_read (objfile, symcount, symbol_table, &oso_vector);
00896 
00897           install_minimal_symbols (objfile);
00898         }
00899 
00900       /* Try to read .eh_frame / .debug_frame.  */
00901       /* First, locate these sections.  We ignore the result status
00902          as it only checks for debug info.  */
00903       dwarf2_has_info (objfile, NULL);
00904       dwarf2_build_frame_info (objfile);
00905 
00906       /* Check for DSYM file.  */
00907       dsym_bfd = macho_check_dsym (objfile, &dsym_filename);
00908       if (dsym_bfd != NULL)
00909         {
00910           int ix;
00911           oso_el *oso;
00912           struct bfd_section *asect, *dsect;
00913 
00914           make_cleanup (xfree, dsym_filename);
00915 
00916           if (mach_o_debug_level > 0)
00917             printf_unfiltered (_("dsym file found\n"));
00918 
00919           /* Set dsym section size.  */
00920           for (asect = objfile->obfd->sections, dsect = dsym_bfd->sections;
00921                asect && dsect;
00922                asect = asect->next, dsect = dsect->next)
00923             {
00924               if (strcmp (asect->name, dsect->name) != 0)
00925                 break;
00926               bfd_set_section_size (dsym_bfd, dsect,
00927                                     bfd_get_section_size (asect));
00928             }
00929 
00930           /* Add the dsym file as a separate file.  */
00931           make_cleanup_bfd_unref (dsym_bfd);
00932           symbol_file_add_separate (dsym_bfd, dsym_filename, symfile_flags,
00933                                     objfile);
00934 
00935           /* Don't try to read dwarf2 from main file or shared libraries.  */
00936           do_cleanups (old_chain);
00937           return;
00938         }
00939     }
00940 
00941   if (dwarf2_has_info (objfile, NULL))
00942     {
00943       /* DWARF 2 sections */
00944       dwarf2_build_psymtabs (objfile);
00945     }
00946 
00947   /* Then the oso.  */
00948   if (oso_vector != NULL)
00949     macho_symfile_read_all_oso (&oso_vector, objfile, symfile_flags);
00950 
00951   do_cleanups (old_chain);
00952 }
00953 
00954 static bfd_byte *
00955 macho_symfile_relocate (struct objfile *objfile, asection *sectp,
00956                         bfd_byte *buf)
00957 {
00958   bfd *abfd = objfile->obfd;
00959 
00960   /* We're only interested in sections with relocation
00961      information.  */
00962   if ((sectp->flags & SEC_RELOC) == 0)
00963     return NULL;
00964 
00965   if (mach_o_debug_level > 0)
00966     printf_unfiltered (_("Relocate section '%s' of %s\n"),
00967                        sectp->name, objfile_name (objfile));
00968 
00969   return bfd_simple_get_relocated_section_contents (abfd, sectp, buf, NULL);
00970 }
00971 
00972 static void
00973 macho_symfile_finish (struct objfile *objfile)
00974 {
00975 }
00976 
00977 static void
00978 macho_symfile_offsets (struct objfile *objfile,
00979                        const struct section_addr_info *addrs)
00980 {
00981   unsigned int i;
00982   unsigned int num_sections;
00983   struct obj_section *osect;
00984 
00985   /* Allocate section_offsets.  */
00986   objfile->num_sections = bfd_count_sections (objfile->obfd);
00987   objfile->section_offsets = (struct section_offsets *)
00988     obstack_alloc (&objfile->objfile_obstack,
00989                    SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
00990   memset (objfile->section_offsets, 0,
00991           SIZEOF_N_SECTION_OFFSETS (objfile->num_sections));
00992 
00993   /* This code is run when we first add the objfile with
00994      symfile_add_with_addrs_or_offsets, when "addrs" not "offsets" are
00995      passed in.  The place in symfile.c where the addrs are applied
00996      depends on the addrs having section names.  But in the dyld code
00997      we build an anonymous array of addrs, so that code is a no-op.
00998      Because of that, we have to apply the addrs to the sections here.
00999      N.B. if an objfile slides after we've already created it, then it
01000      goes through objfile_relocate.  */
01001 
01002   for (i = 0; i < addrs->num_sections; i++)
01003     {
01004       ALL_OBJFILE_OSECTIONS (objfile, osect)
01005         {
01006           const char *bfd_sect_name = osect->the_bfd_section->name;
01007 
01008           if (strcmp (bfd_sect_name, addrs->other[i].name) == 0)
01009             {
01010               obj_section_offset (osect) = addrs->other[i].addr;
01011               break;
01012             }
01013         }
01014     }
01015 
01016   objfile->sect_index_text = 0;
01017 
01018   ALL_OBJFILE_OSECTIONS (objfile, osect)
01019     {
01020       const char *bfd_sect_name = osect->the_bfd_section->name;
01021       int sect_index = osect - objfile->sections;;
01022 
01023       if (strncmp (bfd_sect_name, "LC_SEGMENT.", 11) == 0)
01024         bfd_sect_name += 11;
01025       if (strcmp (bfd_sect_name, "__TEXT") == 0
01026           || strcmp (bfd_sect_name, "__TEXT.__text") == 0)
01027         objfile->sect_index_text = sect_index;
01028     }
01029 }
01030 
01031 static const struct sym_fns macho_sym_fns = {
01032   macho_new_init,               /* init anything gbl to entire symtab */
01033   macho_symfile_init,           /* read initial info, setup for sym_read() */
01034   macho_symfile_read,           /* read a symbol file into symtab */
01035   NULL,                         /* sym_read_psymbols */
01036   macho_symfile_finish,         /* finished with file, cleanup */
01037   macho_symfile_offsets,        /* xlate external to internal form */
01038   default_symfile_segments,     /* Get segment information from a file.  */
01039   NULL,
01040   macho_symfile_relocate,       /* Relocate a debug section.  */
01041   NULL,                         /* sym_get_probes */
01042   &psym_functions
01043 };
01044 
01045 /* -Wmissing-prototypes */
01046 extern initialize_file_ftype _initialize_machoread;
01047 
01048 void
01049 _initialize_machoread (void)
01050 {
01051   add_symtab_fns (bfd_target_mach_o_flavour, &macho_sym_fns);
01052 
01053   add_setshow_zuinteger_cmd ("mach-o", class_obscure,
01054                              &mach_o_debug_level,
01055                              _("Set if printing Mach-O symbols processing."),
01056                              _("Show if printing Mach-O symbols processing."),
01057                              NULL, NULL, NULL,
01058                              &setdebuglist, &showdebuglist);
01059 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines