mipself.em 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. # This shell script emits a C file. -*- C -*-
  2. # Copyright (C) 2004-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. case ${target} in
  21. *-*-*gnu*)
  22. gnu_target=true
  23. ;;
  24. *)
  25. gnu_target=false
  26. ;;
  27. esac
  28. fragment <<EOF
  29. #include "ldctor.h"
  30. #include "elf/mips.h"
  31. #include "elfxx-mips.h"
  32. #define is_mips_elf(bfd) \
  33. (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
  34. && elf_tdata (bfd) != NULL \
  35. && elf_object_id (bfd) == MIPS_ELF_DATA)
  36. /* Fake input file for stubs. */
  37. static lang_input_statement_type *stub_file;
  38. static bfd *stub_bfd;
  39. static bool insn32;
  40. static bool ignore_branch_isa;
  41. static bool compact_branches;
  42. struct hook_stub_info
  43. {
  44. lang_statement_list_type add;
  45. asection *input_section;
  46. };
  47. /* Traverse the linker tree to find the spot where the stub goes. */
  48. static bool
  49. hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
  50. {
  51. lang_statement_union_type *l;
  52. bool ret;
  53. for (; (l = *lp) != NULL; lp = &l->header.next)
  54. {
  55. switch (l->header.type)
  56. {
  57. case lang_constructors_statement_enum:
  58. ret = hook_in_stub (info, &constructor_list.head);
  59. if (ret)
  60. return ret;
  61. break;
  62. case lang_output_section_statement_enum:
  63. ret = hook_in_stub (info,
  64. &l->output_section_statement.children.head);
  65. if (ret)
  66. return ret;
  67. break;
  68. case lang_wild_statement_enum:
  69. ret = hook_in_stub (info, &l->wild_statement.children.head);
  70. if (ret)
  71. return ret;
  72. break;
  73. case lang_group_statement_enum:
  74. ret = hook_in_stub (info, &l->group_statement.children.head);
  75. if (ret)
  76. return ret;
  77. break;
  78. case lang_input_section_enum:
  79. if (info->input_section == NULL
  80. || l->input_section.section == info->input_section)
  81. {
  82. /* We've found our section. Insert the stub immediately
  83. before its associated input section. */
  84. *lp = info->add.head;
  85. *(info->add.tail) = l;
  86. return true;
  87. }
  88. break;
  89. case lang_data_statement_enum:
  90. case lang_reloc_statement_enum:
  91. case lang_object_symbols_statement_enum:
  92. case lang_output_statement_enum:
  93. case lang_target_statement_enum:
  94. case lang_input_statement_enum:
  95. case lang_assignment_statement_enum:
  96. case lang_padding_statement_enum:
  97. case lang_address_statement_enum:
  98. case lang_fill_statement_enum:
  99. break;
  100. default:
  101. FAIL ();
  102. break;
  103. }
  104. }
  105. return false;
  106. }
  107. /* Create a new stub section called STUB_SEC_NAME and arrange for it to
  108. be linked in OUTPUT_SECTION. The section should go at the beginning of
  109. OUTPUT_SECTION if INPUT_SECTION is null, otherwise it must go immediately
  110. before INPUT_SECTION. */
  111. static asection *
  112. mips_add_stub_section (const char *stub_sec_name, asection *input_section,
  113. asection *output_section)
  114. {
  115. asection *stub_sec;
  116. flagword flags;
  117. lang_output_section_statement_type *os;
  118. struct hook_stub_info info;
  119. /* PR 12845: If the input section has been garbage collected it will
  120. not have its output section set to *ABS*. */
  121. if (bfd_is_abs_section (output_section))
  122. return NULL;
  123. /* Create the stub file, if we haven't already. */
  124. if (stub_file == NULL)
  125. {
  126. stub_file = lang_add_input_file ("linker stubs",
  127. lang_input_file_is_fake_enum,
  128. NULL);
  129. stub_bfd = bfd_create ("linker stubs", link_info.output_bfd);
  130. if (stub_bfd == NULL
  131. || !bfd_set_arch_mach (stub_bfd,
  132. bfd_get_arch (link_info.output_bfd),
  133. bfd_get_mach (link_info.output_bfd)))
  134. {
  135. einfo (_("%F%P: can not create BFD: %E\n"));
  136. return NULL;
  137. }
  138. stub_bfd->flags |= BFD_LINKER_CREATED;
  139. stub_file->the_bfd = stub_bfd;
  140. ldlang_add_file (stub_file);
  141. }
  142. /* Create the section. */
  143. stub_sec = bfd_make_section_anyway (stub_bfd, stub_sec_name);
  144. if (stub_sec == NULL)
  145. goto err_ret;
  146. /* Set the flags. */
  147. flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
  148. | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_KEEP);
  149. if (!bfd_set_section_flags (stub_sec, flags))
  150. goto err_ret;
  151. os = lang_output_section_get (output_section);
  152. /* Initialize a statement list that contains only the new statement. */
  153. lang_list_init (&info.add);
  154. lang_add_section (&info.add, stub_sec, NULL, NULL, os);
  155. if (info.add.head == NULL)
  156. goto err_ret;
  157. /* Insert the new statement in the appropriate place. */
  158. info.input_section = input_section;
  159. if (hook_in_stub (&info, &os->children.head))
  160. return stub_sec;
  161. err_ret:
  162. einfo (_("%X%P: can not make stub section: %E\n"));
  163. return NULL;
  164. }
  165. /* This is called before the input files are opened. */
  166. static void
  167. mips_create_output_section_statements (void)
  168. {
  169. struct elf_link_hash_table *htab;
  170. htab = elf_hash_table (&link_info);
  171. if (is_elf_hash_table (&htab->root) && is_mips_elf (link_info.output_bfd))
  172. _bfd_mips_elf_linker_flags (&link_info, insn32, ignore_branch_isa,
  173. ${gnu_target});
  174. if (is_mips_elf (link_info.output_bfd))
  175. {
  176. _bfd_mips_elf_compact_branches (&link_info, compact_branches);
  177. _bfd_mips_elf_init_stubs (&link_info, mips_add_stub_section);
  178. }
  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. if (is_mips_elf (link_info.output_bfd))
  185. {
  186. flagword flags;
  187. flags = elf_elfheader (link_info.output_bfd)->e_flags;
  188. if (!bfd_link_pic (&link_info)
  189. && !link_info.nocopyreloc
  190. && (flags & (EF_MIPS_PIC | EF_MIPS_CPIC)) == EF_MIPS_CPIC)
  191. _bfd_mips_elf_use_plts_and_copy_relocs (&link_info);
  192. }
  193. gld${EMULATION_NAME}_before_allocation ();
  194. }
  195. EOF
  196. # Define some shell vars to insert bits of code into the standard elf
  197. # parse_args and list_options functions.
  198. #
  199. PARSE_AND_LIST_PROLOGUE='
  200. enum
  201. {
  202. OPTION_INSN32 = 301,
  203. OPTION_NO_INSN32,
  204. OPTION_IGNORE_BRANCH_ISA,
  205. OPTION_NO_IGNORE_BRANCH_ISA,
  206. OPTION_COMPACT_BRANCHES,
  207. OPTION_NO_COMPACT_BRANCHES
  208. };
  209. '
  210. PARSE_AND_LIST_LONGOPTS='
  211. { "insn32", no_argument, NULL, OPTION_INSN32 },
  212. { "no-insn32", no_argument, NULL, OPTION_NO_INSN32 },
  213. { "ignore-branch-isa", no_argument, NULL, OPTION_IGNORE_BRANCH_ISA },
  214. { "no-ignore-branch-isa", no_argument, NULL, OPTION_NO_IGNORE_BRANCH_ISA },
  215. { "compact-branches", no_argument, NULL, OPTION_COMPACT_BRANCHES },
  216. { "no-compact-branches", no_argument, NULL, OPTION_NO_COMPACT_BRANCHES },
  217. '
  218. PARSE_AND_LIST_OPTIONS='
  219. fprintf (file, _("\
  220. --insn32 Only generate 32-bit microMIPS instructions\n"
  221. ));
  222. fprintf (file, _("\
  223. --no-insn32 Generate all microMIPS instructions\n"
  224. ));
  225. fprintf (file, _("\
  226. --ignore-branch-isa Accept invalid branch relocations requiring\n\
  227. an ISA mode switch\n"
  228. ));
  229. fprintf (file, _("\
  230. --no-ignore-branch-isa Reject invalid branch relocations requiring\n\
  231. an ISA mode switch\n"
  232. ));
  233. fprintf (file, _("\
  234. --compact-branches Generate compact branches/jumps for MIPS R6\n"
  235. ));
  236. fprintf (file, _("\
  237. --no-compact-branches Generate delay slot branches/jumps for MIPS R6\n"
  238. ));
  239. '
  240. PARSE_AND_LIST_ARGS_CASES='
  241. case OPTION_INSN32:
  242. insn32 = true;
  243. break;
  244. case OPTION_NO_INSN32:
  245. insn32 = false;
  246. break;
  247. case OPTION_IGNORE_BRANCH_ISA:
  248. ignore_branch_isa = true;
  249. break;
  250. case OPTION_NO_IGNORE_BRANCH_ISA:
  251. ignore_branch_isa = false;
  252. break;
  253. case OPTION_COMPACT_BRANCHES:
  254. compact_branches = true;
  255. break;
  256. case OPTION_NO_COMPACT_BRANCHES:
  257. compact_branches = false;
  258. break;
  259. '
  260. LDEMUL_BEFORE_ALLOCATION=mips_before_allocation
  261. LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=mips_create_output_section_statements