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