elf-m10200.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. /* Matsushita 10200 specific support for 32-bit ELF
  2. Copyright (C) 1996-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. static bool
  21. mn10200_elf_relax_delete_bytes (bfd *, asection *, bfd_vma, int);
  22. static bool
  23. mn10200_elf_symbol_address_p (bfd *, asection *, Elf_Internal_Sym *, bfd_vma);
  24. enum reloc_type
  25. {
  26. R_MN10200_NONE = 0,
  27. R_MN10200_32,
  28. R_MN10200_16,
  29. R_MN10200_8,
  30. R_MN10200_24,
  31. R_MN10200_PCREL8,
  32. R_MN10200_PCREL16,
  33. R_MN10200_PCREL24,
  34. R_MN10200_MAX
  35. };
  36. static reloc_howto_type elf_mn10200_howto_table[] =
  37. {
  38. /* Dummy relocation. Does nothing. */
  39. HOWTO (R_MN10200_NONE,
  40. 0,
  41. 3,
  42. 0,
  43. false,
  44. 0,
  45. complain_overflow_dont,
  46. bfd_elf_generic_reloc,
  47. "R_MN10200_NONE",
  48. false,
  49. 0,
  50. 0,
  51. false),
  52. /* Standard 32 bit reloc. */
  53. HOWTO (R_MN10200_32,
  54. 0,
  55. 2,
  56. 32,
  57. false,
  58. 0,
  59. complain_overflow_bitfield,
  60. bfd_elf_generic_reloc,
  61. "R_MN10200_32",
  62. false,
  63. 0xffffffff,
  64. 0xffffffff,
  65. false),
  66. /* Standard 16 bit reloc. */
  67. HOWTO (R_MN10200_16,
  68. 0,
  69. 1,
  70. 16,
  71. false,
  72. 0,
  73. complain_overflow_bitfield,
  74. bfd_elf_generic_reloc,
  75. "R_MN10200_16",
  76. false,
  77. 0xffff,
  78. 0xffff,
  79. false),
  80. /* Standard 8 bit reloc. */
  81. HOWTO (R_MN10200_8,
  82. 0,
  83. 0,
  84. 8,
  85. false,
  86. 0,
  87. complain_overflow_bitfield,
  88. bfd_elf_generic_reloc,
  89. "R_MN10200_8",
  90. false,
  91. 0xff,
  92. 0xff,
  93. false),
  94. /* Standard 24 bit reloc. */
  95. HOWTO (R_MN10200_24,
  96. 0,
  97. 2,
  98. 24,
  99. false,
  100. 0,
  101. complain_overflow_bitfield,
  102. bfd_elf_generic_reloc,
  103. "R_MN10200_24",
  104. false,
  105. 0xffffff,
  106. 0xffffff,
  107. false),
  108. /* Simple 8 pc-relative reloc. */
  109. HOWTO (R_MN10200_PCREL8,
  110. 0,
  111. 0,
  112. 8,
  113. true,
  114. 0,
  115. complain_overflow_bitfield,
  116. bfd_elf_generic_reloc,
  117. "R_MN10200_PCREL8",
  118. false,
  119. 0xff,
  120. 0xff,
  121. true),
  122. /* Simple 16 pc-relative reloc. */
  123. HOWTO (R_MN10200_PCREL16,
  124. 0,
  125. 1,
  126. 16,
  127. true,
  128. 0,
  129. complain_overflow_bitfield,
  130. bfd_elf_generic_reloc,
  131. "R_MN10200_PCREL16",
  132. false,
  133. 0xffff,
  134. 0xffff,
  135. true),
  136. /* Simple 32bit pc-relative reloc with a 1 byte adjustment
  137. to get the pc-relative offset correct. */
  138. HOWTO (R_MN10200_PCREL24,
  139. 0,
  140. 2,
  141. 24,
  142. true,
  143. 0,
  144. complain_overflow_bitfield,
  145. bfd_elf_generic_reloc,
  146. "R_MN10200_PCREL24",
  147. false,
  148. 0xffffff,
  149. 0xffffff,
  150. true),
  151. };
  152. struct mn10200_reloc_map
  153. {
  154. bfd_reloc_code_real_type bfd_reloc_val;
  155. unsigned char elf_reloc_val;
  156. };
  157. static const struct mn10200_reloc_map mn10200_reloc_map[] =
  158. {
  159. { BFD_RELOC_NONE , R_MN10200_NONE , },
  160. { BFD_RELOC_32 , R_MN10200_32 , },
  161. { BFD_RELOC_16 , R_MN10200_16 , },
  162. { BFD_RELOC_8 , R_MN10200_8 , },
  163. { BFD_RELOC_24 , R_MN10200_24 , },
  164. { BFD_RELOC_8_PCREL , R_MN10200_PCREL8 , },
  165. { BFD_RELOC_16_PCREL, R_MN10200_PCREL16, },
  166. { BFD_RELOC_24_PCREL, R_MN10200_PCREL24, },
  167. };
  168. static reloc_howto_type *
  169. bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  170. bfd_reloc_code_real_type code)
  171. {
  172. unsigned int i;
  173. for (i = 0;
  174. i < sizeof (mn10200_reloc_map) / sizeof (struct mn10200_reloc_map);
  175. i++)
  176. {
  177. if (mn10200_reloc_map[i].bfd_reloc_val == code)
  178. return &elf_mn10200_howto_table[mn10200_reloc_map[i].elf_reloc_val];
  179. }
  180. return NULL;
  181. }
  182. static reloc_howto_type *
  183. bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  184. const char *r_name)
  185. {
  186. unsigned int i;
  187. for (i = 0;
  188. i < (sizeof (elf_mn10200_howto_table)
  189. / sizeof (elf_mn10200_howto_table[0]));
  190. i++)
  191. if (elf_mn10200_howto_table[i].name != NULL
  192. && strcasecmp (elf_mn10200_howto_table[i].name, r_name) == 0)
  193. return &elf_mn10200_howto_table[i];
  194. return NULL;
  195. }
  196. /* Set the howto pointer for an MN10200 ELF reloc. */
  197. static bool
  198. mn10200_info_to_howto (bfd *abfd,
  199. arelent *cache_ptr,
  200. Elf_Internal_Rela *dst)
  201. {
  202. unsigned int r_type;
  203. r_type = ELF32_R_TYPE (dst->r_info);
  204. if (r_type >= (unsigned int) R_MN10200_MAX)
  205. {
  206. /* xgettext:c-format */
  207. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  208. abfd, r_type);
  209. bfd_set_error (bfd_error_bad_value);
  210. return false;
  211. }
  212. cache_ptr->howto = &elf_mn10200_howto_table[r_type];
  213. return cache_ptr->howto != NULL;
  214. }
  215. /* Perform a relocation as part of a final link. */
  216. static bfd_reloc_status_type
  217. mn10200_elf_final_link_relocate (reloc_howto_type *howto,
  218. bfd *input_bfd,
  219. bfd *output_bfd ATTRIBUTE_UNUSED,
  220. asection *input_section,
  221. bfd_byte *contents,
  222. bfd_vma offset,
  223. bfd_vma value,
  224. bfd_vma addend,
  225. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  226. asection *sym_sec ATTRIBUTE_UNUSED,
  227. int is_local ATTRIBUTE_UNUSED)
  228. {
  229. unsigned long r_type = howto->type;
  230. bfd_byte *hit_data = contents + offset;
  231. switch (r_type)
  232. {
  233. case R_MN10200_NONE:
  234. return bfd_reloc_ok;
  235. case R_MN10200_32:
  236. value += addend;
  237. bfd_put_32 (input_bfd, value, hit_data);
  238. return bfd_reloc_ok;
  239. case R_MN10200_16:
  240. value += addend;
  241. if ((long) value > 0x7fff || (long) value < -0x8000)
  242. return bfd_reloc_overflow;
  243. bfd_put_16 (input_bfd, value, hit_data);
  244. return bfd_reloc_ok;
  245. case R_MN10200_8:
  246. value += addend;
  247. if ((long) value > 0x7f || (long) value < -0x80)
  248. return bfd_reloc_overflow;
  249. bfd_put_8 (input_bfd, value, hit_data);
  250. return bfd_reloc_ok;
  251. case R_MN10200_24:
  252. value += addend;
  253. if ((long) value > 0x7fffff || (long) value < -0x800000)
  254. return bfd_reloc_overflow;
  255. value &= 0xffffff;
  256. value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
  257. bfd_put_32 (input_bfd, value, hit_data);
  258. return bfd_reloc_ok;
  259. case R_MN10200_PCREL8:
  260. value -= (input_section->output_section->vma
  261. + input_section->output_offset);
  262. value -= (offset + 1);
  263. value += addend;
  264. if ((long) value > 0xff || (long) value < -0x100)
  265. return bfd_reloc_overflow;
  266. bfd_put_8 (input_bfd, value, hit_data);
  267. return bfd_reloc_ok;
  268. case R_MN10200_PCREL16:
  269. value -= (input_section->output_section->vma
  270. + input_section->output_offset);
  271. value -= (offset + 2);
  272. value += addend;
  273. if ((long) value > 0xffff || (long) value < -0x10000)
  274. return bfd_reloc_overflow;
  275. bfd_put_16 (input_bfd, value, hit_data);
  276. return bfd_reloc_ok;
  277. case R_MN10200_PCREL24:
  278. value -= (input_section->output_section->vma
  279. + input_section->output_offset);
  280. value -= (offset + 3);
  281. value += addend;
  282. if ((long) value > 0xffffff || (long) value < -0x1000000)
  283. return bfd_reloc_overflow;
  284. value &= 0xffffff;
  285. value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
  286. bfd_put_32 (input_bfd, value, hit_data);
  287. return bfd_reloc_ok;
  288. default:
  289. return bfd_reloc_notsupported;
  290. }
  291. }
  292. /* Relocate an MN10200 ELF section. */
  293. static int
  294. mn10200_elf_relocate_section (bfd *output_bfd,
  295. struct bfd_link_info *info,
  296. bfd *input_bfd,
  297. asection *input_section,
  298. bfd_byte *contents,
  299. Elf_Internal_Rela *relocs,
  300. Elf_Internal_Sym *local_syms,
  301. asection **local_sections)
  302. {
  303. Elf_Internal_Shdr *symtab_hdr;
  304. struct elf_link_hash_entry **sym_hashes;
  305. Elf_Internal_Rela *rel, *relend;
  306. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  307. sym_hashes = elf_sym_hashes (input_bfd);
  308. rel = relocs;
  309. relend = relocs + input_section->reloc_count;
  310. for (; rel < relend; rel++)
  311. {
  312. int r_type;
  313. reloc_howto_type *howto;
  314. unsigned long r_symndx;
  315. Elf_Internal_Sym *sym;
  316. asection *sec;
  317. struct elf_link_hash_entry *h;
  318. bfd_vma relocation;
  319. bfd_reloc_status_type r;
  320. r_symndx = ELF32_R_SYM (rel->r_info);
  321. r_type = ELF32_R_TYPE (rel->r_info);
  322. howto = elf_mn10200_howto_table + r_type;
  323. h = NULL;
  324. sym = NULL;
  325. sec = NULL;
  326. if (r_symndx < symtab_hdr->sh_info)
  327. {
  328. sym = local_syms + r_symndx;
  329. sec = local_sections[r_symndx];
  330. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  331. }
  332. else
  333. {
  334. bool unresolved_reloc, warned, ignored;
  335. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  336. r_symndx, symtab_hdr, sym_hashes,
  337. h, sec, relocation,
  338. unresolved_reloc, warned, ignored);
  339. }
  340. if (sec != NULL && discarded_section (sec))
  341. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  342. rel, 1, relend, howto, 0, contents);
  343. if (bfd_link_relocatable (info))
  344. continue;
  345. r = mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
  346. input_section,
  347. contents, rel->r_offset,
  348. relocation, rel->r_addend,
  349. info, sec, h == NULL);
  350. if (r != bfd_reloc_ok)
  351. {
  352. const char *name;
  353. const char *msg = (const char *) 0;
  354. if (h != NULL)
  355. name = h->root.root.string;
  356. else
  357. {
  358. name = (bfd_elf_string_from_elf_section
  359. (input_bfd, symtab_hdr->sh_link, sym->st_name));
  360. if (name == NULL || *name == '\0')
  361. name = bfd_section_name (sec);
  362. }
  363. switch (r)
  364. {
  365. case bfd_reloc_overflow:
  366. (*info->callbacks->reloc_overflow)
  367. (info, (h ? &h->root : NULL), name, howto->name,
  368. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  369. break;
  370. case bfd_reloc_undefined:
  371. (*info->callbacks->undefined_symbol) (info, name, input_bfd,
  372. input_section,
  373. rel->r_offset, true);
  374. break;
  375. case bfd_reloc_outofrange:
  376. msg = _("internal error: out of range error");
  377. goto common_error;
  378. case bfd_reloc_notsupported:
  379. msg = _("internal error: unsupported relocation error");
  380. goto common_error;
  381. case bfd_reloc_dangerous:
  382. msg = _("internal error: dangerous error");
  383. goto common_error;
  384. default:
  385. msg = _("internal error: unknown error");
  386. /* fall through */
  387. common_error:
  388. (*info->callbacks->warning) (info, msg, name, input_bfd,
  389. input_section, rel->r_offset);
  390. break;
  391. }
  392. }
  393. }
  394. return true;
  395. }
  396. /* Delete some bytes from a section while relaxing. */
  397. static bool
  398. mn10200_elf_relax_delete_bytes (bfd *abfd, asection *sec,
  399. bfd_vma addr, int count)
  400. {
  401. Elf_Internal_Shdr *symtab_hdr;
  402. unsigned int sec_shndx;
  403. bfd_byte *contents;
  404. Elf_Internal_Rela *irel, *irelend;
  405. bfd_vma toaddr;
  406. Elf_Internal_Sym *isym;
  407. Elf_Internal_Sym *isymend;
  408. struct elf_link_hash_entry **sym_hashes;
  409. struct elf_link_hash_entry **end_hashes;
  410. unsigned int symcount;
  411. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  412. contents = elf_section_data (sec)->this_hdr.contents;
  413. toaddr = sec->size;
  414. irel = elf_section_data (sec)->relocs;
  415. irelend = irel + sec->reloc_count;
  416. /* Actually delete the bytes. */
  417. memmove (contents + addr, contents + addr + count,
  418. (size_t) (toaddr - addr - count));
  419. sec->size -= count;
  420. /* Adjust all the relocs. */
  421. for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
  422. {
  423. /* Get the new reloc address. */
  424. if ((irel->r_offset > addr
  425. && irel->r_offset < toaddr))
  426. irel->r_offset -= count;
  427. }
  428. /* Adjust the local symbols defined in this section. */
  429. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  430. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  431. for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
  432. {
  433. if (isym->st_shndx == sec_shndx
  434. && isym->st_value > addr
  435. && isym->st_value < toaddr)
  436. isym->st_value -= count;
  437. }
  438. /* Now adjust the global symbols defined in this section. */
  439. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  440. - symtab_hdr->sh_info);
  441. sym_hashes = elf_sym_hashes (abfd);
  442. end_hashes = sym_hashes + symcount;
  443. for (; sym_hashes < end_hashes; sym_hashes++)
  444. {
  445. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  446. if ((sym_hash->root.type == bfd_link_hash_defined
  447. || sym_hash->root.type == bfd_link_hash_defweak)
  448. && sym_hash->root.u.def.section == sec
  449. && sym_hash->root.u.def.value > addr
  450. && sym_hash->root.u.def.value < toaddr)
  451. {
  452. sym_hash->root.u.def.value -= count;
  453. }
  454. }
  455. return true;
  456. }
  457. /* This function handles relaxing for the mn10200.
  458. There are quite a few relaxing opportunities available on the mn10200:
  459. * jsr:24 -> jsr:16 2 bytes
  460. * jmp:24 -> jmp:16 2 bytes
  461. * jmp:16 -> bra:8 1 byte
  462. * If the previous instruction is a conditional branch
  463. around the jump/bra, we may be able to reverse its condition
  464. and change its target to the jump's target. The jump/bra
  465. can then be deleted. 2 bytes
  466. * mov abs24 -> mov abs16 2 byte savings
  467. * Most instructions which accept imm24 can relax to imm16 2 bytes
  468. - Most instructions which accept imm16 can relax to imm8 1 byte
  469. * Most instructions which accept d24 can relax to d16 2 bytes
  470. - Most instructions which accept d16 can relax to d8 1 byte
  471. abs24, imm24, d24 all look the same at the reloc level. It
  472. might make the code simpler if we had different relocs for
  473. the various relaxable operand types.
  474. We don't handle imm16->imm8 or d16->d8 as they're very rare
  475. and somewhat more difficult to support. */
  476. static bool
  477. mn10200_elf_relax_section (bfd *abfd,
  478. asection *sec,
  479. struct bfd_link_info *link_info,
  480. bool *again)
  481. {
  482. Elf_Internal_Shdr *symtab_hdr;
  483. Elf_Internal_Rela *internal_relocs;
  484. Elf_Internal_Rela *irel, *irelend;
  485. bfd_byte *contents = NULL;
  486. Elf_Internal_Sym *isymbuf = NULL;
  487. /* Assume nothing changes. */
  488. *again = false;
  489. /* We don't have to do anything for a relocatable link, if
  490. this section does not have relocs, or if this is not a
  491. code section. */
  492. if (bfd_link_relocatable (link_info)
  493. || (sec->flags & SEC_RELOC) == 0
  494. || sec->reloc_count == 0
  495. || (sec->flags & SEC_CODE) == 0)
  496. return true;
  497. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  498. /* Get a copy of the native relocations. */
  499. internal_relocs = (_bfd_elf_link_read_relocs
  500. (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
  501. link_info->keep_memory));
  502. if (internal_relocs == NULL)
  503. goto error_return;
  504. /* Walk through them looking for relaxing opportunities. */
  505. irelend = internal_relocs + sec->reloc_count;
  506. for (irel = internal_relocs; irel < irelend; irel++)
  507. {
  508. bfd_vma symval;
  509. /* If this isn't something that can be relaxed, then ignore
  510. this reloc. */
  511. if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_NONE
  512. || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_8
  513. || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_MAX)
  514. continue;
  515. /* Get the section contents if we haven't done so already. */
  516. if (contents == NULL)
  517. {
  518. /* Get cached copy if it exists. */
  519. if (elf_section_data (sec)->this_hdr.contents != NULL)
  520. contents = elf_section_data (sec)->this_hdr.contents;
  521. else
  522. {
  523. /* Go get them off disk. */
  524. if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  525. goto error_return;
  526. }
  527. }
  528. /* Read this BFD's local symbols if we haven't done so already. */
  529. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  530. {
  531. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  532. if (isymbuf == NULL)
  533. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  534. symtab_hdr->sh_info, 0,
  535. NULL, NULL, NULL);
  536. if (isymbuf == NULL)
  537. goto error_return;
  538. }
  539. /* Get the value of the symbol referred to by the reloc. */
  540. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  541. {
  542. /* A local symbol. */
  543. Elf_Internal_Sym *isym;
  544. asection *sym_sec;
  545. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  546. if (isym->st_shndx == SHN_UNDEF)
  547. sym_sec = bfd_und_section_ptr;
  548. else if (isym->st_shndx == SHN_ABS)
  549. sym_sec = bfd_abs_section_ptr;
  550. else if (isym->st_shndx == SHN_COMMON)
  551. sym_sec = bfd_com_section_ptr;
  552. else
  553. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  554. symval = (isym->st_value
  555. + sym_sec->output_section->vma
  556. + sym_sec->output_offset);
  557. }
  558. else
  559. {
  560. unsigned long indx;
  561. struct elf_link_hash_entry *h;
  562. /* An external symbol. */
  563. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  564. h = elf_sym_hashes (abfd)[indx];
  565. BFD_ASSERT (h != NULL);
  566. if (h->root.type != bfd_link_hash_defined
  567. && h->root.type != bfd_link_hash_defweak)
  568. {
  569. /* This appears to be a reference to an undefined
  570. symbol. Just ignore it--it will be caught by the
  571. regular reloc processing. */
  572. continue;
  573. }
  574. symval = (h->root.u.def.value
  575. + h->root.u.def.section->output_section->vma
  576. + h->root.u.def.section->output_offset);
  577. }
  578. /* For simplicity of coding, we are going to modify the section
  579. contents, the section relocs, and the BFD symbol table. We
  580. must tell the rest of the code not to free up this
  581. information. It would be possible to instead create a table
  582. of changes which have to be made, as is done in coff-mips.c;
  583. that would be more work, but would require less memory when
  584. the linker is run. */
  585. /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
  586. branch/call. */
  587. if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL24)
  588. {
  589. bfd_vma value = symval;
  590. /* Deal with pc-relative gunk. */
  591. value -= (sec->output_section->vma + sec->output_offset);
  592. value -= (irel->r_offset + 3);
  593. value += irel->r_addend;
  594. /* See if the value will fit in 16 bits, note the high value is
  595. 0x7fff + 2 as the target will be two bytes closer if we are
  596. able to relax. */
  597. if ((long) value < 0x8001 && (long) value > -0x8000)
  598. {
  599. unsigned char code;
  600. /* Get the opcode. */
  601. code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  602. if (code != 0xe0 && code != 0xe1)
  603. continue;
  604. /* Note that we've changed the relocs, section contents, etc. */
  605. elf_section_data (sec)->relocs = internal_relocs;
  606. elf_section_data (sec)->this_hdr.contents = contents;
  607. symtab_hdr->contents = (unsigned char *) isymbuf;
  608. /* Fix the opcode. */
  609. if (code == 0xe0)
  610. bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 2);
  611. else if (code == 0xe1)
  612. bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 2);
  613. /* Fix the relocation's type. */
  614. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  615. R_MN10200_PCREL16);
  616. /* The opcode got shorter too, so we have to fix the offset. */
  617. irel->r_offset -= 1;
  618. /* Delete two bytes of data. */
  619. if (!mn10200_elf_relax_delete_bytes (abfd, sec,
  620. irel->r_offset + 1, 2))
  621. goto error_return;
  622. /* That will change things, so, we should relax again.
  623. Note that this is not required, and it may be slow. */
  624. *again = true;
  625. }
  626. }
  627. /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
  628. branch. */
  629. if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL16)
  630. {
  631. bfd_vma value = symval;
  632. /* Deal with pc-relative gunk. */
  633. value -= (sec->output_section->vma + sec->output_offset);
  634. value -= (irel->r_offset + 2);
  635. value += irel->r_addend;
  636. /* See if the value will fit in 8 bits, note the high value is
  637. 0x7f + 1 as the target will be one bytes closer if we are
  638. able to relax. */
  639. if ((long) value < 0x80 && (long) value > -0x80)
  640. {
  641. unsigned char code;
  642. /* Get the opcode. */
  643. code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  644. if (code != 0xfc)
  645. continue;
  646. /* Note that we've changed the relocs, section contents, etc. */
  647. elf_section_data (sec)->relocs = internal_relocs;
  648. elf_section_data (sec)->this_hdr.contents = contents;
  649. symtab_hdr->contents = (unsigned char *) isymbuf;
  650. /* Fix the opcode. */
  651. bfd_put_8 (abfd, 0xea, contents + irel->r_offset - 1);
  652. /* Fix the relocation's type. */
  653. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  654. R_MN10200_PCREL8);
  655. /* Delete one byte of data. */
  656. if (!mn10200_elf_relax_delete_bytes (abfd, sec,
  657. irel->r_offset + 1, 1))
  658. goto error_return;
  659. /* That will change things, so, we should relax again.
  660. Note that this is not required, and it may be slow. */
  661. *again = true;
  662. }
  663. }
  664. /* Try to eliminate an unconditional 8 bit pc-relative branch
  665. which immediately follows a conditional 8 bit pc-relative
  666. branch around the unconditional branch.
  667. original: new:
  668. bCC lab1 bCC' lab2
  669. bra lab2
  670. lab1: lab1:
  671. This happens when the bCC can't reach lab2 at assembly time,
  672. but due to other relaxations it can reach at link time. */
  673. if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL8)
  674. {
  675. Elf_Internal_Rela *nrel;
  676. unsigned char code;
  677. /* Do nothing if this reloc is the last byte in the section. */
  678. if (irel->r_offset == sec->size)
  679. continue;
  680. /* See if the next instruction is an unconditional pc-relative
  681. branch, more often than not this test will fail, so we
  682. test it first to speed things up. */
  683. code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
  684. if (code != 0xea)
  685. continue;
  686. /* Also make sure the next relocation applies to the next
  687. instruction and that it's a pc-relative 8 bit branch. */
  688. nrel = irel + 1;
  689. if (nrel == irelend
  690. || irel->r_offset + 2 != nrel->r_offset
  691. || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10200_PCREL8)
  692. continue;
  693. /* Make sure our destination immediately follows the
  694. unconditional branch. */
  695. if (symval != (sec->output_section->vma + sec->output_offset
  696. + irel->r_offset + 3))
  697. continue;
  698. /* Now make sure we are a conditional branch. This may not
  699. be necessary, but why take the chance.
  700. Note these checks assume that R_MN10200_PCREL8 relocs
  701. only occur on bCC and bCCx insns. If they occured
  702. elsewhere, we'd need to know the start of this insn
  703. for this check to be accurate. */
  704. code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  705. if (code != 0xe0 && code != 0xe1 && code != 0xe2
  706. && code != 0xe3 && code != 0xe4 && code != 0xe5
  707. && code != 0xe6 && code != 0xe7 && code != 0xe8
  708. && code != 0xe9 && code != 0xec && code != 0xed
  709. && code != 0xee && code != 0xef && code != 0xfc
  710. && code != 0xfd && code != 0xfe && code != 0xff)
  711. continue;
  712. /* We also have to be sure there is no symbol/label
  713. at the unconditional branch. */
  714. if (mn10200_elf_symbol_address_p (abfd, sec, isymbuf,
  715. irel->r_offset + 1))
  716. continue;
  717. /* Note that we've changed the relocs, section contents, etc. */
  718. elf_section_data (sec)->relocs = internal_relocs;
  719. elf_section_data (sec)->this_hdr.contents = contents;
  720. symtab_hdr->contents = (unsigned char *) isymbuf;
  721. /* Reverse the condition of the first branch. */
  722. switch (code)
  723. {
  724. case 0xfc:
  725. code = 0xfd;
  726. break;
  727. case 0xfd:
  728. code = 0xfc;
  729. break;
  730. case 0xfe:
  731. code = 0xff;
  732. break;
  733. case 0xff:
  734. code = 0xfe;
  735. break;
  736. case 0xe8:
  737. code = 0xe9;
  738. break;
  739. case 0xe9:
  740. code = 0xe8;
  741. break;
  742. case 0xe0:
  743. code = 0xe2;
  744. break;
  745. case 0xe2:
  746. code = 0xe0;
  747. break;
  748. case 0xe3:
  749. code = 0xe1;
  750. break;
  751. case 0xe1:
  752. code = 0xe3;
  753. break;
  754. case 0xe4:
  755. code = 0xe6;
  756. break;
  757. case 0xe6:
  758. code = 0xe4;
  759. break;
  760. case 0xe7:
  761. code = 0xe5;
  762. break;
  763. case 0xe5:
  764. code = 0xe7;
  765. break;
  766. case 0xec:
  767. code = 0xed;
  768. break;
  769. case 0xed:
  770. code = 0xec;
  771. break;
  772. case 0xee:
  773. code = 0xef;
  774. break;
  775. case 0xef:
  776. code = 0xee;
  777. break;
  778. }
  779. bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
  780. /* Set the reloc type and symbol for the first branch
  781. from the second branch. */
  782. irel->r_info = nrel->r_info;
  783. /* Make the reloc for the second branch a null reloc. */
  784. nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
  785. R_MN10200_NONE);
  786. /* Delete two bytes of data. */
  787. if (!mn10200_elf_relax_delete_bytes (abfd, sec,
  788. irel->r_offset + 1, 2))
  789. goto error_return;
  790. /* That will change things, so, we should relax again.
  791. Note that this is not required, and it may be slow. */
  792. *again = true;
  793. }
  794. /* Try to turn a 24bit immediate, displacement or absolute address
  795. into a 16bit immediate, displacement or absolute address. */
  796. if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_24)
  797. {
  798. bfd_vma value = symval;
  799. /* See if the value will fit in 16 bits.
  800. We allow any 16bit match here. We prune those we can't
  801. handle below. */
  802. if ((long) value < 0x7fff && (long) value > -0x8000)
  803. {
  804. unsigned char code;
  805. /* All insns which have 24bit operands are 5 bytes long,
  806. the first byte will always be 0xf4, but we double check
  807. it just in case. */
  808. /* Get the first opcode. */
  809. code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
  810. if (code != 0xf4)
  811. continue;
  812. /* Get the second opcode. */
  813. code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
  814. switch (code & 0xfc)
  815. {
  816. /* mov imm24,dn -> mov imm16,dn */
  817. case 0x70:
  818. /* Not safe if the high bit is on as relaxing may
  819. move the value out of high mem and thus not fit
  820. in a signed 16bit value. */
  821. if (value & 0x8000)
  822. continue;
  823. /* Note that we've changed the relocation contents, etc. */
  824. elf_section_data (sec)->relocs = internal_relocs;
  825. elf_section_data (sec)->this_hdr.contents = contents;
  826. symtab_hdr->contents = (unsigned char *) isymbuf;
  827. /* Fix the opcode. */
  828. bfd_put_8 (abfd, 0xf8 + (code & 0x03),
  829. contents + irel->r_offset - 2);
  830. /* Fix the relocation's type. */
  831. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  832. R_MN10200_16);
  833. /* The opcode got shorter too, so we have to fix the
  834. offset. */
  835. irel->r_offset -= 1;
  836. /* Delete two bytes of data. */
  837. if (!mn10200_elf_relax_delete_bytes (abfd, sec,
  838. irel->r_offset + 1, 2))
  839. goto error_return;
  840. /* That will change things, so, we should relax again.
  841. Note that this is not required, and it may be slow. */
  842. *again = true;
  843. break;
  844. /* mov imm24,an -> mov imm16,an
  845. cmp imm24,an -> cmp imm16,an
  846. mov (abs24),dn -> mov (abs16),dn
  847. mov dn,(abs24) -> mov dn,(abs16)
  848. movb dn,(abs24) -> movb dn,(abs16)
  849. movbu (abs24),dn -> movbu (abs16),dn */
  850. case 0x74:
  851. case 0x7c:
  852. case 0xc0:
  853. case 0x40:
  854. case 0x44:
  855. case 0xc8:
  856. /* Note that we've changed the relocation contents, etc. */
  857. elf_section_data (sec)->relocs = internal_relocs;
  858. elf_section_data (sec)->this_hdr.contents = contents;
  859. symtab_hdr->contents = (unsigned char *) isymbuf;
  860. if ((code & 0xfc) == 0x74)
  861. code = 0xdc + (code & 0x03);
  862. else if ((code & 0xfc) == 0x7c)
  863. code = 0xec + (code & 0x03);
  864. else if ((code & 0xfc) == 0xc0)
  865. code = 0xc8 + (code & 0x03);
  866. else if ((code & 0xfc) == 0x40)
  867. code = 0xc0 + (code & 0x03);
  868. else if ((code & 0xfc) == 0x44)
  869. code = 0xc4 + (code & 0x03);
  870. else if ((code & 0xfc) == 0xc8)
  871. code = 0xcc + (code & 0x03);
  872. /* Fix the opcode. */
  873. bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
  874. /* Fix the relocation's type. */
  875. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  876. R_MN10200_16);
  877. /* The opcode got shorter too, so we have to fix the
  878. offset. */
  879. irel->r_offset -= 1;
  880. /* Delete two bytes of data. */
  881. if (!mn10200_elf_relax_delete_bytes (abfd, sec,
  882. irel->r_offset + 1, 2))
  883. goto error_return;
  884. /* That will change things, so, we should relax again.
  885. Note that this is not required, and it may be slow. */
  886. *again = true;
  887. break;
  888. /* cmp imm24,dn -> cmp imm16,dn
  889. mov (abs24),an -> mov (abs16),an
  890. mov an,(abs24) -> mov an,(abs16)
  891. add imm24,dn -> add imm16,dn
  892. add imm24,an -> add imm16,an
  893. sub imm24,dn -> sub imm16,dn
  894. sub imm24,an -> sub imm16,an
  895. And all d24->d16 in memory ops. */
  896. case 0x78:
  897. case 0xd0:
  898. case 0x50:
  899. case 0x60:
  900. case 0x64:
  901. case 0x68:
  902. case 0x6c:
  903. case 0x80:
  904. case 0xf0:
  905. case 0x00:
  906. case 0x10:
  907. case 0xb0:
  908. case 0x30:
  909. case 0xa0:
  910. case 0x20:
  911. case 0x90:
  912. /* Not safe if the high bit is on as relaxing may
  913. move the value out of high mem and thus not fit
  914. in a signed 16bit value. */
  915. if (((code & 0xfc) == 0x78
  916. || (code & 0xfc) == 0x60
  917. || (code & 0xfc) == 0x64
  918. || (code & 0xfc) == 0x68
  919. || (code & 0xfc) == 0x6c
  920. || (code & 0xfc) == 0x80
  921. || (code & 0xfc) == 0xf0
  922. || (code & 0xfc) == 0x00
  923. || (code & 0xfc) == 0x10
  924. || (code & 0xfc) == 0xb0
  925. || (code & 0xfc) == 0x30
  926. || (code & 0xfc) == 0xa0
  927. || (code & 0xfc) == 0x20
  928. || (code & 0xfc) == 0x90)
  929. && (value & 0x8000) != 0)
  930. continue;
  931. /* Note that we've changed the relocation contents, etc. */
  932. elf_section_data (sec)->relocs = internal_relocs;
  933. elf_section_data (sec)->this_hdr.contents = contents;
  934. symtab_hdr->contents = (unsigned char *) isymbuf;
  935. /* Fix the opcode. */
  936. bfd_put_8 (abfd, 0xf7, contents + irel->r_offset - 2);
  937. if ((code & 0xfc) == 0x78)
  938. code = 0x48 + (code & 0x03);
  939. else if ((code & 0xfc) == 0xd0)
  940. code = 0x30 + (code & 0x03);
  941. else if ((code & 0xfc) == 0x50)
  942. code = 0x20 + (code & 0x03);
  943. else if ((code & 0xfc) == 0x60)
  944. code = 0x18 + (code & 0x03);
  945. else if ((code & 0xfc) == 0x64)
  946. code = 0x08 + (code & 0x03);
  947. else if ((code & 0xfc) == 0x68)
  948. code = 0x1c + (code & 0x03);
  949. else if ((code & 0xfc) == 0x6c)
  950. code = 0x0c + (code & 0x03);
  951. else if ((code & 0xfc) == 0x80)
  952. code = 0xc0 + (code & 0x07);
  953. else if ((code & 0xfc) == 0xf0)
  954. code = 0xb0 + (code & 0x07);
  955. else if ((code & 0xfc) == 0x00)
  956. code = 0x80 + (code & 0x07);
  957. else if ((code & 0xfc) == 0x10)
  958. code = 0xa0 + (code & 0x07);
  959. else if ((code & 0xfc) == 0xb0)
  960. code = 0x70 + (code & 0x07);
  961. else if ((code & 0xfc) == 0x30)
  962. code = 0x60 + (code & 0x07);
  963. else if ((code & 0xfc) == 0xa0)
  964. code = 0xd0 + (code & 0x07);
  965. else if ((code & 0xfc) == 0x20)
  966. code = 0x90 + (code & 0x07);
  967. else if ((code & 0xfc) == 0x90)
  968. code = 0x50 + (code & 0x07);
  969. bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
  970. /* Fix the relocation's type. */
  971. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  972. R_MN10200_16);
  973. /* Delete one bytes of data. */
  974. if (!mn10200_elf_relax_delete_bytes (abfd, sec,
  975. irel->r_offset + 2, 1))
  976. goto error_return;
  977. /* That will change things, so, we should relax again.
  978. Note that this is not required, and it may be slow. */
  979. *again = true;
  980. break;
  981. /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
  982. case 0xc4:
  983. /* Note that we've changed the reldection contents, etc. */
  984. elf_section_data (sec)->relocs = internal_relocs;
  985. elf_section_data (sec)->this_hdr.contents = contents;
  986. symtab_hdr->contents = (unsigned char *) isymbuf;
  987. bfd_put_8 (abfd, 0xcc + (code & 0x03),
  988. contents + irel->r_offset - 2);
  989. bfd_put_8 (abfd, 0xb8 + (code & 0x03),
  990. contents + irel->r_offset - 1);
  991. /* Fix the relocation's type. */
  992. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  993. R_MN10200_16);
  994. /* The reloc will be applied one byte in front of its
  995. current location. */
  996. irel->r_offset -= 1;
  997. /* Delete one bytes of data. */
  998. if (!mn10200_elf_relax_delete_bytes (abfd, sec,
  999. irel->r_offset + 2, 1))
  1000. goto error_return;
  1001. /* That will change things, so, we should relax again.
  1002. Note that this is not required, and it may be slow. */
  1003. *again = true;
  1004. break;
  1005. }
  1006. }
  1007. }
  1008. }
  1009. if (isymbuf != NULL
  1010. && symtab_hdr->contents != (unsigned char *) isymbuf)
  1011. {
  1012. if (! link_info->keep_memory)
  1013. free (isymbuf);
  1014. else
  1015. {
  1016. /* Cache the symbols for elf_link_input_bfd. */
  1017. symtab_hdr->contents = (unsigned char *) isymbuf;
  1018. }
  1019. }
  1020. if (contents != NULL
  1021. && elf_section_data (sec)->this_hdr.contents != contents)
  1022. {
  1023. if (! link_info->keep_memory)
  1024. free (contents);
  1025. else
  1026. {
  1027. /* Cache the section contents for elf_link_input_bfd. */
  1028. elf_section_data (sec)->this_hdr.contents = contents;
  1029. }
  1030. }
  1031. if (elf_section_data (sec)->relocs != internal_relocs)
  1032. free (internal_relocs);
  1033. return true;
  1034. error_return:
  1035. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  1036. free (isymbuf);
  1037. if (elf_section_data (sec)->this_hdr.contents != contents)
  1038. free (contents);
  1039. if (elf_section_data (sec)->relocs != internal_relocs)
  1040. free (internal_relocs);
  1041. return false;
  1042. }
  1043. /* Return TRUE if a symbol exists at the given address, else return
  1044. FALSE. */
  1045. static bool
  1046. mn10200_elf_symbol_address_p (bfd *abfd,
  1047. asection *sec,
  1048. Elf_Internal_Sym *isym,
  1049. bfd_vma addr)
  1050. {
  1051. Elf_Internal_Shdr *symtab_hdr;
  1052. unsigned int sec_shndx;
  1053. Elf_Internal_Sym *isymend;
  1054. struct elf_link_hash_entry **sym_hashes;
  1055. struct elf_link_hash_entry **end_hashes;
  1056. unsigned int symcount;
  1057. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  1058. /* Examine all the local symbols. */
  1059. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1060. for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
  1061. {
  1062. if (isym->st_shndx == sec_shndx
  1063. && isym->st_value == addr)
  1064. return true;
  1065. }
  1066. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  1067. - symtab_hdr->sh_info);
  1068. sym_hashes = elf_sym_hashes (abfd);
  1069. end_hashes = sym_hashes + symcount;
  1070. for (; sym_hashes < end_hashes; sym_hashes++)
  1071. {
  1072. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  1073. if ((sym_hash->root.type == bfd_link_hash_defined
  1074. || sym_hash->root.type == bfd_link_hash_defweak)
  1075. && sym_hash->root.u.def.section == sec
  1076. && sym_hash->root.u.def.value == addr)
  1077. return true;
  1078. }
  1079. return false;
  1080. }
  1081. /* This is a version of bfd_generic_get_relocated_section_contents
  1082. which uses mn10200_elf_relocate_section. */
  1083. static bfd_byte *
  1084. mn10200_elf_get_relocated_section_contents (bfd *output_bfd,
  1085. struct bfd_link_info *link_info,
  1086. struct bfd_link_order *link_order,
  1087. bfd_byte *data,
  1088. bool relocatable,
  1089. asymbol **symbols)
  1090. {
  1091. Elf_Internal_Shdr *symtab_hdr;
  1092. asection *input_section = link_order->u.indirect.section;
  1093. bfd *input_bfd = input_section->owner;
  1094. asection **sections = NULL;
  1095. Elf_Internal_Rela *internal_relocs = NULL;
  1096. Elf_Internal_Sym *isymbuf = NULL;
  1097. /* We only need to handle the case of relaxing, or of having a
  1098. particular set of section contents, specially. */
  1099. if (relocatable
  1100. || elf_section_data (input_section)->this_hdr.contents == NULL)
  1101. return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
  1102. link_order, data,
  1103. relocatable,
  1104. symbols);
  1105. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  1106. memcpy (data, elf_section_data (input_section)->this_hdr.contents,
  1107. (size_t) input_section->size);
  1108. if ((input_section->flags & SEC_RELOC) != 0
  1109. && input_section->reloc_count > 0)
  1110. {
  1111. Elf_Internal_Sym *isym;
  1112. Elf_Internal_Sym *isymend;
  1113. asection **secpp;
  1114. bfd_size_type amt;
  1115. internal_relocs = (_bfd_elf_link_read_relocs
  1116. (input_bfd, input_section, NULL,
  1117. (Elf_Internal_Rela *) NULL, false));
  1118. if (internal_relocs == NULL)
  1119. goto error_return;
  1120. if (symtab_hdr->sh_info != 0)
  1121. {
  1122. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  1123. if (isymbuf == NULL)
  1124. isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
  1125. symtab_hdr->sh_info, 0,
  1126. NULL, NULL, NULL);
  1127. if (isymbuf == NULL)
  1128. goto error_return;
  1129. }
  1130. amt = symtab_hdr->sh_info;
  1131. amt *= sizeof (asection *);
  1132. sections = (asection **) bfd_malloc (amt);
  1133. if (sections == NULL && amt != 0)
  1134. goto error_return;
  1135. isymend = isymbuf + symtab_hdr->sh_info;
  1136. for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
  1137. {
  1138. asection *isec;
  1139. if (isym->st_shndx == SHN_UNDEF)
  1140. isec = bfd_und_section_ptr;
  1141. else if (isym->st_shndx == SHN_ABS)
  1142. isec = bfd_abs_section_ptr;
  1143. else if (isym->st_shndx == SHN_COMMON)
  1144. isec = bfd_com_section_ptr;
  1145. else
  1146. isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
  1147. *secpp = isec;
  1148. }
  1149. if (! mn10200_elf_relocate_section (output_bfd, link_info, input_bfd,
  1150. input_section, data, internal_relocs,
  1151. isymbuf, sections))
  1152. goto error_return;
  1153. free (sections);
  1154. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  1155. free (isymbuf);
  1156. if (elf_section_data (input_section)->relocs != internal_relocs)
  1157. free (internal_relocs);
  1158. }
  1159. return data;
  1160. error_return:
  1161. free (sections);
  1162. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  1163. free (isymbuf);
  1164. if (elf_section_data (input_section)->relocs != internal_relocs)
  1165. free (internal_relocs);
  1166. return NULL;
  1167. }
  1168. #define TARGET_LITTLE_SYM mn10200_elf32_vec
  1169. #define TARGET_LITTLE_NAME "elf32-mn10200"
  1170. #define ELF_ARCH bfd_arch_mn10200
  1171. #define ELF_MACHINE_CODE EM_MN10200
  1172. #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10200
  1173. #define ELF_MAXPAGESIZE 0x1000
  1174. #define elf_backend_rela_normal 1
  1175. #define elf_info_to_howto mn10200_info_to_howto
  1176. #define elf_info_to_howto_rel NULL
  1177. #define elf_backend_relocate_section mn10200_elf_relocate_section
  1178. #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
  1179. #define bfd_elf32_bfd_get_relocated_section_contents \
  1180. mn10200_elf_get_relocated_section_contents
  1181. #define elf_symbol_leading_char '_'
  1182. #include "elf32-target.h"