1 1.1 christos /* ELF executable support for BFD. 2 1.1 christos 3 1.14 christos Copyright (C) 1993-2025 Free Software Foundation, Inc. 4 1.1 christos 5 1.1 christos This file is part of BFD, the Binary File Descriptor library. 6 1.1 christos 7 1.1 christos This program is free software; you can redistribute it and/or modify 8 1.1 christos it under the terms of the GNU General Public License as published by 9 1.1 christos the Free Software Foundation; either version 3 of the License, or 10 1.1 christos (at your option) any later version. 11 1.1 christos 12 1.1 christos This program is distributed in the hope that it will be useful, 13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 1.1 christos GNU General Public License for more details. 16 1.1 christos 17 1.1 christos You should have received a copy of the GNU General Public License 18 1.1 christos along with this program; if not, write to the Free Software 19 1.1 christos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 20 1.1 christos MA 02110-1301, USA. */ 21 1.1 christos 22 1.1 christos 23 1.1 christos /* 24 1.1 christos SECTION 25 1.1 christos ELF backends 26 1.1 christos 27 1.1 christos BFD support for ELF formats is being worked on. 28 1.1 christos Currently, the best supported back ends are for sparc and i386 29 1.1 christos (running svr4 or Solaris 2). 30 1.1 christos 31 1.1 christos Documentation of the internals of the support code still needs 32 1.1 christos to be written. The code is changing quickly enough that we 33 1.1 christos haven't bothered yet. */ 34 1.1 christos 35 1.1 christos /* For sparc64-cross-sparc32. */ 36 1.1 christos #define _SYSCALL32 37 1.1 christos #include "sysdep.h" 38 1.10 christos #include <limits.h> 39 1.1 christos #include "bfd.h" 40 1.1 christos #include "bfdlink.h" 41 1.1 christos #include "libbfd.h" 42 1.1 christos #define ARCH_SIZE 0 43 1.1 christos #include "elf-bfd.h" 44 1.1 christos #include "libiberty.h" 45 1.1 christos #include "safe-ctype.h" 46 1.5 christos #include "elf-linux-core.h" 47 1.1 christos 48 1.1 christos #ifdef CORE_HEADER 49 1.1 christos #include CORE_HEADER 50 1.1 christos #endif 51 1.1 christos 52 1.1 christos static int elf_sort_sections (const void *, const void *); 53 1.12 christos static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); 54 1.12 christos static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int, 55 1.12 christos struct bfd_link_info *); 56 1.12 christos static bool elf_parse_notes (bfd *abfd, char *buf, size_t size, 57 1.12 christos file_ptr offset, size_t align); 58 1.1 christos 59 1.1 christos /* Swap version information in and out. The version information is 60 1.1 christos currently size independent. If that ever changes, this code will 61 1.1 christos need to move into elfcode.h. */ 62 1.1 christos 63 1.1 christos /* Swap in a Verdef structure. */ 64 1.1 christos 65 1.1 christos void 66 1.1 christos _bfd_elf_swap_verdef_in (bfd *abfd, 67 1.1 christos const Elf_External_Verdef *src, 68 1.1 christos Elf_Internal_Verdef *dst) 69 1.1 christos { 70 1.1 christos dst->vd_version = H_GET_16 (abfd, src->vd_version); 71 1.1 christos dst->vd_flags = H_GET_16 (abfd, src->vd_flags); 72 1.1 christos dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); 73 1.1 christos dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); 74 1.1 christos dst->vd_hash = H_GET_32 (abfd, src->vd_hash); 75 1.1 christos dst->vd_aux = H_GET_32 (abfd, src->vd_aux); 76 1.1 christos dst->vd_next = H_GET_32 (abfd, src->vd_next); 77 1.1 christos } 78 1.1 christos 79 1.1 christos /* Swap out a Verdef structure. */ 80 1.1 christos 81 1.1 christos void 82 1.1 christos _bfd_elf_swap_verdef_out (bfd *abfd, 83 1.1 christos const Elf_Internal_Verdef *src, 84 1.1 christos Elf_External_Verdef *dst) 85 1.1 christos { 86 1.1 christos H_PUT_16 (abfd, src->vd_version, dst->vd_version); 87 1.1 christos H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); 88 1.1 christos H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); 89 1.1 christos H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); 90 1.1 christos H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); 91 1.1 christos H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); 92 1.1 christos H_PUT_32 (abfd, src->vd_next, dst->vd_next); 93 1.1 christos } 94 1.1 christos 95 1.1 christos /* Swap in a Verdaux structure. */ 96 1.1 christos 97 1.1 christos void 98 1.1 christos _bfd_elf_swap_verdaux_in (bfd *abfd, 99 1.1 christos const Elf_External_Verdaux *src, 100 1.1 christos Elf_Internal_Verdaux *dst) 101 1.1 christos { 102 1.1 christos dst->vda_name = H_GET_32 (abfd, src->vda_name); 103 1.1 christos dst->vda_next = H_GET_32 (abfd, src->vda_next); 104 1.1 christos } 105 1.1 christos 106 1.1 christos /* Swap out a Verdaux structure. */ 107 1.1 christos 108 1.1 christos void 109 1.1 christos _bfd_elf_swap_verdaux_out (bfd *abfd, 110 1.1 christos const Elf_Internal_Verdaux *src, 111 1.1 christos Elf_External_Verdaux *dst) 112 1.1 christos { 113 1.1 christos H_PUT_32 (abfd, src->vda_name, dst->vda_name); 114 1.1 christos H_PUT_32 (abfd, src->vda_next, dst->vda_next); 115 1.1 christos } 116 1.1 christos 117 1.1 christos /* Swap in a Verneed structure. */ 118 1.1 christos 119 1.1 christos void 120 1.1 christos _bfd_elf_swap_verneed_in (bfd *abfd, 121 1.1 christos const Elf_External_Verneed *src, 122 1.1 christos Elf_Internal_Verneed *dst) 123 1.1 christos { 124 1.1 christos dst->vn_version = H_GET_16 (abfd, src->vn_version); 125 1.1 christos dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); 126 1.1 christos dst->vn_file = H_GET_32 (abfd, src->vn_file); 127 1.1 christos dst->vn_aux = H_GET_32 (abfd, src->vn_aux); 128 1.1 christos dst->vn_next = H_GET_32 (abfd, src->vn_next); 129 1.1 christos } 130 1.1 christos 131 1.1 christos /* Swap out a Verneed structure. */ 132 1.1 christos 133 1.1 christos void 134 1.1 christos _bfd_elf_swap_verneed_out (bfd *abfd, 135 1.1 christos const Elf_Internal_Verneed *src, 136 1.1 christos Elf_External_Verneed *dst) 137 1.1 christos { 138 1.1 christos H_PUT_16 (abfd, src->vn_version, dst->vn_version); 139 1.1 christos H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); 140 1.1 christos H_PUT_32 (abfd, src->vn_file, dst->vn_file); 141 1.1 christos H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); 142 1.1 christos H_PUT_32 (abfd, src->vn_next, dst->vn_next); 143 1.1 christos } 144 1.1 christos 145 1.1 christos /* Swap in a Vernaux structure. */ 146 1.1 christos 147 1.1 christos void 148 1.1 christos _bfd_elf_swap_vernaux_in (bfd *abfd, 149 1.1 christos const Elf_External_Vernaux *src, 150 1.1 christos Elf_Internal_Vernaux *dst) 151 1.1 christos { 152 1.1 christos dst->vna_hash = H_GET_32 (abfd, src->vna_hash); 153 1.1 christos dst->vna_flags = H_GET_16 (abfd, src->vna_flags); 154 1.1 christos dst->vna_other = H_GET_16 (abfd, src->vna_other); 155 1.1 christos dst->vna_name = H_GET_32 (abfd, src->vna_name); 156 1.1 christos dst->vna_next = H_GET_32 (abfd, src->vna_next); 157 1.1 christos } 158 1.1 christos 159 1.1 christos /* Swap out a Vernaux structure. */ 160 1.1 christos 161 1.1 christos void 162 1.1 christos _bfd_elf_swap_vernaux_out (bfd *abfd, 163 1.1 christos const Elf_Internal_Vernaux *src, 164 1.1 christos Elf_External_Vernaux *dst) 165 1.1 christos { 166 1.1 christos H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); 167 1.1 christos H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); 168 1.1 christos H_PUT_16 (abfd, src->vna_other, dst->vna_other); 169 1.1 christos H_PUT_32 (abfd, src->vna_name, dst->vna_name); 170 1.1 christos H_PUT_32 (abfd, src->vna_next, dst->vna_next); 171 1.1 christos } 172 1.1 christos 173 1.1 christos /* Swap in a Versym structure. */ 174 1.1 christos 175 1.1 christos void 176 1.1 christos _bfd_elf_swap_versym_in (bfd *abfd, 177 1.1 christos const Elf_External_Versym *src, 178 1.1 christos Elf_Internal_Versym *dst) 179 1.1 christos { 180 1.1 christos dst->vs_vers = H_GET_16 (abfd, src->vs_vers); 181 1.1 christos } 182 1.1 christos 183 1.1 christos /* Swap out a Versym structure. */ 184 1.1 christos 185 1.1 christos void 186 1.1 christos _bfd_elf_swap_versym_out (bfd *abfd, 187 1.1 christos const Elf_Internal_Versym *src, 188 1.1 christos Elf_External_Versym *dst) 189 1.1 christos { 190 1.1 christos H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); 191 1.1 christos } 192 1.1 christos 193 1.1 christos /* Standard ELF hash function. Do not change this function; you will 194 1.1 christos cause invalid hash tables to be generated. */ 195 1.1 christos 196 1.1 christos unsigned long 197 1.1 christos bfd_elf_hash (const char *namearg) 198 1.1 christos { 199 1.13 christos uint32_t h = 0; 200 1.1 christos 201 1.13 christos for (const unsigned char *name = (const unsigned char *) namearg; 202 1.13 christos *name; name++) 203 1.1 christos { 204 1.13 christos h = (h << 4) + *name; 205 1.13 christos h ^= (h >> 24) & 0xf0; 206 1.1 christos } 207 1.13 christos return h & 0x0fffffff; 208 1.1 christos } 209 1.1 christos 210 1.1 christos /* DT_GNU_HASH hash function. Do not change this function; you will 211 1.1 christos cause invalid hash tables to be generated. */ 212 1.1 christos 213 1.1 christos unsigned long 214 1.1 christos bfd_elf_gnu_hash (const char *namearg) 215 1.1 christos { 216 1.13 christos uint32_t h = 5381; 217 1.13 christos 218 1.13 christos for (const unsigned char *name = (const unsigned char *) namearg; 219 1.13 christos *name; name++) 220 1.13 christos h = (h << 5) + h + *name; 221 1.13 christos return h; 222 1.1 christos } 223 1.1 christos 224 1.1 christos /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with 225 1.14 christos the object_id field of an elf_obj_tdata field set. */ 226 1.12 christos bool 227 1.1 christos bfd_elf_allocate_object (bfd *abfd, 228 1.14 christos size_t object_size) 229 1.1 christos { 230 1.1 christos BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); 231 1.1 christos abfd->tdata.any = bfd_zalloc (abfd, object_size); 232 1.1 christos if (abfd->tdata.any == NULL) 233 1.12 christos return false; 234 1.1 christos 235 1.14 christos elf_object_id (abfd) = get_elf_backend_data (abfd)->target_id; 236 1.3 christos if (abfd->direction != read_direction) 237 1.3 christos { 238 1.3 christos struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); 239 1.3 christos if (o == NULL) 240 1.12 christos return false; 241 1.3 christos elf_tdata (abfd)->o = o; 242 1.3 christos elf_program_header_size (abfd) = (bfd_size_type) -1; 243 1.3 christos } 244 1.12 christos return true; 245 1.1 christos } 246 1.1 christos 247 1.1 christos 248 1.12 christos bool 249 1.1 christos bfd_elf_make_object (bfd *abfd) 250 1.1 christos { 251 1.14 christos return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata)); 252 1.1 christos } 253 1.1 christos 254 1.12 christos bool 255 1.1 christos bfd_elf_mkcorefile (bfd *abfd) 256 1.1 christos { 257 1.1 christos /* I think this can be done just like an object file. */ 258 1.3 christos if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) 259 1.12 christos return false; 260 1.3 christos elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); 261 1.3 christos return elf_tdata (abfd)->core != NULL; 262 1.1 christos } 263 1.1 christos 264 1.10 christos char * 265 1.1 christos bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) 266 1.1 christos { 267 1.1 christos Elf_Internal_Shdr **i_shdrp; 268 1.1 christos bfd_byte *shstrtab = NULL; 269 1.1 christos file_ptr offset; 270 1.1 christos bfd_size_type shstrtabsize; 271 1.1 christos 272 1.1 christos i_shdrp = elf_elfsections (abfd); 273 1.1 christos if (i_shdrp == 0 274 1.1 christos || shindex >= elf_numsections (abfd) 275 1.1 christos || i_shdrp[shindex] == 0) 276 1.1 christos return NULL; 277 1.1 christos 278 1.1 christos shstrtab = i_shdrp[shindex]->contents; 279 1.1 christos if (shstrtab == NULL) 280 1.1 christos { 281 1.1 christos /* No cached one, attempt to read, and cache what we read. */ 282 1.1 christos offset = i_shdrp[shindex]->sh_offset; 283 1.1 christos shstrtabsize = i_shdrp[shindex]->sh_size; 284 1.1 christos 285 1.14 christos if (shstrtabsize == 0 286 1.3 christos || bfd_seek (abfd, offset, SEEK_SET) != 0 287 1.14 christos || (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL) 288 1.1 christos { 289 1.1 christos /* Once we've failed to read it, make sure we don't keep 290 1.1 christos trying. Otherwise, we'll keep allocating space for 291 1.1 christos the string table over and over. */ 292 1.1 christos i_shdrp[shindex]->sh_size = 0; 293 1.1 christos } 294 1.14 christos else if (shstrtab[shstrtabsize - 1] != 0) 295 1.14 christos { 296 1.14 christos /* It is an error if a string table isn't terminated. */ 297 1.14 christos _bfd_error_handler 298 1.14 christos /* xgettext:c-format */ 299 1.14 christos (_("%pB: string table [%u] is corrupt"), abfd, shindex); 300 1.14 christos shstrtab[shstrtabsize - 1] = 0; 301 1.14 christos } 302 1.1 christos i_shdrp[shindex]->contents = shstrtab; 303 1.1 christos } 304 1.1 christos return (char *) shstrtab; 305 1.1 christos } 306 1.1 christos 307 1.1 christos char * 308 1.1 christos bfd_elf_string_from_elf_section (bfd *abfd, 309 1.1 christos unsigned int shindex, 310 1.1 christos unsigned int strindex) 311 1.1 christos { 312 1.1 christos Elf_Internal_Shdr *hdr; 313 1.1 christos 314 1.1 christos if (strindex == 0) 315 1.1 christos return ""; 316 1.1 christos 317 1.1 christos if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) 318 1.1 christos return NULL; 319 1.1 christos 320 1.1 christos hdr = elf_elfsections (abfd)[shindex]; 321 1.1 christos 322 1.3 christos if (hdr->contents == NULL) 323 1.3 christos { 324 1.3 christos if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) 325 1.3 christos { 326 1.3 christos /* PR 17512: file: f057ec89. */ 327 1.9 christos /* xgettext:c-format */ 328 1.9 christos _bfd_error_handler (_("%pB: attempt to load strings from" 329 1.9 christos " a non-string section (number %d)"), 330 1.3 christos abfd, shindex); 331 1.3 christos return NULL; 332 1.3 christos } 333 1.3 christos 334 1.3 christos if (bfd_elf_get_str_section (abfd, shindex) == NULL) 335 1.3 christos return NULL; 336 1.3 christos } 337 1.10 christos else 338 1.10 christos { 339 1.10 christos /* PR 24273: The string section's contents may have already 340 1.10 christos been loaded elsewhere, eg because a corrupt file has the 341 1.10 christos string section index in the ELF header pointing at a group 342 1.10 christos section. So be paranoid, and test that the last byte of 343 1.10 christos the section is zero. */ 344 1.10 christos if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0) 345 1.10 christos return NULL; 346 1.10 christos } 347 1.1 christos 348 1.1 christos if (strindex >= hdr->sh_size) 349 1.1 christos { 350 1.1 christos unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; 351 1.9 christos _bfd_error_handler 352 1.9 christos /* xgettext:c-format */ 353 1.9 christos (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"), 354 1.9 christos abfd, strindex, (uint64_t) hdr->sh_size, 355 1.1 christos (shindex == shstrndx && strindex == hdr->sh_name 356 1.1 christos ? ".shstrtab" 357 1.1 christos : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); 358 1.1 christos return NULL; 359 1.1 christos } 360 1.1 christos 361 1.1 christos return ((char *) hdr->contents) + strindex; 362 1.1 christos } 363 1.1 christos 364 1.1 christos /* Read and convert symbols to internal format. 365 1.1 christos SYMCOUNT specifies the number of symbols to read, starting from 366 1.1 christos symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF 367 1.1 christos are non-NULL, they are used to store the internal symbols, external 368 1.1 christos symbols, and symbol section index extensions, respectively. 369 1.1 christos Returns a pointer to the internal symbol buffer (malloced if necessary) 370 1.1 christos or NULL if there were no symbols or some kind of problem. */ 371 1.1 christos 372 1.1 christos Elf_Internal_Sym * 373 1.1 christos bfd_elf_get_elf_syms (bfd *ibfd, 374 1.1 christos Elf_Internal_Shdr *symtab_hdr, 375 1.1 christos size_t symcount, 376 1.1 christos size_t symoffset, 377 1.1 christos Elf_Internal_Sym *intsym_buf, 378 1.1 christos void *extsym_buf, 379 1.1 christos Elf_External_Sym_Shndx *extshndx_buf) 380 1.1 christos { 381 1.1 christos Elf_Internal_Shdr *shndx_hdr; 382 1.1 christos void *alloc_ext; 383 1.1 christos const bfd_byte *esym; 384 1.1 christos Elf_External_Sym_Shndx *alloc_extshndx; 385 1.1 christos Elf_External_Sym_Shndx *shndx; 386 1.1 christos Elf_Internal_Sym *alloc_intsym; 387 1.1 christos Elf_Internal_Sym *isym; 388 1.1 christos Elf_Internal_Sym *isymend; 389 1.1 christos const struct elf_backend_data *bed; 390 1.1 christos size_t extsym_size; 391 1.12 christos size_t amt; 392 1.1 christos file_ptr pos; 393 1.1 christos 394 1.1 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) 395 1.1 christos abort (); 396 1.1 christos 397 1.1 christos if (symcount == 0) 398 1.1 christos return intsym_buf; 399 1.1 christos 400 1.13 christos if (elf_use_dt_symtab_p (ibfd)) 401 1.13 christos { 402 1.13 christos /* Use dynamic symbol table. */ 403 1.13 christos if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset) 404 1.13 christos { 405 1.13 christos bfd_set_error (bfd_error_invalid_operation); 406 1.13 christos return NULL; 407 1.13 christos } 408 1.13 christos return elf_tdata (ibfd)->dt_symtab + symoffset; 409 1.13 christos } 410 1.13 christos 411 1.1 christos /* Normal syms might have section extension entries. */ 412 1.1 christos shndx_hdr = NULL; 413 1.3 christos if (elf_symtab_shndx_list (ibfd) != NULL) 414 1.3 christos { 415 1.3 christos elf_section_list * entry; 416 1.3 christos Elf_Internal_Shdr **sections = elf_elfsections (ibfd); 417 1.3 christos 418 1.3 christos /* Find an index section that is linked to this symtab section. */ 419 1.3 christos for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next) 420 1.5 christos { 421 1.5 christos /* PR 20063. */ 422 1.5 christos if (entry->hdr.sh_link >= elf_numsections (ibfd)) 423 1.5 christos continue; 424 1.5 christos 425 1.5 christos if (sections[entry->hdr.sh_link] == symtab_hdr) 426 1.5 christos { 427 1.5 christos shndx_hdr = & entry->hdr; 428 1.5 christos break; 429 1.5 christos }; 430 1.5 christos } 431 1.3 christos 432 1.3 christos if (shndx_hdr == NULL) 433 1.3 christos { 434 1.13 christos if (symtab_hdr == &elf_symtab_hdr (ibfd)) 435 1.13 christos /* Not really accurate, but this was how the old code used 436 1.13 christos to work. */ 437 1.13 christos shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr; 438 1.3 christos /* Otherwise we do nothing. The assumption is that 439 1.3 christos the index table will not be needed. */ 440 1.3 christos } 441 1.3 christos } 442 1.1 christos 443 1.1 christos /* Read the symbols. */ 444 1.1 christos alloc_ext = NULL; 445 1.1 christos alloc_extshndx = NULL; 446 1.1 christos alloc_intsym = NULL; 447 1.1 christos bed = get_elf_backend_data (ibfd); 448 1.1 christos extsym_size = bed->s->sizeof_sym; 449 1.12 christos if (_bfd_mul_overflow (symcount, extsym_size, &amt)) 450 1.12 christos { 451 1.12 christos bfd_set_error (bfd_error_file_too_big); 452 1.14 christos return NULL; 453 1.12 christos } 454 1.1 christos pos = symtab_hdr->sh_offset + symoffset * extsym_size; 455 1.14 christos size_t alloc_ext_size = amt; 456 1.14 christos if (bfd_seek (ibfd, pos, SEEK_SET) != 0 457 1.14 christos || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size, 458 1.14 christos &alloc_ext, ibfd, false)) 459 1.1 christos { 460 1.1 christos intsym_buf = NULL; 461 1.14 christos goto out2; 462 1.1 christos } 463 1.1 christos 464 1.14 christos size_t alloc_extshndx_size = 0; 465 1.1 christos if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) 466 1.1 christos extshndx_buf = NULL; 467 1.1 christos else 468 1.1 christos { 469 1.12 christos if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt)) 470 1.12 christos { 471 1.12 christos bfd_set_error (bfd_error_file_too_big); 472 1.12 christos intsym_buf = NULL; 473 1.14 christos goto out1; 474 1.12 christos } 475 1.14 christos alloc_extshndx_size = amt; 476 1.1 christos pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); 477 1.14 christos if (bfd_seek (ibfd, pos, SEEK_SET) != 0 478 1.14 christos || !_bfd_mmap_read_temporary ((void **) &extshndx_buf, 479 1.14 christos &alloc_extshndx_size, 480 1.14 christos (void **) &alloc_extshndx, 481 1.14 christos ibfd, false)) 482 1.1 christos { 483 1.1 christos intsym_buf = NULL; 484 1.14 christos goto out1; 485 1.1 christos } 486 1.1 christos } 487 1.1 christos 488 1.1 christos if (intsym_buf == NULL) 489 1.1 christos { 490 1.12 christos if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt)) 491 1.12 christos { 492 1.12 christos bfd_set_error (bfd_error_file_too_big); 493 1.14 christos goto out1; 494 1.12 christos } 495 1.12 christos alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt); 496 1.1 christos intsym_buf = alloc_intsym; 497 1.1 christos if (intsym_buf == NULL) 498 1.14 christos goto out1; 499 1.1 christos } 500 1.1 christos 501 1.1 christos /* Convert the symbols to internal form. */ 502 1.1 christos isymend = intsym_buf + symcount; 503 1.1 christos for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, 504 1.9 christos shndx = extshndx_buf; 505 1.1 christos isym < isymend; 506 1.1 christos esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) 507 1.14 christos { 508 1.14 christos if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) 509 1.14 christos { 510 1.14 christos symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; 511 1.14 christos /* xgettext:c-format */ 512 1.14 christos _bfd_error_handler (_("%pB symbol number %lu references" 513 1.14 christos " nonexistent SHT_SYMTAB_SHNDX section"), 514 1.14 christos ibfd, (unsigned long) symoffset); 515 1.14 christos free (alloc_intsym); 516 1.14 christos intsym_buf = NULL; 517 1.14 christos goto out1; 518 1.14 christos } 519 1.14 christos 520 1.14 christos /* PR 33019: Do not accept unsupported binding values - they will 521 1.14 christos likely cause problems later on. */ 522 1.14 christos int bind = ELF_ST_BIND (isym->st_info); 523 1.14 christos if (bind > STB_WEAK && bind < STB_LOOS) 524 1.14 christos { 525 1.14 christos /* xgettext:c-format */ 526 1.14 christos _bfd_error_handler (_("%pB symbol number %lu uses unsupported binding of %u"), 527 1.14 christos ibfd, (unsigned long) (isym - intsym_buf), bind); 528 1.14 christos free (alloc_intsym); 529 1.14 christos intsym_buf = NULL; 530 1.14 christos goto out1; 531 1.14 christos } 532 1.14 christos 533 1.14 christos /* Paranoia: Also refuse to accept the only undefined symbol type: 7. */ 534 1.14 christos int t = ELF_ST_TYPE (isym->st_info); 535 1.14 christos if (t == 7) 536 1.14 christos { 537 1.14 christos /* xgettext:c-format */ 538 1.14 christos _bfd_error_handler (_("%pB symbol number %lu uses unsupported type of %u"), 539 1.14 christos ibfd, (unsigned long) (isym - intsym_buf), t); 540 1.14 christos free (alloc_intsym); 541 1.14 christos intsym_buf = NULL; 542 1.14 christos goto out1; 543 1.14 christos } 544 1.14 christos } 545 1.1 christos 546 1.14 christos out1: 547 1.14 christos _bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size); 548 1.14 christos out2: 549 1.14 christos _bfd_munmap_temporary (alloc_ext, alloc_ext_size); 550 1.1 christos 551 1.1 christos return intsym_buf; 552 1.1 christos } 553 1.1 christos 554 1.1 christos /* Look up a symbol name. */ 555 1.14 christos static const char * 556 1.14 christos bfd_elf_sym_name_raw (bfd *abfd, 557 1.14 christos Elf_Internal_Shdr *symtab_hdr, 558 1.14 christos Elf_Internal_Sym *isym) 559 1.1 christos { 560 1.1 christos unsigned int iname = isym->st_name; 561 1.1 christos unsigned int shindex = symtab_hdr->sh_link; 562 1.1 christos 563 1.1 christos if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION 564 1.1 christos /* Check for a bogus st_shndx to avoid crashing. */ 565 1.1 christos && isym->st_shndx < elf_numsections (abfd)) 566 1.1 christos { 567 1.1 christos iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; 568 1.1 christos shindex = elf_elfheader (abfd)->e_shstrndx; 569 1.1 christos } 570 1.1 christos 571 1.14 christos return bfd_elf_string_from_elf_section (abfd, shindex, iname); 572 1.14 christos } 573 1.14 christos 574 1.14 christos const char * 575 1.14 christos bfd_elf_sym_name (bfd *abfd, 576 1.14 christos Elf_Internal_Shdr *symtab_hdr, 577 1.14 christos Elf_Internal_Sym *isym, 578 1.14 christos asection *sym_sec) 579 1.14 christos { 580 1.14 christos const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym); 581 1.1 christos if (name == NULL) 582 1.14 christos name = bfd_symbol_error_name; 583 1.1 christos else if (sym_sec && *name == '\0') 584 1.10 christos name = bfd_section_name (sym_sec); 585 1.1 christos 586 1.1 christos return name; 587 1.1 christos } 588 1.1 christos 589 1.1 christos /* Return the name of the group signature symbol. Why isn't the 590 1.1 christos signature just a string? */ 591 1.1 christos 592 1.1 christos static const char * 593 1.1 christos group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) 594 1.1 christos { 595 1.1 christos Elf_Internal_Shdr *hdr; 596 1.1 christos unsigned char esym[sizeof (Elf64_External_Sym)]; 597 1.1 christos Elf_External_Sym_Shndx eshndx; 598 1.1 christos Elf_Internal_Sym isym; 599 1.1 christos 600 1.1 christos /* First we need to ensure the symbol table is available. Make sure 601 1.1 christos that it is a symbol table section. */ 602 1.1 christos if (ghdr->sh_link >= elf_numsections (abfd)) 603 1.1 christos return NULL; 604 1.1 christos hdr = elf_elfsections (abfd) [ghdr->sh_link]; 605 1.1 christos if (hdr->sh_type != SHT_SYMTAB 606 1.1 christos || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) 607 1.1 christos return NULL; 608 1.1 christos 609 1.1 christos /* Go read the symbol. */ 610 1.1 christos hdr = &elf_tdata (abfd)->symtab_hdr; 611 1.1 christos if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, 612 1.1 christos &isym, esym, &eshndx) == NULL) 613 1.1 christos return NULL; 614 1.1 christos 615 1.14 christos return bfd_elf_sym_name_raw (abfd, hdr, &isym); 616 1.1 christos } 617 1.1 christos 618 1.12 christos static bool 619 1.14 christos is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize) 620 1.1 christos { 621 1.14 christos return (shdr->sh_size >= minsize 622 1.14 christos && shdr->sh_entsize == GRP_ENTRY_SIZE 623 1.14 christos && shdr->sh_size % GRP_ENTRY_SIZE == 0 624 1.14 christos && shdr->bfd_section != NULL); 625 1.14 christos } 626 1.1 christos 627 1.1 christos 628 1.14 christos /* Set next_in_group, sec_group list pointers, and group names. */ 629 1.1 christos 630 1.14 christos static bool 631 1.14 christos process_sht_group_entries (bfd *abfd, 632 1.14 christos Elf_Internal_Shdr *ghdr, unsigned int gidx) 633 1.14 christos { 634 1.14 christos unsigned char *contents; 635 1.1 christos 636 1.14 christos /* Read the raw contents. */ 637 1.14 christos if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents)) 638 1.14 christos { 639 1.14 christos _bfd_error_handler 640 1.14 christos /* xgettext:c-format */ 641 1.14 christos (_("%pB: could not read contents of group [%u]"), abfd, gidx); 642 1.14 christos return false; 643 1.14 christos } 644 1.1 christos 645 1.14 christos asection *last_elt = NULL; 646 1.14 christos const char *gname = NULL; 647 1.14 christos unsigned char *p = contents + ghdr->sh_size; 648 1.14 christos while (1) 649 1.14 christos { 650 1.14 christos unsigned int idx; 651 1.14 christos Elf_Internal_Shdr *shdr; 652 1.14 christos asection *elt; 653 1.14 christos 654 1.14 christos p -= 4; 655 1.14 christos idx = H_GET_32 (abfd, p); 656 1.14 christos if (p == contents) 657 1.14 christos { 658 1.14 christos if ((idx & GRP_COMDAT) != 0) 659 1.14 christos ghdr->bfd_section->flags 660 1.14 christos |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; 661 1.14 christos break; 662 1.1 christos } 663 1.1 christos 664 1.14 christos if (idx == 0 665 1.14 christos || idx >= elf_numsections (abfd) 666 1.14 christos || (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP 667 1.14 christos || ((elt = shdr->bfd_section) != NULL 668 1.14 christos && elf_sec_group (elt) != NULL 669 1.14 christos && elf_sec_group (elt) != ghdr->bfd_section)) 670 1.1 christos { 671 1.14 christos _bfd_error_handler 672 1.14 christos (_("%pB: invalid entry (%#x) in group [%u]"), 673 1.14 christos abfd, idx, gidx); 674 1.14 christos continue; 675 1.1 christos } 676 1.1 christos 677 1.14 christos /* PR binutils/23199: According to the ELF gABI all sections in 678 1.14 christos a group must be marked with SHF_GROUP, but some tools 679 1.14 christos generate broken objects. Fix them up here. */ 680 1.14 christos shdr->sh_flags |= SHF_GROUP; 681 1.1 christos 682 1.14 christos if (elt == NULL) 683 1.14 christos { 684 1.14 christos if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL) 685 1.1 christos { 686 1.14 christos const char *name = bfd_elf_string_from_elf_section 687 1.14 christos (abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name); 688 1.1 christos 689 1.14 christos _bfd_error_handler 690 1.14 christos /* xgettext:c-format */ 691 1.14 christos (_("%pB: unexpected type (%#x) section `%s' in group [%u]"), 692 1.14 christos abfd, shdr->sh_type, name, gidx); 693 1.3 christos } 694 1.14 christos continue; 695 1.1 christos } 696 1.1 christos 697 1.14 christos /* Don't try to add a section to elf_next_in_group list twice. */ 698 1.14 christos if (elf_sec_group (elt) != NULL) 699 1.14 christos continue; 700 1.1 christos 701 1.14 christos if (last_elt == NULL) 702 1.1 christos { 703 1.14 christos /* Start a circular list with one element. 704 1.14 christos It will be in reverse order to match what gas does. */ 705 1.14 christos elf_next_in_group (elt) = elt; 706 1.14 christos /* Point the group section to it. */ 707 1.14 christos elf_next_in_group (ghdr->bfd_section) = elt; 708 1.14 christos gname = group_signature (abfd, ghdr); 709 1.14 christos if (gname == NULL) 710 1.9 christos { 711 1.14 christos free (contents); 712 1.12 christos return false; 713 1.9 christos } 714 1.1 christos } 715 1.14 christos else 716 1.14 christos { 717 1.14 christos elf_next_in_group (elt) = elf_next_in_group (last_elt); 718 1.14 christos elf_next_in_group (last_elt) = elt; 719 1.14 christos } 720 1.14 christos last_elt = elt; 721 1.14 christos elf_group_name (elt) = gname; 722 1.14 christos elf_sec_group (elt) = ghdr->bfd_section; 723 1.1 christos } 724 1.1 christos 725 1.14 christos free (contents); 726 1.12 christos return true; 727 1.1 christos } 728 1.1 christos 729 1.12 christos bool 730 1.1 christos _bfd_elf_setup_sections (bfd *abfd) 731 1.1 christos { 732 1.12 christos bool result = true; 733 1.1 christos 734 1.1 christos /* Process SHF_LINK_ORDER. */ 735 1.14 christos for (asection *s = abfd->sections; s != NULL; s = s->next) 736 1.1 christos { 737 1.1 christos Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; 738 1.1 christos if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) 739 1.1 christos { 740 1.1 christos unsigned int elfsec = this_hdr->sh_link; 741 1.12 christos /* An sh_link value of 0 is now allowed. It indicates that linked 742 1.12 christos to section has already been discarded, but that the current 743 1.12 christos section has been retained for some other reason. This linking 744 1.12 christos section is still a candidate for later garbage collection 745 1.12 christos however. */ 746 1.1 christos if (elfsec == 0) 747 1.1 christos { 748 1.12 christos elf_linked_to_section (s) = NULL; 749 1.1 christos } 750 1.1 christos else 751 1.1 christos { 752 1.1 christos asection *linksec = NULL; 753 1.1 christos 754 1.1 christos if (elfsec < elf_numsections (abfd)) 755 1.1 christos { 756 1.1 christos this_hdr = elf_elfsections (abfd)[elfsec]; 757 1.1 christos linksec = this_hdr->bfd_section; 758 1.1 christos } 759 1.1 christos 760 1.1 christos /* PR 1991, 2008: 761 1.1 christos Some strip/objcopy may leave an incorrect value in 762 1.1 christos sh_link. We don't want to proceed. */ 763 1.1 christos if (linksec == NULL) 764 1.1 christos { 765 1.9 christos _bfd_error_handler 766 1.9 christos /* xgettext:c-format */ 767 1.9 christos (_("%pB: sh_link [%d] in section `%pA' is incorrect"), 768 1.9 christos s->owner, elfsec, s); 769 1.12 christos result = false; 770 1.1 christos } 771 1.1 christos 772 1.1 christos elf_linked_to_section (s) = linksec; 773 1.1 christos } 774 1.1 christos } 775 1.1 christos } 776 1.1 christos 777 1.1 christos /* Process section groups. */ 778 1.14 christos for (unsigned int i = 1; i < elf_numsections (abfd); i++) 779 1.1 christos { 780 1.14 christos Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; 781 1.1 christos 782 1.14 christos if (shdr && shdr->sh_type == SHT_GROUP) 783 1.9 christos { 784 1.14 christos if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE)) 785 1.14 christos { 786 1.14 christos if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE 787 1.14 christos && !process_sht_group_entries (abfd, shdr, i)) 788 1.14 christos result = false; 789 1.14 christos } 790 1.14 christos else 791 1.9 christos { 792 1.14 christos /* PR binutils/18758: Beware of corrupt binaries with 793 1.14 christos invalid group data. */ 794 1.9 christos _bfd_error_handler 795 1.9 christos /* xgettext:c-format */ 796 1.14 christos (_("%pB: section group entry number %u is corrupt"), abfd, i); 797 1.12 christos result = false; 798 1.9 christos } 799 1.9 christos } 800 1.1 christos } 801 1.9 christos 802 1.1 christos return result; 803 1.1 christos } 804 1.1 christos 805 1.12 christos bool 806 1.1 christos bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) 807 1.1 christos { 808 1.1 christos return elf_next_in_group (sec) != NULL; 809 1.1 christos } 810 1.1 christos 811 1.10 christos const char * 812 1.10 christos bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) 813 1.10 christos { 814 1.10 christos if (elf_sec_group (sec) != NULL) 815 1.10 christos return elf_group_name (sec); 816 1.10 christos return NULL; 817 1.10 christos } 818 1.10 christos 819 1.1 christos /* Make a BFD section from an ELF section. We store a pointer to the 820 1.1 christos BFD section in the bfd_section field of the header. */ 821 1.1 christos 822 1.12 christos bool 823 1.1 christos _bfd_elf_make_section_from_shdr (bfd *abfd, 824 1.1 christos Elf_Internal_Shdr *hdr, 825 1.1 christos const char *name, 826 1.1 christos int shindex) 827 1.1 christos { 828 1.1 christos asection *newsect; 829 1.1 christos flagword flags; 830 1.1 christos const struct elf_backend_data *bed; 831 1.12 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL); 832 1.1 christos 833 1.1 christos if (hdr->bfd_section != NULL) 834 1.12 christos return true; 835 1.1 christos 836 1.1 christos newsect = bfd_make_section_anyway (abfd, name); 837 1.1 christos if (newsect == NULL) 838 1.12 christos return false; 839 1.1 christos 840 1.1 christos hdr->bfd_section = newsect; 841 1.1 christos elf_section_data (newsect)->this_hdr = *hdr; 842 1.1 christos elf_section_data (newsect)->this_idx = shindex; 843 1.1 christos 844 1.1 christos /* Always use the real type/flags. */ 845 1.1 christos elf_section_type (newsect) = hdr->sh_type; 846 1.1 christos elf_section_flags (newsect) = hdr->sh_flags; 847 1.1 christos 848 1.1 christos newsect->filepos = hdr->sh_offset; 849 1.1 christos 850 1.1 christos flags = SEC_NO_FLAGS; 851 1.1 christos if (hdr->sh_type != SHT_NOBITS) 852 1.1 christos flags |= SEC_HAS_CONTENTS; 853 1.1 christos if (hdr->sh_type == SHT_GROUP) 854 1.9 christos flags |= SEC_GROUP; 855 1.1 christos if ((hdr->sh_flags & SHF_ALLOC) != 0) 856 1.1 christos { 857 1.1 christos flags |= SEC_ALLOC; 858 1.1 christos if (hdr->sh_type != SHT_NOBITS) 859 1.1 christos flags |= SEC_LOAD; 860 1.1 christos } 861 1.1 christos if ((hdr->sh_flags & SHF_WRITE) == 0) 862 1.1 christos flags |= SEC_READONLY; 863 1.1 christos if ((hdr->sh_flags & SHF_EXECINSTR) != 0) 864 1.1 christos flags |= SEC_CODE; 865 1.1 christos else if ((flags & SEC_LOAD) != 0) 866 1.1 christos flags |= SEC_DATA; 867 1.1 christos if ((hdr->sh_flags & SHF_MERGE) != 0) 868 1.1 christos { 869 1.1 christos flags |= SEC_MERGE; 870 1.1 christos newsect->entsize = hdr->sh_entsize; 871 1.1 christos } 872 1.5 christos if ((hdr->sh_flags & SHF_STRINGS) != 0) 873 1.14 christos { 874 1.14 christos flags |= SEC_STRINGS; 875 1.14 christos newsect->entsize = hdr->sh_entsize; 876 1.14 christos } 877 1.1 christos if ((hdr->sh_flags & SHF_TLS) != 0) 878 1.1 christos flags |= SEC_THREAD_LOCAL; 879 1.1 christos if ((hdr->sh_flags & SHF_EXCLUDE) != 0) 880 1.1 christos flags |= SEC_EXCLUDE; 881 1.1 christos 882 1.10 christos switch (elf_elfheader (abfd)->e_ident[EI_OSABI]) 883 1.10 christos { 884 1.10 christos /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE, 885 1.10 christos but binutils as of 2019-07-23 did not set the EI_OSABI header 886 1.10 christos byte. */ 887 1.10 christos case ELFOSABI_GNU: 888 1.10 christos case ELFOSABI_FREEBSD: 889 1.12 christos if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0) 890 1.12 christos elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain; 891 1.12 christos /* Fall through */ 892 1.12 christos case ELFOSABI_NONE: 893 1.10 christos if ((hdr->sh_flags & SHF_GNU_MBIND) != 0) 894 1.10 christos elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind; 895 1.10 christos break; 896 1.10 christos } 897 1.10 christos 898 1.1 christos if ((flags & SEC_ALLOC) == 0) 899 1.1 christos { 900 1.1 christos /* The debugging sections appear to be recognized only by name, 901 1.1 christos not any sort of flag. Their SEC_ALLOC bits are cleared. */ 902 1.1 christos if (name [0] == '.') 903 1.1 christos { 904 1.12 christos if (startswith (name, ".debug") 905 1.12 christos || startswith (name, ".gnu.debuglto_.debug_") 906 1.12 christos || startswith (name, ".gnu.linkonce.wi.") 907 1.12 christos || startswith (name, ".zdebug")) 908 1.10 christos flags |= SEC_DEBUGGING | SEC_ELF_OCTETS; 909 1.12 christos else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME) 910 1.12 christos || startswith (name, ".note.gnu")) 911 1.12 christos { 912 1.12 christos flags |= SEC_ELF_OCTETS; 913 1.12 christos opb = 1; 914 1.12 christos } 915 1.12 christos else if (startswith (name, ".line") 916 1.12 christos || startswith (name, ".stab") 917 1.10 christos || strcmp (name, ".gdb_index") == 0) 918 1.1 christos flags |= SEC_DEBUGGING; 919 1.1 christos } 920 1.1 christos } 921 1.1 christos 922 1.12 christos if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb) 923 1.12 christos || !bfd_set_section_size (newsect, hdr->sh_size) 924 1.12 christos || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign 925 1.12 christos & -hdr->sh_addralign))) 926 1.12 christos return false; 927 1.12 christos 928 1.1 christos /* As a GNU extension, if the name begins with .gnu.linkonce, we 929 1.1 christos only link a single copy of the section. This is used to support 930 1.1 christos g++. g++ will emit each template expansion in its own section. 931 1.1 christos The symbols will be defined as weak, so that multiple definitions 932 1.1 christos are permitted. The GNU linker extension is to actually discard 933 1.1 christos all but one of the sections. */ 934 1.12 christos if (startswith (name, ".gnu.linkonce") 935 1.1 christos && elf_next_in_group (newsect) == NULL) 936 1.1 christos flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; 937 1.1 christos 938 1.12 christos if (!bfd_set_section_flags (newsect, flags)) 939 1.12 christos return false; 940 1.12 christos 941 1.1 christos bed = get_elf_backend_data (abfd); 942 1.1 christos if (bed->elf_backend_section_flags) 943 1.12 christos if (!bed->elf_backend_section_flags (hdr)) 944 1.12 christos return false; 945 1.1 christos 946 1.1 christos /* We do not parse the PT_NOTE segments as we are interested even in the 947 1.1 christos separate debug info files which may have the segments offsets corrupted. 948 1.1 christos PT_NOTEs from the core files are currently not parsed using BFD. */ 949 1.13 christos if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0) 950 1.1 christos { 951 1.1 christos bfd_byte *contents; 952 1.1 christos 953 1.14 christos if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents)) 954 1.12 christos return false; 955 1.1 christos 956 1.9 christos elf_parse_notes (abfd, (char *) contents, hdr->sh_size, 957 1.9 christos hdr->sh_offset, hdr->sh_addralign); 958 1.14 christos _bfd_elf_munmap_section_contents (newsect, contents); 959 1.1 christos } 960 1.1 christos 961 1.12 christos if ((newsect->flags & SEC_ALLOC) != 0) 962 1.1 christos { 963 1.1 christos Elf_Internal_Phdr *phdr; 964 1.1 christos unsigned int i, nload; 965 1.1 christos 966 1.1 christos /* Some ELF linkers produce binaries with all the program header 967 1.1 christos p_paddr fields zero. If we have such a binary with more than 968 1.1 christos one PT_LOAD header, then leave the section lma equal to vma 969 1.1 christos so that we don't create sections with overlapping lma. */ 970 1.1 christos phdr = elf_tdata (abfd)->phdr; 971 1.1 christos for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) 972 1.1 christos if (phdr->p_paddr != 0) 973 1.1 christos break; 974 1.1 christos else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) 975 1.1 christos ++nload; 976 1.1 christos if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) 977 1.12 christos return true; 978 1.1 christos 979 1.1 christos phdr = elf_tdata (abfd)->phdr; 980 1.1 christos for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) 981 1.1 christos { 982 1.1 christos if (((phdr->p_type == PT_LOAD 983 1.1 christos && (hdr->sh_flags & SHF_TLS) == 0) 984 1.1 christos || phdr->p_type == PT_TLS) 985 1.1 christos && ELF_SECTION_IN_SEGMENT (hdr, phdr)) 986 1.1 christos { 987 1.12 christos if ((newsect->flags & SEC_LOAD) == 0) 988 1.1 christos newsect->lma = (phdr->p_paddr 989 1.12 christos + hdr->sh_addr - phdr->p_vaddr) / opb; 990 1.1 christos else 991 1.1 christos /* We used to use the same adjustment for SEC_LOAD 992 1.1 christos sections, but that doesn't work if the segment 993 1.1 christos is packed with code from multiple VMAs. 994 1.1 christos Instead we calculate the section LMA based on 995 1.1 christos the segment LMA. It is assumed that the 996 1.1 christos segment will contain sections with contiguous 997 1.1 christos LMAs, even if the VMAs are not. */ 998 1.1 christos newsect->lma = (phdr->p_paddr 999 1.12 christos + hdr->sh_offset - phdr->p_offset) / opb; 1000 1.1 christos 1001 1.1 christos /* With contiguous segments, we can't tell from file 1002 1.1 christos offsets whether a section with zero size should 1003 1.1 christos be placed at the end of one segment or the 1004 1.1 christos beginning of the next. Decide based on vaddr. */ 1005 1.1 christos if (hdr->sh_addr >= phdr->p_vaddr 1006 1.1 christos && (hdr->sh_addr + hdr->sh_size 1007 1.1 christos <= phdr->p_vaddr + phdr->p_memsz)) 1008 1.1 christos break; 1009 1.1 christos } 1010 1.1 christos } 1011 1.1 christos } 1012 1.1 christos 1013 1.13 christos /* Compress/decompress DWARF debug sections with names: .debug_*, 1014 1.13 christos .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */ 1015 1.13 christos if ((newsect->flags & SEC_DEBUGGING) != 0 1016 1.13 christos && (newsect->flags & SEC_HAS_CONTENTS) != 0 1017 1.13 christos && (newsect->flags & SEC_ELF_OCTETS) != 0) 1018 1.1 christos { 1019 1.1 christos enum { nothing, compress, decompress } action = nothing; 1020 1.3 christos int compression_header_size; 1021 1.3 christos bfd_size_type uncompressed_size; 1022 1.10 christos unsigned int uncompressed_align_power; 1023 1.13 christos enum compression_type ch_type = ch_none; 1024 1.12 christos bool compressed 1025 1.13 christos = bfd_is_section_compressed_info (abfd, newsect, 1026 1.13 christos &compression_header_size, 1027 1.13 christos &uncompressed_size, 1028 1.13 christos &uncompressed_align_power, 1029 1.13 christos &ch_type); 1030 1.13 christos 1031 1.13 christos /* Should we decompress? */ 1032 1.13 christos if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed) 1033 1.13 christos action = decompress; 1034 1.13 christos 1035 1.13 christos /* Should we compress? Or convert to a different compression? */ 1036 1.13 christos else if ((abfd->flags & BFD_COMPRESS) != 0 1037 1.13 christos && newsect->size != 0 1038 1.13 christos && compression_header_size >= 0 1039 1.13 christos && uncompressed_size > 0) 1040 1.13 christos { 1041 1.13 christos if (!compressed) 1042 1.1 christos action = compress; 1043 1.3 christos else 1044 1.13 christos { 1045 1.13 christos enum compression_type new_ch_type = ch_none; 1046 1.13 christos if ((abfd->flags & BFD_COMPRESS_GABI) != 0) 1047 1.13 christos new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0 1048 1.13 christos ? ch_compress_zstd : ch_compress_zlib); 1049 1.13 christos if (new_ch_type != ch_type) 1050 1.13 christos action = compress; 1051 1.13 christos } 1052 1.1 christos } 1053 1.1 christos 1054 1.3 christos if (action == compress) 1055 1.1 christos { 1056 1.1 christos if (!bfd_init_section_compress_status (abfd, newsect)) 1057 1.1 christos { 1058 1.9 christos _bfd_error_handler 1059 1.9 christos /* xgettext:c-format */ 1060 1.13 christos (_("%pB: unable to compress section %s"), abfd, name); 1061 1.12 christos return false; 1062 1.1 christos } 1063 1.3 christos } 1064 1.13 christos else if (action == decompress) 1065 1.3 christos { 1066 1.1 christos if (!bfd_init_section_decompress_status (abfd, newsect)) 1067 1.1 christos { 1068 1.9 christos _bfd_error_handler 1069 1.9 christos /* xgettext:c-format */ 1070 1.13 christos (_("%pB: unable to decompress section %s"), abfd, name); 1071 1.13 christos return false; 1072 1.13 christos } 1073 1.13 christos #ifndef HAVE_ZSTD 1074 1.13 christos if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD) 1075 1.13 christos { 1076 1.13 christos _bfd_error_handler 1077 1.13 christos /* xgettext:c-format */ 1078 1.13 christos (_ ("%pB: section %s is compressed with zstd, but BFD " 1079 1.13 christos "is not built with zstd support"), 1080 1.13 christos abfd, name); 1081 1.13 christos newsect->compress_status = COMPRESS_SECTION_NONE; 1082 1.12 christos return false; 1083 1.1 christos } 1084 1.13 christos #endif 1085 1.13 christos if (abfd->is_linker_input 1086 1.13 christos && name[1] == 'z') 1087 1.13 christos { 1088 1.13 christos /* Rename section from .zdebug_* to .debug_* so that ld 1089 1.13 christos scripts will see this section as a debug section. */ 1090 1.13 christos char *new_name = bfd_zdebug_name_to_debug (abfd, name); 1091 1.1 christos if (new_name == NULL) 1092 1.12 christos return false; 1093 1.10 christos bfd_rename_section (newsect, new_name); 1094 1.1 christos } 1095 1.1 christos } 1096 1.1 christos } 1097 1.1 christos 1098 1.12 christos return true; 1099 1.1 christos } 1100 1.1 christos 1101 1.5 christos const char *const bfd_elf_section_type_names[] = 1102 1.5 christos { 1103 1.1 christos "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", 1104 1.1 christos "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", 1105 1.1 christos "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", 1106 1.1 christos }; 1107 1.1 christos 1108 1.1 christos /* ELF relocs are against symbols. If we are producing relocatable 1109 1.1 christos output, and the reloc is against an external symbol, and nothing 1110 1.1 christos has given us any additional addend, the resulting reloc will also 1111 1.1 christos be against the same symbol. In such a case, we don't want to 1112 1.1 christos change anything about the way the reloc is handled, since it will 1113 1.1 christos all be done at final link time. Rather than put special case code 1114 1.1 christos into bfd_perform_relocation, all the reloc types use this howto 1115 1.12 christos function, or should call this function for relocatable output. */ 1116 1.1 christos 1117 1.1 christos bfd_reloc_status_type 1118 1.1 christos bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, 1119 1.1 christos arelent *reloc_entry, 1120 1.1 christos asymbol *symbol, 1121 1.1 christos void *data ATTRIBUTE_UNUSED, 1122 1.1 christos asection *input_section, 1123 1.1 christos bfd *output_bfd, 1124 1.1 christos char **error_message ATTRIBUTE_UNUSED) 1125 1.1 christos { 1126 1.1 christos if (output_bfd != NULL 1127 1.1 christos && (symbol->flags & BSF_SECTION_SYM) == 0 1128 1.1 christos && (! reloc_entry->howto->partial_inplace 1129 1.1 christos || reloc_entry->addend == 0)) 1130 1.1 christos { 1131 1.1 christos reloc_entry->address += input_section->output_offset; 1132 1.1 christos return bfd_reloc_ok; 1133 1.1 christos } 1134 1.1 christos 1135 1.12 christos /* In some cases the relocation should be treated as output section 1136 1.12 christos relative, as when linking ELF DWARF into PE COFF. Many ELF 1137 1.12 christos targets lack section relative relocations and instead use 1138 1.12 christos ordinary absolute relocations for references between DWARF 1139 1.12 christos sections. That is arguably a bug in those targets but it happens 1140 1.12 christos to work for the usual case of linking to non-loaded ELF debug 1141 1.12 christos sections with VMAs forced to zero. PE COFF on the other hand 1142 1.12 christos doesn't allow a section VMA of zero. */ 1143 1.12 christos if (output_bfd == NULL 1144 1.12 christos && !reloc_entry->howto->pc_relative 1145 1.12 christos && (symbol->section->flags & SEC_DEBUGGING) != 0 1146 1.12 christos && (input_section->flags & SEC_DEBUGGING) != 0) 1147 1.12 christos reloc_entry->addend -= symbol->section->output_section->vma; 1148 1.12 christos 1149 1.1 christos return bfd_reloc_continue; 1150 1.1 christos } 1151 1.1 christos 1152 1.5 christos /* Returns TRUE if section A matches section B. 1154 1.5 christos Names, addresses and links may be different, but everything else 1155 1.5 christos should be the same. */ 1156 1.12 christos 1157 1.5 christos static bool 1158 1.5 christos section_match (const Elf_Internal_Shdr * a, 1159 1.5 christos const Elf_Internal_Shdr * b) 1160 1.10 christos { 1161 1.10 christos if (a->sh_type != b->sh_type 1162 1.10 christos || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0 1163 1.10 christos || a->sh_addralign != b->sh_addralign 1164 1.12 christos || a->sh_entsize != b->sh_entsize) 1165 1.10 christos return false; 1166 1.10 christos if (a->sh_type == SHT_SYMTAB 1167 1.12 christos || a->sh_type == SHT_STRTAB) 1168 1.10 christos return true; 1169 1.5 christos return a->sh_size == b->sh_size; 1170 1.5 christos } 1171 1.5 christos 1172 1.5 christos /* Find a section in OBFD that has the same characteristics 1173 1.5 christos as IHEADER. Return the index of this section or SHN_UNDEF if 1174 1.5 christos none can be found. Check's section HINT first, as this is likely 1175 1.5 christos to be the correct section. */ 1176 1.5 christos 1177 1.9 christos static unsigned int 1178 1.9 christos find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader, 1179 1.5 christos const unsigned int hint) 1180 1.5 christos { 1181 1.5 christos Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); 1182 1.5 christos unsigned int i; 1183 1.9 christos 1184 1.9 christos BFD_ASSERT (iheader != NULL); 1185 1.9 christos 1186 1.9 christos /* See PR 20922 for a reproducer of the NULL test. */ 1187 1.9 christos if (hint < elf_numsections (obfd) 1188 1.9 christos && oheaders[hint] != NULL 1189 1.5 christos && section_match (oheaders[hint], iheader)) 1190 1.5 christos return hint; 1191 1.5 christos 1192 1.5 christos for (i = 1; i < elf_numsections (obfd); i++) 1193 1.5 christos { 1194 1.5 christos Elf_Internal_Shdr * oheader = oheaders[i]; 1195 1.9 christos 1196 1.9 christos if (oheader == NULL) 1197 1.5 christos continue; 1198 1.5 christos if (section_match (oheader, iheader)) 1199 1.5 christos /* FIXME: Do we care if there is a potential for 1200 1.5 christos multiple matches ? */ 1201 1.5 christos return i; 1202 1.5 christos } 1203 1.5 christos 1204 1.5 christos return SHN_UNDEF; 1205 1.5 christos } 1206 1.5 christos 1207 1.5 christos /* PR 19938: Attempt to set the ELF section header fields of an OS or 1208 1.5 christos Processor specific section, based upon a matching input section. 1209 1.9 christos Returns TRUE upon success, FALSE otherwise. */ 1210 1.12 christos 1211 1.5 christos static bool 1212 1.5 christos copy_special_section_fields (const bfd *ibfd, 1213 1.5 christos bfd *obfd, 1214 1.5 christos const Elf_Internal_Shdr *iheader, 1215 1.5 christos Elf_Internal_Shdr *oheader, 1216 1.5 christos const unsigned int secnum) 1217 1.5 christos { 1218 1.13 christos const struct elf_backend_data *bed = get_elf_backend_data (obfd); 1219 1.13 christos const Elf_Internal_Shdr **iheaders 1220 1.12 christos = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); 1221 1.5 christos bool changed = false; 1222 1.5 christos unsigned int sh_link; 1223 1.5 christos 1224 1.5 christos if (oheader->sh_type == SHT_NOBITS) 1225 1.5 christos { 1226 1.5 christos /* This is a feature for objcopy --only-keep-debug: 1227 1.5 christos When a section's type is changed to NOBITS, we preserve 1228 1.5 christos the sh_link and sh_info fields so that they can be 1229 1.5 christos matched up with the original. 1230 1.5 christos 1231 1.5 christos Note: Strictly speaking these assignments are wrong. 1232 1.5 christos The sh_link and sh_info fields should point to the 1233 1.5 christos relevent sections in the output BFD, which may not be in 1234 1.5 christos the same location as they were in the input BFD. But 1235 1.5 christos the whole point of this action is to preserve the 1236 1.5 christos original values of the sh_link and sh_info fields, so 1237 1.5 christos that they can be matched up with the section headers in 1238 1.5 christos the original file. So strictly speaking we may be 1239 1.5 christos creating an invalid ELF file, but it is only for a file 1240 1.5 christos that just contains debug info and only for sections 1241 1.5 christos without any contents. */ 1242 1.5 christos if (oheader->sh_link == 0) 1243 1.5 christos oheader->sh_link = iheader->sh_link; 1244 1.5 christos if (oheader->sh_info == 0) 1245 1.12 christos oheader->sh_info = iheader->sh_info; 1246 1.5 christos return true; 1247 1.5 christos } 1248 1.5 christos 1249 1.12 christos /* Allow the target a chance to decide how these fields should be set. */ 1250 1.12 christos if (bed->elf_backend_copy_special_section_fields (ibfd, obfd, 1251 1.12 christos iheader, oheader)) 1252 1.5 christos return true; 1253 1.5 christos 1254 1.5 christos /* We have an iheader which might match oheader, and which has non-zero 1255 1.5 christos sh_info and/or sh_link fields. Attempt to follow those links and find 1256 1.5 christos the section in the output bfd which corresponds to the linked section 1257 1.5 christos in the input bfd. */ 1258 1.5 christos if (iheader->sh_link != SHN_UNDEF) 1259 1.9 christos { 1260 1.9 christos /* See PR 20931 for a reproducer. */ 1261 1.9 christos if (iheader->sh_link >= elf_numsections (ibfd)) 1262 1.9 christos { 1263 1.9 christos _bfd_error_handler 1264 1.9 christos /* xgettext:c-format */ 1265 1.9 christos (_("%pB: invalid sh_link field (%d) in section number %d"), 1266 1.12 christos ibfd, iheader->sh_link, secnum); 1267 1.9 christos return false; 1268 1.9 christos } 1269 1.5 christos 1270 1.5 christos sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); 1271 1.5 christos if (sh_link != SHN_UNDEF) 1272 1.5 christos { 1273 1.12 christos oheader->sh_link = sh_link; 1274 1.5 christos changed = true; 1275 1.5 christos } 1276 1.5 christos else 1277 1.5 christos /* FIXME: Should we install iheader->sh_link 1278 1.9 christos if we could not find a match ? */ 1279 1.9 christos _bfd_error_handler 1280 1.9 christos /* xgettext:c-format */ 1281 1.5 christos (_("%pB: failed to find link section for section %d"), obfd, secnum); 1282 1.5 christos } 1283 1.5 christos 1284 1.5 christos if (iheader->sh_info) 1285 1.5 christos { 1286 1.5 christos /* The sh_info field can hold arbitrary information, but if the 1287 1.5 christos SHF_LINK_INFO flag is set then it should be interpreted as a 1288 1.5 christos section index. */ 1289 1.5 christos if (iheader->sh_flags & SHF_INFO_LINK) 1290 1.5 christos { 1291 1.5 christos sh_link = find_link (obfd, iheaders[iheader->sh_info], 1292 1.5 christos iheader->sh_info); 1293 1.5 christos if (sh_link != SHN_UNDEF) 1294 1.5 christos oheader->sh_flags |= SHF_INFO_LINK; 1295 1.5 christos } 1296 1.5 christos else 1297 1.5 christos /* No idea what it means - just copy it. */ 1298 1.5 christos sh_link = iheader->sh_info; 1299 1.5 christos 1300 1.5 christos if (sh_link != SHN_UNDEF) 1301 1.5 christos { 1302 1.12 christos oheader->sh_info = sh_link; 1303 1.5 christos changed = true; 1304 1.5 christos } 1305 1.9 christos else 1306 1.9 christos _bfd_error_handler 1307 1.9 christos /* xgettext:c-format */ 1308 1.5 christos (_("%pB: failed to find info section for section %d"), obfd, secnum); 1309 1.5 christos } 1310 1.5 christos 1311 1.5 christos return changed; 1312 1.9 christos } 1313 1.1 christos 1314 1.1 christos /* Copy the program header and other data from one object module to 1315 1.1 christos another. */ 1316 1.12 christos 1317 1.1 christos bool 1318 1.1 christos _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) 1319 1.13 christos { 1320 1.13 christos const Elf_Internal_Shdr **iheaders 1321 1.5 christos = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); 1322 1.5 christos Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); 1323 1.5 christos const struct elf_backend_data *bed; 1324 1.5 christos unsigned int i; 1325 1.1 christos 1326 1.5 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 1327 1.12 christos || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 1328 1.1 christos return true; 1329 1.3 christos 1330 1.3 christos if (!elf_flags_init (obfd)) 1331 1.3 christos { 1332 1.12 christos elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; 1333 1.3 christos elf_flags_init (obfd) = true; 1334 1.1 christos } 1335 1.1 christos 1336 1.3 christos elf_gp (obfd) = elf_gp (ibfd); 1337 1.3 christos 1338 1.3 christos /* Also copy the EI_OSABI field. */ 1339 1.3 christos elf_elfheader (obfd)->e_ident[EI_OSABI] = 1340 1.1 christos elf_elfheader (ibfd)->e_ident[EI_OSABI]; 1341 1.5 christos 1342 1.5 christos /* If set, copy the EI_ABIVERSION field. */ 1343 1.5 christos if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) 1344 1.5 christos elf_elfheader (obfd)->e_ident[EI_ABIVERSION] 1345 1.9 christos = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; 1346 1.1 christos 1347 1.1 christos /* Copy object attributes. */ 1348 1.3 christos _bfd_elf_copy_obj_attributes (ibfd, obfd); 1349 1.5 christos 1350 1.12 christos if (iheaders == NULL || oheaders == NULL) 1351 1.5 christos return true; 1352 1.5 christos 1353 1.3 christos bed = get_elf_backend_data (obfd); 1354 1.5 christos 1355 1.5 christos /* Possibly copy other fields in the section header. */ 1356 1.3 christos for (i = 1; i < elf_numsections (obfd); i++) 1357 1.5 christos { 1358 1.5 christos unsigned int j; 1359 1.5 christos Elf_Internal_Shdr * oheader = oheaders[i]; 1360 1.5 christos 1361 1.5 christos /* Ignore ordinary sections. SHT_NOBITS sections are considered however 1362 1.5 christos because of a special case need for generating separate debug info 1363 1.5 christos files. See below for more details. */ 1364 1.5 christos if (oheader == NULL 1365 1.5 christos || (oheader->sh_type != SHT_NOBITS 1366 1.5 christos && oheader->sh_type < SHT_LOOS)) 1367 1.5 christos continue; 1368 1.5 christos 1369 1.5 christos /* Ignore empty sections, and sections whose 1370 1.5 christos fields have already been initialised. */ 1371 1.5 christos if (oheader->sh_size == 0 1372 1.5 christos || (oheader->sh_info != 0 && oheader->sh_link != 0)) 1373 1.5 christos continue; 1374 1.5 christos 1375 1.13 christos /* Scan for the matching section in the input bfd. 1376 1.13 christos First we try for a direct mapping between the input and 1377 1.5 christos output sections. */ 1378 1.5 christos for (j = 1; j < elf_numsections (ibfd); j++) 1379 1.5 christos { 1380 1.5 christos const Elf_Internal_Shdr * iheader = iheaders[j]; 1381 1.5 christos 1382 1.5 christos if (iheader == NULL) 1383 1.5 christos continue; 1384 1.5 christos 1385 1.5 christos if (oheader->bfd_section != NULL 1386 1.5 christos && iheader->bfd_section != NULL 1387 1.5 christos && iheader->bfd_section->output_section != NULL 1388 1.5 christos && iheader->bfd_section->output_section == oheader->bfd_section) 1389 1.13 christos { 1390 1.13 christos /* We have found a connection from the input section to 1391 1.13 christos the output section. Attempt to copy the header fields. 1392 1.13 christos If this fails then do not try any further sections - 1393 1.13 christos there should only be a one-to-one mapping between 1394 1.13 christos input and output. */ 1395 1.13 christos if (!copy_special_section_fields (ibfd, obfd, 1396 1.5 christos iheader, oheader, i)) 1397 1.5 christos j = elf_numsections (ibfd); 1398 1.5 christos break; 1399 1.5 christos } 1400 1.5 christos } 1401 1.5 christos 1402 1.5 christos if (j < elf_numsections (ibfd)) 1403 1.3 christos continue; 1404 1.5 christos 1405 1.5 christos /* That failed. So try to deduce the corresponding input section. 1406 1.5 christos Unfortunately we cannot compare names as the output string table 1407 1.5 christos is empty, so instead we check size, address and type. */ 1408 1.3 christos for (j = 1; j < elf_numsections (ibfd); j++) 1409 1.5 christos { 1410 1.3 christos const Elf_Internal_Shdr * iheader = iheaders[j]; 1411 1.5 christos 1412 1.3 christos if (iheader == NULL) 1413 1.3 christos continue; 1414 1.5 christos 1415 1.5 christos /* Try matching fields in the input section's header. 1416 1.5 christos Since --only-keep-debug turns all non-debug sections into 1417 1.5 christos SHT_NOBITS sections, the output SHT_NOBITS type matches any 1418 1.5 christos input type. */ 1419 1.5 christos if ((oheader->sh_type == SHT_NOBITS 1420 1.5 christos || iheader->sh_type == oheader->sh_type) 1421 1.5 christos && (iheader->sh_flags & ~ SHF_INFO_LINK) 1422 1.5 christos == (oheader->sh_flags & ~ SHF_INFO_LINK) 1423 1.5 christos && iheader->sh_addralign == oheader->sh_addralign 1424 1.5 christos && iheader->sh_entsize == oheader->sh_entsize 1425 1.5 christos && iheader->sh_size == oheader->sh_size 1426 1.5 christos && iheader->sh_addr == oheader->sh_addr 1427 1.5 christos && (iheader->sh_info != oheader->sh_info 1428 1.5 christos || iheader->sh_link != oheader->sh_link)) 1429 1.5 christos { 1430 1.5 christos if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) 1431 1.3 christos break; 1432 1.3 christos } 1433 1.5 christos } 1434 1.5 christos 1435 1.5 christos if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) 1436 1.5 christos { 1437 1.5 christos /* Final attempt. Call the backend copy function 1438 1.12 christos with a NULL input section. */ 1439 1.12 christos (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd, 1440 1.5 christos NULL, oheader); 1441 1.3 christos } 1442 1.3 christos } 1443 1.12 christos 1444 1.1 christos return true; 1445 1.1 christos } 1446 1.1 christos 1447 1.1 christos static const char * 1448 1.1 christos get_segment_type (unsigned int p_type) 1449 1.1 christos { 1450 1.1 christos const char *pt; 1451 1.1 christos switch (p_type) 1452 1.1 christos { 1453 1.1 christos case PT_NULL: pt = "NULL"; break; 1454 1.1 christos case PT_LOAD: pt = "LOAD"; break; 1455 1.1 christos case PT_DYNAMIC: pt = "DYNAMIC"; break; 1456 1.1 christos case PT_INTERP: pt = "INTERP"; break; 1457 1.1 christos case PT_NOTE: pt = "NOTE"; break; 1458 1.1 christos case PT_SHLIB: pt = "SHLIB"; break; 1459 1.1 christos case PT_PHDR: pt = "PHDR"; break; 1460 1.1 christos case PT_TLS: pt = "TLS"; break; 1461 1.1 christos case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; 1462 1.1 christos case PT_GNU_STACK: pt = "STACK"; break; 1463 1.13 christos case PT_GNU_RELRO: pt = "RELRO"; break; 1464 1.1 christos case PT_GNU_SFRAME: pt = "SFRAME"; break; 1465 1.1 christos default: pt = NULL; break; 1466 1.1 christos } 1467 1.1 christos return pt; 1468 1.1 christos } 1469 1.1 christos 1470 1.1 christos /* Print out the program headers. */ 1471 1.12 christos 1472 1.1 christos bool 1473 1.1 christos _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) 1474 1.1 christos { 1475 1.1 christos FILE *f = (FILE *) farg; 1476 1.1 christos Elf_Internal_Phdr *p; 1477 1.1 christos asection *s; 1478 1.1 christos bfd_byte *dynbuf = NULL; 1479 1.1 christos 1480 1.1 christos p = elf_tdata (abfd)->phdr; 1481 1.1 christos if (p != NULL) 1482 1.1 christos { 1483 1.1 christos unsigned int i, c; 1484 1.1 christos 1485 1.1 christos fprintf (f, _("\nProgram Header:\n")); 1486 1.1 christos c = elf_elfheader (abfd)->e_phnum; 1487 1.1 christos for (i = 0; i < c; i++, p++) 1488 1.1 christos { 1489 1.1 christos const char *pt = get_segment_type (p->p_type); 1490 1.1 christos char buf[20]; 1491 1.1 christos 1492 1.1 christos if (pt == NULL) 1493 1.1 christos { 1494 1.1 christos sprintf (buf, "0x%lx", p->p_type); 1495 1.1 christos pt = buf; 1496 1.1 christos } 1497 1.1 christos fprintf (f, "%8s off 0x", pt); 1498 1.1 christos bfd_fprintf_vma (abfd, f, p->p_offset); 1499 1.1 christos fprintf (f, " vaddr 0x"); 1500 1.1 christos bfd_fprintf_vma (abfd, f, p->p_vaddr); 1501 1.1 christos fprintf (f, " paddr 0x"); 1502 1.1 christos bfd_fprintf_vma (abfd, f, p->p_paddr); 1503 1.1 christos fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); 1504 1.1 christos fprintf (f, " filesz 0x"); 1505 1.1 christos bfd_fprintf_vma (abfd, f, p->p_filesz); 1506 1.1 christos fprintf (f, " memsz 0x"); 1507 1.1 christos bfd_fprintf_vma (abfd, f, p->p_memsz); 1508 1.1 christos fprintf (f, " flags %c%c%c", 1509 1.1 christos (p->p_flags & PF_R) != 0 ? 'r' : '-', 1510 1.1 christos (p->p_flags & PF_W) != 0 ? 'w' : '-', 1511 1.1 christos (p->p_flags & PF_X) != 0 ? 'x' : '-'); 1512 1.1 christos if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) 1513 1.1 christos fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); 1514 1.1 christos fprintf (f, "\n"); 1515 1.1 christos } 1516 1.1 christos } 1517 1.1 christos 1518 1.13 christos s = bfd_get_section_by_name (abfd, ".dynamic"); 1519 1.1 christos if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0) 1520 1.1 christos { 1521 1.1 christos unsigned int elfsec; 1522 1.1 christos unsigned long shlink; 1523 1.1 christos bfd_byte *extdyn, *extdynend; 1524 1.1 christos size_t extdynsize; 1525 1.1 christos void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); 1526 1.1 christos 1527 1.1 christos fprintf (f, _("\nDynamic Section:\n")); 1528 1.14 christos 1529 1.1 christos if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf)) 1530 1.1 christos goto error_return; 1531 1.1 christos 1532 1.1 christos elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 1533 1.1 christos if (elfsec == SHN_BAD) 1534 1.1 christos goto error_return; 1535 1.1 christos shlink = elf_elfsections (abfd)[elfsec]->sh_link; 1536 1.1 christos 1537 1.1 christos extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; 1538 1.1 christos swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; 1539 1.13 christos 1540 1.13 christos for (extdyn = dynbuf, extdynend = dynbuf + s->size; 1541 1.13 christos (size_t) (extdynend - extdyn) >= extdynsize; 1542 1.1 christos extdyn += extdynsize) 1543 1.1 christos { 1544 1.1 christos Elf_Internal_Dyn dyn; 1545 1.1 christos const char *name = ""; 1546 1.12 christos char ab[20]; 1547 1.1 christos bool stringp; 1548 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 1549 1.1 christos 1550 1.1 christos (*swap_dyn_in) (abfd, extdyn, &dyn); 1551 1.1 christos 1552 1.1 christos if (dyn.d_tag == DT_NULL) 1553 1.1 christos break; 1554 1.12 christos 1555 1.1 christos stringp = false; 1556 1.1 christos switch (dyn.d_tag) 1557 1.1 christos { 1558 1.1 christos default: 1559 1.1 christos if (bed->elf_backend_get_target_dtag) 1560 1.1 christos name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); 1561 1.1 christos 1562 1.1 christos if (!strcmp (name, "")) 1563 1.13 christos { 1564 1.1 christos sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag); 1565 1.1 christos name = ab; 1566 1.1 christos } 1567 1.1 christos break; 1568 1.12 christos 1569 1.1 christos case DT_NEEDED: name = "NEEDED"; stringp = true; break; 1570 1.1 christos case DT_PLTRELSZ: name = "PLTRELSZ"; break; 1571 1.1 christos case DT_PLTGOT: name = "PLTGOT"; break; 1572 1.1 christos case DT_HASH: name = "HASH"; break; 1573 1.1 christos case DT_STRTAB: name = "STRTAB"; break; 1574 1.1 christos case DT_SYMTAB: name = "SYMTAB"; break; 1575 1.1 christos case DT_RELA: name = "RELA"; break; 1576 1.1 christos case DT_RELASZ: name = "RELASZ"; break; 1577 1.1 christos case DT_RELAENT: name = "RELAENT"; break; 1578 1.1 christos case DT_STRSZ: name = "STRSZ"; break; 1579 1.1 christos case DT_SYMENT: name = "SYMENT"; break; 1580 1.1 christos case DT_INIT: name = "INIT"; break; 1581 1.12 christos case DT_FINI: name = "FINI"; break; 1582 1.12 christos case DT_SONAME: name = "SONAME"; stringp = true; break; 1583 1.1 christos case DT_RPATH: name = "RPATH"; stringp = true; break; 1584 1.1 christos case DT_SYMBOLIC: name = "SYMBOLIC"; break; 1585 1.1 christos case DT_REL: name = "REL"; break; 1586 1.1 christos case DT_RELSZ: name = "RELSZ"; break; 1587 1.12 christos case DT_RELENT: name = "RELENT"; break; 1588 1.12 christos case DT_RELR: name = "RELR"; break; 1589 1.12 christos case DT_RELRSZ: name = "RELRSZ"; break; 1590 1.1 christos case DT_RELRENT: name = "RELRENT"; break; 1591 1.1 christos case DT_PLTREL: name = "PLTREL"; break; 1592 1.1 christos case DT_DEBUG: name = "DEBUG"; break; 1593 1.1 christos case DT_TEXTREL: name = "TEXTREL"; break; 1594 1.1 christos case DT_JMPREL: name = "JMPREL"; break; 1595 1.1 christos case DT_BIND_NOW: name = "BIND_NOW"; break; 1596 1.1 christos case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; 1597 1.1 christos case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; 1598 1.1 christos case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; 1599 1.12 christos case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; 1600 1.1 christos case DT_RUNPATH: name = "RUNPATH"; stringp = true; break; 1601 1.1 christos case DT_FLAGS: name = "FLAGS"; break; 1602 1.1 christos case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; 1603 1.1 christos case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; 1604 1.1 christos case DT_CHECKSUM: name = "CHECKSUM"; break; 1605 1.1 christos case DT_PLTPADSZ: name = "PLTPADSZ"; break; 1606 1.1 christos case DT_MOVEENT: name = "MOVEENT"; break; 1607 1.1 christos case DT_MOVESZ: name = "MOVESZ"; break; 1608 1.1 christos case DT_FEATURE: name = "FEATURE"; break; 1609 1.1 christos case DT_POSFLAG_1: name = "POSFLAG_1"; break; 1610 1.1 christos case DT_SYMINSZ: name = "SYMINSZ"; break; 1611 1.12 christos case DT_SYMINENT: name = "SYMINENT"; break; 1612 1.12 christos case DT_CONFIG: name = "CONFIG"; stringp = true; break; 1613 1.12 christos case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break; 1614 1.1 christos case DT_AUDIT: name = "AUDIT"; stringp = true; break; 1615 1.1 christos case DT_PLTPAD: name = "PLTPAD"; break; 1616 1.1 christos case DT_MOVETAB: name = "MOVETAB"; break; 1617 1.1 christos case DT_SYMINFO: name = "SYMINFO"; break; 1618 1.1 christos case DT_RELACOUNT: name = "RELACOUNT"; break; 1619 1.1 christos case DT_RELCOUNT: name = "RELCOUNT"; break; 1620 1.1 christos case DT_FLAGS_1: name = "FLAGS_1"; break; 1621 1.1 christos case DT_VERSYM: name = "VERSYM"; break; 1622 1.1 christos case DT_VERDEF: name = "VERDEF"; break; 1623 1.1 christos case DT_VERDEFNUM: name = "VERDEFNUM"; break; 1624 1.1 christos case DT_VERNEED: name = "VERNEED"; break; 1625 1.12 christos case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; 1626 1.1 christos case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break; 1627 1.12 christos case DT_USED: name = "USED"; break; 1628 1.1 christos case DT_FILTER: name = "FILTER"; stringp = true; break; 1629 1.1 christos case DT_GNU_HASH: name = "GNU_HASH"; break; 1630 1.1 christos } 1631 1.1 christos 1632 1.1 christos fprintf (f, " %-20s ", name); 1633 1.1 christos if (! stringp) 1634 1.1 christos { 1635 1.1 christos fprintf (f, "0x"); 1636 1.1 christos bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); 1637 1.1 christos } 1638 1.1 christos else 1639 1.1 christos { 1640 1.1 christos const char *string; 1641 1.1 christos unsigned int tagv = dyn.d_un.d_val; 1642 1.1 christos 1643 1.1 christos string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 1644 1.1 christos if (string == NULL) 1645 1.1 christos goto error_return; 1646 1.1 christos fprintf (f, "%s", string); 1647 1.1 christos } 1648 1.1 christos fprintf (f, "\n"); 1649 1.1 christos } 1650 1.14 christos 1651 1.1 christos _bfd_elf_munmap_section_contents (s, dynbuf); 1652 1.1 christos dynbuf = NULL; 1653 1.1 christos } 1654 1.1 christos 1655 1.1 christos if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) 1656 1.1 christos || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) 1657 1.12 christos { 1658 1.12 christos if (! _bfd_elf_slurp_version_tables (abfd, false)) 1659 1.1 christos return false; 1660 1.1 christos } 1661 1.1 christos 1662 1.1 christos if (elf_dynverdef (abfd) != 0) 1663 1.1 christos { 1664 1.1 christos Elf_Internal_Verdef *t; 1665 1.1 christos 1666 1.1 christos fprintf (f, _("\nVersion definitions:\n")); 1667 1.1 christos for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) 1668 1.1 christos { 1669 1.1 christos fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, 1670 1.1 christos t->vd_flags, t->vd_hash, 1671 1.1 christos t->vd_nodename ? t->vd_nodename : "<corrupt>"); 1672 1.1 christos if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) 1673 1.1 christos { 1674 1.1 christos Elf_Internal_Verdaux *a; 1675 1.1 christos 1676 1.1 christos fprintf (f, "\t"); 1677 1.1 christos for (a = t->vd_auxptr->vda_nextptr; 1678 1.1 christos a != NULL; 1679 1.1 christos a = a->vda_nextptr) 1680 1.1 christos fprintf (f, "%s ", 1681 1.1 christos a->vda_nodename ? a->vda_nodename : "<corrupt>"); 1682 1.1 christos fprintf (f, "\n"); 1683 1.1 christos } 1684 1.1 christos } 1685 1.1 christos } 1686 1.1 christos 1687 1.1 christos if (elf_dynverref (abfd) != 0) 1688 1.1 christos { 1689 1.1 christos Elf_Internal_Verneed *t; 1690 1.1 christos 1691 1.1 christos fprintf (f, _("\nVersion References:\n")); 1692 1.1 christos for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) 1693 1.1 christos { 1694 1.1 christos Elf_Internal_Vernaux *a; 1695 1.1 christos 1696 1.1 christos fprintf (f, _(" required from %s:\n"), 1697 1.1 christos t->vn_filename ? t->vn_filename : "<corrupt>"); 1698 1.1 christos for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 1699 1.1 christos fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, 1700 1.1 christos a->vna_flags, a->vna_other, 1701 1.1 christos a->vna_nodename ? a->vna_nodename : "<corrupt>"); 1702 1.1 christos } 1703 1.1 christos } 1704 1.12 christos 1705 1.1 christos return true; 1706 1.1 christos 1707 1.14 christos error_return: 1708 1.12 christos _bfd_elf_munmap_section_contents (s, dynbuf); 1709 1.1 christos return false; 1710 1.1 christos } 1711 1.13 christos 1712 1.13 christos /* Find the file offset corresponding to VMA by using the program 1713 1.13 christos headers. */ 1714 1.13 christos 1715 1.13 christos static file_ptr 1716 1.13 christos offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma, 1717 1.13 christos size_t size, size_t *max_size_p) 1718 1.13 christos { 1719 1.13 christos Elf_Internal_Phdr *seg; 1720 1.13 christos size_t i; 1721 1.13 christos 1722 1.13 christos for (seg = phdrs, i = 0; i < phnum; ++seg, ++i) 1723 1.13 christos if (seg->p_type == PT_LOAD 1724 1.13 christos && vma >= (seg->p_vaddr & -seg->p_align) 1725 1.13 christos && vma + size <= seg->p_vaddr + seg->p_filesz) 1726 1.13 christos { 1727 1.13 christos if (max_size_p) 1728 1.13 christos *max_size_p = seg->p_vaddr + seg->p_filesz - vma; 1729 1.13 christos return vma - seg->p_vaddr + seg->p_offset; 1730 1.13 christos } 1731 1.13 christos 1732 1.13 christos if (max_size_p) 1733 1.13 christos *max_size_p = 0; 1734 1.13 christos bfd_set_error (bfd_error_invalid_operation); 1735 1.13 christos return (file_ptr) -1; 1736 1.13 christos } 1737 1.13 christos 1738 1.13 christos /* Convert hash table to internal form. */ 1739 1.13 christos 1740 1.13 christos static bfd_vma * 1741 1.13 christos get_hash_table_data (bfd *abfd, bfd_size_type number, 1742 1.13 christos unsigned int ent_size, bfd_size_type filesize) 1743 1.13 christos { 1744 1.13 christos unsigned char *e_data = NULL; 1745 1.13 christos bfd_vma *i_data = NULL; 1746 1.14 christos bfd_size_type size; 1747 1.14 christos void *e_data_addr; 1748 1.13 christos size_t e_data_size ATTRIBUTE_UNUSED; 1749 1.13 christos 1750 1.13 christos if (ent_size != 4 && ent_size != 8) 1751 1.13 christos return NULL; 1752 1.13 christos 1753 1.13 christos if ((size_t) number != number) 1754 1.13 christos { 1755 1.13 christos bfd_set_error (bfd_error_file_too_big); 1756 1.13 christos return NULL; 1757 1.13 christos } 1758 1.13 christos 1759 1.13 christos size = ent_size * number; 1760 1.13 christos /* Be kind to memory checkers (eg valgrind, address sanitizer) by not 1761 1.13 christos attempting to allocate memory when the read is bound to fail. */ 1762 1.13 christos if (size > filesize 1763 1.13 christos || number >= ~(size_t) 0 / ent_size 1764 1.13 christos || number >= ~(size_t) 0 / sizeof (*i_data)) 1765 1.13 christos { 1766 1.13 christos bfd_set_error (bfd_error_file_too_big); 1767 1.13 christos return NULL; 1768 1.13 christos } 1769 1.14 christos 1770 1.13 christos e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size); 1771 1.13 christos if (e_data == NULL) 1772 1.13 christos return NULL; 1773 1.13 christos 1774 1.13 christos i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data)); 1775 1.13 christos if (i_data == NULL) 1776 1.14 christos { 1777 1.13 christos _bfd_munmap_temporary (e_data_addr, e_data_size); 1778 1.13 christos return NULL; 1779 1.13 christos } 1780 1.13 christos 1781 1.13 christos if (ent_size == 4) 1782 1.13 christos while (number--) 1783 1.13 christos i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size); 1784 1.13 christos else 1785 1.13 christos while (number--) 1786 1.13 christos i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size); 1787 1.14 christos 1788 1.13 christos _bfd_munmap_temporary (e_data_addr, e_data_size); 1789 1.13 christos return i_data; 1790 1.13 christos } 1791 1.13 christos 1792 1.13 christos /* Address of .MIPS.xhash section. FIXME: What is the best way to 1793 1.13 christos support DT_MIPS_XHASH? */ 1794 1.13 christos #define DT_MIPS_XHASH 0x70000036 1795 1.13 christos 1796 1.13 christos /* Reconstruct dynamic symbol table from PT_DYNAMIC segment. */ 1797 1.13 christos 1798 1.13 christos bool 1799 1.13 christos _bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr, 1800 1.13 christos Elf_Internal_Phdr *phdrs, size_t phnum, 1801 1.13 christos bfd_size_type filesize) 1802 1.13 christos { 1803 1.13 christos bfd_byte *extdyn, *extdynend; 1804 1.13 christos size_t extdynsize; 1805 1.13 christos void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); 1806 1.13 christos bool (*swap_symbol_in) (bfd *, const void *, const void *, 1807 1.13 christos Elf_Internal_Sym *); 1808 1.13 christos Elf_Internal_Dyn dyn; 1809 1.13 christos bfd_vma dt_hash = 0; 1810 1.13 christos bfd_vma dt_gnu_hash = 0; 1811 1.13 christos bfd_vma dt_mips_xhash = 0; 1812 1.13 christos bfd_vma dt_strtab = 0; 1813 1.13 christos bfd_vma dt_symtab = 0; 1814 1.13 christos size_t dt_strsz = 0; 1815 1.13 christos bfd_vma dt_versym = 0; 1816 1.13 christos bfd_vma dt_verdef = 0; 1817 1.13 christos bfd_vma dt_verneed = 0; 1818 1.13 christos bfd_byte *dynbuf = NULL; 1819 1.13 christos char *strbuf = NULL; 1820 1.13 christos bfd_vma *gnubuckets = NULL; 1821 1.13 christos bfd_vma *gnuchains = NULL; 1822 1.13 christos bfd_vma *mipsxlat = NULL; 1823 1.13 christos file_ptr saved_filepos, filepos; 1824 1.13 christos bool res = false; 1825 1.13 christos size_t amt; 1826 1.13 christos bfd_byte *esymbuf = NULL, *esym; 1827 1.13 christos bfd_size_type symcount; 1828 1.13 christos Elf_Internal_Sym *isymbuf = NULL; 1829 1.13 christos Elf_Internal_Sym *isym, *isymend; 1830 1.13 christos bfd_byte *versym = NULL; 1831 1.13 christos bfd_byte *verdef = NULL; 1832 1.13 christos bfd_byte *verneed = NULL; 1833 1.13 christos size_t verdef_size = 0; 1834 1.13 christos size_t verneed_size = 0; 1835 1.13 christos size_t extsym_size; 1836 1.14 christos const struct elf_backend_data *bed; 1837 1.14 christos void *dynbuf_addr = NULL; 1838 1.14 christos void *esymbuf_addr = NULL; 1839 1.14 christos size_t dynbuf_size = 0; 1840 1.13 christos size_t esymbuf_size = 0; 1841 1.13 christos 1842 1.13 christos /* Return TRUE if symbol table is bad. */ 1843 1.13 christos if (elf_bad_symtab (abfd)) 1844 1.13 christos return true; 1845 1.13 christos 1846 1.13 christos /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before. */ 1847 1.13 christos if (elf_tdata (abfd)->dt_strtab != NULL) 1848 1.13 christos return true; 1849 1.13 christos 1850 1.13 christos bed = get_elf_backend_data (abfd); 1851 1.13 christos 1852 1.13 christos /* Save file position for elf_object_p. */ 1853 1.13 christos saved_filepos = bfd_tell (abfd); 1854 1.13 christos 1855 1.13 christos if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0) 1856 1.13 christos goto error_return; 1857 1.14 christos 1858 1.14 christos dynbuf_size = phdr->p_filesz; 1859 1.13 christos dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size); 1860 1.13 christos if (dynbuf == NULL) 1861 1.13 christos goto error_return; 1862 1.13 christos 1863 1.13 christos extsym_size = bed->s->sizeof_sym; 1864 1.13 christos extdynsize = bed->s->sizeof_dyn; 1865 1.13 christos swap_dyn_in = bed->s->swap_dyn_in; 1866 1.13 christos 1867 1.13 christos extdyn = dynbuf; 1868 1.13 christos if (phdr->p_filesz < extdynsize) 1869 1.13 christos goto error_return; 1870 1.13 christos extdynend = extdyn + phdr->p_filesz; 1871 1.13 christos for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) 1872 1.13 christos { 1873 1.13 christos swap_dyn_in (abfd, extdyn, &dyn); 1874 1.13 christos 1875 1.13 christos if (dyn.d_tag == DT_NULL) 1876 1.13 christos break; 1877 1.13 christos 1878 1.13 christos switch (dyn.d_tag) 1879 1.13 christos { 1880 1.13 christos case DT_HASH: 1881 1.13 christos dt_hash = dyn.d_un.d_val; 1882 1.13 christos break; 1883 1.13 christos case DT_GNU_HASH: 1884 1.13 christos if (bed->elf_machine_code != EM_MIPS 1885 1.13 christos && bed->elf_machine_code != EM_MIPS_RS3_LE) 1886 1.13 christos dt_gnu_hash = dyn.d_un.d_val; 1887 1.13 christos break; 1888 1.13 christos case DT_STRTAB: 1889 1.13 christos dt_strtab = dyn.d_un.d_val; 1890 1.13 christos break; 1891 1.13 christos case DT_SYMTAB: 1892 1.13 christos dt_symtab = dyn.d_un.d_val; 1893 1.13 christos break; 1894 1.13 christos case DT_STRSZ: 1895 1.13 christos dt_strsz = dyn.d_un.d_val; 1896 1.13 christos break; 1897 1.13 christos case DT_SYMENT: 1898 1.13 christos if (dyn.d_un.d_val != extsym_size) 1899 1.13 christos goto error_return; 1900 1.13 christos break; 1901 1.13 christos case DT_VERSYM: 1902 1.13 christos dt_versym = dyn.d_un.d_val; 1903 1.13 christos break; 1904 1.13 christos case DT_VERDEF: 1905 1.13 christos dt_verdef = dyn.d_un.d_val; 1906 1.13 christos break; 1907 1.13 christos case DT_VERNEED: 1908 1.13 christos dt_verneed = dyn.d_un.d_val; 1909 1.13 christos break; 1910 1.13 christos default: 1911 1.13 christos if (dyn.d_tag == DT_MIPS_XHASH 1912 1.13 christos && (bed->elf_machine_code == EM_MIPS 1913 1.13 christos || bed->elf_machine_code == EM_MIPS_RS3_LE)) 1914 1.13 christos { 1915 1.13 christos dt_gnu_hash = dyn.d_un.d_val; 1916 1.13 christos dt_mips_xhash = dyn.d_un.d_val; 1917 1.13 christos } 1918 1.13 christos break; 1919 1.13 christos } 1920 1.13 christos } 1921 1.13 christos 1922 1.13 christos /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC 1923 1.13 christos segment. */ 1924 1.13 christos if ((!dt_hash && !dt_gnu_hash) 1925 1.13 christos || !dt_strtab 1926 1.13 christos || !dt_symtab 1927 1.13 christos || !dt_strsz) 1928 1.13 christos goto error_return; 1929 1.13 christos 1930 1.13 christos /* Get dynamic string table. */ 1931 1.13 christos filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL); 1932 1.13 christos if (filepos == (file_ptr) -1 1933 1.13 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 1934 1.13 christos goto error_return; 1935 1.13 christos 1936 1.14 christos /* Dynamic string table must be valid until ABFD is closed. */ 1937 1.13 christos strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz); 1938 1.13 christos if (strbuf == NULL) 1939 1.14 christos goto error_return; 1940 1.14 christos if (strbuf[dt_strsz - 1] != 0) 1941 1.14 christos { 1942 1.14 christos /* It is an error if a string table is't terminated. */ 1943 1.14 christos _bfd_error_handler 1944 1.14 christos /* xgettext:c-format */ 1945 1.14 christos (_("%pB: DT_STRTAB table is corrupt"), abfd); 1946 1.14 christos strbuf[dt_strsz - 1] = 0; 1947 1.13 christos } 1948 1.13 christos 1949 1.13 christos /* Get the real symbol count from DT_HASH or DT_GNU_HASH. Prefer 1950 1.13 christos DT_HASH since it is simpler than DT_GNU_HASH. */ 1951 1.13 christos if (dt_hash) 1952 1.13 christos { 1953 1.13 christos unsigned char nb[16]; 1954 1.13 christos unsigned int hash_ent_size; 1955 1.13 christos 1956 1.13 christos switch (bed->elf_machine_code) 1957 1.13 christos { 1958 1.13 christos case EM_ALPHA: 1959 1.13 christos case EM_S390: 1960 1.13 christos case EM_S390_OLD: 1961 1.13 christos if (bed->s->elfclass == ELFCLASS64) 1962 1.13 christos { 1963 1.13 christos hash_ent_size = 8; 1964 1.13 christos break; 1965 1.13 christos } 1966 1.13 christos /* FALLTHROUGH */ 1967 1.13 christos default: 1968 1.13 christos hash_ent_size = 4; 1969 1.13 christos break; 1970 1.13 christos } 1971 1.14 christos 1972 1.13 christos filepos = offset_from_vma (phdrs, phnum, dt_hash, 2 * hash_ent_size, 1973 1.13 christos NULL); 1974 1.13 christos if (filepos == (file_ptr) -1 1975 1.13 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0 1976 1.13 christos || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size) 1977 1.13 christos goto error_return; 1978 1.13 christos 1979 1.13 christos /* The number of dynamic symbol table entries equals the number 1980 1.13 christos of chains. */ 1981 1.13 christos if (hash_ent_size == 8) 1982 1.13 christos symcount = bfd_get_64 (abfd, nb + hash_ent_size); 1983 1.13 christos else 1984 1.13 christos symcount = bfd_get_32 (abfd, nb + hash_ent_size); 1985 1.13 christos } 1986 1.13 christos else 1987 1.13 christos { 1988 1.13 christos /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL 1989 1.13 christos bindings are in hash table. Since in dynamic symbol table, 1990 1.13 christos all symbols with STB_LOCAL binding are placed before symbols 1991 1.13 christos with other bindings and all undefined symbols are placed 1992 1.13 christos before defined ones, the highest symbol index in DT_GNU_HASH 1993 1.13 christos is the highest dynamic symbol table index. */ 1994 1.13 christos unsigned char nb[16]; 1995 1.13 christos bfd_vma ngnubuckets; 1996 1.13 christos bfd_vma gnusymidx; 1997 1.13 christos size_t i, ngnuchains; 1998 1.13 christos bfd_vma maxchain = 0xffffffff, bitmaskwords; 1999 1.13 christos bfd_vma buckets_vma; 2000 1.13 christos 2001 1.13 christos filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash, 2002 1.13 christos sizeof (nb), NULL); 2003 1.13 christos if (filepos == (file_ptr) -1 2004 1.13 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0 2005 1.13 christos || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb)) 2006 1.13 christos goto error_return; 2007 1.13 christos 2008 1.13 christos ngnubuckets = bfd_get_32 (abfd, nb); 2009 1.13 christos gnusymidx = bfd_get_32 (abfd, nb + 4); 2010 1.13 christos bitmaskwords = bfd_get_32 (abfd, nb + 8); 2011 1.13 christos buckets_vma = dt_gnu_hash + 16; 2012 1.13 christos if (bed->s->elfclass == ELFCLASS32) 2013 1.13 christos buckets_vma += bitmaskwords * 4; 2014 1.13 christos else 2015 1.13 christos buckets_vma += bitmaskwords * 8; 2016 1.13 christos filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL); 2017 1.13 christos if (filepos == (file_ptr) -1 2018 1.13 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2019 1.13 christos goto error_return; 2020 1.13 christos 2021 1.13 christos gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize); 2022 1.13 christos if (gnubuckets == NULL) 2023 1.13 christos goto error_return; 2024 1.13 christos 2025 1.13 christos for (i = 0; i < ngnubuckets; i++) 2026 1.13 christos if (gnubuckets[i] != 0) 2027 1.13 christos { 2028 1.13 christos if (gnubuckets[i] < gnusymidx) 2029 1.13 christos goto error_return; 2030 1.13 christos 2031 1.13 christos if (maxchain == 0xffffffff || gnubuckets[i] > maxchain) 2032 1.13 christos maxchain = gnubuckets[i]; 2033 1.13 christos } 2034 1.13 christos 2035 1.13 christos if (maxchain == 0xffffffff) 2036 1.13 christos { 2037 1.13 christos symcount = 0; 2038 1.13 christos goto empty_gnu_hash; 2039 1.13 christos } 2040 1.13 christos 2041 1.13 christos maxchain -= gnusymidx; 2042 1.14 christos filepos = offset_from_vma (phdrs, phnum, 2043 1.13 christos buckets_vma + 4 * (ngnubuckets + maxchain), 2044 1.13 christos 4, NULL); 2045 1.13 christos if (filepos == (file_ptr) -1 2046 1.13 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2047 1.13 christos goto error_return; 2048 1.13 christos 2049 1.13 christos do 2050 1.13 christos { 2051 1.13 christos if (bfd_read (nb, 4, abfd) != 4) 2052 1.13 christos goto error_return; 2053 1.13 christos ++maxchain; 2054 1.13 christos if (maxchain == 0) 2055 1.13 christos goto error_return; 2056 1.13 christos } 2057 1.13 christos while ((bfd_get_32 (abfd, nb) & 1) == 0); 2058 1.13 christos 2059 1.14 christos filepos = offset_from_vma (phdrs, phnum, 2060 1.13 christos buckets_vma + 4 * ngnubuckets, 2061 1.13 christos 4, NULL); 2062 1.13 christos if (filepos == (file_ptr) -1 2063 1.13 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2064 1.13 christos goto error_return; 2065 1.13 christos 2066 1.13 christos gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize); 2067 1.13 christos if (gnuchains == NULL) 2068 1.13 christos goto error_return; 2069 1.13 christos ngnuchains = maxchain; 2070 1.13 christos 2071 1.13 christos if (dt_mips_xhash) 2072 1.13 christos { 2073 1.14 christos filepos = offset_from_vma (phdrs, phnum, 2074 1.13 christos buckets_vma + 4 * (ngnubuckets + maxchain), 2075 1.13 christos 4, NULL); 2076 1.13 christos if (filepos == (file_ptr) -1 2077 1.13 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2078 1.13 christos goto error_return; 2079 1.13 christos 2080 1.13 christos mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize); 2081 1.13 christos if (mipsxlat == NULL) 2082 1.13 christos goto error_return; 2083 1.13 christos } 2084 1.13 christos 2085 1.13 christos symcount = 0; 2086 1.13 christos for (i = 0; i < ngnubuckets; ++i) 2087 1.13 christos if (gnubuckets[i] != 0) 2088 1.13 christos { 2089 1.13 christos bfd_vma si = gnubuckets[i]; 2090 1.13 christos bfd_vma off = si - gnusymidx; 2091 1.13 christos do 2092 1.13 christos { 2093 1.13 christos if (mipsxlat) 2094 1.13 christos { 2095 1.13 christos if (mipsxlat[off] >= symcount) 2096 1.13 christos symcount = mipsxlat[off] + 1; 2097 1.13 christos } 2098 1.13 christos else 2099 1.13 christos { 2100 1.13 christos if (si >= symcount) 2101 1.13 christos symcount = si + 1; 2102 1.13 christos } 2103 1.13 christos si++; 2104 1.13 christos } 2105 1.13 christos while (off < ngnuchains && (gnuchains[off++] & 1) == 0); 2106 1.13 christos } 2107 1.13 christos } 2108 1.13 christos 2109 1.13 christos /* Swap in dynamic symbol table. */ 2110 1.13 christos if (_bfd_mul_overflow (symcount, extsym_size, &amt)) 2111 1.13 christos { 2112 1.13 christos bfd_set_error (bfd_error_file_too_big); 2113 1.13 christos goto error_return; 2114 1.13 christos } 2115 1.13 christos 2116 1.13 christos filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL); 2117 1.13 christos if (filepos == (file_ptr) -1 2118 1.13 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2119 1.14 christos goto error_return; 2120 1.14 christos esymbuf_size = amt; 2121 1.14 christos esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size, 2122 1.13 christos &esymbuf_addr, &esymbuf_size); 2123 1.13 christos if (esymbuf == NULL) 2124 1.13 christos goto error_return; 2125 1.13 christos 2126 1.13 christos if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt)) 2127 1.13 christos { 2128 1.13 christos bfd_set_error (bfd_error_file_too_big); 2129 1.13 christos goto error_return; 2130 1.13 christos } 2131 1.13 christos 2132 1.13 christos /* Dynamic symbol table must be valid until ABFD is closed. */ 2133 1.13 christos isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt); 2134 1.13 christos if (isymbuf == NULL) 2135 1.13 christos goto error_return; 2136 1.13 christos 2137 1.13 christos swap_symbol_in = bed->s->swap_symbol_in; 2138 1.13 christos 2139 1.13 christos /* Convert the symbols to internal form. */ 2140 1.13 christos isymend = isymbuf + symcount; 2141 1.13 christos for (esym = esymbuf, isym = isymbuf; 2142 1.13 christos isym < isymend; 2143 1.13 christos esym += extsym_size, isym++) 2144 1.13 christos if (!swap_symbol_in (abfd, esym, NULL, isym) 2145 1.13 christos || isym->st_name >= dt_strsz) 2146 1.13 christos { 2147 1.13 christos bfd_set_error (bfd_error_invalid_operation); 2148 1.13 christos goto error_return; 2149 1.13 christos } 2150 1.13 christos 2151 1.13 christos if (dt_versym) 2152 1.13 christos { 2153 1.13 christos /* Swap in DT_VERSYM. */ 2154 1.13 christos if (_bfd_mul_overflow (symcount, 2, &amt)) 2155 1.13 christos { 2156 1.13 christos bfd_set_error (bfd_error_file_too_big); 2157 1.13 christos goto error_return; 2158 1.13 christos } 2159 1.13 christos 2160 1.13 christos filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL); 2161 1.13 christos if (filepos == (file_ptr) -1 2162 1.13 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2163 1.13 christos goto error_return; 2164 1.13 christos 2165 1.14 christos /* DT_VERSYM info must be valid until ABFD is closed. */ 2166 1.13 christos versym = _bfd_mmap_persistent (abfd, amt); 2167 1.13 christos 2168 1.13 christos if (dt_verdef) 2169 1.13 christos { 2170 1.13 christos /* Read in DT_VERDEF. */ 2171 1.13 christos filepos = offset_from_vma (phdrs, phnum, dt_verdef, 2172 1.13 christos 0, &verdef_size); 2173 1.13 christos if (filepos == (file_ptr) -1 2174 1.13 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2175 1.13 christos goto error_return; 2176 1.13 christos 2177 1.14 christos /* DT_VERDEF info must be valid until ABFD is closed. */ 2178 1.13 christos verdef = _bfd_mmap_persistent (abfd, verdef_size); 2179 1.13 christos } 2180 1.13 christos 2181 1.13 christos if (dt_verneed) 2182 1.13 christos { 2183 1.13 christos /* Read in DT_VERNEED. */ 2184 1.13 christos filepos = offset_from_vma (phdrs, phnum, dt_verneed, 2185 1.13 christos 0, &verneed_size); 2186 1.13 christos if (filepos == (file_ptr) -1 2187 1.13 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2188 1.13 christos goto error_return; 2189 1.13 christos 2190 1.14 christos /* DT_VERNEED info must be valid until ABFD is closed. */ 2191 1.13 christos verneed = _bfd_mmap_persistent (abfd, verneed_size); 2192 1.13 christos } 2193 1.13 christos } 2194 1.13 christos 2195 1.13 christos empty_gnu_hash: 2196 1.13 christos elf_tdata (abfd)->dt_strtab = strbuf; 2197 1.13 christos elf_tdata (abfd)->dt_strsz = dt_strsz; 2198 1.13 christos elf_tdata (abfd)->dt_symtab = isymbuf; 2199 1.13 christos elf_tdata (abfd)->dt_symtab_count = symcount; 2200 1.13 christos elf_tdata (abfd)->dt_versym = versym; 2201 1.13 christos elf_tdata (abfd)->dt_verdef = verdef; 2202 1.13 christos elf_tdata (abfd)->dt_verneed = verneed; 2203 1.13 christos elf_tdata (abfd)->dt_verdef_count 2204 1.13 christos = verdef_size / sizeof (Elf_External_Verdef); 2205 1.13 christos elf_tdata (abfd)->dt_verneed_count 2206 1.13 christos = verneed_size / sizeof (Elf_External_Verneed); 2207 1.13 christos 2208 1.13 christos res = true; 2209 1.13 christos 2210 1.13 christos error_return: 2211 1.13 christos /* Restore file position for elf_object_p. */ 2212 1.13 christos if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0) 2213 1.14 christos res = false; 2214 1.14 christos _bfd_munmap_temporary (dynbuf_addr, dynbuf_size); 2215 1.13 christos _bfd_munmap_temporary (esymbuf_addr, esymbuf_size); 2216 1.13 christos free (gnubuckets); 2217 1.13 christos free (gnuchains); 2218 1.13 christos free (mipsxlat); 2219 1.13 christos return res; 2220 1.13 christos } 2221 1.13 christos 2222 1.13 christos /* Reconstruct section from dynamic symbol. */ 2223 1.13 christos 2224 1.13 christos asection * 2225 1.13 christos _bfd_elf_get_section_from_dynamic_symbol (bfd *abfd, 2226 1.13 christos Elf_Internal_Sym *isym) 2227 1.13 christos { 2228 1.13 christos asection *sec; 2229 1.13 christos flagword flags; 2230 1.13 christos 2231 1.13 christos if (!elf_use_dt_symtab_p (abfd)) 2232 1.13 christos return NULL; 2233 1.13 christos 2234 1.13 christos flags = SEC_ALLOC | SEC_LOAD; 2235 1.13 christos switch (ELF_ST_TYPE (isym->st_info)) 2236 1.13 christos { 2237 1.13 christos case STT_FUNC: 2238 1.13 christos case STT_GNU_IFUNC: 2239 1.13 christos sec = bfd_get_section_by_name (abfd, ".text"); 2240 1.13 christos if (sec == NULL) 2241 1.13 christos sec = bfd_make_section_with_flags (abfd, 2242 1.13 christos ".text", 2243 1.13 christos flags | SEC_CODE); 2244 1.13 christos break; 2245 1.13 christos case STT_COMMON: 2246 1.13 christos sec = bfd_com_section_ptr; 2247 1.13 christos break; 2248 1.13 christos case STT_OBJECT: 2249 1.13 christos sec = bfd_get_section_by_name (abfd, ".data"); 2250 1.13 christos if (sec == NULL) 2251 1.13 christos sec = bfd_make_section_with_flags (abfd, 2252 1.13 christos ".data", 2253 1.13 christos flags | SEC_DATA); 2254 1.13 christos break; 2255 1.13 christos case STT_TLS: 2256 1.13 christos sec = bfd_get_section_by_name (abfd, ".tdata"); 2257 1.13 christos if (sec == NULL) 2258 1.13 christos sec = bfd_make_section_with_flags (abfd, 2259 1.13 christos ".tdata", 2260 1.13 christos (flags 2261 1.13 christos | SEC_DATA 2262 1.13 christos | SEC_THREAD_LOCAL)); 2263 1.13 christos break; 2264 1.13 christos default: 2265 1.13 christos sec = bfd_abs_section_ptr; 2266 1.13 christos break; 2267 1.13 christos } 2268 1.13 christos 2269 1.13 christos return sec; 2270 1.13 christos } 2271 1.12 christos 2272 1.12 christos /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE 2273 1.3 christos and return symbol version for symbol version itself. */ 2274 1.3 christos 2275 1.3 christos const char * 2276 1.12 christos _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, 2277 1.12 christos bool base_p, 2278 1.3 christos bool *hidden) 2279 1.3 christos { 2280 1.13 christos const char *version_string = NULL; 2281 1.13 christos if ((elf_dynversym (abfd) != 0 2282 1.13 christos && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) 2283 1.13 christos || (elf_tdata (abfd)->dt_versym != NULL 2284 1.13 christos && (elf_tdata (abfd)->dt_verdef != NULL 2285 1.3 christos || elf_tdata (abfd)->dt_verneed != NULL))) 2286 1.3 christos { 2287 1.3 christos unsigned int vernum = ((elf_symbol_type *) symbol)->version; 2288 1.3 christos 2289 1.3 christos *hidden = (vernum & VERSYM_HIDDEN) != 0; 2290 1.3 christos vernum &= VERSYM_VERSION; 2291 1.3 christos 2292 1.3 christos if (vernum == 0) 2293 1.9 christos version_string = ""; 2294 1.9 christos else if (vernum == 1 2295 1.9 christos && (vernum > elf_tdata (abfd)->cverdefs 2296 1.9 christos || (elf_tdata (abfd)->verdef[0].vd_flags 2297 1.12 christos == VER_FLG_BASE))) 2298 1.3 christos version_string = base_p ? "Base" : ""; 2299 1.12 christos else if (vernum <= elf_tdata (abfd)->cverdefs) 2300 1.12 christos { 2301 1.12 christos const char *nodename 2302 1.12 christos = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; 2303 1.12 christos version_string = ""; 2304 1.12 christos if (base_p 2305 1.12 christos || nodename == NULL 2306 1.12 christos || symbol->name == NULL 2307 1.12 christos || strcmp (symbol->name, nodename) != 0) 2308 1.12 christos version_string = nodename; 2309 1.3 christos } 2310 1.3 christos else 2311 1.3 christos { 2312 1.3 christos Elf_Internal_Verneed *t; 2313 1.10 christos 2314 1.3 christos version_string = _("<corrupt>"); 2315 1.3 christos for (t = elf_tdata (abfd)->verref; 2316 1.3 christos t != NULL; 2317 1.3 christos t = t->vn_nextref) 2318 1.3 christos { 2319 1.3 christos Elf_Internal_Vernaux *a; 2320 1.3 christos 2321 1.3 christos for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 2322 1.3 christos { 2323 1.3 christos if (a->vna_other == vernum) 2324 1.12 christos { 2325 1.3 christos *hidden = true; 2326 1.3 christos version_string = a->vna_nodename; 2327 1.3 christos break; 2328 1.3 christos } 2329 1.3 christos } 2330 1.3 christos } 2331 1.3 christos } 2332 1.3 christos } 2333 1.3 christos return version_string; 2334 1.3 christos } 2335 1.1 christos 2336 1.1 christos /* Display ELF-specific fields of a symbol. */ 2337 1.1 christos 2338 1.1 christos void 2339 1.1 christos bfd_elf_print_symbol (bfd *abfd, 2340 1.1 christos void *filep, 2341 1.1 christos asymbol *symbol, 2342 1.1 christos bfd_print_symbol_type how) 2343 1.1 christos { 2344 1.14 christos FILE *file = (FILE *) filep; 2345 1.14 christos const char *symname = (symbol->name != bfd_symbol_error_name 2346 1.14 christos ? symbol->name : _("<corrupt>")); 2347 1.1 christos 2348 1.1 christos switch (how) 2349 1.1 christos { 2350 1.14 christos case bfd_print_symbol_name: 2351 1.1 christos fprintf (file, "%s", symname); 2352 1.1 christos break; 2353 1.1 christos case bfd_print_symbol_more: 2354 1.1 christos fprintf (file, "elf "); 2355 1.9 christos bfd_fprintf_vma (abfd, file, symbol->value); 2356 1.1 christos fprintf (file, " %x", symbol->flags); 2357 1.1 christos break; 2358 1.1 christos case bfd_print_symbol_all: 2359 1.1 christos { 2360 1.1 christos const char *section_name; 2361 1.1 christos const char *name = NULL; 2362 1.1 christos const struct elf_backend_data *bed; 2363 1.1 christos unsigned char st_other; 2364 1.3 christos bfd_vma val; 2365 1.12 christos const char *version_string; 2366 1.1 christos bool hidden; 2367 1.1 christos 2368 1.1 christos section_name = symbol->section ? symbol->section->name : "(*none*)"; 2369 1.1 christos 2370 1.1 christos bed = get_elf_backend_data (abfd); 2371 1.1 christos if (bed->elf_backend_print_symbol_all) 2372 1.1 christos name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); 2373 1.14 christos 2374 1.14 christos if (name != NULL) 2375 1.14 christos symname = name; 2376 1.14 christos else 2377 1.1 christos bfd_print_symbol_vandf (abfd, file, symbol); 2378 1.1 christos 2379 1.1 christos fprintf (file, " %s\t", section_name); 2380 1.1 christos /* Print the "other" value for a symbol. For common symbols, 2381 1.1 christos we've already printed the size; now print the alignment. 2382 1.1 christos For other symbols, we have no specified alignment, and 2383 1.1 christos we've printed the address; now print the size. */ 2384 1.1 christos if (symbol->section && bfd_is_com_section (symbol->section)) 2385 1.1 christos val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; 2386 1.1 christos else 2387 1.1 christos val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; 2388 1.1 christos bfd_fprintf_vma (abfd, file, val); 2389 1.1 christos 2390 1.3 christos /* If we have version information, print it. */ 2391 1.3 christos version_string = _bfd_elf_get_symbol_version_string (abfd, 2392 1.12 christos symbol, 2393 1.3 christos true, 2394 1.3 christos &hidden); 2395 1.1 christos if (version_string) 2396 1.3 christos { 2397 1.1 christos if (!hidden) 2398 1.1 christos fprintf (file, " %-11s", version_string); 2399 1.1 christos else 2400 1.1 christos { 2401 1.1 christos int i; 2402 1.1 christos 2403 1.1 christos fprintf (file, " (%s)", version_string); 2404 1.1 christos for (i = 10 - strlen (version_string); i > 0; --i) 2405 1.1 christos putc (' ', file); 2406 1.1 christos } 2407 1.1 christos } 2408 1.1 christos 2409 1.1 christos /* If the st_other field is not zero, print it. */ 2410 1.1 christos st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; 2411 1.1 christos 2412 1.1 christos switch (st_other) 2413 1.1 christos { 2414 1.1 christos case 0: break; 2415 1.1 christos case STV_INTERNAL: fprintf (file, " .internal"); break; 2416 1.1 christos case STV_HIDDEN: fprintf (file, " .hidden"); break; 2417 1.1 christos case STV_PROTECTED: fprintf (file, " .protected"); break; 2418 1.1 christos default: 2419 1.1 christos /* Some other non-defined flags are also present, so print 2420 1.1 christos everything hex. */ 2421 1.1 christos fprintf (file, " 0x%02x", (unsigned int) st_other); 2422 1.1 christos } 2423 1.14 christos 2424 1.1 christos fprintf (file, " %s", symname); 2425 1.1 christos } 2426 1.1 christos break; 2427 1.1 christos } 2428 1.1 christos } 2429 1.1 christos 2430 1.1 christos /* ELF .o/exec file reading */ 2432 1.1 christos 2433 1.12 christos /* Create a new bfd section from an ELF section header. */ 2434 1.1 christos 2435 1.1 christos bool 2436 1.1 christos bfd_section_from_shdr (bfd *abfd, unsigned int shindex) 2437 1.1 christos { 2438 1.1 christos Elf_Internal_Shdr *hdr; 2439 1.1 christos Elf_Internal_Ehdr *ehdr; 2440 1.12 christos const struct elf_backend_data *bed; 2441 1.1 christos const char *name; 2442 1.1 christos bool ret = true; 2443 1.12 christos 2444 1.1 christos if (shindex >= elf_numsections (abfd)) 2445 1.12 christos return false; 2446 1.12 christos 2447 1.12 christos /* PR17512: A corrupt ELF binary might contain a loop of sections via 2448 1.12 christos sh_link or sh_info. Detect this here, by refusing to load a 2449 1.3 christos section that we are already in the process of loading. */ 2450 1.12 christos if (elf_tdata (abfd)->being_created[shindex]) 2451 1.12 christos { 2452 1.12 christos _bfd_error_handler 2453 1.3 christos (_("%pB: warning: loop in section dependencies detected"), abfd); 2454 1.12 christos return false; 2455 1.3 christos } 2456 1.1 christos elf_tdata (abfd)->being_created[shindex] = true; 2457 1.1 christos 2458 1.1 christos hdr = elf_elfsections (abfd)[shindex]; 2459 1.1 christos ehdr = elf_elfheader (abfd); 2460 1.1 christos name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, 2461 1.3 christos hdr->sh_name); 2462 1.1 christos if (name == NULL) 2463 1.1 christos goto fail; 2464 1.1 christos 2465 1.1 christos bed = get_elf_backend_data (abfd); 2466 1.1 christos switch (hdr->sh_type) 2467 1.1 christos { 2468 1.3 christos case SHT_NULL: 2469 1.1 christos /* Inactive section. Throw it away. */ 2470 1.3 christos goto success; 2471 1.3 christos 2472 1.3 christos case SHT_PROGBITS: /* Normal section with contents. */ 2473 1.3 christos case SHT_NOBITS: /* .bss section. */ 2474 1.1 christos case SHT_HASH: /* .hash section. */ 2475 1.1 christos case SHT_NOTE: /* .note section. */ 2476 1.1 christos case SHT_INIT_ARRAY: /* .init_array section. */ 2477 1.1 christos case SHT_FINI_ARRAY: /* .fini_array section. */ 2478 1.1 christos case SHT_PREINIT_ARRAY: /* .preinit_array section. */ 2479 1.14 christos case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ 2480 1.3 christos case SHT_GNU_HASH: /* .gnu.hash section. */ 2481 1.3 christos case SHT_GNU_SFRAME: /* .sframe section. */ 2482 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2483 1.1 christos goto success; 2484 1.1 christos 2485 1.3 christos case SHT_DYNAMIC: /* Dynamic linking information. */ 2486 1.3 christos if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 2487 1.1 christos goto fail; 2488 1.1 christos 2489 1.13 christos if (hdr->sh_link > elf_numsections (abfd)) 2490 1.13 christos { 2491 1.1 christos /* PR 10478: Accept Solaris binaries with a sh_link field 2492 1.1 christos set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */ 2493 1.1 christos switch (bfd_get_arch (abfd)) 2494 1.1 christos { 2495 1.13 christos case bfd_arch_i386: 2496 1.13 christos case bfd_arch_sparc: 2497 1.1 christos if (hdr->sh_link == (SHN_LORESERVE & 0xffff) 2498 1.1 christos || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff)) 2499 1.1 christos break; 2500 1.3 christos /* Otherwise fall through. */ 2501 1.1 christos default: 2502 1.1 christos goto fail; 2503 1.1 christos } 2504 1.3 christos } 2505 1.1 christos else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) 2506 1.1 christos goto fail; 2507 1.1 christos else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) 2508 1.1 christos { 2509 1.1 christos Elf_Internal_Shdr *dynsymhdr; 2510 1.1 christos 2511 1.1 christos /* The shared libraries distributed with hpux11 have a bogus 2512 1.1 christos sh_link field for the ".dynamic" section. Find the 2513 1.1 christos string table for the ".dynsym" section instead. */ 2514 1.1 christos if (elf_dynsymtab (abfd) != 0) 2515 1.1 christos { 2516 1.1 christos dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; 2517 1.1 christos hdr->sh_link = dynsymhdr->sh_link; 2518 1.1 christos } 2519 1.1 christos else 2520 1.1 christos { 2521 1.1 christos unsigned int i, num_sec; 2522 1.1 christos 2523 1.1 christos num_sec = elf_numsections (abfd); 2524 1.1 christos for (i = 1; i < num_sec; i++) 2525 1.1 christos { 2526 1.1 christos dynsymhdr = elf_elfsections (abfd)[i]; 2527 1.1 christos if (dynsymhdr->sh_type == SHT_DYNSYM) 2528 1.1 christos { 2529 1.1 christos hdr->sh_link = dynsymhdr->sh_link; 2530 1.1 christos break; 2531 1.1 christos } 2532 1.1 christos } 2533 1.3 christos } 2534 1.1 christos } 2535 1.3 christos goto success; 2536 1.1 christos 2537 1.3 christos case SHT_SYMTAB: /* A symbol table. */ 2538 1.1 christos if (elf_onesymtab (abfd) == shindex) 2539 1.1 christos goto success; 2540 1.3 christos 2541 1.3 christos if (hdr->sh_entsize != bed->s->sizeof_sym) 2542 1.1 christos goto fail; 2543 1.1 christos 2544 1.1 christos if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) 2545 1.3 christos { 2546 1.1 christos if (hdr->sh_size != 0) 2547 1.1 christos goto fail; 2548 1.1 christos /* Some assemblers erroneously set sh_info to one with a 2549 1.1 christos zero sh_size. ld sees this as a global symbol count 2550 1.3 christos of (unsigned) -1. Fix it here. */ 2551 1.3 christos hdr->sh_info = 0; 2552 1.3 christos goto success; 2553 1.3 christos } 2554 1.3 christos 2555 1.3 christos /* PR 18854: A binary might contain more than one symbol table. 2556 1.3 christos Unusual, but possible. Warn, but continue. */ 2557 1.9 christos if (elf_onesymtab (abfd) != 0) 2558 1.9 christos { 2559 1.9 christos _bfd_error_handler 2560 1.9 christos /* xgettext:c-format */ 2561 1.3 christos (_("%pB: warning: multiple symbol tables detected" 2562 1.3 christos " - ignoring the table in section %u"), 2563 1.1 christos abfd, shindex); 2564 1.1 christos goto success; 2565 1.3 christos } 2566 1.3 christos elf_onesymtab (abfd) = shindex; 2567 1.1 christos elf_symtab_hdr (abfd) = *hdr; 2568 1.1 christos elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); 2569 1.1 christos abfd->flags |= HAS_SYMS; 2570 1.1 christos 2571 1.1 christos /* Sometimes a shared object will map in the symbol table. If 2572 1.1 christos SHF_ALLOC is set, and this is a shared object, then we also 2573 1.1 christos treat this section as a BFD section. We can not base the 2574 1.1 christos decision purely on SHF_ALLOC, because that flag is sometimes 2575 1.1 christos set in a relocatable object file, which would confuse the 2576 1.1 christos linker. */ 2577 1.1 christos if ((hdr->sh_flags & SHF_ALLOC) != 0 2578 1.1 christos && (abfd->flags & DYNAMIC) != 0 2579 1.3 christos && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, 2580 1.1 christos shindex)) 2581 1.1 christos goto fail; 2582 1.1 christos 2583 1.1 christos /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we 2584 1.3 christos can't read symbols without that section loaded as well. It 2585 1.3 christos is most likely specified by the next section header. */ 2586 1.3 christos { 2587 1.3 christos elf_section_list * entry; 2588 1.13 christos unsigned int i, num_sec; 2589 1.3 christos 2590 1.3 christos for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next) 2591 1.3 christos if (entry->hdr.sh_link == shindex) 2592 1.3 christos goto success; 2593 1.3 christos 2594 1.3 christos num_sec = elf_numsections (abfd); 2595 1.3 christos for (i = shindex + 1; i < num_sec; i++) 2596 1.3 christos { 2597 1.3 christos Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2598 1.3 christos 2599 1.3 christos if (hdr2->sh_type == SHT_SYMTAB_SHNDX 2600 1.3 christos && hdr2->sh_link == shindex) 2601 1.1 christos break; 2602 1.3 christos } 2603 1.3 christos 2604 1.1 christos if (i == num_sec) 2605 1.1 christos for (i = 1; i < shindex; i++) 2606 1.3 christos { 2607 1.1 christos Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2608 1.1 christos 2609 1.1 christos if (hdr2->sh_type == SHT_SYMTAB_SHNDX 2610 1.1 christos && hdr2->sh_link == shindex) 2611 1.1 christos break; 2612 1.3 christos } 2613 1.3 christos 2614 1.13 christos if (i != shindex) 2615 1.13 christos ret = bfd_section_from_shdr (abfd, i); 2616 1.3 christos /* else FIXME: we have failed to find the symbol table. 2617 1.3 christos Should we issue an error? */ 2618 1.3 christos goto success; 2619 1.3 christos } 2620 1.1 christos 2621 1.3 christos case SHT_DYNSYM: /* A dynamic symbol table. */ 2622 1.1 christos if (elf_dynsymtab (abfd) == shindex) 2623 1.1 christos goto success; 2624 1.3 christos 2625 1.3 christos if (hdr->sh_entsize != bed->s->sizeof_sym) 2626 1.1 christos goto fail; 2627 1.1 christos 2628 1.1 christos if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) 2629 1.3 christos { 2630 1.3 christos if (hdr->sh_size != 0) 2631 1.1 christos goto fail; 2632 1.1 christos 2633 1.1 christos /* Some linkers erroneously set sh_info to one with a 2634 1.1 christos zero sh_size. ld sees this as a global symbol count 2635 1.3 christos of (unsigned) -1. Fix it here. */ 2636 1.3 christos hdr->sh_info = 0; 2637 1.3 christos goto success; 2638 1.3 christos } 2639 1.3 christos 2640 1.3 christos /* PR 18854: A binary might contain more than one dynamic symbol table. 2641 1.3 christos Unusual, but possible. Warn, but continue. */ 2642 1.9 christos if (elf_dynsymtab (abfd) != 0) 2643 1.9 christos { 2644 1.9 christos _bfd_error_handler 2645 1.9 christos /* xgettext:c-format */ 2646 1.3 christos (_("%pB: warning: multiple dynamic symbol tables detected" 2647 1.3 christos " - ignoring the table in section %u"), 2648 1.1 christos abfd, shindex); 2649 1.1 christos goto success; 2650 1.1 christos } 2651 1.1 christos elf_dynsymtab (abfd) = shindex; 2652 1.1 christos elf_tdata (abfd)->dynsymtab_hdr = *hdr; 2653 1.1 christos elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; 2654 1.1 christos abfd->flags |= HAS_SYMS; 2655 1.1 christos 2656 1.3 christos /* Besides being a symbol table, we also treat this as a regular 2657 1.3 christos section, so that objcopy can handle it. */ 2658 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2659 1.13 christos goto success; 2660 1.3 christos 2661 1.3 christos case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ 2662 1.1 christos { 2663 1.13 christos elf_section_list * entry; 2664 1.3 christos 2665 1.3 christos for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next) 2666 1.9 christos if (entry->ndx == shindex) 2667 1.10 christos goto success; 2668 1.3 christos 2669 1.3 christos entry = bfd_alloc (abfd, sizeof (*entry)); 2670 1.3 christos if (entry == NULL) 2671 1.3 christos goto fail; 2672 1.3 christos entry->ndx = shindex; 2673 1.3 christos entry->hdr = * hdr; 2674 1.3 christos entry->next = elf_symtab_shndx_list (abfd); 2675 1.3 christos elf_symtab_shndx_list (abfd) = entry; 2676 1.3 christos elf_elfsections (abfd)[shindex] = & entry->hdr; 2677 1.1 christos goto success; 2678 1.3 christos } 2679 1.1 christos 2680 1.3 christos case SHT_STRTAB: /* A string table. */ 2681 1.3 christos if (hdr->bfd_section != NULL) 2682 1.1 christos goto success; 2683 1.1 christos 2684 1.1 christos if (ehdr->e_shstrndx == shindex) 2685 1.1 christos { 2686 1.3 christos elf_tdata (abfd)->shstrtab_hdr = *hdr; 2687 1.1 christos elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; 2688 1.3 christos goto success; 2689 1.1 christos } 2690 1.1 christos 2691 1.1 christos if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) 2692 1.1 christos { 2693 1.1 christos symtab_strtab: 2694 1.3 christos elf_tdata (abfd)->strtab_hdr = *hdr; 2695 1.1 christos elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; 2696 1.3 christos goto success; 2697 1.1 christos } 2698 1.1 christos 2699 1.1 christos if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) 2700 1.1 christos { 2701 1.1 christos dynsymtab_strtab: 2702 1.1 christos elf_tdata (abfd)->dynstrtab_hdr = *hdr; 2703 1.1 christos hdr = &elf_tdata (abfd)->dynstrtab_hdr; 2704 1.1 christos elf_elfsections (abfd)[shindex] = hdr; 2705 1.3 christos /* We also treat this as a regular section, so that objcopy 2706 1.3 christos can handle it. */ 2707 1.3 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, 2708 1.1 christos shindex); 2709 1.1 christos goto success; 2710 1.1 christos } 2711 1.1 christos 2712 1.1 christos /* If the string table isn't one of the above, then treat it as a 2713 1.1 christos regular section. We need to scan all the headers to be sure, 2714 1.1 christos just in case this strtab section appeared before the above. */ 2715 1.1 christos if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) 2716 1.1 christos { 2717 1.1 christos unsigned int i, num_sec; 2718 1.1 christos 2719 1.1 christos num_sec = elf_numsections (abfd); 2720 1.1 christos for (i = 1; i < num_sec; i++) 2721 1.1 christos { 2722 1.1 christos Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2723 1.1 christos if (hdr2->sh_link == shindex) 2724 1.1 christos { 2725 1.3 christos /* Prevent endless recursion on broken objects. */ 2726 1.1 christos if (i == shindex) 2727 1.3 christos goto fail; 2728 1.1 christos if (! bfd_section_from_shdr (abfd, i)) 2729 1.1 christos goto fail; 2730 1.1 christos if (elf_onesymtab (abfd) == i) 2731 1.1 christos goto symtab_strtab; 2732 1.1 christos if (elf_dynsymtab (abfd) == i) 2733 1.1 christos goto dynsymtab_strtab; 2734 1.1 christos } 2735 1.3 christos } 2736 1.3 christos } 2737 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2738 1.1 christos goto success; 2739 1.1 christos 2740 1.12 christos case SHT_REL: 2741 1.1 christos case SHT_RELA: 2742 1.1 christos case SHT_RELR: 2743 1.1 christos /* *These* do a lot of work -- but build no sections! */ 2744 1.1 christos { 2745 1.1 christos asection *target_sect; 2746 1.1 christos Elf_Internal_Shdr *hdr2, **p_hdr; 2747 1.12 christos unsigned int num_sec = elf_numsections (abfd); 2748 1.1 christos struct bfd_elf_section_data *esdt; 2749 1.12 christos bfd_size_type size; 2750 1.12 christos 2751 1.12 christos if (hdr->sh_type == SHT_REL) 2752 1.12 christos size = bed->s->sizeof_rel; 2753 1.12 christos else if (hdr->sh_type == SHT_RELA) 2754 1.12 christos size = bed->s->sizeof_rela; 2755 1.12 christos else 2756 1.3 christos size = bed->s->arch_size / 8; 2757 1.1 christos if (hdr->sh_entsize != size) 2758 1.1 christos goto fail; 2759 1.1 christos 2760 1.1 christos /* Check for a bogus link to avoid crashing. */ 2761 1.9 christos if (hdr->sh_link >= num_sec) 2762 1.9 christos { 2763 1.9 christos _bfd_error_handler 2764 1.9 christos /* xgettext:c-format */ 2765 1.13 christos (_("%pB: invalid link %u for reloc section %s (index %u)"), 2766 1.3 christos abfd, hdr->sh_link, name, shindex); 2767 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2768 1.1 christos goto success; 2769 1.1 christos } 2770 1.1 christos 2771 1.1 christos /* Get the symbol table. */ 2772 1.1 christos if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB 2773 1.3 christos || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) 2774 1.1 christos && ! bfd_section_from_shdr (abfd, hdr->sh_link)) 2775 1.10 christos goto fail; 2776 1.10 christos 2777 1.10 christos /* If this is an alloc section in an executable or shared 2778 1.10 christos library, or the reloc section does not use the main symbol 2779 1.10 christos table we don't treat it as a reloc section. BFD can't 2780 1.10 christos adequately represent such a section, so at least for now, 2781 1.10 christos we don't try. We just present it as a normal section. We 2782 1.10 christos also can't use it as a reloc section if it points to the 2783 1.10 christos null section, an invalid section, another reloc section, or 2784 1.10 christos its sh_link points to the null section. */ 2785 1.13 christos if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0 2786 1.13 christos && (hdr->sh_flags & SHF_ALLOC) != 0) 2787 1.1 christos || (hdr->sh_flags & SHF_COMPRESSED) != 0 2788 1.10 christos || hdr->sh_type == SHT_RELR 2789 1.1 christos || hdr->sh_link == SHN_UNDEF 2790 1.1 christos || hdr->sh_link != elf_onesymtab (abfd) 2791 1.1 christos || hdr->sh_info == SHN_UNDEF 2792 1.1 christos || hdr->sh_info >= num_sec 2793 1.3 christos || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL 2794 1.13 christos || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) 2795 1.3 christos { 2796 1.3 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2797 1.1 christos goto success; 2798 1.1 christos } 2799 1.3 christos 2800 1.3 christos if (! bfd_section_from_shdr (abfd, hdr->sh_info)) 2801 1.1 christos goto fail; 2802 1.1 christos 2803 1.3 christos target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); 2804 1.1 christos if (target_sect == NULL) 2805 1.1 christos goto fail; 2806 1.1 christos 2807 1.1 christos esdt = elf_section_data (target_sect); 2808 1.1 christos if (hdr->sh_type == SHT_RELA) 2809 1.1 christos p_hdr = &esdt->rela.hdr; 2810 1.1 christos else 2811 1.10 christos p_hdr = &esdt->rel.hdr; 2812 1.10 christos 2813 1.10 christos /* PR 17512: file: 0b4f81b7. 2814 1.3 christos Also see PR 24456, for a file which deliberately has two reloc 2815 1.10 christos sections. */ 2816 1.12 christos if (*p_hdr != NULL) 2817 1.12 christos { 2818 1.12 christos if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex)) 2819 1.12 christos { 2820 1.12 christos _bfd_error_handler 2821 1.12 christos /* xgettext:c-format */ 2822 1.12 christos (_("%pB: warning: secondary relocation section '%s' " 2823 1.12 christos "for section %pA found - ignoring"), 2824 1.12 christos abfd, name, target_sect); 2825 1.12 christos } 2826 1.10 christos else 2827 1.10 christos esdt->has_secondary_relocs = true; 2828 1.12 christos goto success; 2829 1.5 christos } 2830 1.1 christos 2831 1.3 christos hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); 2832 1.1 christos if (hdr2 == NULL) 2833 1.1 christos goto fail; 2834 1.1 christos *hdr2 = *hdr; 2835 1.9 christos *p_hdr = hdr2; 2836 1.9 christos elf_elfsections (abfd)[shindex] = hdr2; 2837 1.1 christos target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr) 2838 1.1 christos * bed->s->int_rels_per_ext_rel); 2839 1.1 christos target_sect->flags |= SEC_RELOC; 2840 1.1 christos target_sect->relocation = NULL; 2841 1.1 christos target_sect->rel_filepos = hdr->sh_offset; 2842 1.1 christos /* In the section to which the relocations apply, mark whether 2843 1.1 christos its relocations are of the REL or RELA variety. */ 2844 1.1 christos if (hdr->sh_size != 0) 2845 1.1 christos { 2846 1.1 christos if (hdr->sh_type == SHT_RELA) 2847 1.1 christos target_sect->use_rela_p = 1; 2848 1.3 christos } 2849 1.1 christos abfd->flags |= HAS_RELOC; 2850 1.1 christos goto success; 2851 1.1 christos } 2852 1.13 christos 2853 1.13 christos case SHT_GNU_verdef: 2854 1.1 christos if (hdr->sh_info != 0) 2855 1.3 christos elf_dynverdef (abfd) = shindex; 2856 1.3 christos elf_tdata (abfd)->dynverdef_hdr = *hdr; 2857 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2858 1.1 christos goto success; 2859 1.1 christos 2860 1.3 christos case SHT_GNU_versym: 2861 1.3 christos if (hdr->sh_entsize != sizeof (Elf_External_Versym)) 2862 1.1 christos goto fail; 2863 1.1 christos 2864 1.3 christos elf_dynversym (abfd) = shindex; 2865 1.3 christos elf_tdata (abfd)->dynversym_hdr = *hdr; 2866 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2867 1.1 christos goto success; 2868 1.13 christos 2869 1.13 christos case SHT_GNU_verneed: 2870 1.1 christos if (hdr->sh_info != 0) 2871 1.3 christos elf_dynverref (abfd) = shindex; 2872 1.3 christos elf_tdata (abfd)->dynverref_hdr = *hdr; 2873 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2874 1.1 christos goto success; 2875 1.3 christos 2876 1.1 christos case SHT_SHLIB: 2877 1.1 christos goto success; 2878 1.1 christos 2879 1.3 christos case SHT_GROUP: 2880 1.3 christos if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 2881 1.3 christos goto fail; 2882 1.1 christos 2883 1.1 christos goto success; 2884 1.1 christos 2885 1.14 christos default: 2886 1.14 christos /* Possibly an attributes section. */ 2887 1.14 christos if (get_elf_backend_data (abfd)->target_os != is_solaris 2888 1.14 christos /* PR 33153: Solaris defines SHT_SUNW_cap which collides with SHT_GNU_ATTRIBUTES. */ 2889 1.1 christos && (hdr->sh_type == SHT_GNU_ATTRIBUTES 2890 1.1 christos || hdr->sh_type == bed->obj_attrs_section_type)) 2891 1.3 christos { 2892 1.1 christos if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 2893 1.3 christos goto fail; 2894 1.1 christos _bfd_elf_parse_attributes (abfd, hdr); 2895 1.1 christos goto success; 2896 1.1 christos } 2897 1.1 christos 2898 1.3 christos /* Check for any processor-specific section types. */ 2899 1.1 christos if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) 2900 1.1 christos goto success; 2901 1.1 christos 2902 1.1 christos if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) 2903 1.1 christos { 2904 1.1 christos if ((hdr->sh_flags & SHF_ALLOC) != 0) 2905 1.9 christos /* FIXME: How to properly handle allocated section reserved 2906 1.9 christos for applications? */ 2907 1.9 christos _bfd_error_handler 2908 1.9 christos /* xgettext:c-format */ 2909 1.1 christos (_("%pB: unknown type [%#x] section `%s'"), 2910 1.3 christos abfd, hdr->sh_type, name); 2911 1.3 christos else 2912 1.13 christos { 2913 1.3 christos /* Allow sections reserved for applications. */ 2914 1.3 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2915 1.1 christos goto success; 2916 1.1 christos } 2917 1.1 christos } 2918 1.1 christos else if (hdr->sh_type >= SHT_LOPROC 2919 1.9 christos && hdr->sh_type <= SHT_HIPROC) 2920 1.9 christos /* FIXME: We should handle this section. */ 2921 1.9 christos _bfd_error_handler 2922 1.9 christos /* xgettext:c-format */ 2923 1.1 christos (_("%pB: unknown type [%#x] section `%s'"), 2924 1.1 christos abfd, hdr->sh_type, name); 2925 1.1 christos else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) 2926 1.1 christos { 2927 1.1 christos /* Unrecognised OS-specific sections. */ 2928 1.1 christos if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) 2929 1.1 christos /* SHF_OS_NONCONFORMING indicates that special knowledge is 2930 1.9 christos required to correctly process the section and the file should 2931 1.9 christos be rejected with an error message. */ 2932 1.9 christos _bfd_error_handler 2933 1.9 christos /* xgettext:c-format */ 2934 1.1 christos (_("%pB: unknown type [%#x] section `%s'"), 2935 1.3 christos abfd, hdr->sh_type, name); 2936 1.3 christos else 2937 1.3 christos { 2938 1.3 christos /* Otherwise it should be processed. */ 2939 1.3 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2940 1.1 christos goto success; 2941 1.1 christos } 2942 1.1 christos } 2943 1.9 christos else 2944 1.9 christos /* FIXME: We should handle this section. */ 2945 1.9 christos _bfd_error_handler 2946 1.9 christos /* xgettext:c-format */ 2947 1.1 christos (_("%pB: unknown type [%#x] section `%s'"), 2948 1.3 christos abfd, hdr->sh_type, name); 2949 1.1 christos 2950 1.1 christos goto fail; 2951 1.3 christos } 2952 1.12 christos 2953 1.3 christos fail: 2954 1.12 christos ret = false; 2955 1.3 christos success: 2956 1.1 christos elf_tdata (abfd)->being_created[shindex] = false; 2957 1.1 christos return ret; 2958 1.1 christos } 2959 1.1 christos 2960 1.1 christos /* Return the local symbol specified by ABFD, R_SYMNDX. */ 2961 1.1 christos 2962 1.1 christos Elf_Internal_Sym * 2963 1.1 christos bfd_sym_from_r_symndx (struct sym_cache *cache, 2964 1.1 christos bfd *abfd, 2965 1.1 christos unsigned long r_symndx) 2966 1.1 christos { 2967 1.1 christos unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; 2968 1.1 christos 2969 1.1 christos if (cache->abfd != abfd || cache->indx[ent] != r_symndx) 2970 1.1 christos { 2971 1.1 christos Elf_Internal_Shdr *symtab_hdr; 2972 1.1 christos unsigned char esym[sizeof (Elf64_External_Sym)]; 2973 1.1 christos Elf_External_Sym_Shndx eshndx; 2974 1.1 christos 2975 1.1 christos symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2976 1.1 christos if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, 2977 1.1 christos &cache->sym[ent], esym, &eshndx) == NULL) 2978 1.1 christos return NULL; 2979 1.1 christos 2980 1.1 christos if (cache->abfd != abfd) 2981 1.1 christos { 2982 1.1 christos memset (cache->indx, -1, sizeof (cache->indx)); 2983 1.1 christos cache->abfd = abfd; 2984 1.1 christos } 2985 1.1 christos cache->indx[ent] = r_symndx; 2986 1.1 christos } 2987 1.1 christos 2988 1.1 christos return &cache->sym[ent]; 2989 1.1 christos } 2990 1.1 christos 2991 1.1 christos /* Given an ELF section number, retrieve the corresponding BFD 2992 1.1 christos section. */ 2993 1.1 christos 2994 1.1 christos asection * 2995 1.1 christos bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) 2996 1.1 christos { 2997 1.1 christos if (sec_index >= elf_numsections (abfd)) 2998 1.1 christos return NULL; 2999 1.1 christos return elf_elfsections (abfd)[sec_index]->bfd_section; 3000 1.1 christos } 3001 1.1 christos 3002 1.1 christos static const struct bfd_elf_special_section special_sections_b[] = 3003 1.9 christos { 3004 1.1 christos { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3005 1.1 christos { NULL, 0, 0, 0, 0 } 3006 1.1 christos }; 3007 1.1 christos 3008 1.1 christos static const struct bfd_elf_special_section special_sections_c[] = 3009 1.10 christos { 3010 1.9 christos { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, 3011 1.1 christos { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 }, 3012 1.1 christos { NULL, 0, 0, 0, 0 } 3013 1.1 christos }; 3014 1.1 christos 3015 1.9 christos static const struct bfd_elf_special_section special_sections_d[] = 3016 1.9 christos { 3017 1.3 christos { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3018 1.3 christos { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3019 1.3 christos /* There are more DWARF sections than these, but they needn't be added here 3020 1.9 christos unless you have to cope with broken compilers that don't emit section 3021 1.9 christos attributes or you want to help the user writing assembler. */ 3022 1.9 christos { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, 3023 1.9 christos { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, 3024 1.1 christos { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, 3025 1.9 christos { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, 3026 1.9 christos { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, 3027 1.9 christos { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, 3028 1.9 christos { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, 3029 1.1 christos { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, 3030 1.1 christos { NULL, 0, 0, 0, 0 } 3031 1.1 christos }; 3032 1.1 christos 3033 1.9 christos static const struct bfd_elf_special_section special_sections_f[] = 3034 1.9 christos { 3035 1.9 christos { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 3036 1.1 christos { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, 3037 1.1 christos { NULL, 0 , 0, 0, 0 } 3038 1.1 christos }; 3039 1.1 christos 3040 1.1 christos static const struct bfd_elf_special_section special_sections_g[] = 3041 1.12 christos { 3042 1.12 christos { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3043 1.9 christos { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3044 1.9 christos { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3045 1.14 christos { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, 3046 1.9 christos { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3047 1.1 christos { STRING_COMMA_LEN (".gnu_object_only"), 0, SHT_GNU_OBJECT_ONLY, SHF_EXCLUDE }, 3048 1.1 christos { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, 3049 1.9 christos { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, 3050 1.9 christos { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, 3051 1.9 christos { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, 3052 1.9 christos { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, 3053 1.1 christos { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, 3054 1.1 christos { NULL, 0, 0, 0, 0 } 3055 1.1 christos }; 3056 1.1 christos 3057 1.9 christos static const struct bfd_elf_special_section special_sections_h[] = 3058 1.9 christos { 3059 1.1 christos { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, 3060 1.1 christos { NULL, 0, 0, 0, 0 } 3061 1.1 christos }; 3062 1.1 christos 3063 1.9 christos static const struct bfd_elf_special_section special_sections_i[] = 3064 1.9 christos { 3065 1.9 christos { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 3066 1.9 christos { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, 3067 1.1 christos { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, 3068 1.1 christos { NULL, 0, 0, 0, 0 } 3069 1.1 christos }; 3070 1.1 christos 3071 1.1 christos static const struct bfd_elf_special_section special_sections_l[] = 3072 1.9 christos { 3073 1.1 christos { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, 3074 1.1 christos { NULL, 0, 0, 0, 0 } 3075 1.1 christos }; 3076 1.1 christos 3077 1.12 christos static const struct bfd_elf_special_section special_sections_n[] = 3078 1.1 christos { 3079 1.9 christos { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3080 1.9 christos { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, 3081 1.1 christos { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, 3082 1.1 christos { NULL, 0, 0, 0, 0 } 3083 1.1 christos }; 3084 1.1 christos 3085 1.12 christos static const struct bfd_elf_special_section special_sections_p[] = 3086 1.12 christos { 3087 1.9 christos { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3088 1.9 christos { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3089 1.9 christos { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, 3090 1.1 christos { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 3091 1.1 christos { NULL, 0, 0, 0, 0 } 3092 1.1 christos }; 3093 1.1 christos 3094 1.1 christos static const struct bfd_elf_special_section special_sections_r[] = 3095 1.1 christos { 3096 1.12 christos { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, 3097 1.9 christos { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, 3098 1.14 christos { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC }, 3099 1.14 christos { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, 3100 1.14 christos /* .relro_padding is generated by lld. It should not be confused with a 3101 1.14 christos reloc containing section, because otherwise elf_fake_sections() will 3102 1.14 christos set the entsize to 8, which may not be an actual multiple of the 3103 1.14 christos section's size. 3104 1.9 christos Note - this entry must appear before the ".rel" entry below. */ 3105 1.9 christos { STRING_COMMA_LEN (".relro_padding"), 0, SHT_NOBITS, SHF_ALLOC | SHF_WRITE }, 3106 1.1 christos { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, 3107 1.1 christos { NULL, 0, 0, 0, 0 } 3108 1.1 christos }; 3109 1.1 christos 3110 1.1 christos static const struct bfd_elf_special_section special_sections_s[] = 3111 1.1 christos { 3112 1.1 christos { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, 3113 1.1 christos { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, 3114 1.1 christos { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, 3115 1.1 christos /* See struct bfd_elf_special_section declaration for the semantics of 3116 1.9 christos this special case where .prefix_length != strlen (.prefix). */ 3117 1.1 christos { ".stabstr", 5, 3, SHT_STRTAB, 0 }, 3118 1.1 christos { NULL, 0, 0, 0, 0 } 3119 1.1 christos }; 3120 1.1 christos 3121 1.9 christos static const struct bfd_elf_special_section special_sections_t[] = 3122 1.9 christos { 3123 1.1 christos { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 3124 1.9 christos { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, 3125 1.1 christos { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, 3126 1.1 christos { NULL, 0, 0, 0, 0 } 3127 1.1 christos }; 3128 1.1 christos 3129 1.9 christos static const struct bfd_elf_special_section special_sections_z[] = 3130 1.9 christos { 3131 1.1 christos { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, 3132 1.1 christos { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, 3133 1.9 christos { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, 3134 1.1 christos { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, 3135 1.1 christos { NULL, 0, 0, 0, 0 } 3136 1.1 christos }; 3137 1.1 christos 3138 1.1 christos static const struct bfd_elf_special_section * const special_sections[] = 3139 1.1 christos { 3140 1.1 christos special_sections_b, /* 'b' */ 3141 1.1 christos special_sections_c, /* 'c' */ 3142 1.1 christos special_sections_d, /* 'd' */ 3143 1.1 christos NULL, /* 'e' */ 3144 1.1 christos special_sections_f, /* 'f' */ 3145 1.1 christos special_sections_g, /* 'g' */ 3146 1.1 christos special_sections_h, /* 'h' */ 3147 1.1 christos special_sections_i, /* 'i' */ 3148 1.1 christos NULL, /* 'j' */ 3149 1.1 christos NULL, /* 'k' */ 3150 1.1 christos special_sections_l, /* 'l' */ 3151 1.1 christos NULL, /* 'm' */ 3152 1.1 christos special_sections_n, /* 'n' */ 3153 1.1 christos NULL, /* 'o' */ 3154 1.1 christos special_sections_p, /* 'p' */ 3155 1.1 christos NULL, /* 'q' */ 3156 1.1 christos special_sections_r, /* 'r' */ 3157 1.1 christos special_sections_s, /* 's' */ 3158 1.1 christos special_sections_t, /* 't' */ 3159 1.1 christos NULL, /* 'u' */ 3160 1.1 christos NULL, /* 'v' */ 3161 1.1 christos NULL, /* 'w' */ 3162 1.1 christos NULL, /* 'x' */ 3163 1.1 christos NULL, /* 'y' */ 3164 1.1 christos special_sections_z /* 'z' */ 3165 1.1 christos }; 3166 1.1 christos 3167 1.1 christos const struct bfd_elf_special_section * 3168 1.1 christos _bfd_elf_get_special_section (const char *name, 3169 1.1 christos const struct bfd_elf_special_section *spec, 3170 1.1 christos unsigned int rela) 3171 1.1 christos { 3172 1.1 christos int i; 3173 1.1 christos int len; 3174 1.1 christos 3175 1.1 christos len = strlen (name); 3176 1.1 christos 3177 1.1 christos for (i = 0; spec[i].prefix != NULL; i++) 3178 1.1 christos { 3179 1.1 christos int suffix_len; 3180 1.1 christos int prefix_len = spec[i].prefix_length; 3181 1.1 christos 3182 1.1 christos if (len < prefix_len) 3183 1.1 christos continue; 3184 1.1 christos if (memcmp (name, spec[i].prefix, prefix_len) != 0) 3185 1.1 christos continue; 3186 1.1 christos 3187 1.1 christos suffix_len = spec[i].suffix_length; 3188 1.1 christos if (suffix_len <= 0) 3189 1.1 christos { 3190 1.1 christos if (name[prefix_len] != 0) 3191 1.1 christos { 3192 1.1 christos if (suffix_len == 0) 3193 1.1 christos continue; 3194 1.1 christos if (name[prefix_len] != '.' 3195 1.1 christos && (suffix_len == -2 3196 1.1 christos || (rela && spec[i].type == SHT_REL))) 3197 1.1 christos continue; 3198 1.1 christos } 3199 1.1 christos } 3200 1.1 christos else 3201 1.1 christos { 3202 1.1 christos if (len < prefix_len + suffix_len) 3203 1.1 christos continue; 3204 1.1 christos if (memcmp (name + len - suffix_len, 3205 1.1 christos spec[i].prefix + prefix_len, 3206 1.1 christos suffix_len) != 0) 3207 1.1 christos continue; 3208 1.1 christos } 3209 1.1 christos return &spec[i]; 3210 1.1 christos } 3211 1.1 christos 3212 1.1 christos return NULL; 3213 1.1 christos } 3214 1.1 christos 3215 1.1 christos const struct bfd_elf_special_section * 3216 1.1 christos _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) 3217 1.1 christos { 3218 1.1 christos int i; 3219 1.1 christos const struct bfd_elf_special_section *spec; 3220 1.1 christos const struct elf_backend_data *bed; 3221 1.1 christos 3222 1.1 christos /* See if this is one of the special sections. */ 3223 1.1 christos if (sec->name == NULL) 3224 1.1 christos return NULL; 3225 1.1 christos 3226 1.1 christos bed = get_elf_backend_data (abfd); 3227 1.1 christos spec = bed->special_sections; 3228 1.1 christos if (spec) 3229 1.1 christos { 3230 1.1 christos spec = _bfd_elf_get_special_section (sec->name, 3231 1.1 christos bed->special_sections, 3232 1.1 christos sec->use_rela_p); 3233 1.1 christos if (spec != NULL) 3234 1.1 christos return spec; 3235 1.1 christos } 3236 1.1 christos 3237 1.1 christos if (sec->name[0] != '.') 3238 1.1 christos return NULL; 3239 1.1 christos 3240 1.1 christos i = sec->name[1] - 'b'; 3241 1.1 christos if (i < 0 || i > 'z' - 'b') 3242 1.1 christos return NULL; 3243 1.1 christos 3244 1.1 christos spec = special_sections[i]; 3245 1.1 christos 3246 1.1 christos if (spec == NULL) 3247 1.1 christos return NULL; 3248 1.1 christos 3249 1.1 christos return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); 3250 1.12 christos } 3251 1.1 christos 3252 1.1 christos bool 3253 1.1 christos _bfd_elf_new_section_hook (bfd *abfd, asection *sec) 3254 1.1 christos { 3255 1.1 christos struct bfd_elf_section_data *sdata; 3256 1.1 christos const struct elf_backend_data *bed; 3257 1.1 christos const struct bfd_elf_special_section *ssect; 3258 1.1 christos 3259 1.1 christos sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; 3260 1.1 christos if (sdata == NULL) 3261 1.9 christos { 3262 1.1 christos sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, 3263 1.12 christos sizeof (*sdata)); 3264 1.1 christos if (sdata == NULL) 3265 1.1 christos return false; 3266 1.1 christos sec->used_by_bfd = sdata; 3267 1.1 christos } 3268 1.1 christos 3269 1.1 christos /* Indicate whether or not this section should use RELA relocations. */ 3270 1.1 christos bed = get_elf_backend_data (abfd); 3271 1.12 christos sec->use_rela_p = bed->default_use_rela_p; 3272 1.12 christos 3273 1.12 christos /* Set up ELF section type and flags for newly created sections, if 3274 1.12 christos there is an ABI mandated section. */ 3275 1.12 christos ssect = (*bed->get_sec_type_attr) (abfd, sec); 3276 1.12 christos if (ssect != NULL) 3277 1.12 christos { 3278 1.1 christos elf_section_type (sec) = ssect->type; 3279 1.1 christos elf_section_flags (sec) = ssect->attr; 3280 1.1 christos } 3281 1.1 christos 3282 1.1 christos return _bfd_generic_new_section_hook (abfd, sec); 3283 1.1 christos } 3284 1.1 christos 3285 1.1 christos /* Create a new bfd section from an ELF program header. 3286 1.1 christos 3287 1.1 christos Since program segments have no names, we generate a synthetic name 3288 1.1 christos of the form segment<NUM>, where NUM is generally the index in the 3289 1.1 christos program header table. For segments that are split (see below) we 3290 1.1 christos generate the names segment<NUM>a and segment<NUM>b. 3291 1.1 christos 3292 1.1 christos Note that some program segments may have a file size that is different than 3293 1.1 christos (less than) the memory size. All this means is that at execution the 3294 1.1 christos system must allocate the amount of memory specified by the memory size, 3295 1.1 christos but only initialize it with the first "file size" bytes read from the 3296 1.1 christos file. This would occur for example, with program segments consisting 3297 1.1 christos of combined data+bss. 3298 1.1 christos 3299 1.1 christos To handle the above situation, this routine generates TWO bfd sections 3300 1.1 christos for the single program segment. The first has the length specified by 3301 1.13 christos the file size of the segment, and the second has the length specified 3302 1.1 christos by the difference between the two sizes. In effect, the segment is split 3303 1.12 christos into its initialized and uninitialized parts. */ 3304 1.1 christos 3305 1.1 christos bool 3306 1.1 christos _bfd_elf_make_section_from_phdr (bfd *abfd, 3307 1.1 christos Elf_Internal_Phdr *hdr, 3308 1.1 christos int hdr_index, 3309 1.1 christos const char *type_name) 3310 1.1 christos { 3311 1.1 christos asection *newsect; 3312 1.1 christos char *name; 3313 1.1 christos char namebuf[64]; 3314 1.12 christos size_t len; 3315 1.1 christos int split; 3316 1.1 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL); 3317 1.1 christos 3318 1.1 christos split = ((hdr->p_memsz > 0) 3319 1.1 christos && (hdr->p_filesz > 0) 3320 1.1 christos && (hdr->p_memsz > hdr->p_filesz)); 3321 1.1 christos 3322 1.1 christos if (hdr->p_filesz > 0) 3323 1.1 christos { 3324 1.1 christos sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); 3325 1.1 christos len = strlen (namebuf) + 1; 3326 1.12 christos name = (char *) bfd_alloc (abfd, len); 3327 1.1 christos if (!name) 3328 1.1 christos return false; 3329 1.1 christos memcpy (name, namebuf, len); 3330 1.12 christos newsect = bfd_make_section (abfd, name); 3331 1.12 christos if (newsect == NULL) 3332 1.12 christos return false; 3333 1.1 christos newsect->vma = hdr->p_vaddr / opb; 3334 1.1 christos newsect->lma = hdr->p_paddr / opb; 3335 1.1 christos newsect->size = hdr->p_filesz; 3336 1.1 christos newsect->filepos = hdr->p_offset; 3337 1.1 christos newsect->flags |= SEC_HAS_CONTENTS; 3338 1.1 christos newsect->alignment_power = bfd_log2 (hdr->p_align); 3339 1.1 christos if (hdr->p_type == PT_LOAD) 3340 1.1 christos { 3341 1.1 christos newsect->flags |= SEC_ALLOC; 3342 1.1 christos newsect->flags |= SEC_LOAD; 3343 1.1 christos if (hdr->p_flags & PF_X) 3344 1.1 christos { 3345 1.1 christos /* FIXME: all we known is that it has execute PERMISSION, 3346 1.1 christos may be data. */ 3347 1.1 christos newsect->flags |= SEC_CODE; 3348 1.1 christos } 3349 1.1 christos } 3350 1.1 christos if (!(hdr->p_flags & PF_W)) 3351 1.1 christos { 3352 1.1 christos newsect->flags |= SEC_READONLY; 3353 1.1 christos } 3354 1.1 christos } 3355 1.1 christos 3356 1.1 christos if (hdr->p_memsz > hdr->p_filesz) 3357 1.1 christos { 3358 1.1 christos bfd_vma align; 3359 1.1 christos 3360 1.1 christos sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); 3361 1.1 christos len = strlen (namebuf) + 1; 3362 1.12 christos name = (char *) bfd_alloc (abfd, len); 3363 1.1 christos if (!name) 3364 1.1 christos return false; 3365 1.1 christos memcpy (name, namebuf, len); 3366 1.12 christos newsect = bfd_make_section (abfd, name); 3367 1.12 christos if (newsect == NULL) 3368 1.12 christos return false; 3369 1.1 christos newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb; 3370 1.1 christos newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb; 3371 1.1 christos newsect->size = hdr->p_memsz - hdr->p_filesz; 3372 1.1 christos newsect->filepos = hdr->p_offset + hdr->p_filesz; 3373 1.1 christos align = newsect->vma & -newsect->vma; 3374 1.1 christos if (align == 0 || align > hdr->p_align) 3375 1.1 christos align = hdr->p_align; 3376 1.1 christos newsect->alignment_power = bfd_log2 (align); 3377 1.1 christos if (hdr->p_type == PT_LOAD) 3378 1.1 christos { 3379 1.1 christos newsect->flags |= SEC_ALLOC; 3380 1.1 christos if (hdr->p_flags & PF_X) 3381 1.1 christos newsect->flags |= SEC_CODE; 3382 1.1 christos } 3383 1.1 christos if (!(hdr->p_flags & PF_W)) 3384 1.1 christos newsect->flags |= SEC_READONLY; 3385 1.12 christos } 3386 1.1 christos 3387 1.1 christos return true; 3388 1.12 christos } 3389 1.10 christos 3390 1.10 christos static bool 3391 1.10 christos _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset) 3392 1.10 christos { 3393 1.10 christos /* The return value is ignored. Build-ids are considered optional. */ 3394 1.10 christos if (templ->xvec->flavour == bfd_target_elf_flavour) 3395 1.12 christos return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id) 3396 1.10 christos (templ, offset); 3397 1.10 christos return false; 3398 1.12 christos } 3399 1.1 christos 3400 1.1 christos bool 3401 1.1 christos bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) 3402 1.1 christos { 3403 1.1 christos const struct elf_backend_data *bed; 3404 1.1 christos 3405 1.1 christos switch (hdr->p_type) 3406 1.1 christos { 3407 1.1 christos case PT_NULL: 3408 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); 3409 1.10 christos 3410 1.12 christos case PT_LOAD: 3411 1.10 christos if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load")) 3412 1.10 christos return false; 3413 1.12 christos if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL) 3414 1.1 christos _bfd_elf_core_find_build_id (abfd, hdr->p_offset); 3415 1.1 christos return true; 3416 1.1 christos 3417 1.1 christos case PT_DYNAMIC: 3418 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); 3419 1.1 christos 3420 1.1 christos case PT_INTERP: 3421 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); 3422 1.1 christos 3423 1.12 christos case PT_NOTE: 3424 1.9 christos if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) 3425 1.9 christos return false; 3426 1.12 christos if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz, 3427 1.12 christos hdr->p_align)) 3428 1.1 christos return false; 3429 1.1 christos return true; 3430 1.1 christos 3431 1.1 christos case PT_SHLIB: 3432 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); 3433 1.1 christos 3434 1.1 christos case PT_PHDR: 3435 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); 3436 1.1 christos 3437 1.1 christos case PT_GNU_EH_FRAME: 3438 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, 3439 1.1 christos "eh_frame_hdr"); 3440 1.1 christos 3441 1.1 christos case PT_GNU_STACK: 3442 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); 3443 1.1 christos 3444 1.1 christos case PT_GNU_RELRO: 3445 1.13 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); 3446 1.13 christos 3447 1.13 christos case PT_GNU_SFRAME: 3448 1.13 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, 3449 1.1 christos "sframe"); 3450 1.1 christos 3451 1.1 christos default: 3452 1.1 christos /* Check for any processor-specific program segment types. */ 3453 1.1 christos bed = get_elf_backend_data (abfd); 3454 1.1 christos return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); 3455 1.1 christos } 3456 1.1 christos } 3457 1.1 christos 3458 1.1 christos /* Return the REL_HDR for SEC, assuming there is only a single one, either 3459 1.1 christos REL or RELA. */ 3460 1.1 christos 3461 1.1 christos Elf_Internal_Shdr * 3462 1.1 christos _bfd_elf_single_rel_hdr (asection *sec) 3463 1.1 christos { 3464 1.1 christos if (elf_section_data (sec)->rel.hdr) 3465 1.1 christos { 3466 1.1 christos BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); 3467 1.1 christos return elf_section_data (sec)->rel.hdr; 3468 1.1 christos } 3469 1.1 christos else 3470 1.1 christos return elf_section_data (sec)->rela.hdr; 3471 1.12 christos } 3472 1.3 christos 3473 1.3 christos static bool 3474 1.3 christos _bfd_elf_set_reloc_sh_name (bfd *abfd, 3475 1.12 christos Elf_Internal_Shdr *rel_hdr, 3476 1.3 christos const char *sec_name, 3477 1.3 christos bool use_rela_p) 3478 1.3 christos { 3479 1.3 christos char *name = (char *) bfd_alloc (abfd, 3480 1.12 christos sizeof ".rela" + strlen (sec_name)); 3481 1.3 christos if (name == NULL) 3482 1.3 christos return false; 3483 1.3 christos 3484 1.3 christos sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); 3485 1.12 christos rel_hdr->sh_name = 3486 1.3 christos (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, 3487 1.12 christos false); 3488 1.3 christos if (rel_hdr->sh_name == (unsigned int) -1) 3489 1.12 christos return false; 3490 1.3 christos 3491 1.3 christos return true; 3492 1.1 christos } 3493 1.1 christos 3494 1.1 christos /* Allocate and initialize a section-header for a new reloc section, 3495 1.1 christos containing relocations against ASECT. It is stored in RELDATA. If 3496 1.1 christos USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL 3497 1.12 christos relocations. */ 3498 1.1 christos 3499 1.1 christos static bool 3500 1.3 christos _bfd_elf_init_reloc_shdr (bfd *abfd, 3501 1.12 christos struct bfd_elf_section_reloc_data *reldata, 3502 1.14 christos const char *sec_name, 3503 1.1 christos bool use_rela_p, 3504 1.1 christos bool delay_sh_name_p) 3505 1.1 christos { 3506 1.1 christos Elf_Internal_Shdr *rel_hdr; 3507 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 3508 1.5 christos 3509 1.13 christos BFD_ASSERT (reldata->hdr == NULL); 3510 1.13 christos rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); 3511 1.1 christos if (rel_hdr == NULL) 3512 1.1 christos return false; 3513 1.14 christos reldata->hdr = rel_hdr; 3514 1.3 christos 3515 1.3 christos if (delay_sh_name_p) 3516 1.3 christos rel_hdr->sh_name = (unsigned int) -1; 3517 1.12 christos else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, 3518 1.1 christos use_rela_p)) 3519 1.1 christos return false; 3520 1.1 christos rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; 3521 1.1 christos rel_hdr->sh_entsize = (use_rela_p 3522 1.1 christos ? bed->s->sizeof_rela 3523 1.1 christos : bed->s->sizeof_rel); 3524 1.1 christos rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; 3525 1.1 christos rel_hdr->sh_flags = 0; 3526 1.1 christos rel_hdr->sh_addr = 0; 3527 1.1 christos rel_hdr->sh_size = 0; 3528 1.12 christos rel_hdr->sh_offset = 0; 3529 1.1 christos 3530 1.1 christos return true; 3531 1.1 christos } 3532 1.1 christos 3533 1.1 christos /* Return the default section type based on the passed in section flags. */ 3534 1.1 christos 3535 1.1 christos int 3536 1.10 christos bfd_elf_get_default_section_type (flagword flags) 3537 1.1 christos { 3538 1.1 christos if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0 3539 1.1 christos && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) 3540 1.1 christos return SHT_NOBITS; 3541 1.1 christos return SHT_PROGBITS; 3542 1.1 christos } 3543 1.1 christos 3544 1.1 christos struct fake_section_arg 3545 1.12 christos { 3546 1.1 christos struct bfd_link_info *link_info; 3547 1.1 christos bool failed; 3548 1.1 christos }; 3549 1.1 christos 3550 1.1 christos /* Set up an ELF internal section header for a section. */ 3551 1.1 christos 3552 1.1 christos static void 3553 1.1 christos elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) 3554 1.1 christos { 3555 1.1 christos struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; 3556 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 3557 1.1 christos struct bfd_elf_section_data *esd = elf_section_data (asect); 3558 1.3 christos Elf_Internal_Shdr *this_hdr; 3559 1.14 christos unsigned int sh_type; 3560 1.12 christos const char *name = asect->name; 3561 1.1 christos bool delay_sh_name_p = false; 3562 1.1 christos bfd_vma mask; 3563 1.1 christos 3564 1.1 christos if (arg->failed) 3565 1.1 christos { 3566 1.1 christos /* We already failed; just get out of the bfd_map_over_sections 3567 1.1 christos loop. */ 3568 1.1 christos return; 3569 1.1 christos } 3570 1.1 christos 3571 1.13 christos this_hdr = &esd->this_hdr; 3572 1.13 christos 3573 1.13 christos /* ld: compress DWARF debug sections with names: .debug_*. */ 3574 1.13 christos if (arg->link_info 3575 1.14 christos && (abfd->flags & BFD_COMPRESS) != 0 3576 1.14 christos && (asect->flags & SEC_DEBUGGING) != 0 3577 1.13 christos && (asect->flags & SEC_ALLOC) == 0 3578 1.13 christos && (asect->flags & SEC_HAS_CONTENTS) != 0 3579 1.13 christos && name[1] == 'd' 3580 1.13 christos && name[6] == '_') 3581 1.13 christos { 3582 1.13 christos /* If this section will be compressed, delay adding section 3583 1.14 christos name to section name section after it is compressed in 3584 1.3 christos _bfd_elf_assign_file_positions_for_non_load. */ 3585 1.3 christos delay_sh_name_p = true; 3586 1.14 christos } 3587 1.3 christos 3588 1.3 christos if (delay_sh_name_p) 3589 1.1 christos this_hdr->sh_name = (unsigned int) -1; 3590 1.3 christos else 3591 1.3 christos { 3592 1.12 christos this_hdr->sh_name 3593 1.3 christos = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 3594 1.3 christos name, false); 3595 1.12 christos if (this_hdr->sh_name == (unsigned int) -1) 3596 1.3 christos { 3597 1.3 christos arg->failed = true; 3598 1.1 christos return; 3599 1.1 christos } 3600 1.1 christos } 3601 1.1 christos 3602 1.1 christos /* Don't clear sh_flags. Assembler may set additional bits. */ 3603 1.1 christos 3604 1.12 christos if ((asect->flags & SEC_ALLOC) != 0 3605 1.1 christos || asect->user_set_vma) 3606 1.1 christos this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect); 3607 1.1 christos else 3608 1.1 christos this_hdr->sh_addr = 0; 3609 1.1 christos 3610 1.1 christos this_hdr->sh_offset = 0; 3611 1.3 christos this_hdr->sh_size = asect->size; 3612 1.3 christos this_hdr->sh_link = 0; 3613 1.3 christos /* PR 17512: file: 0eb809fe, 8b0535ee. */ 3614 1.9 christos if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) 3615 1.9 christos { 3616 1.9 christos _bfd_error_handler 3617 1.9 christos /* xgettext:c-format */ 3618 1.12 christos (_("%pB: error: alignment power %d of section `%pA' is too big"), 3619 1.3 christos abfd, asect->alignment_power, asect); 3620 1.3 christos arg->failed = true; 3621 1.12 christos return; 3622 1.12 christos } 3623 1.12 christos /* Set sh_addralign to the highest power of two given by alignment 3624 1.12 christos consistent with the section VMA. Linker scripts can force VMA. */ 3625 1.1 christos mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr; 3626 1.1 christos this_hdr->sh_addralign = mask & -mask; 3627 1.1 christos /* The sh_entsize and sh_info fields may have been set already by 3628 1.1 christos copy_private_section_data. */ 3629 1.1 christos 3630 1.1 christos this_hdr->bfd_section = asect; 3631 1.1 christos this_hdr->contents = NULL; 3632 1.1 christos 3633 1.12 christos /* If the section type is unspecified, we set it based on 3634 1.12 christos asect->flags. */ 3635 1.12 christos if (asect->type != 0) 3636 1.1 christos sh_type = asect->type; 3637 1.1 christos else if ((asect->flags & SEC_GROUP) != 0) 3638 1.1 christos sh_type = SHT_GROUP; 3639 1.1 christos else 3640 1.1 christos sh_type = bfd_elf_get_default_section_type (asect->flags); 3641 1.1 christos 3642 1.1 christos if (this_hdr->sh_type == SHT_NULL) 3643 1.1 christos this_hdr->sh_type = sh_type; 3644 1.1 christos else if (this_hdr->sh_type == SHT_NOBITS 3645 1.1 christos && sh_type == SHT_PROGBITS 3646 1.1 christos && (asect->flags & SEC_ALLOC) != 0) 3647 1.1 christos { 3648 1.1 christos /* Warn if we are changing a NOBITS section to PROGBITS, but 3649 1.1 christos allow the link to proceed. This can happen when users link 3650 1.9 christos non-bss input sections to bss output sections, or emit data 3651 1.9 christos to a bss output section via a linker script. */ 3652 1.1 christos _bfd_error_handler 3653 1.1 christos (_("warning: section `%pA' type changed to PROGBITS"), asect); 3654 1.1 christos this_hdr->sh_type = sh_type; 3655 1.1 christos } 3656 1.1 christos 3657 1.1 christos switch (this_hdr->sh_type) 3658 1.1 christos { 3659 1.1 christos default: 3660 1.1 christos break; 3661 1.5 christos 3662 1.5 christos case SHT_STRTAB: 3663 1.5 christos case SHT_NOTE: 3664 1.5 christos case SHT_NOBITS: 3665 1.5 christos case SHT_PROGBITS: 3666 1.1 christos break; 3667 1.1 christos 3668 1.1 christos case SHT_INIT_ARRAY: 3669 1.5 christos case SHT_FINI_ARRAY: 3670 1.1 christos case SHT_PREINIT_ARRAY: 3671 1.1 christos this_hdr->sh_entsize = bed->s->arch_size / 8; 3672 1.1 christos break; 3673 1.1 christos 3674 1.1 christos case SHT_HASH: 3675 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_hash_entry; 3676 1.1 christos break; 3677 1.1 christos 3678 1.1 christos case SHT_DYNSYM: 3679 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_sym; 3680 1.1 christos break; 3681 1.1 christos 3682 1.1 christos case SHT_DYNAMIC: 3683 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_dyn; 3684 1.1 christos break; 3685 1.1 christos 3686 1.1 christos case SHT_RELA: 3687 1.1 christos if (get_elf_backend_data (abfd)->may_use_rela_p) 3688 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_rela; 3689 1.1 christos break; 3690 1.1 christos 3691 1.1 christos case SHT_REL: 3692 1.1 christos if (get_elf_backend_data (abfd)->may_use_rel_p) 3693 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_rel; 3694 1.1 christos break; 3695 1.1 christos 3696 1.1 christos case SHT_GNU_versym: 3697 1.1 christos this_hdr->sh_entsize = sizeof (Elf_External_Versym); 3698 1.1 christos break; 3699 1.1 christos 3700 1.1 christos case SHT_GNU_verdef: 3701 1.1 christos this_hdr->sh_entsize = 0; 3702 1.1 christos /* objcopy or strip will copy over sh_info, but may not set 3703 1.1 christos cverdefs. The linker will set cverdefs, but sh_info will be 3704 1.1 christos zero. */ 3705 1.1 christos if (this_hdr->sh_info == 0) 3706 1.1 christos this_hdr->sh_info = elf_tdata (abfd)->cverdefs; 3707 1.1 christos else 3708 1.1 christos BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 3709 1.1 christos || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); 3710 1.1 christos break; 3711 1.1 christos 3712 1.1 christos case SHT_GNU_verneed: 3713 1.1 christos this_hdr->sh_entsize = 0; 3714 1.1 christos /* objcopy or strip will copy over sh_info, but may not set 3715 1.1 christos cverrefs. The linker will set cverrefs, but sh_info will be 3716 1.1 christos zero. */ 3717 1.1 christos if (this_hdr->sh_info == 0) 3718 1.1 christos this_hdr->sh_info = elf_tdata (abfd)->cverrefs; 3719 1.1 christos else 3720 1.1 christos BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 3721 1.1 christos || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); 3722 1.1 christos break; 3723 1.1 christos 3724 1.1 christos case SHT_GROUP: 3725 1.1 christos this_hdr->sh_entsize = GRP_ENTRY_SIZE; 3726 1.1 christos break; 3727 1.1 christos 3728 1.1 christos case SHT_GNU_HASH: 3729 1.1 christos this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; 3730 1.1 christos break; 3731 1.1 christos } 3732 1.1 christos 3733 1.1 christos if ((asect->flags & SEC_ALLOC) != 0) 3734 1.1 christos this_hdr->sh_flags |= SHF_ALLOC; 3735 1.1 christos if ((asect->flags & SEC_READONLY) == 0) 3736 1.1 christos this_hdr->sh_flags |= SHF_WRITE; 3737 1.1 christos if ((asect->flags & SEC_CODE) != 0) 3738 1.1 christos this_hdr->sh_flags |= SHF_EXECINSTR; 3739 1.1 christos if ((asect->flags & SEC_MERGE) != 0) 3740 1.1 christos { 3741 1.1 christos this_hdr->sh_flags |= SHF_MERGE; 3742 1.5 christos this_hdr->sh_entsize = asect->entsize; 3743 1.14 christos } 3744 1.14 christos if ((asect->flags & SEC_STRINGS) != 0) 3745 1.14 christos { 3746 1.14 christos this_hdr->sh_flags |= SHF_STRINGS; 3747 1.1 christos this_hdr->sh_entsize = asect->entsize; 3748 1.1 christos } 3749 1.1 christos if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) 3750 1.1 christos this_hdr->sh_flags |= SHF_GROUP; 3751 1.1 christos if ((asect->flags & SEC_THREAD_LOCAL) != 0) 3752 1.1 christos { 3753 1.1 christos this_hdr->sh_flags |= SHF_TLS; 3754 1.1 christos if (asect->size == 0 3755 1.1 christos && (asect->flags & SEC_HAS_CONTENTS) == 0) 3756 1.1 christos { 3757 1.1 christos struct bfd_link_order *o = asect->map_tail.link_order; 3758 1.1 christos 3759 1.1 christos this_hdr->sh_size = 0; 3760 1.1 christos if (o != NULL) 3761 1.1 christos { 3762 1.1 christos this_hdr->sh_size = o->offset + o->size; 3763 1.1 christos if (this_hdr->sh_size != 0) 3764 1.1 christos this_hdr->sh_type = SHT_NOBITS; 3765 1.1 christos } 3766 1.1 christos } 3767 1.1 christos } 3768 1.1 christos if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) 3769 1.1 christos this_hdr->sh_flags |= SHF_EXCLUDE; 3770 1.1 christos 3771 1.1 christos /* If the section has relocs, set up a section header for the 3772 1.1 christos SHT_REL[A] section. If two relocation sections are required for 3773 1.1 christos this section, it is up to the processor-specific back-end to 3774 1.1 christos create the other. */ 3775 1.1 christos if ((asect->flags & SEC_RELOC) != 0) 3776 1.1 christos { 3777 1.1 christos /* When doing a relocatable link, create both REL and RELA sections if 3778 1.1 christos needed. */ 3779 1.1 christos if (arg->link_info 3780 1.3 christos /* Do the normal setup if we wouldn't create any sections here. */ 3781 1.3 christos && esd->rel.count + esd->rela.count > 0 3782 1.1 christos && (bfd_link_relocatable (arg->link_info) 3783 1.1 christos || arg->link_info->emitrelocations)) 3784 1.9 christos { 3785 1.14 christos if (esd->rel.count && esd->rel.hdr == NULL 3786 1.1 christos && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, 3787 1.12 christos false, delay_sh_name_p)) 3788 1.1 christos { 3789 1.1 christos arg->failed = true; 3790 1.1 christos return; 3791 1.9 christos } 3792 1.14 christos if (esd->rela.count && esd->rela.hdr == NULL 3793 1.1 christos && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, 3794 1.12 christos true, delay_sh_name_p)) 3795 1.1 christos { 3796 1.1 christos arg->failed = true; 3797 1.1 christos return; 3798 1.1 christos } 3799 1.1 christos } 3800 1.1 christos else if (!_bfd_elf_init_reloc_shdr (abfd, 3801 1.3 christos (asect->use_rela_p 3802 1.3 christos ? &esd->rela : &esd->rel), 3803 1.14 christos name, 3804 1.9 christos asect->use_rela_p, 3805 1.12 christos delay_sh_name_p)) 3806 1.9 christos { 3807 1.9 christos arg->failed = true; 3808 1.1 christos return; 3809 1.1 christos } 3810 1.1 christos } 3811 1.1 christos 3812 1.1 christos /* Check for processor-specific section types. */ 3813 1.1 christos sh_type = this_hdr->sh_type; 3814 1.9 christos if (bed->elf_backend_fake_sections 3815 1.12 christos && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) 3816 1.9 christos { 3817 1.9 christos arg->failed = true; 3818 1.1 christos return; 3819 1.1 christos } 3820 1.1 christos 3821 1.1 christos if (sh_type == SHT_NOBITS && asect->size != 0) 3822 1.1 christos { 3823 1.1 christos /* Don't change the header type from NOBITS if we are being 3824 1.1 christos called for objcopy --only-keep-debug. */ 3825 1.1 christos this_hdr->sh_type = sh_type; 3826 1.1 christos } 3827 1.1 christos } 3828 1.1 christos 3829 1.1 christos /* Fill in the contents of a SHT_GROUP section. Called from 3830 1.1 christos _bfd_elf_compute_section_file_positions for gas, objcopy, and 3831 1.1 christos when ELF targets use the generic linker, ld. Called for ld -r 3832 1.1 christos from bfd_elf_final_link. */ 3833 1.1 christos 3834 1.1 christos void 3835 1.12 christos bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) 3836 1.1 christos { 3837 1.1 christos bool *failedptr = (bool *) failedptrarg; 3838 1.12 christos asection *elt, *first; 3839 1.1 christos unsigned char *loc; 3840 1.1 christos bool gas; 3841 1.1 christos 3842 1.10 christos /* Ignore linker created group section. See elfNN_ia64_object_p in 3843 1.10 christos elfxx-ia64.c. */ 3844 1.1 christos if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP 3845 1.1 christos || sec->size == 0 3846 1.1 christos || *failedptr) 3847 1.1 christos return; 3848 1.1 christos 3849 1.1 christos if (elf_section_data (sec)->this_hdr.sh_info == 0) 3850 1.1 christos { 3851 1.1 christos unsigned long symindx = 0; 3852 1.1 christos 3853 1.1 christos /* elf_group_id will have been set up by objcopy and the 3854 1.1 christos generic linker. */ 3855 1.1 christos if (elf_group_id (sec) != NULL) 3856 1.1 christos symindx = elf_group_id (sec)->udata.i; 3857 1.1 christos 3858 1.1 christos if (symindx == 0) 3859 1.12 christos { 3860 1.12 christos /* If called from the assembler, swap_out_syms will have set up 3861 1.12 christos elf_section_syms. 3862 1.12 christos PR 25699: A corrupt input file could contain bogus group info. */ 3863 1.12 christos if (sec->index >= elf_num_section_syms (abfd) 3864 1.12 christos || elf_section_syms (abfd)[sec->index] == NULL) 3865 1.12 christos { 3866 1.12 christos *failedptr = true; 3867 1.1 christos return; 3868 1.1 christos } 3869 1.1 christos symindx = elf_section_syms (abfd)[sec->index]->udata.i; 3870 1.1 christos } 3871 1.1 christos elf_section_data (sec)->this_hdr.sh_info = symindx; 3872 1.1 christos } 3873 1.1 christos else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) 3874 1.1 christos { 3875 1.1 christos /* The ELF backend linker sets sh_info to -2 when the group 3876 1.9 christos signature symbol is global, and thus the index can't be 3877 1.9 christos set until all local symbols are output. */ 3878 1.9 christos asection *igroup; 3879 1.9 christos struct bfd_elf_section_data *sec_data; 3880 1.1 christos unsigned long symndx; 3881 1.1 christos unsigned long extsymoff; 3882 1.9 christos struct elf_link_hash_entry *h; 3883 1.9 christos 3884 1.9 christos /* The point of this little dance to the first SHF_GROUP section 3885 1.9 christos then back to the SHT_GROUP section is that this gets us to 3886 1.9 christos the SHT_GROUP in the input object. */ 3887 1.9 christos igroup = elf_sec_group (elf_next_in_group (sec)); 3888 1.9 christos sec_data = elf_section_data (igroup); 3889 1.1 christos symndx = sec_data->this_hdr.sh_info; 3890 1.1 christos extsymoff = 0; 3891 1.1 christos if (!elf_bad_symtab (igroup->owner)) 3892 1.1 christos { 3893 1.1 christos Elf_Internal_Shdr *symtab_hdr; 3894 1.1 christos 3895 1.1 christos symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; 3896 1.1 christos extsymoff = symtab_hdr->sh_info; 3897 1.1 christos } 3898 1.1 christos h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; 3899 1.1 christos while (h->root.type == bfd_link_hash_indirect 3900 1.1 christos || h->root.type == bfd_link_hash_warning) 3901 1.1 christos h = (struct elf_link_hash_entry *) h->root.u.i.link; 3902 1.1 christos 3903 1.1 christos elf_section_data (sec)->this_hdr.sh_info = h->indx; 3904 1.1 christos } 3905 1.12 christos 3906 1.1 christos /* The contents won't be allocated for "ld -r" or objcopy. */ 3907 1.1 christos gas = true; 3908 1.12 christos if (sec->contents == NULL) 3909 1.1 christos { 3910 1.1 christos gas = false; 3911 1.1 christos sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); 3912 1.1 christos 3913 1.1 christos /* Arrange for the section to be written out. */ 3914 1.1 christos elf_section_data (sec)->this_hdr.contents = sec->contents; 3915 1.12 christos if (sec->contents == NULL) 3916 1.1 christos { 3917 1.1 christos *failedptr = true; 3918 1.14 christos return; 3919 1.1 christos } 3920 1.1 christos sec->alloced = 1; 3921 1.1 christos } 3922 1.1 christos 3923 1.1 christos loc = sec->contents + sec->size; 3924 1.1 christos 3925 1.1 christos /* Get the pointer to the first section in the group that gas 3926 1.1 christos squirreled away here. objcopy arranges for this to be set to the 3927 1.1 christos start of the input section group. */ 3928 1.1 christos first = elt = elf_next_in_group (sec); 3929 1.1 christos 3930 1.1 christos /* First element is a flag word. Rest of section is elf section 3931 1.1 christos indices for all the sections of the group. Write them backwards 3932 1.1 christos just to keep the group in the same order as given in .section 3933 1.1 christos directives, not that it matters. */ 3934 1.1 christos while (elt != NULL) 3935 1.1 christos { 3936 1.1 christos asection *s; 3937 1.1 christos 3938 1.1 christos s = elt; 3939 1.1 christos if (!gas) 3940 1.1 christos s = s->output_section; 3941 1.1 christos if (s != NULL 3942 1.9 christos && !bfd_is_abs_section (s)) 3943 1.9 christos { 3944 1.1 christos struct bfd_elf_section_data *elf_sec = elf_section_data (s); 3945 1.9 christos struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt); 3946 1.9 christos 3947 1.9 christos if (elf_sec->rel.hdr != NULL 3948 1.9 christos && (gas 3949 1.9 christos || (input_elf_sec->rel.hdr != NULL 3950 1.9 christos && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)) 3951 1.9 christos { 3952 1.13 christos elf_sec->rel.hdr->sh_flags |= SHF_GROUP; 3953 1.13 christos loc -= 4; 3954 1.9 christos if (loc == sec->contents) 3955 1.9 christos break; 3956 1.9 christos H_PUT_32 (abfd, elf_sec->rel.idx, loc); 3957 1.9 christos } 3958 1.9 christos if (elf_sec->rela.hdr != NULL 3959 1.9 christos && (gas 3960 1.9 christos || (input_elf_sec->rela.hdr != NULL 3961 1.9 christos && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)) 3962 1.9 christos { 3963 1.13 christos elf_sec->rela.hdr->sh_flags |= SHF_GROUP; 3964 1.13 christos loc -= 4; 3965 1.9 christos if (loc == sec->contents) 3966 1.9 christos break; 3967 1.1 christos H_PUT_32 (abfd, elf_sec->rela.idx, loc); 3968 1.13 christos } 3969 1.13 christos loc -= 4; 3970 1.9 christos if (loc == sec->contents) 3971 1.1 christos break; 3972 1.1 christos H_PUT_32 (abfd, elf_sec->this_idx, loc); 3973 1.1 christos } 3974 1.1 christos elt = elf_next_in_group (elt); 3975 1.1 christos if (elt == first) 3976 1.1 christos break; 3977 1.14 christos } 3978 1.14 christos 3979 1.14 christos /* We should always get here with loc == sec->contents + 4. Return 3980 1.14 christos an error for bogus SHT_GROUP sections. */ 3981 1.13 christos loc -= 4; 3982 1.14 christos if (loc != sec->contents) 3983 1.14 christos { 3984 1.14 christos /* xgettext:c-format */ 3985 1.14 christos _bfd_error_handler (_("%pB: corrupted group section: `%pA'"), 3986 1.14 christos abfd, sec); 3987 1.14 christos bfd_set_error (bfd_error_bad_value); 3988 1.13 christos *failedptr = true; 3989 1.1 christos return; 3990 1.1 christos } 3991 1.1 christos 3992 1.1 christos H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); 3993 1.9 christos } 3994 1.9 christos 3995 1.9 christos /* Given NAME, the name of a relocation section stripped of its 3996 1.3 christos .rel/.rela prefix, return the section in ABFD to which the 3997 1.3 christos relocations apply. */ 3998 1.9 christos 3999 1.9 christos asection * 4000 1.9 christos _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name) 4001 1.9 christos { 4002 1.9 christos /* If a target needs .got.plt section, relocations in rela.plt/rel.plt 4003 1.9 christos section likely apply to .got.plt or .got section. */ 4004 1.9 christos if (get_elf_backend_data (abfd)->want_got_plt 4005 1.9 christos && strcmp (name, ".plt") == 0) 4006 1.9 christos { 4007 1.9 christos asection *sec; 4008 1.9 christos 4009 1.9 christos name = ".got.plt"; 4010 1.9 christos sec = bfd_get_section_by_name (abfd, name); 4011 1.9 christos if (sec != NULL) 4012 1.9 christos return sec; 4013 1.9 christos name = ".got"; 4014 1.9 christos } 4015 1.9 christos 4016 1.9 christos return bfd_get_section_by_name (abfd, name); 4017 1.9 christos } 4018 1.9 christos 4019 1.9 christos /* Return the section to which RELOC_SEC applies. */ 4020 1.9 christos 4021 1.3 christos static asection * 4022 1.3 christos elf_get_reloc_section (asection *reloc_sec) 4023 1.3 christos { 4024 1.3 christos const char *name; 4025 1.9 christos unsigned int type; 4026 1.3 christos bfd *abfd; 4027 1.3 christos const struct elf_backend_data *bed; 4028 1.3 christos 4029 1.3 christos type = elf_section_data (reloc_sec)->this_hdr.sh_type; 4030 1.3 christos if (type != SHT_REL && type != SHT_RELA) 4031 1.3 christos return NULL; 4032 1.3 christos 4033 1.12 christos /* We look up the section the relocs apply to by name. */ 4034 1.9 christos name = reloc_sec->name; 4035 1.9 christos if (!startswith (name, ".rel")) 4036 1.9 christos return NULL; 4037 1.9 christos name += 4; 4038 1.3 christos if (type == SHT_RELA && *name++ != 'a') 4039 1.3 christos return NULL; 4040 1.9 christos 4041 1.9 christos abfd = reloc_sec->owner; 4042 1.3 christos bed = get_elf_backend_data (abfd); 4043 1.3 christos return bed->get_reloc_section (abfd, name); 4044 1.1 christos } 4045 1.1 christos 4046 1.12 christos /* Assign all ELF section numbers. The dummy first section is handled here 4047 1.13 christos too. The link/info pointers for the standard section types are filled 4048 1.1 christos in here too, while we're at it. LINK_INFO will be 0 when arriving 4049 1.12 christos here for gas, objcopy, and when using the generic ELF linker. */ 4050 1.1 christos 4051 1.1 christos static bool 4052 1.1 christos assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) 4053 1.1 christos { 4054 1.3 christos struct elf_obj_tdata *t = elf_tdata (abfd); 4055 1.1 christos asection *sec; 4056 1.1 christos unsigned int section_number; 4057 1.12 christos Elf_Internal_Shdr **i_shdrp; 4058 1.12 christos struct bfd_elf_section_data *d; 4059 1.1 christos bool need_symtab; 4060 1.1 christos size_t amt; 4061 1.1 christos 4062 1.1 christos section_number = 1; 4063 1.1 christos 4064 1.1 christos _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); 4065 1.9 christos 4066 1.1 christos /* SHT_GROUP sections are in relocatable files only. */ 4067 1.5 christos if (link_info == NULL || !link_info->resolve_section_groups) 4068 1.5 christos { 4069 1.1 christos size_t reloc_count = 0; 4070 1.1 christos 4071 1.1 christos /* Put SHT_GROUP sections first. */ 4072 1.1 christos for (sec = abfd->sections; sec != NULL; sec = sec->next) 4073 1.1 christos { 4074 1.1 christos d = elf_section_data (sec); 4075 1.1 christos 4076 1.1 christos if (d->this_hdr.sh_type == SHT_GROUP) 4077 1.1 christos { 4078 1.1 christos if (sec->flags & SEC_LINKER_CREATED) 4079 1.1 christos { 4080 1.1 christos /* Remove the linker created SHT_GROUP sections. */ 4081 1.1 christos bfd_section_list_remove (abfd, sec); 4082 1.1 christos abfd->section_count--; 4083 1.1 christos } 4084 1.1 christos else 4085 1.5 christos d->this_idx = section_number++; 4086 1.5 christos } 4087 1.5 christos 4088 1.1 christos /* Count relocations. */ 4089 1.5 christos reloc_count += sec->reloc_count; 4090 1.13 christos } 4091 1.5 christos 4092 1.5 christos /* Set/clear HAS_RELOC depending on whether there are relocations. */ 4093 1.13 christos if (reloc_count == 0) 4094 1.13 christos abfd->flags &= ~HAS_RELOC; 4095 1.1 christos else 4096 1.1 christos abfd->flags |= HAS_RELOC; 4097 1.1 christos } 4098 1.1 christos 4099 1.1 christos for (sec = abfd->sections; sec; sec = sec->next) 4100 1.1 christos { 4101 1.1 christos d = elf_section_data (sec); 4102 1.1 christos 4103 1.3 christos if (d->this_hdr.sh_type != SHT_GROUP) 4104 1.3 christos d->this_idx = section_number++; 4105 1.1 christos if (d->this_hdr.sh_name != (unsigned int) -1) 4106 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); 4107 1.1 christos if (d->rel.hdr) 4108 1.3 christos { 4109 1.3 christos d->rel.idx = section_number++; 4110 1.1 christos if (d->rel.hdr->sh_name != (unsigned int) -1) 4111 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); 4112 1.1 christos } 4113 1.1 christos else 4114 1.1 christos d->rel.idx = 0; 4115 1.1 christos 4116 1.1 christos if (d->rela.hdr) 4117 1.3 christos { 4118 1.3 christos d->rela.idx = section_number++; 4119 1.1 christos if (d->rela.hdr->sh_name != (unsigned int) -1) 4120 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); 4121 1.1 christos } 4122 1.1 christos else 4123 1.1 christos d->rela.idx = 0; 4124 1.1 christos } 4125 1.12 christos 4126 1.12 christos need_symtab = (bfd_get_symcount (abfd) > 0 4127 1.12 christos || (link_info == NULL 4128 1.1 christos && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) 4129 1.1 christos == HAS_RELOC))); 4130 1.3 christos if (need_symtab) 4131 1.1 christos { 4132 1.1 christos elf_onesymtab (abfd) = section_number++; 4133 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); 4134 1.10 christos if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) 4135 1.3 christos { 4136 1.3 christos elf_section_list *entry; 4137 1.3 christos 4138 1.10 christos BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); 4139 1.3 christos 4140 1.3 christos entry = bfd_zalloc (abfd, sizeof (*entry)); 4141 1.3 christos entry->ndx = section_number++; 4142 1.1 christos elf_symtab_shndx_list (abfd) = entry; 4143 1.12 christos entry->hdr.sh_name 4144 1.3 christos = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 4145 1.12 christos ".symtab_shndx", false); 4146 1.1 christos if (entry->hdr.sh_name == (unsigned int) -1) 4147 1.3 christos return false; 4148 1.1 christos } 4149 1.1 christos elf_strtab_sec (abfd) = section_number++; 4150 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); 4151 1.9 christos } 4152 1.9 christos 4153 1.9 christos elf_shstrtab_sec (abfd) = section_number++; 4154 1.9 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); 4155 1.1 christos elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); 4156 1.1 christos 4157 1.9 christos if (section_number >= SHN_LORESERVE) 4158 1.9 christos { 4159 1.1 christos /* xgettext:c-format */ 4160 1.12 christos _bfd_error_handler (_("%pB: too many sections: %u"), 4161 1.1 christos abfd, section_number); 4162 1.1 christos return false; 4163 1.1 christos } 4164 1.1 christos 4165 1.1 christos elf_numsections (abfd) = section_number; 4166 1.1 christos elf_elfheader (abfd)->e_shnum = section_number; 4167 1.1 christos 4168 1.12 christos /* Set up the list of section header pointers, in agreement with the 4169 1.12 christos indices. */ 4170 1.1 christos amt = section_number * sizeof (Elf_Internal_Shdr *); 4171 1.12 christos i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt); 4172 1.1 christos if (i_shdrp == NULL) 4173 1.1 christos return false; 4174 1.9 christos 4175 1.1 christos i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, 4176 1.1 christos sizeof (Elf_Internal_Shdr)); 4177 1.1 christos if (i_shdrp[0] == NULL) 4178 1.12 christos { 4179 1.1 christos bfd_release (abfd, i_shdrp); 4180 1.1 christos return false; 4181 1.1 christos } 4182 1.1 christos 4183 1.3 christos elf_elfsections (abfd) = i_shdrp; 4184 1.1 christos 4185 1.1 christos i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; 4186 1.3 christos if (need_symtab) 4187 1.1 christos { 4188 1.1 christos i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; 4189 1.3 christos if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) 4190 1.3 christos { 4191 1.3 christos elf_section_list * entry = elf_symtab_shndx_list (abfd); 4192 1.3 christos BFD_ASSERT (entry != NULL); 4193 1.1 christos i_shdrp[entry->ndx] = & entry->hdr; 4194 1.3 christos entry->hdr.sh_link = elf_onesymtab (abfd); 4195 1.3 christos } 4196 1.1 christos i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; 4197 1.1 christos t->symtab_hdr.sh_link = elf_strtab_sec (abfd); 4198 1.1 christos } 4199 1.1 christos 4200 1.1 christos for (sec = abfd->sections; sec; sec = sec->next) 4201 1.1 christos { 4202 1.1 christos asection *s; 4203 1.1 christos 4204 1.1 christos d = elf_section_data (sec); 4205 1.1 christos 4206 1.1 christos i_shdrp[d->this_idx] = &d->this_hdr; 4207 1.1 christos if (d->rel.idx != 0) 4208 1.1 christos i_shdrp[d->rel.idx] = d->rel.hdr; 4209 1.1 christos if (d->rela.idx != 0) 4210 1.1 christos i_shdrp[d->rela.idx] = d->rela.hdr; 4211 1.1 christos 4212 1.1 christos /* Fill in the sh_link and sh_info fields while we're at it. */ 4213 1.1 christos 4214 1.1 christos /* sh_link of a reloc section is the section index of the symbol 4215 1.1 christos table. sh_info is the section index of the section to which 4216 1.1 christos the relocation entries apply. */ 4217 1.3 christos if (d->rel.idx != 0) 4218 1.1 christos { 4219 1.3 christos d->rel.hdr->sh_link = elf_onesymtab (abfd); 4220 1.1 christos d->rel.hdr->sh_info = d->this_idx; 4221 1.1 christos d->rel.hdr->sh_flags |= SHF_INFO_LINK; 4222 1.1 christos } 4223 1.3 christos if (d->rela.idx != 0) 4224 1.1 christos { 4225 1.3 christos d->rela.hdr->sh_link = elf_onesymtab (abfd); 4226 1.1 christos d->rela.hdr->sh_info = d->this_idx; 4227 1.1 christos d->rela.hdr->sh_flags |= SHF_INFO_LINK; 4228 1.1 christos } 4229 1.1 christos 4230 1.1 christos /* We need to set up sh_link for SHF_LINK_ORDER. */ 4231 1.1 christos if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) 4232 1.12 christos { 4233 1.12 christos s = elf_linked_to_section (sec); 4234 1.12 christos /* We can now have a NULL linked section pointer. 4235 1.12 christos This happens when the sh_link field is 0, which is done 4236 1.1 christos when a linked to section is discarded but the linking 4237 1.1 christos section has been retained for some reason. */ 4238 1.12 christos if (s) 4239 1.12 christos { 4240 1.1 christos /* Check discarded linkonce section. */ 4241 1.12 christos if (discarded_section (s)) 4242 1.12 christos { 4243 1.12 christos asection *kept; 4244 1.12 christos _bfd_error_handler 4245 1.12 christos /* xgettext:c-format */ 4246 1.12 christos (_("%pB: sh_link of section `%pA' points to" 4247 1.12 christos " discarded section `%pA' of `%pB'"), 4248 1.12 christos abfd, d->this_hdr.bfd_section, s, s->owner); 4249 1.12 christos /* Point to the kept section if it has the same 4250 1.12 christos size as the discarded one. */ 4251 1.1 christos kept = _bfd_elf_check_kept_section (s, link_info); 4252 1.12 christos if (kept == NULL) 4253 1.12 christos { 4254 1.1 christos bfd_set_error (bfd_error_bad_value); 4255 1.12 christos return false; 4256 1.1 christos } 4257 1.12 christos s = kept; 4258 1.12 christos } 4259 1.1 christos /* Handle objcopy. */ 4260 1.12 christos else if (s->output_section == NULL) 4261 1.12 christos { 4262 1.12 christos _bfd_error_handler 4263 1.12 christos /* xgettext:c-format */ 4264 1.12 christos (_("%pB: sh_link of section `%pA' points to" 4265 1.12 christos " removed section `%pA' of `%pB'"), 4266 1.12 christos abfd, d->this_hdr.bfd_section, s, s->owner); 4267 1.1 christos bfd_set_error (bfd_error_bad_value); 4268 1.12 christos return false; 4269 1.1 christos } 4270 1.1 christos s = s->output_section; 4271 1.1 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4272 1.1 christos } 4273 1.1 christos } 4274 1.1 christos 4275 1.1 christos switch (d->this_hdr.sh_type) 4276 1.1 christos { 4277 1.13 christos case SHT_REL: 4278 1.13 christos case SHT_RELA: 4279 1.13 christos /* sh_link is the section index of the symbol table. 4280 1.13 christos sh_info is the section index of the section to which the 4281 1.12 christos relocation entries apply. */ 4282 1.13 christos if (d->this_hdr.sh_link == 0) 4283 1.13 christos { 4284 1.13 christos /* FIXME maybe: If this is a reloc section which we are 4285 1.13 christos treating as a normal section then we likely should 4286 1.13 christos not be assuming its sh_link is .dynsym or .symtab. */ 4287 1.13 christos if ((sec->flags & SEC_ALLOC) != 0) 4288 1.13 christos { 4289 1.13 christos s = bfd_get_section_by_name (abfd, ".dynsym"); 4290 1.13 christos if (s != NULL) 4291 1.13 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4292 1.13 christos } 4293 1.12 christos else 4294 1.1 christos d->this_hdr.sh_link = elf_onesymtab (abfd); 4295 1.9 christos } 4296 1.1 christos 4297 1.3 christos s = elf_get_reloc_section (sec); 4298 1.3 christos if (s != NULL) 4299 1.3 christos { 4300 1.3 christos d->this_hdr.sh_info = elf_section_data (s)->this_idx; 4301 1.1 christos d->this_hdr.sh_flags |= SHF_INFO_LINK; 4302 1.1 christos } 4303 1.1 christos break; 4304 1.1 christos 4305 1.1 christos case SHT_STRTAB: 4306 1.1 christos /* We assume that a section named .stab*str is a stabs 4307 1.1 christos string section. We look for a section with the same name 4308 1.12 christos but without the trailing ``str'', and set its sh_link 4309 1.1 christos field to point to this section. */ 4310 1.1 christos if (startswith (sec->name, ".stab") 4311 1.1 christos && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) 4312 1.1 christos { 4313 1.1 christos size_t len; 4314 1.1 christos char *alc; 4315 1.1 christos 4316 1.1 christos len = strlen (sec->name); 4317 1.12 christos alc = (char *) bfd_malloc (len - 2); 4318 1.1 christos if (alc == NULL) 4319 1.1 christos return false; 4320 1.1 christos memcpy (alc, sec->name, len - 3); 4321 1.1 christos alc[len - 3] = '\0'; 4322 1.1 christos s = bfd_get_section_by_name (abfd, alc); 4323 1.1 christos free (alc); 4324 1.1 christos if (s != NULL) 4325 1.1 christos { 4326 1.1 christos elf_section_data (s)->this_hdr.sh_link = d->this_idx; 4327 1.12 christos 4328 1.1 christos /* This is a .stab section. */ 4329 1.1 christos elf_section_data (s)->this_hdr.sh_entsize = 12; 4330 1.1 christos } 4331 1.1 christos } 4332 1.1 christos break; 4333 1.1 christos 4334 1.1 christos case SHT_DYNAMIC: 4335 1.1 christos case SHT_DYNSYM: 4336 1.1 christos case SHT_GNU_verneed: 4337 1.1 christos case SHT_GNU_verdef: 4338 1.1 christos /* sh_link is the section header index of the string table 4339 1.1 christos used for the dynamic entries, or the symbol table, or the 4340 1.1 christos version strings. */ 4341 1.1 christos s = bfd_get_section_by_name (abfd, ".dynstr"); 4342 1.1 christos if (s != NULL) 4343 1.1 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4344 1.1 christos break; 4345 1.1 christos 4346 1.1 christos case SHT_GNU_LIBLIST: 4347 1.1 christos /* sh_link is the section header index of the prelink library 4348 1.12 christos list used for the dynamic entries, or the symbol table, or 4349 1.12 christos the version strings. */ 4350 1.1 christos s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC) 4351 1.1 christos ? ".dynstr" : ".gnu.libstr")); 4352 1.1 christos if (s != NULL) 4353 1.1 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4354 1.1 christos break; 4355 1.1 christos 4356 1.1 christos case SHT_HASH: 4357 1.1 christos case SHT_GNU_HASH: 4358 1.1 christos case SHT_GNU_versym: 4359 1.1 christos /* sh_link is the section header index of the symbol table 4360 1.1 christos this hash table or version table is for. */ 4361 1.1 christos s = bfd_get_section_by_name (abfd, ".dynsym"); 4362 1.1 christos if (s != NULL) 4363 1.1 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4364 1.1 christos break; 4365 1.3 christos 4366 1.1 christos case SHT_GROUP: 4367 1.1 christos d->this_hdr.sh_link = elf_onesymtab (abfd); 4368 1.1 christos } 4369 1.3 christos } 4370 1.3 christos 4371 1.3 christos /* Delay setting sh_name to _bfd_elf_write_object_contents so that 4372 1.3 christos _bfd_elf_assign_file_positions_for_non_load can convert DWARF 4373 1.12 christos debug section name from .debug_* to .zdebug_* if needed. */ 4374 1.1 christos 4375 1.1 christos return true; 4376 1.12 christos } 4377 1.1 christos 4378 1.1 christos static bool 4379 1.1 christos sym_is_global (bfd *abfd, asymbol *sym) 4380 1.1 christos { 4381 1.1 christos /* If the backend has a special mapping, use it. */ 4382 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4383 1.1 christos if (bed->elf_backend_sym_is_global) 4384 1.1 christos return (*bed->elf_backend_sym_is_global) (abfd, sym); 4385 1.10 christos 4386 1.10 christos return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 4387 1.1 christos || bfd_is_und_section (bfd_asymbol_section (sym)) 4388 1.1 christos || bfd_is_com_section (bfd_asymbol_section (sym))); 4389 1.9 christos } 4390 1.9 christos 4391 1.9 christos /* Filter global symbols of ABFD to include in the import library. All 4392 1.9 christos SYMCOUNT symbols of ABFD can be examined from their pointers in 4393 1.9 christos SYMS. Pointers of symbols to keep should be stored contiguously at 4394 1.9 christos the beginning of that array. 4395 1.9 christos 4396 1.9 christos Returns the number of symbols to keep. */ 4397 1.9 christos 4398 1.9 christos unsigned int 4399 1.9 christos _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, 4400 1.9 christos asymbol **syms, long symcount) 4401 1.9 christos { 4402 1.9 christos long src_count, dst_count = 0; 4403 1.9 christos 4404 1.9 christos for (src_count = 0; src_count < symcount; src_count++) 4405 1.9 christos { 4406 1.9 christos asymbol *sym = syms[src_count]; 4407 1.9 christos char *name = (char *) bfd_asymbol_name (sym); 4408 1.9 christos struct bfd_link_hash_entry *h; 4409 1.9 christos 4410 1.9 christos if (!sym_is_global (abfd, sym)) 4411 1.12 christos continue; 4412 1.9 christos 4413 1.9 christos h = bfd_link_hash_lookup (info->hash, name, false, false, false); 4414 1.9 christos if (h == NULL) 4415 1.9 christos continue; 4416 1.9 christos if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak) 4417 1.9 christos continue; 4418 1.9 christos if (h->linker_def || h->ldscript_def) 4419 1.9 christos continue; 4420 1.9 christos 4421 1.9 christos syms[dst_count++] = sym; 4422 1.9 christos } 4423 1.9 christos 4424 1.9 christos syms[dst_count] = NULL; 4425 1.9 christos 4426 1.9 christos return dst_count; 4427 1.14 christos } 4428 1.14 christos 4429 1.1 christos /* Don't output symbols for sections that are not going to be output, 4430 1.12 christos that are duplicates or there is no BFD section. */ 4431 1.14 christos 4432 1.1 christos static bool 4433 1.9 christos ignore_sym (asymbol *sym) 4434 1.12 christos { 4435 1.9 christos if (sym == NULL) 4436 1.14 christos return false; 4437 1.12 christos 4438 1.1 christos if (sym->section == NULL) 4439 1.14 christos return true; 4440 1.14 christos 4441 1.14 christos if ((sym->flags & BSF_SECTION_SYM) != 0) 4442 1.14 christos { 4443 1.14 christos if ((sym->flags & BSF_SECTION_SYM_USED) == 0) 4444 1.14 christos return true; 4445 1.14 christos /* With ld -r on generic elf targets it is possible to have 4446 1.14 christos multiple section symbols in the output for a given section. 4447 1.14 christos We'd like to get rid of all but the first one. This drops 4448 1.14 christos them if the first input section is non-zero size, but fails 4449 1.14 christos to do so if the first input section is zero sized. */ 4450 1.14 christos if (sym->section->output_offset != 0) 4451 1.9 christos return true; 4452 1.14 christos } 4453 1.1 christos 4454 1.1 christos return discarded_section (sym->section); 4455 1.1 christos } 4456 1.1 christos 4457 1.1 christos /* Map symbol from it's internal number to the external number, moving 4458 1.12 christos all local symbols to be at the head of the list. */ 4459 1.3 christos 4460 1.1 christos static bool 4461 1.1 christos elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) 4462 1.1 christos { 4463 1.1 christos unsigned int symcount = bfd_get_symcount (abfd); 4464 1.1 christos asymbol **syms = bfd_get_outsymbols (abfd); 4465 1.1 christos asymbol **sect_syms; 4466 1.3 christos unsigned int num_locals = 0; 4467 1.1 christos unsigned int num_globals = 0; 4468 1.1 christos unsigned int max_index = 0; 4469 1.1 christos unsigned int idx; 4470 1.12 christos asection *asect; 4471 1.1 christos asymbol **new_syms; 4472 1.1 christos size_t amt; 4473 1.1 christos 4474 1.1 christos #ifdef DEBUG 4475 1.1 christos fprintf (stderr, "elf_map_symbols\n"); 4476 1.1 christos fflush (stderr); 4477 1.1 christos #endif 4478 1.1 christos 4479 1.1 christos for (asect = abfd->sections; asect; asect = asect->next) 4480 1.1 christos { 4481 1.1 christos if (max_index < asect->index) 4482 1.1 christos max_index = asect->index; 4483 1.1 christos } 4484 1.12 christos 4485 1.12 christos max_index++; 4486 1.1 christos amt = max_index * sizeof (asymbol *); 4487 1.12 christos sect_syms = (asymbol **) bfd_zalloc (abfd, amt); 4488 1.1 christos if (sect_syms == NULL) 4489 1.1 christos return false; 4490 1.1 christos elf_section_syms (abfd) = sect_syms; 4491 1.1 christos elf_num_section_syms (abfd) = max_index; 4492 1.1 christos 4493 1.1 christos /* Init sect_syms entries for any section symbols we have already 4494 1.1 christos decided to output. */ 4495 1.1 christos for (idx = 0; idx < symcount; idx++) 4496 1.1 christos { 4497 1.1 christos asymbol *sym = syms[idx]; 4498 1.1 christos 4499 1.14 christos if ((sym->flags & BSF_SECTION_SYM) != 0 4500 1.1 christos && sym->value == 0 4501 1.1 christos && !ignore_sym (sym) 4502 1.1 christos && !bfd_is_abs_section (sym->section)) 4503 1.1 christos { 4504 1.1 christos asection *sec = sym->section; 4505 1.14 christos 4506 1.14 christos if (sec->owner != abfd) 4507 1.14 christos { 4508 1.14 christos sec = sec->output_section; 4509 1.14 christos if (sec == NULL) 4510 1.1 christos return false; 4511 1.1 christos } 4512 1.1 christos 4513 1.1 christos sect_syms[sec->index] = syms[idx]; 4514 1.1 christos } 4515 1.1 christos } 4516 1.1 christos 4517 1.1 christos /* Classify all of the symbols. */ 4518 1.14 christos for (idx = 0; idx < symcount; idx++) 4519 1.14 christos { 4520 1.1 christos if (ignore_sym (syms[idx])) 4521 1.1 christos continue; 4522 1.14 christos if (sym_is_global (abfd, syms[idx])) 4523 1.1 christos num_globals++; 4524 1.1 christos else 4525 1.1 christos num_locals++; 4526 1.1 christos } 4527 1.1 christos 4528 1.1 christos /* We will be adding a section symbol for each normal BFD section. Most 4529 1.1 christos sections will already have a section symbol in outsymbols, but 4530 1.1 christos eg. SHT_GROUP sections will not, and we need the section symbol mapped 4531 1.1 christos at least in that case. */ 4532 1.12 christos for (asect = abfd->sections; asect; asect = asect->next) 4533 1.12 christos { 4534 1.14 christos asymbol *sym = asect->symbol; 4535 1.12 christos /* Don't include ignored section symbols. */ 4536 1.1 christos if (!ignore_sym (sym) 4537 1.14 christos && sect_syms[asect->index] == NULL) 4538 1.14 christos { 4539 1.14 christos if (sym_is_global (abfd, asect->symbol)) 4540 1.1 christos num_globals++; 4541 1.1 christos else 4542 1.1 christos num_locals++; 4543 1.1 christos } 4544 1.1 christos } 4545 1.12 christos 4546 1.12 christos /* Now sort the symbols so the local symbols are first. */ 4547 1.1 christos amt = (num_locals + num_globals) * sizeof (asymbol *); 4548 1.12 christos new_syms = (asymbol **) bfd_alloc (abfd, amt); 4549 1.1 christos if (new_syms == NULL) 4550 1.14 christos return false; 4551 1.14 christos 4552 1.1 christos unsigned int num_globals2 = 0; 4553 1.1 christos unsigned int num_locals2 = 0; 4554 1.1 christos for (idx = 0; idx < symcount; idx++) 4555 1.1 christos { 4556 1.1 christos asymbol *sym = syms[idx]; 4557 1.14 christos unsigned int i; 4558 1.14 christos 4559 1.14 christos if (ignore_sym (sym)) 4560 1.1 christos continue; 4561 1.1 christos 4562 1.14 christos if (sym_is_global (abfd, sym)) 4563 1.1 christos i = num_locals + num_globals2++; 4564 1.1 christos else 4565 1.1 christos i = num_locals2++; 4566 1.1 christos new_syms[i] = sym; 4567 1.1 christos sym->udata.i = i + 1; 4568 1.1 christos } 4569 1.12 christos for (asect = abfd->sections; asect; asect = asect->next) 4570 1.14 christos { 4571 1.12 christos asymbol *sym = asect->symbol; 4572 1.1 christos if (!ignore_sym (sym) 4573 1.1 christos && sect_syms[asect->index] == NULL) 4574 1.1 christos { 4575 1.1 christos unsigned int i; 4576 1.14 christos 4577 1.14 christos sect_syms[asect->index] = sym; 4578 1.14 christos if (sym_is_global (abfd, sym)) 4579 1.1 christos i = num_locals + num_globals2++; 4580 1.1 christos else 4581 1.1 christos i = num_locals2++; 4582 1.1 christos new_syms[i] = sym; 4583 1.1 christos sym->udata.i = i + 1; 4584 1.1 christos } 4585 1.1 christos } 4586 1.1 christos 4587 1.3 christos bfd_set_symtab (abfd, new_syms, num_locals + num_globals); 4588 1.12 christos 4589 1.1 christos *pnum_locals = num_locals; 4590 1.1 christos return true; 4591 1.1 christos } 4592 1.1 christos 4593 1.1 christos /* Assign a file position to a section, optionally aligning to the 4594 1.1 christos required section alignment. */ 4595 1.1 christos 4596 1.1 christos file_ptr 4597 1.14 christos _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, 4598 1.14 christos file_ptr offset, 4599 1.1 christos bool align, 4600 1.14 christos unsigned char log_file_align) 4601 1.14 christos { 4602 1.14 christos if (i_shdrp->sh_addralign > 1) 4603 1.14 christos { 4604 1.14 christos file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign; 4605 1.14 christos 4606 1.14 christos if (align) 4607 1.14 christos offset = BFD_ALIGN (offset, salign); 4608 1.14 christos else if (log_file_align) 4609 1.14 christos { 4610 1.14 christos /* Heuristic: Cap alignment at log_file_align. */ 4611 1.14 christos file_ptr falign = 1u << log_file_align; 4612 1.14 christos 4613 1.14 christos offset = BFD_ALIGN (offset, salign < falign ? salign : falign); 4614 1.1 christos } 4615 1.1 christos } 4616 1.1 christos i_shdrp->sh_offset = offset; 4617 1.1 christos if (i_shdrp->bfd_section != NULL) 4618 1.1 christos i_shdrp->bfd_section->filepos = offset; 4619 1.1 christos if (i_shdrp->sh_type != SHT_NOBITS) 4620 1.1 christos offset += i_shdrp->sh_size; 4621 1.1 christos return offset; 4622 1.1 christos } 4623 1.1 christos 4624 1.1 christos /* Compute the file positions we are going to put the sections at, and 4625 1.1 christos otherwise prepare to begin writing out the ELF file. If LINK_INFO 4626 1.12 christos is not NULL, this is being called by the ELF backend linker. */ 4627 1.1 christos 4628 1.1 christos bool 4629 1.1 christos _bfd_elf_compute_section_file_positions (bfd *abfd, 4630 1.1 christos struct bfd_link_info *link_info) 4631 1.1 christos { 4632 1.12 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4633 1.3 christos struct fake_section_arg fsargs; 4634 1.1 christos bool failed; 4635 1.12 christos struct elf_strtab_hash *strtab = NULL; 4636 1.1 christos Elf_Internal_Shdr *shstrtab_hdr; 4637 1.1 christos bool need_symtab; 4638 1.12 christos 4639 1.1 christos if (abfd->output_has_begun) 4640 1.1 christos return true; 4641 1.1 christos 4642 1.1 christos /* Do any elf backend specific processing first. */ 4643 1.1 christos if (bed->elf_backend_begin_write_processing) 4644 1.10 christos (*bed->elf_backend_begin_write_processing) (abfd, link_info); 4645 1.12 christos 4646 1.1 christos if (!(*bed->elf_backend_init_file_header) (abfd, link_info)) 4647 1.12 christos return false; 4648 1.1 christos 4649 1.1 christos fsargs.failed = false; 4650 1.1 christos fsargs.link_info = link_info; 4651 1.12 christos bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); 4652 1.1 christos if (fsargs.failed) 4653 1.1 christos return false; 4654 1.12 christos 4655 1.1 christos if (!assign_section_numbers (abfd, link_info)) 4656 1.1 christos return false; 4657 1.1 christos 4658 1.1 christos /* The backend linker builds symbol table information itself. */ 4659 1.1 christos need_symtab = (link_info == NULL 4660 1.1 christos && (bfd_get_symcount (abfd) > 0 4661 1.1 christos || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) 4662 1.1 christos == HAS_RELOC))); 4663 1.1 christos if (need_symtab) 4664 1.1 christos { 4665 1.1 christos /* Non-zero if doing a relocatable link. */ 4666 1.12 christos int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); 4667 1.12 christos 4668 1.1 christos if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info)) 4669 1.1 christos return false; 4670 1.12 christos } 4671 1.1 christos 4672 1.1 christos failed = false; 4673 1.1 christos if (link_info == NULL) 4674 1.1 christos { 4675 1.13 christos bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); 4676 1.1 christos if (failed) 4677 1.1 christos goto err_free_strtab; 4678 1.1 christos } 4679 1.10 christos 4680 1.1 christos shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; 4681 1.5 christos /* sh_name was set in init_file_header. */ 4682 1.1 christos shstrtab_hdr->sh_type = SHT_STRTAB; 4683 1.3 christos shstrtab_hdr->sh_flags = bed->elf_strtab_flags; 4684 1.1 christos shstrtab_hdr->sh_addr = 0; 4685 1.1 christos /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ 4686 1.1 christos shstrtab_hdr->sh_entsize = 0; 4687 1.3 christos shstrtab_hdr->sh_link = 0; 4688 1.1 christos shstrtab_hdr->sh_info = 0; 4689 1.1 christos /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ 4690 1.1 christos shstrtab_hdr->sh_addralign = 1; 4691 1.13 christos 4692 1.1 christos if (!assign_file_positions_except_relocs (abfd, link_info)) 4693 1.13 christos goto err_free_strtab; 4694 1.1 christos 4695 1.1 christos if (strtab != NULL) 4696 1.1 christos { 4697 1.1 christos file_ptr off; 4698 1.3 christos Elf_Internal_Shdr *hdr; 4699 1.1 christos 4700 1.3 christos off = elf_next_file_pos (abfd); 4701 1.14 christos 4702 1.1 christos hdr = & elf_symtab_hdr (abfd); 4703 1.3 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0); 4704 1.3 christos 4705 1.3 christos if (elf_symtab_shndx_list (abfd) != NULL) 4706 1.3 christos { 4707 1.14 christos hdr = & elf_symtab_shndx_list (abfd)->hdr; 4708 1.3 christos if (hdr->sh_size != 0) 4709 1.3 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0); 4710 1.1 christos /* FIXME: What about other symtab_shndx sections in the list ? */ 4711 1.1 christos } 4712 1.14 christos 4713 1.1 christos hdr = &elf_tdata (abfd)->strtab_hdr; 4714 1.3 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0); 4715 1.1 christos 4716 1.1 christos elf_next_file_pos (abfd) = off; 4717 1.1 christos 4718 1.1 christos /* Now that we know where the .strtab section goes, write it 4719 1.3 christos out. */ 4720 1.13 christos if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 4721 1.3 christos || ! _bfd_elf_strtab_emit (abfd, strtab)) 4722 1.1 christos goto err_free_strtab; 4723 1.1 christos _bfd_elf_strtab_free (strtab); 4724 1.12 christos } 4725 1.13 christos 4726 1.12 christos abfd->output_has_begun = true; 4727 1.13 christos return true; 4728 1.13 christos 4729 1.13 christos err_free_strtab: 4730 1.13 christos if (strtab != NULL) 4731 1.12 christos _bfd_elf_strtab_free (strtab); 4732 1.12 christos return false; 4733 1.12 christos } 4734 1.12 christos 4735 1.12 christos /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the 4736 1.12 christos function effectively returns whether --eh-frame-hdr is given on the 4737 1.12 christos command line. After size_dynamic_sections the result reflects 4738 1.1 christos whether .eh_frame_hdr will actually be output (sizing isn't done 4739 1.12 christos until ldemul_after_allocation). */ 4740 1.12 christos 4741 1.12 christos static asection * 4742 1.12 christos elf_eh_frame_hdr (const struct bfd_link_info *info) 4743 1.12 christos { 4744 1.12 christos if (info != NULL && is_elf_hash_table (info->hash)) 4745 1.1 christos return elf_hash_table (info)->eh_info.hdr_sec; 4746 1.1 christos return NULL; 4747 1.1 christos } 4748 1.1 christos 4749 1.1 christos /* Make an initial estimate of the size of the program header. If we 4750 1.1 christos get the number wrong here, we'll redo section placement. */ 4751 1.1 christos 4752 1.1 christos static bfd_size_type 4753 1.1 christos get_program_header_size (bfd *abfd, struct bfd_link_info *info) 4754 1.8 joerg { 4755 1.1 christos size_t segs; 4756 1.1 christos asection *s, *s2; 4757 1.1 christos const struct elf_backend_data *bed; 4758 1.1 christos 4759 1.1 christos /* Assume we will need exactly two PT_LOAD segments: one for text 4760 1.1 christos and one for data. */ 4761 1.1 christos segs = 2; 4762 1.8 joerg 4763 1.10 christos s = bfd_get_section_by_name (abfd, ".interp"); 4764 1.1 christos s2 = bfd_get_section_by_name (abfd, ".dynamic"); 4765 1.8 joerg if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) 4766 1.1 christos { 4767 1.1 christos ++segs; 4768 1.8 joerg } 4769 1.1 christos 4770 1.1 christos if (s2 != NULL && (s2->flags & SEC_LOAD) != 0) 4771 1.1 christos { 4772 1.1 christos /* We need a PT_DYNAMIC segment. */ 4773 1.1 christos ++segs; 4774 1.8 joerg } 4775 1.8 joerg 4776 1.8 joerg if ((s != NULL && (s->flags & SEC_LOAD) != 0) || 4777 1.8 joerg (s2 != NULL && (s2->flags & SEC_LOAD) != 0)) 4778 1.8 joerg { 4779 1.8 joerg /* 4780 1.8 joerg * If either a PT_INTERP or PT_DYNAMIC segment is created, 4781 1.8 joerg * also create a PT_PHDR segment. 4782 1.8 joerg */ 4783 1.8 joerg ++segs; 4784 1.1 christos } 4785 1.1 christos 4786 1.1 christos if (info != NULL && info->relro) 4787 1.1 christos { 4788 1.1 christos /* We need a PT_GNU_RELRO segment. */ 4789 1.1 christos ++segs; 4790 1.12 christos } 4791 1.1 christos 4792 1.1 christos if (elf_eh_frame_hdr (info)) 4793 1.1 christos { 4794 1.1 christos /* We need a PT_GNU_EH_FRAME segment. */ 4795 1.1 christos ++segs; 4796 1.3 christos } 4797 1.1 christos 4798 1.1 christos if (elf_stack_flags (abfd)) 4799 1.1 christos { 4800 1.1 christos /* We need a PT_GNU_STACK segment. */ 4801 1.1 christos ++segs; 4802 1.13 christos } 4803 1.13 christos 4804 1.13 christos if (elf_sframe (abfd)) 4805 1.13 christos { 4806 1.13 christos /* We need a PT_GNU_SFRAME segment. */ 4807 1.13 christos ++segs; 4808 1.10 christos } 4809 1.10 christos 4810 1.10 christos s = bfd_get_section_by_name (abfd, 4811 1.10 christos NOTE_GNU_PROPERTY_SECTION_NAME); 4812 1.10 christos if (s != NULL && s->size != 0) 4813 1.10 christos { 4814 1.10 christos /* We need a PT_GNU_PROPERTY segment. */ 4815 1.10 christos ++segs; 4816 1.1 christos } 4817 1.1 christos 4818 1.1 christos for (s = abfd->sections; s != NULL; s = s->next) 4819 1.10 christos { 4820 1.1 christos if ((s->flags & SEC_LOAD) != 0 4821 1.10 christos && elf_section_type (s) == SHT_NOTE) 4822 1.1 christos { 4823 1.1 christos unsigned int alignment_power; 4824 1.10 christos /* We need a PT_NOTE segment. */ 4825 1.10 christos ++segs; 4826 1.10 christos /* Try to create just one PT_NOTE segment for all adjacent 4827 1.10 christos loadable SHT_NOTE sections. gABI requires that within a 4828 1.10 christos PT_NOTE segment (and also inside of each SHT_NOTE section) 4829 1.10 christos each note should have the same alignment. So we check 4830 1.10 christos whether the sections are correctly aligned. */ 4831 1.10 christos alignment_power = s->alignment_power; 4832 1.10 christos while (s->next != NULL 4833 1.10 christos && s->next->alignment_power == alignment_power 4834 1.10 christos && (s->next->flags & SEC_LOAD) != 0 4835 1.1 christos && elf_section_type (s->next) == SHT_NOTE) 4836 1.1 christos s = s->next; 4837 1.1 christos } 4838 1.1 christos } 4839 1.1 christos 4840 1.1 christos for (s = abfd->sections; s != NULL; s = s->next) 4841 1.1 christos { 4842 1.1 christos if (s->flags & SEC_THREAD_LOCAL) 4843 1.1 christos { 4844 1.1 christos /* We need a PT_TLS segment. */ 4845 1.1 christos ++segs; 4846 1.1 christos break; 4847 1.1 christos } 4848 1.1 christos } 4849 1.9 christos 4850 1.10 christos bed = get_elf_backend_data (abfd); 4851 1.10 christos 4852 1.10 christos if ((abfd->flags & D_PAGED) != 0 4853 1.10 christos && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) 4854 1.12 christos { 4855 1.12 christos /* Add a PT_GNU_MBIND segment for each mbind section. */ 4856 1.12 christos bfd_vma commonpagesize; 4857 1.12 christos unsigned int page_align_power; 4858 1.12 christos 4859 1.12 christos if (info != NULL) 4860 1.12 christos commonpagesize = info->commonpagesize; 4861 1.12 christos else 4862 1.10 christos commonpagesize = bed->commonpagesize; 4863 1.10 christos page_align_power = bfd_log2 (commonpagesize); 4864 1.10 christos for (s = abfd->sections; s != NULL; s = s->next) 4865 1.10 christos if (elf_section_flags (s) & SHF_GNU_MBIND) 4866 1.10 christos { 4867 1.10 christos if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM) 4868 1.10 christos { 4869 1.10 christos _bfd_error_handler 4870 1.10 christos /* xgettext:c-format */ 4871 1.10 christos (_("%pB: GNU_MBIND section `%pA' has invalid " 4872 1.10 christos "sh_info field: %d"), 4873 1.10 christos abfd, s, elf_section_data (s)->this_hdr.sh_info); 4874 1.10 christos continue; 4875 1.10 christos } 4876 1.10 christos /* Align mbind section to page size. */ 4877 1.10 christos if (s->alignment_power < page_align_power) 4878 1.10 christos s->alignment_power = page_align_power; 4879 1.10 christos segs ++; 4880 1.9 christos } 4881 1.10 christos } 4882 1.10 christos 4883 1.1 christos /* Let the backend count up any program headers it might need. */ 4884 1.1 christos if (bed->elf_backend_additional_program_headers) 4885 1.1 christos { 4886 1.1 christos int a; 4887 1.1 christos 4888 1.1 christos a = (*bed->elf_backend_additional_program_headers) (abfd, info); 4889 1.1 christos if (a == -1) 4890 1.1 christos abort (); 4891 1.1 christos segs += a; 4892 1.1 christos } 4893 1.1 christos 4894 1.1 christos return segs * bed->s->sizeof_phdr; 4895 1.1 christos } 4896 1.1 christos 4897 1.1 christos /* Find the segment that contains the output_section of section. */ 4898 1.1 christos 4899 1.1 christos Elf_Internal_Phdr * 4900 1.1 christos _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) 4901 1.1 christos { 4902 1.1 christos struct elf_segment_map *m; 4903 1.3 christos Elf_Internal_Phdr *p; 4904 1.1 christos 4905 1.1 christos for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; 4906 1.1 christos m != NULL; 4907 1.1 christos m = m->next, p++) 4908 1.1 christos { 4909 1.1 christos int i; 4910 1.1 christos 4911 1.1 christos for (i = m->count - 1; i >= 0; i--) 4912 1.1 christos if (m->sections[i] == section) 4913 1.1 christos return p; 4914 1.1 christos } 4915 1.1 christos 4916 1.1 christos return NULL; 4917 1.1 christos } 4918 1.1 christos 4919 1.1 christos /* Create a mapping from a set of sections to a program segment. */ 4920 1.1 christos 4921 1.1 christos static struct elf_segment_map * 4922 1.1 christos make_mapping (bfd *abfd, 4923 1.1 christos asection **sections, 4924 1.12 christos unsigned int from, 4925 1.1 christos unsigned int to, 4926 1.1 christos bool phdr) 4927 1.1 christos { 4928 1.1 christos struct elf_segment_map *m; 4929 1.12 christos unsigned int i; 4930 1.1 christos asection **hdrpp; 4931 1.10 christos size_t amt; 4932 1.10 christos 4933 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *); 4934 1.1 christos amt += (to - from) * sizeof (asection *); 4935 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 4936 1.1 christos if (m == NULL) 4937 1.1 christos return NULL; 4938 1.1 christos m->next = NULL; 4939 1.1 christos m->p_type = PT_LOAD; 4940 1.1 christos for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) 4941 1.1 christos m->sections[i - from] = *hdrpp; 4942 1.1 christos m->count = to - from; 4943 1.1 christos 4944 1.1 christos if (from == 0 && phdr) 4945 1.1 christos { 4946 1.1 christos /* Include the headers in the first PT_LOAD segment. */ 4947 1.1 christos m->includes_filehdr = 1; 4948 1.1 christos m->includes_phdrs = 1; 4949 1.1 christos } 4950 1.1 christos 4951 1.1 christos return m; 4952 1.1 christos } 4953 1.1 christos 4954 1.1 christos /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL 4955 1.1 christos on failure. */ 4956 1.1 christos 4957 1.1 christos struct elf_segment_map * 4958 1.1 christos _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) 4959 1.1 christos { 4960 1.1 christos struct elf_segment_map *m; 4961 1.9 christos 4962 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, 4963 1.1 christos sizeof (struct elf_segment_map)); 4964 1.1 christos if (m == NULL) 4965 1.1 christos return NULL; 4966 1.1 christos m->next = NULL; 4967 1.1 christos m->p_type = PT_DYNAMIC; 4968 1.1 christos m->count = 1; 4969 1.1 christos m->sections[0] = dynsec; 4970 1.1 christos 4971 1.1 christos return m; 4972 1.1 christos } 4973 1.1 christos 4974 1.12 christos /* Possibly add or remove segments from the segment map. */ 4975 1.1 christos 4976 1.1 christos static bool 4977 1.12 christos elf_modify_segment_map (bfd *abfd, 4978 1.1 christos struct bfd_link_info *info, 4979 1.1 christos bool remove_empty_load) 4980 1.1 christos { 4981 1.1 christos struct elf_segment_map **m; 4982 1.1 christos const struct elf_backend_data *bed; 4983 1.1 christos 4984 1.1 christos /* The placement algorithm assumes that non allocated sections are 4985 1.1 christos not in PT_LOAD segments. We ensure this here by removing such 4986 1.1 christos sections from the segment map. We also remove excluded 4987 1.3 christos sections. Finally, any PT_LOAD segment without sections is 4988 1.1 christos removed. */ 4989 1.1 christos m = &elf_seg_map (abfd); 4990 1.1 christos while (*m) 4991 1.1 christos { 4992 1.1 christos unsigned int i, new_count; 4993 1.1 christos 4994 1.1 christos for (new_count = 0, i = 0; i < (*m)->count; i++) 4995 1.1 christos { 4996 1.1 christos if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 4997 1.1 christos && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 4998 1.1 christos || (*m)->p_type != PT_LOAD)) 4999 1.1 christos { 5000 1.1 christos (*m)->sections[new_count] = (*m)->sections[i]; 5001 1.1 christos new_count++; 5002 1.1 christos } 5003 1.1 christos } 5004 1.9 christos (*m)->count = new_count; 5005 1.9 christos 5006 1.9 christos if (remove_empty_load 5007 1.9 christos && (*m)->p_type == PT_LOAD 5008 1.1 christos && (*m)->count == 0 5009 1.1 christos && !(*m)->includes_phdrs) 5010 1.1 christos *m = (*m)->next; 5011 1.1 christos else 5012 1.1 christos m = &(*m)->next; 5013 1.1 christos } 5014 1.1 christos 5015 1.1 christos bed = get_elf_backend_data (abfd); 5016 1.1 christos if (bed->elf_backend_modify_segment_map != NULL) 5017 1.12 christos { 5018 1.1 christos if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) 5019 1.1 christos return false; 5020 1.12 christos } 5021 1.1 christos 5022 1.1 christos return true; 5023 1.9 christos } 5024 1.9 christos 5025 1.9 christos #define IS_TBSS(s) \ 5026 1.12 christos ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL) 5027 1.12 christos 5028 1.1 christos /* Set up a mapping from BFD sections to program segments. Update 5029 1.12 christos NEED_LAYOUT if the section layout is changed. */ 5030 1.12 christos 5031 1.12 christos bool 5032 1.12 christos _bfd_elf_map_sections_to_segments (bfd *abfd, 5033 1.1 christos struct bfd_link_info *info, 5034 1.1 christos bool *need_layout) 5035 1.1 christos { 5036 1.1 christos unsigned int count; 5037 1.1 christos struct elf_segment_map *m; 5038 1.12 christos asection **sections = NULL; 5039 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 5040 1.3 christos bool no_user_phdrs; 5041 1.1 christos 5042 1.1 christos no_user_phdrs = elf_seg_map (abfd) == NULL; 5043 1.12 christos 5044 1.12 christos if (info != NULL) 5045 1.12 christos { 5046 1.12 christos info->user_phdrs = !no_user_phdrs; 5047 1.12 christos 5048 1.12 christos /* Size the relative relocations if DT_RELR is enabled. */ 5049 1.12 christos if (info->enable_dt_relr 5050 1.12 christos && need_layout != NULL 5051 1.14 christos && bed->size_relative_relocs 5052 1.14 christos && !bed->size_relative_relocs (info, need_layout)) 5053 1.12 christos info->callbacks->fatal 5054 1.1 christos (_("%P: failed to size relative relocations\n")); 5055 1.1 christos } 5056 1.1 christos 5057 1.1 christos if (no_user_phdrs && bfd_count_sections (abfd) != 0) 5058 1.1 christos { 5059 1.1 christos asection *s; 5060 1.1 christos unsigned int i; 5061 1.1 christos struct elf_segment_map *mfirst; 5062 1.1 christos struct elf_segment_map **pm; 5063 1.10 christos asection *last_hdr; 5064 1.1 christos bfd_vma last_size; 5065 1.1 christos unsigned int hdr_index; 5066 1.12 christos bfd_vma maxpagesize; 5067 1.12 christos asection **hdrpp; 5068 1.12 christos bool phdr_in_segment; 5069 1.12 christos bool writable; 5070 1.1 christos bool executable; 5071 1.9 christos unsigned int tls_count = 0; 5072 1.1 christos asection *first_tls = NULL; 5073 1.13 christos asection *first_mbind = NULL; 5074 1.12 christos asection *dynsec, *eh_frame_hdr; 5075 1.12 christos asection *sframe; 5076 1.12 christos size_t amt; 5077 1.12 christos bfd_vma addr_mask, wrap_to = 0; /* Bytes. */ 5078 1.1 christos bfd_size_type phdr_size; /* Octets/bytes. */ 5079 1.1 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL); 5080 1.1 christos 5081 1.12 christos /* Select the allocated sections, and sort them. */ 5082 1.12 christos 5083 1.1 christos amt = bfd_count_sections (abfd) * sizeof (asection *); 5084 1.1 christos sections = (asection **) bfd_malloc (amt); 5085 1.1 christos if (sections == NULL) 5086 1.1 christos goto error_return; 5087 1.1 christos 5088 1.1 christos /* Calculate top address, avoiding undefined behaviour of shift 5089 1.1 christos left operator when shift count is equal to size of type 5090 1.1 christos being shifted. */ 5091 1.1 christos addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; 5092 1.1 christos addr_mask = (addr_mask << 1) + 1; 5093 1.1 christos 5094 1.1 christos i = 0; 5095 1.1 christos for (s = abfd->sections; s != NULL; s = s->next) 5096 1.1 christos { 5097 1.10 christos if ((s->flags & SEC_ALLOC) != 0) 5098 1.10 christos { 5099 1.10 christos /* target_index is unused until bfd_elf_final_link 5100 1.10 christos starts output of section symbols. Use it to make 5101 1.1 christos qsort stable. */ 5102 1.1 christos s->target_index = i; 5103 1.1 christos sections[i] = s; 5104 1.12 christos ++i; 5105 1.12 christos /* A wrapping section potentially clashes with header. */ 5106 1.1 christos if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask)) 5107 1.1 christos wrap_to = (s->lma + s->size / opb) & addr_mask; 5108 1.1 christos } 5109 1.1 christos } 5110 1.1 christos BFD_ASSERT (i <= bfd_count_sections (abfd)); 5111 1.1 christos count = i; 5112 1.1 christos 5113 1.10 christos qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); 5114 1.10 christos 5115 1.10 christos phdr_size = elf_program_header_size (abfd); 5116 1.10 christos if (phdr_size == (bfd_size_type) -1) 5117 1.12 christos phdr_size = get_program_header_size (abfd, info); 5118 1.12 christos phdr_size += bed->s->sizeof_ehdr; 5119 1.12 christos /* phdr_size is compared to LMA values which are in bytes. */ 5120 1.12 christos phdr_size /= opb; 5121 1.12 christos if (info != NULL) 5122 1.12 christos maxpagesize = info->maxpagesize; 5123 1.10 christos else 5124 1.10 christos maxpagesize = bed->maxpagesize; 5125 1.10 christos if (maxpagesize == 0) 5126 1.10 christos maxpagesize = 1; 5127 1.10 christos phdr_in_segment = info != NULL && info->load_phdrs; 5128 1.10 christos if (count != 0 5129 1.10 christos && (((sections[0]->lma & addr_mask) & (maxpagesize - 1)) 5130 1.10 christos >= (phdr_size & (maxpagesize - 1)))) 5131 1.10 christos /* For compatibility with old scripts that may not be using 5132 1.12 christos SIZEOF_HEADERS, add headers when it looks like space has 5133 1.10 christos been left for them. */ 5134 1.1 christos phdr_in_segment = true; 5135 1.1 christos 5136 1.1 christos /* Build the mapping. */ 5137 1.1 christos mfirst = NULL; 5138 1.1 christos pm = &mfirst; 5139 1.1 christos 5140 1.1 christos /* If we have a .interp section, then create a PT_PHDR segment for 5141 1.1 christos the program headers and a PT_INTERP segment for the .interp 5142 1.8 joerg section. */ 5143 1.10 christos s = bfd_get_section_by_name (abfd, ".interp"); 5144 1.8 joerg if (s != NULL && (s->flags & SEC_LOAD) == 0) 5145 1.8 joerg if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) 5146 1.8 joerg s = NULL; 5147 1.8 joerg dynsec = bfd_get_section_by_name (abfd, ".dynamic"); 5148 1.8 joerg if (dynsec != NULL && (dynsec->flags & SEC_LOAD) == 0) 5149 1.8 joerg dynsec = NULL; 5150 1.1 christos 5151 1.1 christos if (s != NULL || dynsec != NULL) 5152 1.1 christos { 5153 1.1 christos amt = sizeof (struct elf_segment_map); 5154 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5155 1.1 christos if (m == NULL) 5156 1.1 christos goto error_return; 5157 1.9 christos m->next = NULL; 5158 1.1 christos m->p_type = PT_PHDR; 5159 1.1 christos m->p_flags = PF_R; 5160 1.12 christos m->p_flags_valid = 1; 5161 1.1 christos m->includes_phdrs = 1; 5162 1.1 christos phdr_in_segment = true; 5163 1.8 joerg *pm = m; 5164 1.1 christos pm = &m->next; 5165 1.8 joerg } 5166 1.8 joerg 5167 1.1 christos if (s != NULL) 5168 1.1 christos { 5169 1.1 christos amt = sizeof (struct elf_segment_map); 5170 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5171 1.1 christos if (m == NULL) 5172 1.1 christos goto error_return; 5173 1.1 christos m->next = NULL; 5174 1.1 christos m->p_type = PT_INTERP; 5175 1.1 christos m->count = 1; 5176 1.1 christos m->sections[0] = s; 5177 1.1 christos 5178 1.1 christos *pm = m; 5179 1.1 christos pm = &m->next; 5180 1.1 christos } 5181 1.1 christos 5182 1.1 christos /* Look through the sections. We put sections in the same program 5183 1.1 christos segment when the start of the second section can be placed within 5184 1.1 christos a few bytes of the end of the first section. */ 5185 1.10 christos last_hdr = NULL; 5186 1.12 christos last_size = 0; 5187 1.12 christos hdr_index = 0; 5188 1.1 christos writable = false; 5189 1.10 christos executable = false; 5190 1.12 christos 5191 1.10 christos if ((abfd->flags & D_PAGED) == 0) 5192 1.1 christos phdr_in_segment = false; 5193 1.1 christos 5194 1.1 christos /* Deal with -Ttext or something similar such that the first section 5195 1.1 christos is not adjacent to the program headers. This is an 5196 1.10 christos approximation, since at this point we don't know exactly how many 5197 1.1 christos program headers we will need. */ 5198 1.12 christos if (phdr_in_segment && count > 0) 5199 1.12 christos { 5200 1.1 christos bfd_vma phdr_lma; /* Bytes. */ 5201 1.10 christos bool separate_phdr = false; 5202 1.10 christos 5203 1.10 christos phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize; 5204 1.10 christos if (info != NULL 5205 1.10 christos && info->separate_code 5206 1.10 christos && (sections[0]->flags & SEC_CODE) != 0) 5207 1.10 christos { 5208 1.10 christos /* If data sections should be separate from code and 5209 1.10 christos thus not executable, and the first section is 5210 1.14 christos executable then put the file and program headers in 5211 1.14 christos their own PT_LOAD. */ 5212 1.14 christos if (!info->one_rosegment) 5213 1.10 christos separate_phdr = true; 5214 1.10 christos 5215 1.10 christos if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize) 5216 1.10 christos == (sections[0]->lma & addr_mask & -maxpagesize))) 5217 1.10 christos { 5218 1.10 christos /* The file and program headers are currently on the 5219 1.10 christos same page as the first section. Put them on the 5220 1.10 christos previous page if we can. */ 5221 1.10 christos if (phdr_lma >= maxpagesize) 5222 1.12 christos phdr_lma -= maxpagesize; 5223 1.10 christos else 5224 1.10 christos separate_phdr = false; 5225 1.10 christos } 5226 1.10 christos } 5227 1.10 christos if ((sections[0]->lma & addr_mask) < phdr_lma 5228 1.10 christos || (sections[0]->lma & addr_mask) < phdr_size) 5229 1.12 christos /* If file and program headers would be placed at the end 5230 1.10 christos of memory then it's probably better to omit them. */ 5231 1.10 christos phdr_in_segment = false; 5232 1.10 christos else if (phdr_lma < wrap_to) 5233 1.10 christos /* If a section wraps around to where we'll be placing 5234 1.12 christos file and program headers, then the headers will be 5235 1.10 christos overwritten. */ 5236 1.10 christos phdr_in_segment = false; 5237 1.10 christos else if (separate_phdr) 5238 1.10 christos { 5239 1.10 christos m = make_mapping (abfd, sections, 0, 0, phdr_in_segment); 5240 1.12 christos if (m == NULL) 5241 1.10 christos goto error_return; 5242 1.10 christos m->p_paddr = phdr_lma * opb; 5243 1.10 christos m->p_vaddr_offset 5244 1.10 christos = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize; 5245 1.10 christos m->p_paddr_valid = 1; 5246 1.12 christos *pm = m; 5247 1.9 christos pm = &m->next; 5248 1.1 christos phdr_in_segment = false; 5249 1.1 christos } 5250 1.1 christos } 5251 1.1 christos 5252 1.1 christos for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) 5253 1.12 christos { 5254 1.1 christos asection *hdr; 5255 1.1 christos bool new_segment; 5256 1.1 christos 5257 1.1 christos hdr = *hdrpp; 5258 1.1 christos 5259 1.1 christos /* See if this section and the last one will fit in the same 5260 1.1 christos segment. */ 5261 1.1 christos 5262 1.1 christos if (last_hdr == NULL) 5263 1.1 christos { 5264 1.12 christos /* If we don't have a segment yet, then we don't need a new 5265 1.1 christos one (we build the last one after this loop). */ 5266 1.1 christos new_segment = false; 5267 1.1 christos } 5268 1.1 christos else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) 5269 1.1 christos { 5270 1.1 christos /* If this section has a different relation between the 5271 1.12 christos virtual address and the load address, then we need a new 5272 1.1 christos segment. */ 5273 1.1 christos new_segment = true; 5274 1.1 christos } 5275 1.1 christos else if (hdr->lma < last_hdr->lma + last_size 5276 1.1 christos || last_hdr->lma + last_size < last_hdr->lma) 5277 1.1 christos { 5278 1.12 christos /* If this section has a load address that makes it overlap 5279 1.1 christos the previous section, then we need a new segment. */ 5280 1.9 christos new_segment = true; 5281 1.9 christos } 5282 1.9 christos else if ((abfd->flags & D_PAGED) != 0 5283 1.9 christos && (((last_hdr->lma + last_size - 1) & -maxpagesize) 5284 1.9 christos == (hdr->lma & -maxpagesize))) 5285 1.9 christos { 5286 1.12 christos /* If we are demand paged then we can't map two disk 5287 1.9 christos pages onto the same memory page. */ 5288 1.1 christos new_segment = false; 5289 1.1 christos } 5290 1.1 christos /* In the next test we have to be careful when last_hdr->lma is close 5291 1.1 christos to the end of the address space. If the aligned address wraps 5292 1.1 christos around to the start of the address space, then there are no more 5293 1.9 christos pages left in memory and it is OK to assume that the current 5294 1.9 christos section can be included in the current segment. */ 5295 1.9 christos else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) 5296 1.9 christos + maxpagesize > last_hdr->lma) 5297 1.1 christos && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) 5298 1.1 christos + maxpagesize <= hdr->lma)) 5299 1.1 christos { 5300 1.12 christos /* If putting this section in this segment would force us to 5301 1.1 christos skip a page in the segment, then we need a new segment. */ 5302 1.1 christos new_segment = true; 5303 1.9 christos } 5304 1.3 christos else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 5305 1.3 christos && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) 5306 1.3 christos { 5307 1.3 christos /* We don't want to put a loaded section after a 5308 1.9 christos nonloaded (ie. bss style) section in the same segment 5309 1.12 christos as that will force the non-loaded section to be loaded. 5310 1.1 christos Consider .tbss sections as loaded for this purpose. */ 5311 1.1 christos new_segment = true; 5312 1.1 christos } 5313 1.1 christos else if ((abfd->flags & D_PAGED) == 0) 5314 1.1 christos { 5315 1.1 christos /* If the file is not demand paged, which means that we 5316 1.12 christos don't require the sections to be correctly aligned in the 5317 1.1 christos file, then there is no other reason for a new segment. */ 5318 1.9 christos new_segment = false; 5319 1.9 christos } 5320 1.9 christos else if (info != NULL 5321 1.9 christos && info->separate_code 5322 1.12 christos && executable != ((hdr->flags & SEC_CODE) != 0)) 5323 1.9 christos { 5324 1.1 christos new_segment = true; 5325 1.9 christos } 5326 1.1 christos else if (! writable 5327 1.1 christos && (hdr->flags & SEC_READONLY) == 0) 5328 1.9 christos { 5329 1.12 christos /* We don't want to put a writable section in a read only 5330 1.1 christos segment. */ 5331 1.1 christos new_segment = true; 5332 1.1 christos } 5333 1.1 christos else 5334 1.12 christos { 5335 1.1 christos /* Otherwise, we can use the same segment. */ 5336 1.1 christos new_segment = false; 5337 1.1 christos } 5338 1.1 christos 5339 1.1 christos /* Allow interested parties a chance to override our decision. */ 5340 1.1 christos if (last_hdr != NULL 5341 1.1 christos && info != NULL 5342 1.1 christos && info->callbacks->override_segment_assignment != NULL) 5343 1.1 christos new_segment 5344 1.1 christos = info->callbacks->override_segment_assignment (info, abfd, hdr, 5345 1.1 christos last_hdr, 5346 1.1 christos new_segment); 5347 1.1 christos 5348 1.1 christos if (! new_segment) 5349 1.12 christos { 5350 1.9 christos if ((hdr->flags & SEC_READONLY) == 0) 5351 1.12 christos writable = true; 5352 1.1 christos if ((hdr->flags & SEC_CODE) != 0) 5353 1.1 christos executable = true; 5354 1.12 christos last_hdr = hdr; 5355 1.1 christos /* .tbss sections effectively have zero size. */ 5356 1.1 christos last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb; 5357 1.1 christos continue; 5358 1.1 christos } 5359 1.10 christos 5360 1.1 christos /* We need a new program segment. We must create a new program 5361 1.10 christos header holding all the sections from hdr_index until hdr. */ 5362 1.1 christos 5363 1.1 christos m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); 5364 1.1 christos if (m == NULL) 5365 1.1 christos goto error_return; 5366 1.1 christos 5367 1.1 christos *pm = m; 5368 1.1 christos pm = &m->next; 5369 1.12 christos 5370 1.1 christos if ((hdr->flags & SEC_READONLY) == 0) 5371 1.12 christos writable = true; 5372 1.1 christos else 5373 1.9 christos writable = false; 5374 1.12 christos 5375 1.9 christos if ((hdr->flags & SEC_CODE) == 0) 5376 1.12 christos executable = false; 5377 1.9 christos else 5378 1.1 christos executable = true; 5379 1.1 christos 5380 1.12 christos last_hdr = hdr; 5381 1.10 christos /* .tbss sections effectively have zero size. */ 5382 1.12 christos last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb; 5383 1.1 christos hdr_index = i; 5384 1.1 christos phdr_in_segment = false; 5385 1.1 christos } 5386 1.1 christos 5387 1.1 christos /* Create a final PT_LOAD program segment, but not if it's just 5388 1.10 christos for .tbss. */ 5389 1.9 christos if (last_hdr != NULL 5390 1.1 christos && (i - hdr_index != 1 5391 1.10 christos || !IS_TBSS (last_hdr))) 5392 1.1 christos { 5393 1.1 christos m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); 5394 1.1 christos if (m == NULL) 5395 1.1 christos goto error_return; 5396 1.1 christos 5397 1.1 christos *pm = m; 5398 1.1 christos pm = &m->next; 5399 1.1 christos } 5400 1.1 christos 5401 1.1 christos /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ 5402 1.1 christos if (dynsec != NULL) 5403 1.1 christos { 5404 1.1 christos m = _bfd_elf_make_dynamic_segment (abfd, dynsec); 5405 1.1 christos if (m == NULL) 5406 1.1 christos goto error_return; 5407 1.1 christos *pm = m; 5408 1.1 christos pm = &m->next; 5409 1.10 christos } 5410 1.1 christos 5411 1.1 christos /* For each batch of consecutive loadable SHT_NOTE sections, 5412 1.1 christos add a PT_NOTE segment. We don't use bfd_get_section_by_name, 5413 1.10 christos because if we link together nonloadable .note sections and 5414 1.1 christos loadable .note sections, we will generate two .note sections 5415 1.1 christos in the output file. */ 5416 1.1 christos for (s = abfd->sections; s != NULL; s = s->next) 5417 1.10 christos { 5418 1.1 christos if ((s->flags & SEC_LOAD) != 0 5419 1.1 christos && elf_section_type (s) == SHT_NOTE) 5420 1.10 christos { 5421 1.1 christos asection *s2; 5422 1.1 christos unsigned int alignment_power = s->alignment_power; 5423 1.10 christos 5424 1.10 christos count = 1; 5425 1.10 christos for (s2 = s; s2->next != NULL; s2 = s2->next) 5426 1.10 christos { 5427 1.10 christos if (s2->next->alignment_power == alignment_power 5428 1.12 christos && (s2->next->flags & SEC_LOAD) != 0 5429 1.10 christos && elf_section_type (s2->next) == SHT_NOTE 5430 1.10 christos && align_power (s2->lma + s2->size / opb, 5431 1.10 christos alignment_power) 5432 1.10 christos == s2->next->lma) 5433 1.10 christos count++; 5434 1.10 christos else 5435 1.10 christos break; 5436 1.10 christos } 5437 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *); 5438 1.1 christos amt += count * sizeof (asection *); 5439 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5440 1.1 christos if (m == NULL) 5441 1.1 christos goto error_return; 5442 1.1 christos m->next = NULL; 5443 1.1 christos m->p_type = PT_NOTE; 5444 1.1 christos m->count = count; 5445 1.1 christos while (count > 1) 5446 1.1 christos { 5447 1.1 christos m->sections[m->count - count--] = s; 5448 1.1 christos BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); 5449 1.1 christos s = s->next; 5450 1.1 christos } 5451 1.1 christos m->sections[m->count - 1] = s; 5452 1.1 christos BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); 5453 1.1 christos *pm = m; 5454 1.1 christos pm = &m->next; 5455 1.1 christos } 5456 1.1 christos if (s->flags & SEC_THREAD_LOCAL) 5457 1.1 christos { 5458 1.1 christos if (! tls_count) 5459 1.1 christos first_tls = s; 5460 1.9 christos tls_count++; 5461 1.9 christos } 5462 1.9 christos if (first_mbind == NULL 5463 1.1 christos && (elf_section_flags (s) & SHF_GNU_MBIND) != 0) 5464 1.1 christos first_mbind = s; 5465 1.1 christos } 5466 1.1 christos 5467 1.1 christos /* If there are any SHF_TLS output sections, add PT_TLS segment. */ 5468 1.10 christos if (tls_count > 0) 5469 1.10 christos { 5470 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *); 5471 1.1 christos amt += tls_count * sizeof (asection *); 5472 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5473 1.1 christos if (m == NULL) 5474 1.1 christos goto error_return; 5475 1.1 christos m->next = NULL; 5476 1.1 christos m->p_type = PT_TLS; 5477 1.1 christos m->count = tls_count; 5478 1.1 christos /* Mandated PF_R. */ 5479 1.3 christos m->p_flags = PF_R; 5480 1.12 christos m->p_flags_valid = 1; 5481 1.1 christos s = first_tls; 5482 1.3 christos for (i = 0; i < tls_count; ++i) 5483 1.3 christos { 5484 1.3 christos if ((s->flags & SEC_THREAD_LOCAL) == 0) 5485 1.9 christos { 5486 1.3 christos _bfd_error_handler 5487 1.3 christos (_("%pB: TLS sections are not adjacent:"), abfd); 5488 1.12 christos s = first_tls; 5489 1.3 christos i = 0; 5490 1.3 christos while (i < tls_count) 5491 1.3 christos { 5492 1.9 christos if ((s->flags & SEC_THREAD_LOCAL) != 0) 5493 1.3 christos { 5494 1.3 christos _bfd_error_handler (_(" TLS: %pA"), s); 5495 1.3 christos i++; 5496 1.9 christos } 5497 1.3 christos else 5498 1.3 christos _bfd_error_handler (_(" non-TLS: %pA"), s); 5499 1.3 christos s = s->next; 5500 1.3 christos } 5501 1.3 christos bfd_set_error (bfd_error_bad_value); 5502 1.3 christos goto error_return; 5503 1.3 christos } 5504 1.1 christos m->sections[i] = s; 5505 1.1 christos s = s->next; 5506 1.1 christos } 5507 1.1 christos 5508 1.1 christos *pm = m; 5509 1.1 christos pm = &m->next; 5510 1.10 christos } 5511 1.10 christos 5512 1.10 christos if (first_mbind 5513 1.9 christos && (abfd->flags & D_PAGED) != 0 5514 1.9 christos && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) 5515 1.10 christos for (s = first_mbind; s != NULL; s = s->next) 5516 1.9 christos if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0 5517 1.9 christos && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM) 5518 1.9 christos { 5519 1.9 christos /* Mandated PF_R. */ 5520 1.9 christos unsigned long p_flags = PF_R; 5521 1.9 christos if ((s->flags & SEC_READONLY) == 0) 5522 1.9 christos p_flags |= PF_W; 5523 1.9 christos if ((s->flags & SEC_CODE) != 0) 5524 1.9 christos p_flags |= PF_X; 5525 1.9 christos 5526 1.9 christos amt = sizeof (struct elf_segment_map) + sizeof (asection *); 5527 1.9 christos m = bfd_zalloc (abfd, amt); 5528 1.9 christos if (m == NULL) 5529 1.9 christos goto error_return; 5530 1.9 christos m->next = NULL; 5531 1.9 christos m->p_type = (PT_GNU_MBIND_LO 5532 1.9 christos + elf_section_data (s)->this_hdr.sh_info); 5533 1.9 christos m->count = 1; 5534 1.9 christos m->p_flags_valid = 1; 5535 1.9 christos m->sections[0] = s; 5536 1.9 christos m->p_flags = p_flags; 5537 1.9 christos 5538 1.9 christos *pm = m; 5539 1.9 christos pm = &m->next; 5540 1.10 christos } 5541 1.10 christos 5542 1.10 christos s = bfd_get_section_by_name (abfd, 5543 1.10 christos NOTE_GNU_PROPERTY_SECTION_NAME); 5544 1.10 christos if (s != NULL && s->size != 0) 5545 1.10 christos { 5546 1.10 christos amt = sizeof (struct elf_segment_map) + sizeof (asection *); 5547 1.10 christos m = bfd_zalloc (abfd, amt); 5548 1.10 christos if (m == NULL) 5549 1.10 christos goto error_return; 5550 1.10 christos m->next = NULL; 5551 1.10 christos m->p_type = PT_GNU_PROPERTY; 5552 1.10 christos m->count = 1; 5553 1.10 christos m->p_flags_valid = 1; 5554 1.10 christos m->sections[0] = s; 5555 1.10 christos m->p_flags = PF_R; 5556 1.10 christos *pm = m; 5557 1.10 christos pm = &m->next; 5558 1.1 christos } 5559 1.1 christos 5560 1.12 christos /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME 5561 1.1 christos segment. */ 5562 1.1 christos eh_frame_hdr = elf_eh_frame_hdr (info); 5563 1.1 christos if (eh_frame_hdr != NULL 5564 1.1 christos && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) 5565 1.1 christos { 5566 1.1 christos amt = sizeof (struct elf_segment_map); 5567 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5568 1.1 christos if (m == NULL) 5569 1.1 christos goto error_return; 5570 1.1 christos m->next = NULL; 5571 1.1 christos m->p_type = PT_GNU_EH_FRAME; 5572 1.1 christos m->count = 1; 5573 1.1 christos m->sections[0] = eh_frame_hdr->output_section; 5574 1.1 christos 5575 1.1 christos *pm = m; 5576 1.1 christos pm = &m->next; 5577 1.13 christos } 5578 1.13 christos 5579 1.13 christos /* If there is a .sframe section, throw in a PT_GNU_SFRAME 5580 1.13 christos segment. */ 5581 1.13 christos sframe = elf_sframe (abfd); 5582 1.13 christos if (sframe != NULL 5583 1.13 christos && (sframe->output_section->flags & SEC_LOAD) != 0 5584 1.13 christos && sframe->size != 0) 5585 1.13 christos { 5586 1.13 christos amt = sizeof (struct elf_segment_map); 5587 1.13 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5588 1.13 christos if (m == NULL) 5589 1.13 christos goto error_return; 5590 1.13 christos m->next = NULL; 5591 1.13 christos m->p_type = PT_GNU_SFRAME; 5592 1.13 christos m->count = 1; 5593 1.13 christos m->sections[0] = sframe->output_section; 5594 1.13 christos 5595 1.13 christos *pm = m; 5596 1.13 christos pm = &m->next; 5597 1.3 christos } 5598 1.1 christos 5599 1.1 christos if (elf_stack_flags (abfd)) 5600 1.1 christos { 5601 1.1 christos amt = sizeof (struct elf_segment_map); 5602 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5603 1.1 christos if (m == NULL) 5604 1.1 christos goto error_return; 5605 1.3 christos m->next = NULL; 5606 1.3 christos m->p_type = PT_GNU_STACK; 5607 1.1 christos m->p_flags = elf_stack_flags (abfd); 5608 1.3 christos m->p_align = bed->stack_align; 5609 1.3 christos m->p_flags_valid = 1; 5610 1.3 christos m->p_align_valid = m->p_align != 0; 5611 1.3 christos if (info->stacksize > 0) 5612 1.3 christos { 5613 1.3 christos m->p_size = info->stacksize; 5614 1.1 christos m->p_size_valid = 1; 5615 1.1 christos } 5616 1.1 christos 5617 1.1 christos *pm = m; 5618 1.1 christos pm = &m->next; 5619 1.1 christos } 5620 1.1 christos 5621 1.1 christos if (info != NULL && info->relro) 5622 1.1 christos { 5623 1.1 christos for (m = mfirst; m != NULL; m = m->next) 5624 1.1 christos { 5625 1.1 christos if (m->p_type == PT_LOAD 5626 1.1 christos && m->count != 0 5627 1.1 christos && m->sections[0]->vma >= info->relro_start 5628 1.1 christos && m->sections[0]->vma < info->relro_end) 5629 1.1 christos { 5630 1.12 christos i = m->count; 5631 1.12 christos while (--i != (unsigned) -1) 5632 1.13 christos { 5633 1.13 christos if (m->sections[i]->size > 0 5634 1.12 christos && (m->sections[i]->flags & SEC_LOAD) != 0 5635 1.12 christos && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0) 5636 1.1 christos break; 5637 1.3 christos } 5638 1.1 christos 5639 1.1 christos if (i != (unsigned) -1) 5640 1.1 christos break; 5641 1.1 christos } 5642 1.1 christos } 5643 1.1 christos 5644 1.1 christos /* Make a PT_GNU_RELRO segment only when it isn't empty. */ 5645 1.1 christos if (m != NULL) 5646 1.1 christos { 5647 1.1 christos amt = sizeof (struct elf_segment_map); 5648 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5649 1.1 christos if (m == NULL) 5650 1.1 christos goto error_return; 5651 1.1 christos m->next = NULL; 5652 1.1 christos m->p_type = PT_GNU_RELRO; 5653 1.1 christos *pm = m; 5654 1.1 christos pm = &m->next; 5655 1.1 christos } 5656 1.1 christos } 5657 1.3 christos 5658 1.1 christos free (sections); 5659 1.1 christos elf_seg_map (abfd) = mfirst; 5660 1.14 christos } 5661 1.12 christos 5662 1.1 christos if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL)) 5663 1.3 christos return false; 5664 1.1 christos 5665 1.3 christos for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) 5666 1.1 christos ++count; 5667 1.12 christos elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; 5668 1.1 christos 5669 1.1 christos return true; 5670 1.12 christos 5671 1.12 christos error_return: 5672 1.1 christos free (sections); 5673 1.1 christos return false; 5674 1.1 christos } 5675 1.1 christos 5676 1.1 christos /* Sort sections by address. */ 5677 1.1 christos 5678 1.1 christos static int 5679 1.1 christos elf_sort_sections (const void *arg1, const void *arg2) 5680 1.1 christos { 5681 1.1 christos const asection *sec1 = *(const asection **) arg1; 5682 1.1 christos const asection *sec2 = *(const asection **) arg2; 5683 1.1 christos bfd_size_type size1, size2; 5684 1.1 christos 5685 1.1 christos /* Sort by LMA first, since this is the address used to 5686 1.1 christos place the section into a segment. */ 5687 1.1 christos if (sec1->lma < sec2->lma) 5688 1.1 christos return -1; 5689 1.1 christos else if (sec1->lma > sec2->lma) 5690 1.1 christos return 1; 5691 1.1 christos 5692 1.1 christos /* Then sort by VMA. Normally the LMA and the VMA will be 5693 1.1 christos the same, and this will do nothing. */ 5694 1.1 christos if (sec1->vma < sec2->vma) 5695 1.1 christos return -1; 5696 1.1 christos else if (sec1->vma > sec2->vma) 5697 1.1 christos return 1; 5698 1.1 christos 5699 1.12 christos /* Put !SEC_LOAD sections after SEC_LOAD ones. */ 5700 1.12 christos 5701 1.1 christos #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \ 5702 1.1 christos && (x)->size != 0) 5703 1.1 christos 5704 1.10 christos if (TOEND (sec1)) 5705 1.1 christos { 5706 1.1 christos if (!TOEND (sec2)) 5707 1.1 christos return 1; 5708 1.1 christos } 5709 1.1 christos else if (TOEND (sec2)) 5710 1.1 christos return -1; 5711 1.1 christos 5712 1.1 christos #undef TOEND 5713 1.1 christos 5714 1.1 christos /* Sort by size, to put zero sized sections 5715 1.1 christos before others at the same address. */ 5716 1.1 christos 5717 1.1 christos size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; 5718 1.1 christos size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; 5719 1.1 christos 5720 1.1 christos if (size1 < size2) 5721 1.1 christos return -1; 5722 1.1 christos if (size1 > size2) 5723 1.1 christos return 1; 5724 1.1 christos 5725 1.1 christos return sec1->target_index - sec2->target_index; 5726 1.10 christos } 5727 1.10 christos 5728 1.10 christos /* This qsort comparison functions sorts PT_LOAD segments first and 5729 1.10 christos by p_paddr, for assign_file_positions_for_load_sections. */ 5730 1.10 christos 5731 1.10 christos static int 5732 1.10 christos elf_sort_segments (const void *arg1, const void *arg2) 5733 1.10 christos { 5734 1.10 christos const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1; 5735 1.10 christos const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2; 5736 1.10 christos 5737 1.10 christos if (m1->p_type != m2->p_type) 5738 1.10 christos { 5739 1.10 christos if (m1->p_type == PT_NULL) 5740 1.10 christos return 1; 5741 1.10 christos if (m2->p_type == PT_NULL) 5742 1.10 christos return -1; 5743 1.10 christos return m1->p_type < m2->p_type ? -1 : 1; 5744 1.10 christos } 5745 1.10 christos if (m1->includes_filehdr != m2->includes_filehdr) 5746 1.10 christos return m1->includes_filehdr ? -1 : 1; 5747 1.10 christos if (m1->no_sort_lma != m2->no_sort_lma) 5748 1.10 christos return m1->no_sort_lma ? -1 : 1; 5749 1.12 christos if (m1->p_type == PT_LOAD && !m1->no_sort_lma) 5750 1.10 christos { 5751 1.10 christos bfd_vma lma1, lma2; /* Octets. */ 5752 1.10 christos lma1 = 0; 5753 1.10 christos if (m1->p_paddr_valid) 5754 1.12 christos lma1 = m1->p_paddr; 5755 1.12 christos else if (m1->count != 0) 5756 1.12 christos { 5757 1.12 christos unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner, 5758 1.12 christos m1->sections[0]); 5759 1.10 christos lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb; 5760 1.10 christos } 5761 1.10 christos lma2 = 0; 5762 1.10 christos if (m2->p_paddr_valid) 5763 1.12 christos lma2 = m2->p_paddr; 5764 1.12 christos else if (m2->count != 0) 5765 1.12 christos { 5766 1.12 christos unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner, 5767 1.12 christos m2->sections[0]); 5768 1.10 christos lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb; 5769 1.10 christos } 5770 1.10 christos if (lma1 != lma2) 5771 1.10 christos return lma1 < lma2 ? -1 : 1; 5772 1.10 christos } 5773 1.10 christos if (m1->idx != m2->idx) 5774 1.10 christos return m1->idx < m2->idx ? -1 : 1; 5775 1.10 christos return 0; 5776 1.1 christos } 5777 1.1 christos 5778 1.1 christos /* Ian Lance Taylor writes: 5779 1.1 christos 5780 1.1 christos We shouldn't be using % with a negative signed number. That's just 5781 1.1 christos not good. We have to make sure either that the number is not 5782 1.1 christos negative, or that the number has an unsigned type. When the types 5783 1.1 christos are all the same size they wind up as unsigned. When file_ptr is a 5784 1.1 christos larger signed type, the arithmetic winds up as signed long long, 5785 1.1 christos which is wrong. 5786 1.1 christos 5787 1.1 christos What we're trying to say here is something like ``increase OFF by 5788 1.1 christos the least amount that will cause it to be equal to the VMA modulo 5789 1.1 christos the page size.'' */ 5790 1.1 christos /* In other words, something like: 5791 1.1 christos 5792 1.1 christos vma_offset = m->sections[0]->vma % bed->maxpagesize; 5793 1.1 christos off_offset = off % bed->maxpagesize; 5794 1.1 christos if (vma_offset < off_offset) 5795 1.1 christos adjustment = vma_offset + bed->maxpagesize - off_offset; 5796 1.1 christos else 5797 1.9 christos adjustment = vma_offset - off_offset; 5798 1.1 christos 5799 1.1 christos which can be collapsed into the expression below. */ 5800 1.1 christos 5801 1.1 christos static file_ptr 5802 1.3 christos vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) 5803 1.3 christos { 5804 1.3 christos /* PR binutils/16199: Handle an alignment of zero. */ 5805 1.1 christos if (maxpagesize == 0) 5806 1.1 christos maxpagesize = 1; 5807 1.1 christos return ((vma - off) % maxpagesize); 5808 1.1 christos } 5809 1.1 christos 5810 1.1 christos static void 5811 1.1 christos print_segment_map (const struct elf_segment_map *m) 5812 1.1 christos { 5813 1.1 christos unsigned int j; 5814 1.1 christos const char *pt = get_segment_type (m->p_type); 5815 1.1 christos char buf[32]; 5816 1.1 christos 5817 1.1 christos if (pt == NULL) 5818 1.1 christos { 5819 1.1 christos if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) 5820 1.1 christos sprintf (buf, "LOPROC+%7.7x", 5821 1.1 christos (unsigned int) (m->p_type - PT_LOPROC)); 5822 1.1 christos else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) 5823 1.1 christos sprintf (buf, "LOOS+%7.7x", 5824 1.1 christos (unsigned int) (m->p_type - PT_LOOS)); 5825 1.1 christos else 5826 1.1 christos snprintf (buf, sizeof (buf), "%8.8x", 5827 1.1 christos (unsigned int) m->p_type); 5828 1.1 christos pt = buf; 5829 1.1 christos } 5830 1.1 christos fflush (stdout); 5831 1.1 christos fprintf (stderr, "%s:", pt); 5832 1.1 christos for (j = 0; j < m->count; j++) 5833 1.1 christos fprintf (stderr, " %s", m->sections [j]->name); 5834 1.1 christos putc ('\n',stderr); 5835 1.1 christos fflush (stderr); 5836 1.1 christos } 5837 1.1 christos 5838 1.1 christos /* Assign file positions to the sections based on the mapping from 5839 1.1 christos sections to segments. This function also sets up some fields in 5840 1.12 christos the file header. */ 5841 1.1 christos 5842 1.1 christos static bool 5843 1.1 christos assign_file_positions_for_load_sections (bfd *abfd, 5844 1.1 christos struct bfd_link_info *link_info) 5845 1.1 christos { 5846 1.10 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 5847 1.1 christos struct elf_segment_map *m; 5848 1.1 christos struct elf_segment_map *phdr_load_seg; 5849 1.12 christos Elf_Internal_Phdr *phdrs; 5850 1.1 christos Elf_Internal_Phdr *p; 5851 1.10 christos file_ptr off; /* Octets. */ 5852 1.1 christos bfd_size_type maxpagesize; 5853 1.10 christos unsigned int alloc, actual; 5854 1.12 christos unsigned int i, j; 5855 1.1 christos struct elf_segment_map **sorted_seg_map; 5856 1.1 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL); 5857 1.12 christos 5858 1.12 christos if (link_info == NULL 5859 1.1 christos && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL)) 5860 1.1 christos return false; 5861 1.3 christos 5862 1.10 christos alloc = 0; 5863 1.1 christos for (m = elf_seg_map (abfd); m != NULL; m = m->next) 5864 1.1 christos m->idx = alloc++; 5865 1.1 christos 5866 1.1 christos if (alloc) 5867 1.1 christos { 5868 1.1 christos elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; 5869 1.1 christos elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; 5870 1.1 christos } 5871 1.1 christos else 5872 1.1 christos { 5873 1.1 christos /* PR binutils/12467. */ 5874 1.1 christos elf_elfheader (abfd)->e_phoff = 0; 5875 1.1 christos elf_elfheader (abfd)->e_phentsize = 0; 5876 1.1 christos } 5877 1.1 christos 5878 1.3 christos elf_elfheader (abfd)->e_phnum = alloc; 5879 1.10 christos 5880 1.10 christos if (elf_program_header_size (abfd) == (bfd_size_type) -1) 5881 1.10 christos { 5882 1.10 christos actual = alloc; 5883 1.1 christos elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; 5884 1.10 christos } 5885 1.10 christos else 5886 1.10 christos { 5887 1.10 christos actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr; 5888 1.10 christos BFD_ASSERT (elf_program_header_size (abfd) 5889 1.10 christos == actual * bed->s->sizeof_phdr); 5890 1.1 christos BFD_ASSERT (actual >= alloc); 5891 1.1 christos } 5892 1.1 christos 5893 1.3 christos if (alloc == 0) 5894 1.12 christos { 5895 1.1 christos elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; 5896 1.1 christos return true; 5897 1.3 christos } 5898 1.1 christos 5899 1.1 christos /* We're writing the size in elf_program_header_size (abfd), 5900 1.3 christos see assign_file_positions_except_relocs, so make sure we have 5901 1.3 christos that amount allocated, with trailing space cleared. 5902 1.1 christos The variable alloc contains the computed need, while 5903 1.1 christos elf_program_header_size (abfd) contains the size used for the 5904 1.1 christos layout. 5905 1.1 christos See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments 5906 1.10 christos where the layout is forced to according to a larger size in the 5907 1.10 christos last iterations for the testcase ld-elf/header. */ 5908 1.10 christos phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs) 5909 1.1 christos + alloc * sizeof (*sorted_seg_map))); 5910 1.1 christos sorted_seg_map = (struct elf_segment_map **) (phdrs + actual); 5911 1.12 christos elf_tdata (abfd)->phdr = phdrs; 5912 1.1 christos if (phdrs == NULL) 5913 1.10 christos return false; 5914 1.10 christos 5915 1.10 christos for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++) 5916 1.10 christos { 5917 1.10 christos sorted_seg_map[j] = m; 5918 1.10 christos /* If elf_segment_map is not from map_sections_to_segments, the 5919 1.10 christos sections may not be correctly ordered. NOTE: sorting should 5920 1.10 christos not be done to the PT_NOTE section of a corefile, which may 5921 1.10 christos contain several pseudo-sections artificially created by bfd. 5922 1.10 christos Sorting these pseudo-sections breaks things badly. */ 5923 1.10 christos if (m->count > 1 5924 1.10 christos && !(elf_elfheader (abfd)->e_type == ET_CORE 5925 1.10 christos && m->p_type == PT_NOTE)) 5926 1.10 christos { 5927 1.10 christos for (i = 0; i < m->count; i++) 5928 1.10 christos m->sections[i]->target_index = i; 5929 1.10 christos qsort (m->sections, (size_t) m->count, sizeof (asection *), 5930 1.10 christos elf_sort_sections); 5931 1.10 christos } 5932 1.10 christos } 5933 1.10 christos if (alloc > 1) 5934 1.10 christos qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map), 5935 1.1 christos elf_sort_segments); 5936 1.1 christos 5937 1.12 christos maxpagesize = 1; 5938 1.12 christos if ((abfd->flags & D_PAGED) != 0) 5939 1.12 christos { 5940 1.12 christos if (link_info != NULL) 5941 1.12 christos maxpagesize = link_info->maxpagesize; 5942 1.12 christos else 5943 1.1 christos maxpagesize = bed->maxpagesize; 5944 1.10 christos } 5945 1.1 christos 5946 1.10 christos /* Sections must map to file offsets past the ELF file header. */ 5947 1.10 christos off = bed->s->sizeof_ehdr; 5948 1.10 christos /* And if one of the PT_LOAD headers doesn't include the program 5949 1.10 christos headers then we'll be mapping program headers in the usual 5950 1.10 christos position after the ELF file header. */ 5951 1.10 christos phdr_load_seg = NULL; 5952 1.10 christos for (j = 0; j < alloc; j++) 5953 1.10 christos { 5954 1.10 christos m = sorted_seg_map[j]; 5955 1.10 christos if (m->p_type != PT_LOAD) 5956 1.10 christos break; 5957 1.10 christos if (m->includes_phdrs) 5958 1.10 christos { 5959 1.10 christos phdr_load_seg = m; 5960 1.10 christos break; 5961 1.10 christos } 5962 1.10 christos } 5963 1.1 christos if (phdr_load_seg == NULL) 5964 1.10 christos off += actual * bed->s->sizeof_phdr; 5965 1.1 christos 5966 1.1 christos for (j = 0; j < alloc; j++) 5967 1.12 christos { 5968 1.12 christos asection **secpp; 5969 1.14 christos bfd_vma off_adjust; /* Octets. */ 5970 1.1 christos bool no_contents; 5971 1.1 christos bfd_size_type align_pagesize; 5972 1.1 christos 5973 1.1 christos /* An ELF segment (described by Elf_Internal_Phdr) may contain a 5974 1.1 christos number of sections with contents contributing to both p_filesz 5975 1.1 christos and p_memsz, followed by a number of sections with no contents 5976 1.10 christos that just contribute to p_memsz. In this loop, OFF tracks next 5977 1.10 christos available file offset for PT_LOAD and PT_NOTE segments. */ 5978 1.1 christos m = sorted_seg_map[j]; 5979 1.1 christos p = phdrs + m->idx; 5980 1.1 christos p->p_type = m->p_type; 5981 1.1 christos p->p_flags = m->p_flags; 5982 1.12 christos 5983 1.1 christos if (m->count == 0) 5984 1.12 christos p->p_vaddr = m->p_vaddr_offset * opb; 5985 1.1 christos else 5986 1.1 christos p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb; 5987 1.1 christos 5988 1.1 christos if (m->p_paddr_valid) 5989 1.1 christos p->p_paddr = m->p_paddr; 5990 1.1 christos else if (m->count == 0) 5991 1.12 christos p->p_paddr = 0; 5992 1.1 christos else 5993 1.14 christos p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb; 5994 1.1 christos 5995 1.1 christos align_pagesize = 0; 5996 1.1 christos if (p->p_type == PT_LOAD 5997 1.1 christos && (abfd->flags & D_PAGED) != 0) 5998 1.1 christos { 5999 1.1 christos /* p_align in demand paged PT_LOAD segments effectively stores 6000 1.1 christos the maximum page size. When copying an executable with 6001 1.1 christos objcopy, we set m->p_align from the input file. Use this 6002 1.1 christos value for maxpagesize rather than bed->maxpagesize, which 6003 1.1 christos may be different. Note that we use maxpagesize for PT_TLS 6004 1.1 christos segment alignment later in this function, so we are relying 6005 1.1 christos on at least one PT_LOAD segment appearing before a PT_TLS 6006 1.1 christos segment. */ 6007 1.14 christos if (m->p_align_valid) 6008 1.12 christos maxpagesize = m->p_align; 6009 1.12 christos else if (bed->p_align != 0 6010 1.14 christos && (link_info == NULL 6011 1.14 christos || !link_info->maxpagesize_is_set)) 6012 1.14 christos /* We will lay out this binary using maxpagesize but set 6013 1.14 christos p->p_align later to the possibly smaller bed->p_align. 6014 1.14 christos The run-time loader will then be able to load this 6015 1.14 christos binary when the system page size is maxpagesize, but if 6016 1.14 christos the system page size is smaller can use p->p_align. 6017 1.14 christos In either case p->p_align will be increased if 6018 1.1 christos necessary to match section alignment. */ 6019 1.1 christos align_pagesize = bed->p_align; 6020 1.1 christos 6021 1.1 christos p->p_align = maxpagesize; 6022 1.1 christos } 6023 1.1 christos else if (m->p_align_valid) 6024 1.1 christos p->p_align = m->p_align; 6025 1.10 christos else if (m->count == 0) 6026 1.10 christos p->p_align = 1 << bed->s->log_file_align; 6027 1.14 christos 6028 1.1 christos if (m == phdr_load_seg) 6029 1.12 christos off += actual * bed->s->sizeof_phdr; 6030 1.1 christos 6031 1.1 christos no_contents = false; 6032 1.1 christos off_adjust = 0; 6033 1.1 christos if (p->p_type == PT_LOAD 6034 1.12 christos && m->count > 0) 6035 1.1 christos { 6036 1.1 christos bfd_size_type align; /* Bytes. */ 6037 1.1 christos unsigned int align_power = 0; 6038 1.1 christos 6039 1.1 christos if (m->p_align_valid) 6040 1.1 christos align = p->p_align; 6041 1.1 christos else 6042 1.1 christos { 6043 1.1 christos for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) 6044 1.1 christos { 6045 1.10 christos unsigned int secalign; 6046 1.1 christos 6047 1.1 christos secalign = bfd_section_alignment (*secpp); 6048 1.1 christos if (secalign > align_power) 6049 1.1 christos align_power = secalign; 6050 1.14 christos } 6051 1.14 christos align = (bfd_size_type) 1 << align_power; 6052 1.14 christos /* If a section requires alignment higher than the 6053 1.14 christos minimum p_align value, don't reduce a maxpagesize 6054 1.14 christos p->p_align set earlier in this function. */ 6055 1.1 christos if (align > bed->p_align) 6056 1.14 christos align_pagesize = 0; 6057 1.12 christos if (align < maxpagesize) 6058 1.12 christos align = maxpagesize; 6059 1.12 christos else 6060 1.12 christos { 6061 1.12 christos /* If a section requires alignment higher than the 6062 1.14 christos maximum page size, set p_align to the section 6063 1.14 christos alignment. */ 6064 1.12 christos if ((abfd->flags & D_PAGED) != 0) 6065 1.1 christos p->p_align = align; 6066 1.1 christos } 6067 1.1 christos } 6068 1.1 christos 6069 1.1 christos for (i = 0; i < m->count; i++) 6070 1.1 christos if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) 6071 1.1 christos /* If we aren't making room for this section, then 6072 1.1 christos it must be SHT_NOBITS regardless of what we've 6073 1.1 christos set via struct bfd_elf_special_section. */ 6074 1.1 christos elf_section_type (m->sections[i]) = SHT_NOBITS; 6075 1.1 christos 6076 1.12 christos /* Find out whether this segment contains any loadable 6077 1.1 christos sections. */ 6078 1.1 christos no_contents = true; 6079 1.1 christos for (i = 0; i < m->count; i++) 6080 1.12 christos if (elf_section_type (m->sections[i]) != SHT_NOBITS) 6081 1.1 christos { 6082 1.1 christos no_contents = false; 6083 1.1 christos break; 6084 1.12 christos } 6085 1.9 christos 6086 1.9 christos off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb); 6087 1.9 christos 6088 1.9 christos /* Broken hardware and/or kernel require that files do not 6089 1.10 christos map the same page with different permissions on some hppa 6090 1.10 christos processors. */ 6091 1.9 christos if (j != 0 6092 1.9 christos && (abfd->flags & D_PAGED) != 0 6093 1.12 christos && bed->no_page_alias 6094 1.12 christos && (off & (maxpagesize - 1)) != 0 6095 1.9 christos && ((off & -maxpagesize) 6096 1.1 christos == ((off + off_adjust) & -maxpagesize))) 6097 1.1 christos off_adjust += maxpagesize; 6098 1.1 christos off += off_adjust; 6099 1.1 christos if (no_contents) 6100 1.1 christos { 6101 1.1 christos /* We shouldn't need to align the segment on disk since 6102 1.1 christos the segment doesn't need file space, but the gABI 6103 1.1 christos arguably requires the alignment and glibc ld.so 6104 1.1 christos checks it. So to comply with the alignment 6105 1.1 christos requirement but not waste file space, we adjust 6106 1.1 christos p_offset for just this segment. (OFF_ADJUST is 6107 1.1 christos subtracted from OFF later.) This may put p_offset 6108 1.1 christos past the end of file, but that shouldn't matter. */ 6109 1.1 christos } 6110 1.1 christos else 6111 1.1 christos off_adjust = 0; 6112 1.1 christos } 6113 1.1 christos /* Make sure the .dynamic section is the first section in the 6114 1.1 christos PT_DYNAMIC segment. */ 6115 1.1 christos else if (p->p_type == PT_DYNAMIC 6116 1.1 christos && m->count > 1 6117 1.1 christos && strcmp (m->sections[0]->name, ".dynamic") != 0) 6118 1.9 christos { 6119 1.9 christos _bfd_error_handler 6120 1.1 christos (_("%pB: The first section in the PT_DYNAMIC segment" 6121 1.1 christos " is not the .dynamic section"), 6122 1.12 christos abfd); 6123 1.1 christos bfd_set_error (bfd_error_bad_value); 6124 1.1 christos return false; 6125 1.1 christos } 6126 1.1 christos /* Set the note section type to SHT_NOTE. */ 6127 1.1 christos else if (p->p_type == PT_NOTE) 6128 1.1 christos for (i = 0; i < m->count; i++) 6129 1.1 christos elf_section_type (m->sections[i]) = SHT_NOTE; 6130 1.1 christos 6131 1.1 christos if (m->includes_filehdr) 6132 1.1 christos { 6133 1.1 christos if (!m->p_flags_valid) 6134 1.1 christos p->p_flags |= PF_R; 6135 1.10 christos p->p_filesz = bed->s->sizeof_ehdr; 6136 1.1 christos p->p_memsz = bed->s->sizeof_ehdr; 6137 1.10 christos if (p->p_type == PT_LOAD) 6138 1.1 christos { 6139 1.10 christos if (m->count > 0) 6140 1.10 christos { 6141 1.10 christos if (p->p_vaddr < (bfd_vma) off 6142 1.10 christos || (!m->p_paddr_valid 6143 1.10 christos && p->p_paddr < (bfd_vma) off)) 6144 1.10 christos { 6145 1.10 christos _bfd_error_handler 6146 1.10 christos (_("%pB: not enough room for program headers," 6147 1.10 christos " try linking with -N"), 6148 1.12 christos abfd); 6149 1.10 christos bfd_set_error (bfd_error_bad_value); 6150 1.10 christos return false; 6151 1.10 christos } 6152 1.10 christos p->p_vaddr -= off; 6153 1.1 christos if (!m->p_paddr_valid) 6154 1.10 christos p->p_paddr -= off; 6155 1.10 christos } 6156 1.10 christos } 6157 1.10 christos else if (sorted_seg_map[0]->includes_filehdr) 6158 1.10 christos { 6159 1.1 christos Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx; 6160 1.10 christos p->p_vaddr = filehdr->p_vaddr; 6161 1.1 christos if (!m->p_paddr_valid) 6162 1.1 christos p->p_paddr = filehdr->p_paddr; 6163 1.1 christos } 6164 1.1 christos } 6165 1.1 christos 6166 1.1 christos if (m->includes_phdrs) 6167 1.1 christos { 6168 1.10 christos if (!m->p_flags_valid) 6169 1.10 christos p->p_flags |= PF_R; 6170 1.1 christos p->p_filesz += actual * bed->s->sizeof_phdr; 6171 1.1 christos p->p_memsz += actual * bed->s->sizeof_phdr; 6172 1.10 christos if (!m->includes_filehdr) 6173 1.10 christos { 6174 1.14 christos if (p->p_type == PT_LOAD) 6175 1.10 christos { 6176 1.10 christos p->p_offset = off - actual * bed->s->sizeof_phdr; 6177 1.10 christos elf_elfheader (abfd)->e_phoff = p->p_offset; 6178 1.10 christos if (m->count > 0) 6179 1.10 christos { 6180 1.10 christos p->p_vaddr -= off - p->p_offset; 6181 1.10 christos if (!m->p_paddr_valid) 6182 1.10 christos p->p_paddr -= off - p->p_offset; 6183 1.10 christos } 6184 1.1 christos } 6185 1.14 christos else if (phdr_load_seg != NULL) 6186 1.14 christos { 6187 1.14 christos /* Also set PT_PHDR to match phdr_load_seg. We've 6188 1.10 christos sorted segments so that phdr_load_seg will 6189 1.12 christos already be set by the code immediately above. */ 6190 1.10 christos Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx; 6191 1.10 christos bfd_vma phdr_off = 0; /* Octets. */ 6192 1.10 christos if (phdr_load_seg->includes_filehdr) 6193 1.1 christos phdr_off = bed->s->sizeof_ehdr; 6194 1.10 christos p->p_vaddr = phdr->p_vaddr + phdr_off; 6195 1.10 christos if (!m->p_paddr_valid) 6196 1.1 christos p->p_paddr = phdr->p_paddr + phdr_off; 6197 1.10 christos p->p_offset = phdr->p_offset + phdr_off; 6198 1.10 christos } 6199 1.1 christos else 6200 1.1 christos p->p_offset = bed->s->sizeof_ehdr; 6201 1.1 christos } 6202 1.1 christos } 6203 1.1 christos 6204 1.1 christos if (p->p_type == PT_LOAD 6205 1.1 christos || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) 6206 1.10 christos { 6207 1.10 christos if (!m->includes_filehdr && !m->includes_phdrs) 6208 1.10 christos { 6209 1.10 christos p->p_offset = off; 6210 1.10 christos if (no_contents) 6211 1.10 christos { 6212 1.10 christos /* Put meaningless p_offset for PT_LOAD segments 6213 1.10 christos without file contents somewhere within the first 6214 1.10 christos page, in an attempt to not point past EOF. */ 6215 1.10 christos bfd_size_type align = maxpagesize; 6216 1.10 christos if (align < p->p_align) 6217 1.10 christos align = p->p_align; 6218 1.14 christos if (align < 1) 6219 1.14 christos align = 1; 6220 1.14 christos /* Avoid p_offset of zero, which might be wrongly 6221 1.14 christos interpreted as the segment being the first one, 6222 1.10 christos containing the file header. PR32763. */ 6223 1.10 christos p->p_offset = (off + align - 1) % align + 1; 6224 1.1 christos } 6225 1.1 christos } 6226 1.12 christos else 6227 1.1 christos { 6228 1.1 christos file_ptr adjust; /* Octets. */ 6229 1.1 christos 6230 1.1 christos adjust = off - (p->p_offset + p->p_filesz); 6231 1.1 christos if (!no_contents) 6232 1.1 christos p->p_filesz += adjust; 6233 1.1 christos p->p_memsz += adjust; 6234 1.1 christos } 6235 1.14 christos } 6236 1.14 christos 6237 1.14 christos if (align_pagesize) 6238 1.1 christos p->p_align = align_pagesize; 6239 1.1 christos 6240 1.1 christos /* Set up p_filesz, p_memsz, p_align and p_flags from the section 6241 1.1 christos maps. Set filepos for sections in PT_LOAD segments, and in 6242 1.1 christos core files, for sections in PT_NOTE segments. 6243 1.1 christos assign_file_positions_for_non_load_sections will set filepos 6244 1.1 christos for other sections and update p_filesz for other segments. */ 6245 1.1 christos for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) 6246 1.1 christos { 6247 1.1 christos asection *sec; 6248 1.1 christos bfd_size_type align; 6249 1.1 christos Elf_Internal_Shdr *this_hdr; 6250 1.1 christos 6251 1.10 christos sec = *secpp; 6252 1.1 christos this_hdr = &elf_section_data (sec)->this_hdr; 6253 1.1 christos align = (bfd_size_type) 1 << bfd_section_alignment (sec); 6254 1.1 christos 6255 1.1 christos if ((p->p_type == PT_LOAD 6256 1.1 christos || p->p_type == PT_TLS) 6257 1.1 christos && (this_hdr->sh_type != SHT_NOBITS 6258 1.1 christos || ((this_hdr->sh_flags & SHF_ALLOC) != 0 6259 1.1 christos && ((this_hdr->sh_flags & SHF_TLS) == 0 6260 1.13 christos || p->p_type == PT_TLS)))) 6261 1.13 christos { 6262 1.13 christos bfd_vma p_start = p->p_paddr; /* Octets. */ 6263 1.13 christos bfd_vma p_end = p_start + p->p_memsz; /* Octets. */ 6264 1.1 christos bfd_vma s_start = sec->lma * opb; /* Octets. */ 6265 1.1 christos bfd_vma adjust = s_start - p_end; /* Octets. */ 6266 1.1 christos 6267 1.1 christos if (adjust != 0 6268 1.1 christos && (s_start < p_end 6269 1.9 christos || p_end < p_start)) 6270 1.9 christos { 6271 1.13 christos _bfd_error_handler 6272 1.13 christos /* xgettext:c-format */ 6273 1.12 christos (_("%pB: section %pA lma %#" PRIx64 6274 1.12 christos " adjusted to %#" PRIx64), 6275 1.1 christos abfd, sec, (uint64_t) s_start / opb, 6276 1.12 christos (uint64_t) p_end / opb); 6277 1.1 christos adjust = 0; 6278 1.1 christos sec->lma = p_end / opb; 6279 1.1 christos } 6280 1.12 christos p->p_memsz += adjust; 6281 1.1 christos 6282 1.12 christos if (p->p_type == PT_LOAD) 6283 1.1 christos { 6284 1.12 christos if (this_hdr->sh_type != SHT_NOBITS) 6285 1.10 christos { 6286 1.10 christos off_adjust = 0; 6287 1.10 christos if (p->p_filesz + adjust < p->p_memsz) 6288 1.13 christos { 6289 1.13 christos /* We have a PROGBITS section following NOBITS ones. 6290 1.13 christos Allocate file space for the NOBITS section(s). 6291 1.13 christos We don't need to write out the zeros, posix 6292 1.13 christos fseek past the end of data already written 6293 1.13 christos followed by a write at that location is 6294 1.10 christos guaranteed to result in zeros being read 6295 1.10 christos from the gap. */ 6296 1.12 christos adjust = p->p_memsz - p->p_filesz; 6297 1.12 christos } 6298 1.12 christos } 6299 1.12 christos /* We only adjust sh_offset in SHT_NOBITS sections 6300 1.12 christos as would seem proper for their address when the 6301 1.12 christos section is first in the segment. sh_offset 6302 1.12 christos doesn't really have any significance for 6303 1.12 christos SHT_NOBITS anyway, apart from a notional position 6304 1.12 christos relative to other sections. Historically we 6305 1.12 christos didn't bother with adjusting sh_offset and some 6306 1.12 christos programs depend on it not being adjusted. See 6307 1.12 christos pr12921 and pr25662. */ 6308 1.10 christos if (this_hdr->sh_type != SHT_NOBITS || i == 0) 6309 1.12 christos { 6310 1.12 christos off += adjust; 6311 1.1 christos if (this_hdr->sh_type == SHT_NOBITS) 6312 1.1 christos off_adjust += adjust; 6313 1.12 christos } 6314 1.12 christos } 6315 1.1 christos if (this_hdr->sh_type != SHT_NOBITS) 6316 1.1 christos p->p_filesz += adjust; 6317 1.1 christos } 6318 1.1 christos 6319 1.1 christos if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) 6320 1.1 christos { 6321 1.1 christos /* The section at i == 0 is the one that actually contains 6322 1.1 christos everything. */ 6323 1.1 christos if (i == 0) 6324 1.1 christos { 6325 1.1 christos this_hdr->sh_offset = sec->filepos = off; 6326 1.1 christos off += this_hdr->sh_size; 6327 1.1 christos p->p_filesz = this_hdr->sh_size; 6328 1.1 christos p->p_memsz = 0; 6329 1.1 christos p->p_align = 1; 6330 1.1 christos } 6331 1.1 christos else 6332 1.1 christos { 6333 1.1 christos /* The rest are fake sections that shouldn't be written. */ 6334 1.1 christos sec->filepos = 0; 6335 1.1 christos sec->size = 0; 6336 1.1 christos sec->flags = 0; 6337 1.1 christos continue; 6338 1.1 christos } 6339 1.1 christos } 6340 1.14 christos else 6341 1.14 christos { 6342 1.14 christos if (this_hdr->sh_type == SHT_NOBITS 6343 1.14 christos && (this_hdr->sh_flags & SHF_TLS) != 0 6344 1.14 christos && this_hdr->sh_offset == 0) 6345 1.14 christos { 6346 1.14 christos /* Set sh_offset for .tbss sections to their nominal 6347 1.14 christos offset after aligning. They are not loaded from 6348 1.14 christos disk so the value doesn't really matter, except 6349 1.14 christos when the .tbss section is the first one in a 6350 1.14 christos PT_TLS segment. In that case it sets the 6351 1.14 christos p_offset for the PT_TLS segment, which according 6352 1.14 christos to the ELF gABI ought to satisfy 6353 1.14 christos p_offset % p_align == p_vaddr % p_align. */ 6354 1.14 christos bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, 6355 1.14 christos off, align); 6356 1.14 christos this_hdr->sh_offset = sec->filepos = off + adjust; 6357 1.1 christos } 6358 1.1 christos else if (p->p_type == PT_LOAD) 6359 1.1 christos { 6360 1.1 christos this_hdr->sh_offset = sec->filepos = off; 6361 1.1 christos if (this_hdr->sh_type != SHT_NOBITS) 6362 1.1 christos off += this_hdr->sh_size; 6363 1.1 christos } 6364 1.1 christos 6365 1.1 christos if (this_hdr->sh_type != SHT_NOBITS) 6366 1.1 christos { 6367 1.1 christos p->p_filesz += this_hdr->sh_size; 6368 1.1 christos /* A load section without SHF_ALLOC is something like 6369 1.1 christos a note section in a PT_NOTE segment. These take 6370 1.1 christos file space but are not loaded into memory. */ 6371 1.1 christos if ((this_hdr->sh_flags & SHF_ALLOC) != 0) 6372 1.1 christos p->p_memsz += this_hdr->sh_size; 6373 1.1 christos } 6374 1.1 christos else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) 6375 1.1 christos { 6376 1.1 christos if (p->p_type == PT_TLS) 6377 1.1 christos p->p_memsz += this_hdr->sh_size; 6378 1.1 christos 6379 1.1 christos /* .tbss is special. It doesn't contribute to p_memsz of 6380 1.1 christos normal segments. */ 6381 1.1 christos else if ((this_hdr->sh_flags & SHF_TLS) == 0) 6382 1.1 christos p->p_memsz += this_hdr->sh_size; 6383 1.1 christos } 6384 1.1 christos 6385 1.1 christos if (align > p->p_align 6386 1.1 christos && !m->p_align_valid 6387 1.1 christos && (p->p_type != PT_LOAD 6388 1.1 christos || (abfd->flags & D_PAGED) == 0)) 6389 1.1 christos p->p_align = align; 6390 1.1 christos } 6391 1.1 christos 6392 1.1 christos if (!m->p_flags_valid) 6393 1.1 christos { 6394 1.1 christos p->p_flags |= PF_R; 6395 1.1 christos if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) 6396 1.1 christos p->p_flags |= PF_X; 6397 1.1 christos if ((this_hdr->sh_flags & SHF_WRITE) != 0) 6398 1.1 christos p->p_flags |= PF_W; 6399 1.3 christos } 6400 1.1 christos } 6401 1.1 christos 6402 1.10 christos off -= off_adjust; 6403 1.10 christos 6404 1.10 christos /* PR ld/20815 - Check that the program header segment, if 6405 1.10 christos present, will be loaded into memory. */ 6406 1.10 christos if (p->p_type == PT_PHDR 6407 1.10 christos && phdr_load_seg == NULL 6408 1.10 christos && !(bed->elf_backend_allow_non_load_phdr != NULL 6409 1.10 christos && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc))) 6410 1.10 christos { 6411 1.10 christos /* The fix for this error is usually to edit the linker script being 6412 1.10 christos used and set up the program headers manually. Either that or 6413 1.10 christos leave room for the headers at the start of the SECTIONS. */ 6414 1.10 christos _bfd_error_handler (_("%pB: error: PHDR segment not covered" 6415 1.10 christos " by LOAD segment"), 6416 1.12 christos abfd); 6417 1.10 christos if (link_info == NULL) 6418 1.10 christos return false; 6419 1.10 christos /* Arrange for the linker to exit with an error, deleting 6420 1.10 christos the output file unless --noinhibit-exec is given. */ 6421 1.10 christos link_info->callbacks->info ("%X"); 6422 1.1 christos } 6423 1.1 christos 6424 1.1 christos /* Check that all sections are in a PT_LOAD segment. 6425 1.1 christos Don't check funky gdb generated core files. */ 6426 1.12 christos if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) 6427 1.1 christos { 6428 1.1 christos bool check_vma = true; 6429 1.1 christos 6430 1.1 christos for (i = 1; i < m->count; i++) 6431 1.1 christos if (m->sections[i]->vma == m->sections[i - 1]->vma 6432 1.1 christos && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) 6433 1.1 christos ->this_hdr), p) != 0 6434 1.1 christos && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) 6435 1.1 christos ->this_hdr), p) != 0) 6436 1.12 christos { 6437 1.1 christos /* Looks like we have overlays packed into the segment. */ 6438 1.1 christos check_vma = false; 6439 1.1 christos break; 6440 1.1 christos } 6441 1.1 christos 6442 1.1 christos for (i = 0; i < m->count; i++) 6443 1.1 christos { 6444 1.1 christos Elf_Internal_Shdr *this_hdr; 6445 1.1 christos asection *sec; 6446 1.1 christos 6447 1.1 christos sec = m->sections[i]; 6448 1.1 christos this_hdr = &(elf_section_data(sec)->this_hdr); 6449 1.1 christos if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) 6450 1.9 christos && !ELF_TBSS_SPECIAL (this_hdr, p)) 6451 1.9 christos { 6452 1.14 christos _bfd_error_handler 6453 1.14 christos /* xgettext:c-format */ 6454 1.1 christos (_("%pB: section `%pA' can't be allocated in segment %u"), 6455 1.1 christos abfd, sec, m->idx); 6456 1.1 christos print_segment_map (m); 6457 1.1 christos } 6458 1.1 christos } 6459 1.1 christos } 6460 1.3 christos } 6461 1.10 christos 6462 1.10 christos elf_next_file_pos (abfd) = off; 6463 1.10 christos 6464 1.10 christos if (link_info != NULL 6465 1.10 christos && phdr_load_seg != NULL 6466 1.10 christos && phdr_load_seg->includes_filehdr) 6467 1.10 christos { 6468 1.10 christos /* There is a segment that contains both the file headers and the 6469 1.10 christos program headers, so provide a symbol __ehdr_start pointing there. 6470 1.14 christos A program can use this to examine itself robustly. */ 6471 1.14 christos 6472 1.14 christos struct elf_link_hash_table *htab = elf_hash_table (link_info); 6473 1.10 christos struct elf_link_hash_entry *hash = htab->hehdr_start; 6474 1.10 christos 6475 1.10 christos /* If the symbol was referenced and not defined, define it. */ 6476 1.10 christos if (hash != NULL 6477 1.10 christos && (hash->root.type == bfd_link_hash_new 6478 1.10 christos || hash->root.type == bfd_link_hash_undefined 6479 1.10 christos || hash->root.type == bfd_link_hash_undefweak 6480 1.10 christos || hash->root.type == bfd_link_hash_common)) 6481 1.12 christos { 6482 1.10 christos asection *s = NULL; 6483 1.10 christos bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb; 6484 1.10 christos 6485 1.10 christos if (phdr_load_seg->count != 0) 6486 1.10 christos /* The segment contains sections, so use the first one. */ 6487 1.10 christos s = phdr_load_seg->sections[0]; 6488 1.10 christos else 6489 1.10 christos /* Use the first (i.e. lowest-addressed) section in any segment. */ 6490 1.10 christos for (m = elf_seg_map (abfd); m != NULL; m = m->next) 6491 1.10 christos if (m->p_type == PT_LOAD && m->count != 0) 6492 1.10 christos { 6493 1.10 christos s = m->sections[0]; 6494 1.10 christos break; 6495 1.10 christos } 6496 1.10 christos 6497 1.10 christos if (s != NULL) 6498 1.10 christos { 6499 1.10 christos hash->root.u.def.value = filehdr_vaddr - s->vma; 6500 1.10 christos hash->root.u.def.section = s; 6501 1.10 christos } 6502 1.10 christos else 6503 1.10 christos { 6504 1.10 christos hash->root.u.def.value = filehdr_vaddr; 6505 1.10 christos hash->root.u.def.section = bfd_abs_section_ptr; 6506 1.10 christos } 6507 1.10 christos 6508 1.10 christos hash->root.type = bfd_link_hash_defined; 6509 1.10 christos hash->def_regular = 1; 6510 1.10 christos hash->non_elf = 0; 6511 1.10 christos } 6512 1.12 christos } 6513 1.10 christos 6514 1.10 christos return true; 6515 1.10 christos } 6516 1.10 christos 6517 1.10 christos /* Determine if a bfd is a debuginfo file. Unfortunately there 6518 1.10 christos is no defined method for detecting such files, so we have to 6519 1.12 christos use heuristics instead. */ 6520 1.10 christos 6521 1.10 christos bool 6522 1.10 christos is_debuginfo_file (bfd *abfd) 6523 1.12 christos { 6524 1.10 christos if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour) 6525 1.10 christos return false; 6526 1.10 christos 6527 1.10 christos Elf_Internal_Shdr **start_headers = elf_elfsections (abfd); 6528 1.10 christos Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd); 6529 1.10 christos Elf_Internal_Shdr **headerp; 6530 1.10 christos 6531 1.10 christos for (headerp = start_headers; headerp < end_headers; headerp ++) 6532 1.10 christos { 6533 1.10 christos Elf_Internal_Shdr *header = * headerp; 6534 1.10 christos 6535 1.10 christos /* Debuginfo files do not have any allocated SHT_PROGBITS sections. 6536 1.10 christos The only allocated sections are SHT_NOBITS or SHT_NOTES. */ 6537 1.10 christos if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC 6538 1.12 christos && header->sh_type != SHT_NOBITS 6539 1.10 christos && header->sh_type != SHT_NOTE) 6540 1.10 christos return false; 6541 1.12 christos } 6542 1.1 christos 6543 1.1 christos return true; 6544 1.13 christos } 6545 1.13 christos 6546 1.1 christos /* Assign file positions for other sections, except for compressed debug 6547 1.12 christos and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */ 6548 1.1 christos 6549 1.1 christos static bool 6550 1.1 christos assign_file_positions_for_non_load_sections (bfd *abfd, 6551 1.1 christos struct bfd_link_info *link_info) 6552 1.1 christos { 6553 1.3 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 6554 1.1 christos Elf_Internal_Shdr **i_shdrpp; 6555 1.1 christos Elf_Internal_Shdr **hdrpp, **end_hdrpp; 6556 1.1 christos Elf_Internal_Phdr *phdrs; 6557 1.1 christos Elf_Internal_Phdr *p; 6558 1.12 christos struct elf_segment_map *m; 6559 1.12 christos file_ptr off; 6560 1.1 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL); 6561 1.12 christos bfd_vma maxpagesize; 6562 1.12 christos 6563 1.12 christos if (link_info != NULL) 6564 1.12 christos maxpagesize = link_info->maxpagesize; 6565 1.1 christos else 6566 1.3 christos maxpagesize = bed->maxpagesize; 6567 1.3 christos i_shdrpp = elf_elfsections (abfd); 6568 1.3 christos end_hdrpp = i_shdrpp + elf_numsections (abfd); 6569 1.1 christos off = elf_next_file_pos (abfd); 6570 1.1 christos for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) 6571 1.12 christos { 6572 1.1 christos Elf_Internal_Shdr *hdr; 6573 1.1 christos bfd_vma align; 6574 1.1 christos 6575 1.1 christos hdr = *hdrpp; 6576 1.1 christos if (hdr->bfd_section != NULL 6577 1.1 christos && (hdr->bfd_section->filepos != 0 6578 1.1 christos || (hdr->sh_type == SHT_NOBITS 6579 1.1 christos && hdr->contents == NULL))) 6580 1.1 christos BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); 6581 1.10 christos else if ((hdr->sh_flags & SHF_ALLOC) != 0) 6582 1.10 christos { 6583 1.10 christos if (hdr->sh_size != 0 6584 1.10 christos /* PR 24717 - debuginfo files are known to be not strictly 6585 1.10 christos compliant with the ELF standard. In particular they often 6586 1.10 christos have .note.gnu.property sections that are outside of any 6587 1.10 christos loadable segment. This is not a problem for such files, 6588 1.9 christos so do not warn about them. */ 6589 1.9 christos && ! is_debuginfo_file (abfd)) 6590 1.9 christos _bfd_error_handler 6591 1.1 christos /* xgettext:c-format */ 6592 1.1 christos (_("%pB: warning: allocated section `%s' not in segment"), 6593 1.1 christos abfd, 6594 1.1 christos (hdr->bfd_section == NULL 6595 1.1 christos ? "*unknown*" 6596 1.1 christos : hdr->bfd_section->name)); 6597 1.12 christos /* We don't need to page align empty sections. */ 6598 1.1 christos if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) 6599 1.12 christos align = maxpagesize; 6600 1.12 christos else 6601 1.14 christos align = hdr->sh_addralign & -hdr->sh_addralign; 6602 1.14 christos off += vma_page_aligned_bias (hdr->sh_addr, off, align); 6603 1.1 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, false, 6604 1.1 christos bed->s->log_file_align); 6605 1.1 christos } 6606 1.13 christos else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) 6607 1.13 christos && hdr->bfd_section == NULL) 6608 1.13 christos /* We don't know the offset of these sections yet: 6609 1.13 christos their size has not been decided. */ 6610 1.13 christos || (abfd->is_linker_output 6611 1.13 christos && hdr->bfd_section != NULL 6612 1.3 christos && (hdr->sh_name == -1u 6613 1.3 christos || bfd_section_is_ctf (hdr->bfd_section))) 6614 1.3 christos || hdr == i_shdrpp[elf_onesymtab (abfd)] 6615 1.3 christos || (elf_symtab_shndx_list (abfd) != NULL 6616 1.3 christos && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) 6617 1.1 christos || hdr == i_shdrpp[elf_strtab_sec (abfd)] 6618 1.1 christos || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) 6619 1.14 christos hdr->sh_offset = -1; 6620 1.1 christos else 6621 1.10 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0); 6622 1.1 christos } 6623 1.1 christos elf_next_file_pos (abfd) = off; 6624 1.1 christos 6625 1.1 christos /* Now that we have set the section file positions, we can set up 6626 1.3 christos the file positions for the non PT_LOAD segments. */ 6627 1.1 christos phdrs = elf_tdata (abfd)->phdr; 6628 1.1 christos for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) 6629 1.1 christos { 6630 1.12 christos if (p->p_type == PT_GNU_RELRO) 6631 1.12 christos { 6632 1.1 christos bfd_vma start, end; /* Bytes. */ 6633 1.1 christos bool ok; 6634 1.1 christos 6635 1.1 christos if (link_info != NULL) 6636 1.9 christos { 6637 1.9 christos /* During linking the range of the RELRO segment is passed 6638 1.9 christos in link_info. Note that there may be padding between 6639 1.9 christos relro_start and the first RELRO section. */ 6640 1.9 christos start = link_info->relro_start; 6641 1.9 christos end = link_info->relro_end; 6642 1.9 christos } 6643 1.9 christos else if (m->count != 0) 6644 1.9 christos { 6645 1.9 christos if (!m->p_size_valid) 6646 1.12 christos abort (); 6647 1.9 christos start = m->sections[0]->vma; 6648 1.9 christos end = start + m->p_size / opb; 6649 1.9 christos } 6650 1.9 christos else 6651 1.9 christos { 6652 1.9 christos start = 0; 6653 1.9 christos end = 0; 6654 1.12 christos } 6655 1.9 christos 6656 1.9 christos ok = false; 6657 1.9 christos if (start < end) 6658 1.9 christos { 6659 1.9 christos struct elf_segment_map *lm; 6660 1.9 christos const Elf_Internal_Phdr *lp; 6661 1.9 christos unsigned int i; 6662 1.9 christos 6663 1.3 christos /* Find a LOAD segment containing a section in the RELRO 6664 1.1 christos segment. */ 6665 1.1 christos for (lm = elf_seg_map (abfd), lp = phdrs; 6666 1.1 christos lm != NULL; 6667 1.1 christos lm = lm->next, lp++) 6668 1.1 christos { 6669 1.9 christos if (lp->p_type == PT_LOAD 6670 1.9 christos && lm->count != 0 6671 1.12 christos && (lm->sections[lm->count - 1]->vma 6672 1.9 christos + (!IS_TBSS (lm->sections[lm->count - 1]) 6673 1.9 christos ? lm->sections[lm->count - 1]->size / opb 6674 1.1 christos : 0)) > start 6675 1.1 christos && lm->sections[0]->vma < end) 6676 1.1 christos break; 6677 1.9 christos } 6678 1.1 christos 6679 1.9 christos if (lm != NULL) 6680 1.9 christos { 6681 1.9 christos /* Find the section starting the RELRO segment. */ 6682 1.9 christos for (i = 0; i < lm->count; i++) 6683 1.9 christos { 6684 1.9 christos asection *s = lm->sections[i]; 6685 1.9 christos if (s->vma >= start 6686 1.9 christos && s->vma < end 6687 1.9 christos && s->size != 0) 6688 1.9 christos break; 6689 1.9 christos } 6690 1.9 christos 6691 1.12 christos if (i < lm->count) 6692 1.12 christos { 6693 1.9 christos p->p_vaddr = lm->sections[i]->vma * opb; 6694 1.12 christos p->p_paddr = lm->sections[i]->lma * opb; 6695 1.9 christos p->p_offset = lm->sections[i]->filepos; 6696 1.9 christos p->p_memsz = end * opb - p->p_vaddr; 6697 1.9 christos p->p_filesz = p->p_memsz; 6698 1.9 christos 6699 1.9 christos /* The RELRO segment typically ends a few bytes 6700 1.9 christos into .got.plt but other layouts are possible. 6701 1.9 christos In cases where the end does not match any 6702 1.9 christos loaded section (for instance is in file 6703 1.9 christos padding), trim p_filesz back to correspond to 6704 1.9 christos the end of loaded section contents. */ 6705 1.9 christos if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr) 6706 1.9 christos p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr; 6707 1.9 christos 6708 1.9 christos /* Preserve the alignment and flags if they are 6709 1.9 christos valid. The gold linker generates RW/4 for 6710 1.9 christos the PT_GNU_RELRO section. It is better for 6711 1.9 christos objcopy/strip to honor these attributes 6712 1.9 christos otherwise gdb will choke when using separate 6713 1.9 christos debug files. */ 6714 1.9 christos if (!m->p_align_valid) 6715 1.9 christos p->p_align = 1; 6716 1.12 christos if (!m->p_flags_valid) 6717 1.9 christos p->p_flags = PF_R; 6718 1.1 christos ok = true; 6719 1.1 christos } 6720 1.12 christos } 6721 1.9 christos } 6722 1.12 christos 6723 1.12 christos if (!ok) 6724 1.12 christos { 6725 1.13 christos if (link_info != NULL) 6726 1.13 christos _bfd_error_handler 6727 1.12 christos (_("%pB: warning: unable to allocate any sections" 6728 1.12 christos " to PT_GNU_RELRO segment"), 6729 1.12 christos abfd); 6730 1.1 christos memset (p, 0, sizeof *p); 6731 1.3 christos } 6732 1.3 christos } 6733 1.3 christos else if (p->p_type == PT_GNU_STACK) 6734 1.3 christos { 6735 1.3 christos if (m->p_size_valid) 6736 1.1 christos p->p_memsz = m->p_size; 6737 1.1 christos } 6738 1.3 christos else if (m->count != 0) 6739 1.9 christos { 6740 1.1 christos unsigned int i; 6741 1.1 christos 6742 1.1 christos if (p->p_type != PT_LOAD 6743 1.1 christos && (p->p_type != PT_NOTE 6744 1.9 christos || bfd_get_format (abfd) != bfd_core)) 6745 1.9 christos { 6746 1.9 christos /* A user specified segment layout may include a PHDR 6747 1.9 christos segment that overlaps with a LOAD segment... */ 6748 1.9 christos if (p->p_type == PT_PHDR) 6749 1.9 christos { 6750 1.9 christos m->count = 0; 6751 1.9 christos continue; 6752 1.3 christos } 6753 1.3 christos 6754 1.3 christos if (m->includes_filehdr || m->includes_phdrs) 6755 1.9 christos { 6756 1.9 christos /* PR 17512: file: 2195325e. */ 6757 1.9 christos _bfd_error_handler 6758 1.9 christos (_("%pB: error: non-load segment %d includes file header " 6759 1.12 christos "and/or program header"), 6760 1.3 christos abfd, (int) (p - phdrs)); 6761 1.1 christos return false; 6762 1.1 christos } 6763 1.1 christos 6764 1.1 christos p->p_filesz = 0; 6765 1.1 christos p->p_offset = m->sections[0]->filepos; 6766 1.1 christos for (i = m->count; i-- != 0;) 6767 1.1 christos { 6768 1.1 christos asection *sect = m->sections[i]; 6769 1.1 christos Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; 6770 1.12 christos if (hdr->sh_type != SHT_NOBITS) 6771 1.12 christos { 6772 1.12 christos p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size; 6773 1.12 christos /* NB: p_memsz of the loadable PT_NOTE segment 6774 1.12 christos should be the same as p_filesz. */ 6775 1.12 christos if (p->p_type == PT_NOTE 6776 1.1 christos && (hdr->sh_flags & SHF_ALLOC) != 0) 6777 1.1 christos p->p_memsz = p->p_filesz; 6778 1.1 christos break; 6779 1.1 christos } 6780 1.1 christos } 6781 1.1 christos } 6782 1.1 christos } 6783 1.12 christos } 6784 1.1 christos 6785 1.1 christos return true; 6786 1.3 christos } 6787 1.3 christos 6788 1.3 christos static elf_section_list * 6789 1.3 christos find_section_in_list (unsigned int i, elf_section_list * list) 6790 1.3 christos { 6791 1.3 christos for (;list != NULL; list = list->next) 6792 1.3 christos if (list->ndx == i) 6793 1.3 christos break; 6794 1.3 christos return list; 6795 1.1 christos } 6796 1.1 christos 6797 1.1 christos /* Work out the file positions of all the sections. This is called by 6798 1.1 christos _bfd_elf_compute_section_file_positions. All the section sizes and 6799 1.1 christos VMAs must be known before this is called. 6800 1.13 christos 6801 1.13 christos Reloc sections come in two flavours: Those processed specially as 6802 1.13 christos "side-channel" data attached to a section to which they apply, and 6803 1.13 christos those that bfd doesn't process as relocations. The latter sort are 6804 1.13 christos stored in a normal bfd section by bfd_section_from_shdr. We don't 6805 1.13 christos consider the former sort here, unless they form part of the loadable 6806 1.13 christos image. Reloc sections not assigned here (and compressed debugging 6807 1.1 christos sections and CTF sections which nothing else in the file can rely 6808 1.1 christos upon) will be handled later by assign_file_positions_for_relocs. 6809 1.1 christos 6810 1.12 christos We also don't set the positions of the .symtab and .strtab here. */ 6811 1.1 christos 6812 1.1 christos static bool 6813 1.1 christos assign_file_positions_except_relocs (bfd *abfd, 6814 1.1 christos struct bfd_link_info *link_info) 6815 1.1 christos { 6816 1.1 christos struct elf_obj_tdata *tdata = elf_tdata (abfd); 6817 1.10 christos Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); 6818 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 6819 1.1 christos unsigned int alloc; 6820 1.1 christos 6821 1.1 christos if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 6822 1.1 christos && bfd_get_format (abfd) != bfd_core) 6823 1.1 christos { 6824 1.1 christos Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); 6825 1.1 christos unsigned int num_sec = elf_numsections (abfd); 6826 1.3 christos Elf_Internal_Shdr **hdrpp; 6827 1.1 christos unsigned int i; 6828 1.1 christos file_ptr off; 6829 1.1 christos 6830 1.1 christos /* Start after the ELF header. */ 6831 1.1 christos off = i_ehdrp->e_ehsize; 6832 1.1 christos 6833 1.1 christos /* We are not creating an executable, which means that we are 6834 1.1 christos not creating a program header, and that the actual order of 6835 1.1 christos the sections in the file is unimportant. */ 6836 1.1 christos for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) 6837 1.1 christos { 6838 1.1 christos Elf_Internal_Shdr *hdr; 6839 1.1 christos 6840 1.1 christos hdr = *hdrpp; 6841 1.10 christos if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) 6842 1.10 christos && hdr->bfd_section == NULL) 6843 1.13 christos /* Do not assign offsets for these sections yet: we don't know 6844 1.13 christos their sizes. */ 6845 1.13 christos || (abfd->is_linker_output 6846 1.13 christos && hdr->bfd_section != NULL 6847 1.3 christos && (hdr->sh_name == -1u 6848 1.3 christos || bfd_section_is_ctf (hdr->bfd_section))) 6849 1.3 christos || i == elf_onesymtab (abfd) 6850 1.3 christos || (elf_symtab_shndx_list (abfd) != NULL 6851 1.3 christos && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) 6852 1.1 christos || i == elf_strtab_sec (abfd) 6853 1.1 christos || i == elf_shstrtab_sec (abfd)) 6854 1.1 christos { 6855 1.1 christos hdr->sh_offset = -1; 6856 1.14 christos } 6857 1.14 christos else 6858 1.14 christos /* There shouldn't be a need to effect "capped" file alignment here, 6859 1.14 christos yet at least the Linux kernel's modpost utility was found to be 6860 1.14 christos unhappy without. While the issue was addressed there, let's be 6861 1.14 christos kind for at least the foreseeable future ... */ 6862 1.1 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, false, 6863 1.3 christos bed->s->log_file_align); 6864 1.3 christos } 6865 1.10 christos 6866 1.1 christos elf_next_file_pos (abfd) = off; 6867 1.1 christos elf_program_header_size (abfd) = 0; 6868 1.1 christos } 6869 1.1 christos else 6870 1.1 christos { 6871 1.1 christos /* Assign file positions for the loaded sections based on the 6872 1.12 christos assignment of sections to segments. */ 6873 1.1 christos if (!assign_file_positions_for_load_sections (abfd, link_info)) 6874 1.1 christos return false; 6875 1.1 christos 6876 1.12 christos /* And for non-load sections. */ 6877 1.10 christos if (!assign_file_positions_for_non_load_sections (abfd, link_info)) 6878 1.1 christos return false; 6879 1.10 christos } 6880 1.12 christos 6881 1.1 christos if (!(*bed->elf_backend_modify_headers) (abfd, link_info)) 6882 1.10 christos return false; 6883 1.10 christos 6884 1.10 christos /* Write out the program headers. */ 6885 1.10 christos alloc = i_ehdrp->e_phnum; 6886 1.12 christos if (alloc != 0) 6887 1.12 christos { 6888 1.13 christos if (link_info != NULL && ! link_info->no_warn_rwx_segments) 6889 1.13 christos { 6890 1.13 christos bool warned_tls = false; 6891 1.13 christos bool warned_rwx = false; 6892 1.13 christos 6893 1.13 christos /* Memory resident segments with non-zero size and RWX 6894 1.12 christos permissions are a security risk, so we generate a warning 6895 1.12 christos here if we are creating any. */ 6896 1.12 christos unsigned int i; 6897 1.12 christos 6898 1.12 christos for (i = 0; i < alloc; i++) 6899 1.12 christos { 6900 1.12 christos const Elf_Internal_Phdr * phdr = tdata->phdr + i; 6901 1.12 christos 6902 1.12 christos if (phdr->p_memsz == 0) 6903 1.13 christos continue; 6904 1.13 christos 6905 1.13 christos if (! warned_tls 6906 1.13 christos && phdr->p_type == PT_TLS 6907 1.13 christos && (phdr->p_flags & PF_X)) 6908 1.13 christos { 6909 1.13 christos if (link_info->warn_is_error_for_rwx_segments) 6910 1.13 christos { 6911 1.13 christos _bfd_error_handler (_("\ 6912 1.13 christos error: %pB has a TLS segment with execute permission"), 6913 1.13 christos abfd); 6914 1.13 christos return false; 6915 1.13 christos } 6916 1.13 christos 6917 1.13 christos _bfd_error_handler (_("\ 6918 1.13 christos warning: %pB has a TLS segment with execute permission"), 6919 1.13 christos abfd); 6920 1.13 christos if (warned_rwx) 6921 1.13 christos break; 6922 1.13 christos 6923 1.13 christos warned_tls = true; 6924 1.13 christos } 6925 1.13 christos else if (! warned_rwx 6926 1.13 christos && phdr->p_type == PT_LOAD 6927 1.13 christos && ((phdr->p_flags & (PF_R | PF_W | PF_X)) 6928 1.13 christos == (PF_R | PF_W | PF_X))) 6929 1.13 christos { 6930 1.13 christos if (link_info->warn_is_error_for_rwx_segments) 6931 1.13 christos { 6932 1.13 christos _bfd_error_handler (_("\ 6933 1.13 christos error: %pB has a LOAD segment with RWX permissions"), 6934 1.13 christos abfd); 6935 1.13 christos return false; 6936 1.13 christos } 6937 1.13 christos 6938 1.13 christos _bfd_error_handler (_("\ 6939 1.13 christos warning: %pB has a LOAD segment with RWX permissions"), 6940 1.13 christos abfd); 6941 1.13 christos if (warned_tls) 6942 1.13 christos break; 6943 1.13 christos 6944 1.12 christos warned_rwx = true; 6945 1.12 christos } 6946 1.13 christos } 6947 1.10 christos } 6948 1.1 christos 6949 1.12 christos if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0 6950 1.1 christos || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) 6951 1.1 christos return false; 6952 1.12 christos } 6953 1.1 christos 6954 1.1 christos return true; 6955 1.12 christos } 6956 1.10 christos 6957 1.10 christos bool 6958 1.1 christos _bfd_elf_init_file_header (bfd *abfd, 6959 1.1 christos struct bfd_link_info *info ATTRIBUTE_UNUSED) 6960 1.1 christos { 6961 1.1 christos Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ 6962 1.1 christos struct elf_strtab_hash *shstrtab; 6963 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 6964 1.1 christos 6965 1.1 christos i_ehdrp = elf_elfheader (abfd); 6966 1.1 christos 6967 1.12 christos shstrtab = _bfd_elf_strtab_init (); 6968 1.1 christos if (shstrtab == NULL) 6969 1.1 christos return false; 6970 1.1 christos 6971 1.1 christos elf_shstrtab (abfd) = shstrtab; 6972 1.1 christos 6973 1.1 christos i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; 6974 1.1 christos i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; 6975 1.1 christos i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; 6976 1.1 christos i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; 6977 1.1 christos 6978 1.1 christos i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; 6979 1.1 christos i_ehdrp->e_ident[EI_DATA] = 6980 1.1 christos bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; 6981 1.1 christos i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; 6982 1.1 christos 6983 1.1 christos if ((abfd->flags & DYNAMIC) != 0) 6984 1.1 christos i_ehdrp->e_type = ET_DYN; 6985 1.1 christos else if ((abfd->flags & EXEC_P) != 0) 6986 1.1 christos i_ehdrp->e_type = ET_EXEC; 6987 1.1 christos else if (bfd_get_format (abfd) == bfd_core) 6988 1.1 christos i_ehdrp->e_type = ET_CORE; 6989 1.1 christos else 6990 1.1 christos i_ehdrp->e_type = ET_REL; 6991 1.1 christos 6992 1.1 christos switch (bfd_get_arch (abfd)) 6993 1.1 christos { 6994 1.1 christos case bfd_arch_unknown: 6995 1.1 christos i_ehdrp->e_machine = EM_NONE; 6996 1.1 christos break; 6997 1.1 christos 6998 1.1 christos /* There used to be a long list of cases here, each one setting 6999 1.1 christos e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE 7000 1.1 christos in the corresponding bfd definition. To avoid duplication, 7001 1.1 christos the switch was removed. Machines that need special handling 7002 1.1 christos can generally do it in elf_backend_final_write_processing(), 7003 1.1 christos unless they need the information earlier than the final write. 7004 1.1 christos Such need can generally be supplied by replacing the tests for 7005 1.1 christos e_machine with the conditions used to determine it. */ 7006 1.1 christos default: 7007 1.1 christos i_ehdrp->e_machine = bed->elf_machine_code; 7008 1.1 christos } 7009 1.1 christos 7010 1.1 christos i_ehdrp->e_version = bed->s->ev_current; 7011 1.1 christos i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; 7012 1.1 christos 7013 1.1 christos /* No program header, for now. */ 7014 1.1 christos i_ehdrp->e_phoff = 0; 7015 1.1 christos i_ehdrp->e_phentsize = 0; 7016 1.1 christos i_ehdrp->e_phnum = 0; 7017 1.1 christos 7018 1.1 christos /* Each bfd section is section header entry. */ 7019 1.1 christos i_ehdrp->e_entry = bfd_get_start_address (abfd); 7020 1.1 christos i_ehdrp->e_shentsize = bed->s->sizeof_shdr; 7021 1.12 christos 7022 1.1 christos elf_tdata (abfd)->symtab_hdr.sh_name = 7023 1.12 christos (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false); 7024 1.1 christos elf_tdata (abfd)->strtab_hdr.sh_name = 7025 1.12 christos (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false); 7026 1.1 christos elf_tdata (abfd)->shstrtab_hdr.sh_name = 7027 1.3 christos (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false); 7028 1.1 christos if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 7029 1.12 christos || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 7030 1.1 christos || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) 7031 1.12 christos return false; 7032 1.1 christos 7033 1.1 christos return true; 7034 1.10 christos } 7035 1.10 christos 7036 1.10 christos /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. 7037 1.10 christos 7038 1.10 christos FIXME: We used to have code here to sort the PT_LOAD segments into 7039 1.10 christos ascending order, as per the ELF spec. But this breaks some programs, 7040 1.10 christos including the Linux kernel. But really either the spec should be 7041 1.12 christos changed or the programs updated. */ 7042 1.10 christos 7043 1.10 christos bool 7044 1.10 christos _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info) 7045 1.10 christos { 7046 1.10 christos if (link_info != NULL && bfd_link_pie (link_info)) 7047 1.10 christos { 7048 1.10 christos Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd); 7049 1.10 christos unsigned int num_segments = i_ehdrp->e_phnum; 7050 1.10 christos struct elf_obj_tdata *tdata = elf_tdata (obfd); 7051 1.10 christos Elf_Internal_Phdr *segment = tdata->phdr; 7052 1.10 christos Elf_Internal_Phdr *end_segment = &segment[num_segments]; 7053 1.10 christos 7054 1.10 christos /* Find the lowest p_vaddr in PT_LOAD segments. */ 7055 1.10 christos bfd_vma p_vaddr = (bfd_vma) -1; 7056 1.10 christos for (; segment < end_segment; segment++) 7057 1.10 christos if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) 7058 1.10 christos p_vaddr = segment->p_vaddr; 7059 1.10 christos 7060 1.10 christos /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD 7061 1.10 christos segments is non-zero. */ 7062 1.10 christos if (p_vaddr) 7063 1.12 christos i_ehdrp->e_type = ET_EXEC; 7064 1.10 christos } 7065 1.10 christos return true; 7066 1.1 christos } 7067 1.3 christos 7068 1.1 christos /* Assign file positions for all the reloc sections which are not part 7069 1.12 christos of the loadable file image, and the file position of section headers. */ 7070 1.3 christos 7071 1.1 christos static bool 7072 1.1 christos _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) 7073 1.3 christos { 7074 1.3 christos file_ptr off; 7075 1.3 christos Elf_Internal_Shdr **shdrpp, **end_shdrpp; 7076 1.14 christos Elf_Internal_Shdr *shdrp; 7077 1.1 christos Elf_Internal_Ehdr *i_ehdrp; 7078 1.13 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 7079 1.13 christos 7080 1.13 christos /* Skip non-load sections without section header. */ 7081 1.13 christos if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0) 7082 1.3 christos return true; 7083 1.1 christos 7084 1.3 christos off = elf_next_file_pos (abfd); 7085 1.3 christos 7086 1.3 christos shdrpp = elf_elfsections (abfd); 7087 1.1 christos end_shdrpp = shdrpp + elf_numsections (abfd); 7088 1.3 christos for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) 7089 1.3 christos { 7090 1.3 christos shdrp = *shdrpp; 7091 1.3 christos if (shdrp->sh_offset == -1) 7092 1.13 christos { 7093 1.13 christos asection *sec = shdrp->bfd_section; 7094 1.13 christos if (sec == NULL 7095 1.13 christos || shdrp->sh_type == SHT_REL 7096 1.13 christos || shdrp->sh_type == SHT_RELA) 7097 1.13 christos ; 7098 1.13 christos else if (bfd_section_is_ctf (sec)) 7099 1.13 christos { 7100 1.13 christos /* Update section size and contents. */ 7101 1.13 christos shdrp->sh_size = sec->size; 7102 1.13 christos shdrp->contents = sec->contents; 7103 1.13 christos } 7104 1.13 christos else if (shdrp->sh_name == -1u) 7105 1.13 christos { 7106 1.13 christos const char *name = sec->name; 7107 1.13 christos struct bfd_elf_section_data *d; 7108 1.13 christos 7109 1.13 christos /* Compress DWARF debug sections. */ 7110 1.1 christos if (!bfd_compress_section (abfd, sec, shdrp->contents)) 7111 1.13 christos return false; 7112 1.13 christos 7113 1.13 christos if (sec->compress_status == COMPRESS_SECTION_DONE 7114 1.13 christos && (abfd->flags & BFD_COMPRESS_GABI) == 0 7115 1.13 christos && name[1] == 'd') 7116 1.13 christos { 7117 1.13 christos /* If section is compressed with zlib-gnu, convert 7118 1.13 christos section name from .debug_* to .zdebug_*. */ 7119 1.12 christos char *new_name = bfd_debug_name_to_zdebug (abfd, name); 7120 1.13 christos if (new_name == NULL) 7121 1.10 christos return false; 7122 1.13 christos name = new_name; 7123 1.13 christos } 7124 1.13 christos /* Add section name to section name section. */ 7125 1.13 christos shdrp->sh_name 7126 1.13 christos = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 7127 1.13 christos name, false); 7128 1.13 christos d = elf_section_data (sec); 7129 1.13 christos 7130 1.13 christos /* Add reloc section name to section name section. */ 7131 1.13 christos if (d->rel.hdr 7132 1.13 christos && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr, 7133 1.13 christos name, false)) 7134 1.13 christos return false; 7135 1.13 christos if (d->rela.hdr 7136 1.13 christos && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr, 7137 1.10 christos name, true)) 7138 1.13 christos return false; 7139 1.13 christos 7140 1.13 christos /* Update section size and contents. */ 7141 1.13 christos shdrp->sh_size = sec->size; 7142 1.3 christos shdrp->contents = sec->contents; 7143 1.13 christos sec->contents = NULL; 7144 1.14 christos } 7145 1.14 christos 7146 1.14 christos off = _bfd_elf_assign_file_position_for_section (shdrp, off, 7147 1.14 christos (abfd->flags & (EXEC_P | DYNAMIC)) 7148 1.3 christos || bfd_get_format (abfd) == bfd_core, 7149 1.1 christos bed->s->log_file_align); 7150 1.1 christos } 7151 1.3 christos } 7152 1.3 christos 7153 1.3 christos /* Place section name section after DWARF debug sections have been 7154 1.3 christos compressed. */ 7155 1.3 christos _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); 7156 1.14 christos shdrp = &elf_tdata (abfd)->shstrtab_hdr; 7157 1.3 christos shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); 7158 1.3 christos off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0); 7159 1.3 christos 7160 1.14 christos /* Place the section headers. */ 7161 1.3 christos i_ehdrp = elf_elfheader (abfd); 7162 1.3 christos off = BFD_ALIGN (off, 1u << bed->s->log_file_align); 7163 1.3 christos i_ehdrp->e_shoff = off; 7164 1.3 christos off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; 7165 1.12 christos elf_next_file_pos (abfd) = off; 7166 1.1 christos 7167 1.1 christos return true; 7168 1.12 christos } 7169 1.1 christos 7170 1.1 christos bool 7171 1.1 christos _bfd_elf_write_object_contents (bfd *abfd) 7172 1.1 christos { 7173 1.12 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 7174 1.1 christos Elf_Internal_Shdr **i_shdrp; 7175 1.3 christos bool failed; 7176 1.1 christos unsigned int count, num_sec; 7177 1.1 christos struct elf_obj_tdata *t; 7178 1.1 christos 7179 1.12 christos if (! abfd->output_has_begun 7180 1.10 christos && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) 7181 1.13 christos return false; 7182 1.13 christos /* Do not rewrite ELF data when the BFD has been opened for update. 7183 1.13 christos abfd->output_has_begun was set to TRUE on opening, so creation of 7184 1.13 christos new sections, and modification of existing section sizes was 7185 1.13 christos restricted. This means the ELF header, program headers and 7186 1.13 christos section headers can't have changed. If the contents of any 7187 1.10 christos sections has been modified, then those changes have already been 7188 1.10 christos written to the BFD. */ 7189 1.10 christos else if (abfd->direction == both_direction) 7190 1.12 christos { 7191 1.10 christos BFD_ASSERT (abfd->output_has_begun); 7192 1.1 christos return true; 7193 1.1 christos } 7194 1.1 christos 7195 1.12 christos i_shdrp = elf_elfsections (abfd); 7196 1.1 christos 7197 1.1 christos failed = false; 7198 1.12 christos bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); 7199 1.1 christos if (failed) 7200 1.3 christos return false; 7201 1.12 christos 7202 1.1 christos if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) 7203 1.1 christos return false; 7204 1.1 christos 7205 1.1 christos /* After writing the headers, we need to write the sections too... */ 7206 1.1 christos num_sec = elf_numsections (abfd); 7207 1.13 christos for (count = 1; count < num_sec; count++) 7208 1.13 christos { 7209 1.13 christos /* Don't set the sh_name field without section header. */ 7210 1.13 christos if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0) 7211 1.13 christos i_shdrp[count]->sh_name 7212 1.1 christos = _bfd_elf_strtab_offset (elf_shstrtab (abfd), 7213 1.9 christos i_shdrp[count]->sh_name); 7214 1.12 christos if (bed->elf_backend_section_processing) 7215 1.1 christos if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count])) 7216 1.1 christos return false; 7217 1.1 christos if (i_shdrp[count]->contents) 7218 1.1 christos { 7219 1.1 christos bfd_size_type amt = i_shdrp[count]->sh_size; 7220 1.13 christos 7221 1.12 christos if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 7222 1.1 christos || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt) 7223 1.1 christos return false; 7224 1.1 christos } 7225 1.1 christos } 7226 1.3 christos 7227 1.1 christos /* Write out the section header names. */ 7228 1.13 christos t = elf_tdata (abfd); 7229 1.3 christos if (elf_shstrtab (abfd) != NULL 7230 1.1 christos && t->shstrtab_hdr.sh_offset != -1 7231 1.12 christos && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 7232 1.1 christos || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) 7233 1.10 christos return false; 7234 1.12 christos 7235 1.1 christos if (!(*bed->elf_backend_final_write_processing) (abfd)) 7236 1.1 christos return false; 7237 1.12 christos 7238 1.1 christos if (!bed->s->write_shdrs_and_ehdr (abfd)) 7239 1.1 christos return false; 7240 1.12 christos 7241 1.12 christos /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ 7242 1.12 christos if (t->o->build_id.after_write_object_contents != NULL 7243 1.12 christos && !(*t->o->build_id.after_write_object_contents) (abfd)) 7244 1.12 christos return false; 7245 1.12 christos if (t->o->package_metadata.after_write_object_contents != NULL 7246 1.1 christos && !(*t->o->package_metadata.after_write_object_contents) (abfd)) 7247 1.12 christos return false; 7248 1.1 christos 7249 1.1 christos return true; 7250 1.12 christos } 7251 1.1 christos 7252 1.1 christos bool 7253 1.1 christos _bfd_elf_write_corefile_contents (bfd *abfd) 7254 1.1 christos { 7255 1.1 christos /* Hopefully this can be done just like an object file. */ 7256 1.1 christos return _bfd_elf_write_object_contents (abfd); 7257 1.1 christos } 7258 1.1 christos 7259 1.1 christos /* Given a section, search the header to find them. */ 7260 1.1 christos 7261 1.1 christos unsigned int 7262 1.1 christos _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) 7263 1.1 christos { 7264 1.1 christos const struct elf_backend_data *bed; 7265 1.1 christos unsigned int sec_index; 7266 1.1 christos 7267 1.1 christos if (elf_section_data (asect) != NULL 7268 1.1 christos && elf_section_data (asect)->this_idx != 0) 7269 1.1 christos return elf_section_data (asect)->this_idx; 7270 1.1 christos 7271 1.1 christos if (bfd_is_abs_section (asect)) 7272 1.1 christos sec_index = SHN_ABS; 7273 1.1 christos else if (bfd_is_com_section (asect)) 7274 1.1 christos sec_index = SHN_COMMON; 7275 1.1 christos else if (bfd_is_und_section (asect)) 7276 1.1 christos sec_index = SHN_UNDEF; 7277 1.1 christos else 7278 1.1 christos sec_index = SHN_BAD; 7279 1.1 christos 7280 1.1 christos bed = get_elf_backend_data (abfd); 7281 1.1 christos if (bed->elf_backend_section_from_bfd_section) 7282 1.1 christos { 7283 1.1 christos int retval = sec_index; 7284 1.1 christos 7285 1.1 christos if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) 7286 1.1 christos return retval; 7287 1.1 christos } 7288 1.1 christos 7289 1.1 christos if (sec_index == SHN_BAD) 7290 1.1 christos bfd_set_error (bfd_error_nonrepresentable_section); 7291 1.1 christos 7292 1.1 christos return sec_index; 7293 1.1 christos } 7294 1.1 christos 7295 1.1 christos /* Given a BFD symbol, return the index in the ELF symbol table, or -1 7296 1.1 christos on error. */ 7297 1.1 christos 7298 1.1 christos int 7299 1.1 christos _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) 7300 1.1 christos { 7301 1.1 christos asymbol *asym_ptr = *asym_ptr_ptr; 7302 1.1 christos int idx; 7303 1.1 christos flagword flags = asym_ptr->flags; 7304 1.1 christos 7305 1.1 christos /* When gas creates relocations against local labels, it creates its 7306 1.1 christos own symbol for the section, but does put the symbol into the 7307 1.1 christos symbol chain, so udata is 0. When the linker is generating 7308 1.1 christos relocatable output, this section symbol may be for one of the 7309 1.1 christos input sections rather than the output section. */ 7310 1.1 christos if (asym_ptr->udata.i == 0 7311 1.1 christos && (flags & BSF_SECTION_SYM) 7312 1.1 christos && asym_ptr->section) 7313 1.1 christos { 7314 1.1 christos asection *sec; 7315 1.1 christos 7316 1.1 christos sec = asym_ptr->section; 7317 1.1 christos if (sec->owner != abfd && sec->output_section != NULL) 7318 1.12 christos sec = sec->output_section; 7319 1.12 christos if (sec->owner == abfd 7320 1.12 christos && sec->index < elf_num_section_syms (abfd) 7321 1.1 christos && elf_section_syms (abfd)[sec->index] != NULL) 7322 1.1 christos asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i; 7323 1.1 christos } 7324 1.1 christos 7325 1.1 christos idx = asym_ptr->udata.i; 7326 1.1 christos 7327 1.1 christos if (idx == 0) 7328 1.1 christos { 7329 1.9 christos /* This case can occur when using --strip-symbol on a symbol 7330 1.9 christos which is used in a relocation entry. */ 7331 1.9 christos _bfd_error_handler 7332 1.1 christos /* xgettext:c-format */ 7333 1.1 christos (_("%pB: symbol `%s' required but not present"), 7334 1.1 christos abfd, bfd_asymbol_name (asym_ptr)); 7335 1.1 christos bfd_set_error (bfd_error_no_symbols); 7336 1.1 christos return -1; 7337 1.1 christos } 7338 1.1 christos 7339 1.1 christos #if DEBUG & 4 7340 1.13 christos { 7341 1.13 christos fprintf (stderr, 7342 1.9 christos "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d," 7343 1.1 christos " flags = 0x%.8x\n", 7344 1.1 christos (long) asym_ptr, asym_ptr->name, idx, flags); 7345 1.1 christos fflush (stderr); 7346 1.1 christos } 7347 1.1 christos #endif 7348 1.1 christos 7349 1.1 christos return idx; 7350 1.13 christos } 7351 1.13 christos 7352 1.13 christos static inline bfd_vma 7353 1.13 christos segment_size (Elf_Internal_Phdr *segment) 7354 1.13 christos { 7355 1.13 christos return (segment->p_memsz > segment->p_filesz 7356 1.13 christos ? segment->p_memsz : segment->p_filesz); 7357 1.13 christos } 7358 1.13 christos 7359 1.13 christos 7360 1.13 christos /* Returns the end address of the segment + 1. */ 7361 1.13 christos static inline bfd_vma 7362 1.13 christos segment_end (Elf_Internal_Phdr *segment, bfd_vma start) 7363 1.13 christos { 7364 1.13 christos return start + segment_size (segment); 7365 1.13 christos } 7366 1.13 christos 7367 1.13 christos static inline bfd_size_type 7368 1.13 christos section_size (asection *section, Elf_Internal_Phdr *segment) 7369 1.13 christos { 7370 1.13 christos if ((section->flags & SEC_HAS_CONTENTS) != 0 7371 1.13 christos || (section->flags & SEC_THREAD_LOCAL) == 0 7372 1.13 christos || segment->p_type == PT_TLS) 7373 1.13 christos return section->size; 7374 1.13 christos return 0; 7375 1.13 christos } 7376 1.13 christos 7377 1.13 christos /* Returns TRUE if the given section is contained within the given 7378 1.13 christos segment. LMA addresses are compared against PADDR when 7379 1.13 christos USE_VADDR is false, VMA against VADDR when true. */ 7380 1.13 christos static bool 7381 1.13 christos is_contained_by (asection *section, Elf_Internal_Phdr *segment, 7382 1.13 christos bfd_vma paddr, bfd_vma vaddr, unsigned int opb, 7383 1.13 christos bool use_vaddr) 7384 1.13 christos { 7385 1.13 christos bfd_vma seg_addr = !use_vaddr ? paddr : vaddr; 7386 1.13 christos bfd_vma addr = !use_vaddr ? section->lma : section->vma; 7387 1.13 christos bfd_vma octet; 7388 1.13 christos if (_bfd_mul_overflow (addr, opb, &octet)) 7389 1.13 christos return false; 7390 1.13 christos /* The third and fourth lines below are testing that the section end 7391 1.13 christos address is within the segment. It's written this way to avoid 7392 1.13 christos overflow. Add seg_addr + section_size to both sides of the 7393 1.13 christos inequality to make it obvious. */ 7394 1.13 christos return (octet >= seg_addr 7395 1.13 christos && segment_size (segment) >= section_size (section, segment) 7396 1.13 christos && (octet - seg_addr 7397 1.13 christos <= segment_size (segment) - section_size (section, segment))); 7398 1.13 christos } 7399 1.13 christos 7400 1.13 christos /* Handle PT_NOTE segment. */ 7401 1.13 christos static bool 7402 1.13 christos is_note (asection *s, Elf_Internal_Phdr *p) 7403 1.13 christos { 7404 1.13 christos return (p->p_type == PT_NOTE 7405 1.13 christos && elf_section_type (s) == SHT_NOTE 7406 1.13 christos && (ufile_ptr) s->filepos >= p->p_offset 7407 1.13 christos && p->p_filesz >= s->size 7408 1.13 christos && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size); 7409 1.1 christos } 7410 1.1 christos 7411 1.12 christos /* Rewrite program header information. */ 7412 1.12 christos 7413 1.1 christos static bool 7414 1.1 christos rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize) 7415 1.1 christos { 7416 1.1 christos Elf_Internal_Ehdr *iehdr; 7417 1.1 christos struct elf_segment_map *map; 7418 1.1 christos struct elf_segment_map *map_first; 7419 1.1 christos struct elf_segment_map **pointer_to_map; 7420 1.1 christos Elf_Internal_Phdr *segment; 7421 1.1 christos asection *section; 7422 1.12 christos unsigned int i; 7423 1.12 christos unsigned int num_segments; 7424 1.1 christos bool phdr_included = false; 7425 1.1 christos bool p_paddr_valid; 7426 1.1 christos struct elf_segment_map *phdr_adjust_seg = NULL; 7427 1.12 christos unsigned int phdr_adjust_num = 0; 7428 1.1 christos const struct elf_backend_data *bed; 7429 1.1 christos unsigned int opb = bfd_octets_per_byte (ibfd, NULL); 7430 1.1 christos 7431 1.1 christos bed = get_elf_backend_data (ibfd); 7432 1.1 christos iehdr = elf_elfheader (ibfd); 7433 1.1 christos 7434 1.1 christos map_first = NULL; 7435 1.1 christos pointer_to_map = &map_first; 7436 1.1 christos 7437 1.1 christos num_segments = elf_elfheader (ibfd)->e_phnum; 7438 1.1 christos 7439 1.1 christos /* The complicated case when p_vaddr is 0 is to handle the Solaris 7440 1.1 christos linker, which generates a PT_INTERP section with p_vaddr and 7441 1.1 christos p_memsz set to 0. */ 7442 1.1 christos #define IS_SOLARIS_PT_INTERP(p, s) \ 7443 1.1 christos (p->p_vaddr == 0 \ 7444 1.1 christos && p->p_paddr == 0 \ 7445 1.1 christos && p->p_memsz == 0 \ 7446 1.1 christos && p->p_filesz > 0 \ 7447 1.1 christos && (s->flags & SEC_HAS_CONTENTS) != 0 \ 7448 1.1 christos && s->size > 0 \ 7449 1.1 christos && (bfd_vma) s->filepos >= p->p_offset \ 7450 1.1 christos && ((bfd_vma) s->filepos + s->size \ 7451 1.1 christos <= p->p_offset + p->p_filesz)) 7452 1.1 christos 7453 1.1 christos /* Decide if the given section should be included in the given segment. 7454 1.1 christos A section will be included if: 7455 1.1 christos 1. It is within the address space of the segment -- we use the LMA 7456 1.1 christos if that is set for the segment and the VMA otherwise, 7457 1.1 christos 2. It is an allocated section or a NOTE section in a PT_NOTE 7458 1.1 christos segment. 7459 1.1 christos 3. There is an output section associated with it, 7460 1.1 christos 4. The section has not already been allocated to a previous segment. 7461 1.1 christos 5. PT_GNU_STACK segments do not include any sections. 7462 1.1 christos 6. PT_TLS segment includes only SHF_TLS sections. 7463 1.1 christos 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. 7464 1.14 christos 8. PT_DYNAMIC should not contain empty sections at the beginning 7465 1.13 christos (with the possible exception of .dynamic). */ 7466 1.14 christos #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \ 7467 1.1 christos (((is_contained_by (section, segment, segment->p_paddr, \ 7468 1.13 christos segment->p_vaddr, opb, !paddr_valid) \ 7469 1.1 christos && (section->flags & SEC_ALLOC) != 0) \ 7470 1.1 christos || is_note (section, segment)) \ 7471 1.1 christos && segment->p_type != PT_GNU_STACK \ 7472 1.1 christos && (segment->p_type != PT_TLS \ 7473 1.1 christos || (section->flags & SEC_THREAD_LOCAL)) \ 7474 1.1 christos && (segment->p_type == PT_LOAD \ 7475 1.1 christos || segment->p_type == PT_TLS \ 7476 1.13 christos || (section->flags & SEC_THREAD_LOCAL) == 0) \ 7477 1.1 christos && (segment->p_type != PT_DYNAMIC \ 7478 1.12 christos || section_size (section, segment) > 0 \ 7479 1.12 christos || (segment->p_paddr \ 7480 1.10 christos ? segment->p_paddr != section->lma * (opb) \ 7481 1.9 christos : segment->p_vaddr != section->vma * (opb)) \ 7482 1.1 christos || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \ 7483 1.1 christos && (segment->p_type != PT_LOAD || !section->segment_mark)) 7484 1.1 christos 7485 1.14 christos /* If the output section of a section in the input segment is NULL, 7486 1.14 christos it is removed from the corresponding output segment. */ 7487 1.1 christos #define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid) \ 7488 1.1 christos (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \ 7489 1.1 christos && section->output_section != NULL) 7490 1.1 christos 7491 1.13 christos /* Returns TRUE iff seg1 starts after the end of seg2. */ 7492 1.1 christos #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ 7493 1.1 christos (seg1->field >= segment_end (seg2, seg2->field)) 7494 1.1 christos 7495 1.1 christos /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both 7496 1.1 christos their VMA address ranges and their LMA address ranges overlap. 7497 1.1 christos It is possible to have overlapping VMA ranges without overlapping LMA 7498 1.1 christos ranges. RedBoot images for example can have both .data and .bss mapped 7499 1.1 christos to the same VMA range, but with the .data section mapped to a different 7500 1.1 christos LMA. */ 7501 1.1 christos #define SEGMENT_OVERLAPS(seg1, seg2) \ 7502 1.1 christos ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ 7503 1.1 christos || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ 7504 1.1 christos && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ 7505 1.12 christos || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) 7506 1.1 christos 7507 1.12 christos /* Initialise the segment mark field, and discard stupid alignment. */ 7508 1.12 christos for (section = ibfd->sections; section != NULL; section = section->next) 7509 1.12 christos { 7510 1.12 christos asection *o = section->output_section; 7511 1.12 christos if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1) 7512 1.12 christos o->alignment_power = 0; 7513 1.1 christos section->segment_mark = false; 7514 1.1 christos } 7515 1.1 christos 7516 1.1 christos /* The Solaris linker creates program headers in which all the 7517 1.1 christos p_paddr fields are zero. When we try to objcopy or strip such a 7518 1.12 christos file, we get confused. Check for this case, and if we find it 7519 1.1 christos don't set the p_paddr_valid fields. */ 7520 1.1 christos p_paddr_valid = false; 7521 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr; 7522 1.1 christos i < num_segments; 7523 1.1 christos i++, segment++) 7524 1.12 christos if (segment->p_paddr != 0) 7525 1.1 christos { 7526 1.1 christos p_paddr_valid = true; 7527 1.1 christos break; 7528 1.1 christos } 7529 1.1 christos 7530 1.1 christos /* Scan through the segments specified in the program header 7531 1.1 christos of the input BFD. For this first scan we look for overlaps 7532 1.1 christos in the loadable segments. These can be created by weird 7533 1.1 christos parameters to objcopy. Also, fix some solaris weirdness. */ 7534 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr; 7535 1.1 christos i < num_segments; 7536 1.1 christos i++, segment++) 7537 1.1 christos { 7538 1.1 christos unsigned int j; 7539 1.1 christos Elf_Internal_Phdr *segment2; 7540 1.1 christos 7541 1.1 christos if (segment->p_type == PT_INTERP) 7542 1.1 christos for (section = ibfd->sections; section; section = section->next) 7543 1.1 christos if (IS_SOLARIS_PT_INTERP (segment, section)) 7544 1.1 christos { 7545 1.12 christos /* Mininal change so that the normal section to segment 7546 1.1 christos assignment code will work. */ 7547 1.1 christos segment->p_vaddr = section->vma * opb; 7548 1.1 christos break; 7549 1.1 christos } 7550 1.1 christos 7551 1.1 christos if (segment->p_type != PT_LOAD) 7552 1.1 christos { 7553 1.1 christos /* Remove PT_GNU_RELRO segment. */ 7554 1.1 christos if (segment->p_type == PT_GNU_RELRO) 7555 1.1 christos segment->p_type = PT_NULL; 7556 1.1 christos continue; 7557 1.1 christos } 7558 1.1 christos 7559 1.1 christos /* Determine if this segment overlaps any previous segments. */ 7560 1.1 christos for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) 7561 1.1 christos { 7562 1.1 christos bfd_signed_vma extra_length; 7563 1.1 christos 7564 1.1 christos if (segment2->p_type != PT_LOAD 7565 1.1 christos || !SEGMENT_OVERLAPS (segment, segment2)) 7566 1.1 christos continue; 7567 1.1 christos 7568 1.1 christos /* Merge the two segments together. */ 7569 1.1 christos if (segment2->p_vaddr < segment->p_vaddr) 7570 1.1 christos { 7571 1.13 christos /* Extend SEGMENT2 to include SEGMENT and then delete 7572 1.13 christos SEGMENT. */ 7573 1.1 christos extra_length = (segment_end (segment, segment->p_vaddr) 7574 1.1 christos - segment_end (segment2, segment2->p_vaddr)); 7575 1.1 christos 7576 1.1 christos if (extra_length > 0) 7577 1.1 christos { 7578 1.1 christos segment2->p_memsz += extra_length; 7579 1.1 christos segment2->p_filesz += extra_length; 7580 1.1 christos } 7581 1.1 christos 7582 1.1 christos segment->p_type = PT_NULL; 7583 1.1 christos 7584 1.1 christos /* Since we have deleted P we must restart the outer loop. */ 7585 1.1 christos i = 0; 7586 1.1 christos segment = elf_tdata (ibfd)->phdr; 7587 1.1 christos break; 7588 1.1 christos } 7589 1.1 christos else 7590 1.1 christos { 7591 1.13 christos /* Extend SEGMENT to include SEGMENT2 and then delete 7592 1.13 christos SEGMENT2. */ 7593 1.1 christos extra_length = (segment_end (segment2, segment2->p_vaddr) 7594 1.1 christos - segment_end (segment, segment->p_vaddr)); 7595 1.1 christos 7596 1.1 christos if (extra_length > 0) 7597 1.1 christos { 7598 1.1 christos segment->p_memsz += extra_length; 7599 1.1 christos segment->p_filesz += extra_length; 7600 1.1 christos } 7601 1.1 christos 7602 1.1 christos segment2->p_type = PT_NULL; 7603 1.1 christos } 7604 1.1 christos } 7605 1.1 christos } 7606 1.1 christos 7607 1.1 christos /* The second scan attempts to assign sections to segments. */ 7608 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr; 7609 1.1 christos i < num_segments; 7610 1.1 christos i++, segment++) 7611 1.1 christos { 7612 1.1 christos unsigned int section_count; 7613 1.1 christos asection **sections; 7614 1.9 christos asection *output_section; 7615 1.9 christos unsigned int isec; 7616 1.1 christos asection *matching_lma; 7617 1.12 christos asection *suggested_lma; 7618 1.1 christos unsigned int j; 7619 1.1 christos size_t amt; 7620 1.1 christos asection *first_section; 7621 1.1 christos 7622 1.1 christos if (segment->p_type == PT_NULL) 7623 1.1 christos continue; 7624 1.1 christos 7625 1.1 christos first_section = NULL; 7626 1.1 christos /* Compute how many sections might be placed into this segment. */ 7627 1.1 christos for (section = ibfd->sections, section_count = 0; 7628 1.1 christos section != NULL; 7629 1.1 christos section = section->next) 7630 1.1 christos { 7631 1.14 christos /* Find the first section in the input segment, which may be 7632 1.1 christos removed from the corresponding output segment. */ 7633 1.1 christos if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid)) 7634 1.1 christos { 7635 1.1 christos if (first_section == NULL) 7636 1.1 christos first_section = section; 7637 1.1 christos if (section->output_section != NULL) 7638 1.1 christos ++section_count; 7639 1.1 christos } 7640 1.1 christos } 7641 1.1 christos 7642 1.10 christos /* Allocate a segment map big enough to contain 7643 1.12 christos all of the sections we have selected. */ 7644 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *); 7645 1.1 christos amt += section_count * sizeof (asection *); 7646 1.12 christos map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); 7647 1.1 christos if (map == NULL) 7648 1.1 christos return false; 7649 1.1 christos 7650 1.1 christos /* Initialise the fields of the segment map. Default to 7651 1.1 christos using the physical address of the segment in the input BFD. */ 7652 1.1 christos map->next = NULL; 7653 1.1 christos map->p_type = segment->p_type; 7654 1.1 christos map->p_flags = segment->p_flags; 7655 1.12 christos map->p_flags_valid = 1; 7656 1.12 christos 7657 1.12 christos if (map->p_type == PT_LOAD 7658 1.12 christos && (ibfd->flags & D_PAGED) != 0 7659 1.12 christos && maxpagesize > 1 7660 1.12 christos && segment->p_align > 1) 7661 1.12 christos { 7662 1.12 christos map->p_align = segment->p_align; 7663 1.12 christos if (segment->p_align > maxpagesize) 7664 1.12 christos map->p_align = maxpagesize; 7665 1.12 christos map->p_align_valid = 1; 7666 1.1 christos } 7667 1.1 christos 7668 1.1 christos /* If the first section in the input segment is removed, there is 7669 1.1 christos no need to preserve segment physical address in the corresponding 7670 1.1 christos output segment. */ 7671 1.1 christos if (!first_section || first_section->output_section != NULL) 7672 1.1 christos { 7673 1.1 christos map->p_paddr = segment->p_paddr; 7674 1.1 christos map->p_paddr_valid = p_paddr_valid; 7675 1.1 christos } 7676 1.1 christos 7677 1.1 christos /* Determine if this segment contains the ELF file header 7678 1.1 christos and if it contains the program headers themselves. */ 7679 1.1 christos map->includes_filehdr = (segment->p_offset == 0 7680 1.1 christos && segment->p_filesz >= iehdr->e_ehsize); 7681 1.1 christos map->includes_phdrs = 0; 7682 1.1 christos 7683 1.1 christos if (!phdr_included || segment->p_type != PT_LOAD) 7684 1.1 christos { 7685 1.1 christos map->includes_phdrs = 7686 1.1 christos (segment->p_offset <= (bfd_vma) iehdr->e_phoff 7687 1.1 christos && (segment->p_offset + segment->p_filesz 7688 1.1 christos >= ((bfd_vma) iehdr->e_phoff 7689 1.1 christos + iehdr->e_phnum * iehdr->e_phentsize))); 7690 1.12 christos 7691 1.1 christos if (segment->p_type == PT_LOAD && map->includes_phdrs) 7692 1.1 christos phdr_included = true; 7693 1.1 christos } 7694 1.1 christos 7695 1.1 christos if (section_count == 0) 7696 1.1 christos { 7697 1.1 christos /* Special segments, such as the PT_PHDR segment, may contain 7698 1.9 christos no sections, but ordinary, loadable segments should contain 7699 1.13 christos something. They are allowed by the ELF spec however, so only 7700 1.9 christos a warning is produced. 7701 1.9 christos Don't warn if an empty PT_LOAD contains the program headers. 7702 1.9 christos There is however the valid use case of embedded systems which 7703 1.9 christos have segments with p_filesz of 0 and a p_memsz > 0 to initialize 7704 1.13 christos flash memory with zeros. No warning is shown for that case. */ 7705 1.9 christos if (segment->p_type == PT_LOAD 7706 1.9 christos && !map->includes_phdrs 7707 1.9 christos && (segment->p_filesz > 0 || segment->p_memsz == 0)) 7708 1.9 christos /* xgettext:c-format */ 7709 1.9 christos _bfd_error_handler 7710 1.9 christos (_("%pB: warning: empty loadable segment detected" 7711 1.1 christos " at vaddr=%#" PRIx64 ", is this intentional?"), 7712 1.12 christos ibfd, (uint64_t) segment->p_vaddr); 7713 1.1 christos 7714 1.1 christos map->p_vaddr_offset = segment->p_vaddr / opb; 7715 1.1 christos map->count = 0; 7716 1.1 christos *pointer_to_map = map; 7717 1.1 christos pointer_to_map = &map->next; 7718 1.1 christos 7719 1.1 christos continue; 7720 1.1 christos } 7721 1.1 christos 7722 1.1 christos /* Now scan the sections in the input BFD again and attempt 7723 1.1 christos to add their corresponding output sections to the segment map. 7724 1.1 christos The problem here is how to handle an output section which has 7725 1.1 christos been moved (ie had its LMA changed). There are four possibilities: 7726 1.1 christos 7727 1.1 christos 1. None of the sections have been moved. 7728 1.1 christos In this case we can continue to use the segment LMA from the 7729 1.1 christos input BFD. 7730 1.1 christos 7731 1.1 christos 2. All of the sections have been moved by the same amount. 7732 1.1 christos In this case we can change the segment's LMA to match the LMA 7733 1.1 christos of the first section. 7734 1.1 christos 7735 1.1 christos 3. Some of the sections have been moved, others have not. 7736 1.1 christos In this case those sections which have not been moved can be 7737 1.1 christos placed in the current segment which will have to have its size, 7738 1.1 christos and possibly its LMA changed, and a new segment or segments will 7739 1.1 christos have to be created to contain the other sections. 7740 1.1 christos 7741 1.1 christos 4. The sections have been moved, but not by the same amount. 7742 1.1 christos In this case we can change the segment's LMA to match the LMA 7743 1.1 christos of the first section and we will have to create a new segment 7744 1.1 christos or segments to contain the other sections. 7745 1.1 christos 7746 1.1 christos In order to save time, we allocate an array to hold the section 7747 1.1 christos pointers that we are interested in. As these sections get assigned 7748 1.12 christos to a segment, they are removed from this array. */ 7749 1.12 christos 7750 1.1 christos amt = section_count * sizeof (asection *); 7751 1.12 christos sections = (asection **) bfd_malloc (amt); 7752 1.1 christos if (sections == NULL) 7753 1.1 christos return false; 7754 1.1 christos 7755 1.1 christos /* Step One: Scan for segment vs section LMA conflicts. 7756 1.1 christos Also add the sections to the section array allocated above. 7757 1.1 christos Also add the sections to the current segment. In the common 7758 1.1 christos case, where the sections have not been moved, this means that 7759 1.1 christos we have completely filled the segment, and there is nothing 7760 1.9 christos more to do. */ 7761 1.9 christos isec = 0; 7762 1.1 christos matching_lma = NULL; 7763 1.5 christos suggested_lma = NULL; 7764 1.1 christos 7765 1.1 christos for (section = first_section, j = 0; 7766 1.1 christos section != NULL; 7767 1.14 christos section = section->next) 7768 1.1 christos { 7769 1.1 christos if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid)) 7770 1.1 christos { 7771 1.1 christos output_section = section->output_section; 7772 1.1 christos 7773 1.1 christos sections[j++] = section; 7774 1.1 christos 7775 1.1 christos /* The Solaris native linker always sets p_paddr to 0. 7776 1.1 christos We try to catch that case here, and set it to the 7777 1.1 christos correct value. Note - some backends require that 7778 1.1 christos p_paddr be left as zero. */ 7779 1.1 christos if (!p_paddr_valid 7780 1.1 christos && segment->p_vaddr != 0 7781 1.1 christos && !bed->want_p_paddr_set_to_zero 7782 1.9 christos && isec == 0 7783 1.9 christos && output_section->lma != 0 7784 1.9 christos && (align_power (segment->p_vaddr 7785 1.9 christos + (map->includes_filehdr 7786 1.9 christos ? iehdr->e_ehsize : 0) 7787 1.9 christos + (map->includes_phdrs 7788 1.12 christos ? iehdr->e_phnum * iehdr->e_phentsize 7789 1.12 christos : 0), 7790 1.1 christos output_section->alignment_power * opb) 7791 1.1 christos == (output_section->vma * opb))) 7792 1.1 christos map->p_paddr = segment->p_vaddr; 7793 1.1 christos 7794 1.13 christos /* Match up the physical address of the segment with the 7795 1.13 christos LMA address of the output section. */ 7796 1.13 christos if (is_contained_by (output_section, segment, map->p_paddr, 7797 1.1 christos 0, opb, false) 7798 1.9 christos || is_note (section, segment)) 7799 1.9 christos { 7800 1.9 christos if (matching_lma == NULL 7801 1.1 christos || output_section->lma < matching_lma->lma) 7802 1.1 christos matching_lma = output_section; 7803 1.1 christos 7804 1.1 christos /* We assume that if the section fits within the segment 7805 1.1 christos then it does not overlap any other section within that 7806 1.1 christos segment. */ 7807 1.9 christos map->sections[isec++] = output_section; 7808 1.9 christos } 7809 1.1 christos else if (suggested_lma == NULL) 7810 1.1 christos suggested_lma = output_section; 7811 1.1 christos 7812 1.1 christos if (j == section_count) 7813 1.1 christos break; 7814 1.1 christos } 7815 1.1 christos } 7816 1.1 christos 7817 1.1 christos BFD_ASSERT (j == section_count); 7818 1.1 christos 7819 1.1 christos /* Step Two: Adjust the physical address of the current segment, 7820 1.1 christos if necessary. */ 7821 1.1 christos if (isec == section_count) 7822 1.1 christos { 7823 1.1 christos /* All of the sections fitted within the segment as currently 7824 1.1 christos specified. This is the default case. Add the segment to 7825 1.1 christos the list of built segments and carry on to process the next 7826 1.1 christos program header in the input BFD. */ 7827 1.1 christos map->count = section_count; 7828 1.1 christos *pointer_to_map = map; 7829 1.1 christos pointer_to_map = &map->next; 7830 1.10 christos 7831 1.10 christos if (p_paddr_valid 7832 1.10 christos && !bed->want_p_paddr_set_to_zero) 7833 1.10 christos { 7834 1.10 christos bfd_vma hdr_size = 0; 7835 1.10 christos if (map->includes_filehdr) 7836 1.10 christos hdr_size = iehdr->e_ehsize; 7837 1.10 christos if (map->includes_phdrs) 7838 1.10 christos hdr_size += iehdr->e_phnum * iehdr->e_phentsize; 7839 1.10 christos 7840 1.12 christos /* Account for padding before the first section in the 7841 1.12 christos segment. */ 7842 1.10 christos map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb 7843 1.1 christos - matching_lma->lma); 7844 1.1 christos } 7845 1.1 christos 7846 1.1 christos free (sections); 7847 1.1 christos continue; 7848 1.1 christos } 7849 1.9 christos else 7850 1.9 christos { 7851 1.9 christos /* Change the current segment's physical address to match 7852 1.9 christos the LMA of the first section that fitted, or if no 7853 1.9 christos section fitted, the first section. */ 7854 1.9 christos if (matching_lma == NULL) 7855 1.12 christos matching_lma = suggested_lma; 7856 1.1 christos 7857 1.1 christos map->p_paddr = matching_lma->lma * opb; 7858 1.1 christos 7859 1.9 christos /* Offset the segment physical address from the lma 7860 1.1 christos to allow for space taken up by elf headers. */ 7861 1.9 christos if (map->includes_phdrs) 7862 1.9 christos { 7863 1.9 christos map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; 7864 1.9 christos 7865 1.9 christos /* iehdr->e_phnum is just an estimate of the number 7866 1.9 christos of program headers that we will need. Make a note 7867 1.9 christos here of the number we used and the segment we chose 7868 1.9 christos to hold these headers, so that we can adjust the 7869 1.9 christos offset when we know the correct value. */ 7870 1.1 christos phdr_adjust_num = iehdr->e_phnum; 7871 1.1 christos phdr_adjust_seg = map; 7872 1.9 christos } 7873 1.1 christos 7874 1.9 christos if (map->includes_filehdr) 7875 1.9 christos { 7876 1.9 christos bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power; 7877 1.9 christos map->p_paddr -= iehdr->e_ehsize; 7878 1.9 christos /* We've subtracted off the size of headers from the 7879 1.9 christos first section lma, but there may have been some 7880 1.9 christos alignment padding before that section too. Try to 7881 1.9 christos account for that by adjusting the segment lma down to 7882 1.9 christos the same alignment. */ 7883 1.12 christos if (segment->p_align != 0 && segment->p_align < align) 7884 1.1 christos align = segment->p_align; 7885 1.1 christos map->p_paddr &= -(align * opb); 7886 1.1 christos } 7887 1.1 christos } 7888 1.1 christos 7889 1.1 christos /* Step Three: Loop over the sections again, this time assigning 7890 1.1 christos those that fit to the current segment and removing them from the 7891 1.1 christos sections array; but making sure not to leave large gaps. Once all 7892 1.1 christos possible sections have been assigned to the current segment it is 7893 1.1 christos added to the list of built segments and if sections still remain 7894 1.1 christos to be assigned, a new segment is constructed before repeating 7895 1.1 christos the loop. */ 7896 1.1 christos isec = 0; 7897 1.1 christos do 7898 1.9 christos { 7899 1.1 christos map->count = 0; 7900 1.1 christos suggested_lma = NULL; 7901 1.1 christos 7902 1.1 christos /* Fill the current segment with sections that fit. */ 7903 1.1 christos for (j = 0; j < section_count; j++) 7904 1.1 christos { 7905 1.1 christos section = sections[j]; 7906 1.1 christos 7907 1.1 christos if (section == NULL) 7908 1.1 christos continue; 7909 1.1 christos 7910 1.1 christos output_section = section->output_section; 7911 1.1 christos 7912 1.13 christos BFD_ASSERT (output_section != NULL); 7913 1.13 christos 7914 1.13 christos if (is_contained_by (output_section, segment, map->p_paddr, 7915 1.1 christos 0, opb, false) 7916 1.1 christos || is_note (section, segment)) 7917 1.1 christos { 7918 1.1 christos if (map->count == 0) 7919 1.1 christos { 7920 1.1 christos /* If the first section in a segment does not start at 7921 1.9 christos the beginning of the segment, then something is 7922 1.9 christos wrong. */ 7923 1.9 christos if (align_power (map->p_paddr 7924 1.9 christos + (map->includes_filehdr 7925 1.9 christos ? iehdr->e_ehsize : 0) 7926 1.9 christos + (map->includes_phdrs 7927 1.12 christos ? iehdr->e_phnum * iehdr->e_phentsize 7928 1.12 christos : 0), 7929 1.10 christos output_section->alignment_power * opb) 7930 1.1 christos != output_section->lma * opb) 7931 1.1 christos goto sorry; 7932 1.1 christos } 7933 1.1 christos else 7934 1.1 christos { 7935 1.1 christos asection *prev_sec; 7936 1.1 christos 7937 1.1 christos prev_sec = map->sections[map->count - 1]; 7938 1.1 christos 7939 1.1 christos /* If the gap between the end of the previous section 7940 1.1 christos and the start of this section is more than 7941 1.1 christos maxpagesize then we need to start a new segment. */ 7942 1.1 christos if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, 7943 1.1 christos maxpagesize) 7944 1.1 christos < BFD_ALIGN (output_section->lma, maxpagesize)) 7945 1.1 christos || (prev_sec->lma + prev_sec->size 7946 1.9 christos > output_section->lma)) 7947 1.9 christos { 7948 1.1 christos if (suggested_lma == NULL) 7949 1.1 christos suggested_lma = output_section; 7950 1.1 christos 7951 1.1 christos continue; 7952 1.1 christos } 7953 1.1 christos } 7954 1.1 christos 7955 1.1 christos map->sections[map->count++] = output_section; 7956 1.9 christos ++isec; 7957 1.12 christos sections[j] = NULL; 7958 1.1 christos if (segment->p_type == PT_LOAD) 7959 1.9 christos section->segment_mark = true; 7960 1.9 christos } 7961 1.1 christos else if (suggested_lma == NULL) 7962 1.1 christos suggested_lma = output_section; 7963 1.10 christos } 7964 1.10 christos 7965 1.12 christos /* PR 23932. A corrupt input file may contain sections that cannot 7966 1.12 christos be assigned to any segment - because for example they have a 7967 1.12 christos negative size - or segments that do not contain any sections. 7968 1.1 christos But there are also valid reasons why a segment can be empty. 7969 1.1 christos So allow a count of zero. */ 7970 1.1 christos 7971 1.1 christos /* Add the current segment to the list of built segments. */ 7972 1.1 christos *pointer_to_map = map; 7973 1.1 christos pointer_to_map = &map->next; 7974 1.1 christos 7975 1.1 christos if (isec < section_count) 7976 1.1 christos { 7977 1.1 christos /* We still have not allocated all of the sections to 7978 1.10 christos segments. Create a new segment here, initialise it 7979 1.12 christos and carry on looping. */ 7980 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *); 7981 1.1 christos amt += section_count * sizeof (asection *); 7982 1.1 christos map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); 7983 1.1 christos if (map == NULL) 7984 1.12 christos { 7985 1.1 christos free (sections); 7986 1.1 christos return false; 7987 1.1 christos } 7988 1.1 christos 7989 1.1 christos /* Initialise the fields of the segment map. Set the physical 7990 1.1 christos physical address to the LMA of the first section that has 7991 1.1 christos not yet been assigned. */ 7992 1.1 christos map->next = NULL; 7993 1.1 christos map->p_type = segment->p_type; 7994 1.12 christos map->p_flags = segment->p_flags; 7995 1.1 christos map->p_flags_valid = 1; 7996 1.1 christos map->p_paddr = suggested_lma->lma * opb; 7997 1.1 christos map->p_paddr_valid = p_paddr_valid; 7998 1.1 christos map->includes_filehdr = 0; 7999 1.12 christos map->includes_phdrs = 0; 8000 1.12 christos } 8001 1.12 christos 8002 1.12 christos continue; 8003 1.12 christos sorry: 8004 1.12 christos bfd_set_error (bfd_error_sorry); 8005 1.1 christos free (sections); 8006 1.1 christos return false; 8007 1.1 christos } 8008 1.1 christos while (isec < section_count); 8009 1.1 christos 8010 1.1 christos free (sections); 8011 1.3 christos } 8012 1.1 christos 8013 1.1 christos elf_seg_map (obfd) = map_first; 8014 1.1 christos 8015 1.1 christos /* If we had to estimate the number of program headers that were 8016 1.1 christos going to be needed, then check our estimate now and adjust 8017 1.1 christos the offset if necessary. */ 8018 1.1 christos if (phdr_adjust_seg != NULL) 8019 1.1 christos { 8020 1.1 christos unsigned int count; 8021 1.1 christos 8022 1.1 christos for (count = 0, map = map_first; map != NULL; map = map->next) 8023 1.1 christos count++; 8024 1.1 christos 8025 1.1 christos if (count > phdr_adjust_num) 8026 1.9 christos phdr_adjust_seg->p_paddr 8027 1.9 christos -= (count - phdr_adjust_num) * iehdr->e_phentsize; 8028 1.9 christos 8029 1.9 christos for (map = map_first; map != NULL; map = map->next) 8030 1.9 christos if (map->p_type == PT_PHDR) 8031 1.9 christos { 8032 1.9 christos bfd_vma adjust 8033 1.9 christos = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0; 8034 1.9 christos map->p_paddr = phdr_adjust_seg->p_paddr + adjust; 8035 1.1 christos break; 8036 1.1 christos } 8037 1.1 christos } 8038 1.1 christos 8039 1.1 christos #undef IS_SOLARIS_PT_INTERP 8040 1.1 christos #undef IS_SECTION_IN_INPUT_SEGMENT 8041 1.1 christos #undef INCLUDE_SECTION_IN_SEGMENT 8042 1.12 christos #undef SEGMENT_AFTER_SEGMENT 8043 1.12 christos #undef SEGMENT_OVERLAPS 8044 1.12 christos return true; 8045 1.12 christos } 8046 1.12 christos 8047 1.12 christos /* Return true if p_align in the ELF program header in ABFD is valid. */ 8048 1.12 christos 8049 1.12 christos static bool 8050 1.12 christos elf_is_p_align_valid (bfd *abfd) 8051 1.12 christos { 8052 1.12 christos unsigned int i; 8053 1.12 christos Elf_Internal_Phdr *segment; 8054 1.12 christos unsigned int num_segments; 8055 1.12 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 8056 1.12 christos bfd_size_type maxpagesize = bed->maxpagesize; 8057 1.12 christos bfd_size_type p_align = bed->p_align; 8058 1.12 christos 8059 1.12 christos /* Return true if the default p_align value isn't set or the maximum 8060 1.12 christos page size is the same as the minimum page size. */ 8061 1.12 christos if (p_align == 0 || maxpagesize == bed->minpagesize) 8062 1.12 christos return true; 8063 1.12 christos 8064 1.12 christos /* When the default p_align value is set, p_align may be set to the 8065 1.12 christos default p_align value while segments are aligned to the maximum 8066 1.12 christos page size. In this case, the input p_align will be ignored and 8067 1.12 christos the maximum page size will be used to align the output segments. */ 8068 1.12 christos segment = elf_tdata (abfd)->phdr; 8069 1.12 christos num_segments = elf_elfheader (abfd)->e_phnum; 8070 1.12 christos for (i = 0; i < num_segments; i++, segment++) 8071 1.12 christos if (segment->p_type == PT_LOAD 8072 1.12 christos && (segment->p_align != p_align 8073 1.12 christos || vma_page_aligned_bias (segment->p_vaddr, 8074 1.12 christos segment->p_offset, 8075 1.12 christos maxpagesize) != 0)) 8076 1.12 christos return true; 8077 1.1 christos 8078 1.1 christos return false; 8079 1.1 christos } 8080 1.1 christos 8081 1.12 christos /* Copy ELF program header information. */ 8082 1.1 christos 8083 1.1 christos static bool 8084 1.1 christos copy_elf_program_header (bfd *ibfd, bfd *obfd) 8085 1.1 christos { 8086 1.1 christos Elf_Internal_Ehdr *iehdr; 8087 1.1 christos struct elf_segment_map *map; 8088 1.1 christos struct elf_segment_map *map_first; 8089 1.1 christos struct elf_segment_map **pointer_to_map; 8090 1.1 christos Elf_Internal_Phdr *segment; 8091 1.12 christos unsigned int i; 8092 1.12 christos unsigned int num_segments; 8093 1.12 christos bool phdr_included = false; 8094 1.12 christos bool p_paddr_valid; 8095 1.1 christos bool p_palign_valid; 8096 1.1 christos unsigned int opb = bfd_octets_per_byte (ibfd, NULL); 8097 1.1 christos 8098 1.1 christos iehdr = elf_elfheader (ibfd); 8099 1.1 christos 8100 1.1 christos map_first = NULL; 8101 1.1 christos pointer_to_map = &map_first; 8102 1.1 christos 8103 1.12 christos /* If all the segment p_paddr fields are zero, don't set 8104 1.1 christos map->p_paddr_valid. */ 8105 1.1 christos p_paddr_valid = false; 8106 1.1 christos num_segments = elf_elfheader (ibfd)->e_phnum; 8107 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr; 8108 1.1 christos i < num_segments; 8109 1.1 christos i++, segment++) 8110 1.12 christos if (segment->p_paddr != 0) 8111 1.1 christos { 8112 1.1 christos p_paddr_valid = true; 8113 1.1 christos break; 8114 1.12 christos } 8115 1.12 christos 8116 1.1 christos p_palign_valid = elf_is_p_align_valid (ibfd); 8117 1.1 christos 8118 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr; 8119 1.1 christos i < num_segments; 8120 1.1 christos i++, segment++) 8121 1.1 christos { 8122 1.12 christos asection *section; 8123 1.1 christos unsigned int section_count; 8124 1.1 christos size_t amt; 8125 1.1 christos Elf_Internal_Shdr *this_hdr; 8126 1.1 christos asection *first_section = NULL; 8127 1.1 christos asection *lowest_section; 8128 1.1 christos 8129 1.1 christos /* Compute how many sections are in this segment. */ 8130 1.1 christos for (section = ibfd->sections, section_count = 0; 8131 1.1 christos section != NULL; 8132 1.1 christos section = section->next) 8133 1.1 christos { 8134 1.1 christos this_hdr = &(elf_section_data(section)->this_hdr); 8135 1.1 christos if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) 8136 1.1 christos { 8137 1.1 christos if (first_section == NULL) 8138 1.1 christos first_section = section; 8139 1.1 christos section_count++; 8140 1.1 christos } 8141 1.1 christos } 8142 1.1 christos 8143 1.10 christos /* Allocate a segment map big enough to contain 8144 1.12 christos all of the sections we have selected. */ 8145 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *); 8146 1.1 christos amt += section_count * sizeof (asection *); 8147 1.12 christos map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); 8148 1.1 christos if (map == NULL) 8149 1.1 christos return false; 8150 1.1 christos 8151 1.1 christos /* Initialize the fields of the output segment map with the 8152 1.1 christos input segment. */ 8153 1.1 christos map->next = NULL; 8154 1.1 christos map->p_type = segment->p_type; 8155 1.1 christos map->p_flags = segment->p_flags; 8156 1.1 christos map->p_flags_valid = 1; 8157 1.1 christos map->p_paddr = segment->p_paddr; 8158 1.12 christos map->p_paddr_valid = p_paddr_valid; 8159 1.12 christos map->p_align = segment->p_align; 8160 1.12 christos /* Keep p_align of PT_GNU_STACK for stack alignment. */ 8161 1.1 christos map->p_align_valid = (map->p_type == PT_GNU_STACK 8162 1.1 christos || p_palign_valid); 8163 1.3 christos map->p_vaddr_offset = 0; 8164 1.3 christos 8165 1.1 christos if (map->p_type == PT_GNU_RELRO 8166 1.1 christos || map->p_type == PT_GNU_STACK) 8167 1.1 christos { 8168 1.1 christos /* The PT_GNU_RELRO segment may contain the first a few 8169 1.3 christos bytes in the .got.plt section even if the whole .got.plt 8170 1.3 christos section isn't in the PT_GNU_RELRO segment. We won't 8171 1.3 christos change the size of the PT_GNU_RELRO segment. 8172 1.1 christos Similarly, PT_GNU_STACK size is significant on uclinux 8173 1.1 christos systems. */ 8174 1.1 christos map->p_size = segment->p_memsz; 8175 1.1 christos map->p_size_valid = 1; 8176 1.1 christos } 8177 1.1 christos 8178 1.1 christos /* Determine if this segment contains the ELF file header 8179 1.1 christos and if it contains the program headers themselves. */ 8180 1.1 christos map->includes_filehdr = (segment->p_offset == 0 8181 1.1 christos && segment->p_filesz >= iehdr->e_ehsize); 8182 1.1 christos 8183 1.1 christos map->includes_phdrs = 0; 8184 1.1 christos if (! phdr_included || segment->p_type != PT_LOAD) 8185 1.1 christos { 8186 1.1 christos map->includes_phdrs = 8187 1.1 christos (segment->p_offset <= (bfd_vma) iehdr->e_phoff 8188 1.1 christos && (segment->p_offset + segment->p_filesz 8189 1.1 christos >= ((bfd_vma) iehdr->e_phoff 8190 1.1 christos + iehdr->e_phnum * iehdr->e_phentsize))); 8191 1.12 christos 8192 1.1 christos if (segment->p_type == PT_LOAD && map->includes_phdrs) 8193 1.1 christos phdr_included = true; 8194 1.3 christos } 8195 1.1 christos 8196 1.1 christos lowest_section = NULL; 8197 1.1 christos if (section_count != 0) 8198 1.1 christos { 8199 1.1 christos unsigned int isec = 0; 8200 1.1 christos 8201 1.1 christos for (section = first_section; 8202 1.1 christos section != NULL; 8203 1.1 christos section = section->next) 8204 1.1 christos { 8205 1.1 christos this_hdr = &(elf_section_data(section)->this_hdr); 8206 1.1 christos if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) 8207 1.1 christos { 8208 1.1 christos map->sections[isec++] = section->output_section; 8209 1.1 christos if ((section->flags & SEC_ALLOC) != 0) 8210 1.1 christos { 8211 1.3 christos bfd_vma seg_off; 8212 1.3 christos 8213 1.3 christos if (lowest_section == NULL 8214 1.3 christos || section->lma < lowest_section->lma) 8215 1.1 christos lowest_section = section; 8216 1.1 christos 8217 1.1 christos /* Section lmas are set up from PT_LOAD header 8218 1.1 christos p_paddr in _bfd_elf_make_section_from_shdr. 8219 1.1 christos If this header has a p_paddr that disagrees 8220 1.1 christos with the section lma, flag the p_paddr as 8221 1.1 christos invalid. */ 8222 1.1 christos if ((section->flags & SEC_LOAD) != 0) 8223 1.1 christos seg_off = this_hdr->sh_offset - segment->p_offset; 8224 1.12 christos else 8225 1.12 christos seg_off = this_hdr->sh_addr - segment->p_vaddr; 8226 1.1 christos if (section->lma * opb - segment->p_paddr != seg_off) 8227 1.1 christos map->p_paddr_valid = false; 8228 1.1 christos } 8229 1.1 christos if (isec == section_count) 8230 1.1 christos break; 8231 1.1 christos } 8232 1.1 christos } 8233 1.10 christos } 8234 1.12 christos 8235 1.10 christos if (section_count == 0) 8236 1.10 christos map->p_vaddr_offset = segment->p_vaddr / opb; 8237 1.10 christos else if (map->p_paddr_valid) 8238 1.10 christos { 8239 1.10 christos /* Account for padding before the first section in the segment. */ 8240 1.10 christos bfd_vma hdr_size = 0; 8241 1.10 christos if (map->includes_filehdr) 8242 1.10 christos hdr_size = iehdr->e_ehsize; 8243 1.10 christos if (map->includes_phdrs) 8244 1.12 christos hdr_size += iehdr->e_phnum * iehdr->e_phentsize; 8245 1.10 christos 8246 1.10 christos map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb 8247 1.1 christos - (lowest_section ? lowest_section->lma : 0)); 8248 1.1 christos } 8249 1.1 christos 8250 1.1 christos map->count = section_count; 8251 1.1 christos *pointer_to_map = map; 8252 1.1 christos pointer_to_map = &map->next; 8253 1.3 christos } 8254 1.12 christos 8255 1.1 christos elf_seg_map (obfd) = map_first; 8256 1.1 christos return true; 8257 1.1 christos } 8258 1.1 christos 8259 1.1 christos /* Copy private BFD data. This copies or rewrites ELF program header 8260 1.12 christos information. */ 8261 1.1 christos 8262 1.1 christos static bool 8263 1.12 christos copy_private_bfd_data (bfd *ibfd, bfd *obfd) 8264 1.12 christos { 8265 1.1 christos bfd_vma maxpagesize; 8266 1.1 christos 8267 1.12 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 8268 1.1 christos || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 8269 1.1 christos return true; 8270 1.12 christos 8271 1.1 christos if (elf_tdata (ibfd)->phdr == NULL) 8272 1.1 christos return true; 8273 1.1 christos 8274 1.1 christos if (ibfd->xvec == obfd->xvec) 8275 1.1 christos { 8276 1.1 christos /* Check to see if any sections in the input BFD 8277 1.14 christos covered by ELF program header have changed. */ 8278 1.14 christos Elf_Internal_Phdr *segment; 8279 1.14 christos asection * section; 8280 1.1 christos asection * osec; 8281 1.1 christos asection * prev; 8282 1.1 christos unsigned int i, num_segments; 8283 1.1 christos Elf_Internal_Shdr *this_hdr; 8284 1.1 christos const struct elf_backend_data *bed; 8285 1.1 christos 8286 1.1 christos bed = get_elf_backend_data (ibfd); 8287 1.1 christos 8288 1.1 christos /* Regenerate the segment map if p_paddr is set to 0. */ 8289 1.1 christos if (bed->want_p_paddr_set_to_zero) 8290 1.1 christos goto rewrite; 8291 1.1 christos 8292 1.1 christos /* Initialize the segment mark field. */ 8293 1.12 christos for (section = obfd->sections; section != NULL; 8294 1.1 christos section = section->next) 8295 1.1 christos section->segment_mark = false; 8296 1.1 christos 8297 1.1 christos num_segments = elf_elfheader (ibfd)->e_phnum; 8298 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr; 8299 1.1 christos i < num_segments; 8300 1.1 christos i++, segment++) 8301 1.1 christos { 8302 1.1 christos /* PR binutils/3535. The Solaris linker always sets the p_paddr 8303 1.1 christos and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 8304 1.1 christos which severly confuses things, so always regenerate the segment 8305 1.1 christos map in this case. */ 8306 1.13 christos if (segment->p_paddr == 0 8307 1.13 christos && segment->p_memsz == 0 8308 1.1 christos && (segment->p_type == PT_INTERP 8309 1.1 christos || segment->p_type == PT_DYNAMIC)) 8310 1.14 christos goto rewrite; 8311 1.1 christos 8312 1.1 christos for (section = ibfd->sections, prev = NULL; 8313 1.1 christos section != NULL; section = section->next) 8314 1.1 christos { 8315 1.1 christos /* We mark the output section so that we know it comes 8316 1.1 christos from the input BFD. */ 8317 1.12 christos osec = section->output_section; 8318 1.1 christos if (osec) 8319 1.1 christos osec->segment_mark = true; 8320 1.1 christos 8321 1.1 christos /* Check if this section is covered by the segment. */ 8322 1.1 christos this_hdr = &(elf_section_data(section)->this_hdr); 8323 1.1 christos if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) 8324 1.1 christos { 8325 1.1 christos /* FIXME: Check if its output section is changed or 8326 1.1 christos removed. What else do we need to check? */ 8327 1.1 christos if (osec == NULL 8328 1.1 christos || section->flags != osec->flags 8329 1.1 christos || section->lma != osec->lma 8330 1.1 christos || section->vma != osec->vma 8331 1.1 christos || section->size != osec->size 8332 1.1 christos || section->rawsize != osec->rawsize 8333 1.14 christos || section->alignment_power != osec->alignment_power) 8334 1.14 christos goto rewrite; 8335 1.14 christos 8336 1.14 christos /* PR 31450: If this is an allocated section then make sure 8337 1.14 christos that this section's vma to lma relationship is the same 8338 1.14 christos as previous (allocated) section's. */ 8339 1.14 christos if (prev != NULL 8340 1.14 christos && section->flags & SEC_ALLOC 8341 1.14 christos && section->lma - section->vma != prev->lma - prev->vma) 8342 1.14 christos goto rewrite; 8343 1.14 christos 8344 1.1 christos if (section->flags & SEC_ALLOC) 8345 1.1 christos prev = section; 8346 1.1 christos } 8347 1.1 christos } 8348 1.1 christos } 8349 1.1 christos 8350 1.1 christos /* Check to see if any output section do not come from the 8351 1.1 christos input BFD. */ 8352 1.1 christos for (section = obfd->sections; section != NULL; 8353 1.9 christos section = section->next) 8354 1.1 christos { 8355 1.1 christos if (!section->segment_mark) 8356 1.12 christos goto rewrite; 8357 1.1 christos else 8358 1.1 christos section->segment_mark = false; 8359 1.1 christos } 8360 1.1 christos 8361 1.1 christos return copy_elf_program_header (ibfd, obfd); 8362 1.12 christos } 8363 1.12 christos 8364 1.1 christos rewrite: 8365 1.1 christos maxpagesize = 0; 8366 1.1 christos if (ibfd->xvec == obfd->xvec) 8367 1.1 christos { 8368 1.1 christos /* When rewriting program header, set the output maxpagesize to 8369 1.1 christos the maximum alignment of input PT_LOAD segments. */ 8370 1.1 christos Elf_Internal_Phdr *segment; 8371 1.1 christos unsigned int i; 8372 1.1 christos unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; 8373 1.1 christos 8374 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr; 8375 1.1 christos i < num_segments; 8376 1.1 christos i++, segment++) 8377 1.3 christos if (segment->p_type == PT_LOAD 8378 1.3 christos && maxpagesize < segment->p_align) 8379 1.3 christos { 8380 1.9 christos /* PR 17512: file: f17299af. */ 8381 1.9 christos if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) 8382 1.9 christos /* xgettext:c-format */ 8383 1.9 christos _bfd_error_handler (_("%pB: warning: segment alignment of %#" 8384 1.3 christos PRIx64 " is too large"), 8385 1.3 christos ibfd, (uint64_t) segment->p_align); 8386 1.3 christos else 8387 1.1 christos maxpagesize = segment->p_align; 8388 1.12 christos } 8389 1.12 christos } 8390 1.1 christos if (maxpagesize == 0) 8391 1.12 christos maxpagesize = get_elf_backend_data (obfd)->maxpagesize; 8392 1.1 christos 8393 1.1 christos return rewrite_elf_program_header (ibfd, obfd, maxpagesize); 8394 1.14 christos } 8395 1.14 christos 8396 1.14 christos /* Copy private section information from input to output. This function 8397 1.14 christos is called both by objcopy where LINK_INFO is NULL, and ld where 8398 1.1 christos LINK_INFO is non-NULL in the usual case but NULL for a special case 8399 1.12 christos when dealing with LTO IR or cloning output sections. */ 8400 1.14 christos 8401 1.1 christos bool 8402 1.1 christos _bfd_elf_copy_private_section_data (bfd *ibfd, 8403 1.1 christos asection *isec, 8404 1.1 christos bfd *obfd, 8405 1.1 christos asection *osec, 8406 1.1 christos struct bfd_link_info *link_info) 8407 1.1 christos { 8408 1.12 christos if (ibfd->xvec->flavour != bfd_target_elf_flavour 8409 1.1 christos || obfd->xvec->flavour != bfd_target_elf_flavour) 8410 1.14 christos return true; 8411 1.14 christos 8412 1.14 christos Elf_Internal_Shdr *ihdr = &elf_section_data (isec)->this_hdr; 8413 1.14 christos Elf_Internal_Shdr *ohdr = &elf_section_data (osec)->this_hdr; 8414 1.14 christos if (link_info == NULL) 8415 1.14 christos { 8416 1.14 christos ohdr->sh_entsize = ihdr->sh_entsize; 8417 1.14 christos 8418 1.14 christos if (ihdr->sh_type == SHT_SYMTAB 8419 1.14 christos || ihdr->sh_type == SHT_DYNSYM 8420 1.14 christos || ihdr->sh_type == SHT_GNU_verneed 8421 1.14 christos || ihdr->sh_type == SHT_GNU_verdef) 8422 1.1 christos ohdr->sh_info = ihdr->sh_info; 8423 1.12 christos } 8424 1.12 christos 8425 1.12 christos /* If this is a known ABI section, ELF section type and flags may 8426 1.12 christos have been set up when OSEC was created. For normal sections we 8427 1.12 christos allow the user to override the type and flags other than 8428 1.12 christos SHF_MASKOS and SHF_MASKPROC. */ 8429 1.12 christos if (elf_section_type (osec) == SHT_PROGBITS 8430 1.12 christos || elf_section_type (osec) == SHT_NOTE 8431 1.14 christos || elf_section_type (osec) == SHT_NOBITS) 8432 1.12 christos elf_section_type (osec) = SHT_NULL; 8433 1.12 christos 8434 1.12 christos /* For objcopy and relocatable link, copy the ELF section type from 8435 1.12 christos the input file if the BFD section flags are the same. (If they 8436 1.12 christos are different the user may be doing something like 8437 1.14 christos "objcopy --set-section-flags .text=alloc,data".) For a final 8438 1.14 christos link allow some flags that the linker clears to differ. */ 8439 1.1 christos bool final_link = (link_info != NULL 8440 1.1 christos && !bfd_link_relocatable (link_info)); 8441 1.1 christos if (elf_section_type (osec) == SHT_NULL 8442 1.1 christos && (osec->flags == isec->flags 8443 1.1 christos || (final_link 8444 1.1 christos && ((osec->flags ^ isec->flags) 8445 1.1 christos & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) 8446 1.1 christos elf_section_type (osec) = elf_section_type (isec); 8447 1.12 christos 8448 1.12 christos /* FIXME: Is this correct for all OS/PROC specific flags? */ 8449 1.1 christos elf_section_flags (osec) = (elf_section_flags (isec) 8450 1.9 christos & (SHF_MASKOS | SHF_MASKPROC)); 8451 1.10 christos 8452 1.10 christos /* Copy sh_info from input for mbind section. */ 8453 1.9 christos if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0 8454 1.9 christos && elf_section_flags (isec) & SHF_GNU_MBIND) 8455 1.9 christos elf_section_data (osec)->this_hdr.sh_info 8456 1.1 christos = elf_section_data (isec)->this_hdr.sh_info; 8457 1.1 christos 8458 1.1 christos /* Set things up for objcopy and relocatable link. The output 8459 1.1 christos SHT_GROUP section will have its elf_next_in_group pointing back 8460 1.9 christos to the input group members. Ignore linker created group section. 8461 1.9 christos See elfNN_ia64_object_p in elfxx-ia64.c. */ 8462 1.9 christos if ((link_info == NULL 8463 1.9 christos || !link_info->resolve_section_groups) 8464 1.9 christos && (elf_sec_group (isec) == NULL 8465 1.9 christos || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)) 8466 1.9 christos { 8467 1.9 christos if (elf_section_flags (isec) & SHF_GROUP) 8468 1.9 christos elf_section_flags (osec) |= SHF_GROUP; 8469 1.9 christos elf_next_in_group (osec) = elf_next_in_group (isec); 8470 1.3 christos elf_section_data (osec)->group = elf_section_data (isec)->group; 8471 1.9 christos } 8472 1.9 christos 8473 1.9 christos /* If not decompress, preserve SHF_COMPRESSED. */ 8474 1.9 christos if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0) 8475 1.1 christos elf_section_flags (osec) |= (elf_section_flags (isec) 8476 1.1 christos & SHF_COMPRESSED); 8477 1.1 christos 8478 1.1 christos /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We 8479 1.1 christos don't use the output section of the linked-to section since it 8480 1.1 christos may be NULL at this point. */ 8481 1.1 christos if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) 8482 1.1 christos { 8483 1.1 christos ohdr->sh_flags |= SHF_LINK_ORDER; 8484 1.1 christos elf_linked_to_section (osec) = elf_linked_to_section (isec); 8485 1.1 christos } 8486 1.1 christos 8487 1.12 christos osec->use_rela_p = isec->use_rela_p; 8488 1.1 christos 8489 1.1 christos return true; 8490 1.1 christos } 8491 1.1 christos 8492 1.1 christos /* Look at all the SHT_GROUP sections in IBFD, making any adjustments 8493 1.1 christos necessary if we are removing either the SHT_GROUP section or any of 8494 1.1 christos the group member sections. DISCARDED is the value that a section's 8495 1.1 christos output_section has if the section will be discarded, NULL when this 8496 1.1 christos function is called from objcopy, bfd_abs_section_ptr when called 8497 1.12 christos from the linker. */ 8498 1.1 christos 8499 1.1 christos bool 8500 1.1 christos _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) 8501 1.1 christos { 8502 1.1 christos asection *isec; 8503 1.1 christos 8504 1.1 christos for (isec = ibfd->sections; isec != NULL; isec = isec->next) 8505 1.1 christos if (elf_section_type (isec) == SHT_GROUP) 8506 1.1 christos { 8507 1.1 christos asection *first = elf_next_in_group (isec); 8508 1.1 christos asection *s = first; 8509 1.1 christos bfd_size_type removed = 0; 8510 1.1 christos 8511 1.1 christos while (s != NULL) 8512 1.1 christos { 8513 1.1 christos /* If this member section is being output but the 8514 1.1 christos SHT_GROUP section is not, then clear the group info 8515 1.1 christos set up by _bfd_elf_copy_private_section_data. */ 8516 1.1 christos if (s->output_section != discarded 8517 1.1 christos && isec->output_section == discarded) 8518 1.1 christos { 8519 1.1 christos elf_section_flags (s->output_section) &= ~SHF_GROUP; 8520 1.12 christos elf_group_name (s->output_section) = NULL; 8521 1.9 christos } 8522 1.9 christos else 8523 1.12 christos { 8524 1.12 christos struct bfd_elf_section_data *elf_sec = elf_section_data (s); 8525 1.12 christos if (s->output_section == discarded 8526 1.12 christos && isec->output_section != discarded) 8527 1.12 christos { 8528 1.12 christos /* Conversely, if the member section is not being 8529 1.12 christos output but the SHT_GROUP section is, then adjust 8530 1.12 christos its size. */ 8531 1.12 christos removed += 4; 8532 1.12 christos if (elf_sec->rel.hdr != NULL 8533 1.12 christos && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0) 8534 1.12 christos removed += 4; 8535 1.12 christos if (elf_sec->rela.hdr != NULL 8536 1.12 christos && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0) 8537 1.12 christos removed += 4; 8538 1.12 christos } 8539 1.12 christos else 8540 1.12 christos { 8541 1.12 christos /* Also adjust for zero-sized relocation member 8542 1.12 christos section. */ 8543 1.12 christos if (elf_sec->rel.hdr != NULL 8544 1.12 christos && elf_sec->rel.hdr->sh_size == 0) 8545 1.12 christos removed += 4; 8546 1.12 christos if (elf_sec->rela.hdr != NULL 8547 1.12 christos && elf_sec->rela.hdr->sh_size == 0) 8548 1.9 christos removed += 4; 8549 1.1 christos } 8550 1.1 christos } 8551 1.1 christos s = elf_next_in_group (s); 8552 1.1 christos if (s == first) 8553 1.1 christos break; 8554 1.1 christos } 8555 1.1 christos if (removed != 0) 8556 1.1 christos { 8557 1.1 christos if (discarded != NULL) 8558 1.9 christos { 8559 1.1 christos /* If we've been called for ld -r, then we need to 8560 1.1 christos adjust the input section size. */ 8561 1.1 christos if (isec->rawsize == 0) 8562 1.9 christos isec->rawsize = isec->size; 8563 1.9 christos isec->size = isec->rawsize - removed; 8564 1.9 christos if (isec->size <= 4) 8565 1.9 christos { 8566 1.9 christos isec->size = 0; 8567 1.1 christos isec->flags |= SEC_EXCLUDE; 8568 1.12 christos } 8569 1.1 christos } 8570 1.1 christos else if (isec->output_section != NULL) 8571 1.1 christos { 8572 1.1 christos /* Adjust the output section size when called from 8573 1.9 christos objcopy. */ 8574 1.9 christos isec->output_section->size -= removed; 8575 1.9 christos if (isec->output_section->size <= 4) 8576 1.9 christos { 8577 1.9 christos isec->output_section->size = 0; 8578 1.1 christos isec->output_section->flags |= SEC_EXCLUDE; 8579 1.1 christos } 8580 1.1 christos } 8581 1.1 christos } 8582 1.12 christos } 8583 1.1 christos 8584 1.1 christos return true; 8585 1.1 christos } 8586 1.1 christos 8587 1.12 christos /* Copy private header information. */ 8588 1.1 christos 8589 1.1 christos bool 8590 1.1 christos _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) 8591 1.1 christos { 8592 1.12 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 8593 1.1 christos || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 8594 1.1 christos return true; 8595 1.1 christos 8596 1.1 christos /* Copy over private BFD data if it has not already been copied. 8597 1.1 christos This must be done here, rather than in the copy_private_bfd_data 8598 1.1 christos entry point, because the latter is called after the section 8599 1.3 christos contents have been set, which means that the program headers have 8600 1.1 christos already been worked out. */ 8601 1.1 christos if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) 8602 1.12 christos { 8603 1.1 christos if (! copy_private_bfd_data (ibfd, obfd)) 8604 1.1 christos return false; 8605 1.1 christos } 8606 1.1 christos 8607 1.1 christos return _bfd_elf_fixup_group_sections (ibfd, NULL); 8608 1.1 christos } 8609 1.1 christos 8610 1.1 christos /* Copy private symbol information. If this symbol is in a section 8611 1.1 christos which we did not map into a BFD section, try to map the section 8612 1.1 christos index correctly. We use special macro definitions for the mapped 8613 1.1 christos section indices; these definitions are interpreted by the 8614 1.1 christos swap_out_syms function. */ 8615 1.1 christos 8616 1.1 christos #define MAP_ONESYMTAB (SHN_HIOS + 1) 8617 1.1 christos #define MAP_DYNSYMTAB (SHN_HIOS + 2) 8618 1.1 christos #define MAP_STRTAB (SHN_HIOS + 3) 8619 1.1 christos #define MAP_SHSTRTAB (SHN_HIOS + 4) 8620 1.12 christos #define MAP_SYM_SHNDX (SHN_HIOS + 5) 8621 1.1 christos 8622 1.1 christos bool 8623 1.1 christos _bfd_elf_copy_private_symbol_data (bfd *ibfd, 8624 1.1 christos asymbol *isymarg, 8625 1.1 christos bfd *obfd, 8626 1.1 christos asymbol *osymarg) 8627 1.1 christos { 8628 1.1 christos elf_symbol_type *isym, *osym; 8629 1.1 christos 8630 1.12 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 8631 1.1 christos || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 8632 1.12 christos return true; 8633 1.12 christos 8634 1.1 christos isym = elf_symbol_from (isymarg); 8635 1.1 christos osym = elf_symbol_from (osymarg); 8636 1.1 christos 8637 1.1 christos if (isym != NULL 8638 1.1 christos && isym->internal_elf_sym.st_shndx != 0 8639 1.1 christos && osym != NULL 8640 1.1 christos && bfd_is_abs_section (isym->symbol.section)) 8641 1.1 christos { 8642 1.1 christos unsigned int shndx; 8643 1.1 christos 8644 1.1 christos shndx = isym->internal_elf_sym.st_shndx; 8645 1.1 christos if (shndx == elf_onesymtab (ibfd)) 8646 1.1 christos shndx = MAP_ONESYMTAB; 8647 1.14 christos else if (shndx == elf_dynsymtab (ibfd)) 8648 1.1 christos shndx = MAP_DYNSYMTAB; 8649 1.14 christos else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link) 8650 1.1 christos shndx = MAP_STRTAB; 8651 1.3 christos else if (shndx == elf_elfheader (ibfd)->e_shstrndx) 8652 1.1 christos shndx = MAP_SHSTRTAB; 8653 1.1 christos else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) 8654 1.1 christos shndx = MAP_SYM_SHNDX; 8655 1.1 christos osym->internal_elf_sym.st_shndx = shndx; 8656 1.12 christos } 8657 1.1 christos 8658 1.1 christos return true; 8659 1.1 christos } 8660 1.1 christos 8661 1.12 christos /* Swap out the symbols. */ 8662 1.1 christos 8663 1.3 christos static bool 8664 1.12 christos swap_out_syms (bfd *abfd, 8665 1.12 christos struct elf_strtab_hash **sttp, 8666 1.1 christos int relocatable_p, 8667 1.1 christos struct bfd_link_info *info) 8668 1.12 christos { 8669 1.1 christos const struct elf_backend_data *bed; 8670 1.3 christos unsigned int symcount; 8671 1.1 christos asymbol **syms; 8672 1.1 christos struct elf_strtab_hash *stt; 8673 1.1 christos Elf_Internal_Shdr *symtab_hdr; 8674 1.3 christos Elf_Internal_Shdr *symtab_shndx_hdr; 8675 1.1 christos Elf_Internal_Shdr *symstrtab_hdr; 8676 1.1 christos struct elf_sym_strtab *symstrtab; 8677 1.3 christos bfd_byte *outbound_syms; 8678 1.12 christos bfd_byte *outbound_shndx; 8679 1.3 christos unsigned long outbound_syms_index; 8680 1.12 christos unsigned int idx; 8681 1.12 christos unsigned int num_locals; 8682 1.1 christos size_t amt; 8683 1.3 christos bool name_local_sections; 8684 1.12 christos 8685 1.1 christos if (!elf_map_symbols (abfd, &num_locals)) 8686 1.1 christos return false; 8687 1.3 christos 8688 1.1 christos /* Dump out the symtabs. */ 8689 1.12 christos stt = _bfd_elf_strtab_init (); 8690 1.1 christos if (stt == NULL) 8691 1.1 christos return false; 8692 1.1 christos 8693 1.1 christos bed = get_elf_backend_data (abfd); 8694 1.1 christos symcount = bfd_get_symcount (abfd); 8695 1.1 christos symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 8696 1.1 christos symtab_hdr->sh_type = SHT_SYMTAB; 8697 1.3 christos symtab_hdr->sh_entsize = bed->s->sizeof_sym; 8698 1.1 christos symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); 8699 1.1 christos symtab_hdr->sh_info = num_locals + 1; 8700 1.1 christos symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; 8701 1.1 christos 8702 1.1 christos symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; 8703 1.3 christos symstrtab_hdr->sh_type = SHT_STRTAB; 8704 1.12 christos 8705 1.12 christos /* Allocate buffer to swap out the .strtab section. */ 8706 1.3 christos if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt) 8707 1.12 christos || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL) 8708 1.3 christos { 8709 1.12 christos bfd_set_error (bfd_error_no_memory); 8710 1.3 christos _bfd_elf_strtab_free (stt); 8711 1.3 christos return false; 8712 1.12 christos } 8713 1.14 christos 8714 1.1 christos if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt) 8715 1.12 christos || (outbound_syms = bfd_malloc (amt)) == NULL) 8716 1.12 christos { 8717 1.12 christos error_no_mem: 8718 1.12 christos bfd_set_error (bfd_error_no_memory); 8719 1.3 christos error_return: 8720 1.12 christos free (symstrtab); 8721 1.1 christos _bfd_elf_strtab_free (stt); 8722 1.1 christos return false; 8723 1.3 christos } 8724 1.1 christos symtab_hdr->contents = outbound_syms; 8725 1.1 christos outbound_syms_index = 0; 8726 1.3 christos 8727 1.3 christos outbound_shndx = NULL; 8728 1.1 christos 8729 1.3 christos if (elf_symtab_shndx_list (abfd)) 8730 1.3 christos { 8731 1.1 christos symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; 8732 1.12 christos if (symtab_shndx_hdr->sh_name != 0) 8733 1.12 christos { 8734 1.12 christos if (_bfd_mul_overflow (symcount + 1, 8735 1.12 christos sizeof (Elf_External_Sym_Shndx), &amt)) 8736 1.3 christos goto error_no_mem; 8737 1.3 christos outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt); 8738 1.3 christos if (outbound_shndx == NULL) 8739 1.3 christos goto error_return; 8740 1.3 christos 8741 1.3 christos symtab_shndx_hdr->contents = outbound_shndx; 8742 1.3 christos symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; 8743 1.3 christos symtab_shndx_hdr->sh_size = amt; 8744 1.1 christos symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); 8745 1.3 christos symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); 8746 1.1 christos } 8747 1.1 christos /* FIXME: What about any other headers in the list ? */ 8748 1.1 christos } 8749 1.1 christos 8750 1.1 christos /* Now generate the data (for "contents"). */ 8751 1.1 christos { 8752 1.1 christos /* Fill in zeroth symbol and swap it out. */ 8753 1.1 christos Elf_Internal_Sym sym; 8754 1.1 christos sym.st_name = 0; 8755 1.1 christos sym.st_value = 0; 8756 1.1 christos sym.st_size = 0; 8757 1.1 christos sym.st_info = 0; 8758 1.1 christos sym.st_other = 0; 8759 1.14 christos sym.st_shndx = SHN_UNDEF; 8760 1.14 christos sym.st_target_internal = 0; 8761 1.3 christos symstrtab[outbound_syms_index].sym = sym; 8762 1.1 christos symstrtab[outbound_syms_index].dest_index = outbound_syms_index; 8763 1.1 christos outbound_syms_index++; 8764 1.1 christos } 8765 1.1 christos 8766 1.1 christos name_local_sections 8767 1.1 christos = (bed->elf_backend_name_local_section_symbols 8768 1.1 christos && bed->elf_backend_name_local_section_symbols (abfd)); 8769 1.14 christos 8770 1.1 christos syms = bfd_get_outsymbols (abfd); 8771 1.1 christos for (idx = 0; idx < symcount; idx++) 8772 1.14 christos { 8773 1.1 christos Elf_Internal_Sym sym; 8774 1.1 christos 8775 1.1 christos flagword flags = syms[idx]->flags; 8776 1.1 christos if (!name_local_sections 8777 1.1 christos && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) 8778 1.14 christos { 8779 1.1 christos /* Local section symbols have no name. */ 8780 1.1 christos sym.st_name = 0; 8781 1.1 christos } 8782 1.3 christos else 8783 1.3 christos { 8784 1.14 christos /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize 8785 1.14 christos to get the final offset for st_name. */ 8786 1.3 christos size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false); 8787 1.14 christos if (stridx == (size_t) -1) 8788 1.1 christos goto error_return; 8789 1.1 christos sym.st_name = stridx; 8790 1.14 christos } 8791 1.14 christos 8792 1.14 christos bfd_vma value = syms[idx]->value; 8793 1.1 christos elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]); 8794 1.14 christos asection *sec = syms[idx]->section; 8795 1.1 christos 8796 1.1 christos if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec)) 8797 1.1 christos { 8798 1.1 christos /* ELF common symbols put the alignment into the `value' field, 8799 1.1 christos and the size into the `size' field. This is backwards from 8800 1.1 christos how BFD handles it, so reverse it here. */ 8801 1.1 christos sym.st_size = value; 8802 1.1 christos if (type_ptr == NULL 8803 1.1 christos || type_ptr->internal_elf_sym.st_value == 0) 8804 1.1 christos sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); 8805 1.14 christos else 8806 1.1 christos sym.st_value = type_ptr->internal_elf_sym.st_value; 8807 1.1 christos sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 8808 1.1 christos } 8809 1.1 christos else 8810 1.1 christos { 8811 1.1 christos unsigned int shndx; 8812 1.1 christos 8813 1.1 christos if (sec->output_section) 8814 1.1 christos { 8815 1.1 christos value += sec->output_offset; 8816 1.1 christos sec = sec->output_section; 8817 1.1 christos } 8818 1.1 christos 8819 1.1 christos /* Don't add in the section vma for relocatable output. */ 8820 1.1 christos if (! relocatable_p) 8821 1.1 christos value += sec->vma; 8822 1.1 christos sym.st_value = value; 8823 1.1 christos sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; 8824 1.1 christos 8825 1.1 christos if (bfd_is_abs_section (sec) 8826 1.1 christos && type_ptr != NULL 8827 1.1 christos && type_ptr->internal_elf_sym.st_shndx != 0) 8828 1.1 christos { 8829 1.1 christos /* This symbol is in a real ELF section which we did 8830 1.1 christos not create as a BFD section. Undo the mapping done 8831 1.1 christos by copy_private_symbol_data. */ 8832 1.1 christos shndx = type_ptr->internal_elf_sym.st_shndx; 8833 1.1 christos switch (shndx) 8834 1.1 christos { 8835 1.1 christos case MAP_ONESYMTAB: 8836 1.1 christos shndx = elf_onesymtab (abfd); 8837 1.1 christos break; 8838 1.1 christos case MAP_DYNSYMTAB: 8839 1.1 christos shndx = elf_dynsymtab (abfd); 8840 1.3 christos break; 8841 1.1 christos case MAP_STRTAB: 8842 1.1 christos shndx = elf_strtab_sec (abfd); 8843 1.3 christos break; 8844 1.1 christos case MAP_SHSTRTAB: 8845 1.1 christos shndx = elf_shstrtab_sec (abfd); 8846 1.3 christos break; 8847 1.3 christos case MAP_SYM_SHNDX: 8848 1.1 christos if (elf_symtab_shndx_list (abfd)) 8849 1.12 christos shndx = elf_symtab_shndx_list (abfd)->ndx; 8850 1.12 christos break; 8851 1.12 christos case SHN_COMMON: 8852 1.12 christos case SHN_ABS: 8853 1.1 christos shndx = SHN_ABS; 8854 1.12 christos break; 8855 1.12 christos default: 8856 1.12 christos if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS) 8857 1.12 christos { 8858 1.12 christos if (bed->symbol_section_index) 8859 1.12 christos shndx = bed->symbol_section_index (abfd, type_ptr); 8860 1.12 christos /* Otherwise just leave the index alone. */ 8861 1.12 christos } 8862 1.12 christos else 8863 1.12 christos { 8864 1.12 christos if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE) 8865 1.12 christos _bfd_error_handler (_("%pB: \ 8866 1.12 christos Unable to handle section index %x in ELF symbol. Using ABS instead."), 8867 1.12 christos abfd, shndx); 8868 1.1 christos shndx = SHN_ABS; 8869 1.1 christos } 8870 1.1 christos break; 8871 1.1 christos } 8872 1.1 christos } 8873 1.1 christos else 8874 1.1 christos { 8875 1.1 christos shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 8876 1.1 christos 8877 1.1 christos if (shndx == SHN_BAD) 8878 1.1 christos { 8879 1.1 christos asection *sec2; 8880 1.1 christos 8881 1.1 christos /* Writing this would be a hell of a lot easier if 8882 1.1 christos we had some decent documentation on bfd, and 8883 1.1 christos knew what to expect of the library, and what to 8884 1.1 christos demand of applications. For example, it 8885 1.1 christos appears that `objcopy' might not set the 8886 1.1 christos section of a symbol to be a section that is 8887 1.9 christos actually in the output file. */ 8888 1.9 christos sec2 = bfd_get_section_by_name (abfd, sec->name); 8889 1.9 christos if (sec2 != NULL) 8890 1.1 christos shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); 8891 1.9 christos if (shndx == SHN_BAD) 8892 1.9 christos { 8893 1.9 christos /* xgettext:c-format */ 8894 1.9 christos _bfd_error_handler 8895 1.9 christos (_("unable to find equivalent output section" 8896 1.9 christos " for symbol '%s' from section '%s'"), 8897 1.1 christos syms[idx]->name ? syms[idx]->name : "<Local sym>", 8898 1.3 christos sec->name); 8899 1.1 christos bfd_set_error (bfd_error_invalid_operation); 8900 1.1 christos goto error_return; 8901 1.1 christos } 8902 1.1 christos } 8903 1.1 christos } 8904 1.1 christos 8905 1.1 christos sym.st_shndx = shndx; 8906 1.14 christos } 8907 1.1 christos 8908 1.1 christos int type; 8909 1.1 christos if ((flags & BSF_THREAD_LOCAL) != 0) 8910 1.1 christos type = STT_TLS; 8911 1.1 christos else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) 8912 1.1 christos type = STT_GNU_IFUNC; 8913 1.1 christos else if ((flags & BSF_FUNCTION) != 0) 8914 1.1 christos type = STT_FUNC; 8915 1.1 christos else if ((flags & BSF_OBJECT) != 0) 8916 1.1 christos type = STT_OBJECT; 8917 1.1 christos else if ((flags & BSF_RELC) != 0) 8918 1.1 christos type = STT_RELC; 8919 1.1 christos else if ((flags & BSF_SRELC) != 0) 8920 1.1 christos type = STT_SRELC; 8921 1.1 christos else 8922 1.1 christos type = STT_NOTYPE; 8923 1.1 christos 8924 1.1 christos if (syms[idx]->section->flags & SEC_THREAD_LOCAL) 8925 1.1 christos type = STT_TLS; 8926 1.1 christos 8927 1.1 christos /* Processor-specific types. */ 8928 1.1 christos if (type_ptr != NULL 8929 1.1 christos && bed->elf_backend_get_symbol_type) 8930 1.1 christos type = ((*bed->elf_backend_get_symbol_type) 8931 1.1 christos (&type_ptr->internal_elf_sym, type)); 8932 1.1 christos 8933 1.1 christos if (flags & BSF_SECTION_SYM) 8934 1.1 christos { 8935 1.1 christos if (flags & BSF_GLOBAL) 8936 1.1 christos sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); 8937 1.1 christos else 8938 1.1 christos sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 8939 1.1 christos } 8940 1.5 christos else if (bfd_is_com_section (syms[idx]->section)) 8941 1.5 christos { 8942 1.5 christos if (type != STT_TLS) 8943 1.5 christos { 8944 1.5 christos if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) 8945 1.5 christos type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) 8946 1.5 christos ? STT_COMMON : STT_OBJECT); 8947 1.5 christos else 8948 1.5 christos type = ((flags & BSF_ELF_COMMON) != 0 8949 1.5 christos ? STT_COMMON : STT_OBJECT); 8950 1.1 christos } 8951 1.1 christos sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); 8952 1.1 christos } 8953 1.1 christos else if (bfd_is_und_section (syms[idx]->section)) 8954 1.1 christos sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) 8955 1.1 christos ? STB_WEAK 8956 1.1 christos : STB_GLOBAL), 8957 1.1 christos type); 8958 1.1 christos else if (flags & BSF_FILE) 8959 1.1 christos sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 8960 1.1 christos else 8961 1.1 christos { 8962 1.1 christos int bind = STB_LOCAL; 8963 1.1 christos 8964 1.1 christos if (flags & BSF_LOCAL) 8965 1.1 christos bind = STB_LOCAL; 8966 1.1 christos else if (flags & BSF_GNU_UNIQUE) 8967 1.1 christos bind = STB_GNU_UNIQUE; 8968 1.1 christos else if (flags & BSF_WEAK) 8969 1.1 christos bind = STB_WEAK; 8970 1.1 christos else if (flags & BSF_GLOBAL) 8971 1.1 christos bind = STB_GLOBAL; 8972 1.1 christos 8973 1.1 christos sym.st_info = ELF_ST_INFO (bind, type); 8974 1.1 christos } 8975 1.1 christos 8976 1.1 christos if (type_ptr != NULL) 8977 1.1 christos { 8978 1.1 christos sym.st_other = type_ptr->internal_elf_sym.st_other; 8979 1.1 christos sym.st_target_internal 8980 1.1 christos = type_ptr->internal_elf_sym.st_target_internal; 8981 1.1 christos } 8982 1.1 christos else 8983 1.1 christos { 8984 1.1 christos sym.st_other = 0; 8985 1.1 christos sym.st_target_internal = 0; 8986 1.14 christos } 8987 1.14 christos 8988 1.3 christos symstrtab[outbound_syms_index].sym = sym; 8989 1.1 christos symstrtab[outbound_syms_index].dest_index = outbound_syms_index; 8990 1.1 christos outbound_syms_index++; 8991 1.3 christos } 8992 1.3 christos 8993 1.3 christos /* Finalize the .strtab section. */ 8994 1.3 christos _bfd_elf_strtab_finalize (stt); 8995 1.14 christos 8996 1.3 christos /* Swap out the .strtab section. */ 8997 1.3 christos for (idx = 0; idx < outbound_syms_index; idx++) 8998 1.14 christos { 8999 1.3 christos struct elf_sym_strtab *elfsym = &symstrtab[idx]; 9000 1.3 christos if (elfsym->sym.st_name != 0) 9001 1.12 christos elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, 9002 1.12 christos elfsym->sym.st_name); 9003 1.12 christos if (info && info->callbacks->ctf_new_symbol) 9004 1.12 christos info->callbacks->ctf_new_symbol (elfsym->dest_index, 9005 1.12 christos &elfsym->sym); 9006 1.12 christos 9007 1.3 christos /* Inform the linker of the addition of this symbol. */ 9008 1.3 christos 9009 1.3 christos bed->s->swap_symbol_out (abfd, &elfsym->sym, 9010 1.3 christos (outbound_syms 9011 1.12 christos + (elfsym->dest_index 9012 1.12 christos * bed->s->sizeof_sym)), 9013 1.12 christos NPTR_ADD (outbound_shndx, 9014 1.3 christos (elfsym->dest_index 9015 1.3 christos * sizeof (Elf_External_Sym_Shndx)))); 9016 1.3 christos } 9017 1.1 christos free (symstrtab); 9018 1.3 christos 9019 1.1 christos *sttp = stt; 9020 1.5 christos symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); 9021 1.1 christos symstrtab_hdr->sh_type = SHT_STRTAB; 9022 1.1 christos symstrtab_hdr->sh_flags = bed->elf_strtab_flags; 9023 1.1 christos symstrtab_hdr->sh_addr = 0; 9024 1.1 christos symstrtab_hdr->sh_entsize = 0; 9025 1.1 christos symstrtab_hdr->sh_link = 0; 9026 1.1 christos symstrtab_hdr->sh_info = 0; 9027 1.12 christos symstrtab_hdr->sh_addralign = 1; 9028 1.1 christos 9029 1.1 christos return true; 9030 1.1 christos } 9031 1.1 christos 9032 1.1 christos /* Return the number of bytes required to hold the symtab vector. 9033 1.1 christos 9034 1.1 christos Note that we base it on the count plus 1, since we will null terminate 9035 1.1 christos the vector allocated based on this size. However, the ELF symbol table 9036 1.1 christos always has a dummy entry as symbol #0, so it ends up even. */ 9037 1.1 christos 9038 1.1 christos long 9039 1.10 christos _bfd_elf_get_symtab_upper_bound (bfd *abfd) 9040 1.1 christos { 9041 1.1 christos bfd_size_type symcount; 9042 1.1 christos long symtab_size; 9043 1.1 christos Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; 9044 1.12 christos 9045 1.10 christos symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 9046 1.10 christos if (symcount > LONG_MAX / sizeof (asymbol *)) 9047 1.10 christos { 9048 1.10 christos bfd_set_error (bfd_error_file_too_big); 9049 1.12 christos return -1; 9050 1.12 christos } 9051 1.12 christos symtab_size = symcount * (sizeof (asymbol *)); 9052 1.12 christos if (symcount == 0) 9053 1.12 christos symtab_size = sizeof (asymbol *); 9054 1.12 christos else if (!bfd_write_p (abfd)) 9055 1.12 christos { 9056 1.12 christos ufile_ptr filesize = bfd_get_file_size (abfd); 9057 1.12 christos 9058 1.12 christos if (filesize != 0 && (unsigned long) symtab_size > filesize) 9059 1.12 christos { 9060 1.12 christos bfd_set_error (bfd_error_file_truncated); 9061 1.12 christos return -1; 9062 1.1 christos } 9063 1.1 christos } 9064 1.1 christos 9065 1.1 christos return symtab_size; 9066 1.1 christos } 9067 1.1 christos 9068 1.1 christos long 9069 1.10 christos _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) 9070 1.1 christos { 9071 1.1 christos bfd_size_type symcount; 9072 1.1 christos long symtab_size; 9073 1.1 christos Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; 9074 1.1 christos 9075 1.13 christos if (elf_dynsymtab (abfd) == 0) 9076 1.13 christos { 9077 1.13 christos /* Check if there is dynamic symbol table. */ 9078 1.13 christos symcount = elf_tdata (abfd)->dt_symtab_count; 9079 1.13 christos if (symcount) 9080 1.1 christos goto compute_symtab_size; 9081 1.1 christos 9082 1.1 christos bfd_set_error (bfd_error_invalid_operation); 9083 1.1 christos return -1; 9084 1.1 christos } 9085 1.12 christos 9086 1.10 christos symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 9087 1.10 christos if (symcount > LONG_MAX / sizeof (asymbol *)) 9088 1.10 christos { 9089 1.10 christos bfd_set_error (bfd_error_file_too_big); 9090 1.13 christos return -1; 9091 1.13 christos } 9092 1.12 christos 9093 1.12 christos compute_symtab_size: 9094 1.12 christos symtab_size = symcount * (sizeof (asymbol *)); 9095 1.12 christos if (symcount == 0) 9096 1.12 christos symtab_size = sizeof (asymbol *); 9097 1.12 christos else if (!bfd_write_p (abfd)) 9098 1.12 christos { 9099 1.12 christos ufile_ptr filesize = bfd_get_file_size (abfd); 9100 1.12 christos 9101 1.12 christos if (filesize != 0 && (unsigned long) symtab_size > filesize) 9102 1.12 christos { 9103 1.12 christos bfd_set_error (bfd_error_file_truncated); 9104 1.12 christos return -1; 9105 1.1 christos } 9106 1.1 christos } 9107 1.1 christos 9108 1.1 christos return symtab_size; 9109 1.1 christos } 9110 1.12 christos 9111 1.1 christos long 9112 1.12 christos _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect) 9113 1.12 christos { 9114 1.12 christos if (asect->reloc_count != 0 && !bfd_write_p (abfd)) 9115 1.12 christos { 9116 1.12 christos /* Sanity check reloc section size. */ 9117 1.13 christos ufile_ptr filesize = bfd_get_file_size (abfd); 9118 1.12 christos 9119 1.13 christos if (filesize != 0) 9120 1.13 christos { 9121 1.13 christos struct bfd_elf_section_data *d = elf_section_data (asect); 9122 1.13 christos bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0; 9123 1.13 christos bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0; 9124 1.13 christos 9125 1.13 christos if (rel_size + rela_size > filesize 9126 1.13 christos || rel_size + rela_size < rel_size) 9127 1.13 christos { 9128 1.13 christos bfd_set_error (bfd_error_file_truncated); 9129 1.12 christos return -1; 9130 1.12 christos } 9131 1.12 christos } 9132 1.10 christos } 9133 1.10 christos 9134 1.10 christos #if SIZEOF_LONG == SIZEOF_INT 9135 1.10 christos if (asect->reloc_count >= LONG_MAX / sizeof (arelent *)) 9136 1.10 christos { 9137 1.10 christos bfd_set_error (bfd_error_file_too_big); 9138 1.10 christos return -1; 9139 1.12 christos } 9140 1.1 christos #endif 9141 1.1 christos return (asect->reloc_count + 1L) * sizeof (arelent *); 9142 1.1 christos } 9143 1.1 christos 9144 1.1 christos /* Canonicalize the relocs. */ 9145 1.1 christos 9146 1.1 christos long 9147 1.1 christos _bfd_elf_canonicalize_reloc (bfd *abfd, 9148 1.1 christos sec_ptr section, 9149 1.1 christos arelent **relptr, 9150 1.1 christos asymbol **symbols) 9151 1.1 christos { 9152 1.1 christos arelent *tblptr; 9153 1.1 christos unsigned int i; 9154 1.12 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9155 1.1 christos 9156 1.1 christos if (! bed->s->slurp_reloc_table (abfd, section, symbols, false)) 9157 1.1 christos return -1; 9158 1.1 christos 9159 1.1 christos tblptr = section->relocation; 9160 1.1 christos for (i = 0; i < section->reloc_count; i++) 9161 1.1 christos *relptr++ = tblptr++; 9162 1.1 christos 9163 1.1 christos *relptr = NULL; 9164 1.1 christos 9165 1.1 christos return section->reloc_count; 9166 1.1 christos } 9167 1.1 christos 9168 1.1 christos long 9169 1.1 christos _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) 9170 1.12 christos { 9171 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9172 1.1 christos long symcount = bed->s->slurp_symbol_table (abfd, allocation, false); 9173 1.10 christos 9174 1.1 christos if (symcount >= 0) 9175 1.1 christos abfd->symcount = symcount; 9176 1.1 christos return symcount; 9177 1.1 christos } 9178 1.1 christos 9179 1.1 christos long 9180 1.1 christos _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, 9181 1.1 christos asymbol **allocation) 9182 1.12 christos { 9183 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9184 1.1 christos long symcount = bed->s->slurp_symbol_table (abfd, allocation, true); 9185 1.10 christos 9186 1.1 christos if (symcount >= 0) 9187 1.1 christos abfd->dynsymcount = symcount; 9188 1.1 christos return symcount; 9189 1.1 christos } 9190 1.1 christos 9191 1.1 christos /* Return the size required for the dynamic reloc entries. Any loadable 9192 1.1 christos section that was actually installed in the BFD, and has type SHT_REL 9193 1.1 christos or SHT_RELA, and uses the dynamic symbol table, is considered to be a 9194 1.1 christos dynamic reloc section. */ 9195 1.1 christos 9196 1.1 christos long 9197 1.12 christos _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) 9198 1.1 christos { 9199 1.1 christos bfd_size_type count, ext_rel_size; 9200 1.1 christos asection *s; 9201 1.1 christos 9202 1.1 christos if (elf_dynsymtab (abfd) == 0) 9203 1.1 christos { 9204 1.1 christos bfd_set_error (bfd_error_invalid_operation); 9205 1.1 christos return -1; 9206 1.10 christos } 9207 1.12 christos 9208 1.1 christos count = 1; 9209 1.1 christos ext_rel_size = 0; 9210 1.1 christos for (s = abfd->sections; s != NULL; s = s->next) 9211 1.13 christos if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) 9212 1.13 christos && (elf_section_data (s)->this_hdr.sh_type == SHT_REL 9213 1.10 christos || elf_section_data (s)->this_hdr.sh_type == SHT_RELA) 9214 1.13 christos && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0) 9215 1.13 christos { 9216 1.12 christos ext_rel_size += elf_section_data (s)->this_hdr.sh_size; 9217 1.12 christos if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size) 9218 1.12 christos { 9219 1.12 christos bfd_set_error (bfd_error_file_truncated); 9220 1.13 christos return -1; 9221 1.10 christos } 9222 1.10 christos count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr); 9223 1.10 christos if (count > LONG_MAX / sizeof (arelent *)) 9224 1.10 christos { 9225 1.10 christos bfd_set_error (bfd_error_file_too_big); 9226 1.10 christos return -1; 9227 1.12 christos } 9228 1.12 christos } 9229 1.12 christos if (count > 1 && !bfd_write_p (abfd)) 9230 1.12 christos { 9231 1.12 christos /* Sanity check reloc section sizes. */ 9232 1.12 christos ufile_ptr filesize = bfd_get_file_size (abfd); 9233 1.12 christos if (filesize != 0 && ext_rel_size > filesize) 9234 1.12 christos { 9235 1.12 christos bfd_set_error (bfd_error_file_truncated); 9236 1.12 christos return -1; 9237 1.10 christos } 9238 1.1 christos } 9239 1.1 christos return count * sizeof (arelent *); 9240 1.1 christos } 9241 1.1 christos 9242 1.1 christos /* Canonicalize the dynamic relocation entries. Note that we return the 9243 1.1 christos dynamic relocations as a single block, although they are actually 9244 1.1 christos associated with particular sections; the interface, which was 9245 1.1 christos designed for SunOS style shared libraries, expects that there is only 9246 1.1 christos one set of dynamic relocs. Any loadable section that was actually 9247 1.1 christos installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the 9248 1.1 christos dynamic symbol table, is considered to be a dynamic reloc section. */ 9249 1.1 christos 9250 1.1 christos long 9251 1.1 christos _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, 9252 1.1 christos arelent **storage, 9253 1.12 christos asymbol **syms) 9254 1.1 christos { 9255 1.1 christos bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool); 9256 1.1 christos asection *s; 9257 1.1 christos long ret; 9258 1.1 christos 9259 1.1 christos if (elf_dynsymtab (abfd) == 0) 9260 1.1 christos { 9261 1.1 christos bfd_set_error (bfd_error_invalid_operation); 9262 1.1 christos return -1; 9263 1.1 christos } 9264 1.1 christos 9265 1.1 christos slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; 9266 1.1 christos ret = 0; 9267 1.1 christos for (s = abfd->sections; s != NULL; s = s->next) 9268 1.1 christos { 9269 1.13 christos if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) 9270 1.13 christos && (elf_section_data (s)->this_hdr.sh_type == SHT_REL 9271 1.1 christos || elf_section_data (s)->this_hdr.sh_type == SHT_RELA) 9272 1.1 christos && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0) 9273 1.1 christos { 9274 1.1 christos arelent *p; 9275 1.12 christos long count, i; 9276 1.1 christos 9277 1.13 christos if (! (*slurp_relocs) (abfd, s, syms, true)) 9278 1.1 christos return -1; 9279 1.1 christos count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr); 9280 1.1 christos p = s->relocation; 9281 1.1 christos for (i = 0; i < count; i++) 9282 1.1 christos *storage++ = p++; 9283 1.1 christos ret += count; 9284 1.1 christos } 9285 1.1 christos } 9286 1.1 christos 9287 1.1 christos *storage = NULL; 9288 1.1 christos 9289 1.1 christos return ret; 9290 1.1 christos } 9291 1.1 christos 9292 1.12 christos /* Read in the version information. */ 9294 1.1 christos 9295 1.1 christos bool 9296 1.1 christos _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver) 9297 1.12 christos { 9298 1.14 christos bfd_byte *contents = NULL; 9299 1.14 christos unsigned int freeidx = 0; 9300 1.1 christos size_t amt; 9301 1.13 christos void *contents_addr = NULL; 9302 1.1 christos size_t contents_size = 0; 9303 1.1 christos 9304 1.1 christos if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL) 9305 1.1 christos { 9306 1.1 christos Elf_Internal_Shdr *hdr; 9307 1.1 christos Elf_External_Verneed *everneed; 9308 1.13 christos Elf_Internal_Verneed *iverneed; 9309 1.13 christos unsigned int i; 9310 1.1 christos bfd_byte *contents_end; 9311 1.13 christos size_t verneed_count; 9312 1.1 christos size_t verneed_size; 9313 1.13 christos 9314 1.13 christos if (elf_tdata (abfd)->dt_verneed != NULL) 9315 1.13 christos { 9316 1.13 christos hdr = NULL; 9317 1.1 christos contents = elf_tdata (abfd)->dt_verneed; 9318 1.13 christos verneed_count = elf_tdata (abfd)->dt_verneed_count; 9319 1.13 christos verneed_size = verneed_count * sizeof (Elf_External_Verneed); 9320 1.13 christos } 9321 1.13 christos else 9322 1.13 christos { 9323 1.13 christos hdr = &elf_tdata (abfd)->dynverref_hdr; 9324 1.13 christos 9325 1.13 christos if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed)) 9326 1.13 christos { 9327 1.13 christos error_return_bad_verref: 9328 1.13 christos _bfd_error_handler 9329 1.13 christos (_("%pB: .gnu.version_r invalid entry"), abfd); 9330 1.13 christos bfd_set_error (bfd_error_bad_value); 9331 1.13 christos error_return_verref: 9332 1.13 christos elf_tdata (abfd)->verref = NULL; 9333 1.3 christos elf_tdata (abfd)->cverrefs = 0; 9334 1.13 christos goto error_return; 9335 1.13 christos } 9336 1.14 christos 9337 1.14 christos if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) 9338 1.14 christos goto error_return_verref; 9339 1.13 christos contents_size = hdr->sh_size; 9340 1.13 christos contents = _bfd_mmap_temporary (abfd, contents_size, 9341 1.13 christos &contents_addr, &contents_size); 9342 1.13 christos if (contents == NULL) 9343 1.13 christos goto error_return_verref; 9344 1.13 christos 9345 1.12 christos verneed_size = hdr->sh_size; 9346 1.13 christos verneed_count = hdr->sh_info; 9347 1.13 christos } 9348 1.10 christos 9349 1.12 christos if (_bfd_mul_overflow (verneed_count, 9350 1.10 christos sizeof (Elf_Internal_Verneed), &amt)) 9351 1.10 christos { 9352 1.13 christos bfd_set_error (bfd_error_file_too_big); 9353 1.13 christos goto error_return_verref; 9354 1.13 christos } 9355 1.3 christos if (amt == 0) 9356 1.1 christos goto error_return_verref; 9357 1.1 christos elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt); 9358 1.1 christos if (elf_tdata (abfd)->verref == NULL) 9359 1.1 christos goto error_return_verref; 9360 1.13 christos 9361 1.13 christos BFD_ASSERT (sizeof (Elf_External_Verneed) 9362 1.1 christos == sizeof (Elf_External_Vernaux)); 9363 1.1 christos contents_end = (contents + verneed_size 9364 1.13 christos - sizeof (Elf_External_Verneed)); 9365 1.1 christos everneed = (Elf_External_Verneed *) contents; 9366 1.1 christos iverneed = elf_tdata (abfd)->verref; 9367 1.1 christos for (i = 0; i < verneed_count; i++, iverneed++) 9368 1.1 christos { 9369 1.1 christos Elf_External_Vernaux *evernaux; 9370 1.1 christos Elf_Internal_Vernaux *ivernaux; 9371 1.1 christos unsigned int j; 9372 1.1 christos 9373 1.1 christos _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); 9374 1.13 christos 9375 1.13 christos iverneed->vn_bfd = abfd; 9376 1.13 christos 9377 1.13 christos if (elf_use_dt_symtab_p (abfd)) 9378 1.13 christos { 9379 1.13 christos if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz) 9380 1.13 christos iverneed->vn_filename 9381 1.13 christos = elf_tdata (abfd)->dt_strtab + iverneed->vn_file; 9382 1.13 christos else 9383 1.13 christos iverneed->vn_filename = NULL; 9384 1.13 christos } 9385 1.13 christos else if (hdr == NULL) 9386 1.13 christos goto error_return_bad_verref; 9387 1.13 christos else 9388 1.1 christos iverneed->vn_filename 9389 1.3 christos = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 9390 1.1 christos iverneed->vn_file); 9391 1.1 christos if (iverneed->vn_filename == NULL) 9392 1.1 christos goto error_return_bad_verref; 9393 1.1 christos 9394 1.1 christos if (iverneed->vn_cnt == 0) 9395 1.12 christos iverneed->vn_auxptr = NULL; 9396 1.12 christos else 9397 1.12 christos { 9398 1.12 christos if (_bfd_mul_overflow (iverneed->vn_cnt, 9399 1.12 christos sizeof (Elf_Internal_Vernaux), &amt)) 9400 1.12 christos { 9401 1.1 christos bfd_set_error (bfd_error_file_too_big); 9402 1.12 christos goto error_return_verref; 9403 1.1 christos } 9404 1.1 christos iverneed->vn_auxptr = (struct elf_internal_vernaux *) 9405 1.1 christos bfd_alloc (abfd, amt); 9406 1.1 christos if (iverneed->vn_auxptr == NULL) 9407 1.1 christos goto error_return_verref; 9408 1.1 christos } 9409 1.3 christos 9410 1.1 christos if (iverneed->vn_aux 9411 1.1 christos > (size_t) (contents_end - (bfd_byte *) everneed)) 9412 1.1 christos goto error_return_bad_verref; 9413 1.1 christos 9414 1.1 christos evernaux = ((Elf_External_Vernaux *) 9415 1.1 christos ((bfd_byte *) everneed + iverneed->vn_aux)); 9416 1.1 christos ivernaux = iverneed->vn_auxptr; 9417 1.1 christos for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) 9418 1.13 christos { 9419 1.13 christos _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); 9420 1.13 christos 9421 1.13 christos if (elf_use_dt_symtab_p (abfd)) 9422 1.13 christos { 9423 1.13 christos if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz) 9424 1.13 christos ivernaux->vna_nodename 9425 1.13 christos = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name; 9426 1.13 christos else 9427 1.13 christos ivernaux->vna_nodename = NULL; 9428 1.13 christos } 9429 1.13 christos else if (hdr == NULL) 9430 1.13 christos goto error_return_bad_verref; 9431 1.13 christos else 9432 1.1 christos ivernaux->vna_nodename 9433 1.3 christos = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 9434 1.3 christos ivernaux->vna_name); 9435 1.3 christos if (ivernaux->vna_nodename == NULL) 9436 1.3 christos goto error_return_bad_verref; 9437 1.1 christos 9438 1.3 christos if (ivernaux->vna_other > freeidx) 9439 1.3 christos freeidx = ivernaux->vna_other; 9440 1.3 christos 9441 1.3 christos ivernaux->vna_nextptr = NULL; 9442 1.3 christos if (ivernaux->vna_next == 0) 9443 1.3 christos { 9444 1.1 christos iverneed->vn_cnt = j + 1; 9445 1.1 christos break; 9446 1.1 christos } 9447 1.1 christos if (j + 1 < iverneed->vn_cnt) 9448 1.1 christos ivernaux->vna_nextptr = ivernaux + 1; 9449 1.3 christos 9450 1.1 christos if (ivernaux->vna_next 9451 1.1 christos > (size_t) (contents_end - (bfd_byte *) evernaux)) 9452 1.1 christos goto error_return_bad_verref; 9453 1.1 christos 9454 1.1 christos evernaux = ((Elf_External_Vernaux *) 9455 1.3 christos ((bfd_byte *) evernaux + ivernaux->vna_next)); 9456 1.3 christos } 9457 1.3 christos 9458 1.13 christos iverneed->vn_nextref = NULL; 9459 1.1 christos if (iverneed->vn_next == 0) 9460 1.1 christos break; 9461 1.1 christos if (hdr != NULL && (i + 1 < hdr->sh_info)) 9462 1.1 christos iverneed->vn_nextref = iverneed + 1; 9463 1.3 christos 9464 1.1 christos if (iverneed->vn_next 9465 1.1 christos > (size_t) (contents_end - (bfd_byte *) everneed)) 9466 1.1 christos goto error_return_bad_verref; 9467 1.1 christos 9468 1.3 christos everneed = ((Elf_External_Verneed *) 9469 1.1 christos ((bfd_byte *) everneed + iverneed->vn_next)); 9470 1.13 christos } 9471 1.14 christos elf_tdata (abfd)->cverrefs = i; 9472 1.1 christos 9473 1.14 christos if (contents != elf_tdata (abfd)->dt_verneed) 9474 1.1 christos _bfd_munmap_temporary (contents_addr, contents_size); 9475 1.1 christos contents = NULL; 9476 1.13 christos contents_addr = NULL; 9477 1.1 christos } 9478 1.1 christos 9479 1.1 christos if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL) 9480 1.1 christos { 9481 1.1 christos Elf_Internal_Shdr *hdr; 9482 1.1 christos Elf_External_Verdef *everdef; 9483 1.1 christos Elf_Internal_Verdef *iverdef; 9484 1.1 christos Elf_Internal_Verdef *iverdefarr; 9485 1.1 christos Elf_Internal_Verdef iverdefmem; 9486 1.13 christos unsigned int i; 9487 1.13 christos unsigned int maxidx; 9488 1.1 christos bfd_byte *contents_end_def, *contents_end_aux; 9489 1.13 christos size_t verdef_count; 9490 1.3 christos size_t verdef_size; 9491 1.13 christos 9492 1.13 christos if (elf_tdata (abfd)->dt_verdef != NULL) 9493 1.13 christos { 9494 1.13 christos hdr = NULL; 9495 1.3 christos contents = elf_tdata (abfd)->dt_verdef; 9496 1.13 christos verdef_count = elf_tdata (abfd)->dt_verdef_count; 9497 1.13 christos verdef_size = verdef_count * sizeof (Elf_External_Verdef); 9498 1.13 christos } 9499 1.13 christos else 9500 1.13 christos { 9501 1.13 christos hdr = &elf_tdata (abfd)->dynverdef_hdr; 9502 1.13 christos 9503 1.13 christos if (hdr->sh_size < sizeof (Elf_External_Verdef)) 9504 1.13 christos { 9505 1.13 christos error_return_bad_verdef: 9506 1.13 christos _bfd_error_handler 9507 1.13 christos (_("%pB: .gnu.version_d invalid entry"), abfd); 9508 1.13 christos bfd_set_error (bfd_error_bad_value); 9509 1.13 christos error_return_verdef: 9510 1.13 christos elf_tdata (abfd)->verdef = NULL; 9511 1.13 christos elf_tdata (abfd)->cverdefs = 0; 9512 1.13 christos goto error_return; 9513 1.13 christos } 9514 1.14 christos 9515 1.14 christos if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) 9516 1.14 christos goto error_return_verdef; 9517 1.13 christos contents_size = hdr->sh_size; 9518 1.13 christos contents = _bfd_mmap_temporary (abfd, contents_size, 9519 1.3 christos &contents_addr, &contents_size); 9520 1.13 christos if (contents == NULL) 9521 1.13 christos goto error_return_verdef; 9522 1.13 christos 9523 1.13 christos BFD_ASSERT (sizeof (Elf_External_Verdef) 9524 1.13 christos >= sizeof (Elf_External_Verdaux)); 9525 1.13 christos 9526 1.1 christos verdef_count = hdr->sh_info; 9527 1.13 christos verdef_size = hdr->sh_size; 9528 1.13 christos } 9529 1.13 christos 9530 1.13 christos contents_end_def = (contents + verdef_size 9531 1.1 christos - sizeof (Elf_External_Verdef)); 9532 1.1 christos contents_end_aux = (contents + verdef_size 9533 1.1 christos - sizeof (Elf_External_Verdaux)); 9534 1.1 christos 9535 1.1 christos /* We know the number of entries in the section but not the maximum 9536 1.1 christos index. Therefore we have to run through all entries and find 9537 1.13 christos the maximum. */ 9538 1.1 christos everdef = (Elf_External_Verdef *) contents; 9539 1.1 christos maxidx = 0; 9540 1.1 christos for (i = 0; i < verdef_count; ++i) 9541 1.3 christos { 9542 1.3 christos _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); 9543 1.1 christos 9544 1.1 christos if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) 9545 1.1 christos goto error_return_bad_verdef; 9546 1.3 christos if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) 9547 1.3 christos maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); 9548 1.3 christos 9549 1.1 christos if (iverdefmem.vd_next == 0) 9550 1.1 christos break; 9551 1.3 christos 9552 1.1 christos if (iverdefmem.vd_next 9553 1.1 christos > (size_t) (contents_end_def - (bfd_byte *) everdef)) 9554 1.1 christos goto error_return_bad_verdef; 9555 1.1 christos 9556 1.1 christos everdef = ((Elf_External_Verdef *) 9557 1.1 christos ((bfd_byte *) everdef + iverdefmem.vd_next)); 9558 1.1 christos } 9559 1.1 christos 9560 1.1 christos if (default_imported_symver) 9561 1.1 christos { 9562 1.1 christos if (freeidx > maxidx) 9563 1.1 christos maxidx = ++freeidx; 9564 1.12 christos else 9565 1.12 christos freeidx = ++maxidx; 9566 1.12 christos } 9567 1.12 christos if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt)) 9568 1.12 christos { 9569 1.13 christos bfd_set_error (bfd_error_file_too_big); 9570 1.13 christos goto error_return_verdef; 9571 1.13 christos } 9572 1.12 christos 9573 1.1 christos if (amt == 0) 9574 1.3 christos goto error_return_verdef; 9575 1.1 christos elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); 9576 1.1 christos if (elf_tdata (abfd)->verdef == NULL) 9577 1.1 christos goto error_return_verdef; 9578 1.1 christos 9579 1.1 christos elf_tdata (abfd)->cverdefs = maxidx; 9580 1.13 christos 9581 1.1 christos everdef = (Elf_External_Verdef *) contents; 9582 1.1 christos iverdefarr = elf_tdata (abfd)->verdef; 9583 1.1 christos for (i = 0; i < verdef_count; ++i) 9584 1.1 christos { 9585 1.1 christos Elf_External_Verdaux *everdaux; 9586 1.1 christos Elf_Internal_Verdaux *iverdaux; 9587 1.1 christos unsigned int j; 9588 1.1 christos 9589 1.3 christos _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); 9590 1.1 christos 9591 1.1 christos if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) 9592 1.3 christos goto error_return_bad_verdef; 9593 1.1 christos 9594 1.1 christos iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; 9595 1.1 christos memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); 9596 1.1 christos 9597 1.1 christos iverdef->vd_bfd = abfd; 9598 1.1 christos 9599 1.1 christos if (iverdef->vd_cnt == 0) 9600 1.12 christos iverdef->vd_auxptr = NULL; 9601 1.12 christos else 9602 1.12 christos { 9603 1.12 christos if (_bfd_mul_overflow (iverdef->vd_cnt, 9604 1.12 christos sizeof (Elf_Internal_Verdaux), &amt)) 9605 1.12 christos { 9606 1.1 christos bfd_set_error (bfd_error_file_too_big); 9607 1.12 christos goto error_return_verdef; 9608 1.1 christos } 9609 1.1 christos iverdef->vd_auxptr = (struct elf_internal_verdaux *) 9610 1.1 christos bfd_alloc (abfd, amt); 9611 1.1 christos if (iverdef->vd_auxptr == NULL) 9612 1.1 christos goto error_return_verdef; 9613 1.1 christos } 9614 1.3 christos 9615 1.1 christos if (iverdef->vd_aux 9616 1.1 christos > (size_t) (contents_end_aux - (bfd_byte *) everdef)) 9617 1.1 christos goto error_return_bad_verdef; 9618 1.1 christos 9619 1.1 christos everdaux = ((Elf_External_Verdaux *) 9620 1.1 christos ((bfd_byte *) everdef + iverdef->vd_aux)); 9621 1.1 christos iverdaux = iverdef->vd_auxptr; 9622 1.1 christos for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) 9623 1.13 christos { 9624 1.13 christos _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); 9625 1.13 christos 9626 1.13 christos if (elf_use_dt_symtab_p (abfd)) 9627 1.13 christos { 9628 1.13 christos if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz) 9629 1.13 christos iverdaux->vda_nodename 9630 1.13 christos = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name; 9631 1.13 christos else 9632 1.13 christos iverdaux->vda_nodename = NULL; 9633 1.13 christos } 9634 1.13 christos else 9635 1.1 christos iverdaux->vda_nodename 9636 1.3 christos = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 9637 1.1 christos iverdaux->vda_name); 9638 1.3 christos if (iverdaux->vda_nodename == NULL) 9639 1.3 christos goto error_return_bad_verdef; 9640 1.3 christos 9641 1.3 christos iverdaux->vda_nextptr = NULL; 9642 1.3 christos if (iverdaux->vda_next == 0) 9643 1.3 christos { 9644 1.1 christos iverdef->vd_cnt = j + 1; 9645 1.1 christos break; 9646 1.1 christos } 9647 1.1 christos if (j + 1 < iverdef->vd_cnt) 9648 1.1 christos iverdaux->vda_nextptr = iverdaux + 1; 9649 1.3 christos 9650 1.1 christos if (iverdaux->vda_next 9651 1.1 christos > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) 9652 1.1 christos goto error_return_bad_verdef; 9653 1.1 christos 9654 1.1 christos everdaux = ((Elf_External_Verdaux *) 9655 1.3 christos ((bfd_byte *) everdaux + iverdaux->vda_next)); 9656 1.1 christos } 9657 1.1 christos 9658 1.1 christos iverdef->vd_nodename = NULL; 9659 1.3 christos if (iverdef->vd_cnt) 9660 1.3 christos iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; 9661 1.3 christos 9662 1.1 christos iverdef->vd_nextdef = NULL; 9663 1.1 christos if (iverdef->vd_next == 0) 9664 1.1 christos break; 9665 1.1 christos if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) 9666 1.1 christos iverdef->vd_nextdef = iverdef + 1; 9667 1.1 christos 9668 1.1 christos everdef = ((Elf_External_Verdef *) 9669 1.13 christos ((bfd_byte *) everdef + iverdef->vd_next)); 9670 1.14 christos } 9671 1.1 christos 9672 1.14 christos if (contents != elf_tdata (abfd)->dt_verdef) 9673 1.1 christos _bfd_munmap_temporary (contents_addr, contents_size); 9674 1.1 christos contents = NULL; 9675 1.1 christos contents_addr = NULL; 9676 1.1 christos } 9677 1.1 christos else if (default_imported_symver) 9678 1.1 christos { 9679 1.1 christos if (freeidx < 3) 9680 1.1 christos freeidx = 3; 9681 1.12 christos else 9682 1.12 christos freeidx++; 9683 1.12 christos 9684 1.12 christos if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt)) 9685 1.12 christos { 9686 1.13 christos bfd_set_error (bfd_error_file_too_big); 9687 1.13 christos goto error_return; 9688 1.12 christos } 9689 1.1 christos if (amt == 0) 9690 1.1 christos goto error_return; 9691 1.1 christos elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); 9692 1.1 christos if (elf_tdata (abfd)->verdef == NULL) 9693 1.1 christos goto error_return; 9694 1.1 christos 9695 1.1 christos elf_tdata (abfd)->cverdefs = freeidx; 9696 1.1 christos } 9697 1.1 christos 9698 1.1 christos /* Create a default version based on the soname. */ 9699 1.1 christos if (default_imported_symver) 9700 1.1 christos { 9701 1.3 christos Elf_Internal_Verdef *iverdef; 9702 1.1 christos Elf_Internal_Verdaux *iverdaux; 9703 1.1 christos 9704 1.1 christos iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; 9705 1.1 christos 9706 1.1 christos iverdef->vd_version = VER_DEF_CURRENT; 9707 1.1 christos iverdef->vd_flags = 0; 9708 1.1 christos iverdef->vd_ndx = freeidx; 9709 1.1 christos iverdef->vd_cnt = 1; 9710 1.1 christos 9711 1.1 christos iverdef->vd_bfd = abfd; 9712 1.1 christos 9713 1.1 christos iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); 9714 1.3 christos if (iverdef->vd_nodename == NULL) 9715 1.3 christos goto error_return_verdef; 9716 1.1 christos iverdef->vd_nextdef = NULL; 9717 1.1 christos iverdef->vd_auxptr = ((struct elf_internal_verdaux *) 9718 1.1 christos bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); 9719 1.1 christos if (iverdef->vd_auxptr == NULL) 9720 1.1 christos goto error_return_verdef; 9721 1.1 christos 9722 1.1 christos iverdaux = iverdef->vd_auxptr; 9723 1.12 christos iverdaux->vda_nodename = iverdef->vd_nodename; 9724 1.1 christos } 9725 1.1 christos 9726 1.13 christos return true; 9727 1.13 christos 9728 1.14 christos error_return: 9729 1.12 christos if (contents != elf_tdata (abfd)->dt_verneed 9730 1.1 christos && contents != elf_tdata (abfd)->dt_verdef) 9731 1.1 christos _bfd_munmap_temporary (contents_addr, contents_size); 9732 1.1 christos return false; 9733 1.1 christos } 9734 1.1 christos 9735 1.1 christos asymbol * 9737 1.10 christos _bfd_elf_make_empty_symbol (bfd *abfd) 9738 1.1 christos { 9739 1.1 christos elf_symbol_type *newsym; 9740 1.3 christos 9741 1.3 christos newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym)); 9742 1.1 christos if (!newsym) 9743 1.1 christos return NULL; 9744 1.1 christos newsym->symbol.the_bfd = abfd; 9745 1.1 christos return &newsym->symbol; 9746 1.1 christos } 9747 1.1 christos 9748 1.1 christos void 9749 1.1 christos _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, 9750 1.1 christos asymbol *symbol, 9751 1.1 christos symbol_info *ret) 9752 1.1 christos { 9753 1.1 christos bfd_symbol_info (symbol, ret); 9754 1.1 christos } 9755 1.1 christos 9756 1.12 christos /* Return whether a symbol name implies a local symbol. Most targets 9757 1.1 christos use this function for the is_local_label_name entry point, but some 9758 1.1 christos override it. */ 9759 1.1 christos 9760 1.1 christos bool 9761 1.1 christos _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, 9762 1.12 christos const char *name) 9763 1.1 christos { 9764 1.1 christos /* Normal local symbols start with ``.L''. */ 9765 1.1 christos if (name[0] == '.' && name[1] == 'L') 9766 1.1 christos return true; 9767 1.12 christos 9768 1.1 christos /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate 9769 1.1 christos DWARF debugging symbols starting with ``..''. */ 9770 1.1 christos if (name[0] == '.' && name[1] == '.') 9771 1.1 christos return true; 9772 1.1 christos 9773 1.1 christos /* gcc will sometimes generate symbols beginning with ``_.L_'' when 9774 1.1 christos emitting DWARF debugging output. I suspect this is actually a 9775 1.1 christos small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call 9776 1.12 christos ASM_GENERATE_INTERNAL_LABEL, and this causes the leading 9777 1.1 christos underscore to be emitted on some ELF targets). For ease of use, 9778 1.3 christos we treat such symbols as local. */ 9779 1.3 christos if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') 9780 1.3 christos return true; 9781 1.3 christos 9782 1.9 christos /* Treat assembler generated fake symbols, dollar local labels and 9783 1.3 christos forward-backward labels (aka local labels) as locals. 9784 1.3 christos These labels have the form: 9785 1.3 christos 9786 1.3 christos L0^A.* (fake symbols) 9787 1.3 christos 9788 1.3 christos [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) 9789 1.3 christos 9790 1.12 christos Versions which start with .L will have already been matched above, 9791 1.3 christos so we only need to match the rest. */ 9792 1.3 christos if (name[0] == 'L' && ISDIGIT (name[1])) 9793 1.3 christos { 9794 1.3 christos bool ret = false; 9795 1.3 christos const char * p; 9796 1.3 christos char c; 9797 1.3 christos 9798 1.3 christos for (p = name + 2; (c = *p); p++) 9799 1.3 christos { 9800 1.12 christos if (c == 1 || c == 2) 9801 1.3 christos { 9802 1.3 christos if (c == 1 && p == name + 2) 9803 1.3 christos /* A fake symbol. */ 9804 1.3 christos return true; 9805 1.3 christos 9806 1.3 christos /* FIXME: We are being paranoid here and treating symbols like 9807 1.12 christos L0^Bfoo as if there were non-local, on the grounds that the 9808 1.3 christos assembler will never generate them. But can any symbol 9809 1.3 christos containing an ASCII value in the range 1-31 ever be anything 9810 1.3 christos other than some kind of local ? */ 9811 1.3 christos ret = true; 9812 1.12 christos } 9813 1.3 christos 9814 1.3 christos if (! ISDIGIT (c)) 9815 1.3 christos { 9816 1.3 christos ret = false; 9817 1.3 christos break; 9818 1.3 christos } 9819 1.12 christos } 9820 1.1 christos return ret; 9821 1.1 christos } 9822 1.1 christos 9823 1.1 christos return false; 9824 1.1 christos } 9825 1.1 christos 9826 1.1 christos alent * 9827 1.1 christos _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, 9828 1.1 christos asymbol *symbol ATTRIBUTE_UNUSED) 9829 1.1 christos { 9830 1.12 christos abort (); 9831 1.1 christos return NULL; 9832 1.1 christos } 9833 1.1 christos 9834 1.1 christos bool 9835 1.1 christos _bfd_elf_set_arch_mach (bfd *abfd, 9836 1.1 christos enum bfd_architecture arch, 9837 1.1 christos unsigned long machine) 9838 1.1 christos { 9839 1.1 christos /* If this isn't the right architecture for this backend, and this 9840 1.12 christos isn't the generic backend, fail. */ 9841 1.1 christos if (arch != get_elf_backend_data (abfd)->arch 9842 1.1 christos && arch != bfd_arch_unknown 9843 1.1 christos && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) 9844 1.1 christos return false; 9845 1.1 christos 9846 1.1 christos return bfd_default_set_arch_mach (abfd, arch, machine); 9847 1.1 christos } 9848 1.12 christos 9849 1.1 christos /* Find the nearest line to a particular section and offset, 9850 1.3 christos for error reporting. */ 9851 1.1 christos 9852 1.1 christos bool 9853 1.1 christos _bfd_elf_find_nearest_line (bfd *abfd, 9854 1.1 christos asymbol **symbols, 9855 1.3 christos asection *section, 9856 1.3 christos bfd_vma offset, 9857 1.1 christos const char **filename_ptr, 9858 1.13 christos const char **functionname_ptr, 9859 1.13 christos unsigned int *line_ptr, 9860 1.13 christos unsigned int *discriminator_ptr) 9861 1.13 christos { 9862 1.13 christos return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section, 9863 1.13 christos offset, filename_ptr, 9864 1.13 christos functionname_ptr, line_ptr, 9865 1.13 christos discriminator_ptr); 9866 1.13 christos } 9867 1.13 christos 9868 1.13 christos /* Find the nearest line to a particular section and offset, 9869 1.13 christos for error reporting. ALT_BFD representing a .gnu_debugaltlink file 9870 1.13 christos can be optionally specified. */ 9871 1.13 christos 9872 1.13 christos bool 9873 1.13 christos _bfd_elf_find_nearest_line_with_alt (bfd *abfd, 9874 1.13 christos const char *alt_filename, 9875 1.13 christos asymbol **symbols, 9876 1.13 christos asection *section, 9877 1.13 christos bfd_vma offset, 9878 1.13 christos const char **filename_ptr, 9879 1.12 christos const char **functionname_ptr, 9880 1.1 christos unsigned int *line_ptr, 9881 1.13 christos unsigned int *discriminator_ptr) 9882 1.13 christos { 9883 1.13 christos bool found; 9884 1.13 christos 9885 1.13 christos if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL, 9886 1.13 christos section, offset, filename_ptr, 9887 1.12 christos functionname_ptr, line_ptr, 9888 1.10 christos discriminator_ptr, 9889 1.10 christos dwarf_debug_sections, 9890 1.10 christos &elf_tdata (abfd)->dwarf2_find_line_info)) 9891 1.1 christos return true; 9892 1.1 christos 9893 1.3 christos if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, 9894 1.3 christos filename_ptr, functionname_ptr, line_ptr)) 9895 1.3 christos { 9896 1.12 christos if (!*functionname_ptr) 9897 1.1 christos _bfd_elf_find_function (abfd, symbols, section, offset, 9898 1.1 christos *filename_ptr ? NULL : filename_ptr, 9899 1.1 christos functionname_ptr); 9900 1.1 christos return true; 9901 1.1 christos } 9902 1.1 christos 9903 1.12 christos if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, 9904 1.1 christos &found, filename_ptr, 9905 1.12 christos functionname_ptr, line_ptr, 9906 1.1 christos &elf_tdata (abfd)->line_info)) 9907 1.1 christos return false; 9908 1.12 christos if (found && (*functionname_ptr || *line_ptr)) 9909 1.1 christos return true; 9910 1.3 christos 9911 1.3 christos if (symbols == NULL) 9912 1.12 christos return false; 9913 1.1 christos 9914 1.1 christos if (! _bfd_elf_find_function (abfd, symbols, section, offset, 9915 1.12 christos filename_ptr, functionname_ptr)) 9916 1.1 christos return false; 9917 1.1 christos 9918 1.1 christos *line_ptr = 0; 9919 1.1 christos return true; 9920 1.12 christos } 9921 1.1 christos 9922 1.1 christos /* Find the line for a symbol. */ 9923 1.1 christos 9924 1.13 christos bool 9925 1.3 christos _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, 9926 1.3 christos const char **filename_ptr, unsigned int *line_ptr) 9927 1.10 christos { 9928 1.13 christos struct elf_obj_tdata *tdata = elf_tdata (abfd); 9929 1.1 christos return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, 9930 1.1 christos filename_ptr, NULL, line_ptr, NULL, 9931 1.1 christos dwarf_debug_sections, 9932 1.1 christos &tdata->dwarf2_find_line_info); 9933 1.1 christos } 9934 1.1 christos 9935 1.1 christos /* After a call to bfd_find_nearest_line, successive calls to 9936 1.1 christos bfd_find_inliner_info can be used to get source information about 9937 1.12 christos each level of function inlining that terminated at the address 9938 1.1 christos passed to bfd_find_nearest_line. Currently this is only supported 9939 1.1 christos for DWARF2 with appropriate DWARF3 extensions. */ 9940 1.1 christos 9941 1.1 christos bool 9942 1.1 christos _bfd_elf_find_inliner_info (bfd *abfd, 9943 1.13 christos const char **filename_ptr, 9944 1.13 christos const char **functionname_ptr, 9945 1.13 christos unsigned int *line_ptr) 9946 1.13 christos { 9947 1.1 christos struct elf_obj_tdata *tdata = elf_tdata (abfd); 9948 1.1 christos return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, 9949 1.1 christos functionname_ptr, line_ptr, 9950 1.1 christos &tdata->dwarf2_find_line_info); 9951 1.1 christos } 9952 1.1 christos 9953 1.1 christos int 9954 1.1 christos _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) 9955 1.3 christos { 9956 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9957 1.3 christos int ret = bed->s->sizeof_ehdr; 9958 1.1 christos 9959 1.1 christos if (!bfd_link_relocatable (info)) 9960 1.1 christos { 9961 1.1 christos bfd_size_type phdr_size = elf_program_header_size (abfd); 9962 1.1 christos 9963 1.1 christos if (phdr_size == (bfd_size_type) -1) 9964 1.3 christos { 9965 1.1 christos struct elf_segment_map *m; 9966 1.1 christos 9967 1.1 christos phdr_size = 0; 9968 1.1 christos for (m = elf_seg_map (abfd); m != NULL; m = m->next) 9969 1.1 christos phdr_size += bed->s->sizeof_phdr; 9970 1.1 christos 9971 1.3 christos if (phdr_size == 0) 9972 1.1 christos phdr_size = get_program_header_size (abfd, info); 9973 1.1 christos } 9974 1.1 christos 9975 1.1 christos elf_program_header_size (abfd) = phdr_size; 9976 1.1 christos ret += phdr_size; 9977 1.1 christos } 9978 1.12 christos 9979 1.1 christos return ret; 9980 1.1 christos } 9981 1.1 christos 9982 1.1 christos bool 9983 1.1 christos _bfd_elf_set_section_contents (bfd *abfd, 9984 1.1 christos sec_ptr section, 9985 1.1 christos const void *location, 9986 1.1 christos file_ptr offset, 9987 1.1 christos bfd_size_type count) 9988 1.1 christos { 9989 1.12 christos Elf_Internal_Shdr *hdr; 9990 1.1 christos 9991 1.3 christos if (! abfd->output_has_begun 9992 1.12 christos && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) 9993 1.3 christos return false; 9994 1.1 christos 9995 1.3 christos if (!count) 9996 1.3 christos return true; 9997 1.12 christos 9998 1.12 christos hdr = &elf_section_data (section)->this_hdr; 9999 1.10 christos if (hdr->sh_offset == (file_ptr) -1) 10000 1.10 christos { 10001 1.10 christos unsigned char *contents; 10002 1.12 christos 10003 1.12 christos if (bfd_section_is_ctf (section)) 10004 1.12 christos /* Nothing to do with this section: the contents are generated 10005 1.12 christos later. */ 10006 1.12 christos return true; 10007 1.13 christos 10008 1.13 christos if ((offset + count) > hdr->sh_size) 10009 1.12 christos { 10010 1.12 christos _bfd_error_handler 10011 1.12 christos (_("%pB:%pA: error: attempting to write" 10012 1.12 christos " over the end of the section"), 10013 1.12 christos abfd, section); 10014 1.12 christos 10015 1.12 christos bfd_set_error (bfd_error_invalid_operation); 10016 1.12 christos return false; 10017 1.12 christos } 10018 1.12 christos 10019 1.13 christos contents = hdr->contents; 10020 1.13 christos if (contents == NULL) 10021 1.12 christos { 10022 1.12 christos _bfd_error_handler 10023 1.12 christos (_("%pB:%pA: error: attempting to write" 10024 1.12 christos " section into an empty buffer"), 10025 1.12 christos abfd, section); 10026 1.10 christos 10027 1.3 christos bfd_set_error (bfd_error_invalid_operation); 10028 1.12 christos return false; 10029 1.3 christos } 10030 1.1 christos 10031 1.12 christos memcpy (contents + offset, location, count); 10032 1.12 christos return true; 10033 1.1 christos } 10034 1.1 christos 10035 1.12 christos return _bfd_generic_set_section_contents (abfd, section, 10036 1.1 christos location, offset, count); 10037 1.1 christos } 10038 1.1 christos 10039 1.1 christos bool 10040 1.1 christos _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, 10041 1.12 christos arelent *cache_ptr ATTRIBUTE_UNUSED, 10042 1.1 christos Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) 10043 1.1 christos { 10044 1.1 christos abort (); 10045 1.1 christos return false; 10046 1.12 christos } 10047 1.1 christos 10048 1.1 christos /* Try to convert a non-ELF reloc into an ELF one. */ 10049 1.1 christos 10050 1.1 christos bool 10051 1.1 christos _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) 10052 1.1 christos { 10053 1.1 christos /* Check whether we really have an ELF howto. */ 10054 1.1 christos 10055 1.1 christos if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) 10056 1.1 christos { 10057 1.1 christos bfd_reloc_code_real_type code; 10058 1.1 christos reloc_howto_type *howto; 10059 1.1 christos 10060 1.1 christos /* Alien reloc: Try to determine its type to replace it with an 10061 1.1 christos equivalent ELF reloc. */ 10062 1.1 christos 10063 1.1 christos if (areloc->howto->pc_relative) 10064 1.1 christos { 10065 1.1 christos switch (areloc->howto->bitsize) 10066 1.1 christos { 10067 1.1 christos case 8: 10068 1.1 christos code = BFD_RELOC_8_PCREL; 10069 1.1 christos break; 10070 1.1 christos case 12: 10071 1.1 christos code = BFD_RELOC_12_PCREL; 10072 1.1 christos break; 10073 1.1 christos case 16: 10074 1.1 christos code = BFD_RELOC_16_PCREL; 10075 1.1 christos break; 10076 1.1 christos case 24: 10077 1.1 christos code = BFD_RELOC_24_PCREL; 10078 1.1 christos break; 10079 1.1 christos case 32: 10080 1.1 christos code = BFD_RELOC_32_PCREL; 10081 1.1 christos break; 10082 1.1 christos case 64: 10083 1.1 christos code = BFD_RELOC_64_PCREL; 10084 1.1 christos break; 10085 1.1 christos default: 10086 1.1 christos goto fail; 10087 1.10 christos } 10088 1.1 christos 10089 1.1 christos howto = bfd_reloc_type_lookup (abfd, code); 10090 1.1 christos 10091 1.1 christos if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset) 10092 1.1 christos { 10093 1.1 christos if (howto->pcrel_offset) 10094 1.1 christos areloc->addend += areloc->address; 10095 1.1 christos else 10096 1.1 christos areloc->addend -= areloc->address; /* addend is unsigned!! */ 10097 1.1 christos } 10098 1.1 christos } 10099 1.1 christos else 10100 1.1 christos { 10101 1.1 christos switch (areloc->howto->bitsize) 10102 1.1 christos { 10103 1.1 christos case 8: 10104 1.1 christos code = BFD_RELOC_8; 10105 1.1 christos break; 10106 1.1 christos case 14: 10107 1.1 christos code = BFD_RELOC_14; 10108 1.1 christos break; 10109 1.1 christos case 16: 10110 1.1 christos code = BFD_RELOC_16; 10111 1.1 christos break; 10112 1.1 christos case 26: 10113 1.1 christos code = BFD_RELOC_26; 10114 1.1 christos break; 10115 1.1 christos case 32: 10116 1.1 christos code = BFD_RELOC_32; 10117 1.1 christos break; 10118 1.1 christos case 64: 10119 1.1 christos code = BFD_RELOC_64; 10120 1.1 christos break; 10121 1.1 christos default: 10122 1.1 christos goto fail; 10123 1.1 christos } 10124 1.1 christos 10125 1.1 christos howto = bfd_reloc_type_lookup (abfd, code); 10126 1.1 christos } 10127 1.1 christos 10128 1.1 christos if (howto) 10129 1.1 christos areloc->howto = howto; 10130 1.12 christos else 10131 1.1 christos goto fail; 10132 1.1 christos } 10133 1.9 christos 10134 1.9 christos return true; 10135 1.9 christos 10136 1.10 christos fail: 10137 1.12 christos /* xgettext:c-format */ 10138 1.1 christos _bfd_error_handler (_("%pB: %s unsupported"), 10139 1.1 christos abfd, areloc->howto->name); 10140 1.12 christos bfd_set_error (bfd_error_sorry); 10141 1.13 christos return false; 10142 1.1 christos } 10143 1.13 christos 10144 1.13 christos bool 10145 1.13 christos _bfd_elf_free_cached_info (bfd *abfd) 10146 1.13 christos { 10147 1.13 christos struct elf_obj_tdata *tdata; 10148 1.1 christos 10149 1.13 christos if ((bfd_get_format (abfd) == bfd_object 10150 1.1 christos || bfd_get_format (abfd) == bfd_core) 10151 1.1 christos && (tdata = elf_tdata (abfd)) != NULL) 10152 1.13 christos { 10153 1.13 christos if (tdata->o != NULL && elf_shstrtab (abfd) != NULL) 10154 1.14 christos _bfd_elf_strtab_free (elf_shstrtab (abfd)); 10155 1.14 christos _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); 10156 1.14 christos _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info); 10157 1.14 christos _bfd_stab_cleanup (abfd, &tdata->line_info); 10158 1.14 christos for (asection *sec = abfd->sections; sec != NULL; sec = sec->next) 10159 1.14 christos { 10160 1.14 christos _bfd_elf_munmap_section_contents (sec, sec->contents); 10161 1.14 christos if (!sec->alloced) 10162 1.14 christos { 10163 1.14 christos free (elf_section_data (sec)->this_hdr.contents); 10164 1.14 christos elf_section_data (sec)->this_hdr.contents = NULL; 10165 1.14 christos } 10166 1.14 christos free (elf_section_data (sec)->relocs); 10167 1.14 christos elf_section_data (sec)->relocs = NULL; 10168 1.14 christos if (sec->sec_info_type == SEC_INFO_TYPE_EH_FRAME) 10169 1.14 christos { 10170 1.14 christos struct eh_frame_sec_info *sec_info 10171 1.14 christos = elf_section_data (sec)->sec_info; 10172 1.14 christos free (sec_info->cies); 10173 1.1 christos } 10174 1.1 christos } 10175 1.13 christos free (tdata->symtab_hdr.contents); 10176 1.1 christos tdata->symtab_hdr.contents = NULL; 10177 1.1 christos } 10178 1.1 christos 10179 1.1 christos return _bfd_generic_bfd_free_cached_info (abfd); 10180 1.1 christos } 10181 1.1 christos 10182 1.1 christos /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY 10183 1.1 christos in the relocation's offset. Thus we cannot allow any sort of sanity 10184 1.1 christos range-checking to interfere. There is nothing else to do in processing 10185 1.1 christos this reloc. */ 10186 1.1 christos 10187 1.1 christos bfd_reloc_status_type 10188 1.1 christos _bfd_elf_rel_vtable_reloc_fn 10189 1.1 christos (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, 10190 1.1 christos struct bfd_symbol *symbol ATTRIBUTE_UNUSED, 10191 1.1 christos void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, 10192 1.1 christos bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) 10193 1.1 christos { 10194 1.1 christos return bfd_reloc_ok; 10195 1.1 christos } 10196 1.1 christos 10197 1.1 christos /* Elf core file support. Much of this only works on native 10199 1.1 christos toolchains, since we rely on knowing the 10200 1.1 christos machine-dependent procfs structure in order to pick 10201 1.1 christos out details about the corefile. */ 10202 1.1 christos 10203 1.1 christos #ifdef HAVE_SYS_PROCFS_H 10204 1.1 christos # include <sys/procfs.h> 10205 1.1 christos #endif 10206 1.1 christos 10207 1.1 christos /* Return a PID that identifies a "thread" for threaded cores, or the 10208 1.1 christos PID of the main process for non-threaded cores. */ 10209 1.1 christos 10210 1.3 christos static int 10211 1.1 christos elfcore_make_pid (bfd *abfd) 10212 1.3 christos { 10213 1.1 christos int pid; 10214 1.1 christos 10215 1.1 christos pid = elf_tdata (abfd)->core->lwpid; 10216 1.1 christos if (pid == 0) 10217 1.13 christos pid = elf_tdata (abfd)->core->pid; 10218 1.13 christos 10219 1.13 christos return pid; 10220 1.1 christos } 10221 1.12 christos 10222 1.1 christos /* If there isn't a section called NAME, make one, using data from 10223 1.1 christos SECT. Note, this function will generate a reference to NAME, so 10224 1.1 christos you shouldn't deallocate or overwrite it. */ 10225 1.1 christos 10226 1.1 christos static bool 10227 1.12 christos elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) 10228 1.1 christos { 10229 1.1 christos asection *sect2; 10230 1.1 christos 10231 1.12 christos if (bfd_get_section_by_name (abfd, name) != NULL) 10232 1.1 christos return true; 10233 1.1 christos 10234 1.1 christos sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); 10235 1.1 christos if (sect2 == NULL) 10236 1.12 christos return false; 10237 1.1 christos 10238 1.1 christos sect2->size = sect->size; 10239 1.1 christos sect2->filepos = sect->filepos; 10240 1.1 christos sect2->alignment_power = sect->alignment_power; 10241 1.1 christos return true; 10242 1.1 christos } 10243 1.1 christos 10244 1.1 christos /* Create a pseudosection containing SIZE bytes at FILEPOS. This 10245 1.9 christos actually creates up to two pseudosections: 10246 1.12 christos - For the single-threaded case, a section named NAME, unless 10247 1.1 christos such a section already exists. 10248 1.1 christos - For the multi-threaded case, a section named "NAME/PID", where 10249 1.1 christos PID is elfcore_make_pid (abfd). 10250 1.1 christos Both pseudosections have identical contents. */ 10251 1.1 christos bool 10252 1.1 christos _bfd_elfcore_make_pseudosection (bfd *abfd, 10253 1.1 christos char *name, 10254 1.1 christos size_t size, 10255 1.1 christos ufile_ptr filepos) 10256 1.1 christos { 10257 1.1 christos char buf[100]; 10258 1.1 christos char *threaded_name; 10259 1.1 christos size_t len; 10260 1.1 christos asection *sect; 10261 1.1 christos 10262 1.1 christos /* Build the section name. */ 10263 1.12 christos 10264 1.1 christos sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); 10265 1.1 christos len = strlen (buf) + 1; 10266 1.1 christos threaded_name = (char *) bfd_alloc (abfd, len); 10267 1.1 christos if (threaded_name == NULL) 10268 1.1 christos return false; 10269 1.12 christos memcpy (threaded_name, buf, len); 10270 1.1 christos 10271 1.1 christos sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, 10272 1.1 christos SEC_HAS_CONTENTS); 10273 1.1 christos if (sect == NULL) 10274 1.1 christos return false; 10275 1.1 christos sect->size = size; 10276 1.1 christos sect->filepos = filepos; 10277 1.12 christos sect->alignment_power = 2; 10278 1.5 christos 10279 1.10 christos return elfcore_maybe_make_sect (abfd, name, sect); 10280 1.5 christos } 10281 1.5 christos 10282 1.10 christos static bool 10283 1.5 christos elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note, 10284 1.5 christos size_t offs) 10285 1.12 christos { 10286 1.10 christos asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", 10287 1.5 christos SEC_HAS_CONTENTS); 10288 1.5 christos 10289 1.5 christos if (sect == NULL) 10290 1.5 christos return false; 10291 1.12 christos 10292 1.5 christos sect->size = note->descsz - offs; 10293 1.5 christos sect->filepos = note->descpos + offs; 10294 1.1 christos sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; 10295 1.1 christos 10296 1.1 christos return true; 10297 1.1 christos } 10298 1.1 christos 10299 1.1 christos /* prstatus_t exists on: 10300 1.1 christos solaris 2.5+ 10301 1.1 christos linux 2.[01] + glibc 10302 1.12 christos unixware 4.2 10303 1.1 christos */ 10304 1.1 christos 10305 1.1 christos #if defined (HAVE_PRSTATUS_T) 10306 1.1 christos 10307 1.1 christos static bool 10308 1.1 christos elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) 10309 1.1 christos { 10310 1.1 christos size_t size; 10311 1.1 christos int offset; 10312 1.1 christos 10313 1.1 christos if (note->descsz == sizeof (prstatus_t)) 10314 1.1 christos { 10315 1.1 christos prstatus_t prstat; 10316 1.1 christos 10317 1.1 christos size = sizeof (prstat.pr_reg); 10318 1.3 christos offset = offsetof (prstatus_t, pr_reg); 10319 1.3 christos memcpy (&prstat, note->descdata, sizeof (prstat)); 10320 1.3 christos 10321 1.3 christos /* Do not overwrite the core signal if it 10322 1.1 christos has already been set by another thread. */ 10323 1.1 christos if (elf_tdata (abfd)->core->signal == 0) 10324 1.1 christos elf_tdata (abfd)->core->signal = prstat.pr_cursig; 10325 1.1 christos if (elf_tdata (abfd)->core->pid == 0) 10326 1.1 christos elf_tdata (abfd)->core->pid = prstat.pr_pid; 10327 1.1 christos 10328 1.1 christos /* pr_who exists on: 10329 1.1 christos solaris 2.5+ 10330 1.3 christos unixware 4.2 10331 1.1 christos pr_who doesn't exist on: 10332 1.3 christos linux 2.[01] 10333 1.1 christos */ 10334 1.1 christos #if defined (HAVE_PRSTATUS_T_PR_WHO) 10335 1.1 christos elf_tdata (abfd)->core->lwpid = prstat.pr_who; 10336 1.1 christos #else 10337 1.1 christos elf_tdata (abfd)->core->lwpid = prstat.pr_pid; 10338 1.1 christos #endif 10339 1.1 christos } 10340 1.1 christos #if defined (HAVE_PRSTATUS32_T) 10341 1.1 christos else if (note->descsz == sizeof (prstatus32_t)) 10342 1.1 christos { 10343 1.1 christos /* 64-bit host, 32-bit corefile */ 10344 1.1 christos prstatus32_t prstat; 10345 1.1 christos 10346 1.1 christos size = sizeof (prstat.pr_reg); 10347 1.3 christos offset = offsetof (prstatus32_t, pr_reg); 10348 1.3 christos memcpy (&prstat, note->descdata, sizeof (prstat)); 10349 1.3 christos 10350 1.3 christos /* Do not overwrite the core signal if it 10351 1.1 christos has already been set by another thread. */ 10352 1.1 christos if (elf_tdata (abfd)->core->signal == 0) 10353 1.1 christos elf_tdata (abfd)->core->signal = prstat.pr_cursig; 10354 1.1 christos if (elf_tdata (abfd)->core->pid == 0) 10355 1.1 christos elf_tdata (abfd)->core->pid = prstat.pr_pid; 10356 1.1 christos 10357 1.1 christos /* pr_who exists on: 10358 1.1 christos solaris 2.5+ 10359 1.3 christos unixware 4.2 10360 1.1 christos pr_who doesn't exist on: 10361 1.3 christos linux 2.[01] 10362 1.1 christos */ 10363 1.1 christos #if defined (HAVE_PRSTATUS32_T_PR_WHO) 10364 1.1 christos elf_tdata (abfd)->core->lwpid = prstat.pr_who; 10365 1.1 christos #else 10366 1.1 christos elf_tdata (abfd)->core->lwpid = prstat.pr_pid; 10367 1.1 christos #endif 10368 1.1 christos } 10369 1.12 christos #endif /* HAVE_PRSTATUS32_T */ 10370 1.1 christos else 10371 1.1 christos { 10372 1.1 christos /* Fail - we don't know how to handle any other 10373 1.1 christos note size (ie. data object type). */ 10374 1.1 christos return true; 10375 1.1 christos } 10376 1.1 christos 10377 1.1 christos /* Make a ".reg/999" section and a ".reg" section. */ 10378 1.1 christos return _bfd_elfcore_make_pseudosection (abfd, ".reg", 10379 1.12 christos size, note->descpos + offset); 10380 1.1 christos } 10381 1.1 christos #endif /* defined (HAVE_PRSTATUS_T) */ 10382 1.1 christos 10383 1.1 christos /* Create a pseudosection containing the exact contents of NOTE. */ 10384 1.1 christos static bool 10385 1.1 christos elfcore_make_note_pseudosection (bfd *abfd, 10386 1.1 christos char *name, 10387 1.1 christos Elf_Internal_Note *note) 10388 1.1 christos { 10389 1.1 christos return _bfd_elfcore_make_pseudosection (abfd, name, 10390 1.1 christos note->descsz, note->descpos); 10391 1.1 christos } 10392 1.12 christos 10393 1.1 christos /* There isn't a consistent prfpregset_t across platforms, 10394 1.1 christos but it doesn't matter, because we don't have to pick this 10395 1.1 christos data structure apart. */ 10396 1.1 christos 10397 1.1 christos static bool 10398 1.1 christos elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) 10399 1.1 christos { 10400 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg2", note); 10401 1.1 christos } 10402 1.12 christos 10403 1.1 christos /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note 10404 1.1 christos type of NT_PRXFPREG. Just include the whole note's contents 10405 1.1 christos literally. */ 10406 1.1 christos 10407 1.1 christos static bool 10408 1.1 christos elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) 10409 1.1 christos { 10410 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); 10411 1.1 christos } 10412 1.12 christos 10413 1.1 christos /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" 10414 1.1 christos with a note type of NT_X86_XSTATE. Just include the whole note's 10415 1.1 christos contents literally. */ 10416 1.1 christos 10417 1.1 christos static bool 10418 1.12 christos elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) 10419 1.14 christos { 10420 1.14 christos return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); 10421 1.14 christos } 10422 1.14 christos 10423 1.14 christos static bool 10424 1.14 christos elfcore_grok_sspreg (bfd *abfd, Elf_Internal_Note *note) 10425 1.1 christos { 10426 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-ssp", note); 10427 1.1 christos } 10428 1.1 christos 10429 1.1 christos static bool 10430 1.12 christos elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) 10431 1.1 christos { 10432 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); 10433 1.1 christos } 10434 1.1 christos 10435 1.1 christos static bool 10436 1.12 christos elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) 10437 1.10 christos { 10438 1.10 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); 10439 1.10 christos } 10440 1.10 christos 10441 1.10 christos static bool 10442 1.12 christos elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note) 10443 1.10 christos { 10444 1.10 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note); 10445 1.10 christos } 10446 1.10 christos 10447 1.10 christos static bool 10448 1.12 christos elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note) 10449 1.10 christos { 10450 1.10 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note); 10451 1.10 christos } 10452 1.10 christos 10453 1.10 christos static bool 10454 1.12 christos elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note) 10455 1.10 christos { 10456 1.10 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note); 10457 1.10 christos } 10458 1.10 christos 10459 1.10 christos static bool 10460 1.12 christos elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note) 10461 1.10 christos { 10462 1.10 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note); 10463 1.10 christos } 10464 1.10 christos 10465 1.10 christos static bool 10466 1.12 christos elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note) 10467 1.10 christos { 10468 1.10 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note); 10469 1.10 christos } 10470 1.10 christos 10471 1.10 christos static bool 10472 1.12 christos elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note) 10473 1.10 christos { 10474 1.10 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note); 10475 1.10 christos } 10476 1.10 christos 10477 1.10 christos static bool 10478 1.12 christos elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note) 10479 1.10 christos { 10480 1.10 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note); 10481 1.10 christos } 10482 1.10 christos 10483 1.10 christos static bool 10484 1.12 christos elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note) 10485 1.10 christos { 10486 1.10 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note); 10487 1.10 christos } 10488 1.10 christos 10489 1.10 christos static bool 10490 1.12 christos elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note) 10491 1.10 christos { 10492 1.10 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note); 10493 1.10 christos } 10494 1.10 christos 10495 1.10 christos static bool 10496 1.12 christos elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note) 10497 1.10 christos { 10498 1.10 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note); 10499 1.10 christos } 10500 1.10 christos 10501 1.10 christos static bool 10502 1.12 christos elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note) 10503 1.10 christos { 10504 1.10 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note); 10505 1.10 christos } 10506 1.10 christos 10507 1.10 christos static bool 10508 1.12 christos elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note) 10509 1.10 christos { 10510 1.10 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note); 10511 1.10 christos } 10512 1.10 christos 10513 1.10 christos static bool 10514 1.12 christos elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note) 10515 1.1 christos { 10516 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note); 10517 1.1 christos } 10518 1.1 christos 10519 1.1 christos static bool 10520 1.12 christos elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) 10521 1.1 christos { 10522 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); 10523 1.1 christos } 10524 1.1 christos 10525 1.1 christos static bool 10526 1.12 christos elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) 10527 1.1 christos { 10528 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); 10529 1.1 christos } 10530 1.1 christos 10531 1.1 christos static bool 10532 1.12 christos elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) 10533 1.1 christos { 10534 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); 10535 1.1 christos } 10536 1.1 christos 10537 1.1 christos static bool 10538 1.12 christos elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) 10539 1.1 christos { 10540 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); 10541 1.1 christos } 10542 1.1 christos 10543 1.1 christos static bool 10544 1.12 christos elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) 10545 1.1 christos { 10546 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); 10547 1.1 christos } 10548 1.1 christos 10549 1.1 christos static bool 10550 1.12 christos elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) 10551 1.1 christos { 10552 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); 10553 1.1 christos } 10554 1.1 christos 10555 1.1 christos static bool 10556 1.12 christos elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) 10557 1.1 christos { 10558 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); 10559 1.1 christos } 10560 1.1 christos 10561 1.1 christos static bool 10562 1.12 christos elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) 10563 1.3 christos { 10564 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); 10565 1.3 christos } 10566 1.3 christos 10567 1.3 christos static bool 10568 1.12 christos elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) 10569 1.3 christos { 10570 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); 10571 1.3 christos } 10572 1.3 christos 10573 1.3 christos static bool 10574 1.12 christos elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) 10575 1.3 christos { 10576 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); 10577 1.3 christos } 10578 1.3 christos 10579 1.3 christos static bool 10580 1.12 christos elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) 10581 1.9 christos { 10582 1.9 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); 10583 1.9 christos } 10584 1.9 christos 10585 1.9 christos static bool 10586 1.12 christos elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note) 10587 1.9 christos { 10588 1.9 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note); 10589 1.9 christos } 10590 1.9 christos 10591 1.9 christos static bool 10592 1.12 christos elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note) 10593 1.1 christos { 10594 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note); 10595 1.1 christos } 10596 1.1 christos 10597 1.1 christos static bool 10598 1.12 christos elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) 10599 1.3 christos { 10600 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); 10601 1.3 christos } 10602 1.3 christos 10603 1.3 christos static bool 10604 1.12 christos elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) 10605 1.3 christos { 10606 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); 10607 1.3 christos } 10608 1.3 christos 10609 1.3 christos static bool 10610 1.12 christos elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) 10611 1.3 christos { 10612 1.3 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); 10613 1.3 christos } 10614 1.3 christos 10615 1.3 christos static bool 10616 1.12 christos elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) 10617 1.10 christos { 10618 1.10 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); 10619 1.10 christos } 10620 1.10 christos 10621 1.10 christos static bool 10622 1.12 christos elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note) 10623 1.10 christos { 10624 1.10 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note); 10625 1.10 christos } 10626 1.10 christos 10627 1.10 christos static bool 10628 1.12 christos elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note) 10629 1.12 christos { 10630 1.12 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note); 10631 1.12 christos } 10632 1.12 christos 10633 1.12 christos static bool 10634 1.12 christos elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note) 10635 1.12 christos { 10636 1.13 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte", 10637 1.13 christos note); 10638 1.13 christos } 10639 1.13 christos 10640 1.13 christos static bool 10641 1.13 christos elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note) 10642 1.13 christos { 10643 1.13 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-ssve", note); 10644 1.13 christos } 10645 1.13 christos 10646 1.13 christos static bool 10647 1.13 christos elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note) 10648 1.13 christos { 10649 1.13 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-za", note); 10650 1.13 christos } 10651 1.13 christos 10652 1.13 christos /* Convert NOTE into a bfd_section called ".reg-aarch-zt". Return TRUE if 10653 1.13 christos successful, otherwise return FALSE. */ 10654 1.13 christos 10655 1.13 christos static bool 10656 1.14 christos elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note) 10657 1.14 christos { 10658 1.14 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-zt", note); 10659 1.14 christos } 10660 1.14 christos 10661 1.14 christos /* Convert NOTE into a bfd_section called ".reg-aarch-gcs". Return TRUE if 10662 1.14 christos successful, otherwise return FALSE. */ 10663 1.14 christos 10664 1.14 christos static bool 10665 1.13 christos elfcore_grok_aarch_gcs (bfd *abfd, Elf_Internal_Note *note) 10666 1.12 christos { 10667 1.12 christos return elfcore_make_note_pseudosection (abfd, ".reg-aarch-gcs", note); 10668 1.12 christos } 10669 1.12 christos 10670 1.12 christos static bool 10671 1.12 christos elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note) 10672 1.12 christos { 10673 1.12 christos return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note); 10674 1.12 christos } 10675 1.12 christos 10676 1.12 christos /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if 10677 1.12 christos successful otherwise, return FALSE. */ 10678 1.12 christos 10679 1.12 christos static bool 10680 1.12 christos elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note) 10681 1.12 christos { 10682 1.12 christos return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note); 10683 1.12 christos } 10684 1.12 christos 10685 1.12 christos /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if 10686 1.12 christos successful otherwise, return FALSE. */ 10687 1.12 christos 10688 1.12 christos static bool 10689 1.12 christos elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note) 10690 1.12 christos { 10691 1.12 christos return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note); 10692 1.12 christos } 10693 1.12 christos 10694 1.12 christos static bool 10695 1.12 christos elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note) 10696 1.12 christos { 10697 1.12 christos return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note); 10698 1.12 christos } 10699 1.12 christos 10700 1.12 christos static bool 10701 1.12 christos elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note) 10702 1.12 christos { 10703 1.12 christos return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note); 10704 1.12 christos } 10705 1.12 christos 10706 1.12 christos static bool 10707 1.12 christos elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note) 10708 1.12 christos { 10709 1.12 christos return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note); 10710 1.12 christos } 10711 1.12 christos 10712 1.12 christos static bool 10713 1.1 christos elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note) 10714 1.1 christos { 10715 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note); 10716 1.1 christos } 10717 1.1 christos 10718 1.1 christos #if defined (HAVE_PRPSINFO_T) 10719 1.1 christos typedef prpsinfo_t elfcore_psinfo_t; 10720 1.1 christos #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ 10721 1.1 christos typedef prpsinfo32_t elfcore_psinfo32_t; 10722 1.1 christos #endif 10723 1.1 christos #endif 10724 1.1 christos 10725 1.1 christos #if defined (HAVE_PSINFO_T) 10726 1.1 christos typedef psinfo_t elfcore_psinfo_t; 10727 1.1 christos #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ 10728 1.1 christos typedef psinfo32_t elfcore_psinfo32_t; 10729 1.1 christos #endif 10730 1.1 christos #endif 10731 1.1 christos 10732 1.1 christos /* return a malloc'ed copy of a string at START which is at 10733 1.1 christos most MAX bytes long, possibly without a terminating '\0'. 10734 1.1 christos the copy will always have a terminating '\0'. */ 10735 1.1 christos 10736 1.1 christos char * 10737 1.1 christos _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) 10738 1.1 christos { 10739 1.1 christos char *dups; 10740 1.1 christos char *end = (char *) memchr (start, '\0', max); 10741 1.1 christos size_t len; 10742 1.1 christos 10743 1.1 christos if (end == NULL) 10744 1.1 christos len = max; 10745 1.1 christos else 10746 1.1 christos len = end - start; 10747 1.1 christos 10748 1.1 christos dups = (char *) bfd_alloc (abfd, len + 1); 10749 1.1 christos if (dups == NULL) 10750 1.1 christos return NULL; 10751 1.1 christos 10752 1.1 christos memcpy (dups, start, len); 10753 1.1 christos dups[len] = '\0'; 10754 1.12 christos 10755 1.1 christos return dups; 10756 1.1 christos } 10757 1.1 christos 10758 1.1 christos #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 10759 1.1 christos static bool 10760 1.1 christos elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) 10761 1.1 christos { 10762 1.1 christos if (note->descsz == sizeof (elfcore_psinfo_t)) 10763 1.1 christos { 10764 1.3 christos elfcore_psinfo_t psinfo; 10765 1.1 christos 10766 1.3 christos memcpy (&psinfo, note->descdata, sizeof (psinfo)); 10767 1.1 christos 10768 1.1 christos #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) 10769 1.1 christos elf_tdata (abfd)->core->pid = psinfo.pr_pid; 10770 1.3 christos #endif 10771 1.1 christos elf_tdata (abfd)->core->program 10772 1.1 christos = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, 10773 1.1 christos sizeof (psinfo.pr_fname)); 10774 1.1 christos 10775 1.1 christos elf_tdata (abfd)->core->command 10776 1.1 christos = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, 10777 1.1 christos sizeof (psinfo.pr_psargs)); 10778 1.1 christos } 10779 1.1 christos #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) 10780 1.1 christos else if (note->descsz == sizeof (elfcore_psinfo32_t)) 10781 1.1 christos { 10782 1.1 christos /* 64-bit host, 32-bit corefile */ 10783 1.3 christos elfcore_psinfo32_t psinfo; 10784 1.1 christos 10785 1.3 christos memcpy (&psinfo, note->descdata, sizeof (psinfo)); 10786 1.1 christos 10787 1.1 christos #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) 10788 1.1 christos elf_tdata (abfd)->core->pid = psinfo.pr_pid; 10789 1.3 christos #endif 10790 1.1 christos elf_tdata (abfd)->core->program 10791 1.1 christos = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, 10792 1.1 christos sizeof (psinfo.pr_fname)); 10793 1.1 christos 10794 1.1 christos elf_tdata (abfd)->core->command 10795 1.1 christos = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, 10796 1.1 christos sizeof (psinfo.pr_psargs)); 10797 1.1 christos } 10798 1.1 christos #endif 10799 1.12 christos 10800 1.1 christos else 10801 1.1 christos { 10802 1.1 christos /* Fail - we don't know how to handle any other 10803 1.1 christos note size (ie. data object type). */ 10804 1.1 christos return true; 10805 1.1 christos } 10806 1.1 christos 10807 1.3 christos /* Note that for some reason, a spurious space is tacked 10808 1.1 christos onto the end of the args in some (at least one anyway) 10809 1.1 christos implementations, so strip it off if it exists. */ 10810 1.1 christos 10811 1.1 christos { 10812 1.1 christos char *command = elf_tdata (abfd)->core->command; 10813 1.1 christos int n = strlen (command); 10814 1.12 christos 10815 1.1 christos if (0 < n && command[n - 1] == ' ') 10816 1.1 christos command[n - 1] = '\0'; 10817 1.1 christos } 10818 1.1 christos 10819 1.12 christos return true; 10820 1.1 christos } 10821 1.1 christos #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ 10822 1.1 christos 10823 1.1 christos #if defined (HAVE_PSTATUS_T) 10824 1.1 christos static bool 10825 1.1 christos elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) 10826 1.1 christos { 10827 1.1 christos if (note->descsz == sizeof (pstatus_t) 10828 1.1 christos #if defined (HAVE_PXSTATUS_T) 10829 1.1 christos || note->descsz == sizeof (pxstatus_t) 10830 1.1 christos #endif 10831 1.1 christos ) 10832 1.3 christos { 10833 1.1 christos pstatus_t pstat; 10834 1.1 christos 10835 1.1 christos memcpy (&pstat, note->descdata, sizeof (pstat)); 10836 1.1 christos 10837 1.1 christos elf_tdata (abfd)->core->pid = pstat.pr_pid; 10838 1.1 christos } 10839 1.1 christos #if defined (HAVE_PSTATUS32_T) 10840 1.1 christos else if (note->descsz == sizeof (pstatus32_t)) 10841 1.1 christos { 10842 1.3 christos /* 64-bit host, 32-bit corefile */ 10843 1.1 christos pstatus32_t pstat; 10844 1.1 christos 10845 1.1 christos memcpy (&pstat, note->descdata, sizeof (pstat)); 10846 1.1 christos 10847 1.1 christos elf_tdata (abfd)->core->pid = pstat.pr_pid; 10848 1.1 christos } 10849 1.12 christos #endif 10850 1.1 christos /* Could grab some more details from the "representative" 10851 1.1 christos lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an 10852 1.1 christos NT_LWPSTATUS note, presumably. */ 10853 1.1 christos 10854 1.12 christos return true; 10855 1.1 christos } 10856 1.1 christos #endif /* defined (HAVE_PSTATUS_T) */ 10857 1.1 christos 10858 1.1 christos #if defined (HAVE_LWPSTATUS_T) 10859 1.1 christos static bool 10860 1.1 christos elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) 10861 1.1 christos { 10862 1.1 christos lwpstatus_t lwpstat; 10863 1.1 christos char buf[100]; 10864 1.1 christos char *name; 10865 1.1 christos size_t len; 10866 1.1 christos asection *sect; 10867 1.1 christos 10868 1.12 christos if (note->descsz != sizeof (lwpstat) 10869 1.1 christos #if defined (HAVE_LWPXSTATUS_T) 10870 1.1 christos && note->descsz != sizeof (lwpxstatus_t) 10871 1.1 christos #endif 10872 1.3 christos ) 10873 1.1 christos return true; 10874 1.1 christos 10875 1.3 christos memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); 10876 1.3 christos 10877 1.1 christos elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; 10878 1.1 christos /* Do not overwrite the core signal if it has already been set by 10879 1.1 christos another thread. */ 10880 1.1 christos if (elf_tdata (abfd)->core->signal == 0) 10881 1.1 christos elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; 10882 1.1 christos 10883 1.1 christos /* Make a ".reg/999" section. */ 10884 1.12 christos 10885 1.1 christos sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); 10886 1.1 christos len = strlen (buf) + 1; 10887 1.1 christos name = bfd_alloc (abfd, len); 10888 1.1 christos if (name == NULL) 10889 1.12 christos return false; 10890 1.1 christos memcpy (name, buf, len); 10891 1.1 christos 10892 1.1 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 10893 1.1 christos if (sect == NULL) 10894 1.1 christos return false; 10895 1.1 christos 10896 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 10897 1.1 christos sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); 10898 1.1 christos sect->filepos = note->descpos 10899 1.1 christos + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); 10900 1.1 christos #endif 10901 1.1 christos 10902 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_REG) 10903 1.1 christos sect->size = sizeof (lwpstat.pr_reg); 10904 1.1 christos sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); 10905 1.12 christos #endif 10906 1.1 christos 10907 1.1 christos sect->alignment_power = 2; 10908 1.1 christos 10909 1.1 christos if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) 10910 1.1 christos return false; 10911 1.1 christos 10912 1.1 christos /* Make a ".reg2/999" section */ 10913 1.12 christos 10914 1.1 christos sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); 10915 1.1 christos len = strlen (buf) + 1; 10916 1.1 christos name = bfd_alloc (abfd, len); 10917 1.1 christos if (name == NULL) 10918 1.12 christos return false; 10919 1.1 christos memcpy (name, buf, len); 10920 1.1 christos 10921 1.1 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 10922 1.1 christos if (sect == NULL) 10923 1.1 christos return false; 10924 1.1 christos 10925 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 10926 1.1 christos sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); 10927 1.1 christos sect->filepos = note->descpos 10928 1.1 christos + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); 10929 1.1 christos #endif 10930 1.1 christos 10931 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_FPREG) 10932 1.1 christos sect->size = sizeof (lwpstat.pr_fpreg); 10933 1.1 christos sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); 10934 1.1 christos #endif 10935 1.1 christos 10936 1.1 christos sect->alignment_power = 2; 10937 1.12 christos 10938 1.12 christos return elfcore_maybe_make_sect (abfd, ".reg2", sect); 10939 1.12 christos } 10940 1.12 christos #endif /* defined (HAVE_LWPSTATUS_T) */ 10941 1.12 christos 10942 1.12 christos /* These constants, and the structure offsets used below, are defined by 10943 1.12 christos Cygwin's core_dump.h */ 10944 1.12 christos #define NOTE_INFO_PROCESS 1 10945 1.1 christos #define NOTE_INFO_THREAD 2 10946 1.1 christos #define NOTE_INFO_MODULE 3 10947 1.1 christos #define NOTE_INFO_MODULE64 4 10948 1.1 christos 10949 1.1 christos static bool 10950 1.12 christos elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) 10951 1.1 christos { 10952 1.12 christos char buf[30]; 10953 1.1 christos char *name; 10954 1.1 christos size_t len; 10955 1.1 christos unsigned int name_size; 10956 1.12 christos asection *sect; 10957 1.12 christos unsigned int type; 10958 1.1 christos int is_active_thread; 10959 1.12 christos bfd_vma base_addr; 10960 1.12 christos 10961 1.1 christos if (note->descsz < 4) 10962 1.1 christos return true; 10963 1.1 christos 10964 1.12 christos if (! startswith (note->namedata, "win32")) 10965 1.12 christos return true; 10966 1.12 christos 10967 1.12 christos type = bfd_get_32 (abfd, note->descdata); 10968 1.12 christos 10969 1.12 christos struct 10970 1.12 christos { 10971 1.12 christos const char *type_name; 10972 1.12 christos unsigned long min_size; 10973 1.12 christos } size_check[] = 10974 1.12 christos { 10975 1.12 christos { "NOTE_INFO_PROCESS", 12 }, 10976 1.12 christos { "NOTE_INFO_THREAD", 12 }, 10977 1.12 christos { "NOTE_INFO_MODULE", 12 }, 10978 1.12 christos { "NOTE_INFO_MODULE64", 16 }, 10979 1.12 christos }; 10980 1.12 christos 10981 1.13 christos if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0]))) 10982 1.13 christos return true; 10983 1.13 christos 10984 1.12 christos if (note->descsz < size_check[type - 1].min_size) 10985 1.12 christos { 10986 1.12 christos _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes" 10987 1.1 christos " is too small"), 10988 1.1 christos abfd, size_check[type - 1].type_name, note->descsz); 10989 1.12 christos return true; 10990 1.3 christos } 10991 1.12 christos 10992 1.12 christos switch (type) 10993 1.1 christos { 10994 1.1 christos case NOTE_INFO_PROCESS: 10995 1.12 christos /* FIXME: need to add ->core->command. */ 10996 1.12 christos elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4); 10997 1.13 christos elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8); 10998 1.1 christos break; 10999 1.12 christos 11000 1.1 christos case NOTE_INFO_THREAD: 11001 1.1 christos /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT 11002 1.1 christos structure. */ 11003 1.1 christos /* thread_info.tid */ 11004 1.12 christos sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4)); 11005 1.1 christos 11006 1.1 christos len = strlen (buf) + 1; 11007 1.1 christos name = (char *) bfd_alloc (abfd, len); 11008 1.1 christos if (name == NULL) 11009 1.1 christos return false; 11010 1.12 christos 11011 1.1 christos memcpy (name, buf, len); 11012 1.1 christos 11013 1.12 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 11014 1.1 christos if (sect == NULL) 11015 1.1 christos return false; 11016 1.1 christos 11017 1.1 christos /* sizeof (thread_info.thread_context) */ 11018 1.1 christos sect->size = note->descsz - 12; 11019 1.1 christos /* offsetof (thread_info.thread_context) */ 11020 1.1 christos sect->filepos = note->descpos + 12; 11021 1.1 christos sect->alignment_power = 2; 11022 1.1 christos 11023 1.12 christos /* thread_info.is_active_thread */ 11024 1.1 christos is_active_thread = bfd_get_32 (abfd, note->descdata + 8); 11025 1.1 christos 11026 1.12 christos if (is_active_thread) 11027 1.12 christos if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) 11028 1.1 christos return false; 11029 1.12 christos break; 11030 1.13 christos 11031 1.13 christos case NOTE_INFO_MODULE: 11032 1.13 christos case NOTE_INFO_MODULE64: 11033 1.13 christos /* Make a ".module/xxxxxxxx" section. */ 11034 1.13 christos if (type == NOTE_INFO_MODULE) 11035 1.13 christos { 11036 1.13 christos /* module_info.base_address */ 11037 1.12 christos base_addr = bfd_get_32 (abfd, note->descdata + 4); 11038 1.13 christos sprintf (buf, ".module/%08lx", (unsigned long) base_addr); 11039 1.13 christos /* module_info.module_name_size */ 11040 1.13 christos name_size = bfd_get_32 (abfd, note->descdata + 8); 11041 1.13 christos } 11042 1.13 christos else /* NOTE_INFO_MODULE64 */ 11043 1.13 christos { 11044 1.13 christos /* module_info.base_address */ 11045 1.1 christos base_addr = bfd_get_64 (abfd, note->descdata + 4); 11046 1.1 christos sprintf (buf, ".module/%016lx", (unsigned long) base_addr); 11047 1.1 christos /* module_info.module_name_size */ 11048 1.1 christos name_size = bfd_get_32 (abfd, note->descdata + 12); 11049 1.12 christos } 11050 1.1 christos 11051 1.1 christos len = strlen (buf) + 1; 11052 1.1 christos name = (char *) bfd_alloc (abfd, len); 11053 1.1 christos if (name == NULL) 11054 1.1 christos return false; 11055 1.1 christos 11056 1.12 christos memcpy (name, buf, len); 11057 1.12 christos 11058 1.12 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 11059 1.13 christos 11060 1.13 christos if (sect == NULL) 11061 1.13 christos return false; 11062 1.13 christos 11063 1.13 christos if (note->descsz < 12 + name_size) 11064 1.13 christos { 11065 1.1 christos _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu" 11066 1.1 christos " is too small to contain a name of size %u"), 11067 1.1 christos abfd, note->descsz, name_size); 11068 1.1 christos return true; 11069 1.1 christos } 11070 1.1 christos 11071 1.1 christos sect->size = note->descsz; 11072 1.12 christos sect->filepos = note->descpos; 11073 1.1 christos sect->alignment_power = 2; 11074 1.1 christos break; 11075 1.12 christos 11076 1.1 christos default: 11077 1.1 christos return true; 11078 1.12 christos } 11079 1.1 christos 11080 1.1 christos return true; 11081 1.1 christos } 11082 1.1 christos 11083 1.1 christos static bool 11084 1.1 christos elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) 11085 1.1 christos { 11086 1.12 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11087 1.1 christos 11088 1.1 christos switch (note->type) 11089 1.1 christos { 11090 1.1 christos default: 11091 1.12 christos return true; 11092 1.1 christos 11093 1.1 christos case NT_PRSTATUS: 11094 1.1 christos if (bed->elf_backend_grok_prstatus) 11095 1.12 christos if ((*bed->elf_backend_grok_prstatus) (abfd, note)) 11096 1.1 christos return true; 11097 1.1 christos #if defined (HAVE_PRSTATUS_T) 11098 1.1 christos return elfcore_grok_prstatus (abfd, note); 11099 1.1 christos #else 11100 1.1 christos return true; 11101 1.1 christos #endif 11102 1.1 christos 11103 1.1 christos #if defined (HAVE_PSTATUS_T) 11104 1.1 christos case NT_PSTATUS: 11105 1.1 christos return elfcore_grok_pstatus (abfd, note); 11106 1.1 christos #endif 11107 1.1 christos 11108 1.1 christos #if defined (HAVE_LWPSTATUS_T) 11109 1.1 christos case NT_LWPSTATUS: 11110 1.1 christos return elfcore_grok_lwpstatus (abfd, note); 11111 1.1 christos #endif 11112 1.1 christos 11113 1.1 christos case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ 11114 1.1 christos return elfcore_grok_prfpreg (abfd, note); 11115 1.1 christos 11116 1.1 christos case NT_WIN32PSTATUS: 11117 1.1 christos return elfcore_grok_win32pstatus (abfd, note); 11118 1.1 christos 11119 1.12 christos case NT_PRXFPREG: /* Linux SSE extension */ 11120 1.1 christos if (note->namesz == 6 11121 1.1 christos && strcmp (note->namedata, "LINUX") == 0) 11122 1.1 christos return elfcore_grok_prxfpreg (abfd, note); 11123 1.1 christos else 11124 1.1 christos return true; 11125 1.1 christos 11126 1.12 christos case NT_X86_XSTATE: /* Linux XSAVE extension */ 11127 1.1 christos if (note->namesz == 6 11128 1.14 christos && strcmp (note->namedata, "LINUX") == 0) 11129 1.14 christos return elfcore_grok_xstatereg (abfd, note); 11130 1.14 christos else 11131 1.14 christos return true; 11132 1.14 christos 11133 1.14 christos case NT_X86_SHSTK: /* Linux CET extension. */ 11134 1.14 christos if (note->namesz == 6 11135 1.1 christos && strcmp (note->namedata, "LINUX") == 0) 11136 1.1 christos return elfcore_grok_sspreg (abfd, note); 11137 1.1 christos else 11138 1.1 christos return true; 11139 1.1 christos 11140 1.12 christos case NT_PPC_VMX: 11141 1.1 christos if (note->namesz == 6 11142 1.1 christos && strcmp (note->namedata, "LINUX") == 0) 11143 1.1 christos return elfcore_grok_ppc_vmx (abfd, note); 11144 1.9 christos else 11145 1.9 christos return true; 11146 1.1 christos 11147 1.12 christos case NT_PPC_VSX: 11148 1.1 christos if (note->namesz == 6 11149 1.10 christos && strcmp (note->namedata, "LINUX") == 0) 11150 1.10 christos return elfcore_grok_ppc_vsx (abfd, note); 11151 1.10 christos else 11152 1.10 christos return true; 11153 1.10 christos 11154 1.12 christos case NT_PPC_TAR: 11155 1.10 christos if (note->namesz == 6 11156 1.10 christos && strcmp (note->namedata, "LINUX") == 0) 11157 1.10 christos return elfcore_grok_ppc_tar (abfd, note); 11158 1.10 christos else 11159 1.10 christos return true; 11160 1.10 christos 11161 1.12 christos case NT_PPC_PPR: 11162 1.10 christos if (note->namesz == 6 11163 1.10 christos && strcmp (note->namedata, "LINUX") == 0) 11164 1.10 christos return elfcore_grok_ppc_ppr (abfd, note); 11165 1.10 christos else 11166 1.10 christos return true; 11167 1.10 christos 11168 1.12 christos case NT_PPC_DSCR: 11169 1.10 christos if (note->namesz == 6 11170 1.10 christos && strcmp (note->namedata, "LINUX") == 0) 11171 1.10 christos return elfcore_grok_ppc_dscr (abfd, note); 11172 1.10 christos else 11173 1.10 christos return true; 11174 1.10 christos 11175 1.12 christos case NT_PPC_EBB: 11176 1.10 christos if (note->namesz == 6 11177 1.10 christos && strcmp (note->namedata, "LINUX") == 0) 11178 1.10 christos return elfcore_grok_ppc_ebb (abfd, note); 11179 1.10 christos else 11180 1.10 christos return true; 11181 1.10 christos 11182 1.12 christos case NT_PPC_PMU: 11183 1.10 christos if (note->namesz == 6 11184 1.10 christos && strcmp (note->namedata, "LINUX") == 0) 11185 1.10 christos return elfcore_grok_ppc_pmu (abfd, note); 11186 1.10 christos else 11187 1.10 christos return true; 11188 1.10 christos 11189 1.12 christos case NT_PPC_TM_CGPR: 11190 1.10 christos if (note->namesz == 6 11191 1.10 christos && strcmp (note->namedata, "LINUX") == 0) 11192 1.10 christos return elfcore_grok_ppc_tm_cgpr (abfd, note); 11193 1.10 christos else 11194 1.10 christos return true; 11195 1.10 christos 11196 1.12 christos case NT_PPC_TM_CFPR: 11197 1.10 christos if (note->namesz == 6 11198 1.10 christos && strcmp (note->namedata, "LINUX") == 0) 11199 1.10 christos return elfcore_grok_ppc_tm_cfpr (abfd, note); 11200 1.10 christos else 11201 1.10 christos return true; 11202 1.10 christos 11203 1.12 christos case NT_PPC_TM_CVMX: 11204 1.10 christos if (note->namesz == 6 11205 1.10 christos && strcmp (note->namedata, "LINUX") == 0) 11206 1.10 christos return elfcore_grok_ppc_tm_cvmx (abfd, note); 11207 1.10 christos else 11208 1.10 christos return true; 11209 1.10 christos 11210 1.12 christos case NT_PPC_TM_CVSX: 11211 1.10 christos if (note->namesz == 6 11212 1.10 christos && strcmp (note->namedata, "LINUX") == 0) 11213 1.10 christos return elfcore_grok_ppc_tm_cvsx (abfd, note); 11214 1.10 christos else 11215 1.10 christos return true; 11216 1.10 christos 11217 1.12 christos case NT_PPC_TM_SPR: 11218 1.10 christos if (note->namesz == 6 11219 1.10 christos && strcmp (note->namedata, "LINUX") == 0) 11220 1.10 christos return elfcore_grok_ppc_tm_spr (abfd, note); 11221 1.10 christos else 11222 1.10 christos return true; 11223 1.10 christos 11224 1.12 christos case NT_PPC_TM_CTAR: 11225 1.10 christos if (note->namesz == 6 11226 1.10 christos && strcmp (note->namedata, "LINUX") == 0) 11227 1.10 christos return elfcore_grok_ppc_tm_ctar (abfd, note); 11228 1.10 christos else 11229 1.10 christos return true; 11230 1.10 christos 11231 1.12 christos case NT_PPC_TM_CPPR: 11232 1.10 christos if (note->namesz == 6 11233 1.10 christos && strcmp (note->namedata, "LINUX") == 0) 11234 1.10 christos return elfcore_grok_ppc_tm_cppr (abfd, note); 11235 1.10 christos else 11236 1.10 christos return true; 11237 1.10 christos 11238 1.12 christos case NT_PPC_TM_CDSCR: 11239 1.10 christos if (note->namesz == 6 11240 1.1 christos && strcmp (note->namedata, "LINUX") == 0) 11241 1.1 christos return elfcore_grok_ppc_tm_cdscr (abfd, note); 11242 1.9 christos else 11243 1.9 christos return true; 11244 1.1 christos 11245 1.12 christos case NT_S390_HIGH_GPRS: 11246 1.1 christos if (note->namesz == 6 11247 1.1 christos && strcmp (note->namedata, "LINUX") == 0) 11248 1.1 christos return elfcore_grok_s390_high_gprs (abfd, note); 11249 1.9 christos else 11250 1.9 christos return true; 11251 1.1 christos 11252 1.12 christos case NT_S390_TIMER: 11253 1.1 christos if (note->namesz == 6 11254 1.1 christos && strcmp (note->namedata, "LINUX") == 0) 11255 1.1 christos return elfcore_grok_s390_timer (abfd, note); 11256 1.9 christos else 11257 1.9 christos return true; 11258 1.1 christos 11259 1.12 christos case NT_S390_TODCMP: 11260 1.1 christos if (note->namesz == 6 11261 1.1 christos && strcmp (note->namedata, "LINUX") == 0) 11262 1.1 christos return elfcore_grok_s390_todcmp (abfd, note); 11263 1.9 christos else 11264 1.9 christos return true; 11265 1.1 christos 11266 1.12 christos case NT_S390_TODPREG: 11267 1.1 christos if (note->namesz == 6 11268 1.1 christos && strcmp (note->namedata, "LINUX") == 0) 11269 1.1 christos return elfcore_grok_s390_todpreg (abfd, note); 11270 1.9 christos else 11271 1.9 christos return true; 11272 1.1 christos 11273 1.12 christos case NT_S390_CTRS: 11274 1.1 christos if (note->namesz == 6 11275 1.1 christos && strcmp (note->namedata, "LINUX") == 0) 11276 1.1 christos return elfcore_grok_s390_ctrs (abfd, note); 11277 1.9 christos else 11278 1.9 christos return true; 11279 1.1 christos 11280 1.12 christos case NT_S390_PREFIX: 11281 1.1 christos if (note->namesz == 6 11282 1.1 christos && strcmp (note->namedata, "LINUX") == 0) 11283 1.1 christos return elfcore_grok_s390_prefix (abfd, note); 11284 1.9 christos else 11285 1.9 christos return true; 11286 1.1 christos 11287 1.12 christos case NT_S390_LAST_BREAK: 11288 1.1 christos if (note->namesz == 6 11289 1.1 christos && strcmp (note->namedata, "LINUX") == 0) 11290 1.1 christos return elfcore_grok_s390_last_break (abfd, note); 11291 1.9 christos else 11292 1.9 christos return true; 11293 1.1 christos 11294 1.12 christos case NT_S390_SYSTEM_CALL: 11295 1.1 christos if (note->namesz == 6 11296 1.3 christos && strcmp (note->namedata, "LINUX") == 0) 11297 1.3 christos return elfcore_grok_s390_system_call (abfd, note); 11298 1.9 christos else 11299 1.9 christos return true; 11300 1.3 christos 11301 1.12 christos case NT_S390_TDB: 11302 1.3 christos if (note->namesz == 6 11303 1.3 christos && strcmp (note->namedata, "LINUX") == 0) 11304 1.3 christos return elfcore_grok_s390_tdb (abfd, note); 11305 1.3 christos else 11306 1.3 christos return true; 11307 1.3 christos 11308 1.12 christos case NT_S390_VXRS_LOW: 11309 1.3 christos if (note->namesz == 6 11310 1.3 christos && strcmp (note->namedata, "LINUX") == 0) 11311 1.3 christos return elfcore_grok_s390_vxrs_low (abfd, note); 11312 1.3 christos else 11313 1.3 christos return true; 11314 1.3 christos 11315 1.12 christos case NT_S390_VXRS_HIGH: 11316 1.3 christos if (note->namesz == 6 11317 1.9 christos && strcmp (note->namedata, "LINUX") == 0) 11318 1.9 christos return elfcore_grok_s390_vxrs_high (abfd, note); 11319 1.9 christos else 11320 1.9 christos return true; 11321 1.9 christos 11322 1.12 christos case NT_S390_GS_CB: 11323 1.9 christos if (note->namesz == 6 11324 1.9 christos && strcmp (note->namedata, "LINUX") == 0) 11325 1.9 christos return elfcore_grok_s390_gs_cb (abfd, note); 11326 1.9 christos else 11327 1.9 christos return true; 11328 1.9 christos 11329 1.12 christos case NT_S390_GS_BC: 11330 1.12 christos if (note->namesz == 6 11331 1.12 christos && strcmp (note->namedata, "LINUX") == 0) 11332 1.12 christos return elfcore_grok_s390_gs_bc (abfd, note); 11333 1.12 christos else 11334 1.12 christos return true; 11335 1.12 christos 11336 1.12 christos case NT_ARC_V2: 11337 1.9 christos if (note->namesz == 6 11338 1.1 christos && strcmp (note->namedata, "LINUX") == 0) 11339 1.1 christos return elfcore_grok_arc_v2 (abfd, note); 11340 1.1 christos else 11341 1.1 christos return true; 11342 1.1 christos 11343 1.12 christos case NT_ARM_VFP: 11344 1.1 christos if (note->namesz == 6 11345 1.3 christos && strcmp (note->namedata, "LINUX") == 0) 11346 1.3 christos return elfcore_grok_arm_vfp (abfd, note); 11347 1.3 christos else 11348 1.3 christos return true; 11349 1.3 christos 11350 1.12 christos case NT_ARM_TLS: 11351 1.3 christos if (note->namesz == 6 11352 1.3 christos && strcmp (note->namedata, "LINUX") == 0) 11353 1.3 christos return elfcore_grok_aarch_tls (abfd, note); 11354 1.3 christos else 11355 1.3 christos return true; 11356 1.3 christos 11357 1.12 christos case NT_ARM_HW_BREAK: 11358 1.3 christos if (note->namesz == 6 11359 1.3 christos && strcmp (note->namedata, "LINUX") == 0) 11360 1.3 christos return elfcore_grok_aarch_hw_break (abfd, note); 11361 1.3 christos else 11362 1.3 christos return true; 11363 1.3 christos 11364 1.12 christos case NT_ARM_HW_WATCH: 11365 1.3 christos if (note->namesz == 6 11366 1.10 christos && strcmp (note->namedata, "LINUX") == 0) 11367 1.10 christos return elfcore_grok_aarch_hw_watch (abfd, note); 11368 1.10 christos else 11369 1.10 christos return true; 11370 1.10 christos 11371 1.12 christos case NT_ARM_SVE: 11372 1.10 christos if (note->namesz == 6 11373 1.10 christos && strcmp (note->namedata, "LINUX") == 0) 11374 1.10 christos return elfcore_grok_aarch_sve (abfd, note); 11375 1.10 christos else 11376 1.10 christos return true; 11377 1.10 christos 11378 1.12 christos case NT_ARM_PAC_MASK: 11379 1.12 christos if (note->namesz == 6 11380 1.12 christos && strcmp (note->namedata, "LINUX") == 0) 11381 1.12 christos return elfcore_grok_aarch_pauth (abfd, note); 11382 1.12 christos else 11383 1.12 christos return true; 11384 1.12 christos 11385 1.12 christos case NT_ARM_TAGGED_ADDR_CTRL: 11386 1.12 christos if (note->namesz == 6 11387 1.13 christos && strcmp (note->namedata, "LINUX") == 0) 11388 1.13 christos return elfcore_grok_aarch_mte (abfd, note); 11389 1.13 christos else 11390 1.13 christos return true; 11391 1.13 christos 11392 1.13 christos case NT_ARM_SSVE: 11393 1.13 christos if (note->namesz == 6 11394 1.13 christos && strcmp (note->namedata, "LINUX") == 0) 11395 1.13 christos return elfcore_grok_aarch_ssve (abfd, note); 11396 1.13 christos else 11397 1.13 christos return true; 11398 1.13 christos 11399 1.13 christos case NT_ARM_ZA: 11400 1.13 christos if (note->namesz == 6 11401 1.13 christos && strcmp (note->namedata, "LINUX") == 0) 11402 1.13 christos return elfcore_grok_aarch_za (abfd, note); 11403 1.13 christos else 11404 1.13 christos return true; 11405 1.13 christos 11406 1.13 christos case NT_ARM_ZT: 11407 1.13 christos if (note->namesz == 6 11408 1.14 christos && strcmp (note->namedata, "LINUX") == 0) 11409 1.14 christos return elfcore_grok_aarch_zt (abfd, note); 11410 1.14 christos else 11411 1.14 christos return true; 11412 1.14 christos 11413 1.14 christos case NT_ARM_GCS: 11414 1.12 christos if (note->namesz == 6 && strcmp (note->namedata, "LINUX") == 0) 11415 1.12 christos return elfcore_grok_aarch_gcs (abfd, note); 11416 1.13 christos else 11417 1.13 christos return true; 11418 1.12 christos 11419 1.13 christos case NT_GDB_TDESC: 11420 1.12 christos if (note->namesz == 4 11421 1.12 christos && strcmp (note->namedata, "GDB") == 0) 11422 1.12 christos return elfcore_grok_gdb_tdesc (abfd, note); 11423 1.13 christos else 11424 1.13 christos return true; 11425 1.12 christos 11426 1.12 christos case NT_RISCV_CSR: 11427 1.12 christos if (note->namesz == 4 11428 1.12 christos && strcmp (note->namedata, "GDB") == 0) 11429 1.12 christos return elfcore_grok_riscv_csr (abfd, note); 11430 1.12 christos else 11431 1.12 christos return true; 11432 1.12 christos 11433 1.12 christos case NT_LARCH_CPUCFG: 11434 1.12 christos if (note->namesz == 6 11435 1.12 christos && strcmp (note->namedata, "LINUX") == 0) 11436 1.12 christos return elfcore_grok_loongarch_cpucfg (abfd, note); 11437 1.12 christos else 11438 1.12 christos return true; 11439 1.12 christos 11440 1.12 christos case NT_LARCH_LBT: 11441 1.12 christos if (note->namesz == 6 11442 1.12 christos && strcmp (note->namedata, "LINUX") == 0) 11443 1.12 christos return elfcore_grok_loongarch_lbt (abfd, note); 11444 1.12 christos else 11445 1.12 christos return true; 11446 1.12 christos 11447 1.12 christos case NT_LARCH_LSX: 11448 1.12 christos if (note->namesz == 6 11449 1.12 christos && strcmp (note->namedata, "LINUX") == 0) 11450 1.12 christos return elfcore_grok_loongarch_lsx (abfd, note); 11451 1.12 christos else 11452 1.12 christos return true; 11453 1.12 christos 11454 1.12 christos case NT_LARCH_LASX: 11455 1.10 christos if (note->namesz == 6 11456 1.1 christos && strcmp (note->namedata, "LINUX") == 0) 11457 1.1 christos return elfcore_grok_loongarch_lasx (abfd, note); 11458 1.1 christos else 11459 1.1 christos return true; 11460 1.12 christos 11461 1.1 christos case NT_PRPSINFO: 11462 1.1 christos case NT_PSINFO: 11463 1.1 christos if (bed->elf_backend_grok_psinfo) 11464 1.12 christos if ((*bed->elf_backend_grok_psinfo) (abfd, note)) 11465 1.1 christos return true; 11466 1.1 christos #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 11467 1.1 christos return elfcore_grok_psinfo (abfd, note); 11468 1.5 christos #else 11469 1.3 christos return true; 11470 1.3 christos #endif 11471 1.3 christos 11472 1.3 christos case NT_AUXV: 11473 1.3 christos return elfcore_make_auxv_note_section (abfd, note, 0); 11474 1.3 christos 11475 1.3 christos case NT_FILE: 11476 1.3 christos return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", 11477 1.5 christos note); 11478 1.1 christos 11479 1.1 christos case NT_SIGINFO: 11480 1.1 christos return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", 11481 1.12 christos note); 11482 1.1 christos 11483 1.1 christos } 11484 1.3 christos } 11485 1.3 christos 11486 1.3 christos static bool 11487 1.12 christos elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) 11488 1.3 christos { 11489 1.3 christos struct bfd_build_id* build_id; 11490 1.3 christos 11491 1.12 christos if (note->descsz == 0) 11492 1.1 christos return false; 11493 1.3 christos 11494 1.3 christos build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); 11495 1.3 christos if (build_id == NULL) 11496 1.1 christos return false; 11497 1.12 christos 11498 1.1 christos build_id->size = note->descsz; 11499 1.1 christos memcpy (build_id->data, note->descdata, note->descsz); 11500 1.12 christos abfd->build_id = build_id; 11501 1.1 christos 11502 1.1 christos return true; 11503 1.1 christos } 11504 1.1 christos 11505 1.1 christos static bool 11506 1.12 christos elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) 11507 1.1 christos { 11508 1.9 christos switch (note->type) 11509 1.9 christos { 11510 1.9 christos default: 11511 1.1 christos return true; 11512 1.1 christos 11513 1.1 christos case NT_GNU_PROPERTY_TYPE_0: 11514 1.1 christos return _bfd_elf_parse_gnu_properties (abfd, note); 11515 1.1 christos 11516 1.12 christos case NT_GNU_BUILD_ID: 11517 1.1 christos return elfobj_grok_gnu_build_id (abfd, note); 11518 1.1 christos } 11519 1.1 christos } 11520 1.10 christos 11521 1.10 christos static bool 11522 1.1 christos elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) 11523 1.1 christos { 11524 1.1 christos struct sdt_note *cur = 11525 1.1 christos (struct sdt_note *) bfd_alloc (abfd, 11526 1.1 christos sizeof (struct sdt_note) + note->descsz); 11527 1.1 christos 11528 1.1 christos cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; 11529 1.12 christos cur->size = (bfd_size_type) note->descsz; 11530 1.1 christos memcpy (cur->data, note->descdata, note->descsz); 11531 1.1 christos 11532 1.12 christos elf_tdata (abfd)->sdt_note_head = cur; 11533 1.1 christos 11534 1.1 christos return true; 11535 1.1 christos } 11536 1.1 christos 11537 1.1 christos static bool 11538 1.1 christos elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) 11539 1.1 christos { 11540 1.1 christos switch (note->type) 11541 1.12 christos { 11542 1.1 christos case NT_STAPSDT: 11543 1.1 christos return elfobj_grok_stapsdt_note_1 (abfd, note); 11544 1.1 christos 11545 1.12 christos default: 11546 1.5 christos return true; 11547 1.5 christos } 11548 1.5 christos } 11549 1.5 christos 11550 1.9 christos static bool 11551 1.9 christos elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) 11552 1.9 christos { 11553 1.9 christos size_t offset; 11554 1.12 christos 11555 1.9 christos switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) 11556 1.9 christos { 11557 1.9 christos case ELFCLASS32: 11558 1.9 christos if (note->descsz < 108) 11559 1.12 christos return false; 11560 1.9 christos break; 11561 1.9 christos 11562 1.9 christos case ELFCLASS64: 11563 1.12 christos if (note->descsz < 120) 11564 1.9 christos return false; 11565 1.9 christos break; 11566 1.9 christos 11567 1.5 christos default: 11568 1.12 christos return false; 11569 1.9 christos } 11570 1.5 christos 11571 1.5 christos /* Check for version 1 in pr_version. */ 11572 1.5 christos if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) 11573 1.9 christos return false; 11574 1.9 christos 11575 1.9 christos offset = 4; 11576 1.5 christos 11577 1.5 christos /* Skip over pr_psinfosz. */ 11578 1.5 christos if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) 11579 1.5 christos offset += 4; 11580 1.5 christos else 11581 1.5 christos { 11582 1.5 christos offset += 4; /* Padding before pr_psinfosz. */ 11583 1.5 christos offset += 8; 11584 1.5 christos } 11585 1.5 christos 11586 1.5 christos /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ 11587 1.5 christos elf_tdata (abfd)->core->program 11588 1.5 christos = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); 11589 1.9 christos offset += 17; 11590 1.9 christos 11591 1.9 christos /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ 11592 1.9 christos elf_tdata (abfd)->core->command 11593 1.9 christos = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); 11594 1.9 christos offset += 81; 11595 1.9 christos 11596 1.12 christos /* Padding before pr_pid. */ 11597 1.9 christos offset += 2; 11598 1.9 christos 11599 1.9 christos /* The pr_pid field was added in version "1a". */ 11600 1.5 christos if (note->descsz < offset + 4) 11601 1.12 christos return true; 11602 1.5 christos 11603 1.5 christos elf_tdata (abfd)->core->pid 11604 1.12 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 11605 1.5 christos 11606 1.5 christos return true; 11607 1.5 christos } 11608 1.5 christos 11609 1.9 christos static bool 11610 1.5 christos elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) 11611 1.9 christos { 11612 1.9 christos size_t offset; 11613 1.9 christos size_t size; 11614 1.9 christos size_t min_size; 11615 1.9 christos 11616 1.9 christos /* Compute offset of pr_getregsz, skipping over pr_statussz. 11617 1.9 christos Also compute minimum size of this note. */ 11618 1.5 christos switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) 11619 1.5 christos { 11620 1.9 christos case ELFCLASS32: 11621 1.9 christos offset = 4 + 4; 11622 1.9 christos min_size = offset + (4 * 2) + 4 + 4 + 4; 11623 1.5 christos break; 11624 1.5 christos 11625 1.5 christos case ELFCLASS64: 11626 1.12 christos offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */ 11627 1.5 christos min_size = offset + (8 * 2) + 4 + 4 + 4 + 4; 11628 1.5 christos break; 11629 1.9 christos 11630 1.12 christos default: 11631 1.9 christos return false; 11632 1.9 christos } 11633 1.9 christos 11634 1.12 christos if (note->descsz < min_size) 11635 1.9 christos return false; 11636 1.5 christos 11637 1.9 christos /* Check for version 1 in pr_version. */ 11638 1.9 christos if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) 11639 1.9 christos return false; 11640 1.9 christos 11641 1.9 christos /* Extract size of pr_reg from pr_gregsetsz. */ 11642 1.9 christos /* Skip over pr_gregsetsz and pr_fpregsetsz. */ 11643 1.5 christos if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) 11644 1.9 christos { 11645 1.9 christos size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 11646 1.9 christos offset += 4 * 2; 11647 1.9 christos } 11648 1.5 christos else 11649 1.9 christos { 11650 1.5 christos size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); 11651 1.5 christos offset += 8 * 2; 11652 1.9 christos } 11653 1.5 christos 11654 1.5 christos /* Skip over pr_osreldate. */ 11655 1.5 christos offset += 4; 11656 1.5 christos 11657 1.5 christos /* Read signal from pr_cursig. */ 11658 1.9 christos if (elf_tdata (abfd)->core->signal == 0) 11659 1.5 christos elf_tdata (abfd)->core->signal 11660 1.5 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 11661 1.5 christos offset += 4; 11662 1.5 christos 11663 1.9 christos /* Read TID from pr_pid. */ 11664 1.9 christos elf_tdata (abfd)->core->lwpid 11665 1.5 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 11666 1.5 christos offset += 4; 11667 1.9 christos 11668 1.9 christos /* Padding before pr_reg. */ 11669 1.12 christos if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) 11670 1.9 christos offset += 4; 11671 1.5 christos 11672 1.5 christos /* Make sure that there is enough data remaining in the note. */ 11673 1.5 christos if ((note->descsz - offset) < size) 11674 1.5 christos return false; 11675 1.5 christos 11676 1.12 christos /* Make a ".reg/999" section and a ".reg" section. */ 11677 1.5 christos return _bfd_elfcore_make_pseudosection (abfd, ".reg", 11678 1.5 christos size, note->descpos + offset); 11679 1.9 christos } 11680 1.9 christos 11681 1.5 christos static bool 11682 1.5 christos elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) 11683 1.5 christos { 11684 1.9 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11685 1.9 christos 11686 1.12 christos switch (note->type) 11687 1.5 christos { 11688 1.5 christos case NT_PRSTATUS: 11689 1.5 christos if (bed->elf_backend_grok_freebsd_prstatus) 11690 1.5 christos if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note)) 11691 1.5 christos return true; 11692 1.5 christos return elfcore_grok_freebsd_prstatus (abfd, note); 11693 1.5 christos 11694 1.5 christos case NT_FPREGSET: 11695 1.5 christos return elfcore_grok_prfpreg (abfd, note); 11696 1.12 christos 11697 1.5 christos case NT_PRPSINFO: 11698 1.9 christos return elfcore_grok_freebsd_psinfo (abfd, note); 11699 1.9 christos 11700 1.9 christos case NT_FREEBSD_THRMISC: 11701 1.9 christos return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); 11702 1.9 christos 11703 1.9 christos case NT_FREEBSD_PROCSTAT_PROC: 11704 1.9 christos return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc", 11705 1.9 christos note); 11706 1.9 christos 11707 1.9 christos case NT_FREEBSD_PROCSTAT_FILES: 11708 1.9 christos return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files", 11709 1.9 christos note); 11710 1.5 christos 11711 1.5 christos case NT_FREEBSD_PROCSTAT_VMMAP: 11712 1.5 christos return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap", 11713 1.12 christos note); 11714 1.12 christos 11715 1.12 christos case NT_FREEBSD_PROCSTAT_AUXV: 11716 1.5 christos return elfcore_make_auxv_note_section (abfd, note, 4); 11717 1.12 christos 11718 1.5 christos case NT_FREEBSD_X86_SEGBASES: 11719 1.9 christos return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note); 11720 1.9 christos 11721 1.9 christos case NT_X86_XSTATE: 11722 1.9 christos return elfcore_grok_xstatereg (abfd, note); 11723 1.12 christos 11724 1.12 christos case NT_FREEBSD_PTLWPINFO: 11725 1.12 christos return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo", 11726 1.9 christos note); 11727 1.9 christos 11728 1.9 christos case NT_ARM_TLS: 11729 1.5 christos return elfcore_grok_aarch_tls (abfd, note); 11730 1.12 christos 11731 1.5 christos case NT_ARM_VFP: 11732 1.5 christos return elfcore_grok_arm_vfp (abfd, note); 11733 1.5 christos 11734 1.12 christos default: 11735 1.1 christos return true; 11736 1.1 christos } 11737 1.1 christos } 11738 1.1 christos 11739 1.1 christos static bool 11740 1.1 christos elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) 11741 1.1 christos { 11742 1.1 christos char *cp; 11743 1.12 christos 11744 1.1 christos cp = strchr (note->namedata, '@'); 11745 1.12 christos if (cp != NULL) 11746 1.1 christos { 11747 1.1 christos *lwpidp = atoi(cp + 1); 11748 1.12 christos return true; 11749 1.1 christos } 11750 1.1 christos return false; 11751 1.9 christos } 11752 1.12 christos 11753 1.9 christos static bool 11754 1.1 christos elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) 11755 1.3 christos { 11756 1.1 christos if (note->descsz <= 0x7c + 31) 11757 1.1 christos return false; 11758 1.1 christos 11759 1.3 christos /* Signal number at offset 0x08. */ 11760 1.1 christos elf_tdata (abfd)->core->signal 11761 1.1 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); 11762 1.1 christos 11763 1.3 christos /* Process ID at offset 0x50. */ 11764 1.1 christos elf_tdata (abfd)->core->pid 11765 1.1 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); 11766 1.1 christos 11767 1.1 christos /* Command name at 0x7c (max 32 bytes, including nul). */ 11768 1.1 christos elf_tdata (abfd)->core->command 11769 1.1 christos = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); 11770 1.12 christos 11771 1.1 christos return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", 11772 1.1 christos note); 11773 1.1 christos } 11774 1.1 christos 11775 1.1 christos static bool 11776 1.3 christos elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) 11777 1.1 christos { 11778 1.5 christos int lwp; 11779 1.1 christos 11780 1.5 christos if (elfcore_netbsd_get_lwpid (note, &lwp)) 11781 1.1 christos elf_tdata (abfd)->core->lwpid = lwp; 11782 1.1 christos 11783 1.1 christos switch (note->type) 11784 1.1 christos { 11785 1.5 christos case NT_NETBSDCORE_PROCINFO: 11786 1.5 christos /* NetBSD-specific core "procinfo". Note that we expect to 11787 1.5 christos find this note before any of the others, which is fine, 11788 1.11 rin since the kernel writes this note out first when it 11789 1.12 christos creates a core file. */ 11790 1.12 christos return elfcore_grok_netbsd_procinfo (abfd, note); 11791 1.12 christos case NT_NETBSDCORE_AUXV: 11792 1.12 christos /* NetBSD-specific Elf Auxiliary Vector data. */ 11793 1.5 christos return elfcore_make_auxv_note_section (abfd, note, 0); 11794 1.5 christos case NT_NETBSDCORE_LWPSTATUS: 11795 1.1 christos return elfcore_make_note_pseudosection (abfd, 11796 1.1 christos ".note.netbsdcore.lwpstatus", 11797 1.12 christos note); 11798 1.1 christos default: 11799 1.1 christos break; 11800 1.1 christos } 11801 1.1 christos 11802 1.1 christos /* As of March 2020 there are no other machine-independent notes 11803 1.12 christos defined for NetBSD core files. If the note type is less 11804 1.1 christos than the start of the machine-dependent note types, we don't 11805 1.1 christos understand it. */ 11806 1.1 christos 11807 1.1 christos if (note->type < NT_NETBSDCORE_FIRSTMACH) 11808 1.1 christos return true; 11809 1.1 christos 11810 1.1 christos 11811 1.12 christos switch (bfd_get_arch (abfd)) 11812 1.1 christos { 11813 1.1 christos /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and 11814 1.1 christos PT_GETFPREGS == mach+2. */ 11815 1.1 christos 11816 1.1 christos case bfd_arch_aarch64: 11817 1.1 christos case bfd_arch_alpha: 11818 1.1 christos case bfd_arch_sparc: 11819 1.1 christos switch (note->type) 11820 1.1 christos { 11821 1.1 christos case NT_NETBSDCORE_FIRSTMACH+0: 11822 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg", note); 11823 1.12 christos 11824 1.1 christos case NT_NETBSDCORE_FIRSTMACH+2: 11825 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg2", note); 11826 1.1 christos 11827 1.1 christos default: 11828 1.1 christos return true; 11829 1.1 christos } 11830 1.1 christos 11831 1.1 christos /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5. 11832 1.1 christos There's also old PT___GETREGS40 == mach + 1 for old reg 11833 1.1 christos structure which lacks GBR. */ 11834 1.1 christos 11835 1.1 christos case bfd_arch_sh: 11836 1.1 christos switch (note->type) 11837 1.1 christos { 11838 1.1 christos case NT_NETBSDCORE_FIRSTMACH+3: 11839 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg", note); 11840 1.12 christos 11841 1.1 christos case NT_NETBSDCORE_FIRSTMACH+5: 11842 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg2", note); 11843 1.1 christos 11844 1.1 christos default: 11845 1.1 christos return true; 11846 1.1 christos } 11847 1.1 christos 11848 1.1 christos /* On all other arch's, PT_GETREGS == mach+1 and 11849 1.1 christos PT_GETFPREGS == mach+3. */ 11850 1.1 christos 11851 1.1 christos default: 11852 1.1 christos switch (note->type) 11853 1.1 christos { 11854 1.1 christos case NT_NETBSDCORE_FIRSTMACH+1: 11855 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg", note); 11856 1.12 christos 11857 1.1 christos case NT_NETBSDCORE_FIRSTMACH+3: 11858 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg2", note); 11859 1.1 christos 11860 1.1 christos default: 11861 1.1 christos return true; 11862 1.12 christos } 11863 1.1 christos } 11864 1.1 christos /* NOTREACHED */ 11865 1.9 christos } 11866 1.12 christos 11867 1.9 christos static bool 11868 1.1 christos elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) 11869 1.3 christos { 11870 1.1 christos if (note->descsz <= 0x48 + 31) 11871 1.1 christos return false; 11872 1.1 christos 11873 1.3 christos /* Signal number at offset 0x08. */ 11874 1.1 christos elf_tdata (abfd)->core->signal 11875 1.1 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); 11876 1.1 christos 11877 1.3 christos /* Process ID at offset 0x20. */ 11878 1.1 christos elf_tdata (abfd)->core->pid 11879 1.1 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); 11880 1.12 christos 11881 1.12 christos /* Command name at 0x48 (max 32 bytes, including nul). */ 11882 1.12 christos elf_tdata (abfd)->core->command 11883 1.12 christos = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); 11884 1.12 christos 11885 1.12 christos return true; 11886 1.12 christos } 11887 1.12 christos 11888 1.12 christos /* Processes Solaris's process status note. 11889 1.12 christos sig_off ~ offsetof(prstatus_t, pr_cursig) 11890 1.12 christos pid_off ~ offsetof(prstatus_t, pr_pid) 11891 1.12 christos lwpid_off ~ offsetof(prstatus_t, pr_who) 11892 1.12 christos gregset_size ~ sizeof(gregset_t) 11893 1.12 christos gregset_offset ~ offsetof(prstatus_t, pr_reg) */ 11894 1.12 christos 11895 1.12 christos static bool 11896 1.12 christos elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off, 11897 1.12 christos int pid_off, int lwpid_off, size_t gregset_size, 11898 1.12 christos size_t gregset_offset) 11899 1.12 christos { 11900 1.12 christos asection *sect = NULL; 11901 1.12 christos elf_tdata (abfd)->core->signal 11902 1.12 christos = bfd_get_16 (abfd, note->descdata + sig_off); 11903 1.12 christos elf_tdata (abfd)->core->pid 11904 1.12 christos = bfd_get_32 (abfd, note->descdata + pid_off); 11905 1.12 christos elf_tdata (abfd)->core->lwpid 11906 1.12 christos = bfd_get_32 (abfd, note->descdata + lwpid_off); 11907 1.12 christos 11908 1.12 christos sect = bfd_get_section_by_name (abfd, ".reg"); 11909 1.12 christos if (sect != NULL) 11910 1.12 christos sect->size = gregset_size; 11911 1.12 christos 11912 1.12 christos return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size, 11913 1.12 christos note->descpos + gregset_offset); 11914 1.12 christos } 11915 1.12 christos 11916 1.12 christos /* Gets program and arguments from a core. 11917 1.12 christos prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname) 11918 1.12 christos comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */ 11919 1.12 christos 11920 1.12 christos static bool 11921 1.12 christos elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note, 11922 1.12 christos int prog_off, int comm_off) 11923 1.12 christos { 11924 1.12 christos elf_tdata (abfd)->core->program 11925 1.12 christos = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16); 11926 1.12 christos elf_tdata (abfd)->core->command 11927 1.12 christos = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80); 11928 1.12 christos 11929 1.12 christos return true; 11930 1.12 christos } 11931 1.12 christos 11932 1.12 christos /* Processes Solaris's LWP status note. 11933 1.12 christos gregset_size ~ sizeof(gregset_t) 11934 1.12 christos gregset_off ~ offsetof(lwpstatus_t, pr_reg) 11935 1.12 christos fpregset_size ~ sizeof(fpregset_t) 11936 1.12 christos fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */ 11937 1.12 christos 11938 1.12 christos static bool 11939 1.12 christos elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note, 11940 1.12 christos size_t gregset_size, int gregset_off, 11941 1.12 christos size_t fpregset_size, int fpregset_off) 11942 1.12 christos { 11943 1.12 christos asection *sect = NULL; 11944 1.12 christos char reg2_section_name[16] = { 0 }; 11945 1.12 christos 11946 1.12 christos (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2", 11947 1.12 christos elf_tdata (abfd)->core->lwpid); 11948 1.12 christos 11949 1.12 christos /* offsetof(lwpstatus_t, pr_lwpid) */ 11950 1.12 christos elf_tdata (abfd)->core->lwpid 11951 1.12 christos = bfd_get_32 (abfd, note->descdata + 4); 11952 1.12 christos /* offsetof(lwpstatus_t, pr_cursig) */ 11953 1.12 christos elf_tdata (abfd)->core->signal 11954 1.12 christos = bfd_get_16 (abfd, note->descdata + 12); 11955 1.12 christos 11956 1.12 christos sect = bfd_get_section_by_name (abfd, ".reg"); 11957 1.12 christos if (sect != NULL) 11958 1.12 christos sect->size = gregset_size; 11959 1.12 christos else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size, 11960 1.12 christos note->descpos + gregset_off)) 11961 1.12 christos return false; 11962 1.12 christos 11963 1.12 christos sect = bfd_get_section_by_name (abfd, reg2_section_name); 11964 1.12 christos if (sect != NULL) 11965 1.12 christos { 11966 1.12 christos sect->size = fpregset_size; 11967 1.12 christos sect->filepos = note->descpos + fpregset_off; 11968 1.12 christos sect->alignment_power = 2; 11969 1.12 christos } 11970 1.12 christos else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size, 11971 1.12 christos note->descpos + fpregset_off)) 11972 1.12 christos return false; 11973 1.12 christos 11974 1.12 christos return true; 11975 1.12 christos } 11976 1.12 christos 11977 1.12 christos static bool 11978 1.12 christos elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note) 11979 1.12 christos { 11980 1.12 christos if (note == NULL) 11981 1.12 christos return false; 11982 1.12 christos 11983 1.12 christos /* core files are identified as 32- or 64-bit, SPARC or x86, 11984 1.12 christos by the size of the descsz which matches the sizeof() 11985 1.12 christos the type appropriate for that note type (e.g., prstatus_t for 11986 1.12 christos SOLARIS_NT_PRSTATUS) for the corresponding architecture 11987 1.12 christos on Solaris. The core file bitness may differ from the bitness of 11988 1.12 christos gdb itself, so fixed values are used instead of sizeof(). 11989 1.12 christos Appropriate fixed offsets are also used to obtain data from 11990 1.12 christos the note. */ 11991 1.12 christos 11992 1.12 christos switch ((int) note->type) 11993 1.12 christos { 11994 1.12 christos case SOLARIS_NT_PRSTATUS: 11995 1.12 christos switch (note->descsz) 11996 1.12 christos { 11997 1.12 christos case 508: /* sizeof(prstatus_t) SPARC 32-bit */ 11998 1.12 christos return elfcore_grok_solaris_prstatus(abfd, note, 11999 1.12 christos 136, 216, 308, 152, 356); 12000 1.12 christos case 904: /* sizeof(prstatus_t) SPARC 64-bit */ 12001 1.12 christos return elfcore_grok_solaris_prstatus(abfd, note, 12002 1.12 christos 264, 360, 520, 304, 600); 12003 1.12 christos case 432: /* sizeof(prstatus_t) Intel 32-bit */ 12004 1.12 christos return elfcore_grok_solaris_prstatus(abfd, note, 12005 1.12 christos 136, 216, 308, 76, 356); 12006 1.12 christos case 824: /* sizeof(prstatus_t) Intel 64-bit */ 12007 1.12 christos return elfcore_grok_solaris_prstatus(abfd, note, 12008 1.12 christos 264, 360, 520, 224, 600); 12009 1.12 christos default: 12010 1.12 christos return true; 12011 1.12 christos } 12012 1.12 christos 12013 1.12 christos case SOLARIS_NT_PSINFO: 12014 1.12 christos case SOLARIS_NT_PRPSINFO: 12015 1.12 christos switch (note->descsz) 12016 1.12 christos { 12017 1.12 christos case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */ 12018 1.12 christos return elfcore_grok_solaris_info(abfd, note, 84, 100); 12019 1.12 christos case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */ 12020 1.12 christos return elfcore_grok_solaris_info(abfd, note, 120, 136); 12021 1.12 christos case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */ 12022 1.12 christos return elfcore_grok_solaris_info(abfd, note, 88, 104); 12023 1.12 christos case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */ 12024 1.12 christos return elfcore_grok_solaris_info(abfd, note, 136, 152); 12025 1.12 christos default: 12026 1.12 christos return true; 12027 1.12 christos } 12028 1.12 christos 12029 1.12 christos case SOLARIS_NT_LWPSTATUS: 12030 1.12 christos switch (note->descsz) 12031 1.12 christos { 12032 1.12 christos case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */ 12033 1.12 christos return elfcore_grok_solaris_lwpstatus(abfd, note, 12034 1.12 christos 152, 344, 400, 496); 12035 1.12 christos case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */ 12036 1.12 christos return elfcore_grok_solaris_lwpstatus(abfd, note, 12037 1.12 christos 304, 544, 544, 848); 12038 1.12 christos case 800: /* sizeof(lwpstatus_t) Intel 32-bit */ 12039 1.12 christos return elfcore_grok_solaris_lwpstatus(abfd, note, 12040 1.12 christos 76, 344, 380, 420); 12041 1.12 christos case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */ 12042 1.12 christos return elfcore_grok_solaris_lwpstatus(abfd, note, 12043 1.12 christos 224, 544, 528, 768); 12044 1.12 christos default: 12045 1.12 christos return true; 12046 1.12 christos } 12047 1.12 christos 12048 1.12 christos case SOLARIS_NT_LWPSINFO: 12049 1.12 christos /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */ 12050 1.12 christos if (note->descsz == 128 || note->descsz == 152) 12051 1.12 christos elf_tdata (abfd)->core->lwpid = 12052 1.12 christos bfd_get_32 (abfd, note->descdata + 4); 12053 1.12 christos break; 12054 1.12 christos 12055 1.12 christos default: 12056 1.12 christos break; 12057 1.12 christos } 12058 1.12 christos 12059 1.12 christos return true; 12060 1.12 christos } 12061 1.12 christos 12062 1.12 christos /* For name starting with "CORE" this may be either a Solaris 12063 1.12 christos core file or a gdb-generated core file. Do Solaris-specific 12064 1.12 christos processing on selected note types first with 12065 1.12 christos elfcore_grok_solaris_note(), then process the note 12066 1.12 christos in elfcore_grok_note(). */ 12067 1.12 christos 12068 1.12 christos static bool 12069 1.12 christos elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note) 12070 1.1 christos { 12071 1.1 christos if (!elfcore_grok_solaris_note_impl (abfd, note)) 12072 1.12 christos return false; 12073 1.1 christos 12074 1.1 christos return elfcore_grok_note (abfd, note); 12075 1.1 christos } 12076 1.1 christos 12077 1.1 christos static bool 12078 1.1 christos elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) 12079 1.1 christos { 12080 1.1 christos if (note->type == NT_OPENBSD_PROCINFO) 12081 1.1 christos return elfcore_grok_openbsd_procinfo (abfd, note); 12082 1.1 christos 12083 1.1 christos if (note->type == NT_OPENBSD_REGS) 12084 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg", note); 12085 1.1 christos 12086 1.1 christos if (note->type == NT_OPENBSD_FPREGS) 12087 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg2", note); 12088 1.5 christos 12089 1.1 christos if (note->type == NT_OPENBSD_XFPREGS) 12090 1.1 christos return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); 12091 1.1 christos 12092 1.1 christos if (note->type == NT_OPENBSD_AUXV) 12093 1.1 christos return elfcore_make_auxv_note_section (abfd, note, 0); 12094 1.1 christos 12095 1.1 christos if (note->type == NT_OPENBSD_WCOOKIE) 12096 1.12 christos { 12097 1.1 christos asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", 12098 1.1 christos SEC_HAS_CONTENTS); 12099 1.1 christos 12100 1.1 christos if (sect == NULL) 12101 1.12 christos return false; 12102 1.1 christos sect->size = note->descsz; 12103 1.1 christos sect->filepos = note->descpos; 12104 1.12 christos sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; 12105 1.1 christos 12106 1.1 christos return true; 12107 1.12 christos } 12108 1.1 christos 12109 1.1 christos return true; 12110 1.1 christos } 12111 1.1 christos 12112 1.1 christos static bool 12113 1.1 christos elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) 12114 1.1 christos { 12115 1.1 christos void *ddata = note->descdata; 12116 1.1 christos char buf[100]; 12117 1.9 christos char *name; 12118 1.12 christos asection *sect; 12119 1.9 christos short sig; 12120 1.1 christos unsigned flags; 12121 1.3 christos 12122 1.1 christos if (note->descsz < 16) 12123 1.1 christos return false; 12124 1.1 christos 12125 1.1 christos /* nto_procfs_status 'pid' field is at offset 0. */ 12126 1.1 christos elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); 12127 1.1 christos 12128 1.1 christos /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ 12129 1.1 christos *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); 12130 1.1 christos 12131 1.1 christos /* nto_procfs_status 'flags' field is at offset 8. */ 12132 1.3 christos flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); 12133 1.3 christos 12134 1.1 christos /* nto_procfs_status 'what' field is at offset 14. */ 12135 1.1 christos if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) 12136 1.1 christos { 12137 1.1 christos elf_tdata (abfd)->core->signal = sig; 12138 1.1 christos elf_tdata (abfd)->core->lwpid = *tid; 12139 1.1 christos } 12140 1.3 christos 12141 1.1 christos /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores 12142 1.1 christos do not come from signals so we make sure we set the current 12143 1.1 christos thread just in case. */ 12144 1.1 christos if (flags & 0x00000080) 12145 1.1 christos elf_tdata (abfd)->core->lwpid = *tid; 12146 1.1 christos 12147 1.12 christos /* Make a ".qnx_core_status/%d" section. */ 12148 1.1 christos sprintf (buf, ".qnx_core_status/%ld", *tid); 12149 1.1 christos 12150 1.1 christos name = (char *) bfd_alloc (abfd, strlen (buf) + 1); 12151 1.1 christos if (name == NULL) 12152 1.12 christos return false; 12153 1.1 christos strcpy (name, buf); 12154 1.9 christos 12155 1.9 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 12156 1.1 christos if (sect == NULL) 12157 1.1 christos return false; 12158 1.1 christos 12159 1.1 christos sect->size = note->descsz; 12160 1.1 christos sect->filepos = note->descpos; 12161 1.12 christos sect->alignment_power = 2; 12162 1.1 christos 12163 1.1 christos return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); 12164 1.1 christos } 12165 1.1 christos 12166 1.1 christos static bool 12167 1.1 christos elfcore_grok_nto_regs (bfd *abfd, 12168 1.1 christos Elf_Internal_Note *note, 12169 1.1 christos long tid, 12170 1.1 christos char *base) 12171 1.1 christos { 12172 1.1 christos char buf[100]; 12173 1.1 christos char *name; 12174 1.1 christos asection *sect; 12175 1.1 christos 12176 1.12 christos /* Make a "(base)/%d" section. */ 12177 1.1 christos sprintf (buf, "%s/%ld", base, tid); 12178 1.1 christos 12179 1.1 christos name = (char *) bfd_alloc (abfd, strlen (buf) + 1); 12180 1.1 christos if (name == NULL) 12181 1.12 christos return false; 12182 1.1 christos strcpy (name, buf); 12183 1.9 christos 12184 1.9 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 12185 1.1 christos if (sect == NULL) 12186 1.1 christos return false; 12187 1.1 christos 12188 1.3 christos sect->size = note->descsz; 12189 1.1 christos sect->filepos = note->descpos; 12190 1.1 christos sect->alignment_power = 2; 12191 1.12 christos 12192 1.1 christos /* This is the current thread. */ 12193 1.1 christos if (elf_tdata (abfd)->core->lwpid == tid) 12194 1.12 christos return elfcore_maybe_make_sect (abfd, base, sect); 12195 1.1 christos 12196 1.1 christos return true; 12197 1.1 christos } 12198 1.1 christos 12199 1.1 christos static bool 12200 1.1 christos elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) 12201 1.1 christos { 12202 1.1 christos /* Every GREG section has a STATUS section before it. Store the 12203 1.1 christos tid from the previous call to pass down to the next gregs 12204 1.13 christos function. */ 12205 1.1 christos static long tid = 1; 12206 1.13 christos 12207 1.1 christos switch (note->type) 12208 1.13 christos { 12209 1.1 christos case QNT_CORE_INFO: 12210 1.13 christos return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); 12211 1.1 christos case QNT_CORE_STATUS: 12212 1.1 christos return elfcore_grok_nto_status (abfd, note, &tid); 12213 1.12 christos case QNT_CORE_GREG: 12214 1.1 christos return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); 12215 1.1 christos case QNT_CORE_FPREG: 12216 1.1 christos return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); 12217 1.12 christos default: 12218 1.1 christos return true; 12219 1.1 christos } 12220 1.1 christos } 12221 1.1 christos 12222 1.1 christos static bool 12223 1.1 christos elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) 12224 1.1 christos { 12225 1.1 christos char *name; 12226 1.1 christos asection *sect; 12227 1.1 christos size_t len; 12228 1.12 christos 12229 1.1 christos /* Use note name as section name. */ 12230 1.1 christos len = note->namesz; 12231 1.1 christos name = (char *) bfd_alloc (abfd, len); 12232 1.1 christos if (name == NULL) 12233 1.1 christos return false; 12234 1.12 christos memcpy (name, note->namedata, len); 12235 1.1 christos name[len - 1] = '\0'; 12236 1.9 christos 12237 1.9 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 12238 1.1 christos if (sect == NULL) 12239 1.1 christos return false; 12240 1.12 christos 12241 1.1 christos sect->size = note->descsz; 12242 1.1 christos sect->filepos = note->descpos; 12243 1.1 christos sect->alignment_power = 1; 12244 1.1 christos 12245 1.1 christos return true; 12246 1.1 christos } 12247 1.1 christos 12248 1.1 christos /* Function: elfcore_write_note 12249 1.1 christos 12250 1.1 christos Inputs: 12251 1.1 christos buffer to hold note, and current size of buffer 12252 1.1 christos name of note 12253 1.1 christos type of note 12254 1.1 christos data for note 12255 1.1 christos size of data for note 12256 1.1 christos 12257 1.1 christos Writes note to end of buffer. ELF64 notes are written exactly as 12258 1.1 christos for ELF32, despite the current (as of 2006) ELF gabi specifying 12259 1.1 christos that they ought to have 8-byte namesz and descsz field, and have 12260 1.1 christos 8-byte alignment. Other writers, eg. Linux kernel, do the same. 12261 1.1 christos 12262 1.1 christos Return: 12263 1.1 christos Pointer to realloc'd buffer, *BUFSIZ updated. */ 12264 1.1 christos 12265 1.1 christos char * 12266 1.1 christos elfcore_write_note (bfd *abfd, 12267 1.1 christos char *buf, 12268 1.1 christos int *bufsiz, 12269 1.1 christos const char *name, 12270 1.1 christos int type, 12271 1.1 christos const void *input, 12272 1.1 christos int size) 12273 1.1 christos { 12274 1.1 christos Elf_External_Note *xnp; 12275 1.1 christos size_t namesz; 12276 1.1 christos size_t newspace; 12277 1.1 christos char *dest; 12278 1.1 christos 12279 1.1 christos namesz = 0; 12280 1.1 christos if (name != NULL) 12281 1.1 christos namesz = strlen (name) + 1; 12282 1.1 christos 12283 1.1 christos newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); 12284 1.1 christos 12285 1.1 christos buf = (char *) realloc (buf, *bufsiz + newspace); 12286 1.1 christos if (buf == NULL) 12287 1.1 christos return buf; 12288 1.1 christos dest = buf + *bufsiz; 12289 1.1 christos *bufsiz += newspace; 12290 1.1 christos xnp = (Elf_External_Note *) dest; 12291 1.1 christos H_PUT_32 (abfd, namesz, xnp->namesz); 12292 1.1 christos H_PUT_32 (abfd, size, xnp->descsz); 12293 1.1 christos H_PUT_32 (abfd, type, xnp->type); 12294 1.1 christos dest = xnp->name; 12295 1.1 christos if (name != NULL) 12296 1.1 christos { 12297 1.1 christos memcpy (dest, name, namesz); 12298 1.1 christos dest += namesz; 12299 1.1 christos while (namesz & 3) 12300 1.1 christos { 12301 1.1 christos *dest++ = '\0'; 12302 1.1 christos ++namesz; 12303 1.1 christos } 12304 1.1 christos } 12305 1.1 christos memcpy (dest, input, size); 12306 1.1 christos dest += size; 12307 1.1 christos while (size & 3) 12308 1.1 christos { 12309 1.1 christos *dest++ = '\0'; 12310 1.9 christos ++size; 12311 1.9 christos } 12312 1.9 christos return buf; 12313 1.9 christos } 12314 1.9 christos 12315 1.9 christos /* gcc-8 warns (*) on all the strncpy calls in this function about 12316 1.9 christos possible string truncation. The "truncation" is not a bug. We 12317 1.9 christos have an external representation of structs with fields that are not 12318 1.9 christos necessarily NULL terminated and corresponding internal 12319 1.9 christos representation fields that are one larger so that they can always 12320 1.9 christos be NULL terminated. 12321 1.9 christos gcc versions between 4.2 and 4.6 do not allow pragma control of 12322 1.9 christos diagnostics inside functions, giving a hard error if you try to use 12323 1.9 christos the finer control available with later versions. 12324 1.9 christos gcc prior to 4.2 warns about diagnostic push and pop. 12325 1.9 christos gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown, 12326 1.9 christos unless you also add #pragma GCC diagnostic ignored "-Wpragma". 12327 1.1 christos (*) Depending on your system header files! */ 12328 1.1 christos #if GCC_VERSION >= 8000 12329 1.1 christos # pragma GCC diagnostic push 12330 1.1 christos # pragma GCC diagnostic ignored "-Wstringop-truncation" 12331 1.1 christos #endif 12332 1.1 christos char * 12333 1.1 christos elfcore_write_prpsinfo (bfd *abfd, 12334 1.1 christos char *buf, 12335 1.1 christos int *bufsiz, 12336 1.1 christos const char *fname, 12337 1.1 christos const char *psargs) 12338 1.1 christos { 12339 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12340 1.1 christos 12341 1.1 christos if (bed->elf_backend_write_core_note != NULL) 12342 1.1 christos { 12343 1.1 christos char *ret; 12344 1.1 christos ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, 12345 1.1 christos NT_PRPSINFO, fname, psargs); 12346 1.9 christos if (ret != NULL) 12347 1.1 christos return ret; 12348 1.1 christos } 12349 1.9 christos 12350 1.1 christos #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 12351 1.1 christos # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) 12352 1.9 christos if (bed->s->elfclass == ELFCLASS32) 12353 1.1 christos { 12354 1.1 christos # if defined (HAVE_PSINFO32_T) 12355 1.9 christos psinfo32_t data; 12356 1.1 christos int note_type = NT_PSINFO; 12357 1.1 christos # else 12358 1.1 christos prpsinfo32_t data; 12359 1.1 christos int note_type = NT_PRPSINFO; 12360 1.1 christos # endif 12361 1.1 christos 12362 1.1 christos memset (&data, 0, sizeof (data)); 12363 1.1 christos strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); 12364 1.9 christos strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); 12365 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12366 1.9 christos "CORE", note_type, &data, sizeof (data)); 12367 1.1 christos } 12368 1.1 christos else 12369 1.9 christos # endif 12370 1.1 christos { 12371 1.1 christos # if defined (HAVE_PSINFO_T) 12372 1.9 christos psinfo_t data; 12373 1.1 christos int note_type = NT_PSINFO; 12374 1.1 christos # else 12375 1.1 christos prpsinfo_t data; 12376 1.1 christos int note_type = NT_PRPSINFO; 12377 1.1 christos # endif 12378 1.1 christos 12379 1.1 christos memset (&data, 0, sizeof (data)); 12380 1.1 christos strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); 12381 1.1 christos strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); 12382 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12383 1.1 christos "CORE", note_type, &data, sizeof (data)); 12384 1.1 christos } 12385 1.9 christos #endif /* PSINFO_T or PRPSINFO_T */ 12386 1.9 christos 12387 1.9 christos free (buf); 12388 1.1 christos return NULL; 12389 1.1 christos } 12390 1.3 christos #if GCC_VERSION >= 8000 12391 1.3 christos # pragma GCC diagnostic pop 12392 1.3 christos #endif 12393 1.3 christos 12394 1.9 christos char * 12395 1.9 christos elfcore_write_linux_prpsinfo32 12396 1.9 christos (bfd *abfd, char *buf, int *bufsiz, 12397 1.3 christos const struct elf_internal_linux_prpsinfo *prpsinfo) 12398 1.9 christos { 12399 1.9 christos if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16) 12400 1.9 christos { 12401 1.9 christos struct elf_external_linux_prpsinfo32_ugid16 data; 12402 1.9 christos 12403 1.9 christos swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data); 12404 1.9 christos return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, 12405 1.9 christos &data, sizeof (data)); 12406 1.9 christos } 12407 1.9 christos else 12408 1.9 christos { 12409 1.9 christos struct elf_external_linux_prpsinfo32_ugid32 data; 12410 1.3 christos 12411 1.3 christos swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data); 12412 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, 12413 1.3 christos &data, sizeof (data)); 12414 1.3 christos } 12415 1.3 christos } 12416 1.3 christos 12417 1.9 christos char * 12418 1.9 christos elfcore_write_linux_prpsinfo64 12419 1.9 christos (bfd *abfd, char *buf, int *bufsiz, 12420 1.3 christos const struct elf_internal_linux_prpsinfo *prpsinfo) 12421 1.9 christos { 12422 1.9 christos if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16) 12423 1.9 christos { 12424 1.9 christos struct elf_external_linux_prpsinfo64_ugid16 data; 12425 1.9 christos 12426 1.9 christos swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data); 12427 1.9 christos return elfcore_write_note (abfd, buf, bufsiz, 12428 1.9 christos "CORE", NT_PRPSINFO, &data, sizeof (data)); 12429 1.9 christos } 12430 1.9 christos else 12431 1.9 christos { 12432 1.9 christos struct elf_external_linux_prpsinfo64_ugid32 data; 12433 1.3 christos 12434 1.3 christos swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data); 12435 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, 12436 1.1 christos "CORE", NT_PRPSINFO, &data, sizeof (data)); 12437 1.1 christos } 12438 1.1 christos } 12439 1.1 christos 12440 1.1 christos char * 12441 1.1 christos elfcore_write_prstatus (bfd *abfd, 12442 1.1 christos char *buf, 12443 1.1 christos int *bufsiz, 12444 1.1 christos long pid, 12445 1.1 christos int cursig, 12446 1.1 christos const void *gregs) 12447 1.1 christos { 12448 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12449 1.1 christos 12450 1.1 christos if (bed->elf_backend_write_core_note != NULL) 12451 1.1 christos { 12452 1.1 christos char *ret; 12453 1.1 christos ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, 12454 1.1 christos NT_PRSTATUS, 12455 1.1 christos pid, cursig, gregs); 12456 1.1 christos if (ret != NULL) 12457 1.1 christos return ret; 12458 1.1 christos } 12459 1.1 christos 12460 1.1 christos #if defined (HAVE_PRSTATUS_T) 12461 1.1 christos #if defined (HAVE_PRSTATUS32_T) 12462 1.1 christos if (bed->s->elfclass == ELFCLASS32) 12463 1.1 christos { 12464 1.1 christos prstatus32_t prstat; 12465 1.1 christos 12466 1.1 christos memset (&prstat, 0, sizeof (prstat)); 12467 1.1 christos prstat.pr_pid = pid; 12468 1.1 christos prstat.pr_cursig = cursig; 12469 1.1 christos memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); 12470 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, "CORE", 12471 1.1 christos NT_PRSTATUS, &prstat, sizeof (prstat)); 12472 1.1 christos } 12473 1.1 christos else 12474 1.1 christos #endif 12475 1.1 christos { 12476 1.1 christos prstatus_t prstat; 12477 1.1 christos 12478 1.1 christos memset (&prstat, 0, sizeof (prstat)); 12479 1.1 christos prstat.pr_pid = pid; 12480 1.1 christos prstat.pr_cursig = cursig; 12481 1.1 christos memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); 12482 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, "CORE", 12483 1.1 christos NT_PRSTATUS, &prstat, sizeof (prstat)); 12484 1.1 christos } 12485 1.1 christos #endif /* HAVE_PRSTATUS_T */ 12486 1.1 christos 12487 1.1 christos free (buf); 12488 1.1 christos return NULL; 12489 1.1 christos } 12490 1.1 christos 12491 1.1 christos #if defined (HAVE_LWPSTATUS_T) 12492 1.1 christos char * 12493 1.1 christos elfcore_write_lwpstatus (bfd *abfd, 12494 1.1 christos char *buf, 12495 1.1 christos int *bufsiz, 12496 1.1 christos long pid, 12497 1.1 christos int cursig, 12498 1.1 christos const void *gregs) 12499 1.1 christos { 12500 1.1 christos lwpstatus_t lwpstat; 12501 1.1 christos const char *note_name = "CORE"; 12502 1.3 christos 12503 1.1 christos memset (&lwpstat, 0, sizeof (lwpstat)); 12504 1.1 christos lwpstat.pr_lwpid = pid >> 16; 12505 1.1 christos lwpstat.pr_cursig = cursig; 12506 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_REG) 12507 1.1 christos memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); 12508 1.1 christos #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 12509 1.1 christos #if !defined(gregs) 12510 1.1 christos memcpy (lwpstat.pr_context.uc_mcontext.gregs, 12511 1.1 christos gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); 12512 1.1 christos #else 12513 1.1 christos memcpy (lwpstat.pr_context.uc_mcontext.__gregs, 12514 1.1 christos gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); 12515 1.1 christos #endif 12516 1.1 christos #endif 12517 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, note_name, 12518 1.1 christos NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); 12519 1.1 christos } 12520 1.1 christos #endif /* HAVE_LWPSTATUS_T */ 12521 1.1 christos 12522 1.1 christos #if defined (HAVE_PSTATUS_T) 12523 1.1 christos char * 12524 1.1 christos elfcore_write_pstatus (bfd *abfd, 12525 1.1 christos char *buf, 12526 1.1 christos int *bufsiz, 12527 1.1 christos long pid, 12528 1.1 christos int cursig ATTRIBUTE_UNUSED, 12529 1.1 christos const void *gregs ATTRIBUTE_UNUSED) 12530 1.1 christos { 12531 1.1 christos const char *note_name = "CORE"; 12532 1.1 christos #if defined (HAVE_PSTATUS32_T) 12533 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12534 1.1 christos 12535 1.1 christos if (bed->s->elfclass == ELFCLASS32) 12536 1.1 christos { 12537 1.1 christos pstatus32_t pstat; 12538 1.1 christos 12539 1.1 christos memset (&pstat, 0, sizeof (pstat)); 12540 1.1 christos pstat.pr_pid = pid & 0xffff; 12541 1.1 christos buf = elfcore_write_note (abfd, buf, bufsiz, note_name, 12542 1.1 christos NT_PSTATUS, &pstat, sizeof (pstat)); 12543 1.1 christos return buf; 12544 1.1 christos } 12545 1.1 christos else 12546 1.1 christos #endif 12547 1.1 christos { 12548 1.1 christos pstatus_t pstat; 12549 1.1 christos 12550 1.1 christos memset (&pstat, 0, sizeof (pstat)); 12551 1.1 christos pstat.pr_pid = pid & 0xffff; 12552 1.1 christos buf = elfcore_write_note (abfd, buf, bufsiz, note_name, 12553 1.1 christos NT_PSTATUS, &pstat, sizeof (pstat)); 12554 1.1 christos return buf; 12555 1.1 christos } 12556 1.1 christos } 12557 1.1 christos #endif /* HAVE_PSTATUS_T */ 12558 1.1 christos 12559 1.1 christos char * 12560 1.1 christos elfcore_write_prfpreg (bfd *abfd, 12561 1.1 christos char *buf, 12562 1.1 christos int *bufsiz, 12563 1.1 christos const void *fpregs, 12564 1.1 christos int size) 12565 1.1 christos { 12566 1.1 christos const char *note_name = "CORE"; 12567 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12568 1.1 christos note_name, NT_FPREGSET, fpregs, size); 12569 1.1 christos } 12570 1.1 christos 12571 1.1 christos char * 12572 1.1 christos elfcore_write_prxfpreg (bfd *abfd, 12573 1.1 christos char *buf, 12574 1.1 christos int *bufsiz, 12575 1.1 christos const void *xfpregs, 12576 1.1 christos int size) 12577 1.1 christos { 12578 1.1 christos char *note_name = "LINUX"; 12579 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12580 1.1 christos note_name, NT_PRXFPREG, xfpregs, size); 12581 1.1 christos } 12582 1.3 christos 12583 1.3 christos char * 12584 1.3 christos elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, 12585 1.3 christos const void *xfpregs, int size) 12586 1.3 christos { 12587 1.1 christos char *note_name; 12588 1.1 christos if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) 12589 1.1 christos note_name = "FreeBSD"; 12590 1.1 christos else 12591 1.14 christos note_name = "LINUX"; 12592 1.14 christos return elfcore_write_note (abfd, buf, bufsiz, 12593 1.14 christos note_name, NT_X86_XSTATE, xfpregs, size); 12594 1.14 christos } 12595 1.14 christos 12596 1.14 christos static char * 12597 1.14 christos elfcore_write_sspreg (bfd *abfd, char *buf, int *bufsiz, 12598 1.14 christos const void *ssp, int size) 12599 1.14 christos { 12600 1.1 christos const char *note_name = "LINUX"; 12601 1.12 christos return elfcore_write_note (abfd, buf, bufsiz, 12602 1.12 christos note_name, NT_X86_SHSTK, ssp, size); 12603 1.12 christos } 12604 1.12 christos 12605 1.12 christos char * 12606 1.12 christos elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz, 12607 1.12 christos const void *regs, int size) 12608 1.12 christos { 12609 1.12 christos char *note_name = "FreeBSD"; 12610 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12611 1.1 christos note_name, NT_FREEBSD_X86_SEGBASES, regs, size); 12612 1.1 christos } 12613 1.1 christos 12614 1.1 christos char * 12615 1.1 christos elfcore_write_ppc_vmx (bfd *abfd, 12616 1.1 christos char *buf, 12617 1.1 christos int *bufsiz, 12618 1.1 christos const void *ppc_vmx, 12619 1.1 christos int size) 12620 1.1 christos { 12621 1.1 christos char *note_name = "LINUX"; 12622 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12623 1.9 christos note_name, NT_PPC_VMX, ppc_vmx, size); 12624 1.9 christos } 12625 1.9 christos 12626 1.9 christos char * 12627 1.1 christos elfcore_write_ppc_vsx (bfd *abfd, 12628 1.1 christos char *buf, 12629 1.1 christos int *bufsiz, 12630 1.9 christos const void *ppc_vsx, 12631 1.1 christos int size) 12632 1.1 christos { 12633 1.10 christos char *note_name = "LINUX"; 12634 1.10 christos return elfcore_write_note (abfd, buf, bufsiz, 12635 1.10 christos note_name, NT_PPC_VSX, ppc_vsx, size); 12636 1.10 christos } 12637 1.10 christos 12638 1.10 christos char * 12639 1.10 christos elfcore_write_ppc_tar (bfd *abfd, 12640 1.10 christos char *buf, 12641 1.10 christos int *bufsiz, 12642 1.10 christos const void *ppc_tar, 12643 1.10 christos int size) 12644 1.10 christos { 12645 1.10 christos char *note_name = "LINUX"; 12646 1.10 christos return elfcore_write_note (abfd, buf, bufsiz, 12647 1.10 christos note_name, NT_PPC_TAR, ppc_tar, size); 12648 1.10 christos } 12649 1.10 christos 12650 1.10 christos char * 12651 1.10 christos elfcore_write_ppc_ppr (bfd *abfd, 12652 1.10 christos char *buf, 12653 1.10 christos int *bufsiz, 12654 1.10 christos const void *ppc_ppr, 12655 1.10 christos int size) 12656 1.10 christos { 12657 1.10 christos char *note_name = "LINUX"; 12658 1.10 christos return elfcore_write_note (abfd, buf, bufsiz, 12659 1.10 christos note_name, NT_PPC_PPR, ppc_ppr, size); 12660 1.10 christos } 12661 1.10 christos 12662 1.10 christos char * 12663 1.10 christos elfcore_write_ppc_dscr (bfd *abfd, 12664 1.10 christos char *buf, 12665 1.10 christos int *bufsiz, 12666 1.10 christos const void *ppc_dscr, 12667 1.10 christos int size) 12668 1.10 christos { 12669 1.10 christos char *note_name = "LINUX"; 12670 1.10 christos return elfcore_write_note (abfd, buf, bufsiz, 12671 1.10 christos note_name, NT_PPC_DSCR, ppc_dscr, size); 12672 1.10 christos } 12673 1.10 christos 12674 1.10 christos char * 12675 1.10 christos elfcore_write_ppc_ebb (bfd *abfd, 12676 1.10 christos char *buf, 12677 1.10 christos int *bufsiz, 12678 1.10 christos const void *ppc_ebb, 12679 1.10 christos int size) 12680 1.10 christos { 12681 1.10 christos char *note_name = "LINUX"; 12682 1.10 christos return elfcore_write_note (abfd, buf, bufsiz, 12683 1.10 christos note_name, NT_PPC_EBB, ppc_ebb, size); 12684 1.10 christos } 12685 1.10 christos 12686 1.10 christos char * 12687 1.10 christos elfcore_write_ppc_pmu (bfd *abfd, 12688 1.10 christos char *buf, 12689 1.10 christos int *bufsiz, 12690 1.10 christos const void *ppc_pmu, 12691 1.10 christos int size) 12692 1.10 christos { 12693 1.10 christos char *note_name = "LINUX"; 12694 1.10 christos return elfcore_write_note (abfd, buf, bufsiz, 12695 1.10 christos note_name, NT_PPC_PMU, ppc_pmu, size); 12696 1.10 christos } 12697 1.10 christos 12698 1.10 christos char * 12699 1.10 christos elfcore_write_ppc_tm_cgpr (bfd *abfd, 12700 1.10 christos char *buf, 12701 1.10 christos int *bufsiz, 12702 1.10 christos const void *ppc_tm_cgpr, 12703 1.10 christos int size) 12704 1.10 christos { 12705 1.10 christos char *note_name = "LINUX"; 12706 1.10 christos return elfcore_write_note (abfd, buf, bufsiz, 12707 1.10 christos note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size); 12708 1.10 christos } 12709 1.10 christos 12710 1.10 christos char * 12711 1.10 christos elfcore_write_ppc_tm_cfpr (bfd *abfd, 12712 1.10 christos char *buf, 12713 1.10 christos int *bufsiz, 12714 1.10 christos const void *ppc_tm_cfpr, 12715 1.10 christos int size) 12716 1.10 christos { 12717 1.10 christos char *note_name = "LINUX"; 12718 1.10 christos return elfcore_write_note (abfd, buf, bufsiz, 12719 1.10 christos note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size); 12720 1.10 christos } 12721 1.10 christos 12722 1.10 christos char * 12723 1.10 christos elfcore_write_ppc_tm_cvmx (bfd *abfd, 12724 1.10 christos char *buf, 12725 1.10 christos int *bufsiz, 12726 1.10 christos const void *ppc_tm_cvmx, 12727 1.10 christos int size) 12728 1.10 christos { 12729 1.10 christos char *note_name = "LINUX"; 12730 1.10 christos return elfcore_write_note (abfd, buf, bufsiz, 12731 1.10 christos note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size); 12732 1.10 christos } 12733 1.10 christos 12734 1.10 christos char * 12735 1.10 christos elfcore_write_ppc_tm_cvsx (bfd *abfd, 12736 1.10 christos char *buf, 12737 1.10 christos int *bufsiz, 12738 1.10 christos const void *ppc_tm_cvsx, 12739 1.10 christos int size) 12740 1.10 christos { 12741 1.10 christos char *note_name = "LINUX"; 12742 1.10 christos return elfcore_write_note (abfd, buf, bufsiz, 12743 1.10 christos note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size); 12744 1.10 christos } 12745 1.10 christos 12746 1.10 christos char * 12747 1.10 christos elfcore_write_ppc_tm_spr (bfd *abfd, 12748 1.10 christos char *buf, 12749 1.10 christos int *bufsiz, 12750 1.10 christos const void *ppc_tm_spr, 12751 1.10 christos int size) 12752 1.10 christos { 12753 1.10 christos char *note_name = "LINUX"; 12754 1.10 christos return elfcore_write_note (abfd, buf, bufsiz, 12755 1.10 christos note_name, NT_PPC_TM_SPR, ppc_tm_spr, size); 12756 1.10 christos } 12757 1.10 christos 12758 1.10 christos char * 12759 1.10 christos elfcore_write_ppc_tm_ctar (bfd *abfd, 12760 1.10 christos char *buf, 12761 1.10 christos int *bufsiz, 12762 1.10 christos const void *ppc_tm_ctar, 12763 1.10 christos int size) 12764 1.10 christos { 12765 1.10 christos char *note_name = "LINUX"; 12766 1.10 christos return elfcore_write_note (abfd, buf, bufsiz, 12767 1.10 christos note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size); 12768 1.10 christos } 12769 1.10 christos 12770 1.10 christos char * 12771 1.10 christos elfcore_write_ppc_tm_cppr (bfd *abfd, 12772 1.10 christos char *buf, 12773 1.10 christos int *bufsiz, 12774 1.10 christos const void *ppc_tm_cppr, 12775 1.10 christos int size) 12776 1.10 christos { 12777 1.10 christos char *note_name = "LINUX"; 12778 1.10 christos return elfcore_write_note (abfd, buf, bufsiz, 12779 1.10 christos note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size); 12780 1.10 christos } 12781 1.10 christos 12782 1.10 christos char * 12783 1.10 christos elfcore_write_ppc_tm_cdscr (bfd *abfd, 12784 1.10 christos char *buf, 12785 1.10 christos int *bufsiz, 12786 1.10 christos const void *ppc_tm_cdscr, 12787 1.10 christos int size) 12788 1.10 christos { 12789 1.1 christos char *note_name = "LINUX"; 12790 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12791 1.1 christos note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size); 12792 1.1 christos } 12793 1.1 christos 12794 1.1 christos static char * 12795 1.1 christos elfcore_write_s390_high_gprs (bfd *abfd, 12796 1.1 christos char *buf, 12797 1.1 christos int *bufsiz, 12798 1.9 christos const void *s390_high_gprs, 12799 1.1 christos int size) 12800 1.1 christos { 12801 1.1 christos char *note_name = "LINUX"; 12802 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12803 1.1 christos note_name, NT_S390_HIGH_GPRS, 12804 1.9 christos s390_high_gprs, size); 12805 1.9 christos } 12806 1.9 christos 12807 1.9 christos char * 12808 1.1 christos elfcore_write_s390_timer (bfd *abfd, 12809 1.1 christos char *buf, 12810 1.1 christos int *bufsiz, 12811 1.9 christos const void *s390_timer, 12812 1.1 christos int size) 12813 1.1 christos { 12814 1.1 christos char *note_name = "LINUX"; 12815 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12816 1.9 christos note_name, NT_S390_TIMER, s390_timer, size); 12817 1.9 christos } 12818 1.9 christos 12819 1.9 christos char * 12820 1.1 christos elfcore_write_s390_todcmp (bfd *abfd, 12821 1.1 christos char *buf, 12822 1.1 christos int *bufsiz, 12823 1.9 christos const void *s390_todcmp, 12824 1.1 christos int size) 12825 1.1 christos { 12826 1.1 christos char *note_name = "LINUX"; 12827 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12828 1.9 christos note_name, NT_S390_TODCMP, s390_todcmp, size); 12829 1.9 christos } 12830 1.9 christos 12831 1.9 christos char * 12832 1.1 christos elfcore_write_s390_todpreg (bfd *abfd, 12833 1.1 christos char *buf, 12834 1.1 christos int *bufsiz, 12835 1.9 christos const void *s390_todpreg, 12836 1.1 christos int size) 12837 1.1 christos { 12838 1.1 christos char *note_name = "LINUX"; 12839 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12840 1.9 christos note_name, NT_S390_TODPREG, s390_todpreg, size); 12841 1.9 christos } 12842 1.9 christos 12843 1.9 christos char * 12844 1.1 christos elfcore_write_s390_ctrs (bfd *abfd, 12845 1.1 christos char *buf, 12846 1.1 christos int *bufsiz, 12847 1.9 christos const void *s390_ctrs, 12848 1.1 christos int size) 12849 1.1 christos { 12850 1.1 christos char *note_name = "LINUX"; 12851 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12852 1.9 christos note_name, NT_S390_CTRS, s390_ctrs, size); 12853 1.9 christos } 12854 1.9 christos 12855 1.9 christos char * 12856 1.1 christos elfcore_write_s390_prefix (bfd *abfd, 12857 1.1 christos char *buf, 12858 1.1 christos int *bufsiz, 12859 1.9 christos const void *s390_prefix, 12860 1.1 christos int size) 12861 1.1 christos { 12862 1.1 christos char *note_name = "LINUX"; 12863 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12864 1.1 christos note_name, NT_S390_PREFIX, s390_prefix, size); 12865 1.1 christos } 12866 1.1 christos 12867 1.1 christos char * 12868 1.1 christos elfcore_write_s390_last_break (bfd *abfd, 12869 1.1 christos char *buf, 12870 1.1 christos int *bufsiz, 12871 1.9 christos const void *s390_last_break, 12872 1.1 christos int size) 12873 1.1 christos { 12874 1.1 christos char *note_name = "LINUX"; 12875 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12876 1.1 christos note_name, NT_S390_LAST_BREAK, 12877 1.1 christos s390_last_break, size); 12878 1.1 christos } 12879 1.1 christos 12880 1.1 christos char * 12881 1.1 christos elfcore_write_s390_system_call (bfd *abfd, 12882 1.1 christos char *buf, 12883 1.1 christos int *bufsiz, 12884 1.9 christos const void *s390_system_call, 12885 1.1 christos int size) 12886 1.1 christos { 12887 1.1 christos char *note_name = "LINUX"; 12888 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12889 1.3 christos note_name, NT_S390_SYSTEM_CALL, 12890 1.3 christos s390_system_call, size); 12891 1.3 christos } 12892 1.3 christos 12893 1.3 christos char * 12894 1.3 christos elfcore_write_s390_tdb (bfd *abfd, 12895 1.3 christos char *buf, 12896 1.3 christos int *bufsiz, 12897 1.9 christos const void *s390_tdb, 12898 1.3 christos int size) 12899 1.3 christos { 12900 1.3 christos char *note_name = "LINUX"; 12901 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, 12902 1.3 christos note_name, NT_S390_TDB, s390_tdb, size); 12903 1.3 christos } 12904 1.3 christos 12905 1.3 christos char * 12906 1.3 christos elfcore_write_s390_vxrs_low (bfd *abfd, 12907 1.3 christos char *buf, 12908 1.3 christos int *bufsiz, 12909 1.3 christos const void *s390_vxrs_low, 12910 1.3 christos int size) 12911 1.3 christos { 12912 1.3 christos char *note_name = "LINUX"; 12913 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, 12914 1.3 christos note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); 12915 1.3 christos } 12916 1.3 christos 12917 1.3 christos char * 12918 1.3 christos elfcore_write_s390_vxrs_high (bfd *abfd, 12919 1.3 christos char *buf, 12920 1.3 christos int *bufsiz, 12921 1.3 christos const void *s390_vxrs_high, 12922 1.3 christos int size) 12923 1.3 christos { 12924 1.3 christos char *note_name = "LINUX"; 12925 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, 12926 1.9 christos note_name, NT_S390_VXRS_HIGH, 12927 1.9 christos s390_vxrs_high, size); 12928 1.9 christos } 12929 1.9 christos 12930 1.9 christos char * 12931 1.9 christos elfcore_write_s390_gs_cb (bfd *abfd, 12932 1.9 christos char *buf, 12933 1.9 christos int *bufsiz, 12934 1.9 christos const void *s390_gs_cb, 12935 1.9 christos int size) 12936 1.9 christos { 12937 1.9 christos char *note_name = "LINUX"; 12938 1.9 christos return elfcore_write_note (abfd, buf, bufsiz, 12939 1.9 christos note_name, NT_S390_GS_CB, 12940 1.9 christos s390_gs_cb, size); 12941 1.9 christos } 12942 1.9 christos 12943 1.9 christos char * 12944 1.9 christos elfcore_write_s390_gs_bc (bfd *abfd, 12945 1.9 christos char *buf, 12946 1.9 christos int *bufsiz, 12947 1.9 christos const void *s390_gs_bc, 12948 1.9 christos int size) 12949 1.9 christos { 12950 1.9 christos char *note_name = "LINUX"; 12951 1.9 christos return elfcore_write_note (abfd, buf, bufsiz, 12952 1.1 christos note_name, NT_S390_GS_BC, 12953 1.1 christos s390_gs_bc, size); 12954 1.1 christos } 12955 1.1 christos 12956 1.1 christos char * 12957 1.1 christos elfcore_write_arm_vfp (bfd *abfd, 12958 1.1 christos char *buf, 12959 1.1 christos int *bufsiz, 12960 1.1 christos const void *arm_vfp, 12961 1.1 christos int size) 12962 1.1 christos { 12963 1.1 christos char *note_name = "LINUX"; 12964 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, 12965 1.3 christos note_name, NT_ARM_VFP, arm_vfp, size); 12966 1.3 christos } 12967 1.3 christos 12968 1.3 christos char * 12969 1.3 christos elfcore_write_aarch_tls (bfd *abfd, 12970 1.3 christos char *buf, 12971 1.3 christos int *bufsiz, 12972 1.3 christos const void *aarch_tls, 12973 1.3 christos int size) 12974 1.3 christos { 12975 1.3 christos char *note_name = "LINUX"; 12976 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, 12977 1.3 christos note_name, NT_ARM_TLS, aarch_tls, size); 12978 1.3 christos } 12979 1.3 christos 12980 1.3 christos char * 12981 1.3 christos elfcore_write_aarch_hw_break (bfd *abfd, 12982 1.3 christos char *buf, 12983 1.3 christos int *bufsiz, 12984 1.3 christos const void *aarch_hw_break, 12985 1.3 christos int size) 12986 1.3 christos { 12987 1.3 christos char *note_name = "LINUX"; 12988 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, 12989 1.3 christos note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); 12990 1.3 christos } 12991 1.3 christos 12992 1.3 christos char * 12993 1.3 christos elfcore_write_aarch_hw_watch (bfd *abfd, 12994 1.3 christos char *buf, 12995 1.3 christos int *bufsiz, 12996 1.3 christos const void *aarch_hw_watch, 12997 1.3 christos int size) 12998 1.3 christos { 12999 1.3 christos char *note_name = "LINUX"; 13000 1.10 christos return elfcore_write_note (abfd, buf, bufsiz, 13001 1.10 christos note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); 13002 1.10 christos } 13003 1.10 christos 13004 1.10 christos char * 13005 1.10 christos elfcore_write_aarch_sve (bfd *abfd, 13006 1.10 christos char *buf, 13007 1.10 christos int *bufsiz, 13008 1.10 christos const void *aarch_sve, 13009 1.10 christos int size) 13010 1.10 christos { 13011 1.10 christos char *note_name = "LINUX"; 13012 1.10 christos return elfcore_write_note (abfd, buf, bufsiz, 13013 1.10 christos note_name, NT_ARM_SVE, aarch_sve, size); 13014 1.10 christos } 13015 1.10 christos 13016 1.10 christos char * 13017 1.10 christos elfcore_write_aarch_pauth (bfd *abfd, 13018 1.10 christos char *buf, 13019 1.10 christos int *bufsiz, 13020 1.10 christos const void *aarch_pauth, 13021 1.10 christos int size) 13022 1.10 christos { 13023 1.10 christos char *note_name = "LINUX"; 13024 1.12 christos return elfcore_write_note (abfd, buf, bufsiz, 13025 1.12 christos note_name, NT_ARM_PAC_MASK, aarch_pauth, size); 13026 1.12 christos } 13027 1.12 christos 13028 1.12 christos char * 13029 1.12 christos elfcore_write_aarch_mte (bfd *abfd, 13030 1.12 christos char *buf, 13031 1.12 christos int *bufsiz, 13032 1.12 christos const void *aarch_mte, 13033 1.12 christos int size) 13034 1.12 christos { 13035 1.12 christos char *note_name = "LINUX"; 13036 1.12 christos return elfcore_write_note (abfd, buf, bufsiz, 13037 1.12 christos note_name, NT_ARM_TAGGED_ADDR_CTRL, 13038 1.13 christos aarch_mte, 13039 1.13 christos size); 13040 1.13 christos } 13041 1.13 christos 13042 1.13 christos char * 13043 1.13 christos elfcore_write_aarch_ssve (bfd *abfd, 13044 1.13 christos char *buf, 13045 1.13 christos int *bufsiz, 13046 1.13 christos const void *aarch_ssve, 13047 1.13 christos int size) 13048 1.13 christos { 13049 1.13 christos char *note_name = "LINUX"; 13050 1.13 christos return elfcore_write_note (abfd, buf, bufsiz, 13051 1.13 christos note_name, NT_ARM_SSVE, 13052 1.13 christos aarch_ssve, 13053 1.13 christos size); 13054 1.13 christos } 13055 1.13 christos 13056 1.13 christos char * 13057 1.13 christos elfcore_write_aarch_za (bfd *abfd, 13058 1.13 christos char *buf, 13059 1.13 christos int *bufsiz, 13060 1.13 christos const void *aarch_za, 13061 1.13 christos int size) 13062 1.13 christos { 13063 1.13 christos char *note_name = "LINUX"; 13064 1.13 christos return elfcore_write_note (abfd, buf, bufsiz, 13065 1.13 christos note_name, NT_ARM_ZA, 13066 1.13 christos aarch_za, 13067 1.13 christos size); 13068 1.13 christos } 13069 1.13 christos 13070 1.13 christos /* Write the buffer of zt register values in aarch_zt (length SIZE) into 13071 1.13 christos the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being 13072 1.13 christos written into. Return a pointer to the new start of the note buffer, to 13073 1.13 christos replace BUF which may no longer be valid. */ 13074 1.13 christos 13075 1.13 christos char * 13076 1.13 christos elfcore_write_aarch_zt (bfd *abfd, 13077 1.13 christos char *buf, 13078 1.13 christos int *bufsiz, 13079 1.13 christos const void *aarch_zt, 13080 1.13 christos int size) 13081 1.13 christos { 13082 1.13 christos char *note_name = "LINUX"; 13083 1.13 christos return elfcore_write_note (abfd, buf, bufsiz, 13084 1.14 christos note_name, NT_ARM_ZT, 13085 1.14 christos aarch_zt, 13086 1.14 christos size); 13087 1.14 christos } 13088 1.14 christos 13089 1.14 christos /* Write the buffer of GCS register values in AARCH_GCS (length SIZE) into 13090 1.14 christos the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being 13091 1.14 christos written into. Return a pointer to the new start of the note buffer, to 13092 1.14 christos replace BUF which may no longer be valid. */ 13093 1.14 christos 13094 1.14 christos static char * 13095 1.14 christos elfcore_write_aarch_gcs (bfd *abfd, char *buf, int *bufsiz, 13096 1.14 christos const void *aarch_gcs, int size) 13097 1.14 christos { 13098 1.13 christos const char *note_name = "LINUX"; 13099 1.12 christos return elfcore_write_note (abfd, buf, bufsiz, note_name, NT_ARM_GCS, 13100 1.12 christos aarch_gcs, size); 13101 1.12 christos } 13102 1.12 christos 13103 1.12 christos char * 13104 1.12 christos elfcore_write_arc_v2 (bfd *abfd, 13105 1.12 christos char *buf, 13106 1.12 christos int *bufsiz, 13107 1.12 christos const void *arc_v2, 13108 1.12 christos int size) 13109 1.12 christos { 13110 1.12 christos char *note_name = "LINUX"; 13111 1.12 christos return elfcore_write_note (abfd, buf, bufsiz, 13112 1.12 christos note_name, NT_ARC_V2, arc_v2, size); 13113 1.12 christos } 13114 1.12 christos 13115 1.12 christos char * 13116 1.12 christos elfcore_write_loongarch_cpucfg (bfd *abfd, 13117 1.12 christos char *buf, 13118 1.12 christos int *bufsiz, 13119 1.12 christos const void *loongarch_cpucfg, 13120 1.12 christos int size) 13121 1.12 christos { 13122 1.12 christos char *note_name = "LINUX"; 13123 1.12 christos return elfcore_write_note (abfd, buf, bufsiz, 13124 1.12 christos note_name, NT_LARCH_CPUCFG, 13125 1.12 christos loongarch_cpucfg, size); 13126 1.12 christos } 13127 1.12 christos 13128 1.12 christos char * 13129 1.12 christos elfcore_write_loongarch_lbt (bfd *abfd, 13130 1.12 christos char *buf, 13131 1.12 christos int *bufsiz, 13132 1.12 christos const void *loongarch_lbt, 13133 1.12 christos int size) 13134 1.12 christos { 13135 1.12 christos char *note_name = "LINUX"; 13136 1.12 christos return elfcore_write_note (abfd, buf, bufsiz, 13137 1.12 christos note_name, NT_LARCH_LBT, loongarch_lbt, size); 13138 1.12 christos } 13139 1.12 christos 13140 1.12 christos char * 13141 1.12 christos elfcore_write_loongarch_lsx (bfd *abfd, 13142 1.12 christos char *buf, 13143 1.12 christos int *bufsiz, 13144 1.12 christos const void *loongarch_lsx, 13145 1.12 christos int size) 13146 1.12 christos { 13147 1.12 christos char *note_name = "LINUX"; 13148 1.12 christos return elfcore_write_note (abfd, buf, bufsiz, 13149 1.12 christos note_name, NT_LARCH_LSX, loongarch_lsx, size); 13150 1.12 christos } 13151 1.12 christos 13152 1.12 christos char * 13153 1.12 christos elfcore_write_loongarch_lasx (bfd *abfd, 13154 1.12 christos char *buf, 13155 1.12 christos int *bufsiz, 13156 1.12 christos const void *loongarch_lasx, 13157 1.12 christos int size) 13158 1.12 christos { 13159 1.12 christos char *note_name = "LINUX"; 13160 1.12 christos return elfcore_write_note (abfd, buf, bufsiz, 13161 1.12 christos note_name, NT_LARCH_LASX, loongarch_lasx, size); 13162 1.12 christos } 13163 1.12 christos 13164 1.12 christos /* Write the buffer of csr values in CSRS (length SIZE) into the note 13165 1.12 christos buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being 13166 1.13 christos written into. Return a pointer to the new start of the note buffer, to 13167 1.13 christos replace BUF which may no longer be valid. */ 13168 1.13 christos 13169 1.13 christos char * 13170 1.12 christos elfcore_write_riscv_csr (bfd *abfd, 13171 1.12 christos char *buf, 13172 1.12 christos int *bufsiz, 13173 1.12 christos const void *csrs, 13174 1.12 christos int size) 13175 1.12 christos { 13176 1.12 christos const char *note_name = "GDB"; 13177 1.12 christos return elfcore_write_note (abfd, buf, bufsiz, 13178 1.12 christos note_name, NT_RISCV_CSR, csrs, size); 13179 1.12 christos } 13180 1.12 christos 13181 1.12 christos /* Write the target description (a string) pointed to by TDESC, length 13182 1.12 christos SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the 13183 1.12 christos note is being written into. Return a pointer to the new start of the 13184 1.12 christos note buffer, to replace BUF which may no longer be valid. */ 13185 1.12 christos 13186 1.12 christos char * 13187 1.12 christos elfcore_write_gdb_tdesc (bfd *abfd, 13188 1.12 christos char *buf, 13189 1.12 christos int *bufsiz, 13190 1.13 christos const void *tdesc, 13191 1.12 christos int size) 13192 1.12 christos { 13193 1.12 christos const char *note_name = "GDB"; 13194 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 13195 1.1 christos note_name, NT_GDB_TDESC, tdesc, size); 13196 1.1 christos } 13197 1.1 christos 13198 1.1 christos char * 13199 1.1 christos elfcore_write_register_note (bfd *abfd, 13200 1.1 christos char *buf, 13201 1.1 christos int *bufsiz, 13202 1.1 christos const char *section, 13203 1.1 christos const void *data, 13204 1.1 christos int size) 13205 1.1 christos { 13206 1.1 christos if (strcmp (section, ".reg2") == 0) 13207 1.12 christos return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); 13208 1.12 christos if (strcmp (section, ".reg-xfp") == 0) 13209 1.14 christos return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); 13210 1.14 christos if (strcmp (section, ".reg-xstate") == 0) 13211 1.1 christos return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); 13212 1.1 christos if (strcmp (section, ".reg-x86-segbases") == 0) 13213 1.1 christos return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size); 13214 1.1 christos if (strcmp (section, ".reg-ssp") == 0) 13215 1.10 christos return elfcore_write_sspreg (abfd, buf, bufsiz, data, size); 13216 1.10 christos if (strcmp (section, ".reg-ppc-vmx") == 0) 13217 1.10 christos return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); 13218 1.10 christos if (strcmp (section, ".reg-ppc-vsx") == 0) 13219 1.10 christos return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); 13220 1.10 christos if (strcmp (section, ".reg-ppc-tar") == 0) 13221 1.10 christos return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size); 13222 1.10 christos if (strcmp (section, ".reg-ppc-ppr") == 0) 13223 1.10 christos return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size); 13224 1.10 christos if (strcmp (section, ".reg-ppc-dscr") == 0) 13225 1.10 christos return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size); 13226 1.10 christos if (strcmp (section, ".reg-ppc-ebb") == 0) 13227 1.10 christos return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size); 13228 1.10 christos if (strcmp (section, ".reg-ppc-pmu") == 0) 13229 1.10 christos return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size); 13230 1.10 christos if (strcmp (section, ".reg-ppc-tm-cgpr") == 0) 13231 1.10 christos return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size); 13232 1.10 christos if (strcmp (section, ".reg-ppc-tm-cfpr") == 0) 13233 1.10 christos return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size); 13234 1.10 christos if (strcmp (section, ".reg-ppc-tm-cvmx") == 0) 13235 1.10 christos return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size); 13236 1.10 christos if (strcmp (section, ".reg-ppc-tm-cvsx") == 0) 13237 1.10 christos return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size); 13238 1.10 christos if (strcmp (section, ".reg-ppc-tm-spr") == 0) 13239 1.10 christos return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size); 13240 1.10 christos if (strcmp (section, ".reg-ppc-tm-ctar") == 0) 13241 1.1 christos return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size); 13242 1.1 christos if (strcmp (section, ".reg-ppc-tm-cppr") == 0) 13243 1.1 christos return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size); 13244 1.1 christos if (strcmp (section, ".reg-ppc-tm-cdscr") == 0) 13245 1.1 christos return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size); 13246 1.1 christos if (strcmp (section, ".reg-s390-high-gprs") == 0) 13247 1.1 christos return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); 13248 1.1 christos if (strcmp (section, ".reg-s390-timer") == 0) 13249 1.1 christos return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); 13250 1.1 christos if (strcmp (section, ".reg-s390-todcmp") == 0) 13251 1.1 christos return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); 13252 1.1 christos if (strcmp (section, ".reg-s390-todpreg") == 0) 13253 1.1 christos return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); 13254 1.1 christos if (strcmp (section, ".reg-s390-ctrs") == 0) 13255 1.1 christos return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); 13256 1.1 christos if (strcmp (section, ".reg-s390-prefix") == 0) 13257 1.3 christos return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); 13258 1.3 christos if (strcmp (section, ".reg-s390-last-break") == 0) 13259 1.3 christos return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); 13260 1.3 christos if (strcmp (section, ".reg-s390-system-call") == 0) 13261 1.3 christos return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); 13262 1.3 christos if (strcmp (section, ".reg-s390-tdb") == 0) 13263 1.9 christos return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); 13264 1.9 christos if (strcmp (section, ".reg-s390-vxrs-low") == 0) 13265 1.9 christos return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); 13266 1.9 christos if (strcmp (section, ".reg-s390-vxrs-high") == 0) 13267 1.1 christos return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); 13268 1.1 christos if (strcmp (section, ".reg-s390-gs-cb") == 0) 13269 1.3 christos return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size); 13270 1.3 christos if (strcmp (section, ".reg-s390-gs-bc") == 0) 13271 1.3 christos return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size); 13272 1.3 christos if (strcmp (section, ".reg-arm-vfp") == 0) 13273 1.3 christos return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); 13274 1.3 christos if (strcmp (section, ".reg-aarch-tls") == 0) 13275 1.10 christos return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); 13276 1.10 christos if (strcmp (section, ".reg-aarch-hw-break") == 0) 13277 1.10 christos return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); 13278 1.10 christos if (strcmp (section, ".reg-aarch-hw-watch") == 0) 13279 1.12 christos return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); 13280 1.12 christos if (strcmp (section, ".reg-aarch-sve") == 0) 13281 1.13 christos return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size); 13282 1.13 christos if (strcmp (section, ".reg-aarch-pauth") == 0) 13283 1.13 christos return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size); 13284 1.13 christos if (strcmp (section, ".reg-aarch-mte") == 0) 13285 1.13 christos return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size); 13286 1.13 christos if (strcmp (section, ".reg-aarch-ssve") == 0) 13287 1.14 christos return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size); 13288 1.14 christos if (strcmp (section, ".reg-aarch-za") == 0) 13289 1.12 christos return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size); 13290 1.12 christos if (strcmp (section, ".reg-aarch-zt") == 0) 13291 1.12 christos return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size); 13292 1.12 christos if (strcmp (section, ".reg-aarch-gcs") == 0) 13293 1.12 christos return elfcore_write_aarch_gcs (abfd, buf, bufsiz, data, size); 13294 1.12 christos if (strcmp (section, ".reg-arc-v2") == 0) 13295 1.12 christos return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size); 13296 1.12 christos if (strcmp (section, ".gdb-tdesc") == 0) 13297 1.12 christos return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size); 13298 1.12 christos if (strcmp (section, ".reg-riscv-csr") == 0) 13299 1.12 christos return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size); 13300 1.12 christos if (strcmp (section, ".reg-loongarch-cpucfg") == 0) 13301 1.12 christos return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size); 13302 1.12 christos if (strcmp (section, ".reg-loongarch-lbt") == 0) 13303 1.1 christos return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size); 13304 1.1 christos if (strcmp (section, ".reg-loongarch-lsx") == 0) 13305 1.1 christos return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size); 13306 1.12 christos if (strcmp (section, ".reg-loongarch-lasx") == 0) 13307 1.12 christos return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size); 13308 1.12 christos return NULL; 13309 1.12 christos } 13310 1.12 christos 13311 1.12 christos char * 13312 1.12 christos elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size, 13313 1.12 christos const void *buf, int bufsiz) 13314 1.12 christos { 13315 1.9 christos return elfcore_write_note (obfd, note_data, note_size, 13316 1.9 christos "CORE", NT_FILE, buf, bufsiz); 13317 1.1 christos } 13318 1.1 christos 13319 1.1 christos static bool 13320 1.9 christos elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset, 13321 1.9 christos size_t align) 13322 1.9 christos { 13323 1.9 christos char *p; 13324 1.9 christos 13325 1.9 christos /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1. 13326 1.9 christos gABI specifies that PT_NOTE alignment should be aligned to 4 13327 1.12 christos bytes for 32-bit objects and to 8 bytes for 64-bit objects. If 13328 1.9 christos align is less than 4, we use 4 byte alignment. */ 13329 1.1 christos if (align < 4) 13330 1.1 christos align = 4; 13331 1.1 christos if (align != 4 && align != 8) 13332 1.1 christos return false; 13333 1.1 christos 13334 1.1 christos p = buf; 13335 1.1 christos while (p < buf + size) 13336 1.12 christos { 13337 1.1 christos Elf_External_Note *xnp = (Elf_External_Note *) p; 13338 1.1 christos Elf_Internal_Note in; 13339 1.1 christos 13340 1.1 christos if (offsetof (Elf_External_Note, name) > buf - p + size) 13341 1.1 christos return false; 13342 1.1 christos 13343 1.12 christos in.type = H_GET_32 (abfd, xnp->type); 13344 1.1 christos 13345 1.1 christos in.namesz = H_GET_32 (abfd, xnp->namesz); 13346 1.9 christos in.namedata = xnp->name; 13347 1.1 christos if (in.namesz > buf - in.namedata + size) 13348 1.1 christos return false; 13349 1.1 christos 13350 1.1 christos in.descsz = H_GET_32 (abfd, xnp->descsz); 13351 1.12 christos in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align); 13352 1.1 christos in.descpos = offset + (in.descdata - buf); 13353 1.1 christos if (in.descsz != 0 13354 1.9 christos && (in.descdata >= buf + size 13355 1.1 christos || in.descsz > buf - in.descdata + size)) 13356 1.12 christos return false; 13357 1.1 christos 13358 1.1 christos switch (bfd_get_format (abfd)) 13359 1.3 christos { 13360 1.3 christos default: 13361 1.3 christos return true; 13362 1.1 christos 13363 1.3 christos case bfd_core: 13364 1.3 christos { 13365 1.12 christos #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} 13366 1.1 christos struct 13367 1.3 christos { 13368 1.1 christos const char * string; 13369 1.3 christos size_t len; 13370 1.5 christos bool (*func) (bfd *, Elf_Internal_Note *); 13371 1.3 christos } 13372 1.12 christos grokers[] = 13373 1.3 christos { 13374 1.10 christos GROKER_ELEMENT ("", elfcore_grok_note), 13375 1.12 christos GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note), 13376 1.12 christos GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), 13377 1.3 christos GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note), 13378 1.3 christos GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), 13379 1.3 christos GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note), 13380 1.3 christos GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note), 13381 1.3 christos GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note) 13382 1.3 christos }; 13383 1.3 christos #undef GROKER_ELEMENT 13384 1.3 christos int i; 13385 1.3 christos 13386 1.3 christos for (i = ARRAY_SIZE (grokers); i--;) 13387 1.3 christos { 13388 1.12 christos if (in.namesz >= grokers[i].len 13389 1.3 christos && strncmp (in.namedata, grokers[i].string, 13390 1.3 christos grokers[i].len) == 0) 13391 1.3 christos { 13392 1.3 christos if (! grokers[i].func (abfd, & in)) 13393 1.3 christos return false; 13394 1.1 christos break; 13395 1.1 christos } 13396 1.1 christos } 13397 1.1 christos break; 13398 1.1 christos } 13399 1.12 christos 13400 1.1 christos case bfd_object: 13401 1.1 christos if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) 13402 1.1 christos { 13403 1.1 christos if (! elfobj_grok_gnu_note (abfd, &in)) 13404 1.1 christos return false; 13405 1.12 christos } 13406 1.1 christos else if (in.namesz == sizeof "stapsdt" 13407 1.1 christos && strcmp (in.namedata, "stapsdt") == 0) 13408 1.1 christos { 13409 1.1 christos if (! elfobj_grok_stapsdt_note (abfd, &in)) 13410 1.9 christos return false; 13411 1.1 christos } 13412 1.1 christos break; 13413 1.12 christos } 13414 1.1 christos 13415 1.1 christos p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align); 13416 1.12 christos } 13417 1.9 christos 13418 1.9 christos return true; 13419 1.1 christos } 13420 1.1 christos 13421 1.1 christos bool 13422 1.9 christos elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size, 13423 1.12 christos size_t align) 13424 1.1 christos { 13425 1.1 christos char *buf; 13426 1.12 christos 13427 1.1 christos if (size == 0 || (size + 1) == 0) 13428 1.12 christos return true; 13429 1.1 christos 13430 1.12 christos if (bfd_seek (abfd, offset, SEEK_SET) != 0) 13431 1.1 christos return false; 13432 1.3 christos 13433 1.3 christos buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size); 13434 1.3 christos if (buf == NULL) 13435 1.3 christos return false; 13436 1.12 christos 13437 1.1 christos /* PR 17512: file: ec08f814 13438 1.1 christos 0-termintate the buffer so that string searches will not overflow. */ 13439 1.12 christos buf[size] = 0; 13440 1.1 christos 13441 1.1 christos if (!elf_parse_notes (abfd, buf, size, offset, align)) 13442 1.1 christos { 13443 1.12 christos free (buf); 13444 1.1 christos return false; 13445 1.1 christos } 13446 1.1 christos 13447 1.1 christos free (buf); 13448 1.1 christos return true; 13449 1.1 christos } 13450 1.1 christos 13451 1.1 christos /* Providing external access to the ELF program header table. */ 13453 1.1 christos 13454 1.1 christos /* Return an upper bound on the number of bytes required to store a 13455 1.1 christos copy of ABFD's program header table entries. Return -1 if an error 13456 1.1 christos occurs; bfd_get_error will return an appropriate code. */ 13457 1.1 christos 13458 1.1 christos long 13459 1.1 christos bfd_get_elf_phdr_upper_bound (bfd *abfd) 13460 1.1 christos { 13461 1.1 christos if (abfd->xvec->flavour != bfd_target_elf_flavour) 13462 1.1 christos { 13463 1.1 christos bfd_set_error (bfd_error_wrong_format); 13464 1.1 christos return -1; 13465 1.1 christos } 13466 1.1 christos 13467 1.1 christos return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); 13468 1.1 christos } 13469 1.1 christos 13470 1.1 christos /* Copy ABFD's program header table entries to *PHDRS. The entries 13471 1.1 christos will be stored as an array of Elf_Internal_Phdr structures, as 13472 1.1 christos defined in include/elf/internal.h. To find out how large the 13473 1.1 christos buffer needs to be, call bfd_get_elf_phdr_upper_bound. 13474 1.1 christos 13475 1.1 christos Return the number of program header table entries read, or -1 if an 13476 1.1 christos error occurs; bfd_get_error will return an appropriate code. */ 13477 1.1 christos 13478 1.1 christos int 13479 1.1 christos bfd_get_elf_phdrs (bfd *abfd, void *phdrs) 13480 1.1 christos { 13481 1.1 christos int num_phdrs; 13482 1.1 christos 13483 1.1 christos if (abfd->xvec->flavour != bfd_target_elf_flavour) 13484 1.10 christos { 13485 1.10 christos bfd_set_error (bfd_error_wrong_format); 13486 1.10 christos return -1; 13487 1.1 christos } 13488 1.1 christos 13489 1.1 christos num_phdrs = elf_elfheader (abfd)->e_phnum; 13490 1.1 christos if (num_phdrs != 0) 13491 1.1 christos memcpy (phdrs, elf_tdata (abfd)->phdr, 13492 1.3 christos num_phdrs * sizeof (Elf_Internal_Phdr)); 13493 1.3 christos 13494 1.3 christos return num_phdrs; 13495 1.1 christos } 13496 1.1 christos 13497 1.1 christos enum elf_reloc_type_class 13498 1.1 christos _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, 13499 1.1 christos const asection *rel_sec ATTRIBUTE_UNUSED, 13500 1.1 christos const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) 13501 1.1 christos { 13502 1.1 christos return reloc_class_normal; 13503 1.1 christos } 13504 1.1 christos 13505 1.1 christos /* For RELA architectures, return the relocation value for a 13506 1.1 christos relocation against a local symbol. */ 13507 1.1 christos 13508 1.1 christos bfd_vma 13509 1.1 christos _bfd_elf_rela_local_sym (bfd *abfd, 13510 1.1 christos Elf_Internal_Sym *sym, 13511 1.1 christos asection **psec, 13512 1.1 christos Elf_Internal_Rela *rel) 13513 1.1 christos { 13514 1.1 christos asection *sec = *psec; 13515 1.1 christos bfd_vma relocation; 13516 1.1 christos 13517 1.1 christos relocation = (sec->output_section->vma 13518 1.1 christos + sec->output_offset 13519 1.1 christos + sym->st_value); 13520 1.1 christos if ((sec->flags & SEC_MERGE) 13521 1.1 christos && ELF_ST_TYPE (sym->st_info) == STT_SECTION 13522 1.1 christos && sec->sec_info_type == SEC_INFO_TYPE_MERGE) 13523 1.1 christos { 13524 1.1 christos rel->r_addend = 13525 1.1 christos _bfd_merged_section_offset (abfd, psec, 13526 1.1 christos elf_section_data (sec)->sec_info, 13527 1.1 christos sym->st_value + rel->r_addend); 13528 1.1 christos if (sec != *psec) 13529 1.1 christos { 13530 1.1 christos /* If we have changed the section, and our original section is 13531 1.1 christos marked with SEC_EXCLUDE, it means that the original 13532 1.1 christos SEC_MERGE section has been completely subsumed in some 13533 1.1 christos other SEC_MERGE section. In this case, we need to leave 13534 1.1 christos some info around for --emit-relocs. */ 13535 1.1 christos if ((sec->flags & SEC_EXCLUDE) != 0) 13536 1.1 christos sec->kept_section = *psec; 13537 1.1 christos sec = *psec; 13538 1.1 christos } 13539 1.1 christos rel->r_addend -= relocation; 13540 1.1 christos rel->r_addend += sec->output_section->vma + sec->output_offset; 13541 1.1 christos } 13542 1.1 christos return relocation; 13543 1.1 christos } 13544 1.1 christos 13545 1.1 christos bfd_vma 13546 1.1 christos _bfd_elf_rel_local_sym (bfd *abfd, 13547 1.1 christos Elf_Internal_Sym *sym, 13548 1.1 christos asection **psec, 13549 1.1 christos bfd_vma addend) 13550 1.1 christos { 13551 1.1 christos asection *sec = *psec; 13552 1.1 christos 13553 1.1 christos if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) 13554 1.1 christos return sym->st_value + addend; 13555 1.5 christos 13556 1.5 christos return _bfd_merged_section_offset (abfd, psec, 13557 1.5 christos elf_section_data (sec)->sec_info, 13558 1.5 christos sym->st_value + addend); 13559 1.5 christos } 13560 1.5 christos 13561 1.1 christos /* Adjust an address within a section. Given OFFSET within SEC, return 13562 1.1 christos the new offset within the section, based upon changes made to the 13563 1.1 christos section. Returns -1 if the offset is now invalid. 13564 1.1 christos The offset (in abnd out) is in target sized bytes, however big a 13565 1.1 christos byte may be. */ 13566 1.1 christos 13567 1.1 christos bfd_vma 13568 1.1 christos _bfd_elf_section_offset (bfd *abfd, 13569 1.1 christos struct bfd_link_info *info, 13570 1.1 christos asection *sec, 13571 1.1 christos bfd_vma offset) 13572 1.1 christos { 13573 1.1 christos switch (sec->sec_info_type) 13574 1.5 christos { 13575 1.14 christos case SEC_INFO_TYPE_STABS: 13576 1.14 christos return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, 13577 1.14 christos offset); 13578 1.1 christos case SEC_INFO_TYPE_EH_FRAME: 13579 1.1 christos return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); 13580 1.1 christos 13581 1.5 christos case SEC_INFO_TYPE_SFRAME: 13582 1.1 christos return _bfd_elf_sframe_section_offset (abfd, info, sec, offset); 13583 1.1 christos 13584 1.5 christos default: 13585 1.5 christos if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) 13586 1.5 christos { 13587 1.10 christos /* Reverse the offset. */ 13588 1.10 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13589 1.1 christos bfd_size_type address_size = bed->s->arch_size / 8; 13590 1.1 christos 13591 1.1 christos /* address_size and sec->size are in octets. Convert 13592 1.1 christos to bytes before subtracting the original offset. */ 13593 1.1 christos offset = ((sec->size - address_size) 13594 1.1 christos / bfd_octets_per_byte (abfd, sec) - offset); 13595 1.1 christos } 13596 1.1 christos return offset; 13597 1.1 christos } 13598 1.1 christos } 13599 1.1 christos 13600 1.1 christos long 13602 1.1 christos _bfd_elf_get_synthetic_symtab (bfd *abfd, 13603 1.1 christos long symcount ATTRIBUTE_UNUSED, 13604 1.1 christos asymbol **syms ATTRIBUTE_UNUSED, 13605 1.1 christos long dynsymcount, 13606 1.12 christos asymbol **dynsyms, 13607 1.1 christos asymbol **ret) 13608 1.1 christos { 13609 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13610 1.1 christos asection *relplt; 13611 1.1 christos asymbol *s; 13612 1.1 christos const char *relplt_name; 13613 1.1 christos bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool); 13614 1.1 christos arelent *p; 13615 1.1 christos long count, i, n; 13616 1.1 christos size_t size; 13617 1.1 christos Elf_Internal_Shdr *hdr; 13618 1.1 christos char *names; 13619 1.1 christos asection *plt; 13620 1.1 christos 13621 1.1 christos *ret = NULL; 13622 1.1 christos 13623 1.1 christos if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) 13624 1.1 christos return 0; 13625 1.1 christos 13626 1.1 christos if (dynsymcount <= 0) 13627 1.1 christos return 0; 13628 1.1 christos 13629 1.1 christos if (!bed->plt_sym_val) 13630 1.1 christos return 0; 13631 1.1 christos 13632 1.1 christos relplt_name = bed->relplt_name; 13633 1.1 christos if (relplt_name == NULL) 13634 1.1 christos relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; 13635 1.1 christos relplt = bfd_get_section_by_name (abfd, relplt_name); 13636 1.1 christos if (relplt == NULL) 13637 1.1 christos return 0; 13638 1.1 christos 13639 1.1 christos hdr = &elf_section_data (relplt)->this_hdr; 13640 1.1 christos if (hdr->sh_link != elf_dynsymtab (abfd) 13641 1.1 christos || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) 13642 1.12 christos return 0; 13643 1.1 christos 13644 1.1 christos plt = bfd_get_section_by_name (abfd, ".plt"); 13645 1.13 christos if (plt == NULL) 13646 1.1 christos return 0; 13647 1.1 christos 13648 1.1 christos slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; 13649 1.1 christos if (! (*slurp_relocs) (abfd, relplt, dynsyms, true)) 13650 1.1 christos return -1; 13651 1.1 christos 13652 1.1 christos count = NUM_SHDR_ENTRIES (hdr); 13653 1.1 christos size = count * sizeof (asymbol); 13654 1.1 christos p = relplt->relocation; 13655 1.1 christos for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) 13656 1.1 christos { 13657 1.1 christos size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); 13658 1.1 christos if (p->addend != 0) 13659 1.1 christos { 13660 1.1 christos #ifdef BFD64 13661 1.1 christos size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); 13662 1.1 christos #else 13663 1.1 christos size += sizeof ("+0x") - 1 + 8; 13664 1.1 christos #endif 13665 1.1 christos } 13666 1.1 christos } 13667 1.1 christos 13668 1.1 christos s = *ret = (asymbol *) bfd_malloc (size); 13669 1.1 christos if (s == NULL) 13670 1.1 christos return -1; 13671 1.1 christos 13672 1.1 christos names = (char *) (s + count); 13673 1.1 christos p = relplt->relocation; 13674 1.1 christos n = 0; 13675 1.1 christos for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) 13676 1.1 christos { 13677 1.1 christos size_t len; 13678 1.1 christos bfd_vma addr; 13679 1.1 christos 13680 1.1 christos addr = bed->plt_sym_val (i, plt, p); 13681 1.1 christos if (addr == (bfd_vma) -1) 13682 1.1 christos continue; 13683 1.1 christos 13684 1.1 christos *s = **p->sym_ptr_ptr; 13685 1.1 christos /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since 13686 1.1 christos we are defining a symbol, ensure one of them is set. */ 13687 1.1 christos if ((s->flags & BSF_LOCAL) == 0) 13688 1.1 christos s->flags |= BSF_GLOBAL; 13689 1.1 christos s->flags |= BSF_SYNTHETIC; 13690 1.1 christos s->section = plt; 13691 1.1 christos s->value = addr - plt->vma; 13692 1.1 christos s->name = names; 13693 1.1 christos s->udata.p = NULL; 13694 1.1 christos len = strlen ((*p->sym_ptr_ptr)->name); 13695 1.1 christos memcpy (names, (*p->sym_ptr_ptr)->name, len); 13696 1.1 christos names += len; 13697 1.1 christos if (p->addend != 0) 13698 1.1 christos { 13699 1.1 christos char buf[30], *a; 13700 1.1 christos 13701 1.1 christos memcpy (names, "+0x", sizeof ("+0x") - 1); 13702 1.1 christos names += sizeof ("+0x") - 1; 13703 1.1 christos bfd_sprintf_vma (abfd, buf, p->addend); 13704 1.1 christos for (a = buf; *a == '0'; ++a) 13705 1.1 christos ; 13706 1.1 christos len = strlen (a); 13707 1.1 christos memcpy (names, a, len); 13708 1.1 christos names += len; 13709 1.1 christos } 13710 1.1 christos memcpy (names, "@plt", sizeof ("@plt")); 13711 1.9 christos names += sizeof ("@plt"); 13712 1.9 christos ++s, ++n; 13713 1.9 christos } 13714 1.9 christos 13715 1.9 christos return n; 13716 1.1 christos } 13717 1.9 christos 13718 1.9 christos /* It is only used by x86-64 so far. 13719 1.1 christos ??? This repeats *COM* id of zero. sec->id is supposed to be unique, 13720 1.12 christos but current usage would allow all of _bfd_std_section to be zero. */ 13721 1.10 christos static const asymbol lcomm_sym 13722 1.1 christos = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section); 13723 1.10 christos asection _bfd_elf_large_com_section 13724 1.1 christos = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym, 13725 1.1 christos "LARGE_COMMON", 0, SEC_IS_COMMON); 13726 1.1 christos 13727 1.10 christos bool 13728 1.10 christos _bfd_elf_final_write_processing (bfd *abfd) 13729 1.1 christos { 13730 1.10 christos Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */ 13731 1.12 christos 13732 1.12 christos i_ehdrp = elf_elfheader (abfd); 13733 1.10 christos 13734 1.10 christos if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) 13735 1.10 christos i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; 13736 1.10 christos 13737 1.10 christos /* Set the osabi field to ELFOSABI_GNU if the binary contains 13738 1.10 christos SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type 13739 1.10 christos or STB_GNU_UNIQUE binding. */ 13740 1.10 christos if (elf_tdata (abfd)->has_gnu_osabi != 0) 13741 1.12 christos { 13742 1.12 christos if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) 13743 1.10 christos i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; 13744 1.12 christos else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU 13745 1.12 christos && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD) 13746 1.10 christos { 13747 1.12 christos if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) 13748 1.12 christos _bfd_error_handler (_("GNU_MBIND section is supported only by GNU " 13749 1.12 christos "and FreeBSD targets")); 13750 1.12 christos if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc) 13751 1.12 christos _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported " 13752 1.10 christos "only by GNU and FreeBSD targets")); 13753 1.12 christos if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique) 13754 1.10 christos _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported " 13755 1.10 christos "only by GNU and FreeBSD targets")); 13756 1.12 christos if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain) 13757 1.1 christos _bfd_error_handler (_("GNU_RETAIN section is supported " 13758 1.1 christos "only by GNU and FreeBSD targets")); 13759 1.1 christos bfd_set_error (bfd_error_sorry); 13760 1.1 christos return false; 13761 1.1 christos } 13762 1.1 christos } 13763 1.1 christos return true; 13764 1.12 christos } 13765 1.1 christos 13766 1.1 christos 13767 1.1 christos /* Return TRUE for ELF symbol types that represent functions. 13768 1.1 christos This is the default version of this function, which is sufficient for 13769 1.1 christos most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ 13770 1.1 christos 13771 1.1 christos bool 13772 1.1 christos _bfd_elf_is_function_type (unsigned int type) 13773 1.1 christos { 13774 1.1 christos return (type == STT_FUNC 13775 1.1 christos || type == STT_GNU_IFUNC); 13776 1.1 christos } 13777 1.1 christos 13778 1.1 christos /* If the ELF symbol SYM might be a function in SEC, return the 13779 1.1 christos function size and set *CODE_OFF to the function's entry point, 13780 1.12 christos otherwise return zero. */ 13781 1.1 christos 13782 1.1 christos bfd_size_type 13783 1.1 christos _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, 13784 1.1 christos bfd_vma *code_off) 13785 1.1 christos { 13786 1.1 christos bfd_size_type size; 13787 1.12 christos elf_symbol_type * elf_sym = (elf_symbol_type *) sym; 13788 1.13 christos 13789 1.12 christos if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT 13790 1.12 christos | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 13791 1.12 christos || sym->section != sec) 13792 1.12 christos return 0; 13793 1.12 christos 13794 1.12 christos size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size; 13795 1.12 christos 13796 1.12 christos /* In theory we should check that the symbol's type satisfies 13797 1.12 christos _bfd_elf_is_function_type(), but there are some function-like 13798 1.12 christos symbols which would fail this test. (eg _start). Instead 13799 1.12 christos we check for hidden, local, notype symbols with zero size. 13800 1.13 christos This type of symbol is generated by the annobin plugin for gcc 13801 1.1 christos and clang, and should not be considered to be a function symbol. */ 13802 1.12 christos if (size == 0 13803 1.12 christos && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL) 13804 1.12 christos && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE 13805 1.12 christos && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN) 13806 1.12 christos return 0; 13807 1.12 christos 13808 1.12 christos *code_off = sym->value; 13809 1.12 christos /* Do not return 0 for the function's size. */ 13810 1.12 christos return size ? size : 1; 13811 1.12 christos } 13812 1.12 christos 13813 1.12 christos /* Set to non-zero to enable some debug messages. */ 13814 1.12 christos #define DEBUG_SECONDARY_RELOCS 0 13815 1.12 christos 13816 1.12 christos /* An internal-to-the-bfd-library only section type 13817 1.12 christos used to indicate a cached secondary reloc section. */ 13818 1.12 christos #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA) 13819 1.12 christos 13820 1.12 christos /* Create a BFD section to hold a secondary reloc section. */ 13821 1.12 christos 13822 1.12 christos bool 13823 1.12 christos _bfd_elf_init_secondary_reloc_section (bfd * abfd, 13824 1.12 christos Elf_Internal_Shdr *hdr, 13825 1.12 christos const char * name, 13826 1.12 christos unsigned int shindex) 13827 1.12 christos { 13828 1.12 christos /* We only support RELA secondary relocs. */ 13829 1.12 christos if (hdr->sh_type != SHT_RELA) 13830 1.12 christos return false; 13831 1.12 christos 13832 1.12 christos #if DEBUG_SECONDARY_RELOCS 13833 1.12 christos fprintf (stderr, "secondary reloc section %s encountered\n", name); 13834 1.12 christos #endif 13835 1.12 christos hdr->sh_type = SHT_SECONDARY_RELOC; 13836 1.12 christos return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 13837 1.12 christos } 13838 1.12 christos 13839 1.12 christos /* Read in any secondary relocs associated with SEC. */ 13840 1.12 christos 13841 1.12 christos bool 13842 1.12 christos _bfd_elf_slurp_secondary_reloc_section (bfd * abfd, 13843 1.12 christos asection * sec, 13844 1.13 christos asymbol ** symbols, 13845 1.12 christos bool dynamic) 13846 1.12 christos { 13847 1.12 christos const struct elf_backend_data * const ebd = get_elf_backend_data (abfd); 13848 1.12 christos asection * relsec; 13849 1.12 christos bool result = true; 13850 1.12 christos bfd_vma (*r_sym) (bfd_vma); 13851 1.12 christos ufile_ptr filesize; 13852 1.13 christos 13853 1.12 christos #if BFD_DEFAULT_TARGET_SIZE > 32 13854 1.12 christos if (bfd_arch_bits_per_address (abfd) != 32) 13855 1.12 christos r_sym = elf64_r_sym; 13856 1.12 christos else 13857 1.12 christos #endif 13858 1.13 christos r_sym = elf32_r_sym; 13859 1.12 christos 13860 1.12 christos if (!elf_section_data (sec)->has_secondary_relocs) 13861 1.12 christos return true; 13862 1.12 christos 13863 1.12 christos /* Discover if there are any secondary reloc sections 13864 1.12 christos associated with SEC. */ 13865 1.12 christos filesize = bfd_get_file_size (abfd); 13866 1.12 christos for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next) 13867 1.12 christos { 13868 1.12 christos Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr; 13869 1.12 christos 13870 1.12 christos if (hdr->sh_type == SHT_SECONDARY_RELOC 13871 1.12 christos && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx 13872 1.13 christos && (hdr->sh_entsize == ebd->s->sizeof_rel 13873 1.12 christos || hdr->sh_entsize == ebd->s->sizeof_rela)) 13874 1.12 christos { 13875 1.13 christos bfd_byte * native_relocs; 13876 1.12 christos bfd_byte * native_reloc; 13877 1.12 christos arelent * internal_relocs; 13878 1.12 christos arelent * internal_reloc; 13879 1.12 christos size_t i; 13880 1.12 christos unsigned int entsize; 13881 1.12 christos unsigned int symcount; 13882 1.12 christos bfd_size_type reloc_count; 13883 1.12 christos size_t amt; 13884 1.12 christos 13885 1.12 christos if (ebd->elf_info_to_howto == NULL) 13886 1.12 christos return false; 13887 1.13 christos 13888 1.13 christos #if DEBUG_SECONDARY_RELOCS 13889 1.13 christos fprintf (stderr, "read secondary relocs for %s from %s\n", 13890 1.13 christos sec->name, relsec->name); 13891 1.13 christos #endif 13892 1.13 christos entsize = hdr->sh_entsize; 13893 1.13 christos 13894 1.13 christos if (filesize != 0 13895 1.13 christos && ((ufile_ptr) hdr->sh_offset > filesize 13896 1.12 christos || hdr->sh_size > filesize - hdr->sh_offset)) 13897 1.12 christos { 13898 1.12 christos bfd_set_error (bfd_error_file_truncated); 13899 1.12 christos result = false; 13900 1.12 christos continue; 13901 1.12 christos } 13902 1.12 christos 13903 1.12 christos native_relocs = bfd_malloc (hdr->sh_size); 13904 1.12 christos if (native_relocs == NULL) 13905 1.12 christos { 13906 1.12 christos result = false; 13907 1.12 christos continue; 13908 1.12 christos } 13909 1.12 christos 13910 1.12 christos reloc_count = NUM_SHDR_ENTRIES (hdr); 13911 1.12 christos if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt)) 13912 1.12 christos { 13913 1.12 christos free (native_relocs); 13914 1.12 christos bfd_set_error (bfd_error_file_too_big); 13915 1.12 christos result = false; 13916 1.12 christos continue; 13917 1.12 christos } 13918 1.12 christos 13919 1.12 christos internal_relocs = (arelent *) bfd_alloc (abfd, amt); 13920 1.12 christos if (internal_relocs == NULL) 13921 1.13 christos { 13922 1.12 christos free (native_relocs); 13923 1.12 christos result = false; 13924 1.12 christos continue; 13925 1.12 christos } 13926 1.12 christos 13927 1.12 christos if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 13928 1.12 christos || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size) 13929 1.12 christos { 13930 1.12 christos free (native_relocs); 13931 1.12 christos /* The internal_relocs will be freed when 13932 1.12 christos the memory for the bfd is released. */ 13933 1.12 christos result = false; 13934 1.12 christos continue; 13935 1.12 christos } 13936 1.12 christos 13937 1.12 christos if (dynamic) 13938 1.12 christos symcount = bfd_get_dynamic_symcount (abfd); 13939 1.12 christos else 13940 1.12 christos symcount = bfd_get_symcount (abfd); 13941 1.12 christos 13942 1.12 christos for (i = 0, internal_reloc = internal_relocs, 13943 1.12 christos native_reloc = native_relocs; 13944 1.12 christos i < reloc_count; 13945 1.12 christos i++, internal_reloc++, native_reloc += entsize) 13946 1.12 christos { 13947 1.12 christos bool res; 13948 1.12 christos Elf_Internal_Rela rela; 13949 1.12 christos 13950 1.12 christos if (entsize == ebd->s->sizeof_rel) 13951 1.12 christos ebd->s->swap_reloc_in (abfd, native_reloc, & rela); 13952 1.12 christos else /* entsize == ebd->s->sizeof_rela */ 13953 1.12 christos ebd->s->swap_reloca_in (abfd, native_reloc, & rela); 13954 1.12 christos 13955 1.12 christos /* The address of an ELF reloc is section relative for an object 13956 1.12 christos file, and absolute for an executable file or shared library. 13957 1.12 christos The address of a normal BFD reloc is always section relative, 13958 1.12 christos and the address of a dynamic reloc is absolute.. */ 13959 1.12 christos if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) 13960 1.12 christos internal_reloc->address = rela.r_offset; 13961 1.14 christos else 13962 1.12 christos internal_reloc->address = rela.r_offset - sec->vma; 13963 1.12 christos 13964 1.12 christos if (r_sym (rela.r_info) == STN_UNDEF) 13965 1.12 christos { 13966 1.12 christos /* FIXME: This and the error case below mean that we 13967 1.13 christos have a symbol on relocs that is not elf_symbol_type. */ 13968 1.12 christos internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol; 13969 1.12 christos } 13970 1.14 christos else if (r_sym (rela.r_info) > symcount) 13971 1.12 christos { 13972 1.12 christos _bfd_error_handler 13973 1.12 christos /* xgettext:c-format */ 13974 1.12 christos (_("%pB(%pA): relocation %zu has invalid symbol index %lu"), 13975 1.12 christos abfd, sec, i, (long) r_sym (rela.r_info)); 13976 1.12 christos bfd_set_error (bfd_error_bad_value); 13977 1.12 christos internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol; 13978 1.12 christos result = false; 13979 1.12 christos } 13980 1.12 christos else 13981 1.12 christos { 13982 1.12 christos asymbol **ps; 13983 1.12 christos 13984 1.12 christos ps = symbols + r_sym (rela.r_info) - 1; 13985 1.12 christos internal_reloc->sym_ptr_ptr = ps; 13986 1.12 christos /* Make sure that this symbol is not removed by strip. */ 13987 1.12 christos (*ps)->flags |= BSF_KEEP; 13988 1.12 christos } 13989 1.13 christos 13990 1.13 christos internal_reloc->addend = rela.r_addend; 13991 1.12 christos 13992 1.12 christos res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela); 13993 1.12 christos if (! res || internal_reloc->howto == NULL) 13994 1.12 christos { 13995 1.12 christos #if DEBUG_SECONDARY_RELOCS 13996 1.12 christos fprintf (stderr, 13997 1.12 christos "there is no howto associated with reloc %lx\n", 13998 1.12 christos rela.r_info); 13999 1.12 christos #endif 14000 1.12 christos result = false; 14001 1.12 christos } 14002 1.12 christos } 14003 1.12 christos 14004 1.12 christos free (native_relocs); 14005 1.12 christos /* Store the internal relocs. */ 14006 1.12 christos elf_section_data (relsec)->sec_info = internal_relocs; 14007 1.12 christos } 14008 1.12 christos } 14009 1.13 christos 14010 1.13 christos return result; 14011 1.13 christos } 14012 1.13 christos 14013 1.12 christos /* Set the ELF section header fields of an output secondary reloc section. */ 14014 1.12 christos 14015 1.12 christos bool 14016 1.12 christos _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED, 14017 1.12 christos bfd *obfd ATTRIBUTE_UNUSED, 14018 1.12 christos const Elf_Internal_Shdr *isection, 14019 1.12 christos Elf_Internal_Shdr *osection) 14020 1.12 christos { 14021 1.12 christos asection * isec; 14022 1.12 christos asection * osec; 14023 1.12 christos struct bfd_elf_section_data * esd; 14024 1.12 christos 14025 1.12 christos if (isection == NULL) 14026 1.12 christos return false; 14027 1.12 christos 14028 1.12 christos if (isection->sh_type != SHT_SECONDARY_RELOC) 14029 1.12 christos return true; 14030 1.12 christos 14031 1.12 christos isec = isection->bfd_section; 14032 1.12 christos if (isec == NULL) 14033 1.12 christos return false; 14034 1.12 christos 14035 1.12 christos osec = osection->bfd_section; 14036 1.12 christos if (osec == NULL) 14037 1.12 christos return false; 14038 1.12 christos 14039 1.12 christos esd = elf_section_data (osec); 14040 1.12 christos BFD_ASSERT (esd->sec_info == NULL); 14041 1.12 christos esd->sec_info = elf_section_data (isec)->sec_info; 14042 1.13 christos osection->sh_type = SHT_RELA; 14043 1.13 christos osection->sh_link = elf_onesymtab (obfd); 14044 1.12 christos if (osection->sh_link == 0) 14045 1.12 christos { 14046 1.12 christos /* There is no symbol table - we are hosed... */ 14047 1.12 christos _bfd_error_handler 14048 1.12 christos /* xgettext:c-format */ 14049 1.13 christos (_("%pB(%pA): link section cannot be set" 14050 1.13 christos " because the output file does not have a symbol table"), 14051 1.12 christos obfd, osec); 14052 1.12 christos bfd_set_error (bfd_error_bad_value); 14053 1.12 christos return false; 14054 1.12 christos } 14055 1.12 christos 14056 1.12 christos /* Find the output section that corresponds to the isection's 14057 1.12 christos sh_info link. */ 14058 1.12 christos if (isection->sh_info == 0 14059 1.12 christos || isection->sh_info >= elf_numsections (ibfd)) 14060 1.12 christos { 14061 1.12 christos _bfd_error_handler 14062 1.12 christos /* xgettext:c-format */ 14063 1.12 christos (_("%pB(%pA): info section index is invalid"), 14064 1.12 christos obfd, osec); 14065 1.12 christos bfd_set_error (bfd_error_bad_value); 14066 1.12 christos return false; 14067 1.12 christos } 14068 1.12 christos 14069 1.12 christos isection = elf_elfsections (ibfd)[isection->sh_info]; 14070 1.13 christos 14071 1.13 christos if (isection == NULL 14072 1.12 christos || isection->bfd_section == NULL 14073 1.12 christos || isection->bfd_section->output_section == NULL) 14074 1.12 christos { 14075 1.12 christos _bfd_error_handler 14076 1.12 christos /* xgettext:c-format */ 14077 1.12 christos (_("%pB(%pA): info section index cannot be set" 14078 1.12 christos " because the section is not in the output"), 14079 1.12 christos obfd, osec); 14080 1.12 christos bfd_set_error (bfd_error_bad_value); 14081 1.12 christos return false; 14082 1.12 christos } 14083 1.12 christos 14084 1.12 christos esd = elf_section_data (isection->bfd_section->output_section); 14085 1.12 christos BFD_ASSERT (esd != NULL); 14086 1.12 christos osection->sh_info = esd->this_idx; 14087 1.12 christos esd->has_secondary_relocs = true; 14088 1.12 christos #if DEBUG_SECONDARY_RELOCS 14089 1.12 christos fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n", 14090 1.12 christos osec->name, osection->sh_link, osection->sh_info); 14091 1.12 christos fprintf (stderr, "mark section %s as having secondary relocs\n", 14092 1.12 christos bfd_section_name (isection->bfd_section->output_section)); 14093 1.12 christos #endif 14094 1.12 christos 14095 1.12 christos return true; 14096 1.12 christos } 14097 1.12 christos 14098 1.12 christos /* Write out a secondary reloc section. 14099 1.12 christos 14100 1.12 christos FIXME: Currently this function can result in a serious performance penalty 14101 1.12 christos for files with secondary relocs and lots of sections. The proper way to 14102 1.12 christos fix this is for _bfd_elf_copy_special_section_fields() to chain secondary 14103 1.12 christos relocs together and then to have this function just walk that chain. */ 14104 1.12 christos 14105 1.12 christos bool 14106 1.12 christos _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec) 14107 1.12 christos { 14108 1.12 christos const struct elf_backend_data * const ebd = get_elf_backend_data (abfd); 14109 1.12 christos bfd_vma addr_offset; 14110 1.12 christos asection * relsec; 14111 1.12 christos bfd_vma (*r_info) (bfd_vma, bfd_vma); 14112 1.12 christos bool result = true; 14113 1.12 christos 14114 1.12 christos if (sec == NULL) 14115 1.12 christos return false; 14116 1.12 christos 14117 1.12 christos #if BFD_DEFAULT_TARGET_SIZE > 32 14118 1.12 christos if (bfd_arch_bits_per_address (abfd) != 32) 14119 1.12 christos r_info = elf64_r_info; 14120 1.12 christos else 14121 1.12 christos #endif 14122 1.12 christos r_info = elf32_r_info; 14123 1.12 christos 14124 1.12 christos /* The address of an ELF reloc is section relative for an object 14125 1.12 christos file, and absolute for an executable file or shared library. 14126 1.12 christos The address of a BFD reloc is always section relative. */ 14127 1.12 christos addr_offset = 0; 14128 1.12 christos if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) 14129 1.12 christos addr_offset = sec->vma; 14130 1.12 christos 14131 1.12 christos /* Discover if there are any secondary reloc sections 14132 1.12 christos associated with SEC. */ 14133 1.12 christos for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next) 14134 1.12 christos { 14135 1.12 christos const struct bfd_elf_section_data * const esd = elf_section_data (relsec); 14136 1.13 christos Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr; 14137 1.13 christos 14138 1.13 christos if (hdr->sh_type == SHT_RELA 14139 1.12 christos && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx) 14140 1.12 christos { 14141 1.12 christos asymbol * last_sym; 14142 1.12 christos int last_sym_idx; 14143 1.12 christos size_t reloc_count; 14144 1.12 christos size_t idx; 14145 1.12 christos bfd_size_type entsize; 14146 1.12 christos arelent * src_irel; 14147 1.12 christos bfd_byte * dst_rela; 14148 1.12 christos 14149 1.12 christos if (hdr->contents != NULL) 14150 1.12 christos { 14151 1.12 christos _bfd_error_handler 14152 1.12 christos /* xgettext:c-format */ 14153 1.12 christos (_("%pB(%pA): error: secondary reloc section processed twice"), 14154 1.12 christos abfd, relsec); 14155 1.12 christos bfd_set_error (bfd_error_bad_value); 14156 1.12 christos result = false; 14157 1.12 christos continue; 14158 1.13 christos } 14159 1.13 christos 14160 1.12 christos entsize = hdr->sh_entsize; 14161 1.12 christos if (entsize == 0) 14162 1.12 christos { 14163 1.12 christos _bfd_error_handler 14164 1.12 christos /* xgettext:c-format */ 14165 1.12 christos (_("%pB(%pA): error: secondary reloc section" 14166 1.12 christos " has zero sized entries"), 14167 1.12 christos abfd, relsec); 14168 1.12 christos bfd_set_error (bfd_error_bad_value); 14169 1.12 christos result = false; 14170 1.13 christos continue; 14171 1.13 christos } 14172 1.12 christos else if (entsize != ebd->s->sizeof_rel 14173 1.12 christos && entsize != ebd->s->sizeof_rela) 14174 1.12 christos { 14175 1.12 christos _bfd_error_handler 14176 1.12 christos /* xgettext:c-format */ 14177 1.12 christos (_("%pB(%pA): error: secondary reloc section" 14178 1.12 christos " has non-standard sized entries"), 14179 1.13 christos abfd, relsec); 14180 1.13 christos bfd_set_error (bfd_error_bad_value); 14181 1.12 christos result = false; 14182 1.12 christos continue; 14183 1.12 christos } 14184 1.12 christos 14185 1.12 christos reloc_count = hdr->sh_size / entsize; 14186 1.12 christos hdr->sh_size = entsize * reloc_count; 14187 1.12 christos if (reloc_count == 0) 14188 1.12 christos { 14189 1.12 christos _bfd_error_handler 14190 1.12 christos /* xgettext:c-format */ 14191 1.12 christos (_("%pB(%pA): error: secondary reloc section is empty!"), 14192 1.12 christos abfd, relsec); 14193 1.12 christos bfd_set_error (bfd_error_bad_value); 14194 1.14 christos result = false; 14195 1.12 christos continue; 14196 1.12 christos } 14197 1.12 christos 14198 1.12 christos hdr->contents = bfd_alloc (abfd, hdr->sh_size); 14199 1.12 christos if (hdr->contents == NULL) 14200 1.12 christos continue; 14201 1.12 christos relsec->alloced = 1; 14202 1.12 christos 14203 1.12 christos #if DEBUG_SECONDARY_RELOCS 14204 1.12 christos fprintf (stderr, "write %u secondary relocs for %s from %s\n", 14205 1.12 christos reloc_count, sec->name, relsec->name); 14206 1.12 christos #endif 14207 1.12 christos last_sym = NULL; 14208 1.13 christos last_sym_idx = 0; 14209 1.13 christos dst_rela = hdr->contents; 14210 1.12 christos src_irel = (arelent *) esd->sec_info; 14211 1.12 christos if (src_irel == NULL) 14212 1.12 christos { 14213 1.12 christos _bfd_error_handler 14214 1.12 christos /* xgettext:c-format */ 14215 1.12 christos (_("%pB(%pA): error: internal relocs missing" 14216 1.12 christos " for secondary reloc section"), 14217 1.12 christos abfd, relsec); 14218 1.12 christos bfd_set_error (bfd_error_bad_value); 14219 1.12 christos result = false; 14220 1.12 christos continue; 14221 1.12 christos } 14222 1.12 christos 14223 1.12 christos for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize) 14224 1.12 christos { 14225 1.12 christos Elf_Internal_Rela src_rela; 14226 1.12 christos arelent *ptr; 14227 1.12 christos asymbol *sym; 14228 1.13 christos int n; 14229 1.12 christos 14230 1.12 christos ptr = src_irel + idx; 14231 1.12 christos if (ptr == NULL) 14232 1.12 christos { 14233 1.12 christos _bfd_error_handler 14234 1.12 christos /* xgettext:c-format */ 14235 1.12 christos (_("%pB(%pA): error: reloc table entry %zu is empty"), 14236 1.12 christos abfd, relsec, idx); 14237 1.12 christos bfd_set_error (bfd_error_bad_value); 14238 1.12 christos result = false; 14239 1.12 christos break; 14240 1.12 christos } 14241 1.12 christos 14242 1.12 christos if (ptr->sym_ptr_ptr == NULL) 14243 1.12 christos { 14244 1.12 christos /* FIXME: Is this an error ? */ 14245 1.12 christos n = 0; 14246 1.12 christos } 14247 1.12 christos else 14248 1.12 christos { 14249 1.12 christos sym = *ptr->sym_ptr_ptr; 14250 1.12 christos 14251 1.12 christos if (sym == last_sym) 14252 1.12 christos n = last_sym_idx; 14253 1.13 christos else 14254 1.13 christos { 14255 1.12 christos n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym); 14256 1.12 christos if (n < 0) 14257 1.12 christos { 14258 1.12 christos _bfd_error_handler 14259 1.12 christos /* xgettext:c-format */ 14260 1.12 christos (_("%pB(%pA): error: secondary reloc %zu" 14261 1.12 christos " references a missing symbol"), 14262 1.12 christos abfd, relsec, idx); 14263 1.12 christos bfd_set_error (bfd_error_bad_value); 14264 1.12 christos result = false; 14265 1.12 christos n = 0; 14266 1.12 christos } 14267 1.12 christos 14268 1.12 christos last_sym = sym; 14269 1.12 christos last_sym_idx = n; 14270 1.12 christos } 14271 1.13 christos 14272 1.13 christos if (sym->the_bfd != NULL 14273 1.12 christos && sym->the_bfd->xvec != abfd->xvec 14274 1.12 christos && ! _bfd_elf_validate_reloc (abfd, ptr)) 14275 1.12 christos { 14276 1.12 christos _bfd_error_handler 14277 1.12 christos /* xgettext:c-format */ 14278 1.12 christos (_("%pB(%pA): error: secondary reloc %zu" 14279 1.12 christos " references a deleted symbol"), 14280 1.12 christos abfd, relsec, idx); 14281 1.12 christos bfd_set_error (bfd_error_bad_value); 14282 1.12 christos result = false; 14283 1.12 christos n = 0; 14284 1.12 christos } 14285 1.13 christos } 14286 1.13 christos 14287 1.12 christos src_rela.r_offset = ptr->address + addr_offset; 14288 1.12 christos if (ptr->howto == NULL) 14289 1.12 christos { 14290 1.12 christos _bfd_error_handler 14291 1.12 christos /* xgettext:c-format */ 14292 1.12 christos (_("%pB(%pA): error: secondary reloc %zu" 14293 1.12 christos " is of an unknown type"), 14294 1.12 christos abfd, relsec, idx); 14295 1.12 christos bfd_set_error (bfd_error_bad_value); 14296 1.12 christos result = false; 14297 1.12 christos src_rela.r_info = r_info (0, 0); 14298 1.12 christos } 14299 1.12 christos else 14300 1.12 christos src_rela.r_info = r_info (n, ptr->howto->type); 14301 1.12 christos src_rela.r_addend = ptr->addend; 14302 1.12 christos 14303 1.12 christos if (entsize == ebd->s->sizeof_rel) 14304 1.12 christos ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela); 14305 1.1 christos else /* entsize == ebd->s->sizeof_rela */ 14306 1.14 christos ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela); 14307 1.14 christos } 14308 1.14 christos } 14309 1.14 christos } 14310 1.14 christos 14311 1.14 christos return result; 14312 1.14 christos } 14313 1.14 christos 14314 1.14 christos /* Mmap in section contents. */ 14315 1.14 christos 14316 1.14 christos static bool 14317 1.14 christos elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf) 14318 1.14 christos { 14319 1.14 christos #ifdef USE_MMAP 14320 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 14321 1.14 christos if (bed->use_mmap 14322 1.14 christos && sec->compress_status == COMPRESS_SECTION_NONE 14323 1.14 christos && (sec->flags & SEC_LINKER_CREATED) == 0) 14324 1.14 christos { 14325 1.14 christos /* Use mmap only if section size >= the minimum mmap section 14326 1.14 christos size. */ 14327 1.14 christos size_t readsz = bfd_get_section_limit_octets (abfd, sec); 14328 1.14 christos size_t allocsz = bfd_get_section_alloc_size (abfd, sec); 14329 1.14 christos if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size) 14330 1.14 christos { 14331 1.14 christos if (sec->contents != NULL) 14332 1.14 christos { 14333 1.14 christos if (!sec->mmapped_p) 14334 1.14 christos abort (); 14335 1.14 christos *buf = sec->contents; 14336 1.14 christos return true; 14337 1.14 christos } 14338 1.14 christos if (sec->mmapped_p) 14339 1.14 christos abort (); 14340 1.14 christos sec->mmapped_p = 1; 14341 1.14 christos 14342 1.14 christos /* We can't use the final link preallocated buffer for mmap. */ 14343 1.14 christos *buf = NULL; 14344 1.14 christos } 14345 1.14 christos } 14346 1.14 christos #endif 14347 1.14 christos /* FIXME: We should not get here if sec->alloced is set. */ 14348 1.14 christos bool ret = bfd_get_full_section_contents (abfd, sec, buf); 14349 1.14 christos if (ret && sec->mmapped_p) 14350 1.14 christos *buf = sec->contents; 14351 1.14 christos return ret; 14352 1.14 christos } 14353 1.14 christos 14354 1.14 christos /* Mmap in section contents. */ 14355 1.14 christos 14356 1.14 christos bool 14357 1.14 christos _bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf) 14358 1.14 christos { 14359 1.14 christos *buf = NULL; 14360 1.14 christos return elf_mmap_section_contents (abfd, sec, buf); 14361 1.14 christos } 14362 1.14 christos 14363 1.14 christos /* Mmap in the full section contents for the final link. */ 14364 1.14 christos 14365 1.14 christos bool 14366 1.14 christos _bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec, 14367 1.14 christos bfd_byte **buf) 14368 1.14 christos { 14369 1.14 christos return elf_mmap_section_contents (abfd, sec, buf); 14370 1.14 christos } 14371 1.14 christos 14372 1.14 christos /* Munmap section contents. */ 14373 1.14 christos 14374 1.14 christos void 14375 1.14 christos _bfd_elf_munmap_section_contents (asection *sec, void *contents) 14376 1.14 christos { 14377 1.14 christos /* NB: Since _bfd_elf_munmap_section_contents is called like free, 14378 1.14 christos CONTENTS may be NULL. */ 14379 1.14 christos if (contents == NULL) 14380 1.14 christos return; 14381 1.14 christos 14382 1.14 christos if (sec->alloced 14383 1.14 christos /* What a tangled web we weave with section contents. 14384 1.14 christos FIXME: We shouldn't need to test anything but sec->alloced 14385 1.14 christos here, but there are cases where a buffer is allocated for a 14386 1.14 christos section but then another buffer is malloc'd anyway. eg. 14387 1.14 christos trace through ld-elf/eh4 testcase on x86_64. */ 14388 1.14 christos && (sec->contents == contents 14389 1.14 christos || elf_section_data (sec)->this_hdr.contents == contents)) 14390 1.14 christos return; 14391 1.14 christos 14392 1.14 christos /* Don't leave pointers to data we are about to munmap or free. */ 14393 1.14 christos if (sec->contents == contents) 14394 1.14 christos sec->contents = NULL; 14395 1.14 christos if (elf_section_data (sec)->this_hdr.contents == contents) 14396 1.14 christos elf_section_data (sec)->this_hdr.contents = NULL; 14397 1.14 christos 14398 1.14 christos #ifdef USE_MMAP 14399 1.14 christos if (sec->mmapped_p) 14400 1.14 christos { 14401 1.14 christos /* When _bfd_elf_mmap_section_contents returns CONTENTS as 14402 1.14 christos malloced, CONTENTS_ADDR is set to NULL. */ 14403 1.14 christos if (elf_section_data (sec)->contents_addr != NULL) 14404 1.14 christos { 14405 1.14 christos /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */ 14406 1.14 christos if (munmap (elf_section_data (sec)->contents_addr, 14407 1.14 christos elf_section_data (sec)->contents_size) != 0) 14408 1.14 christos abort (); 14409 1.14 christos sec->mmapped_p = 0; 14410 1.14 christos elf_section_data (sec)->contents_addr = NULL; 14411 1.14 christos elf_section_data (sec)->contents_size = 0; 14412 1.14 christos return; 14413 1.14 christos } 14414 1.14 christos } 14415 1.14 christos #endif 14416 1.14 christos 14417 1.14 christos free (contents); 14418 1.14 christos } 14419 1.14 christos 14420 1.14 christos /* Munmap the full section contents for the final link. */ 14421 1.14 christos 14422 1.14 christos void 14423 1.14 christos _bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED) 14424 1.14 christos { 14425 1.14 christos #ifdef USE_MMAP 14426 1.14 christos if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL) 14427 1.14 christos { 14428 1.14 christos /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as 14429 1.14 christos malloced, CONTENTS_ADDR is set to NULL. */ 14430 1.14 christos /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */ 14431 1.14 christos if (munmap (elf_section_data (sec)->contents_addr, 14432 1.14 christos elf_section_data (sec)->contents_size) != 0) 14433 1.14 christos abort (); 14434 1.14 christos sec->mmapped_p = 0; 14435 sec->contents = NULL; 14436 elf_section_data (sec)->this_hdr.contents = NULL; 14437 elf_section_data (sec)->contents_addr = NULL; 14438 elf_section_data (sec)->contents_size = 0; 14439 } 14440 #endif 14441 } 14442