elf64-sparc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /* SPARC-specific support for 64-bit ELF
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "libbfd.h"
  19. #include "elf-bfd.h"
  20. #include "elf/sparc.h"
  21. #include "opcode/sparc.h"
  22. #include "elfxx-sparc.h"
  23. /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
  24. #define MINUS_ONE (~ (bfd_vma) 0)
  25. /* Due to the way how we handle R_SPARC_OLO10, each entry in a SHT_RELA
  26. section can represent up to two relocs, we must tell the user to allocate
  27. more space. */
  28. static long
  29. elf64_sparc_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, asection *sec)
  30. {
  31. return (sec->reloc_count * 2 + 1) * sizeof (arelent *);
  32. }
  33. static long
  34. elf64_sparc_get_dynamic_reloc_upper_bound (bfd *abfd)
  35. {
  36. return _bfd_elf_get_dynamic_reloc_upper_bound (abfd) * 2;
  37. }
  38. /* Read relocations for ASECT from REL_HDR. There are RELOC_COUNT of
  39. them. We cannot use generic elf routines for this, because R_SPARC_OLO10
  40. has secondary addend in ELF64_R_TYPE_DATA. We handle it as two relocations
  41. for the same location, R_SPARC_LO10 and R_SPARC_13. */
  42. static bfd_boolean
  43. elf64_sparc_slurp_one_reloc_table (bfd *abfd, asection *asect,
  44. Elf_Internal_Shdr *rel_hdr,
  45. asymbol **symbols, bfd_boolean dynamic)
  46. {
  47. void * allocated = NULL;
  48. bfd_byte *native_relocs;
  49. arelent *relent;
  50. unsigned int i;
  51. int entsize;
  52. bfd_size_type count;
  53. arelent *relents;
  54. allocated = bfd_malloc (rel_hdr->sh_size);
  55. if (allocated == NULL)
  56. goto error_return;
  57. if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0
  58. || bfd_bread (allocated, rel_hdr->sh_size, abfd) != rel_hdr->sh_size)
  59. goto error_return;
  60. native_relocs = (bfd_byte *) allocated;
  61. relents = asect->relocation + canon_reloc_count (asect);
  62. entsize = rel_hdr->sh_entsize;
  63. BFD_ASSERT (entsize == sizeof (Elf64_External_Rela));
  64. count = rel_hdr->sh_size / entsize;
  65. for (i = 0, relent = relents; i < count;
  66. i++, relent++, native_relocs += entsize)
  67. {
  68. Elf_Internal_Rela rela;
  69. unsigned int r_type;
  70. bfd_elf64_swap_reloca_in (abfd, native_relocs, &rela);
  71. /* The address of an ELF reloc is section relative for an object
  72. file, and absolute for an executable file or shared library.
  73. The address of a normal BFD reloc is always section relative,
  74. and the address of a dynamic reloc is absolute.. */
  75. if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic)
  76. relent->address = rela.r_offset;
  77. else
  78. relent->address = rela.r_offset - asect->vma;
  79. if (ELF64_R_SYM (rela.r_info) == STN_UNDEF
  80. /* PR 17512: file: 996185f8. */
  81. || ELF64_R_SYM (rela.r_info) > bfd_get_symcount (abfd))
  82. relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  83. else
  84. {
  85. asymbol **ps, *s;
  86. ps = symbols + ELF64_R_SYM (rela.r_info) - 1;
  87. s = *ps;
  88. /* Canonicalize ELF section symbols. FIXME: Why? */
  89. if ((s->flags & BSF_SECTION_SYM) == 0)
  90. relent->sym_ptr_ptr = ps;
  91. else
  92. relent->sym_ptr_ptr = s->section->symbol_ptr_ptr;
  93. }
  94. relent->addend = rela.r_addend;
  95. r_type = ELF64_R_TYPE_ID (rela.r_info);
  96. if (r_type == R_SPARC_OLO10)
  97. {
  98. relent->howto = _bfd_sparc_elf_info_to_howto_ptr (R_SPARC_LO10);
  99. relent[1].address = relent->address;
  100. relent++;
  101. relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
  102. relent->addend = ELF64_R_TYPE_DATA (rela.r_info);
  103. relent->howto = _bfd_sparc_elf_info_to_howto_ptr (R_SPARC_13);
  104. }
  105. else
  106. relent->howto = _bfd_sparc_elf_info_to_howto_ptr (r_type);
  107. }
  108. canon_reloc_count (asect) += relent - relents;
  109. if (allocated != NULL)
  110. free (allocated);
  111. return TRUE;
  112. error_return:
  113. if (allocated != NULL)
  114. free (allocated);
  115. return FALSE;
  116. }
  117. /* Read in and swap the external relocs. */
  118. static bfd_boolean
  119. elf64_sparc_slurp_reloc_table (bfd *abfd, asection *asect,
  120. asymbol **symbols, bfd_boolean dynamic)
  121. {
  122. struct bfd_elf_section_data * const d = elf_section_data (asect);
  123. Elf_Internal_Shdr *rel_hdr;
  124. Elf_Internal_Shdr *rel_hdr2;
  125. bfd_size_type amt;
  126. if (asect->relocation != NULL)
  127. return TRUE;
  128. if (! dynamic)
  129. {
  130. if ((asect->flags & SEC_RELOC) == 0
  131. || asect->reloc_count == 0)
  132. return TRUE;
  133. rel_hdr = d->rel.hdr;
  134. rel_hdr2 = d->rela.hdr;
  135. BFD_ASSERT ((rel_hdr && asect->rel_filepos == rel_hdr->sh_offset)
  136. || (rel_hdr2 && asect->rel_filepos == rel_hdr2->sh_offset));
  137. }
  138. else
  139. {
  140. /* Note that ASECT->RELOC_COUNT tends not to be accurate in this
  141. case because relocations against this section may use the
  142. dynamic symbol table, and in that case bfd_section_from_shdr
  143. in elf.c does not update the RELOC_COUNT. */
  144. if (asect->size == 0)
  145. return TRUE;
  146. rel_hdr = &d->this_hdr;
  147. asect->reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
  148. rel_hdr2 = NULL;
  149. }
  150. amt = asect->reloc_count;
  151. amt *= 2 * sizeof (arelent);
  152. asect->relocation = (arelent *) bfd_alloc (abfd, amt);
  153. if (asect->relocation == NULL)
  154. return FALSE;
  155. /* The elf64_sparc_slurp_one_reloc_table routine increments
  156. canon_reloc_count. */
  157. canon_reloc_count (asect) = 0;
  158. if (rel_hdr
  159. && !elf64_sparc_slurp_one_reloc_table (abfd, asect, rel_hdr, symbols,
  160. dynamic))
  161. return FALSE;
  162. if (rel_hdr2
  163. && !elf64_sparc_slurp_one_reloc_table (abfd, asect, rel_hdr2, symbols,
  164. dynamic))
  165. return FALSE;
  166. return TRUE;
  167. }
  168. /* Canonicalize the relocs. */
  169. static long
  170. elf64_sparc_canonicalize_reloc (bfd *abfd, sec_ptr section,
  171. arelent **relptr, asymbol **symbols)
  172. {
  173. arelent *tblptr;
  174. unsigned int i;
  175. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  176. if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE))
  177. return -1;
  178. tblptr = section->relocation;
  179. for (i = 0; i < canon_reloc_count (section); i++)
  180. *relptr++ = tblptr++;
  181. *relptr = NULL;
  182. return canon_reloc_count (section);
  183. }
  184. /* Canonicalize the dynamic relocation entries. Note that we return
  185. the dynamic relocations as a single block, although they are
  186. actually associated with particular sections; the interface, which
  187. was designed for SunOS style shared libraries, expects that there
  188. is only one set of dynamic relocs. Any section that was actually
  189. installed in the BFD, and has type SHT_REL or SHT_RELA, and uses
  190. the dynamic symbol table, is considered to be a dynamic reloc
  191. section. */
  192. static long
  193. elf64_sparc_canonicalize_dynamic_reloc (bfd *abfd, arelent **storage,
  194. asymbol **syms)
  195. {
  196. asection *s;
  197. long ret;
  198. if (elf_dynsymtab (abfd) == 0)
  199. {
  200. bfd_set_error (bfd_error_invalid_operation);
  201. return -1;
  202. }
  203. ret = 0;
  204. for (s = abfd->sections; s != NULL; s = s->next)
  205. {
  206. if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd)
  207. && (elf_section_data (s)->this_hdr.sh_type == SHT_RELA))
  208. {
  209. arelent *p;
  210. long count, i;
  211. if (! elf64_sparc_slurp_reloc_table (abfd, s, syms, TRUE))
  212. return -1;
  213. count = canon_reloc_count (s);
  214. p = s->relocation;
  215. for (i = 0; i < count; i++)
  216. *storage++ = p++;
  217. ret += count;
  218. }
  219. }
  220. *storage = NULL;
  221. return ret;
  222. }
  223. /* Write out the relocs. */
  224. static void
  225. elf64_sparc_write_relocs (bfd *abfd, asection *sec, void * data)
  226. {
  227. bfd_boolean *failedp = (bfd_boolean *) data;
  228. Elf_Internal_Shdr *rela_hdr;
  229. bfd_vma addr_offset;
  230. Elf64_External_Rela *outbound_relocas, *src_rela;
  231. unsigned int idx, count;
  232. asymbol *last_sym = 0;
  233. int last_sym_idx = 0;
  234. /* If we have already failed, don't do anything. */
  235. if (*failedp)
  236. return;
  237. if ((sec->flags & SEC_RELOC) == 0)
  238. return;
  239. /* The linker backend writes the relocs out itself, and sets the
  240. reloc_count field to zero to inhibit writing them here. Also,
  241. sometimes the SEC_RELOC flag gets set even when there aren't any
  242. relocs. */
  243. if (sec->reloc_count == 0)
  244. return;
  245. /* We can combine two relocs that refer to the same address
  246. into R_SPARC_OLO10 if first one is R_SPARC_LO10 and the
  247. latter is R_SPARC_13 with no associated symbol. */
  248. count = 0;
  249. for (idx = 0; idx < sec->reloc_count; idx++)
  250. {
  251. bfd_vma addr;
  252. ++count;
  253. addr = sec->orelocation[idx]->address;
  254. if (sec->orelocation[idx]->howto->type == R_SPARC_LO10
  255. && idx < sec->reloc_count - 1)
  256. {
  257. arelent *r = sec->orelocation[idx + 1];
  258. if (r->howto->type == R_SPARC_13
  259. && r->address == addr
  260. && bfd_is_abs_section ((*r->sym_ptr_ptr)->section)
  261. && (*r->sym_ptr_ptr)->value == 0)
  262. ++idx;
  263. }
  264. }
  265. rela_hdr = elf_section_data (sec)->rela.hdr;
  266. rela_hdr->sh_size = rela_hdr->sh_entsize * count;
  267. rela_hdr->contents = bfd_alloc (abfd, rela_hdr->sh_size);
  268. if (rela_hdr->contents == NULL)
  269. {
  270. *failedp = TRUE;
  271. return;
  272. }
  273. /* Figure out whether the relocations are RELA or REL relocations. */
  274. if (rela_hdr->sh_type != SHT_RELA)
  275. abort ();
  276. /* The address of an ELF reloc is section relative for an object
  277. file, and absolute for an executable file or shared library.
  278. The address of a BFD reloc is always section relative. */
  279. addr_offset = 0;
  280. if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0)
  281. addr_offset = sec->vma;
  282. /* orelocation has the data, reloc_count has the count... */
  283. outbound_relocas = (Elf64_External_Rela *) rela_hdr->contents;
  284. src_rela = outbound_relocas;
  285. for (idx = 0; idx < sec->reloc_count; idx++)
  286. {
  287. Elf_Internal_Rela dst_rela;
  288. arelent *ptr;
  289. asymbol *sym;
  290. int n;
  291. ptr = sec->orelocation[idx];
  292. sym = *ptr->sym_ptr_ptr;
  293. if (sym == last_sym)
  294. n = last_sym_idx;
  295. else if (bfd_is_abs_section (sym->section) && sym->value == 0)
  296. n = STN_UNDEF;
  297. else
  298. {
  299. last_sym = sym;
  300. n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym);
  301. if (n < 0)
  302. {
  303. *failedp = TRUE;
  304. return;
  305. }
  306. last_sym_idx = n;
  307. }
  308. if ((*ptr->sym_ptr_ptr)->the_bfd != NULL
  309. && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec
  310. && ! _bfd_elf_validate_reloc (abfd, ptr))
  311. {
  312. *failedp = TRUE;
  313. return;
  314. }
  315. if (ptr->howto->type == R_SPARC_LO10
  316. && idx < sec->reloc_count - 1)
  317. {
  318. arelent *r = sec->orelocation[idx + 1];
  319. if (r->howto->type == R_SPARC_13
  320. && r->address == ptr->address
  321. && bfd_is_abs_section ((*r->sym_ptr_ptr)->section)
  322. && (*r->sym_ptr_ptr)->value == 0)
  323. {
  324. idx++;
  325. dst_rela.r_info
  326. = ELF64_R_INFO (n, ELF64_R_TYPE_INFO (r->addend,
  327. R_SPARC_OLO10));
  328. }
  329. else
  330. dst_rela.r_info = ELF64_R_INFO (n, R_SPARC_LO10);
  331. }
  332. else
  333. dst_rela.r_info = ELF64_R_INFO (n, ptr->howto->type);
  334. dst_rela.r_offset = ptr->address + addr_offset;
  335. dst_rela.r_addend = ptr->addend;
  336. bfd_elf64_swap_reloca_out (abfd, &dst_rela, (bfd_byte *) src_rela);
  337. ++src_rela;
  338. }
  339. }
  340. /* Hook called by the linker routine which adds symbols from an object
  341. file. We use it for STT_REGISTER symbols. */
  342. static bfd_boolean
  343. elf64_sparc_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
  344. Elf_Internal_Sym *sym, const char **namep,
  345. flagword *flagsp ATTRIBUTE_UNUSED,
  346. asection **secp ATTRIBUTE_UNUSED,
  347. bfd_vma *valp ATTRIBUTE_UNUSED)
  348. {
  349. static const char *const stt_types[] = { "NOTYPE", "OBJECT", "FUNCTION" };
  350. if ((ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
  351. || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
  352. && (abfd->flags & DYNAMIC) == 0
  353. && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
  354. elf_tdata (info->output_bfd)->has_gnu_symbols = elf_gnu_symbol_any;
  355. if (ELF_ST_TYPE (sym->st_info) == STT_REGISTER)
  356. {
  357. int reg;
  358. struct _bfd_sparc_elf_app_reg *p;
  359. reg = (int)sym->st_value;
  360. switch (reg & ~1)
  361. {
  362. case 2: reg -= 2; break;
  363. case 6: reg -= 4; break;
  364. default:
  365. (*_bfd_error_handler)
  366. (_("%B: Only registers %%g[2367] can be declared using STT_REGISTER"),
  367. abfd);
  368. return FALSE;
  369. }
  370. if (info->output_bfd->xvec != abfd->xvec
  371. || (abfd->flags & DYNAMIC) != 0)
  372. {
  373. /* STT_REGISTER only works when linking an elf64_sparc object.
  374. If STT_REGISTER comes from a dynamic object, don't put it into
  375. the output bfd. The dynamic linker will recheck it. */
  376. *namep = NULL;
  377. return TRUE;
  378. }
  379. p = _bfd_sparc_elf_hash_table(info)->app_regs + reg;
  380. if (p->name != NULL && strcmp (p->name, *namep))
  381. {
  382. (*_bfd_error_handler)
  383. (_("Register %%g%d used incompatibly: %s in %B, previously %s in %B"),
  384. abfd, p->abfd, (int) sym->st_value,
  385. **namep ? *namep : "#scratch",
  386. *p->name ? p->name : "#scratch");
  387. return FALSE;
  388. }
  389. if (p->name == NULL)
  390. {
  391. if (**namep)
  392. {
  393. struct elf_link_hash_entry *h;
  394. h = (struct elf_link_hash_entry *)
  395. bfd_link_hash_lookup (info->hash, *namep, FALSE, FALSE, FALSE);
  396. if (h != NULL)
  397. {
  398. unsigned char type = h->type;
  399. if (type > STT_FUNC)
  400. type = 0;
  401. (*_bfd_error_handler)
  402. (_("Symbol `%s' has differing types: REGISTER in %B, previously %s in %B"),
  403. abfd, p->abfd, *namep, stt_types[type]);
  404. return FALSE;
  405. }
  406. p->name = bfd_hash_allocate (&info->hash->table,
  407. strlen (*namep) + 1);
  408. if (!p->name)
  409. return FALSE;
  410. strcpy (p->name, *namep);
  411. }
  412. else
  413. p->name = "";
  414. p->bind = ELF_ST_BIND (sym->st_info);
  415. p->abfd = abfd;
  416. p->shndx = sym->st_shndx;
  417. }
  418. else
  419. {
  420. if (p->bind == STB_WEAK
  421. && ELF_ST_BIND (sym->st_info) == STB_GLOBAL)
  422. {
  423. p->bind = STB_GLOBAL;
  424. p->abfd = abfd;
  425. }
  426. }
  427. *namep = NULL;
  428. return TRUE;
  429. }
  430. else if (*namep && **namep
  431. && info->output_bfd->xvec == abfd->xvec)
  432. {
  433. int i;
  434. struct _bfd_sparc_elf_app_reg *p;
  435. p = _bfd_sparc_elf_hash_table(info)->app_regs;
  436. for (i = 0; i < 4; i++, p++)
  437. if (p->name != NULL && ! strcmp (p->name, *namep))
  438. {
  439. unsigned char type = ELF_ST_TYPE (sym->st_info);
  440. if (type > STT_FUNC)
  441. type = 0;
  442. (*_bfd_error_handler)
  443. (_("Symbol `%s' has differing types: %s in %B, previously REGISTER in %B"),
  444. abfd, p->abfd, *namep, stt_types[type]);
  445. return FALSE;
  446. }
  447. }
  448. return TRUE;
  449. }
  450. /* This function takes care of emitting STT_REGISTER symbols
  451. which we cannot easily keep in the symbol hash table. */
  452. static bfd_boolean
  453. elf64_sparc_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
  454. struct bfd_link_info *info,
  455. void * flaginfo,
  456. int (*func) (void *, const char *,
  457. Elf_Internal_Sym *,
  458. asection *,
  459. struct elf_link_hash_entry *))
  460. {
  461. int reg;
  462. struct _bfd_sparc_elf_app_reg *app_regs =
  463. _bfd_sparc_elf_hash_table(info)->app_regs;
  464. Elf_Internal_Sym sym;
  465. /* We arranged in size_dynamic_sections to put the STT_REGISTER entries
  466. at the end of the dynlocal list, so they came at the end of the local
  467. symbols in the symtab. Except that they aren't STB_LOCAL, so we need
  468. to back up symtab->sh_info. */
  469. if (elf_hash_table (info)->dynlocal)
  470. {
  471. bfd * dynobj = elf_hash_table (info)->dynobj;
  472. asection *dynsymsec = bfd_get_linker_section (dynobj, ".dynsym");
  473. struct elf_link_local_dynamic_entry *e;
  474. for (e = elf_hash_table (info)->dynlocal; e ; e = e->next)
  475. if (e->input_indx == -1)
  476. break;
  477. if (e)
  478. {
  479. elf_section_data (dynsymsec->output_section)->this_hdr.sh_info
  480. = e->dynindx;
  481. }
  482. }
  483. if (info->strip == strip_all)
  484. return TRUE;
  485. for (reg = 0; reg < 4; reg++)
  486. if (app_regs [reg].name != NULL)
  487. {
  488. if (info->strip == strip_some
  489. && bfd_hash_lookup (info->keep_hash,
  490. app_regs [reg].name,
  491. FALSE, FALSE) == NULL)
  492. continue;
  493. sym.st_value = reg < 2 ? reg + 2 : reg + 4;
  494. sym.st_size = 0;
  495. sym.st_other = 0;
  496. sym.st_info = ELF_ST_INFO (app_regs [reg].bind, STT_REGISTER);
  497. sym.st_shndx = app_regs [reg].shndx;
  498. sym.st_target_internal = 0;
  499. if ((*func) (flaginfo, app_regs [reg].name, &sym,
  500. sym.st_shndx == SHN_ABS
  501. ? bfd_abs_section_ptr : bfd_und_section_ptr,
  502. NULL) != 1)
  503. return FALSE;
  504. }
  505. return TRUE;
  506. }
  507. static int
  508. elf64_sparc_get_symbol_type (Elf_Internal_Sym *elf_sym, int type)
  509. {
  510. if (ELF_ST_TYPE (elf_sym->st_info) == STT_REGISTER)
  511. return STT_REGISTER;
  512. else
  513. return type;
  514. }
  515. /* A STB_GLOBAL,STT_REGISTER symbol should be BSF_GLOBAL
  516. even in SHN_UNDEF section. */
  517. static void
  518. elf64_sparc_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED, asymbol *asym)
  519. {
  520. elf_symbol_type *elfsym;
  521. elfsym = (elf_symbol_type *) asym;
  522. if (elfsym->internal_elf_sym.st_info
  523. == ELF_ST_INFO (STB_GLOBAL, STT_REGISTER))
  524. {
  525. asym->flags |= BSF_GLOBAL;
  526. }
  527. }
  528. /* Functions for dealing with the e_flags field. */
  529. /* Merge backend specific data from an object file to the output
  530. object file when linking. */
  531. static bfd_boolean
  532. elf64_sparc_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
  533. {
  534. bfd_boolean error;
  535. flagword new_flags, old_flags;
  536. int new_mm, old_mm;
  537. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  538. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  539. return TRUE;
  540. new_flags = elf_elfheader (ibfd)->e_flags;
  541. old_flags = elf_elfheader (obfd)->e_flags;
  542. if (!elf_flags_init (obfd)) /* First call, no flags set */
  543. {
  544. elf_flags_init (obfd) = TRUE;
  545. elf_elfheader (obfd)->e_flags = new_flags;
  546. }
  547. else if (new_flags == old_flags) /* Compatible flags are ok */
  548. ;
  549. else /* Incompatible flags */
  550. {
  551. error = FALSE;
  552. #define EF_SPARC_ISA_EXTENSIONS \
  553. (EF_SPARC_SUN_US1 | EF_SPARC_SUN_US3 | EF_SPARC_HAL_R1)
  554. if ((ibfd->flags & DYNAMIC) != 0)
  555. {
  556. /* We don't want dynamic objects memory ordering and
  557. architecture to have any role. That's what dynamic linker
  558. should do. */
  559. new_flags &= ~(EF_SPARCV9_MM | EF_SPARC_ISA_EXTENSIONS);
  560. new_flags |= (old_flags
  561. & (EF_SPARCV9_MM | EF_SPARC_ISA_EXTENSIONS));
  562. }
  563. else
  564. {
  565. /* Choose the highest architecture requirements. */
  566. old_flags |= (new_flags & EF_SPARC_ISA_EXTENSIONS);
  567. new_flags |= (old_flags & EF_SPARC_ISA_EXTENSIONS);
  568. if ((old_flags & (EF_SPARC_SUN_US1 | EF_SPARC_SUN_US3))
  569. && (old_flags & EF_SPARC_HAL_R1))
  570. {
  571. error = TRUE;
  572. (*_bfd_error_handler)
  573. (_("%B: linking UltraSPARC specific with HAL specific code"),
  574. ibfd);
  575. }
  576. /* Choose the most restrictive memory ordering. */
  577. old_mm = (old_flags & EF_SPARCV9_MM);
  578. new_mm = (new_flags & EF_SPARCV9_MM);
  579. old_flags &= ~EF_SPARCV9_MM;
  580. new_flags &= ~EF_SPARCV9_MM;
  581. if (new_mm < old_mm)
  582. old_mm = new_mm;
  583. old_flags |= old_mm;
  584. new_flags |= old_mm;
  585. }
  586. /* Warn about any other mismatches */
  587. if (new_flags != old_flags)
  588. {
  589. error = TRUE;
  590. (*_bfd_error_handler)
  591. (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
  592. ibfd, (long) new_flags, (long) old_flags);
  593. }
  594. elf_elfheader (obfd)->e_flags = old_flags;
  595. if (error)
  596. {
  597. bfd_set_error (bfd_error_bad_value);
  598. return FALSE;
  599. }
  600. }
  601. return _bfd_sparc_elf_merge_private_bfd_data (ibfd, obfd);
  602. }
  603. /* MARCO: Set the correct entry size for the .stab section. */
  604. static bfd_boolean
  605. elf64_sparc_fake_sections (bfd *abfd ATTRIBUTE_UNUSED,
  606. Elf_Internal_Shdr *hdr ATTRIBUTE_UNUSED,
  607. asection *sec)
  608. {
  609. const char *name;
  610. name = bfd_get_section_name (abfd, sec);
  611. if (strcmp (name, ".stab") == 0)
  612. {
  613. /* Even in the 64bit case the stab entries are only 12 bytes long. */
  614. elf_section_data (sec)->this_hdr.sh_entsize = 12;
  615. }
  616. return TRUE;
  617. }
  618. /* Print a STT_REGISTER symbol to file FILE. */
  619. static const char *
  620. elf64_sparc_print_symbol_all (bfd *abfd ATTRIBUTE_UNUSED, void * filep,
  621. asymbol *symbol)
  622. {
  623. FILE *file = (FILE *) filep;
  624. int reg, type;
  625. if (ELF_ST_TYPE (((elf_symbol_type *) symbol)->internal_elf_sym.st_info)
  626. != STT_REGISTER)
  627. return NULL;
  628. reg = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value;
  629. type = symbol->flags;
  630. fprintf (file, "REG_%c%c%11s%c%c R", "GOLI" [reg / 8], '0' + (reg & 7), "",
  631. ((type & BSF_LOCAL)
  632. ? (type & BSF_GLOBAL) ? '!' : 'l'
  633. : (type & BSF_GLOBAL) ? 'g' : ' '),
  634. (type & BSF_WEAK) ? 'w' : ' ');
  635. if (symbol->name == NULL || symbol->name [0] == '\0')
  636. return "#scratch";
  637. else
  638. return symbol->name;
  639. }
  640. static enum elf_reloc_type_class
  641. elf64_sparc_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  642. const asection *rel_sec ATTRIBUTE_UNUSED,
  643. const Elf_Internal_Rela *rela)
  644. {
  645. switch ((int) ELF64_R_TYPE (rela->r_info))
  646. {
  647. case R_SPARC_RELATIVE:
  648. return reloc_class_relative;
  649. case R_SPARC_JMP_SLOT:
  650. return reloc_class_plt;
  651. case R_SPARC_COPY:
  652. return reloc_class_copy;
  653. default:
  654. return reloc_class_normal;
  655. }
  656. }
  657. /* Relocations in the 64 bit SPARC ELF ABI are more complex than in
  658. standard ELF, because R_SPARC_OLO10 has secondary addend in
  659. ELF64_R_TYPE_DATA field. This structure is used to redirect the
  660. relocation handling routines. */
  661. const struct elf_size_info elf64_sparc_size_info =
  662. {
  663. sizeof (Elf64_External_Ehdr),
  664. sizeof (Elf64_External_Phdr),
  665. sizeof (Elf64_External_Shdr),
  666. sizeof (Elf64_External_Rel),
  667. sizeof (Elf64_External_Rela),
  668. sizeof (Elf64_External_Sym),
  669. sizeof (Elf64_External_Dyn),
  670. sizeof (Elf_External_Note),
  671. 4, /* hash-table entry size. */
  672. /* Internal relocations per external relocations.
  673. For link purposes we use just 1 internal per
  674. 1 external, for assembly and slurp symbol table
  675. we use 2. */
  676. 1,
  677. 64, /* arch_size. */
  678. 3, /* log_file_align. */
  679. ELFCLASS64,
  680. EV_CURRENT,
  681. bfd_elf64_write_out_phdrs,
  682. bfd_elf64_write_shdrs_and_ehdr,
  683. bfd_elf64_checksum_contents,
  684. elf64_sparc_write_relocs,
  685. bfd_elf64_swap_symbol_in,
  686. bfd_elf64_swap_symbol_out,
  687. elf64_sparc_slurp_reloc_table,
  688. bfd_elf64_slurp_symbol_table,
  689. bfd_elf64_swap_dyn_in,
  690. bfd_elf64_swap_dyn_out,
  691. bfd_elf64_swap_reloc_in,
  692. bfd_elf64_swap_reloc_out,
  693. bfd_elf64_swap_reloca_in,
  694. bfd_elf64_swap_reloca_out
  695. };
  696. #define TARGET_BIG_SYM sparc_elf64_vec
  697. #define TARGET_BIG_NAME "elf64-sparc"
  698. #define ELF_ARCH bfd_arch_sparc
  699. #define ELF_MAXPAGESIZE 0x100000
  700. #define ELF_COMMONPAGESIZE 0x2000
  701. /* This is the official ABI value. */
  702. #define ELF_MACHINE_CODE EM_SPARCV9
  703. /* This is the value that we used before the ABI was released. */
  704. #define ELF_MACHINE_ALT1 EM_OLD_SPARCV9
  705. #define elf_backend_reloc_type_class \
  706. elf64_sparc_reloc_type_class
  707. #define bfd_elf64_get_reloc_upper_bound \
  708. elf64_sparc_get_reloc_upper_bound
  709. #define bfd_elf64_get_dynamic_reloc_upper_bound \
  710. elf64_sparc_get_dynamic_reloc_upper_bound
  711. #define bfd_elf64_canonicalize_reloc \
  712. elf64_sparc_canonicalize_reloc
  713. #define bfd_elf64_canonicalize_dynamic_reloc \
  714. elf64_sparc_canonicalize_dynamic_reloc
  715. #define elf_backend_add_symbol_hook \
  716. elf64_sparc_add_symbol_hook
  717. #define elf_backend_get_symbol_type \
  718. elf64_sparc_get_symbol_type
  719. #define elf_backend_symbol_processing \
  720. elf64_sparc_symbol_processing
  721. #define elf_backend_print_symbol_all \
  722. elf64_sparc_print_symbol_all
  723. #define elf_backend_output_arch_syms \
  724. elf64_sparc_output_arch_syms
  725. #define bfd_elf64_bfd_merge_private_bfd_data \
  726. elf64_sparc_merge_private_bfd_data
  727. #define elf_backend_fake_sections \
  728. elf64_sparc_fake_sections
  729. #define elf_backend_size_info \
  730. elf64_sparc_size_info
  731. #define elf_backend_plt_sym_val \
  732. _bfd_sparc_elf_plt_sym_val
  733. #define bfd_elf64_bfd_link_hash_table_create \
  734. _bfd_sparc_elf_link_hash_table_create
  735. #define elf_info_to_howto \
  736. _bfd_sparc_elf_info_to_howto
  737. #define elf_backend_copy_indirect_symbol \
  738. _bfd_sparc_elf_copy_indirect_symbol
  739. #define bfd_elf64_bfd_reloc_type_lookup \
  740. _bfd_sparc_elf_reloc_type_lookup
  741. #define bfd_elf64_bfd_reloc_name_lookup \
  742. _bfd_sparc_elf_reloc_name_lookup
  743. #define bfd_elf64_bfd_relax_section \
  744. _bfd_sparc_elf_relax_section
  745. #define bfd_elf64_new_section_hook \
  746. _bfd_sparc_elf_new_section_hook
  747. #define elf_backend_create_dynamic_sections \
  748. _bfd_sparc_elf_create_dynamic_sections
  749. #define elf_backend_relocs_compatible \
  750. _bfd_elf_relocs_compatible
  751. #define elf_backend_check_relocs \
  752. _bfd_sparc_elf_check_relocs
  753. #define elf_backend_adjust_dynamic_symbol \
  754. _bfd_sparc_elf_adjust_dynamic_symbol
  755. #define elf_backend_omit_section_dynsym \
  756. _bfd_sparc_elf_omit_section_dynsym
  757. #define elf_backend_size_dynamic_sections \
  758. _bfd_sparc_elf_size_dynamic_sections
  759. #define elf_backend_relocate_section \
  760. _bfd_sparc_elf_relocate_section
  761. #define elf_backend_finish_dynamic_symbol \
  762. _bfd_sparc_elf_finish_dynamic_symbol
  763. #define elf_backend_finish_dynamic_sections \
  764. _bfd_sparc_elf_finish_dynamic_sections
  765. #define bfd_elf64_mkobject \
  766. _bfd_sparc_elf_mkobject
  767. #define elf_backend_object_p \
  768. _bfd_sparc_elf_object_p
  769. #define elf_backend_gc_mark_hook \
  770. _bfd_sparc_elf_gc_mark_hook
  771. #define elf_backend_gc_sweep_hook \
  772. _bfd_sparc_elf_gc_sweep_hook
  773. #define elf_backend_init_index_section \
  774. _bfd_elf_init_1_index_section
  775. #define elf_backend_can_gc_sections 1
  776. #define elf_backend_can_refcount 1
  777. #define elf_backend_want_got_plt 0
  778. #define elf_backend_plt_readonly 0
  779. #define elf_backend_want_plt_sym 1
  780. #define elf_backend_got_header_size 8
  781. #define elf_backend_rela_normal 1
  782. /* Section 5.2.4 of the ABI specifies a 256-byte boundary for the table. */
  783. #define elf_backend_plt_alignment 8
  784. #include "elf64-target.h"
  785. /* FreeBSD support */
  786. #undef TARGET_BIG_SYM
  787. #define TARGET_BIG_SYM sparc_elf64_fbsd_vec
  788. #undef TARGET_BIG_NAME
  789. #define TARGET_BIG_NAME "elf64-sparc-freebsd"
  790. #undef ELF_OSABI
  791. #define ELF_OSABI ELFOSABI_FREEBSD
  792. #undef elf64_bed
  793. #define elf64_bed elf64_sparc_fbsd_bed
  794. #include "elf64-target.h"
  795. /* Solaris 2. */
  796. #undef TARGET_BIG_SYM
  797. #define TARGET_BIG_SYM sparc_elf64_sol2_vec
  798. #undef TARGET_BIG_NAME
  799. #define TARGET_BIG_NAME "elf64-sparc-sol2"
  800. /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE
  801. objects won't be recognized. */
  802. #undef ELF_OSABI
  803. #undef elf64_bed
  804. #define elf64_bed elf64_sparc_sol2_bed
  805. /* The 64-bit static TLS arena size is rounded to the nearest 16-byte
  806. boundary. */
  807. #undef elf_backend_static_tls_alignment
  808. #define elf_backend_static_tls_alignment 16
  809. #include "elf64-target.h"