Commit | Line | Data |
---|---|---|
b8891f8d | 1 | # This shell script emits a C file. -*- C -*- |
b3adc24a | 2 | # Copyright (C) 2013-2020 Free Software Foundation, Inc. |
b8891f8d AJ |
3 | # |
4 | # This file is part of 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 | ||
075a2b89 | 21 | # This file is sourced from elf.em, and defines extra C-SKY ELF |
b8891f8d AJ |
22 | # specific routines. |
23 | # | |
24 | fragment <<EOF | |
25 | ||
26 | #include "ldctor.h" | |
27 | #include "elf/csky.h" | |
6e67e6b0 | 28 | #include "elf32-csky.h" |
b8891f8d AJ |
29 | |
30 | /* To use branch stub or not. */ | |
31 | extern bfd_boolean use_branch_stub; | |
32 | ||
33 | /* Fake input file for stubs. */ | |
34 | static lang_input_statement_type *stub_file; | |
35 | ||
36 | /* Whether we need to call gldcsky_layout_sections_again. */ | |
37 | static int need_laying_out = 0; | |
38 | ||
39 | /* Maximum size of a group of input sections that can be handled by | |
40 | one stub section. A value of +/-1 indicates the bfd back-end | |
41 | should use a suitable default size. */ | |
42 | static bfd_signed_vma group_size = 1; | |
43 | ||
44 | struct hook_stub_info | |
45 | { | |
46 | lang_statement_list_type add; | |
47 | asection *input_section; | |
48 | }; | |
49 | ||
50 | /* Traverse the linker tree to find the spot where the stub goes. */ | |
51 | static bfd_boolean | |
52 | hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp) | |
53 | { | |
54 | lang_statement_union_type *l; | |
55 | bfd_boolean ret; | |
56 | ||
57 | for (l = *lp; l != NULL; lp = &l->header.next, l = *lp) | |
58 | switch (l->header.type) | |
59 | { | |
60 | case lang_constructors_statement_enum: | |
61 | ret = hook_in_stub (info, &constructor_list.head); | |
62 | if (ret) | |
63 | return ret; | |
64 | break; | |
65 | ||
66 | case lang_output_section_statement_enum: | |
67 | ret = hook_in_stub (info, | |
68 | &l->output_section_statement.children.head); | |
69 | if (ret) | |
70 | return ret; | |
71 | break; | |
72 | ||
73 | case lang_wild_statement_enum: | |
74 | ret = hook_in_stub (info, &l->wild_statement.children.head); | |
75 | if (ret) | |
76 | return ret; | |
77 | break; | |
78 | ||
79 | case lang_group_statement_enum: | |
80 | ret = hook_in_stub (info, &l->group_statement.children.head); | |
81 | if (ret) | |
82 | return ret; | |
83 | break; | |
84 | ||
85 | case lang_input_section_enum: | |
86 | if (l->input_section.section == info->input_section) | |
87 | { | |
88 | /* We've found our section. Insert the stub immediately | |
89 | after its associated input section. */ | |
90 | *(info->add.tail) = l->header.next; | |
91 | l->header.next = info->add.head; | |
92 | return TRUE; | |
93 | } | |
94 | break; | |
95 | ||
96 | case lang_data_statement_enum: | |
97 | case lang_reloc_statement_enum: | |
98 | case lang_object_symbols_statement_enum: | |
99 | case lang_output_statement_enum: | |
100 | case lang_target_statement_enum: | |
101 | case lang_input_statement_enum: | |
102 | case lang_assignment_statement_enum: | |
103 | case lang_padding_statement_enum: | |
104 | case lang_address_statement_enum: | |
105 | case lang_fill_statement_enum: | |
106 | break; | |
107 | ||
108 | default: | |
109 | FAIL (); | |
110 | break; | |
111 | } | |
112 | ||
113 | return FALSE; | |
114 | } | |
115 | EOF | |
116 | ||
117 | case ${target} in | |
118 | csky-*-linux-*) | |
119 | fragment <<EOF | |
53b81c6d | 120 | |
b8891f8d | 121 | static void |
53b81c6d | 122 | csky_elf_before_parse (void) |
b8891f8d AJ |
123 | { |
124 | use_branch_stub = FALSE; | |
53b81c6d | 125 | gld${EMULATION_NAME}_before_parse (); |
b8891f8d AJ |
126 | } |
127 | EOF | |
128 | ;; | |
53b81c6d AM |
129 | esac |
130 | ||
b8891f8d | 131 | fragment <<EOF |
53b81c6d | 132 | |
b8891f8d AJ |
133 | /* This is a convenient point to tell BFD about target specific flags. |
134 | After the output has been created, but before inputs are read. */ | |
135 | static void | |
136 | csky_elf_create_output_section_statements (void) | |
137 | { | |
53b81c6d AM |
138 | if (!(bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour |
139 | && elf_object_id (link_info.output_bfd) == CSKY_ELF_DATA)) | |
140 | use_branch_stub = FALSE; | |
141 | ||
b8891f8d | 142 | /* If don't use branch stub, just do not emit stub_file. */ |
53b81c6d | 143 | if (!use_branch_stub) |
b8891f8d | 144 | return; |
53b81c6d | 145 | |
b8891f8d AJ |
146 | stub_file = lang_add_input_file ("linker stubs", |
147 | lang_input_file_is_fake_enum, NULL); | |
148 | stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd); | |
149 | if (stub_file->the_bfd == NULL | |
150 | || !bfd_set_arch_mach (stub_file->the_bfd, | |
151 | bfd_get_arch (link_info.output_bfd), | |
152 | bfd_get_mach (link_info.output_bfd))) | |
153 | { | |
154 | einfo (_("%F%P: can not create BFD: %E\n")); | |
155 | return; | |
156 | } | |
157 | ||
158 | stub_file->the_bfd->flags |= BFD_LINKER_CREATED; | |
159 | ldlang_add_file (stub_file); | |
160 | } | |
b8891f8d | 161 | |
b8891f8d AJ |
162 | /* Call-back for elf32_csky_size_stubs. */ |
163 | ||
164 | /* Create a new stub section, and arrange for it to be linked | |
165 | immediately after INPUT_SECTION. */ | |
166 | static asection * | |
167 | elf32_csky_add_stub_section (const char *stub_sec_name, | |
168 | asection *input_section) | |
169 | { | |
170 | asection *stub_sec; | |
171 | flagword flags; | |
172 | asection *output_section; | |
173 | const char *secname; | |
174 | lang_output_section_statement_type *os; | |
175 | struct hook_stub_info info; | |
176 | ||
177 | flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | |
178 | | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP); | |
179 | stub_sec = bfd_make_section_anyway_with_flags (stub_file->the_bfd, | |
180 | stub_sec_name, flags); | |
181 | if (stub_sec == NULL) | |
182 | goto err_ret; | |
183 | ||
fd361982 | 184 | bfd_set_section_alignment (stub_sec, 3); |
b8891f8d AJ |
185 | |
186 | output_section = input_section->output_section; | |
fd361982 | 187 | secname = bfd_section_name (output_section); |
b8891f8d AJ |
188 | os = lang_output_section_find (secname); |
189 | ||
190 | info.input_section = input_section; | |
191 | lang_list_init (&info.add); | |
192 | lang_add_section (&info.add, stub_sec, NULL, os); | |
193 | ||
194 | if (info.add.head == NULL) | |
195 | goto err_ret; | |
196 | ||
197 | if (hook_in_stub (&info, &os->children.head)) | |
198 | return stub_sec; | |
199 | ||
dc1e8a47 | 200 | err_ret: |
b8891f8d AJ |
201 | einfo (_("%X%P: can not make stub section: %E\n")); |
202 | return NULL; | |
203 | } | |
204 | ||
205 | /* Another call-back for elf_csky_size_stubs. */ | |
206 | static void | |
207 | gldcsky_layout_sections_again (void) | |
208 | { | |
209 | /* If we have changed sizes of the stub sections, then we need | |
210 | to recalculate all the section offsets. This may mean we need to | |
211 | add even more stubs. */ | |
d871d478 | 212 | ldelf_map_segments (TRUE); |
b8891f8d AJ |
213 | need_laying_out = -1; |
214 | } | |
215 | ||
216 | static void | |
217 | build_section_lists (lang_statement_union_type *statement) | |
218 | { | |
219 | if (statement->header.type == lang_input_section_enum) | |
220 | { | |
221 | asection *i = statement->input_section.section; | |
222 | ||
223 | if (i->sec_info_type != SEC_INFO_TYPE_JUST_SYMS | |
224 | && (i->flags & SEC_EXCLUDE) == 0 | |
225 | && i->output_section != NULL | |
226 | && i->output_section->owner == link_info.output_bfd) | |
227 | elf32_csky_next_input_section (&link_info, i); | |
228 | } | |
229 | } | |
230 | ||
231 | static void | |
232 | gld${EMULATION_NAME}_after_allocation (void) | |
233 | { | |
234 | /* bfd_elf32_discard_info just plays with debugging sections, | |
235 | ie. doesn't affect any code, so we can delay resizing the | |
236 | sections. It's likely we'll resize everything in the process of | |
237 | adding stubs. */ | |
238 | if (bfd_elf_discard_info (link_info.output_bfd, &link_info)) | |
239 | need_laying_out = 1; | |
240 | ||
241 | /* If generating a relocatable output file, then we don't | |
242 | have to examine the relocs. */ | |
243 | ||
244 | if (stub_file != NULL && !bfd_link_relocatable (&link_info)) | |
245 | { | |
246 | int ret = elf32_csky_setup_section_lists (link_info.output_bfd, | |
247 | &link_info); | |
248 | ||
249 | if (ret < 0) | |
250 | { | |
251 | einfo (_("%X%P: could not compute sections lists for stub generation: %E\n")); | |
252 | return; | |
253 | } | |
254 | else if (ret != 0) | |
255 | { | |
256 | lang_for_each_statement (build_section_lists); | |
257 | ||
258 | /* Call into the BFD backend to do the real work. */ | |
259 | if (! elf32_csky_size_stubs (link_info.output_bfd, | |
260 | stub_file->the_bfd, | |
261 | &link_info, | |
262 | group_size, | |
263 | &elf32_csky_add_stub_section, | |
264 | &gldcsky_layout_sections_again)) | |
265 | { | |
266 | einfo (_("%X%P: cannot size stub section: %E\n")); | |
267 | return; | |
268 | } | |
269 | } | |
270 | } | |
271 | ||
272 | if (need_laying_out != -1) | |
d871d478 | 273 | ldelf_map_segments (need_laying_out); |
b8891f8d AJ |
274 | } |
275 | ||
276 | static void | |
277 | gld${EMULATION_NAME}_finish (void) | |
278 | { | |
279 | if (stub_file != NULL | |
280 | && !bfd_link_relocatable (&link_info) | |
281 | && stub_file->the_bfd->sections != NULL | |
282 | && !elf32_csky_build_stubs (&link_info)) | |
283 | einfo (_("%X%P: cannot build stubs: %E\n")); | |
284 | finish_default (); | |
285 | } | |
286 | ||
b8891f8d AJ |
287 | EOF |
288 | ||
289 | # This code gets inserted into the generic elf32.sc linker script | |
290 | # and allows us to define our own command line switches. | |
b8891f8d AJ |
291 | PARSE_AND_LIST_PROLOGUE=' |
292 | #define OPTION_BRANCH_STUB 301 | |
293 | #define OPTION_NO_BRANCH_STUB 302 | |
294 | #define OPTION_STUBGROUP_SIZE 303 | |
295 | ' | |
296 | ||
297 | PARSE_AND_LIST_LONGOPTS=' | |
298 | {"branch-stub", no_argument, NULL, OPTION_BRANCH_STUB}, | |
299 | {"no-branch-stub", no_argument, NULL, OPTION_NO_BRANCH_STUB}, | |
300 | {"stub-group-size", required_argument, NULL, OPTION_STUBGROUP_SIZE}, | |
301 | ' | |
302 | PARSE_AND_LIST_OPTIONS=' | |
303 | fprintf (file, _(" --[no-]branch-stub\n")); | |
304 | fprintf (file, _("\t\t\tDisable/enable use of stubs to expand branch " | |
305 | "instructions that cannot reach the target.\n")); | |
306 | fprintf (file, _(" --stub-group-size=N\n")); | |
307 | fprintf (file, _("\t\t\tMaximum size of a group of input sections " | |
308 | "handled by one stub section.")); | |
309 | ' | |
310 | ||
311 | PARSE_AND_LIST_ARGS_CASES=' | |
312 | case OPTION_BRANCH_STUB: | |
313 | use_branch_stub = TRUE; | |
314 | break; | |
315 | case OPTION_NO_BRANCH_STUB: | |
316 | use_branch_stub = FALSE; | |
317 | break; | |
318 | ||
319 | case OPTION_STUBGROUP_SIZE: | |
320 | { | |
321 | const char *end; | |
322 | ||
323 | group_size = bfd_scan_vma (optarg, &end, 0); | |
324 | if (*end) | |
325 | einfo (_("%F%P: invalid number `%s'\''\n"), optarg); | |
326 | } | |
327 | break; | |
328 | ' | |
b8891f8d | 329 | |
53b81c6d AM |
330 | case ${target} in |
331 | csky-*-linux-*) LDEMUL_BEFORE_PARSE=csky_elf_before_parse ;; | |
332 | esac | |
b8891f8d AJ |
333 | LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation |
334 | LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=csky_elf_create_output_section_statements | |
335 | LDEMUL_FINISH=gld${EMULATION_NAME}_finish |