mirror of
https://sourceware.org/git/binutils-gdb.git
synced 2024-12-21 04:42:53 +08:00
47275900ad
We have an issue in the MIPS backend, with the handling of undefined hidden and internal weak symbols. References to such symbols are supposed to resolve to 0 according to the ELF gABI[1]: "Unresolved weak symbols have a zero value." and the 64-bit MIPS psABI[2]: "If a symbol with one of these [hidden or internal] attributes has no definition within the executable/DSO being linked, then it must be resolved to allocated space if common, resolved to zero if weak, or an error reported otherwise." however if a GOT relocation is used, then a local GOT entry is created and used to satisfy the reference. Such an entry is then (in DSO and PIE binaries) subject to the usual load-time relocation, which means a non-zero value will be returned if the base address is non-zero. This will defeat the usual run-time sequence like: void a (void) __attribute__ ((visibility ("hidden"), weak)); void x (void) { if (a) a (); } This can be reproduced with this simple code: $ cat libtest.c extern int a __attribute__ ((visibility ("hidden"), weak)); int * x (void) { return &a; } $ cat test.c int *x (void); int main (void) { printf ("a: %p\n", x ()); return 0; } $ gcc -shared -fPIC -o libtest.so libtest.c $ gcc -o test test.c -Wl,-rpath,$(pwd) libtest.so $ ./test a: 0x77184000 $ The usual approach targets take is making all the steps required to assign a GOT entry for the symbol referred, and then leave its contents at zero with no dynamic relocation attached, therefore ensuring that the value does not change at load time. However this is not going to work with the implicitly relocated GOT the MIPS psABI specifies[3]: "The dynamic linker relocates the global offset table by first adding the difference between the base where the shared object is loaded and the value of the dynamic tag DT_MIPS_BASE_ADDRESS to all local global offset table entries." and we cannot therefore use the local GOT part. And we cannot offhand use the global part either, as the symbol would then have to be exported and possibly wrongly preempt symbols in other modules involved in the dynamic load, because as per the ELF gABI[1] we are not allowed to enter a hidden or internal symbol into the dynamic symbol table (and then use its associated GOT entry): "A hidden symbol contained in a relocatable object must be either removed or converted to STB_LOCAL binding by the link-editor when the relocatable object is included in an executable file or shared object." and: "An internal symbol contained in a relocatable object must be either removed or converted to STB_LOCAL binding by the link-editor when the relocatable object is included in an executable file or shared object." So we have to choose something else. Our choice is further limited by the need for the reference associated with the GOT relocation to stay within the signed 16-bit limit from the GOT pointer base register, while being compliant with the ELF gABI and the MIPS psABI. However as Alan Modra has observed[4] one possibility is to edit (relax) the code such that the GOT reference is removed altogether. Based on these observations then modify MIPS BFD linker backend code to: 1. Interpret code associated with GOT relocations and relax the usual LW or LD instructions into a corresponding immediate load operation that places the value of 0 in the intended register, while leaving the GOT entry allocated and initialized as usually. 2. Leave any other instructions associated with GOT relocations in place and instead redirect the reference to a global GOT entry associated with a special `__gnu_absolute_zero' symbol created for this purpose, whose value is 0, SHN_ABS section marks it absolute, binding is global and export class protected, ensuring that the locally provided value is always used at load time, and that the value is not relocated by the dynamic loader. 3. Adjust any high-part GOT relocation used, typically associated with a LUI instruction, accordingly, so that run-time consistency is maintained, either by resolving to the original entry if the instruction associated with the corresponding low-part GOT relocation has been relaxed to an immediate load (in which case the value loaded with LUI will be overwritten), or by also redirecting the reference to `__gnu_absolute_zero' to complete the GOT access sequence if that symbol has been used. 4. Add a target `elf_backend_hide_symbol' hook, for the three MIPS ABIs, which prevents the `__gnu_absolute_zero' symbol from being forced local, to ensure that the redirection works and the symbol remains global/protected with existing linker scripts unchanged. 5. Observing the issue with handling SHN_ABS symbols in the GNU dynamic loader, covered by glibc PR 19818, set the EI_ABIVERSION field in the ELF file header produced to 4 (ABI_ABSOLUTE) if `__gnu_absolute_zero' symbol has been produced and the target configured indicates the GNU operating system, so that broken versions of the GNU dynamic loader gracefully reject the file in loading rather than going astray. Keep EI_ABIVERSION at the original value for other operating systems or if no `__gnu_absolute_zero' symbol has been made. The name of the special `__gnu_absolute_zero' has no meaning other than how a human reader can interpret it, as it is ignored in dynamic loading in the handling of the scenarios concerned. This is because the symbol resolves locally, and it's only the symbol's attributes that matter so that the associated GOT entry remains unchanged at load time. Therefore the name is somewhat arbitrary, observing however the need to use the name space reserved for the system so that it does not conflict with a possible user symbol, and hence the leading underscore, and also the `gnu' infix to denote a GNU feature. Other implementations wishing to address the problem in a similar way may choose a different name and have the solution still work, possibly with a mixture of modules used in a dynamic having symbols of different names provided, which will however not interact with each other due to the protected export class. The symbol can be referred explicitly, however the name is an internal implementation detail rather than a part of the ABI, and therefore no specific semantics is guaranteed. One limitation of this change is that if `__gnu_absolute_zero' has been already defined, then we do not wipe the old definition and all kinds of odd behavior can result. This is however like with other symbols we internally define, such as `_GLOBAL_OFFSET_TABLE_' or `__rld_map', and therefore left as a possible future enhancement. As an optimization the relaxation of LW and LD instructions to a load of immediate zero is always made, even SVR4 PIC code for code that will end up in a regular (non-PIE) executable, because there is a cache advantage with the avoidance of a load from the GOT, even if it is otherwise guaranteed to remain zero. It does not reliably happen though, due to a symbol exportation issue affecting executables, covered by PR ld/21805. One existing test case needs to be updated, as it triggers relaxation introduced with this change and consequently linker output does not match expectations anymore. As we want to keep the original issue covered with the test case modify it then to use the LWL instruction in place of LW, and adjust the output expected accordingly. References: [1] "System V Application Binary Interface - DRAFT - 19 October 2010", The SCO Group, Section "Symbol Table", <http://www.sco.com/developers/gabi/2012-12-31/ch4.symtab.html> [2] "64-bit ELF Object File Specification, Draft Version 2.5", MIPS Technologies / Silicon Graphics Computer Systems, Order Number 007-4658-001, Section 2.5 "Symbol Table", p. 22, <http://techpubs.sgi.com/library/manuals/4000/007-4658-001/pdf/007-4658-001.pdf> [3] "SYSTEM V APPLICATION BINARY INTERFACE, MIPS RISC Processor Supplement, 3rd Edition", Section "Global Offset Table", p. 5-10, <http://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf> [4] "Undo dynamic symbol state after regular object sym type mismatch", <https://sourceware.org/ml/binutils/2017-07/msg00265.html> bfd/ PR ld/21375 * elfxx-mips.h (_bfd_mips_elf_hide_symbol): New prototype. (_bfd_mips_elf_linker_flags): Update prototype. * elf32-mips.c (elf_backend_hide_symbol): New macro. * elf64-mips.c (elf_backend_hide_symbol): Likewise. * elfn32-mips.c (elf_backend_hide_symbol): Likewise. * elfxx-mips.c (mips_elf_link_hash_table): Add `use_absolute_zero' and `gnu_target' members. (mips_elf_record_global_got_symbol): Call `_bfd_mips_elf_hide_symbol' rather than `_bfd_elf_link_hash_hide_symbol'. (mips_use_local_got_p): Return FALSE if the symbol is absolute. (mips_elf_obtain_contents): Reorder function. (mips_elf_nullify_got_load): New function. (mips_elf_calculate_relocation): Add `contents' parameter. Nullify GOT loads or if it is not possible, then redirect GOT relocations to the `__gnu_absolute_zero' symbol, for references that are supposed to resolve to zero. (mips_elf_define_absolute_zero): New function. (_bfd_mips_elf_check_relocs): Prepare for arrangements made in `mips_elf_calculate_relocation' for references made via the GOT that are supposed to resolve to zero. (_bfd_mips_elf_hide_symbol): New function. (_bfd_mips_elf_linker_flags): Add the `gnu_target' parameter, set the `gnu_target' member of the MIPS hash table. (MIPS_LIBC_ABI_ABSOLUTE): New enumeration constant. (_bfd_mips_post_process_headers): Use it. ld/ PR ld/21375 * emultempl/mipself.em: Set `gnu_target' according to ${target}. (mips_create_output_section_statements): Update call to `_bfd_mips_elf_linker_flags'. * testsuite/ld-mips-elf/pr21334.s: Use LWL rather than LW. * testsuite/ld-mips-elf/pr21334.dd: Update accordingly.
321 lines
8.6 KiB
Plaintext
321 lines
8.6 KiB
Plaintext
# This shell script emits a C file. -*- C -*-
|
|
# Copyright (C) 2004-2018 Free Software Foundation, Inc.
|
|
#
|
|
# This file is part of the GNU Binutils.
|
|
#
|
|
# 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 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program; if not, write to the Free Software
|
|
# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
|
|
# MA 02110-1301, USA.
|
|
|
|
case ${target} in
|
|
*-*-*gnu*)
|
|
gnu_target=TRUE
|
|
;;
|
|
*)
|
|
gnu_target=FALSE
|
|
;;
|
|
esac
|
|
|
|
fragment <<EOF
|
|
|
|
#include "ldctor.h"
|
|
#include "elf/mips.h"
|
|
#include "elfxx-mips.h"
|
|
|
|
#define is_mips_elf(bfd) \
|
|
(bfd_get_flavour (bfd) == bfd_target_elf_flavour \
|
|
&& elf_tdata (bfd) != NULL \
|
|
&& elf_object_id (bfd) == MIPS_ELF_DATA)
|
|
|
|
/* Fake input file for stubs. */
|
|
static lang_input_statement_type *stub_file;
|
|
static bfd *stub_bfd;
|
|
|
|
static bfd_boolean insn32;
|
|
static bfd_boolean ignore_branch_isa;
|
|
|
|
static void
|
|
mips_after_parse (void)
|
|
{
|
|
/* .gnu.hash and the MIPS ABI require .dynsym to be sorted in different
|
|
ways. .gnu.hash needs symbols to be grouped by hash code whereas the
|
|
MIPS ABI requires a mapping between the GOT and the symbol table. */
|
|
if (link_info.emit_gnu_hash)
|
|
{
|
|
einfo (_("%X%P: .gnu.hash is incompatible with the MIPS ABI\n"));
|
|
link_info.emit_hash = TRUE;
|
|
link_info.emit_gnu_hash = FALSE;
|
|
}
|
|
gld${EMULATION_NAME}_after_parse ();
|
|
}
|
|
|
|
struct hook_stub_info
|
|
{
|
|
lang_statement_list_type add;
|
|
asection *input_section;
|
|
};
|
|
|
|
/* Traverse the linker tree to find the spot where the stub goes. */
|
|
|
|
static bfd_boolean
|
|
hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
|
|
{
|
|
lang_statement_union_type *l;
|
|
bfd_boolean ret;
|
|
|
|
for (; (l = *lp) != NULL; lp = &l->header.next)
|
|
{
|
|
switch (l->header.type)
|
|
{
|
|
case lang_constructors_statement_enum:
|
|
ret = hook_in_stub (info, &constructor_list.head);
|
|
if (ret)
|
|
return ret;
|
|
break;
|
|
|
|
case lang_output_section_statement_enum:
|
|
ret = hook_in_stub (info,
|
|
&l->output_section_statement.children.head);
|
|
if (ret)
|
|
return ret;
|
|
break;
|
|
|
|
case lang_wild_statement_enum:
|
|
ret = hook_in_stub (info, &l->wild_statement.children.head);
|
|
if (ret)
|
|
return ret;
|
|
break;
|
|
|
|
case lang_group_statement_enum:
|
|
ret = hook_in_stub (info, &l->group_statement.children.head);
|
|
if (ret)
|
|
return ret;
|
|
break;
|
|
|
|
case lang_input_section_enum:
|
|
if (info->input_section == NULL
|
|
|| l->input_section.section == info->input_section)
|
|
{
|
|
/* We've found our section. Insert the stub immediately
|
|
before its associated input section. */
|
|
*lp = info->add.head;
|
|
*(info->add.tail) = l;
|
|
return TRUE;
|
|
}
|
|
break;
|
|
|
|
case lang_data_statement_enum:
|
|
case lang_reloc_statement_enum:
|
|
case lang_object_symbols_statement_enum:
|
|
case lang_output_statement_enum:
|
|
case lang_target_statement_enum:
|
|
case lang_input_statement_enum:
|
|
case lang_assignment_statement_enum:
|
|
case lang_padding_statement_enum:
|
|
case lang_address_statement_enum:
|
|
case lang_fill_statement_enum:
|
|
break;
|
|
|
|
default:
|
|
FAIL ();
|
|
break;
|
|
}
|
|
}
|
|
return FALSE;
|
|
}
|
|
|
|
/* Create a new stub section called STUB_SEC_NAME and arrange for it to
|
|
be linked in OUTPUT_SECTION. The section should go at the beginning of
|
|
OUTPUT_SECTION if INPUT_SECTION is null, otherwise it must go immediately
|
|
before INPUT_SECTION. */
|
|
|
|
static asection *
|
|
mips_add_stub_section (const char *stub_sec_name, asection *input_section,
|
|
asection *output_section)
|
|
{
|
|
asection *stub_sec;
|
|
flagword flags;
|
|
lang_output_section_statement_type *os;
|
|
struct hook_stub_info info;
|
|
|
|
/* PR 12845: If the input section has been garbage collected it will
|
|
not have its output section set to *ABS*. */
|
|
if (bfd_is_abs_section (output_section))
|
|
return NULL;
|
|
|
|
/* Create the stub file, if we haven't already. */
|
|
if (stub_file == NULL)
|
|
{
|
|
stub_file = lang_add_input_file ("linker stubs",
|
|
lang_input_file_is_fake_enum,
|
|
NULL);
|
|
stub_bfd = bfd_create ("linker stubs", link_info.output_bfd);
|
|
if (stub_bfd == NULL
|
|
|| !bfd_set_arch_mach (stub_bfd,
|
|
bfd_get_arch (link_info.output_bfd),
|
|
bfd_get_mach (link_info.output_bfd)))
|
|
{
|
|
einfo (_("%F%P: can not create BFD: %E\n"));
|
|
return NULL;
|
|
}
|
|
stub_bfd->flags |= BFD_LINKER_CREATED;
|
|
stub_file->the_bfd = stub_bfd;
|
|
ldlang_add_file (stub_file);
|
|
}
|
|
|
|
/* Create the section. */
|
|
stub_sec = bfd_make_section_anyway (stub_bfd, stub_sec_name);
|
|
if (stub_sec == NULL)
|
|
goto err_ret;
|
|
|
|
/* Set the flags. */
|
|
flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
|
|
| SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_KEEP);
|
|
if (!bfd_set_section_flags (stub_bfd, stub_sec, flags))
|
|
goto err_ret;
|
|
|
|
os = lang_output_section_get (output_section);
|
|
|
|
/* Initialize a statement list that contains only the new statement. */
|
|
lang_list_init (&info.add);
|
|
lang_add_section (&info.add, stub_sec, NULL, os);
|
|
if (info.add.head == NULL)
|
|
goto err_ret;
|
|
|
|
/* Insert the new statement in the appropriate place. */
|
|
info.input_section = input_section;
|
|
if (hook_in_stub (&info, &os->children.head))
|
|
return stub_sec;
|
|
|
|
err_ret:
|
|
einfo (_("%X%P: can not make stub section: %E\n"));
|
|
return NULL;
|
|
}
|
|
|
|
/* This is called before the input files are opened. */
|
|
|
|
static void
|
|
mips_create_output_section_statements (void)
|
|
{
|
|
struct elf_link_hash_table *htab;
|
|
|
|
htab = elf_hash_table (&link_info);
|
|
if (is_elf_hash_table (htab) && is_mips_elf (link_info.output_bfd))
|
|
_bfd_mips_elf_linker_flags (&link_info, insn32, ignore_branch_isa,
|
|
${gnu_target});
|
|
|
|
if (is_mips_elf (link_info.output_bfd))
|
|
_bfd_mips_elf_init_stubs (&link_info, mips_add_stub_section);
|
|
}
|
|
|
|
/* This is called after we have merged the private data of the input bfds. */
|
|
|
|
static void
|
|
mips_before_allocation (void)
|
|
{
|
|
if (is_mips_elf (link_info.output_bfd))
|
|
{
|
|
flagword flags;
|
|
|
|
flags = elf_elfheader (link_info.output_bfd)->e_flags;
|
|
if (!bfd_link_pic (&link_info)
|
|
&& !link_info.nocopyreloc
|
|
&& (flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) == EF_MIPS_CPIC)
|
|
_bfd_mips_elf_use_plts_and_copy_relocs (&link_info);
|
|
}
|
|
|
|
gld${EMULATION_NAME}_before_allocation ();
|
|
}
|
|
|
|
/* Avoid processing the fake stub_file in vercheck, stat_needed and
|
|
check_needed routines. */
|
|
|
|
static void (*real_func) (lang_input_statement_type *);
|
|
|
|
static void mips_for_each_input_file_wrapper (lang_input_statement_type *l)
|
|
{
|
|
if (l != stub_file)
|
|
(*real_func) (l);
|
|
}
|
|
|
|
static void
|
|
mips_lang_for_each_input_file (void (*func) (lang_input_statement_type *))
|
|
{
|
|
real_func = func;
|
|
lang_for_each_input_file (&mips_for_each_input_file_wrapper);
|
|
}
|
|
|
|
#define lang_for_each_input_file mips_lang_for_each_input_file
|
|
|
|
EOF
|
|
|
|
# Define some shell vars to insert bits of code into the standard elf
|
|
# parse_args and list_options functions.
|
|
#
|
|
PARSE_AND_LIST_PROLOGUE='
|
|
enum
|
|
{
|
|
OPTION_INSN32 = 301,
|
|
OPTION_NO_INSN32,
|
|
OPTION_IGNORE_BRANCH_ISA,
|
|
OPTION_NO_IGNORE_BRANCH_ISA
|
|
};
|
|
'
|
|
|
|
PARSE_AND_LIST_LONGOPTS='
|
|
{ "insn32", no_argument, NULL, OPTION_INSN32 },
|
|
{ "no-insn32", no_argument, NULL, OPTION_NO_INSN32 },
|
|
{ "ignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA },
|
|
{ "no-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA },
|
|
'
|
|
|
|
PARSE_AND_LIST_OPTIONS='
|
|
fprintf (file, _("\
|
|
--insn32 Only generate 32-bit microMIPS instructions\n"
|
|
));
|
|
fprintf (file, _("\
|
|
--no-insn32 Generate all microMIPS instructions\n"
|
|
));
|
|
fprintf (file, _("\
|
|
--ignore-branch-isa Accept invalid branch relocations requiring\n\
|
|
an ISA mode switch\n"
|
|
));
|
|
fprintf (file, _("\
|
|
--no-ignore-branch-isa Reject invalid branch relocations requiring\n\
|
|
an ISA mode switch\n"
|
|
));
|
|
'
|
|
|
|
PARSE_AND_LIST_ARGS_CASES='
|
|
case OPTION_INSN32:
|
|
insn32 = TRUE;
|
|
break;
|
|
|
|
case OPTION_NO_INSN32:
|
|
insn32 = FALSE;
|
|
break;
|
|
|
|
case OPTION_IGNORE_BRANCH_ISA:
|
|
ignore_branch_isa = TRUE;
|
|
break;
|
|
|
|
case OPTION_NO_IGNORE_BRANCH_ISA:
|
|
ignore_branch_isa = FALSE;
|
|
break;
|
|
'
|
|
|
|
LDEMUL_AFTER_PARSE=mips_after_parse
|
|
LDEMUL_BEFORE_ALLOCATION=mips_before_allocation
|
|
LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=mips_create_output_section_statements
|