Skip to content

Commit 2d5783f

Browse files
committed
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.
1 parent 71f646f commit 2d5783f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+243
-278
lines changed

ld/ChangeLog

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,58 @@
1+
2023-03-15 Nick Clifton <[email protected]>
2+
3+
PR 30187
4+
* NEWS: Mention the new feature.
5+
* ld.texi: Document the new feature.
6+
* ldgram.y: Handle LINKER_VERSION token.
7+
* ldlang.c (lang_add_version): New function.
8+
(enable_linker_version): New global variable.
9+
* ldlang.h (land_add_version): Prototype.
10+
(enable_linker_version): Export.
11+
* ldlex.h (OPTION_ENABLE_LINKER_VERSION): Define.
12+
(OPTION_DISABLE_LINKER_VERSION): Define.
13+
* ldlex.l (LINKER_VERSION): Add token.
14+
* lexsup.c (ld_options): Add --enable-linker-version and
15+
--disable-linker-version.
16+
(parse_args): Handle the new options.
17+
* scripttempl/arclinux.sc: Remove stabs and comment sections and
18+
replace with inclusion of misc-sections.sc
19+
* scripttempl/avr.sc: Likewise.
20+
* scripttempl/dlx.sc: Likewise.
21+
* scripttempl/elf.sc: Likewise.
22+
* scripttempl/elf32cr16.sc: Likewise.
23+
* scripttempl/elf32crx.sc: Likewise.
24+
* scripttempl/elf32msp430.sc: Likewise.
25+
* scripttempl/elf64bpf.sc: Likewise.
26+
* scripttempl/elf64hppa.sc: Likewise.
27+
* scripttempl/elf_chaos.sc: Likewise.
28+
* scripttempl/elfarc.sc: Likewise.
29+
* scripttempl/elfarcv2.sc: Likewise.
30+
* scripttempl/elfd10v.sc: Likewise.
31+
* scripttempl/elfd30v.sc: Likewise.
32+
* scripttempl/elfm68hc11.sc: Likewise.
33+
* scripttempl/elfm68hc12.sc: Likewise.
34+
* scripttempl/elfm9s12z.sc: Likewise.
35+
* scripttempl/elfmicroblaze.sc: Likewise.
36+
* scripttempl/elfxgate.sc: Likewise.
37+
* scripttempl/elfxtensa.sc: Likewise.
38+
* scripttempl/epiphany_4x4.sc: Likewise.
39+
* scripttempl/ft32.sc: Likewise.
40+
* scripttempl/ip2k.sc: Likewise.
41+
* scripttempl/iq2000.sc: Likewise.
42+
* scripttempl/mep.sc: Likewise.
43+
* scripttempl/nds32elf.sc: Likewise.
44+
* scripttempl/pru.sc: Likewise.
45+
* scripttempl/v850.sc: Likewise.
46+
* scripttempl/v850_rh850.sc: Likewise.
47+
* scripttempl/visium.sc: Likewise.
48+
* scripttempl/xstormy16.sc: Likewise.
49+
* scripttempl/z80.sc: Likewise.
50+
* testsuite/ld-scripts/script.exp: Run new tests.
51+
* scripttempl/misc-sections.sc: New file.
52+
* testsuite/ld-scripts/ld-version-2.d: New file.
53+
* testsuite/ld-scripts/ld-version.d: New file.
54+
* testsuite/ld-scripts/ld-version.t: New file.
55+
156
2023-02-23 Fangrui Song <[email protected]>
257

358
* emultempl/riscvelf.em: Add option parsing.

ld/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
-*- text -*-
22

3+
* For most ELF based targets, if the --enable-linker-version option is used
4+
then the version of the linker will be inserted as a string into the .comment
5+
section.
6+
37
* The linker script syntax has a new command for output sections: ASCIZ "string"
48
This will insert a zero-terminated string at the current location.
59

ld/ld.texi

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,22 @@ will contain a colon separated list of audit interfaces to use. This
461461
option is only meaningful on ELF platforms supporting the rtld-audit interface.
462462
The -P option is provided for Solaris compatibility.
463463

