elf32-xstormy16.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. /* Xstormy16-specific support for 32-bit ELF.
  2. Copyright (C) 2000-2022 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. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "libbfd.h"
  19. #include "elf-bfd.h"
  20. #include "elf/xstormy16.h"
  21. #include "libiberty.h"
  22. /* Handle the R_XSTORMY16_24 reloc, which has an odd bit arrangement. */
  23. static bfd_reloc_status_type
  24. xstormy16_elf_24_reloc (bfd *abfd,
  25. arelent *reloc_entry,
  26. asymbol *symbol,
  27. void * data,
  28. asection *input_section,
  29. bfd *output_bfd,
  30. char **error_message ATTRIBUTE_UNUSED)
  31. {
  32. bfd_vma relocation, x;
  33. if (output_bfd != NULL)
  34. {
  35. reloc_entry->address += input_section->output_offset;
  36. return bfd_reloc_ok;
  37. }
  38. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  39. return bfd_reloc_outofrange;
  40. if (bfd_is_com_section (symbol->section))
  41. relocation = 0;
  42. else
  43. relocation = symbol->value;
  44. relocation += symbol->section->output_section->vma;
  45. relocation += symbol->section->output_offset;
  46. relocation += reloc_entry->addend;
  47. x = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
  48. x &= 0x0000ff00;
  49. x |= relocation & 0xff;
  50. x |= (relocation << 8) & 0xffff0000;
  51. bfd_put_32 (abfd, x, (bfd_byte *) data + reloc_entry->address);
  52. if (relocation & ~ (bfd_vma) 0xffffff)
  53. return bfd_reloc_overflow;
  54. return bfd_reloc_ok;
  55. }
  56. static reloc_howto_type xstormy16_elf_howto_table [] =
  57. {
  58. /* This reloc does nothing. */
  59. HOWTO (R_XSTORMY16_NONE, /* type */
  60. 0, /* rightshift */
  61. 3, /* size (0 = byte, 1 = short, 2 = long) */
  62. 0, /* bitsize */
  63. false, /* pc_relative */
  64. 0, /* bitpos */
  65. complain_overflow_dont, /* complain_on_overflow */
  66. bfd_elf_generic_reloc, /* special_function */
  67. "R_XSTORMY16_NONE", /* name */
  68. false, /* partial_inplace */
  69. 0, /* src_mask */
  70. 0, /* dst_mask */
  71. false), /* pcrel_offset */
  72. /* A 32 bit absolute relocation. */
  73. HOWTO (R_XSTORMY16_32, /* type */
  74. 0, /* rightshift */
  75. 2, /* size (0 = byte, 1 = short, 2 = long) */
  76. 32, /* bitsize */
  77. false, /* pc_relative */
  78. 0, /* bitpos */
  79. complain_overflow_dont, /* complain_on_overflow */
  80. bfd_elf_generic_reloc, /* special_function */
  81. "R_XSTORMY16_32", /* name */
  82. false, /* partial_inplace */
  83. 0, /* src_mask */
  84. 0xffffffff, /* dst_mask */
  85. false), /* pcrel_offset */
  86. /* A 16 bit absolute relocation. */
  87. HOWTO (R_XSTORMY16_16, /* type */
  88. 0, /* rightshift */
  89. 1, /* size (0 = byte, 1 = short, 2 = long) */
  90. 16, /* bitsize */
  91. false, /* pc_relative */
  92. 0, /* bitpos */
  93. complain_overflow_bitfield, /* complain_on_overflow */
  94. bfd_elf_generic_reloc, /* special_function */
  95. "R_XSTORMY16_16", /* name */
  96. false, /* partial_inplace */
  97. 0, /* src_mask */
  98. 0xffff, /* dst_mask */
  99. false), /* pcrel_offset */
  100. /* An 8 bit absolute relocation. */
  101. HOWTO (R_XSTORMY16_8, /* type */
  102. 0, /* rightshift */
  103. 0, /* size (0 = byte, 1 = short, 2 = long) */
  104. 8, /* bitsize */
  105. false, /* pc_relative */
  106. 0, /* bitpos */
  107. complain_overflow_unsigned, /* complain_on_overflow */
  108. bfd_elf_generic_reloc, /* special_function */
  109. "R_XSTORMY16_8", /* name */
  110. false, /* partial_inplace */
  111. 0, /* src_mask */
  112. 0xff, /* dst_mask */
  113. false), /* pcrel_offset */
  114. /* A 32 bit pc-relative relocation. */
  115. HOWTO (R_XSTORMY16_PC32, /* type */
  116. 0, /* rightshift */
  117. 2, /* size (0 = byte, 1 = short, 2 = long) */
  118. 32, /* bitsize */
  119. true, /* pc_relative */
  120. 0, /* bitpos */
  121. complain_overflow_dont, /* complain_on_overflow */
  122. bfd_elf_generic_reloc, /* special_function */
  123. "R_XSTORMY16_PC32", /* name */
  124. false, /* partial_inplace */
  125. 0, /* src_mask */
  126. 0xffffffff, /* dst_mask */
  127. true), /* pcrel_offset */
  128. /* A 16 bit pc-relative relocation. */
  129. HOWTO (R_XSTORMY16_PC16, /* type */
  130. 0, /* rightshift */
  131. 1, /* size (0 = byte, 1 = short, 2 = long) */
  132. 16, /* bitsize */
  133. true, /* pc_relative */
  134. 0, /* bitpos */
  135. complain_overflow_signed, /* complain_on_overflow */
  136. bfd_elf_generic_reloc, /* special_function */
  137. "R_XSTORMY16_PC16", /* name */
  138. false, /* partial_inplace */
  139. 0, /* src_mask */
  140. 0xffffffff, /* dst_mask */
  141. true), /* pcrel_offset */
  142. /* An 8 bit pc-relative relocation. */
  143. HOWTO (R_XSTORMY16_PC8, /* type */
  144. 0, /* rightshift */
  145. 0, /* size (0 = byte, 1 = short, 2 = long) */
  146. 8, /* bitsize */
  147. true, /* pc_relative */
  148. 0, /* bitpos */
  149. complain_overflow_signed, /* complain_on_overflow */
  150. bfd_elf_generic_reloc, /* special_function */
  151. "R_XSTORMY16_PC8", /* name */
  152. false, /* partial_inplace */
  153. 0, /* src_mask */
  154. 0xffffffff, /* dst_mask */
  155. true), /* pcrel_offset */
  156. /* A 12-bit pc-relative relocation suitable for the branch instructions. */
  157. HOWTO (R_XSTORMY16_REL_12, /* type */
  158. 1, /* rightshift */
  159. 1, /* size (0 = byte, 1 = short, 2 = long) */
  160. 11, /* bitsize */
  161. true, /* pc_relative */
  162. 1, /* bitpos */
  163. complain_overflow_signed, /* complain_on_overflow */
  164. bfd_elf_generic_reloc, /* special_function */
  165. "R_XSTORMY16_REL_12", /* name */
  166. false, /* partial_inplace */
  167. 0, /* src_mask */
  168. 0x0ffe, /* dst_mask */
  169. true), /* pcrel_offset */
  170. /* A 24-bit absolute relocation suitable for the jump instructions. */
  171. HOWTO (R_XSTORMY16_24, /* type */
  172. 0, /* rightshift */
  173. 2, /* size (0 = byte, 1 = short, 2 = long) */
  174. 24, /* bitsize */
  175. false, /* pc_relative */
  176. 0, /* bitpos */
  177. complain_overflow_unsigned, /* complain_on_overflow */
  178. xstormy16_elf_24_reloc, /* special_function */
  179. "R_XSTORMY16_24", /* name */
  180. true, /* partial_inplace */
  181. 0, /* src_mask */
  182. 0xffff00ff, /* dst_mask */
  183. true), /* pcrel_offset */
  184. /* A 16 bit absolute relocation to a function pointer. */
  185. HOWTO (R_XSTORMY16_FPTR16, /* type */
  186. 0, /* rightshift */
  187. 1, /* size (0 = byte, 1 = short, 2 = long) */
  188. 16, /* bitsize */
  189. false, /* pc_relative */
  190. 0, /* bitpos */
  191. complain_overflow_bitfield, /* complain_on_overflow */
  192. bfd_elf_generic_reloc, /* special_function */
  193. "R_XSTORMY16_FPTR16", /* name */
  194. false, /* partial_inplace */
  195. 0, /* src_mask */
  196. 0xffffffff, /* dst_mask */
  197. false), /* pcrel_offset */
  198. /* Low order 16 bit value of a high memory address. */
  199. HOWTO (R_XSTORMY16_LO16, /* type */
  200. 0, /* rightshift */
  201. 1, /* size (0 = byte, 1 = short, 2 = long) */
  202. 16, /* bitsize */
  203. false, /* pc_relative */
  204. 0, /* bitpos */
  205. complain_overflow_dont, /* complain_on_overflow */
  206. bfd_elf_generic_reloc, /* special_function */
  207. "R_XSTORMY16_LO16", /* name */
  208. false, /* partial_inplace */
  209. 0, /* src_mask */
  210. 0xffff, /* dst_mask */
  211. false), /* pcrel_offset */
  212. /* High order 16 bit value of a high memory address. */
  213. HOWTO (R_XSTORMY16_HI16, /* type */
  214. 16, /* rightshift */
  215. 1, /* size (0 = byte, 1 = short, 2 = long) */
  216. 16, /* bitsize */
  217. false, /* pc_relative */
  218. 0, /* bitpos */
  219. complain_overflow_dont, /* complain_on_overflow */
  220. bfd_elf_generic_reloc, /* special_function */
  221. "R_XSTORMY16_HI16", /* name */
  222. false, /* partial_inplace */
  223. 0, /* src_mask */
  224. 0xffff, /* dst_mask */
  225. false), /* pcrel_offset */
  226. /* A 12 bit absolute relocation. */
  227. HOWTO (R_XSTORMY16_12, /* type */
  228. 0, /* rightshift */
  229. 1, /* size (0 = byte, 1 = short, 2 = long) */
  230. 12, /* bitsize */
  231. false, /* pc_relative */
  232. 0, /* bitpos */
  233. complain_overflow_signed, /* complain_on_overflow */
  234. bfd_elf_generic_reloc, /* special_function */
  235. "R_XSTORMY16_12", /* name */
  236. false, /* partial_inplace */
  237. 0x0000, /* src_mask */
  238. 0x0fff, /* dst_mask */
  239. false), /* pcrel_offset */
  240. };
  241. static reloc_howto_type xstormy16_elf_howto_table2 [] =
  242. {
  243. /* GNU extension to record C++ vtable hierarchy */
  244. HOWTO (R_XSTORMY16_GNU_VTINHERIT, /* type */
  245. 0, /* rightshift */
  246. 2, /* size (0 = byte, 1 = short, 2 = long) */
  247. 0, /* bitsize */
  248. false, /* pc_relative */
  249. 0, /* bitpos */
  250. complain_overflow_dont, /* complain_on_overflow */
  251. NULL, /* special_function */
  252. "R_XSTORMY16_GNU_VTINHERIT", /* name */
  253. false, /* partial_inplace */
  254. 0, /* src_mask */
  255. 0, /* dst_mask */
  256. false), /* pcrel_offset */
  257. /* GNU extension to record C++ vtable member usage */
  258. HOWTO (R_XSTORMY16_GNU_VTENTRY, /* type */
  259. 0, /* rightshift */
  260. 2, /* size (0 = byte, 1 = short, 2 = long) */
  261. 0, /* bitsize */
  262. false, /* pc_relative */
  263. 0, /* bitpos */
  264. complain_overflow_dont, /* complain_on_overflow */
  265. _bfd_elf_rel_vtable_reloc_fn, /* special_function */
  266. "R_XSTORMY16_GNU_VTENTRY", /* name */
  267. false, /* partial_inplace */
  268. 0, /* src_mask */
  269. 0, /* dst_mask */
  270. false), /* pcrel_offset */
  271. };
  272. /* Map BFD reloc types to XSTORMY16 ELF reloc types. */
  273. typedef struct xstormy16_reloc_map
  274. {
  275. bfd_reloc_code_real_type bfd_reloc_val;
  276. unsigned int xstormy16_reloc_val;
  277. reloc_howto_type * table;
  278. } reloc_map;
  279. static const reloc_map xstormy16_reloc_map [] =
  280. {
  281. { BFD_RELOC_NONE, R_XSTORMY16_NONE, xstormy16_elf_howto_table },
  282. { BFD_RELOC_32, R_XSTORMY16_32, xstormy16_elf_howto_table },
  283. { BFD_RELOC_16, R_XSTORMY16_16, xstormy16_elf_howto_table },
  284. { BFD_RELOC_8, R_XSTORMY16_8, xstormy16_elf_howto_table },
  285. { BFD_RELOC_32_PCREL, R_XSTORMY16_PC32, xstormy16_elf_howto_table },
  286. { BFD_RELOC_16_PCREL, R_XSTORMY16_PC16, xstormy16_elf_howto_table },
  287. { BFD_RELOC_8_PCREL, R_XSTORMY16_PC8, xstormy16_elf_howto_table },
  288. { BFD_RELOC_XSTORMY16_REL_12, R_XSTORMY16_REL_12, xstormy16_elf_howto_table },
  289. { BFD_RELOC_XSTORMY16_24, R_XSTORMY16_24, xstormy16_elf_howto_table },
  290. { BFD_RELOC_XSTORMY16_FPTR16, R_XSTORMY16_FPTR16, xstormy16_elf_howto_table },
  291. { BFD_RELOC_LO16, R_XSTORMY16_LO16, xstormy16_elf_howto_table },
  292. { BFD_RELOC_HI16, R_XSTORMY16_HI16, xstormy16_elf_howto_table },
  293. { BFD_RELOC_XSTORMY16_12, R_XSTORMY16_12, xstormy16_elf_howto_table },
  294. { BFD_RELOC_VTABLE_INHERIT, R_XSTORMY16_GNU_VTINHERIT, xstormy16_elf_howto_table2 },
  295. { BFD_RELOC_VTABLE_ENTRY, R_XSTORMY16_GNU_VTENTRY, xstormy16_elf_howto_table2 },
  296. };
  297. static reloc_howto_type *
  298. xstormy16_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
  299. bfd_reloc_code_real_type code)
  300. {
  301. unsigned int i;
  302. for (i = ARRAY_SIZE (xstormy16_reloc_map); i--;)
  303. {
  304. const reloc_map * entry;
  305. entry = xstormy16_reloc_map + i;
  306. if (entry->bfd_reloc_val == code)
  307. return entry->table + (entry->xstormy16_reloc_val
  308. - entry->table[0].type);
  309. }
  310. return NULL;
  311. }
  312. static reloc_howto_type *
  313. xstormy16_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  314. const char *r_name)
  315. {
  316. unsigned int i;
  317. for (i = 0;
  318. i < (sizeof (xstormy16_elf_howto_table)
  319. / sizeof (xstormy16_elf_howto_table[0]));
  320. i++)
  321. if (xstormy16_elf_howto_table[i].name != NULL
  322. && strcasecmp (xstormy16_elf_howto_table[i].name, r_name) == 0)
  323. return &xstormy16_elf_howto_table[i];
  324. for (i = 0;
  325. i < (sizeof (xstormy16_elf_howto_table2)
  326. / sizeof (xstormy16_elf_howto_table2[0]));
  327. i++)
  328. if (xstormy16_elf_howto_table2[i].name != NULL
  329. && strcasecmp (xstormy16_elf_howto_table2[i].name, r_name) == 0)
  330. return &xstormy16_elf_howto_table2[i];
  331. return NULL;
  332. }
  333. /* Set the howto pointer for an XSTORMY16 ELF reloc. */
  334. static bool
  335. xstormy16_info_to_howto_rela (bfd * abfd,
  336. arelent * cache_ptr,
  337. Elf_Internal_Rela * dst)
  338. {
  339. unsigned int r_type = ELF32_R_TYPE (dst->r_info);
  340. if (r_type <= (unsigned int) R_XSTORMY16_12)
  341. cache_ptr->howto = &xstormy16_elf_howto_table [r_type];
  342. else if (r_type - R_XSTORMY16_GNU_VTINHERIT
  343. <= ((unsigned int) R_XSTORMY16_GNU_VTENTRY
  344. - (unsigned int) R_XSTORMY16_GNU_VTINHERIT))
  345. cache_ptr->howto
  346. = &xstormy16_elf_howto_table2 [r_type - R_XSTORMY16_GNU_VTINHERIT];
  347. else
  348. {
  349. /* xgettext:c-format */
  350. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  351. abfd, r_type);
  352. bfd_set_error (bfd_error_bad_value);
  353. return false;
  354. }
  355. return true;
  356. }
  357. /* We support 16-bit pointers to code above 64k by generating a thunk
  358. below 64k containing a JMPF instruction to the final address. We
  359. cannot, unfortunately, minimize the number of thunks unless the
  360. -relax switch is given, as otherwise we have no idea where the
  361. sections will fall in the address space. */
  362. static bool
  363. xstormy16_elf_check_relocs (bfd *abfd,
  364. struct bfd_link_info *info,
  365. asection *sec,
  366. const Elf_Internal_Rela *relocs)
  367. {
  368. const Elf_Internal_Rela *rel, *relend;
  369. struct elf_link_hash_entry **sym_hashes;
  370. Elf_Internal_Shdr *symtab_hdr;
  371. bfd_vma *local_plt_offsets;
  372. asection *splt;
  373. bfd *dynobj;
  374. if (bfd_link_relocatable (info))
  375. return true;
  376. symtab_hdr = &elf_tdata(abfd)->symtab_hdr;
  377. sym_hashes = elf_sym_hashes (abfd);
  378. local_plt_offsets = elf_local_got_offsets (abfd);
  379. dynobj = elf_hash_table(info)->dynobj;
  380. relend = relocs + sec->reloc_count;
  381. for (rel = relocs; rel < relend; ++rel)
  382. {
  383. unsigned long r_symndx;
  384. struct elf_link_hash_entry *h;
  385. bfd_vma *offset;
  386. r_symndx = ELF32_R_SYM (rel->r_info);
  387. if (r_symndx < symtab_hdr->sh_info)
  388. h = NULL;
  389. else
  390. {
  391. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  392. while (h->root.type == bfd_link_hash_indirect
  393. || h->root.type == bfd_link_hash_warning)
  394. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  395. }
  396. switch (ELF32_R_TYPE (rel->r_info))
  397. {
  398. /* This relocation describes a 16-bit pointer to a function.
  399. We may need to allocate a thunk in low memory; reserve memory
  400. for it now. */
  401. case R_XSTORMY16_FPTR16:
  402. if (rel->r_addend != 0)
  403. {
  404. (*info->callbacks->warning)
  405. (info, _("non-zero addend in @fptr reloc"), 0,
  406. abfd, 0, 0);
  407. }
  408. if (dynobj == NULL)
  409. elf_hash_table (info)->dynobj = dynobj = abfd;
  410. splt = elf_hash_table (info)->splt;
  411. if (splt == NULL)
  412. {
  413. flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
  414. | SEC_IN_MEMORY | SEC_LINKER_CREATED
  415. | SEC_READONLY | SEC_CODE);
  416. splt = bfd_make_section_anyway_with_flags (dynobj, ".plt",
  417. flags);
  418. elf_hash_table (info)->splt = splt;
  419. if (splt == NULL
  420. || !bfd_set_section_alignment (splt, 1))
  421. return false;
  422. }
  423. if (h != NULL)
  424. offset = &h->plt.offset;
  425. else
  426. {
  427. if (local_plt_offsets == NULL)
  428. {
  429. size_t size;
  430. unsigned int i;
  431. size = symtab_hdr->sh_info * sizeof (bfd_vma);
  432. local_plt_offsets = bfd_alloc (abfd, size);
  433. if (local_plt_offsets == NULL)
  434. return false;
  435. elf_local_got_offsets (abfd) = local_plt_offsets;
  436. for (i = 0; i < symtab_hdr->sh_info; i++)
  437. local_plt_offsets[i] = (bfd_vma) -1;
  438. }
  439. offset = &local_plt_offsets[r_symndx];
  440. }
  441. if (*offset == (bfd_vma) -1)
  442. {
  443. *offset = splt->size;
  444. splt->size += 4;
  445. }
  446. break;
  447. /* This relocation describes the C++ object vtable hierarchy.
  448. Reconstruct it for later use during GC. */
  449. case R_XSTORMY16_GNU_VTINHERIT:
  450. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  451. return false;
  452. break;
  453. /* This relocation describes which C++ vtable entries are actually
  454. used. Record for later use during GC. */
  455. case R_XSTORMY16_GNU_VTENTRY:
  456. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  457. return false;
  458. break;
  459. }
  460. }
  461. return true;
  462. }
  463. /* A subroutine of xstormy16_elf_relax_section. If the global symbol H
  464. is within the low 64k, remove any entry for it in the plt. */
  465. struct relax_plt_data
  466. {
  467. asection *splt;
  468. bool *again;
  469. };
  470. static bool
  471. xstormy16_relax_plt_check (struct elf_link_hash_entry *h, void * xdata)
  472. {
  473. struct relax_plt_data *data = (struct relax_plt_data *) xdata;
  474. if (h->plt.offset != (bfd_vma) -1)
  475. {
  476. bfd_vma address;
  477. if (h->root.type == bfd_link_hash_undefined
  478. || h->root.type == bfd_link_hash_undefweak)
  479. address = 0;
  480. else
  481. address = (h->root.u.def.section->output_section->vma
  482. + h->root.u.def.section->output_offset
  483. + h->root.u.def.value);
  484. if (address <= 0xffff)
  485. {
  486. h->plt.offset = -1;
  487. data->splt->size -= 4;
  488. *data->again = true;
  489. }
  490. }
  491. return true;
  492. }
  493. /* A subroutine of xstormy16_elf_relax_section. If the global symbol H
  494. previously had a plt entry, give it a new entry offset. */
  495. static bool
  496. xstormy16_relax_plt_realloc (struct elf_link_hash_entry *h, void * xdata)
  497. {
  498. bfd_vma *entry = (bfd_vma *) xdata;
  499. if (h->plt.offset != (bfd_vma) -1)
  500. {
  501. h->plt.offset = *entry;
  502. *entry += 4;
  503. }
  504. return true;
  505. }
  506. static bool
  507. xstormy16_elf_relax_section (bfd *dynobj,
  508. asection *splt,
  509. struct bfd_link_info *info,
  510. bool *again)
  511. {
  512. struct relax_plt_data relax_plt_data;
  513. bfd *ibfd;
  514. /* Assume nothing changes. */
  515. *again = false;
  516. if (bfd_link_relocatable (info)
  517. || !is_elf_hash_table (info->hash))
  518. return true;
  519. /* We only relax the .plt section at the moment. */
  520. if (dynobj != elf_hash_table (info)->dynobj
  521. || strcmp (splt->name, ".plt") != 0)
  522. return true;
  523. /* Quick check for an empty plt. */
  524. if (splt->size == 0)
  525. return true;
  526. /* Map across all global symbols; see which ones happen to
  527. fall in the low 64k. */
  528. relax_plt_data.splt = splt;
  529. relax_plt_data.again = again;
  530. elf_link_hash_traverse (elf_hash_table (info), xstormy16_relax_plt_check,
  531. &relax_plt_data);
  532. /* Likewise for local symbols, though that's somewhat less convenient
  533. as we have to walk the list of input bfds and swap in symbol data. */
  534. for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
  535. {
  536. bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
  537. Elf_Internal_Shdr *symtab_hdr;
  538. Elf_Internal_Sym *isymbuf = NULL;
  539. unsigned int idx;
  540. if (! local_plt_offsets)
  541. continue;
  542. symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
  543. if (symtab_hdr->sh_info != 0)
  544. {
  545. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  546. if (isymbuf == NULL)
  547. isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
  548. symtab_hdr->sh_info, 0,
  549. NULL, NULL, NULL);
  550. if (isymbuf == NULL)
  551. return false;
  552. }
  553. for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
  554. {
  555. Elf_Internal_Sym *isym;
  556. asection *tsec;
  557. bfd_vma address;
  558. if (local_plt_offsets[idx] == (bfd_vma) -1)
  559. continue;
  560. isym = &isymbuf[idx];
  561. if (isym->st_shndx == SHN_UNDEF)
  562. continue;
  563. else if (isym->st_shndx == SHN_ABS)
  564. tsec = bfd_abs_section_ptr;
  565. else if (isym->st_shndx == SHN_COMMON)
  566. tsec = bfd_com_section_ptr;
  567. else
  568. tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
  569. address = (tsec->output_section->vma
  570. + tsec->output_offset
  571. + isym->st_value);
  572. if (address <= 0xffff)
  573. {
  574. local_plt_offsets[idx] = -1;
  575. splt->size -= 4;
  576. *again = true;
  577. }
  578. }
  579. if (isymbuf != NULL
  580. && symtab_hdr->contents != (unsigned char *) isymbuf)
  581. {
  582. if (! info->keep_memory)
  583. free (isymbuf);
  584. else
  585. {
  586. /* Cache the symbols for elf_link_input_bfd. */
  587. symtab_hdr->contents = (unsigned char *) isymbuf;
  588. }
  589. }
  590. }
  591. /* If we changed anything, walk the symbols again to reallocate
  592. .plt entry addresses. */
  593. if (*again && splt->size > 0)
  594. {
  595. bfd_vma entry = 0;
  596. elf_link_hash_traverse (elf_hash_table (info),
  597. xstormy16_relax_plt_realloc, &entry);
  598. for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
  599. {
  600. bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
  601. unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info;
  602. unsigned int idx;
  603. if (! local_plt_offsets)
  604. continue;
  605. for (idx = 0; idx < nlocals; ++idx)
  606. if (local_plt_offsets[idx] != (bfd_vma) -1)
  607. {
  608. local_plt_offsets[idx] = entry;
  609. entry += 4;
  610. }
  611. }
  612. }
  613. return true;
  614. }
  615. static bool
  616. xstormy16_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
  617. struct bfd_link_info *info)
  618. {
  619. bfd *dynobj;
  620. asection *splt;
  621. if (bfd_link_relocatable (info))
  622. return true;
  623. dynobj = elf_hash_table (info)->dynobj;
  624. if (dynobj == NULL)
  625. return true;
  626. splt = elf_hash_table (info)->splt;
  627. BFD_ASSERT (splt != NULL);
  628. splt->contents = bfd_zalloc (dynobj, splt->size);
  629. if (splt->contents == NULL)
  630. return false;
  631. return true;
  632. }
  633. /* Relocate an XSTORMY16 ELF section.
  634. The RELOCATE_SECTION function is called by the new ELF backend linker
  635. to handle the relocations for a section.
  636. The relocs are always passed as Rela structures; if the section
  637. actually uses Rel structures, the r_addend field will always be
  638. zero.
  639. This function is responsible for adjusting the section contents as
  640. necessary, and (if using Rela relocs and generating a relocatable
  641. output file) adjusting the reloc addend as necessary.
  642. This function does not have to worry about setting the reloc
  643. address or the reloc symbol index.
  644. LOCAL_SYMS is a pointer to the swapped in local symbols.
  645. LOCAL_SECTIONS is an array giving the section in the input file
  646. corresponding to the st_shndx field of each local symbol.
  647. The global hash table entry for the global symbols can be found
  648. via elf_sym_hashes (input_bfd).
  649. When generating relocatable output, this function must handle
  650. STB_LOCAL/STT_SECTION symbols specially. The output symbol is
  651. going to be the section symbol corresponding to the output
  652. section, which means that the addend must be adjusted
  653. accordingly. */
  654. static int
  655. xstormy16_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
  656. struct bfd_link_info * info,
  657. bfd * input_bfd,
  658. asection * input_section,
  659. bfd_byte * contents,
  660. Elf_Internal_Rela * relocs,
  661. Elf_Internal_Sym * local_syms,
  662. asection ** local_sections)
  663. {
  664. Elf_Internal_Shdr * symtab_hdr;
  665. struct elf_link_hash_entry ** sym_hashes;
  666. Elf_Internal_Rela * rel;
  667. Elf_Internal_Rela * relend;
  668. asection *splt;
  669. symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
  670. sym_hashes = elf_sym_hashes (input_bfd);
  671. relend = relocs + input_section->reloc_count;
  672. splt = elf_hash_table (info)->splt;
  673. for (rel = relocs; rel < relend; rel ++)
  674. {
  675. reloc_howto_type * howto;
  676. unsigned long r_symndx;
  677. Elf_Internal_Sym * sym;
  678. asection * sec;
  679. struct elf_link_hash_entry * h;
  680. bfd_vma relocation;
  681. bfd_reloc_status_type r;
  682. const char * name = NULL;
  683. int r_type;
  684. r_type = ELF32_R_TYPE (rel->r_info);
  685. if ( r_type == R_XSTORMY16_GNU_VTINHERIT
  686. || r_type == R_XSTORMY16_GNU_VTENTRY)
  687. continue;
  688. r_symndx = ELF32_R_SYM (rel->r_info);
  689. howto = xstormy16_elf_howto_table + ELF32_R_TYPE (rel->r_info);
  690. h = NULL;
  691. sym = NULL;
  692. sec = NULL;
  693. if (r_symndx < symtab_hdr->sh_info)
  694. {
  695. sym = local_syms + r_symndx;
  696. sec = local_sections [r_symndx];
  697. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  698. }
  699. else
  700. {
  701. bool unresolved_reloc, warned, ignored;
  702. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  703. r_symndx, symtab_hdr, sym_hashes,
  704. h, sec, relocation,
  705. unresolved_reloc, warned, ignored);
  706. }
  707. if (sec != NULL && discarded_section (sec))
  708. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  709. rel, 1, relend, howto, 0, contents);
  710. if (bfd_link_relocatable (info))
  711. continue;
  712. if (h != NULL)
  713. name = h->root.root.string;
  714. else
  715. {
  716. name = (bfd_elf_string_from_elf_section
  717. (input_bfd, symtab_hdr->sh_link, sym->st_name));
  718. if (name == NULL || *name == '\0')
  719. name = bfd_section_name (sec);
  720. }
  721. switch (ELF32_R_TYPE (rel->r_info))
  722. {
  723. case R_XSTORMY16_24:
  724. {
  725. bfd_vma reloc = relocation + rel->r_addend;
  726. unsigned int x;
  727. x = bfd_get_32 (input_bfd, contents + rel->r_offset);
  728. x &= 0x0000ff00;
  729. x |= reloc & 0xff;
  730. x |= (reloc << 8) & 0xffff0000;
  731. bfd_put_32 (input_bfd, x, contents + rel->r_offset);
  732. if (reloc & ~0xffffff)
  733. r = bfd_reloc_overflow;
  734. else
  735. r = bfd_reloc_ok;
  736. break;
  737. }
  738. case R_XSTORMY16_FPTR16:
  739. {
  740. bfd_vma *plt_offset;
  741. if (h != NULL)
  742. plt_offset = &h->plt.offset;
  743. else
  744. plt_offset = elf_local_got_offsets (input_bfd) + r_symndx;
  745. if (relocation <= 0xffff)
  746. {
  747. /* If the symbol is in range for a 16-bit address, we should
  748. have deallocated the plt entry in relax_section. */
  749. BFD_ASSERT (*plt_offset == (bfd_vma) -1);
  750. }
  751. else
  752. {
  753. /* If the symbol is out of range for a 16-bit address,
  754. we must have allocated a plt entry. */
  755. BFD_ASSERT (*plt_offset != (bfd_vma) -1);
  756. /* If this is the first time we've processed this symbol,
  757. fill in the plt entry with the correct symbol address. */
  758. if ((*plt_offset & 1) == 0)
  759. {
  760. unsigned int x;
  761. x = 0x00000200; /* jmpf */
  762. x |= relocation & 0xff;
  763. x |= (relocation << 8) & 0xffff0000;
  764. bfd_put_32 (input_bfd, x, splt->contents + *plt_offset);
  765. *plt_offset |= 1;
  766. }
  767. relocation = (splt->output_section->vma
  768. + splt->output_offset
  769. + (*plt_offset & -2));
  770. }
  771. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  772. contents, rel->r_offset,
  773. relocation, 0);
  774. break;
  775. }
  776. default:
  777. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  778. contents, rel->r_offset,
  779. relocation, rel->r_addend);
  780. break;
  781. }
  782. if (r != bfd_reloc_ok)
  783. {
  784. const char * msg = NULL;
  785. switch (r)
  786. {
  787. case bfd_reloc_overflow:
  788. (*info->callbacks->reloc_overflow)
  789. (info, (h ? &h->root : NULL), name, howto->name,
  790. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  791. break;
  792. case bfd_reloc_undefined:
  793. (*info->callbacks->undefined_symbol)
  794. (info, name, input_bfd, input_section, rel->r_offset, true);
  795. break;
  796. case bfd_reloc_outofrange:
  797. msg = _("internal error: out of range error");
  798. break;
  799. case bfd_reloc_notsupported:
  800. msg = _("internal error: unsupported relocation error");
  801. break;
  802. case bfd_reloc_dangerous:
  803. msg = _("internal error: dangerous relocation");
  804. break;
  805. default:
  806. msg = _("internal error: unknown error");
  807. break;
  808. }
  809. if (msg)
  810. (*info->callbacks->warning) (info, msg, name, input_bfd,
  811. input_section, rel->r_offset);
  812. }
  813. }
  814. return true;
  815. }
  816. /* This must exist if dynobj is ever set. */
  817. static bool
  818. xstormy16_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED,
  819. struct bfd_link_info *info)
  820. {
  821. bfd *dynobj = elf_hash_table (info)->dynobj;
  822. asection *splt = elf_hash_table (info)->splt;
  823. /* As an extra sanity check, verify that all plt entries have
  824. been filled in. */
  825. if (dynobj != NULL && splt != NULL)
  826. {
  827. bfd_byte *contents = splt->contents;
  828. unsigned int i, size = splt->size;
  829. for (i = 0; i < size; i += 4)
  830. {
  831. unsigned int x = bfd_get_32 (dynobj, contents + i);
  832. BFD_ASSERT (x != 0);
  833. }
  834. }
  835. return true;
  836. }
  837. /* Return the section that should be marked against GC for a given
  838. relocation. */
  839. static asection *
  840. xstormy16_elf_gc_mark_hook (asection *sec,
  841. struct bfd_link_info *info,
  842. Elf_Internal_Rela *rel,
  843. struct elf_link_hash_entry *h,
  844. Elf_Internal_Sym *sym)
  845. {
  846. if (h != NULL)
  847. switch (ELF32_R_TYPE (rel->r_info))
  848. {
  849. case R_XSTORMY16_GNU_VTINHERIT:
  850. case R_XSTORMY16_GNU_VTENTRY:
  851. return NULL;
  852. }
  853. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  854. }
  855. #define ELF_ARCH bfd_arch_xstormy16
  856. #define ELF_MACHINE_CODE EM_XSTORMY16
  857. #define ELF_MAXPAGESIZE 0x100
  858. #define TARGET_LITTLE_SYM xstormy16_elf32_vec
  859. #define TARGET_LITTLE_NAME "elf32-xstormy16"
  860. #define elf_info_to_howto_rel NULL
  861. #define elf_info_to_howto xstormy16_info_to_howto_rela
  862. #define elf_backend_relocate_section xstormy16_elf_relocate_section
  863. #define elf_backend_gc_mark_hook xstormy16_elf_gc_mark_hook
  864. #define elf_backend_check_relocs xstormy16_elf_check_relocs
  865. #define elf_backend_always_size_sections \
  866. xstormy16_elf_always_size_sections
  867. #define elf_backend_omit_section_dynsym \
  868. _bfd_elf_omit_section_dynsym_all
  869. #define elf_backend_finish_dynamic_sections \
  870. xstormy16_elf_finish_dynamic_sections
  871. #define elf_backend_can_gc_sections 1
  872. #define elf_backend_rela_normal 1
  873. #define bfd_elf32_bfd_reloc_type_lookup xstormy16_reloc_type_lookup
  874. #define bfd_elf32_bfd_reloc_name_lookup \
  875. xstormy16_reloc_name_lookup
  876. #define bfd_elf32_bfd_relax_section xstormy16_elf_relax_section
  877. #include "elf32-target.h"