elf32-sh64.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /* SuperH SH64-specific support for 32-bit ELF
  2. Copyright (C) 2000-2015 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #define SH64_ELF
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "elf-bfd.h"
  20. #include "../opcodes/sh64-opc.h"
  21. #include "elf32-sh64.h"
  22. /* Add a suffix for datalabel indirection symbols. It must not match any
  23. other symbols; user symbols with or without version or other
  24. decoration. It must only be used internally and not emitted by any
  25. means. */
  26. #define DATALABEL_SUFFIX " DL"
  27. /* Used to hold data for function called through bfd_map_over_sections. */
  28. struct sh64_find_section_vma_data
  29. {
  30. asection *section;
  31. bfd_vma addr;
  32. };
  33. static bfd_boolean sh64_elf_new_section_hook
  34. (bfd *, asection *);
  35. static bfd_boolean sh64_elf_copy_private_data
  36. (bfd *, bfd *);
  37. static bfd_boolean sh64_elf_merge_private_data
  38. (bfd *, bfd *);
  39. static bfd_boolean sh64_elf_fake_sections
  40. (bfd *, Elf_Internal_Shdr *, asection *);
  41. static bfd_boolean sh64_elf_set_private_flags
  42. (bfd *, flagword);
  43. static bfd_boolean sh64_elf_set_mach_from_flags
  44. (bfd *);
  45. static bfd_boolean shmedia_prepare_reloc
  46. (struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  47. const Elf_Internal_Rela *, bfd_vma *);
  48. static int sh64_elf_get_symbol_type
  49. (Elf_Internal_Sym *, int);
  50. static bfd_boolean sh64_elf_add_symbol_hook
  51. (bfd *, struct bfd_link_info *, Elf_Internal_Sym *, const char **,
  52. flagword *, asection **, bfd_vma *);
  53. static int sh64_elf_link_output_symbol_hook
  54. (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *,
  55. struct elf_link_hash_entry *);
  56. static bfd_boolean sh64_backend_section_from_shdr
  57. (bfd *, Elf_Internal_Shdr *, const char *, int);
  58. static void sh64_elf_final_write_processing
  59. (bfd *, bfd_boolean);
  60. static bfd_boolean sh64_bfd_elf_copy_private_section_data
  61. (bfd *, asection *, bfd *, asection *);
  62. static void sh64_find_section_for_address
  63. (bfd *, asection *, void *);
  64. /* Let elf32-sh.c handle the "bfd_" definitions, so we only have to
  65. intrude with an #ifndef around the function definition. */
  66. #define sh_elf_copy_private_data sh64_elf_copy_private_data
  67. #define sh_elf_merge_private_data sh64_elf_merge_private_data
  68. #define sh_elf_set_private_flags sh64_elf_set_private_flags
  69. /* Typo in elf32-sh.c (and unlinear name). */
  70. #define bfd_elf32_bfd_set_private_flags sh64_elf_set_private_flags
  71. #define sh_elf_set_mach_from_flags sh64_elf_set_mach_from_flags
  72. #define elf_backend_sign_extend_vma 1
  73. #define elf_backend_fake_sections sh64_elf_fake_sections
  74. #define elf_backend_get_symbol_type sh64_elf_get_symbol_type
  75. #define elf_backend_add_symbol_hook sh64_elf_add_symbol_hook
  76. #define elf_backend_link_output_symbol_hook \
  77. sh64_elf_link_output_symbol_hook
  78. #define elf_backend_merge_symbol_attribute sh64_elf_merge_symbol_attribute
  79. #define elf_backend_final_write_processing sh64_elf_final_write_processing
  80. #define elf_backend_section_from_shdr sh64_backend_section_from_shdr
  81. #define elf_backend_special_sections sh64_elf_special_sections
  82. #define elf_backend_section_flags sh64_elf_section_flags
  83. #define bfd_elf32_new_section_hook sh64_elf_new_section_hook
  84. /* For objcopy, we need to set up sh64_elf_section_data (asection *) from
  85. incoming section flags. This is otherwise done in sh64elf.em when
  86. linking or tc-sh64.c when assembling. */
  87. #define bfd_elf32_bfd_copy_private_section_data \
  88. sh64_bfd_elf_copy_private_section_data
  89. /* This COFF-only function (only compiled with COFF support, making
  90. ELF-only chains problematic) returns TRUE early for SH4, so let's just
  91. define it TRUE here. */
  92. #define _bfd_sh_align_load_span(a,b,c,d,e,f,g,h,i,j) \
  93. ((void) f, (void) h, (void) i, TRUE)
  94. #define GOT_BIAS (-((long)-32768))
  95. #define INCLUDE_SHMEDIA
  96. #define SH_TARGET_ALREADY_DEFINED
  97. #include "elf32-sh.c"
  98. /* Tack some extra info on struct bfd_elf_section_data. */
  99. static bfd_boolean
  100. sh64_elf_new_section_hook (bfd *abfd, asection *sec)
  101. {
  102. if (!sec->used_by_bfd)
  103. {
  104. struct _sh64_elf_section_data *sdata;
  105. bfd_size_type amt = sizeof (*sdata);
  106. sdata = bfd_zalloc (abfd, amt);
  107. if (sdata == NULL)
  108. return FALSE;
  109. sec->used_by_bfd = sdata;
  110. }
  111. return _bfd_elf_new_section_hook (abfd, sec);
  112. }
  113. /* Set the SHF_SH5_ISA32 flag for ISA SHmedia code sections, and pass
  114. through SHT_SH5_CR_SORTED on a sorted .cranges section. */
  115. bfd_boolean
  116. sh64_elf_fake_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
  117. Elf_Internal_Shdr *elf_section_hdr,
  118. asection *asect)
  119. {
  120. if (sh64_elf_section_data (asect)->sh64_info != NULL)
  121. elf_section_hdr->sh_flags
  122. |= sh64_elf_section_data (asect)->sh64_info->contents_flags;
  123. /* If this section has the SEC_SORT_ENTRIES flag set, it is a sorted
  124. .cranges section passing through objcopy. */
  125. if ((bfd_get_section_flags (output_bfd, asect) & SEC_SORT_ENTRIES) != 0
  126. && strcmp (bfd_get_section_name (output_bfd, asect),
  127. SH64_CRANGES_SECTION_NAME) == 0)
  128. elf_section_hdr->sh_type = SHT_SH5_CR_SORTED;
  129. return TRUE;
  130. }
  131. static bfd_boolean
  132. sh64_elf_set_mach_from_flags (bfd *abfd)
  133. {
  134. flagword flags = elf_elfheader (abfd)->e_flags;
  135. switch (flags & EF_SH_MACH_MASK)
  136. {
  137. case EF_SH5:
  138. /* These are fit to execute on SH5. Just one but keep the switch
  139. construct to make additions easy. */
  140. bfd_default_set_arch_mach (abfd, bfd_arch_sh, bfd_mach_sh5);
  141. break;
  142. default:
  143. bfd_set_error (bfd_error_wrong_format);
  144. return FALSE;
  145. }
  146. return TRUE;
  147. }
  148. static bfd_boolean
  149. sh64_elf_section_flags (flagword *flags,
  150. const Elf_Internal_Shdr *hdr)
  151. {
  152. if (hdr->bfd_section == NULL)
  153. return FALSE;
  154. if (strcmp (hdr->bfd_section->name, SH64_CRANGES_SECTION_NAME) == 0)
  155. *flags |= SEC_DEBUGGING;
  156. return TRUE;
  157. }
  158. static bfd_boolean
  159. sh64_elf_copy_private_data (bfd * ibfd, bfd * obfd)
  160. {
  161. if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  162. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  163. return TRUE;
  164. BFD_ASSERT (!elf_flags_init (obfd)
  165. || (elf_elfheader (obfd)->e_flags
  166. == elf_elfheader (ibfd)->e_flags));
  167. elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
  168. return _bfd_elf_copy_private_bfd_data (ibfd, obfd);
  169. }
  170. static bfd_boolean
  171. sh64_elf_merge_private_data (bfd *ibfd, bfd *obfd)
  172. {
  173. flagword old_flags, new_flags;
  174. if (! _bfd_generic_verify_endian_match (ibfd, obfd))
  175. return FALSE;
  176. if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  177. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  178. return TRUE;
  179. if (bfd_get_arch_size (ibfd) != bfd_get_arch_size (obfd))
  180. {
  181. const char *msg;
  182. if (bfd_get_arch_size (ibfd) == 32
  183. && bfd_get_arch_size (obfd) == 64)
  184. msg = _("%s: compiled as 32-bit object and %s is 64-bit");
  185. else if (bfd_get_arch_size (ibfd) == 64
  186. && bfd_get_arch_size (obfd) == 32)
  187. msg = _("%s: compiled as 64-bit object and %s is 32-bit");
  188. else
  189. msg = _("%s: object size does not match that of target %s");
  190. (*_bfd_error_handler) (msg, bfd_get_filename (ibfd),
  191. bfd_get_filename (obfd));
  192. bfd_set_error (bfd_error_wrong_format);
  193. return FALSE;
  194. }
  195. old_flags = elf_elfheader (obfd)->e_flags;
  196. new_flags = elf_elfheader (ibfd)->e_flags;
  197. if (! elf_flags_init (obfd))
  198. {
  199. /* This happens when ld starts out with a 'blank' output file. */
  200. elf_flags_init (obfd) = TRUE;
  201. elf_elfheader (obfd)->e_flags = old_flags = new_flags;
  202. }
  203. /* We don't allow linking in non-SH64 code. */
  204. else if ((new_flags & EF_SH_MACH_MASK) != EF_SH5)
  205. {
  206. (*_bfd_error_handler)
  207. ("%s: uses non-SH64 instructions while previous modules use SH64 instructions",
  208. bfd_get_filename (ibfd));
  209. bfd_set_error (bfd_error_bad_value);
  210. return FALSE;
  211. }
  212. /* I can't think of anything sane other than old_flags being EF_SH5 and
  213. that we need to preserve that. */
  214. elf_elfheader (obfd)->e_flags = old_flags;
  215. return sh64_elf_set_mach_from_flags (obfd);
  216. }
  217. /* Handle a SH64-specific section when reading an object file. This
  218. is called when bfd_section_from_shdr finds a section with an unknown
  219. type.
  220. We only recognize SHT_SH5_CR_SORTED, on the .cranges section. */
  221. bfd_boolean
  222. sh64_backend_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr,
  223. const char *name, int shindex)
  224. {
  225. flagword flags = 0;
  226. /* We do like MIPS with a bit switch for recognized types, and returning
  227. FALSE for a recognized section type with an unexpected name. Right
  228. now we only have one recognized type, but that might change. */
  229. switch (hdr->sh_type)
  230. {
  231. case SHT_SH5_CR_SORTED:
  232. if (strcmp (name, SH64_CRANGES_SECTION_NAME) != 0)
  233. return FALSE;
  234. /* We set the SEC_SORT_ENTRIES flag so it can be passed on to
  235. sh64_elf_fake_sections, keeping SHT_SH5_CR_SORTED if this object
  236. passes through objcopy. Perhaps it is brittle; the flag can
  237. suddenly be used by other BFD parts, but it seems not really used
  238. anywhere at the moment. */
  239. flags = SEC_DEBUGGING | SEC_SORT_ENTRIES;
  240. break;
  241. default:
  242. return FALSE;
  243. }
  244. if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
  245. return FALSE;
  246. if (flags
  247. && ! bfd_set_section_flags (abfd, hdr->bfd_section,
  248. bfd_get_section_flags (abfd,
  249. hdr->bfd_section)
  250. | flags))
  251. return FALSE;
  252. return TRUE;
  253. }
  254. /* In contrast to sh64_backend_section_from_shdr, this is called for all
  255. sections, but only when copying sections, not when linking or
  256. assembling. We need to set up the sh64_elf_section_data (asection *)
  257. structure for the SH64 ELF section flags to be copied correctly. */
  258. bfd_boolean
  259. sh64_bfd_elf_copy_private_section_data (bfd *ibfd, asection *isec,
  260. bfd *obfd, asection *osec)
  261. {
  262. struct sh64_section_data *sh64_sec_data;
  263. if (ibfd->xvec->flavour != bfd_target_elf_flavour
  264. || obfd->xvec->flavour != bfd_target_elf_flavour)
  265. return TRUE;
  266. if (! _bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec))
  267. return FALSE;
  268. sh64_sec_data = sh64_elf_section_data (isec)->sh64_info;
  269. if (sh64_sec_data == NULL)
  270. {
  271. sh64_sec_data = bfd_zmalloc (sizeof (struct sh64_section_data));
  272. if (sh64_sec_data == NULL)
  273. return FALSE;
  274. sh64_sec_data->contents_flags
  275. = (elf_section_data (isec)->this_hdr.sh_flags
  276. & (SHF_SH5_ISA32 | SHF_SH5_ISA32_MIXED));
  277. sh64_elf_section_data (osec)->sh64_info = sh64_sec_data;
  278. }
  279. return TRUE;
  280. }
  281. /* Function to keep SH64 specific file flags. */
  282. static bfd_boolean
  283. sh64_elf_set_private_flags (bfd *abfd, flagword flags)
  284. {
  285. BFD_ASSERT (! elf_flags_init (abfd)
  286. || elf_elfheader (abfd)->e_flags == flags);
  287. elf_elfheader (abfd)->e_flags = flags;
  288. elf_flags_init (abfd) = TRUE;
  289. return sh64_elf_set_mach_from_flags (abfd);
  290. }
  291. /* Called when writing out an object file to decide the type of a symbol. */
  292. static int
  293. sh64_elf_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
  294. {
  295. if (ELF_ST_TYPE (elf_sym->st_info) == STT_DATALABEL)
  296. return STT_DATALABEL;
  297. return type;
  298. }
  299. /* Hook called by the linker routine which adds symbols from an object
  300. file. We must make indirect symbols for undefined symbols marked with
  301. STT_DATALABEL, so relocations passing them will pick up that attribute
  302. and neutralize STO_SH5_ISA32 found on the symbol definition.
  303. There is a problem, though: We want to fill in the hash-table entry for
  304. this symbol and signal to the caller that no further processing is
  305. needed. But we don't have the index for this hash-table entry. We
  306. rely here on that the current entry is the first hash-entry with NULL,
  307. which seems brittle. Also, iterating over the hash-table to find that
  308. entry is a linear operation on the number of symbols in this input
  309. file, and this function should take constant time, so that's not good
  310. too. Only comfort is that DataLabel references should only be found in
  311. hand-written assembly code and thus be rare. FIXME: Talk maintainers
  312. into adding an option to elf_add_symbol_hook (preferably) for the index
  313. or the hash entry, alternatively adding the index to Elf_Internal_Sym
  314. (not so good). */
  315. static bfd_boolean
  316. sh64_elf_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
  317. Elf_Internal_Sym *sym, const char **namep,
  318. flagword *flagsp ATTRIBUTE_UNUSED,
  319. asection **secp, bfd_vma *valp)
  320. {
  321. /* We want to do this for relocatable as well as final linking. */
  322. if (ELF_ST_TYPE (sym->st_info) == STT_DATALABEL
  323. && is_elf_hash_table (info->hash))
  324. {
  325. struct elf_link_hash_entry *h;
  326. /* For relocatable links, we register the DataLabel sym in its own
  327. right, and tweak the name when it's output. Otherwise, we make
  328. an indirect symbol of it. */
  329. flagword flags
  330. = bfd_link_relocatable (info) || info->emitrelocations
  331. ? BSF_GLOBAL : BSF_GLOBAL | BSF_INDIRECT;
  332. char *dl_name
  333. = bfd_malloc (strlen (*namep) + sizeof (DATALABEL_SUFFIX));
  334. struct elf_link_hash_entry ** sym_hash = elf_sym_hashes (abfd);
  335. BFD_ASSERT (sym_hash != NULL);
  336. /* Allocation may fail. */
  337. if (dl_name == NULL)
  338. return FALSE;
  339. strcpy (dl_name, *namep);
  340. strcat (dl_name, DATALABEL_SUFFIX);
  341. h = (struct elf_link_hash_entry *)
  342. bfd_link_hash_lookup (info->hash, dl_name, FALSE, FALSE, FALSE);
  343. if (h == NULL)
  344. {
  345. /* No previous datalabel symbol. Make one. */
  346. struct bfd_link_hash_entry *bh = NULL;
  347. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  348. if (! _bfd_generic_link_add_one_symbol (info, abfd, dl_name,
  349. flags, *secp, *valp,
  350. *namep, FALSE,
  351. bed->collect, &bh))
  352. {
  353. free (dl_name);
  354. return FALSE;
  355. }
  356. h = (struct elf_link_hash_entry *) bh;
  357. h->non_elf = 0;
  358. h->type = STT_DATALABEL;
  359. }
  360. else
  361. /* If a new symbol was created, it holds the allocated name.
  362. Otherwise, we don't need it anymore and should deallocate it. */
  363. free (dl_name);
  364. if (h->type != STT_DATALABEL
  365. || ((bfd_link_relocatable (info) || info->emitrelocations)
  366. && h->root.type != bfd_link_hash_undefined)
  367. || (! bfd_link_relocatable (info) && !info->emitrelocations
  368. && h->root.type != bfd_link_hash_indirect))
  369. {
  370. /* Make sure we don't get confused on invalid input. */
  371. (*_bfd_error_handler)
  372. (_("%s: encountered datalabel symbol in input"),
  373. bfd_get_filename (abfd));
  374. bfd_set_error (bfd_error_bad_value);
  375. return FALSE;
  376. }
  377. /* Now find the hash-table slot for this entry and fill it in. */
  378. while (*sym_hash != NULL)
  379. sym_hash++;
  380. *sym_hash = h;
  381. /* Signal to caller to skip this symbol - we've handled it. */
  382. *namep = NULL;
  383. }
  384. return TRUE;
  385. }
  386. /* This hook function is called before the linker writes out a global
  387. symbol. For relocatable links, DataLabel symbols will be present in
  388. linker output. We cut off the special suffix on those symbols, so the
  389. right name appears in the output.
  390. When linking and emitting relocations, there can appear global symbols
  391. that are not referenced by relocs, but rather only implicitly through
  392. DataLabel references, a relation that is not visible to the linker.
  393. Since no stripping of global symbols in done when doing such linking,
  394. we don't need to look up and make sure to emit the main symbol for each
  395. DataLabel symbol. */
  396. static int
  397. sh64_elf_link_output_symbol_hook (struct bfd_link_info *info,
  398. const char *cname,
  399. Elf_Internal_Sym *sym,
  400. asection *input_sec ATTRIBUTE_UNUSED,
  401. struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
  402. {
  403. char *name = (char *) cname;
  404. if (bfd_link_relocatable (info) || info->emitrelocations)
  405. {
  406. if (ELF_ST_TYPE (sym->st_info) == STT_DATALABEL)
  407. name[strlen (name) - strlen (DATALABEL_SUFFIX)] = 0;
  408. }
  409. return 1;
  410. }
  411. /* Check a SH64-specific reloc and put the value to relocate to into
  412. RELOCATION, ready to pass to _bfd_final_link_relocate. Return FALSE if
  413. bad value, TRUE if ok. */
  414. static bfd_boolean
  415. shmedia_prepare_reloc (struct bfd_link_info *info, bfd *abfd,
  416. asection *input_section, bfd_byte *contents,
  417. const Elf_Internal_Rela *rel, bfd_vma *relocation)
  418. {
  419. bfd_vma disp, dropped;
  420. switch (ELF32_R_TYPE (rel->r_info))
  421. {
  422. case R_SH_PT_16:
  423. /* Check the lowest bit of the destination field. If it is 1, we
  424. check the ISA type of the destination (i.e. the low bit of the
  425. "relocation" value, and emit an error if the instruction does not
  426. match). If it is 0, we change a PTA to PTB. There should never
  427. be a PTB that should change to a PTA; that indicates a toolchain
  428. error; a mismatch with GAS. */
  429. {
  430. char *msg = NULL;
  431. bfd_vma insn = bfd_get_32 (abfd, contents + rel->r_offset);
  432. if (insn & (1 << 10))
  433. {
  434. /* Check matching insn and ISA (address of target). */
  435. if ((insn & SHMEDIA_PTB_BIT) != 0
  436. && ((*relocation + rel->r_addend) & 1) != 0)
  437. msg = _("PTB mismatch: a SHmedia address (bit 0 == 1)");
  438. else if ((insn & SHMEDIA_PTB_BIT) == 0
  439. && ((*relocation + rel->r_addend) & 1) == 0)
  440. msg = _("PTA mismatch: a SHcompact address (bit 0 == 0)");
  441. if (msg != NULL
  442. && ! ((*info->callbacks->reloc_dangerous)
  443. (info, msg, abfd, input_section,
  444. rel->r_offset)))
  445. return FALSE;
  446. }
  447. else
  448. {
  449. /* We shouldn't get here with a PTB insn and a R_SH_PT_16. It
  450. means GAS output does not match expectations; a PTA or PTB
  451. expressed as such (or a PT found at assembly to be PTB)
  452. would match the test above, and PT expansion with an
  453. unknown destination (or when relaxing) will get us here. */
  454. if ((insn & SHMEDIA_PTB_BIT) != 0)
  455. {
  456. (*_bfd_error_handler)
  457. (_("%s: GAS error: unexpected PTB insn with R_SH_PT_16"),
  458. bfd_get_filename (input_section->owner));
  459. return FALSE;
  460. }
  461. /* Change the PTA to a PTB, if destination indicates so. */
  462. if (((*relocation + rel->r_addend) & 1) == 0)
  463. bfd_put_32 (abfd, insn | SHMEDIA_PTB_BIT,
  464. contents + rel->r_offset);
  465. }
  466. }
  467. case R_SH_SHMEDIA_CODE:
  468. case R_SH_DIR5U:
  469. case R_SH_DIR6S:
  470. case R_SH_DIR6U:
  471. case R_SH_DIR10S:
  472. case R_SH_DIR10SW:
  473. case R_SH_DIR10SL:
  474. case R_SH_DIR10SQ:
  475. case R_SH_IMMS16:
  476. case R_SH_IMMU16:
  477. case R_SH_IMM_LOW16:
  478. case R_SH_IMM_LOW16_PCREL:
  479. case R_SH_IMM_MEDLOW16:
  480. case R_SH_IMM_MEDLOW16_PCREL:
  481. case R_SH_IMM_MEDHI16:
  482. case R_SH_IMM_MEDHI16_PCREL:
  483. case R_SH_IMM_HI16:
  484. case R_SH_IMM_HI16_PCREL:
  485. case R_SH_64:
  486. case R_SH_64_PCREL:
  487. break;
  488. default:
  489. return FALSE;
  490. }
  491. disp = (*relocation & 0xf);
  492. dropped = 0;
  493. switch (ELF32_R_TYPE (rel->r_info))
  494. {
  495. case R_SH_DIR10SW: dropped = disp & 1; break;
  496. case R_SH_DIR10SL: dropped = disp & 3; break;
  497. case R_SH_DIR10SQ: dropped = disp & 7; break;
  498. }
  499. if (dropped != 0)
  500. {
  501. (*_bfd_error_handler)
  502. (_("%B: error: unaligned relocation type %d at %08x reloc %p\n"),
  503. input_section->owner, ELF32_R_TYPE (rel->r_info),
  504. (unsigned) rel->r_offset, relocation);
  505. return FALSE;
  506. }
  507. return TRUE;
  508. }
  509. /* Helper function to locate the section holding a certain address. This
  510. is called via bfd_map_over_sections. */
  511. static void
  512. sh64_find_section_for_address (bfd *abfd ATTRIBUTE_UNUSED,
  513. asection *section, void *data)
  514. {
  515. bfd_vma vma;
  516. bfd_size_type size;
  517. struct sh64_find_section_vma_data *fsec_datap
  518. = (struct sh64_find_section_vma_data *) data;
  519. /* Return if already found. */
  520. if (fsec_datap->section)
  521. return;
  522. /* If this section isn't part of the addressable contents, skip it. */
  523. if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
  524. return;
  525. vma = bfd_get_section_vma (abfd, section);
  526. if (fsec_datap->addr < vma)
  527. return;
  528. size = section->size;
  529. if (fsec_datap->addr >= vma + size)
  530. return;
  531. fsec_datap->section = section;
  532. }
  533. /* Make sure to write out the generated entries in the .cranges section
  534. when doing partial linking, and set bit 0 on the entry address if it
  535. points to SHmedia code and write sorted .cranges entries when writing
  536. executables (final linking and objcopy). */
  537. static void
  538. sh64_elf_final_write_processing (bfd *abfd,
  539. bfd_boolean linker ATTRIBUTE_UNUSED)
  540. {
  541. bfd_vma ld_generated_cranges_size;
  542. asection *cranges
  543. = bfd_get_section_by_name (abfd, SH64_CRANGES_SECTION_NAME);
  544. /* If no new .cranges were added, the generic ELF linker parts will
  545. write it all out. If not, we need to write them out when doing
  546. partial linking. For a final link, we will sort them and write them
  547. all out further below. */
  548. if (linker
  549. && cranges != NULL
  550. && elf_elfheader (abfd)->e_type != ET_EXEC
  551. && (ld_generated_cranges_size
  552. = sh64_elf_section_data (cranges)->sh64_info->cranges_growth) != 0)
  553. {
  554. bfd_vma incoming_cranges_size
  555. = cranges->size - ld_generated_cranges_size;
  556. if (! bfd_set_section_contents (abfd, cranges,
  557. cranges->contents
  558. + incoming_cranges_size,
  559. cranges->output_offset
  560. + incoming_cranges_size,
  561. ld_generated_cranges_size))
  562. {
  563. bfd_set_error (bfd_error_file_truncated);
  564. (*_bfd_error_handler)
  565. (_("%s: could not write out added .cranges entries"),
  566. bfd_get_filename (abfd));
  567. }
  568. }
  569. /* Only set entry address bit 0 and sort .cranges when linking to an
  570. executable; never with objcopy or strip. */
  571. if (linker && elf_elfheader (abfd)->e_type == ET_EXEC)
  572. {
  573. struct sh64_find_section_vma_data fsec_data;
  574. sh64_elf_crange dummy;
  575. /* For a final link, set the low bit of the entry address to
  576. reflect whether or not it is a SHmedia address.
  577. FIXME: Perhaps we shouldn't do this if the entry address was
  578. supplied numerically, but we currently lack the infrastructure to
  579. recognize that: The entry symbol, and info whether it is numeric
  580. or a symbol name is kept private in the linker. */
  581. fsec_data.addr = elf_elfheader (abfd)->e_entry;
  582. fsec_data.section = NULL;
  583. bfd_map_over_sections (abfd, sh64_find_section_for_address,
  584. &fsec_data);
  585. if (fsec_data.section
  586. && (sh64_get_contents_type (fsec_data.section,
  587. elf_elfheader (abfd)->e_entry,
  588. &dummy) == CRT_SH5_ISA32))
  589. elf_elfheader (abfd)->e_entry |= 1;
  590. /* If we have a .cranges section, sort the entries. */
  591. if (cranges != NULL)
  592. {
  593. bfd_size_type cranges_size = cranges->size;
  594. /* We know we always have these in memory at this time. */
  595. BFD_ASSERT (cranges->contents != NULL);
  596. /* The .cranges may already have been sorted in the process of
  597. finding out the ISA-type of the entry address. If not, we do
  598. it here. */
  599. if (elf_section_data (cranges)->this_hdr.sh_type
  600. != SHT_SH5_CR_SORTED)
  601. {
  602. qsort (cranges->contents, cranges_size / SH64_CRANGE_SIZE,
  603. SH64_CRANGE_SIZE,
  604. bfd_big_endian (cranges->owner)
  605. ? _bfd_sh64_crange_qsort_cmpb
  606. : _bfd_sh64_crange_qsort_cmpl);
  607. elf_section_data (cranges)->this_hdr.sh_type
  608. = SHT_SH5_CR_SORTED;
  609. }
  610. /* We need to write it out in whole as sorted. */
  611. if (! bfd_set_section_contents (abfd, cranges,
  612. cranges->contents,
  613. cranges->output_offset,
  614. cranges_size))
  615. {
  616. bfd_set_error (bfd_error_file_truncated);
  617. (*_bfd_error_handler)
  618. (_("%s: could not write out sorted .cranges entries"),
  619. bfd_get_filename (abfd));
  620. }
  621. }
  622. }
  623. }
  624. /* Merge non visibility st_other attribute when the symbol comes from
  625. a dynamic object. */
  626. static void
  627. sh64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
  628. const Elf_Internal_Sym *isym,
  629. bfd_boolean definition,
  630. bfd_boolean dynamic ATTRIBUTE_UNUSED)
  631. {
  632. if ((isym->st_other & ~ELF_ST_VISIBILITY (-1)) != 0)
  633. {
  634. unsigned char other;
  635. /* Take the balance of OTHER from the definition. */
  636. other = (definition ? isym->st_other : h->other);
  637. other &= ~ ELF_ST_VISIBILITY (-1);
  638. h->other = other | ELF_ST_VISIBILITY (h->other);
  639. }
  640. return;
  641. }
  642. static const struct bfd_elf_special_section sh64_elf_special_sections[] =
  643. {
  644. { STRING_COMMA_LEN (".cranges"), 0, SHT_PROGBITS, 0 },
  645. { NULL, 0, 0, 0, 0 }
  646. };
  647. #undef TARGET_BIG_SYM
  648. #define TARGET_BIG_SYM sh64_elf32_vec
  649. #undef TARGET_BIG_NAME
  650. #define TARGET_BIG_NAME "elf32-sh64"
  651. #undef TARGET_LITTLE_SYM
  652. #define TARGET_LITTLE_SYM sh64_elf32_le_vec
  653. #undef TARGET_LITTLE_NAME
  654. #define TARGET_LITTLE_NAME "elf32-sh64l"
  655. #include "elf32-target.h"
  656. /* NetBSD support. */
  657. #undef TARGET_BIG_SYM
  658. #define TARGET_BIG_SYM sh64_elf32_nbsd_vec
  659. #undef TARGET_BIG_NAME
  660. #define TARGET_BIG_NAME "elf32-sh64-nbsd"
  661. #undef TARGET_LITTLE_SYM
  662. #define TARGET_LITTLE_SYM sh64_elf32_nbsd_le_vec
  663. #undef TARGET_LITTLE_NAME
  664. #define TARGET_LITTLE_NAME "elf32-sh64l-nbsd"
  665. #undef ELF_MAXPAGESIZE
  666. #define ELF_MAXPAGESIZE 0x10000
  667. #undef ELF_COMMONPAGESIZE
  668. #undef elf_symbol_leading_char
  669. #define elf_symbol_leading_char 0
  670. #undef elf32_bed
  671. #define elf32_bed elf32_sh64_nbsd_bed
  672. #include "elf32-target.h"
  673. /* Linux support. */
  674. #undef TARGET_BIG_SYM
  675. #define TARGET_BIG_SYM sh64_elf32_linux_be_vec
  676. #undef TARGET_BIG_NAME
  677. #define TARGET_BIG_NAME "elf32-sh64big-linux"
  678. #undef TARGET_LITTLE_SYM
  679. #define TARGET_LITTLE_SYM sh64_elf32_linux_vec
  680. #undef TARGET_LITTLE_NAME
  681. #define TARGET_LITTLE_NAME "elf32-sh64-linux"
  682. #undef elf32_bed
  683. #define elf32_bed elf32_sh64_lin_bed
  684. #undef ELF_COMMONPAGESIZE
  685. #define ELF_COMMONPAGESIZE 0x1000
  686. #include "elf32-target.h"