m68kelf.em 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. # This shell script emits a C file. -*- C -*-
  2. # Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. # Written by Michael Sokolov <msokolov@ivan.Harhan.ORG>, based on armelf.em
  4. #
  5. # This file is part of the GNU Binutils.
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  20. # MA 02110-1301, USA.
  21. # This file is sourced from elf32.em, and defines some extra routines for m68k
  22. # embedded systems using ELF and for some other systems using m68k ELF. While
  23. # it is sourced from elf32.em for all m68k ELF configurations, here we include
  24. # only the features we want depending on the configuration.
  25. case ${target} in
  26. m68*-*-elf)
  27. echo "#define SUPPORT_EMBEDDED_RELOCS" >>e${EMULATION_NAME}.c
  28. ;;
  29. esac
  30. case ${target} in
  31. *-linux*)
  32. # Don't use multi-GOT by default due to glibc linker's assumption
  33. # that GOT pointer points to GOT[0].
  34. # got_handling_target_default=GOT_HANDLING_MULTIGOT
  35. got_handling_target_default=GOT_HANDLING_SINGLE
  36. ;;
  37. *)
  38. got_handling_target_default=GOT_HANDLING_SINGLE
  39. ;;
  40. esac
  41. fragment <<EOF
  42. #define GOT_HANDLING_SINGLE (0)
  43. #define GOT_HANDLING_NEGATIVE (1)
  44. #define GOT_HANDLING_MULTIGOT (2)
  45. #define GOT_HANDLING_TARGET_DEFAULT ${got_handling_target_default}
  46. /* How to generate GOT. */
  47. static int got_handling = GOT_HANDLING_DEFAULT;
  48. #ifdef SUPPORT_EMBEDDED_RELOCS
  49. static void check_sections (bfd *, asection *, void *);
  50. #endif
  51. /* This function is run after all the input files have been opened. */
  52. static void
  53. m68k_elf_after_open (void)
  54. {
  55. /* Call the standard elf routine. */
  56. gld${EMULATION_NAME}_after_open ();
  57. #ifdef SUPPORT_EMBEDDED_RELOCS
  58. if (command_line.embedded_relocs
  59. && (!bfd_link_relocatable (&link_info)))
  60. {
  61. bfd *abfd;
  62. /* In the embedded relocs mode we create a .emreloc section for each
  63. input file with a nonzero .data section. The BFD backend will fill in
  64. these sections with magic numbers which can be used to relocate the
  65. data section at run time. */
  66. for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
  67. {
  68. asection *datasec;
  69. /* As first-order business, make sure that each input BFD is either
  70. COFF or ELF. We need to call a special BFD backend function to
  71. generate the embedded relocs, and we have such functions only for
  72. COFF and ELF. */
  73. if (bfd_get_flavour (abfd) != bfd_target_coff_flavour
  74. && bfd_get_flavour (abfd) != bfd_target_elf_flavour)
  75. einfo ("%F%B: all input objects must be COFF or ELF for --embedded-relocs\n");
  76. datasec = bfd_get_section_by_name (abfd, ".data");
  77. /* Note that we assume that the reloc_count field has already
  78. been set up. We could call bfd_get_reloc_upper_bound, but
  79. that returns the size of a memory buffer rather than a reloc
  80. count. We do not want to call bfd_canonicalize_reloc,
  81. because although it would always work it would force us to
  82. read in the relocs into BFD canonical form, which would waste
  83. a significant amount of time and memory. */
  84. if (datasec != NULL && datasec->reloc_count > 0)
  85. {
  86. asection *relsec;
  87. relsec = bfd_make_section_with_flags (abfd, ".emreloc",
  88. (SEC_ALLOC
  89. | SEC_LOAD
  90. | SEC_HAS_CONTENTS
  91. | SEC_IN_MEMORY));
  92. if (relsec == NULL
  93. || ! bfd_set_section_alignment (abfd, relsec, 2)
  94. || ! bfd_set_section_size (abfd, relsec,
  95. datasec->reloc_count * 12))
  96. einfo ("%F%B: can not create .emreloc section: %E\n");
  97. }
  98. /* Double check that all other data sections are empty, as is
  99. required for embedded PIC code. */
  100. bfd_map_over_sections (abfd, check_sections, datasec);
  101. }
  102. }
  103. #endif /* SUPPORT_EMBEDDED_RELOCS */
  104. }
  105. #ifdef SUPPORT_EMBEDDED_RELOCS
  106. /* Check that of the data sections, only the .data section has
  107. relocs. This is called via bfd_map_over_sections. */
  108. static void
  109. check_sections (bfd *abfd, asection *sec, void *datasec)
  110. {
  111. if ((bfd_get_section_flags (abfd, sec) & SEC_DATA)
  112. && sec != datasec
  113. && sec->reloc_count != 0)
  114. einfo ("%B%X: section %s has relocs; can not use --embedded-relocs\n",
  115. abfd, bfd_get_section_name (abfd, sec));
  116. }
  117. #endif /* SUPPORT_EMBEDDED_RELOCS */
  118. /* This function is called after the section sizes and offsets have
  119. been set. */
  120. static void
  121. m68k_elf_after_allocation (void)
  122. {
  123. /* Call the standard elf routine. */
  124. gld${EMULATION_NAME}_after_allocation ();
  125. #ifdef SUPPORT_EMBEDDED_RELOCS
  126. if (command_line.embedded_relocs
  127. && (!bfd_link_relocatable (&link_info)))
  128. {
  129. bfd *abfd;
  130. /* If we are generating embedded relocs, call a special BFD backend
  131. routine to do the work. */
  132. for (abfd = link_info.input_bfds; abfd != NULL; abfd = abfd->link.next)
  133. {
  134. asection *datasec, *relsec;
  135. char *errmsg;
  136. datasec = bfd_get_section_by_name (abfd, ".data");
  137. if (datasec == NULL || datasec->reloc_count == 0)
  138. continue;
  139. relsec = bfd_get_section_by_name (abfd, ".emreloc");
  140. ASSERT (relsec != NULL);
  141. if (bfd_get_flavour (abfd) == bfd_target_coff_flavour)
  142. {
  143. if (! bfd_m68k_coff_create_embedded_relocs (abfd, &link_info,
  144. datasec, relsec,
  145. &errmsg))
  146. {
  147. if (errmsg == NULL)
  148. einfo ("%B%X: can not create runtime reloc information: %E\n",
  149. abfd);
  150. else
  151. einfo ("%X%B: can not create runtime reloc information: %s\n",
  152. abfd, errmsg);
  153. }
  154. }
  155. else if (bfd_get_flavour (abfd) == bfd_target_elf_flavour)
  156. {
  157. if (! bfd_m68k_elf32_create_embedded_relocs (abfd, &link_info,
  158. datasec, relsec,
  159. &errmsg))
  160. {
  161. if (errmsg == NULL)
  162. einfo ("%B%X: can not create runtime reloc information: %E\n",
  163. abfd);
  164. else
  165. einfo ("%X%B: can not create runtime reloc information: %s\n",
  166. abfd, errmsg);
  167. }
  168. }
  169. else
  170. abort ();
  171. }
  172. }
  173. #endif /* SUPPORT_EMBEDDED_RELOCS */
  174. }
  175. /* This is a convenient point to tell BFD about target specific flags.
  176. After the output has been created, but before inputs are read. */
  177. static void
  178. elf_m68k_create_output_section_statements (void)
  179. {
  180. bfd_elf_m68k_set_target_options (&link_info, got_handling);
  181. }
  182. EOF
  183. # Define some shell vars to insert bits of code into the standard elf
  184. # parse_args and list_options functions.
  185. #
  186. PARSE_AND_LIST_PROLOGUE='
  187. #define OPTION_GOT 301
  188. '
  189. PARSE_AND_LIST_LONGOPTS='
  190. { "got", required_argument, NULL, OPTION_GOT},
  191. '
  192. PARSE_AND_LIST_OPTIONS='
  193. fprintf (file, _(" --got=<type> Specify GOT handling scheme\n"));
  194. '
  195. PARSE_AND_LIST_ARGS_CASES='
  196. case OPTION_GOT:
  197. if (strcmp (optarg, "target") == 0)
  198. got_handling = GOT_HANDLING_TARGET_DEFAULT;
  199. else if (strcmp (optarg, "single") == 0)
  200. got_handling = 0;
  201. else if (strcmp (optarg, "negative") == 0)
  202. got_handling = 1;
  203. else if (strcmp (optarg, "multigot") == 0)
  204. got_handling = 2;
  205. else
  206. einfo (_("Unrecognized --got argument '\''%s'\''.\n"), optarg);
  207. break;
  208. '
  209. # We have our own after_open and after_allocation functions, but they call
  210. # the standard routines, so give them a different name.
  211. LDEMUL_AFTER_OPEN=m68k_elf_after_open
  212. LDEMUL_AFTER_ALLOCATION=m68k_elf_after_allocation
  213. LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=elf_m68k_create_output_section_statements