1 1.1 christos /* Mach-O support for BFD. 2 1.10 christos Copyright (C) 1999-2025 Free Software Foundation, Inc. 3 1.1 christos 4 1.1 christos This file is part of BFD, the Binary File Descriptor library. 5 1.1 christos 6 1.1 christos This program is free software; you can redistribute it and/or modify 7 1.1 christos it under the terms of the GNU General Public License as published by 8 1.1 christos the Free Software Foundation; either version 3 of the License, or 9 1.1 christos (at your option) any later version. 10 1.1 christos 11 1.1 christos This program is distributed in the hope that it will be useful, 12 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 13 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 1.1 christos GNU General Public License for more details. 15 1.1 christos 16 1.1 christos You should have received a copy of the GNU General Public License 17 1.1 christos along with this program; if not, write to the Free Software 18 1.1 christos Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 19 1.1 christos MA 02110-1301, USA. */ 20 1.1 christos 21 1.1 christos #include "sysdep.h" 22 1.7 christos #include <limits.h> 23 1.1 christos #include "bfd.h" 24 1.1 christos #include "libbfd.h" 25 1.1 christos #include "libiberty.h" 26 1.7 christos #include "mach-o.h" 27 1.1 christos #include "aout/stab_gnu.h" 28 1.1 christos #include "mach-o/reloc.h" 29 1.1 christos #include "mach-o/external.h" 30 1.1 christos #include <ctype.h> 31 1.1 christos #include <stdlib.h> 32 1.1 christos #include <string.h> 33 1.1 christos 34 1.1 christos #define bfd_mach_o_object_p bfd_mach_o_gen_object_p 35 1.1 christos #define bfd_mach_o_core_p bfd_mach_o_gen_core_p 36 1.1 christos #define bfd_mach_o_mkobject bfd_mach_o_gen_mkobject 37 1.1 christos 38 1.1 christos #define FILE_ALIGN(off, algn) \ 39 1.8 christos (((off) + ((ufile_ptr) 1 << (algn)) - 1) & ((ufile_ptr) -1 << (algn))) 40 1.3 christos 41 1.8 christos static bool 42 1.3 christos bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd); 43 1.1 christos 44 1.1 christos unsigned int 45 1.1 christos bfd_mach_o_version (bfd *abfd) 46 1.1 christos { 47 1.1 christos bfd_mach_o_data_struct *mdata = NULL; 48 1.1 christos 49 1.1 christos BFD_ASSERT (bfd_mach_o_valid (abfd)); 50 1.1 christos mdata = bfd_mach_o_get_data (abfd); 51 1.1 christos 52 1.1 christos return mdata->header.version; 53 1.1 christos } 54 1.1 christos 55 1.8 christos bool 56 1.1 christos bfd_mach_o_valid (bfd *abfd) 57 1.1 christos { 58 1.1 christos if (abfd == NULL || abfd->xvec == NULL) 59 1.8 christos return false; 60 1.1 christos 61 1.1 christos if (abfd->xvec->flavour != bfd_target_mach_o_flavour) 62 1.8 christos return false; 63 1.1 christos 64 1.1 christos if (bfd_mach_o_get_data (abfd) == NULL) 65 1.8 christos return false; 66 1.8 christos return true; 67 1.1 christos } 68 1.1 christos 69 1.8 christos static inline bool 70 1.1 christos mach_o_wide_p (bfd_mach_o_header *header) 71 1.1 christos { 72 1.1 christos switch (header->version) 73 1.1 christos { 74 1.1 christos case 1: 75 1.8 christos return false; 76 1.1 christos case 2: 77 1.8 christos return true; 78 1.1 christos default: 79 1.1 christos BFD_FAIL (); 80 1.8 christos return false; 81 1.1 christos } 82 1.1 christos } 83 1.1 christos 84 1.8 christos static inline bool 85 1.1 christos bfd_mach_o_wide_p (bfd *abfd) 86 1.1 christos { 87 1.1 christos return mach_o_wide_p (&bfd_mach_o_get_data (abfd)->header); 88 1.1 christos } 89 1.3 christos 90 1.1 christos /* Tables to translate well known Mach-O segment/section names to bfd 91 1.1 christos names. Use of canonical names (such as .text or .debug_frame) is required 92 1.1 christos by gdb. */ 93 1.1 christos 94 1.1 christos /* __TEXT Segment. */ 95 1.1 christos static const mach_o_section_name_xlat text_section_names_xlat[] = 96 1.1 christos { 97 1.3 christos { ".text", "__text", 98 1.1 christos SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR, 99 1.1 christos BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS, 0}, 100 1.1 christos { ".const", "__const", 101 1.1 christos SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 102 1.1 christos BFD_MACH_O_S_ATTR_NONE, 0}, 103 1.1 christos { ".static_const", "__static_const", 104 1.1 christos SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 105 1.1 christos BFD_MACH_O_S_ATTR_NONE, 0}, 106 1.1 christos { ".cstring", "__cstring", 107 1.1 christos SEC_READONLY | SEC_DATA | SEC_LOAD | SEC_MERGE | SEC_STRINGS, 108 1.1 christos BFD_MACH_O_S_CSTRING_LITERALS, 109 1.1 christos BFD_MACH_O_S_ATTR_NONE, 0}, 110 1.1 christos { ".literal4", "__literal4", 111 1.1 christos SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_4BYTE_LITERALS, 112 1.1 christos BFD_MACH_O_S_ATTR_NONE, 2}, 113 1.1 christos { ".literal8", "__literal8", 114 1.1 christos SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_8BYTE_LITERALS, 115 1.1 christos BFD_MACH_O_S_ATTR_NONE, 3}, 116 1.1 christos { ".literal16", "__literal16", 117 1.1 christos SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_16BYTE_LITERALS, 118 1.1 christos BFD_MACH_O_S_ATTR_NONE, 4}, 119 1.1 christos { ".constructor", "__constructor", 120 1.1 christos SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR, 121 1.1 christos BFD_MACH_O_S_ATTR_NONE, 0}, 122 1.1 christos { ".destructor", "__destructor", 123 1.1 christos SEC_CODE | SEC_LOAD, BFD_MACH_O_S_REGULAR, 124 1.1 christos BFD_MACH_O_S_ATTR_NONE, 0}, 125 1.1 christos { ".eh_frame", "__eh_frame", 126 1.1 christos SEC_READONLY | SEC_DATA | SEC_LOAD, BFD_MACH_O_S_COALESCED, 127 1.1 christos BFD_MACH_O_S_ATTR_LIVE_SUPPORT 128 1.1 christos | BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS 129 1.1 christos | BFD_MACH_O_S_ATTR_NO_TOC, 2}, 130 1.1 christos { NULL, NULL, 0, 0, 0, 0} 131 1.1 christos }; 132 1.1 christos 133 1.1 christos /* __DATA Segment. */ 134 1.1 christos static const mach_o_section_name_xlat data_section_names_xlat[] = 135 1.1 christos { 136 1.1 christos { ".data", "__data", 137 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 138 1.1 christos BFD_MACH_O_S_ATTR_NONE, 0}, 139 1.1 christos { ".bss", "__bss", 140 1.1 christos SEC_NO_FLAGS, BFD_MACH_O_S_ZEROFILL, 141 1.1 christos BFD_MACH_O_S_ATTR_NONE, 0}, 142 1.1 christos { ".const_data", "__const", 143 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 144 1.1 christos BFD_MACH_O_S_ATTR_NONE, 0}, 145 1.1 christos { ".static_data", "__static_data", 146 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 147 1.1 christos BFD_MACH_O_S_ATTR_NONE, 0}, 148 1.1 christos { ".mod_init_func", "__mod_init_func", 149 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS, 150 1.1 christos BFD_MACH_O_S_ATTR_NONE, 2}, 151 1.1 christos { ".mod_term_func", "__mod_term_func", 152 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS, 153 1.1 christos BFD_MACH_O_S_ATTR_NONE, 2}, 154 1.1 christos { ".dyld", "__dyld", 155 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 156 1.1 christos BFD_MACH_O_S_ATTR_NONE, 0}, 157 1.1 christos { ".cfstring", "__cfstring", 158 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 159 1.1 christos BFD_MACH_O_S_ATTR_NONE, 2}, 160 1.1 christos { NULL, NULL, 0, 0, 0, 0} 161 1.1 christos }; 162 1.1 christos 163 1.1 christos /* __DWARF Segment. */ 164 1.1 christos static const mach_o_section_name_xlat dwarf_section_names_xlat[] = 165 1.1 christos { 166 1.1 christos { ".debug_frame", "__debug_frame", 167 1.1 christos SEC_DEBUGGING, BFD_MACH_O_S_REGULAR, 168 1.1 christos BFD_MACH_O_S_ATTR_DEBUG, 0}, 169 1.1 christos { ".debug_info", "__debug_info", 170 1.1 christos SEC_DEBUGGING, BFD_MACH_O_S_REGULAR, 171 1.1 christos BFD_MACH_O_S_ATTR_DEBUG, 0}, 172 1.1 christos { ".debug_abbrev", "__debug_abbrev", 173 1.1 christos SEC_DEBUGGING, BFD_MACH_O_S_REGULAR, 174 1.1 christos BFD_MACH_O_S_ATTR_DEBUG, 0}, 175 1.1 christos { ".debug_aranges", "__debug_aranges", 176 1.1 christos SEC_DEBUGGING, BFD_MACH_O_S_REGULAR, 177 1.1 christos BFD_MACH_O_S_ATTR_DEBUG, 0}, 178 1.1 christos { ".debug_macinfo", "__debug_macinfo", 179 1.1 christos SEC_DEBUGGING, BFD_MACH_O_S_REGULAR, 180 1.1 christos BFD_MACH_O_S_ATTR_DEBUG, 0}, 181 1.1 christos { ".debug_line", "__debug_line", 182 1.1 christos SEC_DEBUGGING, BFD_MACH_O_S_REGULAR, 183 1.1 christos BFD_MACH_O_S_ATTR_DEBUG, 0}, 184 1.1 christos { ".debug_loc", "__debug_loc", 185 1.1 christos SEC_DEBUGGING, BFD_MACH_O_S_REGULAR, 186 1.1 christos BFD_MACH_O_S_ATTR_DEBUG, 0}, 187 1.1 christos { ".debug_pubnames", "__debug_pubnames", 188 1.1 christos SEC_DEBUGGING, BFD_MACH_O_S_REGULAR, 189 1.1 christos BFD_MACH_O_S_ATTR_DEBUG, 0}, 190 1.1 christos { ".debug_pubtypes", "__debug_pubtypes", 191 1.1 christos SEC_DEBUGGING, BFD_MACH_O_S_REGULAR, 192 1.1 christos BFD_MACH_O_S_ATTR_DEBUG, 0}, 193 1.1 christos { ".debug_str", "__debug_str", 194 1.1 christos SEC_DEBUGGING, BFD_MACH_O_S_REGULAR, 195 1.1 christos BFD_MACH_O_S_ATTR_DEBUG, 0}, 196 1.1 christos { ".debug_ranges", "__debug_ranges", 197 1.1 christos SEC_DEBUGGING, BFD_MACH_O_S_REGULAR, 198 1.1 christos BFD_MACH_O_S_ATTR_DEBUG, 0}, 199 1.1 christos { ".debug_macro", "__debug_macro", 200 1.1 christos SEC_DEBUGGING, BFD_MACH_O_S_REGULAR, 201 1.1 christos BFD_MACH_O_S_ATTR_DEBUG, 0}, 202 1.3 christos { ".debug_gdb_scripts", "__debug_gdb_scri", 203 1.3 christos SEC_DEBUGGING, BFD_MACH_O_S_REGULAR, 204 1.3 christos BFD_MACH_O_S_ATTR_DEBUG, 0}, 205 1.1 christos { NULL, NULL, 0, 0, 0, 0} 206 1.1 christos }; 207 1.1 christos 208 1.1 christos /* __OBJC Segment. */ 209 1.1 christos static const mach_o_section_name_xlat objc_section_names_xlat[] = 210 1.1 christos { 211 1.1 christos { ".objc_class", "__class", 212 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 213 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 214 1.1 christos { ".objc_meta_class", "__meta_class", 215 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 216 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 217 1.1 christos { ".objc_cat_cls_meth", "__cat_cls_meth", 218 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 219 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 220 1.1 christos { ".objc_cat_inst_meth", "__cat_inst_meth", 221 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 222 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 223 1.1 christos { ".objc_protocol", "__protocol", 224 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 225 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 226 1.1 christos { ".objc_string_object", "__string_object", 227 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 228 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 229 1.1 christos { ".objc_cls_meth", "__cls_meth", 230 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 231 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 232 1.1 christos { ".objc_inst_meth", "__inst_meth", 233 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 234 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 235 1.1 christos { ".objc_cls_refs", "__cls_refs", 236 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LITERAL_POINTERS, 237 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 238 1.1 christos { ".objc_message_refs", "__message_refs", 239 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_LITERAL_POINTERS, 240 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 241 1.1 christos { ".objc_symbols", "__symbols", 242 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 243 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 244 1.1 christos { ".objc_category", "__category", 245 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 246 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 247 1.1 christos { ".objc_class_vars", "__class_vars", 248 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 249 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 250 1.1 christos { ".objc_instance_vars", "__instance_vars", 251 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 252 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 253 1.1 christos { ".objc_module_info", "__module_info", 254 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 255 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 256 1.1 christos { ".objc_selector_strs", "__selector_strs", 257 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_CSTRING_LITERALS, 258 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 259 1.1 christos { ".objc_image_info", "__image_info", 260 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 261 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 262 1.1 christos { ".objc_selector_fixup", "__sel_fixup", 263 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 264 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 265 1.1 christos /* Objc V1 */ 266 1.1 christos { ".objc1_class_ext", "__class_ext", 267 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 268 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 269 1.1 christos { ".objc1_property_list", "__property", 270 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 271 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 272 1.1 christos { ".objc1_protocol_ext", "__protocol_ext", 273 1.1 christos SEC_DATA | SEC_LOAD, BFD_MACH_O_S_REGULAR, 274 1.1 christos BFD_MACH_O_S_ATTR_NO_DEAD_STRIP, 0}, 275 1.1 christos { NULL, NULL, 0, 0, 0, 0} 276 1.1 christos }; 277 1.1 christos 278 1.1 christos static const mach_o_segment_name_xlat segsec_names_xlat[] = 279 1.1 christos { 280 1.1 christos { "__TEXT", text_section_names_xlat }, 281 1.1 christos { "__DATA", data_section_names_xlat }, 282 1.1 christos { "__DWARF", dwarf_section_names_xlat }, 283 1.1 christos { "__OBJC", objc_section_names_xlat }, 284 1.1 christos { NULL, NULL } 285 1.1 christos }; 286 1.1 christos 287 1.1 christos static const char dsym_subdir[] = ".dSYM/Contents/Resources/DWARF"; 288 1.1 christos 289 1.1 christos /* For both cases bfd-name => mach-o name and vice versa, the specific target 290 1.1 christos is checked before the generic. This allows a target (e.g. ppc for cstring) 291 1.1 christos to override the generic definition with a more specific one. */ 292 1.1 christos 293 1.1 christos /* Fetch the translation from a Mach-O section designation (segment, section) 294 1.1 christos as a bfd short name, if one exists. Otherwise return NULL. 295 1.3 christos 296 1.1 christos Allow the segment and section names to be unterminated 16 byte arrays. */ 297 1.1 christos 298 1.1 christos const mach_o_section_name_xlat * 299 1.1 christos bfd_mach_o_section_data_for_mach_sect (bfd *abfd, const char *segname, 300 1.1 christos const char *sectname) 301 1.1 christos { 302 1.1 christos const struct mach_o_segment_name_xlat *seg; 303 1.1 christos const mach_o_section_name_xlat *sec; 304 1.1 christos bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd); 305 1.1 christos 306 1.1 christos /* First try any target-specific translations defined... */ 307 1.1 christos if (bed->segsec_names_xlat) 308 1.1 christos for (seg = bed->segsec_names_xlat; seg->segname; seg++) 309 1.1 christos if (strncmp (seg->segname, segname, BFD_MACH_O_SEGNAME_SIZE) == 0) 310 1.1 christos for (sec = seg->sections; sec->mach_o_name; sec++) 311 1.1 christos if (strncmp (sec->mach_o_name, sectname, 312 1.1 christos BFD_MACH_O_SECTNAME_SIZE) == 0) 313 1.1 christos return sec; 314 1.1 christos 315 1.1 christos /* ... and then the Mach-O generic ones. */ 316 1.1 christos for (seg = segsec_names_xlat; seg->segname; seg++) 317 1.1 christos if (strncmp (seg->segname, segname, BFD_MACH_O_SEGNAME_SIZE) == 0) 318 1.1 christos for (sec = seg->sections; sec->mach_o_name; sec++) 319 1.6 christos if (strncmp (sec->mach_o_name, sectname, 320 1.1 christos BFD_MACH_O_SECTNAME_SIZE) == 0) 321 1.6 christos return sec; 322 1.1 christos 323 1.3 christos return NULL; 324 1.1 christos } 325 1.1 christos 326 1.1 christos /* If the bfd_name for this section is a 'canonical' form for which we 327 1.3 christos know the Mach-O data, return the segment name and the data for the 328 1.1 christos Mach-O equivalent. Otherwise return NULL. */ 329 1.1 christos 330 1.1 christos const mach_o_section_name_xlat * 331 1.1 christos bfd_mach_o_section_data_for_bfd_name (bfd *abfd, const char *bfd_name, 332 1.1 christos const char **segname) 333 1.1 christos { 334 1.1 christos const struct mach_o_segment_name_xlat *seg; 335 1.1 christos const mach_o_section_name_xlat *sec; 336 1.1 christos bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd); 337 1.1 christos *segname = NULL; 338 1.1 christos 339 1.1 christos if (bfd_name[0] != '.') 340 1.1 christos return NULL; 341 1.1 christos 342 1.1 christos /* First try any target-specific translations defined... */ 343 1.1 christos if (bed->segsec_names_xlat) 344 1.1 christos for (seg = bed->segsec_names_xlat; seg->segname; seg++) 345 1.1 christos for (sec = seg->sections; sec->bfd_name; sec++) 346 1.1 christos if (strcmp (bfd_name, sec->bfd_name) == 0) 347 1.1 christos { 348 1.1 christos *segname = seg->segname; 349 1.1 christos return sec; 350 1.1 christos } 351 1.1 christos 352 1.1 christos /* ... and then the Mach-O generic ones. */ 353 1.1 christos for (seg = segsec_names_xlat; seg->segname; seg++) 354 1.1 christos for (sec = seg->sections; sec->bfd_name; sec++) 355 1.1 christos if (strcmp (bfd_name, sec->bfd_name) == 0) 356 1.1 christos { 357 1.1 christos *segname = seg->segname; 358 1.1 christos return sec; 359 1.1 christos } 360 1.1 christos 361 1.3 christos return NULL; 362 1.1 christos } 363 1.1 christos 364 1.1 christos /* Convert Mach-O section name to BFD. 365 1.1 christos 366 1.3 christos Try to use standard/canonical names, for which we have tables including 367 1.1 christos default flag settings - which are returned. Otherwise forge a new name 368 1.1 christos in the form "<segmentname>.<sectionname>" this will be prefixed with 369 1.1 christos LC_SEGMENT. if the segment name does not begin with an underscore. 370 1.1 christos 371 1.1 christos SEGNAME and SECTNAME are 16 byte arrays (they do not need to be NUL- 372 1.1 christos terminated if the name length is exactly 16 bytes - but must be if the name 373 1.1 christos length is less than 16 characters). */ 374 1.1 christos 375 1.1 christos void 376 1.1 christos bfd_mach_o_convert_section_name_to_bfd (bfd *abfd, const char *segname, 377 1.1 christos const char *secname, const char **name, 378 1.1 christos flagword *flags) 379 1.1 christos { 380 1.1 christos const mach_o_section_name_xlat *xlat; 381 1.1 christos char *res; 382 1.9 christos size_t len; 383 1.1 christos const char *pfx = ""; 384 1.1 christos 385 1.1 christos *name = NULL; 386 1.1 christos *flags = SEC_NO_FLAGS; 387 1.1 christos 388 1.3 christos /* First search for a canonical name... 389 1.1 christos xlat will be non-null if there is an entry for segname, secname. */ 390 1.1 christos xlat = bfd_mach_o_section_data_for_mach_sect (abfd, segname, secname); 391 1.1 christos if (xlat) 392 1.1 christos { 393 1.1 christos len = strlen (xlat->bfd_name); 394 1.3 christos res = bfd_alloc (abfd, len + 1); 395 1.1 christos if (res == NULL) 396 1.1 christos return; 397 1.9 christos memcpy (res, xlat->bfd_name, len + 1); 398 1.1 christos *name = res; 399 1.1 christos *flags = xlat->bfd_flags; 400 1.1 christos return; 401 1.1 christos } 402 1.1 christos 403 1.1 christos /* ... else we make up a bfd name from the segment concatenated with the 404 1.1 christos section. */ 405 1.1 christos 406 1.1 christos len = 16 + 1 + 16 + 1; 407 1.1 christos 408 1.1 christos /* Put "LC_SEGMENT." prefix if the segment name is weird (ie doesn't start 409 1.1 christos with an underscore. */ 410 1.1 christos if (segname[0] != '_') 411 1.1 christos { 412 1.1 christos static const char seg_pfx[] = "LC_SEGMENT."; 413 1.1 christos 414 1.1 christos pfx = seg_pfx; 415 1.1 christos len += sizeof (seg_pfx) - 1; 416 1.1 christos } 417 1.1 christos 418 1.1 christos res = bfd_alloc (abfd, len); 419 1.1 christos if (res == NULL) 420 1.1 christos return; 421 1.1 christos snprintf (res, len, "%s%.16s.%.16s", pfx, segname, secname); 422 1.1 christos *name = res; 423 1.1 christos } 424 1.1 christos 425 1.1 christos /* Convert a bfd section name to a Mach-O segment + section name. 426 1.1 christos 427 1.1 christos If the name is a canonical one for which we have a Darwin match 428 1.1 christos return the translation table - which contains defaults for flags, 429 1.1 christos type, attribute and default alignment data. 430 1.1 christos 431 1.3 christos Otherwise, expand the bfd_name (assumed to be in the form 432 1.1 christos "[LC_SEGMENT.]<segmentname>.<sectionname>") and return NULL. */ 433 1.1 christos 434 1.1 christos static const mach_o_section_name_xlat * 435 1.1 christos bfd_mach_o_convert_section_name_to_mach_o (bfd *abfd ATTRIBUTE_UNUSED, 436 1.6 christos asection *sect, 437 1.6 christos bfd_mach_o_section *section) 438 1.1 christos { 439 1.1 christos const mach_o_section_name_xlat *xlat; 440 1.7 christos const char *name = bfd_section_name (sect); 441 1.1 christos const char *segname; 442 1.1 christos const char *dot; 443 1.9 christos size_t len; 444 1.9 christos size_t seglen; 445 1.9 christos size_t seclen; 446 1.1 christos 447 1.1 christos memset (section->segname, 0, BFD_MACH_O_SEGNAME_SIZE + 1); 448 1.1 christos memset (section->sectname, 0, BFD_MACH_O_SECTNAME_SIZE + 1); 449 1.1 christos 450 1.1 christos /* See if is a canonical name ... */ 451 1.1 christos xlat = bfd_mach_o_section_data_for_bfd_name (abfd, name, &segname); 452 1.1 christos if (xlat) 453 1.1 christos { 454 1.1 christos strcpy (section->segname, segname); 455 1.1 christos strcpy (section->sectname, xlat->mach_o_name); 456 1.1 christos return xlat; 457 1.1 christos } 458 1.1 christos 459 1.1 christos /* .. else we convert our constructed one back to Mach-O. 460 1.1 christos Strip LC_SEGMENT. prefix, if present. */ 461 1.1 christos if (strncmp (name, "LC_SEGMENT.", 11) == 0) 462 1.1 christos name += 11; 463 1.1 christos 464 1.1 christos /* Find a dot. */ 465 1.1 christos dot = strchr (name, '.'); 466 1.1 christos len = strlen (name); 467 1.1 christos 468 1.1 christos /* Try to split name into segment and section names. */ 469 1.1 christos if (dot && dot != name) 470 1.1 christos { 471 1.1 christos seglen = dot - name; 472 1.1 christos seclen = len - (dot + 1 - name); 473 1.1 christos 474 1.3 christos if (seglen <= BFD_MACH_O_SEGNAME_SIZE 475 1.3 christos && seclen <= BFD_MACH_O_SECTNAME_SIZE) 476 1.6 christos { 477 1.6 christos memcpy (section->segname, name, seglen); 478 1.6 christos section->segname[seglen] = 0; 479 1.6 christos memcpy (section->sectname, dot + 1, seclen); 480 1.6 christos section->sectname[seclen] = 0; 481 1.6 christos return NULL; 482 1.6 christos } 483 1.1 christos } 484 1.1 christos 485 1.1 christos /* The segment and section names are both missing - don't make them 486 1.1 christos into dots. */ 487 1.1 christos if (dot && dot == name) 488 1.1 christos return NULL; 489 1.1 christos 490 1.1 christos /* Just duplicate the name into both segment and section. */ 491 1.1 christos if (len > 16) 492 1.1 christos len = 16; 493 1.1 christos memcpy (section->segname, name, len); 494 1.1 christos section->segname[len] = 0; 495 1.1 christos memcpy (section->sectname, name, len); 496 1.1 christos section->sectname[len] = 0; 497 1.1 christos return NULL; 498 1.1 christos } 499 1.1 christos 500 1.1 christos /* Return the size of an entry for section SEC. 501 1.1 christos Must be called only for symbol pointer section and symbol stubs 502 1.1 christos sections. */ 503 1.1 christos 504 1.1 christos unsigned int 505 1.1 christos bfd_mach_o_section_get_entry_size (bfd *abfd, bfd_mach_o_section *sec) 506 1.1 christos { 507 1.1 christos switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK) 508 1.1 christos { 509 1.1 christos case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS: 510 1.1 christos case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS: 511 1.1 christos return bfd_mach_o_wide_p (abfd) ? 8 : 4; 512 1.1 christos case BFD_MACH_O_S_SYMBOL_STUBS: 513 1.1 christos return sec->reserved2; 514 1.1 christos default: 515 1.1 christos BFD_FAIL (); 516 1.1 christos return 0; 517 1.1 christos } 518 1.1 christos } 519 1.1 christos 520 1.1 christos /* Return the number of indirect symbols for a section. 521 1.1 christos Must be called only for symbol pointer section and symbol stubs 522 1.1 christos sections. */ 523 1.1 christos 524 1.1 christos unsigned int 525 1.1 christos bfd_mach_o_section_get_nbr_indirect (bfd *abfd, bfd_mach_o_section *sec) 526 1.1 christos { 527 1.1 christos unsigned int elsz; 528 1.1 christos 529 1.9 christos /* FIXME: This array is set by the assembler but does not seem to be 530 1.9 christos set anywhere for objcopy. Since bfd_mach_o_build_dysymtab will 531 1.9 christos not fill in output bfd_mach_o_dysymtab_command indirect_syms when 532 1.9 christos this array is NULL we may as well return zero for the size. 533 1.9 christos This is enough to stop objcopy allocating huge amounts of memory 534 1.9 christos for indirect symbols in fuzzed object files. */ 535 1.9 christos if (sec->indirect_syms == NULL) 536 1.9 christos return 0; 537 1.9 christos 538 1.1 christos elsz = bfd_mach_o_section_get_entry_size (abfd, sec); 539 1.1 christos if (elsz == 0) 540 1.1 christos return 0; 541 1.1 christos else 542 1.1 christos return sec->size / elsz; 543 1.1 christos } 544 1.1 christos 545 1.3 christos /* Append command CMD to ABFD. Note that header.ncmds is not updated. */ 546 1.3 christos 547 1.3 christos static void 548 1.3 christos bfd_mach_o_append_command (bfd *abfd, bfd_mach_o_load_command *cmd) 549 1.3 christos { 550 1.3 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 551 1.3 christos 552 1.3 christos if (mdata->last_command != NULL) 553 1.3 christos mdata->last_command->next = cmd; 554 1.3 christos else 555 1.3 christos mdata->first_command = cmd; 556 1.3 christos mdata->last_command = cmd; 557 1.3 christos cmd->next = NULL; 558 1.3 christos } 559 1.1 christos 560 1.1 christos /* Copy any private info we understand from the input symbol 561 1.1 christos to the output symbol. */ 562 1.1 christos 563 1.8 christos bool 564 1.1 christos bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED, 565 1.1 christos asymbol *isymbol, 566 1.1 christos bfd *obfd ATTRIBUTE_UNUSED, 567 1.1 christos asymbol *osymbol) 568 1.1 christos { 569 1.1 christos bfd_mach_o_asymbol *os, *is; 570 1.3 christos 571 1.1 christos os = (bfd_mach_o_asymbol *)osymbol; 572 1.1 christos is = (bfd_mach_o_asymbol *)isymbol; 573 1.1 christos os->n_type = is->n_type; 574 1.1 christos os->n_sect = is->n_sect; 575 1.1 christos os->n_desc = is->n_desc; 576 1.1 christos os->symbol.udata.i = is->symbol.udata.i; 577 1.3 christos 578 1.8 christos return true; 579 1.1 christos } 580 1.1 christos 581 1.1 christos /* Copy any private info we understand from the input section 582 1.1 christos to the output section. */ 583 1.1 christos 584 1.8 christos bool 585 1.3 christos bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd, asection *isection, 586 1.10 christos bfd *obfd, asection *osection, 587 1.10 christos struct bfd_link_info *link_info) 588 1.3 christos { 589 1.10 christos if (link_info != NULL 590 1.10 christos || ibfd->xvec->flavour != bfd_target_mach_o_flavour 591 1.10 christos || obfd->xvec->flavour != bfd_target_mach_o_flavour) 592 1.10 christos return true; 593 1.10 christos 594 1.3 christos bfd_mach_o_section *os = bfd_mach_o_get_mach_o_section (osection); 595 1.3 christos bfd_mach_o_section *is = bfd_mach_o_get_mach_o_section (isection); 596 1.3 christos 597 1.3 christos BFD_ASSERT (is != NULL && os != NULL); 598 1.3 christos 599 1.3 christos os->flags = is->flags; 600 1.3 christos os->reserved1 = is->reserved1; 601 1.3 christos os->reserved2 = is->reserved2; 602 1.3 christos os->reserved3 = is->reserved3; 603 1.1 christos 604 1.8 christos return true; 605 1.1 christos } 606 1.1 christos 607 1.6 christos static const char * 608 1.6 christos cputype (unsigned long value) 609 1.6 christos { 610 1.6 christos switch (value) 611 1.6 christos { 612 1.6 christos case BFD_MACH_O_CPU_TYPE_VAX: return "VAX"; 613 1.6 christos case BFD_MACH_O_CPU_TYPE_MC680x0: return "MC68k"; 614 1.6 christos case BFD_MACH_O_CPU_TYPE_I386: return "I386"; 615 1.6 christos case BFD_MACH_O_CPU_TYPE_MIPS: return "MIPS"; 616 1.6 christos case BFD_MACH_O_CPU_TYPE_MC98000: return "MC98k"; 617 1.6 christos case BFD_MACH_O_CPU_TYPE_HPPA: return "HPPA"; 618 1.6 christos case BFD_MACH_O_CPU_TYPE_ARM: return "ARM"; 619 1.6 christos case BFD_MACH_O_CPU_TYPE_MC88000: return "MC88K"; 620 1.6 christos case BFD_MACH_O_CPU_TYPE_SPARC: return "SPARC"; 621 1.6 christos case BFD_MACH_O_CPU_TYPE_I860: return "I860"; 622 1.6 christos case BFD_MACH_O_CPU_TYPE_ALPHA: return "ALPHA"; 623 1.6 christos case BFD_MACH_O_CPU_TYPE_POWERPC: return "PPC"; 624 1.6 christos case BFD_MACH_O_CPU_TYPE_POWERPC_64: return "PPC64"; 625 1.6 christos case BFD_MACH_O_CPU_TYPE_X86_64: return "X86_64"; 626 1.6 christos case BFD_MACH_O_CPU_TYPE_ARM64: return "ARM64"; 627 1.6 christos default: return _("<unknown>"); 628 1.6 christos } 629 1.6 christos } 630 1.6 christos 631 1.6 christos static const char * 632 1.8 christos cpusubtype (unsigned long cpu_type, unsigned long cpu_subtype, char *buffer) 633 1.6 christos { 634 1.6 christos buffer[0] = 0; 635 1.7 christos switch (cpu_subtype & BFD_MACH_O_CPU_SUBTYPE_MASK) 636 1.6 christos { 637 1.6 christos case 0: 638 1.6 christos break; 639 1.6 christos case BFD_MACH_O_CPU_SUBTYPE_LIB64: 640 1.6 christos sprintf (buffer, " (LIB64)"); break; 641 1.6 christos default: 642 1.6 christos sprintf (buffer, _("<unknown mask flags>")); break; 643 1.6 christos } 644 1.6 christos 645 1.7 christos cpu_subtype &= ~ BFD_MACH_O_CPU_SUBTYPE_MASK; 646 1.6 christos 647 1.7 christos switch (cpu_type) 648 1.6 christos { 649 1.6 christos case BFD_MACH_O_CPU_TYPE_X86_64: 650 1.6 christos case BFD_MACH_O_CPU_TYPE_I386: 651 1.7 christos switch (cpu_subtype) 652 1.6 christos { 653 1.6 christos case BFD_MACH_O_CPU_SUBTYPE_X86_ALL: 654 1.6 christos return strcat (buffer, " (X86_ALL)"); 655 1.6 christos default: 656 1.6 christos break; 657 1.6 christos } 658 1.6 christos break; 659 1.7 christos 660 1.6 christos case BFD_MACH_O_CPU_TYPE_ARM: 661 1.7 christos switch (cpu_subtype) 662 1.6 christos { 663 1.6 christos case BFD_MACH_O_CPU_SUBTYPE_ARM_ALL: 664 1.6 christos return strcat (buffer, " (ARM_ALL)"); 665 1.6 christos case BFD_MACH_O_CPU_SUBTYPE_ARM_V4T: 666 1.6 christos return strcat (buffer, " (ARM_V4T)"); 667 1.6 christos case BFD_MACH_O_CPU_SUBTYPE_ARM_V6: 668 1.6 christos return strcat (buffer, " (ARM_V6)"); 669 1.6 christos case BFD_MACH_O_CPU_SUBTYPE_ARM_V5TEJ: 670 1.6 christos return strcat (buffer, " (ARM_V5TEJ)"); 671 1.6 christos case BFD_MACH_O_CPU_SUBTYPE_ARM_XSCALE: 672 1.6 christos return strcat (buffer, " (ARM_XSCALE)"); 673 1.6 christos case BFD_MACH_O_CPU_SUBTYPE_ARM_V7: 674 1.6 christos return strcat (buffer, " (ARM_V7)"); 675 1.6 christos default: 676 1.6 christos break; 677 1.6 christos } 678 1.6 christos break; 679 1.7 christos 680 1.6 christos case BFD_MACH_O_CPU_TYPE_ARM64: 681 1.7 christos switch (cpu_subtype) 682 1.6 christos { 683 1.6 christos case BFD_MACH_O_CPU_SUBTYPE_ARM64_ALL: 684 1.6 christos return strcat (buffer, " (ARM64_ALL)"); 685 1.6 christos case BFD_MACH_O_CPU_SUBTYPE_ARM64_V8: 686 1.6 christos return strcat (buffer, " (ARM64_V8)"); 687 1.6 christos default: 688 1.6 christos break; 689 1.6 christos } 690 1.6 christos break; 691 1.6 christos 692 1.6 christos default: 693 1.6 christos break; 694 1.6 christos } 695 1.6 christos 696 1.7 christos if (cpu_subtype != 0) 697 1.6 christos return strcat (buffer, _(" (<unknown>)")); 698 1.6 christos 699 1.6 christos return buffer; 700 1.6 christos } 701 1.6 christos 702 1.8 christos bool 703 1.6 christos bfd_mach_o_bfd_print_private_bfd_data (bfd *abfd, void *ptr) 704 1.6 christos { 705 1.6 christos FILE * file = (FILE *) ptr; 706 1.6 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 707 1.8 christos char buff[128]; 708 1.6 christos 709 1.6 christos fprintf (file, _(" MACH-O header:\n")); 710 1.6 christos fprintf (file, _(" magic: %#lx\n"), (long) mdata->header.magic); 711 1.6 christos fprintf (file, _(" cputype: %#lx (%s)\n"), (long) mdata->header.cputype, 712 1.6 christos cputype (mdata->header.cputype)); 713 1.6 christos fprintf (file, _(" cpusubtype: %#lx%s\n"), (long) mdata->header.cpusubtype, 714 1.8 christos cpusubtype (mdata->header.cputype, mdata->header.cpusubtype, buff)); 715 1.6 christos fprintf (file, _(" filetype: %#lx\n"), (long) mdata->header.filetype); 716 1.6 christos fprintf (file, _(" ncmds: %#lx\n"), (long) mdata->header.ncmds); 717 1.6 christos fprintf (file, _(" sizeocmds: %#lx\n"), (long) mdata->header.sizeofcmds); 718 1.6 christos fprintf (file, _(" flags: %#lx\n"), (long) mdata->header.flags); 719 1.6 christos fprintf (file, _(" version: %x\n"), mdata->header.version); 720 1.7 christos 721 1.8 christos return true; 722 1.6 christos } 723 1.6 christos 724 1.1 christos /* Copy any private info we understand from the input bfd 725 1.1 christos to the output bfd. */ 726 1.1 christos 727 1.8 christos bool 728 1.3 christos bfd_mach_o_bfd_copy_private_header_data (bfd *ibfd, bfd *obfd) 729 1.1 christos { 730 1.3 christos bfd_mach_o_data_struct *imdata; 731 1.3 christos bfd_mach_o_data_struct *omdata; 732 1.3 christos bfd_mach_o_load_command *icmd; 733 1.3 christos 734 1.1 christos if (bfd_get_flavour (ibfd) != bfd_target_mach_o_flavour 735 1.1 christos || bfd_get_flavour (obfd) != bfd_target_mach_o_flavour) 736 1.8 christos return true; 737 1.1 christos 738 1.1 christos BFD_ASSERT (bfd_mach_o_valid (ibfd)); 739 1.1 christos BFD_ASSERT (bfd_mach_o_valid (obfd)); 740 1.1 christos 741 1.3 christos imdata = bfd_mach_o_get_data (ibfd); 742 1.3 christos omdata = bfd_mach_o_get_data (obfd); 743 1.3 christos 744 1.3 christos /* Copy header flags. */ 745 1.3 christos omdata->header.flags = imdata->header.flags; 746 1.3 christos 747 1.6 christos /* PR 23299. Copy the cputype. */ 748 1.6 christos if (imdata->header.cputype != omdata->header.cputype) 749 1.6 christos { 750 1.6 christos if (omdata->header.cputype == 0) 751 1.6 christos omdata->header.cputype = imdata->header.cputype; 752 1.6 christos else if (imdata->header.cputype != 0) 753 1.6 christos /* Urg - what has happened ? */ 754 1.6 christos _bfd_error_handler (_("incompatible cputypes in mach-o files: %ld vs %ld"), 755 1.6 christos (long) imdata->header.cputype, 756 1.6 christos (long) omdata->header.cputype); 757 1.6 christos } 758 1.6 christos 759 1.6 christos /* Copy the cpusubtype. */ 760 1.6 christos omdata->header.cpusubtype = imdata->header.cpusubtype; 761 1.7 christos 762 1.3 christos /* Copy commands. */ 763 1.3 christos for (icmd = imdata->first_command; icmd != NULL; icmd = icmd->next) 764 1.3 christos { 765 1.3 christos bfd_mach_o_load_command *ocmd; 766 1.3 christos 767 1.3 christos switch (icmd->type) 768 1.3 christos { 769 1.3 christos case BFD_MACH_O_LC_LOAD_DYLIB: 770 1.3 christos case BFD_MACH_O_LC_LOAD_DYLINKER: 771 1.3 christos case BFD_MACH_O_LC_DYLD_INFO: 772 1.3 christos /* Command is copied. */ 773 1.3 christos ocmd = bfd_alloc (obfd, sizeof (bfd_mach_o_load_command)); 774 1.3 christos if (ocmd == NULL) 775 1.8 christos return false; 776 1.3 christos 777 1.3 christos /* Copy common fields. */ 778 1.3 christos ocmd->type = icmd->type; 779 1.3 christos ocmd->type_required = icmd->type_required; 780 1.3 christos ocmd->offset = 0; 781 1.3 christos ocmd->len = icmd->len; 782 1.3 christos break; 783 1.3 christos 784 1.3 christos default: 785 1.3 christos /* Command is not copied. */ 786 1.3 christos continue; 787 1.3 christos break; 788 1.3 christos } 789 1.3 christos 790 1.3 christos switch (icmd->type) 791 1.3 christos { 792 1.3 christos case BFD_MACH_O_LC_LOAD_DYLIB: 793 1.3 christos { 794 1.3 christos bfd_mach_o_dylib_command *idy = &icmd->command.dylib; 795 1.3 christos bfd_mach_o_dylib_command *ody = &ocmd->command.dylib; 796 1.3 christos 797 1.3 christos ody->name_offset = idy->name_offset; 798 1.3 christos ody->timestamp = idy->timestamp; 799 1.3 christos ody->current_version = idy->current_version; 800 1.3 christos ody->compatibility_version = idy->compatibility_version; 801 1.3 christos ody->name_str = idy->name_str; 802 1.3 christos } 803 1.3 christos break; 804 1.3 christos 805 1.3 christos case BFD_MACH_O_LC_LOAD_DYLINKER: 806 1.3 christos { 807 1.3 christos bfd_mach_o_dylinker_command *idy = &icmd->command.dylinker; 808 1.3 christos bfd_mach_o_dylinker_command *ody = &ocmd->command.dylinker; 809 1.3 christos 810 1.3 christos ody->name_offset = idy->name_offset; 811 1.3 christos ody->name_str = idy->name_str; 812 1.3 christos } 813 1.3 christos break; 814 1.3 christos 815 1.3 christos case BFD_MACH_O_LC_DYLD_INFO: 816 1.3 christos { 817 1.3 christos bfd_mach_o_dyld_info_command *idy = &icmd->command.dyld_info; 818 1.3 christos bfd_mach_o_dyld_info_command *ody = &ocmd->command.dyld_info; 819 1.3 christos 820 1.3 christos if (bfd_mach_o_read_dyld_content (ibfd, idy)) 821 1.3 christos { 822 1.3 christos ody->rebase_size = idy->rebase_size; 823 1.3 christos ody->rebase_content = idy->rebase_content; 824 1.3 christos 825 1.3 christos ody->bind_size = idy->bind_size; 826 1.3 christos ody->bind_content = idy->bind_content; 827 1.3 christos 828 1.3 christos ody->weak_bind_size = idy->weak_bind_size; 829 1.3 christos ody->weak_bind_content = idy->weak_bind_content; 830 1.3 christos 831 1.3 christos ody->lazy_bind_size = idy->lazy_bind_size; 832 1.3 christos ody->lazy_bind_content = idy->lazy_bind_content; 833 1.3 christos 834 1.3 christos ody->export_size = idy->export_size; 835 1.3 christos ody->export_content = idy->export_content; 836 1.3 christos } 837 1.3 christos /* PR 17512L: file: 730e492d. */ 838 1.3 christos else 839 1.3 christos { 840 1.3 christos ody->rebase_size = 841 1.3 christos ody->bind_size = 842 1.3 christos ody->weak_bind_size = 843 1.3 christos ody->lazy_bind_size = 844 1.3 christos ody->export_size = 0; 845 1.3 christos ody->rebase_content = 846 1.3 christos ody->bind_content = 847 1.3 christos ody->weak_bind_content = 848 1.3 christos ody->lazy_bind_content = 849 1.3 christos ody->export_content = NULL; 850 1.3 christos } 851 1.3 christos } 852 1.3 christos break; 853 1.3 christos 854 1.3 christos default: 855 1.3 christos /* That command should be handled. */ 856 1.3 christos abort (); 857 1.3 christos } 858 1.3 christos 859 1.3 christos /* Insert command. */ 860 1.3 christos bfd_mach_o_append_command (obfd, ocmd); 861 1.3 christos } 862 1.1 christos 863 1.8 christos return true; 864 1.1 christos } 865 1.1 christos 866 1.1 christos /* This allows us to set up to 32 bits of flags (unless we invent some 867 1.1 christos fiendish scheme to subdivide). For now, we'll just set the file flags 868 1.1 christos without error checking - just overwrite. */ 869 1.3 christos 870 1.8 christos bool 871 1.1 christos bfd_mach_o_bfd_set_private_flags (bfd *abfd, flagword flags) 872 1.1 christos { 873 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 874 1.1 christos 875 1.1 christos if (!mdata) 876 1.8 christos return false; 877 1.1 christos 878 1.1 christos mdata->header.flags = flags; 879 1.8 christos return true; 880 1.1 christos } 881 1.1 christos 882 1.1 christos /* Count the total number of symbols. */ 883 1.1 christos 884 1.1 christos static long 885 1.1 christos bfd_mach_o_count_symbols (bfd *abfd) 886 1.1 christos { 887 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 888 1.1 christos 889 1.1 christos if (mdata->symtab == NULL) 890 1.1 christos return 0; 891 1.1 christos return mdata->symtab->nsyms; 892 1.1 christos } 893 1.1 christos 894 1.1 christos long 895 1.1 christos bfd_mach_o_get_symtab_upper_bound (bfd *abfd) 896 1.1 christos { 897 1.1 christos long nsyms = bfd_mach_o_count_symbols (abfd); 898 1.1 christos 899 1.1 christos return ((nsyms + 1) * sizeof (asymbol *)); 900 1.1 christos } 901 1.1 christos 902 1.1 christos long 903 1.1 christos bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation) 904 1.1 christos { 905 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 906 1.1 christos long nsyms = bfd_mach_o_count_symbols (abfd); 907 1.1 christos bfd_mach_o_symtab_command *sym = mdata->symtab; 908 1.1 christos unsigned long j; 909 1.1 christos 910 1.1 christos if (nsyms < 0) 911 1.1 christos return nsyms; 912 1.1 christos 913 1.1 christos if (nsyms == 0) 914 1.1 christos { 915 1.1 christos /* Do not try to read symbols if there are none. */ 916 1.1 christos alocation[0] = NULL; 917 1.1 christos return 0; 918 1.1 christos } 919 1.1 christos 920 1.1 christos if (!bfd_mach_o_read_symtab_symbols (abfd)) 921 1.1 christos { 922 1.6 christos _bfd_error_handler 923 1.6 christos (_("bfd_mach_o_canonicalize_symtab: unable to load symbols")); 924 1.9 christos return -1; 925 1.1 christos } 926 1.1 christos 927 1.1 christos BFD_ASSERT (sym->symbols != NULL); 928 1.1 christos 929 1.1 christos for (j = 0; j < sym->nsyms; j++) 930 1.1 christos alocation[j] = &sym->symbols[j].symbol; 931 1.1 christos 932 1.1 christos alocation[j] = NULL; 933 1.1 christos 934 1.1 christos return nsyms; 935 1.1 christos } 936 1.1 christos 937 1.1 christos /* Create synthetic symbols for indirect symbols. */ 938 1.1 christos 939 1.1 christos long 940 1.1 christos bfd_mach_o_get_synthetic_symtab (bfd *abfd, 941 1.6 christos long symcount ATTRIBUTE_UNUSED, 942 1.6 christos asymbol **syms ATTRIBUTE_UNUSED, 943 1.6 christos long dynsymcount ATTRIBUTE_UNUSED, 944 1.6 christos asymbol **dynsyms ATTRIBUTE_UNUSED, 945 1.6 christos asymbol **ret) 946 1.1 christos { 947 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 948 1.1 christos bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab; 949 1.1 christos bfd_mach_o_symtab_command *symtab = mdata->symtab; 950 1.1 christos asymbol *s; 951 1.3 christos char * s_start; 952 1.1 christos unsigned long count, i, j, n; 953 1.1 christos size_t size; 954 1.1 christos char *names; 955 1.3 christos const char stub [] = "$stub"; 956 1.1 christos 957 1.1 christos *ret = NULL; 958 1.1 christos 959 1.1 christos /* Stop now if no symbols or no indirect symbols. */ 960 1.3 christos if (dysymtab == NULL || dysymtab->nindirectsyms == 0 961 1.3 christos || symtab == NULL || symtab->symbols == NULL) 962 1.1 christos return 0; 963 1.1 christos 964 1.1 christos /* We need to allocate a bfd symbol for every indirect symbol and to 965 1.1 christos allocate the memory for its name. */ 966 1.1 christos count = dysymtab->nindirectsyms; 967 1.9 christos size = 0; 968 1.1 christos for (j = 0; j < count; j++) 969 1.1 christos { 970 1.1 christos unsigned int isym = dysymtab->indirect_syms[j]; 971 1.9 christos const char *str; 972 1.1 christos 973 1.1 christos /* Some indirect symbols are anonymous. */ 974 1.9 christos if (isym < symtab->nsyms 975 1.9 christos && (str = symtab->symbols[isym].symbol.name) != NULL) 976 1.9 christos { 977 1.9 christos /* PR 17512: file: f5b8eeba. */ 978 1.9 christos size += strnlen (str, symtab->strsize - (str - symtab->strtab)); 979 1.9 christos size += sizeof (stub); 980 1.9 christos } 981 1.1 christos } 982 1.1 christos 983 1.9 christos s_start = bfd_malloc (size + count * sizeof (asymbol)); 984 1.3 christos s = *ret = (asymbol *) s_start; 985 1.1 christos if (s == NULL) 986 1.1 christos return -1; 987 1.1 christos names = (char *) (s + count); 988 1.3 christos 989 1.1 christos n = 0; 990 1.1 christos for (i = 0; i < mdata->nsects; i++) 991 1.1 christos { 992 1.1 christos bfd_mach_o_section *sec = mdata->sections[i]; 993 1.1 christos unsigned int first, last; 994 1.1 christos bfd_vma addr; 995 1.9 christos unsigned int entry_size; 996 1.3 christos 997 1.1 christos switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK) 998 1.6 christos { 999 1.6 christos case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS: 1000 1.6 christos case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS: 1001 1.6 christos case BFD_MACH_O_S_SYMBOL_STUBS: 1002 1.6 christos /* Only these sections have indirect symbols. */ 1003 1.6 christos first = sec->reserved1; 1004 1.6 christos last = first + bfd_mach_o_section_get_nbr_indirect (abfd, sec); 1005 1.6 christos addr = sec->addr; 1006 1.6 christos entry_size = bfd_mach_o_section_get_entry_size (abfd, sec); 1007 1.3 christos 1008 1.3 christos /* PR 17512: file: 08e15eec. */ 1009 1.9 christos if (first >= count || last > count || first > last) 1010 1.3 christos goto fail; 1011 1.3 christos 1012 1.6 christos for (j = first; j < last; j++) 1013 1.6 christos { 1014 1.6 christos unsigned int isym = dysymtab->indirect_syms[j]; 1015 1.9 christos const char *str; 1016 1.9 christos size_t len; 1017 1.1 christos 1018 1.6 christos if (isym < symtab->nsyms 1019 1.9 christos && (str = symtab->symbols[isym].symbol.name) != NULL) 1020 1.6 christos { 1021 1.9 christos /* PR 17512: file: 04d64d9b. */ 1022 1.9 christos if (n >= count) 1023 1.3 christos goto fail; 1024 1.9 christos len = strnlen (str, symtab->strsize - (str - symtab->strtab)); 1025 1.9 christos /* PR 17512: file: 47dfd4d2, 18f340a4. */ 1026 1.9 christos if (size < len + sizeof (stub)) 1027 1.3 christos goto fail; 1028 1.9 christos memcpy (names, str, len); 1029 1.9 christos memcpy (names + len, stub, sizeof (stub)); 1030 1.9 christos s->name = names; 1031 1.9 christos names += len + sizeof (stub); 1032 1.9 christos size -= len + sizeof (stub); 1033 1.9 christos s->the_bfd = symtab->symbols[isym].symbol.the_bfd; 1034 1.9 christos s->flags = BSF_GLOBAL | BSF_SYNTHETIC; 1035 1.9 christos s->section = sec->bfdsection; 1036 1.9 christos s->value = addr - sec->addr; 1037 1.9 christos s->udata.p = NULL; 1038 1.9 christos s++; 1039 1.9 christos n++; 1040 1.6 christos } 1041 1.6 christos addr += entry_size; 1042 1.6 christos } 1043 1.6 christos break; 1044 1.6 christos default: 1045 1.6 christos break; 1046 1.6 christos } 1047 1.1 christos } 1048 1.1 christos 1049 1.1 christos return n; 1050 1.3 christos 1051 1.3 christos fail: 1052 1.3 christos free (s_start); 1053 1.3 christos * ret = NULL; 1054 1.3 christos return -1; 1055 1.1 christos } 1056 1.1 christos 1057 1.1 christos void 1058 1.1 christos bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, 1059 1.1 christos asymbol *symbol, 1060 1.1 christos symbol_info *ret) 1061 1.1 christos { 1062 1.1 christos bfd_symbol_info (symbol, ret); 1063 1.1 christos } 1064 1.1 christos 1065 1.1 christos void 1066 1.1 christos bfd_mach_o_print_symbol (bfd *abfd, 1067 1.1 christos void * afile, 1068 1.1 christos asymbol *symbol, 1069 1.1 christos bfd_print_symbol_type how) 1070 1.1 christos { 1071 1.1 christos FILE *file = (FILE *) afile; 1072 1.1 christos const char *name; 1073 1.1 christos bfd_mach_o_asymbol *asym = (bfd_mach_o_asymbol *)symbol; 1074 1.1 christos 1075 1.1 christos switch (how) 1076 1.1 christos { 1077 1.1 christos case bfd_print_symbol_name: 1078 1.1 christos fprintf (file, "%s", symbol->name); 1079 1.1 christos break; 1080 1.1 christos default: 1081 1.1 christos bfd_print_symbol_vandf (abfd, (void *) file, symbol); 1082 1.1 christos if (asym->n_type & BFD_MACH_O_N_STAB) 1083 1.1 christos name = bfd_get_stab_name (asym->n_type); 1084 1.1 christos else 1085 1.1 christos switch (asym->n_type & BFD_MACH_O_N_TYPE) 1086 1.1 christos { 1087 1.1 christos case BFD_MACH_O_N_UNDF: 1088 1.6 christos if (symbol->value == 0) 1089 1.6 christos name = "UND"; 1090 1.6 christos else 1091 1.6 christos name = "COM"; 1092 1.1 christos break; 1093 1.1 christos case BFD_MACH_O_N_ABS: 1094 1.1 christos name = "ABS"; 1095 1.1 christos break; 1096 1.1 christos case BFD_MACH_O_N_INDR: 1097 1.1 christos name = "INDR"; 1098 1.1 christos break; 1099 1.1 christos case BFD_MACH_O_N_PBUD: 1100 1.1 christos name = "PBUD"; 1101 1.1 christos break; 1102 1.1 christos case BFD_MACH_O_N_SECT: 1103 1.1 christos name = "SECT"; 1104 1.1 christos break; 1105 1.1 christos default: 1106 1.1 christos name = "???"; 1107 1.1 christos break; 1108 1.1 christos } 1109 1.1 christos if (name == NULL) 1110 1.1 christos name = ""; 1111 1.1 christos fprintf (file, " %02x %-6s %02x %04x", 1112 1.6 christos asym->n_type, name, asym->n_sect, asym->n_desc); 1113 1.1 christos if ((asym->n_type & BFD_MACH_O_N_STAB) == 0 1114 1.1 christos && (asym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT) 1115 1.1 christos fprintf (file, " [%s]", symbol->section->name); 1116 1.1 christos fprintf (file, " %s", symbol->name); 1117 1.1 christos } 1118 1.1 christos } 1119 1.1 christos 1120 1.1 christos static void 1121 1.1 christos bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype, 1122 1.1 christos bfd_mach_o_cpu_subtype msubtype, 1123 1.1 christos enum bfd_architecture *type, 1124 1.1 christos unsigned long *subtype) 1125 1.1 christos { 1126 1.1 christos *subtype = bfd_arch_unknown; 1127 1.1 christos 1128 1.1 christos switch (mtype) 1129 1.1 christos { 1130 1.1 christos case BFD_MACH_O_CPU_TYPE_VAX: 1131 1.1 christos *type = bfd_arch_vax; 1132 1.1 christos break; 1133 1.1 christos case BFD_MACH_O_CPU_TYPE_MC680x0: 1134 1.1 christos *type = bfd_arch_m68k; 1135 1.1 christos break; 1136 1.1 christos case BFD_MACH_O_CPU_TYPE_I386: 1137 1.1 christos *type = bfd_arch_i386; 1138 1.1 christos *subtype = bfd_mach_i386_i386; 1139 1.1 christos break; 1140 1.1 christos case BFD_MACH_O_CPU_TYPE_X86_64: 1141 1.1 christos *type = bfd_arch_i386; 1142 1.1 christos *subtype = bfd_mach_x86_64; 1143 1.1 christos break; 1144 1.1 christos case BFD_MACH_O_CPU_TYPE_MIPS: 1145 1.1 christos *type = bfd_arch_mips; 1146 1.1 christos break; 1147 1.1 christos case BFD_MACH_O_CPU_TYPE_MC98000: 1148 1.1 christos *type = bfd_arch_m98k; 1149 1.1 christos break; 1150 1.1 christos case BFD_MACH_O_CPU_TYPE_HPPA: 1151 1.1 christos *type = bfd_arch_hppa; 1152 1.1 christos break; 1153 1.1 christos case BFD_MACH_O_CPU_TYPE_ARM: 1154 1.1 christos *type = bfd_arch_arm; 1155 1.1 christos switch (msubtype) 1156 1.6 christos { 1157 1.6 christos case BFD_MACH_O_CPU_SUBTYPE_ARM_V4T: 1158 1.6 christos *subtype = bfd_mach_arm_4T; 1159 1.6 christos break; 1160 1.6 christos case BFD_MACH_O_CPU_SUBTYPE_ARM_V6: 1161 1.6 christos *subtype = bfd_mach_arm_4T; /* Best fit ? */ 1162 1.6 christos break; 1163 1.6 christos case BFD_MACH_O_CPU_SUBTYPE_ARM_V5TEJ: 1164 1.6 christos *subtype = bfd_mach_arm_5TE; 1165 1.6 christos break; 1166 1.6 christos case BFD_MACH_O_CPU_SUBTYPE_ARM_XSCALE: 1167 1.6 christos *subtype = bfd_mach_arm_XScale; 1168 1.6 christos break; 1169 1.6 christos case BFD_MACH_O_CPU_SUBTYPE_ARM_V7: 1170 1.6 christos *subtype = bfd_mach_arm_5TE; /* Best fit ? */ 1171 1.6 christos break; 1172 1.6 christos case BFD_MACH_O_CPU_SUBTYPE_ARM_ALL: 1173 1.6 christos default: 1174 1.6 christos break; 1175 1.6 christos } 1176 1.1 christos break; 1177 1.1 christos case BFD_MACH_O_CPU_TYPE_SPARC: 1178 1.1 christos *type = bfd_arch_sparc; 1179 1.1 christos *subtype = bfd_mach_sparc; 1180 1.1 christos break; 1181 1.1 christos case BFD_MACH_O_CPU_TYPE_ALPHA: 1182 1.1 christos *type = bfd_arch_alpha; 1183 1.1 christos break; 1184 1.1 christos case BFD_MACH_O_CPU_TYPE_POWERPC: 1185 1.1 christos *type = bfd_arch_powerpc; 1186 1.1 christos *subtype = bfd_mach_ppc; 1187 1.1 christos break; 1188 1.1 christos case BFD_MACH_O_CPU_TYPE_POWERPC_64: 1189 1.1 christos *type = bfd_arch_powerpc; 1190 1.1 christos *subtype = bfd_mach_ppc64; 1191 1.1 christos break; 1192 1.3 christos case BFD_MACH_O_CPU_TYPE_ARM64: 1193 1.3 christos *type = bfd_arch_aarch64; 1194 1.3 christos *subtype = bfd_mach_aarch64; 1195 1.3 christos break; 1196 1.1 christos default: 1197 1.1 christos *type = bfd_arch_unknown; 1198 1.1 christos break; 1199 1.1 christos } 1200 1.1 christos } 1201 1.1 christos 1202 1.3 christos /* Write n NUL bytes to ABFD so that LEN + n is a multiple of 4. Return the 1203 1.3 christos number of bytes written or -1 in case of error. */ 1204 1.3 christos 1205 1.3 christos static int 1206 1.9 christos bfd_mach_o_pad4 (bfd *abfd, size_t len) 1207 1.3 christos { 1208 1.3 christos if (len % 4 != 0) 1209 1.3 christos { 1210 1.3 christos char pad[4] = {0,0,0,0}; 1211 1.3 christos unsigned int padlen = 4 - (len % 4); 1212 1.3 christos 1213 1.9 christos if (bfd_write (pad, padlen, abfd) != padlen) 1214 1.3 christos return -1; 1215 1.3 christos 1216 1.3 christos return padlen; 1217 1.3 christos } 1218 1.3 christos else 1219 1.3 christos return 0; 1220 1.3 christos } 1221 1.3 christos 1222 1.3 christos /* Likewise, but for a command. */ 1223 1.3 christos 1224 1.3 christos static int 1225 1.9 christos bfd_mach_o_pad_command (bfd *abfd, size_t len) 1226 1.3 christos { 1227 1.9 christos size_t align = bfd_mach_o_wide_p (abfd) ? 8 : 4; 1228 1.3 christos 1229 1.3 christos if (len % align != 0) 1230 1.3 christos { 1231 1.3 christos char pad[8] = {0}; 1232 1.9 christos size_t padlen = align - (len % align); 1233 1.3 christos 1234 1.9 christos if (bfd_write (pad, padlen, abfd) != padlen) 1235 1.3 christos return -1; 1236 1.3 christos 1237 1.3 christos return padlen; 1238 1.3 christos } 1239 1.3 christos else 1240 1.3 christos return 0; 1241 1.3 christos } 1242 1.3 christos 1243 1.8 christos static bool 1244 1.1 christos bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header) 1245 1.1 christos { 1246 1.1 christos struct mach_o_header_external raw; 1247 1.9 christos size_t size; 1248 1.1 christos 1249 1.1 christos size = mach_o_wide_p (header) ? 1250 1.1 christos BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE; 1251 1.1 christos 1252 1.1 christos bfd_h_put_32 (abfd, header->magic, raw.magic); 1253 1.1 christos bfd_h_put_32 (abfd, header->cputype, raw.cputype); 1254 1.1 christos bfd_h_put_32 (abfd, header->cpusubtype, raw.cpusubtype); 1255 1.1 christos bfd_h_put_32 (abfd, header->filetype, raw.filetype); 1256 1.1 christos bfd_h_put_32 (abfd, header->ncmds, raw.ncmds); 1257 1.1 christos bfd_h_put_32 (abfd, header->sizeofcmds, raw.sizeofcmds); 1258 1.1 christos bfd_h_put_32 (abfd, header->flags, raw.flags); 1259 1.1 christos 1260 1.1 christos if (mach_o_wide_p (header)) 1261 1.1 christos bfd_h_put_32 (abfd, header->reserved, raw.reserved); 1262 1.1 christos 1263 1.1 christos if (bfd_seek (abfd, 0, SEEK_SET) != 0 1264 1.9 christos || bfd_write (&raw, size, abfd) != size) 1265 1.8 christos return false; 1266 1.1 christos 1267 1.8 christos return true; 1268 1.1 christos } 1269 1.1 christos 1270 1.8 christos static bool 1271 1.1 christos bfd_mach_o_write_thread (bfd *abfd, bfd_mach_o_load_command *command) 1272 1.1 christos { 1273 1.1 christos bfd_mach_o_thread_command *cmd = &command->command.thread; 1274 1.1 christos unsigned int i; 1275 1.1 christos struct mach_o_thread_command_external raw; 1276 1.9 christos size_t offset; 1277 1.1 christos 1278 1.1 christos BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD) 1279 1.1 christos || (command->type == BFD_MACH_O_LC_UNIXTHREAD)); 1280 1.1 christos 1281 1.3 christos offset = BFD_MACH_O_LC_SIZE; 1282 1.1 christos for (i = 0; i < cmd->nflavours; i++) 1283 1.1 christos { 1284 1.1 christos BFD_ASSERT ((cmd->flavours[i].size % 4) == 0); 1285 1.9 christos BFD_ASSERT (cmd->flavours[i].offset 1286 1.9 christos == command->offset + offset + BFD_MACH_O_LC_SIZE); 1287 1.1 christos 1288 1.1 christos bfd_h_put_32 (abfd, cmd->flavours[i].flavour, raw.flavour); 1289 1.1 christos bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), raw.count); 1290 1.1 christos 1291 1.1 christos if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0 1292 1.9 christos || bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw)) 1293 1.8 christos return false; 1294 1.1 christos 1295 1.1 christos offset += cmd->flavours[i].size + sizeof (raw); 1296 1.1 christos } 1297 1.1 christos 1298 1.8 christos return true; 1299 1.3 christos } 1300 1.3 christos 1301 1.8 christos static bool 1302 1.3 christos bfd_mach_o_write_dylinker (bfd *abfd, bfd_mach_o_load_command *command) 1303 1.3 christos { 1304 1.3 christos bfd_mach_o_dylinker_command *cmd = &command->command.dylinker; 1305 1.3 christos struct mach_o_str_command_external raw; 1306 1.9 christos size_t namelen; 1307 1.3 christos 1308 1.3 christos bfd_h_put_32 (abfd, cmd->name_offset, raw.str); 1309 1.3 christos 1310 1.3 christos if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0 1311 1.9 christos || bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw)) 1312 1.8 christos return false; 1313 1.3 christos 1314 1.3 christos namelen = strlen (cmd->name_str) + 1; 1315 1.9 christos if (bfd_write (cmd->name_str, namelen, abfd) != namelen) 1316 1.8 christos return false; 1317 1.3 christos 1318 1.3 christos if (bfd_mach_o_pad_command (abfd, namelen) < 0) 1319 1.8 christos return false; 1320 1.3 christos 1321 1.8 christos return true; 1322 1.3 christos } 1323 1.3 christos 1324 1.8 christos static bool 1325 1.3 christos bfd_mach_o_write_dylib (bfd *abfd, bfd_mach_o_load_command *command) 1326 1.3 christos { 1327 1.3 christos bfd_mach_o_dylib_command *cmd = &command->command.dylib; 1328 1.3 christos struct mach_o_dylib_command_external raw; 1329 1.9 christos size_t namelen; 1330 1.3 christos 1331 1.3 christos bfd_h_put_32 (abfd, cmd->name_offset, raw.name); 1332 1.3 christos bfd_h_put_32 (abfd, cmd->timestamp, raw.timestamp); 1333 1.3 christos bfd_h_put_32 (abfd, cmd->current_version, raw.current_version); 1334 1.3 christos bfd_h_put_32 (abfd, cmd->compatibility_version, raw.compatibility_version); 1335 1.3 christos 1336 1.3 christos if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0 1337 1.9 christos || bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw)) 1338 1.8 christos return false; 1339 1.3 christos 1340 1.3 christos namelen = strlen (cmd->name_str) + 1; 1341 1.9 christos if (bfd_write (cmd->name_str, namelen, abfd) != namelen) 1342 1.8 christos return false; 1343 1.3 christos 1344 1.3 christos if (bfd_mach_o_pad_command (abfd, namelen) < 0) 1345 1.8 christos return false; 1346 1.3 christos 1347 1.8 christos return true; 1348 1.3 christos } 1349 1.3 christos 1350 1.8 christos static bool 1351 1.3 christos bfd_mach_o_write_main (bfd *abfd, bfd_mach_o_load_command *command) 1352 1.3 christos { 1353 1.3 christos bfd_mach_o_main_command *cmd = &command->command.main; 1354 1.3 christos struct mach_o_entry_point_command_external raw; 1355 1.3 christos 1356 1.3 christos bfd_h_put_64 (abfd, cmd->entryoff, raw.entryoff); 1357 1.3 christos bfd_h_put_64 (abfd, cmd->stacksize, raw.stacksize); 1358 1.3 christos 1359 1.3 christos if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0 1360 1.9 christos || bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw)) 1361 1.8 christos return false; 1362 1.3 christos 1363 1.8 christos return true; 1364 1.3 christos } 1365 1.3 christos 1366 1.8 christos static bool 1367 1.3 christos bfd_mach_o_write_dyld_info (bfd *abfd, bfd_mach_o_load_command *command) 1368 1.3 christos { 1369 1.3 christos bfd_mach_o_dyld_info_command *cmd = &command->command.dyld_info; 1370 1.3 christos struct mach_o_dyld_info_command_external raw; 1371 1.3 christos 1372 1.3 christos bfd_h_put_32 (abfd, cmd->rebase_off, raw.rebase_off); 1373 1.3 christos bfd_h_put_32 (abfd, cmd->rebase_size, raw.rebase_size); 1374 1.3 christos bfd_h_put_32 (abfd, cmd->bind_off, raw.bind_off); 1375 1.3 christos bfd_h_put_32 (abfd, cmd->bind_size, raw.bind_size); 1376 1.3 christos bfd_h_put_32 (abfd, cmd->weak_bind_off, raw.weak_bind_off); 1377 1.3 christos bfd_h_put_32 (abfd, cmd->weak_bind_size, raw.weak_bind_size); 1378 1.3 christos bfd_h_put_32 (abfd, cmd->lazy_bind_off, raw.lazy_bind_off); 1379 1.3 christos bfd_h_put_32 (abfd, cmd->lazy_bind_size, raw.lazy_bind_size); 1380 1.3 christos bfd_h_put_32 (abfd, cmd->export_off, raw.export_off); 1381 1.3 christos bfd_h_put_32 (abfd, cmd->export_size, raw.export_size); 1382 1.3 christos 1383 1.3 christos if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0 1384 1.9 christos || bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw)) 1385 1.8 christos return false; 1386 1.3 christos 1387 1.3 christos if (cmd->rebase_size != 0) 1388 1.3 christos if (bfd_seek (abfd, cmd->rebase_off, SEEK_SET) != 0 1389 1.9 christos || (bfd_write (cmd->rebase_content, cmd->rebase_size, abfd) != 1390 1.3 christos cmd->rebase_size)) 1391 1.8 christos return false; 1392 1.3 christos 1393 1.3 christos if (cmd->bind_size != 0) 1394 1.3 christos if (bfd_seek (abfd, cmd->bind_off, SEEK_SET) != 0 1395 1.9 christos || (bfd_write (cmd->bind_content, cmd->bind_size, abfd) != 1396 1.3 christos cmd->bind_size)) 1397 1.8 christos return false; 1398 1.3 christos 1399 1.3 christos if (cmd->weak_bind_size != 0) 1400 1.3 christos if (bfd_seek (abfd, cmd->weak_bind_off, SEEK_SET) != 0 1401 1.9 christos || (bfd_write (cmd->weak_bind_content, cmd->weak_bind_size, abfd) != 1402 1.3 christos cmd->weak_bind_size)) 1403 1.8 christos return false; 1404 1.3 christos 1405 1.3 christos if (cmd->lazy_bind_size != 0) 1406 1.3 christos if (bfd_seek (abfd, cmd->lazy_bind_off, SEEK_SET) != 0 1407 1.9 christos || (bfd_write (cmd->lazy_bind_content, cmd->lazy_bind_size, abfd) != 1408 1.3 christos cmd->lazy_bind_size)) 1409 1.8 christos return false; 1410 1.3 christos 1411 1.3 christos if (cmd->export_size != 0) 1412 1.3 christos if (bfd_seek (abfd, cmd->export_off, SEEK_SET) != 0 1413 1.9 christos || (bfd_write (cmd->export_content, cmd->export_size, abfd) != 1414 1.3 christos cmd->export_size)) 1415 1.8 christos return false; 1416 1.3 christos 1417 1.8 christos return true; 1418 1.1 christos } 1419 1.1 christos 1420 1.1 christos long 1421 1.9 christos bfd_mach_o_get_reloc_upper_bound (bfd *abfd, asection *asect) 1422 1.1 christos { 1423 1.9 christos size_t count, raw; 1424 1.9 christos 1425 1.9 christos count = asect->reloc_count; 1426 1.9 christos if (count >= LONG_MAX / sizeof (arelent *) 1427 1.9 christos || _bfd_mul_overflow (count, BFD_MACH_O_RELENT_SIZE, &raw)) 1428 1.7 christos { 1429 1.7 christos bfd_set_error (bfd_error_file_too_big); 1430 1.7 christos return -1; 1431 1.7 christos } 1432 1.9 christos if (!bfd_write_p (abfd)) 1433 1.9 christos { 1434 1.9 christos ufile_ptr filesize = bfd_get_file_size (abfd); 1435 1.9 christos if (filesize != 0 && raw > filesize) 1436 1.9 christos { 1437 1.9 christos bfd_set_error (bfd_error_file_truncated); 1438 1.9 christos return -1; 1439 1.9 christos } 1440 1.9 christos } 1441 1.9 christos return (count + 1) * sizeof (arelent *); 1442 1.1 christos } 1443 1.1 christos 1444 1.1 christos /* In addition to the need to byte-swap the symbol number, the bit positions 1445 1.1 christos of the fields in the relocation information vary per target endian-ness. */ 1446 1.1 christos 1447 1.5 christos void 1448 1.1 christos bfd_mach_o_swap_in_non_scattered_reloc (bfd *abfd, bfd_mach_o_reloc_info *rel, 1449 1.6 christos unsigned char *fields) 1450 1.1 christos { 1451 1.1 christos unsigned char info = fields[3]; 1452 1.1 christos 1453 1.1 christos if (bfd_big_endian (abfd)) 1454 1.1 christos { 1455 1.1 christos rel->r_value = (fields[0] << 16) | (fields[1] << 8) | fields[2]; 1456 1.1 christos rel->r_type = (info >> BFD_MACH_O_BE_TYPE_SHIFT) & BFD_MACH_O_TYPE_MASK; 1457 1.1 christos rel->r_pcrel = (info & BFD_MACH_O_BE_PCREL) ? 1 : 0; 1458 1.3 christos rel->r_length = (info >> BFD_MACH_O_BE_LENGTH_SHIFT) 1459 1.1 christos & BFD_MACH_O_LENGTH_MASK; 1460 1.1 christos rel->r_extern = (info & BFD_MACH_O_BE_EXTERN) ? 1 : 0; 1461 1.1 christos } 1462 1.1 christos else 1463 1.1 christos { 1464 1.1 christos rel->r_value = (fields[2] << 16) | (fields[1] << 8) | fields[0]; 1465 1.1 christos rel->r_type = (info >> BFD_MACH_O_LE_TYPE_SHIFT) & BFD_MACH_O_TYPE_MASK; 1466 1.1 christos rel->r_pcrel = (info & BFD_MACH_O_LE_PCREL) ? 1 : 0; 1467 1.3 christos rel->r_length = (info >> BFD_MACH_O_LE_LENGTH_SHIFT) 1468 1.1 christos & BFD_MACH_O_LENGTH_MASK; 1469 1.1 christos rel->r_extern = (info & BFD_MACH_O_LE_EXTERN) ? 1 : 0; 1470 1.1 christos } 1471 1.1 christos } 1472 1.1 christos 1473 1.5 christos /* Set syms_ptr_ptr and addend of RES. */ 1474 1.5 christos 1475 1.8 christos bool 1476 1.5 christos bfd_mach_o_canonicalize_non_scattered_reloc (bfd *abfd, 1477 1.5 christos bfd_mach_o_reloc_info *reloc, 1478 1.5 christos arelent *res, asymbol **syms) 1479 1.5 christos { 1480 1.5 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 1481 1.5 christos unsigned int num; 1482 1.5 christos asymbol **sym; 1483 1.5 christos 1484 1.5 christos /* Non-scattered relocation. */ 1485 1.5 christos reloc->r_scattered = 0; 1486 1.5 christos res->addend = 0; 1487 1.5 christos 1488 1.5 christos num = reloc->r_value; 1489 1.5 christos 1490 1.5 christos if (reloc->r_extern) 1491 1.5 christos { 1492 1.5 christos /* PR 17512: file: 8396-1185-0.004. */ 1493 1.5 christos if (num >= (unsigned) bfd_mach_o_count_symbols (abfd)) 1494 1.10 christos sym = &bfd_und_section_ptr->symbol; 1495 1.5 christos else if (syms == NULL) 1496 1.10 christos sym = &bfd_und_section_ptr->symbol; 1497 1.5 christos else 1498 1.5 christos /* An external symbol number. */ 1499 1.5 christos sym = syms + num; 1500 1.5 christos } 1501 1.5 christos else if (num == 0x00ffffff || num == 0) 1502 1.5 christos { 1503 1.5 christos /* The 'symnum' in a non-scattered PAIR is 0x00ffffff. But as this 1504 1.5 christos is generic code, we don't know wether this is really a PAIR. 1505 1.5 christos This value is almost certainly not a valid section number, hence 1506 1.5 christos this specific case to avoid an assertion failure. 1507 1.5 christos Target specific swap_reloc_in routine should adjust that. */ 1508 1.10 christos sym = &bfd_abs_section_ptr->symbol; 1509 1.5 christos } 1510 1.5 christos else 1511 1.5 christos { 1512 1.5 christos /* PR 17512: file: 006-2964-0.004. */ 1513 1.5 christos if (num > mdata->nsects) 1514 1.7 christos { 1515 1.7 christos _bfd_error_handler (_("\ 1516 1.7 christos malformed mach-o reloc: section index is greater than the number of sections")); 1517 1.8 christos return false; 1518 1.7 christos } 1519 1.5 christos 1520 1.5 christos /* A section number. */ 1521 1.10 christos sym = &mdata->sections[num - 1]->bfdsection->symbol; 1522 1.5 christos /* For a symbol defined in section S, the addend (stored in the 1523 1.5 christos binary) contains the address of the section. To comply with 1524 1.5 christos bfd convention, subtract the section address. 1525 1.5 christos Use the address from the header, so that the user can modify 1526 1.6 christos the vma of the section. */ 1527 1.5 christos res->addend = -mdata->sections[num - 1]->addr; 1528 1.5 christos } 1529 1.5 christos 1530 1.5 christos /* Note: Pairs for PPC LO/HI/HA are not scattered, but contain the offset 1531 1.5 christos in the lower 16bits of the address value. So we have to find the 1532 1.5 christos 'symbol' from the preceding reloc. We do this even though the 1533 1.5 christos section symbol is probably not needed here, because NULL symbol 1534 1.5 christos values cause an assert in generic BFD code. This must be done in 1535 1.5 christos the PPC swap_reloc_in routine. */ 1536 1.5 christos res->sym_ptr_ptr = sym; 1537 1.5 christos 1538 1.8 christos return true; 1539 1.5 christos } 1540 1.5 christos 1541 1.5 christos /* Do most of the work for canonicalize_relocs on RAW: create internal 1542 1.5 christos representation RELOC and set most fields of RES using symbol table SYMS. 1543 1.5 christos Each target still has to set the howto of RES and possibly adjust other 1544 1.5 christos fields. 1545 1.5 christos Previously the Mach-O hook point was simply swap_in, but some targets 1546 1.5 christos (like arm64) don't follow the generic rules (symnum is a value for the 1547 1.5 christos non-scattered relocation ADDEND). */ 1548 1.5 christos 1549 1.8 christos bool 1550 1.5 christos bfd_mach_o_pre_canonicalize_one_reloc (bfd *abfd, 1551 1.5 christos struct mach_o_reloc_info_external *raw, 1552 1.5 christos bfd_mach_o_reloc_info *reloc, 1553 1.5 christos arelent *res, asymbol **syms) 1554 1.1 christos { 1555 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 1556 1.1 christos bfd_vma addr; 1557 1.1 christos 1558 1.1 christos addr = bfd_get_32 (abfd, raw->r_address); 1559 1.10 christos res->sym_ptr_ptr = &bfd_und_section_ptr->symbol; 1560 1.1 christos res->addend = 0; 1561 1.3 christos 1562 1.1 christos if (addr & BFD_MACH_O_SR_SCATTERED) 1563 1.1 christos { 1564 1.1 christos unsigned int j; 1565 1.1 christos bfd_vma symnum = bfd_get_32 (abfd, raw->r_symbolnum); 1566 1.1 christos 1567 1.1 christos /* Scattered relocation, can't be extern. */ 1568 1.5 christos reloc->r_scattered = 1; 1569 1.5 christos reloc->r_extern = 0; 1570 1.1 christos 1571 1.1 christos /* Extract section and offset from r_value (symnum). */ 1572 1.5 christos reloc->r_value = symnum; 1573 1.1 christos /* FIXME: This breaks when a symbol in a reloc exactly follows the 1574 1.1 christos end of the data for the section (e.g. in a calculation of section 1575 1.1 christos data length). At present, the symbol will end up associated with 1576 1.1 christos the following section or, if it falls within alignment padding, as 1577 1.9 christos the undefined section symbol. */ 1578 1.1 christos for (j = 0; j < mdata->nsects; j++) 1579 1.6 christos { 1580 1.6 christos bfd_mach_o_section *sect = mdata->sections[j]; 1581 1.6 christos if (symnum >= sect->addr && symnum < sect->addr + sect->size) 1582 1.6 christos { 1583 1.10 christos res->sym_ptr_ptr = §->bfdsection->symbol; 1584 1.6 christos res->addend = symnum - sect->addr; 1585 1.6 christos break; 1586 1.6 christos } 1587 1.6 christos } 1588 1.1 christos 1589 1.1 christos /* Extract the info and address fields from r_address. */ 1590 1.5 christos reloc->r_type = BFD_MACH_O_GET_SR_TYPE (addr); 1591 1.5 christos reloc->r_length = BFD_MACH_O_GET_SR_LENGTH (addr); 1592 1.5 christos reloc->r_pcrel = addr & BFD_MACH_O_SR_PCREL; 1593 1.5 christos reloc->r_address = BFD_MACH_O_GET_SR_TYPE (addr); 1594 1.1 christos res->address = BFD_MACH_O_GET_SR_ADDRESS (addr); 1595 1.1 christos } 1596 1.1 christos else 1597 1.1 christos { 1598 1.1 christos /* Non-scattered relocation. */ 1599 1.5 christos reloc->r_scattered = 0; 1600 1.5 christos reloc->r_address = addr; 1601 1.5 christos res->address = addr; 1602 1.3 christos 1603 1.1 christos /* The value and info fields have to be extracted dependent on target 1604 1.6 christos endian-ness. */ 1605 1.5 christos bfd_mach_o_swap_in_non_scattered_reloc (abfd, reloc, raw->r_symbolnum); 1606 1.1 christos 1607 1.5 christos if (!bfd_mach_o_canonicalize_non_scattered_reloc (abfd, reloc, 1608 1.5 christos res, syms)) 1609 1.8 christos return false; 1610 1.1 christos } 1611 1.3 christos 1612 1.3 christos /* We have set up a reloc with all the information present, so the swapper 1613 1.3 christos can modify address, value and addend fields, if necessary, to convey 1614 1.3 christos information in the generic BFD reloc that is mach-o specific. */ 1615 1.1 christos 1616 1.8 christos return true; 1617 1.1 christos } 1618 1.1 christos 1619 1.1 christos static int 1620 1.1 christos bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos, 1621 1.6 christos unsigned long count, 1622 1.6 christos arelent *res, asymbol **syms) 1623 1.1 christos { 1624 1.5 christos bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd); 1625 1.1 christos unsigned long i; 1626 1.7 christos struct mach_o_reloc_info_external *native_relocs = NULL; 1627 1.8 christos size_t native_size; 1628 1.1 christos 1629 1.1 christos /* Allocate and read relocs. */ 1630 1.8 christos if (_bfd_mul_overflow (count, BFD_MACH_O_RELENT_SIZE, &native_size)) 1631 1.8 christos /* PR 17512: file: 09477b57. */ 1632 1.7 christos goto err; 1633 1.3 christos 1634 1.8 christos if (bfd_seek (abfd, filepos, SEEK_SET) != 0) 1635 1.8 christos return -1; 1636 1.8 christos native_relocs = (struct mach_o_reloc_info_external *) 1637 1.8 christos _bfd_malloc_and_read (abfd, native_size, native_size); 1638 1.1 christos if (native_relocs == NULL) 1639 1.1 christos return -1; 1640 1.1 christos 1641 1.1 christos for (i = 0; i < count; i++) 1642 1.1 christos { 1643 1.5 christos if (!(*bed->_bfd_mach_o_canonicalize_one_reloc)(abfd, &native_relocs[i], 1644 1.6 christos &res[i], syms, res)) 1645 1.6 christos goto err; 1646 1.1 christos } 1647 1.1 christos free (native_relocs); 1648 1.1 christos return i; 1649 1.7 christos 1650 1.1 christos err: 1651 1.1 christos free (native_relocs); 1652 1.7 christos if (bfd_get_error () == bfd_error_no_error) 1653 1.7 christos bfd_set_error (bfd_error_invalid_operation); 1654 1.1 christos return -1; 1655 1.1 christos } 1656 1.1 christos 1657 1.1 christos long 1658 1.1 christos bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect, 1659 1.6 christos arelent **rels, asymbol **syms) 1660 1.1 christos { 1661 1.1 christos bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd); 1662 1.1 christos unsigned long i; 1663 1.1 christos arelent *res; 1664 1.1 christos 1665 1.1 christos if (asect->reloc_count == 0) 1666 1.1 christos return 0; 1667 1.1 christos 1668 1.1 christos /* No need to go further if we don't know how to read relocs. */ 1669 1.5 christos if (bed->_bfd_mach_o_canonicalize_one_reloc == NULL) 1670 1.1 christos return 0; 1671 1.1 christos 1672 1.1 christos if (asect->relocation == NULL) 1673 1.1 christos { 1674 1.8 christos size_t amt; 1675 1.8 christos 1676 1.8 christos if (_bfd_mul_overflow (asect->reloc_count, sizeof (arelent), &amt)) 1677 1.9 christos { 1678 1.9 christos bfd_set_error (bfd_error_file_too_big); 1679 1.9 christos return -1; 1680 1.9 christos } 1681 1.8 christos res = bfd_malloc (amt); 1682 1.1 christos if (res == NULL) 1683 1.6 christos return -1; 1684 1.1 christos 1685 1.1 christos if (bfd_mach_o_canonicalize_relocs (abfd, asect->rel_filepos, 1686 1.6 christos asect->reloc_count, res, syms) < 0) 1687 1.6 christos { 1688 1.6 christos free (res); 1689 1.6 christos return -1; 1690 1.6 christos } 1691 1.1 christos asect->relocation = res; 1692 1.1 christos } 1693 1.1 christos 1694 1.1 christos res = asect->relocation; 1695 1.1 christos for (i = 0; i < asect->reloc_count; i++) 1696 1.1 christos rels[i] = &res[i]; 1697 1.1 christos rels[i] = NULL; 1698 1.1 christos 1699 1.1 christos return i; 1700 1.1 christos } 1701 1.1 christos 1702 1.1 christos long 1703 1.1 christos bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *abfd) 1704 1.1 christos { 1705 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 1706 1.9 christos bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab; 1707 1.1 christos 1708 1.9 christos if (dysymtab == NULL) 1709 1.1 christos return 1; 1710 1.9 christos 1711 1.9 christos ufile_ptr filesize = bfd_get_file_size (abfd); 1712 1.9 christos size_t amt; 1713 1.9 christos 1714 1.9 christos if (filesize != 0) 1715 1.9 christos { 1716 1.9 christos if (dysymtab->extreloff > filesize 1717 1.9 christos || dysymtab->nextrel > ((filesize - dysymtab->extreloff) 1718 1.9 christos / BFD_MACH_O_RELENT_SIZE) 1719 1.9 christos || dysymtab->locreloff > filesize 1720 1.9 christos || dysymtab->nlocrel > ((filesize - dysymtab->locreloff) 1721 1.9 christos / BFD_MACH_O_RELENT_SIZE)) 1722 1.9 christos { 1723 1.9 christos bfd_set_error (bfd_error_file_truncated); 1724 1.9 christos return -1; 1725 1.9 christos } 1726 1.9 christos } 1727 1.9 christos if (dysymtab->nextrel + dysymtab->nlocrel < dysymtab->nextrel 1728 1.9 christos || _bfd_mul_overflow (dysymtab->nextrel + dysymtab->nlocrel, 1729 1.9 christos sizeof (arelent), &amt)) 1730 1.9 christos { 1731 1.9 christos bfd_set_error (bfd_error_file_too_big); 1732 1.9 christos return -1; 1733 1.9 christos } 1734 1.9 christos 1735 1.9 christos return (dysymtab->nextrel + dysymtab->nlocrel + 1) * sizeof (arelent *); 1736 1.1 christos } 1737 1.1 christos 1738 1.1 christos long 1739 1.1 christos bfd_mach_o_canonicalize_dynamic_reloc (bfd *abfd, arelent **rels, 1740 1.6 christos struct bfd_symbol **syms) 1741 1.1 christos { 1742 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 1743 1.1 christos bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab; 1744 1.1 christos bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd); 1745 1.1 christos unsigned long i; 1746 1.1 christos arelent *res; 1747 1.1 christos 1748 1.1 christos if (dysymtab == NULL) 1749 1.1 christos return 0; 1750 1.1 christos if (dysymtab->nextrel == 0 && dysymtab->nlocrel == 0) 1751 1.1 christos return 0; 1752 1.1 christos 1753 1.1 christos /* No need to go further if we don't know how to read relocs. */ 1754 1.5 christos if (bed->_bfd_mach_o_canonicalize_one_reloc == NULL) 1755 1.1 christos return 0; 1756 1.1 christos 1757 1.1 christos if (mdata->dyn_reloc_cache == NULL) 1758 1.1 christos { 1759 1.9 christos size_t amt = (dysymtab->nextrel + dysymtab->nlocrel) * sizeof (arelent); 1760 1.8 christos res = bfd_malloc (amt); 1761 1.1 christos if (res == NULL) 1762 1.6 christos return -1; 1763 1.1 christos 1764 1.1 christos if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->extreloff, 1765 1.6 christos dysymtab->nextrel, res, syms) < 0) 1766 1.6 christos { 1767 1.6 christos free (res); 1768 1.6 christos return -1; 1769 1.6 christos } 1770 1.1 christos 1771 1.1 christos if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->locreloff, 1772 1.6 christos dysymtab->nlocrel, 1773 1.6 christos res + dysymtab->nextrel, syms) < 0) 1774 1.6 christos { 1775 1.6 christos free (res); 1776 1.6 christos return -1; 1777 1.6 christos } 1778 1.1 christos 1779 1.1 christos mdata->dyn_reloc_cache = res; 1780 1.1 christos } 1781 1.1 christos 1782 1.1 christos res = mdata->dyn_reloc_cache; 1783 1.1 christos for (i = 0; i < dysymtab->nextrel + dysymtab->nlocrel; i++) 1784 1.1 christos rels[i] = &res[i]; 1785 1.1 christos rels[i] = NULL; 1786 1.1 christos return i; 1787 1.1 christos } 1788 1.1 christos 1789 1.1 christos /* In addition to the need to byte-swap the symbol number, the bit positions 1790 1.1 christos of the fields in the relocation information vary per target endian-ness. */ 1791 1.1 christos 1792 1.1 christos static void 1793 1.1 christos bfd_mach_o_swap_out_non_scattered_reloc (bfd *abfd, unsigned char *fields, 1794 1.6 christos bfd_mach_o_reloc_info *rel) 1795 1.1 christos { 1796 1.1 christos unsigned char info = 0; 1797 1.1 christos 1798 1.1 christos BFD_ASSERT (rel->r_type <= 15); 1799 1.1 christos BFD_ASSERT (rel->r_length <= 3); 1800 1.1 christos 1801 1.1 christos if (bfd_big_endian (abfd)) 1802 1.1 christos { 1803 1.1 christos fields[0] = (rel->r_value >> 16) & 0xff; 1804 1.1 christos fields[1] = (rel->r_value >> 8) & 0xff; 1805 1.1 christos fields[2] = rel->r_value & 0xff; 1806 1.1 christos info |= rel->r_type << BFD_MACH_O_BE_TYPE_SHIFT; 1807 1.1 christos info |= rel->r_pcrel ? BFD_MACH_O_BE_PCREL : 0; 1808 1.1 christos info |= rel->r_length << BFD_MACH_O_BE_LENGTH_SHIFT; 1809 1.1 christos info |= rel->r_extern ? BFD_MACH_O_BE_EXTERN : 0; 1810 1.1 christos } 1811 1.1 christos else 1812 1.1 christos { 1813 1.1 christos fields[2] = (rel->r_value >> 16) & 0xff; 1814 1.1 christos fields[1] = (rel->r_value >> 8) & 0xff; 1815 1.1 christos fields[0] = rel->r_value & 0xff; 1816 1.1 christos info |= rel->r_type << BFD_MACH_O_LE_TYPE_SHIFT; 1817 1.1 christos info |= rel->r_pcrel ? BFD_MACH_O_LE_PCREL : 0; 1818 1.1 christos info |= rel->r_length << BFD_MACH_O_LE_LENGTH_SHIFT; 1819 1.1 christos info |= rel->r_extern ? BFD_MACH_O_LE_EXTERN : 0; 1820 1.1 christos } 1821 1.1 christos fields[3] = info; 1822 1.1 christos } 1823 1.1 christos 1824 1.8 christos static bool 1825 1.1 christos bfd_mach_o_write_relocs (bfd *abfd, bfd_mach_o_section *section) 1826 1.1 christos { 1827 1.1 christos unsigned int i; 1828 1.1 christos arelent **entries; 1829 1.1 christos asection *sec; 1830 1.1 christos bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd); 1831 1.1 christos 1832 1.1 christos sec = section->bfdsection; 1833 1.1 christos if (sec->reloc_count == 0) 1834 1.8 christos return true; 1835 1.1 christos 1836 1.1 christos if (bed->_bfd_mach_o_swap_reloc_out == NULL) 1837 1.8 christos return true; 1838 1.1 christos 1839 1.1 christos if (bfd_seek (abfd, section->reloff, SEEK_SET) != 0) 1840 1.8 christos return false; 1841 1.1 christos 1842 1.1 christos /* Convert and write. */ 1843 1.1 christos entries = section->bfdsection->orelocation; 1844 1.1 christos for (i = 0; i < section->nreloc; i++) 1845 1.1 christos { 1846 1.1 christos arelent *rel = entries[i]; 1847 1.1 christos struct mach_o_reloc_info_external raw; 1848 1.1 christos bfd_mach_o_reloc_info info, *pinfo = &info; 1849 1.1 christos 1850 1.1 christos /* Convert relocation to an intermediate representation. */ 1851 1.1 christos if (!(*bed->_bfd_mach_o_swap_reloc_out) (rel, pinfo)) 1852 1.8 christos return false; 1853 1.1 christos 1854 1.1 christos /* Lower the relocation info. */ 1855 1.1 christos if (pinfo->r_scattered) 1856 1.6 christos { 1857 1.6 christos unsigned long v; 1858 1.1 christos 1859 1.6 christos v = BFD_MACH_O_SR_SCATTERED 1860 1.6 christos | (pinfo->r_pcrel ? BFD_MACH_O_SR_PCREL : 0) 1861 1.6 christos | BFD_MACH_O_SET_SR_LENGTH (pinfo->r_length) 1862 1.6 christos | BFD_MACH_O_SET_SR_TYPE (pinfo->r_type) 1863 1.6 christos | BFD_MACH_O_SET_SR_ADDRESS (pinfo->r_address); 1864 1.6 christos /* Note: scattered relocs have field in reverse order... */ 1865 1.6 christos bfd_put_32 (abfd, v, raw.r_address); 1866 1.6 christos bfd_put_32 (abfd, pinfo->r_value, raw.r_symbolnum); 1867 1.6 christos } 1868 1.1 christos else 1869 1.6 christos { 1870 1.6 christos bfd_put_32 (abfd, pinfo->r_address, raw.r_address); 1871 1.6 christos bfd_mach_o_swap_out_non_scattered_reloc (abfd, raw.r_symbolnum, 1872 1.1 christos pinfo); 1873 1.6 christos } 1874 1.1 christos 1875 1.9 christos if (bfd_write (&raw, BFD_MACH_O_RELENT_SIZE, abfd) 1876 1.6 christos != BFD_MACH_O_RELENT_SIZE) 1877 1.8 christos return false; 1878 1.1 christos } 1879 1.8 christos return true; 1880 1.1 christos } 1881 1.1 christos 1882 1.8 christos static bool 1883 1.1 christos bfd_mach_o_write_section_32 (bfd *abfd, bfd_mach_o_section *section) 1884 1.1 christos { 1885 1.1 christos struct mach_o_section_32_external raw; 1886 1.1 christos 1887 1.1 christos memcpy (raw.sectname, section->sectname, 16); 1888 1.1 christos memcpy (raw.segname, section->segname, 16); 1889 1.1 christos bfd_h_put_32 (abfd, section->addr, raw.addr); 1890 1.1 christos bfd_h_put_32 (abfd, section->size, raw.size); 1891 1.1 christos bfd_h_put_32 (abfd, section->offset, raw.offset); 1892 1.1 christos bfd_h_put_32 (abfd, section->align, raw.align); 1893 1.1 christos bfd_h_put_32 (abfd, section->reloff, raw.reloff); 1894 1.1 christos bfd_h_put_32 (abfd, section->nreloc, raw.nreloc); 1895 1.1 christos bfd_h_put_32 (abfd, section->flags, raw.flags); 1896 1.1 christos bfd_h_put_32 (abfd, section->reserved1, raw.reserved1); 1897 1.1 christos bfd_h_put_32 (abfd, section->reserved2, raw.reserved2); 1898 1.1 christos 1899 1.9 christos if (bfd_write (&raw, BFD_MACH_O_SECTION_SIZE, abfd) 1900 1.1 christos != BFD_MACH_O_SECTION_SIZE) 1901 1.8 christos return false; 1902 1.1 christos 1903 1.8 christos return true; 1904 1.1 christos } 1905 1.1 christos 1906 1.8 christos static bool 1907 1.1 christos bfd_mach_o_write_section_64 (bfd *abfd, bfd_mach_o_section *section) 1908 1.1 christos { 1909 1.1 christos struct mach_o_section_64_external raw; 1910 1.1 christos 1911 1.1 christos memcpy (raw.sectname, section->sectname, 16); 1912 1.1 christos memcpy (raw.segname, section->segname, 16); 1913 1.1 christos bfd_h_put_64 (abfd, section->addr, raw.addr); 1914 1.1 christos bfd_h_put_64 (abfd, section->size, raw.size); 1915 1.1 christos bfd_h_put_32 (abfd, section->offset, raw.offset); 1916 1.1 christos bfd_h_put_32 (abfd, section->align, raw.align); 1917 1.1 christos bfd_h_put_32 (abfd, section->reloff, raw.reloff); 1918 1.1 christos bfd_h_put_32 (abfd, section->nreloc, raw.nreloc); 1919 1.1 christos bfd_h_put_32 (abfd, section->flags, raw.flags); 1920 1.1 christos bfd_h_put_32 (abfd, section->reserved1, raw.reserved1); 1921 1.1 christos bfd_h_put_32 (abfd, section->reserved2, raw.reserved2); 1922 1.1 christos bfd_h_put_32 (abfd, section->reserved3, raw.reserved3); 1923 1.1 christos 1924 1.9 christos if (bfd_write (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd) 1925 1.1 christos != BFD_MACH_O_SECTION_64_SIZE) 1926 1.8 christos return false; 1927 1.1 christos 1928 1.8 christos return true; 1929 1.1 christos } 1930 1.1 christos 1931 1.8 christos static bool 1932 1.1 christos bfd_mach_o_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command) 1933 1.1 christos { 1934 1.1 christos struct mach_o_segment_command_32_external raw; 1935 1.1 christos bfd_mach_o_segment_command *seg = &command->command.segment; 1936 1.1 christos bfd_mach_o_section *sec; 1937 1.1 christos 1938 1.1 christos BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT); 1939 1.1 christos 1940 1.1 christos for (sec = seg->sect_head; sec != NULL; sec = sec->next) 1941 1.1 christos if (!bfd_mach_o_write_relocs (abfd, sec)) 1942 1.8 christos return false; 1943 1.1 christos 1944 1.1 christos memcpy (raw.segname, seg->segname, 16); 1945 1.1 christos bfd_h_put_32 (abfd, seg->vmaddr, raw.vmaddr); 1946 1.1 christos bfd_h_put_32 (abfd, seg->vmsize, raw.vmsize); 1947 1.1 christos bfd_h_put_32 (abfd, seg->fileoff, raw.fileoff); 1948 1.1 christos bfd_h_put_32 (abfd, seg->filesize, raw.filesize); 1949 1.1 christos bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot); 1950 1.1 christos bfd_h_put_32 (abfd, seg->initprot, raw.initprot); 1951 1.1 christos bfd_h_put_32 (abfd, seg->nsects, raw.nsects); 1952 1.1 christos bfd_h_put_32 (abfd, seg->flags, raw.flags); 1953 1.3 christos 1954 1.1 christos if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0 1955 1.9 christos || bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw)) 1956 1.8 christos return false; 1957 1.1 christos 1958 1.1 christos for (sec = seg->sect_head; sec != NULL; sec = sec->next) 1959 1.3 christos if (!bfd_mach_o_write_section_32 (abfd, sec)) 1960 1.8 christos return false; 1961 1.1 christos 1962 1.8 christos return true; 1963 1.1 christos } 1964 1.1 christos 1965 1.8 christos static bool 1966 1.1 christos bfd_mach_o_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command) 1967 1.1 christos { 1968 1.1 christos struct mach_o_segment_command_64_external raw; 1969 1.1 christos bfd_mach_o_segment_command *seg = &command->command.segment; 1970 1.1 christos bfd_mach_o_section *sec; 1971 1.1 christos 1972 1.1 christos BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64); 1973 1.1 christos 1974 1.1 christos for (sec = seg->sect_head; sec != NULL; sec = sec->next) 1975 1.1 christos if (!bfd_mach_o_write_relocs (abfd, sec)) 1976 1.8 christos return false; 1977 1.1 christos 1978 1.1 christos memcpy (raw.segname, seg->segname, 16); 1979 1.1 christos bfd_h_put_64 (abfd, seg->vmaddr, raw.vmaddr); 1980 1.1 christos bfd_h_put_64 (abfd, seg->vmsize, raw.vmsize); 1981 1.1 christos bfd_h_put_64 (abfd, seg->fileoff, raw.fileoff); 1982 1.1 christos bfd_h_put_64 (abfd, seg->filesize, raw.filesize); 1983 1.1 christos bfd_h_put_32 (abfd, seg->maxprot, raw.maxprot); 1984 1.1 christos bfd_h_put_32 (abfd, seg->initprot, raw.initprot); 1985 1.1 christos bfd_h_put_32 (abfd, seg->nsects, raw.nsects); 1986 1.1 christos bfd_h_put_32 (abfd, seg->flags, raw.flags); 1987 1.1 christos 1988 1.1 christos if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0 1989 1.9 christos || bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw)) 1990 1.8 christos return false; 1991 1.1 christos 1992 1.1 christos for (sec = seg->sect_head; sec != NULL; sec = sec->next) 1993 1.3 christos if (!bfd_mach_o_write_section_64 (abfd, sec)) 1994 1.8 christos return false; 1995 1.1 christos 1996 1.8 christos return true; 1997 1.1 christos } 1998 1.1 christos 1999 1.8 christos static bool 2000 1.3 christos bfd_mach_o_write_symtab_content (bfd *abfd, bfd_mach_o_symtab_command *sym) 2001 1.1 christos { 2002 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 2003 1.1 christos unsigned long i; 2004 1.1 christos unsigned int wide = bfd_mach_o_wide_p (abfd); 2005 1.1 christos struct bfd_strtab_hash *strtab; 2006 1.1 christos asymbol **symbols = bfd_get_outsymbols (abfd); 2007 1.3 christos int padlen; 2008 1.1 christos 2009 1.1 christos /* Write the symbols first. */ 2010 1.1 christos if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0) 2011 1.8 christos return false; 2012 1.1 christos 2013 1.1 christos strtab = _bfd_stringtab_init (); 2014 1.1 christos if (strtab == NULL) 2015 1.8 christos return false; 2016 1.1 christos 2017 1.1 christos if (sym->nsyms > 0) 2018 1.1 christos /* Although we don't strictly need to do this, for compatibility with 2019 1.1 christos Darwin system tools, actually output an empty string for the index 2020 1.1 christos 0 entry. */ 2021 1.8 christos _bfd_stringtab_add (strtab, "", true, false); 2022 1.1 christos 2023 1.1 christos for (i = 0; i < sym->nsyms; i++) 2024 1.1 christos { 2025 1.1 christos bfd_size_type str_index; 2026 1.1 christos bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i]; 2027 1.3 christos 2028 1.1 christos if (s->symbol.name == 0 || s->symbol.name[0] == '\0') 2029 1.1 christos /* An index of 0 always means the empty string. */ 2030 1.6 christos str_index = 0; 2031 1.1 christos else 2032 1.6 christos { 2033 1.8 christos str_index = _bfd_stringtab_add (strtab, s->symbol.name, true, false); 2034 1.1 christos 2035 1.6 christos if (str_index == (bfd_size_type) -1) 2036 1.6 christos goto err; 2037 1.6 christos } 2038 1.1 christos 2039 1.1 christos if (wide) 2040 1.6 christos { 2041 1.6 christos struct mach_o_nlist_64_external raw; 2042 1.1 christos 2043 1.6 christos bfd_h_put_32 (abfd, str_index, raw.n_strx); 2044 1.6 christos bfd_h_put_8 (abfd, s->n_type, raw.n_type); 2045 1.6 christos bfd_h_put_8 (abfd, s->n_sect, raw.n_sect); 2046 1.6 christos bfd_h_put_16 (abfd, s->n_desc, raw.n_desc); 2047 1.6 christos bfd_h_put_64 (abfd, s->symbol.section->vma + s->symbol.value, 2048 1.6 christos raw.n_value); 2049 1.6 christos 2050 1.9 christos if (bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw)) 2051 1.6 christos goto err; 2052 1.6 christos } 2053 1.1 christos else 2054 1.6 christos { 2055 1.6 christos struct mach_o_nlist_external raw; 2056 1.6 christos 2057 1.6 christos bfd_h_put_32 (abfd, str_index, raw.n_strx); 2058 1.6 christos bfd_h_put_8 (abfd, s->n_type, raw.n_type); 2059 1.6 christos bfd_h_put_8 (abfd, s->n_sect, raw.n_sect); 2060 1.6 christos bfd_h_put_16 (abfd, s->n_desc, raw.n_desc); 2061 1.6 christos bfd_h_put_32 (abfd, s->symbol.section->vma + s->symbol.value, 2062 1.6 christos raw.n_value); 2063 1.1 christos 2064 1.9 christos if (bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw)) 2065 1.6 christos goto err; 2066 1.6 christos } 2067 1.1 christos } 2068 1.1 christos sym->strsize = _bfd_stringtab_size (strtab); 2069 1.1 christos sym->stroff = mdata->filelen; 2070 1.1 christos mdata->filelen += sym->strsize; 2071 1.1 christos 2072 1.3 christos if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0) 2073 1.3 christos goto err; 2074 1.3 christos 2075 1.6 christos if (!_bfd_stringtab_emit (abfd, strtab)) 2076 1.1 christos goto err; 2077 1.1 christos 2078 1.9 christos _bfd_stringtab_free (strtab); 2079 1.9 christos 2080 1.3 christos /* Pad string table. */ 2081 1.3 christos padlen = bfd_mach_o_pad4 (abfd, sym->strsize); 2082 1.3 christos if (padlen < 0) 2083 1.8 christos return false; 2084 1.3 christos mdata->filelen += padlen; 2085 1.3 christos sym->strsize += padlen; 2086 1.1 christos 2087 1.8 christos return true; 2088 1.1 christos 2089 1.1 christos err: 2090 1.1 christos _bfd_stringtab_free (strtab); 2091 1.3 christos sym->strsize = 0; 2092 1.8 christos return false; 2093 1.1 christos } 2094 1.1 christos 2095 1.8 christos static bool 2096 1.3 christos bfd_mach_o_write_symtab (bfd *abfd, bfd_mach_o_load_command *command) 2097 1.1 christos { 2098 1.3 christos bfd_mach_o_symtab_command *sym = &command->command.symtab; 2099 1.3 christos struct mach_o_symtab_command_external raw; 2100 1.1 christos 2101 1.3 christos BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB); 2102 1.3 christos 2103 1.3 christos /* The command. */ 2104 1.3 christos bfd_h_put_32 (abfd, sym->symoff, raw.symoff); 2105 1.3 christos bfd_h_put_32 (abfd, sym->nsyms, raw.nsyms); 2106 1.3 christos bfd_h_put_32 (abfd, sym->stroff, raw.stroff); 2107 1.3 christos bfd_h_put_32 (abfd, sym->strsize, raw.strsize); 2108 1.3 christos 2109 1.3 christos if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0 2110 1.9 christos || bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw)) 2111 1.8 christos return false; 2112 1.3 christos 2113 1.8 christos return true; 2114 1.3 christos } 2115 1.3 christos 2116 1.3 christos /* Count the number of indirect symbols in the image. 2117 1.3 christos Requires that the sections are in their final order. */ 2118 1.3 christos 2119 1.3 christos static unsigned int 2120 1.3 christos bfd_mach_o_count_indirect_symbols (bfd *abfd, bfd_mach_o_data_struct *mdata) 2121 1.3 christos { 2122 1.3 christos unsigned int i; 2123 1.3 christos unsigned int nisyms = 0; 2124 1.3 christos 2125 1.3 christos for (i = 0; i < mdata->nsects; ++i) 2126 1.3 christos { 2127 1.3 christos bfd_mach_o_section *sec = mdata->sections[i]; 2128 1.3 christos 2129 1.3 christos switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK) 2130 1.3 christos { 2131 1.3 christos case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS: 2132 1.3 christos case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS: 2133 1.3 christos case BFD_MACH_O_S_SYMBOL_STUBS: 2134 1.3 christos nisyms += bfd_mach_o_section_get_nbr_indirect (abfd, sec); 2135 1.3 christos break; 2136 1.3 christos default: 2137 1.3 christos break; 2138 1.3 christos } 2139 1.3 christos } 2140 1.3 christos return nisyms; 2141 1.3 christos } 2142 1.3 christos 2143 1.3 christos /* Create the dysymtab. */ 2144 1.3 christos 2145 1.8 christos static bool 2146 1.3 christos bfd_mach_o_build_dysymtab (bfd *abfd, bfd_mach_o_dysymtab_command *cmd) 2147 1.3 christos { 2148 1.3 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 2149 1.3 christos 2150 1.3 christos /* TODO: 2151 1.3 christos We are not going to try and fill these in yet and, moreover, we are 2152 1.3 christos going to bail if they are already set. */ 2153 1.3 christos if (cmd->nmodtab != 0 2154 1.3 christos || cmd->ntoc != 0 2155 1.3 christos || cmd->nextrefsyms != 0) 2156 1.3 christos { 2157 1.6 christos _bfd_error_handler (_("sorry: modtab, toc and extrefsyms are not yet" 2158 1.6 christos " implemented for dysymtab commands.")); 2159 1.8 christos return false; 2160 1.3 christos } 2161 1.3 christos 2162 1.3 christos cmd->ilocalsym = 0; 2163 1.3 christos 2164 1.3 christos if (bfd_get_symcount (abfd) > 0) 2165 1.3 christos { 2166 1.3 christos asymbol **symbols = bfd_get_outsymbols (abfd); 2167 1.3 christos unsigned long i; 2168 1.3 christos 2169 1.3 christos /* Count the number of each kind of symbol. */ 2170 1.3 christos for (i = 0; i < bfd_get_symcount (abfd); ++i) 2171 1.3 christos { 2172 1.3 christos bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i]; 2173 1.3 christos if (s->n_type & (BFD_MACH_O_N_EXT | BFD_MACH_O_N_PEXT)) 2174 1.3 christos break; 2175 1.3 christos } 2176 1.3 christos cmd->nlocalsym = i; 2177 1.3 christos cmd->iextdefsym = i; 2178 1.3 christos for (; i < bfd_get_symcount (abfd); ++i) 2179 1.3 christos { 2180 1.3 christos bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i]; 2181 1.3 christos if ((s->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_UNDF) 2182 1.3 christos break; 2183 1.3 christos } 2184 1.3 christos cmd->nextdefsym = i - cmd->nlocalsym; 2185 1.3 christos cmd->iundefsym = cmd->nextdefsym + cmd->iextdefsym; 2186 1.3 christos cmd->nundefsym = bfd_get_symcount (abfd) 2187 1.3 christos - cmd->nlocalsym 2188 1.3 christos - cmd->nextdefsym; 2189 1.3 christos } 2190 1.3 christos else 2191 1.3 christos { 2192 1.3 christos cmd->nlocalsym = 0; 2193 1.3 christos cmd->iextdefsym = 0; 2194 1.3 christos cmd->nextdefsym = 0; 2195 1.3 christos cmd->iundefsym = 0; 2196 1.3 christos cmd->nundefsym = 0; 2197 1.3 christos } 2198 1.3 christos 2199 1.3 christos cmd->nindirectsyms = bfd_mach_o_count_indirect_symbols (abfd, mdata); 2200 1.3 christos if (cmd->nindirectsyms > 0) 2201 1.3 christos { 2202 1.3 christos unsigned i; 2203 1.3 christos unsigned n; 2204 1.8 christos size_t amt; 2205 1.3 christos 2206 1.3 christos mdata->filelen = FILE_ALIGN (mdata->filelen, 2); 2207 1.3 christos cmd->indirectsymoff = mdata->filelen; 2208 1.8 christos if (_bfd_mul_overflow (cmd->nindirectsyms, 4, &amt)) 2209 1.8 christos return false; 2210 1.8 christos mdata->filelen += amt; 2211 1.3 christos 2212 1.8 christos cmd->indirect_syms = bfd_zalloc (abfd, amt); 2213 1.3 christos if (cmd->indirect_syms == NULL) 2214 1.8 christos return false; 2215 1.3 christos 2216 1.3 christos n = 0; 2217 1.3 christos for (i = 0; i < mdata->nsects; ++i) 2218 1.3 christos { 2219 1.3 christos bfd_mach_o_section *sec = mdata->sections[i]; 2220 1.3 christos 2221 1.3 christos switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK) 2222 1.3 christos { 2223 1.3 christos case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS: 2224 1.3 christos case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS: 2225 1.3 christos case BFD_MACH_O_S_SYMBOL_STUBS: 2226 1.3 christos { 2227 1.3 christos unsigned j, num; 2228 1.3 christos bfd_mach_o_asymbol **isyms = sec->indirect_syms; 2229 1.3 christos 2230 1.3 christos num = bfd_mach_o_section_get_nbr_indirect (abfd, sec); 2231 1.3 christos if (isyms == NULL || num == 0) 2232 1.3 christos break; 2233 1.3 christos /* Record the starting index in the reserved1 field. */ 2234 1.3 christos sec->reserved1 = n; 2235 1.3 christos for (j = 0; j < num; j++, n++) 2236 1.3 christos { 2237 1.3 christos if (isyms[j] == NULL) 2238 1.6 christos cmd->indirect_syms[n] = BFD_MACH_O_INDIRECT_SYM_LOCAL; 2239 1.3 christos else if (isyms[j]->symbol.section == bfd_abs_section_ptr 2240 1.3 christos && ! (isyms[j]->n_type & BFD_MACH_O_N_EXT)) 2241 1.6 christos cmd->indirect_syms[n] = BFD_MACH_O_INDIRECT_SYM_LOCAL 2242 1.3 christos | BFD_MACH_O_INDIRECT_SYM_ABS; 2243 1.3 christos else 2244 1.6 christos cmd->indirect_syms[n] = isyms[j]->symbol.udata.i; 2245 1.3 christos } 2246 1.3 christos } 2247 1.3 christos break; 2248 1.3 christos default: 2249 1.3 christos break; 2250 1.3 christos } 2251 1.3 christos } 2252 1.3 christos } 2253 1.3 christos 2254 1.8 christos return true; 2255 1.3 christos } 2256 1.3 christos 2257 1.3 christos /* Write a dysymtab command. 2258 1.3 christos TODO: Possibly coalesce writes of smaller objects. */ 2259 1.3 christos 2260 1.8 christos static bool 2261 1.3 christos bfd_mach_o_write_dysymtab (bfd *abfd, bfd_mach_o_load_command *command) 2262 1.3 christos { 2263 1.3 christos bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab; 2264 1.3 christos 2265 1.3 christos BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB); 2266 1.1 christos 2267 1.1 christos if (cmd->nmodtab != 0) 2268 1.1 christos { 2269 1.1 christos unsigned int i; 2270 1.1 christos 2271 1.1 christos if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0) 2272 1.8 christos return false; 2273 1.1 christos 2274 1.1 christos for (i = 0; i < cmd->nmodtab; i++) 2275 1.1 christos { 2276 1.1 christos bfd_mach_o_dylib_module *module = &cmd->dylib_module[i]; 2277 1.1 christos unsigned int iinit; 2278 1.1 christos unsigned int ninit; 2279 1.1 christos 2280 1.1 christos iinit = module->iinit & 0xffff; 2281 1.1 christos iinit |= ((module->iterm & 0xffff) << 16); 2282 1.1 christos 2283 1.1 christos ninit = module->ninit & 0xffff; 2284 1.1 christos ninit |= ((module->nterm & 0xffff) << 16); 2285 1.1 christos 2286 1.1 christos if (bfd_mach_o_wide_p (abfd)) 2287 1.1 christos { 2288 1.1 christos struct mach_o_dylib_module_64_external w; 2289 1.1 christos 2290 1.1 christos bfd_h_put_32 (abfd, module->module_name_idx, &w.module_name); 2291 1.1 christos bfd_h_put_32 (abfd, module->iextdefsym, &w.iextdefsym); 2292 1.1 christos bfd_h_put_32 (abfd, module->nextdefsym, &w.nextdefsym); 2293 1.1 christos bfd_h_put_32 (abfd, module->irefsym, &w.irefsym); 2294 1.1 christos bfd_h_put_32 (abfd, module->nrefsym, &w.nrefsym); 2295 1.1 christos bfd_h_put_32 (abfd, module->ilocalsym, &w.ilocalsym); 2296 1.1 christos bfd_h_put_32 (abfd, module->nlocalsym, &w.nlocalsym); 2297 1.1 christos bfd_h_put_32 (abfd, module->iextrel, &w.iextrel); 2298 1.1 christos bfd_h_put_32 (abfd, module->nextrel, &w.nextrel); 2299 1.1 christos bfd_h_put_32 (abfd, iinit, &w.iinit_iterm); 2300 1.1 christos bfd_h_put_32 (abfd, ninit, &w.ninit_nterm); 2301 1.1 christos bfd_h_put_64 (abfd, module->objc_module_info_addr, 2302 1.1 christos &w.objc_module_info_addr); 2303 1.1 christos bfd_h_put_32 (abfd, module->objc_module_info_size, 2304 1.1 christos &w.objc_module_info_size); 2305 1.1 christos 2306 1.9 christos if (bfd_write (&w, sizeof (w), abfd) != sizeof (w)) 2307 1.8 christos return false; 2308 1.1 christos } 2309 1.1 christos else 2310 1.1 christos { 2311 1.1 christos struct mach_o_dylib_module_external n; 2312 1.1 christos 2313 1.1 christos bfd_h_put_32 (abfd, module->module_name_idx, &n.module_name); 2314 1.1 christos bfd_h_put_32 (abfd, module->iextdefsym, &n.iextdefsym); 2315 1.1 christos bfd_h_put_32 (abfd, module->nextdefsym, &n.nextdefsym); 2316 1.1 christos bfd_h_put_32 (abfd, module->irefsym, &n.irefsym); 2317 1.1 christos bfd_h_put_32 (abfd, module->nrefsym, &n.nrefsym); 2318 1.1 christos bfd_h_put_32 (abfd, module->ilocalsym, &n.ilocalsym); 2319 1.1 christos bfd_h_put_32 (abfd, module->nlocalsym, &n.nlocalsym); 2320 1.1 christos bfd_h_put_32 (abfd, module->iextrel, &n.iextrel); 2321 1.1 christos bfd_h_put_32 (abfd, module->nextrel, &n.nextrel); 2322 1.1 christos bfd_h_put_32 (abfd, iinit, &n.iinit_iterm); 2323 1.1 christos bfd_h_put_32 (abfd, ninit, &n.ninit_nterm); 2324 1.1 christos bfd_h_put_32 (abfd, module->objc_module_info_addr, 2325 1.1 christos &n.objc_module_info_addr); 2326 1.1 christos bfd_h_put_32 (abfd, module->objc_module_info_size, 2327 1.1 christos &n.objc_module_info_size); 2328 1.1 christos 2329 1.9 christos if (bfd_write (&n, sizeof (n), abfd) != sizeof (n)) 2330 1.8 christos return false; 2331 1.1 christos } 2332 1.1 christos } 2333 1.1 christos } 2334 1.1 christos 2335 1.1 christos if (cmd->ntoc != 0) 2336 1.1 christos { 2337 1.1 christos unsigned int i; 2338 1.1 christos 2339 1.1 christos if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0) 2340 1.8 christos return false; 2341 1.1 christos 2342 1.1 christos for (i = 0; i < cmd->ntoc; i++) 2343 1.1 christos { 2344 1.1 christos struct mach_o_dylib_table_of_contents_external raw; 2345 1.1 christos bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i]; 2346 1.1 christos 2347 1.1 christos bfd_h_put_32 (abfd, toc->symbol_index, &raw.symbol_index); 2348 1.1 christos bfd_h_put_32 (abfd, toc->module_index, &raw.module_index); 2349 1.1 christos 2350 1.9 christos if (bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw)) 2351 1.8 christos return false; 2352 1.1 christos } 2353 1.1 christos } 2354 1.3 christos 2355 1.1 christos if (cmd->nindirectsyms > 0) 2356 1.1 christos { 2357 1.1 christos unsigned int i; 2358 1.1 christos 2359 1.1 christos if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0) 2360 1.8 christos return false; 2361 1.1 christos 2362 1.1 christos for (i = 0; i < cmd->nindirectsyms; ++i) 2363 1.1 christos { 2364 1.1 christos unsigned char raw[4]; 2365 1.1 christos 2366 1.1 christos bfd_h_put_32 (abfd, cmd->indirect_syms[i], &raw); 2367 1.9 christos if (bfd_write (raw, sizeof (raw), abfd) != sizeof (raw)) 2368 1.8 christos return false; 2369 1.3 christos } 2370 1.1 christos } 2371 1.1 christos 2372 1.1 christos if (cmd->nextrefsyms != 0) 2373 1.1 christos { 2374 1.1 christos unsigned int i; 2375 1.1 christos 2376 1.1 christos if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0) 2377 1.8 christos return false; 2378 1.1 christos 2379 1.1 christos for (i = 0; i < cmd->nextrefsyms; i++) 2380 1.1 christos { 2381 1.1 christos unsigned long v; 2382 1.1 christos unsigned char raw[4]; 2383 1.1 christos bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i]; 2384 1.1 christos 2385 1.1 christos /* Fields isym and flags are written as bit-fields, thus we need 2386 1.1 christos a specific processing for endianness. */ 2387 1.1 christos 2388 1.1 christos if (bfd_big_endian (abfd)) 2389 1.1 christos { 2390 1.1 christos v = ((ref->isym & 0xffffff) << 8); 2391 1.1 christos v |= ref->flags & 0xff; 2392 1.1 christos } 2393 1.1 christos else 2394 1.1 christos { 2395 1.1 christos v = ref->isym & 0xffffff; 2396 1.1 christos v |= ((ref->flags & 0xff) << 24); 2397 1.1 christos } 2398 1.1 christos 2399 1.1 christos bfd_h_put_32 (abfd, v, raw); 2400 1.9 christos if (bfd_write (raw, sizeof (raw), abfd) != sizeof (raw)) 2401 1.8 christos return false; 2402 1.1 christos } 2403 1.1 christos } 2404 1.1 christos 2405 1.1 christos /* The command. */ 2406 1.1 christos if (bfd_seek (abfd, command->offset + BFD_MACH_O_LC_SIZE, SEEK_SET) != 0) 2407 1.8 christos return false; 2408 1.1 christos else 2409 1.1 christos { 2410 1.1 christos struct mach_o_dysymtab_command_external raw; 2411 1.1 christos 2412 1.1 christos bfd_h_put_32 (abfd, cmd->ilocalsym, &raw.ilocalsym); 2413 1.1 christos bfd_h_put_32 (abfd, cmd->nlocalsym, &raw.nlocalsym); 2414 1.1 christos bfd_h_put_32 (abfd, cmd->iextdefsym, &raw.iextdefsym); 2415 1.1 christos bfd_h_put_32 (abfd, cmd->nextdefsym, &raw.nextdefsym); 2416 1.1 christos bfd_h_put_32 (abfd, cmd->iundefsym, &raw.iundefsym); 2417 1.1 christos bfd_h_put_32 (abfd, cmd->nundefsym, &raw.nundefsym); 2418 1.1 christos bfd_h_put_32 (abfd, cmd->tocoff, &raw.tocoff); 2419 1.1 christos bfd_h_put_32 (abfd, cmd->ntoc, &raw.ntoc); 2420 1.1 christos bfd_h_put_32 (abfd, cmd->modtaboff, &raw.modtaboff); 2421 1.1 christos bfd_h_put_32 (abfd, cmd->nmodtab, &raw.nmodtab); 2422 1.1 christos bfd_h_put_32 (abfd, cmd->extrefsymoff, &raw.extrefsymoff); 2423 1.1 christos bfd_h_put_32 (abfd, cmd->nextrefsyms, &raw.nextrefsyms); 2424 1.1 christos bfd_h_put_32 (abfd, cmd->indirectsymoff, &raw.indirectsymoff); 2425 1.1 christos bfd_h_put_32 (abfd, cmd->nindirectsyms, &raw.nindirectsyms); 2426 1.1 christos bfd_h_put_32 (abfd, cmd->extreloff, &raw.extreloff); 2427 1.1 christos bfd_h_put_32 (abfd, cmd->nextrel, &raw.nextrel); 2428 1.1 christos bfd_h_put_32 (abfd, cmd->locreloff, &raw.locreloff); 2429 1.1 christos bfd_h_put_32 (abfd, cmd->nlocrel, &raw.nlocrel); 2430 1.1 christos 2431 1.9 christos if (bfd_write (&raw, sizeof (raw), abfd) != sizeof (raw)) 2432 1.8 christos return false; 2433 1.1 christos } 2434 1.1 christos 2435 1.8 christos return true; 2436 1.1 christos } 2437 1.1 christos 2438 1.1 christos static unsigned 2439 1.1 christos bfd_mach_o_primary_symbol_sort_key (bfd_mach_o_asymbol *s) 2440 1.1 christos { 2441 1.1 christos unsigned mtyp = s->n_type & BFD_MACH_O_N_TYPE; 2442 1.1 christos 2443 1.1 christos /* Just leave debug symbols where they are (pretend they are local, and 2444 1.1 christos then they will just be sorted on position). */ 2445 1.1 christos if (s->n_type & BFD_MACH_O_N_STAB) 2446 1.1 christos return 0; 2447 1.1 christos 2448 1.1 christos /* Local (we should never see an undefined local AFAICT). */ 2449 1.1 christos if (! (s->n_type & (BFD_MACH_O_N_EXT | BFD_MACH_O_N_PEXT))) 2450 1.1 christos return 0; 2451 1.1 christos 2452 1.1 christos /* Common symbols look like undefined externs. */ 2453 1.1 christos if (mtyp == BFD_MACH_O_N_UNDF) 2454 1.1 christos return 2; 2455 1.1 christos 2456 1.1 christos /* A defined non-local, non-debug symbol. */ 2457 1.1 christos return 1; 2458 1.1 christos } 2459 1.1 christos 2460 1.1 christos static int 2461 1.1 christos bfd_mach_o_cf_symbols (const void *a, const void *b) 2462 1.1 christos { 2463 1.1 christos bfd_mach_o_asymbol *sa = *(bfd_mach_o_asymbol **) a; 2464 1.1 christos bfd_mach_o_asymbol *sb = *(bfd_mach_o_asymbol **) b; 2465 1.1 christos unsigned int soa, sob; 2466 1.1 christos 2467 1.1 christos soa = bfd_mach_o_primary_symbol_sort_key (sa); 2468 1.1 christos sob = bfd_mach_o_primary_symbol_sort_key (sb); 2469 1.1 christos if (soa < sob) 2470 1.1 christos return -1; 2471 1.1 christos 2472 1.1 christos if (soa > sob) 2473 1.1 christos return 1; 2474 1.1 christos 2475 1.1 christos /* If it's local or stab, just preserve the input order. */ 2476 1.1 christos if (soa == 0) 2477 1.1 christos { 2478 1.1 christos if (sa->symbol.udata.i < sb->symbol.udata.i) 2479 1.6 christos return -1; 2480 1.1 christos if (sa->symbol.udata.i > sb->symbol.udata.i) 2481 1.6 christos return 1; 2482 1.1 christos 2483 1.1 christos /* This is probably an error. */ 2484 1.1 christos return 0; 2485 1.1 christos } 2486 1.1 christos 2487 1.1 christos /* The second sort key is name. */ 2488 1.1 christos return strcmp (sa->symbol.name, sb->symbol.name); 2489 1.1 christos } 2490 1.1 christos 2491 1.1 christos /* Process the symbols. 2492 1.1 christos 2493 1.1 christos This should be OK for single-module files - but it is not likely to work 2494 1.1 christos for multi-module shared libraries. 2495 1.1 christos 2496 1.1 christos (a) If the application has not filled in the relevant mach-o fields, make 2497 1.1 christos an estimate. 2498 1.1 christos 2499 1.1 christos (b) Order them, like this: 2500 1.1 christos ( i) local. 2501 1.1 christos (unsorted) 2502 1.1 christos ( ii) external defined 2503 1.1 christos (by name) 2504 1.1 christos (iii) external undefined/common 2505 1.1 christos (by name) 2506 1.1 christos ( iv) common 2507 1.1 christos (by name) 2508 1.1 christos */ 2509 1.1 christos 2510 1.8 christos static bool 2511 1.1 christos bfd_mach_o_mangle_symbols (bfd *abfd) 2512 1.1 christos { 2513 1.1 christos unsigned long i; 2514 1.1 christos asymbol **symbols = bfd_get_outsymbols (abfd); 2515 1.1 christos 2516 1.1 christos if (symbols == NULL || bfd_get_symcount (abfd) == 0) 2517 1.8 christos return true; 2518 1.1 christos 2519 1.1 christos for (i = 0; i < bfd_get_symcount (abfd); i++) 2520 1.1 christos { 2521 1.1 christos bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i]; 2522 1.1 christos 2523 1.1 christos /* We use this value, which is out-of-range as a symbol index, to signal 2524 1.1 christos that the mach-o-specific data are not filled in and need to be created 2525 1.1 christos from the bfd values. It is much preferable for the application to do 2526 1.1 christos this, since more meaningful diagnostics can be made that way. */ 2527 1.1 christos 2528 1.1 christos if (s->symbol.udata.i == SYM_MACHO_FIELDS_UNSET) 2529 1.6 christos { 2530 1.6 christos /* No symbol information has been set - therefore determine 2531 1.6 christos it from the bfd symbol flags/info. */ 2532 1.6 christos if (s->symbol.section == bfd_abs_section_ptr) 2533 1.6 christos s->n_type = BFD_MACH_O_N_ABS; 2534 1.6 christos else if (s->symbol.section == bfd_und_section_ptr) 2535 1.6 christos { 2536 1.6 christos s->n_type = BFD_MACH_O_N_UNDF; 2537 1.6 christos if (s->symbol.flags & BSF_WEAK) 2538 1.6 christos s->n_desc |= BFD_MACH_O_N_WEAK_REF; 2539 1.6 christos /* mach-o automatically makes undefined symbols extern. */ 2540 1.1 christos s->n_type |= BFD_MACH_O_N_EXT; 2541 1.1 christos s->symbol.flags |= BSF_GLOBAL; 2542 1.6 christos } 2543 1.6 christos else if (s->symbol.section == bfd_com_section_ptr) 2544 1.1 christos { 2545 1.6 christos s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT; 2546 1.6 christos s->symbol.flags |= BSF_GLOBAL; 2547 1.6 christos } 2548 1.6 christos else 2549 1.6 christos s->n_type = BFD_MACH_O_N_SECT; 2550 1.7 christos } 2551 1.6 christos 2552 1.7 christos /* Update external symbol bit in case objcopy changed it. */ 2553 1.7 christos if (s->symbol.flags & BSF_GLOBAL) 2554 1.7 christos s->n_type |= BFD_MACH_O_N_EXT; 2555 1.7 christos else 2556 1.7 christos s->n_type &= ~BFD_MACH_O_N_EXT; 2557 1.1 christos 2558 1.1 christos /* Put the section index in, where required. */ 2559 1.1 christos if ((s->symbol.section != bfd_abs_section_ptr 2560 1.6 christos && s->symbol.section != bfd_und_section_ptr 2561 1.6 christos && s->symbol.section != bfd_com_section_ptr) 2562 1.6 christos || ((s->n_type & BFD_MACH_O_N_STAB) != 0 2563 1.6 christos && s->symbol.name == NULL)) 2564 1.3 christos s->n_sect = s->symbol.section->output_section->target_index; 2565 1.1 christos 2566 1.1 christos /* Number to preserve order for local and debug syms. */ 2567 1.1 christos s->symbol.udata.i = i; 2568 1.1 christos } 2569 1.1 christos 2570 1.1 christos /* Sort the symbols. */ 2571 1.1 christos qsort ((void *) symbols, (size_t) bfd_get_symcount (abfd), 2572 1.1 christos sizeof (asymbol *), bfd_mach_o_cf_symbols); 2573 1.1 christos 2574 1.1 christos for (i = 0; i < bfd_get_symcount (abfd); ++i) 2575 1.1 christos { 2576 1.1 christos bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i]; 2577 1.1 christos s->symbol.udata.i = i; /* renumber. */ 2578 1.1 christos } 2579 1.1 christos 2580 1.8 christos return true; 2581 1.1 christos } 2582 1.1 christos 2583 1.1 christos /* We build a flat table of sections, which can be re-ordered if necessary. 2584 1.1 christos Fill in the section number and other mach-o-specific data. */ 2585 1.1 christos 2586 1.8 christos static bool 2587 1.1 christos bfd_mach_o_mangle_sections (bfd *abfd, bfd_mach_o_data_struct *mdata) 2588 1.1 christos { 2589 1.1 christos asection *sec; 2590 1.1 christos unsigned target_index; 2591 1.1 christos unsigned nsect; 2592 1.8 christos size_t amt; 2593 1.1 christos 2594 1.1 christos nsect = bfd_count_sections (abfd); 2595 1.3 christos 2596 1.1 christos /* Don't do it if it's already set - assume the application knows what it's 2597 1.1 christos doing. */ 2598 1.1 christos if (mdata->nsects == nsect 2599 1.1 christos && (mdata->nsects == 0 || mdata->sections != NULL)) 2600 1.8 christos return true; 2601 1.1 christos 2602 1.3 christos /* We need to check that this can be done... */ 2603 1.3 christos if (nsect > 255) 2604 1.3 christos { 2605 1.6 christos _bfd_error_handler (_("mach-o: there are too many sections (%u)" 2606 1.6 christos " maximum is 255,\n"), nsect); 2607 1.8 christos return false; 2608 1.3 christos } 2609 1.3 christos 2610 1.1 christos mdata->nsects = nsect; 2611 1.8 christos amt = mdata->nsects * sizeof (bfd_mach_o_section *); 2612 1.8 christos mdata->sections = bfd_alloc (abfd, amt); 2613 1.1 christos if (mdata->sections == NULL) 2614 1.8 christos return false; 2615 1.1 christos 2616 1.1 christos /* Create Mach-O sections. 2617 1.3 christos Section type, attribute and align should have been set when the 2618 1.1 christos section was created - either read in or specified. */ 2619 1.1 christos target_index = 0; 2620 1.1 christos for (sec = abfd->sections; sec; sec = sec->next) 2621 1.1 christos { 2622 1.7 christos unsigned bfd_align = bfd_section_alignment (sec); 2623 1.1 christos bfd_mach_o_section *msect = bfd_mach_o_get_mach_o_section (sec); 2624 1.1 christos 2625 1.1 christos mdata->sections[target_index] = msect; 2626 1.1 christos 2627 1.7 christos msect->addr = bfd_section_vma (sec); 2628 1.7 christos msect->size = bfd_section_size (sec); 2629 1.1 christos 2630 1.3 christos /* Use the largest alignment set, in case it was bumped after the 2631 1.1 christos section was created. */ 2632 1.1 christos msect->align = msect->align > bfd_align ? msect->align : bfd_align; 2633 1.1 christos 2634 1.1 christos msect->offset = 0; 2635 1.1 christos sec->target_index = ++target_index; 2636 1.1 christos } 2637 1.1 christos 2638 1.8 christos return true; 2639 1.1 christos } 2640 1.1 christos 2641 1.8 christos bool 2642 1.1 christos bfd_mach_o_write_contents (bfd *abfd) 2643 1.1 christos { 2644 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 2645 1.3 christos bfd_mach_o_load_command *cmd; 2646 1.3 christos bfd_mach_o_symtab_command *symtab = NULL; 2647 1.3 christos bfd_mach_o_dysymtab_command *dysymtab = NULL; 2648 1.3 christos bfd_mach_o_segment_command *linkedit = NULL; 2649 1.1 christos 2650 1.1 christos /* Make the commands, if not already present. */ 2651 1.3 christos if (!abfd->output_has_begun && !bfd_mach_o_build_commands (abfd)) 2652 1.8 christos return false; 2653 1.8 christos abfd->output_has_begun = true; 2654 1.1 christos 2655 1.3 christos /* Write the header. */ 2656 1.1 christos if (!bfd_mach_o_write_header (abfd, &mdata->header)) 2657 1.8 christos return false; 2658 1.1 christos 2659 1.3 christos /* First pass: allocate the linkedit segment. */ 2660 1.3 christos for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next) 2661 1.3 christos switch (cmd->type) 2662 1.3 christos { 2663 1.3 christos case BFD_MACH_O_LC_SEGMENT_64: 2664 1.3 christos case BFD_MACH_O_LC_SEGMENT: 2665 1.3 christos if (strcmp (cmd->command.segment.segname, "__LINKEDIT") == 0) 2666 1.3 christos linkedit = &cmd->command.segment; 2667 1.3 christos break; 2668 1.3 christos case BFD_MACH_O_LC_SYMTAB: 2669 1.3 christos symtab = &cmd->command.symtab; 2670 1.3 christos break; 2671 1.3 christos case BFD_MACH_O_LC_DYSYMTAB: 2672 1.3 christos dysymtab = &cmd->command.dysymtab; 2673 1.3 christos break; 2674 1.3 christos case BFD_MACH_O_LC_DYLD_INFO: 2675 1.3 christos { 2676 1.3 christos bfd_mach_o_dyld_info_command *di = &cmd->command.dyld_info; 2677 1.3 christos 2678 1.8 christos di->rebase_off = di->rebase_size != 0 ? mdata->filelen : 0; 2679 1.8 christos mdata->filelen += di->rebase_size; 2680 1.8 christos di->bind_off = di->bind_size != 0 ? mdata->filelen : 0; 2681 1.8 christos mdata->filelen += di->bind_size; 2682 1.8 christos di->weak_bind_off = di->weak_bind_size != 0 ? mdata->filelen : 0; 2683 1.8 christos mdata->filelen += di->weak_bind_size; 2684 1.8 christos di->lazy_bind_off = di->lazy_bind_size != 0 ? mdata->filelen : 0; 2685 1.8 christos mdata->filelen += di->lazy_bind_size; 2686 1.8 christos di->export_off = di->export_size != 0 ? mdata->filelen : 0; 2687 1.8 christos mdata->filelen += di->export_size; 2688 1.3 christos } 2689 1.3 christos break; 2690 1.3 christos case BFD_MACH_O_LC_LOAD_DYLIB: 2691 1.3 christos case BFD_MACH_O_LC_LOAD_DYLINKER: 2692 1.3 christos case BFD_MACH_O_LC_MAIN: 2693 1.3 christos /* Nothing to do. */ 2694 1.3 christos break; 2695 1.3 christos default: 2696 1.6 christos _bfd_error_handler 2697 1.6 christos (_("unable to allocate data for load command %#x"), 2698 1.6 christos cmd->type); 2699 1.3 christos break; 2700 1.3 christos } 2701 1.3 christos 2702 1.3 christos /* Specially handle symtab and dysymtab. */ 2703 1.3 christos 2704 1.3 christos /* Pre-allocate the symbol table (but not the string table). The reason 2705 1.3 christos is that the dysymtab is after the symbol table but before the string 2706 1.3 christos table (required by the native strip tool). */ 2707 1.3 christos if (symtab != NULL) 2708 1.3 christos { 2709 1.3 christos unsigned int symlen; 2710 1.3 christos unsigned int wide = bfd_mach_o_wide_p (abfd); 2711 1.3 christos 2712 1.3 christos symlen = wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE; 2713 1.3 christos 2714 1.3 christos /* Align for symbols. */ 2715 1.3 christos mdata->filelen = FILE_ALIGN (mdata->filelen, wide ? 3 : 2); 2716 1.3 christos symtab->symoff = mdata->filelen; 2717 1.3 christos 2718 1.3 christos symtab->nsyms = bfd_get_symcount (abfd); 2719 1.3 christos mdata->filelen += symtab->nsyms * symlen; 2720 1.3 christos } 2721 1.3 christos 2722 1.3 christos /* Build the dysymtab. */ 2723 1.3 christos if (dysymtab != NULL) 2724 1.3 christos if (!bfd_mach_o_build_dysymtab (abfd, dysymtab)) 2725 1.8 christos return false; 2726 1.3 christos 2727 1.3 christos /* Write symtab and strtab. */ 2728 1.3 christos if (symtab != NULL) 2729 1.3 christos if (!bfd_mach_o_write_symtab_content (abfd, symtab)) 2730 1.8 christos return false; 2731 1.3 christos 2732 1.3 christos /* Adjust linkedit size. */ 2733 1.3 christos if (linkedit != NULL) 2734 1.3 christos { 2735 1.3 christos /* bfd_vma pagemask = bfd_mach_o_get_backend_data (abfd)->page_size - 1; */ 2736 1.3 christos 2737 1.3 christos linkedit->vmsize = mdata->filelen - linkedit->fileoff; 2738 1.3 christos /* linkedit->vmsize = (linkedit->vmsize + pagemask) & ~pagemask; */ 2739 1.3 christos linkedit->filesize = mdata->filelen - linkedit->fileoff; 2740 1.3 christos 2741 1.3 christos linkedit->initprot = BFD_MACH_O_PROT_READ; 2742 1.3 christos linkedit->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE 2743 1.3 christos | BFD_MACH_O_PROT_EXECUTE; 2744 1.3 christos } 2745 1.3 christos 2746 1.3 christos /* Second pass: write commands. */ 2747 1.3 christos for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next) 2748 1.1 christos { 2749 1.1 christos struct mach_o_load_command_external raw; 2750 1.1 christos unsigned long typeflag; 2751 1.1 christos 2752 1.3 christos typeflag = cmd->type | (cmd->type_required ? BFD_MACH_O_LC_REQ_DYLD : 0); 2753 1.1 christos 2754 1.1 christos bfd_h_put_32 (abfd, typeflag, raw.cmd); 2755 1.3 christos bfd_h_put_32 (abfd, cmd->len, raw.cmdsize); 2756 1.1 christos 2757 1.3 christos if (bfd_seek (abfd, cmd->offset, SEEK_SET) != 0 2758 1.9 christos || bfd_write (&raw, BFD_MACH_O_LC_SIZE, abfd) != 8) 2759 1.8 christos return false; 2760 1.1 christos 2761 1.3 christos switch (cmd->type) 2762 1.1 christos { 2763 1.1 christos case BFD_MACH_O_LC_SEGMENT: 2764 1.3 christos if (!bfd_mach_o_write_segment_32 (abfd, cmd)) 2765 1.8 christos return false; 2766 1.1 christos break; 2767 1.1 christos case BFD_MACH_O_LC_SEGMENT_64: 2768 1.3 christos if (!bfd_mach_o_write_segment_64 (abfd, cmd)) 2769 1.8 christos return false; 2770 1.1 christos break; 2771 1.1 christos case BFD_MACH_O_LC_SYMTAB: 2772 1.3 christos if (!bfd_mach_o_write_symtab (abfd, cmd)) 2773 1.8 christos return false; 2774 1.1 christos break; 2775 1.1 christos case BFD_MACH_O_LC_DYSYMTAB: 2776 1.3 christos if (!bfd_mach_o_write_dysymtab (abfd, cmd)) 2777 1.8 christos return false; 2778 1.1 christos break; 2779 1.1 christos case BFD_MACH_O_LC_THREAD: 2780 1.1 christos case BFD_MACH_O_LC_UNIXTHREAD: 2781 1.3 christos if (!bfd_mach_o_write_thread (abfd, cmd)) 2782 1.8 christos return false; 2783 1.1 christos break; 2784 1.1 christos case BFD_MACH_O_LC_LOAD_DYLIB: 2785 1.3 christos if (!bfd_mach_o_write_dylib (abfd, cmd)) 2786 1.8 christos return false; 2787 1.3 christos break; 2788 1.1 christos case BFD_MACH_O_LC_LOAD_DYLINKER: 2789 1.3 christos if (!bfd_mach_o_write_dylinker (abfd, cmd)) 2790 1.8 christos return false; 2791 1.3 christos break; 2792 1.3 christos case BFD_MACH_O_LC_MAIN: 2793 1.3 christos if (!bfd_mach_o_write_main (abfd, cmd)) 2794 1.8 christos return false; 2795 1.3 christos break; 2796 1.3 christos case BFD_MACH_O_LC_DYLD_INFO: 2797 1.3 christos if (!bfd_mach_o_write_dyld_info (abfd, cmd)) 2798 1.8 christos return false; 2799 1.1 christos break; 2800 1.1 christos default: 2801 1.6 christos _bfd_error_handler 2802 1.6 christos (_("unable to write unknown load command %#x"), 2803 1.6 christos cmd->type); 2804 1.8 christos return false; 2805 1.1 christos } 2806 1.1 christos } 2807 1.1 christos 2808 1.8 christos return true; 2809 1.1 christos } 2810 1.1 christos 2811 1.1 christos static void 2812 1.1 christos bfd_mach_o_append_section_to_segment (bfd_mach_o_segment_command *seg, 2813 1.6 christos bfd_mach_o_section *s) 2814 1.1 christos { 2815 1.1 christos if (seg->sect_head == NULL) 2816 1.1 christos seg->sect_head = s; 2817 1.1 christos else 2818 1.1 christos seg->sect_tail->next = s; 2819 1.1 christos seg->sect_tail = s; 2820 1.1 christos } 2821 1.1 christos 2822 1.1 christos /* Create section Mach-O flags from BFD flags. */ 2823 1.1 christos 2824 1.1 christos static void 2825 1.3 christos bfd_mach_o_set_section_flags_from_bfd (bfd *abfd ATTRIBUTE_UNUSED, 2826 1.3 christos asection *sec) 2827 1.1 christos { 2828 1.1 christos flagword bfd_flags; 2829 1.1 christos bfd_mach_o_section *s = bfd_mach_o_get_mach_o_section (sec); 2830 1.1 christos 2831 1.1 christos /* Create default flags. */ 2832 1.7 christos bfd_flags = bfd_section_flags (sec); 2833 1.1 christos if ((bfd_flags & SEC_CODE) == SEC_CODE) 2834 1.1 christos s->flags = BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS 2835 1.1 christos | BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS 2836 1.1 christos | BFD_MACH_O_S_REGULAR; 2837 1.1 christos else if ((bfd_flags & (SEC_ALLOC | SEC_LOAD)) == SEC_ALLOC) 2838 1.1 christos s->flags = BFD_MACH_O_S_ZEROFILL; 2839 1.1 christos else if (bfd_flags & SEC_DEBUGGING) 2840 1.1 christos s->flags = BFD_MACH_O_S_REGULAR | BFD_MACH_O_S_ATTR_DEBUG; 2841 1.1 christos else 2842 1.1 christos s->flags = BFD_MACH_O_S_REGULAR; 2843 1.1 christos } 2844 1.1 christos 2845 1.8 christos static bool 2846 1.3 christos bfd_mach_o_build_obj_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg) 2847 1.1 christos { 2848 1.3 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 2849 1.3 christos unsigned int i, j; 2850 1.1 christos 2851 1.1 christos seg->vmaddr = 0; 2852 1.1 christos seg->fileoff = mdata->filelen; 2853 1.3 christos seg->initprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE 2854 1.3 christos | BFD_MACH_O_PROT_EXECUTE; 2855 1.3 christos seg->maxprot = seg->initprot; 2856 1.1 christos 2857 1.3 christos /* Append sections to the segment. 2858 1.1 christos 2859 1.1 christos This is a little tedious, we have to honor the need to account zerofill 2860 1.1 christos sections after all the rest. This forces us to do the calculation of 2861 1.3 christos total vmsize in three passes so that any alignment increments are 2862 1.1 christos properly accounted. */ 2863 1.1 christos for (i = 0; i < mdata->nsects; ++i) 2864 1.1 christos { 2865 1.1 christos bfd_mach_o_section *s = mdata->sections[i]; 2866 1.1 christos asection *sec = s->bfdsection; 2867 1.1 christos 2868 1.1 christos /* Although we account for zerofill section sizes in vm order, they are 2869 1.1 christos placed in the file in source sequence. */ 2870 1.3 christos bfd_mach_o_append_section_to_segment (seg, s); 2871 1.1 christos s->offset = 0; 2872 1.3 christos 2873 1.3 christos /* Zerofill sections have zero file size & offset, the only content 2874 1.3 christos written to the file is the symbols. */ 2875 1.1 christos if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) == BFD_MACH_O_S_ZEROFILL 2876 1.6 christos || ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) 2877 1.3 christos == BFD_MACH_O_S_GB_ZEROFILL)) 2878 1.6 christos continue; 2879 1.1 christos 2880 1.3 christos /* The Darwin system tools (in MH_OBJECT files, at least) always account 2881 1.3 christos sections, even those with zero size. */ 2882 1.1 christos if (s->size > 0) 2883 1.3 christos { 2884 1.1 christos seg->vmsize = FILE_ALIGN (seg->vmsize, s->align); 2885 1.1 christos seg->vmsize += s->size; 2886 1.1 christos 2887 1.3 christos /* MH_OBJECT files have unaligned content. */ 2888 1.3 christos if (1) 2889 1.3 christos { 2890 1.3 christos seg->filesize = FILE_ALIGN (seg->filesize, s->align); 2891 1.6 christos mdata->filelen = FILE_ALIGN (mdata->filelen, s->align); 2892 1.6 christos } 2893 1.1 christos seg->filesize += s->size; 2894 1.1 christos 2895 1.3 christos /* The system tools write even zero-sized sections with an offset 2896 1.3 christos field set to the current file position. */ 2897 1.6 christos s->offset = mdata->filelen; 2898 1.3 christos } 2899 1.1 christos 2900 1.1 christos sec->filepos = s->offset; 2901 1.1 christos mdata->filelen += s->size; 2902 1.1 christos } 2903 1.1 christos 2904 1.3 christos /* Now pass through again, for zerofill, only now we just update the 2905 1.3 christos vmsize, and then for zerofill_GB. */ 2906 1.3 christos for (j = 0; j < 2; j++) 2907 1.1 christos { 2908 1.3 christos unsigned int stype; 2909 1.1 christos 2910 1.3 christos if (j == 0) 2911 1.3 christos stype = BFD_MACH_O_S_ZEROFILL; 2912 1.3 christos else 2913 1.3 christos stype = BFD_MACH_O_S_GB_ZEROFILL; 2914 1.1 christos 2915 1.3 christos for (i = 0; i < mdata->nsects; ++i) 2916 1.1 christos { 2917 1.3 christos bfd_mach_o_section *s = mdata->sections[i]; 2918 1.1 christos 2919 1.3 christos if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) != stype) 2920 1.3 christos continue; 2921 1.1 christos 2922 1.3 christos if (s->size > 0) 2923 1.3 christos { 2924 1.3 christos seg->vmsize = FILE_ALIGN (seg->vmsize, s->align); 2925 1.3 christos seg->vmsize += s->size; 2926 1.3 christos } 2927 1.1 christos } 2928 1.1 christos } 2929 1.1 christos 2930 1.1 christos /* Allocate space for the relocations. */ 2931 1.3 christos mdata->filelen = FILE_ALIGN (mdata->filelen, 2); 2932 1.1 christos 2933 1.1 christos for (i = 0; i < mdata->nsects; ++i) 2934 1.1 christos { 2935 1.1 christos bfd_mach_o_section *ms = mdata->sections[i]; 2936 1.1 christos asection *sec = ms->bfdsection; 2937 1.3 christos 2938 1.3 christos ms->nreloc = sec->reloc_count; 2939 1.3 christos if (ms->nreloc == 0) 2940 1.6 christos { 2941 1.3 christos /* Clear nreloc and reloff if there is no relocs. */ 2942 1.1 christos ms->reloff = 0; 2943 1.1 christos continue; 2944 1.6 christos } 2945 1.1 christos sec->rel_filepos = mdata->filelen; 2946 1.1 christos ms->reloff = sec->rel_filepos; 2947 1.1 christos mdata->filelen += sec->reloc_count * BFD_MACH_O_RELENT_SIZE; 2948 1.1 christos } 2949 1.1 christos 2950 1.8 christos return true; 2951 1.1 christos } 2952 1.1 christos 2953 1.8 christos static bool 2954 1.3 christos bfd_mach_o_build_exec_seg_command (bfd *abfd, bfd_mach_o_segment_command *seg) 2955 1.1 christos { 2956 1.3 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 2957 1.1 christos unsigned int i; 2958 1.3 christos bfd_vma pagemask = bfd_mach_o_get_backend_data (abfd)->page_size - 1; 2959 1.3 christos bfd_vma vma; 2960 1.3 christos bfd_mach_o_section *s; 2961 1.3 christos 2962 1.3 christos seg->vmsize = 0; 2963 1.3 christos 2964 1.3 christos seg->fileoff = mdata->filelen; 2965 1.3 christos seg->maxprot = 0; 2966 1.3 christos seg->initprot = 0; 2967 1.3 christos seg->flags = 0; 2968 1.1 christos 2969 1.3 christos /* Append sections to the segment. We assume they are properly ordered 2970 1.3 christos by vma (but we check that). */ 2971 1.3 christos vma = 0; 2972 1.1 christos for (i = 0; i < mdata->nsects; ++i) 2973 1.1 christos { 2974 1.3 christos s = mdata->sections[i]; 2975 1.3 christos 2976 1.3 christos /* Consider only sections for this segment. */ 2977 1.3 christos if (strcmp (seg->segname, s->segname) != 0) 2978 1.3 christos continue; 2979 1.3 christos 2980 1.3 christos bfd_mach_o_append_section_to_segment (seg, s); 2981 1.1 christos 2982 1.3 christos if (s->addr < vma) 2983 1.1 christos { 2984 1.6 christos _bfd_error_handler 2985 1.6 christos /* xgettext:c-format */ 2986 1.6 christos (_("section address (%#" PRIx64 ") " 2987 1.6 christos "below start of segment (%#" PRIx64 ")"), 2988 1.6 christos (uint64_t) s->addr, (uint64_t) vma); 2989 1.8 christos return false; 2990 1.1 christos } 2991 1.3 christos 2992 1.3 christos vma = s->addr + s->size; 2993 1.1 christos } 2994 1.1 christos 2995 1.3 christos /* Set segment file offset: make it page aligned. */ 2996 1.3 christos vma = seg->sect_head->addr; 2997 1.3 christos seg->vmaddr = vma & ~pagemask; 2998 1.3 christos if ((mdata->filelen & pagemask) > (vma & pagemask)) 2999 1.3 christos mdata->filelen += pagemask + 1; 3000 1.3 christos seg->fileoff = mdata->filelen & ~pagemask; 3001 1.3 christos mdata->filelen = seg->fileoff + (vma & pagemask); 3002 1.1 christos 3003 1.3 christos /* Set section file offset. */ 3004 1.3 christos for (s = seg->sect_head; s != NULL; s = s->next) 3005 1.1 christos { 3006 1.3 christos asection *sec = s->bfdsection; 3007 1.7 christos flagword flags = bfd_section_flags (sec); 3008 1.1 christos 3009 1.3 christos /* Adjust segment size. */ 3010 1.3 christos seg->vmsize = FILE_ALIGN (seg->vmsize, s->align); 3011 1.3 christos seg->vmsize += s->size; 3012 1.3 christos 3013 1.3 christos /* File offset and length. */ 3014 1.3 christos seg->filesize = FILE_ALIGN (seg->filesize, s->align); 3015 1.3 christos 3016 1.3 christos if ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) != BFD_MACH_O_S_ZEROFILL 3017 1.6 christos && ((s->flags & BFD_MACH_O_SECTION_TYPE_MASK) 3018 1.3 christos != BFD_MACH_O_S_GB_ZEROFILL)) 3019 1.3 christos { 3020 1.3 christos mdata->filelen = FILE_ALIGN (mdata->filelen, s->align); 3021 1.1 christos 3022 1.3 christos s->offset = mdata->filelen; 3023 1.3 christos s->bfdsection->filepos = s->offset; 3024 1.1 christos 3025 1.3 christos seg->filesize += s->size; 3026 1.3 christos mdata->filelen += s->size; 3027 1.3 christos } 3028 1.3 christos else 3029 1.1 christos { 3030 1.3 christos s->offset = 0; 3031 1.3 christos s->bfdsection->filepos = 0; 3032 1.1 christos } 3033 1.3 christos 3034 1.3 christos /* Set protection. */ 3035 1.3 christos if (flags & SEC_LOAD) 3036 1.1 christos { 3037 1.3 christos if (flags & SEC_CODE) 3038 1.3 christos seg->initprot |= BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_EXECUTE; 3039 1.3 christos if ((flags & (SEC_DATA | SEC_READONLY)) == SEC_DATA) 3040 1.3 christos seg->initprot |= BFD_MACH_O_PROT_WRITE | BFD_MACH_O_PROT_READ; 3041 1.1 christos } 3042 1.3 christos 3043 1.3 christos /* Relocs shouldn't appear in non-object files. */ 3044 1.3 christos if (s->bfdsection->reloc_count != 0) 3045 1.8 christos return false; 3046 1.1 christos } 3047 1.3 christos 3048 1.3 christos /* Set maxprot. */ 3049 1.3 christos if (seg->initprot != 0) 3050 1.3 christos seg->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE 3051 1.3 christos | BFD_MACH_O_PROT_EXECUTE; 3052 1.1 christos else 3053 1.3 christos seg->maxprot = 0; 3054 1.3 christos 3055 1.3 christos /* Round segment size (and file size). */ 3056 1.3 christos seg->vmsize = (seg->vmsize + pagemask) & ~pagemask; 3057 1.3 christos seg->filesize = (seg->filesize + pagemask) & ~pagemask; 3058 1.3 christos mdata->filelen = (mdata->filelen + pagemask) & ~pagemask; 3059 1.3 christos 3060 1.8 christos return true; 3061 1.3 christos } 3062 1.3 christos 3063 1.3 christos /* Layout the commands: set commands size and offset, set ncmds and sizeofcmds 3064 1.3 christos fields in header. */ 3065 1.3 christos 3066 1.8 christos static bool 3067 1.3 christos bfd_mach_o_layout_commands (bfd_mach_o_data_struct *mdata) 3068 1.3 christos { 3069 1.3 christos unsigned wide = mach_o_wide_p (&mdata->header); 3070 1.3 christos unsigned int hdrlen; 3071 1.3 christos ufile_ptr offset; 3072 1.3 christos bfd_mach_o_load_command *cmd; 3073 1.3 christos unsigned int align; 3074 1.8 christos bool ret = true; 3075 1.3 christos 3076 1.3 christos hdrlen = wide ? BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE; 3077 1.3 christos align = wide ? 8 - 1 : 4 - 1; 3078 1.3 christos offset = hdrlen; 3079 1.3 christos mdata->header.ncmds = 0; 3080 1.1 christos 3081 1.3 christos for (cmd = mdata->first_command; cmd; cmd = cmd->next) 3082 1.1 christos { 3083 1.3 christos mdata->header.ncmds++; 3084 1.3 christos cmd->offset = offset; 3085 1.1 christos 3086 1.3 christos switch (cmd->type) 3087 1.1 christos { 3088 1.3 christos case BFD_MACH_O_LC_SEGMENT_64: 3089 1.3 christos cmd->len = BFD_MACH_O_LC_SEGMENT_64_SIZE 3090 1.3 christos + BFD_MACH_O_SECTION_64_SIZE * cmd->command.segment.nsects; 3091 1.3 christos break; 3092 1.3 christos case BFD_MACH_O_LC_SEGMENT: 3093 1.3 christos cmd->len = BFD_MACH_O_LC_SEGMENT_SIZE 3094 1.3 christos + BFD_MACH_O_SECTION_SIZE * cmd->command.segment.nsects; 3095 1.3 christos break; 3096 1.3 christos case BFD_MACH_O_LC_SYMTAB: 3097 1.3 christos cmd->len = sizeof (struct mach_o_symtab_command_external) 3098 1.3 christos + BFD_MACH_O_LC_SIZE; 3099 1.3 christos break; 3100 1.3 christos case BFD_MACH_O_LC_DYSYMTAB: 3101 1.3 christos cmd->len = sizeof (struct mach_o_dysymtab_command_external) 3102 1.3 christos + BFD_MACH_O_LC_SIZE; 3103 1.3 christos break; 3104 1.3 christos case BFD_MACH_O_LC_LOAD_DYLIB: 3105 1.3 christos cmd->len = sizeof (struct mach_o_dylib_command_external) 3106 1.3 christos + BFD_MACH_O_LC_SIZE; 3107 1.3 christos cmd->command.dylib.name_offset = cmd->len; 3108 1.3 christos cmd->len += strlen (cmd->command.dylib.name_str); 3109 1.3 christos cmd->len = (cmd->len + align) & ~align; 3110 1.3 christos break; 3111 1.3 christos case BFD_MACH_O_LC_LOAD_DYLINKER: 3112 1.3 christos cmd->len = sizeof (struct mach_o_str_command_external) 3113 1.3 christos + BFD_MACH_O_LC_SIZE; 3114 1.3 christos cmd->command.dylinker.name_offset = cmd->len; 3115 1.3 christos cmd->len += strlen (cmd->command.dylinker.name_str); 3116 1.3 christos cmd->len = (cmd->len + align) & ~align; 3117 1.3 christos break; 3118 1.3 christos case BFD_MACH_O_LC_MAIN: 3119 1.3 christos cmd->len = sizeof (struct mach_o_entry_point_command_external) 3120 1.3 christos + BFD_MACH_O_LC_SIZE; 3121 1.3 christos break; 3122 1.3 christos case BFD_MACH_O_LC_DYLD_INFO: 3123 1.3 christos cmd->len = sizeof (struct mach_o_dyld_info_command_external) 3124 1.3 christos + BFD_MACH_O_LC_SIZE; 3125 1.3 christos break; 3126 1.3 christos default: 3127 1.6 christos _bfd_error_handler 3128 1.6 christos (_("unable to layout unknown load command %#x"), 3129 1.6 christos cmd->type); 3130 1.8 christos ret = false; 3131 1.3 christos break; 3132 1.3 christos } 3133 1.1 christos 3134 1.3 christos BFD_ASSERT (cmd->len % (align + 1) == 0); 3135 1.3 christos offset += cmd->len; 3136 1.1 christos } 3137 1.3 christos mdata->header.sizeofcmds = offset - hdrlen; 3138 1.3 christos mdata->filelen = offset; 3139 1.3 christos 3140 1.3 christos return ret; 3141 1.3 christos } 3142 1.3 christos 3143 1.3 christos /* Subroutine of bfd_mach_o_build_commands: set type, name and nsects of a 3144 1.3 christos segment. */ 3145 1.3 christos 3146 1.3 christos static void 3147 1.3 christos bfd_mach_o_init_segment (bfd_mach_o_data_struct *mdata, 3148 1.3 christos bfd_mach_o_load_command *cmd, 3149 1.3 christos const char *segname, unsigned int nbr_sect) 3150 1.3 christos { 3151 1.3 christos bfd_mach_o_segment_command *seg = &cmd->command.segment; 3152 1.3 christos unsigned wide = mach_o_wide_p (&mdata->header); 3153 1.1 christos 3154 1.3 christos /* Init segment command. */ 3155 1.3 christos cmd->type = wide ? BFD_MACH_O_LC_SEGMENT_64 : BFD_MACH_O_LC_SEGMENT; 3156 1.8 christos cmd->type_required = false; 3157 1.3 christos 3158 1.3 christos strcpy (seg->segname, segname); 3159 1.3 christos seg->nsects = nbr_sect; 3160 1.3 christos 3161 1.3 christos seg->vmaddr = 0; 3162 1.3 christos seg->vmsize = 0; 3163 1.3 christos 3164 1.3 christos seg->fileoff = 0; 3165 1.3 christos seg->filesize = 0; 3166 1.3 christos seg->maxprot = 0; 3167 1.3 christos seg->initprot = 0; 3168 1.3 christos seg->flags = 0; 3169 1.3 christos seg->sect_head = NULL; 3170 1.3 christos seg->sect_tail = NULL; 3171 1.1 christos } 3172 1.1 christos 3173 1.1 christos /* Build Mach-O load commands (currently assuming an MH_OBJECT file). 3174 1.1 christos TODO: Other file formats, rebuilding symtab/dysymtab commands for strip 3175 1.1 christos and copy functionality. */ 3176 1.1 christos 3177 1.8 christos bool 3178 1.1 christos bfd_mach_o_build_commands (bfd *abfd) 3179 1.1 christos { 3180 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 3181 1.1 christos unsigned wide = mach_o_wide_p (&mdata->header); 3182 1.3 christos unsigned int nbr_segcmd = 0; 3183 1.3 christos bfd_mach_o_load_command *commands; 3184 1.3 christos unsigned int nbr_commands; 3185 1.1 christos int symtab_idx = -1; 3186 1.1 christos int dysymtab_idx = -1; 3187 1.3 christos int main_idx = -1; 3188 1.3 christos unsigned int i; 3189 1.1 christos 3190 1.3 christos /* Return now if already built. */ 3191 1.3 christos if (mdata->header.ncmds != 0) 3192 1.8 christos return true; 3193 1.1 christos 3194 1.1 christos /* Fill in the file type, if not already set. */ 3195 1.1 christos if (mdata->header.filetype == 0) 3196 1.1 christos { 3197 1.1 christos if (abfd->flags & EXEC_P) 3198 1.6 christos mdata->header.filetype = BFD_MACH_O_MH_EXECUTE; 3199 1.1 christos else if (abfd->flags & DYNAMIC) 3200 1.6 christos mdata->header.filetype = BFD_MACH_O_MH_DYLIB; 3201 1.1 christos else 3202 1.6 christos mdata->header.filetype = BFD_MACH_O_MH_OBJECT; 3203 1.1 christos } 3204 1.1 christos 3205 1.1 christos /* If hasn't already been done, flatten sections list, and sort 3206 1.1 christos if/when required. Must be done before the symbol table is adjusted, 3207 1.1 christos since that depends on properly numbered sections. */ 3208 1.1 christos if (mdata->nsects == 0 || mdata->sections == NULL) 3209 1.1 christos if (! bfd_mach_o_mangle_sections (abfd, mdata)) 3210 1.8 christos return false; 3211 1.1 christos 3212 1.1 christos /* Order the symbol table, fill-in/check mach-o specific fields and 3213 1.1 christos partition out any indirect symbols. */ 3214 1.1 christos if (!bfd_mach_o_mangle_symbols (abfd)) 3215 1.8 christos return false; 3216 1.1 christos 3217 1.3 christos /* Segment commands. */ 3218 1.3 christos if (mdata->header.filetype == BFD_MACH_O_MH_OBJECT) 3219 1.3 christos { 3220 1.3 christos /* Only one segment for all the sections. But the segment is 3221 1.3 christos optional if there is no sections. */ 3222 1.3 christos nbr_segcmd = (mdata->nsects > 0) ? 1 : 0; 3223 1.3 christos } 3224 1.3 christos else 3225 1.3 christos { 3226 1.3 christos bfd_mach_o_section *prev_sect = NULL; 3227 1.1 christos 3228 1.3 christos /* One pagezero segment and one linkedit segment. */ 3229 1.3 christos nbr_segcmd = 2; 3230 1.1 christos 3231 1.3 christos /* Create one segment for associated segment name in sections. 3232 1.3 christos Assume that sections with the same segment name are consecutive. */ 3233 1.3 christos for (i = 0; i < mdata->nsects; i++) 3234 1.3 christos { 3235 1.3 christos bfd_mach_o_section *this_sect = mdata->sections[i]; 3236 1.1 christos 3237 1.3 christos if (prev_sect == NULL 3238 1.3 christos || strcmp (prev_sect->segname, this_sect->segname) != 0) 3239 1.3 christos { 3240 1.3 christos nbr_segcmd++; 3241 1.3 christos prev_sect = this_sect; 3242 1.3 christos } 3243 1.3 christos } 3244 1.1 christos } 3245 1.1 christos 3246 1.3 christos nbr_commands = nbr_segcmd; 3247 1.3 christos 3248 1.3 christos /* One command for the symbol table (only if there are symbols. */ 3249 1.1 christos if (bfd_get_symcount (abfd) > 0) 3250 1.3 christos symtab_idx = nbr_commands++; 3251 1.1 christos 3252 1.1 christos /* FIXME: 3253 1.1 christos This is a rather crude test for whether we should build a dysymtab. */ 3254 1.1 christos if (bfd_mach_o_should_emit_dysymtab () 3255 1.1 christos && bfd_get_symcount (abfd)) 3256 1.1 christos { 3257 1.1 christos /* If there should be a case where a dysymtab could be emitted without 3258 1.1 christos a symtab (seems improbable), this would need amending. */ 3259 1.3 christos dysymtab_idx = nbr_commands++; 3260 1.1 christos } 3261 1.1 christos 3262 1.3 christos /* Add an entry point command. */ 3263 1.3 christos if (mdata->header.filetype == BFD_MACH_O_MH_EXECUTE 3264 1.3 christos && bfd_get_start_address (abfd) != 0) 3265 1.3 christos main_idx = nbr_commands++; 3266 1.1 christos 3267 1.1 christos /* Well, we must have a header, at least. */ 3268 1.3 christos mdata->filelen = wide ? BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE; 3269 1.1 christos 3270 1.1 christos /* A bit unusual, but no content is valid; 3271 1.1 christos as -n empty.s -o empty.o */ 3272 1.3 christos if (nbr_commands == 0) 3273 1.3 christos { 3274 1.3 christos /* Layout commands (well none...) and set headers command fields. */ 3275 1.3 christos return bfd_mach_o_layout_commands (mdata); 3276 1.3 christos } 3277 1.1 christos 3278 1.3 christos /* Create commands for segments (and symtabs), prepend them. */ 3279 1.3 christos commands = bfd_zalloc (abfd, nbr_commands * sizeof (bfd_mach_o_load_command)); 3280 1.3 christos if (commands == NULL) 3281 1.8 christos return false; 3282 1.3 christos for (i = 0; i < nbr_commands - 1; i++) 3283 1.3 christos commands[i].next = &commands[i + 1]; 3284 1.3 christos commands[nbr_commands - 1].next = mdata->first_command; 3285 1.3 christos if (mdata->first_command == NULL) 3286 1.3 christos mdata->last_command = &commands[nbr_commands - 1]; 3287 1.3 christos mdata->first_command = &commands[0]; 3288 1.3 christos 3289 1.3 christos if (mdata->header.filetype == BFD_MACH_O_MH_OBJECT && nbr_segcmd != 0) 3290 1.3 christos { 3291 1.3 christos /* For object file, there is only one segment. */ 3292 1.3 christos bfd_mach_o_init_segment (mdata, &commands[0], "", mdata->nsects); 3293 1.3 christos } 3294 1.3 christos else if (nbr_segcmd != 0) 3295 1.3 christos { 3296 1.3 christos bfd_mach_o_load_command *cmd; 3297 1.3 christos 3298 1.3 christos BFD_ASSERT (nbr_segcmd >= 2); 3299 1.1 christos 3300 1.3 christos /* The pagezero. */ 3301 1.3 christos cmd = &commands[0]; 3302 1.3 christos bfd_mach_o_init_segment (mdata, cmd, "__PAGEZERO", 0); 3303 1.1 christos 3304 1.3 christos /* Segments from sections. */ 3305 1.3 christos cmd++; 3306 1.3 christos for (i = 0; i < mdata->nsects;) 3307 1.1 christos { 3308 1.3 christos const char *segname = mdata->sections[i]->segname; 3309 1.3 christos unsigned int nbr_sect = 1; 3310 1.3 christos 3311 1.3 christos /* Count number of sections for this segment. */ 3312 1.3 christos for (i++; i < mdata->nsects; i++) 3313 1.3 christos if (strcmp (mdata->sections[i]->segname, segname) == 0) 3314 1.3 christos nbr_sect++; 3315 1.3 christos else 3316 1.3 christos break; 3317 1.3 christos 3318 1.3 christos bfd_mach_o_init_segment (mdata, cmd, segname, nbr_sect); 3319 1.3 christos cmd++; 3320 1.1 christos } 3321 1.1 christos 3322 1.3 christos /* The linkedit. */ 3323 1.3 christos bfd_mach_o_init_segment (mdata, cmd, "__LINKEDIT", 0); 3324 1.1 christos } 3325 1.1 christos 3326 1.1 christos if (symtab_idx >= 0) 3327 1.1 christos { 3328 1.1 christos /* Init symtab command. */ 3329 1.3 christos bfd_mach_o_load_command *cmd = &commands[symtab_idx]; 3330 1.3 christos 3331 1.1 christos cmd->type = BFD_MACH_O_LC_SYMTAB; 3332 1.8 christos cmd->type_required = false; 3333 1.1 christos } 3334 1.1 christos 3335 1.1 christos /* If required, setup symtab command, see comment above about the quality 3336 1.1 christos of this test. */ 3337 1.1 christos if (dysymtab_idx >= 0) 3338 1.1 christos { 3339 1.3 christos bfd_mach_o_load_command *cmd = &commands[dysymtab_idx]; 3340 1.1 christos 3341 1.1 christos cmd->type = BFD_MACH_O_LC_DYSYMTAB; 3342 1.8 christos cmd->type_required = false; 3343 1.3 christos } 3344 1.3 christos 3345 1.3 christos /* Create the main command. */ 3346 1.3 christos if (main_idx >= 0) 3347 1.3 christos { 3348 1.3 christos bfd_mach_o_load_command *cmd = &commands[main_idx]; 3349 1.1 christos 3350 1.3 christos cmd->type = BFD_MACH_O_LC_MAIN; 3351 1.8 christos cmd->type_required = true; 3352 1.1 christos 3353 1.3 christos cmd->command.main.entryoff = 0; 3354 1.3 christos cmd->command.main.stacksize = 0; 3355 1.1 christos } 3356 1.1 christos 3357 1.3 christos /* Layout commands. */ 3358 1.3 christos if (! bfd_mach_o_layout_commands (mdata)) 3359 1.8 christos return false; 3360 1.3 christos 3361 1.1 christos /* So, now we have sized the commands and the filelen set to that. 3362 1.1 christos Now we can build the segment command and set the section file offsets. */ 3363 1.3 christos if (mdata->header.filetype == BFD_MACH_O_MH_OBJECT) 3364 1.3 christos { 3365 1.3 christos for (i = 0; i < nbr_segcmd; i++) 3366 1.3 christos if (!bfd_mach_o_build_obj_seg_command 3367 1.3 christos (abfd, &commands[i].command.segment)) 3368 1.8 christos return false; 3369 1.3 christos } 3370 1.3 christos else 3371 1.3 christos { 3372 1.3 christos bfd_vma maxvma = 0; 3373 1.3 christos 3374 1.3 christos /* Skip pagezero and linkedit segments. */ 3375 1.3 christos for (i = 1; i < nbr_segcmd - 1; i++) 3376 1.3 christos { 3377 1.3 christos bfd_mach_o_segment_command *seg = &commands[i].command.segment; 3378 1.3 christos 3379 1.3 christos if (!bfd_mach_o_build_exec_seg_command (abfd, seg)) 3380 1.8 christos return false; 3381 1.3 christos 3382 1.3 christos if (seg->vmaddr + seg->vmsize > maxvma) 3383 1.3 christos maxvma = seg->vmaddr + seg->vmsize; 3384 1.3 christos } 3385 1.1 christos 3386 1.3 christos /* Set the size of __PAGEZERO. */ 3387 1.3 christos commands[0].command.segment.vmsize = 3388 1.3 christos commands[1].command.segment.vmaddr; 3389 1.3 christos 3390 1.3 christos /* Set the vma and fileoff of __LINKEDIT. */ 3391 1.3 christos commands[nbr_segcmd - 1].command.segment.vmaddr = maxvma; 3392 1.3 christos commands[nbr_segcmd - 1].command.segment.fileoff = mdata->filelen; 3393 1.3 christos 3394 1.3 christos /* Set entry point (once segments have been laid out). */ 3395 1.3 christos if (main_idx >= 0) 3396 1.3 christos commands[main_idx].command.main.entryoff = 3397 1.3 christos bfd_get_start_address (abfd) - commands[1].command.segment.vmaddr; 3398 1.3 christos } 3399 1.1 christos 3400 1.8 christos return true; 3401 1.1 christos } 3402 1.1 christos 3403 1.1 christos /* Set the contents of a section. */ 3404 1.1 christos 3405 1.8 christos bool 3406 1.1 christos bfd_mach_o_set_section_contents (bfd *abfd, 3407 1.1 christos asection *section, 3408 1.1 christos const void * location, 3409 1.1 christos file_ptr offset, 3410 1.1 christos bfd_size_type count) 3411 1.1 christos { 3412 1.1 christos file_ptr pos; 3413 1.1 christos 3414 1.1 christos /* Trying to write the first section contents will trigger the creation of 3415 1.1 christos the load commands if they are not already present. */ 3416 1.3 christos if (!abfd->output_has_begun && !bfd_mach_o_build_commands (abfd)) 3417 1.8 christos return false; 3418 1.1 christos 3419 1.1 christos if (count == 0) 3420 1.8 christos return true; 3421 1.1 christos 3422 1.1 christos pos = section->filepos + offset; 3423 1.1 christos if (bfd_seek (abfd, pos, SEEK_SET) != 0 3424 1.9 christos || bfd_write (location, count, abfd) != count) 3425 1.8 christos return false; 3426 1.1 christos 3427 1.8 christos return true; 3428 1.1 christos } 3429 1.1 christos 3430 1.1 christos int 3431 1.1 christos bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED, 3432 1.1 christos struct bfd_link_info *info ATTRIBUTE_UNUSED) 3433 1.1 christos { 3434 1.1 christos return 0; 3435 1.1 christos } 3436 1.1 christos 3437 1.1 christos /* Make an empty symbol. This is required only because 3438 1.1 christos bfd_make_section_anyway wants to create a symbol for the section. */ 3439 1.1 christos 3440 1.1 christos asymbol * 3441 1.1 christos bfd_mach_o_make_empty_symbol (bfd *abfd) 3442 1.1 christos { 3443 1.1 christos asymbol *new_symbol; 3444 1.1 christos 3445 1.1 christos new_symbol = bfd_zalloc (abfd, sizeof (bfd_mach_o_asymbol)); 3446 1.1 christos if (new_symbol == NULL) 3447 1.1 christos return new_symbol; 3448 1.1 christos new_symbol->the_bfd = abfd; 3449 1.1 christos new_symbol->udata.i = SYM_MACHO_FIELDS_UNSET; 3450 1.1 christos return new_symbol; 3451 1.1 christos } 3452 1.1 christos 3453 1.8 christos static bool 3454 1.5 christos bfd_mach_o_read_header (bfd *abfd, file_ptr hdr_off, bfd_mach_o_header *header) 3455 1.1 christos { 3456 1.1 christos struct mach_o_header_external raw; 3457 1.1 christos unsigned int size; 3458 1.1 christos bfd_vma (*get32) (const void *) = NULL; 3459 1.1 christos 3460 1.1 christos /* Just read the magic number. */ 3461 1.5 christos if (bfd_seek (abfd, hdr_off, SEEK_SET) != 0 3462 1.9 christos || bfd_read (raw.magic, sizeof (raw.magic), abfd) != 4) 3463 1.8 christos return false; 3464 1.1 christos 3465 1.1 christos if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC) 3466 1.1 christos { 3467 1.1 christos header->byteorder = BFD_ENDIAN_BIG; 3468 1.1 christos header->magic = BFD_MACH_O_MH_MAGIC; 3469 1.1 christos header->version = 1; 3470 1.1 christos get32 = bfd_getb32; 3471 1.1 christos } 3472 1.1 christos else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC) 3473 1.1 christos { 3474 1.1 christos header->byteorder = BFD_ENDIAN_LITTLE; 3475 1.1 christos header->magic = BFD_MACH_O_MH_MAGIC; 3476 1.1 christos header->version = 1; 3477 1.1 christos get32 = bfd_getl32; 3478 1.1 christos } 3479 1.1 christos else if (bfd_getb32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64) 3480 1.1 christos { 3481 1.1 christos header->byteorder = BFD_ENDIAN_BIG; 3482 1.1 christos header->magic = BFD_MACH_O_MH_MAGIC_64; 3483 1.1 christos header->version = 2; 3484 1.1 christos get32 = bfd_getb32; 3485 1.1 christos } 3486 1.1 christos else if (bfd_getl32 (raw.magic) == BFD_MACH_O_MH_MAGIC_64) 3487 1.1 christos { 3488 1.1 christos header->byteorder = BFD_ENDIAN_LITTLE; 3489 1.1 christos header->magic = BFD_MACH_O_MH_MAGIC_64; 3490 1.1 christos header->version = 2; 3491 1.1 christos get32 = bfd_getl32; 3492 1.1 christos } 3493 1.1 christos else 3494 1.1 christos { 3495 1.1 christos header->byteorder = BFD_ENDIAN_UNKNOWN; 3496 1.8 christos return false; 3497 1.1 christos } 3498 1.1 christos 3499 1.1 christos /* Once the size of the header is known, read the full header. */ 3500 1.1 christos size = mach_o_wide_p (header) ? 3501 1.1 christos BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE; 3502 1.1 christos 3503 1.5 christos if (bfd_seek (abfd, hdr_off, SEEK_SET) != 0 3504 1.9 christos || bfd_read (&raw, size, abfd) != size) 3505 1.8 christos return false; 3506 1.1 christos 3507 1.1 christos header->cputype = (*get32) (raw.cputype); 3508 1.1 christos header->cpusubtype = (*get32) (raw.cpusubtype); 3509 1.1 christos header->filetype = (*get32) (raw.filetype); 3510 1.1 christos header->ncmds = (*get32) (raw.ncmds); 3511 1.1 christos header->sizeofcmds = (*get32) (raw.sizeofcmds); 3512 1.1 christos header->flags = (*get32) (raw.flags); 3513 1.1 christos 3514 1.1 christos if (mach_o_wide_p (header)) 3515 1.1 christos header->reserved = (*get32) (raw.reserved); 3516 1.1 christos else 3517 1.1 christos header->reserved = 0; 3518 1.1 christos 3519 1.8 christos return true; 3520 1.1 christos } 3521 1.1 christos 3522 1.8 christos bool 3523 1.1 christos bfd_mach_o_new_section_hook (bfd *abfd, asection *sec) 3524 1.1 christos { 3525 1.10 christos bfd_mach_o_section *s = bfd_zalloc (abfd, sizeof (*s)); 3526 1.1 christos if (s == NULL) 3527 1.10 christos return false; 3528 1.10 christos sec->used_by_bfd = s; 3529 1.10 christos s->bfdsection = sec; 3530 1.1 christos 3531 1.10 christos /* Create the Darwin seg/sect name pair from the bfd name. 3532 1.10 christos If this is a canonical name for which a specific paiting exists 3533 1.10 christos there will also be defined flags, type, attribute and alignment 3534 1.10 christos values. */ 3535 1.10 christos const mach_o_section_name_xlat *xlat 3536 1.10 christos = bfd_mach_o_convert_section_name_to_mach_o (abfd, sec, s); 3537 1.10 christos if (xlat != NULL) 3538 1.10 christos { 3539 1.10 christos s->flags = xlat->macho_sectype | xlat->macho_secattr; 3540 1.10 christos unsigned bfdalign = bfd_section_alignment (sec); 3541 1.10 christos s->align = xlat->sectalign > bfdalign ? xlat->sectalign : bfdalign; 3542 1.10 christos bfd_set_section_alignment (sec, s->align); 3543 1.10 christos flagword bfd_flags = bfd_section_flags (sec); 3544 1.10 christos if (bfd_flags == SEC_NO_FLAGS) 3545 1.10 christos bfd_set_section_flags (sec, xlat->bfd_flags); 3546 1.1 christos } 3547 1.10 christos else 3548 1.10 christos /* Create default flags. */ 3549 1.10 christos bfd_mach_o_set_section_flags_from_bfd (abfd, sec); 3550 1.1 christos 3551 1.1 christos return _bfd_generic_new_section_hook (abfd, sec); 3552 1.1 christos } 3553 1.1 christos 3554 1.1 christos static void 3555 1.7 christos bfd_mach_o_init_section_from_mach_o (asection *sec, unsigned long prot) 3556 1.1 christos { 3557 1.1 christos flagword flags; 3558 1.1 christos bfd_mach_o_section *section; 3559 1.1 christos 3560 1.7 christos flags = bfd_section_flags (sec); 3561 1.1 christos section = bfd_mach_o_get_mach_o_section (sec); 3562 1.1 christos 3563 1.1 christos /* TODO: see if we should use the xlat system for doing this by 3564 1.1 christos preference and fall back to this for unknown sections. */ 3565 1.1 christos 3566 1.1 christos if (flags == SEC_NO_FLAGS) 3567 1.1 christos { 3568 1.1 christos /* Try to guess flags. */ 3569 1.1 christos if (section->flags & BFD_MACH_O_S_ATTR_DEBUG) 3570 1.6 christos flags = SEC_DEBUGGING; 3571 1.1 christos else 3572 1.6 christos { 3573 1.6 christos flags = SEC_ALLOC; 3574 1.6 christos if ((section->flags & BFD_MACH_O_SECTION_TYPE_MASK) 3575 1.6 christos != BFD_MACH_O_S_ZEROFILL) 3576 1.6 christos { 3577 1.6 christos flags |= SEC_LOAD; 3578 1.6 christos if (prot & BFD_MACH_O_PROT_EXECUTE) 3579 1.6 christos flags |= SEC_CODE; 3580 1.6 christos if (prot & BFD_MACH_O_PROT_WRITE) 3581 1.6 christos flags |= SEC_DATA; 3582 1.6 christos else if (prot & BFD_MACH_O_PROT_READ) 3583 1.6 christos flags |= SEC_READONLY; 3584 1.6 christos } 3585 1.6 christos } 3586 1.1 christos } 3587 1.1 christos else 3588 1.1 christos { 3589 1.1 christos if ((flags & SEC_DEBUGGING) == 0) 3590 1.6 christos flags |= SEC_ALLOC; 3591 1.1 christos } 3592 1.1 christos 3593 1.1 christos if (section->offset != 0) 3594 1.1 christos flags |= SEC_HAS_CONTENTS; 3595 1.1 christos if (section->nreloc != 0) 3596 1.1 christos flags |= SEC_RELOC; 3597 1.1 christos 3598 1.7 christos bfd_set_section_flags (sec, flags); 3599 1.1 christos 3600 1.1 christos sec->vma = section->addr; 3601 1.1 christos sec->lma = section->addr; 3602 1.1 christos sec->size = section->size; 3603 1.1 christos sec->filepos = section->offset; 3604 1.1 christos sec->alignment_power = section->align; 3605 1.1 christos sec->segment_mark = 0; 3606 1.1 christos sec->reloc_count = section->nreloc; 3607 1.1 christos sec->rel_filepos = section->reloff; 3608 1.1 christos } 3609 1.1 christos 3610 1.1 christos static asection * 3611 1.1 christos bfd_mach_o_make_bfd_section (bfd *abfd, 3612 1.6 christos const unsigned char *segname, 3613 1.6 christos const unsigned char *sectname) 3614 1.1 christos { 3615 1.1 christos const char *sname; 3616 1.1 christos flagword flags; 3617 1.1 christos 3618 1.1 christos bfd_mach_o_convert_section_name_to_bfd 3619 1.1 christos (abfd, (const char *)segname, (const char *)sectname, &sname, &flags); 3620 1.1 christos if (sname == NULL) 3621 1.1 christos return NULL; 3622 1.1 christos 3623 1.1 christos return bfd_make_section_anyway_with_flags (abfd, sname, flags); 3624 1.1 christos } 3625 1.1 christos 3626 1.1 christos static asection * 3627 1.5 christos bfd_mach_o_read_section_32 (bfd *abfd, unsigned long prot) 3628 1.1 christos { 3629 1.1 christos struct mach_o_section_32_external raw; 3630 1.1 christos asection *sec; 3631 1.1 christos bfd_mach_o_section *section; 3632 1.1 christos 3633 1.9 christos if (bfd_read (&raw, BFD_MACH_O_SECTION_SIZE, abfd) 3634 1.5 christos != BFD_MACH_O_SECTION_SIZE) 3635 1.1 christos return NULL; 3636 1.1 christos 3637 1.1 christos sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname); 3638 1.1 christos if (sec == NULL) 3639 1.1 christos return NULL; 3640 1.1 christos 3641 1.1 christos section = bfd_mach_o_get_mach_o_section (sec); 3642 1.1 christos memcpy (section->segname, raw.segname, sizeof (raw.segname)); 3643 1.1 christos section->segname[BFD_MACH_O_SEGNAME_SIZE] = 0; 3644 1.1 christos memcpy (section->sectname, raw.sectname, sizeof (raw.sectname)); 3645 1.1 christos section->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0; 3646 1.1 christos section->addr = bfd_h_get_32 (abfd, raw.addr); 3647 1.1 christos section->size = bfd_h_get_32 (abfd, raw.size); 3648 1.1 christos section->offset = bfd_h_get_32 (abfd, raw.offset); 3649 1.1 christos section->align = bfd_h_get_32 (abfd, raw.align); 3650 1.3 christos /* PR 17512: file: 0017eb76. */ 3651 1.8 christos if (section->align >= 31) 3652 1.3 christos { 3653 1.6 christos _bfd_error_handler 3654 1.8 christos (_("bfd_mach_o_read_section_32: overlarge alignment value: %#lx"), 3655 1.8 christos section->align); 3656 1.8 christos section->align = 30; 3657 1.3 christos } 3658 1.1 christos section->reloff = bfd_h_get_32 (abfd, raw.reloff); 3659 1.1 christos section->nreloc = bfd_h_get_32 (abfd, raw.nreloc); 3660 1.1 christos section->flags = bfd_h_get_32 (abfd, raw.flags); 3661 1.1 christos section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1); 3662 1.1 christos section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2); 3663 1.1 christos section->reserved3 = 0; 3664 1.1 christos 3665 1.7 christos bfd_mach_o_init_section_from_mach_o (sec, prot); 3666 1.1 christos 3667 1.1 christos return sec; 3668 1.1 christos } 3669 1.1 christos 3670 1.1 christos static asection * 3671 1.5 christos bfd_mach_o_read_section_64 (bfd *abfd, unsigned long prot) 3672 1.1 christos { 3673 1.1 christos struct mach_o_section_64_external raw; 3674 1.1 christos asection *sec; 3675 1.1 christos bfd_mach_o_section *section; 3676 1.1 christos 3677 1.9 christos if (bfd_read (&raw, BFD_MACH_O_SECTION_64_SIZE, abfd) 3678 1.5 christos != BFD_MACH_O_SECTION_64_SIZE) 3679 1.1 christos return NULL; 3680 1.1 christos 3681 1.1 christos sec = bfd_mach_o_make_bfd_section (abfd, raw.segname, raw.sectname); 3682 1.1 christos if (sec == NULL) 3683 1.1 christos return NULL; 3684 1.1 christos 3685 1.1 christos section = bfd_mach_o_get_mach_o_section (sec); 3686 1.1 christos memcpy (section->segname, raw.segname, sizeof (raw.segname)); 3687 1.1 christos section->segname[BFD_MACH_O_SEGNAME_SIZE] = 0; 3688 1.1 christos memcpy (section->sectname, raw.sectname, sizeof (raw.sectname)); 3689 1.1 christos section->sectname[BFD_MACH_O_SECTNAME_SIZE] = 0; 3690 1.1 christos section->addr = bfd_h_get_64 (abfd, raw.addr); 3691 1.1 christos section->size = bfd_h_get_64 (abfd, raw.size); 3692 1.1 christos section->offset = bfd_h_get_32 (abfd, raw.offset); 3693 1.1 christos section->align = bfd_h_get_32 (abfd, raw.align); 3694 1.8 christos if (section->align >= 63) 3695 1.3 christos { 3696 1.6 christos _bfd_error_handler 3697 1.8 christos (_("bfd_mach_o_read_section_64: overlarge alignment value: %#lx"), 3698 1.8 christos section->align); 3699 1.8 christos section->align = 62; 3700 1.3 christos } 3701 1.1 christos section->reloff = bfd_h_get_32 (abfd, raw.reloff); 3702 1.1 christos section->nreloc = bfd_h_get_32 (abfd, raw.nreloc); 3703 1.1 christos section->flags = bfd_h_get_32 (abfd, raw.flags); 3704 1.1 christos section->reserved1 = bfd_h_get_32 (abfd, raw.reserved1); 3705 1.1 christos section->reserved2 = bfd_h_get_32 (abfd, raw.reserved2); 3706 1.1 christos section->reserved3 = bfd_h_get_32 (abfd, raw.reserved3); 3707 1.1 christos 3708 1.7 christos bfd_mach_o_init_section_from_mach_o (sec, prot); 3709 1.1 christos 3710 1.1 christos return sec; 3711 1.1 christos } 3712 1.1 christos 3713 1.1 christos static asection * 3714 1.5 christos bfd_mach_o_read_section (bfd *abfd, unsigned long prot, unsigned int wide) 3715 1.1 christos { 3716 1.1 christos if (wide) 3717 1.5 christos return bfd_mach_o_read_section_64 (abfd, prot); 3718 1.1 christos else 3719 1.5 christos return bfd_mach_o_read_section_32 (abfd, prot); 3720 1.1 christos } 3721 1.1 christos 3722 1.8 christos static bool 3723 1.1 christos bfd_mach_o_read_symtab_symbol (bfd *abfd, 3724 1.6 christos bfd_mach_o_symtab_command *sym, 3725 1.6 christos bfd_mach_o_asymbol *s, 3726 1.6 christos unsigned long i) 3727 1.1 christos { 3728 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 3729 1.1 christos unsigned int wide = mach_o_wide_p (&mdata->header); 3730 1.1 christos unsigned int symwidth = 3731 1.1 christos wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE; 3732 1.1 christos unsigned int symoff = sym->symoff + (i * symwidth); 3733 1.1 christos struct mach_o_nlist_64_external raw; 3734 1.1 christos unsigned char type = -1; 3735 1.1 christos unsigned char section = -1; 3736 1.1 christos short desc = -1; 3737 1.1 christos symvalue value = -1; 3738 1.1 christos unsigned long stroff = -1; 3739 1.1 christos unsigned int symtype = -1; 3740 1.1 christos 3741 1.1 christos BFD_ASSERT (sym->strtab != NULL); 3742 1.1 christos 3743 1.1 christos if (bfd_seek (abfd, symoff, SEEK_SET) != 0 3744 1.9 christos || bfd_read (&raw, symwidth, abfd) != symwidth) 3745 1.1 christos { 3746 1.6 christos _bfd_error_handler 3747 1.6 christos /* xgettext:c-format */ 3748 1.6 christos (_("bfd_mach_o_read_symtab_symbol: unable to read %d bytes at %u"), 3749 1.6 christos symwidth, symoff); 3750 1.8 christos return false; 3751 1.1 christos } 3752 1.1 christos 3753 1.1 christos stroff = bfd_h_get_32 (abfd, raw.n_strx); 3754 1.1 christos type = bfd_h_get_8 (abfd, raw.n_type); 3755 1.1 christos symtype = type & BFD_MACH_O_N_TYPE; 3756 1.1 christos section = bfd_h_get_8 (abfd, raw.n_sect); 3757 1.1 christos desc = bfd_h_get_16 (abfd, raw.n_desc); 3758 1.1 christos if (wide) 3759 1.1 christos value = bfd_h_get_64 (abfd, raw.n_value); 3760 1.1 christos else 3761 1.1 christos value = bfd_h_get_32 (abfd, raw.n_value); 3762 1.1 christos 3763 1.1 christos if (stroff >= sym->strsize) 3764 1.1 christos { 3765 1.6 christos _bfd_error_handler 3766 1.6 christos /* xgettext:c-format */ 3767 1.6 christos (_("bfd_mach_o_read_symtab_symbol: name out of range (%lu >= %u)"), 3768 1.6 christos stroff, 3769 1.6 christos sym->strsize); 3770 1.8 christos return false; 3771 1.1 christos } 3772 1.1 christos 3773 1.1 christos s->symbol.the_bfd = abfd; 3774 1.1 christos s->symbol.name = sym->strtab + stroff; 3775 1.1 christos s->symbol.value = value; 3776 1.1 christos s->symbol.flags = 0x0; 3777 1.1 christos s->symbol.udata.i = i; 3778 1.1 christos s->n_type = type; 3779 1.1 christos s->n_sect = section; 3780 1.1 christos s->n_desc = desc; 3781 1.1 christos 3782 1.1 christos if (type & BFD_MACH_O_N_STAB) 3783 1.1 christos { 3784 1.1 christos s->symbol.flags |= BSF_DEBUGGING; 3785 1.1 christos s->symbol.section = bfd_und_section_ptr; 3786 1.1 christos switch (type) 3787 1.1 christos { 3788 1.1 christos case N_FUN: 3789 1.1 christos case N_STSYM: 3790 1.1 christos case N_LCSYM: 3791 1.1 christos case N_BNSYM: 3792 1.1 christos case N_SLINE: 3793 1.1 christos case N_ENSYM: 3794 1.1 christos case N_ECOMM: 3795 1.1 christos case N_ECOML: 3796 1.1 christos case N_GSYM: 3797 1.1 christos if ((section > 0) && (section <= mdata->nsects)) 3798 1.1 christos { 3799 1.1 christos s->symbol.section = mdata->sections[section - 1]->bfdsection; 3800 1.1 christos s->symbol.value = 3801 1.6 christos s->symbol.value - mdata->sections[section - 1]->addr; 3802 1.1 christos } 3803 1.1 christos break; 3804 1.1 christos } 3805 1.1 christos } 3806 1.1 christos else 3807 1.1 christos { 3808 1.1 christos if (type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)) 3809 1.1 christos s->symbol.flags |= BSF_GLOBAL; 3810 1.1 christos else 3811 1.1 christos s->symbol.flags |= BSF_LOCAL; 3812 1.1 christos 3813 1.1 christos switch (symtype) 3814 1.1 christos { 3815 1.1 christos case BFD_MACH_O_N_UNDF: 3816 1.6 christos if (type == (BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT) 3817 1.6 christos && s->symbol.value != 0) 3818 1.6 christos { 3819 1.6 christos /* A common symbol. */ 3820 1.6 christos s->symbol.section = bfd_com_section_ptr; 3821 1.6 christos s->symbol.flags = BSF_NO_FLAGS; 3822 1.6 christos } 3823 1.6 christos else 3824 1.6 christos { 3825 1.6 christos s->symbol.section = bfd_und_section_ptr; 3826 1.6 christos if (s->n_desc & BFD_MACH_O_N_WEAK_REF) 3827 1.6 christos s->symbol.flags |= BSF_WEAK; 3828 1.6 christos } 3829 1.1 christos break; 3830 1.1 christos case BFD_MACH_O_N_PBUD: 3831 1.1 christos s->symbol.section = bfd_und_section_ptr; 3832 1.1 christos break; 3833 1.1 christos case BFD_MACH_O_N_ABS: 3834 1.1 christos s->symbol.section = bfd_abs_section_ptr; 3835 1.1 christos break; 3836 1.1 christos case BFD_MACH_O_N_SECT: 3837 1.1 christos if ((section > 0) && (section <= mdata->nsects)) 3838 1.1 christos { 3839 1.1 christos s->symbol.section = mdata->sections[section - 1]->bfdsection; 3840 1.1 christos s->symbol.value = 3841 1.6 christos s->symbol.value - mdata->sections[section - 1]->addr; 3842 1.1 christos } 3843 1.1 christos else 3844 1.1 christos { 3845 1.1 christos /* Mach-O uses 0 to mean "no section"; not an error. */ 3846 1.1 christos if (section != 0) 3847 1.1 christos { 3848 1.6 christos _bfd_error_handler 3849 1.6 christos /* xgettext:c-format */ 3850 1.6 christos (_("bfd_mach_o_read_symtab_symbol: " 3851 1.6 christos "symbol \"%s\" specified invalid section %d (max %lu): " 3852 1.6 christos "setting to undefined"), 3853 1.6 christos s->symbol.name, section, mdata->nsects); 3854 1.1 christos } 3855 1.1 christos s->symbol.section = bfd_und_section_ptr; 3856 1.1 christos } 3857 1.1 christos break; 3858 1.1 christos case BFD_MACH_O_N_INDR: 3859 1.1 christos /* FIXME: we don't follow the BFD convention as this indirect symbol 3860 1.1 christos won't be followed by the referenced one. This looks harmless 3861 1.1 christos unless we start using the linker. */ 3862 1.1 christos s->symbol.flags |= BSF_INDIRECT; 3863 1.1 christos s->symbol.section = bfd_ind_section_ptr; 3864 1.1 christos s->symbol.value = 0; 3865 1.1 christos break; 3866 1.1 christos default: 3867 1.6 christos _bfd_error_handler 3868 1.6 christos /* xgettext:c-format */ 3869 1.6 christos (_("bfd_mach_o_read_symtab_symbol: " 3870 1.6 christos "symbol \"%s\" specified invalid type field 0x%x: " 3871 1.6 christos "setting to undefined"), s->symbol.name, symtype); 3872 1.1 christos s->symbol.section = bfd_und_section_ptr; 3873 1.1 christos break; 3874 1.1 christos } 3875 1.1 christos } 3876 1.1 christos 3877 1.8 christos return true; 3878 1.1 christos } 3879 1.1 christos 3880 1.8 christos bool 3881 1.1 christos bfd_mach_o_read_symtab_strtab (bfd *abfd) 3882 1.1 christos { 3883 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 3884 1.1 christos bfd_mach_o_symtab_command *sym = mdata->symtab; 3885 1.1 christos 3886 1.1 christos /* Fail if there is no symtab. */ 3887 1.1 christos if (sym == NULL) 3888 1.8 christos return false; 3889 1.1 christos 3890 1.1 christos /* Success if already loaded. */ 3891 1.1 christos if (sym->strtab) 3892 1.8 christos return true; 3893 1.1 christos 3894 1.1 christos if (abfd->flags & BFD_IN_MEMORY) 3895 1.1 christos { 3896 1.1 christos struct bfd_in_memory *b; 3897 1.1 christos 3898 1.1 christos b = (struct bfd_in_memory *) abfd->iostream; 3899 1.1 christos 3900 1.1 christos if ((sym->stroff + sym->strsize) > b->size) 3901 1.1 christos { 3902 1.1 christos bfd_set_error (bfd_error_file_truncated); 3903 1.8 christos return false; 3904 1.1 christos } 3905 1.1 christos sym->strtab = (char *) b->buffer + sym->stroff; 3906 1.1 christos } 3907 1.1 christos else 3908 1.1 christos { 3909 1.6 christos /* See PR 21840 for a reproducer. */ 3910 1.6 christos if ((sym->strsize + 1) == 0) 3911 1.8 christos return false; 3912 1.8 christos if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0) 3913 1.8 christos return false; 3914 1.8 christos sym->strtab = (char *) _bfd_alloc_and_read (abfd, sym->strsize + 1, 3915 1.8 christos sym->strsize); 3916 1.1 christos if (sym->strtab == NULL) 3917 1.8 christos return false; 3918 1.1 christos 3919 1.3 christos /* Zero terminate the string table. */ 3920 1.3 christos sym->strtab[sym->strsize] = 0; 3921 1.1 christos } 3922 1.1 christos 3923 1.8 christos return true; 3924 1.1 christos } 3925 1.1 christos 3926 1.8 christos bool 3927 1.1 christos bfd_mach_o_read_symtab_symbols (bfd *abfd) 3928 1.1 christos { 3929 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 3930 1.1 christos bfd_mach_o_symtab_command *sym = mdata->symtab; 3931 1.1 christos unsigned long i; 3932 1.8 christos size_t amt; 3933 1.8 christos ufile_ptr filesize; 3934 1.1 christos 3935 1.8 christos if (sym == NULL || sym->nsyms == 0 || sym->symbols) 3936 1.3 christos /* Return now if there are no symbols or if already loaded. */ 3937 1.8 christos return true; 3938 1.8 christos 3939 1.8 christos filesize = bfd_get_file_size (abfd); 3940 1.8 christos if (filesize != 0) 3941 1.8 christos { 3942 1.8 christos unsigned int wide = mach_o_wide_p (&mdata->header); 3943 1.8 christos unsigned int symwidth 3944 1.8 christos = wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE; 3945 1.1 christos 3946 1.8 christos if (sym->symoff > filesize 3947 1.8 christos || sym->nsyms > (filesize - sym->symoff) / symwidth) 3948 1.8 christos { 3949 1.8 christos bfd_set_error (bfd_error_file_truncated); 3950 1.8 christos sym->nsyms = 0; 3951 1.8 christos return false; 3952 1.8 christos } 3953 1.8 christos } 3954 1.8 christos if (_bfd_mul_overflow (sym->nsyms, sizeof (bfd_mach_o_asymbol), &amt) 3955 1.8 christos || (sym->symbols = bfd_alloc (abfd, amt)) == NULL) 3956 1.1 christos { 3957 1.8 christos bfd_set_error (bfd_error_no_memory); 3958 1.3 christos sym->nsyms = 0; 3959 1.8 christos return false; 3960 1.1 christos } 3961 1.1 christos 3962 1.1 christos if (!bfd_mach_o_read_symtab_strtab (abfd)) 3963 1.3 christos goto fail; 3964 1.1 christos 3965 1.1 christos for (i = 0; i < sym->nsyms; i++) 3966 1.3 christos if (!bfd_mach_o_read_symtab_symbol (abfd, sym, &sym->symbols[i], i)) 3967 1.3 christos goto fail; 3968 1.1 christos 3969 1.8 christos return true; 3970 1.3 christos 3971 1.3 christos fail: 3972 1.3 christos bfd_release (abfd, sym->symbols); 3973 1.3 christos sym->symbols = NULL; 3974 1.3 christos sym->nsyms = 0; 3975 1.8 christos return false; 3976 1.1 christos } 3977 1.1 christos 3978 1.1 christos static const char * 3979 1.1 christos bfd_mach_o_i386_flavour_string (unsigned int flavour) 3980 1.1 christos { 3981 1.1 christos switch ((int) flavour) 3982 1.1 christos { 3983 1.1 christos case BFD_MACH_O_x86_THREAD_STATE32: return "x86_THREAD_STATE32"; 3984 1.1 christos case BFD_MACH_O_x86_FLOAT_STATE32: return "x86_FLOAT_STATE32"; 3985 1.1 christos case BFD_MACH_O_x86_EXCEPTION_STATE32: return "x86_EXCEPTION_STATE32"; 3986 1.1 christos case BFD_MACH_O_x86_THREAD_STATE64: return "x86_THREAD_STATE64"; 3987 1.1 christos case BFD_MACH_O_x86_FLOAT_STATE64: return "x86_FLOAT_STATE64"; 3988 1.1 christos case BFD_MACH_O_x86_EXCEPTION_STATE64: return "x86_EXCEPTION_STATE64"; 3989 1.1 christos case BFD_MACH_O_x86_THREAD_STATE: return "x86_THREAD_STATE"; 3990 1.1 christos case BFD_MACH_O_x86_FLOAT_STATE: return "x86_FLOAT_STATE"; 3991 1.1 christos case BFD_MACH_O_x86_EXCEPTION_STATE: return "x86_EXCEPTION_STATE"; 3992 1.1 christos case BFD_MACH_O_x86_DEBUG_STATE32: return "x86_DEBUG_STATE32"; 3993 1.1 christos case BFD_MACH_O_x86_DEBUG_STATE64: return "x86_DEBUG_STATE64"; 3994 1.1 christos case BFD_MACH_O_x86_DEBUG_STATE: return "x86_DEBUG_STATE"; 3995 1.1 christos case BFD_MACH_O_x86_THREAD_STATE_NONE: return "x86_THREAD_STATE_NONE"; 3996 1.1 christos default: return "UNKNOWN"; 3997 1.1 christos } 3998 1.1 christos } 3999 1.1 christos 4000 1.1 christos static const char * 4001 1.1 christos bfd_mach_o_ppc_flavour_string (unsigned int flavour) 4002 1.1 christos { 4003 1.1 christos switch ((int) flavour) 4004 1.1 christos { 4005 1.1 christos case BFD_MACH_O_PPC_THREAD_STATE: return "PPC_THREAD_STATE"; 4006 1.1 christos case BFD_MACH_O_PPC_FLOAT_STATE: return "PPC_FLOAT_STATE"; 4007 1.1 christos case BFD_MACH_O_PPC_EXCEPTION_STATE: return "PPC_EXCEPTION_STATE"; 4008 1.1 christos case BFD_MACH_O_PPC_VECTOR_STATE: return "PPC_VECTOR_STATE"; 4009 1.1 christos case BFD_MACH_O_PPC_THREAD_STATE64: return "PPC_THREAD_STATE64"; 4010 1.1 christos case BFD_MACH_O_PPC_EXCEPTION_STATE64: return "PPC_EXCEPTION_STATE64"; 4011 1.1 christos default: return "UNKNOWN"; 4012 1.1 christos } 4013 1.1 christos } 4014 1.1 christos 4015 1.8 christos static unsigned char * 4016 1.9 christos bfd_mach_o_alloc_and_read (bfd *abfd, file_ptr filepos, 4017 1.9 christos size_t size, size_t extra) 4018 1.8 christos { 4019 1.8 christos if (bfd_seek (abfd, filepos, SEEK_SET) != 0) 4020 1.8 christos return NULL; 4021 1.9 christos unsigned char *ret = _bfd_alloc_and_read (abfd, size + extra, size); 4022 1.9 christos if (ret && extra != 0) 4023 1.9 christos memset (ret + size, 0, extra); 4024 1.9 christos return ret; 4025 1.8 christos } 4026 1.8 christos 4027 1.8 christos static bool 4028 1.1 christos bfd_mach_o_read_dylinker (bfd *abfd, bfd_mach_o_load_command *command) 4029 1.1 christos { 4030 1.1 christos bfd_mach_o_dylinker_command *cmd = &command->command.dylinker; 4031 1.1 christos struct mach_o_str_command_external raw; 4032 1.1 christos unsigned int nameoff; 4033 1.9 christos size_t namelen; 4034 1.1 christos 4035 1.7 christos if (command->len < sizeof (raw) + 8) 4036 1.8 christos return false; 4037 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4038 1.8 christos return false; 4039 1.1 christos 4040 1.1 christos nameoff = bfd_h_get_32 (abfd, raw.str); 4041 1.7 christos if (nameoff > command->len) 4042 1.8 christos return false; 4043 1.1 christos 4044 1.3 christos cmd->name_offset = nameoff; 4045 1.3 christos namelen = command->len - nameoff; 4046 1.3 christos nameoff += command->offset; 4047 1.9 christos cmd->name_str = (char *) bfd_mach_o_alloc_and_read (abfd, nameoff, 4048 1.9 christos namelen, 1); 4049 1.8 christos return cmd->name_str != NULL; 4050 1.1 christos } 4051 1.1 christos 4052 1.8 christos static bool 4053 1.1 christos bfd_mach_o_read_dylib (bfd *abfd, bfd_mach_o_load_command *command) 4054 1.1 christos { 4055 1.5 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 4056 1.1 christos bfd_mach_o_dylib_command *cmd = &command->command.dylib; 4057 1.1 christos struct mach_o_dylib_command_external raw; 4058 1.1 christos unsigned int nameoff; 4059 1.9 christos size_t namelen; 4060 1.8 christos file_ptr pos; 4061 1.1 christos 4062 1.7 christos if (command->len < sizeof (raw) + 8) 4063 1.8 christos return false; 4064 1.1 christos switch (command->type) 4065 1.1 christos { 4066 1.1 christos case BFD_MACH_O_LC_LOAD_DYLIB: 4067 1.3 christos case BFD_MACH_O_LC_LAZY_LOAD_DYLIB: 4068 1.1 christos case BFD_MACH_O_LC_LOAD_WEAK_DYLIB: 4069 1.1 christos case BFD_MACH_O_LC_ID_DYLIB: 4070 1.1 christos case BFD_MACH_O_LC_REEXPORT_DYLIB: 4071 1.1 christos case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB: 4072 1.1 christos break; 4073 1.1 christos default: 4074 1.1 christos BFD_FAIL (); 4075 1.8 christos return false; 4076 1.1 christos } 4077 1.1 christos 4078 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4079 1.8 christos return false; 4080 1.1 christos 4081 1.1 christos nameoff = bfd_h_get_32 (abfd, raw.name); 4082 1.7 christos if (nameoff > command->len) 4083 1.8 christos return false; 4084 1.1 christos cmd->timestamp = bfd_h_get_32 (abfd, raw.timestamp); 4085 1.1 christos cmd->current_version = bfd_h_get_32 (abfd, raw.current_version); 4086 1.1 christos cmd->compatibility_version = bfd_h_get_32 (abfd, raw.compatibility_version); 4087 1.1 christos 4088 1.1 christos cmd->name_offset = command->offset + nameoff; 4089 1.3 christos namelen = command->len - nameoff; 4090 1.8 christos pos = mdata->hdr_offset + cmd->name_offset; 4091 1.9 christos cmd->name_str = (char *) bfd_mach_o_alloc_and_read (abfd, pos, namelen, 1); 4092 1.8 christos return cmd->name_str != NULL; 4093 1.3 christos } 4094 1.3 christos 4095 1.8 christos static bool 4096 1.3 christos bfd_mach_o_read_prebound_dylib (bfd *abfd, 4097 1.6 christos bfd_mach_o_load_command *command) 4098 1.3 christos { 4099 1.3 christos bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; 4100 1.3 christos struct mach_o_prebound_dylib_command_external raw; 4101 1.3 christos unsigned int nameoff; 4102 1.3 christos unsigned int modoff; 4103 1.3 christos unsigned int str_len; 4104 1.3 christos unsigned char *str; 4105 1.3 christos 4106 1.7 christos if (command->len < sizeof (raw) + 8) 4107 1.8 christos return false; 4108 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4109 1.8 christos return false; 4110 1.3 christos 4111 1.3 christos nameoff = bfd_h_get_32 (abfd, raw.name); 4112 1.3 christos modoff = bfd_h_get_32 (abfd, raw.linked_modules); 4113 1.3 christos if (nameoff > command->len || modoff > command->len) 4114 1.8 christos return false; 4115 1.3 christos 4116 1.3 christos str_len = command->len - sizeof (raw); 4117 1.8 christos str = _bfd_alloc_and_read (abfd, str_len, str_len); 4118 1.3 christos if (str == NULL) 4119 1.8 christos return false; 4120 1.3 christos 4121 1.3 christos cmd->name_offset = command->offset + nameoff; 4122 1.3 christos cmd->nmodules = bfd_h_get_32 (abfd, raw.nmodules); 4123 1.3 christos cmd->linked_modules_offset = command->offset + modoff; 4124 1.3 christos 4125 1.3 christos cmd->name_str = (char *)str + nameoff - (sizeof (raw) + BFD_MACH_O_LC_SIZE); 4126 1.3 christos cmd->linked_modules = str + modoff - (sizeof (raw) + BFD_MACH_O_LC_SIZE); 4127 1.8 christos return true; 4128 1.3 christos } 4129 1.3 christos 4130 1.8 christos static bool 4131 1.3 christos bfd_mach_o_read_prebind_cksum (bfd *abfd, 4132 1.3 christos bfd_mach_o_load_command *command) 4133 1.3 christos { 4134 1.3 christos bfd_mach_o_prebind_cksum_command *cmd = &command->command.prebind_cksum; 4135 1.3 christos struct mach_o_prebind_cksum_command_external raw; 4136 1.3 christos 4137 1.7 christos if (command->len < sizeof (raw) + 8) 4138 1.8 christos return false; 4139 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4140 1.8 christos return false; 4141 1.3 christos 4142 1.3 christos cmd->cksum = bfd_get_32 (abfd, raw.cksum); 4143 1.8 christos return true; 4144 1.1 christos } 4145 1.1 christos 4146 1.8 christos static bool 4147 1.3 christos bfd_mach_o_read_twolevel_hints (bfd *abfd, 4148 1.3 christos bfd_mach_o_load_command *command) 4149 1.1 christos { 4150 1.3 christos bfd_mach_o_twolevel_hints_command *cmd = &command->command.twolevel_hints; 4151 1.3 christos struct mach_o_twolevel_hints_command_external raw; 4152 1.3 christos 4153 1.7 christos if (command->len < sizeof (raw) + 8) 4154 1.8 christos return false; 4155 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4156 1.8 christos return false; 4157 1.1 christos 4158 1.3 christos cmd->offset = bfd_get_32 (abfd, raw.offset); 4159 1.3 christos cmd->nhints = bfd_get_32 (abfd, raw.nhints); 4160 1.8 christos return true; 4161 1.1 christos } 4162 1.1 christos 4163 1.8 christos static bool 4164 1.1 christos bfd_mach_o_read_fvmlib (bfd *abfd, bfd_mach_o_load_command *command) 4165 1.1 christos { 4166 1.1 christos bfd_mach_o_fvmlib_command *fvm = &command->command.fvmlib; 4167 1.1 christos struct mach_o_fvmlib_command_external raw; 4168 1.1 christos unsigned int nameoff; 4169 1.9 christos size_t namelen; 4170 1.1 christos 4171 1.7 christos if (command->len < sizeof (raw) + 8) 4172 1.8 christos return false; 4173 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4174 1.8 christos return false; 4175 1.1 christos 4176 1.1 christos nameoff = bfd_h_get_32 (abfd, raw.name); 4177 1.7 christos if (nameoff > command->len) 4178 1.8 christos return false; 4179 1.1 christos fvm->minor_version = bfd_h_get_32 (abfd, raw.minor_version); 4180 1.1 christos fvm->header_addr = bfd_h_get_32 (abfd, raw.header_addr); 4181 1.1 christos 4182 1.1 christos fvm->name_offset = command->offset + nameoff; 4183 1.3 christos namelen = command->len - nameoff; 4184 1.8 christos fvm->name_str = (char *) bfd_mach_o_alloc_and_read (abfd, fvm->name_offset, 4185 1.9 christos namelen, 1); 4186 1.8 christos return fvm->name_str != NULL; 4187 1.1 christos } 4188 1.1 christos 4189 1.8 christos static bool 4190 1.1 christos bfd_mach_o_read_thread (bfd *abfd, bfd_mach_o_load_command *command) 4191 1.1 christos { 4192 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 4193 1.1 christos bfd_mach_o_thread_command *cmd = &command->command.thread; 4194 1.1 christos unsigned int offset; 4195 1.1 christos unsigned int nflavours; 4196 1.1 christos unsigned int i; 4197 1.7 christos struct mach_o_thread_command_external raw; 4198 1.8 christos size_t amt; 4199 1.1 christos 4200 1.1 christos BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD) 4201 1.1 christos || (command->type == BFD_MACH_O_LC_UNIXTHREAD)); 4202 1.1 christos 4203 1.1 christos /* Count the number of threads. */ 4204 1.1 christos offset = 8; 4205 1.1 christos nflavours = 0; 4206 1.7 christos while (offset + sizeof (raw) <= command->len) 4207 1.1 christos { 4208 1.7 christos unsigned int count; 4209 1.1 christos 4210 1.1 christos if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0 4211 1.9 christos || bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4212 1.8 christos return false; 4213 1.1 christos 4214 1.7 christos count = bfd_h_get_32 (abfd, raw.count); 4215 1.7 christos if (count > (unsigned) -1 / 4 4216 1.7 christos || command->len - (offset + sizeof (raw)) < count * 4) 4217 1.8 christos return false; 4218 1.7 christos offset += sizeof (raw) + count * 4; 4219 1.1 christos nflavours++; 4220 1.1 christos } 4221 1.7 christos if (nflavours == 0 || offset != command->len) 4222 1.8 christos return false; 4223 1.1 christos 4224 1.1 christos /* Allocate threads. */ 4225 1.8 christos if (_bfd_mul_overflow (nflavours, sizeof (bfd_mach_o_thread_flavour), &amt)) 4226 1.8 christos { 4227 1.8 christos bfd_set_error (bfd_error_file_too_big); 4228 1.8 christos return false; 4229 1.8 christos } 4230 1.8 christos cmd->flavours = bfd_alloc (abfd, amt); 4231 1.1 christos if (cmd->flavours == NULL) 4232 1.8 christos return false; 4233 1.1 christos cmd->nflavours = nflavours; 4234 1.1 christos 4235 1.1 christos offset = 8; 4236 1.1 christos nflavours = 0; 4237 1.1 christos while (offset != command->len) 4238 1.1 christos { 4239 1.1 christos if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0 4240 1.9 christos || bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4241 1.8 christos return false; 4242 1.1 christos 4243 1.1 christos cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, raw.flavour); 4244 1.1 christos cmd->flavours[nflavours].offset = command->offset + offset + sizeof (raw); 4245 1.1 christos cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, raw.count) * 4; 4246 1.1 christos offset += cmd->flavours[nflavours].size + sizeof (raw); 4247 1.1 christos nflavours++; 4248 1.1 christos } 4249 1.1 christos 4250 1.1 christos for (i = 0; i < nflavours; i++) 4251 1.1 christos { 4252 1.1 christos asection *bfdsec; 4253 1.9 christos size_t snamelen; 4254 1.1 christos char *sname; 4255 1.1 christos const char *flavourstr; 4256 1.1 christos const char *prefix = "LC_THREAD"; 4257 1.1 christos unsigned int j = 0; 4258 1.1 christos 4259 1.1 christos switch (mdata->header.cputype) 4260 1.1 christos { 4261 1.1 christos case BFD_MACH_O_CPU_TYPE_POWERPC: 4262 1.1 christos case BFD_MACH_O_CPU_TYPE_POWERPC_64: 4263 1.3 christos flavourstr = 4264 1.3 christos bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour); 4265 1.1 christos break; 4266 1.1 christos case BFD_MACH_O_CPU_TYPE_I386: 4267 1.1 christos case BFD_MACH_O_CPU_TYPE_X86_64: 4268 1.3 christos flavourstr = 4269 1.3 christos bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour); 4270 1.1 christos break; 4271 1.1 christos default: 4272 1.1 christos flavourstr = "UNKNOWN_ARCHITECTURE"; 4273 1.1 christos break; 4274 1.1 christos } 4275 1.1 christos 4276 1.1 christos snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1; 4277 1.1 christos sname = bfd_alloc (abfd, snamelen); 4278 1.1 christos if (sname == NULL) 4279 1.8 christos return false; 4280 1.1 christos 4281 1.1 christos for (;;) 4282 1.1 christos { 4283 1.1 christos sprintf (sname, "%s.%s.%u", prefix, flavourstr, j); 4284 1.1 christos if (bfd_get_section_by_name (abfd, sname) == NULL) 4285 1.1 christos break; 4286 1.1 christos j++; 4287 1.1 christos } 4288 1.1 christos 4289 1.1 christos bfdsec = bfd_make_section_with_flags (abfd, sname, SEC_HAS_CONTENTS); 4290 1.1 christos 4291 1.1 christos bfdsec->vma = 0; 4292 1.1 christos bfdsec->lma = 0; 4293 1.1 christos bfdsec->size = cmd->flavours[i].size; 4294 1.1 christos bfdsec->filepos = cmd->flavours[i].offset; 4295 1.1 christos bfdsec->alignment_power = 0x0; 4296 1.1 christos 4297 1.1 christos cmd->section = bfdsec; 4298 1.1 christos } 4299 1.1 christos 4300 1.8 christos return true; 4301 1.1 christos } 4302 1.1 christos 4303 1.8 christos static bool 4304 1.8 christos bfd_mach_o_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command, 4305 1.8 christos ufile_ptr filesize) 4306 1.1 christos { 4307 1.1 christos bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab; 4308 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 4309 1.1 christos 4310 1.1 christos BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB); 4311 1.1 christos 4312 1.1 christos { 4313 1.1 christos struct mach_o_dysymtab_command_external raw; 4314 1.1 christos 4315 1.7 christos if (command->len < sizeof (raw) + 8) 4316 1.8 christos return false; 4317 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4318 1.8 christos return false; 4319 1.1 christos 4320 1.1 christos cmd->ilocalsym = bfd_h_get_32 (abfd, raw.ilocalsym); 4321 1.1 christos cmd->nlocalsym = bfd_h_get_32 (abfd, raw.nlocalsym); 4322 1.1 christos cmd->iextdefsym = bfd_h_get_32 (abfd, raw.iextdefsym); 4323 1.1 christos cmd->nextdefsym = bfd_h_get_32 (abfd, raw.nextdefsym); 4324 1.1 christos cmd->iundefsym = bfd_h_get_32 (abfd, raw.iundefsym); 4325 1.1 christos cmd->nundefsym = bfd_h_get_32 (abfd, raw.nundefsym); 4326 1.1 christos cmd->tocoff = bfd_h_get_32 (abfd, raw.tocoff); 4327 1.1 christos cmd->ntoc = bfd_h_get_32 (abfd, raw.ntoc); 4328 1.1 christos cmd->modtaboff = bfd_h_get_32 (abfd, raw.modtaboff); 4329 1.1 christos cmd->nmodtab = bfd_h_get_32 (abfd, raw.nmodtab); 4330 1.1 christos cmd->extrefsymoff = bfd_h_get_32 (abfd, raw.extrefsymoff); 4331 1.1 christos cmd->nextrefsyms = bfd_h_get_32 (abfd, raw.nextrefsyms); 4332 1.1 christos cmd->indirectsymoff = bfd_h_get_32 (abfd, raw.indirectsymoff); 4333 1.1 christos cmd->nindirectsyms = bfd_h_get_32 (abfd, raw.nindirectsyms); 4334 1.1 christos cmd->extreloff = bfd_h_get_32 (abfd, raw.extreloff); 4335 1.1 christos cmd->nextrel = bfd_h_get_32 (abfd, raw.nextrel); 4336 1.1 christos cmd->locreloff = bfd_h_get_32 (abfd, raw.locreloff); 4337 1.1 christos cmd->nlocrel = bfd_h_get_32 (abfd, raw.nlocrel); 4338 1.1 christos } 4339 1.1 christos 4340 1.1 christos if (cmd->nmodtab != 0) 4341 1.1 christos { 4342 1.1 christos unsigned int i; 4343 1.1 christos int wide = bfd_mach_o_wide_p (abfd); 4344 1.1 christos unsigned int module_len = wide ? 56 : 52; 4345 1.8 christos size_t amt; 4346 1.1 christos 4347 1.8 christos if (cmd->modtaboff > filesize 4348 1.8 christos || cmd->nmodtab > (filesize - cmd->modtaboff) / module_len) 4349 1.8 christos { 4350 1.8 christos bfd_set_error (bfd_error_file_truncated); 4351 1.8 christos return false; 4352 1.8 christos } 4353 1.8 christos if (_bfd_mul_overflow (cmd->nmodtab, 4354 1.8 christos sizeof (bfd_mach_o_dylib_module), &amt)) 4355 1.8 christos { 4356 1.8 christos bfd_set_error (bfd_error_file_too_big); 4357 1.8 christos return false; 4358 1.8 christos } 4359 1.8 christos cmd->dylib_module = bfd_alloc (abfd, amt); 4360 1.1 christos if (cmd->dylib_module == NULL) 4361 1.8 christos return false; 4362 1.1 christos 4363 1.1 christos if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0) 4364 1.8 christos return false; 4365 1.1 christos 4366 1.1 christos for (i = 0; i < cmd->nmodtab; i++) 4367 1.6 christos { 4368 1.6 christos bfd_mach_o_dylib_module *module = &cmd->dylib_module[i]; 4369 1.6 christos unsigned long v; 4370 1.6 christos unsigned char buf[56]; 4371 1.6 christos 4372 1.9 christos if (bfd_read (buf, module_len, abfd) != module_len) 4373 1.8 christos return false; 4374 1.6 christos 4375 1.6 christos module->module_name_idx = bfd_h_get_32 (abfd, buf + 0); 4376 1.6 christos module->iextdefsym = bfd_h_get_32 (abfd, buf + 4); 4377 1.6 christos module->nextdefsym = bfd_h_get_32 (abfd, buf + 8); 4378 1.6 christos module->irefsym = bfd_h_get_32 (abfd, buf + 12); 4379 1.6 christos module->nrefsym = bfd_h_get_32 (abfd, buf + 16); 4380 1.6 christos module->ilocalsym = bfd_h_get_32 (abfd, buf + 20); 4381 1.6 christos module->nlocalsym = bfd_h_get_32 (abfd, buf + 24); 4382 1.6 christos module->iextrel = bfd_h_get_32 (abfd, buf + 28); 4383 1.6 christos module->nextrel = bfd_h_get_32 (abfd, buf + 32); 4384 1.6 christos v = bfd_h_get_32 (abfd, buf +36); 4385 1.6 christos module->iinit = v & 0xffff; 4386 1.6 christos module->iterm = (v >> 16) & 0xffff; 4387 1.6 christos v = bfd_h_get_32 (abfd, buf + 40); 4388 1.6 christos module->ninit = v & 0xffff; 4389 1.6 christos module->nterm = (v >> 16) & 0xffff; 4390 1.6 christos if (wide) 4391 1.6 christos { 4392 1.6 christos module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 44); 4393 1.6 christos module->objc_module_info_addr = bfd_h_get_64 (abfd, buf + 48); 4394 1.6 christos } 4395 1.6 christos else 4396 1.6 christos { 4397 1.6 christos module->objc_module_info_addr = bfd_h_get_32 (abfd, buf + 44); 4398 1.6 christos module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 48); 4399 1.6 christos } 4400 1.6 christos } 4401 1.1 christos } 4402 1.1 christos 4403 1.1 christos if (cmd->ntoc != 0) 4404 1.1 christos { 4405 1.3 christos unsigned long i; 4406 1.8 christos size_t amt; 4407 1.8 christos struct mach_o_dylib_table_of_contents_external raw; 4408 1.1 christos 4409 1.8 christos if (cmd->tocoff > filesize 4410 1.8 christos || cmd->ntoc > (filesize - cmd->tocoff) / sizeof (raw)) 4411 1.8 christos { 4412 1.8 christos bfd_set_error (bfd_error_file_truncated); 4413 1.8 christos return false; 4414 1.8 christos } 4415 1.8 christos if (_bfd_mul_overflow (cmd->ntoc, 4416 1.8 christos sizeof (bfd_mach_o_dylib_table_of_content), &amt)) 4417 1.8 christos { 4418 1.8 christos bfd_set_error (bfd_error_file_too_big); 4419 1.8 christos return false; 4420 1.8 christos } 4421 1.8 christos cmd->dylib_toc = bfd_alloc (abfd, amt); 4422 1.1 christos if (cmd->dylib_toc == NULL) 4423 1.8 christos return false; 4424 1.1 christos 4425 1.1 christos if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0) 4426 1.8 christos return false; 4427 1.1 christos 4428 1.1 christos for (i = 0; i < cmd->ntoc; i++) 4429 1.6 christos { 4430 1.6 christos bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i]; 4431 1.6 christos 4432 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4433 1.8 christos return false; 4434 1.6 christos 4435 1.6 christos toc->symbol_index = bfd_h_get_32 (abfd, raw.symbol_index); 4436 1.6 christos toc->module_index = bfd_h_get_32 (abfd, raw.module_index); 4437 1.6 christos } 4438 1.1 christos } 4439 1.1 christos 4440 1.1 christos if (cmd->nindirectsyms != 0) 4441 1.1 christos { 4442 1.1 christos unsigned int i; 4443 1.8 christos size_t amt; 4444 1.1 christos 4445 1.8 christos if (cmd->indirectsymoff > filesize 4446 1.8 christos || cmd->nindirectsyms > (filesize - cmd->indirectsymoff) / 4) 4447 1.8 christos { 4448 1.8 christos bfd_set_error (bfd_error_file_truncated); 4449 1.8 christos return false; 4450 1.8 christos } 4451 1.8 christos if (_bfd_mul_overflow (cmd->nindirectsyms, sizeof (unsigned int), &amt)) 4452 1.8 christos { 4453 1.8 christos bfd_set_error (bfd_error_file_too_big); 4454 1.8 christos return false; 4455 1.8 christos } 4456 1.8 christos cmd->indirect_syms = bfd_alloc (abfd, amt); 4457 1.1 christos if (cmd->indirect_syms == NULL) 4458 1.8 christos return false; 4459 1.1 christos 4460 1.1 christos if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0) 4461 1.8 christos return false; 4462 1.1 christos 4463 1.1 christos for (i = 0; i < cmd->nindirectsyms; i++) 4464 1.6 christos { 4465 1.6 christos unsigned char raw[4]; 4466 1.6 christos unsigned int *is = &cmd->indirect_syms[i]; 4467 1.1 christos 4468 1.9 christos if (bfd_read (raw, sizeof (raw), abfd) != sizeof (raw)) 4469 1.8 christos return false; 4470 1.1 christos 4471 1.6 christos *is = bfd_h_get_32 (abfd, raw); 4472 1.6 christos } 4473 1.1 christos } 4474 1.1 christos 4475 1.1 christos if (cmd->nextrefsyms != 0) 4476 1.1 christos { 4477 1.1 christos unsigned long v; 4478 1.1 christos unsigned int i; 4479 1.8 christos size_t amt; 4480 1.1 christos 4481 1.8 christos if (cmd->extrefsymoff > filesize 4482 1.8 christos || cmd->nextrefsyms > (filesize - cmd->extrefsymoff) / 4) 4483 1.8 christos { 4484 1.8 christos bfd_set_error (bfd_error_file_truncated); 4485 1.8 christos return false; 4486 1.8 christos } 4487 1.8 christos if (_bfd_mul_overflow (cmd->nextrefsyms, 4488 1.8 christos sizeof (bfd_mach_o_dylib_reference), &amt)) 4489 1.8 christos { 4490 1.8 christos bfd_set_error (bfd_error_file_too_big); 4491 1.8 christos return false; 4492 1.8 christos } 4493 1.8 christos cmd->ext_refs = bfd_alloc (abfd, amt); 4494 1.1 christos if (cmd->ext_refs == NULL) 4495 1.8 christos return false; 4496 1.1 christos 4497 1.1 christos if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0) 4498 1.8 christos return false; 4499 1.1 christos 4500 1.1 christos for (i = 0; i < cmd->nextrefsyms; i++) 4501 1.6 christos { 4502 1.6 christos unsigned char raw[4]; 4503 1.6 christos bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i]; 4504 1.6 christos 4505 1.9 christos if (bfd_read (raw, sizeof (raw), abfd) != sizeof (raw)) 4506 1.8 christos return false; 4507 1.6 christos 4508 1.6 christos /* Fields isym and flags are written as bit-fields, thus we need 4509 1.6 christos a specific processing for endianness. */ 4510 1.6 christos v = bfd_h_get_32 (abfd, raw); 4511 1.6 christos if (bfd_big_endian (abfd)) 4512 1.6 christos { 4513 1.6 christos ref->isym = (v >> 8) & 0xffffff; 4514 1.6 christos ref->flags = v & 0xff; 4515 1.6 christos } 4516 1.6 christos else 4517 1.6 christos { 4518 1.6 christos ref->isym = v & 0xffffff; 4519 1.6 christos ref->flags = (v >> 24) & 0xff; 4520 1.6 christos } 4521 1.6 christos } 4522 1.1 christos } 4523 1.1 christos 4524 1.1 christos if (mdata->dysymtab) 4525 1.8 christos return false; 4526 1.1 christos mdata->dysymtab = cmd; 4527 1.1 christos 4528 1.8 christos return true; 4529 1.1 christos } 4530 1.1 christos 4531 1.8 christos static bool 4532 1.8 christos bfd_mach_o_read_symtab (bfd *abfd, bfd_mach_o_load_command *command, 4533 1.8 christos ufile_ptr filesize) 4534 1.1 christos { 4535 1.1 christos bfd_mach_o_symtab_command *symtab = &command->command.symtab; 4536 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 4537 1.1 christos struct mach_o_symtab_command_external raw; 4538 1.1 christos 4539 1.1 christos BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB); 4540 1.1 christos 4541 1.7 christos if (command->len < sizeof (raw) + 8) 4542 1.8 christos return false; 4543 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4544 1.8 christos return false; 4545 1.1 christos 4546 1.1 christos symtab->symoff = bfd_h_get_32 (abfd, raw.symoff); 4547 1.1 christos symtab->nsyms = bfd_h_get_32 (abfd, raw.nsyms); 4548 1.1 christos symtab->stroff = bfd_h_get_32 (abfd, raw.stroff); 4549 1.1 christos symtab->strsize = bfd_h_get_32 (abfd, raw.strsize); 4550 1.1 christos symtab->symbols = NULL; 4551 1.1 christos symtab->strtab = NULL; 4552 1.1 christos 4553 1.8 christos if (symtab->symoff > filesize 4554 1.8 christos || symtab->nsyms > (filesize - symtab->symoff) / BFD_MACH_O_NLIST_SIZE 4555 1.8 christos || symtab->stroff > filesize 4556 1.8 christos || symtab->strsize > filesize - symtab->stroff) 4557 1.8 christos { 4558 1.8 christos bfd_set_error (bfd_error_file_truncated); 4559 1.8 christos return false; 4560 1.8 christos } 4561 1.8 christos 4562 1.1 christos if (symtab->nsyms != 0) 4563 1.1 christos abfd->flags |= HAS_SYMS; 4564 1.1 christos 4565 1.1 christos if (mdata->symtab) 4566 1.8 christos return false; 4567 1.1 christos mdata->symtab = symtab; 4568 1.8 christos return true; 4569 1.1 christos } 4570 1.1 christos 4571 1.8 christos static bool 4572 1.1 christos bfd_mach_o_read_uuid (bfd *abfd, bfd_mach_o_load_command *command) 4573 1.1 christos { 4574 1.1 christos bfd_mach_o_uuid_command *cmd = &command->command.uuid; 4575 1.1 christos 4576 1.1 christos BFD_ASSERT (command->type == BFD_MACH_O_LC_UUID); 4577 1.1 christos 4578 1.7 christos if (command->len < 16 + 8) 4579 1.8 christos return false; 4580 1.9 christos if (bfd_read (cmd->uuid, 16, abfd) != 16) 4581 1.8 christos return false; 4582 1.1 christos 4583 1.8 christos return true; 4584 1.1 christos } 4585 1.1 christos 4586 1.8 christos static bool 4587 1.1 christos bfd_mach_o_read_linkedit (bfd *abfd, bfd_mach_o_load_command *command) 4588 1.1 christos { 4589 1.1 christos bfd_mach_o_linkedit_command *cmd = &command->command.linkedit; 4590 1.1 christos struct mach_o_linkedit_data_command_external raw; 4591 1.1 christos 4592 1.7 christos if (command->len < sizeof (raw) + 8) 4593 1.8 christos return false; 4594 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4595 1.8 christos return false; 4596 1.1 christos 4597 1.1 christos cmd->dataoff = bfd_get_32 (abfd, raw.dataoff); 4598 1.1 christos cmd->datasize = bfd_get_32 (abfd, raw.datasize); 4599 1.8 christos return true; 4600 1.1 christos } 4601 1.1 christos 4602 1.8 christos static bool 4603 1.1 christos bfd_mach_o_read_str (bfd *abfd, bfd_mach_o_load_command *command) 4604 1.1 christos { 4605 1.1 christos bfd_mach_o_str_command *cmd = &command->command.str; 4606 1.1 christos struct mach_o_str_command_external raw; 4607 1.1 christos unsigned long off; 4608 1.1 christos 4609 1.7 christos if (command->len < sizeof (raw) + 8) 4610 1.8 christos return false; 4611 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4612 1.8 christos return false; 4613 1.1 christos 4614 1.1 christos off = bfd_get_32 (abfd, raw.str); 4615 1.7 christos if (off > command->len) 4616 1.8 christos return false; 4617 1.7 christos 4618 1.1 christos cmd->stroff = command->offset + off; 4619 1.1 christos cmd->str_len = command->len - off; 4620 1.8 christos cmd->str = (char *) bfd_mach_o_alloc_and_read (abfd, cmd->stroff, 4621 1.9 christos cmd->str_len, 1); 4622 1.8 christos return cmd->str != NULL; 4623 1.3 christos } 4624 1.3 christos 4625 1.8 christos static bool 4626 1.3 christos bfd_mach_o_read_dyld_content (bfd *abfd, bfd_mach_o_dyld_info_command *cmd) 4627 1.3 christos { 4628 1.3 christos /* Read rebase content. */ 4629 1.3 christos if (cmd->rebase_content == NULL && cmd->rebase_size != 0) 4630 1.3 christos { 4631 1.8 christos cmd->rebase_content 4632 1.9 christos = bfd_mach_o_alloc_and_read (abfd, cmd->rebase_off, 4633 1.9 christos cmd->rebase_size, 0); 4634 1.3 christos if (cmd->rebase_content == NULL) 4635 1.8 christos return false; 4636 1.3 christos } 4637 1.3 christos 4638 1.3 christos /* Read bind content. */ 4639 1.3 christos if (cmd->bind_content == NULL && cmd->bind_size != 0) 4640 1.3 christos { 4641 1.8 christos cmd->bind_content 4642 1.9 christos = bfd_mach_o_alloc_and_read (abfd, cmd->bind_off, 4643 1.9 christos cmd->bind_size, 0); 4644 1.3 christos if (cmd->bind_content == NULL) 4645 1.8 christos return false; 4646 1.3 christos } 4647 1.3 christos 4648 1.3 christos /* Read weak bind content. */ 4649 1.3 christos if (cmd->weak_bind_content == NULL && cmd->weak_bind_size != 0) 4650 1.3 christos { 4651 1.9 christos cmd->weak_bind_content 4652 1.9 christos = bfd_mach_o_alloc_and_read (abfd, cmd->weak_bind_off, 4653 1.9 christos cmd->weak_bind_size, 0); 4654 1.3 christos if (cmd->weak_bind_content == NULL) 4655 1.8 christos return false; 4656 1.3 christos } 4657 1.3 christos 4658 1.3 christos /* Read lazy bind content. */ 4659 1.3 christos if (cmd->lazy_bind_content == NULL && cmd->lazy_bind_size != 0) 4660 1.3 christos { 4661 1.9 christos cmd->lazy_bind_content 4662 1.9 christos = bfd_mach_o_alloc_and_read (abfd, cmd->lazy_bind_off, 4663 1.9 christos cmd->lazy_bind_size, 0); 4664 1.3 christos if (cmd->lazy_bind_content == NULL) 4665 1.8 christos return false; 4666 1.3 christos } 4667 1.3 christos 4668 1.3 christos /* Read export content. */ 4669 1.3 christos if (cmd->export_content == NULL && cmd->export_size != 0) 4670 1.3 christos { 4671 1.9 christos cmd->export_content 4672 1.9 christos = bfd_mach_o_alloc_and_read (abfd, cmd->export_off, 4673 1.9 christos cmd->export_size, 0); 4674 1.3 christos if (cmd->export_content == NULL) 4675 1.8 christos return false; 4676 1.3 christos } 4677 1.3 christos 4678 1.8 christos return true; 4679 1.1 christos } 4680 1.1 christos 4681 1.8 christos static bool 4682 1.1 christos bfd_mach_o_read_dyld_info (bfd *abfd, bfd_mach_o_load_command *command) 4683 1.1 christos { 4684 1.1 christos bfd_mach_o_dyld_info_command *cmd = &command->command.dyld_info; 4685 1.1 christos struct mach_o_dyld_info_command_external raw; 4686 1.1 christos 4687 1.7 christos if (command->len < sizeof (raw) + 8) 4688 1.8 christos return false; 4689 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4690 1.8 christos return false; 4691 1.1 christos 4692 1.1 christos cmd->rebase_off = bfd_get_32 (abfd, raw.rebase_off); 4693 1.1 christos cmd->rebase_size = bfd_get_32 (abfd, raw.rebase_size); 4694 1.3 christos cmd->rebase_content = NULL; 4695 1.1 christos cmd->bind_off = bfd_get_32 (abfd, raw.bind_off); 4696 1.1 christos cmd->bind_size = bfd_get_32 (abfd, raw.bind_size); 4697 1.3 christos cmd->bind_content = NULL; 4698 1.1 christos cmd->weak_bind_off = bfd_get_32 (abfd, raw.weak_bind_off); 4699 1.1 christos cmd->weak_bind_size = bfd_get_32 (abfd, raw.weak_bind_size); 4700 1.3 christos cmd->weak_bind_content = NULL; 4701 1.1 christos cmd->lazy_bind_off = bfd_get_32 (abfd, raw.lazy_bind_off); 4702 1.1 christos cmd->lazy_bind_size = bfd_get_32 (abfd, raw.lazy_bind_size); 4703 1.3 christos cmd->lazy_bind_content = NULL; 4704 1.1 christos cmd->export_off = bfd_get_32 (abfd, raw.export_off); 4705 1.1 christos cmd->export_size = bfd_get_32 (abfd, raw.export_size); 4706 1.3 christos cmd->export_content = NULL; 4707 1.8 christos return true; 4708 1.1 christos } 4709 1.1 christos 4710 1.8 christos static bool 4711 1.1 christos bfd_mach_o_read_version_min (bfd *abfd, bfd_mach_o_load_command *command) 4712 1.1 christos { 4713 1.1 christos bfd_mach_o_version_min_command *cmd = &command->command.version_min; 4714 1.1 christos struct mach_o_version_min_command_external raw; 4715 1.1 christos 4716 1.7 christos if (command->len < sizeof (raw) + 8) 4717 1.8 christos return false; 4718 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4719 1.8 christos return false; 4720 1.1 christos 4721 1.7 christos cmd->version = bfd_get_32 (abfd, raw.version); 4722 1.7 christos cmd->sdk = bfd_get_32 (abfd, raw.sdk); 4723 1.8 christos return true; 4724 1.1 christos } 4725 1.1 christos 4726 1.8 christos static bool 4727 1.1 christos bfd_mach_o_read_encryption_info (bfd *abfd, bfd_mach_o_load_command *command) 4728 1.1 christos { 4729 1.1 christos bfd_mach_o_encryption_info_command *cmd = &command->command.encryption_info; 4730 1.1 christos struct mach_o_encryption_info_command_external raw; 4731 1.1 christos 4732 1.7 christos if (command->len < sizeof (raw) + 8) 4733 1.8 christos return false; 4734 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4735 1.8 christos return false; 4736 1.5 christos 4737 1.5 christos cmd->cryptoff = bfd_get_32 (abfd, raw.cryptoff); 4738 1.5 christos cmd->cryptsize = bfd_get_32 (abfd, raw.cryptsize); 4739 1.5 christos cmd->cryptid = bfd_get_32 (abfd, raw.cryptid); 4740 1.8 christos return true; 4741 1.5 christos } 4742 1.5 christos 4743 1.8 christos static bool 4744 1.5 christos bfd_mach_o_read_encryption_info_64 (bfd *abfd, bfd_mach_o_load_command *command) 4745 1.5 christos { 4746 1.5 christos bfd_mach_o_encryption_info_command *cmd = &command->command.encryption_info; 4747 1.5 christos struct mach_o_encryption_info_64_command_external raw; 4748 1.5 christos 4749 1.7 christos if (command->len < sizeof (raw) + 8) 4750 1.8 christos return false; 4751 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4752 1.8 christos return false; 4753 1.1 christos 4754 1.1 christos cmd->cryptoff = bfd_get_32 (abfd, raw.cryptoff); 4755 1.1 christos cmd->cryptsize = bfd_get_32 (abfd, raw.cryptsize); 4756 1.1 christos cmd->cryptid = bfd_get_32 (abfd, raw.cryptid); 4757 1.8 christos return true; 4758 1.1 christos } 4759 1.1 christos 4760 1.8 christos static bool 4761 1.3 christos bfd_mach_o_read_main (bfd *abfd, bfd_mach_o_load_command *command) 4762 1.3 christos { 4763 1.3 christos bfd_mach_o_main_command *cmd = &command->command.main; 4764 1.3 christos struct mach_o_entry_point_command_external raw; 4765 1.3 christos 4766 1.7 christos if (command->len < sizeof (raw) + 8) 4767 1.8 christos return false; 4768 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4769 1.8 christos return false; 4770 1.3 christos 4771 1.3 christos cmd->entryoff = bfd_get_64 (abfd, raw.entryoff); 4772 1.3 christos cmd->stacksize = bfd_get_64 (abfd, raw.stacksize); 4773 1.8 christos return true; 4774 1.3 christos } 4775 1.3 christos 4776 1.8 christos static bool 4777 1.3 christos bfd_mach_o_read_source_version (bfd *abfd, bfd_mach_o_load_command *command) 4778 1.3 christos { 4779 1.3 christos bfd_mach_o_source_version_command *cmd = &command->command.source_version; 4780 1.3 christos struct mach_o_source_version_command_external raw; 4781 1.8 christos uint64_t ver; 4782 1.3 christos 4783 1.7 christos if (command->len < sizeof (raw) + 8) 4784 1.8 christos return false; 4785 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4786 1.8 christos return false; 4787 1.3 christos 4788 1.3 christos ver = bfd_get_64 (abfd, raw.version); 4789 1.3 christos /* Note: we use a serie of shift to avoid shift > 32 (for which gcc 4790 1.3 christos generates warnings) in case of the host doesn't support 64 bit 4791 1.3 christos integers. */ 4792 1.3 christos cmd->e = ver & 0x3ff; 4793 1.3 christos ver >>= 10; 4794 1.3 christos cmd->d = ver & 0x3ff; 4795 1.3 christos ver >>= 10; 4796 1.3 christos cmd->c = ver & 0x3ff; 4797 1.3 christos ver >>= 10; 4798 1.3 christos cmd->b = ver & 0x3ff; 4799 1.3 christos ver >>= 10; 4800 1.3 christos cmd->a = ver & 0xffffff; 4801 1.8 christos return true; 4802 1.3 christos } 4803 1.3 christos 4804 1.8 christos static bool 4805 1.7 christos bfd_mach_o_read_note (bfd *abfd, bfd_mach_o_load_command *command) 4806 1.7 christos { 4807 1.7 christos bfd_mach_o_note_command *cmd = &command->command.note; 4808 1.7 christos struct mach_o_note_command_external raw; 4809 1.7 christos 4810 1.7 christos if (command->len < sizeof (raw) + 8) 4811 1.8 christos return false; 4812 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4813 1.8 christos return false; 4814 1.7 christos 4815 1.7 christos memcpy (cmd->data_owner, raw.data_owner, 16); 4816 1.7 christos cmd->offset = bfd_get_64 (abfd, raw.offset); 4817 1.7 christos cmd->size = bfd_get_64 (abfd, raw.size); 4818 1.8 christos return true; 4819 1.7 christos } 4820 1.7 christos 4821 1.8 christos static bool 4822 1.7 christos bfd_mach_o_read_build_version (bfd *abfd, bfd_mach_o_load_command *command) 4823 1.7 christos { 4824 1.7 christos bfd_mach_o_build_version_command *cmd = &command->command.build_version; 4825 1.7 christos struct mach_o_build_version_command_external raw; 4826 1.7 christos 4827 1.7 christos if (command->len < sizeof (raw) + 8) 4828 1.8 christos return false; 4829 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4830 1.8 christos return false; 4831 1.7 christos 4832 1.7 christos cmd->platform = bfd_get_32 (abfd, raw.platform); 4833 1.7 christos cmd->minos = bfd_get_32 (abfd, raw.minos); 4834 1.7 christos cmd->sdk = bfd_get_32 (abfd, raw.sdk); 4835 1.7 christos cmd->ntools = bfd_get_32 (abfd, raw.ntools); 4836 1.8 christos return true; 4837 1.7 christos } 4838 1.7 christos 4839 1.8 christos static bool 4840 1.1 christos bfd_mach_o_read_segment (bfd *abfd, 4841 1.6 christos bfd_mach_o_load_command *command, 4842 1.6 christos unsigned int wide) 4843 1.1 christos { 4844 1.1 christos bfd_mach_o_segment_command *seg = &command->command.segment; 4845 1.1 christos unsigned long i; 4846 1.1 christos 4847 1.1 christos if (wide) 4848 1.1 christos { 4849 1.1 christos struct mach_o_segment_command_64_external raw; 4850 1.1 christos 4851 1.1 christos BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64); 4852 1.1 christos 4853 1.7 christos if (command->len < sizeof (raw) + 8) 4854 1.8 christos return false; 4855 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4856 1.8 christos return false; 4857 1.1 christos 4858 1.1 christos memcpy (seg->segname, raw.segname, 16); 4859 1.1 christos seg->segname[16] = '\0'; 4860 1.1 christos 4861 1.1 christos seg->vmaddr = bfd_h_get_64 (abfd, raw.vmaddr); 4862 1.1 christos seg->vmsize = bfd_h_get_64 (abfd, raw.vmsize); 4863 1.1 christos seg->fileoff = bfd_h_get_64 (abfd, raw.fileoff); 4864 1.1 christos seg->filesize = bfd_h_get_64 (abfd, raw.filesize); 4865 1.1 christos seg->maxprot = bfd_h_get_32 (abfd, raw.maxprot); 4866 1.1 christos seg->initprot = bfd_h_get_32 (abfd, raw.initprot); 4867 1.1 christos seg->nsects = bfd_h_get_32 (abfd, raw.nsects); 4868 1.1 christos seg->flags = bfd_h_get_32 (abfd, raw.flags); 4869 1.1 christos } 4870 1.1 christos else 4871 1.1 christos { 4872 1.1 christos struct mach_o_segment_command_32_external raw; 4873 1.1 christos 4874 1.1 christos BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT); 4875 1.1 christos 4876 1.7 christos if (command->len < sizeof (raw) + 8) 4877 1.8 christos return false; 4878 1.9 christos if (bfd_read (&raw, sizeof (raw), abfd) != sizeof (raw)) 4879 1.8 christos return false; 4880 1.1 christos 4881 1.1 christos memcpy (seg->segname, raw.segname, 16); 4882 1.1 christos seg->segname[16] = '\0'; 4883 1.1 christos 4884 1.1 christos seg->vmaddr = bfd_h_get_32 (abfd, raw.vmaddr); 4885 1.1 christos seg->vmsize = bfd_h_get_32 (abfd, raw.vmsize); 4886 1.1 christos seg->fileoff = bfd_h_get_32 (abfd, raw.fileoff); 4887 1.1 christos seg->filesize = bfd_h_get_32 (abfd, raw.filesize); 4888 1.1 christos seg->maxprot = bfd_h_get_32 (abfd, raw.maxprot); 4889 1.1 christos seg->initprot = bfd_h_get_32 (abfd, raw.initprot); 4890 1.1 christos seg->nsects = bfd_h_get_32 (abfd, raw.nsects); 4891 1.1 christos seg->flags = bfd_h_get_32 (abfd, raw.flags); 4892 1.1 christos } 4893 1.1 christos seg->sect_head = NULL; 4894 1.1 christos seg->sect_tail = NULL; 4895 1.1 christos 4896 1.1 christos for (i = 0; i < seg->nsects; i++) 4897 1.1 christos { 4898 1.1 christos asection *sec; 4899 1.1 christos 4900 1.5 christos sec = bfd_mach_o_read_section (abfd, seg->initprot, wide); 4901 1.1 christos if (sec == NULL) 4902 1.8 christos return false; 4903 1.1 christos 4904 1.3 christos bfd_mach_o_append_section_to_segment 4905 1.3 christos (seg, bfd_mach_o_get_mach_o_section (sec)); 4906 1.1 christos } 4907 1.1 christos 4908 1.8 christos return true; 4909 1.1 christos } 4910 1.1 christos 4911 1.8 christos static bool 4912 1.1 christos bfd_mach_o_read_segment_32 (bfd *abfd, bfd_mach_o_load_command *command) 4913 1.1 christos { 4914 1.1 christos return bfd_mach_o_read_segment (abfd, command, 0); 4915 1.1 christos } 4916 1.1 christos 4917 1.8 christos static bool 4918 1.1 christos bfd_mach_o_read_segment_64 (bfd *abfd, bfd_mach_o_load_command *command) 4919 1.1 christos { 4920 1.1 christos return bfd_mach_o_read_segment (abfd, command, 1); 4921 1.1 christos } 4922 1.1 christos 4923 1.8 christos static bool 4924 1.8 christos bfd_mach_o_read_command (bfd *abfd, bfd_mach_o_load_command *command, 4925 1.8 christos ufile_ptr filesize) 4926 1.1 christos { 4927 1.5 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 4928 1.1 christos struct mach_o_load_command_external raw; 4929 1.1 christos unsigned int cmd; 4930 1.1 christos 4931 1.1 christos /* Read command type and length. */ 4932 1.5 christos if (bfd_seek (abfd, mdata->hdr_offset + command->offset, SEEK_SET) != 0 4933 1.9 christos || bfd_read (&raw, BFD_MACH_O_LC_SIZE, abfd) != BFD_MACH_O_LC_SIZE) 4934 1.8 christos return false; 4935 1.1 christos 4936 1.1 christos cmd = bfd_h_get_32 (abfd, raw.cmd); 4937 1.7 christos command->type = cmd & ~BFD_MACH_O_LC_REQ_DYLD; 4938 1.8 christos command->type_required = (cmd & BFD_MACH_O_LC_REQ_DYLD) != 0; 4939 1.1 christos command->len = bfd_h_get_32 (abfd, raw.cmdsize); 4940 1.7 christos if (command->len < 8 || command->len % 4 != 0) 4941 1.8 christos return false; 4942 1.1 christos 4943 1.1 christos switch (command->type) 4944 1.1 christos { 4945 1.1 christos case BFD_MACH_O_LC_SEGMENT: 4946 1.3 christos if (!bfd_mach_o_read_segment_32 (abfd, command)) 4947 1.8 christos return false; 4948 1.1 christos break; 4949 1.1 christos case BFD_MACH_O_LC_SEGMENT_64: 4950 1.3 christos if (!bfd_mach_o_read_segment_64 (abfd, command)) 4951 1.8 christos return false; 4952 1.1 christos break; 4953 1.1 christos case BFD_MACH_O_LC_SYMTAB: 4954 1.8 christos if (!bfd_mach_o_read_symtab (abfd, command, filesize)) 4955 1.8 christos return false; 4956 1.1 christos break; 4957 1.1 christos case BFD_MACH_O_LC_SYMSEG: 4958 1.1 christos break; 4959 1.1 christos case BFD_MACH_O_LC_THREAD: 4960 1.1 christos case BFD_MACH_O_LC_UNIXTHREAD: 4961 1.3 christos if (!bfd_mach_o_read_thread (abfd, command)) 4962 1.8 christos return false; 4963 1.1 christos break; 4964 1.1 christos case BFD_MACH_O_LC_LOAD_DYLINKER: 4965 1.1 christos case BFD_MACH_O_LC_ID_DYLINKER: 4966 1.3 christos case BFD_MACH_O_LC_DYLD_ENVIRONMENT: 4967 1.3 christos if (!bfd_mach_o_read_dylinker (abfd, command)) 4968 1.8 christos return false; 4969 1.1 christos break; 4970 1.1 christos case BFD_MACH_O_LC_LOAD_DYLIB: 4971 1.3 christos case BFD_MACH_O_LC_LAZY_LOAD_DYLIB: 4972 1.1 christos case BFD_MACH_O_LC_ID_DYLIB: 4973 1.1 christos case BFD_MACH_O_LC_LOAD_WEAK_DYLIB: 4974 1.1 christos case BFD_MACH_O_LC_REEXPORT_DYLIB: 4975 1.1 christos case BFD_MACH_O_LC_LOAD_UPWARD_DYLIB: 4976 1.3 christos if (!bfd_mach_o_read_dylib (abfd, command)) 4977 1.8 christos return false; 4978 1.1 christos break; 4979 1.1 christos case BFD_MACH_O_LC_PREBOUND_DYLIB: 4980 1.3 christos if (!bfd_mach_o_read_prebound_dylib (abfd, command)) 4981 1.8 christos return false; 4982 1.1 christos break; 4983 1.1 christos case BFD_MACH_O_LC_LOADFVMLIB: 4984 1.1 christos case BFD_MACH_O_LC_IDFVMLIB: 4985 1.3 christos if (!bfd_mach_o_read_fvmlib (abfd, command)) 4986 1.8 christos return false; 4987 1.1 christos break; 4988 1.1 christos case BFD_MACH_O_LC_IDENT: 4989 1.1 christos case BFD_MACH_O_LC_FVMFILE: 4990 1.1 christos case BFD_MACH_O_LC_PREPAGE: 4991 1.1 christos case BFD_MACH_O_LC_ROUTINES: 4992 1.1 christos case BFD_MACH_O_LC_ROUTINES_64: 4993 1.1 christos break; 4994 1.1 christos case BFD_MACH_O_LC_SUB_FRAMEWORK: 4995 1.1 christos case BFD_MACH_O_LC_SUB_UMBRELLA: 4996 1.1 christos case BFD_MACH_O_LC_SUB_LIBRARY: 4997 1.1 christos case BFD_MACH_O_LC_SUB_CLIENT: 4998 1.1 christos case BFD_MACH_O_LC_RPATH: 4999 1.3 christos if (!bfd_mach_o_read_str (abfd, command)) 5000 1.8 christos return false; 5001 1.1 christos break; 5002 1.1 christos case BFD_MACH_O_LC_DYSYMTAB: 5003 1.8 christos if (!bfd_mach_o_read_dysymtab (abfd, command, filesize)) 5004 1.8 christos return false; 5005 1.3 christos break; 5006 1.3 christos case BFD_MACH_O_LC_PREBIND_CKSUM: 5007 1.3 christos if (!bfd_mach_o_read_prebind_cksum (abfd, command)) 5008 1.8 christos return false; 5009 1.1 christos break; 5010 1.1 christos case BFD_MACH_O_LC_TWOLEVEL_HINTS: 5011 1.3 christos if (!bfd_mach_o_read_twolevel_hints (abfd, command)) 5012 1.8 christos return false; 5013 1.1 christos break; 5014 1.1 christos case BFD_MACH_O_LC_UUID: 5015 1.3 christos if (!bfd_mach_o_read_uuid (abfd, command)) 5016 1.8 christos return false; 5017 1.1 christos break; 5018 1.1 christos case BFD_MACH_O_LC_CODE_SIGNATURE: 5019 1.1 christos case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO: 5020 1.1 christos case BFD_MACH_O_LC_FUNCTION_STARTS: 5021 1.3 christos case BFD_MACH_O_LC_DATA_IN_CODE: 5022 1.3 christos case BFD_MACH_O_LC_DYLIB_CODE_SIGN_DRS: 5023 1.5 christos case BFD_MACH_O_LC_LINKER_OPTIMIZATION_HINT: 5024 1.8 christos case BFD_MACH_O_LC_DYLD_EXPORTS_TRIE: 5025 1.8 christos case BFD_MACH_O_LC_DYLD_CHAINED_FIXUPS: 5026 1.3 christos if (!bfd_mach_o_read_linkedit (abfd, command)) 5027 1.8 christos return false; 5028 1.1 christos break; 5029 1.1 christos case BFD_MACH_O_LC_ENCRYPTION_INFO: 5030 1.1 christos if (!bfd_mach_o_read_encryption_info (abfd, command)) 5031 1.8 christos return false; 5032 1.1 christos break; 5033 1.5 christos case BFD_MACH_O_LC_ENCRYPTION_INFO_64: 5034 1.5 christos if (!bfd_mach_o_read_encryption_info_64 (abfd, command)) 5035 1.8 christos return false; 5036 1.5 christos break; 5037 1.1 christos case BFD_MACH_O_LC_DYLD_INFO: 5038 1.3 christos if (!bfd_mach_o_read_dyld_info (abfd, command)) 5039 1.8 christos return false; 5040 1.1 christos break; 5041 1.1 christos case BFD_MACH_O_LC_VERSION_MIN_MACOSX: 5042 1.1 christos case BFD_MACH_O_LC_VERSION_MIN_IPHONEOS: 5043 1.5 christos case BFD_MACH_O_LC_VERSION_MIN_WATCHOS: 5044 1.7 christos case BFD_MACH_O_LC_VERSION_MIN_TVOS: 5045 1.1 christos if (!bfd_mach_o_read_version_min (abfd, command)) 5046 1.8 christos return false; 5047 1.3 christos break; 5048 1.3 christos case BFD_MACH_O_LC_MAIN: 5049 1.3 christos if (!bfd_mach_o_read_main (abfd, command)) 5050 1.8 christos return false; 5051 1.3 christos break; 5052 1.3 christos case BFD_MACH_O_LC_SOURCE_VERSION: 5053 1.3 christos if (!bfd_mach_o_read_source_version (abfd, command)) 5054 1.8 christos return false; 5055 1.1 christos break; 5056 1.7 christos case BFD_MACH_O_LC_LINKER_OPTIONS: 5057 1.7 christos break; 5058 1.7 christos case BFD_MACH_O_LC_NOTE: 5059 1.7 christos if (!bfd_mach_o_read_note (abfd, command)) 5060 1.8 christos return false; 5061 1.7 christos break; 5062 1.7 christos case BFD_MACH_O_LC_BUILD_VERSION: 5063 1.7 christos if (!bfd_mach_o_read_build_version (abfd, command)) 5064 1.8 christos return false; 5065 1.7 christos break; 5066 1.1 christos default: 5067 1.3 christos command->len = 0; 5068 1.6 christos _bfd_error_handler (_("%pB: unknown load command %#x"), 5069 1.6 christos abfd, command->type); 5070 1.8 christos return false; 5071 1.1 christos } 5072 1.1 christos 5073 1.8 christos return true; 5074 1.1 christos } 5075 1.1 christos 5076 1.8 christos static bool 5077 1.1 christos bfd_mach_o_flatten_sections (bfd *abfd) 5078 1.1 christos { 5079 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 5080 1.3 christos bfd_mach_o_load_command *cmd; 5081 1.10 christos unsigned long csect; 5082 1.8 christos size_t amt; 5083 1.1 christos 5084 1.1 christos /* Count total number of sections. */ 5085 1.1 christos mdata->nsects = 0; 5086 1.1 christos 5087 1.3 christos for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next) 5088 1.1 christos { 5089 1.3 christos if (cmd->type == BFD_MACH_O_LC_SEGMENT 5090 1.3 christos || cmd->type == BFD_MACH_O_LC_SEGMENT_64) 5091 1.1 christos { 5092 1.3 christos bfd_mach_o_segment_command *seg = &cmd->command.segment; 5093 1.1 christos 5094 1.1 christos mdata->nsects += seg->nsects; 5095 1.1 christos } 5096 1.1 christos } 5097 1.1 christos 5098 1.1 christos /* Allocate sections array. */ 5099 1.8 christos if (_bfd_mul_overflow (mdata->nsects, sizeof (bfd_mach_o_section *), &amt)) 5100 1.8 christos { 5101 1.8 christos bfd_set_error (bfd_error_file_too_big); 5102 1.8 christos return false; 5103 1.8 christos } 5104 1.8 christos mdata->sections = bfd_alloc (abfd, amt); 5105 1.8 christos if (mdata->sections == NULL && mdata->nsects != 0) 5106 1.8 christos return false; 5107 1.1 christos 5108 1.1 christos /* Fill the array. */ 5109 1.1 christos csect = 0; 5110 1.1 christos 5111 1.3 christos for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next) 5112 1.1 christos { 5113 1.3 christos if (cmd->type == BFD_MACH_O_LC_SEGMENT 5114 1.3 christos || cmd->type == BFD_MACH_O_LC_SEGMENT_64) 5115 1.1 christos { 5116 1.3 christos bfd_mach_o_segment_command *seg = &cmd->command.segment; 5117 1.6 christos bfd_mach_o_section *sec; 5118 1.1 christos 5119 1.1 christos BFD_ASSERT (csect + seg->nsects <= mdata->nsects); 5120 1.1 christos 5121 1.6 christos for (sec = seg->sect_head; sec != NULL; sec = sec->next) 5122 1.1 christos mdata->sections[csect++] = sec; 5123 1.1 christos } 5124 1.1 christos } 5125 1.10 christos BFD_ASSERT (mdata->nsects == csect); 5126 1.8 christos return true; 5127 1.1 christos } 5128 1.1 christos 5129 1.8 christos static bool 5130 1.1 christos bfd_mach_o_scan_start_address (bfd *abfd) 5131 1.1 christos { 5132 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 5133 1.3 christos bfd_mach_o_thread_command *thr = NULL; 5134 1.3 christos bfd_mach_o_load_command *cmd; 5135 1.1 christos unsigned long i; 5136 1.1 christos 5137 1.3 christos for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next) 5138 1.3 christos if (cmd->type == BFD_MACH_O_LC_THREAD 5139 1.3 christos || cmd->type == BFD_MACH_O_LC_UNIXTHREAD) 5140 1.1 christos { 5141 1.6 christos thr = &cmd->command.thread; 5142 1.6 christos break; 5143 1.1 christos } 5144 1.3 christos else if (cmd->type == BFD_MACH_O_LC_MAIN && mdata->nsects > 1) 5145 1.3 christos { 5146 1.3 christos bfd_mach_o_main_command *main_cmd = &cmd->command.main; 5147 1.3 christos bfd_mach_o_section *text_sect = mdata->sections[0]; 5148 1.3 christos 5149 1.3 christos if (text_sect) 5150 1.3 christos { 5151 1.3 christos abfd->start_address = main_cmd->entryoff 5152 1.3 christos + (text_sect->addr - text_sect->offset); 5153 1.8 christos return true; 5154 1.3 christos } 5155 1.3 christos } 5156 1.1 christos 5157 1.3 christos /* An object file has no start address, so do not fail if not found. */ 5158 1.3 christos if (thr == NULL) 5159 1.8 christos return true; 5160 1.1 christos 5161 1.1 christos /* FIXME: create a subtarget hook ? */ 5162 1.3 christos for (i = 0; i < thr->nflavours; i++) 5163 1.1 christos { 5164 1.1 christos if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386) 5165 1.3 christos && (thr->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE32)) 5166 1.1 christos { 5167 1.1 christos unsigned char buf[4]; 5168 1.1 christos 5169 1.3 christos if (bfd_seek (abfd, thr->flavours[i].offset + 40, SEEK_SET) != 0 5170 1.9 christos || bfd_read (buf, 4, abfd) != 4) 5171 1.8 christos return false; 5172 1.1 christos 5173 1.1 christos abfd->start_address = bfd_h_get_32 (abfd, buf); 5174 1.1 christos } 5175 1.1 christos else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC) 5176 1.3 christos && (thr->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE)) 5177 1.1 christos { 5178 1.1 christos unsigned char buf[4]; 5179 1.1 christos 5180 1.3 christos if (bfd_seek (abfd, thr->flavours[i].offset + 0, SEEK_SET) != 0 5181 1.9 christos || bfd_read (buf, 4, abfd) != 4) 5182 1.8 christos return false; 5183 1.1 christos 5184 1.1 christos abfd->start_address = bfd_h_get_32 (abfd, buf); 5185 1.1 christos } 5186 1.1 christos else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC_64) 5187 1.6 christos && (thr->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE64)) 5188 1.6 christos { 5189 1.6 christos unsigned char buf[8]; 5190 1.1 christos 5191 1.6 christos if (bfd_seek (abfd, thr->flavours[i].offset + 0, SEEK_SET) != 0 5192 1.9 christos || bfd_read (buf, 8, abfd) != 8) 5193 1.8 christos return false; 5194 1.6 christos 5195 1.6 christos abfd->start_address = bfd_h_get_64 (abfd, buf); 5196 1.6 christos } 5197 1.1 christos else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_X86_64) 5198 1.6 christos && (thr->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE64)) 5199 1.6 christos { 5200 1.6 christos unsigned char buf[8]; 5201 1.6 christos 5202 1.6 christos if (bfd_seek (abfd, thr->flavours[i].offset + (16 * 8), SEEK_SET) != 0 5203 1.9 christos || bfd_read (buf, 8, abfd) != 8) 5204 1.8 christos return false; 5205 1.1 christos 5206 1.6 christos abfd->start_address = bfd_h_get_64 (abfd, buf); 5207 1.6 christos } 5208 1.1 christos } 5209 1.1 christos 5210 1.8 christos return true; 5211 1.1 christos } 5212 1.1 christos 5213 1.8 christos bool 5214 1.1 christos bfd_mach_o_set_arch_mach (bfd *abfd, 5215 1.6 christos enum bfd_architecture arch, 5216 1.6 christos unsigned long machine) 5217 1.1 christos { 5218 1.1 christos bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd); 5219 1.1 christos 5220 1.1 christos /* If this isn't the right architecture for this backend, and this 5221 1.1 christos isn't the generic backend, fail. */ 5222 1.1 christos if (arch != bed->arch 5223 1.1 christos && arch != bfd_arch_unknown 5224 1.1 christos && bed->arch != bfd_arch_unknown) 5225 1.8 christos return false; 5226 1.1 christos 5227 1.1 christos return bfd_default_set_arch_mach (abfd, arch, machine); 5228 1.1 christos } 5229 1.1 christos 5230 1.8 christos static bool 5231 1.1 christos bfd_mach_o_scan (bfd *abfd, 5232 1.1 christos bfd_mach_o_header *header, 5233 1.1 christos bfd_mach_o_data_struct *mdata) 5234 1.1 christos { 5235 1.1 christos unsigned int i; 5236 1.7 christos enum bfd_architecture cpu_type; 5237 1.7 christos unsigned long cpu_subtype; 5238 1.1 christos unsigned int hdrsize; 5239 1.1 christos 5240 1.1 christos hdrsize = mach_o_wide_p (header) ? 5241 1.1 christos BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE; 5242 1.1 christos 5243 1.1 christos mdata->header = *header; 5244 1.1 christos 5245 1.1 christos abfd->flags = abfd->flags & BFD_IN_MEMORY; 5246 1.1 christos switch (header->filetype) 5247 1.1 christos { 5248 1.1 christos case BFD_MACH_O_MH_OBJECT: 5249 1.1 christos abfd->flags |= HAS_RELOC; 5250 1.1 christos break; 5251 1.1 christos case BFD_MACH_O_MH_EXECUTE: 5252 1.1 christos abfd->flags |= EXEC_P; 5253 1.1 christos break; 5254 1.1 christos case BFD_MACH_O_MH_DYLIB: 5255 1.1 christos case BFD_MACH_O_MH_BUNDLE: 5256 1.1 christos abfd->flags |= DYNAMIC; 5257 1.1 christos break; 5258 1.1 christos } 5259 1.1 christos 5260 1.1 christos bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype, 5261 1.7 christos &cpu_type, &cpu_subtype); 5262 1.7 christos if (cpu_type == bfd_arch_unknown) 5263 1.1 christos { 5264 1.6 christos _bfd_error_handler 5265 1.6 christos /* xgettext:c-format */ 5266 1.6 christos (_("bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx"), 5267 1.6 christos header->cputype, header->cpusubtype); 5268 1.8 christos return false; 5269 1.1 christos } 5270 1.1 christos 5271 1.7 christos bfd_set_arch_mach (abfd, cpu_type, cpu_subtype); 5272 1.1 christos 5273 1.1 christos if (header->ncmds != 0) 5274 1.1 christos { 5275 1.3 christos bfd_mach_o_load_command *cmd; 5276 1.8 christos size_t amt; 5277 1.8 christos ufile_ptr filesize = bfd_get_file_size (abfd); 5278 1.8 christos 5279 1.8 christos if (filesize == 0) 5280 1.8 christos filesize = (ufile_ptr) -1; 5281 1.3 christos 5282 1.3 christos mdata->first_command = NULL; 5283 1.3 christos mdata->last_command = NULL; 5284 1.3 christos 5285 1.8 christos if (header->ncmds > (filesize - hdrsize) / BFD_MACH_O_LC_SIZE) 5286 1.8 christos { 5287 1.8 christos bfd_set_error (bfd_error_file_truncated); 5288 1.8 christos return false; 5289 1.8 christos } 5290 1.8 christos if (_bfd_mul_overflow (header->ncmds, 5291 1.8 christos sizeof (bfd_mach_o_load_command), &amt)) 5292 1.8 christos { 5293 1.8 christos bfd_set_error (bfd_error_file_too_big); 5294 1.8 christos return false; 5295 1.8 christos } 5296 1.8 christos cmd = bfd_alloc (abfd, amt); 5297 1.3 christos if (cmd == NULL) 5298 1.8 christos return false; 5299 1.1 christos 5300 1.1 christos for (i = 0; i < header->ncmds; i++) 5301 1.1 christos { 5302 1.3 christos bfd_mach_o_load_command *cur = &cmd[i]; 5303 1.3 christos 5304 1.3 christos bfd_mach_o_append_command (abfd, cur); 5305 1.1 christos 5306 1.1 christos if (i == 0) 5307 1.1 christos cur->offset = hdrsize; 5308 1.1 christos else 5309 1.1 christos { 5310 1.3 christos bfd_mach_o_load_command *prev = &cmd[i - 1]; 5311 1.1 christos cur->offset = prev->offset + prev->len; 5312 1.1 christos } 5313 1.1 christos 5314 1.8 christos if (!bfd_mach_o_read_command (abfd, cur, filesize)) 5315 1.10 christos { 5316 1.10 christos bfd_set_error (bfd_error_wrong_format); 5317 1.10 christos return false; 5318 1.10 christos } 5319 1.1 christos } 5320 1.1 christos } 5321 1.1 christos 5322 1.3 christos /* Sections should be flatten before scanning start address. */ 5323 1.8 christos if (!bfd_mach_o_flatten_sections (abfd)) 5324 1.8 christos return false; 5325 1.3 christos if (!bfd_mach_o_scan_start_address (abfd)) 5326 1.8 christos return false; 5327 1.1 christos 5328 1.8 christos return true; 5329 1.1 christos } 5330 1.1 christos 5331 1.8 christos bool 5332 1.1 christos bfd_mach_o_mkobject_init (bfd *abfd) 5333 1.1 christos { 5334 1.1 christos bfd_mach_o_data_struct *mdata = NULL; 5335 1.1 christos 5336 1.3 christos mdata = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct)); 5337 1.1 christos if (mdata == NULL) 5338 1.8 christos return false; 5339 1.1 christos abfd->tdata.mach_o_data = mdata; 5340 1.1 christos 5341 1.1 christos mdata->header.magic = 0; 5342 1.1 christos mdata->header.cputype = 0; 5343 1.1 christos mdata->header.cpusubtype = 0; 5344 1.1 christos mdata->header.filetype = 0; 5345 1.1 christos mdata->header.ncmds = 0; 5346 1.1 christos mdata->header.sizeofcmds = 0; 5347 1.1 christos mdata->header.flags = 0; 5348 1.1 christos mdata->header.byteorder = BFD_ENDIAN_UNKNOWN; 5349 1.3 christos mdata->first_command = NULL; 5350 1.3 christos mdata->last_command = NULL; 5351 1.1 christos mdata->nsects = 0; 5352 1.1 christos mdata->sections = NULL; 5353 1.1 christos mdata->dyn_reloc_cache = NULL; 5354 1.1 christos 5355 1.8 christos return true; 5356 1.1 christos } 5357 1.1 christos 5358 1.8 christos static bool 5359 1.1 christos bfd_mach_o_gen_mkobject (bfd *abfd) 5360 1.1 christos { 5361 1.1 christos bfd_mach_o_data_struct *mdata; 5362 1.1 christos 5363 1.1 christos if (!bfd_mach_o_mkobject_init (abfd)) 5364 1.8 christos return false; 5365 1.1 christos 5366 1.1 christos mdata = bfd_mach_o_get_data (abfd); 5367 1.1 christos mdata->header.magic = BFD_MACH_O_MH_MAGIC; 5368 1.1 christos mdata->header.cputype = 0; 5369 1.1 christos mdata->header.cpusubtype = 0; 5370 1.1 christos mdata->header.byteorder = abfd->xvec->byteorder; 5371 1.1 christos mdata->header.version = 1; 5372 1.1 christos 5373 1.8 christos return true; 5374 1.1 christos } 5375 1.1 christos 5376 1.8 christos bfd_cleanup 5377 1.1 christos bfd_mach_o_header_p (bfd *abfd, 5378 1.5 christos file_ptr hdr_off, 5379 1.7 christos bfd_mach_o_filetype file_type, 5380 1.7 christos bfd_mach_o_cpu_type cpu_type) 5381 1.1 christos { 5382 1.1 christos bfd_mach_o_header header; 5383 1.3 christos bfd_mach_o_data_struct *mdata; 5384 1.1 christos 5385 1.5 christos if (!bfd_mach_o_read_header (abfd, hdr_off, &header)) 5386 1.1 christos goto wrong; 5387 1.1 christos 5388 1.1 christos if (! (header.byteorder == BFD_ENDIAN_BIG 5389 1.1 christos || header.byteorder == BFD_ENDIAN_LITTLE)) 5390 1.1 christos { 5391 1.6 christos _bfd_error_handler (_("unknown header byte-order value %#x"), 5392 1.6 christos header.byteorder); 5393 1.1 christos goto wrong; 5394 1.1 christos } 5395 1.1 christos 5396 1.1 christos if (! ((header.byteorder == BFD_ENDIAN_BIG 5397 1.1 christos && abfd->xvec->byteorder == BFD_ENDIAN_BIG 5398 1.1 christos && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) 5399 1.1 christos || (header.byteorder == BFD_ENDIAN_LITTLE 5400 1.1 christos && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE 5401 1.1 christos && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE))) 5402 1.1 christos goto wrong; 5403 1.1 christos 5404 1.1 christos /* Check cputype and filetype. 5405 1.1 christos In case of wildcard, do not accept magics that are handled by existing 5406 1.1 christos targets. */ 5407 1.7 christos if (cpu_type) 5408 1.1 christos { 5409 1.7 christos if (header.cputype != cpu_type) 5410 1.6 christos goto wrong; 5411 1.1 christos } 5412 1.3 christos else 5413 1.3 christos { 5414 1.3 christos #ifndef BFD64 5415 1.3 christos /* Do not recognize 64 architectures if not configured for 64bit targets. 5416 1.3 christos This could happen only for generic targets. */ 5417 1.3 christos if (mach_o_wide_p (&header)) 5418 1.3 christos goto wrong; 5419 1.3 christos #endif 5420 1.3 christos } 5421 1.1 christos 5422 1.7 christos if (file_type) 5423 1.1 christos { 5424 1.7 christos if (header.filetype != file_type) 5425 1.6 christos goto wrong; 5426 1.1 christos } 5427 1.1 christos else 5428 1.1 christos { 5429 1.1 christos switch (header.filetype) 5430 1.6 christos { 5431 1.6 christos case BFD_MACH_O_MH_CORE: 5432 1.6 christos /* Handled by core_p */ 5433 1.6 christos goto wrong; 5434 1.6 christos default: 5435 1.6 christos break; 5436 1.6 christos } 5437 1.1 christos } 5438 1.1 christos 5439 1.3 christos mdata = (bfd_mach_o_data_struct *) bfd_zalloc (abfd, sizeof (*mdata)); 5440 1.3 christos if (mdata == NULL) 5441 1.10 christos return NULL; 5442 1.10 christos abfd->tdata.mach_o_data = mdata; 5443 1.10 christos 5444 1.5 christos mdata->hdr_offset = hdr_off; 5445 1.1 christos 5446 1.3 christos if (!bfd_mach_o_scan (abfd, &header, mdata)) 5447 1.10 christos { 5448 1.10 christos bfd_release (abfd, mdata); 5449 1.10 christos return NULL; 5450 1.10 christos } 5451 1.1 christos 5452 1.8 christos return _bfd_no_cleanup; 5453 1.1 christos 5454 1.1 christos wrong: 5455 1.1 christos bfd_set_error (bfd_error_wrong_format); 5456 1.1 christos return NULL; 5457 1.1 christos } 5458 1.1 christos 5459 1.8 christos static bfd_cleanup 5460 1.1 christos bfd_mach_o_gen_object_p (bfd *abfd) 5461 1.1 christos { 5462 1.5 christos return bfd_mach_o_header_p (abfd, 0, 0, 0); 5463 1.1 christos } 5464 1.1 christos 5465 1.8 christos static bfd_cleanup 5466 1.1 christos bfd_mach_o_gen_core_p (bfd *abfd) 5467 1.1 christos { 5468 1.5 christos return bfd_mach_o_header_p (abfd, 0, BFD_MACH_O_MH_CORE, 0); 5469 1.1 christos } 5470 1.1 christos 5471 1.3 christos /* Return the base address of ABFD, ie the address at which the image is 5472 1.3 christos mapped. The possible initial pagezero is ignored. */ 5473 1.3 christos 5474 1.3 christos bfd_vma 5475 1.3 christos bfd_mach_o_get_base_address (bfd *abfd) 5476 1.3 christos { 5477 1.3 christos bfd_mach_o_data_struct *mdata; 5478 1.3 christos bfd_mach_o_load_command *cmd; 5479 1.3 christos 5480 1.3 christos /* Check for Mach-O. */ 5481 1.3 christos if (!bfd_mach_o_valid (abfd)) 5482 1.3 christos return 0; 5483 1.3 christos mdata = bfd_mach_o_get_data (abfd); 5484 1.3 christos 5485 1.3 christos for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next) 5486 1.3 christos { 5487 1.3 christos if ((cmd->type == BFD_MACH_O_LC_SEGMENT 5488 1.3 christos || cmd->type == BFD_MACH_O_LC_SEGMENT_64)) 5489 1.3 christos { 5490 1.3 christos struct bfd_mach_o_segment_command *segcmd = &cmd->command.segment; 5491 1.3 christos 5492 1.3 christos if (segcmd->initprot != 0) 5493 1.3 christos return segcmd->vmaddr; 5494 1.3 christos } 5495 1.3 christos } 5496 1.3 christos return 0; 5497 1.3 christos } 5498 1.3 christos 5499 1.1 christos typedef struct mach_o_fat_archentry 5500 1.1 christos { 5501 1.1 christos unsigned long cputype; 5502 1.1 christos unsigned long cpusubtype; 5503 1.1 christos unsigned long offset; 5504 1.1 christos unsigned long size; 5505 1.1 christos unsigned long align; 5506 1.1 christos } mach_o_fat_archentry; 5507 1.1 christos 5508 1.1 christos typedef struct mach_o_fat_data_struct 5509 1.1 christos { 5510 1.1 christos unsigned long magic; 5511 1.1 christos unsigned long nfat_arch; 5512 1.1 christos mach_o_fat_archentry *archentries; 5513 1.1 christos } mach_o_fat_data_struct; 5514 1.1 christos 5515 1.9 christos /* Check for overlapping archive elements. Note that we can't allow 5516 1.9 christos multiple elements at the same offset even if one is empty, because 5517 1.9 christos bfd_mach_o_fat_openr_next_archived_file assume distinct offsets. */ 5518 1.9 christos static bool 5519 1.9 christos overlap_previous (const mach_o_fat_archentry *elt, unsigned long i) 5520 1.9 christos { 5521 1.9 christos unsigned long j = i; 5522 1.9 christos while (j-- != 0) 5523 1.9 christos if (elt[i].offset == elt[j].offset 5524 1.9 christos || (elt[i].offset > elt[j].offset 5525 1.9 christos ? elt[i].offset - elt[j].offset < elt[j].size 5526 1.9 christos : elt[j].offset - elt[i].offset < elt[i].size)) 5527 1.9 christos return true; 5528 1.9 christos return false; 5529 1.9 christos } 5530 1.9 christos 5531 1.8 christos bfd_cleanup 5532 1.5 christos bfd_mach_o_fat_archive_p (bfd *abfd) 5533 1.1 christos { 5534 1.1 christos mach_o_fat_data_struct *adata = NULL; 5535 1.1 christos struct mach_o_fat_header_external hdr; 5536 1.1 christos unsigned long i; 5537 1.8 christos size_t amt; 5538 1.8 christos ufile_ptr filesize; 5539 1.1 christos 5540 1.1 christos if (bfd_seek (abfd, 0, SEEK_SET) != 0 5541 1.9 christos || bfd_read (&hdr, sizeof (hdr), abfd) != sizeof (hdr)) 5542 1.10 christos { 5543 1.10 christos if (bfd_get_error () != bfd_error_system_call) 5544 1.10 christos goto wrong; 5545 1.10 christos goto error; 5546 1.10 christos } 5547 1.1 christos 5548 1.1 christos adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct)); 5549 1.1 christos if (adata == NULL) 5550 1.1 christos goto error; 5551 1.1 christos 5552 1.1 christos adata->magic = bfd_getb32 (hdr.magic); 5553 1.1 christos adata->nfat_arch = bfd_getb32 (hdr.nfat_arch); 5554 1.1 christos if (adata->magic != 0xcafebabe) 5555 1.10 christos goto wrong; 5556 1.1 christos /* Avoid matching Java bytecode files, which have the same magic number. 5557 1.1 christos In the Java bytecode file format this field contains the JVM version, 5558 1.1 christos which starts at 43.0. */ 5559 1.1 christos if (adata->nfat_arch > 30) 5560 1.10 christos goto wrong; 5561 1.1 christos 5562 1.8 christos if (_bfd_mul_overflow (adata->nfat_arch, 5563 1.8 christos sizeof (mach_o_fat_archentry), &amt)) 5564 1.8 christos { 5565 1.8 christos bfd_set_error (bfd_error_file_too_big); 5566 1.8 christos goto error; 5567 1.8 christos } 5568 1.8 christos adata->archentries = bfd_alloc (abfd, amt); 5569 1.1 christos if (adata->archentries == NULL) 5570 1.1 christos goto error; 5571 1.1 christos 5572 1.8 christos filesize = bfd_get_file_size (abfd); 5573 1.1 christos for (i = 0; i < adata->nfat_arch; i++) 5574 1.1 christos { 5575 1.1 christos struct mach_o_fat_arch_external arch; 5576 1.9 christos if (bfd_read (&arch, sizeof (arch), abfd) != sizeof (arch)) 5577 1.1 christos goto error; 5578 1.1 christos adata->archentries[i].cputype = bfd_getb32 (arch.cputype); 5579 1.1 christos adata->archentries[i].cpusubtype = bfd_getb32 (arch.cpusubtype); 5580 1.1 christos adata->archentries[i].offset = bfd_getb32 (arch.offset); 5581 1.1 christos adata->archentries[i].size = bfd_getb32 (arch.size); 5582 1.1 christos adata->archentries[i].align = bfd_getb32 (arch.align); 5583 1.9 christos if ((filesize != 0 5584 1.9 christos && (adata->archentries[i].offset > filesize 5585 1.9 christos || (adata->archentries[i].size 5586 1.9 christos > filesize - adata->archentries[i].offset))) 5587 1.9 christos || (adata->archentries[i].offset 5588 1.9 christos < sizeof (hdr) + adata->nfat_arch * sizeof (arch)) 5589 1.9 christos || overlap_previous (adata->archentries, i)) 5590 1.8 christos { 5591 1.8 christos bfd_release (abfd, adata); 5592 1.8 christos bfd_set_error (bfd_error_malformed_archive); 5593 1.8 christos return NULL; 5594 1.8 christos } 5595 1.1 christos } 5596 1.1 christos 5597 1.1 christos abfd->tdata.mach_o_fat_data = adata; 5598 1.3 christos 5599 1.8 christos return _bfd_no_cleanup; 5600 1.1 christos 5601 1.10 christos wrong: 5602 1.10 christos bfd_set_error (bfd_error_wrong_format); 5603 1.1 christos error: 5604 1.1 christos if (adata != NULL) 5605 1.1 christos bfd_release (abfd, adata); 5606 1.1 christos return NULL; 5607 1.1 christos } 5608 1.1 christos 5609 1.1 christos /* Set the filename for a fat binary member ABFD, whose bfd architecture is 5610 1.1 christos ARCH_TYPE/ARCH_SUBTYPE and corresponding entry in header is ENTRY. 5611 1.1 christos Set arelt_data and origin fields too. */ 5612 1.1 christos 5613 1.8 christos static bool 5614 1.1 christos bfd_mach_o_fat_member_init (bfd *abfd, 5615 1.6 christos enum bfd_architecture arch_type, 5616 1.6 christos unsigned long arch_subtype, 5617 1.6 christos mach_o_fat_archentry *entry) 5618 1.1 christos { 5619 1.1 christos struct areltdata *areltdata; 5620 1.1 christos /* Create the member filename. Use ARCH_NAME. */ 5621 1.1 christos const bfd_arch_info_type *ap = bfd_lookup_arch (arch_type, arch_subtype); 5622 1.8 christos const char *filename; 5623 1.1 christos 5624 1.1 christos if (ap) 5625 1.1 christos { 5626 1.1 christos /* Use the architecture name if known. */ 5627 1.8 christos filename = bfd_set_filename (abfd, ap->printable_name); 5628 1.1 christos } 5629 1.1 christos else 5630 1.1 christos { 5631 1.1 christos /* Forge a uniq id. */ 5632 1.8 christos char buf[2 + 8 + 1 + 2 + 8 + 1]; 5633 1.8 christos snprintf (buf, sizeof (buf), "0x%lx-0x%lx", 5634 1.6 christos entry->cputype, entry->cpusubtype); 5635 1.8 christos filename = bfd_set_filename (abfd, buf); 5636 1.1 christos } 5637 1.8 christos if (!filename) 5638 1.8 christos return false; 5639 1.1 christos 5640 1.3 christos areltdata = bfd_zmalloc (sizeof (struct areltdata)); 5641 1.7 christos if (areltdata == NULL) 5642 1.8 christos return false; 5643 1.1 christos areltdata->parsed_size = entry->size; 5644 1.1 christos abfd->arelt_data = areltdata; 5645 1.1 christos abfd->iostream = NULL; 5646 1.1 christos abfd->origin = entry->offset; 5647 1.8 christos return true; 5648 1.1 christos } 5649 1.1 christos 5650 1.1 christos bfd * 5651 1.5 christos bfd_mach_o_fat_openr_next_archived_file (bfd *archive, bfd *prev) 5652 1.1 christos { 5653 1.1 christos mach_o_fat_data_struct *adata; 5654 1.1 christos mach_o_fat_archentry *entry = NULL; 5655 1.1 christos unsigned long i; 5656 1.1 christos bfd *nbfd; 5657 1.1 christos enum bfd_architecture arch_type; 5658 1.1 christos unsigned long arch_subtype; 5659 1.1 christos 5660 1.1 christos adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data; 5661 1.1 christos BFD_ASSERT (adata != NULL); 5662 1.1 christos 5663 1.1 christos /* Find index of previous entry. */ 5664 1.1 christos if (prev == NULL) 5665 1.1 christos { 5666 1.1 christos /* Start at first one. */ 5667 1.1 christos i = 0; 5668 1.1 christos } 5669 1.1 christos else 5670 1.1 christos { 5671 1.1 christos /* Find index of PREV. */ 5672 1.1 christos for (i = 0; i < adata->nfat_arch; i++) 5673 1.1 christos { 5674 1.1 christos if (adata->archentries[i].offset == prev->origin) 5675 1.1 christos break; 5676 1.1 christos } 5677 1.1 christos 5678 1.1 christos if (i == adata->nfat_arch) 5679 1.1 christos { 5680 1.1 christos /* Not found. */ 5681 1.1 christos bfd_set_error (bfd_error_bad_value); 5682 1.1 christos return NULL; 5683 1.1 christos } 5684 1.1 christos 5685 1.1 christos /* Get next entry. */ 5686 1.1 christos i++; 5687 1.1 christos } 5688 1.1 christos 5689 1.1 christos if (i >= adata->nfat_arch) 5690 1.1 christos { 5691 1.1 christos bfd_set_error (bfd_error_no_more_archived_files); 5692 1.1 christos return NULL; 5693 1.1 christos } 5694 1.1 christos 5695 1.1 christos entry = &adata->archentries[i]; 5696 1.1 christos nbfd = _bfd_new_bfd_contained_in (archive); 5697 1.1 christos if (nbfd == NULL) 5698 1.1 christos return NULL; 5699 1.1 christos 5700 1.1 christos bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype, 5701 1.1 christos &arch_type, &arch_subtype); 5702 1.1 christos 5703 1.7 christos if (!bfd_mach_o_fat_member_init (nbfd, arch_type, arch_subtype, entry)) 5704 1.7 christos { 5705 1.7 christos bfd_close (nbfd); 5706 1.7 christos return NULL; 5707 1.7 christos } 5708 1.1 christos 5709 1.1 christos bfd_set_arch_mach (nbfd, arch_type, arch_subtype); 5710 1.1 christos 5711 1.1 christos return nbfd; 5712 1.1 christos } 5713 1.1 christos 5714 1.1 christos /* Analogous to stat call. */ 5715 1.1 christos 5716 1.1 christos static int 5717 1.1 christos bfd_mach_o_fat_stat_arch_elt (bfd *abfd, struct stat *buf) 5718 1.1 christos { 5719 1.1 christos if (abfd->arelt_data == NULL) 5720 1.1 christos { 5721 1.1 christos bfd_set_error (bfd_error_invalid_operation); 5722 1.1 christos return -1; 5723 1.1 christos } 5724 1.1 christos 5725 1.1 christos buf->st_mtime = 0; 5726 1.1 christos buf->st_uid = 0; 5727 1.1 christos buf->st_gid = 0; 5728 1.1 christos buf->st_mode = 0644; 5729 1.1 christos buf->st_size = arelt_size (abfd); 5730 1.1 christos 5731 1.1 christos return 0; 5732 1.1 christos } 5733 1.1 christos 5734 1.1 christos /* If ABFD format is FORMAT and architecture is ARCH, return it. 5735 1.1 christos If ABFD is a fat image containing a member that corresponds to FORMAT 5736 1.1 christos and ARCH, returns it. 5737 1.1 christos In other case, returns NULL. 5738 1.1 christos This function allows transparent uses of fat images. */ 5739 1.1 christos 5740 1.1 christos bfd * 5741 1.1 christos bfd_mach_o_fat_extract (bfd *abfd, 5742 1.1 christos bfd_format format, 5743 1.1 christos const bfd_arch_info_type *arch) 5744 1.1 christos { 5745 1.1 christos bfd *res; 5746 1.1 christos mach_o_fat_data_struct *adata; 5747 1.1 christos unsigned int i; 5748 1.1 christos 5749 1.1 christos if (bfd_check_format (abfd, format)) 5750 1.1 christos { 5751 1.1 christos if (bfd_get_arch_info (abfd) == arch) 5752 1.1 christos return abfd; 5753 1.1 christos return NULL; 5754 1.1 christos } 5755 1.1 christos if (!bfd_check_format (abfd, bfd_archive) 5756 1.1 christos || abfd->xvec != &mach_o_fat_vec) 5757 1.1 christos return NULL; 5758 1.1 christos 5759 1.1 christos /* This is a Mach-O fat image. */ 5760 1.1 christos adata = (mach_o_fat_data_struct *) abfd->tdata.mach_o_fat_data; 5761 1.1 christos BFD_ASSERT (adata != NULL); 5762 1.1 christos 5763 1.1 christos for (i = 0; i < adata->nfat_arch; i++) 5764 1.1 christos { 5765 1.1 christos struct mach_o_fat_archentry *e = &adata->archentries[i]; 5766 1.1 christos enum bfd_architecture cpu_type; 5767 1.1 christos unsigned long cpu_subtype; 5768 1.1 christos 5769 1.1 christos bfd_mach_o_convert_architecture (e->cputype, e->cpusubtype, 5770 1.1 christos &cpu_type, &cpu_subtype); 5771 1.1 christos if (cpu_type != arch->arch || cpu_subtype != arch->mach) 5772 1.1 christos continue; 5773 1.1 christos 5774 1.1 christos /* The architecture is found. */ 5775 1.1 christos res = _bfd_new_bfd_contained_in (abfd); 5776 1.1 christos if (res == NULL) 5777 1.1 christos return NULL; 5778 1.1 christos 5779 1.7 christos if (bfd_mach_o_fat_member_init (res, cpu_type, cpu_subtype, e) 5780 1.7 christos && bfd_check_format (res, format)) 5781 1.1 christos { 5782 1.1 christos BFD_ASSERT (bfd_get_arch_info (res) == arch); 5783 1.1 christos return res; 5784 1.1 christos } 5785 1.1 christos bfd_close (res); 5786 1.1 christos return NULL; 5787 1.1 christos } 5788 1.1 christos 5789 1.1 christos return NULL; 5790 1.1 christos } 5791 1.1 christos 5792 1.8 christos static bool 5793 1.7 christos bfd_mach_o_fat_close_and_cleanup (bfd *abfd) 5794 1.7 christos { 5795 1.7 christos _bfd_unlink_from_archive_parent (abfd); 5796 1.8 christos return true; 5797 1.7 christos } 5798 1.7 christos 5799 1.1 christos int 5800 1.1 christos bfd_mach_o_lookup_command (bfd *abfd, 5801 1.1 christos bfd_mach_o_load_command_type type, 5802 1.1 christos bfd_mach_o_load_command **mcommand) 5803 1.1 christos { 5804 1.3 christos struct mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 5805 1.3 christos struct bfd_mach_o_load_command *cmd; 5806 1.3 christos unsigned int num; 5807 1.1 christos 5808 1.3 christos BFD_ASSERT (mdata != NULL); 5809 1.1 christos BFD_ASSERT (mcommand != NULL); 5810 1.1 christos 5811 1.1 christos num = 0; 5812 1.3 christos for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next) 5813 1.1 christos { 5814 1.1 christos if (cmd->type != type) 5815 1.1 christos continue; 5816 1.1 christos 5817 1.1 christos if (num == 0) 5818 1.3 christos *mcommand = cmd; 5819 1.1 christos num++; 5820 1.1 christos } 5821 1.1 christos 5822 1.1 christos return num; 5823 1.1 christos } 5824 1.1 christos 5825 1.1 christos unsigned long 5826 1.1 christos bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type) 5827 1.1 christos { 5828 1.1 christos switch (type) 5829 1.1 christos { 5830 1.1 christos case BFD_MACH_O_CPU_TYPE_MC680x0: 5831 1.1 christos return 0x04000000; 5832 1.1 christos case BFD_MACH_O_CPU_TYPE_POWERPC: 5833 1.1 christos return 0xc0000000; 5834 1.1 christos case BFD_MACH_O_CPU_TYPE_I386: 5835 1.1 christos return 0xc0000000; 5836 1.1 christos case BFD_MACH_O_CPU_TYPE_SPARC: 5837 1.1 christos return 0xf0000000; 5838 1.1 christos case BFD_MACH_O_CPU_TYPE_HPPA: 5839 1.1 christos return 0xc0000000 - 0x04000000; 5840 1.1 christos default: 5841 1.1 christos return 0; 5842 1.1 christos } 5843 1.1 christos } 5844 1.1 christos 5845 1.1 christos /* The following two tables should be kept, as far as possible, in order of 5846 1.1 christos most frequently used entries to optimize their use from gas. */ 5847 1.1 christos 5848 1.1 christos const bfd_mach_o_xlat_name bfd_mach_o_section_type_name[] = 5849 1.1 christos { 5850 1.1 christos { "regular", BFD_MACH_O_S_REGULAR}, 5851 1.1 christos { "coalesced", BFD_MACH_O_S_COALESCED}, 5852 1.1 christos { "zerofill", BFD_MACH_O_S_ZEROFILL}, 5853 1.1 christos { "cstring_literals", BFD_MACH_O_S_CSTRING_LITERALS}, 5854 1.1 christos { "4byte_literals", BFD_MACH_O_S_4BYTE_LITERALS}, 5855 1.1 christos { "8byte_literals", BFD_MACH_O_S_8BYTE_LITERALS}, 5856 1.1 christos { "16byte_literals", BFD_MACH_O_S_16BYTE_LITERALS}, 5857 1.1 christos { "literal_pointers", BFD_MACH_O_S_LITERAL_POINTERS}, 5858 1.1 christos { "mod_init_func_pointers", BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS}, 5859 1.1 christos { "mod_fini_func_pointers", BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS}, 5860 1.1 christos { "gb_zerofill", BFD_MACH_O_S_GB_ZEROFILL}, 5861 1.1 christos { "interposing", BFD_MACH_O_S_INTERPOSING}, 5862 1.1 christos { "dtrace_dof", BFD_MACH_O_S_DTRACE_DOF}, 5863 1.1 christos { "non_lazy_symbol_pointers", BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS}, 5864 1.1 christos { "lazy_symbol_pointers", BFD_MACH_O_S_LAZY_SYMBOL_POINTERS}, 5865 1.1 christos { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS}, 5866 1.1 christos { "lazy_dylib_symbol_pointers", BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS}, 5867 1.1 christos { NULL, 0} 5868 1.1 christos }; 5869 1.1 christos 5870 1.1 christos const bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name[] = 5871 1.1 christos { 5872 1.1 christos { "pure_instructions", BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS }, 5873 1.1 christos { "some_instructions", BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS }, 5874 1.1 christos { "loc_reloc", BFD_MACH_O_S_ATTR_LOC_RELOC }, 5875 1.1 christos { "ext_reloc", BFD_MACH_O_S_ATTR_EXT_RELOC }, 5876 1.1 christos { "debug", BFD_MACH_O_S_ATTR_DEBUG }, 5877 1.1 christos { "live_support", BFD_MACH_O_S_ATTR_LIVE_SUPPORT }, 5878 1.1 christos { "no_dead_strip", BFD_MACH_O_S_ATTR_NO_DEAD_STRIP }, 5879 1.1 christos { "strip_static_syms", BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS }, 5880 1.1 christos { "no_toc", BFD_MACH_O_S_ATTR_NO_TOC }, 5881 1.1 christos { "self_modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE }, 5882 1.1 christos { "modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE }, 5883 1.1 christos { NULL, 0} 5884 1.1 christos }; 5885 1.1 christos 5886 1.1 christos /* Get the section type from NAME. Return 256 if NAME is unknown. */ 5887 1.1 christos 5888 1.1 christos unsigned int 5889 1.1 christos bfd_mach_o_get_section_type_from_name (bfd *abfd, const char *name) 5890 1.1 christos { 5891 1.1 christos const bfd_mach_o_xlat_name *x; 5892 1.1 christos bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd); 5893 1.1 christos 5894 1.1 christos for (x = bfd_mach_o_section_type_name; x->name; x++) 5895 1.1 christos if (strcmp (x->name, name) == 0) 5896 1.1 christos { 5897 1.1 christos /* We found it... does the target support it? */ 5898 1.1 christos if (bed->bfd_mach_o_section_type_valid_for_target == NULL 5899 1.1 christos || bed->bfd_mach_o_section_type_valid_for_target (x->val)) 5900 1.1 christos return x->val; /* OK. */ 5901 1.1 christos else 5902 1.1 christos break; /* Not supported. */ 5903 1.1 christos } 5904 1.1 christos /* Maximum section ID = 0xff. */ 5905 1.1 christos return 256; 5906 1.1 christos } 5907 1.1 christos 5908 1.1 christos /* Get the section attribute from NAME. Return -1 if NAME is unknown. */ 5909 1.1 christos 5910 1.1 christos unsigned int 5911 1.1 christos bfd_mach_o_get_section_attribute_from_name (const char *name) 5912 1.1 christos { 5913 1.1 christos const bfd_mach_o_xlat_name *x; 5914 1.1 christos 5915 1.1 christos for (x = bfd_mach_o_section_attribute_name; x->name; x++) 5916 1.1 christos if (strcmp (x->name, name) == 0) 5917 1.1 christos return x->val; 5918 1.1 christos return (unsigned int)-1; 5919 1.1 christos } 5920 1.1 christos 5921 1.1 christos int 5922 1.1 christos bfd_mach_o_core_fetch_environment (bfd *abfd, 5923 1.1 christos unsigned char **rbuf, 5924 1.1 christos unsigned int *rlen) 5925 1.1 christos { 5926 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 5927 1.1 christos unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype); 5928 1.3 christos bfd_mach_o_load_command *cmd; 5929 1.1 christos 5930 1.3 christos for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next) 5931 1.1 christos { 5932 1.3 christos bfd_mach_o_segment_command *seg; 5933 1.1 christos 5934 1.3 christos if (cmd->type != BFD_MACH_O_LC_SEGMENT) 5935 1.1 christos continue; 5936 1.1 christos 5937 1.3 christos seg = &cmd->command.segment; 5938 1.1 christos 5939 1.1 christos if ((seg->vmaddr + seg->vmsize) == stackaddr) 5940 1.1 christos { 5941 1.1 christos unsigned long start = seg->fileoff; 5942 1.1 christos unsigned long end = seg->fileoff + seg->filesize; 5943 1.1 christos unsigned char *buf = bfd_malloc (1024); 5944 1.1 christos unsigned long size = 1024; 5945 1.1 christos 5946 1.7 christos if (buf == NULL) 5947 1.7 christos return -1; 5948 1.1 christos for (;;) 5949 1.1 christos { 5950 1.1 christos bfd_size_type nread = 0; 5951 1.1 christos unsigned long offset; 5952 1.1 christos int found_nonnull = 0; 5953 1.1 christos 5954 1.1 christos if (size > (end - start)) 5955 1.1 christos size = (end - start); 5956 1.1 christos 5957 1.1 christos buf = bfd_realloc_or_free (buf, size); 5958 1.1 christos if (buf == NULL) 5959 1.1 christos return -1; 5960 1.1 christos 5961 1.1 christos if (bfd_seek (abfd, end - size, SEEK_SET) != 0) 5962 1.6 christos { 5963 1.6 christos free (buf); 5964 1.6 christos return -1; 5965 1.6 christos } 5966 1.1 christos 5967 1.9 christos nread = bfd_read (buf, size, abfd); 5968 1.1 christos 5969 1.1 christos if (nread != size) 5970 1.1 christos { 5971 1.1 christos free (buf); 5972 1.1 christos return -1; 5973 1.1 christos } 5974 1.1 christos 5975 1.1 christos for (offset = 4; offset <= size; offset += 4) 5976 1.1 christos { 5977 1.1 christos unsigned long val; 5978 1.1 christos 5979 1.8 christos val = bfd_get_32(abfd, buf + size - offset); 5980 1.8 christos 5981 1.1 christos if (! found_nonnull) 5982 1.1 christos { 5983 1.1 christos if (val != 0) 5984 1.1 christos found_nonnull = 1; 5985 1.1 christos } 5986 1.1 christos else if (val == 0x0) 5987 1.1 christos { 5988 1.1 christos unsigned long bottom; 5989 1.1 christos unsigned long top; 5990 1.1 christos 5991 1.1 christos bottom = seg->fileoff + seg->filesize - offset; 5992 1.1 christos top = seg->fileoff + seg->filesize - 4; 5993 1.1 christos *rbuf = bfd_malloc (top - bottom); 5994 1.7 christos if (*rbuf == NULL) 5995 1.7 christos return -1; 5996 1.1 christos *rlen = top - bottom; 5997 1.1 christos 5998 1.1 christos memcpy (*rbuf, buf + size - *rlen, *rlen); 5999 1.1 christos free (buf); 6000 1.1 christos return 0; 6001 1.1 christos } 6002 1.1 christos } 6003 1.1 christos 6004 1.1 christos if (size == (end - start)) 6005 1.1 christos break; 6006 1.1 christos 6007 1.1 christos size *= 2; 6008 1.1 christos } 6009 1.1 christos 6010 1.1 christos free (buf); 6011 1.1 christos } 6012 1.1 christos } 6013 1.1 christos 6014 1.1 christos return -1; 6015 1.1 christos } 6016 1.1 christos 6017 1.1 christos char * 6018 1.1 christos bfd_mach_o_core_file_failing_command (bfd *abfd) 6019 1.1 christos { 6020 1.1 christos unsigned char *buf = NULL; 6021 1.1 christos unsigned int len = 0; 6022 1.3 christos int ret; 6023 1.1 christos 6024 1.1 christos ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len); 6025 1.10 christos if (ret < 0 || len == 0) 6026 1.1 christos return NULL; 6027 1.10 christos buf[len - 1] = 0; 6028 1.1 christos return (char *) buf; 6029 1.1 christos } 6030 1.1 christos 6031 1.1 christos int 6032 1.1 christos bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED) 6033 1.1 christos { 6034 1.1 christos return 0; 6035 1.1 christos } 6036 1.1 christos 6037 1.1 christos static bfd_mach_o_uuid_command * 6038 1.1 christos bfd_mach_o_lookup_uuid_command (bfd *abfd) 6039 1.1 christos { 6040 1.6 christos bfd_mach_o_load_command *uuid_cmd = NULL; 6041 1.1 christos int ncmd = bfd_mach_o_lookup_command (abfd, BFD_MACH_O_LC_UUID, &uuid_cmd); 6042 1.6 christos if (ncmd != 1 || uuid_cmd == NULL) 6043 1.10 christos return NULL; 6044 1.1 christos return &uuid_cmd->command.uuid; 6045 1.1 christos } 6046 1.1 christos 6047 1.1 christos /* Return true if ABFD is a dSYM file and its UUID matches UUID_CMD. */ 6048 1.1 christos 6049 1.8 christos static bool 6050 1.1 christos bfd_mach_o_dsym_for_uuid_p (bfd *abfd, const bfd_mach_o_uuid_command *uuid_cmd) 6051 1.1 christos { 6052 1.1 christos bfd_mach_o_uuid_command *dsym_uuid_cmd; 6053 1.1 christos 6054 1.1 christos BFD_ASSERT (abfd); 6055 1.1 christos BFD_ASSERT (uuid_cmd); 6056 1.1 christos 6057 1.1 christos if (!bfd_check_format (abfd, bfd_object)) 6058 1.8 christos return false; 6059 1.1 christos 6060 1.1 christos if (bfd_get_flavour (abfd) != bfd_target_mach_o_flavour 6061 1.1 christos || bfd_mach_o_get_data (abfd) == NULL 6062 1.1 christos || bfd_mach_o_get_data (abfd)->header.filetype != BFD_MACH_O_MH_DSYM) 6063 1.8 christos return false; 6064 1.1 christos 6065 1.1 christos dsym_uuid_cmd = bfd_mach_o_lookup_uuid_command (abfd); 6066 1.1 christos if (dsym_uuid_cmd == NULL) 6067 1.8 christos return false; 6068 1.1 christos 6069 1.1 christos if (memcmp (uuid_cmd->uuid, dsym_uuid_cmd->uuid, 6070 1.6 christos sizeof (uuid_cmd->uuid)) != 0) 6071 1.8 christos return false; 6072 1.1 christos 6073 1.8 christos return true; 6074 1.1 christos } 6075 1.1 christos 6076 1.1 christos /* Find a BFD in DSYM_FILENAME which matches ARCH and UUID_CMD. 6077 1.1 christos The caller is responsible for closing the returned BFD object and 6078 1.1 christos its my_archive if the returned BFD is in a fat dSYM. */ 6079 1.1 christos 6080 1.1 christos static bfd * 6081 1.1 christos bfd_mach_o_find_dsym (const char *dsym_filename, 6082 1.6 christos const bfd_mach_o_uuid_command *uuid_cmd, 6083 1.6 christos const bfd_arch_info_type *arch) 6084 1.1 christos { 6085 1.1 christos bfd *base_dsym_bfd, *dsym_bfd; 6086 1.1 christos 6087 1.1 christos BFD_ASSERT (uuid_cmd); 6088 1.1 christos 6089 1.1 christos base_dsym_bfd = bfd_openr (dsym_filename, NULL); 6090 1.1 christos if (base_dsym_bfd == NULL) 6091 1.1 christos return NULL; 6092 1.1 christos 6093 1.1 christos dsym_bfd = bfd_mach_o_fat_extract (base_dsym_bfd, bfd_object, arch); 6094 1.1 christos if (bfd_mach_o_dsym_for_uuid_p (dsym_bfd, uuid_cmd)) 6095 1.1 christos return dsym_bfd; 6096 1.1 christos 6097 1.1 christos bfd_close (dsym_bfd); 6098 1.1 christos if (base_dsym_bfd != dsym_bfd) 6099 1.1 christos bfd_close (base_dsym_bfd); 6100 1.1 christos 6101 1.1 christos return NULL; 6102 1.1 christos } 6103 1.1 christos 6104 1.1 christos /* Return a BFD created from a dSYM file for ABFD. 6105 1.1 christos The caller is responsible for closing the returned BFD object, its 6106 1.1 christos filename, and its my_archive if the returned BFD is in a fat dSYM. */ 6107 1.1 christos 6108 1.1 christos static bfd * 6109 1.1 christos bfd_mach_o_follow_dsym (bfd *abfd) 6110 1.1 christos { 6111 1.1 christos char *dsym_filename; 6112 1.1 christos bfd_mach_o_uuid_command *uuid_cmd; 6113 1.1 christos bfd *dsym_bfd, *base_bfd = abfd; 6114 1.1 christos const char *base_basename; 6115 1.1 christos 6116 1.1 christos if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_mach_o_flavour) 6117 1.1 christos return NULL; 6118 1.1 christos 6119 1.5 christos if (abfd->my_archive && !bfd_is_thin_archive (abfd->my_archive)) 6120 1.1 christos base_bfd = abfd->my_archive; 6121 1.1 christos /* BFD may have been opened from a stream. */ 6122 1.8 christos if (bfd_get_filename (base_bfd) == NULL) 6123 1.1 christos { 6124 1.1 christos bfd_set_error (bfd_error_invalid_operation); 6125 1.1 christos return NULL; 6126 1.1 christos } 6127 1.8 christos base_basename = lbasename (bfd_get_filename (base_bfd)); 6128 1.1 christos 6129 1.1 christos uuid_cmd = bfd_mach_o_lookup_uuid_command (abfd); 6130 1.1 christos if (uuid_cmd == NULL) 6131 1.1 christos return NULL; 6132 1.1 christos 6133 1.1 christos /* TODO: We assume the DWARF file has the same as the binary's. 6134 1.1 christos It seems apple's GDB checks all files in the dSYM bundle directory. 6135 1.1 christos http://opensource.apple.com/source/gdb/gdb-1708/src/gdb/macosx/macosx-tdep.c 6136 1.1 christos */ 6137 1.8 christos dsym_filename = (char *)bfd_malloc (strlen (bfd_get_filename (base_bfd)) 6138 1.6 christos + strlen (dsym_subdir) + 1 6139 1.6 christos + strlen (base_basename) + 1); 6140 1.7 christos if (dsym_filename == NULL) 6141 1.7 christos return NULL; 6142 1.7 christos 6143 1.1 christos sprintf (dsym_filename, "%s%s/%s", 6144 1.8 christos bfd_get_filename (base_bfd), dsym_subdir, base_basename); 6145 1.1 christos 6146 1.1 christos dsym_bfd = bfd_mach_o_find_dsym (dsym_filename, uuid_cmd, 6147 1.6 christos bfd_get_arch_info (abfd)); 6148 1.1 christos if (dsym_bfd == NULL) 6149 1.1 christos free (dsym_filename); 6150 1.1 christos 6151 1.1 christos return dsym_bfd; 6152 1.1 christos } 6153 1.1 christos 6154 1.8 christos bool 6155 1.1 christos bfd_mach_o_find_nearest_line (bfd *abfd, 6156 1.3 christos asymbol **symbols, 6157 1.1 christos asection *section, 6158 1.1 christos bfd_vma offset, 6159 1.1 christos const char **filename_ptr, 6160 1.1 christos const char **functionname_ptr, 6161 1.3 christos unsigned int *line_ptr, 6162 1.3 christos unsigned int *discriminator_ptr) 6163 1.1 christos { 6164 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 6165 1.1 christos if (mdata == NULL) 6166 1.8 christos return false; 6167 1.1 christos switch (mdata->header.filetype) 6168 1.1 christos { 6169 1.1 christos case BFD_MACH_O_MH_OBJECT: 6170 1.1 christos break; 6171 1.1 christos case BFD_MACH_O_MH_EXECUTE: 6172 1.1 christos case BFD_MACH_O_MH_DYLIB: 6173 1.1 christos case BFD_MACH_O_MH_BUNDLE: 6174 1.1 christos case BFD_MACH_O_MH_KEXT_BUNDLE: 6175 1.1 christos if (mdata->dwarf2_find_line_info == NULL) 6176 1.6 christos { 6177 1.6 christos mdata->dsym_bfd = bfd_mach_o_follow_dsym (abfd); 6178 1.6 christos /* When we couldn't find dSYM for this binary, we look for 6179 1.6 christos the debug information in the binary itself. In this way, 6180 1.6 christos we won't try finding separated dSYM again because 6181 1.6 christos mdata->dwarf2_find_line_info will be filled. */ 6182 1.6 christos if (! mdata->dsym_bfd) 6183 1.6 christos break; 6184 1.6 christos if (! _bfd_dwarf2_slurp_debug_info (abfd, mdata->dsym_bfd, 6185 1.6 christos dwarf_debug_sections, symbols, 6186 1.6 christos &mdata->dwarf2_find_line_info, 6187 1.8 christos false)) 6188 1.8 christos return false; 6189 1.6 christos } 6190 1.1 christos break; 6191 1.1 christos default: 6192 1.8 christos return false; 6193 1.1 christos } 6194 1.3 christos return _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, 6195 1.3 christos filename_ptr, functionname_ptr, 6196 1.3 christos line_ptr, discriminator_ptr, 6197 1.7 christos dwarf_debug_sections, 6198 1.3 christos &mdata->dwarf2_find_line_info); 6199 1.1 christos } 6200 1.1 christos 6201 1.8 christos bool 6202 1.1 christos bfd_mach_o_close_and_cleanup (bfd *abfd) 6203 1.1 christos { 6204 1.1 christos bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd); 6205 1.1 christos if (bfd_get_format (abfd) == bfd_object && mdata != NULL) 6206 1.1 christos { 6207 1.1 christos if (mdata->dsym_bfd != NULL) 6208 1.6 christos { 6209 1.6 christos bfd *fat_bfd = mdata->dsym_bfd->my_archive; 6210 1.5 christos #if 0 6211 1.5 christos /* FIXME: PR 19435: This calculation to find the memory allocated by 6212 1.5 christos bfd_mach_o_follow_dsym for the filename does not always end up 6213 1.5 christos selecting the correct pointer. Unfortunately this problem is 6214 1.5 christos very hard to reproduce on a non Mach-O native system, so until it 6215 1.5 christos can be traced and fixed on such a system, this code will remain 6216 1.5 christos commented out. This does mean that there will be a memory leak, 6217 1.5 christos but it is small, and happens when we are closing down, so it 6218 1.5 christos should not matter too much. */ 6219 1.6 christos char *dsym_filename = (char *)(fat_bfd 6220 1.8 christos ? bfd_get_filename (fat_bfd) 6221 1.8 christos : bfd_get_filename (mdata->dsym_bfd)); 6222 1.5 christos #endif 6223 1.6 christos bfd_close (mdata->dsym_bfd); 6224 1.6 christos mdata->dsym_bfd = NULL; 6225 1.6 christos if (fat_bfd) 6226 1.6 christos bfd_close (fat_bfd); 6227 1.5 christos #if 0 6228 1.6 christos free (dsym_filename); 6229 1.5 christos #endif 6230 1.6 christos } 6231 1.1 christos } 6232 1.1 christos 6233 1.1 christos return _bfd_generic_close_and_cleanup (abfd); 6234 1.1 christos } 6235 1.1 christos 6236 1.8 christos bool 6237 1.9 christos bfd_mach_o_bfd_free_cached_info (bfd *abfd) 6238 1.1 christos { 6239 1.9 christos bfd_mach_o_data_struct *mdata; 6240 1.9 christos 6241 1.9 christos if ((bfd_get_format (abfd) == bfd_object 6242 1.9 christos || bfd_get_format (abfd) == bfd_core) 6243 1.9 christos && (mdata = bfd_mach_o_get_data (abfd)) != NULL) 6244 1.1 christos { 6245 1.9 christos _bfd_dwarf2_cleanup_debug_info (abfd, &mdata->dwarf2_find_line_info); 6246 1.9 christos free (mdata->dyn_reloc_cache); 6247 1.9 christos mdata->dyn_reloc_cache = NULL; 6248 1.9 christos 6249 1.9 christos for (asection *asect = abfd->sections; asect; asect = asect->next) 6250 1.9 christos { 6251 1.9 christos free (asect->relocation); 6252 1.9 christos asect->relocation = NULL; 6253 1.9 christos } 6254 1.1 christos } 6255 1.1 christos 6256 1.9 christos /* Do not call _bfd_generic_bfd_free_cached_info here. 6257 1.9 christos bfd_mach_o_close_and_cleanup uses tdata. */ 6258 1.8 christos return true; 6259 1.1 christos } 6260 1.1 christos 6261 1.3 christos #define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup 6262 1.1 christos #define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup 6263 1.1 christos 6264 1.5 christos #define bfd_mach_o_canonicalize_one_reloc NULL 6265 1.1 christos #define bfd_mach_o_swap_reloc_out NULL 6266 1.1 christos #define bfd_mach_o_print_thread NULL 6267 1.1 christos #define bfd_mach_o_tgt_seg_table NULL 6268 1.1 christos #define bfd_mach_o_section_type_valid_for_tgt NULL 6269 1.1 christos 6270 1.6 christos #define TARGET_NAME mach_o_be_vec 6271 1.6 christos #define TARGET_STRING "mach-o-be" 6272 1.1 christos #define TARGET_ARCHITECTURE bfd_arch_unknown 6273 1.3 christos #define TARGET_PAGESIZE 1 6274 1.6 christos #define TARGET_BIG_ENDIAN 1 6275 1.6 christos #define TARGET_ARCHIVE 0 6276 1.1 christos #define TARGET_PRIORITY 1 6277 1.1 christos #include "mach-o-target.c" 6278 1.1 christos 6279 1.1 christos #undef TARGET_NAME 6280 1.1 christos #undef TARGET_STRING 6281 1.1 christos #undef TARGET_ARCHITECTURE 6282 1.3 christos #undef TARGET_PAGESIZE 6283 1.1 christos #undef TARGET_BIG_ENDIAN 6284 1.1 christos #undef TARGET_ARCHIVE 6285 1.1 christos #undef TARGET_PRIORITY 6286 1.1 christos 6287 1.6 christos #define TARGET_NAME mach_o_le_vec 6288 1.6 christos #define TARGET_STRING "mach-o-le" 6289 1.1 christos #define TARGET_ARCHITECTURE bfd_arch_unknown 6290 1.3 christos #define TARGET_PAGESIZE 1 6291 1.6 christos #define TARGET_BIG_ENDIAN 0 6292 1.6 christos #define TARGET_ARCHIVE 0 6293 1.1 christos #define TARGET_PRIORITY 1 6294 1.1 christos 6295 1.1 christos #include "mach-o-target.c" 6296 1.1 christos 6297 1.1 christos #undef TARGET_NAME 6298 1.1 christos #undef TARGET_STRING 6299 1.1 christos #undef TARGET_ARCHITECTURE 6300 1.3 christos #undef TARGET_PAGESIZE 6301 1.1 christos #undef TARGET_BIG_ENDIAN 6302 1.1 christos #undef TARGET_ARCHIVE 6303 1.1 christos #undef TARGET_PRIORITY 6304 1.1 christos 6305 1.1 christos /* Not yet handled: creating an archive. */ 6306 1.6 christos #define bfd_mach_o_mkarchive _bfd_noarchive_mkarchive 6307 1.1 christos 6308 1.7 christos #define bfd_mach_o_close_and_cleanup bfd_mach_o_fat_close_and_cleanup 6309 1.5 christos 6310 1.1 christos /* Not used. */ 6311 1.6 christos #define bfd_mach_o_generic_stat_arch_elt bfd_mach_o_fat_stat_arch_elt 6312 1.5 christos #define bfd_mach_o_openr_next_archived_file bfd_mach_o_fat_openr_next_archived_file 6313 1.5 christos #define bfd_mach_o_archive_p bfd_mach_o_fat_archive_p 6314 1.1 christos 6315 1.6 christos #define TARGET_NAME mach_o_fat_vec 6316 1.6 christos #define TARGET_STRING "mach-o-fat" 6317 1.1 christos #define TARGET_ARCHITECTURE bfd_arch_unknown 6318 1.3 christos #define TARGET_PAGESIZE 1 6319 1.6 christos #define TARGET_BIG_ENDIAN 1 6320 1.6 christos #define TARGET_ARCHIVE 1 6321 1.1 christos #define TARGET_PRIORITY 0 6322 1.1 christos 6323 1.1 christos #include "mach-o-target.c" 6324 1.1 christos 6325 1.1 christos #undef TARGET_NAME 6326 1.1 christos #undef TARGET_STRING 6327 1.1 christos #undef TARGET_ARCHITECTURE 6328 1.3 christos #undef TARGET_PAGESIZE 6329 1.1 christos #undef TARGET_BIG_ENDIAN 6330 1.1 christos #undef TARGET_ARCHIVE 6331 1.1 christos #undef TARGET_PRIORITY 6332