]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-x86-64.c
2000-12-07 Elena Zannoni <ezannoni@kwikemart.cygnus.com>
[thirdparty/binutils-gdb.git] / bfd / elf64-x86-64.c
CommitLineData
8d88c4ca
NC
1/* X86-64 specific support for 64-bit ELF
2 Copyright 2000 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka <jh@suse.cz>.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21#include "bfd.h"
22#include "sysdep.h"
23#include "libbfd.h"
24#include "elf-bfd.h"
25
26#include "elf/x86-64.h"
27
28/* We use only the RELA entries. */
29#define USE_RELA
30
31/* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
32#define MINUS_ONE (~ (bfd_vma) 0)
33
34/* The relocation "howto" table. Order of fields:
35 type, size, bitsize, pc_relative, complain_on_overflow, special_function,
36 name, partial_inplace, src_mask, dst_pack, pcrel_offset */
37static reloc_howto_type x86_64_elf_howto_table[] =
38{
39 HOWTO(R_X86_64_NONE, 0,0, 0,false,0,complain_overflow_dont, 0, "R_X86_64_NONE", false,0x00000000,0x00000000,false),
40 HOWTO(R_X86_64_64, 0,4,64,false,0,complain_overflow_bitfield,0, "R_X86_64_64", false,MINUS_ONE ,MINUS_ONE ,false),
41 HOWTO(R_X86_64_PC32, 0,4,32,true ,0,complain_overflow_signed ,0, "R_X86_64_PC32", false,0xffffffff,0xffffffff,true),
42 HOWTO(R_X86_64_GOT32, 0,4,32,false,0,complain_overflow_signed ,0, "R_X86_64_GOT32", false,0xffffffff,0xffffffff,false),
43 HOWTO(R_X86_64_PLT32, 0,4,32,true ,0,complain_overflow_signed ,0, "R_X86_64_PLT32", false,0xffffffff,0xffffffff,true),
44 HOWTO(R_X86_64_COPY, 0,4,32,false,0,complain_overflow_bitfield,0, "R_X86_64_COPY", false,0xffffffff,0xffffffff,false),
45 HOWTO(R_X86_64_GLOB_DAT, 0,4,64,false,0,complain_overflow_bitfield,0,"R_X86_64_GLOB_DAT",false,MINUS_ONE ,MINUS_ONE ,false),
46 HOWTO(R_X86_64_RELATIVE ,0,4,64,false,0,complain_overflow_bitfield,0,"R_X86_64_RELATIVE",false,MINUS_ONE ,MINUS_ONE ,false),
47 HOWTO(R_X86_64_JUMP_SLOT,0,4,64,false,0,complain_overflow_bitfield,0,"R_X86_64_JUMP_SLOT",false,MINUS_ONE,MINUS_ONE ,false),
48 HOWTO(R_X86_64_GOTPCREL, 0,4,32,true, 0,complain_overflow_signed ,0, "R_X86_64_PCREL", false,0xffffffff,0xffffffff,true),
49 HOWTO(R_X86_64_32, 0,4,32,false,0,complain_overflow_unsigned,0, "R_X86_64_32", false,0xffffffff,0xffffffff,false),
50 HOWTO(R_X86_64_32S, 0,4,32,false,0,complain_overflow_signed, 0, "R_X86_64_32S", false,0xffffffff,0xffffffff,false),
51 HOWTO(R_X86_64_16, 0,1,16,false,0,complain_overflow_bitfield,0, "R_X86_64_16", false,0xffff ,0xffff, false),
52 HOWTO(R_X86_64_PC16, 0,1,16,true ,0,complain_overflow_bitfield,0, "R_X86_64_PC16", false,0xffff ,0xffff, true),
53 HOWTO(R_X86_64_8, 0,0, 8,false,0,complain_overflow_signed ,0, "R_X86_64_8", false,0xff ,0xff, false),
54 HOWTO(R_X86_64_PC8, 0,0, 8,true ,0,complain_overflow_signed ,0, "R_X86_64_PC8", false,0xff ,0xff, true),
55};
56
57/* Map BFD relocs to the x86_64 elf relocs. */
58struct elf_reloc_map {
59 bfd_reloc_code_real_type bfd_reloc_val;
60 unsigned char elf_reloc_val;
61};
62
63static CONST struct elf_reloc_map x86_64_reloc_map[] =
64{
65 { BFD_RELOC_NONE, R_X86_64_NONE, },
66 { BFD_RELOC_64, R_X86_64_64, },
67 { BFD_RELOC_32_PCREL, R_X86_64_PC32, },
68 { BFD_RELOC_X86_64_GOT32, R_X86_64_GOT32,},
69 { BFD_RELOC_X86_64_PLT32, R_X86_64_PLT32,},
70 { BFD_RELOC_X86_64_COPY, R_X86_64_COPY, },
71 { BFD_RELOC_X86_64_GLOB_DAT, R_X86_64_GLOB_DAT, },
72 { BFD_RELOC_X86_64_JUMP_SLOT, R_X86_64_JUMP_SLOT, },
73 { BFD_RELOC_X86_64_RELATIVE, R_X86_64_RELATIVE, },
74 { BFD_RELOC_X86_64_GOTPCREL, R_X86_64_GOTPCREL, },
75 { BFD_RELOC_32, R_X86_64_32, },
76 { BFD_RELOC_X86_64_32S, R_X86_64_32S, },
77 { BFD_RELOC_16, R_X86_64_16, },
78 { BFD_RELOC_16_PCREL, R_X86_64_PC16, },
79 { BFD_RELOC_8, R_X86_64_8, },
80 { BFD_RELOC_8_PCREL, R_X86_64_PC8, },
81};
82
83
84static reloc_howto_type *elf64_x86_64_reloc_type_lookup
85 PARAMS ((bfd *, bfd_reloc_code_real_type));
86static void elf64_x86_64_info_to_howto
87 PARAMS ((bfd *, arelent *, Elf64_Internal_Rela *));
88static struct bfd_link_hash_table *elf64_x86_64_link_hash_table_create
89 PARAMS ((bfd *));
90static boolean elf64_x86_64_relocate_section
91 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
92 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
93
94/* Given a BFD reloc type, return a HOWTO structure. */
95static reloc_howto_type *
96elf64_x86_64_reloc_type_lookup (abfd, code)
97 bfd *abfd ATTRIBUTE_UNUSED;
98 bfd_reloc_code_real_type code;
99{
100 unsigned int i;
101 for (i = 0; i < sizeof (x86_64_reloc_map) / sizeof (struct elf_reloc_map);
102 i++)
103 {
104 if (x86_64_reloc_map[i].bfd_reloc_val == code)
105 return &x86_64_elf_howto_table[(int)
106 x86_64_reloc_map[i].elf_reloc_val];
107 }
108 return 0;
109}
110
111
112/* Given an x86_64 ELF reloc type, fill in an arelent structure. */
113static void
114elf64_x86_64_info_to_howto (abfd, cache_ptr, dst)
115 bfd *abfd ATTRIBUTE_UNUSED;
116 arelent *cache_ptr;
117 Elf64_Internal_Rela *dst;
118{
119 unsigned r_type;
120
121 r_type = ELF64_R_TYPE (dst->r_info);
122 BFD_ASSERT (r_type < (unsigned int) R_X86_64_max);
123 cache_ptr->howto = &x86_64_elf_howto_table[r_type];
124 BFD_ASSERT (r_type == cache_ptr->howto->type);
125}
126
127/* Hash table functions - these will be used by dynamic linking code and
128 right now they are needed to keep ld happy. */
129
130/* x86_64 ELF linker hash table. */
131
132struct elf64_x86_64_link_hash_table
133{
134 struct elf_link_hash_table root;
135};
136
137
138/* Get the X86-64 ELF linker hash table from a link_info structure. */
139
140#define elf64_x86_64_hash_table(p) \
141 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
142
143/* Create an X86-64 ELF linker hash table. */
144
145static struct bfd_link_hash_table *
146elf64_x86_64_link_hash_table_create (abfd)
147 bfd *abfd;
148{
149 struct elf64_x86_64_link_hash_table *ret;
150
151 ret = ((struct elf64_x86_64_link_hash_table *)
152 bfd_alloc (abfd, sizeof (struct elf64_x86_64_link_hash_table)));
153 if (ret == (struct elf64_x86_64_link_hash_table *) NULL)
154 return NULL;
155
156 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
157 _bfd_elf_link_hash_newfunc))
158 {
159 bfd_release (abfd, ret);
160 return NULL;
161 }
162
163 return &ret->root.root;
164}
165
166boolean
167elf64_x86_64_elf_object_p (abfd)
168 bfd *abfd;
169{
170 /* Set the right machine number for an x86-64 elf64 file. */
171 bfd_default_set_arch_mach (abfd, bfd_arch_i386, bfd_mach_x86_64);
172 return true;
173}
174
175/* Relocate an x86_64 ELF section. */
176
177static boolean
178elf64_x86_64_relocate_section (output_bfd, info, input_bfd, input_section,
179 contents, relocs, local_syms, local_sections)
180 bfd *output_bfd;
181 struct bfd_link_info *info;
182 bfd *input_bfd;
183 asection *input_section;
184 bfd_byte *contents;
185 Elf_Internal_Rela *relocs;
186 Elf_Internal_Sym *local_syms;
187 asection **local_sections;
188{
189 bfd *dynobj;
190 Elf_Internal_Shdr *symtab_hdr;
191 struct elf_link_hash_entry **sym_hashes;
192 bfd_vma *local_got_offsets;
193 asection *sreloc;
194 Elf_Internal_Rela *rel;
195 Elf_Internal_Rela *relend;
196
197 dynobj = elf_hash_table (info)->dynobj;
198 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
199 sym_hashes = elf_sym_hashes (input_bfd);
200 local_got_offsets = elf_local_got_offsets (input_bfd);
201
202 sreloc = NULL;
203 if (dynobj != NULL)
204 abort ();
205
206 rel = relocs;
207 relend = relocs + input_section->reloc_count;
208 for (; rel < relend; rel++)
209 {
210 int r_type;
211 reloc_howto_type *howto;
212 unsigned long r_symndx;
213 struct elf_link_hash_entry *h;
214 Elf_Internal_Sym *sym;
215 asection *sec;
216 bfd_vma relocation;
217 bfd_reloc_status_type r;
218 unsigned int indx;
219
220 r_type = ELF64_R_TYPE (rel->r_info);
221
222 if ((indx = (unsigned) r_type) >= R_X86_64_max)
223 {
224 bfd_set_error (bfd_error_bad_value);
225 return false;
226 }
227 howto = x86_64_elf_howto_table + indx;
228
229 r_symndx = ELF64_R_SYM (rel->r_info);
230
231 if (info->relocateable)
232 {
233 /* This is a relocateable link. We don't have to change
234 anything, unless the reloc is against a section symbol,
235 in which case we have to adjust according to where the
236 section symbol winds up in the output section. */
237 if (r_symndx < symtab_hdr->sh_info)
238 {
239 sym = local_syms + r_symndx;
240 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
241 {
242 sec = local_sections[r_symndx];
243 rel->r_addend += sec->output_offset + sym->st_value;
244 }
245 }
246
247 continue;
248 }
249
250 /* This is a final link. */
251 h = NULL;
252 sym = NULL;
253 sec = NULL;
254 if (r_symndx < symtab_hdr->sh_info)
255 {
256 sym = local_syms + r_symndx;
257 sec = local_sections[r_symndx];
258 relocation = (sec->output_section->vma
259 + sec->output_offset
260 + sym->st_value);
261 }
262 else
263 {
264 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
265 while (h->root.type == bfd_link_hash_indirect
266 || h->root.type == bfd_link_hash_warning)
267 h = (struct elf_link_hash_entry *) h->root.u.i.link;
268 if (h->root.type == bfd_link_hash_defined
269 || h->root.type == bfd_link_hash_defweak)
270 {
271 sec = h->root.u.def.section;
272 if (sec->output_section == NULL)
273 {
274 (*_bfd_error_handler)
275 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
276 bfd_get_filename (input_bfd), h->root.root.string,
277 bfd_get_section_name (input_bfd, input_section));
278 relocation = 0;
279 }
280 else
281 relocation = (h->root.u.def.value
282 + sec->output_section->vma
283 + sec->output_offset);
284 }
285 else if (h->root.type == bfd_link_hash_undefweak)
286 relocation = 0;
287 else
288 {
289 if (! ((*info->callbacks->undefined_symbol)
290 (info, h->root.root.string, input_bfd,
291 input_section, rel->r_offset,
292 (!info->shared || info->no_undefined
293 || ELF_ST_VISIBILITY (h->other)))))
294 return false;
295 relocation = 0;
296 }
297 }
298 /* This function should support shared objects, but don't. */
299 if (info->shared)
300 abort();
301
302 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
303 contents, rel->r_offset,
304 relocation, rel->r_addend);
305
306 if (r != bfd_reloc_ok)
307 {
308 switch (r)
309 {
310 default:
311 case bfd_reloc_outofrange:
312 abort ();
313 case bfd_reloc_overflow:
314 {
315 const char *name;
316
317 if (h != NULL)
318 name = h->root.root.string;
319 else
320 {
321 name = bfd_elf_string_from_elf_section (input_bfd,
322 symtab_hdr->sh_link,
323 sym->st_name);
324 if (name == NULL)
325 return false;
326 if (*name == '\0')
327 name = bfd_section_name (input_bfd, sec);
328 }
329 if (! ((*info->callbacks->reloc_overflow)
330 (info, name, howto->name, (bfd_vma) 0,
331 input_bfd, input_section, rel->r_offset)))
332 return false;
333 }
334 break;
335 }
336 }
337 }
338 return true;
339}
340
341
342#define TARGET_LITTLE_SYM bfd_elf64_x86_64_vec
343#define TARGET_LITTLE_NAME "elf64-x86-64"
344#define ELF_ARCH bfd_arch_i386
345#define ELF_MACHINE_CODE EM_X86_64
346#define ELF_MAXPAGESIZE 0x100000
347#define elf_info_to_howto elf64_x86_64_info_to_howto
348#define bfd_elf64_bfd_reloc_type_lookup elf64_x86_64_reloc_type_lookup
349#define elf_backend_object_p elf64_x86_64_elf_object_p
350#define elf_backend_relocate_section elf64_x86_64_relocate_section
351#define bfd_elf64_bfd_link_hash_table_create elf64_x86_64_link_hash_table_create
352
353#include "elf64-target.h"