00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "bfd.h"
00027 #include "sysdep.h"
00028 #include "bfdlink.h"
00029 #include "libbfd.h"
00030 #include "elf-bfd.h"
00031
00032 #include "elf/x86-64.h"
00033
00034
00035 #define MINUS_ONE (~ (bfd_vma) 0)
00036
00037
00038
00039
00040 static reloc_howto_type x86_64_elf_howto_table[] =
00041 {
00042 HOWTO(R_X86_64_NONE, 0, 0, 0, FALSE, 0, complain_overflow_dont,
00043 bfd_elf_generic_reloc, "R_X86_64_NONE", FALSE, 0x00000000, 0x00000000,
00044 FALSE),
00045 HOWTO(R_X86_64_64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
00046 bfd_elf_generic_reloc, "R_X86_64_64", FALSE, MINUS_ONE, MINUS_ONE,
00047 FALSE),
00048 HOWTO(R_X86_64_PC32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
00049 bfd_elf_generic_reloc, "R_X86_64_PC32", FALSE, 0xffffffff, 0xffffffff,
00050 TRUE),
00051 HOWTO(R_X86_64_GOT32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
00052 bfd_elf_generic_reloc, "R_X86_64_GOT32", FALSE, 0xffffffff, 0xffffffff,
00053 FALSE),
00054 HOWTO(R_X86_64_PLT32, 0, 2, 32, TRUE, 0, complain_overflow_signed,
00055 bfd_elf_generic_reloc, "R_X86_64_PLT32", FALSE, 0xffffffff, 0xffffffff,
00056 TRUE),
00057 HOWTO(R_X86_64_COPY, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00058 bfd_elf_generic_reloc, "R_X86_64_COPY", FALSE, 0xffffffff, 0xffffffff,
00059 FALSE),
00060 HOWTO(R_X86_64_GLOB_DAT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
00061 bfd_elf_generic_reloc, "R_X86_64_GLOB_DAT", FALSE, MINUS_ONE,
00062 MINUS_ONE, FALSE),
00063 HOWTO(R_X86_64_JUMP_SLOT, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
00064 bfd_elf_generic_reloc, "R_X86_64_JUMP_SLOT", FALSE, MINUS_ONE,
00065 MINUS_ONE, FALSE),
00066 HOWTO(R_X86_64_RELATIVE, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
00067 bfd_elf_generic_reloc, "R_X86_64_RELATIVE", FALSE, MINUS_ONE,
00068 MINUS_ONE, FALSE),
00069 HOWTO(R_X86_64_GOTPCREL, 0, 2, 32, TRUE, 0, complain_overflow_signed,
00070 bfd_elf_generic_reloc, "R_X86_64_GOTPCREL", FALSE, 0xffffffff,
00071 0xffffffff, TRUE),
00072 HOWTO(R_X86_64_32, 0, 2, 32, FALSE, 0, complain_overflow_unsigned,
00073 bfd_elf_generic_reloc, "R_X86_64_32", FALSE, 0xffffffff, 0xffffffff,
00074 FALSE),
00075 HOWTO(R_X86_64_32S, 0, 2, 32, FALSE, 0, complain_overflow_signed,
00076 bfd_elf_generic_reloc, "R_X86_64_32S", FALSE, 0xffffffff, 0xffffffff,
00077 FALSE),
00078 HOWTO(R_X86_64_16, 0, 1, 16, FALSE, 0, complain_overflow_bitfield,
00079 bfd_elf_generic_reloc, "R_X86_64_16", FALSE, 0xffff, 0xffff, FALSE),
00080 HOWTO(R_X86_64_PC16,0, 1, 16, TRUE, 0, complain_overflow_bitfield,
00081 bfd_elf_generic_reloc, "R_X86_64_PC16", FALSE, 0xffff, 0xffff, TRUE),
00082 HOWTO(R_X86_64_8, 0, 0, 8, FALSE, 0, complain_overflow_signed,
00083 bfd_elf_generic_reloc, "R_X86_64_8", FALSE, 0xff, 0xff, FALSE),
00084 HOWTO(R_X86_64_PC8, 0, 0, 8, TRUE, 0, complain_overflow_signed,
00085 bfd_elf_generic_reloc, "R_X86_64_PC8", FALSE, 0xff, 0xff, TRUE),
00086 HOWTO(R_X86_64_DTPMOD64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
00087 bfd_elf_generic_reloc, "R_X86_64_DTPMOD64", FALSE, MINUS_ONE,
00088 MINUS_ONE, FALSE),
00089 HOWTO(R_X86_64_DTPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
00090 bfd_elf_generic_reloc, "R_X86_64_DTPOFF64", FALSE, MINUS_ONE,
00091 MINUS_ONE, FALSE),
00092 HOWTO(R_X86_64_TPOFF64, 0, 4, 64, FALSE, 0, complain_overflow_bitfield,
00093 bfd_elf_generic_reloc, "R_X86_64_TPOFF64", FALSE, MINUS_ONE,
00094 MINUS_ONE, FALSE),
00095 HOWTO(R_X86_64_TLSGD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
00096 bfd_elf_generic_reloc, "R_X86_64_TLSGD", FALSE, 0xffffffff,
00097 0xffffffff, TRUE),
00098 HOWTO(R_X86_64_TLSLD, 0, 2, 32, TRUE, 0, complain_overflow_signed,
00099 bfd_elf_generic_reloc, "R_X86_64_TLSLD", FALSE, 0xffffffff,
00100 0xffffffff, TRUE),
00101 HOWTO(R_X86_64_DTPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
00102 bfd_elf_generic_reloc, "R_X86_64_DTPOFF32", FALSE, 0xffffffff,
00103 0xffffffff, FALSE),
00104 HOWTO(R_X86_64_GOTTPOFF, 0, 2, 32, TRUE, 0, complain_overflow_signed,
00105 bfd_elf_generic_reloc, "R_X86_64_GOTTPOFF", FALSE, 0xffffffff,
00106 0xffffffff, TRUE),
00107 HOWTO(R_X86_64_TPOFF32, 0, 2, 32, FALSE, 0, complain_overflow_signed,
00108 bfd_elf_generic_reloc, "R_X86_64_TPOFF32", FALSE, 0xffffffff,
00109 0xffffffff, FALSE),
00110
00111
00112 HOWTO (R_X86_64_GNU_VTINHERIT, 0, 4, 0, FALSE, 0, complain_overflow_dont,
00113 NULL, "R_X86_64_GNU_VTINHERIT", FALSE, 0, 0, FALSE),
00114
00115
00116 HOWTO (R_X86_64_GNU_VTENTRY, 0, 4, 0, FALSE, 0, complain_overflow_dont,
00117 _bfd_elf_rel_vtable_reloc_fn, "R_X86_64_GNU_VTENTRY", FALSE, 0, 0,
00118 FALSE)
00119 };
00120
00121
00122 struct elf_reloc_map
00123 {
00124 bfd_reloc_code_real_type bfd_reloc_val;
00125 unsigned char elf_reloc_val;
00126 };
00127
00128 static const struct elf_reloc_map x86_64_reloc_map[] =
00129 {
00130 { BFD_RELOC_NONE, R_X86_64_NONE, },
00131 { BFD_RELOC_64, R_X86_64_64, },
00132 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
00133 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
00134 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
00135 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
00136 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
00137 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
00138 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
00139 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
00140 { BFD_RELOC_32, R_X86_64_32, },
00141 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
00142 { BFD_RELOC_16, R_X86_64_16, },
00143 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
00144 { BFD_RELOC_8, R_X86_64_8, },
00145 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
00146 { BFD_RELOC_X86_64_DTPMOD64, R_X86_64_DTPMOD64, },
00147 { BFD_RELOC_X86_64_DTPOFF64, R_X86_64_DTPOFF64, },
00148 { BFD_RELOC_X86_64_TPOFF64, R_X86_64_TPOFF64, },
00149 { BFD_RELOC_X86_64_TLSGD, R_X86_64_TLSGD, },
00150 { BFD_RELOC_X86_64_TLSLD, R_X86_64_TLSLD, },
00151 { BFD_RELOC_X86_64_DTPOFF32, R_X86_64_DTPOFF32, },
00152 { BFD_RELOC_X86_64_GOTTPOFF, R_X86_64_GOTTPOFF, },
00153 { BFD_RELOC_X86_64_TPOFF32, R_X86_64_TPOFF32, },
00154 { BFD_RELOC_VTABLE_INHERIT, R_X86_64_GNU_VTINHERIT, },
00155 { BFD_RELOC_VTABLE_ENTRY, R_X86_64_GNU_VTENTRY, },
00156 };
00157
00158
00159
00160 static reloc_howto_type *
00161 elf64_x86_64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
00162 bfd_reloc_code_real_type code)
00163 {
00164 unsigned int i;
00165
00166 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
00167 i++)
00168 {
00169 if (x86_64_reloc_map[i].bfd_reloc_val == code)
00170 return &x86_64_elf_howto_table[i];
00171 }
00172 return 0;
00173 }
00174
00175
00176
00177 static void
00178 elf64_x86_64_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
00179 Elf_Internal_Rela *dst)
00180 {
00181 unsigned r_type, i;
00182
00183 r_type = ELF64_R_TYPE (dst->r_info);
00184 if (r_type < (unsigned int) R_X86_64_GNU_VTINHERIT)
00185 {
00186 BFD_ASSERT (r_type <= (unsigned int) R_X86_64_TPOFF32);
00187 i = r_type;
00188 }
00189 else
00190 {
00191 BFD_ASSERT (r_type < (unsigned int) R_X86_64_max);
00192 i = r_type - ((unsigned int) R_X86_64_GNU_VTINHERIT - R_X86_64_TPOFF32 - 1);
00193 }
00194 cache_ptr->howto = &x86_64_elf_howto_table[i];
00195 BFD_ASSERT (r_type == cache_ptr->howto->type);
00196 }
00197
00198
00199 static bfd_boolean
00200 elf64_x86_64_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
00201 {
00202 int offset;
00203 size_t size;
00204
00205 switch (note->descsz)
00206 {
00207 default:
00208 return FALSE;
00209
00210 case 336:
00211
00212 elf_tdata (abfd)->core_signal
00213 = bfd_get_16 (abfd, note->descdata + 12);
00214
00215
00216 elf_tdata (abfd)->core_pid
00217 = bfd_get_32 (abfd, note->descdata + 32);
00218
00219
00220 offset = 112;
00221 size = 216;
00222
00223 break;
00224 }
00225
00226
00227 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
00228 size, note->descpos + offset);
00229 }
00230
00231 static bfd_boolean
00232 elf64_x86_64_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
00233 {
00234 switch (note->descsz)
00235 {
00236 default:
00237 return FALSE;
00238
00239 case 136:
00240 elf_tdata (abfd)->core_program
00241 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
00242 elf_tdata (abfd)->core_command
00243 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
00244 }
00245
00246
00247
00248
00249
00250 {
00251 char *command = elf_tdata (abfd)->core_command;
00252 int n = strlen (command);
00253
00254 if (0 < n && command[n - 1] == ' ')
00255 command[n - 1] = '\0';
00256 }
00257
00258 return TRUE;
00259 }
00260
00261
00262
00263
00264
00265
00266 #define ELF_DYNAMIC_INTERPRETER "/lib/ld64.so.1"
00267
00268
00269
00270
00271
00272 #define ELIMINATE_COPY_RELOCS 1
00273
00274
00275
00276 #define GOT_ENTRY_SIZE 8
00277
00278
00279
00280 #define PLT_ENTRY_SIZE 16
00281
00282
00283
00284
00285 static const bfd_byte elf64_x86_64_plt0_entry[PLT_ENTRY_SIZE] =
00286 {
00287 0xff, 0x35, 8, 0, 0, 0,
00288 0xff, 0x25, 16, 0, 0, 0,
00289 0x90, 0x90, 0x90, 0x90
00290 };
00291
00292
00293
00294 static const bfd_byte elf64_x86_64_plt_entry[PLT_ENTRY_SIZE] =
00295 {
00296 0xff, 0x25,
00297 0, 0, 0, 0,
00298 0x68,
00299 0, 0, 0, 0,
00300 0xe9,
00301 0, 0, 0, 0
00302 };
00303
00304
00305
00306
00307
00308
00309
00310 struct elf64_x86_64_dyn_relocs
00311 {
00312
00313 struct elf64_x86_64_dyn_relocs *next;
00314
00315
00316 asection *sec;
00317
00318
00319 bfd_size_type count;
00320
00321
00322 bfd_size_type pc_count;
00323 };
00324
00325
00326
00327 struct elf64_x86_64_link_hash_entry
00328 {
00329 struct elf_link_hash_entry elf;
00330
00331
00332 struct elf64_x86_64_dyn_relocs *dyn_relocs;
00333
00334 #define GOT_UNKNOWN 0
00335 #define GOT_NORMAL 1
00336 #define GOT_TLS_GD 2
00337 #define GOT_TLS_IE 3
00338 unsigned char tls_type;
00339 };
00340
00341 #define elf64_x86_64_hash_entry(ent) \
00342 ((struct elf64_x86_64_link_hash_entry *)(ent))
00343
00344 struct elf64_x86_64_obj_tdata
00345 {
00346 struct elf_obj_tdata root;
00347
00348
00349 char *local_got_tls_type;
00350 };
00351
00352 #define elf64_x86_64_tdata(abfd) \
00353 ((struct elf64_x86_64_obj_tdata *) (abfd)->tdata.any)
00354
00355 #define elf64_x86_64_local_got_tls_type(abfd) \
00356 (elf64_x86_64_tdata (abfd)->local_got_tls_type)
00357
00358
00359
00360
00361 struct elf64_x86_64_link_hash_table
00362 {
00363 struct elf_link_hash_table elf;
00364
00365
00366 asection *sgot;
00367 asection *sgotplt;
00368 asection *srelgot;
00369 asection *splt;
00370 asection *srelplt;
00371 asection *sdynbss;
00372 asection *srelbss;
00373
00374 union {
00375 bfd_signed_vma refcount;
00376 bfd_vma offset;
00377 } tls_ld_got;
00378
00379
00380 struct sym_sec_cache sym_sec;
00381 };
00382
00383
00384
00385 #define elf64_x86_64_hash_table(p) \
00386 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
00387
00388
00389
00390 static struct bfd_hash_entry *
00391 link_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
00392 const char *string)
00393 {
00394
00395
00396 if (entry == NULL)
00397 {
00398 entry = bfd_hash_allocate (table,
00399 sizeof (struct elf64_x86_64_link_hash_entry));
00400 if (entry == NULL)
00401 return entry;
00402 }
00403
00404
00405 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
00406 if (entry != NULL)
00407 {
00408 struct elf64_x86_64_link_hash_entry *eh;
00409
00410 eh = (struct elf64_x86_64_link_hash_entry *) entry;
00411 eh->dyn_relocs = NULL;
00412 eh->tls_type = GOT_UNKNOWN;
00413 }
00414
00415 return entry;
00416 }
00417
00418
00419
00420 static struct bfd_link_hash_table *
00421 elf64_x86_64_link_hash_table_create (bfd *abfd)
00422 {
00423 struct elf64_x86_64_link_hash_table *ret;
00424 bfd_size_type amt = sizeof (struct elf64_x86_64_link_hash_table);
00425
00426 ret = (struct elf64_x86_64_link_hash_table *) bfd_malloc (amt);
00427 if (ret == NULL)
00428 return NULL;
00429
00430 if (! _bfd_elf_link_hash_table_init (&ret->elf, abfd, link_hash_newfunc))
00431 {
00432 free (ret);
00433 return NULL;
00434 }
00435
00436 ret->sgot = NULL;
00437 ret->sgotplt = NULL;
00438 ret->srelgot = NULL;
00439 ret->splt = NULL;
00440 ret->srelplt = NULL;
00441 ret->sdynbss = NULL;
00442 ret->srelbss = NULL;
00443 ret->sym_sec.abfd = NULL;
00444 ret->tls_ld_got.refcount = 0;
00445
00446 return &ret->elf.root;
00447 }
00448
00449
00450
00451
00452 static bfd_boolean
00453 create_got_section (bfd *dynobj, struct bfd_link_info *info)
00454 {
00455 struct elf64_x86_64_link_hash_table *htab;
00456
00457 if (! _bfd_elf_create_got_section (dynobj, info))
00458 return FALSE;
00459
00460 htab = elf64_x86_64_hash_table (info);
00461 htab->sgot = bfd_get_section_by_name (dynobj, ".got");
00462 htab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
00463 if (!htab->sgot || !htab->sgotplt)
00464 abort ();
00465
00466 htab->srelgot = bfd_make_section (dynobj, ".rela.got");
00467 if (htab->srelgot == NULL
00468 || ! bfd_set_section_flags (dynobj, htab->srelgot,
00469 (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
00470 | SEC_IN_MEMORY | SEC_LINKER_CREATED
00471 | SEC_READONLY))
00472 || ! bfd_set_section_alignment (dynobj, htab->srelgot, 3))
00473 return FALSE;
00474 return TRUE;
00475 }
00476
00477
00478
00479
00480
00481 static bfd_boolean
00482 elf64_x86_64_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
00483 {
00484 struct elf64_x86_64_link_hash_table *htab;
00485
00486 htab = elf64_x86_64_hash_table (info);
00487 if (!htab->sgot && !create_got_section (dynobj, info))
00488 return FALSE;
00489
00490 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
00491 return FALSE;
00492
00493 htab->splt = bfd_get_section_by_name (dynobj, ".plt");
00494 htab->srelplt = bfd_get_section_by_name (dynobj, ".rela.plt");
00495 htab->sdynbss = bfd_get_section_by_name (dynobj, ".dynbss");
00496 if (!info->shared)
00497 htab->srelbss = bfd_get_section_by_name (dynobj, ".rela.bss");
00498
00499 if (!htab->splt || !htab->srelplt || !htab->sdynbss
00500 || (!info->shared && !htab->srelbss))
00501 abort ();
00502
00503 return TRUE;
00504 }
00505
00506
00507
00508 static void
00509 elf64_x86_64_copy_indirect_symbol (const struct elf_backend_data *bed,
00510 struct elf_link_hash_entry *dir,
00511 struct elf_link_hash_entry *ind)
00512 {
00513 struct elf64_x86_64_link_hash_entry *edir, *eind;
00514
00515 edir = (struct elf64_x86_64_link_hash_entry *) dir;
00516 eind = (struct elf64_x86_64_link_hash_entry *) ind;
00517
00518 if (eind->dyn_relocs != NULL)
00519 {
00520 if (edir->dyn_relocs != NULL)
00521 {
00522 struct elf64_x86_64_dyn_relocs **pp;
00523 struct elf64_x86_64_dyn_relocs *p;
00524
00525 if (ind->root.type == bfd_link_hash_indirect)
00526 abort ();
00527
00528
00529
00530 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
00531 {
00532 struct elf64_x86_64_dyn_relocs *q;
00533
00534 for (q = edir->dyn_relocs; q != NULL; q = q->next)
00535 if (q->sec == p->sec)
00536 {
00537 q->pc_count += p->pc_count;
00538 q->count += p->count;
00539 *pp = p->next;
00540 break;
00541 }
00542 if (q == NULL)
00543 pp = &p->next;
00544 }
00545 *pp = edir->dyn_relocs;
00546 }
00547
00548 edir->dyn_relocs = eind->dyn_relocs;
00549 eind->dyn_relocs = NULL;
00550 }
00551
00552 if (ind->root.type == bfd_link_hash_indirect
00553 && dir->got.refcount <= 0)
00554 {
00555 edir->tls_type = eind->tls_type;
00556 eind->tls_type = GOT_UNKNOWN;
00557 }
00558
00559 if (ELIMINATE_COPY_RELOCS
00560 && ind->root.type != bfd_link_hash_indirect
00561 && dir->dynamic_adjusted)
00562 {
00563
00564
00565
00566 dir->ref_dynamic |= ind->ref_dynamic;
00567 dir->ref_regular |= ind->ref_regular;
00568 dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
00569 dir->needs_plt |= ind->needs_plt;
00570 dir->pointer_equality_needed |= ind->pointer_equality_needed;
00571 }
00572 else
00573 _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
00574 }
00575
00576 static bfd_boolean
00577 elf64_x86_64_mkobject (bfd *abfd)
00578 {
00579 bfd_size_type amt = sizeof (struct elf64_x86_64_obj_tdata);
00580 abfd->tdata.any = bfd_zalloc (abfd, amt);
00581 if (abfd->tdata.any == NULL)
00582 return FALSE;
00583 return TRUE;
00584 }
00585
00586 static bfd_boolean
00587 elf64_x86_64_elf_object_p (bfd *abfd)
00588 {
00589
00590 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
00591 return TRUE;
00592 }
00593
00594 static int
00595 elf64_x86_64_tls_transition (struct bfd_link_info *info, int r_type, int is_local)
00596 {
00597 if (info->shared)
00598 return r_type;
00599
00600 switch (r_type)
00601 {
00602 case R_X86_64_TLSGD:
00603 case R_X86_64_GOTTPOFF:
00604 if (is_local)
00605 return R_X86_64_TPOFF32;
00606 return R_X86_64_GOTTPOFF;
00607 case R_X86_64_TLSLD:
00608 return R_X86_64_TPOFF32;
00609 }
00610
00611 return r_type;
00612 }
00613
00614
00615
00616
00617
00618 static bfd_boolean
00619 elf64_x86_64_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
00620 const Elf_Internal_Rela *relocs)
00621 {
00622 struct elf64_x86_64_link_hash_table *htab;
00623 Elf_Internal_Shdr *symtab_hdr;
00624 struct elf_link_hash_entry **sym_hashes;
00625 const Elf_Internal_Rela *rel;
00626 const Elf_Internal_Rela *rel_end;
00627 asection *sreloc;
00628
00629 if (info->relocatable)
00630 return TRUE;
00631
00632 htab = elf64_x86_64_hash_table (info);
00633 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
00634 sym_hashes = elf_sym_hashes (abfd);
00635
00636 sreloc = NULL;
00637
00638 rel_end = relocs + sec->reloc_count;
00639 for (rel = relocs; rel < rel_end; rel++)
00640 {
00641 unsigned int r_type;
00642 unsigned long r_symndx;
00643 struct elf_link_hash_entry *h;
00644
00645 r_symndx = ELF64_R_SYM (rel->r_info);
00646 r_type = ELF64_R_TYPE (rel->r_info);
00647
00648 if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
00649 {
00650 (*_bfd_error_handler) (_("%B: bad symbol index: %d"),
00651 abfd, r_symndx);
00652 return FALSE;
00653 }
00654
00655 if (r_symndx < symtab_hdr->sh_info)
00656 h = NULL;
00657 else
00658 {
00659 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
00660
00661
00662 h->ref_regular = 1;
00663 }
00664 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
00665 switch (r_type)
00666 {
00667 case R_X86_64_TLSLD:
00668 htab->tls_ld_got.refcount += 1;
00669 goto create_got;
00670
00671 case R_X86_64_TPOFF32:
00672 if (info->shared)
00673 {
00674 (*_bfd_error_handler)
00675 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
00676 abfd,
00677 x86_64_elf_howto_table[r_type].name,
00678 (h) ? h->root.root.string : "a local symbol");
00679 bfd_set_error (bfd_error_bad_value);
00680 return FALSE;
00681 }
00682 break;
00683
00684 case R_X86_64_GOTTPOFF:
00685 if (info->shared)
00686 info->flags |= DF_STATIC_TLS;
00687
00688
00689 case R_X86_64_GOT32:
00690 case R_X86_64_GOTPCREL:
00691 case R_X86_64_TLSGD:
00692
00693 {
00694 int tls_type, old_tls_type;
00695
00696 switch (r_type)
00697 {
00698 default: tls_type = GOT_NORMAL; break;
00699 case R_X86_64_TLSGD: tls_type = GOT_TLS_GD; break;
00700 case R_X86_64_GOTTPOFF: tls_type = GOT_TLS_IE; break;
00701 }
00702
00703 if (h != NULL)
00704 {
00705 h->got.refcount += 1;
00706 old_tls_type = elf64_x86_64_hash_entry (h)->tls_type;
00707 }
00708 else
00709 {
00710 bfd_signed_vma *local_got_refcounts;
00711
00712
00713 local_got_refcounts = elf_local_got_refcounts (abfd);
00714 if (local_got_refcounts == NULL)
00715 {
00716 bfd_size_type size;
00717
00718 size = symtab_hdr->sh_info;
00719 size *= sizeof (bfd_signed_vma) + sizeof (char);
00720 local_got_refcounts = ((bfd_signed_vma *)
00721 bfd_zalloc (abfd, size));
00722 if (local_got_refcounts == NULL)
00723 return FALSE;
00724 elf_local_got_refcounts (abfd) = local_got_refcounts;
00725 elf64_x86_64_local_got_tls_type (abfd)
00726 = (char *) (local_got_refcounts + symtab_hdr->sh_info);
00727 }
00728 local_got_refcounts[r_symndx] += 1;
00729 old_tls_type
00730 = elf64_x86_64_local_got_tls_type (abfd) [r_symndx];
00731 }
00732
00733
00734
00735 if (old_tls_type != tls_type && old_tls_type != GOT_UNKNOWN
00736 && (old_tls_type != GOT_TLS_GD || tls_type != GOT_TLS_IE))
00737 {
00738 if (old_tls_type == GOT_TLS_IE && tls_type == GOT_TLS_GD)
00739 tls_type = old_tls_type;
00740 else
00741 {
00742 (*_bfd_error_handler)
00743 (_("%B: %s' accessed both as normal and thread local symbol"),
00744 abfd, h ? h->root.root.string : "<local>");
00745 return FALSE;
00746 }
00747 }
00748
00749 if (old_tls_type != tls_type)
00750 {
00751 if (h != NULL)
00752 elf64_x86_64_hash_entry (h)->tls_type = tls_type;
00753 else
00754 elf64_x86_64_local_got_tls_type (abfd) [r_symndx] = tls_type;
00755 }
00756 }
00757
00758
00759
00760 create_got:
00761 if (htab->sgot == NULL)
00762 {
00763 if (htab->elf.dynobj == NULL)
00764 htab->elf.dynobj = abfd;
00765 if (!create_got_section (htab->elf.dynobj, info))
00766 return FALSE;
00767 }
00768 break;
00769
00770 case R_X86_64_PLT32:
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780 if (h == NULL)
00781 continue;
00782
00783 h->needs_plt = 1;
00784 h->plt.refcount += 1;
00785 break;
00786
00787 case R_X86_64_8:
00788 case R_X86_64_16:
00789 case R_X86_64_32:
00790 case R_X86_64_32S:
00791
00792
00793
00794
00795 if (info->shared
00796 && (sec->flags & SEC_ALLOC) != 0
00797 && (sec->flags & SEC_READONLY) != 0)
00798 {
00799 (*_bfd_error_handler)
00800 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
00801 abfd,
00802 x86_64_elf_howto_table[r_type].name,
00803 (h) ? h->root.root.string : "a local symbol");
00804 bfd_set_error (bfd_error_bad_value);
00805 return FALSE;
00806 }
00807
00808
00809 case R_X86_64_PC8:
00810 case R_X86_64_PC16:
00811 case R_X86_64_PC32:
00812 case R_X86_64_64:
00813 if (h != NULL && !info->shared)
00814 {
00815
00816
00817
00818
00819
00820
00821 h->non_got_ref = 1;
00822
00823
00824
00825 h->plt.refcount += 1;
00826 if (r_type != R_X86_64_PC32)
00827 h->pointer_equality_needed = 1;
00828 }
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851 if ((info->shared
00852 && (sec->flags & SEC_ALLOC) != 0
00853 && (((r_type != R_X86_64_PC8)
00854 && (r_type != R_X86_64_PC16)
00855 && (r_type != R_X86_64_PC32))
00856 || (h != NULL
00857 && (! info->symbolic
00858 || h->root.type == bfd_link_hash_defweak
00859 || !h->def_regular))))
00860 || (ELIMINATE_COPY_RELOCS
00861 && !info->shared
00862 && (sec->flags & SEC_ALLOC) != 0
00863 && h != NULL
00864 && (h->root.type == bfd_link_hash_defweak
00865 || !h->def_regular)))
00866 {
00867 struct elf64_x86_64_dyn_relocs *p;
00868 struct elf64_x86_64_dyn_relocs **head;
00869
00870
00871
00872
00873 if (sreloc == NULL)
00874 {
00875 const char *name;
00876 bfd *dynobj;
00877
00878 name = (bfd_elf_string_from_elf_section
00879 (abfd,
00880 elf_elfheader (abfd)->e_shstrndx,
00881 elf_section_data (sec)->rel_hdr.sh_name));
00882 if (name == NULL)
00883 return FALSE;
00884
00885 if (strncmp (name, ".rela", 5) != 0
00886 || strcmp (bfd_get_section_name (abfd, sec),
00887 name + 5) != 0)
00888 {
00889 (*_bfd_error_handler)
00890 (_("%B: bad relocation section name `%s\'"),
00891 abfd, name);
00892 }
00893
00894 if (htab->elf.dynobj == NULL)
00895 htab->elf.dynobj = abfd;
00896
00897 dynobj = htab->elf.dynobj;
00898
00899 sreloc = bfd_get_section_by_name (dynobj, name);
00900 if (sreloc == NULL)
00901 {
00902 flagword flags;
00903
00904 sreloc = bfd_make_section (dynobj, name);
00905 flags = (SEC_HAS_CONTENTS | SEC_READONLY
00906 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
00907 if ((sec->flags & SEC_ALLOC) != 0)
00908 flags |= SEC_ALLOC | SEC_LOAD;
00909 if (sreloc == NULL
00910 || ! bfd_set_section_flags (dynobj, sreloc, flags)
00911 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
00912 return FALSE;
00913 }
00914 elf_section_data (sec)->sreloc = sreloc;
00915 }
00916
00917
00918
00919 if (h != NULL)
00920 {
00921 head = &((struct elf64_x86_64_link_hash_entry *) h)->dyn_relocs;
00922 }
00923 else
00924 {
00925
00926
00927
00928
00929 asection *s;
00930 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
00931 sec, r_symndx);
00932 if (s == NULL)
00933 return FALSE;
00934
00935 head = ((struct elf64_x86_64_dyn_relocs **)
00936 &elf_section_data (s)->local_dynrel);
00937 }
00938
00939 p = *head;
00940 if (p == NULL || p->sec != sec)
00941 {
00942 bfd_size_type amt = sizeof *p;
00943 p = ((struct elf64_x86_64_dyn_relocs *)
00944 bfd_alloc (htab->elf.dynobj, amt));
00945 if (p == NULL)
00946 return FALSE;
00947 p->next = *head;
00948 *head = p;
00949 p->sec = sec;
00950 p->count = 0;
00951 p->pc_count = 0;
00952 }
00953
00954 p->count += 1;
00955 if (r_type == R_X86_64_PC8
00956 || r_type == R_X86_64_PC16
00957 || r_type == R_X86_64_PC32)
00958 p->pc_count += 1;
00959 }
00960 break;
00961
00962
00963
00964 case R_X86_64_GNU_VTINHERIT:
00965 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
00966 return FALSE;
00967 break;
00968
00969
00970
00971 case R_X86_64_GNU_VTENTRY:
00972 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
00973 return FALSE;
00974 break;
00975
00976 default:
00977 break;
00978 }
00979 }
00980
00981 return TRUE;
00982 }
00983
00984
00985
00986
00987 static asection *
00988 elf64_x86_64_gc_mark_hook (asection *sec,
00989 struct bfd_link_info *info ATTRIBUTE_UNUSED,
00990 Elf_Internal_Rela *rel,
00991 struct elf_link_hash_entry *h,
00992 Elf_Internal_Sym *sym)
00993 {
00994 if (h != NULL)
00995 {
00996 switch (ELF64_R_TYPE (rel->r_info))
00997 {
00998 case R_X86_64_GNU_VTINHERIT:
00999 case R_X86_64_GNU_VTENTRY:
01000 break;
01001
01002 default:
01003 switch (h->root.type)
01004 {
01005 case bfd_link_hash_defined:
01006 case bfd_link_hash_defweak:
01007 return h->root.u.def.section;
01008
01009 case bfd_link_hash_common:
01010 return h->root.u.c.p->section;
01011
01012 default:
01013 break;
01014 }
01015 }
01016 }
01017 else
01018 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
01019
01020 return NULL;
01021 }
01022
01023
01024
01025 static bfd_boolean
01026 elf64_x86_64_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
01027 asection *sec, const Elf_Internal_Rela *relocs)
01028 {
01029 Elf_Internal_Shdr *symtab_hdr;
01030 struct elf_link_hash_entry **sym_hashes;
01031 bfd_signed_vma *local_got_refcounts;
01032 const Elf_Internal_Rela *rel, *relend;
01033
01034 elf_section_data (sec)->local_dynrel = NULL;
01035
01036 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
01037 sym_hashes = elf_sym_hashes (abfd);
01038 local_got_refcounts = elf_local_got_refcounts (abfd);
01039
01040 relend = relocs + sec->reloc_count;
01041 for (rel = relocs; rel < relend; rel++)
01042 {
01043 unsigned long r_symndx;
01044 unsigned int r_type;
01045 struct elf_link_hash_entry *h = NULL;
01046
01047 r_symndx = ELF64_R_SYM (rel->r_info);
01048 if (r_symndx >= symtab_hdr->sh_info)
01049 {
01050 struct elf64_x86_64_link_hash_entry *eh;
01051 struct elf64_x86_64_dyn_relocs **pp;
01052 struct elf64_x86_64_dyn_relocs *p;
01053
01054 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
01055 while (h->root.type == bfd_link_hash_indirect
01056 || h->root.type == bfd_link_hash_warning)
01057 h = (struct elf_link_hash_entry *) h->root.u.i.link;
01058 eh = (struct elf64_x86_64_link_hash_entry *) h;
01059
01060 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
01061 if (p->sec == sec)
01062 {
01063
01064 *pp = p->next;
01065 break;
01066 }
01067 }
01068
01069 r_type = ELF64_R_TYPE (rel->r_info);
01070 r_type = elf64_x86_64_tls_transition (info, r_type, h != NULL);
01071 switch (r_type)
01072 {
01073 case R_X86_64_TLSLD:
01074 if (elf64_x86_64_hash_table (info)->tls_ld_got.refcount > 0)
01075 elf64_x86_64_hash_table (info)->tls_ld_got.refcount -= 1;
01076 break;
01077
01078 case R_X86_64_TLSGD:
01079 case R_X86_64_GOTTPOFF:
01080 case R_X86_64_GOT32:
01081 case R_X86_64_GOTPCREL:
01082 if (h != NULL)
01083 {
01084 if (h->got.refcount > 0)
01085 h->got.refcount -= 1;
01086 }
01087 else if (local_got_refcounts != NULL)
01088 {
01089 if (local_got_refcounts[r_symndx] > 0)
01090 local_got_refcounts[r_symndx] -= 1;
01091 }
01092 break;
01093
01094 case R_X86_64_8:
01095 case R_X86_64_16:
01096 case R_X86_64_32:
01097 case R_X86_64_64:
01098 case R_X86_64_32S:
01099 case R_X86_64_PC8:
01100 case R_X86_64_PC16:
01101 case R_X86_64_PC32:
01102 if (info->shared)
01103 break;
01104
01105
01106 case R_X86_64_PLT32:
01107 if (h != NULL)
01108 {
01109 if (h->plt.refcount > 0)
01110 h->plt.refcount -= 1;
01111 }
01112 break;
01113
01114 default:
01115 break;
01116 }
01117 }
01118
01119 return TRUE;
01120 }
01121
01122
01123
01124
01125
01126
01127
01128 static bfd_boolean
01129 elf64_x86_64_adjust_dynamic_symbol (struct bfd_link_info *info,
01130 struct elf_link_hash_entry *h)
01131 {
01132 struct elf64_x86_64_link_hash_table *htab;
01133 asection *s;
01134 unsigned int power_of_two;
01135
01136
01137
01138
01139 if (h->type == STT_FUNC
01140 || h->needs_plt)
01141 {
01142 if (h->plt.refcount <= 0
01143 || SYMBOL_CALLS_LOCAL (info, h)
01144 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
01145 && h->root.type == bfd_link_hash_undefweak))
01146 {
01147
01148
01149
01150
01151
01152 h->plt.offset = (bfd_vma) -1;
01153 h->needs_plt = 0;
01154 }
01155
01156 return TRUE;
01157 }
01158 else
01159
01160
01161
01162
01163
01164 h->plt.offset = (bfd_vma) -1;
01165
01166
01167
01168
01169 if (h->u.weakdef != NULL)
01170 {
01171 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
01172 || h->u.weakdef->root.type == bfd_link_hash_defweak);
01173 h->root.u.def.section = h->u.weakdef->root.u.def.section;
01174 h->root.u.def.value = h->u.weakdef->root.u.def.value;
01175 if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
01176 h->non_got_ref = h->u.weakdef->non_got_ref;
01177 return TRUE;
01178 }
01179
01180
01181
01182
01183
01184
01185
01186
01187 if (info->shared)
01188 return TRUE;
01189
01190
01191
01192 if (!h->non_got_ref)
01193 return TRUE;
01194
01195
01196 if (info->nocopyreloc)
01197 {
01198 h->non_got_ref = 0;
01199 return TRUE;
01200 }
01201
01202 if (ELIMINATE_COPY_RELOCS)
01203 {
01204 struct elf64_x86_64_link_hash_entry * eh;
01205 struct elf64_x86_64_dyn_relocs *p;
01206
01207 eh = (struct elf64_x86_64_link_hash_entry *) h;
01208 for (p = eh->dyn_relocs; p != NULL; p = p->next)
01209 {
01210 s = p->sec->output_section;
01211 if (s != NULL && (s->flags & SEC_READONLY) != 0)
01212 break;
01213 }
01214
01215
01216
01217 if (p == NULL)
01218 {
01219 h->non_got_ref = 0;
01220 return TRUE;
01221 }
01222 }
01223
01224
01225
01226
01227
01228
01229
01230
01231
01232
01233
01234 htab = elf64_x86_64_hash_table (info);
01235
01236
01237
01238
01239 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
01240 {
01241 htab->srelbss->size += sizeof (Elf64_External_Rela);
01242 h->needs_copy = 1;
01243 }
01244
01245
01246
01247
01248
01249
01250 power_of_two = bfd_log2 (h->size);
01251 if (power_of_two > 4)
01252 power_of_two = 4;
01253
01254
01255 s = htab->sdynbss;
01256 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
01257 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
01258 {
01259 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
01260 return FALSE;
01261 }
01262
01263
01264 h->root.u.def.section = s;
01265 h->root.u.def.value = s->size;
01266
01267
01268 s->size += h->size;
01269
01270 return TRUE;
01271 }
01272
01273
01274
01275
01276 static bfd_boolean
01277 allocate_dynrelocs (struct elf_link_hash_entry *h, void * inf)
01278 {
01279 struct bfd_link_info *info;
01280 struct elf64_x86_64_link_hash_table *htab;
01281 struct elf64_x86_64_link_hash_entry *eh;
01282 struct elf64_x86_64_dyn_relocs *p;
01283
01284 if (h->root.type == bfd_link_hash_indirect)
01285 return TRUE;
01286
01287 if (h->root.type == bfd_link_hash_warning)
01288 h = (struct elf_link_hash_entry *) h->root.u.i.link;
01289
01290 info = (struct bfd_link_info *) inf;
01291 htab = elf64_x86_64_hash_table (info);
01292
01293 if (htab->elf.dynamic_sections_created
01294 && h->plt.refcount > 0)
01295 {
01296
01297
01298 if (h->dynindx == -1
01299 && !h->forced_local)
01300 {
01301 if (! bfd_elf_link_record_dynamic_symbol (info, h))
01302 return FALSE;
01303 }
01304
01305 if (info->shared
01306 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
01307 {
01308 asection *s = htab->splt;
01309
01310
01311
01312 if (s->size == 0)
01313 s->size += PLT_ENTRY_SIZE;
01314
01315 h->plt.offset = s->size;
01316
01317
01318
01319
01320
01321
01322 if (! info->shared
01323 && !h->def_regular)
01324 {
01325 h->root.u.def.section = s;
01326 h->root.u.def.value = h->plt.offset;
01327 }
01328
01329
01330 s->size += PLT_ENTRY_SIZE;
01331
01332
01333
01334 htab->sgotplt->size += GOT_ENTRY_SIZE;
01335
01336
01337 htab->srelplt->size += sizeof (Elf64_External_Rela);
01338 }
01339 else
01340 {
01341 h->plt.offset = (bfd_vma) -1;
01342 h->needs_plt = 0;
01343 }
01344 }
01345 else
01346 {
01347 h->plt.offset = (bfd_vma) -1;
01348 h->needs_plt = 0;
01349 }
01350
01351
01352
01353 if (h->got.refcount > 0
01354 && !info->shared
01355 && h->dynindx == -1
01356 && elf64_x86_64_hash_entry (h)->tls_type == GOT_TLS_IE)
01357 h->got.offset = (bfd_vma) -1;
01358 else if (h->got.refcount > 0)
01359 {
01360 asection *s;
01361 bfd_boolean dyn;
01362 int tls_type = elf64_x86_64_hash_entry (h)->tls_type;
01363
01364
01365
01366 if (h->dynindx == -1
01367 && !h->forced_local)
01368 {
01369 if (! bfd_elf_link_record_dynamic_symbol (info, h))
01370 return FALSE;
01371 }
01372
01373 s = htab->sgot;
01374 h->got.offset = s->size;
01375 s->size += GOT_ENTRY_SIZE;
01376
01377 if (tls_type == GOT_TLS_GD)
01378 s->size += GOT_ENTRY_SIZE;
01379 dyn = htab->elf.dynamic_sections_created;
01380
01381
01382
01383 if ((tls_type == GOT_TLS_GD && h->dynindx == -1)
01384 || tls_type == GOT_TLS_IE)
01385 htab->srelgot->size += sizeof (Elf64_External_Rela);
01386 else if (tls_type == GOT_TLS_GD)
01387 htab->srelgot->size += 2 * sizeof (Elf64_External_Rela);
01388 else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
01389 || h->root.type != bfd_link_hash_undefweak)
01390 && (info->shared
01391 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
01392 htab->srelgot->size += sizeof (Elf64_External_Rela);
01393 }
01394 else
01395 h->got.offset = (bfd_vma) -1;
01396
01397 eh = (struct elf64_x86_64_link_hash_entry *) h;
01398 if (eh->dyn_relocs == NULL)
01399 return TRUE;
01400
01401
01402
01403
01404
01405
01406
01407 if (info->shared)
01408 {
01409
01410
01411
01412
01413
01414
01415 if (SYMBOL_CALLS_LOCAL (info, h))
01416 {
01417 struct elf64_x86_64_dyn_relocs **pp;
01418
01419 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
01420 {
01421 p->count -= p->pc_count;
01422 p->pc_count = 0;
01423 if (p->count == 0)
01424 *pp = p->next;
01425 else
01426 pp = &p->next;
01427 }
01428 }
01429
01430
01431
01432 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
01433 && h->root.type == bfd_link_hash_undefweak)
01434 eh->dyn_relocs = NULL;
01435 }
01436 else if (ELIMINATE_COPY_RELOCS)
01437 {
01438
01439
01440
01441
01442 if (!h->non_got_ref
01443 && ((h->def_dynamic
01444 && !h->def_regular)
01445 || (htab->elf.dynamic_sections_created
01446 && (h->root.type == bfd_link_hash_undefweak
01447 || h->root.type == bfd_link_hash_undefined))))
01448 {
01449
01450
01451 if (h->dynindx == -1
01452 && !h->forced_local)
01453 {
01454 if (! bfd_elf_link_record_dynamic_symbol (info, h))
01455 return FALSE;
01456 }
01457
01458
01459
01460 if (h->dynindx != -1)
01461 goto keep;
01462 }
01463
01464 eh->dyn_relocs = NULL;
01465
01466 keep: ;
01467 }
01468
01469
01470 for (p = eh->dyn_relocs; p != NULL; p = p->next)
01471 {
01472 asection *sreloc = elf_section_data (p->sec)->sreloc;
01473 sreloc->size += p->count * sizeof (Elf64_External_Rela);
01474 }
01475
01476 return TRUE;
01477 }
01478
01479
01480
01481 static bfd_boolean
01482 readonly_dynrelocs (struct elf_link_hash_entry *h, void * inf)
01483 {
01484 struct elf64_x86_64_link_hash_entry *eh;
01485 struct elf64_x86_64_dyn_relocs *p;
01486
01487 if (h->root.type == bfd_link_hash_warning)
01488 h = (struct elf_link_hash_entry *) h->root.u.i.link;
01489
01490 eh = (struct elf64_x86_64_link_hash_entry *) h;
01491 for (p = eh->dyn_relocs; p != NULL; p = p->next)
01492 {
01493 asection *s = p->sec->output_section;
01494
01495 if (s != NULL && (s->flags & SEC_READONLY) != 0)
01496 {
01497 struct bfd_link_info *info = (struct bfd_link_info *) inf;
01498
01499 info->flags |= DF_TEXTREL;
01500
01501
01502 return FALSE;
01503 }
01504 }
01505 return TRUE;
01506 }
01507
01508
01509
01510 static bfd_boolean
01511 elf64_x86_64_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
01512 struct bfd_link_info *info)
01513 {
01514 struct elf64_x86_64_link_hash_table *htab;
01515 bfd *dynobj;
01516 asection *s;
01517 bfd_boolean relocs;
01518 bfd *ibfd;
01519
01520 htab = elf64_x86_64_hash_table (info);
01521 dynobj = htab->elf.dynobj;
01522 if (dynobj == NULL)
01523 abort ();
01524
01525 if (htab->elf.dynamic_sections_created)
01526 {
01527
01528 if (info->executable)
01529 {
01530 s = bfd_get_section_by_name (dynobj, ".interp");
01531 if (s == NULL)
01532 abort ();
01533 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
01534 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
01535 }
01536 }
01537
01538
01539
01540 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
01541 {
01542 bfd_signed_vma *local_got;
01543 bfd_signed_vma *end_local_got;
01544 char *local_tls_type;
01545 bfd_size_type locsymcount;
01546 Elf_Internal_Shdr *symtab_hdr;
01547 asection *srel;
01548
01549 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
01550 continue;
01551
01552 for (s = ibfd->sections; s != NULL; s = s->next)
01553 {
01554 struct elf64_x86_64_dyn_relocs *p;
01555
01556 for (p = *((struct elf64_x86_64_dyn_relocs **)
01557 &elf_section_data (s)->local_dynrel);
01558 p != NULL;
01559 p = p->next)
01560 {
01561 if (!bfd_is_abs_section (p->sec)
01562 && bfd_is_abs_section (p->sec->output_section))
01563 {
01564
01565
01566
01567
01568 }
01569 else if (p->count != 0)
01570 {
01571 srel = elf_section_data (p->sec)->sreloc;
01572 srel->size += p->count * sizeof (Elf64_External_Rela);
01573 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
01574 info->flags |= DF_TEXTREL;
01575
01576 }
01577 }
01578 }
01579
01580 local_got = elf_local_got_refcounts (ibfd);
01581 if (!local_got)
01582 continue;
01583
01584 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
01585 locsymcount = symtab_hdr->sh_info;
01586 end_local_got = local_got + locsymcount;
01587 local_tls_type = elf64_x86_64_local_got_tls_type (ibfd);
01588 s = htab->sgot;
01589 srel = htab->srelgot;
01590 for (; local_got < end_local_got; ++local_got, ++local_tls_type)
01591 {
01592 if (*local_got > 0)
01593 {
01594 *local_got = s->size;
01595 s->size += GOT_ENTRY_SIZE;
01596 if (*local_tls_type == GOT_TLS_GD)
01597 s->size += GOT_ENTRY_SIZE;
01598 if (info->shared
01599 || *local_tls_type == GOT_TLS_GD
01600 || *local_tls_type == GOT_TLS_IE)
01601 srel->size += sizeof (Elf64_External_Rela);
01602 }
01603 else
01604 *local_got = (bfd_vma) -1;
01605 }
01606 }
01607
01608 if (htab->tls_ld_got.refcount > 0)
01609 {
01610
01611
01612 htab->tls_ld_got.offset = htab->sgot->size;
01613 htab->sgot->size += 2 * GOT_ENTRY_SIZE;
01614 htab->srelgot->size += sizeof (Elf64_External_Rela);
01615 }
01616 else
01617 htab->tls_ld_got.offset = -1;
01618
01619
01620
01621 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, (PTR) info);
01622
01623
01624
01625 relocs = FALSE;
01626 for (s = dynobj->sections; s != NULL; s = s->next)
01627 {
01628 if ((s->flags & SEC_LINKER_CREATED) == 0)
01629 continue;
01630
01631 if (s == htab->splt
01632 || s == htab->sgot
01633 || s == htab->sgotplt)
01634 {
01635
01636
01637 }
01638 else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
01639 {
01640 if (s->size != 0 && s != htab->srelplt)
01641 relocs = TRUE;
01642
01643
01644
01645 s->reloc_count = 0;
01646 }
01647 else
01648 {
01649
01650 continue;
01651 }
01652
01653 if (s->size == 0)
01654 {
01655
01656
01657
01658
01659
01660
01661
01662
01663
01664
01665 _bfd_strip_section_from_output (info, s);
01666 continue;
01667 }
01668
01669
01670
01671
01672
01673
01674 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
01675 if (s->contents == NULL)
01676 return FALSE;
01677 }
01678
01679 if (htab->elf.dynamic_sections_created)
01680 {
01681
01682
01683
01684
01685
01686 #define add_dynamic_entry(TAG, VAL) \
01687 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
01688
01689 if (info->executable)
01690 {
01691 if (!add_dynamic_entry (DT_DEBUG, 0))
01692 return FALSE;
01693 }
01694
01695 if (htab->splt->size != 0)
01696 {
01697 if (!add_dynamic_entry (DT_PLTGOT, 0)
01698 || !add_dynamic_entry (DT_PLTRELSZ, 0)
01699 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
01700 || !add_dynamic_entry (DT_JMPREL, 0))
01701 return FALSE;
01702 }
01703
01704 if (relocs)
01705 {
01706 if (!add_dynamic_entry (DT_RELA, 0)
01707 || !add_dynamic_entry (DT_RELASZ, 0)
01708 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
01709 return FALSE;
01710
01711
01712
01713 if ((info->flags & DF_TEXTREL) == 0)
01714 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs,
01715 (PTR) info);
01716
01717 if ((info->flags & DF_TEXTREL) != 0)
01718 {
01719 if (!add_dynamic_entry (DT_TEXTREL, 0))
01720 return FALSE;
01721 }
01722 }
01723 }
01724 #undef add_dynamic_entry
01725
01726 return TRUE;
01727 }
01728
01729
01730
01731
01732
01733 static bfd_vma
01734 dtpoff_base (struct bfd_link_info *info)
01735 {
01736
01737 if (elf_hash_table (info)->tls_sec == NULL)
01738 return 0;
01739 return elf_hash_table (info)->tls_sec->vma;
01740 }
01741
01742
01743
01744
01745 static bfd_vma
01746 tpoff (struct bfd_link_info *info, bfd_vma address)
01747 {
01748 struct elf_link_hash_table *htab = elf_hash_table (info);
01749
01750
01751 if (htab->tls_sec == NULL)
01752 return 0;
01753 return address - htab->tls_size - htab->tls_sec->vma;
01754 }
01755
01756
01757
01758
01759 static bfd_boolean
01760 is_32bit_relative_branch (bfd_byte *contents, bfd_vma offset)
01761 {
01762
01763
01764
01765
01766 return ((offset > 0
01767 && (contents [offset - 1] == 0xe8
01768 || contents [offset - 1] == 0xe9))
01769 || (offset > 1
01770 && contents [offset - 2] == 0x0f
01771 && (contents [offset - 1] & 0xf0) == 0x80));
01772 }
01773
01774
01775
01776 static bfd_boolean
01777 elf64_x86_64_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
01778 bfd *input_bfd, asection *input_section,
01779 bfd_byte *contents, Elf_Internal_Rela *relocs,
01780 Elf_Internal_Sym *local_syms,
01781 asection **local_sections)
01782 {
01783 struct elf64_x86_64_link_hash_table *htab;
01784 Elf_Internal_Shdr *symtab_hdr;
01785 struct elf_link_hash_entry **sym_hashes;
01786 bfd_vma *local_got_offsets;
01787 Elf_Internal_Rela *rel;
01788 Elf_Internal_Rela *relend;
01789
01790 if (info->relocatable)
01791 return TRUE;
01792
01793 htab = elf64_x86_64_hash_table (info);
01794 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
01795 sym_hashes = elf_sym_hashes (input_bfd);
01796 local_got_offsets = elf_local_got_offsets (input_bfd);
01797
01798 rel = relocs;
01799 relend = relocs + input_section->reloc_count;
01800 for (; rel < relend; rel++)
01801 {
01802 unsigned int r_type;
01803 reloc_howto_type *howto;
01804 unsigned long r_symndx;
01805 struct elf_link_hash_entry *h;
01806 Elf_Internal_Sym *sym;
01807 asection *sec;
01808 bfd_vma off;
01809 bfd_vma relocation;
01810 bfd_boolean unresolved_reloc;
01811 bfd_reloc_status_type r;
01812 int tls_type;
01813
01814 r_type = ELF64_R_TYPE (rel->r_info);
01815 if (r_type == (int) R_X86_64_GNU_VTINHERIT
01816 || r_type == (int) R_X86_64_GNU_VTENTRY)
01817 continue;
01818
01819 if (r_type >= R_X86_64_max)
01820 {
01821 bfd_set_error (bfd_error_bad_value);
01822 return FALSE;
01823 }
01824
01825 howto = x86_64_elf_howto_table + r_type;
01826 r_symndx = ELF64_R_SYM (rel->r_info);
01827 h = NULL;
01828 sym = NULL;
01829 sec = NULL;
01830 unresolved_reloc = FALSE;
01831 if (r_symndx < symtab_hdr->sh_info)
01832 {
01833 sym = local_syms + r_symndx;
01834 sec = local_sections[r_symndx];
01835
01836 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
01837 }
01838 else
01839 {
01840 bfd_boolean warned;
01841
01842 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
01843 r_symndx, symtab_hdr, sym_hashes,
01844 h, sec, relocation,
01845 unresolved_reloc, warned);
01846 }
01847
01848
01849 switch (r_type)
01850 {
01851 case R_X86_64_GOT32:
01852
01853
01854 case R_X86_64_GOTPCREL:
01855
01856 if (htab->sgot == NULL)
01857 abort ();
01858
01859 if (h != NULL)
01860 {
01861 bfd_boolean dyn;
01862
01863 off = h->got.offset;
01864 dyn = htab->elf.dynamic_sections_created;
01865
01866 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, h)
01867 || (info->shared
01868 && SYMBOL_REFERENCES_LOCAL (info, h))
01869 || (ELF_ST_VISIBILITY (h->other)
01870 && h->root.type == bfd_link_hash_undefweak))
01871 {
01872
01873
01874
01875
01876
01877
01878
01879
01880
01881
01882
01883 if ((off & 1) != 0)
01884 off &= ~1;
01885 else
01886 {
01887 bfd_put_64 (output_bfd, relocation,
01888 htab->sgot->contents + off);
01889 h->got.offset |= 1;
01890 }
01891 }
01892 else
01893 unresolved_reloc = FALSE;
01894 }
01895 else
01896 {
01897 if (local_got_offsets == NULL)
01898 abort ();
01899
01900 off = local_got_offsets[r_symndx];
01901
01902
01903
01904
01905 if ((off & 1) != 0)
01906 off &= ~1;
01907 else
01908 {
01909 bfd_put_64 (output_bfd, relocation,
01910 htab->sgot->contents + off);
01911
01912 if (info->shared)
01913 {
01914 asection *s;
01915 Elf_Internal_Rela outrel;
01916 bfd_byte *loc;
01917
01918
01919
01920 s = htab->srelgot;
01921 if (s == NULL)
01922 abort ();
01923
01924 outrel.r_offset = (htab->sgot->output_section->vma
01925 + htab->sgot->output_offset
01926 + off);
01927 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
01928 outrel.r_addend = relocation;
01929 loc = s->contents;
01930 loc += s->reloc_count++ * sizeof (Elf64_External_Rela);
01931 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
01932 }
01933
01934 local_got_offsets[r_symndx] |= 1;
01935 }
01936 }
01937
01938 if (off >= (bfd_vma) -2)
01939 abort ();
01940
01941 relocation = htab->sgot->output_section->vma
01942 + htab->sgot->output_offset + off;
01943 if (r_type != R_X86_64_GOTPCREL)
01944 relocation -= htab->sgotplt->output_section->vma
01945 - htab->sgotplt->output_offset;
01946
01947 break;
01948
01949 case R_X86_64_PLT32:
01950
01951
01952
01953
01954
01955 if (h == NULL)
01956 break;
01957
01958 if (h->plt.offset == (bfd_vma) -1
01959 || htab->splt == NULL)
01960 {
01961
01962
01963
01964 break;
01965 }
01966
01967 relocation = (htab->splt->output_section->vma
01968 + htab->splt->output_offset
01969 + h->plt.offset);
01970 unresolved_reloc = FALSE;
01971 break;
01972
01973 case R_X86_64_PC8:
01974 case R_X86_64_PC16:
01975 case R_X86_64_PC32:
01976 if (info->shared
01977 && !SYMBOL_REFERENCES_LOCAL (info, h)
01978 && (input_section->flags & SEC_ALLOC) != 0
01979 && (input_section->flags & SEC_READONLY) != 0
01980 && (!h->def_regular
01981 || r_type != R_X86_64_PC32
01982 || h->type != STT_FUNC
01983 || ELF_ST_VISIBILITY (h->other) != STV_PROTECTED
01984 || !is_32bit_relative_branch (contents,
01985 rel->r_offset)))
01986 {
01987 if (h->def_regular
01988 && r_type == R_X86_64_PC32
01989 && h->type == STT_FUNC
01990 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED)
01991 (*_bfd_error_handler)
01992 (_("%B: relocation R_X86_64_PC32 against protected function `%s' can not be used when making a shared object"),
01993 input_bfd, h->root.root.string);
01994 else
01995 (*_bfd_error_handler)
01996 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"),
01997 input_bfd, x86_64_elf_howto_table[r_type].name,
01998 h->root.root.string);
01999 bfd_set_error (bfd_error_bad_value);
02000 return FALSE;
02001 }
02002
02003
02004 case R_X86_64_8:
02005 case R_X86_64_16:
02006 case R_X86_64_32:
02007 case R_X86_64_64:
02008
02009
02010
02011
02012
02013
02014 if (r_symndx == 0
02015 || (input_section->flags & SEC_ALLOC) == 0)
02016 break;
02017
02018 if ((info->shared
02019 && (h == NULL
02020 || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
02021 || h->root.type != bfd_link_hash_undefweak)
02022 && ((r_type != R_X86_64_PC8
02023 && r_type != R_X86_64_PC16
02024 && r_type != R_X86_64_PC32)
02025 || !SYMBOL_CALLS_LOCAL (info, h)))
02026 || (ELIMINATE_COPY_RELOCS
02027 && !info->shared
02028 && h != NULL
02029 && h->dynindx != -1
02030 && !h->non_got_ref
02031 && ((h->def_dynamic
02032 && !h->def_regular)
02033 || h->root.type == bfd_link_hash_undefweak
02034 || h->root.type == bfd_link_hash_undefined)))
02035 {
02036 Elf_Internal_Rela outrel;
02037 bfd_byte *loc;
02038 bfd_boolean skip, relocate;
02039 asection *sreloc;
02040
02041
02042
02043
02044 skip = FALSE;
02045 relocate = FALSE;
02046
02047 outrel.r_offset =
02048 _bfd_elf_section_offset (output_bfd, info, input_section,
02049 rel->r_offset);
02050 if (outrel.r_offset == (bfd_vma) -1)
02051 skip = TRUE;
02052 else if (outrel.r_offset == (bfd_vma) -2)
02053 skip = TRUE, relocate = TRUE;
02054
02055 outrel.r_offset += (input_section->output_section->vma
02056 + input_section->output_offset);
02057
02058 if (skip)
02059 memset (&outrel, 0, sizeof outrel);
02060
02061
02062
02063 else if (h != NULL
02064 && h->dynindx != -1
02065 && (r_type == R_X86_64_PC8
02066 || r_type == R_X86_64_PC16
02067 || r_type == R_X86_64_PC32
02068 || !info->shared
02069 || !info->symbolic
02070 || !h->def_regular))
02071 {
02072 outrel.r_info = ELF64_R_INFO (h->dynindx, r_type);
02073 outrel.r_addend = rel->r_addend;
02074 }
02075 else
02076 {
02077
02078 if (r_type == R_X86_64_64)
02079 {
02080 relocate = TRUE;
02081 outrel.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
02082 outrel.r_addend = relocation + rel->r_addend;
02083 }
02084 else
02085 {
02086 long sindx;
02087
02088 if (bfd_is_abs_section (sec))
02089 sindx = 0;
02090 else if (sec == NULL || sec->owner == NULL)
02091 {
02092 bfd_set_error (bfd_error_bad_value);
02093 return FALSE;
02094 }
02095 else
02096 {
02097 asection *osec;
02098
02099 osec = sec->output_section;
02100 sindx = elf_section_data (osec)->dynindx;
02101 BFD_ASSERT (sindx > 0);
02102 }
02103
02104 outrel.r_info = ELF64_R_INFO (sindx, r_type);
02105 outrel.r_addend = relocation + rel->r_addend;
02106 }
02107 }
02108
02109 sreloc = elf_section_data (input_section)->sreloc;
02110 if (sreloc == NULL)
02111 abort ();
02112
02113 loc = sreloc->contents;
02114 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
02115 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
02116
02117
02118
02119
02120
02121 if (! relocate)
02122 continue;
02123 }
02124
02125 break;
02126
02127 case R_X86_64_TLSGD:
02128 case R_X86_64_GOTTPOFF:
02129 r_type = elf64_x86_64_tls_transition (info, r_type, h == NULL);
02130 tls_type = GOT_UNKNOWN;
02131 if (h == NULL && local_got_offsets)
02132 tls_type = elf64_x86_64_local_got_tls_type (input_bfd) [r_symndx];
02133 else if (h != NULL)
02134 {
02135 tls_type = elf64_x86_64_hash_entry (h)->tls_type;
02136 if (!info->shared && h->dynindx == -1 && tls_type == GOT_TLS_IE)
02137 r_type = R_X86_64_TPOFF32;
02138 }
02139 if (r_type == R_X86_64_TLSGD)
02140 {
02141 if (tls_type == GOT_TLS_IE)
02142 r_type = R_X86_64_GOTTPOFF;
02143 }
02144
02145 if (r_type == R_X86_64_TPOFF32)
02146 {
02147 BFD_ASSERT (! unresolved_reloc);
02148 if (ELF64_R_TYPE (rel->r_info) == R_X86_64_TLSGD)
02149 {
02150 unsigned int i;
02151 static unsigned char tlsgd[8]
02152 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
02153
02154
02155
02156
02157
02158
02159
02160 BFD_ASSERT (rel->r_offset >= 4);
02161 for (i = 0; i < 4; i++)
02162 BFD_ASSERT (bfd_get_8 (input_bfd,
02163 contents + rel->r_offset - 4 + i)
02164 == tlsgd[i]);
02165 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
02166 for (i = 0; i < 4; i++)
02167 BFD_ASSERT (bfd_get_8 (input_bfd,
02168 contents + rel->r_offset + 4 + i)
02169 == tlsgd[i+4]);
02170 BFD_ASSERT (rel + 1 < relend);
02171 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
02172 memcpy (contents + rel->r_offset - 4,
02173 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x8d\x80\0\0\0",
02174 16);
02175 bfd_put_32 (output_bfd, tpoff (info, relocation),
02176 contents + rel->r_offset + 8);
02177
02178 rel++;
02179 continue;
02180 }
02181 else
02182 {
02183 unsigned int val, type, reg;
02184
02185
02186
02187
02188
02189
02190
02191
02192
02193 BFD_ASSERT (rel->r_offset >= 3);
02194 val = bfd_get_8 (input_bfd, contents + rel->r_offset - 3);
02195 BFD_ASSERT (val == 0x48 || val == 0x4c);
02196 type = bfd_get_8 (input_bfd, contents + rel->r_offset - 2);
02197 BFD_ASSERT (type == 0x8b || type == 0x03);
02198 reg = bfd_get_8 (input_bfd, contents + rel->r_offset - 1);
02199 BFD_ASSERT ((reg & 0xc7) == 5);
02200 reg >>= 3;
02201 BFD_ASSERT (rel->r_offset + 4 <= input_section->size);
02202 if (type == 0x8b)
02203 {
02204
02205 if (val == 0x4c)
02206 bfd_put_8 (output_bfd, 0x49,
02207 contents + rel->r_offset - 3);
02208 bfd_put_8 (output_bfd, 0xc7,
02209 contents + rel->r_offset - 2);
02210 bfd_put_8 (output_bfd, 0xc0 | reg,
02211 contents + rel->r_offset - 1);
02212 }
02213 else if (reg == 4)
02214 {
02215
02216
02217 if (val == 0x4c)
02218 bfd_put_8 (output_bfd, 0x49,
02219 contents + rel->r_offset - 3);
02220 bfd_put_8 (output_bfd, 0x81,
02221 contents + rel->r_offset - 2);
02222 bfd_put_8 (output_bfd, 0xc0 | reg,
02223 contents + rel->r_offset - 1);
02224 }
02225 else
02226 {
02227
02228 if (val == 0x4c)
02229 bfd_put_8 (output_bfd, 0x4d,
02230 contents + rel->r_offset - 3);
02231 bfd_put_8 (output_bfd, 0x8d,
02232 contents + rel->r_offset - 2);
02233 bfd_put_8 (output_bfd, 0x80 | reg | (reg << 3),
02234 contents + rel->r_offset - 1);
02235 }
02236 bfd_put_32 (output_bfd, tpoff (info, relocation),
02237 contents + rel->r_offset);
02238 continue;
02239 }
02240 }
02241
02242 if (htab->sgot == NULL)
02243 abort ();
02244
02245 if (h != NULL)
02246 off = h->got.offset;
02247 else
02248 {
02249 if (local_got_offsets == NULL)
02250 abort ();
02251
02252 off = local_got_offsets[r_symndx];
02253 }
02254
02255 if ((off & 1) != 0)
02256 off &= ~1;
02257 else
02258 {
02259 Elf_Internal_Rela outrel;
02260 bfd_byte *loc;
02261 int dr_type, indx;
02262
02263 if (htab->srelgot == NULL)
02264 abort ();
02265
02266 outrel.r_offset = (htab->sgot->output_section->vma
02267 + htab->sgot->output_offset + off);
02268
02269 indx = h && h->dynindx != -1 ? h->dynindx : 0;
02270 if (r_type == R_X86_64_TLSGD)
02271 dr_type = R_X86_64_DTPMOD64;
02272 else
02273 dr_type = R_X86_64_TPOFF64;
02274
02275 bfd_put_64 (output_bfd, 0, htab->sgot->contents + off);
02276 outrel.r_addend = 0;
02277 if (dr_type == R_X86_64_TPOFF64 && indx == 0)
02278 outrel.r_addend = relocation - dtpoff_base (info);
02279 outrel.r_info = ELF64_R_INFO (indx, dr_type);
02280
02281 loc = htab->srelgot->contents;
02282 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
02283 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
02284
02285 if (r_type == R_X86_64_TLSGD)
02286 {
02287 if (indx == 0)
02288 {
02289 BFD_ASSERT (! unresolved_reloc);
02290 bfd_put_64 (output_bfd,
02291 relocation - dtpoff_base (info),
02292 htab->sgot->contents + off + GOT_ENTRY_SIZE);
02293 }
02294 else
02295 {
02296 bfd_put_64 (output_bfd, 0,
02297 htab->sgot->contents + off + GOT_ENTRY_SIZE);
02298 outrel.r_info = ELF64_R_INFO (indx,
02299 R_X86_64_DTPOFF64);
02300 outrel.r_offset += GOT_ENTRY_SIZE;
02301 htab->srelgot->reloc_count++;
02302 loc += sizeof (Elf64_External_Rela);
02303 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
02304 }
02305 }
02306
02307 if (h != NULL)
02308 h->got.offset |= 1;
02309 else
02310 local_got_offsets[r_symndx] |= 1;
02311 }
02312
02313 if (off >= (bfd_vma) -2)
02314 abort ();
02315 if (r_type == ELF64_R_TYPE (rel->r_info))
02316 {
02317 relocation = htab->sgot->output_section->vma
02318 + htab->sgot->output_offset + off;
02319 unresolved_reloc = FALSE;
02320 }
02321 else
02322 {
02323 unsigned int i;
02324 static unsigned char tlsgd[8]
02325 = { 0x66, 0x48, 0x8d, 0x3d, 0x66, 0x66, 0x48, 0xe8 };
02326
02327
02328
02329
02330
02331
02332
02333 BFD_ASSERT (rel->r_offset >= 4);
02334 for (i = 0; i < 4; i++)
02335 BFD_ASSERT (bfd_get_8 (input_bfd,
02336 contents + rel->r_offset - 4 + i)
02337 == tlsgd[i]);
02338 BFD_ASSERT (rel->r_offset + 12 <= input_section->size);
02339 for (i = 0; i < 4; i++)
02340 BFD_ASSERT (bfd_get_8 (input_bfd,
02341 contents + rel->r_offset + 4 + i)
02342 == tlsgd[i+4]);
02343 BFD_ASSERT (rel + 1 < relend);
02344 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
02345 memcpy (contents + rel->r_offset - 4,
02346 "\x64\x48\x8b\x04\x25\0\0\0\0\x48\x03\x05\0\0\0",
02347 16);
02348
02349 relocation = (htab->sgot->output_section->vma
02350 + htab->sgot->output_offset + off
02351 - rel->r_offset
02352 - input_section->output_section->vma
02353 - input_section->output_offset
02354 - 12);
02355 bfd_put_32 (output_bfd, relocation,
02356 contents + rel->r_offset + 8);
02357
02358 rel++;
02359 continue;
02360 }
02361 break;
02362
02363 case R_X86_64_TLSLD:
02364 if (! info->shared)
02365 {
02366
02367
02368
02369
02370
02371 BFD_ASSERT (rel->r_offset >= 3);
02372 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 3)
02373 == 0x48);
02374 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 2)
02375 == 0x8d);
02376 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset - 1)
02377 == 0x3d);
02378 BFD_ASSERT (rel->r_offset + 9 <= input_section->size);
02379 BFD_ASSERT (bfd_get_8 (input_bfd, contents + rel->r_offset + 4)
02380 == 0xe8);
02381 BFD_ASSERT (rel + 1 < relend);
02382 BFD_ASSERT (ELF64_R_TYPE (rel[1].r_info) == R_X86_64_PLT32);
02383 memcpy (contents + rel->r_offset - 3,
02384 "\x66\x66\x66\x64\x48\x8b\x04\x25\0\0\0", 12);
02385
02386 rel++;
02387 continue;
02388 }
02389
02390 if (htab->sgot == NULL)
02391 abort ();
02392
02393 off = htab->tls_ld_got.offset;
02394 if (off & 1)
02395 off &= ~1;
02396 else
02397 {
02398 Elf_Internal_Rela outrel;
02399 bfd_byte *loc;
02400
02401 if (htab->srelgot == NULL)
02402 abort ();
02403
02404 outrel.r_offset = (htab->sgot->output_section->vma
02405 + htab->sgot->output_offset + off);
02406
02407 bfd_put_64 (output_bfd, 0,
02408 htab->sgot->contents + off);
02409 bfd_put_64 (output_bfd, 0,
02410 htab->sgot->contents + off + GOT_ENTRY_SIZE);
02411 outrel.r_info = ELF64_R_INFO (0, R_X86_64_DTPMOD64);
02412 outrel.r_addend = 0;
02413 loc = htab->srelgot->contents;
02414 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
02415 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
02416 htab->tls_ld_got.offset |= 1;
02417 }
02418 relocation = htab->sgot->output_section->vma
02419 + htab->sgot->output_offset + off;
02420 unresolved_reloc = FALSE;
02421 break;
02422
02423 case R_X86_64_DTPOFF32:
02424 if (info->shared || (input_section->flags & SEC_CODE) == 0)
02425 relocation -= dtpoff_base (info);
02426 else
02427 relocation = tpoff (info, relocation);
02428 break;
02429
02430 case R_X86_64_TPOFF32:
02431 BFD_ASSERT (! info->shared);
02432 relocation = tpoff (info, relocation);
02433 break;
02434
02435 default:
02436 break;
02437 }
02438
02439
02440
02441
02442 if (unresolved_reloc
02443 && !((input_section->flags & SEC_DEBUGGING) != 0
02444 && h->def_dynamic))
02445 (*_bfd_error_handler)
02446 (_("%B(%A+0x%lx): unresolvable relocation against symbol `%s'"),
02447 input_bfd,
02448 input_section,
02449 (long) rel->r_offset,
02450 h->root.root.string);
02451
02452 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
02453 contents, rel->r_offset,
02454 relocation, rel->r_addend);
02455
02456 if (r != bfd_reloc_ok)
02457 {
02458 const char *name;
02459
02460 if (h != NULL)
02461 name = h->root.root.string;
02462 else
02463 {
02464 name = bfd_elf_string_from_elf_section (input_bfd,
02465 symtab_hdr->sh_link,
02466 sym->st_name);
02467 if (name == NULL)
02468 return FALSE;
02469 if (*name == '\0')
02470 name = bfd_section_name (input_bfd, sec);
02471 }
02472
02473 if (r == bfd_reloc_overflow)
02474 {
02475 if (h != NULL
02476 && h->root.type == bfd_link_hash_undefweak
02477 && howto->pc_relative)
02478
02479 continue;
02480
02481 if (! ((*info->callbacks->reloc_overflow)
02482 (info, (h ? &h->root : NULL), name, howto->name,
02483 (bfd_vma) 0, input_bfd, input_section,
02484 rel->r_offset)))
02485 return FALSE;
02486 }
02487 else
02488 {
02489 (*_bfd_error_handler)
02490 (_("%B(%A+0x%lx): reloc against `%s': error %d"),
02491 input_bfd, input_section,
02492 (long) rel->r_offset, name, (int) r);
02493 return FALSE;
02494 }
02495 }
02496 }
02497
02498 return TRUE;
02499 }
02500
02501
02502
02503
02504 static bfd_boolean
02505 elf64_x86_64_finish_dynamic_symbol (bfd *output_bfd,
02506 struct bfd_link_info *info,
02507 struct elf_link_hash_entry *h,
02508 Elf_Internal_Sym *sym)
02509 {
02510 struct elf64_x86_64_link_hash_table *htab;
02511
02512 htab = elf64_x86_64_hash_table (info);
02513
02514 if (h->plt.offset != (bfd_vma) -1)
02515 {
02516 bfd_vma plt_index;
02517 bfd_vma got_offset;
02518 Elf_Internal_Rela rela;
02519 bfd_byte *loc;
02520
02521
02522
02523 if (h->dynindx == -1
02524 || htab->splt == NULL
02525 || htab->sgotplt == NULL
02526 || htab->srelplt == NULL)
02527 abort ();
02528
02529
02530
02531
02532
02533 plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
02534
02535
02536
02537
02538 got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
02539
02540
02541 memcpy (htab->splt->contents + h->plt.offset, elf64_x86_64_plt_entry,
02542 PLT_ENTRY_SIZE);
02543
02544
02545
02546
02547
02548
02549 bfd_put_32 (output_bfd,
02550 (htab->sgotplt->output_section->vma
02551 + htab->sgotplt->output_offset
02552 + got_offset
02553 - htab->splt->output_section->vma
02554 - htab->splt->output_offset
02555 - h->plt.offset
02556 - 6),
02557 htab->splt->contents + h->plt.offset + 2);
02558
02559 bfd_put_32 (output_bfd, plt_index,
02560 htab->splt->contents + h->plt.offset + 7);
02561
02562 bfd_put_32 (output_bfd, - (h->plt.offset + PLT_ENTRY_SIZE),
02563 htab->splt->contents + h->plt.offset + 12);
02564
02565
02566
02567 bfd_put_64 (output_bfd, (htab->splt->output_section->vma
02568 + htab->splt->output_offset
02569 + h->plt.offset + 6),
02570 htab->sgotplt->contents + got_offset);
02571
02572
02573 rela.r_offset = (htab->sgotplt->output_section->vma
02574 + htab->sgotplt->output_offset
02575 + got_offset);
02576 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_JUMP_SLOT);
02577 rela.r_addend = 0;
02578 loc = htab->srelplt->contents + plt_index * sizeof (Elf64_External_Rela);
02579 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
02580
02581 if (!h->def_regular)
02582 {
02583
02584
02585
02586
02587
02588
02589
02590
02591 sym->st_shndx = SHN_UNDEF;
02592 if (!h->pointer_equality_needed)
02593 sym->st_value = 0;
02594 }
02595 }
02596
02597 if (h->got.offset != (bfd_vma) -1
02598 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_GD
02599 && elf64_x86_64_hash_entry (h)->tls_type != GOT_TLS_IE)
02600 {
02601 Elf_Internal_Rela rela;
02602 bfd_byte *loc;
02603
02604
02605
02606 if (htab->sgot == NULL || htab->srelgot == NULL)
02607 abort ();
02608
02609 rela.r_offset = (htab->sgot->output_section->vma
02610 + htab->sgot->output_offset
02611 + (h->got.offset &~ (bfd_vma) 1));
02612
02613
02614
02615
02616
02617
02618 if (info->shared
02619 && SYMBOL_REFERENCES_LOCAL (info, h))
02620 {
02621 BFD_ASSERT((h->got.offset & 1) != 0);
02622 rela.r_info = ELF64_R_INFO (0, R_X86_64_RELATIVE);
02623 rela.r_addend = (h->root.u.def.value
02624 + h->root.u.def.section->output_section->vma
02625 + h->root.u.def.section->output_offset);
02626 }
02627 else
02628 {
02629 BFD_ASSERT((h->got.offset & 1) == 0);
02630 bfd_put_64 (output_bfd, (bfd_vma) 0,
02631 htab->sgot->contents + h->got.offset);
02632 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_GLOB_DAT);
02633 rela.r_addend = 0;
02634 }
02635
02636 loc = htab->srelgot->contents;
02637 loc += htab->srelgot->reloc_count++ * sizeof (Elf64_External_Rela);
02638 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
02639 }
02640
02641 if (h->needs_copy)
02642 {
02643 Elf_Internal_Rela rela;
02644 bfd_byte *loc;
02645
02646
02647
02648 if (h->dynindx == -1
02649 || (h->root.type != bfd_link_hash_defined
02650 && h->root.type != bfd_link_hash_defweak)
02651 || htab->srelbss == NULL)
02652 abort ();
02653
02654 rela.r_offset = (h->root.u.def.value
02655 + h->root.u.def.section->output_section->vma
02656 + h->root.u.def.section->output_offset);
02657 rela.r_info = ELF64_R_INFO (h->dynindx, R_X86_64_COPY);
02658 rela.r_addend = 0;
02659 loc = htab->srelbss->contents;
02660 loc += htab->srelbss->reloc_count++ * sizeof (Elf64_External_Rela);
02661 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
02662 }
02663
02664
02665 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
02666 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
02667 sym->st_shndx = SHN_ABS;
02668
02669 return TRUE;
02670 }
02671
02672
02673
02674
02675 static enum elf_reloc_type_class
02676 elf64_x86_64_reloc_type_class (const Elf_Internal_Rela *rela)
02677 {
02678 switch ((int) ELF64_R_TYPE (rela->r_info))
02679 {
02680 case R_X86_64_RELATIVE:
02681 return reloc_class_relative;
02682 case R_X86_64_JUMP_SLOT:
02683 return reloc_class_plt;
02684 case R_X86_64_COPY:
02685 return reloc_class_copy;
02686 default:
02687 return reloc_class_normal;
02688 }
02689 }
02690
02691
02692
02693 static bfd_boolean
02694 elf64_x86_64_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
02695 {
02696 struct elf64_x86_64_link_hash_table *htab;
02697 bfd *dynobj;
02698 asection *sdyn;
02699
02700 htab = elf64_x86_64_hash_table (info);
02701 dynobj = htab->elf.dynobj;
02702 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
02703
02704 if (htab->elf.dynamic_sections_created)
02705 {
02706 Elf64_External_Dyn *dyncon, *dynconend;
02707
02708 if (sdyn == NULL || htab->sgot == NULL)
02709 abort ();
02710
02711 dyncon = (Elf64_External_Dyn *) sdyn->contents;
02712 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
02713 for (; dyncon < dynconend; dyncon++)
02714 {
02715 Elf_Internal_Dyn dyn;
02716 asection *s;
02717
02718 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
02719
02720 switch (dyn.d_tag)
02721 {
02722 default:
02723 continue;
02724
02725 case DT_PLTGOT:
02726 s = htab->sgotplt;
02727 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
02728 break;
02729
02730 case DT_JMPREL:
02731 dyn.d_un.d_ptr = htab->srelplt->output_section->vma;
02732 break;
02733
02734 case DT_PLTRELSZ:
02735 s = htab->srelplt->output_section;
02736 dyn.d_un.d_val = s->size;
02737 break;
02738
02739 case DT_RELASZ:
02740
02741
02742
02743
02744
02745
02746
02747 if (htab->srelplt != NULL)
02748 {
02749 s = htab->srelplt->output_section;
02750 dyn.d_un.d_val -= s->size;
02751 }
02752 break;
02753 }
02754
02755 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
02756 }
02757
02758
02759 if (htab->splt && htab->splt->size > 0)
02760 {
02761
02762 memcpy (htab->splt->contents, elf64_x86_64_plt0_entry,
02763 PLT_ENTRY_SIZE);
02764
02765
02766 bfd_put_32 (output_bfd,
02767 (htab->sgotplt->output_section->vma
02768 + htab->sgotplt->output_offset
02769 + 8
02770 - htab->splt->output_section->vma
02771 - htab->splt->output_offset
02772 - 6),
02773 htab->splt->contents + 2);
02774
02775
02776 bfd_put_32 (output_bfd,
02777 (htab->sgotplt->output_section->vma
02778 + htab->sgotplt->output_offset
02779 + 16
02780 - htab->splt->output_section->vma
02781 - htab->splt->output_offset
02782 - 12),
02783 htab->splt->contents + 8);
02784
02785 elf_section_data (htab->splt->output_section)->this_hdr.sh_entsize =
02786 PLT_ENTRY_SIZE;
02787 }
02788 }
02789
02790 if (htab->sgotplt)
02791 {
02792
02793 if (htab->sgotplt->size > 0)
02794 {
02795
02796
02797 if (sdyn == NULL)
02798 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents);
02799 else
02800 bfd_put_64 (output_bfd,
02801 sdyn->output_section->vma + sdyn->output_offset,
02802 htab->sgotplt->contents);
02803
02804 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE);
02805 bfd_put_64 (output_bfd, (bfd_vma) 0, htab->sgotplt->contents + GOT_ENTRY_SIZE*2);
02806 }
02807
02808 elf_section_data (htab->sgotplt->output_section)->this_hdr.sh_entsize =
02809 GOT_ENTRY_SIZE;
02810 }
02811
02812 if (htab->sgot && htab->sgot->size > 0)
02813 elf_section_data (htab->sgot->output_section)->this_hdr.sh_entsize
02814 = GOT_ENTRY_SIZE;
02815
02816 return TRUE;
02817 }
02818
02819
02820
02821
02822 static bfd_vma
02823 elf64_x86_64_plt_sym_val (bfd_vma i, const asection *plt,
02824 const arelent *rel ATTRIBUTE_UNUSED)
02825 {
02826 return plt->vma + (i + 1) * PLT_ENTRY_SIZE;
02827 }
02828
02829
02830
02831
02832 static bfd_boolean
02833 elf64_x86_64_section_from_shdr (bfd *abfd, Elf_Internal_Shdr *hdr, const char *name)
02834 {
02835 if (hdr->sh_type != SHT_X86_64_UNWIND)
02836 return FALSE;
02837
02838 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
02839 return FALSE;
02840
02841 return TRUE;
02842 }
02843
02844 #define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
02845 #define TARGET_LITTLE_NAME "elf64-x86-64"
02846 #define ELF_ARCH bfd_arch_i386
02847 #define ELF_MACHINE_CODE EM_X86_64
02848 #define ELF_MAXPAGESIZE 0x100000
02849
02850 #define elf_backend_can_gc_sections 1
02851 #define elf_backend_can_refcount 1
02852 #define elf_backend_want_got_plt 1
02853 #define elf_backend_plt_readonly 1
02854 #define elf_backend_want_plt_sym 0
02855 #define elf_backend_got_header_size (GOT_ENTRY_SIZE*3)
02856 #define elf_backend_rela_normal 1
02857
02858 #define elf_info_to_howto elf64_x86_64_info_to_howto
02859
02860 #define bfd_elf64_bfd_link_hash_table_create \
02861 elf64_x86_64_link_hash_table_create
02862 #define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
02863
02864 #define elf_backend_adjust_dynamic_symbol elf64_x86_64_adjust_dynamic_symbol
02865 #define elf_backend_check_relocs elf64_x86_64_check_relocs
02866 #define elf_backend_copy_indirect_symbol elf64_x86_64_copy_indirect_symbol
02867 #define elf_backend_create_dynamic_sections elf64_x86_64_create_dynamic_sections
02868 #define elf_backend_finish_dynamic_sections elf64_x86_64_finish_dynamic_sections
02869 #define elf_backend_finish_dynamic_symbol elf64_x86_64_finish_dynamic_symbol
02870 #define elf_backend_gc_mark_hook elf64_x86_64_gc_mark_hook
02871 #define elf_backend_gc_sweep_hook elf64_x86_64_gc_sweep_hook
02872 #define elf_backend_grok_prstatus elf64_x86_64_grok_prstatus
02873 #define elf_backend_grok_psinfo elf64_x86_64_grok_psinfo
02874 #define elf_backend_reloc_type_class elf64_x86_64_reloc_type_class
02875 #define elf_backend_relocate_section elf64_x86_64_relocate_section
02876 #define elf_backend_size_dynamic_sections elf64_x86_64_size_dynamic_sections
02877 #define elf_backend_plt_sym_val elf64_x86_64_plt_sym_val
02878 #define elf_backend_object_p elf64_x86_64_elf_object_p
02879 #define bfd_elf64_mkobject elf64_x86_64_mkobject
02880
02881 #define elf_backend_section_from_shdr \
02882 elf64_x86_64_section_from_shdr
02883
02884 #include "elf64-target.h"