From 2d5783fad77c2cb9cdcb396d65fe0a60e3d8938b Mon Sep 17 00:00:00 2001 From: Nick Clifton Date: Wed, 15 Mar 2023 14:27:21 +0000 Subject: Add --enable-linker-version option to bfd linker to add an entry in the .comment section. PR 30187 * NEWS: Mention the new feature. * ld.texi: Document the new feature. * ldgram.y: Handle LINKER_VERSION token. * ldlang.c (lang_add_version): New function. (enable_linker_version): New global variable. * ldlang.h (land_add_version): Prototype. (enable_linker_version): Export. * ldlex.h (OPTION_ENABLE_LINKER_VERSION): Define. (OPTION_DISABLE_LINKER_VERSION): Define. * ldlex.l (LINKER_VERSION): Add token. * lexsup.c (ld_options): Add --enable-linker-version and --disable-linker-version. (parse_args): Handle the new options. * scripttempl/arclinux.sc: Remove stabs and comment sections and replace with inclusion of misc-sections.sc * scripttempl/avr.sc: Likewise. * scripttempl/dlx.sc: Likewise. * scripttempl/elf.sc: Likewise. * scripttempl/elf32cr16.sc: Likewise. * scripttempl/elf32crx.sc: Likewise. * scripttempl/elf32msp430.sc: Likewise. * scripttempl/elf64bpf.sc: Likewise. * scripttempl/elf64hppa.sc: Likewise. * scripttempl/elf_chaos.sc: Likewise. * scripttempl/elfarc.sc: Likewise. * scripttempl/elfarcv2.sc: Likewise. * scripttempl/elfd10v.sc: Likewise. * scripttempl/elfd30v.sc: Likewise. * scripttempl/elfm68hc11.sc: Likewise. * scripttempl/elfm68hc12.sc: Likewise. * scripttempl/elfm9s12z.sc: Likewise. * scripttempl/elfmicroblaze.sc: Likewise. * scripttempl/elfxgate.sc: Likewise. * scripttempl/elfxtensa.sc: Likewise. * scripttempl/epiphany_4x4.sc: Likewise. * scripttempl/ft32.sc: Likewise. * scripttempl/ip2k.sc: Likewise. * scripttempl/iq2000.sc: Likewise. * scripttempl/mep.sc: Likewise. * scripttempl/nds32elf.sc: Likewise. * scripttempl/pru.sc: Likewise. * scripttempl/v850.sc: Likewise. * scripttempl/v850_rh850.sc: Likewise. * scripttempl/visium.sc: Likewise. * scripttempl/xstormy16.sc: Likewise. * scripttempl/z80.sc: Likewise. * testsuite/ld-scripts/script.exp: Run new tests. * scripttempl/misc-sections.sc: New file. * testsuite/ld-scripts/ld-version-2.d: New file. * testsuite/ld-scripts/ld-version.d: New file. * testsuite/ld-scripts/ld-version.t: New file. --- ld/ChangeLog | 55 ++++++++++++++++++++++++++++++++++ ld/NEWS | 4 +++ ld/ld.texi | 30 ++++++++++++++++++- ld/ldgram.y | 6 +++- ld/ldlang.c | 28 +++++++++++++++-- ld/ldlang.h | 5 +++- ld/ldlex.h | 2 ++ ld/ldlex.l | 1 + ld/lexsup.c | 11 +++++++ ld/scripttempl/arclinux.sc | 13 +------- ld/scripttempl/avr.sc | 11 ++----- ld/scripttempl/dlx.sc | 8 +++++ ld/scripttempl/elf.sc | 15 +--------- ld/scripttempl/elf32cr16.sc | 3 +- ld/scripttempl/elf32crx.sc | 3 +- ld/scripttempl/elf32msp430.sc | 9 +----- ld/scripttempl/elf64bpf.sc | 15 +--------- ld/scripttempl/elf64hppa.sc | 14 +-------- ld/scripttempl/elf_chaos.sc | 11 +------ ld/scripttempl/elfarc.sc | 14 +-------- ld/scripttempl/elfarcv2.sc | 10 +------ ld/scripttempl/elfd10v.sc | 11 +------ ld/scripttempl/elfd30v.sc | 11 +------ ld/scripttempl/elfm68hc11.sc | 11 +------ ld/scripttempl/elfm68hc12.sc | 12 ++------ ld/scripttempl/elfm9s12z.sc | 12 ++------ ld/scripttempl/elfmicroblaze.sc | 6 ++++ ld/scripttempl/elfxgate.sc | 12 +------- ld/scripttempl/elfxtensa.sc | 13 +------- ld/scripttempl/epiphany_4x4.sc | 13 +------- ld/scripttempl/ft32.sc | 5 ---- ld/scripttempl/ip2k.sc | 11 ++----- ld/scripttempl/iq2000.sc | 12 ++------ ld/scripttempl/mep.sc | 12 ++------ ld/scripttempl/misc-sections.sc | 20 +++++++++++++ ld/scripttempl/nds32elf.sc | 13 +------- ld/scripttempl/pru.sc | 10 ++----- ld/scripttempl/v850.sc | 11 ++----- ld/scripttempl/v850_rh850.sc | 11 ++----- ld/scripttempl/visium.sc | 2 +- ld/scripttempl/xstormy16.sc | 13 ++------ ld/scripttempl/z80.sc | 6 ++++ ld/testsuite/ld-scripts/ld-version-2.d | 8 +++++ ld/testsuite/ld-scripts/ld-version.d | 7 +++++ ld/testsuite/ld-scripts/ld-version.t | 9 ++++++ ld/testsuite/ld-scripts/script.exp | 2 ++ 46 files changed, 243 insertions(+), 278 deletions(-) create mode 100644 ld/scripttempl/misc-sections.sc create mode 100644 ld/testsuite/ld-scripts/ld-version-2.d create mode 100644 ld/testsuite/ld-scripts/ld-version.d create mode 100644 ld/testsuite/ld-scripts/ld-version.t (limited to 'ld') diff --git a/ld/ChangeLog b/ld/ChangeLog index 9e876b63d9f..698b3ea4c11 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,58 @@ +2023-03-15 Nick Clifton + + PR 30187 + * NEWS: Mention the new feature. + * ld.texi: Document the new feature. + * ldgram.y: Handle LINKER_VERSION token. + * ldlang.c (lang_add_version): New function. + (enable_linker_version): New global variable. + * ldlang.h (land_add_version): Prototype. + (enable_linker_version): Export. + * ldlex.h (OPTION_ENABLE_LINKER_VERSION): Define. + (OPTION_DISABLE_LINKER_VERSION): Define. + * ldlex.l (LINKER_VERSION): Add token. + * lexsup.c (ld_options): Add --enable-linker-version and + --disable-linker-version. + (parse_args): Handle the new options. + * scripttempl/arclinux.sc: Remove stabs and comment sections and + replace with inclusion of misc-sections.sc + * scripttempl/avr.sc: Likewise. + * scripttempl/dlx.sc: Likewise. + * scripttempl/elf.sc: Likewise. + * scripttempl/elf32cr16.sc: Likewise. + * scripttempl/elf32crx.sc: Likewise. + * scripttempl/elf32msp430.sc: Likewise. + * scripttempl/elf64bpf.sc: Likewise. + * scripttempl/elf64hppa.sc: Likewise. + * scripttempl/elf_chaos.sc: Likewise. + * scripttempl/elfarc.sc: Likewise. + * scripttempl/elfarcv2.sc: Likewise. + * scripttempl/elfd10v.sc: Likewise. + * scripttempl/elfd30v.sc: Likewise. + * scripttempl/elfm68hc11.sc: Likewise. + * scripttempl/elfm68hc12.sc: Likewise. + * scripttempl/elfm9s12z.sc: Likewise. + * scripttempl/elfmicroblaze.sc: Likewise. + * scripttempl/elfxgate.sc: Likewise. + * scripttempl/elfxtensa.sc: Likewise. + * scripttempl/epiphany_4x4.sc: Likewise. + * scripttempl/ft32.sc: Likewise. + * scripttempl/ip2k.sc: Likewise. + * scripttempl/iq2000.sc: Likewise. + * scripttempl/mep.sc: Likewise. + * scripttempl/nds32elf.sc: Likewise. + * scripttempl/pru.sc: Likewise. + * scripttempl/v850.sc: Likewise. + * scripttempl/v850_rh850.sc: Likewise. + * scripttempl/visium.sc: Likewise. + * scripttempl/xstormy16.sc: Likewise. + * scripttempl/z80.sc: Likewise. + * testsuite/ld-scripts/script.exp: Run new tests. + * scripttempl/misc-sections.sc: New file. + * testsuite/ld-scripts/ld-version-2.d: New file. + * testsuite/ld-scripts/ld-version.d: New file. + * testsuite/ld-scripts/ld-version.t: New file. + 2023-02-23 Fangrui Song * emultempl/riscvelf.em: Add option parsing. diff --git a/ld/NEWS b/ld/NEWS index 4ce7e19d40b..37041e7456f 100644 --- a/ld/NEWS +++ b/ld/NEWS @@ -1,5 +1,9 @@ -*- text -*- +* For most ELF based targets, if the --enable-linker-version option is used + then the version of the linker will be inserted as a string into the .comment + section. + * The linker script syntax has a new command for output sections: ASCIZ "string" This will insert a zero-terminated string at the current location. diff --git a/ld/ld.texi b/ld/ld.texi index 8205d4e6771..736563e1e3e 100644 --- a/ld/ld.texi +++ b/ld/ld.texi @@ -461,6 +461,22 @@ will contain a colon separated list of audit interfaces to use. This option is only meaningful on ELF platforms supporting the rtld-audit interface. The -P option is provided for Solaris compatibility. +@kindex --enable-linker-version +@item --enable-linker-version +Enables the @code{LINKER_VERSION} linker script directive, described +in @ref{Output Section Data}. If this directive is used in a linker +script and this option has been enabled then a string containing the +linker version will be inserted at the current point. + +Note - this location of this option on the linker command line is +significant. It will only affect linker scripts that come after it on +the command line, or which are built into the linker. + +@kindex --disable-linker-version +@item --disable-linker-version +Disables the @code{LINKER_VERSION} linker script directive, so that it +does not insert a version string. This is the default. + @kindex --enable-non-contiguous-regions @item --enable-non-contiguous-regions This option avoids generating an error if an input section does not @@ -1168,7 +1184,9 @@ in a linker script. @itemx --version @itemx -V Display the version number for @command{ld}. The @option{-V} option also -lists the supported emulations. +lists the supported emulations. See also the description of the +@option{--enable-linker-version} in @ref{Options,,Command-line Options} +which can be used to insert the linker version string into a binary. @kindex -x @kindex --discard-all @@ -5393,6 +5411,16 @@ entire section. If both are used, the @code{FILL} command takes precedence. @xref{Output Section Fill}, for details on the fill expression. +@kindex LINKER_VERSION +@cindex LINKER_VERSION +Inserts a string containing the version of the linker at the current +point. Note - by default this directive is disabled and will do +nothing. It only becomes active if the +@option{--enable-linker-version} command line option is used. + +Built-in linker scripts for ELF based targets already include this +directive in their @samp{.comment} section. + @node Output Section Keywords @subsection Output Section Keywords There are a couple of keywords which can appear as output section diff --git a/ld/ldgram.y b/ld/ldgram.y index 8240cf97327..4ced9edf7a3 100644 --- a/ld/ldgram.y +++ b/ld/ldgram.y @@ -126,7 +126,7 @@ static int error_index; %token END %left '(' %token ALIGN_K BLOCK BIND QUAD SQUAD LONG SHORT BYTE ASCIZ -%token SECTIONS PHDRS INSERT_K AFTER BEFORE +%token SECTIONS PHDRS INSERT_K AFTER BEFORE LINKER_VERSION %token DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END DATA_SEGMENT_END %token SORT_BY_NAME SORT_BY_ALIGNMENT SORT_NONE %token SORT_BY_INIT_PRIORITY @@ -676,6 +676,10 @@ statement: { lang_add_fill ($3); } + | LINKER_VERSION + { + lang_add_version_string (); + } | ASSERT_K { ldlex_expression (); } '(' exp ',' NAME ')' separator diff --git a/ld/ldlang.c b/ld/ldlang.c index 61de63809cd..b684e2d479a 100644 --- a/ld/ldlang.c +++ b/ld/ldlang.c @@ -27,7 +27,6 @@ #include "obstack.h" #include "bfdlink.h" #include "ctf-api.h" - #include "ld.h" #include "ldmain.h" #include "ldexp.h" @@ -42,9 +41,11 @@ #include "demangle.h" #include "hashtab.h" #include "elf-bfd.h" +#include "bfdver.h" + #if BFD_SUPPORTS_PLUGINS #include "plugin.h" -#endif /* BFD_SUPPORTS_PLUGINS */ +#endif #ifndef offsetof #define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER)) @@ -131,6 +132,7 @@ bool lang_has_input_file = false; bool had_output_filename = false; bool lang_float_flag = false; bool delete_output_file_on_failure = false; +bool enable_linker_version = false; struct lang_phdr *lang_phdr_list; struct lang_nocrossrefs *nocrossref_list; struct asneeded_minfo **asneeded_list_tail; @@ -8261,6 +8263,28 @@ lang_process (void) lang_end (); } +void +lang_add_version_string (void) +{ + if (! enable_linker_version) + return; + + const char * str = "GNU ld "; + int len = strlen (str); + int i; + + for (i = 0 ; i < len ; i++) + lang_add_data (BYTE, exp_intop (str[i])); + + str = BFD_VERSION_STRING; + len = strlen (str); + + for (i = 0 ; i < len ; i++) + lang_add_data (BYTE, exp_intop (str[i])); + + lang_add_data (BYTE, exp_intop ('\0')); +} + /* EXPORTED TO YACC */ void diff --git a/ld/ldlang.h b/ld/ldlang.h index 32819066b8a..463cce394e9 100644 --- a/ld/ldlang.h +++ b/ld/ldlang.h @@ -530,6 +530,7 @@ extern bool lang_has_input_file; extern lang_statement_list_type statement_list; extern lang_statement_list_type *stat_ptr; extern bool delete_output_file_on_failure; +extern bool enable_linker_version; extern struct bfd_sym_chain entry_symbol; extern const char *entry_section; @@ -735,6 +736,8 @@ extern void lang_add_gc_name (const char *); extern bool -print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr); +print_one_symbol (struct bfd_link_hash_entry *, void *); +extern void lang_add_version_string + (void); #endif diff --git a/ld/ldlex.h b/ld/ldlex.h index 0538f0a06a1..48d323d0265 100644 --- a/ld/ldlex.h +++ b/ld/ldlex.h @@ -170,6 +170,8 @@ enum option_values OPTION_NO_WARN_EXECSTACK, OPTION_WARN_RWX_SEGMENTS, OPTION_NO_WARN_RWX_SEGMENTS, + OPTION_ENABLE_LINKER_VERSION, + OPTION_DISABLE_LINKER_VERSION, }; /* The initial parser states. */ diff --git a/ld/ldlex.l b/ld/ldlex.l index 32336cf0be2..3b55e794c57 100644 --- a/ld/ldlex.l +++ b/ld/ldlex.l @@ -310,6 +310,7 @@ V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-9\[\]\-\!\^\\]|::)* "SHORT" { RTOKEN(SHORT); } "BYTE" { RTOKEN(BYTE); } "ASCIZ" { RTOKEN(ASCIZ); } +"LINKER_VERSION" { RTOKEN(LINKER_VERSION); }