1 /* Intel 80386/80486-specific support for 32-bit ELF 2 Copyright (C) 1993-2014 Free Software Foundation, Inc. 3 4 This file is part of BFD, the Binary File Descriptor library. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 19 MA 02110-1301, USA. */ 20 21 #include "sysdep.h" 22 #include "bfd.h" 23 #include "bfdlink.h" 24 #include "libbfd.h" 25 #include "elf-bfd.h" 26 #include "elf-nacl.h" 27 #include "elf-vxworks.h" 28 #include "bfd_stdint.h" 29 #include "objalloc.h" 30 #include "hashtab.h" 31 #include "dwarf2.h" 32 33 /* 386 uses REL relocations instead of RELA. */ 34 #define USE_REL 1 35 36 #include "elf/i386.h" 37 38 static reloc_howto_type elf_howto_table[]= 39 { 40 HOWTO(R_386_NONE, 0, 0, 0, FALSE, 0, complain_overflow_bitfield, 41 bfd_elf_generic_reloc, "R_386_NONE", 42 TRUE, 0x00000000, 0x00000000, FALSE), 43 HOWTO(R_386_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 44 bfd_elf_generic_reloc, "R_386_32", 45 TRUE, 0xffffffff, 0xffffffff, FALSE), 46 HOWTO(R_386_PC32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield, 47 bfd_elf_generic_reloc, "R_386_PC32", 48 TRUE, 0xffffffff, 0xffffffff, TRUE), 49 HOWTO(R_386_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 50 bfd_elf_generic_reloc, "R_386_GOT32", 51 TRUE, 0xffffffff, 0xffffffff, FALSE), 52 HOWTO(R_386_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_bitfield, 53 bfd_elf_generic_reloc, "R_386_PLT32", 54 TRUE, 0xffffffff, 0xffffffff, TRUE), 55 HOWTO(R_386_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 56 bfd_elf_generic_reloc, "R_386_COPY", 57 TRUE, 0xffffffff, 0xffffffff, FALSE), 58 HOWTO(R_386_GLOB_DAT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 59 bfd_elf_generic_reloc, "R_386_GLOB_DAT", 60 TRUE, 0xffffffff, 0xffffffff, FALSE), 61 HOWTO(R_386_JUMP_SLOT, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 62 bfd_elf_generic_reloc, "R_386_JUMP_SLOT", 63 TRUE, 0xffffffff, 0xffffffff, FALSE), 64 HOWTO(R_386_RELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 65 bfd_elf_generic_reloc, "R_386_RELATIVE", 66 TRUE, 0xffffffff, 0xffffffff, FALSE), 67 HOWTO(R_386_GOTOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 68 bfd_elf_generic_reloc, "R_386_GOTOFF", 69 TRUE, 0xffffffff, 0xffffffff, FALSE), 70 HOWTO(R_386_GOTPC, 0, 2, 32, TRUE, 0, complain_overflow_bitfield, 71 bfd_elf_generic_reloc, "R_386_GOTPC", 72 TRUE, 0xffffffff, 0xffffffff, TRUE), 73 74 /* We have a gap in the reloc numbers here. 75 R_386_standard counts the number up to this point, and 76 R_386_ext_offset is the value to subtract from a reloc type of 77 R_386_16 thru R_386_PC8 to form an index into this table. */ 78 #define R_386_standard (R_386_GOTPC + 1) 79 #define R_386_ext_offset (R_386_TLS_TPOFF - R_386_standard) 80 81 /* These relocs are a GNU extension. */ 82 HOWTO(R_386_TLS_TPOFF, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 83 bfd_elf_generic_reloc, "R_386_TLS_TPOFF", 84 TRUE, 0xffffffff, 0xffffffff, FALSE), 85 HOWTO(R_386_TLS_IE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 86 bfd_elf_generic_reloc, "R_386_TLS_IE", 87 TRUE, 0xffffffff, 0xffffffff, FALSE), 88 HOWTO(R_386_TLS_GOTIE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 89 bfd_elf_generic_reloc, "R_386_TLS_GOTIE", 90 TRUE, 0xffffffff, 0xffffffff, FALSE), 91 HOWTO(R_386_TLS_LE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 92 bfd_elf_generic_reloc, "R_386_TLS_LE", 93 TRUE, 0xffffffff, 0xffffffff, FALSE), 94 HOWTO(R_386_TLS_GD, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 95 bfd_elf_generic_reloc, "R_386_TLS_GD", 96 TRUE, 0xffffffff, 0xffffffff, FALSE), 97 HOWTO(R_386_TLS_LDM, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 98 bfd_elf_generic_reloc, "R_386_TLS_LDM", 99 TRUE, 0xffffffff, 0xffffffff, FALSE), 100 HOWTO(R_386_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, 101 bfd_elf_generic_reloc, "R_386_16", 102 TRUE, 0xffff, 0xffff, FALSE), 103 HOWTO(R_386_PC16, 0, 1, 16, TRUE, 0, complain_overflow_bitfield, 104 bfd_elf_generic_reloc, "R_386_PC16", 105 TRUE, 0xffff, 0xffff, TRUE), 106 HOWTO(R_386_8, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, 107 bfd_elf_generic_reloc, "R_386_8", 108 TRUE, 0xff, 0xff, FALSE), 109 HOWTO(R_386_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed, 110 bfd_elf_generic_reloc, "R_386_PC8", 111 TRUE, 0xff, 0xff, TRUE), 112 113 #define R_386_ext (R_386_PC8 + 1 - R_386_ext_offset) 114 #define R_386_tls_offset (R_386_TLS_LDO_32 - R_386_ext) 115 /* These are common with Solaris TLS implementation. */ 116 HOWTO(R_386_TLS_LDO_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 117 bfd_elf_generic_reloc, "R_386_TLS_LDO_32", 118 TRUE, 0xffffffff, 0xffffffff, FALSE), 119 HOWTO(R_386_TLS_IE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 120 bfd_elf_generic_reloc, "R_386_TLS_IE_32", 121 TRUE, 0xffffffff, 0xffffffff, FALSE), 122 HOWTO(R_386_TLS_LE_32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 123 bfd_elf_generic_reloc, "R_386_TLS_LE_32", 124 TRUE, 0xffffffff, 0xffffffff, FALSE), 125 HOWTO(R_386_TLS_DTPMOD32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 126 bfd_elf_generic_reloc, "R_386_TLS_DTPMOD32", 127 TRUE, 0xffffffff, 0xffffffff, FALSE), 128 HOWTO(R_386_TLS_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 129 bfd_elf_generic_reloc, "R_386_TLS_DTPOFF32", 130 TRUE, 0xffffffff, 0xffffffff, FALSE), 131 HOWTO(R_386_TLS_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 132 bfd_elf_generic_reloc, "R_386_TLS_TPOFF32", 133 TRUE, 0xffffffff, 0xffffffff, FALSE), 134 HOWTO(R_386_SIZE32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned, 135 bfd_elf_generic_reloc, "R_386_SIZE32", 136 TRUE, 0xffffffff, 0xffffffff, FALSE), 137 HOWTO(R_386_TLS_GOTDESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 138 bfd_elf_generic_reloc, "R_386_TLS_GOTDESC", 139 TRUE, 0xffffffff, 0xffffffff, FALSE), 140 HOWTO(R_386_TLS_DESC_CALL, 0, 0, 0, FALSE, 0, complain_overflow_dont, 141 bfd_elf_generic_reloc, "R_386_TLS_DESC_CALL", 142 FALSE, 0, 0, FALSE), 143 HOWTO(R_386_TLS_DESC, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 144 bfd_elf_generic_reloc, "R_386_TLS_DESC", 145 TRUE, 0xffffffff, 0xffffffff, FALSE), 146 HOWTO(R_386_IRELATIVE, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, 147 bfd_elf_generic_reloc, "R_386_IRELATIVE", 148 TRUE, 0xffffffff, 0xffffffff, FALSE), 149 150 /* Another gap. */ 151 #define R_386_irelative (R_386_IRELATIVE + 1 - R_386_tls_offset) 152 #define R_386_vt_offset (R_386_GNU_VTINHERIT - R_386_irelative) 153 154 /* GNU extension to record C++ vtable hierarchy. */ 155 HOWTO (R_386_GNU_VTINHERIT, /* type */ 156 0, /* rightshift */ 157 2, /* size (0 = byte, 1 = short, 2 = long) */ 158 0, /* bitsize */ 159 FALSE, /* pc_relative */ 160 0, /* bitpos */ 161 complain_overflow_dont, /* complain_on_overflow */ 162 NULL, /* special_function */ 163 "R_386_GNU_VTINHERIT", /* name */ 164 FALSE, /* partial_inplace */ 165 0, /* src_mask */ 166 0, /* dst_mask */ 167 FALSE), /* pcrel_offset */ 168 169 /* GNU extension to record C++ vtable member usage. */ 170 HOWTO (R_386_GNU_VTENTRY, /* type */ 171 0, /* rightshift */ 172 2, /* size (0 = byte, 1 = short, 2 = long) */ 173 0, /* bitsize */ 174 FALSE, /* pc_relative */ 175 0, /* bitpos */ 176 complain_overflow_dont, /* complain_on_overflow */ 177 _bfd_elf_rel_vtable_reloc_fn, /* special_function */ 178 "R_386_GNU_VTENTRY", /* name */ 179 FALSE, /* partial_inplace */ 180 0, /* src_mask */ 181 0, /* dst_mask */ 182 FALSE) /* pcrel_offset */ 183 184 #define R_386_vt (R_386_GNU_VTENTRY + 1 - R_386_vt_offset) 185 186 }; 187 188 #ifdef DEBUG_GEN_RELOC 189 #define TRACE(str) \ 190 fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str) 191 #else 192 #define TRACE(str) 193 #endif 194 195 static reloc_howto_type * 196 elf_i386_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, 197 bfd_reloc_code_real_type code) 198 { 199 switch (code) 200 { 201 case BFD_RELOC_NONE: 202 TRACE ("BFD_RELOC_NONE"); 203 return &elf_howto_table[R_386_NONE]; 204 205 case BFD_RELOC_32: 206 TRACE ("BFD_RELOC_32"); 207 return &elf_howto_table[R_386_32]; 208 209 case BFD_RELOC_CTOR: 210 TRACE ("BFD_RELOC_CTOR"); 211 return &elf_howto_table[R_386_32]; 212 213 case BFD_RELOC_32_PCREL: 214 TRACE ("BFD_RELOC_PC32"); 215 return &elf_howto_table[R_386_PC32]; 216 217 case BFD_RELOC_386_GOT32: 218 TRACE ("BFD_RELOC_386_GOT32"); 219 return &elf_howto_table[R_386_GOT32]; 220 221 case BFD_RELOC_386_PLT32: 222 TRACE ("BFD_RELOC_386_PLT32"); 223 return &elf_howto_table[R_386_PLT32]; 224 225 case BFD_RELOC_386_COPY: 226 TRACE ("BFD_RELOC_386_COPY"); 227 return &elf_howto_table[R_386_COPY]; 228 229 case BFD_RELOC_386_GLOB_DAT: 230 TRACE ("BFD_RELOC_386_GLOB_DAT"); 231 return &elf_howto_table[R_386_GLOB_DAT]; 232 233 case BFD_RELOC_386_JUMP_SLOT: 234 TRACE ("BFD_RELOC_386_JUMP_SLOT"); 235 return &elf_howto_table[R_386_JUMP_SLOT]; 236 237 case BFD_RELOC_386_RELATIVE: 238 TRACE ("BFD_RELOC_386_RELATIVE"); 239 return &elf_howto_table[R_386_RELATIVE]; 240 241 case BFD_RELOC_386_GOTOFF: 242 TRACE ("BFD_RELOC_386_GOTOFF"); 243 return &elf_howto_table[R_386_GOTOFF]; 244 245 case BFD_RELOC_386_GOTPC: 246 TRACE ("BFD_RELOC_386_GOTPC"); 247 return &elf_howto_table[R_386_GOTPC]; 248 249 /* These relocs are a GNU extension. */ 250 case BFD_RELOC_386_TLS_TPOFF: 251 TRACE ("BFD_RELOC_386_TLS_TPOFF"); 252 return &elf_howto_table[R_386_TLS_TPOFF - R_386_ext_offset]; 253 254 case BFD_RELOC_386_TLS_IE: 255 TRACE ("BFD_RELOC_386_TLS_IE"); 256 return &elf_howto_table[R_386_TLS_IE - R_386_ext_offset]; 257 258 case BFD_RELOC_386_TLS_GOTIE: 259 TRACE ("BFD_RELOC_386_TLS_GOTIE"); 260 return &elf_howto_table[R_386_TLS_GOTIE - R_386_ext_offset]; 261 262 case BFD_RELOC_386_TLS_LE: 263 TRACE ("BFD_RELOC_386_TLS_LE"); 264 return &elf_howto_table[R_386_TLS_LE - R_386_ext_offset]; 265 266 case BFD_RELOC_386_TLS_GD: 267 TRACE ("BFD_RELOC_386_TLS_GD"); 268 return &elf_howto_table[R_386_TLS_GD - R_386_ext_offset]; 269 270 case BFD_RELOC_386_TLS_LDM: 271 TRACE ("BFD_RELOC_386_TLS_LDM"); 272 return &elf_howto_table[R_386_TLS_LDM - R_386_ext_offset]; 273 274 case BFD_RELOC_16: 275 TRACE ("BFD_RELOC_16"); 276 return &elf_howto_table[R_386_16 - R_386_ext_offset]; 277 278 case BFD_RELOC_16_PCREL: 279 TRACE ("BFD_RELOC_16_PCREL"); 280 return &elf_howto_table[R_386_PC16 - R_386_ext_offset]; 281 282 case BFD_RELOC_8: 283 TRACE ("BFD_RELOC_8"); 284 return &elf_howto_table[R_386_8 - R_386_ext_offset]; 285 286 case BFD_RELOC_8_PCREL: 287 TRACE ("BFD_RELOC_8_PCREL"); 288 return &elf_howto_table[R_386_PC8 - R_386_ext_offset]; 289 290 /* Common with Sun TLS implementation. */ 291 case BFD_RELOC_386_TLS_LDO_32: 292 TRACE ("BFD_RELOC_386_TLS_LDO_32"); 293 return &elf_howto_table[R_386_TLS_LDO_32 - R_386_tls_offset]; 294 295 case BFD_RELOC_386_TLS_IE_32: 296 TRACE ("BFD_RELOC_386_TLS_IE_32"); 297 return &elf_howto_table[R_386_TLS_IE_32 - R_386_tls_offset]; 298 299 case BFD_RELOC_386_TLS_LE_32: 300 TRACE ("BFD_RELOC_386_TLS_LE_32"); 301 return &elf_howto_table[R_386_TLS_LE_32 - R_386_tls_offset]; 302 303 case BFD_RELOC_386_TLS_DTPMOD32: 304 TRACE ("BFD_RELOC_386_TLS_DTPMOD32"); 305 return &elf_howto_table[R_386_TLS_DTPMOD32 - R_386_tls_offset]; 306 307 case BFD_RELOC_386_TLS_DTPOFF32: 308 TRACE ("BFD_RELOC_386_TLS_DTPOFF32"); 309 return &elf_howto_table[R_386_TLS_DTPOFF32 - R_386_tls_offset]; 310 311 case BFD_RELOC_386_TLS_TPOFF32: 312 TRACE ("BFD_RELOC_386_TLS_TPOFF32"); 313 return &elf_howto_table[R_386_TLS_TPOFF32 - R_386_tls_offset]; 314 315 case BFD_RELOC_SIZE32: 316 TRACE ("BFD_RELOC_SIZE32"); 317 return &elf_howto_table[R_386_SIZE32 - R_386_tls_offset]; 318 319 case BFD_RELOC_386_TLS_GOTDESC: 320 TRACE ("BFD_RELOC_386_TLS_GOTDESC"); 321 return &elf_howto_table[R_386_TLS_GOTDESC - R_386_tls_offset]; 322 323 case BFD_RELOC_386_TLS_DESC_CALL: 324 TRACE ("BFD_RELOC_386_TLS_DESC_CALL"); 325 return &elf_howto_table[R_386_TLS_DESC_CALL - R_386_tls_offset]; 326 327 case BFD_RELOC_386_TLS_DESC: 328 TRACE ("BFD_RELOC_386_TLS_DESC"); 329 return &elf_howto_table[R_386_TLS_DESC - R_386_tls_offset]; 330 331 case BFD_RELOC_386_IRELATIVE: 332 TRACE ("BFD_RELOC_386_IRELATIVE"); 333 return &elf_howto_table[R_386_IRELATIVE - R_386_tls_offset]; 334 335 case BFD_RELOC_VTABLE_INHERIT: 336 TRACE ("BFD_RELOC_VTABLE_INHERIT"); 337 return &elf_howto_table[R_386_GNU_VTINHERIT - R_386_vt_offset]; 338 339 case BFD_RELOC_VTABLE_ENTRY: 340 TRACE ("BFD_RELOC_VTABLE_ENTRY"); 341 return &elf_howto_table[R_386_GNU_VTENTRY - R_386_vt_offset]; 342 343 default: 344 break; 345 } 346 347 TRACE ("Unknown"); 348 return 0; 349 } 350 351 static reloc_howto_type * 352 elf_i386_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, 353 const char *r_name) 354 { 355 unsigned int i; 356 357 for (i = 0; i < sizeof (elf_howto_table) / sizeof (elf_howto_table[0]); i++) 358 if (elf_howto_table[i].name != NULL 359 && strcasecmp (elf_howto_table[i].name, r_name) == 0) 360 return &elf_howto_table[i]; 361 362 return NULL; 363 } 364 365 static reloc_howto_type * 366 elf_i386_rtype_to_howto (bfd *abfd, unsigned r_type) 367 { 368 unsigned int indx; 369 370 if ((indx = r_type) >= R_386_standard 371 && ((indx = r_type - R_386_ext_offset) - R_386_standard 372 >= R_386_ext - R_386_standard) 373 && ((indx = r_type - R_386_tls_offset) - R_386_ext 374 >= R_386_irelative - R_386_ext) 375 && ((indx = r_type - R_386_vt_offset) - R_386_irelative 376 >= R_386_vt - R_386_irelative)) 377 { 378 (*_bfd_error_handler) (_("%B: invalid relocation type %d"), 379 abfd, (int) r_type); 380 indx = R_386_NONE; 381 } 382 BFD_ASSERT (elf_howto_table [indx].type == r_type); 383 return &elf_howto_table[indx]; 384 } 385 386 static void 387 elf_i386_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED, 388 arelent *cache_ptr, 389 Elf_Internal_Rela *dst) 390 { 391 unsigned int r_type = ELF32_R_TYPE (dst->r_info); 392 cache_ptr->howto = elf_i386_rtype_to_howto (abfd, r_type); 393 } 394 395 /* Return whether a symbol name implies a local label. The UnixWare 396 2.1 cc generates temporary symbols that start with .X, so we 397 recognize them here. FIXME: do other SVR4 compilers also use .X?. 398 If so, we should move the .X recognition into 399 _bfd_elf_is_local_label_name. */ 400 401 static bfd_boolean 402 elf_i386_is_local_label_name (bfd *abfd, const char *name) 403 { 404 if (name[0] == '.' && name[1] == 'X') 405 return TRUE; 406 407 return _bfd_elf_is_local_label_name (abfd, name); 408 } 409 410 /* Support for core dump NOTE sections. */ 412 413 static bfd_boolean 414 elf_i386_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) 415 { 416 int offset; 417 size_t size; 418 419 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0) 420 { 421 int pr_version = bfd_get_32 (abfd, note->descdata); 422 423 if (pr_version != 1) 424 return FALSE; 425 426 /* pr_cursig */ 427 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 20); 428 429 /* pr_pid */ 430 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24); 431 432 /* pr_reg */ 433 offset = 28; 434 size = bfd_get_32 (abfd, note->descdata + 8); 435 } 436 else 437 { 438 switch (note->descsz) 439 { 440 default: 441 return FALSE; 442 443 case 144: /* Linux/i386 */ 444 /* pr_cursig */ 445 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12); 446 447 /* pr_pid */ 448 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24); 449 450 /* pr_reg */ 451 offset = 72; 452 size = 68; 453 454 break; 455 } 456 } 457 458 /* Make a ".reg/999" section. */ 459 return _bfd_elfcore_make_pseudosection (abfd, ".reg", 460 size, note->descpos + offset); 461 } 462 463 static bfd_boolean 464 elf_i386_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) 465 { 466 if (note->namesz == 8 && strcmp (note->namedata, "FreeBSD") == 0) 467 { 468 int pr_version = bfd_get_32 (abfd, note->descdata); 469 470 if (pr_version != 1) 471 return FALSE; 472 473 elf_tdata (abfd)->core->program 474 = _bfd_elfcore_strndup (abfd, note->descdata + 8, 17); 475 elf_tdata (abfd)->core->command 476 = _bfd_elfcore_strndup (abfd, note->descdata + 25, 81); 477 } 478 else 479 { 480 switch (note->descsz) 481 { 482 default: 483 return FALSE; 484 485 case 124: /* Linux/i386 elf_prpsinfo. */ 486 elf_tdata (abfd)->core->pid 487 = bfd_get_32 (abfd, note->descdata + 12); 488 elf_tdata (abfd)->core->program 489 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16); 490 elf_tdata (abfd)->core->command 491 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80); 492 } 493 } 494 495 /* Note that for some reason, a spurious space is tacked 496 onto the end of the args in some (at least one anyway) 497 implementations, so strip it off if it exists. */ 498 { 499 char *command = elf_tdata (abfd)->core->command; 500 int n = strlen (command); 501 502 if (0 < n && command[n - 1] == ' ') 503 command[n - 1] = '\0'; 504 } 505 506 return TRUE; 507 } 508 509 /* Functions for the i386 ELF linker. 511 512 In order to gain some understanding of code in this file without 513 knowing all the intricate details of the linker, note the 514 following: 515 516 Functions named elf_i386_* are called by external routines, other 517 functions are only called locally. elf_i386_* functions appear 518 in this file more or less in the order in which they are called 519 from external routines. eg. elf_i386_check_relocs is called 520 early in the link process, elf_i386_finish_dynamic_sections is 521 one of the last functions. */ 522 523 524 /* The name of the dynamic interpreter. This is put in the .interp 525 section. */ 526 527 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1" 528 529 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid 530 copying dynamic variables from a shared lib into an app's dynbss 531 section, and instead use a dynamic relocation to point into the 532 shared lib. */ 533 #define ELIMINATE_COPY_RELOCS 1 534 535 /* The size in bytes of an entry in the procedure linkage table. */ 536 537 #define PLT_ENTRY_SIZE 16 538 539 /* The first entry in an absolute procedure linkage table looks like 540 this. See the SVR4 ABI i386 supplement to see how this works. 541 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */ 542 543 static const bfd_byte elf_i386_plt0_entry[12] = 544 { 545 0xff, 0x35, /* pushl contents of address */ 546 0, 0, 0, 0, /* replaced with address of .got + 4. */ 547 0xff, 0x25, /* jmp indirect */ 548 0, 0, 0, 0 /* replaced with address of .got + 8. */ 549 }; 550 551 /* Subsequent entries in an absolute procedure linkage table look like 552 this. */ 553 554 static const bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] = 555 { 556 0xff, 0x25, /* jmp indirect */ 557 0, 0, 0, 0, /* replaced with address of this symbol in .got. */ 558 0x68, /* pushl immediate */ 559 0, 0, 0, 0, /* replaced with offset into relocation table. */ 560 0xe9, /* jmp relative */ 561 0, 0, 0, 0 /* replaced with offset to start of .plt. */ 562 }; 563 564 /* The first entry in a PIC procedure linkage table look like this. 565 Will be padded to PLT_ENTRY_SIZE with htab->plt0_pad_byte. */ 566 567 static const bfd_byte elf_i386_pic_plt0_entry[12] = 568 { 569 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */ 570 0xff, 0xa3, 8, 0, 0, 0 /* jmp *8(%ebx) */ 571 }; 572 573 /* Subsequent entries in a PIC procedure linkage table look like this. */ 574 575 static const bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] = 576 { 577 0xff, 0xa3, /* jmp *offset(%ebx) */ 578 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */ 579 0x68, /* pushl immediate */ 580 0, 0, 0, 0, /* replaced with offset into relocation table. */ 581 0xe9, /* jmp relative */ 582 0, 0, 0, 0 /* replaced with offset to start of .plt. */ 583 }; 584 585 /* .eh_frame covering the .plt section. */ 586 587 static const bfd_byte elf_i386_eh_frame_plt[] = 588 { 589 #define PLT_CIE_LENGTH 20 590 #define PLT_FDE_LENGTH 36 591 #define PLT_FDE_START_OFFSET 4 + PLT_CIE_LENGTH + 8 592 #define PLT_FDE_LEN_OFFSET 4 + PLT_CIE_LENGTH + 12 593 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */ 594 0, 0, 0, 0, /* CIE ID */ 595 1, /* CIE version */ 596 'z', 'R', 0, /* Augmentation string */ 597 1, /* Code alignment factor */ 598 0x7c, /* Data alignment factor */ 599 8, /* Return address column */ 600 1, /* Augmentation size */ 601 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */ 602 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */ 603 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */ 604 DW_CFA_nop, DW_CFA_nop, 605 606 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */ 607 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */ 608 0, 0, 0, 0, /* R_386_PC32 .plt goes here */ 609 0, 0, 0, 0, /* .plt size goes here */ 610 0, /* Augmentation size */ 611 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */ 612 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */ 613 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */ 614 DW_CFA_advance_loc + 10, /* DW_CFA_advance_loc: 10 to __PLT__+16 */ 615 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */ 616 11, /* Block length */ 617 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */ 618 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */ 619 DW_OP_lit15, DW_OP_and, DW_OP_lit11, DW_OP_ge, 620 DW_OP_lit2, DW_OP_shl, DW_OP_plus, 621 DW_CFA_nop, DW_CFA_nop, DW_CFA_nop, DW_CFA_nop 622 }; 623 624 struct elf_i386_plt_layout 625 { 626 /* The first entry in an absolute procedure linkage table looks like this. */ 627 const bfd_byte *plt0_entry; 628 unsigned int plt0_entry_size; 629 630 /* Offsets into plt0_entry that are to be replaced with GOT[1] and GOT[2]. */ 631 unsigned int plt0_got1_offset; 632 unsigned int plt0_got2_offset; 633 634 /* Later entries in an absolute procedure linkage table look like this. */ 635 const bfd_byte *plt_entry; 636 unsigned int plt_entry_size; 637 638 /* Offsets into plt_entry that are to be replaced with... */ 639 unsigned int plt_got_offset; /* ... address of this symbol in .got. */ 640 unsigned int plt_reloc_offset; /* ... offset into relocation table. */ 641 unsigned int plt_plt_offset; /* ... offset to start of .plt. */ 642 643 /* Offset into plt_entry where the initial value of the GOT entry points. */ 644 unsigned int plt_lazy_offset; 645 646 /* The first entry in a PIC procedure linkage table looks like this. */ 647 const bfd_byte *pic_plt0_entry; 648 649 /* Subsequent entries in a PIC procedure linkage table look like this. */ 650 const bfd_byte *pic_plt_entry; 651 652 /* .eh_frame covering the .plt section. */ 653 const bfd_byte *eh_frame_plt; 654 unsigned int eh_frame_plt_size; 655 }; 656 657 #define GET_PLT_ENTRY_SIZE(abfd) \ 658 get_elf_i386_backend_data (abfd)->plt->plt_entry_size 659 660 /* These are the standard parameters. */ 661 static const struct elf_i386_plt_layout elf_i386_plt = 662 { 663 elf_i386_plt0_entry, /* plt0_entry */ 664 sizeof (elf_i386_plt0_entry), /* plt0_entry_size */ 665 2, /* plt0_got1_offset */ 666 8, /* plt0_got2_offset */ 667 elf_i386_plt_entry, /* plt_entry */ 668 PLT_ENTRY_SIZE, /* plt_entry_size */ 669 2, /* plt_got_offset */ 670 7, /* plt_reloc_offset */ 671 12, /* plt_plt_offset */ 672 6, /* plt_lazy_offset */ 673 elf_i386_pic_plt0_entry, /* pic_plt0_entry */ 674 elf_i386_pic_plt_entry, /* pic_plt_entry */ 675 elf_i386_eh_frame_plt, /* eh_frame_plt */ 676 sizeof (elf_i386_eh_frame_plt), /* eh_frame_plt_size */ 677 }; 678 679 681 /* On VxWorks, the .rel.plt.unloaded section has absolute relocations 682 for the PLTResolve stub and then for each PLT entry. */ 683 #define PLTRESOLVE_RELOCS_SHLIB 0 684 #define PLTRESOLVE_RELOCS 2 685 #define PLT_NON_JUMP_SLOT_RELOCS 2 686 687 /* Architecture-specific backend data for i386. */ 688 689 struct elf_i386_backend_data 690 { 691 /* Parameters describing PLT generation. */ 692 const struct elf_i386_plt_layout *plt; 693 694 /* Value used to fill the unused bytes of the first PLT entry. */ 695 bfd_byte plt0_pad_byte; 696 697 /* True if the target system is VxWorks. */ 698 int is_vxworks; 699 }; 700 701 #define get_elf_i386_backend_data(abfd) \ 702 ((const struct elf_i386_backend_data *) \ 703 get_elf_backend_data (abfd)->arch_data) 704 705 /* These are the standard parameters. */ 706 static const struct elf_i386_backend_data elf_i386_arch_bed = 707 { 708 &elf_i386_plt, /* plt */ 709 0, /* plt0_pad_byte */ 710 0, /* is_vxworks */ 711 }; 712 713 #define elf_backend_arch_data &elf_i386_arch_bed 714 715 /* i386 ELF linker hash entry. */ 716 717 struct elf_i386_link_hash_entry 718 { 719 struct elf_link_hash_entry elf; 720 721 /* Track dynamic relocs copied for this symbol. */ 722 struct elf_dyn_relocs *dyn_relocs; 723 724 #define GOT_UNKNOWN 0 725 #define GOT_NORMAL 1 726 #define GOT_TLS_GD 2 727 #define GOT_TLS_IE 4 728 #define GOT_TLS_IE_POS 5 729 #define GOT_TLS_IE_NEG 6 730 #define GOT_TLS_IE_BOTH 7 731 #define GOT_TLS_GDESC 8 732 #define GOT_TLS_GD_BOTH_P(type) \ 733 ((type) == (GOT_TLS_GD | GOT_TLS_GDESC)) 734 #define GOT_TLS_GD_P(type) \ 735 ((type) == GOT_TLS_GD || GOT_TLS_GD_BOTH_P (type)) 736 #define GOT_TLS_GDESC_P(type) \ 737 ((type) == GOT_TLS_GDESC || GOT_TLS_GD_BOTH_P (type)) 738 #define GOT_TLS_GD_ANY_P(type) \ 739 (GOT_TLS_GD_P (type) || GOT_TLS_GDESC_P (type)) 740 unsigned char tls_type; 741 742 /* Offset of the GOTPLT entry reserved for the TLS descriptor, 743 starting at the end of the jump table. */ 744 bfd_vma tlsdesc_got; 745 }; 746 747 #define elf_i386_hash_entry(ent) ((struct elf_i386_link_hash_entry *)(ent)) 748 749 struct elf_i386_obj_tdata 750 { 751 struct elf_obj_tdata root; 752 753 /* tls_type for each local got entry. */ 754 char *local_got_tls_type; 755 756 /* GOTPLT entries for TLS descriptors. */ 757 bfd_vma *local_tlsdesc_gotent; 758 }; 759 760 #define elf_i386_tdata(abfd) \ 761 ((struct elf_i386_obj_tdata *) (abfd)->tdata.any) 762 763 #define elf_i386_local_got_tls_type(abfd) \ 764 (elf_i386_tdata (abfd)->local_got_tls_type) 765 766 #define elf_i386_local_tlsdesc_gotent(abfd) \ 767 (elf_i386_tdata (abfd)->local_tlsdesc_gotent) 768 769 #define is_i386_elf(bfd) \ 770 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \ 771 && elf_tdata (bfd) != NULL \ 772 && elf_object_id (bfd) == I386_ELF_DATA) 773 774 static bfd_boolean 775 elf_i386_mkobject (bfd *abfd) 776 { 777 return bfd_elf_allocate_object (abfd, sizeof (struct elf_i386_obj_tdata), 778 I386_ELF_DATA); 779 } 780 781 /* i386 ELF linker hash table. */ 782 783 struct elf_i386_link_hash_table 784 { 785 struct elf_link_hash_table elf; 786 787 /* Short-cuts to get to dynamic linker sections. */ 788 asection *sdynbss; 789 asection *srelbss; 790 asection *plt_eh_frame; 791 792 union 793 { 794 bfd_signed_vma refcount; 795 bfd_vma offset; 796 } tls_ldm_got; 797 798 /* The amount of space used by the reserved portion of the sgotplt 799 section, plus whatever space is used by the jump slots. */ 800 bfd_vma sgotplt_jump_table_size; 801 802 /* Small local sym cache. */ 803 struct sym_cache sym_cache; 804 805 /* _TLS_MODULE_BASE_ symbol. */ 806 struct bfd_link_hash_entry *tls_module_base; 807 808 /* Used by local STT_GNU_IFUNC symbols. */ 809 htab_t loc_hash_table; 810 void * loc_hash_memory; 811 812 /* The (unloaded but important) .rel.plt.unloaded section on VxWorks. */ 813 asection *srelplt2; 814 815 /* The index of the next unused R_386_TLS_DESC slot in .rel.plt. */ 816 bfd_vma next_tls_desc_index; 817 818 /* The index of the next unused R_386_JUMP_SLOT slot in .rel.plt. */ 819 bfd_vma next_jump_slot_index; 820 821 /* The index of the next unused R_386_IRELATIVE slot in .rel.plt. */ 822 bfd_vma next_irelative_index; 823 }; 824 825 /* Get the i386 ELF linker hash table from a link_info structure. */ 826 827 #define elf_i386_hash_table(p) \ 828 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \ 829 == I386_ELF_DATA ? ((struct elf_i386_link_hash_table *) ((p)->hash)) : NULL) 830 831 #define elf_i386_compute_jump_table_size(htab) \ 832 ((htab)->elf.srelplt->reloc_count * 4) 833 834 /* Create an entry in an i386 ELF linker hash table. */ 835 836 static struct bfd_hash_entry * 837 elf_i386_link_hash_newfunc (struct bfd_hash_entry *entry, 838 struct bfd_hash_table *table, 839 const char *string) 840 { 841 /* Allocate the structure if it has not already been allocated by a 842 subclass. */ 843 if (entry == NULL) 844 { 845 entry = (struct bfd_hash_entry *) 846 bfd_hash_allocate (table, sizeof (struct elf_i386_link_hash_entry)); 847 if (entry == NULL) 848 return entry; 849 } 850 851 /* Call the allocation method of the superclass. */ 852 entry = _bfd_elf_link_hash_newfunc (entry, table, string); 853 if (entry != NULL) 854 { 855 struct elf_i386_link_hash_entry *eh; 856 857 eh = (struct elf_i386_link_hash_entry *) entry; 858 eh->dyn_relocs = NULL; 859 eh->tls_type = GOT_UNKNOWN; 860 eh->tlsdesc_got = (bfd_vma) -1; 861 } 862 863 return entry; 864 } 865 866 /* Compute a hash of a local hash entry. We use elf_link_hash_entry 867 for local symbol so that we can handle local STT_GNU_IFUNC symbols 868 as global symbol. We reuse indx and dynstr_index for local symbol 869 hash since they aren't used by global symbols in this backend. */ 870 871 static hashval_t 872 elf_i386_local_htab_hash (const void *ptr) 873 { 874 struct elf_link_hash_entry *h 875 = (struct elf_link_hash_entry *) ptr; 876 return ELF_LOCAL_SYMBOL_HASH (h->indx, h->dynstr_index); 877 } 878 879 /* Compare local hash entries. */ 880 881 static int 882 elf_i386_local_htab_eq (const void *ptr1, const void *ptr2) 883 { 884 struct elf_link_hash_entry *h1 885 = (struct elf_link_hash_entry *) ptr1; 886 struct elf_link_hash_entry *h2 887 = (struct elf_link_hash_entry *) ptr2; 888 889 return h1->indx == h2->indx && h1->dynstr_index == h2->dynstr_index; 890 } 891 892 /* Find and/or create a hash entry for local symbol. */ 893 894 static struct elf_link_hash_entry * 895 elf_i386_get_local_sym_hash (struct elf_i386_link_hash_table *htab, 896 bfd *abfd, const Elf_Internal_Rela *rel, 897 bfd_boolean create) 898 { 899 struct elf_i386_link_hash_entry e, *ret; 900 asection *sec = abfd->sections; 901 hashval_t h = ELF_LOCAL_SYMBOL_HASH (sec->id, 902 ELF32_R_SYM (rel->r_info)); 903 void **slot; 904 905 e.elf.indx = sec->id; 906 e.elf.dynstr_index = ELF32_R_SYM (rel->r_info); 907 slot = htab_find_slot_with_hash (htab->loc_hash_table, &e, h, 908 create ? INSERT : NO_INSERT); 909 910 if (!slot) 911 return NULL; 912 913 if (*slot) 914 { 915 ret = (struct elf_i386_link_hash_entry *) *slot; 916 return &ret->elf; 917 } 918 919 ret = (struct elf_i386_link_hash_entry *) 920 objalloc_alloc ((struct objalloc *) htab->loc_hash_memory, 921 sizeof (struct elf_i386_link_hash_entry)); 922 if (ret) 923 { 924 memset (ret, 0, sizeof (*ret)); 925 ret->elf.indx = sec->id; 926 ret->elf.dynstr_index = ELF32_R_SYM (rel->r_info); 927 ret->elf.dynindx = -1; 928 *slot = ret; 929 } 930 return &ret->elf; 931 } 932 933 /* Destroy an i386 ELF linker hash table. */ 934 935 static void 936 elf_i386_link_hash_table_free (bfd *obfd) 937 { 938 struct elf_i386_link_hash_table *htab 939 = (struct elf_i386_link_hash_table *) obfd->link.hash; 940 941 if (htab->loc_hash_table) 942 htab_delete (htab->loc_hash_table); 943 if (htab->loc_hash_memory) 944 objalloc_free ((struct objalloc *) htab->loc_hash_memory); 945 _bfd_elf_link_hash_table_free (obfd); 946 } 947 948 /* Create an i386 ELF linker hash table. */ 949 950 static struct bfd_link_hash_table * 951 elf_i386_link_hash_table_create (bfd *abfd) 952 { 953 struct elf_i386_link_hash_table *ret; 954 bfd_size_type amt = sizeof (struct elf_i386_link_hash_table); 955 956 ret = (struct elf_i386_link_hash_table *) bfd_zmalloc (amt); 957 if (ret == NULL) 958 return NULL; 959 960 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, 961 elf_i386_link_hash_newfunc, 962 sizeof (struct elf_i386_link_hash_entry), 963 I386_ELF_DATA)) 964 { 965 free (ret); 966 return NULL; 967 } 968 969 ret->loc_hash_table = htab_try_create (1024, 970 elf_i386_local_htab_hash, 971 elf_i386_local_htab_eq, 972 NULL); 973 ret->loc_hash_memory = objalloc_create (); 974 if (!ret->loc_hash_table || !ret->loc_hash_memory) 975 { 976 elf_i386_link_hash_table_free (abfd); 977 return NULL; 978 } 979 ret->elf.root.hash_table_free = elf_i386_link_hash_table_free; 980 981 return &ret->elf.root; 982 } 983 984 /* Create .plt, .rel.plt, .got, .got.plt, .rel.got, .dynbss, and 985 .rel.bss sections in DYNOBJ, and set up shortcuts to them in our 986 hash table. */ 987 988 static bfd_boolean 989 elf_i386_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info) 990 { 991 struct elf_i386_link_hash_table *htab; 992 993 if (!_bfd_elf_create_dynamic_sections (dynobj, info)) 994 return FALSE; 995 996 htab = elf_i386_hash_table (info); 997 if (htab == NULL) 998 return FALSE; 999 1000 htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss"); 1001 if (!info->shared) 1002 htab->srelbss = bfd_get_linker_section (dynobj, ".rel.bss"); 1003 1004 if (!htab->sdynbss 1005 || (!info->shared && !htab->srelbss)) 1006 abort (); 1007 1008 if (get_elf_i386_backend_data (dynobj)->is_vxworks 1009 && !elf_vxworks_create_dynamic_sections (dynobj, info, 1010 &htab->srelplt2)) 1011 return FALSE; 1012 1013 if (!info->no_ld_generated_unwind_info 1014 && htab->plt_eh_frame == NULL 1015 && htab->elf.splt != NULL) 1016 { 1017 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY 1018 | SEC_HAS_CONTENTS | SEC_IN_MEMORY 1019 | SEC_LINKER_CREATED); 1020 htab->plt_eh_frame 1021 = bfd_make_section_anyway_with_flags (dynobj, ".eh_frame", flags); 1022 if (htab->plt_eh_frame == NULL 1023 || !bfd_set_section_alignment (dynobj, htab->plt_eh_frame, 2)) 1024 return FALSE; 1025 } 1026 1027 return TRUE; 1028 } 1029 1030 /* Copy the extra info we tack onto an elf_link_hash_entry. */ 1031 1032 static void 1033 elf_i386_copy_indirect_symbol (struct bfd_link_info *info, 1034 struct elf_link_hash_entry *dir, 1035 struct elf_link_hash_entry *ind) 1036 { 1037 struct elf_i386_link_hash_entry *edir, *eind; 1038 1039 edir = (struct elf_i386_link_hash_entry *) dir; 1040 eind = (struct elf_i386_link_hash_entry *) ind; 1041 1042 if (eind->dyn_relocs != NULL) 1043 { 1044 if (edir->dyn_relocs != NULL) 1045 { 1046 struct elf_dyn_relocs **pp; 1047 struct elf_dyn_relocs *p; 1048 1049 /* Add reloc counts against the indirect sym to the direct sym 1050 list. Merge any entries against the same section. */ 1051 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; ) 1052 { 1053 struct elf_dyn_relocs *q; 1054 1055 for (q = edir->dyn_relocs; q != NULL; q = q->next) 1056 if (q->sec == p->sec) 1057 { 1058 q->pc_count += p->pc_count; 1059 q->count += p->count; 1060 *pp = p->next; 1061 break; 1062 } 1063 if (q == NULL) 1064 pp = &p->next; 1065 } 1066 *pp = edir->dyn_relocs; 1067 } 1068 1069 edir->dyn_relocs = eind->dyn_relocs; 1070 eind->dyn_relocs = NULL; 1071 } 1072 1073 if (ind->root.type == bfd_link_hash_indirect 1074 && dir->got.refcount <= 0) 1075 { 1076 edir->tls_type = eind->tls_type; 1077 eind->tls_type = GOT_UNKNOWN; 1078 } 1079 1080 if (ELIMINATE_COPY_RELOCS 1081 && ind->root.type != bfd_link_hash_indirect 1082 && dir->dynamic_adjusted) 1083 { 1084 /* If called to transfer flags for a weakdef during processing 1085 of elf_adjust_dynamic_symbol, don't copy non_got_ref. 1086 We clear it ourselves for ELIMINATE_COPY_RELOCS. */ 1087 dir->ref_dynamic |= ind->ref_dynamic; 1088 dir->ref_regular |= ind->ref_regular; 1089 dir->ref_regular_nonweak |= ind->ref_regular_nonweak; 1090 dir->needs_plt |= ind->needs_plt; 1091 dir->pointer_equality_needed |= ind->pointer_equality_needed; 1092 } 1093 else 1094 _bfd_elf_link_hash_copy_indirect (info, dir, ind); 1095 } 1096 1097 /* Return TRUE if the TLS access code sequence support transition 1098 from R_TYPE. */ 1099 1100 static bfd_boolean 1101 elf_i386_check_tls_transition (bfd *abfd, asection *sec, 1102 bfd_byte *contents, 1103 Elf_Internal_Shdr *symtab_hdr, 1104 struct elf_link_hash_entry **sym_hashes, 1105 unsigned int r_type, 1106 const Elf_Internal_Rela *rel, 1107 const Elf_Internal_Rela *relend) 1108 { 1109 unsigned int val, type; 1110 unsigned long r_symndx; 1111 struct elf_link_hash_entry *h; 1112 bfd_vma offset; 1113 1114 /* Get the section contents. */ 1115 if (contents == NULL) 1116 { 1117 if (elf_section_data (sec)->this_hdr.contents != NULL) 1118 contents = elf_section_data (sec)->this_hdr.contents; 1119 else 1120 { 1121 /* FIXME: How to better handle error condition? */ 1122 if (!bfd_malloc_and_get_section (abfd, sec, &contents)) 1123 return FALSE; 1124 1125 /* Cache the section contents for elf_link_input_bfd. */ 1126 elf_section_data (sec)->this_hdr.contents = contents; 1127 } 1128 } 1129 1130 offset = rel->r_offset; 1131 switch (r_type) 1132 { 1133 case R_386_TLS_GD: 1134 case R_386_TLS_LDM: 1135 if (offset < 2 || (rel + 1) >= relend) 1136 return FALSE; 1137 1138 type = bfd_get_8 (abfd, contents + offset - 2); 1139 if (r_type == R_386_TLS_GD) 1140 { 1141 /* Check transition from GD access model. Only 1142 leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr 1143 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop 1144 can transit to different access model. */ 1145 if ((offset + 10) > sec->size || 1146 (type != 0x8d && type != 0x04)) 1147 return FALSE; 1148 1149 val = bfd_get_8 (abfd, contents + offset - 1); 1150 if (type == 0x04) 1151 { 1152 /* leal foo@tlsgd(,%reg,1), %eax; call ___tls_get_addr */ 1153 if (offset < 3) 1154 return FALSE; 1155 1156 if (bfd_get_8 (abfd, contents + offset - 3) != 0x8d) 1157 return FALSE; 1158 1159 if ((val & 0xc7) != 0x05 || val == (4 << 3)) 1160 return FALSE; 1161 } 1162 else 1163 { 1164 /* leal foo@tlsgd(%reg), %eax; call ___tls_get_addr; nop */ 1165 if ((val & 0xf8) != 0x80 || (val & 7) == 4) 1166 return FALSE; 1167 1168 if (bfd_get_8 (abfd, contents + offset + 9) != 0x90) 1169 return FALSE; 1170 } 1171 } 1172 else 1173 { 1174 /* Check transition from LD access model. Only 1175 leal foo@tlsgd(%reg), %eax; call ___tls_get_addr 1176 can transit to different access model. */ 1177 if (type != 0x8d || (offset + 9) > sec->size) 1178 return FALSE; 1179 1180 val = bfd_get_8 (abfd, contents + offset - 1); 1181 if ((val & 0xf8) != 0x80 || (val & 7) == 4) 1182 return FALSE; 1183 } 1184 1185 if (bfd_get_8 (abfd, contents + offset + 4) != 0xe8) 1186 return FALSE; 1187 1188 r_symndx = ELF32_R_SYM (rel[1].r_info); 1189 if (r_symndx < symtab_hdr->sh_info) 1190 return FALSE; 1191 1192 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 1193 /* Use strncmp to check ___tls_get_addr since ___tls_get_addr 1194 may be versioned. */ 1195 return (h != NULL 1196 && h->root.root.string != NULL 1197 && (ELF32_R_TYPE (rel[1].r_info) == R_386_PC32 1198 || ELF32_R_TYPE (rel[1].r_info) == R_386_PLT32) 1199 && (strncmp (h->root.root.string, "___tls_get_addr", 1200 15) == 0)); 1201 1202 case R_386_TLS_IE: 1203 /* Check transition from IE access model: 1204 movl foo@indntpoff(%rip), %eax 1205 movl foo@indntpoff(%rip), %reg 1206 addl foo@indntpoff(%rip), %reg 1207 */ 1208 1209 if (offset < 1 || (offset + 4) > sec->size) 1210 return FALSE; 1211 1212 /* Check "movl foo@tpoff(%rip), %eax" first. */ 1213 val = bfd_get_8 (abfd, contents + offset - 1); 1214 if (val == 0xa1) 1215 return TRUE; 1216 1217 if (offset < 2) 1218 return FALSE; 1219 1220 /* Check movl|addl foo@tpoff(%rip), %reg. */ 1221 type = bfd_get_8 (abfd, contents + offset - 2); 1222 return ((type == 0x8b || type == 0x03) 1223 && (val & 0xc7) == 0x05); 1224 1225 case R_386_TLS_GOTIE: 1226 case R_386_TLS_IE_32: 1227 /* Check transition from {IE_32,GOTIE} access model: 1228 subl foo@{tpoff,gontoff}(%reg1), %reg2 1229 movl foo@{tpoff,gontoff}(%reg1), %reg2 1230 addl foo@{tpoff,gontoff}(%reg1), %reg2 1231 */ 1232 1233 if (offset < 2 || (offset + 4) > sec->size) 1234 return FALSE; 1235 1236 val = bfd_get_8 (abfd, contents + offset - 1); 1237 if ((val & 0xc0) != 0x80 || (val & 7) == 4) 1238 return FALSE; 1239 1240 type = bfd_get_8 (abfd, contents + offset - 2); 1241 return type == 0x8b || type == 0x2b || type == 0x03; 1242 1243 case R_386_TLS_GOTDESC: 1244 /* Check transition from GDesc access model: 1245 leal x@tlsdesc(%ebx), %eax 1246 1247 Make sure it's a leal adding ebx to a 32-bit offset 1248 into any register, although it's probably almost always 1249 going to be eax. */ 1250 1251 if (offset < 2 || (offset + 4) > sec->size) 1252 return FALSE; 1253 1254 if (bfd_get_8 (abfd, contents + offset - 2) != 0x8d) 1255 return FALSE; 1256 1257 val = bfd_get_8 (abfd, contents + offset - 1); 1258 return (val & 0xc7) == 0x83; 1259 1260 case R_386_TLS_DESC_CALL: 1261 /* Check transition from GDesc access model: 1262 call *x@tlsdesc(%rax) 1263 */ 1264 if (offset + 2 <= sec->size) 1265 { 1266 /* Make sure that it's a call *x@tlsdesc(%rax). */ 1267 static const unsigned char call[] = { 0xff, 0x10 }; 1268 return memcmp (contents + offset, call, 2) == 0; 1269 } 1270 1271 return FALSE; 1272 1273 default: 1274 abort (); 1275 } 1276 } 1277 1278 /* Return TRUE if the TLS access transition is OK or no transition 1279 will be performed. Update R_TYPE if there is a transition. */ 1280 1281 static bfd_boolean 1282 elf_i386_tls_transition (struct bfd_link_info *info, bfd *abfd, 1283 asection *sec, bfd_byte *contents, 1284 Elf_Internal_Shdr *symtab_hdr, 1285 struct elf_link_hash_entry **sym_hashes, 1286 unsigned int *r_type, int tls_type, 1287 const Elf_Internal_Rela *rel, 1288 const Elf_Internal_Rela *relend, 1289 struct elf_link_hash_entry *h, 1290 unsigned long r_symndx) 1291 { 1292 unsigned int from_type = *r_type; 1293 unsigned int to_type = from_type; 1294 bfd_boolean check = TRUE; 1295 1296 /* Skip TLS transition for functions. */ 1297 if (h != NULL 1298 && (h->type == STT_FUNC 1299 || h->type == STT_GNU_IFUNC)) 1300 return TRUE; 1301 1302 switch (from_type) 1303 { 1304 case R_386_TLS_GD: 1305 case R_386_TLS_GOTDESC: 1306 case R_386_TLS_DESC_CALL: 1307 case R_386_TLS_IE_32: 1308 case R_386_TLS_IE: 1309 case R_386_TLS_GOTIE: 1310 if (info->executable) 1311 { 1312 if (h == NULL) 1313 to_type = R_386_TLS_LE_32; 1314 else if (from_type != R_386_TLS_IE 1315 && from_type != R_386_TLS_GOTIE) 1316 to_type = R_386_TLS_IE_32; 1317 } 1318 1319 /* When we are called from elf_i386_relocate_section, CONTENTS 1320 isn't NULL and there may be additional transitions based on 1321 TLS_TYPE. */ 1322 if (contents != NULL) 1323 { 1324 unsigned int new_to_type = to_type; 1325 1326 if (info->executable 1327 && h != NULL 1328 && h->dynindx == -1 1329 && (tls_type & GOT_TLS_IE)) 1330 new_to_type = R_386_TLS_LE_32; 1331 1332 if (to_type == R_386_TLS_GD 1333 || to_type == R_386_TLS_GOTDESC 1334 || to_type == R_386_TLS_DESC_CALL) 1335 { 1336 if (tls_type == GOT_TLS_IE_POS) 1337 new_to_type = R_386_TLS_GOTIE; 1338 else if (tls_type & GOT_TLS_IE) 1339 new_to_type = R_386_TLS_IE_32; 1340 } 1341 1342 /* We checked the transition before when we were called from 1343 elf_i386_check_relocs. We only want to check the new 1344 transition which hasn't been checked before. */ 1345 check = new_to_type != to_type && from_type == to_type; 1346 to_type = new_to_type; 1347 } 1348 1349 break; 1350 1351 case R_386_TLS_LDM: 1352 if (info->executable) 1353 to_type = R_386_TLS_LE_32; 1354 break; 1355 1356 default: 1357 return TRUE; 1358 } 1359 1360 /* Return TRUE if there is no transition. */ 1361 if (from_type == to_type) 1362 return TRUE; 1363 1364 /* Check if the transition can be performed. */ 1365 if (check 1366 && ! elf_i386_check_tls_transition (abfd, sec, contents, 1367 symtab_hdr, sym_hashes, 1368 from_type, rel, relend)) 1369 { 1370 reloc_howto_type *from, *to; 1371 const char *name; 1372 1373 from = elf_i386_rtype_to_howto (abfd, from_type); 1374 to = elf_i386_rtype_to_howto (abfd, to_type); 1375 1376 if (h) 1377 name = h->root.root.string; 1378 else 1379 { 1380 struct elf_i386_link_hash_table *htab; 1381 1382 htab = elf_i386_hash_table (info); 1383 if (htab == NULL) 1384 name = "*unknown*"; 1385 else 1386 { 1387 Elf_Internal_Sym *isym; 1388 1389 isym = bfd_sym_from_r_symndx (&htab->sym_cache, 1390 abfd, r_symndx); 1391 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL); 1392 } 1393 } 1394 1395 (*_bfd_error_handler) 1396 (_("%B: TLS transition from %s to %s against `%s' at 0x%lx " 1397 "in section `%A' failed"), 1398 abfd, sec, from->name, to->name, name, 1399 (unsigned long) rel->r_offset); 1400 bfd_set_error (bfd_error_bad_value); 1401 return FALSE; 1402 } 1403 1404 *r_type = to_type; 1405 return TRUE; 1406 } 1407 1408 /* Look through the relocs for a section during the first phase, and 1409 calculate needed space in the global offset table, procedure linkage 1410 table, and dynamic reloc sections. */ 1411 1412 static bfd_boolean 1413 elf_i386_check_relocs (bfd *abfd, 1414 struct bfd_link_info *info, 1415 asection *sec, 1416 const Elf_Internal_Rela *relocs) 1417 { 1418 struct elf_i386_link_hash_table *htab; 1419 Elf_Internal_Shdr *symtab_hdr; 1420 struct elf_link_hash_entry **sym_hashes; 1421 const Elf_Internal_Rela *rel; 1422 const Elf_Internal_Rela *rel_end; 1423 asection *sreloc; 1424 1425 if (info->relocatable) 1426 return TRUE; 1427 1428 BFD_ASSERT (is_i386_elf (abfd)); 1429 1430 htab = elf_i386_hash_table (info); 1431 if (htab == NULL) 1432 return FALSE; 1433 1434 symtab_hdr = &elf_symtab_hdr (abfd); 1435 sym_hashes = elf_sym_hashes (abfd); 1436 1437 sreloc = NULL; 1438 1439 rel_end = relocs + sec->reloc_count; 1440 for (rel = relocs; rel < rel_end; rel++) 1441 { 1442 unsigned int r_type; 1443 unsigned long r_symndx; 1444 struct elf_link_hash_entry *h; 1445 Elf_Internal_Sym *isym; 1446 const char *name; 1447 bfd_boolean size_reloc; 1448 1449 r_symndx = ELF32_R_SYM (rel->r_info); 1450 r_type = ELF32_R_TYPE (rel->r_info); 1451 1452 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr)) 1453 { 1454 (*_bfd_error_handler) (_("%B: bad symbol index: %d"), 1455 abfd, 1456 r_symndx); 1457 return FALSE; 1458 } 1459 1460 if (r_symndx < symtab_hdr->sh_info) 1461 { 1462 /* A local symbol. */ 1463 isym = bfd_sym_from_r_symndx (&htab->sym_cache, 1464 abfd, r_symndx); 1465 if (isym == NULL) 1466 return FALSE; 1467 1468 /* Check relocation against local STT_GNU_IFUNC symbol. */ 1469 if (ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC) 1470 { 1471 h = elf_i386_get_local_sym_hash (htab, abfd, rel, TRUE); 1472 if (h == NULL) 1473 return FALSE; 1474 1475 /* Fake a STT_GNU_IFUNC symbol. */ 1476 h->type = STT_GNU_IFUNC; 1477 h->def_regular = 1; 1478 h->ref_regular = 1; 1479 h->forced_local = 1; 1480 h->root.type = bfd_link_hash_defined; 1481 } 1482 else 1483 h = NULL; 1484 } 1485 else 1486 { 1487 isym = NULL; 1488 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 1489 while (h->root.type == bfd_link_hash_indirect 1490 || h->root.type == bfd_link_hash_warning) 1491 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1492 } 1493 1494 if (h != NULL) 1495 { 1496 /* Create the ifunc sections for static executables. If we 1497 never see an indirect function symbol nor we are building 1498 a static executable, those sections will be empty and 1499 won't appear in output. */ 1500 switch (r_type) 1501 { 1502 default: 1503 break; 1504 1505 case R_386_32: 1506 case R_386_PC32: 1507 case R_386_PLT32: 1508 case R_386_GOT32: 1509 case R_386_GOTOFF: 1510 if (htab->elf.dynobj == NULL) 1511 htab->elf.dynobj = abfd; 1512 if (!_bfd_elf_create_ifunc_sections (htab->elf.dynobj, info)) 1513 return FALSE; 1514 break; 1515 } 1516 1517 /* It is referenced by a non-shared object. */ 1518 h->ref_regular = 1; 1519 h->root.non_ir_ref = 1; 1520 } 1521 1522 if (! elf_i386_tls_transition (info, abfd, sec, NULL, 1523 symtab_hdr, sym_hashes, 1524 &r_type, GOT_UNKNOWN, 1525 rel, rel_end, h, r_symndx)) 1526 return FALSE; 1527 1528 switch (r_type) 1529 { 1530 case R_386_TLS_LDM: 1531 htab->tls_ldm_got.refcount += 1; 1532 goto create_got; 1533 1534 case R_386_PLT32: 1535 /* This symbol requires a procedure linkage table entry. We 1536 actually build the entry in adjust_dynamic_symbol, 1537 because this might be a case of linking PIC code which is 1538 never referenced by a dynamic object, in which case we 1539 don't need to generate a procedure linkage table entry 1540 after all. */ 1541 1542 /* If this is a local symbol, we resolve it directly without 1543 creating a procedure linkage table entry. */ 1544 if (h == NULL) 1545 continue; 1546 1547 h->needs_plt = 1; 1548 h->plt.refcount += 1; 1549 break; 1550 1551 case R_386_SIZE32: 1552 size_reloc = TRUE; 1553 goto do_size; 1554 1555 case R_386_TLS_IE_32: 1556 case R_386_TLS_IE: 1557 case R_386_TLS_GOTIE: 1558 if (!info->executable) 1559 info->flags |= DF_STATIC_TLS; 1560 /* Fall through */ 1561 1562 case R_386_GOT32: 1563 case R_386_TLS_GD: 1564 case R_386_TLS_GOTDESC: 1565 case R_386_TLS_DESC_CALL: 1566 /* This symbol requires a global offset table entry. */ 1567 { 1568 int tls_type, old_tls_type; 1569 1570 switch (r_type) 1571 { 1572 default: 1573 case R_386_GOT32: tls_type = GOT_NORMAL; break; 1574 case R_386_TLS_GD: tls_type = GOT_TLS_GD; break; 1575 case R_386_TLS_GOTDESC: 1576 case R_386_TLS_DESC_CALL: 1577 tls_type = GOT_TLS_GDESC; break; 1578 case R_386_TLS_IE_32: 1579 if (ELF32_R_TYPE (rel->r_info) == r_type) 1580 tls_type = GOT_TLS_IE_NEG; 1581 else 1582 /* If this is a GD->IE transition, we may use either of 1583 R_386_TLS_TPOFF and R_386_TLS_TPOFF32. */ 1584 tls_type = GOT_TLS_IE; 1585 break; 1586 case R_386_TLS_IE: 1587 case R_386_TLS_GOTIE: 1588 tls_type = GOT_TLS_IE_POS; break; 1589 } 1590 1591 if (h != NULL) 1592 { 1593 h->got.refcount += 1; 1594 old_tls_type = elf_i386_hash_entry(h)->tls_type; 1595 } 1596 else 1597 { 1598 bfd_signed_vma *local_got_refcounts; 1599 1600 /* This is a global offset table entry for a local symbol. */ 1601 local_got_refcounts = elf_local_got_refcounts (abfd); 1602 if (local_got_refcounts == NULL) 1603 { 1604 bfd_size_type size; 1605 1606 size = symtab_hdr->sh_info; 1607 size *= (sizeof (bfd_signed_vma) 1608 + sizeof (bfd_vma) + sizeof(char)); 1609 local_got_refcounts = (bfd_signed_vma *) 1610 bfd_zalloc (abfd, size); 1611 if (local_got_refcounts == NULL) 1612 return FALSE; 1613 elf_local_got_refcounts (abfd) = local_got_refcounts; 1614 elf_i386_local_tlsdesc_gotent (abfd) 1615 = (bfd_vma *) (local_got_refcounts + symtab_hdr->sh_info); 1616 elf_i386_local_got_tls_type (abfd) 1617 = (char *) (local_got_refcounts + 2 * symtab_hdr->sh_info); 1618 } 1619 local_got_refcounts[r_symndx] += 1; 1620 old_tls_type = elf_i386_local_got_tls_type (abfd) [r_symndx]; 1621 } 1622 1623 if ((old_tls_type & GOT_TLS_IE) && (tls_type & GOT_TLS_IE)) 1624 tls_type |= old_tls_type; 1625 /* If a TLS symbol is accessed using IE at least once, 1626 there is no point to use dynamic model for it. */ 1627 else if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN 1628 && (! GOT_TLS_GD_ANY_P (old_tls_type) 1629 || (tls_type & GOT_TLS_IE) == 0)) 1630 { 1631 if ((old_tls_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (tls_type)) 1632 tls_type = old_tls_type; 1633 else if (GOT_TLS_GD_ANY_P (old_tls_type) 1634 && GOT_TLS_GD_ANY_P (tls_type)) 1635 tls_type |= old_tls_type; 1636 else 1637 { 1638 if (h) 1639 name = h->root.root.string; 1640 else 1641 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, 1642 NULL); 1643 (*_bfd_error_handler) 1644 (_("%B: `%s' accessed both as normal and " 1645 "thread local symbol"), 1646 abfd, name); 1647 bfd_set_error (bfd_error_bad_value); 1648 return FALSE; 1649 } 1650 } 1651 1652 if (old_tls_type != tls_type) 1653 { 1654 if (h != NULL) 1655 elf_i386_hash_entry (h)->tls_type = tls_type; 1656 else 1657 elf_i386_local_got_tls_type (abfd) [r_symndx] = tls_type; 1658 } 1659 } 1660 /* Fall through */ 1661 1662 case R_386_GOTOFF: 1663 case R_386_GOTPC: 1664 create_got: 1665 if (htab->elf.sgot == NULL) 1666 { 1667 if (htab->elf.dynobj == NULL) 1668 htab->elf.dynobj = abfd; 1669 if (!_bfd_elf_create_got_section (htab->elf.dynobj, info)) 1670 return FALSE; 1671 } 1672 if (r_type != R_386_TLS_IE) 1673 break; 1674 /* Fall through */ 1675 1676 case R_386_TLS_LE_32: 1677 case R_386_TLS_LE: 1678 if (info->executable) 1679 break; 1680 info->flags |= DF_STATIC_TLS; 1681 /* Fall through */ 1682 1683 case R_386_32: 1684 case R_386_PC32: 1685 if (h != NULL && info->executable) 1686 { 1687 /* If this reloc is in a read-only section, we might 1688 need a copy reloc. We can't check reliably at this 1689 stage whether the section is read-only, as input 1690 sections have not yet been mapped to output sections. 1691 Tentatively set the flag for now, and correct in 1692 adjust_dynamic_symbol. */ 1693 h->non_got_ref = 1; 1694 1695 /* We may need a .plt entry if the function this reloc 1696 refers to is in a shared lib. */ 1697 h->plt.refcount += 1; 1698 if (r_type != R_386_PC32) 1699 h->pointer_equality_needed = 1; 1700 } 1701 1702 size_reloc = FALSE; 1703 do_size: 1704 /* If we are creating a shared library, and this is a reloc 1705 against a global symbol, or a non PC relative reloc 1706 against a local symbol, then we need to copy the reloc 1707 into the shared library. However, if we are linking with 1708 -Bsymbolic, we do not need to copy a reloc against a 1709 global symbol which is defined in an object we are 1710 including in the link (i.e., DEF_REGULAR is set). At 1711 this point we have not seen all the input files, so it is 1712 possible that DEF_REGULAR is not set now but will be set 1713 later (it is never cleared). In case of a weak definition, 1714 DEF_REGULAR may be cleared later by a strong definition in 1715 a shared library. We account for that possibility below by 1716 storing information in the relocs_copied field of the hash 1717 table entry. A similar situation occurs when creating 1718 shared libraries and symbol visibility changes render the 1719 symbol local. 1720 1721 If on the other hand, we are creating an executable, we 1722 may need to keep relocations for symbols satisfied by a 1723 dynamic library if we manage to avoid copy relocs for the 1724 symbol. */ 1725 if ((info->shared 1726 && (sec->flags & SEC_ALLOC) != 0 1727 && (r_type != R_386_PC32 1728 || (h != NULL 1729 && (! SYMBOLIC_BIND (info, h) 1730 || h->root.type == bfd_link_hash_defweak 1731 || !h->def_regular)))) 1732 || (ELIMINATE_COPY_RELOCS 1733 && !info->shared 1734 && (sec->flags & SEC_ALLOC) != 0 1735 && h != NULL 1736 && (h->root.type == bfd_link_hash_defweak 1737 || !h->def_regular))) 1738 { 1739 struct elf_dyn_relocs *p; 1740 struct elf_dyn_relocs **head; 1741 1742 /* We must copy these reloc types into the output file. 1743 Create a reloc section in dynobj and make room for 1744 this reloc. */ 1745 if (sreloc == NULL) 1746 { 1747 if (htab->elf.dynobj == NULL) 1748 htab->elf.dynobj = abfd; 1749 1750 sreloc = _bfd_elf_make_dynamic_reloc_section 1751 (sec, htab->elf.dynobj, 2, abfd, /*rela?*/ FALSE); 1752 1753 if (sreloc == NULL) 1754 return FALSE; 1755 } 1756 1757 /* If this is a global symbol, we count the number of 1758 relocations we need for this symbol. */ 1759 if (h != NULL) 1760 { 1761 head = &((struct elf_i386_link_hash_entry *) h)->dyn_relocs; 1762 } 1763 else 1764 { 1765 /* Track dynamic relocs needed for local syms too. 1766 We really need local syms available to do this 1767 easily. Oh well. */ 1768 void **vpp; 1769 asection *s; 1770 1771 isym = bfd_sym_from_r_symndx (&htab->sym_cache, 1772 abfd, r_symndx); 1773 if (isym == NULL) 1774 return FALSE; 1775 1776 s = bfd_section_from_elf_index (abfd, isym->st_shndx); 1777 if (s == NULL) 1778 s = sec; 1779 1780 vpp = &elf_section_data (s)->local_dynrel; 1781 head = (struct elf_dyn_relocs **)vpp; 1782 } 1783 1784 p = *head; 1785 if (p == NULL || p->sec != sec) 1786 { 1787 bfd_size_type amt = sizeof *p; 1788 p = (struct elf_dyn_relocs *) bfd_alloc (htab->elf.dynobj, 1789 amt); 1790 if (p == NULL) 1791 return FALSE; 1792 p->next = *head; 1793 *head = p; 1794 p->sec = sec; 1795 p->count = 0; 1796 p->pc_count = 0; 1797 } 1798 1799 p->count += 1; 1800 /* Count size relocation as PC-relative relocation. */ 1801 if (r_type == R_386_PC32 || size_reloc) 1802 p->pc_count += 1; 1803 } 1804 break; 1805 1806 /* This relocation describes the C++ object vtable hierarchy. 1807 Reconstruct it for later use during GC. */ 1808 case R_386_GNU_VTINHERIT: 1809 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) 1810 return FALSE; 1811 break; 1812 1813 /* This relocation describes which C++ vtable entries are actually 1814 used. Record for later use during GC. */ 1815 case R_386_GNU_VTENTRY: 1816 BFD_ASSERT (h != NULL); 1817 if (h != NULL 1818 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset)) 1819 return FALSE; 1820 break; 1821 1822 default: 1823 break; 1824 } 1825 } 1826 1827 return TRUE; 1828 } 1829 1830 /* Return the section that should be marked against GC for a given 1831 relocation. */ 1832 1833 static asection * 1834 elf_i386_gc_mark_hook (asection *sec, 1835 struct bfd_link_info *info, 1836 Elf_Internal_Rela *rel, 1837 struct elf_link_hash_entry *h, 1838 Elf_Internal_Sym *sym) 1839 { 1840 if (h != NULL) 1841 switch (ELF32_R_TYPE (rel->r_info)) 1842 { 1843 case R_386_GNU_VTINHERIT: 1844 case R_386_GNU_VTENTRY: 1845 return NULL; 1846 } 1847 1848 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym); 1849 } 1850 1851 /* Update the got entry reference counts for the section being removed. */ 1852 1853 static bfd_boolean 1854 elf_i386_gc_sweep_hook (bfd *abfd, 1855 struct bfd_link_info *info, 1856 asection *sec, 1857 const Elf_Internal_Rela *relocs) 1858 { 1859 struct elf_i386_link_hash_table *htab; 1860 Elf_Internal_Shdr *symtab_hdr; 1861 struct elf_link_hash_entry **sym_hashes; 1862 bfd_signed_vma *local_got_refcounts; 1863 const Elf_Internal_Rela *rel, *relend; 1864 1865 if (info->relocatable) 1866 return TRUE; 1867 1868 htab = elf_i386_hash_table (info); 1869 if (htab == NULL) 1870 return FALSE; 1871 1872 elf_section_data (sec)->local_dynrel = NULL; 1873 1874 symtab_hdr = &elf_symtab_hdr (abfd); 1875 sym_hashes = elf_sym_hashes (abfd); 1876 local_got_refcounts = elf_local_got_refcounts (abfd); 1877 1878 relend = relocs + sec->reloc_count; 1879 for (rel = relocs; rel < relend; rel++) 1880 { 1881 unsigned long r_symndx; 1882 unsigned int r_type; 1883 struct elf_link_hash_entry *h = NULL; 1884 1885 r_symndx = ELF32_R_SYM (rel->r_info); 1886 if (r_symndx >= symtab_hdr->sh_info) 1887 { 1888 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 1889 while (h->root.type == bfd_link_hash_indirect 1890 || h->root.type == bfd_link_hash_warning) 1891 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1892 } 1893 else 1894 { 1895 /* A local symbol. */ 1896 Elf_Internal_Sym *isym; 1897 1898 isym = bfd_sym_from_r_symndx (&htab->sym_cache, 1899 abfd, r_symndx); 1900 1901 /* Check relocation against local STT_GNU_IFUNC symbol. */ 1902 if (isym != NULL 1903 && ELF32_ST_TYPE (isym->st_info) == STT_GNU_IFUNC) 1904 { 1905 h = elf_i386_get_local_sym_hash (htab, abfd, rel, FALSE); 1906 if (h == NULL) 1907 abort (); 1908 } 1909 } 1910 1911 if (h) 1912 { 1913 struct elf_i386_link_hash_entry *eh; 1914 struct elf_dyn_relocs **pp; 1915 struct elf_dyn_relocs *p; 1916 1917 eh = (struct elf_i386_link_hash_entry *) h; 1918 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next) 1919 if (p->sec == sec) 1920 { 1921 /* Everything must go for SEC. */ 1922 *pp = p->next; 1923 break; 1924 } 1925 } 1926 1927 r_type = ELF32_R_TYPE (rel->r_info); 1928 if (! elf_i386_tls_transition (info, abfd, sec, NULL, 1929 symtab_hdr, sym_hashes, 1930 &r_type, GOT_UNKNOWN, 1931 rel, relend, h, r_symndx)) 1932 return FALSE; 1933 1934 switch (r_type) 1935 { 1936 case R_386_TLS_LDM: 1937 if (htab->tls_ldm_got.refcount > 0) 1938 htab->tls_ldm_got.refcount -= 1; 1939 break; 1940 1941 case R_386_TLS_GD: 1942 case R_386_TLS_GOTDESC: 1943 case R_386_TLS_DESC_CALL: 1944 case R_386_TLS_IE_32: 1945 case R_386_TLS_IE: 1946 case R_386_TLS_GOTIE: 1947 case R_386_GOT32: 1948 if (h != NULL) 1949 { 1950 if (h->got.refcount > 0) 1951 h->got.refcount -= 1; 1952 if (h->type == STT_GNU_IFUNC) 1953 { 1954 if (h->plt.refcount > 0) 1955 h->plt.refcount -= 1; 1956 } 1957 } 1958 else if (local_got_refcounts != NULL) 1959 { 1960 if (local_got_refcounts[r_symndx] > 0) 1961 local_got_refcounts[r_symndx] -= 1; 1962 } 1963 break; 1964 1965 case R_386_32: 1966 case R_386_PC32: 1967 case R_386_SIZE32: 1968 if (info->shared 1969 && (h == NULL || h->type != STT_GNU_IFUNC)) 1970 break; 1971 /* Fall through */ 1972 1973 case R_386_PLT32: 1974 if (h != NULL) 1975 { 1976 if (h->plt.refcount > 0) 1977 h->plt.refcount -= 1; 1978 } 1979 break; 1980 1981 case R_386_GOTOFF: 1982 if (h != NULL && h->type == STT_GNU_IFUNC) 1983 { 1984 if (h->got.refcount > 0) 1985 h->got.refcount -= 1; 1986 if (h->plt.refcount > 0) 1987 h->plt.refcount -= 1; 1988 } 1989 break; 1990 1991 default: 1992 break; 1993 } 1994 } 1995 1996 return TRUE; 1997 } 1998 1999 /* Adjust a symbol defined by a dynamic object and referenced by a 2000 regular object. The current definition is in some section of the 2001 dynamic object, but we're not including those sections. We have to 2002 change the definition to something the rest of the link can 2003 understand. */ 2004 2005 static bfd_boolean 2006 elf_i386_adjust_dynamic_symbol (struct bfd_link_info *info, 2007 struct elf_link_hash_entry *h) 2008 { 2009 struct elf_i386_link_hash_table *htab; 2010 asection *s; 2011 struct elf_i386_link_hash_entry *eh; 2012 struct elf_dyn_relocs *p; 2013 2014 /* STT_GNU_IFUNC symbol must go through PLT. */ 2015 if (h->type == STT_GNU_IFUNC) 2016 { 2017 /* All local STT_GNU_IFUNC references must be treate as local 2018 calls via local PLT. */ 2019 if (h->ref_regular 2020 && SYMBOL_CALLS_LOCAL (info, h)) 2021 { 2022 bfd_size_type pc_count = 0, count = 0; 2023 struct elf_dyn_relocs **pp; 2024 2025 eh = (struct elf_i386_link_hash_entry *) h; 2026 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) 2027 { 2028 pc_count += p->pc_count; 2029 p->count -= p->pc_count; 2030 p->pc_count = 0; 2031 count += p->count; 2032 if (p->count == 0) 2033 *pp = p->next; 2034 else 2035 pp = &p->next; 2036 } 2037 2038 if (pc_count || count) 2039 { 2040 h->needs_plt = 1; 2041 h->non_got_ref = 1; 2042 if (h->plt.refcount <= 0) 2043 h->plt.refcount = 1; 2044 else 2045 h->plt.refcount += 1; 2046 } 2047 } 2048 2049 if (h->plt.refcount <= 0) 2050 { 2051 h->plt.offset = (bfd_vma) -1; 2052 h->needs_plt = 0; 2053 } 2054 return TRUE; 2055 } 2056 2057 /* If this is a function, put it in the procedure linkage table. We 2058 will fill in the contents of the procedure linkage table later, 2059 when we know the address of the .got section. */ 2060 if (h->type == STT_FUNC 2061 || h->needs_plt) 2062 { 2063 if (h->plt.refcount <= 0 2064 || SYMBOL_CALLS_LOCAL (info, h) 2065 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT 2066 && h->root.type == bfd_link_hash_undefweak)) 2067 { 2068 /* This case can occur if we saw a PLT32 reloc in an input 2069 file, but the symbol was never referred to by a dynamic 2070 object, or if all references were garbage collected. In 2071 such a case, we don't actually need to build a procedure 2072 linkage table, and we can just do a PC32 reloc instead. */ 2073 h->plt.offset = (bfd_vma) -1; 2074 h->needs_plt = 0; 2075 } 2076 2077 return TRUE; 2078 } 2079 else 2080 /* It's possible that we incorrectly decided a .plt reloc was 2081 needed for an R_386_PC32 reloc to a non-function sym in 2082 check_relocs. We can't decide accurately between function and 2083 non-function syms in check-relocs; Objects loaded later in 2084 the link may change h->type. So fix it now. */ 2085 h->plt.offset = (bfd_vma) -1; 2086 2087 /* If this is a weak symbol, and there is a real definition, the 2088 processor independent code will have arranged for us to see the 2089 real definition first, and we can just use the same value. */ 2090 if (h->u.weakdef != NULL) 2091 { 2092 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined 2093 || h->u.weakdef->root.type == bfd_link_hash_defweak); 2094 h->root.u.def.section = h->u.weakdef->root.u.def.section; 2095 h->root.u.def.value = h->u.weakdef->root.u.def.value; 2096 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc) 2097 h->non_got_ref = h->u.weakdef->non_got_ref; 2098 return TRUE; 2099 } 2100 2101 /* This is a reference to a symbol defined by a dynamic object which 2102 is not a function. */ 2103 2104 /* If we are creating a shared library, we must presume that the 2105 only references to the symbol are via the global offset table. 2106 For such cases we need not do anything here; the relocations will 2107 be handled correctly by relocate_section. */ 2108 if (info->shared) 2109 return TRUE; 2110 2111 /* If there are no references to this symbol that do not use the 2112 GOT, we don't need to generate a copy reloc. */ 2113 if (!h->non_got_ref) 2114 return TRUE; 2115 2116 /* If -z nocopyreloc was given, we won't generate them either. */ 2117 if (info->nocopyreloc) 2118 { 2119 h->non_got_ref = 0; 2120 return TRUE; 2121 } 2122 2123 htab = elf_i386_hash_table (info); 2124 if (htab == NULL) 2125 return FALSE; 2126 2127 /* If there aren't any dynamic relocs in read-only sections, then 2128 we can keep the dynamic relocs and avoid the copy reloc. This 2129 doesn't work on VxWorks, where we can not have dynamic relocations 2130 (other than copy and jump slot relocations) in an executable. */ 2131 if (ELIMINATE_COPY_RELOCS 2132 && !get_elf_i386_backend_data (info->output_bfd)->is_vxworks) 2133 { 2134 eh = (struct elf_i386_link_hash_entry *) h; 2135 for (p = eh->dyn_relocs; p != NULL; p = p->next) 2136 { 2137 s = p->sec->output_section; 2138 if (s != NULL && (s->flags & SEC_READONLY) != 0) 2139 break; 2140 } 2141 2142 if (p == NULL) 2143 { 2144 h->non_got_ref = 0; 2145 return TRUE; 2146 } 2147 } 2148 2149 /* We must allocate the symbol in our .dynbss section, which will 2150 become part of the .bss section of the executable. There will be 2151 an entry for this symbol in the .dynsym section. The dynamic 2152 object will contain position independent code, so all references 2153 from the dynamic object to this symbol will go through the global 2154 offset table. The dynamic linker will use the .dynsym entry to 2155 determine the address it must put in the global offset table, so 2156 both the dynamic object and the regular object will refer to the 2157 same memory location for the variable. */ 2158 2159 /* We must generate a R_386_COPY reloc to tell the dynamic linker to 2160 copy the initial value out of the dynamic object and into the 2161 runtime process image. */ 2162 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0) 2163 { 2164 htab->srelbss->size += sizeof (Elf32_External_Rel); 2165 h->needs_copy = 1; 2166 } 2167 2168 s = htab->sdynbss; 2169 2170 return _bfd_elf_adjust_dynamic_copy (h, s); 2171 } 2172 2173 /* Allocate space in .plt, .got and associated reloc sections for 2174 dynamic relocs. */ 2175 2176 static bfd_boolean 2177 elf_i386_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf) 2178 { 2179 struct bfd_link_info *info; 2180 struct elf_i386_link_hash_table *htab; 2181 struct elf_i386_link_hash_entry *eh; 2182 struct elf_dyn_relocs *p; 2183 unsigned plt_entry_size; 2184 2185 if (h->root.type == bfd_link_hash_indirect) 2186 return TRUE; 2187 2188 eh = (struct elf_i386_link_hash_entry *) h; 2189 2190 info = (struct bfd_link_info *) inf; 2191 htab = elf_i386_hash_table (info); 2192 if (htab == NULL) 2193 return FALSE; 2194 2195 plt_entry_size = GET_PLT_ENTRY_SIZE (info->output_bfd); 2196 2197 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle it 2198 here if it is defined and referenced in a non-shared object. */ 2199 if (h->type == STT_GNU_IFUNC 2200 && h->def_regular) 2201 return _bfd_elf_allocate_ifunc_dyn_relocs (info, h, &eh->dyn_relocs, 2202 plt_entry_size, 2203 plt_entry_size, 4); 2204 else if (htab->elf.dynamic_sections_created 2205 && h->plt.refcount > 0) 2206 { 2207 /* Make sure this symbol is output as a dynamic symbol. 2208 Undefined weak syms won't yet be marked as dynamic. */ 2209 if (h->dynindx == -1 2210 && !h->forced_local) 2211 { 2212 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 2213 return FALSE; 2214 } 2215 2216 if (info->shared 2217 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h)) 2218 { 2219 asection *s = htab->elf.splt; 2220 2221 /* If this is the first .plt entry, make room for the special 2222 first entry. */ 2223 if (s->size == 0) 2224 s->size = plt_entry_size; 2225 2226 h->plt.offset = s->size; 2227 2228 /* If this symbol is not defined in a regular file, and we are 2229 not generating a shared library, then set the symbol to this 2230 location in the .plt. This is required to make function 2231 pointers compare as equal between the normal executable and 2232 the shared library. */ 2233 if (! info->shared 2234 && !h->def_regular) 2235 { 2236 h->root.u.def.section = s; 2237 h->root.u.def.value = h->plt.offset; 2238 } 2239 2240 /* Make room for this entry. */ 2241 s->size += plt_entry_size; 2242 2243 /* We also need to make an entry in the .got.plt section, which 2244 will be placed in the .got section by the linker script. */ 2245 htab->elf.sgotplt->size += 4; 2246 2247 /* We also need to make an entry in the .rel.plt section. */ 2248 htab->elf.srelplt->size += sizeof (Elf32_External_Rel); 2249 htab->elf.srelplt->reloc_count++; 2250 2251 if (get_elf_i386_backend_data (info->output_bfd)->is_vxworks 2252 && !info->shared) 2253 { 2254 /* VxWorks has a second set of relocations for each PLT entry 2255 in executables. They go in a separate relocation section, 2256 which is processed by the kernel loader. */ 2257 2258 /* There are two relocations for the initial PLT entry: an 2259 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 4 and an 2260 R_386_32 relocation for _GLOBAL_OFFSET_TABLE_ + 8. */ 2261 2262 if (h->plt.offset == plt_entry_size) 2263 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2); 2264 2265 /* There are two extra relocations for each subsequent PLT entry: 2266 an R_386_32 relocation for the GOT entry, and an R_386_32 2267 relocation for the PLT entry. */ 2268 2269 htab->srelplt2->size += (sizeof (Elf32_External_Rel) * 2); 2270 } 2271 } 2272 else 2273 { 2274 h->plt.offset = (bfd_vma) -1; 2275 h->needs_plt = 0; 2276 } 2277 } 2278 else 2279 { 2280 h->plt.offset = (bfd_vma) -1; 2281 h->needs_plt = 0; 2282 } 2283 2284 eh->tlsdesc_got = (bfd_vma) -1; 2285 2286 /* If R_386_TLS_{IE_32,IE,GOTIE} symbol is now local to the binary, 2287 make it a R_386_TLS_LE_32 requiring no TLS entry. */ 2288 if (h->got.refcount > 0 2289 && info->executable 2290 && h->dynindx == -1 2291 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE)) 2292 h->got.offset = (bfd_vma) -1; 2293 else if (h->got.refcount > 0) 2294 { 2295 asection *s; 2296 bfd_boolean dyn; 2297 int tls_type = elf_i386_hash_entry(h)->tls_type; 2298 2299 /* Make sure this symbol is output as a dynamic symbol. 2300 Undefined weak syms won't yet be marked as dynamic. */ 2301 if (h->dynindx == -1 2302 && !h->forced_local) 2303 { 2304 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 2305 return FALSE; 2306 } 2307 2308 s = htab->elf.sgot; 2309 if (GOT_TLS_GDESC_P (tls_type)) 2310 { 2311 eh->tlsdesc_got = htab->elf.sgotplt->size 2312 - elf_i386_compute_jump_table_size (htab); 2313 htab->elf.sgotplt->size += 8; 2314 h->got.offset = (bfd_vma) -2; 2315 } 2316 if (! GOT_TLS_GDESC_P (tls_type) 2317 || GOT_TLS_GD_P (tls_type)) 2318 { 2319 h->got.offset = s->size; 2320 s->size += 4; 2321 /* R_386_TLS_GD needs 2 consecutive GOT slots. */ 2322 if (GOT_TLS_GD_P (tls_type) || tls_type == GOT_TLS_IE_BOTH) 2323 s->size += 4; 2324 } 2325 dyn = htab->elf.dynamic_sections_created; 2326 /* R_386_TLS_IE_32 needs one dynamic relocation, 2327 R_386_TLS_IE resp. R_386_TLS_GOTIE needs one dynamic relocation, 2328 (but if both R_386_TLS_IE_32 and R_386_TLS_IE is present, we 2329 need two), R_386_TLS_GD needs one if local symbol and two if 2330 global. */ 2331 if (tls_type == GOT_TLS_IE_BOTH) 2332 htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel); 2333 else if ((GOT_TLS_GD_P (tls_type) && h->dynindx == -1) 2334 || (tls_type & GOT_TLS_IE)) 2335 htab->elf.srelgot->size += sizeof (Elf32_External_Rel); 2336 else if (GOT_TLS_GD_P (tls_type)) 2337 htab->elf.srelgot->size += 2 * sizeof (Elf32_External_Rel); 2338 else if (! GOT_TLS_GDESC_P (tls_type) 2339 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 2340 || h->root.type != bfd_link_hash_undefweak) 2341 && (info->shared 2342 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))) 2343 htab->elf.srelgot->size += sizeof (Elf32_External_Rel); 2344 if (GOT_TLS_GDESC_P (tls_type)) 2345 htab->elf.srelplt->size += sizeof (Elf32_External_Rel); 2346 } 2347 else 2348 h->got.offset = (bfd_vma) -1; 2349 2350 if (eh->dyn_relocs == NULL) 2351 return TRUE; 2352 2353 /* In the shared -Bsymbolic case, discard space allocated for 2354 dynamic pc-relative relocs against symbols which turn out to be 2355 defined in regular objects. For the normal shared case, discard 2356 space for pc-relative relocs that have become local due to symbol 2357 visibility changes. */ 2358 2359 if (info->shared) 2360 { 2361 /* The only reloc that uses pc_count is R_386_PC32, which will 2362 appear on a call or on something like ".long foo - .". We 2363 want calls to protected symbols to resolve directly to the 2364 function rather than going via the plt. If people want 2365 function pointer comparisons to work as expected then they 2366 should avoid writing assembly like ".long foo - .". */ 2367 if (SYMBOL_CALLS_LOCAL (info, h)) 2368 { 2369 struct elf_dyn_relocs **pp; 2370 2371 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) 2372 { 2373 p->count -= p->pc_count; 2374 p->pc_count = 0; 2375 if (p->count == 0) 2376 *pp = p->next; 2377 else 2378 pp = &p->next; 2379 } 2380 } 2381 2382 if (get_elf_i386_backend_data (info->output_bfd)->is_vxworks) 2383 { 2384 struct elf_dyn_relocs **pp; 2385 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; ) 2386 { 2387 if (strcmp (p->sec->output_section->name, ".tls_vars") == 0) 2388 *pp = p->next; 2389 else 2390 pp = &p->next; 2391 } 2392 } 2393 2394 /* Also discard relocs on undefined weak syms with non-default 2395 visibility. */ 2396 if (eh->dyn_relocs != NULL 2397 && h->root.type == bfd_link_hash_undefweak) 2398 { 2399 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) 2400 eh->dyn_relocs = NULL; 2401 2402 /* Make sure undefined weak symbols are output as a dynamic 2403 symbol in PIEs. */ 2404 else if (h->dynindx == -1 2405 && !h->forced_local) 2406 { 2407 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 2408 return FALSE; 2409 } 2410 } 2411 } 2412 else if (ELIMINATE_COPY_RELOCS) 2413 { 2414 /* For the non-shared case, discard space for relocs against 2415 symbols which turn out to need copy relocs or are not 2416 dynamic. */ 2417 2418 if (!h->non_got_ref 2419 && ((h->def_dynamic 2420 && !h->def_regular) 2421 || (htab->elf.dynamic_sections_created 2422 && (h->root.type == bfd_link_hash_undefweak 2423 || h->root.type == bfd_link_hash_undefined)))) 2424 { 2425 /* Make sure this symbol is output as a dynamic symbol. 2426 Undefined weak syms won't yet be marked as dynamic. */ 2427 if (h->dynindx == -1 2428 && !h->forced_local) 2429 { 2430 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 2431 return FALSE; 2432 } 2433 2434 /* If that succeeded, we know we'll be keeping all the 2435 relocs. */ 2436 if (h->dynindx != -1) 2437 goto keep; 2438 } 2439 2440 eh->dyn_relocs = NULL; 2441 2442 keep: ; 2443 } 2444 2445 /* Finally, allocate space. */ 2446 for (p = eh->dyn_relocs; p != NULL; p = p->next) 2447 { 2448 asection *sreloc; 2449 2450 sreloc = elf_section_data (p->sec)->sreloc; 2451 2452 BFD_ASSERT (sreloc != NULL); 2453 sreloc->size += p->count * sizeof (Elf32_External_Rel); 2454 } 2455 2456 return TRUE; 2457 } 2458 2459 /* Allocate space in .plt, .got and associated reloc sections for 2460 local dynamic relocs. */ 2461 2462 static bfd_boolean 2463 elf_i386_allocate_local_dynrelocs (void **slot, void *inf) 2464 { 2465 struct elf_link_hash_entry *h 2466 = (struct elf_link_hash_entry *) *slot; 2467 2468 if (h->type != STT_GNU_IFUNC 2469 || !h->def_regular 2470 || !h->ref_regular 2471 || !h->forced_local 2472 || h->root.type != bfd_link_hash_defined) 2473 abort (); 2474 2475 return elf_i386_allocate_dynrelocs (h, inf); 2476 } 2477 2478 /* Find any dynamic relocs that apply to read-only sections. */ 2479 2480 static bfd_boolean 2481 elf_i386_readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf) 2482 { 2483 struct elf_i386_link_hash_entry *eh; 2484 struct elf_dyn_relocs *p; 2485 2486 /* Skip local IFUNC symbols. */ 2487 if (h->forced_local && h->type == STT_GNU_IFUNC) 2488 return TRUE; 2489 2490 eh = (struct elf_i386_link_hash_entry *) h; 2491 for (p = eh->dyn_relocs; p != NULL; p = p->next) 2492 { 2493 asection *s = p->sec->output_section; 2494 2495 if (s != NULL && (s->flags & SEC_READONLY) != 0) 2496 { 2497 struct bfd_link_info *info = (struct bfd_link_info *) inf; 2498 2499 info->flags |= DF_TEXTREL; 2500 2501 if (info->warn_shared_textrel && info->shared) 2502 info->callbacks->einfo (_("%P: %B: warning: relocation against `%s' in readonly section `%A'.\n"), 2503 p->sec->owner, h->root.root.string, 2504 p->sec); 2505 2506 /* Not an error, just cut short the traversal. */ 2507 return FALSE; 2508 } 2509 } 2510 return TRUE; 2511 } 2512 2513 /* Convert 2514 mov foo@GOT(%reg), %reg 2515 to 2516 lea foo@GOTOFF(%reg), %reg 2517 with the local symbol, foo. */ 2518 2519 static bfd_boolean 2520 elf_i386_convert_mov_to_lea (bfd *abfd, asection *sec, 2521 struct bfd_link_info *link_info) 2522 { 2523 Elf_Internal_Shdr *symtab_hdr; 2524 Elf_Internal_Rela *internal_relocs; 2525 Elf_Internal_Rela *irel, *irelend; 2526 bfd_byte *contents; 2527 struct elf_i386_link_hash_table *htab; 2528 bfd_boolean changed_contents; 2529 bfd_boolean changed_relocs; 2530 bfd_signed_vma *local_got_refcounts; 2531 2532 /* Don't even try to convert non-ELF outputs. */ 2533 if (!is_elf_hash_table (link_info->hash)) 2534 return FALSE; 2535 2536 /* Nothing to do if there are no codes, no relocations or no output. */ 2537 if ((sec->flags & (SEC_CODE | SEC_RELOC)) != (SEC_CODE | SEC_RELOC) 2538 || sec->reloc_count == 0 2539 || bfd_is_abs_section (sec->output_section)) 2540 return TRUE; 2541 2542 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2543 2544 /* Load the relocations for this section. */ 2545 internal_relocs = (_bfd_elf_link_read_relocs 2546 (abfd, sec, NULL, (Elf_Internal_Rela *) NULL, 2547 link_info->keep_memory)); 2548 if (internal_relocs == NULL) 2549 return FALSE; 2550 2551 htab = elf_i386_hash_table (link_info); 2552 changed_contents = FALSE; 2553 changed_relocs = FALSE; 2554 local_got_refcounts = elf_local_got_refcounts (abfd); 2555 2556 /* Get the section contents. */ 2557 if (elf_section_data (sec)->this_hdr.contents != NULL) 2558 contents = elf_section_data (sec)->this_hdr.contents; 2559 else 2560 { 2561 if (!bfd_malloc_and_get_section (abfd, sec, &contents)) 2562 goto error_return; 2563 } 2564 2565 irelend = internal_relocs + sec->reloc_count; 2566 for (irel = internal_relocs; irel < irelend; irel++) 2567 { 2568 unsigned int r_type = ELF32_R_TYPE (irel->r_info); 2569 unsigned int r_symndx = ELF32_R_SYM (irel->r_info); 2570 unsigned int indx; 2571 struct elf_link_hash_entry *h; 2572 2573 if (r_type != R_386_GOT32) 2574 continue; 2575 2576 /* Get the symbol referred to by the reloc. */ 2577 if (r_symndx < symtab_hdr->sh_info) 2578 { 2579 Elf_Internal_Sym *isym; 2580 2581 isym = bfd_sym_from_r_symndx (&htab->sym_cache, 2582 abfd, r_symndx); 2583 2584 /* STT_GNU_IFUNC must keep R_386_GOT32 relocation. */ 2585 if (ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC 2586 && irel->r_offset >= 2 2587 && bfd_get_8 (input_bfd, 2588 contents + irel->r_offset - 2) == 0x8b) 2589 { 2590 bfd_put_8 (output_bfd, 0x8d, 2591 contents + irel->r_offset - 2); 2592 irel->r_info = ELF32_R_INFO (r_symndx, R_386_GOTOFF); 2593 if (local_got_refcounts != NULL 2594 && local_got_refcounts[r_symndx] > 0) 2595 local_got_refcounts[r_symndx] -= 1; 2596 changed_contents = TRUE; 2597 changed_relocs = TRUE; 2598 } 2599 continue; 2600 } 2601 2602 indx = r_symndx - symtab_hdr->sh_info; 2603 h = elf_sym_hashes (abfd)[indx]; 2604 BFD_ASSERT (h != NULL); 2605 2606 while (h->root.type == bfd_link_hash_indirect 2607 || h->root.type == bfd_link_hash_warning) 2608 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2609 2610 /* STT_GNU_IFUNC must keep R_386_GOT32 relocation. We also avoid 2611 optimizing _DYNAMIC since ld.so may use its link-time address. */ 2612 if (h->def_regular 2613 && h->type != STT_GNU_IFUNC 2614 && h != htab->elf.hdynamic 2615 && SYMBOL_REFERENCES_LOCAL (link_info, h) 2616 && irel->r_offset >= 2 2617 && bfd_get_8 (input_bfd, 2618 contents + irel->r_offset - 2) == 0x8b) 2619 { 2620 bfd_put_8 (output_bfd, 0x8d, 2621 contents + irel->r_offset - 2); 2622 irel->r_info = ELF32_R_INFO (r_symndx, R_386_GOTOFF); 2623 if (h->got.refcount > 0) 2624 h->got.refcount -= 1; 2625 changed_contents = TRUE; 2626 changed_relocs = TRUE; 2627 } 2628 } 2629 2630 if (contents != NULL 2631 && elf_section_data (sec)->this_hdr.contents != contents) 2632 { 2633 if (!changed_contents && !link_info->keep_memory) 2634 free (contents); 2635 else 2636 { 2637 /* Cache the section contents for elf_link_input_bfd. */ 2638 elf_section_data (sec)->this_hdr.contents = contents; 2639 } 2640 } 2641 2642 if (elf_section_data (sec)->relocs != internal_relocs) 2643 { 2644 if (!changed_relocs) 2645 free (internal_relocs); 2646 else 2647 elf_section_data (sec)->relocs = internal_relocs; 2648 } 2649 2650 return TRUE; 2651 2652 error_return: 2653 if (contents != NULL 2654 && elf_section_data (sec)->this_hdr.contents != contents) 2655 free (contents); 2656 if (internal_relocs != NULL 2657 && elf_section_data (sec)->relocs != internal_relocs) 2658 free (internal_relocs); 2659 return FALSE; 2660 } 2661 2662 /* Set the sizes of the dynamic sections. */ 2663 2664 static bfd_boolean 2665 elf_i386_size_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info) 2666 { 2667 struct elf_i386_link_hash_table *htab; 2668 bfd *dynobj; 2669 asection *s; 2670 bfd_boolean relocs; 2671 bfd *ibfd; 2672 2673 htab = elf_i386_hash_table (info); 2674 if (htab == NULL) 2675 return FALSE; 2676 dynobj = htab->elf.dynobj; 2677 if (dynobj == NULL) 2678 abort (); 2679 2680 if (htab->elf.dynamic_sections_created) 2681 { 2682 /* Set the contents of the .interp section to the interpreter. */ 2683 if (info->executable) 2684 { 2685 s = bfd_get_linker_section (dynobj, ".interp"); 2686 if (s == NULL) 2687 abort (); 2688 s->size = sizeof ELF_DYNAMIC_INTERPRETER; 2689 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; 2690 } 2691 } 2692 2693 /* Set up .got offsets for local syms, and space for local dynamic 2694 relocs. */ 2695 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 2696 { 2697 bfd_signed_vma *local_got; 2698 bfd_signed_vma *end_local_got; 2699 char *local_tls_type; 2700 bfd_vma *local_tlsdesc_gotent; 2701 bfd_size_type locsymcount; 2702 Elf_Internal_Shdr *symtab_hdr; 2703 asection *srel; 2704 2705 if (! is_i386_elf (ibfd)) 2706 continue; 2707 2708 for (s = ibfd->sections; s != NULL; s = s->next) 2709 { 2710 struct elf_dyn_relocs *p; 2711 2712 if (!elf_i386_convert_mov_to_lea (ibfd, s, info)) 2713 return FALSE; 2714 2715 for (p = ((struct elf_dyn_relocs *) 2716 elf_section_data (s)->local_dynrel); 2717 p != NULL; 2718 p = p->next) 2719 { 2720 if (!bfd_is_abs_section (p->sec) 2721 && bfd_is_abs_section (p->sec->output_section)) 2722 { 2723 /* Input section has been discarded, either because 2724 it is a copy of a linkonce section or due to 2725 linker script /DISCARD/, so we'll be discarding 2726 the relocs too. */ 2727 } 2728 else if (get_elf_i386_backend_data (output_bfd)->is_vxworks 2729 && strcmp (p->sec->output_section->name, 2730 ".tls_vars") == 0) 2731 { 2732 /* Relocations in vxworks .tls_vars sections are 2733 handled specially by the loader. */ 2734 } 2735 else if (p->count != 0) 2736 { 2737 srel = elf_section_data (p->sec)->sreloc; 2738 srel->size += p->count * sizeof (Elf32_External_Rel); 2739 if ((p->sec->output_section->flags & SEC_READONLY) != 0 2740 && (info->flags & DF_TEXTREL) == 0) 2741 { 2742 info->flags |= DF_TEXTREL; 2743 if (info->warn_shared_textrel && info->shared) 2744 info->callbacks->einfo (_("%P: %B: warning: relocation in readonly section `%A'.\n"), 2745 p->sec->owner, p->sec); 2746 } 2747 } 2748 } 2749 } 2750 2751 local_got = elf_local_got_refcounts (ibfd); 2752 if (!local_got) 2753 continue; 2754 2755 symtab_hdr = &elf_symtab_hdr (ibfd); 2756 locsymcount = symtab_hdr->sh_info; 2757 end_local_got = local_got + locsymcount; 2758 local_tls_type = elf_i386_local_got_tls_type (ibfd); 2759 local_tlsdesc_gotent = elf_i386_local_tlsdesc_gotent (ibfd); 2760 s = htab->elf.sgot; 2761 srel = htab->elf.srelgot; 2762 for (; local_got < end_local_got; 2763 ++local_got, ++local_tls_type, ++local_tlsdesc_gotent) 2764 { 2765 *local_tlsdesc_gotent = (bfd_vma) -1; 2766 if (*local_got > 0) 2767 { 2768 if (GOT_TLS_GDESC_P (*local_tls_type)) 2769 { 2770 *local_tlsdesc_gotent = htab->elf.sgotplt->size 2771 - elf_i386_compute_jump_table_size (htab); 2772 htab->elf.sgotplt->size += 8; 2773 *local_got = (bfd_vma) -2; 2774 } 2775 if (! GOT_TLS_GDESC_P (*local_tls_type) 2776 || GOT_TLS_GD_P (*local_tls_type)) 2777 { 2778 *local_got = s->size; 2779 s->size += 4; 2780 if (GOT_TLS_GD_P (*local_tls_type) 2781 || *local_tls_type == GOT_TLS_IE_BOTH) 2782 s->size += 4; 2783 } 2784 if (info->shared 2785 || GOT_TLS_GD_ANY_P (*local_tls_type) 2786 || (*local_tls_type & GOT_TLS_IE)) 2787 { 2788 if (*local_tls_type == GOT_TLS_IE_BOTH) 2789 srel->size += 2 * sizeof (Elf32_External_Rel); 2790 else if (GOT_TLS_GD_P (*local_tls_type) 2791 || ! GOT_TLS_GDESC_P (*local_tls_type)) 2792 srel->size += sizeof (Elf32_External_Rel); 2793 if (GOT_TLS_GDESC_P (*local_tls_type)) 2794 htab->elf.srelplt->size += sizeof (Elf32_External_Rel); 2795 } 2796 } 2797 else 2798 *local_got = (bfd_vma) -1; 2799 } 2800 } 2801 2802 if (htab->tls_ldm_got.refcount > 0) 2803 { 2804 /* Allocate 2 got entries and 1 dynamic reloc for R_386_TLS_LDM 2805 relocs. */ 2806 htab->tls_ldm_got.offset = htab->elf.sgot->size; 2807 htab->elf.sgot->size += 8; 2808 htab->elf.srelgot->size += sizeof (Elf32_External_Rel); 2809 } 2810 else 2811 htab->tls_ldm_got.offset = -1; 2812 2813 /* Allocate global sym .plt and .got entries, and space for global 2814 sym dynamic relocs. */ 2815 elf_link_hash_traverse (&htab->elf, elf_i386_allocate_dynrelocs, info); 2816 2817 /* Allocate .plt and .got entries, and space for local symbols. */ 2818 htab_traverse (htab->loc_hash_table, 2819 elf_i386_allocate_local_dynrelocs, 2820 info); 2821 2822 /* For every jump slot reserved in the sgotplt, reloc_count is 2823 incremented. However, when we reserve space for TLS descriptors, 2824 it's not incremented, so in order to compute the space reserved 2825 for them, it suffices to multiply the reloc count by the jump 2826 slot size. 2827 2828 PR ld/13302: We start next_irelative_index at the end of .rela.plt 2829 so that R_386_IRELATIVE entries come last. */ 2830 if (htab->elf.srelplt) 2831 { 2832 htab->next_tls_desc_index = htab->elf.srelplt->reloc_count; 2833 htab->sgotplt_jump_table_size = htab->next_tls_desc_index * 4; 2834 htab->next_irelative_index = htab->elf.srelplt->reloc_count - 1; 2835 } 2836 else if (htab->elf.irelplt) 2837 htab->next_irelative_index = htab->elf.irelplt->reloc_count - 1; 2838 2839 2840 if (htab->elf.sgotplt) 2841 { 2842 /* Don't allocate .got.plt section if there are no GOT nor PLT 2843 entries and there is no reference to _GLOBAL_OFFSET_TABLE_. */ 2844 if ((htab->elf.hgot == NULL 2845 || !htab->elf.hgot->ref_regular_nonweak) 2846 && (htab->elf.sgotplt->size 2847 == get_elf_backend_data (output_bfd)->got_header_size) 2848 && (htab->elf.splt == NULL 2849 || htab->elf.splt->size == 0) 2850 && (htab->elf.sgot == NULL 2851 || htab->elf.sgot->size == 0) 2852 && (htab->elf.iplt == NULL 2853 || htab->elf.iplt->size == 0) 2854 && (htab->elf.igotplt == NULL 2855 || htab->elf.igotplt->size == 0)) 2856 htab->elf.sgotplt->size = 0; 2857 } 2858 2859 2860 if (htab->plt_eh_frame != NULL 2861 && htab->elf.splt != NULL 2862 && htab->elf.splt->size != 0 2863 && !bfd_is_abs_section (htab->elf.splt->output_section) 2864 && _bfd_elf_eh_frame_present (info)) 2865 htab->plt_eh_frame->size = sizeof (elf_i386_eh_frame_plt); 2866 2867 /* We now have determined the sizes of the various dynamic sections. 2868 Allocate memory for them. */ 2869 relocs = FALSE; 2870 for (s = dynobj->sections; s != NULL; s = s->next) 2871 { 2872 bfd_boolean strip_section = TRUE; 2873 2874 if ((s->flags & SEC_LINKER_CREATED) == 0) 2875 continue; 2876 2877 if (s == htab->elf.splt 2878 || s == htab->elf.sgot) 2879 { 2880 /* Strip this section if we don't need it; see the 2881 comment below. */ 2882 /* We'd like to strip these sections if they aren't needed, but if 2883 we've exported dynamic symbols from them we must leave them. 2884 It's too late to tell BFD to get rid of the symbols. */ 2885 2886 if (htab->elf.hplt != NULL) 2887 strip_section = FALSE; 2888 } 2889 else if (s == htab->elf.sgotplt 2890 || s == htab->elf.iplt 2891 || s == htab->elf.igotplt 2892 || s == htab->plt_eh_frame 2893 || s == htab->sdynbss) 2894 { 2895 /* Strip these too. */ 2896 } 2897 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rel")) 2898 { 2899 if (s->size != 0 2900 && s != htab->elf.srelplt 2901 && s != htab->srelplt2) 2902 relocs = TRUE; 2903 2904 /* We use the reloc_count field as a counter if we need 2905 to copy relocs into the output file. */ 2906 s->reloc_count = 0; 2907 } 2908 else 2909 { 2910 /* It's not one of our sections, so don't allocate space. */ 2911 continue; 2912 } 2913 2914 if (s->size == 0) 2915 { 2916 /* If we don't need this section, strip it from the 2917 output file. This is mostly to handle .rel.bss and 2918 .rel.plt. We must create both sections in 2919 create_dynamic_sections, because they must be created 2920 before the linker maps input sections to output 2921 sections. The linker does that before 2922 adjust_dynamic_symbol is called, and it is that 2923 function which decides whether anything needs to go 2924 into these sections. */ 2925 if (strip_section) 2926 s->flags |= SEC_EXCLUDE; 2927 continue; 2928 } 2929 2930 if ((s->flags & SEC_HAS_CONTENTS) == 0) 2931 continue; 2932 2933 /* Allocate memory for the section contents. We use bfd_zalloc 2934 here in case unused entries are not reclaimed before the 2935 section's contents are written out. This should not happen, 2936 but this way if it does, we get a R_386_NONE reloc instead 2937 of garbage. */ 2938 s->contents = (unsigned char *) bfd_zalloc (dynobj, s->size); 2939 if (s->contents == NULL) 2940 return FALSE; 2941 } 2942 2943 if (htab->plt_eh_frame != NULL 2944 && htab->plt_eh_frame->contents != NULL) 2945 { 2946 memcpy (htab->plt_eh_frame->contents, elf_i386_eh_frame_plt, 2947 sizeof (elf_i386_eh_frame_plt)); 2948 bfd_put_32 (dynobj, htab->elf.splt->size, 2949 htab->plt_eh_frame->contents + PLT_FDE_LEN_OFFSET); 2950 } 2951 2952 if (htab->elf.dynamic_sections_created) 2953 { 2954 /* Add some entries to the .dynamic section. We fill in the 2955 values later, in elf_i386_finish_dynamic_sections, but we 2956 must add the entries now so that we get the correct size for 2957 the .dynamic section. The DT_DEBUG entry is filled in by the 2958 dynamic linker and used by the debugger. */ 2959 #define add_dynamic_entry(TAG, VAL) \ 2960 _bfd_elf_add_dynamic_entry (info, TAG, VAL) 2961 2962 if (info->executable) 2963 { 2964 if (!add_dynamic_entry (DT_DEBUG, 0)) 2965 return FALSE; 2966 } 2967 2968 if (htab->elf.splt->size != 0) 2969 { 2970 if (!add_dynamic_entry (DT_PLTGOT, 0) 2971 || !add_dynamic_entry (DT_PLTRELSZ, 0) 2972 || !add_dynamic_entry (DT_PLTREL, DT_REL) 2973 || !add_dynamic_entry (DT_JMPREL, 0)) 2974 return FALSE; 2975 } 2976 2977 if (relocs) 2978 { 2979 if (!add_dynamic_entry (DT_REL, 0) 2980 || !add_dynamic_entry (DT_RELSZ, 0) 2981 || !add_dynamic_entry (DT_RELENT, sizeof (Elf32_External_Rel))) 2982 return FALSE; 2983 2984 /* If any dynamic relocs apply to a read-only section, 2985 then we need a DT_TEXTREL entry. */ 2986 if ((info->flags & DF_TEXTREL) == 0) 2987 elf_link_hash_traverse (&htab->elf, 2988 elf_i386_readonly_dynrelocs, info); 2989 2990 if ((info->flags & DF_TEXTREL) != 0) 2991 { 2992 if (!add_dynamic_entry (DT_TEXTREL, 0)) 2993 return FALSE; 2994 } 2995 } 2996 if (get_elf_i386_backend_data (output_bfd)->is_vxworks 2997 && !elf_vxworks_add_dynamic_entries (output_bfd, info)) 2998 return FALSE; 2999 } 3000 #undef add_dynamic_entry 3001 3002 return TRUE; 3003 } 3004 3005 static bfd_boolean 3006 elf_i386_always_size_sections (bfd *output_bfd, 3007 struct bfd_link_info *info) 3008 { 3009 asection *tls_sec = elf_hash_table (info)->tls_sec; 3010 3011 if (tls_sec) 3012 { 3013 struct elf_link_hash_entry *tlsbase; 3014 3015 tlsbase = elf_link_hash_lookup (elf_hash_table (info), 3016 "_TLS_MODULE_BASE_", 3017 FALSE, FALSE, FALSE); 3018 3019 if (tlsbase && tlsbase->type == STT_TLS) 3020 { 3021 struct elf_i386_link_hash_table *htab; 3022 struct bfd_link_hash_entry *bh = NULL; 3023 const struct elf_backend_data *bed 3024 = get_elf_backend_data (output_bfd); 3025 3026 htab = elf_i386_hash_table (info); 3027 if (htab == NULL) 3028 return FALSE; 3029 3030 if (!(_bfd_generic_link_add_one_symbol 3031 (info, output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL, 3032 tls_sec, 0, NULL, FALSE, 3033 bed->collect, &bh))) 3034 return FALSE; 3035 3036 htab->tls_module_base = bh; 3037 3038 tlsbase = (struct elf_link_hash_entry *)bh; 3039 tlsbase->def_regular = 1; 3040 tlsbase->other = STV_HIDDEN; 3041 (*bed->elf_backend_hide_symbol) (info, tlsbase, TRUE); 3042 } 3043 } 3044 3045 return TRUE; 3046 } 3047 3048 /* Set the correct type for an x86 ELF section. We do this by the 3049 section name, which is a hack, but ought to work. */ 3050 3051 static bfd_boolean 3052 elf_i386_fake_sections (bfd *abfd ATTRIBUTE_UNUSED, 3053 Elf_Internal_Shdr *hdr, 3054 asection *sec) 3055 { 3056 const char *name; 3057 3058 name = bfd_get_section_name (abfd, sec); 3059 3060 /* This is an ugly, but unfortunately necessary hack that is 3061 needed when producing EFI binaries on x86. It tells 3062 elf.c:elf_fake_sections() not to consider ".reloc" as a section 3063 containing ELF relocation info. We need this hack in order to 3064 be able to generate ELF binaries that can be translated into 3065 EFI applications (which are essentially COFF objects). Those 3066 files contain a COFF ".reloc" section inside an ELFNN object, 3067 which would normally cause BFD to segfault because it would 3068 attempt to interpret this section as containing relocation 3069 entries for section "oc". With this hack enabled, ".reloc" 3070 will be treated as a normal data section, which will avoid the 3071 segfault. However, you won't be able to create an ELFNN binary 3072 with a section named "oc" that needs relocations, but that's 3073 the kind of ugly side-effects you get when detecting section 3074 types based on their names... In practice, this limitation is 3075 unlikely to bite. */ 3076 if (strcmp (name, ".reloc") == 0) 3077 hdr->sh_type = SHT_PROGBITS; 3078 3079 return TRUE; 3080 } 3081 3082 /* _TLS_MODULE_BASE_ needs to be treated especially when linking 3083 executables. Rather than setting it to the beginning of the TLS 3084 section, we have to set it to the end. This function may be called 3085 multiple times, it is idempotent. */ 3086 3087 static void 3088 elf_i386_set_tls_module_base (struct bfd_link_info *info) 3089 { 3090 struct elf_i386_link_hash_table *htab; 3091 struct bfd_link_hash_entry *base; 3092 3093 if (!info->executable) 3094 return; 3095 3096 htab = elf_i386_hash_table (info); 3097 if (htab == NULL) 3098 return; 3099 3100 base = htab->tls_module_base; 3101 if (base == NULL) 3102 return; 3103 3104 base->u.def.value = htab->elf.tls_size; 3105 } 3106 3107 /* Return the base VMA address which should be subtracted from real addresses 3108 when resolving @dtpoff relocation. 3109 This is PT_TLS segment p_vaddr. */ 3110 3111 static bfd_vma 3112 elf_i386_dtpoff_base (struct bfd_link_info *info) 3113 { 3114 /* If tls_sec is NULL, we should have signalled an error already. */ 3115 if (elf_hash_table (info)->tls_sec == NULL) 3116 return 0; 3117 return elf_hash_table (info)->tls_sec->vma; 3118 } 3119 3120 /* Return the relocation value for @tpoff relocation 3121 if STT_TLS virtual address is ADDRESS. */ 3122 3123 static bfd_vma 3124 elf_i386_tpoff (struct bfd_link_info *info, bfd_vma address) 3125 { 3126 struct elf_link_hash_table *htab = elf_hash_table (info); 3127 const struct elf_backend_data *bed = get_elf_backend_data (info->output_bfd); 3128 bfd_vma static_tls_size; 3129 3130 /* If tls_sec is NULL, we should have signalled an error already. */ 3131 if (htab->tls_sec == NULL) 3132 return 0; 3133 3134 /* Consider special static TLS alignment requirements. */ 3135 static_tls_size = BFD_ALIGN (htab->tls_size, bed->static_tls_alignment); 3136 return static_tls_size + htab->tls_sec->vma - address; 3137 } 3138 3139 /* Relocate an i386 ELF section. */ 3140 3141 static bfd_boolean 3142 elf_i386_relocate_section (bfd *output_bfd, 3143 struct bfd_link_info *info, 3144 bfd *input_bfd, 3145 asection *input_section, 3146 bfd_byte *contents, 3147 Elf_Internal_Rela *relocs, 3148 Elf_Internal_Sym *local_syms, 3149 asection **local_sections) 3150 { 3151 struct elf_i386_link_hash_table *htab; 3152 Elf_Internal_Shdr *symtab_hdr; 3153 struct elf_link_hash_entry **sym_hashes; 3154 bfd_vma *local_got_offsets; 3155 bfd_vma *local_tlsdesc_gotents; 3156 Elf_Internal_Rela *rel; 3157 Elf_Internal_Rela *relend; 3158 bfd_boolean is_vxworks_tls; 3159 unsigned plt_entry_size; 3160 3161 BFD_ASSERT (is_i386_elf (input_bfd)); 3162 3163 htab = elf_i386_hash_table (info); 3164 if (htab == NULL) 3165 return FALSE; 3166 symtab_hdr = &elf_symtab_hdr (input_bfd); 3167 sym_hashes = elf_sym_hashes (input_bfd); 3168 local_got_offsets = elf_local_got_offsets (input_bfd); 3169 local_tlsdesc_gotents = elf_i386_local_tlsdesc_gotent (input_bfd); 3170 /* We have to handle relocations in vxworks .tls_vars sections 3171 specially, because the dynamic loader is 'weird'. */ 3172 is_vxworks_tls = (get_elf_i386_backend_data (output_bfd)->is_vxworks 3173 && info->shared 3174 && !strcmp (input_section->output_section->name, 3175 ".tls_vars")); 3176 3177 elf_i386_set_tls_module_base (info); 3178 3179 plt_entry_size = GET_PLT_ENTRY_SIZE (output_bfd); 3180 3181 rel = relocs; 3182 relend = relocs + input_section->reloc_count; 3183 for (; rel < relend; rel++) 3184 { 3185 unsigned int r_type; 3186 reloc_howto_type *howto; 3187 unsigned long r_symndx; 3188 struct elf_link_hash_entry *h; 3189 Elf_Internal_Sym *sym; 3190 asection *sec; 3191 bfd_vma off, offplt; 3192 bfd_vma relocation; 3193 bfd_boolean unresolved_reloc; 3194 bfd_reloc_status_type r; 3195 unsigned int indx; 3196 int tls_type; 3197 bfd_vma st_size; 3198 3199 r_type = ELF32_R_TYPE (rel->r_info); 3200 if (r_type == R_386_GNU_VTINHERIT 3201 || r_type == R_386_GNU_VTENTRY) 3202 continue; 3203 3204 if ((indx = r_type) >= R_386_standard 3205 && ((indx = r_type - R_386_ext_offset) - R_386_standard 3206 >= R_386_ext - R_386_standard) 3207 && ((indx = r_type - R_386_tls_offset) - R_386_ext 3208 >= R_386_irelative - R_386_ext)) 3209 { 3210 (*_bfd_error_handler) 3211 (_("%B: unrecognized relocation (0x%x) in section `%A'"), 3212 input_bfd, input_section, r_type); 3213 bfd_set_error (bfd_error_bad_value); 3214 return FALSE; 3215 } 3216 howto = elf_howto_table + indx; 3217 3218 r_symndx = ELF32_R_SYM (rel->r_info); 3219 h = NULL; 3220 sym = NULL; 3221 sec = NULL; 3222 unresolved_reloc = FALSE; 3223 if (r_symndx < symtab_hdr->sh_info) 3224 { 3225 sym = local_syms + r_symndx; 3226 sec = local_sections[r_symndx]; 3227 relocation = (sec->output_section->vma 3228 + sec->output_offset 3229 + sym->st_value); 3230 st_size = sym->st_size; 3231 3232 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION 3233 && ((sec->flags & SEC_MERGE) != 0 3234 || (info->relocatable 3235 && sec->output_offset != 0))) 3236 { 3237 bfd_vma addend; 3238 bfd_byte *where = contents + rel->r_offset; 3239 3240 switch (howto->size) 3241 { 3242 case 0: 3243 addend = bfd_get_8 (input_bfd, where); 3244 if (howto->pc_relative) 3245 { 3246 addend = (addend ^ 0x80) - 0x80; 3247 addend += 1; 3248 } 3249 break; 3250 case 1: 3251 addend = bfd_get_16 (input_bfd, where); 3252 if (howto->pc_relative) 3253 { 3254 addend = (addend ^ 0x8000) - 0x8000; 3255 addend += 2; 3256 } 3257 break; 3258 case 2: 3259 addend = bfd_get_32 (input_bfd, where); 3260 if (howto->pc_relative) 3261 { 3262 addend = (addend ^ 0x80000000) - 0x80000000; 3263 addend += 4; 3264 } 3265 break; 3266 default: 3267 abort (); 3268 } 3269 3270 if (info->relocatable) 3271 addend += sec->output_offset; 3272 else 3273 { 3274 asection *msec = sec; 3275 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec, 3276 addend); 3277 addend -= relocation; 3278 addend += msec->output_section->vma + msec->output_offset; 3279 } 3280 3281 switch (howto->size) 3282 { 3283 case 0: 3284 /* FIXME: overflow checks. */ 3285 if (howto->pc_relative) 3286 addend -= 1; 3287 bfd_put_8 (input_bfd, addend, where); 3288 break; 3289 case 1: 3290 if (howto->pc_relative) 3291 addend -= 2; 3292 bfd_put_16 (input_bfd, addend, where); 3293 break; 3294 case 2: 3295 if (howto->pc_relative) 3296 addend -= 4; 3297 bfd_put_32 (input_bfd, addend, where); 3298 break; 3299 } 3300 } 3301 else if (!info->relocatable 3302 && ELF32_ST_TYPE (sym->st_info) == STT_GNU_IFUNC) 3303 { 3304 /* Relocate against local STT_GNU_IFUNC symbol. */ 3305 h = elf_i386_get_local_sym_hash (htab, input_bfd, rel, 3306 FALSE); 3307 if (h == NULL) 3308 abort (); 3309 3310 /* Set STT_GNU_IFUNC symbol value. */ 3311 h->root.u.def.value = sym->st_value; 3312 h->root.u.def.section = sec; 3313 } 3314 } 3315 else 3316 { 3317 bfd_boolean warned ATTRIBUTE_UNUSED; 3318 bfd_boolean ignored ATTRIBUTE_UNUSED; 3319 3320 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, 3321 r_symndx, symtab_hdr, sym_hashes, 3322 h, sec, relocation, 3323 unresolved_reloc, warned, ignored); 3324 st_size = h->size; 3325 } 3326 3327 if (sec != NULL && discarded_section (sec)) 3328 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section, 3329 rel, 1, relend, howto, 0, contents); 3330 3331 if (info->relocatable) 3332 continue; 3333 3334 /* Since STT_GNU_IFUNC symbol must go through PLT, we handle 3335 it here if it is defined in a non-shared object. */ 3336 if (h != NULL 3337 && h->type == STT_GNU_IFUNC 3338 && h->def_regular) 3339 { 3340 asection *plt, *gotplt, *base_got; 3341 bfd_vma plt_index; 3342 const char *name; 3343 3344 if ((input_section->flags & SEC_ALLOC) == 0 3345 || h->plt.offset == (bfd_vma) -1) 3346 abort (); 3347 3348 /* STT_GNU_IFUNC symbol must go through PLT. */ 3349 if (htab->elf.splt != NULL) 3350 { 3351 plt = htab->elf.splt; 3352 gotplt = htab->elf.sgotplt; 3353 } 3354 else 3355 { 3356 plt = htab->elf.iplt; 3357 gotplt = htab->elf.igotplt; 3358 } 3359 3360 relocation = (plt->output_section->vma 3361 + plt->output_offset + h->plt.offset); 3362 3363 switch (r_type) 3364 { 3365 default: 3366 if (h->root.root.string) 3367 name = h->root.root.string; 3368 else 3369 name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, 3370 NULL); 3371 (*_bfd_error_handler) 3372 (_("%B: relocation %s against STT_GNU_IFUNC " 3373 "symbol `%s' isn't handled by %s"), input_bfd, 3374 elf_howto_table[r_type].name, 3375 name, __FUNCTION__); 3376 bfd_set_error (bfd_error_bad_value); 3377 return FALSE; 3378 3379 case R_386_32: 3380 /* Generate dynamic relcoation only when there is a 3381 non-GOT reference in a shared object. */ 3382 if (info->shared && h->non_got_ref) 3383 { 3384 Elf_Internal_Rela outrel; 3385 asection *sreloc; 3386 bfd_vma offset; 3387 3388 /* Need a dynamic relocation to get the real function 3389 adddress. */ 3390 offset = _bfd_elf_section_offset (output_bfd, 3391 info, 3392 input_section, 3393 rel->r_offset); 3394 if (offset == (bfd_vma) -1 3395 || offset == (bfd_vma) -2) 3396 abort (); 3397 3398 outrel.r_offset = (input_section->output_section->vma 3399 + input_section->output_offset 3400 + offset); 3401 3402 if (h->dynindx == -1 3403 || h->forced_local 3404 || info->executable) 3405 { 3406 /* This symbol is resolved locally. */ 3407 outrel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE); 3408 bfd_put_32 (output_bfd, 3409 (h->root.u.def.value 3410 + h->root.u.def.section->output_section->vma 3411 + h->root.u.def.section->output_offset), 3412 contents + offset); 3413 } 3414 else 3415 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type); 3416 3417 sreloc = htab->elf.irelifunc; 3418 elf_append_rel (output_bfd, sreloc, &outrel); 3419 3420 /* If this reloc is against an external symbol, we 3421 do not want to fiddle with the addend. Otherwise, 3422 we need to include the symbol value so that it 3423 becomes an addend for the dynamic reloc. For an 3424 internal symbol, we have updated addend. */ 3425 continue; 3426 } 3427 /* FALLTHROUGH */ 3428 case R_386_PC32: 3429 case R_386_PLT32: 3430 goto do_relocation; 3431 3432 case R_386_GOT32: 3433 base_got = htab->elf.sgot; 3434 off = h->got.offset; 3435 3436 if (base_got == NULL) 3437 abort (); 3438 3439 if (off == (bfd_vma) -1) 3440 { 3441 /* We can't use h->got.offset here to save state, or 3442 even just remember the offset, as finish_dynamic_symbol 3443 would use that as offset into .got. */ 3444 3445 if (htab->elf.splt != NULL) 3446 { 3447 plt_index = h->plt.offset / plt_entry_size - 1; 3448 off = (plt_index + 3) * 4; 3449 base_got = htab->elf.sgotplt; 3450 } 3451 else 3452 { 3453 plt_index = h->plt.offset / plt_entry_size; 3454 off = plt_index * 4; 3455 base_got = htab->elf.igotplt; 3456 } 3457 3458 if (h->dynindx == -1 3459 || h->forced_local 3460 || info->symbolic) 3461 { 3462 /* This references the local defitionion. We must 3463 initialize this entry in the global offset table. 3464 Since the offset must always be a multiple of 8, 3465 we use the least significant bit to record 3466 whether we have initialized it already. 3467 3468 When doing a dynamic link, we create a .rela.got 3469 relocation entry to initialize the value. This 3470 is done in the finish_dynamic_symbol routine. */ 3471 if ((off & 1) != 0) 3472 off &= ~1; 3473 else 3474 { 3475 bfd_put_32 (output_bfd, relocation, 3476 base_got->contents + off); 3477 h->got.offset |= 1; 3478 } 3479 } 3480 3481 relocation = off; 3482 3483 /* Adjust for static executables. */ 3484 if (htab->elf.splt == NULL) 3485 relocation += gotplt->output_offset; 3486 } 3487 else 3488 { 3489 relocation = (base_got->output_section->vma 3490 + base_got->output_offset + off 3491 - gotplt->output_section->vma 3492 - gotplt->output_offset); 3493 /* Adjust for static executables. */ 3494 if (htab->elf.splt == NULL) 3495 relocation += gotplt->output_offset; 3496 } 3497 3498 goto do_relocation; 3499 3500 case R_386_GOTOFF: 3501 relocation -= (gotplt->output_section->vma 3502 + gotplt->output_offset); 3503 goto do_relocation; 3504 } 3505 } 3506 3507 switch (r_type) 3508 { 3509 case R_386_GOT32: 3510 /* Relocation is to the entry for this symbol in the global 3511 offset table. */ 3512 if (htab->elf.sgot == NULL) 3513 abort (); 3514 3515 if (h != NULL) 3516 { 3517 bfd_boolean dyn; 3518 3519 off = h->got.offset; 3520 dyn = htab->elf.dynamic_sections_created; 3521 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h) 3522 || (info->shared 3523 && SYMBOL_REFERENCES_LOCAL (info, h)) 3524 || (ELF_ST_VISIBILITY (h->other) 3525 && h->root.type == bfd_link_hash_undefweak)) 3526 { 3527 /* This is actually a static link, or it is a 3528 -Bsymbolic link and the symbol is defined 3529 locally, or the symbol was forced to be local 3530 because of a version file. We must initialize 3531 this entry in the global offset table. Since the 3532 offset must always be a multiple of 4, we use the 3533 least significant bit to record whether we have 3534 initialized it already. 3535 3536 When doing a dynamic link, we create a .rel.got 3537 relocation entry to initialize the value. This 3538 is done in the finish_dynamic_symbol routine. */ 3539 if ((off & 1) != 0) 3540 off &= ~1; 3541 else 3542 { 3543 bfd_put_32 (output_bfd, relocation, 3544 htab->elf.sgot->contents + off); 3545 h->got.offset |= 1; 3546 } 3547 } 3548 else 3549 unresolved_reloc = FALSE; 3550 } 3551 else 3552 { 3553 if (local_got_offsets == NULL) 3554 abort (); 3555 3556 off = local_got_offsets[r_symndx]; 3557 3558 /* The offset must always be a multiple of 4. We use 3559 the least significant bit to record whether we have 3560 already generated the necessary reloc. */ 3561 if ((off & 1) != 0) 3562 off &= ~1; 3563 else 3564 { 3565 bfd_put_32 (output_bfd, relocation, 3566 htab->elf.sgot->contents + off); 3567 3568 if (info->shared) 3569 { 3570 asection *s; 3571 Elf_Internal_Rela outrel; 3572 3573 s = htab->elf.srelgot; 3574 if (s == NULL) 3575 abort (); 3576 3577 outrel.r_offset = (htab->elf.sgot->output_section->vma 3578 + htab->elf.sgot->output_offset 3579 + off); 3580 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE); 3581 elf_append_rel (output_bfd, s, &outrel); 3582 } 3583 3584 local_got_offsets[r_symndx] |= 1; 3585 } 3586 } 3587 3588 if (off >= (bfd_vma) -2) 3589 abort (); 3590 3591 relocation = htab->elf.sgot->output_section->vma 3592 + htab->elf.sgot->output_offset + off 3593 - htab->elf.sgotplt->output_section->vma 3594 - htab->elf.sgotplt->output_offset; 3595 break; 3596 3597 case R_386_GOTOFF: 3598 /* Relocation is relative to the start of the global offset 3599 table. */ 3600 3601 /* Check to make sure it isn't a protected function symbol 3602 for shared library since it may not be local when used 3603 as function address. We also need to make sure that a 3604 symbol is defined locally. */ 3605 if (info->shared && h) 3606 { 3607 if (!h->def_regular) 3608 { 3609 const char *v; 3610 3611 switch (ELF_ST_VISIBILITY (h->other)) 3612 { 3613 case STV_HIDDEN: 3614 v = _("hidden symbol"); 3615 break; 3616 case STV_INTERNAL: 3617 v = _("internal symbol"); 3618 break; 3619 case STV_PROTECTED: 3620 v = _("protected symbol"); 3621 break; 3622 default: 3623 v = _("symbol"); 3624 break; 3625 } 3626 3627 (*_bfd_error_handler) 3628 (_("%B: relocation R_386_GOTOFF against undefined %s `%s' can not be used when making a shared object"), 3629 input_bfd, v, h->root.root.string); 3630 bfd_set_error (bfd_error_bad_value); 3631 return FALSE; 3632 } 3633 else if (!info->executable 3634 && !SYMBOLIC_BIND (info, h) 3635 && h->type == STT_FUNC 3636 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) 3637 { 3638 (*_bfd_error_handler) 3639 (_("%B: relocation R_386_GOTOFF against protected function `%s' can not be used when making a shared object"), 3640 input_bfd, h->root.root.string); 3641 bfd_set_error (bfd_error_bad_value); 3642 return FALSE; 3643 } 3644 } 3645 3646 /* Note that sgot is not involved in this 3647 calculation. We always want the start of .got.plt. If we 3648 defined _GLOBAL_OFFSET_TABLE_ in a different way, as is 3649 permitted by the ABI, we might have to change this 3650 calculation. */ 3651 relocation -= htab->elf.sgotplt->output_section->vma 3652 + htab->elf.sgotplt->output_offset; 3653 break; 3654 3655 case R_386_GOTPC: 3656 /* Use global offset table as symbol value. */ 3657 relocation = htab->elf.sgotplt->output_section->vma 3658 + htab->elf.sgotplt->output_offset; 3659 unresolved_reloc = FALSE; 3660 break; 3661 3662 case R_386_PLT32: 3663 /* Relocation is to the entry for this symbol in the 3664 procedure linkage table. */ 3665 3666 /* Resolve a PLT32 reloc against a local symbol directly, 3667 without using the procedure linkage table. */ 3668 if (h == NULL) 3669 break; 3670 3671 if (h->plt.offset == (bfd_vma) -1 3672 || htab->elf.splt == NULL) 3673 { 3674 /* We didn't make a PLT entry for this symbol. This 3675 happens when statically linking PIC code, or when 3676 using -Bsymbolic. */ 3677 break; 3678 } 3679 3680 relocation = (htab->elf.splt->output_section->vma 3681 + htab->elf.splt->output_offset 3682 + h->plt.offset); 3683 unresolved_reloc = FALSE; 3684 break; 3685 3686 case R_386_SIZE32: 3687 /* Set to symbol size. */ 3688 relocation = st_size; 3689 /* Fall through. */ 3690 3691 case R_386_32: 3692 case R_386_PC32: 3693 if ((input_section->flags & SEC_ALLOC) == 0 3694 || is_vxworks_tls) 3695 break; 3696 3697 if ((info->shared 3698 && (h == NULL 3699 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 3700 || h->root.type != bfd_link_hash_undefweak) 3701 && ((r_type != R_386_PC32 && r_type != R_386_SIZE32) 3702 || !SYMBOL_CALLS_LOCAL (info, h))) 3703 || (ELIMINATE_COPY_RELOCS 3704 && !info->shared 3705 && h != NULL 3706 && h->dynindx != -1 3707 && !h->non_got_ref 3708 && ((h->def_dynamic 3709 && !h->def_regular) 3710 || h->root.type == bfd_link_hash_undefweak 3711 || h->root.type == bfd_link_hash_undefined))) 3712 { 3713 Elf_Internal_Rela outrel; 3714 bfd_boolean skip, relocate; 3715 asection *sreloc; 3716 3717 /* When generating a shared object, these relocations 3718 are copied into the output file to be resolved at run 3719 time. */ 3720 3721 skip = FALSE; 3722 relocate = FALSE; 3723 3724 outrel.r_offset = 3725 _bfd_elf_section_offset (output_bfd, info, input_section, 3726 rel->r_offset); 3727 if (outrel.r_offset == (bfd_vma) -1) 3728 skip = TRUE; 3729 else if (outrel.r_offset == (bfd_vma) -2) 3730 skip = TRUE, relocate = TRUE; 3731 outrel.r_offset += (input_section->output_section->vma 3732 + input_section->output_offset); 3733 3734 if (skip) 3735 memset (&outrel, 0, sizeof outrel); 3736 else if (h != NULL 3737 && h->dynindx != -1 3738 && (r_type == R_386_PC32 3739 || !info->shared 3740 || !SYMBOLIC_BIND (info, h) 3741 || !h->def_regular)) 3742 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type); 3743 else 3744 { 3745 /* This symbol is local, or marked to become local. */ 3746 relocate = TRUE; 3747 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE); 3748 } 3749 3750 sreloc = elf_section_data (input_section)->sreloc; 3751 3752 if (sreloc == NULL || sreloc->contents == NULL) 3753 { 3754 r = bfd_reloc_notsupported; 3755 goto check_relocation_error; 3756 } 3757 3758 elf_append_rel (output_bfd, sreloc, &outrel); 3759 3760 /* If this reloc is against an external symbol, we do 3761 not want to fiddle with the addend. Otherwise, we 3762 need to include the symbol value so that it becomes 3763 an addend for the dynamic reloc. */ 3764 if (! relocate) 3765 continue; 3766 } 3767 break; 3768 3769 case R_386_TLS_IE: 3770 if (!info->executable) 3771 { 3772 Elf_Internal_Rela outrel; 3773 asection *sreloc; 3774 3775 outrel.r_offset = rel->r_offset 3776 + input_section->output_section->vma 3777 + input_section->output_offset; 3778 outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE); 3779 sreloc = elf_section_data (input_section)->sreloc; 3780 if (sreloc == NULL) 3781 abort (); 3782 elf_append_rel (output_bfd, sreloc, &outrel); 3783 } 3784 /* Fall through */ 3785 3786 case R_386_TLS_GD: 3787 case R_386_TLS_GOTDESC: 3788 case R_386_TLS_DESC_CALL: 3789 case R_386_TLS_IE_32: 3790 case R_386_TLS_GOTIE: 3791 tls_type = GOT_UNKNOWN; 3792 if (h == NULL && local_got_offsets) 3793 tls_type = elf_i386_local_got_tls_type (input_bfd) [r_symndx]; 3794 else if (h != NULL) 3795 tls_type = elf_i386_hash_entry(h)->tls_type; 3796 if (tls_type == GOT_TLS_IE) 3797 tls_type = GOT_TLS_IE_NEG; 3798 3799 if (! elf_i386_tls_transition (info, input_bfd, 3800 input_section, contents, 3801 symtab_hdr, sym_hashes, 3802 &r_type, tls_type, rel, 3803 relend, h, r_symndx)) 3804 return FALSE; 3805 3806 if (r_type == R_386_TLS_LE_32) 3807 { 3808 BFD_ASSERT (! unresolved_reloc); 3809 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD) 3810 { 3811 unsigned int type; 3812 bfd_vma roff; 3813 3814 /* GD->LE transition. */ 3815 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2); 3816 if (type == 0x04) 3817 { 3818 /* leal foo(,%reg,1), %eax; call ___tls_get_addr 3819 Change it into: 3820 movl %gs:0, %eax; subl $foo@tpoff, %eax 3821 (6 byte form of subl). */ 3822 memcpy (contents + rel->r_offset - 3, 3823 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12); 3824 roff = rel->r_offset + 5; 3825 } 3826 else 3827 { 3828 /* leal foo(%reg), %eax; call ___tls_get_addr; nop 3829 Change it into: 3830 movl %gs:0, %eax; subl $foo@tpoff, %eax 3831 (6 byte form of subl). */ 3832 memcpy (contents + rel->r_offset - 2, 3833 "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12); 3834 roff = rel->r_offset + 6; 3835 } 3836 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation), 3837 contents + roff); 3838 /* Skip R_386_PC32/R_386_PLT32. */ 3839 rel++; 3840 continue; 3841 } 3842 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC) 3843 { 3844 /* GDesc -> LE transition. 3845 It's originally something like: 3846 leal x@tlsdesc(%ebx), %eax 3847 3848 leal x@ntpoff, %eax 3849 3850 Registers other than %eax may be set up here. */ 3851 3852 unsigned int val; 3853 bfd_vma roff; 3854 3855 roff = rel->r_offset; 3856 val = bfd_get_8 (input_bfd, contents + roff - 1); 3857 3858 /* Now modify the instruction as appropriate. */ 3859 /* aoliva FIXME: remove the above and xor the byte 3860 below with 0x86. */ 3861 bfd_put_8 (output_bfd, val ^ 0x86, 3862 contents + roff - 1); 3863 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation), 3864 contents + roff); 3865 continue; 3866 } 3867 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL) 3868 { 3869 /* GDesc -> LE transition. 3870 It's originally: 3871 call *(%eax) 3872 Turn it into: 3873 xchg %ax,%ax */ 3874 3875 bfd_vma roff; 3876 3877 roff = rel->r_offset; 3878 bfd_put_8 (output_bfd, 0x66, contents + roff); 3879 bfd_put_8 (output_bfd, 0x90, contents + roff + 1); 3880 continue; 3881 } 3882 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_IE) 3883 { 3884 unsigned int val; 3885 3886 /* IE->LE transition: 3887 Originally it can be one of: 3888 movl foo, %eax 3889 movl foo, %reg 3890 addl foo, %reg 3891 We change it into: 3892 movl $foo, %eax 3893 movl $foo, %reg 3894 addl $foo, %reg. */ 3895 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1); 3896 if (val == 0xa1) 3897 { 3898 /* movl foo, %eax. */ 3899 bfd_put_8 (output_bfd, 0xb8, 3900 contents + rel->r_offset - 1); 3901 } 3902 else 3903 { 3904 unsigned int type; 3905 3906 type = bfd_get_8 (input_bfd, 3907 contents + rel->r_offset - 2); 3908 switch (type) 3909 { 3910 case 0x8b: 3911 /* movl */ 3912 bfd_put_8 (output_bfd, 0xc7, 3913 contents + rel->r_offset - 2); 3914 bfd_put_8 (output_bfd, 3915 0xc0 | ((val >> 3) & 7), 3916 contents + rel->r_offset - 1); 3917 break; 3918 case 0x03: 3919 /* addl */ 3920 bfd_put_8 (output_bfd, 0x81, 3921 contents + rel->r_offset - 2); 3922 bfd_put_8 (output_bfd, 3923 0xc0 | ((val >> 3) & 7), 3924 contents + rel->r_offset - 1); 3925 break; 3926 default: 3927 BFD_FAIL (); 3928 break; 3929 } 3930 } 3931 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation), 3932 contents + rel->r_offset); 3933 continue; 3934 } 3935 else 3936 { 3937 unsigned int val, type; 3938 3939 /* {IE_32,GOTIE}->LE transition: 3940 Originally it can be one of: 3941 subl foo(%reg1), %reg2 3942 movl foo(%reg1), %reg2 3943 addl foo(%reg1), %reg2 3944 We change it into: 3945 subl $foo, %reg2 3946 movl $foo, %reg2 (6 byte form) 3947 addl $foo, %reg2. */ 3948 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2); 3949 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1); 3950 if (type == 0x8b) 3951 { 3952 /* movl */ 3953 bfd_put_8 (output_bfd, 0xc7, 3954 contents + rel->r_offset - 2); 3955 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7), 3956 contents + rel->r_offset - 1); 3957 } 3958 else if (type == 0x2b) 3959 { 3960 /* subl */ 3961 bfd_put_8 (output_bfd, 0x81, 3962 contents + rel->r_offset - 2); 3963 bfd_put_8 (output_bfd, 0xe8 | ((val >> 3) & 7), 3964 contents + rel->r_offset - 1); 3965 } 3966 else if (type == 0x03) 3967 { 3968 /* addl */ 3969 bfd_put_8 (output_bfd, 0x81, 3970 contents + rel->r_offset - 2); 3971 bfd_put_8 (output_bfd, 0xc0 | ((val >> 3) & 7), 3972 contents + rel->r_offset - 1); 3973 } 3974 else 3975 BFD_FAIL (); 3976 if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTIE) 3977 bfd_put_32 (output_bfd, -elf_i386_tpoff (info, relocation), 3978 contents + rel->r_offset); 3979 else 3980 bfd_put_32 (output_bfd, elf_i386_tpoff (info, relocation), 3981 contents + rel->r_offset); 3982 continue; 3983 } 3984 } 3985 3986 if (htab->elf.sgot == NULL) 3987 abort (); 3988 3989 if (h != NULL) 3990 { 3991 off = h->got.offset; 3992 offplt = elf_i386_hash_entry (h)->tlsdesc_got; 3993 } 3994 else 3995 { 3996 if (local_got_offsets == NULL) 3997 abort (); 3998 3999 off = local_got_offsets[r_symndx]; 4000 offplt = local_tlsdesc_gotents[r_symndx]; 4001 } 4002 4003 if ((off & 1) != 0) 4004 off &= ~1; 4005 else 4006 { 4007 Elf_Internal_Rela outrel; 4008 int dr_type; 4009 asection *sreloc; 4010 4011 if (htab->elf.srelgot == NULL) 4012 abort (); 4013 4014 indx = h && h->dynindx != -1 ? h->dynindx : 0; 4015 4016 if (GOT_TLS_GDESC_P (tls_type)) 4017 { 4018 bfd_byte *loc; 4019 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_DESC); 4020 BFD_ASSERT (htab->sgotplt_jump_table_size + offplt + 8 4021 <= htab->elf.sgotplt->size); 4022 outrel.r_offset = (htab->elf.sgotplt->output_section->vma 4023 + htab->elf.sgotplt->output_offset 4024 + offplt 4025 + htab->sgotplt_jump_table_size); 4026 sreloc = htab->elf.srelplt; 4027 loc = sreloc->contents; 4028 loc += (htab->next_tls_desc_index++ 4029 * sizeof (Elf32_External_Rel)); 4030 BFD_ASSERT (loc + sizeof (Elf32_External_Rel) 4031 <= sreloc->contents + sreloc->size); 4032 bfd_elf32_swap_reloc_out (output_bfd, &outrel, loc); 4033 if (indx == 0) 4034 { 4035 BFD_ASSERT (! unresolved_reloc); 4036 bfd_put_32 (output_bfd, 4037 relocation - elf_i386_dtpoff_base (info), 4038 htab->elf.sgotplt->contents + offplt 4039 + htab->sgotplt_jump_table_size + 4); 4040 } 4041 else 4042 { 4043 bfd_put_32 (output_bfd, 0, 4044 htab->elf.sgotplt->contents + offplt 4045 + htab->sgotplt_jump_table_size + 4); 4046 } 4047 } 4048 4049 sreloc = htab->elf.srelgot; 4050 4051 outrel.r_offset = (htab->elf.sgot->output_section->vma 4052 + htab->elf.sgot->output_offset + off); 4053 4054 if (GOT_TLS_GD_P (tls_type)) 4055 dr_type = R_386_TLS_DTPMOD32; 4056 else if (GOT_TLS_GDESC_P (tls_type)) 4057 goto dr_done; 4058 else if (tls_type == GOT_TLS_IE_POS) 4059 dr_type = R_386_TLS_TPOFF; 4060 else 4061 dr_type = R_386_TLS_TPOFF32; 4062 4063 if (dr_type == R_386_TLS_TPOFF && indx == 0) 4064 bfd_put_32 (output_bfd, 4065 relocation - elf_i386_dtpoff_base (info), 4066 htab->elf.sgot->contents + off); 4067 else if (dr_type == R_386_TLS_TPOFF32 && indx == 0) 4068 bfd_put_32 (output_bfd, 4069 elf_i386_dtpoff_base (info) - relocation, 4070 htab->elf.sgot->contents + off); 4071 else if (dr_type != R_386_TLS_DESC) 4072 bfd_put_32 (output_bfd, 0, 4073 htab->elf.sgot->contents + off); 4074 outrel.r_info = ELF32_R_INFO (indx, dr_type); 4075 4076 elf_append_rel (output_bfd, sreloc, &outrel); 4077 4078 if (GOT_TLS_GD_P (tls_type)) 4079 { 4080 if (indx == 0) 4081 { 4082 BFD_ASSERT (! unresolved_reloc); 4083 bfd_put_32 (output_bfd, 4084 relocation - elf_i386_dtpoff_base (info), 4085 htab->elf.sgot->contents + off + 4); 4086 } 4087 else 4088 { 4089 bfd_put_32 (output_bfd, 0, 4090 htab->elf.sgot->contents + off + 4); 4091 outrel.r_info = ELF32_R_INFO (indx, 4092 R_386_TLS_DTPOFF32); 4093 outrel.r_offset += 4; 4094 elf_append_rel (output_bfd, sreloc, &outrel); 4095 } 4096 } 4097 else if (tls_type == GOT_TLS_IE_BOTH) 4098 { 4099 bfd_put_32 (output_bfd, 4100 (indx == 0 4101 ? relocation - elf_i386_dtpoff_base (info) 4102 : 0), 4103 htab->elf.sgot->contents + off + 4); 4104 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF); 4105 outrel.r_offset += 4; 4106 elf_append_rel (output_bfd, sreloc, &outrel); 4107 } 4108 4109 dr_done: 4110 if (h != NULL) 4111 h->got.offset |= 1; 4112 else 4113 local_got_offsets[r_symndx] |= 1; 4114 } 4115 4116 if (off >= (bfd_vma) -2 4117 && ! GOT_TLS_GDESC_P (tls_type)) 4118 abort (); 4119 if (r_type == R_386_TLS_GOTDESC 4120 || r_type == R_386_TLS_DESC_CALL) 4121 { 4122 relocation = htab->sgotplt_jump_table_size + offplt; 4123 unresolved_reloc = FALSE; 4124 } 4125 else if (r_type == ELF32_R_TYPE (rel->r_info)) 4126 { 4127 bfd_vma g_o_t = htab->elf.sgotplt->output_section->vma 4128 + htab->elf.sgotplt->output_offset; 4129 relocation = htab->elf.sgot->output_section->vma 4130 + htab->elf.sgot->output_offset + off - g_o_t; 4131 if ((r_type == R_386_TLS_IE || r_type == R_386_TLS_GOTIE) 4132 && tls_type == GOT_TLS_IE_BOTH) 4133 relocation += 4; 4134 if (r_type == R_386_TLS_IE) 4135 relocation += g_o_t; 4136 unresolved_reloc = FALSE; 4137 } 4138 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GD) 4139 { 4140 unsigned int val, type; 4141 bfd_vma roff; 4142 4143 /* GD->IE transition. */ 4144 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2); 4145 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 1); 4146 if (type == 0x04) 4147 { 4148 /* leal foo(,%reg,1), %eax; call ___tls_get_addr 4149 Change it into: 4150 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */ 4151 val >>= 3; 4152 roff = rel->r_offset - 3; 4153 } 4154 else 4155 { 4156 /* leal foo(%reg), %eax; call ___tls_get_addr; nop 4157 Change it into: 4158 movl %gs:0, %eax; subl $foo@gottpoff(%reg), %eax. */ 4159 roff = rel->r_offset - 2; 4160 } 4161 memcpy (contents + roff, 4162 "\x65\xa1\0\0\0\0\x2b\x80\0\0\0", 12); 4163 contents[roff + 7] = 0x80 | (val & 7); 4164 /* If foo is used only with foo@gotntpoff(%reg) and 4165 foo@indntpoff, but not with foo@gottpoff(%reg), change 4166 subl $foo@gottpoff(%reg), %eax 4167 into: 4168 addl $foo@gotntpoff(%reg), %eax. */ 4169 if (tls_type == GOT_TLS_IE_POS) 4170 contents[roff + 6] = 0x03; 4171 bfd_put_32 (output_bfd, 4172 htab->elf.sgot->output_section->vma 4173 + htab->elf.sgot->output_offset + off 4174 - htab->elf.sgotplt->output_section->vma 4175 - htab->elf.sgotplt->output_offset, 4176 contents + roff + 8); 4177 /* Skip R_386_PLT32. */ 4178 rel++; 4179 continue; 4180 } 4181 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_GOTDESC) 4182 { 4183 /* GDesc -> IE transition. 4184 It's originally something like: 4185 leal x@tlsdesc(%ebx), %eax 4186 4187 Change it to: 4188 movl x@gotntpoff(%ebx), %eax # before xchg %ax,%ax 4189 or: 4190 movl x@gottpoff(%ebx), %eax # before negl %eax 4191 4192 Registers other than %eax may be set up here. */ 4193 4194 bfd_vma roff; 4195 4196 /* First, make sure it's a leal adding ebx to a 32-bit 4197 offset into any register, although it's probably 4198 almost always going to be eax. */ 4199 roff = rel->r_offset; 4200 4201 /* Now modify the instruction as appropriate. */ 4202 /* To turn a leal into a movl in the form we use it, it 4203 suffices to change the first byte from 0x8d to 0x8b. 4204 aoliva FIXME: should we decide to keep the leal, all 4205 we have to do is remove the statement below, and 4206 adjust the relaxation of R_386_TLS_DESC_CALL. */ 4207 bfd_put_8 (output_bfd, 0x8b, contents + roff - 2); 4208 4209 if (tls_type == GOT_TLS_IE_BOTH) 4210 off += 4; 4211 4212 bfd_put_32 (output_bfd, 4213 htab->elf.sgot->output_section->vma 4214 + htab->elf.sgot->output_offset + off 4215 - htab->elf.sgotplt->output_section->vma 4216 - htab->elf.sgotplt->output_offset, 4217 contents + roff); 4218 continue; 4219 } 4220 else if (ELF32_R_TYPE (rel->r_info) == R_386_TLS_DESC_CALL) 4221 { 4222 /* GDesc -> IE transition. 4223 It's originally: 4224 call *(%eax) 4225 4226 Change it to: 4227 xchg %ax,%ax 4228 or 4229 negl %eax 4230 depending on how we transformed the TLS_GOTDESC above. 4231 */ 4232 4233 bfd_vma roff; 4234 4235 roff = rel->r_offset; 4236 4237 /* Now modify the instruction as appropriate. */ 4238 if (tls_type != GOT_TLS_IE_NEG) 4239 { 4240 /* xchg %ax,%ax */ 4241 bfd_put_8 (output_bfd, 0x66, contents + roff); 4242 bfd_put_8 (output_bfd, 0x90, contents + roff + 1); 4243 } 4244 else 4245 { 4246 /* negl %eax */ 4247 bfd_put_8 (output_bfd, 0xf7, contents + roff); 4248 bfd_put_8 (output_bfd, 0xd8, contents + roff + 1); 4249 } 4250 4251 continue; 4252 } 4253 else 4254 BFD_ASSERT (FALSE); 4255 break; 4256 4257 case R_386_TLS_LDM: 4258 if (! elf_i386_tls_transition (info, input_bfd, 4259 input_section, contents, 4260 symtab_hdr, sym_hashes, 4261 &r_type, GOT_UNKNOWN, rel, 4262 relend, h, r_symndx)) 4263 return FALSE; 4264 4265 if (r_type != R_386_TLS_LDM) 4266 { 4267 /* LD->LE transition: 4268 leal foo(%reg), %eax; call ___tls_get_addr. 4269 We change it into: 4270 movl %gs:0, %eax; nop; leal 0(%esi,1), %esi. */ 4271 BFD_ASSERT (r_type == R_386_TLS_LE_32); 4272 memcpy (contents + rel->r_offset - 2, 4273 "\x65\xa1\0\0\0\0\x90\x8d\x74\x26", 11); 4274 /* Skip R_386_PC32/R_386_PLT32. */ 4275 rel++; 4276 continue; 4277 } 4278 4279 if (htab->elf.sgot == NULL) 4280 abort (); 4281 4282 off = htab->tls_ldm_got.offset; 4283 if (off & 1) 4284 off &= ~1; 4285 else 4286 { 4287 Elf_Internal_Rela outrel; 4288 4289 if (htab->elf.srelgot == NULL) 4290 abort (); 4291 4292 outrel.r_offset = (htab->elf.sgot->output_section->vma 4293 + htab->elf.sgot->output_offset + off); 4294 4295 bfd_put_32 (output_bfd, 0, 4296 htab->elf.sgot->contents + off); 4297 bfd_put_32 (output_bfd, 0, 4298 htab->elf.sgot->contents + off + 4); 4299 outrel.r_info = ELF32_R_INFO (0, R_386_TLS_DTPMOD32); 4300 elf_append_rel (output_bfd, htab->elf.srelgot, &outrel); 4301 htab->tls_ldm_got.offset |= 1; 4302 } 4303 relocation = htab->elf.sgot->output_section->vma 4304 + htab->elf.sgot->output_offset + off 4305 - htab->elf.sgotplt->output_section->vma 4306 - htab->elf.sgotplt->output_offset; 4307 unresolved_reloc = FALSE; 4308 break; 4309 4310 case R_386_TLS_LDO_32: 4311 if (!info->executable || (input_section->flags & SEC_CODE) == 0) 4312 relocation -= elf_i386_dtpoff_base (info); 4313 else 4314 /* When converting LDO to LE, we must negate. */ 4315 relocation = -elf_i386_tpoff (info, relocation); 4316 break; 4317 4318 case R_386_TLS_LE_32: 4319 case R_386_TLS_LE: 4320 if (!info->executable) 4321 { 4322 Elf_Internal_Rela outrel; 4323 asection *sreloc; 4324 4325 outrel.r_offset = rel->r_offset 4326 + input_section->output_section->vma 4327 + input_section->output_offset; 4328 if (h != NULL && h->dynindx != -1) 4329 indx = h->dynindx; 4330 else 4331 indx = 0; 4332 if (r_type == R_386_TLS_LE_32) 4333 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF32); 4334 else 4335 outrel.r_info = ELF32_R_INFO (indx, R_386_TLS_TPOFF); 4336 sreloc = elf_section_data (input_section)->sreloc; 4337 if (sreloc == NULL) 4338 abort (); 4339 elf_append_rel (output_bfd, sreloc, &outrel); 4340 if (indx) 4341 continue; 4342 else if (r_type == R_386_TLS_LE_32) 4343 relocation = elf_i386_dtpoff_base (info) - relocation; 4344 else 4345 relocation -= elf_i386_dtpoff_base (info); 4346 } 4347 else if (r_type == R_386_TLS_LE_32) 4348 relocation = elf_i386_tpoff (info, relocation); 4349 else 4350 relocation = -elf_i386_tpoff (info, relocation); 4351 break; 4352 4353 default: 4354 break; 4355 } 4356 4357 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections 4358 because such sections are not SEC_ALLOC and thus ld.so will 4359 not process them. */ 4360 if (unresolved_reloc 4361 && !((input_section->flags & SEC_DEBUGGING) != 0 4362 && h->def_dynamic) 4363 && _bfd_elf_section_offset (output_bfd, info, input_section, 4364 rel->r_offset) != (bfd_vma) -1) 4365 { 4366 (*_bfd_error_handler) 4367 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"), 4368 input_bfd, 4369 input_section, 4370 (long) rel->r_offset, 4371 howto->name, 4372 h->root.root.string); 4373 return FALSE; 4374 } 4375 4376 do_relocation: 4377 r = _bfd_final_link_relocate (howto, input_bfd, input_section, 4378 contents, rel->r_offset, 4379 relocation, 0); 4380 4381 check_relocation_error: 4382 if (r != bfd_reloc_ok) 4383 { 4384 const char *name; 4385 4386 if (h != NULL) 4387 name = h->root.root.string; 4388 else 4389 { 4390 name = bfd_elf_string_from_elf_section (input_bfd, 4391 symtab_hdr->sh_link, 4392 sym->st_name); 4393 if (name == NULL) 4394 return FALSE; 4395 if (*name == '\0') 4396 name = bfd_section_name (input_bfd, sec); 4397 } 4398 4399 if (r == bfd_reloc_overflow) 4400 { 4401 if (! ((*info->callbacks->reloc_overflow) 4402 (info, (h ? &h->root : NULL), name, howto->name, 4403 (bfd_vma) 0, input_bfd, input_section, 4404 rel->r_offset))) 4405 return FALSE; 4406 } 4407 else 4408 { 4409 (*_bfd_error_handler) 4410 (_("%B(%A+0x%lx): reloc against `%s': error %d"), 4411 input_bfd, input_section, 4412 (long) rel->r_offset, name, (int) r); 4413 return FALSE; 4414 } 4415 } 4416 } 4417 4418 return TRUE; 4419 } 4420 4421 /* Finish up dynamic symbol handling. We set the contents of various 4422 dynamic sections here. */ 4423 4424 static bfd_boolean 4425 elf_i386_finish_dynamic_symbol (bfd *output_bfd, 4426 struct bfd_link_info *info, 4427 struct elf_link_hash_entry *h, 4428 Elf_Internal_Sym *sym) 4429 { 4430 struct elf_i386_link_hash_table *htab; 4431 unsigned plt_entry_size; 4432 const struct elf_i386_backend_data *abed; 4433 4434 htab = elf_i386_hash_table (info); 4435 if (htab == NULL) 4436 return FALSE; 4437 4438 abed = get_elf_i386_backend_data (output_bfd); 4439 plt_entry_size = GET_PLT_ENTRY_SIZE (output_bfd); 4440 4441 if (h->plt.offset != (bfd_vma) -1) 4442 { 4443 bfd_vma plt_index; 4444 bfd_vma got_offset; 4445 Elf_Internal_Rela rel; 4446 bfd_byte *loc; 4447 asection *plt, *gotplt, *relplt; 4448 4449 /* When building a static executable, use .iplt, .igot.plt and 4450 .rel.iplt sections for STT_GNU_IFUNC symbols. */ 4451 if (htab->elf.splt != NULL) 4452 { 4453 plt = htab->elf.splt; 4454 gotplt = htab->elf.sgotplt; 4455 relplt = htab->elf.srelplt; 4456 } 4457 else 4458 { 4459 plt = htab->elf.iplt; 4460 gotplt = htab->elf.igotplt; 4461 relplt = htab->elf.irelplt; 4462 } 4463 4464 /* This symbol has an entry in the procedure linkage table. Set 4465 it up. */ 4466 4467 if ((h->dynindx == -1 4468 && !((h->forced_local || info->executable) 4469 && h->def_regular 4470 && h->type == STT_GNU_IFUNC)) 4471 || plt == NULL 4472 || gotplt == NULL 4473 || relplt == NULL) 4474 abort (); 4475 4476 /* Get the index in the procedure linkage table which 4477 corresponds to this symbol. This is the index of this symbol 4478 in all the symbols for which we are making plt entries. The 4479 first entry in the procedure linkage table is reserved. 4480 4481 Get the offset into the .got table of the entry that 4482 corresponds to this function. Each .got entry is 4 bytes. 4483 The first three are reserved. 4484 4485 For static executables, we don't reserve anything. */ 4486 4487 if (plt == htab->elf.splt) 4488 { 4489 got_offset = h->plt.offset / plt_entry_size - 1; 4490 got_offset = (got_offset + 3) * 4; 4491 } 4492 else 4493 { 4494 got_offset = h->plt.offset / plt_entry_size; 4495 got_offset = got_offset * 4; 4496 } 4497 4498 /* Fill in the entry in the procedure linkage table. */ 4499 if (! info->shared) 4500 { 4501 memcpy (plt->contents + h->plt.offset, abed->plt->plt_entry, 4502 abed->plt->plt_entry_size); 4503 bfd_put_32 (output_bfd, 4504 (gotplt->output_section->vma 4505 + gotplt->output_offset 4506 + got_offset), 4507 plt->contents + h->plt.offset 4508 + abed->plt->plt_got_offset); 4509 4510 if (abed->is_vxworks) 4511 { 4512 int s, k, reloc_index; 4513 4514 /* Create the R_386_32 relocation referencing the GOT 4515 for this PLT entry. */ 4516 4517 /* S: Current slot number (zero-based). */ 4518 s = ((h->plt.offset - abed->plt->plt_entry_size) 4519 / abed->plt->plt_entry_size); 4520 /* K: Number of relocations for PLTResolve. */ 4521 if (info->shared) 4522 k = PLTRESOLVE_RELOCS_SHLIB; 4523 else 4524 k = PLTRESOLVE_RELOCS; 4525 /* Skip the PLTresolve relocations, and the relocations for 4526 the other PLT slots. */ 4527 reloc_index = k + s * PLT_NON_JUMP_SLOT_RELOCS; 4528 loc = (htab->srelplt2->contents + reloc_index 4529 * sizeof (Elf32_External_Rel)); 4530 4531 rel.r_offset = (htab->elf.splt->output_section->vma 4532 + htab->elf.splt->output_offset 4533 + h->plt.offset + 2), 4534 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32); 4535 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc); 4536 4537 /* Create the R_386_32 relocation referencing the beginning of 4538 the PLT for this GOT entry. */ 4539 rel.r_offset = (htab->elf.sgotplt->output_section->vma 4540 + htab->elf.sgotplt->output_offset 4541 + got_offset); 4542 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32); 4543 bfd_elf32_swap_reloc_out (output_bfd, &rel, 4544 loc + sizeof (Elf32_External_Rel)); 4545 } 4546 } 4547 else 4548 { 4549 memcpy (plt->contents + h->plt.offset, abed->plt->pic_plt_entry, 4550 abed->plt->plt_entry_size); 4551 bfd_put_32 (output_bfd, got_offset, 4552 plt->contents + h->plt.offset 4553 + abed->plt->plt_got_offset); 4554 } 4555 4556 /* Fill in the entry in the global offset table. */ 4557 bfd_put_32 (output_bfd, 4558 (plt->output_section->vma 4559 + plt->output_offset 4560 + h->plt.offset 4561 + abed->plt->plt_lazy_offset), 4562 gotplt->contents + got_offset); 4563 4564 /* Fill in the entry in the .rel.plt section. */ 4565 rel.r_offset = (gotplt->output_section->vma 4566 + gotplt->output_offset 4567 + got_offset); 4568 if (h->dynindx == -1 4569 || ((info->executable 4570 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) 4571 && h->def_regular 4572 && h->type == STT_GNU_IFUNC)) 4573 { 4574 /* If an STT_GNU_IFUNC symbol is locally defined, generate 4575 R_386_IRELATIVE instead of R_386_JUMP_SLOT. Store addend 4576 in the .got.plt section. */ 4577 bfd_put_32 (output_bfd, 4578 (h->root.u.def.value 4579 + h->root.u.def.section->output_section->vma 4580 + h->root.u.def.section->output_offset), 4581 gotplt->contents + got_offset); 4582 rel.r_info = ELF32_R_INFO (0, R_386_IRELATIVE); 4583 /* R_386_IRELATIVE comes last. */ 4584 plt_index = htab->next_irelative_index--; 4585 } 4586 else 4587 { 4588 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT); 4589 plt_index = htab->next_jump_slot_index++; 4590 } 4591 loc = relplt->contents + plt_index * sizeof (Elf32_External_Rel); 4592 bfd_elf32_swap_reloc_out (output_bfd, &rel, loc); 4593 4594 /* Don't fill PLT entry for static executables. */ 4595 if (plt == htab->elf.splt) 4596 { 4597 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel), 4598 plt->contents + h->plt.offset 4599 + abed->plt->plt_reloc_offset); 4600 bfd_put_32 (output_bfd, - (h->plt.offset 4601 + abed->plt->plt_plt_offset + 4), 4602 plt->contents + h->plt.offset 4603 + abed->plt->plt_plt_offset); 4604 } 4605 4606 if (!h->def_regular) 4607 { 4608 /* Mark the symbol as undefined, rather than as defined in 4609 the .plt section. Leave the value if there were any 4610 relocations where pointer equality matters (this is a clue 4611 for the dynamic linker, to make function pointer 4612 comparisons work between an application and shared 4613 library), otherwise set it to zero. If a function is only 4614 called from a binary, there is no need to slow down 4615 shared libraries because of that. */ 4616 sym->st_shndx = SHN_UNDEF; 4617 if (!h->pointer_equality_needed) 4618 sym->st_value = 0; 4619 } 4620 } 4621 4622 if (h->got.offset != (bfd_vma) -1 4623 && ! GOT_TLS_GD_ANY_P (elf_i386_hash_entry(h)->tls_type) 4624 && (elf_i386_hash_entry(h)->tls_type & GOT_TLS_IE) == 0) 4625 { 4626 Elf_Internal_Rela rel; 4627 4628 /* This symbol has an entry in the global offset table. Set it 4629 up. */ 4630 4631 if (htab->elf.sgot == NULL || htab->elf.srelgot == NULL) 4632 abort (); 4633 4634 rel.r_offset = (htab->elf.sgot->output_section->vma 4635 + htab->elf.sgot->output_offset 4636 + (h->got.offset & ~(bfd_vma) 1)); 4637 4638 /* If this is a static link, or it is a -Bsymbolic link and the 4639 symbol is defined locally or was forced to be local because 4640 of a version file, we just want to emit a RELATIVE reloc. 4641 The entry in the global offset table will already have been 4642 initialized in the relocate_section function. */ 4643 if (h->def_regular 4644 && h->type == STT_GNU_IFUNC) 4645 { 4646 if (info->shared) 4647 { 4648 /* Generate R_386_GLOB_DAT. */ 4649 goto do_glob_dat; 4650 } 4651 else 4652 { 4653 asection *plt; 4654 4655 if (!h->pointer_equality_needed) 4656 abort (); 4657 4658 /* For non-shared object, we can't use .got.plt, which 4659 contains the real function addres if we need pointer 4660 equality. We load the GOT entry with the PLT entry. */ 4661 plt = htab->elf.splt ? htab->elf.splt : htab->elf.iplt; 4662 bfd_put_32 (output_bfd, 4663 (plt->output_section->vma 4664 + plt->output_offset + h->plt.offset), 4665 htab->elf.sgot->contents + h->got.offset); 4666 return TRUE; 4667 } 4668 } 4669 else if (info->shared 4670 && SYMBOL_REFERENCES_LOCAL (info, h)) 4671 { 4672 BFD_ASSERT((h->got.offset & 1) != 0); 4673 rel.r_info = ELF32_R_INFO (0, R_386_RELATIVE); 4674 } 4675 else 4676 { 4677 BFD_ASSERT((h->got.offset & 1) == 0); 4678 do_glob_dat: 4679 bfd_put_32 (output_bfd, (bfd_vma) 0, 4680 htab->elf.sgot->contents + h->got.offset); 4681 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT); 4682 } 4683 4684 elf_append_rel (output_bfd, htab->elf.srelgot, &rel); 4685 } 4686 4687 if (h->needs_copy) 4688 { 4689 Elf_Internal_Rela rel; 4690 4691 /* This symbol needs a copy reloc. Set it up. */ 4692 4693 if (h->dynindx == -1 4694 || (h->root.type != bfd_link_hash_defined 4695 && h->root.type != bfd_link_hash_defweak) 4696 || htab->srelbss == NULL) 4697 abort (); 4698 4699 rel.r_offset = (h->root.u.def.value 4700 + h->root.u.def.section->output_section->vma 4701 + h->root.u.def.section->output_offset); 4702 rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY); 4703 elf_append_rel (output_bfd, htab->srelbss, &rel); 4704 } 4705 4706 return TRUE; 4707 } 4708 4709 /* Finish up local dynamic symbol handling. We set the contents of 4710 various dynamic sections here. */ 4711 4712 static bfd_boolean 4713 elf_i386_finish_local_dynamic_symbol (void **slot, void *inf) 4714 { 4715 struct elf_link_hash_entry *h 4716 = (struct elf_link_hash_entry *) *slot; 4717 struct bfd_link_info *info 4718 = (struct bfd_link_info *) inf; 4719 4720 return elf_i386_finish_dynamic_symbol (info->output_bfd, info, 4721 h, NULL); 4722 } 4723 4724 /* Used to decide how to sort relocs in an optimal manner for the 4725 dynamic linker, before writing them out. */ 4726 4727 static enum elf_reloc_type_class 4728 elf_i386_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, 4729 const asection *rel_sec ATTRIBUTE_UNUSED, 4730 const Elf_Internal_Rela *rela) 4731 { 4732 switch (ELF32_R_TYPE (rela->r_info)) 4733 { 4734 case R_386_RELATIVE: 4735 return reloc_class_relative; 4736 case R_386_JUMP_SLOT: 4737 return reloc_class_plt; 4738 case R_386_COPY: 4739 return reloc_class_copy; 4740 default: 4741 return reloc_class_normal; 4742 } 4743 } 4744 4745 /* Finish up the dynamic sections. */ 4746 4747 static bfd_boolean 4748 elf_i386_finish_dynamic_sections (bfd *output_bfd, 4749 struct bfd_link_info *info) 4750 { 4751 struct elf_i386_link_hash_table *htab; 4752 bfd *dynobj; 4753 asection *sdyn; 4754 const struct elf_i386_backend_data *abed; 4755 4756 htab = elf_i386_hash_table (info); 4757 if (htab == NULL) 4758 return FALSE; 4759 4760 dynobj = htab->elf.dynobj; 4761 sdyn = bfd_get_linker_section (dynobj, ".dynamic"); 4762 abed = get_elf_i386_backend_data (output_bfd); 4763 4764 if (htab->elf.dynamic_sections_created) 4765 { 4766 Elf32_External_Dyn *dyncon, *dynconend; 4767 4768 if (sdyn == NULL || htab->elf.sgot == NULL) 4769 abort (); 4770 4771 dyncon = (Elf32_External_Dyn *) sdyn->contents; 4772 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size); 4773 for (; dyncon < dynconend; dyncon++) 4774 { 4775 Elf_Internal_Dyn dyn; 4776 asection *s; 4777 4778 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn); 4779 4780 switch (dyn.d_tag) 4781 { 4782 default: 4783 if (abed->is_vxworks 4784 && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn)) 4785 break; 4786 continue; 4787 4788 case DT_PLTGOT: 4789 s = htab->elf.sgotplt; 4790 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset; 4791 break; 4792 4793 case DT_JMPREL: 4794 s = htab->elf.srelplt; 4795 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset; 4796 break; 4797 4798 case DT_PLTRELSZ: 4799 s = htab->elf.srelplt; 4800 dyn.d_un.d_val = s->size; 4801 break; 4802 4803 case DT_RELSZ: 4804 /* My reading of the SVR4 ABI indicates that the 4805 procedure linkage table relocs (DT_JMPREL) should be 4806 included in the overall relocs (DT_REL). This is 4807 what Solaris does. However, UnixWare can not handle 4808 that case. Therefore, we override the DT_RELSZ entry 4809 here to make it not include the JMPREL relocs. */ 4810 s = htab->elf.srelplt; 4811 if (s == NULL) 4812 continue; 4813 dyn.d_un.d_val -= s->size; 4814 break; 4815 4816 case DT_REL: 4817 /* We may not be using the standard ELF linker script. 4818 If .rel.plt is the first .rel section, we adjust 4819 DT_REL to not include it. */ 4820 s = htab->elf.srelplt; 4821 if (s == NULL) 4822 continue; 4823 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset) 4824 continue; 4825 dyn.d_un.d_ptr += s->size; 4826 break; 4827 } 4828 4829 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 4830 } 4831 4832 /* Fill in the first entry in the procedure linkage table. */ 4833 if (htab->elf.splt && htab->elf.splt->size > 0) 4834 { 4835 if (info->shared) 4836 { 4837 memcpy (htab->elf.splt->contents, abed->plt->pic_plt0_entry, 4838 abed->plt->plt0_entry_size); 4839 memset (htab->elf.splt->contents + abed->plt->plt0_entry_size, 4840 abed->plt0_pad_byte, 4841 abed->plt->plt_entry_size - abed->plt->plt0_entry_size); 4842 } 4843 else 4844 { 4845 memcpy (htab->elf.splt->contents, abed->plt->plt0_entry, 4846 abed->plt->plt0_entry_size); 4847 memset (htab->elf.splt->contents + abed->plt->plt0_entry_size, 4848 abed->plt0_pad_byte, 4849 abed->plt->plt_entry_size - abed->plt->plt0_entry_size); 4850 bfd_put_32 (output_bfd, 4851 (htab->elf.sgotplt->output_section->vma 4852 + htab->elf.sgotplt->output_offset 4853 + 4), 4854 htab->elf.splt->contents 4855 + abed->plt->plt0_got1_offset); 4856 bfd_put_32 (output_bfd, 4857 (htab->elf.sgotplt->output_section->vma 4858 + htab->elf.sgotplt->output_offset 4859 + 8), 4860 htab->elf.splt->contents 4861 + abed->plt->plt0_got2_offset); 4862 4863 if (abed->is_vxworks) 4864 { 4865 Elf_Internal_Rela rel; 4866 4867 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 4. 4868 On IA32 we use REL relocations so the addend goes in 4869 the PLT directly. */ 4870 rel.r_offset = (htab->elf.splt->output_section->vma 4871 + htab->elf.splt->output_offset 4872 + abed->plt->plt0_got1_offset); 4873 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32); 4874 bfd_elf32_swap_reloc_out (output_bfd, &rel, 4875 htab->srelplt2->contents); 4876 /* Generate a relocation for _GLOBAL_OFFSET_TABLE_ + 8. */ 4877 rel.r_offset = (htab->elf.splt->output_section->vma 4878 + htab->elf.splt->output_offset 4879 + abed->plt->plt0_got2_offset); 4880 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32); 4881 bfd_elf32_swap_reloc_out (output_bfd, &rel, 4882 htab->srelplt2->contents + 4883 sizeof (Elf32_External_Rel)); 4884 } 4885 } 4886 4887 /* UnixWare sets the entsize of .plt to 4, although that doesn't 4888 really seem like the right value. */ 4889 elf_section_data (htab->elf.splt->output_section) 4890 ->this_hdr.sh_entsize = 4; 4891 4892 /* Correct the .rel.plt.unloaded relocations. */ 4893 if (abed->is_vxworks && !info->shared) 4894 { 4895 int num_plts = (htab->elf.splt->size 4896 / abed->plt->plt_entry_size) - 1; 4897 unsigned char *p; 4898 4899 p = htab->srelplt2->contents; 4900 if (info->shared) 4901 p += PLTRESOLVE_RELOCS_SHLIB * sizeof (Elf32_External_Rel); 4902 else 4903 p += PLTRESOLVE_RELOCS * sizeof (Elf32_External_Rel); 4904 4905 for (; num_plts; num_plts--) 4906 { 4907 Elf_Internal_Rela rel; 4908 bfd_elf32_swap_reloc_in (output_bfd, p, &rel); 4909 rel.r_info = ELF32_R_INFO (htab->elf.hgot->indx, R_386_32); 4910 bfd_elf32_swap_reloc_out (output_bfd, &rel, p); 4911 p += sizeof (Elf32_External_Rel); 4912 4913 bfd_elf32_swap_reloc_in (output_bfd, p, &rel); 4914 rel.r_info = ELF32_R_INFO (htab->elf.hplt->indx, R_386_32); 4915 bfd_elf32_swap_reloc_out (output_bfd, &rel, p); 4916 p += sizeof (Elf32_External_Rel); 4917 } 4918 } 4919 } 4920 } 4921 4922 if (htab->elf.sgotplt) 4923 { 4924 if (bfd_is_abs_section (htab->elf.sgotplt->output_section)) 4925 { 4926 (*_bfd_error_handler) 4927 (_("discarded output section: `%A'"), htab->elf.sgotplt); 4928 return FALSE; 4929 } 4930 4931 /* Fill in the first three entries in the global offset table. */ 4932 if (htab->elf.sgotplt->size > 0) 4933 { 4934 bfd_put_32 (output_bfd, 4935 (sdyn == NULL ? 0 4936 : sdyn->output_section->vma + sdyn->output_offset), 4937 htab->elf.sgotplt->contents); 4938 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 4); 4939 bfd_put_32 (output_bfd, 0, htab->elf.sgotplt->contents + 8); 4940 } 4941 4942 elf_section_data (htab->elf.sgotplt->output_section)->this_hdr.sh_entsize = 4; 4943 } 4944 4945 /* Adjust .eh_frame for .plt section. */ 4946 if (htab->plt_eh_frame != NULL 4947 && htab->plt_eh_frame->contents != NULL) 4948 { 4949 if (htab->elf.splt != NULL 4950 && htab->elf.splt->size != 0 4951 && (htab->elf.splt->flags & SEC_EXCLUDE) == 0 4952 && htab->elf.splt->output_section != NULL 4953 && htab->plt_eh_frame->output_section != NULL) 4954 { 4955 bfd_vma plt_start = htab->elf.splt->output_section->vma; 4956 bfd_vma eh_frame_start = htab->plt_eh_frame->output_section->vma 4957 + htab->plt_eh_frame->output_offset 4958 + PLT_FDE_START_OFFSET; 4959 bfd_put_signed_32 (dynobj, plt_start - eh_frame_start, 4960 htab->plt_eh_frame->contents 4961 + PLT_FDE_START_OFFSET); 4962 } 4963 if (htab->plt_eh_frame->sec_info_type 4964 == SEC_INFO_TYPE_EH_FRAME) 4965 { 4966 if (! _bfd_elf_write_section_eh_frame (output_bfd, info, 4967 htab->plt_eh_frame, 4968 htab->plt_eh_frame->contents)) 4969 return FALSE; 4970 } 4971 } 4972 4973 if (htab->elf.sgot && htab->elf.sgot->size > 0) 4974 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 4; 4975 4976 /* Fill PLT and GOT entries for local STT_GNU_IFUNC symbols. */ 4977 htab_traverse (htab->loc_hash_table, 4978 elf_i386_finish_local_dynamic_symbol, 4979 info); 4980 4981 return TRUE; 4982 } 4983 4984 /* Return address in section PLT for the Ith GOTPLT relocation, for 4985 relocation REL or (bfd_vma) -1 if it should not be included. */ 4986 4987 static bfd_vma 4988 elf_i386_plt_sym_val (bfd_vma i, const asection *plt, const arelent *rel) 4989 { 4990 bfd *abfd; 4991 const struct elf_i386_backend_data *bed; 4992 bfd_vma plt_offset; 4993 4994 /* Only match R_386_JUMP_SLOT and R_386_IRELATIVE. */ 4995 if (rel->howto->type != R_386_JUMP_SLOT 4996 && rel->howto->type != R_386_IRELATIVE) 4997 return (bfd_vma) -1; 4998 4999 abfd = plt->owner; 5000 bed = get_elf_i386_backend_data (abfd); 5001 plt_offset = bed->plt->plt_entry_size; 5002 5003 if (elf_elfheader (abfd)->e_ident[EI_OSABI] != ELFOSABI_GNU) 5004 return plt->vma + (i + 1) * plt_offset; 5005 5006 while (plt_offset < plt->size) 5007 { 5008 bfd_vma reloc_offset; 5009 bfd_byte reloc_offset_raw[4]; 5010 5011 if (!bfd_get_section_contents (abfd, (asection *) plt, 5012 reloc_offset_raw, 5013 plt_offset + bed->plt->plt_reloc_offset, 5014 sizeof (reloc_offset_raw))) 5015 return (bfd_vma) -1; 5016 5017 reloc_offset = H_GET_32 (abfd, reloc_offset_raw); 5018 if (reloc_offset == i * sizeof (Elf32_External_Rel)) 5019 return plt->vma + plt_offset; 5020 plt_offset += bed->plt->plt_entry_size; 5021 } 5022 5023 abort (); 5024 } 5025 5026 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ 5027 5028 static bfd_boolean 5029 elf_i386_hash_symbol (struct elf_link_hash_entry *h) 5030 { 5031 if (h->plt.offset != (bfd_vma) -1 5032 && !h->def_regular 5033 && !h->pointer_equality_needed) 5034 return FALSE; 5035 5036 return _bfd_elf_hash_symbol (h); 5037 } 5038 5039 /* Hook called by the linker routine which adds symbols from an object 5040 file. */ 5041 5042 static bfd_boolean 5043 elf_i386_add_symbol_hook (bfd * abfd, 5044 struct bfd_link_info * info, 5045 Elf_Internal_Sym * sym, 5046 const char ** namep ATTRIBUTE_UNUSED, 5047 flagword * flagsp ATTRIBUTE_UNUSED, 5048 asection ** secp ATTRIBUTE_UNUSED, 5049 bfd_vma * valp ATTRIBUTE_UNUSED) 5050 { 5051 if ((ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC 5052 || ELF_ST_BIND (sym->st_info) == STB_GNU_UNIQUE) 5053 && (abfd->flags & DYNAMIC) == 0 5054 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour) 5055 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE; 5056 5057 return TRUE; 5058 } 5059 5060 #define TARGET_LITTLE_SYM i386_elf32_vec 5061 #define TARGET_LITTLE_NAME "elf32-i386" 5062 #define ELF_ARCH bfd_arch_i386 5063 #define ELF_TARGET_ID I386_ELF_DATA 5064 #define ELF_MACHINE_CODE EM_386 5065 #define ELF_MAXPAGESIZE 0x1000 5066 5067 #define elf_backend_can_gc_sections 1 5068 #define elf_backend_can_refcount 1 5069 #define elf_backend_want_got_plt 1 5070 #define elf_backend_plt_readonly 1 5071 #define elf_backend_want_plt_sym 0 5072 #define elf_backend_got_header_size 12 5073 #define elf_backend_plt_alignment 4 5074 5075 /* Support RELA for objdump of prelink objects. */ 5076 #define elf_info_to_howto elf_i386_info_to_howto_rel 5077 #define elf_info_to_howto_rel elf_i386_info_to_howto_rel 5078 5079 #define bfd_elf32_mkobject elf_i386_mkobject 5080 5081 #define bfd_elf32_bfd_is_local_label_name elf_i386_is_local_label_name 5082 #define bfd_elf32_bfd_link_hash_table_create elf_i386_link_hash_table_create 5083 #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup 5084 #define bfd_elf32_bfd_reloc_name_lookup elf_i386_reloc_name_lookup 5085 5086 #define elf_backend_adjust_dynamic_symbol elf_i386_adjust_dynamic_symbol 5087 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible 5088 #define elf_backend_check_relocs elf_i386_check_relocs 5089 #define elf_backend_copy_indirect_symbol elf_i386_copy_indirect_symbol 5090 #define elf_backend_create_dynamic_sections elf_i386_create_dynamic_sections 5091 #define elf_backend_fake_sections elf_i386_fake_sections 5092 #define elf_backend_finish_dynamic_sections elf_i386_finish_dynamic_sections 5093 #define elf_backend_finish_dynamic_symbol elf_i386_finish_dynamic_symbol 5094 #define elf_backend_gc_mark_hook elf_i386_gc_mark_hook 5095 #define elf_backend_gc_sweep_hook elf_i386_gc_sweep_hook 5096 #define elf_backend_grok_prstatus elf_i386_grok_prstatus 5097 #define elf_backend_grok_psinfo elf_i386_grok_psinfo 5098 #define elf_backend_reloc_type_class elf_i386_reloc_type_class 5099 #define elf_backend_relocate_section elf_i386_relocate_section 5100 #define elf_backend_size_dynamic_sections elf_i386_size_dynamic_sections 5101 #define elf_backend_always_size_sections elf_i386_always_size_sections 5102 #define elf_backend_omit_section_dynsym \ 5103 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true) 5104 #define elf_backend_plt_sym_val elf_i386_plt_sym_val 5105 #define elf_backend_hash_symbol elf_i386_hash_symbol 5106 #define elf_backend_add_symbol_hook elf_i386_add_symbol_hook 5107 5108 #include "elf32-target.h" 5109 5110 /* FreeBSD support. */ 5111 5112 #undef TARGET_LITTLE_SYM 5113 #define TARGET_LITTLE_SYM i386_elf32_fbsd_vec 5114 #undef TARGET_LITTLE_NAME 5115 #define TARGET_LITTLE_NAME "elf32-i386-freebsd" 5116 #undef ELF_OSABI 5117 #define ELF_OSABI ELFOSABI_FREEBSD 5118 5119 /* The kernel recognizes executables as valid only if they carry a 5120 "FreeBSD" label in the ELF header. So we put this label on all 5121 executables and (for simplicity) also all other object files. */ 5122 5123 static void 5124 elf_i386_fbsd_post_process_headers (bfd *abfd, struct bfd_link_info *info) 5125 { 5126 _bfd_elf_post_process_headers (abfd, info); 5127 5128 #ifdef OLD_FREEBSD_ABI_LABEL 5129 { 5130 /* The ABI label supported by FreeBSD <= 4.0 is quite nonstandard. */ 5131 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); 5132 memcpy (&i_ehdrp->e_ident[EI_ABIVERSION], "FreeBSD", 8); 5133 } 5134 #endif 5135 } 5136 5137 #undef elf_backend_post_process_headers 5138 #define elf_backend_post_process_headers elf_i386_fbsd_post_process_headers 5139 #undef elf32_bed 5140 #define elf32_bed elf32_i386_fbsd_bed 5141 5142 #undef elf_backend_add_symbol_hook 5143 5144 #include "elf32-target.h" 5145 5146 /* Solaris 2. */ 5147 5148 #undef TARGET_LITTLE_SYM 5149 #define TARGET_LITTLE_SYM i386_elf32_sol2_vec 5150 #undef TARGET_LITTLE_NAME 5151 #define TARGET_LITTLE_NAME "elf32-i386-sol2" 5152 5153 /* Restore default: we cannot use ELFOSABI_SOLARIS, otherwise ELFOSABI_NONE 5154 objects won't be recognized. */ 5155 #undef ELF_OSABI 5156 5157 #undef elf32_bed 5158 #define elf32_bed elf32_i386_sol2_bed 5159 5160 /* The 32-bit static TLS arena size is rounded to the nearest 8-byte 5161 boundary. */ 5162 #undef elf_backend_static_tls_alignment 5163 #define elf_backend_static_tls_alignment 8 5164 5165 /* The Solaris 2 ABI requires a plt symbol on all platforms. 5166 5167 Cf. Linker and Libraries Guide, Ch. 2, Link-Editor, Generating the Output 5168 File, p.63. */ 5169 #undef elf_backend_want_plt_sym 5170 #define elf_backend_want_plt_sym 1 5171 5172 #include "elf32-target.h" 5173 5174 /* Native Client support. */ 5175 5176 #undef TARGET_LITTLE_SYM 5177 #define TARGET_LITTLE_SYM i386_elf32_nacl_vec 5178 #undef TARGET_LITTLE_NAME 5179 #define TARGET_LITTLE_NAME "elf32-i386-nacl" 5180 #undef elf32_bed 5181 #define elf32_bed elf32_i386_nacl_bed 5182 5183 #undef ELF_MAXPAGESIZE 5184 #define ELF_MAXPAGESIZE 0x10000 5185 5186 /* Restore defaults. */ 5187 #undef ELF_OSABI 5188 #undef elf_backend_want_plt_sym 5189 #define elf_backend_want_plt_sym 0 5190 #undef elf_backend_post_process_headers 5191 #undef elf_backend_static_tls_alignment 5192 5193 /* NaCl uses substantially different PLT entries for the same effects. */ 5194 5195 #undef elf_backend_plt_alignment 5196 #define elf_backend_plt_alignment 5 5197 #define NACL_PLT_ENTRY_SIZE 64 5198 #define NACLMASK 0xe0 /* 32-byte alignment mask. */ 5199 5200 static const bfd_byte elf_i386_nacl_plt0_entry[] = 5201 { 5202 0xff, 0x35, /* pushl contents of address */ 5203 0, 0, 0, 0, /* replaced with address of .got + 4. */ 5204 0x8b, 0x0d, /* movl contents of address, %ecx */ 5205 0, 0, 0, 0, /* replaced with address of .got + 8. */ 5206 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */ 5207 0xff, 0xe1 /* jmp *%ecx */ 5208 }; 5209 5210 static const bfd_byte elf_i386_nacl_plt_entry[NACL_PLT_ENTRY_SIZE] = 5211 { 5212 0x8b, 0x0d, /* movl contents of address, %ecx */ 5213 0, 0, 0, 0, /* replaced with GOT slot address. */ 5214 0x83, 0xe1, NACLMASK, /* andl $NACLMASK, %ecx */ 5215 0xff, 0xe1, /* jmp *%ecx */ 5216 5217 /* Pad to the next 32-byte boundary with nop instructions. */ 5218 0x90, 5219 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 5220 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 5221 5222 /* Lazy GOT entries point here (32-byte aligned). */ 5223 0x68, /* pushl immediate */ 5224 0, 0, 0, 0, /* replaced with reloc offset. */ 5225 0xe9, /* jmp relative */ 5226 0, 0, 0, 0, /* replaced with offset to .plt. */ 5227 5228 /* Pad to the next 32-byte boundary with nop instructions. */ 5229 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 5230 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 5231 0x90, 0x90 5232 }; 5233 5234 static const bfd_byte 5235 elf_i386_nacl_pic_plt0_entry[sizeof (elf_i386_nacl_plt0_entry)] = 5236 { 5237 0xff, 0x73, 0x04, /* pushl 4(%ebx) */ 5238 0x8b, 0x4b, 0x08, /* mov 0x8(%ebx), %ecx */ 5239 0x83, 0xe1, 0xe0, /* and $NACLMASK, %ecx */ 5240 0xff, 0xe1, /* jmp *%ecx */ 5241 5242 /* This is expected to be the same size as elf_i386_nacl_plt0_entry, 5243 so pad to that size with nop instructions. */ 5244 0x90, 0x90, 0x90, 0x90, 0x90, 0x90 5245 }; 5246 5247 static const bfd_byte elf_i386_nacl_pic_plt_entry[NACL_PLT_ENTRY_SIZE] = 5248 { 5249 0x8b, 0x8b, /* movl offset(%ebx), %ecx */ 5250 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */ 5251 0x83, 0xe1, 0xe0, /* andl $NACLMASK, %ecx */ 5252 0xff, 0xe1, /* jmp *%ecx */ 5253 5254 /* Pad to the next 32-byte boundary with nop instructions. */ 5255 0x90, 5256 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 5257 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 5258 5259 /* Lazy GOT entries point here (32-byte aligned). */ 5260 0x68, /* pushl immediate */ 5261 0, 0, 0, 0, /* replaced with offset into relocation table. */ 5262 0xe9, /* jmp relative */ 5263 0, 0, 0, 0, /* replaced with offset to start of .plt. */ 5264 5265 /* Pad to the next 32-byte boundary with nop instructions. */ 5266 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 5267 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 5268 0x90, 0x90 5269 }; 5270 5271 static const bfd_byte elf_i386_nacl_eh_frame_plt[] = 5272 { 5273 #if (PLT_CIE_LENGTH != 20 \ 5274 || PLT_FDE_LENGTH != 36 \ 5275 || PLT_FDE_START_OFFSET != 4 + PLT_CIE_LENGTH + 8 \ 5276 || PLT_FDE_LEN_OFFSET != 4 + PLT_CIE_LENGTH + 12) 5277 # error "Need elf_i386_backend_data parameters for eh_frame_plt offsets!" 5278 #endif 5279 PLT_CIE_LENGTH, 0, 0, 0, /* CIE length */ 5280 0, 0, 0, 0, /* CIE ID */ 5281 1, /* CIE version */ 5282 'z', 'R', 0, /* Augmentation string */ 5283 1, /* Code alignment factor */ 5284 0x7c, /* Data alignment factor: -4 */ 5285 8, /* Return address column */ 5286 1, /* Augmentation size */ 5287 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding */ 5288 DW_CFA_def_cfa, 4, 4, /* DW_CFA_def_cfa: r4 (esp) ofs 4 */ 5289 DW_CFA_offset + 8, 1, /* DW_CFA_offset: r8 (eip) at cfa-4 */ 5290 DW_CFA_nop, DW_CFA_nop, 5291 5292 PLT_FDE_LENGTH, 0, 0, 0, /* FDE length */ 5293 PLT_CIE_LENGTH + 8, 0, 0, 0, /* CIE pointer */ 5294 0, 0, 0, 0, /* R_386_PC32 .plt goes here */ 5295 0, 0, 0, 0, /* .plt size goes here */ 5296 0, /* Augmentation size */ 5297 DW_CFA_def_cfa_offset, 8, /* DW_CFA_def_cfa_offset: 8 */ 5298 DW_CFA_advance_loc + 6, /* DW_CFA_advance_loc: 6 to __PLT__+6 */ 5299 DW_CFA_def_cfa_offset, 12, /* DW_CFA_def_cfa_offset: 12 */ 5300 DW_CFA_advance_loc + 58, /* DW_CFA_advance_loc: 58 to __PLT__+64 */ 5301 DW_CFA_def_cfa_expression, /* DW_CFA_def_cfa_expression */ 5302 13, /* Block length */ 5303 DW_OP_breg4, 4, /* DW_OP_breg4 (esp): 4 */ 5304 DW_OP_breg8, 0, /* DW_OP_breg8 (eip): 0 */ 5305 DW_OP_const1u, 63, DW_OP_and, DW_OP_const1u, 37, DW_OP_ge, 5306 DW_OP_lit2, DW_OP_shl, DW_OP_plus, 5307 DW_CFA_nop, DW_CFA_nop 5308 }; 5309 5310 static const struct elf_i386_plt_layout elf_i386_nacl_plt = 5311 { 5312 elf_i386_nacl_plt0_entry, /* plt0_entry */ 5313 sizeof (elf_i386_nacl_plt0_entry), /* plt0_entry_size */ 5314 2, /* plt0_got1_offset */ 5315 8, /* plt0_got2_offset */ 5316 elf_i386_nacl_plt_entry, /* plt_entry */ 5317 NACL_PLT_ENTRY_SIZE, /* plt_entry_size */ 5318 2, /* plt_got_offset */ 5319 33, /* plt_reloc_offset */ 5320 38, /* plt_plt_offset */ 5321 32, /* plt_lazy_offset */ 5322 elf_i386_nacl_pic_plt0_entry, /* pic_plt0_entry */ 5323 elf_i386_nacl_pic_plt_entry, /* pic_plt_entry */ 5324 elf_i386_nacl_eh_frame_plt, /* eh_frame_plt */ 5325 sizeof (elf_i386_nacl_eh_frame_plt),/* eh_frame_plt_size */ 5326 }; 5327 5328 static const struct elf_i386_backend_data elf_i386_nacl_arch_bed = 5329 { 5330 &elf_i386_nacl_plt, /* plt */ 5331 0x90, /* plt0_pad_byte: nop insn */ 5332 0, /* is_vxworks */ 5333 }; 5334 5335 static bfd_boolean 5336 elf32_i386_nacl_elf_object_p (bfd *abfd) 5337 { 5338 /* Set the right machine number for a NaCl i386 ELF32 file. */ 5339 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_i386_i386_nacl); 5340 return TRUE; 5341 } 5342 5343 #undef elf_backend_arch_data 5344 #define elf_backend_arch_data &elf_i386_nacl_arch_bed 5345 5346 #undef elf_backend_object_p 5347 #define elf_backend_object_p elf32_i386_nacl_elf_object_p 5348 #undef elf_backend_modify_segment_map 5349 #define elf_backend_modify_segment_map nacl_modify_segment_map 5350 #undef elf_backend_modify_program_headers 5351 #define elf_backend_modify_program_headers nacl_modify_program_headers 5352 #undef elf_backend_final_write_processing 5353 #define elf_backend_final_write_processing nacl_final_write_processing 5354 5355 #include "elf32-target.h" 5356 5357 /* Restore defaults. */ 5358 #undef elf_backend_object_p 5359 #undef elf_backend_modify_segment_map 5360 #undef elf_backend_modify_program_headers 5361 #undef elf_backend_final_write_processing 5362 5363 /* VxWorks support. */ 5364 5365 #undef TARGET_LITTLE_SYM 5366 #define TARGET_LITTLE_SYM i386_elf32_vxworks_vec 5367 #undef TARGET_LITTLE_NAME 5368 #define TARGET_LITTLE_NAME "elf32-i386-vxworks" 5369 #undef ELF_OSABI 5370 #undef elf_backend_plt_alignment 5371 #define elf_backend_plt_alignment 4 5372 5373 static const struct elf_i386_backend_data elf_i386_vxworks_arch_bed = 5374 { 5375 &elf_i386_plt, /* plt */ 5376 0x90, /* plt0_pad_byte */ 5377 1, /* is_vxworks */ 5378 }; 5379 5380 #undef elf_backend_arch_data 5381 #define elf_backend_arch_data &elf_i386_vxworks_arch_bed 5382 5383 #undef elf_backend_relocs_compatible 5384 #undef elf_backend_add_symbol_hook 5385 #define elf_backend_add_symbol_hook \ 5386 elf_vxworks_add_symbol_hook 5387 #undef elf_backend_link_output_symbol_hook 5388 #define elf_backend_link_output_symbol_hook \ 5389 elf_vxworks_link_output_symbol_hook 5390 #undef elf_backend_emit_relocs 5391 #define elf_backend_emit_relocs elf_vxworks_emit_relocs 5392 #undef elf_backend_final_write_processing 5393 #define elf_backend_final_write_processing \ 5394 elf_vxworks_final_write_processing 5395 #undef elf_backend_static_tls_alignment 5396 5397 /* On VxWorks, we emit relocations against _PROCEDURE_LINKAGE_TABLE_, so 5398 define it. */ 5399 #undef elf_backend_want_plt_sym 5400 #define elf_backend_want_plt_sym 1 5401 5402 #undef elf32_bed 5403 #define elf32_bed elf32_i386_vxworks_bed 5404 5405 #include "elf32-target.h" 5406