]>
Commit | Line | Data |
---|---|---|
99c513f6 | 1 | /* tc-rl78.c -- Assembler for the Renesas RL78 |
4b95cf5c | 2 | Copyright (C) 2011-2014 Free Software Foundation, Inc. |
99c513f6 DD |
3 | |
4 | This file is part of GAS, the GNU Assembler. | |
5 | ||
6 | GAS is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 3, or (at your option) | |
9 | any later version. | |
10 | ||
11 | GAS is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GAS; see the file COPYING. If not, write to the Free | |
18 | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA | |
19 | 02110-1301, USA. */ | |
20 | ||
21 | #include "as.h" | |
22 | #include "struc-symbol.h" | |
23 | #include "obstack.h" | |
24 | #include "safe-ctype.h" | |
25 | #include "dwarf2dbg.h" | |
26 | #include "libbfd.h" | |
27 | #include "elf/common.h" | |
28 | #include "elf/rl78.h" | |
29 | #include "rl78-defs.h" | |
30 | #include "filenames.h" | |
31 | #include "listing.h" | |
32 | #include "sb.h" | |
33 | #include "macro.h" | |
34 | ||
35 | const char comment_chars[] = ";"; | |
36 | /* Note that input_file.c hand checks for '#' at the beginning of the | |
37 | first line of the input file. This is because the compiler outputs | |
38 | #NO_APP at the beginning of its output. */ | |
39 | const char line_comment_chars[] = "#"; | |
cbf1fdc9 DD |
40 | /* Use something that isn't going to be needed by any expressions or |
41 | other syntax. */ | |
42 | const char line_separator_chars[] = "@"; | |
99c513f6 DD |
43 | |
44 | const char EXP_CHARS[] = "eE"; | |
45 | const char FLT_CHARS[] = "dD"; | |
46 | ||
4046d87a NC |
47 | /* ELF flags to set in the output file header. */ |
48 | static int elf_flags = 0; | |
49 | ||
99c513f6 DD |
50 | /*------------------------------------------------------------------*/ |
51 | ||
52 | char * rl78_lex_start; | |
53 | char * rl78_lex_end; | |
54 | ||
55 | typedef struct rl78_bytesT | |
56 | { | |
57 | char prefix[1]; | |
58 | int n_prefix; | |
59 | char base[4]; | |
60 | int n_base; | |
61 | char ops[8]; | |
62 | int n_ops; | |
63 | struct | |
64 | { | |
65 | expressionS exp; | |
66 | char offset; | |
67 | char nbits; | |
68 | char type; /* RL78REL_*. */ | |
69 | int reloc; | |
70 | fixS * fixP; | |
71 | } fixups[2]; | |
72 | int n_fixups; | |
73 | struct | |
74 | { | |
75 | char type; | |
76 | char field_pos; | |
77 | char val_ofs; | |
78 | } relax[2]; | |
79 | int n_relax; | |
80 | int link_relax; | |
81 | fixS *link_relax_fixP; | |
82 | char times_grown; | |
83 | char times_shrank; | |
84 | } rl78_bytesT; | |
85 | ||
86 | static rl78_bytesT rl78_bytes; | |
87 | ||
0c315784 DD |
88 | void |
89 | rl78_relax (int type, int pos) | |
90 | { | |
91 | rl78_bytes.relax[rl78_bytes.n_relax].type = type; | |
92 | rl78_bytes.relax[rl78_bytes.n_relax].field_pos = pos; | |
93 | rl78_bytes.relax[rl78_bytes.n_relax].val_ofs = rl78_bytes.n_base + rl78_bytes.n_ops; | |
94 | rl78_bytes.n_relax ++; | |
95 | } | |
96 | ||
9cea966c DD |
97 | void |
98 | rl78_linkrelax_addr16 (void) | |
99 | { | |
100 | rl78_bytes.link_relax |= RL78_RELAXA_ADDR16; | |
101 | } | |
102 | ||
103 | void | |
104 | rl78_linkrelax_branch (void) | |
105 | { | |
106 | rl78_bytes.link_relax |= RL78_RELAXA_BRA; | |
107 | } | |
108 | ||
99c513f6 DD |
109 | static void |
110 | rl78_fixup (expressionS exp, int offsetbits, int nbits, int type) | |
111 | { | |
112 | rl78_bytes.fixups[rl78_bytes.n_fixups].exp = exp; | |
113 | rl78_bytes.fixups[rl78_bytes.n_fixups].offset = offsetbits; | |
114 | rl78_bytes.fixups[rl78_bytes.n_fixups].nbits = nbits; | |
115 | rl78_bytes.fixups[rl78_bytes.n_fixups].type = type; | |
116 | rl78_bytes.fixups[rl78_bytes.n_fixups].reloc = exp.X_md; | |
117 | rl78_bytes.n_fixups ++; | |
118 | } | |
119 | ||
120 | #define rl78_field_fixup(exp, offset, nbits, type) \ | |
121 | rl78_fixup (exp, offset + 8 * rl78_bytes.n_prefix), nbits, type) | |
122 | ||
123 | #define rl78_op_fixup(exp, offset, nbits, type) \ | |
124 | rl78_fixup (exp, offset + 8 * (rl78_bytes.n_prefix + rl78_bytes.n_base), nbits, type) | |
125 | ||
126 | void | |
127 | rl78_prefix (int p) | |
128 | { | |
129 | rl78_bytes.prefix[0] = p; | |
130 | rl78_bytes.n_prefix = 1; | |
131 | } | |
132 | ||
133 | int | |
134 | rl78_has_prefix () | |
135 | { | |
136 | return rl78_bytes.n_prefix; | |
137 | } | |
138 | ||
139 | void | |
140 | rl78_base1 (int b1) | |
141 | { | |
142 | rl78_bytes.base[0] = b1; | |
143 | rl78_bytes.n_base = 1; | |
144 | } | |
145 | ||
146 | void | |
147 | rl78_base2 (int b1, int b2) | |
148 | { | |
149 | rl78_bytes.base[0] = b1; | |
150 | rl78_bytes.base[1] = b2; | |
151 | rl78_bytes.n_base = 2; | |
152 | } | |
153 | ||
154 | void | |
155 | rl78_base3 (int b1, int b2, int b3) | |
156 | { | |
157 | rl78_bytes.base[0] = b1; | |
158 | rl78_bytes.base[1] = b2; | |
159 | rl78_bytes.base[2] = b3; | |
160 | rl78_bytes.n_base = 3; | |
161 | } | |
162 | ||
163 | void | |
164 | rl78_base4 (int b1, int b2, int b3, int b4) | |
165 | { | |
166 | rl78_bytes.base[0] = b1; | |
167 | rl78_bytes.base[1] = b2; | |
168 | rl78_bytes.base[2] = b3; | |
169 | rl78_bytes.base[3] = b4; | |
170 | rl78_bytes.n_base = 4; | |
171 | } | |
172 | ||
173 | #define F_PRECISION 2 | |
174 | ||
175 | void | |
176 | rl78_op (expressionS exp, int nbytes, int type) | |
177 | { | |
178 | int v = 0; | |
179 | ||
180 | if ((exp.X_op == O_constant || exp.X_op == O_big) | |
181 | && type != RL78REL_PCREL) | |
182 | { | |
183 | if (exp.X_op == O_big && exp.X_add_number <= 0) | |
184 | { | |
185 | LITTLENUM_TYPE w[2]; | |
186 | char * ip = rl78_bytes.ops + rl78_bytes.n_ops; | |
187 | ||
188 | gen_to_words (w, F_PRECISION, 8); | |
189 | ip[3] = w[0] >> 8; | |
190 | ip[2] = w[0]; | |
191 | ip[1] = w[1] >> 8; | |
192 | ip[0] = w[1]; | |
193 | rl78_bytes.n_ops += 4; | |
194 | } | |
195 | else | |
196 | { | |
197 | v = exp.X_add_number; | |
198 | while (nbytes) | |
199 | { | |
200 | rl78_bytes.ops[rl78_bytes.n_ops++] =v & 0xff; | |
201 | v >>= 8; | |
202 | nbytes --; | |
203 | } | |
204 | } | |
205 | } | |
206 | else | |
207 | { | |
4107ae22 DD |
208 | if (nbytes > 2 |
209 | && exp.X_md == BFD_RELOC_RL78_CODE) | |
210 | exp.X_md = 0; | |
b3fe4307 NC |
211 | |
212 | if (nbytes == 1 | |
213 | && (exp.X_md == BFD_RELOC_RL78_LO16 | |
214 | || exp.X_md == BFD_RELOC_RL78_HI16)) | |
215 | as_bad (_("16-bit relocation used in 8-bit operand")); | |
216 | ||
217 | if (nbytes == 2 | |
218 | && exp.X_md == BFD_RELOC_RL78_HI8) | |
219 | as_bad (_("8-bit relocation used in 16-bit operand")); | |
220 | ||
99c513f6 DD |
221 | rl78_op_fixup (exp, rl78_bytes.n_ops * 8, nbytes * 8, type); |
222 | memset (rl78_bytes.ops + rl78_bytes.n_ops, 0, nbytes); | |
223 | rl78_bytes.n_ops += nbytes; | |
224 | } | |
225 | } | |
226 | ||
227 | /* This gets complicated when the field spans bytes, because fields | |
228 | are numbered from the MSB of the first byte as zero, and bits are | |
229 | stored LSB towards the LSB of the byte. Thus, a simple four-bit | |
230 | insertion of 12 at position 4 of 0x00 yields: 0x0b. A three-bit | |
231 | insertion of b'MXL at position 7 is like this: | |
232 | ||
233 | - - - - - - - - - - - - - - - - | |
234 | M X L */ | |
235 | ||
236 | void | |
237 | rl78_field (int val, int pos, int sz) | |
238 | { | |
239 | int valm; | |
240 | int bytep, bitp; | |
241 | ||
242 | if (sz > 0) | |
243 | { | |
244 | if (val < 0 || val >= (1 << sz)) | |
245 | as_bad (_("Value %d doesn't fit in unsigned %d-bit field"), val, sz); | |
246 | } | |
247 | else | |
248 | { | |
249 | sz = - sz; | |
250 | if (val < -(1 << (sz - 1)) || val >= (1 << (sz - 1))) | |
251 | as_bad (_("Value %d doesn't fit in signed %d-bit field"), val, sz); | |
252 | } | |
253 | ||
254 | /* This code points at 'M' in the above example. */ | |
255 | bytep = pos / 8; | |
256 | bitp = pos % 8; | |
257 | ||
258 | while (bitp + sz > 8) | |
259 | { | |
260 | int ssz = 8 - bitp; | |
261 | int svalm; | |
262 | ||
263 | svalm = val >> (sz - ssz); | |
264 | svalm = svalm & ((1 << ssz) - 1); | |
265 | svalm = svalm << (8 - bitp - ssz); | |
266 | gas_assert (bytep < rl78_bytes.n_base); | |
267 | rl78_bytes.base[bytep] |= svalm; | |
268 | ||
269 | bitp = 0; | |
270 | sz -= ssz; | |
271 | bytep ++; | |
272 | } | |
273 | valm = val & ((1 << sz) - 1); | |
274 | valm = valm << (8 - bitp - sz); | |
275 | gas_assert (bytep < rl78_bytes.n_base); | |
276 | rl78_bytes.base[bytep] |= valm; | |
277 | } | |
278 | ||
279 | /*------------------------------------------------------------------*/ | |
280 | ||
9cea966c DD |
281 | enum options |
282 | { | |
283 | OPTION_RELAX = OPTION_MD_BASE, | |
4046d87a | 284 | OPTION_G10, |
9cea966c DD |
285 | }; |
286 | ||
99c513f6 DD |
287 | #define RL78_SHORTOPTS "" |
288 | const char * md_shortopts = RL78_SHORTOPTS; | |
289 | ||
290 | /* Assembler options. */ | |
291 | struct option md_longopts[] = | |
292 | { | |
9cea966c | 293 | {"relax", no_argument, NULL, OPTION_RELAX}, |
4046d87a | 294 | {"mg10", no_argument, NULL, OPTION_G10}, |
99c513f6 DD |
295 | {NULL, no_argument, NULL, 0} |
296 | }; | |
297 | size_t md_longopts_size = sizeof (md_longopts); | |
298 | ||
299 | int | |
9cea966c | 300 | md_parse_option (int c, char * arg ATTRIBUTE_UNUSED) |
99c513f6 | 301 | { |
9cea966c DD |
302 | switch (c) |
303 | { | |
304 | case OPTION_RELAX: | |
305 | linkrelax = 1; | |
306 | return 1; | |
307 | ||
4046d87a NC |
308 | case OPTION_G10: |
309 | elf_flags |= E_FLAG_RL78_G10; | |
310 | return 1; | |
9cea966c | 311 | } |
99c513f6 DD |
312 | return 0; |
313 | } | |
314 | ||
315 | void | |
316 | md_show_usage (FILE * stream ATTRIBUTE_UNUSED) | |
317 | { | |
318 | } | |
319 | ||
320 | ||
321 | static void | |
322 | s_bss (int ignore ATTRIBUTE_UNUSED) | |
323 | { | |
324 | int temp; | |
325 | ||
326 | temp = get_absolute_expression (); | |
327 | subseg_set (bss_section, (subsegT) temp); | |
328 | demand_empty_rest_of_line (); | |
329 | } | |
330 | ||
331 | /* The target specific pseudo-ops which we support. */ | |
332 | const pseudo_typeS md_pseudo_table[] = | |
333 | { | |
334 | /* Our "standard" pseudos. */ | |
335 | { "double", float_cons, 'd' }, | |
336 | { "bss", s_bss, 0 }, | |
337 | { "3byte", cons, 3 }, | |
338 | { "int", cons, 4 }, | |
339 | { "word", cons, 4 }, | |
340 | ||
341 | /* End of list marker. */ | |
342 | { NULL, NULL, 0 } | |
343 | }; | |
344 | ||
345 | void | |
346 | md_begin (void) | |
347 | { | |
348 | } | |
349 | ||
350 | void | |
351 | rl78_md_end (void) | |
352 | { | |
353 | } | |
354 | ||
4046d87a NC |
355 | /* Set the ELF specific flags. */ |
356 | void | |
357 | rl78_elf_final_processing (void) | |
358 | { | |
359 | elf_elfheader (stdoutput)->e_flags |= elf_flags; | |
360 | } | |
361 | ||
99c513f6 DD |
362 | /* Write a value out to the object file, using the appropriate endianness. */ |
363 | void | |
364 | md_number_to_chars (char * buf, valueT val, int n) | |
365 | { | |
366 | number_to_chars_littleendian (buf, val, n); | |
367 | } | |
368 | ||
c9d66558 DD |
369 | static void |
370 | require_end_of_expr (char *fname) | |
371 | { | |
372 | while (* input_line_pointer == ' ' | |
373 | || * input_line_pointer == '\t') | |
374 | input_line_pointer ++; | |
375 | ||
376 | if (! * input_line_pointer | |
377 | || strchr ("\n\r,", * input_line_pointer) | |
378 | || strchr (comment_chars, * input_line_pointer) | |
379 | || strchr (line_comment_chars, * input_line_pointer) | |
380 | || strchr (line_separator_chars, * input_line_pointer)) | |
381 | return; | |
382 | ||
383 | as_bad (_("%%%s() must be outermost term in expression"), fname); | |
384 | } | |
385 | ||
99c513f6 DD |
386 | static struct |
387 | { | |
388 | char * fname; | |
389 | int reloc; | |
390 | } | |
391 | reloc_functions[] = | |
392 | { | |
4107ae22 | 393 | { "code", BFD_RELOC_RL78_CODE }, |
99c513f6 DD |
394 | { "lo16", BFD_RELOC_RL78_LO16 }, |
395 | { "hi16", BFD_RELOC_RL78_HI16 }, | |
396 | { "hi8", BFD_RELOC_RL78_HI8 }, | |
397 | { 0, 0 } | |
398 | }; | |
399 | ||
400 | void | |
401 | md_operand (expressionS * exp ATTRIBUTE_UNUSED) | |
402 | { | |
403 | int reloc = 0; | |
404 | int i; | |
405 | ||
406 | for (i = 0; reloc_functions[i].fname; i++) | |
407 | { | |
408 | int flen = strlen (reloc_functions[i].fname); | |
409 | ||
410 | if (input_line_pointer[0] == '%' | |
411 | && strncasecmp (input_line_pointer + 1, reloc_functions[i].fname, flen) == 0 | |
412 | && input_line_pointer[flen + 1] == '(') | |
413 | { | |
414 | reloc = reloc_functions[i].reloc; | |
415 | input_line_pointer += flen + 2; | |
416 | break; | |
417 | } | |
418 | } | |
419 | if (reloc == 0) | |
420 | return; | |
421 | ||
422 | expression (exp); | |
423 | if (* input_line_pointer == ')') | |
424 | input_line_pointer ++; | |
425 | ||
426 | exp->X_md = reloc; | |
c9d66558 DD |
427 | |
428 | require_end_of_expr (reloc_functions[i].fname); | |
99c513f6 DD |
429 | } |
430 | ||
431 | void | |
432 | rl78_frag_init (fragS * fragP) | |
433 | { | |
9cea966c DD |
434 | if (rl78_bytes.n_relax || rl78_bytes.link_relax) |
435 | { | |
436 | fragP->tc_frag_data = malloc (sizeof (rl78_bytesT)); | |
437 | memcpy (fragP->tc_frag_data, & rl78_bytes, sizeof (rl78_bytesT)); | |
438 | } | |
439 | else | |
440 | fragP->tc_frag_data = 0; | |
441 | } | |
442 | ||
443 | /* When relaxing, we need to output a reloc for any .align directive | |
444 | so that we can retain this alignment as we adjust opcode sizes. */ | |
445 | void | |
446 | rl78_handle_align (fragS * frag) | |
447 | { | |
448 | if (linkrelax | |
449 | && (frag->fr_type == rs_align | |
450 | || frag->fr_type == rs_align_code) | |
451 | && frag->fr_address + frag->fr_fix > 0 | |
452 | && frag->fr_offset > 0 | |
453 | && now_seg != bss_section) | |
454 | { | |
455 | fix_new (frag, frag->fr_fix, 0, | |
456 | &abs_symbol, RL78_RELAXA_ALIGN + frag->fr_offset, | |
457 | 0, BFD_RELOC_RL78_RELAX); | |
458 | /* For the purposes of relaxation, this relocation is attached | |
459 | to the byte *after* the alignment - i.e. the byte that must | |
460 | remain aligned. */ | |
461 | fix_new (frag->fr_next, 0, 0, | |
462 | &abs_symbol, RL78_RELAXA_ELIGN + frag->fr_offset, | |
463 | 0, BFD_RELOC_RL78_RELAX); | |
464 | } | |
99c513f6 DD |
465 | } |
466 | ||
467 | char * | |
468 | md_atof (int type, char * litP, int * sizeP) | |
469 | { | |
470 | return ieee_md_atof (type, litP, sizeP, target_big_endian); | |
471 | } | |
472 | ||
473 | symbolS * | |
474 | md_undefined_symbol (char * name ATTRIBUTE_UNUSED) | |
475 | { | |
476 | return NULL; | |
477 | } | |
478 | ||
479 | #define APPEND(B, N_B) \ | |
480 | if (rl78_bytes.N_B) \ | |
481 | { \ | |
482 | memcpy (bytes + idx, rl78_bytes.B, rl78_bytes.N_B); \ | |
483 | idx += rl78_bytes.N_B; \ | |
484 | } | |
485 | ||
486 | ||
487 | void | |
488 | md_assemble (char * str) | |
489 | { | |
490 | char * bytes; | |
491 | fragS * frag_then = frag_now; | |
492 | int idx = 0; | |
493 | int i; | |
494 | int rel; | |
495 | expressionS *exp; | |
496 | ||
497 | /*printf("\033[32mASM: %s\033[0m\n", str);*/ | |
498 | ||
499 | dwarf2_emit_insn (0); | |
500 | ||
501 | memset (& rl78_bytes, 0, sizeof (rl78_bytes)); | |
502 | ||
503 | rl78_lex_init (str, str + strlen (str)); | |
504 | ||
505 | rl78_parse (); | |
506 | ||
9cea966c | 507 | /* This simplifies the relaxation code. */ |
0c315784 | 508 | if (rl78_bytes.n_relax || rl78_bytes.link_relax) |
9cea966c DD |
509 | { |
510 | int olen = rl78_bytes.n_prefix + rl78_bytes.n_base + rl78_bytes.n_ops; | |
511 | /* We do it this way because we want the frag to have the | |
0c315784 DD |
512 | rl78_bytes in it, which we initialize above. The extra bytes |
513 | are for relaxing. */ | |
514 | bytes = frag_more (olen + 3); | |
9cea966c DD |
515 | frag_then = frag_now; |
516 | frag_variant (rs_machine_dependent, | |
517 | olen /* max_chars */, | |
518 | 0 /* var */, | |
519 | olen /* subtype */, | |
520 | 0 /* symbol */, | |
521 | 0 /* offset */, | |
522 | 0 /* opcode */); | |
523 | frag_then->fr_opcode = bytes; | |
524 | frag_then->fr_fix = olen + (bytes - frag_then->fr_literal); | |
525 | frag_then->fr_subtype = olen; | |
526 | frag_then->fr_var = 0; | |
527 | } | |
528 | else | |
529 | { | |
530 | bytes = frag_more (rl78_bytes.n_prefix + rl78_bytes.n_base + rl78_bytes.n_ops); | |
531 | frag_then = frag_now; | |
532 | } | |
99c513f6 DD |
533 | |
534 | APPEND (prefix, n_prefix); | |
535 | APPEND (base, n_base); | |
536 | APPEND (ops, n_ops); | |
537 | ||
9cea966c DD |
538 | if (rl78_bytes.link_relax) |
539 | { | |
540 | fixS * f; | |
541 | ||
542 | f = fix_new (frag_then, | |
543 | (char *) bytes - frag_then->fr_literal, | |
544 | 0, | |
545 | abs_section_sym, | |
546 | rl78_bytes.link_relax | rl78_bytes.n_fixups, | |
547 | 0, | |
548 | BFD_RELOC_RL78_RELAX); | |
549 | frag_then->tc_frag_data->link_relax_fixP = f; | |
550 | } | |
551 | ||
99c513f6 DD |
552 | for (i = 0; i < rl78_bytes.n_fixups; i ++) |
553 | { | |
554 | /* index: [nbytes][type] */ | |
555 | static int reloc_map[5][4] = | |
556 | { | |
557 | { 0, 0 }, | |
558 | { BFD_RELOC_8, BFD_RELOC_8_PCREL }, | |
559 | { BFD_RELOC_16, BFD_RELOC_16_PCREL }, | |
560 | { BFD_RELOC_24, BFD_RELOC_24_PCREL }, | |
561 | { BFD_RELOC_32, BFD_RELOC_32_PCREL }, | |
562 | }; | |
563 | fixS * f; | |
564 | ||
565 | idx = rl78_bytes.fixups[i].offset / 8; | |
566 | rel = reloc_map [rl78_bytes.fixups[i].nbits / 8][(int) rl78_bytes.fixups[i].type]; | |
567 | ||
568 | if (rl78_bytes.fixups[i].reloc) | |
569 | rel = rl78_bytes.fixups[i].reloc; | |
570 | ||
571 | if (frag_then->tc_frag_data) | |
572 | exp = & frag_then->tc_frag_data->fixups[i].exp; | |
573 | else | |
574 | exp = & rl78_bytes.fixups[i].exp; | |
575 | ||
576 | f = fix_new_exp (frag_then, | |
577 | (char *) bytes + idx - frag_then->fr_literal, | |
578 | rl78_bytes.fixups[i].nbits / 8, | |
579 | exp, | |
580 | rl78_bytes.fixups[i].type == RL78REL_PCREL ? 1 : 0, | |
581 | rel); | |
582 | if (frag_then->tc_frag_data) | |
583 | frag_then->tc_frag_data->fixups[i].fixP = f; | |
584 | } | |
585 | } | |
586 | ||
587 | void | |
588 | rl78_cons_fix_new (fragS * frag, | |
589 | int where, | |
590 | int size, | |
591 | expressionS * exp) | |
592 | { | |
593 | bfd_reloc_code_real_type type; | |
c9d66558 | 594 | fixS *fixP; |
99c513f6 DD |
595 | |
596 | switch (size) | |
597 | { | |
598 | case 1: | |
599 | type = BFD_RELOC_8; | |
600 | break; | |
601 | case 2: | |
602 | type = BFD_RELOC_16; | |
603 | break; | |
604 | case 3: | |
605 | type = BFD_RELOC_24; | |
606 | break; | |
607 | case 4: | |
608 | type = BFD_RELOC_32; | |
609 | break; | |
610 | default: | |
611 | as_bad (_("unsupported constant size %d\n"), size); | |
612 | return; | |
613 | } | |
614 | ||
392ca752 DD |
615 | switch (exp->X_md) |
616 | { | |
4107ae22 DD |
617 | case BFD_RELOC_RL78_CODE: |
618 | if (size == 2) | |
619 | type = exp->X_md; | |
620 | break; | |
392ca752 DD |
621 | case BFD_RELOC_RL78_LO16: |
622 | case BFD_RELOC_RL78_HI16: | |
623 | if (size != 2) | |
624 | as_bad (_("%%hi16/%%lo16 only applies to .short or .hword")); | |
625 | type = exp->X_md; | |
626 | break; | |
627 | case BFD_RELOC_RL78_HI8: | |
628 | if (size != 1) | |
629 | as_bad (_("%%hi8 only applies to .byte")); | |
630 | type = exp->X_md; | |
631 | break; | |
632 | default: | |
633 | break; | |
634 | } | |
635 | ||
99c513f6 DD |
636 | if (exp->X_op == O_subtract && exp->X_op_symbol) |
637 | { | |
638 | if (size != 4 && size != 2 && size != 1) | |
639 | as_bad (_("difference of two symbols only supported with .long, .short, or .byte")); | |
640 | else | |
641 | type = BFD_RELOC_RL78_DIFF; | |
642 | } | |
643 | ||
c9d66558 DD |
644 | fixP = fix_new_exp (frag, where, (int) size, exp, 0, type); |
645 | switch (exp->X_md) | |
646 | { | |
647 | /* These are intended to have values larger than the container, | |
648 | since the backend puts only the portion we need in it. | |
649 | However, we don't have a backend-specific reloc for them as | |
650 | they're handled with complex relocations. */ | |
651 | case BFD_RELOC_RL78_LO16: | |
652 | case BFD_RELOC_RL78_HI16: | |
653 | case BFD_RELOC_RL78_HI8: | |
654 | fixP->fx_no_overflow = 1; | |
655 | break; | |
656 | default: | |
657 | break; | |
658 | } | |
99c513f6 DD |
659 | } |
660 | ||
0c315784 DD |
661 | \f |
662 | /*----------------------------------------------------------------------*/ | |
663 | /* To recap: we estimate everything based on md_estimate_size, then | |
664 | adjust based on rl78_relax_frag. When it all settles, we call | |
665 | md_convert frag to update the bytes. The relaxation types and | |
666 | relocations are in fragP->tc_frag_data, which is a copy of that | |
667 | rl78_bytes. | |
668 | ||
669 | Our scheme is as follows: fr_fix has the size of the smallest | |
670 | opcode (like BRA.S). We store the number of total bytes we need in | |
671 | fr_subtype. When we're done relaxing, we use fr_subtype and the | |
672 | existing opcode bytes to figure out what actual opcode we need to | |
673 | put in there. If the fixup isn't resolvable now, we use the | |
674 | maximal size. */ | |
675 | ||
676 | #define TRACE_RELAX 0 | |
677 | #define tprintf if (TRACE_RELAX) printf | |
678 | ||
679 | ||
680 | typedef enum | |
681 | { | |
682 | OT_other, | |
683 | OT_bt, | |
684 | OT_bt_sfr, | |
685 | OT_bt_es, | |
686 | OT_bc, | |
687 | OT_bh | |
688 | } op_type_T; | |
689 | ||
690 | /* We're looking for these types of relaxations: | |
691 | ||
692 | BT 00110001 sbit0cc1 addr---- (cc is 10 (BF) or 01 (BT)) | |
693 | B~T 00110001 sbit0cc1 00000011 11101110 pcrel16- -------- (BR $!pcrel20) | |
694 | ||
695 | BT sfr 00110001 sbit0cc0 sfr----- addr---- | |
696 | BT ES: 00010001 00101110 sbit0cc1 addr---- | |
697 | ||
698 | BC 110111cc addr---- | |
699 | B~C 110111cc 00000011 11101110 pcrel16- -------- (BR $!pcrel20) | |
700 | ||
701 | BH 01100001 110c0011 00000011 11101110 pcrel16- -------- (BR $!pcrel20) | |
702 | B~H 01100001 110c0011 00000011 11101110 pcrel16- -------- (BR $!pcrel20) | |
703 | */ | |
704 | ||
705 | /* Given the opcode bytes at OP, figure out which opcode it is and | |
706 | return the type of opcode. We use this to re-encode the opcode as | |
707 | a different size later. */ | |
708 | ||
709 | static op_type_T | |
710 | rl78_opcode_type (char * op) | |
711 | { | |
712 | if (op[0] == 0x31 | |
713 | && ((op[1] & 0x0f) == 0x05 | |
714 | || (op[1] & 0x0f) == 0x03)) | |
715 | return OT_bt; | |
716 | ||
717 | if (op[0] == 0x31 | |
718 | && ((op[1] & 0x0f) == 0x04 | |
719 | || (op[1] & 0x0f) == 0x02)) | |
720 | return OT_bt_sfr; | |
721 | ||
722 | if (op[0] == 0x11 | |
723 | && op[1] == 0x31 | |
724 | && ((op[2] & 0x0f) == 0x05 | |
725 | || (op[2] & 0x0f) == 0x03)) | |
726 | return OT_bt_es; | |
727 | ||
728 | if ((op[0] & 0xfc) == 0xdc) | |
729 | return OT_bc; | |
730 | ||
731 | if (op[0] == 0x61 | |
732 | && (op[1] & 0xef) == 0xc3) | |
733 | return OT_bh; | |
734 | ||
735 | return OT_other; | |
736 | } | |
737 | ||
738 | /* Returns zero if *addrP has the target address. Else returns nonzero | |
739 | if we cannot compute the target address yet. */ | |
740 | ||
741 | static int | |
742 | rl78_frag_fix_value (fragS * fragP, | |
743 | segT segment, | |
744 | int which, | |
745 | addressT * addrP, | |
746 | int need_diff, | |
747 | addressT * sym_addr) | |
748 | { | |
749 | addressT addr = 0; | |
750 | rl78_bytesT * b = fragP->tc_frag_data; | |
751 | expressionS * exp = & b->fixups[which].exp; | |
752 | ||
753 | if (need_diff && exp->X_op != O_subtract) | |
754 | return 1; | |
755 | ||
756 | if (exp->X_add_symbol) | |
757 | { | |
758 | if (S_FORCE_RELOC (exp->X_add_symbol, 1)) | |
759 | return 1; | |
760 | if (S_GET_SEGMENT (exp->X_add_symbol) != segment) | |
761 | return 1; | |
762 | addr += S_GET_VALUE (exp->X_add_symbol); | |
763 | } | |
764 | ||
765 | if (exp->X_op_symbol) | |
766 | { | |
767 | if (exp->X_op != O_subtract) | |
768 | return 1; | |
769 | if (S_FORCE_RELOC (exp->X_op_symbol, 1)) | |
770 | return 1; | |
771 | if (S_GET_SEGMENT (exp->X_op_symbol) != segment) | |
772 | return 1; | |
773 | addr -= S_GET_VALUE (exp->X_op_symbol); | |
774 | } | |
775 | if (sym_addr) | |
776 | * sym_addr = addr; | |
777 | addr += exp->X_add_number; | |
778 | * addrP = addr; | |
779 | return 0; | |
780 | } | |
781 | ||
782 | /* Estimate how big the opcode is after this relax pass. The return | |
783 | value is the difference between fr_fix and the actual size. We | |
784 | compute the total size in rl78_relax_frag and store it in fr_subtype, | |
785 | sowe only need to subtract fx_fix and return it. */ | |
786 | ||
99c513f6 DD |
787 | int |
788 | md_estimate_size_before_relax (fragS * fragP ATTRIBUTE_UNUSED, segT segment ATTRIBUTE_UNUSED) | |
789 | { | |
0c315784 DD |
790 | int opfixsize; |
791 | int delta; | |
792 | ||
793 | /* This is the size of the opcode that's accounted for in fr_fix. */ | |
794 | opfixsize = fragP->fr_fix - (fragP->fr_opcode - fragP->fr_literal); | |
795 | /* This is the size of the opcode that isn't. */ | |
796 | delta = (fragP->fr_subtype - opfixsize); | |
797 | ||
798 | tprintf (" -> opfixsize %d delta %d\n", opfixsize, delta); | |
799 | return delta; | |
800 | } | |
801 | ||
802 | /* Given the new addresses for this relax pass, figure out how big | |
803 | each opcode must be. We store the total number of bytes needed in | |
804 | fr_subtype. The return value is the difference between the size | |
805 | after the last pass and the size after this pass, so we use the old | |
806 | fr_subtype to calculate the difference. */ | |
807 | ||
808 | int | |
809 | rl78_relax_frag (segT segment ATTRIBUTE_UNUSED, fragS * fragP, long stretch) | |
810 | { | |
811 | addressT addr0, sym_addr; | |
812 | addressT mypc; | |
813 | int disp; | |
814 | int oldsize = fragP->fr_subtype; | |
815 | int newsize = oldsize; | |
816 | op_type_T optype; | |
817 | int ri; | |
818 | ||
819 | mypc = fragP->fr_address + (fragP->fr_opcode - fragP->fr_literal); | |
820 | ||
821 | /* If we ever get more than one reloc per opcode, this is the one | |
822 | we're relaxing. */ | |
823 | ri = 0; | |
824 | ||
825 | optype = rl78_opcode_type (fragP->fr_opcode); | |
826 | /* Try to get the target address. */ | |
827 | if (rl78_frag_fix_value (fragP, segment, ri, & addr0, | |
828 | fragP->tc_frag_data->relax[ri].type != RL78_RELAX_BRANCH, | |
829 | & sym_addr)) | |
830 | { | |
831 | /* If we don't, we must use the maximum size for the linker. */ | |
832 | switch (fragP->tc_frag_data->relax[ri].type) | |
833 | { | |
834 | case RL78_RELAX_BRANCH: | |
835 | switch (optype) | |
836 | { | |
837 | case OT_bt: | |
838 | newsize = 6; | |
839 | break; | |
840 | case OT_bt_sfr: | |
841 | case OT_bt_es: | |
842 | newsize = 7; | |
843 | break; | |
844 | case OT_bc: | |
845 | newsize = 5; | |
846 | break; | |
847 | case OT_bh: | |
848 | newsize = 6; | |
849 | break; | |
850 | case OT_other: | |
851 | newsize = oldsize; | |
852 | break; | |
853 | } | |
854 | break; | |
855 | ||
856 | } | |
857 | fragP->fr_subtype = newsize; | |
858 | tprintf (" -> new %d old %d delta %d (external)\n", newsize, oldsize, newsize-oldsize); | |
859 | return newsize - oldsize; | |
860 | } | |
861 | ||
862 | if (sym_addr > mypc) | |
863 | addr0 += stretch; | |
864 | ||
865 | switch (fragP->tc_frag_data->relax[ri].type) | |
866 | { | |
867 | case RL78_RELAX_BRANCH: | |
868 | disp = (int) addr0 - (int) mypc; | |
869 | ||
870 | switch (optype) | |
871 | { | |
872 | case OT_bt: | |
873 | if (disp >= -128 && (disp - (oldsize-2)) <= 127) | |
874 | newsize = 3; | |
875 | else | |
876 | newsize = 6; | |
877 | break; | |
878 | case OT_bt_sfr: | |
879 | case OT_bt_es: | |
880 | if (disp >= -128 && (disp - (oldsize-3)) <= 127) | |
881 | newsize = 4; | |
882 | else | |
883 | newsize = 7; | |
884 | break; | |
885 | case OT_bc: | |
886 | if (disp >= -128 && (disp - (oldsize-1)) <= 127) | |
887 | newsize = 2; | |
888 | else | |
889 | newsize = 5; | |
890 | break; | |
891 | case OT_bh: | |
892 | if (disp >= -128 && (disp - (oldsize-2)) <= 127) | |
893 | newsize = 3; | |
894 | else | |
895 | newsize = 6; | |
896 | break; | |
897 | case OT_other: | |
898 | newsize = oldsize; | |
899 | break; | |
900 | } | |
901 | break; | |
902 | } | |
903 | ||
904 | /* This prevents infinite loops in align-heavy sources. */ | |
905 | if (newsize < oldsize) | |
906 | { | |
907 | if (fragP->tc_frag_data->times_shrank > 10 | |
908 | && fragP->tc_frag_data->times_grown > 10) | |
909 | newsize = oldsize; | |
910 | if (fragP->tc_frag_data->times_shrank < 20) | |
911 | fragP->tc_frag_data->times_shrank ++; | |
912 | } | |
913 | else if (newsize > oldsize) | |
914 | { | |
915 | if (fragP->tc_frag_data->times_grown < 20) | |
916 | fragP->tc_frag_data->times_grown ++; | |
917 | } | |
918 | ||
919 | fragP->fr_subtype = newsize; | |
920 | tprintf (" -> new %d old %d delta %d\n", newsize, oldsize, newsize-oldsize); | |
921 | return newsize - oldsize; | |
922 | } | |
923 | ||
924 | /* This lets us test for the opcode type and the desired size in a | |
925 | switch statement. */ | |
926 | #define OPCODE(type,size) ((type) * 16 + (size)) | |
927 | ||
928 | /* Given the opcode stored in fr_opcode and the number of bytes we | |
929 | think we need, encode a new opcode. We stored a pointer to the | |
930 | fixup for this opcode in the tc_frag_data structure. If we can do | |
931 | the fixup here, we change the relocation type to "none" (we test | |
932 | for that in tc_gen_reloc) else we change it to the right type for | |
933 | the new (biggest) opcode. */ | |
934 | ||
935 | void | |
936 | md_convert_frag (bfd * abfd ATTRIBUTE_UNUSED, | |
937 | segT segment ATTRIBUTE_UNUSED, | |
938 | fragS * fragP ATTRIBUTE_UNUSED) | |
939 | { | |
940 | rl78_bytesT * rl78b = fragP->tc_frag_data; | |
941 | addressT addr0, mypc; | |
942 | int disp; | |
943 | int reloc_type, reloc_adjust; | |
944 | char * op = fragP->fr_opcode; | |
945 | int keep_reloc = 0; | |
946 | int ri; | |
947 | int fi = (rl78b->n_fixups > 1) ? 1 : 0; | |
948 | fixS * fix = rl78b->fixups[fi].fixP; | |
949 | ||
950 | /* If we ever get more than one reloc per opcode, this is the one | |
951 | we're relaxing. */ | |
952 | ri = 0; | |
953 | ||
954 | /* We used a new frag for this opcode, so the opcode address should | |
955 | be the frag address. */ | |
956 | mypc = fragP->fr_address + (fragP->fr_opcode - fragP->fr_literal); | |
957 | tprintf("\033[32mmypc: 0x%x\033[0m\n", (int)mypc); | |
958 | ||
959 | /* Try to get the target address. If we fail here, we just use the | |
960 | largest format. */ | |
961 | if (rl78_frag_fix_value (fragP, segment, 0, & addr0, | |
962 | fragP->tc_frag_data->relax[ri].type != RL78_RELAX_BRANCH, 0)) | |
963 | { | |
964 | /* We don't know the target address. */ | |
965 | keep_reloc = 1; | |
966 | addr0 = 0; | |
967 | disp = 0; | |
968 | tprintf ("unknown addr ? - %x = ?\n", (int)mypc); | |
969 | } | |
970 | else | |
971 | { | |
972 | /* We know the target address, and it's in addr0. */ | |
973 | disp = (int) addr0 - (int) mypc; | |
974 | tprintf ("known addr %x - %x = %d\n", (int)addr0, (int)mypc, disp); | |
975 | } | |
976 | ||
977 | if (linkrelax) | |
978 | keep_reloc = 1; | |
979 | ||
980 | reloc_type = BFD_RELOC_NONE; | |
981 | reloc_adjust = 0; | |
982 | ||
983 | switch (fragP->tc_frag_data->relax[ri].type) | |
984 | { | |
985 | case RL78_RELAX_BRANCH: | |
986 | switch (OPCODE (rl78_opcode_type (fragP->fr_opcode), fragP->fr_subtype)) | |
987 | { | |
988 | ||
989 | case OPCODE (OT_bt, 3): /* BT A,$ - no change. */ | |
990 | disp -= 3; | |
991 | op[2] = disp; | |
992 | break; | |
993 | ||
994 | case OPCODE (OT_bt, 6): /* BT A,$ - long version. */ | |
995 | disp -= 3; | |
996 | op[1] ^= 0x06; /* toggle conditional. */ | |
997 | op[2] = 3; /* displacement over long branch. */ | |
998 | disp -= 3; | |
999 | op[3] = 0xEE; /* BR $!addr20 */ | |
1000 | op[4] = disp & 0xff; | |
1001 | op[5] = disp >> 8; | |
1002 | reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE; | |
1003 | reloc_adjust = 2; | |
1004 | break; | |
1005 | ||
1006 | case OPCODE (OT_bt_sfr, 4): /* BT PSW,$ - no change. */ | |
1007 | disp -= 4; | |
1008 | op[3] = disp; | |
1009 | break; | |
1010 | ||
1011 | case OPCODE (OT_bt_sfr, 7): /* BT PSW,$ - long version. */ | |
1012 | disp -= 4; | |
1013 | op[1] ^= 0x06; /* toggle conditional. */ | |
1014 | op[3] = 3; /* displacement over long branch. */ | |
1015 | disp -= 3; | |
1016 | op[4] = 0xEE; /* BR $!addr20 */ | |
1017 | op[5] = disp & 0xff; | |
1018 | op[6] = disp >> 8; | |
1019 | reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE; | |
1020 | reloc_adjust = 2; | |
1021 | break; | |
1022 | ||
1023 | case OPCODE (OT_bt_es, 4): /* BT ES:[HL],$ - no change. */ | |
1024 | disp -= 4; | |
1025 | op[3] = disp; | |
1026 | break; | |
1027 | ||
1028 | case OPCODE (OT_bt_es, 7): /* BT PSW,$ - long version. */ | |
1029 | disp -= 4; | |
1030 | op[2] ^= 0x06; /* toggle conditional. */ | |
1031 | op[3] = 3; /* displacement over long branch. */ | |
1032 | disp -= 3; | |
1033 | op[4] = 0xEE; /* BR $!addr20 */ | |
1034 | op[5] = disp & 0xff; | |
1035 | op[6] = disp >> 8; | |
1036 | reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE; | |
1037 | reloc_adjust = 2; | |
1038 | break; | |
1039 | ||
1040 | case OPCODE (OT_bc, 2): /* BC $ - no change. */ | |
1041 | disp -= 2; | |
1042 | op[1] = disp; | |
1043 | break; | |
1044 | ||
1045 | case OPCODE (OT_bc, 5): /* BC $ - long version. */ | |
1046 | disp -= 2; | |
1047 | op[0] ^= 0x02; /* toggle conditional. */ | |
1048 | op[1] = 3; | |
1049 | disp -= 3; | |
1050 | op[2] = 0xEE; /* BR $!addr20 */ | |
1051 | op[3] = disp & 0xff; | |
1052 | op[4] = disp >> 8; | |
1053 | reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE; | |
1054 | reloc_adjust = 2; | |
1055 | break; | |
1056 | ||
1057 | case OPCODE (OT_bh, 3): /* BH $ - no change. */ | |
1058 | disp -= 3; | |
1059 | op[2] = disp; | |
1060 | break; | |
1061 | ||
1062 | case OPCODE (OT_bh, 6): /* BC $ - long version. */ | |
1063 | disp -= 3; | |
1064 | op[1] ^= 0x10; /* toggle conditional. */ | |
1065 | op[2] = 3; | |
1066 | disp -= 3; | |
1067 | op[3] = 0xEE; /* BR $!addr20 */ | |
1068 | op[4] = disp & 0xff; | |
1069 | op[5] = disp >> 8; | |
1070 | reloc_type = keep_reloc ? BFD_RELOC_16_PCREL : BFD_RELOC_NONE; | |
1071 | reloc_adjust = 2; | |
1072 | break; | |
1073 | ||
1074 | default: | |
1075 | fprintf(stderr, "Missed case %d %d at 0x%lx\n", | |
1076 | rl78_opcode_type (fragP->fr_opcode), fragP->fr_subtype, mypc); | |
1077 | abort (); | |
1078 | ||
1079 | } | |
1080 | break; | |
1081 | ||
1082 | default: | |
1083 | if (rl78b->n_fixups) | |
1084 | { | |
1085 | reloc_type = fix->fx_r_type; | |
1086 | reloc_adjust = 0; | |
1087 | } | |
1088 | break; | |
1089 | } | |
1090 | ||
1091 | if (rl78b->n_fixups) | |
1092 | { | |
1093 | ||
1094 | fix->fx_r_type = reloc_type; | |
1095 | fix->fx_where += reloc_adjust; | |
1096 | switch (reloc_type) | |
1097 | { | |
1098 | case BFD_RELOC_NONE: | |
1099 | fix->fx_size = 0; | |
1100 | break; | |
1101 | case BFD_RELOC_8: | |
1102 | fix->fx_size = 1; | |
1103 | break; | |
1104 | case BFD_RELOC_16_PCREL: | |
1105 | fix->fx_size = 2; | |
1106 | break; | |
1107 | } | |
1108 | } | |
1109 | ||
1110 | fragP->fr_fix = fragP->fr_subtype + (fragP->fr_opcode - fragP->fr_literal); | |
1111 | tprintf ("fragP->fr_fix now %ld (%d + (%p - %p)\n", (long) fragP->fr_fix, | |
1112 | fragP->fr_subtype, fragP->fr_opcode, fragP->fr_literal); | |
1113 | fragP->fr_var = 0; | |
1114 | ||
1115 | tprintf ("compare 0x%lx vs 0x%lx - 0x%lx = 0x%lx (%p)\n", | |
1116 | (long)fragP->fr_fix, | |
1117 | (long)fragP->fr_next->fr_address, (long)fragP->fr_address, | |
1118 | (long)(fragP->fr_next->fr_address - fragP->fr_address), | |
1119 | fragP->fr_next); | |
1120 | ||
1121 | if (fragP->fr_next != NULL | |
1122 | && ((offsetT) (fragP->fr_next->fr_address - fragP->fr_address) | |
1123 | != fragP->fr_fix)) | |
1124 | as_bad (_("bad frag at %p : fix %ld addr %ld %ld \n"), fragP, | |
1125 | (long) fragP->fr_fix, | |
1126 | (long) fragP->fr_address, (long) fragP->fr_next->fr_address); | |
99c513f6 | 1127 | } |
9cea966c | 1128 | |
0c315784 DD |
1129 | /* End of relaxation code. |
1130 | ----------------------------------------------------------------------*/ | |
1131 | \f | |
1132 | ||
99c513f6 DD |
1133 | arelent ** |
1134 | tc_gen_reloc (asection * seg ATTRIBUTE_UNUSED, fixS * fixp) | |
1135 | { | |
1136 | static arelent * reloc[8]; | |
1137 | int rp; | |
99c513f6 DD |
1138 | |
1139 | if (fixp->fx_r_type == BFD_RELOC_NONE) | |
1140 | { | |
1141 | reloc[0] = NULL; | |
1142 | return reloc; | |
1143 | } | |
1144 | ||
1145 | if (fixp->fx_subsy | |
1146 | && S_GET_SEGMENT (fixp->fx_subsy) == absolute_section) | |
1147 | { | |
1148 | fixp->fx_offset -= S_GET_VALUE (fixp->fx_subsy); | |
1149 | fixp->fx_subsy = NULL; | |
1150 | } | |
1151 | ||
1152 | reloc[0] = (arelent *) xmalloc (sizeof (arelent)); | |
1153 | reloc[0]->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *)); | |
1154 | * reloc[0]->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy); | |
1155 | reloc[0]->address = fixp->fx_frag->fr_address + fixp->fx_where; | |
1156 | reloc[0]->addend = fixp->fx_offset; | |
1157 | ||
1158 | if (fixp->fx_r_type == BFD_RELOC_RL78_32_OP | |
1159 | && fixp->fx_subsy) | |
1160 | { | |
1161 | fixp->fx_r_type = BFD_RELOC_RL78_DIFF; | |
99c513f6 DD |
1162 | } |
1163 | ||
1164 | #define OPX(REL,SYM,ADD) \ | |
1165 | reloc[rp] = (arelent *) xmalloc (sizeof (arelent)); \ | |
1166 | reloc[rp]->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *)); \ | |
1167 | reloc[rp]->howto = bfd_reloc_type_lookup (stdoutput, REL); \ | |
1168 | reloc[rp]->addend = ADD; \ | |
1169 | * reloc[rp]->sym_ptr_ptr = SYM; \ | |
1170 | reloc[rp]->address = fixp->fx_frag->fr_address + fixp->fx_where; \ | |
1171 | reloc[++rp] = NULL | |
1172 | #define OPSYM(SYM) OPX(BFD_RELOC_RL78_SYM, SYM, 0) | |
1173 | #define OPIMM(IMM) OPX(BFD_RELOC_RL78_SYM, abs_symbol.bsym, IMM) | |
1174 | #define OP(OP) OPX(BFD_RELOC_RL78_##OP, *reloc[0]->sym_ptr_ptr, 0) | |
1175 | #define SYM0() reloc[0]->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_RL78_SYM) | |
1176 | ||
1177 | rp = 1; | |
1178 | ||
1179 | /* Certain BFD relocations cannot be translated directly into | |
1180 | a single (non-Red Hat) RL78 relocation, but instead need | |
1181 | multiple RL78 relocations - handle them here. */ | |
1182 | switch (fixp->fx_r_type) | |
1183 | { | |
1184 | case BFD_RELOC_RL78_DIFF: | |
1185 | SYM0 (); | |
1186 | OPSYM (symbol_get_bfdsym (fixp->fx_subsy)); | |
1187 | OP(OP_SUBTRACT); | |
1188 | ||
1189 | switch (fixp->fx_size) | |
1190 | { | |
1191 | case 1: | |
1192 | OP(ABS8); | |
1193 | break; | |
1194 | case 2: | |
1195 | OP (ABS16); | |
1196 | break; | |
1197 | case 4: | |
1198 | OP (ABS32); | |
1199 | break; | |
1200 | } | |
1201 | break; | |
1202 | ||
1203 | case BFD_RELOC_RL78_NEG32: | |
1204 | SYM0 (); | |
1205 | OP (OP_NEG); | |
1206 | OP (ABS32); | |
1207 | break; | |
1208 | ||
4107ae22 | 1209 | case BFD_RELOC_RL78_CODE: |
b3fe4307 NC |
1210 | reloc[0]->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_RL78_16U); |
1211 | reloc[1] = NULL; | |
4107ae22 DD |
1212 | break; |
1213 | ||
99c513f6 DD |
1214 | case BFD_RELOC_RL78_LO16: |
1215 | SYM0 (); | |
1216 | OPIMM (0xffff); | |
1217 | OP (OP_AND); | |
1218 | OP (ABS16); | |
1219 | break; | |
1220 | ||
1221 | case BFD_RELOC_RL78_HI16: | |
1222 | SYM0 (); | |
1223 | OPIMM (16); | |
1224 | OP (OP_SHRA); | |
1225 | OP (ABS16); | |
1226 | break; | |
1227 | ||
1228 | case BFD_RELOC_RL78_HI8: | |
1229 | SYM0 (); | |
1230 | OPIMM (16); | |
1231 | OP (OP_SHRA); | |
1232 | OPIMM (0xff); | |
1233 | OP (OP_AND); | |
1234 | OP (ABS8); | |
1235 | break; | |
1236 | ||
1237 | default: | |
1238 | reloc[0]->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type); | |
1239 | reloc[1] = NULL; | |
1240 | break; | |
1241 | } | |
1242 | ||
1243 | return reloc; | |
1244 | } | |
1245 | ||
1246 | int | |
1247 | rl78_validate_fix_sub (struct fix * f) | |
1248 | { | |
1249 | /* We permit the subtraction of two symbols in a few cases. */ | |
1250 | /* mov #sym1-sym2, R3 */ | |
1251 | if (f->fx_r_type == BFD_RELOC_RL78_32_OP) | |
1252 | return 1; | |
1253 | /* .long sym1-sym2 */ | |
1254 | if (f->fx_r_type == BFD_RELOC_RL78_DIFF | |
1255 | && ! f->fx_pcrel | |
1256 | && (f->fx_size == 4 || f->fx_size == 2 || f->fx_size == 1)) | |
1257 | return 1; | |
1258 | return 0; | |
1259 | } | |
1260 | ||
1261 | long | |
1262 | md_pcrel_from_section (fixS * fixP, segT sec) | |
1263 | { | |
1264 | long rv; | |
1265 | ||
1266 | if (fixP->fx_addsy != NULL | |
1267 | && (! S_IS_DEFINED (fixP->fx_addsy) | |
1268 | || S_GET_SEGMENT (fixP->fx_addsy) != sec)) | |
1269 | /* The symbol is undefined (or is defined but not in this section). | |
1270 | Let the linker figure it out. */ | |
1271 | return 0; | |
1272 | ||
1273 | rv = fixP->fx_frag->fr_address + fixP->fx_where; | |
1274 | switch (fixP->fx_r_type) | |
1275 | { | |
1276 | case BFD_RELOC_8_PCREL: | |
1277 | rv += 1; | |
1278 | break; | |
1279 | case BFD_RELOC_16_PCREL: | |
1280 | rv += 2; | |
1281 | break; | |
1282 | default: | |
1283 | break; | |
1284 | } | |
1285 | return rv; | |
1286 | } | |
1287 | ||
1288 | void | |
1289 | md_apply_fix (struct fix * f ATTRIBUTE_UNUSED, | |
1290 | valueT * t ATTRIBUTE_UNUSED, | |
1291 | segT s ATTRIBUTE_UNUSED) | |
1292 | { | |
1293 | char * op; | |
1294 | unsigned long val; | |
1295 | ||
1296 | if (f->fx_addsy && S_FORCE_RELOC (f->fx_addsy, 1)) | |
1297 | return; | |
1298 | if (f->fx_subsy && S_FORCE_RELOC (f->fx_subsy, 1)) | |
1299 | return; | |
1300 | ||
1301 | op = f->fx_frag->fr_literal + f->fx_where; | |
1302 | val = (unsigned long) * t; | |
1303 | ||
1304 | switch (f->fx_r_type) | |
1305 | { | |
1306 | case BFD_RELOC_NONE: | |
1307 | break; | |
1308 | ||
9cea966c DD |
1309 | case BFD_RELOC_RL78_RELAX: |
1310 | f->fx_done = 1; | |
1311 | break; | |
1312 | ||
99c513f6 DD |
1313 | case BFD_RELOC_8: |
1314 | case BFD_RELOC_8_PCREL: | |
1315 | op[0] = val; | |
1316 | break; | |
1317 | ||
1318 | case BFD_RELOC_16: | |
1319 | case BFD_RELOC_16_PCREL: | |
4107ae22 | 1320 | case BFD_RELOC_RL78_CODE: |
99c513f6 DD |
1321 | op[0] = val; |
1322 | op[1] = val >> 8; | |
1323 | break; | |
1324 | ||
1325 | case BFD_RELOC_24: | |
1326 | op[0] = val; | |
1327 | op[1] = val >> 8; | |
1328 | op[2] = val >> 16; | |
1329 | break; | |
1330 | ||
1331 | case BFD_RELOC_32: | |
1332 | case BFD_RELOC_RL78_DIFF: | |
1333 | op[0] = val; | |
1334 | op[1] = val >> 8; | |
1335 | op[2] = val >> 16; | |
1336 | op[3] = val >> 24; | |
1337 | break; | |
1338 | ||
b3fe4307 NC |
1339 | case BFD_RELOC_RL78_HI8: |
1340 | val = val >> 16; | |
1341 | op[0] = val; | |
1342 | break; | |
1343 | ||
1344 | case BFD_RELOC_RL78_HI16: | |
1345 | val = val >> 16; | |
1346 | op[0] = val; | |
1347 | op[1] = val >> 8; | |
1348 | break; | |
1349 | ||
1350 | case BFD_RELOC_RL78_LO16: | |
1351 | op[0] = val; | |
1352 | op[1] = val >> 8; | |
1353 | break; | |
1354 | ||
99c513f6 DD |
1355 | default: |
1356 | as_bad (_("Unknown reloc in md_apply_fix: %s"), | |
1357 | bfd_get_reloc_code_name (f->fx_r_type)); | |
1358 | break; | |
1359 | } | |
1360 | ||
1361 | if (f->fx_addsy == NULL) | |
1362 | f->fx_done = 1; | |
1363 | } | |
1364 | ||
1365 | valueT | |
1366 | md_section_align (segT segment, valueT size) | |
1367 | { | |
1368 | int align = bfd_get_section_alignment (stdoutput, segment); | |
1369 | return ((size + (1 << align) - 1) & (-1 << align)); | |
1370 | } |