1/* ARC-specific support for 32-bit ELF
2   Copyright 1994, 1995, 1997, 1999, 2001, 2002, 2005
3   Free Software Foundation, Inc.
4   Contributed by Doug Evans (dje@cygnus.com).
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., 51 Franklin Street - Fifth Floor, Boston,
21   MA 02110-1301, USA.  */
22
23#include "bfd.h"
24#include "sysdep.h"
25#include "libbfd.h"
26#include "elf-bfd.h"
27#include "elf/arc.h"
28#include "libiberty.h"
29
30/* Try to minimize the amount of space occupied by relocation tables
31   on the ROM (not that the ROM won't be swamped by other ELF overhead).  */
32
33#define USE_REL	1
34
35static bfd_reloc_status_type
36arc_elf_b22_pcrel (bfd * abfd,
37		   arelent * reloc_entry,
38		   asymbol * symbol,
39		   void * data,
40		   asection * input_section,
41		   bfd * output_bfd,
42		   char ** error_message)
43{
44  /* If linking, back up the final symbol address by the address of the
45     reloc.  This cannot be accomplished by setting the pcrel_offset
46     field to TRUE, as bfd_install_relocation will detect this and refuse
47     to install the offset in the first place, but bfd_perform_relocation
48     will still insist on removing it.  */
49  if (output_bfd == NULL)
50    reloc_entry->addend -= reloc_entry->address;
51
52  /* Fall through to the default elf reloc handler.  */
53  return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
54				input_section, output_bfd, error_message);
55}
56
57static reloc_howto_type elf_arc_howto_table[] =
58{
59  /* This reloc does nothing.  */
60  HOWTO (R_ARC_NONE,		/* Type.  */
61	 0,			/* Rightshift.  */
62	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
63	 32,			/* Bitsize.  */
64	 FALSE,			/* PC_relative.  */
65	 0,			/* Bitpos.  */
66	 complain_overflow_bitfield, /* Complain_on_overflow.  */
67	 bfd_elf_generic_reloc,	/* Special_function.  */
68	 "R_ARC_NONE",		/* Name.  */
69	 TRUE,			/* Partial_inplace.  */
70	 0,			/* Src_mask.  */
71	 0,			/* Dst_mask.  */
72	 FALSE),		/* PCrel_offset.  */
73
74  /* A standard 32 bit relocation.  */
75  HOWTO (R_ARC_32,		/* Type.  */
76	 0,			/* Rightshift.  */
77	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
78	 32,			/* Bitsize.  */
79	 FALSE,			/* PC_relative.  */
80	 0,			/* Bitpos.  */
81	 complain_overflow_bitfield, /* Complain_on_overflow.  */
82	 bfd_elf_generic_reloc,	/* Special_function.  */
83	 "R_ARC_32",		/* Name.  */
84	 TRUE,			/* Partial_inplace.  */
85	 0xffffffff,		/* Src_mask.  */
86	 0xffffffff,		/* Dst_mask.  */
87	 FALSE),		/* PCrel_offset.  */
88
89  /* A 26 bit absolute branch, right shifted by 2.  */
90  HOWTO (R_ARC_B26,		/* Type.  */
91	 2,			/* Rightshift.  */
92	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
93	 26,			/* Bitsize.  */
94	 FALSE,			/* PC_relative.  */
95	 0,			/* Bitpos.  */
96	 complain_overflow_bitfield, /* Complain_on_overflow.  */
97	 bfd_elf_generic_reloc,	/* Special_function.  */
98	 "R_ARC_B26",		/* Name.  */
99	 TRUE,			/* Partial_inplace.  */
100	 0x00ffffff,		/* Src_mask.  */
101	 0x00ffffff,		/* Dst_mask.  */
102	 FALSE),		/* PCrel_offset.  */
103
104  /* A relative 22 bit branch; bits 21-2 are stored in bits 26-7.  */
105  HOWTO (R_ARC_B22_PCREL,	/* Type.  */
106	 2,			/* Rightshift.  */
107	 2,			/* Size (0 = byte, 1 = short, 2 = long).  */
108	 22,			/* Bitsize.  */
109	 TRUE,			/* PC_relative.  */
110	 7,			/* Bitpos.  */
111	 complain_overflow_signed, /* Complain_on_overflow.  */
112	 arc_elf_b22_pcrel,	/* Special_function.  */
113	 "R_ARC_B22_PCREL",	/* Name.  */
114	 TRUE,			/* Partial_inplace.  */
115	 0x07ffff80,		/* Src_mask.  */
116	 0x07ffff80,		/* Dst_mask.  */
117	 FALSE),		/* PCrel_offset.  */
118};
119
120/* Map BFD reloc types to ARC ELF reloc types.  */
121
122struct arc_reloc_map
123{
124  bfd_reloc_code_real_type bfd_reloc_val;
125  unsigned char elf_reloc_val;
126};
127
128static const struct arc_reloc_map arc_reloc_map[] =
129{
130  { BFD_RELOC_NONE, R_ARC_NONE, },
131  { BFD_RELOC_32, R_ARC_32 },
132  { BFD_RELOC_CTOR, R_ARC_32 },
133  { BFD_RELOC_ARC_B26, R_ARC_B26 },
134  { BFD_RELOC_ARC_B22_PCREL, R_ARC_B22_PCREL },
135};
136
137static reloc_howto_type *
138bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
139				 bfd_reloc_code_real_type code)
140{
141  unsigned int i;
142
143  for (i = ARRAY_SIZE (arc_reloc_map); i--;)
144    if (arc_reloc_map[i].bfd_reloc_val == code)
145      return elf_arc_howto_table + arc_reloc_map[i].elf_reloc_val;
146
147  return NULL;
148}
149
150/* Set the howto pointer for an ARC ELF reloc.  */
151
152static void
153arc_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
154		       arelent *cache_ptr,
155		       Elf_Internal_Rela *dst)
156{
157  unsigned int r_type;
158
159  r_type = ELF32_R_TYPE (dst->r_info);
160  BFD_ASSERT (r_type < (unsigned int) R_ARC_max);
161  cache_ptr->howto = &elf_arc_howto_table[r_type];
162}
163
164/* Set the right machine number for an ARC ELF file.  */
165
166static bfd_boolean
167arc_elf_object_p (bfd *abfd)
168{
169  unsigned int mach = bfd_mach_arc_6;
170
171  if (elf_elfheader(abfd)->e_machine == EM_ARC)
172    {
173      unsigned long arch = elf_elfheader (abfd)->e_flags & EF_ARC_MACH;
174
175      switch (arch)
176	{
177	case E_ARC_MACH_ARC5:
178	  mach = bfd_mach_arc_5;
179	  break;
180	default:
181	case E_ARC_MACH_ARC6:
182	  mach = bfd_mach_arc_6;
183	  break;
184	case E_ARC_MACH_ARC7:
185	  mach = bfd_mach_arc_7;
186	  break;
187	case E_ARC_MACH_ARC8:
188	  mach = bfd_mach_arc_8;
189	  break;
190	}
191    }
192  return bfd_default_set_arch_mach (abfd, bfd_arch_arc, mach);
193}
194
195/* The final processing done just before writing out an ARC ELF object file.
196   This gets the ARC architecture right based on the machine number.  */
197
198static void
199arc_elf_final_write_processing (bfd *abfd,
200				bfd_boolean linker ATTRIBUTE_UNUSED)
201{
202  unsigned long val;
203
204  switch (bfd_get_mach (abfd))
205    {
206    case bfd_mach_arc_5:
207      val = E_ARC_MACH_ARC5;
208      break;
209    default:
210    case bfd_mach_arc_6:
211      val = E_ARC_MACH_ARC6;
212      break;
213    case bfd_mach_arc_7:
214      val = E_ARC_MACH_ARC7;
215      break;
216    case bfd_mach_arc_8:
217      val = E_ARC_MACH_ARC8;
218      break;
219    }
220  elf_elfheader (abfd)->e_flags &=~ EF_ARC_MACH;
221  elf_elfheader (abfd)->e_flags |= val;
222}
223
224#define TARGET_LITTLE_SYM   bfd_elf32_littlearc_vec
225#define TARGET_LITTLE_NAME  "elf32-littlearc"
226#define TARGET_BIG_SYM      bfd_elf32_bigarc_vec
227#define TARGET_BIG_NAME	    "elf32-bigarc"
228#define ELF_ARCH            bfd_arch_arc
229#define ELF_MACHINE_CODE    EM_ARC
230#define ELF_MAXPAGESIZE     0x1000
231
232#define elf_info_to_howto                   0
233#define elf_info_to_howto_rel               arc_info_to_howto_rel
234#define elf_backend_object_p                arc_elf_object_p
235#define elf_backend_final_write_processing  arc_elf_final_write_processing
236
237#include "elf32-target.h"
238