1# This shell script emits a C file. -*- C -*- 2# Copyright (C) 2004-2024 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 21case ${target} in 22 *-*-*gnu*) 23 gnu_target=true 24 ;; 25 *) 26 gnu_target=false 27 ;; 28esac 29 30fragment <<EOF 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 \ 39 && elf_object_id (bfd) == MIPS_ELF_DATA) 40 41/* Fake input file for stubs. */ 42static lang_input_statement_type *stub_file; 43static bfd *stub_bfd; 44 45static bool insn32; 46static bool ignore_branch_isa; 47static bool compact_branches; 48 49struct hook_stub_info 50{ 51 lang_statement_list_type add; 52 asection *input_section; 53}; 54 55/* Traverse the linker tree to find the spot where the stub goes. */ 56 57static bool 58hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp) 59{ 60 lang_statement_union_type *l; 61 bool ret; 62 63 for (; (l = *lp) != NULL; lp = &l->header.next) 64 { 65 switch (l->header.type) 66 { 67 case lang_constructors_statement_enum: 68 ret = hook_in_stub (info, &constructor_list.head); 69 if (ret) 70 return ret; 71 break; 72 73 case lang_output_section_statement_enum: 74 ret = hook_in_stub (info, 75 &l->output_section_statement.children.head); 76 if (ret) 77 return ret; 78 break; 79 80 case lang_wild_statement_enum: 81 ret = hook_in_stub (info, &l->wild_statement.children.head); 82 if (ret) 83 return ret; 84 break; 85 86 case lang_group_statement_enum: 87 ret = hook_in_stub (info, &l->group_statement.children.head); 88 if (ret) 89 return ret; 90 break; 91 92 case lang_input_section_enum: 93 if (info->input_section == NULL 94 || l->input_section.section == info->input_section) 95 { 96 /* We've found our section. Insert the stub immediately 97 before its associated input section. */ 98 *lp = info->add.head; 99 *(info->add.tail) = l; 100 return true; 101 } 102 break; 103 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: 114 break; 115 116 default: 117 FAIL (); 118 break; 119 } 120 } 121 return false; 122} 123 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. */ 128 129static asection * 130mips_add_stub_section (const char *stub_sec_name, asection *input_section, 131 asection *output_section) 132{ 133 asection *stub_sec; 134 flagword flags; 135 lang_output_section_statement_type *os; 136 struct hook_stub_info info; 137 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)) 141 return NULL; 142 143 /* Create the stub file, if we haven't already. */ 144 if (stub_file == NULL) 145 { 146 stub_file = lang_add_input_file ("linker stubs", 147 lang_input_file_is_fake_enum, 148 NULL); 149 stub_bfd = bfd_create ("linker stubs", link_info.output_bfd); 150 if (stub_bfd == NULL 151 || !bfd_set_arch_mach (stub_bfd, 152 bfd_get_arch (link_info.output_bfd), 153 bfd_get_mach (link_info.output_bfd))) 154 { 155 einfo (_("%F%P: can not create BFD: %E\n")); 156 return NULL; 157 } 158 stub_bfd->flags |= BFD_LINKER_CREATED; 159 stub_file->the_bfd = stub_bfd; 160 ldlang_add_file (stub_file); 161 } 162 163 /* Create the section. */ 164 stub_sec = bfd_make_section_anyway (stub_bfd, stub_sec_name); 165 if (stub_sec == NULL) 166 goto err_ret; 167 168 /* Set the flags. */ 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)) 172 goto err_ret; 173 174 os = lang_output_section_get (output_section); 175 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) 180 goto err_ret; 181 182 /* Insert the new statement in the appropriate place. */ 183 info.input_section = input_section; 184 if (hook_in_stub (&info, &os->children.head)) 185 return stub_sec; 186 187 err_ret: 188 einfo (_("%X%P: can not make stub section: %E\n")); 189 return NULL; 190} 191 192/* This is called before the input files are opened. */ 193 194static void 195mips_create_output_section_statements (void) 196{ 197 struct elf_link_hash_table *htab; 198 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, 202 ${gnu_target}); 203 204 if (is_mips_elf (link_info.output_bfd)) 205 { 206 _bfd_mips_elf_compact_branches (&link_info, compact_branches); 207 _bfd_mips_elf_init_stubs (&link_info, mips_add_stub_section); 208 } 209} 210 211/* This is called after we have merged the private data of the input bfds. */ 212 213static void 214mips_before_allocation (void) 215{ 216 if (is_mips_elf (link_info.output_bfd)) 217 { 218 flagword flags; 219 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); 225 } 226 227 gld${EMULATION_NAME}_before_allocation (); 228} 229 230EOF 231 232# Define some shell vars to insert bits of code into the standard elf 233# parse_args and list_options functions. 234# 235PARSE_AND_LIST_PROLOGUE=' 236enum 237 { 238 OPTION_INSN32 = 301, 239 OPTION_NO_INSN32, 240 OPTION_IGNORE_BRANCH_ISA, 241 OPTION_NO_IGNORE_BRANCH_ISA, 242 OPTION_COMPACT_BRANCHES, 243 OPTION_NO_COMPACT_BRANCHES 244 }; 245' 246 247PARSE_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 }, 254' 255 256PARSE_AND_LIST_OPTIONS=' 257 fprintf (file, _("\ 258 --insn32 Only generate 32-bit microMIPS instructions\n" 259 )); 260 fprintf (file, _("\ 261 --no-insn32 Generate all microMIPS instructions\n" 262 )); 263 fprintf (file, _("\ 264 --ignore-branch-isa Accept invalid branch relocations requiring\n\ 265 an ISA mode switch\n" 266 )); 267 fprintf (file, _("\ 268 --no-ignore-branch-isa Reject invalid branch relocations requiring\n\ 269 an ISA mode switch\n" 270 )); 271 fprintf (file, _("\ 272 --compact-branches Generate compact branches/jumps for MIPS R6\n" 273 )); 274 fprintf (file, _("\ 275 --no-compact-branches Generate delay slot branches/jumps for MIPS R6\n" 276 )); 277' 278 279PARSE_AND_LIST_ARGS_CASES=' 280 case OPTION_INSN32: 281 insn32 = true; 282 break; 283 284 case OPTION_NO_INSN32: 285 insn32 = false; 286 break; 287 288 case OPTION_IGNORE_BRANCH_ISA: 289 ignore_branch_isa = true; 290 break; 291 292 case OPTION_NO_IGNORE_BRANCH_ISA: 293 ignore_branch_isa = false; 294 break; 295 296 case OPTION_COMPACT_BRANCHES: 297 compact_branches = true; 298 break; 299 300 case OPTION_NO_COMPACT_BRANCHES: 301 compact_branches = false; 302 break; 303' 304 305LDEMUL_BEFORE_ALLOCATION=mips_before_allocation 306LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=mips_create_output_section_statements 307