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