1 # This shell script emits a C file. -*- C -*-
2 # Copyright (C) 2004-2022 Free Software Foundation, Inc.
4 # This file is part of the GNU Binutils.
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.
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.
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,
34 #include "elfxx-mips.h"
36 #define is_mips_elf(bfd) \
37 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
38 && elf_tdata (bfd) != NULL \
39 && elf_object_id (bfd) == MIPS_ELF_DATA)
41 /* Fake input file for stubs. */
42 static lang_input_statement_type *stub_file;
46 static bool ignore_branch_isa;
47 static bool compact_branches;
51 lang_statement_list_type add;
52 asection *input_section;
55 /* Traverse the linker tree to find the spot where the stub goes. */
58 hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
60 lang_statement_union_type *l;
63 for (; (l = *lp) != NULL; lp = &l->header.next)
65 switch (l->header.type)
67 case lang_constructors_statement_enum:
68 ret = hook_in_stub (info, &constructor_list.head);
73 case lang_output_section_statement_enum:
74 ret = hook_in_stub (info,
75 &l->output_section_statement.children.head);
80 case lang_wild_statement_enum:
81 ret = hook_in_stub (info, &l->wild_statement.children.head);
86 case lang_group_statement_enum:
87 ret = hook_in_stub (info, &l->group_statement.children.head);
92 case lang_input_section_enum:
93 if (info->input_section == NULL
94 || l->input_section.section == info->input_section)
96 /* We've found our section. Insert the stub immediately
97 before its associated input section. */
99 *(info->add.tail) = l;
104 case lang_data_statement_enum:
105 case lang_reloc_statement_enum:
106 case lang_object_symbols_statement_enum:
107 case lang_output_statement_enum:
108 case lang_target_statement_enum:
109 case lang_input_statement_enum:
110 case lang_assignment_statement_enum:
111 case lang_padding_statement_enum:
112 case lang_address_statement_enum:
113 case lang_fill_statement_enum:
124 /* Create a new stub section called STUB_SEC_NAME and arrange for it to
125 be linked in OUTPUT_SECTION. The section should go at the beginning of
126 OUTPUT_SECTION if INPUT_SECTION is null, otherwise it must go immediately
127 before INPUT_SECTION. */
130 mips_add_stub_section (const char *stub_sec_name, asection *input_section,
131 asection *output_section)
135 lang_output_section_statement_type *os;
136 struct hook_stub_info info;
138 /* PR 12845: If the input section has been garbage collected it will
139 not have its output section set to *ABS*. */
140 if (bfd_is_abs_section (output_section))
143 /* Create the stub file, if we haven't already. */
144 if (stub_file == NULL)
146 stub_file = lang_add_input_file ("linker stubs",
147 lang_input_file_is_fake_enum,
149 stub_bfd = bfd_create ("linker stubs", link_info.output_bfd);
151 || !bfd_set_arch_mach (stub_bfd,
152 bfd_get_arch (link_info.output_bfd),
153 bfd_get_mach (link_info.output_bfd)))
155 einfo (_("%F%P: can not create BFD: %E\n"));
158 stub_bfd->flags |= BFD_LINKER_CREATED;
159 stub_file->the_bfd = stub_bfd;
160 ldlang_add_file (stub_file);
163 /* Create the section. */
164 stub_sec = bfd_make_section_anyway (stub_bfd, stub_sec_name);
165 if (stub_sec == NULL)
169 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
170 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_KEEP);
171 if (!bfd_set_section_flags (stub_sec, flags))
174 os = lang_output_section_get (output_section);
176 /* Initialize a statement list that contains only the new statement. */
177 lang_list_init (&info.add);
178 lang_add_section (&info.add, stub_sec, NULL, NULL, os);
179 if (info.add.head == NULL)
182 /* Insert the new statement in the appropriate place. */
183 info.input_section = input_section;
184 if (hook_in_stub (&info, &os->children.head))
188 einfo (_("%X%P: can not make stub section: %E\n"));
192 /* This is called before the input files are opened. */
195 mips_create_output_section_statements (void)
197 struct elf_link_hash_table *htab;
199 htab = elf_hash_table (&link_info);
200 if (is_elf_hash_table (&htab->root) && is_mips_elf (link_info.output_bfd))
201 _bfd_mips_elf_linker_flags (&link_info, insn32, ignore_branch_isa,
204 if (is_mips_elf (link_info.output_bfd))
206 _bfd_mips_elf_compact_branches (&link_info, compact_branches);
207 _bfd_mips_elf_init_stubs (&link_info, mips_add_stub_section);
211 /* This is called after we have merged the private data of the input bfds. */
214 mips_before_allocation (void)
216 if (is_mips_elf (link_info.output_bfd))
220 flags = elf_elfheader (link_info.output_bfd)->e_flags;
221 if (!bfd_link_pic (&link_info)
222 && !link_info.nocopyreloc
223 && (flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) == EF_MIPS_CPIC)
224 _bfd_mips_elf_use_plts_and_copy_relocs (&link_info);
227 gld${EMULATION_NAME}_before_allocation ();
232 # Define some shell vars to insert bits of code into the standard elf
233 # parse_args and list_options functions.
235 PARSE_AND_LIST_PROLOGUE='
240 OPTION_IGNORE_BRANCH_ISA,
241 OPTION_NO_IGNORE_BRANCH_ISA,
242 OPTION_COMPACT_BRANCHES,
243 OPTION_NO_COMPACT_BRANCHES
247 PARSE_AND_LIST_LONGOPTS='
248 { "insn32", no_argument, NULL, OPTION_INSN32 },
249 { "no-insn32", no_argument, NULL, OPTION_NO_INSN32 },
250 { "ignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA },
251 { "no-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA },
252 { "compact-branches", no_argument, NULL, OPTION_COMPACT_BRANCHES },
253 { "no-compact-branches", no_argument, NULL, OPTION_NO_COMPACT_BRANCHES },
256 PARSE_AND_LIST_OPTIONS='
258 --insn32 Only generate 32-bit microMIPS instructions\n"
261 --no-insn32 Generate all microMIPS instructions\n"
264 --ignore-branch-isa Accept invalid branch relocations requiring\n\
265 an ISA mode switch\n"
268 --no-ignore-branch-isa Reject invalid branch relocations requiring\n\
269 an ISA mode switch\n"
272 --compact-branches Generate compact branches/jumps for MIPS R6\n"
275 --no-compact-branches Generate delay slot branches/jumps for MIPS R6\n"
279 PARSE_AND_LIST_ARGS_CASES='
284 case OPTION_NO_INSN32:
288 case OPTION_IGNORE_BRANCH_ISA:
289 ignore_branch_isa = true;
292 case OPTION_NO_IGNORE_BRANCH_ISA:
293 ignore_branch_isa = false;
296 case OPTION_COMPACT_BRANCHES:
297 compact_branches = true;
300 case OPTION_NO_COMPACT_BRANCHES:
301 compact_branches = false;
305 LDEMUL_BEFORE_ALLOCATION=mips_before_allocation
306 LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=mips_create_output_section_statements