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