Coverage Report

Created: 2025-06-24 06:45

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