Coverage Report

Created: 2025-06-24 06:45

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