/* COFF specific linker code.
Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006 Free Software Foundation, Inc.
Written by Ian Lance Taylor, Cygnus Support.
This file is part of BFD, the Binary File Descriptor library.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the 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 of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
/* This file contains the COFF backend linker code. */
#include "bfd.h"
#include "sysdep.h"
#include "bfdlink.h"
#include "libbfd.h"
#include "coff/internal.h"
#include "libcoff.h"
#include "safe-ctype.h"
static bfd_boolean coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info);
static bfd_boolean coff_link_check_archive_element (bfd *abfd, struct bfd_link_info *info, bfd_boolean *pneeded);
static bfd_boolean coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info);
/* Return TRUE if SYM is a weak, external symbol. */
#define IS_WEAK_EXTERNAL(abfd, sym) \
((sym).n_sclass == C_WEAKEXT \
|| (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
/* Return TRUE if SYM is an external symbol. */
#define IS_EXTERNAL(abfd, sym) \
((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
/* Define macros so that the ISFCN, et. al., macros work correctly.
These macros are defined in include/coff/internal.h in terms of
N_TMASK, etc. These definitions require a user to define local
variables with the appropriate names, and with values from the
coff_data (abfd) structure. */
#define N_TMASK n_tmask
#define N_BTSHFT n_btshft
#define N_BTMASK n_btmask
/* Create an entry in a COFF linker hash table. */
struct bfd_hash_entry *
_bfd_coff_link_hash_newfunc (struct bfd_hash_entry *entry,
struct bfd_hash_table *table,
const char *string)
{
struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
/* Allocate the structure if it has not already been allocated by a
subclass. */
if (ret == (struct coff_link_hash_entry *) NULL)
ret = ((struct coff_link_hash_entry *)
bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
if (ret == (struct coff_link_hash_entry *) NULL)
return (struct bfd_hash_entry *) ret;
/* Call the allocation method of the superclass. */
ret = ((struct coff_link_hash_entry *)
_bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
table, string));
if (ret != (struct coff_link_hash_entry *) NULL)
{
/* Set local fields. */
ret->indx = -1;
ret->type = T_NULL;
ret->class = C_NULL;
ret->numaux = 0;
ret->auxbfd = NULL;
ret->aux = NULL;
}
return (struct bfd_hash_entry *) ret;
}
/* Initialize a COFF linker hash table. */
bfd_boolean
_bfd_coff_link_hash_table_init (struct coff_link_hash_table *table,
bfd *abfd,
struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
struct bfd_hash_table *,
const char *),
unsigned int entsize)
{
memset (&table->stab_info, 0, sizeof (table->stab_info));
return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
}
/* Create a COFF linker hash table. */
struct bfd_link_hash_table *
_bfd_coff_link_hash_table_create (bfd *abfd)
{
struct coff_link_hash_table *ret;
bfd_size_type amt = sizeof (struct coff_link_hash_table);
ret = bfd_malloc (amt);
if (ret == NULL)
return NULL;
if (! _bfd_coff_link_hash_table_init (ret, abfd,
_bfd_coff_link_hash_newfunc,
sizeof (struct coff_link_hash_entry)))
{
free (ret);
return (struct bfd_link_hash_table *) NULL;
}
return &ret->root;
}
/* Create an entry in a COFF debug merge hash table. */
struct bfd_hash_entry *
_bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry *entry,
struct bfd_hash_table *table,
const char *string)
{
struct coff_debug_merge_hash_entry *ret =
(struct coff_debug_merge_hash_entry *) entry;
/* Allocate the structure if it has not already been allocated by a
subclass. */
if (ret == (struct coff_debug_merge_hash_entry *) NULL)
ret = ((struct coff_debug_merge_hash_entry *)
bfd_hash_allocate (table,
sizeof (struct coff_debug_merge_hash_entry)));
if (ret == (struct coff_debug_merge_hash_entry *) NULL)
return (struct bfd_hash_entry *) ret;
/* Call the allocation method of the superclass. */
ret = ((struct coff_debug_merge_hash_entry *)
bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
if (ret != (struct coff_debug_merge_hash_entry *) NULL)
{
/* Set local fields. */
ret->types = NULL;
}
return (struct bfd_hash_entry *) ret;
}
/* Given a COFF BFD, add symbols to the global hash table as
appropriate. */
bfd_boolean
_bfd_coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
{
switch (bfd_get_format (abfd))
{
case bfd_object:
return coff_link_add_object_symbols (abfd, info);
case bfd_archive:
return _bfd_generic_link_add_archive_symbols
(abfd, info, coff_link_check_archive_element);
default:
bfd_set_error (bfd_error_wrong_format);
return FALSE;
}
}
/* Add symbols from a COFF object file. */
static bfd_boolean
coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
{
if (! _bfd_coff_get_external_symbols (abfd))
return FALSE;
if (! coff_link_add_symbols (abfd, info))
return FALSE;
if (! info->keep_memory
&& ! _bfd_coff_free_symbols (abfd))
return FALSE;
return TRUE;
}
/* Look through the symbols to see if this object file should be
included in the link. */
static bfd_boolean
coff_link_check_ar_symbols (bfd *abfd,
struct bfd_link_info *info,
bfd_boolean *pneeded)
{
bfd_size_type symesz;
bfd_byte *esym;
bfd_byte *esym_end;
*pneeded = FALSE;
symesz = bfd_coff_symesz (abfd);
esym = (bfd_byte *) obj_coff_external_syms (abfd);
esym_end = esym + obj_raw_syment_count (abfd) * symesz;
while (esym < esym_end)
{
struct internal_syment sym;
enum coff_symbol_classification classification;
bfd_coff_swap_sym_in (abfd, esym, &sym);
classification = bfd_coff_classify_symbol (abfd, &sym);
if (classification == COFF_SYMBOL_GLOBAL
|| classification == COFF_SYMBOL_COMMON)
{
const char *name;
char buf[SYMNMLEN + 1];
struct bfd_link_hash_entry *h;
/* This symbol is externally visible, and is defined by this
object file. */
name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
if (name == NULL)
return FALSE;
h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
/* Auto import. */
if (!h
&& info->pei386_auto_import
&& !strncmp (name,"__imp_", 6))
h = bfd_link_hash_lookup (info->hash, name + 6, FALSE, FALSE, TRUE);
/* We are only interested in symbols that are currently
undefined. If a symbol is currently known to be common,
COFF linkers do not bring in an object file which defines
it. */
if (h != (struct bfd_link_hash_entry *) NULL
&& h->type == bfd_link_hash_undefined)
{
if (! (*info->callbacks->add_archive_element) (info, abfd, name))
return FALSE;
*pneeded = TRUE;
return TRUE;
}
}
esym += (sym.n_numaux + 1) * symesz;
}
/* We do not need this object file. */
return TRUE;
}
/* Check a single archive element to see if we need to include it in
the link. *PNEEDED is set according to whether this element is
needed in the link or not. This is called via
_bfd_generic_link_add_archive_symbols. */
static bfd_boolean
coff_link_check_archive_element (bfd *abfd,
struct bfd_link_info *info,
bfd_boolean *pneeded)
{
if (! _bfd_coff_get_external_symbols (abfd))
return FALSE;
if (! coff_link_check_ar_symbols (abfd, info, pneeded))
return FALSE;
if (*pneeded
&& ! coff_link_add_symbols (abfd, info))
return FALSE;
if ((! info->keep_memory || ! *pneeded)
&& ! _bfd_coff_free_symbols (abfd))
return FALSE;
return TRUE;
}
/* Add all the symbols from an object file to the hash table. */
static bfd_boolean
coff_link_add_symbols (bfd *abfd,
struct bfd_link_info *info)
{
unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
bfd_boolean keep_syms;
bfd_boolean default_copy;
bfd_size_type symcount;
struct coff_link_hash_entry **sym_hash;
bfd_size_type symesz;
bfd_byte *esym;
bfd_byte *esym_end;
bfd_size_type amt;
/* Keep the symbols during this function, in case the linker needs
to read the generic symbols in order to report an error message. */
keep_syms = obj_coff_keep_syms (abfd);
|