Initial revision
[deliverable/binutils-gdb.git] / bfd / reloc.c
CommitLineData
985fca12
SC
1/*doc*
2@section Relocations
3
4Bfd maintains relocations in much the same was as it maintains
5symbols; they are left alone until required, then read in en-mass and
6traslated into an internal form. There is a common routine
7@code{bfd_perform_relocation} which acts upon the canonical form to to
8the actual fixup.
9
10Note that relocations are maintained on a per section basis, whilst
11symbols are maintained on a per bfd basis.
12
13All a back end has to do to fit the bfd interface is to create as many
14@code{struct reloc_cache_entry} as there are relocations in a
15particuar section, and fill in the right bits:
16
17@menu
18* typedef arelent::
19* reloc handling functions::
20@end menu
21
22*/
23#include "sysdep.h"
24#include "bfd.h"
25#include "libbfd.h"
26/*doc
27*node typedef arelent, Relocations, reloc handling functions, Relocations
28@section typedef arelent
29
30
31*/
32
33/*proto* bfd_perform_relocation
34The relocation routine returns as a status an enumerated type:
35
36*+++
37
38$typedef enum bfd_reloc_status {
39No errors detected
40
41$ bfd_reloc_ok,
42
43The relocation was performed, but there was an overflow.
44
45$ bfd_reloc_overflow,
46
47The address to relocate was not within the section supplied
48
49$ bfd_reloc_outofrange,
50
51Used by special functions
52
53$ bfd_reloc_continue,
54
55Unused
56
57$ bfd_reloc_notsupported,
58
59Unsupported relocation size requested.
60
61$ bfd_reloc_other,
62
63The symbol to relocate against was undefined.
64
65$ bfd_reloc_undefined,
66
67The relocaction was performed, but may not be ok - presently generated
68only when linking i960 coff files with i960 b.out symbols.
69
70$ bfd_reloc_dangerous
71$ }
72$ bfd_reloc_status_enum_type;
73
74*---
75
76*/
77
78/*proto*
79
80*+++
81
82$typedef struct reloc_cache_entry
83${
84
85A pointer into the canonical table of pointers
86
87$ struct symbol_cache_entry **sym_ptr_ptr;
88
89offset in section
90
91$ rawdata_offset address;
92
93addend for relocation value
94
95$ bfd_vma addend;
96
97if sym is null this is the section
98
99$ struct sec *section;
100
101Pointer to how to perform the required relocation
102
103$ struct reloc_howto_struct *howto;
104$} arelent;
105
106*---
107
108*/
109
110/*doc*
111@table @code
112@item sym_ptr_ptr
113The symbol table pointer points to a pointer to the symbol ascociated with the
114relocation request. This would naturaly be the pointer into the table
115returned by the back end's get_symtab action. @xref{Symbols}. The
116symbol is referenced through a pointer to a pointer so that tools like
117the linker can fixup all the symbols of the same name by modifying
118only one pointer. The relocation routine looks in the symbol and uses
119the base of the section the symbol is attatched to and the value of
120the symbol as the initial relocation offset. If the symbol pointer is
121zero, then the section provided is looked up.
122@item address
123The address field gives the offset in bytes from the base of the
124section data which owns the relocation record to the first byte of
125relocatable information. The actual data relocated will be relative to
126this point - for example, a relocation type which modifies the bottom
127two bytes of a four byte word would not touch the first byte pointed
128to in a big endian world.
129@item addend
130The addend is a value provided by the back end to be added (!) to the
131relocation offset. It's interpretation is dependent upon the howto.
132For example, on the 68k the code:
133
134*+
135 char foo[];
136 main()
137 {
138 return foo[0x12345678];
139 }
140*-
141Could be compiled into:
142
143*+
144 linkw fp,#-4
145 moveb @@#12345678,d0
146 extbl d0
147 unlk fp
148 rts
149*-
150
151This could create a reloc pointing to foo, but leave the offset in the data
152(something like)
153
154*+
155RELOCATION RECORDS FOR [.text]:
156OFFSET TYPE VALUE
15700000006 32 _foo
158
15900000000 4e56 fffc ; linkw fp,#-4
16000000004 1039 1234 5678 ; moveb @@#12345678,d0
1610000000a 49c0 ; extbl d0
1620000000c 4e5e ; unlk fp
1630000000e 4e75 ; rts
164*-
165Using coff and an 88k, some instructions don't have enough space in them to
166represent the full address range, and pointers have to be loaded in
167two parts. So you'd get something like:
168
169*+
170 or.u r13,r0,hi16(_foo+0x12345678)
171 ld.b r2,r13,lo16(_foo+0x12345678)
172 jmp r1
173*-
174This whould create two relocs, both pointing to _foo, and with 0x12340000
175in their addend field. The data would consist of:
176
177*+
178
179RELOCATION RECORDS FOR [.text]:
180OFFSET TYPE VALUE
18100000002 HVRT16 _foo+0x12340000
18200000006 LVRT16 _foo+0x12340000
183
18400000000 5da05678 ; or.u r13,r0,0x5678
18500000004 1c4d5678 ; ld.b r2,r13,0x5678
18600000008 f400c001 ; jmp r1
187*-
188The relocation routine digs out the value from the data, adds it to
189the addend to get the original offset and then adds the value of _foo.
190Note that all 32 bits have to be kept around somewhere, to cope with
191carry from bit 15 to bit 16.
192
193On further example is the sparc and the a.out format. The sparc has a
194similar problem to the 88k, in that some instructions don't have
195room for an entire offset, but on the sparc the parts are created odd
196sized lumps. The designers of the a.out format chose not to use the
197data within the section for storing part of the offset; all the offset
198is kept within the reloc. Any thing in the data should be ignored.
199
200*+
201 save %sp,-112,%sp
202 sethi %hi(_foo+0x12345678),%g2
203 ldsb [%g2+%lo(_foo+0x12345678)],%i0
204 ret
205 restore
206*-
207Both relocs contains a pointer to foo, and the offsets would contain junk.
208
209*+
210RELOCATION RECORDS FOR [.text]:
211OFFSET TYPE VALUE
21200000004 HI22 _foo+0x12345678
21300000008 LO10 _foo+0x12345678
214
21500000000 9de3bf90 ; save %sp,-112,%sp
21600000004 05000000 ; sethi %hi(_foo+0),%g2
21700000008 f048a000 ; ldsb [%g2+%lo(_foo+0)],%i0
2180000000c 81c7e008 ; ret
21900000010 81e80000 ; restore
220*-
221@item section
222The section field is only used when the symbol pointer field is null.
223It supplies the section into which the data should be relocated. The
224field's main use comes from assemblers which do most of the symbol fixups
225themselves; an assembler may take an internal reference to a label,
226but since it knows where the label is, it can turn the relocation
227request from a symbol lookup into a section relative relocation - the
228relocation emitted has no symbol, just a section to relocate against.
229
230I'm not sure what it means when both a symbol pointer an a section
231pointer are present. Some formats use this sort of mechanism to
232describe PIC relocations, but bfd can't to that sort of thing yet.
233@item howto
234The howto field can be imagined as a relocation instruction. It is a
235pointer to a struct which contains information on what to do with all
236the other information in the reloc record and data section. A back end
237would normally have a relocation instruction set and turn relocations
238into pointers to the correct structure on input - but it would be
239possible to create each howto field on demand.
240@end table
241*/
242
243
244/*proto* reloc_howto_type
245The @code{reloc_howto_type} is a structure which contains all the
246information that bfd needs to know to tie up a back end's data.
247
248*+++
249
250$typedef CONST struct reloc_howto_struct
251${
252The type field has mainly a documetary use - the back end can to what
253it wants with it, though the normally the back end's external idea of
254what a reloc number would be would be stored in this field. For
255example, the a PC relative word relocation in a coff environment would
256have the type 023 - because that's what the outside world calls a
257R_PCRWORD reloc.
258
259$ unsigned int type;
260
261The value the final relocation is shifted right by. This drops
262unwanted data from the relocation.
263
264$ unsigned int rightshift;
265
266The size of the item to be relocated - 0, is one byte, 1 is 2 bytes, 3
267is four bytes.
268
269$ unsigned int size;
270
271Now obsolete
272
273$ unsigned int bitsize;
274
275Notes that the relocation is relative to the location in the data
276section of the addend. The relocation function will subtract from the
277relocation value the address of the location being relocated.
278
279$ boolean pc_relative;
280
281Now obsolete
282
283$ unsigned int bitpos;
284
285Now obsolete
286
287$ boolean absolute;
288
289Causes the relocation routine to return an error if overflow is
290detected when relocating.
291
292$ boolean complain_on_overflow;
293
294If this field is non null, then the supplied function is called rather
295than the normal function. This allows really strange relocation
296methods to be accomodated (eg, i960 callj instructions).
297
298$ bfd_reloc_status_enum_type (*special_function)();
299
300The textual name of the relocation type.
301
302$ char *name;
303
304When performing a partial link, some formats must modify the
305relocations rather than the data - this flag signals this.
306
307$ boolean partial_inplace;
308
309The src_mask is used to select what parts of the read in data are to
310be used in the relocation sum. Eg, if this was an 8 bit bit of data
311which we read and relocated, this would be 0x000000ff. When we have
312relocs which have an addend, such as sun4 extended relocs, the value
313in the offset part of a relocating field is garbage so we never use
314it. In this case the mask would be 0x00000000.
315
316$ bfd_word src_mask;
317The dst_mask is what parts of the instruction are replaced into the
318instruction. In most cases src_mask == dst_mask, except in the above
319special case, where dst_mask would be 0x000000ff, and src_mask would
320be 0x00000000.
321
322$ bfd_word dst_mask;
323
324When some formats create PC relative instructions, they leave the
325value of the pc of the place being relocated in the offset slot of the
326instruction, so that a PC relative relocation can be made just by
327adding in an ordinary offset (eg sun3 a.out). Some formats leave the
328displacement part of an instruction empty (eg m88k bcs), this flag
329signals the fact.
330
331$ boolean pcrel_offset;
332$} reloc_howto_type;
333*---
334
335*/
336
337/*proto* HOWTO
338The HOWTO define is horrible and will go away.
339*+
340#define HOWTO(C, R,S,B, P, BI, ABS, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
341 {(unsigned)C,R,S,B, P, BI, ABS,O,SF,NAME,INPLACE,MASKSRC,MASKDST,PC}
342*-
343
344*/
345
346/*proto* reloc_chain
347*+
348typedef unsigned char bfd_byte;
349
350typedef struct relent_chain {
351 arelent relent;
352 struct relent_chain *next;
353} arelent_chain;
354
355*-
356
357*/
358
359
360
361/*proto*
362If an output_bfd is supplied to this function the generated image
363will be relocatable, the relocations are copied to the output file
364after they have been changed to reflect the new state of the world.
365There are two ways of reflecting the results of partial linkage in an
366output file; by modifying the output data in place, and by modifying
367the relocation record. Some native formats (eg basic a.out and basic
368coff) have no way of specifying an addend in the relocation type, so
369the addend has to go in the output data. This is no big deal since in
370these formats the output data slot will always be big enough for the
371addend. Complex reloc types with addends were invented to solve just
372this problem.
373*; PROTO(bfd_reloc_status_enum_type,
374 bfd_perform_relocation,
375 (bfd * abfd,
376 arelent *reloc_entry,
377 PTR data,
378 asection *input_section,
379 bfd *output_bfd));
380*/
381
382
383bfd_reloc_status_enum_type
384DEFUN(bfd_perform_relocation,(abfd,
385 reloc_entry,
386 data,
387 input_section,
388 output_bfd),
389 bfd *abfd AND
390 arelent *reloc_entry AND
391 PTR data AND
392 asection *input_section AND
393 bfd *output_bfd)
394{
395 bfd_vma relocation;
396 bfd_reloc_status_enum_type flag = bfd_reloc_ok;
397 bfd_vma addr = reloc_entry->address ;
398 bfd_vma output_base = 0;
399 reloc_howto_type *howto = reloc_entry->howto;
400 asection *reloc_target_output_section;
401 asection *reloc_target_input_section;
402 asymbol *symbol;
403
404 if (reloc_entry->sym_ptr_ptr) {
405 symbol = *( reloc_entry->sym_ptr_ptr);
406 if ((symbol->flags & BSF_UNDEFINED) && output_bfd == (bfd *)NULL) {
407 flag = bfd_reloc_undefined;
408 }
409 }
410 else {
411 symbol = (asymbol*)NULL;
412 }
413
414 if (howto->special_function){
415 bfd_reloc_status_enum_type cont;
416 cont = howto->special_function(abfd,
417 reloc_entry,
418 symbol,
419 data,
420 input_section);
421 if (cont != bfd_reloc_continue) return cont;
422 }
423
424 /*
425 Work out which section the relocation is targetted at and the
426 initial relocation command value.
427 */
428
429
430 if (symbol != (asymbol *)NULL){
431 if (symbol->flags & BSF_FORT_COMM) {
432 relocation = 0;
433 }
434 else {
435 relocation = symbol->value;
436 }
437 if (symbol->section != (asection *)NULL)
438 {
439 reloc_target_input_section = symbol->section;
440 }
441 else {
442 reloc_target_input_section = (asection *)NULL;
443 }
444 }
445 else if (reloc_entry->section != (asection *)NULL)
446 {
447 relocation = 0;
448 reloc_target_input_section = reloc_entry->section;
449 }
450 else {
451 relocation = 0;
452 reloc_target_input_section = (asection *)NULL;
453 }
454
455
456 if (reloc_target_input_section != (asection *)NULL) {
457
458 reloc_target_output_section =
459 reloc_target_input_section->output_section;
460
461 if (output_bfd && howto->partial_inplace==false) {
462 output_base = 0;
463 }
464 else {
465 output_base = reloc_target_output_section->vma;
466
467 }
468
469 relocation += output_base + reloc_target_input_section->output_offset;
470 }
471
472 relocation += reloc_entry->addend ;
473
474
475 if(reloc_entry->address > (bfd_vma)(input_section->size))
476 {
477 return bfd_reloc_outofrange;
478 }
479
480
481 if (howto->pc_relative == true)
482 {
483 /*
484 Anything which started out as pc relative should end up that
485 way too.
486
487 There are two ways we can see a pcrel instruction. Sometimes
488 the pcrel displacement has been partially calculated, it
489 includes the distance from the start of the section to the
490 instruction in it (eg sun3), and sometimes the field is
491 totally blank - eg m88kbcs.
492 */
493
494
495 relocation -=
496 output_base + input_section->output_offset;
497
498 if (howto->pcrel_offset == true) {
499 relocation -= reloc_entry->address;
500 }
501
502 }
503
504 if (output_bfd!= (bfd *)NULL) {
505 if ( howto->partial_inplace == false) {
506 /*
507 This is a partial relocation, and we want to apply the relocation
508 to the reloc entry rather than the raw data. Modify the reloc
509 inplace to reflect what we now know.
510 */
511 reloc_entry->addend = relocation ;
512 reloc_entry->section = reloc_target_input_section;
513 if (reloc_target_input_section != (asection *)NULL) {
514 /* If we know the output section we can forget the symbol */
515 reloc_entry->sym_ptr_ptr = (asymbol**)NULL;
516 }
517 reloc_entry->address +=
518 input_section->output_offset;
519 return flag;
520 }
521 else
522 {
523 /* This is a partial relocation, but inplace, so modify the
524 reloc record a bit
525 */
526
527 }
528 }
529
530 reloc_entry->addend = 0;
531
532
533 /*
534 Either we are relocating all the way, or we don't want to apply
535 the relocation to the reloc entry (probably because there isn't
536 any room in the output format to describe addends to relocs)
537 */
538 relocation >>= howto->rightshift;
539
540 /* Shift everything up to where it's going to be used */
541
542 relocation <<= howto->bitpos;
543
544 /* Wait for the day when all have the mask in them */
545
546 /* What we do:
547 i instruction to be left alone
548 o offset within instruction
549 r relocation offset to apply
550 S src mask
551 D dst mask
552 N ~dst mask
553 A part 1
554 B part 2
555 R result
556
557 Do this:
558 i i i i i o o o o o from bfd_get<size>
559 and S S S S S to get the size offset we want
560 + r r r r r r r r r r to get the final value to place
561 and D D D D D to chop to right size
562 -----------------------
563 A A A A A
564 And this:
565 ... i i i i i o o o o o from bfd_get<size>
566 and N N N N N get instruction
567 -----------------------
568 ... B B B B B
569
570 And then:
571 B B B B B
572 or A A A A A
573 -----------------------
574 R R R R R R R R R R put into bfd_put<size>
575 */
576
577#define DOIT(x) \
578 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
579
580 switch (howto->size)
581 {
582 case 0:
583 {
584 char x = bfd_get_8(abfd, (char *)data + addr);
585 DOIT(x);
586 bfd_put_8(abfd,x, (unsigned char *) data + addr);
587 }
588 break;
589
590 case 1:
591 {
592 short x = bfd_get_16(abfd, (bfd_byte *)data + addr);
593 DOIT(x);
594 bfd_put_16(abfd, x, (unsigned char *)data + addr);
595 }
596 break;
597 case 2:
598 {
599 long x = bfd_get_32(abfd, (bfd_byte *) data + addr);
600 DOIT(x);
601 bfd_put_32(abfd,x, (bfd_byte *)data + addr);
602 }
603 break;
604 case 3:
605 /* Do nothing */
606 break;
607 default:
608 return bfd_reloc_other;
609 }
610
611 return flag;
612}
This page took 0.042539 seconds and 4 git commands to generate.