Coverage Report

Created: 2026-11-21 09:00

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