blob: 26808abf6148325324e18a103bf181a2a77eac24 [file] [log] [blame]
/* ELF linker support.
Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001
Free Software Foundation, Inc.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* ELF linker code. */
/* This struct is used to pass information to routines called via
elf_link_hash_traverse which must return failure. */
struct elf_info_failed
{
boolean failed;
struct bfd_link_info *info;
};
static boolean elf_link_add_object_symbols
PARAMS ((bfd *, struct bfd_link_info *));
static boolean elf_link_add_archive_symbols
PARAMS ((bfd *, struct bfd_link_info *));
static boolean elf_merge_symbol
PARAMS ((bfd *, struct bfd_link_info *, const char *, Elf_Internal_Sym *,
asection **, bfd_vma *, struct elf_link_hash_entry **,
boolean *, boolean *, boolean *, boolean));
static boolean elf_export_symbol
PARAMS ((struct elf_link_hash_entry *, PTR));
static boolean elf_fix_symbol_flags
PARAMS ((struct elf_link_hash_entry *, struct elf_info_failed *));
static boolean elf_adjust_dynamic_symbol
PARAMS ((struct elf_link_hash_entry *, PTR));
static boolean elf_link_find_version_dependencies
PARAMS ((struct elf_link_hash_entry *, PTR));
static boolean elf_link_find_version_dependencies
PARAMS ((struct elf_link_hash_entry *, PTR));
static boolean elf_link_assign_sym_version
PARAMS ((struct elf_link_hash_entry *, PTR));
static boolean elf_collect_hash_codes
PARAMS ((struct elf_link_hash_entry *, PTR));
static boolean elf_link_read_relocs_from_section
PARAMS ((bfd *, Elf_Internal_Shdr *, PTR, Elf_Internal_Rela *));
static void elf_link_output_relocs
PARAMS ((bfd *, asection *, Elf_Internal_Shdr *, Elf_Internal_Rela *));
static boolean elf_link_size_reloc_section
PARAMS ((bfd *, Elf_Internal_Shdr *, asection *));
static void elf_link_adjust_relocs
PARAMS ((bfd *, Elf_Internal_Shdr *, unsigned int,
struct elf_link_hash_entry **));
/* Given an ELF BFD, add symbols to the global hash table as
appropriate. */
boolean
elf_bfd_link_add_symbols (abfd, info)
bfd *abfd;
struct bfd_link_info *info;
{
switch (bfd_get_format (abfd))
{
case bfd_object:
return elf_link_add_object_symbols (abfd, info);
case bfd_archive:
return elf_link_add_archive_symbols (abfd, info);
default:
bfd_set_error (bfd_error_wrong_format);
return false;
}
}
/* Return true iff this is a non-common, definition of a non-function symbol. */
static boolean
is_global_data_symbol_definition (abfd, sym)
bfd * abfd ATTRIBUTE_UNUSED;
Elf_Internal_Sym * sym;
{
/* Local symbols do not count, but target specific ones might. */
if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL
&& ELF_ST_BIND (sym->st_info) < STB_LOOS)
return false;
/* Function symbols do not count. */
if (ELF_ST_TYPE (sym->st_info) == STT_FUNC)
return false;
/* If the section is undefined, then so is the symbol. */
if (sym->st_shndx == SHN_UNDEF)
return false;
/* If the symbol is defined in the common section, then
it is a common definition and so does not count. */
if (sym->st_shndx == SHN_COMMON)
return false;
/* If the symbol is in a target specific section then we
must rely upon the backend to tell us what it is. */
if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS)
/* FIXME - this function is not coded yet:
return _bfd_is_global_symbol_definition (abfd, sym);
Instead for now assume that the definition is not global,
Even if this is wrong, at least the linker will behave
in the same way that it used to do. */
return false;
return true;
}
/* Search the symbol table of the archive element of the archive ABFD
whose archive map contains a mention of SYMDEF, and determine if
the symbol is defined in this element. */
static boolean
elf_link_is_defined_archive_symbol (abfd, symdef)
bfd * abfd;
carsym * symdef;
{
Elf_Internal_Shdr * hdr;
Elf_External_Sym * esym;
Elf_External_Sym * esymend;
Elf_External_Sym * buf = NULL;
size_t symcount;
size_t extsymcount;
size_t extsymoff;
boolean result = false;
abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
if (abfd == (bfd *) NULL)
return false;
if (! bfd_check_format (abfd, bfd_object))
return false;
/* If we have already included the element containing this symbol in the
link then we do not need to include it again. Just claim that any symbol
it contains is not a definition, so that our caller will not decide to
(re)include this element. */
if (abfd->archive_pass)
return false;
/* Select the appropriate symbol table. */
if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0)
hdr = &elf_tdata (abfd)->symtab_hdr;
else
hdr = &elf_tdata (abfd)->dynsymtab_hdr;
symcount = hdr->sh_size / sizeof (Elf_External_Sym);
/* The sh_info field of the symtab header tells us where the
external symbols start. We don't care about the local symbols. */
if (elf_bad_symtab (abfd))
{
extsymcount = symcount;
extsymoff = 0;
}
else
{
extsymcount = symcount - hdr->sh_info;
extsymoff = hdr->sh_info;
}
buf = ((Elf_External_Sym *)
bfd_malloc (extsymcount * sizeof (Elf_External_Sym)));
if (buf == NULL && extsymcount != 0)
return false;
/* Read in the symbol table.
FIXME: This ought to be cached somewhere. */
if (bfd_seek (abfd,
hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym),
SEEK_SET) != 0
|| (bfd_read ((PTR) buf, sizeof (Elf_External_Sym), extsymcount, abfd)
!= extsymcount * sizeof (Elf_External_Sym)))
{
free (buf);
return false;
}
/* Scan the symbol table looking for SYMDEF. */
esymend = buf + extsymcount;
for (esym = buf;
esym < esymend;
esym++)
{
Elf_Internal_Sym sym;
const char * name;
elf_swap_symbol_in (abfd, esym, & sym);
name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
if (name == (const char *) NULL)
break;
if (strcmp (name, symdef->name) == 0)
{
result = is_global_data_symbol_definition (abfd, & sym);
break;
}
}
free (buf);
return result;
}
/* Add symbols from an ELF archive file to the linker hash table. We
don't use _bfd_generic_link_add_archive_symbols because of a
problem which arises on UnixWare. The UnixWare libc.so is an
archive which includes an entry libc.so.1 which defines a bunch of
symbols. The libc.so archive also includes a number of other
object files, which also define symbols, some of which are the same
as those defined in libc.so.1. Correct linking requires that we
consider each object file in turn, and include it if it defines any
symbols we need. _bfd_generic_link_add_archive_symbols does not do
this; it looks through the list of undefined symbols, and includes
any object file which defines them. When this algorithm is used on
UnixWare, it winds up pulling in libc.so.1 early and defining a
bunch of symbols. This means that some of the other objects in the
archive are not included in the link, which is incorrect since they
precede libc.so.1 in the archive.
Fortunately, ELF archive handling is simpler than that done by
_bfd_generic_link_add_archive_symbols, which has to allow for a.out
oddities. In ELF, if we find a symbol in the archive map, and the
symbol is currently undefined, we know that we must pull in that
object file.
Unfortunately, we do have to make multiple passes over the symbol
table until nothing further is resolved. */
static boolean
elf_link_add_archive_symbols (abfd, info)
bfd *abfd;
struct bfd_link_info *info;
{
symindex c;
boolean *defined = NULL;
boolean *included = NULL;
carsym *symdefs;
boolean loop;
if (! bfd_has_map (abfd))
{
/* An empty archive is a special case. */
if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
return true;
bfd_set_error (bfd_error_no_armap);
return false;
}
/* Keep track of all symbols we know to be already defined, and all
files we know to be already included. This is to speed up the
second and subsequent passes. */
c = bfd_ardata (abfd)->symdef_count;
if (c == 0)
return true;
defined = (boolean *) bfd_malloc (c * sizeof (boolean));
included = (boolean *) bfd_malloc (c * sizeof (boolean));
if (defined == (boolean *) NULL || included == (boolean *) NULL)
goto error_return;
memset (defined, 0, c * sizeof (boolean));
memset (included, 0, c * sizeof (boolean));
symdefs = bfd_ardata (abfd)->symdefs;
do
{
file_ptr last;
symindex i;
carsym *symdef;
carsym *symdefend;
loop = false;
last = -1;
symdef = symdefs;
symdefend = symdef + c;
for (i = 0; symdef < symdefend; symdef++, i++)
{
struct elf_link_hash_entry *h;
bfd *element;
struct bfd_link_hash_entry *undefs_tail;
symindex mark;
if (defined[i] || included[i])
continue;
if (symdef->file_offset == last)
{
included[i] = true;
continue;
}
h = elf_link_hash_lookup (elf_hash_table (info), symdef->name,
false, false, false);
if (h == NULL)
{
char *p, *copy;
/* If this is a default version (the name contains @@),
look up the symbol again without the version. The
effect is that references to the symbol without the
version will be matched by the default symbol in the
archive. */
p = strchr (symdef->name, ELF_VER_CHR);
if (p == NULL || p[1] != ELF_VER_CHR)
continue;
copy = bfd_alloc (abfd, p - symdef->name + 1);
if (copy == NULL)
goto error_return;
memcpy (copy, symdef->name, p - symdef->name);
copy[p - symdef->name] = '\0';
h = elf_link_hash_lookup (elf_hash_table (info), copy,
false, false, false);
bfd_release (abfd, copy);
}
if (h == NULL)
continue;
if (h->root.type == bfd_link_hash_common)
{
/* We currently have a common symbol. The archive map contains
a reference to this symbol, so we may want to include it. We
only want to include it however, if this archive element
contains a definition of the symbol, not just another common
declaration of it.
Unfortunately some archivers (including GNU ar) will put
declarations of common symbols into their archive maps, as
well as real definitions, so we cannot just go by the archive
map alone. Instead we must read in the element's symbol
table and check that to see what kind of symbol definition
this is. */
if (! elf_link_is_defined_archive_symbol (abfd, symdef))
continue;
}
else if (h->root.type != bfd_link_hash_undefined)
{
if (h->root.type != bfd_link_hash_undefweak)
defined[i] = true;
continue;
}
/* We need to include this archive member. */
element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset);
if (element == (bfd *) NULL)
goto error_return;
if (! bfd_check_format (element, bfd_object))
goto error_return;
/* Doublecheck that we have not included this object
already--it should be impossible, but there may be
something wrong with the archive. */
if (element->archive_pass != 0)
{
bfd_set_error (bfd_error_bad_value);
goto error_return;
}
element->archive_pass = 1;
undefs_tail = info->hash->undefs_tail;
if (! (*info->callbacks->add_archive_element) (info, element,
symdef->name))
goto error_return;
if (! elf_link_add_object_symbols (element, info))
goto error_return;
/* If there are any new undefined symbols, we need to make
another pass through the archive in order to see whether
they can be defined. FIXME: This isn't perfect, because
common symbols wind up on undefs_tail and because an
undefined symbol which is defined later on in this pass
does not require another pass. This isn't a bug, but it
does make the code less efficient than it could be. */
if (undefs_tail != info->hash->undefs_tail)
loop = true;
/* Look backward to mark all symbols from this object file
which we have already seen in this pass. */
mark = i;
do
{
included[mark] = true;
if (mark == 0)
break;
--mark;
}
while (symdefs[mark].file_offset == symdef->file_offset);
/* We mark subsequent symbols from this object file as we go
on through the loop. */
last = symdef->file_offset;
}
}
while (loop);
free (defined);
free (included);
return true;
error_return:
if (defined != (boolean *) NULL)
free (defined);
if (included != (boolean *) NULL)
free (included);
return false;
}
/* This function is called when we want to define a new symbol. It
handles the various cases which arise when we find a definition in
a dynamic object, or when there is already a definition in a
dynamic object. The new symbol is described by NAME, SYM, PSEC,
and PVALUE. We set SYM_HASH to the hash table entry. We set
OVERRIDE if the old symbol is overriding a new definition. We set
TYPE_CHANGE_OK if it is OK for the type to change. We set
SIZE_CHANGE_OK if it is OK for the size to change. By OK to
change, we mean that we shouldn't warn if the type or size does
change. DT_NEEDED indicates if it comes from a DT_NEEDED entry of
a shared object. */
static boolean
elf_merge_symbol (abfd, info, name, sym, psec, pvalue, sym_hash,
override, type_change_ok, size_change_ok, dt_needed)
bfd *abfd;
struct bfd_link_info *info;
const char *name;
Elf_Internal_Sym *sym;
asection **psec;
bfd_vma *pvalue;
struct elf_link_hash_entry **sym_hash;
boolean *override;
boolean *type_change_ok;
boolean *size_change_ok;
boolean dt_needed;
{
asection *sec;
struct elf_link_hash_entry *h;
int bind;
bfd *oldbfd;
boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon;
*override = false;
sec = *psec;
bind = ELF_ST_BIND (sym->st_info);
if (! bfd_is_und_section (sec))
h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false);
else
h = ((struct elf_link_hash_entry *)
bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false));
if (h == NULL)
return false;
*sym_hash = h;
/* This code is for coping with dynamic objects, and is only useful
if we are doing an ELF link. */
if (info->hash->creator != abfd->xvec)
return true;
/* For merging, we only care about real symbols. */
while (h->root.type == bfd_link_hash_indirect
|| h->root.type == bfd_link_hash_warning)
h = (struct elf_link_hash_entry *) h->root.u.i.link;
/* If we just created the symbol, mark it as being an ELF symbol.
Other than that, there is nothing to do--there is no merge issue
with a newly defined symbol--so we just return. */
if (h->root.type == bfd_link_hash_new)
{
h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
return true;
}
/* OLDBFD is a BFD associated with the existing symbol. */
switch (h->root.type)
{
default:
oldbfd = NULL;
break;
case bfd_link_hash_undefined:
case bfd_link_hash_undefweak:
oldbfd = h->root.u.undef.abfd;
break;
case bfd_link_hash_defined:
case bfd_link_hash_defweak:
oldbfd = h->root.u.def.section->owner;
break;
case bfd_link_hash_common:
oldbfd = h->root.u.c.p->section->owner;
break;
}
/* In cases involving weak versioned symbols, we may wind up trying
to merge a symbol with itself. Catch that here, to avoid the
confusion that results if we try to override a symbol with
itself. The additional tests catch cases like
_GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a
dynamic object, which we do want to handle here. */
if (abfd == oldbfd
&& ((abfd->flags & DYNAMIC) == 0
|| (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))
return true;
/* NEWDYN and OLDDYN indicate whether the new or old symbol,
respectively, is from a dynamic object. */
if ((abfd->flags & DYNAMIC) != 0)
newdyn = true;
else
newdyn = false;
if (oldbfd != NULL)
olddyn = (oldbfd->flags & DYNAMIC) != 0;
else
{
asection *hsec;
/* This code handles the special SHN_MIPS_{TEXT,DATA} section
indices used by MIPS ELF. */
switch (h->root.type)
{
default:
hsec = NULL;
break;
case bfd_link_hash_defined:
case bfd_link_hash_defweak:
hsec = h->root.u.def.section;
break;
case bfd_link_hash_common:
hsec = h->root.u.c.p->section;
break;
}
if (hsec == NULL)
olddyn = false;
else
olddyn = (hsec->symbol->flags & BSF_DYNAMIC) != 0;
}
/* NEWDEF and OLDDEF indicate whether the new or old symbol,
respectively, appear to be a definition rather than reference. */
if (bfd_is_und_section (sec) || bfd_is_com_section (sec))
newdef = false;
else
newdef = true;
if (h->root.type == bfd_link_hash_undefined
|| h->root.type == bfd_link_hash_undefweak
|| h->root.type == bfd_link_hash_common)
olddef = false;
else
olddef = true;
/* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old
symbol, respectively, appears to be a common symbol in a dynamic
object. If a symbol appears in an uninitialized section, and is
not weak, and is not a function, then it may be a common symbol
which was resolved when the dynamic object was created. We want
to treat such symbols specially, because they raise special
considerations when setting the symbol size: if the symbol
appears as a common symbol in a regular object, and the size in
the regular object is larger, we must make sure that we use the
larger size. This problematic case can always be avoided in C,
but it must be handled correctly when using Fortran shared
libraries.
Note that if NEWDYNCOMMON is set, NEWDEF will be set, and
likewise for OLDDYNCOMMON and OLDDEF.
Note that this test is just a heuristic, and that it is quite
possible to have an uninitialized symbol in a shared object which
is really a definition, rather than a common symbol. This could
lead to some minor confusion when the symbol really is a common
symbol in some regular object. However, I think it will be
harmless. */
if (newdyn
&& newdef
&& (sec->flags & SEC_ALLOC) != 0
&& (sec->flags & SEC_LOAD) == 0
&& sym->st_size > 0
&& bind != STB_WEAK
&& ELF_ST_TYPE (sym->st_info) != STT_FUNC)
newdyncommon = true;
else
newdyncommon = false;
if (olddyn
&& olddef
&& h->root.type == bfd_link_hash_defined
&& (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
&& (h->root.u.def.section->flags & SEC_ALLOC) != 0
&& (h->root.u.def.section->flags & SEC_LOAD) == 0
&& h->size > 0
&& h->type != STT_FUNC)
olddyncommon = true;
else
olddyncommon = false;
/* It's OK to change the type if either the existing symbol or the
new symbol is weak unless it comes from a DT_NEEDED entry of
a shared object, in which case, the DT_NEEDED entry may not be
required at the run time. */
if ((! dt_needed && h->root.type == bfd_link_hash_defweak)
|| h->root.type == bfd_link_hash_undefweak
|| bind == STB_WEAK)
*type_change_ok = true;
/* It's OK to change the size if either the existing symbol or the
new symbol is weak, or if the old symbol is undefined. */
if (*type_change_ok
|| h->root.type == bfd_link_hash_undefined)
*size_change_ok = true;
/* If both the old and the new symbols look like common symbols in a
dynamic object, set the size of the symbol to the larger of the
two. */
if (olddyncommon
&& newdyncommon
&& sym->st_size != h->size)
{
/* Since we think we have two common symbols, issue a multiple
common warning if desired. Note that we only warn if the
size is different. If the size is the same, we simply let
the old symbol override the new one as normally happens with
symbols defined in dynamic objects. */
if (! ((*info->callbacks->multiple_common)
(info, h->root.root.string, oldbfd, bfd_link_hash_common,
h->size, abfd, bfd_link_hash_common, sym->st_size)))
return false;
if (sym->st_size > h->size)
h->size = sym->st_size;
*size_change_ok = true;
}
/* If we are looking at a dynamic object, and we have found a
definition, we need to see if the symbol was already defined by
some other object. If so, we want to use the existing
definition, and we do not want to report a multiple symbol
definition error; we do this by clobbering *PSEC to be
bfd_und_section_ptr.
We treat a common symbol as a definition if the symbol in the
shared library is a function, since common symbols always
represent variables; this can cause confusion in principle, but
any such confusion would seem to indicate an erroneous program or
shared library. We also permit a common symbol in a regular
object to override a weak symbol in a shared object.
We prefer a non-weak definition in a shared library to a weak
definition in the executable unless it comes from a DT_NEEDED
entry of a shared object, in which case, the DT_NEEDED entry
may not be required at the run time. */
if (newdyn
&& newdef
&& (olddef
|| (h->root.type == bfd_link_hash_common
&& (bind == STB_WEAK
|| ELF_ST_TYPE (sym->st_info) == STT_FUNC)))
&& (h->root.type != bfd_link_hash_defweak
|| dt_needed
|| bind == STB_WEAK))
{
*override = true;
newdef = false;
newdyncommon = false;
*psec = sec = bfd_und_section_ptr;
*size_change_ok = true;
/* If we get here when the old symbol is a common symbol, then
we are explicitly letting it override a weak symbol or
function in a dynamic object, and we don't want to warn about
a type change. If the old symbol is a defined symbol, a type
change warning may still be appropriate. */
if (h->root.type == bfd_link_hash_common)
*type_change_ok = true;
}
/* Handle the special case of an old common symbol merging with a
new symbol which looks like a common symbol in a shared object.
We change *PSEC and *PVALUE to make the new symbol look like a
common symbol, and let _bfd_generic_link_add_one_symbol will do
the right thing. */
if (newdyncommon
&& h->root.type == bfd_link_hash_common)
{
*override = true;
newdef = false;
newdyncommon = false;
*pvalue = sym->st_size;
*psec = sec = bfd_com_section_ptr;
*size_change_ok = true;
}
/* If the old symbol is from a dynamic object, and the new symbol is
a definition which is not from a dynamic object, then the new
symbol overrides the old symbol. Symbols from regular files
always take precedence over symbols from dynamic objects, even if
they are defined after the dynamic object in the link.
As above, we again permit a common symbol in a regular object to
override a definition in a shared object if the shared object
symbol is a function or is weak.
As above, we permit a non-weak definition in a shared object to
override a weak definition in a regular object. */
if (! newdyn
&& (newdef
|| (bfd_is_com_section (sec)
&& (h->root.type == bfd_link_hash_defweak
|| h->type == STT_FUNC)))
&& olddyn
&& olddef
&& (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
&& (bind != STB_WEAK
|| h->root.type == bfd_link_hash_defweak))
{
/* Change the hash table entry to undefined, and let
_bfd_generic_link_add_one_symbol do the right thing with the
new definition. */
h->root.type = bfd_link_hash_undefined;
h->root.u.undef.abfd = h->root.u.def.section->owner;
*size_change_ok = true;
olddef = false;
olddyncommon = false;
/* We again permit a type change when a common symbol may be
overriding a function. */
if (bfd_is_com_section (sec))
*type_change_ok = true;
/* This union may have been set to be non-NULL when this symbol
was seen in a dynamic object. We must force the union to be
NULL, so that it is correct for a regular symbol. */
h->verinfo.vertree = NULL;
/* In this special case, if H is the target of an indirection,
we want the caller to frob with H rather than with the
indirect symbol. That will permit the caller to redefine the
target of the indirection, rather than the indirect symbol
itself. FIXME: This will break the -y option if we store a
symbol with a different name. */
*sym_hash = h;
}
/* Handle the special case of a new common symbol merging with an
old symbol that looks like it might be a common symbol defined in
a shared object. Note that we have already handled the case in
which a new common symbol should simply override the definition
in the shared library. */
if (! newdyn
&& bfd_is_com_section (sec)
&& olddyncommon)
{
/* It would be best if we could set the hash table entry to a
common symbol, but we don't know what to use for the section
or the alignment. */
if (! ((*info->callbacks->multiple_common)
(info, h->root.root.string, oldbfd, bfd_link_hash_common,
h->size, abfd, bfd_link_hash_common, sym->st_size)))
return false;
/* If the predumed common symbol in the dynamic object is
larger, pretend that the new symbol has its size. */
if (h->size > *pvalue)
*pvalue = h->size;
/* FIXME: We no longer know the alignment required by the symbol
in the dynamic object, so we just wind up using the one from
the regular object. */
olddef = false;
olddyncommon = false;
h->root.type = bfd_link_hash_undefined;
h->root.u.undef.abfd = h->root.u.def.section->owner;
*size_change_ok = true;
*type_change_ok = true;
h->verinfo.vertree = NULL;
}
/* Handle the special case of a weak definition in a regular object
followed by a non-weak definition in a shared object. In this
case, we prefer the definition in the shared object unless it
comes from a DT_NEEDED entry of a shared object, in which case,
the DT_NEEDED entry may not be required at the run time. */
if (olddef
&& ! dt_needed
&& h->root.type == bfd_link_hash_defweak
&& newdef
&& newdyn
&& bind != STB_WEAK)
{
/* To make this work we have to frob the flags so that the rest
of the code does not think we are using the regular
definition. */
if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
h->elf_link_hash_flags |= ELF_LINK_HASH_REF_REGULAR;
else if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0)
h->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
h->elf_link_hash_flags &= ~ (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_DEF_DYNAMIC);
/* If H is the target of an indirection, we want the caller to
use H rather than the indirect symbol. Otherwise if we are
defining a new indirect symbol we will wind up attaching it
to the entry we are overriding. */
*sym_hash = h;
}
/* Handle the special case of a non-weak definition in a shared
object followed by a weak definition in a regular object. In
this case we prefer to definition in the shared object. To make
this work we have to tell the caller to not treat the new symbol
as a definition. */
if (olddef
&& olddyn
&& h->root.type != bfd_link_hash_defweak
&& newdef
&& ! newdyn
&& bind == STB_WEAK)
*override = true;
return true;
}
/* Add symbols from an ELF object file to the linker hash table. */
static boolean
elf_link_add_object_symbols (abfd, info)
bfd *abfd;
struct bfd_link_info *info;
{
boolean (*add_symbol_hook) PARAMS ((bfd *, struct bfd_link_info *,
const Elf_Internal_Sym *,
const char **, flagword *,
asection **, bfd_vma *));
boolean (*check_relocs) PARAMS ((bfd *, struct bfd_link_info *,
asection *, const Elf_Internal_Rela *));
boolean collect;
Elf_Internal_Shdr *hdr;
size_t symcount;
size_t extsymcount;
size_t extsymoff;
Elf_External_Sym *buf = NULL;
struct elf_link_hash_entry **sym_hash;
boolean dynamic;
Elf_External_Versym *extversym = NULL;
Elf_External_Versym *ever;
Elf_External_Dyn *dynbuf = NULL;
struct elf_link_hash_entry *weaks;
Elf_External_Sym *esym;
Elf_External_Sym *esymend;
struct elf_backend_data *bed;
boolean dt_needed;
bed = get_elf_backend_data (abfd);
add_symbol_hook = bed->elf_add_symbol_hook;
collect = bed->collect;
if ((abfd->flags & DYNAMIC) == 0)
dynamic = false;
else
{
dynamic = true;
/* You can't use -r against a dynamic object. Also, there's no
hope of using a dynamic object which does not exactly match
the format of the output file. */
if (info->relocateable || info->hash->creator != abfd->xvec)
{
bfd_set_error (bfd_error_invalid_operation);
goto error_return;
}
}
/* As a GNU extension, any input sections which are named
.gnu.warning.SYMBOL are treated as warning symbols for the given
symbol. This differs from .gnu.warning sections, which generate
warnings when they are included in an output file. */
if (! info->shared)
{
asection *s;
for (s = abfd->sections; s != NULL; s = s->next)
{
const char *name;
name = bfd_get_section_name (abfd, s);
if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0)
{
char *msg;
bfd_size_type sz;
name += sizeof ".gnu.warning." - 1;
/* If this is a shared object, then look up the symbol
in the hash table. If it is there, and it is already
been defined, then we will not be using the entry
from this shared object, so we don't need to warn.
FIXME: If we see the definition in a regular object
later on, we will warn, but we shouldn't. The only
fix is to keep track of what warnings we are supposed
to emit, and then handle them all at the end of the
link. */
if (dynamic && abfd->xvec == info->hash->creator)
{
struct elf_link_hash_entry *h;
h = elf_link_hash_lookup (elf_hash_table (info), name,
false, false, true);
/* FIXME: What about bfd_link_hash_common? */
if (h != NULL
&& (h->root.type == bfd_link_hash_defined
|| h->root.type == bfd_link_hash_defweak))
{
/* We don't want to issue this warning. Clobber
the section size so that the warning does not
get copied into the output file. */
s->_raw_size = 0;
continue;
}
}
sz = bfd_section_size (abfd, s);
msg = (char *) bfd_alloc (abfd, sz + 1);
if (msg == NULL)
goto error_return;
if (! bfd_get_section_contents (abfd, s, msg, (file_ptr) 0, sz))
goto error_return;
msg[sz] = '\0';
if (! (_bfd_generic_link_add_one_symbol
(info, abfd, name, BSF_WARNING, s, (bfd_vma) 0, msg,
false, collect, (struct bfd_link_hash_entry **) NULL)))
goto error_return;
if (! info->relocateable)
{
/* Clobber the section size so that the warning does
not get copied into the output file. */
s->_raw_size = 0;
}
}
}
}
/* If this is a dynamic object, we always link against the .dynsym
symbol table, not the .symtab symbol table. The dynamic linker
will only see the .dynsym symbol table, so there is no reason to
look at .symtab for a dynamic object. */
if (! dynamic || elf_dynsymtab (abfd) == 0)
hdr = &elf_tdata (abfd)->symtab_hdr;
else
hdr = &elf_tdata (abfd)->dynsymtab_hdr;
if (dynamic)
{
/* Read in any version definitions. */
if (! _bfd_elf_slurp_version_tables (abfd))
goto error_return;
/* Read in the symbol versions, but don't bother to convert them
to internal format. */
if (elf_dynversym (abfd) != 0)
{
Elf_Internal_Shdr *versymhdr;
versymhdr = &elf_tdata (abfd)->dynversym_hdr;
extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size);
if (extversym == NULL)
goto error_return;
if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0
|| (bfd_read ((PTR) extversym, 1, versymhdr->sh_size, abfd)
!= versymhdr->sh_size))
goto error_return;
}
}
symcount = hdr->sh_size / sizeof (Elf_External_Sym);
/* The sh_info field of the symtab header tells us where the
external symbols start. We don't care about the local symbols at
this point. */
if (elf_bad_symtab (abfd))
{
extsymcount = symcount;
extsymoff = 0;
}
else
{
extsymcount = symcount - hdr->sh_info;
extsymoff = hdr->sh_info;
}
buf = ((Elf_External_Sym *)
bfd_malloc (extsymcount * sizeof (Elf_External_Sym)));
if (buf == NULL && extsymcount != 0)
goto error_return;
/* We store a pointer to the hash table entry for each external
symbol. */
sym_hash = ((struct elf_link_hash_entry **)
bfd_alloc (abfd,
extsymcount * sizeof (struct elf_link_hash_entry *)));
if (sym_hash == NULL)
goto error_return;
elf_sym_hashes (abfd) = sym_hash;
dt_needed = false;
if (! dynamic)
{
/* If we are creating a shared library, create all the dynamic
sections immediately. We need to attach them to something,
so we attach them to this BFD, provided it is the right
format. FIXME: If there are no input BFD's of the same
format as the output, we can't make a shared library. */
if (info->shared
&& ! elf_hash_table (info)->dynamic_sections_created
&& abfd->xvec == info->hash->creator)
{
if (! elf_link_create_dynamic_sections (abfd, info))
goto error_return;
}
}
else
{
asection *s;
boolean add_needed;
const char *name;
bfd_size_type oldsize;
bfd_size_type strindex;
/* Find the name to use in a DT_NEEDED entry that refers to this
object. If the object has a DT_SONAME entry, we use it.
Otherwise, if the generic linker stuck something in
elf_dt_name, we use that. Otherwise, we just use the file
name. If the generic linker put a null string into
elf_dt_name, we don't make a DT_NEEDED entry at all, even if
there is a DT_SONAME entry. */
add_needed = true;
name = bfd_get_filename (abfd);
if (elf_dt_name (abfd) != NULL)
{
name = elf_dt_name (abfd);
if (*name == '\0')
{
if (elf_dt_soname (abfd) != NULL)
dt_needed = true;
add_needed = false;
}
}
s = bfd_get_section_by_name (abfd, ".dynamic");
if (s != NULL)
{
Elf_External_Dyn *extdyn;
Elf_External_Dyn *extdynend;
int elfsec;
unsigned long link;
int rpath;
int runpath;
dynbuf = (Elf_External_Dyn *) bfd_malloc ((size_t) s->_raw_size);
if (dynbuf == NULL)
goto error_return;
if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf,
(file_ptr) 0, s->_raw_size))
goto error_return;
elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
if (elfsec == -1)
goto error_return;
link = elf_elfsections (abfd)[elfsec]->sh_link;
{
/* The shared libraries distributed with hpux11 have a bogus
sh_link field for the ".dynamic" section. This code detects
when LINK refers to a section that is not a string table and
tries to find the string table for the ".dynsym" section
instead. */
Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[link];
if (hdr->sh_type != SHT_STRTAB)
{
asection *s = bfd_get_section_by_name (abfd, ".dynsym");
int elfsec = _bfd_elf_section_from_bfd_section (abfd, s);
if (elfsec == -1)
goto error_return;
link = elf_elfsections (abfd)[elfsec]->sh_link;
}
}
extdyn = dynbuf;
extdynend = extdyn + s->_raw_size / sizeof (Elf_External_Dyn);
rpath = 0;
runpath = 0;
for (; extdyn < extdynend; extdyn++)
{
Elf_Internal_Dyn dyn;
elf_swap_dyn_in (abfd, extdyn, &dyn);
if (dyn.d_tag == DT_SONAME)
{
name = bfd_elf_string_from_elf_section (abfd, link,
dyn.d_un.d_val);
if (name == NULL)
goto error_return;
}
if (dyn.d_tag == DT_NEEDED)
{
struct bfd_link_needed_list *n, **pn;
char *fnm, *anm;
n = ((struct bfd_link_needed_list *)
bfd_alloc (abfd, sizeof (struct bfd_link_needed_list)));
fnm = bfd_elf_string_from_elf_section (abfd, link,
dyn.d_un.d_val);
if (n == NULL || fnm == NULL)
goto error_return;
anm = bfd_alloc (abfd, strlen (fnm) + 1);
if (anm == NULL)
goto error_return;
strcpy (anm, fnm);
n->name = anm;
n->by = abfd;
n->next = NULL;
for (pn = &elf_hash_table (info)->needed;
*pn != NULL;
pn = &(*pn)->next)
;
*pn = n;
}
if (dyn.d_tag == DT_RUNPATH)
{
struct bfd_link_needed_list *n, **pn;
char *fnm, *anm;
/* When we see DT_RPATH before DT_RUNPATH, we have
to clear runpath. Do _NOT_ bfd_release, as that
frees all more recently bfd_alloc'd blocks as
well. */
if (rpath && elf_hash_table (info)->runpath)
elf_hash_table (info)->runpath = NULL;
n = ((struct bfd_link_needed_list *)
bfd_alloc (abfd, sizeof (struct bfd_link_needed_list)));
fnm = bfd_elf_string_from_elf_section (abfd, link,
dyn.d_un.d_val);
if (n == NULL || fnm == NULL)
goto error_return;
anm = bfd_alloc (abfd, strlen (fnm) + 1);
if (anm == NULL)
goto error_return;
strcpy (anm, fnm);
n->name = anm;
n->by = abfd;
n->next = NULL;
for (pn = &elf_hash_table (info)->runpath;
*pn != NULL;
pn = &(*pn)->next)
;
*pn = n;
runpath = 1;
rpath = 0;
}
/* Ignore DT_RPATH if we have seen DT_RUNPATH. */
if (!runpath && dyn.d_tag == DT_RPATH)
{
struct bfd_link_needed_list *n, **pn;
char *fnm, *anm;
n = ((struct bfd_link_needed_list *)
bfd_alloc (abfd, sizeof (struct bfd_link_needed_list)));
fnm = bfd_elf_string_from_elf_section (abfd, link,
dyn.d_un.d_val);
if (n == NULL || fnm == NULL)
goto error_return;
anm = bfd_alloc (abfd, strlen (fnm) + 1);
if (anm == NULL)
goto error_return;
strcpy (anm, fnm);
n->name = anm;
n->by = abfd;
n->next = NULL;
for (pn = &elf_hash_table (info)->runpath;
*pn != NULL;
pn = &(*pn)->next)
;
*pn = n;
rpath = 1;
}
}
free (dynbuf);
dynbuf = NULL;
}
/* We do not want to include any of the sections in a dynamic
object in the output file. We hack by simply clobbering the
list of sections in the BFD. This could be handled more
cleanly by, say, a new section flag; the existing
SEC_NEVER_LOAD flag is not the one we want, because that one
still implies that the section takes up space in the output
file. */
abfd->sections = NULL;
abfd->section_count = 0;
/* If this is the first dynamic object found in the link, create
the special sections required for dynamic linking. */
if (! elf_hash_table (info)->dynamic_sections_created)
{
if (! elf_link_create_dynamic_sections (abfd, info))
goto error_return;
}
if (add_needed)
{
/* Add a DT_NEEDED entry for this dynamic object. */
oldsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
strindex = _bfd_stringtab_add (elf_hash_table (info)->dynstr, name,
true, false);
if (strindex == (bfd_size_type) -1)
goto error_return;
if (oldsize == _bfd_stringtab_size (elf_hash_table (info)->dynstr))
{
asection *sdyn;
Elf_External_Dyn *dyncon, *dynconend;
/* The hash table size did not change, which means that
the dynamic object name was already entered. If we
have already included this dynamic object in the
link, just ignore it. There is no reason to include
a particular dynamic object more than once. */
sdyn = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
".dynamic");
BFD_ASSERT (sdyn != NULL);
dyncon = (Elf_External_Dyn *) sdyn->contents;
dynconend = (Elf_External_Dyn *) (sdyn->contents +
sdyn->_raw_size);
for (; dyncon < dynconend; dyncon++)
{
Elf_Internal_Dyn dyn;
elf_swap_dyn_in (elf_hash_table (info)->dynobj, dyncon,
&dyn);
if (dyn.d_tag == DT_NEEDED
&& dyn.d_un.d_val == strindex)
{
if (buf != NULL)
free (buf);
if (extversym != NULL)
free (extversym);
return true;
}
}
}
if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
goto error_return;
}
/* Save the SONAME, if there is one, because sometimes the
linker emulation code will need to know it. */
if (*name == '\0')
name = bfd_get_filename (abfd);
elf_dt_name (abfd) = name;
}
if (bfd_seek (abfd,
hdr->sh_offset + extsymoff * sizeof (Elf_External_Sym),
SEEK_SET) != 0
|| (bfd_read ((PTR) buf, sizeof (Elf_External_Sym), extsymcount, abfd)
!= extsymcount * sizeof (Elf_External_Sym)))
goto error_return;
weaks = NULL;
ever = extversym != NULL ? extversym + extsymoff : NULL;
esymend = buf + extsymcount;
for (esym = buf;
esym < esymend;
esym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL))
{
Elf_Internal_Sym sym;
int bind;
bfd_vma value;
asection *sec;
flagword flags;
const char *name;
struct elf_link_hash_entry *h;
boolean definition;
boolean size_change_ok, type_change_ok;
boolean new_weakdef;
unsigned int old_alignment;
elf_swap_symbol_in (abfd, esym, &sym);
flags = BSF_NO_FLAGS;
sec = NULL;
value = sym.st_value;
*sym_hash = NULL;
bind = ELF_ST_BIND (sym.st_info);
if (bind == STB_LOCAL)
{
/* This should be impossible, since ELF requires that all
global symbols follow all local symbols, and that sh_info
point to the first global symbol. Unfortunatealy, Irix 5
screws this up. */
continue;
}
else if (bind == STB_GLOBAL)
{
if (sym.st_shndx != SHN_UNDEF
&& sym.st_shndx != SHN_COMMON)
flags = BSF_GLOBAL;
}
else if (bind == STB_WEAK)
flags = BSF_WEAK;
else
{
/* Leave it up to the processor backend. */
}
if (sym.st_shndx == SHN_UNDEF)
sec = bfd_und_section_ptr;
else if (sym.st_shndx > 0 && sym.st_shndx < SHN_LORESERVE)
{
sec = section_from_elf_index (abfd, sym.st_shndx);
if (sec == NULL)
sec = bfd_abs_section_ptr;
else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
value -= sec->vma;
}
else if (sym.st_shndx == SHN_ABS)
sec = bfd_abs_section_ptr;
else if (sym.st_shndx == SHN_COMMON)
{
sec = bfd_com_section_ptr;
/* What ELF calls the size we call the value. What ELF
calls the value we call the alignment. */
value = sym.st_size;
}
else
{
/* Leave it up to the processor backend. */
}
name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, sym.st_name);
if (name == (const char *) NULL)
goto error_return;
if (add_symbol_hook)
{
if (! (*add_symbol_hook) (abfd, info, &sym, &name, &flags, &sec,
&value))
goto error_return;
/* The hook function sets the name to NULL if this symbol
should be skipped for some reason. */
if (name == (const char *) NULL)
continue;
}
/* Sanity check that all possibilities were handled. */
if (sec == (asection *) NULL)
{
bfd_set_error (bfd_error_bad_value);
goto error_return;
}
if (bfd_is_und_section (sec)
|| bfd_is_com_section (sec))
definition = false;
else
definition = true;
size_change_ok = false;
type_change_ok = get_elf_backend_data (abfd)->type_change_ok;
old_alignment = 0;
if (info->hash->creator->flavour == bfd_target_elf_flavour)
{
Elf_Internal_Versym iver;
unsigned int vernum = 0;
boolean override;
if (ever != NULL)
{
_bfd_elf_swap_versym_in (abfd, ever, &iver);
vernum = iver.vs_vers & VERSYM_VERSION;
/* If this is a hidden symbol, or if it is not version
1, we append the version name to the symbol name.
However, we do not modify a non-hidden absolute
symbol, because it might be the version symbol
itself. FIXME: What if it isn't? */
if ((iver.vs_vers & VERSYM_HIDDEN) != 0
|| (vernum > 1 && ! bfd_is_abs_section (sec)))
{
const char *verstr;
int namelen, newlen;
char *newname, *p;
if (sym.st_shndx != SHN_UNDEF)
{
if (vernum > elf_tdata (abfd)->dynverdef_hdr.sh_info)
{
(*_bfd_error_handler)
(_("%s: %s: invalid version %u (max %d)"),
bfd_get_filename (abfd), name, vernum,
elf_tdata (abfd)->dynverdef_hdr.sh_info);
bfd_set_error (bfd_error_bad_value);
goto error_return;
}
else if (vernum > 1)
verstr =
elf_tdata (abfd)->verdef[vernum - 1].vd_nodename;
else
verstr = "";
}
else
{
/* We cannot simply test for the number of
entries in the VERNEED section since the
numbers for the needed versions do not start
at 0. */
Elf_Internal_Verneed *t;
verstr = NULL;
for (t = elf_tdata (abfd)->verref;
t != NULL;
t = t->vn_nextref)
{
Elf_Internal_Vernaux *a;
for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr)
{
if (a->vna_other == vernum)
{
verstr = a->vna_nodename;
break;
}
}
if (a != NULL)
break;
}
if (verstr == NULL)
{
(*_bfd_error_handler)
(_("%s: %s: invalid needed version %d"),
bfd_get_filename (abfd), name, vernum);
bfd_set_error (bfd_error_bad_value);
goto error_return;
}
}
namelen = strlen (name);
newlen = namelen + strlen (verstr) + 2;
if ((iver.vs_vers & VERSYM_HIDDEN) == 0)
++newlen;
newname = (char *) bfd_alloc (abfd, newlen);
if (newname == NULL)
goto error_return;
strcpy (newname, name);
p = newname + namelen;
*p++ = ELF_VER_CHR;
/* If this is a defined non-hidden version symbol,
we add another @ to the name. This indicates the
default version of the symbol. */
if ((iver.vs_vers & VERSYM_HIDDEN) == 0
&& sym.st_shndx != SHN_UNDEF)
*p++ = ELF_VER_CHR;
strcpy (p, verstr);
name = newname;
}
}
if (! elf_merge_symbol (abfd, info, name, &sym, &sec, &value,
sym_hash, &override, &type_change_ok,
&size_change_ok, dt_needed))
goto error_return;
if (override)
definition = false;
h = *sym_hash;
while (h->root.type == bfd_link_hash_indirect
|| h->root.type == bfd_link_hash_warning)
h = (struct elf_link_hash_entry *) h->root.u.i.link;
/* Remember the old alignment if this is a common symbol, so
that we don't reduce the alignment later on. We can't
check later, because _bfd_generic_link_add_one_symbol
will set a default for the alignment which we want to
override. */
if (h->root.type == bfd_link_hash_common)
old_alignment = h->root.u.c.p->alignment_power;
if (elf_tdata (abfd)->verdef != NULL
&& ! override
&& vernum > 1
&& definition)
h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1];
}
if (! (_bfd_generic_link_add_one_symbol
(info, abfd, name, flags, sec, value, (const char *) NULL,
false, collect, (struct bfd_link_hash_entry **) sym_hash)))
goto error_return;
h = *sym_hash;
while (h->root.type == bfd_link_hash_indirect
|| h->root.type == bfd_link_hash_warning)
h = (struct elf_link_hash_entry *) h->root.u.i.link;
*sym_hash = h;
new_weakdef = false;
if (dynamic
&& definition
&& (flags & BSF_WEAK) != 0
&& ELF_ST_TYPE (sym.st_info) != STT_FUNC
&& info->hash->creator->flavour == bfd_target_elf_flavour
&& h->weakdef == NULL)
{
/* Keep a list of all weak defined non function symbols from
a dynamic object, using the weakdef field. Later in this
function we will set the weakdef field to the correct
value. We only put non-function symbols from dynamic
objects on this list, because that happens to be the only
time we need to know the normal symbol corresponding to a
weak symbol, and the information is time consuming to
figure out. If the weakdef field is not already NULL,
then this symbol was already defined by some previous
dynamic object, and we will be using that previous
definition anyhow. */
h->weakdef = weaks;
weaks = h;
new_weakdef = true;
}
/* Set the alignment of a common symbol. */
if (sym.st_shndx == SHN_COMMON
&& h->root.type == bfd_link_hash_common)
{
unsigned int align;
align = bfd_log2 (sym.st_value);
if (align > old_alignment
/* Permit an alignment power of zero if an alignment of one
is specified and no other alignments have been specified. */
|| (sym.st_value == 1 && old_alignment == 0))
h->root.u.c.p->alignment_power = align;
}
if (info->hash->creator->flavour == bfd_target_elf_flavour)
{
int old_flags;
boolean dynsym;
int new_flag;
/* Remember the symbol size and type. */
if (sym.st_size != 0
&& (definition || h->size == 0))
{
if (h->size != 0 && h->size != sym.st_size && ! size_change_ok)
(*_bfd_error_handler)
(_("Warning: size of symbol `%s' changed from %lu to %lu in %s"),
name, (unsigned long) h->size, (unsigned long) sym.st_size,
bfd_get_filename (abfd));
h->size = sym.st_size;
}
/* If this is a common symbol, then we always want H->SIZE
to be the size of the common symbol. The code just above
won't fix the size if a common symbol becomes larger. We
don't warn about a size change here, because that is
covered by --warn-common. */
if (h->root.type == bfd_link_hash_common)
h->size = h->root.u.c.size;
if (ELF_ST_TYPE (sym.st_info) != STT_NOTYPE
&& (definition || h->type == STT_NOTYPE))
{
if (h->type != STT_NOTYPE
&& h->type != ELF_ST_TYPE (sym.st_info)
&& ! type_change_ok)
(*_bfd_error_handler)
(_("Warning: type of symbol `%s' changed from %d to %d in %s"),
name, h->type, ELF_ST_TYPE (sym.st_info),
bfd_get_filename (abfd));
h->type = ELF_ST_TYPE (sym.st_info);
}
/* If st_other has a processor-specific meaning, specific code
might be needed here. */
if (sym.st_other != 0)
{
/* Combine visibilities, using the most constraining one. */
unsigned char hvis = ELF_ST_VISIBILITY (h->other);
unsigned char symvis = ELF_ST_VISIBILITY (sym.st_other);
if (symvis && (hvis > symvis || hvis == 0))
h->other = sym.st_other;
/* If neither has visibility, use the st_other of the
definition. This is an arbitrary choice, since the
other bits have no general meaning. */
if (!symvis && !hvis
&& (definition || h->other == 0))
h->other = sym.st_other;
}
/* Set a flag in the hash table entry indicating the type of
reference or definition we just found. Keep a count of
the number of dynamic symbols we find. A dynamic symbol
is one which is referenced or defined by both a regular
object and a shared object. */
old_flags = h->elf_link_hash_flags;
dynsym = false;
if (! dynamic)
{
if (! definition)
{
new_flag = ELF_LINK_HASH_REF_REGULAR;
if (bind != STB_WEAK)
new_flag |= ELF_LINK_HASH_REF_REGULAR_NONWEAK;
}
else
new_flag = ELF_LINK_HASH_DEF_REGULAR;
if (info->shared
|| (old_flags & (ELF_LINK_HASH_DEF_DYNAMIC
| ELF_LINK_HASH_REF_DYNAMIC)) != 0)
dynsym = true;
}
else
{
if (! definition)
new_flag = ELF_LINK_HASH_REF_DYNAMIC;
else
new_flag = ELF_LINK_HASH_DEF_DYNAMIC;
if ((old_flags & (ELF_LINK_HASH_DEF_REGULAR
| ELF_LINK_HASH_REF_REGULAR)) != 0
|| (h->weakdef != NULL
&& ! new_weakdef
&& h->weakdef->dynindx != -1))
dynsym = true;
}
h->elf_link_hash_flags |= new_flag;
/* If this symbol has a version, and it is the default
version, we create an indirect symbol from the default
name to the fully decorated name. This will cause
external references which do not specify a version to be
bound to this version of the symbol. */
if (definition || h->root.type == bfd_link_hash_common)
{
char *p;
p = strchr (name, ELF_VER_CHR);
if (p != NULL && p[1] == ELF_VER_CHR)
{
char *shortname;
struct elf_link_hash_entry *hi;
boolean override;
shortname = bfd_hash_allocate (&info->hash->table,
p - name + 1);
if (shortname == NULL)
goto error_return;
strncpy (shortname, name, p - name);
shortname[p - name] = '\0';
/* We are going to create a new symbol. Merge it
with any existing symbol with this name. For the
purposes of the merge, act as though we were
defining the symbol we just defined, although we
actually going to define an indirect symbol. */
type_change_ok = false;
size_change_ok = false;
if (! elf_merge_symbol (abfd, info, shortname, &sym, &sec,
&value, &hi, &override,
&type_change_ok,
&size_change_ok, dt_needed))
goto error_return;
if (! override)
{
if (! (_bfd_generic_link_add_one_symbol
(info, abfd, shortname, BSF_INDIRECT,
bfd_ind_section_ptr, (bfd_vma) 0, name, false,
collect, (struct bfd_link_hash_entry **) &hi)))
goto error_return;
}
else
{
/* In this case the symbol named SHORTNAME is
overriding the indirect symbol we want to
add. We were planning on making SHORTNAME an
indirect symbol referring to NAME. SHORTNAME
is the name without a version. NAME is the
fully versioned name, and it is the default
version.
Overriding means that we already saw a
definition for the symbol SHORTNAME in a
regular object, and it is overriding the
symbol defined in the dynamic object.
When this happens, we actually want to change
NAME, the symbol we just added, to refer to
SHORTNAME. This will cause references to
NAME in the shared object to become
references to SHORTNAME in the regular
object. This is what we expect when we
override a function in a shared object: that
the references in the shared object will be
mapped to the definition in the regular
object. */
while (hi->root.type == bfd_link_hash_indirect
|| hi->root.type == bfd_link_hash_warning)
hi = (struct elf_link_hash_entry *) hi->root.u.i.link;
h->root.type = bfd_link_hash_indirect;
h->root.u.i.link = (struct bfd_link_hash_entry *) hi;
if (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC)
{
h->elf_link_hash_flags &=~ ELF_LINK_HASH_DEF_DYNAMIC;
hi->elf_link_hash_flags |= ELF_LINK_HASH_REF_DYNAMIC;
if (hi->elf_link_hash_flags
& (ELF_LINK_HASH_REF_REGULAR
| ELF_LINK_HASH_DEF_REGULAR))
{
if (! _bfd_elf_link_record_dynamic_symbol (info,
hi))
goto error_return;
}
}
/* Now set HI to H, so that the following code
will set the other fields correctly. */
hi = h;
}
/* If there is a duplicate definition somewhere,
then HI may not point to an indirect symbol. We
will have reported an error to the user in that
case. */
if (hi->root.type == bfd_link_hash_indirect)
{
struct elf_link_hash_entry *ht;
/* If the symbol became indirect, then we assume
that we have not seen a definition before. */
BFD_ASSERT ((hi->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
| ELF_LINK_HASH_DEF_REGULAR))
== 0);
ht = (struct elf_link_hash_entry *) hi->root.u.i.link;
(*bed->elf_backend_copy_indirect_symbol) (ht, hi);
/* See if the new flags lead us to realize that
the symbol must be dynamic. */
if (! dynsym)
{
if (! dynamic)
{
if (info->shared
|| ((hi->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC)
!= 0))
dynsym = true;
}
else
{
if ((hi->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR) != 0)
dynsym = true;
}
}
}
/* We also need to define an indirection from the
nondefault version of the symbol. */
shortname = bfd_hash_allocate (&info->hash->table,
strlen (name));
if (shortname == NULL)
goto error_return;
strncpy (shortname, name, p - name);
strcpy (shortname + (p - name), p + 1);
/* Once again, merge with any existing symbol. */
type_change_ok = false;
size_change_ok = false;
if (! elf_merge_symbol (abfd, info, shortname, &sym, &sec,
&value, &hi, &override,
&type_change_ok,
&size_change_ok, dt_needed))
goto error_return;
if (override)
{
/* Here SHORTNAME is a versioned name, so we
don't expect to see the type of override we
do in the case above. */
(*_bfd_error_handler)
(_("%s: warning: unexpected redefinition of `%s'"),
bfd_get_filename (abfd), shortname);
}
else
{
if (! (_bfd_generic_link_add_one_symbol
(info, abfd, shortname, BSF_INDIRECT,
bfd_ind_section_ptr, (bfd_vma) 0, name, false,
collect, (struct bfd_link_hash_entry **) &hi)))
goto error_return;
/* If there is a duplicate definition somewhere,
then HI may not point to an indirect symbol.
We will have reported an error to the user in
that case. */
if (hi->root.type == bfd_link_hash_indirect)
{
/* If the symbol became indirect, then we
assume that we have not seen a definition
before. */
BFD_ASSERT ((hi->elf_link_hash_flags
& (ELF_LINK_HASH_DEF_DYNAMIC
| ELF_LINK_HASH_DEF_REGULAR))
== 0);
(*bed->elf_backend_copy_indirect_symbol) (h, hi);
/* See if the new flags lead us to realize
that the symbol must be dynamic. */
if (! dynsym)
{
if (! dynamic)
{
if (info->shared
|| ((hi->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC)
!= 0))
dynsym = true;
}
else
{
if ((hi->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR) != 0)
dynsym = true;
}
}
}
}
}
}
if (dynsym && h->dynindx == -1)
{
if (! _bfd_elf_link_record_dynamic_symbol (info, h))
goto error_return;
if (h->weakdef != NULL
&& ! new_weakdef
&& h->weakdef->dynindx == -1)
{
if (! _bfd_elf_link_record_dynamic_symbol (info,
h->weakdef))
goto error_return;
}
}
else if (dynsym && h->dynindx != -1)
/* If the symbol already has a dynamic index, but
visibility says it should not be visible, turn it into
a local symbol. */
switch (ELF_ST_VISIBILITY (h->other))
{
case STV_INTERNAL:
case STV_HIDDEN:
h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL;
(*bed->elf_backend_hide_symbol) (info, h);
break;
}
if (dt_needed && definition
&& (h->elf_link_hash_flags
& ELF_LINK_HASH_REF_REGULAR) != 0)
{
bfd_size_type oldsize;
bfd_size_type strindex;
/* The symbol from a DT_NEEDED object is referenced from
the regular object to create a dynamic executable. We
have to make sure there is a DT_NEEDED entry for it. */
dt_needed = false;
oldsize = _bfd_stringtab_size (elf_hash_table (info)->dynstr);
strindex = _bfd_stringtab_add (elf_hash_table (info)->dynstr,
elf_dt_soname (abfd),
true, false);
if (strindex == (bfd_size_type) -1)
goto error_return;
if (oldsize
== _bfd_stringtab_size (elf_hash_table (info)->dynstr))
{
asection *sdyn;
Elf_External_Dyn *dyncon, *dynconend;
sdyn = bfd_get_section_by_name (elf_hash_table (info)->dynobj,
".dynamic");
BFD_ASSERT (sdyn != NULL);
dyncon = (Elf_External_Dyn *) sdyn->contents;
dynconend = (Elf_External_Dyn *) (sdyn->contents +
sdyn->_raw_size);
for (; dyncon < dynconend; dyncon++)
{
Elf_Internal_Dyn dyn;
elf_swap_dyn_in (elf_hash_table (info)->dynobj,
dyncon, &dyn);
BFD_ASSERT (dyn.d_tag != DT_NEEDED ||
dyn.d_un.d_val != strindex);
}
}
if (! elf_add_dynamic_entry (info, DT_NEEDED, strindex))
goto error_return;
}
}
}
/* Now set the weakdefs field correctly for all the weak defined
symbols we found. The only way to do this is to search all the
symbols. Since we only need the information for non functions in
dynamic objects, that's the only time we actually put anything on
the list WEAKS. We need this information so that if a regular
object refers to a symbol defined weakly in a dynamic object, the
real symbol in the dynamic object is also put in the dynamic
symbols; we also must arrange for both symbols to point to the
same memory location. We could handle the general case of symbol
aliasing, but a general symbol alias can only be generated in
assembler code, handling it correctly would be very time
consuming, and other ELF linkers don't handle general aliasing
either. */
while (weaks != NULL)
{
struct elf_link_hash_entry *hlook;
asection *slook;
bfd_vma vlook;
struct elf_link_hash_entry **hpp;
struct elf_link_hash_entry **hppend;
hlook = weaks;
weaks = hlook->weakdef;
hlook->weakdef = NULL;
BFD_ASSERT (hlook->root.type == bfd_link_hash_defined
|| hlook->root.type == bfd_link_hash_defweak
|| hlook->root.type == bfd_link_hash_common
|| hlook->root.type == bfd_link_hash_indirect);
slook = hlook->root.u.def.section;
vlook = hlook->root.u.def.value;
hpp = elf_sym_hashes (abfd);
hppend = hpp + extsymcount;
for (; hpp < hppend; hpp++)
{
struct elf_link_hash_entry *h;
h = *hpp;
if (h != NULL && h != hlook
&& h->root.type == bfd_link_hash_defined
&& h->root.u.def.section == slook
&& h->root.u.def.value == vlook)
{
hlook->weakdef = h;
/* If the weak definition is in the list of dynamic
symbols, make sure the real definition is put there
as well. */
if (hlook->dynindx != -1
&& h->dynindx == -1)
{
if (! _bfd_elf_link_record_dynamic_symbol (info, h))
goto error_return;
}
/* If the real definition is in the list of dynamic
symbols, make sure the weak definition is put there
as well. If we don't do this, then the dynamic
loader might not merge the entries for the real
definition and the weak definition. */
if (h->dynindx != -1
&& hlook->dynindx == -1)
{
if (! _bfd_elf_link_record_dynamic_symbol (info, hlook))
goto error_return;
}
break;
}
}
}
if (buf != NULL)
{
free (buf);
buf = NULL;
}
if (extversym != NULL)
{
free (extversym);
extversym = NULL;
}
/* If this object is the same format as the output object, and it is
not a shared library, then let the backend look through the
relocs.
This is required to build global offset table entries and to
arrange for dynamic relocs. It is not required for the
particular common case of linking non PIC code, even when linking
against shared libraries, but unfortunately there is no way of
knowing whether an object file has been compiled PIC or not.
Looking through the relocs is not particularly time consuming.
The problem is that we must either (1) keep the relocs in memory,
which causes the linker to require additional runtime memory or
(2) read the relocs twice from the input file, which wastes time.
This would be a good case for using mmap.
I have no idea how to handle linking PIC code into a file of a
different format. It probably can't be done. */
check_relocs = get_elf_backend_data (abfd)->check_relocs;
if (! dynamic
&& abfd->xvec == info->hash->creator
&& check_relocs != NULL)
{
asection *o;
for (o = abfd->sections; o != NULL; o = o->next)
{
Elf_Internal_Rela *internal_relocs;
boolean ok;
if ((o->flags & SEC_RELOC) == 0
|| o->reloc_count == 0
|| ((info->strip == strip_all || info->strip == strip_debugger)
&& (o->flags & SEC_DEBUGGING) != 0)
|| bfd_is_abs_section (o->output_section))
continue;
internal_relocs = (NAME(_bfd_elf,link_read_relocs)
(abfd, o, (PTR) NULL,
(Elf_Internal_Rela *) NULL,
info->keep_memory));
if (internal_relocs == NULL)
goto error_return;
ok = (*check_relocs) (abfd, info, o, internal_relocs);
if (! info->keep_memory)
free (internal_relocs);
if (! ok)
goto error_return;
}
}
/* If this is a non-traditional, non-relocateable link, try to
optimize the handling of the .stab/.stabstr sections. */
if (! dynamic
&& ! info->relocateable
&& ! info->traditional_format
&& info->hash->creator->flavour == bfd_target_elf_flavour
&& (info->strip != strip_all && info->strip != strip_debugger))
{
asection *stab, *stabstr;
stab = bfd_get_section_by_name (abfd, ".stab");
if (stab != NULL)
{
stabstr = bfd_get_section_by_name (abfd, ".stabstr");
if (stabstr != NULL)
{
struct bfd_elf_section_data *secdata;
secdata = elf_section_data (stab);
if (! _bfd_link_section_stabs (abfd,
&elf_hash_table (info)->stab_info,
stab, stabstr,
&secdata->stab_info))
goto error_return;
}
}
}
return true;
error_return:
if (buf != NULL)
free (buf);
if (dynbuf != NULL)
free (dynbuf);
if (extversym != NULL)
free (extversym);
return false;
}
/* Create some sections which will be filled in with dynamic linking
information. ABFD is an input file which requires dynamic sections
to be created. The dynamic sections take up virtual memory space
when the final executable is run, so we need to create them before
addresses are assigned to the output sections. We work out the
actual contents and size of these sections later. */
boolean
elf_link_create_dynamic_sections (abfd, info)
bfd *abfd;
struct bfd_link_info *info;
{
flagword flags;
register asection *s;
struct elf_link_hash_entry *h;
struct elf_backend_data *bed;
if (elf_hash_table (info)->dynamic_sections_created)
return true;
/* Make sure that all dynamic sections use the same input BFD. */
if (elf_hash_table (info)->dynobj == NULL)
elf_hash_table (info)->dynobj = abfd;
else
abfd = elf_hash_table (info)->dynobj;
/* Note that we set the SEC_IN_MEMORY flag for all of these
sections. */
flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
| SEC_IN_MEMORY | SEC_LINKER_CREATED);
/* A dynamically linked executable has a .interp section, but a
shared library does not. */
if (! info->shared)
{
s = bfd_make_section (abfd, ".interp");
if (s == NULL
|| ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
return false;
}
/* Create sections to hold version informations. These are removed
if they are not needed. */
s = bfd_make_section (abfd, ".gnu.version_d");
if (s == NULL
|| ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
|| ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
return false;
s = bfd_make_section (abfd, ".gnu.version");
if (s == NULL
|| ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
|| ! bfd_set_section_alignment (abfd, s, 1))
return false;
s = bfd_make_section (abfd, ".gnu.version_r");
if (s == NULL
|| ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
|| ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
return false;
s = bfd_make_section (abfd, ".dynsym");
if (s == NULL
|| ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
|| ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
return false;
s = bfd_make_section (abfd, ".dynstr");
if (s == NULL
|| ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY))
return false;
/* Create a strtab to hold the dynamic symbol names. */
if (elf_hash_table (info)->dynstr == NULL)
{
elf_hash_table (info)->dynstr = elf_stringtab_init ();
if (elf_hash_table (info)->dynstr == NULL)
return false;
}
s = bfd_make_section (abfd, ".dynamic");
if (s == NULL
|| ! bfd_set_section_flags (abfd, s, flags)
|| ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
return false;
/* The special symbol _DYNAMIC is always set to the start of the
.dynamic section. This call occurs before we have processed the
symbols for any dynamic object, so we don't have to worry about
overriding a dynamic definition. We could set _DYNAMIC in a
linker script, but we only want to define it if we are, in fact,
creating a .dynamic section. We don't want to define it if there
is no .dynamic section, since on some ELF platforms the start up
code examines it to decide how to initialize the process. */
h = NULL;
if (! (_bfd_generic_link_add_one_symbol
(info, abfd, "_DYNAMIC", BSF_GLOBAL, s, (bfd_vma) 0,
(const char *) NULL, false, get_elf_backend_data (abfd)->collect,
(struct bfd_link_hash_entry **) &h)))
return false;
h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
h->type = STT_OBJECT;
if (info->shared
&& ! _bfd_elf_link_record_dynamic_symbol (info, h))
return false;
bed = get_elf_backend_data (abfd);
s = bfd_make_section (abfd, ".hash");
if (s == NULL
|| ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
|| ! bfd_set_section_alignment (abfd, s, LOG_FILE_ALIGN))
return false;
elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry;
/* Let the backend create the rest of the sections. This lets the
backend set the right flags. The backend will normally create
the .got and .plt sections. */
if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info))
return false;
elf_hash_table (info)->dynamic_sections_created = true;
return true;
}
/* Add an entry to the .dynamic table. */
boolean
elf_add_dynamic_entry (info, tag, val)
struct bfd_link_info *info;
bfd_vma tag;
bfd_vma val;
{
Elf_Internal_Dyn dyn;
bfd *dynobj;
asection *s;
size_t newsize;
bfd_byte *newcontents;
dynobj = elf_hash_table (info)->dynobj;
s = bfd_get_section_by_name (dynobj, ".dynamic");
BFD_ASSERT (s != NULL);
newsize = s->_raw_size + sizeof (Elf_External_Dyn);
newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize);
if (newcontents == NULL)
return false;
dyn.d_tag = tag;
dyn.d_un.d_val = val;
elf_swap_dyn_out (dynobj, &dyn,
(Elf_External_Dyn *) (newcontents + s->_raw_size));
s->_raw_size = newsize;
s->contents = newcontents;
return true;
}
/* Record a new local dynamic symbol. */
boolean
elf_link_record_local_dynamic_symbol (info, input_bfd, input_indx)
struct bfd_link_info *info;
bfd *input_bfd;
long input_indx;
{
struct elf_link_local_dynamic_entry *entry;
struct elf_link_hash_table *eht;
struct bfd_strtab_hash *dynstr;
Elf_External_Sym esym;
unsigned long dynstr_index;
char *name;
/* See if the entry exists already. */
for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next)
if (entry->input_bfd == input_bfd && entry->input_indx == input_indx)
return true;
entry = (struct elf_link_local_dynamic_entry *)
bfd_alloc (input_bfd, sizeof (*entry));
if (entry == NULL)
return false;
/* Go find the symbol, so that we can find it's name. */
if (bfd_seek (input_bfd,
(elf_tdata (input_bfd)->symtab_hdr.sh_offset
+ input_indx * sizeof (Elf_External_Sym)),
SEEK_SET) != 0
|| (bfd_read (&esym, sizeof (Elf_External_Sym), 1, input_bfd)
!= sizeof (Elf_External_Sym)))
return false;
elf_swap_symbol_in (input_bfd, &esym, &entry->isym);
name = (bfd_elf_string_from_elf_section
(input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link,
entry->isym.st_name));
dynstr = elf_hash_table (info)->dynstr;
if (dynstr == NULL)
{
/* Create a strtab to hold the dynamic symbol names. */
elf_hash_table (info)->dynstr = dynstr = _bfd_elf_stringtab_init ();
if (dynstr == NULL)
return false;
}
dynstr_index = _bfd_stringtab_add (dynstr, name, true, false);
if (dynstr_index == (unsigned long) -1)
return false;
entry->isym.st_name = dynstr_index;
eht = elf_hash_table (info);
entry->next = eht->dynlocal;
eht->dynlocal = entry;
entry->input_bfd = input_bfd;
entry->input_indx = input_indx;
eht->dynsymcount++;
/* Whatever binding the symbol had before, it's now local. */
entry->isym.st_info
= ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info));
/* The dynindx will be set at the end of size_dynamic_sections. */
return true;
}
/* Read and swap the relocs from the section indicated by SHDR. This
may be either a REL or a RELA section. The relocations are
translated into RELA relocations and stored in INTERNAL_RELOCS,
which should have already been allocated to contain enough space.
The EXTERNAL_RELOCS are a buffer where the external form of the
relocations should be stored.
Returns false if something goes wrong. */
static boolean
elf_link_read_relocs_from_section (abfd, shdr, external_relocs,
internal_relocs)
bfd *abfd;
Elf_Internal_Shdr *shdr;
PTR external_relocs;
Elf_Internal_Rela *internal_relocs;
{
struct elf_backend_data *bed;
/* If there aren't any relocations, that's OK. */
if (!shdr)
return true;
/* Position ourselves at the start of the section. */
if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0)
return false;
/* Read the relocations. */
if (bfd_read (external_relocs, 1, shdr->sh_size, abfd)
!= shdr->sh_size)
return false;
bed = get_elf_backend_data (abfd);
/* Convert the external relocations to the internal format. */
if (shdr->sh_entsize == sizeof (Elf_External_Rel))
{
Elf_External_Rel *erel;
Elf_External_Rel *erelend;
Elf_Internal_Rela *irela;
Elf_Internal_Rel *irel;
erel = (Elf_External_Rel *) external_relocs;
erelend = erel + NUM_SHDR_ENTRIES (shdr);
irela = internal_relocs;
irel = bfd_alloc (abfd, (bed->s->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rel)));
for (; erel < erelend; erel++, irela += bed->s->int_rels_per_ext_rel)
{
unsigned int i;
if (bed->s->swap_reloc_in)
(*bed->s->swap_reloc_in) (abfd, (bfd_byte *) erel, irel);
else
elf_swap_reloc_in (abfd, erel, irel);
for (i = 0; i < bed->s->int_rels_per_ext_rel; ++i)
{
irela[i].r_offset = irel[i].r_offset;
irela[i].r_info = irel[i].r_info;
irela[i].r_addend = 0;
}
}
}
else
{
Elf_External_Rela *erela;
Elf_External_Rela *erelaend;
Elf_Internal_Rela *irela;
BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
erela = (Elf_External_Rela *) external_relocs;
erelaend = erela + NUM_SHDR_ENTRIES (shdr);
irela = internal_relocs;
for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
{
if (bed->s->swap_reloca_in)
(*bed->s->swap_reloca_in) (abfd, (bfd_byte *) erela, irela);
else
elf_swap_reloca_in (abfd, erela, irela);
}
}
return true;
}
/* Read and swap the relocs for a section O. They may have been
cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are
not NULL, they are used as buffers to read into. They are known to
be large enough. If the INTERNAL_RELOCS relocs argument is NULL,
the return value is allocated using either malloc or bfd_alloc,
according to the KEEP_MEMORY argument. If O has two relocation
sections (both REL and RELA relocations), then the REL_HDR
relocations will appear first in INTERNAL_RELOCS, followed by the
REL_HDR2 relocations. */
Elf_Internal_Rela *
NAME(_bfd_elf,link_read_relocs) (abfd, o, external_relocs, internal_relocs,
keep_memory)
bfd *abfd;
asection *o;
PTR external_relocs;
Elf_Internal_Rela *internal_relocs;
boolean keep_memory;
{
Elf_Internal_Shdr *rel_hdr;
PTR alloc1 = NULL;
Elf_Internal_Rela *alloc2 = NULL;
struct elf_backend_data *bed = get_elf_backend_data (abfd);
if (elf_section_data (o)->relocs != NULL)
return elf_section_data (o)->relocs;
if (o->reloc_count == 0)
return NULL;
rel_hdr = &elf_section_data (o)->rel_hdr;
if (internal_relocs == NULL)
{
size_t size;
size = (o->reloc_count * bed->s->int_rels_per_ext_rel
* sizeof (Elf_Internal_Rela));
if (keep_memory)
internal_relocs = (Elf_Internal_Rela *) bfd_alloc (abfd, size);
else
internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size);
if (internal_relocs == NULL)
goto error_return;
}
if (external_relocs == NULL)
{
size_t size = (size_t) rel_hdr->sh_size;
if (elf_section_data (o)->rel_hdr2)
size += (size_t) elf_section_data (o)->rel_hdr2->sh_size;
alloc1 = (PTR) bfd_malloc (size);
if (alloc1 == NULL)
goto error_return;
external_relocs = alloc1;
}
if (!elf_link_read_relocs_from_section (abfd, rel_hdr,
external_relocs,
internal_relocs))
goto error_return;
if (!elf_link_read_relocs_from_section
(abfd,
elf_section_data (o)->rel_hdr2,
((bfd_byte *) external_relocs) + rel_hdr->sh_size,
internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
* bed->s->int_rels_per_ext_rel)))
goto error_return;
/* Cache the results for next time, if we can. */
if (keep_memory)
elf_section_data (o)->relocs = internal_relocs;
if (alloc1 != NULL)
free (alloc1);
/* Don't free alloc2, since if it was allocated we are passing it
back (under the name of internal_relocs). */
return internal_relocs;
error_return:
if (alloc1 != NULL)
free (alloc1);
if (alloc2 != NULL)
free (alloc2);
return NULL;
}
/* Record an assignment to a symbol made by a linker script. We need
this in case some dynamic object refers to this symbol. */
/*ARGSUSED*/
boolean
NAME(bfd_elf,record_link_assignment) (output_bfd, info, name, provide)
bfd *output_bfd ATTRIBUTE_UNUSED;
struct bfd_link_info *info;
const char *name;
boolean provide;
{
struct elf_link_hash_entry *h;
if (info->hash->creator->flavour != bfd_target_elf_flavour)
return true;
h = elf_link_hash_lookup (elf_hash_table (info), name, true, true, false);
if (h == NULL)
return false;
if (h->root.type == bfd_link_hash_new)
h->elf_link_hash_flags &=~ ELF_LINK_NON_ELF;
/* If this symbol is being provided by the linker script, and it is
currently defined by a dynamic object, but not by a regular
object, then mark it as undefined so that the generic linker will
force the correct value. */
if (provide
&& (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
&& (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
h->root.type = bfd_link_hash_undefined;
/* If this symbol is not being provided by the linker script, and it is
currently defined by a dynamic object, but not by a regular object,
then clear out any version information because the symbol will not be
associated with the dynamic object any more. */
if (!provide
&& (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0
&& (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0)
h->verinfo.verdef = NULL;
h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR;
/* When possible, keep the original type of the symbol */
if (h->type == STT_NOTYPE)
h->type = STT_OBJECT;
if (((h->elf_link_hash_flags & (ELF_LINK_HASH_DEF_DYNAMIC
| ELF_LINK_HASH_REF_DYNAMIC)) != 0
|| info->shared)
&& h->dynindx == -1)
{
if (! _bfd_elf_link_record_dynamic_symbol (info, h))
return false;
/* If this is a weak defined symbol, and we know a corresponding
real symbol from the same dynamic object, make sure the real
symbol is also made into a dynamic symbol. */
if (h->weakdef != NULL
&& h->weakdef->dynindx == -1)
{
if (! _bfd_elf_link_record_dynamic_symbol (info, h->weakdef))
return false;
}
}
return true;
}
/* This structure is used to pass information to
elf_link_assign_sym_version. */
struct elf_assign_sym_version_info
{
/* Output BFD. */
bfd *output_bfd;
/* General link information. */
struct bfd_link_info *info;
/* Version tree. */
struct bfd_elf_version_tree *verdefs;
/* Whether we are exporting all dynamic symbols. */
boolean export_dynamic;
/* Whether we had a failure. */
boolean failed;
};
/* This structure is used to pass information to
elf_link_find_version_dependencies. */
struct elf_find_verdep_info
{
/* Output BFD. */
bfd *output_bfd;
/* General link information. */
struct bfd_link_info *info;
/* The number of dependencies. */
unsigned int vers;
/* Whether we had a failure. */
boolean failed;
};
/* Array used to determine the number of hash table buckets to use
based on the number of symbols there are. If there are fewer than
3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets,
fewer than 37 we use 17 buckets, and so forth. We never use more
than 32771 buckets. */
static const size_t elf_buckets[] =
{
1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
16411, 32771, 0
};
/* Compute bucket count for hashing table. We do not use a static set
of possible tables sizes anymore. Instead we determine for all
possible reasonable sizes of the table the outcome (i.e., the
number of collisions etc) and choose the best solution. The
weighting functions are not too simple to allow the table to grow
without bounds. Instead one of the weighting factors is the size.
Therefore the result is always a good payoff between few collisions
(= short chain lengths) and table size. */
static size_t
compute_bucket_count (info)
struct bfd_link_info *info;
{
size_t dynsymcount = elf_hash_table (info)->dynsymcount;
size_t best_size = 0;
unsigned long int *hashcodes;
unsigned long int *hashcodesp;
unsigned long int i;
/* Compute the hash values for all exported symbols. At the same
time store the values in an array so that we could use them for
optimizations. */
hashcodes = (unsigned long int *) bfd_malloc (dynsymcount
* sizeof (unsigned long int));
if (hashcodes == NULL)
return 0;
hashcodesp = hashcodes;
/* Put all hash values in HASHCODES. */
elf_link_hash_traverse (elf_hash_table (info),
elf_collect_hash_codes, &hashcodesp);
/* We have a problem here. The following code to optimize the table
size requires an integer type with more the 32 bits. If
BFD_HOST_U_64_BIT is set we know about such a type. */
#ifdef BFD_HOST_U_64_BIT
if (info->optimize == true)
{
unsigned long int nsyms = hashcodesp - hashcodes;
size_t minsize;
size_t maxsize;
BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0);
unsigned long int *counts ;
/* Possible optimization parameters: if we have NSYMS symbols we say
that the hashing table must at least have NSYMS/4 and at most
2*NSYMS buckets. */
minsize = nsyms / 4;
if (minsize == 0)
minsize = 1;
best_size = maxsize = nsyms * 2;
/* Create array where we count the collisions in. We must use bfd_malloc
since the size could be large. */
counts = (unsigned long int *) bfd_malloc (maxsize
* sizeof (unsigned long int));
if (counts == NULL)
{
free (hashcodes);
return 0;
}
/* Compute the "optimal" size for the hash table. The criteria is a
minimal chain length. The minor criteria is (of course) the size
of the table. */
for (i = minsize; i < maxsize; ++i)
{
/* Walk through the array of hashcodes and count the collisions. */
BFD_HOST_U_64_BIT max;
unsigned long int j;
unsigned long int fact;
memset (counts, '\0', i * sizeof (unsigned long int));
/* Determine how often each hash bucket is used. */
for (j = 0; j < nsyms; ++j)
++counts[hashcodes[j] % i];
/* For the weight function we need some information about the
pagesize on the target. This is information need not be 100%
accurate. Since this information is not available (so far) we
define it here to a reasonable default value. If it is crucial
to have a better value some day simply define this value. */
# ifndef BFD_TARGET_PAGESIZE
# define BFD_TARGET_PAGESIZE (4096)
# endif
/* We in any case need 2 + NSYMS entries for the size values and
the chains. */
max = (2 + nsyms) * (ARCH_SIZE / 8);
# if 1
/* Variant 1: optimize for short chains. We add the squares
of all the chain lengths (which favous many small ch