reloc.c 192 KB
Newer Older
Richard Henderson's avatar
Richard Henderson committed
1
/* BFD support for handling relocation entries.
2
   Copyright (C) 1990-2019 Free Software Foundation, Inc.
Richard Henderson's avatar
Richard Henderson committed
3
4
   Written by Cygnus Support.

5
   This file is part of BFD, the Binary File Descriptor library.
Richard Henderson's avatar
Richard Henderson committed
6

7
8
   This program is free software; you can redistribute it and/or modify
   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.
Richard Henderson's avatar
Richard Henderson committed
11

12
13
14
15
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
Richard Henderson's avatar
Richard Henderson committed
16

17
18
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
19
20
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */
Richard Henderson's avatar
Richard Henderson committed
21
22
23
24
25
26
27

/*
SECTION
	Relocations

	BFD maintains relocations in much the same way it maintains
	symbols: they are left alone until required, then read in
28
	en-masse and translated into an internal form.  A common
Richard Henderson's avatar
Richard Henderson committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
	routine <<bfd_perform_relocation>> acts upon the
	canonical form to do the fixup.

	Relocations are maintained on a per section basis,
	while symbols are maintained on a per BFD basis.

	All that a back end has to do to fit the BFD interface is to create
	a <<struct reloc_cache_entry>> for each relocation
	in a particular section, and fill in the right bits of the structures.

@menu
@* typedef arelent::
@* howto manager::
@end menu

*/

/* DO compile in the reloc_code name table from libbfd.h.  */
#define _BFD_MAKE_TABLE_bfd_reloc_code_real

#include "sysdep.h"
Alan Modra's avatar
   
Alan Modra committed
50
#include "bfd.h"
Richard Henderson's avatar
Richard Henderson committed
51
52
#include "bfdlink.h"
#include "libbfd.h"
53
#include "bfdver.h"
Richard Henderson's avatar
Richard Henderson committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
DOCDD
INODE
	typedef arelent, howto manager, Relocations, Relocations

SUBSECTION
	typedef arelent

	This is the structure of a relocation entry:

CODE_FRAGMENT
.
.typedef enum bfd_reloc_status
.{
68
69
70
.  {* No errors detected.  Note - the value 2 is used so that it
.     will not be mistaken for the boolean TRUE or FALSE values.  *}
.  bfd_reloc_ok = 2,
Richard Henderson's avatar
Richard Henderson committed
71
.
Nick Clifton's avatar
Nick Clifton committed
72
.  {* The relocation was performed, but there was an overflow.  *}
Richard Henderson's avatar
Richard Henderson committed
73
74
.  bfd_reloc_overflow,
.
Nick Clifton's avatar
Nick Clifton committed
75
.  {* The address to relocate was not within the section supplied.  *}
Richard Henderson's avatar
Richard Henderson committed
76
77
.  bfd_reloc_outofrange,
.
Nick Clifton's avatar
Nick Clifton committed
78
.  {* Used by special functions.  *}
Richard Henderson's avatar
Richard Henderson committed
79
80
.  bfd_reloc_continue,
.
Nick Clifton's avatar
Nick Clifton committed
81
.  {* Unsupported relocation size requested.  *}
Richard Henderson's avatar
Richard Henderson committed
82
83
.  bfd_reloc_notsupported,
.
Nick Clifton's avatar
Nick Clifton committed
84
.  {* Unused.  *}
Richard Henderson's avatar
Richard Henderson committed
85
86
.  bfd_reloc_other,
.
Nick Clifton's avatar
Nick Clifton committed
87
.  {* The symbol to relocate against was undefined.  *}
Richard Henderson's avatar
Richard Henderson committed
88
89
.  bfd_reloc_undefined,
.
90
91
92
.  {* The relocation was performed, but may not be ok.  If this type is
.     returned, the error_message argument to bfd_perform_relocation
.     will be set.  *}
Richard Henderson's avatar
Richard Henderson committed
93
94
95
96
.  bfd_reloc_dangerous
. }
. bfd_reloc_status_type;
.
Alan Modra's avatar
Alan Modra committed
97
.typedef const struct reloc_howto_struct reloc_howto_type;
Richard Henderson's avatar
Richard Henderson committed
98
99
100
.
.typedef struct reloc_cache_entry
.{
Nick Clifton's avatar
Nick Clifton committed
101
.  {* A pointer into the canonical table of pointers.  *}
Andrew Cagney's avatar
Andrew Cagney committed
102
.  struct bfd_symbol **sym_ptr_ptr;
Richard Henderson's avatar
Richard Henderson committed
103
.
Nick Clifton's avatar
Nick Clifton committed
104
.  {* offset in section.  *}
Richard Henderson's avatar
Richard Henderson committed
105
106
.  bfd_size_type address;
.
Nick Clifton's avatar
Nick Clifton committed
107
.  {* addend for relocation value.  *}
Richard Henderson's avatar
Richard Henderson committed
108
109
.  bfd_vma addend;
.
Nick Clifton's avatar
Nick Clifton committed
110
.  {* Pointer to how to perform the required relocation.  *}
Richard Henderson's avatar
Richard Henderson committed
111
112
.  reloc_howto_type *howto;
.
Nick Clifton's avatar
Nick Clifton committed
113
114
115
.}
.arelent;
.
Richard Henderson's avatar
Richard Henderson committed
116
117
118
119
120
*/

