coff-m68k.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /* BFD back-end for Motorola 68000 COFF binaries.
  2. Copyright (C) 1990-2015 Free Software Foundation, Inc.
  3. Written by Cygnus Support.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "coff/m68k.h"
  21. #include "coff/internal.h"
  22. #include "libcoff.h"
  23. /* This source file is compiled multiple times for various m68k COFF
  24. variants. The following macros control its behaviour:
  25. TARGET_SYM
  26. The C name of the BFD target vector. The default is m68k_coff_vec.
  27. TARGET_NAME
  28. The user visible target name. The default is "coff-m68k".
  29. NAMES_HAVE_UNDERSCORE
  30. Whether symbol names have an underscore.
  31. ONLY_DECLARE_RELOCS
  32. Only declare the relocation howto array. Don't actually compile
  33. it. The actual array will be picked up in another version of the
  34. file.
  35. STATIC_RELOCS
  36. Make the relocation howto array, and associated functions, static.
  37. COFF_COMMON_ADDEND
  38. If this is defined, then, for a relocation against a common
  39. symbol, the object file holds the value (the size) of the common
  40. symbol. If this is not defined, then, for a relocation against a
  41. common symbol, the object file holds zero. */
  42. #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
  43. #ifndef COFF_PAGE_SIZE
  44. /* The page size is a guess based on ELF. */
  45. #define COFF_PAGE_SIZE 0x2000
  46. #endif
  47. #ifndef COFF_COMMON_ADDEND
  48. #define RELOC_SPECIAL_FN 0
  49. #else
  50. static bfd_reloc_status_type m68kcoff_common_addend_special_fn
  51. (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  52. #define RELOC_SPECIAL_FN m68kcoff_common_addend_special_fn
  53. #endif
  54. static bfd_boolean m68k_coff_is_local_label_name (bfd *, const char *);
  55. /* On the delta, a symbol starting with L% is local. We won't see
  56. such a symbol on other platforms, so it should be safe to always
  57. consider it local here. */
  58. static bfd_boolean
  59. m68k_coff_is_local_label_name (bfd *abfd, const char *name)
  60. {
  61. if (name[0] == 'L' && name[1] == '%')
  62. return TRUE;
  63. return _bfd_coff_is_local_label_name (abfd, name);
  64. }
  65. #ifndef STATIC_RELOCS
  66. /* Clean up namespace. */
  67. #define m68kcoff_howto_table _bfd_m68kcoff_howto_table
  68. #define m68k_rtype2howto _bfd_m68kcoff_rtype2howto
  69. #define m68k_howto2rtype _bfd_m68kcoff_howto2rtype
  70. #define m68k_reloc_type_lookup _bfd_m68kcoff_reloc_type_lookup
  71. #define m68k_reloc_name_lookup _bfd_m68kcoff_reloc_name_lookup
  72. #endif
  73. #ifdef ONLY_DECLARE_RELOCS
  74. extern reloc_howto_type m68kcoff_howto_table[];
  75. #else
  76. #ifdef STATIC_RELOCS
  77. static
  78. #endif
  79. reloc_howto_type m68kcoff_howto_table[] =
  80. {
  81. HOWTO (R_RELBYTE, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "8", TRUE, 0x000000ff,0x000000ff, FALSE),
  82. HOWTO (R_RELWORD, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
  83. HOWTO (R_RELLONG, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "32", TRUE, 0xffffffff,0xffffffff, FALSE),
  84. HOWTO (R_PCRBYTE, 0, 0, 8, TRUE, 0, complain_overflow_signed, RELOC_SPECIAL_FN, "DISP8", TRUE, 0x000000ff,0x000000ff, FALSE),
  85. HOWTO (R_PCRWORD, 0, 1, 16, TRUE, 0, complain_overflow_signed, RELOC_SPECIAL_FN, "DISP16", TRUE, 0x0000ffff,0x0000ffff, FALSE),
  86. HOWTO (R_PCRLONG, 0, 2, 32, TRUE, 0, complain_overflow_signed, RELOC_SPECIAL_FN, "DISP32", TRUE, 0xffffffff,0xffffffff, FALSE),
  87. HOWTO (R_RELLONG_NEG, 0, -2, 32, FALSE, 0, complain_overflow_bitfield, RELOC_SPECIAL_FN, "-32", TRUE, 0xffffffff,0xffffffff, FALSE),
  88. };
  89. #endif /* not ONLY_DECLARE_RELOCS */
  90. #ifndef BADMAG
  91. #define BADMAG(x) M68KBADMAG(x)
  92. #endif
  93. #define M68 1 /* Customize coffcode.h */
  94. /* Turn a howto into a reloc number */
  95. #ifdef ONLY_DECLARE_RELOCS
  96. extern void m68k_rtype2howto (arelent *internal, int relocentry);
  97. extern int m68k_howto2rtype (reloc_howto_type *);
  98. extern reloc_howto_type * m68k_reloc_type_lookup
  99. (bfd *, bfd_reloc_code_real_type);
  100. extern reloc_howto_type * m68k_reloc_name_lookup (bfd *, const char *);
  101. #else
  102. #ifdef STATIC_RELOCS
  103. #define STAT_REL static
  104. #else
  105. #define STAT_REL
  106. #endif
  107. STAT_REL void m68k_rtype2howto (arelent *, int);
  108. STAT_REL int m68k_howto2rtype (reloc_howto_type *);
  109. STAT_REL reloc_howto_type * m68k_reloc_type_lookup (bfd *, bfd_reloc_code_real_type);
  110. STAT_REL reloc_howto_type * m68k_reloc_name_lookup (bfd *, const char *);
  111. STAT_REL void
  112. m68k_rtype2howto (arelent *internal, int relocentry)
  113. {
  114. switch (relocentry)
  115. {
  116. case R_RELBYTE: internal->howto = m68kcoff_howto_table + 0; break;
  117. case R_RELWORD: internal->howto = m68kcoff_howto_table + 1; break;
  118. case R_RELLONG: internal->howto = m68kcoff_howto_table + 2; break;
  119. case R_PCRBYTE: internal->howto = m68kcoff_howto_table + 3; break;
  120. case R_PCRWORD: internal->howto = m68kcoff_howto_table + 4; break;
  121. case R_PCRLONG: internal->howto = m68kcoff_howto_table + 5; break;
  122. case R_RELLONG_NEG: internal->howto = m68kcoff_howto_table + 6; break;
  123. default: internal->howto = NULL; break;
  124. }
  125. }
  126. STAT_REL int
  127. m68k_howto2rtype (reloc_howto_type * internal)
  128. {
  129. if (internal->pc_relative)
  130. {
  131. switch (internal->bitsize)
  132. {
  133. case 32: return R_PCRLONG;
  134. case 16: return R_PCRWORD;
  135. case 8: return R_PCRBYTE;
  136. }
  137. }
  138. else
  139. {
  140. switch (internal->bitsize)
  141. {
  142. case 32: return R_RELLONG;
  143. case 16: return R_RELWORD;
  144. case 8: return R_RELBYTE;
  145. }
  146. }
  147. return R_RELLONG;
  148. }
  149. STAT_REL reloc_howto_type *
  150. m68k_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  151. bfd_reloc_code_real_type code)
  152. {
  153. switch (code)
  154. {
  155. default: return NULL;
  156. case BFD_RELOC_8: return m68kcoff_howto_table + 0;
  157. case BFD_RELOC_16: return m68kcoff_howto_table + 1;
  158. case BFD_RELOC_CTOR:
  159. case BFD_RELOC_32: return m68kcoff_howto_table + 2;
  160. case BFD_RELOC_8_PCREL: return m68kcoff_howto_table + 3;
  161. case BFD_RELOC_16_PCREL: return m68kcoff_howto_table + 4;
  162. case BFD_RELOC_32_PCREL: return m68kcoff_howto_table + 5;
  163. /* FIXME: There doesn't seem to be a code for R_RELLONG_NEG. */
  164. }
  165. /*NOTREACHED*/
  166. }
  167. STAT_REL reloc_howto_type *
  168. m68k_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  169. const char *r_name)
  170. {
  171. unsigned int i;
  172. for (i = 0;
  173. i < sizeof (m68kcoff_howto_table) / sizeof (m68kcoff_howto_table[0]);
  174. i++)
  175. if (m68kcoff_howto_table[i].name != NULL
  176. && strcasecmp (m68kcoff_howto_table[i].name, r_name) == 0)
  177. return &m68kcoff_howto_table[i];
  178. return NULL;
  179. }
  180. #endif /* not ONLY_DECLARE_RELOCS */
  181. #define RTYPE2HOWTO(internal, relocentry) \
  182. m68k_rtype2howto(internal, (relocentry)->r_type)
  183. #define SELECT_RELOC(external, internal) \
  184. external.r_type = m68k_howto2rtype (internal)
  185. #define coff_bfd_reloc_type_lookup m68k_reloc_type_lookup
  186. #define coff_bfd_reloc_name_lookup m68k_reloc_name_lookup
  187. #ifndef COFF_COMMON_ADDEND
  188. #ifndef coff_rtype_to_howto
  189. #define coff_rtype_to_howto m68kcoff_rtype_to_howto
  190. static reloc_howto_type *
  191. m68kcoff_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
  192. asection *sec,
  193. struct internal_reloc *rel,
  194. struct coff_link_hash_entry *h ATTRIBUTE_UNUSED,
  195. struct internal_syment *sym ATTRIBUTE_UNUSED,
  196. bfd_vma *addendp)
  197. {
  198. arelent relent;
  199. reloc_howto_type *howto;
  200. relent.howto = NULL;
  201. RTYPE2HOWTO (&relent, rel);
  202. howto = relent.howto;
  203. if (howto != NULL && howto->pc_relative)
  204. *addendp += sec->vma;
  205. return howto;
  206. }
  207. #endif /* ! defined (coff_rtype_to_howto) */
  208. #endif /* ! defined (COFF_COMMON_ADDEND) */
  209. #ifdef COFF_COMMON_ADDEND
  210. /* If COFF_COMMON_ADDEND is defined, then when using m68k COFF the
  211. value stored in the .text section for a reference to a common
  212. symbol is the value itself plus any desired offset. (taken from
  213. work done by Ian Taylor, Cygnus Support, for I386 COFF). */
  214. /* If we are producing relocatable output, we need to do some
  215. adjustments to the object file that are not done by the
  216. bfd_perform_relocation function. This function is called by every
  217. reloc type to make any required adjustments. */
  218. static bfd_reloc_status_type
  219. m68kcoff_common_addend_special_fn (bfd *abfd,
  220. arelent *reloc_entry,
  221. asymbol *symbol,
  222. void * data,
  223. asection *input_section ATTRIBUTE_UNUSED,
  224. bfd *output_bfd,
  225. char **error_message ATTRIBUTE_UNUSED)
  226. {
  227. symvalue diff;
  228. if (output_bfd == (bfd *) NULL)
  229. return bfd_reloc_continue;
  230. if (bfd_is_com_section (symbol->section))
  231. {
  232. /* We are relocating a common symbol. The current value in the
  233. object file is ORIG + OFFSET, where ORIG is the value of the
  234. common symbol as seen by the object file when it was compiled
  235. (this may be zero if the symbol was undefined) and OFFSET is
  236. the offset into the common symbol (normally zero, but may be
  237. non-zero when referring to a field in a common structure).
  238. ORIG is the negative of reloc_entry->addend, which is set by
  239. the CALC_ADDEND macro below. We want to replace the value in
  240. the object file with NEW + OFFSET, where NEW is the value of
  241. the common symbol which we are going to put in the final
  242. object file. NEW is symbol->value. */
  243. diff = symbol->value + reloc_entry->addend;
  244. }
  245. else
  246. {
  247. /* For some reason bfd_perform_relocation always effectively
  248. ignores the addend for a COFF target when producing
  249. relocatable output. This seems to be always wrong for 386
  250. COFF, so we handle the addend here instead. */
  251. diff = reloc_entry->addend;
  252. }
  253. #define DOIT(x) \
  254. x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
  255. if (diff != 0)
  256. {
  257. reloc_howto_type *howto = reloc_entry->howto;
  258. unsigned char *addr = (unsigned char *) data + reloc_entry->address;
  259. switch (howto->size)
  260. {
  261. case 0:
  262. {
  263. char x = bfd_get_8 (abfd, addr);
  264. DOIT (x);
  265. bfd_put_8 (abfd, x, addr);
  266. }
  267. break;
  268. case 1:
  269. {
  270. short x = bfd_get_16 (abfd, addr);
  271. DOIT (x);
  272. bfd_put_16 (abfd, (bfd_vma) x, addr);
  273. }
  274. break;
  275. case 2:
  276. {
  277. long x = bfd_get_32 (abfd, addr);
  278. DOIT (x);
  279. bfd_put_32 (abfd, (bfd_vma) x, addr);
  280. }
  281. break;
  282. default:
  283. abort ();
  284. }
  285. }
  286. /* Now let bfd_perform_relocation finish everything up. */
  287. return bfd_reloc_continue;
  288. }
  289. /* Compute the addend of a reloc. If the reloc is to a common symbol,
  290. the object file contains the value of the common symbol. By the
  291. time this is called, the linker may be using a different symbol
  292. from a different object file with a different value. Therefore, we
  293. hack wildly to locate the original symbol from this file so that we
  294. can make the correct adjustment. This macro sets coffsym to the
  295. symbol from the original file, and uses it to set the addend value
  296. correctly. If this is not a common symbol, the usual addend
  297. calculation is done, except that an additional tweak is needed for
  298. PC relative relocs.
  299. FIXME: This macro refers to symbols and asect; these are from the
  300. calling function, not the macro arguments. */
  301. #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
  302. { \
  303. coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
  304. if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
  305. coffsym = (obj_symbols (abfd) \
  306. + (cache_ptr->sym_ptr_ptr - symbols)); \
  307. else if (ptr) \
  308. coffsym = coff_symbol_from (ptr); \
  309. if (coffsym != (coff_symbol_type *) NULL \
  310. && coffsym->native->u.syment.n_scnum == 0) \
  311. cache_ptr->addend = - coffsym->native->u.syment.n_value; \
  312. else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
  313. && ptr->section != (asection *) NULL) \
  314. cache_ptr->addend = - (ptr->section->vma + ptr->value); \
  315. else \
  316. cache_ptr->addend = 0; \
  317. if (ptr && (reloc.r_type == R_PCRBYTE \
  318. || reloc.r_type == R_PCRWORD \
  319. || reloc.r_type == R_PCRLONG)) \
  320. cache_ptr->addend += asect->vma; \
  321. }
  322. #ifndef coff_rtype_to_howto
  323. /* coff-m68k.c uses the special COFF backend linker. We need to
  324. adjust common symbols. */
  325. static reloc_howto_type *
  326. m68kcoff_common_addend_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
  327. asection *sec,
  328. struct internal_reloc *rel,
  329. struct coff_link_hash_entry *h,
  330. struct internal_syment *sym,
  331. bfd_vma *addendp)
  332. {
  333. arelent relent;
  334. reloc_howto_type *howto;
  335. relent.howto = NULL;
  336. RTYPE2HOWTO (&relent, rel);
  337. howto = relent.howto;
  338. if (howto->pc_relative)
  339. *addendp += sec->vma;
  340. if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
  341. {
  342. /* This is a common symbol. The section contents include the
  343. size (sym->n_value) as an addend. The relocate_section
  344. function will be adding in the final value of the symbol. We
  345. need to subtract out the current size in order to get the
  346. correct result. */
  347. BFD_ASSERT (h != NULL);
  348. *addendp -= sym->n_value;
  349. }
  350. /* If the output symbol is common (in which case this must be a
  351. relocatable link), we need to add in the final size of the
  352. common symbol. */
  353. if (h != NULL && h->root.type == bfd_link_hash_common)
  354. *addendp += h->root.u.c.size;
  355. return howto;
  356. }
  357. #define coff_rtype_to_howto m68kcoff_common_addend_rtype_to_howto
  358. #endif /* ! defined (coff_rtype_to_howto) */
  359. #endif /* COFF_COMMON_ADDEND */
  360. #if !defined ONLY_DECLARE_RELOCS && ! defined STATIC_RELOCS
  361. /* Given a .data section and a .emreloc in-memory section, store
  362. relocation information into the .emreloc section which can be
  363. used at runtime to relocate the section. This is called by the
  364. linker when the --embedded-relocs switch is used. This is called
  365. after the add_symbols entry point has been called for all the
  366. objects, and before the final_link entry point is called. */
  367. bfd_boolean
  368. bfd_m68k_coff_create_embedded_relocs (bfd *abfd,
  369. struct bfd_link_info *info,
  370. asection *datasec,
  371. asection *relsec,
  372. char **errmsg)
  373. {
  374. char *extsyms;
  375. bfd_size_type symesz;
  376. struct internal_reloc *irel, *irelend;
  377. bfd_byte *p;
  378. bfd_size_type amt;
  379. BFD_ASSERT (! bfd_link_relocatable (info));
  380. *errmsg = NULL;
  381. if (datasec->reloc_count == 0)
  382. return TRUE;
  383. extsyms = obj_coff_external_syms (abfd);
  384. symesz = bfd_coff_symesz (abfd);
  385. irel = _bfd_coff_read_internal_relocs (abfd, datasec, TRUE, NULL, FALSE,
  386. NULL);
  387. irelend = irel + datasec->reloc_count;
  388. amt = (bfd_size_type) datasec->reloc_count * 12;
  389. relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
  390. if (relsec->contents == NULL)
  391. return FALSE;
  392. p = relsec->contents;
  393. for (; irel < irelend; irel++, p += 12)
  394. {
  395. asection *targetsec;
  396. /* We are going to write a four byte longword into the runtime
  397. reloc section. The longword will be the address in the data
  398. section which must be relocated. It is followed by the name
  399. of the target section NUL-padded or truncated to 8
  400. characters. */
  401. /* We can only relocate absolute longword relocs at run time. */
  402. if (irel->r_type != R_RELLONG)
  403. {
  404. *errmsg = _("unsupported reloc type");
  405. bfd_set_error (bfd_error_bad_value);
  406. return FALSE;
  407. }
  408. if (irel->r_symndx == -1)
  409. targetsec = bfd_abs_section_ptr;
  410. else
  411. {
  412. struct coff_link_hash_entry *h;
  413. h = obj_coff_sym_hashes (abfd)[irel->r_symndx];
  414. if (h == NULL)
  415. {
  416. struct internal_syment isym;
  417. bfd_coff_swap_sym_in (abfd, extsyms + symesz * irel->r_symndx,
  418. &isym);
  419. targetsec = coff_section_from_bfd_index (abfd, isym.n_scnum);
  420. }
  421. else if (h->root.type == bfd_link_hash_defined
  422. || h->root.type == bfd_link_hash_defweak)
  423. targetsec = h->root.u.def.section;
  424. else
  425. targetsec = NULL;
  426. }
  427. bfd_put_32 (abfd,
  428. (irel->r_vaddr - datasec->vma + datasec->output_offset), p);
  429. memset (p + 4, 0, 8);
  430. if (targetsec != NULL)
  431. strncpy ((char *) p + 4, targetsec->output_section->name, 8);
  432. }
  433. return TRUE;
  434. }
  435. #endif /* neither ONLY_DECLARE_RELOCS not STATIC_RELOCS */
  436. #define coff_bfd_is_local_label_name m68k_coff_is_local_label_name
  437. #define coff_relocate_section _bfd_coff_generic_relocate_section
  438. #ifndef bfd_pe_print_pdata
  439. #define bfd_pe_print_pdata NULL
  440. #endif
  441. #include "coffcode.h"
  442. #ifndef TARGET_SYM
  443. #define TARGET_SYM m68k_coff_vec
  444. #endif
  445. #ifndef TARGET_NAME
  446. #define TARGET_NAME "coff-m68k"
  447. #endif
  448. #ifdef NAMES_HAVE_UNDERSCORE
  449. CREATE_BIG_COFF_TARGET_VEC (TARGET_SYM, TARGET_NAME, D_PAGED, 0, '_', NULL, COFF_SWAP_TABLE)
  450. #else
  451. CREATE_BIG_COFF_TARGET_VEC (TARGET_SYM, TARGET_NAME, D_PAGED, 0, 0, NULL, COFF_SWAP_TABLE)
  452. #endif