mipself.em 7.7 KB

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