hppaelf.em 10.0 KB

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