464+
@kindex --enable-linker-version
465+
@item --enable-linker-version
466+
Enables the @code{LINKER_VERSION} linker script directive, described
467+
in @ref{Output Section Data}. If this directive is used in a linker
468+
script and this option has been enabled then a string containing the
469+
linker version will be inserted at the current point.
470+
471+
Note - this location of this option on the linker command line is
472+
significant. It will only affect linker scripts that come after it on
473+
the command line, or which are built into the linker.
474+
475+
@kindex --disable-linker-version
476+
@item --disable-linker-version
477+
Disables the @code{LINKER_VERSION} linker script directive, so that it
478+
does not insert a version string. This is the default.
479+
464480
@kindex --enable-non-contiguous-regions
465481
@item --enable-non-contiguous-regions
466482
This option avoids generating an error if an input section does not
@@ -1168,7 +1184,9 @@ in a linker script.
11681184
@itemx --version
11691185
@itemx -V
11701186
Display the version number for @command{ld}. The @option{-V} option also
1171-
lists the supported emulations.
1187+
lists the supported emulations. See also the description of the
1188+
@option{--enable-linker-version} in @ref{Options,,Command-line Options}
1189+
which can be used to insert the linker version string into a binary.
11721190

11731191
@kindex -x
11741192
@kindex --discard-all
@@ -5393,6 +5411,16 @@ entire section. If both are used, the @code{FILL} command takes
53935411
precedence. @xref{Output Section Fill}, for details on the fill
53945412
expression.
53955413

5414+
@kindex LINKER_VERSION
5415+
@cindex LINKER_VERSION
5416+
Inserts a string containing the version of the linker at the current
5417+
point. Note - by default this directive is disabled and will do
5418+
nothing. It only becomes active if the
5419+
@option{--enable-linker-version} command line option is used.
5420+
5421+
Built-in linker scripts for ELF based targets already include this
5422+
directive in their @samp{.comment} section.
5423+
53965424
@node Output Section Keywords
53975425
@subsection Output Section Keywords
53985426
There are a couple of keywords which can appear as output section

ld/ldgram.y

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ static int error_index;
126126
%token END
127127
%left <token> '('
128128
%token <token> ALIGN_K BLOCK BIND QUAD SQUAD LONG SHORT BYTE ASCIZ
129-
%token SECTIONS PHDRS INSERT_K AFTER BEFORE
129+
%token SECTIONS PHDRS INSERT_K AFTER BEFORE LINKER_VERSION
130130
%token DATA_SEGMENT_ALIGN DATA_SEGMENT_RELRO_END DATA_SEGMENT_END
131131
%token SORT_BY_NAME SORT_BY_ALIGNMENT SORT_NONE
132132
%token SORT_BY_INIT_PRIORITY
@@ -676,6 +676,10 @@ statement:
676676
{
677677
lang_add_fill ($3);
678678
}
679+
| LINKER_VERSION
680+
{
681+
lang_add_version_string ();
682+
}
679683
| ASSERT_K
680684
{ ldlex_expression (); }
681685
'(' exp ',' NAME ')' separator

ld/ldlang.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include "obstack.h"
2828
#include "bfdlink.h"
2929
#include "ctf-api.h"
30-
3130
#include "ld.h"
3231
#include "ldmain.h"
3332
#include "ldexp.h"
@@ -42,9 +41,11 @@
4241
#include "demangle.h"
4342
#include "hashtab.h"
4443
#include "elf-bfd.h"
44+
#include "bfdver.h"
45+
4546
#if BFD_SUPPORTS_PLUGINS
4647
#include "plugin.h"
47-
#endif /* BFD_SUPPORTS_PLUGINS */
48+
#endif
4849

4950
#ifndef offsetof
5051
#define offsetof(TYPE, MEMBER) ((size_t) & (((TYPE*) 0)->MEMBER))
@@ -131,6 +132,7 @@ bool lang_has_input_file = false;
131132
bool had_output_filename = false;
132133
bool lang_float_flag = false;
133134
bool delete_output_file_on_failure = false;
135+
bool enable_linker_version = false;
134136
struct lang_phdr *lang_phdr_list;
135137
struct lang_nocrossrefs *nocrossref_list;
136138
struct asneeded_minfo **asneeded_list_tail;
@@ -8261,6 +8263,28 @@ lang_process (void)
82618263
lang_end ();
82628264
}
82638265

8266+
void
8267+
lang_add_version_string (void)
8268+
{
8269+
if (! enable_linker_version)
8270+
return;
8271+
8272+
const char * str = "GNU ld ";
8273+
int len = strlen (str);
8274+
int i;
8275+
8276+
for (i = 0 ; i < len ; i++)
8277+
lang_add_data (BYTE, exp_intop (str[i]));
8278+
8279+
str = BFD_VERSION_STRING;
8280+
len = strlen (str);
8281+
8282+
for (i = 0 ; i < len ; i++)
8283+
lang_add_data (BYTE, exp_intop (str[i]));
8284+
8285+
lang_add_data (BYTE, exp_intop ('\0'));
8286+
}
8287+
82648288
/* EXPORTED TO YACC */
82658289

