metagelf.em 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. # This shell script emits a C file. -*- C -*-
  2. # Copyright (C) 2013-2015 Free Software Foundation, Inc.
  3. #
  4. # This file is part of 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 elf32.em, and defines extra metagelf
  22. # specific routines. Taken from hppaelf.em.
  23. #
  24. fragment <<EOF
  25. #include "ldctor.h"
  26. #include "elf32-metag.h"
  27. /* Fake input file for stubs. */
  28. static lang_input_statement_type *stub_file;
  29. /* Whether we need to call metag_layout_sections_again. */
  30. static int need_laying_out = 0;
  31. /* Maximum size of a group of input sections that can be handled by
  32. one stub section. A value of +/-1 indicates the bfd back-end
  33. should use a suitable default size. */
  34. static bfd_signed_vma group_size = 1;
  35. /* This is called before the input files are opened. We create a new
  36. fake input file to hold the stub sections. */
  37. static void
  38. metagelf_create_output_section_statements (void)
  39. {
  40. extern const bfd_target metag_elf32_vec;
  41. if (link_info.output_bfd->xvec != &metag_elf32_vec)
  42. return;
  43. stub_file = lang_add_input_file ("linker stubs",
  44. lang_input_file_is_fake_enum,
  45. NULL);
  46. stub_file->the_bfd = bfd_create ("linker stubs", link_info.output_bfd);
  47. if (stub_file->the_bfd == NULL
  48. || ! bfd_set_arch_mach (stub_file->the_bfd,
  49. bfd_get_arch (link_info.output_bfd),
  50. bfd_get_mach (link_info.output_bfd)))
  51. {
  52. einfo ("%X%P: can not create BFD %E\n");
  53. return;
  54. }
  55. stub_file->the_bfd->flags |= BFD_LINKER_CREATED;
  56. ldlang_add_file (stub_file);
  57. }
  58. struct hook_stub_info
  59. {
  60. lang_statement_list_type add;
  61. asection *input_section;
  62. };
  63. /* Traverse the linker tree to find the spot where the stub goes. */
  64. static bfd_boolean
  65. hook_in_stub (struct hook_stub_info *info, lang_statement_union_type **lp)
  66. {
  67. lang_statement_union_type *l;
  68. bfd_boolean ret;
  69. for (; (l = *lp) != NULL; lp = &l->header.next)
  70. {
  71. switch (l->header.type)
  72. {
  73. case lang_constructors_statement_enum:
  74. ret = hook_in_stub (info, &constructor_list.head);
  75. if (ret)
  76. return ret;
  77. break;
  78. case lang_output_section_statement_enum:
  79. ret = hook_in_stub (info,
  80. &l->output_section_statement.children.head);
  81. if (ret)
  82. return ret;
  83. break;
  84. case lang_wild_statement_enum:
  85. ret = hook_in_stub (info, &l->wild_statement.children.head);
  86. if (ret)
  87. return ret;
  88. break;
  89. case lang_group_statement_enum:
  90. ret = hook_in_stub (info, &l->group_statement.children.head);
  91. if (ret)
  92. return ret;
  93. break;
  94. case lang_input_section_enum:
  95. if (l->input_section.section == info->input_section)
  96. {
  97. /* We've found our section. Insert the stub immediately
  98. before its associated input section. */
  99. *lp = info->add.head;
  100. *(info->add.tail) = l;
  101. return TRUE;
  102. }
  103. break;
  104. case lang_data_statement_enum:
  105. case lang_reloc_statement_enum:
  106. case lang_object_symbols_statement_enum:
  107. case lang_output_statement_enum:
  108. case lang_target_statement_enum:
  109. case lang_input_statement_enum:
  110. case lang_assignment_statement_enum:
  111. case lang_padding_statement_enum:
  112. case lang_address_statement_enum:
  113. case lang_fill_statement_enum:
  114. break;
  115. default:
  116. FAIL ();
  117. break;
  118. }
  119. }
  120. return FALSE;
  121. }
  122. /* Call-back for elf_metag_size_stubs. */
  123. /* Create a new stub section, and arrange for it to be linked
  124. immediately before INPUT_SECTION. */
  125. static asection *
  126. metagelf_add_stub_section (const char *stub_sec_name, asection *input_section)
  127. {
  128. asection *stub_sec;
  129. flagword flags;
  130. asection *output_section;
  131. lang_output_section_statement_type *os;
  132. struct hook_stub_info info;
  133. flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
  134. | SEC_HAS_CONTENTS | SEC_RELOC | SEC_IN_MEMORY | SEC_KEEP);
  135. stub_sec = bfd_make_section_anyway_with_flags (stub_file->the_bfd,
  136. stub_sec_name, flags);
  137. if (stub_sec == NULL)
  138. goto err_ret;
  139. output_section = input_section->output_section;
  140. os = lang_output_section_get (output_section);
  141. info.input_section = input_section;
  142. lang_list_init (&info.add);
  143. lang_add_section (&info.add, stub_sec, NULL, os);
  144. if (info.add.head == NULL)
  145. goto err_ret;
  146. if (hook_in_stub (&info, &os->children.head))
  147. return stub_sec;
  148. err_ret:
  149. einfo ("%X%P: can not make stub section: %E\n");
  150. return NULL;
  151. }
  152. /* Another call-back for elf_metag_size_stubs. */
  153. static void
  154. metagelf_layout_sections_again (void)
  155. {
  156. /* If we have changed sizes of the stub sections, then we need
  157. to recalculate all the section offsets. This may mean we need to
  158. add even more stubs. */
  159. gld${EMULATION_NAME}_map_segments (TRUE);
  160. need_laying_out = -1;
  161. }
  162. static void
  163. build_section_lists (lang_statement_union_type *statement)
  164. {
  165. if (statement->header.type == lang_input_section_enum)
  166. {
  167. asection *i = statement->input_section.section;
  168. if (i->sec_info_type != SEC_INFO_TYPE_JUST_SYMS
  169. && (i->flags & SEC_EXCLUDE) == 0
  170. && i->output_section != NULL
  171. && i->output_section->owner == link_info.output_bfd)
  172. {
  173. elf_metag_next_input_section (&link_info, i);
  174. }
  175. }
  176. }
  177. /* For Meta we use this opportunity to build linker stubs. */
  178. static void
  179. gld${EMULATION_NAME}_after_allocation (void)
  180. {
  181. int ret;
  182. /* bfd_elf_discard_info just plays with data and debugging sections,
  183. ie. doesn't affect code size, so we can delay resizing the
  184. sections. It's likely we'll resize everything in the process of
  185. adding stubs. */
  186. ret = bfd_elf_discard_info (link_info.output_bfd, &link_info);
  187. if (ret < 0)
  188. {
  189. einfo ("%X%P: .eh_frame/.stab edit: %E\n");
  190. return;
  191. }
  192. else if (ret > 0)
  193. need_laying_out = 1;
  194. /* If generating a relocatable output file, then we don't
  195. have to examine the relocs. */
  196. if (stub_file != NULL && !bfd_link_relocatable (&link_info))
  197. {
  198. ret = elf_metag_setup_section_lists (link_info.output_bfd, &link_info);
  199. if (ret != 0)
  200. {
  201. if (ret < 0)
  202. {
  203. einfo ("%X%P: can not size stub section: %E\n");
  204. return;
  205. }
  206. lang_for_each_statement (build_section_lists);
  207. /* Call into the BFD backend to do the real work. */
  208. if (! elf_metag_size_stubs (link_info.output_bfd,
  209. stub_file->the_bfd,
  210. &link_info,
  211. group_size,
  212. &metagelf_add_stub_section,
  213. &metagelf_layout_sections_again))
  214. {
  215. einfo ("%X%P: can not size stub section: %E\n");
  216. return;
  217. }
  218. }
  219. }
  220. if (need_laying_out != -1)
  221. gld${EMULATION_NAME}_map_segments (need_laying_out);
  222. if (!bfd_link_relocatable (&link_info))
  223. {
  224. /* Now build the linker stubs. */
  225. if (stub_file != NULL && stub_file->the_bfd->sections != NULL)
  226. {
  227. if (! elf_metag_build_stubs (&link_info))
  228. einfo ("%X%P: can not build stubs: %E\n");
  229. }
  230. }
  231. }
  232. /* Avoid processing the fake stub_file in vercheck, stat_needed and
  233. check_needed routines. */
  234. static void (*real_func) (lang_input_statement_type *);
  235. static void metag_for_each_input_file_wrapper (lang_input_statement_type *l)
  236. {
  237. if (l != stub_file)
  238. (*real_func) (l);
  239. }
  240. static void
  241. metag_lang_for_each_input_file (void (*func) (lang_input_statement_type *))
  242. {
  243. real_func = func;
  244. lang_for_each_input_file (&metag_for_each_input_file_wrapper);
  245. }
  246. #define lang_for_each_input_file metag_lang_for_each_input_file
  247. EOF
  248. # Define some shell vars to insert bits of code into the standard elf
  249. # parse_args and list_options functions.
  250. #
  251. PARSE_AND_LIST_PROLOGUE='
  252. #define OPTION_STUBGROUP_SIZE 301
  253. '
  254. PARSE_AND_LIST_LONGOPTS='
  255. { "stub-group-size", required_argument, NULL, OPTION_STUBGROUP_SIZE },
  256. '
  257. PARSE_AND_LIST_OPTIONS='
  258. fprintf (file, _("\
  259. --stub-group-size=N Maximum size of a group of input sections that\n\
  260. can be handled by one stub section. A negative\n\
  261. value locates all stubs before their branches\n\
  262. (with a group size of -N), while a positive\n\
  263. value allows two groups of input sections, one\n\
  264. before, and one after each stub section.\n\
  265. Values of +/-1 indicate the linker should\n\
  266. choose suitable defaults.\n"
  267. ));
  268. '
  269. PARSE_AND_LIST_ARGS_CASES='
  270. case OPTION_STUBGROUP_SIZE:
  271. {
  272. const char *end;
  273. group_size = bfd_scan_vma (optarg, &end, 0);
  274. if (*end)
  275. einfo (_("%P%F: invalid number `%s'\''\n"), optarg);
  276. }
  277. break;
  278. '
  279. # Put these extra metagelf routines in ld_${EMULATION_NAME}_emulation
  280. #
  281. LDEMUL_AFTER_ALLOCATION=gld${EMULATION_NAME}_after_allocation
  282. LDEMUL_CREATE_OUTPUT_SECTION_STATEMENTS=metagelf_create_output_section_statements