elf32-iq2000.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. /* IQ2000-specific support for 32-bit ELF.
  2. Copyright (C) 2003-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, MA 02110-1301, USA. */
  15. #include "sysdep.h"
  16. #include "bfd.h"
  17. #include "libbfd.h"
  18. #include "elf-bfd.h"
  19. #include "elf/iq2000.h"
  20. #include "libiberty.h"
  21. /* Forward declarations. */
  22. static bfd_reloc_status_type iq2000_elf_howto_hi16_reloc (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  23. static reloc_howto_type iq2000_elf_howto_table [] =
  24. {
  25. /* This reloc does nothing. */
  26. HOWTO (R_IQ2000_NONE, /* type */
  27. 0, /* rightshift */
  28. 3, /* size (0 = byte, 1 = short, 2 = long) */
  29. 0, /* bitsize */
  30. false, /* pc_relative */
  31. 0, /* bitpos */
  32. complain_overflow_dont, /* complain_on_overflow */
  33. bfd_elf_generic_reloc, /* special_function */
  34. "R_IQ2000_NONE", /* name */
  35. false, /* partial_inplace */
  36. 0, /* src_mask */
  37. 0, /* dst_mask */
  38. false), /* pcrel_offset */
  39. /* A 16 bit absolute relocation. */
  40. HOWTO (R_IQ2000_16, /* type */
  41. 0, /* rightshift */
  42. 1, /* size (0 = byte, 1 = short, 2 = long) */
  43. 16, /* bitsize */
  44. false, /* pc_relative */
  45. 0, /* bitpos */
  46. complain_overflow_bitfield, /* complain_on_overflow */
  47. bfd_elf_generic_reloc, /* special_function */
  48. "R_IQ2000_16", /* name */
  49. false, /* partial_inplace */
  50. 0x0000, /* src_mask */
  51. 0xffff, /* dst_mask */
  52. false), /* pcrel_offset */
  53. /* A 32 bit absolute relocation. */
  54. HOWTO (R_IQ2000_32, /* type */
  55. 0, /* rightshift */
  56. 2, /* size (0 = byte, 1 = short, 2 = long) */
  57. 31, /* bitsize */
  58. false, /* pc_relative */
  59. 0, /* bitpos */
  60. complain_overflow_bitfield, /* complain_on_overflow */
  61. bfd_elf_generic_reloc, /* special_function */
  62. "R_IQ2000_32", /* name */
  63. false, /* partial_inplace */
  64. 0x00000000, /* src_mask */
  65. 0x7fffffff, /* dst_mask */
  66. false), /* pcrel_offset */
  67. /* 26 bit branch address. */
  68. HOWTO (R_IQ2000_26, /* type */
  69. 2, /* rightshift */
  70. 2, /* size (0 = byte, 1 = short, 2 = long) */
  71. 26, /* bitsize */
  72. false, /* pc_relative */
  73. 0, /* bitpos */
  74. complain_overflow_dont, /* complain_on_overflow */
  75. /* This needs complex overflow
  76. detection, because the upper four
  77. bits must match the PC. */
  78. bfd_elf_generic_reloc, /* special_function */
  79. "R_IQ2000_26", /* name */
  80. false, /* partial_inplace */
  81. 0x00000000, /* src_mask */
  82. 0x03ffffff, /* dst_mask */
  83. false), /* pcrel_offset */
  84. /* 16 bit PC relative reference. */
  85. HOWTO (R_IQ2000_PC16, /* type */
  86. 2, /* rightshift */
  87. 2, /* size (0 = byte, 1 = short, 2 = long) */
  88. 16, /* bitsize */
  89. true, /* pc_relative */
  90. 0, /* bitpos */
  91. complain_overflow_signed, /* complain_on_overflow */
  92. bfd_elf_generic_reloc, /* special_function */
  93. "R_IQ2000_PC16", /* name */
  94. false, /* partial_inplace */
  95. 0x0000, /* src_mask */
  96. 0xffff, /* dst_mask */
  97. true), /* pcrel_offset */
  98. /* high 16 bits of symbol value. */
  99. HOWTO (R_IQ2000_HI16, /* type */
  100. 16, /* rightshift */
  101. 2, /* size (0 = byte, 1 = short, 2 = long) */
  102. 15, /* bitsize */
  103. false, /* pc_relative */
  104. 0, /* bitpos */
  105. complain_overflow_dont, /* complain_on_overflow */
  106. iq2000_elf_howto_hi16_reloc, /* special_function */
  107. "R_IQ2000_HI16", /* name */
  108. false, /* partial_inplace */
  109. 0x0000, /* src_mask */
  110. 0x7fff, /* dst_mask */
  111. false), /* pcrel_offset */
  112. /* Low 16 bits of symbol value. */
  113. HOWTO (R_IQ2000_LO16, /* type */
  114. 0, /* rightshift */
  115. 2, /* size (0 = byte, 1 = short, 2 = long) */
  116. 16, /* bitsize */
  117. false, /* pc_relative */
  118. 0, /* bitpos */
  119. complain_overflow_dont, /* complain_on_overflow */
  120. bfd_elf_generic_reloc, /* special_function */
  121. "R_IQ2000_LO16", /* name */
  122. false, /* partial_inplace */
  123. 0x0000, /* src_mask */
  124. 0xffff, /* dst_mask */
  125. false), /* pcrel_offset */
  126. /* 16-bit jump offset. */
  127. HOWTO (R_IQ2000_OFFSET_16, /* type */
  128. 2, /* rightshift */
  129. 2, /* size (0 = byte, 1 = short, 2 = long) */
  130. 16, /* bitsize */
  131. false, /* pc_relative */
  132. 0, /* bitpos */
  133. complain_overflow_dont, /* complain_on_overflow */
  134. bfd_elf_generic_reloc, /* special_function */
  135. "R_IQ2000_OFFSET_16", /* name */
  136. false, /* partial_inplace */
  137. 0x0000, /* src_mask */
  138. 0xffff, /* dst_mask */
  139. false), /* pcrel_offset */
  140. /* 21-bit jump offset. */
  141. HOWTO (R_IQ2000_OFFSET_21, /* type */
  142. 2, /* rightshift */
  143. 2, /* size (0 = byte, 1 = short, 2 = long) */
  144. 21, /* bitsize */
  145. false, /* pc_relative */
  146. 0, /* bitpos */
  147. complain_overflow_dont, /* complain_on_overflow */
  148. bfd_elf_generic_reloc, /* special_function */
  149. "R_IQ2000_OFFSET_21", /* name */
  150. false, /* partial_inplace */
  151. 0x000000, /* src_mask */
  152. 0x1fffff, /* dst_mask */
  153. false), /* pcrel_offset */
  154. /* unsigned high 16 bits of value. */
  155. HOWTO (R_IQ2000_OFFSET_21, /* type */
  156. 16, /* rightshift */
  157. 2, /* size (0 = byte, 1 = short, 2 = long) */
  158. 16, /* bitsize */
  159. false, /* pc_relative */
  160. 0, /* bitpos */
  161. complain_overflow_dont, /* complain_on_overflow */
  162. bfd_elf_generic_reloc, /* special_function */
  163. "R_IQ2000_UHI16", /* name */
  164. false, /* partial_inplace */
  165. 0x0000, /* src_mask */
  166. 0x7fff, /* dst_mask */
  167. false), /* pcrel_offset */
  168. /* A 32 bit absolute debug relocation. */
  169. HOWTO (R_IQ2000_32_DEBUG, /* type */
  170. 0, /* rightshift */
  171. 2, /* size (0 = byte, 1 = short, 2 = long) */
  172. 32, /* bitsize */
  173. false, /* pc_relative */
  174. 0, /* bitpos */
  175. complain_overflow_bitfield, /* complain_on_overflow */
  176. bfd_elf_generic_reloc, /* special_function */
  177. "R_IQ2000_32", /* name */
  178. false, /* partial_inplace */
  179. 0x00000000, /* src_mask */
  180. 0xffffffff, /* dst_mask */
  181. false), /* pcrel_offset */
  182. };
  183. /* GNU extension to record C++ vtable hierarchy. */
  184. static reloc_howto_type iq2000_elf_vtinherit_howto =
  185. HOWTO (R_IQ2000_GNU_VTINHERIT, /* type */
  186. 0, /* rightshift */
  187. 2, /* size (0 = byte, 1 = short, 2 = long) */
  188. 0, /* bitsize */
  189. false, /* pc_relative */
  190. 0, /* bitpos */
  191. complain_overflow_dont, /* complain_on_overflow */
  192. NULL, /* special_function */
  193. "R_IQ2000_GNU_VTINHERIT", /* name */
  194. false, /* partial_inplace */
  195. 0, /* src_mask */
  196. 0, /* dst_mask */
  197. false); /* pcrel_offset */
  198. /* GNU extension to record C++ vtable member usage. */
  199. static reloc_howto_type iq2000_elf_vtentry_howto =
  200. HOWTO (R_IQ2000_GNU_VTENTRY, /* type */
  201. 0, /* rightshift */
  202. 2, /* size (0 = byte, 1 = short, 2 = long) */
  203. 0, /* bitsize */
  204. false, /* pc_relative */
  205. 0, /* bitpos */
  206. complain_overflow_dont, /* complain_on_overflow */
  207. NULL, /* special_function */
  208. "R_IQ2000_GNU_VTENTRY", /* name */
  209. false, /* partial_inplace */
  210. 0, /* src_mask */
  211. 0, /* dst_mask */
  212. false); /* pcrel_offset */
  213. static bfd_reloc_status_type
  214. iq2000_elf_howto_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED,
  215. arelent *reloc_entry,
  216. asymbol *symbol,
  217. void * data,
  218. asection *input_section,
  219. bfd *output_bfd,
  220. char **error_message ATTRIBUTE_UNUSED)
  221. {
  222. bfd_reloc_status_type ret;
  223. bfd_vma relocation;
  224. /* If we're relocating and this an external symbol,
  225. we don't want to change anything. */
  226. if (output_bfd != (bfd *) NULL
  227. && (symbol->flags & BSF_SECTION_SYM) == 0
  228. && reloc_entry->addend == 0)
  229. {
  230. reloc_entry->address += input_section->output_offset;
  231. return bfd_reloc_ok;
  232. }
  233. if (bfd_is_com_section (symbol->section))
  234. relocation = 0;
  235. else
  236. relocation = symbol->value;
  237. relocation += symbol->section->output_section->vma;
  238. relocation += symbol->section->output_offset;
  239. relocation += reloc_entry->addend;
  240. /* If %lo will have sign-extension, compensate by add 0x10000 to hi portion. */
  241. if (relocation & 0x8000)
  242. reloc_entry->addend += 0x10000;
  243. /* Now do the reloc in the usual way. */
  244. ret = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  245. input_section, output_bfd, error_message);
  246. /* Put it back the way it was. */
  247. if (relocation & 0x8000)
  248. reloc_entry->addend -= 0x10000;
  249. return ret;
  250. }
  251. static bfd_reloc_status_type
  252. iq2000_elf_relocate_hi16 (bfd *input_bfd,
  253. Elf_Internal_Rela *relhi,
  254. bfd_byte *contents,
  255. bfd_vma value)
  256. {
  257. bfd_vma insn;
  258. insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
  259. value += relhi->r_addend;
  260. value &= 0x7fffffff; /* Mask off top-bit which is Harvard mask bit. */
  261. /* If top-bit of %lo value is on, this means that %lo will
  262. sign-propagate and so we compensate by adding 1 to %hi value. */
  263. if (value & 0x8000)
  264. value += 0x10000;
  265. value >>= 16;
  266. insn = ((insn & ~0xFFFF) | value);
  267. bfd_put_32 (input_bfd, insn, contents + relhi->r_offset);
  268. return bfd_reloc_ok;
  269. }
  270. static bfd_reloc_status_type
  271. iq2000_elf_relocate_offset16 (bfd *input_bfd,
  272. Elf_Internal_Rela *rel,
  273. bfd_byte *contents,
  274. bfd_vma value,
  275. bfd_vma location)
  276. {
  277. bfd_vma insn;
  278. bfd_vma jtarget;
  279. insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
  280. value += rel->r_addend;
  281. if (value & 3)
  282. return bfd_reloc_dangerous;
  283. jtarget = (value & 0x3fffc) | (location & 0xf0000000L);
  284. if (jtarget != value)
  285. return bfd_reloc_overflow;
  286. insn = (insn & ~0xFFFF) | ((value >> 2) & 0xFFFF);
  287. bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
  288. return bfd_reloc_ok;
  289. }
  290. /* Map BFD reloc types to IQ2000 ELF reloc types. */
  291. static reloc_howto_type *
  292. iq2000_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  293. bfd_reloc_code_real_type code)
  294. {
  295. /* Note that the iq2000_elf_howto_table is indxed by the R_
  296. constants. Thus, the order that the howto records appear in the
  297. table *must* match the order of the relocation types defined in
  298. include/elf/iq2000.h. */
  299. switch (code)
  300. {
  301. case BFD_RELOC_NONE:
  302. return &iq2000_elf_howto_table[ (int) R_IQ2000_NONE];
  303. case BFD_RELOC_16:
  304. return &iq2000_elf_howto_table[ (int) R_IQ2000_16];
  305. case BFD_RELOC_32:
  306. return &iq2000_elf_howto_table[ (int) R_IQ2000_32];
  307. case BFD_RELOC_MIPS_JMP:
  308. return &iq2000_elf_howto_table[ (int) R_IQ2000_26];
  309. case BFD_RELOC_IQ2000_OFFSET_16:
  310. return &iq2000_elf_howto_table[ (int) R_IQ2000_OFFSET_16];
  311. case BFD_RELOC_IQ2000_OFFSET_21:
  312. return &iq2000_elf_howto_table[ (int) R_IQ2000_OFFSET_21];
  313. case BFD_RELOC_16_PCREL_S2:
  314. return &iq2000_elf_howto_table[ (int) R_IQ2000_PC16];
  315. case BFD_RELOC_HI16:
  316. return &iq2000_elf_howto_table[ (int) R_IQ2000_HI16];
  317. case BFD_RELOC_IQ2000_UHI16:
  318. return &iq2000_elf_howto_table[ (int) R_IQ2000_UHI16];
  319. case BFD_RELOC_LO16:
  320. return &iq2000_elf_howto_table[ (int) R_IQ2000_LO16];
  321. case BFD_RELOC_VTABLE_INHERIT:
  322. return &iq2000_elf_vtinherit_howto;
  323. case BFD_RELOC_VTABLE_ENTRY:
  324. return &iq2000_elf_vtentry_howto;
  325. default:
  326. /* Pacify gcc -Wall. */
  327. return NULL;
  328. }
  329. return NULL;
  330. }
  331. static reloc_howto_type *
  332. iq2000_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
  333. {
  334. unsigned int i;
  335. for (i = 0;
  336. i < (sizeof (iq2000_elf_howto_table)
  337. / sizeof (iq2000_elf_howto_table[0]));
  338. i++)
  339. if (iq2000_elf_howto_table[i].name != NULL
  340. && strcasecmp (iq2000_elf_howto_table[i].name, r_name) == 0)
  341. return &iq2000_elf_howto_table[i];
  342. if (strcasecmp (iq2000_elf_vtinherit_howto.name, r_name) == 0)
  343. return &iq2000_elf_vtinherit_howto;
  344. if (strcasecmp (iq2000_elf_vtentry_howto.name, r_name) == 0)
  345. return &iq2000_elf_vtentry_howto;
  346. return NULL;
  347. }
  348. /* Perform a single relocation. By default we use the standard BFD
  349. routines. */
  350. static bfd_reloc_status_type
  351. iq2000_final_link_relocate (reloc_howto_type * howto,
  352. bfd * input_bfd,
  353. asection * input_section,
  354. bfd_byte * contents,
  355. Elf_Internal_Rela * rel,
  356. bfd_vma relocation)
  357. {
  358. return _bfd_final_link_relocate (howto, input_bfd, input_section,
  359. contents, rel->r_offset,
  360. relocation, rel->r_addend);
  361. }
  362. /* Set the howto pointer for a IQ2000 ELF reloc. */
  363. static bool
  364. iq2000_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED,
  365. arelent * cache_ptr,
  366. Elf_Internal_Rela * dst)
  367. {
  368. unsigned int r_type;
  369. r_type = ELF32_R_TYPE (dst->r_info);
  370. switch (r_type)
  371. {
  372. case R_IQ2000_GNU_VTINHERIT:
  373. cache_ptr->howto = & iq2000_elf_vtinherit_howto;
  374. break;
  375. case R_IQ2000_GNU_VTENTRY:
  376. cache_ptr->howto = & iq2000_elf_vtentry_howto;
  377. break;
  378. default:
  379. if (r_type >= ARRAY_SIZE (iq2000_elf_howto_table))
  380. {
  381. /* xgettext:c-format */
  382. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  383. abfd, r_type);
  384. bfd_set_error (bfd_error_bad_value);
  385. return false;
  386. }
  387. cache_ptr->howto = & iq2000_elf_howto_table [r_type];
  388. break;
  389. }
  390. return true;
  391. }
  392. /* Look through the relocs for a section during the first phase.
  393. Since we don't do .gots or .plts, we just need to consider the
  394. virtual table relocs for gc. */
  395. static bool
  396. iq2000_elf_check_relocs (bfd *abfd,
  397. struct bfd_link_info *info,
  398. asection *sec,
  399. const Elf_Internal_Rela *relocs)
  400. {
  401. Elf_Internal_Shdr *symtab_hdr;
  402. struct elf_link_hash_entry **sym_hashes;
  403. const Elf_Internal_Rela *rel;
  404. const Elf_Internal_Rela *rel_end;
  405. bool changed = false;
  406. if (bfd_link_relocatable (info))
  407. return true;
  408. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  409. sym_hashes = elf_sym_hashes (abfd);
  410. rel_end = relocs + sec->reloc_count;
  411. for (rel = relocs; rel < rel_end; rel++)
  412. {
  413. struct elf_link_hash_entry *h;
  414. unsigned long r_symndx;
  415. r_symndx = ELF32_R_SYM (rel->r_info);
  416. if (r_symndx < symtab_hdr->sh_info)
  417. h = NULL;
  418. else
  419. {
  420. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  421. while (h->root.type == bfd_link_hash_indirect
  422. || h->root.type == bfd_link_hash_warning)
  423. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  424. }
  425. switch (ELF32_R_TYPE (rel->r_info))
  426. {
  427. /* This relocation describes the C++ object vtable
  428. hierarchy. Reconstruct it for later use during GC. */
  429. case R_IQ2000_GNU_VTINHERIT:
  430. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  431. return false;
  432. break;
  433. /* This relocation describes which C++ vtable entries
  434. are actually used. Record for later use during GC. */
  435. case R_IQ2000_GNU_VTENTRY:
  436. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  437. return false;
  438. break;
  439. case R_IQ2000_32:
  440. /* For debug section, change to special harvard-aware relocations. */
  441. if (startswith (sec->name, ".debug")
  442. || startswith (sec->name, ".stab")
  443. || startswith (sec->name, ".eh_frame"))
  444. {
  445. ((Elf_Internal_Rela *) rel)->r_info
  446. = ELF32_R_INFO (ELF32_R_SYM (rel->r_info), R_IQ2000_32_DEBUG);
  447. changed = true;
  448. }
  449. break;
  450. }
  451. }
  452. if (changed)
  453. /* Note that we've changed relocs, otherwise if !info->keep_memory
  454. we'll free the relocs and lose our changes. */
  455. elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs;
  456. return true;
  457. }
  458. /* Relocate a IQ2000 ELF section.
  459. There is some attempt to make this function usable for many architectures,
  460. both USE_REL and USE_RELA ['twould be nice if such a critter existed],
  461. if only to serve as a learning tool.
  462. The RELOCATE_SECTION function is called by the new ELF backend linker
  463. to handle the relocations for a section.
  464. The relocs are always passed as Rela structures; if the section
  465. actually uses Rel structures, the r_addend field will always be
  466. zero.
  467. This function is responsible for adjusting the section contents as
  468. necessary, and (if using Rela relocs and generating a relocatable
  469. output file) adjusting the reloc addend as necessary.
  470. This function does not have to worry about setting the reloc
  471. address or the reloc symbol index.
  472. LOCAL_SYMS is a pointer to the swapped in local symbols.
  473. LOCAL_SECTIONS is an array giving the section in the input file
  474. corresponding to the st_shndx field of each local symbol.
  475. The global hash table entry for the global symbols can be found
  476. via elf_sym_hashes (input_bfd).
  477. When generating relocatable output, this function must handle
  478. STB_LOCAL/STT_SECTION symbols specially. The output symbol is
  479. going to be the section symbol corresponding to the output
  480. section, which means that the addend must be adjusted
  481. accordingly. */
  482. static int
  483. iq2000_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
  484. struct bfd_link_info * info,
  485. bfd * input_bfd,
  486. asection * input_section,
  487. bfd_byte * contents,
  488. Elf_Internal_Rela * relocs,
  489. Elf_Internal_Sym * local_syms,
  490. asection ** local_sections)
  491. {
  492. Elf_Internal_Shdr * symtab_hdr;
  493. struct elf_link_hash_entry ** sym_hashes;
  494. Elf_Internal_Rela * rel;
  495. Elf_Internal_Rela * relend;
  496. symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
  497. sym_hashes = elf_sym_hashes (input_bfd);
  498. relend = relocs + input_section->reloc_count;
  499. for (rel = relocs; rel < relend; rel ++)
  500. {
  501. reloc_howto_type * howto;
  502. unsigned long r_symndx;
  503. Elf_Internal_Sym * sym;
  504. asection * sec;
  505. struct elf_link_hash_entry * h;
  506. bfd_vma relocation;
  507. bfd_reloc_status_type r;
  508. const char * name = NULL;
  509. int r_type;
  510. r_type = ELF32_R_TYPE (rel->r_info);
  511. if ( r_type == R_IQ2000_GNU_VTINHERIT
  512. || r_type == R_IQ2000_GNU_VTENTRY)
  513. continue;
  514. r_symndx = ELF32_R_SYM (rel->r_info);
  515. howto = iq2000_elf_howto_table + ELF32_R_TYPE (rel->r_info);
  516. h = NULL;
  517. sym = NULL;
  518. sec = NULL;
  519. if (r_symndx < symtab_hdr->sh_info)
  520. {
  521. asection *osec;
  522. sym = local_syms + r_symndx;
  523. osec = sec = local_sections [r_symndx];
  524. if ((sec->flags & SEC_MERGE)
  525. && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  526. /* This relocation is relative to a section symbol that is
  527. going to be merged. Change it so that it is relative
  528. to the merged section symbol. */
  529. rel->r_addend = _bfd_elf_rel_local_sym (output_bfd, sym, &sec,
  530. rel->r_addend);
  531. relocation = (sec->output_section->vma
  532. + sec->output_offset
  533. + sym->st_value);
  534. name = bfd_elf_string_from_elf_section
  535. (input_bfd, symtab_hdr->sh_link, sym->st_name);
  536. name = name == NULL ? bfd_section_name (osec) : name;
  537. }
  538. else
  539. {
  540. bool unresolved_reloc;
  541. bool warned, ignored;
  542. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  543. r_symndx, symtab_hdr, sym_hashes,
  544. h, sec, relocation,
  545. unresolved_reloc, warned, ignored);
  546. name = h->root.root.string;
  547. }
  548. if (sec != NULL && discarded_section (sec))
  549. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  550. rel, 1, relend, howto, 0, contents);
  551. if (bfd_link_relocatable (info))
  552. continue;
  553. switch (r_type)
  554. {
  555. case R_IQ2000_HI16:
  556. r = iq2000_elf_relocate_hi16 (input_bfd, rel, contents, relocation);
  557. break;
  558. case R_IQ2000_OFFSET_16:
  559. r = iq2000_elf_relocate_offset16 (input_bfd, rel, contents, relocation,
  560. input_section->output_section->vma
  561. + input_section->output_offset
  562. + rel->r_offset);
  563. break;
  564. case R_IQ2000_PC16:
  565. rel->r_addend -= 4;
  566. /* Fall through. */
  567. default:
  568. r = iq2000_final_link_relocate (howto, input_bfd, input_section,
  569. contents, rel, relocation);
  570. break;
  571. }
  572. if (r != bfd_reloc_ok)
  573. {
  574. const char * msg = (const char *) NULL;
  575. switch (r)
  576. {
  577. case bfd_reloc_overflow:
  578. (*info->callbacks->reloc_overflow)
  579. (info, (h ? &h->root : NULL), name, howto->name,
  580. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  581. break;
  582. case bfd_reloc_undefined:
  583. (*info->callbacks->undefined_symbol)
  584. (info, name, input_bfd, input_section, rel->r_offset, true);
  585. break;
  586. case bfd_reloc_outofrange:
  587. msg = _("internal error: out of range error");
  588. break;
  589. case bfd_reloc_notsupported:
  590. msg = _("internal error: unsupported relocation error");
  591. break;
  592. case bfd_reloc_dangerous:
  593. msg = _("internal error: dangerous relocation");
  594. break;
  595. default:
  596. msg = _("internal error: unknown error");
  597. break;
  598. }
  599. if (msg)
  600. (*info->callbacks->warning) (info, msg, name, input_bfd,
  601. input_section, rel->r_offset);
  602. }
  603. }
  604. return true;
  605. }
  606. /* Return the section that should be marked against GC for a given
  607. relocation. */
  608. static asection *
  609. iq2000_elf_gc_mark_hook (asection *sec,
  610. struct bfd_link_info *info,
  611. Elf_Internal_Rela *rel,
  612. struct elf_link_hash_entry *h,
  613. Elf_Internal_Sym *sym)
  614. {
  615. if (h != NULL)
  616. switch (ELF32_R_TYPE (rel->r_info))
  617. {
  618. case R_IQ2000_GNU_VTINHERIT:
  619. case R_IQ2000_GNU_VTENTRY:
  620. return NULL;
  621. }
  622. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  623. }
  624. /* Return the MACH for an e_flags value. */
  625. static int
  626. elf32_iq2000_machine (bfd *abfd)
  627. {
  628. switch (elf_elfheader (abfd)->e_flags & EF_IQ2000_CPU_MASK)
  629. {
  630. case EF_IQ2000_CPU_IQ10:
  631. return bfd_mach_iq10;
  632. case EF_IQ2000_CPU_IQ2000:
  633. default:
  634. return bfd_mach_iq2000;
  635. }
  636. }
  637. /* Function to set the ELF flag bits. */
  638. static bool
  639. iq2000_elf_set_private_flags (bfd *abfd, flagword flags)
  640. {
  641. elf_elfheader (abfd)->e_flags = flags;
  642. elf_flags_init (abfd) = true;
  643. return true;
  644. }
  645. /* Merge backend specific data from an object
  646. file to the output object file when linking. */
  647. static bool
  648. iq2000_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  649. {
  650. bfd *obfd = info->output_bfd;
  651. flagword old_flags, old_partial;
  652. flagword new_flags, new_partial;
  653. bool error = false;
  654. char new_opt[80];
  655. char old_opt[80];
  656. new_opt[0] = old_opt[0] = '\0';
  657. new_flags = elf_elfheader (ibfd)->e_flags;
  658. old_flags = elf_elfheader (obfd)->e_flags;
  659. if (!elf_flags_init (obfd))
  660. {
  661. /* First call, no flags set. */
  662. elf_flags_init (obfd) = true;
  663. elf_elfheader (obfd)->e_flags = new_flags;
  664. }
  665. else if (new_flags != old_flags)
  666. {
  667. /* Warn if different cpu is used, but allow a
  668. specific cpu to override the generic cpu. */
  669. new_partial = (new_flags & EF_IQ2000_CPU_MASK);
  670. old_partial = (old_flags & EF_IQ2000_CPU_MASK);
  671. if (new_partial != old_partial)
  672. {
  673. switch (new_partial)
  674. {
  675. case EF_IQ2000_CPU_IQ10:
  676. strcat (new_opt, " -m10");
  677. break;
  678. default:
  679. case EF_IQ2000_CPU_IQ2000:
  680. strcat (new_opt, " -m2000");
  681. break;
  682. }
  683. switch (old_partial)
  684. {
  685. case EF_IQ2000_CPU_IQ10:
  686. strcat (old_opt, " -m10");
  687. break;
  688. default:
  689. case EF_IQ2000_CPU_IQ2000:
  690. strcat (old_opt, " -m2000");
  691. break;
  692. }
  693. }
  694. /* Print out any mismatches from above. */
  695. if (new_opt[0])
  696. {
  697. error = true;
  698. _bfd_error_handler
  699. /* xgettext:c-format */
  700. (_("%pB: compiled with %s and linked with modules compiled with %s"),
  701. ibfd, new_opt, old_opt);
  702. }
  703. new_flags &= ~ EF_IQ2000_ALL_FLAGS;
  704. old_flags &= ~ EF_IQ2000_ALL_FLAGS;
  705. /* Warn about any other mismatches. */
  706. if (new_flags != old_flags)
  707. {
  708. error = true;
  709. _bfd_error_handler
  710. /* xgettext:c-format */
  711. (_("%pB: uses different e_flags (%#x) fields than previous modules (%#x)"),
  712. ibfd, new_flags, old_flags);
  713. }
  714. }
  715. if (error)
  716. bfd_set_error (bfd_error_bad_value);
  717. return !error;
  718. }
  719. static bool
  720. iq2000_elf_print_private_bfd_data (bfd *abfd, void * ptr)
  721. {
  722. FILE *file = (FILE *) ptr;
  723. flagword flags;
  724. BFD_ASSERT (abfd != NULL && ptr != NULL);
  725. /* Print normal ELF private data. */
  726. _bfd_elf_print_private_bfd_data (abfd, ptr);
  727. flags = elf_elfheader (abfd)->e_flags;
  728. fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
  729. switch (flags & EF_IQ2000_CPU_MASK)
  730. {
  731. case EF_IQ2000_CPU_IQ10:
  732. fprintf (file, " -m10");
  733. break;
  734. case EF_IQ2000_CPU_IQ2000:
  735. fprintf (file, " -m2000");
  736. break;
  737. default:
  738. break;
  739. }
  740. fputc ('\n', file);
  741. return true;
  742. }
  743. static
  744. bool
  745. iq2000_elf_object_p (bfd *abfd)
  746. {
  747. bfd_default_set_arch_mach (abfd, bfd_arch_iq2000,
  748. elf32_iq2000_machine (abfd));
  749. return true;
  750. }
  751. #define ELF_ARCH bfd_arch_iq2000
  752. #define ELF_MACHINE_CODE EM_IQ2000
  753. #define ELF_MAXPAGESIZE 0x1000
  754. #define TARGET_BIG_SYM iq2000_elf32_vec
  755. #define TARGET_BIG_NAME "elf32-iq2000"
  756. #define elf_info_to_howto_rel NULL
  757. #define elf_info_to_howto iq2000_info_to_howto_rela
  758. #define elf_backend_relocate_section iq2000_elf_relocate_section
  759. #define elf_backend_gc_mark_hook iq2000_elf_gc_mark_hook
  760. #define elf_backend_check_relocs iq2000_elf_check_relocs
  761. #define elf_backend_object_p iq2000_elf_object_p
  762. #define elf_backend_rela_normal 1
  763. #define elf_backend_can_gc_sections 1
  764. #define bfd_elf32_bfd_reloc_type_lookup iq2000_reloc_type_lookup
  765. #define bfd_elf32_bfd_reloc_name_lookup iq2000_reloc_name_lookup
  766. #define bfd_elf32_bfd_set_private_flags iq2000_elf_set_private_flags
  767. #define bfd_elf32_bfd_merge_private_bfd_data iq2000_elf_merge_private_bfd_data
  768. #define bfd_elf32_bfd_print_private_bfd_data iq2000_elf_print_private_bfd_data
  769. #include "elf32-target.h"