elf32-mt.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /* Morpho Technologies MT specific support for 32-bit ELF
  2. Copyright (C) 2001-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/mt.h"
  21. /* Prototypes. */
  22. static reloc_howto_type * mt_reloc_type_lookup
  23. (bfd *, bfd_reloc_code_real_type);
  24. static bool 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 int 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 bool
  200. mt_info_to_howto_rela (bfd *abfd,
  201. arelent *cache_ptr,
  202. Elf_Internal_Rela *dst)
  203. {
  204. unsigned int r_type;
  205. r_type = ELF32_R_TYPE (dst->r_info);
  206. if (r_type >= (unsigned int) R_MT_max)
  207. {
  208. /* xgettext:c-format */
  209. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  210. abfd, r_type);
  211. bfd_set_error (bfd_error_bad_value);
  212. return false;
  213. }
  214. cache_ptr->howto = & mt_elf_howto_table [r_type];
  215. return true;
  216. }
  217. /* Perform a single relocation. By default we use the standard BFD
  218. routines. */
  219. static bfd_reloc_status_type
  220. mt_final_link_relocate
  221. (reloc_howto_type * howto,
  222. bfd * input_bfd,
  223. asection * input_section,
  224. bfd_byte * contents,
  225. Elf_Internal_Rela * rel,
  226. bfd_vma relocation)
  227. {
  228. return _bfd_final_link_relocate (howto, input_bfd, input_section,
  229. contents, rel->r_offset,
  230. relocation, rel->r_addend);
  231. }
  232. /* Relocate a MT ELF section.
  233. There is some attempt to make this function usable for many architectures,
  234. both USE_REL and USE_RELA ['twould be nice if such a critter existed],
  235. if only to serve as a learning tool.
  236. The RELOCATE_SECTION function is called by the new ELF backend linker
  237. to handle the relocations for a section.
  238. The relocs are always passed as Rela structures; if the section
  239. actually uses Rel structures, the r_addend field will always be
  240. zero.
  241. This function is responsible for adjusting the section contents as
  242. necessary, and (if using Rela relocs and generating a relocatable
  243. output file) adjusting the reloc addend as necessary.
  244. This function does not have to worry about setting the reloc
  245. address or the reloc symbol index.
  246. LOCAL_SYMS is a pointer to the swapped in local symbols.
  247. LOCAL_SECTIONS is an array giving the section in the input file
  248. corresponding to the st_shndx field of each local symbol.
  249. The global hash table entry for the global symbols can be found
  250. via elf_sym_hashes (input_bfd).
  251. When generating relocatable output, this function must handle
  252. STB_LOCAL/STT_SECTION symbols specially. The output symbol is
  253. going to be the section symbol corresponding to the output
  254. section, which means that the addend must be adjusted
  255. accordingly. */
  256. static int
  257. mt_elf_relocate_section
  258. (bfd * output_bfd ATTRIBUTE_UNUSED,
  259. struct bfd_link_info * info,
  260. bfd * input_bfd,
  261. asection * input_section,
  262. bfd_byte * contents,
  263. Elf_Internal_Rela * relocs,
  264. Elf_Internal_Sym * local_syms,
  265. asection ** local_sections)
  266. {
  267. Elf_Internal_Shdr * symtab_hdr;
  268. struct elf_link_hash_entry ** sym_hashes;
  269. Elf_Internal_Rela * rel;
  270. Elf_Internal_Rela * relend;
  271. symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
  272. sym_hashes = elf_sym_hashes (input_bfd);
  273. relend = relocs + input_section->reloc_count;
  274. for (rel = relocs; rel < relend; rel ++)
  275. {
  276. reloc_howto_type * howto;
  277. unsigned long r_symndx;
  278. Elf_Internal_Sym * sym;
  279. asection * sec;
  280. struct elf_link_hash_entry * h;
  281. bfd_vma relocation;
  282. bfd_reloc_status_type r;
  283. const char * name = NULL;
  284. int r_type;
  285. r_type = ELF32_R_TYPE (rel->r_info);
  286. r_symndx = ELF32_R_SYM (rel->r_info);
  287. howto = mt_elf_howto_table + ELF32_R_TYPE (rel->r_info);
  288. h = NULL;
  289. sym = NULL;
  290. sec = NULL;
  291. if (r_symndx < symtab_hdr->sh_info)
  292. {
  293. sym = local_syms + r_symndx;
  294. sec = local_sections [r_symndx];
  295. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  296. name = bfd_elf_string_from_elf_section
  297. (input_bfd, symtab_hdr->sh_link, sym->st_name);
  298. name = name == NULL ? bfd_section_name (sec) : name;
  299. }
  300. else
  301. {
  302. bool unresolved_reloc;
  303. bool warned, ignored;
  304. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  305. r_symndx, symtab_hdr, sym_hashes,
  306. h, sec, relocation,
  307. unresolved_reloc, warned, ignored);
  308. name = h->root.root.string;
  309. }
  310. if (sec != NULL && discarded_section (sec))
  311. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  312. rel, 1, relend, howto, 0, contents);
  313. if (bfd_link_relocatable (info))
  314. continue;
  315. /* Finally, the sole MT-specific part. */
  316. switch (r_type)
  317. {
  318. case R_MT_HI16:
  319. r = mt_elf_relocate_hi16 (input_bfd, rel, contents, relocation);
  320. break;
  321. default:
  322. r = mt_final_link_relocate (howto, input_bfd, input_section,
  323. contents, rel, relocation);
  324. break;
  325. }
  326. if (r != bfd_reloc_ok)
  327. {
  328. const char * msg = (const char *) NULL;
  329. switch (r)
  330. {
  331. case bfd_reloc_overflow:
  332. (*info->callbacks->reloc_overflow)
  333. (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
  334. input_bfd, input_section, rel->r_offset);
  335. break;
  336. case bfd_reloc_undefined:
  337. (*info->callbacks->undefined_symbol)
  338. (info, name, input_bfd, input_section, rel->r_offset, true);
  339. break;
  340. case bfd_reloc_outofrange:
  341. msg = _("internal error: out of range error");
  342. break;
  343. case bfd_reloc_dangerous:
  344. msg = _("internal error: dangerous relocation");
  345. break;
  346. default:
  347. msg = _("internal error: unknown error");
  348. break;
  349. }
  350. if (msg)
  351. (*info->callbacks->warning) (info, msg, name, input_bfd,
  352. input_section, rel->r_offset);
  353. }
  354. }
  355. return true;
  356. }
  357. /* Look through the relocs for a section during the first phase.
  358. Since we don't do .gots or .plts, we just need to consider the
  359. virtual table relocs for gc. */
  360. static bool
  361. mt_elf_check_relocs (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. }
  389. }
  390. return true;
  391. }
  392. /* Return the MACH for an e_flags value. */
  393. static int
  394. elf32_mt_machine (bfd *abfd)
  395. {
  396. switch (elf_elfheader (abfd)->e_flags & EF_MT_CPU_MASK)
  397. {
  398. case EF_MT_CPU_MRISC: return bfd_mach_ms1;
  399. case EF_MT_CPU_MRISC2: return bfd_mach_mrisc2;
  400. case EF_MT_CPU_MS2: return bfd_mach_ms2;
  401. }
  402. return bfd_mach_ms1;
  403. }
  404. static bool
  405. mt_elf_object_p (bfd *abfd)
  406. {
  407. bfd_default_set_arch_mach (abfd, bfd_arch_mt, elf32_mt_machine (abfd));
  408. return true;
  409. }
  410. /* Function to set the ELF flag bits. */
  411. static bool
  412. mt_elf_set_private_flags (bfd *abfd, flagword flags)
  413. {
  414. elf_elfheader (abfd)->e_flags = flags;
  415. elf_flags_init (abfd) = true;
  416. return true;
  417. }
  418. /* Merge backend specific data from an object file to the output
  419. object file when linking. */
  420. static bool
  421. mt_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  422. {
  423. bfd *obfd = info->output_bfd;
  424. flagword old_flags, new_flags;
  425. bool ok = true;
  426. /* Check if we have the same endianness. */
  427. if (!_bfd_generic_verify_endian_match (ibfd, info))
  428. return false;
  429. /* If they're not both mt, then merging is meaningless, so just
  430. don't do it. */
  431. if (strcmp (ibfd->arch_info->arch_name, "mt") != 0)
  432. return true;
  433. if (strcmp (obfd->arch_info->arch_name, "mt") != 0)
  434. return true;
  435. new_flags = elf_elfheader (ibfd)->e_flags;
  436. old_flags = elf_elfheader (obfd)->e_flags;
  437. #ifdef DEBUG
  438. _bfd_error_handler ("%pB: old_flags = 0x%.8x, new_flags = 0x%.8x, init = %s",
  439. ibfd, old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no");
  440. #endif
  441. if (!elf_flags_init (obfd))
  442. {
  443. old_flags = new_flags;
  444. elf_flags_init (obfd) = true;
  445. }
  446. else if ((new_flags & EF_MT_CPU_MASK) != (old_flags & EF_MT_CPU_MASK))
  447. {
  448. /* CPU has changed. This is invalid, because MRISC, MRISC2 and
  449. MS2 are not subsets of each other. */
  450. ok = false;
  451. }
  452. if (ok)
  453. {
  454. obfd->arch_info = ibfd->arch_info;
  455. elf_elfheader (obfd)->e_flags = old_flags;
  456. }
  457. return ok;
  458. }
  459. static bool
  460. mt_elf_print_private_bfd_data (bfd *abfd, void *ptr)
  461. {
  462. FILE *file = (FILE *) ptr;
  463. flagword flags;
  464. BFD_ASSERT (abfd != NULL && ptr != NULL);
  465. /* Print normal ELF private data. */
  466. _bfd_elf_print_private_bfd_data (abfd, ptr);
  467. flags = elf_elfheader (abfd)->e_flags;
  468. fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
  469. switch (flags & EF_MT_CPU_MASK)
  470. {
  471. default:
  472. case EF_MT_CPU_MRISC: fprintf (file, " ms1-16-002"); break;
  473. case EF_MT_CPU_MRISC2: fprintf (file, " ms1-16-003"); break;
  474. case EF_MT_CPU_MS2: fprintf (file, " ms2"); break;
  475. }
  476. fputc ('\n', file);
  477. return true;
  478. }
  479. #define TARGET_BIG_SYM mt_elf32_vec
  480. #define TARGET_BIG_NAME "elf32-mt"
  481. #define ELF_ARCH bfd_arch_mt
  482. #define ELF_MACHINE_CODE EM_MT
  483. #define ELF_MAXPAGESIZE 1 /* No pages on the MT. */
  484. #define elf_info_to_howto_rel NULL
  485. #define elf_info_to_howto mt_info_to_howto_rela
  486. #define elf_backend_relocate_section mt_elf_relocate_section
  487. #define bfd_elf32_bfd_reloc_type_lookup mt_reloc_type_lookup
  488. #define bfd_elf32_bfd_reloc_name_lookup mt_reloc_name_lookup
  489. #define elf_backend_check_relocs mt_elf_check_relocs
  490. #define elf_backend_object_p mt_elf_object_p
  491. #define elf_backend_rela_normal 1
  492. #define elf_backend_can_gc_sections 1
  493. #define bfd_elf32_bfd_set_private_flags mt_elf_set_private_flags
  494. #define bfd_elf32_bfd_merge_private_bfd_data mt_elf_merge_private_bfd_data
  495. #define bfd_elf32_bfd_print_private_bfd_data mt_elf_print_private_bfd_data
  496. #include "elf32-target.h"