archive.c

来自「基于4个mips核的noc设计」· C语言 代码 · 共 2,234 行 · 第 1/5 页

C
2,234
字号
/* BFD back-end for archive files (libraries).   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,   2000   Free Software Foundation, Inc.   Written by Cygnus Support.  Mostly Gumby Henkel-Wallace's fault.This file is part of BFD, the Binary File Descriptor library.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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  *//*@setfilename archive-infoSECTION	ArchivesDESCRIPTION	An archive (or library) is just another BFD.  It has a symbol	table, although there's not much a user program will do with it.	The big difference between an archive BFD and an ordinary BFD	is that the archive doesn't have sections.  Instead it has a	chain of BFDs that are considered its contents.  These BFDs can	be manipulated like any other.  The BFDs contained in an	archive opened for reading will all be opened for reading.  You	may put either input or output BFDs into an archive opened for	output; they will be handled correctly when the archive is closed.	Use <<bfd_openr_next_archived_file>> to step through	the contents of an archive opened for input.  You don't	have to read the entire archive if you don't want	to!  Read it until you find what you want.	Archive contents of output BFDs are chained through the	<<next>> pointer in a BFD.  The first one is findable through	the <<archive_head>> slot of the archive.  Set it with	<<bfd_set_archive_head>> (q.v.).  A given BFD may be in only one	open output archive at a time.	As expected, the BFD archive code is more general than the	archive code of any given environment.  BFD archives may	contain files of different formats (e.g., a.out and coff) and	even different architectures.  You may even place archives	recursively into archives!	This can cause unexpected confusion, since some archive	formats are more expressive than others.  For instance, Intel	COFF archives can preserve long filenames; SunOS a.out archives	cannot.  If you move a file from the first to the second	format and back again, the filename may be truncated.	Likewise, different a.out environments have different	conventions as to how they truncate filenames, whether they	preserve directory names in filenames, etc.  When	interoperating with native tools, be sure your files are	homogeneous.	Beware: most of these formats do not react well to the	presence of spaces in filenames.  We do the best we can, but	can't always handle this case due to restrictions in the format of	archives.  Many Unix utilities are braindead in regards to	spaces and such in filenames anyway, so this shouldn't be much	of a restriction.	Archives are supported in BFD in <<archive.c>>.*//* Assumes:   o - all archive elements start on an even boundary, newline padded;   o - all arch headers are char *;   o - all arch headers are the same size (across architectures).*//* Some formats provide a way to cram a long filename into the short   (16 chars) space provided by a BSD archive.  The trick is: make a   special "file" in the front of the archive, sort of like the SYMDEF   entry.  If the filename is too long to fit, put it in the extended   name table, and use its index as the filename.  To prevent   confusion prepend the index with a space.  This means you can't   have filenames that start with a space, but then again, many Unix   utilities can't handle that anyway.   This scheme unfortunately requires that you stand on your head in   order to write an archive since you need to put a magic file at the   front, and need to touch every entry to do so.  C'est la vie.   We support two variants of this idea:   The SVR4 format (extended name table is named "//"),   and an extended pseudo-BSD variant (extended name table is named   "ARFILENAMES/").  The origin of the latter format is uncertain.   BSD 4.4 uses a third scheme:  It writes a long filename   directly after the header.  This allows 'ar q' to work.   We currently can read BSD 4.4 archives, but not write them.*//* Summary of archive member names: Symbol table (must be first): "__.SYMDEF       " - Symbol table, Berkeley style, produced by ranlib. "/               " - Symbol table, system 5 style. Long name table (must be before regular file members): "//              " - Long name table, System 5 R4 style. "ARFILENAMES/    " - Long name table, non-standard extended BSD (not BSD 4.4). Regular file members with short names: "filename.o/     " - Regular file, System 5 style (embedded spaces ok). "filename.o      " - Regular file, Berkeley style (no embedded spaces). Regular files with long names (or embedded spaces, for BSD variants): "/18             " - SVR4 style, name at offset 18 in name table. "#1/23           " - Long name (or embedded paces) 23 characters long,		      BSD 4.4 style, full name follows header.		      Implemented for reading, not writing. " 18             " - Long name 18 characters long, extended pseudo-BSD. */#include "bfd.h"#include "sysdep.h"#include "libbfd.h"#include "aout/ar.h"#include "aout/ranlib.h"#include <ctype.h>#ifndef errnoextern int errno;#endif#ifdef GNU960#define BFD_GNU960_ARMAG(abfd)	(BFD_COFF_FILE_P((abfd)) ? ARMAG : ARMAGB)#endif/* Define offsetof for those systems which lack it */#ifndef offsetof#define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)#endif/* We keep a cache of archive filepointers to archive elements to   speed up searching the archive by filepos.  We only add an entry to   the cache when we actually read one.  We also don't sort the cache;   it's generally short enough to search linearly.   Note that the pointers here point to the front of the ar_hdr, not   to the front of the contents!  */struct ar_cache {  file_ptr ptr;  bfd *arelt;  struct ar_cache *next;};#define ar_padchar(abfd) ((abfd)->xvec->ar_pad_char)#define ar_maxnamelen(abfd) ((abfd)->xvec->ar_max_namelen)#define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))#define arch_hdr(bfd) ((struct ar_hdr *)arch_eltdata(bfd)->arch_header)static char *get_extended_arelt_filename PARAMS ((bfd *arch,						  const char *name));static boolean do_slurp_bsd_armap PARAMS ((bfd *abfd));static boolean do_slurp_coff_armap PARAMS ((bfd *abfd));static const char *normalize PARAMS ((bfd *, const char *file));static struct areltdata *bfd_ar_hdr_from_filesystem PARAMS ((bfd *abfd,							     const char *,							     bfd *member));boolean_bfd_generic_mkarchive (abfd)     bfd *abfd;{  abfd->tdata.aout_ar_data = ((struct artdata *)			      bfd_zalloc (abfd, sizeof (struct artdata)));  if (bfd_ardata (abfd) == NULL)    return false;  bfd_ardata (abfd)->cache = NULL;  bfd_ardata (abfd)->archive_head = NULL;  bfd_ardata (abfd)->symdefs = NULL;  bfd_ardata (abfd)->extended_names = NULL;  bfd_ardata (abfd)->tdata = NULL;  return true;}/*FUNCTION	bfd_get_next_mapentSYNOPSIS	symindex bfd_get_next_mapent(bfd *abfd, symindex previous, carsym **sym);DESCRIPTION	Step through archive @var{abfd}'s symbol table (if it	has one).  Successively update @var{sym} with the next symbol's	information, returning that symbol's (internal) index into the	symbol table.	Supply <<BFD_NO_MORE_SYMBOLS>> as the @var{previous} entry to get	the first one; returns <<BFD_NO_MORE_SYMBOLS>> when you've already	got the last one.	A <<carsym>> is a canonical archive symbol.  The only	user-visible element is its name, a null-terminated string.*/symindexbfd_get_next_mapent (abfd, prev, entry)     bfd *abfd;     symindex prev;     carsym **entry;{  if (!bfd_has_map (abfd))    {      bfd_set_error (bfd_error_invalid_operation);      return BFD_NO_MORE_SYMBOLS;    }  if (prev == BFD_NO_MORE_SYMBOLS)    prev = 0;  else    ++prev;  if (prev >= bfd_ardata (abfd)->symdef_count)    return BFD_NO_MORE_SYMBOLS;  *entry = (bfd_ardata (abfd)->symdefs + prev);  return prev;}/* To be called by backends only */bfd *_bfd_create_empty_archive_element_shell (obfd)     bfd *obfd;{  return _bfd_new_bfd_contained_in (obfd);}/*FUNCTION	bfd_set_archive_headSYNOPSIS	boolean bfd_set_archive_head(bfd *output, bfd *new_head);DESCRIPTION	Set the head of the chain of	BFDs contained in the archive @var{output} to @var{new_head}.*/booleanbfd_set_archive_head (output_archive, new_head)     bfd *output_archive;     bfd *new_head;{  output_archive->archive_head = new_head;  return true;}bfd *_bfd_look_for_bfd_in_cache (arch_bfd, filepos)     bfd *arch_bfd;     file_ptr filepos;{  struct ar_cache *current;  for (current = bfd_ardata (arch_bfd)->cache; current != NULL;       current = current->next)    if (current->ptr == filepos)      return current->arelt;  return NULL;}/* Kind of stupid to call cons for each one, but we don't do too many */boolean_bfd_add_bfd_to_archive_cache (arch_bfd, filepos, new_elt)     bfd *arch_bfd, *new_elt;     file_ptr filepos;{  struct ar_cache *new_cache = ((struct ar_cache *)				bfd_zalloc (arch_bfd,					    sizeof (struct ar_cache)));  if (new_cache == NULL)    return false;  new_cache->ptr = filepos;  new_cache->arelt = new_elt;  new_cache->next = (struct ar_cache *) NULL;  if (bfd_ardata (arch_bfd)->cache == NULL)    bfd_ardata (arch_bfd)->cache = new_cache;  else    {      struct ar_cache *current = bfd_ardata (arch_bfd)->cache;      while (current->next != NULL)	current = current->next;      current->next = new_cache;    }  return true;}/* The name begins with space.  Hence the rest of the name is an index into   the string table.  */static char *get_extended_arelt_filename (arch, name)     bfd *arch;     const char *name;{  unsigned long index = 0;  /* Should extract string so that I can guarantee not to overflow into     the next region, but I'm too lazy.  */  errno = 0;  /* Skip first char, which is '/' in SVR4 or ' ' in some other variants.  */  index = strtol (name + 1, NULL, 10);  if (errno != 0)    {      bfd_set_error (bfd_error_malformed_archive);      return NULL;    }  return bfd_ardata (arch)->extended_names + index;}/* This functions reads an arch header and returns an areltdata pointer, or   NULL on error.   Presumes the file pointer is already in the right place (ie pointing   to the ar_hdr in the file).   Moves the file pointer; on success it   should be pointing to the front of the file contents; on failure it   could have been moved arbitrarily.*/PTR_bfd_generic_read_ar_hdr (abfd)     bfd *abfd;{  return _bfd_generic_read_ar_hdr_mag (abfd, (const char *) NULL);}/* Alpha ECOFF uses an optional different ARFMAG value, so we have a   variant of _bfd_generic_read_ar_hdr which accepts a magic string.  */PTR_bfd_generic_read_ar_hdr_mag (abfd, mag)     bfd *abfd;     const char *mag;{  struct ar_hdr hdr;  char *hdrp = (char *) &hdr;  unsigned int parsed_size;  struct areltdata *ared;  char *filename = NULL;  unsigned int namelen = 0;  unsigned int allocsize = sizeof (struct areltdata) + sizeof (struct ar_hdr);  char *allocptr = 0;  if (bfd_read ((PTR) hdrp, 1, sizeof (struct ar_hdr), abfd)      != sizeof (struct ar_hdr))    {      if (bfd_get_error () != bfd_error_system_call)	bfd_set_error (bfd_error_no_more_archived_files);      return NULL;    }  if (strncmp (hdr.ar_fmag, ARFMAG, 2) != 0      && (mag == NULL	  || strncmp (hdr.ar_fmag, mag, 2) != 0))    {      bfd_set_error (bfd_error_malformed_archive);      return NULL;    }  errno = 0;  parsed_size = strtol (hdr.ar_size, NULL, 10);  if (errno != 0)    {      bfd_set_error (bfd_error_malformed_archive);      return NULL;    }  /* Extract the filename from the archive - there are two ways to     specify an extended name table, either the first char of the     name is a space, or it's a slash.  */  if ((hdr.ar_name[0] == '/'       || (hdr.ar_name[0] == ' '	   && memchr (hdr.ar_name, '/', ar_maxnamelen (abfd)) == NULL))      && bfd_ardata (abfd)->extended_names != NULL)    {      filename = get_extended_arelt_filename (abfd, hdr.ar_name);      if (filename == NULL)	{	  bfd_set_error (bfd_error_malformed_archive);	  return NULL;	}    }  /* BSD4.4-style long filename.     Only implemented for reading, so far!  */  else if (hdr.ar_name[0] == '#'	   && hdr.ar_name[1] == '1'	   && hdr.ar_name[2] == '/'	   && isdigit ((unsigned char) hdr.ar_name[3]))    {      /* BSD-4.4 extended name */      namelen = atoi (&hdr.ar_name[3]);      allocsize += namelen + 1;      parsed_size -= namelen;      allocptr = bfd_zalloc (abfd, allocsize);      if (allocptr == NULL)	return NULL;      filename = (allocptr		  + sizeof (struct areltdata)		  + sizeof (struct ar_hdr));      if (bfd_read (filename, 1, namelen, abfd) != namelen)	{	  if (bfd_get_error () != bfd_error_system_call)	    bfd_set_error (bfd_error_no_more_archived_files);	  return NULL;	}      filename[namelen] = '\0';    }  else    {      /* We judge the end of the name by looking for '/' or ' '.	 Note:  The SYSV format (terminated by '/') allows embedded	 spaces, so only look for ' ' if we don't find '/'.  */      char *e;      e = memchr (hdr.ar_name, '\0', ar_maxnamelen (abfd));      if (e == NULL)	{

⌨️ 快捷键说明

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