elf32-mep.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. /* MeP-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., 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/mep.h"
  21. #include "libiberty.h"
  22. /* Forward declarations. */
  23. /* Private relocation functions. */
  24. #define MEPREL(type, size, bits, right, left, pcrel, overflow, mask) \
  25. {(unsigned)type, right, size, bits, pcrel, left, overflow, bfd_elf_generic_reloc, #type, FALSE, 0, mask, 0 }
  26. #define N complain_overflow_dont
  27. #define S complain_overflow_signed
  28. #define U complain_overflow_unsigned
  29. static reloc_howto_type mep_elf_howto_table [] =
  30. {
  31. /* type, size, bits, leftshift, rightshift, pcrel, OD/OS/OU, mask. */
  32. MEPREL (R_MEP_NONE, 3, 0, 0, 0, 0, N, 0),
  33. MEPREL (R_RELC, 0, 0, 0, 0, 0, N, 0),
  34. /* MEPRELOC:HOWTO */
  35. /* This section generated from bfd/mep-relocs.pl from include/elf/mep.h. */
  36. MEPREL (R_MEP_8, 0, 8, 0, 0, 0, U, 0xff),
  37. MEPREL (R_MEP_16, 1, 16, 0, 0, 0, U, 0xffff),
  38. MEPREL (R_MEP_32, 2, 32, 0, 0, 0, U, 0xffffffff),
  39. MEPREL (R_MEP_PCREL8A2, 1, 8, 1, 1, 1, S, 0x00fe),
  40. MEPREL (R_MEP_PCREL12A2,1, 12, 1, 1, 1, S, 0x0ffe),
  41. MEPREL (R_MEP_PCREL17A2,2, 17, 0, 1, 1, S, 0x0000ffff),
  42. MEPREL (R_MEP_PCREL24A2,2, 24, 0, 1, 1, S, 0x07f0ffff),
  43. MEPREL (R_MEP_PCABS24A2,2, 24, 0, 1, 0, U, 0x07f0ffff),
  44. MEPREL (R_MEP_LOW16, 2, 16, 0, 0, 0, N, 0x0000ffff),
  45. MEPREL (R_MEP_HI16U, 2, 32, 0,16, 0, N, 0x0000ffff),
  46. MEPREL (R_MEP_HI16S, 2, 32, 0,16, 0, N, 0x0000ffff),
  47. MEPREL (R_MEP_GPREL, 2, 16, 0, 0, 0, S, 0x0000ffff),
  48. MEPREL (R_MEP_TPREL, 2, 16, 0, 0, 0, S, 0x0000ffff),
  49. MEPREL (R_MEP_TPREL7, 1, 7, 0, 0, 0, U, 0x007f),
  50. MEPREL (R_MEP_TPREL7A2, 1, 7, 1, 1, 0, U, 0x007e),
  51. MEPREL (R_MEP_TPREL7A4, 1, 7, 2, 2, 0, U, 0x007c),
  52. MEPREL (R_MEP_UIMM24, 2, 24, 0, 0, 0, U, 0x00ffffff),
  53. MEPREL (R_MEP_ADDR24A4, 2, 24, 0, 2, 0, U, 0x00fcffff),
  54. MEPREL (R_MEP_GNU_VTINHERIT,1, 0,16,32, 0, N, 0x0000),
  55. MEPREL (R_MEP_GNU_VTENTRY,1, 0,16,32, 0, N, 0x0000),
  56. /* MEPRELOC:END */
  57. };
  58. #define VALID_MEP_RELOC(N) ((N) >= 0 \
  59. && (N) < ARRAY_SIZE (mep_elf_howto_table)
  60. #undef N
  61. #undef S
  62. #undef U
  63. #define BFD_RELOC_MEP_NONE BFD_RELOC_NONE
  64. #if defined (__STDC__) || defined (ALMOST_STDC) || defined (HAVE_STRINGIZE)
  65. #define MAP(n) case BFD_RELOC_MEP_##n: type = R_MEP_##n; break
  66. #else
  67. #define MAP(n) case BFD_RELOC_MEP_/**/n: type = R_MEP_/**/n; break
  68. #endif
  69. static reloc_howto_type *
  70. mep_reloc_type_lookup
  71. (bfd * abfd ATTRIBUTE_UNUSED,
  72. bfd_reloc_code_real_type code)
  73. {
  74. unsigned int type = 0;
  75. switch (code)
  76. {
  77. MAP(NONE);
  78. case BFD_RELOC_8:
  79. type = R_MEP_8;
  80. break;
  81. case BFD_RELOC_16:
  82. type = R_MEP_16;
  83. break;
  84. case BFD_RELOC_32:
  85. type = R_MEP_32;
  86. break;
  87. case BFD_RELOC_VTABLE_ENTRY:
  88. type = R_MEP_GNU_VTENTRY;
  89. break;
  90. case BFD_RELOC_VTABLE_INHERIT:
  91. type = R_MEP_GNU_VTINHERIT;
  92. break;
  93. case BFD_RELOC_RELC:
  94. type = R_RELC;
  95. break;
  96. /* MEPRELOC:MAP */
  97. /* This section generated from bfd/mep-relocs.pl from include/elf/mep.h. */
  98. MAP(8);
  99. MAP(16);
  100. MAP(32);
  101. MAP(PCREL8A2);
  102. MAP(PCREL12A2);
  103. MAP(PCREL17A2);
  104. MAP(PCREL24A2);
  105. MAP(PCABS24A2);
  106. MAP(LOW16);
  107. MAP(HI16U);
  108. MAP(HI16S);
  109. MAP(GPREL);
  110. MAP(TPREL);
  111. MAP(TPREL7);
  112. MAP(TPREL7A2);
  113. MAP(TPREL7A4);
  114. MAP(UIMM24);
  115. MAP(ADDR24A4);
  116. MAP(GNU_VTINHERIT);
  117. MAP(GNU_VTENTRY);
  118. /* MEPRELOC:END */
  119. default:
  120. /* Pacify gcc -Wall. */
  121. (*_bfd_error_handler) (_("mep: no reloc for code %d"), code);
  122. return NULL;
  123. }
  124. if (mep_elf_howto_table[type].type != type)
  125. {
  126. (*_bfd_error_handler) (_("MeP: howto %d has type %d"),
  127. type, mep_elf_howto_table[type].type);
  128. abort ();
  129. }
  130. return mep_elf_howto_table + type;
  131. }
  132. #undef MAP
  133. static reloc_howto_type *
  134. mep_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
  135. {
  136. unsigned int i;
  137. for (i = 0;
  138. i < sizeof (mep_elf_howto_table) / sizeof (mep_elf_howto_table[0]);
  139. i++)
  140. if (mep_elf_howto_table[i].name != NULL
  141. && strcasecmp (mep_elf_howto_table[i].name, r_name) == 0)
  142. return &mep_elf_howto_table[i];
  143. return NULL;
  144. }
  145. /* Perform a single relocation. */
  146. static struct bfd_link_info *mep_info;
  147. static int warn_tp = 0, warn_sda = 0;
  148. static bfd_vma
  149. mep_lookup_global
  150. (char * name,
  151. bfd_vma ofs,
  152. bfd_vma * cache,
  153. int * warn)
  154. {
  155. struct bfd_link_hash_entry *h;
  156. if (*cache || *warn)
  157. return *cache;
  158. h = bfd_link_hash_lookup (mep_info->hash, name, FALSE, FALSE, TRUE);
  159. if (h == 0 || h->type != bfd_link_hash_defined)
  160. {
  161. *warn = ofs + 1;
  162. return 0;
  163. }
  164. *cache = (h->u.def.value
  165. + h->u.def.section->output_section->vma
  166. + h->u.def.section->output_offset);
  167. return *cache;
  168. }
  169. static bfd_vma
  170. mep_tpoff_base (bfd_vma ofs)
  171. {
  172. static bfd_vma cache = 0;
  173. return mep_lookup_global ("__tpbase", ofs, &cache, &warn_tp);
  174. }
  175. static bfd_vma
  176. mep_sdaoff_base (bfd_vma ofs)
  177. {
  178. static bfd_vma cache = 0;
  179. return mep_lookup_global ("__sdabase", ofs, &cache, &warn_sda);
  180. }
  181. static bfd_reloc_status_type
  182. mep_final_link_relocate
  183. (reloc_howto_type * howto,
  184. bfd * input_bfd,
  185. asection * input_section,
  186. bfd_byte * contents,
  187. Elf_Internal_Rela * rel,
  188. bfd_vma relocation)
  189. {
  190. unsigned long u;
  191. long s;
  192. unsigned char *byte;
  193. bfd_vma pc;
  194. bfd_reloc_status_type r = bfd_reloc_ok;
  195. int e2, e4;
  196. if (bfd_big_endian (input_bfd))
  197. {
  198. e2 = 0;
  199. e4 = 0;
  200. }
  201. else
  202. {
  203. e2 = 1;
  204. e4 = 3;
  205. }
  206. pc = (input_section->output_section->vma
  207. + input_section->output_offset
  208. + rel->r_offset);
  209. s = relocation + rel->r_addend;
  210. byte = (unsigned char *)contents + rel->r_offset;
  211. if (howto->type == R_MEP_PCREL24A2
  212. && s == 0
  213. && pc >= 0x800000)
  214. {
  215. /* This is an unreachable branch to an undefined weak function.
  216. Silently ignore it, since the opcode can't do that but should
  217. never be executed anyway. */
  218. return bfd_reloc_ok;
  219. }
  220. if (howto->pc_relative)
  221. s -= pc;
  222. u = (unsigned long) s;
  223. switch (howto->type)
  224. {
  225. /* MEPRELOC:APPLY */
  226. /* This section generated from bfd/mep-relocs.pl from include/elf/mep.h. */
  227. case R_MEP_8: /* 76543210 */
  228. if (u > 255) r = bfd_reloc_overflow;
  229. byte[0] = (u & 0xff);
  230. break;
  231. case R_MEP_16: /* fedcba9876543210 */
  232. if (u > 65535) r = bfd_reloc_overflow;
  233. byte[0^e2] = ((u >> 8) & 0xff);
  234. byte[1^e2] = (u & 0xff);
  235. break;
  236. case R_MEP_32: /* vutsrqponmlkjihgfedcba9876543210 */
  237. byte[0^e4] = ((u >> 24) & 0xff);
  238. byte[1^e4] = ((u >> 16) & 0xff);
  239. byte[2^e4] = ((u >> 8) & 0xff);
  240. byte[3^e4] = (u & 0xff);
  241. break;
  242. case R_MEP_PCREL8A2: /* --------7654321- */
  243. if (-128 > s || s > 127) r = bfd_reloc_overflow;
  244. byte[1^e2] = (byte[1^e2] & 0x01) | (s & 0xfe);
  245. break;
  246. case R_MEP_PCREL12A2: /* ----ba987654321- */
  247. if (-2048 > s || s > 2047) r = bfd_reloc_overflow;
  248. byte[0^e2] = (byte[0^e2] & 0xf0) | ((s >> 8) & 0x0f);
  249. byte[1^e2] = (byte[1^e2] & 0x01) | (s & 0xfe);
  250. break;
  251. case R_MEP_PCREL17A2: /* ----------------gfedcba987654321 */
  252. if (-65536 > s || s > 65535) r = bfd_reloc_overflow;
  253. byte[2^e2] = ((s >> 9) & 0xff);
  254. byte[3^e2] = ((s >> 1) & 0xff);
  255. break;
  256. case R_MEP_PCREL24A2: /* -----7654321----nmlkjihgfedcba98 */
  257. if (-8388608 > s || s > 8388607) r = bfd_reloc_overflow;
  258. byte[0^e2] = (byte[0^e2] & 0xf8) | ((s >> 5) & 0x07);
  259. byte[1^e2] = (byte[1^e2] & 0x0f) | ((s << 3) & 0xf0);
  260. byte[2^e2] = ((s >> 16) & 0xff);
  261. byte[3^e2] = ((s >> 8) & 0xff);
  262. break;
  263. case R_MEP_PCABS24A2: /* -----7654321----nmlkjihgfedcba98 */
  264. if (u > 16777215) r = bfd_reloc_overflow;
  265. byte[0^e2] = (byte[0^e2] & 0xf8) | ((u >> 5) & 0x07);
  266. byte[1^e2] = (byte[1^e2] & 0x0f) | ((u << 3) & 0xf0);
  267. byte[2^e2] = ((u >> 16) & 0xff);
  268. byte[3^e2] = ((u >> 8) & 0xff);
  269. break;
  270. case R_MEP_LOW16: /* ----------------fedcba9876543210 */
  271. byte[2^e2] = ((u >> 8) & 0xff);
  272. byte[3^e2] = (u & 0xff);
  273. break;
  274. case R_MEP_HI16U: /* ----------------vutsrqponmlkjihg */
  275. byte[2^e2] = ((u >> 24) & 0xff);
  276. byte[3^e2] = ((u >> 16) & 0xff);
  277. break;
  278. case R_MEP_HI16S: /* ----------------vutsrqponmlkjihg */
  279. if (s & 0x8000)
  280. s += 0x10000;
  281. byte[2^e2] = ((s >> 24) & 0xff);
  282. byte[3^e2] = ((s >> 16) & 0xff);
  283. break;
  284. case R_MEP_GPREL: /* ----------------fedcba9876543210 */
  285. s -= mep_sdaoff_base(rel->r_offset);
  286. if (-32768 > s || s > 32767) r = bfd_reloc_overflow;
  287. byte[2^e2] = ((s >> 8) & 0xff);
  288. byte[3^e2] = (s & 0xff);
  289. break;
  290. case R_MEP_TPREL: /* ----------------fedcba9876543210 */
  291. s -= mep_tpoff_base(rel->r_offset);
  292. if (-32768 > s || s > 32767) r = bfd_reloc_overflow;
  293. byte[2^e2] = ((s >> 8) & 0xff);
  294. byte[3^e2] = (s & 0xff);
  295. break;
  296. case R_MEP_TPREL7: /* ---------6543210 */
  297. u -= mep_tpoff_base(rel->r_offset);
  298. if (u > 127) r = bfd_reloc_overflow;
  299. byte[1^e2] = (byte[1^e2] & 0x80) | (u & 0x7f);
  300. break;
  301. case R_MEP_TPREL7A2: /* ---------654321- */
  302. u -= mep_tpoff_base(rel->r_offset);
  303. if (u > 127) r = bfd_reloc_overflow;
  304. byte[1^e2] = (byte[1^e2] & 0x81) | (u & 0x7e);
  305. break;
  306. case R_MEP_TPREL7A4: /* ---------65432-- */
  307. u -= mep_tpoff_base(rel->r_offset);
  308. if (u > 127) r = bfd_reloc_overflow;
  309. byte[1^e2] = (byte[1^e2] & 0x83) | (u & 0x7c);
  310. break;
  311. case R_MEP_UIMM24: /* --------76543210nmlkjihgfedcba98 */
  312. if (u > 16777215) r = bfd_reloc_overflow;
  313. byte[1^e2] = (u & 0xff);
  314. byte[2^e2] = ((u >> 16) & 0xff);
  315. byte[3^e2] = ((u >> 8) & 0xff);
  316. break;
  317. case R_MEP_ADDR24A4: /* --------765432--nmlkjihgfedcba98 */
  318. if (u > 16777215) r = bfd_reloc_overflow;
  319. byte[1^e2] = (byte[1^e2] & 0x03) | (u & 0xfc);
  320. byte[2^e2] = ((u >> 16) & 0xff);
  321. byte[3^e2] = ((u >> 8) & 0xff);
  322. break;
  323. case R_MEP_GNU_VTINHERIT: /* ---------------- */
  324. break;
  325. case R_MEP_GNU_VTENTRY: /* ---------------- */
  326. break;
  327. /* MEPRELOC:END */
  328. default:
  329. abort ();
  330. }
  331. return r;
  332. }
  333. /* Set the howto pointer for a MEP ELF reloc. */
  334. static void
  335. mep_info_to_howto_rela
  336. (bfd * abfd ATTRIBUTE_UNUSED,
  337. arelent * cache_ptr,
  338. Elf_Internal_Rela * dst)
  339. {
  340. unsigned int r_type;
  341. r_type = ELF32_R_TYPE (dst->r_info);
  342. if (r_type >= R_MEP_max)
  343. {
  344. _bfd_error_handler (_("%B: invalid MEP reloc number: %d"), abfd, r_type);
  345. r_type = 0;
  346. }
  347. cache_ptr->howto = & mep_elf_howto_table [r_type];
  348. }
  349. /* Relocate a MEP ELF section.
  350. There is some attempt to make this function usable for many architectures,
  351. both USE_REL and USE_RELA ['twould be nice if such a critter existed],
  352. if only to serve as a learning tool.
  353. The RELOCATE_SECTION function is called by the new ELF backend linker
  354. to handle the relocations for a section.
  355. The relocs are always passed as Rela structures; if the section
  356. actually uses Rel structures, the r_addend field will always be
  357. zero.
  358. This function is responsible for adjusting the section contents as
  359. necessary, and (if using Rela relocs and generating a relocatable
  360. output file) adjusting the reloc addend as necessary.
  361. This function does not have to worry about setting the reloc
  362. address or the reloc symbol index.
  363. LOCAL_SYMS is a pointer to the swapped in local symbols.
  364. LOCAL_SECTIONS is an array giving the section in the input file
  365. corresponding to the st_shndx field of each local symbol.
  366. The global hash table entry for the global symbols can be found
  367. via elf_sym_hashes (input_bfd).
  368. When generating relocatable output, this function must handle
  369. STB_LOCAL/STT_SECTION symbols specially. The output symbol is
  370. going to be the section symbol corresponding to the output
  371. section, which means that the addend must be adjusted
  372. accordingly. */
  373. static bfd_boolean
  374. mep_elf_relocate_section
  375. (bfd * output_bfd ATTRIBUTE_UNUSED,
  376. struct bfd_link_info * info,
  377. bfd * input_bfd,
  378. asection * input_section,
  379. bfd_byte * contents,
  380. Elf_Internal_Rela * relocs,
  381. Elf_Internal_Sym * local_syms,
  382. asection ** local_sections)
  383. {
  384. Elf_Internal_Shdr * symtab_hdr;
  385. struct elf_link_hash_entry ** sym_hashes;
  386. Elf_Internal_Rela * rel;
  387. Elf_Internal_Rela * relend;
  388. symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
  389. sym_hashes = elf_sym_hashes (input_bfd);
  390. relend = relocs + input_section->reloc_count;
  391. mep_info = info;
  392. for (rel = relocs; rel < relend; rel ++)
  393. {
  394. reloc_howto_type * howto;
  395. unsigned long r_symndx;
  396. Elf_Internal_Sym * sym;
  397. asection * sec;
  398. struct elf_link_hash_entry * h;
  399. bfd_vma relocation;
  400. bfd_reloc_status_type r;
  401. const char * name = NULL;
  402. int r_type;
  403. r_type = ELF32_R_TYPE (rel->r_info);
  404. r_symndx = ELF32_R_SYM (rel->r_info);
  405. howto = mep_elf_howto_table + ELF32_R_TYPE (rel->r_info);
  406. h = NULL;
  407. sym = NULL;
  408. sec = NULL;
  409. if (r_symndx < symtab_hdr->sh_info)
  410. {
  411. sym = local_syms + r_symndx;
  412. sec = local_sections [r_symndx];
  413. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  414. name = bfd_elf_string_from_elf_section
  415. (input_bfd, symtab_hdr->sh_link, sym->st_name);
  416. name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
  417. }
  418. else
  419. {
  420. bfd_boolean warned, unresolved_reloc, ignored;
  421. RELOC_FOR_GLOBAL_SYMBOL(info, input_bfd, input_section, rel,
  422. r_symndx, symtab_hdr, sym_hashes,
  423. h, sec, relocation,
  424. unresolved_reloc, warned, ignored);
  425. name = h->root.root.string;
  426. }
  427. if (sec != NULL && discarded_section (sec))
  428. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  429. rel, 1, relend, howto, 0, contents);
  430. if (bfd_link_relocatable (info))
  431. continue;
  432. if (r_type == R_RELC)
  433. r = bfd_elf_perform_complex_relocation (input_bfd, input_section,
  434. contents, rel, relocation);
  435. else
  436. r = mep_final_link_relocate (howto, input_bfd, input_section,
  437. contents, rel, relocation);
  438. if (r != bfd_reloc_ok)
  439. {
  440. const char * msg = (const char *) NULL;
  441. switch (r)
  442. {
  443. case bfd_reloc_overflow:
  444. r = info->callbacks->reloc_overflow
  445. (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
  446. input_bfd, input_section, rel->r_offset);
  447. break;
  448. case bfd_reloc_undefined:
  449. r = info->callbacks->undefined_symbol
  450. (info, name, input_bfd, input_section, rel->r_offset, TRUE);
  451. break;
  452. case bfd_reloc_outofrange:
  453. msg = _("internal error: out of range error");
  454. break;
  455. case bfd_reloc_notsupported:
  456. msg = _("internal error: unsupported relocation error");
  457. break;
  458. case bfd_reloc_dangerous:
  459. msg = _("internal error: dangerous relocation");
  460. break;
  461. default:
  462. msg = _("internal error: unknown error");
  463. break;
  464. }
  465. if (msg)
  466. r = info->callbacks->warning
  467. (info, msg, name, input_bfd, input_section, rel->r_offset);
  468. if (! r)
  469. return FALSE;
  470. }
  471. }
  472. if (warn_tp)
  473. info->callbacks->undefined_symbol
  474. (info, "__tpbase", input_bfd, input_section, warn_tp-1, TRUE);
  475. if (warn_sda)
  476. info->callbacks->undefined_symbol
  477. (info, "__sdabase", input_bfd, input_section, warn_sda-1, TRUE);
  478. if (warn_sda || warn_tp)
  479. return FALSE;
  480. return TRUE;
  481. }
  482. /* Function to set the ELF flag bits. */
  483. static bfd_boolean
  484. mep_elf_set_private_flags (bfd * abfd,
  485. flagword flags)
  486. {
  487. elf_elfheader (abfd)->e_flags = flags;
  488. elf_flags_init (abfd) = TRUE;
  489. return TRUE;
  490. }
  491. /* Merge backend specific data from an object file to the output
  492. object file when linking. */
  493. static bfd_boolean
  494. mep_elf_merge_private_bfd_data (bfd * ibfd, bfd * obfd)
  495. {
  496. static bfd *last_ibfd = 0;
  497. flagword old_flags, new_flags;
  498. flagword old_partial, new_partial;
  499. /* Check if we have the same endianness. */
  500. if (_bfd_generic_verify_endian_match (ibfd, obfd) == FALSE)
  501. return FALSE;
  502. new_flags = elf_elfheader (ibfd)->e_flags;
  503. old_flags = elf_elfheader (obfd)->e_flags;
  504. #ifdef DEBUG
  505. _bfd_error_handler ("%B: old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s",
  506. ibfd, old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no");
  507. #endif
  508. /* First call, no flags set. */
  509. if (!elf_flags_init (obfd))
  510. {
  511. elf_flags_init (obfd) = TRUE;
  512. old_flags = new_flags;
  513. }
  514. else if ((new_flags | old_flags) & EF_MEP_LIBRARY)
  515. {
  516. /* Non-library flags trump library flags. The choice doesn't really
  517. matter if both OLD_FLAGS and NEW_FLAGS have EF_MEP_LIBRARY set. */
  518. if (old_flags & EF_MEP_LIBRARY)
  519. old_flags = new_flags;
  520. }
  521. else
  522. {
  523. /* Make sure they're for the same mach. Allow upgrade from the "mep"
  524. mach. */
  525. new_partial = (new_flags & EF_MEP_CPU_MASK);
  526. old_partial = (old_flags & EF_MEP_CPU_MASK);
  527. if (new_partial == old_partial)
  528. ;
  529. else if (new_partial == EF_MEP_CPU_MEP)
  530. ;
  531. else if (old_partial == EF_MEP_CPU_MEP)
  532. old_flags = (old_flags & ~EF_MEP_CPU_MASK) | new_partial;
  533. else
  534. {
  535. _bfd_error_handler (_("%B and %B are for different cores"), last_ibfd, ibfd);
  536. bfd_set_error (bfd_error_invalid_target);
  537. return FALSE;
  538. }
  539. /* Make sure they're for the same me_module. Allow basic config to
  540. mix with any other. */
  541. new_partial = (new_flags & EF_MEP_INDEX_MASK);
  542. old_partial = (old_flags & EF_MEP_INDEX_MASK);
  543. if (new_partial == old_partial)
  544. ;
  545. else if (new_partial == 0)
  546. ;
  547. else if (old_partial == 0)
  548. old_flags = (old_flags & ~EF_MEP_INDEX_MASK) | new_partial;
  549. else
  550. {
  551. _bfd_error_handler (_("%B and %B are for different configurations"), last_ibfd, ibfd);
  552. bfd_set_error (bfd_error_invalid_target);
  553. return FALSE;
  554. }
  555. }
  556. elf_elfheader (obfd)->e_flags = old_flags;
  557. last_ibfd = ibfd;
  558. return TRUE;
  559. }
  560. /* This will be edited by the MeP configration tool. */
  561. static const char * config_names[] =
  562. {
  563. "basic"
  564. /* start-mepcfgtool */
  565. ,"default"
  566. /* end-mepcfgtool */
  567. };
  568. static const char * core_names[] =
  569. {
  570. "MeP", "MeP-c2", "MeP-c3", "MeP-h1"
  571. };
  572. static bfd_boolean
  573. mep_elf_print_private_bfd_data (bfd * abfd, void * ptr)
  574. {
  575. FILE * file = (FILE *) ptr;
  576. flagword flags, partial_flags;
  577. BFD_ASSERT (abfd != NULL && ptr != NULL);
  578. /* Print normal ELF private data. */
  579. _bfd_elf_print_private_bfd_data (abfd, ptr);
  580. flags = elf_elfheader (abfd)->e_flags;
  581. fprintf (file, _("private flags = 0x%lx"), (unsigned long) flags);
  582. partial_flags = (flags & EF_MEP_CPU_MASK) >> 24;
  583. if (partial_flags < ARRAY_SIZE (core_names))
  584. fprintf (file, " core: %s", core_names[(long)partial_flags]);
  585. partial_flags = flags & EF_MEP_INDEX_MASK;
  586. if (partial_flags < ARRAY_SIZE (config_names))
  587. fprintf (file, " me_module: %s", config_names[(long)partial_flags]);
  588. fputc ('\n', file);
  589. return TRUE;
  590. }
  591. /* Return the machine subcode from the ELF e_flags header. */
  592. static int
  593. elf32_mep_machine (bfd * abfd)
  594. {
  595. switch (elf_elfheader (abfd)->e_flags & EF_MEP_CPU_MASK)
  596. {
  597. default: break;
  598. case EF_MEP_CPU_C2: return bfd_mach_mep;
  599. case EF_MEP_CPU_C3: return bfd_mach_mep;
  600. case EF_MEP_CPU_C4: return bfd_mach_mep;
  601. case EF_MEP_CPU_C5: return bfd_mach_mep_c5;
  602. case EF_MEP_CPU_H1: return bfd_mach_mep_h1;
  603. }
  604. return bfd_mach_mep;
  605. }
  606. static bfd_boolean
  607. mep_elf_object_p (bfd * abfd)
  608. {
  609. bfd_default_set_arch_mach (abfd, bfd_arch_mep, elf32_mep_machine (abfd));
  610. return TRUE;
  611. }
  612. static bfd_boolean
  613. mep_elf_section_flags (flagword * flags, const Elf_Internal_Shdr * hdr)
  614. {
  615. if (hdr->sh_flags & SHF_MEP_VLIW)
  616. * flags |= SEC_MEP_VLIW;
  617. return TRUE;
  618. }
  619. static bfd_boolean
  620. mep_elf_fake_sections (bfd * abfd ATTRIBUTE_UNUSED,
  621. Elf_Internal_Shdr * hdr,
  622. asection * sec)
  623. {
  624. if (sec->flags & SEC_MEP_VLIW)
  625. hdr->sh_flags |= SHF_MEP_VLIW;
  626. return TRUE;
  627. }
  628. #define ELF_ARCH bfd_arch_mep
  629. #define ELF_MACHINE_CODE EM_CYGNUS_MEP
  630. #define ELF_MAXPAGESIZE 0x1000
  631. #define TARGET_BIG_SYM mep_elf32_vec
  632. #define TARGET_BIG_NAME "elf32-mep"
  633. #define TARGET_LITTLE_SYM mep_elf32_le_vec
  634. #define TARGET_LITTLE_NAME "elf32-mep-little"
  635. #define elf_info_to_howto_rel NULL
  636. #define elf_info_to_howto mep_info_to_howto_rela
  637. #define elf_backend_relocate_section mep_elf_relocate_section
  638. #define elf_backend_object_p mep_elf_object_p
  639. #define elf_backend_section_flags mep_elf_section_flags
  640. #define elf_backend_fake_sections mep_elf_fake_sections
  641. #define bfd_elf32_bfd_reloc_type_lookup mep_reloc_type_lookup
  642. #define bfd_elf32_bfd_reloc_name_lookup mep_reloc_name_lookup
  643. #define bfd_elf32_bfd_set_private_flags mep_elf_set_private_flags
  644. #define bfd_elf32_bfd_merge_private_bfd_data mep_elf_merge_private_bfd_data
  645. #define bfd_elf32_bfd_print_private_bfd_data mep_elf_print_private_bfd_data
  646. #define elf_backend_rela_normal 1
  647. #include "elf32-target.h"