]>
Commit | Line | Data |
---|---|---|
c618de01 SC |
1 | /* BFD support for handling relocation entries. |
2 | Copyright (C) 1990-1991 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 2 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
0cda46cf SC |
21 | /* |
22 | SECTION | |
23 | Relocations | |
985fca12 | 24 | |
0cda46cf SC |
25 | BFD maintains relocations in much the same was as it maintains |
26 | symbols; they are left alone until required, then read in | |
27 | en-mass and traslated into an internal form. There is a common | |
28 | routine <<bfd_perform_relocation>> which acts upon the | |
29 | canonical form to to the actual fixup. | |
985fca12 | 30 | |
0cda46cf SC |
31 | Note that relocations are maintained on a per section basis, |
32 | whilst symbols are maintained on a per BFD basis. | |
985fca12 | 33 | |
0cda46cf SC |
34 | All a back end has to do to fit the BFD interface is to create |
35 | as many <<struct reloc_cache_entry>> as there are relocations | |
36 | in a particuar section, and fill in the right bits: | |
985fca12 SC |
37 | |
38 | @menu | |
e98e6ec1 SC |
39 | @* typedef arelent:: |
40 | @* howto manager:: | |
985fca12 SC |
41 | @end menu |
42 | ||
43 | */ | |
985fca12 | 44 | #include "bfd.h" |
0cda46cf | 45 | #include "sysdep.h" |
985fca12 | 46 | #include "libbfd.h" |
e98e6ec1 | 47 | #include "seclet.h" |
0cda46cf | 48 | /*doc* |
e98e6ec1 SC |
49 | INODE |
50 | typedef arelent, howto manager, Relocations, Relocations | |
985fca12 | 51 | |
0cda46cf SC |
52 | SUBSECTION |
53 | typedef arelent | |
985fca12 | 54 | |
e98e6ec1 | 55 | This is the structure of a relocation entry: |
985fca12 | 56 | |
e98e6ec1 SC |
57 | CODE_FRAGMENT |
58 | . | |
59 | .typedef enum bfd_reloc_status | |
60 | .{ | |
61 | . {* No errors detected *} | |
0cda46cf | 62 | . bfd_reloc_ok, |
e98e6ec1 SC |
63 | . |
64 | . {* The relocation was performed, but there was an overflow. *} | |
0cda46cf | 65 | . bfd_reloc_overflow, |
e98e6ec1 SC |
66 | . |
67 | . {* The address to relocate was not within the section supplied*} | |
0cda46cf | 68 | . bfd_reloc_outofrange, |
e98e6ec1 SC |
69 | . |
70 | . {* Used by special functions *} | |
0cda46cf | 71 | . bfd_reloc_continue, |
e98e6ec1 SC |
72 | . |
73 | . {* Unused *} | |
0cda46cf | 74 | . bfd_reloc_notsupported, |
e98e6ec1 SC |
75 | . |
76 | . {* Unsupported relocation size requested. *} | |
0cda46cf | 77 | . bfd_reloc_other, |
e98e6ec1 SC |
78 | . |
79 | . {* The symbol to relocate against was undefined.*} | |
0cda46cf | 80 | . bfd_reloc_undefined, |
e98e6ec1 SC |
81 | . |
82 | . {* The relocation was performed, but may not be ok - presently | |
83 | . generated only when linking i960 coff files with i960 b.out | |
84 | . symbols. *} | |
0cda46cf | 85 | . bfd_reloc_dangerous |
e98e6ec1 | 86 | . } |
0cda46cf | 87 | . bfd_reloc_status_type; |
e98e6ec1 SC |
88 | . |
89 | . | |
0cda46cf SC |
90 | .typedef struct reloc_cache_entry |
91 | .{ | |
e98e6ec1 SC |
92 | . {* A pointer into the canonical table of pointers *} |
93 | . struct symbol_cache_entry **sym_ptr_ptr; | |
94 | . | |
95 | . {* offset in section *} | |
96 | . rawdata_offset address; | |
97 | . | |
98 | . {* addend for relocation value *} | |
99 | . bfd_vma addend; | |
100 | . | |
101 | . {* Pointer to how to perform the required relocation *} | |
102 | . CONST struct reloc_howto_struct *howto; | |
103 | . | |
104 | .} arelent; | |
985fca12 | 105 | |
e98e6ec1 | 106 | */ |
985fca12 | 107 | |
e98e6ec1 SC |
108 | /* |
109 | DESCRIPTION | |
985fca12 | 110 | |
e98e6ec1 | 111 | Here is a description of each of the fields within a relent: |
985fca12 | 112 | |
e98e6ec1 | 113 | o sym_ptr_ptr |
985fca12 | 114 | |
e98e6ec1 SC |
115 | The symbol table pointer points to a pointer to the symbol |
116 | associated with the relocation request. This would naturally | |
117 | be the pointer into the table returned by the back end's | |
118 | get_symtab action. @xref{Symbols}. The symbol is referenced | |
119 | through a pointer to a pointer so that tools like the linker | |
120 | can fix up all the symbols of the same name by modifying only | |
121 | one pointer. The relocation routine looks in the symbol and | |
122 | uses the base of the section the symbol is attached to and the | |
123 | value of the symbol as the initial relocation offset. If the | |
124 | symbol pointer is zero, then the section provided is looked up. | |
985fca12 | 125 | |
e98e6ec1 | 126 | o address |
985fca12 | 127 | |
e98e6ec1 SC |
128 | The address field gives the offset in bytes from the base of |
129 | the section data which owns the relocation record to the first | |
130 | byte of relocatable information. The actual data relocated | |
131 | will be relative to this point - for example, a relocation | |
132 | type which modifies the bottom two bytes of a four byte word | |
133 | would not touch the first byte pointed to in a big endian | |
134 | world. @item addend The addend is a value provided by the back | |
135 | end to be added (!) to the relocation offset. Its | |
136 | interpretation is dependent upon the howto. For example, on | |
137 | the 68k the code: | |
985fca12 | 138 | |
985fca12 | 139 | |
e98e6ec1 SC |
140 | | char foo[]; |
141 | | main() | |
142 | | { | |
143 | | return foo[0x12345678]; | |
144 | | } | |
985fca12 | 145 | |
e98e6ec1 | 146 | Could be compiled into: |
985fca12 | 147 | |
e98e6ec1 SC |
148 | | linkw fp,#-4 |
149 | | moveb @@#12345678,d0 | |
150 | | extbl d0 | |
151 | | unlk fp | |
152 | | rts | |
985fca12 | 153 | |
985fca12 | 154 | |
e98e6ec1 SC |
155 | This could create a reloc pointing to foo, but leave the |
156 | offset in the data (something like) | |
0cda46cf | 157 | |
985fca12 | 158 | |
e98e6ec1 SC |
159 | |RELOCATION RECORDS FOR [.text]: |
160 | |offset type value | |
161 | |00000006 32 _foo | |
162 | | | |
163 | |00000000 4e56 fffc ; linkw fp,#-4 | |
164 | |00000004 1039 1234 5678 ; moveb @@#12345678,d0 | |
165 | |0000000a 49c0 ; extbl d0 | |
166 | |0000000c 4e5e ; unlk fp | |
167 | |0000000e 4e75 ; rts | |
0cda46cf | 168 | |
985fca12 | 169 | |
e98e6ec1 SC |
170 | Using coff and an 88k, some instructions don't have enough |
171 | space in them to represent the full address range, and | |
172 | pointers have to be loaded in two parts. So you'd get something like: | |
0cda46cf | 173 | |
985fca12 | 174 | |
e98e6ec1 SC |
175 | | or.u r13,r0,hi16(_foo+0x12345678) |
176 | | ld.b r2,r13,lo16(_foo+0x12345678) | |
177 | | jmp r1 | |
985fca12 | 178 | |
985fca12 | 179 | |
e98e6ec1 SC |
180 | This whould create two relocs, both pointing to _foo, and with |
181 | 0x12340000 in their addend field. The data would consist of: | |
0cda46cf | 182 | |
985fca12 | 183 | |
e98e6ec1 SC |
184 | |RELOCATION RECORDS FOR [.text]: |
185 | |offset type value | |
186 | |00000002 HVRT16 _foo+0x12340000 | |
187 | |00000006 LVRT16 _foo+0x12340000 | |
985fca12 | 188 | |
e98e6ec1 SC |
189 | |00000000 5da05678 ; or.u r13,r0,0x5678 |
190 | |00000004 1c4d5678 ; ld.b r2,r13,0x5678 | |
191 | |00000008 f400c001 ; jmp r1 | |
985fca12 | 192 | |
0cda46cf | 193 | |
e98e6ec1 SC |
194 | The relocation routine digs out the value from the data, adds |
195 | it to the addend to get the original offset and then adds the | |
196 | value of _foo. Note that all 32 bits have to be kept around | |
197 | somewhere, to cope with carry from bit 15 to bit 16. | |
985fca12 | 198 | |
e98e6ec1 SC |
199 | On further example is the sparc and the a.out format. The |
200 | sparc has a similar problem to the 88k, in that some | |
201 | instructions don't have room for an entire offset, but on the | |
202 | sparc the parts are created odd sized lumps. The designers of | |
203 | the a.out format chose not to use the data within the section | |
204 | for storing part of the offset; all the offset is kept within | |
205 | the reloc. Any thing in the data should be ignored. | |
0cda46cf | 206 | |
e98e6ec1 SC |
207 | | save %sp,-112,%sp |
208 | | sethi %hi(_foo+0x12345678),%g2 | |
209 | | ldsb [%g2+%lo(_foo+0x12345678)],%i0 | |
210 | | ret | |
211 | | restore | |
0cda46cf | 212 | |
e98e6ec1 SC |
213 | Both relocs contains a pointer to foo, and the offsets would |
214 | contain junk. | |
985fca12 | 215 | |
0cda46cf | 216 | |
e98e6ec1 SC |
217 | |RELOCATION RECORDS FOR [.text]: |
218 | |offset type value | |
219 | |00000004 HI22 _foo+0x12345678 | |
220 | |00000008 LO10 _foo+0x12345678 | |
221 | ||
222 | |00000000 9de3bf90 ; save %sp,-112,%sp | |
223 | |00000004 05000000 ; sethi %hi(_foo+0),%g2 | |
224 | |00000008 f048a000 ; ldsb [%g2+%lo(_foo+0)],%i0 | |
225 | |0000000c 81c7e008 ; ret | |
226 | |00000010 81e80000 ; restore | |
227 | ||
0cda46cf | 228 | |
e98e6ec1 SC |
229 | o howto |
230 | ||
231 | The howto field can be imagined as a | |
232 | relocation instruction. It is a pointer to a struct which | |
233 | contains information on what to do with all the other | |
234 | information in the reloc record and data section. A back end | |
235 | would normally have a relocation instruction set and turn | |
236 | relocations into pointers to the correct structure on input - | |
237 | but it would be possible to create each howto field on demand. | |
238 | ||
985fca12 SC |
239 | */ |
240 | ||
241 | ||
0cda46cf SC |
242 | /* |
243 | SUBSUBSECTION | |
e98e6ec1 | 244 | <<reloc_howto_type>> |
985fca12 | 245 | |
e98e6ec1 SC |
246 | The <<reloc_howto_type>> is a structure which contains all the |
247 | information that BFD needs to know to tie up a back end's data. | |
985fca12 | 248 | |
e98e6ec1 SC |
249 | CODE_FRAGMENT |
250 | . | |
0cda46cf SC |
251 | .typedef CONST struct reloc_howto_struct |
252 | .{ | |
e98e6ec1 SC |
253 | . {* The type field has mainly a documetary use - the back end can |
254 | . to what it wants with it, though the normally the back end's | |
255 | . external idea of what a reloc number would be would be stored | |
256 | . in this field. For example, the a PC relative word relocation | |
257 | . in a coff environment would have the type 023 - because that's | |
258 | . what the outside world calls a R_PCRWORD reloc. *} | |
0cda46cf | 259 | . unsigned int type; |
e98e6ec1 SC |
260 | . |
261 | . {* The value the final relocation is shifted right by. This drops | |
262 | . unwanted data from the relocation. *} | |
0cda46cf | 263 | . unsigned int rightshift; |
e98e6ec1 SC |
264 | . |
265 | . {* The size of the item to be relocated - 0, is one byte, 1 is 2 | |
266 | . bytes, 3 is four bytes. *} | |
0cda46cf | 267 | . unsigned int size; |
e98e6ec1 SC |
268 | . |
269 | . {* Now obsolete *} | |
0cda46cf | 270 | . unsigned int bitsize; |
e98e6ec1 SC |
271 | . |
272 | . {* Notes that the relocation is relative to the location in the | |
273 | . data section of the addend. The relocation function will | |
274 | . subtract from the relocation value the address of the location | |
275 | . being relocated. *} | |
0cda46cf | 276 | . boolean pc_relative; |
e98e6ec1 SC |
277 | . |
278 | . {* Now obsolete *} | |
0cda46cf | 279 | . unsigned int bitpos; |
e98e6ec1 SC |
280 | . |
281 | . {* Now obsolete *} | |
0cda46cf | 282 | . boolean absolute; |
e98e6ec1 SC |
283 | . |
284 | . {* Causes the relocation routine to return an error if overflow | |
285 | . is detected when relocating. *} | |
0cda46cf | 286 | . boolean complain_on_overflow; |
e98e6ec1 SC |
287 | . |
288 | . {* If this field is non null, then the supplied function is | |
289 | . called rather than the normal function. This allows really | |
290 | . strange relocation methods to be accomodated (eg, i960 callj | |
291 | . instructions). *} | |
0cda46cf | 292 | . bfd_reloc_status_type (*special_function)(); |
e98e6ec1 SC |
293 | . |
294 | . {* The textual name of the relocation type. *} | |
0cda46cf | 295 | . char *name; |
e98e6ec1 SC |
296 | . |
297 | . {* When performing a partial link, some formats must modify the | |
298 | . relocations rather than the data - this flag signals this.*} | |
0cda46cf | 299 | . boolean partial_inplace; |
e98e6ec1 SC |
300 | . |
301 | . {* The src_mask is used to select what parts of the read in data | |
302 | . are to be used in the relocation sum. Eg, if this was an 8 bit | |
303 | . bit of data which we read and relocated, this would be | |
304 | . 0x000000ff. When we have relocs which have an addend, such as | |
305 | . sun4 extended relocs, the value in the offset part of a | |
306 | . relocating field is garbage so we never use it. In this case | |
307 | . the mask would be 0x00000000. *} | |
0cda46cf | 308 | . bfd_word src_mask; |
e98e6ec1 SC |
309 | . |
310 | . {* The dst_mask is what parts of the instruction are replaced | |
311 | . into the instruction. In most cases src_mask == dst_mask, | |
312 | . except in the above special case, where dst_mask would be | |
313 | . 0x000000ff, and src_mask would be 0x00000000. *} | |
0cda46cf | 314 | . bfd_word dst_mask; |
e98e6ec1 SC |
315 | . |
316 | . {* When some formats create PC relative instructions, they leave | |
317 | . the value of the pc of the place being relocated in the offset | |
318 | . slot of the instruction, so that a PC relative relocation can | |
319 | . be made just by adding in an ordinary offset (eg sun3 a.out). | |
320 | . Some formats leave the displacement part of an instruction | |
321 | . empty (eg m88k bcs), this flag signals the fact.*} | |
0cda46cf | 322 | . boolean pcrel_offset; |
e98e6ec1 | 323 | . |
0cda46cf | 324 | .} reloc_howto_type; |
985fca12 | 325 | |
0cda46cf | 326 | */ |
985fca12 | 327 | |
0cda46cf SC |
328 | /* |
329 | FUNCTION | |
e98e6ec1 SC |
330 | the HOWTO macro |
331 | ||
0cda46cf SC |
332 | DESCRIPTION |
333 | The HOWTO define is horrible and will go away. | |
334 | ||
335 | ||
336 | .#define HOWTO(C, R,S,B, P, BI, ABS, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \ | |
337 | . {(unsigned)C,R,S,B, P, BI, ABS,O,SF,NAME,INPLACE,MASKSRC,MASKDST,PC} | |
338 | ||
339 | DESCRIPTION | |
340 | And will be replaced with the totally magic way. But for the | |
341 | moment, we are compatible, so do it this way.. | |
342 | ||
343 | ||
344 | .#define NEWHOWTO( FUNCTION, NAME,SIZE,REL,IN) HOWTO(0,0,SIZE,0,REL,0,false,false,FUNCTION, NAME,false,0,0,IN) | |
345 | . | |
346 | DESCRIPTION | |
347 | Helper routine to turn a symbol into a relocation value. | |
348 | ||
e98e6ec1 SC |
349 | .#define HOWTO_PREPARE(relocation, symbol) \ |
350 | . { \ | |
351 | . if (symbol != (asymbol *)NULL) { \ | |
352 | . if (symbol->section == &bfd_com_section) { \ | |
353 | . relocation = 0; \ | |
354 | . } \ | |
355 | . else { \ | |
356 | . relocation = symbol->value; \ | |
357 | . } \ | |
358 | . } \ | |
359 | .} | |
985fca12 SC |
360 | |
361 | */ | |
362 | ||
0cda46cf SC |
363 | /* |
364 | TYPEDEF | |
365 | reloc_chain | |
985fca12 | 366 | |
0cda46cf | 367 | DESCRIPTION |
985fca12 | 368 | |
0cda46cf | 369 | How relocs are tied together |
985fca12 | 370 | |
0cda46cf SC |
371 | .typedef unsigned char bfd_byte; |
372 | . | |
373 | .typedef struct relent_chain { | |
374 | . arelent relent; | |
375 | . struct relent_chain *next; | |
376 | .} arelent_chain; | |
985fca12 SC |
377 | |
378 | */ | |
379 | ||
380 | ||
381 | ||
0cda46cf SC |
382 | /* |
383 | FUNCTION | |
384 | bfd_perform_relocation | |
385 | ||
e98e6ec1 SC |
386 | SYNOPSIS |
387 | bfd_reloc_status_type | |
388 | bfd_perform_relocation | |
389 | (bfd * abfd, | |
390 | arelent *reloc_entry, | |
391 | PTR data, | |
392 | asection *input_section, | |
393 | bfd *output_bfd); | |
394 | ||
0cda46cf SC |
395 | DESCRIPTION |
396 | If an output_bfd is supplied to this function the generated | |
397 | image will be relocatable, the relocations are copied to the | |
398 | output file after they have been changed to reflect the new | |
399 | state of the world. There are two ways of reflecting the | |
400 | results of partial linkage in an output file; by modifying the | |
401 | output data in place, and by modifying the relocation record. | |
402 | Some native formats (eg basic a.out and basic coff) have no | |
403 | way of specifying an addend in the relocation type, so the | |
404 | addend has to go in the output data. This is no big deal | |
405 | since in these formats the output data slot will always be big | |
406 | enough for the addend. Complex reloc types with addends were | |
407 | invented to solve just this problem. | |
408 | ||
985fca12 SC |
409 | */ |
410 | ||
411 | ||
0cda46cf | 412 | bfd_reloc_status_type |
985fca12 SC |
413 | DEFUN(bfd_perform_relocation,(abfd, |
414 | reloc_entry, | |
415 | data, | |
416 | input_section, | |
417 | output_bfd), | |
418 | bfd *abfd AND | |
419 | arelent *reloc_entry AND | |
420 | PTR data AND | |
421 | asection *input_section AND | |
422 | bfd *output_bfd) | |
423 | { | |
424 | bfd_vma relocation; | |
0cda46cf | 425 | bfd_reloc_status_type flag = bfd_reloc_ok; |
985fca12 SC |
426 | bfd_vma addr = reloc_entry->address ; |
427 | bfd_vma output_base = 0; | |
428 | reloc_howto_type *howto = reloc_entry->howto; | |
e98e6ec1 SC |
429 | asection *reloc_target_output_section ; |
430 | ||
985fca12 SC |
431 | asymbol *symbol; |
432 | ||
e98e6ec1 SC |
433 | symbol = *( reloc_entry->sym_ptr_ptr); |
434 | if ((symbol->section == &bfd_und_section) && output_bfd == (bfd *)NULL) { | |
985fca12 SC |
435 | flag = bfd_reloc_undefined; |
436 | } | |
985fca12 SC |
437 | |
438 | if (howto->special_function){ | |
e98e6ec1 SC |
439 | bfd_reloc_status_type cont; |
440 | cont = howto->special_function(abfd, | |
441 | reloc_entry, | |
442 | symbol, | |
443 | data, | |
444 | input_section); | |
445 | if (cont != bfd_reloc_continue) return cont; | |
446 | } | |
985fca12 SC |
447 | |
448 | /* | |
449 | Work out which section the relocation is targetted at and the | |
450 | initial relocation command value. | |
451 | */ | |
452 | ||
453 | ||
e98e6ec1 | 454 | if (symbol->section == &bfd_com_section) { |
985fca12 SC |
455 | relocation = 0; |
456 | } | |
e98e6ec1 | 457 | else { |
985fca12 SC |
458 | relocation = symbol->value; |
459 | } | |
985fca12 | 460 | |
985fca12 | 461 | |
e98e6ec1 | 462 | reloc_target_output_section = symbol->section->output_section; |
985fca12 | 463 | |
e98e6ec1 | 464 | if (output_bfd && howto->partial_inplace==false) { |
985fca12 SC |
465 | output_base = 0; |
466 | } | |
e98e6ec1 | 467 | else { |
985fca12 SC |
468 | output_base = reloc_target_output_section->vma; |
469 | ||
470 | } | |
471 | ||
e98e6ec1 SC |
472 | relocation += output_base + symbol->section->output_offset; |
473 | ||
985fca12 SC |
474 | |
475 | relocation += reloc_entry->addend ; | |
476 | ||
477 | ||
e98e6ec1 SC |
478 | if(reloc_entry->address > input_section->_cooked_size) |
479 | { | |
480 | return bfd_reloc_outofrange; | |
481 | } | |
985fca12 SC |
482 | |
483 | ||
484 | if (howto->pc_relative == true) | |
e98e6ec1 SC |
485 | { |
486 | /* | |
487 | Anything which started out as pc relative should end up that | |
488 | way too. | |
489 | ||
490 | There are two ways we can see a pcrel instruction. Sometimes | |
491 | the pcrel displacement has been partially calculated, it | |
492 | includes the distance from the start of the section to the | |
493 | instruction in it (eg sun3), and sometimes the field is | |
494 | totally blank - eg m88kbcs. | |
495 | */ | |
985fca12 SC |
496 | |
497 | ||
e98e6ec1 SC |
498 | relocation -= |
499 | input_section->output_section->vma + input_section->output_offset; | |
985fca12 | 500 | |
e98e6ec1 SC |
501 | if (howto->pcrel_offset == true) { |
502 | relocation -= reloc_entry->address; | |
985fca12 SC |
503 | } |
504 | ||
e98e6ec1 SC |
505 | } |
506 | ||
985fca12 | 507 | if (output_bfd!= (bfd *)NULL) { |
e98e6ec1 SC |
508 | if ( howto->partial_inplace == false) { |
509 | /* | |
510 | This is a partial relocation, and we want to apply the relocation | |
511 | to the reloc entry rather than the raw data. Modify the reloc | |
512 | inplace to reflect what we now know. | |
513 | */ | |
514 | reloc_entry->addend = relocation ; | |
515 | reloc_entry->address += input_section->output_offset; | |
516 | return flag; | |
517 | } | |
518 | else | |
519 | { | |
520 | /* This is a partial relocation, but inplace, so modify the | |
521 | reloc record a bit. | |
522 | ||
523 | If we've relocated with a symbol with a section, change | |
524 | into a ref to the section belonging to the symbol | |
525 | */ | |
526 | reloc_entry->addend = relocation ; | |
527 | reloc_entry->address += input_section->output_offset; | |
528 | ||
529 | ||
985fca12 | 530 | } |
985fca12 | 531 | } |
e98e6ec1 SC |
532 | else |
533 | { | |
534 | ||
985fca12 | 535 | reloc_entry->addend = 0; |
e98e6ec1 SC |
536 | } |
537 | ||
985fca12 SC |
538 | |
539 | ||
540 | /* | |
541 | Either we are relocating all the way, or we don't want to apply | |
542 | the relocation to the reloc entry (probably because there isn't | |
543 | any room in the output format to describe addends to relocs) | |
544 | */ | |
545 | relocation >>= howto->rightshift; | |
546 | ||
547 | /* Shift everything up to where it's going to be used */ | |
548 | ||
549 | relocation <<= howto->bitpos; | |
550 | ||
551 | /* Wait for the day when all have the mask in them */ | |
552 | ||
553 | /* What we do: | |
554 | i instruction to be left alone | |
555 | o offset within instruction | |
556 | r relocation offset to apply | |
557 | S src mask | |
558 | D dst mask | |
559 | N ~dst mask | |
560 | A part 1 | |
561 | B part 2 | |
562 | R result | |
563 | ||
564 | Do this: | |
565 | i i i i i o o o o o from bfd_get<size> | |
566 | and S S S S S to get the size offset we want | |
567 | + r r r r r r r r r r to get the final value to place | |
568 | and D D D D D to chop to right size | |
569 | ----------------------- | |
570 | A A A A A | |
571 | And this: | |
572 | ... i i i i i o o o o o from bfd_get<size> | |
573 | and N N N N N get instruction | |
574 | ----------------------- | |
575 | ... B B B B B | |
576 | ||
577 | And then: | |
578 | B B B B B | |
579 | or A A A A A | |
580 | ----------------------- | |
581 | R R R R R R R R R R put into bfd_put<size> | |
582 | */ | |
583 | ||
584 | #define DOIT(x) \ | |
585 | x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask)) | |
586 | ||
e98e6ec1 SC |
587 | switch (howto->size) |
588 | { | |
589 | case 0: | |
590 | { | |
591 | char x = bfd_get_8(abfd, (char *)data + addr); | |
592 | DOIT(x); | |
593 | bfd_put_8(abfd,x, (unsigned char *) data + addr); | |
594 | } | |
595 | break; | |
596 | ||
597 | case 1: | |
598 | { | |
599 | short x = bfd_get_16(abfd, (bfd_byte *)data + addr); | |
600 | DOIT(x); | |
601 | bfd_put_16(abfd, x, (unsigned char *)data + addr); | |
602 | } | |
603 | break; | |
604 | case 2: | |
605 | { | |
606 | long x = bfd_get_32(abfd, (bfd_byte *) data + addr); | |
607 | DOIT(x); | |
608 | bfd_put_32(abfd,x, (bfd_byte *)data + addr); | |
609 | } | |
610 | break; | |
611 | case 3: | |
612 | ||
613 | /* Do nothing */ | |
614 | break; | |
615 | default: | |
616 | return bfd_reloc_other; | |
617 | } | |
985fca12 SC |
618 | |
619 | return flag; | |
620 | } | |
c618de01 SC |
621 | |
622 | ||
2cf44d7b | 623 | |
0cda46cf | 624 | /* |
e98e6ec1 SC |
625 | INODE |
626 | howto manager, , typedef arelent, Relocations | |
627 | ||
0cda46cf SC |
628 | SECTION |
629 | The howto manager | |
2cf44d7b | 630 | |
0cda46cf SC |
631 | When an application wants to create a relocation, but doesn't |
632 | know what the target machine might call it, it can find out by | |
633 | using this bit of code. | |
2cf44d7b | 634 | |
0cda46cf | 635 | */ |
2cf44d7b | 636 | |
0cda46cf SC |
637 | /* |
638 | TYPEDEF | |
639 | bfd_reloc_code_type | |
2cf44d7b | 640 | |
0cda46cf SC |
641 | DESCRIPTION |
642 | The insides of a reloc code | |
643 | ||
e98e6ec1 SC |
644 | CODE_FRAGMENT |
645 | . | |
646 | .typedef enum bfd_reloc_code_real | |
647 | .{ | |
648 | . {* 16 bits wide, simple reloc *} | |
649 | . BFD_RELOC_16, | |
650 | . | |
651 | . {* 8 bits wide, but used to form an address like 0xffnn *} | |
0cda46cf | 652 | . BFD_RELOC_8_FFnn, |
e98e6ec1 SC |
653 | . |
654 | . {* 8 bits wide, simple *} | |
0cda46cf | 655 | . BFD_RELOC_8, |
e98e6ec1 SC |
656 | . |
657 | . {* 8 bits wide, pc relative *} | |
0cda46cf | 658 | . BFD_RELOC_8_PCREL, |
e98e6ec1 SC |
659 | . |
660 | . {* The type of reloc used to build a contructor table - at the | |
661 | . moment probably a 32 bit wide abs address, but the cpu can | |
662 | . choose. *} | |
663 | . | |
0cda46cf | 664 | . BFD_RELOC_CTOR |
0cda46cf | 665 | . } bfd_reloc_code_real_type; |
2cf44d7b SC |
666 | */ |
667 | ||
668 | ||
669 | ||
0cda46cf SC |
670 | /* |
671 | SECTION | |
672 | bfd_reloc_type_lookup | |
2cf44d7b | 673 | |
e98e6ec1 SC |
674 | SYNOPSIS |
675 | CONST struct reloc_howto_struct * | |
676 | bfd_reloc_type_lookup | |
677 | (CONST bfd_arch_info_type *arch, bfd_reloc_code_type code); | |
678 | ||
0cda46cf SC |
679 | DESCRIPTION |
680 | This routine returns a pointer to a howto struct which when | |
681 | invoked, will perform the supplied relocation on data from the | |
682 | architecture noted. | |
2cf44d7b | 683 | |
2cf44d7b SC |
684 | */ |
685 | ||
686 | ||
687 | CONST struct reloc_howto_struct * | |
688 | DEFUN(bfd_reloc_type_lookup,(arch, code), | |
0cda46cf SC |
689 | CONST bfd_arch_info_type *arch AND |
690 | bfd_reloc_code_type code) | |
2cf44d7b SC |
691 | { |
692 | return arch->reloc_type_lookup(arch, code); | |
693 | } | |
694 | ||
0cda46cf SC |
695 | static reloc_howto_type bfd_howto_32 = |
696 | HOWTO(0, 00,2,32,false,0,false,true,0,"VRT32", false,0xffffffff,0xffffffff,true); | |
2cf44d7b SC |
697 | |
698 | ||
0cda46cf | 699 | /* |
e98e6ec1 | 700 | INTERNAL_FUNCTION |
0cda46cf SC |
701 | bfd_default_reloc_type_lookup |
702 | ||
0cda46cf SC |
703 | SYNOPSIS |
704 | CONST struct reloc_howto_struct *bfd_default_reloc_type_lookup | |
705 | (CONST struct bfd_arch_info *, | |
706 | bfd_reloc_code_type code); | |
707 | ||
e98e6ec1 SC |
708 | DESCRIPTION |
709 | Provides a default relocation lookuperer for any architectue | |
710 | ||
711 | ||
0cda46cf SC |
712 | */ |
713 | CONST struct reloc_howto_struct * | |
714 | DEFUN(bfd_default_reloc_type_lookup,(arch, code), | |
715 | CONST struct bfd_arch_info *arch AND | |
716 | bfd_reloc_code_type code) | |
717 | { | |
718 | switch (code) | |
719 | { | |
720 | case BFD_RELOC_CTOR: | |
721 | /* The type of reloc used in a ctor, which will be as wide as the | |
722 | address - so either a 64, 32, or 16 bitter.. */ | |
723 | switch (arch->bits_per_address) { | |
724 | case 64: | |
725 | BFD_FAIL(); | |
726 | case 32: | |
727 | return &bfd_howto_32; | |
728 | case 16: | |
729 | BFD_FAIL(); | |
730 | default: | |
731 | BFD_FAIL(); | |
732 | } | |
733 | default: | |
734 | BFD_FAIL(); | |
735 | } | |
736 | return (struct reloc_howto_struct *)NULL; | |
737 | } | |
e98e6ec1 SC |
738 | |
739 | ||
d58b7049 SC |
740 | /* |
741 | INTERNAL_FUNCTION | |
742 | bfd_generic_relax_section | |
743 | ||
744 | SYNOPSIS | |
745 | boolean bfd_generic_relax_section | |
746 | (bfd *abfd, | |
747 | asection *section, | |
748 | asymbol **symbols, | |
749 | struct bfd_seclet_struct *seclet); | |
750 | ||
751 | DESCRIPTION | |
752 | Provides default handling for relaxing for back ends which | |
753 | don't do relaxing - ie does nothing | |
754 | */ | |
755 | ||
756 | boolean | |
757 | DEFUN(bfd_generic_relax_section,(abfd, section, symbols, seclet), | |
758 | bfd *abfd AND | |
759 | asection *section AND | |
760 | asymbol **symbols AND | |
761 | struct bfd_seclet_struct *seclet) | |
762 | { | |
763 | ||
764 | return false; | |
765 | ||
766 | } | |
e98e6ec1 | 767 | |
d58b7049 | 768 | |
e98e6ec1 SC |
769 | /* |
770 | INTERNAL_FUNCTION | |
771 | bfd_generic_get_relocated_section_contents | |
772 | ||
773 | SYNOPSIS | |
774 | bfd_byte * | |
775 | bfd_generic_get_relocated_section_contents(bfd *abfd, | |
776 | struct bfd_seclet_struct *seclet) | |
777 | ||
778 | DESCRIPTION | |
779 | Provides default handling of relocation effort for back ends | |
780 | which can't be bothered to do it efficiently. | |
781 | ||
782 | */ | |
783 | ||
784 | bfd_byte * | |
785 | DEFUN(bfd_generic_get_relocated_section_contents,(abfd, seclet), | |
786 | bfd *abfd AND | |
787 | struct bfd_seclet_struct *seclet) | |
788 | { | |
789 | extern bfd_error_vector_type bfd_error_vector; | |
790 | ||
791 | ||
792 | asymbol **symbols = 0; | |
793 | ||
794 | /* Get enough memory to hold the stuff */ | |
795 | bfd *input_bfd = seclet->u.indirect.section->owner; | |
796 | asection *input_section = seclet->u.indirect.section; | |
797 | ||
798 | bfd_byte *data = (bfd_byte *)malloc(input_section->_raw_size); | |
799 | bfd_byte *dst = data; | |
800 | bfd_byte *prev_dst = data; | |
801 | unsigned int gap = 0; | |
802 | ||
803 | bfd_size_type reloc_size = bfd_get_reloc_upper_bound(input_bfd, | |
804 | input_section); | |
805 | arelent **reloc_vector = (arelent **)malloc(reloc_size); | |
806 | ||
807 | /* read in the section */ | |
808 | bfd_get_section_contents(input_bfd, | |
809 | input_section, | |
810 | data, | |
811 | 0, | |
812 | input_section->_raw_size); | |
813 | ||
814 | /* We're not relaxing the section, so just copy the size info */ | |
815 | input_section->_cooked_size = input_section->_raw_size; | |
816 | input_section->reloc_done = true; | |
817 | ||
818 | ||
819 | if (bfd_canonicalize_reloc(input_bfd, | |
820 | input_section, | |
821 | reloc_vector, | |
822 | seclet->u.indirect.symbols) ) | |
823 | { | |
824 | arelent **parent; | |
825 | for (parent = reloc_vector; * parent != (arelent *)NULL; | |
826 | parent++) | |
827 | { | |
828 | bfd_reloc_status_type r= | |
829 | bfd_perform_relocation(input_bfd, | |
830 | *parent, | |
831 | data, | |
832 | input_section, 0); | |
833 | ||
834 | ||
835 | if (r != bfd_reloc_ok) | |
836 | { | |
837 | asymbol *s; | |
838 | ||
839 | switch (r) | |
840 | { | |
841 | case bfd_reloc_undefined: | |
842 | bfd_error_vector.undefined_symbol(*parent, seclet); | |
843 | break; | |
844 | case bfd_reloc_dangerous: | |
845 | bfd_error_vector.reloc_dangerous(*parent, seclet); | |
846 | break; | |
847 | case bfd_reloc_outofrange: | |
848 | case bfd_reloc_overflow: | |
849 | bfd_error_vector.reloc_value_truncated(*parent, seclet); | |
850 | break; | |
851 | default: | |
852 | abort(); | |
853 | break; | |
854 | } | |
855 | ||
856 | } | |
857 | } | |
858 | } | |
859 | ||
860 | free((char *)reloc_vector); | |
861 | return data; | |
862 | ||
863 | ||
864 | } | |
865 |