elf32-m68hc1x.c 47 KB

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