/*
DESCRIPTION

Alan Modra's avatar
Alan Modra committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
	Here is a description of each of the fields within an <<arelent>>:

	o <<sym_ptr_ptr>>

	The symbol table pointer points to a pointer to the symbol
	associated with the relocation request.  It is the pointer
	into the table returned by the back end's
	<<canonicalize_symtab>> action. @xref{Symbols}. The symbol is
	referenced through a pointer to a pointer so that tools like
	the linker can fix up all the symbols of the same name by
	modifying only one pointer. The relocation routine looks in
	the symbol and uses the base of the section the symbol is
	attached to and the value of the symbol as the initial
	relocation offset. If the symbol pointer is zero, then the
	section provided is looked up.

	o <<address>>

	The <<address>> field gives the offset in bytes from the base of
	the section data which owns the relocation record to the first
	byte of relocatable information. The actual data relocated
	will be relative to this point; for example, a relocation
	type which modifies the bottom two bytes of a four byte word
	would not touch the first byte pointed to in a big endian
	world.
Richard Henderson's avatar
Richard Henderson committed
146
147
148
149
150
151
152
153
154
155
156
157
158

	o <<addend>>

	The <<addend>> is a value provided by the back end to be added (!)
	to the relocation offset. Its interpretation is dependent upon
	the howto. For example, on the 68k the code:

|        char foo[];
|        main()
|                {
|                return foo[0x12345678];
|                }

Alan Modra's avatar
Alan Modra committed
159
	Could be compiled into:
Richard Henderson's avatar
Richard Henderson committed
160
161
162
163
164
165
166

|        linkw fp,#-4
|        moveb @@#12345678,d0
|        extbl d0
|        unlk fp
|        rts

Alan Modra's avatar
Alan Modra committed
167
168
	This could create a reloc pointing to <<foo>>, but leave the
	offset in the data, something like:
Richard Henderson's avatar
Richard Henderson committed
169
170
171
172
173
174
175
176
177
178
179

|RELOCATION RECORDS FOR [.text]:
|offset   type      value
|00000006 32        _foo
|
|00000000 4e56 fffc          ; linkw fp,#-4
|00000004 1039 1234 5678     ; moveb @@#12345678,d0
|0000000a 49c0               ; extbl d0
|0000000c 4e5e               ; unlk fp
|0000000e 4e75               ; rts

Alan Modra's avatar
Alan Modra committed
180
181
182
	Using coff and an 88k, some instructions don't have enough
	space in them to represent the full address range, and
	pointers have to be loaded in two parts. So you'd get something like:
Richard Henderson's avatar
Richard Henderson committed
183
184
185
186
187

|        or.u     r13,r0,hi16(_foo+0x12345678)
|        ld.b     r2,r13,lo16(_foo+0x12345678)
|        jmp      r1

Alan Modra's avatar
Alan Modra committed
188
189
	This should create two relocs, both pointing to <<_foo>>, and with
	0x12340000 in their addend field. The data would consist of:
Richard Henderson's avatar
Richard Henderson committed
190
191
192
193
194
195
196
197
198
199

|RELOCATION RECORDS FOR [.text]:
|offset   type      value
|00000002 HVRT16    _foo+0x12340000
|00000006 LVRT16    _foo+0x12340000
|
|00000000 5da05678           ; or.u r13,r0,0x5678
|00000004 1c4d5678           ; ld.b r2,r13,0x5678
|00000008 f400c001           ; jmp r1

Alan Modra's avatar
Alan Modra committed
200
201
202
203
	The relocation routine digs out the value from the data, adds
	it to the addend to get the original offset, and then adds the
	value of <<_foo>>. Note that all 32 bits have to be kept around
	somewhere, to cope with carry from bit 15 to bit 16.
Richard Henderson's avatar
Richard Henderson committed
204

Alan Modra's avatar
Alan Modra committed
205
206
207
208
209
210
211
	One further example is the sparc and the a.out format. The
	sparc has a similar problem to the 88k, in that some
	instructions don't have room for an entire offset, but on the
	sparc the parts are created in odd sized lumps. The designers of
	the a.out format chose to not use the data within the section
	for storing part of the offset; all the offset is kept within
	the reloc. Anything in the data should be ignored.
Richard Henderson's avatar
Richard Henderson committed
212
213
214
215
216
217
218

|        save %sp,-112,%sp
|        sethi %hi(_foo+0x12345678),%g2
|        ldsb [%g2+%lo(_foo+0x12345678)],%i0
|        ret
|        restore

Alan Modra's avatar
Alan Modra committed
219
220
	Both relocs contain a pointer to <<foo>>, and the offsets
	contain junk.
Richard Henderson's avatar
Richard Henderson committed
221
222
223
224
225
226
227
228
229
230
231
232

|RELOCATION RECORDS FOR [.text]:
|offset   type      value
|00000004 HI22      _foo+0x12345678
|00000008 LO10      _foo+0x12345678
|
|00000000 9de3bf90     ; save %sp,-112,%sp
|00000004 05000000     ; sethi %hi(_foo+0),%g2
|00000008 f048a000     ; ldsb [%g2+%lo(_foo+0)],%i0
|0000000c 81c7e008     ; ret
|00000010 81e80000     ; restore

Alan Modra's avatar
Alan Modra committed
233
	o <<howto>>
Richard Henderson's avatar
Richard Henderson committed
234

Alan Modra's avatar
Alan Modra committed
235
236
237
238
239
240
241
	The <<howto>> field can be imagined as a
	relocation instruction. It is a pointer to a structure which
	contains information on what to do with all of the other
	information in the reloc record and data section. A back end
	would normally have a relocation instruction set and turn
	relocations into pointers to the correct structure on input -
	but it would be possible to create each howto field on demand.
Richard Henderson's avatar
Richard Henderson committed
242
243
244
245
246
247
248
249
250
251
252
253
254
255

*/

