Coverage Report

Created: 2025-07-08 11:15

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