From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2124) id F0CE0386D60C; Wed, 26 Jun 2024 12:18:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F0CE0386D60C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1719404289; bh=l1ajd5Y4PBJXkI4CXt6kpOE3wgA3j4jLAVWtD2u4Zlc=; h=From:To:Subject:Date:From; b=G0yAfRF9IzauU/Zb1cJmMdViFYWJzphu6erLHaewpITSqKemtKYu1XZScA9NdzYB/ FcchHz/ealFqa9IasAtBbR3qbzqm5nFhjtrmAyeK4fo9RzGa7hWTir3QmJ4PcVwL9r VXQ5wYvvP8RTb2Dr4OhzYhYCI6/tY35ptHLgA1Vw= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Nick Clifton To: binutils-cvs@sourceware.org Subject: [binutils-gdb] Revert: 35fd2ddeb1d90f1750401cfb6d01fe055656b88d X-Act-Checkin: binutils-gdb X-Git-Author: Nick Clifton X-Git-Refname: refs/heads/master X-Git-Oldrev: cbccccfdf19a388abb10579aea42c8b35c25c933 X-Git-Newrev: 057a2b4c4b57ef495dd32c9087e4af28bda41737 Message-Id: <20240626121809.F0CE0386D60C@sourceware.org> Date: Wed, 26 Jun 2024 12:18:09 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D057a2b4c4b57= ef495dd32c9087e4af28bda41737 commit 057a2b4c4b57ef495dd32c9087e4af28bda41737 Author: Nick Clifton Date: Wed Jun 26 13:17:25 2024 +0100 Revert: 35fd2ddeb1d90f1750401cfb6d01fe055656b88d =20 PR 20814 Diff: --- binutils/dlltool.c | 21 ++++-- binutils/doc/binutils.texi | 2 +- ld/ld.texi | 19 ++++++ ld/ldelf.c | 2 +- ld/ldfile.c | 69 ++++++++++++++------ ld/ldfile.h | 21 ++++-- ld/ldgram.y | 2 +- ld/ldlang.h | 8 ++- ld/lexsup.c | 6 +- ld/plugin.c | 12 +++- ld/testsuite/ld-plugin/libdep-a.c | 7 ++ ld/testsuite/ld-plugin/libdep-b.c | 5 ++ ld/testsuite/ld-plugin/libdep-main.c | 7 ++ ld/testsuite/ld-plugin/libdep.exp | 123 +++++++++++++++++++++++++++++++= ++++ 14 files changed, 265 insertions(+), 39 deletions(-) diff --git a/binutils/dlltool.c b/binutils/dlltool.c index 7a3d20f0af9..2c37ad3c6a3 100644 --- a/binutils/dlltool.c +++ b/binutils/dlltool.c @@ -784,9 +784,10 @@ typedef struct export int ordinal; int constant; int noname; /* Don't put name in image file. */ - int private; /* Don't put reference in import lib. */ + int private; /* Don't put reference in import lib. */ int data; - int forward; /* Number of forward label, 0 means no forward. */ + int hint; + int forward; /* Number of forward label, 0 means no forward. */ struct export *next; } export_type; @@ -2697,8 +2698,10 @@ make_one_lib_file (export_type *exp, int i, int dela= y) case IDATA6: if (!exp->noname) { - int idx =3D exp->ordinal; - + /* This used to add 1 to exp->hint. I don't know + why it did that, and it does not match what I see + in programs compiled with the MS tools. */ + int idx =3D exp->hint; if (exp->its_name) si->size =3D strlen (exp->its_name) + 3; else @@ -3058,6 +3061,7 @@ gen_lib_file (int delay) alias_exp.noname =3D exp->noname; alias_exp.private =3D exp->private; alias_exp.data =3D exp->data; + alias_exp.hint =3D exp->hint; alias_exp.forward =3D exp->forward; alias_exp.next =3D exp->next; n =3D make_one_lib_file (&alias_exp, i + PREFIX_ALIAS_BASE, delay); @@ -3711,8 +3715,10 @@ mangle_defs (void) { /* First work out the minimum ordinal chosen. */ export_type *exp; - export_type **d_export_vec =3D xmalloc (sizeof (export_type *) * d_nfunc= s); + int i; + int hint =3D 0; + export_type **d_export_vec =3D xmalloc (sizeof (export_type *) * d_nfunc= s); =20 inform (_("Processing definitions")); =20 @@ -3741,6 +3747,11 @@ mangle_defs (void) =20 qsort (d_exports_lexically, i, sizeof (export_type *), nfunc); =20 + /* Fill exp entries with their hint values. */ + for (i =3D 0; i < d_nfuncs; i++) + if (!d_exports_lexically[i]->noname || show_allnames) + d_exports_lexically[i]->hint =3D hint++; + inform (_("Processed definitions")); } =20 diff --git a/binutils/doc/binutils.texi b/binutils/doc/binutils.texi index c78c9e393f2..2b13a4bfeb1 100644 --- a/binutils/doc/binutils.texi +++ b/binutils/doc/binutils.texi @@ -457,7 +457,7 @@ Specify dependencies of this library. The dependencies= must immediately follow this option character, must use the same syntax as the linker command line, and must be specified within a single argument. I.e., if multiple items are needed, they must be quoted to form a single command -line argument. For example @samp{L "-L/usr/local/lib -lmydep1 -lmydep2"} +line argument. For example @samp{l "-L/usr/local/lib -lmydep1 -lmydep2"} =20 @item N Uses the @var{count} parameter. This is used if there are multiple diff --git a/ld/ld.texi b/ld/ld.texi index 2eafeb7dd39..89e3913317a 100644 --- a/ld/ld.texi +++ b/ld/ld.texi @@ -7614,6 +7614,7 @@ command line option. @xref{Options}. =20 @menu * libdep Plugin:: Static Library Dependencies Plugin +* lto Plugin:: LTO Plugin @end menu =20 @node libdep Plugin @@ -7652,6 +7653,24 @@ the @samp{__.LIBDEP} member of @file{libssl.a} would= contain -L/usr/local/lib -lcrypto @end smallexample =20 +Note any library search directories added in this way are only used to +search for libraries which are also added the same plugin. They are +not used for searching for libraries specified on the linker command +line, linker scripts or other plugins. This does however present a +problem if multiple archives with @samp{__.LIBDEP} entries are present +as they will all be handled by the libdep plugin and thus they will +share library search paths. This could result in a library being +loaded from an unexpected location. + +@node lto Plugin +@section LTO Plugin +Although not shipped with the binutils the LTO Plugin from the GCC +project is nevertheless a plugin that is designed to work with the +linker. The plugin intercepts object files as they are loaded by the +linker and instead sends them to the LTO compiler. When that compiler +finishes the resulting object file(s) are passed back to the linker +for normal processing. + @node Special Sections @chapter Special Sections When linking ELF format object files @command{ld} treats some sections diff --git a/ld/ldelf.c b/ld/ldelf.c index 04045acbf3d..09691609fc0 100644 --- a/ld/ldelf.c +++ b/ld/ldelf.c @@ -1170,7 +1170,7 @@ ldelf_handle_dt_needed (struct elf_link_hash_table *h= tab, { char *filename; =20 - if (search->cmdline) + if (search->source !=3D search_dir_linker_script) continue; filename =3D (char *) xmalloc (strlen (search->name) + len + 2); sprintf (filename, "%s/%s", search->name, l->name); diff --git a/ld/ldfile.c b/ld/ldfile.c index f1107a1b7d7..87be885d31a 100644 --- a/ld/ldfile.c +++ b/ld/ldfile.c @@ -40,12 +40,6 @@ #include "plugin.h" #endif /* BFD_SUPPORTS_PLUGINS */ =20 -bool ldfile_assumed_script =3D false; -const char *ldfile_output_machine_name =3D ""; -unsigned long ldfile_output_machine; -enum bfd_architecture ldfile_output_architecture; -search_dirs_type *search_head; - #ifdef VMS static char *slash =3D ""; #else @@ -62,6 +56,12 @@ typedef struct search_arch struct search_arch *next; } search_arch_type; =20 +bool ldfile_assumed_script =3D false; +const char * ldfile_output_machine_name =3D ""; +unsigned long ldfile_output_machine; +enum bfd_architecture ldfile_output_architecture; +search_dirs_type * search_head; + static search_dirs_type **search_tail_ptr =3D &search_head; static search_arch_type *search_arch_head; static search_arch_type **search_arch_tail_ptr =3D &search_arch_head; @@ -303,21 +303,20 @@ is_sysrooted_pathname (const char *name) } =20 /* Adds NAME to the library search path. - Makes a copy of NAME using xmalloc(). */ + Makes a copy of NAME using xmalloc(). + Returns a pointer to the newly created search_dirs_type structure + or NULL if there was a problem. */ =20 -void -ldfile_add_library_path (const char *name, bool cmdline) +search_dirs_type * +ldfile_add_library_path (const char *name, enum search_dir_source source) { search_dirs_type *new_dirs; =20 - if (!cmdline && config.only_cmd_line_lib_dirs) - return; + if (source !=3D search_dir_cmd_line && config.only_cmd_line_lib_dirs) + return NULL; =20 new_dirs =3D (search_dirs_type *) xmalloc (sizeof (search_dirs_type)); - new_dirs->next =3D NULL; - new_dirs->cmdline =3D cmdline; - *search_tail_ptr =3D new_dirs; - search_tail_ptr =3D &new_dirs->next; + new_dirs->source =3D source; =20 /* If a directory is marked as honoring sysroot, prepend the sysroot path now. */ @@ -327,6 +326,25 @@ ldfile_add_library_path (const char *name, bool cmdlin= e) new_dirs->name =3D concat (ld_sysroot, name + strlen ("$SYSROOT"), (co= nst char *) NULL); else new_dirs->name =3D xstrdup (name); + + /* Accumulate script and command line sourced + search paths at the end of the current list. */ +#if BFD_SUPPORTS_PLUGINS + /* PR 31904: But put plugin sourced paths at the start of the list. */ + if (source =3D=3D search_dir_plugin) + { + new_dirs->next =3D search_head; + search_head =3D new_dirs; + } + else +#endif + { + new_dirs->next =3D NULL; + *search_tail_ptr =3D new_dirs; + search_tail_ptr =3D &new_dirs->next; + } + + return new_dirs; } =20 /* Try to open a BFD for a lang_input_statement. */ @@ -352,9 +370,9 @@ ldfile_try_open_bfd (const char *attempt, return false; } =20 - /* PR 30568: Do not track lto generated temporary object files. */ + /* PR 30568: Do not track plugin generated object files. */ #if BFD_SUPPORTS_PLUGINS - if (!entry->flags.lto_output) + if (entry->plugin !=3D NULL) #endif track_dependency_files (attempt); =20 @@ -365,7 +383,7 @@ ldfile_try_open_bfd (const char *attempt, entry->the_bfd->is_linker_input =3D 1; =20 #if BFD_SUPPORTS_PLUGINS - if (entry->flags.lto_output) + if (entry->plugin !=3D NULL) entry->the_bfd->lto_output =3D 1; #endif =20 @@ -576,6 +594,14 @@ ldfile_open_file_search (const char *arch, { char *string; =20 +#if BFD_SUPPORTS_PLUGINS + /* PR 31904: Only check a plugin sourced search + directory if the file is from the same plugin. */ + if (search->source =3D=3D search_dir_plugin + && entry->plugin !=3D search->plugin) + continue; +#endif + if (entry->flags.dynamic && !bfd_link_relocatable (&link_info)) { if (ldemul_open_dynamic_archive (arch, search, entry)) @@ -844,7 +870,7 @@ ldfile_find_command_file (const char *name, { search_dirs_type **save_tail_ptr =3D search_tail_ptr; search_tail_ptr =3D &script_search; - ldfile_add_library_path (script_dir, true); + (void) ldfile_add_library_path (script_dir, search_dir_cmd_line); search_tail_ptr =3D save_tail_ptr; } } @@ -858,6 +884,11 @@ ldfile_find_command_file (const char *name, search !=3D NULL; search =3D search->next) { +#if BFD_SUPPORTS_PLUGINS + /* Do not search for linker commands in plugin sourced search direct= ories. */ + if (search->source =3D=3D search_dir_plugin) + continue; +#endif path =3D concat (search->name, slash, name, (const char *) NULL); result =3D try_open (path, sysrooted); free (path); diff --git a/ld/ldfile.h b/ld/ldfile.h index f79abf2310d..08826f65f04 100644 --- a/ld/ldfile.h +++ b/ld/ldfile.h @@ -26,6 +26,15 @@ extern unsigned long ldfile_output_machine; extern enum bfd_architecture ldfile_output_architecture; extern const char *ldfile_output_machine_name; =20 +enum search_dir_source +{ + search_dir_cmd_line, +#if BFD_SUPPORTS_PLUGINS + search_dir_plugin, +#endif + search_dir_linker_script +}; + /* Structure used to hold the list of directories to search for libraries. */ =20 @@ -35,8 +44,12 @@ typedef struct search_dirs struct search_dirs *next; /* Name of directory. */ const char *name; - /* TRUE if this is from the command line. */ - bool cmdline; + /* Where the search path came from. */ + enum search_dir_source source; +#if BFD_SUPPORTS_PLUGINS + /* For search dirs added by plugins, the plugin that added them. */ + void * plugin; +#endif } search_dirs_type; =20 enum script_open_style @@ -59,8 +72,8 @@ extern search_dirs_type *search_head; =20 extern void ldfile_add_arch (const char *); -extern void ldfile_add_library_path - (const char *, bool cmdline); +extern search_dirs_type * ldfile_add_library_path + (const char *, enum search_dir_source); extern void ldfile_open_command_file (const char *name); extern void ldfile_open_script_file diff --git a/ld/ldgram.y b/ld/ldgram.y index 07c19ba8692..7c04025f526 100644 --- a/ld/ldgram.y +++ b/ld/ldgram.y @@ -321,7 +321,7 @@ ifile_p1: | TARGET_K '(' NAME ')' { lang_add_target($3); } | SEARCH_DIR '(' filename ')' - { ldfile_add_library_path ($3, false); } + { ldfile_add_library_path ($3, search_dir_linker_script); } | OUTPUT '(' filename ')' { lang_add_output($3, 1); } | OUTPUT_FORMAT '(' NAME ')' diff --git a/ld/ldlang.h b/ld/ldlang.h index 4c1bb002f8e..f36e04c586a 100644 --- a/ld/ldlang.h +++ b/ld/ldlang.h @@ -293,9 +293,6 @@ struct lang_input_statement_flags =20 /* Set if the file was claimed from an archive. */ unsigned int claim_archive : 1; - - /* Set if added by the lto plugin add_input_file callback. */ - unsigned int lto_output : 1; #endif /* BFD_SUPPORTS_PLUGINS */ =20 /* Head of list of pushed flags. */ @@ -332,6 +329,11 @@ typedef struct lang_input_statement_struct const char *target; =20 struct lang_input_statement_flags flags; + +#if BFD_SUPPORTS_PLUGINS + /* If non-NULL the plugin that added this file. */ + void * plugin; +#endif =20 } lang_input_statement_type; =20 typedef struct diff --git a/ld/lexsup.c b/ld/lexsup.c index 96fb73159bf..4aa0124ce2f 100644 --- a/ld/lexsup.c +++ b/ld/lexsup.c @@ -1052,7 +1052,8 @@ parse_args (unsigned argc, char **argv) xexit (0); break; case 'L': - ldfile_add_library_path (optarg, true); + /* FIXME: Check the return value ? */ + (void) ldfile_add_library_path (optarg, search_dir_cmd_line); break; case 'l': lang_add_input_file (optarg, lang_input_file_is_l_enum, NULL); @@ -2086,7 +2087,8 @@ set_default_dirlist (char *dirlist_ptr) if (p !=3D NULL) *p =3D '\0'; if (*dirlist_ptr !=3D '\0') - ldfile_add_library_path (dirlist_ptr, true); + /* FIXME: Check the return value ? */ + (void) ldfile_add_library_path (dirlist_ptr, search_dir_cmd_line); if (p =3D=3D NULL) break; dirlist_ptr =3D p + 1; diff --git a/ld/plugin.c b/ld/plugin.c index 13d29d6b09f..8107841407a 100644 --- a/ld/plugin.c +++ b/ld/plugin.c @@ -928,7 +928,7 @@ add_input_file (const char *pathname) NULL); if (!is) return LDPS_ERR; - is->flags.lto_output =3D 1; + is->plugin =3D called_plugin; return LDPS_OK; } =20 @@ -943,17 +943,23 @@ add_input_library (const char *pathname) NULL); if (!is) return LDPS_ERR; - is->flags.lto_output =3D 1; + is->plugin =3D called_plugin; return LDPS_OK; } =20 /* Set the extra library path to be used by libraries added via add_input_library. */ + static enum ld_plugin_status set_extra_library_path (const char *path) { + search_dirs_type * sdt; + ASSERT (called_plugin); - ldfile_add_library_path (xstrdup (path), false); + sdt =3D ldfile_add_library_path (xstrdup (path), search_dir_plugin); + if (sdt =3D=3D NULL) + return LDPS_ERR; + sdt->plugin =3D called_plugin; return LDPS_OK; } =20 diff --git a/ld/testsuite/ld-plugin/libdep-a.c b/ld/testsuite/ld-plugin/lib= dep-a.c new file mode 100644 index 00000000000..5a01ce8c88a --- /dev/null +++ b/ld/testsuite/ld-plugin/libdep-a.c @@ -0,0 +1,7 @@ +extern int g (void); + +int +f (void) +{ + return g(); +} diff --git a/ld/testsuite/ld-plugin/libdep-b.c b/ld/testsuite/ld-plugin/lib= dep-b.c new file mode 100644 index 00000000000..d2ca7e08694 --- /dev/null +++ b/ld/testsuite/ld-plugin/libdep-b.c @@ -0,0 +1,5 @@ +int +g (void) +{ + return 4; +} diff --git a/ld/testsuite/ld-plugin/libdep-main.c b/ld/testsuite/ld-plugin/= libdep-main.c new file mode 100644 index 00000000000..7563a7d5f88 --- /dev/null +++ b/ld/testsuite/ld-plugin/libdep-main.c @@ -0,0 +1,7 @@ +extern int f (void); + +int +main (void) +{ + return f(); +} diff --git a/ld/testsuite/ld-plugin/libdep.exp b/ld/testsuite/ld-plugin/lib= dep.exp new file mode 100644 index 00000000000..e0afcfc342a --- /dev/null +++ b/ld/testsuite/ld-plugin/libdep.exp @@ -0,0 +1,123 @@ +# Expect script for ld-plugin LIBDEP tests +# Copyright (C) 2024 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. + +# These tests require the plugin API. +if { ![check_plugin_api_available] } { + return +} + +# Check to see if the C compiler works +# FIXME: This is being lazy, we could create assembler equivalents instead. +if { ![check_compiler_available] } { + return +} + +proc run_test { } { + global CC_FOR_TARGET + global srcdir + global subdir + global ar + global ld + global libdep + global base_dir + =20 + set testname "libdep test" + =20 + # Create temporary directories if they do not already exist. + file mkdir -p libdep-a libdep-b + + # Delete old versions of the files we are about to build, just in case. + file delete libdep-a/a.o libdep-a/liba.a libdep-b/b.o libdep-b/libc.a = libdep-main.o + + # Build object files. + if {![ld_compile $CC_FOR_TARGET $srcdir/$subdir/libdep-a.c libdep-a/a.= o]} { + fail "$testname: could not compile source file 1" + return + } + if {![ld_compile $CC_FOR_TARGET $srcdir/$subdir/libdep-b.c libdep-b/b.= o]} { + fail "$testname: could not compile source file 2" + return + } + if {![ld_compile $CC_FOR_TARGET $srcdir/$subdir/libdep-main.c libdep-m= ain.o]} { + fail "$testname: could not compile source file 3" + return + } + + # Create static archives from the objects. + + # For the first archive we add a libdep element that loads the second = library. + if {![ar_simple_create $ar {--record-libdeps "-Llibdep-b -lc"} libdep-= a/liba.a libdep-a/a.o]} { + fail "$testname: could not create archive 1" + return + } + + # For the second archive we choose a name - libc.a - that is likely to= clash + # with a system library. This will help to check that the library loa= ded by + # following the libdep element in the first library is the one that we= expect. + if {![ar_simple_create $ar {} libdep-b/libc.a libdep-b/b.o]} { + fail "$testname: could not create archive 2" + return + } + + # Find the libdep plugin. + # Use libtool to find the path to the plugin rather + # than worrying about run paths or anything like that. + catch "exec $base_dir/libtool --config" lt_config + verbose "Full lt config: $lt_config" 2 + # Look for "objdir=3D.libs" + regexp -line "^objdir=3D.*$" "$lt_config" lt_objdir + verbose "lt_objdir line is '$lt_objdir'" 2 + set lt_objdir [regsub "objdir=3D" "$lt_objdir" ""] + + if { [ file exists "$base_dir/$lt_objdir/libdep.so" ] } { + set libdep "$base_dir/$lt_objdir/libdep.so" + } else { + # FIXME: Check in the system bfd-plugin directory ? + fail "$testname - could not locate libdep plugin" + } + + verbose "Full plugin path: $libdep" 1 + =20 + # Link the main object file with the liba.a library. + # Use the libdep plugin to read the __.LIBDEP element in the liba.a li= brary + # and so bring in the libdep-b.o object file from the libc.a library. + # Failure to locate the libc.a library, or loading the wrong libc.a li= brary + # will result in an unresolved reference error. + set exec_output [run_host_cmd "$ld" "-plugin $libdep -o libdep.exe lib= dep-main.o -L libdep-a -la -e 0"] + set exec_output [prune_warnings $exec_output] + + set expected_output "got deps for library libdep-a/liba.a: -Llibdep-b = -lc" + =20 + if ![string match $expected_output $exec_output] then { + fail "$testname: did not get expected output from the linker" + return + } + + regsub -all "$expected_output" $exec_output "\\1" exec_output + + if {![string match "" $exec_output]} { + fail "$testname: unexpected output from linker: $exec_output" + return + } + + pass "$testname" +} + +run_test