/*
SUBSUBSECTION
	<<enum complain_overflow>>

	Indicates what sort of overflow checking should be done when
	performing a relocation.

CODE_FRAGMENT
.
.enum complain_overflow
.{
Nick Clifton's avatar
Nick Clifton committed
256
.  {* Do not complain on overflow.  *}
Richard Henderson's avatar
Richard Henderson committed
257
258
.  complain_overflow_dont,
.
259
260
261
.  {* Complain if the value overflows when considered as a signed
.     number one bit larger than the field.  ie. A bitfield of N bits
.     is allowed to represent -2**n to 2**n-1.  *}
Richard Henderson's avatar
Richard Henderson committed
262
263
.  complain_overflow_bitfield,
.
264
.  {* Complain if the value overflows when considered as a signed
Nick Clifton's avatar
Nick Clifton committed
265
.     number.  *}
Richard Henderson's avatar
Richard Henderson committed
266
267
.  complain_overflow_signed,
.
268
.  {* Complain if the value overflows when considered as an
Nick Clifton's avatar
Nick Clifton committed
269
.     unsigned number.  *}
Richard Henderson's avatar
Richard Henderson committed
270
271
272
273
274
275
276
.  complain_overflow_unsigned
.};

*/

/*
SUBSUBSECTION
Alan Modra's avatar
Alan Modra committed
277
	<<reloc_howto_type>>
Richard Henderson's avatar
Richard Henderson committed
278

Alan Modra's avatar
Alan Modra committed
279
280
	The <<reloc_howto_type>> is a structure which contains all the
	information that libbfd needs to know to tie up a back end's data.
Richard Henderson's avatar
Richard Henderson committed
281
282
283
284

CODE_FRAGMENT
.struct reloc_howto_struct
.{
Alan Modra's avatar
Alan Modra committed
285
286
287
.  {* The type field has mainly a documentary use - the back end can
.     do what it wants with it, though normally the back end's idea of
.     an external reloc number is stored in this field.  *}
Richard Henderson's avatar
Richard Henderson committed
288
289
.  unsigned int type;
.
Alan Modra's avatar
Alan Modra committed
290
291
292
293
.  {* The encoded size of the item to be relocated.  This is *not* a
.     power-of-two measure.  Use bfd_get_reloc_size to find the size
.     of the item in bytes.  *}
.  unsigned int size:3;
Richard Henderson's avatar
Richard Henderson committed
294
.
Alan Modra's avatar
Alan Modra committed
295
296
297
.  {* The number of bits in the field to be relocated.  This is used
.     when doing overflow checking.  *}
.  unsigned int bitsize:7;
Richard Henderson's avatar
Richard Henderson committed
298
.
Alan Modra's avatar
Alan Modra committed
299
300
301
.  {* The value the final relocation is shifted right by.  This drops
.     unwanted data from the relocation.  *}
.  unsigned int rightshift:6;
Richard Henderson's avatar
Richard Henderson committed
302
.
Alan Modra's avatar
Alan Modra committed
303
304
305
.  {* The bit position of the reloc value in the destination.
.     The relocated value is left shifted by this amount.  *}
.  unsigned int bitpos:6;
Richard Henderson's avatar
Richard Henderson committed
306
.
307
308
.  {* What type of overflow error should be checked for when
.     relocating.  *}
Alan Modra's avatar
Alan Modra committed
309
.  ENUM_BITFIELD (complain_overflow) complain_on_overflow:2;
Richard Henderson's avatar
Richard Henderson committed
310
.
Alan Modra's avatar
Alan Modra committed
311
312
.  {* The relocation value should be negated before applying.  *}
.  unsigned int negate:1;
Richard Henderson's avatar
Richard Henderson committed
313
.
Alan Modra's avatar
Alan Modra committed
314
315
.  {* The relocation is relative to the item being relocated.  *}
.  unsigned int pc_relative:1;
Richard Henderson's avatar
Richard Henderson committed
316
.
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
.  {* Some formats record a relocation addend in the section contents
.     rather than with the relocation.  For ELF formats this is the
.     distinction between USE_REL and USE_RELA (though the code checks
.     for USE_REL == 1/0).  The value of this field is TRUE if the
.     addend is recorded with the section contents; when performing a
.     partial link (ld -r) the section contents (the data) will be
.     modified.  The value of this field is FALSE if addends are
.     recorded with the relocation (in arelent.addend); when performing
.     a partial link the relocation will be modified.
.     All relocations for all ELF USE_RELA targets should set this field
.     to FALSE (values of TRUE should be looked on with suspicion).
.     However, the converse is not true: not all relocations of all ELF
.     USE_REL targets set this field to TRUE.  Why this is so is peculiar
.     to each particular target.  For relocs that aren't used in partial
.     links (e.g. GOT stuff) it doesn't matter what this is set to.  *}
Alan Modra's avatar
Alan Modra committed
332
333
334
335
336
337
338
339
340
.  unsigned int partial_inplace:1;
.
.  {* When some formats create PC relative instructions, they leave
.     the value of the pc of the place being relocated in the offset
.     slot of the instruction, so that a PC relative relocation can
.     be made just by adding in an ordinary offset (e.g., sun3 a.out).
.     Some formats leave the displacement part of an instruction
.     empty (e.g., ELF); this flag signals the fact.  *}
.  unsigned int pcrel_offset:1;
Richard Henderson's avatar
Richard Henderson committed
341
.
342
343
344
345
346
.  {* src_mask selects the part of the instruction (or data) to be used
.     in the relocation sum.  If the target relocations don't have an
.     addend in the reloc, eg. ELF USE_REL, src_mask will normally equal
.     dst_mask to extract the addend from the section contents.  If
.     relocations do have an addend in the reloc, eg. ELF USE_RELA, this
Alan Modra's avatar
Alan Modra committed
347
348
349
.     field should normally be zero.  Non-zero values for ELF USE_RELA
.     targets should be viewed with suspicion as normally the value in
.     the dst_mask part of the section contents should be ignored.  *}
Richard Henderson's avatar
Richard Henderson committed
350
351
.  bfd_vma src_mask;
.
352
353
.  {* dst_mask selects which parts of the instruction (or data) are
.     replaced with a relocated value.  *}
Richard Henderson's avatar
Richard Henderson committed
354
355
.  bfd_vma dst_mask;
.
Alan Modra's avatar
Alan Modra committed
356
357
358
359
360
361
362
363
364
.  {* If this field is non null, then the supplied function is
.     called rather than the normal function.  This allows really
.     strange relocation methods to be accommodated.  *}
.  bfd_reloc_status_type (*special_function)
.    (bfd *, arelent *, struct bfd_symbol *, void *, asection *,
.     bfd *, char **);
.
.  {* The textual name of the relocation type.  *}
.  char *name;
Richard Henderson's avatar
Richard Henderson committed
365
.};
Nick Clifton's avatar
Nick Clifton committed
366
.
Richard Henderson's avatar
Richard Henderson committed
367
368
369
370
371
372
373
*/

