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