82668290
void

ld/ldlang.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ extern bool lang_has_input_file;
530530
extern lang_statement_list_type statement_list;
531531
extern lang_statement_list_type *stat_ptr;
532532
extern bool delete_output_file_on_failure;
533+
extern bool enable_linker_version;
533534

534535
extern struct bfd_sym_chain entry_symbol;
535536
extern const char *entry_section;
@@ -735,6 +736,8 @@ extern void
735736
lang_add_gc_name (const char *);
736737

737738
extern bool
738-
print_one_symbol (struct bfd_link_hash_entry *hash_entry, void *ptr);
739+
print_one_symbol (struct bfd_link_hash_entry *, void *);
739740

741+
extern void lang_add_version_string
742+
(void);
740743
#endif

ld/ldlex.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ enum option_values
170170
OPTION_NO_WARN_EXECSTACK,
171171
OPTION_WARN_RWX_SEGMENTS,
172172
OPTION_NO_WARN_RWX_SEGMENTS,
173+
OPTION_ENABLE_LINKER_VERSION,
174+
OPTION_DISABLE_LINKER_VERSION,
173175
};
174176

175177
/* The initial parser states. */

ld/ldlex.l

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ V_IDENTIFIER [*?.$_a-zA-Z\[\]\-\!\^\\]([*?.$_a-zA-Z0-9\[\]\-\!\^\\]|::)*
310310
<WILD>"SHORT" { RTOKEN(SHORT); }
311311
<WILD>"BYTE" { RTOKEN(BYTE); }
312312
<WILD>"ASCIZ" { RTOKEN(ASCIZ); }
313+
<WILD>"LINKER_VERSION" { RTOKEN(LINKER_VERSION); }
313314
<SCRIPT>"NOFLOAT" { RTOKEN(NOFLOAT); }
314315
<SCRIPT,EXPRESSION>"NOCROSSREFS" { RTOKEN(NOCROSSREFS); }
315316
<SCRIPT,EXPRESSION>"NOCROSSREFS_TO" { RTOKEN(NOCROSSREFS_TO); }

ld/lexsup.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ static const struct ld_option ld_options[] =
130130
'\0', NULL, N_("Enable support of non-contiguous memory regions"), TWO_DASHES },
131131
{ {"enable-non-contiguous-regions-warnings", no_argument, NULL, OPTION_NON_CONTIGUOUS_REGIONS_WARNINGS},
132132
'\0', NULL, N_("Enable warnings when --enable-non-contiguous-regions may cause unexpected behaviour"), TWO_DASHES },
133+
{ {"disable-linker-version", no_argument, NULL, OPTION_DISABLE_LINKER_VERSION},
134+
'\0', NULL, N_("Disable the LINKER_VERSION linker script directive"), TWO_DASHES },
135+
{ {"enable-linker-version", no_argument, NULL, OPTION_ENABLE_LINKER_VERSION},
136+
'\0', NULL, N_("Enable the LINKER_VERSION linker script directive"), TWO_DASHES },
133137
{ {"EB", no_argument, NULL, OPTION_EB},
134138
'\0', NULL, N_("Link big-endian objects"), ONE_DASH },
135139
{ {"EL", no_argument, NULL, OPTION_EL},
@@ -1095,6 +1099,13 @@ parse_args (unsigned argc, char **argv)
10951099
break;
10961100
#endif
10971101

1102+
case OPTION_ENABLE_LINKER_VERSION:
1103+
enable_linker_version = true;
1104+
break;
1105+
case OPTION_DISABLE_LINKER_VERSION:
1106+
enable_linker_version = false;
1107+
break;
1108+
10981109
case OPTION_UNDEFINED_VERSION:
10991110
link_info.allow_undefined_version = true;
11001111
break;

ld/scripttempl/arclinux.sc

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -656,18 +656,7 @@ EOF
656656

657657
test -z "${NON_ALLOC_DYN}" || emit_dyn
658658

659-
cat <<EOF
660-
/* Stabs debugging sections. */
661-
.stab 0 : { *(.stab) }
662-
.stabstr 0 : { *(.stabstr) }
663-
.stab.excl 0 : { *(.stab.excl) }
664-
.stab.exclstr 0 : { *(.stab.exclstr) }
665-
.stab.index 0 : { *(.stab.index) }
666-
.stab.indexstr 0 : { *(.stab.indexstr) }
667-
668-
.comment 0 : { *(.comment) }
669-
670-
EOF
659+
. $srcdir/scripttempl/misc-sections.sc
671660

672661
. $srcdir/scripttempl/DWARF.sc
673662

0 commit comments

Comments
 (0)