/*
FUNCTION
	The HOWTO Macro

DESCRIPTION
374
375
	The HOWTO macro fills in a reloc_howto_type (a typedef for
	const struct reloc_howto_struct).
Richard Henderson's avatar
Richard Henderson committed
376

Alan Modra's avatar
Alan Modra committed
377
378
379
380
.#define HOWTO(type, right, size, bits, pcrel, left, ovf, func, name,	\
.              inplace, src_mask, dst_mask, pcrel_off)			\
.  { (unsigned) type, size < 0 ? -size : size, bits, right, left, ovf,	\
.    size < 0, pcrel, inplace, pcrel_off, src_mask, dst_mask, func, name }
Richard Henderson's avatar
Richard Henderson committed
381

382
383
384
385
DESCRIPTION
	This is used to fill in an empty howto entry in an array.

.#define EMPTY_HOWTO(C) \
386
.  HOWTO ((C), 0, 0, 0, FALSE, 0, complain_overflow_dont, NULL, \
Alan Modra's avatar
Alan Modra committed
387
.	  NULL, FALSE, 0, 0, FALSE)
388
.
Richard Henderson's avatar
Richard Henderson committed
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
*/

/*
FUNCTION
	bfd_get_reloc_size

SYNOPSIS
	unsigned int bfd_get_reloc_size (reloc_howto_type *);

DESCRIPTION
	For a reloc_howto_type that operates on a fixed number of bytes,
	this returns the number of bytes operated on.
 */

unsigned int
Alan Modra's avatar
Alan Modra committed
404
bfd_get_reloc_size (reloc_howto_type *howto)
Richard Henderson's avatar
Richard Henderson committed
405
406
407
408
{
  switch (howto->size)
    {
    case 0: return 1;
Alan Modra's avatar
Alan Modra committed
409
410
    case 1: return 2;
    case 2: return 4;
Richard Henderson's avatar
Richard Henderson committed
411
412
    case 3: return 0;
    case 4: return 8;
413
    case 5: return 3;
Richard Henderson's avatar
Richard Henderson committed
414
415
416
417
418
419
420
421
422
423
424
425
    default: abort ();
    }
}

/*
TYPEDEF
	arelent_chain

DESCRIPTION

	How relocs are tied together in an <<asection>>:

426
427
.typedef struct relent_chain
.{
Richard Henderson's avatar
Richard Henderson committed
428
.  arelent relent;
429
.  struct relent_chain *next;
Nick Clifton's avatar
Nick Clifton committed
430
431
432
.}
.arelent_chain;
.
Richard Henderson's avatar
Richard Henderson committed
433
434
435
436
437
438
439
440
441
442
*/

/* N_ONES produces N one bits, without overflowing machine arithmetic.  */
#define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)

/*
FUNCTION
	bfd_check_overflow

SYNOPSIS
Alan Modra's avatar
Alan Modra committed
443
444
445
446
447
448
	bfd_reloc_status_type bfd_check_overflow
	  (enum complain_overflow how,
	   unsigned int bitsize,
	   unsigned int rightshift,
	   unsigned int addrsize,
	   bfd_vma relocation);
Richard Henderson's avatar
Richard Henderson committed
449
450
451
452
453
454
455
456
457
458
459

DESCRIPTION
	Perform overflow checking on @var{relocation} which has
	@var{bitsize} significant bits and will be shifted right by
	@var{rightshift} bits, on a machine with addresses containing
	@var{addrsize} significant bits.  The result is either of
	@code{bfd_reloc_ok} or @code{bfd_reloc_overflow}.

*/

