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