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