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