elf32-m68hc1x.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. /* Motorola 68HC11/HC12-specific support for 32-bit ELF
  2. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  3. Contributed by Stephane Carrez (stcarrez@nerim.fr)
  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 "bfdlink.h"
  20. #include "libbfd.h"
  21. #include "elf-bfd.h"
  22. #include "elf32-m68hc1x.h"
  23. #include "elf/m68hc11.h"
  24. #include "opcode/m68hc11.h"
  25. #include "libiberty.h"
  26. #define m68hc12_stub_hash_lookup(table, string, create, copy) \
  27. ((struct elf32_m68hc11_stub_hash_entry *) \
  28. bfd_hash_lookup ((table), (string), (create), (copy)))
  29. static struct elf32_m68hc11_stub_hash_entry* m68hc12_add_stub
  30. (const char *stub_name,
  31. asection *section,
  32. struct m68hc11_elf_link_hash_table *htab);
  33. static struct bfd_hash_entry *stub_hash_newfunc
  34. (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
  35. static void m68hc11_elf_set_symbol (bfd* abfd, struct bfd_link_info *info,
  36. const char* name, bfd_vma value,
  37. asection* sec);
  38. static bool m68hc11_elf_export_one_stub
  39. (struct bfd_hash_entry *gen_entry, void *in_arg);
  40. static void scan_sections_for_abi (bfd*, asection*, void *);
  41. struct m68hc11_scan_param
  42. {
  43. struct m68hc11_page_info* pinfo;
  44. bool use_memory_banks;
  45. };
  46. /* Destroy a 68HC11/68HC12 ELF linker hash table. */
  47. static void
  48. m68hc11_elf_bfd_link_hash_table_free (bfd *obfd)
  49. {
  50. struct m68hc11_elf_link_hash_table *ret
  51. = (struct m68hc11_elf_link_hash_table *) obfd->link.hash;
  52. bfd_hash_table_free (ret->stub_hash_table);
  53. free (ret->stub_hash_table);
  54. _bfd_elf_link_hash_table_free (obfd);
  55. }
  56. /* Create a 68HC11/68HC12 ELF linker hash table. */
  57. struct m68hc11_elf_link_hash_table*
  58. m68hc11_elf_hash_table_create (bfd *abfd)
  59. {
  60. struct m68hc11_elf_link_hash_table *ret;
  61. size_t amt = sizeof (struct m68hc11_elf_link_hash_table);
  62. ret = (struct m68hc11_elf_link_hash_table *) bfd_zmalloc (amt);
  63. if (ret == (struct m68hc11_elf_link_hash_table *) NULL)
  64. return NULL;
  65. if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
  66. _bfd_elf_link_hash_newfunc,
  67. sizeof (struct elf_link_hash_entry),
  68. M68HC11_ELF_DATA))
  69. {
  70. free (ret);
  71. return NULL;
  72. }
  73. /* Init the stub hash table too. */
  74. amt = sizeof (struct bfd_hash_table);
  75. ret->stub_hash_table = (struct bfd_hash_table*) bfd_malloc (amt);
  76. if (ret->stub_hash_table == NULL)
  77. {
  78. _bfd_elf_link_hash_table_free (abfd);
  79. return NULL;
  80. }
  81. if (!bfd_hash_table_init (ret->stub_hash_table, stub_hash_newfunc,
  82. sizeof (struct elf32_m68hc11_stub_hash_entry)))
  83. {
  84. free (ret->stub_hash_table);
  85. _bfd_elf_link_hash_table_free (abfd);
  86. return NULL;
  87. }
  88. ret->root.root.hash_table_free = m68hc11_elf_bfd_link_hash_table_free;
  89. return ret;
  90. }
  91. /* Assorted hash table functions. */
  92. /* Initialize an entry in the stub hash table. */
  93. static struct bfd_hash_entry *
  94. stub_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
  95. const char *string)
  96. {
  97. /* Allocate the structure if it has not already been allocated by a
  98. subclass. */
  99. if (entry == NULL)
  100. {
  101. entry = bfd_hash_allocate (table,
  102. sizeof (struct elf32_m68hc11_stub_hash_entry));
  103. if (entry == NULL)
  104. return entry;
  105. }
  106. /* Call the allocation method of the superclass. */
  107. entry = bfd_hash_newfunc (entry, table, string);
  108. if (entry != NULL)
  109. {
  110. struct elf32_m68hc11_stub_hash_entry *eh;
  111. /* Initialize the local fields. */
  112. eh = (struct elf32_m68hc11_stub_hash_entry *) entry;
  113. eh->stub_sec = NULL;
  114. eh->stub_offset = 0;
  115. eh->target_value = 0;
  116. eh->target_section = NULL;
  117. }
  118. return entry;
  119. }
  120. /* Add a new stub entry to the stub hash. Not all fields of the new
  121. stub entry are initialised. */
  122. static struct elf32_m68hc11_stub_hash_entry *
  123. m68hc12_add_stub (const char *stub_name, asection *section,
  124. struct m68hc11_elf_link_hash_table *htab)
  125. {
  126. struct elf32_m68hc11_stub_hash_entry *stub_entry;
  127. /* Enter this entry into the linker stub hash table. */
  128. stub_entry = m68hc12_stub_hash_lookup (htab->stub_hash_table, stub_name,
  129. true, false);
  130. if (stub_entry == NULL)
  131. {
  132. /* xgettext:c-format */
  133. _bfd_error_handler (_("%pB: cannot create stub entry %s"),
  134. section->owner, stub_name);
  135. return NULL;
  136. }
  137. if (htab->stub_section == 0)
  138. {
  139. htab->stub_section = (*htab->add_stub_section) (".tramp",
  140. htab->tramp_section);
  141. }
  142. stub_entry->stub_sec = htab->stub_section;
  143. stub_entry->stub_offset = 0;
  144. return stub_entry;
  145. }
  146. /* Hook called by the linker routine which adds symbols from an object
  147. file. We use it for identify far symbols and force a loading of
  148. the trampoline handler. */
  149. bool
  150. elf32_m68hc11_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
  151. Elf_Internal_Sym *sym,
  152. const char **namep ATTRIBUTE_UNUSED,
  153. flagword *flagsp ATTRIBUTE_UNUSED,
  154. asection **secp ATTRIBUTE_UNUSED,
  155. bfd_vma *valp ATTRIBUTE_UNUSED)
  156. {
  157. if (sym->st_other & STO_M68HC12_FAR)
  158. {
  159. struct elf_link_hash_entry *h;
  160. h = (struct elf_link_hash_entry *)
  161. bfd_link_hash_lookup (info->hash, "__far_trampoline",
  162. false, false, false);
  163. if (h == NULL)
  164. {
  165. struct bfd_link_hash_entry* entry = NULL;
  166. _bfd_generic_link_add_one_symbol (info, abfd,
  167. "__far_trampoline",
  168. BSF_GLOBAL,
  169. bfd_und_section_ptr,
  170. (bfd_vma) 0, (const char*) NULL,
  171. false, false, &entry);
  172. }
  173. }
  174. return true;
  175. }
  176. /* Merge non-visibility st_other attributes, STO_M68HC12_FAR and
  177. STO_M68HC12_INTERRUPT. */
  178. void
  179. elf32_m68hc11_merge_symbol_attribute (struct elf_link_hash_entry *h,
  180. unsigned int st_other,
  181. bool definition,
  182. bool dynamic ATTRIBUTE_UNUSED)
  183. {
  184. if (definition)
  185. h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
  186. | ELF_ST_VISIBILITY (h->other));
  187. }
  188. /* External entry points for sizing and building linker stubs. */
  189. /* Set up various things so that we can make a list of input sections
  190. for each output section included in the link. Returns -1 on error,
  191. 0 when no stubs will be needed, and 1 on success. */
  192. int
  193. elf32_m68hc11_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
  194. {
  195. bfd *input_bfd;
  196. unsigned int bfd_count;
  197. unsigned int top_id, top_index;
  198. asection *section;
  199. asection **input_list, **list;
  200. size_t amt;
  201. asection *text_section;
  202. struct m68hc11_elf_link_hash_table *htab;
  203. htab = m68hc11_elf_hash_table (info);
  204. if (htab == NULL)
  205. return -1;
  206. if (bfd_get_flavour (info->output_bfd) != bfd_target_elf_flavour)
  207. return 0;
  208. /* Count the number of input BFDs and find the top input section id.
  209. Also search for an existing ".tramp" section so that we know
  210. where generated trampolines must go. Default to ".text" if we
  211. can't find it. */
  212. htab->tramp_section = 0;
  213. text_section = 0;
  214. for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
  215. input_bfd != NULL;
  216. input_bfd = input_bfd->link.next)
  217. {
  218. bfd_count += 1;
  219. for (section = input_bfd->sections;
  220. section != NULL;
  221. section = section->next)
  222. {
  223. const char *name = bfd_section_name (section);
  224. if (!strcmp (name, ".tramp"))
  225. htab->tramp_section = section;
  226. if (!strcmp (name, ".text"))
  227. text_section = section;
  228. if (top_id < section->id)
  229. top_id = section->id;
  230. }
  231. }
  232. htab->bfd_count = bfd_count;
  233. if (htab->tramp_section == 0)
  234. htab->tramp_section = text_section;
  235. /* We can't use output_bfd->section_count here to find the top output
  236. section index as some sections may have been removed, and
  237. strip_excluded_output_sections doesn't renumber the indices. */
  238. for (section = output_bfd->sections, top_index = 0;
  239. section != NULL;
  240. section = section->next)
  241. {
  242. if (top_index < section->index)
  243. top_index = section->index;
  244. }
  245. htab->top_index = top_index;
  246. amt = sizeof (asection *) * (top_index + 1);
  247. input_list = (asection **) bfd_malloc (amt);
  248. htab->input_list = input_list;
  249. if (input_list == NULL)
  250. return -1;
  251. /* For sections we aren't interested in, mark their entries with a
  252. value we can check later. */
  253. list = input_list + top_index;
  254. do
  255. *list = bfd_abs_section_ptr;
  256. while (list-- != input_list);
  257. for (section = output_bfd->sections;
  258. section != NULL;
  259. section = section->next)
  260. {
  261. if ((section->flags & SEC_CODE) != 0)
  262. input_list[section->index] = NULL;
  263. }
  264. return 1;
  265. }
  266. /* Determine and set the size of the stub section for a final link.
  267. The basic idea here is to examine all the relocations looking for
  268. PC-relative calls to a target that is unreachable with a "bl"
  269. instruction. */
  270. bool
  271. elf32_m68hc11_size_stubs (bfd *output_bfd, bfd *stub_bfd,
  272. struct bfd_link_info *info,
  273. asection * (*add_stub_section) (const char*, asection*))
  274. {
  275. bfd *input_bfd;
  276. asection *section;
  277. Elf_Internal_Sym *local_syms, **all_local_syms;
  278. unsigned int bfd_indx, bfd_count;
  279. size_t amt;
  280. asection *stub_sec;
  281. struct m68hc11_elf_link_hash_table *htab = m68hc11_elf_hash_table (info);
  282. if (htab == NULL)
  283. return false;
  284. /* Stash our params away. */
  285. htab->stub_bfd = stub_bfd;
  286. htab->add_stub_section = add_stub_section;
  287. /* Count the number of input BFDs and find the top input section id. */
  288. for (input_bfd = info->input_bfds, bfd_count = 0;
  289. input_bfd != NULL;
  290. input_bfd = input_bfd->link.next)
  291. bfd_count += 1;
  292. /* We want to read in symbol extension records only once. To do this
  293. we need to read in the local symbols in parallel and save them for
  294. later use; so hold pointers to the local symbols in an array. */
  295. amt = sizeof (Elf_Internal_Sym *) * bfd_count;
  296. all_local_syms = (Elf_Internal_Sym **) bfd_zmalloc (amt);
  297. if (all_local_syms == NULL)
  298. return false;
  299. /* Walk over all the input BFDs, swapping in local symbols. */
  300. for (input_bfd = info->input_bfds, bfd_indx = 0;
  301. input_bfd != NULL;
  302. input_bfd = input_bfd->link.next, bfd_indx++)
  303. {
  304. Elf_Internal_Shdr *symtab_hdr;
  305. /* We'll need the symbol table in a second. */
  306. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  307. if (symtab_hdr->sh_info == 0)
  308. continue;
  309. /* We need an array of the local symbols attached to the input bfd. */
  310. local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
  311. if (local_syms == NULL)
  312. {
  313. local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
  314. symtab_hdr->sh_info, 0,
  315. NULL, NULL, NULL);
  316. /* Cache them for elf_link_input_bfd. */
  317. symtab_hdr->contents = (unsigned char *) local_syms;
  318. }
  319. if (local_syms == NULL)
  320. {
  321. free (all_local_syms);
  322. return false;
  323. }
  324. all_local_syms[bfd_indx] = local_syms;
  325. }
  326. for (input_bfd = info->input_bfds, bfd_indx = 0;
  327. input_bfd != NULL;
  328. input_bfd = input_bfd->link.next, bfd_indx++)
  329. {
  330. Elf_Internal_Shdr *symtab_hdr;
  331. struct elf_link_hash_entry ** sym_hashes;
  332. sym_hashes = elf_sym_hashes (input_bfd);
  333. /* We'll need the symbol table in a second. */
  334. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  335. if (symtab_hdr->sh_info == 0)
  336. continue;
  337. local_syms = all_local_syms[bfd_indx];
  338. /* Walk over each section attached to the input bfd. */
  339. for (section = input_bfd->sections;
  340. section != NULL;
  341. section = section->next)
  342. {
  343. Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
  344. /* If there aren't any relocs, then there's nothing more
  345. to do. */
  346. if ((section->flags & SEC_RELOC) == 0
  347. || section->reloc_count == 0)
  348. continue;
  349. /* If this section is a link-once section that will be
  350. discarded, then don't create any stubs. */
  351. if (section->output_section == NULL
  352. || section->output_section->owner != output_bfd)
  353. continue;
  354. /* Get the relocs. */
  355. internal_relocs
  356. = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
  357. (Elf_Internal_Rela *) NULL,
  358. info->keep_memory);
  359. if (internal_relocs == NULL)
  360. goto error_ret_free_local;
  361. /* Now examine each relocation. */
  362. irela = internal_relocs;
  363. irelaend = irela + section->reloc_count;
  364. for (; irela < irelaend; irela++)
  365. {
  366. unsigned int r_type, r_indx;
  367. struct elf32_m68hc11_stub_hash_entry *stub_entry;
  368. asection *sym_sec;
  369. bfd_vma sym_value;
  370. struct elf_link_hash_entry *hash;
  371. const char *stub_name;
  372. Elf_Internal_Sym *sym;
  373. r_type = ELF32_R_TYPE (irela->r_info);
  374. /* Only look at 16-bit relocs. */
  375. if (r_type != (unsigned int) R_M68HC11_16)
  376. continue;
  377. /* Now determine the call target, its name, value,
  378. section. */
  379. r_indx = ELF32_R_SYM (irela->r_info);
  380. if (r_indx < symtab_hdr->sh_info)
  381. {
  382. /* It's a local symbol. */
  383. Elf_Internal_Shdr *hdr;
  384. bool is_far;
  385. sym = local_syms + r_indx;
  386. is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
  387. if (!is_far)
  388. continue;
  389. if (sym->st_shndx >= elf_numsections (input_bfd))
  390. sym_sec = NULL;
  391. else
  392. {
  393. hdr = elf_elfsections (input_bfd)[sym->st_shndx];
  394. sym_sec = hdr->bfd_section;
  395. }
  396. stub_name = (bfd_elf_string_from_elf_section
  397. (input_bfd, symtab_hdr->sh_link,
  398. sym->st_name));
  399. sym_value = sym->st_value;
  400. hash = NULL;
  401. }
  402. else
  403. {
  404. /* It's an external symbol. */
  405. int e_indx;
  406. e_indx = r_indx - symtab_hdr->sh_info;
  407. hash = (struct elf_link_hash_entry *)
  408. (sym_hashes[e_indx]);
  409. while (hash->root.type == bfd_link_hash_indirect
  410. || hash->root.type == bfd_link_hash_warning)
  411. hash = ((struct elf_link_hash_entry *)
  412. hash->root.u.i.link);
  413. if (hash->root.type == bfd_link_hash_defined
  414. || hash->root.type == bfd_link_hash_defweak
  415. || hash->root.type == bfd_link_hash_new)
  416. {
  417. if (!(hash->other & STO_M68HC12_FAR))
  418. continue;
  419. }
  420. else if (hash->root.type == bfd_link_hash_undefweak)
  421. {
  422. continue;
  423. }
  424. else if (hash->root.type == bfd_link_hash_undefined)
  425. {
  426. continue;
  427. }
  428. else
  429. {
  430. bfd_set_error (bfd_error_bad_value);
  431. goto error_ret_free_internal;
  432. }
  433. sym_sec = hash->root.u.def.section;
  434. sym_value = hash->root.u.def.value;
  435. stub_name = hash->root.root.string;
  436. }
  437. if (!stub_name)
  438. goto error_ret_free_internal;
  439. stub_entry = m68hc12_stub_hash_lookup
  440. (htab->stub_hash_table,
  441. stub_name,
  442. false, false);
  443. if (stub_entry == NULL)
  444. {
  445. if (add_stub_section == 0)
  446. continue;
  447. stub_entry = m68hc12_add_stub (stub_name, section, htab);
  448. if (stub_entry == NULL)
  449. {
  450. error_ret_free_internal:
  451. if (elf_section_data (section)->relocs == NULL)
  452. free (internal_relocs);
  453. goto error_ret_free_local;
  454. }
  455. }
  456. stub_entry->target_value = sym_value;
  457. stub_entry->target_section = sym_sec;
  458. }
  459. /* We're done with the internal relocs, free them. */
  460. if (elf_section_data (section)->relocs == NULL)
  461. free (internal_relocs);
  462. }
  463. }
  464. if (add_stub_section)
  465. {
  466. /* OK, we've added some stubs. Find out the new size of the
  467. stub sections. */
  468. for (stub_sec = htab->stub_bfd->sections;
  469. stub_sec != NULL;
  470. stub_sec = stub_sec->next)
  471. {
  472. stub_sec->size = 0;
  473. }
  474. bfd_hash_traverse (htab->stub_hash_table, htab->size_one_stub, htab);
  475. }
  476. free (all_local_syms);
  477. return true;
  478. error_ret_free_local:
  479. free (all_local_syms);
  480. return false;
  481. }
  482. /* Export the trampoline addresses in the symbol table. */
  483. static bool
  484. m68hc11_elf_export_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
  485. {
  486. struct bfd_link_info *info;
  487. struct m68hc11_elf_link_hash_table *htab;
  488. struct elf32_m68hc11_stub_hash_entry *stub_entry;
  489. char* name;
  490. bool result;
  491. info = (struct bfd_link_info *) in_arg;
  492. htab = m68hc11_elf_hash_table (info);
  493. if (htab == NULL)
  494. return false;
  495. /* Massage our args to the form they really have. */
  496. stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
  497. /* Generate the trampoline according to HC11 or HC12. */
  498. result = (* htab->build_one_stub) (gen_entry, in_arg);
  499. /* Make a printable name that does not conflict with the real function. */
  500. name = concat ("tramp.", stub_entry->root.string, NULL);
  501. /* Export the symbol for debugging/disassembling. */
  502. m68hc11_elf_set_symbol (htab->stub_bfd, info, name,
  503. stub_entry->stub_offset,
  504. stub_entry->stub_sec);
  505. free (name);
  506. return result;
  507. }
  508. /* Export a symbol or set its value and section. */
  509. static void
  510. m68hc11_elf_set_symbol (bfd *abfd, struct bfd_link_info *info,
  511. const char *name, bfd_vma value, asection *sec)
  512. {
  513. struct elf_link_hash_entry *h;
  514. h = (struct elf_link_hash_entry *)
  515. bfd_link_hash_lookup (info->hash, name, false, false, false);
  516. if (h == NULL)
  517. {
  518. _bfd_generic_link_add_one_symbol (info, abfd,
  519. name,
  520. BSF_GLOBAL,
  521. sec,
  522. value,
  523. (const char*) NULL,
  524. true, false, NULL);
  525. }
  526. else
  527. {
  528. h->root.type = bfd_link_hash_defined;
  529. h->root.u.def.value = value;
  530. h->root.u.def.section = sec;
  531. }
  532. }
  533. /* Build all the stubs associated with the current output file. The
  534. stubs are kept in a hash table attached to the main linker hash
  535. table. This function is called via m68hc12elf_finish in the
  536. linker. */
  537. bool
  538. elf32_m68hc11_build_stubs (bfd *abfd, struct bfd_link_info *info)
  539. {
  540. asection *stub_sec;
  541. struct bfd_hash_table *table;
  542. struct m68hc11_elf_link_hash_table *htab;
  543. struct m68hc11_scan_param param;
  544. m68hc11_elf_get_bank_parameters (info);
  545. htab = m68hc11_elf_hash_table (info);
  546. if (htab == NULL)
  547. return false;
  548. for (stub_sec = htab->stub_bfd->sections;
  549. stub_sec != NULL;
  550. stub_sec = stub_sec->next)
  551. {
  552. bfd_size_type size;
  553. /* Allocate memory to hold the linker stubs. */
  554. size = stub_sec->size;
  555. stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
  556. if (stub_sec->contents == NULL && size != 0)
  557. return false;
  558. stub_sec->size = 0;
  559. }
  560. /* Build the stubs as directed by the stub hash table. */
  561. table = htab->stub_hash_table;
  562. bfd_hash_traverse (table, m68hc11_elf_export_one_stub, info);
  563. /* Scan the output sections to see if we use the memory banks.
  564. If so, export the symbols that define how the memory banks
  565. are mapped. This is used by gdb and the simulator to obtain
  566. the information. It can be used by programs to burn the eprom
  567. at the good addresses. */
  568. param.use_memory_banks = false;
  569. param.pinfo = &htab->pinfo;
  570. bfd_map_over_sections (abfd, scan_sections_for_abi, &param);
  571. if (param.use_memory_banks)
  572. {
  573. m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_START_NAME,
  574. htab->pinfo.bank_physical,
  575. bfd_abs_section_ptr);
  576. m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_VIRTUAL_NAME,
  577. htab->pinfo.bank_virtual,
  578. bfd_abs_section_ptr);
  579. m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_SIZE_NAME,
  580. htab->pinfo.bank_size,
  581. bfd_abs_section_ptr);
  582. }
  583. return true;
  584. }
  585. void
  586. m68hc11_elf_get_bank_parameters (struct bfd_link_info *info)
  587. {
  588. unsigned i;
  589. struct m68hc11_page_info *pinfo;
  590. struct bfd_link_hash_entry *h;
  591. struct m68hc11_elf_link_hash_table *htab;
  592. htab = m68hc11_elf_hash_table (info);
  593. if (htab == NULL)
  594. return;
  595. pinfo = & htab->pinfo;
  596. if (pinfo->bank_param_initialized)
  597. return;
  598. pinfo->bank_virtual = M68HC12_BANK_VIRT;
  599. pinfo->bank_mask = M68HC12_BANK_MASK;
  600. pinfo->bank_physical = M68HC12_BANK_BASE;
  601. pinfo->bank_shift = M68HC12_BANK_SHIFT;
  602. pinfo->bank_size = 1 << M68HC12_BANK_SHIFT;
  603. h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_START_NAME,
  604. false, false, true);
  605. if (h != (struct bfd_link_hash_entry*) NULL
  606. && h->type == bfd_link_hash_defined)
  607. pinfo->bank_physical = (h->u.def.value
  608. + h->u.def.section->output_section->vma
  609. + h->u.def.section->output_offset);
  610. h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_VIRTUAL_NAME,
  611. false, false, true);
  612. if (h != (struct bfd_link_hash_entry*) NULL
  613. && h->type == bfd_link_hash_defined)
  614. pinfo->bank_virtual = (h->u.def.value
  615. + h->u.def.section->output_section->vma
  616. + h->u.def.section->output_offset);
  617. h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_SIZE_NAME,
  618. false, false, true);
  619. if (h != (struct bfd_link_hash_entry*) NULL
  620. && h->type == bfd_link_hash_defined)
  621. pinfo->bank_size = (h->u.def.value
  622. + h->u.def.section->output_section->vma
  623. + h->u.def.section->output_offset);
  624. pinfo->bank_shift = 0;
  625. for (i = pinfo->bank_size; i != 0; i >>= 1)
  626. pinfo->bank_shift++;
  627. pinfo->bank_shift--;
  628. pinfo->bank_mask = (1 << pinfo->bank_shift) - 1;
  629. pinfo->bank_physical_end = pinfo->bank_physical + pinfo->bank_size;
  630. pinfo->bank_param_initialized = 1;
  631. h = bfd_link_hash_lookup (info->hash, "__far_trampoline", false,
  632. false, true);
  633. if (h != (struct bfd_link_hash_entry*) NULL
  634. && h->type == bfd_link_hash_defined)
  635. pinfo->trampoline_addr = (h->u.def.value
  636. + h->u.def.section->output_section->vma
  637. + h->u.def.section->output_offset);
  638. }
  639. /* Return 1 if the address is in banked memory.
  640. This can be applied to a virtual address and to a physical address. */
  641. int
  642. m68hc11_addr_is_banked (struct m68hc11_page_info *pinfo, bfd_vma addr)
  643. {
  644. if (addr >= pinfo->bank_virtual)
  645. return 1;
  646. if (addr >= pinfo->bank_physical && addr <= pinfo->bank_physical_end)
  647. return 1;
  648. return 0;
  649. }
  650. /* Return the physical address seen by the processor, taking
  651. into account banked memory. */
  652. bfd_vma
  653. m68hc11_phys_addr (struct m68hc11_page_info *pinfo, bfd_vma addr)
  654. {
  655. if (addr < pinfo->bank_virtual)
  656. return addr;
  657. /* Map the address to the memory bank. */
  658. addr -= pinfo->bank_virtual;
  659. addr &= pinfo->bank_mask;
  660. addr += pinfo->bank_physical;
  661. return addr;
  662. }
  663. /* Return the page number corresponding to an address in banked memory. */
  664. bfd_vma
  665. m68hc11_phys_page (struct m68hc11_page_info *pinfo, bfd_vma addr)
  666. {
  667. if (addr < pinfo->bank_virtual)
  668. return 0;
  669. /* Map the address to the memory bank. */
  670. addr -= pinfo->bank_virtual;
  671. addr >>= pinfo->bank_shift;
  672. addr &= 0x0ff;
  673. return addr;
  674. }
  675. /* This function is used for relocs which are only used for relaxing,
  676. which the linker should otherwise ignore. */
  677. bfd_reloc_status_type
  678. m68hc11_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED,
  679. arelent *reloc_entry,
  680. asymbol *symbol ATTRIBUTE_UNUSED,
  681. void *data ATTRIBUTE_UNUSED,
  682. asection *input_section,
  683. bfd *output_bfd,
  684. char **error_message ATTRIBUTE_UNUSED)
  685. {
  686. if (output_bfd != NULL)
  687. reloc_entry->address += input_section->output_offset;
  688. return bfd_reloc_ok;
  689. }
  690. bfd_reloc_status_type
  691. m68hc11_elf_special_reloc (bfd *abfd ATTRIBUTE_UNUSED,
  692. arelent *reloc_entry,
  693. asymbol *symbol,
  694. void *data ATTRIBUTE_UNUSED,
  695. asection *input_section,
  696. bfd *output_bfd,
  697. char **error_message ATTRIBUTE_UNUSED)
  698. {
  699. if (output_bfd != (bfd *) NULL
  700. && (symbol->flags & BSF_SECTION_SYM) == 0
  701. && (! reloc_entry->howto->partial_inplace
  702. || reloc_entry->addend == 0))
  703. {
  704. reloc_entry->address += input_section->output_offset;
  705. return bfd_reloc_ok;
  706. }
  707. if (output_bfd != NULL)
  708. return bfd_reloc_continue;
  709. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  710. return bfd_reloc_outofrange;
  711. abort();
  712. }
  713. /* Look through the relocs for a section during the first phase.
  714. Since we don't do .gots or .plts, we just need to consider the
  715. virtual table relocs for gc. */
  716. bool
  717. elf32_m68hc11_check_relocs (bfd *abfd, struct bfd_link_info *info,
  718. asection *sec, const Elf_Internal_Rela *relocs)
  719. {
  720. Elf_Internal_Shdr * symtab_hdr;
  721. struct elf_link_hash_entry ** sym_hashes;
  722. const Elf_Internal_Rela * rel;
  723. const Elf_Internal_Rela * rel_end;
  724. if (bfd_link_relocatable (info))
  725. return true;
  726. symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
  727. sym_hashes = elf_sym_hashes (abfd);
  728. rel_end = relocs + sec->reloc_count;
  729. for (rel = relocs; rel < rel_end; rel++)
  730. {
  731. struct elf_link_hash_entry * h;
  732. unsigned long r_symndx;
  733. r_symndx = ELF32_R_SYM (rel->r_info);
  734. if (r_symndx < symtab_hdr->sh_info)
  735. h = NULL;
  736. else
  737. {
  738. h = sym_hashes [r_symndx - symtab_hdr->sh_info];
  739. while (h->root.type == bfd_link_hash_indirect
  740. || h->root.type == bfd_link_hash_warning)
  741. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  742. }
  743. switch (ELF32_R_TYPE (rel->r_info))
  744. {
  745. /* This relocation describes the C++ object vtable hierarchy.
  746. Reconstruct it for later use during GC. */
  747. case R_M68HC11_GNU_VTINHERIT:
  748. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  749. return false;
  750. break;
  751. /* This relocation describes which C++ vtable entries are actually
  752. used. Record for later use during GC. */
  753. case R_M68HC11_GNU_VTENTRY:
  754. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  755. return false;
  756. break;
  757. }
  758. }
  759. return true;
  760. }
  761. static bool ATTRIBUTE_PRINTF (6, 7)
  762. reloc_warning (struct bfd_link_info *info, const char *name, bfd *input_bfd,
  763. asection *input_section, const Elf_Internal_Rela *rel,
  764. const char *fmt, ...)
  765. {
  766. va_list ap;
  767. char *buf;
  768. int ret;
  769. va_start (ap, fmt);
  770. ret = vasprintf (&buf, fmt, ap);
  771. va_end (ap);
  772. if (ret < 0)
  773. {
  774. bfd_set_error (bfd_error_no_memory);
  775. return false;
  776. }
  777. info->callbacks->warning (info, buf, name, input_bfd, input_section,
  778. rel->r_offset);
  779. free (buf);
  780. return true;
  781. }
  782. /* Relocate a 68hc11/68hc12 ELF section. */
  783. int
  784. elf32_m68hc11_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
  785. struct bfd_link_info *info,
  786. bfd *input_bfd, asection *input_section,
  787. bfd_byte *contents, Elf_Internal_Rela *relocs,
  788. Elf_Internal_Sym *local_syms,
  789. asection **local_sections)
  790. {
  791. Elf_Internal_Shdr *symtab_hdr;
  792. struct elf_link_hash_entry **sym_hashes;
  793. Elf_Internal_Rela *rel, *relend;
  794. const char *name = NULL;
  795. struct m68hc11_page_info *pinfo;
  796. const struct elf_backend_data * const ebd = get_elf_backend_data (input_bfd);
  797. struct m68hc11_elf_link_hash_table *htab;
  798. unsigned long e_flags;
  799. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  800. sym_hashes = elf_sym_hashes (input_bfd);
  801. e_flags = elf_elfheader (input_bfd)->e_flags;
  802. htab = m68hc11_elf_hash_table (info);
  803. if (htab == NULL)
  804. return false;
  805. /* Get memory bank parameters. */
  806. m68hc11_elf_get_bank_parameters (info);
  807. pinfo = & htab->pinfo;
  808. rel = relocs;
  809. relend = relocs + input_section->reloc_count;
  810. for (; rel < relend; rel++)
  811. {
  812. int r_type;
  813. arelent arel;
  814. reloc_howto_type *howto;
  815. unsigned long r_symndx;
  816. Elf_Internal_Sym *sym;
  817. asection *sec;
  818. bfd_vma relocation = 0;
  819. bfd_reloc_status_type r = bfd_reloc_undefined;
  820. bfd_vma phys_page;
  821. bfd_vma phys_addr;
  822. bfd_vma insn_addr;
  823. bfd_vma insn_page;
  824. bool is_far = false;
  825. bool is_xgate_symbol = false;
  826. bool is_section_symbol = false;
  827. struct elf_link_hash_entry *h;
  828. bfd_vma val;
  829. const char *msg;
  830. r_symndx = ELF32_R_SYM (rel->r_info);
  831. r_type = ELF32_R_TYPE (rel->r_info);
  832. if (r_type == R_M68HC11_GNU_VTENTRY
  833. || r_type == R_M68HC11_GNU_VTINHERIT)
  834. continue;
  835. if (! (*ebd->elf_info_to_howto_rel) (input_bfd, &arel, rel))
  836. continue;
  837. howto = arel.howto;
  838. h = NULL;
  839. sym = NULL;
  840. sec = NULL;
  841. if (r_symndx < symtab_hdr->sh_info)
  842. {
  843. sym = local_syms + r_symndx;
  844. sec = local_sections[r_symndx];
  845. relocation = (sec->output_section->vma
  846. + sec->output_offset
  847. + sym->st_value);
  848. is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
  849. is_xgate_symbol = (sym && (sym->st_target_internal));
  850. is_section_symbol = ELF_ST_TYPE (sym->st_info) & STT_SECTION;
  851. }
  852. else
  853. {
  854. bool unresolved_reloc, warned, ignored;
  855. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  856. r_symndx, symtab_hdr, sym_hashes,
  857. h, sec, relocation, unresolved_reloc,
  858. warned, ignored);
  859. is_far = (h && (h->other & STO_M68HC12_FAR));
  860. is_xgate_symbol = (h && (h->target_internal));
  861. }
  862. if (sec != NULL && discarded_section (sec))
  863. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  864. rel, 1, relend, howto, 0, contents);
  865. if (bfd_link_relocatable (info))
  866. {
  867. /* This is a relocatable link. We don't have to change
  868. anything, unless the reloc is against a section symbol,
  869. in which case we have to adjust according to where the
  870. section symbol winds up in the output section. */
  871. if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  872. rel->r_addend += sec->output_offset;
  873. continue;
  874. }
  875. if (h != NULL)
  876. name = h->root.root.string;
  877. else
  878. {
  879. name = (bfd_elf_string_from_elf_section
  880. (input_bfd, symtab_hdr->sh_link, sym->st_name));
  881. if (name == NULL || *name == '\0')
  882. name = bfd_section_name (sec);
  883. }
  884. if (is_far && ELF32_R_TYPE (rel->r_info) == R_M68HC11_16)
  885. {
  886. struct elf32_m68hc11_stub_hash_entry* stub;
  887. stub = m68hc12_stub_hash_lookup (htab->stub_hash_table,
  888. name, false, false);
  889. if (stub)
  890. {
  891. relocation = stub->stub_offset
  892. + stub->stub_sec->output_section->vma
  893. + stub->stub_sec->output_offset;
  894. is_far = false;
  895. }
  896. }
  897. /* Do the memory bank mapping. */
  898. phys_addr = m68hc11_phys_addr (pinfo, relocation + rel->r_addend);
  899. phys_page = m68hc11_phys_page (pinfo, relocation + rel->r_addend);
  900. switch (r_type)
  901. {
  902. case R_M68HC12_LO8XG:
  903. /* This relocation is specific to XGATE IMM16 calls and will precede
  904. a HI8. tc-m68hc11 only generates them in pairs.
  905. Leave the relocation to the HI8XG step. */
  906. r = bfd_reloc_ok;
  907. r_type = R_M68HC11_NONE;
  908. break;
  909. case R_M68HC12_HI8XG:
  910. /* This relocation is specific to XGATE IMM16 calls and must follow
  911. a LO8XG. Does not actually check that it was a LO8XG.
  912. Adjusts high and low bytes. */
  913. relocation = phys_addr;
  914. if ((e_flags & E_M68HC11_XGATE_RAMOFFSET)
  915. && (relocation >= 0x2000))
  916. relocation += 0xc000; /* HARDCODED RAM offset for XGATE. */
  917. /* Fetch 16 bit value including low byte in previous insn. */
  918. val = (bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset) << 8)
  919. | bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset - 2);
  920. /* Add on value to preserve carry, then write zero to high byte. */
  921. relocation += val;
  922. /* Write out top byte. */
  923. bfd_put_8 (input_bfd, (relocation >> 8) & 0xff,
  924. (bfd_byte*) contents + rel->r_offset);
  925. /* Write out low byte to previous instruction. */
  926. bfd_put_8 (input_bfd, relocation & 0xff,
  927. (bfd_byte*) contents + rel->r_offset - 2);
  928. /* Mark as relocation completed. */
  929. r = bfd_reloc_ok;
  930. r_type = R_M68HC11_NONE;
  931. break;
  932. /* The HI8 and LO8 relocs are generated by %hi(expr) %lo(expr)
  933. assembler directives. %hi does not support carry. */
  934. case R_M68HC11_HI8:
  935. case R_M68HC11_LO8:
  936. relocation = phys_addr;
  937. break;
  938. case R_M68HC11_24:
  939. /* Reloc used by 68HC12 call instruction. */
  940. bfd_put_16 (input_bfd, phys_addr,
  941. (bfd_byte*) contents + rel->r_offset);
  942. bfd_put_8 (input_bfd, phys_page,
  943. (bfd_byte*) contents + rel->r_offset + 2);
  944. r = bfd_reloc_ok;
  945. r_type = R_M68HC11_NONE;
  946. break;
  947. case R_M68HC11_NONE:
  948. r = bfd_reloc_ok;
  949. break;
  950. case R_M68HC11_LO16:
  951. /* Reloc generated by %addr(expr) gas to obtain the
  952. address as mapped in the memory bank window. */
  953. relocation = phys_addr;
  954. break;
  955. case R_M68HC11_PAGE:
  956. /* Reloc generated by %page(expr) gas to obtain the
  957. page number associated with the address. */
  958. relocation = phys_page;
  959. break;
  960. case R_M68HC11_16:
  961. if (is_far)
  962. {
  963. if (!reloc_warning (info, name, input_bfd, input_section, rel,
  964. _("reference to the far symbol `%s' using a "
  965. "wrong relocation may result in incorrect "
  966. "execution"), name))
  967. return false;
  968. }
  969. /* Get virtual address of instruction having the relocation. */
  970. insn_addr = input_section->output_section->vma
  971. + input_section->output_offset
  972. + rel->r_offset;
  973. insn_page = m68hc11_phys_page (pinfo, insn_addr);
  974. /* If we are linking an S12 instruction against an XGATE symbol, we
  975. need to change the offset of the symbol value so that it's correct
  976. from the S12's perspective. */
  977. if (is_xgate_symbol)
  978. {
  979. /* The ram in the global space is mapped to 0x2000 in the 16-bit
  980. address space for S12 and 0xE000 in the 16-bit address space
  981. for XGATE. */
  982. if (relocation >= 0xE000)
  983. {
  984. /* We offset the address by the difference
  985. between these two mappings. */
  986. relocation -= 0xC000;
  987. break;
  988. }
  989. else
  990. {
  991. if (!reloc_warning (info, name, input_bfd, input_section, rel,
  992. _("XGATE address (%lx) is not within "
  993. "shared RAM(0xE000-0xFFFF), therefore "
  994. "you must manually offset the address, "
  995. "and possibly manage the page, in your "
  996. "code."), (long) phys_addr))
  997. return false;
  998. break;
  999. }
  1000. }
  1001. if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend)
  1002. && m68hc11_addr_is_banked (pinfo, insn_addr)
  1003. && phys_page != insn_page
  1004. && !(e_flags & E_M68HC11_NO_BANK_WARNING))
  1005. {
  1006. if (!reloc_warning (info, name, input_bfd, input_section, rel,
  1007. _("banked address [%lx:%04lx] (%lx) is not "
  1008. "in the same bank as current banked "
  1009. "address [%lx:%04lx] (%lx)"),
  1010. (long) phys_page, (long) phys_addr,
  1011. (long) (relocation + rel->r_addend),
  1012. (long) insn_page,
  1013. (long) m68hc11_phys_addr (pinfo, insn_addr),
  1014. (long) insn_addr))
  1015. return false;
  1016. break;
  1017. }
  1018. if (phys_page != 0 && insn_page == 0)
  1019. {
  1020. if (!reloc_warning (info, name, input_bfd, input_section, rel,
  1021. _("reference to a banked address [%lx:%04lx] "
  1022. "in the normal address space at %04lx"),
  1023. (long) phys_page, (long) phys_addr,
  1024. (long) insn_addr))
  1025. return false;
  1026. relocation = phys_addr;
  1027. break;
  1028. }
  1029. /* If this is a banked address use the phys_addr so that
  1030. we stay in the banked window. */
  1031. if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend))
  1032. relocation = phys_addr;
  1033. break;
  1034. }
  1035. /* If we are linking an XGATE instruction against an S12 symbol, we
  1036. need to change the offset of the symbol value so that it's correct
  1037. from the XGATE's perspective. */
  1038. if (!strcmp (howto->name, "R_XGATE_IMM8_LO")
  1039. || !strcmp (howto->name, "R_XGATE_IMM8_HI"))
  1040. {
  1041. /* We can only offset S12 addresses that lie within the non-paged
  1042. area of RAM. */
  1043. if (!is_xgate_symbol && !is_section_symbol)
  1044. {
  1045. /* The ram in the global space is mapped to 0x2000 and stops at
  1046. 0x4000 in the 16-bit address space for S12 and 0xE000 in the
  1047. 16-bit address space for XGATE. */
  1048. if (relocation >= 0x2000 && relocation < 0x4000)
  1049. /* We offset the address by the difference
  1050. between these two mappings. */
  1051. relocation += 0xC000;
  1052. else
  1053. {
  1054. if (!reloc_warning (info, name, input_bfd, input_section, rel,
  1055. _("S12 address (%lx) is not within "
  1056. "shared RAM(0x2000-0x4000), therefore "
  1057. "you must manually offset the address "
  1058. "in your code"), (long) phys_addr))
  1059. return false;
  1060. break;
  1061. }
  1062. }
  1063. }
  1064. if (r_type != R_M68HC11_NONE)
  1065. {
  1066. if ((r_type == R_M68HC12_PCREL_9) || (r_type == R_M68HC12_PCREL_10))
  1067. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  1068. contents, rel->r_offset,
  1069. relocation - 2, rel->r_addend);
  1070. else
  1071. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  1072. contents, rel->r_offset,
  1073. relocation, rel->r_addend);
  1074. }
  1075. if (r != bfd_reloc_ok)
  1076. {
  1077. switch (r)
  1078. {
  1079. case bfd_reloc_overflow:
  1080. (*info->callbacks->reloc_overflow)
  1081. (info, NULL, name, howto->name, (bfd_vma) 0,
  1082. input_bfd, input_section, rel->r_offset);
  1083. break;
  1084. case bfd_reloc_undefined:
  1085. (*info->callbacks->undefined_symbol)
  1086. (info, name, input_bfd, input_section, rel->r_offset, true);
  1087. break;
  1088. case bfd_reloc_outofrange:
  1089. msg = _ ("internal error: out of range error");
  1090. goto common_error;
  1091. case bfd_reloc_notsupported:
  1092. msg = _ ("internal error: unsupported relocation error");
  1093. goto common_error;
  1094. case bfd_reloc_dangerous:
  1095. msg = _ ("internal error: dangerous error");
  1096. goto common_error;
  1097. default:
  1098. msg = _ ("internal error: unknown error");
  1099. /* fall through */
  1100. common_error:
  1101. (*info->callbacks->warning) (info, msg, name, input_bfd,
  1102. input_section, rel->r_offset);
  1103. break;
  1104. }
  1105. }
  1106. }
  1107. return true;
  1108. }
  1109. /* Set and control ELF flags in ELF header. */
  1110. bool
  1111. _bfd_m68hc11_elf_set_private_flags (bfd *abfd, flagword flags)
  1112. {
  1113. BFD_ASSERT (!elf_flags_init (abfd)
  1114. || elf_elfheader (abfd)->e_flags == flags);
  1115. elf_elfheader (abfd)->e_flags = flags;
  1116. elf_flags_init (abfd) = true;
  1117. return true;
  1118. }
  1119. /* Merge backend specific data from an object file to the output
  1120. object file when linking. */
  1121. bool
  1122. _bfd_m68hc11_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  1123. {
  1124. bfd *obfd = info->output_bfd;
  1125. flagword old_flags;
  1126. flagword new_flags;
  1127. bool ok = true;
  1128. /* Check if we have the same endianness */
  1129. if (!_bfd_generic_verify_endian_match (ibfd, info))
  1130. return false;
  1131. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  1132. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  1133. return true;
  1134. new_flags = elf_elfheader (ibfd)->e_flags;
  1135. elf_elfheader (obfd)->e_flags |= new_flags & EF_M68HC11_ABI;
  1136. old_flags = elf_elfheader (obfd)->e_flags;
  1137. if (! elf_flags_init (obfd))
  1138. {
  1139. elf_flags_init (obfd) = true;
  1140. elf_elfheader (obfd)->e_flags = new_flags;
  1141. elf_elfheader (obfd)->e_ident[EI_CLASS]
  1142. = elf_elfheader (ibfd)->e_ident[EI_CLASS];
  1143. if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
  1144. && bfd_get_arch_info (obfd)->the_default)
  1145. {
  1146. if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
  1147. bfd_get_mach (ibfd)))
  1148. return false;
  1149. }
  1150. return true;
  1151. }
  1152. /* Check ABI compatibility. */
  1153. if ((new_flags & E_M68HC11_I32) != (old_flags & E_M68HC11_I32))
  1154. {
  1155. _bfd_error_handler
  1156. (_("%pB: linking files compiled for 16-bit integers (-mshort) "
  1157. "and others for 32-bit integers"), ibfd);
  1158. ok = false;
  1159. }
  1160. if ((new_flags & E_M68HC11_F64) != (old_flags & E_M68HC11_F64))
  1161. {
  1162. _bfd_error_handler
  1163. (_("%pB: linking files compiled for 32-bit double (-fshort-double) "
  1164. "and others for 64-bit double"), ibfd);
  1165. ok = false;
  1166. }
  1167. /* Processor compatibility. */
  1168. if (!EF_M68HC11_CAN_MERGE_MACH (new_flags, old_flags))
  1169. {
  1170. _bfd_error_handler
  1171. (_("%pB: linking files compiled for HCS12 with "
  1172. "others compiled for HC12"), ibfd);
  1173. ok = false;
  1174. }
  1175. new_flags = ((new_flags & ~EF_M68HC11_MACH_MASK)
  1176. | (EF_M68HC11_MERGE_MACH (new_flags, old_flags)));
  1177. elf_elfheader (obfd)->e_flags = new_flags;
  1178. new_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
  1179. old_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
  1180. /* Warn about any other mismatches */
  1181. if (new_flags != old_flags)
  1182. {
  1183. _bfd_error_handler
  1184. /* xgettext:c-format */
  1185. (_("%pB: uses different e_flags (%#x) fields than previous modules (%#x)"),
  1186. ibfd, new_flags, old_flags);
  1187. ok = false;
  1188. }
  1189. if (! ok)
  1190. {
  1191. bfd_set_error (bfd_error_bad_value);
  1192. return false;
  1193. }
  1194. return true;
  1195. }
  1196. bool
  1197. _bfd_m68hc11_elf_print_private_bfd_data (bfd *abfd, void *ptr)
  1198. {
  1199. FILE *file = (FILE *) ptr;
  1200. BFD_ASSERT (abfd != NULL && ptr != NULL);
  1201. /* Print normal ELF private data. */
  1202. _bfd_elf_print_private_bfd_data (abfd, ptr);
  1203. /* xgettext:c-format */
  1204. fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
  1205. if (elf_elfheader (abfd)->e_flags & E_M68HC11_I32)
  1206. fprintf (file, _("[abi=32-bit int, "));
  1207. else
  1208. fprintf (file, _("[abi=16-bit int, "));
  1209. if (elf_elfheader (abfd)->e_flags & E_M68HC11_F64)
  1210. fprintf (file, _("64-bit double, "));
  1211. else
  1212. fprintf (file, _("32-bit double, "));
  1213. if (strcmp (bfd_get_target (abfd), "elf32-m68hc11") == 0)
  1214. fprintf (file, _("cpu=HC11]"));
  1215. else if (elf_elfheader (abfd)->e_flags & EF_M68HCS12_MACH)
  1216. fprintf (file, _("cpu=HCS12]"));
  1217. else
  1218. fprintf (file, _("cpu=HC12]"));
  1219. if (elf_elfheader (abfd)->e_flags & E_M68HC12_BANKS)
  1220. fprintf (file, _(" [memory=bank-model]"));
  1221. else
  1222. fprintf (file, _(" [memory=flat]"));
  1223. if (elf_elfheader (abfd)->e_flags & E_M68HC11_XGATE_RAMOFFSET)
  1224. fprintf (file, _(" [XGATE RAM offsetting]"));
  1225. fputc ('\n', file);
  1226. return true;
  1227. }
  1228. static void scan_sections_for_abi (bfd *abfd ATTRIBUTE_UNUSED,
  1229. asection *asect, void *arg)
  1230. {
  1231. struct m68hc11_scan_param* p = (struct m68hc11_scan_param*) arg;
  1232. if (asect->vma >= p->pinfo->bank_virtual)
  1233. p->use_memory_banks = true;
  1234. }
  1235. /* Tweak the OSABI field of the elf header. */
  1236. bool
  1237. elf32_m68hc11_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
  1238. {
  1239. struct m68hc11_scan_param param;
  1240. struct m68hc11_elf_link_hash_table *htab;
  1241. if (!_bfd_elf_init_file_header (abfd, link_info))
  1242. return false;
  1243. if (link_info == NULL)
  1244. return true;
  1245. htab = m68hc11_elf_hash_table (link_info);
  1246. if (htab == NULL)
  1247. return true;
  1248. m68hc11_elf_get_bank_parameters (link_info);
  1249. param.use_memory_banks = false;
  1250. param.pinfo = & htab->pinfo;
  1251. bfd_map_over_sections (abfd, scan_sections_for_abi, &param);
  1252. if (param.use_memory_banks)
  1253. {
  1254. Elf_Internal_Ehdr * i_ehdrp;
  1255. i_ehdrp = elf_elfheader (abfd);
  1256. i_ehdrp->e_flags |= E_M68HC12_BANKS;
  1257. }
  1258. return true;
  1259. }