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