elf32-mt.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. /* Morpho Technologies MT specific support for 32-bit ELF
  2. Copyright (C) 2001-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., 59 Temple Place - Suite 330, Boston,
  15. MA 02111-1307, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "libbfd.h"
  19. #include "elf-bfd.h"
  20. #include "elf/mt.h"
  21. /* Prototypes. */
  22. static reloc_howto_type * mt_reloc_type_lookup
  23. (bfd *, bfd_reloc_code_real_type);
  24. static void mt_info_to_howto_rela
  25. (bfd *, arelent *, Elf_Internal_Rela *);
  26. static bfd_reloc_status_type mt_elf_relocate_hi16
  27. (bfd *, Elf_Internal_Rela *, bfd_byte *, bfd_vma);
  28. static bfd_reloc_status_type mt_final_link_relocate
  29. (reloc_howto_type *, bfd *, asection *, bfd_byte *,
  30. Elf_Internal_Rela *, bfd_vma);
  31. static bfd_boolean mt_elf_relocate_section
  32. (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
  33. Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
  34. /* Relocation tables. */
  35. static reloc_howto_type mt_elf_howto_table [] =
  36. {
  37. /* This reloc does nothing. */
  38. HOWTO (R_MT_NONE, /* type */
  39. 0, /* rightshift */
  40. 3, /* size (0 = byte, 1 = short, 2 = long) */
  41. 0, /* bitsize */
  42. FALSE, /* pc_relative */
  43. 0, /* bitpos */
  44. complain_overflow_dont, /* complain_on_overflow */
  45. bfd_elf_generic_reloc, /* special_function */
  46. "R_MT_NONE", /* name */
  47. FALSE, /* partial_inplace */
  48. 0 , /* src_mask */
  49. 0, /* dst_mask */
  50. FALSE), /* pcrel_offset */
  51. /* A 16 bit absolute relocation. */
  52. HOWTO (R_MT_16, /* type */
  53. 0, /* rightshift */
  54. 2, /* size (0 = byte, 1 = short, 2 = long) */
  55. 16, /* bitsize */
  56. FALSE, /* pc_relative */
  57. 0, /* bitpos */
  58. complain_overflow_dont, /* complain_on_overflow */
  59. bfd_elf_generic_reloc, /* special_function */
  60. "R_MT_16", /* name */
  61. FALSE, /* partial_inplace */
  62. 0 , /* src_mask */
  63. 0xffff, /* dst_mask */
  64. FALSE), /* pcrel_offset */
  65. /* A 32 bit absolute relocation. */
  66. HOWTO (R_MT_32, /* type */
  67. 0, /* rightshift */
  68. 2, /* size (0 = byte, 1 = short, 2 = long) */
  69. 32, /* bitsize */
  70. FALSE, /* pc_relative */
  71. 0, /* bitpos */
  72. complain_overflow_dont, /* complain_on_overflow */
  73. bfd_elf_generic_reloc, /* special_function */
  74. "R_MT_32", /* name */
  75. FALSE, /* partial_inplace */
  76. 0 , /* src_mask */
  77. 0xffffffff, /* dst_mask */
  78. FALSE), /* pcrel_offset */
  79. /* A 32 bit pc-relative relocation. */
  80. HOWTO (R_MT_32_PCREL, /* type */
  81. 0, /* rightshift */
  82. 2, /* size (0 = byte, 1 = short, 2 = long) */
  83. 32, /* bitsize */
  84. TRUE, /* pc_relative */
  85. 0, /* bitpos */
  86. complain_overflow_dont, /* complain_on_overflow */
  87. bfd_elf_generic_reloc, /* special_function */
  88. "R_MT_32_PCREL", /* name */
  89. FALSE, /* partial_inplace */
  90. 0 , /* src_mask */
  91. 0xffffffff, /* dst_mask */
  92. TRUE), /* pcrel_offset */
  93. /* A 16 bit pc-relative relocation. */
  94. HOWTO (R_MT_PC16, /* type */
  95. 0, /* rightshift */
  96. 2, /* size (0 = byte, 1 = short, 2 = long) */
  97. 16, /* bitsize */
  98. TRUE, /* pc_relative */
  99. 0, /* bitpos */
  100. complain_overflow_signed, /* complain_on_overflow */
  101. bfd_elf_generic_reloc, /* special_function */
  102. "R_MT_PC16", /* name */
  103. FALSE, /* partial_inplace */
  104. 0, /* src_mask */
  105. 0xffff, /* dst_mask */
  106. TRUE), /* pcrel_offset */
  107. /* high 16 bits of symbol value. */
  108. HOWTO (R_MT_HI16, /* type */
  109. 0, /* rightshift */
  110. 2, /* size (0 = byte, 1 = short, 2 = long) */
  111. 16, /* bitsize */
  112. FALSE, /* pc_relative */
  113. 0, /* bitpos */
  114. complain_overflow_dont, /* complain_on_overflow */
  115. bfd_elf_generic_reloc, /* special_function */
  116. "R_MT_HI16", /* name */
  117. FALSE, /* partial_inplace */
  118. 0xffff0000, /* src_mask */
  119. 0xffff0000, /* dst_mask */
  120. FALSE), /* pcrel_offset */
  121. /* Low 16 bits of symbol value. */
  122. HOWTO (R_MT_LO16, /* type */
  123. 0, /* rightshift */
  124. 2, /* size (0 = byte, 1 = short, 2 = long) */
  125. 16, /* bitsize */
  126. FALSE, /* pc_relative */
  127. 0, /* bitpos */
  128. complain_overflow_dont, /* complain_on_overflow */
  129. bfd_elf_generic_reloc, /* special_function */
  130. "R_MT_LO16", /* name */
  131. FALSE, /* partial_inplace */
  132. 0xffff, /* src_mask */
  133. 0xffff, /* dst_mask */
  134. FALSE), /* pcrel_offset */
  135. };
  136. /* Map BFD reloc types to MT ELF reloc types. */
  137. static reloc_howto_type *
  138. mt_reloc_type_lookup
  139. (bfd * abfd ATTRIBUTE_UNUSED,
  140. bfd_reloc_code_real_type code)
  141. {
  142. /* Note that the mt_elf_howto_table is indxed by the R_
  143. constants. Thus, the order that the howto records appear in the
  144. table *must* match the order of the relocation types defined in
  145. include/elf/mt.h. */
  146. switch (code)
  147. {
  148. case BFD_RELOC_NONE:
  149. return &mt_elf_howto_table[ (int) R_MT_NONE];
  150. case BFD_RELOC_16:
  151. return &mt_elf_howto_table[ (int) R_MT_16];
  152. case BFD_RELOC_32:
  153. return &mt_elf_howto_table[ (int) R_MT_32];
  154. case BFD_RELOC_32_PCREL:
  155. return &mt_elf_howto_table[ (int) R_MT_32_PCREL];
  156. case BFD_RELOC_16_PCREL:
  157. return &mt_elf_howto_table[ (int) R_MT_PC16];
  158. case BFD_RELOC_HI16:
  159. return &mt_elf_howto_table[ (int) R_MT_HI16];
  160. case BFD_RELOC_LO16:
  161. return &mt_elf_howto_table[ (int) R_MT_LO16];
  162. default:
  163. /* Pacify gcc -Wall. */
  164. return NULL;
  165. }
  166. return NULL;
  167. }
  168. static reloc_howto_type *
  169. mt_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  170. const char *r_name)
  171. {
  172. unsigned int i;
  173. for (i = 0;
  174. i < sizeof (mt_elf_howto_table) / sizeof (mt_elf_howto_table[0]);
  175. i++)
  176. if (mt_elf_howto_table[i].name != NULL
  177. && strcasecmp (mt_elf_howto_table[i].name, r_name) == 0)
  178. return &mt_elf_howto_table[i];
  179. return NULL;
  180. }
  181. bfd_reloc_status_type
  182. mt_elf_relocate_hi16
  183. (bfd * input_bfd,
  184. Elf_Internal_Rela * relhi,
  185. bfd_byte * contents,
  186. bfd_vma value)
  187. {
  188. bfd_vma insn;
  189. insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
  190. value += relhi->r_addend;
  191. value >>= 16;
  192. insn = ((insn & ~0xFFFF) | value);
  193. bfd_put_32 (input_bfd, insn, contents + relhi->r_offset);
  194. return bfd_reloc_ok;
  195. }
  196. /* XXX: The following code is the result of a cut&paste. This unfortunate
  197. practice is very widespread in the various target back-end files. */
  198. /* Set the howto pointer for a MT ELF reloc. */
  199. static void
  200. mt_info_to_howto_rela
  201. (bfd * abfd ATTRIBUTE_UNUSED,
  202. arelent * cache_ptr,
  203. Elf_Internal_Rela * dst)
  204. {
  205. unsigned int r_type;
  206. r_type = ELF32_R_TYPE (dst->r_info);
  207. if (r_type >= (unsigned int) R_MT_max)
  208. {
  209. _bfd_error_handler (_("%B: invalid MT reloc number: %d"), abfd, r_type);
  210. r_type = 0;
  211. }
  212. cache_ptr->howto = & mt_elf_howto_table [r_type];
  213. }
  214. /* Perform a single relocation. By default we use the standard BFD
  215. routines. */
  216. static bfd_reloc_status_type
  217. mt_final_link_relocate
  218. (reloc_howto_type * howto,
  219. bfd * input_bfd,
  220. asection * input_section,
  221. bfd_byte * contents,
  222. Elf_Internal_Rela * rel,
  223. bfd_vma relocation)
  224. {
  225. return _bfd_final_link_relocate (howto, input_bfd, input_section,
  226. contents, rel->r_offset,
  227. relocation, rel->r_addend);
  228. }
  229. /* Relocate a MT ELF section.
  230. There is some attempt to make this function usable for many architectures,
  231. both USE_REL and USE_RELA ['twould be nice if such a critter existed],
  232. if only to serve as a learning tool.
  233. The RELOCATE_SECTION function is called by the new ELF backend linker
  234. to handle the relocations for a section.
  235. The relocs are always passed as Rela structures; if the section
  236. actually uses Rel structures, the r_addend field will always be
  237. zero.
  238. This function is responsible for adjusting the section contents as
  239. necessary, and (if using Rela relocs and generating a relocatable
  240. output file) adjusting the reloc addend as necessary.
  241. This function does not have to worry about setting the reloc
  242. address or the reloc symbol index.
  243. LOCAL_SYMS is a pointer to the swapped in local symbols.
  244. LOCAL_SECTIONS is an array giving the section in the input file
  245. corresponding to the st_shndx field of each local symbol.
  246. The global hash table entry for the global symbols can be found
  247. via elf_sym_hashes (input_bfd).
  248. When generating relocatable output, this function must handle
  249. STB_LOCAL/STT_SECTION symbols specially. The output symbol is
  250. going to be the section symbol corresponding to the output
  251. section, which means that the addend must be adjusted
  252. accordingly. */
  253. static bfd_boolean
  254. mt_elf_relocate_section
  255. (bfd * output_bfd ATTRIBUTE_UNUSED,
  256. struct bfd_link_info * info,
  257. bfd * input_bfd,
  258. asection * input_section,
  259. bfd_byte * contents,
  260. Elf_Internal_Rela * relocs,
  261. Elf_Internal_Sym * local_syms,
  262. asection ** local_sections)
  263. {
  264. Elf_Internal_Shdr * symtab_hdr;
  265. struct elf_link_hash_entry ** sym_hashes;
  266. Elf_Internal_Rela * rel;
  267. Elf_Internal_Rela * relend;
  268. symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
  269. sym_hashes = elf_sym_hashes (input_bfd);
  270. relend = relocs + input_section->reloc_count;
  271. for (rel = relocs; rel < relend; rel ++)
  272. {
  273. reloc_howto_type * howto;
  274. unsigned long r_symndx;
  275. Elf_Internal_Sym * sym;
  276. asection * sec;
  277. struct elf_link_hash_entry * h;
  278. bfd_vma relocation;
  279. bfd_reloc_status_type r;
  280. const char * name = NULL;
  281. int r_type;
  282. r_type = ELF32_R_TYPE (rel->r_info);
  283. r_symndx = ELF32_R_SYM (rel->r_info);
  284. howto = mt_elf_howto_table + ELF32_R_TYPE (rel->r_info);
  285. h = NULL;
  286. sym = NULL;
  287. sec = NULL;
  288. if (r_symndx < symtab_hdr->sh_info)
  289. {
  290. sym = local_syms + r_symndx;
  291. sec = local_sections [r_symndx];
  292. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  293. name = bfd_elf_string_from_elf_section
  294. (input_bfd, symtab_hdr->sh_link, sym->st_name);
  295. name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
  296. }
  297. else
  298. {
  299. bfd_boolean unresolved_reloc;
  300. bfd_boolean warned, ignored;
  301. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  302. r_symndx, symtab_hdr, sym_hashes,
  303. h, sec, relocation,
  304. unresolved_reloc, warned, ignored);
  305. name = h->root.root.string;
  306. }
  307. if (sec != NULL && discarded_section (sec))
  308. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  309. rel, 1, relend, howto, 0, contents);
  310. if (bfd_link_relocatable (info))
  311. continue;
  312. /* Finally, the sole MT-specific part. */
  313. switch (r_type)
  314. {
  315. case R_MT_HI16:
  316. r = mt_elf_relocate_hi16 (input_bfd, rel, contents, relocation);
  317. break;
  318. default:
  319. r = mt_final_link_relocate (howto, input_bfd, input_section,
  320. contents, rel, relocation);
  321. break;
  322. }
  323. if (r != bfd_reloc_ok)
  324. {
  325. const char * msg = (const char *) NULL;
  326. switch (r)
  327. {
  328. case bfd_reloc_overflow:
  329. r = info->callbacks->reloc_overflow
  330. (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
  331. input_bfd, input_section, rel->r_offset);
  332. break;
  333. case bfd_reloc_undefined:
  334. r = info->callbacks->undefined_symbol
  335. (info, name, input_bfd, input_section, rel->r_offset, TRUE);
  336. break;
  337. case bfd_reloc_outofrange:
  338. msg = _("internal error: out of range error");
  339. break;
  340. case bfd_reloc_dangerous:
  341. msg = _("internal error: dangerous relocation");
  342. break;
  343. default:
  344. msg = _("internal error: unknown error");
  345. break;
  346. }
  347. if (msg)
  348. r = info->callbacks->warning
  349. (info, msg, name, input_bfd, input_section, rel->r_offset);
  350. if (! r)
  351. return FALSE;
  352. }
  353. }
  354. return TRUE;
  355. }
  356. /* Look through the relocs for a section during the first phase.
  357. Since we don't do .gots or .plts, we just need to consider the
  358. virtual table relocs for gc. */
  359. static bfd_boolean
  360. mt_elf_check_relocs
  361. (bfd * abfd,
  362. struct bfd_link_info * info,
  363. asection * sec,
  364. const Elf_Internal_Rela * relocs)
  365. {
  366. Elf_Internal_Shdr * symtab_hdr;
  367. struct elf_link_hash_entry ** sym_hashes;
  368. const Elf_Internal_Rela * rel;
  369. const Elf_Internal_Rela * rel_end;
  370. if (bfd_link_relocatable (info))
  371. return TRUE;
  372. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  373. sym_hashes = elf_sym_hashes (abfd);
  374. rel_end = relocs + sec->reloc_count;
  375. for (rel = relocs; rel < rel_end; rel++)
  376. {
  377. struct elf_link_hash_entry *h;
  378. unsigned long r_symndx;
  379. r_symndx = ELF32_R_SYM (rel->r_info);
  380. if (r_symndx < symtab_hdr->sh_info)
  381. h = NULL;
  382. else
  383. {
  384. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  385. while (h->root.type == bfd_link_hash_indirect
  386. || h->root.type == bfd_link_hash_warning)
  387. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  388. /* PR15323, ref flags aren't set for references in the same
  389. object. */
  390. h->root.non_ir_ref = 1;
  391. }
  392. }
  393. return TRUE;
  394. }
  395. /* Return the MACH for an e_flags value. */
  396. static int
  397. elf32_mt_machine (bfd *abfd)
  398. {
  399. switch (elf_elfheader (abfd)->e_flags & EF_MT_CPU_MASK)
  400. {
  401. case EF_MT_CPU_MRISC: return bfd_mach_ms1;
  402. case EF_MT_CPU_MRISC2: return bfd_mach_mrisc2;
  403. case EF_MT_CPU_MS2: return bfd_mach_ms2;
  404. }
  405. return bfd_mach_ms1;
  406. }
  407. static bfd_boolean
  408. mt_elf_object_p (bfd * abfd)
  409. {
  410. bfd_default_set_arch_mach (abfd, bfd_arch_mt, elf32_mt_machine (abfd));
  411. return TRUE;
  412. }
  413. /* Function to set the ELF flag bits. */
  414. static bfd_boolean
  415. mt_elf_set_private_flags (bfd * abfd,
  416. flagword flags)
  417. {
  418. elf_elfheader (abfd)->e_flags = flags;
  419. elf_flags_init (abfd) = TRUE;
  420. return TRUE;
  421. }
  422. /* Merge backend specific data from an object file to the output
  423. object file when linking. */
  424. static bfd_boolean
  425. mt_elf_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
  426. {
  427. flagword old_flags, new_flags;
  428. bfd_boolean ok = TRUE;
  429. /* Check if we have the same endianness. */
  430. if (_bfd_generic_verify_endian_match (ibfd, obfd) == FALSE)
  431. return FALSE;
  432. /* If they're not both mt, then merging is meaningless, so just
  433. don't do it. */
  434. if (strcmp (ibfd->arch_info->arch_name, "mt") != 0)
  435. return TRUE;
  436. if (strcmp (obfd->arch_info->arch_name, "mt") != 0)
  437. return TRUE;
  438. new_flags = elf_elfheader (ibfd)->e_flags;
  439. old_flags = elf_elfheader (obfd)->e_flags;
  440. #ifdef DEBUG
  441. _bfd_error_handler ("%B: old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s",
  442. ibfd, old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no");
  443. #endif
  444. if (!elf_flags_init (obfd))
  445. {
  446. old_flags = new_flags;
  447. elf_flags_init (obfd) = TRUE;
  448. }
  449. else if ((new_flags & EF_MT_CPU_MASK) != (old_flags & EF_MT_CPU_MASK))
  450. {
  451. /* CPU has changed. This is invalid, because MRISC, MRISC2 and
  452. MS2 are not subsets of each other. */
  453. ok = FALSE;
  454. }
  455. if (ok)
  456. {
  457. obfd->arch_info = ibfd->arch_info;
  458. elf_elfheader (obfd)->e_flags = old_flags;
  459. }
  460. return ok;
  461. }
  462. static bfd_boolean
  463. mt_elf_print_private_bfd_data (bfd * abfd, void * ptr)
  464. {
  465. FILE * file = (FILE *) ptr;
  466. flagword flags;
  467. BFD_ASSERT (abfd != NULL && ptr != NULL);
  468. /* Print normal ELF private data. */
  469. _bfd_elf_print_private_bfd_data (abfd, ptr);
  470. flags = elf_elfheader (abfd)->e_flags;
  471. fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
  472. switch (flags & EF_MT_CPU_MASK)
  473. {
  474. default:
  475. case EF_MT_CPU_MRISC: fprintf (file, " ms1-16-002"); break;
  476. case EF_MT_CPU_MRISC2: fprintf (file, " ms1-16-003"); break;
  477. case EF_MT_CPU_MS2: fprintf (file, " ms2"); break;
  478. }
  479. fputc ('\n', file);
  480. return TRUE;
  481. }
  482. #define TARGET_BIG_SYM mt_elf32_vec
  483. #define TARGET_BIG_NAME "elf32-mt"
  484. #define ELF_ARCH bfd_arch_mt
  485. #define ELF_MACHINE_CODE EM_MT
  486. #define ELF_MAXPAGESIZE 1 /* No pages on the MT. */
  487. #define elf_info_to_howto_rel NULL
  488. #define elf_info_to_howto mt_info_to_howto_rela
  489. #define elf_backend_relocate_section mt_elf_relocate_section
  490. #define bfd_elf32_bfd_reloc_type_lookup mt_reloc_type_lookup
  491. #define bfd_elf32_bfd_reloc_name_lookup mt_reloc_name_lookup
  492. #define elf_backend_check_relocs mt_elf_check_relocs
  493. #define elf_backend_object_p mt_elf_object_p
  494. #define elf_backend_rela_normal 1
  495. #define elf_backend_can_gc_sections 1
  496. #define bfd_elf32_bfd_set_private_flags mt_elf_set_private_flags
  497. #define bfd_elf32_bfd_merge_private_bfd_data mt_elf_merge_private_bfd_data
  498. #define bfd_elf32_bfd_print_private_bfd_data mt_elf_print_private_bfd_data
  499. #include "elf32-target.h"