1# This shell script emits a C file. -*- C -*- 2# Copyright (C) 1991-2018 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 22# This file is sourced from elf32.em, and defines extra m68hc12-elf 23# and m68hc11-elf specific routines. It is used to generate the 24# HC11/HC12 trampolines to call a far function by using a normal 'jsr/bsr'. 25# 26# - The HC11/HC12 relocations are checked to see if there is a 27# R_M68HC11_16 relocation to a symbol marked with STO_M68HC12_FAR. 28# This relocation cannot be made on the symbol but must be made on 29# its trampoline 30# The trampolines to generate are collected during this pass 31# (See elf32_m68hc11_size_stubs) 32# 33# - The trampolines are generated in a ".tramp" section. The generation 34# takes care of HC11 and HC12 specificities. 35# (See elf32_m68hc11_build_stubs) 36# 37# - During relocation the R_M68HC11_16 relocation to the far symbols 38# are redirected to the trampoline that was generated. 39# 40# Copied from hppaelf and adapted for M68HC11/M68HC12 specific needs. 41# 42fragment <<EOF 43 44#include "ldctor.h" 45#include "elf32-m68hc1x.h" 46 47static asection *m68hc11elf_add_stub_section (const char *, asection *); 48 49/* Fake input file for stubs. */ 50static lang_input_statement_type *stub_file; 51 52/* By default the HC11/HC12 trampolines to call a far function using 53 a normal 'bsr' and 'jsr' convention are generated during the link. 54 The --no-trampoline option prevents that. */ 55static int no_trampoline = 0; 56 57/* Name of memory bank window in the MEMORY description. 58 This is set by --bank-window option. */ 59static const char* bank_window_name = 0; 60 61static void 62m68hc11_elf_${EMULATION_NAME}_before_allocation (void) 63{ 64 lang_memory_region_type* region; 65 int ret; 66 67 gld${EMULATION_NAME}_before_allocation (); 68 69 if (bfd_get_flavour (link_info.output_bfd) != bfd_target_elf_flavour) 70 return; 71 72 /* If generating a relocatable output file, then we don't 73 have to generate the trampolines. */ 74 if (bfd_link_relocatable (&link_info)) 75 return; 76 77 ret = elf32_m68hc11_setup_section_lists (link_info.output_bfd, &link_info); 78 if (ret != 0 && no_trampoline == 0) 79 { 80 if (ret < 0) 81 { 82 einfo (_("%X%P: can not size stub section: %E\n")); 83 return; 84 } 85 86 /* Call into the BFD backend to do the real work. */ 87 if (!elf32_m68hc11_size_stubs (link_info.output_bfd, 88 stub_file->the_bfd, 89 &link_info, 90 &m68hc11elf_add_stub_section)) 91 { 92 einfo (_("%X%P: can not size stub section: %E\n")); 93 return; 94 } 95 } 96 97 if (bank_window_name == 0) 98 return; 99 100 /* The 'bank_window_name' memory region is a special region that describes 101 the memory bank window to access to paged memory. For 68HC12 102 this is fixed and should be: 103 104 window (rx) : ORIGIN = 0x8000, LENGTH = 16K 105 106 But for 68HC11 this is board specific. The definition of such 107 memory region allows to control how this paged memory is accessed. */ 108 region = lang_memory_region_lookup (bank_window_name, FALSE); 109 110 /* Check the length to see if it was defined in the script. */ 111 if (region->length != 0) 112 { 113 struct m68hc11_page_info *pinfo; 114 unsigned i; 115 116 /* Get default values */ 117 m68hc11_elf_get_bank_parameters (&link_info); 118 pinfo = &m68hc11_elf_hash_table (&link_info)->pinfo; 119 120 /* And override them with the region definition. */ 121 pinfo->bank_size = region->length; 122 pinfo->bank_shift = 0; 123 for (i = pinfo->bank_size; i != 0; i >>= 1) 124 pinfo->bank_shift++; 125 pinfo->bank_shift--; 126 pinfo->bank_size = 1L << pinfo->bank_shift; 127 pinfo->bank_mask = (1 << pinfo->bank_shift) - 1; 128 pinfo->bank_physical = region->origin; 129 pinfo->bank_physical_end = region->origin + pinfo->bank_size; 130 131 if (pinfo->bank_size != region->length) 132 { 133 einfo (_("%P: warning: the size of the 'window' memory region " 134 "is not a power of 2; its size %d is truncated to %d\n"), 135 region->length, pinfo->bank_size); 136 } 137 } 138} 139 140/* This is called before the input files are opened. We create a new 141 fake input file to hold the stub sections. */ 142 143static void 144m68hc11elf_create_output_section_statements (void) 145{ 146 if (bfd_get_flavour (link_info.output_bfd) != bfd_target_elf_flavour) 147 { 148 einfo (_("%X%P: changing output format whilst linking " 149 "is not supported\n")); 150 return; 151 } 152 153 stub_file = lang_add_input_file ("linker stubs", 154 lang_input_file_is_fake_enum, 155 NULL); 156 stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd); 157 if (stub_file->the_bfd == NULL 158 || !bfd_set_arch_mach (stub_file->the_bfd, 159 bfd_get_arch (link_info.output_bfd), 160 bfd_get_mach (link_info.output_bfd))) 161 { 162 einfo (_("%F%P: can not create BFD: %E\n")); 163 return; 164 } 165 166 ldlang_add_file (stub_file); 167} 168 169 170struct hook_stub_info 171{ 172 lang_statement_list_type add; 173 asection *input_section; 174}; 175 176/* Traverse the linker tree to find the spot where the stub goes. */ 177 178static bfd_boolean 179hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp) 180{ 181 lang_statement_union_type *l; 182 bfd_boolean ret; 183 184 for (; (l = *lp) != NULL; lp = &l->header.next) 185 { 186 switch (l->header.type) 187 { 188 case lang_constructors_statement_enum: 189 ret = hook_in_stub (info, &constructor_list.head); 190 if (ret) 191 return ret; 192 break; 193 194 case lang_output_section_statement_enum: 195 ret = hook_in_stub (info, 196 &l->output_section_statement.children.head); 197 if (ret) 198 return ret; 199 break; 200 201 case lang_wild_statement_enum: 202 ret = hook_in_stub (info, &l->wild_statement.children.head); 203 if (ret) 204 return ret; 205 break; 206 207 case lang_group_statement_enum: 208 ret = hook_in_stub (info, &l->group_statement.children.head); 209 if (ret) 210 return ret; 211 break; 212 213 case lang_input_section_enum: 214 if (l->input_section.section == info->input_section 215 || strcmp (bfd_get_section_name (l->input_section.section->owner, 216 l->input_section.section), 217 bfd_get_section_name (info->input_section->owner, 218 info->input_section)) == 0) 219 { 220 /* We've found our section. Insert the stub immediately 221 before its associated input section. */ 222 *lp = info->add.head; 223 *(info->add.tail) = l; 224 return TRUE; 225 } 226 break; 227 228 case lang_data_statement_enum: 229 case lang_reloc_statement_enum: 230 case lang_object_symbols_statement_enum: 231 case lang_output_statement_enum: 232 case lang_target_statement_enum: 233 case lang_input_statement_enum: 234 case lang_assignment_statement_enum: 235 case lang_padding_statement_enum: 236 case lang_address_statement_enum: 237 case lang_fill_statement_enum: 238 break; 239 240 default: 241 FAIL (); 242 break; 243 } 244 } 245 return FALSE; 246} 247 248 249/* Call-back for elf32_m68hc11_size_stubs. */ 250 251/* Create a new stub section, and arrange for it to be linked 252 immediately before INPUT_SECTION. */ 253 254static asection * 255m68hc11elf_add_stub_section (const char *stub_sec_name, 256 asection *tramp_section) 257{ 258 asection *stub_sec; 259 flagword flags; 260 asection *output_section; 261 lang_output_section_statement_type *os; 262 struct hook_stub_info info; 263 264 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE 265 | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP); 266 stub_sec = bfd_make_section_anyway_with_flags (stub_file->the_bfd, 267 stub_sec_name, flags); 268 if (stub_sec == NULL) 269 goto err_ret; 270 271 output_section = tramp_section->output_section; 272 os = lang_output_section_get (output_section); 273 274 /* Try to put the new section at the same place as an existing 275 .tramp section. Such .tramp section exists in most cases and 276 contains the trampoline code. This way we put the generated trampoline 277 at the correct place. */ 278 info.input_section = tramp_section; 279 lang_list_init (&info.add); 280 lang_add_section (&info.add, stub_sec, NULL, os); 281 282 if (info.add.head == NULL) 283 goto err_ret; 284 285 if (hook_in_stub (&info, &os->children.head)) 286 return stub_sec; 287 288 err_ret: 289 einfo (_("%X%P: can not make stub section: %E\n")); 290 return NULL; 291} 292 293/* For the 68HC12 we use this opportunity to build linker stubs. */ 294 295static void 296m68hc11elf_after_allocation (void) 297{ 298 if (bfd_get_flavour (link_info.output_bfd) == bfd_target_elf_flavour) 299 { 300 /* Now build the linker stubs. */ 301 if (stub_file->the_bfd->sections != NULL) 302 { 303 /* Call again the trampoline analyzer to initialize the trampoline 304 stubs with the correct symbol addresses. Since there could have 305 been relaxation, the symbol addresses that were found during 306 first call may no longer be correct. */ 307 if (!elf32_m68hc11_size_stubs (link_info.output_bfd, 308 stub_file->the_bfd, 309 &link_info, 0)) 310 { 311 einfo (_("%X%P: can not size stub section: %E\n")); 312 return; 313 } 314 if (!elf32_m68hc11_build_stubs (link_info.output_bfd, &link_info)) 315 einfo (_("%X%P: can not build stubs: %E\n")); 316 } 317 } 318 319 gld${EMULATION_NAME}_after_allocation (); 320} 321 322 323/* Avoid processing the fake stub_file in vercheck, stat_needed and 324 check_needed routines. */ 325 326static void (*real_func) (lang_input_statement_type *); 327 328static void m68hc11_for_each_input_file_wrapper (lang_input_statement_type *l) 329{ 330 if (l != stub_file) 331 (*real_func) (l); 332} 333 334static void 335m68hc11_lang_for_each_input_file (void (*func) (lang_input_statement_type *)) 336{ 337 real_func = func; 338 lang_for_each_input_file (&m68hc11_for_each_input_file_wrapper); 339} 340 341#define lang_for_each_input_file m68hc11_lang_for_each_input_file 342 343EOF 344 345# Define some shell vars to insert bits of code into the standard elf 346# parse_args and list_options functions. 347# 348PARSE_AND_LIST_PROLOGUE=' 349#define OPTION_NO_TRAMPOLINE 300 350#define OPTION_BANK_WINDOW 301 351' 352 353# The options are repeated below so that no abbreviations are allowed. 354# Otherwise -s matches stub-group-size 355PARSE_AND_LIST_LONGOPTS=' 356 { "no-trampoline", no_argument, NULL, OPTION_NO_TRAMPOLINE }, 357 { "bank-window", required_argument, NULL, OPTION_BANK_WINDOW }, 358' 359 360PARSE_AND_LIST_OPTIONS=' 361 fprintf (file, _( 362" --no-trampoline Do not generate the far trampolines used to call\n" 363" a far function using 'jsr' or 'bsr'\n")); 364 fprintf (file, _( 365" --bank-window NAME Specify the name of the memory region describing\n" 366" the layout of the memory bank window\n")); 367' 368 369PARSE_AND_LIST_ARGS_CASES=' 370 case OPTION_NO_TRAMPOLINE: 371 no_trampoline = 1; 372 break; 373 case OPTION_BANK_WINDOW: 374 bank_window_name = optarg; 375 break; 376' 377 378# Put these extra m68hc11elf routines in ld_${EMULATION_NAME}_emulation 379# 380LDEMUL_BEFORE_ALLOCATION=m68hc11_elf_${EMULATION_NAME}_before_allocation 381LDEMUL_AFTER_ALLOCATION=m68hc11elf_after_allocation 382LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=m68hc11elf_create_output_section_statements 383