⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 elfread.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/* Read ELF (Executable and Linking Format) object files for GDB.   Copyright 1991, 1992 Free Software Foundation, Inc.   Written by Fred Fish at Cygnus Support.This file is part of GDB.This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  *//************************************************************************ *									* *				NOTICE					* *									* * This file is still under construction.  When it is complete, this	* * notice will be removed.  Until then, direct any questions or changes	* * to Fred Fish at Cygnus Support (fnf@cygnus.com)			* *									*  * FIXME	Still needs support for shared libraries.		* * FIXME	Still needs support for core files.			* * FIXME	The ".debug" and ".line" section names are hardwired.	* *									* ************************************************************************/#include "defs.h"#include "elf/common.h"#include "elf/external.h"#include "elf/internal.h"#include "bfd.h"#include "libbfd.h"		/* For bfd_elf_find_section */#include "symtab.h"#include "symfile.h"#include "objfiles.h"#include "buildsym.h"#include "gdb-stabs.h"#define STREQ(a,b) (strcmp((a),(b))==0)/* The struct elfinfo is available only during ELF symbol table and   psymtab reading.  It is destroyed at the complation of psymtab-reading.   It's local to elf_symfile_read.  */struct elfinfo {  file_ptr dboffset;		/* Offset to dwarf debug section */  unsigned int dbsize;		/* Size of dwarf debug section */  file_ptr lnoffset;		/* Offset to dwarf line number section */  unsigned int lnsize;		/* Size of dwarf line number section */  asection *stabsect;		/* Section pointer for .stab section */  asection *stabindexsect;	/* Section pointer for .stab.index section */};/* Various things we might complain about... */struct complaint section_info_complaint =   {"elf/stab section information %s without a preceding file symbol", 0, 0};struct complaint section_info_dup_complaint =   {"duplicated elf/stab section information for %s", 0, 0};struct complaint stab_info_mismatch_complaint =   {"elf/stab section information missing for %s", 0, 0};struct complaint stab_info_questionable_complaint =   {"elf/stab section information questionable for %s", 0, 0};static voidelf_symfile_init PARAMS ((struct objfile *));static voidelf_new_init PARAMS ((struct objfile *));static voidelf_symfile_read PARAMS ((struct objfile *, struct section_offsets *, int));static voidelf_symfile_finish PARAMS ((struct objfile *));static voidelf_symtab_read PARAMS ((bfd *,  CORE_ADDR, struct objfile *));static voidfree_elfinfo PARAMS ((PTR));static struct section_offsets *elf_symfile_offsets PARAMS ((struct objfile *, CORE_ADDR));#if 0static voidrecord_minimal_symbol PARAMS ((char *, CORE_ADDR, enum minimal_symbol_type,			       struct objfile *));#endifstatic voidrecord_minimal_symbol_and_info PARAMS ((char *, CORE_ADDR,					enum minimal_symbol_type, char *,					struct objfile *));static voidelf_locate_sections PARAMS ((bfd *, asection *, PTR));/* We are called once per section from elf_symfile_read.  We   need to examine each section we are passed, check to see   if it is something we are interested in processing, and   if so, stash away some access information for the section.   For now we recognize the dwarf debug information sections and   line number sections from matching their section names.  The   ELF definition is no real help here since it has no direct   knowledge of DWARF (by design, so any debugging format can be   used).   We also recognize the ".stab" sections used by the Sun compilers   released with Solaris 2.   FIXME:  The section names should not be hardwired strings. */static voidelf_locate_sections (ignore_abfd, sectp, eip)     bfd *ignore_abfd;     asection *sectp;     PTR eip;{  register struct elfinfo *ei;  ei = (struct elfinfo *) eip;  if (STREQ (sectp -> name, ".debug"))    {      ei -> dboffset = sectp -> filepos;      ei -> dbsize = bfd_get_section_size_before_reloc (sectp);    }  else if (STREQ (sectp -> name, ".line"))    {      ei -> lnoffset = sectp -> filepos;      ei -> lnsize = bfd_get_section_size_before_reloc (sectp);    }  else if (STREQ (sectp -> name, ".stab"))    {      ei -> stabsect = sectp;    }  else if (STREQ (sectp -> name, ".stab.index"))    {      ei -> stabindexsect = sectp;    }}#if 0	/* Currently unused */char *elf_interpreter (abfd)     bfd *abfd;{  sec_ptr interp_sec;  unsigned size;  char *interp = NULL;  interp_sec = bfd_get_section_by_name (abfd, ".interp");  if (interp_sec)    {      size = bfd_section_size (abfd, interp_sec);      interp = alloca (size);      if (bfd_get_section_contents (abfd, interp_sec, interp, (file_ptr)0,				    size))	{	  interp = savestring (interp, size - 1);	}      else	{	  interp = NULL;	}    }  return (interp);}#endif/*LOCAL FUNCTION	record_minimal_symbol -- add entry to minimal symbol tableSYNOPSIS	static void record_minimal_symbol (char *name, CORE_ADDR address)DESCRIPTION	Given a pointer to the name of a symbol that should be added to the	minimal symbol table and the address associated with that symbol, records	this information for later use in building the minimal symbol table. */#if 0	/* FIXME:  Unused */static voidrecord_minimal_symbol (name, address, ms_type, objfile)     char *name;     CORE_ADDR address;     enum minimal_symbol_type ms_type;     struct objfile *objfile;{  name = obsavestring (name, strlen (name), &objfile -> symbol_obstack);  prim_record_minimal_symbol (name, address, ms_type);}#endifstatic voidrecord_minimal_symbol_and_info (name, address, ms_type, info, objfile)     char *name;     CORE_ADDR address;     enum minimal_symbol_type ms_type;     char *info;		/* FIXME, is this really char *? */     struct objfile *objfile;{  name = obsavestring (name, strlen (name), &objfile -> symbol_obstack);  prim_record_minimal_symbol_and_info (name, address, ms_type, info);}/*LOCAL FUNCTION	elf_symtab_read -- read the symbol table of an ELF fileSYNOPSIS	void elf_symtab_read (bfd *abfd, CORE_ADDR addr,			      struct objfile *objfile)DESCRIPTION	Given an open bfd, a base address to relocate symbols to, and a	flag that specifies whether or not this bfd is for an executable	or not (may be shared library for example), add all the global	function and data symbols to the minimal symbol table.	In stabs-in-ELF, as implemented by Sun, there are some local symbols	defined in the ELF symbol table, which can be used to locate	the beginnings of sections from each ".o" file that was linked to	form the executable objfile.  We gather any such info and record it	in data structures hung off the objfile's private data.*/static voidelf_symtab_read (abfd, addr, objfile)     bfd *abfd;     CORE_ADDR addr;     struct objfile *objfile;{  unsigned int storage_needed;  asymbol *sym;  asymbol **symbol_table;  unsigned int number_of_symbols;  unsigned int i;  int index;  struct cleanup *back_to;  CORE_ADDR symaddr;  enum minimal_symbol_type ms_type;  /* If sectinfo is nonzero, it contains section info that should end up     filed in the objfile.  */  struct stab_section_info *sectinfo = 0;  /* If filesym is nonzero, it points to a file symbol, but we haven't     seen any section info for it yet.  */  asymbol *filesym = 0;  struct dbx_symfile_info *dbx = (struct dbx_symfile_info *)				 objfile->sym_private;    storage_needed = get_symtab_upper_bound (abfd);  if (storage_needed > 0)    {      symbol_table = (asymbol **) xmalloc (storage_needed);      back_to = make_cleanup (free, symbol_table);      number_of_symbols = bfd_canonicalize_symtab (abfd, symbol_table);         for (i = 0; i < number_of_symbols; i++)	{	  sym = symbol_table[i];	  /* Select global/weak symbols that are defined in a specific section.	     Note that bfd now puts abs symbols in their own section, so	     all symbols we are interested in will have a section. */	  if ((sym -> flags & (BSF_GLOBAL | BSF_WEAK))	      && (sym -> section != NULL))	    {	      symaddr = sym -> value;	      /* Relocate all non-absolute symbols by base address.  */	      if (sym -> section != &bfd_abs_section)		symaddr += addr;	      /* For non-absolute symbols, use the type of the section		 they are relative to, to intuit text/data.  Bfd provides		 no way of figuring this out for absolute symbols. */	      if (sym -> section -> flags & SEC_CODE)		{		  ms_type = mst_text;		}	      else if (sym -> section -> flags & SEC_DATA)		{		  ms_type = mst_data;		}	      else		{		  /* FIXME:  Solaris2 shared libraries include lots of		     odd "absolute" and "undefined" symbols, that play 		     hob with actions like finding what function the PC		     is in.  Ignore them if they aren't text or data.  */		  /* ms_type = mst_unknown; */		  continue;		/* Skip this symbol. */		}	      /* Pass symbol size field in via BFD.  FIXME!!!  */	      record_minimal_symbol_and_info ((char *) sym -> name,			 symaddr, ms_type, sym->udata, objfile);	    }	  /* See if this is a debugging symbol that helps Solaris	     stabs-in-elf debugging.  */	  else if (sym->flags & BSF_FILE)	    {	      /* Chain any old one onto the objfile; remember new sym.  */	      if (sectinfo)		{		    sectinfo->next = dbx->stab_section_info;		    dbx->stab_section_info = sectinfo;		    sectinfo = 0;		}	      filesym = sym;	    }	  else if ((sym->flags & BSF_LOCAL) &&		   (sym->section) &&		   (sym->section->flags & SEC_DATA) &&		   (sym->name))	    {	      /* Named Local variable in a Data section.  Check its name.  */	      index = -1;	      switch (sym->name[1])		{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -