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