summaryrefslogtreecommitdiffstats
path: root/contrib/binutils/ld/ldctor.c
diff options
context:
space:
mode:
authorobrien <obrien@FreeBSD.org>2000-05-12 23:15:20 +0000
committerobrien <obrien@FreeBSD.org>2000-05-12 23:15:20 +0000
commit2a9ea95d682586d2b0c31da28d82a73d786c7c0a (patch)
tree9d4ce42d357c391a11d77254b770908c02ecf672 /contrib/binutils/ld/ldctor.c
parentbffe850874e72664f78cf171ab1c4339b9b63cab (diff)
downloadFreeBSD-src-2a9ea95d682586d2b0c31da28d82a73d786c7c0a.zip
FreeBSD-src-2a9ea95d682586d2b0c31da28d82a73d786c7c0a.tar.gz
Import of Binutils 2.10 snapshot.
Diffstat (limited to 'contrib/binutils/ld/ldctor.c')
-rw-r--r--contrib/binutils/ld/ldctor.c149
1 files changed, 138 insertions, 11 deletions
diff --git a/contrib/binutils/ld/ldctor.c b/contrib/binutils/ld/ldctor.c
index 6e40938..0a434b8 100644
--- a/contrib/binutils/ld/ldctor.c
+++ b/contrib/binutils/ld/ldctor.c
@@ -1,5 +1,6 @@
/* ldctor.c -- constructor support routines
- Copyright (C) 1991, 92, 93, 94, 1995 Free Software Foundation, Inc.
+ Copyright (C) 1991, 92, 93, 94, 95, 96, 97, 1998
+ Free Software Foundation, Inc.
By Steve Chamberlain <sac@cygnus.com>
This file is part of GLD, the Gnu Linker.
@@ -15,13 +16,16 @@ 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 GLD; see the file COPYING. If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+along with GLD; see the file COPYING. If not, write to the Free
+Software Foundation, 59 Temple Place - Suite 330, Boston, MA
+02111-1307, USA. */
#include "bfd.h"
#include "sysdep.h"
#include "bfdlink.h"
+#include <ctype.h>
+
#include "ld.h"
#include "ldexp.h"
#include "ldlang.h"
@@ -30,10 +34,18 @@ the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307
#include "ldmain.h"
#include "ldctor.h"
+static int ctor_prio PARAMS ((const char *));
+static int ctor_cmp PARAMS ((const PTR, const PTR));
+
/* The list of statements needed to handle constructors. These are
invoked by the command CONSTRUCTORS in the linker script. */
lang_statement_list_type constructor_list;
+/* Whether the constructors should be sorted. Note that this is
+ global for the entire link; we assume that there is only a single
+ CONSTRUCTORS command in the linker script. */
+boolean constructors_sorted;
+
/* The sets we have seen. */
struct set_info *sets;
@@ -74,7 +86,7 @@ ldctor_add_set_entry (h, reloc, name, section, value)
{
if (p->reloc != reloc)
{
- einfo ("%P%X: Different relocs used in set %s\n", h->root.string);
+ einfo (_("%P%X: Different relocs used in set %s\n"), h->root.string);
return;
}
@@ -91,7 +103,7 @@ ldctor_add_set_entry (h, reloc, name, section, value)
&& strcmp (bfd_get_target (section->owner),
bfd_get_target (p->elements->section->owner)) != 0)
{
- einfo ("%P%X: Different object file formats composing set %s\n",
+ einfo (_("%P%X: Different object file formats composing set %s\n"),
h->root.string);
return;
}
@@ -110,6 +122,80 @@ ldctor_add_set_entry (h, reloc, name, section, value)
++p->count;
}
+/* Get the priority of a g++ global constructor or destructor from the
+ symbol name. */
+
+static int
+ctor_prio (name)
+ const char *name;
+{
+ /* The name will look something like _GLOBAL_$I$65535$test02__Fv.
+ There might be extra leading underscores, and the $ characters
+ might be something else. The I might be a D. */
+
+ while (*name == '_')
+ ++name;
+
+ if (strncmp (name, "GLOBAL_", sizeof "GLOBAL_" - 1) != 0)
+ return -1;
+
+ name += sizeof "GLOBAL_" - 1;
+
+ if (name[0] != name[2])
+ return -1;
+ if (name[1] != 'I' && name[1] != 'D')
+ return -1;
+ if (! isdigit ((unsigned char) name[3]))
+ return -1;
+
+ return atoi (name + 3);
+}
+
+/* This function is used to sort constructor elements by priority. It
+ is called via qsort. */
+
+static int
+ctor_cmp (p1, p2)
+ const PTR p1;
+ const PTR p2;
+{
+ const struct set_element **pe1 = (const struct set_element **) p1;
+ const struct set_element **pe2 = (const struct set_element **) p2;
+ const char *n1;
+ const char *n2;
+ int prio1;
+ int prio2;
+
+ n1 = (*pe1)->name;
+ if (n1 == NULL)
+ n1 = "";
+ n2 = (*pe2)->name;
+ if (n2 == NULL)
+ n2 = "";
+
+ /* We need to sort in reverse order by priority. When two
+ constructors have the same priority, we should maintain their
+ current relative position. */
+
+ prio1 = ctor_prio (n1);
+ prio2 = ctor_prio (n2);
+
+ /* We sort in reverse order because that is what g++ expects. */
+ if (prio1 < prio2)
+ return 1;
+ else if (prio1 > prio2)
+ return -1;
+
+ /* Force a stable sort. */
+
+ if (pe1 < pe2)
+ return -1;
+ else if (pe1 > pe2)
+ return 1;
+ else
+ return 0;
+}
+
/* This function is called after the first phase of the link and
before the second phase. At this point all set information has
been gathered. We now put the statements to build the sets
@@ -129,6 +215,42 @@ ldctor_build_sets ()
return;
called = true;
+ if (constructors_sorted)
+ {
+ for (p = sets; p != NULL; p = p->next)
+ {
+ int c, i;
+ struct set_element *e;
+ struct set_element **array;
+
+ if (p->elements == NULL)
+ continue;
+
+ c = 0;
+ for (e = p->elements; e != NULL; e = e->next)
+ ++c;
+
+ array = (struct set_element **) xmalloc (c * sizeof *array);
+
+ i = 0;
+ for (e = p->elements; e != NULL; e = e->next)
+ {
+ array[i] = e;
+ ++i;
+ }
+
+ qsort (array, c, sizeof *array, ctor_cmp);
+
+ e = array[0];
+ p->elements = e;
+ for (i = 0; i < c - 1; i++)
+ array[i]->next = array[i + 1];
+ array[i]->next = NULL;
+
+ free (array);
+ }
+ }
+
old = stat_ptr;
stat_ptr = &constructor_list;
@@ -163,7 +285,7 @@ ldctor_build_sets ()
{
if (link_info.relocateable)
{
- einfo ("%P%X: %s does not support reloc %s for set %s\n",
+ einfo (_("%P%X: %s does not support reloc %s for set %s\n"),
bfd_get_target (output_bfd),
bfd_get_reloc_code_name (p->reloc),
p->h->root.string);
@@ -172,11 +294,12 @@ ldctor_build_sets ()
/* If this is not a relocateable link, all we need is the
size, which we can get from the input BFD. */
- howto = bfd_reloc_type_lookup (p->elements->section->owner,
- p->reloc);
+ if (p->elements->section->owner != NULL)
+ howto = bfd_reloc_type_lookup (p->elements->section->owner,
+ p->reloc);
if (howto == NULL)
{
- einfo ("%P%X: %s does not support reloc %s for set %s\n",
+ einfo (_("%P%X: %s does not support reloc %s for set %s\n"),
bfd_get_target (p->elements->section->owner),
bfd_get_reloc_code_name (p->reloc),
p->h->root.string);
@@ -197,7 +320,7 @@ ldctor_build_sets ()
size = QUAD;
break;
default:
- einfo ("%P%X: Unsupported size %d for set %s\n",
+ einfo (_("%P%X: Unsupported size %d for set %s\n"),
bfd_get_reloc_size (howto), p->h->root.string);
size = LONG;
break;
@@ -218,7 +341,7 @@ ldctor_build_sets ()
if (! header_printed)
{
- minfo ("\nSet Symbol\n\n");
+ minfo (_("\nSet Symbol\n\n"));
header_printed = true;
}
@@ -242,6 +365,10 @@ ldctor_build_sets ()
minfo ("%G\n", e->section->owner, e->section, e->value);
}
+ /* Need SEC_KEEP for --gc-sections */
+ if (! bfd_is_abs_section (e->section))
+ e->section->flags |= SEC_KEEP;
+
if (link_info.relocateable)
lang_add_reloc (p->reloc, howto, e->section, e->name,
exp_intop (e->value));
OpenPOWER on IntegriCloud