elf-vxworks.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* VxWorks support for ELF
  2. Copyright (C) 2005-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., 59 Temple Place - Suite 330, Boston,
  15. MA 02111-1307, USA. */
  16. /* This file provides routines used by all VxWorks targets. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "elf-bfd.h"
  21. #include "elf-vxworks.h"
  22. #include "elf/vxworks.h"
  23. /* Return true if symbol NAME, as defined by ABFD, is one of the special
  24. __GOTT_BASE__ or __GOTT_INDEX__ symbols. */
  25. static bfd_boolean
  26. elf_vxworks_gott_symbol_p (bfd *abfd, const char *name)
  27. {
  28. char leading;
  29. leading = bfd_get_symbol_leading_char (abfd);
  30. if (leading)
  31. {
  32. if (*name != leading)
  33. return FALSE;
  34. name++;
  35. }
  36. return (strcmp (name, "__GOTT_BASE__") == 0
  37. || strcmp (name, "__GOTT_INDEX__") == 0);
  38. }
  39. /* Tweak magic VxWorks symbols as they are loaded. */
  40. bfd_boolean
  41. elf_vxworks_add_symbol_hook (bfd *abfd,
  42. struct bfd_link_info *info,
  43. Elf_Internal_Sym *sym,
  44. const char **namep,
  45. flagword *flagsp,
  46. asection **secp ATTRIBUTE_UNUSED,
  47. bfd_vma *valp ATTRIBUTE_UNUSED)
  48. {
  49. /* Ideally these "magic" symbols would be exported by libc.so.1
  50. which would be found via a DT_NEEDED tag, and then handled
  51. specially by the linker at runtime. Except shared libraries
  52. don't even link to libc.so.1 by default...
  53. If the symbol is imported from, or will be put in a shared library,
  54. give the symbol weak binding to get the desired samantics.
  55. This transformation will be undone in
  56. elf_i386_vxworks_link_output_symbol_hook. */
  57. if ((bfd_link_pic (info) || abfd->flags & DYNAMIC)
  58. && elf_vxworks_gott_symbol_p (abfd, *namep))
  59. {
  60. sym->st_info = ELF_ST_INFO (STB_WEAK, ELF_ST_TYPE (sym->st_info));
  61. *flagsp |= BSF_WEAK;
  62. }
  63. return TRUE;
  64. }
  65. /* Perform VxWorks-specific handling of the create_dynamic_sections hook.
  66. When creating an executable, set *SRELPLT2_OUT to the .rel(a).plt.unloaded
  67. section. */
  68. bfd_boolean
  69. elf_vxworks_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info,
  70. asection **srelplt2_out)
  71. {
  72. struct elf_link_hash_table *htab;
  73. const struct elf_backend_data *bed;
  74. asection *s;
  75. htab = elf_hash_table (info);
  76. bed = get_elf_backend_data (dynobj);
  77. if (!bfd_link_pic (info))
  78. {
  79. s = bfd_make_section_anyway_with_flags (dynobj,
  80. bed->default_use_rela_p
  81. ? ".rela.plt.unloaded"
  82. : ".rel.plt.unloaded",
  83. SEC_HAS_CONTENTS | SEC_IN_MEMORY
  84. | SEC_READONLY
  85. | SEC_LINKER_CREATED);
  86. if (s == NULL
  87. || !bfd_set_section_alignment (dynobj, s, bed->s->log_file_align))
  88. return FALSE;
  89. *srelplt2_out = s;
  90. }
  91. /* Mark the GOT and PLT symbols as having relocations; they might
  92. not, but we won't know for sure until we build the GOT in
  93. finish_dynamic_symbol. Also make sure that the GOT symbol
  94. is entered into the dynamic symbol table; the loader uses it
  95. to initialize __GOTT_BASE__[__GOTT_INDEX__]. */
  96. if (htab->hgot)
  97. {
  98. htab->hgot->indx = -2;
  99. htab->hgot->other &= ~ELF_ST_VISIBILITY (-1);
  100. htab->hgot->forced_local = 0;
  101. if (!bfd_elf_link_record_dynamic_symbol (info, htab->hgot))
  102. return FALSE;
  103. }
  104. if (htab->hplt)
  105. {
  106. htab->hplt->indx = -2;
  107. htab->hplt->type = STT_FUNC;
  108. }
  109. return TRUE;
  110. }
  111. /* Tweak magic VxWorks symbols as they are written to the output file. */
  112. int
  113. elf_vxworks_link_output_symbol_hook (struct bfd_link_info *info
  114. ATTRIBUTE_UNUSED,
  115. const char *name,
  116. Elf_Internal_Sym *sym,
  117. asection *input_sec ATTRIBUTE_UNUSED,
  118. struct elf_link_hash_entry *h)
  119. {
  120. /* Reverse the effects of the hack in elf_vxworks_add_symbol_hook. */
  121. if (h
  122. && h->root.type == bfd_link_hash_undefweak
  123. && elf_vxworks_gott_symbol_p (h->root.u.undef.abfd, name))
  124. sym->st_info = ELF_ST_INFO (STB_GLOBAL, ELF_ST_TYPE (sym->st_info));
  125. return 1;
  126. }
  127. /* Copy relocations into the output file. Fixes up relocations against PLT
  128. entries, then calls the generic routine. */
  129. bfd_boolean
  130. elf_vxworks_emit_relocs (bfd *output_bfd,
  131. asection *input_section,
  132. Elf_Internal_Shdr *input_rel_hdr,
  133. Elf_Internal_Rela *internal_relocs,
  134. struct elf_link_hash_entry **rel_hash)
  135. {
  136. const struct elf_backend_data *bed;
  137. int j;
  138. bed = get_elf_backend_data (output_bfd);
  139. if (output_bfd->flags & (DYNAMIC|EXEC_P))
  140. {
  141. Elf_Internal_Rela *irela;
  142. Elf_Internal_Rela *irelaend;
  143. struct elf_link_hash_entry **hash_ptr;
  144. for (irela = internal_relocs,
  145. irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr)
  146. * bed->s->int_rels_per_ext_rel),
  147. hash_ptr = rel_hash;
  148. irela < irelaend;
  149. irela += bed->s->int_rels_per_ext_rel,
  150. hash_ptr++)
  151. {
  152. if (*hash_ptr
  153. && (*hash_ptr)->def_dynamic
  154. && !(*hash_ptr)->def_regular
  155. && ((*hash_ptr)->root.type == bfd_link_hash_defined
  156. || (*hash_ptr)->root.type == bfd_link_hash_defweak)
  157. && (*hash_ptr)->root.u.def.section->output_section != NULL)
  158. {
  159. /* This is a relocation from an executable or shared
  160. library against a symbol in a different shared
  161. library. We are creating a definition in the output
  162. file but it does not come from any of our normal (.o)
  163. files. ie. a PLT stub. Normally this would be a
  164. relocation against against SHN_UNDEF with the VMA of
  165. the PLT stub. This upsets the VxWorks loader.
  166. Convert it to a section-relative relocation. This
  167. gets some other symbols (for instance .dynbss), but
  168. is conservatively correct. */
  169. for (j = 0; j < bed->s->int_rels_per_ext_rel; j++)
  170. {
  171. asection *sec = (*hash_ptr)->root.u.def.section;
  172. int this_idx = sec->output_section->target_index;
  173. irela[j].r_info
  174. = ELF32_R_INFO (this_idx, ELF32_R_TYPE (irela[j].r_info));
  175. irela[j].r_addend += (*hash_ptr)->root.u.def.value;
  176. irela[j].r_addend += sec->output_offset;
  177. }
  178. /* Stop the generic routine adjusting this entry. */
  179. *hash_ptr = NULL;
  180. }
  181. }
  182. }
  183. return _bfd_elf_link_output_relocs (output_bfd, input_section,
  184. input_rel_hdr, internal_relocs,
  185. rel_hash);
  186. }
  187. /* Set the sh_link and sh_info fields on the static plt relocation secton. */
  188. void
  189. elf_vxworks_final_write_processing (bfd *abfd,
  190. bfd_boolean linker ATTRIBUTE_UNUSED)
  191. {
  192. asection * sec;
  193. struct bfd_elf_section_data *d;
  194. sec = bfd_get_section_by_name (abfd, ".rel.plt.unloaded");
  195. if (!sec)
  196. sec = bfd_get_section_by_name (abfd, ".rela.plt.unloaded");
  197. if (!sec)
  198. return;
  199. d = elf_section_data (sec);
  200. d->this_hdr.sh_link = elf_onesymtab (abfd);
  201. sec = bfd_get_section_by_name (abfd, ".plt");
  202. if (sec)
  203. d->this_hdr.sh_info = elf_section_data (sec)->this_idx;
  204. }
  205. /* Add the dynamic entries required by VxWorks. These point to the
  206. tls sections. */
  207. bfd_boolean
  208. elf_vxworks_add_dynamic_entries (bfd *output_bfd, struct bfd_link_info *info)
  209. {
  210. if (bfd_get_section_by_name (output_bfd, ".tls_data"))
  211. {
  212. if (!_bfd_elf_add_dynamic_entry (info, DT_VX_WRS_TLS_DATA_START, 0)
  213. || !_bfd_elf_add_dynamic_entry (info, DT_VX_WRS_TLS_DATA_SIZE, 0)
  214. || !_bfd_elf_add_dynamic_entry (info, DT_VX_WRS_TLS_DATA_ALIGN, 0))
  215. return FALSE;
  216. }
  217. if (bfd_get_section_by_name (output_bfd, ".tls_vars"))
  218. {
  219. if (!_bfd_elf_add_dynamic_entry (info, DT_VX_WRS_TLS_VARS_START, 0)
  220. || !_bfd_elf_add_dynamic_entry (info, DT_VX_WRS_TLS_VARS_SIZE, 0))
  221. return FALSE;
  222. }
  223. return TRUE;
  224. }
  225. /* If *DYN is one of the VxWorks-specific dynamic entries, then fill
  226. in the value now and return TRUE. Otherwise return FALSE. */
  227. bfd_boolean
  228. elf_vxworks_finish_dynamic_entry (bfd *output_bfd, Elf_Internal_Dyn *dyn)
  229. {
  230. asection *sec;
  231. switch (dyn->d_tag)
  232. {
  233. default:
  234. return FALSE;
  235. case DT_VX_WRS_TLS_DATA_START:
  236. sec = bfd_get_section_by_name (output_bfd, ".tls_data");
  237. dyn->d_un.d_ptr = sec->vma;
  238. break;
  239. case DT_VX_WRS_TLS_DATA_SIZE:
  240. sec = bfd_get_section_by_name (output_bfd, ".tls_data");
  241. dyn->d_un.d_val = sec->size;
  242. break;
  243. case DT_VX_WRS_TLS_DATA_ALIGN:
  244. sec = bfd_get_section_by_name (output_bfd, ".tls_data");
  245. dyn->d_un.d_val
  246. = (bfd_size_type)1 << bfd_get_section_alignment (output_bfd,
  247. sec);
  248. break;
  249. case DT_VX_WRS_TLS_VARS_START:
  250. sec = bfd_get_section_by_name (output_bfd, ".tls_vars");
  251. dyn->d_un.d_ptr = sec->vma;
  252. break;
  253. case DT_VX_WRS_TLS_VARS_SIZE:
  254. sec = bfd_get_section_by_name (output_bfd, ".tls_vars");
  255. dyn->d_un.d_val = sec->size;
  256. break;
  257. }
  258. return TRUE;
  259. }