Initial revision
[binutils.git] / bfd / coff-sparc.c
blobbc2cf153843e6b645a2567bd8b36b5183b316a0e
1 /* BFD back-end for Sparc COFF files.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program 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 of the License, or
10 (at your option) any later version.
12 This program 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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "coff/sparc.h"
25 #include "coff/internal.h"
26 #include "libcoff.h"
28 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
30 #define BADMAG(x) ((x).f_magic != SPARCMAGIC && (x).f_magic != LYNXCOFFMAGIC)
32 /* The page size is a guess based on ELF. */
33 #define COFF_PAGE_SIZE 0x10000
35 enum reloc_type
37 R_SPARC_NONE = 0,
38 R_SPARC_8, R_SPARC_16, R_SPARC_32,
39 R_SPARC_DISP8, R_SPARC_DISP16, R_SPARC_DISP32,
40 R_SPARC_WDISP30, R_SPARC_WDISP22,
41 R_SPARC_HI22, R_SPARC_22,
42 R_SPARC_13, R_SPARC_LO10,
43 R_SPARC_GOT10, R_SPARC_GOT13, R_SPARC_GOT22,
44 R_SPARC_PC10, R_SPARC_PC22,
45 R_SPARC_WPLT30,
46 R_SPARC_COPY,
47 R_SPARC_GLOB_DAT, R_SPARC_JMP_SLOT,
48 R_SPARC_RELATIVE,
49 R_SPARC_UA32,
50 R_SPARC_max
53 #if 0
54 static CONST char *CONST reloc_type_names[] =
56 "R_SPARC_NONE",
57 "R_SPARC_8", "R_SPARC_16", "R_SPARC_32",
58 "R_SPARC_DISP8", "R_SPARC_DISP16", "R_SPARC_DISP32",
59 "R_SPARC_WDISP30", "R_SPARC_WDISP22",
60 "R_SPARC_HI22", "R_SPARC_22",
61 "R_SPARC_13", "R_SPARC_LO10",
62 "R_SPARC_GOT10", "R_SPARC_GOT13", "R_SPARC_GOT22",
63 "R_SPARC_PC10", "R_SPARC_PC22",
64 "R_SPARC_WPLT30",
65 "R_SPARC_COPY",
66 "R_SPARC_GLOB_DAT", "R_SPARC_JMP_SLOT",
67 "R_SPARC_RELATIVE",
68 "R_SPARC_UA32",
70 #endif
72 /* This is stolen pretty directly from elf.c. */
73 static bfd_reloc_status_type
74 bfd_coff_generic_reloc PARAMS ((bfd *, arelent *, asymbol *, PTR,
75 asection *, bfd *, char **));
77 static bfd_reloc_status_type
78 bfd_coff_generic_reloc (abfd, reloc_entry, symbol, data, input_section,
79 output_bfd, error_message)
80 bfd *abfd;
81 arelent *reloc_entry;
82 asymbol *symbol;
83 PTR data;
84 asection *input_section;
85 bfd *output_bfd;
86 char **error_message;
88 if (output_bfd != (bfd *) NULL
89 && (symbol->flags & BSF_SECTION_SYM) == 0)
91 reloc_entry->address += input_section->output_offset;
92 return bfd_reloc_ok;
95 return bfd_reloc_continue;
98 static reloc_howto_type coff_sparc_howto_table[] =
100 HOWTO(R_SPARC_NONE, 0,0, 0,false,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_NONE", false,0,0x00000000,true),
101 HOWTO(R_SPARC_8, 0,0, 8,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_8", false,0,0x000000ff,true),
102 HOWTO(R_SPARC_16, 0,1,16,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_16", false,0,0x0000ffff,true),
103 HOWTO(R_SPARC_32, 0,2,32,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_32", false,0,0xffffffff,true),
104 HOWTO(R_SPARC_DISP8, 0,0, 8,true, 0,complain_overflow_signed, bfd_coff_generic_reloc,"R_SPARC_DISP8", false,0,0x000000ff,true),
105 HOWTO(R_SPARC_DISP16, 0,1,16,true, 0,complain_overflow_signed, bfd_coff_generic_reloc,"R_SPARC_DISP16", false,0,0x0000ffff,true),
106 HOWTO(R_SPARC_DISP32, 0,2,32,true, 0,complain_overflow_signed, bfd_coff_generic_reloc,"R_SPARC_DISP32", false,0,0x00ffffff,true),
107 HOWTO(R_SPARC_WDISP30, 2,2,30,true, 0,complain_overflow_signed, bfd_coff_generic_reloc,"R_SPARC_WDISP30", false,0,0x3fffffff,true),
108 HOWTO(R_SPARC_WDISP22, 2,2,22,true, 0,complain_overflow_signed, bfd_coff_generic_reloc,"R_SPARC_WDISP22", false,0,0x003fffff,true),
109 HOWTO(R_SPARC_HI22, 10,2,22,false,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_HI22", false,0,0x003fffff,true),
110 HOWTO(R_SPARC_22, 0,2,22,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_22", false,0,0x003fffff,true),
111 HOWTO(R_SPARC_13, 0,2,13,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_13", false,0,0x00001fff,true),
112 HOWTO(R_SPARC_LO10, 0,2,10,false,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_LO10", false,0,0x000003ff,true),
113 HOWTO(R_SPARC_GOT10, 0,2,10,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_GOT10", false,0,0x000003ff,true),
114 HOWTO(R_SPARC_GOT13, 0,2,13,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_GOT13", false,0,0x00001fff,true),
115 HOWTO(R_SPARC_GOT22, 10,2,22,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_GOT22", false,0,0x003fffff,true),
116 HOWTO(R_SPARC_PC10, 0,2,10,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_PC10", false,0,0x000003ff,true),
117 HOWTO(R_SPARC_PC22, 0,2,22,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_PC22", false,0,0x003fffff,true),
118 HOWTO(R_SPARC_WPLT30, 0,0,00,false,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_WPLT30", false,0,0x00000000,true),
119 HOWTO(R_SPARC_COPY, 0,0,00,false,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_COPY", false,0,0x00000000,true),
120 HOWTO(R_SPARC_GLOB_DAT,0,0,00,false,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_GLOB_DAT",false,0,0x00000000,true),
121 HOWTO(R_SPARC_JMP_SLOT,0,0,00,false,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_JMP_SLOT",false,0,0x00000000,true),
122 HOWTO(R_SPARC_RELATIVE,0,0,00,false,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_RELATIVE",false,0,0x00000000,true),
123 HOWTO(R_SPARC_UA32, 0,0,00,false,0,complain_overflow_dont, bfd_coff_generic_reloc,"R_SPARC_UA32", false,0,0x00000000,true),
126 struct coff_reloc_map {
127 bfd_reloc_code_real_type bfd_reloc_val;
128 unsigned char coff_reloc_val;
131 static CONST struct coff_reloc_map sparc_reloc_map[] =
133 { BFD_RELOC_NONE, R_SPARC_NONE, },
134 { BFD_RELOC_16, R_SPARC_16, },
135 { BFD_RELOC_8, R_SPARC_8 },
136 { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
137 { BFD_RELOC_CTOR, R_SPARC_32 }, /* @@ Assumes 32 bits. */
138 { BFD_RELOC_32, R_SPARC_32 },
139 { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
140 { BFD_RELOC_HI22, R_SPARC_HI22 },
141 { BFD_RELOC_LO10, R_SPARC_LO10, },
142 { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
143 { BFD_RELOC_SPARC22, R_SPARC_22 },
144 { BFD_RELOC_SPARC13, R_SPARC_13 },
145 { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
146 { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
147 { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
148 { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
149 { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
150 { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
151 { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
152 { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
153 { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
154 { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
155 { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
156 /* { BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */
159 static reloc_howto_type *
160 coff_sparc_reloc_type_lookup (abfd, code)
161 bfd *abfd;
162 bfd_reloc_code_real_type code;
164 unsigned int i;
165 for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct coff_reloc_map); i++)
167 if (sparc_reloc_map[i].bfd_reloc_val == code)
168 return &coff_sparc_howto_table[(int) sparc_reloc_map[i].coff_reloc_val];
170 return 0;
172 #define coff_bfd_reloc_type_lookup coff_sparc_reloc_type_lookup
174 static void
175 rtype2howto (cache_ptr, dst)
176 arelent *cache_ptr;
177 struct internal_reloc *dst;
179 BFD_ASSERT (dst->r_type < (unsigned int) R_SPARC_max);
180 cache_ptr->howto = &coff_sparc_howto_table[dst->r_type];
183 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
185 #define SWAP_IN_RELOC_OFFSET bfd_h_get_32
186 #define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
187 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
188 cache_ptr->addend = reloc.r_offset;
190 /* Clear the r_spare field in relocs. */
191 #define SWAP_OUT_RELOC_EXTRA(abfd,src,dst) \
192 do { \
193 dst->r_spare[0] = 0; \
194 dst->r_spare[1] = 0; \
195 } while (0)
197 #define __A_MAGIC_SET__
199 /* Enable Sparc-specific hacks in coffcode.h. */
201 #define COFF_SPARC
203 #include "coffcode.h"
205 const bfd_target
206 #ifdef TARGET_SYM
207 TARGET_SYM =
208 #else
209 sparccoff_vec =
210 #endif
212 #ifdef TARGET_NAME
213 TARGET_NAME,
214 #else
215 "coff-sparc", /* name */
216 #endif
217 bfd_target_coff_flavour,
218 BFD_ENDIAN_BIG, /* data byte order is big */
219 BFD_ENDIAN_BIG, /* header byte order is big */
221 (HAS_RELOC | EXEC_P | /* object flags */
222 HAS_LINENO | HAS_DEBUG |
223 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
225 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
226 '_', /* leading underscore */
227 '/', /* ar_pad_char */
228 15, /* ar_max_namelen */
230 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
231 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
232 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
233 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
234 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
235 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
237 /* Note that we allow an object file to be treated as a core file as well. */
238 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
239 bfd_generic_archive_p, coff_object_p},
240 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
241 bfd_false},
242 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
243 _bfd_write_archive_contents, bfd_false},
245 BFD_JUMP_TABLE_GENERIC (coff),
246 BFD_JUMP_TABLE_COPY (coff),
247 BFD_JUMP_TABLE_CORE (_bfd_nocore),
248 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
249 BFD_JUMP_TABLE_SYMBOLS (coff),
250 BFD_JUMP_TABLE_RELOCS (coff),
251 BFD_JUMP_TABLE_WRITE (coff),
252 BFD_JUMP_TABLE_LINK (coff),
253 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
255 COFF_SWAP_TABLE,