/* COFF specific linker code.
- Copyright 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
+ Copyright 1994, 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
Written by Ian Lance Taylor, Cygnus Support.
This file is part of BFD, the Binary File Descriptor library.
static char *get_name PARAMS ((char *, char **));
static int process_embedded_commands
PARAMS ((bfd *, struct bfd_link_info *, bfd *));
+static void mark_relocs PARAMS ((struct coff_final_link_info *, bfd *));
/* Create an entry in a COFF linker hash table. */
struct bfd_link_info *info;
boolean *pneeded;
{
- boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
bfd_size_type symesz;
bfd_byte *esym;
bfd_byte *esym_end;
*pneeded = false;
- sym_is_global = coff_backend_info (abfd)->_bfd_coff_sym_is_global;
-
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, (PTR) esym, (PTR) &sym);
- if ((sym.n_sclass == C_EXT
- || (sym_is_global && (*sym_is_global) (abfd, &sym)))
- && (sym.n_scnum != 0 || sym.n_value != 0))
+ classification = bfd_coff_classify_symbol (abfd, &sym);
+ if (classification == COFF_SYMBOL_GLOBAL
+ || classification == COFF_SYMBOL_COMMON)
{
const char *name;
char buf[SYMNMLEN + 1];
bfd *abfd;
struct bfd_link_info *info;
{
- boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
boolean keep_syms;
boolean default_copy;
bfd_size_type symcount;
keep_syms = obj_coff_keep_syms (abfd);
obj_coff_keep_syms (abfd) = true;
- sym_is_global = coff_backend_info (abfd)->_bfd_coff_sym_is_global;
-
if (info->keep_memory)
default_copy = false;
else
while (esym < esym_end)
{
struct internal_syment sym;
+ enum coff_symbol_classification classification;
boolean copy;
bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &sym);
- if (sym.n_sclass == C_EXT
- || (sym_is_global && (*sym_is_global) (abfd, &sym)))
+ classification = bfd_coff_classify_symbol (abfd, &sym);
+ if (classification != COFF_SYMBOL_LOCAL)
{
const char *name;
char buf[SYMNMLEN + 1];
flagword flags;
asection *section;
bfd_vma value;
+ boolean addit;
/* This symbol is externally visible. */
value = sym.n_value;
- if (sym.n_scnum == 0)
+ switch (classification)
{
- if (value == 0)
- {
- flags = 0;
- section = bfd_und_section_ptr;
- }
- else
+ default:
+ abort ();
+
+ case COFF_SYMBOL_GLOBAL:
+ flags = BSF_EXPORT | BSF_GLOBAL;
+ section = coff_section_from_bfd_index (abfd, sym.n_scnum);
+ if (! obj_pe (abfd))
+ value -= section->vma;
+ break;
+
+ case COFF_SYMBOL_UNDEFINED:
+ flags = 0;
+ section = bfd_und_section_ptr;
+ break;
+
+ case COFF_SYMBOL_COMMON:
+ flags = BSF_GLOBAL;
+ section = bfd_com_section_ptr;
+ break;
+
+ case COFF_SYMBOL_PE_SECTION:
+ flags = BSF_SECTION_SYM | BSF_GLOBAL;
+ section = coff_section_from_bfd_index (abfd, sym.n_scnum);
+ break;
+ }
+
+ if (sym.n_sclass == C_WEAKEXT
+ || (obj_pe (abfd) && sym.n_sclass == C_NT_WEAK))
+ flags = BSF_WEAK;
+
+ addit = true;
+
+ /* In the PE format, section symbols actually refer to the
+ start of the output section. We handle them specially
+ here. */
+ if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
+ {
+ *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
+ name, false, copy, false);
+ if (*sym_hash != NULL)
{
- flags = BSF_GLOBAL;
- section = bfd_com_section_ptr;
+ if (((*sym_hash)->coff_link_hash_flags
+ & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
+ && (*sym_hash)->root.type != bfd_link_hash_undefined
+ && (*sym_hash)->root.type != bfd_link_hash_undefweak)
+ (*_bfd_error_handler)
+ ("Warning: symbol `%s' is both section and non-section",
+ name);
+
+ addit = false;
}
}
- else
+
+ if (addit)
{
- flags = BSF_EXPORT | BSF_GLOBAL;
- section = coff_section_from_bfd_index (abfd, sym.n_scnum);
- value -= section->vma;
+ if (! (bfd_coff_link_add_one_symbol
+ (info, abfd, name, flags, section, value,
+ (const char *) NULL, copy, false,
+ (struct bfd_link_hash_entry **) sym_hash)))
+ goto error_return;
}
- if (! (bfd_coff_link_add_one_symbol
- (info, abfd, name, flags, section, value,
- (const char *) NULL, copy, false,
- (struct bfd_link_hash_entry **) sym_hash)))
- goto error_return;
+ if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
+ (*sym_hash)->coff_link_hash_flags |=
+ COFF_LINK_HASH_PE_SECTION_SYMBOL;
if (section == bfd_com_section_ptr
&& (*sym_hash)->root.type == bfd_link_hash_common
&& (*sym_hash)->type == T_NULL)
|| sym.n_scnum != 0
|| (sym.n_value != 0
- && (*sym_hash)->root.type != bfd_link_hash_defined))
+ && (*sym_hash)->root.type != bfd_link_hash_defined
+ && (*sym_hash)->root.type != bfd_link_hash_defweak))
{
(*sym_hash)->class = sym.n_sclass;
if (sym.n_type != T_NULL)
if ((*sym_hash)->type != T_NULL
&& (*sym_hash)->type != sym.n_type)
(*_bfd_error_handler)
- ("Warning: type of symbol `%s' changed from %d to %d in %s",
+ (_("Warning: type of symbol `%s' changed from %d to %d in %s"),
name, (*sym_hash)->type, sym.n_type,
bfd_get_filename (abfd));
(*sym_hash)->type = sym.n_type;
}
}
}
+
+ if (classification == COFF_SYMBOL_PE_SECTION
+ && (*sym_hash)->numaux != 0)
+ {
+ /* Some PE sections (such as .bss) have a zero size in
+ the section header, but a non-zero size in the AUX
+ record. Correct that here.
+
+ FIXME: This is not at all the right place to do this.
+ For example, it won't help objdump. This needs to be
+ done when we swap in the section header. */
+
+ BFD_ASSERT ((*sym_hash)->numaux == 1);
+ if (section->_raw_size == 0)
+ section->_raw_size = (*sym_hash)->aux[0].x_scn.x_scnlen;
+
+ /* FIXME: We could test whether the section sizes
+ matches the size in the aux entry, but apparently
+ that sometimes fails unexpectedly. */
+ }
}
esym += (sym.n_numaux + 1) * symesz;
finfo.contents = NULL;
finfo.external_relocs = NULL;
finfo.internal_relocs = NULL;
+ finfo.global_to_static = false;
debug_merge_allocated = false;
coff_data (abfd)->link_info = info;
== bfd_target_coff_flavour))
{
sub = p->u.indirect.section->owner;
- if (! sub->output_has_begun)
+ if (! bfd_coff_link_output_has_begun (sub, & finfo))
{
if (! _bfd_coff_link_input_bfd (&finfo, sub))
goto error_return;
}
}
+ if (! bfd_coff_final_link_postscript (abfd, & finfo))
+ goto error_return;
+
/* Free up the buffers used by _bfd_coff_link_input_bfd. */
coff_debug_merge_hash_table_free (&finfo.debug_merge);
static int
process_embedded_commands (output_bfd, info, abfd)
bfd *output_bfd;
- struct bfd_link_info *info;
+ struct bfd_link_info *info ATTRIBUTE_UNUSED;
bfd *abfd;
{
asection *sec = bfd_get_section_by_name (abfd, ".drectve");
struct coff_final_link_info *finfo;
bfd *input_bfd;
{
- boolean (*sym_is_global) PARAMS ((bfd *, struct internal_syment *));
boolean (*adjust_symndx) PARAMS ((bfd *, struct bfd_link_info *, bfd *,
asection *, struct internal_reloc *,
boolean *));
/* Move all the symbols to the output file. */
output_bfd = finfo->output_bfd;
- sym_is_global = coff_backend_info (input_bfd)->_bfd_coff_sym_is_global;
strings = NULL;
syment_base = obj_raw_syment_count (output_bfd);
isymesz = bfd_coff_symesz (input_bfd);
while (esym < esym_end)
{
struct internal_syment isym;
+ enum coff_symbol_classification classification;
boolean skip;
boolean global;
boolean dont_skip_symbol;
the symbol. */
isym = *isymp;
- if (isym.n_scnum != 0)
- *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
- else
+ classification = bfd_coff_classify_symbol (input_bfd, &isym);
+ switch (classification)
{
- if (isym.n_value == 0)
- *secpp = bfd_und_section_ptr;
- else
- *secpp = bfd_com_section_ptr;
+ default:
+ abort ();
+ case COFF_SYMBOL_GLOBAL:
+ case COFF_SYMBOL_PE_SECTION:
+ case COFF_SYMBOL_LOCAL:
+ *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
+ break;
+ case COFF_SYMBOL_COMMON:
+ *secpp = bfd_com_section_ptr;
+ break;
+ case COFF_SYMBOL_UNDEFINED:
+ *secpp = bfd_und_section_ptr;
+ break;
}
- /* Extract the flag indicating if this symbol is used by a relocation */
- if (( finfo->info->strip != strip_none
+ /* Extract the flag indicating if this symbol is used by a
+ relocation. */
+ if ((finfo->info->strip != strip_none
|| finfo->info->discard != discard_none)
&& finfo->info->relocateable)
dont_skip_symbol = *indexp;
if (! skip)
{
- if (isym.n_sclass == C_EXT
- || (sym_is_global && (*sym_is_global) (input_bfd, &isym)))
+ switch (classification)
{
+ default:
+ abort ();
+ case COFF_SYMBOL_GLOBAL:
+ case COFF_SYMBOL_COMMON:
+ case COFF_SYMBOL_PE_SECTION:
/* This is a global symbol. Global symbols come at the
end of the symbol table, so skip them for now.
- Function symbols, however, are an exception, and are
- not moved to the end. */
+ Locally defined function symbols, however, are an
+ exception, and are not moved to the end. */
global = true;
if (! ISFCN (isym.n_type))
skip = true;
- }
- else
- {
+ break;
+
+ case COFF_SYMBOL_UNDEFINED:
+ /* Undefined symbols are left for the end. */
+ global = true;
+ skip = true;
+ break;
+
+ case COFF_SYMBOL_LOCAL:
/* This is a local symbol. Skip it if we are discarding
local symbols. */
if (finfo->info->discard == discard_all && ! dont_skip_symbol)
skip = true;
+ break;
}
}
/* If we stripping debugging symbols, and this is a debugging
- symbol, then skip it. */
+ symbol, then skip it. FIXME: gas sets the section to N_ABS
+ for some types of debugging symbols; I don't know if this is
+ a bug or not. In any case, we handle it here. */
if (! skip
&& finfo->info->strip == strip_debugger
&& ! dont_skip_symbol
- && isym.n_scnum == N_DEBUG)
+ && (isym.n_scnum == N_DEBUG
+ || (isym.n_scnum == N_ABS
+ && (isym.n_sclass == C_AUTO
+ || isym.n_sclass == C_REG
+ || isym.n_sclass == C_MOS
+ || isym.n_sclass == C_MOE
+ || isym.n_sclass == C_MOU
+ || isym.n_sclass == C_ARG
+ || isym.n_sclass == C_REGPARM
+ || isym.n_sclass == C_FIELD
+ || isym.n_sclass == C_EOS))))
skip = true;
/* If some symbols are stripped based on the name, work out the
{
isym.n_scnum = (*secpp)->output_section->target_index;
isym.n_value += (*secpp)->output_offset;
+ if (! obj_pe (input_bfd))
+ isym.n_value -= (*secpp)->vma;
if (! obj_pe (finfo->output_bfd))
- isym.n_value += ((*secpp)->output_section->vma
- - (*secpp)->vma);
+ isym.n_value += (*secpp)->output_section->vma;
}
/* The value of a C_FILE symbol is the symbol index of the
/* If doing task linking, convert normal global function symbols to
static functions. */
- if (finfo->info->task_link && isym.n_sclass == C_EXT)
+ if (finfo->info->task_link
+ && (isym.n_sclass == C_EXT
+ || isym.n_sclass == C_WEAKEXT
+ || (obj_pe (input_bfd) && isym.n_sclass == C_NT_WEAK)))
isym.n_sclass = C_STAT;
/* Output the symbol. */
&& o->reloc_count != 0)
{
((*_bfd_error_handler)
- ("%s: relocs in section `%s', but it has no contents",
+ (_("%s: relocs in section `%s', but it has no contents"),
bfd_get_filename (input_bfd),
bfd_get_section_name (input_bfd, o)));
bfd_set_error (bfd_error_no_contents);
if (isym.n_sclass == C_NULL)
isym.n_sclass = C_EXT;
- /* If doing task linking and this is the pass where we convert defined globals to
- statics, then do that conversion now. If the symbol is not being converted,
- just ignore it and it will be output during a later pass. */
+ /* If doing task linking and this is the pass where we convert
+ defined globals to statics, then do that conversion now. If the
+ symbol is not being converted, just ignore it and it will be
+ output during a later pass. */
if (finfo->global_to_static)
{
- if (isym.n_sclass != C_EXT)
+ if (isym.n_sclass != C_EXT
+ && isym.n_sclass != C_WEAKEXT
+ && (! obj_pe (output_bfd) || isym.n_sclass != C_NT_WEAK))
{
return true;
}
{
struct coff_final_link_info *finfo = (struct coff_final_link_info *) data;
boolean rtnval = true;
+ boolean save_global_to_static;
if (h->indx < 0)
{
{
case bfd_link_hash_defined:
case bfd_link_hash_defweak:
+ save_global_to_static = finfo->global_to_static;
finfo->global_to_static = true;
rtnval = _bfd_coff_write_global_sym (h, data);
- finfo->global_to_static = false;
+ finfo->global_to_static = save_global_to_static;
+ break;
+ default:
break;
}
}
h = NULL;
sym = NULL;
}
+ else if (symndx < 0
+ || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
+ {
+ (*_bfd_error_handler)
+ ("%s: illegal symbol index %ld in relocs",
+ bfd_get_filename (input_bfd), symndx);
+ return false;
+ }
else
{
h = obj_coff_sym_hashes (input_bfd)[symndx];
val = (sec->output_section->vma
+ sec->output_offset
+ sym->n_value);
- if (! obj_pe (output_bfd))
+ if (! obj_pe (input_bfd))
val -= sec->vma;
}
}
if (info->base_file)
{
/* Emit a reloc if the backend thinks it needs it. */
- if (sym && pe_data(output_bfd)->in_reloc_p(output_bfd, howto))
+ if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
{
- /* relocation to a symbol in a section which
- isn't absolute - we output the address here
- to a file */
- bfd_vma addr = rel->r_vaddr
- - input_section->vma
- + input_section->output_offset
- + input_section->output_section->vma;
- if (coff_data(output_bfd)->pe)
+ /* Relocation to a symbol in a section which isn't
+ absolute. We output the address here to a file.
+ This file is then read by dlltool when generating the
+ reloc section. Note that the base file is not
+ portable between systems. We write out a long here,
+ and dlltool reads in a long. */
+ long addr = (rel->r_vaddr
+ - input_section->vma
+ + input_section->output_offset
+ + input_section->output_section->vma);
+ if (coff_data (output_bfd)->pe)
addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
- /* FIXME: Shouldn't 4 be sizeof (addr)? */
- fwrite (&addr, 1,4, (FILE *) info->base_file);
+ if (fwrite (&addr, 1, sizeof (long), (FILE *) info->base_file)
+ != sizeof (long))
+ {
+ bfd_set_error (bfd_error_system_call);
+ return false;
+ }
}
}
break;
case bfd_reloc_outofrange:
(*_bfd_error_handler)
- ("%s: bad reloc address 0x%lx in section `%s'",
+ (_("%s: bad reloc address 0x%lx in section `%s'"),
bfd_get_filename (input_bfd),
(unsigned long) rel->r_vaddr,
bfd_get_section_name (input_bfd, input_section));