elf32-sparc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /* SPARC-specific support for 32-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 "bfd.h"
  18. #include "bfdlink.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. #include "elf-vxworks.h"
  25. /* Support for core dump NOTE sections. */
  26. static bool
  27. elf32_sparc_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
  28. {
  29. switch (note->descsz)
  30. {
  31. default:
  32. return false;
  33. case 260: /* Solaris prpsinfo_t. */
  34. elf_tdata (abfd)->core->program
  35. = _bfd_elfcore_strndup (abfd, note->descdata + 84, 16);
  36. elf_tdata (abfd)->core->command
  37. = _bfd_elfcore_strndup (abfd, note->descdata + 100, 80);
  38. break;
  39. case 336: /* Solaris psinfo_t. */
  40. elf_tdata (abfd)->core->program
  41. = _bfd_elfcore_strndup (abfd, note->descdata + 88, 16);
  42. elf_tdata (abfd)->core->command
  43. = _bfd_elfcore_strndup (abfd, note->descdata + 104, 80);
  44. break;
  45. }
  46. return true;
  47. }
  48. /* Functions for dealing with the e_flags field.
  49. We don't define set_private_flags or copy_private_bfd_data because
  50. the only currently defined values are based on the bfd mach number,
  51. so we use the latter instead and defer setting e_flags until the
  52. file is written out. */
  53. /* Merge backend specific data from an object file to the output
  54. object file when linking. */
  55. static bool
  56. elf32_sparc_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  57. {
  58. bfd *obfd = info->output_bfd;
  59. bool error;
  60. unsigned long ibfd_mach;
  61. /* FIXME: This should not be static. */
  62. static unsigned long previous_ibfd_e_flags = (unsigned long) -1;
  63. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  64. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  65. return true;
  66. error = false;
  67. ibfd_mach = bfd_get_mach (ibfd);
  68. if (bfd_mach_sparc_64bit_p (ibfd_mach))
  69. {
  70. error = true;
  71. _bfd_error_handler
  72. (_("%pB: compiled for a 64 bit system and target is 32 bit"), ibfd);
  73. }
  74. else if ((ibfd->flags & DYNAMIC) == 0)
  75. {
  76. if (bfd_get_mach (obfd) < ibfd_mach)
  77. bfd_set_arch_mach (obfd, bfd_arch_sparc, ibfd_mach);
  78. }
  79. if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA)
  80. != previous_ibfd_e_flags)
  81. && previous_ibfd_e_flags != (unsigned long) -1)
  82. {
  83. _bfd_error_handler
  84. (_("%pB: linking little endian files with big endian files"), ibfd);
  85. error = true;
  86. }
  87. previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA;
  88. if (error)
  89. {
  90. bfd_set_error (bfd_error_bad_value);
  91. return false;
  92. }
  93. return _bfd_sparc_elf_merge_private_bfd_data (ibfd, info);
  94. }
  95. /* The final processing done just before writing out the object file.
  96. We need to set the e_machine field appropriately. */
  97. static void
  98. sparc_final_write_processing (bfd *abfd)
  99. {
  100. switch (bfd_get_mach (abfd))
  101. {
  102. case bfd_mach_sparc:
  103. case bfd_mach_sparc_sparclet:
  104. case bfd_mach_sparc_sparclite:
  105. break; /* nothing to do */
  106. case bfd_mach_sparc_v8plus:
  107. elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
  108. elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
  109. elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS;
  110. break;
  111. case bfd_mach_sparc_v8plusa:
  112. elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
  113. elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
  114. elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1;
  115. break;
  116. case bfd_mach_sparc_v8plusb:
  117. case bfd_mach_sparc_v8plusc:
  118. case bfd_mach_sparc_v8plusd:
  119. case bfd_mach_sparc_v8pluse:
  120. case bfd_mach_sparc_v8plusv:
  121. case bfd_mach_sparc_v8plusm:
  122. case bfd_mach_sparc_v8plusm8:
  123. elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
  124. elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
  125. elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1
  126. | EF_SPARC_SUN_US3;
  127. break;
  128. case bfd_mach_sparc_sparclite_le:
  129. elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA;
  130. break;
  131. case 0: /* A non-sparc architecture - ignore. */
  132. break;
  133. default:
  134. _bfd_error_handler
  135. (_("%pB: unhandled sparc machine value '%lu' detected during write processing"),
  136. abfd, (long) bfd_get_mach (abfd));
  137. break;
  138. }
  139. }
  140. static bool
  141. elf32_sparc_final_write_processing (bfd *abfd)
  142. {
  143. sparc_final_write_processing (abfd);
  144. return _bfd_elf_final_write_processing (abfd);
  145. }
  146. /* Used to decide how to sort relocs in an optimal manner for the
  147. dynamic linker, before writing them out. */
  148. static enum elf_reloc_type_class
  149. elf32_sparc_reloc_type_class (const struct bfd_link_info *info,
  150. const asection *rel_sec ATTRIBUTE_UNUSED,
  151. const Elf_Internal_Rela *rela)
  152. {
  153. bfd *abfd = info->output_bfd;
  154. const struct elf_backend_data *bed = get_elf_backend_data (abfd);
  155. struct _bfd_sparc_elf_link_hash_table *htab
  156. = _bfd_sparc_elf_hash_table (info);
  157. BFD_ASSERT (htab != NULL);
  158. if (htab->elf.dynsym != NULL
  159. && htab->elf.dynsym->contents != NULL)
  160. {
  161. /* Check relocation against STT_GNU_IFUNC symbol if there are
  162. dynamic symbols. */
  163. unsigned long r_symndx = htab->r_symndx (rela->r_info);
  164. if (r_symndx != STN_UNDEF)
  165. {
  166. Elf_Internal_Sym sym;
  167. if (!bed->s->swap_symbol_in (abfd,
  168. (htab->elf.dynsym->contents
  169. + r_symndx * bed->s->sizeof_sym),
  170. 0, &sym))
  171. abort ();
  172. if (ELF_ST_TYPE (sym.st_info) == STT_GNU_IFUNC)
  173. return reloc_class_ifunc;
  174. }
  175. }
  176. switch ((int) ELF32_R_TYPE (rela->r_info))
  177. {
  178. case R_SPARC_IRELATIVE:
  179. return reloc_class_ifunc;
  180. case R_SPARC_RELATIVE:
  181. return reloc_class_relative;
  182. case R_SPARC_JMP_SLOT:
  183. return reloc_class_plt;
  184. case R_SPARC_COPY:
  185. return reloc_class_copy;
  186. default:
  187. return reloc_class_normal;
  188. }
  189. }
  190. #define TARGET_BIG_SYM sparc_elf32_vec
  191. #define TARGET_BIG_NAME "elf32-sparc"
  192. #define ELF_ARCH bfd_arch_sparc
  193. #define ELF_TARGET_ID SPARC_ELF_DATA
  194. #define ELF_MACHINE_CODE EM_SPARC
  195. #define ELF_MACHINE_ALT1 EM_SPARC32PLUS
  196. #define ELF_MAXPAGESIZE 0x10000
  197. #define ELF_COMMONPAGESIZE 0x2000
  198. #define bfd_elf32_bfd_merge_private_bfd_data \
  199. elf32_sparc_merge_private_bfd_data
  200. #define elf_backend_final_write_processing \
  201. elf32_sparc_final_write_processing
  202. #define elf_backend_grok_psinfo elf32_sparc_grok_psinfo
  203. #define elf_backend_reloc_type_class elf32_sparc_reloc_type_class
  204. #define elf_info_to_howto _bfd_sparc_elf_info_to_howto
  205. #define bfd_elf32_bfd_reloc_type_lookup _bfd_sparc_elf_reloc_type_lookup
  206. #define bfd_elf32_bfd_reloc_name_lookup \
  207. _bfd_sparc_elf_reloc_name_lookup
  208. #define bfd_elf32_bfd_link_hash_table_create \
  209. _bfd_sparc_elf_link_hash_table_create
  210. #define bfd_elf32_bfd_relax_section _bfd_sparc_elf_relax_section
  211. #define bfd_elf32_new_section_hook _bfd_sparc_elf_new_section_hook
  212. #define elf_backend_copy_indirect_symbol \
  213. _bfd_sparc_elf_copy_indirect_symbol
  214. #define elf_backend_create_dynamic_sections \
  215. _bfd_sparc_elf_create_dynamic_sections
  216. #define elf_backend_check_relocs _bfd_sparc_elf_check_relocs
  217. #define elf_backend_adjust_dynamic_symbol \
  218. _bfd_sparc_elf_adjust_dynamic_symbol
  219. #define elf_backend_omit_section_dynsym _bfd_sparc_elf_omit_section_dynsym
  220. #define elf_backend_size_dynamic_sections \
  221. _bfd_sparc_elf_size_dynamic_sections
  222. #define elf_backend_relocate_section _bfd_sparc_elf_relocate_section
  223. #define elf_backend_finish_dynamic_symbol \
  224. _bfd_sparc_elf_finish_dynamic_symbol
  225. #define elf_backend_finish_dynamic_sections \
  226. _bfd_sparc_elf_finish_dynamic_sections
  227. #define bfd_elf32_mkobject _bfd_sparc_elf_mkobject
  228. #define elf_backend_object_p _bfd_sparc_elf_object_p
  229. #define elf_backend_gc_mark_hook _bfd_sparc_elf_gc_mark_hook
  230. #define elf_backend_plt_sym_val _bfd_sparc_elf_plt_sym_val
  231. #define elf_backend_init_index_section _bfd_elf_init_1_index_section
  232. #define elf_backend_fixup_symbol _bfd_sparc_elf_fixup_symbol
  233. #define elf_backend_can_gc_sections 1
  234. #define elf_backend_can_refcount 1
  235. #define elf_backend_want_got_plt 0
  236. #define elf_backend_plt_readonly 0
  237. #define elf_backend_want_plt_sym 1
  238. #define elf_backend_got_header_size 4
  239. #define elf_backend_want_dynrelro 1
  240. #define elf_backend_rela_normal 1
  241. #define elf_backend_linux_prpsinfo32_ugid16 true
  242. #include "elf32-target.h"
  243. /* Solaris 2. */
  244. #undef TARGET_BIG_SYM
  245. #define TARGET_BIG_SYM sparc_elf32_sol2_vec
  246. #undef TARGET_BIG_NAME
  247. #define TARGET_BIG_NAME "elf32-sparc-sol2"
  248. #undef elf32_bed
  249. #define elf32_bed elf32_sparc_sol2_bed
  250. /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
  251. boundary. */
  252. #undef elf_backend_static_tls_alignment
  253. #define elf_backend_static_tls_alignment 8
  254. #undef elf_backend_strtab_flags
  255. #define elf_backend_strtab_flags SHF_STRINGS
  256. static bool
  257. elf32_sparc_copy_solaris_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED,
  258. bfd *obfd ATTRIBUTE_UNUSED,
  259. const Elf_Internal_Shdr *isection ATTRIBUTE_UNUSED,
  260. Elf_Internal_Shdr *osection ATTRIBUTE_UNUSED)
  261. {
  262. /* PR 19938: FIXME: Need to add code for setting the sh_info
  263. and sh_link fields of Solaris specific section types. */
  264. return false;
  265. }
  266. #undef elf_backend_copy_special_section_fields
  267. #define elf_backend_copy_special_section_fields elf32_sparc_copy_solaris_special_section_fields
  268. #include "elf32-target.h"
  269. /* A final_write_processing hook that does both the SPARC- and VxWorks-
  270. specific handling. */
  271. static bool
  272. elf32_sparc_vxworks_final_write_processing (bfd *abfd)
  273. {
  274. sparc_final_write_processing (abfd);
  275. return elf_vxworks_final_write_processing (abfd);
  276. }
  277. #undef TARGET_BIG_SYM
  278. #define TARGET_BIG_SYM sparc_elf32_vxworks_vec
  279. #undef TARGET_BIG_NAME
  280. #define TARGET_BIG_NAME "elf32-sparc-vxworks"
  281. #undef ELF_MINPAGESIZE
  282. #define ELF_MINPAGESIZE 0x1000
  283. #undef ELF_TARGET_OS
  284. #define ELF_TARGET_OS is_vxworks
  285. #undef elf_backend_want_got_plt
  286. #define elf_backend_want_got_plt 1
  287. #undef elf_backend_plt_readonly
  288. #define elf_backend_plt_readonly 1
  289. #undef elf_backend_got_header_size
  290. #define elf_backend_got_header_size 12
  291. #undef elf_backend_dtrel_excludes_plt
  292. #define elf_backend_dtrel_excludes_plt 1
  293. #undef elf_backend_add_symbol_hook
  294. #define elf_backend_add_symbol_hook \
  295. elf_vxworks_add_symbol_hook
  296. #undef elf_backend_link_output_symbol_hook
  297. #define elf_backend_link_output_symbol_hook \
  298. elf_vxworks_link_output_symbol_hook
  299. #undef elf_backend_emit_relocs
  300. #define elf_backend_emit_relocs \
  301. elf_vxworks_emit_relocs
  302. #undef elf_backend_final_write_processing
  303. #define elf_backend_final_write_processing \
  304. elf32_sparc_vxworks_final_write_processing
  305. #undef elf_backend_static_tls_alignment
  306. #undef elf_backend_strtab_flags
  307. #undef elf_backend_copy_special_section_fields
  308. #undef elf32_bed
  309. #define elf32_bed sparc_elf_vxworks_bed
  310. #include "elf32-target.h"