1 1.1 christos /* ELF executable support for BFD. 2 1.1 christos 3 1.19 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.11 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.8 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.19 christos /* Utility macro to make testing for string equality easier to read. */ 53 1.19 christos #ifndef streq 54 1.19 christos #define streq(A,B) (strcmp ((A), (B)) == 0) 55 1.19 christos #endif 56 1.19 christos 57 1.19 christos /* Core note names. */ 58 1.19 christos #define NOTE_NAME_CORE "CORE" 59 1.19 christos #define NOTE_NAME_FREEBSD "FreeBSD" 60 1.19 christos #define NOTE_NAME_GDB "GDB" 61 1.19 christos #define NOTE_NAME_LINUX "LINUX" 62 1.19 christos 63 1.19 christos /* Names of a pseudo-section which represent core notes. */ 64 1.19 christos #define NOTE_PSEUDO_SECTION_AARCH_GCS ".reg-aarch-gcs" 65 1.19 christos #define NOTE_PSEUDO_SECTION_AARCH_HW_BREAK ".reg-aarch-hw-break" 66 1.19 christos #define NOTE_PSEUDO_SECTION_AARCH_HW_WATCH ".reg-aarch-hw-watch" 67 1.19 christos #define NOTE_PSEUDO_SECTION_AARCH_MTE ".reg-aarch-mte" 68 1.19 christos #define NOTE_PSEUDO_SECTION_AARCH_PAUTH ".reg-aarch-pauth" 69 1.19 christos #define NOTE_PSEUDO_SECTION_AARCH_SSVE ".reg-aarch-ssve" 70 1.19 christos #define NOTE_PSEUDO_SECTION_AARCH_SVE ".reg-aarch-sve" 71 1.19 christos #define NOTE_PSEUDO_SECTION_AARCH_TLS ".reg-aarch-tls" 72 1.19 christos #define NOTE_PSEUDO_SECTION_AARCH_ZA ".reg-aarch-za" 73 1.19 christos #define NOTE_PSEUDO_SECTION_AARCH_ZT ".reg-aarch-zt" 74 1.19 christos #define NOTE_PSEUDO_SECTION_ARC_V2 ".reg-arc-v2" 75 1.19 christos #define NOTE_PSEUDO_SECTION_ARM_VFP ".reg-arm-vfp" 76 1.19 christos #define NOTE_PSEUDO_SECTION_I386_TLS ".reg-i386-tls" 77 1.19 christos #define NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG ".reg-loongarch-cpucfg" 78 1.19 christos #define NOTE_PSEUDO_SECTION_LOONGARCH_LASX ".reg-loongarch-lasx" 79 1.19 christos #define NOTE_PSEUDO_SECTION_LOONGARCH_LBT ".reg-loongarch-lbt" 80 1.19 christos #define NOTE_PSEUDO_SECTION_LOONGARCH_LSX ".reg-loongarch-lsx" 81 1.19 christos #define NOTE_PSEUDO_SECTION_PPC_DSCR ".reg-ppc-dscr" 82 1.19 christos #define NOTE_PSEUDO_SECTION_PPC_EBB ".reg-ppc-ebb" 83 1.19 christos #define NOTE_PSEUDO_SECTION_PPC_PMU ".reg-ppc-pmu" 84 1.19 christos #define NOTE_PSEUDO_SECTION_PPC_PPR ".reg-ppc-ppr" 85 1.19 christos #define NOTE_PSEUDO_SECTION_PPC_TAR ".reg-ppc-tar" 86 1.19 christos #define NOTE_PSEUDO_SECTION_PPC_TM_CDSCR ".reg-ppc-tm-cdscr" 87 1.19 christos #define NOTE_PSEUDO_SECTION_PPC_TM_CFPR ".reg-ppc-tm-cfpr" 88 1.19 christos #define NOTE_PSEUDO_SECTION_PPC_TM_CGPR ".reg-ppc-tm-cgpr" 89 1.19 christos #define NOTE_PSEUDO_SECTION_PPC_TM_CPPR ".reg-ppc-tm-cppr" 90 1.19 christos #define NOTE_PSEUDO_SECTION_PPC_TM_CTAR ".reg-ppc-tm-ctar" 91 1.19 christos #define NOTE_PSEUDO_SECTION_PPC_TM_CVMX ".reg-ppc-tm-cvmx" 92 1.19 christos #define NOTE_PSEUDO_SECTION_PPC_TM_CVSX ".reg-ppc-tm-cvsx" 93 1.19 christos #define NOTE_PSEUDO_SECTION_PPC_TM_SPR ".reg-ppc-tm-spr" 94 1.19 christos #define NOTE_PSEUDO_SECTION_PPC_VSX ".reg-ppc-vsx" 95 1.19 christos #define NOTE_PSEUDO_SECTION_PPC_VMX ".reg-ppc-vmx" 96 1.19 christos #define NOTE_PSEUDO_SECTION_REG ".reg" 97 1.19 christos #define NOTE_PSEUDO_SECTION_REG2 ".reg2" 98 1.19 christos #define NOTE_PSEUDO_SECTION_RISCV_CSR ".reg-riscv-csr" 99 1.19 christos #define NOTE_PSEUDO_SECTION_S390_CTRS ".reg-s390-ctrs" 100 1.19 christos #define NOTE_PSEUDO_SECTION_S390_GS_BC ".reg-s390-gs-bc" 101 1.19 christos #define NOTE_PSEUDO_SECTION_S390_GS_CB ".reg-s390-gs-cb" 102 1.19 christos #define NOTE_PSEUDO_SECTION_S390_HIGH_GPRS ".reg-s390-high-gprs" 103 1.19 christos #define NOTE_PSEUDO_SECTION_S390_LAST_BREAK ".reg-s390-last-break" 104 1.19 christos #define NOTE_PSEUDO_SECTION_S390_PREFIX ".reg-s390-prefix" 105 1.19 christos #define NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL ".reg-s390-system-call" 106 1.19 christos #define NOTE_PSEUDO_SECTION_S390_TDB ".reg-s390-tdb" 107 1.19 christos #define NOTE_PSEUDO_SECTION_S390_TIMER ".reg-s390-timer" 108 1.19 christos #define NOTE_PSEUDO_SECTION_S390_TODCMP ".reg-s390-todcmp" 109 1.19 christos #define NOTE_PSEUDO_SECTION_S390_TODPREG ".reg-s390-todpreg" 110 1.19 christos #define NOTE_PSEUDO_SECTION_S390_VXRS_HIGH ".reg-s390-vxrs-high" 111 1.19 christos #define NOTE_PSEUDO_SECTION_S390_VXRS_LOW ".reg-s390-vxrs-low" 112 1.19 christos #define NOTE_PSEUDO_SECTION_SSP ".reg-ssp" 113 1.19 christos #define NOTE_PSEUDO_SECTION_TDESC ".gdb-tdesc" 114 1.19 christos #define NOTE_PSEUDO_SECTION_X86_SEGBASES ".reg-x86-segbases" 115 1.19 christos #define NOTE_PSEUDO_SECTION_XFP ".reg-xfp" 116 1.19 christos #define NOTE_PSEUDO_SECTION_XSTATE ".reg-xstate" 117 1.19 christos 118 1.1 christos static int elf_sort_sections (const void *, const void *); 119 1.14 christos static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); 120 1.14 christos static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int, 121 1.14 christos struct bfd_link_info *); 122 1.14 christos static bool elf_parse_notes (bfd *abfd, char *buf, size_t size, 123 1.14 christos file_ptr offset, size_t align); 124 1.1 christos 125 1.1 christos /* Swap version information in and out. The version information is 126 1.1 christos currently size independent. If that ever changes, this code will 127 1.1 christos need to move into elfcode.h. */ 128 1.1 christos 129 1.1 christos /* Swap in a Verdef structure. */ 130 1.1 christos 131 1.1 christos void 132 1.1 christos _bfd_elf_swap_verdef_in (bfd *abfd, 133 1.1 christos const Elf_External_Verdef *src, 134 1.1 christos Elf_Internal_Verdef *dst) 135 1.1 christos { 136 1.1 christos dst->vd_version = H_GET_16 (abfd, src->vd_version); 137 1.1 christos dst->vd_flags = H_GET_16 (abfd, src->vd_flags); 138 1.1 christos dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); 139 1.1 christos dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); 140 1.1 christos dst->vd_hash = H_GET_32 (abfd, src->vd_hash); 141 1.1 christos dst->vd_aux = H_GET_32 (abfd, src->vd_aux); 142 1.1 christos dst->vd_next = H_GET_32 (abfd, src->vd_next); 143 1.1 christos } 144 1.1 christos 145 1.1 christos /* Swap out a Verdef structure. */ 146 1.1 christos 147 1.1 christos void 148 1.1 christos _bfd_elf_swap_verdef_out (bfd *abfd, 149 1.1 christos const Elf_Internal_Verdef *src, 150 1.1 christos Elf_External_Verdef *dst) 151 1.1 christos { 152 1.1 christos H_PUT_16 (abfd, src->vd_version, dst->vd_version); 153 1.1 christos H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); 154 1.1 christos H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); 155 1.1 christos H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); 156 1.1 christos H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); 157 1.1 christos H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); 158 1.1 christos H_PUT_32 (abfd, src->vd_next, dst->vd_next); 159 1.1 christos } 160 1.1 christos 161 1.1 christos /* Swap in a Verdaux structure. */ 162 1.1 christos 163 1.1 christos void 164 1.1 christos _bfd_elf_swap_verdaux_in (bfd *abfd, 165 1.1 christos const Elf_External_Verdaux *src, 166 1.1 christos Elf_Internal_Verdaux *dst) 167 1.1 christos { 168 1.1 christos dst->vda_name = H_GET_32 (abfd, src->vda_name); 169 1.1 christos dst->vda_next = H_GET_32 (abfd, src->vda_next); 170 1.1 christos } 171 1.1 christos 172 1.1 christos /* Swap out a Verdaux structure. */ 173 1.1 christos 174 1.1 christos void 175 1.1 christos _bfd_elf_swap_verdaux_out (bfd *abfd, 176 1.1 christos const Elf_Internal_Verdaux *src, 177 1.1 christos Elf_External_Verdaux *dst) 178 1.1 christos { 179 1.1 christos H_PUT_32 (abfd, src->vda_name, dst->vda_name); 180 1.1 christos H_PUT_32 (abfd, src->vda_next, dst->vda_next); 181 1.1 christos } 182 1.1 christos 183 1.1 christos /* Swap in a Verneed structure. */ 184 1.1 christos 185 1.1 christos void 186 1.1 christos _bfd_elf_swap_verneed_in (bfd *abfd, 187 1.1 christos const Elf_External_Verneed *src, 188 1.1 christos Elf_Internal_Verneed *dst) 189 1.1 christos { 190 1.1 christos dst->vn_version = H_GET_16 (abfd, src->vn_version); 191 1.1 christos dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); 192 1.1 christos dst->vn_file = H_GET_32 (abfd, src->vn_file); 193 1.1 christos dst->vn_aux = H_GET_32 (abfd, src->vn_aux); 194 1.1 christos dst->vn_next = H_GET_32 (abfd, src->vn_next); 195 1.1 christos } 196 1.1 christos 197 1.1 christos /* Swap out a Verneed structure. */ 198 1.1 christos 199 1.1 christos void 200 1.1 christos _bfd_elf_swap_verneed_out (bfd *abfd, 201 1.1 christos const Elf_Internal_Verneed *src, 202 1.1 christos Elf_External_Verneed *dst) 203 1.1 christos { 204 1.1 christos H_PUT_16 (abfd, src->vn_version, dst->vn_version); 205 1.1 christos H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); 206 1.1 christos H_PUT_32 (abfd, src->vn_file, dst->vn_file); 207 1.1 christos H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); 208 1.1 christos H_PUT_32 (abfd, src->vn_next, dst->vn_next); 209 1.1 christos } 210 1.1 christos 211 1.1 christos /* Swap in a Vernaux structure. */ 212 1.1 christos 213 1.1 christos void 214 1.1 christos _bfd_elf_swap_vernaux_in (bfd *abfd, 215 1.1 christos const Elf_External_Vernaux *src, 216 1.1 christos Elf_Internal_Vernaux *dst) 217 1.1 christos { 218 1.1 christos dst->vna_hash = H_GET_32 (abfd, src->vna_hash); 219 1.1 christos dst->vna_flags = H_GET_16 (abfd, src->vna_flags); 220 1.1 christos dst->vna_other = H_GET_16 (abfd, src->vna_other); 221 1.1 christos dst->vna_name = H_GET_32 (abfd, src->vna_name); 222 1.1 christos dst->vna_next = H_GET_32 (abfd, src->vna_next); 223 1.1 christos } 224 1.1 christos 225 1.1 christos /* Swap out a Vernaux structure. */ 226 1.1 christos 227 1.1 christos void 228 1.1 christos _bfd_elf_swap_vernaux_out (bfd *abfd, 229 1.1 christos const Elf_Internal_Vernaux *src, 230 1.1 christos Elf_External_Vernaux *dst) 231 1.1 christos { 232 1.1 christos H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); 233 1.1 christos H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); 234 1.1 christos H_PUT_16 (abfd, src->vna_other, dst->vna_other); 235 1.1 christos H_PUT_32 (abfd, src->vna_name, dst->vna_name); 236 1.1 christos H_PUT_32 (abfd, src->vna_next, dst->vna_next); 237 1.1 christos } 238 1.1 christos 239 1.1 christos /* Swap in a Versym structure. */ 240 1.1 christos 241 1.1 christos void 242 1.1 christos _bfd_elf_swap_versym_in (bfd *abfd, 243 1.1 christos const Elf_External_Versym *src, 244 1.1 christos Elf_Internal_Versym *dst) 245 1.1 christos { 246 1.1 christos dst->vs_vers = H_GET_16 (abfd, src->vs_vers); 247 1.1 christos } 248 1.1 christos 249 1.1 christos /* Swap out a Versym structure. */ 250 1.1 christos 251 1.1 christos void 252 1.1 christos _bfd_elf_swap_versym_out (bfd *abfd, 253 1.1 christos const Elf_Internal_Versym *src, 254 1.1 christos Elf_External_Versym *dst) 255 1.1 christos { 256 1.1 christos H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); 257 1.1 christos } 258 1.1 christos 259 1.1 christos /* Standard ELF hash function. Do not change this function; you will 260 1.1 christos cause invalid hash tables to be generated. */ 261 1.1 christos 262 1.1 christos unsigned long 263 1.1 christos bfd_elf_hash (const char *namearg) 264 1.1 christos { 265 1.17 christos uint32_t h = 0; 266 1.1 christos 267 1.17 christos for (const unsigned char *name = (const unsigned char *) namearg; 268 1.17 christos *name; name++) 269 1.1 christos { 270 1.17 christos h = (h << 4) + *name; 271 1.17 christos h ^= (h >> 24) & 0xf0; 272 1.1 christos } 273 1.17 christos return h & 0x0fffffff; 274 1.1 christos } 275 1.1 christos 276 1.1 christos /* DT_GNU_HASH hash function. Do not change this function; you will 277 1.1 christos cause invalid hash tables to be generated. */ 278 1.1 christos 279 1.1 christos unsigned long 280 1.1 christos bfd_elf_gnu_hash (const char *namearg) 281 1.1 christos { 282 1.17 christos uint32_t h = 5381; 283 1.17 christos 284 1.17 christos for (const unsigned char *name = (const unsigned char *) namearg; 285 1.17 christos *name; name++) 286 1.17 christos h = (h << 5) + h + *name; 287 1.17 christos return h; 288 1.1 christos } 289 1.1 christos 290 1.1 christos /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with 291 1.18 christos the object_id field of an elf_obj_tdata field set. */ 292 1.14 christos bool 293 1.1 christos bfd_elf_allocate_object (bfd *abfd, 294 1.18 christos size_t object_size) 295 1.1 christos { 296 1.1 christos BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); 297 1.1 christos abfd->tdata.any = bfd_zalloc (abfd, object_size); 298 1.1 christos if (abfd->tdata.any == NULL) 299 1.14 christos return false; 300 1.1 christos 301 1.18 christos elf_object_id (abfd) = get_elf_backend_data (abfd)->target_id; 302 1.3 christos if (abfd->direction != read_direction) 303 1.3 christos { 304 1.3 christos struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); 305 1.3 christos if (o == NULL) 306 1.14 christos return false; 307 1.3 christos elf_tdata (abfd)->o = o; 308 1.3 christos elf_program_header_size (abfd) = (bfd_size_type) -1; 309 1.3 christos } 310 1.14 christos return true; 311 1.1 christos } 312 1.1 christos 313 1.1 christos 314 1.14 christos bool 315 1.1 christos bfd_elf_make_object (bfd *abfd) 316 1.1 christos { 317 1.18 christos return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata)); 318 1.1 christos } 319 1.1 christos 320 1.14 christos bool 321 1.1 christos bfd_elf_mkcorefile (bfd *abfd) 322 1.1 christos { 323 1.1 christos /* I think this can be done just like an object file. */ 324 1.3 christos if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) 325 1.14 christos return false; 326 1.3 christos elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); 327 1.3 christos return elf_tdata (abfd)->core != NULL; 328 1.1 christos } 329 1.1 christos 330 1.12 christos char * 331 1.1 christos bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) 332 1.1 christos { 333 1.1 christos Elf_Internal_Shdr **i_shdrp; 334 1.1 christos bfd_byte *shstrtab = NULL; 335 1.1 christos file_ptr offset; 336 1.1 christos bfd_size_type shstrtabsize; 337 1.1 christos 338 1.1 christos i_shdrp = elf_elfsections (abfd); 339 1.1 christos if (i_shdrp == 0 340 1.1 christos || shindex >= elf_numsections (abfd) 341 1.1 christos || i_shdrp[shindex] == 0) 342 1.1 christos return NULL; 343 1.1 christos 344 1.1 christos shstrtab = i_shdrp[shindex]->contents; 345 1.1 christos if (shstrtab == NULL) 346 1.1 christos { 347 1.1 christos /* No cached one, attempt to read, and cache what we read. */ 348 1.1 christos offset = i_shdrp[shindex]->sh_offset; 349 1.1 christos shstrtabsize = i_shdrp[shindex]->sh_size; 350 1.1 christos 351 1.18 christos if (shstrtabsize == 0 352 1.5 christos || bfd_seek (abfd, offset, SEEK_SET) != 0 353 1.18 christos || (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL) 354 1.1 christos { 355 1.1 christos /* Once we've failed to read it, make sure we don't keep 356 1.1 christos trying. Otherwise, we'll keep allocating space for 357 1.1 christos the string table over and over. */ 358 1.1 christos i_shdrp[shindex]->sh_size = 0; 359 1.1 christos } 360 1.18 christos else if (shstrtab[shstrtabsize - 1] != 0) 361 1.17 christos { 362 1.17 christos /* It is an error if a string table isn't terminated. */ 363 1.17 christos _bfd_error_handler 364 1.17 christos /* xgettext:c-format */ 365 1.18 christos (_("%pB: string table [%u] is corrupt"), abfd, shindex); 366 1.18 christos shstrtab[shstrtabsize - 1] = 0; 367 1.17 christos } 368 1.1 christos i_shdrp[shindex]->contents = shstrtab; 369 1.1 christos } 370 1.1 christos return (char *) shstrtab; 371 1.1 christos } 372 1.1 christos 373 1.1 christos char * 374 1.1 christos bfd_elf_string_from_elf_section (bfd *abfd, 375 1.1 christos unsigned int shindex, 376 1.1 christos unsigned int strindex) 377 1.1 christos { 378 1.1 christos Elf_Internal_Shdr *hdr; 379 1.1 christos 380 1.1 christos if (strindex == 0) 381 1.1 christos return ""; 382 1.1 christos 383 1.1 christos if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) 384 1.1 christos return NULL; 385 1.1 christos 386 1.1 christos hdr = elf_elfsections (abfd)[shindex]; 387 1.1 christos 388 1.5 christos if (hdr->contents == NULL) 389 1.5 christos { 390 1.5 christos if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) 391 1.5 christos { 392 1.5 christos /* PR 17512: file: f057ec89. */ 393 1.9 christos /* xgettext:c-format */ 394 1.11 christos _bfd_error_handler (_("%pB: attempt to load strings from" 395 1.9 christos " a non-string section (number %d)"), 396 1.5 christos abfd, shindex); 397 1.5 christos return NULL; 398 1.5 christos } 399 1.6 christos 400 1.5 christos if (bfd_elf_get_str_section (abfd, shindex) == NULL) 401 1.5 christos return NULL; 402 1.5 christos } 403 1.12 christos else 404 1.12 christos { 405 1.12 christos /* PR 24273: The string section's contents may have already 406 1.12 christos been loaded elsewhere, eg because a corrupt file has the 407 1.12 christos string section index in the ELF header pointing at a group 408 1.12 christos section. So be paranoid, and test that the last byte of 409 1.12 christos the section is zero. */ 410 1.12 christos if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0) 411 1.12 christos return NULL; 412 1.12 christos } 413 1.1 christos 414 1.1 christos if (strindex >= hdr->sh_size) 415 1.1 christos { 416 1.1 christos unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; 417 1.9 christos _bfd_error_handler 418 1.9 christos /* xgettext:c-format */ 419 1.11 christos (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"), 420 1.11 christos abfd, strindex, (uint64_t) hdr->sh_size, 421 1.1 christos (shindex == shstrndx && strindex == hdr->sh_name 422 1.1 christos ? ".shstrtab" 423 1.1 christos : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); 424 1.1 christos return NULL; 425 1.1 christos } 426 1.1 christos 427 1.1 christos return ((char *) hdr->contents) + strindex; 428 1.1 christos } 429 1.1 christos 430 1.1 christos /* Read and convert symbols to internal format. 431 1.1 christos SYMCOUNT specifies the number of symbols to read, starting from 432 1.1 christos symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF 433 1.1 christos are non-NULL, they are used to store the internal symbols, external 434 1.1 christos symbols, and symbol section index extensions, respectively. 435 1.1 christos Returns a pointer to the internal symbol buffer (malloced if necessary) 436 1.1 christos or NULL if there were no symbols or some kind of problem. */ 437 1.1 christos 438 1.1 christos Elf_Internal_Sym * 439 1.1 christos bfd_elf_get_elf_syms (bfd *ibfd, 440 1.1 christos Elf_Internal_Shdr *symtab_hdr, 441 1.1 christos size_t symcount, 442 1.1 christos size_t symoffset, 443 1.1 christos Elf_Internal_Sym *intsym_buf, 444 1.1 christos void *extsym_buf, 445 1.1 christos Elf_External_Sym_Shndx *extshndx_buf) 446 1.1 christos { 447 1.1 christos Elf_Internal_Shdr *shndx_hdr; 448 1.1 christos void *alloc_ext; 449 1.1 christos const bfd_byte *esym; 450 1.1 christos Elf_External_Sym_Shndx *alloc_extshndx; 451 1.1 christos Elf_External_Sym_Shndx *shndx; 452 1.1 christos Elf_Internal_Sym *alloc_intsym; 453 1.1 christos Elf_Internal_Sym *isym; 454 1.1 christos Elf_Internal_Sym *isymend; 455 1.1 christos const struct elf_backend_data *bed; 456 1.1 christos size_t extsym_size; 457 1.12 christos size_t amt; 458 1.1 christos file_ptr pos; 459 1.1 christos 460 1.1 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) 461 1.1 christos abort (); 462 1.1 christos 463 1.1 christos if (symcount == 0) 464 1.1 christos return intsym_buf; 465 1.1 christos 466 1.17 christos if (elf_use_dt_symtab_p (ibfd)) 467 1.17 christos { 468 1.17 christos /* Use dynamic symbol table. */ 469 1.17 christos if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset) 470 1.17 christos { 471 1.17 christos bfd_set_error (bfd_error_invalid_operation); 472 1.17 christos return NULL; 473 1.17 christos } 474 1.17 christos return elf_tdata (ibfd)->dt_symtab + symoffset; 475 1.17 christos } 476 1.17 christos 477 1.1 christos /* Normal syms might have section extension entries. */ 478 1.1 christos shndx_hdr = NULL; 479 1.8 christos if (elf_symtab_shndx_list (ibfd) != NULL) 480 1.8 christos { 481 1.8 christos elf_section_list * entry; 482 1.8 christos Elf_Internal_Shdr **sections = elf_elfsections (ibfd); 483 1.8 christos 484 1.8 christos /* Find an index section that is linked to this symtab section. */ 485 1.8 christos for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next) 486 1.8 christos { 487 1.8 christos /* PR 20063. */ 488 1.8 christos if (entry->hdr.sh_link >= elf_numsections (ibfd)) 489 1.8 christos continue; 490 1.8 christos 491 1.8 christos if (sections[entry->hdr.sh_link] == symtab_hdr) 492 1.8 christos { 493 1.8 christos shndx_hdr = & entry->hdr; 494 1.8 christos break; 495 1.8 christos }; 496 1.8 christos } 497 1.8 christos 498 1.8 christos if (shndx_hdr == NULL) 499 1.8 christos { 500 1.14 christos if (symtab_hdr == &elf_symtab_hdr (ibfd)) 501 1.14 christos /* Not really accurate, but this was how the old code used 502 1.14 christos to work. */ 503 1.14 christos shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr; 504 1.8 christos /* Otherwise we do nothing. The assumption is that 505 1.8 christos the index table will not be needed. */ 506 1.8 christos } 507 1.8 christos } 508 1.1 christos 509 1.1 christos /* Read the symbols. */ 510 1.1 christos alloc_ext = NULL; 511 1.1 christos alloc_extshndx = NULL; 512 1.1 christos alloc_intsym = NULL; 513 1.1 christos bed = get_elf_backend_data (ibfd); 514 1.1 christos extsym_size = bed->s->sizeof_sym; 515 1.12 christos if (_bfd_mul_overflow (symcount, extsym_size, &amt)) 516 1.12 christos { 517 1.12 christos bfd_set_error (bfd_error_file_too_big); 518 1.17 christos return NULL; 519 1.12 christos } 520 1.1 christos pos = symtab_hdr->sh_offset + symoffset * extsym_size; 521 1.17 christos size_t alloc_ext_size = amt; 522 1.17 christos if (bfd_seek (ibfd, pos, SEEK_SET) != 0 523 1.17 christos || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size, 524 1.17 christos &alloc_ext, ibfd, false)) 525 1.1 christos { 526 1.1 christos intsym_buf = NULL; 527 1.17 christos goto out2; 528 1.1 christos } 529 1.1 christos 530 1.17 christos size_t alloc_extshndx_size = 0; 531 1.1 christos if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) 532 1.1 christos extshndx_buf = NULL; 533 1.1 christos else 534 1.1 christos { 535 1.12 christos if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt)) 536 1.12 christos { 537 1.12 christos bfd_set_error (bfd_error_file_too_big); 538 1.12 christos intsym_buf = NULL; 539 1.17 christos goto out1; 540 1.12 christos } 541 1.17 christos alloc_extshndx_size = amt; 542 1.1 christos pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); 543 1.17 christos if (bfd_seek (ibfd, pos, SEEK_SET) != 0 544 1.17 christos || !_bfd_mmap_read_temporary ((void **) &extshndx_buf, 545 1.17 christos &alloc_extshndx_size, 546 1.17 christos (void **) &alloc_extshndx, 547 1.17 christos ibfd, false)) 548 1.1 christos { 549 1.1 christos intsym_buf = NULL; 550 1.17 christos goto out1; 551 1.1 christos } 552 1.1 christos } 553 1.1 christos 554 1.1 christos if (intsym_buf == NULL) 555 1.1 christos { 556 1.12 christos if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt)) 557 1.12 christos { 558 1.12 christos bfd_set_error (bfd_error_file_too_big); 559 1.17 christos goto out1; 560 1.12 christos } 561 1.12 christos alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt); 562 1.1 christos intsym_buf = alloc_intsym; 563 1.1 christos if (intsym_buf == NULL) 564 1.17 christos goto out1; 565 1.1 christos } 566 1.1 christos 567 1.1 christos /* Convert the symbols to internal form. */ 568 1.1 christos isymend = intsym_buf + symcount; 569 1.1 christos for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, 570 1.11 christos shndx = extshndx_buf; 571 1.1 christos isym < isymend; 572 1.1 christos esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) 573 1.19 christos { 574 1.19 christos if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) 575 1.19 christos { 576 1.19 christos symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; 577 1.19 christos /* xgettext:c-format */ 578 1.19 christos _bfd_error_handler (_("%pB symbol number %lu references" 579 1.19 christos " nonexistent SHT_SYMTAB_SHNDX section"), 580 1.19 christos ibfd, (unsigned long) symoffset); 581 1.19 christos free (alloc_intsym); 582 1.19 christos intsym_buf = NULL; 583 1.19 christos goto out1; 584 1.19 christos } 585 1.19 christos 586 1.19 christos /* PR 33019: Do not accept unsupported binding values - they will 587 1.19 christos likely cause problems later on. */ 588 1.19 christos int bind = ELF_ST_BIND (isym->st_info); 589 1.19 christos if (bind > STB_WEAK && bind < STB_LOOS) 590 1.19 christos { 591 1.19 christos /* xgettext:c-format */ 592 1.19 christos _bfd_error_handler (_("%pB symbol number %lu uses unsupported binding of %u"), 593 1.19 christos ibfd, (unsigned long) (isym - intsym_buf), bind); 594 1.19 christos free (alloc_intsym); 595 1.19 christos intsym_buf = NULL; 596 1.19 christos goto out1; 597 1.19 christos } 598 1.19 christos 599 1.19 christos /* Paranoia: Also refuse to accept the only undefined symbol type: 7. */ 600 1.19 christos int t = ELF_ST_TYPE (isym->st_info); 601 1.19 christos if (t == 7) 602 1.19 christos { 603 1.19 christos /* xgettext:c-format */ 604 1.19 christos _bfd_error_handler (_("%pB symbol number %lu uses unsupported type of %u"), 605 1.19 christos ibfd, (unsigned long) (isym - intsym_buf), t); 606 1.19 christos free (alloc_intsym); 607 1.19 christos intsym_buf = NULL; 608 1.19 christos goto out1; 609 1.19 christos } 610 1.19 christos } 611 1.1 christos 612 1.17 christos out1: 613 1.18 christos _bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size); 614 1.17 christos out2: 615 1.18 christos _bfd_munmap_temporary (alloc_ext, alloc_ext_size); 616 1.1 christos 617 1.1 christos return intsym_buf; 618 1.1 christos } 619 1.1 christos 620 1.1 christos /* Look up a symbol name. */ 621 1.18 christos static const char * 622 1.18 christos bfd_elf_sym_name_raw (bfd *abfd, 623 1.18 christos Elf_Internal_Shdr *symtab_hdr, 624 1.18 christos Elf_Internal_Sym *isym) 625 1.1 christos { 626 1.1 christos unsigned int iname = isym->st_name; 627 1.1 christos unsigned int shindex = symtab_hdr->sh_link; 628 1.1 christos 629 1.1 christos if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION 630 1.1 christos /* Check for a bogus st_shndx to avoid crashing. */ 631 1.1 christos && isym->st_shndx < elf_numsections (abfd)) 632 1.1 christos { 633 1.1 christos iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; 634 1.1 christos shindex = elf_elfheader (abfd)->e_shstrndx; 635 1.1 christos } 636 1.1 christos 637 1.18 christos return bfd_elf_string_from_elf_section (abfd, shindex, iname); 638 1.18 christos } 639 1.18 christos 640 1.18 christos const char * 641 1.18 christos bfd_elf_sym_name (bfd *abfd, 642 1.18 christos Elf_Internal_Shdr *symtab_hdr, 643 1.18 christos Elf_Internal_Sym *isym, 644 1.18 christos asection *sym_sec) 645 1.18 christos { 646 1.18 christos const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym); 647 1.1 christos if (name == NULL) 648 1.18 christos name = bfd_symbol_error_name; 649 1.1 christos else if (sym_sec && *name == '\0') 650 1.12 christos name = bfd_section_name (sym_sec); 651 1.1 christos 652 1.1 christos return name; 653 1.1 christos } 654 1.1 christos 655 1.1 christos /* Return the name of the group signature symbol. Why isn't the 656 1.1 christos signature just a string? */ 657 1.1 christos 658 1.1 christos static const char * 659 1.1 christos group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) 660 1.1 christos { 661 1.1 christos Elf_Internal_Shdr *hdr; 662 1.1 christos unsigned char esym[sizeof (Elf64_External_Sym)]; 663 1.1 christos Elf_External_Sym_Shndx eshndx; 664 1.1 christos Elf_Internal_Sym isym; 665 1.1 christos 666 1.1 christos /* First we need to ensure the symbol table is available. Make sure 667 1.1 christos that it is a symbol table section. */ 668 1.1 christos if (ghdr->sh_link >= elf_numsections (abfd)) 669 1.1 christos return NULL; 670 1.1 christos hdr = elf_elfsections (abfd) [ghdr->sh_link]; 671 1.1 christos if (hdr->sh_type != SHT_SYMTAB 672 1.1 christos || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) 673 1.1 christos return NULL; 674 1.1 christos 675 1.1 christos /* Go read the symbol. */ 676 1.1 christos hdr = &elf_tdata (abfd)->symtab_hdr; 677 1.1 christos if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, 678 1.1 christos &isym, esym, &eshndx) == NULL) 679 1.1 christos return NULL; 680 1.1 christos 681 1.18 christos return bfd_elf_sym_name_raw (abfd, hdr, &isym); 682 1.1 christos } 683 1.1 christos 684 1.14 christos static bool 685 1.18 christos is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize) 686 1.1 christos { 687 1.18 christos return (shdr->sh_size >= minsize 688 1.18 christos && shdr->sh_entsize == GRP_ENTRY_SIZE 689 1.18 christos && shdr->sh_size % GRP_ENTRY_SIZE == 0 690 1.18 christos && shdr->bfd_section != NULL); 691 1.18 christos } 692 1.1 christos 693 1.1 christos 694 1.18 christos /* Set next_in_group, sec_group list pointers, and group names. */ 695 1.1 christos 696 1.18 christos static bool 697 1.18 christos process_sht_group_entries (bfd *abfd, 698 1.18 christos Elf_Internal_Shdr *ghdr, unsigned int gidx) 699 1.18 christos { 700 1.18 christos unsigned char *contents; 701 1.1 christos 702 1.18 christos /* Read the raw contents. */ 703 1.18 christos if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents)) 704 1.18 christos { 705 1.18 christos _bfd_error_handler 706 1.18 christos /* xgettext:c-format */ 707 1.18 christos (_("%pB: could not read contents of group [%u]"), abfd, gidx); 708 1.18 christos return false; 709 1.18 christos } 710 1.1 christos 711 1.18 christos asection *last_elt = NULL; 712 1.18 christos const char *gname = NULL; 713 1.18 christos unsigned char *p = contents + ghdr->sh_size; 714 1.18 christos while (1) 715 1.18 christos { 716 1.18 christos unsigned int idx; 717 1.18 christos Elf_Internal_Shdr *shdr; 718 1.18 christos asection *elt; 719 1.18 christos 720 1.18 christos p -= 4; 721 1.18 christos idx = H_GET_32 (abfd, p); 722 1.18 christos if (p == contents) 723 1.18 christos { 724 1.18 christos if ((idx & GRP_COMDAT) != 0) 725 1.18 christos ghdr->bfd_section->flags 726 1.18 christos |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; 727 1.18 christos break; 728 1.1 christos } 729 1.1 christos 730 1.18 christos if (idx == 0 731 1.18 christos || idx >= elf_numsections (abfd) 732 1.18 christos || (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP 733 1.18 christos || ((elt = shdr->bfd_section) != NULL 734 1.18 christos && elf_sec_group (elt) != NULL 735 1.18 christos && elf_sec_group (elt) != ghdr->bfd_section)) 736 1.1 christos { 737 1.18 christos _bfd_error_handler 738 1.18 christos (_("%pB: invalid entry (%#x) in group [%u]"), 739 1.18 christos abfd, idx, gidx); 740 1.18 christos continue; 741 1.1 christos } 742 1.1 christos 743 1.18 christos /* PR binutils/23199: According to the ELF gABI all sections in 744 1.18 christos a group must be marked with SHF_GROUP, but some tools 745 1.18 christos generate broken objects. Fix them up here. */ 746 1.18 christos shdr->sh_flags |= SHF_GROUP; 747 1.1 christos 748 1.18 christos if (elt == NULL) 749 1.18 christos { 750 1.18 christos if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL) 751 1.1 christos { 752 1.18 christos const char *name = bfd_elf_string_from_elf_section 753 1.18 christos (abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name); 754 1.1 christos 755 1.18 christos _bfd_error_handler 756 1.18 christos /* xgettext:c-format */ 757 1.18 christos (_("%pB: unexpected type (%#x) section `%s' in group [%u]"), 758 1.18 christos abfd, shdr->sh_type, name, gidx); 759 1.5 christos } 760 1.18 christos continue; 761 1.1 christos } 762 1.1 christos 763 1.18 christos /* Don't try to add a section to elf_next_in_group list twice. */ 764 1.18 christos if (elf_sec_group (elt) != NULL) 765 1.18 christos continue; 766 1.1 christos 767 1.18 christos if (last_elt == NULL) 768 1.1 christos { 769 1.18 christos /* Start a circular list with one element. 770 1.18 christos It will be in reverse order to match what gas does. */ 771 1.18 christos elf_next_in_group (elt) = elt; 772 1.18 christos /* Point the group section to it. */ 773 1.18 christos elf_next_in_group (ghdr->bfd_section) = elt; 774 1.18 christos gname = group_signature (abfd, ghdr); 775 1.18 christos if (gname == NULL) 776 1.11 christos { 777 1.18 christos free (contents); 778 1.14 christos return false; 779 1.11 christos } 780 1.1 christos } 781 1.18 christos else 782 1.18 christos { 783 1.18 christos elf_next_in_group (elt) = elf_next_in_group (last_elt); 784 1.18 christos elf_next_in_group (last_elt) = elt; 785 1.18 christos } 786 1.18 christos last_elt = elt; 787 1.18 christos elf_group_name (elt) = gname; 788 1.18 christos elf_sec_group (elt) = ghdr->bfd_section; 789 1.1 christos } 790 1.1 christos 791 1.18 christos free (contents); 792 1.14 christos return true; 793 1.1 christos } 794 1.1 christos 795 1.14 christos bool 796 1.1 christos _bfd_elf_setup_sections (bfd *abfd) 797 1.1 christos { 798 1.14 christos bool result = true; 799 1.1 christos 800 1.1 christos /* Process SHF_LINK_ORDER. */ 801 1.18 christos for (asection *s = abfd->sections; s != NULL; s = s->next) 802 1.1 christos { 803 1.1 christos Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; 804 1.1 christos if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) 805 1.1 christos { 806 1.1 christos unsigned int elfsec = this_hdr->sh_link; 807 1.14 christos /* An sh_link value of 0 is now allowed. It indicates that linked 808 1.14 christos to section has already been discarded, but that the current 809 1.14 christos section has been retained for some other reason. This linking 810 1.14 christos section is still a candidate for later garbage collection 811 1.14 christos however. */ 812 1.1 christos if (elfsec == 0) 813 1.1 christos { 814 1.14 christos elf_linked_to_section (s) = NULL; 815 1.1 christos } 816 1.1 christos else 817 1.1 christos { 818 1.1 christos asection *linksec = NULL; 819 1.1 christos 820 1.1 christos if (elfsec < elf_numsections (abfd)) 821 1.1 christos { 822 1.1 christos this_hdr = elf_elfsections (abfd)[elfsec]; 823 1.1 christos linksec = this_hdr->bfd_section; 824 1.1 christos } 825 1.1 christos 826 1.1 christos /* PR 1991, 2008: 827 1.1 christos Some strip/objcopy may leave an incorrect value in 828 1.1 christos sh_link. We don't want to proceed. */ 829 1.1 christos if (linksec == NULL) 830 1.1 christos { 831 1.9 christos _bfd_error_handler 832 1.9 christos /* xgettext:c-format */ 833 1.11 christos (_("%pB: sh_link [%d] in section `%pA' is incorrect"), 834 1.9 christos s->owner, elfsec, s); 835 1.14 christos result = false; 836 1.1 christos } 837 1.1 christos 838 1.1 christos elf_linked_to_section (s) = linksec; 839 1.1 christos } 840 1.1 christos } 841 1.1 christos } 842 1.1 christos 843 1.1 christos /* Process section groups. */ 844 1.18 christos for (unsigned int i = 1; i < elf_numsections (abfd); i++) 845 1.1 christos { 846 1.18 christos Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; 847 1.1 christos 848 1.18 christos if (shdr && shdr->sh_type == SHT_GROUP) 849 1.11 christos { 850 1.18 christos if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE)) 851 1.18 christos { 852 1.18 christos if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE 853 1.18 christos && !process_sht_group_entries (abfd, shdr, i)) 854 1.18 christos result = false; 855 1.18 christos } 856 1.18 christos else 857 1.11 christos { 858 1.18 christos /* PR binutils/18758: Beware of corrupt binaries with 859 1.18 christos invalid group data. */ 860 1.11 christos _bfd_error_handler 861 1.11 christos /* xgettext:c-format */ 862 1.18 christos (_("%pB: section group entry number %u is corrupt"), abfd, i); 863 1.14 christos result = false; 864 1.11 christos } 865 1.11 christos } 866 1.1 christos } 867 1.11 christos 868 1.1 christos return result; 869 1.1 christos } 870 1.1 christos 871 1.14 christos bool 872 1.1 christos bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) 873 1.1 christos { 874 1.1 christos return elf_next_in_group (sec) != NULL; 875 1.1 christos } 876 1.1 christos 877 1.12 christos const char * 878 1.12 christos bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) 879 1.12 christos { 880 1.12 christos if (elf_sec_group (sec) != NULL) 881 1.12 christos return elf_group_name (sec); 882 1.12 christos return NULL; 883 1.12 christos } 884 1.12 christos 885 1.1 christos /* Make a BFD section from an ELF section. We store a pointer to the 886 1.1 christos BFD section in the bfd_section field of the header. */ 887 1.1 christos 888 1.14 christos bool 889 1.1 christos _bfd_elf_make_section_from_shdr (bfd *abfd, 890 1.1 christos Elf_Internal_Shdr *hdr, 891 1.1 christos const char *name, 892 1.1 christos int shindex) 893 1.1 christos { 894 1.1 christos asection *newsect; 895 1.1 christos flagword flags; 896 1.1 christos const struct elf_backend_data *bed; 897 1.12 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL); 898 1.1 christos 899 1.1 christos if (hdr->bfd_section != NULL) 900 1.14 christos return true; 901 1.1 christos 902 1.1 christos newsect = bfd_make_section_anyway (abfd, name); 903 1.1 christos if (newsect == NULL) 904 1.14 christos return false; 905 1.1 christos 906 1.1 christos hdr->bfd_section = newsect; 907 1.1 christos elf_section_data (newsect)->this_hdr = *hdr; 908 1.1 christos elf_section_data (newsect)->this_idx = shindex; 909 1.1 christos 910 1.1 christos /* Always use the real type/flags. */ 911 1.1 christos elf_section_type (newsect) = hdr->sh_type; 912 1.1 christos elf_section_flags (newsect) = hdr->sh_flags; 913 1.1 christos 914 1.1 christos newsect->filepos = hdr->sh_offset; 915 1.1 christos 916 1.1 christos flags = SEC_NO_FLAGS; 917 1.1 christos if (hdr->sh_type != SHT_NOBITS) 918 1.1 christos flags |= SEC_HAS_CONTENTS; 919 1.1 christos if (hdr->sh_type == SHT_GROUP) 920 1.11 christos flags |= SEC_GROUP; 921 1.1 christos if ((hdr->sh_flags & SHF_ALLOC) != 0) 922 1.1 christos { 923 1.1 christos flags |= SEC_ALLOC; 924 1.1 christos if (hdr->sh_type != SHT_NOBITS) 925 1.1 christos flags |= SEC_LOAD; 926 1.1 christos } 927 1.1 christos if ((hdr->sh_flags & SHF_WRITE) == 0) 928 1.1 christos flags |= SEC_READONLY; 929 1.1 christos if ((hdr->sh_flags & SHF_EXECINSTR) != 0) 930 1.1 christos flags |= SEC_CODE; 931 1.1 christos else if ((flags & SEC_LOAD) != 0) 932 1.1 christos flags |= SEC_DATA; 933 1.1 christos if ((hdr->sh_flags & SHF_MERGE) != 0) 934 1.19 christos flags |= SEC_MERGE; 935 1.8 christos if ((hdr->sh_flags & SHF_STRINGS) != 0) 936 1.19 christos flags |= SEC_STRINGS; 937 1.1 christos if ((hdr->sh_flags & SHF_TLS) != 0) 938 1.1 christos flags |= SEC_THREAD_LOCAL; 939 1.1 christos if ((hdr->sh_flags & SHF_EXCLUDE) != 0) 940 1.1 christos flags |= SEC_EXCLUDE; 941 1.1 christos 942 1.19 christos newsect->entsize = hdr->sh_entsize; 943 1.19 christos 944 1.12 christos switch (elf_elfheader (abfd)->e_ident[EI_OSABI]) 945 1.12 christos { 946 1.12 christos /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE, 947 1.12 christos but binutils as of 2019-07-23 did not set the EI_OSABI header 948 1.12 christos byte. */ 949 1.12 christos case ELFOSABI_GNU: 950 1.12 christos case ELFOSABI_FREEBSD: 951 1.14 christos if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0) 952 1.14 christos elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain; 953 1.14 christos /* Fall through */ 954 1.14 christos case ELFOSABI_NONE: 955 1.12 christos if ((hdr->sh_flags & SHF_GNU_MBIND) != 0) 956 1.12 christos elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind; 957 1.12 christos break; 958 1.12 christos } 959 1.12 christos 960 1.19 christos if ((flags & (SEC_ALLOC | SEC_GROUP)) == 0) 961 1.1 christos { 962 1.1 christos /* The debugging sections appear to be recognized only by name, 963 1.1 christos not any sort of flag. Their SEC_ALLOC bits are cleared. */ 964 1.1 christos if (name [0] == '.') 965 1.1 christos { 966 1.14 christos if (startswith (name, ".debug") 967 1.14 christos || startswith (name, ".gnu.debuglto_.debug_") 968 1.14 christos || startswith (name, ".gnu.linkonce.wi.") 969 1.14 christos || startswith (name, ".zdebug")) 970 1.12 christos flags |= SEC_DEBUGGING | SEC_ELF_OCTETS; 971 1.14 christos else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME) 972 1.14 christos || startswith (name, ".note.gnu")) 973 1.12 christos { 974 1.12 christos flags |= SEC_ELF_OCTETS; 975 1.12 christos opb = 1; 976 1.12 christos } 977 1.14 christos else if (startswith (name, ".line") 978 1.14 christos || startswith (name, ".stab") 979 1.19 christos || streq (name, ".gdb_index")) 980 1.1 christos flags |= SEC_DEBUGGING; 981 1.1 christos } 982 1.1 christos } 983 1.1 christos 984 1.12 christos if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb) 985 1.12 christos || !bfd_set_section_size (newsect, hdr->sh_size) 986 1.14 christos || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign 987 1.14 christos & -hdr->sh_addralign))) 988 1.14 christos return false; 989 1.12 christos 990 1.1 christos /* As a GNU extension, if the name begins with .gnu.linkonce, we 991 1.1 christos only link a single copy of the section. This is used to support 992 1.1 christos g++. g++ will emit each template expansion in its own section. 993 1.1 christos The symbols will be defined as weak, so that multiple definitions 994 1.1 christos are permitted. The GNU linker extension is to actually discard 995 1.1 christos all but one of the sections. */ 996 1.14 christos if (startswith (name, ".gnu.linkonce") 997 1.1 christos && elf_next_in_group (newsect) == NULL) 998 1.1 christos flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; 999 1.1 christos 1000 1.12 christos if (!bfd_set_section_flags (newsect, flags)) 1001 1.14 christos return false; 1002 1.12 christos 1003 1.1 christos bed = get_elf_backend_data (abfd); 1004 1.1 christos if (bed->elf_backend_section_flags) 1005 1.12 christos if (!bed->elf_backend_section_flags (hdr)) 1006 1.14 christos return false; 1007 1.1 christos 1008 1.1 christos /* We do not parse the PT_NOTE segments as we are interested even in the 1009 1.1 christos separate debug info files which may have the segments offsets corrupted. 1010 1.1 christos PT_NOTEs from the core files are currently not parsed using BFD. */ 1011 1.14 christos if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0) 1012 1.1 christos { 1013 1.1 christos bfd_byte *contents; 1014 1.1 christos 1015 1.17 christos if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents)) 1016 1.14 christos return false; 1017 1.1 christos 1018 1.11 christos elf_parse_notes (abfd, (char *) contents, hdr->sh_size, 1019 1.11 christos hdr->sh_offset, hdr->sh_addralign); 1020 1.17 christos _bfd_elf_munmap_section_contents (newsect, contents); 1021 1.1 christos } 1022 1.1 christos 1023 1.12 christos if ((newsect->flags & SEC_ALLOC) != 0) 1024 1.1 christos { 1025 1.1 christos Elf_Internal_Phdr *phdr; 1026 1.1 christos unsigned int i, nload; 1027 1.1 christos 1028 1.1 christos /* Some ELF linkers produce binaries with all the program header 1029 1.1 christos p_paddr fields zero. If we have such a binary with more than 1030 1.1 christos one PT_LOAD header, then leave the section lma equal to vma 1031 1.1 christos so that we don't create sections with overlapping lma. */ 1032 1.1 christos phdr = elf_tdata (abfd)->phdr; 1033 1.1 christos for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) 1034 1.1 christos if (phdr->p_paddr != 0) 1035 1.1 christos break; 1036 1.1 christos else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) 1037 1.1 christos ++nload; 1038 1.1 christos if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) 1039 1.14 christos return true; 1040 1.1 christos 1041 1.1 christos phdr = elf_tdata (abfd)->phdr; 1042 1.1 christos for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) 1043 1.1 christos { 1044 1.3 christos if (((phdr->p_type == PT_LOAD 1045 1.3 christos && (hdr->sh_flags & SHF_TLS) == 0) 1046 1.3 christos || phdr->p_type == PT_TLS) 1047 1.1 christos && ELF_SECTION_IN_SEGMENT (hdr, phdr)) 1048 1.1 christos { 1049 1.12 christos if ((newsect->flags & SEC_LOAD) == 0) 1050 1.1 christos newsect->lma = (phdr->p_paddr 1051 1.12 christos + hdr->sh_addr - phdr->p_vaddr) / opb; 1052 1.1 christos else 1053 1.1 christos /* We used to use the same adjustment for SEC_LOAD 1054 1.1 christos sections, but that doesn't work if the segment 1055 1.1 christos is packed with code from multiple VMAs. 1056 1.1 christos Instead we calculate the section LMA based on 1057 1.1 christos the segment LMA. It is assumed that the 1058 1.1 christos segment will contain sections with contiguous 1059 1.1 christos LMAs, even if the VMAs are not. */ 1060 1.1 christos newsect->lma = (phdr->p_paddr 1061 1.12 christos + hdr->sh_offset - phdr->p_offset) / opb; 1062 1.1 christos 1063 1.1 christos /* With contiguous segments, we can't tell from file 1064 1.1 christos offsets whether a section with zero size should 1065 1.1 christos be placed at the end of one segment or the 1066 1.1 christos beginning of the next. Decide based on vaddr. */ 1067 1.1 christos if (hdr->sh_addr >= phdr->p_vaddr 1068 1.1 christos && (hdr->sh_addr + hdr->sh_size 1069 1.1 christos <= phdr->p_vaddr + phdr->p_memsz)) 1070 1.1 christos break; 1071 1.1 christos } 1072 1.1 christos } 1073 1.1 christos } 1074 1.1 christos 1075 1.14 christos /* Compress/decompress DWARF debug sections with names: .debug_*, 1076 1.14 christos .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */ 1077 1.14 christos if ((newsect->flags & SEC_DEBUGGING) != 0 1078 1.14 christos && (newsect->flags & SEC_HAS_CONTENTS) != 0 1079 1.14 christos && (newsect->flags & SEC_ELF_OCTETS) != 0) 1080 1.1 christos { 1081 1.1 christos enum { nothing, compress, decompress } action = nothing; 1082 1.6 christos int compression_header_size; 1083 1.6 christos bfd_size_type uncompressed_size; 1084 1.11 christos unsigned int uncompressed_align_power; 1085 1.14 christos enum compression_type ch_type = ch_none; 1086 1.14 christos bool compressed 1087 1.14 christos = bfd_is_section_compressed_info (abfd, newsect, 1088 1.14 christos &compression_header_size, 1089 1.14 christos &uncompressed_size, 1090 1.14 christos &uncompressed_align_power, 1091 1.14 christos &ch_type); 1092 1.14 christos 1093 1.14 christos /* Should we decompress? */ 1094 1.14 christos if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed) 1095 1.14 christos action = decompress; 1096 1.14 christos 1097 1.14 christos /* Should we compress? Or convert to a different compression? */ 1098 1.14 christos else if ((abfd->flags & BFD_COMPRESS) != 0 1099 1.14 christos && newsect->size != 0 1100 1.14 christos && compression_header_size >= 0 1101 1.14 christos && uncompressed_size > 0) 1102 1.14 christos { 1103 1.14 christos if (!compressed) 1104 1.1 christos action = compress; 1105 1.6 christos else 1106 1.14 christos { 1107 1.14 christos enum compression_type new_ch_type = ch_none; 1108 1.14 christos if ((abfd->flags & BFD_COMPRESS_GABI) != 0) 1109 1.14 christos new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0 1110 1.14 christos ? ch_compress_zstd : ch_compress_zlib); 1111 1.14 christos if (new_ch_type != ch_type) 1112 1.14 christos action = compress; 1113 1.14 christos } 1114 1.1 christos } 1115 1.1 christos 1116 1.6 christos if (action == compress) 1117 1.1 christos { 1118 1.1 christos if (!bfd_init_section_compress_status (abfd, newsect)) 1119 1.1 christos { 1120 1.9 christos _bfd_error_handler 1121 1.9 christos /* xgettext:c-format */ 1122 1.14 christos (_("%pB: unable to compress section %s"), abfd, name); 1123 1.14 christos return false; 1124 1.1 christos } 1125 1.6 christos } 1126 1.14 christos else if (action == decompress) 1127 1.6 christos { 1128 1.1 christos if (!bfd_init_section_decompress_status (abfd, newsect)) 1129 1.1 christos { 1130 1.9 christos _bfd_error_handler 1131 1.9 christos /* xgettext:c-format */ 1132 1.14 christos (_("%pB: unable to decompress section %s"), abfd, name); 1133 1.14 christos return false; 1134 1.1 christos } 1135 1.14 christos #ifndef HAVE_ZSTD 1136 1.14 christos if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD) 1137 1.6 christos { 1138 1.14 christos _bfd_error_handler 1139 1.14 christos /* xgettext:c-format */ 1140 1.14 christos (_ ("%pB: section %s is compressed with zstd, but BFD " 1141 1.14 christos "is not built with zstd support"), 1142 1.14 christos abfd, name); 1143 1.14 christos newsect->compress_status = COMPRESS_SECTION_NONE; 1144 1.14 christos return false; 1145 1.14 christos } 1146 1.14 christos #endif 1147 1.14 christos if (abfd->is_linker_input 1148 1.14 christos && name[1] == 'z') 1149 1.14 christos { 1150 1.14 christos /* Rename section from .zdebug_* to .debug_* so that ld 1151 1.14 christos scripts will see this section as a debug section. */ 1152 1.14 christos char *new_name = bfd_zdebug_name_to_debug (abfd, name); 1153 1.1 christos if (new_name == NULL) 1154 1.14 christos return false; 1155 1.12 christos bfd_rename_section (newsect, new_name); 1156 1.1 christos } 1157 1.1 christos } 1158 1.1 christos } 1159 1.1 christos 1160 1.14 christos return true; 1161 1.1 christos } 1162 1.1 christos 1163 1.8 christos const char *const bfd_elf_section_type_names[] = 1164 1.8 christos { 1165 1.1 christos "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", 1166 1.1 christos "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", 1167 1.1 christos "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", 1168 1.1 christos }; 1169 1.1 christos 1170 1.1 christos /* ELF relocs are against symbols. If we are producing relocatable 1171 1.1 christos output, and the reloc is against an external symbol, and nothing 1172 1.1 christos has given us any additional addend, the resulting reloc will also 1173 1.1 christos be against the same symbol. In such a case, we don't want to 1174 1.1 christos change anything about the way the reloc is handled, since it will 1175 1.1 christos all be done at final link time. Rather than put special case code 1176 1.1 christos into bfd_perform_relocation, all the reloc types use this howto 1177 1.14 christos function, or should call this function for relocatable output. */ 1178 1.1 christos 1179 1.1 christos bfd_reloc_status_type 1180 1.1 christos bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, 1181 1.1 christos arelent *reloc_entry, 1182 1.1 christos asymbol *symbol, 1183 1.1 christos void *data ATTRIBUTE_UNUSED, 1184 1.1 christos asection *input_section, 1185 1.1 christos bfd *output_bfd, 1186 1.1 christos char **error_message ATTRIBUTE_UNUSED) 1187 1.1 christos { 1188 1.1 christos if (output_bfd != NULL 1189 1.1 christos && (symbol->flags & BSF_SECTION_SYM) == 0 1190 1.1 christos && (! reloc_entry->howto->partial_inplace 1191 1.1 christos || reloc_entry->addend == 0)) 1192 1.1 christos { 1193 1.1 christos reloc_entry->address += input_section->output_offset; 1194 1.1 christos return bfd_reloc_ok; 1195 1.1 christos } 1196 1.1 christos 1197 1.14 christos /* In some cases the relocation should be treated as output section 1198 1.14 christos relative, as when linking ELF DWARF into PE COFF. Many ELF 1199 1.14 christos targets lack section relative relocations and instead use 1200 1.14 christos ordinary absolute relocations for references between DWARF 1201 1.14 christos sections. That is arguably a bug in those targets but it happens 1202 1.14 christos to work for the usual case of linking to non-loaded ELF debug 1203 1.14 christos sections with VMAs forced to zero. PE COFF on the other hand 1204 1.14 christos doesn't allow a section VMA of zero. */ 1205 1.14 christos if (output_bfd == NULL 1206 1.14 christos && !reloc_entry->howto->pc_relative 1207 1.14 christos && (symbol->section->flags & SEC_DEBUGGING) != 0 1208 1.14 christos && (input_section->flags & SEC_DEBUGGING) != 0) 1209 1.14 christos reloc_entry->addend -= symbol->section->output_section->vma; 1210 1.14 christos 1211 1.1 christos return bfd_reloc_continue; 1212 1.1 christos } 1213 1.1 christos 1214 1.8 christos /* Returns TRUE if section A matches section B. 1216 1.8 christos Names, addresses and links may be different, but everything else 1217 1.8 christos should be the same. */ 1218 1.14 christos 1219 1.8 christos static bool 1220 1.8 christos section_match (const Elf_Internal_Shdr * a, 1221 1.8 christos const Elf_Internal_Shdr * b) 1222 1.11 christos { 1223 1.11 christos if (a->sh_type != b->sh_type 1224 1.11 christos || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0 1225 1.11 christos || a->sh_addralign != b->sh_addralign 1226 1.14 christos || a->sh_entsize != b->sh_entsize) 1227 1.11 christos return false; 1228 1.11 christos if (a->sh_type == SHT_SYMTAB 1229 1.14 christos || a->sh_type == SHT_STRTAB) 1230 1.11 christos return true; 1231 1.8 christos return a->sh_size == b->sh_size; 1232 1.8 christos } 1233 1.8 christos 1234 1.8 christos /* Find a section in OBFD that has the same characteristics 1235 1.8 christos as IHEADER. Return the index of this section or SHN_UNDEF if 1236 1.8 christos none can be found. Check's section HINT first, as this is likely 1237 1.8 christos to be the correct section. */ 1238 1.8 christos 1239 1.11 christos static unsigned int 1240 1.11 christos find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader, 1241 1.8 christos const unsigned int hint) 1242 1.8 christos { 1243 1.8 christos Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); 1244 1.8 christos unsigned int i; 1245 1.9 christos 1246 1.9 christos BFD_ASSERT (iheader != NULL); 1247 1.9 christos 1248 1.11 christos /* See PR 20922 for a reproducer of the NULL test. */ 1249 1.11 christos if (hint < elf_numsections (obfd) 1250 1.9 christos && oheaders[hint] != NULL 1251 1.8 christos && section_match (oheaders[hint], iheader)) 1252 1.8 christos return hint; 1253 1.8 christos 1254 1.8 christos for (i = 1; i < elf_numsections (obfd); i++) 1255 1.8 christos { 1256 1.8 christos Elf_Internal_Shdr * oheader = oheaders[i]; 1257 1.9 christos 1258 1.9 christos if (oheader == NULL) 1259 1.8 christos continue; 1260 1.8 christos if (section_match (oheader, iheader)) 1261 1.8 christos /* FIXME: Do we care if there is a potential for 1262 1.8 christos multiple matches ? */ 1263 1.8 christos return i; 1264 1.8 christos } 1265 1.8 christos 1266 1.8 christos return SHN_UNDEF; 1267 1.8 christos } 1268 1.8 christos 1269 1.8 christos /* PR 19938: Attempt to set the ELF section header fields of an OS or 1270 1.8 christos Processor specific section, based upon a matching input section. 1271 1.11 christos Returns TRUE upon success, FALSE otherwise. */ 1272 1.14 christos 1273 1.8 christos static bool 1274 1.8 christos copy_special_section_fields (const bfd *ibfd, 1275 1.8 christos bfd *obfd, 1276 1.8 christos const Elf_Internal_Shdr *iheader, 1277 1.8 christos Elf_Internal_Shdr *oheader, 1278 1.8 christos const unsigned int secnum) 1279 1.8 christos { 1280 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (obfd); 1281 1.14 christos const Elf_Internal_Shdr **iheaders 1282 1.14 christos = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); 1283 1.8 christos bool changed = false; 1284 1.8 christos unsigned int sh_link; 1285 1.8 christos 1286 1.8 christos if (oheader->sh_type == SHT_NOBITS) 1287 1.8 christos { 1288 1.8 christos /* This is a feature for objcopy --only-keep-debug: 1289 1.8 christos When a section's type is changed to NOBITS, we preserve 1290 1.8 christos the sh_link and sh_info fields so that they can be 1291 1.8 christos matched up with the original. 1292 1.8 christos 1293 1.8 christos Note: Strictly speaking these assignments are wrong. 1294 1.8 christos The sh_link and sh_info fields should point to the 1295 1.8 christos relevent sections in the output BFD, which may not be in 1296 1.8 christos the same location as they were in the input BFD. But 1297 1.8 christos the whole point of this action is to preserve the 1298 1.8 christos original values of the sh_link and sh_info fields, so 1299 1.8 christos that they can be matched up with the section headers in 1300 1.8 christos the original file. So strictly speaking we may be 1301 1.8 christos creating an invalid ELF file, but it is only for a file 1302 1.8 christos that just contains debug info and only for sections 1303 1.8 christos without any contents. */ 1304 1.8 christos if (oheader->sh_link == 0) 1305 1.8 christos oheader->sh_link = iheader->sh_link; 1306 1.8 christos if (oheader->sh_info == 0) 1307 1.14 christos oheader->sh_info = iheader->sh_info; 1308 1.8 christos return true; 1309 1.8 christos } 1310 1.8 christos 1311 1.12 christos /* Allow the target a chance to decide how these fields should be set. */ 1312 1.12 christos if (bed->elf_backend_copy_special_section_fields (ibfd, obfd, 1313 1.14 christos iheader, oheader)) 1314 1.8 christos return true; 1315 1.8 christos 1316 1.8 christos /* We have an iheader which might match oheader, and which has non-zero 1317 1.8 christos sh_info and/or sh_link fields. Attempt to follow those links and find 1318 1.8 christos the section in the output bfd which corresponds to the linked section 1319 1.8 christos in the input bfd. */ 1320 1.8 christos if (iheader->sh_link != SHN_UNDEF) 1321 1.9 christos { 1322 1.9 christos /* See PR 20931 for a reproducer. */ 1323 1.9 christos if (iheader->sh_link >= elf_numsections (ibfd)) 1324 1.11 christos { 1325 1.9 christos _bfd_error_handler 1326 1.11 christos /* xgettext:c-format */ 1327 1.9 christos (_("%pB: invalid sh_link field (%d) in section number %d"), 1328 1.14 christos ibfd, iheader->sh_link, secnum); 1329 1.9 christos return false; 1330 1.9 christos } 1331 1.8 christos 1332 1.8 christos sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); 1333 1.8 christos if (sh_link != SHN_UNDEF) 1334 1.8 christos { 1335 1.14 christos oheader->sh_link = sh_link; 1336 1.8 christos changed = true; 1337 1.8 christos } 1338 1.8 christos else 1339 1.8 christos /* FIXME: Should we install iheader->sh_link 1340 1.11 christos if we could not find a match ? */ 1341 1.9 christos _bfd_error_handler 1342 1.11 christos /* xgettext:c-format */ 1343 1.8 christos (_("%pB: failed to find link section for section %d"), obfd, secnum); 1344 1.8 christos } 1345 1.8 christos 1346 1.8 christos if (iheader->sh_info) 1347 1.8 christos { 1348 1.8 christos /* The sh_info field can hold arbitrary information, but if the 1349 1.8 christos SHF_LINK_INFO flag is set then it should be interpreted as a 1350 1.8 christos section index. */ 1351 1.8 christos if (iheader->sh_flags & SHF_INFO_LINK) 1352 1.8 christos { 1353 1.8 christos sh_link = find_link (obfd, iheaders[iheader->sh_info], 1354 1.8 christos iheader->sh_info); 1355 1.8 christos if (sh_link != SHN_UNDEF) 1356 1.8 christos oheader->sh_flags |= SHF_INFO_LINK; 1357 1.8 christos } 1358 1.8 christos else 1359 1.8 christos /* No idea what it means - just copy it. */ 1360 1.8 christos sh_link = iheader->sh_info; 1361 1.8 christos 1362 1.8 christos if (sh_link != SHN_UNDEF) 1363 1.8 christos { 1364 1.14 christos oheader->sh_info = sh_link; 1365 1.8 christos changed = true; 1366 1.8 christos } 1367 1.11 christos else 1368 1.9 christos _bfd_error_handler 1369 1.11 christos /* xgettext:c-format */ 1370 1.8 christos (_("%pB: failed to find info section for section %d"), obfd, secnum); 1371 1.8 christos } 1372 1.8 christos 1373 1.8 christos return changed; 1374 1.11 christos } 1375 1.1 christos 1376 1.1 christos /* Copy the program header and other data from one object module to 1377 1.1 christos another. */ 1378 1.14 christos 1379 1.1 christos bool 1380 1.1 christos _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) 1381 1.14 christos { 1382 1.14 christos const Elf_Internal_Shdr **iheaders 1383 1.8 christos = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); 1384 1.8 christos Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); 1385 1.8 christos const struct elf_backend_data *bed; 1386 1.8 christos unsigned int i; 1387 1.1 christos 1388 1.8 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 1389 1.14 christos || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 1390 1.1 christos return true; 1391 1.4 christos 1392 1.4 christos if (!elf_flags_init (obfd)) 1393 1.4 christos { 1394 1.14 christos elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; 1395 1.4 christos elf_flags_init (obfd) = true; 1396 1.1 christos } 1397 1.1 christos 1398 1.4 christos elf_gp (obfd) = elf_gp (ibfd); 1399 1.4 christos 1400 1.4 christos /* Also copy the EI_OSABI field. */ 1401 1.4 christos elf_elfheader (obfd)->e_ident[EI_OSABI] = 1402 1.1 christos elf_elfheader (ibfd)->e_ident[EI_OSABI]; 1403 1.8 christos 1404 1.8 christos /* If set, copy the EI_ABIVERSION field. */ 1405 1.8 christos if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) 1406 1.8 christos elf_elfheader (obfd)->e_ident[EI_ABIVERSION] 1407 1.11 christos = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; 1408 1.1 christos 1409 1.1 christos /* Copy object attributes. */ 1410 1.8 christos _bfd_elf_copy_obj_attributes (ibfd, obfd); 1411 1.8 christos 1412 1.14 christos if (iheaders == NULL || oheaders == NULL) 1413 1.8 christos return true; 1414 1.8 christos 1415 1.8 christos bed = get_elf_backend_data (obfd); 1416 1.8 christos 1417 1.8 christos /* Possibly copy other fields in the section header. */ 1418 1.8 christos for (i = 1; i < elf_numsections (obfd); i++) 1419 1.8 christos { 1420 1.8 christos unsigned int j; 1421 1.8 christos Elf_Internal_Shdr * oheader = oheaders[i]; 1422 1.8 christos 1423 1.8 christos /* Ignore ordinary sections. SHT_NOBITS sections are considered however 1424 1.8 christos because of a special case need for generating separate debug info 1425 1.8 christos files. See below for more details. */ 1426 1.8 christos if (oheader == NULL 1427 1.8 christos || (oheader->sh_type != SHT_NOBITS 1428 1.8 christos && oheader->sh_type < SHT_LOOS)) 1429 1.8 christos continue; 1430 1.8 christos 1431 1.8 christos /* Ignore empty sections, and sections whose 1432 1.8 christos fields have already been initialised. */ 1433 1.8 christos if (oheader->sh_size == 0 1434 1.8 christos || (oheader->sh_info != 0 && oheader->sh_link != 0)) 1435 1.8 christos continue; 1436 1.8 christos 1437 1.14 christos /* Scan for the matching section in the input bfd. 1438 1.14 christos First we try for a direct mapping between the input and 1439 1.8 christos output sections. */ 1440 1.8 christos for (j = 1; j < elf_numsections (ibfd); j++) 1441 1.8 christos { 1442 1.8 christos const Elf_Internal_Shdr * iheader = iheaders[j]; 1443 1.8 christos 1444 1.8 christos if (iheader == NULL) 1445 1.8 christos continue; 1446 1.8 christos 1447 1.8 christos if (oheader->bfd_section != NULL 1448 1.8 christos && iheader->bfd_section != NULL 1449 1.8 christos && iheader->bfd_section->output_section != NULL 1450 1.8 christos && iheader->bfd_section->output_section == oheader->bfd_section) 1451 1.14 christos { 1452 1.14 christos /* We have found a connection from the input section to 1453 1.14 christos the output section. Attempt to copy the header fields. 1454 1.14 christos If this fails then do not try any further sections - 1455 1.14 christos there should only be a one-to-one mapping between 1456 1.14 christos input and output. */ 1457 1.14 christos if (!copy_special_section_fields (ibfd, obfd, 1458 1.8 christos iheader, oheader, i)) 1459 1.8 christos j = elf_numsections (ibfd); 1460 1.8 christos break; 1461 1.8 christos } 1462 1.8 christos } 1463 1.8 christos 1464 1.8 christos if (j < elf_numsections (ibfd)) 1465 1.8 christos continue; 1466 1.8 christos 1467 1.8 christos /* That failed. So try to deduce the corresponding input section. 1468 1.8 christos Unfortunately we cannot compare names as the output string table 1469 1.8 christos is empty, so instead we check size, address and type. */ 1470 1.8 christos for (j = 1; j < elf_numsections (ibfd); j++) 1471 1.8 christos { 1472 1.8 christos const Elf_Internal_Shdr * iheader = iheaders[j]; 1473 1.8 christos 1474 1.8 christos if (iheader == NULL) 1475 1.8 christos continue; 1476 1.8 christos 1477 1.8 christos /* Try matching fields in the input section's header. 1478 1.8 christos Since --only-keep-debug turns all non-debug sections into 1479 1.8 christos SHT_NOBITS sections, the output SHT_NOBITS type matches any 1480 1.8 christos input type. */ 1481 1.8 christos if ((oheader->sh_type == SHT_NOBITS 1482 1.8 christos || iheader->sh_type == oheader->sh_type) 1483 1.8 christos && (iheader->sh_flags & ~ SHF_INFO_LINK) 1484 1.8 christos == (oheader->sh_flags & ~ SHF_INFO_LINK) 1485 1.8 christos && iheader->sh_addralign == oheader->sh_addralign 1486 1.8 christos && iheader->sh_entsize == oheader->sh_entsize 1487 1.8 christos && iheader->sh_size == oheader->sh_size 1488 1.8 christos && iheader->sh_addr == oheader->sh_addr 1489 1.8 christos && (iheader->sh_info != oheader->sh_info 1490 1.8 christos || iheader->sh_link != oheader->sh_link)) 1491 1.8 christos { 1492 1.8 christos if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) 1493 1.8 christos break; 1494 1.8 christos } 1495 1.8 christos } 1496 1.8 christos 1497 1.8 christos if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) 1498 1.8 christos { 1499 1.8 christos /* Final attempt. Call the backend copy function 1500 1.12 christos with a NULL input section. */ 1501 1.12 christos (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd, 1502 1.8 christos NULL, oheader); 1503 1.8 christos } 1504 1.8 christos } 1505 1.14 christos 1506 1.1 christos return true; 1507 1.1 christos } 1508 1.1 christos 1509 1.1 christos static const char * 1510 1.1 christos get_segment_type (unsigned int p_type) 1511 1.1 christos { 1512 1.1 christos const char *pt; 1513 1.1 christos switch (p_type) 1514 1.1 christos { 1515 1.1 christos case PT_NULL: pt = "NULL"; break; 1516 1.1 christos case PT_LOAD: pt = "LOAD"; break; 1517 1.1 christos case PT_DYNAMIC: pt = "DYNAMIC"; break; 1518 1.1 christos case PT_INTERP: pt = "INTERP"; break; 1519 1.1 christos case PT_NOTE: pt = "NOTE"; break; 1520 1.1 christos case PT_SHLIB: pt = "SHLIB"; break; 1521 1.1 christos case PT_PHDR: pt = "PHDR"; break; 1522 1.1 christos case PT_TLS: pt = "TLS"; break; 1523 1.1 christos case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; 1524 1.1 christos case PT_GNU_STACK: pt = "STACK"; break; 1525 1.14 christos case PT_GNU_RELRO: pt = "RELRO"; break; 1526 1.1 christos case PT_GNU_SFRAME: pt = "SFRAME"; break; 1527 1.1 christos default: pt = NULL; break; 1528 1.1 christos } 1529 1.1 christos return pt; 1530 1.1 christos } 1531 1.1 christos 1532 1.1 christos /* Print out the program headers. */ 1533 1.14 christos 1534 1.1 christos bool 1535 1.1 christos _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) 1536 1.1 christos { 1537 1.1 christos FILE *f = (FILE *) farg; 1538 1.1 christos Elf_Internal_Phdr *p; 1539 1.1 christos asection *s; 1540 1.1 christos bfd_byte *dynbuf = NULL; 1541 1.1 christos 1542 1.1 christos p = elf_tdata (abfd)->phdr; 1543 1.1 christos if (p != NULL) 1544 1.1 christos { 1545 1.1 christos unsigned int i, c; 1546 1.1 christos 1547 1.1 christos fprintf (f, _("\nProgram Header:\n")); 1548 1.1 christos c = elf_elfheader (abfd)->e_phnum; 1549 1.1 christos for (i = 0; i < c; i++, p++) 1550 1.1 christos { 1551 1.1 christos const char *pt = get_segment_type (p->p_type); 1552 1.1 christos char buf[20]; 1553 1.1 christos 1554 1.1 christos if (pt == NULL) 1555 1.1 christos { 1556 1.1 christos sprintf (buf, "0x%lx", p->p_type); 1557 1.1 christos pt = buf; 1558 1.1 christos } 1559 1.1 christos fprintf (f, "%8s off 0x", pt); 1560 1.1 christos bfd_fprintf_vma (abfd, f, p->p_offset); 1561 1.1 christos fprintf (f, " vaddr 0x"); 1562 1.1 christos bfd_fprintf_vma (abfd, f, p->p_vaddr); 1563 1.1 christos fprintf (f, " paddr 0x"); 1564 1.1 christos bfd_fprintf_vma (abfd, f, p->p_paddr); 1565 1.1 christos fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); 1566 1.1 christos fprintf (f, " filesz 0x"); 1567 1.1 christos bfd_fprintf_vma (abfd, f, p->p_filesz); 1568 1.1 christos fprintf (f, " memsz 0x"); 1569 1.1 christos bfd_fprintf_vma (abfd, f, p->p_memsz); 1570 1.1 christos fprintf (f, " flags %c%c%c", 1571 1.1 christos (p->p_flags & PF_R) != 0 ? 'r' : '-', 1572 1.1 christos (p->p_flags & PF_W) != 0 ? 'w' : '-', 1573 1.1 christos (p->p_flags & PF_X) != 0 ? 'x' : '-'); 1574 1.1 christos if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) 1575 1.1 christos fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); 1576 1.1 christos fprintf (f, "\n"); 1577 1.1 christos } 1578 1.1 christos } 1579 1.1 christos 1580 1.17 christos s = bfd_get_section_by_name (abfd, ".dynamic"); 1581 1.1 christos if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0) 1582 1.1 christos { 1583 1.1 christos unsigned int elfsec; 1584 1.1 christos unsigned long shlink; 1585 1.1 christos bfd_byte *extdyn, *extdynend; 1586 1.1 christos size_t extdynsize; 1587 1.1 christos void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); 1588 1.1 christos 1589 1.1 christos fprintf (f, _("\nDynamic Section:\n")); 1590 1.17 christos 1591 1.1 christos if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf)) 1592 1.1 christos goto error_return; 1593 1.1 christos 1594 1.1 christos elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 1595 1.1 christos if (elfsec == SHN_BAD) 1596 1.1 christos goto error_return; 1597 1.1 christos shlink = elf_elfsections (abfd)[elfsec]->sh_link; 1598 1.1 christos 1599 1.1 christos extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; 1600 1.1 christos swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; 1601 1.14 christos 1602 1.14 christos for (extdyn = dynbuf, extdynend = dynbuf + s->size; 1603 1.14 christos (size_t) (extdynend - extdyn) >= extdynsize; 1604 1.1 christos extdyn += extdynsize) 1605 1.1 christos { 1606 1.1 christos Elf_Internal_Dyn dyn; 1607 1.1 christos const char *name = ""; 1608 1.14 christos char ab[20]; 1609 1.1 christos bool stringp; 1610 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 1611 1.1 christos 1612 1.1 christos (*swap_dyn_in) (abfd, extdyn, &dyn); 1613 1.1 christos 1614 1.1 christos if (dyn.d_tag == DT_NULL) 1615 1.1 christos break; 1616 1.14 christos 1617 1.1 christos stringp = false; 1618 1.1 christos switch (dyn.d_tag) 1619 1.1 christos { 1620 1.1 christos default: 1621 1.1 christos if (bed->elf_backend_get_target_dtag) 1622 1.1 christos name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); 1623 1.19 christos 1624 1.1 christos if (streq (name, "")) 1625 1.14 christos { 1626 1.1 christos sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag); 1627 1.1 christos name = ab; 1628 1.1 christos } 1629 1.1 christos break; 1630 1.14 christos 1631 1.1 christos case DT_NEEDED: name = "NEEDED"; stringp = true; break; 1632 1.1 christos case DT_PLTRELSZ: name = "PLTRELSZ"; break; 1633 1.1 christos case DT_PLTGOT: name = "PLTGOT"; break; 1634 1.1 christos case DT_HASH: name = "HASH"; break; 1635 1.1 christos case DT_STRTAB: name = "STRTAB"; break; 1636 1.1 christos case DT_SYMTAB: name = "SYMTAB"; break; 1637 1.1 christos case DT_RELA: name = "RELA"; break; 1638 1.1 christos case DT_RELASZ: name = "RELASZ"; break; 1639 1.1 christos case DT_RELAENT: name = "RELAENT"; break; 1640 1.1 christos case DT_STRSZ: name = "STRSZ"; break; 1641 1.1 christos case DT_SYMENT: name = "SYMENT"; break; 1642 1.1 christos case DT_INIT: name = "INIT"; break; 1643 1.14 christos case DT_FINI: name = "FINI"; break; 1644 1.14 christos case DT_SONAME: name = "SONAME"; stringp = true; break; 1645 1.1 christos case DT_RPATH: name = "RPATH"; stringp = true; break; 1646 1.1 christos case DT_SYMBOLIC: name = "SYMBOLIC"; break; 1647 1.1 christos case DT_REL: name = "REL"; break; 1648 1.1 christos case DT_RELSZ: name = "RELSZ"; break; 1649 1.14 christos case DT_RELENT: name = "RELENT"; break; 1650 1.14 christos case DT_RELR: name = "RELR"; break; 1651 1.14 christos case DT_RELRSZ: name = "RELRSZ"; break; 1652 1.1 christos case DT_RELRENT: name = "RELRENT"; break; 1653 1.1 christos case DT_PLTREL: name = "PLTREL"; break; 1654 1.1 christos case DT_DEBUG: name = "DEBUG"; break; 1655 1.1 christos case DT_TEXTREL: name = "TEXTREL"; break; 1656 1.1 christos case DT_JMPREL: name = "JMPREL"; break; 1657 1.1 christos case DT_BIND_NOW: name = "BIND_NOW"; break; 1658 1.1 christos case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; 1659 1.1 christos case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; 1660 1.1 christos case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; 1661 1.14 christos case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; 1662 1.1 christos case DT_RUNPATH: name = "RUNPATH"; stringp = true; break; 1663 1.1 christos case DT_FLAGS: name = "FLAGS"; break; 1664 1.1 christos case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; 1665 1.1 christos case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; 1666 1.1 christos case DT_CHECKSUM: name = "CHECKSUM"; break; 1667 1.1 christos case DT_PLTPADSZ: name = "PLTPADSZ"; break; 1668 1.1 christos case DT_MOVEENT: name = "MOVEENT"; break; 1669 1.1 christos case DT_MOVESZ: name = "MOVESZ"; break; 1670 1.1 christos case DT_FEATURE: name = "FEATURE"; break; 1671 1.1 christos case DT_POSFLAG_1: name = "POSFLAG_1"; break; 1672 1.1 christos case DT_SYMINSZ: name = "SYMINSZ"; break; 1673 1.14 christos case DT_SYMINENT: name = "SYMINENT"; break; 1674 1.14 christos case DT_CONFIG: name = "CONFIG"; stringp = true; break; 1675 1.14 christos case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break; 1676 1.1 christos case DT_AUDIT: name = "AUDIT"; stringp = true; break; 1677 1.1 christos case DT_PLTPAD: name = "PLTPAD"; break; 1678 1.1 christos case DT_MOVETAB: name = "MOVETAB"; break; 1679 1.1 christos case DT_SYMINFO: name = "SYMINFO"; break; 1680 1.1 christos case DT_RELACOUNT: name = "RELACOUNT"; break; 1681 1.1 christos case DT_RELCOUNT: name = "RELCOUNT"; break; 1682 1.1 christos case DT_FLAGS_1: name = "FLAGS_1"; break; 1683 1.1 christos case DT_VERSYM: name = "VERSYM"; break; 1684 1.1 christos case DT_VERDEF: name = "VERDEF"; break; 1685 1.1 christos case DT_VERDEFNUM: name = "VERDEFNUM"; break; 1686 1.1 christos case DT_VERNEED: name = "VERNEED"; break; 1687 1.14 christos case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; 1688 1.1 christos case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break; 1689 1.14 christos case DT_USED: name = "USED"; break; 1690 1.1 christos case DT_FILTER: name = "FILTER"; stringp = true; break; 1691 1.1 christos case DT_GNU_HASH: name = "GNU_HASH"; break; 1692 1.1 christos } 1693 1.1 christos 1694 1.1 christos fprintf (f, " %-20s ", name); 1695 1.1 christos if (! stringp) 1696 1.1 christos { 1697 1.1 christos fprintf (f, "0x"); 1698 1.1 christos bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); 1699 1.1 christos } 1700 1.1 christos else 1701 1.1 christos { 1702 1.1 christos const char *string; 1703 1.1 christos unsigned int tagv = dyn.d_un.d_val; 1704 1.1 christos 1705 1.1 christos string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 1706 1.1 christos if (string == NULL) 1707 1.1 christos goto error_return; 1708 1.1 christos fprintf (f, "%s", string); 1709 1.1 christos } 1710 1.1 christos fprintf (f, "\n"); 1711 1.1 christos } 1712 1.17 christos 1713 1.1 christos _bfd_elf_munmap_section_contents (s, dynbuf); 1714 1.1 christos dynbuf = NULL; 1715 1.1 christos } 1716 1.1 christos 1717 1.1 christos if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) 1718 1.1 christos || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) 1719 1.14 christos { 1720 1.14 christos if (! _bfd_elf_slurp_version_tables (abfd, false)) 1721 1.1 christos return false; 1722 1.1 christos } 1723 1.1 christos 1724 1.1 christos if (elf_dynverdef (abfd) != 0) 1725 1.1 christos { 1726 1.1 christos Elf_Internal_Verdef *t; 1727 1.1 christos 1728 1.1 christos fprintf (f, _("\nVersion definitions:\n")); 1729 1.1 christos for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) 1730 1.1 christos { 1731 1.1 christos fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, 1732 1.1 christos t->vd_flags, t->vd_hash, 1733 1.1 christos t->vd_nodename ? t->vd_nodename : "<corrupt>"); 1734 1.1 christos if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) 1735 1.1 christos { 1736 1.1 christos Elf_Internal_Verdaux *a; 1737 1.1 christos 1738 1.1 christos fprintf (f, "\t"); 1739 1.1 christos for (a = t->vd_auxptr->vda_nextptr; 1740 1.1 christos a != NULL; 1741 1.1 christos a = a->vda_nextptr) 1742 1.1 christos fprintf (f, "%s ", 1743 1.1 christos a->vda_nodename ? a->vda_nodename : "<corrupt>"); 1744 1.1 christos fprintf (f, "\n"); 1745 1.1 christos } 1746 1.1 christos } 1747 1.1 christos } 1748 1.1 christos 1749 1.1 christos if (elf_dynverref (abfd) != 0) 1750 1.1 christos { 1751 1.1 christos Elf_Internal_Verneed *t; 1752 1.1 christos 1753 1.1 christos fprintf (f, _("\nVersion References:\n")); 1754 1.1 christos for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) 1755 1.1 christos { 1756 1.1 christos Elf_Internal_Vernaux *a; 1757 1.1 christos 1758 1.1 christos fprintf (f, _(" required from %s:\n"), 1759 1.1 christos t->vn_filename ? t->vn_filename : "<corrupt>"); 1760 1.1 christos for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 1761 1.1 christos fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, 1762 1.1 christos a->vna_flags, a->vna_other, 1763 1.1 christos a->vna_nodename ? a->vna_nodename : "<corrupt>"); 1764 1.1 christos } 1765 1.1 christos } 1766 1.14 christos 1767 1.1 christos return true; 1768 1.1 christos 1769 1.17 christos error_return: 1770 1.14 christos _bfd_elf_munmap_section_contents (s, dynbuf); 1771 1.1 christos return false; 1772 1.1 christos } 1773 1.17 christos 1774 1.17 christos /* Find the file offset corresponding to VMA by using the program 1775 1.17 christos headers. */ 1776 1.17 christos 1777 1.17 christos static file_ptr 1778 1.17 christos offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma, 1779 1.17 christos size_t size, size_t *max_size_p) 1780 1.17 christos { 1781 1.17 christos Elf_Internal_Phdr *seg; 1782 1.17 christos size_t i; 1783 1.17 christos 1784 1.17 christos for (seg = phdrs, i = 0; i < phnum; ++seg, ++i) 1785 1.17 christos if (seg->p_type == PT_LOAD 1786 1.17 christos && vma >= (seg->p_vaddr & -seg->p_align) 1787 1.17 christos && vma + size <= seg->p_vaddr + seg->p_filesz) 1788 1.17 christos { 1789 1.17 christos if (max_size_p) 1790 1.17 christos *max_size_p = seg->p_vaddr + seg->p_filesz - vma; 1791 1.17 christos return vma - seg->p_vaddr + seg->p_offset; 1792 1.17 christos } 1793 1.17 christos 1794 1.17 christos if (max_size_p) 1795 1.17 christos *max_size_p = 0; 1796 1.17 christos bfd_set_error (bfd_error_invalid_operation); 1797 1.17 christos return (file_ptr) -1; 1798 1.17 christos } 1799 1.17 christos 1800 1.17 christos /* Convert hash table to internal form. */ 1801 1.17 christos 1802 1.17 christos static bfd_vma * 1803 1.17 christos get_hash_table_data (bfd *abfd, bfd_size_type number, 1804 1.17 christos unsigned int ent_size, bfd_size_type filesize) 1805 1.17 christos { 1806 1.17 christos unsigned char *e_data = NULL; 1807 1.17 christos bfd_vma *i_data = NULL; 1808 1.17 christos bfd_size_type size; 1809 1.17 christos void *e_data_addr; 1810 1.17 christos size_t e_data_size ATTRIBUTE_UNUSED; 1811 1.17 christos 1812 1.17 christos if (ent_size != 4 && ent_size != 8) 1813 1.17 christos return NULL; 1814 1.17 christos 1815 1.17 christos if ((size_t) number != number) 1816 1.17 christos { 1817 1.17 christos bfd_set_error (bfd_error_file_too_big); 1818 1.17 christos return NULL; 1819 1.17 christos } 1820 1.17 christos 1821 1.17 christos size = ent_size * number; 1822 1.17 christos /* Be kind to memory checkers (eg valgrind, address sanitizer) by not 1823 1.17 christos attempting to allocate memory when the read is bound to fail. */ 1824 1.17 christos if (size > filesize 1825 1.17 christos || number >= ~(size_t) 0 / ent_size 1826 1.17 christos || number >= ~(size_t) 0 / sizeof (*i_data)) 1827 1.17 christos { 1828 1.17 christos bfd_set_error (bfd_error_file_too_big); 1829 1.17 christos return NULL; 1830 1.17 christos } 1831 1.18 christos 1832 1.17 christos e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size); 1833 1.17 christos if (e_data == NULL) 1834 1.17 christos return NULL; 1835 1.17 christos 1836 1.17 christos i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data)); 1837 1.17 christos if (i_data == NULL) 1838 1.19 christos { 1839 1.17 christos _bfd_munmap_temporary (e_data_addr, e_data_size); 1840 1.17 christos return NULL; 1841 1.17 christos } 1842 1.17 christos 1843 1.17 christos if (ent_size == 4) 1844 1.17 christos while (number--) 1845 1.17 christos i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size); 1846 1.17 christos else 1847 1.17 christos while (number--) 1848 1.17 christos i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size); 1849 1.18 christos 1850 1.17 christos _bfd_munmap_temporary (e_data_addr, e_data_size); 1851 1.17 christos return i_data; 1852 1.17 christos } 1853 1.17 christos 1854 1.17 christos /* Address of .MIPS.xhash section. FIXME: What is the best way to 1855 1.17 christos support DT_MIPS_XHASH? */ 1856 1.17 christos #define DT_MIPS_XHASH 0x70000036 1857 1.17 christos 1858 1.17 christos /* Reconstruct dynamic symbol table from PT_DYNAMIC segment. */ 1859 1.17 christos 1860 1.17 christos bool 1861 1.17 christos _bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr, 1862 1.17 christos Elf_Internal_Phdr *phdrs, size_t phnum, 1863 1.17 christos bfd_size_type filesize) 1864 1.17 christos { 1865 1.17 christos bfd_byte *extdyn, *extdynend; 1866 1.17 christos size_t extdynsize; 1867 1.17 christos void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); 1868 1.17 christos bool (*swap_symbol_in) (bfd *, const void *, const void *, 1869 1.17 christos Elf_Internal_Sym *); 1870 1.17 christos Elf_Internal_Dyn dyn; 1871 1.17 christos bfd_vma dt_hash = 0; 1872 1.17 christos bfd_vma dt_gnu_hash = 0; 1873 1.17 christos bfd_vma dt_mips_xhash = 0; 1874 1.17 christos bfd_vma dt_strtab = 0; 1875 1.17 christos bfd_vma dt_symtab = 0; 1876 1.17 christos size_t dt_strsz = 0; 1877 1.17 christos bfd_vma dt_versym = 0; 1878 1.17 christos bfd_vma dt_verdef = 0; 1879 1.17 christos bfd_vma dt_verneed = 0; 1880 1.17 christos bfd_byte *dynbuf = NULL; 1881 1.17 christos char *strbuf = NULL; 1882 1.17 christos bfd_vma *gnubuckets = NULL; 1883 1.17 christos bfd_vma *gnuchains = NULL; 1884 1.17 christos bfd_vma *mipsxlat = NULL; 1885 1.17 christos file_ptr saved_filepos, filepos; 1886 1.17 christos bool res = false; 1887 1.17 christos size_t amt; 1888 1.17 christos bfd_byte *esymbuf = NULL, *esym; 1889 1.17 christos bfd_size_type symcount; 1890 1.17 christos Elf_Internal_Sym *isymbuf = NULL; 1891 1.17 christos Elf_Internal_Sym *isym, *isymend; 1892 1.17 christos bfd_byte *versym = NULL; 1893 1.17 christos bfd_byte *verdef = NULL; 1894 1.17 christos bfd_byte *verneed = NULL; 1895 1.17 christos size_t verdef_size = 0; 1896 1.17 christos size_t verneed_size = 0; 1897 1.17 christos size_t extsym_size; 1898 1.17 christos const struct elf_backend_data *bed; 1899 1.17 christos void *dynbuf_addr = NULL; 1900 1.17 christos void *esymbuf_addr = NULL; 1901 1.17 christos size_t dynbuf_size = 0; 1902 1.17 christos size_t esymbuf_size = 0; 1903 1.17 christos 1904 1.17 christos /* Return TRUE if symbol table is bad. */ 1905 1.17 christos if (elf_bad_symtab (abfd)) 1906 1.17 christos return true; 1907 1.17 christos 1908 1.17 christos /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before. */ 1909 1.17 christos if (elf_tdata (abfd)->dt_strtab != NULL) 1910 1.17 christos return true; 1911 1.17 christos 1912 1.17 christos bed = get_elf_backend_data (abfd); 1913 1.17 christos 1914 1.17 christos /* Save file position for elf_object_p. */ 1915 1.17 christos saved_filepos = bfd_tell (abfd); 1916 1.17 christos 1917 1.17 christos if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0) 1918 1.17 christos goto error_return; 1919 1.17 christos 1920 1.18 christos dynbuf_size = phdr->p_filesz; 1921 1.17 christos dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size); 1922 1.17 christos if (dynbuf == NULL) 1923 1.17 christos goto error_return; 1924 1.17 christos 1925 1.17 christos extsym_size = bed->s->sizeof_sym; 1926 1.17 christos extdynsize = bed->s->sizeof_dyn; 1927 1.17 christos swap_dyn_in = bed->s->swap_dyn_in; 1928 1.17 christos 1929 1.17 christos extdyn = dynbuf; 1930 1.17 christos if (phdr->p_filesz < extdynsize) 1931 1.17 christos goto error_return; 1932 1.17 christos extdynend = extdyn + phdr->p_filesz; 1933 1.17 christos for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) 1934 1.17 christos { 1935 1.17 christos swap_dyn_in (abfd, extdyn, &dyn); 1936 1.17 christos 1937 1.17 christos if (dyn.d_tag == DT_NULL) 1938 1.17 christos break; 1939 1.17 christos 1940 1.17 christos switch (dyn.d_tag) 1941 1.17 christos { 1942 1.17 christos case DT_HASH: 1943 1.17 christos dt_hash = dyn.d_un.d_val; 1944 1.17 christos break; 1945 1.17 christos case DT_GNU_HASH: 1946 1.17 christos if (bed->elf_machine_code != EM_MIPS 1947 1.17 christos && bed->elf_machine_code != EM_MIPS_RS3_LE) 1948 1.17 christos dt_gnu_hash = dyn.d_un.d_val; 1949 1.17 christos break; 1950 1.17 christos case DT_STRTAB: 1951 1.17 christos dt_strtab = dyn.d_un.d_val; 1952 1.17 christos break; 1953 1.17 christos case DT_SYMTAB: 1954 1.17 christos dt_symtab = dyn.d_un.d_val; 1955 1.17 christos break; 1956 1.17 christos case DT_STRSZ: 1957 1.17 christos dt_strsz = dyn.d_un.d_val; 1958 1.17 christos break; 1959 1.17 christos case DT_SYMENT: 1960 1.17 christos if (dyn.d_un.d_val != extsym_size) 1961 1.17 christos goto error_return; 1962 1.17 christos break; 1963 1.17 christos case DT_VERSYM: 1964 1.17 christos dt_versym = dyn.d_un.d_val; 1965 1.17 christos break; 1966 1.17 christos case DT_VERDEF: 1967 1.17 christos dt_verdef = dyn.d_un.d_val; 1968 1.17 christos break; 1969 1.17 christos case DT_VERNEED: 1970 1.17 christos dt_verneed = dyn.d_un.d_val; 1971 1.17 christos break; 1972 1.17 christos default: 1973 1.17 christos if (dyn.d_tag == DT_MIPS_XHASH 1974 1.17 christos && (bed->elf_machine_code == EM_MIPS 1975 1.17 christos || bed->elf_machine_code == EM_MIPS_RS3_LE)) 1976 1.17 christos { 1977 1.17 christos dt_gnu_hash = dyn.d_un.d_val; 1978 1.17 christos dt_mips_xhash = dyn.d_un.d_val; 1979 1.17 christos } 1980 1.17 christos break; 1981 1.17 christos } 1982 1.17 christos } 1983 1.17 christos 1984 1.17 christos /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC 1985 1.17 christos segment. */ 1986 1.17 christos if ((!dt_hash && !dt_gnu_hash) 1987 1.17 christos || !dt_strtab 1988 1.17 christos || !dt_symtab 1989 1.17 christos || !dt_strsz) 1990 1.17 christos goto error_return; 1991 1.17 christos 1992 1.17 christos /* Get dynamic string table. */ 1993 1.17 christos filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL); 1994 1.17 christos if (filepos == (file_ptr) -1 1995 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 1996 1.17 christos goto error_return; 1997 1.17 christos 1998 1.18 christos /* Dynamic string table must be valid until ABFD is closed. */ 1999 1.17 christos strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz); 2000 1.17 christos if (strbuf == NULL) 2001 1.17 christos goto error_return; 2002 1.17 christos if (strbuf[dt_strsz - 1] != 0) 2003 1.17 christos { 2004 1.17 christos /* It is an error if a string table is't terminated. */ 2005 1.17 christos _bfd_error_handler 2006 1.17 christos /* xgettext:c-format */ 2007 1.18 christos (_("%pB: DT_STRTAB table is corrupt"), abfd); 2008 1.17 christos strbuf[dt_strsz - 1] = 0; 2009 1.17 christos } 2010 1.17 christos 2011 1.17 christos /* Get the real symbol count from DT_HASH or DT_GNU_HASH. Prefer 2012 1.17 christos DT_HASH since it is simpler than DT_GNU_HASH. */ 2013 1.17 christos if (dt_hash) 2014 1.17 christos { 2015 1.17 christos unsigned char nb[16]; 2016 1.17 christos unsigned int hash_ent_size; 2017 1.17 christos 2018 1.17 christos switch (bed->elf_machine_code) 2019 1.17 christos { 2020 1.17 christos case EM_ALPHA: 2021 1.17 christos case EM_S390: 2022 1.17 christos case EM_S390_OLD: 2023 1.17 christos if (bed->s->elfclass == ELFCLASS64) 2024 1.17 christos { 2025 1.17 christos hash_ent_size = 8; 2026 1.17 christos break; 2027 1.17 christos } 2028 1.17 christos /* FALLTHROUGH */ 2029 1.17 christos default: 2030 1.17 christos hash_ent_size = 4; 2031 1.17 christos break; 2032 1.17 christos } 2033 1.19 christos 2034 1.17 christos filepos = offset_from_vma (phdrs, phnum, dt_hash, 2 * hash_ent_size, 2035 1.17 christos NULL); 2036 1.17 christos if (filepos == (file_ptr) -1 2037 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0 2038 1.17 christos || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size) 2039 1.17 christos goto error_return; 2040 1.17 christos 2041 1.17 christos /* The number of dynamic symbol table entries equals the number 2042 1.17 christos of chains. */ 2043 1.17 christos if (hash_ent_size == 8) 2044 1.17 christos symcount = bfd_get_64 (abfd, nb + hash_ent_size); 2045 1.17 christos else 2046 1.17 christos symcount = bfd_get_32 (abfd, nb + hash_ent_size); 2047 1.17 christos } 2048 1.17 christos else 2049 1.17 christos { 2050 1.17 christos /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL 2051 1.17 christos bindings are in hash table. Since in dynamic symbol table, 2052 1.17 christos all symbols with STB_LOCAL binding are placed before symbols 2053 1.17 christos with other bindings and all undefined symbols are placed 2054 1.17 christos before defined ones, the highest symbol index in DT_GNU_HASH 2055 1.17 christos is the highest dynamic symbol table index. */ 2056 1.17 christos unsigned char nb[16]; 2057 1.17 christos bfd_vma ngnubuckets; 2058 1.17 christos bfd_vma gnusymidx; 2059 1.17 christos size_t i, ngnuchains; 2060 1.17 christos bfd_vma maxchain = 0xffffffff, bitmaskwords; 2061 1.17 christos bfd_vma buckets_vma; 2062 1.17 christos 2063 1.17 christos filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash, 2064 1.17 christos sizeof (nb), NULL); 2065 1.17 christos if (filepos == (file_ptr) -1 2066 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0 2067 1.17 christos || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb)) 2068 1.17 christos goto error_return; 2069 1.17 christos 2070 1.17 christos ngnubuckets = bfd_get_32 (abfd, nb); 2071 1.17 christos gnusymidx = bfd_get_32 (abfd, nb + 4); 2072 1.17 christos bitmaskwords = bfd_get_32 (abfd, nb + 8); 2073 1.17 christos buckets_vma = dt_gnu_hash + 16; 2074 1.17 christos if (bed->s->elfclass == ELFCLASS32) 2075 1.17 christos buckets_vma += bitmaskwords * 4; 2076 1.17 christos else 2077 1.17 christos buckets_vma += bitmaskwords * 8; 2078 1.17 christos filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL); 2079 1.17 christos if (filepos == (file_ptr) -1 2080 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2081 1.17 christos goto error_return; 2082 1.17 christos 2083 1.17 christos gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize); 2084 1.17 christos if (gnubuckets == NULL) 2085 1.17 christos goto error_return; 2086 1.17 christos 2087 1.17 christos for (i = 0; i < ngnubuckets; i++) 2088 1.17 christos if (gnubuckets[i] != 0) 2089 1.17 christos { 2090 1.17 christos if (gnubuckets[i] < gnusymidx) 2091 1.17 christos goto error_return; 2092 1.17 christos 2093 1.17 christos if (maxchain == 0xffffffff || gnubuckets[i] > maxchain) 2094 1.17 christos maxchain = gnubuckets[i]; 2095 1.17 christos } 2096 1.17 christos 2097 1.17 christos if (maxchain == 0xffffffff) 2098 1.17 christos { 2099 1.17 christos symcount = 0; 2100 1.17 christos goto empty_gnu_hash; 2101 1.17 christos } 2102 1.17 christos 2103 1.17 christos maxchain -= gnusymidx; 2104 1.19 christos filepos = offset_from_vma (phdrs, phnum, 2105 1.17 christos buckets_vma + 4 * (ngnubuckets + maxchain), 2106 1.17 christos 4, NULL); 2107 1.17 christos if (filepos == (file_ptr) -1 2108 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2109 1.17 christos goto error_return; 2110 1.17 christos 2111 1.17 christos do 2112 1.17 christos { 2113 1.17 christos if (bfd_read (nb, 4, abfd) != 4) 2114 1.17 christos goto error_return; 2115 1.17 christos ++maxchain; 2116 1.17 christos if (maxchain == 0) 2117 1.17 christos goto error_return; 2118 1.17 christos } 2119 1.17 christos while ((bfd_get_32 (abfd, nb) & 1) == 0); 2120 1.17 christos 2121 1.19 christos filepos = offset_from_vma (phdrs, phnum, 2122 1.17 christos buckets_vma + 4 * ngnubuckets, 2123 1.17 christos 4, NULL); 2124 1.17 christos if (filepos == (file_ptr) -1 2125 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2126 1.17 christos goto error_return; 2127 1.17 christos 2128 1.17 christos gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize); 2129 1.17 christos if (gnuchains == NULL) 2130 1.17 christos goto error_return; 2131 1.17 christos ngnuchains = maxchain; 2132 1.17 christos 2133 1.17 christos if (dt_mips_xhash) 2134 1.17 christos { 2135 1.19 christos filepos = offset_from_vma (phdrs, phnum, 2136 1.17 christos buckets_vma + 4 * (ngnubuckets + maxchain), 2137 1.17 christos 4, NULL); 2138 1.17 christos if (filepos == (file_ptr) -1 2139 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2140 1.17 christos goto error_return; 2141 1.17 christos 2142 1.17 christos mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize); 2143 1.17 christos if (mipsxlat == NULL) 2144 1.17 christos goto error_return; 2145 1.17 christos } 2146 1.17 christos 2147 1.17 christos symcount = 0; 2148 1.17 christos for (i = 0; i < ngnubuckets; ++i) 2149 1.17 christos if (gnubuckets[i] != 0) 2150 1.17 christos { 2151 1.17 christos bfd_vma si = gnubuckets[i]; 2152 1.17 christos bfd_vma off = si - gnusymidx; 2153 1.17 christos do 2154 1.17 christos { 2155 1.17 christos if (mipsxlat) 2156 1.17 christos { 2157 1.17 christos if (mipsxlat[off] >= symcount) 2158 1.17 christos symcount = mipsxlat[off] + 1; 2159 1.17 christos } 2160 1.17 christos else 2161 1.17 christos { 2162 1.17 christos if (si >= symcount) 2163 1.17 christos symcount = si + 1; 2164 1.17 christos } 2165 1.17 christos si++; 2166 1.17 christos } 2167 1.17 christos while (off < ngnuchains && (gnuchains[off++] & 1) == 0); 2168 1.17 christos } 2169 1.17 christos } 2170 1.17 christos 2171 1.17 christos /* Swap in dynamic symbol table. */ 2172 1.17 christos if (_bfd_mul_overflow (symcount, extsym_size, &amt)) 2173 1.17 christos { 2174 1.17 christos bfd_set_error (bfd_error_file_too_big); 2175 1.17 christos goto error_return; 2176 1.17 christos } 2177 1.17 christos 2178 1.17 christos filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL); 2179 1.17 christos if (filepos == (file_ptr) -1 2180 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2181 1.17 christos goto error_return; 2182 1.18 christos esymbuf_size = amt; 2183 1.18 christos esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size, 2184 1.17 christos &esymbuf_addr, &esymbuf_size); 2185 1.17 christos if (esymbuf == NULL) 2186 1.17 christos goto error_return; 2187 1.17 christos 2188 1.17 christos if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt)) 2189 1.17 christos { 2190 1.17 christos bfd_set_error (bfd_error_file_too_big); 2191 1.17 christos goto error_return; 2192 1.17 christos } 2193 1.17 christos 2194 1.17 christos /* Dynamic symbol table must be valid until ABFD is closed. */ 2195 1.17 christos isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt); 2196 1.17 christos if (isymbuf == NULL) 2197 1.17 christos goto error_return; 2198 1.17 christos 2199 1.17 christos swap_symbol_in = bed->s->swap_symbol_in; 2200 1.17 christos 2201 1.17 christos /* Convert the symbols to internal form. */ 2202 1.17 christos isymend = isymbuf + symcount; 2203 1.17 christos for (esym = esymbuf, isym = isymbuf; 2204 1.17 christos isym < isymend; 2205 1.17 christos esym += extsym_size, isym++) 2206 1.17 christos if (!swap_symbol_in (abfd, esym, NULL, isym) 2207 1.17 christos || isym->st_name >= dt_strsz) 2208 1.17 christos { 2209 1.17 christos bfd_set_error (bfd_error_invalid_operation); 2210 1.17 christos goto error_return; 2211 1.17 christos } 2212 1.17 christos 2213 1.17 christos if (dt_versym) 2214 1.17 christos { 2215 1.17 christos /* Swap in DT_VERSYM. */ 2216 1.17 christos if (_bfd_mul_overflow (symcount, 2, &amt)) 2217 1.17 christos { 2218 1.17 christos bfd_set_error (bfd_error_file_too_big); 2219 1.17 christos goto error_return; 2220 1.17 christos } 2221 1.17 christos 2222 1.17 christos filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL); 2223 1.17 christos if (filepos == (file_ptr) -1 2224 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2225 1.17 christos goto error_return; 2226 1.17 christos 2227 1.18 christos /* DT_VERSYM info must be valid until ABFD is closed. */ 2228 1.17 christos versym = _bfd_mmap_persistent (abfd, amt); 2229 1.17 christos 2230 1.17 christos if (dt_verdef) 2231 1.17 christos { 2232 1.17 christos /* Read in DT_VERDEF. */ 2233 1.17 christos filepos = offset_from_vma (phdrs, phnum, dt_verdef, 2234 1.17 christos 0, &verdef_size); 2235 1.17 christos if (filepos == (file_ptr) -1 2236 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2237 1.17 christos goto error_return; 2238 1.17 christos 2239 1.18 christos /* DT_VERDEF info must be valid until ABFD is closed. */ 2240 1.17 christos verdef = _bfd_mmap_persistent (abfd, verdef_size); 2241 1.17 christos } 2242 1.17 christos 2243 1.17 christos if (dt_verneed) 2244 1.17 christos { 2245 1.17 christos /* Read in DT_VERNEED. */ 2246 1.17 christos filepos = offset_from_vma (phdrs, phnum, dt_verneed, 2247 1.17 christos 0, &verneed_size); 2248 1.17 christos if (filepos == (file_ptr) -1 2249 1.17 christos || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2250 1.17 christos goto error_return; 2251 1.17 christos 2252 1.18 christos /* DT_VERNEED info must be valid until ABFD is closed. */ 2253 1.17 christos verneed = _bfd_mmap_persistent (abfd, verneed_size); 2254 1.17 christos } 2255 1.17 christos } 2256 1.17 christos 2257 1.17 christos empty_gnu_hash: 2258 1.17 christos elf_tdata (abfd)->dt_strtab = strbuf; 2259 1.17 christos elf_tdata (abfd)->dt_strsz = dt_strsz; 2260 1.17 christos elf_tdata (abfd)->dt_symtab = isymbuf; 2261 1.17 christos elf_tdata (abfd)->dt_symtab_count = symcount; 2262 1.17 christos elf_tdata (abfd)->dt_versym = versym; 2263 1.17 christos elf_tdata (abfd)->dt_verdef = verdef; 2264 1.17 christos elf_tdata (abfd)->dt_verneed = verneed; 2265 1.17 christos elf_tdata (abfd)->dt_verdef_count 2266 1.17 christos = verdef_size / sizeof (Elf_External_Verdef); 2267 1.17 christos elf_tdata (abfd)->dt_verneed_count 2268 1.17 christos = verneed_size / sizeof (Elf_External_Verneed); 2269 1.17 christos 2270 1.17 christos res = true; 2271 1.17 christos 2272 1.17 christos error_return: 2273 1.17 christos /* Restore file position for elf_object_p. */ 2274 1.17 christos if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0) 2275 1.18 christos res = false; 2276 1.18 christos _bfd_munmap_temporary (dynbuf_addr, dynbuf_size); 2277 1.17 christos _bfd_munmap_temporary (esymbuf_addr, esymbuf_size); 2278 1.17 christos free (gnubuckets); 2279 1.17 christos free (gnuchains); 2280 1.17 christos free (mipsxlat); 2281 1.17 christos return res; 2282 1.17 christos } 2283 1.17 christos 2284 1.17 christos /* Reconstruct section from dynamic symbol. */ 2285 1.17 christos 2286 1.17 christos asection * 2287 1.17 christos _bfd_elf_get_section_from_dynamic_symbol (bfd *abfd, 2288 1.17 christos Elf_Internal_Sym *isym) 2289 1.17 christos { 2290 1.17 christos asection *sec; 2291 1.17 christos flagword flags; 2292 1.17 christos 2293 1.17 christos if (!elf_use_dt_symtab_p (abfd)) 2294 1.17 christos return NULL; 2295 1.17 christos 2296 1.17 christos flags = SEC_ALLOC | SEC_LOAD; 2297 1.17 christos switch (ELF_ST_TYPE (isym->st_info)) 2298 1.17 christos { 2299 1.17 christos case STT_FUNC: 2300 1.17 christos case STT_GNU_IFUNC: 2301 1.17 christos sec = bfd_get_section_by_name (abfd, ".text"); 2302 1.17 christos if (sec == NULL) 2303 1.17 christos sec = bfd_make_section_with_flags (abfd, 2304 1.17 christos ".text", 2305 1.17 christos flags | SEC_CODE); 2306 1.17 christos break; 2307 1.17 christos case STT_COMMON: 2308 1.17 christos sec = bfd_com_section_ptr; 2309 1.17 christos break; 2310 1.17 christos case STT_OBJECT: 2311 1.17 christos sec = bfd_get_section_by_name (abfd, ".data"); 2312 1.17 christos if (sec == NULL) 2313 1.17 christos sec = bfd_make_section_with_flags (abfd, 2314 1.17 christos ".data", 2315 1.17 christos flags | SEC_DATA); 2316 1.17 christos break; 2317 1.17 christos case STT_TLS: 2318 1.17 christos sec = bfd_get_section_by_name (abfd, ".tdata"); 2319 1.17 christos if (sec == NULL) 2320 1.17 christos sec = bfd_make_section_with_flags (abfd, 2321 1.17 christos ".tdata", 2322 1.17 christos (flags 2323 1.17 christos | SEC_DATA 2324 1.17 christos | SEC_THREAD_LOCAL)); 2325 1.17 christos break; 2326 1.17 christos default: 2327 1.17 christos sec = bfd_abs_section_ptr; 2328 1.17 christos break; 2329 1.17 christos } 2330 1.17 christos 2331 1.17 christos return sec; 2332 1.17 christos } 2333 1.12 christos 2334 1.12 christos /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE 2335 1.5 christos and return symbol version for symbol version itself. */ 2336 1.5 christos 2337 1.5 christos const char * 2338 1.14 christos _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, 2339 1.14 christos bool base_p, 2340 1.5 christos bool *hidden) 2341 1.5 christos { 2342 1.17 christos const char *version_string = NULL; 2343 1.17 christos if ((elf_dynversym (abfd) != 0 2344 1.17 christos && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) 2345 1.17 christos || (elf_tdata (abfd)->dt_versym != NULL 2346 1.17 christos && (elf_tdata (abfd)->dt_verdef != NULL 2347 1.5 christos || elf_tdata (abfd)->dt_verneed != NULL))) 2348 1.5 christos { 2349 1.5 christos unsigned int vernum = ((elf_symbol_type *) symbol)->version; 2350 1.5 christos 2351 1.5 christos *hidden = (vernum & VERSYM_HIDDEN) != 0; 2352 1.5 christos vernum &= VERSYM_VERSION; 2353 1.5 christos 2354 1.5 christos if (vernum == 0) 2355 1.11 christos version_string = ""; 2356 1.11 christos else if (vernum == 1 2357 1.11 christos && (vernum > elf_tdata (abfd)->cverdefs 2358 1.11 christos || (elf_tdata (abfd)->verdef[0].vd_flags 2359 1.12 christos == VER_FLG_BASE))) 2360 1.5 christos version_string = base_p ? "Base" : ""; 2361 1.12 christos else if (vernum <= elf_tdata (abfd)->cverdefs) 2362 1.12 christos { 2363 1.12 christos const char *nodename 2364 1.12 christos = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; 2365 1.12 christos version_string = ""; 2366 1.12 christos if (base_p 2367 1.12 christos || nodename == NULL 2368 1.19 christos || symbol->name == NULL 2369 1.12 christos || ! streq (symbol->name, nodename)) 2370 1.12 christos version_string = nodename; 2371 1.5 christos } 2372 1.5 christos else 2373 1.5 christos { 2374 1.5 christos Elf_Internal_Verneed *t; 2375 1.11 christos 2376 1.5 christos version_string = _("<corrupt>"); 2377 1.5 christos for (t = elf_tdata (abfd)->verref; 2378 1.5 christos t != NULL; 2379 1.5 christos t = t->vn_nextref) 2380 1.5 christos { 2381 1.5 christos Elf_Internal_Vernaux *a; 2382 1.5 christos 2383 1.5 christos for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 2384 1.5 christos { 2385 1.5 christos if (a->vna_other == vernum) 2386 1.14 christos { 2387 1.5 christos *hidden = true; 2388 1.5 christos version_string = a->vna_nodename; 2389 1.5 christos break; 2390 1.5 christos } 2391 1.5 christos } 2392 1.5 christos } 2393 1.5 christos } 2394 1.5 christos } 2395 1.5 christos return version_string; 2396 1.5 christos } 2397 1.1 christos 2398 1.1 christos /* Display ELF-specific fields of a symbol. */ 2399 1.1 christos 2400 1.1 christos void 2401 1.1 christos bfd_elf_print_symbol (bfd *abfd, 2402 1.1 christos void *filep, 2403 1.1 christos asymbol *symbol, 2404 1.1 christos bfd_print_symbol_type how) 2405 1.1 christos { 2406 1.18 christos FILE *file = (FILE *) filep; 2407 1.18 christos const char *symname = (symbol->name != bfd_symbol_error_name 2408 1.18 christos ? symbol->name : _("<corrupt>")); 2409 1.1 christos 2410 1.1 christos switch (how) 2411 1.1 christos { 2412 1.18 christos case bfd_print_symbol_name: 2413 1.1 christos fprintf (file, "%s", symname); 2414 1.1 christos break; 2415 1.1 christos case bfd_print_symbol_more: 2416 1.1 christos fprintf (file, "elf "); 2417 1.11 christos bfd_fprintf_vma (abfd, file, symbol->value); 2418 1.1 christos fprintf (file, " %x", symbol->flags); 2419 1.1 christos break; 2420 1.1 christos case bfd_print_symbol_all: 2421 1.1 christos { 2422 1.1 christos const char *section_name; 2423 1.1 christos const char *name = NULL; 2424 1.1 christos const struct elf_backend_data *bed; 2425 1.1 christos unsigned char st_other; 2426 1.5 christos bfd_vma val; 2427 1.14 christos const char *version_string; 2428 1.1 christos bool hidden; 2429 1.1 christos 2430 1.1 christos section_name = symbol->section ? symbol->section->name : "(*none*)"; 2431 1.1 christos 2432 1.1 christos bed = get_elf_backend_data (abfd); 2433 1.1 christos if (bed->elf_backend_print_symbol_all) 2434 1.1 christos name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); 2435 1.18 christos 2436 1.18 christos if (name != NULL) 2437 1.18 christos symname = name; 2438 1.18 christos else 2439 1.1 christos bfd_print_symbol_vandf (abfd, file, symbol); 2440 1.1 christos 2441 1.1 christos fprintf (file, " %s\t", section_name); 2442 1.1 christos /* Print the "other" value for a symbol. For common symbols, 2443 1.1 christos we've already printed the size; now print the alignment. 2444 1.1 christos For other symbols, we have no specified alignment, and 2445 1.1 christos we've printed the address; now print the size. */ 2446 1.1 christos if (symbol->section && bfd_is_com_section (symbol->section)) 2447 1.1 christos val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; 2448 1.1 christos else 2449 1.1 christos val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; 2450 1.1 christos bfd_fprintf_vma (abfd, file, val); 2451 1.1 christos 2452 1.5 christos /* If we have version information, print it. */ 2453 1.5 christos version_string = _bfd_elf_get_symbol_version_string (abfd, 2454 1.14 christos symbol, 2455 1.5 christos true, 2456 1.5 christos &hidden); 2457 1.1 christos if (version_string) 2458 1.5 christos { 2459 1.1 christos if (!hidden) 2460 1.1 christos fprintf (file, " %-11s", version_string); 2461 1.1 christos else 2462 1.1 christos { 2463 1.1 christos int i; 2464 1.1 christos 2465 1.1 christos fprintf (file, " (%s)", version_string); 2466 1.1 christos for (i = 10 - strlen (version_string); i > 0; --i) 2467 1.1 christos putc (' ', file); 2468 1.1 christos } 2469 1.1 christos } 2470 1.1 christos 2471 1.1 christos /* If the st_other field is not zero, print it. */ 2472 1.1 christos st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; 2473 1.1 christos 2474 1.1 christos switch (st_other) 2475 1.1 christos { 2476 1.1 christos case 0: break; 2477 1.1 christos case STV_INTERNAL: fprintf (file, " .internal"); break; 2478 1.1 christos case STV_HIDDEN: fprintf (file, " .hidden"); break; 2479 1.1 christos case STV_PROTECTED: fprintf (file, " .protected"); break; 2480 1.1 christos default: 2481 1.1 christos /* Some other non-defined flags are also present, so print 2482 1.1 christos everything hex. */ 2483 1.1 christos fprintf (file, " 0x%02x", (unsigned int) st_other); 2484 1.1 christos } 2485 1.18 christos 2486 1.1 christos fprintf (file, " %s", symname); 2487 1.1 christos } 2488 1.1 christos break; 2489 1.1 christos } 2490 1.1 christos } 2491 1.1 christos 2492 1.1 christos /* ELF .o/exec file reading */ 2494 1.1 christos 2495 1.14 christos /* Create a new bfd section from an ELF section header. */ 2496 1.1 christos 2497 1.1 christos bool 2498 1.1 christos bfd_section_from_shdr (bfd *abfd, unsigned int shindex) 2499 1.1 christos { 2500 1.1 christos Elf_Internal_Shdr *hdr; 2501 1.1 christos Elf_Internal_Ehdr *ehdr; 2502 1.14 christos const struct elf_backend_data *bed; 2503 1.1 christos const char *name; 2504 1.1 christos bool ret = true; 2505 1.14 christos 2506 1.1 christos if (shindex >= elf_numsections (abfd)) 2507 1.14 christos return false; 2508 1.14 christos 2509 1.14 christos /* PR17512: A corrupt ELF binary might contain a loop of sections via 2510 1.14 christos sh_link or sh_info. Detect this here, by refusing to load a 2511 1.5 christos section that we are already in the process of loading. */ 2512 1.14 christos if (elf_tdata (abfd)->being_created[shindex]) 2513 1.14 christos { 2514 1.14 christos _bfd_error_handler 2515 1.5 christos (_("%pB: warning: loop in section dependencies detected"), abfd); 2516 1.14 christos return false; 2517 1.5 christos } 2518 1.1 christos elf_tdata (abfd)->being_created[shindex] = true; 2519 1.1 christos 2520 1.1 christos hdr = elf_elfsections (abfd)[shindex]; 2521 1.1 christos ehdr = elf_elfheader (abfd); 2522 1.1 christos name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, 2523 1.5 christos hdr->sh_name); 2524 1.1 christos if (name == NULL) 2525 1.1 christos goto fail; 2526 1.1 christos 2527 1.1 christos bed = get_elf_backend_data (abfd); 2528 1.1 christos switch (hdr->sh_type) 2529 1.1 christos { 2530 1.5 christos case SHT_NULL: 2531 1.1 christos /* Inactive section. Throw it away. */ 2532 1.5 christos goto success; 2533 1.5 christos 2534 1.5 christos case SHT_PROGBITS: /* Normal section with contents. */ 2535 1.5 christos case SHT_NOBITS: /* .bss section. */ 2536 1.1 christos case SHT_HASH: /* .hash section. */ 2537 1.1 christos case SHT_NOTE: /* .note section. */ 2538 1.1 christos case SHT_INIT_ARRAY: /* .init_array section. */ 2539 1.1 christos case SHT_FINI_ARRAY: /* .fini_array section. */ 2540 1.1 christos case SHT_PREINIT_ARRAY: /* .preinit_array section. */ 2541 1.19 christos case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ 2542 1.5 christos case SHT_GNU_HASH: /* .gnu.hash section. */ 2543 1.5 christos case SHT_GNU_SFRAME: /* .sframe section. */ 2544 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2545 1.1 christos goto success; 2546 1.1 christos 2547 1.5 christos case SHT_DYNAMIC: /* Dynamic linking information. */ 2548 1.5 christos if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 2549 1.1 christos goto fail; 2550 1.1 christos 2551 1.14 christos if (hdr->sh_link > elf_numsections (abfd)) 2552 1.14 christos { 2553 1.1 christos /* PR 10478: Accept Solaris binaries with a sh_link field 2554 1.1 christos set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */ 2555 1.1 christos switch (bfd_get_arch (abfd)) 2556 1.1 christos { 2557 1.14 christos case bfd_arch_i386: 2558 1.14 christos case bfd_arch_sparc: 2559 1.1 christos if (hdr->sh_link == (SHN_LORESERVE & 0xffff) 2560 1.1 christos || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff)) 2561 1.1 christos break; 2562 1.5 christos /* Otherwise fall through. */ 2563 1.1 christos default: 2564 1.1 christos goto fail; 2565 1.1 christos } 2566 1.5 christos } 2567 1.1 christos else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) 2568 1.1 christos goto fail; 2569 1.1 christos else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) 2570 1.1 christos { 2571 1.1 christos Elf_Internal_Shdr *dynsymhdr; 2572 1.1 christos 2573 1.1 christos /* The shared libraries distributed with hpux11 have a bogus 2574 1.1 christos sh_link field for the ".dynamic" section. Find the 2575 1.1 christos string table for the ".dynsym" section instead. */ 2576 1.1 christos if (elf_dynsymtab (abfd) != 0) 2577 1.1 christos { 2578 1.1 christos dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; 2579 1.1 christos hdr->sh_link = dynsymhdr->sh_link; 2580 1.1 christos } 2581 1.1 christos else 2582 1.1 christos { 2583 1.1 christos unsigned int i, num_sec; 2584 1.1 christos 2585 1.1 christos num_sec = elf_numsections (abfd); 2586 1.1 christos for (i = 1; i < num_sec; i++) 2587 1.1 christos { 2588 1.1 christos dynsymhdr = elf_elfsections (abfd)[i]; 2589 1.1 christos if (dynsymhdr->sh_type == SHT_DYNSYM) 2590 1.1 christos { 2591 1.1 christos hdr->sh_link = dynsymhdr->sh_link; 2592 1.1 christos break; 2593 1.1 christos } 2594 1.1 christos } 2595 1.5 christos } 2596 1.1 christos } 2597 1.5 christos goto success; 2598 1.1 christos 2599 1.5 christos case SHT_SYMTAB: /* A symbol table. */ 2600 1.1 christos if (elf_onesymtab (abfd) == shindex) 2601 1.1 christos goto success; 2602 1.5 christos 2603 1.5 christos if (hdr->sh_entsize != bed->s->sizeof_sym) 2604 1.1 christos goto fail; 2605 1.3 christos 2606 1.3 christos if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) 2607 1.5 christos { 2608 1.3 christos if (hdr->sh_size != 0) 2609 1.3 christos goto fail; 2610 1.3 christos /* Some assemblers erroneously set sh_info to one with a 2611 1.3 christos zero sh_size. ld sees this as a global symbol count 2612 1.5 christos of (unsigned) -1. Fix it here. */ 2613 1.3 christos hdr->sh_info = 0; 2614 1.5 christos goto success; 2615 1.8 christos } 2616 1.8 christos 2617 1.8 christos /* PR 18854: A binary might contain more than one symbol table. 2618 1.8 christos Unusual, but possible. Warn, but continue. */ 2619 1.9 christos if (elf_onesymtab (abfd) != 0) 2620 1.9 christos { 2621 1.11 christos _bfd_error_handler 2622 1.9 christos /* xgettext:c-format */ 2623 1.8 christos (_("%pB: warning: multiple symbol tables detected" 2624 1.8 christos " - ignoring the table in section %u"), 2625 1.8 christos abfd, shindex); 2626 1.1 christos goto success; 2627 1.8 christos } 2628 1.8 christos elf_onesymtab (abfd) = shindex; 2629 1.1 christos elf_symtab_hdr (abfd) = *hdr; 2630 1.1 christos elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); 2631 1.1 christos abfd->flags |= HAS_SYMS; 2632 1.1 christos 2633 1.1 christos /* Sometimes a shared object will map in the symbol table. If 2634 1.1 christos SHF_ALLOC is set, and this is a shared object, then we also 2635 1.1 christos treat this section as a BFD section. We can not base the 2636 1.1 christos decision purely on SHF_ALLOC, because that flag is sometimes 2637 1.1 christos set in a relocatable object file, which would confuse the 2638 1.1 christos linker. */ 2639 1.1 christos if ((hdr->sh_flags & SHF_ALLOC) != 0 2640 1.1 christos && (abfd->flags & DYNAMIC) != 0 2641 1.5 christos && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, 2642 1.1 christos shindex)) 2643 1.1 christos goto fail; 2644 1.1 christos 2645 1.1 christos /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we 2646 1.8 christos can't read symbols without that section loaded as well. It 2647 1.8 christos is most likely specified by the next section header. */ 2648 1.8 christos { 2649 1.8 christos elf_section_list * entry; 2650 1.14 christos unsigned int i, num_sec; 2651 1.8 christos 2652 1.8 christos for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next) 2653 1.8 christos if (entry->hdr.sh_link == shindex) 2654 1.8 christos goto success; 2655 1.8 christos 2656 1.8 christos num_sec = elf_numsections (abfd); 2657 1.8 christos for (i = shindex + 1; i < num_sec; i++) 2658 1.8 christos { 2659 1.8 christos Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2660 1.8 christos 2661 1.8 christos if (hdr2->sh_type == SHT_SYMTAB_SHNDX 2662 1.8 christos && hdr2->sh_link == shindex) 2663 1.1 christos break; 2664 1.8 christos } 2665 1.8 christos 2666 1.1 christos if (i == num_sec) 2667 1.1 christos for (i = 1; i < shindex; i++) 2668 1.8 christos { 2669 1.1 christos Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2670 1.1 christos 2671 1.1 christos if (hdr2->sh_type == SHT_SYMTAB_SHNDX 2672 1.1 christos && hdr2->sh_link == shindex) 2673 1.8 christos break; 2674 1.8 christos } 2675 1.8 christos 2676 1.14 christos if (i != shindex) 2677 1.14 christos ret = bfd_section_from_shdr (abfd, i); 2678 1.8 christos /* else FIXME: we have failed to find the symbol table. 2679 1.8 christos Should we issue an error? */ 2680 1.1 christos goto success; 2681 1.5 christos } 2682 1.1 christos 2683 1.5 christos case SHT_DYNSYM: /* A dynamic symbol table. */ 2684 1.1 christos if (elf_dynsymtab (abfd) == shindex) 2685 1.1 christos goto success; 2686 1.5 christos 2687 1.5 christos if (hdr->sh_entsize != bed->s->sizeof_sym) 2688 1.3 christos goto fail; 2689 1.3 christos 2690 1.3 christos if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) 2691 1.5 christos { 2692 1.5 christos if (hdr->sh_size != 0) 2693 1.3 christos goto fail; 2694 1.3 christos 2695 1.3 christos /* Some linkers erroneously set sh_info to one with a 2696 1.3 christos zero sh_size. ld sees this as a global symbol count 2697 1.5 christos of (unsigned) -1. Fix it here. */ 2698 1.3 christos hdr->sh_info = 0; 2699 1.5 christos goto success; 2700 1.8 christos } 2701 1.8 christos 2702 1.8 christos /* PR 18854: A binary might contain more than one dynamic symbol table. 2703 1.8 christos Unusual, but possible. Warn, but continue. */ 2704 1.9 christos if (elf_dynsymtab (abfd) != 0) 2705 1.9 christos { 2706 1.11 christos _bfd_error_handler 2707 1.9 christos /* xgettext:c-format */ 2708 1.8 christos (_("%pB: warning: multiple dynamic symbol tables detected" 2709 1.8 christos " - ignoring the table in section %u"), 2710 1.8 christos abfd, shindex); 2711 1.1 christos goto success; 2712 1.1 christos } 2713 1.1 christos elf_dynsymtab (abfd) = shindex; 2714 1.1 christos elf_tdata (abfd)->dynsymtab_hdr = *hdr; 2715 1.1 christos elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; 2716 1.1 christos abfd->flags |= HAS_SYMS; 2717 1.1 christos 2718 1.5 christos /* Besides being a symbol table, we also treat this as a regular 2719 1.5 christos section, so that objcopy can handle it. */ 2720 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2721 1.14 christos goto success; 2722 1.8 christos 2723 1.8 christos case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ 2724 1.8 christos { 2725 1.14 christos elf_section_list * entry; 2726 1.8 christos 2727 1.8 christos for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next) 2728 1.11 christos if (entry->ndx == shindex) 2729 1.12 christos goto success; 2730 1.8 christos 2731 1.8 christos entry = bfd_alloc (abfd, sizeof (*entry)); 2732 1.8 christos if (entry == NULL) 2733 1.8 christos goto fail; 2734 1.8 christos entry->ndx = shindex; 2735 1.8 christos entry->hdr = * hdr; 2736 1.8 christos entry->next = elf_symtab_shndx_list (abfd); 2737 1.5 christos elf_symtab_shndx_list (abfd) = entry; 2738 1.8 christos elf_elfsections (abfd)[shindex] = & entry->hdr; 2739 1.1 christos goto success; 2740 1.5 christos } 2741 1.1 christos 2742 1.5 christos case SHT_STRTAB: /* A string table. */ 2743 1.5 christos if (hdr->bfd_section != NULL) 2744 1.1 christos goto success; 2745 1.1 christos 2746 1.1 christos if (ehdr->e_shstrndx == shindex) 2747 1.1 christos { 2748 1.5 christos elf_tdata (abfd)->shstrtab_hdr = *hdr; 2749 1.1 christos elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; 2750 1.5 christos goto success; 2751 1.1 christos } 2752 1.1 christos 2753 1.1 christos if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) 2754 1.1 christos { 2755 1.1 christos symtab_strtab: 2756 1.5 christos elf_tdata (abfd)->strtab_hdr = *hdr; 2757 1.1 christos elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; 2758 1.5 christos goto success; 2759 1.1 christos } 2760 1.1 christos 2761 1.1 christos if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) 2762 1.1 christos { 2763 1.1 christos dynsymtab_strtab: 2764 1.1 christos elf_tdata (abfd)->dynstrtab_hdr = *hdr; 2765 1.1 christos hdr = &elf_tdata (abfd)->dynstrtab_hdr; 2766 1.1 christos elf_elfsections (abfd)[shindex] = hdr; 2767 1.5 christos /* We also treat this as a regular section, so that objcopy 2768 1.5 christos can handle it. */ 2769 1.5 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, 2770 1.1 christos shindex); 2771 1.1 christos goto success; 2772 1.1 christos } 2773 1.1 christos 2774 1.1 christos /* If the string table isn't one of the above, then treat it as a 2775 1.1 christos regular section. We need to scan all the headers to be sure, 2776 1.1 christos just in case this strtab section appeared before the above. */ 2777 1.1 christos if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) 2778 1.1 christos { 2779 1.1 christos unsigned int i, num_sec; 2780 1.1 christos 2781 1.1 christos num_sec = elf_numsections (abfd); 2782 1.1 christos for (i = 1; i < num_sec; i++) 2783 1.1 christos { 2784 1.1 christos Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2785 1.1 christos if (hdr2->sh_link == shindex) 2786 1.1 christos { 2787 1.5 christos /* Prevent endless recursion on broken objects. */ 2788 1.1 christos if (i == shindex) 2789 1.5 christos goto fail; 2790 1.1 christos if (! bfd_section_from_shdr (abfd, i)) 2791 1.1 christos goto fail; 2792 1.1 christos if (elf_onesymtab (abfd) == i) 2793 1.1 christos goto symtab_strtab; 2794 1.1 christos if (elf_dynsymtab (abfd) == i) 2795 1.1 christos goto dynsymtab_strtab; 2796 1.1 christos } 2797 1.5 christos } 2798 1.5 christos } 2799 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2800 1.1 christos goto success; 2801 1.1 christos 2802 1.14 christos case SHT_REL: 2803 1.1 christos case SHT_RELA: 2804 1.1 christos case SHT_RELR: 2805 1.1 christos /* *These* do a lot of work -- but build no sections! */ 2806 1.1 christos { 2807 1.1 christos asection *target_sect; 2808 1.1 christos Elf_Internal_Shdr *hdr2, **p_hdr; 2809 1.14 christos unsigned int num_sec = elf_numsections (abfd); 2810 1.1 christos struct bfd_elf_section_data *esdt; 2811 1.14 christos bfd_size_type size; 2812 1.14 christos 2813 1.14 christos if (hdr->sh_type == SHT_REL) 2814 1.14 christos size = bed->s->sizeof_rel; 2815 1.14 christos else if (hdr->sh_type == SHT_RELA) 2816 1.14 christos size = bed->s->sizeof_rela; 2817 1.14 christos else 2818 1.5 christos size = bed->s->arch_size / 8; 2819 1.1 christos if (hdr->sh_entsize != size) 2820 1.1 christos goto fail; 2821 1.1 christos 2822 1.1 christos /* Check for a bogus link to avoid crashing. */ 2823 1.9 christos if (hdr->sh_link >= num_sec) 2824 1.9 christos { 2825 1.11 christos _bfd_error_handler 2826 1.9 christos /* xgettext:c-format */ 2827 1.14 christos (_("%pB: invalid link %u for reloc section %s (index %u)"), 2828 1.5 christos abfd, hdr->sh_link, name, shindex); 2829 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2830 1.1 christos goto success; 2831 1.1 christos } 2832 1.1 christos 2833 1.1 christos /* Get the symbol table. */ 2834 1.1 christos if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB 2835 1.5 christos || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) 2836 1.1 christos && ! bfd_section_from_shdr (abfd, hdr->sh_link)) 2837 1.11 christos goto fail; 2838 1.11 christos 2839 1.11 christos /* If this is an alloc section in an executable or shared 2840 1.11 christos library, or the reloc section does not use the main symbol 2841 1.11 christos table we don't treat it as a reloc section. BFD can't 2842 1.11 christos adequately represent such a section, so at least for now, 2843 1.11 christos we don't try. We just present it as a normal section. We 2844 1.11 christos also can't use it as a reloc section if it points to the 2845 1.11 christos null section, an invalid section, another reloc section, or 2846 1.11 christos its sh_link points to the null section. */ 2847 1.17 christos if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0 2848 1.14 christos && (hdr->sh_flags & SHF_ALLOC) != 0) 2849 1.1 christos || (hdr->sh_flags & SHF_COMPRESSED) != 0 2850 1.11 christos || hdr->sh_type == SHT_RELR 2851 1.1 christos || hdr->sh_link == SHN_UNDEF 2852 1.1 christos || hdr->sh_link != elf_onesymtab (abfd) 2853 1.1 christos || hdr->sh_info == SHN_UNDEF 2854 1.1 christos || hdr->sh_info >= num_sec 2855 1.5 christos || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL 2856 1.14 christos || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) 2857 1.5 christos { 2858 1.5 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2859 1.1 christos goto success; 2860 1.1 christos } 2861 1.5 christos 2862 1.5 christos if (! bfd_section_from_shdr (abfd, hdr->sh_info)) 2863 1.1 christos goto fail; 2864 1.1 christos 2865 1.5 christos target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); 2866 1.1 christos if (target_sect == NULL) 2867 1.1 christos goto fail; 2868 1.1 christos 2869 1.1 christos esdt = elf_section_data (target_sect); 2870 1.1 christos if (hdr->sh_type == SHT_RELA) 2871 1.1 christos p_hdr = &esdt->rela.hdr; 2872 1.1 christos else 2873 1.12 christos p_hdr = &esdt->rel.hdr; 2874 1.12 christos 2875 1.12 christos /* PR 17512: file: 0b4f81b7. 2876 1.5 christos Also see PR 24456, for a file which deliberately has two reloc 2877 1.12 christos sections. */ 2878 1.12 christos if (*p_hdr != NULL) 2879 1.12 christos { 2880 1.12 christos if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex)) 2881 1.12 christos { 2882 1.12 christos _bfd_error_handler 2883 1.12 christos /* xgettext:c-format */ 2884 1.12 christos (_("%pB: warning: secondary relocation section '%s' " 2885 1.12 christos "for section %pA found - ignoring"), 2886 1.14 christos abfd, name, target_sect); 2887 1.14 christos } 2888 1.12 christos else 2889 1.12 christos esdt->has_secondary_relocs = true; 2890 1.12 christos goto success; 2891 1.8 christos } 2892 1.1 christos 2893 1.5 christos hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); 2894 1.1 christos if (hdr2 == NULL) 2895 1.1 christos goto fail; 2896 1.1 christos *hdr2 = *hdr; 2897 1.11 christos *p_hdr = hdr2; 2898 1.11 christos elf_elfsections (abfd)[shindex] = hdr2; 2899 1.1 christos target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr) 2900 1.1 christos * bed->s->int_rels_per_ext_rel); 2901 1.1 christos target_sect->flags |= SEC_RELOC; 2902 1.1 christos target_sect->relocation = NULL; 2903 1.1 christos target_sect->rel_filepos = hdr->sh_offset; 2904 1.1 christos /* In the section to which the relocations apply, mark whether 2905 1.1 christos its relocations are of the REL or RELA variety. */ 2906 1.1 christos if (hdr->sh_size != 0) 2907 1.1 christos { 2908 1.1 christos if (hdr->sh_type == SHT_RELA) 2909 1.1 christos target_sect->use_rela_p = 1; 2910 1.5 christos } 2911 1.1 christos abfd->flags |= HAS_RELOC; 2912 1.1 christos goto success; 2913 1.1 christos } 2914 1.14 christos 2915 1.14 christos case SHT_GNU_verdef: 2916 1.1 christos if (hdr->sh_info != 0) 2917 1.5 christos elf_dynverdef (abfd) = shindex; 2918 1.5 christos elf_tdata (abfd)->dynverdef_hdr = *hdr; 2919 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2920 1.1 christos goto success; 2921 1.1 christos 2922 1.5 christos case SHT_GNU_versym: 2923 1.5 christos if (hdr->sh_entsize != sizeof (Elf_External_Versym)) 2924 1.1 christos goto fail; 2925 1.1 christos 2926 1.5 christos elf_dynversym (abfd) = shindex; 2927 1.5 christos elf_tdata (abfd)->dynversym_hdr = *hdr; 2928 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2929 1.1 christos goto success; 2930 1.14 christos 2931 1.14 christos case SHT_GNU_verneed: 2932 1.1 christos if (hdr->sh_info != 0) 2933 1.5 christos elf_dynverref (abfd) = shindex; 2934 1.5 christos elf_tdata (abfd)->dynverref_hdr = *hdr; 2935 1.1 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2936 1.1 christos goto success; 2937 1.5 christos 2938 1.1 christos case SHT_SHLIB: 2939 1.1 christos goto success; 2940 1.1 christos 2941 1.5 christos case SHT_GROUP: 2942 1.5 christos if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 2943 1.5 christos goto fail; 2944 1.1 christos 2945 1.1 christos goto success; 2946 1.1 christos 2947 1.19 christos default: 2948 1.19 christos /* Possibly an attributes section. */ 2949 1.19 christos if (get_elf_backend_data (abfd)->target_os != is_solaris 2950 1.19 christos /* PR 33153: Solaris defines SHT_SUNW_cap which collides with SHT_GNU_ATTRIBUTES. */ 2951 1.1 christos && (hdr->sh_type == SHT_GNU_ATTRIBUTES 2952 1.1 christos || hdr->sh_type == bed->obj_attrs_section_type)) 2953 1.5 christos { 2954 1.1 christos if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 2955 1.5 christos goto fail; 2956 1.1 christos _bfd_elf_parse_attributes (abfd, hdr); 2957 1.1 christos goto success; 2958 1.1 christos } 2959 1.1 christos 2960 1.5 christos /* Check for any processor-specific section types. */ 2961 1.1 christos if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) 2962 1.1 christos goto success; 2963 1.1 christos 2964 1.1 christos if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) 2965 1.1 christos { 2966 1.1 christos if ((hdr->sh_flags & SHF_ALLOC) != 0) 2967 1.9 christos /* FIXME: How to properly handle allocated section reserved 2968 1.9 christos for applications? */ 2969 1.11 christos _bfd_error_handler 2970 1.11 christos /* xgettext:c-format */ 2971 1.1 christos (_("%pB: unknown type [%#x] section `%s'"), 2972 1.5 christos abfd, hdr->sh_type, name); 2973 1.5 christos else 2974 1.14 christos { 2975 1.5 christos /* Allow sections reserved for applications. */ 2976 1.5 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2977 1.1 christos goto success; 2978 1.1 christos } 2979 1.1 christos } 2980 1.1 christos else if (hdr->sh_type >= SHT_LOPROC 2981 1.9 christos && hdr->sh_type <= SHT_HIPROC) 2982 1.9 christos /* FIXME: We should handle this section. */ 2983 1.11 christos _bfd_error_handler 2984 1.11 christos /* xgettext:c-format */ 2985 1.1 christos (_("%pB: unknown type [%#x] section `%s'"), 2986 1.1 christos abfd, hdr->sh_type, name); 2987 1.1 christos else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) 2988 1.1 christos { 2989 1.1 christos /* Unrecognised OS-specific sections. */ 2990 1.1 christos if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) 2991 1.1 christos /* SHF_OS_NONCONFORMING indicates that special knowledge is 2992 1.9 christos required to correctly process the section and the file should 2993 1.9 christos be rejected with an error message. */ 2994 1.11 christos _bfd_error_handler 2995 1.11 christos /* xgettext:c-format */ 2996 1.1 christos (_("%pB: unknown type [%#x] section `%s'"), 2997 1.5 christos abfd, hdr->sh_type, name); 2998 1.5 christos else 2999 1.5 christos { 3000 1.5 christos /* Otherwise it should be processed. */ 3001 1.5 christos ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 3002 1.1 christos goto success; 3003 1.1 christos } 3004 1.1 christos } 3005 1.9 christos else 3006 1.9 christos /* FIXME: We should handle this section. */ 3007 1.11 christos _bfd_error_handler 3008 1.11 christos /* xgettext:c-format */ 3009 1.1 christos (_("%pB: unknown type [%#x] section `%s'"), 3010 1.5 christos abfd, hdr->sh_type, name); 3011 1.1 christos 3012 1.1 christos goto fail; 3013 1.5 christos } 3014 1.14 christos 3015 1.5 christos fail: 3016 1.14 christos ret = false; 3017 1.5 christos success: 3018 1.1 christos elf_tdata (abfd)->being_created[shindex] = false; 3019 1.1 christos return ret; 3020 1.1 christos } 3021 1.1 christos 3022 1.1 christos /* Return the local symbol specified by ABFD, R_SYMNDX. */ 3023 1.1 christos 3024 1.1 christos Elf_Internal_Sym * 3025 1.1 christos bfd_sym_from_r_symndx (struct sym_cache *cache, 3026 1.1 christos bfd *abfd, 3027 1.1 christos unsigned long r_symndx) 3028 1.1 christos { 3029 1.1 christos unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; 3030 1.1 christos 3031 1.1 christos if (cache->abfd != abfd || cache->indx[ent] != r_symndx) 3032 1.1 christos { 3033 1.1 christos Elf_Internal_Shdr *symtab_hdr; 3034 1.1 christos unsigned char esym[sizeof (Elf64_External_Sym)]; 3035 1.1 christos Elf_External_Sym_Shndx eshndx; 3036 1.1 christos 3037 1.1 christos symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 3038 1.1 christos if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, 3039 1.1 christos &cache->sym[ent], esym, &eshndx) == NULL) 3040 1.1 christos return NULL; 3041 1.1 christos 3042 1.1 christos if (cache->abfd != abfd) 3043 1.1 christos { 3044 1.1 christos memset (cache->indx, -1, sizeof (cache->indx)); 3045 1.1 christos cache->abfd = abfd; 3046 1.1 christos } 3047 1.1 christos cache->indx[ent] = r_symndx; 3048 1.1 christos } 3049 1.1 christos 3050 1.1 christos return &cache->sym[ent]; 3051 1.1 christos } 3052 1.1 christos 3053 1.1 christos /* Given an ELF section number, retrieve the corresponding BFD 3054 1.1 christos section. */ 3055 1.1 christos 3056 1.1 christos asection * 3057 1.1 christos bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) 3058 1.1 christos { 3059 1.1 christos if (sec_index >= elf_numsections (abfd)) 3060 1.1 christos return NULL; 3061 1.1 christos return elf_elfsections (abfd)[sec_index]->bfd_section; 3062 1.1 christos } 3063 1.1 christos 3064 1.1 christos static const struct bfd_elf_special_section special_sections_b[] = 3065 1.11 christos { 3066 1.1 christos { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3067 1.1 christos { NULL, 0, 0, 0, 0 } 3068 1.1 christos }; 3069 1.1 christos 3070 1.1 christos static const struct bfd_elf_special_section special_sections_c[] = 3071 1.12 christos { 3072 1.11 christos { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, 3073 1.1 christos { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 }, 3074 1.1 christos { NULL, 0, 0, 0, 0 } 3075 1.1 christos }; 3076 1.1 christos 3077 1.11 christos static const struct bfd_elf_special_section special_sections_d[] = 3078 1.11 christos { 3079 1.3 christos { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3080 1.3 christos { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3081 1.3 christos /* There are more DWARF sections than these, but they needn't be added here 3082 1.11 christos unless you have to cope with broken compilers that don't emit section 3083 1.11 christos attributes or you want to help the user writing assembler. */ 3084 1.11 christos { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, 3085 1.11 christos { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, 3086 1.1 christos { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, 3087 1.11 christos { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, 3088 1.11 christos { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, 3089 1.11 christos { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, 3090 1.11 christos { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, 3091 1.1 christos { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, 3092 1.1 christos { NULL, 0, 0, 0, 0 } 3093 1.1 christos }; 3094 1.1 christos 3095 1.11 christos static const struct bfd_elf_special_section special_sections_f[] = 3096 1.9 christos { 3097 1.11 christos { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 3098 1.1 christos { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, 3099 1.1 christos { NULL, 0 , 0, 0, 0 } 3100 1.1 christos }; 3101 1.1 christos 3102 1.1 christos static const struct bfd_elf_special_section special_sections_g[] = 3103 1.14 christos { 3104 1.14 christos { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3105 1.11 christos { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3106 1.11 christos { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3107 1.19 christos { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, 3108 1.11 christos { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3109 1.1 christos { STRING_COMMA_LEN (".gnu_object_only"), 0, SHT_GNU_OBJECT_ONLY, SHF_EXCLUDE }, 3110 1.1 christos { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, 3111 1.11 christos { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, 3112 1.11 christos { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, 3113 1.11 christos { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, 3114 1.11 christos { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, 3115 1.1 christos { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, 3116 1.1 christos { NULL, 0, 0, 0, 0 } 3117 1.1 christos }; 3118 1.1 christos 3119 1.11 christos static const struct bfd_elf_special_section special_sections_h[] = 3120 1.11 christos { 3121 1.1 christos { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, 3122 1.1 christos { NULL, 0, 0, 0, 0 } 3123 1.1 christos }; 3124 1.1 christos 3125 1.11 christos static const struct bfd_elf_special_section special_sections_i[] = 3126 1.9 christos { 3127 1.11 christos { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 3128 1.11 christos { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, 3129 1.1 christos { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, 3130 1.1 christos { NULL, 0, 0, 0, 0 } 3131 1.1 christos }; 3132 1.1 christos 3133 1.1 christos static const struct bfd_elf_special_section special_sections_l[] = 3134 1.11 christos { 3135 1.1 christos { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, 3136 1.1 christos { NULL, 0, 0, 0, 0 } 3137 1.1 christos }; 3138 1.1 christos 3139 1.14 christos static const struct bfd_elf_special_section special_sections_n[] = 3140 1.1 christos { 3141 1.11 christos { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3142 1.11 christos { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, 3143 1.1 christos { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, 3144 1.1 christos { NULL, 0, 0, 0, 0 } 3145 1.1 christos }; 3146 1.1 christos 3147 1.14 christos static const struct bfd_elf_special_section special_sections_p[] = 3148 1.14 christos { 3149 1.9 christos { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3150 1.11 christos { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3151 1.11 christos { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, 3152 1.1 christos { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 3153 1.1 christos { NULL, 0, 0, 0, 0 } 3154 1.1 christos }; 3155 1.1 christos 3156 1.1 christos static const struct bfd_elf_special_section special_sections_r[] = 3157 1.1 christos { 3158 1.14 christos { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, 3159 1.11 christos { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, 3160 1.18 christos { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC }, 3161 1.18 christos { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, 3162 1.18 christos /* .relro_padding is generated by lld. It should not be confused with a 3163 1.18 christos reloc containing section, because otherwise elf_fake_sections() will 3164 1.18 christos set the entsize to 8, which may not be an actual multiple of the 3165 1.18 christos section's size. 3166 1.11 christos Note - this entry must appear before the ".rel" entry below. */ 3167 1.11 christos { STRING_COMMA_LEN (".relro_padding"), 0, SHT_NOBITS, SHF_ALLOC | SHF_WRITE }, 3168 1.1 christos { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, 3169 1.1 christos { NULL, 0, 0, 0, 0 } 3170 1.1 christos }; 3171 1.1 christos 3172 1.1 christos static const struct bfd_elf_special_section special_sections_s[] = 3173 1.1 christos { 3174 1.1 christos { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, 3175 1.1 christos { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, 3176 1.1 christos { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, 3177 1.1 christos /* See struct bfd_elf_special_section declaration for the semantics of 3178 1.11 christos this special case where .prefix_length != strlen (.prefix). */ 3179 1.1 christos { ".stabstr", 5, 3, SHT_STRTAB, 0 }, 3180 1.1 christos { NULL, 0, 0, 0, 0 } 3181 1.1 christos }; 3182 1.1 christos 3183 1.11 christos static const struct bfd_elf_special_section special_sections_t[] = 3184 1.11 christos { 3185 1.1 christos { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 3186 1.11 christos { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, 3187 1.1 christos { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, 3188 1.1 christos { NULL, 0, 0, 0, 0 } 3189 1.1 christos }; 3190 1.1 christos 3191 1.11 christos static const struct bfd_elf_special_section special_sections_z[] = 3192 1.11 christos { 3193 1.1 christos { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, 3194 1.1 christos { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, 3195 1.11 christos { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, 3196 1.1 christos { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, 3197 1.1 christos { NULL, 0, 0, 0, 0 } 3198 1.3 christos }; 3199 1.1 christos 3200 1.1 christos static const struct bfd_elf_special_section * const special_sections[] = 3201 1.1 christos { 3202 1.1 christos special_sections_b, /* 'b' */ 3203 1.1 christos special_sections_c, /* 'c' */ 3204 1.1 christos special_sections_d, /* 'd' */ 3205 1.1 christos NULL, /* 'e' */ 3206 1.1 christos special_sections_f, /* 'f' */ 3207 1.1 christos special_sections_g, /* 'g' */ 3208 1.1 christos special_sections_h, /* 'h' */ 3209 1.1 christos special_sections_i, /* 'i' */ 3210 1.1 christos NULL, /* 'j' */ 3211 1.1 christos NULL, /* 'k' */ 3212 1.1 christos special_sections_l, /* 'l' */ 3213 1.1 christos NULL, /* 'm' */ 3214 1.1 christos special_sections_n, /* 'n' */ 3215 1.1 christos NULL, /* 'o' */ 3216 1.1 christos special_sections_p, /* 'p' */ 3217 1.1 christos NULL, /* 'q' */ 3218 1.1 christos special_sections_r, /* 'r' */ 3219 1.1 christos special_sections_s, /* 's' */ 3220 1.1 christos special_sections_t, /* 't' */ 3221 1.1 christos NULL, /* 'u' */ 3222 1.1 christos NULL, /* 'v' */ 3223 1.1 christos NULL, /* 'w' */ 3224 1.1 christos NULL, /* 'x' */ 3225 1.1 christos NULL, /* 'y' */ 3226 1.1 christos special_sections_z /* 'z' */ 3227 1.1 christos }; 3228 1.1 christos 3229 1.1 christos const struct bfd_elf_special_section * 3230 1.1 christos _bfd_elf_get_special_section (const char *name, 3231 1.1 christos const struct bfd_elf_special_section *spec, 3232 1.1 christos unsigned int rela) 3233 1.1 christos { 3234 1.1 christos int i; 3235 1.1 christos int len; 3236 1.1 christos 3237 1.1 christos len = strlen (name); 3238 1.1 christos 3239 1.1 christos for (i = 0; spec[i].prefix != NULL; i++) 3240 1.1 christos { 3241 1.1 christos int suffix_len; 3242 1.1 christos int prefix_len = spec[i].prefix_length; 3243 1.1 christos 3244 1.1 christos if (len < prefix_len) 3245 1.1 christos continue; 3246 1.1 christos if (memcmp (name, spec[i].prefix, prefix_len) != 0) 3247 1.1 christos continue; 3248 1.1 christos 3249 1.1 christos suffix_len = spec[i].suffix_length; 3250 1.1 christos if (suffix_len <= 0) 3251 1.1 christos { 3252 1.1 christos if (name[prefix_len] != 0) 3253 1.1 christos { 3254 1.1 christos if (suffix_len == 0) 3255 1.1 christos continue; 3256 1.1 christos if (name[prefix_len] != '.' 3257 1.1 christos && (suffix_len == -2 3258 1.1 christos || (rela && spec[i].type == SHT_REL))) 3259 1.1 christos continue; 3260 1.1 christos } 3261 1.1 christos } 3262 1.1 christos else 3263 1.1 christos { 3264 1.1 christos if (len < prefix_len + suffix_len) 3265 1.1 christos continue; 3266 1.1 christos if (memcmp (name + len - suffix_len, 3267 1.1 christos spec[i].prefix + prefix_len, 3268 1.1 christos suffix_len) != 0) 3269 1.1 christos continue; 3270 1.1 christos } 3271 1.1 christos return &spec[i]; 3272 1.1 christos } 3273 1.1 christos 3274 1.1 christos return NULL; 3275 1.1 christos } 3276 1.1 christos 3277 1.1 christos const struct bfd_elf_special_section * 3278 1.1 christos _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) 3279 1.1 christos { 3280 1.1 christos int i; 3281 1.1 christos const struct bfd_elf_special_section *spec; 3282 1.1 christos const struct elf_backend_data *bed; 3283 1.1 christos 3284 1.1 christos /* See if this is one of the special sections. */ 3285 1.1 christos if (sec->name == NULL) 3286 1.1 christos return NULL; 3287 1.1 christos 3288 1.1 christos bed = get_elf_backend_data (abfd); 3289 1.1 christos spec = bed->special_sections; 3290 1.1 christos if (spec) 3291 1.1 christos { 3292 1.1 christos spec = _bfd_elf_get_special_section (sec->name, 3293 1.1 christos bed->special_sections, 3294 1.1 christos sec->use_rela_p); 3295 1.1 christos if (spec != NULL) 3296 1.1 christos return spec; 3297 1.1 christos } 3298 1.1 christos 3299 1.1 christos if (sec->name[0] != '.') 3300 1.1 christos return NULL; 3301 1.1 christos 3302 1.1 christos i = sec->name[1] - 'b'; 3303 1.1 christos if (i < 0 || i > 'z' - 'b') 3304 1.1 christos return NULL; 3305 1.1 christos 3306 1.1 christos spec = special_sections[i]; 3307 1.1 christos 3308 1.1 christos if (spec == NULL) 3309 1.1 christos return NULL; 3310 1.1 christos 3311 1.1 christos return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); 3312 1.14 christos } 3313 1.1 christos 3314 1.1 christos bool 3315 1.1 christos _bfd_elf_new_section_hook (bfd *abfd, asection *sec) 3316 1.1 christos { 3317 1.1 christos struct bfd_elf_section_data *sdata; 3318 1.1 christos const struct elf_backend_data *bed; 3319 1.1 christos const struct bfd_elf_special_section *ssect; 3320 1.1 christos 3321 1.1 christos sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; 3322 1.1 christos if (sdata == NULL) 3323 1.11 christos { 3324 1.1 christos sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, 3325 1.14 christos sizeof (*sdata)); 3326 1.1 christos if (sdata == NULL) 3327 1.1 christos return false; 3328 1.1 christos sec->used_by_bfd = sdata; 3329 1.1 christos } 3330 1.1 christos 3331 1.1 christos /* Indicate whether or not this section should use RELA relocations. */ 3332 1.1 christos bed = get_elf_backend_data (abfd); 3333 1.12 christos sec->use_rela_p = bed->default_use_rela_p; 3334 1.12 christos 3335 1.12 christos /* Set up ELF section type and flags for newly created sections, if 3336 1.12 christos there is an ABI mandated section. */ 3337 1.12 christos ssect = (*bed->get_sec_type_attr) (abfd, sec); 3338 1.12 christos if (ssect != NULL) 3339 1.12 christos { 3340 1.1 christos elf_section_type (sec) = ssect->type; 3341 1.1 christos elf_section_flags (sec) = ssect->attr; 3342 1.1 christos } 3343 1.1 christos 3344 1.1 christos return _bfd_generic_new_section_hook (abfd, sec); 3345 1.1 christos } 3346 1.1 christos 3347 1.1 christos /* Create a new bfd section from an ELF program header. 3348 1.1 christos 3349 1.1 christos Since program segments have no names, we generate a synthetic name 3350 1.1 christos of the form segment<NUM>, where NUM is generally the index in the 3351 1.1 christos program header table. For segments that are split (see below) we 3352 1.1 christos generate the names segment<NUM>a and segment<NUM>b. 3353 1.1 christos 3354 1.1 christos Note that some program segments may have a file size that is different than 3355 1.1 christos (less than) the memory size. All this means is that at execution the 3356 1.1 christos system must allocate the amount of memory specified by the memory size, 3357 1.1 christos but only initialize it with the first "file size" bytes read from the 3358 1.1 christos file. This would occur for example, with program segments consisting 3359 1.1 christos of combined data+bss. 3360 1.1 christos 3361 1.1 christos To handle the above situation, this routine generates TWO bfd sections 3362 1.1 christos for the single program segment. The first has the length specified by 3363 1.14 christos the file size of the segment, and the second has the length specified 3364 1.1 christos by the difference between the two sizes. In effect, the segment is split 3365 1.14 christos into its initialized and uninitialized parts. */ 3366 1.1 christos 3367 1.1 christos bool 3368 1.1 christos _bfd_elf_make_section_from_phdr (bfd *abfd, 3369 1.1 christos Elf_Internal_Phdr *hdr, 3370 1.1 christos int hdr_index, 3371 1.1 christos const char *type_name) 3372 1.1 christos { 3373 1.1 christos asection *newsect; 3374 1.1 christos char *name; 3375 1.1 christos char namebuf[64]; 3376 1.12 christos size_t len; 3377 1.1 christos int split; 3378 1.1 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL); 3379 1.1 christos 3380 1.1 christos split = ((hdr->p_memsz > 0) 3381 1.1 christos && (hdr->p_filesz > 0) 3382 1.1 christos && (hdr->p_memsz > hdr->p_filesz)); 3383 1.1 christos 3384 1.1 christos if (hdr->p_filesz > 0) 3385 1.1 christos { 3386 1.1 christos sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); 3387 1.1 christos len = strlen (namebuf) + 1; 3388 1.14 christos name = (char *) bfd_alloc (abfd, len); 3389 1.1 christos if (!name) 3390 1.1 christos return false; 3391 1.1 christos memcpy (name, namebuf, len); 3392 1.14 christos newsect = bfd_make_section (abfd, name); 3393 1.12 christos if (newsect == NULL) 3394 1.12 christos return false; 3395 1.1 christos newsect->vma = hdr->p_vaddr / opb; 3396 1.1 christos newsect->lma = hdr->p_paddr / opb; 3397 1.1 christos newsect->size = hdr->p_filesz; 3398 1.1 christos newsect->filepos = hdr->p_offset; 3399 1.1 christos newsect->flags |= SEC_HAS_CONTENTS; 3400 1.1 christos newsect->alignment_power = bfd_log2 (hdr->p_align); 3401 1.1 christos if (hdr->p_type == PT_LOAD) 3402 1.1 christos { 3403 1.1 christos newsect->flags |= SEC_ALLOC; 3404 1.1 christos newsect->flags |= SEC_LOAD; 3405 1.1 christos if (hdr->p_flags & PF_X) 3406 1.1 christos { 3407 1.1 christos /* FIXME: all we known is that it has execute PERMISSION, 3408 1.1 christos may be data. */ 3409 1.1 christos newsect->flags |= SEC_CODE; 3410 1.1 christos } 3411 1.1 christos } 3412 1.1 christos if (!(hdr->p_flags & PF_W)) 3413 1.1 christos { 3414 1.1 christos newsect->flags |= SEC_READONLY; 3415 1.1 christos } 3416 1.1 christos } 3417 1.1 christos 3418 1.1 christos if (hdr->p_memsz > hdr->p_filesz) 3419 1.1 christos { 3420 1.1 christos bfd_vma align; 3421 1.1 christos 3422 1.1 christos sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); 3423 1.1 christos len = strlen (namebuf) + 1; 3424 1.14 christos name = (char *) bfd_alloc (abfd, len); 3425 1.1 christos if (!name) 3426 1.1 christos return false; 3427 1.1 christos memcpy (name, namebuf, len); 3428 1.14 christos newsect = bfd_make_section (abfd, name); 3429 1.12 christos if (newsect == NULL) 3430 1.12 christos return false; 3431 1.1 christos newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb; 3432 1.1 christos newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb; 3433 1.1 christos newsect->size = hdr->p_memsz - hdr->p_filesz; 3434 1.1 christos newsect->filepos = hdr->p_offset + hdr->p_filesz; 3435 1.1 christos align = newsect->vma & -newsect->vma; 3436 1.1 christos if (align == 0 || align > hdr->p_align) 3437 1.1 christos align = hdr->p_align; 3438 1.1 christos newsect->alignment_power = bfd_log2 (align); 3439 1.1 christos if (hdr->p_type == PT_LOAD) 3440 1.1 christos { 3441 1.1 christos newsect->flags |= SEC_ALLOC; 3442 1.1 christos if (hdr->p_flags & PF_X) 3443 1.1 christos newsect->flags |= SEC_CODE; 3444 1.1 christos } 3445 1.1 christos if (!(hdr->p_flags & PF_W)) 3446 1.1 christos newsect->flags |= SEC_READONLY; 3447 1.14 christos } 3448 1.1 christos 3449 1.1 christos return true; 3450 1.14 christos } 3451 1.12 christos 3452 1.12 christos static bool 3453 1.12 christos _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset) 3454 1.12 christos { 3455 1.12 christos /* The return value is ignored. Build-ids are considered optional. */ 3456 1.12 christos if (templ->xvec->flavour == bfd_target_elf_flavour) 3457 1.14 christos return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id) 3458 1.12 christos (templ, offset); 3459 1.12 christos return false; 3460 1.14 christos } 3461 1.1 christos 3462 1.1 christos bool 3463 1.1 christos bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) 3464 1.1 christos { 3465 1.1 christos const struct elf_backend_data *bed; 3466 1.1 christos 3467 1.1 christos switch (hdr->p_type) 3468 1.1 christos { 3469 1.1 christos case PT_NULL: 3470 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); 3471 1.12 christos 3472 1.14 christos case PT_LOAD: 3473 1.12 christos if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load")) 3474 1.12 christos return false; 3475 1.14 christos if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL) 3476 1.1 christos _bfd_elf_core_find_build_id (abfd, hdr->p_offset); 3477 1.1 christos return true; 3478 1.1 christos 3479 1.1 christos case PT_DYNAMIC: 3480 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); 3481 1.1 christos 3482 1.1 christos case PT_INTERP: 3483 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); 3484 1.1 christos 3485 1.14 christos case PT_NOTE: 3486 1.11 christos if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) 3487 1.11 christos return false; 3488 1.14 christos if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz, 3489 1.14 christos hdr->p_align)) 3490 1.1 christos return false; 3491 1.1 christos return true; 3492 1.1 christos 3493 1.1 christos case PT_SHLIB: 3494 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); 3495 1.1 christos 3496 1.1 christos case PT_PHDR: 3497 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); 3498 1.1 christos 3499 1.1 christos case PT_GNU_EH_FRAME: 3500 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, 3501 1.1 christos "eh_frame_hdr"); 3502 1.1 christos 3503 1.1 christos case PT_GNU_STACK: 3504 1.1 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); 3505 1.1 christos 3506 1.1 christos case PT_GNU_RELRO: 3507 1.14 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); 3508 1.14 christos 3509 1.14 christos case PT_GNU_SFRAME: 3510 1.14 christos return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, 3511 1.1 christos "sframe"); 3512 1.1 christos 3513 1.1 christos default: 3514 1.1 christos /* Check for any processor-specific program segment types. */ 3515 1.1 christos bed = get_elf_backend_data (abfd); 3516 1.1 christos return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); 3517 1.1 christos } 3518 1.1 christos } 3519 1.1 christos 3520 1.1 christos /* Return the REL_HDR for SEC, assuming there is only a single one, either 3521 1.1 christos REL or RELA. */ 3522 1.1 christos 3523 1.1 christos Elf_Internal_Shdr * 3524 1.1 christos _bfd_elf_single_rel_hdr (asection *sec) 3525 1.1 christos { 3526 1.1 christos if (elf_section_data (sec)->rel.hdr) 3527 1.1 christos { 3528 1.1 christos BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); 3529 1.1 christos return elf_section_data (sec)->rel.hdr; 3530 1.1 christos } 3531 1.1 christos else 3532 1.1 christos return elf_section_data (sec)->rela.hdr; 3533 1.14 christos } 3534 1.6 christos 3535 1.6 christos static bool 3536 1.6 christos _bfd_elf_set_reloc_sh_name (bfd *abfd, 3537 1.14 christos Elf_Internal_Shdr *rel_hdr, 3538 1.6 christos const char *sec_name, 3539 1.6 christos bool use_rela_p) 3540 1.6 christos { 3541 1.6 christos char *name = (char *) bfd_alloc (abfd, 3542 1.14 christos sizeof ".rela" + strlen (sec_name)); 3543 1.6 christos if (name == NULL) 3544 1.6 christos return false; 3545 1.6 christos 3546 1.6 christos sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); 3547 1.14 christos rel_hdr->sh_name = 3548 1.6 christos (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, 3549 1.14 christos false); 3550 1.6 christos if (rel_hdr->sh_name == (unsigned int) -1) 3551 1.14 christos return false; 3552 1.6 christos 3553 1.6 christos return true; 3554 1.1 christos } 3555 1.1 christos 3556 1.1 christos /* Allocate and initialize a section-header for a new reloc section, 3557 1.1 christos containing relocations against ASECT. It is stored in RELDATA. If 3558 1.1 christos USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL 3559 1.14 christos relocations. */ 3560 1.1 christos 3561 1.1 christos static bool 3562 1.6 christos _bfd_elf_init_reloc_shdr (bfd *abfd, 3563 1.14 christos struct bfd_elf_section_reloc_data *reldata, 3564 1.19 christos const char *sec_name, 3565 1.1 christos bool use_rela_p, 3566 1.1 christos bool delay_sh_name_p) 3567 1.1 christos { 3568 1.1 christos Elf_Internal_Shdr *rel_hdr; 3569 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 3570 1.8 christos 3571 1.17 christos BFD_ASSERT (reldata->hdr == NULL); 3572 1.17 christos rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); 3573 1.1 christos if (rel_hdr == NULL) 3574 1.1 christos return false; 3575 1.19 christos reldata->hdr = rel_hdr; 3576 1.6 christos 3577 1.6 christos if (delay_sh_name_p) 3578 1.6 christos rel_hdr->sh_name = (unsigned int) -1; 3579 1.14 christos else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, 3580 1.1 christos use_rela_p)) 3581 1.1 christos return false; 3582 1.1 christos rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; 3583 1.1 christos rel_hdr->sh_entsize = (use_rela_p 3584 1.1 christos ? bed->s->sizeof_rela 3585 1.1 christos : bed->s->sizeof_rel); 3586 1.1 christos rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; 3587 1.1 christos rel_hdr->sh_flags = 0; 3588 1.1 christos rel_hdr->sh_addr = 0; 3589 1.1 christos rel_hdr->sh_size = 0; 3590 1.14 christos rel_hdr->sh_offset = 0; 3591 1.1 christos 3592 1.1 christos return true; 3593 1.1 christos } 3594 1.1 christos 3595 1.1 christos /* Return the default section type based on the passed in section flags. */ 3596 1.1 christos 3597 1.1 christos int 3598 1.11 christos bfd_elf_get_default_section_type (flagword flags) 3599 1.1 christos { 3600 1.1 christos if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0 3601 1.1 christos && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) 3602 1.1 christos return SHT_NOBITS; 3603 1.1 christos return SHT_PROGBITS; 3604 1.1 christos } 3605 1.1 christos 3606 1.1 christos struct fake_section_arg 3607 1.14 christos { 3608 1.1 christos struct bfd_link_info *link_info; 3609 1.1 christos bool failed; 3610 1.1 christos }; 3611 1.1 christos 3612 1.1 christos /* Set up an ELF internal section header for a section. */ 3613 1.1 christos 3614 1.1 christos static void 3615 1.1 christos elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) 3616 1.1 christos { 3617 1.1 christos struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; 3618 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 3619 1.1 christos struct bfd_elf_section_data *esd = elf_section_data (asect); 3620 1.6 christos Elf_Internal_Shdr *this_hdr; 3621 1.19 christos unsigned int sh_type; 3622 1.12 christos const char *name = asect->name; 3623 1.1 christos bool delay_sh_name_p = false; 3624 1.1 christos bfd_vma mask; 3625 1.1 christos 3626 1.1 christos if (arg->failed) 3627 1.1 christos { 3628 1.1 christos /* We already failed; just get out of the bfd_map_over_sections 3629 1.1 christos loop. */ 3630 1.1 christos return; 3631 1.1 christos } 3632 1.1 christos 3633 1.14 christos this_hdr = &esd->this_hdr; 3634 1.14 christos 3635 1.14 christos /* ld: compress DWARF debug sections with names: .debug_*. */ 3636 1.14 christos if (arg->link_info 3637 1.19 christos && (abfd->flags & BFD_COMPRESS) != 0 3638 1.19 christos && (asect->flags & SEC_DEBUGGING) != 0 3639 1.14 christos && (asect->flags & SEC_ALLOC) == 0 3640 1.14 christos && (asect->flags & SEC_HAS_CONTENTS) != 0 3641 1.14 christos && name[1] == 'd' 3642 1.14 christos && name[6] == '_') 3643 1.14 christos { 3644 1.14 christos /* If this section will be compressed, delay adding section 3645 1.19 christos name to section name section after it is compressed in 3646 1.6 christos _bfd_elf_assign_file_positions_for_non_load. */ 3647 1.6 christos delay_sh_name_p = true; 3648 1.19 christos } 3649 1.6 christos 3650 1.6 christos if (delay_sh_name_p) 3651 1.1 christos this_hdr->sh_name = (unsigned int) -1; 3652 1.6 christos else 3653 1.6 christos { 3654 1.14 christos this_hdr->sh_name 3655 1.6 christos = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 3656 1.6 christos name, false); 3657 1.14 christos if (this_hdr->sh_name == (unsigned int) -1) 3658 1.6 christos { 3659 1.6 christos arg->failed = true; 3660 1.1 christos return; 3661 1.1 christos } 3662 1.1 christos } 3663 1.1 christos 3664 1.1 christos /* Don't clear sh_flags. Assembler may set additional bits. */ 3665 1.1 christos 3666 1.12 christos if ((asect->flags & SEC_ALLOC) != 0 3667 1.1 christos || asect->user_set_vma) 3668 1.1 christos this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect); 3669 1.1 christos else 3670 1.1 christos this_hdr->sh_addr = 0; 3671 1.1 christos 3672 1.1 christos this_hdr->sh_offset = 0; 3673 1.6 christos this_hdr->sh_size = asect->size; 3674 1.6 christos this_hdr->sh_link = 0; 3675 1.6 christos /* PR 17512: file: 0eb809fe, 8b0535ee. */ 3676 1.9 christos if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) 3677 1.9 christos { 3678 1.11 christos _bfd_error_handler 3679 1.9 christos /* xgettext:c-format */ 3680 1.14 christos (_("%pB: error: alignment power %d of section `%pA' is too big"), 3681 1.6 christos abfd, asect->alignment_power, asect); 3682 1.6 christos arg->failed = true; 3683 1.12 christos return; 3684 1.12 christos } 3685 1.12 christos /* Set sh_addralign to the highest power of two given by alignment 3686 1.12 christos consistent with the section VMA. Linker scripts can force VMA. */ 3687 1.1 christos mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr; 3688 1.1 christos this_hdr->sh_addralign = mask & -mask; 3689 1.1 christos /* The sh_entsize and sh_info fields may have been set already by 3690 1.1 christos copy_private_section_data. */ 3691 1.1 christos 3692 1.1 christos this_hdr->bfd_section = asect; 3693 1.1 christos this_hdr->contents = NULL; 3694 1.1 christos 3695 1.14 christos /* If the section type is unspecified, we set it based on 3696 1.14 christos asect->flags. */ 3697 1.14 christos if (asect->type != 0) 3698 1.1 christos sh_type = asect->type; 3699 1.1 christos else if ((asect->flags & SEC_GROUP) != 0) 3700 1.1 christos sh_type = SHT_GROUP; 3701 1.1 christos else 3702 1.1 christos sh_type = bfd_elf_get_default_section_type (asect->flags); 3703 1.1 christos 3704 1.1 christos if (this_hdr->sh_type == SHT_NULL) 3705 1.1 christos this_hdr->sh_type = sh_type; 3706 1.1 christos else if (this_hdr->sh_type == SHT_NOBITS 3707 1.1 christos && sh_type == SHT_PROGBITS 3708 1.1 christos && (asect->flags & SEC_ALLOC) != 0) 3709 1.1 christos { 3710 1.1 christos /* Warn if we are changing a NOBITS section to PROGBITS, but 3711 1.1 christos allow the link to proceed. This can happen when users link 3712 1.9 christos non-bss input sections to bss output sections, or emit data 3713 1.11 christos to a bss output section via a linker script. */ 3714 1.1 christos _bfd_error_handler 3715 1.1 christos (_("warning: section `%pA' type changed to PROGBITS"), asect); 3716 1.1 christos this_hdr->sh_type = sh_type; 3717 1.1 christos } 3718 1.1 christos 3719 1.1 christos switch (this_hdr->sh_type) 3720 1.1 christos { 3721 1.1 christos default: 3722 1.1 christos break; 3723 1.8 christos 3724 1.8 christos case SHT_STRTAB: 3725 1.8 christos case SHT_NOTE: 3726 1.8 christos case SHT_NOBITS: 3727 1.8 christos case SHT_PROGBITS: 3728 1.1 christos break; 3729 1.1 christos 3730 1.1 christos case SHT_INIT_ARRAY: 3731 1.8 christos case SHT_FINI_ARRAY: 3732 1.1 christos case SHT_PREINIT_ARRAY: 3733 1.1 christos this_hdr->sh_entsize = bed->s->arch_size / 8; 3734 1.1 christos break; 3735 1.1 christos 3736 1.1 christos case SHT_HASH: 3737 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_hash_entry; 3738 1.1 christos break; 3739 1.1 christos 3740 1.1 christos case SHT_DYNSYM: 3741 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_sym; 3742 1.1 christos break; 3743 1.1 christos 3744 1.1 christos case SHT_DYNAMIC: 3745 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_dyn; 3746 1.1 christos break; 3747 1.1 christos 3748 1.1 christos case SHT_RELA: 3749 1.1 christos if (get_elf_backend_data (abfd)->may_use_rela_p) 3750 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_rela; 3751 1.1 christos break; 3752 1.1 christos 3753 1.1 christos case SHT_REL: 3754 1.1 christos if (get_elf_backend_data (abfd)->may_use_rel_p) 3755 1.1 christos this_hdr->sh_entsize = bed->s->sizeof_rel; 3756 1.1 christos break; 3757 1.1 christos 3758 1.1 christos case SHT_GNU_versym: 3759 1.1 christos this_hdr->sh_entsize = sizeof (Elf_External_Versym); 3760 1.1 christos break; 3761 1.1 christos 3762 1.1 christos case SHT_GNU_verdef: 3763 1.1 christos this_hdr->sh_entsize = 0; 3764 1.1 christos /* objcopy or strip will copy over sh_info, but may not set 3765 1.1 christos cverdefs. The linker will set cverdefs, but sh_info will be 3766 1.1 christos zero. */ 3767 1.1 christos if (this_hdr->sh_info == 0) 3768 1.1 christos this_hdr->sh_info = elf_tdata (abfd)->cverdefs; 3769 1.1 christos else 3770 1.1 christos BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 3771 1.1 christos || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); 3772 1.1 christos break; 3773 1.1 christos 3774 1.1 christos case SHT_GNU_verneed: 3775 1.1 christos this_hdr->sh_entsize = 0; 3776 1.1 christos /* objcopy or strip will copy over sh_info, but may not set 3777 1.1 christos cverrefs. The linker will set cverrefs, but sh_info will be 3778 1.1 christos zero. */ 3779 1.1 christos if (this_hdr->sh_info == 0) 3780 1.1 christos this_hdr->sh_info = elf_tdata (abfd)->cverrefs; 3781 1.1 christos else 3782 1.1 christos BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 3783 1.1 christos || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); 3784 1.1 christos break; 3785 1.1 christos 3786 1.1 christos case SHT_GROUP: 3787 1.1 christos this_hdr->sh_entsize = GRP_ENTRY_SIZE; 3788 1.1 christos break; 3789 1.1 christos 3790 1.1 christos case SHT_GNU_HASH: 3791 1.1 christos this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; 3792 1.1 christos break; 3793 1.1 christos } 3794 1.1 christos 3795 1.1 christos if ((asect->flags & SEC_ALLOC) != 0) 3796 1.1 christos this_hdr->sh_flags |= SHF_ALLOC; 3797 1.1 christos if ((asect->flags & SEC_READONLY) == 0) 3798 1.1 christos this_hdr->sh_flags |= SHF_WRITE; 3799 1.1 christos if ((asect->flags & SEC_CODE) != 0) 3800 1.1 christos this_hdr->sh_flags |= SHF_EXECINSTR; 3801 1.1 christos if ((asect->flags & SEC_MERGE) != 0) 3802 1.1 christos { 3803 1.1 christos this_hdr->sh_flags |= SHF_MERGE; 3804 1.8 christos this_hdr->sh_entsize = asect->entsize; 3805 1.18 christos } 3806 1.18 christos if ((asect->flags & SEC_STRINGS) != 0) 3807 1.18 christos { 3808 1.18 christos this_hdr->sh_flags |= SHF_STRINGS; 3809 1.1 christos this_hdr->sh_entsize = asect->entsize; 3810 1.1 christos } 3811 1.1 christos if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) 3812 1.1 christos this_hdr->sh_flags |= SHF_GROUP; 3813 1.1 christos if ((asect->flags & SEC_THREAD_LOCAL) != 0) 3814 1.1 christos { 3815 1.1 christos this_hdr->sh_flags |= SHF_TLS; 3816 1.1 christos if (asect->size == 0 3817 1.1 christos && (asect->flags & SEC_HAS_CONTENTS) == 0) 3818 1.1 christos { 3819 1.1 christos struct bfd_link_order *o = asect->map_tail.link_order; 3820 1.1 christos 3821 1.1 christos this_hdr->sh_size = 0; 3822 1.1 christos if (o != NULL) 3823 1.1 christos { 3824 1.1 christos this_hdr->sh_size = o->offset + o->size; 3825 1.1 christos if (this_hdr->sh_size != 0) 3826 1.1 christos this_hdr->sh_type = SHT_NOBITS; 3827 1.1 christos } 3828 1.1 christos } 3829 1.1 christos } 3830 1.1 christos if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) 3831 1.19 christos this_hdr->sh_flags |= SHF_EXCLUDE; 3832 1.19 christos 3833 1.19 christos if (this_hdr->sh_entsize == 0) 3834 1.1 christos this_hdr->sh_entsize = asect->entsize; 3835 1.1 christos 3836 1.1 christos /* If the section has relocs, set up a section header for the 3837 1.1 christos SHT_REL[A] section. If two relocation sections are required for 3838 1.1 christos this section, it is up to the processor-specific back-end to 3839 1.1 christos create the other. */ 3840 1.1 christos if ((asect->flags & SEC_RELOC) != 0) 3841 1.1 christos { 3842 1.1 christos /* When doing a relocatable link, create both REL and RELA sections if 3843 1.1 christos needed. */ 3844 1.1 christos if (arg->link_info 3845 1.8 christos /* Do the normal setup if we wouldn't create any sections here. */ 3846 1.8 christos && esd->rel.count + esd->rela.count > 0 3847 1.1 christos && (bfd_link_relocatable (arg->link_info) 3848 1.1 christos || arg->link_info->emitrelocations)) 3849 1.11 christos { 3850 1.19 christos if (esd->rel.count && esd->rel.hdr == NULL 3851 1.1 christos && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, 3852 1.14 christos false, delay_sh_name_p)) 3853 1.1 christos { 3854 1.1 christos arg->failed = true; 3855 1.1 christos return; 3856 1.11 christos } 3857 1.19 christos if (esd->rela.count && esd->rela.hdr == NULL 3858 1.1 christos && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, 3859 1.14 christos true, delay_sh_name_p)) 3860 1.1 christos { 3861 1.1 christos arg->failed = true; 3862 1.1 christos return; 3863 1.1 christos } 3864 1.1 christos } 3865 1.1 christos else if (!_bfd_elf_init_reloc_shdr (abfd, 3866 1.6 christos (asect->use_rela_p 3867 1.6 christos ? &esd->rela : &esd->rel), 3868 1.19 christos name, 3869 1.11 christos asect->use_rela_p, 3870 1.14 christos delay_sh_name_p)) 3871 1.11 christos { 3872 1.11 christos arg->failed = true; 3873 1.1 christos return; 3874 1.1 christos } 3875 1.1 christos } 3876 1.1 christos 3877 1.1 christos /* Check for processor-specific section types. */ 3878 1.1 christos sh_type = this_hdr->sh_type; 3879 1.11 christos if (bed->elf_backend_fake_sections 3880 1.14 christos && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) 3881 1.11 christos { 3882 1.11 christos arg->failed = true; 3883 1.1 christos return; 3884 1.1 christos } 3885 1.1 christos 3886 1.1 christos if (sh_type == SHT_NOBITS && asect->size != 0) 3887 1.1 christos { 3888 1.1 christos /* Don't change the header type from NOBITS if we are being 3889 1.1 christos called for objcopy --only-keep-debug. */ 3890 1.1 christos this_hdr->sh_type = sh_type; 3891 1.1 christos } 3892 1.1 christos } 3893 1.1 christos 3894 1.1 christos /* Fill in the contents of a SHT_GROUP section. Called from 3895 1.1 christos _bfd_elf_compute_section_file_positions for gas, objcopy, and 3896 1.1 christos when ELF targets use the generic linker, ld. Called for ld -r 3897 1.1 christos from bfd_elf_final_link. */ 3898 1.1 christos 3899 1.1 christos void 3900 1.14 christos bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) 3901 1.1 christos { 3902 1.1 christos bool *failedptr = (bool *) failedptrarg; 3903 1.14 christos asection *elt, *first; 3904 1.1 christos unsigned char *loc; 3905 1.1 christos bool gas; 3906 1.1 christos 3907 1.12 christos /* Ignore linker created group section. See elfNN_ia64_object_p in 3908 1.12 christos elfxx-ia64.c. */ 3909 1.1 christos if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP 3910 1.1 christos || sec->size == 0 3911 1.1 christos || *failedptr) 3912 1.1 christos return; 3913 1.1 christos 3914 1.1 christos if (elf_section_data (sec)->this_hdr.sh_info == 0) 3915 1.1 christos { 3916 1.1 christos unsigned long symindx = 0; 3917 1.1 christos 3918 1.1 christos /* elf_group_id will have been set up by objcopy and the 3919 1.1 christos generic linker. */ 3920 1.1 christos if (elf_group_id (sec) != NULL) 3921 1.1 christos symindx = elf_group_id (sec)->udata.i; 3922 1.1 christos 3923 1.1 christos if (symindx == 0) 3924 1.12 christos { 3925 1.12 christos /* If called from the assembler, swap_out_syms will have set up 3926 1.14 christos elf_section_syms. 3927 1.14 christos PR 25699: A corrupt input file could contain bogus group info. */ 3928 1.12 christos if (sec->index >= elf_num_section_syms (abfd) 3929 1.14 christos || elf_section_syms (abfd)[sec->index] == NULL) 3930 1.12 christos { 3931 1.12 christos *failedptr = true; 3932 1.1 christos return; 3933 1.1 christos } 3934 1.1 christos symindx = elf_section_syms (abfd)[sec->index]->udata.i; 3935 1.1 christos } 3936 1.1 christos elf_section_data (sec)->this_hdr.sh_info = symindx; 3937 1.1 christos } 3938 1.1 christos else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) 3939 1.1 christos { 3940 1.1 christos /* The ELF backend linker sets sh_info to -2 when the group 3941 1.9 christos signature symbol is global, and thus the index can't be 3942 1.9 christos set until all local symbols are output. */ 3943 1.9 christos asection *igroup; 3944 1.9 christos struct bfd_elf_section_data *sec_data; 3945 1.1 christos unsigned long symndx; 3946 1.1 christos unsigned long extsymoff; 3947 1.9 christos struct elf_link_hash_entry *h; 3948 1.9 christos 3949 1.9 christos /* The point of this little dance to the first SHF_GROUP section 3950 1.9 christos then back to the SHT_GROUP section is that this gets us to 3951 1.9 christos the SHT_GROUP in the input object. */ 3952 1.9 christos igroup = elf_sec_group (elf_next_in_group (sec)); 3953 1.9 christos sec_data = elf_section_data (igroup); 3954 1.1 christos symndx = sec_data->this_hdr.sh_info; 3955 1.1 christos extsymoff = 0; 3956 1.1 christos if (!elf_bad_symtab (igroup->owner)) 3957 1.1 christos { 3958 1.1 christos Elf_Internal_Shdr *symtab_hdr; 3959 1.1 christos 3960 1.1 christos symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; 3961 1.1 christos extsymoff = symtab_hdr->sh_info; 3962 1.1 christos } 3963 1.1 christos h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; 3964 1.1 christos while (h->root.type == bfd_link_hash_indirect 3965 1.1 christos || h->root.type == bfd_link_hash_warning) 3966 1.1 christos h = (struct elf_link_hash_entry *) h->root.u.i.link; 3967 1.1 christos 3968 1.1 christos elf_section_data (sec)->this_hdr.sh_info = h->indx; 3969 1.1 christos } 3970 1.14 christos 3971 1.1 christos /* The contents won't be allocated for "ld -r" or objcopy. */ 3972 1.1 christos gas = true; 3973 1.14 christos if (sec->contents == NULL) 3974 1.1 christos { 3975 1.1 christos gas = false; 3976 1.1 christos sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); 3977 1.1 christos 3978 1.1 christos /* Arrange for the section to be written out. */ 3979 1.1 christos elf_section_data (sec)->this_hdr.contents = sec->contents; 3980 1.14 christos if (sec->contents == NULL) 3981 1.1 christos { 3982 1.1 christos *failedptr = true; 3983 1.19 christos return; 3984 1.1 christos } 3985 1.1 christos sec->alloced = 1; 3986 1.1 christos } 3987 1.1 christos 3988 1.1 christos loc = sec->contents + sec->size; 3989 1.1 christos 3990 1.1 christos /* Get the pointer to the first section in the group that gas 3991 1.1 christos squirreled away here. objcopy arranges for this to be set to the 3992 1.1 christos start of the input section group. */ 3993 1.1 christos first = elt = elf_next_in_group (sec); 3994 1.1 christos 3995 1.1 christos /* First element is a flag word. Rest of section is elf section 3996 1.1 christos indices for all the sections of the group. Write them backwards 3997 1.1 christos just to keep the group in the same order as given in .section 3998 1.1 christos directives, not that it matters. */ 3999 1.1 christos while (elt != NULL) 4000 1.1 christos { 4001 1.1 christos asection *s; 4002 1.1 christos 4003 1.1 christos s = elt; 4004 1.1 christos if (!gas) 4005 1.1 christos s = s->output_section; 4006 1.1 christos if (s != NULL 4007 1.11 christos && !bfd_is_abs_section (s)) 4008 1.11 christos { 4009 1.1 christos struct bfd_elf_section_data *elf_sec = elf_section_data (s); 4010 1.11 christos struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt); 4011 1.11 christos 4012 1.11 christos if (elf_sec->rel.hdr != NULL 4013 1.11 christos && (gas 4014 1.11 christos || (input_elf_sec->rel.hdr != NULL 4015 1.11 christos && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)) 4016 1.11 christos { 4017 1.14 christos elf_sec->rel.hdr->sh_flags |= SHF_GROUP; 4018 1.14 christos loc -= 4; 4019 1.11 christos if (loc == sec->contents) 4020 1.11 christos break; 4021 1.11 christos H_PUT_32 (abfd, elf_sec->rel.idx, loc); 4022 1.11 christos } 4023 1.11 christos if (elf_sec->rela.hdr != NULL 4024 1.11 christos && (gas 4025 1.11 christos || (input_elf_sec->rela.hdr != NULL 4026 1.11 christos && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)) 4027 1.11 christos { 4028 1.14 christos elf_sec->rela.hdr->sh_flags |= SHF_GROUP; 4029 1.14 christos loc -= 4; 4030 1.11 christos if (loc == sec->contents) 4031 1.11 christos break; 4032 1.1 christos H_PUT_32 (abfd, elf_sec->rela.idx, loc); 4033 1.14 christos } 4034 1.14 christos loc -= 4; 4035 1.11 christos if (loc == sec->contents) 4036 1.1 christos break; 4037 1.1 christos H_PUT_32 (abfd, elf_sec->this_idx, loc); 4038 1.1 christos } 4039 1.1 christos elt = elf_next_in_group (elt); 4040 1.1 christos if (elt == first) 4041 1.1 christos break; 4042 1.19 christos } 4043 1.19 christos 4044 1.19 christos /* We should always get here with loc == sec->contents + 4. Return 4045 1.19 christos an error for bogus SHT_GROUP sections. */ 4046 1.14 christos loc -= 4; 4047 1.19 christos if (loc != sec->contents) 4048 1.19 christos { 4049 1.19 christos /* xgettext:c-format */ 4050 1.19 christos _bfd_error_handler (_("%pB: corrupted group section: `%pA'"), 4051 1.19 christos abfd, sec); 4052 1.19 christos bfd_set_error (bfd_error_bad_value); 4053 1.14 christos *failedptr = true; 4054 1.1 christos return; 4055 1.1 christos } 4056 1.1 christos 4057 1.1 christos H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); 4058 1.11 christos } 4059 1.11 christos 4060 1.11 christos /* Given NAME, the name of a relocation section stripped of its 4061 1.6 christos .rel/.rela prefix, return the section in ABFD to which the 4062 1.6 christos relocations apply. */ 4063 1.11 christos 4064 1.11 christos asection * 4065 1.11 christos _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name) 4066 1.11 christos { 4067 1.11 christos /* If a target needs .got.plt section, relocations in rela.plt/rel.plt 4068 1.19 christos section likely apply to .got.plt or .got section. */ 4069 1.11 christos if (get_elf_backend_data (abfd)->want_got_plt 4070 1.11 christos && streq (name, ".plt")) 4071 1.11 christos { 4072 1.11 christos asection *sec; 4073 1.11 christos 4074 1.11 christos name = ".got.plt"; 4075 1.11 christos sec = bfd_get_section_by_name (abfd, name); 4076 1.11 christos if (sec != NULL) 4077 1.11 christos return sec; 4078 1.11 christos name = ".got"; 4079 1.11 christos } 4080 1.11 christos 4081 1.11 christos return bfd_get_section_by_name (abfd, name); 4082 1.11 christos } 4083 1.11 christos 4084 1.11 christos /* Return the section to which RELOC_SEC applies. */ 4085 1.11 christos 4086 1.6 christos static asection * 4087 1.6 christos elf_get_reloc_section (asection *reloc_sec) 4088 1.6 christos { 4089 1.6 christos const char *name; 4090 1.11 christos unsigned int type; 4091 1.6 christos bfd *abfd; 4092 1.6 christos const struct elf_backend_data *bed; 4093 1.6 christos 4094 1.6 christos type = elf_section_data (reloc_sec)->this_hdr.sh_type; 4095 1.6 christos if (type != SHT_REL && type != SHT_RELA) 4096 1.6 christos return NULL; 4097 1.6 christos 4098 1.14 christos /* We look up the section the relocs apply to by name. */ 4099 1.11 christos name = reloc_sec->name; 4100 1.11 christos if (!startswith (name, ".rel")) 4101 1.11 christos return NULL; 4102 1.11 christos name += 4; 4103 1.6 christos if (type == SHT_RELA && *name++ != 'a') 4104 1.6 christos return NULL; 4105 1.11 christos 4106 1.11 christos abfd = reloc_sec->owner; 4107 1.6 christos bed = get_elf_backend_data (abfd); 4108 1.6 christos return bed->get_reloc_section (abfd, name); 4109 1.1 christos } 4110 1.1 christos 4111 1.12 christos /* Assign all ELF section numbers. The dummy first section is handled here 4112 1.14 christos too. The link/info pointers for the standard section types are filled 4113 1.1 christos in here too, while we're at it. LINK_INFO will be 0 when arriving 4114 1.14 christos here for gas, objcopy, and when using the generic ELF linker. */ 4115 1.1 christos 4116 1.1 christos static bool 4117 1.1 christos assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) 4118 1.1 christos { 4119 1.6 christos struct elf_obj_tdata *t = elf_tdata (abfd); 4120 1.1 christos asection *sec; 4121 1.1 christos unsigned int section_number; 4122 1.14 christos Elf_Internal_Shdr **i_shdrp; 4123 1.12 christos struct bfd_elf_section_data *d; 4124 1.1 christos bool need_symtab; 4125 1.1 christos size_t amt; 4126 1.1 christos 4127 1.1 christos section_number = 1; 4128 1.1 christos 4129 1.1 christos _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); 4130 1.11 christos 4131 1.1 christos /* SHT_GROUP sections are in relocatable files only. */ 4132 1.8 christos if (link_info == NULL || !link_info->resolve_section_groups) 4133 1.8 christos { 4134 1.1 christos size_t reloc_count = 0; 4135 1.1 christos 4136 1.1 christos /* Put SHT_GROUP sections first. */ 4137 1.1 christos for (sec = abfd->sections; sec != NULL; sec = sec->next) 4138 1.1 christos { 4139 1.1 christos d = elf_section_data (sec); 4140 1.1 christos 4141 1.1 christos if (d->this_hdr.sh_type == SHT_GROUP) 4142 1.1 christos { 4143 1.1 christos if (sec->flags & SEC_LINKER_CREATED) 4144 1.1 christos { 4145 1.1 christos /* Remove the linker created SHT_GROUP sections. */ 4146 1.1 christos bfd_section_list_remove (abfd, sec); 4147 1.1 christos abfd->section_count--; 4148 1.1 christos } 4149 1.1 christos else 4150 1.8 christos d->this_idx = section_number++; 4151 1.8 christos } 4152 1.8 christos 4153 1.1 christos /* Count relocations. */ 4154 1.8 christos reloc_count += sec->reloc_count; 4155 1.14 christos } 4156 1.8 christos 4157 1.8 christos /* Set/clear HAS_RELOC depending on whether there are relocations. */ 4158 1.14 christos if (reloc_count == 0) 4159 1.14 christos abfd->flags &= ~HAS_RELOC; 4160 1.1 christos else 4161 1.1 christos abfd->flags |= HAS_RELOC; 4162 1.1 christos } 4163 1.1 christos 4164 1.1 christos for (sec = abfd->sections; sec; sec = sec->next) 4165 1.1 christos { 4166 1.1 christos d = elf_section_data (sec); 4167 1.1 christos 4168 1.6 christos if (d->this_hdr.sh_type != SHT_GROUP) 4169 1.6 christos d->this_idx = section_number++; 4170 1.1 christos if (d->this_hdr.sh_name != (unsigned int) -1) 4171 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); 4172 1.1 christos if (d->rel.hdr) 4173 1.6 christos { 4174 1.6 christos d->rel.idx = section_number++; 4175 1.1 christos if (d->rel.hdr->sh_name != (unsigned int) -1) 4176 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); 4177 1.1 christos } 4178 1.1 christos else 4179 1.1 christos d->rel.idx = 0; 4180 1.1 christos 4181 1.1 christos if (d->rela.hdr) 4182 1.6 christos { 4183 1.6 christos d->rela.idx = section_number++; 4184 1.1 christos if (d->rela.hdr->sh_name != (unsigned int) -1) 4185 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); 4186 1.1 christos } 4187 1.1 christos else 4188 1.1 christos d->rela.idx = 0; 4189 1.1 christos } 4190 1.14 christos 4191 1.14 christos need_symtab = (bfd_get_symcount (abfd) > 0 4192 1.14 christos || (link_info == NULL 4193 1.1 christos && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) 4194 1.1 christos == HAS_RELOC))); 4195 1.3 christos if (need_symtab) 4196 1.1 christos { 4197 1.1 christos elf_onesymtab (abfd) = section_number++; 4198 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); 4199 1.12 christos if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) 4200 1.8 christos { 4201 1.8 christos elf_section_list *entry; 4202 1.8 christos 4203 1.12 christos BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); 4204 1.8 christos 4205 1.8 christos entry = bfd_zalloc (abfd, sizeof (*entry)); 4206 1.8 christos entry->ndx = section_number++; 4207 1.1 christos elf_symtab_shndx_list (abfd) = entry; 4208 1.14 christos entry->hdr.sh_name 4209 1.8 christos = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 4210 1.14 christos ".symtab_shndx", false); 4211 1.1 christos if (entry->hdr.sh_name == (unsigned int) -1) 4212 1.3 christos return false; 4213 1.1 christos } 4214 1.1 christos elf_strtab_sec (abfd) = section_number++; 4215 1.1 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); 4216 1.9 christos } 4217 1.9 christos 4218 1.9 christos elf_shstrtab_sec (abfd) = section_number++; 4219 1.9 christos _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); 4220 1.3 christos elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); 4221 1.3 christos 4222 1.9 christos if (section_number >= SHN_LORESERVE) 4223 1.11 christos { 4224 1.3 christos /* xgettext:c-format */ 4225 1.14 christos _bfd_error_handler (_("%pB: too many sections: %u"), 4226 1.3 christos abfd, section_number); 4227 1.3 christos return false; 4228 1.1 christos } 4229 1.1 christos 4230 1.1 christos elf_numsections (abfd) = section_number; 4231 1.1 christos elf_elfheader (abfd)->e_shnum = section_number; 4232 1.1 christos 4233 1.12 christos /* Set up the list of section header pointers, in agreement with the 4234 1.12 christos indices. */ 4235 1.1 christos amt = section_number * sizeof (Elf_Internal_Shdr *); 4236 1.14 christos i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt); 4237 1.1 christos if (i_shdrp == NULL) 4238 1.1 christos return false; 4239 1.11 christos 4240 1.1 christos i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, 4241 1.1 christos sizeof (Elf_Internal_Shdr)); 4242 1.1 christos if (i_shdrp[0] == NULL) 4243 1.14 christos { 4244 1.1 christos bfd_release (abfd, i_shdrp); 4245 1.1 christos return false; 4246 1.1 christos } 4247 1.1 christos 4248 1.3 christos elf_elfsections (abfd) = i_shdrp; 4249 1.1 christos 4250 1.1 christos i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; 4251 1.3 christos if (need_symtab) 4252 1.1 christos { 4253 1.1 christos i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; 4254 1.8 christos if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) 4255 1.8 christos { 4256 1.8 christos elf_section_list * entry = elf_symtab_shndx_list (abfd); 4257 1.8 christos BFD_ASSERT (entry != NULL); 4258 1.1 christos i_shdrp[entry->ndx] = & entry->hdr; 4259 1.3 christos entry->hdr.sh_link = elf_onesymtab (abfd); 4260 1.3 christos } 4261 1.1 christos i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; 4262 1.1 christos t->symtab_hdr.sh_link = elf_strtab_sec (abfd); 4263 1.1 christos } 4264 1.1 christos 4265 1.1 christos for (sec = abfd->sections; sec; sec = sec->next) 4266 1.1 christos { 4267 1.1 christos asection *s; 4268 1.1 christos 4269 1.1 christos d = elf_section_data (sec); 4270 1.1 christos 4271 1.1 christos i_shdrp[d->this_idx] = &d->this_hdr; 4272 1.1 christos if (d->rel.idx != 0) 4273 1.1 christos i_shdrp[d->rel.idx] = d->rel.hdr; 4274 1.1 christos if (d->rela.idx != 0) 4275 1.1 christos i_shdrp[d->rela.idx] = d->rela.hdr; 4276 1.1 christos 4277 1.1 christos /* Fill in the sh_link and sh_info fields while we're at it. */ 4278 1.1 christos 4279 1.1 christos /* sh_link of a reloc section is the section index of the symbol 4280 1.1 christos table. sh_info is the section index of the section to which 4281 1.1 christos the relocation entries apply. */ 4282 1.3 christos if (d->rel.idx != 0) 4283 1.1 christos { 4284 1.4 christos d->rel.hdr->sh_link = elf_onesymtab (abfd); 4285 1.1 christos d->rel.hdr->sh_info = d->this_idx; 4286 1.1 christos d->rel.hdr->sh_flags |= SHF_INFO_LINK; 4287 1.1 christos } 4288 1.3 christos if (d->rela.idx != 0) 4289 1.1 christos { 4290 1.4 christos d->rela.hdr->sh_link = elf_onesymtab (abfd); 4291 1.1 christos d->rela.hdr->sh_info = d->this_idx; 4292 1.1 christos d->rela.hdr->sh_flags |= SHF_INFO_LINK; 4293 1.1 christos } 4294 1.1 christos 4295 1.1 christos /* We need to set up sh_link for SHF_LINK_ORDER. */ 4296 1.1 christos if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) 4297 1.14 christos { 4298 1.14 christos s = elf_linked_to_section (sec); 4299 1.14 christos /* We can now have a NULL linked section pointer. 4300 1.14 christos This happens when the sh_link field is 0, which is done 4301 1.1 christos when a linked to section is discarded but the linking 4302 1.1 christos section has been retained for some reason. */ 4303 1.12 christos if (s) 4304 1.12 christos { 4305 1.1 christos /* Check discarded linkonce section. */ 4306 1.12 christos if (discarded_section (s)) 4307 1.12 christos { 4308 1.12 christos asection *kept; 4309 1.12 christos _bfd_error_handler 4310 1.12 christos /* xgettext:c-format */ 4311 1.12 christos (_("%pB: sh_link of section `%pA' points to" 4312 1.12 christos " discarded section `%pA' of `%pB'"), 4313 1.12 christos abfd, d->this_hdr.bfd_section, s, s->owner); 4314 1.12 christos /* Point to the kept section if it has the same 4315 1.12 christos size as the discarded one. */ 4316 1.1 christos kept = _bfd_elf_check_kept_section (s, link_info); 4317 1.12 christos if (kept == NULL) 4318 1.14 christos { 4319 1.1 christos bfd_set_error (bfd_error_bad_value); 4320 1.12 christos return false; 4321 1.1 christos } 4322 1.12 christos s = kept; 4323 1.12 christos } 4324 1.1 christos /* Handle objcopy. */ 4325 1.12 christos else if (s->output_section == NULL) 4326 1.12 christos { 4327 1.12 christos _bfd_error_handler 4328 1.12 christos /* xgettext:c-format */ 4329 1.12 christos (_("%pB: sh_link of section `%pA' points to" 4330 1.12 christos " removed section `%pA' of `%pB'"), 4331 1.14 christos abfd, d->this_hdr.bfd_section, s, s->owner); 4332 1.1 christos bfd_set_error (bfd_error_bad_value); 4333 1.12 christos return false; 4334 1.1 christos } 4335 1.1 christos s = s->output_section; 4336 1.1 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4337 1.1 christos } 4338 1.1 christos } 4339 1.1 christos 4340 1.1 christos switch (d->this_hdr.sh_type) 4341 1.1 christos { 4342 1.17 christos case SHT_REL: 4343 1.17 christos case SHT_RELA: 4344 1.17 christos /* sh_link is the section index of the symbol table. 4345 1.17 christos sh_info is the section index of the section to which the 4346 1.14 christos relocation entries apply. */ 4347 1.17 christos if (d->this_hdr.sh_link == 0) 4348 1.17 christos { 4349 1.17 christos /* FIXME maybe: If this is a reloc section which we are 4350 1.17 christos treating as a normal section then we likely should 4351 1.17 christos not be assuming its sh_link is .dynsym or .symtab. */ 4352 1.17 christos if ((sec->flags & SEC_ALLOC) != 0) 4353 1.17 christos { 4354 1.17 christos s = bfd_get_section_by_name (abfd, ".dynsym"); 4355 1.17 christos if (s != NULL) 4356 1.17 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4357 1.17 christos } 4358 1.14 christos else 4359 1.1 christos d->this_hdr.sh_link = elf_onesymtab (abfd); 4360 1.11 christos } 4361 1.1 christos 4362 1.4 christos s = elf_get_reloc_section (sec); 4363 1.4 christos if (s != NULL) 4364 1.4 christos { 4365 1.4 christos d->this_hdr.sh_info = elf_section_data (s)->this_idx; 4366 1.1 christos d->this_hdr.sh_flags |= SHF_INFO_LINK; 4367 1.1 christos } 4368 1.1 christos break; 4369 1.1 christos 4370 1.1 christos case SHT_STRTAB: 4371 1.1 christos /* We assume that a section named .stab*str is a stabs 4372 1.1 christos string section. We look for a section with the same name 4373 1.14 christos but without the trailing ``str'', and set its sh_link 4374 1.19 christos field to point to this section. */ 4375 1.1 christos if (startswith (sec->name, ".stab") 4376 1.1 christos && streq (sec->name + strlen (sec->name) - 3, "str")) 4377 1.1 christos { 4378 1.1 christos size_t len; 4379 1.1 christos char *alc; 4380 1.1 christos 4381 1.1 christos len = strlen (sec->name); 4382 1.14 christos alc = (char *) bfd_malloc (len - 2); 4383 1.1 christos if (alc == NULL) 4384 1.1 christos return false; 4385 1.1 christos memcpy (alc, sec->name, len - 3); 4386 1.1 christos alc[len - 3] = '\0'; 4387 1.1 christos s = bfd_get_section_by_name (abfd, alc); 4388 1.1 christos free (alc); 4389 1.1 christos if (s != NULL) 4390 1.1 christos { 4391 1.1 christos elf_section_data (s)->this_hdr.sh_link = d->this_idx; 4392 1.12 christos 4393 1.1 christos /* This is a .stab section. */ 4394 1.1 christos elf_section_data (s)->this_hdr.sh_entsize = 12; 4395 1.1 christos } 4396 1.1 christos } 4397 1.1 christos break; 4398 1.1 christos 4399 1.1 christos case SHT_DYNAMIC: 4400 1.1 christos case SHT_DYNSYM: 4401 1.1 christos case SHT_GNU_verneed: 4402 1.1 christos case SHT_GNU_verdef: 4403 1.1 christos /* sh_link is the section header index of the string table 4404 1.1 christos used for the dynamic entries, or the symbol table, or the 4405 1.1 christos version strings. */ 4406 1.1 christos s = bfd_get_section_by_name (abfd, ".dynstr"); 4407 1.1 christos if (s != NULL) 4408 1.1 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4409 1.1 christos break; 4410 1.1 christos 4411 1.1 christos case SHT_GNU_LIBLIST: 4412 1.1 christos /* sh_link is the section header index of the prelink library 4413 1.14 christos list used for the dynamic entries, or the symbol table, or 4414 1.14 christos the version strings. */ 4415 1.1 christos s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC) 4416 1.1 christos ? ".dynstr" : ".gnu.libstr")); 4417 1.1 christos if (s != NULL) 4418 1.1 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4419 1.1 christos break; 4420 1.1 christos 4421 1.1 christos case SHT_HASH: 4422 1.1 christos case SHT_GNU_HASH: 4423 1.1 christos case SHT_GNU_versym: 4424 1.1 christos /* sh_link is the section header index of the symbol table 4425 1.1 christos this hash table or version table is for. */ 4426 1.1 christos s = bfd_get_section_by_name (abfd, ".dynsym"); 4427 1.1 christos if (s != NULL) 4428 1.1 christos d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4429 1.1 christos break; 4430 1.3 christos 4431 1.1 christos case SHT_GROUP: 4432 1.1 christos d->this_hdr.sh_link = elf_onesymtab (abfd); 4433 1.1 christos } 4434 1.6 christos } 4435 1.6 christos 4436 1.6 christos /* Delay setting sh_name to _bfd_elf_write_object_contents so that 4437 1.6 christos _bfd_elf_assign_file_positions_for_non_load can convert DWARF 4438 1.14 christos debug section name from .debug_* to .zdebug_* if needed. */ 4439 1.1 christos 4440 1.1 christos return true; 4441 1.14 christos } 4442 1.1 christos 4443 1.1 christos static bool 4444 1.1 christos sym_is_global (bfd *abfd, asymbol *sym) 4445 1.1 christos { 4446 1.1 christos /* If the backend has a special mapping, use it. */ 4447 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4448 1.1 christos if (bed->elf_backend_sym_is_global) 4449 1.1 christos return (*bed->elf_backend_sym_is_global) (abfd, sym); 4450 1.12 christos 4451 1.12 christos return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 4452 1.1 christos || bfd_is_und_section (bfd_asymbol_section (sym)) 4453 1.1 christos || bfd_is_com_section (bfd_asymbol_section (sym))); 4454 1.8 christos } 4455 1.8 christos 4456 1.8 christos /* Filter global symbols of ABFD to include in the import library. All 4457 1.8 christos SYMCOUNT symbols of ABFD can be examined from their pointers in 4458 1.8 christos SYMS. Pointers of symbols to keep should be stored contiguously at 4459 1.8 christos the beginning of that array. 4460 1.8 christos 4461 1.8 christos Returns the number of symbols to keep. */ 4462 1.8 christos 4463 1.8 christos unsigned int 4464 1.8 christos _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, 4465 1.8 christos asymbol **syms, long symcount) 4466 1.8 christos { 4467 1.8 christos long src_count, dst_count = 0; 4468 1.8 christos 4469 1.8 christos for (src_count = 0; src_count < symcount; src_count++) 4470 1.8 christos { 4471 1.8 christos asymbol *sym = syms[src_count]; 4472 1.8 christos char *name = (char *) bfd_asymbol_name (sym); 4473 1.8 christos struct bfd_link_hash_entry *h; 4474 1.8 christos 4475 1.8 christos if (!sym_is_global (abfd, sym)) 4476 1.14 christos continue; 4477 1.8 christos 4478 1.8 christos h = bfd_link_hash_lookup (info->hash, name, false, false, false); 4479 1.8 christos if (h == NULL) 4480 1.8 christos continue; 4481 1.8 christos if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak) 4482 1.8 christos continue; 4483 1.8 christos if (h->linker_def || h->ldscript_def) 4484 1.8 christos continue; 4485 1.8 christos 4486 1.8 christos syms[dst_count++] = sym; 4487 1.8 christos } 4488 1.8 christos 4489 1.8 christos syms[dst_count] = NULL; 4490 1.8 christos 4491 1.8 christos return dst_count; 4492 1.17 christos } 4493 1.17 christos 4494 1.1 christos /* Don't output symbols for sections that are not going to be output, 4495 1.14 christos that are duplicates or there is no BFD section. */ 4496 1.17 christos 4497 1.1 christos static bool 4498 1.11 christos ignore_sym (asymbol *sym) 4499 1.14 christos { 4500 1.11 christos if (sym == NULL) 4501 1.17 christos return false; 4502 1.14 christos 4503 1.3 christos if (sym->section == NULL) 4504 1.17 christos return true; 4505 1.17 christos 4506 1.17 christos if ((sym->flags & BSF_SECTION_SYM) != 0) 4507 1.17 christos { 4508 1.17 christos if ((sym->flags & BSF_SECTION_SYM_USED) == 0) 4509 1.17 christos return true; 4510 1.17 christos /* With ld -r on generic elf targets it is possible to have 4511 1.17 christos multiple section symbols in the output for a given section. 4512 1.17 christos We'd like to get rid of all but the first one. This drops 4513 1.17 christos them if the first input section is non-zero size, but fails 4514 1.17 christos to do so if the first input section is zero sized. */ 4515 1.17 christos if (sym->section->output_offset != 0) 4516 1.11 christos return true; 4517 1.17 christos } 4518 1.1 christos 4519 1.1 christos return discarded_section (sym->section); 4520 1.3 christos } 4521 1.3 christos 4522 1.3 christos /* Map symbol from it's internal number to the external number, moving 4523 1.14 christos all local symbols to be at the head of the list. */ 4524 1.3 christos 4525 1.1 christos static bool 4526 1.1 christos elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) 4527 1.1 christos { 4528 1.1 christos unsigned int symcount = bfd_get_symcount (abfd); 4529 1.1 christos asymbol **syms = bfd_get_outsymbols (abfd); 4530 1.1 christos asymbol **sect_syms; 4531 1.8 christos unsigned int num_locals = 0; 4532 1.1 christos unsigned int num_globals = 0; 4533 1.1 christos unsigned int max_index = 0; 4534 1.1 christos unsigned int idx; 4535 1.12 christos asection *asect; 4536 1.1 christos asymbol **new_syms; 4537 1.1 christos size_t amt; 4538 1.1 christos 4539 1.1 christos #ifdef DEBUG 4540 1.1 christos fprintf (stderr, "elf_map_symbols\n"); 4541 1.1 christos fflush (stderr); 4542 1.1 christos #endif 4543 1.1 christos 4544 1.1 christos for (asect = abfd->sections; asect; asect = asect->next) 4545 1.1 christos { 4546 1.1 christos if (max_index < asect->index) 4547 1.1 christos max_index = asect->index; 4548 1.1 christos } 4549 1.12 christos 4550 1.12 christos max_index++; 4551 1.1 christos amt = max_index * sizeof (asymbol *); 4552 1.14 christos sect_syms = (asymbol **) bfd_zalloc (abfd, amt); 4553 1.1 christos if (sect_syms == NULL) 4554 1.1 christos return false; 4555 1.1 christos elf_section_syms (abfd) = sect_syms; 4556 1.1 christos elf_num_section_syms (abfd) = max_index; 4557 1.1 christos 4558 1.1 christos /* Init sect_syms entries for any section symbols we have already 4559 1.1 christos decided to output. */ 4560 1.1 christos for (idx = 0; idx < symcount; idx++) 4561 1.1 christos { 4562 1.1 christos asymbol *sym = syms[idx]; 4563 1.1 christos 4564 1.17 christos if ((sym->flags & BSF_SECTION_SYM) != 0 4565 1.3 christos && sym->value == 0 4566 1.1 christos && !ignore_sym (sym) 4567 1.1 christos && !bfd_is_abs_section (sym->section)) 4568 1.1 christos { 4569 1.1 christos asection *sec = sym->section; 4570 1.19 christos 4571 1.19 christos if (sec->owner != abfd) 4572 1.19 christos { 4573 1.19 christos sec = sec->output_section; 4574 1.19 christos if (sec == NULL) 4575 1.1 christos return false; 4576 1.1 christos } 4577 1.1 christos 4578 1.1 christos sect_syms[sec->index] = syms[idx]; 4579 1.1 christos } 4580 1.1 christos } 4581 1.1 christos 4582 1.1 christos /* Classify all of the symbols. */ 4583 1.17 christos for (idx = 0; idx < symcount; idx++) 4584 1.17 christos { 4585 1.3 christos if (ignore_sym (syms[idx])) 4586 1.3 christos continue; 4587 1.17 christos if (sym_is_global (abfd, syms[idx])) 4588 1.1 christos num_globals++; 4589 1.1 christos else 4590 1.1 christos num_locals++; 4591 1.1 christos } 4592 1.1 christos 4593 1.1 christos /* We will be adding a section symbol for each normal BFD section. Most 4594 1.1 christos sections will already have a section symbol in outsymbols, but 4595 1.1 christos eg. SHT_GROUP sections will not, and we need the section symbol mapped 4596 1.1 christos at least in that case. */ 4597 1.14 christos for (asect = abfd->sections; asect; asect = asect->next) 4598 1.14 christos { 4599 1.17 christos asymbol *sym = asect->symbol; 4600 1.14 christos /* Don't include ignored section symbols. */ 4601 1.1 christos if (!ignore_sym (sym) 4602 1.17 christos && sect_syms[asect->index] == NULL) 4603 1.17 christos { 4604 1.17 christos if (sym_is_global (abfd, asect->symbol)) 4605 1.1 christos num_globals++; 4606 1.1 christos else 4607 1.1 christos num_locals++; 4608 1.1 christos } 4609 1.1 christos } 4610 1.12 christos 4611 1.12 christos /* Now sort the symbols so the local symbols are first. */ 4612 1.1 christos amt = (num_locals + num_globals) * sizeof (asymbol *); 4613 1.14 christos new_syms = (asymbol **) bfd_alloc (abfd, amt); 4614 1.1 christos if (new_syms == NULL) 4615 1.17 christos return false; 4616 1.17 christos 4617 1.1 christos unsigned int num_globals2 = 0; 4618 1.1 christos unsigned int num_locals2 = 0; 4619 1.1 christos for (idx = 0; idx < symcount; idx++) 4620 1.1 christos { 4621 1.1 christos asymbol *sym = syms[idx]; 4622 1.17 christos unsigned int i; 4623 1.17 christos 4624 1.17 christos if (ignore_sym (sym)) 4625 1.3 christos continue; 4626 1.3 christos 4627 1.17 christos if (sym_is_global (abfd, sym)) 4628 1.1 christos i = num_locals + num_globals2++; 4629 1.1 christos else 4630 1.1 christos i = num_locals2++; 4631 1.1 christos new_syms[i] = sym; 4632 1.1 christos sym->udata.i = i + 1; 4633 1.1 christos } 4634 1.14 christos for (asect = abfd->sections; asect; asect = asect->next) 4635 1.17 christos { 4636 1.14 christos asymbol *sym = asect->symbol; 4637 1.1 christos if (!ignore_sym (sym) 4638 1.1 christos && sect_syms[asect->index] == NULL) 4639 1.1 christos { 4640 1.1 christos unsigned int i; 4641 1.17 christos 4642 1.17 christos sect_syms[asect->index] = sym; 4643 1.17 christos if (sym_is_global (abfd, sym)) 4644 1.1 christos i = num_locals + num_globals2++; 4645 1.1 christos else 4646 1.1 christos i = num_locals2++; 4647 1.1 christos new_syms[i] = sym; 4648 1.1 christos sym->udata.i = i + 1; 4649 1.1 christos } 4650 1.1 christos } 4651 1.1 christos 4652 1.3 christos bfd_set_symtab (abfd, new_syms, num_locals + num_globals); 4653 1.14 christos 4654 1.1 christos *pnum_locals = num_locals; 4655 1.1 christos return true; 4656 1.1 christos } 4657 1.1 christos 4658 1.1 christos /* Assign a file position to a section, optionally aligning to the 4659 1.1 christos required section alignment. */ 4660 1.1 christos 4661 1.1 christos file_ptr 4662 1.18 christos _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, 4663 1.18 christos file_ptr offset, 4664 1.1 christos bool align, 4665 1.18 christos unsigned char log_file_align) 4666 1.18 christos { 4667 1.18 christos if (i_shdrp->sh_addralign > 1) 4668 1.18 christos { 4669 1.18 christos file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign; 4670 1.18 christos 4671 1.18 christos if (align) 4672 1.18 christos offset = BFD_ALIGN (offset, salign); 4673 1.18 christos else if (log_file_align) 4674 1.18 christos { 4675 1.18 christos /* Heuristic: Cap alignment at log_file_align. */ 4676 1.18 christos file_ptr falign = 1u << log_file_align; 4677 1.18 christos 4678 1.18 christos offset = BFD_ALIGN (offset, salign < falign ? salign : falign); 4679 1.1 christos } 4680 1.1 christos } 4681 1.1 christos i_shdrp->sh_offset = offset; 4682 1.1 christos if (i_shdrp->bfd_section != NULL) 4683 1.1 christos i_shdrp->bfd_section->filepos = offset; 4684 1.1 christos if (i_shdrp->sh_type != SHT_NOBITS) 4685 1.1 christos offset += i_shdrp->sh_size; 4686 1.1 christos return offset; 4687 1.1 christos } 4688 1.1 christos 4689 1.1 christos /* Compute the file positions we are going to put the sections at, and 4690 1.1 christos otherwise prepare to begin writing out the ELF file. If LINK_INFO 4691 1.14 christos is not NULL, this is being called by the ELF backend linker. */ 4692 1.1 christos 4693 1.1 christos bool 4694 1.1 christos _bfd_elf_compute_section_file_positions (bfd *abfd, 4695 1.1 christos struct bfd_link_info *link_info) 4696 1.1 christos { 4697 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4698 1.6 christos struct fake_section_arg fsargs; 4699 1.1 christos bool failed; 4700 1.14 christos struct elf_strtab_hash *strtab = NULL; 4701 1.1 christos Elf_Internal_Shdr *shstrtab_hdr; 4702 1.1 christos bool need_symtab; 4703 1.14 christos 4704 1.1 christos if (abfd->output_has_begun) 4705 1.1 christos return true; 4706 1.1 christos 4707 1.1 christos /* Do any elf backend specific processing first. */ 4708 1.1 christos if (bed->elf_backend_begin_write_processing) 4709 1.12 christos (*bed->elf_backend_begin_write_processing) (abfd, link_info); 4710 1.14 christos 4711 1.1 christos if (!(*bed->elf_backend_init_file_header) (abfd, link_info)) 4712 1.14 christos return false; 4713 1.1 christos 4714 1.1 christos fsargs.failed = false; 4715 1.1 christos fsargs.link_info = link_info; 4716 1.14 christos bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); 4717 1.1 christos if (fsargs.failed) 4718 1.1 christos return false; 4719 1.14 christos 4720 1.1 christos if (!assign_section_numbers (abfd, link_info)) 4721 1.1 christos return false; 4722 1.1 christos 4723 1.1 christos /* The backend linker builds symbol table information itself. */ 4724 1.1 christos need_symtab = (link_info == NULL 4725 1.1 christos && (bfd_get_symcount (abfd) > 0 4726 1.1 christos || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) 4727 1.1 christos == HAS_RELOC))); 4728 1.1 christos if (need_symtab) 4729 1.1 christos { 4730 1.1 christos /* Non-zero if doing a relocatable link. */ 4731 1.14 christos int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); 4732 1.14 christos 4733 1.1 christos if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info)) 4734 1.1 christos return false; 4735 1.14 christos } 4736 1.1 christos 4737 1.1 christos failed = false; 4738 1.1 christos if (link_info == NULL) 4739 1.1 christos { 4740 1.17 christos bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); 4741 1.1 christos if (failed) 4742 1.1 christos goto err_free_strtab; 4743 1.1 christos } 4744 1.12 christos 4745 1.1 christos shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; 4746 1.8 christos /* sh_name was set in init_file_header. */ 4747 1.1 christos shstrtab_hdr->sh_type = SHT_STRTAB; 4748 1.6 christos shstrtab_hdr->sh_flags = bed->elf_strtab_flags; 4749 1.1 christos shstrtab_hdr->sh_addr = 0; 4750 1.1 christos /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ 4751 1.1 christos shstrtab_hdr->sh_entsize = 0; 4752 1.6 christos shstrtab_hdr->sh_link = 0; 4753 1.1 christos shstrtab_hdr->sh_info = 0; 4754 1.1 christos /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ 4755 1.1 christos shstrtab_hdr->sh_addralign = 1; 4756 1.17 christos 4757 1.1 christos if (!assign_file_positions_except_relocs (abfd, link_info)) 4758 1.17 christos goto err_free_strtab; 4759 1.1 christos 4760 1.1 christos if (strtab != NULL) 4761 1.1 christos { 4762 1.1 christos file_ptr off; 4763 1.3 christos Elf_Internal_Shdr *hdr; 4764 1.1 christos 4765 1.8 christos off = elf_next_file_pos (abfd); 4766 1.18 christos 4767 1.1 christos hdr = & elf_symtab_hdr (abfd); 4768 1.8 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0); 4769 1.8 christos 4770 1.8 christos if (elf_symtab_shndx_list (abfd) != NULL) 4771 1.8 christos { 4772 1.18 christos hdr = & elf_symtab_shndx_list (abfd)->hdr; 4773 1.8 christos if (hdr->sh_size != 0) 4774 1.8 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0); 4775 1.1 christos /* FIXME: What about other symtab_shndx sections in the list ? */ 4776 1.1 christos } 4777 1.18 christos 4778 1.1 christos hdr = &elf_tdata (abfd)->strtab_hdr; 4779 1.3 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0); 4780 1.1 christos 4781 1.1 christos elf_next_file_pos (abfd) = off; 4782 1.1 christos 4783 1.1 christos /* Now that we know where the .strtab section goes, write it 4784 1.6 christos out. */ 4785 1.17 christos if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 4786 1.6 christos || ! _bfd_elf_strtab_emit (abfd, strtab)) 4787 1.1 christos goto err_free_strtab; 4788 1.1 christos _bfd_elf_strtab_free (strtab); 4789 1.14 christos } 4790 1.17 christos 4791 1.1 christos abfd->output_has_begun = true; 4792 1.17 christos return true; 4793 1.17 christos 4794 1.17 christos err_free_strtab: 4795 1.17 christos if (strtab != NULL) 4796 1.14 christos _bfd_elf_strtab_free (strtab); 4797 1.14 christos return false; 4798 1.14 christos } 4799 1.14 christos 4800 1.14 christos /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the 4801 1.14 christos function effectively returns whether --eh-frame-hdr is given on the 4802 1.14 christos command line. After size_dynamic_sections the result reflects 4803 1.14 christos whether .eh_frame_hdr will actually be output (sizing isn't done 4804 1.14 christos until ldemul_after_allocation). */ 4805 1.14 christos 4806 1.14 christos static asection * 4807 1.14 christos elf_eh_frame_hdr (const struct bfd_link_info *info) 4808 1.14 christos { 4809 1.14 christos if (info != NULL && is_elf_hash_table (info->hash)) 4810 1.1 christos return elf_hash_table (info)->eh_info.hdr_sec; 4811 1.1 christos return NULL; 4812 1.1 christos } 4813 1.1 christos 4814 1.1 christos /* Make an initial estimate of the size of the program header. If we 4815 1.1 christos get the number wrong here, we'll redo section placement. */ 4816 1.1 christos 4817 1.1 christos static bfd_size_type 4818 1.1 christos get_program_header_size (bfd *abfd, struct bfd_link_info *info) 4819 1.1 christos { 4820 1.1 christos size_t segs; 4821 1.1 christos asection *s; 4822 1.1 christos const struct elf_backend_data *bed; 4823 1.1 christos 4824 1.1 christos /* Assume we will need exactly two PT_LOAD segments: one for text 4825 1.1 christos and one for data. */ 4826 1.1 christos segs = 2; 4827 1.11 christos 4828 1.1 christos s = bfd_get_section_by_name (abfd, ".interp"); 4829 1.1 christos if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) 4830 1.1 christos { 4831 1.1 christos /* If we have a loadable interpreter section, we need a 4832 1.1 christos PT_INTERP segment. In this case, assume we also need a 4833 1.1 christos PT_PHDR segment, although that may not be true for all 4834 1.1 christos targets. */ 4835 1.1 christos segs += 2; 4836 1.1 christos } 4837 1.1 christos 4838 1.1 christos if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) 4839 1.1 christos { 4840 1.1 christos /* We need a PT_DYNAMIC segment. */ 4841 1.1 christos ++segs; 4842 1.1 christos } 4843 1.1 christos 4844 1.1 christos if (info != NULL && info->relro) 4845 1.1 christos { 4846 1.1 christos /* We need a PT_GNU_RELRO segment. */ 4847 1.1 christos ++segs; 4848 1.14 christos } 4849 1.1 christos 4850 1.1 christos if (elf_eh_frame_hdr (info)) 4851 1.1 christos { 4852 1.1 christos /* We need a PT_GNU_EH_FRAME segment. */ 4853 1.1 christos ++segs; 4854 1.3 christos } 4855 1.1 christos 4856 1.1 christos if (elf_stack_flags (abfd)) 4857 1.1 christos { 4858 1.1 christos /* We need a PT_GNU_STACK segment. */ 4859 1.1 christos ++segs; 4860 1.14 christos } 4861 1.14 christos 4862 1.14 christos if (elf_sframe (abfd)) 4863 1.14 christos { 4864 1.14 christos /* We need a PT_GNU_SFRAME segment. */ 4865 1.14 christos ++segs; 4866 1.11 christos } 4867 1.11 christos 4868 1.11 christos s = bfd_get_section_by_name (abfd, 4869 1.11 christos NOTE_GNU_PROPERTY_SECTION_NAME); 4870 1.11 christos if (s != NULL && s->size != 0) 4871 1.11 christos { 4872 1.11 christos /* We need a PT_GNU_PROPERTY segment. */ 4873 1.11 christos ++segs; 4874 1.1 christos } 4875 1.1 christos 4876 1.1 christos for (s = abfd->sections; s != NULL; s = s->next) 4877 1.11 christos { 4878 1.1 christos if ((s->flags & SEC_LOAD) != 0 4879 1.11 christos && elf_section_type (s) == SHT_NOTE) 4880 1.1 christos { 4881 1.1 christos unsigned int alignment_power; 4882 1.11 christos /* We need a PT_NOTE segment. */ 4883 1.11 christos ++segs; 4884 1.11 christos /* Try to create just one PT_NOTE segment for all adjacent 4885 1.11 christos loadable SHT_NOTE sections. gABI requires that within a 4886 1.11 christos PT_NOTE segment (and also inside of each SHT_NOTE section) 4887 1.11 christos each note should have the same alignment. So we check 4888 1.11 christos whether the sections are correctly aligned. */ 4889 1.11 christos alignment_power = s->alignment_power; 4890 1.11 christos while (s->next != NULL 4891 1.11 christos && s->next->alignment_power == alignment_power 4892 1.11 christos && (s->next->flags & SEC_LOAD) != 0 4893 1.1 christos && elf_section_type (s->next) == SHT_NOTE) 4894 1.1 christos s = s->next; 4895 1.1 christos } 4896 1.1 christos } 4897 1.1 christos 4898 1.1 christos for (s = abfd->sections; s != NULL; s = s->next) 4899 1.1 christos { 4900 1.1 christos if (s->flags & SEC_THREAD_LOCAL) 4901 1.1 christos { 4902 1.1 christos /* We need a PT_TLS segment. */ 4903 1.1 christos ++segs; 4904 1.1 christos break; 4905 1.1 christos } 4906 1.1 christos } 4907 1.9 christos 4908 1.12 christos bed = get_elf_backend_data (abfd); 4909 1.12 christos 4910 1.12 christos if ((abfd->flags & D_PAGED) != 0 4911 1.12 christos && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) 4912 1.14 christos { 4913 1.14 christos /* Add a PT_GNU_MBIND segment for each mbind section. */ 4914 1.14 christos bfd_vma commonpagesize; 4915 1.14 christos unsigned int page_align_power; 4916 1.14 christos 4917 1.14 christos if (info != NULL) 4918 1.14 christos commonpagesize = info->commonpagesize; 4919 1.14 christos else 4920 1.12 christos commonpagesize = bed->commonpagesize; 4921 1.12 christos page_align_power = bfd_log2 (commonpagesize); 4922 1.12 christos for (s = abfd->sections; s != NULL; s = s->next) 4923 1.12 christos if (elf_section_flags (s) & SHF_GNU_MBIND) 4924 1.12 christos { 4925 1.12 christos if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM) 4926 1.12 christos { 4927 1.12 christos _bfd_error_handler 4928 1.12 christos /* xgettext:c-format */ 4929 1.12 christos (_("%pB: GNU_MBIND section `%pA' has invalid " 4930 1.12 christos "sh_info field: %d"), 4931 1.12 christos abfd, s, elf_section_data (s)->this_hdr.sh_info); 4932 1.12 christos continue; 4933 1.12 christos } 4934 1.12 christos /* Align mbind section to page size. */ 4935 1.12 christos if (s->alignment_power < page_align_power) 4936 1.12 christos s->alignment_power = page_align_power; 4937 1.12 christos segs ++; 4938 1.9 christos } 4939 1.12 christos } 4940 1.12 christos 4941 1.1 christos /* Let the backend count up any program headers it might need. */ 4942 1.1 christos if (bed->elf_backend_additional_program_headers) 4943 1.1 christos { 4944 1.1 christos int a; 4945 1.1 christos 4946 1.1 christos a = (*bed->elf_backend_additional_program_headers) (abfd, info); 4947 1.1 christos if (a == -1) 4948 1.1 christos abort (); 4949 1.1 christos segs += a; 4950 1.1 christos } 4951 1.1 christos 4952 1.1 christos return segs * bed->s->sizeof_phdr; 4953 1.1 christos } 4954 1.1 christos 4955 1.1 christos /* Find the segment that contains the output_section of section. */ 4956 1.1 christos 4957 1.1 christos Elf_Internal_Phdr * 4958 1.1 christos _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) 4959 1.1 christos { 4960 1.1 christos struct elf_segment_map *m; 4961 1.3 christos Elf_Internal_Phdr *p; 4962 1.1 christos 4963 1.1 christos for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; 4964 1.1 christos m != NULL; 4965 1.1 christos m = m->next, p++) 4966 1.1 christos { 4967 1.1 christos int i; 4968 1.1 christos 4969 1.1 christos for (i = m->count - 1; i >= 0; i--) 4970 1.1 christos if (m->sections[i] == section) 4971 1.1 christos return p; 4972 1.1 christos } 4973 1.1 christos 4974 1.1 christos return NULL; 4975 1.1 christos } 4976 1.1 christos 4977 1.1 christos /* Create a mapping from a set of sections to a program segment. */ 4978 1.1 christos 4979 1.1 christos static struct elf_segment_map * 4980 1.1 christos make_mapping (bfd *abfd, 4981 1.1 christos asection **sections, 4982 1.14 christos unsigned int from, 4983 1.1 christos unsigned int to, 4984 1.1 christos bool phdr) 4985 1.1 christos { 4986 1.1 christos struct elf_segment_map *m; 4987 1.12 christos unsigned int i; 4988 1.1 christos asection **hdrpp; 4989 1.11 christos size_t amt; 4990 1.11 christos 4991 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *); 4992 1.1 christos amt += (to - from) * sizeof (asection *); 4993 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 4994 1.1 christos if (m == NULL) 4995 1.1 christos return NULL; 4996 1.1 christos m->next = NULL; 4997 1.1 christos m->p_type = PT_LOAD; 4998 1.1 christos for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) 4999 1.1 christos m->sections[i - from] = *hdrpp; 5000 1.1 christos m->count = to - from; 5001 1.1 christos 5002 1.1 christos if (from == 0 && phdr) 5003 1.1 christos { 5004 1.1 christos /* Include the headers in the first PT_LOAD segment. */ 5005 1.1 christos m->includes_filehdr = 1; 5006 1.1 christos m->includes_phdrs = 1; 5007 1.1 christos } 5008 1.1 christos 5009 1.1 christos return m; 5010 1.1 christos } 5011 1.1 christos 5012 1.1 christos /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL 5013 1.1 christos on failure. */ 5014 1.1 christos 5015 1.1 christos struct elf_segment_map * 5016 1.1 christos _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) 5017 1.1 christos { 5018 1.1 christos struct elf_segment_map *m; 5019 1.11 christos 5020 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, 5021 1.1 christos sizeof (struct elf_segment_map)); 5022 1.1 christos if (m == NULL) 5023 1.1 christos return NULL; 5024 1.1 christos m->next = NULL; 5025 1.1 christos m->p_type = PT_DYNAMIC; 5026 1.1 christos m->count = 1; 5027 1.1 christos m->sections[0] = dynsec; 5028 1.1 christos 5029 1.1 christos return m; 5030 1.1 christos } 5031 1.1 christos 5032 1.14 christos /* Possibly add or remove segments from the segment map. */ 5033 1.1 christos 5034 1.1 christos static bool 5035 1.14 christos elf_modify_segment_map (bfd *abfd, 5036 1.1 christos struct bfd_link_info *info, 5037 1.1 christos bool remove_empty_load) 5038 1.1 christos { 5039 1.1 christos struct elf_segment_map **m; 5040 1.1 christos const struct elf_backend_data *bed; 5041 1.1 christos 5042 1.1 christos /* The placement algorithm assumes that non allocated sections are 5043 1.1 christos not in PT_LOAD segments. We ensure this here by removing such 5044 1.1 christos sections from the segment map. We also remove excluded 5045 1.3 christos sections. Finally, any PT_LOAD segment without sections is 5046 1.1 christos removed. */ 5047 1.1 christos m = &elf_seg_map (abfd); 5048 1.1 christos while (*m) 5049 1.1 christos { 5050 1.1 christos unsigned int i, new_count; 5051 1.1 christos 5052 1.1 christos for (new_count = 0, i = 0; i < (*m)->count; i++) 5053 1.1 christos { 5054 1.1 christos if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 5055 1.1 christos && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 5056 1.1 christos || (*m)->p_type != PT_LOAD)) 5057 1.1 christos { 5058 1.1 christos (*m)->sections[new_count] = (*m)->sections[i]; 5059 1.1 christos new_count++; 5060 1.1 christos } 5061 1.1 christos } 5062 1.9 christos (*m)->count = new_count; 5063 1.9 christos 5064 1.9 christos if (remove_empty_load 5065 1.9 christos && (*m)->p_type == PT_LOAD 5066 1.1 christos && (*m)->count == 0 5067 1.1 christos && !(*m)->includes_phdrs) 5068 1.1 christos *m = (*m)->next; 5069 1.1 christos else 5070 1.1 christos m = &(*m)->next; 5071 1.1 christos } 5072 1.1 christos 5073 1.1 christos bed = get_elf_backend_data (abfd); 5074 1.1 christos if (bed->elf_backend_modify_segment_map != NULL) 5075 1.14 christos { 5076 1.1 christos if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) 5077 1.1 christos return false; 5078 1.14 christos } 5079 1.1 christos 5080 1.1 christos return true; 5081 1.11 christos } 5082 1.11 christos 5083 1.11 christos #define IS_TBSS(s) \ 5084 1.14 christos ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL) 5085 1.14 christos 5086 1.1 christos /* Set up a mapping from BFD sections to program segments. Update 5087 1.14 christos NEED_LAYOUT if the section layout is changed. */ 5088 1.14 christos 5089 1.14 christos bool 5090 1.14 christos _bfd_elf_map_sections_to_segments (bfd *abfd, 5091 1.1 christos struct bfd_link_info *info, 5092 1.1 christos bool *need_layout) 5093 1.1 christos { 5094 1.1 christos unsigned int count; 5095 1.1 christos struct elf_segment_map *m; 5096 1.14 christos asection **sections = NULL; 5097 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 5098 1.3 christos bool no_user_phdrs; 5099 1.3 christos 5100 1.3 christos no_user_phdrs = elf_seg_map (abfd) == NULL; 5101 1.14 christos 5102 1.14 christos if (info != NULL) 5103 1.14 christos { 5104 1.14 christos info->user_phdrs = !no_user_phdrs; 5105 1.14 christos 5106 1.14 christos /* Size the relative relocations if DT_RELR is enabled. */ 5107 1.14 christos if (info->enable_dt_relr 5108 1.14 christos && need_layout != NULL 5109 1.19 christos && bed->size_relative_relocs 5110 1.19 christos && !bed->size_relative_relocs (info, need_layout)) 5111 1.14 christos info->callbacks->fatal 5112 1.3 christos (_("%P: failed to size relative relocations\n")); 5113 1.1 christos } 5114 1.1 christos 5115 1.1 christos if (no_user_phdrs && bfd_count_sections (abfd) != 0) 5116 1.1 christos { 5117 1.1 christos asection *s; 5118 1.1 christos unsigned int i; 5119 1.1 christos struct elf_segment_map *mfirst; 5120 1.1 christos struct elf_segment_map **pm; 5121 1.11 christos asection *last_hdr; 5122 1.1 christos bfd_vma last_size; 5123 1.1 christos unsigned int hdr_index; 5124 1.14 christos bfd_vma maxpagesize; 5125 1.14 christos asection **hdrpp; 5126 1.14 christos bool phdr_in_segment; 5127 1.12 christos bool writable; 5128 1.1 christos bool executable; 5129 1.9 christos unsigned int tls_count = 0; 5130 1.1 christos asection *first_tls = NULL; 5131 1.14 christos asection *first_mbind = NULL; 5132 1.12 christos asection *dynsec, *eh_frame_hdr; 5133 1.12 christos asection *sframe; 5134 1.12 christos size_t amt; 5135 1.12 christos bfd_vma addr_mask, wrap_to = 0; /* Bytes. */ 5136 1.1 christos bfd_size_type phdr_size; /* Octets/bytes. */ 5137 1.1 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL); 5138 1.1 christos 5139 1.12 christos /* Select the allocated sections, and sort them. */ 5140 1.12 christos 5141 1.1 christos amt = bfd_count_sections (abfd) * sizeof (asection *); 5142 1.1 christos sections = (asection **) bfd_malloc (amt); 5143 1.1 christos if (sections == NULL) 5144 1.1 christos goto error_return; 5145 1.1 christos 5146 1.1 christos /* Calculate top address, avoiding undefined behaviour of shift 5147 1.1 christos left operator when shift count is equal to size of type 5148 1.1 christos being shifted. */ 5149 1.1 christos addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; 5150 1.1 christos addr_mask = (addr_mask << 1) + 1; 5151 1.1 christos 5152 1.1 christos i = 0; 5153 1.1 christos for (s = abfd->sections; s != NULL; s = s->next) 5154 1.1 christos { 5155 1.12 christos if ((s->flags & SEC_ALLOC) != 0) 5156 1.12 christos { 5157 1.12 christos /* target_index is unused until bfd_elf_final_link 5158 1.12 christos starts output of section symbols. Use it to make 5159 1.1 christos qsort stable. */ 5160 1.1 christos s->target_index = i; 5161 1.1 christos sections[i] = s; 5162 1.12 christos ++i; 5163 1.12 christos /* A wrapping section potentially clashes with header. */ 5164 1.1 christos if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask)) 5165 1.1 christos wrap_to = (s->lma + s->size / opb) & addr_mask; 5166 1.1 christos } 5167 1.1 christos } 5168 1.1 christos BFD_ASSERT (i <= bfd_count_sections (abfd)); 5169 1.1 christos count = i; 5170 1.1 christos 5171 1.11 christos qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); 5172 1.11 christos 5173 1.11 christos phdr_size = elf_program_header_size (abfd); 5174 1.11 christos if (phdr_size == (bfd_size_type) -1) 5175 1.12 christos phdr_size = get_program_header_size (abfd, info); 5176 1.12 christos phdr_size += bed->s->sizeof_ehdr; 5177 1.14 christos /* phdr_size is compared to LMA values which are in bytes. */ 5178 1.14 christos phdr_size /= opb; 5179 1.14 christos if (info != NULL) 5180 1.14 christos maxpagesize = info->maxpagesize; 5181 1.11 christos else 5182 1.11 christos maxpagesize = bed->maxpagesize; 5183 1.11 christos if (maxpagesize == 0) 5184 1.11 christos maxpagesize = 1; 5185 1.11 christos phdr_in_segment = info != NULL && info->load_phdrs; 5186 1.11 christos if (count != 0 5187 1.11 christos && (((sections[0]->lma & addr_mask) & (maxpagesize - 1)) 5188 1.11 christos >= (phdr_size & (maxpagesize - 1)))) 5189 1.11 christos /* For compatibility with old scripts that may not be using 5190 1.14 christos SIZEOF_HEADERS, add headers when it looks like space has 5191 1.11 christos been left for them. */ 5192 1.1 christos phdr_in_segment = true; 5193 1.1 christos 5194 1.1 christos /* Build the mapping. */ 5195 1.1 christos mfirst = NULL; 5196 1.1 christos pm = &mfirst; 5197 1.1 christos 5198 1.1 christos /* If we have a .interp section, then create a PT_PHDR segment for 5199 1.1 christos the program headers and a PT_INTERP segment for the .interp 5200 1.11 christos section. */ 5201 1.1 christos s = bfd_get_section_by_name (abfd, ".interp"); 5202 1.1 christos if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) 5203 1.1 christos { 5204 1.1 christos amt = sizeof (struct elf_segment_map); 5205 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5206 1.1 christos if (m == NULL) 5207 1.1 christos goto error_return; 5208 1.11 christos m->next = NULL; 5209 1.1 christos m->p_type = PT_PHDR; 5210 1.1 christos m->p_flags = PF_R; 5211 1.14 christos m->p_flags_valid = 1; 5212 1.1 christos m->includes_phdrs = 1; 5213 1.1 christos phdr_in_segment = true; 5214 1.1 christos *pm = m; 5215 1.1 christos pm = &m->next; 5216 1.1 christos 5217 1.1 christos amt = sizeof (struct elf_segment_map); 5218 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5219 1.1 christos if (m == NULL) 5220 1.1 christos goto error_return; 5221 1.1 christos m->next = NULL; 5222 1.1 christos m->p_type = PT_INTERP; 5223 1.1 christos m->count = 1; 5224 1.1 christos m->sections[0] = s; 5225 1.1 christos 5226 1.1 christos *pm = m; 5227 1.1 christos pm = &m->next; 5228 1.1 christos } 5229 1.1 christos 5230 1.1 christos /* Look through the sections. We put sections in the same program 5231 1.1 christos segment when the start of the second section can be placed within 5232 1.1 christos a few bytes of the end of the first section. */ 5233 1.11 christos last_hdr = NULL; 5234 1.14 christos last_size = 0; 5235 1.14 christos hdr_index = 0; 5236 1.1 christos writable = false; 5237 1.1 christos executable = false; 5238 1.1 christos dynsec = bfd_get_section_by_name (abfd, ".dynamic"); 5239 1.1 christos if (dynsec != NULL 5240 1.1 christos && (dynsec->flags & SEC_LOAD) == 0) 5241 1.11 christos dynsec = NULL; 5242 1.14 christos 5243 1.11 christos if ((abfd->flags & D_PAGED) == 0) 5244 1.1 christos phdr_in_segment = false; 5245 1.1 christos 5246 1.1 christos /* Deal with -Ttext or something similar such that the first section 5247 1.1 christos is not adjacent to the program headers. This is an 5248 1.11 christos approximation, since at this point we don't know exactly how many 5249 1.1 christos program headers we will need. */ 5250 1.12 christos if (phdr_in_segment && count > 0) 5251 1.14 christos { 5252 1.1 christos bfd_vma phdr_lma; /* Bytes. */ 5253 1.11 christos bool separate_phdr = false; 5254 1.11 christos 5255 1.11 christos phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize; 5256 1.11 christos if (info != NULL 5257 1.11 christos && info->separate_code 5258 1.11 christos && (sections[0]->flags & SEC_CODE) != 0) 5259 1.11 christos { 5260 1.11 christos /* If data sections should be separate from code and 5261 1.11 christos thus not executable, and the first section is 5262 1.18 christos executable then put the file and program headers in 5263 1.18 christos their own PT_LOAD. */ 5264 1.18 christos if (!info->one_rosegment) 5265 1.11 christos separate_phdr = true; 5266 1.11 christos 5267 1.11 christos if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize) 5268 1.11 christos == (sections[0]->lma & addr_mask & -maxpagesize))) 5269 1.11 christos { 5270 1.11 christos /* The file and program headers are currently on the 5271 1.11 christos same page as the first section. Put them on the 5272 1.11 christos previous page if we can. */ 5273 1.11 christos if (phdr_lma >= maxpagesize) 5274 1.14 christos phdr_lma -= maxpagesize; 5275 1.11 christos else 5276 1.11 christos separate_phdr = false; 5277 1.11 christos } 5278 1.11 christos } 5279 1.11 christos if ((sections[0]->lma & addr_mask) < phdr_lma 5280 1.11 christos || (sections[0]->lma & addr_mask) < phdr_size) 5281 1.14 christos /* If file and program headers would be placed at the end 5282 1.11 christos of memory then it's probably better to omit them. */ 5283 1.11 christos phdr_in_segment = false; 5284 1.11 christos else if (phdr_lma < wrap_to) 5285 1.11 christos /* If a section wraps around to where we'll be placing 5286 1.14 christos file and program headers, then the headers will be 5287 1.11 christos overwritten. */ 5288 1.11 christos phdr_in_segment = false; 5289 1.11 christos else if (separate_phdr) 5290 1.11 christos { 5291 1.11 christos m = make_mapping (abfd, sections, 0, 0, phdr_in_segment); 5292 1.12 christos if (m == NULL) 5293 1.11 christos goto error_return; 5294 1.11 christos m->p_paddr = phdr_lma * opb; 5295 1.11 christos m->p_vaddr_offset 5296 1.11 christos = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize; 5297 1.11 christos m->p_paddr_valid = 1; 5298 1.14 christos *pm = m; 5299 1.9 christos pm = &m->next; 5300 1.1 christos phdr_in_segment = false; 5301 1.1 christos } 5302 1.1 christos } 5303 1.1 christos 5304 1.1 christos for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) 5305 1.14 christos { 5306 1.1 christos asection *hdr; 5307 1.1 christos bool new_segment; 5308 1.1 christos 5309 1.1 christos hdr = *hdrpp; 5310 1.1 christos 5311 1.1 christos /* See if this section and the last one will fit in the same 5312 1.1 christos segment. */ 5313 1.1 christos 5314 1.1 christos if (last_hdr == NULL) 5315 1.1 christos { 5316 1.14 christos /* If we don't have a segment yet, then we don't need a new 5317 1.1 christos one (we build the last one after this loop). */ 5318 1.1 christos new_segment = false; 5319 1.1 christos } 5320 1.1 christos else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) 5321 1.1 christos { 5322 1.1 christos /* If this section has a different relation between the 5323 1.14 christos virtual address and the load address, then we need a new 5324 1.1 christos segment. */ 5325 1.1 christos new_segment = true; 5326 1.1 christos } 5327 1.1 christos else if (hdr->lma < last_hdr->lma + last_size 5328 1.1 christos || last_hdr->lma + last_size < last_hdr->lma) 5329 1.1 christos { 5330 1.14 christos /* If this section has a load address that makes it overlap 5331 1.1 christos the previous section, then we need a new segment. */ 5332 1.11 christos new_segment = true; 5333 1.11 christos } 5334 1.11 christos else if ((abfd->flags & D_PAGED) != 0 5335 1.11 christos && (((last_hdr->lma + last_size - 1) & -maxpagesize) 5336 1.11 christos == (hdr->lma & -maxpagesize))) 5337 1.11 christos { 5338 1.14 christos /* If we are demand paged then we can't map two disk 5339 1.11 christos pages onto the same memory page. */ 5340 1.1 christos new_segment = false; 5341 1.1 christos } 5342 1.1 christos /* In the next test we have to be careful when last_hdr->lma is close 5343 1.1 christos to the end of the address space. If the aligned address wraps 5344 1.1 christos around to the start of the address space, then there are no more 5345 1.11 christos pages left in memory and it is OK to assume that the current 5346 1.11 christos section can be included in the current segment. */ 5347 1.11 christos else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) 5348 1.11 christos + maxpagesize > last_hdr->lma) 5349 1.1 christos && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) 5350 1.1 christos + maxpagesize <= hdr->lma)) 5351 1.1 christos { 5352 1.14 christos /* If putting this section in this segment would force us to 5353 1.1 christos skip a page in the segment, then we need a new segment. */ 5354 1.1 christos new_segment = true; 5355 1.11 christos } 5356 1.8 christos else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 5357 1.8 christos && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) 5358 1.8 christos { 5359 1.8 christos /* We don't want to put a loaded section after a 5360 1.11 christos nonloaded (ie. bss style) section in the same segment 5361 1.14 christos as that will force the non-loaded section to be loaded. 5362 1.1 christos Consider .tbss sections as loaded for this purpose. */ 5363 1.1 christos new_segment = true; 5364 1.1 christos } 5365 1.1 christos else if ((abfd->flags & D_PAGED) == 0) 5366 1.1 christos { 5367 1.1 christos /* If the file is not demand paged, which means that we 5368 1.14 christos don't require the sections to be correctly aligned in the 5369 1.1 christos file, then there is no other reason for a new segment. */ 5370 1.11 christos new_segment = false; 5371 1.11 christos } 5372 1.11 christos else if (info != NULL 5373 1.11 christos && info->separate_code 5374 1.14 christos && executable != ((hdr->flags & SEC_CODE) != 0)) 5375 1.11 christos { 5376 1.1 christos new_segment = true; 5377 1.11 christos } 5378 1.1 christos else if (! writable 5379 1.1 christos && (hdr->flags & SEC_READONLY) == 0) 5380 1.11 christos { 5381 1.14 christos /* We don't want to put a writable section in a read only 5382 1.1 christos segment. */ 5383 1.1 christos new_segment = true; 5384 1.1 christos } 5385 1.1 christos else 5386 1.14 christos { 5387 1.1 christos /* Otherwise, we can use the same segment. */ 5388 1.1 christos new_segment = false; 5389 1.1 christos } 5390 1.1 christos 5391 1.1 christos /* Allow interested parties a chance to override our decision. */ 5392 1.1 christos if (last_hdr != NULL 5393 1.1 christos && info != NULL 5394 1.1 christos && info->callbacks->override_segment_assignment != NULL) 5395 1.1 christos new_segment 5396 1.1 christos = info->callbacks->override_segment_assignment (info, abfd, hdr, 5397 1.1 christos last_hdr, 5398 1.1 christos new_segment); 5399 1.1 christos 5400 1.1 christos if (! new_segment) 5401 1.14 christos { 5402 1.11 christos if ((hdr->flags & SEC_READONLY) == 0) 5403 1.14 christos writable = true; 5404 1.1 christos if ((hdr->flags & SEC_CODE) != 0) 5405 1.1 christos executable = true; 5406 1.12 christos last_hdr = hdr; 5407 1.1 christos /* .tbss sections effectively have zero size. */ 5408 1.1 christos last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb; 5409 1.1 christos continue; 5410 1.1 christos } 5411 1.11 christos 5412 1.1 christos /* We need a new program segment. We must create a new program 5413 1.11 christos header holding all the sections from hdr_index until hdr. */ 5414 1.1 christos 5415 1.1 christos m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); 5416 1.1 christos if (m == NULL) 5417 1.1 christos goto error_return; 5418 1.1 christos 5419 1.1 christos *pm = m; 5420 1.1 christos pm = &m->next; 5421 1.14 christos 5422 1.1 christos if ((hdr->flags & SEC_READONLY) == 0) 5423 1.14 christos writable = true; 5424 1.1 christos else 5425 1.11 christos writable = false; 5426 1.14 christos 5427 1.11 christos if ((hdr->flags & SEC_CODE) == 0) 5428 1.14 christos executable = false; 5429 1.11 christos else 5430 1.1 christos executable = true; 5431 1.1 christos 5432 1.12 christos last_hdr = hdr; 5433 1.11 christos /* .tbss sections effectively have zero size. */ 5434 1.14 christos last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb; 5435 1.1 christos hdr_index = i; 5436 1.1 christos phdr_in_segment = false; 5437 1.3 christos } 5438 1.3 christos 5439 1.3 christos /* Create a final PT_LOAD program segment, but not if it's just 5440 1.11 christos for .tbss. */ 5441 1.11 christos if (last_hdr != NULL 5442 1.1 christos && (i - hdr_index != 1 5443 1.11 christos || !IS_TBSS (last_hdr))) 5444 1.1 christos { 5445 1.1 christos m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); 5446 1.1 christos if (m == NULL) 5447 1.1 christos goto error_return; 5448 1.1 christos 5449 1.1 christos *pm = m; 5450 1.1 christos pm = &m->next; 5451 1.1 christos } 5452 1.1 christos 5453 1.1 christos /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ 5454 1.1 christos if (dynsec != NULL) 5455 1.1 christos { 5456 1.1 christos m = _bfd_elf_make_dynamic_segment (abfd, dynsec); 5457 1.1 christos if (m == NULL) 5458 1.1 christos goto error_return; 5459 1.1 christos *pm = m; 5460 1.1 christos pm = &m->next; 5461 1.11 christos } 5462 1.1 christos 5463 1.1 christos /* For each batch of consecutive loadable SHT_NOTE sections, 5464 1.1 christos add a PT_NOTE segment. We don't use bfd_get_section_by_name, 5465 1.11 christos because if we link together nonloadable .note sections and 5466 1.1 christos loadable .note sections, we will generate two .note sections 5467 1.1 christos in the output file. */ 5468 1.1 christos for (s = abfd->sections; s != NULL; s = s->next) 5469 1.11 christos { 5470 1.1 christos if ((s->flags & SEC_LOAD) != 0 5471 1.1 christos && elf_section_type (s) == SHT_NOTE) 5472 1.11 christos { 5473 1.1 christos asection *s2; 5474 1.1 christos unsigned int alignment_power = s->alignment_power; 5475 1.11 christos 5476 1.11 christos count = 1; 5477 1.11 christos for (s2 = s; s2->next != NULL; s2 = s2->next) 5478 1.11 christos { 5479 1.11 christos if (s2->next->alignment_power == alignment_power 5480 1.12 christos && (s2->next->flags & SEC_LOAD) != 0 5481 1.11 christos && elf_section_type (s2->next) == SHT_NOTE 5482 1.11 christos && align_power (s2->lma + s2->size / opb, 5483 1.11 christos alignment_power) 5484 1.11 christos == s2->next->lma) 5485 1.11 christos count++; 5486 1.11 christos else 5487 1.11 christos break; 5488 1.11 christos } 5489 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *); 5490 1.1 christos amt += count * sizeof (asection *); 5491 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5492 1.1 christos if (m == NULL) 5493 1.1 christos goto error_return; 5494 1.1 christos m->next = NULL; 5495 1.1 christos m->p_type = PT_NOTE; 5496 1.1 christos m->count = count; 5497 1.1 christos while (count > 1) 5498 1.1 christos { 5499 1.1 christos m->sections[m->count - count--] = s; 5500 1.1 christos BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); 5501 1.1 christos s = s->next; 5502 1.1 christos } 5503 1.1 christos m->sections[m->count - 1] = s; 5504 1.1 christos BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); 5505 1.1 christos *pm = m; 5506 1.1 christos pm = &m->next; 5507 1.1 christos } 5508 1.1 christos if (s->flags & SEC_THREAD_LOCAL) 5509 1.1 christos { 5510 1.1 christos if (! tls_count) 5511 1.1 christos first_tls = s; 5512 1.9 christos tls_count++; 5513 1.9 christos } 5514 1.9 christos if (first_mbind == NULL 5515 1.1 christos && (elf_section_flags (s) & SHF_GNU_MBIND) != 0) 5516 1.1 christos first_mbind = s; 5517 1.1 christos } 5518 1.1 christos 5519 1.1 christos /* If there are any SHF_TLS output sections, add PT_TLS segment. */ 5520 1.11 christos if (tls_count > 0) 5521 1.11 christos { 5522 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *); 5523 1.1 christos amt += tls_count * sizeof (asection *); 5524 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5525 1.1 christos if (m == NULL) 5526 1.1 christos goto error_return; 5527 1.1 christos m->next = NULL; 5528 1.1 christos m->p_type = PT_TLS; 5529 1.1 christos m->count = tls_count; 5530 1.1 christos /* Mandated PF_R. */ 5531 1.5 christos m->p_flags = PF_R; 5532 1.12 christos m->p_flags_valid = 1; 5533 1.1 christos s = first_tls; 5534 1.5 christos for (i = 0; i < tls_count; ++i) 5535 1.5 christos { 5536 1.5 christos if ((s->flags & SEC_THREAD_LOCAL) == 0) 5537 1.11 christos { 5538 1.5 christos _bfd_error_handler 5539 1.5 christos (_("%pB: TLS sections are not adjacent:"), abfd); 5540 1.12 christos s = first_tls; 5541 1.5 christos i = 0; 5542 1.5 christos while (i < tls_count) 5543 1.5 christos { 5544 1.11 christos if ((s->flags & SEC_THREAD_LOCAL) != 0) 5545 1.5 christos { 5546 1.5 christos _bfd_error_handler (_(" TLS: %pA"), s); 5547 1.5 christos i++; 5548 1.11 christos } 5549 1.5 christos else 5550 1.5 christos _bfd_error_handler (_(" non-TLS: %pA"), s); 5551 1.5 christos s = s->next; 5552 1.5 christos } 5553 1.5 christos bfd_set_error (bfd_error_bad_value); 5554 1.5 christos goto error_return; 5555 1.5 christos } 5556 1.1 christos m->sections[i] = s; 5557 1.1 christos s = s->next; 5558 1.1 christos } 5559 1.1 christos 5560 1.1 christos *pm = m; 5561 1.1 christos pm = &m->next; 5562 1.12 christos } 5563 1.12 christos 5564 1.12 christos if (first_mbind 5565 1.9 christos && (abfd->flags & D_PAGED) != 0 5566 1.9 christos && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) 5567 1.12 christos for (s = first_mbind; s != NULL; s = s->next) 5568 1.9 christos if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0 5569 1.9 christos && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM) 5570 1.9 christos { 5571 1.9 christos /* Mandated PF_R. */ 5572 1.9 christos unsigned long p_flags = PF_R; 5573 1.9 christos if ((s->flags & SEC_READONLY) == 0) 5574 1.9 christos p_flags |= PF_W; 5575 1.9 christos if ((s->flags & SEC_CODE) != 0) 5576 1.9 christos p_flags |= PF_X; 5577 1.9 christos 5578 1.9 christos amt = sizeof (struct elf_segment_map) + sizeof (asection *); 5579 1.9 christos m = bfd_zalloc (abfd, amt); 5580 1.9 christos if (m == NULL) 5581 1.9 christos goto error_return; 5582 1.9 christos m->next = NULL; 5583 1.9 christos m->p_type = (PT_GNU_MBIND_LO 5584 1.9 christos + elf_section_data (s)->this_hdr.sh_info); 5585 1.9 christos m->count = 1; 5586 1.9 christos m->p_flags_valid = 1; 5587 1.9 christos m->sections[0] = s; 5588 1.9 christos m->p_flags = p_flags; 5589 1.9 christos 5590 1.9 christos *pm = m; 5591 1.9 christos pm = &m->next; 5592 1.11 christos } 5593 1.11 christos 5594 1.11 christos s = bfd_get_section_by_name (abfd, 5595 1.11 christos NOTE_GNU_PROPERTY_SECTION_NAME); 5596 1.11 christos if (s != NULL && s->size != 0) 5597 1.11 christos { 5598 1.11 christos amt = sizeof (struct elf_segment_map) + sizeof (asection *); 5599 1.11 christos m = bfd_zalloc (abfd, amt); 5600 1.11 christos if (m == NULL) 5601 1.11 christos goto error_return; 5602 1.11 christos m->next = NULL; 5603 1.11 christos m->p_type = PT_GNU_PROPERTY; 5604 1.11 christos m->count = 1; 5605 1.11 christos m->p_flags_valid = 1; 5606 1.11 christos m->sections[0] = s; 5607 1.11 christos m->p_flags = PF_R; 5608 1.11 christos *pm = m; 5609 1.11 christos pm = &m->next; 5610 1.1 christos } 5611 1.1 christos 5612 1.14 christos /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME 5613 1.1 christos segment. */ 5614 1.1 christos eh_frame_hdr = elf_eh_frame_hdr (info); 5615 1.1 christos if (eh_frame_hdr != NULL 5616 1.1 christos && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) 5617 1.1 christos { 5618 1.1 christos amt = sizeof (struct elf_segment_map); 5619 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5620 1.1 christos if (m == NULL) 5621 1.1 christos goto error_return; 5622 1.1 christos m->next = NULL; 5623 1.1 christos m->p_type = PT_GNU_EH_FRAME; 5624 1.1 christos m->count = 1; 5625 1.1 christos m->sections[0] = eh_frame_hdr->output_section; 5626 1.1 christos 5627 1.1 christos *pm = m; 5628 1.1 christos pm = &m->next; 5629 1.14 christos } 5630 1.14 christos 5631 1.14 christos /* If there is a .sframe section, throw in a PT_GNU_SFRAME 5632 1.14 christos segment. */ 5633 1.14 christos sframe = elf_sframe (abfd); 5634 1.14 christos if (sframe != NULL 5635 1.14 christos && (sframe->output_section->flags & SEC_LOAD) != 0 5636 1.14 christos && sframe->size != 0) 5637 1.14 christos { 5638 1.14 christos amt = sizeof (struct elf_segment_map); 5639 1.14 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5640 1.14 christos if (m == NULL) 5641 1.14 christos goto error_return; 5642 1.14 christos m->next = NULL; 5643 1.14 christos m->p_type = PT_GNU_SFRAME; 5644 1.14 christos m->count = 1; 5645 1.14 christos m->sections[0] = sframe->output_section; 5646 1.14 christos 5647 1.14 christos *pm = m; 5648 1.14 christos pm = &m->next; 5649 1.3 christos } 5650 1.1 christos 5651 1.1 christos if (elf_stack_flags (abfd)) 5652 1.1 christos { 5653 1.1 christos amt = sizeof (struct elf_segment_map); 5654 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5655 1.1 christos if (m == NULL) 5656 1.1 christos goto error_return; 5657 1.3 christos m->next = NULL; 5658 1.3 christos m->p_type = PT_GNU_STACK; 5659 1.1 christos m->p_flags = elf_stack_flags (abfd); 5660 1.3 christos m->p_align = bed->stack_align; 5661 1.3 christos m->p_flags_valid = 1; 5662 1.3 christos m->p_align_valid = m->p_align != 0; 5663 1.3 christos if (info->stacksize > 0) 5664 1.3 christos { 5665 1.3 christos m->p_size = info->stacksize; 5666 1.1 christos m->p_size_valid = 1; 5667 1.1 christos } 5668 1.1 christos 5669 1.1 christos *pm = m; 5670 1.1 christos pm = &m->next; 5671 1.1 christos } 5672 1.1 christos 5673 1.1 christos if (info != NULL && info->relro) 5674 1.1 christos { 5675 1.3 christos for (m = mfirst; m != NULL; m = m->next) 5676 1.3 christos { 5677 1.3 christos if (m->p_type == PT_LOAD 5678 1.3 christos && m->count != 0 5679 1.1 christos && m->sections[0]->vma >= info->relro_start 5680 1.3 christos && m->sections[0]->vma < info->relro_end) 5681 1.3 christos { 5682 1.12 christos i = m->count; 5683 1.12 christos while (--i != (unsigned) -1) 5684 1.14 christos { 5685 1.14 christos if (m->sections[i]->size > 0 5686 1.12 christos && (m->sections[i]->flags & SEC_LOAD) != 0 5687 1.12 christos && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0) 5688 1.3 christos break; 5689 1.5 christos } 5690 1.1 christos 5691 1.1 christos if (i != (unsigned) -1) 5692 1.3 christos break; 5693 1.1 christos } 5694 1.1 christos } 5695 1.1 christos 5696 1.1 christos /* Make a PT_GNU_RELRO segment only when it isn't empty. */ 5697 1.1 christos if (m != NULL) 5698 1.1 christos { 5699 1.1 christos amt = sizeof (struct elf_segment_map); 5700 1.1 christos m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5701 1.1 christos if (m == NULL) 5702 1.1 christos goto error_return; 5703 1.1 christos m->next = NULL; 5704 1.1 christos m->p_type = PT_GNU_RELRO; 5705 1.1 christos *pm = m; 5706 1.1 christos pm = &m->next; 5707 1.1 christos } 5708 1.1 christos } 5709 1.3 christos 5710 1.1 christos free (sections); 5711 1.1 christos elf_seg_map (abfd) = mfirst; 5712 1.17 christos } 5713 1.14 christos 5714 1.1 christos if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL)) 5715 1.3 christos return false; 5716 1.1 christos 5717 1.3 christos for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) 5718 1.1 christos ++count; 5719 1.14 christos elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; 5720 1.1 christos 5721 1.1 christos return true; 5722 1.12 christos 5723 1.14 christos error_return: 5724 1.1 christos free (sections); 5725 1.1 christos return false; 5726 1.1 christos } 5727 1.1 christos 5728 1.1 christos /* Sort sections by address. */ 5729 1.1 christos 5730 1.1 christos static int 5731 1.1 christos elf_sort_sections (const void *arg1, const void *arg2) 5732 1.1 christos { 5733 1.1 christos const asection *sec1 = *(const asection **) arg1; 5734 1.1 christos const asection *sec2 = *(const asection **) arg2; 5735 1.1 christos bfd_size_type size1, size2; 5736 1.1 christos 5737 1.1 christos /* Sort by LMA first, since this is the address used to 5738 1.1 christos place the section into a segment. */ 5739 1.1 christos if (sec1->lma < sec2->lma) 5740 1.1 christos return -1; 5741 1.1 christos else if (sec1->lma > sec2->lma) 5742 1.1 christos return 1; 5743 1.1 christos 5744 1.1 christos /* Then sort by VMA. Normally the LMA and the VMA will be 5745 1.1 christos the same, and this will do nothing. */ 5746 1.1 christos if (sec1->vma < sec2->vma) 5747 1.1 christos return -1; 5748 1.1 christos else if (sec1->vma > sec2->vma) 5749 1.1 christos return 1; 5750 1.1 christos 5751 1.14 christos /* Put !SEC_LOAD sections after SEC_LOAD ones. */ 5752 1.14 christos 5753 1.1 christos #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \ 5754 1.1 christos && (x)->size != 0) 5755 1.1 christos 5756 1.12 christos if (TOEND (sec1)) 5757 1.1 christos { 5758 1.1 christos if (!TOEND (sec2)) 5759 1.1 christos return 1; 5760 1.1 christos } 5761 1.1 christos else if (TOEND (sec2)) 5762 1.1 christos return -1; 5763 1.1 christos 5764 1.1 christos #undef TOEND 5765 1.1 christos 5766 1.1 christos /* Sort by size, to put zero sized sections 5767 1.1 christos before others at the same address. */ 5768 1.1 christos 5769 1.1 christos size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; 5770 1.1 christos size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; 5771 1.1 christos 5772 1.1 christos if (size1 < size2) 5773 1.1 christos return -1; 5774 1.1 christos if (size1 > size2) 5775 1.1 christos return 1; 5776 1.1 christos 5777 1.1 christos return sec1->target_index - sec2->target_index; 5778 1.12 christos } 5779 1.12 christos 5780 1.12 christos /* This qsort comparison functions sorts PT_LOAD segments first and 5781 1.12 christos by p_paddr, for assign_file_positions_for_load_sections. */ 5782 1.12 christos 5783 1.12 christos static int 5784 1.12 christos elf_sort_segments (const void *arg1, const void *arg2) 5785 1.12 christos { 5786 1.12 christos const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1; 5787 1.12 christos const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2; 5788 1.12 christos 5789 1.12 christos if (m1->p_type != m2->p_type) 5790 1.12 christos { 5791 1.12 christos if (m1->p_type == PT_NULL) 5792 1.12 christos return 1; 5793 1.12 christos if (m2->p_type == PT_NULL) 5794 1.12 christos return -1; 5795 1.12 christos return m1->p_type < m2->p_type ? -1 : 1; 5796 1.12 christos } 5797 1.12 christos if (m1->includes_filehdr != m2->includes_filehdr) 5798 1.12 christos return m1->includes_filehdr ? -1 : 1; 5799 1.12 christos if (m1->no_sort_lma != m2->no_sort_lma) 5800 1.12 christos return m1->no_sort_lma ? -1 : 1; 5801 1.12 christos if (m1->p_type == PT_LOAD && !m1->no_sort_lma) 5802 1.12 christos { 5803 1.12 christos bfd_vma lma1, lma2; /* Octets. */ 5804 1.12 christos lma1 = 0; 5805 1.12 christos if (m1->p_paddr_valid) 5806 1.12 christos lma1 = m1->p_paddr; 5807 1.12 christos else if (m1->count != 0) 5808 1.12 christos { 5809 1.12 christos unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner, 5810 1.12 christos m1->sections[0]); 5811 1.12 christos lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb; 5812 1.12 christos } 5813 1.12 christos lma2 = 0; 5814 1.12 christos if (m2->p_paddr_valid) 5815 1.12 christos lma2 = m2->p_paddr; 5816 1.12 christos else if (m2->count != 0) 5817 1.12 christos { 5818 1.12 christos unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner, 5819 1.12 christos m2->sections[0]); 5820 1.12 christos lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb; 5821 1.12 christos } 5822 1.12 christos if (lma1 != lma2) 5823 1.12 christos return lma1 < lma2 ? -1 : 1; 5824 1.12 christos } 5825 1.12 christos if (m1->idx != m2->idx) 5826 1.12 christos return m1->idx < m2->idx ? -1 : 1; 5827 1.12 christos return 0; 5828 1.1 christos } 5829 1.1 christos 5830 1.1 christos /* Ian Lance Taylor writes: 5831 1.1 christos 5832 1.1 christos We shouldn't be using % with a negative signed number. That's just 5833 1.1 christos not good. We have to make sure either that the number is not 5834 1.1 christos negative, or that the number has an unsigned type. When the types 5835 1.1 christos are all the same size they wind up as unsigned. When file_ptr is a 5836 1.1 christos larger signed type, the arithmetic winds up as signed long long, 5837 1.1 christos which is wrong. 5838 1.1 christos 5839 1.1 christos What we're trying to say here is something like ``increase OFF by 5840 1.1 christos the least amount that will cause it to be equal to the VMA modulo 5841 1.1 christos the page size.'' */ 5842 1.1 christos /* In other words, something like: 5843 1.1 christos 5844 1.1 christos vma_offset = m->sections[0]->vma % bed->maxpagesize; 5845 1.1 christos off_offset = off % bed->maxpagesize; 5846 1.1 christos if (vma_offset < off_offset) 5847 1.1 christos adjustment = vma_offset + bed->maxpagesize - off_offset; 5848 1.1 christos else 5849 1.11 christos adjustment = vma_offset - off_offset; 5850 1.1 christos 5851 1.1 christos which can be collapsed into the expression below. */ 5852 1.1 christos 5853 1.1 christos static file_ptr 5854 1.4 christos vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) 5855 1.4 christos { 5856 1.4 christos /* PR binutils/16199: Handle an alignment of zero. */ 5857 1.1 christos if (maxpagesize == 0) 5858 1.1 christos maxpagesize = 1; 5859 1.1 christos return ((vma - off) % maxpagesize); 5860 1.1 christos } 5861 1.1 christos 5862 1.1 christos static void 5863 1.1 christos print_segment_map (const struct elf_segment_map *m) 5864 1.1 christos { 5865 1.1 christos unsigned int j; 5866 1.1 christos const char *pt = get_segment_type (m->p_type); 5867 1.1 christos char buf[32]; 5868 1.1 christos 5869 1.1 christos if (pt == NULL) 5870 1.1 christos { 5871 1.1 christos if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) 5872 1.1 christos sprintf (buf, "LOPROC+%7.7x", 5873 1.1 christos (unsigned int) (m->p_type - PT_LOPROC)); 5874 1.1 christos else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) 5875 1.1 christos sprintf (buf, "LOOS+%7.7x", 5876 1.1 christos (unsigned int) (m->p_type - PT_LOOS)); 5877 1.1 christos else 5878 1.1 christos snprintf (buf, sizeof (buf), "%8.8x", 5879 1.1 christos (unsigned int) m->p_type); 5880 1.1 christos pt = buf; 5881 1.1 christos } 5882 1.1 christos fflush (stdout); 5883 1.1 christos fprintf (stderr, "%s:", pt); 5884 1.1 christos for (j = 0; j < m->count; j++) 5885 1.1 christos fprintf (stderr, " %s", m->sections [j]->name); 5886 1.1 christos putc ('\n',stderr); 5887 1.1 christos fflush (stderr); 5888 1.1 christos } 5889 1.1 christos 5890 1.1 christos /* Assign file positions to the sections based on the mapping from 5891 1.1 christos sections to segments. This function also sets up some fields in 5892 1.14 christos the file header. */ 5893 1.1 christos 5894 1.1 christos static bool 5895 1.1 christos assign_file_positions_for_load_sections (bfd *abfd, 5896 1.1 christos struct bfd_link_info *link_info) 5897 1.1 christos { 5898 1.12 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 5899 1.1 christos struct elf_segment_map *m; 5900 1.1 christos struct elf_segment_map *phdr_load_seg; 5901 1.12 christos Elf_Internal_Phdr *phdrs; 5902 1.1 christos Elf_Internal_Phdr *p; 5903 1.12 christos file_ptr off; /* Octets. */ 5904 1.1 christos bfd_size_type maxpagesize; 5905 1.12 christos unsigned int alloc, actual; 5906 1.12 christos unsigned int i, j; 5907 1.1 christos struct elf_segment_map **sorted_seg_map; 5908 1.1 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL); 5909 1.14 christos 5910 1.14 christos if (link_info == NULL 5911 1.1 christos && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL)) 5912 1.1 christos return false; 5913 1.3 christos 5914 1.12 christos alloc = 0; 5915 1.1 christos for (m = elf_seg_map (abfd); m != NULL; m = m->next) 5916 1.1 christos m->idx = alloc++; 5917 1.1 christos 5918 1.1 christos if (alloc) 5919 1.1 christos { 5920 1.1 christos elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; 5921 1.1 christos elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; 5922 1.1 christos } 5923 1.1 christos else 5924 1.1 christos { 5925 1.1 christos /* PR binutils/12467. */ 5926 1.1 christos elf_elfheader (abfd)->e_phoff = 0; 5927 1.3 christos elf_elfheader (abfd)->e_phentsize = 0; 5928 1.1 christos } 5929 1.1 christos 5930 1.3 christos elf_elfheader (abfd)->e_phnum = alloc; 5931 1.12 christos 5932 1.12 christos if (elf_program_header_size (abfd) == (bfd_size_type) -1) 5933 1.12 christos { 5934 1.12 christos actual = alloc; 5935 1.1 christos elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; 5936 1.12 christos } 5937 1.12 christos else 5938 1.12 christos { 5939 1.12 christos actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr; 5940 1.12 christos BFD_ASSERT (elf_program_header_size (abfd) 5941 1.12 christos == actual * bed->s->sizeof_phdr); 5942 1.1 christos BFD_ASSERT (actual >= alloc); 5943 1.1 christos } 5944 1.1 christos 5945 1.3 christos if (alloc == 0) 5946 1.14 christos { 5947 1.1 christos elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; 5948 1.1 christos return true; 5949 1.3 christos } 5950 1.1 christos 5951 1.1 christos /* We're writing the size in elf_program_header_size (abfd), 5952 1.3 christos see assign_file_positions_except_relocs, so make sure we have 5953 1.3 christos that amount allocated, with trailing space cleared. 5954 1.1 christos The variable alloc contains the computed need, while 5955 1.1 christos elf_program_header_size (abfd) contains the size used for the 5956 1.1 christos layout. 5957 1.1 christos See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments 5958 1.12 christos where the layout is forced to according to a larger size in the 5959 1.12 christos last iterations for the testcase ld-elf/header. */ 5960 1.12 christos phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs) 5961 1.1 christos + alloc * sizeof (*sorted_seg_map))); 5962 1.1 christos sorted_seg_map = (struct elf_segment_map **) (phdrs + actual); 5963 1.14 christos elf_tdata (abfd)->phdr = phdrs; 5964 1.1 christos if (phdrs == NULL) 5965 1.12 christos return false; 5966 1.12 christos 5967 1.12 christos for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++) 5968 1.12 christos { 5969 1.12 christos sorted_seg_map[j] = m; 5970 1.12 christos /* If elf_segment_map is not from map_sections_to_segments, the 5971 1.12 christos sections may not be correctly ordered. NOTE: sorting should 5972 1.12 christos not be done to the PT_NOTE section of a corefile, which may 5973 1.12 christos contain several pseudo-sections artificially created by bfd. 5974 1.12 christos Sorting these pseudo-sections breaks things badly. */ 5975 1.12 christos if (m->count > 1 5976 1.12 christos && !(elf_elfheader (abfd)->e_type == ET_CORE 5977 1.12 christos && m->p_type == PT_NOTE)) 5978 1.12 christos { 5979 1.12 christos for (i = 0; i < m->count; i++) 5980 1.12 christos m->sections[i]->target_index = i; 5981 1.12 christos qsort (m->sections, (size_t) m->count, sizeof (asection *), 5982 1.12 christos elf_sort_sections); 5983 1.12 christos } 5984 1.12 christos } 5985 1.12 christos if (alloc > 1) 5986 1.12 christos qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map), 5987 1.1 christos elf_sort_segments); 5988 1.1 christos 5989 1.14 christos maxpagesize = 1; 5990 1.14 christos if ((abfd->flags & D_PAGED) != 0) 5991 1.14 christos { 5992 1.14 christos if (link_info != NULL) 5993 1.14 christos maxpagesize = link_info->maxpagesize; 5994 1.14 christos else 5995 1.1 christos maxpagesize = bed->maxpagesize; 5996 1.12 christos } 5997 1.1 christos 5998 1.12 christos /* Sections must map to file offsets past the ELF file header. */ 5999 1.12 christos off = bed->s->sizeof_ehdr; 6000 1.12 christos /* And if one of the PT_LOAD headers doesn't include the program 6001 1.12 christos headers then we'll be mapping program headers in the usual 6002 1.12 christos position after the ELF file header. */ 6003 1.12 christos phdr_load_seg = NULL; 6004 1.12 christos for (j = 0; j < alloc; j++) 6005 1.12 christos { 6006 1.12 christos m = sorted_seg_map[j]; 6007 1.12 christos if (m->p_type != PT_LOAD) 6008 1.12 christos break; 6009 1.12 christos if (m->includes_phdrs) 6010 1.12 christos { 6011 1.12 christos phdr_load_seg = m; 6012 1.12 christos break; 6013 1.12 christos } 6014 1.12 christos } 6015 1.1 christos if (phdr_load_seg == NULL) 6016 1.12 christos off += actual * bed->s->sizeof_phdr; 6017 1.1 christos 6018 1.1 christos for (j = 0; j < alloc; j++) 6019 1.12 christos { 6020 1.14 christos asection **secpp; 6021 1.19 christos bfd_vma off_adjust; /* Octets. */ 6022 1.1 christos bool no_contents; 6023 1.1 christos bfd_size_type align_pagesize; 6024 1.1 christos 6025 1.1 christos /* An ELF segment (described by Elf_Internal_Phdr) may contain a 6026 1.1 christos number of sections with contents contributing to both p_filesz 6027 1.1 christos and p_memsz, followed by a number of sections with no contents 6028 1.12 christos that just contribute to p_memsz. In this loop, OFF tracks next 6029 1.12 christos available file offset for PT_LOAD and PT_NOTE segments. */ 6030 1.1 christos m = sorted_seg_map[j]; 6031 1.1 christos p = phdrs + m->idx; 6032 1.1 christos p->p_type = m->p_type; 6033 1.1 christos p->p_flags = m->p_flags; 6034 1.12 christos 6035 1.1 christos if (m->count == 0) 6036 1.12 christos p->p_vaddr = m->p_vaddr_offset * opb; 6037 1.1 christos else 6038 1.1 christos p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb; 6039 1.1 christos 6040 1.1 christos if (m->p_paddr_valid) 6041 1.1 christos p->p_paddr = m->p_paddr; 6042 1.1 christos else if (m->count == 0) 6043 1.12 christos p->p_paddr = 0; 6044 1.1 christos else 6045 1.19 christos p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb; 6046 1.1 christos 6047 1.1 christos align_pagesize = 0; 6048 1.1 christos if (p->p_type == PT_LOAD 6049 1.1 christos && (abfd->flags & D_PAGED) != 0) 6050 1.1 christos { 6051 1.1 christos /* p_align in demand paged PT_LOAD segments effectively stores 6052 1.1 christos the maximum page size. When copying an executable with 6053 1.1 christos objcopy, we set m->p_align from the input file. Use this 6054 1.1 christos value for maxpagesize rather than bed->maxpagesize, which 6055 1.1 christos may be different. Note that we use maxpagesize for PT_TLS 6056 1.1 christos segment alignment later in this function, so we are relying 6057 1.1 christos on at least one PT_LOAD segment appearing before a PT_TLS 6058 1.1 christos segment. */ 6059 1.19 christos if (m->p_align_valid) 6060 1.14 christos maxpagesize = m->p_align; 6061 1.14 christos else if (bed->p_align != 0 6062 1.19 christos && (link_info == NULL 6063 1.19 christos || !link_info->maxpagesize_is_set)) 6064 1.19 christos /* We will lay out this binary using maxpagesize but set 6065 1.19 christos p->p_align later to the possibly smaller bed->p_align. 6066 1.19 christos The run-time loader will then be able to load this 6067 1.19 christos binary when the system page size is maxpagesize, but if 6068 1.19 christos the system page size is smaller can use p->p_align. 6069 1.19 christos In either case p->p_align will be increased if 6070 1.1 christos necessary to match section alignment. */ 6071 1.1 christos align_pagesize = bed->p_align; 6072 1.1 christos 6073 1.1 christos p->p_align = maxpagesize; 6074 1.1 christos } 6075 1.1 christos else if (m->p_align_valid) 6076 1.1 christos p->p_align = m->p_align; 6077 1.12 christos else if (m->count == 0) 6078 1.12 christos p->p_align = 1 << bed->s->log_file_align; 6079 1.18 christos 6080 1.1 christos if (m == phdr_load_seg) 6081 1.14 christos off += actual * bed->s->sizeof_phdr; 6082 1.1 christos 6083 1.1 christos no_contents = false; 6084 1.1 christos off_adjust = 0; 6085 1.1 christos if (p->p_type == PT_LOAD 6086 1.12 christos && m->count > 0) 6087 1.1 christos { 6088 1.1 christos bfd_size_type align; /* Bytes. */ 6089 1.1 christos unsigned int align_power = 0; 6090 1.1 christos 6091 1.1 christos if (m->p_align_valid) 6092 1.1 christos align = p->p_align; 6093 1.1 christos else 6094 1.1 christos { 6095 1.1 christos for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) 6096 1.1 christos { 6097 1.12 christos unsigned int secalign; 6098 1.1 christos 6099 1.1 christos secalign = bfd_section_alignment (*secpp); 6100 1.1 christos if (secalign > align_power) 6101 1.1 christos align_power = secalign; 6102 1.19 christos } 6103 1.19 christos align = (bfd_size_type) 1 << align_power; 6104 1.19 christos /* If a section requires alignment higher than the 6105 1.19 christos minimum p_align value, don't reduce a maxpagesize 6106 1.19 christos p->p_align set earlier in this function. */ 6107 1.1 christos if (align > bed->p_align) 6108 1.19 christos align_pagesize = 0; 6109 1.14 christos if (align < maxpagesize) 6110 1.14 christos align = maxpagesize; 6111 1.14 christos else 6112 1.14 christos { 6113 1.14 christos /* If a section requires alignment higher than the 6114 1.19 christos maximum page size, set p_align to the section 6115 1.19 christos alignment. */ 6116 1.14 christos if ((abfd->flags & D_PAGED) != 0) 6117 1.1 christos p->p_align = align; 6118 1.1 christos } 6119 1.1 christos } 6120 1.1 christos 6121 1.1 christos for (i = 0; i < m->count; i++) 6122 1.1 christos if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) 6123 1.1 christos /* If we aren't making room for this section, then 6124 1.1 christos it must be SHT_NOBITS regardless of what we've 6125 1.1 christos set via struct bfd_elf_special_section. */ 6126 1.1 christos elf_section_type (m->sections[i]) = SHT_NOBITS; 6127 1.1 christos 6128 1.14 christos /* Find out whether this segment contains any loadable 6129 1.1 christos sections. */ 6130 1.1 christos no_contents = true; 6131 1.1 christos for (i = 0; i < m->count; i++) 6132 1.14 christos if (elf_section_type (m->sections[i]) != SHT_NOBITS) 6133 1.1 christos { 6134 1.1 christos no_contents = false; 6135 1.1 christos break; 6136 1.12 christos } 6137 1.9 christos 6138 1.9 christos off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb); 6139 1.9 christos 6140 1.9 christos /* Broken hardware and/or kernel require that files do not 6141 1.12 christos map the same page with different permissions on some hppa 6142 1.12 christos processors. */ 6143 1.9 christos if (j != 0 6144 1.9 christos && (abfd->flags & D_PAGED) != 0 6145 1.12 christos && bed->no_page_alias 6146 1.12 christos && (off & (maxpagesize - 1)) != 0 6147 1.9 christos && ((off & -maxpagesize) 6148 1.1 christos == ((off + off_adjust) & -maxpagesize))) 6149 1.1 christos off_adjust += maxpagesize; 6150 1.1 christos off += off_adjust; 6151 1.1 christos if (no_contents) 6152 1.1 christos { 6153 1.1 christos /* We shouldn't need to align the segment on disk since 6154 1.1 christos the segment doesn't need file space, but the gABI 6155 1.1 christos arguably requires the alignment and glibc ld.so 6156 1.1 christos checks it. So to comply with the alignment 6157 1.1 christos requirement but not waste file space, we adjust 6158 1.1 christos p_offset for just this segment. (OFF_ADJUST is 6159 1.1 christos subtracted from OFF later.) This may put p_offset 6160 1.1 christos past the end of file, but that shouldn't matter. */ 6161 1.1 christos } 6162 1.1 christos else 6163 1.1 christos off_adjust = 0; 6164 1.1 christos } 6165 1.1 christos /* Make sure the .dynamic section is the first section in the 6166 1.1 christos PT_DYNAMIC segment. */ 6167 1.19 christos else if (p->p_type == PT_DYNAMIC 6168 1.1 christos && m->count > 1 6169 1.1 christos && ! streq (m->sections[0]->name, ".dynamic")) 6170 1.11 christos { 6171 1.9 christos _bfd_error_handler 6172 1.1 christos (_("%pB: The first section in the PT_DYNAMIC segment" 6173 1.1 christos " is not the .dynamic section"), 6174 1.14 christos abfd); 6175 1.1 christos bfd_set_error (bfd_error_bad_value); 6176 1.1 christos return false; 6177 1.1 christos } 6178 1.1 christos /* Set the note section type to SHT_NOTE. */ 6179 1.1 christos else if (p->p_type == PT_NOTE) 6180 1.1 christos for (i = 0; i < m->count; i++) 6181 1.1 christos elf_section_type (m->sections[i]) = SHT_NOTE; 6182 1.1 christos 6183 1.1 christos if (m->includes_filehdr) 6184 1.1 christos { 6185 1.1 christos if (!m->p_flags_valid) 6186 1.1 christos p->p_flags |= PF_R; 6187 1.12 christos p->p_filesz = bed->s->sizeof_ehdr; 6188 1.1 christos p->p_memsz = bed->s->sizeof_ehdr; 6189 1.12 christos if (p->p_type == PT_LOAD) 6190 1.1 christos { 6191 1.12 christos if (m->count > 0) 6192 1.12 christos { 6193 1.12 christos if (p->p_vaddr < (bfd_vma) off 6194 1.12 christos || (!m->p_paddr_valid 6195 1.12 christos && p->p_paddr < (bfd_vma) off)) 6196 1.12 christos { 6197 1.12 christos _bfd_error_handler 6198 1.12 christos (_("%pB: not enough room for program headers," 6199 1.12 christos " try linking with -N"), 6200 1.14 christos abfd); 6201 1.12 christos bfd_set_error (bfd_error_bad_value); 6202 1.12 christos return false; 6203 1.12 christos } 6204 1.12 christos p->p_vaddr -= off; 6205 1.1 christos if (!m->p_paddr_valid) 6206 1.12 christos p->p_paddr -= off; 6207 1.12 christos } 6208 1.12 christos } 6209 1.12 christos else if (sorted_seg_map[0]->includes_filehdr) 6210 1.12 christos { 6211 1.1 christos Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx; 6212 1.12 christos p->p_vaddr = filehdr->p_vaddr; 6213 1.1 christos if (!m->p_paddr_valid) 6214 1.1 christos p->p_paddr = filehdr->p_paddr; 6215 1.1 christos } 6216 1.1 christos } 6217 1.1 christos 6218 1.1 christos if (m->includes_phdrs) 6219 1.1 christos { 6220 1.12 christos if (!m->p_flags_valid) 6221 1.12 christos p->p_flags |= PF_R; 6222 1.1 christos p->p_filesz += actual * bed->s->sizeof_phdr; 6223 1.1 christos p->p_memsz += actual * bed->s->sizeof_phdr; 6224 1.12 christos if (!m->includes_filehdr) 6225 1.12 christos { 6226 1.18 christos if (p->p_type == PT_LOAD) 6227 1.12 christos { 6228 1.12 christos p->p_offset = off - actual * bed->s->sizeof_phdr; 6229 1.12 christos elf_elfheader (abfd)->e_phoff = p->p_offset; 6230 1.12 christos if (m->count > 0) 6231 1.12 christos { 6232 1.12 christos p->p_vaddr -= off - p->p_offset; 6233 1.12 christos if (!m->p_paddr_valid) 6234 1.12 christos p->p_paddr -= off - p->p_offset; 6235 1.12 christos } 6236 1.1 christos } 6237 1.18 christos else if (phdr_load_seg != NULL) 6238 1.18 christos { 6239 1.18 christos /* Also set PT_PHDR to match phdr_load_seg. We've 6240 1.12 christos sorted segments so that phdr_load_seg will 6241 1.12 christos already be set by the code immediately above. */ 6242 1.12 christos Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx; 6243 1.12 christos bfd_vma phdr_off = 0; /* Octets. */ 6244 1.12 christos if (phdr_load_seg->includes_filehdr) 6245 1.1 christos phdr_off = bed->s->sizeof_ehdr; 6246 1.12 christos p->p_vaddr = phdr->p_vaddr + phdr_off; 6247 1.12 christos if (!m->p_paddr_valid) 6248 1.1 christos p->p_paddr = phdr->p_paddr + phdr_off; 6249 1.12 christos p->p_offset = phdr->p_offset + phdr_off; 6250 1.12 christos } 6251 1.1 christos else 6252 1.1 christos p->p_offset = bed->s->sizeof_ehdr; 6253 1.1 christos } 6254 1.1 christos } 6255 1.1 christos 6256 1.1 christos if (p->p_type == PT_LOAD 6257 1.1 christos || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) 6258 1.12 christos { 6259 1.12 christos if (!m->includes_filehdr && !m->includes_phdrs) 6260 1.12 christos { 6261 1.12 christos p->p_offset = off; 6262 1.12 christos if (no_contents) 6263 1.12 christos { 6264 1.12 christos /* Put meaningless p_offset for PT_LOAD segments 6265 1.12 christos without file contents somewhere within the first 6266 1.12 christos page, in an attempt to not point past EOF. */ 6267 1.12 christos bfd_size_type align = maxpagesize; 6268 1.12 christos if (align < p->p_align) 6269 1.12 christos align = p->p_align; 6270 1.19 christos if (align < 1) 6271 1.19 christos align = 1; 6272 1.19 christos /* Avoid p_offset of zero, which might be wrongly 6273 1.19 christos interpreted as the segment being the first one, 6274 1.12 christos containing the file header. PR32763. */ 6275 1.12 christos p->p_offset = (off + align - 1) % align + 1; 6276 1.1 christos } 6277 1.1 christos } 6278 1.12 christos else 6279 1.1 christos { 6280 1.1 christos file_ptr adjust; /* Octets. */ 6281 1.1 christos 6282 1.1 christos adjust = off - (p->p_offset + p->p_filesz); 6283 1.1 christos if (!no_contents) 6284 1.1 christos p->p_filesz += adjust; 6285 1.1 christos p->p_memsz += adjust; 6286 1.1 christos } 6287 1.19 christos } 6288 1.19 christos 6289 1.19 christos if (align_pagesize) 6290 1.1 christos p->p_align = align_pagesize; 6291 1.1 christos 6292 1.1 christos /* Set up p_filesz, p_memsz, p_align and p_flags from the section 6293 1.1 christos maps. Set filepos for sections in PT_LOAD segments, and in 6294 1.1 christos core files, for sections in PT_NOTE segments. 6295 1.1 christos assign_file_positions_for_non_load_sections will set filepos 6296 1.1 christos for other sections and update p_filesz for other segments. */ 6297 1.1 christos for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) 6298 1.1 christos { 6299 1.1 christos asection *sec; 6300 1.1 christos bfd_size_type align; 6301 1.1 christos Elf_Internal_Shdr *this_hdr; 6302 1.1 christos 6303 1.12 christos sec = *secpp; 6304 1.1 christos this_hdr = &elf_section_data (sec)->this_hdr; 6305 1.1 christos align = (bfd_size_type) 1 << bfd_section_alignment (sec); 6306 1.1 christos 6307 1.1 christos if ((p->p_type == PT_LOAD 6308 1.1 christos || p->p_type == PT_TLS) 6309 1.1 christos && (this_hdr->sh_type != SHT_NOBITS 6310 1.1 christos || ((this_hdr->sh_flags & SHF_ALLOC) != 0 6311 1.1 christos && ((this_hdr->sh_flags & SHF_TLS) == 0 6312 1.14 christos || p->p_type == PT_TLS)))) 6313 1.14 christos { 6314 1.14 christos bfd_vma p_start = p->p_paddr; /* Octets. */ 6315 1.14 christos bfd_vma p_end = p_start + p->p_memsz; /* Octets. */ 6316 1.1 christos bfd_vma s_start = sec->lma * opb; /* Octets. */ 6317 1.1 christos bfd_vma adjust = s_start - p_end; /* Octets. */ 6318 1.1 christos 6319 1.1 christos if (adjust != 0 6320 1.1 christos && (s_start < p_end 6321 1.9 christos || p_end < p_start)) 6322 1.9 christos { 6323 1.14 christos _bfd_error_handler 6324 1.14 christos /* xgettext:c-format */ 6325 1.12 christos (_("%pB: section %pA lma %#" PRIx64 6326 1.12 christos " adjusted to %#" PRIx64), 6327 1.1 christos abfd, sec, (uint64_t) s_start / opb, 6328 1.12 christos (uint64_t) p_end / opb); 6329 1.1 christos adjust = 0; 6330 1.1 christos sec->lma = p_end / opb; 6331 1.1 christos } 6332 1.12 christos p->p_memsz += adjust; 6333 1.1 christos 6334 1.12 christos if (p->p_type == PT_LOAD) 6335 1.12 christos { 6336 1.12 christos if (this_hdr->sh_type != SHT_NOBITS) 6337 1.12 christos { 6338 1.12 christos off_adjust = 0; 6339 1.12 christos if (p->p_filesz + adjust < p->p_memsz) 6340 1.17 christos { 6341 1.17 christos /* We have a PROGBITS section following NOBITS ones. 6342 1.17 christos Allocate file space for the NOBITS section(s). 6343 1.17 christos We don't need to write out the zeros, posix 6344 1.17 christos fseek past the end of data already written 6345 1.17 christos followed by a write at that location is 6346 1.12 christos guaranteed to result in zeros being read 6347 1.12 christos from the gap. */ 6348 1.12 christos adjust = p->p_memsz - p->p_filesz; 6349 1.12 christos } 6350 1.12 christos } 6351 1.12 christos /* We only adjust sh_offset in SHT_NOBITS sections 6352 1.12 christos as would seem proper for their address when the 6353 1.12 christos section is first in the segment. sh_offset 6354 1.12 christos doesn't really have any significance for 6355 1.12 christos SHT_NOBITS anyway, apart from a notional position 6356 1.12 christos relative to other sections. Historically we 6357 1.12 christos didn't bother with adjusting sh_offset and some 6358 1.12 christos programs depend on it not being adjusted. See 6359 1.1 christos pr12921 and pr25662. */ 6360 1.12 christos if (this_hdr->sh_type != SHT_NOBITS || i == 0) 6361 1.12 christos { 6362 1.12 christos off += adjust; 6363 1.1 christos if (this_hdr->sh_type == SHT_NOBITS) 6364 1.1 christos off_adjust += adjust; 6365 1.12 christos } 6366 1.12 christos } 6367 1.1 christos if (this_hdr->sh_type != SHT_NOBITS) 6368 1.1 christos p->p_filesz += adjust; 6369 1.1 christos } 6370 1.1 christos 6371 1.1 christos if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) 6372 1.1 christos { 6373 1.1 christos /* The section at i == 0 is the one that actually contains 6374 1.1 christos everything. */ 6375 1.1 christos if (i == 0) 6376 1.1 christos { 6377 1.1 christos this_hdr->sh_offset = sec->filepos = off; 6378 1.1 christos off += this_hdr->sh_size; 6379 1.1 christos p->p_filesz = this_hdr->sh_size; 6380 1.1 christos p->p_memsz = 0; 6381 1.1 christos p->p_align = 1; 6382 1.1 christos } 6383 1.1 christos else 6384 1.1 christos { 6385 1.1 christos /* The rest are fake sections that shouldn't be written. */ 6386 1.1 christos sec->filepos = 0; 6387 1.1 christos sec->size = 0; 6388 1.1 christos sec->flags = 0; 6389 1.1 christos continue; 6390 1.1 christos } 6391 1.1 christos } 6392 1.19 christos else 6393 1.19 christos { 6394 1.19 christos if (this_hdr->sh_type == SHT_NOBITS 6395 1.19 christos && (this_hdr->sh_flags & SHF_TLS) != 0 6396 1.19 christos && this_hdr->sh_offset == 0) 6397 1.19 christos { 6398 1.19 christos /* Set sh_offset for .tbss sections to their nominal 6399 1.19 christos offset after aligning. They are not loaded from 6400 1.19 christos disk so the value doesn't really matter, except 6401 1.19 christos when the .tbss section is the first one in a 6402 1.19 christos PT_TLS segment. In that case it sets the 6403 1.19 christos p_offset for the PT_TLS segment, which according 6404 1.19 christos to the ELF gABI ought to satisfy 6405 1.19 christos p_offset % p_align == p_vaddr % p_align. */ 6406 1.19 christos bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, 6407 1.19 christos off, align); 6408 1.19 christos this_hdr->sh_offset = sec->filepos = off + adjust; 6409 1.1 christos } 6410 1.1 christos else if (p->p_type == PT_LOAD) 6411 1.1 christos { 6412 1.1 christos this_hdr->sh_offset = sec->filepos = off; 6413 1.1 christos if (this_hdr->sh_type != SHT_NOBITS) 6414 1.1 christos off += this_hdr->sh_size; 6415 1.1 christos } 6416 1.1 christos 6417 1.1 christos if (this_hdr->sh_type != SHT_NOBITS) 6418 1.1 christos { 6419 1.1 christos p->p_filesz += this_hdr->sh_size; 6420 1.1 christos /* A load section without SHF_ALLOC is something like 6421 1.1 christos a note section in a PT_NOTE segment. These take 6422 1.1 christos file space but are not loaded into memory. */ 6423 1.1 christos if ((this_hdr->sh_flags & SHF_ALLOC) != 0) 6424 1.1 christos p->p_memsz += this_hdr->sh_size; 6425 1.1 christos } 6426 1.1 christos else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) 6427 1.1 christos { 6428 1.1 christos if (p->p_type == PT_TLS) 6429 1.1 christos p->p_memsz += this_hdr->sh_size; 6430 1.1 christos 6431 1.1 christos /* .tbss is special. It doesn't contribute to p_memsz of 6432 1.1 christos normal segments. */ 6433 1.1 christos else if ((this_hdr->sh_flags & SHF_TLS) == 0) 6434 1.1 christos p->p_memsz += this_hdr->sh_size; 6435 1.1 christos } 6436 1.1 christos 6437 1.1 christos if (align > p->p_align 6438 1.1 christos && !m->p_align_valid 6439 1.1 christos && (p->p_type != PT_LOAD 6440 1.1 christos || (abfd->flags & D_PAGED) == 0)) 6441 1.1 christos p->p_align = align; 6442 1.1 christos } 6443 1.1 christos 6444 1.1 christos if (!m->p_flags_valid) 6445 1.1 christos { 6446 1.1 christos p->p_flags |= PF_R; 6447 1.1 christos if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) 6448 1.1 christos p->p_flags |= PF_X; 6449 1.1 christos if ((this_hdr->sh_flags & SHF_WRITE) != 0) 6450 1.1 christos p->p_flags |= PF_W; 6451 1.5 christos } 6452 1.1 christos } 6453 1.1 christos 6454 1.12 christos off -= off_adjust; 6455 1.12 christos 6456 1.12 christos /* PR ld/20815 - Check that the program header segment, if 6457 1.12 christos present, will be loaded into memory. */ 6458 1.12 christos if (p->p_type == PT_PHDR 6459 1.12 christos && phdr_load_seg == NULL 6460 1.12 christos && !(bed->elf_backend_allow_non_load_phdr != NULL 6461 1.12 christos && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc))) 6462 1.12 christos { 6463 1.12 christos /* The fix for this error is usually to edit the linker script being 6464 1.12 christos used and set up the program headers manually. Either that or 6465 1.12 christos leave room for the headers at the start of the SECTIONS. */ 6466 1.12 christos _bfd_error_handler (_("%pB: error: PHDR segment not covered" 6467 1.12 christos " by LOAD segment"), 6468 1.14 christos abfd); 6469 1.12 christos if (link_info == NULL) 6470 1.12 christos return false; 6471 1.12 christos /* Arrange for the linker to exit with an error, deleting 6472 1.12 christos the output file unless --noinhibit-exec is given. */ 6473 1.12 christos link_info->callbacks->info ("%X"); 6474 1.1 christos } 6475 1.1 christos 6476 1.1 christos /* Check that all sections are in a PT_LOAD segment. 6477 1.1 christos Don't check funky gdb generated core files. */ 6478 1.14 christos if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) 6479 1.1 christos { 6480 1.1 christos bool check_vma = true; 6481 1.1 christos 6482 1.1 christos for (i = 1; i < m->count; i++) 6483 1.1 christos if (m->sections[i]->vma == m->sections[i - 1]->vma 6484 1.1 christos && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) 6485 1.1 christos ->this_hdr), p) != 0 6486 1.1 christos && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) 6487 1.1 christos ->this_hdr), p) != 0) 6488 1.14 christos { 6489 1.1 christos /* Looks like we have overlays packed into the segment. */ 6490 1.1 christos check_vma = false; 6491 1.1 christos break; 6492 1.1 christos } 6493 1.1 christos 6494 1.1 christos for (i = 0; i < m->count; i++) 6495 1.1 christos { 6496 1.1 christos Elf_Internal_Shdr *this_hdr; 6497 1.1 christos asection *sec; 6498 1.1 christos 6499 1.3 christos sec = m->sections[i]; 6500 1.3 christos this_hdr = &(elf_section_data(sec)->this_hdr); 6501 1.1 christos if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) 6502 1.9 christos && !ELF_TBSS_SPECIAL (this_hdr, p)) 6503 1.9 christos { 6504 1.18 christos _bfd_error_handler 6505 1.18 christos /* xgettext:c-format */ 6506 1.1 christos (_("%pB: section `%pA' can't be allocated in segment %u"), 6507 1.1 christos abfd, sec, m->idx); 6508 1.1 christos print_segment_map (m); 6509 1.1 christos } 6510 1.1 christos } 6511 1.1 christos } 6512 1.3 christos } 6513 1.1 christos 6514 1.12 christos elf_next_file_pos (abfd) = off; 6515 1.12 christos 6516 1.12 christos if (link_info != NULL 6517 1.1 christos && phdr_load_seg != NULL 6518 1.12 christos && phdr_load_seg->includes_filehdr) 6519 1.12 christos { 6520 1.12 christos /* There is a segment that contains both the file headers and the 6521 1.12 christos program headers, so provide a symbol __ehdr_start pointing there. 6522 1.19 christos A program can use this to examine itself robustly. */ 6523 1.19 christos 6524 1.19 christos struct elf_link_hash_table *htab = elf_hash_table (link_info); 6525 1.12 christos struct elf_link_hash_entry *hash = htab->hehdr_start; 6526 1.12 christos 6527 1.12 christos /* If the symbol was referenced and not defined, define it. */ 6528 1.12 christos if (hash != NULL 6529 1.12 christos && (hash->root.type == bfd_link_hash_new 6530 1.12 christos || hash->root.type == bfd_link_hash_undefined 6531 1.12 christos || hash->root.type == bfd_link_hash_undefweak 6532 1.12 christos || hash->root.type == bfd_link_hash_common)) 6533 1.12 christos { 6534 1.12 christos asection *s = NULL; 6535 1.12 christos bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb; 6536 1.12 christos 6537 1.12 christos if (phdr_load_seg->count != 0) 6538 1.12 christos /* The segment contains sections, so use the first one. */ 6539 1.12 christos s = phdr_load_seg->sections[0]; 6540 1.12 christos else 6541 1.12 christos /* Use the first (i.e. lowest-addressed) section in any segment. */ 6542 1.12 christos for (m = elf_seg_map (abfd); m != NULL; m = m->next) 6543 1.12 christos if (m->p_type == PT_LOAD && m->count != 0) 6544 1.12 christos { 6545 1.12 christos s = m->sections[0]; 6546 1.12 christos break; 6547 1.12 christos } 6548 1.12 christos 6549 1.12 christos if (s != NULL) 6550 1.12 christos { 6551 1.12 christos hash->root.u.def.value = filehdr_vaddr - s->vma; 6552 1.12 christos hash->root.u.def.section = s; 6553 1.12 christos } 6554 1.12 christos else 6555 1.12 christos { 6556 1.12 christos hash->root.u.def.value = filehdr_vaddr; 6557 1.12 christos hash->root.u.def.section = bfd_abs_section_ptr; 6558 1.12 christos } 6559 1.12 christos 6560 1.12 christos hash->root.type = bfd_link_hash_defined; 6561 1.12 christos hash->def_regular = 1; 6562 1.12 christos hash->non_elf = 0; 6563 1.12 christos } 6564 1.14 christos } 6565 1.12 christos 6566 1.12 christos return true; 6567 1.12 christos } 6568 1.12 christos 6569 1.12 christos /* Determine if a bfd is a debuginfo file. Unfortunately there 6570 1.12 christos is no defined method for detecting such files, so we have to 6571 1.14 christos use heuristics instead. */ 6572 1.12 christos 6573 1.12 christos bool 6574 1.12 christos is_debuginfo_file (bfd *abfd) 6575 1.14 christos { 6576 1.12 christos if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour) 6577 1.12 christos return false; 6578 1.12 christos 6579 1.12 christos Elf_Internal_Shdr **start_headers = elf_elfsections (abfd); 6580 1.12 christos Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd); 6581 1.12 christos Elf_Internal_Shdr **headerp; 6582 1.12 christos 6583 1.12 christos for (headerp = start_headers; headerp < end_headers; headerp ++) 6584 1.12 christos { 6585 1.12 christos Elf_Internal_Shdr *header = * headerp; 6586 1.12 christos 6587 1.12 christos /* Debuginfo files do not have any allocated SHT_PROGBITS sections. 6588 1.12 christos The only allocated sections are SHT_NOBITS or SHT_NOTES. */ 6589 1.12 christos if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC 6590 1.14 christos && header->sh_type != SHT_NOBITS 6591 1.12 christos && header->sh_type != SHT_NOTE) 6592 1.12 christos return false; 6593 1.14 christos } 6594 1.12 christos 6595 1.12 christos return true; 6596 1.14 christos } 6597 1.14 christos 6598 1.12 christos /* Assign file positions for other sections, except for compressed debug 6599 1.14 christos and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */ 6600 1.12 christos 6601 1.12 christos static bool 6602 1.12 christos assign_file_positions_for_non_load_sections (bfd *abfd, 6603 1.12 christos struct bfd_link_info *link_info) 6604 1.12 christos { 6605 1.12 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 6606 1.12 christos Elf_Internal_Shdr **i_shdrpp; 6607 1.12 christos Elf_Internal_Shdr **hdrpp, **end_hdrpp; 6608 1.12 christos Elf_Internal_Phdr *phdrs; 6609 1.12 christos Elf_Internal_Phdr *p; 6610 1.12 christos struct elf_segment_map *m; 6611 1.14 christos file_ptr off; 6612 1.12 christos unsigned int opb = bfd_octets_per_byte (abfd, NULL); 6613 1.14 christos bfd_vma maxpagesize; 6614 1.14 christos 6615 1.14 christos if (link_info != NULL) 6616 1.14 christos maxpagesize = link_info->maxpagesize; 6617 1.12 christos else 6618 1.12 christos maxpagesize = bed->maxpagesize; 6619 1.12 christos i_shdrpp = elf_elfsections (abfd); 6620 1.12 christos end_hdrpp = i_shdrpp + elf_numsections (abfd); 6621 1.12 christos off = elf_next_file_pos (abfd); 6622 1.12 christos for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) 6623 1.14 christos { 6624 1.1 christos Elf_Internal_Shdr *hdr; 6625 1.1 christos bfd_vma align; 6626 1.1 christos 6627 1.1 christos hdr = *hdrpp; 6628 1.1 christos if (hdr->bfd_section != NULL 6629 1.1 christos && (hdr->bfd_section->filepos != 0 6630 1.1 christos || (hdr->sh_type == SHT_NOBITS 6631 1.1 christos && hdr->contents == NULL))) 6632 1.1 christos BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); 6633 1.12 christos else if ((hdr->sh_flags & SHF_ALLOC) != 0) 6634 1.12 christos { 6635 1.12 christos if (hdr->sh_size != 0 6636 1.12 christos /* PR 24717 - debuginfo files are known to be not strictly 6637 1.12 christos compliant with the ELF standard. In particular they often 6638 1.12 christos have .note.gnu.property sections that are outside of any 6639 1.12 christos loadable segment. This is not a problem for such files, 6640 1.9 christos so do not warn about them. */ 6641 1.9 christos && ! is_debuginfo_file (abfd)) 6642 1.11 christos _bfd_error_handler 6643 1.3 christos /* xgettext:c-format */ 6644 1.3 christos (_("%pB: warning: allocated section `%s' not in segment"), 6645 1.3 christos abfd, 6646 1.3 christos (hdr->bfd_section == NULL 6647 1.1 christos ? "*unknown*" 6648 1.1 christos : hdr->bfd_section->name)); 6649 1.14 christos /* We don't need to page align empty sections. */ 6650 1.1 christos if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) 6651 1.14 christos align = maxpagesize; 6652 1.14 christos else 6653 1.18 christos align = hdr->sh_addralign & -hdr->sh_addralign; 6654 1.18 christos off += vma_page_aligned_bias (hdr->sh_addr, off, align); 6655 1.1 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, false, 6656 1.1 christos bed->s->log_file_align); 6657 1.1 christos } 6658 1.14 christos else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) 6659 1.14 christos && hdr->bfd_section == NULL) 6660 1.14 christos /* We don't know the offset of these sections yet: 6661 1.14 christos their size has not been decided. */ 6662 1.14 christos || (abfd->is_linker_output 6663 1.14 christos && hdr->bfd_section != NULL 6664 1.3 christos && (hdr->sh_name == -1u 6665 1.8 christos || bfd_section_is_ctf (hdr->bfd_section))) 6666 1.8 christos || hdr == i_shdrpp[elf_onesymtab (abfd)] 6667 1.6 christos || (elf_symtab_shndx_list (abfd) != NULL 6668 1.6 christos && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) 6669 1.1 christos || hdr == i_shdrpp[elf_strtab_sec (abfd)] 6670 1.1 christos || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) 6671 1.18 christos hdr->sh_offset = -1; 6672 1.1 christos else 6673 1.12 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0); 6674 1.1 christos } 6675 1.1 christos elf_next_file_pos (abfd) = off; 6676 1.1 christos 6677 1.1 christos /* Now that we have set the section file positions, we can set up 6678 1.3 christos the file positions for the non PT_LOAD segments. */ 6679 1.1 christos phdrs = elf_tdata (abfd)->phdr; 6680 1.1 christos for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) 6681 1.1 christos { 6682 1.12 christos if (p->p_type == PT_GNU_RELRO) 6683 1.14 christos { 6684 1.1 christos bfd_vma start, end; /* Bytes. */ 6685 1.1 christos bool ok; 6686 1.1 christos 6687 1.1 christos if (link_info != NULL) 6688 1.11 christos { 6689 1.11 christos /* During linking the range of the RELRO segment is passed 6690 1.11 christos in link_info. Note that there may be padding between 6691 1.11 christos relro_start and the first RELRO section. */ 6692 1.11 christos start = link_info->relro_start; 6693 1.11 christos end = link_info->relro_end; 6694 1.11 christos } 6695 1.11 christos else if (m->count != 0) 6696 1.11 christos { 6697 1.11 christos if (!m->p_size_valid) 6698 1.12 christos abort (); 6699 1.11 christos start = m->sections[0]->vma; 6700 1.11 christos end = start + m->p_size / opb; 6701 1.11 christos } 6702 1.11 christos else 6703 1.11 christos { 6704 1.11 christos start = 0; 6705 1.11 christos end = 0; 6706 1.14 christos } 6707 1.11 christos 6708 1.11 christos ok = false; 6709 1.11 christos if (start < end) 6710 1.11 christos { 6711 1.11 christos struct elf_segment_map *lm; 6712 1.11 christos const Elf_Internal_Phdr *lp; 6713 1.11 christos unsigned int i; 6714 1.11 christos 6715 1.3 christos /* Find a LOAD segment containing a section in the RELRO 6716 1.3 christos segment. */ 6717 1.3 christos for (lm = elf_seg_map (abfd), lp = phdrs; 6718 1.1 christos lm != NULL; 6719 1.1 christos lm = lm->next, lp++) 6720 1.3 christos { 6721 1.11 christos if (lp->p_type == PT_LOAD 6722 1.11 christos && lm->count != 0 6723 1.12 christos && (lm->sections[lm->count - 1]->vma 6724 1.11 christos + (!IS_TBSS (lm->sections[lm->count - 1]) 6725 1.11 christos ? lm->sections[lm->count - 1]->size / opb 6726 1.1 christos : 0)) > start 6727 1.1 christos && lm->sections[0]->vma < end) 6728 1.3 christos break; 6729 1.11 christos } 6730 1.1 christos 6731 1.11 christos if (lm != NULL) 6732 1.11 christos { 6733 1.11 christos /* Find the section starting the RELRO segment. */ 6734 1.11 christos for (i = 0; i < lm->count; i++) 6735 1.11 christos { 6736 1.11 christos asection *s = lm->sections[i]; 6737 1.11 christos if (s->vma >= start 6738 1.11 christos && s->vma < end 6739 1.11 christos && s->size != 0) 6740 1.11 christos break; 6741 1.11 christos } 6742 1.11 christos 6743 1.12 christos if (i < lm->count) 6744 1.12 christos { 6745 1.11 christos p->p_vaddr = lm->sections[i]->vma * opb; 6746 1.12 christos p->p_paddr = lm->sections[i]->lma * opb; 6747 1.11 christos p->p_offset = lm->sections[i]->filepos; 6748 1.11 christos p->p_memsz = end * opb - p->p_vaddr; 6749 1.11 christos p->p_filesz = p->p_memsz; 6750 1.11 christos 6751 1.11 christos /* The RELRO segment typically ends a few bytes 6752 1.11 christos into .got.plt but other layouts are possible. 6753 1.11 christos In cases where the end does not match any 6754 1.11 christos loaded section (for instance is in file 6755 1.11 christos padding), trim p_filesz back to correspond to 6756 1.11 christos the end of loaded section contents. */ 6757 1.11 christos if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr) 6758 1.11 christos p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr; 6759 1.11 christos 6760 1.11 christos /* Preserve the alignment and flags if they are 6761 1.11 christos valid. The gold linker generates RW/4 for 6762 1.11 christos the PT_GNU_RELRO section. It is better for 6763 1.11 christos objcopy/strip to honor these attributes 6764 1.11 christos otherwise gdb will choke when using separate 6765 1.11 christos debug files. */ 6766 1.11 christos if (!m->p_align_valid) 6767 1.11 christos p->p_align = 1; 6768 1.14 christos if (!m->p_flags_valid) 6769 1.11 christos p->p_flags = PF_R; 6770 1.1 christos ok = true; 6771 1.1 christos } 6772 1.14 christos } 6773 1.11 christos } 6774 1.14 christos 6775 1.14 christos if (!ok) 6776 1.14 christos { 6777 1.14 christos if (link_info != NULL) 6778 1.14 christos _bfd_error_handler 6779 1.14 christos (_("%pB: warning: unable to allocate any sections" 6780 1.14 christos " to PT_GNU_RELRO segment"), 6781 1.14 christos abfd); 6782 1.1 christos memset (p, 0, sizeof *p); 6783 1.3 christos } 6784 1.3 christos } 6785 1.3 christos else if (p->p_type == PT_GNU_STACK) 6786 1.3 christos { 6787 1.3 christos if (m->p_size_valid) 6788 1.1 christos p->p_memsz = m->p_size; 6789 1.1 christos } 6790 1.6 christos else if (m->count != 0) 6791 1.9 christos { 6792 1.1 christos unsigned int i; 6793 1.1 christos 6794 1.1 christos if (p->p_type != PT_LOAD 6795 1.1 christos && (p->p_type != PT_NOTE 6796 1.9 christos || bfd_get_format (abfd) != bfd_core)) 6797 1.9 christos { 6798 1.9 christos /* A user specified segment layout may include a PHDR 6799 1.9 christos segment that overlaps with a LOAD segment... */ 6800 1.9 christos if (p->p_type == PT_PHDR) 6801 1.9 christos { 6802 1.9 christos m->count = 0; 6803 1.9 christos continue; 6804 1.6 christos } 6805 1.6 christos 6806 1.6 christos if (m->includes_filehdr || m->includes_phdrs) 6807 1.9 christos { 6808 1.11 christos /* PR 17512: file: 2195325e. */ 6809 1.11 christos _bfd_error_handler 6810 1.11 christos (_("%pB: error: non-load segment %d includes file header " 6811 1.14 christos "and/or program header"), 6812 1.6 christos abfd, (int) (p - phdrs)); 6813 1.1 christos return false; 6814 1.3 christos } 6815 1.1 christos 6816 1.3 christos p->p_filesz = 0; 6817 1.3 christos p->p_offset = m->sections[0]->filepos; 6818 1.3 christos for (i = m->count; i-- != 0;) 6819 1.3 christos { 6820 1.3 christos asection *sect = m->sections[i]; 6821 1.3 christos Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; 6822 1.14 christos if (hdr->sh_type != SHT_NOBITS) 6823 1.14 christos { 6824 1.14 christos p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size; 6825 1.14 christos /* NB: p_memsz of the loadable PT_NOTE segment 6826 1.14 christos should be the same as p_filesz. */ 6827 1.14 christos if (p->p_type == PT_NOTE 6828 1.3 christos && (hdr->sh_flags & SHF_ALLOC) != 0) 6829 1.3 christos p->p_memsz = p->p_filesz; 6830 1.3 christos break; 6831 1.1 christos } 6832 1.1 christos } 6833 1.1 christos } 6834 1.1 christos } 6835 1.14 christos } 6836 1.1 christos 6837 1.1 christos return true; 6838 1.8 christos } 6839 1.8 christos 6840 1.8 christos static elf_section_list * 6841 1.8 christos find_section_in_list (unsigned int i, elf_section_list * list) 6842 1.8 christos { 6843 1.8 christos for (;list != NULL; list = list->next) 6844 1.8 christos if (list->ndx == i) 6845 1.8 christos break; 6846 1.8 christos return list; 6847 1.1 christos } 6848 1.1 christos 6849 1.1 christos /* Work out the file positions of all the sections. This is called by 6850 1.1 christos _bfd_elf_compute_section_file_positions. All the section sizes and 6851 1.1 christos VMAs must be known before this is called. 6852 1.14 christos 6853 1.14 christos Reloc sections come in two flavours: Those processed specially as 6854 1.14 christos "side-channel" data attached to a section to which they apply, and 6855 1.14 christos those that bfd doesn't process as relocations. The latter sort are 6856 1.14 christos stored in a normal bfd section by bfd_section_from_shdr. We don't 6857 1.14 christos consider the former sort here, unless they form part of the loadable 6858 1.14 christos image. Reloc sections not assigned here (and compressed debugging 6859 1.1 christos sections and CTF sections which nothing else in the file can rely 6860 1.1 christos upon) will be handled later by assign_file_positions_for_relocs. 6861 1.1 christos 6862 1.14 christos We also don't set the positions of the .symtab and .strtab here. */ 6863 1.1 christos 6864 1.1 christos static bool 6865 1.1 christos assign_file_positions_except_relocs (bfd *abfd, 6866 1.1 christos struct bfd_link_info *link_info) 6867 1.1 christos { 6868 1.1 christos struct elf_obj_tdata *tdata = elf_tdata (abfd); 6869 1.12 christos Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); 6870 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 6871 1.1 christos unsigned int alloc; 6872 1.1 christos 6873 1.1 christos if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 6874 1.1 christos && bfd_get_format (abfd) != bfd_core) 6875 1.1 christos { 6876 1.1 christos Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); 6877 1.1 christos unsigned int num_sec = elf_numsections (abfd); 6878 1.5 christos Elf_Internal_Shdr **hdrpp; 6879 1.1 christos unsigned int i; 6880 1.1 christos file_ptr off; 6881 1.1 christos 6882 1.1 christos /* Start after the ELF header. */ 6883 1.1 christos off = i_ehdrp->e_ehsize; 6884 1.1 christos 6885 1.1 christos /* We are not creating an executable, which means that we are 6886 1.1 christos not creating a program header, and that the actual order of 6887 1.1 christos the sections in the file is unimportant. */ 6888 1.1 christos for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) 6889 1.1 christos { 6890 1.1 christos Elf_Internal_Shdr *hdr; 6891 1.1 christos 6892 1.1 christos hdr = *hdrpp; 6893 1.12 christos if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) 6894 1.12 christos && hdr->bfd_section == NULL) 6895 1.14 christos /* Do not assign offsets for these sections yet: we don't know 6896 1.14 christos their sizes. */ 6897 1.14 christos || (abfd->is_linker_output 6898 1.14 christos && hdr->bfd_section != NULL 6899 1.3 christos && (hdr->sh_name == -1u 6900 1.8 christos || bfd_section_is_ctf (hdr->bfd_section))) 6901 1.8 christos || i == elf_onesymtab (abfd) 6902 1.6 christos || (elf_symtab_shndx_list (abfd) != NULL 6903 1.6 christos && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) 6904 1.1 christos || i == elf_strtab_sec (abfd) 6905 1.1 christos || i == elf_shstrtab_sec (abfd)) 6906 1.1 christos { 6907 1.1 christos hdr->sh_offset = -1; 6908 1.19 christos } 6909 1.19 christos else 6910 1.19 christos /* There shouldn't be a need to effect "capped" file alignment here, 6911 1.19 christos yet at least the Linux kernel's modpost utility was found to be 6912 1.18 christos unhappy without. While the issue was addressed there, let's be 6913 1.19 christos kind for at least the foreseeable future ... */ 6914 1.1 christos off = _bfd_elf_assign_file_position_for_section (hdr, off, false, 6915 1.5 christos bed->s->log_file_align); 6916 1.5 christos } 6917 1.12 christos 6918 1.1 christos elf_next_file_pos (abfd) = off; 6919 1.1 christos elf_program_header_size (abfd) = 0; 6920 1.1 christos } 6921 1.1 christos else 6922 1.1 christos { 6923 1.1 christos /* Assign file positions for the loaded sections based on the 6924 1.14 christos assignment of sections to segments. */ 6925 1.1 christos if (!assign_file_positions_for_load_sections (abfd, link_info)) 6926 1.1 christos return false; 6927 1.1 christos 6928 1.14 christos /* And for non-load sections. */ 6929 1.12 christos if (!assign_file_positions_for_non_load_sections (abfd, link_info)) 6930 1.1 christos return false; 6931 1.12 christos } 6932 1.14 christos 6933 1.1 christos if (!(*bed->elf_backend_modify_headers) (abfd, link_info)) 6934 1.12 christos return false; 6935 1.12 christos 6936 1.12 christos /* Write out the program headers. */ 6937 1.12 christos alloc = i_ehdrp->e_phnum; 6938 1.14 christos if (alloc != 0) 6939 1.14 christos { 6940 1.17 christos if (link_info != NULL && ! link_info->no_warn_rwx_segments) 6941 1.17 christos { 6942 1.17 christos bool warned_tls = false; 6943 1.14 christos bool warned_rwx = false; 6944 1.14 christos 6945 1.14 christos /* Memory resident segments with non-zero size and RWX 6946 1.14 christos permissions are a security risk, so we generate a warning 6947 1.14 christos here if we are creating any. */ 6948 1.14 christos unsigned int i; 6949 1.14 christos 6950 1.14 christos for (i = 0; i < alloc; i++) 6951 1.14 christos { 6952 1.14 christos const Elf_Internal_Phdr * phdr = tdata->phdr + i; 6953 1.14 christos 6954 1.14 christos if (phdr->p_memsz == 0) 6955 1.17 christos continue; 6956 1.17 christos 6957 1.17 christos if (! warned_tls 6958 1.17 christos && phdr->p_type == PT_TLS 6959 1.17 christos && (phdr->p_flags & PF_X)) 6960 1.17 christos { 6961 1.17 christos if (link_info->warn_is_error_for_rwx_segments) 6962 1.17 christos { 6963 1.17 christos _bfd_error_handler (_("\ 6964 1.17 christos error: %pB has a TLS segment with execute permission"), 6965 1.17 christos abfd); 6966 1.17 christos return false; 6967 1.17 christos } 6968 1.17 christos 6969 1.17 christos _bfd_error_handler (_("\ 6970 1.17 christos warning: %pB has a TLS segment with execute permission"), 6971 1.17 christos abfd); 6972 1.17 christos if (warned_rwx) 6973 1.17 christos break; 6974 1.17 christos 6975 1.17 christos warned_tls = true; 6976 1.17 christos } 6977 1.14 christos else if (! warned_rwx 6978 1.14 christos && phdr->p_type == PT_LOAD 6979 1.17 christos && ((phdr->p_flags & (PF_R | PF_W | PF_X)) 6980 1.17 christos == (PF_R | PF_W | PF_X))) 6981 1.17 christos { 6982 1.17 christos if (link_info->warn_is_error_for_rwx_segments) 6983 1.17 christos { 6984 1.17 christos _bfd_error_handler (_("\ 6985 1.17 christos error: %pB has a LOAD segment with RWX permissions"), 6986 1.17 christos abfd); 6987 1.17 christos return false; 6988 1.17 christos } 6989 1.17 christos 6990 1.17 christos _bfd_error_handler (_("\ 6991 1.17 christos warning: %pB has a LOAD segment with RWX permissions"), 6992 1.17 christos abfd); 6993 1.17 christos if (warned_tls) 6994 1.17 christos break; 6995 1.17 christos 6996 1.14 christos warned_rwx = true; 6997 1.14 christos } 6998 1.14 christos } 6999 1.12 christos } 7000 1.1 christos 7001 1.14 christos if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0 7002 1.1 christos || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) 7003 1.1 christos return false; 7004 1.14 christos } 7005 1.1 christos 7006 1.1 christos return true; 7007 1.14 christos } 7008 1.12 christos 7009 1.12 christos bool 7010 1.1 christos _bfd_elf_init_file_header (bfd *abfd, 7011 1.1 christos struct bfd_link_info *info ATTRIBUTE_UNUSED) 7012 1.1 christos { 7013 1.1 christos Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ 7014 1.1 christos struct elf_strtab_hash *shstrtab; 7015 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 7016 1.1 christos 7017 1.1 christos i_ehdrp = elf_elfheader (abfd); 7018 1.1 christos 7019 1.14 christos shstrtab = _bfd_elf_strtab_init (); 7020 1.1 christos if (shstrtab == NULL) 7021 1.1 christos return false; 7022 1.1 christos 7023 1.1 christos elf_shstrtab (abfd) = shstrtab; 7024 1.1 christos 7025 1.1 christos i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; 7026 1.1 christos i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; 7027 1.1 christos i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; 7028 1.1 christos i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; 7029 1.1 christos 7030 1.1 christos i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; 7031 1.1 christos i_ehdrp->e_ident[EI_DATA] = 7032 1.1 christos bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; 7033 1.1 christos i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; 7034 1.1 christos 7035 1.1 christos if ((abfd->flags & DYNAMIC) != 0) 7036 1.1 christos i_ehdrp->e_type = ET_DYN; 7037 1.1 christos else if ((abfd->flags & EXEC_P) != 0) 7038 1.1 christos i_ehdrp->e_type = ET_EXEC; 7039 1.1 christos else if (bfd_get_format (abfd) == bfd_core) 7040 1.1 christos i_ehdrp->e_type = ET_CORE; 7041 1.1 christos else 7042 1.1 christos i_ehdrp->e_type = ET_REL; 7043 1.1 christos 7044 1.1 christos switch (bfd_get_arch (abfd)) 7045 1.1 christos { 7046 1.1 christos case bfd_arch_unknown: 7047 1.1 christos i_ehdrp->e_machine = EM_NONE; 7048 1.1 christos break; 7049 1.1 christos 7050 1.1 christos /* There used to be a long list of cases here, each one setting 7051 1.1 christos e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE 7052 1.1 christos in the corresponding bfd definition. To avoid duplication, 7053 1.1 christos the switch was removed. Machines that need special handling 7054 1.1 christos can generally do it in elf_backend_final_write_processing(), 7055 1.1 christos unless they need the information earlier than the final write. 7056 1.1 christos Such need can generally be supplied by replacing the tests for 7057 1.1 christos e_machine with the conditions used to determine it. */ 7058 1.1 christos default: 7059 1.1 christos i_ehdrp->e_machine = bed->elf_machine_code; 7060 1.1 christos } 7061 1.1 christos 7062 1.1 christos i_ehdrp->e_version = bed->s->ev_current; 7063 1.1 christos i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; 7064 1.1 christos 7065 1.1 christos /* No program header, for now. */ 7066 1.1 christos i_ehdrp->e_phoff = 0; 7067 1.1 christos i_ehdrp->e_phentsize = 0; 7068 1.1 christos i_ehdrp->e_phnum = 0; 7069 1.1 christos 7070 1.1 christos /* Each bfd section is section header entry. */ 7071 1.1 christos i_ehdrp->e_entry = bfd_get_start_address (abfd); 7072 1.1 christos i_ehdrp->e_shentsize = bed->s->sizeof_shdr; 7073 1.14 christos 7074 1.1 christos elf_tdata (abfd)->symtab_hdr.sh_name = 7075 1.14 christos (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false); 7076 1.1 christos elf_tdata (abfd)->strtab_hdr.sh_name = 7077 1.14 christos (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false); 7078 1.1 christos elf_tdata (abfd)->shstrtab_hdr.sh_name = 7079 1.5 christos (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false); 7080 1.1 christos if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 7081 1.14 christos || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 7082 1.1 christos || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) 7083 1.14 christos return false; 7084 1.1 christos 7085 1.1 christos return true; 7086 1.12 christos } 7087 1.12 christos 7088 1.12 christos /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. 7089 1.12 christos 7090 1.12 christos FIXME: We used to have code here to sort the PT_LOAD segments into 7091 1.12 christos ascending order, as per the ELF spec. But this breaks some programs, 7092 1.12 christos including the Linux kernel. But really either the spec should be 7093 1.14 christos changed or the programs updated. */ 7094 1.12 christos 7095 1.12 christos bool 7096 1.12 christos _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info) 7097 1.12 christos { 7098 1.12 christos if (link_info != NULL && bfd_link_pie (link_info)) 7099 1.12 christos { 7100 1.12 christos Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd); 7101 1.12 christos unsigned int num_segments = i_ehdrp->e_phnum; 7102 1.12 christos struct elf_obj_tdata *tdata = elf_tdata (obfd); 7103 1.12 christos Elf_Internal_Phdr *segment = tdata->phdr; 7104 1.12 christos Elf_Internal_Phdr *end_segment = &segment[num_segments]; 7105 1.12 christos 7106 1.12 christos /* Find the lowest p_vaddr in PT_LOAD segments. */ 7107 1.12 christos bfd_vma p_vaddr = (bfd_vma) -1; 7108 1.12 christos for (; segment < end_segment; segment++) 7109 1.12 christos if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) 7110 1.12 christos p_vaddr = segment->p_vaddr; 7111 1.12 christos 7112 1.12 christos /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD 7113 1.12 christos segments is non-zero. */ 7114 1.12 christos if (p_vaddr) 7115 1.14 christos i_ehdrp->e_type = ET_EXEC; 7116 1.12 christos } 7117 1.12 christos return true; 7118 1.1 christos } 7119 1.5 christos 7120 1.1 christos /* Assign file positions for all the reloc sections which are not part 7121 1.14 christos of the loadable file image, and the file position of section headers. */ 7122 1.6 christos 7123 1.1 christos static bool 7124 1.1 christos _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) 7125 1.6 christos { 7126 1.6 christos file_ptr off; 7127 1.5 christos Elf_Internal_Shdr **shdrpp, **end_shdrpp; 7128 1.18 christos Elf_Internal_Shdr *shdrp; 7129 1.1 christos Elf_Internal_Ehdr *i_ehdrp; 7130 1.17 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 7131 1.17 christos 7132 1.17 christos /* Skip non-load sections without section header. */ 7133 1.17 christos if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0) 7134 1.3 christos return true; 7135 1.1 christos 7136 1.6 christos off = elf_next_file_pos (abfd); 7137 1.6 christos 7138 1.6 christos shdrpp = elf_elfsections (abfd); 7139 1.1 christos end_shdrpp = shdrpp + elf_numsections (abfd); 7140 1.6 christos for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) 7141 1.6 christos { 7142 1.6 christos shdrp = *shdrpp; 7143 1.6 christos if (shdrp->sh_offset == -1) 7144 1.14 christos { 7145 1.14 christos asection *sec = shdrp->bfd_section; 7146 1.14 christos if (sec == NULL 7147 1.14 christos || shdrp->sh_type == SHT_REL 7148 1.14 christos || shdrp->sh_type == SHT_RELA) 7149 1.14 christos ; 7150 1.14 christos else if (bfd_section_is_ctf (sec)) 7151 1.14 christos { 7152 1.14 christos /* Update section size and contents. */ 7153 1.14 christos shdrp->sh_size = sec->size; 7154 1.14 christos shdrp->contents = sec->contents; 7155 1.14 christos } 7156 1.14 christos else if (shdrp->sh_name == -1u) 7157 1.14 christos { 7158 1.14 christos const char *name = sec->name; 7159 1.14 christos struct bfd_elf_section_data *d; 7160 1.14 christos 7161 1.14 christos /* Compress DWARF debug sections. */ 7162 1.14 christos if (!bfd_compress_section (abfd, sec, shdrp->contents)) 7163 1.14 christos return false; 7164 1.14 christos 7165 1.14 christos if (sec->compress_status == COMPRESS_SECTION_DONE 7166 1.14 christos && (abfd->flags & BFD_COMPRESS_GABI) == 0 7167 1.14 christos && name[1] == 'd') 7168 1.14 christos { 7169 1.14 christos /* If section is compressed with zlib-gnu, convert 7170 1.14 christos section name from .debug_* to .zdebug_*. */ 7171 1.14 christos char *new_name = bfd_debug_name_to_zdebug (abfd, name); 7172 1.14 christos if (new_name == NULL) 7173 1.14 christos return false; 7174 1.14 christos name = new_name; 7175 1.14 christos } 7176 1.14 christos /* Add section name to section name section. */ 7177 1.14 christos shdrp->sh_name 7178 1.14 christos = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 7179 1.14 christos name, false); 7180 1.14 christos d = elf_section_data (sec); 7181 1.14 christos 7182 1.14 christos /* Add reloc section name to section name section. */ 7183 1.14 christos if (d->rel.hdr 7184 1.14 christos && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr, 7185 1.14 christos name, false)) 7186 1.14 christos return false; 7187 1.14 christos if (d->rela.hdr 7188 1.14 christos && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr, 7189 1.14 christos name, true)) 7190 1.14 christos return false; 7191 1.14 christos 7192 1.14 christos /* Update section size and contents. */ 7193 1.14 christos shdrp->sh_size = sec->size; 7194 1.14 christos shdrp->contents = sec->contents; 7195 1.1 christos sec->contents = NULL; 7196 1.18 christos } 7197 1.18 christos 7198 1.18 christos off = _bfd_elf_assign_file_position_for_section (shdrp, off, 7199 1.18 christos (abfd->flags & (EXEC_P | DYNAMIC)) 7200 1.6 christos || bfd_get_format (abfd) == bfd_core, 7201 1.1 christos bed->s->log_file_align); 7202 1.1 christos } 7203 1.6 christos } 7204 1.6 christos 7205 1.6 christos /* Place section name section after DWARF debug sections have been 7206 1.6 christos compressed. */ 7207 1.6 christos _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); 7208 1.18 christos shdrp = &elf_tdata (abfd)->shstrtab_hdr; 7209 1.6 christos shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); 7210 1.6 christos off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0); 7211 1.5 christos 7212 1.18 christos /* Place the section headers. */ 7213 1.5 christos i_ehdrp = elf_elfheader (abfd); 7214 1.5 christos off = BFD_ALIGN (off, 1u << bed->s->log_file_align); 7215 1.3 christos i_ehdrp->e_shoff = off; 7216 1.6 christos off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; 7217 1.14 christos elf_next_file_pos (abfd) = off; 7218 1.1 christos 7219 1.1 christos return true; 7220 1.14 christos } 7221 1.1 christos 7222 1.1 christos bool 7223 1.1 christos _bfd_elf_write_object_contents (bfd *abfd) 7224 1.1 christos { 7225 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 7226 1.1 christos Elf_Internal_Shdr **i_shdrp; 7227 1.3 christos bool failed; 7228 1.1 christos unsigned int count, num_sec; 7229 1.1 christos struct elf_obj_tdata *t; 7230 1.1 christos 7231 1.14 christos if (! abfd->output_has_begun 7232 1.11 christos && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) 7233 1.14 christos return false; 7234 1.14 christos /* Do not rewrite ELF data when the BFD has been opened for update. 7235 1.14 christos abfd->output_has_begun was set to TRUE on opening, so creation of 7236 1.14 christos new sections, and modification of existing section sizes was 7237 1.14 christos restricted. This means the ELF header, program headers and 7238 1.14 christos section headers can't have changed. If the contents of any 7239 1.11 christos sections has been modified, then those changes have already been 7240 1.11 christos written to the BFD. */ 7241 1.11 christos else if (abfd->direction == both_direction) 7242 1.14 christos { 7243 1.11 christos BFD_ASSERT (abfd->output_has_begun); 7244 1.1 christos return true; 7245 1.1 christos } 7246 1.1 christos 7247 1.14 christos i_shdrp = elf_elfsections (abfd); 7248 1.1 christos 7249 1.1 christos failed = false; 7250 1.14 christos bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); 7251 1.1 christos if (failed) 7252 1.6 christos return false; 7253 1.14 christos 7254 1.1 christos if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) 7255 1.1 christos return false; 7256 1.1 christos 7257 1.1 christos /* After writing the headers, we need to write the sections too... */ 7258 1.1 christos num_sec = elf_numsections (abfd); 7259 1.17 christos for (count = 1; count < num_sec; count++) 7260 1.17 christos { 7261 1.17 christos /* Don't set the sh_name field without section header. */ 7262 1.17 christos if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0) 7263 1.17 christos i_shdrp[count]->sh_name 7264 1.1 christos = _bfd_elf_strtab_offset (elf_shstrtab (abfd), 7265 1.11 christos i_shdrp[count]->sh_name); 7266 1.14 christos if (bed->elf_backend_section_processing) 7267 1.1 christos if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count])) 7268 1.1 christos return false; 7269 1.1 christos if (i_shdrp[count]->contents) 7270 1.1 christos { 7271 1.1 christos bfd_size_type amt = i_shdrp[count]->sh_size; 7272 1.17 christos 7273 1.14 christos if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 7274 1.1 christos || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt) 7275 1.1 christos return false; 7276 1.1 christos } 7277 1.1 christos } 7278 1.3 christos 7279 1.1 christos /* Write out the section header names. */ 7280 1.17 christos t = elf_tdata (abfd); 7281 1.3 christos if (elf_shstrtab (abfd) != NULL 7282 1.1 christos && t->shstrtab_hdr.sh_offset != -1 7283 1.14 christos && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 7284 1.1 christos || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) 7285 1.12 christos return false; 7286 1.14 christos 7287 1.1 christos if (!(*bed->elf_backend_final_write_processing) (abfd)) 7288 1.1 christos return false; 7289 1.14 christos 7290 1.1 christos if (!bed->s->write_shdrs_and_ehdr (abfd)) 7291 1.1 christos return false; 7292 1.14 christos 7293 1.14 christos /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ 7294 1.14 christos if (t->o->build_id.after_write_object_contents != NULL 7295 1.14 christos && !(*t->o->build_id.after_write_object_contents) (abfd)) 7296 1.14 christos return false; 7297 1.14 christos if (t->o->package_metadata.after_write_object_contents != NULL 7298 1.1 christos && !(*t->o->package_metadata.after_write_object_contents) (abfd)) 7299 1.14 christos return false; 7300 1.1 christos 7301 1.1 christos return true; 7302 1.14 christos } 7303 1.1 christos 7304 1.1 christos bool 7305 1.1 christos _bfd_elf_write_corefile_contents (bfd *abfd) 7306 1.1 christos { 7307 1.1 christos /* Hopefully this can be done just like an object file. */ 7308 1.1 christos return _bfd_elf_write_object_contents (abfd); 7309 1.1 christos } 7310 1.1 christos 7311 1.1 christos /* Given a section, search the header to find them. */ 7312 1.1 christos 7313 1.1 christos unsigned int 7314 1.1 christos _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) 7315 1.1 christos { 7316 1.1 christos const struct elf_backend_data *bed; 7317 1.1 christos unsigned int sec_index; 7318 1.1 christos 7319 1.1 christos if (elf_section_data (asect) != NULL 7320 1.1 christos && elf_section_data (asect)->this_idx != 0) 7321 1.1 christos return elf_section_data (asect)->this_idx; 7322 1.1 christos 7323 1.1 christos if (bfd_is_abs_section (asect)) 7324 1.1 christos sec_index = SHN_ABS; 7325 1.1 christos else if (bfd_is_com_section (asect)) 7326 1.1 christos sec_index = SHN_COMMON; 7327 1.1 christos else if (bfd_is_und_section (asect)) 7328 1.1 christos sec_index = SHN_UNDEF; 7329 1.1 christos else 7330 1.1 christos sec_index = SHN_BAD; 7331 1.1 christos 7332 1.1 christos bed = get_elf_backend_data (abfd); 7333 1.1 christos if (bed->elf_backend_section_from_bfd_section) 7334 1.1 christos { 7335 1.1 christos int retval = sec_index; 7336 1.1 christos 7337 1.1 christos if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) 7338 1.1 christos return retval; 7339 1.1 christos } 7340 1.1 christos 7341 1.1 christos if (sec_index == SHN_BAD) 7342 1.1 christos bfd_set_error (bfd_error_nonrepresentable_section); 7343 1.1 christos 7344 1.1 christos return sec_index; 7345 1.1 christos } 7346 1.1 christos 7347 1.1 christos /* Given a BFD symbol, return the index in the ELF symbol table, or -1 7348 1.1 christos on error. */ 7349 1.1 christos 7350 1.1 christos int 7351 1.1 christos _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) 7352 1.1 christos { 7353 1.1 christos asymbol *asym_ptr = *asym_ptr_ptr; 7354 1.1 christos int idx; 7355 1.1 christos flagword flags = asym_ptr->flags; 7356 1.1 christos 7357 1.1 christos /* When gas creates relocations against local labels, it creates its 7358 1.1 christos own symbol for the section, but does put the symbol into the 7359 1.1 christos symbol chain, so udata is 0. When the linker is generating 7360 1.1 christos relocatable output, this section symbol may be for one of the 7361 1.1 christos input sections rather than the output section. */ 7362 1.1 christos if (asym_ptr->udata.i == 0 7363 1.1 christos && (flags & BSF_SECTION_SYM) 7364 1.1 christos && asym_ptr->section) 7365 1.1 christos { 7366 1.1 christos asection *sec; 7367 1.1 christos 7368 1.1 christos sec = asym_ptr->section; 7369 1.1 christos if (sec->owner != abfd && sec->output_section != NULL) 7370 1.14 christos sec = sec->output_section; 7371 1.14 christos if (sec->owner == abfd 7372 1.14 christos && sec->index < elf_num_section_syms (abfd) 7373 1.1 christos && elf_section_syms (abfd)[sec->index] != NULL) 7374 1.1 christos asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i; 7375 1.1 christos } 7376 1.1 christos 7377 1.1 christos idx = asym_ptr->udata.i; 7378 1.1 christos 7379 1.1 christos if (idx == 0) 7380 1.1 christos { 7381 1.9 christos /* This case can occur when using --strip-symbol on a symbol 7382 1.9 christos which is used in a relocation entry. */ 7383 1.11 christos _bfd_error_handler 7384 1.1 christos /* xgettext:c-format */ 7385 1.1 christos (_("%pB: symbol `%s' required but not present"), 7386 1.1 christos abfd, bfd_asymbol_name (asym_ptr)); 7387 1.1 christos bfd_set_error (bfd_error_no_symbols); 7388 1.1 christos return -1; 7389 1.1 christos } 7390 1.1 christos 7391 1.1 christos #if DEBUG & 4 7392 1.14 christos { 7393 1.14 christos fprintf (stderr, 7394 1.11 christos "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d," 7395 1.1 christos " flags = 0x%.8x\n", 7396 1.1 christos (long) asym_ptr, asym_ptr->name, idx, flags); 7397 1.1 christos fflush (stderr); 7398 1.1 christos } 7399 1.1 christos #endif 7400 1.1 christos 7401 1.1 christos return idx; 7402 1.14 christos } 7403 1.14 christos 7404 1.14 christos static inline bfd_vma 7405 1.14 christos segment_size (Elf_Internal_Phdr *segment) 7406 1.14 christos { 7407 1.14 christos return (segment->p_memsz > segment->p_filesz 7408 1.14 christos ? segment->p_memsz : segment->p_filesz); 7409 1.14 christos } 7410 1.14 christos 7411 1.14 christos 7412 1.14 christos /* Returns the end address of the segment + 1. */ 7413 1.14 christos static inline bfd_vma 7414 1.14 christos segment_end (Elf_Internal_Phdr *segment, bfd_vma start) 7415 1.14 christos { 7416 1.14 christos return start + segment_size (segment); 7417 1.14 christos } 7418 1.14 christos 7419 1.14 christos static inline bfd_size_type 7420 1.14 christos section_size (asection *section, Elf_Internal_Phdr *segment) 7421 1.14 christos { 7422 1.14 christos if ((section->flags & SEC_HAS_CONTENTS) != 0 7423 1.14 christos || (section->flags & SEC_THREAD_LOCAL) == 0 7424 1.14 christos || segment->p_type == PT_TLS) 7425 1.14 christos return section->size; 7426 1.14 christos return 0; 7427 1.14 christos } 7428 1.14 christos 7429 1.17 christos /* Returns TRUE if the given section is contained within the given 7430 1.14 christos segment. LMA addresses are compared against PADDR when 7431 1.14 christos USE_VADDR is false, VMA against VADDR when true. */ 7432 1.14 christos static bool 7433 1.17 christos is_contained_by (asection *section, Elf_Internal_Phdr *segment, 7434 1.14 christos bfd_vma paddr, bfd_vma vaddr, unsigned int opb, 7435 1.17 christos bool use_vaddr) 7436 1.17 christos { 7437 1.14 christos bfd_vma seg_addr = !use_vaddr ? paddr : vaddr; 7438 1.14 christos bfd_vma addr = !use_vaddr ? section->lma : section->vma; 7439 1.14 christos bfd_vma octet; 7440 1.14 christos if (_bfd_mul_overflow (addr, opb, &octet)) 7441 1.14 christos return false; 7442 1.14 christos /* The third and fourth lines below are testing that the section end 7443 1.14 christos address is within the segment. It's written this way to avoid 7444 1.14 christos overflow. Add seg_addr + section_size to both sides of the 7445 1.14 christos inequality to make it obvious. */ 7446 1.14 christos return (octet >= seg_addr 7447 1.14 christos && segment_size (segment) >= section_size (section, segment) 7448 1.14 christos && (octet - seg_addr 7449 1.14 christos <= segment_size (segment) - section_size (section, segment))); 7450 1.14 christos } 7451 1.14 christos 7452 1.14 christos /* Handle PT_NOTE segment. */ 7453 1.14 christos static bool 7454 1.14 christos is_note (asection *s, Elf_Internal_Phdr *p) 7455 1.14 christos { 7456 1.14 christos return (p->p_type == PT_NOTE 7457 1.14 christos && elf_section_type (s) == SHT_NOTE 7458 1.14 christos && (ufile_ptr) s->filepos >= p->p_offset 7459 1.14 christos && p->p_filesz >= s->size 7460 1.14 christos && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size); 7461 1.1 christos } 7462 1.1 christos 7463 1.14 christos /* Rewrite program header information. */ 7464 1.14 christos 7465 1.1 christos static bool 7466 1.1 christos rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize) 7467 1.1 christos { 7468 1.1 christos Elf_Internal_Ehdr *iehdr; 7469 1.1 christos struct elf_segment_map *map; 7470 1.1 christos struct elf_segment_map *map_first; 7471 1.1 christos struct elf_segment_map **pointer_to_map; 7472 1.1 christos Elf_Internal_Phdr *segment; 7473 1.1 christos asection *section; 7474 1.14 christos unsigned int i; 7475 1.14 christos unsigned int num_segments; 7476 1.1 christos bool phdr_included = false; 7477 1.1 christos bool p_paddr_valid; 7478 1.1 christos struct elf_segment_map *phdr_adjust_seg = NULL; 7479 1.12 christos unsigned int phdr_adjust_num = 0; 7480 1.1 christos const struct elf_backend_data *bed; 7481 1.1 christos unsigned int opb = bfd_octets_per_byte (ibfd, NULL); 7482 1.1 christos 7483 1.1 christos bed = get_elf_backend_data (ibfd); 7484 1.1 christos iehdr = elf_elfheader (ibfd); 7485 1.1 christos 7486 1.1 christos map_first = NULL; 7487 1.1 christos pointer_to_map = &map_first; 7488 1.1 christos 7489 1.1 christos num_segments = elf_elfheader (ibfd)->e_phnum; 7490 1.1 christos 7491 1.1 christos /* The complicated case when p_vaddr is 0 is to handle the Solaris 7492 1.1 christos linker, which generates a PT_INTERP section with p_vaddr and 7493 1.1 christos p_memsz set to 0. */ 7494 1.1 christos #define IS_SOLARIS_PT_INTERP(p, s) \ 7495 1.1 christos (p->p_vaddr == 0 \ 7496 1.1 christos && p->p_paddr == 0 \ 7497 1.1 christos && p->p_memsz == 0 \ 7498 1.1 christos && p->p_filesz > 0 \ 7499 1.1 christos && (s->flags & SEC_HAS_CONTENTS) != 0 \ 7500 1.1 christos && s->size > 0 \ 7501 1.1 christos && (bfd_vma) s->filepos >= p->p_offset \ 7502 1.1 christos && ((bfd_vma) s->filepos + s->size \ 7503 1.1 christos <= p->p_offset + p->p_filesz)) 7504 1.1 christos 7505 1.1 christos /* Decide if the given section should be included in the given segment. 7506 1.1 christos A section will be included if: 7507 1.1 christos 1. It is within the address space of the segment -- we use the LMA 7508 1.3 christos if that is set for the segment and the VMA otherwise, 7509 1.1 christos 2. It is an allocated section or a NOTE section in a PT_NOTE 7510 1.1 christos segment. 7511 1.1 christos 3. There is an output section associated with it, 7512 1.1 christos 4. The section has not already been allocated to a previous segment. 7513 1.1 christos 5. PT_GNU_STACK segments do not include any sections. 7514 1.1 christos 6. PT_TLS segment includes only SHF_TLS sections. 7515 1.1 christos 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. 7516 1.17 christos 8. PT_DYNAMIC should not contain empty sections at the beginning 7517 1.14 christos (with the possible exception of .dynamic). */ 7518 1.17 christos #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \ 7519 1.1 christos (((is_contained_by (section, segment, segment->p_paddr, \ 7520 1.14 christos segment->p_vaddr, opb, !paddr_valid) \ 7521 1.1 christos && (section->flags & SEC_ALLOC) != 0) \ 7522 1.1 christos || is_note (section, segment)) \ 7523 1.1 christos && segment->p_type != PT_GNU_STACK \ 7524 1.1 christos && (segment->p_type != PT_TLS \ 7525 1.1 christos || (section->flags & SEC_THREAD_LOCAL)) \ 7526 1.1 christos && (segment->p_type == PT_LOAD \ 7527 1.1 christos || segment->p_type == PT_TLS \ 7528 1.14 christos || (section->flags & SEC_THREAD_LOCAL) == 0) \ 7529 1.1 christos && (segment->p_type != PT_DYNAMIC \ 7530 1.12 christos || section_size (section, segment) > 0 \ 7531 1.12 christos || (segment->p_paddr \ 7532 1.19 christos ? segment->p_paddr != section->lma * (opb) \ 7533 1.11 christos : segment->p_vaddr != section->vma * (opb)) \ 7534 1.1 christos || (streq (bfd_section_name (section), ".dynamic"))) \ 7535 1.1 christos && (segment->p_type != PT_LOAD || !section->segment_mark)) 7536 1.1 christos 7537 1.17 christos /* If the output section of a section in the input segment is NULL, 7538 1.17 christos it is removed from the corresponding output segment. */ 7539 1.1 christos #define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid) \ 7540 1.1 christos (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \ 7541 1.1 christos && section->output_section != NULL) 7542 1.1 christos 7543 1.14 christos /* Returns TRUE iff seg1 starts after the end of seg2. */ 7544 1.1 christos #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ 7545 1.1 christos (seg1->field >= segment_end (seg2, seg2->field)) 7546 1.1 christos 7547 1.1 christos /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both 7548 1.1 christos their VMA address ranges and their LMA address ranges overlap. 7549 1.1 christos It is possible to have overlapping VMA ranges without overlapping LMA 7550 1.1 christos ranges. RedBoot images for example can have both .data and .bss mapped 7551 1.1 christos to the same VMA range, but with the .data section mapped to a different 7552 1.1 christos LMA. */ 7553 1.1 christos #define SEGMENT_OVERLAPS(seg1, seg2) \ 7554 1.1 christos ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ 7555 1.1 christos || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ 7556 1.1 christos && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ 7557 1.14 christos || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) 7558 1.1 christos 7559 1.14 christos /* Initialise the segment mark field, and discard stupid alignment. */ 7560 1.14 christos for (section = ibfd->sections; section != NULL; section = section->next) 7561 1.14 christos { 7562 1.14 christos asection *o = section->output_section; 7563 1.14 christos if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1) 7564 1.14 christos o->alignment_power = 0; 7565 1.1 christos section->segment_mark = false; 7566 1.1 christos } 7567 1.1 christos 7568 1.1 christos /* The Solaris linker creates program headers in which all the 7569 1.1 christos p_paddr fields are zero. When we try to objcopy or strip such a 7570 1.14 christos file, we get confused. Check for this case, and if we find it 7571 1.1 christos don't set the p_paddr_valid fields. */ 7572 1.1 christos p_paddr_valid = false; 7573 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr; 7574 1.1 christos i < num_segments; 7575 1.1 christos i++, segment++) 7576 1.14 christos if (segment->p_paddr != 0) 7577 1.1 christos { 7578 1.1 christos p_paddr_valid = true; 7579 1.1 christos break; 7580 1.1 christos } 7581 1.1 christos 7582 1.1 christos /* Scan through the segments specified in the program header 7583 1.1 christos of the input BFD. For this first scan we look for overlaps 7584 1.1 christos in the loadable segments. These can be created by weird 7585 1.1 christos parameters to objcopy. Also, fix some solaris weirdness. */ 7586 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr; 7587 1.1 christos i < num_segments; 7588 1.1 christos i++, segment++) 7589 1.1 christos { 7590 1.1 christos unsigned int j; 7591 1.1 christos Elf_Internal_Phdr *segment2; 7592 1.1 christos 7593 1.1 christos if (segment->p_type == PT_INTERP) 7594 1.1 christos for (section = ibfd->sections; section; section = section->next) 7595 1.1 christos if (IS_SOLARIS_PT_INTERP (segment, section)) 7596 1.1 christos { 7597 1.12 christos /* Mininal change so that the normal section to segment 7598 1.1 christos assignment code will work. */ 7599 1.1 christos segment->p_vaddr = section->vma * opb; 7600 1.1 christos break; 7601 1.1 christos } 7602 1.1 christos 7603 1.1 christos if (segment->p_type != PT_LOAD) 7604 1.1 christos { 7605 1.1 christos /* Remove PT_GNU_RELRO segment. */ 7606 1.1 christos if (segment->p_type == PT_GNU_RELRO) 7607 1.1 christos segment->p_type = PT_NULL; 7608 1.1 christos continue; 7609 1.1 christos } 7610 1.1 christos 7611 1.1 christos /* Determine if this segment overlaps any previous segments. */ 7612 1.1 christos for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) 7613 1.1 christos { 7614 1.1 christos bfd_signed_vma extra_length; 7615 1.1 christos 7616 1.1 christos if (segment2->p_type != PT_LOAD 7617 1.1 christos || !SEGMENT_OVERLAPS (segment, segment2)) 7618 1.1 christos continue; 7619 1.1 christos 7620 1.1 christos /* Merge the two segments together. */ 7621 1.1 christos if (segment2->p_vaddr < segment->p_vaddr) 7622 1.1 christos { 7623 1.14 christos /* Extend SEGMENT2 to include SEGMENT and then delete 7624 1.14 christos SEGMENT. */ 7625 1.1 christos extra_length = (segment_end (segment, segment->p_vaddr) 7626 1.1 christos - segment_end (segment2, segment2->p_vaddr)); 7627 1.1 christos 7628 1.1 christos if (extra_length > 0) 7629 1.1 christos { 7630 1.1 christos segment2->p_memsz += extra_length; 7631 1.1 christos segment2->p_filesz += extra_length; 7632 1.1 christos } 7633 1.1 christos 7634 1.1 christos segment->p_type = PT_NULL; 7635 1.1 christos 7636 1.1 christos /* Since we have deleted P we must restart the outer loop. */ 7637 1.1 christos i = 0; 7638 1.1 christos segment = elf_tdata (ibfd)->phdr; 7639 1.1 christos break; 7640 1.1 christos } 7641 1.1 christos else 7642 1.1 christos { 7643 1.14 christos /* Extend SEGMENT to include SEGMENT2 and then delete 7644 1.14 christos SEGMENT2. */ 7645 1.1 christos extra_length = (segment_end (segment2, segment2->p_vaddr) 7646 1.1 christos - segment_end (segment, segment->p_vaddr)); 7647 1.1 christos 7648 1.1 christos if (extra_length > 0) 7649 1.1 christos { 7650 1.1 christos segment->p_memsz += extra_length; 7651 1.1 christos segment->p_filesz += extra_length; 7652 1.1 christos } 7653 1.1 christos 7654 1.1 christos segment2->p_type = PT_NULL; 7655 1.1 christos } 7656 1.1 christos } 7657 1.1 christos } 7658 1.1 christos 7659 1.1 christos /* The second scan attempts to assign sections to segments. */ 7660 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr; 7661 1.1 christos i < num_segments; 7662 1.1 christos i++, segment++) 7663 1.1 christos { 7664 1.1 christos unsigned int section_count; 7665 1.1 christos asection **sections; 7666 1.11 christos asection *output_section; 7667 1.11 christos unsigned int isec; 7668 1.1 christos asection *matching_lma; 7669 1.12 christos asection *suggested_lma; 7670 1.1 christos unsigned int j; 7671 1.1 christos size_t amt; 7672 1.1 christos asection *first_section; 7673 1.1 christos 7674 1.1 christos if (segment->p_type == PT_NULL) 7675 1.1 christos continue; 7676 1.1 christos 7677 1.1 christos first_section = NULL; 7678 1.1 christos /* Compute how many sections might be placed into this segment. */ 7679 1.1 christos for (section = ibfd->sections, section_count = 0; 7680 1.1 christos section != NULL; 7681 1.1 christos section = section->next) 7682 1.1 christos { 7683 1.17 christos /* Find the first section in the input segment, which may be 7684 1.1 christos removed from the corresponding output segment. */ 7685 1.1 christos if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid)) 7686 1.1 christos { 7687 1.1 christos if (first_section == NULL) 7688 1.1 christos first_section = section; 7689 1.1 christos if (section->output_section != NULL) 7690 1.1 christos ++section_count; 7691 1.1 christos } 7692 1.1 christos } 7693 1.1 christos 7694 1.11 christos /* Allocate a segment map big enough to contain 7695 1.12 christos all of the sections we have selected. */ 7696 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *); 7697 1.1 christos amt += section_count * sizeof (asection *); 7698 1.14 christos map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); 7699 1.1 christos if (map == NULL) 7700 1.1 christos return false; 7701 1.1 christos 7702 1.1 christos /* Initialise the fields of the segment map. Default to 7703 1.1 christos using the physical address of the segment in the input BFD. */ 7704 1.1 christos map->next = NULL; 7705 1.1 christos map->p_type = segment->p_type; 7706 1.1 christos map->p_flags = segment->p_flags; 7707 1.14 christos map->p_flags_valid = 1; 7708 1.14 christos 7709 1.14 christos if (map->p_type == PT_LOAD 7710 1.14 christos && (ibfd->flags & D_PAGED) != 0 7711 1.14 christos && maxpagesize > 1 7712 1.14 christos && segment->p_align > 1) 7713 1.14 christos { 7714 1.14 christos map->p_align = segment->p_align; 7715 1.14 christos if (segment->p_align > maxpagesize) 7716 1.14 christos map->p_align = maxpagesize; 7717 1.14 christos map->p_align_valid = 1; 7718 1.1 christos } 7719 1.1 christos 7720 1.1 christos /* If the first section in the input segment is removed, there is 7721 1.1 christos no need to preserve segment physical address in the corresponding 7722 1.1 christos output segment. */ 7723 1.1 christos if (!first_section || first_section->output_section != NULL) 7724 1.1 christos { 7725 1.1 christos map->p_paddr = segment->p_paddr; 7726 1.1 christos map->p_paddr_valid = p_paddr_valid; 7727 1.1 christos } 7728 1.1 christos 7729 1.1 christos /* Determine if this segment contains the ELF file header 7730 1.1 christos and if it contains the program headers themselves. */ 7731 1.1 christos map->includes_filehdr = (segment->p_offset == 0 7732 1.1 christos && segment->p_filesz >= iehdr->e_ehsize); 7733 1.1 christos map->includes_phdrs = 0; 7734 1.1 christos 7735 1.1 christos if (!phdr_included || segment->p_type != PT_LOAD) 7736 1.1 christos { 7737 1.1 christos map->includes_phdrs = 7738 1.1 christos (segment->p_offset <= (bfd_vma) iehdr->e_phoff 7739 1.1 christos && (segment->p_offset + segment->p_filesz 7740 1.1 christos >= ((bfd_vma) iehdr->e_phoff 7741 1.1 christos + iehdr->e_phnum * iehdr->e_phentsize))); 7742 1.14 christos 7743 1.1 christos if (segment->p_type == PT_LOAD && map->includes_phdrs) 7744 1.1 christos phdr_included = true; 7745 1.1 christos } 7746 1.1 christos 7747 1.1 christos if (section_count == 0) 7748 1.1 christos { 7749 1.1 christos /* Special segments, such as the PT_PHDR segment, may contain 7750 1.11 christos no sections, but ordinary, loadable segments should contain 7751 1.17 christos something. They are allowed by the ELF spec however, so only 7752 1.9 christos a warning is produced. 7753 1.9 christos Don't warn if an empty PT_LOAD contains the program headers. 7754 1.9 christos There is however the valid use case of embedded systems which 7755 1.9 christos have segments with p_filesz of 0 and a p_memsz > 0 to initialize 7756 1.17 christos flash memory with zeros. No warning is shown for that case. */ 7757 1.9 christos if (segment->p_type == PT_LOAD 7758 1.9 christos && !map->includes_phdrs 7759 1.11 christos && (segment->p_filesz > 0 || segment->p_memsz == 0)) 7760 1.11 christos /* xgettext:c-format */ 7761 1.11 christos _bfd_error_handler 7762 1.11 christos (_("%pB: warning: empty loadable segment detected" 7763 1.1 christos " at vaddr=%#" PRIx64 ", is this intentional?"), 7764 1.12 christos ibfd, (uint64_t) segment->p_vaddr); 7765 1.1 christos 7766 1.1 christos map->p_vaddr_offset = segment->p_vaddr / opb; 7767 1.1 christos map->count = 0; 7768 1.1 christos *pointer_to_map = map; 7769 1.1 christos pointer_to_map = &map->next; 7770 1.1 christos 7771 1.1 christos continue; 7772 1.1 christos } 7773 1.1 christos 7774 1.1 christos /* Now scan the sections in the input BFD again and attempt 7775 1.1 christos to add their corresponding output sections to the segment map. 7776 1.1 christos The problem here is how to handle an output section which has 7777 1.1 christos been moved (ie had its LMA changed). There are four possibilities: 7778 1.1 christos 7779 1.1 christos 1. None of the sections have been moved. 7780 1.1 christos In this case we can continue to use the segment LMA from the 7781 1.1 christos input BFD. 7782 1.1 christos 7783 1.1 christos 2. All of the sections have been moved by the same amount. 7784 1.1 christos In this case we can change the segment's LMA to match the LMA 7785 1.1 christos of the first section. 7786 1.1 christos 7787 1.1 christos 3. Some of the sections have been moved, others have not. 7788 1.1 christos In this case those sections which have not been moved can be 7789 1.1 christos placed in the current segment which will have to have its size, 7790 1.1 christos and possibly its LMA changed, and a new segment or segments will 7791 1.1 christos have to be created to contain the other sections. 7792 1.1 christos 7793 1.1 christos 4. The sections have been moved, but not by the same amount. 7794 1.1 christos In this case we can change the segment's LMA to match the LMA 7795 1.1 christos of the first section and we will have to create a new segment 7796 1.1 christos or segments to contain the other sections. 7797 1.1 christos 7798 1.1 christos In order to save time, we allocate an array to hold the section 7799 1.1 christos pointers that we are interested in. As these sections get assigned 7800 1.12 christos to a segment, they are removed from this array. */ 7801 1.12 christos 7802 1.1 christos amt = section_count * sizeof (asection *); 7803 1.14 christos sections = (asection **) bfd_malloc (amt); 7804 1.1 christos if (sections == NULL) 7805 1.1 christos return false; 7806 1.1 christos 7807 1.1 christos /* Step One: Scan for segment vs section LMA conflicts. 7808 1.1 christos Also add the sections to the section array allocated above. 7809 1.1 christos Also add the sections to the current segment. In the common 7810 1.1 christos case, where the sections have not been moved, this means that 7811 1.1 christos we have completely filled the segment, and there is nothing 7812 1.11 christos more to do. */ 7813 1.11 christos isec = 0; 7814 1.1 christos matching_lma = NULL; 7815 1.8 christos suggested_lma = NULL; 7816 1.1 christos 7817 1.1 christos for (section = first_section, j = 0; 7818 1.1 christos section != NULL; 7819 1.17 christos section = section->next) 7820 1.1 christos { 7821 1.1 christos if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid)) 7822 1.1 christos { 7823 1.1 christos output_section = section->output_section; 7824 1.1 christos 7825 1.1 christos sections[j++] = section; 7826 1.1 christos 7827 1.1 christos /* The Solaris native linker always sets p_paddr to 0. 7828 1.1 christos We try to catch that case here, and set it to the 7829 1.1 christos correct value. Note - some backends require that 7830 1.1 christos p_paddr be left as zero. */ 7831 1.1 christos if (!p_paddr_valid 7832 1.1 christos && segment->p_vaddr != 0 7833 1.1 christos && !bed->want_p_paddr_set_to_zero 7834 1.11 christos && isec == 0 7835 1.11 christos && output_section->lma != 0 7836 1.11 christos && (align_power (segment->p_vaddr 7837 1.11 christos + (map->includes_filehdr 7838 1.11 christos ? iehdr->e_ehsize : 0) 7839 1.11 christos + (map->includes_phdrs 7840 1.12 christos ? iehdr->e_phnum * iehdr->e_phentsize 7841 1.12 christos : 0), 7842 1.1 christos output_section->alignment_power * opb) 7843 1.1 christos == (output_section->vma * opb))) 7844 1.1 christos map->p_paddr = segment->p_vaddr; 7845 1.1 christos 7846 1.14 christos /* Match up the physical address of the segment with the 7847 1.17 christos LMA address of the output section. */ 7848 1.14 christos if (is_contained_by (output_section, segment, map->p_paddr, 7849 1.1 christos 0, opb, false) 7850 1.11 christos || is_note (section, segment)) 7851 1.11 christos { 7852 1.11 christos if (matching_lma == NULL 7853 1.1 christos || output_section->lma < matching_lma->lma) 7854 1.1 christos matching_lma = output_section; 7855 1.1 christos 7856 1.1 christos /* We assume that if the section fits within the segment 7857 1.1 christos then it does not overlap any other section within that 7858 1.1 christos segment. */ 7859 1.11 christos map->sections[isec++] = output_section; 7860 1.11 christos } 7861 1.1 christos else if (suggested_lma == NULL) 7862 1.1 christos suggested_lma = output_section; 7863 1.1 christos 7864 1.1 christos if (j == section_count) 7865 1.1 christos break; 7866 1.1 christos } 7867 1.1 christos } 7868 1.1 christos 7869 1.1 christos BFD_ASSERT (j == section_count); 7870 1.1 christos 7871 1.1 christos /* Step Two: Adjust the physical address of the current segment, 7872 1.1 christos if necessary. */ 7873 1.1 christos if (isec == section_count) 7874 1.1 christos { 7875 1.1 christos /* All of the sections fitted within the segment as currently 7876 1.1 christos specified. This is the default case. Add the segment to 7877 1.1 christos the list of built segments and carry on to process the next 7878 1.1 christos program header in the input BFD. */ 7879 1.1 christos map->count = section_count; 7880 1.1 christos *pointer_to_map = map; 7881 1.1 christos pointer_to_map = &map->next; 7882 1.12 christos 7883 1.12 christos if (p_paddr_valid 7884 1.12 christos && !bed->want_p_paddr_set_to_zero) 7885 1.12 christos { 7886 1.12 christos bfd_vma hdr_size = 0; 7887 1.12 christos if (map->includes_filehdr) 7888 1.12 christos hdr_size = iehdr->e_ehsize; 7889 1.12 christos if (map->includes_phdrs) 7890 1.12 christos hdr_size += iehdr->e_phnum * iehdr->e_phentsize; 7891 1.12 christos 7892 1.12 christos /* Account for padding before the first section in the 7893 1.12 christos segment. */ 7894 1.12 christos map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb 7895 1.1 christos - matching_lma->lma); 7896 1.1 christos } 7897 1.1 christos 7898 1.1 christos free (sections); 7899 1.1 christos continue; 7900 1.1 christos } 7901 1.11 christos else 7902 1.11 christos { 7903 1.11 christos /* Change the current segment's physical address to match 7904 1.11 christos the LMA of the first section that fitted, or if no 7905 1.11 christos section fitted, the first section. */ 7906 1.11 christos if (matching_lma == NULL) 7907 1.12 christos matching_lma = suggested_lma; 7908 1.1 christos 7909 1.1 christos map->p_paddr = matching_lma->lma * opb; 7910 1.1 christos 7911 1.11 christos /* Offset the segment physical address from the lma 7912 1.1 christos to allow for space taken up by elf headers. */ 7913 1.11 christos if (map->includes_phdrs) 7914 1.11 christos { 7915 1.11 christos map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; 7916 1.11 christos 7917 1.11 christos /* iehdr->e_phnum is just an estimate of the number 7918 1.11 christos of program headers that we will need. Make a note 7919 1.11 christos here of the number we used and the segment we chose 7920 1.11 christos to hold these headers, so that we can adjust the 7921 1.11 christos offset when we know the correct value. */ 7922 1.1 christos phdr_adjust_num = iehdr->e_phnum; 7923 1.1 christos phdr_adjust_seg = map; 7924 1.11 christos } 7925 1.1 christos 7926 1.11 christos if (map->includes_filehdr) 7927 1.11 christos { 7928 1.11 christos bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power; 7929 1.11 christos map->p_paddr -= iehdr->e_ehsize; 7930 1.11 christos /* We've subtracted off the size of headers from the 7931 1.11 christos first section lma, but there may have been some 7932 1.11 christos alignment padding before that section too. Try to 7933 1.11 christos account for that by adjusting the segment lma down to 7934 1.11 christos the same alignment. */ 7935 1.12 christos if (segment->p_align != 0 && segment->p_align < align) 7936 1.1 christos align = segment->p_align; 7937 1.1 christos map->p_paddr &= -(align * opb); 7938 1.1 christos } 7939 1.1 christos } 7940 1.1 christos 7941 1.1 christos /* Step Three: Loop over the sections again, this time assigning 7942 1.1 christos those that fit to the current segment and removing them from the 7943 1.1 christos sections array; but making sure not to leave large gaps. Once all 7944 1.1 christos possible sections have been assigned to the current segment it is 7945 1.1 christos added to the list of built segments and if sections still remain 7946 1.1 christos to be assigned, a new segment is constructed before repeating 7947 1.1 christos the loop. */ 7948 1.1 christos isec = 0; 7949 1.1 christos do 7950 1.11 christos { 7951 1.1 christos map->count = 0; 7952 1.1 christos suggested_lma = NULL; 7953 1.1 christos 7954 1.1 christos /* Fill the current segment with sections that fit. */ 7955 1.1 christos for (j = 0; j < section_count; j++) 7956 1.1 christos { 7957 1.1 christos section = sections[j]; 7958 1.1 christos 7959 1.1 christos if (section == NULL) 7960 1.1 christos continue; 7961 1.1 christos 7962 1.1 christos output_section = section->output_section; 7963 1.1 christos 7964 1.14 christos BFD_ASSERT (output_section != NULL); 7965 1.17 christos 7966 1.14 christos if (is_contained_by (output_section, segment, map->p_paddr, 7967 1.1 christos 0, opb, false) 7968 1.1 christos || is_note (section, segment)) 7969 1.1 christos { 7970 1.1 christos if (map->count == 0) 7971 1.1 christos { 7972 1.1 christos /* If the first section in a segment does not start at 7973 1.11 christos the beginning of the segment, then something is 7974 1.11 christos wrong. */ 7975 1.11 christos if (align_power (map->p_paddr 7976 1.11 christos + (map->includes_filehdr 7977 1.11 christos ? iehdr->e_ehsize : 0) 7978 1.11 christos + (map->includes_phdrs 7979 1.12 christos ? iehdr->e_phnum * iehdr->e_phentsize 7980 1.12 christos : 0), 7981 1.12 christos output_section->alignment_power * opb) 7982 1.1 christos != output_section->lma * opb) 7983 1.1 christos goto sorry; 7984 1.1 christos } 7985 1.1 christos else 7986 1.1 christos { 7987 1.1 christos asection *prev_sec; 7988 1.1 christos 7989 1.1 christos prev_sec = map->sections[map->count - 1]; 7990 1.1 christos 7991 1.1 christos /* If the gap between the end of the previous section 7992 1.1 christos and the start of this section is more than 7993 1.1 christos maxpagesize then we need to start a new segment. */ 7994 1.1 christos if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, 7995 1.1 christos maxpagesize) 7996 1.1 christos < BFD_ALIGN (output_section->lma, maxpagesize)) 7997 1.1 christos || (prev_sec->lma + prev_sec->size 7998 1.11 christos > output_section->lma)) 7999 1.11 christos { 8000 1.1 christos if (suggested_lma == NULL) 8001 1.1 christos suggested_lma = output_section; 8002 1.1 christos 8003 1.1 christos continue; 8004 1.1 christos } 8005 1.1 christos } 8006 1.1 christos 8007 1.1 christos map->sections[map->count++] = output_section; 8008 1.11 christos ++isec; 8009 1.14 christos sections[j] = NULL; 8010 1.1 christos if (segment->p_type == PT_LOAD) 8011 1.11 christos section->segment_mark = true; 8012 1.11 christos } 8013 1.1 christos else if (suggested_lma == NULL) 8014 1.1 christos suggested_lma = output_section; 8015 1.11 christos } 8016 1.11 christos 8017 1.12 christos /* PR 23932. A corrupt input file may contain sections that cannot 8018 1.12 christos be assigned to any segment - because for example they have a 8019 1.12 christos negative size - or segments that do not contain any sections. 8020 1.1 christos But there are also valid reasons why a segment can be empty. 8021 1.1 christos So allow a count of zero. */ 8022 1.1 christos 8023 1.1 christos /* Add the current segment to the list of built segments. */ 8024 1.1 christos *pointer_to_map = map; 8025 1.1 christos pointer_to_map = &map->next; 8026 1.1 christos 8027 1.1 christos if (isec < section_count) 8028 1.1 christos { 8029 1.1 christos /* We still have not allocated all of the sections to 8030 1.11 christos segments. Create a new segment here, initialise it 8031 1.12 christos and carry on looping. */ 8032 1.3 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *); 8033 1.1 christos amt += section_count * sizeof (asection *); 8034 1.1 christos map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); 8035 1.1 christos if (map == NULL) 8036 1.14 christos { 8037 1.1 christos free (sections); 8038 1.1 christos return false; 8039 1.1 christos } 8040 1.1 christos 8041 1.1 christos /* Initialise the fields of the segment map. Set the physical 8042 1.1 christos physical address to the LMA of the first section that has 8043 1.1 christos not yet been assigned. */ 8044 1.1 christos map->next = NULL; 8045 1.1 christos map->p_type = segment->p_type; 8046 1.12 christos map->p_flags = segment->p_flags; 8047 1.1 christos map->p_flags_valid = 1; 8048 1.1 christos map->p_paddr = suggested_lma->lma * opb; 8049 1.1 christos map->p_paddr_valid = p_paddr_valid; 8050 1.1 christos map->includes_filehdr = 0; 8051 1.12 christos map->includes_phdrs = 0; 8052 1.12 christos } 8053 1.12 christos 8054 1.12 christos continue; 8055 1.12 christos sorry: 8056 1.14 christos bfd_set_error (bfd_error_sorry); 8057 1.1 christos free (sections); 8058 1.1 christos return false; 8059 1.1 christos } 8060 1.1 christos while (isec < section_count); 8061 1.1 christos 8062 1.1 christos free (sections); 8063 1.3 christos } 8064 1.1 christos 8065 1.1 christos elf_seg_map (obfd) = map_first; 8066 1.1 christos 8067 1.1 christos /* If we had to estimate the number of program headers that were 8068 1.1 christos going to be needed, then check our estimate now and adjust 8069 1.1 christos the offset if necessary. */ 8070 1.1 christos if (phdr_adjust_seg != NULL) 8071 1.1 christos { 8072 1.1 christos unsigned int count; 8073 1.1 christos 8074 1.1 christos for (count = 0, map = map_first; map != NULL; map = map->next) 8075 1.1 christos count++; 8076 1.1 christos 8077 1.1 christos if (count > phdr_adjust_num) 8078 1.11 christos phdr_adjust_seg->p_paddr 8079 1.11 christos -= (count - phdr_adjust_num) * iehdr->e_phentsize; 8080 1.11 christos 8081 1.11 christos for (map = map_first; map != NULL; map = map->next) 8082 1.11 christos if (map->p_type == PT_PHDR) 8083 1.11 christos { 8084 1.11 christos bfd_vma adjust 8085 1.11 christos = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0; 8086 1.11 christos map->p_paddr = phdr_adjust_seg->p_paddr + adjust; 8087 1.1 christos break; 8088 1.1 christos } 8089 1.1 christos } 8090 1.1 christos 8091 1.1 christos #undef IS_SOLARIS_PT_INTERP 8092 1.1 christos #undef IS_SECTION_IN_INPUT_SEGMENT 8093 1.1 christos #undef INCLUDE_SECTION_IN_SEGMENT 8094 1.14 christos #undef SEGMENT_AFTER_SEGMENT 8095 1.14 christos #undef SEGMENT_OVERLAPS 8096 1.14 christos return true; 8097 1.14 christos } 8098 1.14 christos 8099 1.14 christos /* Return true if p_align in the ELF program header in ABFD is valid. */ 8100 1.14 christos 8101 1.14 christos static bool 8102 1.14 christos elf_is_p_align_valid (bfd *abfd) 8103 1.14 christos { 8104 1.14 christos unsigned int i; 8105 1.14 christos Elf_Internal_Phdr *segment; 8106 1.14 christos unsigned int num_segments; 8107 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 8108 1.14 christos bfd_size_type maxpagesize = bed->maxpagesize; 8109 1.14 christos bfd_size_type p_align = bed->p_align; 8110 1.14 christos 8111 1.14 christos /* Return true if the default p_align value isn't set or the maximum 8112 1.14 christos page size is the same as the minimum page size. */ 8113 1.14 christos if (p_align == 0 || maxpagesize == bed->minpagesize) 8114 1.14 christos return true; 8115 1.14 christos 8116 1.14 christos /* When the default p_align value is set, p_align may be set to the 8117 1.14 christos default p_align value while segments are aligned to the maximum 8118 1.14 christos page size. In this case, the input p_align will be ignored and 8119 1.14 christos the maximum page size will be used to align the output segments. */ 8120 1.14 christos segment = elf_tdata (abfd)->phdr; 8121 1.14 christos num_segments = elf_elfheader (abfd)->e_phnum; 8122 1.14 christos for (i = 0; i < num_segments; i++, segment++) 8123 1.14 christos if (segment->p_type == PT_LOAD 8124 1.14 christos && (segment->p_align != p_align 8125 1.14 christos || vma_page_aligned_bias (segment->p_vaddr, 8126 1.14 christos segment->p_offset, 8127 1.14 christos maxpagesize) != 0)) 8128 1.14 christos return true; 8129 1.1 christos 8130 1.1 christos return false; 8131 1.1 christos } 8132 1.1 christos 8133 1.14 christos /* Copy ELF program header information. */ 8134 1.1 christos 8135 1.1 christos static bool 8136 1.1 christos copy_elf_program_header (bfd *ibfd, bfd *obfd) 8137 1.1 christos { 8138 1.1 christos Elf_Internal_Ehdr *iehdr; 8139 1.1 christos struct elf_segment_map *map; 8140 1.1 christos struct elf_segment_map *map_first; 8141 1.1 christos struct elf_segment_map **pointer_to_map; 8142 1.1 christos Elf_Internal_Phdr *segment; 8143 1.14 christos unsigned int i; 8144 1.14 christos unsigned int num_segments; 8145 1.14 christos bool phdr_included = false; 8146 1.12 christos bool p_paddr_valid; 8147 1.1 christos bool p_palign_valid; 8148 1.1 christos unsigned int opb = bfd_octets_per_byte (ibfd, NULL); 8149 1.1 christos 8150 1.1 christos iehdr = elf_elfheader (ibfd); 8151 1.1 christos 8152 1.1 christos map_first = NULL; 8153 1.1 christos pointer_to_map = &map_first; 8154 1.1 christos 8155 1.14 christos /* If all the segment p_paddr fields are zero, don't set 8156 1.1 christos map->p_paddr_valid. */ 8157 1.1 christos p_paddr_valid = false; 8158 1.1 christos num_segments = elf_elfheader (ibfd)->e_phnum; 8159 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr; 8160 1.1 christos i < num_segments; 8161 1.1 christos i++, segment++) 8162 1.14 christos if (segment->p_paddr != 0) 8163 1.1 christos { 8164 1.1 christos p_paddr_valid = true; 8165 1.1 christos break; 8166 1.14 christos } 8167 1.14 christos 8168 1.1 christos p_palign_valid = elf_is_p_align_valid (ibfd); 8169 1.1 christos 8170 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr; 8171 1.1 christos i < num_segments; 8172 1.1 christos i++, segment++) 8173 1.1 christos { 8174 1.12 christos asection *section; 8175 1.1 christos unsigned int section_count; 8176 1.1 christos size_t amt; 8177 1.1 christos Elf_Internal_Shdr *this_hdr; 8178 1.1 christos asection *first_section = NULL; 8179 1.1 christos asection *lowest_section; 8180 1.1 christos 8181 1.1 christos /* Compute how many sections are in this segment. */ 8182 1.1 christos for (section = ibfd->sections, section_count = 0; 8183 1.1 christos section != NULL; 8184 1.1 christos section = section->next) 8185 1.1 christos { 8186 1.1 christos this_hdr = &(elf_section_data(section)->this_hdr); 8187 1.1 christos if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) 8188 1.1 christos { 8189 1.1 christos if (first_section == NULL) 8190 1.1 christos first_section = section; 8191 1.1 christos section_count++; 8192 1.1 christos } 8193 1.1 christos } 8194 1.1 christos 8195 1.11 christos /* Allocate a segment map big enough to contain 8196 1.12 christos all of the sections we have selected. */ 8197 1.1 christos amt = sizeof (struct elf_segment_map) - sizeof (asection *); 8198 1.1 christos amt += section_count * sizeof (asection *); 8199 1.14 christos map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); 8200 1.1 christos if (map == NULL) 8201 1.1 christos return false; 8202 1.1 christos 8203 1.1 christos /* Initialize the fields of the output segment map with the 8204 1.1 christos input segment. */ 8205 1.1 christos map->next = NULL; 8206 1.1 christos map->p_type = segment->p_type; 8207 1.1 christos map->p_flags = segment->p_flags; 8208 1.1 christos map->p_flags_valid = 1; 8209 1.1 christos map->p_paddr = segment->p_paddr; 8210 1.14 christos map->p_paddr_valid = p_paddr_valid; 8211 1.14 christos map->p_align = segment->p_align; 8212 1.14 christos /* Keep p_align of PT_GNU_STACK for stack alignment. */ 8213 1.1 christos map->p_align_valid = (map->p_type == PT_GNU_STACK 8214 1.1 christos || p_palign_valid); 8215 1.3 christos map->p_vaddr_offset = 0; 8216 1.3 christos 8217 1.1 christos if (map->p_type == PT_GNU_RELRO 8218 1.1 christos || map->p_type == PT_GNU_STACK) 8219 1.1 christos { 8220 1.1 christos /* The PT_GNU_RELRO segment may contain the first a few 8221 1.3 christos bytes in the .got.plt section even if the whole .got.plt 8222 1.3 christos section isn't in the PT_GNU_RELRO segment. We won't 8223 1.3 christos change the size of the PT_GNU_RELRO segment. 8224 1.1 christos Similarly, PT_GNU_STACK size is significant on uclinux 8225 1.1 christos systems. */ 8226 1.1 christos map->p_size = segment->p_memsz; 8227 1.1 christos map->p_size_valid = 1; 8228 1.1 christos } 8229 1.1 christos 8230 1.1 christos /* Determine if this segment contains the ELF file header 8231 1.1 christos and if it contains the program headers themselves. */ 8232 1.1 christos map->includes_filehdr = (segment->p_offset == 0 8233 1.1 christos && segment->p_filesz >= iehdr->e_ehsize); 8234 1.1 christos 8235 1.1 christos map->includes_phdrs = 0; 8236 1.1 christos if (! phdr_included || segment->p_type != PT_LOAD) 8237 1.1 christos { 8238 1.1 christos map->includes_phdrs = 8239 1.1 christos (segment->p_offset <= (bfd_vma) iehdr->e_phoff 8240 1.1 christos && (segment->p_offset + segment->p_filesz 8241 1.1 christos >= ((bfd_vma) iehdr->e_phoff 8242 1.1 christos + iehdr->e_phnum * iehdr->e_phentsize))); 8243 1.14 christos 8244 1.1 christos if (segment->p_type == PT_LOAD && map->includes_phdrs) 8245 1.1 christos phdr_included = true; 8246 1.5 christos } 8247 1.1 christos 8248 1.1 christos lowest_section = NULL; 8249 1.1 christos if (section_count != 0) 8250 1.1 christos { 8251 1.1 christos unsigned int isec = 0; 8252 1.1 christos 8253 1.1 christos for (section = first_section; 8254 1.1 christos section != NULL; 8255 1.1 christos section = section->next) 8256 1.1 christos { 8257 1.1 christos this_hdr = &(elf_section_data(section)->this_hdr); 8258 1.1 christos if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) 8259 1.1 christos { 8260 1.1 christos map->sections[isec++] = section->output_section; 8261 1.1 christos if ((section->flags & SEC_ALLOC) != 0) 8262 1.1 christos { 8263 1.5 christos bfd_vma seg_off; 8264 1.5 christos 8265 1.4 christos if (lowest_section == NULL 8266 1.4 christos || section->lma < lowest_section->lma) 8267 1.1 christos lowest_section = section; 8268 1.1 christos 8269 1.1 christos /* Section lmas are set up from PT_LOAD header 8270 1.1 christos p_paddr in _bfd_elf_make_section_from_shdr. 8271 1.1 christos If this header has a p_paddr that disagrees 8272 1.1 christos with the section lma, flag the p_paddr as 8273 1.1 christos invalid. */ 8274 1.1 christos if ((section->flags & SEC_LOAD) != 0) 8275 1.1 christos seg_off = this_hdr->sh_offset - segment->p_offset; 8276 1.12 christos else 8277 1.14 christos seg_off = this_hdr->sh_addr - segment->p_vaddr; 8278 1.1 christos if (section->lma * opb - segment->p_paddr != seg_off) 8279 1.1 christos map->p_paddr_valid = false; 8280 1.1 christos } 8281 1.1 christos if (isec == section_count) 8282 1.1 christos break; 8283 1.1 christos } 8284 1.1 christos } 8285 1.12 christos } 8286 1.12 christos 8287 1.12 christos if (section_count == 0) 8288 1.11 christos map->p_vaddr_offset = segment->p_vaddr / opb; 8289 1.12 christos else if (map->p_paddr_valid) 8290 1.12 christos { 8291 1.12 christos /* Account for padding before the first section in the segment. */ 8292 1.12 christos bfd_vma hdr_size = 0; 8293 1.12 christos if (map->includes_filehdr) 8294 1.12 christos hdr_size = iehdr->e_ehsize; 8295 1.12 christos if (map->includes_phdrs) 8296 1.12 christos hdr_size += iehdr->e_phnum * iehdr->e_phentsize; 8297 1.12 christos 8298 1.11 christos map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb 8299 1.3 christos - (lowest_section ? lowest_section->lma : 0)); 8300 1.1 christos } 8301 1.1 christos 8302 1.1 christos map->count = section_count; 8303 1.1 christos *pointer_to_map = map; 8304 1.1 christos pointer_to_map = &map->next; 8305 1.3 christos } 8306 1.14 christos 8307 1.1 christos elf_seg_map (obfd) = map_first; 8308 1.1 christos return true; 8309 1.1 christos } 8310 1.1 christos 8311 1.1 christos /* Copy private BFD data. This copies or rewrites ELF program header 8312 1.14 christos information. */ 8313 1.1 christos 8314 1.1 christos static bool 8315 1.14 christos copy_private_bfd_data (bfd *ibfd, bfd *obfd) 8316 1.14 christos { 8317 1.1 christos bfd_vma maxpagesize; 8318 1.1 christos 8319 1.14 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 8320 1.1 christos || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 8321 1.1 christos return true; 8322 1.14 christos 8323 1.1 christos if (elf_tdata (ibfd)->phdr == NULL) 8324 1.1 christos return true; 8325 1.1 christos 8326 1.1 christos if (ibfd->xvec == obfd->xvec) 8327 1.1 christos { 8328 1.1 christos /* Check to see if any sections in the input BFD 8329 1.17 christos covered by ELF program header have changed. */ 8330 1.17 christos Elf_Internal_Phdr *segment; 8331 1.17 christos asection * section; 8332 1.1 christos asection * osec; 8333 1.1 christos asection * prev; 8334 1.1 christos unsigned int i, num_segments; 8335 1.1 christos Elf_Internal_Shdr *this_hdr; 8336 1.1 christos const struct elf_backend_data *bed; 8337 1.1 christos 8338 1.1 christos bed = get_elf_backend_data (ibfd); 8339 1.1 christos 8340 1.1 christos /* Regenerate the segment map if p_paddr is set to 0. */ 8341 1.1 christos if (bed->want_p_paddr_set_to_zero) 8342 1.1 christos goto rewrite; 8343 1.1 christos 8344 1.1 christos /* Initialize the segment mark field. */ 8345 1.14 christos for (section = obfd->sections; section != NULL; 8346 1.1 christos section = section->next) 8347 1.1 christos section->segment_mark = false; 8348 1.1 christos 8349 1.1 christos num_segments = elf_elfheader (ibfd)->e_phnum; 8350 1.1 christos for (i = 0, segment = elf_tdata (ibfd)->phdr; 8351 1.1 christos i < num_segments; 8352 1.1 christos i++, segment++) 8353 1.1 christos { 8354 1.1 christos /* PR binutils/3535. The Solaris linker always sets the p_paddr 8355 1.1 christos and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 8356 1.1 christos which severly confuses things, so always regenerate the segment 8357 1.1 christos map in this case. */ 8358 1.14 christos if (segment->p_paddr == 0 8359 1.14 christos && segment->p_memsz == 0 8360 1.1 christos && (segment->p_type == PT_INTERP 8361 1.1 christos || segment->p_type == PT_DYNAMIC)) 8362 1.17 christos goto rewrite; 8363 1.1 christos 8364 1.1 christos for (section = ibfd->sections, prev = NULL; 8365 1.1 christos section != NULL; section = section->next) 8366 1.1 christos { 8367 1.1 christos /* We mark the output section so that we know it comes 8368 1.1 christos from the input BFD. */ 8369 1.14 christos osec = section->output_section; 8370 1.1 christos if (osec) 8371 1.1 christos osec->segment_mark = true; 8372 1.1 christos 8373 1.1 christos /* Check if this section is covered by the segment. */ 8374 1.1 christos this_hdr = &(elf_section_data(section)->this_hdr); 8375 1.1 christos if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) 8376 1.1 christos { 8377 1.1 christos /* FIXME: Check if its output section is changed or 8378 1.1 christos removed. What else do we need to check? */ 8379 1.1 christos if (osec == NULL 8380 1.1 christos || section->flags != osec->flags 8381 1.1 christos || section->lma != osec->lma 8382 1.1 christos || section->vma != osec->vma 8383 1.1 christos || section->size != osec->size 8384 1.1 christos || section->rawsize != osec->rawsize 8385 1.17 christos || section->alignment_power != osec->alignment_power) 8386 1.17 christos goto rewrite; 8387 1.17 christos 8388 1.17 christos /* PR 31450: If this is an allocated section then make sure 8389 1.17 christos that this section's vma to lma relationship is the same 8390 1.17 christos as previous (allocated) section's. */ 8391 1.17 christos if (prev != NULL 8392 1.17 christos && section->flags & SEC_ALLOC 8393 1.17 christos && section->lma - section->vma != prev->lma - prev->vma) 8394 1.17 christos goto rewrite; 8395 1.17 christos 8396 1.1 christos if (section->flags & SEC_ALLOC) 8397 1.1 christos prev = section; 8398 1.1 christos } 8399 1.1 christos } 8400 1.1 christos } 8401 1.1 christos 8402 1.1 christos /* Check to see if any output section do not come from the 8403 1.1 christos input BFD. */ 8404 1.1 christos for (section = obfd->sections; section != NULL; 8405 1.11 christos section = section->next) 8406 1.1 christos { 8407 1.1 christos if (!section->segment_mark) 8408 1.14 christos goto rewrite; 8409 1.1 christos else 8410 1.1 christos section->segment_mark = false; 8411 1.1 christos } 8412 1.1 christos 8413 1.1 christos return copy_elf_program_header (ibfd, obfd); 8414 1.12 christos } 8415 1.14 christos 8416 1.3 christos rewrite: 8417 1.3 christos maxpagesize = 0; 8418 1.3 christos if (ibfd->xvec == obfd->xvec) 8419 1.3 christos { 8420 1.3 christos /* When rewriting program header, set the output maxpagesize to 8421 1.3 christos the maximum alignment of input PT_LOAD segments. */ 8422 1.3 christos Elf_Internal_Phdr *segment; 8423 1.3 christos unsigned int i; 8424 1.3 christos unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; 8425 1.3 christos 8426 1.3 christos for (i = 0, segment = elf_tdata (ibfd)->phdr; 8427 1.3 christos i < num_segments; 8428 1.3 christos i++, segment++) 8429 1.6 christos if (segment->p_type == PT_LOAD 8430 1.6 christos && maxpagesize < segment->p_align) 8431 1.6 christos { 8432 1.9 christos /* PR 17512: file: f17299af. */ 8433 1.11 christos if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) 8434 1.11 christos /* xgettext:c-format */ 8435 1.11 christos _bfd_error_handler (_("%pB: warning: segment alignment of %#" 8436 1.6 christos PRIx64 " is too large"), 8437 1.6 christos ibfd, (uint64_t) segment->p_align); 8438 1.6 christos else 8439 1.3 christos maxpagesize = segment->p_align; 8440 1.14 christos } 8441 1.14 christos } 8442 1.3 christos if (maxpagesize == 0) 8443 1.14 christos maxpagesize = get_elf_backend_data (obfd)->maxpagesize; 8444 1.1 christos 8445 1.1 christos return rewrite_elf_program_header (ibfd, obfd, maxpagesize); 8446 1.19 christos } 8447 1.19 christos 8448 1.19 christos /* Copy private section information from input to output. This function 8449 1.19 christos is called both by objcopy where LINK_INFO is NULL, and ld where 8450 1.1 christos LINK_INFO is non-NULL in the usual case but NULL for a special case 8451 1.14 christos when dealing with LTO IR or cloning output sections. */ 8452 1.19 christos 8453 1.1 christos bool 8454 1.1 christos _bfd_elf_copy_private_section_data (bfd *ibfd, 8455 1.1 christos asection *isec, 8456 1.1 christos bfd *obfd, 8457 1.1 christos asection *osec, 8458 1.1 christos struct bfd_link_info *link_info) 8459 1.1 christos { 8460 1.14 christos if (ibfd->xvec->flavour != bfd_target_elf_flavour 8461 1.1 christos || obfd->xvec->flavour != bfd_target_elf_flavour) 8462 1.19 christos return true; 8463 1.19 christos 8464 1.19 christos Elf_Internal_Shdr *ihdr = &elf_section_data (isec)->this_hdr; 8465 1.19 christos Elf_Internal_Shdr *ohdr = &elf_section_data (osec)->this_hdr; 8466 1.19 christos if (link_info == NULL) 8467 1.19 christos { 8468 1.19 christos ohdr->sh_entsize = ihdr->sh_entsize; 8469 1.19 christos 8470 1.19 christos if (ihdr->sh_type == SHT_SYMTAB 8471 1.19 christos || ihdr->sh_type == SHT_DYNSYM 8472 1.19 christos || ihdr->sh_type == SHT_GNU_verneed 8473 1.19 christos || ihdr->sh_type == SHT_GNU_verdef) 8474 1.3 christos ohdr->sh_info = ihdr->sh_info; 8475 1.12 christos } 8476 1.12 christos 8477 1.12 christos /* If this is a known ABI section, ELF section type and flags may 8478 1.12 christos have been set up when OSEC was created. For normal sections we 8479 1.12 christos allow the user to override the type and flags other than 8480 1.12 christos SHF_MASKOS and SHF_MASKPROC. */ 8481 1.12 christos if (elf_section_type (osec) == SHT_PROGBITS 8482 1.12 christos || elf_section_type (osec) == SHT_NOTE 8483 1.19 christos || elf_section_type (osec) == SHT_NOBITS) 8484 1.12 christos elf_section_type (osec) = SHT_NULL; 8485 1.12 christos 8486 1.12 christos /* For objcopy and relocatable link, copy the ELF section type from 8487 1.12 christos the input file if the BFD section flags are the same. (If they 8488 1.12 christos are different the user may be doing something like 8489 1.19 christos "objcopy --set-section-flags .text=alloc,data".) For a final 8490 1.19 christos link allow some flags that the linker clears to differ. */ 8491 1.1 christos bool final_link = (link_info != NULL 8492 1.1 christos && !bfd_link_relocatable (link_info)); 8493 1.1 christos if (elf_section_type (osec) == SHT_NULL 8494 1.1 christos && (osec->flags == isec->flags 8495 1.1 christos || (final_link 8496 1.1 christos && ((osec->flags ^ isec->flags) 8497 1.1 christos & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) 8498 1.19 christos elf_section_type (osec) = elf_section_type (isec); 8499 1.19 christos 8500 1.19 christos elf_section_flags (osec) = elf_section_flags (isec); 8501 1.19 christos /* Like for type, retain flags for objcopy (yet unlike for type, don't do so 8502 1.19 christos for relocatable link). Same heuristic as there: If the BFD section flags 8503 1.19 christos are different, assume --set-section-flags is in use for the section. 8504 1.19 christos 8505 1.19 christos FIXME: Is this correct for all OS/PROC specific flags? */ 8506 1.19 christos if (link_info != NULL || osec->flags != isec->flags) 8507 1.19 christos elf_section_flags (osec) &= (SHF_MASKOS | SHF_MASKPROC); 8508 1.19 christos else 8509 1.19 christos { 8510 1.19 christos /* Clear only flags which are set below or elsewhere. */ 8511 1.19 christos elf_section_flags (osec) &= ~(SHF_WRITE | SHF_ALLOC | SHF_EXECINSTR 8512 1.19 christos | SHF_MERGE | SHF_STRINGS | SHF_LINK_ORDER 8513 1.19 christos | SHF_INFO_LINK | SHF_GROUP | SHF_TLS 8514 1.19 christos | SHF_COMPRESSED); 8515 1.19 christos if (elf_section_flags (osec) & ~(SHF_MASKOS | SHF_MASKPROC)) 8516 1.19 christos _bfd_error_handler 8517 1.19 christos (_("%pB:%pA: warning: retaining unknown section flag(s) %#" PRIx64), 8518 1.19 christos ibfd, isec, 8519 1.19 christos (uint64_t) (elf_section_flags (osec) 8520 1.1 christos & ~(SHF_MASKOS | SHF_MASKPROC))); 8521 1.9 christos } 8522 1.12 christos 8523 1.12 christos /* Copy sh_info from input for mbind section. */ 8524 1.9 christos if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0 8525 1.9 christos && elf_section_flags (isec) & SHF_GNU_MBIND) 8526 1.9 christos elf_section_data (osec)->this_hdr.sh_info 8527 1.1 christos = elf_section_data (isec)->this_hdr.sh_info; 8528 1.1 christos 8529 1.1 christos /* Set things up for objcopy and relocatable link. The output 8530 1.1 christos SHT_GROUP section will have its elf_next_in_group pointing back 8531 1.11 christos to the input group members. Ignore linker created group section. 8532 1.11 christos See elfNN_ia64_object_p in elfxx-ia64.c. */ 8533 1.11 christos if ((link_info == NULL 8534 1.11 christos || !link_info->resolve_section_groups) 8535 1.11 christos && (elf_sec_group (isec) == NULL 8536 1.11 christos || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)) 8537 1.11 christos { 8538 1.11 christos if (elf_section_flags (isec) & SHF_GROUP) 8539 1.11 christos elf_section_flags (osec) |= SHF_GROUP; 8540 1.11 christos elf_next_in_group (osec) = elf_next_in_group (isec); 8541 1.6 christos elf_section_data (osec)->group = elf_section_data (isec)->group; 8542 1.11 christos } 8543 1.11 christos 8544 1.11 christos /* If not decompress, preserve SHF_COMPRESSED. */ 8545 1.11 christos if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0) 8546 1.1 christos elf_section_flags (osec) |= (elf_section_flags (isec) 8547 1.1 christos & SHF_COMPRESSED); 8548 1.1 christos 8549 1.1 christos /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We 8550 1.1 christos don't use the output section of the linked-to section since it 8551 1.1 christos may be NULL at this point. */ 8552 1.1 christos if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) 8553 1.1 christos { 8554 1.1 christos ohdr->sh_flags |= SHF_LINK_ORDER; 8555 1.1 christos elf_linked_to_section (osec) = elf_linked_to_section (isec); 8556 1.1 christos } 8557 1.1 christos 8558 1.14 christos osec->use_rela_p = isec->use_rela_p; 8559 1.1 christos 8560 1.1 christos return true; 8561 1.1 christos } 8562 1.1 christos 8563 1.1 christos /* Look at all the SHT_GROUP sections in IBFD, making any adjustments 8564 1.1 christos necessary if we are removing either the SHT_GROUP section or any of 8565 1.1 christos the group member sections. DISCARDED is the value that a section's 8566 1.1 christos output_section has if the section will be discarded, NULL when this 8567 1.1 christos function is called from objcopy, bfd_abs_section_ptr when called 8568 1.14 christos from the linker. */ 8569 1.1 christos 8570 1.1 christos bool 8571 1.1 christos _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) 8572 1.1 christos { 8573 1.1 christos asection *isec; 8574 1.1 christos 8575 1.1 christos for (isec = ibfd->sections; isec != NULL; isec = isec->next) 8576 1.1 christos if (elf_section_type (isec) == SHT_GROUP) 8577 1.1 christos { 8578 1.1 christos asection *first = elf_next_in_group (isec); 8579 1.1 christos asection *s = first; 8580 1.1 christos bfd_size_type removed = 0; 8581 1.1 christos 8582 1.1 christos while (s != NULL) 8583 1.1 christos { 8584 1.1 christos /* If this member section is being output but the 8585 1.1 christos SHT_GROUP section is not, then clear the group info 8586 1.1 christos set up by _bfd_elf_copy_private_section_data. */ 8587 1.1 christos if (s->output_section != discarded 8588 1.1 christos && isec->output_section == discarded) 8589 1.1 christos { 8590 1.1 christos elf_section_flags (s->output_section) &= ~SHF_GROUP; 8591 1.12 christos elf_group_name (s->output_section) = NULL; 8592 1.11 christos } 8593 1.11 christos else 8594 1.12 christos { 8595 1.12 christos struct bfd_elf_section_data *elf_sec = elf_section_data (s); 8596 1.12 christos if (s->output_section == discarded 8597 1.12 christos && isec->output_section != discarded) 8598 1.12 christos { 8599 1.12 christos /* Conversely, if the member section is not being 8600 1.12 christos output but the SHT_GROUP section is, then adjust 8601 1.12 christos its size. */ 8602 1.12 christos removed += 4; 8603 1.12 christos if (elf_sec->rel.hdr != NULL 8604 1.12 christos && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0) 8605 1.12 christos removed += 4; 8606 1.12 christos if (elf_sec->rela.hdr != NULL 8607 1.12 christos && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0) 8608 1.12 christos removed += 4; 8609 1.12 christos } 8610 1.12 christos else 8611 1.12 christos { 8612 1.12 christos /* Also adjust for zero-sized relocation member 8613 1.12 christos section. */ 8614 1.12 christos if (elf_sec->rel.hdr != NULL 8615 1.12 christos && elf_sec->rel.hdr->sh_size == 0) 8616 1.12 christos removed += 4; 8617 1.12 christos if (elf_sec->rela.hdr != NULL 8618 1.12 christos && elf_sec->rela.hdr->sh_size == 0) 8619 1.11 christos removed += 4; 8620 1.1 christos } 8621 1.1 christos } 8622 1.1 christos s = elf_next_in_group (s); 8623 1.1 christos if (s == first) 8624 1.1 christos break; 8625 1.1 christos } 8626 1.1 christos if (removed != 0) 8627 1.1 christos { 8628 1.1 christos if (discarded != NULL) 8629 1.11 christos { 8630 1.1 christos /* If we've been called for ld -r, then we need to 8631 1.1 christos adjust the input section size. */ 8632 1.1 christos if (isec->rawsize == 0) 8633 1.11 christos isec->rawsize = isec->size; 8634 1.11 christos isec->size = isec->rawsize - removed; 8635 1.11 christos if (isec->size <= 4) 8636 1.11 christos { 8637 1.11 christos isec->size = 0; 8638 1.1 christos isec->flags |= SEC_EXCLUDE; 8639 1.14 christos } 8640 1.1 christos } 8641 1.1 christos else if (isec->output_section != NULL) 8642 1.1 christos { 8643 1.1 christos /* Adjust the output section size when called from 8644 1.11 christos objcopy. */ 8645 1.11 christos isec->output_section->size -= removed; 8646 1.11 christos if (isec->output_section->size <= 4) 8647 1.11 christos { 8648 1.11 christos isec->output_section->size = 0; 8649 1.1 christos isec->output_section->flags |= SEC_EXCLUDE; 8650 1.1 christos } 8651 1.1 christos } 8652 1.1 christos } 8653 1.14 christos } 8654 1.1 christos 8655 1.1 christos return true; 8656 1.1 christos } 8657 1.1 christos 8658 1.14 christos /* Copy private header information. */ 8659 1.1 christos 8660 1.1 christos bool 8661 1.1 christos _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) 8662 1.1 christos { 8663 1.14 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 8664 1.1 christos || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 8665 1.1 christos return true; 8666 1.1 christos 8667 1.1 christos /* Copy over private BFD data if it has not already been copied. 8668 1.1 christos This must be done here, rather than in the copy_private_bfd_data 8669 1.1 christos entry point, because the latter is called after the section 8670 1.3 christos contents have been set, which means that the program headers have 8671 1.1 christos already been worked out. */ 8672 1.1 christos if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) 8673 1.14 christos { 8674 1.1 christos if (! copy_private_bfd_data (ibfd, obfd)) 8675 1.1 christos return false; 8676 1.1 christos } 8677 1.1 christos 8678 1.1 christos return _bfd_elf_fixup_group_sections (ibfd, NULL); 8679 1.1 christos } 8680 1.1 christos 8681 1.1 christos /* Copy private symbol information. If this symbol is in a section 8682 1.1 christos which we did not map into a BFD section, try to map the section 8683 1.1 christos index correctly. We use special macro definitions for the mapped 8684 1.1 christos section indices; these definitions are interpreted by the 8685 1.1 christos swap_out_syms function. */ 8686 1.1 christos 8687 1.1 christos #define MAP_ONESYMTAB (SHN_HIOS + 1) 8688 1.1 christos #define MAP_DYNSYMTAB (SHN_HIOS + 2) 8689 1.1 christos #define MAP_STRTAB (SHN_HIOS + 3) 8690 1.1 christos #define MAP_SHSTRTAB (SHN_HIOS + 4) 8691 1.14 christos #define MAP_SYM_SHNDX (SHN_HIOS + 5) 8692 1.1 christos 8693 1.1 christos bool 8694 1.1 christos _bfd_elf_copy_private_symbol_data (bfd *ibfd, 8695 1.1 christos asymbol *isymarg, 8696 1.1 christos bfd *obfd, 8697 1.1 christos asymbol *osymarg) 8698 1.1 christos { 8699 1.1 christos elf_symbol_type *isym, *osym; 8700 1.1 christos 8701 1.14 christos if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 8702 1.1 christos || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 8703 1.14 christos return true; 8704 1.14 christos 8705 1.1 christos isym = elf_symbol_from (isymarg); 8706 1.1 christos osym = elf_symbol_from (osymarg); 8707 1.1 christos 8708 1.1 christos if (isym != NULL 8709 1.1 christos && isym->internal_elf_sym.st_shndx != 0 8710 1.1 christos && osym != NULL 8711 1.1 christos && bfd_is_abs_section (isym->symbol.section)) 8712 1.1 christos { 8713 1.1 christos unsigned int shndx; 8714 1.1 christos 8715 1.1 christos shndx = isym->internal_elf_sym.st_shndx; 8716 1.1 christos if (shndx == elf_onesymtab (ibfd)) 8717 1.1 christos shndx = MAP_ONESYMTAB; 8718 1.17 christos else if (shndx == elf_dynsymtab (ibfd)) 8719 1.1 christos shndx = MAP_DYNSYMTAB; 8720 1.17 christos else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link) 8721 1.1 christos shndx = MAP_STRTAB; 8722 1.8 christos else if (shndx == elf_elfheader (ibfd)->e_shstrndx) 8723 1.1 christos shndx = MAP_SHSTRTAB; 8724 1.1 christos else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) 8725 1.1 christos shndx = MAP_SYM_SHNDX; 8726 1.1 christos osym->internal_elf_sym.st_shndx = shndx; 8727 1.14 christos } 8728 1.1 christos 8729 1.1 christos return true; 8730 1.1 christos } 8731 1.1 christos 8732 1.14 christos /* Swap out the symbols. */ 8733 1.1 christos 8734 1.6 christos static bool 8735 1.14 christos swap_out_syms (bfd *abfd, 8736 1.14 christos struct elf_strtab_hash **sttp, 8737 1.1 christos int relocatable_p, 8738 1.1 christos struct bfd_link_info *info) 8739 1.12 christos { 8740 1.1 christos const struct elf_backend_data *bed; 8741 1.6 christos unsigned int symcount; 8742 1.1 christos asymbol **syms; 8743 1.1 christos struct elf_strtab_hash *stt; 8744 1.1 christos Elf_Internal_Shdr *symtab_hdr; 8745 1.6 christos Elf_Internal_Shdr *symtab_shndx_hdr; 8746 1.1 christos Elf_Internal_Shdr *symstrtab_hdr; 8747 1.1 christos struct elf_sym_strtab *symstrtab; 8748 1.6 christos bfd_byte *outbound_syms; 8749 1.12 christos bfd_byte *outbound_shndx; 8750 1.3 christos unsigned long outbound_syms_index; 8751 1.12 christos unsigned int idx; 8752 1.14 christos unsigned int num_locals; 8753 1.1 christos size_t amt; 8754 1.3 christos bool name_local_sections; 8755 1.14 christos 8756 1.1 christos if (!elf_map_symbols (abfd, &num_locals)) 8757 1.1 christos return false; 8758 1.6 christos 8759 1.1 christos /* Dump out the symtabs. */ 8760 1.14 christos stt = _bfd_elf_strtab_init (); 8761 1.1 christos if (stt == NULL) 8762 1.1 christos return false; 8763 1.1 christos 8764 1.1 christos bed = get_elf_backend_data (abfd); 8765 1.1 christos symcount = bfd_get_symcount (abfd); 8766 1.1 christos symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 8767 1.1 christos symtab_hdr->sh_type = SHT_SYMTAB; 8768 1.3 christos symtab_hdr->sh_entsize = bed->s->sizeof_sym; 8769 1.1 christos symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); 8770 1.1 christos symtab_hdr->sh_info = num_locals + 1; 8771 1.1 christos symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; 8772 1.1 christos 8773 1.1 christos symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; 8774 1.6 christos symstrtab_hdr->sh_type = SHT_STRTAB; 8775 1.12 christos 8776 1.12 christos /* Allocate buffer to swap out the .strtab section. */ 8777 1.6 christos if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt) 8778 1.12 christos || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL) 8779 1.6 christos { 8780 1.14 christos bfd_set_error (bfd_error_no_memory); 8781 1.6 christos _bfd_elf_strtab_free (stt); 8782 1.6 christos return false; 8783 1.12 christos } 8784 1.19 christos 8785 1.1 christos if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt) 8786 1.12 christos || (outbound_syms = bfd_malloc (amt)) == NULL) 8787 1.12 christos { 8788 1.12 christos error_no_mem: 8789 1.12 christos bfd_set_error (bfd_error_no_memory); 8790 1.6 christos error_return: 8791 1.14 christos free (symstrtab); 8792 1.1 christos _bfd_elf_strtab_free (stt); 8793 1.1 christos return false; 8794 1.6 christos } 8795 1.1 christos symtab_hdr->contents = outbound_syms; 8796 1.1 christos outbound_syms_index = 0; 8797 1.8 christos 8798 1.8 christos outbound_shndx = NULL; 8799 1.1 christos 8800 1.8 christos if (elf_symtab_shndx_list (abfd)) 8801 1.8 christos { 8802 1.8 christos symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; 8803 1.12 christos if (symtab_shndx_hdr->sh_name != 0) 8804 1.12 christos { 8805 1.12 christos if (_bfd_mul_overflow (symcount + 1, 8806 1.12 christos sizeof (Elf_External_Sym_Shndx), &amt)) 8807 1.8 christos goto error_no_mem; 8808 1.8 christos outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt); 8809 1.1 christos if (outbound_shndx == NULL) 8810 1.8 christos goto error_return; 8811 1.8 christos 8812 1.8 christos symtab_shndx_hdr->contents = outbound_shndx; 8813 1.8 christos symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; 8814 1.8 christos symtab_shndx_hdr->sh_size = amt; 8815 1.8 christos symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); 8816 1.8 christos symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); 8817 1.1 christos } 8818 1.1 christos /* FIXME: What about any other headers in the list ? */ 8819 1.1 christos } 8820 1.1 christos 8821 1.1 christos /* Now generate the data (for "contents"). */ 8822 1.1 christos { 8823 1.1 christos /* Fill in zeroth symbol and swap it out. */ 8824 1.1 christos Elf_Internal_Sym sym; 8825 1.1 christos sym.st_name = 0; 8826 1.1 christos sym.st_value = 0; 8827 1.1 christos sym.st_size = 0; 8828 1.1 christos sym.st_info = 0; 8829 1.1 christos sym.st_other = 0; 8830 1.17 christos sym.st_shndx = SHN_UNDEF; 8831 1.17 christos sym.st_target_internal = 0; 8832 1.6 christos symstrtab[outbound_syms_index].sym = sym; 8833 1.1 christos symstrtab[outbound_syms_index].dest_index = outbound_syms_index; 8834 1.1 christos outbound_syms_index++; 8835 1.1 christos } 8836 1.1 christos 8837 1.1 christos name_local_sections 8838 1.1 christos = (bed->elf_backend_name_local_section_symbols 8839 1.1 christos && bed->elf_backend_name_local_section_symbols (abfd)); 8840 1.17 christos 8841 1.1 christos syms = bfd_get_outsymbols (abfd); 8842 1.1 christos for (idx = 0; idx < symcount; idx++) 8843 1.17 christos { 8844 1.1 christos Elf_Internal_Sym sym; 8845 1.1 christos 8846 1.1 christos flagword flags = syms[idx]->flags; 8847 1.1 christos if (!name_local_sections 8848 1.1 christos && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) 8849 1.18 christos { 8850 1.1 christos /* Local section symbols have no name. */ 8851 1.1 christos sym.st_name = 0; 8852 1.1 christos } 8853 1.6 christos else 8854 1.6 christos { 8855 1.18 christos /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize 8856 1.18 christos to get the final offset for st_name. */ 8857 1.6 christos size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false); 8858 1.18 christos if (stridx == (size_t) -1) 8859 1.1 christos goto error_return; 8860 1.1 christos sym.st_name = stridx; 8861 1.17 christos } 8862 1.17 christos 8863 1.17 christos bfd_vma value = syms[idx]->value; 8864 1.1 christos elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]); 8865 1.17 christos asection *sec = syms[idx]->section; 8866 1.1 christos 8867 1.1 christos if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec)) 8868 1.1 christos { 8869 1.1 christos /* ELF common symbols put the alignment into the `value' field, 8870 1.1 christos and the size into the `size' field. This is backwards from 8871 1.1 christos how BFD handles it, so reverse it here. */ 8872 1.1 christos sym.st_size = value; 8873 1.1 christos if (type_ptr == NULL 8874 1.1 christos || type_ptr->internal_elf_sym.st_value == 0) 8875 1.1 christos sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); 8876 1.17 christos else 8877 1.1 christos sym.st_value = type_ptr->internal_elf_sym.st_value; 8878 1.1 christos sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 8879 1.1 christos } 8880 1.1 christos else 8881 1.1 christos { 8882 1.1 christos unsigned int shndx; 8883 1.1 christos 8884 1.1 christos if (sec->output_section) 8885 1.1 christos { 8886 1.1 christos value += sec->output_offset; 8887 1.1 christos sec = sec->output_section; 8888 1.1 christos } 8889 1.1 christos 8890 1.1 christos /* Don't add in the section vma for relocatable output. */ 8891 1.1 christos if (! relocatable_p) 8892 1.1 christos value += sec->vma; 8893 1.1 christos sym.st_value = value; 8894 1.1 christos sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; 8895 1.1 christos 8896 1.1 christos if (bfd_is_abs_section (sec) 8897 1.1 christos && type_ptr != NULL 8898 1.1 christos && type_ptr->internal_elf_sym.st_shndx != 0) 8899 1.1 christos { 8900 1.1 christos /* This symbol is in a real ELF section which we did 8901 1.1 christos not create as a BFD section. Undo the mapping done 8902 1.1 christos by copy_private_symbol_data. */ 8903 1.1 christos shndx = type_ptr->internal_elf_sym.st_shndx; 8904 1.1 christos switch (shndx) 8905 1.1 christos { 8906 1.1 christos case MAP_ONESYMTAB: 8907 1.1 christos shndx = elf_onesymtab (abfd); 8908 1.1 christos break; 8909 1.1 christos case MAP_DYNSYMTAB: 8910 1.1 christos shndx = elf_dynsymtab (abfd); 8911 1.3 christos break; 8912 1.1 christos case MAP_STRTAB: 8913 1.1 christos shndx = elf_strtab_sec (abfd); 8914 1.3 christos break; 8915 1.1 christos case MAP_SHSTRTAB: 8916 1.1 christos shndx = elf_shstrtab_sec (abfd); 8917 1.8 christos break; 8918 1.8 christos case MAP_SYM_SHNDX: 8919 1.1 christos if (elf_symtab_shndx_list (abfd)) 8920 1.12 christos shndx = elf_symtab_shndx_list (abfd)->ndx; 8921 1.12 christos break; 8922 1.12 christos case SHN_COMMON: 8923 1.12 christos case SHN_ABS: 8924 1.1 christos shndx = SHN_ABS; 8925 1.12 christos break; 8926 1.12 christos default: 8927 1.12 christos if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS) 8928 1.12 christos { 8929 1.12 christos if (bed->symbol_section_index) 8930 1.12 christos shndx = bed->symbol_section_index (abfd, type_ptr); 8931 1.12 christos /* Otherwise just leave the index alone. */ 8932 1.12 christos } 8933 1.12 christos else 8934 1.12 christos { 8935 1.12 christos if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE) 8936 1.12 christos _bfd_error_handler (_("%pB: \ 8937 1.12 christos Unable to handle section index %x in ELF symbol. Using ABS instead."), 8938 1.12 christos abfd, shndx); 8939 1.1 christos shndx = SHN_ABS; 8940 1.1 christos } 8941 1.1 christos break; 8942 1.1 christos } 8943 1.1 christos } 8944 1.1 christos else 8945 1.1 christos { 8946 1.1 christos shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 8947 1.1 christos 8948 1.1 christos if (shndx == SHN_BAD) 8949 1.1 christos { 8950 1.1 christos asection *sec2; 8951 1.1 christos 8952 1.1 christos /* Writing this would be a hell of a lot easier if 8953 1.1 christos we had some decent documentation on bfd, and 8954 1.1 christos knew what to expect of the library, and what to 8955 1.1 christos demand of applications. For example, it 8956 1.1 christos appears that `objcopy' might not set the 8957 1.1 christos section of a symbol to be a section that is 8958 1.8 christos actually in the output file. */ 8959 1.8 christos sec2 = bfd_get_section_by_name (abfd, sec->name); 8960 1.8 christos if (sec2 != NULL) 8961 1.1 christos shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); 8962 1.9 christos if (shndx == SHN_BAD) 8963 1.11 christos { 8964 1.11 christos /* xgettext:c-format */ 8965 1.11 christos _bfd_error_handler 8966 1.11 christos (_("unable to find equivalent output section" 8967 1.11 christos " for symbol '%s' from section '%s'"), 8968 1.1 christos syms[idx]->name ? syms[idx]->name : "<Local sym>", 8969 1.6 christos sec->name); 8970 1.1 christos bfd_set_error (bfd_error_invalid_operation); 8971 1.1 christos goto error_return; 8972 1.1 christos } 8973 1.1 christos } 8974 1.1 christos } 8975 1.1 christos 8976 1.1 christos sym.st_shndx = shndx; 8977 1.17 christos } 8978 1.1 christos 8979 1.1 christos int type; 8980 1.1 christos if ((flags & BSF_THREAD_LOCAL) != 0) 8981 1.1 christos type = STT_TLS; 8982 1.1 christos else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) 8983 1.1 christos type = STT_GNU_IFUNC; 8984 1.1 christos else if ((flags & BSF_FUNCTION) != 0) 8985 1.1 christos type = STT_FUNC; 8986 1.1 christos else if ((flags & BSF_OBJECT) != 0) 8987 1.1 christos type = STT_OBJECT; 8988 1.1 christos else if ((flags & BSF_RELC) != 0) 8989 1.1 christos type = STT_RELC; 8990 1.1 christos else if ((flags & BSF_SRELC) != 0) 8991 1.1 christos type = STT_SRELC; 8992 1.1 christos else 8993 1.1 christos type = STT_NOTYPE; 8994 1.1 christos 8995 1.1 christos if (syms[idx]->section->flags & SEC_THREAD_LOCAL) 8996 1.1 christos type = STT_TLS; 8997 1.1 christos 8998 1.1 christos /* Processor-specific types. */ 8999 1.1 christos if (type_ptr != NULL 9000 1.1 christos && bed->elf_backend_get_symbol_type) 9001 1.1 christos type = ((*bed->elf_backend_get_symbol_type) 9002 1.1 christos (&type_ptr->internal_elf_sym, type)); 9003 1.1 christos 9004 1.1 christos if (flags & BSF_SECTION_SYM) 9005 1.1 christos { 9006 1.1 christos if (flags & BSF_GLOBAL) 9007 1.1 christos sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); 9008 1.1 christos else 9009 1.1 christos sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 9010 1.1 christos } 9011 1.8 christos else if (bfd_is_com_section (syms[idx]->section)) 9012 1.8 christos { 9013 1.8 christos if (type != STT_TLS) 9014 1.8 christos { 9015 1.8 christos if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) 9016 1.8 christos type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) 9017 1.8 christos ? STT_COMMON : STT_OBJECT); 9018 1.8 christos else 9019 1.8 christos type = ((flags & BSF_ELF_COMMON) != 0 9020 1.8 christos ? STT_COMMON : STT_OBJECT); 9021 1.1 christos } 9022 1.1 christos sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); 9023 1.1 christos } 9024 1.1 christos else if (bfd_is_und_section (syms[idx]->section)) 9025 1.1 christos sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) 9026 1.1 christos ? STB_WEAK 9027 1.1 christos : STB_GLOBAL), 9028 1.1 christos type); 9029 1.1 christos else if (flags & BSF_FILE) 9030 1.1 christos sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 9031 1.1 christos else 9032 1.1 christos { 9033 1.1 christos int bind = STB_LOCAL; 9034 1.1 christos 9035 1.1 christos if (flags & BSF_LOCAL) 9036 1.1 christos bind = STB_LOCAL; 9037 1.1 christos else if (flags & BSF_GNU_UNIQUE) 9038 1.1 christos bind = STB_GNU_UNIQUE; 9039 1.1 christos else if (flags & BSF_WEAK) 9040 1.1 christos bind = STB_WEAK; 9041 1.1 christos else if (flags & BSF_GLOBAL) 9042 1.1 christos bind = STB_GLOBAL; 9043 1.1 christos 9044 1.1 christos sym.st_info = ELF_ST_INFO (bind, type); 9045 1.1 christos } 9046 1.1 christos 9047 1.1 christos if (type_ptr != NULL) 9048 1.1 christos { 9049 1.1 christos sym.st_other = type_ptr->internal_elf_sym.st_other; 9050 1.1 christos sym.st_target_internal 9051 1.1 christos = type_ptr->internal_elf_sym.st_target_internal; 9052 1.1 christos } 9053 1.1 christos else 9054 1.1 christos { 9055 1.1 christos sym.st_other = 0; 9056 1.1 christos sym.st_target_internal = 0; 9057 1.17 christos } 9058 1.17 christos 9059 1.6 christos symstrtab[outbound_syms_index].sym = sym; 9060 1.6 christos symstrtab[outbound_syms_index].dest_index = outbound_syms_index; 9061 1.6 christos outbound_syms_index++; 9062 1.6 christos } 9063 1.6 christos 9064 1.6 christos /* Finalize the .strtab section. */ 9065 1.6 christos _bfd_elf_strtab_finalize (stt); 9066 1.17 christos 9067 1.6 christos /* Swap out the .strtab section. */ 9068 1.6 christos for (idx = 0; idx < outbound_syms_index; idx++) 9069 1.18 christos { 9070 1.6 christos struct elf_sym_strtab *elfsym = &symstrtab[idx]; 9071 1.6 christos if (elfsym->sym.st_name != 0) 9072 1.14 christos elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, 9073 1.14 christos elfsym->sym.st_name); 9074 1.14 christos if (info && info->callbacks->ctf_new_symbol) 9075 1.14 christos info->callbacks->ctf_new_symbol (elfsym->dest_index, 9076 1.14 christos &elfsym->sym); 9077 1.14 christos 9078 1.6 christos /* Inform the linker of the addition of this symbol. */ 9079 1.6 christos 9080 1.6 christos bed->s->swap_symbol_out (abfd, &elfsym->sym, 9081 1.6 christos (outbound_syms 9082 1.14 christos + (elfsym->dest_index 9083 1.14 christos * bed->s->sizeof_sym)), 9084 1.14 christos NPTR_ADD (outbound_shndx, 9085 1.1 christos (elfsym->dest_index 9086 1.6 christos * sizeof (Elf_External_Sym_Shndx)))); 9087 1.1 christos } 9088 1.1 christos free (symstrtab); 9089 1.6 christos 9090 1.1 christos *sttp = stt; 9091 1.8 christos symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); 9092 1.1 christos symstrtab_hdr->sh_type = SHT_STRTAB; 9093 1.1 christos symstrtab_hdr->sh_flags = bed->elf_strtab_flags; 9094 1.1 christos symstrtab_hdr->sh_addr = 0; 9095 1.1 christos symstrtab_hdr->sh_entsize = 0; 9096 1.1 christos symstrtab_hdr->sh_link = 0; 9097 1.1 christos symstrtab_hdr->sh_info = 0; 9098 1.14 christos symstrtab_hdr->sh_addralign = 1; 9099 1.1 christos 9100 1.1 christos return true; 9101 1.1 christos } 9102 1.1 christos 9103 1.1 christos /* Return the number of bytes required to hold the symtab vector. 9104 1.1 christos 9105 1.1 christos Note that we base it on the count plus 1, since we will null terminate 9106 1.1 christos the vector allocated based on this size. However, the ELF symbol table 9107 1.1 christos always has a dummy entry as symbol #0, so it ends up even. */ 9108 1.1 christos 9109 1.1 christos long 9110 1.11 christos _bfd_elf_get_symtab_upper_bound (bfd *abfd) 9111 1.1 christos { 9112 1.1 christos bfd_size_type symcount; 9113 1.1 christos long symtab_size; 9114 1.1 christos Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; 9115 1.12 christos 9116 1.11 christos symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 9117 1.11 christos if (symcount > LONG_MAX / sizeof (asymbol *)) 9118 1.11 christos { 9119 1.11 christos bfd_set_error (bfd_error_file_too_big); 9120 1.12 christos return -1; 9121 1.12 christos } 9122 1.12 christos symtab_size = symcount * (sizeof (asymbol *)); 9123 1.12 christos if (symcount == 0) 9124 1.12 christos symtab_size = sizeof (asymbol *); 9125 1.12 christos else if (!bfd_write_p (abfd)) 9126 1.12 christos { 9127 1.12 christos ufile_ptr filesize = bfd_get_file_size (abfd); 9128 1.12 christos 9129 1.12 christos if (filesize != 0 && (unsigned long) symtab_size > filesize) 9130 1.12 christos { 9131 1.12 christos bfd_set_error (bfd_error_file_truncated); 9132 1.12 christos return -1; 9133 1.1 christos } 9134 1.1 christos } 9135 1.1 christos 9136 1.1 christos return symtab_size; 9137 1.1 christos } 9138 1.1 christos 9139 1.1 christos long 9140 1.11 christos _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) 9141 1.1 christos { 9142 1.1 christos bfd_size_type symcount; 9143 1.1 christos long symtab_size; 9144 1.1 christos Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; 9145 1.1 christos 9146 1.17 christos if (elf_dynsymtab (abfd) == 0) 9147 1.17 christos { 9148 1.17 christos /* Check if there is dynamic symbol table. */ 9149 1.17 christos symcount = elf_tdata (abfd)->dt_symtab_count; 9150 1.17 christos if (symcount) 9151 1.1 christos goto compute_symtab_size; 9152 1.1 christos 9153 1.1 christos bfd_set_error (bfd_error_invalid_operation); 9154 1.1 christos return -1; 9155 1.1 christos } 9156 1.12 christos 9157 1.11 christos symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 9158 1.11 christos if (symcount > LONG_MAX / sizeof (asymbol *)) 9159 1.11 christos { 9160 1.11 christos bfd_set_error (bfd_error_file_too_big); 9161 1.17 christos return -1; 9162 1.17 christos } 9163 1.12 christos 9164 1.12 christos compute_symtab_size: 9165 1.12 christos symtab_size = symcount * (sizeof (asymbol *)); 9166 1.12 christos if (symcount == 0) 9167 1.12 christos symtab_size = sizeof (asymbol *); 9168 1.12 christos else if (!bfd_write_p (abfd)) 9169 1.12 christos { 9170 1.12 christos ufile_ptr filesize = bfd_get_file_size (abfd); 9171 1.12 christos 9172 1.12 christos if (filesize != 0 && (unsigned long) symtab_size > filesize) 9173 1.12 christos { 9174 1.12 christos bfd_set_error (bfd_error_file_truncated); 9175 1.12 christos return -1; 9176 1.1 christos } 9177 1.1 christos } 9178 1.1 christos 9179 1.1 christos return symtab_size; 9180 1.1 christos } 9181 1.12 christos 9182 1.1 christos long 9183 1.12 christos _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect) 9184 1.12 christos { 9185 1.12 christos if (asect->reloc_count != 0 && !bfd_write_p (abfd)) 9186 1.12 christos { 9187 1.12 christos /* Sanity check reloc section size. */ 9188 1.14 christos ufile_ptr filesize = bfd_get_file_size (abfd); 9189 1.12 christos 9190 1.14 christos if (filesize != 0) 9191 1.14 christos { 9192 1.14 christos struct bfd_elf_section_data *d = elf_section_data (asect); 9193 1.14 christos bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0; 9194 1.14 christos bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0; 9195 1.14 christos 9196 1.14 christos if (rel_size + rela_size > filesize 9197 1.14 christos || rel_size + rela_size < rel_size) 9198 1.14 christos { 9199 1.14 christos bfd_set_error (bfd_error_file_truncated); 9200 1.12 christos return -1; 9201 1.12 christos } 9202 1.12 christos } 9203 1.12 christos } 9204 1.12 christos 9205 1.12 christos #if SIZEOF_LONG == SIZEOF_INT 9206 1.12 christos if (asect->reloc_count >= LONG_MAX / sizeof (arelent *)) 9207 1.12 christos { 9208 1.12 christos bfd_set_error (bfd_error_file_too_big); 9209 1.12 christos return -1; 9210 1.14 christos } 9211 1.1 christos #endif 9212 1.1 christos return (asect->reloc_count + 1L) * sizeof (arelent *); 9213 1.1 christos } 9214 1.1 christos 9215 1.1 christos /* Canonicalize the relocs. */ 9216 1.1 christos 9217 1.1 christos long 9218 1.1 christos _bfd_elf_canonicalize_reloc (bfd *abfd, 9219 1.1 christos sec_ptr section, 9220 1.1 christos arelent **relptr, 9221 1.1 christos asymbol **symbols) 9222 1.1 christos { 9223 1.1 christos arelent *tblptr; 9224 1.1 christos unsigned int i; 9225 1.14 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9226 1.1 christos 9227 1.1 christos if (! bed->s->slurp_reloc_table (abfd, section, symbols, false)) 9228 1.1 christos return -1; 9229 1.1 christos 9230 1.1 christos tblptr = section->relocation; 9231 1.1 christos for (i = 0; i < section->reloc_count; i++) 9232 1.1 christos *relptr++ = tblptr++; 9233 1.1 christos 9234 1.1 christos *relptr = NULL; 9235 1.1 christos 9236 1.1 christos return section->reloc_count; 9237 1.1 christos } 9238 1.1 christos 9239 1.1 christos long 9240 1.1 christos _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) 9241 1.14 christos { 9242 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9243 1.1 christos long symcount = bed->s->slurp_symbol_table (abfd, allocation, false); 9244 1.12 christos 9245 1.1 christos if (symcount >= 0) 9246 1.1 christos abfd->symcount = symcount; 9247 1.1 christos return symcount; 9248 1.1 christos } 9249 1.1 christos 9250 1.1 christos long 9251 1.1 christos _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, 9252 1.1 christos asymbol **allocation) 9253 1.14 christos { 9254 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9255 1.1 christos long symcount = bed->s->slurp_symbol_table (abfd, allocation, true); 9256 1.12 christos 9257 1.1 christos if (symcount >= 0) 9258 1.1 christos abfd->dynsymcount = symcount; 9259 1.1 christos return symcount; 9260 1.1 christos } 9261 1.1 christos 9262 1.1 christos /* Return the size required for the dynamic reloc entries. Any loadable 9263 1.1 christos section that was actually installed in the BFD, and has type SHT_REL 9264 1.1 christos or SHT_RELA, and uses the dynamic symbol table, is considered to be a 9265 1.1 christos dynamic reloc section. */ 9266 1.1 christos 9267 1.1 christos long 9268 1.12 christos _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) 9269 1.1 christos { 9270 1.1 christos bfd_size_type count, ext_rel_size; 9271 1.1 christos asection *s; 9272 1.1 christos 9273 1.1 christos if (elf_dynsymtab (abfd) == 0) 9274 1.1 christos { 9275 1.1 christos bfd_set_error (bfd_error_invalid_operation); 9276 1.1 christos return -1; 9277 1.11 christos } 9278 1.12 christos 9279 1.1 christos count = 1; 9280 1.1 christos ext_rel_size = 0; 9281 1.1 christos for (s = abfd->sections; s != NULL; s = s->next) 9282 1.17 christos if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) 9283 1.17 christos && (elf_section_data (s)->this_hdr.sh_type == SHT_REL 9284 1.11 christos || elf_section_data (s)->this_hdr.sh_type == SHT_RELA) 9285 1.17 christos && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0) 9286 1.17 christos { 9287 1.12 christos ext_rel_size += elf_section_data (s)->this_hdr.sh_size; 9288 1.12 christos if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size) 9289 1.12 christos { 9290 1.12 christos bfd_set_error (bfd_error_file_truncated); 9291 1.17 christos return -1; 9292 1.11 christos } 9293 1.11 christos count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr); 9294 1.11 christos if (count > LONG_MAX / sizeof (arelent *)) 9295 1.11 christos { 9296 1.11 christos bfd_set_error (bfd_error_file_too_big); 9297 1.11 christos return -1; 9298 1.12 christos } 9299 1.12 christos } 9300 1.12 christos if (count > 1 && !bfd_write_p (abfd)) 9301 1.12 christos { 9302 1.12 christos /* Sanity check reloc section sizes. */ 9303 1.12 christos ufile_ptr filesize = bfd_get_file_size (abfd); 9304 1.12 christos if (filesize != 0 && ext_rel_size > filesize) 9305 1.12 christos { 9306 1.12 christos bfd_set_error (bfd_error_file_truncated); 9307 1.12 christos return -1; 9308 1.11 christos } 9309 1.1 christos } 9310 1.1 christos return count * sizeof (arelent *); 9311 1.1 christos } 9312 1.1 christos 9313 1.1 christos /* Canonicalize the dynamic relocation entries. Note that we return the 9314 1.1 christos dynamic relocations as a single block, although they are actually 9315 1.1 christos associated with particular sections; the interface, which was 9316 1.1 christos designed for SunOS style shared libraries, expects that there is only 9317 1.1 christos one set of dynamic relocs. Any loadable section that was actually 9318 1.1 christos installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the 9319 1.1 christos dynamic symbol table, is considered to be a dynamic reloc section. */ 9320 1.1 christos 9321 1.1 christos long 9322 1.1 christos _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, 9323 1.1 christos arelent **storage, 9324 1.14 christos asymbol **syms) 9325 1.1 christos { 9326 1.1 christos bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool); 9327 1.1 christos asection *s; 9328 1.1 christos long ret; 9329 1.1 christos 9330 1.1 christos if (elf_dynsymtab (abfd) == 0) 9331 1.1 christos { 9332 1.1 christos bfd_set_error (bfd_error_invalid_operation); 9333 1.1 christos return -1; 9334 1.1 christos } 9335 1.1 christos 9336 1.1 christos slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; 9337 1.1 christos ret = 0; 9338 1.1 christos for (s = abfd->sections; s != NULL; s = s->next) 9339 1.1 christos { 9340 1.17 christos if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) 9341 1.17 christos && (elf_section_data (s)->this_hdr.sh_type == SHT_REL 9342 1.1 christos || elf_section_data (s)->this_hdr.sh_type == SHT_RELA) 9343 1.1 christos && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0) 9344 1.1 christos { 9345 1.1 christos arelent *p; 9346 1.14 christos long count, i; 9347 1.1 christos 9348 1.17 christos if (! (*slurp_relocs) (abfd, s, syms, true)) 9349 1.1 christos return -1; 9350 1.1 christos count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr); 9351 1.1 christos p = s->relocation; 9352 1.1 christos for (i = 0; i < count; i++) 9353 1.1 christos *storage++ = p++; 9354 1.1 christos ret += count; 9355 1.1 christos } 9356 1.1 christos } 9357 1.1 christos 9358 1.1 christos *storage = NULL; 9359 1.1 christos 9360 1.1 christos return ret; 9361 1.1 christos } 9362 1.1 christos 9363 1.14 christos /* Read in the version information. */ 9365 1.1 christos 9366 1.1 christos bool 9367 1.1 christos _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver) 9368 1.12 christos { 9369 1.17 christos bfd_byte *contents = NULL; 9370 1.17 christos unsigned int freeidx = 0; 9371 1.1 christos size_t amt; 9372 1.17 christos void *contents_addr = NULL; 9373 1.1 christos size_t contents_size = 0; 9374 1.1 christos 9375 1.1 christos if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL) 9376 1.1 christos { 9377 1.1 christos Elf_Internal_Shdr *hdr; 9378 1.1 christos Elf_External_Verneed *everneed; 9379 1.17 christos Elf_Internal_Verneed *iverneed; 9380 1.17 christos unsigned int i; 9381 1.1 christos bfd_byte *contents_end; 9382 1.17 christos size_t verneed_count; 9383 1.1 christos size_t verneed_size; 9384 1.17 christos 9385 1.17 christos if (elf_tdata (abfd)->dt_verneed != NULL) 9386 1.17 christos { 9387 1.17 christos hdr = NULL; 9388 1.1 christos contents = elf_tdata (abfd)->dt_verneed; 9389 1.17 christos verneed_count = elf_tdata (abfd)->dt_verneed_count; 9390 1.17 christos verneed_size = verneed_count * sizeof (Elf_External_Verneed); 9391 1.17 christos } 9392 1.5 christos else 9393 1.17 christos { 9394 1.17 christos hdr = &elf_tdata (abfd)->dynverref_hdr; 9395 1.17 christos 9396 1.17 christos if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed)) 9397 1.17 christos { 9398 1.17 christos error_return_bad_verref: 9399 1.17 christos _bfd_error_handler 9400 1.17 christos (_("%pB: .gnu.version_r invalid entry"), abfd); 9401 1.17 christos bfd_set_error (bfd_error_bad_value); 9402 1.17 christos error_return_verref: 9403 1.17 christos elf_tdata (abfd)->verref = NULL; 9404 1.17 christos elf_tdata (abfd)->cverrefs = 0; 9405 1.17 christos goto error_return; 9406 1.17 christos } 9407 1.17 christos 9408 1.18 christos if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) 9409 1.18 christos goto error_return_verref; 9410 1.17 christos contents_size = hdr->sh_size; 9411 1.17 christos contents = _bfd_mmap_temporary (abfd, contents_size, 9412 1.17 christos &contents_addr, &contents_size); 9413 1.17 christos if (contents == NULL) 9414 1.17 christos goto error_return_verref; 9415 1.17 christos 9416 1.5 christos verneed_size = hdr->sh_size; 9417 1.17 christos verneed_count = hdr->sh_info; 9418 1.17 christos } 9419 1.12 christos 9420 1.12 christos if (_bfd_mul_overflow (verneed_count, 9421 1.12 christos sizeof (Elf_Internal_Verneed), &amt)) 9422 1.12 christos { 9423 1.14 christos bfd_set_error (bfd_error_file_too_big); 9424 1.14 christos goto error_return_verref; 9425 1.14 christos } 9426 1.5 christos if (amt == 0) 9427 1.1 christos goto error_return_verref; 9428 1.1 christos elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt); 9429 1.1 christos if (elf_tdata (abfd)->verref == NULL) 9430 1.1 christos goto error_return_verref; 9431 1.17 christos 9432 1.17 christos BFD_ASSERT (sizeof (Elf_External_Verneed) 9433 1.1 christos == sizeof (Elf_External_Vernaux)); 9434 1.1 christos contents_end = (contents + verneed_size 9435 1.17 christos - sizeof (Elf_External_Verneed)); 9436 1.1 christos everneed = (Elf_External_Verneed *) contents; 9437 1.1 christos iverneed = elf_tdata (abfd)->verref; 9438 1.1 christos for (i = 0; i < verneed_count; i++, iverneed++) 9439 1.1 christos { 9440 1.1 christos Elf_External_Vernaux *evernaux; 9441 1.1 christos Elf_Internal_Vernaux *ivernaux; 9442 1.1 christos unsigned int j; 9443 1.1 christos 9444 1.1 christos _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); 9445 1.17 christos 9446 1.17 christos iverneed->vn_bfd = abfd; 9447 1.17 christos 9448 1.17 christos if (elf_use_dt_symtab_p (abfd)) 9449 1.17 christos { 9450 1.17 christos if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz) 9451 1.17 christos iverneed->vn_filename 9452 1.17 christos = elf_tdata (abfd)->dt_strtab + iverneed->vn_file; 9453 1.17 christos else 9454 1.17 christos iverneed->vn_filename = NULL; 9455 1.17 christos } 9456 1.17 christos else if (hdr == NULL) 9457 1.17 christos goto error_return_bad_verref; 9458 1.17 christos else 9459 1.1 christos iverneed->vn_filename 9460 1.5 christos = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 9461 1.1 christos iverneed->vn_file); 9462 1.1 christos if (iverneed->vn_filename == NULL) 9463 1.1 christos goto error_return_bad_verref; 9464 1.1 christos 9465 1.1 christos if (iverneed->vn_cnt == 0) 9466 1.12 christos iverneed->vn_auxptr = NULL; 9467 1.12 christos else 9468 1.12 christos { 9469 1.12 christos if (_bfd_mul_overflow (iverneed->vn_cnt, 9470 1.12 christos sizeof (Elf_Internal_Vernaux), &amt)) 9471 1.12 christos { 9472 1.1 christos bfd_set_error (bfd_error_file_too_big); 9473 1.12 christos goto error_return_verref; 9474 1.1 christos } 9475 1.1 christos iverneed->vn_auxptr = (struct elf_internal_vernaux *) 9476 1.1 christos bfd_alloc (abfd, amt); 9477 1.1 christos if (iverneed->vn_auxptr == NULL) 9478 1.1 christos goto error_return_verref; 9479 1.1 christos } 9480 1.5 christos 9481 1.1 christos if (iverneed->vn_aux 9482 1.1 christos > (size_t) (contents_end - (bfd_byte *) everneed)) 9483 1.1 christos goto error_return_bad_verref; 9484 1.1 christos 9485 1.1 christos evernaux = ((Elf_External_Vernaux *) 9486 1.1 christos ((bfd_byte *) everneed + iverneed->vn_aux)); 9487 1.1 christos ivernaux = iverneed->vn_auxptr; 9488 1.1 christos for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) 9489 1.17 christos { 9490 1.17 christos _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); 9491 1.17 christos 9492 1.17 christos if (elf_use_dt_symtab_p (abfd)) 9493 1.17 christos { 9494 1.17 christos if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz) 9495 1.17 christos ivernaux->vna_nodename 9496 1.17 christos = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name; 9497 1.17 christos else 9498 1.17 christos ivernaux->vna_nodename = NULL; 9499 1.17 christos } 9500 1.17 christos else if (hdr == NULL) 9501 1.17 christos goto error_return_bad_verref; 9502 1.17 christos else 9503 1.1 christos ivernaux->vna_nodename 9504 1.5 christos = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 9505 1.5 christos ivernaux->vna_name); 9506 1.5 christos if (ivernaux->vna_nodename == NULL) 9507 1.5 christos goto error_return_bad_verref; 9508 1.1 christos 9509 1.5 christos if (ivernaux->vna_other > freeidx) 9510 1.5 christos freeidx = ivernaux->vna_other; 9511 1.5 christos 9512 1.5 christos ivernaux->vna_nextptr = NULL; 9513 1.5 christos if (ivernaux->vna_next == 0) 9514 1.5 christos { 9515 1.1 christos iverneed->vn_cnt = j + 1; 9516 1.1 christos break; 9517 1.1 christos } 9518 1.1 christos if (j + 1 < iverneed->vn_cnt) 9519 1.1 christos ivernaux->vna_nextptr = ivernaux + 1; 9520 1.5 christos 9521 1.1 christos if (ivernaux->vna_next 9522 1.1 christos > (size_t) (contents_end - (bfd_byte *) evernaux)) 9523 1.1 christos goto error_return_bad_verref; 9524 1.1 christos 9525 1.1 christos evernaux = ((Elf_External_Vernaux *) 9526 1.5 christos ((bfd_byte *) evernaux + ivernaux->vna_next)); 9527 1.5 christos } 9528 1.5 christos 9529 1.17 christos iverneed->vn_nextref = NULL; 9530 1.1 christos if (iverneed->vn_next == 0) 9531 1.1 christos break; 9532 1.1 christos if (hdr != NULL && (i + 1 < hdr->sh_info)) 9533 1.1 christos iverneed->vn_nextref = iverneed + 1; 9534 1.5 christos 9535 1.1 christos if (iverneed->vn_next 9536 1.1 christos > (size_t) (contents_end - (bfd_byte *) everneed)) 9537 1.1 christos goto error_return_bad_verref; 9538 1.1 christos 9539 1.5 christos everneed = ((Elf_External_Verneed *) 9540 1.1 christos ((bfd_byte *) everneed + iverneed->vn_next)); 9541 1.17 christos } 9542 1.18 christos elf_tdata (abfd)->cverrefs = i; 9543 1.1 christos 9544 1.17 christos if (contents != elf_tdata (abfd)->dt_verneed) 9545 1.1 christos _bfd_munmap_temporary (contents_addr, contents_size); 9546 1.1 christos contents = NULL; 9547 1.17 christos contents_addr = NULL; 9548 1.1 christos } 9549 1.1 christos 9550 1.1 christos if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL) 9551 1.1 christos { 9552 1.1 christos Elf_Internal_Shdr *hdr; 9553 1.1 christos Elf_External_Verdef *everdef; 9554 1.1 christos Elf_Internal_Verdef *iverdef; 9555 1.1 christos Elf_Internal_Verdef *iverdefarr; 9556 1.1 christos Elf_Internal_Verdef iverdefmem; 9557 1.17 christos unsigned int i; 9558 1.17 christos unsigned int maxidx; 9559 1.1 christos bfd_byte *contents_end_def, *contents_end_aux; 9560 1.17 christos size_t verdef_count; 9561 1.5 christos size_t verdef_size; 9562 1.17 christos 9563 1.17 christos if (elf_tdata (abfd)->dt_verdef != NULL) 9564 1.17 christos { 9565 1.17 christos hdr = NULL; 9566 1.5 christos contents = elf_tdata (abfd)->dt_verdef; 9567 1.17 christos verdef_count = elf_tdata (abfd)->dt_verdef_count; 9568 1.17 christos verdef_size = verdef_count * sizeof (Elf_External_Verdef); 9569 1.17 christos } 9570 1.5 christos else 9571 1.17 christos { 9572 1.17 christos hdr = &elf_tdata (abfd)->dynverdef_hdr; 9573 1.17 christos 9574 1.17 christos if (hdr->sh_size < sizeof (Elf_External_Verdef)) 9575 1.17 christos { 9576 1.17 christos error_return_bad_verdef: 9577 1.17 christos _bfd_error_handler 9578 1.17 christos (_("%pB: .gnu.version_d invalid entry"), abfd); 9579 1.17 christos bfd_set_error (bfd_error_bad_value); 9580 1.17 christos error_return_verdef: 9581 1.17 christos elf_tdata (abfd)->verdef = NULL; 9582 1.17 christos elf_tdata (abfd)->cverdefs = 0; 9583 1.17 christos goto error_return; 9584 1.17 christos } 9585 1.17 christos 9586 1.18 christos if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) 9587 1.18 christos goto error_return_verdef; 9588 1.17 christos contents_size = hdr->sh_size; 9589 1.17 christos contents = _bfd_mmap_temporary (abfd, contents_size, 9590 1.17 christos &contents_addr, &contents_size); 9591 1.17 christos if (contents == NULL) 9592 1.17 christos goto error_return_verdef; 9593 1.17 christos 9594 1.17 christos BFD_ASSERT (sizeof (Elf_External_Verdef) 9595 1.17 christos >= sizeof (Elf_External_Verdaux)); 9596 1.17 christos 9597 1.17 christos verdef_count = hdr->sh_info; 9598 1.17 christos verdef_size = hdr->sh_size; 9599 1.17 christos } 9600 1.17 christos 9601 1.17 christos contents_end_def = (contents + verdef_size 9602 1.1 christos - sizeof (Elf_External_Verdef)); 9603 1.1 christos contents_end_aux = (contents + verdef_size 9604 1.1 christos - sizeof (Elf_External_Verdaux)); 9605 1.1 christos 9606 1.1 christos /* We know the number of entries in the section but not the maximum 9607 1.1 christos index. Therefore we have to run through all entries and find 9608 1.17 christos the maximum. */ 9609 1.1 christos everdef = (Elf_External_Verdef *) contents; 9610 1.1 christos maxidx = 0; 9611 1.1 christos for (i = 0; i < verdef_count; ++i) 9612 1.5 christos { 9613 1.5 christos _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); 9614 1.1 christos 9615 1.1 christos if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) 9616 1.1 christos goto error_return_bad_verdef; 9617 1.5 christos if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) 9618 1.5 christos maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); 9619 1.5 christos 9620 1.1 christos if (iverdefmem.vd_next == 0) 9621 1.1 christos break; 9622 1.5 christos 9623 1.1 christos if (iverdefmem.vd_next 9624 1.1 christos > (size_t) (contents_end_def - (bfd_byte *) everdef)) 9625 1.1 christos goto error_return_bad_verdef; 9626 1.1 christos 9627 1.1 christos everdef = ((Elf_External_Verdef *) 9628 1.1 christos ((bfd_byte *) everdef + iverdefmem.vd_next)); 9629 1.1 christos } 9630 1.1 christos 9631 1.1 christos if (default_imported_symver) 9632 1.1 christos { 9633 1.1 christos if (freeidx > maxidx) 9634 1.1 christos maxidx = ++freeidx; 9635 1.12 christos else 9636 1.12 christos freeidx = ++maxidx; 9637 1.12 christos } 9638 1.12 christos if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt)) 9639 1.12 christos { 9640 1.17 christos bfd_set_error (bfd_error_file_too_big); 9641 1.17 christos goto error_return_verdef; 9642 1.17 christos } 9643 1.12 christos 9644 1.1 christos if (amt == 0) 9645 1.5 christos goto error_return_verdef; 9646 1.1 christos elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); 9647 1.1 christos if (elf_tdata (abfd)->verdef == NULL) 9648 1.1 christos goto error_return_verdef; 9649 1.1 christos 9650 1.1 christos elf_tdata (abfd)->cverdefs = maxidx; 9651 1.17 christos 9652 1.1 christos everdef = (Elf_External_Verdef *) contents; 9653 1.1 christos iverdefarr = elf_tdata (abfd)->verdef; 9654 1.1 christos for (i = 0; i < verdef_count; ++i) 9655 1.1 christos { 9656 1.1 christos Elf_External_Verdaux *everdaux; 9657 1.1 christos Elf_Internal_Verdaux *iverdaux; 9658 1.1 christos unsigned int j; 9659 1.1 christos 9660 1.5 christos _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); 9661 1.1 christos 9662 1.1 christos if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) 9663 1.8 christos goto error_return_bad_verdef; 9664 1.1 christos 9665 1.1 christos iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; 9666 1.1 christos memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); 9667 1.1 christos 9668 1.1 christos iverdef->vd_bfd = abfd; 9669 1.1 christos 9670 1.1 christos if (iverdef->vd_cnt == 0) 9671 1.12 christos iverdef->vd_auxptr = NULL; 9672 1.12 christos else 9673 1.12 christos { 9674 1.12 christos if (_bfd_mul_overflow (iverdef->vd_cnt, 9675 1.12 christos sizeof (Elf_Internal_Verdaux), &amt)) 9676 1.12 christos { 9677 1.1 christos bfd_set_error (bfd_error_file_too_big); 9678 1.12 christos goto error_return_verdef; 9679 1.1 christos } 9680 1.1 christos iverdef->vd_auxptr = (struct elf_internal_verdaux *) 9681 1.1 christos bfd_alloc (abfd, amt); 9682 1.1 christos if (iverdef->vd_auxptr == NULL) 9683 1.1 christos goto error_return_verdef; 9684 1.1 christos } 9685 1.5 christos 9686 1.1 christos if (iverdef->vd_aux 9687 1.1 christos > (size_t) (contents_end_aux - (bfd_byte *) everdef)) 9688 1.1 christos goto error_return_bad_verdef; 9689 1.1 christos 9690 1.1 christos everdaux = ((Elf_External_Verdaux *) 9691 1.1 christos ((bfd_byte *) everdef + iverdef->vd_aux)); 9692 1.1 christos iverdaux = iverdef->vd_auxptr; 9693 1.1 christos for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) 9694 1.17 christos { 9695 1.17 christos _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); 9696 1.17 christos 9697 1.17 christos if (elf_use_dt_symtab_p (abfd)) 9698 1.17 christos { 9699 1.17 christos if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz) 9700 1.17 christos iverdaux->vda_nodename 9701 1.17 christos = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name; 9702 1.17 christos else 9703 1.17 christos iverdaux->vda_nodename = NULL; 9704 1.17 christos } 9705 1.17 christos else 9706 1.1 christos iverdaux->vda_nodename 9707 1.5 christos = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 9708 1.1 christos iverdaux->vda_name); 9709 1.5 christos if (iverdaux->vda_nodename == NULL) 9710 1.5 christos goto error_return_bad_verdef; 9711 1.5 christos 9712 1.5 christos iverdaux->vda_nextptr = NULL; 9713 1.5 christos if (iverdaux->vda_next == 0) 9714 1.5 christos { 9715 1.1 christos iverdef->vd_cnt = j + 1; 9716 1.1 christos break; 9717 1.1 christos } 9718 1.1 christos if (j + 1 < iverdef->vd_cnt) 9719 1.1 christos iverdaux->vda_nextptr = iverdaux + 1; 9720 1.5 christos 9721 1.1 christos if (iverdaux->vda_next 9722 1.1 christos > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) 9723 1.1 christos goto error_return_bad_verdef; 9724 1.1 christos 9725 1.1 christos everdaux = ((Elf_External_Verdaux *) 9726 1.8 christos ((bfd_byte *) everdaux + iverdaux->vda_next)); 9727 1.1 christos } 9728 1.1 christos 9729 1.1 christos iverdef->vd_nodename = NULL; 9730 1.5 christos if (iverdef->vd_cnt) 9731 1.5 christos iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; 9732 1.5 christos 9733 1.1 christos iverdef->vd_nextdef = NULL; 9734 1.1 christos if (iverdef->vd_next == 0) 9735 1.1 christos break; 9736 1.1 christos if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) 9737 1.1 christos iverdef->vd_nextdef = iverdef + 1; 9738 1.1 christos 9739 1.1 christos everdef = ((Elf_External_Verdef *) 9740 1.17 christos ((bfd_byte *) everdef + iverdef->vd_next)); 9741 1.18 christos } 9742 1.1 christos 9743 1.17 christos if (contents != elf_tdata (abfd)->dt_verdef) 9744 1.1 christos _bfd_munmap_temporary (contents_addr, contents_size); 9745 1.1 christos contents = NULL; 9746 1.1 christos contents_addr = NULL; 9747 1.1 christos } 9748 1.1 christos else if (default_imported_symver) 9749 1.1 christos { 9750 1.1 christos if (freeidx < 3) 9751 1.1 christos freeidx = 3; 9752 1.12 christos else 9753 1.12 christos freeidx++; 9754 1.12 christos 9755 1.12 christos if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt)) 9756 1.12 christos { 9757 1.17 christos bfd_set_error (bfd_error_file_too_big); 9758 1.17 christos goto error_return; 9759 1.12 christos } 9760 1.1 christos if (amt == 0) 9761 1.1 christos goto error_return; 9762 1.1 christos elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); 9763 1.1 christos if (elf_tdata (abfd)->verdef == NULL) 9764 1.1 christos goto error_return; 9765 1.1 christos 9766 1.1 christos elf_tdata (abfd)->cverdefs = freeidx; 9767 1.1 christos } 9768 1.1 christos 9769 1.1 christos /* Create a default version based on the soname. */ 9770 1.1 christos if (default_imported_symver) 9771 1.1 christos { 9772 1.3 christos Elf_Internal_Verdef *iverdef; 9773 1.1 christos Elf_Internal_Verdaux *iverdaux; 9774 1.1 christos 9775 1.1 christos iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; 9776 1.1 christos 9777 1.1 christos iverdef->vd_version = VER_DEF_CURRENT; 9778 1.1 christos iverdef->vd_flags = 0; 9779 1.1 christos iverdef->vd_ndx = freeidx; 9780 1.1 christos iverdef->vd_cnt = 1; 9781 1.1 christos 9782 1.1 christos iverdef->vd_bfd = abfd; 9783 1.1 christos 9784 1.1 christos iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); 9785 1.5 christos if (iverdef->vd_nodename == NULL) 9786 1.5 christos goto error_return_verdef; 9787 1.1 christos iverdef->vd_nextdef = NULL; 9788 1.1 christos iverdef->vd_auxptr = ((struct elf_internal_verdaux *) 9789 1.1 christos bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); 9790 1.1 christos if (iverdef->vd_auxptr == NULL) 9791 1.1 christos goto error_return_verdef; 9792 1.1 christos 9793 1.1 christos iverdaux = iverdef->vd_auxptr; 9794 1.14 christos iverdaux->vda_nodename = iverdef->vd_nodename; 9795 1.1 christos } 9796 1.1 christos 9797 1.17 christos return true; 9798 1.17 christos 9799 1.18 christos error_return: 9800 1.14 christos if (contents != elf_tdata (abfd)->dt_verneed 9801 1.1 christos && contents != elf_tdata (abfd)->dt_verdef) 9802 1.1 christos _bfd_munmap_temporary (contents_addr, contents_size); 9803 1.1 christos return false; 9804 1.1 christos } 9805 1.1 christos 9806 1.1 christos asymbol * 9808 1.12 christos _bfd_elf_make_empty_symbol (bfd *abfd) 9809 1.1 christos { 9810 1.1 christos elf_symbol_type *newsym; 9811 1.5 christos 9812 1.5 christos newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym)); 9813 1.1 christos if (!newsym) 9814 1.1 christos return NULL; 9815 1.1 christos newsym->symbol.the_bfd = abfd; 9816 1.1 christos return &newsym->symbol; 9817 1.1 christos } 9818 1.1 christos 9819 1.1 christos void 9820 1.1 christos _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, 9821 1.1 christos asymbol *symbol, 9822 1.1 christos symbol_info *ret) 9823 1.1 christos { 9824 1.1 christos bfd_symbol_info (symbol, ret); 9825 1.1 christos } 9826 1.1 christos 9827 1.14 christos /* Return whether a symbol name implies a local symbol. Most targets 9828 1.1 christos use this function for the is_local_label_name entry point, but some 9829 1.1 christos override it. */ 9830 1.1 christos 9831 1.1 christos bool 9832 1.1 christos _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, 9833 1.14 christos const char *name) 9834 1.1 christos { 9835 1.1 christos /* Normal local symbols start with ``.L''. */ 9836 1.1 christos if (name[0] == '.' && name[1] == 'L') 9837 1.1 christos return true; 9838 1.14 christos 9839 1.1 christos /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate 9840 1.1 christos DWARF debugging symbols starting with ``..''. */ 9841 1.1 christos if (name[0] == '.' && name[1] == '.') 9842 1.1 christos return true; 9843 1.1 christos 9844 1.1 christos /* gcc will sometimes generate symbols beginning with ``_.L_'' when 9845 1.1 christos emitting DWARF debugging output. I suspect this is actually a 9846 1.1 christos small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call 9847 1.14 christos ASM_GENERATE_INTERNAL_LABEL, and this causes the leading 9848 1.1 christos underscore to be emitted on some ELF targets). For ease of use, 9849 1.6 christos we treat such symbols as local. */ 9850 1.6 christos if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') 9851 1.6 christos return true; 9852 1.6 christos 9853 1.11 christos /* Treat assembler generated fake symbols, dollar local labels and 9854 1.6 christos forward-backward labels (aka local labels) as locals. 9855 1.6 christos These labels have the form: 9856 1.6 christos 9857 1.6 christos L0^A.* (fake symbols) 9858 1.6 christos 9859 1.6 christos [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) 9860 1.6 christos 9861 1.14 christos Versions which start with .L will have already been matched above, 9862 1.6 christos so we only need to match the rest. */ 9863 1.6 christos if (name[0] == 'L' && ISDIGIT (name[1])) 9864 1.6 christos { 9865 1.6 christos bool ret = false; 9866 1.6 christos const char * p; 9867 1.6 christos char c; 9868 1.6 christos 9869 1.6 christos for (p = name + 2; (c = *p); p++) 9870 1.6 christos { 9871 1.14 christos if (c == 1 || c == 2) 9872 1.6 christos { 9873 1.6 christos if (c == 1 && p == name + 2) 9874 1.6 christos /* A fake symbol. */ 9875 1.6 christos return true; 9876 1.6 christos 9877 1.6 christos /* FIXME: We are being paranoid here and treating symbols like 9878 1.14 christos L0^Bfoo as if there were non-local, on the grounds that the 9879 1.6 christos assembler will never generate them. But can any symbol 9880 1.6 christos containing an ASCII value in the range 1-31 ever be anything 9881 1.6 christos other than some kind of local ? */ 9882 1.6 christos ret = true; 9883 1.14 christos } 9884 1.6 christos 9885 1.6 christos if (! ISDIGIT (c)) 9886 1.6 christos { 9887 1.6 christos ret = false; 9888 1.6 christos break; 9889 1.6 christos } 9890 1.14 christos } 9891 1.1 christos return ret; 9892 1.1 christos } 9893 1.1 christos 9894 1.1 christos return false; 9895 1.1 christos } 9896 1.1 christos 9897 1.1 christos alent * 9898 1.1 christos _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, 9899 1.1 christos asymbol *symbol ATTRIBUTE_UNUSED) 9900 1.1 christos { 9901 1.14 christos abort (); 9902 1.1 christos return NULL; 9903 1.1 christos } 9904 1.1 christos 9905 1.1 christos bool 9906 1.1 christos _bfd_elf_set_arch_mach (bfd *abfd, 9907 1.1 christos enum bfd_architecture arch, 9908 1.1 christos unsigned long machine) 9909 1.1 christos { 9910 1.1 christos /* If this isn't the right architecture for this backend, and this 9911 1.14 christos isn't the generic backend, fail. */ 9912 1.1 christos if (arch != get_elf_backend_data (abfd)->arch 9913 1.1 christos && arch != bfd_arch_unknown 9914 1.1 christos && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) 9915 1.1 christos return false; 9916 1.1 christos 9917 1.1 christos return bfd_default_set_arch_mach (abfd, arch, machine); 9918 1.1 christos } 9919 1.14 christos 9920 1.1 christos /* Find the nearest line to a particular section and offset, 9921 1.5 christos for error reporting. */ 9922 1.1 christos 9923 1.1 christos bool 9924 1.1 christos _bfd_elf_find_nearest_line (bfd *abfd, 9925 1.1 christos asymbol **symbols, 9926 1.5 christos asection *section, 9927 1.5 christos bfd_vma offset, 9928 1.3 christos const char **filename_ptr, 9929 1.14 christos const char **functionname_ptr, 9930 1.14 christos unsigned int *line_ptr, 9931 1.14 christos unsigned int *discriminator_ptr) 9932 1.14 christos { 9933 1.14 christos return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section, 9934 1.14 christos offset, filename_ptr, 9935 1.14 christos functionname_ptr, line_ptr, 9936 1.14 christos discriminator_ptr); 9937 1.14 christos } 9938 1.1 christos 9939 1.14 christos /* Find the nearest line to a particular section and offset, 9940 1.14 christos for error reporting. ALT_BFD representing a .gnu_debugaltlink file 9941 1.14 christos can be optionally specified. */ 9942 1.14 christos 9943 1.14 christos bool 9944 1.14 christos _bfd_elf_find_nearest_line_with_alt (bfd *abfd, 9945 1.14 christos const char *alt_filename, 9946 1.14 christos asymbol **symbols, 9947 1.14 christos asection *section, 9948 1.14 christos bfd_vma offset, 9949 1.14 christos const char **filename_ptr, 9950 1.14 christos const char **functionname_ptr, 9951 1.14 christos unsigned int *line_ptr, 9952 1.14 christos unsigned int *discriminator_ptr) 9953 1.14 christos { 9954 1.14 christos bool found; 9955 1.14 christos 9956 1.14 christos if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL, 9957 1.14 christos section, offset, filename_ptr, 9958 1.14 christos functionname_ptr, line_ptr, 9959 1.12 christos discriminator_ptr, 9960 1.12 christos dwarf_debug_sections, 9961 1.12 christos &elf_tdata (abfd)->dwarf2_find_line_info)) 9962 1.1 christos return true; 9963 1.1 christos 9964 1.5 christos if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, 9965 1.5 christos filename_ptr, functionname_ptr, line_ptr)) 9966 1.5 christos { 9967 1.14 christos if (!*functionname_ptr) 9968 1.1 christos _bfd_elf_find_function (abfd, symbols, section, offset, 9969 1.1 christos *filename_ptr ? NULL : filename_ptr, 9970 1.1 christos functionname_ptr); 9971 1.1 christos return true; 9972 1.1 christos } 9973 1.1 christos 9974 1.14 christos if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, 9975 1.1 christos &found, filename_ptr, 9976 1.14 christos functionname_ptr, line_ptr, 9977 1.1 christos &elf_tdata (abfd)->line_info)) 9978 1.1 christos return false; 9979 1.14 christos if (found && (*functionname_ptr || *line_ptr)) 9980 1.1 christos return true; 9981 1.5 christos 9982 1.5 christos if (symbols == NULL) 9983 1.14 christos return false; 9984 1.1 christos 9985 1.1 christos if (! _bfd_elf_find_function (abfd, symbols, section, offset, 9986 1.14 christos filename_ptr, functionname_ptr)) 9987 1.1 christos return false; 9988 1.1 christos 9989 1.1 christos *line_ptr = 0; 9990 1.1 christos return true; 9991 1.14 christos } 9992 1.1 christos 9993 1.1 christos /* Find the line for a symbol. */ 9994 1.1 christos 9995 1.14 christos bool 9996 1.5 christos _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, 9997 1.5 christos const char **filename_ptr, unsigned int *line_ptr) 9998 1.12 christos { 9999 1.14 christos struct elf_obj_tdata *tdata = elf_tdata (abfd); 10000 1.1 christos return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, 10001 1.1 christos filename_ptr, NULL, line_ptr, NULL, 10002 1.1 christos dwarf_debug_sections, 10003 1.1 christos &tdata->dwarf2_find_line_info); 10004 1.1 christos } 10005 1.1 christos 10006 1.1 christos /* After a call to bfd_find_nearest_line, successive calls to 10007 1.1 christos bfd_find_inliner_info can be used to get source information about 10008 1.14 christos each level of function inlining that terminated at the address 10009 1.1 christos passed to bfd_find_nearest_line. Currently this is only supported 10010 1.1 christos for DWARF2 with appropriate DWARF3 extensions. */ 10011 1.1 christos 10012 1.1 christos bool 10013 1.1 christos _bfd_elf_find_inliner_info (bfd *abfd, 10014 1.14 christos const char **filename_ptr, 10015 1.14 christos const char **functionname_ptr, 10016 1.14 christos unsigned int *line_ptr) 10017 1.14 christos { 10018 1.1 christos struct elf_obj_tdata *tdata = elf_tdata (abfd); 10019 1.1 christos return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, 10020 1.1 christos functionname_ptr, line_ptr, 10021 1.1 christos &tdata->dwarf2_find_line_info); 10022 1.1 christos } 10023 1.1 christos 10024 1.1 christos int 10025 1.1 christos _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) 10026 1.8 christos { 10027 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 10028 1.3 christos int ret = bed->s->sizeof_ehdr; 10029 1.1 christos 10030 1.1 christos if (!bfd_link_relocatable (info)) 10031 1.1 christos { 10032 1.1 christos bfd_size_type phdr_size = elf_program_header_size (abfd); 10033 1.1 christos 10034 1.1 christos if (phdr_size == (bfd_size_type) -1) 10035 1.3 christos { 10036 1.1 christos struct elf_segment_map *m; 10037 1.1 christos 10038 1.1 christos phdr_size = 0; 10039 1.1 christos for (m = elf_seg_map (abfd); m != NULL; m = m->next) 10040 1.1 christos phdr_size += bed->s->sizeof_phdr; 10041 1.1 christos 10042 1.3 christos if (phdr_size == 0) 10043 1.1 christos phdr_size = get_program_header_size (abfd, info); 10044 1.1 christos } 10045 1.1 christos 10046 1.1 christos elf_program_header_size (abfd) = phdr_size; 10047 1.1 christos ret += phdr_size; 10048 1.1 christos } 10049 1.14 christos 10050 1.1 christos return ret; 10051 1.1 christos } 10052 1.1 christos 10053 1.1 christos bool 10054 1.1 christos _bfd_elf_set_section_contents (bfd *abfd, 10055 1.1 christos sec_ptr section, 10056 1.1 christos const void *location, 10057 1.1 christos file_ptr offset, 10058 1.1 christos bfd_size_type count) 10059 1.1 christos { 10060 1.14 christos Elf_Internal_Shdr *hdr; 10061 1.1 christos 10062 1.6 christos if (! abfd->output_has_begun 10063 1.14 christos && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) 10064 1.6 christos return false; 10065 1.1 christos 10066 1.6 christos if (!count) 10067 1.6 christos return true; 10068 1.12 christos 10069 1.12 christos hdr = &elf_section_data (section)->this_hdr; 10070 1.12 christos if (hdr->sh_offset == (file_ptr) -1) 10071 1.12 christos { 10072 1.12 christos unsigned char *contents; 10073 1.14 christos 10074 1.12 christos if (bfd_section_is_ctf (section)) 10075 1.12 christos /* Nothing to do with this section: the contents are generated 10076 1.12 christos later. */ 10077 1.12 christos return true; 10078 1.14 christos 10079 1.14 christos if ((offset + count) > hdr->sh_size) 10080 1.12 christos { 10081 1.12 christos _bfd_error_handler 10082 1.12 christos (_("%pB:%pA: error: attempting to write" 10083 1.14 christos " over the end of the section"), 10084 1.12 christos abfd, section); 10085 1.12 christos 10086 1.12 christos bfd_set_error (bfd_error_invalid_operation); 10087 1.12 christos return false; 10088 1.12 christos } 10089 1.12 christos 10090 1.14 christos contents = hdr->contents; 10091 1.14 christos if (contents == NULL) 10092 1.12 christos { 10093 1.12 christos _bfd_error_handler 10094 1.12 christos (_("%pB:%pA: error: attempting to write" 10095 1.14 christos " section into an empty buffer"), 10096 1.12 christos abfd, section); 10097 1.12 christos 10098 1.6 christos bfd_set_error (bfd_error_invalid_operation); 10099 1.14 christos return false; 10100 1.6 christos } 10101 1.12 christos 10102 1.14 christos memcpy (contents + offset, location, count); 10103 1.14 christos return true; 10104 1.1 christos } 10105 1.1 christos 10106 1.14 christos return _bfd_generic_set_section_contents (abfd, section, 10107 1.1 christos location, offset, count); 10108 1.1 christos } 10109 1.1 christos 10110 1.1 christos bool 10111 1.1 christos _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, 10112 1.14 christos arelent *cache_ptr ATTRIBUTE_UNUSED, 10113 1.1 christos Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) 10114 1.1 christos { 10115 1.1 christos abort (); 10116 1.1 christos return false; 10117 1.14 christos } 10118 1.1 christos 10119 1.1 christos /* Try to convert a non-ELF reloc into an ELF one. */ 10120 1.1 christos 10121 1.1 christos bool 10122 1.1 christos _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) 10123 1.1 christos { 10124 1.1 christos /* Check whether we really have an ELF howto. */ 10125 1.1 christos 10126 1.1 christos if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) 10127 1.1 christos { 10128 1.1 christos bfd_reloc_code_real_type code; 10129 1.1 christos reloc_howto_type *howto; 10130 1.1 christos 10131 1.1 christos /* Alien reloc: Try to determine its type to replace it with an 10132 1.1 christos equivalent ELF reloc. */ 10133 1.1 christos 10134 1.1 christos if (areloc->howto->pc_relative) 10135 1.1 christos { 10136 1.1 christos switch (areloc->howto->bitsize) 10137 1.1 christos { 10138 1.1 christos case 8: 10139 1.1 christos code = BFD_RELOC_8_PCREL; 10140 1.1 christos break; 10141 1.1 christos case 12: 10142 1.1 christos code = BFD_RELOC_12_PCREL; 10143 1.1 christos break; 10144 1.1 christos case 16: 10145 1.1 christos code = BFD_RELOC_16_PCREL; 10146 1.1 christos break; 10147 1.1 christos case 24: 10148 1.1 christos code = BFD_RELOC_24_PCREL; 10149 1.1 christos break; 10150 1.1 christos case 32: 10151 1.1 christos code = BFD_RELOC_32_PCREL; 10152 1.1 christos break; 10153 1.1 christos case 64: 10154 1.1 christos code = BFD_RELOC_64_PCREL; 10155 1.1 christos break; 10156 1.1 christos default: 10157 1.1 christos goto fail; 10158 1.12 christos } 10159 1.1 christos 10160 1.1 christos howto = bfd_reloc_type_lookup (abfd, code); 10161 1.1 christos 10162 1.1 christos if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset) 10163 1.1 christos { 10164 1.1 christos if (howto->pcrel_offset) 10165 1.1 christos areloc->addend += areloc->address; 10166 1.1 christos else 10167 1.1 christos areloc->addend -= areloc->address; /* addend is unsigned!! */ 10168 1.1 christos } 10169 1.1 christos } 10170 1.1 christos else 10171 1.1 christos { 10172 1.1 christos switch (areloc->howto->bitsize) 10173 1.1 christos { 10174 1.1 christos case 8: 10175 1.1 christos code = BFD_RELOC_8; 10176 1.1 christos break; 10177 1.1 christos case 14: 10178 1.1 christos code = BFD_RELOC_14; 10179 1.1 christos break; 10180 1.1 christos case 16: 10181 1.1 christos code = BFD_RELOC_16; 10182 1.1 christos break; 10183 1.1 christos case 26: 10184 1.1 christos code = BFD_RELOC_26; 10185 1.1 christos break; 10186 1.1 christos case 32: 10187 1.1 christos code = BFD_RELOC_32; 10188 1.1 christos break; 10189 1.1 christos case 64: 10190 1.1 christos code = BFD_RELOC_64; 10191 1.1 christos break; 10192 1.1 christos default: 10193 1.1 christos goto fail; 10194 1.1 christos } 10195 1.1 christos 10196 1.1 christos howto = bfd_reloc_type_lookup (abfd, code); 10197 1.1 christos } 10198 1.1 christos 10199 1.1 christos if (howto) 10200 1.1 christos areloc->howto = howto; 10201 1.14 christos else 10202 1.1 christos goto fail; 10203 1.1 christos } 10204 1.11 christos 10205 1.11 christos return true; 10206 1.11 christos 10207 1.12 christos fail: 10208 1.14 christos /* xgettext:c-format */ 10209 1.1 christos _bfd_error_handler (_("%pB: %s unsupported"), 10210 1.1 christos abfd, areloc->howto->name); 10211 1.14 christos bfd_set_error (bfd_error_sorry); 10212 1.17 christos return false; 10213 1.1 christos } 10214 1.17 christos 10215 1.17 christos bool 10216 1.17 christos _bfd_elf_free_cached_info (bfd *abfd) 10217 1.17 christos { 10218 1.17 christos struct elf_obj_tdata *tdata; 10219 1.1 christos 10220 1.17 christos if ((bfd_get_format (abfd) == bfd_object 10221 1.1 christos || bfd_get_format (abfd) == bfd_core) 10222 1.3 christos && (tdata = elf_tdata (abfd)) != NULL) 10223 1.17 christos { 10224 1.14 christos if (tdata->o != NULL && elf_shstrtab (abfd) != NULL) 10225 1.19 christos _bfd_elf_strtab_free (elf_shstrtab (abfd)); 10226 1.19 christos _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); 10227 1.19 christos _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info); 10228 1.19 christos _bfd_stab_cleanup (abfd, &tdata->line_info); 10229 1.19 christos for (asection *sec = abfd->sections; sec != NULL; sec = sec->next) 10230 1.19 christos { 10231 1.19 christos _bfd_elf_munmap_section_contents (sec, sec->contents); 10232 1.19 christos if (!sec->alloced) 10233 1.19 christos { 10234 1.19 christos free (elf_section_data (sec)->this_hdr.contents); 10235 1.19 christos elf_section_data (sec)->this_hdr.contents = NULL; 10236 1.19 christos } 10237 1.19 christos free (elf_section_data (sec)->relocs); 10238 1.19 christos elf_section_data (sec)->relocs = NULL; 10239 1.19 christos if (sec->sec_info_type == SEC_INFO_TYPE_EH_FRAME) 10240 1.19 christos { 10241 1.19 christos struct eh_frame_sec_info *sec_info 10242 1.19 christos = elf_section_data (sec)->sec_info; 10243 1.19 christos free (sec_info->cies); 10244 1.1 christos } 10245 1.1 christos } 10246 1.17 christos free (tdata->symtab_hdr.contents); 10247 1.1 christos tdata->symtab_hdr.contents = NULL; 10248 1.1 christos } 10249 1.1 christos 10250 1.1 christos return _bfd_generic_bfd_free_cached_info (abfd); 10251 1.1 christos } 10252 1.1 christos 10253 1.1 christos /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY 10254 1.1 christos in the relocation's offset. Thus we cannot allow any sort of sanity 10255 1.1 christos range-checking to interfere. There is nothing else to do in processing 10256 1.1 christos this reloc. */ 10257 1.1 christos 10258 1.1 christos bfd_reloc_status_type 10259 1.1 christos _bfd_elf_rel_vtable_reloc_fn 10260 1.1 christos (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, 10261 1.1 christos struct bfd_symbol *symbol ATTRIBUTE_UNUSED, 10262 1.1 christos void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, 10263 1.1 christos bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) 10264 1.1 christos { 10265 1.1 christos return bfd_reloc_ok; 10266 1.1 christos } 10267 1.1 christos 10268 1.1 christos /* Elf core file support. Much of this only works on native 10270 1.1 christos toolchains, since we rely on knowing the 10271 1.1 christos machine-dependent procfs structure in order to pick 10272 1.1 christos out details about the corefile. */ 10273 1.1 christos 10274 1.1 christos #ifdef HAVE_SYS_PROCFS_H 10275 1.1 christos # include <sys/procfs.h> 10276 1.1 christos #endif 10277 1.1 christos 10278 1.1 christos /* Return a PID that identifies a "thread" for threaded cores, or the 10279 1.1 christos PID of the main process for non-threaded cores. */ 10280 1.1 christos 10281 1.3 christos static int 10282 1.1 christos elfcore_make_pid (bfd *abfd) 10283 1.3 christos { 10284 1.1 christos int pid; 10285 1.1 christos 10286 1.1 christos pid = elf_tdata (abfd)->core->lwpid; 10287 1.1 christos if (pid == 0) 10288 1.14 christos pid = elf_tdata (abfd)->core->pid; 10289 1.14 christos 10290 1.14 christos return pid; 10291 1.1 christos } 10292 1.14 christos 10293 1.1 christos /* If there isn't a section called NAME, make one, using data from 10294 1.1 christos SECT. Note, this function will generate a reference to NAME, so 10295 1.1 christos you shouldn't deallocate or overwrite it. */ 10296 1.1 christos 10297 1.1 christos static bool 10298 1.14 christos elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) 10299 1.1 christos { 10300 1.1 christos asection *sect2; 10301 1.1 christos 10302 1.14 christos if (bfd_get_section_by_name (abfd, name) != NULL) 10303 1.1 christos return true; 10304 1.1 christos 10305 1.1 christos sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); 10306 1.1 christos if (sect2 == NULL) 10307 1.14 christos return false; 10308 1.1 christos 10309 1.1 christos sect2->size = sect->size; 10310 1.1 christos sect2->filepos = sect->filepos; 10311 1.1 christos sect2->alignment_power = sect->alignment_power; 10312 1.1 christos return true; 10313 1.1 christos } 10314 1.1 christos 10315 1.1 christos /* Create a pseudosection containing SIZE bytes at FILEPOS. This 10316 1.11 christos actually creates up to two pseudosections: 10317 1.14 christos - For the single-threaded case, a section named NAME, unless 10318 1.1 christos such a section already exists. 10319 1.1 christos - For the multi-threaded case, a section named "NAME/PID", where 10320 1.1 christos PID is elfcore_make_pid (abfd). 10321 1.1 christos Both pseudosections have identical contents. */ 10322 1.1 christos bool 10323 1.1 christos _bfd_elfcore_make_pseudosection (bfd *abfd, 10324 1.1 christos char *name, 10325 1.1 christos size_t size, 10326 1.1 christos ufile_ptr filepos) 10327 1.1 christos { 10328 1.1 christos char buf[100]; 10329 1.1 christos char *threaded_name; 10330 1.1 christos size_t len; 10331 1.1 christos asection *sect; 10332 1.1 christos 10333 1.1 christos /* Build the section name. */ 10334 1.14 christos 10335 1.1 christos sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); 10336 1.1 christos len = strlen (buf) + 1; 10337 1.1 christos threaded_name = (char *) bfd_alloc (abfd, len); 10338 1.1 christos if (threaded_name == NULL) 10339 1.1 christos return false; 10340 1.14 christos memcpy (threaded_name, buf, len); 10341 1.1 christos 10342 1.1 christos sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, 10343 1.1 christos SEC_HAS_CONTENTS); 10344 1.1 christos if (sect == NULL) 10345 1.1 christos return false; 10346 1.1 christos sect->size = size; 10347 1.1 christos sect->filepos = filepos; 10348 1.14 christos sect->alignment_power = 2; 10349 1.12 christos 10350 1.12 christos return elfcore_maybe_make_sect (abfd, name, sect); 10351 1.12 christos } 10352 1.12 christos 10353 1.12 christos static bool 10354 1.12 christos elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note, 10355 1.12 christos size_t offs) 10356 1.14 christos { 10357 1.12 christos asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", 10358 1.12 christos SEC_HAS_CONTENTS); 10359 1.12 christos 10360 1.12 christos if (sect == NULL) 10361 1.12 christos return false; 10362 1.14 christos 10363 1.12 christos sect->size = note->descsz - offs; 10364 1.12 christos sect->filepos = note->descpos + offs; 10365 1.1 christos sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; 10366 1.1 christos 10367 1.1 christos return true; 10368 1.1 christos } 10369 1.1 christos 10370 1.1 christos /* prstatus_t exists on: 10371 1.1 christos solaris 2.5+ 10372 1.1 christos linux 2.[01] + glibc 10373 1.14 christos unixware 4.2 10374 1.1 christos */ 10375 1.1 christos 10376 1.1 christos #if defined (HAVE_PRSTATUS_T) 10377 1.1 christos 10378 1.1 christos static bool 10379 1.1 christos elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) 10380 1.1 christos { 10381 1.1 christos size_t size; 10382 1.1 christos int offset; 10383 1.1 christos 10384 1.1 christos if (note->descsz == sizeof (prstatus_t)) 10385 1.1 christos { 10386 1.1 christos prstatus_t prstat; 10387 1.1 christos 10388 1.1 christos size = sizeof (prstat.pr_reg); 10389 1.3 christos offset = offsetof (prstatus_t, pr_reg); 10390 1.3 christos memcpy (&prstat, note->descdata, sizeof (prstat)); 10391 1.3 christos 10392 1.3 christos /* Do not overwrite the core signal if it 10393 1.1 christos has already been set by another thread. */ 10394 1.1 christos if (elf_tdata (abfd)->core->signal == 0) 10395 1.1 christos elf_tdata (abfd)->core->signal = prstat.pr_cursig; 10396 1.1 christos if (elf_tdata (abfd)->core->pid == 0) 10397 1.1 christos elf_tdata (abfd)->core->pid = prstat.pr_pid; 10398 1.1 christos 10399 1.1 christos /* pr_who exists on: 10400 1.1 christos solaris 2.5+ 10401 1.3 christos unixware 4.2 10402 1.1 christos pr_who doesn't exist on: 10403 1.3 christos linux 2.[01] 10404 1.1 christos */ 10405 1.1 christos #if defined (HAVE_PRSTATUS_T_PR_WHO) 10406 1.1 christos elf_tdata (abfd)->core->lwpid = prstat.pr_who; 10407 1.1 christos #else 10408 1.1 christos elf_tdata (abfd)->core->lwpid = prstat.pr_pid; 10409 1.1 christos #endif 10410 1.1 christos } 10411 1.1 christos #if defined (HAVE_PRSTATUS32_T) 10412 1.1 christos else if (note->descsz == sizeof (prstatus32_t)) 10413 1.1 christos { 10414 1.1 christos /* 64-bit host, 32-bit corefile */ 10415 1.1 christos prstatus32_t prstat; 10416 1.1 christos 10417 1.1 christos size = sizeof (prstat.pr_reg); 10418 1.3 christos offset = offsetof (prstatus32_t, pr_reg); 10419 1.3 christos memcpy (&prstat, note->descdata, sizeof (prstat)); 10420 1.3 christos 10421 1.3 christos /* Do not overwrite the core signal if it 10422 1.1 christos has already been set by another thread. */ 10423 1.1 christos if (elf_tdata (abfd)->core->signal == 0) 10424 1.1 christos elf_tdata (abfd)->core->signal = prstat.pr_cursig; 10425 1.1 christos if (elf_tdata (abfd)->core->pid == 0) 10426 1.1 christos elf_tdata (abfd)->core->pid = prstat.pr_pid; 10427 1.1 christos 10428 1.1 christos /* pr_who exists on: 10429 1.1 christos solaris 2.5+ 10430 1.3 christos unixware 4.2 10431 1.1 christos pr_who doesn't exist on: 10432 1.3 christos linux 2.[01] 10433 1.1 christos */ 10434 1.1 christos #if defined (HAVE_PRSTATUS32_T_PR_WHO) 10435 1.1 christos elf_tdata (abfd)->core->lwpid = prstat.pr_who; 10436 1.1 christos #else 10437 1.1 christos elf_tdata (abfd)->core->lwpid = prstat.pr_pid; 10438 1.1 christos #endif 10439 1.1 christos } 10440 1.14 christos #endif /* HAVE_PRSTATUS32_T */ 10441 1.1 christos else 10442 1.1 christos { 10443 1.1 christos /* Fail - we don't know how to handle any other 10444 1.19 christos note size (ie. data object type). */ 10445 1.1 christos return true; 10446 1.1 christos } 10447 1.1 christos 10448 1.1 christos /* Make a ".reg/999" section and a ".reg" section. */ 10449 1.1 christos return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, 10450 1.14 christos size, note->descpos + offset); 10451 1.1 christos } 10452 1.1 christos #endif /* defined (HAVE_PRSTATUS_T) */ 10453 1.1 christos 10454 1.1 christos /* Create a pseudosection containing the exact contents of NOTE. */ 10455 1.1 christos static bool 10456 1.1 christos elfcore_make_note_pseudosection (bfd *abfd, 10457 1.1 christos char *name, 10458 1.1 christos Elf_Internal_Note *note) 10459 1.1 christos { 10460 1.1 christos return _bfd_elfcore_make_pseudosection (abfd, name, 10461 1.1 christos note->descsz, note->descpos); 10462 1.1 christos } 10463 1.14 christos 10464 1.1 christos /* There isn't a consistent prfpregset_t across platforms, 10465 1.1 christos but it doesn't matter, because we don't have to pick this 10466 1.19 christos data structure apart. */ 10467 1.1 christos 10468 1.1 christos static bool 10469 1.1 christos elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) 10470 1.1 christos { 10471 1.1 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note); 10472 1.1 christos } 10473 1.14 christos 10474 1.1 christos /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note 10475 1.1 christos type of NT_PRXFPREG. Just include the whole note's contents 10476 1.19 christos literally. */ 10477 1.1 christos 10478 1.1 christos static bool 10479 1.1 christos elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) 10480 1.1 christos { 10481 1.1 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XFP, note); 10482 1.1 christos } 10483 1.14 christos 10484 1.1 christos /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" 10485 1.1 christos with a note type of NT_X86_XSTATE. Just include the whole note's 10486 1.19 christos contents literally. */ 10487 1.19 christos 10488 1.19 christos static bool 10489 1.19 christos elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) 10490 1.19 christos { 10491 1.19 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XSTATE, note); 10492 1.19 christos } 10493 1.1 christos 10494 1.1 christos static bool 10495 1.14 christos elfcore_grok_sspreg (bfd *abfd, Elf_Internal_Note *note) 10496 1.1 christos { 10497 1.1 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_SSP, note); 10498 1.19 christos } 10499 1.1 christos 10500 1.1 christos static bool 10501 1.14 christos elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) 10502 1.1 christos { 10503 1.1 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_VMX, note); 10504 1.19 christos } 10505 1.1 christos 10506 1.1 christos static bool 10507 1.14 christos elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) 10508 1.11 christos { 10509 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_VSX, note); 10510 1.19 christos } 10511 1.11 christos 10512 1.11 christos static bool 10513 1.14 christos elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note) 10514 1.11 christos { 10515 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TAR, note); 10516 1.19 christos } 10517 1.11 christos 10518 1.11 christos static bool 10519 1.14 christos elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note) 10520 1.11 christos { 10521 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_PPR, note); 10522 1.19 christos } 10523 1.11 christos 10524 1.11 christos static bool 10525 1.14 christos elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note) 10526 1.11 christos { 10527 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_DSCR, note); 10528 1.19 christos } 10529 1.11 christos 10530 1.11 christos static bool 10531 1.14 christos elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note) 10532 1.11 christos { 10533 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_EBB, note); 10534 1.19 christos } 10535 1.11 christos 10536 1.11 christos static bool 10537 1.14 christos elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note) 10538 1.11 christos { 10539 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_PMU, note); 10540 1.19 christos } 10541 1.11 christos 10542 1.11 christos static bool 10543 1.14 christos elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note) 10544 1.11 christos { 10545 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CGPR, note); 10546 1.19 christos } 10547 1.11 christos 10548 1.11 christos static bool 10549 1.14 christos elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note) 10550 1.11 christos { 10551 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CFPR, note); 10552 1.19 christos } 10553 1.11 christos 10554 1.11 christos static bool 10555 1.14 christos elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note) 10556 1.11 christos { 10557 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CVMX, note); 10558 1.19 christos } 10559 1.11 christos 10560 1.11 christos static bool 10561 1.14 christos elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note) 10562 1.11 christos { 10563 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CVSX, note); 10564 1.19 christos } 10565 1.11 christos 10566 1.11 christos static bool 10567 1.14 christos elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note) 10568 1.11 christos { 10569 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_SPR, note); 10570 1.19 christos } 10571 1.11 christos 10572 1.11 christos static bool 10573 1.14 christos elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note) 10574 1.11 christos { 10575 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CTAR, note); 10576 1.19 christos } 10577 1.11 christos 10578 1.11 christos static bool 10579 1.14 christos elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note) 10580 1.11 christos { 10581 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CPPR, note); 10582 1.19 christos } 10583 1.11 christos 10584 1.11 christos static bool 10585 1.14 christos elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note) 10586 1.1 christos { 10587 1.1 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_PPC_TM_CDSCR, note); 10588 1.19 christos } 10589 1.1 christos 10590 1.1 christos static bool 10591 1.14 christos elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) 10592 1.1 christos { 10593 1.1 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_HIGH_GPRS, note); 10594 1.19 christos } 10595 1.1 christos 10596 1.1 christos static bool 10597 1.14 christos elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) 10598 1.1 christos { 10599 1.1 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TIMER, note); 10600 1.19 christos } 10601 1.1 christos 10602 1.1 christos static bool 10603 1.14 christos elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) 10604 1.1 christos { 10605 1.1 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TODCMP, note); 10606 1.19 christos } 10607 1.1 christos 10608 1.1 christos static bool 10609 1.14 christos elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) 10610 1.1 christos { 10611 1.1 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TODPREG, note); 10612 1.19 christos } 10613 1.1 christos 10614 1.1 christos static bool 10615 1.14 christos elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) 10616 1.1 christos { 10617 1.1 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_CTRS, note); 10618 1.19 christos } 10619 1.1 christos 10620 1.1 christos static bool 10621 1.14 christos elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) 10622 1.3 christos { 10623 1.3 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_PREFIX, note); 10624 1.19 christos } 10625 1.3 christos 10626 1.3 christos static bool 10627 1.14 christos elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) 10628 1.3 christos { 10629 1.3 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_LAST_BREAK, note); 10630 1.19 christos } 10631 1.3 christos 10632 1.3 christos static bool 10633 1.14 christos elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) 10634 1.3 christos { 10635 1.3 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL, note); 10636 1.19 christos } 10637 1.3 christos 10638 1.3 christos static bool 10639 1.14 christos elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) 10640 1.6 christos { 10641 1.6 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_TDB, note); 10642 1.19 christos } 10643 1.6 christos 10644 1.6 christos static bool 10645 1.14 christos elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) 10646 1.6 christos { 10647 1.6 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_VXRS_LOW, note); 10648 1.19 christos } 10649 1.6 christos 10650 1.6 christos static bool 10651 1.14 christos elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) 10652 1.11 christos { 10653 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_VXRS_HIGH, note); 10654 1.19 christos } 10655 1.11 christos 10656 1.11 christos static bool 10657 1.14 christos elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note) 10658 1.11 christos { 10659 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_GS_CB, note); 10660 1.19 christos } 10661 1.11 christos 10662 1.11 christos static bool 10663 1.14 christos elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note) 10664 1.3 christos { 10665 1.3 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_S390_GS_BC, note); 10666 1.19 christos } 10667 1.3 christos 10668 1.3 christos static bool 10669 1.14 christos elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) 10670 1.3 christos { 10671 1.3 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_ARM_VFP, note); 10672 1.19 christos } 10673 1.3 christos 10674 1.3 christos static bool 10675 1.14 christos elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) 10676 1.3 christos { 10677 1.3 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_TLS, note); 10678 1.19 christos } 10679 1.3 christos 10680 1.3 christos static bool 10681 1.14 christos elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) 10682 1.3 christos { 10683 1.3 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_HW_BREAK, note); 10684 1.19 christos } 10685 1.3 christos 10686 1.3 christos static bool 10687 1.14 christos elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) 10688 1.11 christos { 10689 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_HW_WATCH, note); 10690 1.19 christos } 10691 1.11 christos 10692 1.11 christos static bool 10693 1.14 christos elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note) 10694 1.11 christos { 10695 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_SVE, note); 10696 1.19 christos } 10697 1.11 christos 10698 1.11 christos static bool 10699 1.14 christos elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note) 10700 1.14 christos { 10701 1.14 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_PAUTH, note); 10702 1.19 christos } 10703 1.14 christos 10704 1.14 christos static bool 10705 1.14 christos elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note) 10706 1.17 christos { 10707 1.17 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_MTE, note); 10708 1.19 christos } 10709 1.17 christos 10710 1.17 christos static bool 10711 1.17 christos elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note) 10712 1.17 christos { 10713 1.17 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_SSVE, note); 10714 1.19 christos } 10715 1.17 christos 10716 1.17 christos static bool 10717 1.17 christos elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note) 10718 1.17 christos { 10719 1.17 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_ZA, note); 10720 1.17 christos } 10721 1.17 christos 10722 1.17 christos /* Convert NOTE into a bfd_section called ".reg-aarch-zt". Return TRUE if 10723 1.19 christos successful, otherwise return FALSE. */ 10724 1.19 christos 10725 1.19 christos static bool 10726 1.19 christos elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note) 10727 1.19 christos { 10728 1.19 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_ZT, note); 10729 1.19 christos } 10730 1.19 christos 10731 1.19 christos /* Convert NOTE into a bfd_section called ".reg-aarch-gcs". Return TRUE if 10732 1.19 christos successful, otherwise return FALSE. */ 10733 1.17 christos 10734 1.17 christos static bool 10735 1.17 christos elfcore_grok_aarch_gcs (bfd *abfd, Elf_Internal_Note *note) 10736 1.12 christos { 10737 1.12 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_AARCH_GCS, note); 10738 1.19 christos } 10739 1.12 christos 10740 1.12 christos static bool 10741 1.14 christos elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note) 10742 1.14 christos { 10743 1.14 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_ARC_V2, note); 10744 1.14 christos } 10745 1.14 christos 10746 1.14 christos /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if 10747 1.19 christos successful otherwise, return FALSE. */ 10748 1.14 christos 10749 1.14 christos static bool 10750 1.14 christos elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note) 10751 1.14 christos { 10752 1.14 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_RISCV_CSR, note); 10753 1.14 christos } 10754 1.14 christos 10755 1.14 christos /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if 10756 1.14 christos successful otherwise, return FALSE. */ 10757 1.14 christos 10758 1.14 christos static bool 10759 1.14 christos elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note) 10760 1.19 christos { 10761 1.19 christos return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note); 10762 1.19 christos } 10763 1.19 christos 10764 1.19 christos static bool 10765 1.19 christos elfcore_grok_i386_tls (bfd *abfd, Elf_Internal_Note *note) 10766 1.19 christos { 10767 1.14 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_I386_TLS, 10768 1.14 christos note); 10769 1.19 christos } 10770 1.14 christos 10771 1.14 christos static bool 10772 1.14 christos elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note) 10773 1.14 christos { 10774 1.14 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG, note); 10775 1.19 christos } 10776 1.14 christos 10777 1.14 christos static bool 10778 1.14 christos elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note) 10779 1.14 christos { 10780 1.14 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LBT, note); 10781 1.19 christos } 10782 1.14 christos 10783 1.14 christos static bool 10784 1.14 christos elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note) 10785 1.14 christos { 10786 1.14 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LSX, note); 10787 1.19 christos } 10788 1.14 christos 10789 1.14 christos static bool 10790 1.1 christos elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note) 10791 1.1 christos { 10792 1.1 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_LOONGARCH_LASX, note); 10793 1.1 christos } 10794 1.1 christos 10795 1.1 christos #if defined (HAVE_PRPSINFO_T) 10796 1.1 christos typedef prpsinfo_t elfcore_psinfo_t; 10797 1.1 christos #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ 10798 1.1 christos typedef prpsinfo32_t elfcore_psinfo32_t; 10799 1.1 christos #endif 10800 1.1 christos #endif 10801 1.1 christos 10802 1.1 christos #if defined (HAVE_PSINFO_T) 10803 1.1 christos typedef psinfo_t elfcore_psinfo_t; 10804 1.1 christos #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ 10805 1.1 christos typedef psinfo32_t elfcore_psinfo32_t; 10806 1.1 christos #endif 10807 1.1 christos #endif 10808 1.1 christos 10809 1.1 christos /* return a malloc'ed copy of a string at START which is at 10810 1.1 christos most MAX bytes long, possibly without a terminating '\0'. 10811 1.1 christos the copy will always have a terminating '\0'. */ 10812 1.1 christos 10813 1.1 christos char * 10814 1.1 christos _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) 10815 1.1 christos { 10816 1.1 christos char *dups; 10817 1.1 christos char *end = (char *) memchr (start, '\0', max); 10818 1.1 christos size_t len; 10819 1.1 christos 10820 1.1 christos if (end == NULL) 10821 1.1 christos len = max; 10822 1.1 christos else 10823 1.1 christos len = end - start; 10824 1.1 christos 10825 1.1 christos dups = (char *) bfd_alloc (abfd, len + 1); 10826 1.1 christos if (dups == NULL) 10827 1.1 christos return NULL; 10828 1.1 christos 10829 1.1 christos memcpy (dups, start, len); 10830 1.1 christos dups[len] = '\0'; 10831 1.14 christos 10832 1.1 christos return dups; 10833 1.1 christos } 10834 1.1 christos 10835 1.1 christos #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 10836 1.1 christos static bool 10837 1.1 christos elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) 10838 1.1 christos { 10839 1.1 christos if (note->descsz == sizeof (elfcore_psinfo_t)) 10840 1.3 christos { 10841 1.3 christos elfcore_psinfo_t psinfo; 10842 1.3 christos 10843 1.3 christos memcpy (&psinfo, note->descdata, sizeof (psinfo)); 10844 1.1 christos 10845 1.1 christos #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) 10846 1.1 christos elf_tdata (abfd)->core->pid = psinfo.pr_pid; 10847 1.3 christos #endif 10848 1.1 christos elf_tdata (abfd)->core->program 10849 1.1 christos = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, 10850 1.1 christos sizeof (psinfo.pr_fname)); 10851 1.1 christos 10852 1.1 christos elf_tdata (abfd)->core->command 10853 1.1 christos = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, 10854 1.1 christos sizeof (psinfo.pr_psargs)); 10855 1.1 christos } 10856 1.1 christos #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) 10857 1.1 christos else if (note->descsz == sizeof (elfcore_psinfo32_t)) 10858 1.1 christos { 10859 1.3 christos /* 64-bit host, 32-bit corefile */ 10860 1.3 christos elfcore_psinfo32_t psinfo; 10861 1.3 christos 10862 1.3 christos memcpy (&psinfo, note->descdata, sizeof (psinfo)); 10863 1.1 christos 10864 1.1 christos #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) 10865 1.1 christos elf_tdata (abfd)->core->pid = psinfo.pr_pid; 10866 1.3 christos #endif 10867 1.1 christos elf_tdata (abfd)->core->program 10868 1.1 christos = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, 10869 1.1 christos sizeof (psinfo.pr_fname)); 10870 1.1 christos 10871 1.1 christos elf_tdata (abfd)->core->command 10872 1.1 christos = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, 10873 1.1 christos sizeof (psinfo.pr_psargs)); 10874 1.1 christos } 10875 1.1 christos #endif 10876 1.14 christos 10877 1.1 christos else 10878 1.1 christos { 10879 1.1 christos /* Fail - we don't know how to handle any other 10880 1.1 christos note size (ie. data object type). */ 10881 1.1 christos return true; 10882 1.1 christos } 10883 1.1 christos 10884 1.3 christos /* Note that for some reason, a spurious space is tacked 10885 1.1 christos onto the end of the args in some (at least one anyway) 10886 1.1 christos implementations, so strip it off if it exists. */ 10887 1.1 christos 10888 1.1 christos { 10889 1.1 christos char *command = elf_tdata (abfd)->core->command; 10890 1.1 christos int n = strlen (command); 10891 1.14 christos 10892 1.1 christos if (0 < n && command[n - 1] == ' ') 10893 1.1 christos command[n - 1] = '\0'; 10894 1.1 christos } 10895 1.1 christos 10896 1.14 christos return true; 10897 1.1 christos } 10898 1.1 christos #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ 10899 1.1 christos 10900 1.1 christos #if defined (HAVE_PSTATUS_T) 10901 1.1 christos static bool 10902 1.1 christos elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) 10903 1.1 christos { 10904 1.1 christos if (note->descsz == sizeof (pstatus_t) 10905 1.1 christos #if defined (HAVE_PXSTATUS_T) 10906 1.1 christos || note->descsz == sizeof (pxstatus_t) 10907 1.1 christos #endif 10908 1.1 christos ) 10909 1.3 christos { 10910 1.1 christos pstatus_t pstat; 10911 1.1 christos 10912 1.1 christos memcpy (&pstat, note->descdata, sizeof (pstat)); 10913 1.1 christos 10914 1.1 christos elf_tdata (abfd)->core->pid = pstat.pr_pid; 10915 1.1 christos } 10916 1.1 christos #if defined (HAVE_PSTATUS32_T) 10917 1.1 christos else if (note->descsz == sizeof (pstatus32_t)) 10918 1.1 christos { 10919 1.3 christos /* 64-bit host, 32-bit corefile */ 10920 1.1 christos pstatus32_t pstat; 10921 1.1 christos 10922 1.1 christos memcpy (&pstat, note->descdata, sizeof (pstat)); 10923 1.1 christos 10924 1.1 christos elf_tdata (abfd)->core->pid = pstat.pr_pid; 10925 1.1 christos } 10926 1.14 christos #endif 10927 1.1 christos /* Could grab some more details from the "representative" 10928 1.1 christos lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an 10929 1.1 christos NT_LWPSTATUS note, presumably. */ 10930 1.1 christos 10931 1.14 christos return true; 10932 1.1 christos } 10933 1.1 christos #endif /* defined (HAVE_PSTATUS_T) */ 10934 1.1 christos 10935 1.1 christos #if defined (HAVE_LWPSTATUS_T) 10936 1.1 christos static bool 10937 1.1 christos elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) 10938 1.1 christos { 10939 1.1 christos lwpstatus_t lwpstat; 10940 1.1 christos char buf[100]; 10941 1.1 christos char *name; 10942 1.1 christos size_t len; 10943 1.1 christos asection *sect; 10944 1.1 christos 10945 1.14 christos if (note->descsz != sizeof (lwpstat) 10946 1.1 christos #if defined (HAVE_LWPXSTATUS_T) 10947 1.1 christos && note->descsz != sizeof (lwpxstatus_t) 10948 1.1 christos #endif 10949 1.3 christos ) 10950 1.1 christos return true; 10951 1.1 christos 10952 1.3 christos memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); 10953 1.3 christos 10954 1.1 christos elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; 10955 1.1 christos /* Do not overwrite the core signal if it has already been set by 10956 1.1 christos another thread. */ 10957 1.19 christos if (elf_tdata (abfd)->core->signal == 0) 10958 1.1 christos elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; 10959 1.1 christos 10960 1.1 christos /* Make a ".reg/999" section. */ 10961 1.14 christos 10962 1.1 christos sprintf (buf, NOTE_PSEUDO_SECTION_REG "/%d", elfcore_make_pid (abfd)); 10963 1.1 christos len = strlen (buf) + 1; 10964 1.1 christos name = bfd_alloc (abfd, len); 10965 1.1 christos if (name == NULL) 10966 1.14 christos return false; 10967 1.1 christos memcpy (name, buf, len); 10968 1.1 christos 10969 1.1 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 10970 1.1 christos if (sect == NULL) 10971 1.1 christos return false; 10972 1.1 christos 10973 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 10974 1.1 christos sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); 10975 1.1 christos sect->filepos = note->descpos 10976 1.1 christos + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); 10977 1.1 christos #endif 10978 1.1 christos 10979 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_REG) 10980 1.1 christos sect->size = sizeof (lwpstat.pr_reg); 10981 1.19 christos sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); 10982 1.14 christos #endif 10983 1.1 christos 10984 1.1 christos sect->alignment_power = 2; 10985 1.1 christos 10986 1.19 christos if (!elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG, sect)) 10987 1.1 christos return false; 10988 1.1 christos 10989 1.1 christos /* Make a ".reg2/999" section */ 10990 1.14 christos 10991 1.1 christos sprintf (buf, NOTE_PSEUDO_SECTION_REG2 "/%d", elfcore_make_pid (abfd)); 10992 1.1 christos len = strlen (buf) + 1; 10993 1.1 christos name = bfd_alloc (abfd, len); 10994 1.1 christos if (name == NULL) 10995 1.14 christos return false; 10996 1.1 christos memcpy (name, buf, len); 10997 1.1 christos 10998 1.1 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 10999 1.1 christos if (sect == NULL) 11000 1.1 christos return false; 11001 1.1 christos 11002 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 11003 1.1 christos sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); 11004 1.1 christos sect->filepos = note->descpos 11005 1.1 christos + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); 11006 1.1 christos #endif 11007 1.1 christos 11008 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_FPREG) 11009 1.1 christos sect->size = sizeof (lwpstat.pr_fpreg); 11010 1.19 christos sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); 11011 1.1 christos #endif 11012 1.1 christos 11013 1.1 christos sect->alignment_power = 2; 11014 1.12 christos 11015 1.12 christos return elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG2, sect); 11016 1.12 christos } 11017 1.12 christos #endif /* defined (HAVE_LWPSTATUS_T) */ 11018 1.12 christos 11019 1.12 christos /* These constants, and the structure offsets used below, are defined by 11020 1.12 christos Cygwin's core_dump.h */ 11021 1.14 christos #define NOTE_INFO_PROCESS 1 11022 1.1 christos #define NOTE_INFO_THREAD 2 11023 1.1 christos #define NOTE_INFO_MODULE 3 11024 1.1 christos #define NOTE_INFO_MODULE64 4 11025 1.1 christos 11026 1.1 christos static bool 11027 1.12 christos elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) 11028 1.1 christos { 11029 1.12 christos char buf[30]; 11030 1.1 christos char *name; 11031 1.1 christos size_t len; 11032 1.1 christos unsigned int name_size; 11033 1.12 christos asection *sect; 11034 1.14 christos unsigned int type; 11035 1.1 christos int is_active_thread; 11036 1.14 christos bfd_vma base_addr; 11037 1.14 christos 11038 1.1 christos if (note->descsz < 4) 11039 1.1 christos return true; 11040 1.1 christos 11041 1.19 christos if (! startswith (note->namedata, "win32")) 11042 1.14 christos return true; 11043 1.12 christos 11044 1.12 christos type = bfd_get_32 (abfd, note->descdata); 11045 1.12 christos 11046 1.12 christos static const struct 11047 1.12 christos { 11048 1.12 christos const char *type_name; 11049 1.12 christos unsigned long min_size; 11050 1.12 christos } size_check[] = 11051 1.12 christos { 11052 1.12 christos { "NOTE_INFO_PROCESS", 12 }, 11053 1.14 christos { "NOTE_INFO_THREAD", 12 }, 11054 1.14 christos { "NOTE_INFO_MODULE", 12 }, 11055 1.12 christos { "NOTE_INFO_MODULE64", 16 }, 11056 1.12 christos }; 11057 1.12 christos 11058 1.14 christos if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0]))) 11059 1.14 christos return true; 11060 1.14 christos 11061 1.14 christos if (note->descsz < size_check[type - 1].min_size) 11062 1.12 christos { 11063 1.12 christos _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes" 11064 1.1 christos " is too small"), 11065 1.1 christos abfd, size_check[type - 1].type_name, note->descsz); 11066 1.12 christos return true; 11067 1.3 christos } 11068 1.12 christos 11069 1.12 christos switch (type) 11070 1.1 christos { 11071 1.1 christos case NOTE_INFO_PROCESS: 11072 1.12 christos /* FIXME: need to add ->core->command. */ 11073 1.12 christos elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4); 11074 1.14 christos elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8); 11075 1.1 christos break; 11076 1.19 christos 11077 1.1 christos case NOTE_INFO_THREAD: 11078 1.1 christos /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT 11079 1.1 christos structure. */ 11080 1.1 christos /* thread_info.tid */ 11081 1.14 christos sprintf (buf, NOTE_PSEUDO_SECTION_REG "/%ld", (long) bfd_get_32 (abfd, note->descdata + 4)); 11082 1.1 christos 11083 1.1 christos len = strlen (buf) + 1; 11084 1.1 christos name = (char *) bfd_alloc (abfd, len); 11085 1.1 christos if (name == NULL) 11086 1.1 christos return false; 11087 1.14 christos 11088 1.1 christos memcpy (name, buf, len); 11089 1.1 christos 11090 1.12 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 11091 1.1 christos if (sect == NULL) 11092 1.1 christos return false; 11093 1.1 christos 11094 1.1 christos /* sizeof (thread_info.thread_context) */ 11095 1.1 christos sect->size = note->descsz - 12; 11096 1.1 christos /* offsetof (thread_info.thread_context) */ 11097 1.1 christos sect->filepos = note->descpos + 12; 11098 1.1 christos sect->alignment_power = 2; 11099 1.19 christos 11100 1.14 christos /* thread_info.is_active_thread */ 11101 1.1 christos is_active_thread = bfd_get_32 (abfd, note->descdata + 8); 11102 1.1 christos 11103 1.12 christos if (is_active_thread) 11104 1.12 christos if (! elfcore_maybe_make_sect (abfd, NOTE_PSEUDO_SECTION_REG, sect)) 11105 1.1 christos return false; 11106 1.12 christos break; 11107 1.14 christos 11108 1.14 christos case NOTE_INFO_MODULE: 11109 1.14 christos case NOTE_INFO_MODULE64: 11110 1.14 christos /* Make a ".module/xxxxxxxx" section. */ 11111 1.14 christos if (type == NOTE_INFO_MODULE) 11112 1.14 christos { 11113 1.14 christos /* module_info.base_address */ 11114 1.12 christos base_addr = bfd_get_32 (abfd, note->descdata + 4); 11115 1.14 christos sprintf (buf, ".module/%08lx", (unsigned long) base_addr); 11116 1.14 christos /* module_info.module_name_size */ 11117 1.14 christos name_size = bfd_get_32 (abfd, note->descdata + 8); 11118 1.14 christos } 11119 1.14 christos else /* NOTE_INFO_MODULE64 */ 11120 1.14 christos { 11121 1.14 christos /* module_info.base_address */ 11122 1.1 christos base_addr = bfd_get_64 (abfd, note->descdata + 4); 11123 1.1 christos sprintf (buf, ".module/%016lx", (unsigned long) base_addr); 11124 1.1 christos /* module_info.module_name_size */ 11125 1.1 christos name_size = bfd_get_32 (abfd, note->descdata + 12); 11126 1.14 christos } 11127 1.1 christos 11128 1.1 christos len = strlen (buf) + 1; 11129 1.1 christos name = (char *) bfd_alloc (abfd, len); 11130 1.1 christos if (name == NULL) 11131 1.1 christos return false; 11132 1.1 christos 11133 1.14 christos memcpy (name, buf, len); 11134 1.1 christos 11135 1.12 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 11136 1.14 christos 11137 1.14 christos if (sect == NULL) 11138 1.14 christos return false; 11139 1.14 christos 11140 1.14 christos if (note->descsz < 12 + name_size) 11141 1.14 christos { 11142 1.12 christos _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu" 11143 1.1 christos " is too small to contain a name of size %u"), 11144 1.1 christos abfd, note->descsz, name_size); 11145 1.1 christos return true; 11146 1.1 christos } 11147 1.1 christos 11148 1.1 christos sect->size = note->descsz; 11149 1.14 christos sect->filepos = note->descpos; 11150 1.1 christos sect->alignment_power = 2; 11151 1.1 christos break; 11152 1.14 christos 11153 1.1 christos default: 11154 1.1 christos return true; 11155 1.14 christos } 11156 1.1 christos 11157 1.1 christos return true; 11158 1.1 christos } 11159 1.1 christos 11160 1.19 christos static bool 11161 1.19 christos elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) 11162 1.19 christos { 11163 1.19 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11164 1.19 christos 11165 1.19 christos /* Short cut for LINUX notes. */ 11166 1.19 christos if (note->namesz == 6 /* strlen (NOTE_NAME_LINUX) + 1 */ 11167 1.19 christos && streq (note->namedata, NOTE_NAME_LINUX)) 11168 1.19 christos { 11169 1.19 christos switch (note->type) 11170 1.19 christos { 11171 1.19 christos case NT_386_TLS: return elfcore_grok_i386_tls (abfd, note); 11172 1.19 christos case NT_ARC_V2: return elfcore_grok_arc_v2 (abfd, note); 11173 1.19 christos case NT_ARM_GCS: return elfcore_grok_aarch_gcs (abfd, note); 11174 1.19 christos case NT_ARM_HW_BREAK: return elfcore_grok_aarch_hw_break (abfd, note); 11175 1.19 christos case NT_ARM_HW_WATCH: return elfcore_grok_aarch_hw_watch (abfd, note); 11176 1.19 christos case NT_ARM_PAC_MASK: return elfcore_grok_aarch_pauth (abfd, note); 11177 1.19 christos case NT_ARM_SSVE: return elfcore_grok_aarch_ssve (abfd, note); 11178 1.19 christos case NT_ARM_SVE: return elfcore_grok_aarch_sve (abfd, note); 11179 1.19 christos case NT_ARM_TAGGED_ADDR_CTRL: return elfcore_grok_aarch_mte (abfd, note); 11180 1.19 christos case NT_ARM_TLS: return elfcore_grok_aarch_tls (abfd, note); 11181 1.19 christos case NT_ARM_VFP: return elfcore_grok_arm_vfp (abfd, note); 11182 1.19 christos case NT_ARM_ZA: return elfcore_grok_aarch_za (abfd, note); 11183 1.19 christos case NT_ARM_ZT: return elfcore_grok_aarch_zt (abfd, note); 11184 1.19 christos case NT_LARCH_CPUCFG: return elfcore_grok_loongarch_cpucfg (abfd, note); 11185 1.19 christos case NT_LARCH_LASX: return elfcore_grok_loongarch_lasx (abfd, note); 11186 1.19 christos case NT_LARCH_LBT: return elfcore_grok_loongarch_lbt (abfd, note); 11187 1.19 christos case NT_LARCH_LSX: return elfcore_grok_loongarch_lsx (abfd, note); 11188 1.19 christos case NT_PPC_DSCR: return elfcore_grok_ppc_dscr (abfd, note); 11189 1.19 christos case NT_PPC_EBB: return elfcore_grok_ppc_ebb (abfd, note); 11190 1.19 christos case NT_PPC_PMU: return elfcore_grok_ppc_pmu (abfd, note); 11191 1.19 christos case NT_PPC_PPR: return elfcore_grok_ppc_ppr (abfd, note); 11192 1.19 christos case NT_PPC_TAR: return elfcore_grok_ppc_tar (abfd, note); 11193 1.19 christos case NT_PPC_TM_CDSCR: return elfcore_grok_ppc_tm_cdscr (abfd, note); 11194 1.19 christos case NT_PPC_TM_CFPR: return elfcore_grok_ppc_tm_cfpr (abfd, note); 11195 1.19 christos case NT_PPC_TM_CGPR: return elfcore_grok_ppc_tm_cgpr (abfd, note); 11196 1.19 christos case NT_PPC_TM_CPPR: return elfcore_grok_ppc_tm_cppr (abfd, note); 11197 1.19 christos case NT_PPC_TM_CTAR: return elfcore_grok_ppc_tm_ctar (abfd, note); 11198 1.19 christos case NT_PPC_TM_CVMX: return elfcore_grok_ppc_tm_cvmx (abfd, note); 11199 1.19 christos case NT_PPC_TM_CVSX: return elfcore_grok_ppc_tm_cvsx (abfd, note); 11200 1.19 christos case NT_PPC_TM_SPR: return elfcore_grok_ppc_tm_spr (abfd, note); 11201 1.19 christos case NT_PPC_VMX: return elfcore_grok_ppc_vmx (abfd, note); 11202 1.19 christos case NT_PPC_VSX: return elfcore_grok_ppc_vsx (abfd, note); 11203 1.19 christos case NT_PRXFPREG: return elfcore_grok_prxfpreg (abfd, note); 11204 1.19 christos case NT_S390_CTRS: return elfcore_grok_s390_ctrs (abfd, note); 11205 1.19 christos case NT_S390_GS_BC: return elfcore_grok_s390_gs_bc (abfd, note); 11206 1.19 christos case NT_S390_GS_CB: return elfcore_grok_s390_gs_cb (abfd, note); 11207 1.19 christos case NT_S390_HIGH_GPRS: return elfcore_grok_s390_high_gprs (abfd, note); 11208 1.19 christos case NT_S390_LAST_BREAK: return elfcore_grok_s390_last_break (abfd, note); 11209 1.19 christos case NT_S390_PREFIX: return elfcore_grok_s390_prefix (abfd, note); 11210 1.19 christos case NT_S390_SYSTEM_CALL: return elfcore_grok_s390_system_call (abfd, note); 11211 1.19 christos case NT_S390_TDB: return elfcore_grok_s390_tdb (abfd, note); 11212 1.19 christos case NT_S390_TIMER: return elfcore_grok_s390_timer (abfd, note); 11213 1.19 christos case NT_S390_TODCMP: return elfcore_grok_s390_todcmp (abfd, note); 11214 1.19 christos case NT_S390_TODPREG: return elfcore_grok_s390_todpreg (abfd, note); 11215 1.19 christos case NT_S390_VXRS_HIGH: return elfcore_grok_s390_vxrs_high (abfd, note); 11216 1.19 christos case NT_S390_VXRS_LOW: return elfcore_grok_s390_vxrs_low (abfd, note); 11217 1.19 christos case NT_X86_SHSTK: return elfcore_grok_sspreg (abfd, note); 11218 1.1 christos case NT_X86_XSTATE: return elfcore_grok_xstatereg (abfd, note); 11219 1.1 christos default: break; 11220 1.1 christos } 11221 1.14 christos } 11222 1.1 christos 11223 1.1 christos switch (note->type) 11224 1.1 christos { 11225 1.1 christos default: 11226 1.14 christos return true; 11227 1.1 christos 11228 1.1 christos case NT_PRSTATUS: 11229 1.1 christos if (bed->elf_backend_grok_prstatus) 11230 1.14 christos if ((*bed->elf_backend_grok_prstatus) (abfd, note)) 11231 1.1 christos return true; 11232 1.1 christos #if defined (HAVE_PRSTATUS_T) 11233 1.1 christos return elfcore_grok_prstatus (abfd, note); 11234 1.1 christos #else 11235 1.1 christos return true; 11236 1.1 christos #endif 11237 1.1 christos 11238 1.1 christos #if defined (HAVE_PSTATUS_T) 11239 1.1 christos case NT_PSTATUS: 11240 1.1 christos return elfcore_grok_pstatus (abfd, note); 11241 1.1 christos #endif 11242 1.1 christos 11243 1.19 christos #if defined (HAVE_LWPSTATUS_T) 11244 1.1 christos case NT_LWPSTATUS: 11245 1.1 christos return elfcore_grok_lwpstatus (abfd, note); 11246 1.1 christos #endif 11247 1.1 christos 11248 1.1 christos case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG. */ 11249 1.14 christos return elfcore_grok_prfpreg (abfd, note); 11250 1.19 christos 11251 1.14 christos case NT_WIN32PSTATUS: 11252 1.14 christos return elfcore_grok_win32pstatus (abfd, note); 11253 1.14 christos 11254 1.14 christos case NT_GDB_TDESC: 11255 1.14 christos if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB)) 11256 1.19 christos return elfcore_grok_gdb_tdesc (abfd, note); 11257 1.14 christos else 11258 1.14 christos return true; 11259 1.14 christos 11260 1.14 christos case NT_RISCV_CSR: 11261 1.1 christos if (note->namesz == 4 && streq (note->namedata, NOTE_NAME_GDB)) 11262 1.1 christos return elfcore_grok_riscv_csr (abfd, note); 11263 1.1 christos else 11264 1.1 christos return true; 11265 1.14 christos 11266 1.1 christos case NT_PRPSINFO: 11267 1.1 christos case NT_PSINFO: 11268 1.1 christos if (bed->elf_backend_grok_psinfo) 11269 1.14 christos if ((*bed->elf_backend_grok_psinfo) (abfd, note)) 11270 1.1 christos return true; 11271 1.1 christos #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 11272 1.1 christos return elfcore_grok_psinfo (abfd, note); 11273 1.12 christos #else 11274 1.3 christos return true; 11275 1.3 christos #endif 11276 1.3 christos 11277 1.3 christos case NT_AUXV: 11278 1.3 christos return elfcore_make_auxv_note_section (abfd, note, 0); 11279 1.3 christos 11280 1.3 christos case NT_FILE: 11281 1.3 christos return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", 11282 1.1 christos note); 11283 1.1 christos 11284 1.1 christos case NT_SIGINFO: 11285 1.14 christos return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", 11286 1.1 christos note); 11287 1.1 christos } 11288 1.6 christos } 11289 1.3 christos 11290 1.3 christos static bool 11291 1.14 christos elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) 11292 1.1 christos { 11293 1.6 christos struct bfd_build_id* build_id; 11294 1.6 christos 11295 1.14 christos if (note->descsz == 0) 11296 1.3 christos return false; 11297 1.6 christos 11298 1.6 christos build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); 11299 1.6 christos if (build_id == NULL) 11300 1.1 christos return false; 11301 1.14 christos 11302 1.1 christos build_id->size = note->descsz; 11303 1.1 christos memcpy (build_id->data, note->descdata, note->descsz); 11304 1.14 christos abfd->build_id = build_id; 11305 1.1 christos 11306 1.1 christos return true; 11307 1.1 christos } 11308 1.1 christos 11309 1.1 christos static bool 11310 1.14 christos elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) 11311 1.1 christos { 11312 1.9 christos switch (note->type) 11313 1.9 christos { 11314 1.9 christos default: 11315 1.1 christos return true; 11316 1.1 christos 11317 1.1 christos case NT_GNU_PROPERTY_TYPE_0: 11318 1.1 christos return _bfd_elf_parse_gnu_properties (abfd, note); 11319 1.1 christos 11320 1.14 christos case NT_GNU_BUILD_ID: 11321 1.3 christos return elfobj_grok_gnu_build_id (abfd, note); 11322 1.3 christos } 11323 1.3 christos } 11324 1.12 christos 11325 1.12 christos static bool 11326 1.3 christos elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) 11327 1.3 christos { 11328 1.3 christos struct sdt_note *cur = 11329 1.3 christos (struct sdt_note *) bfd_alloc (abfd, 11330 1.3 christos sizeof (struct sdt_note) + note->descsz); 11331 1.3 christos 11332 1.3 christos cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; 11333 1.14 christos cur->size = (bfd_size_type) note->descsz; 11334 1.3 christos memcpy (cur->data, note->descdata, note->descsz); 11335 1.3 christos 11336 1.14 christos elf_tdata (abfd)->sdt_note_head = cur; 11337 1.3 christos 11338 1.3 christos return true; 11339 1.3 christos } 11340 1.3 christos 11341 1.3 christos static bool 11342 1.3 christos elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) 11343 1.3 christos { 11344 1.3 christos switch (note->type) 11345 1.14 christos { 11346 1.3 christos case NT_STAPSDT: 11347 1.3 christos return elfobj_grok_stapsdt_note_1 (abfd, note); 11348 1.3 christos 11349 1.14 christos default: 11350 1.8 christos return true; 11351 1.8 christos } 11352 1.8 christos } 11353 1.8 christos 11354 1.11 christos static bool 11355 1.8 christos elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) 11356 1.11 christos { 11357 1.8 christos size_t offset; 11358 1.14 christos 11359 1.8 christos switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) 11360 1.8 christos { 11361 1.11 christos case ELFCLASS32: 11362 1.8 christos if (note->descsz < 108) 11363 1.14 christos return false; 11364 1.8 christos break; 11365 1.8 christos 11366 1.8 christos case ELFCLASS64: 11367 1.14 christos if (note->descsz < 120) 11368 1.8 christos return false; 11369 1.8 christos break; 11370 1.8 christos 11371 1.8 christos default: 11372 1.14 christos return false; 11373 1.11 christos } 11374 1.8 christos 11375 1.8 christos /* Check for version 1 in pr_version. */ 11376 1.8 christos if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) 11377 1.11 christos return false; 11378 1.8 christos 11379 1.8 christos offset = 4; 11380 1.8 christos 11381 1.8 christos /* Skip over pr_psinfosz. */ 11382 1.8 christos if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) 11383 1.8 christos offset += 4; 11384 1.8 christos else 11385 1.8 christos { 11386 1.8 christos offset += 4; /* Padding before pr_psinfosz. */ 11387 1.8 christos offset += 8; 11388 1.8 christos } 11389 1.8 christos 11390 1.8 christos /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ 11391 1.8 christos elf_tdata (abfd)->core->program 11392 1.8 christos = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); 11393 1.8 christos offset += 17; 11394 1.8 christos 11395 1.8 christos /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ 11396 1.8 christos elf_tdata (abfd)->core->command 11397 1.8 christos = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); 11398 1.8 christos offset += 81; 11399 1.8 christos 11400 1.14 christos /* Padding before pr_pid. */ 11401 1.8 christos offset += 2; 11402 1.8 christos 11403 1.8 christos /* The pr_pid field was added in version "1a". */ 11404 1.8 christos if (note->descsz < offset + 4) 11405 1.14 christos return true; 11406 1.8 christos 11407 1.8 christos elf_tdata (abfd)->core->pid 11408 1.14 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 11409 1.8 christos 11410 1.8 christos return true; 11411 1.8 christos } 11412 1.8 christos 11413 1.11 christos static bool 11414 1.8 christos elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) 11415 1.11 christos { 11416 1.11 christos size_t offset; 11417 1.11 christos size_t size; 11418 1.11 christos size_t min_size; 11419 1.11 christos 11420 1.11 christos /* Compute offset of pr_getregsz, skipping over pr_statussz. 11421 1.11 christos Also compute minimum size of this note. */ 11422 1.8 christos switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) 11423 1.8 christos { 11424 1.11 christos case ELFCLASS32: 11425 1.11 christos offset = 4 + 4; 11426 1.11 christos min_size = offset + (4 * 2) + 4 + 4 + 4; 11427 1.8 christos break; 11428 1.8 christos 11429 1.8 christos case ELFCLASS64: 11430 1.14 christos offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */ 11431 1.8 christos min_size = offset + (8 * 2) + 4 + 4 + 4 + 4; 11432 1.8 christos break; 11433 1.11 christos 11434 1.14 christos default: 11435 1.11 christos return false; 11436 1.11 christos } 11437 1.11 christos 11438 1.14 christos if (note->descsz < min_size) 11439 1.11 christos return false; 11440 1.8 christos 11441 1.11 christos /* Check for version 1 in pr_version. */ 11442 1.11 christos if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) 11443 1.11 christos return false; 11444 1.11 christos 11445 1.11 christos /* Extract size of pr_reg from pr_gregsetsz. */ 11446 1.11 christos /* Skip over pr_gregsetsz and pr_fpregsetsz. */ 11447 1.8 christos if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) 11448 1.11 christos { 11449 1.11 christos size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 11450 1.11 christos offset += 4 * 2; 11451 1.11 christos } 11452 1.8 christos else 11453 1.11 christos { 11454 1.8 christos size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); 11455 1.8 christos offset += 8 * 2; 11456 1.11 christos } 11457 1.8 christos 11458 1.8 christos /* Skip over pr_osreldate. */ 11459 1.8 christos offset += 4; 11460 1.8 christos 11461 1.8 christos /* Read signal from pr_cursig. */ 11462 1.11 christos if (elf_tdata (abfd)->core->signal == 0) 11463 1.8 christos elf_tdata (abfd)->core->signal 11464 1.8 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 11465 1.8 christos offset += 4; 11466 1.8 christos 11467 1.11 christos /* Read TID from pr_pid. */ 11468 1.11 christos elf_tdata (abfd)->core->lwpid 11469 1.8 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 11470 1.8 christos offset += 4; 11471 1.11 christos 11472 1.11 christos /* Padding before pr_reg. */ 11473 1.14 christos if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) 11474 1.11 christos offset += 4; 11475 1.8 christos 11476 1.19 christos /* Make sure that there is enough data remaining in the note. */ 11477 1.8 christos if ((note->descsz - offset) < size) 11478 1.8 christos return false; 11479 1.8 christos 11480 1.14 christos /* Make a ".reg/999" section and a ".reg" section. */ 11481 1.8 christos return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, 11482 1.8 christos size, note->descpos + offset); 11483 1.11 christos } 11484 1.11 christos 11485 1.8 christos static bool 11486 1.8 christos elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) 11487 1.8 christos { 11488 1.11 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11489 1.11 christos 11490 1.14 christos switch (note->type) 11491 1.8 christos { 11492 1.8 christos case NT_PRSTATUS: 11493 1.8 christos if (bed->elf_backend_grok_freebsd_prstatus) 11494 1.8 christos if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note)) 11495 1.8 christos return true; 11496 1.8 christos return elfcore_grok_freebsd_prstatus (abfd, note); 11497 1.8 christos 11498 1.8 christos case NT_FPREGSET: 11499 1.8 christos return elfcore_grok_prfpreg (abfd, note); 11500 1.14 christos 11501 1.8 christos case NT_PRPSINFO: 11502 1.11 christos return elfcore_grok_freebsd_psinfo (abfd, note); 11503 1.11 christos 11504 1.11 christos case NT_FREEBSD_THRMISC: 11505 1.11 christos return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); 11506 1.11 christos 11507 1.11 christos case NT_FREEBSD_PROCSTAT_PROC: 11508 1.11 christos return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc", 11509 1.11 christos note); 11510 1.11 christos 11511 1.11 christos case NT_FREEBSD_PROCSTAT_FILES: 11512 1.11 christos return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files", 11513 1.11 christos note); 11514 1.8 christos 11515 1.12 christos case NT_FREEBSD_PROCSTAT_VMMAP: 11516 1.8 christos return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap", 11517 1.14 christos note); 11518 1.19 christos 11519 1.14 christos case NT_FREEBSD_PROCSTAT_AUXV: 11520 1.8 christos return elfcore_make_auxv_note_section (abfd, note, 4); 11521 1.14 christos 11522 1.8 christos case NT_FREEBSD_X86_SEGBASES: 11523 1.11 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_X86_SEGBASES, note); 11524 1.11 christos 11525 1.11 christos case NT_X86_XSTATE: 11526 1.11 christos return elfcore_grok_xstatereg (abfd, note); 11527 1.14 christos 11528 1.14 christos case NT_FREEBSD_PTLWPINFO: 11529 1.14 christos return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo", 11530 1.11 christos note); 11531 1.11 christos 11532 1.11 christos case NT_ARM_TLS: 11533 1.8 christos return elfcore_grok_aarch_tls (abfd, note); 11534 1.14 christos 11535 1.8 christos case NT_ARM_VFP: 11536 1.8 christos return elfcore_grok_arm_vfp (abfd, note); 11537 1.8 christos 11538 1.14 christos default: 11539 1.1 christos return true; 11540 1.1 christos } 11541 1.1 christos } 11542 1.1 christos 11543 1.1 christos static bool 11544 1.1 christos elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) 11545 1.1 christos { 11546 1.1 christos char *cp; 11547 1.14 christos 11548 1.1 christos cp = strchr (note->namedata, '@'); 11549 1.14 christos if (cp != NULL) 11550 1.1 christos { 11551 1.1 christos *lwpidp = atoi(cp + 1); 11552 1.14 christos return true; 11553 1.1 christos } 11554 1.1 christos return false; 11555 1.11 christos } 11556 1.14 christos 11557 1.11 christos static bool 11558 1.1 christos elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) 11559 1.3 christos { 11560 1.1 christos if (note->descsz <= 0x7c + 31) 11561 1.1 christos return false; 11562 1.1 christos 11563 1.3 christos /* Signal number at offset 0x08. */ 11564 1.1 christos elf_tdata (abfd)->core->signal 11565 1.1 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); 11566 1.1 christos 11567 1.3 christos /* Process ID at offset 0x50. */ 11568 1.1 christos elf_tdata (abfd)->core->pid 11569 1.1 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); 11570 1.1 christos 11571 1.1 christos /* Command name at 0x7c (max 32 bytes, including nul). */ 11572 1.1 christos elf_tdata (abfd)->core->command 11573 1.1 christos = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); 11574 1.14 christos 11575 1.1 christos return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", 11576 1.1 christos note); 11577 1.1 christos } 11578 1.1 christos 11579 1.1 christos static bool 11580 1.3 christos elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) 11581 1.1 christos { 11582 1.12 christos int lwp; 11583 1.1 christos 11584 1.12 christos if (elfcore_netbsd_get_lwpid (note, &lwp)) 11585 1.1 christos elf_tdata (abfd)->core->lwpid = lwp; 11586 1.1 christos 11587 1.1 christos switch (note->type) 11588 1.1 christos { 11589 1.1 christos case NT_NETBSDCORE_PROCINFO: 11590 1.12 christos /* NetBSD-specific core "procinfo". Note that we expect to 11591 1.12 christos find this note before any of the others, which is fine, 11592 1.15 rin since the kernel writes this note out first when it 11593 1.12 christos creates a core file. */ 11594 1.12 christos return elfcore_grok_netbsd_procinfo (abfd, note); 11595 1.12 christos case NT_NETBSDCORE_AUXV: 11596 1.12 christos /* NetBSD-specific Elf Auxiliary Vector data. */ 11597 1.12 christos return elfcore_make_auxv_note_section (abfd, note, 0); 11598 1.12 christos case NT_NETBSDCORE_LWPSTATUS: 11599 1.1 christos return elfcore_make_note_pseudosection (abfd, 11600 1.1 christos ".note.netbsdcore.lwpstatus", 11601 1.7 christos note); 11602 1.7 christos default: 11603 1.7 christos break; 11604 1.7 christos } 11605 1.7 christos 11606 1.7 christos if (note->type == NT_NETBSDCORE_AUXV) 11607 1.14 christos { 11608 1.7 christos asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", 11609 1.7 christos SEC_HAS_CONTENTS); 11610 1.7 christos 11611 1.7 christos if (sect == NULL) 11612 1.14 christos return false; 11613 1.7 christos sect->size = note->descsz; 11614 1.7 christos sect->filepos = note->descpos; 11615 1.12 christos sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; 11616 1.1 christos 11617 1.1 christos return true; 11618 1.1 christos } 11619 1.1 christos 11620 1.1 christos /* As of March 2020 there are no other machine-independent notes 11621 1.14 christos defined for NetBSD core files. If the note type is less 11622 1.1 christos than the start of the machine-dependent note types, we don't 11623 1.1 christos understand it. */ 11624 1.1 christos 11625 1.1 christos if (note->type < NT_NETBSDCORE_FIRSTMACH) 11626 1.1 christos return true; 11627 1.1 christos 11628 1.1 christos 11629 1.10 christos switch (bfd_get_arch (abfd)) 11630 1.1 christos { 11631 1.16 rin /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and 11632 1.1 christos PT_GETFPREGS == mach+2. */ 11633 1.1 christos 11634 1.1 christos case bfd_arch_aarch64: 11635 1.1 christos case bfd_arch_alpha: 11636 1.19 christos case bfd_arch_riscv: 11637 1.1 christos case bfd_arch_sparc: 11638 1.1 christos switch (note->type) 11639 1.19 christos { 11640 1.1 christos case NT_NETBSDCORE_FIRSTMACH+0: 11641 1.1 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note); 11642 1.14 christos 11643 1.1 christos case NT_NETBSDCORE_FIRSTMACH+2: 11644 1.1 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note); 11645 1.2 uwe 11646 1.2 uwe default: 11647 1.2 uwe return true; 11648 1.2 uwe } 11649 1.2 uwe 11650 1.2 uwe /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5. 11651 1.2 uwe There's also old PT___GETREGS40 == mach + 1 for old reg 11652 1.2 uwe structure which lacks GBR. */ 11653 1.19 christos 11654 1.2 uwe case bfd_arch_sh: 11655 1.2 uwe switch (note->type) 11656 1.19 christos { 11657 1.2 uwe case NT_NETBSDCORE_FIRSTMACH+3: 11658 1.2 uwe return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note); 11659 1.14 christos 11660 1.2 uwe case NT_NETBSDCORE_FIRSTMACH+5: 11661 1.2 uwe return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note); 11662 1.1 christos 11663 1.1 christos default: 11664 1.1 christos return true; 11665 1.1 christos } 11666 1.1 christos 11667 1.1 christos /* On all other arch's, PT_GETREGS == mach+1 and 11668 1.1 christos PT_GETFPREGS == mach+3. */ 11669 1.19 christos 11670 1.1 christos default: 11671 1.1 christos switch (note->type) 11672 1.19 christos { 11673 1.1 christos case NT_NETBSDCORE_FIRSTMACH+1: 11674 1.1 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note); 11675 1.14 christos 11676 1.1 christos case NT_NETBSDCORE_FIRSTMACH+3: 11677 1.1 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note); 11678 1.1 christos 11679 1.1 christos default: 11680 1.1 christos return true; 11681 1.14 christos } 11682 1.1 christos } 11683 1.1 christos /* NOTREACHED */ 11684 1.11 christos } 11685 1.14 christos 11686 1.11 christos static bool 11687 1.1 christos elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) 11688 1.3 christos { 11689 1.1 christos if (note->descsz <= 0x48 + 31) 11690 1.1 christos return false; 11691 1.1 christos 11692 1.3 christos /* Signal number at offset 0x08. */ 11693 1.1 christos elf_tdata (abfd)->core->signal 11694 1.1 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); 11695 1.1 christos 11696 1.3 christos /* Process ID at offset 0x20. */ 11697 1.1 christos elf_tdata (abfd)->core->pid 11698 1.1 christos = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); 11699 1.14 christos 11700 1.14 christos /* Command name at 0x48 (max 32 bytes, including nul). */ 11701 1.14 christos elf_tdata (abfd)->core->command 11702 1.14 christos = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); 11703 1.14 christos 11704 1.14 christos return true; 11705 1.14 christos } 11706 1.14 christos 11707 1.14 christos /* Processes Solaris's process status note. 11708 1.14 christos sig_off ~ offsetof(prstatus_t, pr_cursig) 11709 1.14 christos pid_off ~ offsetof(prstatus_t, pr_pid) 11710 1.14 christos lwpid_off ~ offsetof(prstatus_t, pr_who) 11711 1.14 christos gregset_size ~ sizeof(gregset_t) 11712 1.14 christos gregset_offset ~ offsetof(prstatus_t, pr_reg) */ 11713 1.14 christos 11714 1.14 christos static bool 11715 1.14 christos elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off, 11716 1.14 christos int pid_off, int lwpid_off, size_t gregset_size, 11717 1.14 christos size_t gregset_offset) 11718 1.14 christos { 11719 1.14 christos asection *sect = NULL; 11720 1.14 christos elf_tdata (abfd)->core->signal 11721 1.14 christos = bfd_get_16 (abfd, note->descdata + sig_off); 11722 1.19 christos elf_tdata (abfd)->core->pid 11723 1.14 christos = bfd_get_32 (abfd, note->descdata + pid_off); 11724 1.14 christos elf_tdata (abfd)->core->lwpid 11725 1.14 christos = bfd_get_32 (abfd, note->descdata + lwpid_off); 11726 1.19 christos 11727 1.14 christos sect = bfd_get_section_by_name (abfd, NOTE_PSEUDO_SECTION_REG); 11728 1.14 christos if (sect != NULL) 11729 1.14 christos sect->size = gregset_size; 11730 1.14 christos 11731 1.14 christos return _bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, gregset_size, 11732 1.14 christos note->descpos + gregset_offset); 11733 1.14 christos } 11734 1.14 christos 11735 1.14 christos /* Gets program and arguments from a core. 11736 1.14 christos prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname) 11737 1.14 christos comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */ 11738 1.14 christos 11739 1.14 christos static bool 11740 1.14 christos elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note, 11741 1.14 christos int prog_off, int comm_off) 11742 1.14 christos { 11743 1.14 christos elf_tdata (abfd)->core->program 11744 1.14 christos = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16); 11745 1.14 christos elf_tdata (abfd)->core->command 11746 1.14 christos = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80); 11747 1.14 christos 11748 1.14 christos return true; 11749 1.14 christos } 11750 1.14 christos 11751 1.14 christos /* Processes Solaris's LWP status note. 11752 1.14 christos gregset_size ~ sizeof(gregset_t) 11753 1.14 christos gregset_off ~ offsetof(lwpstatus_t, pr_reg) 11754 1.14 christos fpregset_size ~ sizeof(fpregset_t) 11755 1.14 christos fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */ 11756 1.14 christos 11757 1.14 christos static bool 11758 1.14 christos elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note, 11759 1.14 christos size_t gregset_size, int gregset_off, 11760 1.19 christos size_t fpregset_size, int fpregset_off) 11761 1.14 christos { 11762 1.14 christos asection *sect = NULL; 11763 1.14 christos char reg2_section_name[16] = { 0 }; 11764 1.14 christos 11765 1.14 christos (void) snprintf (reg2_section_name, 16, "%s/%i", NOTE_PSEUDO_SECTION_REG2, 11766 1.14 christos elf_tdata (abfd)->core->lwpid); 11767 1.14 christos 11768 1.14 christos /* offsetof(lwpstatus_t, pr_lwpid) */ 11769 1.14 christos elf_tdata (abfd)->core->lwpid 11770 1.19 christos = bfd_get_32 (abfd, note->descdata + 4); 11771 1.14 christos /* offsetof(lwpstatus_t, pr_cursig) */ 11772 1.14 christos elf_tdata (abfd)->core->signal 11773 1.19 christos = bfd_get_16 (abfd, note->descdata + 12); 11774 1.14 christos 11775 1.14 christos sect = bfd_get_section_by_name (abfd, NOTE_PSEUDO_SECTION_REG); 11776 1.14 christos if (sect != NULL) 11777 1.14 christos sect->size = gregset_size; 11778 1.14 christos else if (!_bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, gregset_size, 11779 1.14 christos note->descpos + gregset_off)) 11780 1.14 christos return false; 11781 1.14 christos 11782 1.14 christos sect = bfd_get_section_by_name (abfd, reg2_section_name); 11783 1.14 christos if (sect != NULL) 11784 1.19 christos { 11785 1.14 christos sect->size = fpregset_size; 11786 1.14 christos sect->filepos = note->descpos + fpregset_off; 11787 1.14 christos sect->alignment_power = 2; 11788 1.14 christos } 11789 1.14 christos else if (!_bfd_elfcore_make_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, fpregset_size, 11790 1.14 christos note->descpos + fpregset_off)) 11791 1.14 christos return false; 11792 1.14 christos 11793 1.14 christos return true; 11794 1.14 christos } 11795 1.14 christos 11796 1.14 christos static bool 11797 1.14 christos elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note) 11798 1.14 christos { 11799 1.14 christos if (note == NULL) 11800 1.14 christos return false; 11801 1.14 christos 11802 1.14 christos /* core files are identified as 32- or 64-bit, SPARC or x86, 11803 1.14 christos by the size of the descsz which matches the sizeof() 11804 1.14 christos the type appropriate for that note type (e.g., prstatus_t for 11805 1.14 christos SOLARIS_NT_PRSTATUS) for the corresponding architecture 11806 1.14 christos on Solaris. The core file bitness may differ from the bitness of 11807 1.14 christos gdb itself, so fixed values are used instead of sizeof(). 11808 1.14 christos Appropriate fixed offsets are also used to obtain data from 11809 1.14 christos the note. */ 11810 1.14 christos 11811 1.14 christos switch ((int) note->type) 11812 1.14 christos { 11813 1.14 christos case SOLARIS_NT_PRSTATUS: 11814 1.14 christos switch (note->descsz) 11815 1.14 christos { 11816 1.14 christos case 508: /* sizeof(prstatus_t) SPARC 32-bit */ 11817 1.14 christos return elfcore_grok_solaris_prstatus(abfd, note, 11818 1.14 christos 136, 216, 308, 152, 356); 11819 1.14 christos case 904: /* sizeof(prstatus_t) SPARC 64-bit */ 11820 1.14 christos return elfcore_grok_solaris_prstatus(abfd, note, 11821 1.14 christos 264, 360, 520, 304, 600); 11822 1.14 christos case 432: /* sizeof(prstatus_t) Intel 32-bit */ 11823 1.14 christos return elfcore_grok_solaris_prstatus(abfd, note, 11824 1.14 christos 136, 216, 308, 76, 356); 11825 1.14 christos case 824: /* sizeof(prstatus_t) Intel 64-bit */ 11826 1.14 christos return elfcore_grok_solaris_prstatus(abfd, note, 11827 1.14 christos 264, 360, 520, 224, 600); 11828 1.14 christos default: 11829 1.14 christos return true; 11830 1.14 christos } 11831 1.14 christos 11832 1.14 christos case SOLARIS_NT_PSINFO: 11833 1.14 christos case SOLARIS_NT_PRPSINFO: 11834 1.14 christos switch (note->descsz) 11835 1.14 christos { 11836 1.14 christos case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */ 11837 1.14 christos return elfcore_grok_solaris_info(abfd, note, 84, 100); 11838 1.14 christos case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */ 11839 1.14 christos return elfcore_grok_solaris_info(abfd, note, 120, 136); 11840 1.14 christos case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */ 11841 1.14 christos return elfcore_grok_solaris_info(abfd, note, 88, 104); 11842 1.14 christos case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */ 11843 1.14 christos return elfcore_grok_solaris_info(abfd, note, 136, 152); 11844 1.14 christos default: 11845 1.14 christos return true; 11846 1.14 christos } 11847 1.14 christos 11848 1.14 christos case SOLARIS_NT_LWPSTATUS: 11849 1.14 christos switch (note->descsz) 11850 1.14 christos { 11851 1.14 christos case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */ 11852 1.14 christos return elfcore_grok_solaris_lwpstatus(abfd, note, 11853 1.14 christos 152, 344, 400, 496); 11854 1.14 christos case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */ 11855 1.14 christos return elfcore_grok_solaris_lwpstatus(abfd, note, 11856 1.14 christos 304, 544, 544, 848); 11857 1.14 christos case 800: /* sizeof(lwpstatus_t) Intel 32-bit */ 11858 1.14 christos return elfcore_grok_solaris_lwpstatus(abfd, note, 11859 1.14 christos 76, 344, 380, 420); 11860 1.14 christos case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */ 11861 1.14 christos return elfcore_grok_solaris_lwpstatus(abfd, note, 11862 1.14 christos 224, 544, 528, 768); 11863 1.14 christos default: 11864 1.14 christos return true; 11865 1.14 christos } 11866 1.14 christos 11867 1.14 christos case SOLARIS_NT_LWPSINFO: 11868 1.14 christos /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */ 11869 1.14 christos if (note->descsz == 128 || note->descsz == 152) 11870 1.14 christos elf_tdata (abfd)->core->lwpid = 11871 1.14 christos bfd_get_32 (abfd, note->descdata + 4); 11872 1.14 christos break; 11873 1.14 christos 11874 1.1 christos default: 11875 1.1 christos break; 11876 1.14 christos } 11877 1.14 christos 11878 1.14 christos return true; 11879 1.14 christos } 11880 1.14 christos 11881 1.14 christos /* For name starting with "CORE" this may be either a Solaris 11882 1.14 christos core file or a gdb-generated core file. Do Solaris-specific 11883 1.14 christos processing on selected note types first with 11884 1.14 christos elfcore_grok_solaris_note(), then process the note 11885 1.14 christos in elfcore_grok_note(). */ 11886 1.14 christos 11887 1.14 christos static bool 11888 1.14 christos elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note) 11889 1.14 christos { 11890 1.14 christos if (!elfcore_grok_solaris_note_impl (abfd, note)) 11891 1.14 christos return false; 11892 1.1 christos 11893 1.1 christos return elfcore_grok_note (abfd, note); 11894 1.1 christos } 11895 1.1 christos 11896 1.1 christos static bool 11897 1.1 christos elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) 11898 1.19 christos { 11899 1.1 christos if (note->type == NT_OPENBSD_PROCINFO) 11900 1.1 christos return elfcore_grok_openbsd_procinfo (abfd, note); 11901 1.19 christos 11902 1.1 christos if (note->type == NT_OPENBSD_REGS) 11903 1.1 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG, note); 11904 1.19 christos 11905 1.1 christos if (note->type == NT_OPENBSD_FPREGS) 11906 1.1 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_REG2, note); 11907 1.12 christos 11908 1.1 christos if (note->type == NT_OPENBSD_XFPREGS) 11909 1.14 christos return elfcore_make_note_pseudosection (abfd, NOTE_PSEUDO_SECTION_XFP, note); 11910 1.14 christos 11911 1.14 christos if (note->type == NT_OPENBSD_AUXV) 11912 1.14 christos return elfcore_make_auxv_note_section (abfd, note, 0); 11913 1.14 christos 11914 1.14 christos if (note->type == NT_OPENBSD_WCOOKIE) 11915 1.14 christos { 11916 1.14 christos asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", 11917 1.14 christos SEC_HAS_CONTENTS); 11918 1.14 christos 11919 1.14 christos if (sect == NULL) 11920 1.14 christos return false; 11921 1.14 christos sect->size = note->descsz; 11922 1.14 christos sect->filepos = note->descpos; 11923 1.14 christos sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; 11924 1.1 christos 11925 1.1 christos return true; 11926 1.14 christos } 11927 1.1 christos 11928 1.1 christos return true; 11929 1.1 christos } 11930 1.1 christos 11931 1.1 christos static bool 11932 1.1 christos elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) 11933 1.1 christos { 11934 1.1 christos void *ddata = note->descdata; 11935 1.1 christos char buf[100]; 11936 1.11 christos char *name; 11937 1.14 christos asection *sect; 11938 1.11 christos short sig; 11939 1.1 christos unsigned flags; 11940 1.3 christos 11941 1.1 christos if (note->descsz < 16) 11942 1.1 christos return false; 11943 1.1 christos 11944 1.1 christos /* nto_procfs_status 'pid' field is at offset 0. */ 11945 1.1 christos elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); 11946 1.1 christos 11947 1.1 christos /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ 11948 1.1 christos *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); 11949 1.1 christos 11950 1.1 christos /* nto_procfs_status 'flags' field is at offset 8. */ 11951 1.3 christos flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); 11952 1.3 christos 11953 1.1 christos /* nto_procfs_status 'what' field is at offset 14. */ 11954 1.1 christos if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) 11955 1.1 christos { 11956 1.1 christos elf_tdata (abfd)->core->signal = sig; 11957 1.1 christos elf_tdata (abfd)->core->lwpid = *tid; 11958 1.1 christos } 11959 1.3 christos 11960 1.1 christos /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores 11961 1.1 christos do not come from signals so we make sure we set the current 11962 1.1 christos thread just in case. */ 11963 1.1 christos if (flags & 0x00000080) 11964 1.1 christos elf_tdata (abfd)->core->lwpid = *tid; 11965 1.1 christos 11966 1.14 christos /* Make a ".qnx_core_status/%d" section. */ 11967 1.1 christos sprintf (buf, ".qnx_core_status/%ld", *tid); 11968 1.1 christos 11969 1.1 christos name = (char *) bfd_alloc (abfd, strlen (buf) + 1); 11970 1.1 christos if (name == NULL) 11971 1.14 christos return false; 11972 1.1 christos strcpy (name, buf); 11973 1.11 christos 11974 1.11 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 11975 1.1 christos if (sect == NULL) 11976 1.1 christos return false; 11977 1.1 christos 11978 1.1 christos sect->size = note->descsz; 11979 1.1 christos sect->filepos = note->descpos; 11980 1.14 christos sect->alignment_power = 2; 11981 1.1 christos 11982 1.1 christos return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); 11983 1.1 christos } 11984 1.1 christos 11985 1.1 christos static bool 11986 1.1 christos elfcore_grok_nto_regs (bfd *abfd, 11987 1.1 christos Elf_Internal_Note *note, 11988 1.1 christos long tid, 11989 1.1 christos char *base) 11990 1.1 christos { 11991 1.1 christos char buf[100]; 11992 1.1 christos char *name; 11993 1.1 christos asection *sect; 11994 1.1 christos 11995 1.14 christos /* Make a "(base)/%d" section. */ 11996 1.1 christos sprintf (buf, "%s/%ld", base, tid); 11997 1.1 christos 11998 1.1 christos name = (char *) bfd_alloc (abfd, strlen (buf) + 1); 11999 1.1 christos if (name == NULL) 12000 1.14 christos return false; 12001 1.1 christos strcpy (name, buf); 12002 1.11 christos 12003 1.11 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 12004 1.1 christos if (sect == NULL) 12005 1.1 christos return false; 12006 1.1 christos 12007 1.3 christos sect->size = note->descsz; 12008 1.1 christos sect->filepos = note->descpos; 12009 1.1 christos sect->alignment_power = 2; 12010 1.14 christos 12011 1.1 christos /* This is the current thread. */ 12012 1.1 christos if (elf_tdata (abfd)->core->lwpid == tid) 12013 1.14 christos return elfcore_maybe_make_sect (abfd, base, sect); 12014 1.1 christos 12015 1.1 christos return true; 12016 1.1 christos } 12017 1.1 christos 12018 1.1 christos static bool 12019 1.1 christos elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) 12020 1.1 christos { 12021 1.1 christos /* Every GREG section has a STATUS section before it. Store the 12022 1.1 christos tid from the previous call to pass down to the next gregs 12023 1.17 christos function. */ 12024 1.1 christos static long tid = 1; 12025 1.17 christos 12026 1.1 christos switch (note->type) 12027 1.17 christos { 12028 1.19 christos case QNT_CORE_INFO: 12029 1.17 christos return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); 12030 1.19 christos case QNT_CORE_STATUS: 12031 1.1 christos return elfcore_grok_nto_status (abfd, note, &tid); 12032 1.14 christos case QNT_CORE_GREG: 12033 1.1 christos return elfcore_grok_nto_regs (abfd, note, tid, NOTE_PSEUDO_SECTION_REG); 12034 1.1 christos case QNT_CORE_FPREG: 12035 1.1 christos return elfcore_grok_nto_regs (abfd, note, tid, NOTE_PSEUDO_SECTION_REG2); 12036 1.14 christos default: 12037 1.1 christos return true; 12038 1.1 christos } 12039 1.1 christos } 12040 1.1 christos 12041 1.1 christos static bool 12042 1.1 christos elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) 12043 1.1 christos { 12044 1.1 christos char *name; 12045 1.1 christos asection *sect; 12046 1.1 christos size_t len; 12047 1.14 christos 12048 1.1 christos /* Use note name as section name. */ 12049 1.1 christos len = note->namesz; 12050 1.1 christos name = (char *) bfd_alloc (abfd, len); 12051 1.1 christos if (name == NULL) 12052 1.1 christos return false; 12053 1.14 christos memcpy (name, note->namedata, len); 12054 1.1 christos name[len - 1] = '\0'; 12055 1.11 christos 12056 1.11 christos sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 12057 1.1 christos if (sect == NULL) 12058 1.1 christos return false; 12059 1.14 christos 12060 1.1 christos sect->size = note->descsz; 12061 1.1 christos sect->filepos = note->descpos; 12062 1.1 christos sect->alignment_power = 1; 12063 1.1 christos 12064 1.1 christos return true; 12065 1.1 christos } 12066 1.1 christos 12067 1.1 christos /* Function: elfcore_write_note 12068 1.1 christos 12069 1.1 christos Inputs: 12070 1.1 christos buffer to hold note, and current size of buffer 12071 1.1 christos name of note 12072 1.1 christos type of note 12073 1.1 christos data for note 12074 1.1 christos size of data for note 12075 1.1 christos 12076 1.1 christos Writes note to end of buffer. ELF64 notes are written exactly as 12077 1.1 christos for ELF32, despite the current (as of 2006) ELF gabi specifying 12078 1.1 christos that they ought to have 8-byte namesz and descsz field, and have 12079 1.1 christos 8-byte alignment. Other writers, eg. Linux kernel, do the same. 12080 1.1 christos 12081 1.1 christos Return: 12082 1.1 christos Pointer to realloc'd buffer, *BUFSIZ updated. */ 12083 1.1 christos 12084 1.1 christos char * 12085 1.1 christos elfcore_write_note (bfd *abfd, 12086 1.1 christos char *buf, 12087 1.1 christos int *bufsiz, 12088 1.1 christos const char *name, 12089 1.1 christos int type, 12090 1.1 christos const void *input, 12091 1.1 christos int size) 12092 1.1 christos { 12093 1.1 christos Elf_External_Note *xnp; 12094 1.1 christos size_t namesz; 12095 1.1 christos size_t newspace; 12096 1.1 christos char *dest; 12097 1.1 christos 12098 1.1 christos namesz = 0; 12099 1.1 christos if (name != NULL) 12100 1.1 christos namesz = strlen (name) + 1; 12101 1.1 christos 12102 1.1 christos newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); 12103 1.1 christos 12104 1.1 christos buf = (char *) realloc (buf, *bufsiz + newspace); 12105 1.1 christos if (buf == NULL) 12106 1.1 christos return buf; 12107 1.1 christos dest = buf + *bufsiz; 12108 1.1 christos *bufsiz += newspace; 12109 1.1 christos xnp = (Elf_External_Note *) dest; 12110 1.1 christos H_PUT_32 (abfd, namesz, xnp->namesz); 12111 1.1 christos H_PUT_32 (abfd, size, xnp->descsz); 12112 1.1 christos H_PUT_32 (abfd, type, xnp->type); 12113 1.1 christos dest = xnp->name; 12114 1.1 christos if (name != NULL) 12115 1.1 christos { 12116 1.1 christos memcpy (dest, name, namesz); 12117 1.1 christos dest += namesz; 12118 1.1 christos while (namesz & 3) 12119 1.1 christos { 12120 1.1 christos *dest++ = '\0'; 12121 1.1 christos ++namesz; 12122 1.1 christos } 12123 1.1 christos } 12124 1.1 christos memcpy (dest, input, size); 12125 1.1 christos dest += size; 12126 1.1 christos while (size & 3) 12127 1.1 christos { 12128 1.1 christos *dest++ = '\0'; 12129 1.11 christos ++size; 12130 1.11 christos } 12131 1.11 christos return buf; 12132 1.11 christos } 12133 1.11 christos 12134 1.11 christos /* gcc-8 warns (*) on all the strncpy calls in this function about 12135 1.11 christos possible string truncation. The "truncation" is not a bug. We 12136 1.11 christos have an external representation of structs with fields that are not 12137 1.11 christos necessarily NULL terminated and corresponding internal 12138 1.11 christos representation fields that are one larger so that they can always 12139 1.11 christos be NULL terminated. 12140 1.11 christos gcc versions between 4.2 and 4.6 do not allow pragma control of 12141 1.11 christos diagnostics inside functions, giving a hard error if you try to use 12142 1.11 christos the finer control available with later versions. 12143 1.11 christos gcc prior to 4.2 warns about diagnostic push and pop. 12144 1.11 christos gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown, 12145 1.11 christos unless you also add #pragma GCC diagnostic ignored "-Wpragma". 12146 1.1 christos (*) Depending on your system header files! */ 12147 1.1 christos #if GCC_VERSION >= 8000 12148 1.1 christos # pragma GCC diagnostic push 12149 1.1 christos # pragma GCC diagnostic ignored "-Wstringop-truncation" 12150 1.1 christos #endif 12151 1.1 christos char * 12152 1.1 christos elfcore_write_prpsinfo (bfd *abfd, 12153 1.1 christos char *buf, 12154 1.1 christos int *bufsiz, 12155 1.1 christos const char *fname, 12156 1.1 christos const char *psargs) 12157 1.1 christos { 12158 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12159 1.1 christos 12160 1.1 christos if (bed->elf_backend_write_core_note != NULL) 12161 1.1 christos { 12162 1.1 christos char *ret; 12163 1.1 christos ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, 12164 1.3 christos NT_PRPSINFO, fname, psargs); 12165 1.11 christos if (ret != NULL) 12166 1.1 christos return ret; 12167 1.1 christos } 12168 1.11 christos 12169 1.1 christos #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 12170 1.1 christos # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) 12171 1.11 christos if (bed->s->elfclass == ELFCLASS32) 12172 1.1 christos { 12173 1.1 christos # if defined (HAVE_PSINFO32_T) 12174 1.11 christos psinfo32_t data; 12175 1.1 christos int note_type = NT_PSINFO; 12176 1.1 christos # else 12177 1.1 christos prpsinfo32_t data; 12178 1.1 christos int note_type = NT_PRPSINFO; 12179 1.1 christos # endif 12180 1.19 christos 12181 1.1 christos memset (&data, 0, sizeof (data)); 12182 1.1 christos strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); 12183 1.11 christos strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); 12184 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12185 1.11 christos NOTE_NAME_CORE, note_type, &data, sizeof (data)); 12186 1.1 christos } 12187 1.1 christos else 12188 1.11 christos # endif 12189 1.1 christos { 12190 1.1 christos # if defined (HAVE_PSINFO_T) 12191 1.11 christos psinfo_t data; 12192 1.1 christos int note_type = NT_PSINFO; 12193 1.1 christos # else 12194 1.1 christos prpsinfo_t data; 12195 1.1 christos int note_type = NT_PRPSINFO; 12196 1.1 christos # endif 12197 1.19 christos 12198 1.1 christos memset (&data, 0, sizeof (data)); 12199 1.3 christos strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); 12200 1.3 christos strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); 12201 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, 12202 1.3 christos NOTE_NAME_CORE, note_type, &data, sizeof (data)); 12203 1.3 christos } 12204 1.11 christos #endif /* PSINFO_T or PRPSINFO_T */ 12205 1.11 christos 12206 1.11 christos free (buf); 12207 1.3 christos return NULL; 12208 1.3 christos } 12209 1.3 christos #if GCC_VERSION >= 8000 12210 1.3 christos # pragma GCC diagnostic pop 12211 1.3 christos #endif 12212 1.3 christos 12213 1.11 christos char * 12214 1.11 christos elfcore_write_linux_prpsinfo32 12215 1.11 christos (bfd *abfd, char *buf, int *bufsiz, 12216 1.3 christos const struct elf_internal_linux_prpsinfo *prpsinfo) 12217 1.11 christos { 12218 1.19 christos if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16) 12219 1.11 christos { 12220 1.11 christos struct elf_external_linux_prpsinfo32_ugid16 data; 12221 1.11 christos 12222 1.11 christos swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data); 12223 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, NT_PRPSINFO, 12224 1.11 christos &data, sizeof (data)); 12225 1.11 christos } 12226 1.19 christos else 12227 1.11 christos { 12228 1.11 christos struct elf_external_linux_prpsinfo32_ugid32 data; 12229 1.3 christos 12230 1.3 christos swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data); 12231 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, NT_PRPSINFO, 12232 1.3 christos &data, sizeof (data)); 12233 1.3 christos } 12234 1.3 christos } 12235 1.3 christos 12236 1.11 christos char * 12237 1.11 christos elfcore_write_linux_prpsinfo64 12238 1.11 christos (bfd *abfd, char *buf, int *bufsiz, 12239 1.3 christos const struct elf_internal_linux_prpsinfo *prpsinfo) 12240 1.11 christos { 12241 1.11 christos if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16) 12242 1.19 christos { 12243 1.11 christos struct elf_external_linux_prpsinfo64_ugid16 data; 12244 1.11 christos 12245 1.11 christos swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data); 12246 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12247 1.11 christos NOTE_NAME_CORE, NT_PRPSINFO, &data, sizeof (data)); 12248 1.11 christos } 12249 1.11 christos else 12250 1.19 christos { 12251 1.11 christos struct elf_external_linux_prpsinfo64_ugid32 data; 12252 1.1 christos 12253 1.1 christos swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data); 12254 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12255 1.1 christos NOTE_NAME_CORE, NT_PRPSINFO, &data, sizeof (data)); 12256 1.1 christos } 12257 1.1 christos } 12258 1.1 christos 12259 1.1 christos char * 12260 1.1 christos elfcore_write_prstatus (bfd *abfd, 12261 1.1 christos char *buf, 12262 1.1 christos int *bufsiz, 12263 1.1 christos long pid, 12264 1.1 christos int cursig, 12265 1.1 christos const void *gregs) 12266 1.1 christos { 12267 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12268 1.1 christos 12269 1.1 christos if (bed->elf_backend_write_core_note != NULL) 12270 1.1 christos { 12271 1.1 christos char *ret; 12272 1.1 christos ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, 12273 1.1 christos NT_PRSTATUS, 12274 1.3 christos pid, cursig, gregs); 12275 1.1 christos if (ret != NULL) 12276 1.1 christos return ret; 12277 1.1 christos } 12278 1.1 christos 12279 1.1 christos #if defined (HAVE_PRSTATUS_T) 12280 1.1 christos #if defined (HAVE_PRSTATUS32_T) 12281 1.1 christos if (bed->s->elfclass == ELFCLASS32) 12282 1.1 christos { 12283 1.1 christos prstatus32_t prstat; 12284 1.19 christos 12285 1.1 christos memset (&prstat, 0, sizeof (prstat)); 12286 1.1 christos prstat.pr_pid = pid; 12287 1.1 christos prstat.pr_cursig = cursig; 12288 1.1 christos memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); 12289 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, 12290 1.1 christos NT_PRSTATUS, &prstat, sizeof (prstat)); 12291 1.1 christos } 12292 1.1 christos else 12293 1.1 christos #endif 12294 1.1 christos { 12295 1.1 christos prstatus_t prstat; 12296 1.19 christos 12297 1.1 christos memset (&prstat, 0, sizeof (prstat)); 12298 1.1 christos prstat.pr_pid = pid; 12299 1.3 christos prstat.pr_cursig = cursig; 12300 1.3 christos memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); 12301 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, 12302 1.3 christos NT_PRSTATUS, &prstat, sizeof (prstat)); 12303 1.1 christos } 12304 1.1 christos #endif /* HAVE_PRSTATUS_T */ 12305 1.1 christos 12306 1.1 christos free (buf); 12307 1.1 christos return NULL; 12308 1.1 christos } 12309 1.1 christos 12310 1.1 christos #if defined (HAVE_LWPSTATUS_T) 12311 1.1 christos char * 12312 1.1 christos elfcore_write_lwpstatus (bfd *abfd, 12313 1.1 christos char *buf, 12314 1.1 christos int *bufsiz, 12315 1.1 christos long pid, 12316 1.1 christos int cursig, 12317 1.1 christos const void *gregs) 12318 1.1 christos { 12319 1.1 christos lwpstatus_t lwpstat; 12320 1.5 christos 12321 1.1 christos memset (&lwpstat, 0, sizeof (lwpstat)); 12322 1.1 christos lwpstat.pr_lwpid = pid >> 16; 12323 1.1 christos lwpstat.pr_cursig = cursig; 12324 1.1 christos #if defined (HAVE_LWPSTATUS_T_PR_REG) 12325 1.1 christos memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); 12326 1.1 christos #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 12327 1.1 christos #if !defined(gregs) 12328 1.1 christos memcpy (lwpstat.pr_context.uc_mcontext.gregs, 12329 1.1 christos gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); 12330 1.19 christos #else 12331 1.1 christos memcpy (lwpstat.pr_context.uc_mcontext.__gregs, 12332 1.1 christos gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); 12333 1.1 christos #endif 12334 1.1 christos #endif 12335 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, 12336 1.1 christos NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); 12337 1.1 christos } 12338 1.1 christos #endif /* HAVE_LWPSTATUS_T */ 12339 1.1 christos 12340 1.1 christos #if defined (HAVE_PSTATUS_T) 12341 1.1 christos char * 12342 1.1 christos elfcore_write_pstatus (bfd *abfd, 12343 1.1 christos char *buf, 12344 1.1 christos int *bufsiz, 12345 1.1 christos long pid, 12346 1.1 christos int cursig ATTRIBUTE_UNUSED, 12347 1.1 christos const void *gregs ATTRIBUTE_UNUSED) 12348 1.1 christos { 12349 1.1 christos #if defined (HAVE_PSTATUS32_T) 12350 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12351 1.1 christos 12352 1.1 christos if (bed->s->elfclass == ELFCLASS32) 12353 1.19 christos { 12354 1.1 christos pstatus32_t pstat; 12355 1.1 christos 12356 1.1 christos memset (&pstat, 0, sizeof (pstat)); 12357 1.1 christos pstat.pr_pid = pid & 0xffff; 12358 1.1 christos buf = elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, 12359 1.1 christos NT_PSTATUS, &pstat, sizeof (pstat)); 12360 1.1 christos return buf; 12361 1.1 christos } 12362 1.1 christos else 12363 1.1 christos #endif 12364 1.19 christos { 12365 1.1 christos pstatus_t pstat; 12366 1.1 christos 12367 1.1 christos memset (&pstat, 0, sizeof (pstat)); 12368 1.1 christos pstat.pr_pid = pid & 0xffff; 12369 1.1 christos buf = elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_CORE, 12370 1.1 christos NT_PSTATUS, &pstat, sizeof (pstat)); 12371 1.1 christos return buf; 12372 1.1 christos } 12373 1.1 christos } 12374 1.1 christos #endif /* HAVE_PSTATUS_T */ 12375 1.1 christos 12376 1.1 christos char * 12377 1.1 christos elfcore_write_prfpreg (bfd *abfd, 12378 1.1 christos char *buf, 12379 1.19 christos int *bufsiz, 12380 1.1 christos const void *fpregs, 12381 1.1 christos int size) 12382 1.1 christos { 12383 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12384 1.1 christos NOTE_NAME_CORE, NT_FPREGSET, fpregs, size); 12385 1.1 christos } 12386 1.1 christos 12387 1.1 christos char * 12388 1.1 christos elfcore_write_prxfpreg (bfd *abfd, 12389 1.1 christos char *buf, 12390 1.19 christos int *bufsiz, 12391 1.1 christos const void *xfpregs, 12392 1.1 christos int size) 12393 1.1 christos { 12394 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12395 1.1 christos NOTE_NAME_LINUX, NT_PRXFPREG, xfpregs, size); 12396 1.1 christos } 12397 1.6 christos 12398 1.19 christos char * 12399 1.6 christos elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, 12400 1.19 christos const void *xfpregs, int size) 12401 1.6 christos { 12402 1.19 christos char *note_name; 12403 1.19 christos 12404 1.1 christos if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) 12405 1.1 christos note_name = NOTE_NAME_FREEBSD; 12406 1.1 christos else 12407 1.1 christos note_name = NOTE_NAME_LINUX; 12408 1.19 christos 12409 1.19 christos return elfcore_write_note (abfd, buf, bufsiz, 12410 1.19 christos note_name, NT_X86_XSTATE, xfpregs, size); 12411 1.19 christos } 12412 1.19 christos 12413 1.19 christos static char * 12414 1.19 christos elfcore_write_sspreg (bfd *abfd, char *buf, int *bufsiz, 12415 1.19 christos const void *ssp, int size) 12416 1.1 christos { 12417 1.14 christos return elfcore_write_note (abfd, buf, bufsiz, 12418 1.14 christos NOTE_NAME_LINUX, NT_X86_SHSTK, ssp, size); 12419 1.14 christos } 12420 1.14 christos 12421 1.19 christos char * 12422 1.19 christos elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz, 12423 1.19 christos const void *regs, int size) 12424 1.19 christos { 12425 1.19 christos return elfcore_write_note (abfd, buf, bufsiz, 12426 1.19 christos NOTE_NAME_FREEBSD, NT_FREEBSD_X86_SEGBASES, 12427 1.19 christos regs, size); 12428 1.19 christos } 12429 1.19 christos 12430 1.19 christos char * 12431 1.14 christos elfcore_write_i386_tls (bfd *abfd, char *buf, int *bufsiz, 12432 1.14 christos const void *regs, int size) 12433 1.14 christos { 12434 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_386_TLS, 12435 1.1 christos regs, size); 12436 1.1 christos } 12437 1.1 christos 12438 1.1 christos char * 12439 1.1 christos elfcore_write_ppc_vmx (bfd *abfd, 12440 1.1 christos char *buf, 12441 1.19 christos int *bufsiz, 12442 1.1 christos const void *ppc_vmx, 12443 1.1 christos int size) 12444 1.1 christos { 12445 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12446 1.11 christos NOTE_NAME_LINUX, NT_PPC_VMX, ppc_vmx, size); 12447 1.11 christos } 12448 1.11 christos 12449 1.11 christos char * 12450 1.11 christos elfcore_write_ppc_vsx (bfd *abfd, 12451 1.11 christos char *buf, 12452 1.19 christos int *bufsiz, 12453 1.11 christos const void *ppc_vsx, 12454 1.11 christos int size) 12455 1.11 christos { 12456 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12457 1.12 christos NOTE_NAME_LINUX, NT_PPC_VSX, ppc_vsx, size); 12458 1.12 christos } 12459 1.12 christos 12460 1.12 christos char * 12461 1.1 christos elfcore_write_ppc_tar (bfd *abfd, 12462 1.1 christos char *buf, 12463 1.19 christos int *bufsiz, 12464 1.11 christos const void *ppc_tar, 12465 1.11 christos int size) 12466 1.11 christos { 12467 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12468 1.12 christos NOTE_NAME_LINUX, NT_PPC_TAR, ppc_tar, size); 12469 1.12 christos } 12470 1.12 christos 12471 1.12 christos char * 12472 1.11 christos elfcore_write_ppc_ppr (bfd *abfd, 12473 1.11 christos char *buf, 12474 1.19 christos int *bufsiz, 12475 1.11 christos const void *ppc_ppr, 12476 1.11 christos int size) 12477 1.11 christos { 12478 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12479 1.12 christos NOTE_NAME_LINUX, NT_PPC_PPR, ppc_ppr, size); 12480 1.12 christos } 12481 1.12 christos 12482 1.12 christos char * 12483 1.11 christos elfcore_write_ppc_dscr (bfd *abfd, 12484 1.11 christos char *buf, 12485 1.19 christos int *bufsiz, 12486 1.11 christos const void *ppc_dscr, 12487 1.11 christos int size) 12488 1.11 christos { 12489 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12490 1.12 christos NOTE_NAME_LINUX, NT_PPC_DSCR, ppc_dscr, size); 12491 1.12 christos } 12492 1.12 christos 12493 1.12 christos char * 12494 1.11 christos elfcore_write_ppc_ebb (bfd *abfd, 12495 1.11 christos char *buf, 12496 1.19 christos int *bufsiz, 12497 1.11 christos const void *ppc_ebb, 12498 1.11 christos int size) 12499 1.11 christos { 12500 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12501 1.12 christos NOTE_NAME_LINUX, NT_PPC_EBB, ppc_ebb, size); 12502 1.12 christos } 12503 1.12 christos 12504 1.12 christos char * 12505 1.11 christos elfcore_write_ppc_pmu (bfd *abfd, 12506 1.11 christos char *buf, 12507 1.19 christos int *bufsiz, 12508 1.11 christos const void *ppc_pmu, 12509 1.11 christos int size) 12510 1.11 christos { 12511 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12512 1.12 christos NOTE_NAME_LINUX, NT_PPC_PMU, ppc_pmu, size); 12513 1.12 christos } 12514 1.12 christos 12515 1.12 christos char * 12516 1.11 christos elfcore_write_ppc_tm_cgpr (bfd *abfd, 12517 1.11 christos char *buf, 12518 1.19 christos int *bufsiz, 12519 1.19 christos const void *ppc_tm_cgpr, 12520 1.11 christos int size) 12521 1.11 christos { 12522 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12523 1.11 christos NOTE_NAME_LINUX, NT_PPC_TM_CGPR, 12524 1.12 christos ppc_tm_cgpr, size); 12525 1.12 christos } 12526 1.12 christos 12527 1.12 christos char * 12528 1.11 christos elfcore_write_ppc_tm_cfpr (bfd *abfd, 12529 1.11 christos char *buf, 12530 1.19 christos int *bufsiz, 12531 1.19 christos const void *ppc_tm_cfpr, 12532 1.11 christos int size) 12533 1.11 christos { 12534 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12535 1.11 christos NOTE_NAME_LINUX, NT_PPC_TM_CFPR, 12536 1.12 christos ppc_tm_cfpr, size); 12537 1.12 christos } 12538 1.12 christos 12539 1.12 christos char * 12540 1.11 christos elfcore_write_ppc_tm_cvmx (bfd *abfd, 12541 1.11 christos char *buf, 12542 1.19 christos int *bufsiz, 12543 1.19 christos const void *ppc_tm_cvmx, 12544 1.11 christos int size) 12545 1.11 christos { 12546 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12547 1.11 christos NOTE_NAME_LINUX, NT_PPC_TM_CVMX, 12548 1.12 christos ppc_tm_cvmx, size); 12549 1.12 christos } 12550 1.12 christos 12551 1.12 christos char * 12552 1.11 christos elfcore_write_ppc_tm_cvsx (bfd *abfd, 12553 1.11 christos char *buf, 12554 1.19 christos int *bufsiz, 12555 1.19 christos const void *ppc_tm_cvsx, 12556 1.11 christos int size) 12557 1.11 christos { 12558 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12559 1.11 christos NOTE_NAME_LINUX, NT_PPC_TM_CVSX, 12560 1.12 christos ppc_tm_cvsx, size); 12561 1.12 christos } 12562 1.12 christos 12563 1.12 christos char * 12564 1.11 christos elfcore_write_ppc_tm_spr (bfd *abfd, 12565 1.11 christos char *buf, 12566 1.19 christos int *bufsiz, 12567 1.19 christos const void *ppc_tm_spr, 12568 1.11 christos int size) 12569 1.11 christos { 12570 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12571 1.11 christos NOTE_NAME_LINUX, NT_PPC_TM_SPR, 12572 1.12 christos ppc_tm_spr, size); 12573 1.12 christos } 12574 1.12 christos 12575 1.12 christos char * 12576 1.11 christos elfcore_write_ppc_tm_ctar (bfd *abfd, 12577 1.11 christos char *buf, 12578 1.19 christos int *bufsiz, 12579 1.19 christos const void *ppc_tm_ctar, 12580 1.11 christos int size) 12581 1.11 christos { 12582 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12583 1.11 christos NOTE_NAME_LINUX, NT_PPC_TM_CTAR, 12584 1.12 christos ppc_tm_ctar, size); 12585 1.12 christos } 12586 1.12 christos 12587 1.12 christos char * 12588 1.11 christos elfcore_write_ppc_tm_cppr (bfd *abfd, 12589 1.11 christos char *buf, 12590 1.19 christos int *bufsiz, 12591 1.19 christos const void *ppc_tm_cppr, 12592 1.11 christos int size) 12593 1.11 christos { 12594 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12595 1.11 christos NOTE_NAME_LINUX, NT_PPC_TM_CPPR, 12596 1.12 christos ppc_tm_cppr, size); 12597 1.12 christos } 12598 1.12 christos 12599 1.12 christos char * 12600 1.11 christos elfcore_write_ppc_tm_cdscr (bfd *abfd, 12601 1.11 christos char *buf, 12602 1.19 christos int *bufsiz, 12603 1.19 christos const void *ppc_tm_cdscr, 12604 1.1 christos int size) 12605 1.1 christos { 12606 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12607 1.1 christos NOTE_NAME_LINUX, NT_PPC_TM_CDSCR, 12608 1.1 christos ppc_tm_cdscr, size); 12609 1.1 christos } 12610 1.1 christos 12611 1.1 christos static char * 12612 1.1 christos elfcore_write_s390_high_gprs (bfd *abfd, 12613 1.1 christos char *buf, 12614 1.19 christos int *bufsiz, 12615 1.1 christos const void *s390_high_gprs, 12616 1.1 christos int size) 12617 1.1 christos { 12618 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12619 1.1 christos NOTE_NAME_LINUX, NT_S390_HIGH_GPRS, 12620 1.11 christos s390_high_gprs, size); 12621 1.11 christos } 12622 1.11 christos 12623 1.11 christos char * 12624 1.1 christos elfcore_write_s390_timer (bfd *abfd, 12625 1.1 christos char *buf, 12626 1.19 christos int *bufsiz, 12627 1.19 christos const void *s390_timer, 12628 1.1 christos int size) 12629 1.1 christos { 12630 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12631 1.1 christos NOTE_NAME_LINUX, NT_S390_TIMER, 12632 1.11 christos s390_timer, size); 12633 1.11 christos } 12634 1.11 christos 12635 1.11 christos char * 12636 1.1 christos elfcore_write_s390_todcmp (bfd *abfd, 12637 1.1 christos char *buf, 12638 1.19 christos int *bufsiz, 12639 1.19 christos const void *s390_todcmp, 12640 1.1 christos int size) 12641 1.1 christos { 12642 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12643 1.1 christos NOTE_NAME_LINUX, NT_S390_TODCMP, 12644 1.11 christos s390_todcmp, size); 12645 1.11 christos } 12646 1.11 christos 12647 1.11 christos char * 12648 1.1 christos elfcore_write_s390_todpreg (bfd *abfd, 12649 1.1 christos char *buf, 12650 1.19 christos int *bufsiz, 12651 1.19 christos const void *s390_todpreg, 12652 1.1 christos int size) 12653 1.1 christos { 12654 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12655 1.1 christos NOTE_NAME_LINUX, NT_S390_TODPREG, 12656 1.11 christos s390_todpreg, size); 12657 1.11 christos } 12658 1.11 christos 12659 1.11 christos char * 12660 1.1 christos elfcore_write_s390_ctrs (bfd *abfd, 12661 1.1 christos char *buf, 12662 1.19 christos int *bufsiz, 12663 1.1 christos const void *s390_ctrs, 12664 1.1 christos int size) 12665 1.1 christos { 12666 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12667 1.11 christos NOTE_NAME_LINUX, NT_S390_CTRS, s390_ctrs, size); 12668 1.11 christos } 12669 1.11 christos 12670 1.11 christos char * 12671 1.1 christos elfcore_write_s390_prefix (bfd *abfd, 12672 1.1 christos char *buf, 12673 1.19 christos int *bufsiz, 12674 1.19 christos const void *s390_prefix, 12675 1.1 christos int size) 12676 1.1 christos { 12677 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12678 1.3 christos NOTE_NAME_LINUX, NT_S390_PREFIX, 12679 1.3 christos s390_prefix, size); 12680 1.3 christos } 12681 1.3 christos 12682 1.3 christos char * 12683 1.3 christos elfcore_write_s390_last_break (bfd *abfd, 12684 1.3 christos char *buf, 12685 1.19 christos int *bufsiz, 12686 1.3 christos const void *s390_last_break, 12687 1.3 christos int size) 12688 1.3 christos { 12689 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, 12690 1.3 christos NOTE_NAME_LINUX, NT_S390_LAST_BREAK, 12691 1.3 christos s390_last_break, size); 12692 1.3 christos } 12693 1.3 christos 12694 1.3 christos char * 12695 1.3 christos elfcore_write_s390_system_call (bfd *abfd, 12696 1.3 christos char *buf, 12697 1.19 christos int *bufsiz, 12698 1.3 christos const void *s390_system_call, 12699 1.3 christos int size) 12700 1.3 christos { 12701 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, 12702 1.3 christos NOTE_NAME_LINUX, NT_S390_SYSTEM_CALL, 12703 1.3 christos s390_system_call, size); 12704 1.3 christos } 12705 1.3 christos 12706 1.3 christos char * 12707 1.3 christos elfcore_write_s390_tdb (bfd *abfd, 12708 1.3 christos char *buf, 12709 1.19 christos int *bufsiz, 12710 1.3 christos const void *s390_tdb, 12711 1.3 christos int size) 12712 1.3 christos { 12713 1.6 christos return elfcore_write_note (abfd, buf, bufsiz, 12714 1.6 christos NOTE_NAME_LINUX, NT_S390_TDB, s390_tdb, size); 12715 1.6 christos } 12716 1.6 christos 12717 1.6 christos char * 12718 1.6 christos elfcore_write_s390_vxrs_low (bfd *abfd, 12719 1.6 christos char *buf, 12720 1.19 christos int *bufsiz, 12721 1.19 christos const void *s390_vxrs_low, 12722 1.6 christos int size) 12723 1.6 christos { 12724 1.6 christos return elfcore_write_note (abfd, buf, bufsiz, 12725 1.6 christos NOTE_NAME_LINUX, NT_S390_VXRS_LOW, 12726 1.6 christos s390_vxrs_low, size); 12727 1.6 christos } 12728 1.6 christos 12729 1.6 christos char * 12730 1.6 christos elfcore_write_s390_vxrs_high (bfd *abfd, 12731 1.6 christos char *buf, 12732 1.19 christos int *bufsiz, 12733 1.6 christos const void *s390_vxrs_high, 12734 1.6 christos int size) 12735 1.6 christos { 12736 1.6 christos return elfcore_write_note (abfd, buf, bufsiz, 12737 1.11 christos NOTE_NAME_LINUX, NT_S390_VXRS_HIGH, 12738 1.11 christos s390_vxrs_high, size); 12739 1.11 christos } 12740 1.11 christos 12741 1.11 christos char * 12742 1.11 christos elfcore_write_s390_gs_cb (bfd *abfd, 12743 1.11 christos char *buf, 12744 1.19 christos int *bufsiz, 12745 1.11 christos const void *s390_gs_cb, 12746 1.11 christos int size) 12747 1.11 christos { 12748 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12749 1.11 christos NOTE_NAME_LINUX, NT_S390_GS_CB, 12750 1.11 christos s390_gs_cb, size); 12751 1.11 christos } 12752 1.11 christos 12753 1.11 christos char * 12754 1.11 christos elfcore_write_s390_gs_bc (bfd *abfd, 12755 1.11 christos char *buf, 12756 1.19 christos int *bufsiz, 12757 1.11 christos const void *s390_gs_bc, 12758 1.11 christos int size) 12759 1.11 christos { 12760 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12761 1.3 christos NOTE_NAME_LINUX, NT_S390_GS_BC, 12762 1.3 christos s390_gs_bc, size); 12763 1.3 christos } 12764 1.3 christos 12765 1.3 christos char * 12766 1.3 christos elfcore_write_arm_vfp (bfd *abfd, 12767 1.3 christos char *buf, 12768 1.19 christos int *bufsiz, 12769 1.19 christos const void *arm_vfp, 12770 1.3 christos int size) 12771 1.3 christos { 12772 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, 12773 1.3 christos NOTE_NAME_LINUX, NT_ARM_VFP, 12774 1.3 christos arm_vfp, size); 12775 1.3 christos } 12776 1.3 christos 12777 1.3 christos char * 12778 1.3 christos elfcore_write_aarch_tls (bfd *abfd, 12779 1.3 christos char *buf, 12780 1.19 christos int *bufsiz, 12781 1.3 christos const void *aarch_tls, 12782 1.3 christos int size) 12783 1.3 christos { 12784 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, 12785 1.3 christos NOTE_NAME_LINUX, NT_ARM_TLS, aarch_tls, size); 12786 1.3 christos } 12787 1.3 christos 12788 1.3 christos char * 12789 1.3 christos elfcore_write_aarch_hw_break (bfd *abfd, 12790 1.3 christos char *buf, 12791 1.19 christos int *bufsiz, 12792 1.19 christos const void *aarch_hw_break, 12793 1.3 christos int size) 12794 1.3 christos { 12795 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, 12796 1.3 christos NOTE_NAME_LINUX, NT_ARM_HW_BREAK, 12797 1.3 christos aarch_hw_break, size); 12798 1.3 christos } 12799 1.3 christos 12800 1.3 christos char * 12801 1.3 christos elfcore_write_aarch_hw_watch (bfd *abfd, 12802 1.3 christos char *buf, 12803 1.19 christos int *bufsiz, 12804 1.19 christos const void *aarch_hw_watch, 12805 1.3 christos int size) 12806 1.3 christos { 12807 1.3 christos return elfcore_write_note (abfd, buf, bufsiz, 12808 1.11 christos NOTE_NAME_LINUX, NT_ARM_HW_WATCH, 12809 1.11 christos aarch_hw_watch, size); 12810 1.11 christos } 12811 1.11 christos 12812 1.11 christos char * 12813 1.11 christos elfcore_write_aarch_sve (bfd *abfd, 12814 1.11 christos char *buf, 12815 1.19 christos int *bufsiz, 12816 1.11 christos const void *aarch_sve, 12817 1.11 christos int size) 12818 1.11 christos { 12819 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12820 1.11 christos NOTE_NAME_LINUX, NT_ARM_SVE, aarch_sve, size); 12821 1.11 christos } 12822 1.11 christos 12823 1.11 christos char * 12824 1.11 christos elfcore_write_aarch_pauth (bfd *abfd, 12825 1.11 christos char *buf, 12826 1.19 christos int *bufsiz, 12827 1.19 christos const void *aarch_pauth, 12828 1.11 christos int size) 12829 1.11 christos { 12830 1.11 christos return elfcore_write_note (abfd, buf, bufsiz, 12831 1.14 christos NOTE_NAME_LINUX, NT_ARM_PAC_MASK, 12832 1.19 christos aarch_pauth, size); 12833 1.19 christos } 12834 1.19 christos 12835 1.19 christos char * 12836 1.19 christos elfcore_write_aarch_mte (bfd *abfd, 12837 1.19 christos char *buf, 12838 1.19 christos int *bufsiz, 12839 1.19 christos const void *aarch_mte, 12840 1.14 christos int size) 12841 1.14 christos { 12842 1.14 christos return elfcore_write_note (abfd, buf, bufsiz, 12843 1.17 christos NOTE_NAME_LINUX, NT_ARM_TAGGED_ADDR_CTRL, 12844 1.17 christos aarch_mte, size); 12845 1.17 christos } 12846 1.17 christos 12847 1.17 christos char * 12848 1.17 christos elfcore_write_aarch_ssve (bfd *abfd, 12849 1.17 christos char *buf, 12850 1.19 christos int *bufsiz, 12851 1.19 christos const void *aarch_ssve, 12852 1.17 christos int size) 12853 1.17 christos { 12854 1.17 christos return elfcore_write_note (abfd, buf, bufsiz, 12855 1.17 christos NOTE_NAME_LINUX, NT_ARM_SSVE, 12856 1.17 christos aarch_ssve, size); 12857 1.17 christos } 12858 1.17 christos 12859 1.17 christos char * 12860 1.17 christos elfcore_write_aarch_za (bfd *abfd, 12861 1.17 christos char *buf, 12862 1.19 christos int *bufsiz, 12863 1.19 christos const void *aarch_za, 12864 1.17 christos int size) 12865 1.17 christos { 12866 1.17 christos return elfcore_write_note (abfd, buf, bufsiz, 12867 1.17 christos NOTE_NAME_LINUX, NT_ARM_ZA, 12868 1.17 christos aarch_za, size); 12869 1.17 christos } 12870 1.17 christos 12871 1.17 christos /* Write the buffer of zt register values in aarch_zt (length SIZE) into 12872 1.17 christos the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being 12873 1.17 christos written into. Return a pointer to the new start of the note buffer, to 12874 1.17 christos replace BUF which may no longer be valid. */ 12875 1.17 christos 12876 1.17 christos char * 12877 1.17 christos elfcore_write_aarch_zt (bfd *abfd, 12878 1.17 christos char *buf, 12879 1.19 christos int *bufsiz, 12880 1.19 christos const void *aarch_zt, 12881 1.19 christos int size) 12882 1.19 christos { 12883 1.19 christos return elfcore_write_note (abfd, buf, bufsiz, 12884 1.19 christos NOTE_NAME_LINUX, NT_ARM_ZT, 12885 1.19 christos aarch_zt, size); 12886 1.19 christos } 12887 1.19 christos 12888 1.19 christos /* Write the buffer of GCS register values in AARCH_GCS (length SIZE) into 12889 1.19 christos the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being 12890 1.19 christos written into. Return a pointer to the new start of the note buffer, to 12891 1.19 christos replace BUF which may no longer be valid. */ 12892 1.19 christos 12893 1.19 christos static char * 12894 1.17 christos elfcore_write_aarch_gcs (bfd *abfd, char *buf, int *bufsiz, 12895 1.17 christos const void *aarch_gcs, int size) 12896 1.17 christos { 12897 1.12 christos return elfcore_write_note (abfd, buf, bufsiz, NOTE_NAME_LINUX, NT_ARM_GCS, 12898 1.12 christos aarch_gcs, size); 12899 1.12 christos } 12900 1.12 christos 12901 1.12 christos char * 12902 1.12 christos elfcore_write_arc_v2 (bfd *abfd, 12903 1.12 christos char *buf, 12904 1.19 christos int *bufsiz, 12905 1.12 christos const void *arc_v2, 12906 1.12 christos int size) 12907 1.12 christos { 12908 1.14 christos return elfcore_write_note (abfd, buf, bufsiz, 12909 1.14 christos NOTE_NAME_LINUX, NT_ARC_V2, arc_v2, size); 12910 1.14 christos } 12911 1.14 christos 12912 1.14 christos char * 12913 1.14 christos elfcore_write_loongarch_cpucfg (bfd *abfd, 12914 1.14 christos char *buf, 12915 1.19 christos int *bufsiz, 12916 1.14 christos const void *loongarch_cpucfg, 12917 1.14 christos int size) 12918 1.14 christos { 12919 1.14 christos return elfcore_write_note (abfd, buf, bufsiz, 12920 1.14 christos NOTE_NAME_LINUX, NT_LARCH_CPUCFG, 12921 1.14 christos loongarch_cpucfg, size); 12922 1.14 christos } 12923 1.14 christos 12924 1.14 christos char * 12925 1.14 christos elfcore_write_loongarch_lbt (bfd *abfd, 12926 1.14 christos char *buf, 12927 1.19 christos int *bufsiz, 12928 1.19 christos const void *loongarch_lbt, 12929 1.14 christos int size) 12930 1.14 christos { 12931 1.14 christos return elfcore_write_note (abfd, buf, bufsiz, 12932 1.14 christos NOTE_NAME_LINUX, NT_LARCH_LBT, 12933 1.14 christos loongarch_lbt, size); 12934 1.14 christos } 12935 1.14 christos 12936 1.14 christos char * 12937 1.14 christos elfcore_write_loongarch_lsx (bfd *abfd, 12938 1.14 christos char *buf, 12939 1.19 christos int *bufsiz, 12940 1.19 christos const void *loongarch_lsx, 12941 1.14 christos int size) 12942 1.14 christos { 12943 1.14 christos return elfcore_write_note (abfd, buf, bufsiz, 12944 1.14 christos NOTE_NAME_LINUX, NT_LARCH_LSX, 12945 1.14 christos loongarch_lsx, size); 12946 1.14 christos } 12947 1.14 christos 12948 1.14 christos char * 12949 1.14 christos elfcore_write_loongarch_lasx (bfd *abfd, 12950 1.14 christos char *buf, 12951 1.19 christos int *bufsiz, 12952 1.19 christos const void *loongarch_lasx, 12953 1.14 christos int size) 12954 1.14 christos { 12955 1.14 christos return elfcore_write_note (abfd, buf, bufsiz, 12956 1.14 christos NOTE_NAME_LINUX, NT_LARCH_LASX, 12957 1.14 christos loongarch_lasx, size); 12958 1.14 christos } 12959 1.14 christos 12960 1.14 christos /* Write the buffer of csr values in CSRS (length SIZE) into the note 12961 1.14 christos buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being 12962 1.14 christos written into. Return a pointer to the new start of the note buffer, to 12963 1.14 christos replace BUF which may no longer be valid. */ 12964 1.14 christos 12965 1.14 christos char * 12966 1.14 christos elfcore_write_riscv_csr (bfd *abfd, 12967 1.14 christos char *buf, 12968 1.19 christos int *bufsiz, 12969 1.14 christos const void *csrs, 12970 1.14 christos int size) 12971 1.14 christos { 12972 1.14 christos return elfcore_write_note (abfd, buf, bufsiz, 12973 1.14 christos NOTE_NAME_GDB, NT_RISCV_CSR, csrs, size); 12974 1.14 christos } 12975 1.14 christos 12976 1.14 christos /* Write the target description (a string) pointed to by TDESC, length 12977 1.14 christos SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the 12978 1.14 christos note is being written into. Return a pointer to the new start of the 12979 1.14 christos note buffer, to replace BUF which may no longer be valid. */ 12980 1.14 christos 12981 1.14 christos char * 12982 1.14 christos elfcore_write_gdb_tdesc (bfd *abfd, 12983 1.14 christos char *buf, 12984 1.19 christos int *bufsiz, 12985 1.14 christos const void *tdesc, 12986 1.14 christos int size) 12987 1.14 christos { 12988 1.1 christos return elfcore_write_note (abfd, buf, bufsiz, 12989 1.1 christos NOTE_NAME_GDB, NT_GDB_TDESC, tdesc, size); 12990 1.1 christos } 12991 1.1 christos 12992 1.1 christos char * 12993 1.1 christos elfcore_write_register_note (bfd *abfd, 12994 1.1 christos char *buf, 12995 1.19 christos int *bufsiz, 12996 1.19 christos const char *section, 12997 1.19 christos const void *data, 12998 1.19 christos int size) 12999 1.19 christos { 13000 1.19 christos static const struct 13001 1.19 christos { 13002 1.19 christos const char * section_name; 13003 1.19 christos char * (*writer) (bfd *, char *, int *, const void *, int); 13004 1.19 christos } 13005 1.19 christos note_writers [] = 13006 1.19 christos { 13007 1.19 christos { NOTE_PSEUDO_SECTION_AARCH_GCS, elfcore_write_aarch_gcs}, 13008 1.19 christos { NOTE_PSEUDO_SECTION_AARCH_HW_BREAK, elfcore_write_aarch_hw_break}, 13009 1.19 christos { NOTE_PSEUDO_SECTION_AARCH_HW_WATCH, elfcore_write_aarch_hw_watch}, 13010 1.19 christos { NOTE_PSEUDO_SECTION_AARCH_MTE, elfcore_write_aarch_mte}, 13011 1.19 christos { NOTE_PSEUDO_SECTION_AARCH_PAUTH, elfcore_write_aarch_pauth}, 13012 1.19 christos { NOTE_PSEUDO_SECTION_AARCH_SSVE, elfcore_write_aarch_ssve}, 13013 1.19 christos { NOTE_PSEUDO_SECTION_AARCH_SVE, elfcore_write_aarch_sve}, 13014 1.19 christos { NOTE_PSEUDO_SECTION_AARCH_TLS, elfcore_write_aarch_tls}, 13015 1.19 christos { NOTE_PSEUDO_SECTION_AARCH_ZA, elfcore_write_aarch_za}, 13016 1.19 christos { NOTE_PSEUDO_SECTION_AARCH_ZT, elfcore_write_aarch_zt}, 13017 1.19 christos { NOTE_PSEUDO_SECTION_ARC_V2, elfcore_write_arc_v2}, 13018 1.19 christos { NOTE_PSEUDO_SECTION_ARM_VFP, elfcore_write_arm_vfp}, 13019 1.19 christos { NOTE_PSEUDO_SECTION_I386_TLS, elfcore_write_i386_tls}, 13020 1.19 christos { NOTE_PSEUDO_SECTION_LOONGARCH_CPUCFG, elfcore_write_loongarch_cpucfg}, 13021 1.19 christos { NOTE_PSEUDO_SECTION_LOONGARCH_LASX, elfcore_write_loongarch_lasx}, 13022 1.19 christos { NOTE_PSEUDO_SECTION_LOONGARCH_LBT, elfcore_write_loongarch_lbt}, 13023 1.19 christos { NOTE_PSEUDO_SECTION_LOONGARCH_LSX, elfcore_write_loongarch_lsx}, 13024 1.19 christos { NOTE_PSEUDO_SECTION_PPC_DSCR, elfcore_write_ppc_dscr}, 13025 1.19 christos { NOTE_PSEUDO_SECTION_PPC_EBB, elfcore_write_ppc_ebb}, 13026 1.19 christos { NOTE_PSEUDO_SECTION_PPC_PMU, elfcore_write_ppc_pmu}, 13027 1.19 christos { NOTE_PSEUDO_SECTION_PPC_PPR, elfcore_write_ppc_ppr}, 13028 1.19 christos { NOTE_PSEUDO_SECTION_PPC_TAR, elfcore_write_ppc_tar}, 13029 1.19 christos { NOTE_PSEUDO_SECTION_PPC_TM_CDSCR, elfcore_write_ppc_tm_cdscr}, 13030 1.19 christos { NOTE_PSEUDO_SECTION_PPC_TM_CFPR, elfcore_write_ppc_tm_cfpr}, 13031 1.19 christos { NOTE_PSEUDO_SECTION_PPC_TM_CGPR, elfcore_write_ppc_tm_cgpr}, 13032 1.19 christos { NOTE_PSEUDO_SECTION_PPC_TM_CPPR, elfcore_write_ppc_tm_cppr}, 13033 1.19 christos { NOTE_PSEUDO_SECTION_PPC_TM_CTAR, elfcore_write_ppc_tm_ctar}, 13034 1.19 christos { NOTE_PSEUDO_SECTION_PPC_TM_CVMX, elfcore_write_ppc_tm_cvmx}, 13035 1.19 christos { NOTE_PSEUDO_SECTION_PPC_TM_CVSX, elfcore_write_ppc_tm_cvsx}, 13036 1.19 christos { NOTE_PSEUDO_SECTION_PPC_TM_SPR, elfcore_write_ppc_tm_spr}, 13037 1.19 christos { NOTE_PSEUDO_SECTION_PPC_VMX, elfcore_write_ppc_vmx}, 13038 1.19 christos { NOTE_PSEUDO_SECTION_PPC_VSX, elfcore_write_ppc_vsx}, 13039 1.19 christos { NOTE_PSEUDO_SECTION_REG2, elfcore_write_prfpreg}, 13040 1.19 christos { NOTE_PSEUDO_SECTION_RISCV_CSR, elfcore_write_riscv_csr}, 13041 1.19 christos { NOTE_PSEUDO_SECTION_S390_CTRS, elfcore_write_s390_ctrs}, 13042 1.19 christos { NOTE_PSEUDO_SECTION_S390_GS_BC, elfcore_write_s390_gs_bc}, 13043 1.19 christos { NOTE_PSEUDO_SECTION_S390_GS_CB, elfcore_write_s390_gs_cb}, 13044 1.19 christos { NOTE_PSEUDO_SECTION_S390_HIGH_GPRS, elfcore_write_s390_high_gprs}, 13045 1.19 christos { NOTE_PSEUDO_SECTION_S390_LAST_BREAK, elfcore_write_s390_last_break}, 13046 1.19 christos { NOTE_PSEUDO_SECTION_S390_PREFIX, elfcore_write_s390_prefix}, 13047 1.19 christos { NOTE_PSEUDO_SECTION_S390_SYSTEM_CALL, elfcore_write_s390_system_call}, 13048 1.19 christos { NOTE_PSEUDO_SECTION_S390_TDB, elfcore_write_s390_tdb}, 13049 1.19 christos { NOTE_PSEUDO_SECTION_S390_TIMER, elfcore_write_s390_timer}, 13050 1.19 christos { NOTE_PSEUDO_SECTION_S390_TODCMP, elfcore_write_s390_todcmp}, 13051 1.19 christos { NOTE_PSEUDO_SECTION_S390_TODPREG, elfcore_write_s390_todpreg}, 13052 1.19 christos { NOTE_PSEUDO_SECTION_S390_VXRS_HIGH, elfcore_write_s390_vxrs_high}, 13053 1.19 christos { NOTE_PSEUDO_SECTION_S390_VXRS_LOW, elfcore_write_s390_vxrs_low}, 13054 1.19 christos { NOTE_PSEUDO_SECTION_SSP, elfcore_write_sspreg}, 13055 1.19 christos { NOTE_PSEUDO_SECTION_TDESC, elfcore_write_gdb_tdesc}, 13056 1.19 christos { NOTE_PSEUDO_SECTION_X86_SEGBASES, elfcore_write_x86_segbases}, 13057 1.19 christos { NOTE_PSEUDO_SECTION_XFP, elfcore_write_prxfpreg}, 13058 1.19 christos { NOTE_PSEUDO_SECTION_XSTATE, elfcore_write_xstatereg} /* NB/ No comma. */ 13059 1.19 christos }; 13060 1.19 christos 13061 1.19 christos int i; 13062 1.1 christos 13063 1.1 christos for (i = ARRAY_SIZE (note_writers); i--;) 13064 1.1 christos if (streq (section, note_writers[i].section_name)) 13065 1.14 christos return note_writers[i].writer (abfd, buf, bufsiz, data, size); 13066 1.14 christos 13067 1.14 christos return NULL; 13068 1.14 christos } 13069 1.14 christos 13070 1.19 christos char * 13071 1.14 christos elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size, 13072 1.14 christos const void *buf, int bufsiz) 13073 1.14 christos { 13074 1.11 christos return elfcore_write_note (obfd, note_data, note_size, 13075 1.11 christos NOTE_NAME_CORE, NT_FILE, buf, bufsiz); 13076 1.1 christos } 13077 1.1 christos 13078 1.1 christos static bool 13079 1.11 christos elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset, 13080 1.11 christos size_t align) 13081 1.11 christos { 13082 1.11 christos char *p; 13083 1.11 christos 13084 1.11 christos /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1. 13085 1.11 christos gABI specifies that PT_NOTE alignment should be aligned to 4 13086 1.14 christos bytes for 32-bit objects and to 8 bytes for 64-bit objects. If 13087 1.11 christos align is less than 4, we use 4 byte alignment. */ 13088 1.1 christos if (align < 4) 13089 1.1 christos align = 4; 13090 1.1 christos if (align != 4 && align != 8) 13091 1.1 christos return false; 13092 1.1 christos 13093 1.1 christos p = buf; 13094 1.1 christos while (p < buf + size) 13095 1.14 christos { 13096 1.1 christos Elf_External_Note *xnp = (Elf_External_Note *) p; 13097 1.1 christos Elf_Internal_Note in; 13098 1.1 christos 13099 1.1 christos if (offsetof (Elf_External_Note, name) > buf - p + size) 13100 1.1 christos return false; 13101 1.1 christos 13102 1.14 christos in.type = H_GET_32 (abfd, xnp->type); 13103 1.1 christos 13104 1.1 christos in.namesz = H_GET_32 (abfd, xnp->namesz); 13105 1.11 christos in.namedata = xnp->name; 13106 1.1 christos if (in.namesz > buf - in.namedata + size) 13107 1.1 christos return false; 13108 1.1 christos 13109 1.1 christos in.descsz = H_GET_32 (abfd, xnp->descsz); 13110 1.14 christos in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align); 13111 1.1 christos in.descpos = offset + (in.descdata - buf); 13112 1.1 christos if (in.descsz != 0 13113 1.11 christos && (in.descdata >= buf + size 13114 1.1 christos || in.descsz > buf - in.descdata + size)) 13115 1.14 christos return false; 13116 1.1 christos 13117 1.1 christos switch (bfd_get_format (abfd)) 13118 1.5 christos { 13119 1.5 christos default: 13120 1.19 christos return true; 13121 1.1 christos 13122 1.5 christos case bfd_core: 13123 1.5 christos { 13124 1.14 christos #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} 13125 1.1 christos static const struct 13126 1.5 christos { 13127 1.1 christos const char * string; 13128 1.5 christos size_t len; 13129 1.19 christos bool (*func) (bfd *, Elf_Internal_Note *); 13130 1.5 christos } 13131 1.14 christos grokers[] = 13132 1.5 christos { 13133 1.12 christos GROKER_ELEMENT ("", elfcore_grok_note), 13134 1.14 christos GROKER_ELEMENT (NOTE_NAME_FREEBSD, elfcore_grok_freebsd_note), 13135 1.19 christos GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), 13136 1.5 christos GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note), 13137 1.5 christos GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), 13138 1.5 christos GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note), 13139 1.5 christos GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note), 13140 1.5 christos GROKER_ELEMENT (NOTE_NAME_CORE, elfcore_grok_solaris_note) 13141 1.5 christos }; 13142 1.5 christos #undef GROKER_ELEMENT 13143 1.5 christos int i; 13144 1.5 christos 13145 1.5 christos for (i = ARRAY_SIZE (grokers); i--;) 13146 1.5 christos { 13147 1.14 christos if (in.namesz >= grokers[i].len 13148 1.5 christos && strncmp (in.namedata, grokers[i].string, 13149 1.5 christos grokers[i].len) == 0) 13150 1.5 christos { 13151 1.5 christos if (! grokers[i].func (abfd, & in)) 13152 1.5 christos return false; 13153 1.1 christos break; 13154 1.1 christos } 13155 1.1 christos } 13156 1.1 christos break; 13157 1.1 christos } 13158 1.14 christos 13159 1.1 christos case bfd_object: 13160 1.3 christos if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) 13161 1.3 christos { 13162 1.3 christos if (! elfobj_grok_gnu_note (abfd, &in)) 13163 1.3 christos return false; 13164 1.14 christos } 13165 1.3 christos else if (in.namesz == sizeof "stapsdt" 13166 1.1 christos && strcmp (in.namedata, "stapsdt") == 0) 13167 1.1 christos { 13168 1.1 christos if (! elfobj_grok_stapsdt_note (abfd, &in)) 13169 1.11 christos return false; 13170 1.1 christos } 13171 1.1 christos break; 13172 1.14 christos } 13173 1.1 christos 13174 1.1 christos p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align); 13175 1.14 christos } 13176 1.11 christos 13177 1.11 christos return true; 13178 1.1 christos } 13179 1.1 christos 13180 1.1 christos bool 13181 1.11 christos elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size, 13182 1.14 christos size_t align) 13183 1.1 christos { 13184 1.1 christos char *buf; 13185 1.14 christos 13186 1.1 christos if (size == 0 || (size + 1) == 0) 13187 1.12 christos return true; 13188 1.1 christos 13189 1.14 christos if (bfd_seek (abfd, offset, SEEK_SET) != 0) 13190 1.1 christos return false; 13191 1.5 christos 13192 1.5 christos buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size); 13193 1.5 christos if (buf == NULL) 13194 1.5 christos return false; 13195 1.12 christos 13196 1.1 christos /* PR 17512: file: ec08f814 13197 1.1 christos 0-termintate the buffer so that string searches will not overflow. */ 13198 1.14 christos buf[size] = 0; 13199 1.1 christos 13200 1.1 christos if (!elf_parse_notes (abfd, buf, size, offset, align)) 13201 1.1 christos { 13202 1.14 christos free (buf); 13203 1.1 christos return false; 13204 1.1 christos } 13205 1.1 christos 13206 1.1 christos free (buf); 13207 1.1 christos return true; 13208 1.1 christos } 13209 1.1 christos 13210 1.1 christos /* Providing external access to the ELF program header table. */ 13212 1.1 christos 13213 1.1 christos /* Return an upper bound on the number of bytes required to store a 13214 1.1 christos copy of ABFD's program header table entries. Return -1 if an error 13215 1.1 christos occurs; bfd_get_error will return an appropriate code. */ 13216 1.1 christos 13217 1.1 christos long 13218 1.1 christos bfd_get_elf_phdr_upper_bound (bfd *abfd) 13219 1.1 christos { 13220 1.1 christos if (abfd->xvec->flavour != bfd_target_elf_flavour) 13221 1.1 christos { 13222 1.1 christos bfd_set_error (bfd_error_wrong_format); 13223 1.1 christos return -1; 13224 1.1 christos } 13225 1.1 christos 13226 1.1 christos return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); 13227 1.1 christos } 13228 1.1 christos 13229 1.1 christos /* Copy ABFD's program header table entries to *PHDRS. The entries 13230 1.1 christos will be stored as an array of Elf_Internal_Phdr structures, as 13231 1.1 christos defined in include/elf/internal.h. To find out how large the 13232 1.1 christos buffer needs to be, call bfd_get_elf_phdr_upper_bound. 13233 1.1 christos 13234 1.1 christos Return the number of program header table entries read, or -1 if an 13235 1.1 christos error occurs; bfd_get_error will return an appropriate code. */ 13236 1.1 christos 13237 1.1 christos int 13238 1.1 christos bfd_get_elf_phdrs (bfd *abfd, void *phdrs) 13239 1.1 christos { 13240 1.1 christos int num_phdrs; 13241 1.1 christos 13242 1.1 christos if (abfd->xvec->flavour != bfd_target_elf_flavour) 13243 1.11 christos { 13244 1.11 christos bfd_set_error (bfd_error_wrong_format); 13245 1.11 christos return -1; 13246 1.1 christos } 13247 1.1 christos 13248 1.1 christos num_phdrs = elf_elfheader (abfd)->e_phnum; 13249 1.1 christos if (num_phdrs != 0) 13250 1.1 christos memcpy (phdrs, elf_tdata (abfd)->phdr, 13251 1.4 christos num_phdrs * sizeof (Elf_Internal_Phdr)); 13252 1.4 christos 13253 1.4 christos return num_phdrs; 13254 1.1 christos } 13255 1.1 christos 13256 1.1 christos enum elf_reloc_type_class 13257 1.1 christos _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, 13258 1.1 christos const asection *rel_sec ATTRIBUTE_UNUSED, 13259 1.1 christos const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) 13260 1.1 christos { 13261 1.1 christos return reloc_class_normal; 13262 1.1 christos } 13263 1.1 christos 13264 1.1 christos /* For RELA architectures, return the relocation value for a 13265 1.1 christos relocation against a local symbol. */ 13266 1.1 christos 13267 1.1 christos bfd_vma 13268 1.1 christos _bfd_elf_rela_local_sym (bfd *abfd, 13269 1.1 christos Elf_Internal_Sym *sym, 13270 1.1 christos asection **psec, 13271 1.1 christos Elf_Internal_Rela *rel) 13272 1.1 christos { 13273 1.1 christos asection *sec = *psec; 13274 1.1 christos bfd_vma relocation; 13275 1.3 christos 13276 1.1 christos relocation = (sec->output_section->vma 13277 1.1 christos + sec->output_offset 13278 1.1 christos + sym->st_value); 13279 1.1 christos if ((sec->flags & SEC_MERGE) 13280 1.1 christos && ELF_ST_TYPE (sym->st_info) == STT_SECTION 13281 1.1 christos && sec->sec_info_type == SEC_INFO_TYPE_MERGE) 13282 1.1 christos { 13283 1.1 christos rel->r_addend = 13284 1.1 christos _bfd_merged_section_offset (abfd, psec, 13285 1.1 christos elf_section_data (sec)->sec_info, 13286 1.1 christos sym->st_value + rel->r_addend); 13287 1.1 christos if (sec != *psec) 13288 1.1 christos { 13289 1.1 christos /* If we have changed the section, and our original section is 13290 1.1 christos marked with SEC_EXCLUDE, it means that the original 13291 1.1 christos SEC_MERGE section has been completely subsumed in some 13292 1.1 christos other SEC_MERGE section. In this case, we need to leave 13293 1.1 christos some info around for --emit-relocs. */ 13294 1.1 christos if ((sec->flags & SEC_EXCLUDE) != 0) 13295 1.1 christos sec->kept_section = *psec; 13296 1.1 christos sec = *psec; 13297 1.1 christos } 13298 1.1 christos rel->r_addend -= relocation; 13299 1.1 christos rel->r_addend += sec->output_section->vma + sec->output_offset; 13300 1.1 christos } 13301 1.1 christos return relocation; 13302 1.1 christos } 13303 1.1 christos 13304 1.1 christos bfd_vma 13305 1.1 christos _bfd_elf_rel_local_sym (bfd *abfd, 13306 1.3 christos Elf_Internal_Sym *sym, 13307 1.1 christos asection **psec, 13308 1.1 christos bfd_vma addend) 13309 1.1 christos { 13310 1.1 christos asection *sec = *psec; 13311 1.1 christos 13312 1.1 christos if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) 13313 1.1 christos return sym->st_value + addend; 13314 1.8 christos 13315 1.8 christos return _bfd_merged_section_offset (abfd, psec, 13316 1.8 christos elf_section_data (sec)->sec_info, 13317 1.8 christos sym->st_value + addend); 13318 1.8 christos } 13319 1.8 christos 13320 1.1 christos /* Adjust an address within a section. Given OFFSET within SEC, return 13321 1.1 christos the new offset within the section, based upon changes made to the 13322 1.1 christos section. Returns -1 if the offset is now invalid. 13323 1.1 christos The offset (in abnd out) is in target sized bytes, however big a 13324 1.1 christos byte may be. */ 13325 1.1 christos 13326 1.1 christos bfd_vma 13327 1.1 christos _bfd_elf_section_offset (bfd *abfd, 13328 1.3 christos struct bfd_link_info *info, 13329 1.1 christos asection *sec, 13330 1.1 christos bfd_vma offset) 13331 1.3 christos { 13332 1.1 christos switch (sec->sec_info_type) 13333 1.8 christos { 13334 1.19 christos case SEC_INFO_TYPE_STABS: 13335 1.19 christos return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, 13336 1.19 christos offset); 13337 1.1 christos case SEC_INFO_TYPE_EH_FRAME: 13338 1.3 christos return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); 13339 1.3 christos 13340 1.8 christos case SEC_INFO_TYPE_SFRAME: 13341 1.3 christos return _bfd_elf_sframe_section_offset (abfd, info, sec, offset); 13342 1.3 christos 13343 1.8 christos default: 13344 1.8 christos if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) 13345 1.8 christos { 13346 1.12 christos /* Reverse the offset. */ 13347 1.12 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13348 1.3 christos bfd_size_type address_size = bed->s->arch_size / 8; 13349 1.1 christos 13350 1.1 christos /* address_size and sec->size are in octets. Convert 13351 1.1 christos to bytes before subtracting the original offset. */ 13352 1.1 christos offset = ((sec->size - address_size) 13353 1.1 christos / bfd_octets_per_byte (abfd, sec) - offset); 13354 1.1 christos } 13355 1.1 christos return offset; 13356 1.1 christos } 13357 1.1 christos } 13358 1.1 christos 13359 1.1 christos long 13361 1.1 christos _bfd_elf_get_synthetic_symtab (bfd *abfd, 13362 1.1 christos long symcount ATTRIBUTE_UNUSED, 13363 1.1 christos asymbol **syms ATTRIBUTE_UNUSED, 13364 1.1 christos long dynsymcount, 13365 1.14 christos asymbol **dynsyms, 13366 1.1 christos asymbol **ret) 13367 1.1 christos { 13368 1.1 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13369 1.1 christos asection *relplt; 13370 1.1 christos asymbol *s; 13371 1.1 christos const char *relplt_name; 13372 1.1 christos bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool); 13373 1.1 christos arelent *p; 13374 1.1 christos long count, i, n; 13375 1.1 christos size_t size; 13376 1.1 christos Elf_Internal_Shdr *hdr; 13377 1.1 christos char *names; 13378 1.1 christos asection *plt; 13379 1.1 christos 13380 1.1 christos *ret = NULL; 13381 1.1 christos 13382 1.1 christos if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) 13383 1.1 christos return 0; 13384 1.1 christos 13385 1.1 christos if (dynsymcount <= 0) 13386 1.1 christos return 0; 13387 1.1 christos 13388 1.1 christos if (!bed->plt_sym_val) 13389 1.1 christos return 0; 13390 1.1 christos 13391 1.1 christos relplt_name = bed->relplt_name; 13392 1.1 christos if (relplt_name == NULL) 13393 1.1 christos relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; 13394 1.1 christos relplt = bfd_get_section_by_name (abfd, relplt_name); 13395 1.1 christos if (relplt == NULL) 13396 1.1 christos return 0; 13397 1.1 christos 13398 1.1 christos hdr = &elf_section_data (relplt)->this_hdr; 13399 1.1 christos if (hdr->sh_link != elf_dynsymtab (abfd) 13400 1.1 christos || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) 13401 1.14 christos return 0; 13402 1.1 christos 13403 1.1 christos plt = bfd_get_section_by_name (abfd, ".plt"); 13404 1.17 christos if (plt == NULL) 13405 1.1 christos return 0; 13406 1.1 christos 13407 1.1 christos slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; 13408 1.1 christos if (! (*slurp_relocs) (abfd, relplt, dynsyms, true)) 13409 1.1 christos return -1; 13410 1.1 christos 13411 1.1 christos count = NUM_SHDR_ENTRIES (hdr); 13412 1.1 christos size = count * sizeof (asymbol); 13413 1.1 christos p = relplt->relocation; 13414 1.1 christos for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) 13415 1.1 christos { 13416 1.1 christos size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); 13417 1.1 christos if (p->addend != 0) 13418 1.1 christos { 13419 1.1 christos #ifdef BFD64 13420 1.1 christos size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); 13421 1.1 christos #else 13422 1.1 christos size += sizeof ("+0x") - 1 + 8; 13423 1.1 christos #endif 13424 1.1 christos } 13425 1.1 christos } 13426 1.1 christos 13427 1.1 christos s = *ret = (asymbol *) bfd_malloc (size); 13428 1.1 christos if (s == NULL) 13429 1.1 christos return -1; 13430 1.1 christos 13431 1.1 christos names = (char *) (s + count); 13432 1.1 christos p = relplt->relocation; 13433 1.1 christos n = 0; 13434 1.1 christos for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) 13435 1.1 christos { 13436 1.1 christos size_t len; 13437 1.1 christos bfd_vma addr; 13438 1.1 christos 13439 1.1 christos addr = bed->plt_sym_val (i, plt, p); 13440 1.1 christos if (addr == (bfd_vma) -1) 13441 1.1 christos continue; 13442 1.1 christos 13443 1.1 christos *s = **p->sym_ptr_ptr; 13444 1.1 christos /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since 13445 1.1 christos we are defining a symbol, ensure one of them is set. */ 13446 1.1 christos if ((s->flags & BSF_LOCAL) == 0) 13447 1.1 christos s->flags |= BSF_GLOBAL; 13448 1.1 christos s->flags |= BSF_SYNTHETIC; 13449 1.1 christos s->section = plt; 13450 1.1 christos s->value = addr - plt->vma; 13451 1.1 christos s->name = names; 13452 1.3 christos s->udata.p = NULL; 13453 1.1 christos len = strlen ((*p->sym_ptr_ptr)->name); 13454 1.1 christos memcpy (names, (*p->sym_ptr_ptr)->name, len); 13455 1.1 christos names += len; 13456 1.1 christos if (p->addend != 0) 13457 1.1 christos { 13458 1.1 christos char buf[30], *a; 13459 1.1 christos 13460 1.1 christos memcpy (names, "+0x", sizeof ("+0x") - 1); 13461 1.1 christos names += sizeof ("+0x") - 1; 13462 1.1 christos bfd_sprintf_vma (abfd, buf, p->addend); 13463 1.1 christos for (a = buf; *a == '0'; ++a) 13464 1.1 christos ; 13465 1.1 christos len = strlen (a); 13466 1.1 christos memcpy (names, a, len); 13467 1.1 christos names += len; 13468 1.1 christos } 13469 1.1 christos memcpy (names, "@plt", sizeof ("@plt")); 13470 1.9 christos names += sizeof ("@plt"); 13471 1.9 christos ++s, ++n; 13472 1.11 christos } 13473 1.11 christos 13474 1.11 christos return n; 13475 1.1 christos } 13476 1.11 christos 13477 1.9 christos /* It is only used by x86-64 so far. 13478 1.1 christos ??? This repeats *COM* id of zero. sec->id is supposed to be unique, 13479 1.14 christos but current usage would allow all of _bfd_std_section to be zero. */ 13480 1.12 christos static const asymbol lcomm_sym 13481 1.1 christos = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section); 13482 1.12 christos asection _bfd_elf_large_com_section 13483 1.1 christos = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym, 13484 1.1 christos "LARGE_COMMON", 0, SEC_IS_COMMON); 13485 1.1 christos 13486 1.12 christos bool 13487 1.12 christos _bfd_elf_final_write_processing (bfd *abfd) 13488 1.1 christos { 13489 1.12 christos Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */ 13490 1.14 christos 13491 1.14 christos i_ehdrp = elf_elfheader (abfd); 13492 1.12 christos 13493 1.12 christos if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) 13494 1.12 christos i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; 13495 1.12 christos 13496 1.12 christos /* Set the osabi field to ELFOSABI_GNU if the binary contains 13497 1.12 christos SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type 13498 1.12 christos or STB_GNU_UNIQUE binding. */ 13499 1.12 christos if (elf_tdata (abfd)->has_gnu_osabi != 0) 13500 1.14 christos { 13501 1.14 christos if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) 13502 1.12 christos i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; 13503 1.14 christos else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU 13504 1.14 christos && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD) 13505 1.12 christos { 13506 1.14 christos if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) 13507 1.14 christos _bfd_error_handler (_("GNU_MBIND section is supported only by GNU " 13508 1.14 christos "and FreeBSD targets")); 13509 1.14 christos if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc) 13510 1.14 christos _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported " 13511 1.12 christos "only by GNU and FreeBSD targets")); 13512 1.14 christos if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique) 13513 1.12 christos _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported " 13514 1.12 christos "only by GNU and FreeBSD targets")); 13515 1.14 christos if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain) 13516 1.1 christos _bfd_error_handler (_("GNU_RETAIN section is supported " 13517 1.1 christos "only by GNU and FreeBSD targets")); 13518 1.1 christos bfd_set_error (bfd_error_sorry); 13519 1.1 christos return false; 13520 1.1 christos } 13521 1.1 christos } 13522 1.1 christos return true; 13523 1.14 christos } 13524 1.1 christos 13525 1.1 christos 13526 1.1 christos /* Return TRUE for ELF symbol types that represent functions. 13527 1.1 christos This is the default version of this function, which is sufficient for 13528 1.1 christos most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ 13529 1.3 christos 13530 1.3 christos bool 13531 1.3 christos _bfd_elf_is_function_type (unsigned int type) 13532 1.3 christos { 13533 1.3 christos return (type == STT_FUNC 13534 1.3 christos || type == STT_GNU_IFUNC); 13535 1.3 christos } 13536 1.3 christos 13537 1.3 christos /* If the ELF symbol SYM might be a function in SEC, return the 13538 1.3 christos function size and set *CODE_OFF to the function's entry point, 13539 1.14 christos otherwise return zero. */ 13540 1.3 christos 13541 1.3 christos bfd_size_type 13542 1.3 christos _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, 13543 1.3 christos bfd_vma *code_off) 13544 1.3 christos { 13545 1.3 christos bfd_size_type size; 13546 1.14 christos elf_symbol_type * elf_sym = (elf_symbol_type *) sym; 13547 1.14 christos 13548 1.14 christos if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT 13549 1.14 christos | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 13550 1.14 christos || sym->section != sec) 13551 1.14 christos return 0; 13552 1.14 christos 13553 1.14 christos size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size; 13554 1.14 christos 13555 1.14 christos /* In theory we should check that the symbol's type satisfies 13556 1.14 christos _bfd_elf_is_function_type(), but there are some function-like 13557 1.14 christos symbols which would fail this test. (eg _start). Instead 13558 1.14 christos we check for hidden, local, notype symbols with zero size. 13559 1.14 christos This type of symbol is generated by the annobin plugin for gcc 13560 1.3 christos and clang, and should not be considered to be a function symbol. */ 13561 1.14 christos if (size == 0 13562 1.14 christos && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL) 13563 1.3 christos && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE 13564 1.12 christos && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN) 13565 1.12 christos return 0; 13566 1.12 christos 13567 1.12 christos *code_off = sym->value; 13568 1.12 christos /* Do not return 0 for the function's size. */ 13569 1.12 christos return size ? size : 1; 13570 1.12 christos } 13571 1.12 christos 13572 1.12 christos /* Set to non-zero to enable some debug messages. */ 13573 1.12 christos #define DEBUG_SECONDARY_RELOCS 0 13574 1.14 christos 13575 1.12 christos /* An internal-to-the-bfd-library only section type 13576 1.12 christos used to indicate a cached secondary reloc section. */ 13577 1.12 christos #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA) 13578 1.12 christos 13579 1.12 christos /* Create a BFD section to hold a secondary reloc section. */ 13580 1.12 christos 13581 1.12 christos bool 13582 1.14 christos _bfd_elf_init_secondary_reloc_section (bfd * abfd, 13583 1.12 christos Elf_Internal_Shdr *hdr, 13584 1.12 christos const char * name, 13585 1.12 christos unsigned int shindex) 13586 1.12 christos { 13587 1.12 christos /* We only support RELA secondary relocs. */ 13588 1.12 christos if (hdr->sh_type != SHT_RELA) 13589 1.12 christos return false; 13590 1.12 christos 13591 1.12 christos #if DEBUG_SECONDARY_RELOCS 13592 1.12 christos fprintf (stderr, "secondary reloc section %s encountered\n", name); 13593 1.14 christos #endif 13594 1.14 christos hdr->sh_type = SHT_SECONDARY_RELOC; 13595 1.14 christos return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 13596 1.14 christos } 13597 1.14 christos 13598 1.12 christos /* Read in any secondary relocs associated with SEC. */ 13599 1.12 christos 13600 1.12 christos bool 13601 1.14 christos _bfd_elf_slurp_secondary_reloc_section (bfd * abfd, 13602 1.12 christos asection * sec, 13603 1.14 christos asymbol ** symbols, 13604 1.12 christos bool dynamic) 13605 1.12 christos { 13606 1.12 christos const struct elf_backend_data * const ebd = get_elf_backend_data (abfd); 13607 1.12 christos asection * relsec; 13608 1.12 christos bool result = true; 13609 1.12 christos bfd_vma (*r_sym) (bfd_vma); 13610 1.12 christos ufile_ptr filesize; 13611 1.14 christos 13612 1.14 christos #if BFD_DEFAULT_TARGET_SIZE > 32 13613 1.14 christos if (bfd_arch_bits_per_address (abfd) != 32) 13614 1.14 christos r_sym = elf64_r_sym; 13615 1.12 christos else 13616 1.12 christos #endif 13617 1.14 christos r_sym = elf32_r_sym; 13618 1.12 christos 13619 1.12 christos if (!elf_section_data (sec)->has_secondary_relocs) 13620 1.12 christos return true; 13621 1.12 christos 13622 1.12 christos /* Discover if there are any secondary reloc sections 13623 1.12 christos associated with SEC. */ 13624 1.12 christos filesize = bfd_get_file_size (abfd); 13625 1.12 christos for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next) 13626 1.12 christos { 13627 1.12 christos Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr; 13628 1.12 christos 13629 1.12 christos if (hdr->sh_type == SHT_SECONDARY_RELOC 13630 1.12 christos && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx 13631 1.14 christos && (hdr->sh_entsize == ebd->s->sizeof_rel 13632 1.12 christos || hdr->sh_entsize == ebd->s->sizeof_rela)) 13633 1.12 christos { 13634 1.14 christos bfd_byte * native_relocs; 13635 1.12 christos bfd_byte * native_reloc; 13636 1.12 christos arelent * internal_relocs; 13637 1.12 christos arelent * internal_reloc; 13638 1.14 christos size_t i; 13639 1.12 christos unsigned int entsize; 13640 1.12 christos unsigned int symcount; 13641 1.12 christos bfd_size_type reloc_count; 13642 1.12 christos size_t amt; 13643 1.12 christos 13644 1.12 christos if (ebd->elf_info_to_howto == NULL) 13645 1.12 christos return false; 13646 1.14 christos 13647 1.14 christos #if DEBUG_SECONDARY_RELOCS 13648 1.14 christos fprintf (stderr, "read secondary relocs for %s from %s\n", 13649 1.14 christos sec->name, relsec->name); 13650 1.14 christos #endif 13651 1.14 christos entsize = hdr->sh_entsize; 13652 1.14 christos 13653 1.14 christos if (filesize != 0 13654 1.14 christos && ((ufile_ptr) hdr->sh_offset > filesize 13655 1.12 christos || hdr->sh_size > filesize - hdr->sh_offset)) 13656 1.12 christos { 13657 1.12 christos bfd_set_error (bfd_error_file_truncated); 13658 1.14 christos result = false; 13659 1.12 christos continue; 13660 1.12 christos } 13661 1.12 christos 13662 1.12 christos native_relocs = bfd_malloc (hdr->sh_size); 13663 1.12 christos if (native_relocs == NULL) 13664 1.12 christos { 13665 1.12 christos result = false; 13666 1.12 christos continue; 13667 1.14 christos } 13668 1.12 christos 13669 1.12 christos reloc_count = NUM_SHDR_ENTRIES (hdr); 13670 1.12 christos if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt)) 13671 1.12 christos { 13672 1.12 christos free (native_relocs); 13673 1.12 christos bfd_set_error (bfd_error_file_too_big); 13674 1.12 christos result = false; 13675 1.14 christos continue; 13676 1.12 christos } 13677 1.12 christos 13678 1.12 christos internal_relocs = (arelent *) bfd_alloc (abfd, amt); 13679 1.12 christos if (internal_relocs == NULL) 13680 1.17 christos { 13681 1.12 christos free (native_relocs); 13682 1.12 christos result = false; 13683 1.12 christos continue; 13684 1.12 christos } 13685 1.14 christos 13686 1.12 christos if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 13687 1.12 christos || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size) 13688 1.12 christos { 13689 1.14 christos free (native_relocs); 13690 1.14 christos /* The internal_relocs will be freed when 13691 1.14 christos the memory for the bfd is released. */ 13692 1.14 christos result = false; 13693 1.12 christos continue; 13694 1.12 christos } 13695 1.12 christos 13696 1.12 christos if (dynamic) 13697 1.12 christos symcount = bfd_get_dynamic_symcount (abfd); 13698 1.12 christos else 13699 1.14 christos symcount = bfd_get_symcount (abfd); 13700 1.12 christos 13701 1.12 christos for (i = 0, internal_reloc = internal_relocs, 13702 1.14 christos native_reloc = native_relocs; 13703 1.14 christos i < reloc_count; 13704 1.14 christos i++, internal_reloc++, native_reloc += entsize) 13705 1.14 christos { 13706 1.12 christos bool res; 13707 1.12 christos Elf_Internal_Rela rela; 13708 1.12 christos 13709 1.12 christos if (entsize == ebd->s->sizeof_rel) 13710 1.12 christos ebd->s->swap_reloc_in (abfd, native_reloc, & rela); 13711 1.12 christos else /* entsize == ebd->s->sizeof_rela */ 13712 1.12 christos ebd->s->swap_reloca_in (abfd, native_reloc, & rela); 13713 1.12 christos 13714 1.12 christos /* The address of an ELF reloc is section relative for an object 13715 1.12 christos file, and absolute for an executable file or shared library. 13716 1.12 christos The address of a normal BFD reloc is always section relative, 13717 1.12 christos and the address of a dynamic reloc is absolute.. */ 13718 1.12 christos if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) 13719 1.12 christos internal_reloc->address = rela.r_offset; 13720 1.18 christos else 13721 1.12 christos internal_reloc->address = rela.r_offset - sec->vma; 13722 1.12 christos 13723 1.12 christos if (r_sym (rela.r_info) == STN_UNDEF) 13724 1.12 christos { 13725 1.12 christos /* FIXME: This and the error case below mean that we 13726 1.14 christos have a symbol on relocs that is not elf_symbol_type. */ 13727 1.12 christos internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol; 13728 1.12 christos } 13729 1.18 christos else if (r_sym (rela.r_info) > symcount) 13730 1.14 christos { 13731 1.12 christos _bfd_error_handler 13732 1.12 christos /* xgettext:c-format */ 13733 1.12 christos (_("%pB(%pA): relocation %zu has invalid symbol index %lu"), 13734 1.12 christos abfd, sec, i, (long) r_sym (rela.r_info)); 13735 1.12 christos bfd_set_error (bfd_error_bad_value); 13736 1.12 christos internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol; 13737 1.12 christos result = false; 13738 1.12 christos } 13739 1.12 christos else 13740 1.12 christos { 13741 1.12 christos asymbol **ps; 13742 1.12 christos 13743 1.12 christos ps = symbols + r_sym (rela.r_info) - 1; 13744 1.12 christos internal_reloc->sym_ptr_ptr = ps; 13745 1.12 christos /* Make sure that this symbol is not removed by strip. */ 13746 1.12 christos (*ps)->flags |= BSF_KEEP; 13747 1.12 christos } 13748 1.14 christos 13749 1.14 christos internal_reloc->addend = rela.r_addend; 13750 1.12 christos 13751 1.12 christos res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela); 13752 1.14 christos if (! res || internal_reloc->howto == NULL) 13753 1.12 christos { 13754 1.12 christos #if DEBUG_SECONDARY_RELOCS 13755 1.12 christos fprintf (stderr, 13756 1.12 christos "there is no howto associated with reloc %lx\n", 13757 1.12 christos rela.r_info); 13758 1.12 christos #endif 13759 1.12 christos result = false; 13760 1.12 christos } 13761 1.12 christos } 13762 1.12 christos 13763 1.12 christos free (native_relocs); 13764 1.12 christos /* Store the internal relocs. */ 13765 1.12 christos elf_section_data (relsec)->sec_info = internal_relocs; 13766 1.12 christos } 13767 1.14 christos } 13768 1.14 christos 13769 1.14 christos return result; 13770 1.14 christos } 13771 1.14 christos 13772 1.12 christos /* Set the ELF section header fields of an output secondary reloc section. */ 13773 1.12 christos 13774 1.12 christos bool 13775 1.12 christos _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED, 13776 1.12 christos bfd *obfd ATTRIBUTE_UNUSED, 13777 1.12 christos const Elf_Internal_Shdr *isection, 13778 1.14 christos Elf_Internal_Shdr *osection) 13779 1.12 christos { 13780 1.12 christos asection * isec; 13781 1.14 christos asection * osec; 13782 1.12 christos struct bfd_elf_section_data * esd; 13783 1.12 christos 13784 1.12 christos if (isection == NULL) 13785 1.14 christos return false; 13786 1.12 christos 13787 1.12 christos if (isection->sh_type != SHT_SECONDARY_RELOC) 13788 1.12 christos return true; 13789 1.14 christos 13790 1.12 christos isec = isection->bfd_section; 13791 1.12 christos if (isec == NULL) 13792 1.12 christos return false; 13793 1.12 christos 13794 1.12 christos osec = osection->bfd_section; 13795 1.12 christos if (osec == NULL) 13796 1.12 christos return false; 13797 1.12 christos 13798 1.12 christos esd = elf_section_data (osec); 13799 1.12 christos BFD_ASSERT (esd->sec_info == NULL); 13800 1.12 christos esd->sec_info = elf_section_data (isec)->sec_info; 13801 1.14 christos osection->sh_type = SHT_RELA; 13802 1.14 christos osection->sh_link = elf_onesymtab (obfd); 13803 1.12 christos if (osection->sh_link == 0) 13804 1.12 christos { 13805 1.14 christos /* There is no symbol table - we are hosed... */ 13806 1.12 christos _bfd_error_handler 13807 1.12 christos /* xgettext:c-format */ 13808 1.14 christos (_("%pB(%pA): link section cannot be set" 13809 1.14 christos " because the output file does not have a symbol table"), 13810 1.12 christos obfd, osec); 13811 1.12 christos bfd_set_error (bfd_error_bad_value); 13812 1.12 christos return false; 13813 1.12 christos } 13814 1.12 christos 13815 1.12 christos /* Find the output section that corresponds to the isection's 13816 1.12 christos sh_info link. */ 13817 1.12 christos if (isection->sh_info == 0 13818 1.14 christos || isection->sh_info >= elf_numsections (ibfd)) 13819 1.12 christos { 13820 1.12 christos _bfd_error_handler 13821 1.12 christos /* xgettext:c-format */ 13822 1.12 christos (_("%pB(%pA): info section index is invalid"), 13823 1.12 christos obfd, osec); 13824 1.12 christos bfd_set_error (bfd_error_bad_value); 13825 1.12 christos return false; 13826 1.12 christos } 13827 1.12 christos 13828 1.12 christos isection = elf_elfsections (ibfd)[isection->sh_info]; 13829 1.14 christos 13830 1.14 christos if (isection == NULL 13831 1.12 christos || isection->bfd_section == NULL 13832 1.12 christos || isection->bfd_section->output_section == NULL) 13833 1.14 christos { 13834 1.12 christos _bfd_error_handler 13835 1.12 christos /* xgettext:c-format */ 13836 1.12 christos (_("%pB(%pA): info section index cannot be set" 13837 1.12 christos " because the section is not in the output"), 13838 1.12 christos obfd, osec); 13839 1.14 christos bfd_set_error (bfd_error_bad_value); 13840 1.12 christos return false; 13841 1.12 christos } 13842 1.12 christos 13843 1.12 christos esd = elf_section_data (isection->bfd_section->output_section); 13844 1.12 christos BFD_ASSERT (esd != NULL); 13845 1.12 christos osection->sh_info = esd->this_idx; 13846 1.12 christos esd->has_secondary_relocs = true; 13847 1.14 christos #if DEBUG_SECONDARY_RELOCS 13848 1.12 christos fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n", 13849 1.12 christos osec->name, osection->sh_link, osection->sh_info); 13850 1.12 christos fprintf (stderr, "mark section %s as having secondary relocs\n", 13851 1.12 christos bfd_section_name (isection->bfd_section->output_section)); 13852 1.12 christos #endif 13853 1.12 christos 13854 1.12 christos return true; 13855 1.12 christos } 13856 1.12 christos 13857 1.14 christos /* Write out a secondary reloc section. 13858 1.12 christos 13859 1.12 christos FIXME: Currently this function can result in a serious performance penalty 13860 1.12 christos for files with secondary relocs and lots of sections. The proper way to 13861 1.12 christos fix this is for _bfd_elf_copy_special_section_fields() to chain secondary 13862 1.12 christos relocs together and then to have this function just walk that chain. */ 13863 1.12 christos 13864 1.14 christos bool 13865 1.12 christos _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec) 13866 1.12 christos { 13867 1.14 christos const struct elf_backend_data * const ebd = get_elf_backend_data (abfd); 13868 1.12 christos bfd_vma addr_offset; 13869 1.12 christos asection * relsec; 13870 1.12 christos bfd_vma (*r_info) (bfd_vma, bfd_vma); 13871 1.12 christos bool result = true; 13872 1.12 christos 13873 1.12 christos if (sec == NULL) 13874 1.12 christos return false; 13875 1.12 christos 13876 1.12 christos #if BFD_DEFAULT_TARGET_SIZE > 32 13877 1.12 christos if (bfd_arch_bits_per_address (abfd) != 32) 13878 1.12 christos r_info = elf64_r_info; 13879 1.12 christos else 13880 1.12 christos #endif 13881 1.12 christos r_info = elf32_r_info; 13882 1.12 christos 13883 1.12 christos /* The address of an ELF reloc is section relative for an object 13884 1.12 christos file, and absolute for an executable file or shared library. 13885 1.12 christos The address of a BFD reloc is always section relative. */ 13886 1.12 christos addr_offset = 0; 13887 1.12 christos if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) 13888 1.12 christos addr_offset = sec->vma; 13889 1.12 christos 13890 1.12 christos /* Discover if there are any secondary reloc sections 13891 1.12 christos associated with SEC. */ 13892 1.12 christos for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next) 13893 1.12 christos { 13894 1.12 christos const struct bfd_elf_section_data * const esd = elf_section_data (relsec); 13895 1.14 christos Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr; 13896 1.14 christos 13897 1.14 christos if (hdr->sh_type == SHT_RELA 13898 1.12 christos && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx) 13899 1.12 christos { 13900 1.12 christos asymbol * last_sym; 13901 1.12 christos int last_sym_idx; 13902 1.12 christos size_t reloc_count; 13903 1.12 christos size_t idx; 13904 1.12 christos bfd_size_type entsize; 13905 1.12 christos arelent * src_irel; 13906 1.12 christos bfd_byte * dst_rela; 13907 1.12 christos 13908 1.14 christos if (hdr->contents != NULL) 13909 1.12 christos { 13910 1.12 christos _bfd_error_handler 13911 1.12 christos /* xgettext:c-format */ 13912 1.14 christos (_("%pB(%pA): error: secondary reloc section processed twice"), 13913 1.14 christos abfd, relsec); 13914 1.12 christos bfd_set_error (bfd_error_bad_value); 13915 1.12 christos result = false; 13916 1.12 christos continue; 13917 1.14 christos } 13918 1.14 christos 13919 1.12 christos entsize = hdr->sh_entsize; 13920 1.12 christos if (entsize == 0) 13921 1.14 christos { 13922 1.14 christos _bfd_error_handler 13923 1.14 christos /* xgettext:c-format */ 13924 1.14 christos (_("%pB(%pA): error: secondary reloc section" 13925 1.14 christos " has zero sized entries"), 13926 1.14 christos abfd, relsec); 13927 1.14 christos bfd_set_error (bfd_error_bad_value); 13928 1.14 christos result = false; 13929 1.14 christos continue; 13930 1.14 christos } 13931 1.14 christos else if (entsize != ebd->s->sizeof_rel 13932 1.14 christos && entsize != ebd->s->sizeof_rela) 13933 1.14 christos { 13934 1.12 christos _bfd_error_handler 13935 1.12 christos /* xgettext:c-format */ 13936 1.12 christos (_("%pB(%pA): error: secondary reloc section" 13937 1.14 christos " has non-standard sized entries"), 13938 1.14 christos abfd, relsec); 13939 1.14 christos bfd_set_error (bfd_error_bad_value); 13940 1.12 christos result = false; 13941 1.12 christos continue; 13942 1.12 christos } 13943 1.12 christos 13944 1.12 christos reloc_count = hdr->sh_size / entsize; 13945 1.12 christos hdr->sh_size = entsize * reloc_count; 13946 1.14 christos if (reloc_count == 0) 13947 1.12 christos { 13948 1.12 christos _bfd_error_handler 13949 1.12 christos /* xgettext:c-format */ 13950 1.12 christos (_("%pB(%pA): error: secondary reloc section is empty!"), 13951 1.12 christos abfd, relsec); 13952 1.12 christos bfd_set_error (bfd_error_bad_value); 13953 1.19 christos result = false; 13954 1.12 christos continue; 13955 1.12 christos } 13956 1.12 christos 13957 1.12 christos hdr->contents = bfd_alloc (abfd, hdr->sh_size); 13958 1.12 christos if (hdr->contents == NULL) 13959 1.12 christos continue; 13960 1.12 christos relsec->alloced = 1; 13961 1.12 christos 13962 1.12 christos #if DEBUG_SECONDARY_RELOCS 13963 1.12 christos fprintf (stderr, "write %u secondary relocs for %s from %s\n", 13964 1.12 christos reloc_count, sec->name, relsec->name); 13965 1.12 christos #endif 13966 1.12 christos last_sym = NULL; 13967 1.14 christos last_sym_idx = 0; 13968 1.14 christos dst_rela = hdr->contents; 13969 1.12 christos src_irel = (arelent *) esd->sec_info; 13970 1.12 christos if (src_irel == NULL) 13971 1.14 christos { 13972 1.12 christos _bfd_error_handler 13973 1.12 christos /* xgettext:c-format */ 13974 1.12 christos (_("%pB(%pA): error: internal relocs missing" 13975 1.14 christos " for secondary reloc section"), 13976 1.12 christos abfd, relsec); 13977 1.12 christos bfd_set_error (bfd_error_bad_value); 13978 1.12 christos result = false; 13979 1.12 christos continue; 13980 1.12 christos } 13981 1.12 christos 13982 1.12 christos for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize) 13983 1.12 christos { 13984 1.12 christos Elf_Internal_Rela src_rela; 13985 1.12 christos arelent *ptr; 13986 1.12 christos asymbol *sym; 13987 1.14 christos int n; 13988 1.12 christos 13989 1.12 christos ptr = src_irel + idx; 13990 1.14 christos if (ptr == NULL) 13991 1.12 christos { 13992 1.12 christos _bfd_error_handler 13993 1.12 christos /* xgettext:c-format */ 13994 1.12 christos (_("%pB(%pA): error: reloc table entry %zu is empty"), 13995 1.12 christos abfd, relsec, idx); 13996 1.12 christos bfd_set_error (bfd_error_bad_value); 13997 1.12 christos result = false; 13998 1.12 christos break; 13999 1.12 christos } 14000 1.12 christos 14001 1.12 christos if (ptr->sym_ptr_ptr == NULL) 14002 1.12 christos { 14003 1.12 christos /* FIXME: Is this an error ? */ 14004 1.12 christos n = 0; 14005 1.12 christos } 14006 1.12 christos else 14007 1.12 christos { 14008 1.12 christos sym = *ptr->sym_ptr_ptr; 14009 1.12 christos 14010 1.12 christos if (sym == last_sym) 14011 1.12 christos n = last_sym_idx; 14012 1.14 christos else 14013 1.14 christos { 14014 1.12 christos n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym); 14015 1.12 christos if (n < 0) 14016 1.14 christos { 14017 1.12 christos _bfd_error_handler 14018 1.12 christos /* xgettext:c-format */ 14019 1.12 christos (_("%pB(%pA): error: secondary reloc %zu" 14020 1.12 christos " references a missing symbol"), 14021 1.12 christos abfd, relsec, idx); 14022 1.12 christos bfd_set_error (bfd_error_bad_value); 14023 1.12 christos result = false; 14024 1.12 christos n = 0; 14025 1.12 christos } 14026 1.12 christos 14027 1.12 christos last_sym = sym; 14028 1.12 christos last_sym_idx = n; 14029 1.12 christos } 14030 1.14 christos 14031 1.14 christos if (sym->the_bfd != NULL 14032 1.12 christos && sym->the_bfd->xvec != abfd->xvec 14033 1.12 christos && ! _bfd_elf_validate_reloc (abfd, ptr)) 14034 1.14 christos { 14035 1.12 christos _bfd_error_handler 14036 1.12 christos /* xgettext:c-format */ 14037 1.12 christos (_("%pB(%pA): error: secondary reloc %zu" 14038 1.12 christos " references a deleted symbol"), 14039 1.12 christos abfd, relsec, idx); 14040 1.12 christos bfd_set_error (bfd_error_bad_value); 14041 1.12 christos result = false; 14042 1.12 christos n = 0; 14043 1.12 christos } 14044 1.14 christos } 14045 1.14 christos 14046 1.12 christos src_rela.r_offset = ptr->address + addr_offset; 14047 1.12 christos if (ptr->howto == NULL) 14048 1.14 christos { 14049 1.12 christos _bfd_error_handler 14050 1.12 christos /* xgettext:c-format */ 14051 1.12 christos (_("%pB(%pA): error: secondary reloc %zu" 14052 1.12 christos " is of an unknown type"), 14053 1.12 christos abfd, relsec, idx); 14054 1.14 christos bfd_set_error (bfd_error_bad_value); 14055 1.14 christos result = false; 14056 1.14 christos src_rela.r_info = r_info (0, 0); 14057 1.14 christos } 14058 1.14 christos else 14059 1.12 christos src_rela.r_info = r_info (n, ptr->howto->type); 14060 1.12 christos src_rela.r_addend = ptr->addend; 14061 1.12 christos 14062 1.12 christos if (entsize == ebd->s->sizeof_rel) 14063 1.12 christos ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela); 14064 1.12 christos else /* entsize == ebd->s->sizeof_rela */ 14065 1.17 christos ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela); 14066 1.19 christos } 14067 1.17 christos } 14068 1.17 christos } 14069 1.19 christos 14070 1.17 christos return result; 14071 1.17 christos } 14072 1.17 christos 14073 1.17 christos /* Mmap in section contents. */ 14074 1.17 christos 14075 1.17 christos static bool 14076 1.17 christos elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf) 14077 1.17 christos { 14078 1.17 christos #ifdef USE_MMAP 14079 1.17 christos const struct elf_backend_data *bed = get_elf_backend_data (abfd); 14080 1.17 christos if (bed->use_mmap 14081 1.17 christos && sec->compress_status == COMPRESS_SECTION_NONE 14082 1.17 christos && (sec->flags & SEC_LINKER_CREATED) == 0) 14083 1.17 christos { 14084 1.17 christos /* Use mmap only if section size >= the minimum mmap section 14085 1.17 christos size. */ 14086 1.17 christos size_t readsz = bfd_get_section_limit_octets (abfd, sec); 14087 1.17 christos size_t allocsz = bfd_get_section_alloc_size (abfd, sec); 14088 1.17 christos if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size) 14089 1.17 christos { 14090 1.17 christos if (sec->contents != NULL) 14091 1.17 christos { 14092 1.17 christos if (!sec->mmapped_p) 14093 1.17 christos abort (); 14094 1.19 christos *buf = sec->contents; 14095 1.17 christos return true; 14096 1.17 christos } 14097 1.17 christos if (sec->mmapped_p) 14098 1.17 christos abort (); 14099 1.19 christos sec->mmapped_p = 1; 14100 1.17 christos 14101 1.17 christos /* We can't use the final link preallocated buffer for mmap. */ 14102 1.17 christos *buf = NULL; 14103 1.17 christos } 14104 1.17 christos } 14105 1.17 christos #endif 14106 1.17 christos /* FIXME: We should not get here if sec->alloced is set. */ 14107 1.17 christos bool ret = bfd_get_full_section_contents (abfd, sec, buf); 14108 1.17 christos if (ret && sec->mmapped_p) 14109 1.17 christos *buf = sec->contents; 14110 1.17 christos return ret; 14111 1.19 christos } 14112 1.19 christos 14113 1.17 christos /* Mmap in section contents. */ 14114 1.17 christos 14115 1.17 christos bool 14116 1.17 christos _bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf) 14117 1.17 christos { 14118 1.17 christos *buf = NULL; 14119 1.17 christos return elf_mmap_section_contents (abfd, sec, buf); 14120 1.17 christos } 14121 1.19 christos 14122 1.17 christos /* Mmap in the full section contents for the final link. */ 14123 1.17 christos 14124 1.17 christos bool 14125 1.17 christos _bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec, 14126 1.17 christos bfd_byte **buf) 14127 1.19 christos { 14128 1.17 christos return elf_mmap_section_contents (abfd, sec, buf); 14129 1.17 christos } 14130 1.17 christos 14131 1.17 christos /* Munmap section contents. */ 14132 1.17 christos 14133 1.17 christos void 14134 1.19 christos _bfd_elf_munmap_section_contents (asection *sec, void *contents) 14135 1.19 christos { 14136 1.19 christos /* NB: Since _bfd_elf_munmap_section_contents is called like free, 14137 1.19 christos CONTENTS may be NULL. */ 14138 1.19 christos if (contents == NULL) 14139 1.19 christos return; 14140 1.19 christos 14141 1.19 christos if (sec->alloced 14142 1.19 christos /* What a tangled web we weave with section contents. 14143 1.19 christos FIXME: We shouldn't need to test anything but sec->alloced 14144 1.19 christos here, but there are cases where a buffer is allocated for a 14145 1.19 christos section but then another buffer is malloc'd anyway. eg. 14146 1.19 christos trace through ld-elf/eh4 testcase on x86_64. */ 14147 1.19 christos && (sec->contents == contents 14148 1.19 christos || elf_section_data (sec)->this_hdr.contents == contents)) 14149 1.19 christos return; 14150 1.17 christos 14151 1.17 christos /* Don't leave pointers to data we are about to munmap or free. */ 14152 1.17 christos if (sec->contents == contents) 14153 1.17 christos sec->contents = NULL; 14154 1.17 christos if (elf_section_data (sec)->this_hdr.contents == contents) 14155 1.17 christos elf_section_data (sec)->this_hdr.contents = NULL; 14156 1.17 christos 14157 1.17 christos #ifdef USE_MMAP 14158 1.17 christos if (sec->mmapped_p) 14159 1.17 christos { 14160 1.17 christos /* When _bfd_elf_mmap_section_contents returns CONTENTS as 14161 1.17 christos malloced, CONTENTS_ADDR is set to NULL. */ 14162 1.17 christos if (elf_section_data (sec)->contents_addr != NULL) 14163 1.17 christos { 14164 1.17 christos /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */ 14165 1.17 christos if (munmap (elf_section_data (sec)->contents_addr, 14166 1.17 christos elf_section_data (sec)->contents_size) != 0) 14167 1.17 christos abort (); 14168 1.17 christos sec->mmapped_p = 0; 14169 1.17 christos elf_section_data (sec)->contents_addr = NULL; 14170 1.17 christos elf_section_data (sec)->contents_size = 0; 14171 1.17 christos return; 14172 1.17 christos } 14173 1.17 christos } 14174 1.17 christos #endif 14175 1.17 christos 14176 1.17 christos free (contents); 14177 1.17 christos } 14178 1.17 christos 14179 1.17 christos /* Munmap the full section contents for the final link. */ 14180 1.17 christos 14181 1.17 christos void 14182 1.17 christos _bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED) 14183 1.17 christos { 14184 1.17 christos #ifdef USE_MMAP 14185 1.17 christos if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL) 14186 1.17 christos { 14187 1.17 christos /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as 14188 1.19 christos malloced, CONTENTS_ADDR is set to NULL. */ 14189 1.17 christos /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */ 14190 1.17 christos if (munmap (elf_section_data (sec)->contents_addr, 14191 1.17 christos elf_section_data (sec)->contents_size) != 0) 14192 1.17 christos abort (); 14193 1.17 christos sec->mmapped_p = 0; 14194 sec->contents = NULL; 14195 elf_section_data (sec)->this_hdr.contents = NULL; 14196 elf_section_data (sec)->contents_addr = NULL; 14197 elf_section_data (sec)->contents_size = 0; 14198 } 14199 #endif 14200 } 14201