1 /* Intel x86-64 Mach-O support for BFD. 2 Copyright 2010 3 Free Software Foundation, Inc. 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 "mach-o.h" 24 #include "bfd.h" 25 #include "libbfd.h" 26 #include "libiberty.h" 27 #include "mach-o/x86-64.h" 28 29 #define bfd_mach_o_object_p bfd_mach_o_x86_64_object_p 30 #define bfd_mach_o_core_p bfd_mach_o_x86_64_core_p 31 #define bfd_mach_o_mkobject bfd_mach_o_x86_64_mkobject 32 33 static const bfd_target * 34 bfd_mach_o_x86_64_object_p (bfd *abfd) 35 { 36 return bfd_mach_o_header_p (abfd, 0, BFD_MACH_O_CPU_TYPE_X86_64); 37 } 38 39 static const bfd_target * 40 bfd_mach_o_x86_64_core_p (bfd *abfd) 41 { 42 return bfd_mach_o_header_p (abfd, 43 BFD_MACH_O_MH_CORE, BFD_MACH_O_CPU_TYPE_X86_64); 44 } 45 46 static bfd_boolean 47 bfd_mach_o_x86_64_mkobject (bfd *abfd) 48 { 49 bfd_mach_o_data_struct *mdata; 50 51 if (!bfd_mach_o_mkobject_init (abfd)) 52 return FALSE; 53 54 mdata = bfd_mach_o_get_data (abfd); 55 mdata->header.magic = BFD_MACH_O_MH_MAGIC_64; 56 mdata->header.cputype = BFD_MACH_O_CPU_TYPE_X86_64; 57 mdata->header.cpusubtype = BFD_MACH_O_CPU_SUBTYPE_X86_ALL; 58 mdata->header.byteorder = BFD_ENDIAN_LITTLE; 59 mdata->header.version = 2; 60 61 return TRUE; 62 } 63 64 /* In case we're on a 32-bit machine, construct a 64-bit "-1" value. */ 65 #define MINUS_ONE (~ (bfd_vma) 0) 66 67 static reloc_howto_type x86_64_howto_table[]= 68 { 69 /* 0 */ 70 HOWTO(BFD_RELOC_64, 0, 4, 64, FALSE, 0, 71 complain_overflow_bitfield, 72 NULL, "64", 73 FALSE, MINUS_ONE, MINUS_ONE, FALSE), 74 HOWTO(BFD_RELOC_32, 0, 2, 32, FALSE, 0, 75 complain_overflow_bitfield, 76 NULL, "32", 77 FALSE, 0xffffffff, 0xffffffff, FALSE), 78 HOWTO(BFD_RELOC_32_PCREL, 0, 2, 32, TRUE, 0, 79 complain_overflow_bitfield, 80 NULL, "DISP32", 81 FALSE, 0xffffffff, 0xffffffff, TRUE), 82 HOWTO(BFD_RELOC_MACH_O_X86_64_PCREL32_1, 0, 2, 32, TRUE, 0, 83 complain_overflow_bitfield, 84 NULL, "DISP32_1", 85 FALSE, 0xffffffff, 0xffffffff, TRUE), 86 /* 4 */ 87 HOWTO(BFD_RELOC_MACH_O_X86_64_PCREL32_2, 0, 2, 32, TRUE, 0, 88 complain_overflow_bitfield, 89 NULL, "DISP32_2", 90 FALSE, 0xffffffff, 0xffffffff, TRUE), 91 HOWTO(BFD_RELOC_MACH_O_X86_64_PCREL32_4, 0, 2, 32, TRUE, 0, 92 complain_overflow_bitfield, 93 NULL, "DISP32_4", 94 FALSE, 0xffffffff, 0xffffffff, TRUE), 95 HOWTO(BFD_RELOC_MACH_O_X86_64_BRANCH32, 0, 2, 32, TRUE, 0, 96 complain_overflow_bitfield, 97 NULL, "BRANCH32", 98 FALSE, 0xffffffff, 0xffffffff, TRUE), 99 HOWTO(BFD_RELOC_MACH_O_X86_64_GOT_LOAD, 0, 2, 32, TRUE, 0, 100 complain_overflow_bitfield, 101 NULL, "GOT_LOAD", 102 FALSE, 0xffffffff, 0xffffffff, TRUE), 103 /* 8 */ 104 HOWTO(BFD_RELOC_MACH_O_X86_64_SUBTRACTOR32, 0, 2, 32, FALSE, 0, 105 complain_overflow_bitfield, 106 NULL, "SUBTRACTOR32", 107 FALSE, 0xffffffff, 0xffffffff, FALSE), 108 HOWTO(BFD_RELOC_MACH_O_X86_64_SUBTRACTOR64, 0, 4, 64, FALSE, 0, 109 complain_overflow_bitfield, 110 NULL, "SUBTRACTOR64", 111 FALSE, MINUS_ONE, MINUS_ONE, FALSE), 112 HOWTO(BFD_RELOC_MACH_O_X86_64_GOT, 0, 2, 32, TRUE, 0, 113 complain_overflow_bitfield, 114 NULL, "GOT", 115 FALSE, 0xffffffff, 0xffffffff, TRUE), 116 HOWTO(BFD_RELOC_MACH_O_X86_64_BRANCH8, 0, 0, 8, TRUE, 0, 117 complain_overflow_bitfield, 118 NULL, "BRANCH8", 119 FALSE, 0xff, 0xff, TRUE), 120 }; 121 122 static bfd_boolean 123 bfd_mach_o_x86_64_swap_reloc_in (arelent *res, bfd_mach_o_reloc_info *reloc) 124 { 125 /* On x86-64, scattered relocs are not used. */ 126 if (reloc->r_scattered) 127 return FALSE; 128 129 switch (reloc->r_type) 130 { 131 case BFD_MACH_O_X86_64_RELOC_UNSIGNED: 132 if (reloc->r_pcrel) 133 return FALSE; 134 switch (reloc->r_length) 135 { 136 case 2: 137 res->howto = &x86_64_howto_table[1]; 138 return TRUE; 139 case 3: 140 res->howto = &x86_64_howto_table[0]; 141 return TRUE; 142 default: 143 return FALSE; 144 } 145 case BFD_MACH_O_X86_64_RELOC_SIGNED: 146 if (reloc->r_length == 2 && reloc->r_pcrel) 147 { 148 res->howto = &x86_64_howto_table[2]; 149 return TRUE; 150 } 151 break; 152 case BFD_MACH_O_X86_64_RELOC_BRANCH: 153 if (!reloc->r_pcrel) 154 return FALSE; 155 switch (reloc->r_length) 156 { 157 case 2: 158 res->howto = &x86_64_howto_table[6]; 159 return TRUE; 160 default: 161 return FALSE; 162 } 163 break; 164 case BFD_MACH_O_X86_64_RELOC_GOT_LOAD: 165 if (reloc->r_length == 2 && reloc->r_pcrel && reloc->r_extern) 166 { 167 res->howto = &x86_64_howto_table[7]; 168 return TRUE; 169 } 170 break; 171 case BFD_MACH_O_X86_64_RELOC_GOT: 172 if (reloc->r_length == 2 && reloc->r_pcrel && reloc->r_extern) 173 { 174 res->howto = &x86_64_howto_table[10]; 175 return TRUE; 176 } 177 break; 178 case BFD_MACH_O_X86_64_RELOC_SUBTRACTOR: 179 if (reloc->r_pcrel) 180 return FALSE; 181 switch (reloc->r_length) 182 { 183 case 2: 184 res->howto = &x86_64_howto_table[8]; 185 return TRUE; 186 case 3: 187 res->howto = &x86_64_howto_table[9]; 188 return TRUE; 189 default: 190 return FALSE; 191 } 192 break; 193 case BFD_MACH_O_X86_64_RELOC_SIGNED_1: 194 if (reloc->r_length == 2 && reloc->r_pcrel) 195 { 196 res->howto = &x86_64_howto_table[3]; 197 return TRUE; 198 } 199 break; 200 case BFD_MACH_O_X86_64_RELOC_SIGNED_2: 201 if (reloc->r_length == 2 && reloc->r_pcrel) 202 { 203 res->howto = &x86_64_howto_table[4]; 204 return TRUE; 205 } 206 break; 207 case BFD_MACH_O_X86_64_RELOC_SIGNED_4: 208 if (reloc->r_length == 2 && reloc->r_pcrel) 209 { 210 res->howto = &x86_64_howto_table[5]; 211 return TRUE; 212 } 213 break; 214 default: 215 return FALSE; 216 } 217 return FALSE; 218 } 219 220 static bfd_boolean 221 bfd_mach_o_x86_64_swap_reloc_out (arelent *rel, bfd_mach_o_reloc_info *rinfo) 222 { 223 rinfo->r_address = rel->address; 224 rinfo->r_scattered = 0; 225 switch (rel->howto->type) 226 { 227 case BFD_RELOC_32: 228 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_UNSIGNED; 229 rinfo->r_pcrel = 0; 230 rinfo->r_length = 2; 231 break; 232 case BFD_RELOC_64: 233 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_UNSIGNED; 234 rinfo->r_pcrel = 0; 235 rinfo->r_length = 3; 236 break; 237 case BFD_RELOC_32_PCREL: 238 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SIGNED; 239 rinfo->r_pcrel = 1; 240 rinfo->r_length = 2; 241 break; 242 case BFD_RELOC_MACH_O_X86_64_PCREL32_1: 243 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SIGNED_1; 244 rinfo->r_pcrel = 1; 245 rinfo->r_length = 2; 246 break; 247 case BFD_RELOC_MACH_O_X86_64_PCREL32_2: 248 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SIGNED_2; 249 rinfo->r_pcrel = 1; 250 rinfo->r_length = 2; 251 break; 252 case BFD_RELOC_MACH_O_X86_64_PCREL32_4: 253 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SIGNED_4; 254 rinfo->r_pcrel = 1; 255 rinfo->r_length = 2; 256 break; 257 case BFD_RELOC_MACH_O_X86_64_BRANCH32: 258 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_BRANCH; 259 rinfo->r_pcrel = 1; 260 rinfo->r_length = 2; 261 break; 262 case BFD_RELOC_MACH_O_X86_64_SUBTRACTOR32: 263 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SUBTRACTOR; 264 rinfo->r_pcrel = 0; 265 rinfo->r_length = 2; 266 break; 267 case BFD_RELOC_MACH_O_X86_64_SUBTRACTOR64: 268 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_SUBTRACTOR; 269 rinfo->r_pcrel = 0; 270 rinfo->r_length = 3; 271 break; 272 case BFD_RELOC_MACH_O_X86_64_GOT: 273 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_GOT; 274 rinfo->r_pcrel = 1; 275 rinfo->r_length = 2; 276 break; 277 case BFD_RELOC_MACH_O_X86_64_GOT_LOAD: 278 rinfo->r_type = BFD_MACH_O_X86_64_RELOC_GOT_LOAD; 279 rinfo->r_pcrel = 1; 280 rinfo->r_length = 2; 281 break; 282 default: 283 return FALSE; 284 } 285 if ((*rel->sym_ptr_ptr)->flags & BSF_SECTION_SYM) 286 { 287 rinfo->r_extern = 0; 288 rinfo->r_value = (*rel->sym_ptr_ptr)->section->target_index; 289 } 290 else 291 { 292 rinfo->r_extern = 1; 293 rinfo->r_value = (*rel->sym_ptr_ptr)->udata.i; 294 } 295 return TRUE; 296 } 297 298 static reloc_howto_type * 299 bfd_mach_o_x86_64_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, 300 bfd_reloc_code_real_type code) 301 { 302 unsigned int i; 303 304 for (i = 0; 305 i < sizeof (x86_64_howto_table) / sizeof (*x86_64_howto_table); 306 i++) 307 if (code == x86_64_howto_table[i].type) 308 return &x86_64_howto_table[i]; 309 return NULL; 310 } 311 312 static reloc_howto_type * 313 bfd_mach_o_x86_64_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, 314 const char *name ATTRIBUTE_UNUSED) 315 { 316 return NULL; 317 } 318 319 static bfd_boolean 320 bfd_mach_o_section_type_valid_for_x86_64 (unsigned long val) 321 { 322 if (val == BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS 323 || val == BFD_MACH_O_S_LAZY_SYMBOL_POINTERS 324 || val == BFD_MACH_O_S_SYMBOL_STUBS) 325 return FALSE; 326 return TRUE; 327 } 328 329 /* We want to bump the alignment of some sections. */ 330 static const mach_o_section_name_xlat text_section_names_xlat[] = 331 { 332 { ".eh_frame", "__eh_frame", 333 SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_COALESCED, 334 BFD_MACH_O_S_ATTR_LIVE_SUPPORT 335 | BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS 336 | BFD_MACH_O_S_ATTR_NO_TOC, 3}, 337 { NULL, NULL, 0, 0, 0, 0} 338 }; 339 340 const mach_o_segment_name_xlat mach_o_x86_64_segsec_names_xlat[] = 341 { 342 { "__TEXT", text_section_names_xlat }, 343 { NULL, NULL } 344 }; 345 346 #define bfd_mach_o_swap_reloc_in bfd_mach_o_x86_64_swap_reloc_in 347 #define bfd_mach_o_swap_reloc_out bfd_mach_o_x86_64_swap_reloc_out 348 349 #define bfd_mach_o_bfd_reloc_type_lookup bfd_mach_o_x86_64_bfd_reloc_type_lookup 350 #define bfd_mach_o_bfd_reloc_name_lookup bfd_mach_o_x86_64_bfd_reloc_name_lookup 351 #define bfd_mach_o_print_thread NULL 352 #define bfd_mach_o_tgt_seg_table mach_o_x86_64_segsec_names_xlat 353 #define bfd_mach_o_section_type_valid_for_tgt bfd_mach_o_section_type_valid_for_x86_64 354 355 #define TARGET_NAME mach_o_x86_64_vec 356 #define TARGET_STRING "mach-o-x86-64" 357 #define TARGET_ARCHITECTURE bfd_arch_i386 358 #define TARGET_BIG_ENDIAN 0 359 #define TARGET_ARCHIVE 0 360 #define TARGET_PRIORITY 0 361 #include "mach-o-target.c" 362