elf32-sparc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* SPARC-specific support for 32-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 "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 bfd_boolean
  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 bfd_boolean
  56. elf32_sparc_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
  57. {
  58. bfd_boolean error;
  59. unsigned long ibfd_mach;
  60. /* FIXME: This should not be static. */
  61. static unsigned long previous_ibfd_e_flags = (unsigned long) -1;
  62. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
  63. || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
  64. return TRUE;
  65. error = FALSE;
  66. ibfd_mach = bfd_get_mach (ibfd);
  67. if (bfd_mach_sparc_64bit_p (ibfd_mach))
  68. {
  69. error = TRUE;
  70. (*_bfd_error_handler)
  71. (_("%B: compiled for a 64 bit system and target is 32 bit"), ibfd);
  72. }
  73. else if ((ibfd->flags & DYNAMIC) == 0)
  74. {
  75. if (bfd_get_mach (obfd) < ibfd_mach)
  76. bfd_set_arch_mach (obfd, bfd_arch_sparc, ibfd_mach);
  77. }
  78. if (((elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA)
  79. != previous_ibfd_e_flags)
  80. && previous_ibfd_e_flags != (unsigned long) -1)
  81. {
  82. (*_bfd_error_handler)
  83. (_("%B: linking little endian files with big endian files"), ibfd);
  84. error = TRUE;
  85. }
  86. previous_ibfd_e_flags = elf_elfheader (ibfd)->e_flags & EF_SPARC_LEDATA;
  87. if (error)
  88. {
  89. bfd_set_error (bfd_error_bad_value);
  90. return FALSE;
  91. }
  92. return _bfd_sparc_elf_merge_private_bfd_data (ibfd, obfd);
  93. }
  94. /* The final processing done just before writing out the object file.
  95. We need to set the e_machine field appropriately. */
  96. static void
  97. elf32_sparc_final_write_processing (bfd *abfd,
  98. bfd_boolean linker ATTRIBUTE_UNUSED)
  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. elf_elfheader (abfd)->e_machine = EM_SPARC32PLUS;
  118. elf_elfheader (abfd)->e_flags &=~ EF_SPARC_32PLUS_MASK;
  119. elf_elfheader (abfd)->e_flags |= EF_SPARC_32PLUS | EF_SPARC_SUN_US1
  120. | EF_SPARC_SUN_US3;
  121. break;
  122. case bfd_mach_sparc_sparclite_le :
  123. elf_elfheader (abfd)->e_flags |= EF_SPARC_LEDATA;
  124. break;
  125. default :
  126. abort ();
  127. break;
  128. }
  129. }
  130. static enum elf_reloc_type_class
  131. elf32_sparc_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
  132. const asection *rel_sec ATTRIBUTE_UNUSED,
  133. const Elf_Internal_Rela *rela)
  134. {
  135. switch ((int) ELF32_R_TYPE (rela->r_info))
  136. {
  137. case R_SPARC_RELATIVE:
  138. return reloc_class_relative;
  139. case R_SPARC_JMP_SLOT:
  140. return reloc_class_plt;
  141. case R_SPARC_COPY:
  142. return reloc_class_copy;
  143. default:
  144. return reloc_class_normal;
  145. }
  146. }
  147. /* Hook called by the linker routine which adds symbols from an object
  148. file. */
  149. static bfd_boolean
  150. elf32_sparc_add_symbol_hook (bfd * abfd,
  151. struct bfd_link_info * info,
  152. Elf_Internal_Sym * sym,
  153. const char ** namep ATTRIBUTE_UNUSED,
  154. flagword * flagsp ATTRIBUTE_UNUSED,
  155. asection ** secp ATTRIBUTE_UNUSED,
  156. bfd_vma * valp ATTRIBUTE_UNUSED)
  157. {
  158. if ((ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC
  159. || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE)
  160. && (abfd->flags & DYNAMIC) == 0
  161. && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour)
  162. elf_tdata (info->output_bfd)->has_gnu_symbols = elf_gnu_symbol_any;
  163. return TRUE;
  164. }
  165. #define TARGET_BIG_SYM sparc_elf32_vec
  166. #define TARGET_BIG_NAME "elf32-sparc"
  167. #define ELF_ARCH bfd_arch_sparc
  168. #define ELF_TARGET_ID SPARC_ELF_DATA
  169. #define ELF_MACHINE_CODE EM_SPARC
  170. #define ELF_MACHINE_ALT1 EM_SPARC32PLUS
  171. #define ELF_MAXPAGESIZE 0x10000
  172. #define ELF_COMMONPAGESIZE 0x2000
  173. #define bfd_elf32_bfd_merge_private_bfd_data \
  174. elf32_sparc_merge_private_bfd_data
  175. #define elf_backend_final_write_processing \
  176. elf32_sparc_final_write_processing
  177. #define elf_backend_grok_psinfo elf32_sparc_grok_psinfo
  178. #define elf_backend_reloc_type_class elf32_sparc_reloc_type_class
  179. #define elf_info_to_howto _bfd_sparc_elf_info_to_howto
  180. #define bfd_elf32_bfd_reloc_type_lookup _bfd_sparc_elf_reloc_type_lookup
  181. #define bfd_elf32_bfd_reloc_name_lookup \
  182. _bfd_sparc_elf_reloc_name_lookup
  183. #define bfd_elf32_bfd_link_hash_table_create \
  184. _bfd_sparc_elf_link_hash_table_create
  185. #define bfd_elf32_bfd_relax_section _bfd_sparc_elf_relax_section
  186. #define bfd_elf32_new_section_hook _bfd_sparc_elf_new_section_hook
  187. #define elf_backend_copy_indirect_symbol \
  188. _bfd_sparc_elf_copy_indirect_symbol
  189. #define elf_backend_create_dynamic_sections \
  190. _bfd_sparc_elf_create_dynamic_sections
  191. #define elf_backend_check_relocs _bfd_sparc_elf_check_relocs
  192. #define elf_backend_adjust_dynamic_symbol \
  193. _bfd_sparc_elf_adjust_dynamic_symbol
  194. #define elf_backend_omit_section_dynsym _bfd_sparc_elf_omit_section_dynsym
  195. #define elf_backend_size_dynamic_sections \
  196. _bfd_sparc_elf_size_dynamic_sections
  197. #define elf_backend_relocate_section _bfd_sparc_elf_relocate_section
  198. #define elf_backend_finish_dynamic_symbol \
  199. _bfd_sparc_elf_finish_dynamic_symbol
  200. #define elf_backend_finish_dynamic_sections \
  201. _bfd_sparc_elf_finish_dynamic_sections
  202. #define bfd_elf32_mkobject _bfd_sparc_elf_mkobject
  203. #define elf_backend_object_p _bfd_sparc_elf_object_p
  204. #define elf_backend_gc_mark_hook _bfd_sparc_elf_gc_mark_hook
  205. #define elf_backend_gc_sweep_hook _bfd_sparc_elf_gc_sweep_hook
  206. #define elf_backend_plt_sym_val _bfd_sparc_elf_plt_sym_val
  207. #define elf_backend_init_index_section _bfd_elf_init_1_index_section
  208. #define elf_backend_can_gc_sections 1
  209. #define elf_backend_can_refcount 1
  210. #define elf_backend_want_got_plt 0
  211. #define elf_backend_plt_readonly 0
  212. #define elf_backend_want_plt_sym 1
  213. #define elf_backend_got_header_size 4
  214. #define elf_backend_rela_normal 1
  215. #define elf_backend_add_symbol_hook elf32_sparc_add_symbol_hook
  216. #include "elf32-target.h"
  217. /* Solaris 2. */
  218. #undef TARGET_BIG_SYM
  219. #define TARGET_BIG_SYM sparc_elf32_sol2_vec
  220. #undef TARGET_BIG_NAME
  221. #define TARGET_BIG_NAME "elf32-sparc-sol2"
  222. #undef elf32_bed
  223. #define elf32_bed elf32_sparc_sol2_bed
  224. /* The 32-bit static TLS arena size is rounded to the nearest 8-byte
  225. boundary. */
  226. #undef elf_backend_static_tls_alignment
  227. #define elf_backend_static_tls_alignment 8
  228. #include "elf32-target.h"
  229. /* A wrapper around _bfd_sparc_elf_link_hash_table_create that identifies
  230. the target system as VxWorks. */
  231. static struct bfd_link_hash_table *
  232. elf32_sparc_vxworks_link_hash_table_create (bfd *abfd)
  233. {
  234. struct bfd_link_hash_table *ret;
  235. ret = _bfd_sparc_elf_link_hash_table_create (abfd);
  236. if (ret)
  237. {
  238. struct _bfd_sparc_elf_link_hash_table *htab;
  239. htab = (struct _bfd_sparc_elf_link_hash_table *) ret;
  240. htab->is_vxworks = 1;
  241. }
  242. return ret;
  243. }
  244. /* A final_write_processing hook that does both the SPARC- and VxWorks-
  245. specific handling. */
  246. static void
  247. elf32_sparc_vxworks_final_write_processing (bfd *abfd, bfd_boolean linker)
  248. {
  249. elf32_sparc_final_write_processing (abfd, linker);
  250. elf_vxworks_final_write_processing (abfd, linker);
  251. }
  252. #undef TARGET_BIG_SYM
  253. #define TARGET_BIG_SYM sparc_elf32_vxworks_vec
  254. #undef TARGET_BIG_NAME
  255. #define TARGET_BIG_NAME "elf32-sparc-vxworks"
  256. #undef ELF_MINPAGESIZE
  257. #define ELF_MINPAGESIZE 0x1000
  258. #undef bfd_elf32_bfd_link_hash_table_create
  259. #define bfd_elf32_bfd_link_hash_table_create \
  260. elf32_sparc_vxworks_link_hash_table_create
  261. #undef elf_backend_want_got_plt
  262. #define elf_backend_want_got_plt 1
  263. #undef elf_backend_plt_readonly
  264. #define elf_backend_plt_readonly 1
  265. #undef elf_backend_got_header_size
  266. #define elf_backend_got_header_size 12
  267. #undef elf_backend_add_symbol_hook
  268. #define elf_backend_add_symbol_hook \
  269. elf_vxworks_add_symbol_hook
  270. #undef elf_backend_link_output_symbol_hook
  271. #define elf_backend_link_output_symbol_hook \
  272. elf_vxworks_link_output_symbol_hook
  273. #undef elf_backend_emit_relocs
  274. #define elf_backend_emit_relocs \
  275. elf_vxworks_emit_relocs
  276. #undef elf_backend_final_write_processing
  277. #define elf_backend_final_write_processing \
  278. elf32_sparc_vxworks_final_write_processing
  279. #undef elf_backend_static_tls_alignment
  280. #undef elf32_bed
  281. #define elf32_bed sparc_elf_vxworks_bed
  282. #include "elf32-target.h"