Coverage Report

Created: 2026-11-21 09:00

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