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