bfd_reloc_status_type
Alan Modra's avatar
Alan Modra committed
460
461
462
463
464
bfd_check_overflow (enum complain_overflow how,
		    unsigned int bitsize,
		    unsigned int rightshift,
		    unsigned int addrsize,
		    bfd_vma relocation)
Richard Henderson's avatar
Richard Henderson committed
465
466
467
468
469
470
471
472
473
{
  bfd_vma fieldmask, addrmask, signmask, ss, a;
  bfd_reloc_status_type flag = bfd_reloc_ok;

  /* Note: BITSIZE should always be <= ADDRSIZE, but in case it's not,
     we'll be permissive: extra bits in the field mask will
     automatically extend the address mask for purposes of the
     overflow check.  */
  fieldmask = N_ONES (bitsize);
474
  signmask = ~fieldmask;
475
  addrmask = N_ONES (addrsize) | (fieldmask << rightshift);
H.J. Lu's avatar
H.J. Lu committed
476
  a = (relocation & addrmask) >> rightshift;
Richard Henderson's avatar
Richard Henderson committed
477
478
479
480
481
482
483
484

  switch (how)
    {
    case complain_overflow_dont:
      break;

    case complain_overflow_signed:
      /* If any sign bits are set, all sign bits must be set.  That
Alan Modra's avatar
Alan Modra committed
485
	 is, A must be a valid negative address after shifting.  */
Richard Henderson's avatar
Richard Henderson committed
486
      signmask = ~ (fieldmask >> 1);
487
      /* Fall thru */
Richard Henderson's avatar
Richard Henderson committed
488
489
490

    case complain_overflow_bitfield:
      /* Bitfields are sometimes signed, sometimes unsigned.  We
491
492
493
494
	 explicitly allow an address wrap too, which means a bitfield
	 of n bits is allowed to store -2**n to 2**n-1.  Thus overflow
	 if the value has some, but not all, bits set outside the
	 field.  */
495
496
497
498
499
500
501
502
      ss = a & signmask;
      if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
	flag = bfd_reloc_overflow;
      break;

    case complain_overflow_unsigned:
      /* We have an overflow if the address does not fit in the field.  */
      if ((a & signmask) != 0)
503
	flag = bfd_reloc_overflow;
Richard Henderson's avatar
Richard Henderson committed
504
505
506
507
508
509
510
511
512
      break;

    default:
      abort ();
    }

  return flag;
}

513
514
515
516
517
518
/*
FUNCTION
	bfd_reloc_offset_in_range

SYNOPSIS
	bfd_boolean bfd_reloc_offset_in_range
Alan Modra's avatar
Alan Modra committed
519
520
521
522
	  (reloc_howto_type *howto,
	   bfd *abfd,
	   asection *section,
	   bfd_size_type offset);
523
524

DESCRIPTION
Alan Modra's avatar
Alan Modra committed
525
	Returns TRUE if the reloc described by @var{HOWTO} can be
526
527
528
529
	applied at @var{OFFSET} octets in @var{SECTION}.

*/

530
531
532
/* HOWTO describes a relocation, at offset OCTET.  Return whether the
   relocation field is within SECTION of ABFD.  */

533
534
535
536
537
bfd_boolean
bfd_reloc_offset_in_range (reloc_howto_type *howto,
			   bfd *abfd,
			   asection *section,
			   bfd_size_type octet)
538
539
540
541
542
543
544
545
546
547
{
  bfd_size_type octet_end = bfd_get_section_limit_octets (abfd, section);
  bfd_size_type reloc_size = bfd_get_reloc_size (howto);

  /* The reloc field must be contained entirely within the section.
     Allow zero length fields (marker relocs or NONE relocs where no
     relocation will be performed) at the end of the section.  */
  return octet <= octet_end && octet + reloc_size <= octet_end;
}

548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
/* Read and return the section contents at DATA converted to a host
   integer (bfd_vma).  The number of bytes read is given by the HOWTO.  */

static bfd_vma
read_reloc (bfd *abfd, bfd_byte *data, reloc_howto_type *howto)
{
  switch (howto->size)
    {
    case 0:
      return bfd_get_8 (abfd, data);

    case 1:
      return bfd_get_16 (abfd, data);

    case 2:
      return bfd_get_32 (abfd, data);

    case 3:
      break;

#ifdef BFD64
    case 4:
      return bfd_get_64 (abfd, data);
#endif

    case 5:
      return bfd_get_24 (abfd, data);

    default:
      abort ();
    }
  return 0;
}

/* Convert VAL to target format and write to DATA.  The number of
   bytes written is given by the HOWTO.  */

static void
write_reloc (bfd *abfd, bfd_vma val, bfd_byte *data, reloc_howto_type *howto)
{
  switch (howto->size)
    {
    case 0:
      bfd_put_8 (abfd, val, data);
      break;

    case 1:
      bfd_put_16 (abfd, val, data);
      break;

    case 2:
      bfd_put_32 (abfd, val, data);
      break;

    case 3:
      break;

#ifdef BFD64
    case 4:
      bfd_put_64 (abfd, val, data);
      break;
#endif

    case 5:
      bfd_put_24 (abfd, val, data);
      break;

    default:
      abort ();
    }
}

/* Apply RELOCATION value to target bytes at DATA, according to
   HOWTO.  */

static void
apply_reloc (bfd *abfd, bfd_byte *data, reloc_howto_type *howto,
	     bfd_vma relocation)
{
  bfd_vma val = read_reloc (abfd, data, howto);

Alan Modra's avatar
Alan Modra committed
629
  if (howto->negate)
630
631
632
633
634
635
636
637
    relocation = -relocation;

  val = ((val & ~howto->dst_mask)
	 | (((val & howto->src_mask) + relocation) & howto->dst_mask));

  write_reloc (abfd, val, data, howto);
}

