elf-vxworks.c 9.4 KB

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