elf64-sparc.c 28 KB

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