]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - ld/emultempl/riscvelf.em
RISC-V: PR31179, The SET/ADD/SUB fix breaks ABI compatibility with 2.41 objects
[thirdparty/binutils-gdb.git] / ld / emultempl / riscvelf.em
1 # This shell script emits a C file. -*- C -*-
2 # Copyright (C) 2004-2023 Free Software Foundation, Inc.
3 #
4 # This file is part of the GNU Binutils.
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 # MA 02110-1301, USA.
20
21 fragment <<EOF
22
23 #include "ldmain.h"
24 #include "ldctor.h"
25 #include "elf/riscv.h"
26 #include "elfxx-riscv.h"
27
28 static struct riscv_elf_params params = { .relax_gp = 1,
29 .check_uleb128 = 0};
30 EOF
31
32 # Define some shell vars to insert bits of code into the standard elf
33 # parse_args and list_options functions. */
34 PARSE_AND_LIST_PROLOGUE=${PARSE_AND_LIST_PROLOGUE}'
35 enum risccv_opt
36 {
37 OPTION_RELAX_GP = 321,
38 OPTION_NO_RELAX_GP,
39 OPTION_CHECK_ULEB128,
40 OPTION_NO_CHECK_ULEB128,
41 };
42 '
43
44 PARSE_AND_LIST_LONGOPTS=${PARSE_AND_LIST_LONGOPTS}'
45 { "relax-gp", no_argument, NULL, OPTION_RELAX_GP },
46 { "no-relax-gp", no_argument, NULL, OPTION_NO_RELAX_GP },
47 { "check-uleb128", no_argument, NULL, OPTION_CHECK_ULEB128 },
48 { "no-check-uleb128", no_argument, NULL, OPTION_NO_CHECK_ULEB128 },
49 '
50
51 PARSE_AND_LIST_OPTIONS=${PARSE_AND_LIST_OPTIONS}'
52 fprintf (file, _(" --relax-gp Perform GP relaxation\n"));
53 fprintf (file, _(" --no-relax-gp Don'\''t perform GP relaxation\n"));
54 fprintf (file, _(" --check-uleb128 Check if SUB_ULEB128 has non-zero addend\n"));
55 fprintf (file, _(" --no-check-uleb128 Don'\''t check if SUB_ULEB128 has non-zero addend\n"));
56 '
57
58 PARSE_AND_LIST_ARGS_CASES=${PARSE_AND_LIST_ARGS_CASES}'
59 case OPTION_RELAX_GP:
60 params.relax_gp = 1;
61 break;
62
63 case OPTION_NO_RELAX_GP:
64 params.relax_gp = 0;
65 break;
66
67 case OPTION_CHECK_ULEB128:
68 params.check_uleb128 = 1;
69 break;
70
71 case OPTION_NO_CHECK_ULEB128:
72 params.check_uleb128 = 0;
73 break;
74 '
75
76 fragment <<EOF
77 static void
78 riscv_elf_before_allocation (void)
79 {
80 gld${EMULATION_NAME}_before_allocation ();
81
82 if (link_info.discard == discard_sec_merge)
83 link_info.discard = discard_l;
84
85 if (!bfd_link_relocatable (&link_info))
86 {
87 /* We always need at least some relaxation to handle code alignment. */
88 if (RELAXATION_DISABLED_BY_USER)
89 TARGET_ENABLE_RELAXATION;
90 else
91 ENABLE_RELAXATION;
92 }
93
94 link_info.relax_pass = 2;
95 }
96
97 static void
98 gld${EMULATION_NAME}_after_allocation (void)
99 {
100 int need_layout = 0;
101
102 /* Don't attempt to discard unused .eh_frame sections until the final link,
103 as we can't reliably tell if they're used until after relaxation. */
104 if (!bfd_link_relocatable (&link_info))
105 {
106 need_layout = bfd_elf_discard_info (link_info.output_bfd, &link_info);
107 if (need_layout < 0)
108 {
109 einfo (_("%X%P: .eh_frame/.stab edit: %E\n"));
110 return;
111 }
112 }
113
114 /* PR 27566, if the phase of data segment is exp_seg_relro_adjust,
115 that means we are still adjusting the relro, and shouldn't do the
116 relaxations at this stage. Otherwise, we will get the symbol
117 values beofore handling the relro, and may cause truncated fails
118 when the relax range crossing the data segment. One of the solution
119 is to monitor the data segment phase while relaxing, to know whether
120 the relro has been handled or not.
121
122 I think we probably need to record more information about data
123 segment or alignments in the future, to make sure it is safe
124 to doing relaxations. */
125 enum phase_enum *phase = &(expld.dataseg.phase);
126 bfd_elf${ELFSIZE}_riscv_set_data_segment_info (&link_info, (int *) phase);
127
128 ldelf_map_segments (need_layout);
129 }
130
131 /* This is a convenient point to tell BFD about target specific flags.
132 After the output has been created, but before inputs are read. */
133
134 static void
135 riscv_create_output_section_statements (void)
136 {
137 /* See PR 22920 for an example of why this is necessary. */
138 if (strstr (bfd_get_target (link_info.output_bfd), "riscv") == NULL)
139 {
140 /* The RISC-V backend needs special fields in the output hash structure.
141 These will only be created if the output format is a RISC-V format,
142 hence we do not support linking and changing output formats at the
143 same time. Use a link followed by objcopy to change output formats. */
144 einfo (_("%F%P: error: cannot change output format"
145 " whilst linking %s binaries\n"), "RISC-V");
146 return;
147 }
148
149 riscv_elf${ELFSIZE}_set_options (&link_info, &params);
150 }
151
152 EOF
153
154 LDEMUL_BEFORE_ALLOCATION=riscv_elf_before_allocation
155 LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
156 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=riscv_create_output_section_statements