1 /* X86-64 specific support for 64-bit ELF
2 Copyright 2000 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka <jh@suse.cz>.
5 This file is part of BFD, the Binary File Descriptor library.
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 2 of the License, or
10 (at your option) any later version.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
26 #include "elf/x86-64.h"
28 /* We use only the RELA entries. */
31 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */
32 #define MINUS_ONE (~ (bfd_vma) 0)
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 */
37 static reloc_howto_type x86_64_elf_howto_table
[] =
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),
57 /* Map BFD relocs to the x86_64 elf relocs. */
58 struct elf_reloc_map
{
59 bfd_reloc_code_real_type bfd_reloc_val
;
60 unsigned char elf_reloc_val
;
63 static CONST
struct elf_reloc_map x86_64_reloc_map
[] =
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
, },
84 static reloc_howto_type
*elf64_x86_64_reloc_type_lookup
85 PARAMS ((bfd
*, bfd_reloc_code_real_type
));
86 static void elf64_x86_64_info_to_howto
87 PARAMS ((bfd
*, arelent
*, Elf64_Internal_Rela
*));
88 static struct bfd_link_hash_table
*elf64_x86_64_link_hash_table_create
90 static 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
**));
94 /* Given a BFD reloc type, return a HOWTO structure. */
95 static reloc_howto_type
*
96 elf64_x86_64_reloc_type_lookup (abfd
, code
)
97 bfd
*abfd ATTRIBUTE_UNUSED
;
98 bfd_reloc_code_real_type code
;
101 for (i
= 0; i
< sizeof (x86_64_reloc_map
) / sizeof (struct elf_reloc_map
);
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
];
112 /* Given an x86_64 ELF reloc type, fill in an arelent structure. */
114 elf64_x86_64_info_to_howto (abfd
, cache_ptr
, dst
)
115 bfd
*abfd ATTRIBUTE_UNUSED
;
117 Elf64_Internal_Rela
*dst
;
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
);
127 /* Hash table functions - these will be used by dynamic linking code and
128 right now they are needed to keep ld happy. */
130 /* x86_64 ELF linker hash table. */
132 struct elf64_x86_64_link_hash_table
134 struct elf_link_hash_table root
;
138 /* Get the X86-64 ELF linker hash table from a link_info structure. */
140 #define elf64_x86_64_hash_table(p) \
141 ((struct elf64_x86_64_link_hash_table *) ((p)->hash))
143 /* Create an X86-64 ELF linker hash table. */
145 static struct bfd_link_hash_table
*
146 elf64_x86_64_link_hash_table_create (abfd
)
149 struct elf64_x86_64_link_hash_table
*ret
;
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
)
156 if (! _bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
157 _bfd_elf_link_hash_newfunc
))
159 bfd_release (abfd
, ret
);
163 return &ret
->root
.root
;
167 elf64_x86_64_elf_object_p (abfd
)
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
);
175 /* Relocate an x86_64 ELF section. */
178 elf64_x86_64_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
179 contents
, relocs
, local_syms
, local_sections
)
181 struct bfd_link_info
*info
;
183 asection
*input_section
;
185 Elf_Internal_Rela
*relocs
;
186 Elf_Internal_Sym
*local_syms
;
187 asection
**local_sections
;
190 Elf_Internal_Shdr
*symtab_hdr
;
191 struct elf_link_hash_entry
**sym_hashes
;
192 bfd_vma
*local_got_offsets
;
194 Elf_Internal_Rela
*rel
;
195 Elf_Internal_Rela
*relend
;
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
);
207 relend
= relocs
+ input_section
->reloc_count
;
208 for (; rel
< relend
; rel
++)
211 reloc_howto_type
*howto
;
212 unsigned long r_symndx
;
213 struct elf_link_hash_entry
*h
;
214 Elf_Internal_Sym
*sym
;
217 bfd_reloc_status_type r
;
220 r_type
= ELF64_R_TYPE (rel
->r_info
);
222 if ((indx
= (unsigned) r_type
) >= R_X86_64_max
)
224 bfd_set_error (bfd_error_bad_value
);
227 howto
= x86_64_elf_howto_table
+ indx
;
229 r_symndx
= ELF64_R_SYM (rel
->r_info
);
231 if (info
->relocateable
)
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
)
239 sym
= local_syms
+ r_symndx
;
240 if (ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
242 sec
= local_sections
[r_symndx
];
243 rel
->r_addend
+= sec
->output_offset
+ sym
->st_value
;
250 /* This is a final link. */
254 if (r_symndx
< symtab_hdr
->sh_info
)
256 sym
= local_syms
+ r_symndx
;
257 sec
= local_sections
[r_symndx
];
258 relocation
= (sec
->output_section
->vma
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
)
271 sec
= h
->root
.u
.def
.section
;
272 if (sec
->output_section
== NULL
)
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
));
281 relocation
= (h
->root
.u
.def
.value
282 + sec
->output_section
->vma
283 + sec
->output_offset
);
285 else if (h
->root
.type
== bfd_link_hash_undefweak
)
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
)))))
298 /* This function should support shared objects, but don't. */
302 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
303 contents
, rel
->r_offset
,
304 relocation
, rel
->r_addend
);
306 if (r
!= bfd_reloc_ok
)
311 case bfd_reloc_outofrange
:
313 case bfd_reloc_overflow
:
318 name
= h
->root
.root
.string
;
321 name
= bfd_elf_string_from_elf_section (input_bfd
,
327 name
= bfd_section_name (input_bfd
, sec
);
329 if (! ((*info
->callbacks
->reloc_overflow
)
330 (info
, name
, howto
->name
, (bfd_vma
) 0,
331 input_bfd
, input_section
, rel
->r_offset
)))
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
353 #include "elf64-target.h"