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