1/* BFD back-end for Sparc COFF files.
2   Copyright (C) 1990-2017 Free Software Foundation, Inc.
3   Written by Cygnus Support.
4
5   This file is part of BFD, the Binary File Descriptor library.
6
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 3 of the License, or
10   (at your option) any later version.
11
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.
16
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., 51 Franklin Street - Fifth Floor, Boston,
20   MA 02110-1301, USA.  */
21
22#include "sysdep.h"
23#include "bfd.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
36enum reloc_type
37  {
38    R_SPARC_NONE = 0,
39    R_SPARC_8,		R_SPARC_16,		R_SPARC_32,
40    R_SPARC_DISP8,	R_SPARC_DISP16,		R_SPARC_DISP32,
41    R_SPARC_WDISP30,	R_SPARC_WDISP22,
42    R_SPARC_HI22,	R_SPARC_22,
43    R_SPARC_13,		R_SPARC_LO10,
44    R_SPARC_GOT10,	R_SPARC_GOT13,		R_SPARC_GOT22,
45    R_SPARC_PC10,	R_SPARC_PC22,
46    R_SPARC_WPLT30,
47    R_SPARC_COPY,
48    R_SPARC_GLOB_DAT,	R_SPARC_JMP_SLOT,
49    R_SPARC_RELATIVE,
50    R_SPARC_UA32,
51    R_SPARC_max
52  };
53
54/* This is stolen pretty directly from elf.c.  */
55
56static bfd_reloc_status_type
57bfd_coff_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED,
58			arelent *reloc_entry,
59			asymbol *symbol,
60			void * data ATTRIBUTE_UNUSED,
61			asection *input_section,
62			bfd *output_bfd,
63			char **error_message ATTRIBUTE_UNUSED)
64{
65  if (output_bfd != (bfd *) NULL
66      && (symbol->flags & BSF_SECTION_SYM) == 0)
67    {
68      reloc_entry->address += input_section->output_offset;
69      return bfd_reloc_ok;
70    }
71
72  return bfd_reloc_continue;
73}
74
75static reloc_howto_type coff_sparc_howto_table[] =
76{
77  HOWTO(R_SPARC_NONE,    0,3, 0,FALSE,0,complain_overflow_dont,    bfd_coff_generic_reloc,"R_SPARC_NONE",    FALSE,0,0x00000000,TRUE),
78  HOWTO(R_SPARC_8,       0,0, 8,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_8",       FALSE,0,0x000000ff,TRUE),
79  HOWTO(R_SPARC_16,      0,1,16,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_16",      FALSE,0,0x0000ffff,TRUE),
80  HOWTO(R_SPARC_32,      0,2,32,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_32",      FALSE,0,0xffffffff,TRUE),
81  HOWTO(R_SPARC_DISP8,   0,0, 8,TRUE, 0,complain_overflow_signed,  bfd_coff_generic_reloc,"R_SPARC_DISP8",   FALSE,0,0x000000ff,TRUE),
82  HOWTO(R_SPARC_DISP16,  0,1,16,TRUE, 0,complain_overflow_signed,  bfd_coff_generic_reloc,"R_SPARC_DISP16",  FALSE,0,0x0000ffff,TRUE),
83  HOWTO(R_SPARC_DISP32,  0,2,32,TRUE, 0,complain_overflow_signed,  bfd_coff_generic_reloc,"R_SPARC_DISP32",  FALSE,0,0x00ffffff,TRUE),
84  HOWTO(R_SPARC_WDISP30, 2,2,30,TRUE, 0,complain_overflow_signed,  bfd_coff_generic_reloc,"R_SPARC_WDISP30", FALSE,0,0x3fffffff,TRUE),
85  HOWTO(R_SPARC_WDISP22, 2,2,22,TRUE, 0,complain_overflow_signed,  bfd_coff_generic_reloc,"R_SPARC_WDISP22", FALSE,0,0x003fffff,TRUE),
86  HOWTO(R_SPARC_HI22,   10,2,22,FALSE,0,complain_overflow_dont,    bfd_coff_generic_reloc,"R_SPARC_HI22",    FALSE,0,0x003fffff,TRUE),
87  HOWTO(R_SPARC_22,      0,2,22,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_22",      FALSE,0,0x003fffff,TRUE),
88  HOWTO(R_SPARC_13,      0,2,13,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_13",      FALSE,0,0x00001fff,TRUE),
89  HOWTO(R_SPARC_LO10,    0,2,10,FALSE,0,complain_overflow_dont,    bfd_coff_generic_reloc,"R_SPARC_LO10",    FALSE,0,0x000003ff,TRUE),
90  HOWTO(R_SPARC_GOT10,   0,2,10,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_GOT10",   FALSE,0,0x000003ff,TRUE),
91  HOWTO(R_SPARC_GOT13,   0,2,13,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_GOT13",   FALSE,0,0x00001fff,TRUE),
92  HOWTO(R_SPARC_GOT22,  10,2,22,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_GOT22",   FALSE,0,0x003fffff,TRUE),
93  HOWTO(R_SPARC_PC10,    0,2,10,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_PC10",    FALSE,0,0x000003ff,TRUE),
94  HOWTO(R_SPARC_PC22,    0,2,22,FALSE,0,complain_overflow_bitfield,bfd_coff_generic_reloc,"R_SPARC_PC22",    FALSE,0,0x003fffff,TRUE),
95  HOWTO(R_SPARC_WPLT30,  0,0,00,FALSE,0,complain_overflow_dont,    bfd_coff_generic_reloc,"R_SPARC_WPLT30",  FALSE,0,0x00000000,TRUE),
96  HOWTO(R_SPARC_COPY,    0,0,00,FALSE,0,complain_overflow_dont,    bfd_coff_generic_reloc,"R_SPARC_COPY",    FALSE,0,0x00000000,TRUE),
97  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),
98  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),
99  HOWTO(R_SPARC_RELATIVE,0,0,00,FALSE,0,complain_overflow_dont,    bfd_coff_generic_reloc,"R_SPARC_RELATIVE",FALSE,0,0x00000000,TRUE),
100  HOWTO(R_SPARC_UA32,    0,0,00,FALSE,0,complain_overflow_dont,    bfd_coff_generic_reloc,"R_SPARC_UA32",    FALSE,0,0x00000000,TRUE),
101};
102
103struct coff_reloc_map
104{
105  bfd_reloc_code_real_type bfd_reloc_val;
106  unsigned char coff_reloc_val;
107};
108
109static const struct coff_reloc_map sparc_reloc_map[] =
110{
111  { BFD_RELOC_NONE, R_SPARC_NONE, },
112  { BFD_RELOC_16, R_SPARC_16, },
113  { BFD_RELOC_8, R_SPARC_8 },
114  { BFD_RELOC_8_PCREL, R_SPARC_DISP8 },
115  { BFD_RELOC_CTOR, R_SPARC_32 }, /* @@ Assumes 32 bits.  */
116  { BFD_RELOC_32, R_SPARC_32 },
117  { BFD_RELOC_32_PCREL, R_SPARC_DISP32 },
118  { BFD_RELOC_HI22, R_SPARC_HI22 },
119  { BFD_RELOC_LO10, R_SPARC_LO10, },
120  { BFD_RELOC_32_PCREL_S2, R_SPARC_WDISP30 },
121  { BFD_RELOC_SPARC22, R_SPARC_22 },
122  { BFD_RELOC_SPARC13, R_SPARC_13 },
123  { BFD_RELOC_SPARC_GOT10, R_SPARC_GOT10 },
124  { BFD_RELOC_SPARC_GOT13, R_SPARC_GOT13 },
125  { BFD_RELOC_SPARC_GOT22, R_SPARC_GOT22 },
126  { BFD_RELOC_SPARC_PC10, R_SPARC_PC10 },
127  { BFD_RELOC_SPARC_PC22, R_SPARC_PC22 },
128  { BFD_RELOC_SPARC_WPLT30, R_SPARC_WPLT30 },
129  { BFD_RELOC_SPARC_COPY, R_SPARC_COPY },
130  { BFD_RELOC_SPARC_GLOB_DAT, R_SPARC_GLOB_DAT },
131  { BFD_RELOC_SPARC_JMP_SLOT, R_SPARC_JMP_SLOT },
132  { BFD_RELOC_SPARC_RELATIVE, R_SPARC_RELATIVE },
133  { BFD_RELOC_SPARC_WDISP22, R_SPARC_WDISP22 },
134  /*  { BFD_RELOC_SPARC_UA32, R_SPARC_UA32 }, not used?? */
135};
136
137static reloc_howto_type *
138coff_sparc_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
139			      bfd_reloc_code_real_type code)
140{
141  unsigned int i;
142  for (i = 0; i < sizeof (sparc_reloc_map) / sizeof (struct coff_reloc_map); i++)
143    {
144      if (sparc_reloc_map[i].bfd_reloc_val == code)
145	return &coff_sparc_howto_table[(int) sparc_reloc_map[i].coff_reloc_val];
146    }
147  return 0;
148}
149#define coff_bfd_reloc_type_lookup	coff_sparc_reloc_type_lookup
150
151static reloc_howto_type *
152coff_sparc_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
153			      const char *r_name)
154{
155  unsigned int i;
156
157  for (i = 0;
158       i < (sizeof (coff_sparc_howto_table)
159	    / sizeof (coff_sparc_howto_table[0]));
160       i++)
161    if (coff_sparc_howto_table[i].name != NULL
162	&& strcasecmp (coff_sparc_howto_table[i].name, r_name) == 0)
163      return &coff_sparc_howto_table[i];
164
165  return NULL;
166}
167#define coff_bfd_reloc_name_lookup coff_sparc_reloc_name_lookup
168
169static void
170rtype2howto (arelent *cache_ptr, struct internal_reloc *dst)
171{
172  BFD_ASSERT (dst->r_type < (unsigned int) R_SPARC_max);
173  cache_ptr->howto = &coff_sparc_howto_table[dst->r_type];
174}
175
176#define RTYPE2HOWTO(internal, relocentry) rtype2howto(internal,relocentry)
177
178#define SWAP_IN_RELOC_OFFSET	H_GET_32
179#define SWAP_OUT_RELOC_OFFSET	H_PUT_32
180#define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
181  cache_ptr->addend = reloc.r_offset;
182
183/* Clear the r_spare field in relocs.  */
184#define SWAP_OUT_RELOC_EXTRA(abfd,src,dst) \
185  do { \
186       dst->r_spare[0] = 0; \
187       dst->r_spare[1] = 0; \
188     } while (0)
189
190#define __A_MAGIC_SET__
191
192/* Enable Sparc-specific hacks in coffcode.h.  */
193
194#define COFF_SPARC
195
196#define bfd_pe_print_pdata	NULL
197
198#include "coffcode.h"
199
200#ifndef TARGET_SYM
201#define TARGET_SYM sparc_coff_vec
202#endif
203
204#ifndef TARGET_NAME
205#define TARGET_NAME "coff-sparc"
206#endif
207
208CREATE_BIG_COFF_TARGET_VEC (TARGET_SYM, TARGET_NAME, D_PAGED, 0, '_', NULL, COFF_SWAP_TABLE)
209