Fix formatting
[platform/upstream/binutils.git] / gas / cgen.c
1 /* GAS interface for targets using CGEN: Cpu tools GENerator.
2    Copyright 1996, 1997, 1998, 1999, 2000, 2001
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 2, 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 Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 #include <setjmp.h>
22 #include "ansidecl.h"
23 #include "libiberty.h"
24 #include "bfd.h"
25 #include "symcat.h"
26 #include "cgen-desc.h"
27 #include "as.h"
28 #include "subsegs.h"
29 #include "cgen.h"
30 #include "dwarf2dbg.h"
31
32 /* Opcode table descriptor, must be set by md_begin.  */
33
34 CGEN_CPU_DESC gas_cgen_cpu_desc;
35
36 /* Callback to insert a register into the symbol table.
37    A target may choose to let GAS parse the registers.
38    ??? Not currently used.  */
39
40 void
41 cgen_asm_record_register (name, number)
42      char *name;
43      int number;
44 {
45   /* Use symbol_create here instead of symbol_new so we don't try to
46      output registers into the object file's symbol table.  */
47   symbol_table_insert (symbol_create (name, reg_section,
48                                       number, &zero_address_frag));
49 }
50
51 /* We need to keep a list of fixups.  We can't simply generate them as
52    we go, because that would require us to first create the frag, and
53    that would screw up references to ``.''.
54
55    This is used by cpu's with simple operands.  It keeps knowledge of what
56    an `expressionS' is and what a `fixup' is out of CGEN which for the time
57    being is preferable.
58
59    OPINDEX is the index in the operand table.
60    OPINFO is something the caller chooses to help in reloc determination.  */
61
62 struct fixup {
63   int opindex;
64   int opinfo;
65   expressionS exp;
66 };
67
68 static struct fixup fixups[GAS_CGEN_MAX_FIXUPS];
69 static int num_fixups;
70
71 /* Prepare to parse an instruction.
72    ??? May wish to make this static and delete calls in md_assemble.  */
73
74 void
75 gas_cgen_init_parse ()
76 {
77   num_fixups = 0;
78 }
79
80 /* Queue a fixup.  */
81
82 static void
83 queue_fixup (opindex, opinfo, expP)
84      int           opindex;
85      int           opinfo;
86      expressionS * expP;
87 {
88   /* We need to generate a fixup for this expression.  */
89   if (num_fixups >= GAS_CGEN_MAX_FIXUPS)
90     as_fatal (_("too many fixups"));
91   fixups[num_fixups].exp     = *expP;
92   fixups[num_fixups].opindex = opindex;
93   fixups[num_fixups].opinfo  = opinfo;
94   ++ num_fixups;
95 }
96
97 /* The following functions allow fixup chains to be stored, retrieved,
98    and swapped.  They are a generalization of a pre-existing scheme
99    for storing, restoring and swapping fixup chains that was used by
100    the m32r port.  The functionality is essentially the same, only
101    instead of only being able to store a single fixup chain, an entire
102    array of fixup chains can be stored.  It is the user's responsibility
103    to keep track of how many fixup chains have been stored and which
104    elements of the array they are in.
105
106    The algorithms used are the same as in the old scheme.  Other than the 
107    "array-ness" of the whole thing, the functionality is identical to the 
108    old scheme.
109
110    gas_cgen_initialize_saved_fixups_array():
111       Sets num_fixups_in_chain to 0 for each element. Call this from
112       md_begin() if you plan to use these functions and you want the
113       fixup count in each element to be set to 0 intially.  This is
114       not necessary, but it's included just in case.  It performs
115       the same function for each element in the array of fixup chains
116       that gas_init_parse() performs for the current fixups.
117
118    gas_cgen_save_fixups (element):
119       element - element number of the array you wish to store the fixups
120                 to.  No mechanism is built in for tracking what element
121                 was last stored to.
122
123    gas_cgen_restore_fixups (element):
124       element - element number of the array you wish to restore the fixups
125                 from.
126
127    gas_cgen_swap_fixups(int element):
128        element - swap the current fixups with those in this element number.
129 */
130
131 struct saved_fixups {
132   struct fixup fixup_chain[GAS_CGEN_MAX_FIXUPS];
133   int num_fixups_in_chain;
134 };
135
136 static struct saved_fixups stored_fixups[MAX_SAVED_FIXUP_CHAINS];
137
138 void
139 gas_cgen_initialize_saved_fixups_array ()
140 {
141   int i = 0;
142
143   while (i < MAX_SAVED_FIXUP_CHAINS)
144     stored_fixups[i++].num_fixups_in_chain = 0;
145 }
146
147 void
148 gas_cgen_save_fixups (i)
149      int i;
150 {
151   if (i < 0 || i >= MAX_SAVED_FIXUP_CHAINS)
152     {
153       as_fatal ("index into stored_fixups[] out of bounds");
154       return;
155     }
156
157   stored_fixups[i].num_fixups_in_chain = num_fixups;
158   memcpy (stored_fixups[i].fixup_chain, fixups,
159           sizeof (fixups[0]) * num_fixups);
160   num_fixups = 0;
161 }
162
163 void
164 gas_cgen_restore_fixups (i)
165      int i;
166 {
167   if (i < 0 || i >= MAX_SAVED_FIXUP_CHAINS)
168     {
169       as_fatal ("index into stored_fixups[] out of bounds");
170       return;
171     }
172
173   num_fixups = stored_fixups[i].num_fixups_in_chain;
174   memcpy (fixups,stored_fixups[i].fixup_chain,
175           (sizeof (stored_fixups[i].fixup_chain[0])) * num_fixups);
176   stored_fixups[i].num_fixups_in_chain = 0;
177 }
178
179 void
180 gas_cgen_swap_fixups (i)
181      int i;
182 {
183   if (i < 0 || i >= MAX_SAVED_FIXUP_CHAINS)
184     {
185       as_fatal ("index into stored_fixups[] out of bounds");
186       return;
187     }
188
189   if (num_fixups == 0)
190     gas_cgen_restore_fixups (i);
191
192   else if (stored_fixups[i].num_fixups_in_chain == 0)
193     gas_cgen_save_fixups (i);
194
195   else
196     {
197       int tmp;
198       struct fixup tmp_fixup;
199
200       tmp = stored_fixups[i].num_fixups_in_chain;
201       stored_fixups[i].num_fixups_in_chain = num_fixups;
202       num_fixups = tmp;
203
204       for (tmp = GAS_CGEN_MAX_FIXUPS; tmp--;)
205         {
206           tmp_fixup = stored_fixups[i].fixup_chain [tmp];
207           stored_fixups[i].fixup_chain[tmp] = fixups [tmp];
208           fixups [tmp] = tmp_fixup;
209         }
210     }
211 }
212
213 /* Default routine to record a fixup.
214    This is a cover function to fix_new.
215    It exists because we record INSN with the fixup.
216
217    FRAG and WHERE are their respective arguments to fix_new_exp.
218    LENGTH is in bits.
219    OPINFO is something the caller chooses to help in reloc determination.
220
221    At this point we do not use a bfd_reloc_code_real_type for
222    operands residing in the insn, but instead just use the
223    operand index.  This lets us easily handle fixups for any
224    operand type.  We pick a BFD reloc type in md_apply_fix.  */
225
226 fixS *
227 gas_cgen_record_fixup (frag, where, insn, length, operand, opinfo, symbol, offset)
228      fragS *              frag;
229      int                  where;
230      const CGEN_INSN *    insn;
231      int                  length;
232      const CGEN_OPERAND * operand;
233      int                  opinfo;
234      symbolS *            symbol;
235      offsetT              offset;
236 {
237   fixS *fixP;
238
239   /* It may seem strange to use operand->attrs and not insn->attrs here,
240      but it is the operand that has a pc relative relocation.  */
241
242   fixP = fix_new (frag, where, length / 8, symbol, offset,
243                   CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_PCREL_ADDR),
244                   (bfd_reloc_code_real_type)
245                     ((int) BFD_RELOC_UNUSED
246                      + (int) operand->type));
247   fixP->fx_cgen.insn = insn;
248   fixP->fx_cgen.opinfo = opinfo;
249
250   return fixP;
251 }
252
253 /* Default routine to record a fixup given an expression.
254    This is a cover function to fix_new_exp.
255    It exists because we record INSN with the fixup.
256
257    FRAG and WHERE are their respective arguments to fix_new_exp.
258    LENGTH is in bits.
259    OPINFO is something the caller chooses to help in reloc determination.
260
261    At this point we do not use a bfd_reloc_code_real_type for
262    operands residing in the insn, but instead just use the
263    operand index.  This lets us easily handle fixups for any
264    operand type.  We pick a BFD reloc type in md_apply_fix.  */
265
266 fixS *
267 gas_cgen_record_fixup_exp (frag, where, insn, length, operand, opinfo, exp)
268      fragS *              frag;
269      int                  where;
270      const CGEN_INSN *    insn;
271      int                  length;
272      const CGEN_OPERAND * operand;
273      int                  opinfo;
274      expressionS *        exp;
275 {
276   fixS *fixP;
277
278   /* It may seem strange to use operand->attrs and not insn->attrs here,
279      but it is the operand that has a pc relative relocation.  */
280
281   fixP = fix_new_exp (frag, where, length / 8, exp,
282                       CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_PCREL_ADDR),
283                       (bfd_reloc_code_real_type)
284                         ((int) BFD_RELOC_UNUSED
285                          + (int) operand->type));
286   fixP->fx_cgen.insn = insn;
287   fixP->fx_cgen.opinfo = opinfo;
288
289   return fixP;
290 }
291
292 /* Used for communication between the next two procedures.  */
293 static jmp_buf expr_jmp_buf;
294 static int expr_jmp_buf_p;
295
296 /* Callback for cgen interface.  Parse the expression at *STRP.
297    The result is an error message or NULL for success (in which case
298    *STRP is advanced past the parsed text).
299    WANT is an indication of what the caller is looking for.
300    If WANT == CGEN_ASM_PARSE_INIT the caller is beginning to try to match
301    a table entry with the insn, reset the queued fixups counter.
302    An enum cgen_parse_operand_result is stored in RESULTP.
303    OPINDEX is the operand's table entry index.
304    OPINFO is something the caller chooses to help in reloc determination.
305    The resulting value is stored in VALUEP.  */
306
307 const char *
308 gas_cgen_parse_operand (cd, want, strP, opindex, opinfo, resultP, valueP)
309      CGEN_CPU_DESC cd ATTRIBUTE_UNUSED;
310      enum cgen_parse_operand_type want;
311      const char **strP;
312      int opindex;
313      int opinfo;
314      enum cgen_parse_operand_result *resultP;
315      bfd_vma *valueP;
316 {
317 #ifdef __STDC__
318   /* These are volatile to survive the setjmp.  */
319   char * volatile hold;
320   enum cgen_parse_operand_result * volatile resultP_1;
321 #else
322   static char *hold;
323   static enum cgen_parse_operand_result *resultP_1;
324 #endif
325   const char *errmsg = NULL;
326   expressionS exp;
327
328   if (want == CGEN_PARSE_OPERAND_INIT)
329     {
330       gas_cgen_init_parse ();
331       return NULL;
332     }
333
334   resultP_1 = resultP;
335   hold = input_line_pointer;
336   input_line_pointer = (char *) *strP;
337
338   /* We rely on md_operand to longjmp back to us.
339      This is done via gas_cgen_md_operand.  */
340   if (setjmp (expr_jmp_buf) != 0)
341     {
342       expr_jmp_buf_p = 0;
343       input_line_pointer = (char *) hold;
344       *resultP_1 = CGEN_PARSE_OPERAND_RESULT_ERROR;
345       return _("illegal operand");
346     }
347
348   expr_jmp_buf_p = 1;
349   expression (&exp);
350   expr_jmp_buf_p = 0;
351
352   *strP = input_line_pointer;
353   input_line_pointer = hold;
354
355   /* FIXME: Need to check `want'.  */
356
357   switch (exp.X_op)
358     {
359     case O_illegal:
360       errmsg = _("illegal operand");
361       *resultP = CGEN_PARSE_OPERAND_RESULT_ERROR;
362       break;
363     case O_absent:
364       errmsg = _("missing operand");
365       *resultP = CGEN_PARSE_OPERAND_RESULT_ERROR;
366       break;
367     case O_constant:
368       *valueP = exp.X_add_number;
369       *resultP = CGEN_PARSE_OPERAND_RESULT_NUMBER;
370       break;
371     case O_register:
372       *valueP = exp.X_add_number;
373       *resultP = CGEN_PARSE_OPERAND_RESULT_REGISTER;
374       break;
375     default:
376       queue_fixup (opindex, opinfo, &exp);
377       *valueP = 0;
378       *resultP = CGEN_PARSE_OPERAND_RESULT_QUEUED;
379       break;
380     }
381
382   return errmsg;
383 }
384
385 /* md_operand handler to catch unrecognized expressions and halt the
386    parsing process so the next entry can be tried.
387
388    ??? This could be done differently by adding code to `expression'.  */
389
390 void
391 gas_cgen_md_operand (expressionP)
392      expressionS *expressionP ATTRIBUTE_UNUSED;
393 {
394   /* Don't longjmp if we're not called from within cgen_parse_operand().  */
395   if (expr_jmp_buf_p)
396     longjmp (expr_jmp_buf, 1);
397 }
398
399 /* Finish assembling instruction INSN.
400    BUF contains what we've built up so far.
401    LENGTH is the size of the insn in bits.
402    RELAX_P is non-zero if relaxable insns should be emitted as such.
403    Otherwise they're emitted in non-relaxable forms.
404    The "result" is stored in RESULT if non-NULL.  */
405
406 void
407 gas_cgen_finish_insn (insn, buf, length, relax_p, result)
408      const CGEN_INSN *insn;
409      CGEN_INSN_BYTES_PTR buf;
410      unsigned int length;
411      int relax_p;
412      finished_insnS *result;
413 {
414   int i;
415   int relax_operand;
416   char *f;
417   unsigned int byte_len = length / 8;
418
419   /* ??? Target foo issues various warnings here, so one might want to provide
420      a hook here.  However, our caller is defined in tc-foo.c so there
421      shouldn't be a need for a hook.  */
422
423   /* Write out the instruction.
424      It is important to fetch enough space in one call to `frag_more'.
425      We use (f - frag_now->fr_literal) to compute where we are and we
426      don't want frag_now to change between calls.
427
428      Relaxable instructions: We need to ensure we allocate enough
429      space for the largest insn.  */
430
431   if (CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAX))
432     /* These currently shouldn't get here.  */
433     abort ();
434
435   /* Is there a relaxable insn with the relaxable operand needing a fixup?  */
436
437   relax_operand = -1;
438   if (relax_p && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXABLE))
439     {
440       /* Scan the fixups for the operand affected by relaxing
441          (i.e. the branch address).  */
442
443       for (i = 0; i < num_fixups; ++i)
444         {
445           if (CGEN_OPERAND_ATTR_VALUE (cgen_operand_lookup_by_num (gas_cgen_cpu_desc, fixups[i].opindex),
446                                        CGEN_OPERAND_RELAX))
447             {
448               relax_operand = i;
449               break;
450             }
451         }
452     }
453
454   if (relax_operand != -1)
455     {
456       int max_len;
457       fragS *old_frag;
458       expressionS *exp;
459       symbolS *sym;
460       offsetT off;
461
462 #ifdef TC_CGEN_MAX_RELAX
463       max_len = TC_CGEN_MAX_RELAX (insn, byte_len);
464 #else
465       max_len = CGEN_MAX_INSN_SIZE;
466 #endif
467       /* Ensure variable part and fixed part are in same fragment.  */
468       /* FIXME: Having to do this seems like a hack.  */
469       frag_grow (max_len);
470
471       /* Allocate space for the fixed part.  */
472       f = frag_more (byte_len);
473
474       /* Create a relaxable fragment for this instruction.  */
475       old_frag = frag_now;
476
477       exp = &fixups[relax_operand].exp;
478       sym = exp->X_add_symbol;
479       off = exp->X_add_number;
480       if (exp->X_op != O_constant && exp->X_op != O_symbol)
481         {
482           /* Handle complex expressions.  */
483           sym = make_expr_symbol (exp);
484           off = 0;
485         }
486
487       frag_var (rs_machine_dependent,
488                 max_len - byte_len /* max chars */,
489                 0 /* variable part already allocated */,
490                 /* FIXME: When we machine generate the relax table,
491                    machine generate a macro to compute subtype.  */
492                 1 /* subtype */,
493                 sym,
494                 off,
495                 f);
496
497       /* Record the operand number with the fragment so md_convert_frag
498          can use gas_cgen_md_record_fixup to record the appropriate reloc.  */
499       old_frag->fr_cgen.insn    = insn;
500       old_frag->fr_cgen.opindex = fixups[relax_operand].opindex;
501       old_frag->fr_cgen.opinfo  = fixups[relax_operand].opinfo;
502       if (result)
503         result->frag = old_frag;
504     }
505   else
506     {
507       f = frag_more (byte_len);
508       if (result)
509         result->frag = frag_now;
510     }
511
512   /* If we're recording insns as numbers (rather than a string of bytes),
513      target byte order handling is deferred until now.  */
514 #if CGEN_INT_INSN_P
515   cgen_put_insn_value (gas_cgen_cpu_desc, f, length, *buf);
516 #else
517   memcpy (f, buf, byte_len);
518 #endif
519
520   /* Emit DWARF2 debugging information.  */
521   dwarf2_emit_insn (byte_len);
522
523   /* Create any fixups.  */
524   for (i = 0; i < num_fixups; ++i)
525     {
526       fixS *fixP;
527       const CGEN_OPERAND *operand =
528         cgen_operand_lookup_by_num (gas_cgen_cpu_desc, fixups[i].opindex);
529
530       /* Don't create fixups for these.  That's done during relaxation.
531          We don't need to test for CGEN_INSN_RELAX as they can't get here
532          (see above).  */
533       if (relax_p
534           && CGEN_INSN_ATTR_VALUE (insn, CGEN_INSN_RELAXABLE)
535           && CGEN_OPERAND_ATTR_VALUE (operand, CGEN_OPERAND_RELAX))
536         continue;
537
538 #ifndef md_cgen_record_fixup_exp
539 #define md_cgen_record_fixup_exp gas_cgen_record_fixup_exp
540 #endif
541
542       fixP = md_cgen_record_fixup_exp (frag_now, f - frag_now->fr_literal,
543                                        insn, length, operand,
544                                        fixups[i].opinfo,
545                                        &fixups[i].exp);
546       if (result)
547         result->fixups[i] = fixP;
548     }
549
550   if (result)
551     {
552       result->num_fixups = num_fixups;
553       result->addr = f;
554     }
555 }
556
557 /* Apply a fixup to the object code.  This is called for all the
558    fixups we generated by the call to fix_new_exp, above.  In the call
559    above we used a reloc code which was the largest legal reloc code
560    plus the operand index.  Here we undo that to recover the operand
561    index.  At this point all symbol values should be fully resolved,
562    and we attempt to completely resolve the reloc.  If we can not do
563    that, we determine the correct reloc code and put it back in the fixup.  */
564
565 /* FIXME: This function handles some of the fixups and bfd_install_relocation
566    handles the rest.  bfd_install_relocation (or some other bfd function)
567    should handle them all.  */
568
569 int
570 gas_cgen_md_apply_fix3 (fixP, valueP, seg)
571      fixS *   fixP;
572      valueT * valueP;
573      segT     seg ATTRIBUTE_UNUSED;
574 {
575   char *where = fixP->fx_frag->fr_literal + fixP->fx_where;
576   valueT value;
577   /* Canonical name, since used a lot.  */
578   CGEN_CPU_DESC cd = gas_cgen_cpu_desc;
579
580   /* FIXME FIXME FIXME: The value we are passed in *valuep includes
581      the symbol values.  Since we are using BFD_ASSEMBLER, if we are
582      doing this relocation the code in write.c is going to call
583      bfd_install_relocation, which is also going to use the symbol
584      value.  That means that if the reloc is fully resolved we want to
585      use *valuep since bfd_install_relocation is not being used.
586      However, if the reloc is not fully resolved we do not want to use
587      *valuep, and must use fx_offset instead.  However, if the reloc
588      is PC relative, we do want to use *valuep since it includes the
589      result of md_pcrel_from.  This is confusing.  */
590
591   if (fixP->fx_addsy == (symbolS *) NULL)
592     {
593       value = *valueP;
594       fixP->fx_done = 1;
595     }
596   else if (fixP->fx_pcrel)
597     value = *valueP;
598   else
599     {
600       value = fixP->fx_offset;
601       if (fixP->fx_subsy != (symbolS *) NULL)
602         {
603           if (S_GET_SEGMENT (fixP->fx_subsy) == absolute_section)
604             value -= S_GET_VALUE (fixP->fx_subsy);
605           else
606             {
607               /* We don't actually support subtracting a symbol.  */
608               as_bad_where (fixP->fx_file, fixP->fx_line,
609                             _("expression too complex"));
610             }
611         }
612     }
613
614   if ((int) fixP->fx_r_type >= (int) BFD_RELOC_UNUSED)
615     {
616       int opindex = (int) fixP->fx_r_type - (int) BFD_RELOC_UNUSED;
617       const CGEN_OPERAND *operand = cgen_operand_lookup_by_num (cd, opindex);
618       const char *errmsg;
619       bfd_reloc_code_real_type reloc_type;
620       CGEN_FIELDS *fields = alloca (CGEN_CPU_SIZEOF_FIELDS (cd));
621       const CGEN_INSN *insn = fixP->fx_cgen.insn;
622
623       /* If the reloc has been fully resolved finish the operand here.  */
624       /* FIXME: This duplicates the capabilities of code in BFD.  */
625       if (fixP->fx_done
626           /* FIXME: If partial_inplace isn't set bfd_install_relocation won't
627              finish the job.  Testing for pcrel is a temporary hack.  */
628           || fixP->fx_pcrel)
629         {
630           CGEN_CPU_SET_FIELDS_BITSIZE (cd) (fields, CGEN_INSN_BITSIZE (insn));
631           CGEN_CPU_SET_VMA_OPERAND (cd) (cd, opindex, fields, (bfd_vma) value);
632
633 #if CGEN_INT_INSN_P
634           {
635             CGEN_INSN_INT insn_value =
636               cgen_get_insn_value (cd, where, CGEN_INSN_BITSIZE (insn));
637
638             /* ??? 0 is passed for `pc'.  */
639             errmsg = CGEN_CPU_INSERT_OPERAND (cd) (cd, opindex, fields,
640                                                    &insn_value, (bfd_vma) 0);
641             cgen_put_insn_value (cd, where, CGEN_INSN_BITSIZE (insn),
642                                  insn_value);
643           }
644 #else
645           /* ??? 0 is passed for `pc'.  */
646           errmsg = CGEN_CPU_INSERT_OPERAND (cd) (cd, opindex, fields, where,
647                                                  (bfd_vma) 0);
648 #endif
649           if (errmsg)
650             as_bad_where (fixP->fx_file, fixP->fx_line, "%s", errmsg);
651         }
652
653       if (fixP->fx_done)
654         return 1;
655
656       /* The operand isn't fully resolved.  Determine a BFD reloc value
657          based on the operand information and leave it to
658          bfd_install_relocation.  Note that this doesn't work when
659          partial_inplace == false.  */
660
661       reloc_type = md_cgen_lookup_reloc (insn, operand, fixP);
662       if (reloc_type != BFD_RELOC_NONE)
663         {
664           fixP->fx_r_type = reloc_type;
665         }
666       else
667         {
668           as_bad_where (fixP->fx_file, fixP->fx_line,
669                         _("unresolved expression that must be resolved"));
670           fixP->fx_done = 1;
671           return 1;
672         }
673     }
674   else if (fixP->fx_done)
675     {
676       /* We're finished with this fixup.  Install it because
677          bfd_install_relocation won't be called to do it.  */
678       switch (fixP->fx_r_type)
679         {
680         case BFD_RELOC_8:
681           md_number_to_chars (where, value, 1);
682           break;
683         case BFD_RELOC_16:
684           md_number_to_chars (where, value, 2);
685           break;
686         case BFD_RELOC_32:
687           md_number_to_chars (where, value, 4);
688           break;
689         case BFD_RELOC_64:
690           md_number_to_chars (where, value, 8);
691           break;
692         default:
693           as_bad_where (fixP->fx_file, fixP->fx_line,
694                         _("internal error: can't install fix for reloc type %d (`%s')"),
695                         fixP->fx_r_type, bfd_get_reloc_code_name (fixP->fx_r_type));
696           break;
697         }
698     }
699   /* else
700      bfd_install_relocation will be called to finish things up.  */
701
702   /* Tuck `value' away for use by tc_gen_reloc.
703      See the comment describing fx_addnumber in write.h.
704      This field is misnamed (or misused :-).  */
705   fixP->fx_addnumber = value;
706
707   return 1;
708 }
709
710 /* Translate internal representation of relocation info to BFD target format.
711
712    FIXME: To what extent can we get all relevant targets to use this?  */
713
714 arelent *
715 gas_cgen_tc_gen_reloc (section, fixP)
716      asection * section ATTRIBUTE_UNUSED;
717      fixS *     fixP;
718 {
719   arelent *reloc;
720
721   reloc = (arelent *) xmalloc (sizeof (arelent));
722
723   reloc->howto = bfd_reloc_type_lookup (stdoutput, fixP->fx_r_type);
724   if (reloc->howto == (reloc_howto_type *) NULL)
725     {
726       as_bad_where (fixP->fx_file, fixP->fx_line,
727                     _("relocation is not supported"));
728       return NULL;
729     }
730
731   assert (!fixP->fx_pcrel == !reloc->howto->pc_relative);
732
733   reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
734   *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixP->fx_addsy);
735
736   /* Use fx_offset for these cases.  */
737   if (fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY
738       || fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT)
739     reloc->addend = fixP->fx_offset;
740   else
741     reloc->addend = fixP->fx_addnumber;
742
743   reloc->address = fixP->fx_frag->fr_address + fixP->fx_where;
744   return reloc;
745 }