Richard Henderson's avatar
Richard Henderson committed
638
639
640
641
642
/*
FUNCTION
	bfd_perform_relocation

SYNOPSIS
Alan Modra's avatar
Alan Modra committed
643
	bfd_reloc_status_type bfd_perform_relocation
Alan Modra's avatar
Alan Modra committed
644
645
646
647
648
	  (bfd *abfd,
	   arelent *reloc_entry,
	   void *data,
	   asection *input_section,
	   bfd *output_bfd,
Alan Modra's avatar
Alan Modra committed
649
	   char **error_message);
Richard Henderson's avatar
Richard Henderson committed
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669

DESCRIPTION
	If @var{output_bfd} is supplied to this function, the
	generated image will be relocatable; the relocations are
	copied to the output file after they have been changed to
	reflect the new state of the world. There are two ways of
	reflecting the results of partial linkage in an output file:
	by modifying the output data in place, and by modifying the
	relocation record.  Some native formats (e.g., basic a.out and
	basic coff) have no way of specifying an addend in the
	relocation type, so the addend has to go in the output data.
	This is no big deal since in these formats the output data
	slot will always be big enough for the addend. Complex reloc
	types with addends were invented to solve just this problem.
	The @var{error_message} argument is set to an error message if
	this return @code{bfd_reloc_dangerous}.

*/

bfd_reloc_status_type
Alan Modra's avatar
Alan Modra committed
670
671
672
673
674
675
bfd_perform_relocation (bfd *abfd,
			arelent *reloc_entry,
			void *data,
			asection *input_section,
			bfd *output_bfd,
			char **error_message)
