Commit | Line | Data |
---|---|---|
99c513f6 DD |
1 | /* tc-rl78.c -- Assembler for the Renesas RL78 |
2 | Copyright 2011 | |
3 | Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GAS, the GNU Assembler. | |
6 | ||
7 | GAS is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GAS is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GAS; see the file COPYING. If not, write to the Free | |
19 | Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA | |
20 | 02110-1301, USA. */ | |
21 | ||
22 | #include "as.h" | |
23 | #include "struc-symbol.h" | |
24 | #include "obstack.h" | |
25 | #include "safe-ctype.h" | |
26 | #include "dwarf2dbg.h" | |
27 | #include "libbfd.h" | |
28 | #include "elf/common.h" | |
29 | #include "elf/rl78.h" | |
30 | #include "rl78-defs.h" | |
31 | #include "filenames.h" | |
32 | #include "listing.h" | |
33 | #include "sb.h" | |
34 | #include "macro.h" | |
35 | ||
36 | const char comment_chars[] = ";"; | |
37 | /* Note that input_file.c hand checks for '#' at the beginning of the | |
38 | first line of the input file. This is because the compiler outputs | |
39 | #NO_APP at the beginning of its output. */ | |
40 | const char line_comment_chars[] = "#"; | |
cbf1fdc9 DD |
41 | /* Use something that isn't going to be needed by any expressions or |
42 | other syntax. */ | |
43 | const char line_separator_chars[] = "@"; | |
99c513f6 DD |
44 | |
45 | const char EXP_CHARS[] = "eE"; | |
46 | const char FLT_CHARS[] = "dD"; | |
47 | ||
48 | /*------------------------------------------------------------------*/ | |
49 | ||
50 | char * rl78_lex_start; | |
51 | char * rl78_lex_end; | |
52 | ||
53 | typedef struct rl78_bytesT | |
54 | { | |
55 | char prefix[1]; | |
56 | int n_prefix; | |
57 | char base[4]; | |
58 | int n_base; | |
59 | char ops[8]; | |
60 | int n_ops; | |
61 | struct | |
62 | { | |
63 | expressionS exp; | |
64 | char offset; | |
65 | char nbits; | |
66 | char type; /* RL78REL_*. */ | |
67 | int reloc; | |
68 | fixS * fixP; | |
69 | } fixups[2]; | |
70 | int n_fixups; | |
71 | struct | |
72 | { | |
73 | char type; | |
74 | char field_pos; | |
75 | char val_ofs; | |
76 | } relax[2]; | |
77 | int n_relax; | |
78 | int link_relax; | |
79 | fixS *link_relax_fixP; | |
80 | char times_grown; | |
81 | char times_shrank; | |
82 | } rl78_bytesT; | |
83 | ||
84 | static rl78_bytesT rl78_bytes; | |
85 | ||
9cea966c DD |
86 | void |
87 | rl78_linkrelax_addr16 (void) | |
88 | { | |
89 | rl78_bytes.link_relax |= RL78_RELAXA_ADDR16; | |
90 | } | |
91 | ||
92 | void | |
93 | rl78_linkrelax_branch (void) | |
94 | { | |
95 | rl78_bytes.link_relax |= RL78_RELAXA_BRA; | |
96 | } | |
97 | ||
99c513f6 DD |
98 | static void |
99 | rl78_fixup (expressionS exp, int offsetbits, int nbits, int type) | |
100 | { | |
101 | rl78_bytes.fixups[rl78_bytes.n_fixups].exp = exp; | |
102 | rl78_bytes.fixups[rl78_bytes.n_fixups].offset = offsetbits; | |
103 | rl78_bytes.fixups[rl78_bytes.n_fixups].nbits = nbits; | |
104 | rl78_bytes.fixups[rl78_bytes.n_fixups].type = type; | |
105 | rl78_bytes.fixups[rl78_bytes.n_fixups].reloc = exp.X_md; | |
106 | rl78_bytes.n_fixups ++; | |
107 | } | |
108 | ||
109 | #define rl78_field_fixup(exp, offset, nbits, type) \ | |
110 | rl78_fixup (exp, offset + 8 * rl78_bytes.n_prefix), nbits, type) | |
111 | ||
112 | #define rl78_op_fixup(exp, offset, nbits, type) \ | |
113 | rl78_fixup (exp, offset + 8 * (rl78_bytes.n_prefix + rl78_bytes.n_base), nbits, type) | |
114 | ||
115 | void | |
116 | rl78_prefix (int p) | |
117 | { | |
118 | rl78_bytes.prefix[0] = p; | |
119 | rl78_bytes.n_prefix = 1; | |
120 | } | |
121 | ||
122 | int | |
123 | rl78_has_prefix () | |
124 | { | |
125 | return rl78_bytes.n_prefix; | |
126 | } | |
127 | ||
128 | void | |
129 | rl78_base1 (int b1) | |
130 | { | |
131 | rl78_bytes.base[0] = b1; | |
132 | rl78_bytes.n_base = 1; | |
133 | } | |
134 | ||
135 | void | |
136 | rl78_base2 (int b1, int b2) | |
137 | { | |
138 | rl78_bytes.base[0] = b1; | |
139 | rl78_bytes.base[1] = b2; | |
140 | rl78_bytes.n_base = 2; | |
141 | } | |
142 | ||
143 | void | |
144 | rl78_base3 (int b1, int b2, int b3) | |
145 | { | |
146 | rl78_bytes.base[0] = b1; | |
147 | rl78_bytes.base[1] = b2; | |
148 | rl78_bytes.base[2] = b3; | |
149 | rl78_bytes.n_base = 3; | |
150 | } | |
151 | ||
152 | void | |
153 | rl78_base4 (int b1, int b2, int b3, int b4) | |
154 | { | |
155 | rl78_bytes.base[0] = b1; | |
156 | rl78_bytes.base[1] = b2; | |
157 | rl78_bytes.base[2] = b3; | |
158 | rl78_bytes.base[3] = b4; | |
159 | rl78_bytes.n_base = 4; | |
160 | } | |
161 | ||
162 | #define F_PRECISION 2 | |
163 | ||
164 | void | |
165 | rl78_op (expressionS exp, int nbytes, int type) | |
166 | { | |
167 | int v = 0; | |
168 | ||
169 | if ((exp.X_op == O_constant || exp.X_op == O_big) | |
170 | && type != RL78REL_PCREL) | |
171 | { | |
172 | if (exp.X_op == O_big && exp.X_add_number <= 0) | |
173 | { | |
174 | LITTLENUM_TYPE w[2]; | |
175 | char * ip = rl78_bytes.ops + rl78_bytes.n_ops; | |
176 | ||
177 | gen_to_words (w, F_PRECISION, 8); | |
178 | ip[3] = w[0] >> 8; | |
179 | ip[2] = w[0]; | |
180 | ip[1] = w[1] >> 8; | |
181 | ip[0] = w[1]; | |
182 | rl78_bytes.n_ops += 4; | |
183 | } | |
184 | else | |
185 | { | |
186 | v = exp.X_add_number; | |
187 | while (nbytes) | |
188 | { | |
189 | rl78_bytes.ops[rl78_bytes.n_ops++] =v & 0xff; | |
190 | v >>= 8; | |
191 | nbytes --; | |
192 | } | |
193 | } | |
194 | } | |
195 | else | |
196 | { | |
197 | rl78_op_fixup (exp, rl78_bytes.n_ops * 8, nbytes * 8, type); | |
198 | memset (rl78_bytes.ops + rl78_bytes.n_ops, 0, nbytes); | |
199 | rl78_bytes.n_ops += nbytes; | |
200 | } | |
201 | } | |
202 | ||
203 | /* This gets complicated when the field spans bytes, because fields | |
204 | are numbered from the MSB of the first byte as zero, and bits are | |
205 | stored LSB towards the LSB of the byte. Thus, a simple four-bit | |
206 | insertion of 12 at position 4 of 0x00 yields: 0x0b. A three-bit | |
207 | insertion of b'MXL at position 7 is like this: | |
208 | ||
209 | - - - - - - - - - - - - - - - - | |
210 | M X L */ | |
211 | ||
212 | void | |
213 | rl78_field (int val, int pos, int sz) | |
214 | { | |
215 | int valm; | |
216 | int bytep, bitp; | |
217 | ||
218 | if (sz > 0) | |
219 | { | |
220 | if (val < 0 || val >= (1 << sz)) | |
221 | as_bad (_("Value %d doesn't fit in unsigned %d-bit field"), val, sz); | |
222 | } | |
223 | else | |
224 | { | |
225 | sz = - sz; | |
226 | if (val < -(1 << (sz - 1)) || val >= (1 << (sz - 1))) | |
227 | as_bad (_("Value %d doesn't fit in signed %d-bit field"), val, sz); | |
228 | } | |
229 | ||
230 | /* This code points at 'M' in the above example. */ | |
231 | bytep = pos / 8; | |
232 | bitp = pos % 8; | |
233 | ||
234 | while (bitp + sz > 8) | |
235 | { | |
236 | int ssz = 8 - bitp; | |
237 | int svalm; | |
238 | ||
239 | svalm = val >> (sz - ssz); | |
240 | svalm = svalm & ((1 << ssz) - 1); | |
241 | svalm = svalm << (8 - bitp - ssz); | |
242 | gas_assert (bytep < rl78_bytes.n_base); | |
243 | rl78_bytes.base[bytep] |= svalm; | |
244 | ||
245 | bitp = 0; | |
246 | sz -= ssz; | |
247 | bytep ++; | |
248 | } | |
249 | valm = val & ((1 << sz) - 1); | |
250 | valm = valm << (8 - bitp - sz); | |
251 | gas_assert (bytep < rl78_bytes.n_base); | |
252 | rl78_bytes.base[bytep] |= valm; | |
253 | } | |
254 | ||
255 | /*------------------------------------------------------------------*/ | |
256 | ||
9cea966c DD |
257 | enum options |
258 | { | |
259 | OPTION_RELAX = OPTION_MD_BASE, | |
260 | }; | |
261 | ||
99c513f6 DD |
262 | #define RL78_SHORTOPTS "" |
263 | const char * md_shortopts = RL78_SHORTOPTS; | |
264 | ||
265 | /* Assembler options. */ | |
266 | struct option md_longopts[] = | |
267 | { | |
9cea966c | 268 | {"relax", no_argument, NULL, OPTION_RELAX}, |
99c513f6 DD |
269 | {NULL, no_argument, NULL, 0} |
270 | }; | |
271 | size_t md_longopts_size = sizeof (md_longopts); | |
272 | ||
273 | int | |
9cea966c | 274 | md_parse_option (int c, char * arg ATTRIBUTE_UNUSED) |
99c513f6 | 275 | { |
9cea966c DD |
276 | switch (c) |
277 | { | |
278 | case OPTION_RELAX: | |
279 | linkrelax = 1; | |
280 | return 1; | |
281 | ||
282 | } | |
99c513f6 DD |
283 | return 0; |
284 | } | |
285 | ||
286 | void | |
287 | md_show_usage (FILE * stream ATTRIBUTE_UNUSED) | |
288 | { | |
289 | } | |
290 | ||
291 | ||
292 | static void | |
293 | s_bss (int ignore ATTRIBUTE_UNUSED) | |
294 | { | |
295 | int temp; | |
296 | ||
297 | temp = get_absolute_expression (); | |
298 | subseg_set (bss_section, (subsegT) temp); | |
299 | demand_empty_rest_of_line (); | |
300 | } | |
301 | ||
302 | /* The target specific pseudo-ops which we support. */ | |
303 | const pseudo_typeS md_pseudo_table[] = | |
304 | { | |
305 | /* Our "standard" pseudos. */ | |
306 | { "double", float_cons, 'd' }, | |
307 | { "bss", s_bss, 0 }, | |
308 | { "3byte", cons, 3 }, | |
309 | { "int", cons, 4 }, | |
310 | { "word", cons, 4 }, | |
311 | ||
312 | /* End of list marker. */ | |
313 | { NULL, NULL, 0 } | |
314 | }; | |
315 | ||
316 | void | |
317 | md_begin (void) | |
318 | { | |
319 | } | |
320 | ||
321 | void | |
322 | rl78_md_end (void) | |
323 | { | |
324 | } | |
325 | ||
326 | /* Write a value out to the object file, using the appropriate endianness. */ | |
327 | void | |
328 | md_number_to_chars (char * buf, valueT val, int n) | |
329 | { | |
330 | number_to_chars_littleendian (buf, val, n); | |
331 | } | |
332 | ||
333 | static struct | |
334 | { | |
335 | char * fname; | |
336 | int reloc; | |
337 | } | |
338 | reloc_functions[] = | |
339 | { | |
340 | { "lo16", BFD_RELOC_RL78_LO16 }, | |
341 | { "hi16", BFD_RELOC_RL78_HI16 }, | |
342 | { "hi8", BFD_RELOC_RL78_HI8 }, | |
343 | { 0, 0 } | |
344 | }; | |
345 | ||
346 | void | |
347 | md_operand (expressionS * exp ATTRIBUTE_UNUSED) | |
348 | { | |
349 | int reloc = 0; | |
350 | int i; | |
351 | ||
352 | for (i = 0; reloc_functions[i].fname; i++) | |
353 | { | |
354 | int flen = strlen (reloc_functions[i].fname); | |
355 | ||
356 | if (input_line_pointer[0] == '%' | |
357 | && strncasecmp (input_line_pointer + 1, reloc_functions[i].fname, flen) == 0 | |
358 | && input_line_pointer[flen + 1] == '(') | |
359 | { | |
360 | reloc = reloc_functions[i].reloc; | |
361 | input_line_pointer += flen + 2; | |
362 | break; | |
363 | } | |
364 | } | |
365 | if (reloc == 0) | |
366 | return; | |
367 | ||
368 | expression (exp); | |
369 | if (* input_line_pointer == ')') | |
370 | input_line_pointer ++; | |
371 | ||
372 | exp->X_md = reloc; | |
373 | } | |
374 | ||
375 | void | |
376 | rl78_frag_init (fragS * fragP) | |
377 | { | |
9cea966c DD |
378 | if (rl78_bytes.n_relax || rl78_bytes.link_relax) |
379 | { | |
380 | fragP->tc_frag_data = malloc (sizeof (rl78_bytesT)); | |
381 | memcpy (fragP->tc_frag_data, & rl78_bytes, sizeof (rl78_bytesT)); | |
382 | } | |
383 | else | |
384 | fragP->tc_frag_data = 0; | |
385 | } | |
386 | ||
387 | /* When relaxing, we need to output a reloc for any .align directive | |
388 | so that we can retain this alignment as we adjust opcode sizes. */ | |
389 | void | |
390 | rl78_handle_align (fragS * frag) | |
391 | { | |
392 | if (linkrelax | |
393 | && (frag->fr_type == rs_align | |
394 | || frag->fr_type == rs_align_code) | |
395 | && frag->fr_address + frag->fr_fix > 0 | |
396 | && frag->fr_offset > 0 | |
397 | && now_seg != bss_section) | |
398 | { | |
399 | fix_new (frag, frag->fr_fix, 0, | |
400 | &abs_symbol, RL78_RELAXA_ALIGN + frag->fr_offset, | |
401 | 0, BFD_RELOC_RL78_RELAX); | |
402 | /* For the purposes of relaxation, this relocation is attached | |
403 | to the byte *after* the alignment - i.e. the byte that must | |
404 | remain aligned. */ | |
405 | fix_new (frag->fr_next, 0, 0, | |
406 | &abs_symbol, RL78_RELAXA_ELIGN + frag->fr_offset, | |
407 | 0, BFD_RELOC_RL78_RELAX); | |
408 | } | |
99c513f6 DD |
409 | } |
410 | ||
411 | char * | |
412 | md_atof (int type, char * litP, int * sizeP) | |
413 | { | |
414 | return ieee_md_atof (type, litP, sizeP, target_big_endian); | |
415 | } | |
416 | ||
417 | symbolS * | |
418 | md_undefined_symbol (char * name ATTRIBUTE_UNUSED) | |
419 | { | |
420 | return NULL; | |
421 | } | |
422 | ||
423 | #define APPEND(B, N_B) \ | |
424 | if (rl78_bytes.N_B) \ | |
425 | { \ | |
426 | memcpy (bytes + idx, rl78_bytes.B, rl78_bytes.N_B); \ | |
427 | idx += rl78_bytes.N_B; \ | |
428 | } | |
429 | ||
430 | ||
431 | void | |
432 | md_assemble (char * str) | |
433 | { | |
434 | char * bytes; | |
435 | fragS * frag_then = frag_now; | |
436 | int idx = 0; | |
437 | int i; | |
438 | int rel; | |
439 | expressionS *exp; | |
440 | ||
441 | /*printf("\033[32mASM: %s\033[0m\n", str);*/ | |
442 | ||
443 | dwarf2_emit_insn (0); | |
444 | ||
445 | memset (& rl78_bytes, 0, sizeof (rl78_bytes)); | |
446 | ||
447 | rl78_lex_init (str, str + strlen (str)); | |
448 | ||
449 | rl78_parse (); | |
450 | ||
9cea966c DD |
451 | /* This simplifies the relaxation code. */ |
452 | if (rl78_bytes.link_relax) | |
453 | { | |
454 | int olen = rl78_bytes.n_prefix + rl78_bytes.n_base + rl78_bytes.n_ops; | |
455 | /* We do it this way because we want the frag to have the | |
456 | rl78_bytes in it, which we initialize above. */ | |
457 | bytes = frag_more (olen); | |
458 | frag_then = frag_now; | |
459 | frag_variant (rs_machine_dependent, | |
460 | olen /* max_chars */, | |
461 | 0 /* var */, | |
462 | olen /* subtype */, | |
463 | 0 /* symbol */, | |
464 | 0 /* offset */, | |
465 | 0 /* opcode */); | |
466 | frag_then->fr_opcode = bytes; | |
467 | frag_then->fr_fix = olen + (bytes - frag_then->fr_literal); | |
468 | frag_then->fr_subtype = olen; | |
469 | frag_then->fr_var = 0; | |
470 | } | |
471 | else | |
472 | { | |
473 | bytes = frag_more (rl78_bytes.n_prefix + rl78_bytes.n_base + rl78_bytes.n_ops); | |
474 | frag_then = frag_now; | |
475 | } | |
99c513f6 DD |
476 | |
477 | APPEND (prefix, n_prefix); | |
478 | APPEND (base, n_base); | |
479 | APPEND (ops, n_ops); | |
480 | ||
9cea966c DD |
481 | if (rl78_bytes.link_relax) |
482 | { | |
483 | fixS * f; | |
484 | ||
485 | f = fix_new (frag_then, | |
486 | (char *) bytes - frag_then->fr_literal, | |
487 | 0, | |
488 | abs_section_sym, | |
489 | rl78_bytes.link_relax | rl78_bytes.n_fixups, | |
490 | 0, | |
491 | BFD_RELOC_RL78_RELAX); | |
492 | frag_then->tc_frag_data->link_relax_fixP = f; | |
493 | } | |
494 | ||
99c513f6 DD |
495 | for (i = 0; i < rl78_bytes.n_fixups; i ++) |
496 | { | |
497 | /* index: [nbytes][type] */ | |
498 | static int reloc_map[5][4] = | |
499 | { | |
500 | { 0, 0 }, | |
501 | { BFD_RELOC_8, BFD_RELOC_8_PCREL }, | |
502 | { BFD_RELOC_16, BFD_RELOC_16_PCREL }, | |
503 | { BFD_RELOC_24, BFD_RELOC_24_PCREL }, | |
504 | { BFD_RELOC_32, BFD_RELOC_32_PCREL }, | |
505 | }; | |
506 | fixS * f; | |
507 | ||
508 | idx = rl78_bytes.fixups[i].offset / 8; | |
509 | rel = reloc_map [rl78_bytes.fixups[i].nbits / 8][(int) rl78_bytes.fixups[i].type]; | |
510 | ||
511 | if (rl78_bytes.fixups[i].reloc) | |
512 | rel = rl78_bytes.fixups[i].reloc; | |
513 | ||
514 | if (frag_then->tc_frag_data) | |
515 | exp = & frag_then->tc_frag_data->fixups[i].exp; | |
516 | else | |
517 | exp = & rl78_bytes.fixups[i].exp; | |
518 | ||
519 | f = fix_new_exp (frag_then, | |
520 | (char *) bytes + idx - frag_then->fr_literal, | |
521 | rl78_bytes.fixups[i].nbits / 8, | |
522 | exp, | |
523 | rl78_bytes.fixups[i].type == RL78REL_PCREL ? 1 : 0, | |
524 | rel); | |
525 | if (frag_then->tc_frag_data) | |
526 | frag_then->tc_frag_data->fixups[i].fixP = f; | |
527 | } | |
528 | } | |
529 | ||
530 | void | |
531 | rl78_cons_fix_new (fragS * frag, | |
532 | int where, | |
533 | int size, | |
534 | expressionS * exp) | |
535 | { | |
536 | bfd_reloc_code_real_type type; | |
537 | ||
538 | switch (size) | |
539 | { | |
540 | case 1: | |
541 | type = BFD_RELOC_8; | |
542 | break; | |
543 | case 2: | |
544 | type = BFD_RELOC_16; | |
545 | break; | |
546 | case 3: | |
547 | type = BFD_RELOC_24; | |
548 | break; | |
549 | case 4: | |
550 | type = BFD_RELOC_32; | |
551 | break; | |
552 | default: | |
553 | as_bad (_("unsupported constant size %d\n"), size); | |
554 | return; | |
555 | } | |
556 | ||
557 | if (exp->X_op == O_subtract && exp->X_op_symbol) | |
558 | { | |
559 | if (size != 4 && size != 2 && size != 1) | |
560 | as_bad (_("difference of two symbols only supported with .long, .short, or .byte")); | |
561 | else | |
562 | type = BFD_RELOC_RL78_DIFF; | |
563 | } | |
564 | ||
565 | fix_new_exp (frag, where, (int) size, exp, 0, type); | |
566 | } | |
567 | ||
568 | /* No relaxation just yet */ | |
569 | int | |
570 | md_estimate_size_before_relax (fragS * fragP ATTRIBUTE_UNUSED, segT segment ATTRIBUTE_UNUSED) | |
571 | { | |
572 | return 0; | |
573 | } | |
9cea966c | 574 | |
99c513f6 DD |
575 | arelent ** |
576 | tc_gen_reloc (asection * seg ATTRIBUTE_UNUSED, fixS * fixp) | |
577 | { | |
578 | static arelent * reloc[8]; | |
579 | int rp; | |
99c513f6 DD |
580 | |
581 | if (fixp->fx_r_type == BFD_RELOC_NONE) | |
582 | { | |
583 | reloc[0] = NULL; | |
584 | return reloc; | |
585 | } | |
586 | ||
587 | if (fixp->fx_subsy | |
588 | && S_GET_SEGMENT (fixp->fx_subsy) == absolute_section) | |
589 | { | |
590 | fixp->fx_offset -= S_GET_VALUE (fixp->fx_subsy); | |
591 | fixp->fx_subsy = NULL; | |
592 | } | |
593 | ||
594 | reloc[0] = (arelent *) xmalloc (sizeof (arelent)); | |
595 | reloc[0]->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *)); | |
596 | * reloc[0]->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy); | |
597 | reloc[0]->address = fixp->fx_frag->fr_address + fixp->fx_where; | |
598 | reloc[0]->addend = fixp->fx_offset; | |
599 | ||
600 | if (fixp->fx_r_type == BFD_RELOC_RL78_32_OP | |
601 | && fixp->fx_subsy) | |
602 | { | |
603 | fixp->fx_r_type = BFD_RELOC_RL78_DIFF; | |
99c513f6 DD |
604 | } |
605 | ||
606 | #define OPX(REL,SYM,ADD) \ | |
607 | reloc[rp] = (arelent *) xmalloc (sizeof (arelent)); \ | |
608 | reloc[rp]->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *)); \ | |
609 | reloc[rp]->howto = bfd_reloc_type_lookup (stdoutput, REL); \ | |
610 | reloc[rp]->addend = ADD; \ | |
611 | * reloc[rp]->sym_ptr_ptr = SYM; \ | |
612 | reloc[rp]->address = fixp->fx_frag->fr_address + fixp->fx_where; \ | |
613 | reloc[++rp] = NULL | |
614 | #define OPSYM(SYM) OPX(BFD_RELOC_RL78_SYM, SYM, 0) | |
615 | #define OPIMM(IMM) OPX(BFD_RELOC_RL78_SYM, abs_symbol.bsym, IMM) | |
616 | #define OP(OP) OPX(BFD_RELOC_RL78_##OP, *reloc[0]->sym_ptr_ptr, 0) | |
617 | #define SYM0() reloc[0]->howto = bfd_reloc_type_lookup (stdoutput, BFD_RELOC_RL78_SYM) | |
618 | ||
619 | rp = 1; | |
620 | ||
621 | /* Certain BFD relocations cannot be translated directly into | |
622 | a single (non-Red Hat) RL78 relocation, but instead need | |
623 | multiple RL78 relocations - handle them here. */ | |
624 | switch (fixp->fx_r_type) | |
625 | { | |
626 | case BFD_RELOC_RL78_DIFF: | |
627 | SYM0 (); | |
628 | OPSYM (symbol_get_bfdsym (fixp->fx_subsy)); | |
629 | OP(OP_SUBTRACT); | |
630 | ||
631 | switch (fixp->fx_size) | |
632 | { | |
633 | case 1: | |
634 | OP(ABS8); | |
635 | break; | |
636 | case 2: | |
637 | OP (ABS16); | |
638 | break; | |
639 | case 4: | |
640 | OP (ABS32); | |
641 | break; | |
642 | } | |
643 | break; | |
644 | ||
645 | case BFD_RELOC_RL78_NEG32: | |
646 | SYM0 (); | |
647 | OP (OP_NEG); | |
648 | OP (ABS32); | |
649 | break; | |
650 | ||
651 | case BFD_RELOC_RL78_LO16: | |
652 | SYM0 (); | |
653 | OPIMM (0xffff); | |
654 | OP (OP_AND); | |
655 | OP (ABS16); | |
656 | break; | |
657 | ||
658 | case BFD_RELOC_RL78_HI16: | |
659 | SYM0 (); | |
660 | OPIMM (16); | |
661 | OP (OP_SHRA); | |
662 | OP (ABS16); | |
663 | break; | |
664 | ||
665 | case BFD_RELOC_RL78_HI8: | |
666 | SYM0 (); | |
667 | OPIMM (16); | |
668 | OP (OP_SHRA); | |
669 | OPIMM (0xff); | |
670 | OP (OP_AND); | |
671 | OP (ABS8); | |
672 | break; | |
673 | ||
674 | default: | |
675 | reloc[0]->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type); | |
676 | reloc[1] = NULL; | |
677 | break; | |
678 | } | |
679 | ||
680 | return reloc; | |
681 | } | |
682 | ||
683 | int | |
684 | rl78_validate_fix_sub (struct fix * f) | |
685 | { | |
686 | /* We permit the subtraction of two symbols in a few cases. */ | |
687 | /* mov #sym1-sym2, R3 */ | |
688 | if (f->fx_r_type == BFD_RELOC_RL78_32_OP) | |
689 | return 1; | |
690 | /* .long sym1-sym2 */ | |
691 | if (f->fx_r_type == BFD_RELOC_RL78_DIFF | |
692 | && ! f->fx_pcrel | |
693 | && (f->fx_size == 4 || f->fx_size == 2 || f->fx_size == 1)) | |
694 | return 1; | |
695 | return 0; | |
696 | } | |
697 | ||
698 | long | |
699 | md_pcrel_from_section (fixS * fixP, segT sec) | |
700 | { | |
701 | long rv; | |
702 | ||
703 | if (fixP->fx_addsy != NULL | |
704 | && (! S_IS_DEFINED (fixP->fx_addsy) | |
705 | || S_GET_SEGMENT (fixP->fx_addsy) != sec)) | |
706 | /* The symbol is undefined (or is defined but not in this section). | |
707 | Let the linker figure it out. */ | |
708 | return 0; | |
709 | ||
710 | rv = fixP->fx_frag->fr_address + fixP->fx_where; | |
711 | switch (fixP->fx_r_type) | |
712 | { | |
713 | case BFD_RELOC_8_PCREL: | |
714 | rv += 1; | |
715 | break; | |
716 | case BFD_RELOC_16_PCREL: | |
717 | rv += 2; | |
718 | break; | |
719 | default: | |
720 | break; | |
721 | } | |
722 | return rv; | |
723 | } | |
724 | ||
725 | void | |
726 | md_apply_fix (struct fix * f ATTRIBUTE_UNUSED, | |
727 | valueT * t ATTRIBUTE_UNUSED, | |
728 | segT s ATTRIBUTE_UNUSED) | |
729 | { | |
730 | char * op; | |
731 | unsigned long val; | |
732 | ||
733 | if (f->fx_addsy && S_FORCE_RELOC (f->fx_addsy, 1)) | |
734 | return; | |
735 | if (f->fx_subsy && S_FORCE_RELOC (f->fx_subsy, 1)) | |
736 | return; | |
737 | ||
738 | op = f->fx_frag->fr_literal + f->fx_where; | |
739 | val = (unsigned long) * t; | |
740 | ||
741 | switch (f->fx_r_type) | |
742 | { | |
743 | case BFD_RELOC_NONE: | |
744 | break; | |
745 | ||
9cea966c DD |
746 | case BFD_RELOC_RL78_RELAX: |
747 | f->fx_done = 1; | |
748 | break; | |
749 | ||
99c513f6 DD |
750 | case BFD_RELOC_8: |
751 | case BFD_RELOC_8_PCREL: | |
752 | op[0] = val; | |
753 | break; | |
754 | ||
755 | case BFD_RELOC_16: | |
756 | case BFD_RELOC_16_PCREL: | |
757 | op[0] = val; | |
758 | op[1] = val >> 8; | |
759 | break; | |
760 | ||
761 | case BFD_RELOC_24: | |
762 | op[0] = val; | |
763 | op[1] = val >> 8; | |
764 | op[2] = val >> 16; | |
765 | break; | |
766 | ||
767 | case BFD_RELOC_32: | |
768 | case BFD_RELOC_RL78_DIFF: | |
769 | op[0] = val; | |
770 | op[1] = val >> 8; | |
771 | op[2] = val >> 16; | |
772 | op[3] = val >> 24; | |
773 | break; | |
774 | ||
775 | default: | |
776 | as_bad (_("Unknown reloc in md_apply_fix: %s"), | |
777 | bfd_get_reloc_code_name (f->fx_r_type)); | |
778 | break; | |
779 | } | |
780 | ||
781 | if (f->fx_addsy == NULL) | |
782 | f->fx_done = 1; | |
783 | } | |
784 | ||
785 | valueT | |
786 | md_section_align (segT segment, valueT size) | |
787 | { | |
788 | int align = bfd_get_section_alignment (stdoutput, segment); | |
789 | return ((size + (1 << align) - 1) & (-1 << align)); | |
790 | } | |
791 | ||
792 | void | |
793 | md_convert_frag (bfd * abfd ATTRIBUTE_UNUSED, | |
794 | segT segment ATTRIBUTE_UNUSED, | |
795 | fragS * fragP ATTRIBUTE_UNUSED) | |
796 | { | |
797 | /* No relaxation yet */ | |
9cea966c | 798 | fragP->fr_var = 0; |
99c513f6 | 799 | } |