Coverage Report

Created: 2026-10-02 09:53

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