Merge from vendor branch GDB:
[dragonfly.git] / contrib / binutils / bfd / coff-sparc.c
1 /* BFD back-end for Sparc COFF files.
2    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1999, 2000, 2001
3    Free Software Foundation, Inc.
4    Written by Cygnus Support.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "libbfd.h"
25 #include "coff/sparc.h"
26 #include "coff/internal.h"
27 #include "libcoff.h"
28
29 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (3)
30
31 #define BADMAG(x) ((x).f_magic != SPARCMAGIC && (x).f_magic != LYNXCOFFMAGIC)
32
33 /* The page size is a guess based on ELF.  */
34 #define COFF_PAGE_SIZE 0x10000
35
36
37 static reloc_howto_type *coff_sparc_reloc_type_lookup
38   PARAMS ((bfd *, bfd_reloc_code_real_type));
39 static void rtype2howto PARAMS ((arelent *, struct internal_reloc *));
40
41 enum reloc_type
42   {
43     R_SPARC_NONE = 0,
44     R_SPARC_8,          R_SPARC_16,             R_SPARC_32,
45     R_SPARC_DISP8,      R_SPARC_DISP16,         R_SPARC_DISP32,
46     R_SPARC_WDISP30,    R_SPARC_WDISP22,
47     R_SPARC_HI22,       R_SPARC_22,
48     R_SPARC_13,         R_SPARC_LO10,
49     R_SPARC_GOT10,      R_SPARC_GOT13,          R_SPARC_GOT22,
50     R_SPARC_PC10,       R_SPARC_PC22,
51     R_SPARC_WPLT30,
52     R_SPARC_COPY,
53     R_SPARC_GLOB_DAT,   R_SPARC_JMP_SLOT,
54     R_SPARC_RELATIVE,
55     R_SPARC_UA32,
56     R_SPARC_max
57   };
58
59 #if 0
60 static const char *const reloc_type_names[] =
61 {
62   "R_SPARC_NONE",
63   "R_SPARC_8",          "R_SPARC_16",           "R_SPARC_32",
64   "R_SPARC_DISP8",      "R_SPARC_DISP16",       "R_SPARC_DISP32",
65   "R_SPARC_WDISP30",    "R_SPARC_WDISP22",
66   "R_SPARC_HI22",       "R_SPARC_22",
67   "R_SPARC_13",         "R_SPARC_LO10",
68   "R_SPARC_GOT10",      "R_SPARC_GOT13",        "R_SPARC_GOT22",
69   "R_SPARC_PC10",       "R_SPARC_PC22",
70   "R_SPARC_WPLT30",
71   "R_SPARC_COPY",
72   "R_SPARC_GLOB_DAT",   "R_SPARC_JMP_SLOT",
73   "R_SPARC_RELATIVE",
74   "R_SPARC_UA32",
75 };
76 #endif
77
78 /* This is stolen pretty directly from elf.c.  */
79 static bfd_reloc_status_type
80 bfd_coff_generic_reloc PARAMS ((bfd *, arelent *, asymbol *, PTR,
81                                 asection *, bfd *, char **));
82
83 static bfd_reloc_status_type
84 bfd_coff_generic_reloc (abfd, reloc_entry, symbol, data, input_section,
85                         output_bfd, error_message)
86      bfd *abfd ATTRIBUTE_UNUSED;
87      arelent *reloc_entry;
88      asymbol *symbol;
89      PTR data ATTRIBUTE_UNUSED;
90      asection *input_section;
91      bfd *output_bfd;
92      char **error_message ATTRIBUTE_UNUSED;
93 {
94   if (output_bfd != (bfd *) NULL
95       && (symbol->flags & BSF_SECTION_SYM) == 0)
96     {
97       reloc_entry->address += input_section->output_offset;
98       return bfd_reloc_ok;
99     }
100
101   return bfd_reloc_continue;
102 }
103
104 static reloc_howto_type coff_sparc_howto_table[] =
105 {
106   HOWTO(R_SPARC_NONE,    0,0, 0,false,0,complain_overflow_dont,    bfd_coff_generic_reloc,"R_SPARC_NONE",    false,0,0x00000000,true),
107   HOWTO(R_SPARC_8,       0,0, 8,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_8",       false,0,0x000000ff,true),
108   HOWTO(R_SPARC_16,      0,1,16,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_16",      false,0,0x0000ffff,true),
109   HOWTO(R_SPARC_32,      0,2,32,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_32",      false,0,0xffffffff,true),
110   HOWTO(R_SPARC_DISP8,   0,0, 8,true, 0,complain_overflow_signed,  bfd_coff_generic_reloc,"R_SPARC_DISP8",   false,0,0x000000ff,true),
111   HOWTO(R_SPARC_DISP16,  0,1,16,true, 0,complain_overflow_signed,  bfd_coff_generic_reloc,"R_SPARC_DISP16",  false,0,0x0000ffff,true),
112   HOWTO(R_SPARC_DISP32,  0,2,32,true, 0,complain_overflow_signed,  bfd_coff_generic_reloc,"R_SPARC_DISP32",  false,0,0x00ffffff,true),
113   HOWTO(R_SPARC_WDISP30, 2,2,30,true, 0,complain_overflow_signed,  bfd_coff_generic_reloc,"R_SPARC_WDISP30", false,0,0x3fffffff,true),
114   HOWTO(R_SPARC_WDISP22, 2,2,22,true, 0,complain_overflow_signed,  bfd_coff_generic_reloc,"R_SPARC_WDISP22", false,0,0x003fffff,true),
115   HOWTO(R_SPARC_HI22,   10,2,22,false,0,complain_overflow_dont,    bfd_coff_generic_reloc,"R_SPARC_HI22",    false,0,0x003fffff,true),
116   HOWTO(R_SPARC_22,      0,2,22,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_22",      false,0,0x003fffff,true),
117   HOWTO(R_SPARC_13,      0,2,13,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_13",      false,0,0x00001fff,true),
118   HOWTO(R_SPARC_LO10,    0,2,10,false,0,complain_overflow_dont,    bfd_coff_generic_reloc,"R_SPARC_LO10",    false,0,0x000003ff,true),
119   HOWTO(R_SPARC_GOT10,   0,2,10,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_GOT10",   false,0,0x000003ff,true),
120   HOWTO(R_SPARC_GOT13,   0,2,13,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_GOT13",   false,0,0x00001fff,true),
121   HOWTO(R_SPARC_GOT22,  10,2,22,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_GOT22",   false,0,0x003fffff,true),
122   HOWTO(R_SPARC_PC10,    0,2,10,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_PC10",    false,0,0x000003ff,true),
123   HOWTO(R_SPARC_PC22,    0,2,22,false,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_PC22",    false,0,0x003fffff,true),
124   HOWTO(R_SPARC_WPLT30,  0,0,00,false,0,complain_overflow_dont,    bfd_coff_generic_reloc,"R_SPARC_WPLT30",  false,0,0x00000000,true),
125   HOWTO(R_SPARC_COPY,    0,0,00,false,0,complain_overflow_dont,    bfd_coff_generic_reloc,"R_SPARC_COPY",    false,0,0x00000000,true),
126   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),
127   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),
128   HOWTO(R_SPARC_RELATIVE,0,0,00,false,0,complain_overflow_dont,    bfd_coff_generic_reloc,"R_SPARC_RELATIVE",false,0,0x00000000,true),
129   HOWTO(R_SPARC_UA32,    0,0,00,false,0,complain_overflow_dont,    bfd_coff_generic_reloc,"R_SPARC_UA32",    false,0,0x00000000,true),
130 };
131
132 struct coff_reloc_map {
133   bfd_reloc_code_real_type bfd_reloc_val;
134   unsigned char coff_reloc_val;
135 };
136
137 static const struct coff_reloc_map sparc_reloc_map[] =
138 {
139   { BFD_RELOC_NONE, R_SPARC_NONE, },
140   { BFD_RELOC_16, R_SPARC_16, },
141   { BFD_RELOC_8, R_SPARC_8 },
142   { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
143   { BFD_RELOC_CTOR, R_SPARC_32 }, /* @@ Assumes 32 bits.  */
144   { BFD_RELOC_32, R_SPARC_32 },
145   { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
146   { BFD_RELOC_HI22, R_SPARC_HI22 },
147   { BFD_RELOC_LO10, R_SPARC_LO10, },
148   { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
149   { BFD_RELOC_SPARC22, R_SPARC_22 },
150   { BFD_RELOC_SPARC13, R_SPARC_13 },
151   { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
152   { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
153   { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
154   { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
155   { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
156   { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
157   { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
158   { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
159   { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
160   { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
161   { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
162   /*  { BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */
163 };
164
165 static reloc_howto_type *
166 coff_sparc_reloc_type_lookup (abfd, code)
167      bfd *abfd ATTRIBUTE_UNUSED;
168      bfd_reloc_code_real_type code;
169 {
170   unsigned int i;
171   for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct coff_reloc_map); i++)
172     {
173       if (sparc_reloc_map[i].bfd_reloc_val == code)
174         return &coff_sparc_howto_table[(int) sparc_reloc_map[i].coff_reloc_val];
175     }
176   return 0;
177 }
178 #define coff_bfd_reloc_type_lookup      coff_sparc_reloc_type_lookup
179
180 static void
181 rtype2howto (cache_ptr, dst)
182      arelent *cache_ptr;
183      struct internal_reloc *dst;
184 {
185   BFD_ASSERT (dst->r_type < (unsigned int) R_SPARC_max);
186   cache_ptr->howto = &coff_sparc_howto_table[dst->r_type];
187 }
188
189 #define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
190
191 #define SWAP_IN_RELOC_OFFSET    H_GET_32
192 #define SWAP_OUT_RELOC_OFFSET   H_PUT_32
193 #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
194   cache_ptr->addend = reloc.r_offset;
195
196 /* Clear the r_spare field in relocs.  */
197 #define SWAP_OUT_RELOC_EXTRA(abfd,src,dst) \
198   do { \
199        dst->r_spare[0] = 0; \
200        dst->r_spare[1] = 0; \
201      } while (0)
202
203 #define __A_MAGIC_SET__
204
205 /* Enable Sparc-specific hacks in coffcode.h.  */
206
207 #define COFF_SPARC
208
209 #include "coffcode.h"
210
211 #ifndef TARGET_SYM
212 #define TARGET_SYM sparccoff_vec
213 #endif
214
215 #ifndef TARGET_NAME
216 #define TARGET_NAME "coff-sparc"
217 #endif
218
219 CREATE_BIG_COFF_TARGET_VEC (TARGET_SYM, TARGET_NAME, D_PAGED, 0, '_', NULL)