sh64elf.em 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. # This shell script emits a C file. -*- C -*-
  2. # Copyright (C) 2000-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. #
  21. # This file is sourced from elf32.em, and defines extra sh64
  22. # specific routines.
  23. #
  24. LDEMUL_AFTER_ALLOCATION=sh64_elf_${EMULATION_NAME}_after_allocation
  25. LDEMUL_BEFORE_ALLOCATION=sh64_elf_${EMULATION_NAME}_before_allocation
  26. fragment <<EOF
  27. #include "libiberty.h"
  28. #include "libbfd.h"
  29. #include "elf-bfd.h"
  30. #include "elf/sh.h"
  31. #include "elf32-sh64.h"
  32. /* Check if we need a .cranges section and create it if it's not in any
  33. input file. It might seem better to always create it and if unneeded,
  34. discard it, but I don't find a simple way to discard it totally from
  35. the output.
  36. Putting it here instead of as a elf_backend_always_size_sections hook
  37. in elf32-sh64.c, means that we have access to linker command line
  38. options here, and we can access input sections in the order in which
  39. they will be linked. */
  40. static void
  41. sh64_elf_${EMULATION_NAME}_before_allocation (void)
  42. {
  43. asection *cranges;
  44. asection *osec;
  45. /* Call main function; we're just extending it. */
  46. gld${EMULATION_NAME}_before_allocation ();
  47. cranges = bfd_get_section_by_name (link_info.output_bfd,
  48. SH64_CRANGES_SECTION_NAME);
  49. if (cranges != NULL)
  50. {
  51. if (RELAXATION_ENABLED)
  52. {
  53. /* FIXME: Look through incoming sections with .cranges
  54. descriptors, build up some kind of descriptors that the
  55. relaxing function will pick up and adjust, or perhaps make it
  56. find and adjust an associated .cranges descriptor. We could
  57. also look through incoming relocs and kill the ones marking
  58. relaxation areas, but that wouldn't be TRT. */
  59. einfo
  60. (_("%P: Sorry, turning off relaxing: .cranges section in input.\n"));
  61. einfo (_(" A .cranges section is present in:\n"));
  62. {
  63. LANG_FOR_EACH_INPUT_STATEMENT (f)
  64. {
  65. asection *input_cranges
  66. = bfd_get_section_by_name (f->the_bfd,
  67. SH64_CRANGES_SECTION_NAME);
  68. if (input_cranges != NULL)
  69. einfo (" %I\n", f);
  70. }
  71. }
  72. DISABLE_RELAXATION;
  73. }
  74. /* We wouldn't need to do anything when there's already a .cranges
  75. section (and have a return here), except that we need to set the
  76. section flags right for output sections that *don't* need a
  77. .cranges section. */
  78. }
  79. if (RELAXATION_ENABLED)
  80. {
  81. LANG_FOR_EACH_INPUT_STATEMENT (f)
  82. {
  83. if (bfd_get_flavour (f->the_bfd) == bfd_target_elf_flavour)
  84. {
  85. asection *isec;
  86. for (isec = f->the_bfd->sections;
  87. isec != NULL;
  88. isec = isec->next)
  89. {
  90. if (elf_section_data (isec)->this_hdr.sh_flags
  91. & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
  92. {
  93. einfo (_("%P: Sorry, turning off relaxing: SHmedia sections present.\n"));
  94. einfo (" %I\n", f);
  95. DISABLE_RELAXATION;
  96. goto done_scanning_shmedia_sections;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. done_scanning_shmedia_sections:
  103. /* For each non-empty input section in each output section, check if it
  104. has the same SH64-specific flags. If some input section differs, we
  105. need a .cranges section. */
  106. for (osec = link_info.output_bfd->sections;
  107. osec != NULL;
  108. osec = osec->next)
  109. {
  110. struct sh64_section_data *sh64_sec_data;
  111. bfd_vma oflags_isa = 0;
  112. bfd_vma iflags_isa = 0;
  113. if (bfd_get_flavour (link_info.output_bfd) != bfd_target_elf_flavour)
  114. einfo (_("%FError: non-ELF output formats are not supported by this target's linker.\n"));
  115. sh64_sec_data = sh64_elf_section_data (osec)->sh64_info;
  116. /* Omit excluded or garbage-collected sections. */
  117. if (bfd_get_section_flags (link_info.output_bfd, osec) & SEC_EXCLUDE)
  118. continue;
  119. /* Make sure we have the target section data initialized. */
  120. if (sh64_sec_data == NULL)
  121. {
  122. sh64_sec_data = xcalloc (1, sizeof (struct sh64_section_data));
  123. sh64_elf_section_data (osec)->sh64_info = sh64_sec_data;
  124. }
  125. /* First find an input section so we have flags to compare with; the
  126. flags in the output section are not valid. */
  127. {
  128. LANG_FOR_EACH_INPUT_STATEMENT (f)
  129. {
  130. asection *isec;
  131. for (isec = f->the_bfd->sections;
  132. isec != NULL;
  133. isec = isec->next)
  134. {
  135. if (isec->output_section == osec
  136. && isec->size != 0
  137. && (bfd_get_section_flags (isec->owner, isec)
  138. & SEC_EXCLUDE) == 0)
  139. {
  140. oflags_isa
  141. = (elf_section_data (isec)->this_hdr.sh_flags
  142. & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
  143. goto break_1;
  144. }
  145. }
  146. }
  147. }
  148. break_1:
  149. /* Check that all input sections have the same contents-type flags
  150. as the first input section. */
  151. {
  152. LANG_FOR_EACH_INPUT_STATEMENT (f)
  153. {
  154. asection *isec;
  155. for (isec = f->the_bfd->sections;
  156. isec != NULL;
  157. isec = isec->next)
  158. {
  159. if (isec->output_section == osec
  160. && isec->size != 0
  161. && (bfd_get_section_flags (isec->owner, isec)
  162. & SEC_EXCLUDE) == 0)
  163. {
  164. iflags_isa
  165. = (elf_section_data (isec)->this_hdr.sh_flags
  166. & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
  167. /* If flags don't agree, we need a .cranges section.
  168. Create it here if it did not exist through input
  169. sections. */
  170. if (iflags_isa != oflags_isa)
  171. {
  172. if (cranges == NULL)
  173. {
  174. /* This section will be *appended* to
  175. sections, so the outer iteration will reach
  176. it in due time and set
  177. sh64_elf_section_data; no need to set it
  178. specifically here. */
  179. cranges
  180. = bfd_make_section_with_flags (link_info.output_bfd,
  181. SH64_CRANGES_SECTION_NAME,
  182. SEC_LINKER_CREATED
  183. | SEC_KEEP
  184. | SEC_HAS_CONTENTS
  185. | SEC_DEBUGGING);
  186. if (cranges == NULL)
  187. einfo
  188. (_("%P%E%F: Can't make .cranges section\n"));
  189. }
  190. /* We don't need to look at more input sections,
  191. and we know this section will have mixed
  192. contents. */
  193. goto break_2;
  194. }
  195. }
  196. }
  197. }
  198. }
  199. /* If we got here, then all input sections in this output section
  200. have the same contents flag. Put that where we expect to see
  201. contents flags. We don't need to do this for sections that will
  202. need additional, linker-generated .cranges entries. */
  203. sh64_sec_data->contents_flags = iflags_isa;
  204. break_2:
  205. ;
  206. }
  207. }
  208. /* Size up and extend the .cranges section, merging generated entries. */
  209. static void
  210. sh64_elf_${EMULATION_NAME}_after_allocation (void)
  211. {
  212. bfd_vma new_cranges = 0;
  213. bfd_vma cranges_growth = 0;
  214. asection *osec;
  215. bfd_byte *crangesp;
  216. asection *cranges;
  217. gld${EMULATION_NAME}_after_allocation ();
  218. /* Needed, since we create link_orders here. */
  219. lang_clear_os_map ();
  220. cranges = bfd_get_section_by_name (link_info.output_bfd,
  221. SH64_CRANGES_SECTION_NAME);
  222. /* If there is no .cranges section, it is because it was seen earlier on
  223. that none was needed. Otherwise it must have been created then, or
  224. be present in input. */
  225. if (cranges == NULL)
  226. return;
  227. /* First, we set the ISA flags for each output section according to the
  228. first non-discarded section. For each input section in osec, we
  229. check if it has the same flags. If it does not, we set flags to mark
  230. a mixed section (and exit the loop early). */
  231. for (osec = link_info.output_bfd->sections;
  232. osec != NULL;
  233. osec = osec->next)
  234. {
  235. bfd_vma oflags_isa = 0;
  236. bfd_boolean need_check_cranges = FALSE;
  237. /* Omit excluded or garbage-collected sections. */
  238. if (bfd_get_section_flags (link_info.output_bfd, osec) & SEC_EXCLUDE)
  239. continue;
  240. /* First find an input section so we have flags to compare with; the
  241. flags in the output section are not valid. */
  242. {
  243. LANG_FOR_EACH_INPUT_STATEMENT (f)
  244. {
  245. asection *isec;
  246. for (isec = f->the_bfd->sections;
  247. isec != NULL;
  248. isec = isec->next)
  249. {
  250. if (isec->output_section == osec
  251. && isec->size != 0
  252. && (bfd_get_section_flags (isec->owner, isec)
  253. & SEC_EXCLUDE) == 0)
  254. {
  255. oflags_isa
  256. = (elf_section_data (isec)->this_hdr.sh_flags
  257. & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
  258. goto break_1;
  259. }
  260. }
  261. }
  262. }
  263. break_1:
  264. /* Check that all input sections have the same contents-type flags
  265. as the first input section. */
  266. {
  267. LANG_FOR_EACH_INPUT_STATEMENT (f)
  268. {
  269. asection *isec;
  270. for (isec = f->the_bfd->sections;
  271. isec != NULL;
  272. isec = isec->next)
  273. {
  274. if (isec->output_section == osec
  275. && isec->size != 0
  276. && (bfd_get_section_flags (isec->owner, isec)
  277. & SEC_EXCLUDE) == 0)
  278. {
  279. bfd_vma iflags_isa
  280. = (elf_section_data (isec)->this_hdr.sh_flags
  281. & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
  282. /* If flags don't agree, set the target-specific data
  283. of the section to mark that this section needs to
  284. be have .cranges section entries added. Don't
  285. bother setting ELF section flags in output section;
  286. they will be cleared later and will have to be
  287. re-initialized before the linked file is written. */
  288. if (iflags_isa != oflags_isa)
  289. {
  290. oflags_isa = SHF_SH5_ISA32_MIXED;
  291. BFD_ASSERT (sh64_elf_section_data (osec)->sh64_info);
  292. sh64_elf_section_data (osec)->sh64_info->contents_flags
  293. = SHF_SH5_ISA32_MIXED;
  294. need_check_cranges = TRUE;
  295. goto break_2;
  296. }
  297. }
  298. }
  299. }
  300. }
  301. break_2:
  302. /* If there were no new ranges for this output section, we don't
  303. need to iterate over the input sections to check how many are
  304. needed. */
  305. if (! need_check_cranges)
  306. continue;
  307. /* If we found a section with differing contents type, we need more
  308. ranges to mark the sections that are not mixed (and already have
  309. .cranges descriptors). Calculate the maximum number of new
  310. entries here. We may merge some of them, so that number is not
  311. final; it can shrink. */
  312. {
  313. LANG_FOR_EACH_INPUT_STATEMENT (f)
  314. {
  315. asection *isec;
  316. for (isec = f->the_bfd->sections;
  317. isec != NULL;
  318. isec = isec->next)
  319. {
  320. if (isec->output_section == osec
  321. && isec->size != 0
  322. && (bfd_get_section_flags (isec->owner, isec)
  323. & SEC_EXCLUDE) == 0
  324. && ((elf_section_data (isec)->this_hdr.sh_flags
  325. & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
  326. != SHF_SH5_ISA32_MIXED))
  327. new_cranges++;
  328. }
  329. }
  330. }
  331. }
  332. if (cranges->contents != NULL)
  333. free (cranges->contents);
  334. BFD_ASSERT (sh64_elf_section_data (cranges)->sh64_info != NULL);
  335. /* Make sure we have .cranges in memory even if there were only
  336. assembler-generated .cranges. */
  337. cranges_growth = new_cranges * SH64_CRANGE_SIZE;
  338. cranges->contents = xcalloc (cranges->size + cranges_growth, 1);
  339. bfd_set_section_flags (cranges->owner, cranges,
  340. bfd_get_section_flags (cranges->owner, cranges)
  341. | SEC_IN_MEMORY);
  342. /* If we don't need to grow the .cranges section beyond what was in the
  343. input sections, we have nothing more to do here. We then only got
  344. here because there was a .cranges section coming from input. Zero
  345. out the number of generated .cranges. */
  346. if (new_cranges == 0)
  347. {
  348. sh64_elf_section_data (cranges)->sh64_info->cranges_growth = 0;
  349. return;
  350. }
  351. crangesp = cranges->contents + cranges->size;
  352. /* Now pass over the sections again, and make reloc orders for the new
  353. .cranges entries. Constants are set as we go. */
  354. for (osec = link_info.output_bfd->sections;
  355. osec != NULL;
  356. osec = osec->next)
  357. {
  358. struct bfd_link_order *cr_addr_order = NULL;
  359. enum sh64_elf_cr_type last_cr_type = CRT_NONE;
  360. bfd_vma last_cr_size = 0;
  361. bfd_vma continuation_vma = 0;
  362. /* Omit excluded or garbage-collected sections, and output sections
  363. which were not marked as needing further processing. */
  364. if ((bfd_get_section_flags (link_info.output_bfd, osec) & SEC_EXCLUDE) != 0
  365. || (sh64_elf_section_data (osec)->sh64_info->contents_flags
  366. != SHF_SH5_ISA32_MIXED))
  367. continue;
  368. {
  369. LANG_FOR_EACH_INPUT_STATEMENT (f)
  370. {
  371. asection *isec;
  372. for (isec = f->the_bfd->sections;
  373. isec != NULL;
  374. isec = isec->next)
  375. {
  376. /* Allow only sections that have (at least initially) a
  377. non-zero size, and are not excluded, and are not marked
  378. as containing mixed data, thus already having .cranges
  379. entries. */
  380. if (isec->output_section == osec
  381. && isec->size != 0
  382. && (bfd_get_section_flags (isec->owner, isec)
  383. & SEC_EXCLUDE) == 0
  384. && ((elf_section_data (isec)->this_hdr.sh_flags
  385. & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED))
  386. != SHF_SH5_ISA32_MIXED))
  387. {
  388. enum sh64_elf_cr_type cr_type;
  389. bfd_vma cr_size;
  390. bfd_vma isa_flags
  391. = (elf_section_data (isec)->this_hdr.sh_flags
  392. & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
  393. if (isa_flags == SHF_SH5_ISA32)
  394. cr_type = CRT_SH5_ISA32;
  395. else if ((bfd_get_section_flags (isec->owner, isec)
  396. & SEC_CODE) == 0)
  397. cr_type = CRT_DATA;
  398. else
  399. cr_type = CRT_SH5_ISA16;
  400. cr_size = isec->size;
  401. /* Sections can be empty, like .text in a file that
  402. only contains other sections. Ranges shouldn't be
  403. emitted for them. This can presumably happen after
  404. relaxing and is not be caught at the "raw size"
  405. test above. */
  406. if (cr_size == 0)
  407. continue;
  408. /* See if this is a continuation of the previous range
  409. for the same output section. If so, just change
  410. the size of the last range and continue. */
  411. if (cr_type == last_cr_type
  412. && (continuation_vma
  413. == osec->vma + isec->output_offset))
  414. {
  415. last_cr_size += cr_size;
  416. bfd_put_32 (link_info.output_bfd, last_cr_size,
  417. crangesp - SH64_CRANGE_SIZE
  418. + SH64_CRANGE_CR_SIZE_OFFSET);
  419. continuation_vma += cr_size;
  420. continue;
  421. }
  422. /* If we emit relocatable contents, we need a
  423. relocation for the start address. */
  424. if (bfd_link_relocatable (&link_info)
  425. || link_info.emitrelocations)
  426. {
  427. /* FIXME: We could perhaps use lang_add_reloc and
  428. friends here, but I'm not really sure that
  429. would leave us free to do some optimizations
  430. later. */
  431. cr_addr_order
  432. = bfd_new_link_order (link_info.output_bfd, cranges);
  433. if (cr_addr_order == NULL)
  434. {
  435. einfo (_("%P%F: bfd_new_link_order failed\n"));
  436. return;
  437. }
  438. cr_addr_order->type = bfd_section_reloc_link_order;
  439. cr_addr_order->offset
  440. = (cranges->output_offset
  441. + crangesp + SH64_CRANGE_CR_ADDR_OFFSET
  442. - cranges->contents);
  443. cr_addr_order->size = 4;
  444. cr_addr_order->u.reloc.p
  445. = xmalloc (sizeof (struct bfd_link_order_reloc));
  446. cr_addr_order->u.reloc.p->reloc = BFD_RELOC_32;
  447. cr_addr_order->u.reloc.p->u.section = osec;
  448. /* Since SH, unlike normal RELA-targets, uses a
  449. "partial inplace" REL-like relocation for this,
  450. we put the addend in the contents and specify 0
  451. for the reloc. */
  452. bfd_put_32 (link_info.output_bfd, isec->output_offset,
  453. crangesp + SH64_CRANGE_CR_ADDR_OFFSET);
  454. cr_addr_order->u.reloc.p->addend = 0;
  455. }
  456. else
  457. bfd_put_32 (link_info.output_bfd,
  458. osec->vma + isec->output_offset,
  459. crangesp + SH64_CRANGE_CR_ADDR_OFFSET);
  460. /* If we could make a reloc for cr_size we would do
  461. it, but we would have to have a symbol for the size
  462. of the _input_ section and there's no way to
  463. generate that. */
  464. bfd_put_32 (link_info.output_bfd, cr_size,
  465. crangesp + SH64_CRANGE_CR_SIZE_OFFSET);
  466. bfd_put_16 (link_info.output_bfd, cr_type,
  467. crangesp + SH64_CRANGE_CR_TYPE_OFFSET);
  468. last_cr_type = cr_type;
  469. last_cr_size = cr_size;
  470. continuation_vma
  471. = osec->vma + isec->output_offset + cr_size;
  472. crangesp += SH64_CRANGE_SIZE;
  473. }
  474. }
  475. }
  476. }
  477. }
  478. /* The .cranges section will have this size, no larger or smaller.
  479. Since relocs (if relocatable linking) will be emitted into the
  480. "extended" size, we must set the raw size to the total. We have to
  481. keep track of the number of new .cranges entries.
  482. Sorting before writing is done by sh64_elf_final_write_processing. */
  483. sh64_elf_section_data (cranges)->sh64_info->cranges_growth
  484. = crangesp - cranges->contents - cranges->size;
  485. cranges->size = crangesp - cranges->contents;
  486. cranges->rawsize = cranges->size;
  487. }
  488. EOF