Richard Henderson's avatar
Richard Henderson committed
676
677
678
{
  bfd_vma relocation;
  bfd_reloc_status_type flag = bfd_reloc_ok;
679
  bfd_size_type octets;
Richard Henderson's avatar
Richard Henderson committed
680
681
682
683
684
685
  bfd_vma output_base = 0;
  reloc_howto_type *howto = reloc_entry->howto;
  asection *reloc_target_output_section;
  asymbol *symbol;

  symbol = *(reloc_entry->sym_ptr_ptr);
686

687
  /* If we are not producing relocatable output, return an error if
Richard Henderson's avatar
Richard Henderson committed
688
689
690
691
     the symbol is not defined.  An undefined weak symbol is
     considered to have a value of zero (SVR4 ABI, p. 4-27).  */
  if (bfd_is_und_section (symbol->section)
      && (symbol->flags & BSF_WEAK) == 0
Alan Modra's avatar
Alan Modra committed
692
      && output_bfd == NULL)
Richard Henderson's avatar
Richard Henderson committed
693
694
695
696
697
    flag = bfd_reloc_undefined;

  /* If there is a function supplied to handle this relocation type,
     call it.  It'll return `bfd_reloc_continue' if further processing
     can be done.  */
698
  if (howto && howto->special_function)
Richard Henderson's avatar
Richard Henderson committed
699
700
    {
      bfd_reloc_status_type cont;
701
702
703
704
705

      /* Note - we do not call bfd_reloc_offset_in_range here as the
	 reloc_entry->address field might actually be valid for the
	 backend concerned.  It is up to the special_function itself
	 to call bfd_reloc_offset_in_range if needed.  */
Richard Henderson's avatar
Richard Henderson committed
706
707
708
709
710
711
712
      cont = howto->special_function (abfd, reloc_entry, symbol, data,
				      input_section, output_bfd,
				      error_message);
      if (cont != bfd_reloc_continue)
	return cont;
    }

713
714
715
716
717
718
719
720
721
722
723
  if (bfd_is_abs_section (symbol->section)
      && output_bfd != NULL)
    {
      reloc_entry->address += input_section->output_offset;
      return bfd_reloc_ok;
    }

  /* PR 17512: file: 0f67f69d.  */
  if (howto == NULL)
    return bfd_reloc_undefined;

724
  /* Is the address of the relocation really within the section?  */
725
  octets = reloc_entry->address * bfd_octets_per_byte (abfd);
726
  if (!bfd_reloc_offset_in_range (howto, abfd, input_section, octets))
Richard Henderson's avatar
Richard Henderson committed
727
728
    return bfd_reloc_outofrange;

Kazu Hirata's avatar
Kazu Hirata committed
729
  /* Work out which section the relocation is targeted at and the
Richard Henderson's avatar
Richard Henderson committed
730
731
732
733
734
735
736
737
738
739
740
     initial relocation command value.  */

  /* Get symbol value.  (Common symbols are special.)  */
  if (bfd_is_com_section (symbol->section))
    relocation = 0;
  else
    relocation = symbol->value;

  reloc_target_output_section = symbol->section->output_section;

  /* Convert input-section-relative symbol value to absolute.  */
741
742
  if ((output_bfd && ! howto->partial_inplace)
      || reloc_target_output_section == NULL)
Richard Henderson's avatar
Richard Henderson committed
743
744
745
746
747
748
749
750
751
752
753
754
    output_base = 0;
  else
    output_base = reloc_target_output_section->vma;

  relocation += output_base + symbol->section->output_offset;

  /* Add in supplied addend.  */
  relocation += reloc_entry->addend;

  /* Here the variable relocation holds the final address of the
     symbol we are relocating against, plus any addend.  */

755
  if (howto->pc_relative)
Richard Henderson's avatar
Richard Henderson committed
756
757
758
759
760
761
762
763
764
765
766
767
    {
      /* This is a PC relative relocation.  We want to set RELOCATION
	 to the distance between the address of the symbol and the
	 location.  RELOCATION is already the address of the symbol.

	 We start by subtracting the address of the section containing
	 the location.

	 If pcrel_offset is set, we must further subtract the position
	 of the location within the section.  Some targets arrange for
	 the addend to be the negative of the position of the location
	 within the section; for example, i386-aout does this.  For
768
	 i386-aout, pcrel_offset is FALSE.  Some other targets do not
Alan Modra's avatar
Alan Modra committed
769
770
	 include the position of the location; for example, ELF.
	 For those targets, pcrel_offset is TRUE.
Richard Henderson's avatar
Richard Henderson committed
771

772
	 If we are producing relocatable output, then we must ensure
Richard Henderson's avatar
Richard Henderson committed
773
	 that this reloc will be correctly computed when the final
774
	 relocation is done.  If pcrel_offset is FALSE we want to wind
Richard Henderson's avatar
Richard Henderson committed
775
776
	 up with the negative of the location within the section,
	 which means we must adjust the existing addend by the change
777
	 in the location within the section.  If pcrel_offset is TRUE
Richard Henderson's avatar
Richard Henderson committed
778
779
780
	 we do not want to adjust the existing addend at all.

	 FIXME: This seems logical to me, but for the case of
781
	 producing relocatable output it is not what the code
Richard Henderson's avatar
Richard Henderson committed
782
783
784
785
786
787
	 actually does.  I don't want to change it, because it seems
	 far too likely that something will break.  */

      relocation -=
	input_section->output_section->vma + input_section->output_offset;

788
      if (howto->pcrel_offset)
Richard Henderson's avatar
Richard Henderson committed
789
790
791
	relocation -= reloc_entry->address;
    }

Alan Modra's avatar
Alan Modra committed
792
  if (output_bfd != NULL)
Richard Henderson's avatar
Richard Henderson committed
793
    {
794
      if (! howto->partial_inplace)
Richard Henderson's avatar
Richard Henderson committed
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
	{
	  /* This is a partial relocation, and we want to apply the relocation
	     to the reloc entry rather than the raw data. Modify the reloc
	     inplace to reflect what we now know.  */
	  reloc_entry->addend = relocation;
	  reloc_entry->address += input_section->output_offset;
	  return flag;
	}
      else
	{
	  /* This is a partial relocation, but inplace, so modify the
	     reloc record a bit.

	     If we've relocated with a symbol with a section, change
	     into a ref to the section belonging to the symbol.  */

	  reloc_entry->address += input_section->output_offset;

	  /* WTF?? */
	  if (abfd->xvec->flavour == bfd_target_coff_flavour
	      && strcmp (abfd->xvec->name, "coff-Intel-little") != 0
	      && strcmp (abfd->xvec->name, "coff-Intel-big") != 0)
	    {
	      /* For m68k-coff, the addend was being subtracted twice during
		 relocation with -r.  Removing the line below this comment
		 fixes that problem; see PR 2953.

However, Ian wrote the following, regarding removing the line below,
which explains why it is still enabled:  --djm

If you put a patch like that into BFD you need to check all the COFF
linkers.  I am fairly certain that patch will break coff-i386 (e.g.,
SCO); see coff_i386_reloc in coff-i386.c where I worked around the
problem in a different way.  There may very well be a reason that the
code works as it does.

Hmmm.  The first obvious point is that bfd_perform_relocation should
not have any tests that depend upon the flavour.  It's seem like
entirely the wrong place for such a thing.  The second obvious point
is that the current code ignores the reloc addend when producing
835
relocatable output for COFF.  That's peculiar.  In fact, I really
Richard Henderson's avatar
Richard Henderson committed
836
837
838
839
840
841
842
843
844
845
846
847
848
849
have no idea what the point of the line you want to remove is.

A typical COFF reloc subtracts the old value of the symbol and adds in
the new value to the location in the object file (if it's a pc
relative reloc it adds the difference between the symbol value and the
location).  When relocating we need to preserve that property.

BFD handles this by setting the addend to the negative of the old
value of the symbol.  Unfortunately it handles common symbols in a
non-standard way (it doesn't subtract the old value) but that's a
different story (we can't change it without losing backward
compatibility with old object files) (coff-i386 does subtract the old
value, to be compatible with existing coff-i386 targets, like SCO).

850
851
852
So everything works fine when not producing relocatable output.  When
we are producing relocatable output, logically we should do exactly
what we do when not producing relocatable output.  Therefore, your
Richard Henderson's avatar
Richard Henderson committed
853
854
855
856
857
858
859
patch is correct.  In fact, it should probably always just set
reloc_entry->addend to 0 for all cases, since it is, in fact, going to
add the value into the object file.  This won't hurt the COFF code,
which doesn't use the addend; I'm not sure what it will do to other
formats (the thing to check for would be whether any formats both use
the addend and set partial_inplace).

860
When I wanted to make coff-i386 produce relocatable output, I ran
Richard Henderson's avatar
Richard Henderson committed
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
into the problem that you are running into: I wanted to remove that
line.  Rather than risk it, I made the coff-i386 relocs use a special
function; it's coff_i386_reloc in coff-i386.c.  The function
specifically adds the addend field into the object file, knowing that
bfd_perform_relocation is not going to.  If you remove that line, then
coff-i386.c will wind up adding the addend field in twice.  It's
trivial to fix; it just needs to be done.

The problem with removing the line is just that it may break some
working code.  With BFD it's hard to be sure of anything.  The right
way to deal with this is simply to build and test at least all the
supported COFF targets.  It should be straightforward if time and disk
space consuming.  For each target:
    1) build the linker
    2) generate some executable, and link it using -r (I would
       probably use paranoia.o and link against newlib/libc.a, which
       for all the supported targets would be available in
       /usr/cygnus/progressive/H-host/target/lib/libc.a).
    3) make the change to reloc.c
    4) rebuild the linker
    5) repeat step 2
    6) if the resulting object files are the same, you have at least
       made it no worse
    7) if they are different you have to figure out which version is
       right
*/
	      relocation -= reloc_entry->addend;
	      reloc_entry->addend = 0;
	    }
	  else
	    {
	      reloc_entry->addend = relocation;
	    }
	}
    }

  /* FIXME: This overflow checking is incomplete, because the value
     might have overflowed before we get here.  For a correct check we
     need to compute the value in a size larger than bitsize, but we
     can't reasonably do that for a reloc the same size as a host
     machine word.
     FIXME: We should also do overflow checking on the result after
     adding in the value contained in the object file.  */
  if (howto->complain_on_overflow != complain_overflow_dont
      && flag == bfd_reloc_ok)
    flag = bfd_check_overflow (howto->complain_on_overflow,
			       howto->bitsize,
			       howto->rightshift,
			       bfd_arch_bits_per_address (abfd),
			       relocation);

Nick Clifton's avatar
Nick Clifton committed
912
913
914
  /* Either we are relocating all the way, or we don't want to apply
     the relocation to the reloc entry (probably because there isn't
     any room in the output format to describe addends to relocs).  */
Richard Henderson's avatar
Richard Henderson committed
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940

  /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
     (OSF version 1.3, compiler version 3.11).  It miscompiles the
     following program:

     struct str
     {
       unsigned int i0;
     } s = { 0 };

     int
     main ()
     {
       unsigned long x;

       x = 0x100000000;
       x <<= (unsigned long) s.i0;
       if (x == 0)
	 printf ("failed\n");
       else
	 printf ("succeeded (%lx)\n", x);
     }
     */

  relocation >>= (bfd_vma) howto->rightshift;

Nick Clifton's avatar
Nick Clifton committed
941
  /* Shift everything up to where it's going to be used.  */
Richard Henderson's avatar
Richard Henderson committed
942
943
  relocation <<= (bfd_vma) howto->bitpos;

Nick Clifton's avatar
Nick Clifton committed
944
  /* Wait for the day when all have the mask in them.  */
Richard Henderson's avatar
Richard Henderson committed
945
946
947
948
949
950
951
952
953
954
955
956
957

  /* What we do:
     i instruction to be left alone
     o offset within instruction
     r relocation offset to apply
     S src mask
     D dst mask
     N ~dst mask
     A part 1
     B part 2
     R result

     Do this:
Alan Modra's avatar
Alan Modra committed
958
959
960
961
     ((	 i i i i i o o o o o  from bfd_get<size>
     and	   S S S S S) to get the size offset we want
     +	 r r r r r r r r r r) to get the final value to place
     and	   D D D D D  to chop to right size
Richard Henderson's avatar
Richard Henderson committed
962
     -----------------------
Alan Modra's avatar
Alan Modra committed
963
     =		   A A A A A
Richard Henderson's avatar
Richard Henderson committed
964
     And this:
Alan Modra's avatar
Alan Modra committed
965
966
     (	 i i i i i o o o o o  from bfd_get<size>
     and N N N N N	    ) get instruction
Richard Henderson's avatar
Richard Henderson committed
967
     -----------------------
Alan Modra's avatar
Alan Modra committed
968
     =	 B B B B B
Richard Henderson's avatar
Richard Henderson committed
969
970

     And then:
Alan Modra's avatar
Alan Modra committed
971
972
     (	 B B B B B
     or		   A A A A A)
Richard Henderson's avatar
Richard Henderson committed
973
     -----------------------
Alan Modra's avatar
Alan Modra committed
974
     =	 R R R R R R R R R R  put into bfd_put<size>
Richard Henderson's avatar
Richard Henderson committed
975
976
     */

977
978
  data = (bfd_byte *) data + octets;
  apply_reloc (abfd, data, howto, relocation);
Richard Henderson's avatar
Richard Henderson committed
979
980
981
982
983
984
985
986
  return flag;
}

/*
FUNCTION
	bfd_install_relocation

SYNOPSIS
Alan Modra's avatar
Alan Modra committed
987
	bfd_reloc_status_type bfd_install_relocation
Alan Modra's avatar
Alan Modra committed
988
989
990
991
	  (bfd *abfd,
	   arelent *reloc_entry,
	   void *data, bfd_vma data_start,
	   asection *input_section,
Alan Modra's avatar
Alan Modra committed
992
	   char **error_message);
Richard Henderson's avatar
Richard Henderson committed
993
994
995
996
997
998
999
1000

DESCRIPTION
	This looks remarkably like <<bfd_perform_relocation>>, except it
	does not expect that the section contents have been filled in.
	I.e., it's suitable for use when creating, rather than applying
	a relocation.

	For now, this function should be considered reserved for the
For faster browsing, not all history is shown. View entire blame