15796c8dcSSimon Schubert /* Read ELF (Executable and Linking Format) object files for GDB. 25796c8dcSSimon Schubert 35796c8dcSSimon Schubert Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 4*cf7f2e2dSJohn Marino 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 55796c8dcSSimon Schubert Free Software Foundation, Inc. 65796c8dcSSimon Schubert 75796c8dcSSimon Schubert Written by Fred Fish at Cygnus Support. 85796c8dcSSimon Schubert 95796c8dcSSimon Schubert This file is part of GDB. 105796c8dcSSimon Schubert 115796c8dcSSimon Schubert This program is free software; you can redistribute it and/or modify 125796c8dcSSimon Schubert it under the terms of the GNU General Public License as published by 135796c8dcSSimon Schubert the Free Software Foundation; either version 3 of the License, or 145796c8dcSSimon Schubert (at your option) any later version. 155796c8dcSSimon Schubert 165796c8dcSSimon Schubert This program is distributed in the hope that it will be useful, 175796c8dcSSimon Schubert but WITHOUT ANY WARRANTY; without even the implied warranty of 185796c8dcSSimon Schubert MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 195796c8dcSSimon Schubert GNU General Public License for more details. 205796c8dcSSimon Schubert 215796c8dcSSimon Schubert You should have received a copy of the GNU General Public License 225796c8dcSSimon Schubert along with this program. If not, see <http://www.gnu.org/licenses/>. */ 235796c8dcSSimon Schubert 245796c8dcSSimon Schubert #include "defs.h" 255796c8dcSSimon Schubert #include "bfd.h" 265796c8dcSSimon Schubert #include "gdb_string.h" 275796c8dcSSimon Schubert #include "elf-bfd.h" 285796c8dcSSimon Schubert #include "elf/common.h" 295796c8dcSSimon Schubert #include "elf/internal.h" 305796c8dcSSimon Schubert #include "elf/mips.h" 315796c8dcSSimon Schubert #include "symtab.h" 325796c8dcSSimon Schubert #include "symfile.h" 335796c8dcSSimon Schubert #include "objfiles.h" 345796c8dcSSimon Schubert #include "buildsym.h" 355796c8dcSSimon Schubert #include "stabsread.h" 365796c8dcSSimon Schubert #include "gdb-stabs.h" 375796c8dcSSimon Schubert #include "complaints.h" 385796c8dcSSimon Schubert #include "demangle.h" 39*cf7f2e2dSJohn Marino #include "psympriv.h" 405796c8dcSSimon Schubert 415796c8dcSSimon Schubert extern void _initialize_elfread (void); 425796c8dcSSimon Schubert 435796c8dcSSimon Schubert /* The struct elfinfo is available only during ELF symbol table and 445796c8dcSSimon Schubert psymtab reading. It is destroyed at the completion of psymtab-reading. 455796c8dcSSimon Schubert It's local to elf_symfile_read. */ 465796c8dcSSimon Schubert 475796c8dcSSimon Schubert struct elfinfo 485796c8dcSSimon Schubert { 495796c8dcSSimon Schubert asection *stabsect; /* Section pointer for .stab section */ 505796c8dcSSimon Schubert asection *stabindexsect; /* Section pointer for .stab.index section */ 515796c8dcSSimon Schubert asection *mdebugsect; /* Section pointer for .mdebug section */ 525796c8dcSSimon Schubert }; 535796c8dcSSimon Schubert 545796c8dcSSimon Schubert static void free_elfinfo (void *); 555796c8dcSSimon Schubert 565796c8dcSSimon Schubert /* Locate the segments in ABFD. */ 575796c8dcSSimon Schubert 585796c8dcSSimon Schubert static struct symfile_segment_data * 595796c8dcSSimon Schubert elf_symfile_segments (bfd *abfd) 605796c8dcSSimon Schubert { 615796c8dcSSimon Schubert Elf_Internal_Phdr *phdrs, **segments; 625796c8dcSSimon Schubert long phdrs_size; 635796c8dcSSimon Schubert int num_phdrs, num_segments, num_sections, i; 645796c8dcSSimon Schubert asection *sect; 655796c8dcSSimon Schubert struct symfile_segment_data *data; 665796c8dcSSimon Schubert 675796c8dcSSimon Schubert phdrs_size = bfd_get_elf_phdr_upper_bound (abfd); 685796c8dcSSimon Schubert if (phdrs_size == -1) 695796c8dcSSimon Schubert return NULL; 705796c8dcSSimon Schubert 715796c8dcSSimon Schubert phdrs = alloca (phdrs_size); 725796c8dcSSimon Schubert num_phdrs = bfd_get_elf_phdrs (abfd, phdrs); 735796c8dcSSimon Schubert if (num_phdrs == -1) 745796c8dcSSimon Schubert return NULL; 755796c8dcSSimon Schubert 765796c8dcSSimon Schubert num_segments = 0; 775796c8dcSSimon Schubert segments = alloca (sizeof (Elf_Internal_Phdr *) * num_phdrs); 785796c8dcSSimon Schubert for (i = 0; i < num_phdrs; i++) 795796c8dcSSimon Schubert if (phdrs[i].p_type == PT_LOAD) 805796c8dcSSimon Schubert segments[num_segments++] = &phdrs[i]; 815796c8dcSSimon Schubert 825796c8dcSSimon Schubert if (num_segments == 0) 835796c8dcSSimon Schubert return NULL; 845796c8dcSSimon Schubert 855796c8dcSSimon Schubert data = XZALLOC (struct symfile_segment_data); 865796c8dcSSimon Schubert data->num_segments = num_segments; 875796c8dcSSimon Schubert data->segment_bases = XCALLOC (num_segments, CORE_ADDR); 885796c8dcSSimon Schubert data->segment_sizes = XCALLOC (num_segments, CORE_ADDR); 895796c8dcSSimon Schubert 905796c8dcSSimon Schubert for (i = 0; i < num_segments; i++) 915796c8dcSSimon Schubert { 925796c8dcSSimon Schubert data->segment_bases[i] = segments[i]->p_vaddr; 935796c8dcSSimon Schubert data->segment_sizes[i] = segments[i]->p_memsz; 945796c8dcSSimon Schubert } 955796c8dcSSimon Schubert 965796c8dcSSimon Schubert num_sections = bfd_count_sections (abfd); 975796c8dcSSimon Schubert data->segment_info = XCALLOC (num_sections, int); 985796c8dcSSimon Schubert 995796c8dcSSimon Schubert for (i = 0, sect = abfd->sections; sect != NULL; i++, sect = sect->next) 1005796c8dcSSimon Schubert { 1015796c8dcSSimon Schubert int j; 1025796c8dcSSimon Schubert CORE_ADDR vma; 1035796c8dcSSimon Schubert 1045796c8dcSSimon Schubert if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0) 1055796c8dcSSimon Schubert continue; 1065796c8dcSSimon Schubert 1075796c8dcSSimon Schubert vma = bfd_get_section_vma (abfd, sect); 1085796c8dcSSimon Schubert 1095796c8dcSSimon Schubert for (j = 0; j < num_segments; j++) 1105796c8dcSSimon Schubert if (segments[j]->p_memsz > 0 1115796c8dcSSimon Schubert && vma >= segments[j]->p_vaddr 1125796c8dcSSimon Schubert && (vma - segments[j]->p_vaddr) < segments[j]->p_memsz) 1135796c8dcSSimon Schubert { 1145796c8dcSSimon Schubert data->segment_info[i] = j + 1; 1155796c8dcSSimon Schubert break; 1165796c8dcSSimon Schubert } 1175796c8dcSSimon Schubert 118*cf7f2e2dSJohn Marino /* We should have found a segment for every non-empty section. 119*cf7f2e2dSJohn Marino If we haven't, we will not relocate this section by any 120*cf7f2e2dSJohn Marino offsets we apply to the segments. As an exception, do not 121*cf7f2e2dSJohn Marino warn about SHT_NOBITS sections; in normal ELF execution 122*cf7f2e2dSJohn Marino environments, SHT_NOBITS means zero-initialized and belongs 123*cf7f2e2dSJohn Marino in a segment, but in no-OS environments some tools (e.g. ARM 124*cf7f2e2dSJohn Marino RealView) use SHT_NOBITS for uninitialized data. Since it is 125*cf7f2e2dSJohn Marino uninitialized, it doesn't need a program header. Such 126*cf7f2e2dSJohn Marino binaries are not relocatable. */ 127*cf7f2e2dSJohn Marino if (bfd_get_section_size (sect) > 0 && j == num_segments 128*cf7f2e2dSJohn Marino && (bfd_get_section_flags (abfd, sect) & SEC_LOAD) != 0) 1295796c8dcSSimon Schubert warning (_("Loadable segment \"%s\" outside of ELF segments"), 1305796c8dcSSimon Schubert bfd_section_name (abfd, sect)); 1315796c8dcSSimon Schubert } 1325796c8dcSSimon Schubert 1335796c8dcSSimon Schubert return data; 1345796c8dcSSimon Schubert } 1355796c8dcSSimon Schubert 1365796c8dcSSimon Schubert /* We are called once per section from elf_symfile_read. We 1375796c8dcSSimon Schubert need to examine each section we are passed, check to see 1385796c8dcSSimon Schubert if it is something we are interested in processing, and 1395796c8dcSSimon Schubert if so, stash away some access information for the section. 1405796c8dcSSimon Schubert 1415796c8dcSSimon Schubert For now we recognize the dwarf debug information sections and 1425796c8dcSSimon Schubert line number sections from matching their section names. The 1435796c8dcSSimon Schubert ELF definition is no real help here since it has no direct 1445796c8dcSSimon Schubert knowledge of DWARF (by design, so any debugging format can be 1455796c8dcSSimon Schubert used). 1465796c8dcSSimon Schubert 1475796c8dcSSimon Schubert We also recognize the ".stab" sections used by the Sun compilers 1485796c8dcSSimon Schubert released with Solaris 2. 1495796c8dcSSimon Schubert 1505796c8dcSSimon Schubert FIXME: The section names should not be hardwired strings (what 1515796c8dcSSimon Schubert should they be? I don't think most object file formats have enough 1525796c8dcSSimon Schubert section flags to specify what kind of debug section it is 1535796c8dcSSimon Schubert -kingdon). */ 1545796c8dcSSimon Schubert 1555796c8dcSSimon Schubert static void 1565796c8dcSSimon Schubert elf_locate_sections (bfd *ignore_abfd, asection *sectp, void *eip) 1575796c8dcSSimon Schubert { 1585796c8dcSSimon Schubert struct elfinfo *ei; 1595796c8dcSSimon Schubert 1605796c8dcSSimon Schubert ei = (struct elfinfo *) eip; 1615796c8dcSSimon Schubert if (strcmp (sectp->name, ".stab") == 0) 1625796c8dcSSimon Schubert { 1635796c8dcSSimon Schubert ei->stabsect = sectp; 1645796c8dcSSimon Schubert } 1655796c8dcSSimon Schubert else if (strcmp (sectp->name, ".stab.index") == 0) 1665796c8dcSSimon Schubert { 1675796c8dcSSimon Schubert ei->stabindexsect = sectp; 1685796c8dcSSimon Schubert } 1695796c8dcSSimon Schubert else if (strcmp (sectp->name, ".mdebug") == 0) 1705796c8dcSSimon Schubert { 1715796c8dcSSimon Schubert ei->mdebugsect = sectp; 1725796c8dcSSimon Schubert } 1735796c8dcSSimon Schubert } 1745796c8dcSSimon Schubert 1755796c8dcSSimon Schubert static struct minimal_symbol * 176*cf7f2e2dSJohn Marino record_minimal_symbol (const char *name, int name_len, int copy_name, 177*cf7f2e2dSJohn Marino CORE_ADDR address, 1785796c8dcSSimon Schubert enum minimal_symbol_type ms_type, 1795796c8dcSSimon Schubert asection *bfd_section, struct objfile *objfile) 1805796c8dcSSimon Schubert { 1815796c8dcSSimon Schubert struct gdbarch *gdbarch = get_objfile_arch (objfile); 1825796c8dcSSimon Schubert 1835796c8dcSSimon Schubert if (ms_type == mst_text || ms_type == mst_file_text) 1845796c8dcSSimon Schubert address = gdbarch_smash_text_address (gdbarch, address); 1855796c8dcSSimon Schubert 186*cf7f2e2dSJohn Marino return prim_record_minimal_symbol_full (name, name_len, copy_name, address, 187*cf7f2e2dSJohn Marino ms_type, bfd_section->index, 188*cf7f2e2dSJohn Marino bfd_section, objfile); 1895796c8dcSSimon Schubert } 1905796c8dcSSimon Schubert 1915796c8dcSSimon Schubert /* 1925796c8dcSSimon Schubert 1935796c8dcSSimon Schubert LOCAL FUNCTION 1945796c8dcSSimon Schubert 1955796c8dcSSimon Schubert elf_symtab_read -- read the symbol table of an ELF file 1965796c8dcSSimon Schubert 1975796c8dcSSimon Schubert SYNOPSIS 1985796c8dcSSimon Schubert 1995796c8dcSSimon Schubert void elf_symtab_read (struct objfile *objfile, int type, 2005796c8dcSSimon Schubert long number_of_symbols, asymbol **symbol_table) 2015796c8dcSSimon Schubert 2025796c8dcSSimon Schubert DESCRIPTION 2035796c8dcSSimon Schubert 2045796c8dcSSimon Schubert Given an objfile, a symbol table, and a flag indicating whether the 2055796c8dcSSimon Schubert symbol table contains regular, dynamic, or synthetic symbols, add all 2065796c8dcSSimon Schubert the global function and data symbols to the minimal symbol table. 2075796c8dcSSimon Schubert 2085796c8dcSSimon Schubert In stabs-in-ELF, as implemented by Sun, there are some local symbols 2095796c8dcSSimon Schubert defined in the ELF symbol table, which can be used to locate 2105796c8dcSSimon Schubert the beginnings of sections from each ".o" file that was linked to 2115796c8dcSSimon Schubert form the executable objfile. We gather any such info and record it 2125796c8dcSSimon Schubert in data structures hung off the objfile's private data. 2135796c8dcSSimon Schubert 2145796c8dcSSimon Schubert */ 2155796c8dcSSimon Schubert 2165796c8dcSSimon Schubert #define ST_REGULAR 0 2175796c8dcSSimon Schubert #define ST_DYNAMIC 1 2185796c8dcSSimon Schubert #define ST_SYNTHETIC 2 2195796c8dcSSimon Schubert 2205796c8dcSSimon Schubert static void 2215796c8dcSSimon Schubert elf_symtab_read (struct objfile *objfile, int type, 222*cf7f2e2dSJohn Marino long number_of_symbols, asymbol **symbol_table, 223*cf7f2e2dSJohn Marino int copy_names) 2245796c8dcSSimon Schubert { 2255796c8dcSSimon Schubert struct gdbarch *gdbarch = get_objfile_arch (objfile); 2265796c8dcSSimon Schubert asymbol *sym; 2275796c8dcSSimon Schubert long i; 2285796c8dcSSimon Schubert CORE_ADDR symaddr; 2295796c8dcSSimon Schubert CORE_ADDR offset; 2305796c8dcSSimon Schubert enum minimal_symbol_type ms_type; 2315796c8dcSSimon Schubert /* If sectinfo is nonNULL, it contains section info that should end up 2325796c8dcSSimon Schubert filed in the objfile. */ 2335796c8dcSSimon Schubert struct stab_section_info *sectinfo = NULL; 2345796c8dcSSimon Schubert /* If filesym is nonzero, it points to a file symbol, but we haven't 2355796c8dcSSimon Schubert seen any section info for it yet. */ 2365796c8dcSSimon Schubert asymbol *filesym = 0; 237*cf7f2e2dSJohn Marino /* Name of filesym. This is either a constant string or is saved on 238*cf7f2e2dSJohn Marino the objfile's obstack. */ 239*cf7f2e2dSJohn Marino char *filesymname = ""; 2405796c8dcSSimon Schubert struct dbx_symfile_info *dbx = objfile->deprecated_sym_stab_info; 2415796c8dcSSimon Schubert int stripped = (bfd_get_symcount (objfile->obfd) == 0); 2425796c8dcSSimon Schubert 2435796c8dcSSimon Schubert for (i = 0; i < number_of_symbols; i++) 2445796c8dcSSimon Schubert { 2455796c8dcSSimon Schubert sym = symbol_table[i]; 2465796c8dcSSimon Schubert if (sym->name == NULL || *sym->name == '\0') 2475796c8dcSSimon Schubert { 2485796c8dcSSimon Schubert /* Skip names that don't exist (shouldn't happen), or names 2495796c8dcSSimon Schubert that are null strings (may happen). */ 2505796c8dcSSimon Schubert continue; 2515796c8dcSSimon Schubert } 2525796c8dcSSimon Schubert 2535796c8dcSSimon Schubert /* Skip "special" symbols, e.g. ARM mapping symbols. These are 2545796c8dcSSimon Schubert symbols which do not correspond to objects in the symbol table, 2555796c8dcSSimon Schubert but have some other target-specific meaning. */ 2565796c8dcSSimon Schubert if (bfd_is_target_special_symbol (objfile->obfd, sym)) 2575796c8dcSSimon Schubert { 2585796c8dcSSimon Schubert if (gdbarch_record_special_symbol_p (gdbarch)) 2595796c8dcSSimon Schubert gdbarch_record_special_symbol (gdbarch, objfile, sym); 2605796c8dcSSimon Schubert continue; 2615796c8dcSSimon Schubert } 2625796c8dcSSimon Schubert 2635796c8dcSSimon Schubert offset = ANOFFSET (objfile->section_offsets, sym->section->index); 2645796c8dcSSimon Schubert if (type == ST_DYNAMIC 2655796c8dcSSimon Schubert && sym->section == &bfd_und_section 2665796c8dcSSimon Schubert && (sym->flags & BSF_FUNCTION)) 2675796c8dcSSimon Schubert { 2685796c8dcSSimon Schubert struct minimal_symbol *msym; 2695796c8dcSSimon Schubert bfd *abfd = objfile->obfd; 2705796c8dcSSimon Schubert asection *sect; 2715796c8dcSSimon Schubert 2725796c8dcSSimon Schubert /* Symbol is a reference to a function defined in 2735796c8dcSSimon Schubert a shared library. 2745796c8dcSSimon Schubert If its value is non zero then it is usually the address 2755796c8dcSSimon Schubert of the corresponding entry in the procedure linkage table, 2765796c8dcSSimon Schubert plus the desired section offset. 2775796c8dcSSimon Schubert If its value is zero then the dynamic linker has to resolve 2785796c8dcSSimon Schubert the symbol. We are unable to find any meaningful address 2795796c8dcSSimon Schubert for this symbol in the executable file, so we skip it. */ 2805796c8dcSSimon Schubert symaddr = sym->value; 2815796c8dcSSimon Schubert if (symaddr == 0) 2825796c8dcSSimon Schubert continue; 2835796c8dcSSimon Schubert 2845796c8dcSSimon Schubert /* sym->section is the undefined section. However, we want to 2855796c8dcSSimon Schubert record the section where the PLT stub resides with the 2865796c8dcSSimon Schubert minimal symbol. Search the section table for the one that 2875796c8dcSSimon Schubert covers the stub's address. */ 2885796c8dcSSimon Schubert for (sect = abfd->sections; sect != NULL; sect = sect->next) 2895796c8dcSSimon Schubert { 2905796c8dcSSimon Schubert if ((bfd_get_section_flags (abfd, sect) & SEC_ALLOC) == 0) 2915796c8dcSSimon Schubert continue; 2925796c8dcSSimon Schubert 2935796c8dcSSimon Schubert if (symaddr >= bfd_get_section_vma (abfd, sect) 2945796c8dcSSimon Schubert && symaddr < bfd_get_section_vma (abfd, sect) 2955796c8dcSSimon Schubert + bfd_get_section_size (sect)) 2965796c8dcSSimon Schubert break; 2975796c8dcSSimon Schubert } 2985796c8dcSSimon Schubert if (!sect) 2995796c8dcSSimon Schubert continue; 3005796c8dcSSimon Schubert 3015796c8dcSSimon Schubert symaddr += ANOFFSET (objfile->section_offsets, sect->index); 3025796c8dcSSimon Schubert 3035796c8dcSSimon Schubert msym = record_minimal_symbol 304*cf7f2e2dSJohn Marino (sym->name, strlen (sym->name), copy_names, 305*cf7f2e2dSJohn Marino symaddr, mst_solib_trampoline, sect, objfile); 3065796c8dcSSimon Schubert if (msym != NULL) 3075796c8dcSSimon Schubert msym->filename = filesymname; 3085796c8dcSSimon Schubert continue; 3095796c8dcSSimon Schubert } 3105796c8dcSSimon Schubert 3115796c8dcSSimon Schubert /* If it is a nonstripped executable, do not enter dynamic 3125796c8dcSSimon Schubert symbols, as the dynamic symbol table is usually a subset 3135796c8dcSSimon Schubert of the main symbol table. */ 3145796c8dcSSimon Schubert if (type == ST_DYNAMIC && !stripped) 3155796c8dcSSimon Schubert continue; 3165796c8dcSSimon Schubert if (sym->flags & BSF_FILE) 3175796c8dcSSimon Schubert { 3185796c8dcSSimon Schubert /* STT_FILE debugging symbol that helps stabs-in-elf debugging. 3195796c8dcSSimon Schubert Chain any old one onto the objfile; remember new sym. */ 3205796c8dcSSimon Schubert if (sectinfo != NULL) 3215796c8dcSSimon Schubert { 3225796c8dcSSimon Schubert sectinfo->next = dbx->stab_section_info; 3235796c8dcSSimon Schubert dbx->stab_section_info = sectinfo; 3245796c8dcSSimon Schubert sectinfo = NULL; 3255796c8dcSSimon Schubert } 3265796c8dcSSimon Schubert filesym = sym; 3275796c8dcSSimon Schubert filesymname = 3285796c8dcSSimon Schubert obsavestring ((char *) filesym->name, strlen (filesym->name), 3295796c8dcSSimon Schubert &objfile->objfile_obstack); 3305796c8dcSSimon Schubert } 3315796c8dcSSimon Schubert else if (sym->flags & BSF_SECTION_SYM) 3325796c8dcSSimon Schubert continue; 3335796c8dcSSimon Schubert else if (sym->flags & (BSF_GLOBAL | BSF_LOCAL | BSF_WEAK)) 3345796c8dcSSimon Schubert { 3355796c8dcSSimon Schubert struct minimal_symbol *msym; 3365796c8dcSSimon Schubert 3375796c8dcSSimon Schubert /* Select global/local/weak symbols. Note that bfd puts abs 3385796c8dcSSimon Schubert symbols in their own section, so all symbols we are 3395796c8dcSSimon Schubert interested in will have a section. */ 3405796c8dcSSimon Schubert /* Bfd symbols are section relative. */ 3415796c8dcSSimon Schubert symaddr = sym->value + sym->section->vma; 3425796c8dcSSimon Schubert /* Relocate all non-absolute and non-TLS symbols by the 3435796c8dcSSimon Schubert section offset. */ 3445796c8dcSSimon Schubert if (sym->section != &bfd_abs_section 3455796c8dcSSimon Schubert && !(sym->section->flags & SEC_THREAD_LOCAL)) 3465796c8dcSSimon Schubert { 3475796c8dcSSimon Schubert symaddr += offset; 3485796c8dcSSimon Schubert } 3495796c8dcSSimon Schubert /* For non-absolute symbols, use the type of the section 3505796c8dcSSimon Schubert they are relative to, to intuit text/data. Bfd provides 3515796c8dcSSimon Schubert no way of figuring this out for absolute symbols. */ 3525796c8dcSSimon Schubert if (sym->section == &bfd_abs_section) 3535796c8dcSSimon Schubert { 3545796c8dcSSimon Schubert /* This is a hack to get the minimal symbol type 3555796c8dcSSimon Schubert right for Irix 5, which has absolute addresses 3565796c8dcSSimon Schubert with special section indices for dynamic symbols. 3575796c8dcSSimon Schubert 3585796c8dcSSimon Schubert NOTE: uweigand-20071112: Synthetic symbols do not 3595796c8dcSSimon Schubert have an ELF-private part, so do not touch those. */ 3605796c8dcSSimon Schubert unsigned int shndx = type == ST_SYNTHETIC ? 0 : 3615796c8dcSSimon Schubert ((elf_symbol_type *) sym)->internal_elf_sym.st_shndx; 3625796c8dcSSimon Schubert 3635796c8dcSSimon Schubert switch (shndx) 3645796c8dcSSimon Schubert { 3655796c8dcSSimon Schubert case SHN_MIPS_TEXT: 3665796c8dcSSimon Schubert ms_type = mst_text; 3675796c8dcSSimon Schubert break; 3685796c8dcSSimon Schubert case SHN_MIPS_DATA: 3695796c8dcSSimon Schubert ms_type = mst_data; 3705796c8dcSSimon Schubert break; 3715796c8dcSSimon Schubert case SHN_MIPS_ACOMMON: 3725796c8dcSSimon Schubert ms_type = mst_bss; 3735796c8dcSSimon Schubert break; 3745796c8dcSSimon Schubert default: 3755796c8dcSSimon Schubert ms_type = mst_abs; 3765796c8dcSSimon Schubert } 3775796c8dcSSimon Schubert 3785796c8dcSSimon Schubert /* If it is an Irix dynamic symbol, skip section name 3795796c8dcSSimon Schubert symbols, relocate all others by section offset. */ 3805796c8dcSSimon Schubert if (ms_type != mst_abs) 3815796c8dcSSimon Schubert { 3825796c8dcSSimon Schubert if (sym->name[0] == '.') 3835796c8dcSSimon Schubert continue; 3845796c8dcSSimon Schubert symaddr += offset; 3855796c8dcSSimon Schubert } 3865796c8dcSSimon Schubert } 3875796c8dcSSimon Schubert else if (sym->section->flags & SEC_CODE) 3885796c8dcSSimon Schubert { 3895796c8dcSSimon Schubert if (sym->flags & (BSF_GLOBAL | BSF_WEAK)) 3905796c8dcSSimon Schubert { 3915796c8dcSSimon Schubert ms_type = mst_text; 3925796c8dcSSimon Schubert } 3935796c8dcSSimon Schubert else if ((sym->name[0] == '.' && sym->name[1] == 'L') 3945796c8dcSSimon Schubert || ((sym->flags & BSF_LOCAL) 3955796c8dcSSimon Schubert && sym->name[0] == '$' 3965796c8dcSSimon Schubert && sym->name[1] == 'L')) 3975796c8dcSSimon Schubert /* Looks like a compiler-generated label. Skip 3985796c8dcSSimon Schubert it. The assembler should be skipping these (to 3995796c8dcSSimon Schubert keep executables small), but apparently with 4005796c8dcSSimon Schubert gcc on the (deleted) delta m88k SVR4, it loses. 4015796c8dcSSimon Schubert So to have us check too should be harmless (but 4025796c8dcSSimon Schubert I encourage people to fix this in the assembler 4035796c8dcSSimon Schubert instead of adding checks here). */ 4045796c8dcSSimon Schubert continue; 4055796c8dcSSimon Schubert else 4065796c8dcSSimon Schubert { 4075796c8dcSSimon Schubert ms_type = mst_file_text; 4085796c8dcSSimon Schubert } 4095796c8dcSSimon Schubert } 4105796c8dcSSimon Schubert else if (sym->section->flags & SEC_ALLOC) 4115796c8dcSSimon Schubert { 4125796c8dcSSimon Schubert if (sym->flags & (BSF_GLOBAL | BSF_WEAK)) 4135796c8dcSSimon Schubert { 4145796c8dcSSimon Schubert if (sym->section->flags & SEC_LOAD) 4155796c8dcSSimon Schubert { 4165796c8dcSSimon Schubert ms_type = mst_data; 4175796c8dcSSimon Schubert } 4185796c8dcSSimon Schubert else 4195796c8dcSSimon Schubert { 4205796c8dcSSimon Schubert ms_type = mst_bss; 4215796c8dcSSimon Schubert } 4225796c8dcSSimon Schubert } 4235796c8dcSSimon Schubert else if (sym->flags & BSF_LOCAL) 4245796c8dcSSimon Schubert { 4255796c8dcSSimon Schubert /* Named Local variable in a Data section. 4265796c8dcSSimon Schubert Check its name for stabs-in-elf. */ 4275796c8dcSSimon Schubert int special_local_sect; 428*cf7f2e2dSJohn Marino 4295796c8dcSSimon Schubert if (strcmp ("Bbss.bss", sym->name) == 0) 4305796c8dcSSimon Schubert special_local_sect = SECT_OFF_BSS (objfile); 4315796c8dcSSimon Schubert else if (strcmp ("Ddata.data", sym->name) == 0) 4325796c8dcSSimon Schubert special_local_sect = SECT_OFF_DATA (objfile); 4335796c8dcSSimon Schubert else if (strcmp ("Drodata.rodata", sym->name) == 0) 4345796c8dcSSimon Schubert special_local_sect = SECT_OFF_RODATA (objfile); 4355796c8dcSSimon Schubert else 4365796c8dcSSimon Schubert special_local_sect = -1; 4375796c8dcSSimon Schubert if (special_local_sect >= 0) 4385796c8dcSSimon Schubert { 4395796c8dcSSimon Schubert /* Found a special local symbol. Allocate a 4405796c8dcSSimon Schubert sectinfo, if needed, and fill it in. */ 4415796c8dcSSimon Schubert if (sectinfo == NULL) 4425796c8dcSSimon Schubert { 4435796c8dcSSimon Schubert int max_index; 4445796c8dcSSimon Schubert size_t size; 4455796c8dcSSimon Schubert 4465796c8dcSSimon Schubert max_index = SECT_OFF_BSS (objfile); 4475796c8dcSSimon Schubert if (objfile->sect_index_data > max_index) 4485796c8dcSSimon Schubert max_index = objfile->sect_index_data; 4495796c8dcSSimon Schubert if (objfile->sect_index_rodata > max_index) 4505796c8dcSSimon Schubert max_index = objfile->sect_index_rodata; 4515796c8dcSSimon Schubert 4525796c8dcSSimon Schubert /* max_index is the largest index we'll 4535796c8dcSSimon Schubert use into this array, so we must 4545796c8dcSSimon Schubert allocate max_index+1 elements for it. 4555796c8dcSSimon Schubert However, 'struct stab_section_info' 4565796c8dcSSimon Schubert already includes one element, so we 4575796c8dcSSimon Schubert need to allocate max_index aadditional 4585796c8dcSSimon Schubert elements. */ 4595796c8dcSSimon Schubert size = (sizeof (struct stab_section_info) 4605796c8dcSSimon Schubert + (sizeof (CORE_ADDR) 4615796c8dcSSimon Schubert * max_index)); 4625796c8dcSSimon Schubert sectinfo = (struct stab_section_info *) 4635796c8dcSSimon Schubert xmalloc (size); 4645796c8dcSSimon Schubert memset (sectinfo, 0, size); 4655796c8dcSSimon Schubert sectinfo->num_sections = max_index; 4665796c8dcSSimon Schubert if (filesym == NULL) 4675796c8dcSSimon Schubert { 4685796c8dcSSimon Schubert complaint (&symfile_complaints, 4695796c8dcSSimon Schubert _("elf/stab section information %s without a preceding file symbol"), 4705796c8dcSSimon Schubert sym->name); 4715796c8dcSSimon Schubert } 4725796c8dcSSimon Schubert else 4735796c8dcSSimon Schubert { 4745796c8dcSSimon Schubert sectinfo->filename = 4755796c8dcSSimon Schubert (char *) filesym->name; 4765796c8dcSSimon Schubert } 4775796c8dcSSimon Schubert } 4785796c8dcSSimon Schubert if (sectinfo->sections[special_local_sect] != 0) 4795796c8dcSSimon Schubert complaint (&symfile_complaints, 4805796c8dcSSimon Schubert _("duplicated elf/stab section information for %s"), 4815796c8dcSSimon Schubert sectinfo->filename); 4825796c8dcSSimon Schubert /* BFD symbols are section relative. */ 4835796c8dcSSimon Schubert symaddr = sym->value + sym->section->vma; 4845796c8dcSSimon Schubert /* Relocate non-absolute symbols by the 4855796c8dcSSimon Schubert section offset. */ 4865796c8dcSSimon Schubert if (sym->section != &bfd_abs_section) 4875796c8dcSSimon Schubert symaddr += offset; 4885796c8dcSSimon Schubert sectinfo->sections[special_local_sect] = symaddr; 4895796c8dcSSimon Schubert /* The special local symbols don't go in the 4905796c8dcSSimon Schubert minimal symbol table, so ignore this one. */ 4915796c8dcSSimon Schubert continue; 4925796c8dcSSimon Schubert } 4935796c8dcSSimon Schubert /* Not a special stabs-in-elf symbol, do regular 4945796c8dcSSimon Schubert symbol processing. */ 4955796c8dcSSimon Schubert if (sym->section->flags & SEC_LOAD) 4965796c8dcSSimon Schubert { 4975796c8dcSSimon Schubert ms_type = mst_file_data; 4985796c8dcSSimon Schubert } 4995796c8dcSSimon Schubert else 5005796c8dcSSimon Schubert { 5015796c8dcSSimon Schubert ms_type = mst_file_bss; 5025796c8dcSSimon Schubert } 5035796c8dcSSimon Schubert } 5045796c8dcSSimon Schubert else 5055796c8dcSSimon Schubert { 5065796c8dcSSimon Schubert ms_type = mst_unknown; 5075796c8dcSSimon Schubert } 5085796c8dcSSimon Schubert } 5095796c8dcSSimon Schubert else 5105796c8dcSSimon Schubert { 5115796c8dcSSimon Schubert /* FIXME: Solaris2 shared libraries include lots of 5125796c8dcSSimon Schubert odd "absolute" and "undefined" symbols, that play 5135796c8dcSSimon Schubert hob with actions like finding what function the PC 5145796c8dcSSimon Schubert is in. Ignore them if they aren't text, data, or bss. */ 5155796c8dcSSimon Schubert /* ms_type = mst_unknown; */ 5165796c8dcSSimon Schubert continue; /* Skip this symbol. */ 5175796c8dcSSimon Schubert } 5185796c8dcSSimon Schubert msym = record_minimal_symbol 519*cf7f2e2dSJohn Marino (sym->name, strlen (sym->name), copy_names, symaddr, 5205796c8dcSSimon Schubert ms_type, sym->section, objfile); 5215796c8dcSSimon Schubert 5225796c8dcSSimon Schubert if (msym) 5235796c8dcSSimon Schubert { 5245796c8dcSSimon Schubert /* Pass symbol size field in via BFD. FIXME!!! */ 5255796c8dcSSimon Schubert elf_symbol_type *elf_sym; 5265796c8dcSSimon Schubert 5275796c8dcSSimon Schubert /* NOTE: uweigand-20071112: A synthetic symbol does not have an 5285796c8dcSSimon Schubert ELF-private part. However, in some cases (e.g. synthetic 5295796c8dcSSimon Schubert 'dot' symbols on ppc64) the udata.p entry is set to point back 5305796c8dcSSimon Schubert to the original ELF symbol it was derived from. Get the size 5315796c8dcSSimon Schubert from that symbol. */ 5325796c8dcSSimon Schubert if (type != ST_SYNTHETIC) 5335796c8dcSSimon Schubert elf_sym = (elf_symbol_type *) sym; 5345796c8dcSSimon Schubert else 5355796c8dcSSimon Schubert elf_sym = (elf_symbol_type *) sym->udata.p; 5365796c8dcSSimon Schubert 5375796c8dcSSimon Schubert if (elf_sym) 5385796c8dcSSimon Schubert MSYMBOL_SIZE(msym) = elf_sym->internal_elf_sym.st_size; 539*cf7f2e2dSJohn Marino 5405796c8dcSSimon Schubert msym->filename = filesymname; 5415796c8dcSSimon Schubert gdbarch_elf_make_msymbol_special (gdbarch, sym, msym); 542*cf7f2e2dSJohn Marino } 5435796c8dcSSimon Schubert 5445796c8dcSSimon Schubert /* For @plt symbols, also record a trampoline to the 5455796c8dcSSimon Schubert destination symbol. The @plt symbol will be used in 5465796c8dcSSimon Schubert disassembly, and the trampoline will be used when we are 5475796c8dcSSimon Schubert trying to find the target. */ 5485796c8dcSSimon Schubert if (msym && ms_type == mst_text && type == ST_SYNTHETIC) 5495796c8dcSSimon Schubert { 5505796c8dcSSimon Schubert int len = strlen (sym->name); 5515796c8dcSSimon Schubert 5525796c8dcSSimon Schubert if (len > 4 && strcmp (sym->name + len - 4, "@plt") == 0) 5535796c8dcSSimon Schubert { 5545796c8dcSSimon Schubert struct minimal_symbol *mtramp; 5555796c8dcSSimon Schubert 556*cf7f2e2dSJohn Marino mtramp = record_minimal_symbol (sym->name, len - 4, 1, 557*cf7f2e2dSJohn Marino symaddr, 5585796c8dcSSimon Schubert mst_solib_trampoline, 5595796c8dcSSimon Schubert sym->section, objfile); 5605796c8dcSSimon Schubert if (mtramp) 5615796c8dcSSimon Schubert { 5625796c8dcSSimon Schubert MSYMBOL_SIZE (mtramp) = MSYMBOL_SIZE (msym); 5635796c8dcSSimon Schubert mtramp->filename = filesymname; 5645796c8dcSSimon Schubert gdbarch_elf_make_msymbol_special (gdbarch, sym, mtramp); 5655796c8dcSSimon Schubert } 5665796c8dcSSimon Schubert } 5675796c8dcSSimon Schubert } 5685796c8dcSSimon Schubert } 5695796c8dcSSimon Schubert } 5705796c8dcSSimon Schubert } 5715796c8dcSSimon Schubert 572*cf7f2e2dSJohn Marino struct build_id 573*cf7f2e2dSJohn Marino { 574*cf7f2e2dSJohn Marino size_t size; 575*cf7f2e2dSJohn Marino gdb_byte data[1]; 576*cf7f2e2dSJohn Marino }; 577*cf7f2e2dSJohn Marino 578*cf7f2e2dSJohn Marino /* Locate NT_GNU_BUILD_ID from ABFD and return its content. */ 579*cf7f2e2dSJohn Marino 580*cf7f2e2dSJohn Marino static struct build_id * 581*cf7f2e2dSJohn Marino build_id_bfd_get (bfd *abfd) 582*cf7f2e2dSJohn Marino { 583*cf7f2e2dSJohn Marino struct build_id *retval; 584*cf7f2e2dSJohn Marino 585*cf7f2e2dSJohn Marino if (!bfd_check_format (abfd, bfd_object) 586*cf7f2e2dSJohn Marino || bfd_get_flavour (abfd) != bfd_target_elf_flavour 587*cf7f2e2dSJohn Marino || elf_tdata (abfd)->build_id == NULL) 588*cf7f2e2dSJohn Marino return NULL; 589*cf7f2e2dSJohn Marino 590*cf7f2e2dSJohn Marino retval = xmalloc (sizeof *retval - 1 + elf_tdata (abfd)->build_id_size); 591*cf7f2e2dSJohn Marino retval->size = elf_tdata (abfd)->build_id_size; 592*cf7f2e2dSJohn Marino memcpy (retval->data, elf_tdata (abfd)->build_id, retval->size); 593*cf7f2e2dSJohn Marino 594*cf7f2e2dSJohn Marino return retval; 595*cf7f2e2dSJohn Marino } 596*cf7f2e2dSJohn Marino 597*cf7f2e2dSJohn Marino /* Return if FILENAME has NT_GNU_BUILD_ID matching the CHECK value. */ 598*cf7f2e2dSJohn Marino 599*cf7f2e2dSJohn Marino static int 600*cf7f2e2dSJohn Marino build_id_verify (const char *filename, struct build_id *check) 601*cf7f2e2dSJohn Marino { 602*cf7f2e2dSJohn Marino bfd *abfd; 603*cf7f2e2dSJohn Marino struct build_id *found = NULL; 604*cf7f2e2dSJohn Marino int retval = 0; 605*cf7f2e2dSJohn Marino 606*cf7f2e2dSJohn Marino /* We expect to be silent on the non-existing files. */ 607*cf7f2e2dSJohn Marino abfd = bfd_open_maybe_remote (filename); 608*cf7f2e2dSJohn Marino if (abfd == NULL) 609*cf7f2e2dSJohn Marino return 0; 610*cf7f2e2dSJohn Marino 611*cf7f2e2dSJohn Marino found = build_id_bfd_get (abfd); 612*cf7f2e2dSJohn Marino 613*cf7f2e2dSJohn Marino if (found == NULL) 614*cf7f2e2dSJohn Marino warning (_("File \"%s\" has no build-id, file skipped"), filename); 615*cf7f2e2dSJohn Marino else if (found->size != check->size 616*cf7f2e2dSJohn Marino || memcmp (found->data, check->data, found->size) != 0) 617*cf7f2e2dSJohn Marino warning (_("File \"%s\" has a different build-id, file skipped"), filename); 618*cf7f2e2dSJohn Marino else 619*cf7f2e2dSJohn Marino retval = 1; 620*cf7f2e2dSJohn Marino 621*cf7f2e2dSJohn Marino gdb_bfd_close_or_warn (abfd); 622*cf7f2e2dSJohn Marino 623*cf7f2e2dSJohn Marino xfree (found); 624*cf7f2e2dSJohn Marino 625*cf7f2e2dSJohn Marino return retval; 626*cf7f2e2dSJohn Marino } 627*cf7f2e2dSJohn Marino 628*cf7f2e2dSJohn Marino static char * 629*cf7f2e2dSJohn Marino build_id_to_debug_filename (struct build_id *build_id) 630*cf7f2e2dSJohn Marino { 631*cf7f2e2dSJohn Marino char *link, *debugdir, *retval = NULL; 632*cf7f2e2dSJohn Marino 633*cf7f2e2dSJohn Marino /* DEBUG_FILE_DIRECTORY/.build-id/ab/cdef */ 634*cf7f2e2dSJohn Marino link = alloca (strlen (debug_file_directory) + (sizeof "/.build-id/" - 1) + 1 635*cf7f2e2dSJohn Marino + 2 * build_id->size + (sizeof ".debug" - 1) + 1); 636*cf7f2e2dSJohn Marino 637*cf7f2e2dSJohn Marino /* Keep backward compatibility so that DEBUG_FILE_DIRECTORY being "" will 638*cf7f2e2dSJohn Marino cause "/.build-id/..." lookups. */ 639*cf7f2e2dSJohn Marino 640*cf7f2e2dSJohn Marino debugdir = debug_file_directory; 641*cf7f2e2dSJohn Marino do 642*cf7f2e2dSJohn Marino { 643*cf7f2e2dSJohn Marino char *s, *debugdir_end; 644*cf7f2e2dSJohn Marino gdb_byte *data = build_id->data; 645*cf7f2e2dSJohn Marino size_t size = build_id->size; 646*cf7f2e2dSJohn Marino 647*cf7f2e2dSJohn Marino while (*debugdir == DIRNAME_SEPARATOR) 648*cf7f2e2dSJohn Marino debugdir++; 649*cf7f2e2dSJohn Marino 650*cf7f2e2dSJohn Marino debugdir_end = strchr (debugdir, DIRNAME_SEPARATOR); 651*cf7f2e2dSJohn Marino if (debugdir_end == NULL) 652*cf7f2e2dSJohn Marino debugdir_end = &debugdir[strlen (debugdir)]; 653*cf7f2e2dSJohn Marino 654*cf7f2e2dSJohn Marino memcpy (link, debugdir, debugdir_end - debugdir); 655*cf7f2e2dSJohn Marino s = &link[debugdir_end - debugdir]; 656*cf7f2e2dSJohn Marino s += sprintf (s, "/.build-id/"); 657*cf7f2e2dSJohn Marino if (size > 0) 658*cf7f2e2dSJohn Marino { 659*cf7f2e2dSJohn Marino size--; 660*cf7f2e2dSJohn Marino s += sprintf (s, "%02x", (unsigned) *data++); 661*cf7f2e2dSJohn Marino } 662*cf7f2e2dSJohn Marino if (size > 0) 663*cf7f2e2dSJohn Marino *s++ = '/'; 664*cf7f2e2dSJohn Marino while (size-- > 0) 665*cf7f2e2dSJohn Marino s += sprintf (s, "%02x", (unsigned) *data++); 666*cf7f2e2dSJohn Marino strcpy (s, ".debug"); 667*cf7f2e2dSJohn Marino 668*cf7f2e2dSJohn Marino /* lrealpath() is expensive even for the usually non-existent files. */ 669*cf7f2e2dSJohn Marino if (access (link, F_OK) == 0) 670*cf7f2e2dSJohn Marino retval = lrealpath (link); 671*cf7f2e2dSJohn Marino 672*cf7f2e2dSJohn Marino if (retval != NULL && !build_id_verify (retval, build_id)) 673*cf7f2e2dSJohn Marino { 674*cf7f2e2dSJohn Marino xfree (retval); 675*cf7f2e2dSJohn Marino retval = NULL; 676*cf7f2e2dSJohn Marino } 677*cf7f2e2dSJohn Marino 678*cf7f2e2dSJohn Marino if (retval != NULL) 679*cf7f2e2dSJohn Marino break; 680*cf7f2e2dSJohn Marino 681*cf7f2e2dSJohn Marino debugdir = debugdir_end; 682*cf7f2e2dSJohn Marino } 683*cf7f2e2dSJohn Marino while (*debugdir != 0); 684*cf7f2e2dSJohn Marino 685*cf7f2e2dSJohn Marino return retval; 686*cf7f2e2dSJohn Marino } 687*cf7f2e2dSJohn Marino 688*cf7f2e2dSJohn Marino static char * 689*cf7f2e2dSJohn Marino find_separate_debug_file_by_buildid (struct objfile *objfile) 690*cf7f2e2dSJohn Marino { 691*cf7f2e2dSJohn Marino struct build_id *build_id; 692*cf7f2e2dSJohn Marino 693*cf7f2e2dSJohn Marino build_id = build_id_bfd_get (objfile->obfd); 694*cf7f2e2dSJohn Marino if (build_id != NULL) 695*cf7f2e2dSJohn Marino { 696*cf7f2e2dSJohn Marino char *build_id_name; 697*cf7f2e2dSJohn Marino 698*cf7f2e2dSJohn Marino build_id_name = build_id_to_debug_filename (build_id); 699*cf7f2e2dSJohn Marino xfree (build_id); 700*cf7f2e2dSJohn Marino /* Prevent looping on a stripped .debug file. */ 701*cf7f2e2dSJohn Marino if (build_id_name != NULL && strcmp (build_id_name, objfile->name) == 0) 702*cf7f2e2dSJohn Marino { 703*cf7f2e2dSJohn Marino warning (_("\"%s\": separate debug info file has no debug info"), 704*cf7f2e2dSJohn Marino build_id_name); 705*cf7f2e2dSJohn Marino xfree (build_id_name); 706*cf7f2e2dSJohn Marino } 707*cf7f2e2dSJohn Marino else if (build_id_name != NULL) 708*cf7f2e2dSJohn Marino return build_id_name; 709*cf7f2e2dSJohn Marino } 710*cf7f2e2dSJohn Marino return NULL; 711*cf7f2e2dSJohn Marino } 712*cf7f2e2dSJohn Marino 7135796c8dcSSimon Schubert /* Scan and build partial symbols for a symbol file. 7145796c8dcSSimon Schubert We have been initialized by a call to elf_symfile_init, which 7155796c8dcSSimon Schubert currently does nothing. 7165796c8dcSSimon Schubert 7175796c8dcSSimon Schubert SECTION_OFFSETS is a set of offsets to apply to relocate the symbols 7185796c8dcSSimon Schubert in each section. We simplify it down to a single offset for all 7195796c8dcSSimon Schubert symbols. FIXME. 7205796c8dcSSimon Schubert 7215796c8dcSSimon Schubert This function only does the minimum work necessary for letting the 7225796c8dcSSimon Schubert user "name" things symbolically; it does not read the entire symtab. 7235796c8dcSSimon Schubert Instead, it reads the external and static symbols and puts them in partial 7245796c8dcSSimon Schubert symbol tables. When more extensive information is requested of a 7255796c8dcSSimon Schubert file, the corresponding partial symbol table is mutated into a full 7265796c8dcSSimon Schubert fledged symbol table by going back and reading the symbols 7275796c8dcSSimon Schubert for real. 7285796c8dcSSimon Schubert 7295796c8dcSSimon Schubert We look for sections with specific names, to tell us what debug 7305796c8dcSSimon Schubert format to look for: FIXME!!! 7315796c8dcSSimon Schubert 7325796c8dcSSimon Schubert elfstab_build_psymtabs() handles STABS symbols; 7335796c8dcSSimon Schubert mdebug_build_psymtabs() handles ECOFF debugging information. 7345796c8dcSSimon Schubert 7355796c8dcSSimon Schubert Note that ELF files have a "minimal" symbol table, which looks a lot 7365796c8dcSSimon Schubert like a COFF symbol table, but has only the minimal information necessary 7375796c8dcSSimon Schubert for linking. We process this also, and use the information to 7385796c8dcSSimon Schubert build gdb's minimal symbol table. This gives us some minimal debugging 7395796c8dcSSimon Schubert capability even for files compiled without -g. */ 7405796c8dcSSimon Schubert 7415796c8dcSSimon Schubert static void 742*cf7f2e2dSJohn Marino elf_symfile_read (struct objfile *objfile, int symfile_flags) 7435796c8dcSSimon Schubert { 7445796c8dcSSimon Schubert bfd *abfd = objfile->obfd; 7455796c8dcSSimon Schubert struct elfinfo ei; 7465796c8dcSSimon Schubert struct cleanup *back_to; 7475796c8dcSSimon Schubert long symcount = 0, dynsymcount = 0, synthcount, storage_needed; 7485796c8dcSSimon Schubert asymbol **symbol_table = NULL, **dyn_symbol_table = NULL; 7495796c8dcSSimon Schubert asymbol *synthsyms; 7505796c8dcSSimon Schubert 7515796c8dcSSimon Schubert init_minimal_symbol_collection (); 7525796c8dcSSimon Schubert back_to = make_cleanup_discard_minimal_symbols (); 7535796c8dcSSimon Schubert 7545796c8dcSSimon Schubert memset ((char *) &ei, 0, sizeof (ei)); 7555796c8dcSSimon Schubert 7565796c8dcSSimon Schubert /* Allocate struct to keep track of the symfile */ 7575796c8dcSSimon Schubert objfile->deprecated_sym_stab_info = (struct dbx_symfile_info *) 7585796c8dcSSimon Schubert xmalloc (sizeof (struct dbx_symfile_info)); 7595796c8dcSSimon Schubert memset ((char *) objfile->deprecated_sym_stab_info, 0, sizeof (struct dbx_symfile_info)); 7605796c8dcSSimon Schubert make_cleanup (free_elfinfo, (void *) objfile); 7615796c8dcSSimon Schubert 7625796c8dcSSimon Schubert /* Process the normal ELF symbol table first. This may write some 7635796c8dcSSimon Schubert chain of info into the dbx_symfile_info in objfile->deprecated_sym_stab_info, 7645796c8dcSSimon Schubert which can later be used by elfstab_offset_sections. */ 7655796c8dcSSimon Schubert 7665796c8dcSSimon Schubert storage_needed = bfd_get_symtab_upper_bound (objfile->obfd); 7675796c8dcSSimon Schubert if (storage_needed < 0) 7685796c8dcSSimon Schubert error (_("Can't read symbols from %s: %s"), bfd_get_filename (objfile->obfd), 7695796c8dcSSimon Schubert bfd_errmsg (bfd_get_error ())); 7705796c8dcSSimon Schubert 7715796c8dcSSimon Schubert if (storage_needed > 0) 7725796c8dcSSimon Schubert { 7735796c8dcSSimon Schubert symbol_table = (asymbol **) xmalloc (storage_needed); 7745796c8dcSSimon Schubert make_cleanup (xfree, symbol_table); 7755796c8dcSSimon Schubert symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table); 7765796c8dcSSimon Schubert 7775796c8dcSSimon Schubert if (symcount < 0) 7785796c8dcSSimon Schubert error (_("Can't read symbols from %s: %s"), bfd_get_filename (objfile->obfd), 7795796c8dcSSimon Schubert bfd_errmsg (bfd_get_error ())); 7805796c8dcSSimon Schubert 781*cf7f2e2dSJohn Marino elf_symtab_read (objfile, ST_REGULAR, symcount, symbol_table, 0); 7825796c8dcSSimon Schubert } 7835796c8dcSSimon Schubert 7845796c8dcSSimon Schubert /* Add the dynamic symbols. */ 7855796c8dcSSimon Schubert 7865796c8dcSSimon Schubert storage_needed = bfd_get_dynamic_symtab_upper_bound (objfile->obfd); 7875796c8dcSSimon Schubert 7885796c8dcSSimon Schubert if (storage_needed > 0) 7895796c8dcSSimon Schubert { 7905796c8dcSSimon Schubert dyn_symbol_table = (asymbol **) xmalloc (storage_needed); 7915796c8dcSSimon Schubert make_cleanup (xfree, dyn_symbol_table); 7925796c8dcSSimon Schubert dynsymcount = bfd_canonicalize_dynamic_symtab (objfile->obfd, 7935796c8dcSSimon Schubert dyn_symbol_table); 7945796c8dcSSimon Schubert 7955796c8dcSSimon Schubert if (dynsymcount < 0) 7965796c8dcSSimon Schubert error (_("Can't read symbols from %s: %s"), bfd_get_filename (objfile->obfd), 7975796c8dcSSimon Schubert bfd_errmsg (bfd_get_error ())); 7985796c8dcSSimon Schubert 799*cf7f2e2dSJohn Marino elf_symtab_read (objfile, ST_DYNAMIC, dynsymcount, dyn_symbol_table, 0); 8005796c8dcSSimon Schubert } 8015796c8dcSSimon Schubert 8025796c8dcSSimon Schubert /* Add synthetic symbols - for instance, names for any PLT entries. */ 8035796c8dcSSimon Schubert 8045796c8dcSSimon Schubert synthcount = bfd_get_synthetic_symtab (abfd, symcount, symbol_table, 8055796c8dcSSimon Schubert dynsymcount, dyn_symbol_table, 8065796c8dcSSimon Schubert &synthsyms); 8075796c8dcSSimon Schubert if (synthcount > 0) 8085796c8dcSSimon Schubert { 8095796c8dcSSimon Schubert asymbol **synth_symbol_table; 8105796c8dcSSimon Schubert long i; 8115796c8dcSSimon Schubert 8125796c8dcSSimon Schubert make_cleanup (xfree, synthsyms); 8135796c8dcSSimon Schubert synth_symbol_table = xmalloc (sizeof (asymbol *) * synthcount); 8145796c8dcSSimon Schubert for (i = 0; i < synthcount; i++) 8155796c8dcSSimon Schubert synth_symbol_table[i] = synthsyms + i; 8165796c8dcSSimon Schubert make_cleanup (xfree, synth_symbol_table); 817*cf7f2e2dSJohn Marino elf_symtab_read (objfile, ST_SYNTHETIC, synthcount, synth_symbol_table, 1); 8185796c8dcSSimon Schubert } 8195796c8dcSSimon Schubert 8205796c8dcSSimon Schubert /* Install any minimal symbols that have been collected as the current 8215796c8dcSSimon Schubert minimal symbols for this objfile. The debug readers below this point 8225796c8dcSSimon Schubert should not generate new minimal symbols; if they do it's their 8235796c8dcSSimon Schubert responsibility to install them. "mdebug" appears to be the only one 8245796c8dcSSimon Schubert which will do this. */ 8255796c8dcSSimon Schubert 8265796c8dcSSimon Schubert install_minimal_symbols (objfile); 8275796c8dcSSimon Schubert do_cleanups (back_to); 8285796c8dcSSimon Schubert 8295796c8dcSSimon Schubert /* Now process debugging information, which is contained in 8305796c8dcSSimon Schubert special ELF sections. */ 8315796c8dcSSimon Schubert 8325796c8dcSSimon Schubert /* We first have to find them... */ 8335796c8dcSSimon Schubert bfd_map_over_sections (abfd, elf_locate_sections, (void *) & ei); 8345796c8dcSSimon Schubert 8355796c8dcSSimon Schubert /* ELF debugging information is inserted into the psymtab in the 8365796c8dcSSimon Schubert order of least informative first - most informative last. Since 8375796c8dcSSimon Schubert the psymtab table is searched `most recent insertion first' this 8385796c8dcSSimon Schubert increases the probability that more detailed debug information 8395796c8dcSSimon Schubert for a section is found. 8405796c8dcSSimon Schubert 8415796c8dcSSimon Schubert For instance, an object file might contain both .mdebug (XCOFF) 8425796c8dcSSimon Schubert and .debug_info (DWARF2) sections then .mdebug is inserted first 8435796c8dcSSimon Schubert (searched last) and DWARF2 is inserted last (searched first). If 8445796c8dcSSimon Schubert we don't do this then the XCOFF info is found first - for code in 8455796c8dcSSimon Schubert an included file XCOFF info is useless. */ 8465796c8dcSSimon Schubert 8475796c8dcSSimon Schubert if (ei.mdebugsect) 8485796c8dcSSimon Schubert { 8495796c8dcSSimon Schubert const struct ecoff_debug_swap *swap; 8505796c8dcSSimon Schubert 8515796c8dcSSimon Schubert /* .mdebug section, presumably holding ECOFF debugging 8525796c8dcSSimon Schubert information. */ 8535796c8dcSSimon Schubert swap = get_elf_backend_data (abfd)->elf_backend_ecoff_debug_swap; 8545796c8dcSSimon Schubert if (swap) 8555796c8dcSSimon Schubert elfmdebug_build_psymtabs (objfile, swap, ei.mdebugsect); 8565796c8dcSSimon Schubert } 8575796c8dcSSimon Schubert if (ei.stabsect) 8585796c8dcSSimon Schubert { 8595796c8dcSSimon Schubert asection *str_sect; 8605796c8dcSSimon Schubert 8615796c8dcSSimon Schubert /* Stab sections have an associated string table that looks like 8625796c8dcSSimon Schubert a separate section. */ 8635796c8dcSSimon Schubert str_sect = bfd_get_section_by_name (abfd, ".stabstr"); 8645796c8dcSSimon Schubert 8655796c8dcSSimon Schubert /* FIXME should probably warn about a stab section without a stabstr. */ 8665796c8dcSSimon Schubert if (str_sect) 8675796c8dcSSimon Schubert elfstab_build_psymtabs (objfile, 8685796c8dcSSimon Schubert ei.stabsect, 8695796c8dcSSimon Schubert str_sect->filepos, 8705796c8dcSSimon Schubert bfd_section_size (abfd, str_sect)); 8715796c8dcSSimon Schubert } 8725796c8dcSSimon Schubert if (dwarf2_has_info (objfile)) 8735796c8dcSSimon Schubert { 8745796c8dcSSimon Schubert /* DWARF 2 sections */ 875*cf7f2e2dSJohn Marino dwarf2_build_psymtabs (objfile); 8765796c8dcSSimon Schubert } 8775796c8dcSSimon Schubert 878*cf7f2e2dSJohn Marino /* If the file has its own symbol tables it has no separate debug info. 879*cf7f2e2dSJohn Marino `.dynsym'/`.symtab' go to MSYMBOLS, `.debug_info' goes to SYMTABS/PSYMTABS. 880*cf7f2e2dSJohn Marino `.gnu_debuglink' may no longer be present with `.note.gnu.build-id'. */ 881*cf7f2e2dSJohn Marino if (!objfile_has_partial_symbols (objfile)) 882*cf7f2e2dSJohn Marino { 883*cf7f2e2dSJohn Marino char *debugfile; 884*cf7f2e2dSJohn Marino 885*cf7f2e2dSJohn Marino debugfile = find_separate_debug_file_by_buildid (objfile); 886*cf7f2e2dSJohn Marino 887*cf7f2e2dSJohn Marino if (debugfile == NULL) 888*cf7f2e2dSJohn Marino debugfile = find_separate_debug_file_by_debuglink (objfile); 889*cf7f2e2dSJohn Marino 890*cf7f2e2dSJohn Marino if (debugfile) 891*cf7f2e2dSJohn Marino { 892*cf7f2e2dSJohn Marino bfd *abfd = symfile_bfd_open (debugfile); 893*cf7f2e2dSJohn Marino 894*cf7f2e2dSJohn Marino symbol_file_add_separate (abfd, symfile_flags, objfile); 895*cf7f2e2dSJohn Marino xfree (debugfile); 896*cf7f2e2dSJohn Marino } 897*cf7f2e2dSJohn Marino } 8985796c8dcSSimon Schubert } 8995796c8dcSSimon Schubert 9005796c8dcSSimon Schubert /* This cleans up the objfile's deprecated_sym_stab_info pointer, and 9015796c8dcSSimon Schubert the chain of stab_section_info's, that might be dangling from 9025796c8dcSSimon Schubert it. */ 9035796c8dcSSimon Schubert 9045796c8dcSSimon Schubert static void 9055796c8dcSSimon Schubert free_elfinfo (void *objp) 9065796c8dcSSimon Schubert { 9075796c8dcSSimon Schubert struct objfile *objfile = (struct objfile *) objp; 9085796c8dcSSimon Schubert struct dbx_symfile_info *dbxinfo = objfile->deprecated_sym_stab_info; 9095796c8dcSSimon Schubert struct stab_section_info *ssi, *nssi; 9105796c8dcSSimon Schubert 9115796c8dcSSimon Schubert ssi = dbxinfo->stab_section_info; 9125796c8dcSSimon Schubert while (ssi) 9135796c8dcSSimon Schubert { 9145796c8dcSSimon Schubert nssi = ssi->next; 9155796c8dcSSimon Schubert xfree (ssi); 9165796c8dcSSimon Schubert ssi = nssi; 9175796c8dcSSimon Schubert } 9185796c8dcSSimon Schubert 9195796c8dcSSimon Schubert dbxinfo->stab_section_info = 0; /* Just say No mo info about this. */ 9205796c8dcSSimon Schubert } 9215796c8dcSSimon Schubert 9225796c8dcSSimon Schubert 9235796c8dcSSimon Schubert /* Initialize anything that needs initializing when a completely new symbol 9245796c8dcSSimon Schubert file is specified (not just adding some symbols from another file, e.g. a 9255796c8dcSSimon Schubert shared library). 9265796c8dcSSimon Schubert 9275796c8dcSSimon Schubert We reinitialize buildsym, since we may be reading stabs from an ELF file. */ 9285796c8dcSSimon Schubert 9295796c8dcSSimon Schubert static void 9305796c8dcSSimon Schubert elf_new_init (struct objfile *ignore) 9315796c8dcSSimon Schubert { 9325796c8dcSSimon Schubert stabsread_new_init (); 9335796c8dcSSimon Schubert buildsym_new_init (); 9345796c8dcSSimon Schubert } 9355796c8dcSSimon Schubert 9365796c8dcSSimon Schubert /* Perform any local cleanups required when we are done with a particular 9375796c8dcSSimon Schubert objfile. I.E, we are in the process of discarding all symbol information 9385796c8dcSSimon Schubert for an objfile, freeing up all memory held for it, and unlinking the 9395796c8dcSSimon Schubert objfile struct from the global list of known objfiles. */ 9405796c8dcSSimon Schubert 9415796c8dcSSimon Schubert static void 9425796c8dcSSimon Schubert elf_symfile_finish (struct objfile *objfile) 9435796c8dcSSimon Schubert { 9445796c8dcSSimon Schubert if (objfile->deprecated_sym_stab_info != NULL) 9455796c8dcSSimon Schubert { 9465796c8dcSSimon Schubert xfree (objfile->deprecated_sym_stab_info); 9475796c8dcSSimon Schubert } 9485796c8dcSSimon Schubert 9495796c8dcSSimon Schubert dwarf2_free_objfile (objfile); 9505796c8dcSSimon Schubert } 9515796c8dcSSimon Schubert 9525796c8dcSSimon Schubert /* ELF specific initialization routine for reading symbols. 9535796c8dcSSimon Schubert 9545796c8dcSSimon Schubert It is passed a pointer to a struct sym_fns which contains, among other 9555796c8dcSSimon Schubert things, the BFD for the file whose symbols are being read, and a slot for 9565796c8dcSSimon Schubert a pointer to "private data" which we can fill with goodies. 9575796c8dcSSimon Schubert 9585796c8dcSSimon Schubert For now at least, we have nothing in particular to do, so this function is 9595796c8dcSSimon Schubert just a stub. */ 9605796c8dcSSimon Schubert 9615796c8dcSSimon Schubert static void 9625796c8dcSSimon Schubert elf_symfile_init (struct objfile *objfile) 9635796c8dcSSimon Schubert { 9645796c8dcSSimon Schubert /* ELF objects may be reordered, so set OBJF_REORDERED. If we 9655796c8dcSSimon Schubert find this causes a significant slowdown in gdb then we could 9665796c8dcSSimon Schubert set it in the debug symbol readers only when necessary. */ 9675796c8dcSSimon Schubert objfile->flags |= OBJF_REORDERED; 9685796c8dcSSimon Schubert } 9695796c8dcSSimon Schubert 9705796c8dcSSimon Schubert /* When handling an ELF file that contains Sun STABS debug info, 9715796c8dcSSimon Schubert some of the debug info is relative to the particular chunk of the 9725796c8dcSSimon Schubert section that was generated in its individual .o file. E.g. 9735796c8dcSSimon Schubert offsets to static variables are relative to the start of the data 9745796c8dcSSimon Schubert segment *for that module before linking*. This information is 9755796c8dcSSimon Schubert painfully squirreled away in the ELF symbol table as local symbols 9765796c8dcSSimon Schubert with wierd names. Go get 'em when needed. */ 9775796c8dcSSimon Schubert 9785796c8dcSSimon Schubert void 9795796c8dcSSimon Schubert elfstab_offset_sections (struct objfile *objfile, struct partial_symtab *pst) 9805796c8dcSSimon Schubert { 9815796c8dcSSimon Schubert char *filename = pst->filename; 9825796c8dcSSimon Schubert struct dbx_symfile_info *dbx = objfile->deprecated_sym_stab_info; 9835796c8dcSSimon Schubert struct stab_section_info *maybe = dbx->stab_section_info; 9845796c8dcSSimon Schubert struct stab_section_info *questionable = 0; 9855796c8dcSSimon Schubert int i; 9865796c8dcSSimon Schubert char *p; 9875796c8dcSSimon Schubert 9885796c8dcSSimon Schubert /* The ELF symbol info doesn't include path names, so strip the path 9895796c8dcSSimon Schubert (if any) from the psymtab filename. */ 9905796c8dcSSimon Schubert while (0 != (p = strchr (filename, '/'))) 9915796c8dcSSimon Schubert filename = p + 1; 9925796c8dcSSimon Schubert 9935796c8dcSSimon Schubert /* FIXME: This linear search could speed up significantly 9945796c8dcSSimon Schubert if it was chained in the right order to match how we search it, 9955796c8dcSSimon Schubert and if we unchained when we found a match. */ 9965796c8dcSSimon Schubert for (; maybe; maybe = maybe->next) 9975796c8dcSSimon Schubert { 9985796c8dcSSimon Schubert if (filename[0] == maybe->filename[0] 9995796c8dcSSimon Schubert && strcmp (filename, maybe->filename) == 0) 10005796c8dcSSimon Schubert { 10015796c8dcSSimon Schubert /* We found a match. But there might be several source files 10025796c8dcSSimon Schubert (from different directories) with the same name. */ 10035796c8dcSSimon Schubert if (0 == maybe->found) 10045796c8dcSSimon Schubert break; 10055796c8dcSSimon Schubert questionable = maybe; /* Might use it later. */ 10065796c8dcSSimon Schubert } 10075796c8dcSSimon Schubert } 10085796c8dcSSimon Schubert 10095796c8dcSSimon Schubert if (maybe == 0 && questionable != 0) 10105796c8dcSSimon Schubert { 10115796c8dcSSimon Schubert complaint (&symfile_complaints, 10125796c8dcSSimon Schubert _("elf/stab section information questionable for %s"), filename); 10135796c8dcSSimon Schubert maybe = questionable; 10145796c8dcSSimon Schubert } 10155796c8dcSSimon Schubert 10165796c8dcSSimon Schubert if (maybe) 10175796c8dcSSimon Schubert { 10185796c8dcSSimon Schubert /* Found it! Allocate a new psymtab struct, and fill it in. */ 10195796c8dcSSimon Schubert maybe->found++; 10205796c8dcSSimon Schubert pst->section_offsets = (struct section_offsets *) 10215796c8dcSSimon Schubert obstack_alloc (&objfile->objfile_obstack, 10225796c8dcSSimon Schubert SIZEOF_N_SECTION_OFFSETS (objfile->num_sections)); 10235796c8dcSSimon Schubert for (i = 0; i < maybe->num_sections; i++) 10245796c8dcSSimon Schubert (pst->section_offsets)->offsets[i] = maybe->sections[i]; 10255796c8dcSSimon Schubert return; 10265796c8dcSSimon Schubert } 10275796c8dcSSimon Schubert 10285796c8dcSSimon Schubert /* We were unable to find any offsets for this file. Complain. */ 10295796c8dcSSimon Schubert if (dbx->stab_section_info) /* If there *is* any info, */ 10305796c8dcSSimon Schubert complaint (&symfile_complaints, 10315796c8dcSSimon Schubert _("elf/stab section information missing for %s"), filename); 10325796c8dcSSimon Schubert } 10335796c8dcSSimon Schubert 10345796c8dcSSimon Schubert /* Register that we are able to handle ELF object file formats. */ 10355796c8dcSSimon Schubert 10365796c8dcSSimon Schubert static struct sym_fns elf_sym_fns = 10375796c8dcSSimon Schubert { 10385796c8dcSSimon Schubert bfd_target_elf_flavour, 10395796c8dcSSimon Schubert elf_new_init, /* sym_new_init: init anything gbl to entire symtab */ 10405796c8dcSSimon Schubert elf_symfile_init, /* sym_init: read initial info, setup for sym_read() */ 10415796c8dcSSimon Schubert elf_symfile_read, /* sym_read: read a symbol file into symtab */ 10425796c8dcSSimon Schubert elf_symfile_finish, /* sym_finish: finished with file, cleanup */ 10435796c8dcSSimon Schubert default_symfile_offsets, /* sym_offsets: Translate ext. to int. relocation */ 10445796c8dcSSimon Schubert elf_symfile_segments, /* sym_segments: Get segment information from 10455796c8dcSSimon Schubert a file. */ 10465796c8dcSSimon Schubert NULL, /* sym_read_linetable */ 1047*cf7f2e2dSJohn Marino default_symfile_relocate, /* sym_relocate: Relocate a debug section. */ 1048*cf7f2e2dSJohn Marino &psym_functions, 10495796c8dcSSimon Schubert NULL /* next: pointer to next struct sym_fns */ 10505796c8dcSSimon Schubert }; 10515796c8dcSSimon Schubert 10525796c8dcSSimon Schubert void 10535796c8dcSSimon Schubert _initialize_elfread (void) 10545796c8dcSSimon Schubert { 10555796c8dcSSimon Schubert add_symtab_fns (&elf_sym_fns); 10565796c8dcSSimon Schubert } 1057