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