Update year range in copyright notice of binutils files
[binutils-gdb.git] / bfd / coff-x86_64.c
1 /* BFD back-end for AMD 64 COFF files.
2 Copyright (C) 2006-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 Written by Kai Tietz, OneVision Software GmbH&CoKg. */
22
23 /* Note we have to make sure not to include headers twice.
24 Not all headers are wrapped in #ifdef guards, so we define
25 PEI_HEADERS to prevent double including here. */
26 #ifndef PEI_HEADERS
27 #include "sysdep.h"
28 #include "bfd.h"
29 #include "libbfd.h"
30 #include "coff/x86_64.h"
31 #include "coff/internal.h"
32 #include "libcoff.h"
33 #include "libiberty.h"
34 #endif
35
36 #define BADMAG(x) AMD64BADMAG(x)
37
38 #ifdef COFF_WITH_pex64
39 # undef AOUTSZ
40 # define AOUTSZ PEPAOUTSZ
41 # define PEAOUTHDR PEPAOUTHDR
42 #endif
43
44 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
45
46 /* The page size is a guess based on ELF. */
47
48 #define COFF_PAGE_SIZE 0x1000
49
50 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
51 #define OCTETS_PER_BYTE(ABFD, SEC) 1
52
53 /* For some reason when using AMD COFF the value stored in the .text
54 section for a reference to a common symbol is the value itself plus
55 any desired offset. Ian Taylor, Cygnus Support. */
56
57 /* If we are producing relocatable output, we need to do some
58 adjustments to the object file that are not done by the
59 bfd_perform_relocation function. This function is called by every
60 reloc type to make any required adjustments. */
61
62 static bfd_reloc_status_type
63 coff_amd64_reloc (bfd *abfd,
64 arelent *reloc_entry,
65 asymbol *symbol,
66 void * data,
67 asection *input_section ATTRIBUTE_UNUSED,
68 bfd *output_bfd,
69 char **error_message ATTRIBUTE_UNUSED)
70 {
71 symvalue diff;
72
73 #if !defined (COFF_WITH_PE)
74 if (output_bfd == NULL)
75 return bfd_reloc_continue;
76 #endif
77
78 if (bfd_is_com_section (symbol->section))
79 {
80 #if !defined (COFF_WITH_PE)
81 /* We are relocating a common symbol. The current value in the
82 object file is ORIG + OFFSET, where ORIG is the value of the
83 common symbol as seen by the object file when it was compiled
84 (this may be zero if the symbol was undefined) and OFFSET is
85 the offset into the common symbol (normally zero, but may be
86 non-zero when referring to a field in a common structure).
87 ORIG is the negative of reloc_entry->addend, which is set by
88 the CALC_ADDEND macro below. We want to replace the value in
89 the object file with NEW + OFFSET, where NEW is the value of
90 the common symbol which we are going to put in the final
91 object file. NEW is symbol->value. */
92 diff = symbol->value + reloc_entry->addend;
93 #else
94 /* In PE mode, we do not offset the common symbol. */
95 diff = reloc_entry->addend;
96 #endif
97 }
98 else
99 {
100 /* For some reason bfd_perform_relocation always effectively
101 ignores the addend for a COFF target when producing
102 relocatable output. This seems to be always wrong for 386
103 COFF, so we handle the addend here instead. */
104 #if defined (COFF_WITH_PE)
105 if (output_bfd == NULL)
106 {
107 if (symbol->flags & BSF_WEAK)
108 diff = reloc_entry->addend - symbol->value;
109 else
110 diff = -reloc_entry->addend;
111 }
112 else
113 #endif
114 diff = reloc_entry->addend;
115 }
116
117 #if defined (COFF_WITH_PE)
118 if (output_bfd == NULL)
119 {
120 /* PC relative relocations are off by their size. */
121 if (reloc_entry->howto->pc_relative)
122 diff -= bfd_get_reloc_size (reloc_entry->howto);
123
124 if (reloc_entry->howto->type >= R_AMD64_PCRLONG_1
125 && reloc_entry->howto->type <= R_AMD64_PCRLONG_5)
126 diff -= reloc_entry->howto->type - R_AMD64_PCRLONG;
127 }
128
129 if (reloc_entry->howto->type == R_AMD64_IMAGEBASE
130 && output_bfd == NULL)
131 {
132 bfd *obfd = input_section->output_section->owner;
133 struct bfd_link_info *link_info;
134 struct bfd_link_hash_entry *h;
135 switch (bfd_get_flavour (obfd))
136 {
137 case bfd_target_coff_flavour:
138 diff -= pe_data (obfd)->pe_opthdr.ImageBase;
139 break;
140 case bfd_target_elf_flavour:
141 /* Subtract __ImageBase. */
142 link_info = _bfd_get_link_info (obfd);
143 if (link_info == NULL)
144 return bfd_reloc_dangerous;
145 h = bfd_link_hash_lookup (link_info->hash, "__ImageBase",
146 false, false, false);
147 if (h == NULL)
148 return bfd_reloc_dangerous;
149 while (h->type == bfd_link_hash_indirect)
150 h = h->u.i.link;
151 /* ELF symbols in relocatable files are section relative,
152 but in nonrelocatable files they are virtual addresses. */
153 diff -= (h->u.def.value
154 + h->u.def.section->output_offset
155 + h->u.def.section->output_section->vma);
156 break;
157 default:
158 break;
159 }
160 }
161 #endif
162
163 #define DOIT(x) \
164 x = ((x & ~howto->dst_mask) | (((x & howto->src_mask) + diff) & howto->dst_mask))
165
166 if (diff != 0)
167 {
168 reloc_howto_type *howto = reloc_entry->howto;
169 bfd_size_type octets = (reloc_entry->address
170 * OCTETS_PER_BYTE (abfd, input_section));
171 unsigned char *addr = (unsigned char *) data + octets;
172
173 if (!bfd_reloc_offset_in_range (howto, abfd, input_section, octets))
174 return bfd_reloc_outofrange;
175
176 switch (howto->size)
177 {
178 case 0:
179 {
180 char x = bfd_get_8 (abfd, addr);
181 DOIT (x);
182 bfd_put_8 (abfd, x, addr);
183 }
184 break;
185
186 case 1:
187 {
188 short x = bfd_get_16 (abfd, addr);
189 DOIT (x);
190 bfd_put_16 (abfd, (bfd_vma) x, addr);
191 }
192 break;
193
194 case 2:
195 {
196 long x = bfd_get_32 (abfd, addr);
197 DOIT (x);
198 bfd_put_32 (abfd, (bfd_vma) x, addr);
199 }
200 break;
201
202 case 4:
203 {
204 bfd_uint64_t x = bfd_get_64 (abfd, addr);
205 DOIT (x);
206 bfd_put_64 (abfd, x, addr);
207 }
208 break;
209
210 default:
211 bfd_set_error (bfd_error_bad_value);
212 return bfd_reloc_notsupported;
213 }
214 }
215
216 /* Now let bfd_perform_relocation finish everything up. */
217 return bfd_reloc_continue;
218 }
219
220 #if defined(COFF_WITH_PE)
221 /* Return TRUE if this relocation should appear in the output .reloc
222 section. */
223
224 static bool
225 in_reloc_p (bfd *abfd ATTRIBUTE_UNUSED, reloc_howto_type *howto)
226 {
227 return ! howto->pc_relative && howto->type != R_AMD64_IMAGEBASE
228 && howto->type != R_AMD64_SECREL;
229 }
230 #endif /* COFF_WITH_PE */
231
232 #ifndef PCRELOFFSET
233 #define PCRELOFFSET true
234 #endif
235
236 static reloc_howto_type howto_table[] =
237 {
238 EMPTY_HOWTO (0),
239 HOWTO (R_AMD64_DIR64, /* type 1*/
240 0, /* rightshift */
241 4, /* size (0 = byte, 1 = short, 2 = long, 4 = long long) */
242 64, /* bitsize */
243 false, /* pc_relative */
244 0, /* bitpos */
245 complain_overflow_bitfield, /* complain_on_overflow */
246 coff_amd64_reloc, /* special_function */
247 "IMAGE_REL_AMD64_ADDR64", /* name */
248 true, /* partial_inplace */
249 0xffffffffffffffffll, /* src_mask */
250 0xffffffffffffffffll, /* dst_mask */
251 true), /* pcrel_offset */
252 HOWTO (R_AMD64_DIR32, /* type 2 */
253 0, /* rightshift */
254 2, /* size (0 = byte, 1 = short, 2 = long) */
255 32, /* bitsize */
256 false, /* pc_relative */
257 0, /* bitpos */
258 complain_overflow_bitfield, /* complain_on_overflow */
259 coff_amd64_reloc, /* special_function */
260 "IMAGE_REL_AMD64_ADDR32", /* name */
261 true, /* partial_inplace */
262 0xffffffff, /* src_mask */
263 0xffffffff, /* dst_mask */
264 true), /* pcrel_offset */
265 /* PE IMAGE_REL_AMD64_ADDR32NB relocation (3). */
266 HOWTO (R_AMD64_IMAGEBASE, /* type */
267 0, /* rightshift */
268 2, /* size (0 = byte, 1 = short, 2 = long) */
269 32, /* bitsize */
270 false, /* pc_relative */
271 0, /* bitpos */
272 complain_overflow_bitfield, /* complain_on_overflow */
273 coff_amd64_reloc, /* special_function */
274 "IMAGE_REL_AMD64_ADDR32NB", /* name */
275 true, /* partial_inplace */
276 0xffffffff, /* src_mask */
277 0xffffffff, /* dst_mask */
278 false), /* pcrel_offset */
279 /* 32-bit longword PC relative relocation (4). */
280 HOWTO (R_AMD64_PCRLONG, /* type 4 */
281 0, /* rightshift */
282 2, /* size (0 = byte, 1 = short, 2 = long) */
283 32, /* bitsize */
284 true, /* pc_relative */
285 0, /* bitpos */
286 complain_overflow_signed, /* complain_on_overflow */
287 coff_amd64_reloc, /* special_function */
288 "IMAGE_REL_AMD64_REL32", /* name */
289 true, /* partial_inplace */
290 0xffffffff, /* src_mask */
291 0xffffffff, /* dst_mask */
292 PCRELOFFSET), /* pcrel_offset */
293
294 HOWTO (R_AMD64_PCRLONG_1, /* type 5 */
295 0, /* rightshift */
296 2, /* size (0 = byte, 1 = short, 2 = long) */
297 32, /* bitsize */
298 true, /* pc_relative */
299 0, /* bitpos */
300 complain_overflow_signed, /* complain_on_overflow */
301 coff_amd64_reloc, /* special_function */
302 "IMAGE_REL_AMD64_REL32_1", /* name */
303 true, /* partial_inplace */
304 0xffffffff, /* src_mask */
305 0xffffffff, /* dst_mask */
306 PCRELOFFSET), /* pcrel_offset */
307 HOWTO (R_AMD64_PCRLONG_2, /* type 6 */
308 0, /* rightshift */
309 2, /* size (0 = byte, 1 = short, 2 = long) */
310 32, /* bitsize */
311 true, /* pc_relative */
312 0, /* bitpos */
313 complain_overflow_signed, /* complain_on_overflow */
314 coff_amd64_reloc, /* special_function */
315 "IMAGE_REL_AMD64_REL32_2", /* name */
316 true, /* partial_inplace */
317 0xffffffff, /* src_mask */
318 0xffffffff, /* dst_mask */
319 PCRELOFFSET), /* pcrel_offset */
320 HOWTO (R_AMD64_PCRLONG_3, /* type 7 */
321 0, /* rightshift */
322 2, /* size (0 = byte, 1 = short, 2 = long) */
323 32, /* bitsize */
324 true, /* pc_relative */
325 0, /* bitpos */
326 complain_overflow_signed, /* complain_on_overflow */
327 coff_amd64_reloc, /* special_function */
328 "IMAGE_REL_AMD64_REL32_3", /* name */
329 true, /* partial_inplace */
330 0xffffffff, /* src_mask */
331 0xffffffff, /* dst_mask */
332 PCRELOFFSET), /* pcrel_offset */
333 HOWTO (R_AMD64_PCRLONG_4, /* type 8 */
334 0, /* rightshift */
335 2, /* size (0 = byte, 1 = short, 2 = long) */
336 32, /* bitsize */
337 true, /* pc_relative */
338 0, /* bitpos */
339 complain_overflow_signed, /* complain_on_overflow */
340 coff_amd64_reloc, /* special_function */
341 "IMAGE_REL_AMD64_REL32_4", /* name */
342 true, /* partial_inplace */
343 0xffffffff, /* src_mask */
344 0xffffffff, /* dst_mask */
345 PCRELOFFSET), /* pcrel_offset */
346 HOWTO (R_AMD64_PCRLONG_5, /* type 9 */
347 0, /* rightshift */
348 2, /* size (0 = byte, 1 = short, 2 = long) */
349 32, /* bitsize */
350 true, /* pc_relative */
351 0, /* bitpos */
352 complain_overflow_signed, /* complain_on_overflow */
353 coff_amd64_reloc, /* special_function */
354 "IMAGE_REL_AMD64_REL32_5", /* name */
355 true, /* partial_inplace */
356 0xffffffff, /* src_mask */
357 0xffffffff, /* dst_mask */
358 PCRELOFFSET), /* pcrel_offset */
359 EMPTY_HOWTO (10), /* R_AMD64_SECTION 10 */
360 #if defined(COFF_WITH_PE)
361 /* 32-bit longword section relative relocation (11). */
362 HOWTO (R_AMD64_SECREL, /* type */
363 0, /* rightshift */
364 2, /* size (0 = byte, 1 = short, 2 = long) */
365 32, /* bitsize */
366 false, /* pc_relative */
367 0, /* bitpos */
368 complain_overflow_bitfield, /* complain_on_overflow */
369 coff_amd64_reloc, /* special_function */
370 "IMAGE_REL_AMD64_SECREL", /* name */
371 true, /* partial_inplace */
372 0xffffffff, /* src_mask */
373 0xffffffff, /* dst_mask */
374 true), /* pcrel_offset */
375 #else
376 EMPTY_HOWTO (11),
377 #endif
378 EMPTY_HOWTO (12),
379 EMPTY_HOWTO (13),
380 #ifndef DONT_EXTEND_AMD64
381 HOWTO (R_AMD64_PCRQUAD,
382 0, /* rightshift */
383 4, /* size (0 = byte, 1 = short, 2 = long) */
384 64, /* bitsize */
385 true, /* pc_relative */
386 0, /* bitpos */
387 complain_overflow_signed, /* complain_on_overflow */
388 coff_amd64_reloc, /* special_function */
389 "R_X86_64_PC64", /* name */
390 true, /* partial_inplace */
391 0xffffffffffffffffll, /* src_mask */
392 0xffffffffffffffffll, /* dst_mask */
393 PCRELOFFSET), /* pcrel_offset */
394 #else
395 EMPTY_HOWTO (14),
396 #endif
397 /* Byte relocation (15). */
398 HOWTO (R_RELBYTE, /* type */
399 0, /* rightshift */
400 0, /* size (0 = byte, 1 = short, 2 = long) */
401 8, /* bitsize */
402 false, /* pc_relative */
403 0, /* bitpos */
404 complain_overflow_bitfield, /* complain_on_overflow */
405 coff_amd64_reloc, /* special_function */
406 "R_X86_64_8", /* name */
407 true, /* partial_inplace */
408 0x000000ff, /* src_mask */
409 0x000000ff, /* dst_mask */
410 PCRELOFFSET), /* pcrel_offset */
411 /* 16-bit word relocation (16). */
412 HOWTO (R_RELWORD, /* type */
413 0, /* rightshift */
414 1, /* size (0 = byte, 1 = short, 2 = long) */
415 16, /* bitsize */
416 false, /* pc_relative */
417 0, /* bitpos */
418 complain_overflow_bitfield, /* complain_on_overflow */
419 coff_amd64_reloc, /* special_function */
420 "R_X86_64_16", /* name */
421 true, /* partial_inplace */
422 0x0000ffff, /* src_mask */
423 0x0000ffff, /* dst_mask */
424 PCRELOFFSET), /* pcrel_offset */
425 /* 32-bit longword relocation (17). */
426 HOWTO (R_RELLONG, /* type */
427 0, /* rightshift */
428 2, /* size (0 = byte, 1 = short, 2 = long) */
429 32, /* bitsize */
430 false, /* pc_relative */
431 0, /* bitpos */
432 complain_overflow_bitfield, /* complain_on_overflow */
433 coff_amd64_reloc, /* special_function */
434 "R_X86_64_32S", /* name */
435 true, /* partial_inplace */
436 0xffffffff, /* src_mask */
437 0xffffffff, /* dst_mask */
438 PCRELOFFSET), /* pcrel_offset */
439 /* Byte PC relative relocation (18). */
440 HOWTO (R_PCRBYTE, /* type */
441 0, /* rightshift */
442 0, /* size (0 = byte, 1 = short, 2 = long) */
443 8, /* bitsize */
444 true, /* pc_relative */
445 0, /* bitpos */
446 complain_overflow_signed, /* complain_on_overflow */
447 coff_amd64_reloc, /* special_function */
448 "R_X86_64_PC8", /* name */
449 true, /* partial_inplace */
450 0x000000ff, /* src_mask */
451 0x000000ff, /* dst_mask */
452 PCRELOFFSET), /* pcrel_offset */
453 /* 16-bit word PC relative relocation (19). */
454 HOWTO (R_PCRWORD, /* type */
455 0, /* rightshift */
456 1, /* size (0 = byte, 1 = short, 2 = long) */
457 16, /* bitsize */
458 true, /* pc_relative */
459 0, /* bitpos */
460 complain_overflow_signed, /* complain_on_overflow */
461 coff_amd64_reloc, /* special_function */
462 "R_X86_64_PC16", /* name */
463 true, /* partial_inplace */
464 0x0000ffff, /* src_mask */
465 0x0000ffff, /* dst_mask */
466 PCRELOFFSET), /* pcrel_offset */
467 /* 32-bit longword PC relative relocation (20). */
468 HOWTO (R_PCRLONG, /* type */
469 0, /* rightshift */
470 2, /* size (0 = byte, 1 = short, 2 = long) */
471 32, /* bitsize */
472 true, /* pc_relative */
473 0, /* bitpos */
474 complain_overflow_signed, /* complain_on_overflow */
475 coff_amd64_reloc, /* special_function */
476 "R_X86_64_PC32", /* name */
477 true, /* partial_inplace */
478 0xffffffff, /* src_mask */
479 0xffffffff, /* dst_mask */
480 PCRELOFFSET) /* pcrel_offset */
481 };
482
483 #define NUM_HOWTOS ARRAY_SIZE (howto_table)
484
485 /* Turn a howto into a reloc nunmber */
486
487 #define SELECT_RELOC(x,howto) { x.r_type = howto->type; }
488 #define I386 1 /* Customize coffcode.h */
489 #define AMD64 1
490
491 #define RTYPE2HOWTO(cache_ptr, dst) \
492 ((cache_ptr)->howto = \
493 ((dst)->r_type < NUM_HOWTOS) \
494 ? howto_table + (dst)->r_type \
495 : NULL)
496
497 /* For 386 COFF a STYP_NOLOAD | STYP_BSS section is part of a shared
498 library. On some other COFF targets STYP_BSS is normally
499 STYP_NOLOAD. */
500 #define BSS_NOLOAD_IS_SHARED_LIBRARY
501
502 /* Compute the addend of a reloc. If the reloc is to a common symbol,
503 the object file contains the value of the common symbol. By the
504 time this is called, the linker may be using a different symbol
505 from a different object file with a different value. Therefore, we
506 hack wildly to locate the original symbol from this file so that we
507 can make the correct adjustment. This macro sets coffsym to the
508 symbol from the original file, and uses it to set the addend value
509 correctly. If this is not a common symbol, the usual addend
510 calculation is done, except that an additional tweak is needed for
511 PC relative relocs.
512 FIXME: This macro refers to symbols and asect; these are from the
513 calling function, not the macro arguments. */
514
515 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
516 { \
517 coff_symbol_type *coffsym = NULL; \
518 \
519 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
520 coffsym = (obj_symbols (abfd) \
521 + (cache_ptr->sym_ptr_ptr - symbols)); \
522 else if (ptr) \
523 coffsym = coff_symbol_from (ptr); \
524 \
525 if (coffsym != NULL \
526 && coffsym->native->u.syment.n_scnum == 0) \
527 cache_ptr->addend = - coffsym->native->u.syment.n_value; \
528 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
529 && ptr->section != NULL) \
530 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
531 else \
532 cache_ptr->addend = 0; \
533 if (ptr && reloc.r_type < NUM_HOWTOS \
534 && howto_table[reloc.r_type].pc_relative) \
535 cache_ptr->addend += asect->vma; \
536 }
537
538 /* We use the special COFF backend linker. For normal AMD64 COFF, we
539 can use the generic relocate_section routine. For PE, we need our
540 own routine. */
541
542 #if !defined(COFF_WITH_PE)
543
544 #define coff_relocate_section _bfd_coff_generic_relocate_section
545
546 #else /* COFF_WITH_PE */
547
548 /* The PE relocate section routine. The only difference between this
549 and the regular routine is that we don't want to do anything for a
550 relocatable link. */
551
552 static bool
553 coff_pe_amd64_relocate_section (bfd *output_bfd,
554 struct bfd_link_info *info,
555 bfd *input_bfd,
556 asection *input_section,
557 bfd_byte *contents,
558 struct internal_reloc *relocs,
559 struct internal_syment *syms,
560 asection **sections)
561 {
562 if (bfd_link_relocatable (info))
563 return true;
564
565 return _bfd_coff_generic_relocate_section (output_bfd, info, input_bfd,input_section, contents,relocs, syms, sections);
566 }
567
568 #define coff_relocate_section coff_pe_amd64_relocate_section
569
570 #endif /* COFF_WITH_PE */
571
572 /* Convert an rtype to howto for the COFF backend linker. */
573
574 static reloc_howto_type *
575 coff_amd64_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
576 asection *sec,
577 struct internal_reloc *rel,
578 struct coff_link_hash_entry *h,
579 struct internal_syment *sym,
580 bfd_vma *addendp)
581 {
582 reloc_howto_type *howto;
583
584 if (rel->r_type >= NUM_HOWTOS)
585 {
586 bfd_set_error (bfd_error_bad_value);
587 return NULL;
588 }
589 howto = howto_table + rel->r_type;
590
591 #if defined(COFF_WITH_PE)
592 /* Cancel out code in _bfd_coff_generic_relocate_section. */
593 *addendp = 0;
594 if (rel->r_type >= R_AMD64_PCRLONG_1 && rel->r_type <= R_AMD64_PCRLONG_5)
595 {
596 *addendp -= (bfd_vma)(rel->r_type - R_AMD64_PCRLONG);
597 rel->r_type = R_AMD64_PCRLONG;
598 }
599 #endif
600
601 if (howto->pc_relative)
602 *addendp += sec->vma;
603
604 if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
605 {
606 /* This is a common symbol. The section contents include the
607 size (sym->n_value) as an addend. The relocate_section
608 function will be adding in the final value of the symbol. We
609 need to subtract out the current size in order to get the
610 correct result. */
611 BFD_ASSERT (h != NULL);
612
613 #if !defined(COFF_WITH_PE)
614 /* I think we *do* want to bypass this. If we don't, I have
615 seen some data parameters get the wrong relocation address.
616 If I link two versions with and without this section bypassed
617 and then do a binary comparison, the addresses which are
618 different can be looked up in the map. The case in which
619 this section has been bypassed has addresses which correspond
620 to values I can find in the map. */
621 *addendp -= sym->n_value;
622 #endif
623 }
624
625 #if !defined(COFF_WITH_PE)
626 /* If the output symbol is common (in which case this must be a
627 relocatable link), we need to add in the final size of the
628 common symbol. */
629 if (h != NULL && h->root.type == bfd_link_hash_common)
630 *addendp += h->root.u.c.size;
631 #endif
632
633 #if defined(COFF_WITH_PE)
634 if (howto->pc_relative)
635 {
636 #ifndef DONT_EXTEND_AMD64
637 if (rel->r_type == R_AMD64_PCRQUAD)
638 *addendp -= 8;
639 else
640 #endif
641 *addendp -= 4;
642
643 /* If the symbol is defined, then the generic code is going to
644 add back the symbol value in order to cancel out an
645 adjustment it made to the addend. However, we set the addend
646 to 0 at the start of this function. We need to adjust here,
647 to avoid the adjustment the generic code will make. FIXME:
648 This is getting a bit hackish. */
649 if (sym != NULL && sym->n_scnum != 0)
650 *addendp -= sym->n_value;
651 }
652
653 if (rel->r_type == R_AMD64_IMAGEBASE
654 && (bfd_get_flavour (sec->output_section->owner) == bfd_target_coff_flavour))
655 *addendp -= pe_data (sec->output_section->owner)->pe_opthdr.ImageBase;
656
657 if (rel->r_type == R_AMD64_SECREL)
658 {
659 bfd_vma osect_vma;
660
661 if (h && (h->root.type == bfd_link_hash_defined
662 || h->root.type == bfd_link_hash_defweak))
663 osect_vma = h->root.u.def.section->output_section->vma;
664 else
665 {
666 asection *s;
667 int i;
668
669 /* Sigh, the only way to get the section to offset against
670 is to find it the hard way. */
671 for (s = abfd->sections, i = 1; i < sym->n_scnum; i++)
672 s = s->next;
673
674 osect_vma = s->output_section->vma;
675 }
676
677 *addendp -= osect_vma;
678 }
679 #endif
680
681 return howto;
682 }
683
684 #define coff_bfd_reloc_type_lookup coff_amd64_reloc_type_lookup
685 #define coff_bfd_reloc_name_lookup coff_amd64_reloc_name_lookup
686
687 static reloc_howto_type *
688 coff_amd64_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, bfd_reloc_code_real_type code)
689 {
690 switch (code)
691 {
692 case BFD_RELOC_RVA:
693 return howto_table + R_AMD64_IMAGEBASE;
694 case BFD_RELOC_32:
695 return howto_table + R_AMD64_DIR32;
696 case BFD_RELOC_64:
697 return howto_table + R_AMD64_DIR64;
698 case BFD_RELOC_64_PCREL:
699 #ifndef DONT_EXTEND_AMD64
700 return howto_table + R_AMD64_PCRQUAD;
701 #else
702 /* Fall through. */
703 #endif
704 case BFD_RELOC_32_PCREL:
705 return howto_table + R_AMD64_PCRLONG;
706 case BFD_RELOC_X86_64_32S:
707 return howto_table + R_RELLONG;
708 case BFD_RELOC_16:
709 return howto_table + R_RELWORD;
710 case BFD_RELOC_16_PCREL:
711 return howto_table + R_PCRWORD;
712 case BFD_RELOC_8:
713 return howto_table + R_RELBYTE;
714 case BFD_RELOC_8_PCREL:
715 return howto_table + R_PCRBYTE;
716 #if defined(COFF_WITH_PE)
717 case BFD_RELOC_32_SECREL:
718 return howto_table + R_AMD64_SECREL;
719 #endif
720 default:
721 BFD_FAIL ();
722 return 0;
723 }
724 }
725
726 static reloc_howto_type *
727 coff_amd64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
728 const char *r_name)
729 {
730 unsigned int i;
731
732 for (i = 0; i < NUM_HOWTOS; i++)
733 if (howto_table[i].name != NULL
734 && strcasecmp (howto_table[i].name, r_name) == 0)
735 return &howto_table[i];
736
737 return NULL;
738 }
739
740 #define coff_rtype_to_howto coff_amd64_rtype_to_howto
741
742 #ifdef TARGET_UNDERSCORE
743
744 /* If amd64 gcc uses underscores for symbol names, then it does not use
745 a leading dot for local labels, so if TARGET_UNDERSCORE is defined
746 we treat all symbols starting with L as local. */
747
748 static bool
749 coff_amd64_is_local_label_name (bfd *abfd, const char *name)
750 {
751 if (name[0] == 'L')
752 return true;
753
754 return _bfd_coff_is_local_label_name (abfd, name);
755 }
756
757 #define coff_bfd_is_local_label_name coff_amd64_is_local_label_name
758
759 #endif /* TARGET_UNDERSCORE */
760
761 #ifndef bfd_pe_print_pdata
762 #define bfd_pe_print_pdata NULL
763 #endif
764
765 #include "coffcode.h"
766
767 #ifdef PE
768 #define amd64coff_object_p pe_bfd_object_p
769 #else
770 #define amd64coff_object_p coff_object_p
771 #endif
772
773 const bfd_target
774 #ifdef TARGET_SYM
775 TARGET_SYM =
776 #else
777 x86_64_coff_vec =
778 #endif
779 {
780 #ifdef TARGET_NAME
781 TARGET_NAME,
782 #else
783 "coff-x86-64", /* Name. */
784 #endif
785 bfd_target_coff_flavour,
786 BFD_ENDIAN_LITTLE, /* Data byte order is little. */
787 BFD_ENDIAN_LITTLE, /* Header byte order is little. */
788
789 (HAS_RELOC | EXEC_P /* Object flags. */
790 | HAS_LINENO | HAS_DEBUG
791 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS),
792
793 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* Section flags. */
794 #if defined(COFF_WITH_PE)
795 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_READONLY | SEC_DEBUGGING
796 #endif
797 | SEC_CODE | SEC_DATA | SEC_EXCLUDE ),
798
799 #ifdef TARGET_UNDERSCORE
800 TARGET_UNDERSCORE, /* Leading underscore. */
801 #else
802 0, /* Leading underscore. */
803 #endif
804 '/', /* Ar_pad_char. */
805 15, /* Ar_max_namelen. */
806 0, /* match priority. */
807 TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
808
809 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
810 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
811 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Data. */
812 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
813 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
814 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Hdrs. */
815
816 /* Note that we allow an object file to be treated as a core file as well. */
817 { /* bfd_check_format. */
818 _bfd_dummy_target,
819 amd64coff_object_p,
820 bfd_generic_archive_p,
821 amd64coff_object_p
822 },
823 { /* bfd_set_format. */
824 _bfd_bool_bfd_false_error,
825 coff_mkobject,
826 _bfd_generic_mkarchive,
827 _bfd_bool_bfd_false_error
828 },
829 { /* bfd_write_contents. */
830 _bfd_bool_bfd_false_error,
831 coff_write_object_contents,
832 _bfd_write_archive_contents,
833 _bfd_bool_bfd_false_error
834 },
835
836 BFD_JUMP_TABLE_GENERIC (coff),
837 BFD_JUMP_TABLE_COPY (coff),
838 BFD_JUMP_TABLE_CORE (_bfd_nocore),
839 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
840 BFD_JUMP_TABLE_SYMBOLS (coff),
841 BFD_JUMP_TABLE_RELOCS (coff),
842 BFD_JUMP_TABLE_WRITE (coff),
843 BFD_JUMP_TABLE_LINK (coff),
844 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
845
846 NULL,
847
848 COFF_SWAP_TABLE
849 };
850
851 /* Entry for big object files. */
852
853 #ifdef COFF_WITH_PE_BIGOBJ
854 const bfd_target
855 TARGET_SYM_BIG =
856 {
857 TARGET_NAME_BIG,
858 bfd_target_coff_flavour,
859 BFD_ENDIAN_LITTLE, /* Data byte order is little. */
860 BFD_ENDIAN_LITTLE, /* Header byte order is little. */
861
862 (HAS_RELOC | EXEC_P /* Object flags. */
863 | HAS_LINENO | HAS_DEBUG
864 | HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED | BFD_COMPRESS | BFD_DECOMPRESS),
865
866 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC /* Section flags. */
867 #if defined(COFF_WITH_PE)
868 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_READONLY | SEC_DEBUGGING
869 #endif
870 | SEC_CODE | SEC_DATA | SEC_EXCLUDE ),
871
872 #ifdef TARGET_UNDERSCORE
873 TARGET_UNDERSCORE, /* Leading underscore. */
874 #else
875 0, /* Leading underscore. */
876 #endif
877 '/', /* Ar_pad_char. */
878 15, /* Ar_max_namelen. */
879 0, /* match priority. */
880 TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
881
882 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
883 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
884 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Data. */
885 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
886 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
887 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* Hdrs. */
888
889 /* Note that we allow an object file to be treated as a core file as well. */
890 { /* bfd_check_format. */
891 _bfd_dummy_target,
892 amd64coff_object_p,
893 bfd_generic_archive_p,
894 amd64coff_object_p
895 },
896 { /* bfd_set_format. */
897 _bfd_bool_bfd_false_error,
898 coff_mkobject,
899 _bfd_generic_mkarchive,
900 _bfd_bool_bfd_false_error
901 },
902 { /* bfd_write_contents. */
903 _bfd_bool_bfd_false_error,
904 coff_write_object_contents,
905 _bfd_write_archive_contents,
906 _bfd_bool_bfd_false_error
907 },
908
909 BFD_JUMP_TABLE_GENERIC (coff),
910 BFD_JUMP_TABLE_COPY (coff),
911 BFD_JUMP_TABLE_CORE (_bfd_nocore),
912 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
913 BFD_JUMP_TABLE_SYMBOLS (coff),
914 BFD_JUMP_TABLE_RELOCS (coff),
915 BFD_JUMP_TABLE_WRITE (coff),
916 BFD_JUMP_TABLE_LINK (coff),
917 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
918
919 NULL,
920
921 &bigobj_swap_table
922 };
923 #endif