coff-mcore.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /* BFD back-end for Motorola MCore COFF/PE
  2. Copyright (C) 1999-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, 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "libbfd.h"
  19. #include "coff/mcore.h"
  20. #include "coff/internal.h"
  21. #include "coff/pe.h"
  22. #include "libcoff.h"
  23. #ifdef BADMAG
  24. #undef BADMAG
  25. #endif
  26. #define BADMAG(x) MCOREBADMAG(x)
  27. #ifndef NUM_ELEM
  28. #define NUM_ELEM(A) (sizeof (A) / sizeof (A)[0])
  29. #endif
  30. /* This file is compiled more than once, but we only compile the
  31. final_link routine once. */
  32. extern bool mcore_bfd_coff_final_link
  33. (bfd *, struct bfd_link_info *);
  34. static bfd_reloc_status_type mcore_coff_unsupported_reloc
  35. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  36. /* The NT loader points the toc register to &toc + 32768, in order to
  37. use the complete range of a 16-bit displacement. We have to adjust
  38. for this when we fix up loads displaced off the toc reg. */
  39. #define TOC_LOAD_ADJUSTMENT (-32768)
  40. #define TOC_SECTION_NAME ".private.toc"
  41. /* The main body of code is in coffcode.h. */
  42. #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 2
  43. /* In case we're on a 32-bit machine, construct a 64-bit "-1" value
  44. from smaller values. Start with zero, widen, *then* decrement. */
  45. #define MINUS_ONE (((bfd_vma)0) - 1)
  46. static reloc_howto_type mcore_coff_howto_table[] =
  47. {
  48. /* Unused: */
  49. HOWTO (IMAGE_REL_MCORE_ABSOLUTE,/* type */
  50. 0, /* rightshift */
  51. 0, /* size (0 = byte, 1 = short, 2 = long) */
  52. 0, /* bitsize */
  53. false, /* pc_relative */
  54. 0, /* bitpos */
  55. complain_overflow_dont, /* dont complain_on_overflow */
  56. NULL, /* special_function */
  57. "ABSOLUTE", /* name */
  58. false, /* partial_inplace */
  59. 0x00, /* src_mask */
  60. 0x00, /* dst_mask */
  61. false), /* pcrel_offset */
  62. HOWTO (IMAGE_REL_MCORE_ADDR32,/* type */
  63. 0, /* rightshift */
  64. 2, /* size (0 = byte, 1 = short, 2 = long) */
  65. 32, /* bitsize */
  66. false, /* pc_relative */
  67. 0, /* bitpos */
  68. complain_overflow_bitfield, /* complain_on_overflow */
  69. NULL, /* special_function */
  70. "ADDR32", /* name */
  71. true, /* partial_inplace */
  72. 0xffffffff, /* src_mask */
  73. 0xffffffff, /* dst_mask */
  74. false), /* pcrel_offset */
  75. /* 8 bits + 2 zero bits; jmpi/jsri/lrw instructions.
  76. Should not appear in object files. */
  77. HOWTO (IMAGE_REL_MCORE_PCREL_IMM8BY4, /* type */
  78. 2, /* rightshift */
  79. 1, /* size (0 = byte, 1 = short, 2 = long) */
  80. 8, /* bitsize */
  81. true, /* pc_relative */
  82. 0, /* bitpos */
  83. complain_overflow_bitfield, /* complain_on_overflow */
  84. mcore_coff_unsupported_reloc, /* special_function */
  85. "IMM8BY4", /* name */
  86. false, /* partial_inplace */
  87. 0, /* src_mask */
  88. 0, /* dst_mask */
  89. true), /* pcrel_offset */
  90. /* bsr/bt/bf/br instructions; 11 bits + 1 zero bit
  91. Span 2k instructions == 4k bytes.
  92. Only useful pieces at the relocated address are the opcode (5 bits) */
  93. HOWTO (IMAGE_REL_MCORE_PCREL_IMM11BY2,/* type */
  94. 1, /* rightshift */
  95. 1, /* size (0 = byte, 1 = short, 2 = long) */
  96. 11, /* bitsize */
  97. true, /* pc_relative */
  98. 0, /* bitpos */
  99. complain_overflow_signed, /* complain_on_overflow */
  100. NULL, /* special_function */
  101. "IMM11BY2", /* name */
  102. false, /* partial_inplace */
  103. 0x0, /* src_mask */
  104. 0x7ff, /* dst_mask */
  105. true), /* pcrel_offset */
  106. /* 4 bits + 1 zero bit; 'loopt' instruction only; unsupported. */
  107. HOWTO (IMAGE_REL_MCORE_PCREL_IMM4BY2, /* type */
  108. 1, /* rightshift */
  109. 1, /* size (0 = byte, 1 = short, 2 = long) */
  110. 4, /* bitsize */
  111. true, /* pc_relative */
  112. 0, /* bitpos */
  113. complain_overflow_bitfield, /* complain_on_overflow */
  114. mcore_coff_unsupported_reloc, /* special_function */
  115. "IMM4BY2", /* name */
  116. false, /* partial_inplace */
  117. 0, /* src_mask */
  118. 0, /* dst_mask */
  119. true), /* pcrel_offset */
  120. /* 32-bit pc-relative. Eventually this will help support PIC code. */
  121. HOWTO (IMAGE_REL_MCORE_PCREL_32,/* type */
  122. 0, /* rightshift */
  123. 2, /* size (0 = byte, 1 = short, 2 = long) */
  124. 32, /* bitsize */
  125. true, /* pc_relative */
  126. 0, /* bitpos */
  127. complain_overflow_bitfield, /* complain_on_overflow */
  128. NULL, /* special_function */
  129. "PCREL_32", /* name */
  130. false, /* partial_inplace */
  131. 0x0, /* src_mask */
  132. 0xffffffff, /* dst_mask */
  133. true), /* pcrel_offset */
  134. /* Like PCREL_IMM11BY2, this relocation indicates that there is a
  135. 'jsri' at the specified address. There is a separate relocation
  136. entry for the literal pool entry that it references, but we
  137. might be able to change the jsri to a bsr if the target turns out
  138. to be close enough [even though we won't reclaim the literal pool
  139. entry, we'll get some runtime efficiency back]. Note that this
  140. is a relocation that we are allowed to safely ignore. */
  141. HOWTO (IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2,/* type */
  142. 1, /* rightshift */
  143. 1, /* size (0 = byte, 1 = short, 2 = long) */
  144. 11, /* bitsize */
  145. true, /* pc_relative */
  146. 0, /* bitpos */
  147. complain_overflow_signed, /* complain_on_overflow */
  148. NULL, /* special_function */
  149. "JSR_IMM11BY2", /* name */
  150. false, /* partial_inplace */
  151. 0x0, /* src_mask */
  152. 0x7ff, /* dst_mask */
  153. true), /* pcrel_offset */
  154. HOWTO (IMAGE_REL_MCORE_RVA, /* type */
  155. 0, /* rightshift */
  156. 2, /* size (0 = byte, 1 = short, 2 = long) */
  157. 32, /* bitsize */
  158. false, /* pc_relative */
  159. 0, /* bitpos */
  160. complain_overflow_signed, /* complain_on_overflow */
  161. NULL, /* special_function */
  162. "MCORE_RVA", /* name */
  163. true, /* partial_inplace */
  164. 0xffffffff, /* src_mask */
  165. 0xffffffff, /* dst_mask */
  166. true) /* pcrel_offset */
  167. };
  168. /* Extend the coff_link_hash_table structure with a few M*Core specific fields.
  169. This allows us to store global data here without actually creating any
  170. global variables, which is a no-no in the BFD world. */
  171. typedef struct coff_mcore_link_hash_table
  172. {
  173. /* The original coff_link_hash_table structure. MUST be first field. */
  174. struct coff_link_hash_table root;
  175. bfd * bfd_of_toc_owner;
  176. long int global_toc_size;
  177. long int import_table_size;
  178. long int first_thunk_address;
  179. long int thunk_size;
  180. }
  181. mcore_hash_table;
  182. /* Get the MCore coff linker hash table from a link_info structure. */
  183. #define coff_mcore_hash_table(info) \
  184. ((mcore_hash_table *) ((info)->hash))
  185. /* Add an entry to the base file. */
  186. static bool
  187. mcore_emit_base_file_entry (struct bfd_link_info *info,
  188. bfd *output_bfd,
  189. asection *input_section,
  190. bfd_vma reloc_offset)
  191. {
  192. bfd_vma addr = reloc_offset
  193. - input_section->vma
  194. + input_section->output_offset
  195. + input_section->output_section->vma;
  196. if (coff_data (output_bfd)->pe)
  197. addr -= pe_data (output_bfd)->pe_opthdr.ImageBase;
  198. if (fwrite (&addr, sizeof (addr), 1, (FILE *) info->base_file) == 1)
  199. return true;
  200. bfd_set_error (bfd_error_system_call);
  201. return false;
  202. }
  203. static bfd_reloc_status_type
  204. mcore_coff_unsupported_reloc (bfd * abfd,
  205. arelent * reloc_entry,
  206. asymbol * symbol ATTRIBUTE_UNUSED,
  207. void * data ATTRIBUTE_UNUSED,
  208. asection * input_section ATTRIBUTE_UNUSED,
  209. bfd * output_bfd ATTRIBUTE_UNUSED,
  210. char ** error_message ATTRIBUTE_UNUSED)
  211. {
  212. BFD_ASSERT (reloc_entry->howto != (reloc_howto_type *)0);
  213. /* xgettext: c-format */
  214. _bfd_error_handler (_("%pB: Relocation %s (%d) is not currently supported.\n"),
  215. abfd,
  216. reloc_entry->howto->name,
  217. reloc_entry->howto->type);
  218. return bfd_reloc_notsupported;
  219. }
  220. /* A cheesy little macro to make the code a little more readable. */
  221. #define HOW2MAP(bfd_rtype, mcore_rtype) \
  222. case bfd_rtype: return & mcore_coff_howto_table [mcore_rtype]
  223. static reloc_howto_type *
  224. mcore_coff_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
  225. bfd_reloc_code_real_type code)
  226. {
  227. switch (code)
  228. {
  229. HOW2MAP (BFD_RELOC_32, IMAGE_REL_MCORE_ADDR32);
  230. HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM8BY4, IMAGE_REL_MCORE_PCREL_IMM8BY4);
  231. HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM11BY2, IMAGE_REL_MCORE_PCREL_IMM11BY2);
  232. HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM4BY2, IMAGE_REL_MCORE_PCREL_IMM4BY2);
  233. HOW2MAP (BFD_RELOC_32_PCREL, IMAGE_REL_MCORE_PCREL_32);
  234. HOW2MAP (BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2, IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2);
  235. HOW2MAP (BFD_RELOC_RVA, IMAGE_REL_MCORE_RVA);
  236. default:
  237. return NULL;
  238. }
  239. /*NOTREACHED*/
  240. }
  241. #undef HOW2MAP
  242. #define NUM_HOWTOS NUM_ELEM (mcore_coff_howto_table)
  243. static reloc_howto_type *
  244. mcore_coff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  245. const char *r_name)
  246. {
  247. unsigned int i;
  248. for (i = 0; i < NUM_HOWTOS; i++)
  249. if (mcore_coff_howto_table[i].name != NULL
  250. && strcasecmp (mcore_coff_howto_table[i].name, r_name) == 0)
  251. return &mcore_coff_howto_table[i];
  252. return NULL;
  253. }
  254. #define RTYPE2HOWTO(cache_ptr, dst) \
  255. ((cache_ptr)->howto = \
  256. ((dst)->r_type < NUM_HOWTOS \
  257. ? mcore_coff_howto_table + (dst)->r_type \
  258. : NULL))
  259. static reloc_howto_type *
  260. coff_mcore_rtype_to_howto (bfd * abfd ATTRIBUTE_UNUSED,
  261. asection * sec,
  262. struct internal_reloc * rel,
  263. struct coff_link_hash_entry * h ATTRIBUTE_UNUSED,
  264. struct internal_syment * sym,
  265. bfd_vma * addendp)
  266. {
  267. reloc_howto_type * howto;
  268. if (rel->r_type >= NUM_HOWTOS)
  269. return NULL;
  270. howto = mcore_coff_howto_table + rel->r_type;
  271. if (rel->r_type == IMAGE_REL_MCORE_RVA)
  272. * addendp -= pe_data (sec->output_section->owner)->pe_opthdr.ImageBase;
  273. else if (howto->pc_relative)
  274. {
  275. * addendp = sec->vma - 2; /* XXX guess - is this right ? */
  276. /* If the symbol is defined, then the generic code is going to
  277. add back the symbol value in order to cancel out an
  278. adjustment it made to the addend. However, we set the addend
  279. to 0 at the start of this function. We need to adjust here,
  280. to avoid the adjustment the generic code will make. FIXME:
  281. This is getting a bit hackish. */
  282. if (sym != NULL && sym->n_scnum != 0)
  283. * addendp -= sym->n_value;
  284. }
  285. else
  286. * addendp = 0;
  287. return howto;
  288. }
  289. /* Return TRUE if this relocation should appear in the output .reloc section.
  290. This function is referenced in pe_mkobject in peicode.h. */
  291. static bool
  292. in_reloc_p (bfd * abfd ATTRIBUTE_UNUSED, reloc_howto_type * howto)
  293. {
  294. return ! howto->pc_relative && howto->type != IMAGE_REL_MCORE_RVA;
  295. }
  296. /* The reloc processing routine for the optimized COFF linker. */
  297. static bool
  298. coff_mcore_relocate_section (bfd * output_bfd,
  299. struct bfd_link_info * info,
  300. bfd * input_bfd,
  301. asection * input_section,
  302. bfd_byte * contents,
  303. struct internal_reloc * relocs,
  304. struct internal_syment * syms,
  305. asection ** sections)
  306. {
  307. struct internal_reloc * rel;
  308. struct internal_reloc * relend;
  309. /* If we are performing a relocatable link, we don't need to do a
  310. thing. The caller will take care of adjusting the reloc
  311. addresses and symbol indices. */
  312. if (bfd_link_relocatable (info))
  313. return true;
  314. /* Check if we have the same endianness */
  315. if ( input_bfd->xvec->byteorder != output_bfd->xvec->byteorder
  316. && output_bfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
  317. {
  318. _bfd_error_handler
  319. /* xgettext: c-format */
  320. (_("%pB: compiled for a %s system and target is %s.\n"),
  321. input_bfd,
  322. bfd_big_endian (input_bfd) ? _("big endian") : _("little endian"),
  323. bfd_big_endian (output_bfd) ? _("big endian") : _("little endian"));
  324. bfd_set_error (bfd_error_wrong_format);
  325. return false;
  326. }
  327. rel = relocs;
  328. relend = rel + input_section->reloc_count;
  329. for (; rel < relend; rel++)
  330. {
  331. long symndx;
  332. struct internal_syment * sym;
  333. bfd_vma val;
  334. bfd_vma addend;
  335. bfd_reloc_status_type rstat;
  336. bfd_byte * loc;
  337. unsigned short r_type = rel->r_type;
  338. reloc_howto_type * howto = NULL;
  339. struct coff_link_hash_entry * h;
  340. const char * my_name;
  341. char buf[SYMNMLEN + 1];
  342. symndx = rel->r_symndx;
  343. loc = contents + rel->r_vaddr - input_section->vma;
  344. if (symndx == -1)
  345. {
  346. h = NULL;
  347. sym = NULL;
  348. }
  349. else
  350. {
  351. h = obj_coff_sym_hashes (input_bfd)[symndx];
  352. sym = syms + symndx;
  353. }
  354. addend = 0;
  355. /* Get the howto and initialise the addend. */
  356. howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
  357. sym, & addend);
  358. if (howto == NULL)
  359. return false;
  360. val = 0;
  361. if (h == NULL)
  362. {
  363. if (symndx == -1)
  364. my_name = "*ABS*";
  365. else
  366. {
  367. asection * sec = sections[symndx];
  368. val = (sym->n_value
  369. + sec->output_section->vma
  370. + sec->output_offset);
  371. if (sym == NULL)
  372. my_name = "*unknown*";
  373. else if ( sym->_n._n_n._n_zeroes == 0
  374. && sym->_n._n_n._n_offset != 0)
  375. my_name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
  376. else
  377. {
  378. strncpy (buf, sym->_n._n_name, SYMNMLEN);
  379. buf[SYMNMLEN] = '\0';
  380. my_name = buf;
  381. }
  382. }
  383. }
  384. else
  385. {
  386. if ( h->root.type == bfd_link_hash_defined
  387. || h->root.type == bfd_link_hash_defweak)
  388. {
  389. asection * sec = h->root.u.def.section;
  390. val = (h->root.u.def.value
  391. + sec->output_section->vma
  392. + sec->output_offset);
  393. }
  394. else
  395. (*info->callbacks->undefined_symbol)
  396. (info, h->root.root.string, input_bfd, input_section,
  397. rel->r_vaddr - input_section->vma, true);
  398. my_name = h->root.root.string;
  399. }
  400. rstat = bfd_reloc_ok;
  401. /* Each case must do its own relocation, setting rstat appropriately. */
  402. switch (r_type)
  403. {
  404. default:
  405. /* xgettext: c-format */
  406. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  407. input_bfd, r_type);
  408. bfd_set_error (bfd_error_bad_value);
  409. return false;
  410. case IMAGE_REL_MCORE_ABSOLUTE:
  411. _bfd_error_handler
  412. /* xgettext: c-format */
  413. (_("warning: unsupported reloc %s <file %pB, section %pA>\n"
  414. "sym %ld (%s), r_vaddr %" PRId64 " (%#" PRIx64 ")"),
  415. howto->name, input_bfd, input_section,
  416. rel->r_symndx, my_name, (int64_t) rel->r_vaddr,
  417. (uint64_t) rel->r_vaddr);
  418. break;
  419. case IMAGE_REL_MCORE_PCREL_IMM8BY4:
  420. case IMAGE_REL_MCORE_PCREL_IMM11BY2:
  421. case IMAGE_REL_MCORE_PCREL_IMM4BY2:
  422. case IMAGE_REL_MCORE_PCREL_32:
  423. case IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2:
  424. case IMAGE_REL_MCORE_ADDR32:
  425. /* XXX fixme - shouldn't this be like the code for the RVA reloc ? */
  426. rstat = _bfd_relocate_contents (howto, input_bfd, val, loc);
  427. break;
  428. case IMAGE_REL_MCORE_RVA:
  429. rstat = _bfd_final_link_relocate
  430. (howto, input_bfd,
  431. input_section, contents, rel->r_vaddr - input_section->vma,
  432. val, addend);
  433. break;
  434. }
  435. /* Emit a reloc if the backend thinks it needs it. */
  436. if (info->base_file
  437. && sym
  438. && pe_data (output_bfd)->in_reloc_p (output_bfd, howto)
  439. && !mcore_emit_base_file_entry (info, output_bfd, input_section,
  440. rel->r_vaddr))
  441. return false;
  442. switch (rstat)
  443. {
  444. default:
  445. abort ();
  446. case bfd_reloc_ok:
  447. break;
  448. case bfd_reloc_overflow:
  449. (*info->callbacks->reloc_overflow)
  450. (info, (h ? &h->root : NULL), my_name, howto->name,
  451. (bfd_vma) 0, input_bfd,
  452. input_section, rel->r_vaddr - input_section->vma);
  453. }
  454. }
  455. return true;
  456. }
  457. /* Tailor coffcode.h -- macro heaven. */
  458. /* We use the special COFF backend linker, with our own special touch. */
  459. #define coff_bfd_reloc_type_lookup mcore_coff_reloc_type_lookup
  460. #define coff_bfd_reloc_name_lookup mcore_coff_reloc_name_lookup
  461. #define coff_relocate_section coff_mcore_relocate_section
  462. #define coff_rtype_to_howto coff_mcore_rtype_to_howto
  463. #define SELECT_RELOC(internal, howto) {internal.r_type = howto->type;}
  464. /* Make sure that the 'r_offset' field is copied properly
  465. so that identical binaries will compare the same. */
  466. #define SWAP_IN_RELOC_OFFSET H_GET_32
  467. #define SWAP_OUT_RELOC_OFFSET H_PUT_32
  468. #define COFF_PAGE_SIZE 0x1000
  469. #include "coffcode.h"
  470. /* Forward declaration to initialise alternative_target field. */
  471. extern const bfd_target TARGET_LITTLE_SYM;
  472. /* The transfer vectors that lead the outside world to all of the above. */
  473. CREATE_BIG_COFF_TARGET_VEC (TARGET_BIG_SYM, TARGET_BIG_NAME, D_PAGED,
  474. (SEC_CODE | SEC_DATA | SEC_DEBUGGING | SEC_READONLY | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
  475. 0, & TARGET_LITTLE_SYM, COFF_SWAP_TABLE)
  476. CREATE_LITTLE_COFF_TARGET_VEC (TARGET_LITTLE_SYM, TARGET_LITTLE_NAME, D_PAGED,
  477. (SEC_CODE | SEC_DATA | SEC_DEBUGGING | SEC_READONLY | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
  478. 0, & TARGET_BIG_SYM, COFF_SWAP_TABLE)