Commit | Line | Data |
---|---|---|
73934d31 | 1 | # This shell script emits a C file. -*- C -*- |
82704155 | 2 | # Copyright (C) 2004-2019 Free Software Foundation, Inc. |
73934d31 | 3 | # |
f96b4a7b | 4 | # This file is part of the GNU Binutils. |
73934d31 RS |
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 | |
f96b4a7b | 8 | # the Free Software Foundation; either version 3 of the License, or |
73934d31 RS |
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 | |
f96b4a7b NC |
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. | |
73934d31 | 20 | |
47275900 MR |
21 | case ${target} in |
22 | *-*-*gnu*) | |
23 | gnu_target=TRUE | |
24 | ;; | |
25 | *) | |
26 | gnu_target=FALSE | |
27 | ;; | |
28 | esac | |
29 | ||
92b93329 | 30 | fragment <<EOF |
861fb55a DJ |
31 | |
32 | #include "ldctor.h" | |
33 | #include "elf/mips.h" | |
34 | #include "elfxx-mips.h" | |
35 | ||
36 | #define is_mips_elf(bfd) \ | |
37 | (bfd_get_flavour (bfd) == bfd_target_elf_flavour \ | |
38 | && elf_tdata (bfd) != NULL \ | |
4dfe6ac6 | 39 | && elf_object_id (bfd) == MIPS_ELF_DATA) |
861fb55a DJ |
40 | |
41 | /* Fake input file for stubs. */ | |
42 | static lang_input_statement_type *stub_file; | |
43 | static bfd *stub_bfd; | |
44 | ||
833794fc | 45 | static bfd_boolean insn32; |
8b10b0b3 | 46 | static bfd_boolean ignore_branch_isa; |
3734320d | 47 | static bfd_boolean compact_branches; |
833794fc | 48 | |
73934d31 RS |
49 | static void |
50 | mips_after_parse (void) | |
51 | { | |
52 | /* .gnu.hash and the MIPS ABI require .dynsym to be sorted in different | |
53 | ways. .gnu.hash needs symbols to be grouped by hash code whereas the | |
54 | MIPS ABI requires a mapping between the GOT and the symbol table. */ | |
55 | if (link_info.emit_gnu_hash) | |
56 | { | |
d003af55 | 57 | einfo (_("%X%P: .gnu.hash is incompatible with the MIPS ABI\n")); |
73934d31 RS |
58 | link_info.emit_hash = TRUE; |
59 | link_info.emit_gnu_hash = FALSE; | |
60 | } | |
5fe2850d | 61 | gld${EMULATION_NAME}_after_parse (); |
73934d31 | 62 | } |
861fb55a DJ |
63 | |
64 | struct hook_stub_info | |
65 | { | |
66 | lang_statement_list_type add; | |
67 | asection *input_section; | |
68 | }; | |
69 | ||
70 | /* Traverse the linker tree to find the spot where the stub goes. */ | |
71 | ||
72 | static bfd_boolean | |
73 | hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp) | |
74 | { | |
75 | lang_statement_union_type *l; | |
76 | bfd_boolean ret; | |
77 | ||
78 | for (; (l = *lp) != NULL; lp = &l->header.next) | |
79 | { | |
80 | switch (l->header.type) | |
81 | { | |
82 | case lang_constructors_statement_enum: | |
83 | ret = hook_in_stub (info, &constructor_list.head); | |
84 | if (ret) | |
85 | return ret; | |
86 | break; | |
87 | ||
88 | case lang_output_section_statement_enum: | |
89 | ret = hook_in_stub (info, | |
90 | &l->output_section_statement.children.head); | |
91 | if (ret) | |
92 | return ret; | |
93 | break; | |
94 | ||
95 | case lang_wild_statement_enum: | |
96 | ret = hook_in_stub (info, &l->wild_statement.children.head); | |
97 | if (ret) | |
98 | return ret; | |
99 | break; | |
100 | ||
101 | case lang_group_statement_enum: | |
102 | ret = hook_in_stub (info, &l->group_statement.children.head); | |
103 | if (ret) | |
104 | return ret; | |
105 | break; | |
106 | ||
107 | case lang_input_section_enum: | |
108 | if (info->input_section == NULL | |
109 | || l->input_section.section == info->input_section) | |
110 | { | |
111 | /* We've found our section. Insert the stub immediately | |
112 | before its associated input section. */ | |
113 | *lp = info->add.head; | |
114 | *(info->add.tail) = l; | |
115 | return TRUE; | |
116 | } | |
117 | break; | |
118 | ||
119 | case lang_data_statement_enum: | |
120 | case lang_reloc_statement_enum: | |
121 | case lang_object_symbols_statement_enum: | |
122 | case lang_output_statement_enum: | |
123 | case lang_target_statement_enum: | |
124 | case lang_input_statement_enum: | |
125 | case lang_assignment_statement_enum: | |
126 | case lang_padding_statement_enum: | |
127 | case lang_address_statement_enum: | |
128 | case lang_fill_statement_enum: | |
129 | break; | |
130 | ||
131 | default: | |
132 | FAIL (); | |
133 | break; | |
134 | } | |
135 | } | |
136 | return FALSE; | |
137 | } | |
138 | ||
139 | /* Create a new stub section called STUB_SEC_NAME and arrange for it to | |
140 | be linked in OUTPUT_SECTION. The section should go at the beginning of | |
141 | OUTPUT_SECTION if INPUT_SECTION is null, otherwise it must go immediately | |
142 | before INPUT_SECTION. */ | |
143 | ||
144 | static asection * | |
145 | mips_add_stub_section (const char *stub_sec_name, asection *input_section, | |
146 | asection *output_section) | |
147 | { | |
148 | asection *stub_sec; | |
149 | flagword flags; | |
861fb55a DJ |
150 | lang_output_section_statement_type *os; |
151 | struct hook_stub_info info; | |
152 | ||
ba85c43e NC |
153 | /* PR 12845: If the input section has been garbage collected it will |
154 | not have its output section set to *ABS*. */ | |
155 | if (bfd_is_abs_section (output_section)) | |
156 | return NULL; | |
157 | ||
861fb55a DJ |
158 | /* Create the stub file, if we haven't already. */ |
159 | if (stub_file == NULL) | |
160 | { | |
161 | stub_file = lang_add_input_file ("linker stubs", | |
162 | lang_input_file_is_fake_enum, | |
163 | NULL); | |
164 | stub_bfd = bfd_create ("linker stubs", link_info.output_bfd); | |
165 | if (stub_bfd == NULL | |
166 | || !bfd_set_arch_mach (stub_bfd, | |
167 | bfd_get_arch (link_info.output_bfd), | |
168 | bfd_get_mach (link_info.output_bfd))) | |
169 | { | |
df5f2391 | 170 | einfo (_("%F%P: can not create BFD: %E\n")); |
861fb55a DJ |
171 | return NULL; |
172 | } | |
173 | stub_bfd->flags |= BFD_LINKER_CREATED; | |
174 | stub_file->the_bfd = stub_bfd; | |
175 | ldlang_add_file (stub_file); | |
176 | } | |
177 | ||
178 | /* Create the section. */ | |
179 | stub_sec = bfd_make_section_anyway (stub_bfd, stub_sec_name); | |
180 | if (stub_sec == NULL) | |
181 | goto err_ret; | |
182 | ||
183 | /* Set the flags. */ | |
184 | flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE | |
185 | | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_KEEP); | |
186 | if (!bfd_set_section_flags (stub_bfd, stub_sec, flags)) | |
187 | goto err_ret; | |
188 | ||
24ef1aa7 | 189 | os = lang_output_section_get (output_section); |
861fb55a DJ |
190 | |
191 | /* Initialize a statement list that contains only the new statement. */ | |
192 | lang_list_init (&info.add); | |
b9c361e0 | 193 | lang_add_section (&info.add, stub_sec, NULL, os); |
861fb55a DJ |
194 | if (info.add.head == NULL) |
195 | goto err_ret; | |
196 | ||
197 | /* Insert the new statement in the appropriate place. */ | |
198 | info.input_section = input_section; | |
199 | if (hook_in_stub (&info, &os->children.head)) | |
200 | return stub_sec; | |
201 | ||
202 | err_ret: | |
d003af55 | 203 | einfo (_("%X%P: can not make stub section: %E\n")); |
861fb55a DJ |
204 | return NULL; |
205 | } | |
206 | ||
207 | /* This is called before the input files are opened. */ | |
208 | ||
209 | static void | |
210 | mips_create_output_section_statements (void) | |
211 | { | |
833794fc MR |
212 | struct elf_link_hash_table *htab; |
213 | ||
214 | htab = elf_hash_table (&link_info); | |
215 | if (is_elf_hash_table (htab) && is_mips_elf (link_info.output_bfd)) | |
47275900 MR |
216 | _bfd_mips_elf_linker_flags (&link_info, insn32, ignore_branch_isa, |
217 | ${gnu_target}); | |
833794fc | 218 | |
861fb55a | 219 | if (is_mips_elf (link_info.output_bfd)) |
3734320d MF |
220 | { |
221 | _bfd_mips_elf_compact_branches (&link_info, compact_branches); | |
222 | _bfd_mips_elf_init_stubs (&link_info, mips_add_stub_section); | |
223 | } | |
861fb55a DJ |
224 | } |
225 | ||
226 | /* This is called after we have merged the private data of the input bfds. */ | |
227 | ||
228 | static void | |
229 | mips_before_allocation (void) | |
230 | { | |
e54cb31a MR |
231 | if (is_mips_elf (link_info.output_bfd)) |
232 | { | |
233 | flagword flags; | |
861fb55a | 234 | |
e54cb31a MR |
235 | flags = elf_elfheader (link_info.output_bfd)->e_flags; |
236 | if (!bfd_link_pic (&link_info) | |
237 | && !link_info.nocopyreloc | |
238 | && (flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) == EF_MIPS_CPIC) | |
239 | _bfd_mips_elf_use_plts_and_copy_relocs (&link_info); | |
240 | } | |
861fb55a DJ |
241 | |
242 | gld${EMULATION_NAME}_before_allocation (); | |
243 | } | |
244 | ||
245 | /* Avoid processing the fake stub_file in vercheck, stat_needed and | |
246 | check_needed routines. */ | |
247 | ||
248 | static void (*real_func) (lang_input_statement_type *); | |
249 | ||
250 | static void mips_for_each_input_file_wrapper (lang_input_statement_type *l) | |
251 | { | |
252 | if (l != stub_file) | |
253 | (*real_func) (l); | |
254 | } | |
255 | ||
256 | static void | |
257 | mips_lang_for_each_input_file (void (*func) (lang_input_statement_type *)) | |
258 | { | |
259 | real_func = func; | |
260 | lang_for_each_input_file (&mips_for_each_input_file_wrapper); | |
261 | } | |
262 | ||
263 | #define lang_for_each_input_file mips_lang_for_each_input_file | |
264 | ||
73934d31 RS |
265 | EOF |
266 | ||
833794fc MR |
267 | # Define some shell vars to insert bits of code into the standard elf |
268 | # parse_args and list_options functions. | |
269 | # | |
270 | PARSE_AND_LIST_PROLOGUE=' | |
2f2da8fd MR |
271 | enum |
272 | { | |
273 | OPTION_INSN32 = 301, | |
8b10b0b3 MR |
274 | OPTION_NO_INSN32, |
275 | OPTION_IGNORE_BRANCH_ISA, | |
3734320d MF |
276 | OPTION_NO_IGNORE_BRANCH_ISA, |
277 | OPTION_COMPACT_BRANCHES, | |
278 | OPTION_NO_COMPACT_BRANCHES | |
2f2da8fd | 279 | }; |
833794fc MR |
280 | ' |
281 | ||
282 | PARSE_AND_LIST_LONGOPTS=' | |
283 | { "insn32", no_argument, NULL, OPTION_INSN32 }, | |
284 | { "no-insn32", no_argument, NULL, OPTION_NO_INSN32 }, | |
8b10b0b3 MR |
285 | { "ignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA }, |
286 | { "no-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA }, | |
3734320d MF |
287 | { "compact-branches", no_argument, NULL, OPTION_COMPACT_BRANCHES }, |
288 | { "no-compact-branches", no_argument, NULL, OPTION_NO_COMPACT_BRANCHES }, | |
833794fc MR |
289 | ' |
290 | ||
291 | PARSE_AND_LIST_OPTIONS=' | |
292 | fprintf (file, _("\ | |
293 | --insn32 Only generate 32-bit microMIPS instructions\n" | |
294 | )); | |
295 | fprintf (file, _("\ | |
296 | --no-insn32 Generate all microMIPS instructions\n" | |
297 | )); | |
8b10b0b3 MR |
298 | fprintf (file, _("\ |
299 | --ignore-branch-isa Accept invalid branch relocations requiring\n\ | |
300 | an ISA mode switch\n" | |
301 | )); | |
302 | fprintf (file, _("\ | |
303 | --no-ignore-branch-isa Reject invalid branch relocations requiring\n\ | |
304 | an ISA mode switch\n" | |
305 | )); | |
3734320d MF |
306 | fprintf (file, _("\ |
307 | --compact-branches Generate compact branches/jumps for MIPS R6\n" | |
308 | )); | |
309 | fprintf (file, _("\ | |
310 | --no-compact-branches Generate delay slot branches/jumps for MIPS R6\n" | |
311 | )); | |
833794fc MR |
312 | ' |
313 | ||
314 | PARSE_AND_LIST_ARGS_CASES=' | |
315 | case OPTION_INSN32: | |
316 | insn32 = TRUE; | |
317 | break; | |
318 | ||
319 | case OPTION_NO_INSN32: | |
320 | insn32 = FALSE; | |
321 | break; | |
8b10b0b3 MR |
322 | |
323 | case OPTION_IGNORE_BRANCH_ISA: | |
324 | ignore_branch_isa = TRUE; | |
325 | break; | |
326 | ||
327 | case OPTION_NO_IGNORE_BRANCH_ISA: | |
328 | ignore_branch_isa = FALSE; | |
329 | break; | |
3734320d MF |
330 | |
331 | case OPTION_COMPACT_BRANCHES: | |
332 | compact_branches = TRUE; | |
333 | break; | |
334 | ||
335 | case OPTION_NO_COMPACT_BRANCHES: | |
336 | compact_branches = FALSE; | |
337 | break; | |
833794fc MR |
338 | ' |
339 | ||
73934d31 | 340 | LDEMUL_AFTER_PARSE=mips_after_parse |
861fb55a DJ |
341 | LDEMUL_BEFORE_ALLOCATION=mips_before_allocation |
342 | LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=mips_create_output_section_statements |