Files
MSYS2-packages/binutils/0003-objcopy-weak-nt-externals2local.patch
Petros Angelatos 85f0605c8b binutils: re-enable weak NT externals patches
Signed-off-by: Petros Angelatos <petrosagg@gmail.com>
2017-01-10 05:04:42 -08:00

170 lines
5.5 KiB
Diff

From bc03e2e40e51474f2244f5909f3e8f5a4cf60ab0 Mon Sep 17 00:00:00 2001
From: Octavian Purdila <octavian dot purdila at intel dot com>
Date: Mon, 26 Oct 2015 15:57:35 +0200
Subject: [PATCH 3/3] Convert NT weak externals to locals
This patch allows converting a weak external symbol to a local symbol
by setting up its section and value to the ones of the alternate
symbol. This is useful when we want to make only a few selected
symbols visible (e.g. objcopy -G).
The conversion is triggered when an NT weak external symbol has been
marked with BSF_LOCAL.
bfd/
2015-10-22 Octavian Purdila <octavian.purdila@intel.com>
* coffgen.c: add coff_nt_weak_to_local() that implements weak
external to local symbol conversion
* coffcode.h (coff_write_object_contents): call
coff_nt_weak_to_local()
* libcoff.h: add coff_nt_weak_to_local() declaration
binutils/
2015-10-26 Octavian Purdila <octavian.purdila@intel.com>
* objcopy.c (filter_symbols): allow converting undefined weak
symbols to local symbols for PE objects (as PE weak externals are
implemented as undefined weak symbols)
---
bfd/ChangeLog | 8 ++++++++
bfd/coffcode.h | 1 +
bfd/coffgen.c | 43 +++++++++++++++++++++++++++++++++++++++++++
bfd/libcoff.h | 2 ++
binutils/ChangeLog | 6 ++++++
binutils/objcopy.c | 5 ++++-
6 files changed, 64 insertions(+), 1 deletion(-)
diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index 7c78023d7b..a4ba816cb9 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,5 +1,13 @@
2015-10-26 octavian purdila <octavian.purdila@intel.com>
+ * coffgen.c: add coff_nt_weak_to_local() that implements weak
+ external to local symbol conversion
+ * coffcode.h (coff_write_object_contents): call
+ coff_nt_weak_to_local()
+ * libcoff.h: add coff_nt_weak_to_local() declaration
+
+2015-10-26 Octavian Purdila <octavian.purdila@intel.com>
+
* cofflink.c (_bfd_coff_link_input_bfd): relocate AUX entries for
weak externals when generating relocatable objects
diff --git a/bfd/coffcode.h b/bfd/coffcode.h
index 2b1c3d053f..23d932018d 100644
--- a/bfd/coffcode.h
+++ b/bfd/coffcode.h
@@ -4198,6 +4198,7 @@ coff_write_object_contents (bfd * abfd)
{
int firstundef;
+ coff_nt_weak_to_local(abfd);
if (!coff_renumber_symbols (abfd, &firstundef))
return FALSE;
coff_mangle_symbols (abfd);
diff --git a/bfd/coffgen.c b/bfd/coffgen.c
index d071d8a36f..cd2a502963 100644
--- a/bfd/coffgen.c
+++ b/bfd/coffgen.c
@@ -801,6 +801,49 @@ coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
return TRUE;
}
+/* Transform weak externals to local symbols if requested (e.g. objcopy). */
+void coff_nt_weak_to_local(bfd *abfd)
+{
+ unsigned int symbol_count = bfd_get_symcount (abfd);
+ asymbol **symbol_ptr_ptr = abfd->outsymbols;
+ unsigned int symbol_index;
+
+ for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
+ {
+ asymbol *symbol = symbol_ptr_ptr[symbol_index];
+ coff_symbol_type *coff_symbol_ptr;
+ combined_entry_type *native;
+ struct internal_syment *sym;
+
+ coff_symbol_ptr = coff_symbol_from (abfd, symbol);
+ if (!coff_symbol_ptr)
+ continue;
+
+ native = coff_symbol_ptr->native;
+ if (!native)
+ continue;
+
+ sym = &native->u.syment;
+
+ if ((symbol->flags & BSF_LOCAL) && sym->n_sclass == C_NT_WEAK
+ && sym->n_numaux == 1) {
+ union internal_auxent *aux = &native[1].u.auxent;
+ struct internal_syment *wsym = &aux->x_sym.x_tagndx.p->u.syment;
+
+ if (!wsym) {
+ symbol->flags &= BSF_LOCAL;
+ continue;
+ }
+
+ symbol->flags &= ~BSF_WEAK;
+ symbol->value = wsym->n_value;
+ symbol->section = coff_section_from_bfd_index (abfd, wsym->n_scnum);
+ symbol->section->output_section = symbol->section;
+ sym->n_numaux = 0;
+ }
+ }
+}
+
/* Run thorough the symbol table again, and fix it so that all
pointers to entries are changed to the entries' index in the output
symbol table. */
diff --git a/bfd/libcoff.h b/bfd/libcoff.h
index 947998570b..08cf93ac7d 100644
--- a/bfd/libcoff.h
+++ b/bfd/libcoff.h
@@ -326,6 +326,8 @@ extern int coff_count_linenumbers
(bfd *);
extern struct coff_symbol_struct *coff_symbol_from
(bfd *, asymbol *);
+extern void coff_nt_weak_to_local
+ (bfd *);
extern bfd_boolean coff_renumber_symbols
(bfd *, int *);
extern void coff_mangle_symbols
diff --git a/binutils/ChangeLog b/binutils/ChangeLog
index a23a6c3713..07a824e260 100644
--- a/binutils/ChangeLog
+++ b/binutils/ChangeLog
@@ -1,3 +1,9 @@
+2015-10-26 Octavian Purdila <octavian.purdila@intel.com>
+
+ * objcopy.c (filter_symbols): allow converting undefined weak
+ symbols to local symbols for PE objects (as PE weak externals are
+ implemented as undefined weak symbols)
+
2015-07-21 Tristan Gingold <gingold@adacore.com>
* configure: Regenerate.
diff --git a/binutils/objcopy.c b/binutils/objcopy.c
index da429f54ea..d064119da0 100644
--- a/binutils/objcopy.c
+++ b/binutils/objcopy.c
@@ -1336,7 +1336,10 @@ filter_symbols (bfd *abfd, bfd *obfd, asymbol **osyms,
sym->flags |= BSF_WEAK;
}
- if (!undefined
+ /* We want to check even undefined weak symbols in the case of PE,
+ * since weak externals are classified as undefined. */
+ if ((!undefined || (bfd_get_flavour (abfd) == bfd_target_coff_flavour &&
+ CONST_STRNEQ ((abfd)->xvec->name, "pe")))
&& (flags & (BSF_GLOBAL | BSF_WEAK))
&& (is_specified_symbol (name, localize_specific_htab)
|| (htab_elements (keepglobal_specific_htab) != 0
--
2.11.0