cpu-ns32k.c revision 1.1.1.3.2.1 1 1.1 skrll /* BFD support for the ns32k architecture.
2 1.1.1.3.2.1 pgoyette Copyright (C) 1990-2016 Free Software Foundation, Inc.
3 1.1 skrll Almost totally rewritten by Ian Dall from initial work
4 1.1 skrll by Andrew Cagney.
5 1.1 skrll
6 1.1 skrll This file is part of BFD, the Binary File Descriptor library.
7 1.1 skrll
8 1.1 skrll This program is free software; you can redistribute it and/or modify
9 1.1 skrll it under the terms of the GNU General Public License as published by
10 1.1 skrll the Free Software Foundation; either version 3 of the License, or
11 1.1 skrll (at your option) any later version.
12 1.1 skrll
13 1.1 skrll This program is distributed in the hope that it will be useful,
14 1.1 skrll but WITHOUT ANY WARRANTY; without even the implied warranty of
15 1.1 skrll MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 1.1 skrll GNU General Public License for more details.
17 1.1 skrll
18 1.1 skrll You should have received a copy of the GNU General Public License
19 1.1 skrll along with this program; if not, write to the Free Software
20 1.1 skrll Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 1.1 skrll MA 02110-1301, USA. */
22 1.1 skrll
23 1.1 skrll #include "sysdep.h"
24 1.1 skrll #include "bfd.h"
25 1.1 skrll #include "libbfd.h"
26 1.1 skrll #include "ns32k.h"
27 1.1 skrll
28 1.1 skrll #define N(machine, printable, d, next) \
29 1.1.1.2 christos { 32, 32, 8, bfd_arch_ns32k, machine, "ns32k",printable,3,d, \
30 1.1.1.2 christos bfd_default_compatible,bfd_default_scan,bfd_arch_default_fill,next, }
31 1.1 skrll
32 1.1 skrll static const bfd_arch_info_type arch_info_struct[] =
33 1.1 skrll {
34 1.1 skrll N(32532,"ns32k:32532",TRUE, 0), /* The word ns32k will match this too. */
35 1.1 skrll };
36 1.1 skrll
37 1.1 skrll const bfd_arch_info_type bfd_ns32k_arch =
38 1.1 skrll N(32032,"ns32k:32032",FALSE, &arch_info_struct[0]);
39 1.1 skrll
40 1.1 skrll bfd_vma
41 1.1.1.2 christos _bfd_ns32k_get_displacement (bfd_byte *buffer, int size)
42 1.1 skrll {
43 1.1 skrll bfd_signed_vma value;
44 1.1 skrll
45 1.1 skrll switch (size)
46 1.1 skrll {
47 1.1 skrll case 1:
48 1.1 skrll value = ((*buffer & 0x7f) ^ 0x40) - 0x40;
49 1.1 skrll break;
50 1.1 skrll
51 1.1 skrll case 2:
52 1.1 skrll value = ((*buffer++ & 0x3f) ^ 0x20) - 0x20;
53 1.1 skrll value = (value << 8) | (0xff & *buffer);
54 1.1 skrll break;
55 1.1 skrll
56 1.1 skrll case 4:
57 1.1 skrll value = ((*buffer++ & 0x3f) ^ 0x20) - 0x20;
58 1.1 skrll value = (value << 8) | (0xff & *buffer++);
59 1.1 skrll value = (value << 8) | (0xff & *buffer++);
60 1.1 skrll value = (value << 8) | (0xff & *buffer);
61 1.1 skrll break;
62 1.1 skrll
63 1.1 skrll default:
64 1.1 skrll abort ();
65 1.1 skrll return 0;
66 1.1 skrll }
67 1.1 skrll
68 1.1 skrll return value;
69 1.1 skrll }
70 1.1 skrll
71 1.1 skrll void
72 1.1.1.2 christos _bfd_ns32k_put_displacement (bfd_vma value, bfd_byte *buffer, int size)
73 1.1 skrll {
74 1.1 skrll switch (size)
75 1.1 skrll {
76 1.1 skrll case 1:
77 1.1 skrll value &= 0x7f;
78 1.1 skrll *buffer++ = value;
79 1.1 skrll break;
80 1.1 skrll
81 1.1 skrll case 2:
82 1.1 skrll value &= 0x3fff;
83 1.1 skrll value |= 0x8000;
84 1.1 skrll *buffer++ = (value >> 8);
85 1.1 skrll *buffer++ = value;
86 1.1 skrll break;
87 1.1 skrll
88 1.1 skrll case 4:
89 1.1 skrll value |= (bfd_vma) 0xc0000000;
90 1.1 skrll *buffer++ = (value >> 24);
91 1.1 skrll *buffer++ = (value >> 16);
92 1.1 skrll *buffer++ = (value >> 8);
93 1.1 skrll *buffer++ = value;
94 1.1 skrll break;
95 1.1 skrll }
96 1.1 skrll return;
97 1.1 skrll }
98 1.1 skrll
99 1.1 skrll bfd_vma
100 1.1.1.2 christos _bfd_ns32k_get_immediate (bfd_byte *buffer, int size)
101 1.1 skrll {
102 1.1 skrll bfd_vma value = 0;
103 1.1 skrll
104 1.1 skrll switch (size)
105 1.1 skrll {
106 1.1 skrll case 4:
107 1.1 skrll value = (value << 8) | (*buffer++ & 0xff);
108 1.1 skrll value = (value << 8) | (*buffer++ & 0xff);
109 1.1 skrll case 2:
110 1.1 skrll value = (value << 8) | (*buffer++ & 0xff);
111 1.1 skrll case 1:
112 1.1 skrll value = (value << 8) | (*buffer++ & 0xff);
113 1.1 skrll break;
114 1.1 skrll default:
115 1.1 skrll abort ();
116 1.1 skrll }
117 1.1 skrll return value;
118 1.1 skrll }
119 1.1 skrll
120 1.1 skrll void
121 1.1.1.2 christos _bfd_ns32k_put_immediate (bfd_vma value, bfd_byte *buffer, int size)
122 1.1 skrll {
123 1.1 skrll buffer += size - 1;
124 1.1 skrll switch (size)
125 1.1 skrll {
126 1.1 skrll case 4:
127 1.1 skrll *buffer-- = (value & 0xff); value >>= 8;
128 1.1 skrll *buffer-- = (value & 0xff); value >>= 8;
129 1.1 skrll case 2:
130 1.1 skrll *buffer-- = (value & 0xff); value >>= 8;
131 1.1 skrll case 1:
132 1.1 skrll *buffer-- = (value & 0xff); value >>= 8;
133 1.1 skrll }
134 1.1 skrll }
135 1.1 skrll
136 1.1 skrll /* This is just like the standard perform_relocation except we
137 1.1 skrll use get_data and put_data which know about the ns32k storage
138 1.1 skrll methods. This is probably a lot more complicated than it
139 1.1 skrll needs to be! */
140 1.1 skrll
141 1.1 skrll static bfd_reloc_status_type
142 1.1.1.2 christos do_ns32k_reloc (bfd * abfd,
143 1.1.1.2 christos arelent * reloc_entry,
144 1.1.1.2 christos struct bfd_symbol * symbol,
145 1.1.1.2 christos void * data,
146 1.1.1.2 christos asection * input_section,
147 1.1.1.2 christos bfd * output_bfd,
148 1.1.1.2 christos char ** error_message ATTRIBUTE_UNUSED,
149 1.1.1.2 christos bfd_vma (* get_data) (bfd_byte *, int),
150 1.1.1.2 christos void (* put_data) (bfd_vma, bfd_byte *, int))
151 1.1 skrll {
152 1.1 skrll int overflow = 0;
153 1.1 skrll bfd_vma relocation;
154 1.1 skrll bfd_reloc_status_type flag = bfd_reloc_ok;
155 1.1 skrll bfd_size_type addr = reloc_entry->address;
156 1.1 skrll bfd_vma output_base = 0;
157 1.1 skrll reloc_howto_type *howto = reloc_entry->howto;
158 1.1 skrll asection *reloc_target_output_section;
159 1.1 skrll bfd_byte *location;
160 1.1 skrll
161 1.1.1.2 christos if (bfd_is_abs_section (symbol->section)
162 1.1 skrll && output_bfd != (bfd *) NULL)
163 1.1 skrll {
164 1.1 skrll reloc_entry->address += input_section->output_offset;
165 1.1 skrll return bfd_reloc_ok;
166 1.1 skrll }
167 1.1 skrll
168 1.1 skrll /* If we are not producing relocatable output, return an error if
169 1.1 skrll the symbol is not defined. An undefined weak symbol is
170 1.1 skrll considered to have a value of zero (SVR4 ABI, p. 4-27). */
171 1.1.1.2 christos if (bfd_is_und_section (symbol->section)
172 1.1 skrll && (symbol->flags & BSF_WEAK) == 0
173 1.1 skrll && output_bfd == (bfd *) NULL)
174 1.1 skrll flag = bfd_reloc_undefined;
175 1.1 skrll
176 1.1 skrll /* Is the address of the relocation really within the section? */
177 1.1 skrll if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
178 1.1 skrll return bfd_reloc_outofrange;
179 1.1 skrll
180 1.1 skrll /* Work out which section the relocation is targeted at and the
181 1.1 skrll initial relocation command value. */
182 1.1 skrll
183 1.1 skrll /* Get symbol value. (Common symbols are special.) */
184 1.1 skrll if (bfd_is_com_section (symbol->section))
185 1.1 skrll relocation = 0;
186 1.1 skrll else
187 1.1 skrll relocation = symbol->value;
188 1.1 skrll
189 1.1 skrll reloc_target_output_section = symbol->section->output_section;
190 1.1 skrll
191 1.1 skrll /* Convert input-section-relative symbol value to absolute. */
192 1.1 skrll if (output_bfd != NULL && ! howto->partial_inplace)
193 1.1 skrll output_base = 0;
194 1.1 skrll else
195 1.1 skrll output_base = reloc_target_output_section->vma;
196 1.1 skrll
197 1.1 skrll relocation += output_base + symbol->section->output_offset;
198 1.1 skrll
199 1.1 skrll /* Add in supplied addend. */
200 1.1 skrll relocation += reloc_entry->addend;
201 1.1 skrll
202 1.1 skrll /* Here the variable relocation holds the final address of the
203 1.1 skrll symbol we are relocating against, plus any addend. */
204 1.1 skrll
205 1.1 skrll if (howto->pc_relative)
206 1.1 skrll {
207 1.1 skrll /* This is a PC relative relocation. We want to set RELOCATION
208 1.1 skrll to the distance between the address of the symbol and the
209 1.1 skrll location. RELOCATION is already the address of the symbol.
210 1.1 skrll
211 1.1 skrll We start by subtracting the address of the section containing
212 1.1 skrll the location.
213 1.1 skrll
214 1.1 skrll If pcrel_offset is set, we must further subtract the position
215 1.1 skrll of the location within the section. Some targets arrange for
216 1.1 skrll the addend to be the negative of the position of the location
217 1.1 skrll within the section; for example, i386-aout does this. For
218 1.1 skrll i386-aout, pcrel_offset is FALSE. Some other targets do not
219 1.1 skrll include the position of the location; for example, m88kbcs,
220 1.1 skrll or ELF. For those targets, pcrel_offset is TRUE.
221 1.1 skrll
222 1.1 skrll If we are producing relocatable output, then we must ensure
223 1.1 skrll that this reloc will be correctly computed when the final
224 1.1 skrll relocation is done. If pcrel_offset is FALSE we want to wind
225 1.1 skrll up with the negative of the location within the section,
226 1.1 skrll which means we must adjust the existing addend by the change
227 1.1 skrll in the location within the section. If pcrel_offset is TRUE
228 1.1 skrll we do not want to adjust the existing addend at all.
229 1.1 skrll
230 1.1 skrll FIXME: This seems logical to me, but for the case of
231 1.1 skrll producing relocatable output it is not what the code
232 1.1 skrll actually does. I don't want to change it, because it seems
233 1.1 skrll far too likely that something will break. */
234 1.1 skrll relocation -=
235 1.1 skrll input_section->output_section->vma + input_section->output_offset;
236 1.1 skrll
237 1.1 skrll if (howto->pcrel_offset)
238 1.1 skrll relocation -= reloc_entry->address;
239 1.1 skrll }
240 1.1 skrll
241 1.1 skrll if (output_bfd != (bfd *) NULL)
242 1.1 skrll {
243 1.1 skrll if (! howto->partial_inplace)
244 1.1 skrll {
245 1.1 skrll /* This is a partial relocation, and we want to apply the relocation
246 1.1 skrll to the reloc entry rather than the raw data. Modify the reloc
247 1.1 skrll inplace to reflect what we now know. */
248 1.1 skrll reloc_entry->addend = relocation;
249 1.1 skrll reloc_entry->address += input_section->output_offset;
250 1.1 skrll return flag;
251 1.1 skrll }
252 1.1 skrll else
253 1.1 skrll {
254 1.1 skrll /* This is a partial relocation, but inplace, so modify the
255 1.1 skrll reloc record a bit.
256 1.1 skrll
257 1.1 skrll If we've relocated with a symbol with a section, change
258 1.1 skrll into a ref to the section belonging to the symbol. */
259 1.1 skrll
260 1.1 skrll reloc_entry->address += input_section->output_offset;
261 1.1 skrll
262 1.1 skrll /* WTF?? */
263 1.1 skrll if (abfd->xvec->flavour == bfd_target_coff_flavour)
264 1.1 skrll {
265 1.1 skrll /* For m68k-coff, the addend was being subtracted twice during
266 1.1 skrll relocation with -r. Removing the line below this comment
267 1.1 skrll fixes that problem; see PR 2953.
268 1.1 skrll
269 1.1 skrll However, Ian wrote the following, regarding removing the line
270 1.1 skrll below, which explains why it is still enabled: --djm
271 1.1 skrll
272 1.1 skrll If you put a patch like that into BFD you need to check all
273 1.1 skrll the COFF linkers. I am fairly certain that patch will break
274 1.1 skrll coff-i386 (e.g., SCO); see coff_i386_reloc in coff-i386.c
275 1.1 skrll where I worked around the problem in a different way. There
276 1.1 skrll may very well be a reason that the code works as it does.
277 1.1 skrll
278 1.1 skrll Hmmm. The first obvious point is that bfd_perform_relocation
279 1.1 skrll should not have any tests that depend upon the flavour. It's
280 1.1 skrll seem like entirely the wrong place for such a thing. The
281 1.1 skrll second obvious point is that the current code ignores the
282 1.1 skrll reloc addend when producing relocatable output for COFF.
283 1.1 skrll That's peculiar. In fact, I really have no idea what the
284 1.1 skrll point of the line you want to remove is.
285 1.1 skrll
286 1.1 skrll A typical COFF reloc subtracts the old value of the symbol
287 1.1 skrll and adds in the new value to the location in the object file
288 1.1 skrll (if it's a pc relative reloc it adds the difference between
289 1.1 skrll the symbol value and the location). When relocating we need
290 1.1 skrll to preserve that property.
291 1.1 skrll
292 1.1 skrll BFD handles this by setting the addend to the negative of the
293 1.1 skrll old value of the symbol. Unfortunately it handles common
294 1.1 skrll symbols in a non-standard way (it doesn't subtract the old
295 1.1 skrll value) but that's a different story (we can't change it
296 1.1 skrll without losing backward compatibility with old object files)
297 1.1 skrll (coff-i386 does subtract the old value, to be compatible with
298 1.1 skrll existing coff-i386 targets, like SCO).
299 1.1 skrll
300 1.1 skrll So everything works fine when not producing relocatable
301 1.1 skrll output. When we are producing relocatable output, logically
302 1.1 skrll we should do exactly what we do when not producing
303 1.1 skrll relocatable output. Therefore, your patch is correct. In
304 1.1 skrll fact, it should probably always just set reloc_entry->addend
305 1.1 skrll to 0 for all cases, since it is, in fact, going to add the
306 1.1 skrll value into the object file. This won't hurt the COFF code,
307 1.1 skrll which doesn't use the addend; I'm not sure what it will do
308 1.1 skrll to other formats (the thing to check for would be whether
309 1.1 skrll any formats both use the addend and set partial_inplace).
310 1.1 skrll
311 1.1 skrll When I wanted to make coff-i386 produce relocatable output,
312 1.1 skrll I ran into the problem that you are running into: I wanted
313 1.1 skrll to remove that line. Rather than risk it, I made the
314 1.1 skrll coff-i386 relocs use a special function; it's coff_i386_reloc
315 1.1 skrll in coff-i386.c. The function specifically adds the addend
316 1.1 skrll field into the object file, knowing that bfd_perform_relocation
317 1.1 skrll is not going to. If you remove that line, then coff-i386.c
318 1.1 skrll will wind up adding the addend field in twice. It's trivial
319 1.1 skrll to fix; it just needs to be done.
320 1.1 skrll
321 1.1 skrll The problem with removing the line is just that it may break
322 1.1 skrll some working code. With BFD it's hard to be sure of anything.
323 1.1 skrll The right way to deal with this is simply to build and test at
324 1.1 skrll least all the supported COFF targets. It should be
325 1.1 skrll straightforward if time and disk space consuming. For each
326 1.1 skrll target:
327 1.1 skrll 1) build the linker
328 1.1 skrll 2) generate some executable, and link it using -r (I would
329 1.1 skrll probably use paranoia.o and link against newlib/libc.a,
330 1.1 skrll which for all the supported targets would be available in
331 1.1 skrll /usr/cygnus/progressive/H-host/target/lib/libc.a).
332 1.1 skrll 3) make the change to reloc.c
333 1.1 skrll 4) rebuild the linker
334 1.1 skrll 5) repeat step 2
335 1.1 skrll 6) if the resulting object files are the same, you have at
336 1.1 skrll least made it no worse
337 1.1 skrll 7) if they are different you have to figure out which
338 1.1 skrll version is right. */
339 1.1 skrll relocation -= reloc_entry->addend;
340 1.1 skrll reloc_entry->addend = 0;
341 1.1 skrll }
342 1.1 skrll else
343 1.1 skrll {
344 1.1 skrll reloc_entry->addend = relocation;
345 1.1 skrll }
346 1.1 skrll }
347 1.1 skrll }
348 1.1 skrll else
349 1.1 skrll {
350 1.1 skrll reloc_entry->addend = 0;
351 1.1 skrll }
352 1.1 skrll
353 1.1 skrll /* FIXME: This overflow checking is incomplete, because the value
354 1.1 skrll might have overflowed before we get here. For a correct check we
355 1.1 skrll need to compute the value in a size larger than bitsize, but we
356 1.1 skrll can't reasonably do that for a reloc the same size as a host
357 1.1 skrll machine word.
358 1.1 skrll FIXME: We should also do overflow checking on the result after
359 1.1 skrll adding in the value contained in the object file. */
360 1.1 skrll if (howto->complain_on_overflow != complain_overflow_dont)
361 1.1 skrll {
362 1.1 skrll bfd_vma check;
363 1.1 skrll
364 1.1 skrll /* Get the value that will be used for the relocation, but
365 1.1 skrll starting at bit position zero. */
366 1.1 skrll if (howto->rightshift > howto->bitpos)
367 1.1 skrll check = relocation >> (howto->rightshift - howto->bitpos);
368 1.1 skrll else
369 1.1 skrll check = relocation << (howto->bitpos - howto->rightshift);
370 1.1 skrll switch (howto->complain_on_overflow)
371 1.1 skrll {
372 1.1 skrll case complain_overflow_signed:
373 1.1 skrll {
374 1.1 skrll /* Assumes two's complement. */
375 1.1 skrll bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
376 1.1 skrll bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
377 1.1 skrll
378 1.1 skrll /* The above right shift is incorrect for a signed value.
379 1.1 skrll Fix it up by forcing on the upper bits. */
380 1.1 skrll if (howto->rightshift > howto->bitpos
381 1.1 skrll && (bfd_signed_vma) relocation < 0)
382 1.1 skrll check |= ((bfd_vma) - 1
383 1.1 skrll & ~((bfd_vma) - 1
384 1.1 skrll >> (howto->rightshift - howto->bitpos)));
385 1.1 skrll if ((bfd_signed_vma) check > reloc_signed_max
386 1.1 skrll || (bfd_signed_vma) check < reloc_signed_min)
387 1.1 skrll flag = bfd_reloc_overflow;
388 1.1 skrll }
389 1.1 skrll break;
390 1.1 skrll case complain_overflow_unsigned:
391 1.1 skrll {
392 1.1 skrll /* Assumes two's complement. This expression avoids
393 1.1 skrll overflow if howto->bitsize is the number of bits in
394 1.1 skrll bfd_vma. */
395 1.1 skrll bfd_vma reloc_unsigned_max =
396 1.1 skrll (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
397 1.1 skrll
398 1.1 skrll if ((bfd_vma) check > reloc_unsigned_max)
399 1.1 skrll flag = bfd_reloc_overflow;
400 1.1 skrll }
401 1.1 skrll break;
402 1.1 skrll case complain_overflow_bitfield:
403 1.1 skrll {
404 1.1 skrll /* Assumes two's complement. This expression avoids
405 1.1 skrll overflow if howto->bitsize is the number of bits in
406 1.1 skrll bfd_vma. */
407 1.1 skrll bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
408 1.1 skrll
409 1.1 skrll if (((bfd_vma) check & ~reloc_bits) != 0
410 1.1 skrll && (((bfd_vma) check & ~reloc_bits)
411 1.1 skrll != (-(bfd_vma) 1 & ~reloc_bits)))
412 1.1 skrll {
413 1.1 skrll /* The above right shift is incorrect for a signed
414 1.1 skrll value. See if turning on the upper bits fixes the
415 1.1 skrll overflow. */
416 1.1 skrll if (howto->rightshift > howto->bitpos
417 1.1 skrll && (bfd_signed_vma) relocation < 0)
418 1.1 skrll {
419 1.1 skrll check |= ((bfd_vma) - 1
420 1.1 skrll & ~((bfd_vma) - 1
421 1.1 skrll >> (howto->rightshift - howto->bitpos)));
422 1.1 skrll if (((bfd_vma) check & ~reloc_bits)
423 1.1 skrll != (-(bfd_vma) 1 & ~reloc_bits))
424 1.1 skrll flag = bfd_reloc_overflow;
425 1.1 skrll }
426 1.1 skrll else
427 1.1 skrll flag = bfd_reloc_overflow;
428 1.1 skrll }
429 1.1 skrll }
430 1.1 skrll break;
431 1.1 skrll default:
432 1.1 skrll abort ();
433 1.1 skrll }
434 1.1 skrll }
435 1.1 skrll
436 1.1 skrll /* Either we are relocating all the way, or we don't want to apply
437 1.1 skrll the relocation to the reloc entry (probably because there isn't
438 1.1 skrll any room in the output format to describe addends to relocs). */
439 1.1 skrll
440 1.1 skrll /* The cast to bfd_vma avoids a bug in the Alpha OSF/1 C compiler
441 1.1 skrll (OSF version 1.3, compiler version 3.11). It miscompiles the
442 1.1 skrll following program:
443 1.1 skrll
444 1.1 skrll struct str
445 1.1 skrll {
446 1.1 skrll unsigned int i0;
447 1.1 skrll } s = { 0 };
448 1.1 skrll
449 1.1 skrll int
450 1.1 skrll main ()
451 1.1 skrll {
452 1.1 skrll unsigned long x;
453 1.1 skrll
454 1.1 skrll x = 0x100000000;
455 1.1 skrll x <<= (unsigned long) s.i0;
456 1.1 skrll if (x == 0)
457 1.1 skrll printf ("failed\n");
458 1.1 skrll else
459 1.1 skrll printf ("succeeded (%lx)\n", x);
460 1.1 skrll }
461 1.1 skrll */
462 1.1 skrll
463 1.1 skrll relocation >>= (bfd_vma) howto->rightshift;
464 1.1 skrll
465 1.1 skrll /* Shift everything up to where it's going to be used. */
466 1.1 skrll relocation <<= (bfd_vma) howto->bitpos;
467 1.1 skrll
468 1.1 skrll /* Wait for the day when all have the mask in them. */
469 1.1 skrll
470 1.1 skrll /* What we do:
471 1.1 skrll i instruction to be left alone
472 1.1 skrll o offset within instruction
473 1.1 skrll r relocation offset to apply
474 1.1 skrll S src mask
475 1.1 skrll D dst mask
476 1.1 skrll N ~dst mask
477 1.1 skrll A part 1
478 1.1 skrll B part 2
479 1.1 skrll R result
480 1.1 skrll
481 1.1 skrll Do this:
482 1.1 skrll i i i i i o o o o o from bfd_get<size>
483 1.1 skrll and S S S S S to get the size offset we want
484 1.1 skrll + r r r r r r r r r r to get the final value to place
485 1.1 skrll and D D D D D to chop to right size
486 1.1 skrll -----------------------
487 1.1 skrll A A A A A
488 1.1 skrll And this:
489 1.1 skrll ... i i i i i o o o o o from bfd_get<size>
490 1.1 skrll and N N N N N get instruction
491 1.1 skrll -----------------------
492 1.1 skrll ... B B B B B
493 1.1 skrll
494 1.1 skrll And then:
495 1.1 skrll B B B B B
496 1.1 skrll or A A A A A
497 1.1 skrll -----------------------
498 1.1 skrll R R R R R R R R R R put into bfd_put<size>. */
499 1.1 skrll
500 1.1 skrll #define DOIT(x) \
501 1.1 skrll x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
502 1.1 skrll
503 1.1 skrll location = (bfd_byte *) data + addr;
504 1.1 skrll switch (howto->size)
505 1.1 skrll {
506 1.1 skrll case 0:
507 1.1 skrll {
508 1.1 skrll bfd_vma x = get_data (location, 1);
509 1.1 skrll DOIT (x);
510 1.1 skrll put_data ((bfd_vma) x, location, 1);
511 1.1 skrll }
512 1.1 skrll break;
513 1.1 skrll
514 1.1 skrll case 1:
515 1.1 skrll if (relocation)
516 1.1 skrll {
517 1.1 skrll bfd_vma x = get_data (location, 2);
518 1.1 skrll DOIT (x);
519 1.1 skrll put_data ((bfd_vma) x, location, 2);
520 1.1 skrll }
521 1.1 skrll break;
522 1.1 skrll case 2:
523 1.1 skrll if (relocation)
524 1.1 skrll {
525 1.1 skrll bfd_vma x = get_data (location, 4);
526 1.1 skrll DOIT (x);
527 1.1 skrll put_data ((bfd_vma) x, location, 4);
528 1.1 skrll }
529 1.1 skrll break;
530 1.1 skrll case -2:
531 1.1 skrll {
532 1.1 skrll bfd_vma x = get_data (location, 4);
533 1.1 skrll relocation = -relocation;
534 1.1 skrll DOIT(x);
535 1.1 skrll put_data ((bfd_vma) x, location, 4);
536 1.1 skrll }
537 1.1 skrll break;
538 1.1 skrll
539 1.1 skrll case 3:
540 1.1 skrll /* Do nothing. */
541 1.1 skrll break;
542 1.1 skrll
543 1.1 skrll case 4:
544 1.1 skrll #ifdef BFD64
545 1.1 skrll if (relocation)
546 1.1 skrll {
547 1.1 skrll bfd_vma x = get_data (location, 8);
548 1.1 skrll DOIT (x);
549 1.1 skrll put_data (x, location, 8);
550 1.1 skrll }
551 1.1 skrll #else
552 1.1 skrll abort ();
553 1.1 skrll #endif
554 1.1 skrll break;
555 1.1 skrll default:
556 1.1 skrll return bfd_reloc_other;
557 1.1 skrll }
558 1.1 skrll if ((howto->complain_on_overflow != complain_overflow_dont) && overflow)
559 1.1 skrll return bfd_reloc_overflow;
560 1.1 skrll
561 1.1 skrll return flag;
562 1.1 skrll }
563 1.1 skrll
564 1.1 skrll /* Relocate a given location using a given value and howto. */
565 1.1 skrll
566 1.1 skrll bfd_reloc_status_type
567 1.1.1.2 christos _bfd_do_ns32k_reloc_contents (reloc_howto_type *howto,
568 1.1.1.2 christos bfd *input_bfd ATTRIBUTE_UNUSED,
569 1.1.1.2 christos bfd_vma relocation,
570 1.1.1.2 christos bfd_byte *location,
571 1.1.1.2 christos bfd_vma (*get_data) (bfd_byte *, int),
572 1.1.1.2 christos void (*put_data) (bfd_vma, bfd_byte *, int))
573 1.1 skrll {
574 1.1 skrll int size;
575 1.1 skrll bfd_vma x;
576 1.1 skrll bfd_boolean overflow;
577 1.1 skrll
578 1.1 skrll /* If the size is negative, negate RELOCATION. This isn't very
579 1.1 skrll general. */
580 1.1 skrll if (howto->size < 0)
581 1.1 skrll relocation = -relocation;
582 1.1 skrll
583 1.1 skrll /* Get the value we are going to relocate. */
584 1.1 skrll size = bfd_get_reloc_size (howto);
585 1.1 skrll switch (size)
586 1.1 skrll {
587 1.1 skrll default:
588 1.1 skrll abort ();
589 1.1.1.3 christos case 0:
590 1.1.1.3 christos return bfd_reloc_ok;
591 1.1 skrll case 1:
592 1.1 skrll case 2:
593 1.1 skrll case 4:
594 1.1 skrll #ifdef BFD64
595 1.1 skrll case 8:
596 1.1 skrll #endif
597 1.1 skrll x = get_data (location, size);
598 1.1 skrll break;
599 1.1 skrll }
600 1.1 skrll
601 1.1 skrll /* Check for overflow. FIXME: We may drop bits during the addition
602 1.1 skrll which we don't check for. We must either check at every single
603 1.1 skrll operation, which would be tedious, or we must do the computations
604 1.1 skrll in a type larger than bfd_vma, which would be inefficient. */
605 1.1 skrll overflow = FALSE;
606 1.1 skrll if (howto->complain_on_overflow != complain_overflow_dont)
607 1.1 skrll {
608 1.1 skrll bfd_vma check;
609 1.1 skrll bfd_signed_vma signed_check;
610 1.1 skrll bfd_vma add;
611 1.1 skrll bfd_signed_vma signed_add;
612 1.1 skrll
613 1.1 skrll if (howto->rightshift == 0)
614 1.1 skrll {
615 1.1 skrll check = relocation;
616 1.1 skrll signed_check = (bfd_signed_vma) relocation;
617 1.1 skrll }
618 1.1 skrll else
619 1.1 skrll {
620 1.1 skrll /* Drop unwanted bits from the value we are relocating to. */
621 1.1 skrll check = relocation >> howto->rightshift;
622 1.1 skrll
623 1.1 skrll /* If this is a signed value, the rightshift just dropped
624 1.1 skrll leading 1 bits (assuming twos complement). */
625 1.1 skrll if ((bfd_signed_vma) relocation >= 0)
626 1.1 skrll signed_check = check;
627 1.1 skrll else
628 1.1 skrll signed_check = (check
629 1.1 skrll | ((bfd_vma) - 1
630 1.1 skrll & ~((bfd_vma) - 1 >> howto->rightshift)));
631 1.1 skrll }
632 1.1 skrll
633 1.1 skrll /* Get the value from the object file. */
634 1.1 skrll add = x & howto->src_mask;
635 1.1 skrll
636 1.1 skrll /* Get the value from the object file with an appropriate sign.
637 1.1 skrll The expression involving howto->src_mask isolates the upper
638 1.1 skrll bit of src_mask. If that bit is set in the value we are
639 1.1 skrll adding, it is negative, and we subtract out that number times
640 1.1 skrll two. If src_mask includes the highest possible bit, then we
641 1.1 skrll can not get the upper bit, but that does not matter since
642 1.1 skrll signed_add needs no adjustment to become negative in that
643 1.1 skrll case. */
644 1.1 skrll signed_add = add;
645 1.1 skrll if ((add & (((~howto->src_mask) >> 1) & howto->src_mask)) != 0)
646 1.1 skrll signed_add -= (((~howto->src_mask) >> 1) & howto->src_mask) << 1;
647 1.1 skrll
648 1.1 skrll /* Add the value from the object file, shifted so that it is a
649 1.1 skrll straight number. */
650 1.1 skrll if (howto->bitpos == 0)
651 1.1 skrll {
652 1.1 skrll check += add;
653 1.1 skrll signed_check += signed_add;
654 1.1 skrll }
655 1.1 skrll else
656 1.1 skrll {
657 1.1 skrll check += add >> howto->bitpos;
658 1.1 skrll
659 1.1 skrll /* For the signed case we use ADD, rather than SIGNED_ADD,
660 1.1 skrll to avoid warnings from SVR4 cc. This is OK since we
661 1.1 skrll explicitly handle the sign bits. */
662 1.1 skrll if (signed_add >= 0)
663 1.1 skrll signed_check += add >> howto->bitpos;
664 1.1 skrll else
665 1.1 skrll signed_check += ((add >> howto->bitpos)
666 1.1 skrll | ((bfd_vma) - 1
667 1.1 skrll & ~((bfd_vma) - 1 >> howto->bitpos)));
668 1.1 skrll }
669 1.1 skrll
670 1.1 skrll switch (howto->complain_on_overflow)
671 1.1 skrll {
672 1.1 skrll case complain_overflow_signed:
673 1.1 skrll {
674 1.1 skrll /* Assumes two's complement. */
675 1.1 skrll bfd_signed_vma reloc_signed_max = (1 << (howto->bitsize - 1)) - 1;
676 1.1 skrll bfd_signed_vma reloc_signed_min = ~reloc_signed_max;
677 1.1 skrll
678 1.1 skrll if (signed_check > reloc_signed_max
679 1.1 skrll || signed_check < reloc_signed_min)
680 1.1 skrll overflow = TRUE;
681 1.1 skrll }
682 1.1 skrll break;
683 1.1 skrll case complain_overflow_unsigned:
684 1.1 skrll {
685 1.1 skrll /* Assumes two's complement. This expression avoids
686 1.1 skrll overflow if howto->bitsize is the number of bits in
687 1.1 skrll bfd_vma. */
688 1.1 skrll bfd_vma reloc_unsigned_max =
689 1.1 skrll (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
690 1.1 skrll
691 1.1 skrll if (check > reloc_unsigned_max)
692 1.1 skrll overflow = TRUE;
693 1.1 skrll }
694 1.1 skrll break;
695 1.1 skrll case complain_overflow_bitfield:
696 1.1 skrll {
697 1.1 skrll /* Assumes two's complement. This expression avoids
698 1.1 skrll overflow if howto->bitsize is the number of bits in
699 1.1 skrll bfd_vma. */
700 1.1 skrll bfd_vma reloc_bits = (((1 << (howto->bitsize - 1)) - 1) << 1) | 1;
701 1.1 skrll
702 1.1 skrll if ((check & ~reloc_bits) != 0
703 1.1 skrll && (((bfd_vma) signed_check & ~reloc_bits)
704 1.1 skrll != (-(bfd_vma) 1 & ~reloc_bits)))
705 1.1 skrll overflow = TRUE;
706 1.1 skrll }
707 1.1 skrll break;
708 1.1 skrll default:
709 1.1 skrll abort ();
710 1.1 skrll }
711 1.1 skrll }
712 1.1 skrll
713 1.1 skrll /* Put RELOCATION in the right bits. */
714 1.1 skrll relocation >>= (bfd_vma) howto->rightshift;
715 1.1 skrll relocation <<= (bfd_vma) howto->bitpos;
716 1.1 skrll
717 1.1 skrll /* Add RELOCATION to the right bits of X. */
718 1.1 skrll x = ((x & ~howto->dst_mask)
719 1.1 skrll | (((x & howto->src_mask) + relocation) & howto->dst_mask));
720 1.1 skrll
721 1.1 skrll /* Put the relocated value back in the object file. */
722 1.1 skrll switch (size)
723 1.1 skrll {
724 1.1 skrll default:
725 1.1 skrll case 0:
726 1.1 skrll abort ();
727 1.1 skrll case 1:
728 1.1 skrll case 2:
729 1.1 skrll case 4:
730 1.1 skrll #ifdef BFD64
731 1.1 skrll case 8:
732 1.1 skrll #endif
733 1.1 skrll put_data (x, location, size);
734 1.1 skrll break;
735 1.1 skrll }
736 1.1 skrll
737 1.1 skrll return overflow ? bfd_reloc_overflow : bfd_reloc_ok;
738 1.1 skrll }
739 1.1 skrll
740 1.1 skrll bfd_reloc_status_type
741 1.1.1.2 christos _bfd_ns32k_reloc_disp (bfd *abfd,
742 1.1.1.2 christos arelent *reloc_entry,
743 1.1.1.2 christos struct bfd_symbol *symbol,
744 1.1.1.2 christos void * data,
745 1.1.1.2 christos asection *input_section,
746 1.1.1.2 christos bfd *output_bfd,
747 1.1.1.2 christos char **error_message)
748 1.1 skrll {
749 1.1 skrll return do_ns32k_reloc (abfd, reloc_entry, symbol, data, input_section,
750 1.1 skrll output_bfd, error_message,
751 1.1 skrll _bfd_ns32k_get_displacement,
752 1.1 skrll _bfd_ns32k_put_displacement);
753 1.1 skrll }
754 1.1 skrll
755 1.1 skrll bfd_reloc_status_type
756 1.1.1.2 christos _bfd_ns32k_reloc_imm (bfd *abfd,
757 1.1.1.2 christos arelent *reloc_entry,
758 1.1.1.2 christos struct bfd_symbol *symbol,
759 1.1.1.2 christos void * data,
760 1.1.1.2 christos asection *input_section,
761 1.1.1.2 christos bfd *output_bfd,
762 1.1.1.2 christos char **error_message)
763 1.1 skrll {
764 1.1 skrll return do_ns32k_reloc (abfd, reloc_entry, symbol, data, input_section,
765 1.1 skrll output_bfd, error_message, _bfd_ns32k_get_immediate,
766 1.1 skrll _bfd_ns32k_put_immediate);
767 1.1 skrll }
768 1.1 skrll
769 1.1 skrll bfd_reloc_status_type
770 1.1.1.2 christos _bfd_ns32k_final_link_relocate (reloc_howto_type *howto,
771 1.1.1.2 christos bfd *input_bfd,
772 1.1.1.2 christos asection *input_section,
773 1.1.1.2 christos bfd_byte *contents,
774 1.1.1.2 christos bfd_vma address,
775 1.1.1.2 christos bfd_vma value,
776 1.1.1.2 christos bfd_vma addend)
777 1.1 skrll {
778 1.1 skrll bfd_vma relocation;
779 1.1 skrll
780 1.1 skrll /* Sanity check the address. */
781 1.1 skrll if (address > bfd_get_section_limit (input_bfd, input_section))
782 1.1 skrll return bfd_reloc_outofrange;
783 1.1 skrll
784 1.1 skrll /* This function assumes that we are dealing with a basic relocation
785 1.1 skrll against a symbol. We want to compute the value of the symbol to
786 1.1 skrll relocate to. This is just VALUE, the value of the symbol, plus
787 1.1 skrll ADDEND, any addend associated with the reloc. */
788 1.1 skrll relocation = value + addend;
789 1.1 skrll
790 1.1 skrll /* If the relocation is PC relative, we want to set RELOCATION to
791 1.1 skrll the distance between the symbol (currently in RELOCATION) and the
792 1.1 skrll location we are relocating. Some targets (e.g., i386-aout)
793 1.1 skrll arrange for the contents of the section to be the negative of the
794 1.1 skrll offset of the location within the section; for such targets
795 1.1 skrll pcrel_offset is FALSE. Other targets (e.g., m88kbcs or ELF)
796 1.1 skrll simply leave the contents of the section as zero; for such
797 1.1 skrll targets pcrel_offset is TRUE. If pcrel_offset is FALSE we do not
798 1.1 skrll need to subtract out the offset of the location within the
799 1.1 skrll section (which is just ADDRESS). */
800 1.1 skrll if (howto->pc_relative)
801 1.1 skrll {
802 1.1 skrll relocation -= (input_section->output_section->vma
803 1.1 skrll + input_section->output_offset);
804 1.1 skrll if (howto->pcrel_offset)
805 1.1 skrll relocation -= address;
806 1.1 skrll }
807 1.1 skrll
808 1.1 skrll return _bfd_ns32k_relocate_contents (howto, input_bfd, relocation,
809 1.1 skrll contents + address);
810 1.1 skrll }
811