1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24: #include "sysdep.h"
25: #include "bfd.h"
26: #include "libbfd.h"
27: #include "bfdlink.h"
28: #include "genlink.h"
29: #include "coff/h8300.h"
30: #include "coff/internal.h"
31: #include "libcoff.h"
32: #include "libiberty.h"
33:
34: #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (1)
35:
36:
37:
38:
39:
40:
41:
42: struct funcvec_hash_entry
43: {
44:
45: struct bfd_hash_entry root;
46:
47:
48:
49: bfd_vma offset;
50: };
51:
52: struct funcvec_hash_table
53: {
54:
55: struct bfd_hash_table root;
56:
57: bfd *abfd;
58:
59:
60: unsigned int offset;
61: };
62:
63: static struct bfd_hash_entry *
64: funcvec_hash_newfunc
65: (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
66:
67: static bfd_reloc_status_type special
68: (bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **);
69: static int select_reloc
70: (reloc_howto_type *);
71: static void rtype2howto
72: (arelent *, struct internal_reloc *);
73: static void reloc_processing
74: (arelent *, struct internal_reloc *, asymbol **, bfd *, asection *);
75: static bfd_boolean h8300_symbol_address_p
76: (bfd *, asection *, bfd_vma);
77: static int h8300_reloc16_estimate
78: (bfd *, asection *, arelent *, unsigned int,
79: struct bfd_link_info *);
80: static void h8300_reloc16_extra_cases
81: (bfd *, struct bfd_link_info *, struct bfd_link_order *, arelent *,
82: bfd_byte *, unsigned int *, unsigned int *);
83: static bfd_boolean h8300_bfd_link_add_symbols
84: (bfd *, struct bfd_link_info *);
85:
86:
87: #define funcvec_hash_lookup(table, string, create, copy) \
88: ((struct funcvec_hash_entry *) \
89: bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
90:
91:
92:
93:
94:
95: struct h8300_coff_link_hash_table {
96:
97: struct generic_link_hash_table root;
98:
99:
100:
101: asection *vectors_sec;
102:
103:
104:
105: struct funcvec_hash_table *funcvec_hash_table;
106: };
107:
108: static struct bfd_link_hash_table *h8300_coff_link_hash_table_create (bfd *);
109:
110:
111:
112: #define h8300_coff_hash_table(p) \
113: ((struct h8300_coff_link_hash_table *) ((coff_hash_table (p))))
114:
115:
116:
117:
118: static struct bfd_hash_entry *
119: funcvec_hash_newfunc (struct bfd_hash_entry *entry,
120: struct bfd_hash_table *gen_table,
121: const char *string)
122: {
123: struct funcvec_hash_entry *ret;
124: struct funcvec_hash_table *table;
125:
126: ret = (struct funcvec_hash_entry *) entry;
127: table = (struct funcvec_hash_table *) gen_table;
128:
129:
130:
131: if (ret == NULL)
132: ret = ((struct funcvec_hash_entry *)
133: bfd_hash_allocate (gen_table,
134: sizeof (struct funcvec_hash_entry)));
135: if (ret == NULL)
136: return NULL;
137:
138:
139: ret = ((struct funcvec_hash_entry *)
140: bfd_hash_newfunc ((struct bfd_hash_entry *) ret, gen_table, string));
141:
142: if (ret == NULL)
143: return NULL;
144:
145:
146: ret->offset = table->offset;
147:
148:
149:
150:
151: switch (bfd_get_mach (table->abfd))
152: {
153: case bfd_mach_h8300:
154: case bfd_mach_h8300hn:
155: case bfd_mach_h8300sn:
156: table->offset += 2;
157: break;
158: case bfd_mach_h8300h:
159: case bfd_mach_h8300s:
160: table->offset += 4;
161: break;
162: default:
163: return NULL;
164: }
165:
166:
167: return (struct bfd_hash_entry *) ret;
168: }
169:
170:
171:
172: static bfd_boolean
173: funcvec_hash_table_init (struct funcvec_hash_table *table,
174: bfd *abfd,
175: struct bfd_hash_entry *(*newfunc)
176: (struct bfd_hash_entry *,
177: struct bfd_hash_table *,
178: const char *),
179: unsigned int entsize)
180: {
181:
182:
183: table->offset = 0;
184: table->abfd = abfd;
185: return (bfd_hash_table_init (&table->root, newfunc, entsize));
186: }
187:
188:
189:
190:
191:
192: static struct bfd_link_hash_table *
193: h8300_coff_link_hash_table_create (bfd *abfd)
194: {
195: struct h8300_coff_link_hash_table *ret;
196: bfd_size_type amt = sizeof (struct h8300_coff_link_hash_table);
197:
198: ret = (struct h8300_coff_link_hash_table *) bfd_malloc (amt);
199: if (ret == NULL)
200: return NULL;
201: if (!_bfd_link_hash_table_init (&ret->root.root, abfd,
202: _bfd_generic_link_hash_newfunc,
203: sizeof (struct generic_link_hash_entry)))
204: {
205: free (ret);
206: return NULL;
207: }
208:
209:
210: ret->vectors_sec = NULL;
211: ret->funcvec_hash_table = NULL;
212:
213:
214: return &ret->root.root;
215: }
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226: static bfd_reloc_status_type
227: special (bfd *abfd ATTRIBUTE_UNUSED,
228: arelent *reloc_entry ATTRIBUTE_UNUSED,
229: asymbol *symbol ATTRIBUTE_UNUSED,
230: PTR data ATTRIBUTE_UNUSED,
231: asection *input_section ATTRIBUTE_UNUSED,
232: bfd *output_bfd,
233: char **error_message ATTRIBUTE_UNUSED)
234: {
235: if (output_bfd == (bfd *) NULL)
236: return bfd_reloc_continue;
237:
238:
239: reloc_entry->address += input_section->output_offset;
240: return bfd_reloc_ok;
241: }
242:
243: static reloc_howto_type howto_table[] = {
244: HOWTO (R_RELBYTE, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "8", FALSE, 0x000000ff, 0x000000ff, FALSE),
245: HOWTO (R_RELWORD, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "16", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
246: HOWTO (R_RELLONG, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, special, "32", FALSE, 0xffffffff, 0xffffffff, FALSE),
247: HOWTO (R_PCRBYTE, 0, 0, 8, TRUE, 0, complain_overflow_signed, special, "DISP8", FALSE, 0x000000ff, 0x000000ff, TRUE),
248: HOWTO (R_PCRWORD, 0, 1, 16, TRUE, 0, complain_overflow_signed, special, "DISP16", FALSE, 0x0000ffff, 0x0000ffff, TRUE),
249: HOWTO (R_PCRLONG, 0, 2, 32, TRUE, 0, complain_overflow_signed, special, "DISP32", FALSE, 0xffffffff, 0xffffffff, TRUE),
250: HOWTO (R_MOV16B1, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "relaxable mov.b:16", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
251: HOWTO (R_MOV16B2, 0, 1, 8, FALSE, 0, complain_overflow_bitfield, special, "relaxed mov.b:16", FALSE, 0x000000ff, 0x000000ff, FALSE),
252: HOWTO (R_JMP1, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "16/pcrel", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
253: HOWTO (R_JMP2, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "pcrecl/16", FALSE, 0x000000ff, 0x000000ff, FALSE),
254: HOWTO (R_JMPL1, 0, 2, 32, FALSE, 0, complain_overflow_bitfield, special, "24/pcrell", FALSE, 0x00ffffff, 0x00ffffff, FALSE),
255: HOWTO (R_JMPL2, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "pc8/24", FALSE, 0x000000ff, 0x000000ff, FALSE),
256: HOWTO (R_MOV24B1, 0, 1, 32, FALSE, 0, complain_overflow_bitfield, special, "relaxable mov.b:24", FALSE, 0xffffffff, 0xffffffff, FALSE),
257: HOWTO (R_MOV24B2, 0, 1, 8, FALSE, 0, complain_overflow_bitfield, special, "relaxed mov.b:24", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
258:
259:
260:
261:
262: HOWTO (R_MEM_INDIRECT, 0, 0, 8, FALSE, 0, complain_overflow_bitfield, special, "8/indirect", FALSE, 0x000000ff, 0x000000ff, FALSE),
263:
264:
265:
266: HOWTO (R_PCRWORD_B, 0, 0, 8, TRUE, 0, complain_overflow_bitfield, special, "relaxed bCC:16", FALSE, 0x000000ff, 0x000000ff, FALSE),
267:
268: HOWTO (R_MOVL1, 0, 2, 32, FALSE, 0, complain_overflow_bitfield,special, "32/24 relaxable move", FALSE, 0xffffffff, 0xffffffff, FALSE),
269:
270: HOWTO (R_MOVL2, 0, 1, 16, FALSE, 0, complain_overflow_bitfield, special, "32/24 relaxed move", FALSE, 0x0000ffff, 0x0000ffff, FALSE),
271:
272: HOWTO (R_BCC_INV, 0, 0, 8, TRUE, 0, complain_overflow_signed, special, "DISP8 inverted", FALSE, 0x000000ff, 0x000000ff, TRUE),
273:
274: HOWTO (R_JMP_DEL, 0, 0, 8, TRUE, 0, complain_overflow_signed, special, "Deleted jump", FALSE, 0x000000ff, 0x000000ff, TRUE),
275: };
276:
277:
278:
279: #define SELECT_RELOC(x,howto) \
280: { x.r_type = select_reloc (howto); }
281:
282: #define BADMAG(x) (H8300BADMAG (x) && H8300HBADMAG (x) && H8300SBADMAG (x) \
283: && H8300HNBADMAG(x) && H8300SNBADMAG(x))
284: #define H8300 1
285: #define __A_MAGIC_SET__
286:
287:
288: #define SWAP_IN_RELOC_OFFSET H_GET_32
289: #define SWAP_OUT_RELOC_OFFSET H_PUT_32
290: #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
291: dst->r_stuff[0] = 'S'; \
292: dst->r_stuff[1] = 'C';
293:
294: static int
295: select_reloc (reloc_howto_type *howto)
296: {
297: return howto->type;
298: }
299:
300:
301:
302: static void
303: rtype2howto (arelent *internal, struct internal_reloc *dst)
304: {
305: switch (dst->r_type)
306: {
307: case R_RELBYTE:
308: internal->howto = howto_table + 0;
309: break;
310: case R_RELWORD:
311: internal->howto = howto_table + 1;
312: break;
313: case R_RELLONG:
314: internal->howto = howto_table + 2;
315: break;
316: case R_PCRBYTE:
317: internal->howto = howto_table + 3;
318: break;
319: case R_PCRWORD:
320: internal->howto = howto_table + 4;
321: break;
322: case R_PCRLONG:
323: internal->howto = howto_table + 5;
324: break;
325: case R_MOV16B1:
326: internal->howto = howto_table + 6;
327: break;
328: case R_MOV16B2:
329: internal->howto = howto_table + 7;
330: break;
331: case R_JMP1:
332: internal->howto = howto_table + 8;
333: break;
334: case R_JMP2:
335: internal->howto = howto_table + 9;
336: break;
337: case R_JMPL1:
338: internal->howto = howto_table + 10;
339: break;
340: case R_JMPL2:
341: internal->howto = howto_table + 11;
342: break;
343: case R_MOV24B1:
344: internal->howto = howto_table + 12;
345: break;
346: case R_MOV24B2:
347: internal->howto = howto_table + 13;
348: break;
349: case R_MEM_INDIRECT:
350: internal->howto = howto_table + 14;
351: break;
352: case R_PCRWORD_B:
353: internal->howto = howto_table + 15;
354: break;
355: case R_MOVL1:
356: internal->howto = howto_table + 16;
357: break;
358: case R_MOVL2:
359: internal->howto = howto_table + 17;
360: break;
361: case R_BCC_INV:
362: internal->howto = howto_table + 18;
363: break;
364: case R_JMP_DEL:
365: internal->howto = howto_table + 19;
366: break;
367: default:
368: abort ();
369: break;
370: }
371: }
372:
373: #define RTYPE2HOWTO(internal, relocentry) rtype2howto (internal, relocentry)
374:
375:
376:
377: #define CALC_ADDEND(abfd, symbol, ext_reloc, cache_ptr) \
378: cache_ptr->addend = ext_reloc.r_offset;
379:
380: #define RELOC_PROCESSING(relent,reloc,symbols,abfd,section) \
381: reloc_processing (relent, reloc, symbols, abfd, section)
382:
383: static void
384: reloc_processing (arelent *relent, struct internal_reloc *reloc,
385: asymbol **symbols, bfd *abfd, asection *section)
386: {
387: relent->address = reloc->r_vaddr;
388: rtype2howto (relent, reloc);
389:
390: if (((int) reloc->r_symndx) > 0)
391: relent->sym_ptr_ptr = symbols + obj_convert (abfd)[reloc->r_symndx];
392: else
393: relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
394:
395: relent->addend = reloc->r_offset;
396: relent->address -= section->vma;
397: }
398:
399: static bfd_boolean
400: h8300_symbol_address_p (bfd *abfd, asection *input_section, bfd_vma address)
401: {
402: asymbol **s;
403:
404: s = _bfd_generic_link_get_symbols (abfd);
405: BFD_ASSERT (s != (asymbol **) NULL);
406:
407:
408:
409: while (*s)
410: {
411: asymbol *p = *s;
412:
413: if (p->section == input_section
414: && (input_section->output_section->vma
415: + input_section->output_offset
416: + p->value) == address)
417: return TRUE;
418: s++;
419: }
420: return FALSE;
421: }
422:
423:
424:
425:
426:
427:
428:
429:
430:
431: static int
432: h8300_reloc16_estimate (bfd *abfd, asection *input_section, arelent *reloc,
433: unsigned int shrink, struct bfd_link_info *link_info)
434: {
435: bfd_vma value;
436: bfd_vma dot;
437: bfd_vma gap;
438: static asection *last_input_section = NULL;
439: static arelent *last_reloc = NULL;
440:
441:
442:
443:
444:
445: bfd_vma address = reloc->address - shrink;
446:
447: if (input_section != last_input_section)
448: last_reloc = NULL;
449:
450:
451: switch (reloc->howto->type)
452: {
453:
454:
455: case R_JMP1:
456: case R_JMPL1:
457:
458: value = bfd_coff_reloc16_get_value (reloc, link_info, input_section);
459:
460:
461: dot = (input_section->output_section->vma
462: + input_section->output_offset + address);
463:
464:
465: dot += (reloc->howto->type == R_JMP1 ? 1 : 2);
466:
467:
468: gap = value - dot;
469:
470:
471:
472:
473: if ((int) gap >= -128 && (int) gap <= 128)
474: {
475: bfd_byte code;
476:
477: if (!bfd_get_section_contents (abfd, input_section, & code,
478: reloc->address, 1))
479: break;
480: code = bfd_get_8 (abfd, & code);
481:
482:
483:
484:
485:
486:
487:
488:
489:
490:
491:
492:
493:
494:
495:
496:
497:
498:
499:
500:
501:
502:
503:
504:
505:
506:
507:
508:
509:
510: if (code == 0x5a
511: && gap <= 126
512: && last_reloc
513: && last_reloc->howto->type == R_PCRBYTE)
514: {
515: bfd_vma last_value;
516: last_value = bfd_coff_reloc16_get_value (last_reloc, link_info,
517: input_section) + 1;
518:
519: if (last_value == dot + 2
520: && last_reloc->address + 1 == reloc->address
521: && !h8300_symbol_address_p (abfd, input_section, dot - 2))
522: {
523: reloc->howto = howto_table + 19;
524: last_reloc->howto = howto_table + 18;
525: last_reloc->sym_ptr_ptr = reloc->sym_ptr_ptr;
526: last_reloc->addend = reloc->addend;
527: shrink += 4;
528: bfd_perform_slip (abfd, 4, input_section, address);
529: break;
530: }
531: }
532:
533:
534: reloc->howto = reloc->howto + 1;
535:
536:
537: shrink += 2;
538: bfd_perform_slip (abfd, 2, input_section, address);
539: }
540: