m68hc1xelf.em 11 KB

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