avrelf.em 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. # This shell script emits a C file. -*- C -*-
  2. # Copyright (C) 2006-2015 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. # This file is sourced from elf32.em, and defines extra avr-elf specific
  21. # routines. It is used to generate the trampolines for the avr6 family
  22. # of devices where one needs to address the issue that it is not possible
  23. # to reach the whole program memory by using 16 bit pointers.
  24. fragment <<EOF
  25. #include "elf32-avr.h"
  26. #include "ldctor.h"
  27. #include "elf/avr.h"
  28. /* The fake file and it's corresponding section meant to hold
  29. the linker stubs if needed. */
  30. static lang_input_statement_type *stub_file;
  31. static asection *avr_stub_section;
  32. /* Variables set by the command-line parameters and transfered
  33. to the bfd without use of global shared variables. */
  34. static bfd_boolean avr_no_stubs = FALSE;
  35. static bfd_boolean avr_debug_relax = FALSE;
  36. static bfd_boolean avr_debug_stubs = FALSE;
  37. static bfd_boolean avr_replace_call_ret_sequences = TRUE;
  38. static bfd_vma avr_pc_wrap_around = 0x10000000;
  39. /* Transfers information to the bfd frontend. */
  40. static void
  41. avr_elf_set_global_bfd_parameters (void)
  42. {
  43. elf32_avr_setup_params (& link_info,
  44. stub_file->the_bfd,
  45. avr_stub_section,
  46. avr_no_stubs,
  47. avr_debug_stubs,
  48. avr_debug_relax,
  49. avr_pc_wrap_around,
  50. avr_replace_call_ret_sequences);
  51. }
  52. /* Makes a conservative estimate of the trampoline section size that could
  53. be corrected later on. */
  54. static void
  55. avr_elf_${EMULATION_NAME}_before_allocation (void)
  56. {
  57. int ret;
  58. gld${EMULATION_NAME}_before_allocation ();
  59. /* We only need stubs for avr6, avrxmega6, and avrxmega7. */
  60. if (strcmp ("${EMULATION_NAME}","avr6")
  61. && strcmp ("${EMULATION_NAME}","avrxmega6")
  62. && strcmp ("${EMULATION_NAME}","avrxmega7") )
  63. avr_no_stubs = TRUE;
  64. avr_elf_set_global_bfd_parameters ();
  65. /* If generating a relocatable output file, then
  66. we don't have to generate the trampolines. */
  67. if (bfd_link_relocatable (&link_info))
  68. avr_no_stubs = TRUE;
  69. if (avr_no_stubs)
  70. return;
  71. ret = elf32_avr_setup_section_lists (link_info.output_bfd, &link_info);
  72. if (ret < 0)
  73. einfo ("%X%P: can not setup the input section list: %E\n");
  74. if (ret <= 0)
  75. return;
  76. /* Call into the BFD backend to do the real "stub"-work. */
  77. if (! elf32_avr_size_stubs (link_info.output_bfd, &link_info, TRUE))
  78. einfo ("%X%P: can not size stub section: %E\n");
  79. }
  80. /* This is called before the input files are opened. We create a new
  81. fake input file to hold the stub section and generate the section itself. */
  82. static void
  83. avr_elf_create_output_section_statements (void)
  84. {
  85. flagword flags;
  86. stub_file = lang_add_input_file ("linker stubs",
  87. lang_input_file_is_fake_enum,
  88. NULL);
  89. stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd);
  90. if (stub_file->the_bfd == NULL
  91. || !bfd_set_arch_mach (stub_file->the_bfd,
  92. bfd_get_arch (link_info.output_bfd),
  93. bfd_get_mach (link_info.output_bfd)))
  94. {
  95. einfo ("%X%P: can not create stub BFD %E\n");
  96. return;
  97. }
  98. /* Now we add the stub section. */
  99. flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
  100. | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP);
  101. avr_stub_section = bfd_make_section_anyway_with_flags (stub_file->the_bfd,
  102. ".trampolines",
  103. flags);
  104. if (avr_stub_section == NULL)
  105. goto err_ret;
  106. avr_stub_section->alignment_power = 1;
  107. ldlang_add_file (stub_file);
  108. return;
  109. err_ret:
  110. einfo ("%X%P: can not make stub section: %E\n");
  111. return;
  112. }
  113. /* Re-calculates the size of the stubs so that we won't waste space. */
  114. static void
  115. avr_elf_after_allocation (void)
  116. {
  117. if (!avr_no_stubs && ! RELAXATION_ENABLED)
  118. {
  119. /* If relaxing, elf32_avr_size_stubs will be called from
  120. elf32_avr_relax_section. */
  121. if (!elf32_avr_size_stubs (link_info.output_bfd, &link_info, TRUE))
  122. einfo ("%X%P: can not size stub section: %E\n");
  123. }
  124. gld${EMULATION_NAME}_after_allocation ();
  125. /* Now build the linker stubs. */
  126. if (!avr_no_stubs)
  127. {
  128. if (!elf32_avr_build_stubs (&link_info))
  129. einfo ("%X%P: can not build stubs: %E\n");
  130. }
  131. }
  132. static void
  133. avr_elf_before_parse (void)
  134. {
  135. /* Don't create a demand-paged executable, since this feature isn't
  136. meaningful in AVR. */
  137. config.magic_demand_paged = FALSE;
  138. gld${EMULATION_NAME}_before_parse ();
  139. }
  140. static void
  141. avr_finish (void)
  142. {
  143. bfd *abfd;
  144. bfd_boolean avr_link_relax;
  145. if (bfd_link_relocatable (&link_info))
  146. {
  147. avr_link_relax = TRUE;
  148. for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
  149. {
  150. /* Don't let the linker stubs prevent the final object being
  151. marked as link-relax ready. */
  152. if ((elf_elfheader (abfd)->e_flags
  153. & EF_AVR_LINKRELAX_PREPARED) == 0
  154. && abfd != stub_file->the_bfd)
  155. {
  156. avr_link_relax = FALSE;
  157. break;
  158. }
  159. }
  160. }
  161. else
  162. {
  163. avr_link_relax = RELAXATION_ENABLED;
  164. }
  165. abfd = link_info.output_bfd;
  166. if (avr_link_relax)
  167. elf_elfheader (abfd)->e_flags |= EF_AVR_LINKRELAX_PREPARED;
  168. else
  169. elf_elfheader (abfd)->e_flags &= ~EF_AVR_LINKRELAX_PREPARED;
  170. finish_default ();
  171. }
  172. EOF
  173. PARSE_AND_LIST_PROLOGUE='
  174. #define OPTION_NO_CALL_RET_REPLACEMENT 301
  175. #define OPTION_PMEM_WRAP_AROUND 302
  176. #define OPTION_NO_STUBS 303
  177. #define OPTION_DEBUG_STUBS 304
  178. #define OPTION_DEBUG_RELAX 305
  179. '
  180. PARSE_AND_LIST_LONGOPTS='
  181. { "no-call-ret-replacement", no_argument,
  182. NULL, OPTION_NO_CALL_RET_REPLACEMENT},
  183. { "pmem-wrap-around", required_argument,
  184. NULL, OPTION_PMEM_WRAP_AROUND},
  185. { "no-stubs", no_argument,
  186. NULL, OPTION_NO_STUBS},
  187. { "debug-stubs", no_argument,
  188. NULL, OPTION_DEBUG_STUBS},
  189. { "debug-relax", no_argument,
  190. NULL, OPTION_DEBUG_RELAX},
  191. '
  192. PARSE_AND_LIST_OPTIONS='
  193. fprintf (file, _(" --pmem-wrap-around=<val> "
  194. "Make the linker relaxation machine assume that a\n"
  195. " "
  196. " program counter wrap-around occures at address\n"
  197. " "
  198. " <val>. Supported values: 8k, 16k, 32k and 64k.\n"));
  199. fprintf (file, _(" --no-call-ret-replacement "
  200. "The relaxation machine normally will\n"
  201. " "
  202. " substitute two immediately following call/ret\n"
  203. " "
  204. " instructions by a single jump instruction.\n"
  205. " "
  206. " This option disables this optimization.\n"));
  207. fprintf (file, _(" --no-stubs "
  208. "If the linker detects to attempt to access\n"
  209. " "
  210. " an instruction beyond 128k by a reloc that\n"
  211. " "
  212. " is limited to 128k max, it inserts a jump\n"
  213. " "
  214. " stub. You can de-active this with this switch.\n"));
  215. fprintf (file, _(" --debug-stubs "
  216. "Used for debugging avr-ld.\n"));
  217. fprintf (file, _(" --debug-relax "
  218. "Used for debugging avr-ld.\n"));
  219. '
  220. PARSE_AND_LIST_ARGS_CASES='
  221. case OPTION_PMEM_WRAP_AROUND:
  222. {
  223. /* This variable is defined in the bfd library. */
  224. if ((!strcmp (optarg,"32k")) || (!strcmp (optarg,"32K")))
  225. avr_pc_wrap_around = 32768;
  226. else if ((!strcmp (optarg,"8k")) || (!strcmp (optarg,"8K")))
  227. avr_pc_wrap_around = 8192;
  228. else if ((!strcmp (optarg,"16k")) || (!strcmp (optarg,"16K")))
  229. avr_pc_wrap_around = 16384;
  230. else if ((!strcmp (optarg,"64k")) || (!strcmp (optarg,"64K")))
  231. avr_pc_wrap_around = 0x10000;
  232. else
  233. return FALSE;
  234. }
  235. break;
  236. case OPTION_DEBUG_STUBS:
  237. avr_debug_stubs = TRUE;
  238. break;
  239. case OPTION_DEBUG_RELAX:
  240. avr_debug_relax = TRUE;
  241. break;
  242. case OPTION_NO_STUBS:
  243. avr_no_stubs = TRUE;
  244. break;
  245. case OPTION_NO_CALL_RET_REPLACEMENT:
  246. {
  247. /* This variable is defined in the bfd library. */
  248. avr_replace_call_ret_sequences = FALSE;
  249. }
  250. break;
  251. '
  252. #
  253. # Put these extra avr-elf routines in ld_${EMULATION_NAME}_emulation
  254. #
  255. LDEMUL_BEFORE_PARSE=avr_elf_before_parse
  256. LDEMUL_BEFORE_ALLOCATION=avr_elf_${EMULATION_NAME}_before_allocation
  257. LDEMUL_AFTER_ALLOCATION=avr_elf_after_allocation
  258. LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=avr_elf_create_output_section_statements
  259. LDEMUL_FINISH=avr_finish