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