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