* config.sub, config.guess: Update from upstream sources.
[binutils.git] / bfd / coff-maxq.c
blob6414cb9c206ef5b5c397da1c77acfe2e07e20023
1 /* BFD back-end for MAXQ COFF binaries.
2 Copyright 2004 Free Software Foundation, Inc.
4 Contributed by Vineet Sharma (vineets@noida.hcltech.com) Inderpreet S.
5 (inderpreetb@noida.hcltech.com)
7 HCL Technologies Ltd.
9 This file is part of BFD, the Binary File Descriptor library.
11 This program is free software; you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 2 of the License, or (at your option)
14 any later version.
16 This program is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19 for more details.
21 You should have received a copy of the GNU General Public License along
22 with this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
25 #include "bfd.h"
26 #include "sysdep.h"
27 #include "libbfd.h"
28 #include "coff/maxq.h"
29 #include "coff/internal.h"
30 #include "libcoff.h"
31 #include "libiberty.h"
33 #ifndef MAXQ20
34 #define MAXQ20 1
35 #endif
37 #define RTYPE2HOWTO(cache_ptr, dst) \
38 ((cache_ptr)->howto = \
39 ((dst)->r_type < 48 \
40 ? howto_table + (((dst)->r_type==47) ? 6: ((dst)->r_type)) \
41 : NULL))
43 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
45 /* Code to swap in the reloc offset. */
46 #define SWAP_IN_RELOC_OFFSET H_GET_16
47 #define SWAP_OUT_RELOC_OFFSET H_PUT_16
49 #define SHORT_JUMP BFD_RELOC_16_PCREL_S2
50 #define LONG_JUMP BFD_RELOC_14
51 #define ABSOLUTE_ADDR_FOR_DATA BFD_RELOC_24
53 /* checks the range of short jump -127 to 128 */
54 #define IS_SJUMP_RANGE(x) ((x > -128) && (x < 129))
55 #define HIGH_WORD_MASK 0xff00
56 #define LOW_WORD_MASK 0x00ff
58 static long
59 get_symbol_value (asymbol *symbol)
61 long relocation = 0;
63 if (bfd_is_com_section (symbol->section))
64 relocation = 0;
65 else
66 relocation = symbol->value +
67 symbol->section->output_section->vma + symbol->section->output_offset;
69 return relocation;
72 /* This function performs all the maxq relocations.
73 FIXME: The handling of the addend in the 'BFD_*'
74 relocations types. */
76 static bfd_reloc_status_type
77 coff_maxq20_reloc (bfd * abfd,
78 arelent * reloc_entry,
79 asymbol * symbol_in,
80 void * data,
81 asection * input_section ATTRIBUTE_UNUSED,
82 bfd * output_bfd ATTRIBUTE_UNUSED,
83 char ** error_message ATTRIBUTE_UNUSED)
85 reloc_howto_type *howto = NULL;
86 unsigned char *addr = NULL;
87 unsigned long x = 0;
88 long call_addr = 0;
89 short addend = 0;
90 long diff = 0;
92 /* If this is an undefined symbol, return error. */
93 if (symbol_in->section == &bfd_und_section
94 && (symbol_in->flags & BSF_WEAK) == 0)
95 return bfd_reloc_continue;
97 if (data && reloc_entry)
99 howto = reloc_entry->howto;
100 addr = (unsigned char *) data + reloc_entry->address;
101 call_addr = call_addr - call_addr;
102 call_addr = get_symbol_value (symbol_in);
104 /* Over here the value val stores the 8 bit/16 bit value. We will put a
105 check if we are moving a 16 bit immediate value into an 8 bit
106 register. In that case we will generate a Upper bytes into PFX[0]
107 and move the lower 8 bits as SRC. */
109 switch (reloc_entry->howto->type)
111 /* BFD_RELOC_16_PCREL_S2 47 Handles all the relative jumps and
112 calls Note: Every relative jump or call is in words. */
113 case SHORT_JUMP:
114 /* Handle any addend. */
115 addend = reloc_entry->addend;
117 if (addend > call_addr || addend > 0)
118 call_addr = symbol_in->section->output_section->vma + addend;
119 else if (addend < call_addr && addend > 0)
120 call_addr = call_addr + addend;
121 else if (addend < 0)
122 call_addr = call_addr + addend;
124 diff = ((call_addr << 1) - (reloc_entry->address << 1));
126 if (!IS_SJUMP_RANGE (diff))
128 bfd_perror (_("Can't Make it a Short Jump"));
129 return bfd_reloc_outofrange;
132 x = bfd_get_16 (abfd, addr);
134 x = x & LOW_WORD_MASK;
135 x = x | (diff << 8);
136 bfd_put_16 (abfd, (bfd_vma) x, addr);
138 return bfd_reloc_ok;
140 case ABSOLUTE_ADDR_FOR_DATA:
141 case LONG_JUMP:
142 /* BFD_RELOC_14 Handles intersegment or long jumps which might be
143 from code to code or code to data segment jumps. Note: When this
144 fucntion is called by gas the section flags somehow do not
145 contain the info about the section type(CODE or DATA). Thus the
146 user needs to evoke the linker after assembling the files
147 because the Code-Code relocs are word aligned but code-data are
148 byte aligned. */
149 addend = (reloc_entry->addend - reloc_entry->addend);
151 /* Handle any addend. */
152 addend = reloc_entry->addend;
154 /* For relocation involving multiple file added becomes zero thus
155 this fails - check for zero added. In another case when we try
156 to add a stub to a file the addend shows the offset from the
157 start od this file. */
158 addend = 0;
160 if (!bfd_is_com_section (symbol_in->section) &&
161 ((symbol_in->flags & BSF_OLD_COMMON) == 0))
163 if (reloc_entry->addend > symbol_in->value)
164 addend = reloc_entry->addend - symbol_in->value;
166 if ((reloc_entry->addend < symbol_in->value)
167 && (reloc_entry->addend != 0))
168 addend = reloc_entry->addend - symbol_in->value;
170 if (reloc_entry->addend == symbol_in->value)
171 addend = 0;
174 if (bfd_is_com_section (symbol_in->section) ||
175 ((symbol_in->flags & BSF_OLD_COMMON) != 0))
176 addend = reloc_entry->addend;
178 if (addend < 0
179 && (call_addr < (long) (addend * (-1))))
180 addend = 0;
182 call_addr += addend;
184 /* FIXME: This check does not work well with the assembler,
185 linker needs to be run always. */
186 if ((symbol_in->section->flags & SEC_CODE) == SEC_CODE)
188 /* Convert it into words. */
189 call_addr = call_addr >> 1;
191 if (call_addr > 0xFFFF) /* Intersegment Jump. */
193 bfd_perror (_("Exceeds Long Jump Range"));
194 return bfd_reloc_outofrange;
197 else
199 /* case ABSOLUTE_ADDR_FOR_DATA : Resolves any code-data
200 segemnt relocs. These are NOT word aligned. */
202 if (call_addr > 0xFFFF) /* Intersegment Jump. */
204 bfd_perror (_("Absolute address Exceeds 16 bit Range"));
205 return bfd_reloc_outofrange;
209 x = bfd_get_32 (abfd, addr);
211 x = (x & 0xFF00FF00);
212 x = (x | ((call_addr & HIGH_WORD_MASK) >> 8));
213 x = (x | (call_addr & LOW_WORD_MASK) << 16);
215 bfd_put_32 (abfd, (bfd_vma) x, addr);
216 return bfd_reloc_ok;
218 case BFD_RELOC_8:
219 addend = (reloc_entry->addend - reloc_entry->addend);
221 if (!bfd_is_com_section (symbol_in->section) &&
222 ((symbol_in->flags & BSF_OLD_COMMON) == 0))
224 if (reloc_entry->addend > symbol_in->value)
225 addend = reloc_entry->addend - symbol_in->value;
226 if (reloc_entry->addend < symbol_in->value)
227 addend = reloc_entry->addend - symbol_in->value;
228 if (reloc_entry->addend == symbol_in->value)
229 addend = 0;
232 if (bfd_is_com_section (symbol_in->section) ||
233 ((symbol_in->flags & BSF_OLD_COMMON) != 0))
234 addend = reloc_entry->addend;
236 if (addend < 0
237 && (call_addr < (long) (addend * (-1))))
238 addend = 0;
240 if (call_addr + addend > 0xFF)
242 bfd_perror (_("Absolute address Exceeds 8 bit Range"));
243 return bfd_reloc_outofrange;
246 x = bfd_get_8 (abfd, addr);
247 x = x & 0x00;
248 x = x | (call_addr + addend);
250 bfd_put_8 (abfd, (bfd_vma) x, addr);
251 return bfd_reloc_ok;
253 case BFD_RELOC_16:
254 addend = (reloc_entry->addend - reloc_entry->addend);
255 if (!bfd_is_com_section (symbol_in->section) &&
256 ((symbol_in->flags & BSF_OLD_COMMON) == 0))
258 if (reloc_entry->addend > symbol_in->value)
259 addend = reloc_entry->addend - symbol_in->value;
261 if (reloc_entry->addend < symbol_in->value)
262 addend = reloc_entry->addend - symbol_in->value;
264 if (reloc_entry->addend == symbol_in->value)
265 addend = 0;
268 if (bfd_is_com_section (symbol_in->section) ||
269 ((symbol_in->flags & BSF_OLD_COMMON) != 0))
270 addend = reloc_entry->addend;
272 if (addend < 0
273 && (call_addr < (long) (addend * (-1))))
274 addend = 0;
276 if ((call_addr + addend) > 0xFFFF)
278 bfd_perror (_("Absolute address Exceeds 16 bit Range"));
279 return bfd_reloc_outofrange;
281 else
283 unsigned short val = (call_addr + addend);
285 x = bfd_get_16 (abfd, addr);
287 /* LE */
288 x = (x & 0x0000); /* Flush garbage value. */
289 x = val;
290 if ((symbol_in->section->flags & SEC_CODE) == SEC_CODE)
291 x = x >> 1; /* Convert it into words. */
294 bfd_put_16 (abfd, (bfd_vma) x, addr);
295 return bfd_reloc_ok;
297 case BFD_RELOC_32:
298 addend = (reloc_entry->addend - reloc_entry->addend);
300 if (!bfd_is_com_section (symbol_in->section) &&
301 ((symbol_in->flags & BSF_OLD_COMMON) == 0))
303 if (reloc_entry->addend > symbol_in->value)
304 addend = reloc_entry->addend - symbol_in->value;
305 if (reloc_entry->addend < symbol_in->value)
306 addend = reloc_entry->addend - symbol_in->value;
307 if (reloc_entry->addend == symbol_in->value)
308 addend = 0;
311 if (bfd_is_com_section (symbol_in->section) ||
312 ((symbol_in->flags & BSF_OLD_COMMON) != 0))
313 addend = reloc_entry->addend;
315 if (addend < 0
316 && (call_addr < (long) (addend * (-1))))
317 addend = 0;
319 if ((call_addr + addend) < 0)
321 bfd_perror ("Absolute address Exceeds 32 bit Range");
322 return bfd_reloc_outofrange;
325 x = bfd_get_32 (abfd, addr);
326 x = (x & 0x0000); /* Flush garbage value. */
327 x = call_addr + addend;
328 if ((symbol_in->section->flags & SEC_CODE) == SEC_CODE)
329 x = x >> 1; /* Convert it into words. */
331 bfd_put_32 (abfd, (bfd_vma) x, addr);
332 return bfd_reloc_ok;
334 default:
335 bfd_perror (_("Unrecognized Reloc Type"));
336 return bfd_reloc_notsupported;
340 return bfd_reloc_notsupported;
343 static reloc_howto_type howto_table[] =
345 EMPTY_HOWTO (0),
346 EMPTY_HOWTO (1),
348 BFD_RELOC_32, 0, 1, 8, FALSE, 0, complain_overflow_bitfield,
349 coff_maxq20_reloc, "32Bit", TRUE, 0x000000ff, 0x000000ff, TRUE
352 SHORT_JUMP, 0, 1, 8, FALSE, 0, complain_overflow_bitfield,
353 coff_maxq20_reloc, "SHORT_JMP", TRUE, 0x000000ff, 0x000000ff, TRUE
356 ABSOLUTE_ADDR_FOR_DATA, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
357 coff_maxq20_reloc, "INTERSEGMENT_RELOC", TRUE, 0x00000000, 0x00000000,
358 FALSE
361 BFD_RELOC_16, 0, 1, 8, FALSE, 0, complain_overflow_bitfield,
362 coff_maxq20_reloc, "16Bit", TRUE, 0x000000ff, 0x000000ff, TRUE
365 LONG_JUMP, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,
366 coff_maxq20_reloc, "LONG_JUMP", TRUE, 0x00000000, 0x00000000, FALSE
369 BFD_RELOC_8, 0, 1, 8, FALSE, 0, complain_overflow_bitfield,
370 coff_maxq20_reloc, "8bit", TRUE, 0x000000ff, 0x000000ff, TRUE
372 EMPTY_HOWTO (8),
373 EMPTY_HOWTO (9),
374 EMPTY_HOWTO (10),
377 /* Map BFD reloc types to MAXQ COFF reloc types. */
379 typedef struct maxq_reloc_map
381 bfd_reloc_code_real_type bfd_reloc_val;
382 unsigned int maxq_reloc_val;
383 reloc_howto_type * table;
385 reloc_map;
387 static const reloc_map maxq_reloc_map[] =
389 {BFD_RELOC_16_PCREL_S2, SHORT_JUMP, howto_table},
390 {BFD_RELOC_16, LONG_JUMP, howto_table},
393 static reloc_howto_type *
394 maxq_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
395 bfd_reloc_code_real_type code)
397 unsigned int i;
399 for (i = 0; i < ARRAY_SIZE (maxq_reloc_map); i++)
401 const reloc_map *entry;
403 entry = maxq_reloc_map + i;
405 switch (code)
407 /* SHORT JUMP */
408 case BFD_RELOC_16_PCREL_S2:
409 return howto_table + 3;
411 /* INTERSEGMENT JUMP */
412 case BFD_RELOC_24:
413 return howto_table + 4;
415 /* BYTE RELOC */
416 case BFD_RELOC_8:
417 return howto_table + 7;
419 /* WORD RELOC */
420 case BFD_RELOC_16:
421 return howto_table + 5;
423 /* LONG RELOC */
424 case BFD_RELOC_32:
425 return howto_table + 2;
427 /* LONG JUMP */
428 case BFD_RELOC_14:
429 return howto_table + 6;
431 default:
432 return NULL;
436 return NULL;
439 #define coff_bfd_reloc_type_lookup maxq_reloc_type_lookup
441 /* Perform any necessary magic to the addend in a reloc entry. */
442 #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
443 cache_ptr->addend = ext_reloc.r_offset;
445 #include "coffcode.h"
447 #ifndef TARGET_UNDERSCORE
448 #define TARGET_UNDERSCORE 1
449 #endif
451 #ifndef EXTRA_S_FLAGS
452 #define EXTRA_S_FLAGS 0
453 #endif
455 /* Forward declaration for use initialising alternative_target field. */
456 CREATE_LITTLE_COFF_TARGET_VEC (maxqcoff_vec, "coff-maxq", 0, EXTRA_S_FLAGS,
457 TARGET_UNDERSCORE, NULL, COFF_SWAP_TABLE);