OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gdb-6.8/] [bfd/] [mach-o.c] - Blame information for rev 855

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 24 jeremybenn
/* Mach-O support for BFD.
2 225 jeremybenn
   Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 24 jeremybenn
   Free Software Foundation, Inc.
4
 
5
   This file is part of BFD, the Binary File Descriptor library.
6
 
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
 
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
 
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
 
22
#include "sysdep.h"
23
#include "mach-o.h"
24
#include "bfd.h"
25
#include "libbfd.h"
26
#include "libiberty.h"
27 225 jeremybenn
#include "aout/stab_gnu.h"
28 24 jeremybenn
#include <ctype.h>
29
 
30 225 jeremybenn
#define bfd_mach_o_object_p bfd_mach_o_gen_object_p
31
#define bfd_mach_o_core_p bfd_mach_o_gen_core_p
32
#define bfd_mach_o_mkobject bfd_false
33 24 jeremybenn
 
34 225 jeremybenn
#define FILE_ALIGN(off, algn) \
35
  (((off) + ((file_ptr) 1 << (algn)) - 1) & ((file_ptr) -1 << (algn)))
36 24 jeremybenn
 
37 225 jeremybenn
static int bfd_mach_o_scan_read_symtab_symbols (bfd *);
38 24 jeremybenn
 
39 225 jeremybenn
unsigned int
40
bfd_mach_o_version (bfd *abfd)
41
{
42
  bfd_mach_o_data_struct *mdata = NULL;
43 24 jeremybenn
 
44 225 jeremybenn
  BFD_ASSERT (bfd_mach_o_valid (abfd));
45
  mdata = bfd_mach_o_get_data (abfd);
46 24 jeremybenn
 
47 225 jeremybenn
  return mdata->header.version;
48
}
49 24 jeremybenn
 
50
bfd_boolean
51
bfd_mach_o_valid (bfd *abfd)
52
{
53
  if (abfd == NULL || abfd->xvec == NULL)
54 225 jeremybenn
    return FALSE;
55 24 jeremybenn
 
56 225 jeremybenn
  if (abfd->xvec->flavour != bfd_target_mach_o_flavour)
57
    return FALSE;
58 24 jeremybenn
 
59 225 jeremybenn
  if (bfd_mach_o_get_data (abfd) == NULL)
60
    return FALSE;
61
  return TRUE;
62 24 jeremybenn
}
63
 
64 225 jeremybenn
static INLINE bfd_boolean
65
mach_o_wide_p (bfd_mach_o_header *header)
66
{
67
  switch (header->version)
68
    {
69
    case 1:
70
      return FALSE;
71
    case 2:
72
      return TRUE;
73
    default:
74
      BFD_FAIL ();
75
      return FALSE;
76
    }
77
}
78
 
79
static INLINE bfd_boolean
80
bfd_mach_o_wide_p (bfd *abfd)
81
{
82
  return mach_o_wide_p (&bfd_mach_o_get_data (abfd)->header);
83
}
84
 
85
/* Tables to translate well known Mach-O segment/section names to bfd
86
   names.  Use of canonical names (such as .text or .debug_frame) is required
87
   by gdb.  */
88
 
89
struct mach_o_section_name_xlat
90
{
91
  const char *bfd_name;
92
  const char *mach_o_name;
93
};
94
 
95
static const struct mach_o_section_name_xlat dwarf_section_names_xlat[] =
96
  {
97
    { ".debug_frame", "__debug_frame" },
98
    { ".debug_info", "__debug_info" },
99
    { ".debug_abbrev", "__debug_abbrev" },
100
    { ".debug_aranges", "__debug_aranges" },
101
    { ".debug_macinfo", "__debug_macinfo" },
102
    { ".debug_line", "__debug_line" },
103
    { ".debug_loc", "__debug_loc" },
104
    { ".debug_pubnames", "__debug_pubnames" },
105
    { ".debug_pubtypes", "__debug_pubtypes" },
106
    { ".debug_str", "__debug_str" },
107
    { ".debug_ranges", "__debug_ranges" },
108
    { NULL, NULL}
109
  };
110
 
111
static const struct mach_o_section_name_xlat text_section_names_xlat[] =
112
  {
113
    { ".text", "__text" },
114
    { ".const", "__const" },
115
    { ".cstring", "__cstring" },
116
    { ".eh_frame", "__eh_frame" },
117
    { NULL, NULL}
118
  };
119
 
120
static const struct mach_o_section_name_xlat data_section_names_xlat[] =
121
  {
122
    { ".data", "__data" },
123
    { ".bss", "__bss" },
124
    { NULL, NULL}
125
  };
126
 
127
struct mach_o_segment_name_xlat
128
{
129
  const char *segname;
130
  const struct mach_o_section_name_xlat *sections;
131
};
132
 
133
static const struct mach_o_segment_name_xlat segsec_names_xlat[] =
134
  {
135
    { "__DWARF", dwarf_section_names_xlat },
136
    { "__TEXT", text_section_names_xlat },
137
    { "__DATA", data_section_names_xlat },
138
    { NULL, NULL }
139
  };
140
 
141
 
142
/* Mach-O to bfd names.  */
143
 
144
static char *
145
bfd_mach_o_convert_section_name_to_bfd (bfd *abfd, bfd_mach_o_section *section)
146
{
147
  const struct mach_o_segment_name_xlat *seg;
148
  char *res;
149
  unsigned int len;
150
  const char *pfx = "";
151
 
152
  for (seg = segsec_names_xlat; seg->segname; seg++)
153
    {
154
      if (strcmp (seg->segname, section->segname) == 0)
155
        {
156
          const struct mach_o_section_name_xlat *sec;
157
 
158
          for (sec = seg->sections; sec->mach_o_name; sec++)
159
            {
160
              if (strcmp (sec->mach_o_name, section->sectname) == 0)
161
                {
162
                  len = strlen (sec->bfd_name);
163
                  res = bfd_alloc (abfd, len + 1);
164
 
165
                  if (res == NULL)
166
                    return NULL;
167
                  strcpy (res, sec->bfd_name);
168
                  return res;
169
                }
170
            }
171
        }
172
    }
173
 
174
  len = strlen (section->segname) + 1
175
    + strlen (section->sectname) + 1;
176
 
177
  /* Put "LC_SEGMENT." prefix if the segment name is weird (ie doesn't start
178
     with an underscore.  */
179
  if (section->segname[0] != '_')
180
    {
181
      static const char seg_pfx[] = "LC_SEGMENT.";
182
 
183
      pfx = seg_pfx;
184
      len += sizeof (seg_pfx) - 1;
185
    }
186
 
187
  res = bfd_alloc (abfd, len);
188
  if (res == NULL)
189
    return NULL;
190
  snprintf (res, len, "%s%s.%s", pfx, section->segname, section->sectname);
191
  return res;
192
}
193
 
194
/* Convert a bfd section name to a Mach-O segment + section name.  */
195
 
196
static void
197
bfd_mach_o_convert_section_name_to_mach_o (bfd *abfd ATTRIBUTE_UNUSED,
198
                                           asection *sect,
199
                                           bfd_mach_o_section *section)
200
{
201
  const struct mach_o_segment_name_xlat *seg;
202
  const char *name = bfd_get_section_name (abfd, sect);
203
  const char *dot;
204
  unsigned int len;
205
  unsigned int seglen;
206
  unsigned int seclen;
207
 
208
  /* List of well known names.  They all start with a dot.  */
209
  if (name[0] == '.')
210
    for (seg = segsec_names_xlat; seg->segname; seg++)
211
      {
212
        const struct mach_o_section_name_xlat *sec;
213
 
214
        for (sec = seg->sections; sec->mach_o_name; sec++)
215
          {
216
            if (strcmp (sec->bfd_name, name) == 0)
217
              {
218
                strcpy (section->segname, seg->segname);
219
                strcpy (section->sectname, sec->mach_o_name);
220
                return;
221
              }
222
          }
223
      }
224
 
225
  /* Strip LC_SEGMENT. prefix.  */
226
  if (strncmp (name, "LC_SEGMENT.", 11) == 0)
227
    name += 11;
228
 
229
  /* Find a dot.  */
230
  dot = strchr (name, '.');
231
  len = strlen (name);
232
 
233
  /* Try to split name into segment and section names.  */
234
  if (dot && dot != name)
235
    {
236
      seglen = dot - name;
237
      seclen = len - (dot + 1 - name);
238
 
239
      if (seglen < 16 && seclen < 16)
240
        {
241
          memcpy (section->segname, name, seglen);
242
          section->segname[seglen] = 0;
243
          memcpy (section->sectname, dot + 1, seclen);
244
          section->sectname[seclen] = 0;
245
          return;
246
        }
247
    }
248
 
249
  if (len > 16)
250
    len = 16;
251
  memcpy (section->segname, name, len);
252
  section->segname[len] = 0;
253
  memcpy (section->sectname, name, len);
254
  section->sectname[len] = 0;
255
}
256
 
257 24 jeremybenn
/* Copy any private info we understand from the input symbol
258
   to the output symbol.  */
259
 
260 225 jeremybenn
bfd_boolean
261 24 jeremybenn
bfd_mach_o_bfd_copy_private_symbol_data (bfd *ibfd ATTRIBUTE_UNUSED,
262
                                         asymbol *isymbol ATTRIBUTE_UNUSED,
263
                                         bfd *obfd ATTRIBUTE_UNUSED,
264
                                         asymbol *osymbol ATTRIBUTE_UNUSED)
265
{
266
  return TRUE;
267
}
268
 
269
/* Copy any private info we understand from the input section
270
   to the output section.  */
271
 
272 225 jeremybenn
bfd_boolean
273 24 jeremybenn
bfd_mach_o_bfd_copy_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED,
274
                                          asection *isection ATTRIBUTE_UNUSED,
275
                                          bfd *obfd ATTRIBUTE_UNUSED,
276
                                          asection *osection ATTRIBUTE_UNUSED)
277
{
278
  return TRUE;
279
}
280
 
281
/* Copy any private info we understand from the input bfd
282
   to the output bfd.  */
283
 
284 225 jeremybenn
bfd_boolean
285 24 jeremybenn
bfd_mach_o_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
286
{
287 225 jeremybenn
  if (bfd_get_flavour (ibfd) != bfd_target_mach_o_flavour
288
      || bfd_get_flavour (obfd) != bfd_target_mach_o_flavour)
289
    return TRUE;
290
 
291 24 jeremybenn
  BFD_ASSERT (bfd_mach_o_valid (ibfd));
292
  BFD_ASSERT (bfd_mach_o_valid (obfd));
293
 
294 225 jeremybenn
  /* FIXME: copy commands.  */
295
 
296 24 jeremybenn
  return TRUE;
297
}
298
 
299 225 jeremybenn
/* Count the total number of symbols.  */
300
 
301 24 jeremybenn
static long
302
bfd_mach_o_count_symbols (bfd *abfd)
303
{
304 225 jeremybenn
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
305 24 jeremybenn
 
306 225 jeremybenn
  if (mdata->symtab == NULL)
307
    return 0;
308
  return mdata->symtab->nsyms;
309 24 jeremybenn
}
310
 
311 225 jeremybenn
long
312 24 jeremybenn
bfd_mach_o_get_symtab_upper_bound (bfd *abfd)
313
{
314
  long nsyms = bfd_mach_o_count_symbols (abfd);
315
 
316
  return ((nsyms + 1) * sizeof (asymbol *));
317
}
318
 
319 225 jeremybenn
long
320 24 jeremybenn
bfd_mach_o_canonicalize_symtab (bfd *abfd, asymbol **alocation)
321
{
322 225 jeremybenn
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
323 24 jeremybenn
  long nsyms = bfd_mach_o_count_symbols (abfd);
324 225 jeremybenn
  bfd_mach_o_symtab_command *sym = mdata->symtab;
325
  unsigned long j;
326 24 jeremybenn
 
327
  if (nsyms < 0)
328
    return nsyms;
329
 
330 225 jeremybenn
  if (bfd_mach_o_scan_read_symtab_symbols (abfd) != 0)
331 24 jeremybenn
    {
332 225 jeremybenn
      fprintf (stderr,
333
               "bfd_mach_o_canonicalize_symtab: unable to load symbols\n");
334
      return 0;
335
    }
336 24 jeremybenn
 
337 225 jeremybenn
  BFD_ASSERT (sym->symbols != NULL);
338 24 jeremybenn
 
339 225 jeremybenn
  for (j = 0; j < sym->nsyms; j++)
340
    alocation[j] = &sym->symbols[j].symbol;
341 24 jeremybenn
 
342 225 jeremybenn
  alocation[j] = NULL;
343 24 jeremybenn
 
344
  return nsyms;
345
}
346
 
347 225 jeremybenn
void
348 24 jeremybenn
bfd_mach_o_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
349
                            asymbol *symbol,
350
                            symbol_info *ret)
351
{
352
  bfd_symbol_info (symbol, ret);
353
}
354
 
355 225 jeremybenn
void
356 24 jeremybenn
bfd_mach_o_print_symbol (bfd *abfd,
357
                         PTR afile,
358
                         asymbol *symbol,
359
                         bfd_print_symbol_type how)
360
{
361
  FILE *file = (FILE *) afile;
362 225 jeremybenn
  const char *name;
363
  bfd_mach_o_asymbol *asym = (bfd_mach_o_asymbol *)symbol;
364 24 jeremybenn
 
365
  switch (how)
366
    {
367
    case bfd_print_symbol_name:
368
      fprintf (file, "%s", symbol->name);
369
      break;
370
    default:
371
      bfd_print_symbol_vandf (abfd, (PTR) file, symbol);
372 225 jeremybenn
      if (asym->n_type & BFD_MACH_O_N_STAB)
373
        name = bfd_get_stab_name (asym->n_type);
374
      else
375
        switch (asym->n_type & BFD_MACH_O_N_TYPE)
376
          {
377
          case BFD_MACH_O_N_UNDF:
378
            name = "UND";
379
            break;
380
          case BFD_MACH_O_N_ABS:
381
            name = "ABS";
382
            break;
383
          case BFD_MACH_O_N_INDR:
384
            name = "INDR";
385
            break;
386
          case BFD_MACH_O_N_PBUD:
387
            name = "PBUD";
388
            break;
389
          case BFD_MACH_O_N_SECT:
390
            name = "SECT";
391
            break;
392
          default:
393
            name = "???";
394
            break;
395
          }
396
      if (name == NULL)
397
        name = "";
398
      fprintf (file, " %02x %-6s %02x %04x",
399
               asym->n_type, name, asym->n_sect, asym->n_desc);
400
      if ((asym->n_type & BFD_MACH_O_N_STAB) == 0
401
          && (asym->n_type & BFD_MACH_O_N_TYPE) == BFD_MACH_O_N_SECT)
402
        fprintf (file, " %-5s", symbol->section->name);
403
      fprintf (file, " %s", symbol->name);
404 24 jeremybenn
    }
405
}
406
 
407
static void
408
bfd_mach_o_convert_architecture (bfd_mach_o_cpu_type mtype,
409
                                 bfd_mach_o_cpu_subtype msubtype ATTRIBUTE_UNUSED,
410
                                 enum bfd_architecture *type,
411
                                 unsigned long *subtype)
412
{
413
  *subtype = bfd_arch_unknown;
414
 
415
  switch (mtype)
416
    {
417
    case BFD_MACH_O_CPU_TYPE_VAX: *type = bfd_arch_vax; break;
418
    case BFD_MACH_O_CPU_TYPE_MC680x0: *type = bfd_arch_m68k; break;
419 225 jeremybenn
    case BFD_MACH_O_CPU_TYPE_I386:
420
      *type = bfd_arch_i386;
421
      *subtype = bfd_mach_i386_i386;
422
      break;
423
    case BFD_MACH_O_CPU_TYPE_X86_64:
424
      *type = bfd_arch_i386;
425
      *subtype = bfd_mach_x86_64;
426
      break;
427 24 jeremybenn
    case BFD_MACH_O_CPU_TYPE_MIPS: *type = bfd_arch_mips; break;
428
    case BFD_MACH_O_CPU_TYPE_MC98000: *type = bfd_arch_m98k; break;
429
    case BFD_MACH_O_CPU_TYPE_HPPA: *type = bfd_arch_hppa; break;
430
    case BFD_MACH_O_CPU_TYPE_ARM: *type = bfd_arch_arm; break;
431
    case BFD_MACH_O_CPU_TYPE_MC88000: *type = bfd_arch_m88k; break;
432 225 jeremybenn
    case BFD_MACH_O_CPU_TYPE_SPARC:
433
      *type = bfd_arch_sparc;
434
      *subtype = bfd_mach_sparc;
435
      break;
436 24 jeremybenn
    case BFD_MACH_O_CPU_TYPE_I860: *type = bfd_arch_i860; break;
437
    case BFD_MACH_O_CPU_TYPE_ALPHA: *type = bfd_arch_alpha; break;
438 225 jeremybenn
    case BFD_MACH_O_CPU_TYPE_POWERPC:
439
      *type = bfd_arch_powerpc;
440
      *subtype = bfd_mach_ppc;
441
      break;
442
    case BFD_MACH_O_CPU_TYPE_POWERPC_64:
443
      *type = bfd_arch_powerpc;
444
      *subtype = bfd_mach_ppc64;
445
      break;
446 24 jeremybenn
    default:
447 225 jeremybenn
      *type = bfd_arch_unknown;
448
      break;
449 24 jeremybenn
    }
450
}
451
 
452 225 jeremybenn
static bfd_boolean
453 24 jeremybenn
bfd_mach_o_write_header (bfd *abfd, bfd_mach_o_header *header)
454
{
455 225 jeremybenn
  unsigned char buf[32];
456
  unsigned int size;
457 24 jeremybenn
 
458 225 jeremybenn
  size = mach_o_wide_p (header) ?
459
    BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
460
 
461 24 jeremybenn
  bfd_h_put_32 (abfd, header->magic, buf + 0);
462
  bfd_h_put_32 (abfd, header->cputype, buf + 4);
463
  bfd_h_put_32 (abfd, header->cpusubtype, buf + 8);
464
  bfd_h_put_32 (abfd, header->filetype, buf + 12);
465
  bfd_h_put_32 (abfd, header->ncmds, buf + 16);
466
  bfd_h_put_32 (abfd, header->sizeofcmds, buf + 20);
467
  bfd_h_put_32 (abfd, header->flags, buf + 24);
468
 
469 225 jeremybenn
  if (mach_o_wide_p (header))
470
    bfd_h_put_32 (abfd, header->reserved, buf + 28);
471 24 jeremybenn
 
472 225 jeremybenn
  if (bfd_seek (abfd, 0, SEEK_SET) != 0
473
      || bfd_bwrite ((PTR) buf, size, abfd) != size)
474
    return FALSE;
475
 
476
  return TRUE;
477 24 jeremybenn
}
478
 
479
static int
480 225 jeremybenn
bfd_mach_o_scan_write_thread (bfd *abfd, bfd_mach_o_load_command *command)
481 24 jeremybenn
{
482
  bfd_mach_o_thread_command *cmd = &command->command.thread;
483
  unsigned int i;
484
  unsigned char buf[8];
485 225 jeremybenn
  unsigned int offset;
486 24 jeremybenn
  unsigned int nflavours;
487
 
488
  BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
489
              || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
490
 
491
  offset = 8;
492
  nflavours = 0;
493
  for (i = 0; i < cmd->nflavours; i++)
494
    {
495
      BFD_ASSERT ((cmd->flavours[i].size % 4) == 0);
496
      BFD_ASSERT (cmd->flavours[i].offset == (command->offset + offset + 8));
497
 
498
      bfd_h_put_32 (abfd, cmd->flavours[i].flavour, buf);
499
      bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), buf + 4);
500
 
501 225 jeremybenn
      if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
502
          || bfd_bwrite ((PTR) buf, 8, abfd) != 8)
503 24 jeremybenn
        return -1;
504
 
505
      offset += cmd->flavours[i].size + 8;
506
    }
507
 
508
  return 0;
509
}
510
 
511 225 jeremybenn
long
512
bfd_mach_o_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
513
                                  asection *asect)
514
{
515
  return (asect->reloc_count + 1) * sizeof (arelent *);
516
}
517
 
518 24 jeremybenn
static int
519 225 jeremybenn
bfd_mach_o_canonicalize_one_reloc (bfd *abfd, char *buf,
520
                                   arelent *res, asymbol **syms)
521 24 jeremybenn
{
522 225 jeremybenn
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
523
  bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
524
  bfd_mach_o_reloc_info reloc;
525
  bfd_vma addr;
526
  bfd_vma symnum;
527
  asymbol **sym;
528 24 jeremybenn
 
529 225 jeremybenn
  addr = bfd_get_32 (abfd, buf + 0);
530
  symnum = bfd_get_32 (abfd, buf + 4);
531
 
532
  if (addr & BFD_MACH_O_SR_SCATTERED)
533
    {
534
      unsigned int j;
535
 
536
      /* Scattered relocation.
537
         Extract section and offset from r_value.  */
538
      res->sym_ptr_ptr = NULL;
539
      res->addend = 0;
540
      for (j = 0; j < mdata->nsects; j++)
541
        {
542
          bfd_mach_o_section *sect = mdata->sections[j];
543
          if (symnum >= sect->addr && symnum < sect->addr + sect->size)
544
            {
545
              res->sym_ptr_ptr = sect->bfdsection->symbol_ptr_ptr;
546
              res->addend = symnum - sect->addr;
547
              break;
548
            }
549
        }
550
      res->address = BFD_MACH_O_GET_SR_ADDRESS (addr);
551
      reloc.r_type = BFD_MACH_O_GET_SR_TYPE (addr);
552
      reloc.r_length = BFD_MACH_O_GET_SR_LENGTH (addr);
553
      reloc.r_pcrel = addr & BFD_MACH_O_SR_PCREL;
554
      reloc.r_scattered = 1;
555
    }
556
  else
557
    {
558
      unsigned int num = BFD_MACH_O_GET_R_SYMBOLNUM (symnum);
559
      res->addend = 0;
560
      res->address = addr;
561
      if (symnum & BFD_MACH_O_R_EXTERN)
562
        sym = syms + num;
563
      else
564
        {
565
          BFD_ASSERT (num != 0);
566
          BFD_ASSERT (num <= mdata->nsects);
567
          sym = mdata->sections[num - 1]->bfdsection->symbol_ptr_ptr;
568
        }
569
      res->sym_ptr_ptr = sym;
570
      reloc.r_type = BFD_MACH_O_GET_R_TYPE (symnum);
571
      reloc.r_length = BFD_MACH_O_GET_R_LENGTH (symnum);
572
      reloc.r_pcrel = (symnum & BFD_MACH_O_R_PCREL) ? 1 : 0;
573
      reloc.r_scattered = 0;
574
    }
575
 
576
  if (!(*bed->_bfd_mach_o_swap_reloc_in)(res, &reloc))
577
    return -1;
578
  return 0;
579
}
580
 
581
static int
582
bfd_mach_o_canonicalize_relocs (bfd *abfd, unsigned long filepos,
583
                                unsigned long count,
584
                                arelent *res, asymbol **syms)
585
{
586
  unsigned long i;
587
  char *native_relocs;
588
  bfd_size_type native_size;
589
 
590
  /* Allocate and read relocs.  */
591
  native_size = count * BFD_MACH_O_RELENT_SIZE;
592
  native_relocs = bfd_malloc (native_size);
593
  if (native_relocs == NULL)
594
    return -1;
595
 
596
  if (bfd_seek (abfd, filepos, SEEK_SET) != 0
597
      || bfd_bread (native_relocs, native_size, abfd) != native_size)
598
    goto err;
599
 
600
  for (i = 0; i < count; i++)
601
    {
602
      char *buf = native_relocs + BFD_MACH_O_RELENT_SIZE * i;
603
 
604
      if (bfd_mach_o_canonicalize_one_reloc (abfd, buf, &res[i], syms) < 0)
605
        goto err;
606
    }
607
  free (native_relocs);
608
  return i;
609
 err:
610
  free (native_relocs);
611
  return -1;
612
}
613
 
614
long
615
bfd_mach_o_canonicalize_reloc (bfd *abfd, asection *asect,
616
                               arelent **rels, asymbol **syms)
617
{
618
  bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
619
  unsigned long i;
620
  arelent *res;
621
 
622
  if (asect->reloc_count == 0)
623
    return 0;
624
 
625
  /* No need to go further if we don't know how to read relocs.  */
626
  if (bed->_bfd_mach_o_swap_reloc_in == NULL)
627
    return 0;
628
 
629
  res = bfd_malloc (asect->reloc_count * sizeof (arelent));
630
  if (res == NULL)
631
    return -1;
632
 
633
  if (bfd_mach_o_canonicalize_relocs (abfd, asect->rel_filepos,
634
                                      asect->reloc_count, res, syms) < 0)
635
    {
636
      free (res);
637
      return -1;
638
    }
639
 
640
  for (i = 0; i < asect->reloc_count; i++)
641
    rels[i] = &res[i];
642
  rels[i] = NULL;
643
  asect->relocation = res;
644
 
645
  return i;
646
}
647
 
648
long
649
bfd_mach_o_get_dynamic_reloc_upper_bound (bfd *abfd)
650
{
651
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
652
 
653
  if (mdata->dysymtab == NULL)
654
    return 1;
655
  return (mdata->dysymtab->nextrel + mdata->dysymtab->nlocrel)
656
    * sizeof (arelent *);
657
}
658
 
659
long
660
bfd_mach_o_canonicalize_dynamic_reloc (bfd *abfd, arelent **rels,
661
                                       struct bfd_symbol **syms)
662
{
663
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
664
  bfd_mach_o_dysymtab_command *dysymtab = mdata->dysymtab;
665
  bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
666
  unsigned long i;
667
  arelent *res;
668
 
669
  if (dysymtab == NULL)
670
    return 0;
671
  if (dysymtab->nextrel == 0 && dysymtab->nlocrel == 0)
672
    return 0;
673
 
674
  /* No need to go further if we don't know how to read relocs.  */
675
  if (bed->_bfd_mach_o_swap_reloc_in == NULL)
676
    return 0;
677
 
678
  res = bfd_malloc ((dysymtab->nextrel + dysymtab->nlocrel) * sizeof (arelent));
679
  if (res == NULL)
680
    return -1;
681
 
682
  if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->extreloff,
683
                                      dysymtab->nextrel, res, syms) < 0)
684
    {
685
      free (res);
686
      return -1;
687
    }
688
 
689
  if (bfd_mach_o_canonicalize_relocs (abfd, dysymtab->locreloff,
690
                                      dysymtab->nlocrel,
691
                                      res + dysymtab->nextrel, syms) < 0)
692
    {
693
      free (res);
694
      return -1;
695
    }
696
 
697
  for (i = 0; i < dysymtab->nextrel + dysymtab->nlocrel; i++)
698
    rels[i] = &res[i];
699
  rels[i] = NULL;
700
  return i;
701
}
702
 
703
static bfd_boolean
704
bfd_mach_o_scan_write_relocs (bfd *abfd, bfd_mach_o_section *section)
705
{
706
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
707
  unsigned int i;
708
  arelent **entries;
709
  asection *sec;
710
  bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
711
 
712
  sec = section->bfdsection;
713
  if (sec->reloc_count == 0)
714
    return TRUE;
715
 
716
  if (bed->_bfd_mach_o_swap_reloc_out == NULL)
717
    return TRUE;
718
 
719
  /* Allocate relocation room.  */
720
  mdata->filelen = FILE_ALIGN(mdata->filelen, 2);
721
  section->nreloc = sec->reloc_count;
722
  sec->rel_filepos = mdata->filelen;
723
  section->reloff = sec->rel_filepos;
724
  mdata->filelen += sec->reloc_count * BFD_MACH_O_RELENT_SIZE;
725
 
726
  if (bfd_seek (abfd, section->reloff, SEEK_SET) != 0)
727
    return FALSE;
728
 
729
  /* Convert and write.  */
730
  entries = section->bfdsection->orelocation;
731
  for (i = 0; i < section->nreloc; i++)
732
    {
733
      arelent *rel = entries[i];
734
      char buf[8];
735
      bfd_mach_o_reloc_info info, *pinfo = &info;
736
 
737
      /* Convert relocation to an intermediate representation.  */
738
      if (!(*bed->_bfd_mach_o_swap_reloc_out) (rel, pinfo))
739
        return FALSE;
740
 
741
      /* Lower the relocation info.  */
742
      if (pinfo->r_scattered)
743
        {
744
          unsigned long v;
745
 
746
          v = BFD_MACH_O_SR_SCATTERED
747
            | (pinfo->r_pcrel ? BFD_MACH_O_SR_PCREL : 0)
748
            | BFD_MACH_O_SET_SR_LENGTH(pinfo->r_length)
749
            | BFD_MACH_O_SET_SR_TYPE(pinfo->r_type)
750
            | BFD_MACH_O_SET_SR_ADDRESS(pinfo->r_address);
751
          bfd_put_32 (abfd, v, buf);
752
          bfd_put_32 (abfd, pinfo->r_value, buf + 4);
753
        }
754
      else
755
        {
756
          unsigned long v;
757
 
758
          bfd_put_32 (abfd, pinfo->r_address, buf);
759
          v = BFD_MACH_O_SET_R_SYMBOLNUM (pinfo->r_value)
760
            | (pinfo->r_pcrel ? BFD_MACH_O_R_PCREL : 0)
761
            | BFD_MACH_O_SET_R_LENGTH (pinfo->r_length)
762
            | (pinfo->r_extern ? BFD_MACH_O_R_EXTERN : 0)
763
            | BFD_MACH_O_SET_R_TYPE (pinfo->r_type);
764
          bfd_put_32 (abfd, v, buf + 4);
765
        }
766
 
767
      if (bfd_bwrite ((PTR) buf, BFD_MACH_O_RELENT_SIZE, abfd)
768
          != BFD_MACH_O_RELENT_SIZE)
769
        return FALSE;
770
    }
771
  return TRUE;
772
}
773
 
774
static int
775
bfd_mach_o_scan_write_section_32 (bfd *abfd, bfd_mach_o_section *section)
776
{
777
  unsigned char buf[BFD_MACH_O_SECTION_SIZE];
778
 
779 24 jeremybenn
  memcpy (buf, section->sectname, 16);
780
  memcpy (buf + 16, section->segname, 16);
781
  bfd_h_put_32 (abfd, section->addr, buf + 32);
782
  bfd_h_put_32 (abfd, section->size, buf + 36);
783
  bfd_h_put_32 (abfd, section->offset, buf + 40);
784
  bfd_h_put_32 (abfd, section->align, buf + 44);
785
  bfd_h_put_32 (abfd, section->reloff, buf + 48);
786
  bfd_h_put_32 (abfd, section->nreloc, buf + 52);
787
  bfd_h_put_32 (abfd, section->flags, buf + 56);
788 225 jeremybenn
  bfd_h_put_32 (abfd, section->reserved1, buf + 60);
789
  bfd_h_put_32 (abfd, section->reserved2, buf + 64);
790 24 jeremybenn
 
791 225 jeremybenn
  if (bfd_bwrite ((PTR) buf, BFD_MACH_O_SECTION_SIZE, abfd)
792
      != BFD_MACH_O_SECTION_SIZE)
793 24 jeremybenn
    return -1;
794
 
795
  return 0;
796
}
797
 
798
static int
799 225 jeremybenn
bfd_mach_o_scan_write_section_64 (bfd *abfd, bfd_mach_o_section *section)
800 24 jeremybenn
{
801 225 jeremybenn
  unsigned char buf[BFD_MACH_O_SECTION_64_SIZE];
802
 
803
  memcpy (buf, section->sectname, 16);
804
  memcpy (buf + 16, section->segname, 16);
805
  bfd_h_put_64 (abfd, section->addr, buf + 32);
806
  bfd_h_put_64 (abfd, section->size, buf + 40);
807
  bfd_h_put_32 (abfd, section->offset, buf + 48);
808
  bfd_h_put_32 (abfd, section->align, buf + 52);
809
  bfd_h_put_32 (abfd, section->reloff, buf + 56);
810
  bfd_h_put_32 (abfd, section->nreloc, buf + 60);
811
  bfd_h_put_32 (abfd, section->flags, buf + 64);
812
  bfd_h_put_32 (abfd, section->reserved1, buf + 68);
813
  bfd_h_put_32 (abfd, section->reserved2, buf + 72);
814
  bfd_h_put_32 (abfd, section->reserved3, buf + 76);
815
 
816
  if (bfd_bwrite ((PTR) buf, BFD_MACH_O_SECTION_64_SIZE, abfd)
817
      != BFD_MACH_O_SECTION_64_SIZE)
818
    return -1;
819
 
820
  return 0;
821
}
822
 
823
static int
824
bfd_mach_o_scan_write_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
825
{
826
  unsigned char buf[BFD_MACH_O_LC_SEGMENT_SIZE];
827 24 jeremybenn
  bfd_mach_o_segment_command *seg = &command->command.segment;
828
  unsigned long i;
829
 
830
  BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
831
 
832 225 jeremybenn
  for (i = 0; i < seg->nsects; i++)
833
    if (!bfd_mach_o_scan_write_relocs (abfd, &seg->sections[i]))
834
      return -1;
835
 
836 24 jeremybenn
  memcpy (buf, seg->segname, 16);
837
  bfd_h_put_32 (abfd, seg->vmaddr, buf + 16);
838
  bfd_h_put_32 (abfd, seg->vmsize, buf + 20);
839
  bfd_h_put_32 (abfd, seg->fileoff, buf + 24);
840
  bfd_h_put_32 (abfd, seg->filesize, buf + 28);
841 225 jeremybenn
  bfd_h_put_32 (abfd, seg->maxprot, buf + 32);
842
  bfd_h_put_32 (abfd, seg->initprot, buf + 36);
843 24 jeremybenn
  bfd_h_put_32 (abfd, seg->nsects, buf + 40);
844
  bfd_h_put_32 (abfd, seg->flags, buf + 44);
845 225 jeremybenn
 
846
  if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
847
      || (bfd_bwrite ((PTR) buf, BFD_MACH_O_LC_SEGMENT_SIZE - 8, abfd)
848
          != BFD_MACH_O_LC_SEGMENT_SIZE - 8))
849 24 jeremybenn
    return -1;
850
 
851 225 jeremybenn
  for (i = 0; i < seg->nsects; i++)
852
    if (bfd_mach_o_scan_write_section_32 (abfd, &seg->sections[i]))
853
      return -1;
854 24 jeremybenn
 
855 225 jeremybenn
  return 0;
856
}
857 24 jeremybenn
 
858 225 jeremybenn
static int
859
bfd_mach_o_scan_write_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
860
{
861
  unsigned char buf[BFD_MACH_O_LC_SEGMENT_64_SIZE];
862
  bfd_mach_o_segment_command *seg = &command->command.segment;
863
  unsigned long i;
864 24 jeremybenn
 
865 225 jeremybenn
  BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
866 24 jeremybenn
 
867 225 jeremybenn
  for (i = 0; i < seg->nsects; i++)
868
    if (!bfd_mach_o_scan_write_relocs (abfd, &seg->sections[i]))
869
      return -1;
870 24 jeremybenn
 
871 225 jeremybenn
  memcpy (buf, seg->segname, 16);
872
  bfd_h_put_64 (abfd, seg->vmaddr, buf + 16);
873
  bfd_h_put_64 (abfd, seg->vmsize, buf + 24);
874
  bfd_h_put_64 (abfd, seg->fileoff, buf + 32);
875
  bfd_h_put_64 (abfd, seg->filesize, buf + 40);
876
  bfd_h_put_32 (abfd, seg->maxprot, buf + 48);
877
  bfd_h_put_32 (abfd, seg->initprot, buf + 52);
878
  bfd_h_put_32 (abfd, seg->nsects, buf + 56);
879
  bfd_h_put_32 (abfd, seg->flags, buf + 60);
880 24 jeremybenn
 
881 225 jeremybenn
  if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
882
      || (bfd_bwrite ((PTR) buf, BFD_MACH_O_LC_SEGMENT_64_SIZE - 8, abfd)
883
          != BFD_MACH_O_LC_SEGMENT_64_SIZE - 8))
884
    return -1;
885
 
886 24 jeremybenn
  for (i = 0; i < seg->nsects; i++)
887 225 jeremybenn
    if (bfd_mach_o_scan_write_section_64 (abfd, &seg->sections[i]))
888
      return -1;
889 24 jeremybenn
 
890
  return 0;
891
}
892
 
893 225 jeremybenn
static bfd_boolean
894
bfd_mach_o_scan_write_symtab (bfd *abfd, bfd_mach_o_load_command *command)
895 24 jeremybenn
{
896 225 jeremybenn
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
897 24 jeremybenn
  bfd_mach_o_symtab_command *sym = &command->command.symtab;
898 225 jeremybenn
  unsigned char buf[16];
899 24 jeremybenn
  unsigned long i;
900 225 jeremybenn
  unsigned int wide = bfd_mach_o_wide_p (abfd);
901
  unsigned int symlen = wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
902
  struct bfd_strtab_hash *strtab;
903
  asymbol **symbols = bfd_get_outsymbols (abfd);
904 24 jeremybenn
 
905 225 jeremybenn
  BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
906
 
907
  /* Write the symbols first.  */
908
  mdata->filelen = FILE_ALIGN(mdata->filelen, wide ? 3 : 2);
909
  sym->symoff = mdata->filelen;
910
  if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0)
911
    return FALSE;
912
 
913
  sym->nsyms = bfd_get_symcount (abfd);
914
  mdata->filelen += sym->nsyms * symlen;
915
 
916
  strtab = _bfd_stringtab_init ();
917
  if (strtab == NULL)
918
    return FALSE;
919
 
920 24 jeremybenn
  for (i = 0; i < sym->nsyms; i++)
921
    {
922 225 jeremybenn
      unsigned char buf[16];
923
      bfd_size_type index;
924
      bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
925 24 jeremybenn
 
926 225 jeremybenn
      /* Compute name index.  */
927
      /* An index of 0 always means the empty string.  */
928
      if (s->symbol.name == 0 || s->symbol.name[0] == '\0')
929
        index = 0;
930
      else
931
        {
932
          index = _bfd_stringtab_add (strtab, s->symbol.name, TRUE, FALSE);
933
          if (index == (bfd_size_type) -1)
934
            goto err;
935
        }
936
      bfd_h_put_32 (abfd, index, buf);
937
      bfd_h_put_8 (abfd, s->n_type, buf + 4);
938
      bfd_h_put_8 (abfd, s->n_sect, buf + 5);
939
      bfd_h_put_16 (abfd, s->n_desc, buf + 6);
940
      if (wide)
941
        bfd_h_put_64 (abfd, s->symbol.section->vma + s->symbol.value, buf + 8);
942
      else
943
        bfd_h_put_32 (abfd, s->symbol.section->vma + s->symbol.value, buf + 8);
944 24 jeremybenn
 
945 225 jeremybenn
      if (bfd_bwrite ((PTR) buf, symlen, abfd) != symlen)
946
        goto err;
947
    }
948
  sym->strsize = _bfd_stringtab_size (strtab);
949
  sym->stroff = mdata->filelen;
950
  mdata->filelen += sym->strsize;
951 24 jeremybenn
 
952 225 jeremybenn
  if (_bfd_stringtab_emit (abfd, strtab) != TRUE)
953
    goto err;
954
  _bfd_stringtab_free (strtab);
955 24 jeremybenn
 
956 225 jeremybenn
  /* The command.  */
957
  bfd_h_put_32 (abfd, sym->symoff, buf);
958
  bfd_h_put_32 (abfd, sym->nsyms, buf + 4);
959
  bfd_h_put_32 (abfd, sym->stroff, buf + 8);
960
  bfd_h_put_32 (abfd, sym->strsize, buf + 12);
961 24 jeremybenn
 
962 225 jeremybenn
  if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
963
      || bfd_bwrite ((PTR) buf, 16, abfd) != 16)
964
    return FALSE;
965
 
966
  return TRUE;
967
 
968
 err:
969
  _bfd_stringtab_free (strtab);
970
  return FALSE;
971 24 jeremybenn
}
972
 
973 225 jeremybenn
/* Process the symbols and generate Mach-O specific fields.
974
   Number them.  */
975
 
976
static bfd_boolean
977
bfd_mach_o_mangle_symbols (bfd *abfd)
978 24 jeremybenn
{
979 225 jeremybenn
  unsigned long i;
980
  asymbol **symbols = bfd_get_outsymbols (abfd);
981 24 jeremybenn
 
982 225 jeremybenn
  for (i = 0; i < bfd_get_symcount (abfd); i++)
983
    {
984
      bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *)symbols[i];
985 24 jeremybenn
 
986 225 jeremybenn
      if (s->n_type == BFD_MACH_O_N_UNDF && !(s->symbol.flags & BSF_DEBUGGING))
987
        {
988
          /* As genuine Mach-O symbols type shouldn't be N_UNDF (undefined
989
             symbols should be N_UNDEF | N_EXT), we suppose the back-end
990
             values haven't been set.  */
991
          if (s->symbol.section == bfd_abs_section_ptr)
992
            s->n_type = BFD_MACH_O_N_ABS;
993
          else if (s->symbol.section == bfd_und_section_ptr)
994
            {
995
              s->n_type = BFD_MACH_O_N_UNDF;
996
              if (s->symbol.flags & BSF_WEAK)
997
                s->n_desc |= BFD_MACH_O_N_WEAK_REF;
998
            }
999
          else if (s->symbol.section == bfd_com_section_ptr)
1000
            s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
1001
          else
1002
            s->n_type = BFD_MACH_O_N_SECT;
1003
 
1004
          if (s->symbol.flags & BSF_GLOBAL)
1005
            s->n_type |= BFD_MACH_O_N_EXT;
1006
        }
1007 24 jeremybenn
 
1008 225 jeremybenn
      /* Compute section index.  */
1009
      if (s->symbol.section != bfd_abs_section_ptr
1010
          && s->symbol.section != bfd_und_section_ptr
1011
          && s->symbol.section != bfd_com_section_ptr)
1012
        s->n_sect = s->symbol.section->target_index;
1013 24 jeremybenn
 
1014 225 jeremybenn
      /* Number symbols.  */
1015
      s->symbol.udata.i = i;
1016
    }
1017
  return TRUE;
1018 24 jeremybenn
}
1019
 
1020 225 jeremybenn
bfd_boolean
1021 24 jeremybenn
bfd_mach_o_write_contents (bfd *abfd)
1022
{
1023
  unsigned int i;
1024 225 jeremybenn
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1025 24 jeremybenn
 
1026 225 jeremybenn
  if (mdata->header.ncmds == 0)
1027
    if (!bfd_mach_o_build_commands (abfd))
1028
      return FALSE;
1029 24 jeremybenn
 
1030
  /* Now write header information.  */
1031 225 jeremybenn
  if (mdata->header.filetype == 0)
1032
    {
1033
      if (abfd->flags & EXEC_P)
1034
        mdata->header.filetype = BFD_MACH_O_MH_EXECUTE;
1035
      else if (abfd->flags & DYNAMIC)
1036
        mdata->header.filetype = BFD_MACH_O_MH_DYLIB;
1037
      else
1038
        mdata->header.filetype = BFD_MACH_O_MH_OBJECT;
1039
    }
1040
  if (!bfd_mach_o_write_header (abfd, &mdata->header))
1041 24 jeremybenn
    return FALSE;
1042
 
1043 225 jeremybenn
  /* Assign a number to each symbols.  */
1044
  if (!bfd_mach_o_mangle_symbols (abfd))
1045
    return FALSE;
1046
 
1047 24 jeremybenn
  for (i = 0; i < mdata->header.ncmds; i++)
1048
    {
1049
      unsigned char buf[8];
1050
      bfd_mach_o_load_command *cur = &mdata->commands[i];
1051
      unsigned long typeflag;
1052
 
1053 225 jeremybenn
      typeflag = cur->type | (cur->type_required ? BFD_MACH_O_LC_REQ_DYLD : 0);
1054 24 jeremybenn
 
1055
      bfd_h_put_32 (abfd, typeflag, buf);
1056
      bfd_h_put_32 (abfd, cur->len, buf + 4);
1057
 
1058 225 jeremybenn
      if (bfd_seek (abfd, cur->offset, SEEK_SET) != 0
1059
          || bfd_bwrite ((PTR) buf, 8, abfd) != 8)
1060 24 jeremybenn
        return FALSE;
1061
 
1062
      switch (cur->type)
1063
        {
1064
        case BFD_MACH_O_LC_SEGMENT:
1065 225 jeremybenn
          if (bfd_mach_o_scan_write_segment_32 (abfd, cur) != 0)
1066 24 jeremybenn
            return FALSE;
1067
          break;
1068 225 jeremybenn
        case BFD_MACH_O_LC_SEGMENT_64:
1069
          if (bfd_mach_o_scan_write_segment_64 (abfd, cur) != 0)
1070
            return FALSE;
1071
          break;
1072 24 jeremybenn
        case BFD_MACH_O_LC_SYMTAB:
1073 225 jeremybenn
          if (!bfd_mach_o_scan_write_symtab (abfd, cur))
1074 24 jeremybenn
            return FALSE;
1075
          break;
1076
        case BFD_MACH_O_LC_SYMSEG:
1077
          break;
1078
        case BFD_MACH_O_LC_THREAD:
1079
        case BFD_MACH_O_LC_UNIXTHREAD:
1080
          if (bfd_mach_o_scan_write_thread (abfd, cur) != 0)
1081
            return FALSE;
1082
          break;
1083
        case BFD_MACH_O_LC_LOADFVMLIB:
1084
        case BFD_MACH_O_LC_IDFVMLIB:
1085
        case BFD_MACH_O_LC_IDENT:
1086
        case BFD_MACH_O_LC_FVMFILE:
1087
        case BFD_MACH_O_LC_PREPAGE:
1088
        case BFD_MACH_O_LC_DYSYMTAB:
1089
        case BFD_MACH_O_LC_LOAD_DYLIB:
1090
        case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
1091
        case BFD_MACH_O_LC_ID_DYLIB:
1092 225 jeremybenn
        case BFD_MACH_O_LC_REEXPORT_DYLIB:
1093 24 jeremybenn
        case BFD_MACH_O_LC_LOAD_DYLINKER:
1094
        case BFD_MACH_O_LC_ID_DYLINKER:
1095
        case BFD_MACH_O_LC_PREBOUND_DYLIB:
1096
        case BFD_MACH_O_LC_ROUTINES:
1097
        case BFD_MACH_O_LC_SUB_FRAMEWORK:
1098
          break;
1099
        default:
1100
          fprintf (stderr,
1101
                   "unable to write unknown load command 0x%lx\n",
1102 225 jeremybenn
                   (unsigned long) cur->type);
1103 24 jeremybenn
          return FALSE;
1104
        }
1105
    }
1106
 
1107
  return TRUE;
1108
}
1109
 
1110 225 jeremybenn
/* Build Mach-O load commands from the sections.  */
1111
 
1112
bfd_boolean
1113
bfd_mach_o_build_commands (bfd *abfd)
1114
{
1115
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1116
  unsigned int wide = mach_o_wide_p (&mdata->header);
1117
  bfd_mach_o_segment_command *seg;
1118
  bfd_mach_o_section *sections;
1119
  asection *sec;
1120
  bfd_mach_o_load_command *cmd;
1121
  bfd_mach_o_load_command *symtab_cmd;
1122
  int target_index;
1123
 
1124
  /* Return now if commands are already built.  */
1125
  if (mdata->header.ncmds)
1126
    return FALSE;
1127
 
1128
  /* Very simple version: 1 command (segment) containing all sections.  */
1129
  mdata->header.ncmds = 2;
1130
  mdata->commands = bfd_alloc (abfd, mdata->header.ncmds
1131
                               * sizeof (bfd_mach_o_load_command));
1132
  if (mdata->commands == NULL)
1133
    return FALSE;
1134
  cmd = &mdata->commands[0];
1135
  seg = &cmd->command.segment;
1136
 
1137
  seg->nsects = bfd_count_sections (abfd);
1138
  sections = bfd_alloc (abfd, seg->nsects * sizeof (bfd_mach_o_section));
1139
  if (sections == NULL)
1140
    return FALSE;
1141
  seg->sections = sections;
1142
 
1143
  /* Set segment command.  */
1144
  if (wide)
1145
    {
1146
      cmd->type = BFD_MACH_O_LC_SEGMENT_64;
1147
      cmd->offset = BFD_MACH_O_HEADER_64_SIZE;
1148
      cmd->len = BFD_MACH_O_LC_SEGMENT_64_SIZE
1149
        + BFD_MACH_O_SECTION_64_SIZE * seg->nsects;
1150
    }
1151
  else
1152
    {
1153
      cmd->type = BFD_MACH_O_LC_SEGMENT;
1154
      cmd->offset = BFD_MACH_O_HEADER_SIZE;
1155
      cmd->len = BFD_MACH_O_LC_SEGMENT_SIZE
1156
        + BFD_MACH_O_SECTION_SIZE * seg->nsects;
1157
    }
1158
  cmd->type_required = FALSE;
1159
  mdata->header.sizeofcmds = cmd->len;
1160
  mdata->filelen = cmd->offset + cmd->len;
1161
 
1162
  /* Set symtab command.  */
1163
  symtab_cmd = &mdata->commands[1];
1164
 
1165
  symtab_cmd->type = BFD_MACH_O_LC_SYMTAB;
1166
  symtab_cmd->offset = cmd->offset + cmd->len;
1167
  symtab_cmd->len = 6 * 4;
1168
  symtab_cmd->type_required = FALSE;
1169
 
1170
  mdata->header.sizeofcmds += symtab_cmd->len;
1171
  mdata->filelen += symtab_cmd->len;
1172
 
1173
  /* Fill segment command.  */
1174
  memset (seg->segname, 0, sizeof (seg->segname));
1175
  seg->vmaddr = 0;
1176
  seg->fileoff = mdata->filelen;
1177
  seg->filesize = 0;
1178
  seg->maxprot = BFD_MACH_O_PROT_READ | BFD_MACH_O_PROT_WRITE
1179
    | BFD_MACH_O_PROT_EXECUTE;
1180
  seg->initprot = seg->maxprot;
1181
  seg->flags = 0;
1182
 
1183
  /* Create Mach-O sections.  */
1184
  target_index = 0;
1185
  for (sec = abfd->sections; sec; sec = sec->next)
1186
    {
1187
      sections->bfdsection = sec;
1188
      bfd_mach_o_convert_section_name_to_mach_o (abfd, sec, sections);
1189
      sections->addr = bfd_get_section_vma (abfd, sec);
1190
      sections->size = bfd_get_section_size (sec);
1191
      sections->align = bfd_get_section_alignment (abfd, sec);
1192
 
1193
      if (sections->size != 0)
1194
        {
1195
          mdata->filelen = FILE_ALIGN (mdata->filelen, sections->align);
1196
          sections->offset = mdata->filelen;
1197
        }
1198
      else
1199
        sections->offset = 0;
1200
      sections->reloff = 0;
1201
      sections->nreloc = 0;
1202
      sections->reserved1 = 0;
1203
      sections->reserved2 = 0;
1204
      sections->reserved3 = 0;
1205
 
1206
      sec->filepos = sections->offset;
1207
      sec->target_index = ++target_index;
1208
 
1209
      mdata->filelen += sections->size;
1210
      sections++;
1211
    }
1212
  seg->filesize = mdata->filelen - seg->fileoff;
1213
  seg->vmsize = seg->filesize;
1214
 
1215
  return TRUE;
1216
}
1217
 
1218
/* Set the contents of a section.  */
1219
 
1220
bfd_boolean
1221
bfd_mach_o_set_section_contents (bfd *abfd,
1222
                                 asection *section,
1223
                                 const void * location,
1224
                                 file_ptr offset,
1225
                                 bfd_size_type count)
1226
{
1227
  file_ptr pos;
1228
 
1229
  /* This must be done first, because bfd_set_section_contents is
1230
     going to set output_has_begun to TRUE.  */
1231
  if (! abfd->output_has_begun && ! bfd_mach_o_build_commands (abfd))
1232
    return FALSE;
1233
 
1234
  if (count == 0)
1235
    return TRUE;
1236
 
1237
  pos = section->filepos + offset;
1238
  if (bfd_seek (abfd, pos, SEEK_SET) != 0
1239
      || bfd_bwrite (location, count, abfd) != count)
1240
    return FALSE;
1241
 
1242
  return TRUE;
1243
}
1244
 
1245
int
1246 24 jeremybenn
bfd_mach_o_sizeof_headers (bfd *a ATTRIBUTE_UNUSED,
1247
                           struct bfd_link_info *info ATTRIBUTE_UNUSED)
1248
{
1249
  return 0;
1250
}
1251
 
1252
/* Make an empty symbol.  This is required only because
1253
   bfd_make_section_anyway wants to create a symbol for the section.  */
1254
 
1255 225 jeremybenn
asymbol *
1256 24 jeremybenn
bfd_mach_o_make_empty_symbol (bfd *abfd)
1257
{
1258 225 jeremybenn
  asymbol *new_symbol;
1259 24 jeremybenn
 
1260 225 jeremybenn
  new_symbol = bfd_zalloc (abfd, sizeof (bfd_mach_o_asymbol));
1261
  if (new_symbol == NULL)
1262
    return new_symbol;
1263
  new_symbol->the_bfd = abfd;
1264
  new_symbol->udata.i = 0;
1265
  return new_symbol;
1266 24 jeremybenn
}
1267
 
1268 225 jeremybenn
static bfd_boolean
1269 24 jeremybenn
bfd_mach_o_read_header (bfd *abfd, bfd_mach_o_header *header)
1270
{
1271 225 jeremybenn
  unsigned char buf[32];
1272
  unsigned int size;
1273 24 jeremybenn
  bfd_vma (*get32) (const void *) = NULL;
1274
 
1275 225 jeremybenn
  /* Just read the magic number.  */
1276
  if (bfd_seek (abfd, 0, SEEK_SET) != 0
1277
      || bfd_bread ((PTR) buf, 4, abfd) != 4)
1278
    return FALSE;
1279 24 jeremybenn
 
1280 225 jeremybenn
  if (bfd_getb32 (buf) == BFD_MACH_O_MH_MAGIC)
1281 24 jeremybenn
    {
1282
      header->byteorder = BFD_ENDIAN_BIG;
1283 225 jeremybenn
      header->magic = BFD_MACH_O_MH_MAGIC;
1284
      header->version = 1;
1285 24 jeremybenn
      get32 = bfd_getb32;
1286
    }
1287 225 jeremybenn
  else if (bfd_getl32 (buf) == BFD_MACH_O_MH_MAGIC)
1288 24 jeremybenn
    {
1289
      header->byteorder = BFD_ENDIAN_LITTLE;
1290 225 jeremybenn
      header->magic = BFD_MACH_O_MH_MAGIC;
1291
      header->version = 1;
1292 24 jeremybenn
      get32 = bfd_getl32;
1293
    }
1294 225 jeremybenn
  else if (bfd_getb32 (buf) == BFD_MACH_O_MH_MAGIC_64)
1295
    {
1296
      header->byteorder = BFD_ENDIAN_BIG;
1297
      header->magic = BFD_MACH_O_MH_MAGIC_64;
1298
      header->version = 2;
1299
      get32 = bfd_getb32;
1300
    }
1301
  else if (bfd_getl32 (buf) == BFD_MACH_O_MH_MAGIC_64)
1302
    {
1303
      header->byteorder = BFD_ENDIAN_LITTLE;
1304
      header->magic = BFD_MACH_O_MH_MAGIC_64;
1305
      header->version = 2;
1306
      get32 = bfd_getl32;
1307
    }
1308 24 jeremybenn
  else
1309
    {
1310
      header->byteorder = BFD_ENDIAN_UNKNOWN;
1311 225 jeremybenn
      return FALSE;
1312 24 jeremybenn
    }
1313
 
1314 225 jeremybenn
  /* Once the size of the header is known, read the full header.  */
1315
  size = mach_o_wide_p (header) ?
1316
    BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
1317
 
1318
  if (bfd_seek (abfd, 0, SEEK_SET) != 0
1319
      || bfd_bread ((PTR) buf, size, abfd) != size)
1320
    return FALSE;
1321
 
1322 24 jeremybenn
  header->cputype = (*get32) (buf + 4);
1323
  header->cpusubtype = (*get32) (buf + 8);
1324
  header->filetype = (*get32) (buf + 12);
1325
  header->ncmds = (*get32) (buf + 16);
1326
  header->sizeofcmds = (*get32) (buf + 20);
1327
  header->flags = (*get32) (buf + 24);
1328
 
1329 225 jeremybenn
  if (mach_o_wide_p (header))
1330
    header->reserved = (*get32) (buf + 28);
1331
 
1332
  return TRUE;
1333 24 jeremybenn
}
1334
 
1335
static asection *
1336 225 jeremybenn
bfd_mach_o_make_bfd_section (bfd *abfd, bfd_mach_o_section *section,
1337
                             unsigned long prot)
1338 24 jeremybenn
{
1339
  asection *bfdsec;
1340
  char *sname;
1341
  flagword flags;
1342
 
1343 225 jeremybenn
  sname = bfd_mach_o_convert_section_name_to_bfd (abfd, section);
1344 24 jeremybenn
  if (sname == NULL)
1345
    return NULL;
1346
 
1347 225 jeremybenn
  if (section->flags & BFD_MACH_O_S_ATTR_DEBUG)
1348
    flags = SEC_HAS_CONTENTS | SEC_DEBUGGING;
1349
  else
1350
    {
1351
      flags = SEC_ALLOC;
1352
      if ((section->flags & BFD_MACH_O_SECTION_TYPE_MASK)
1353
          != BFD_MACH_O_S_ZEROFILL)
1354
        {
1355
          flags |= SEC_HAS_CONTENTS | SEC_LOAD;
1356
          if (prot & BFD_MACH_O_PROT_EXECUTE)
1357
            flags |= SEC_CODE;
1358
          if (prot & BFD_MACH_O_PROT_WRITE)
1359
            flags |= SEC_DATA;
1360
          else if (prot & BFD_MACH_O_PROT_READ)
1361
            flags |= SEC_READONLY;
1362
        }
1363
    }
1364
  if (section->nreloc != 0)
1365
    flags |= SEC_RELOC;
1366
 
1367 24 jeremybenn
  bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, flags);
1368
  if (bfdsec == NULL)
1369
    return NULL;
1370
 
1371
  bfdsec->vma = section->addr;
1372
  bfdsec->lma = section->addr;
1373
  bfdsec->size = section->size;
1374
  bfdsec->filepos = section->offset;
1375
  bfdsec->alignment_power = section->align;
1376 225 jeremybenn
  bfdsec->segment_mark = 0;
1377
  bfdsec->reloc_count = section->nreloc;
1378
  bfdsec->rel_filepos = section->reloff;
1379 24 jeremybenn
 
1380
  return bfdsec;
1381
}
1382
 
1383
static int
1384 225 jeremybenn
bfd_mach_o_scan_read_section_32 (bfd *abfd,
1385
                                 bfd_mach_o_section *section,
1386
                                 unsigned int offset,
1387
                                 unsigned long prot)
1388 24 jeremybenn
{
1389 225 jeremybenn
  unsigned char buf[BFD_MACH_O_SECTION_SIZE];
1390 24 jeremybenn
 
1391 225 jeremybenn
  if (bfd_seek (abfd, offset, SEEK_SET) != 0
1392
      || (bfd_bread ((PTR) buf, BFD_MACH_O_SECTION_SIZE, abfd)
1393
          != BFD_MACH_O_SECTION_SIZE))
1394 24 jeremybenn
    return -1;
1395
 
1396
  memcpy (section->sectname, buf, 16);
1397
  section->sectname[16] = '\0';
1398
  memcpy (section->segname, buf + 16, 16);
1399
  section->segname[16] = '\0';
1400
  section->addr = bfd_h_get_32 (abfd, buf + 32);
1401
  section->size = bfd_h_get_32 (abfd, buf + 36);
1402
  section->offset = bfd_h_get_32 (abfd, buf + 40);
1403
  section->align = bfd_h_get_32 (abfd, buf + 44);
1404
  section->reloff = bfd_h_get_32 (abfd, buf + 48);
1405
  section->nreloc = bfd_h_get_32 (abfd, buf + 52);
1406
  section->flags = bfd_h_get_32 (abfd, buf + 56);
1407
  section->reserved1 = bfd_h_get_32 (abfd, buf + 60);
1408
  section->reserved2 = bfd_h_get_32 (abfd, buf + 64);
1409 225 jeremybenn
  section->reserved3 = 0;
1410
  section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section, prot);
1411 24 jeremybenn
 
1412
  if (section->bfdsection == NULL)
1413
    return -1;
1414
 
1415
  return 0;
1416
}
1417
 
1418 225 jeremybenn
static int
1419
bfd_mach_o_scan_read_section_64 (bfd *abfd,
1420
                                 bfd_mach_o_section *section,
1421
                                 unsigned int offset,
1422
                                 unsigned long prot)
1423
{
1424
  unsigned char buf[BFD_MACH_O_SECTION_64_SIZE];
1425
 
1426
  if (bfd_seek (abfd, offset, SEEK_SET) != 0
1427
      || (bfd_bread ((PTR) buf, BFD_MACH_O_SECTION_64_SIZE, abfd)
1428
          != BFD_MACH_O_SECTION_64_SIZE))
1429
    return -1;
1430
 
1431
  memcpy (section->sectname, buf, 16);
1432
  section->sectname[16] = '\0';
1433
  memcpy (section->segname, buf + 16, 16);
1434
  section->segname[16] = '\0';
1435
  section->addr = bfd_h_get_64 (abfd, buf + 32);
1436
  section->size = bfd_h_get_64 (abfd, buf + 40);
1437
  section->offset = bfd_h_get_32 (abfd, buf + 48);
1438
  section->align = bfd_h_get_32 (abfd, buf + 52);
1439
  section->reloff = bfd_h_get_32 (abfd, buf + 56);
1440
  section->nreloc = bfd_h_get_32 (abfd, buf + 60);
1441
  section->flags = bfd_h_get_32 (abfd, buf + 64);
1442
  section->reserved1 = bfd_h_get_32 (abfd, buf + 68);
1443
  section->reserved2 = bfd_h_get_32 (abfd, buf + 72);
1444
  section->reserved3 = bfd_h_get_32 (abfd, buf + 76);
1445
  section->bfdsection = bfd_mach_o_make_bfd_section (abfd, section, prot);
1446
 
1447
  if (section->bfdsection == NULL)
1448
    return -1;
1449
 
1450
  return 0;
1451
}
1452
 
1453
static int
1454
bfd_mach_o_scan_read_section (bfd *abfd,
1455
                              bfd_mach_o_section *section,
1456
                              unsigned int offset,
1457
                              unsigned long prot,
1458
                              unsigned int wide)
1459
{
1460
  if (wide)
1461
    return bfd_mach_o_scan_read_section_64 (abfd, section, offset, prot);
1462
  else
1463
    return bfd_mach_o_scan_read_section_32 (abfd, section, offset, prot);
1464
}
1465
 
1466
static int
1467 24 jeremybenn
bfd_mach_o_scan_read_symtab_symbol (bfd *abfd,
1468
                                    bfd_mach_o_symtab_command *sym,
1469 225 jeremybenn
                                    bfd_mach_o_asymbol *s,
1470 24 jeremybenn
                                    unsigned long i)
1471
{
1472 225 jeremybenn
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1473
  unsigned int wide = mach_o_wide_p (&mdata->header);
1474
  unsigned int symwidth =
1475
    wide ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE;
1476
  unsigned int symoff = sym->symoff + (i * symwidth);
1477
  unsigned char buf[16];
1478 24 jeremybenn
  unsigned char type = -1;
1479
  unsigned char section = -1;
1480
  short desc = -1;
1481 225 jeremybenn
  symvalue value = -1;
1482 24 jeremybenn
  unsigned long stroff = -1;
1483
  unsigned int symtype = -1;
1484
 
1485
  BFD_ASSERT (sym->strtab != NULL);
1486
 
1487 225 jeremybenn
  if (bfd_seek (abfd, symoff, SEEK_SET) != 0
1488
      || bfd_bread ((PTR) buf, symwidth, abfd) != symwidth)
1489 24 jeremybenn
    {
1490
      fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: unable to read %d bytes at %lu\n",
1491 225 jeremybenn
               symwidth, (unsigned long) symoff);
1492 24 jeremybenn
      return -1;
1493
    }
1494
 
1495
  stroff = bfd_h_get_32 (abfd, buf);
1496
  type = bfd_h_get_8 (abfd, buf + 4);
1497 225 jeremybenn
  symtype = type & BFD_MACH_O_N_TYPE;
1498
  section = bfd_h_get_8 (abfd, buf + 5);
1499 24 jeremybenn
  desc = bfd_h_get_16 (abfd, buf + 6);
1500 225 jeremybenn
  if (wide)
1501
    value = bfd_h_get_64 (abfd, buf + 8);
1502
  else
1503
    value = bfd_h_get_32 (abfd, buf + 8);
1504 24 jeremybenn
 
1505
  if (stroff >= sym->strsize)
1506
    {
1507
      fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: symbol name out of range (%lu >= %lu)\n",
1508
               (unsigned long) stroff, (unsigned long) sym->strsize);
1509
      return -1;
1510
    }
1511
 
1512 225 jeremybenn
  s->symbol.the_bfd = abfd;
1513
  s->symbol.name = sym->strtab + stroff;
1514
  s->symbol.value = value;
1515
  s->symbol.flags = 0x0;
1516
  s->symbol.udata.i = 0;
1517
  s->n_type = type;
1518
  s->n_sect = section;
1519
  s->n_desc = desc;
1520 24 jeremybenn
 
1521
  if (type & BFD_MACH_O_N_STAB)
1522
    {
1523 225 jeremybenn
      s->symbol.flags |= BSF_DEBUGGING;
1524
      s->symbol.section = bfd_und_section_ptr;
1525
      switch (type)
1526
        {
1527
        case N_FUN:
1528
        case N_STSYM:
1529
        case N_LCSYM:
1530
        case N_BNSYM:
1531
        case N_SLINE:
1532
        case N_ENSYM:
1533
        case N_ECOMM:
1534
        case N_ECOML:
1535
        case N_GSYM:
1536
          if ((section > 0) && (section <= mdata->nsects))
1537
            {
1538
              s->symbol.section = mdata->sections[section - 1]->bfdsection;
1539
              s->symbol.value =
1540
                s->symbol.value - mdata->sections[section - 1]->addr;
1541
            }
1542
          break;
1543
        }
1544 24 jeremybenn
    }
1545
  else
1546
    {
1547
      if (type & BFD_MACH_O_N_PEXT)
1548 225 jeremybenn
        s->symbol.flags |= BSF_GLOBAL;
1549 24 jeremybenn
 
1550
      if (type & BFD_MACH_O_N_EXT)
1551 225 jeremybenn
        s->symbol.flags |= BSF_GLOBAL;
1552 24 jeremybenn
 
1553 225 jeremybenn
      if (!(type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
1554
        s->symbol.flags |= BSF_LOCAL;
1555
 
1556 24 jeremybenn
      switch (symtype)
1557
        {
1558
        case BFD_MACH_O_N_UNDF:
1559 225 jeremybenn
          if (type == (BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT)
1560
              && s->symbol.value != 0)
1561
            {
1562
              /* A common symbol.  */
1563
              s->symbol.section = bfd_com_section_ptr;
1564
              s->symbol.flags = BSF_NO_FLAGS;
1565
            }
1566
          else
1567
            {
1568
              s->symbol.section = bfd_und_section_ptr;
1569
              if (s->n_desc & BFD_MACH_O_N_WEAK_REF)
1570
                s->symbol.flags |= BSF_WEAK;
1571
            }
1572 24 jeremybenn
          break;
1573
        case BFD_MACH_O_N_PBUD:
1574 225 jeremybenn
          s->symbol.section = bfd_und_section_ptr;
1575 24 jeremybenn
          break;
1576
        case BFD_MACH_O_N_ABS:
1577 225 jeremybenn
          s->symbol.section = bfd_abs_section_ptr;
1578 24 jeremybenn
          break;
1579
        case BFD_MACH_O_N_SECT:
1580
          if ((section > 0) && (section <= mdata->nsects))
1581
            {
1582 225 jeremybenn
              s->symbol.section = mdata->sections[section - 1]->bfdsection;
1583
              s->symbol.value =
1584
                s->symbol.value - mdata->sections[section - 1]->addr;
1585 24 jeremybenn
            }
1586
          else
1587
            {
1588
              /* Mach-O uses 0 to mean "no section"; not an error.  */
1589
              if (section != 0)
1590
                {
1591
                  fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
1592
                           "symbol \"%s\" specified invalid section %d (max %lu): setting to undefined\n",
1593 225 jeremybenn
                           s->symbol.name, section, mdata->nsects);
1594 24 jeremybenn
                }
1595 225 jeremybenn
              s->symbol.section = bfd_und_section_ptr;
1596 24 jeremybenn
            }
1597
          break;
1598
        case BFD_MACH_O_N_INDR:
1599
          fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
1600
                   "symbol \"%s\" is unsupported 'indirect' reference: setting to undefined\n",
1601 225 jeremybenn
                   s->symbol.name);
1602
          s->symbol.section = bfd_und_section_ptr;
1603 24 jeremybenn
          break;
1604
        default:
1605
          fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: "
1606
                   "symbol \"%s\" specified invalid type field 0x%x: setting to undefined\n",
1607 225 jeremybenn
                   s->symbol.name, symtype);
1608
          s->symbol.section = bfd_und_section_ptr;
1609 24 jeremybenn
          break;
1610
        }
1611
    }
1612
 
1613
  return 0;
1614
}
1615
 
1616 225 jeremybenn
static int
1617
bfd_mach_o_scan_read_symtab_strtab (bfd *abfd)
1618 24 jeremybenn
{
1619 225 jeremybenn
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1620
  bfd_mach_o_symtab_command *sym = mdata->symtab;
1621 24 jeremybenn
 
1622 225 jeremybenn
  /* Fail if there is no symtab.  */
1623
  if (sym == NULL)
1624
    return -1;
1625
 
1626
  /* Success if already loaded.  */
1627
  if (sym->strtab)
1628
    return 0;
1629
 
1630 24 jeremybenn
  if (abfd->flags & BFD_IN_MEMORY)
1631
    {
1632
      struct bfd_in_memory *b;
1633
 
1634
      b = (struct bfd_in_memory *) abfd->iostream;
1635
 
1636
      if ((sym->stroff + sym->strsize) > b->size)
1637
        {
1638
          bfd_set_error (bfd_error_file_truncated);
1639
          return -1;
1640
        }
1641
      sym->strtab = (char *) b->buffer + sym->stroff;
1642
    }
1643 225 jeremybenn
  else
1644
    {
1645
      sym->strtab = bfd_alloc (abfd, sym->strsize);
1646
      if (sym->strtab == NULL)
1647
        return -1;
1648 24 jeremybenn
 
1649 225 jeremybenn
      if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0
1650
          || bfd_bread ((PTR) sym->strtab, sym->strsize, abfd) != sym->strsize)
1651
        {
1652
          bfd_set_error (bfd_error_file_truncated);
1653
          return -1;
1654
        }
1655 24 jeremybenn
    }
1656
 
1657
  return 0;
1658
}
1659
 
1660 225 jeremybenn
static int
1661
bfd_mach_o_scan_read_symtab_symbols (bfd *abfd)
1662 24 jeremybenn
{
1663 225 jeremybenn
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1664
  bfd_mach_o_symtab_command *sym = mdata->symtab;
1665 24 jeremybenn
  unsigned long i;
1666
  int ret;
1667
 
1668 225 jeremybenn
  if (sym->symbols)
1669
    return 0;
1670 24 jeremybenn
 
1671 225 jeremybenn
  sym->symbols = bfd_alloc (abfd, sym->nsyms * sizeof (bfd_mach_o_asymbol));
1672
 
1673 24 jeremybenn
  if (sym->symbols == NULL)
1674
    {
1675
      fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbols: unable to allocate memory for symbols\n");
1676
      return -1;
1677
    }
1678
 
1679 225 jeremybenn
  ret = bfd_mach_o_scan_read_symtab_strtab (abfd);
1680 24 jeremybenn
  if (ret != 0)
1681
    return ret;
1682
 
1683
  for (i = 0; i < sym->nsyms; i++)
1684
    {
1685
      ret = bfd_mach_o_scan_read_symtab_symbol (abfd, sym, &sym->symbols[i], i);
1686
      if (ret != 0)
1687
        return ret;
1688
    }
1689
 
1690
  return 0;
1691
}
1692
 
1693
int
1694
bfd_mach_o_scan_read_dysymtab_symbol (bfd *abfd,
1695
                                      bfd_mach_o_dysymtab_command *dysym,
1696
                                      bfd_mach_o_symtab_command *sym,
1697 225 jeremybenn
                                      bfd_mach_o_asymbol *s,
1698 24 jeremybenn
                                      unsigned long i)
1699
{
1700
  unsigned long isymoff = dysym->indirectsymoff + (i * 4);
1701
  unsigned long symindex;
1702
  unsigned char buf[4];
1703
 
1704
  BFD_ASSERT (i < dysym->nindirectsyms);
1705
 
1706 225 jeremybenn
  if (bfd_seek (abfd, isymoff, SEEK_SET) != 0
1707
      || bfd_bread ((PTR) buf, 4, abfd) != 4)
1708 24 jeremybenn
    {
1709
      fprintf (stderr, "bfd_mach_o_scan_read_dysymtab_symbol: unable to read %lu bytes at %lu\n",
1710
               (unsigned long) 4, isymoff);
1711
      return -1;
1712
    }
1713
  symindex = bfd_h_get_32 (abfd, buf);
1714
 
1715
  return bfd_mach_o_scan_read_symtab_symbol (abfd, sym, s, symindex);
1716
}
1717
 
1718
static const char *
1719
bfd_mach_o_i386_flavour_string (unsigned int flavour)
1720
{
1721
  switch ((int) flavour)
1722
    {
1723 225 jeremybenn
    case BFD_MACH_O_x86_THREAD_STATE32:    return "x86_THREAD_STATE32";
1724
    case BFD_MACH_O_x86_FLOAT_STATE32:     return "x86_FLOAT_STATE32";
1725
    case BFD_MACH_O_x86_EXCEPTION_STATE32: return "x86_EXCEPTION_STATE32";
1726
    case BFD_MACH_O_x86_THREAD_STATE64:    return "x86_THREAD_STATE64";
1727
    case BFD_MACH_O_x86_FLOAT_STATE64:     return "x86_FLOAT_STATE64";
1728
    case BFD_MACH_O_x86_EXCEPTION_STATE64: return "x86_EXCEPTION_STATE64";
1729
    case BFD_MACH_O_x86_THREAD_STATE:      return "x86_THREAD_STATE";
1730
    case BFD_MACH_O_x86_FLOAT_STATE:       return "x86_FLOAT_STATE";
1731
    case BFD_MACH_O_x86_EXCEPTION_STATE:   return "x86_EXCEPTION_STATE";
1732
    case BFD_MACH_O_x86_DEBUG_STATE32:     return "x86_DEBUG_STATE32";
1733
    case BFD_MACH_O_x86_DEBUG_STATE64:     return "x86_DEBUG_STATE64";
1734
    case BFD_MACH_O_x86_DEBUG_STATE:       return "x86_DEBUG_STATE";
1735
    case BFD_MACH_O_x86_THREAD_STATE_NONE: return "x86_THREAD_STATE_NONE";
1736 24 jeremybenn
    default: return "UNKNOWN";
1737
    }
1738
}
1739
 
1740
static const char *
1741
bfd_mach_o_ppc_flavour_string (unsigned int flavour)
1742
{
1743
  switch ((int) flavour)
1744
    {
1745 225 jeremybenn
    case BFD_MACH_O_PPC_THREAD_STATE:      return "PPC_THREAD_STATE";
1746
    case BFD_MACH_O_PPC_FLOAT_STATE:       return "PPC_FLOAT_STATE";
1747
    case BFD_MACH_O_PPC_EXCEPTION_STATE:   return "PPC_EXCEPTION_STATE";
1748
    case BFD_MACH_O_PPC_VECTOR_STATE:      return "PPC_VECTOR_STATE";
1749
    case BFD_MACH_O_PPC_THREAD_STATE64:    return "PPC_THREAD_STATE64";
1750
    case BFD_MACH_O_PPC_EXCEPTION_STATE64: return "PPC_EXCEPTION_STATE64";
1751 24 jeremybenn
    default: return "UNKNOWN";
1752
    }
1753
}
1754
 
1755
static int
1756
bfd_mach_o_scan_read_dylinker (bfd *abfd,
1757
                               bfd_mach_o_load_command *command)
1758
{
1759
  bfd_mach_o_dylinker_command *cmd = &command->command.dylinker;
1760
  unsigned char buf[4];
1761
  unsigned int nameoff;
1762
 
1763
  BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
1764
              || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));
1765
 
1766 225 jeremybenn
  if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
1767
      || bfd_bread ((PTR) buf, 4, abfd) != 4)
1768 24 jeremybenn
    return -1;
1769
 
1770
  nameoff = bfd_h_get_32 (abfd, buf + 0);
1771
 
1772
  cmd->name_offset = command->offset + nameoff;
1773
  cmd->name_len = command->len - nameoff;
1774 225 jeremybenn
  cmd->name_str = bfd_alloc (abfd, cmd->name_len);
1775
  if (cmd->name_str == NULL)
1776 24 jeremybenn
    return -1;
1777 225 jeremybenn
  if (bfd_seek (abfd, cmd->name_offset, SEEK_SET) != 0
1778
      || bfd_bread (cmd->name_str, cmd->name_len, abfd) != cmd->name_len)
1779 24 jeremybenn
    return -1;
1780
  return 0;
1781
}
1782
 
1783
static int
1784
bfd_mach_o_scan_read_dylib (bfd *abfd, bfd_mach_o_load_command *command)
1785
{
1786
  bfd_mach_o_dylib_command *cmd = &command->command.dylib;
1787
  unsigned char buf[16];
1788
  unsigned int nameoff;
1789
 
1790 225 jeremybenn
  switch (command->type)
1791
    {
1792
    case BFD_MACH_O_LC_LOAD_DYLIB:
1793
    case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
1794
    case BFD_MACH_O_LC_ID_DYLIB:
1795
    case BFD_MACH_O_LC_REEXPORT_DYLIB:
1796
      break;
1797
    default:
1798
      BFD_FAIL ();
1799
      return -1;
1800
    }
1801 24 jeremybenn
 
1802 225 jeremybenn
  if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
1803
      || bfd_bread ((PTR) buf, 16, abfd) != 16)
1804 24 jeremybenn
    return -1;
1805
 
1806
  nameoff = bfd_h_get_32 (abfd, buf + 0);
1807
  cmd->timestamp = bfd_h_get_32 (abfd, buf + 4);
1808
  cmd->current_version = bfd_h_get_32 (abfd, buf + 8);
1809
  cmd->compatibility_version = bfd_h_get_32 (abfd, buf + 12);
1810
 
1811
  cmd->name_offset = command->offset + nameoff;
1812
  cmd->name_len = command->len - nameoff;
1813 225 jeremybenn
  cmd->name_str = bfd_alloc (abfd, cmd->name_len);
1814
  if (cmd->name_str == NULL)
1815 24 jeremybenn
    return -1;
1816 225 jeremybenn
  if (bfd_seek (abfd, cmd->name_offset, SEEK_SET) != 0
1817
      || bfd_bread (cmd->name_str, cmd->name_len, abfd) != cmd->name_len)
1818 24 jeremybenn
    return -1;
1819
  return 0;
1820
}
1821
 
1822
static int
1823
bfd_mach_o_scan_read_prebound_dylib (bfd *abfd ATTRIBUTE_UNUSED,
1824
                                     bfd_mach_o_load_command *command ATTRIBUTE_UNUSED)
1825
{
1826
  /* bfd_mach_o_prebound_dylib_command *cmd = &command->command.prebound_dylib; */
1827
 
1828
  BFD_ASSERT (command->type == BFD_MACH_O_LC_PREBOUND_DYLIB);
1829
  return 0;
1830
}
1831
 
1832
static int
1833
bfd_mach_o_scan_read_thread (bfd *abfd, bfd_mach_o_load_command *command)
1834
{
1835 225 jeremybenn
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1836 24 jeremybenn
  bfd_mach_o_thread_command *cmd = &command->command.thread;
1837
  unsigned char buf[8];
1838 225 jeremybenn
  unsigned int offset;
1839 24 jeremybenn
  unsigned int nflavours;
1840
  unsigned int i;
1841
 
1842
  BFD_ASSERT ((command->type == BFD_MACH_O_LC_THREAD)
1843
              || (command->type == BFD_MACH_O_LC_UNIXTHREAD));
1844
 
1845 225 jeremybenn
  /* Count the number of threads.  */
1846 24 jeremybenn
  offset = 8;
1847
  nflavours = 0;
1848
  while (offset != command->len)
1849
    {
1850
      if (offset >= command->len)
1851
        return -1;
1852
 
1853 225 jeremybenn
      if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
1854
          || bfd_bread ((PTR) buf, 8, abfd) != 8)
1855 24 jeremybenn
        return -1;
1856
 
1857
      offset += 8 + bfd_h_get_32 (abfd, buf + 4) * 4;
1858
      nflavours++;
1859
    }
1860
 
1861 225 jeremybenn
  /* Allocate threads.  */
1862
  cmd->flavours = bfd_alloc
1863
    (abfd, nflavours * sizeof (bfd_mach_o_thread_flavour));
1864 24 jeremybenn
  if (cmd->flavours == NULL)
1865
    return -1;
1866
  cmd->nflavours = nflavours;
1867
 
1868
  offset = 8;
1869
  nflavours = 0;
1870
  while (offset != command->len)
1871
    {
1872
      if (offset >= command->len)
1873
        return -1;
1874
 
1875
      if (nflavours >= cmd->nflavours)
1876
        return -1;
1877
 
1878 225 jeremybenn
      if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
1879
          || bfd_bread ((PTR) buf, 8, abfd) != 8)
1880 24 jeremybenn
        return -1;
1881
 
1882
      cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, buf);
1883
      cmd->flavours[nflavours].offset = command->offset + offset + 8;
1884
      cmd->flavours[nflavours].size = bfd_h_get_32 (abfd, buf + 4) * 4;
1885
      offset += cmd->flavours[nflavours].size + 8;
1886
      nflavours++;
1887
    }
1888
 
1889
  for (i = 0; i < nflavours; i++)
1890
    {
1891
      asection *bfdsec;
1892
      unsigned int snamelen;
1893
      char *sname;
1894
      const char *flavourstr;
1895
      const char *prefix = "LC_THREAD";
1896
      unsigned int j = 0;
1897
 
1898
      switch (mdata->header.cputype)
1899
        {
1900
        case BFD_MACH_O_CPU_TYPE_POWERPC:
1901 225 jeremybenn
        case BFD_MACH_O_CPU_TYPE_POWERPC_64:
1902 24 jeremybenn
          flavourstr = bfd_mach_o_ppc_flavour_string (cmd->flavours[i].flavour);
1903
          break;
1904
        case BFD_MACH_O_CPU_TYPE_I386:
1905 225 jeremybenn
        case BFD_MACH_O_CPU_TYPE_X86_64:
1906 24 jeremybenn
          flavourstr = bfd_mach_o_i386_flavour_string (cmd->flavours[i].flavour);
1907
          break;
1908
        default:
1909
          flavourstr = "UNKNOWN_ARCHITECTURE";
1910
          break;
1911
        }
1912
 
1913
      snamelen = strlen (prefix) + 1 + 20 + 1 + strlen (flavourstr) + 1;
1914
      sname = bfd_alloc (abfd, snamelen);
1915
      if (sname == NULL)
1916
        return -1;
1917
 
1918
      for (;;)
1919
        {
1920
          sprintf (sname, "%s.%s.%u", prefix, flavourstr, j);
1921
          if (bfd_get_section_by_name (abfd, sname) == NULL)
1922
            break;
1923
          j++;
1924
        }
1925
 
1926
      bfdsec = bfd_make_section_with_flags (abfd, sname, SEC_HAS_CONTENTS);
1927
 
1928
      bfdsec->vma = 0;
1929
      bfdsec->lma = 0;
1930
      bfdsec->size = cmd->flavours[i].size;
1931
      bfdsec->filepos = cmd->flavours[i].offset;
1932
      bfdsec->alignment_power = 0x0;
1933
 
1934
      cmd->section = bfdsec;
1935
    }
1936
 
1937
  return 0;
1938
}
1939
 
1940
static int
1941
bfd_mach_o_scan_read_dysymtab (bfd *abfd, bfd_mach_o_load_command *command)
1942
{
1943 225 jeremybenn
  bfd_mach_o_dysymtab_command *cmd = &command->command.dysymtab;
1944
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
1945 24 jeremybenn
  unsigned char buf[72];
1946
 
1947
  BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);
1948
 
1949 225 jeremybenn
  if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
1950
      || bfd_bread ((PTR) buf, 72, abfd) != 72)
1951 24 jeremybenn
    return -1;
1952
 
1953 225 jeremybenn
  cmd->ilocalsym = bfd_h_get_32 (abfd, buf + 0);
1954
  cmd->nlocalsym = bfd_h_get_32 (abfd, buf + 4);
1955
  cmd->iextdefsym = bfd_h_get_32 (abfd, buf + 8);
1956
  cmd->nextdefsym = bfd_h_get_32 (abfd, buf + 12);
1957
  cmd->iundefsym = bfd_h_get_32 (abfd, buf + 16);
1958
  cmd->nundefsym = bfd_h_get_32 (abfd, buf + 20);
1959
  cmd->tocoff = bfd_h_get_32 (abfd, buf + 24);
1960
  cmd->ntoc = bfd_h_get_32 (abfd, buf + 28);
1961
  cmd->modtaboff = bfd_h_get_32 (abfd, buf + 32);
1962
  cmd->nmodtab = bfd_h_get_32 (abfd, buf + 36);
1963
  cmd->extrefsymoff = bfd_h_get_32 (abfd, buf + 40);
1964
  cmd->nextrefsyms = bfd_h_get_32 (abfd, buf + 44);
1965
  cmd->indirectsymoff = bfd_h_get_32 (abfd, buf + 48);
1966
  cmd->nindirectsyms = bfd_h_get_32 (abfd, buf + 52);
1967
  cmd->extreloff = bfd_h_get_32 (abfd, buf + 56);
1968
  cmd->nextrel = bfd_h_get_32 (abfd, buf + 60);
1969
  cmd->locreloff = bfd_h_get_32 (abfd, buf + 64);
1970
  cmd->nlocrel = bfd_h_get_32 (abfd, buf + 68);
1971 24 jeremybenn
 
1972 225 jeremybenn
  if (cmd->nmodtab != 0)
1973
    {
1974
      char buf[56];
1975
      unsigned int i;
1976
      int wide = bfd_mach_o_wide_p (abfd);
1977
      unsigned int module_len = wide ? 56 : 52;
1978
 
1979
      cmd->dylib_module =
1980
        bfd_alloc (abfd, cmd->nmodtab * sizeof (bfd_mach_o_dylib_module));
1981
      if (cmd->dylib_module == NULL)
1982
        return -1;
1983
 
1984
      if (bfd_seek (abfd, cmd->modtaboff, SEEK_SET) != 0)
1985
        return -1;
1986
 
1987
      for (i = 0; i < cmd->nmodtab; i++)
1988
        {
1989
          bfd_mach_o_dylib_module *module = &cmd->dylib_module[i];
1990
          unsigned long v;
1991
 
1992
          if (bfd_bread ((PTR) buf, module_len, abfd) != module_len)
1993
            return -1;
1994
 
1995
          module->module_name_idx = bfd_h_get_32 (abfd, buf + 0);
1996
          module->iextdefsym = bfd_h_get_32 (abfd, buf + 4);
1997
          module->nextdefsym = bfd_h_get_32 (abfd, buf + 8);
1998
          module->irefsym = bfd_h_get_32 (abfd, buf + 12);
1999
          module->nrefsym = bfd_h_get_32 (abfd, buf + 16);
2000
          module->ilocalsym = bfd_h_get_32 (abfd, buf + 20);
2001
          module->nlocalsym = bfd_h_get_32 (abfd, buf + 24);
2002
          module->iextrel = bfd_h_get_32 (abfd, buf + 28);
2003
          module->nextrel = bfd_h_get_32 (abfd, buf + 32);
2004
          v = bfd_h_get_32 (abfd, buf +36);
2005
          module->iinit = v & 0xffff;
2006
          module->iterm = (v >> 16) & 0xffff;
2007
          v = bfd_h_get_32 (abfd, buf + 40);
2008
          module->ninit = v & 0xffff;
2009
          module->nterm = (v >> 16) & 0xffff;
2010
          if (wide)
2011
            {
2012
              module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 44);
2013
              module->objc_module_info_addr = bfd_h_get_64 (abfd, buf + 48);
2014
            }
2015
          else
2016
            {
2017
              module->objc_module_info_addr = bfd_h_get_32 (abfd, buf + 44);
2018
              module->objc_module_info_size = bfd_h_get_32 (abfd, buf + 48);
2019
            }
2020
        }
2021
    }
2022
 
2023
  if (cmd->ntoc != 0)
2024
    {
2025
      char buf[8];
2026
      unsigned int i;
2027
 
2028
      cmd->dylib_toc = bfd_alloc
2029
        (abfd, cmd->ntoc * sizeof (bfd_mach_o_dylib_table_of_content));
2030
      if (cmd->dylib_toc == NULL)
2031
        return -1;
2032
 
2033
      if (bfd_seek (abfd, cmd->tocoff, SEEK_SET) != 0)
2034
        return -1;
2035
 
2036
      for (i = 0; i < cmd->ntoc; i++)
2037
        {
2038
          bfd_mach_o_dylib_table_of_content *toc = &cmd->dylib_toc[i];
2039
 
2040
          if (bfd_bread ((PTR) buf, 8, abfd) != 8)
2041
            return -1;
2042
 
2043
          toc->symbol_index = bfd_h_get_32 (abfd, buf + 0);
2044
          toc->module_index = bfd_h_get_32 (abfd, buf + 4);
2045
        }
2046
    }
2047
 
2048
  if (cmd->nindirectsyms != 0)
2049
    {
2050
      char buf[4];
2051
      unsigned int i;
2052
 
2053
      cmd->indirect_syms = bfd_alloc
2054
        (abfd, cmd->nindirectsyms * sizeof (unsigned int));
2055
      if (cmd->indirect_syms == NULL)
2056
        return -1;
2057
 
2058
      if (bfd_seek (abfd, cmd->indirectsymoff, SEEK_SET) != 0)
2059
        return -1;
2060
 
2061
      for (i = 0; i < cmd->nindirectsyms; i++)
2062
        {
2063
          unsigned int *is = &cmd->indirect_syms[i];
2064
 
2065
          if (bfd_bread ((PTR) buf, 4, abfd) != 4)
2066
            return -1;
2067
 
2068
          *is = bfd_h_get_32 (abfd, buf + 0);
2069
        }
2070
    }
2071
 
2072
  if (cmd->nextrefsyms != 0)
2073
    {
2074
      char buf[4];
2075
      unsigned long v;
2076
      unsigned int i;
2077
 
2078
      cmd->ext_refs = bfd_alloc
2079
        (abfd, cmd->nextrefsyms * sizeof (bfd_mach_o_dylib_reference));
2080
      if (cmd->ext_refs == NULL)
2081
        return -1;
2082
 
2083
      if (bfd_seek (abfd, cmd->extrefsymoff, SEEK_SET) != 0)
2084
        return -1;
2085
 
2086
      for (i = 0; i < cmd->nextrefsyms; i++)
2087
        {
2088
          bfd_mach_o_dylib_reference *ref = &cmd->ext_refs[i];
2089
 
2090
          if (bfd_bread ((PTR) buf, 4, abfd) != 4)
2091
            return -1;
2092
 
2093
          /* Fields isym and flags are written as bit-fields, thus we need
2094
             a specific processing for endianness.  */
2095
          v = bfd_h_get_32 (abfd, buf + 0);
2096
          if (bfd_big_endian (abfd))
2097
            {
2098
              ref->isym = (v >> 8) & 0xffffff;
2099
              ref->flags = v & 0xff;
2100
            }
2101
          else
2102
            {
2103
              ref->isym = v & 0xffffff;
2104
              ref->flags = (v >> 24) & 0xff;
2105
            }
2106
        }
2107
    }
2108
 
2109
  if (mdata->dysymtab)
2110
    return -1;
2111
  mdata->dysymtab = cmd;
2112
 
2113 24 jeremybenn
  return 0;
2114
}
2115
 
2116
static int
2117
bfd_mach_o_scan_read_symtab (bfd *abfd, bfd_mach_o_load_command *command)
2118
{
2119 225 jeremybenn
  bfd_mach_o_symtab_command *symtab = &command->command.symtab;
2120
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2121 24 jeremybenn
  unsigned char buf[16];
2122
 
2123
  BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
2124
 
2125 225 jeremybenn
  if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2126
      || bfd_bread ((PTR) buf, 16, abfd) != 16)
2127 24 jeremybenn
    return -1;
2128
 
2129 225 jeremybenn
  symtab->symoff = bfd_h_get_32 (abfd, buf);
2130
  symtab->nsyms = bfd_h_get_32 (abfd, buf + 4);
2131
  symtab->stroff = bfd_h_get_32 (abfd, buf + 8);
2132
  symtab->strsize = bfd_h_get_32 (abfd, buf + 12);
2133
  symtab->symbols = NULL;
2134
  symtab->strtab = NULL;
2135 24 jeremybenn
 
2136 225 jeremybenn
  if (symtab->nsyms != 0)
2137
    abfd->flags |= HAS_SYMS;
2138 24 jeremybenn
 
2139 225 jeremybenn
  if (mdata->symtab)
2140 24 jeremybenn
    return -1;
2141 225 jeremybenn
  mdata->symtab = symtab;
2142
  return 0;
2143
}
2144 24 jeremybenn
 
2145 225 jeremybenn
static int
2146
bfd_mach_o_scan_read_uuid (bfd *abfd, bfd_mach_o_load_command *command)
2147
{
2148
  bfd_mach_o_uuid_command *cmd = &command->command.uuid;
2149
  asection *bfdsec;
2150
  char *sname;
2151
  static const char prefix[] = "LC_UUID";
2152 24 jeremybenn
 
2153 225 jeremybenn
  BFD_ASSERT (command->type == BFD_MACH_O_LC_UUID);
2154 24 jeremybenn
 
2155 225 jeremybenn
  if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2156
      || bfd_bread ((PTR) cmd->uuid, 16, abfd) != 16)
2157
    return -1;
2158
 
2159 24 jeremybenn
  sname = bfd_alloc (abfd, strlen (prefix) + 1);
2160
  if (sname == NULL)
2161
    return -1;
2162
  strcpy (sname, prefix);
2163
 
2164
  bfdsec = bfd_make_section_anyway_with_flags (abfd, sname, SEC_HAS_CONTENTS);
2165
  if (bfdsec == NULL)
2166
    return -1;
2167
 
2168
  bfdsec->vma = 0;
2169
  bfdsec->lma = 0;
2170 225 jeremybenn
  bfdsec->size = command->len - 8;
2171
  bfdsec->filepos = command->offset + 8;
2172 24 jeremybenn
  bfdsec->alignment_power = 0;
2173
 
2174 225 jeremybenn
  cmd->section = bfdsec;
2175 24 jeremybenn
 
2176
  return 0;
2177
}
2178
 
2179
static int
2180 225 jeremybenn
bfd_mach_o_scan_read_linkedit (bfd *abfd, bfd_mach_o_load_command *command)
2181 24 jeremybenn
{
2182 225 jeremybenn
  bfd_mach_o_linkedit_command *cmd = &command->command.linkedit;
2183
  char buf[8];
2184 24 jeremybenn
 
2185 225 jeremybenn
  if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2186
      || bfd_bread ((PTR) buf, 8, abfd) != 8)
2187 24 jeremybenn
    return -1;
2188
 
2189 225 jeremybenn
  cmd->dataoff = bfd_get_32 (abfd, buf + 0);
2190
  cmd->datasize = bfd_get_32 (abfd, buf + 4);
2191
  return 0;
2192
}
2193 24 jeremybenn
 
2194 225 jeremybenn
static int
2195
bfd_mach_o_scan_read_str (bfd *abfd, bfd_mach_o_load_command *command)
2196
{
2197
  bfd_mach_o_str_command *cmd = &command->command.str;
2198
  char buf[8];
2199
  unsigned long off;
2200
 
2201
  if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2202
      || bfd_bread ((PTR) buf, 4, abfd) != 4)
2203 24 jeremybenn
    return -1;
2204
 
2205 225 jeremybenn
  off = bfd_get_32 (abfd, buf + 0);
2206
  cmd->stroff = command->offset + off;
2207
  cmd->str_len = command->len - off;
2208
  cmd->str = bfd_alloc (abfd, cmd->str_len);
2209
  if (cmd->str == NULL)
2210 24 jeremybenn
    return -1;
2211 225 jeremybenn
  if (bfd_seek (abfd, cmd->stroff, SEEK_SET) != 0
2212
      || bfd_bread ((PTR) cmd->str, cmd->str_len, abfd) != cmd->str_len)
2213
    return -1;
2214
  return 0;
2215
}
2216 24 jeremybenn
 
2217 225 jeremybenn
static int
2218
bfd_mach_o_scan_read_segment (bfd *abfd,
2219
                              bfd_mach_o_load_command *command,
2220
                              unsigned int wide)
2221
{
2222
  unsigned char buf[64];
2223
  bfd_mach_o_segment_command *seg = &command->command.segment;
2224
  unsigned long i;
2225 24 jeremybenn
 
2226 225 jeremybenn
  if (wide)
2227
    {
2228
      BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
2229 24 jeremybenn
 
2230 225 jeremybenn
      if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2231
          || bfd_bread ((PTR) buf, 64, abfd) != 64)
2232
        return -1;
2233
 
2234
      memcpy (seg->segname, buf, 16);
2235
      seg->segname[16] = '\0';
2236
 
2237
      seg->vmaddr = bfd_h_get_64 (abfd, buf + 16);
2238
      seg->vmsize = bfd_h_get_64 (abfd, buf + 24);
2239
      seg->fileoff = bfd_h_get_64 (abfd, buf + 32);
2240
      seg->filesize = bfd_h_get_64 (abfd, buf + 40);
2241
      seg->maxprot = bfd_h_get_32 (abfd, buf + 48);
2242
      seg->initprot = bfd_h_get_32 (abfd, buf + 52);
2243
      seg->nsects = bfd_h_get_32 (abfd, buf + 56);
2244
      seg->flags = bfd_h_get_32 (abfd, buf + 60);
2245
    }
2246
  else
2247
    {
2248
      BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
2249
 
2250
      if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
2251
          || bfd_bread ((PTR) buf, 48, abfd) != 48)
2252
        return -1;
2253
 
2254
      memcpy (seg->segname, buf, 16);
2255
      seg->segname[16] = '\0';
2256
 
2257
      seg->vmaddr = bfd_h_get_32 (abfd, buf + 16);
2258
      seg->vmsize = bfd_h_get_32 (abfd, buf + 20);
2259
      seg->fileoff = bfd_h_get_32 (abfd, buf + 24);
2260
      seg->filesize = bfd_h_get_32 (abfd, buf +  28);
2261
      seg->maxprot = bfd_h_get_32 (abfd, buf + 32);
2262
      seg->initprot = bfd_h_get_32 (abfd, buf + 36);
2263
      seg->nsects = bfd_h_get_32 (abfd, buf + 40);
2264
      seg->flags = bfd_h_get_32 (abfd, buf + 44);
2265
    }
2266
 
2267 24 jeremybenn
  if (seg->nsects != 0)
2268
    {
2269 225 jeremybenn
      seg->sections = bfd_alloc (abfd, seg->nsects
2270
                                 * sizeof (bfd_mach_o_section));
2271 24 jeremybenn
      if (seg->sections == NULL)
2272
        return -1;
2273
 
2274
      for (i = 0; i < seg->nsects; i++)
2275
        {
2276 225 jeremybenn
          bfd_vma segoff;
2277
          if (wide)
2278
            segoff = command->offset + BFD_MACH_O_LC_SEGMENT_64_SIZE
2279
              + (i * BFD_MACH_O_SECTION_64_SIZE);
2280
          else
2281
            segoff = command->offset + BFD_MACH_O_LC_SEGMENT_SIZE
2282
              + (i * BFD_MACH_O_SECTION_SIZE);
2283 24 jeremybenn
 
2284 225 jeremybenn
          if (bfd_mach_o_scan_read_section
2285
              (abfd, &seg->sections[i], segoff, seg->initprot, wide) != 0)
2286 24 jeremybenn
            return -1;
2287
        }
2288
    }
2289
 
2290
  return 0;
2291
}
2292
 
2293
static int
2294 225 jeremybenn
bfd_mach_o_scan_read_segment_32 (bfd *abfd, bfd_mach_o_load_command *command)
2295
{
2296
  return bfd_mach_o_scan_read_segment (abfd, command, 0);
2297
}
2298
 
2299
static int
2300
bfd_mach_o_scan_read_segment_64 (bfd *abfd, bfd_mach_o_load_command *command)
2301
{
2302
  return bfd_mach_o_scan_read_segment (abfd, command, 1);
2303
}
2304
 
2305
static int
2306 24 jeremybenn
bfd_mach_o_scan_read_command (bfd *abfd, bfd_mach_o_load_command *command)
2307
{
2308
  unsigned char buf[8];
2309
 
2310 225 jeremybenn
  /* Read command type and length.  */
2311
  if (bfd_seek (abfd, command->offset, SEEK_SET) != 0
2312
      || bfd_bread ((PTR) buf, 8, abfd) != 8)
2313 24 jeremybenn
    return -1;
2314
 
2315 225 jeremybenn
  command->type = bfd_h_get_32 (abfd, buf) & ~BFD_MACH_O_LC_REQ_DYLD;
2316 24 jeremybenn
  command->type_required = (bfd_h_get_32 (abfd, buf) & BFD_MACH_O_LC_REQ_DYLD
2317 225 jeremybenn
                            ? TRUE : FALSE);
2318 24 jeremybenn
  command->len = bfd_h_get_32 (abfd, buf + 4);
2319
 
2320
  switch (command->type)
2321
    {
2322
    case BFD_MACH_O_LC_SEGMENT:
2323 225 jeremybenn
      if (bfd_mach_o_scan_read_segment_32 (abfd, command) != 0)
2324 24 jeremybenn
        return -1;
2325
      break;
2326 225 jeremybenn
    case BFD_MACH_O_LC_SEGMENT_64:
2327
      if (bfd_mach_o_scan_read_segment_64 (abfd, command) != 0)
2328
        return -1;
2329
      break;
2330 24 jeremybenn
    case BFD_MACH_O_LC_SYMTAB:
2331
      if (bfd_mach_o_scan_read_symtab (abfd, command) != 0)
2332
        return -1;
2333
      break;
2334
    case BFD_MACH_O_LC_SYMSEG:
2335
      break;
2336
    case BFD_MACH_O_LC_THREAD:
2337
    case BFD_MACH_O_LC_UNIXTHREAD:
2338
      if (bfd_mach_o_scan_read_thread (abfd, command) != 0)
2339
        return -1;
2340
      break;
2341
    case BFD_MACH_O_LC_LOAD_DYLINKER:
2342
    case BFD_MACH_O_LC_ID_DYLINKER:
2343
      if (bfd_mach_o_scan_read_dylinker (abfd, command) != 0)
2344
        return -1;
2345
      break;
2346
    case BFD_MACH_O_LC_LOAD_DYLIB:
2347
    case BFD_MACH_O_LC_ID_DYLIB:
2348
    case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
2349 225 jeremybenn
    case BFD_MACH_O_LC_REEXPORT_DYLIB:
2350 24 jeremybenn
      if (bfd_mach_o_scan_read_dylib (abfd, command) != 0)
2351
        return -1;
2352
      break;
2353
    case BFD_MACH_O_LC_PREBOUND_DYLIB:
2354
      if (bfd_mach_o_scan_read_prebound_dylib (abfd, command) != 0)
2355
        return -1;
2356
      break;
2357
    case BFD_MACH_O_LC_LOADFVMLIB:
2358
    case BFD_MACH_O_LC_IDFVMLIB:
2359
    case BFD_MACH_O_LC_IDENT:
2360
    case BFD_MACH_O_LC_FVMFILE:
2361
    case BFD_MACH_O_LC_PREPAGE:
2362
    case BFD_MACH_O_LC_ROUTINES:
2363 225 jeremybenn
      break;
2364 24 jeremybenn
    case BFD_MACH_O_LC_SUB_FRAMEWORK:
2365 225 jeremybenn
    case BFD_MACH_O_LC_SUB_UMBRELLA:
2366
    case BFD_MACH_O_LC_SUB_LIBRARY:
2367
    case BFD_MACH_O_LC_SUB_CLIENT:
2368
      if (bfd_mach_o_scan_read_str (abfd, command) != 0)
2369
        return -1;
2370 24 jeremybenn
      break;
2371
    case BFD_MACH_O_LC_DYSYMTAB:
2372
      if (bfd_mach_o_scan_read_dysymtab (abfd, command) != 0)
2373
        return -1;
2374
      break;
2375
    case BFD_MACH_O_LC_TWOLEVEL_HINTS:
2376
    case BFD_MACH_O_LC_PREBIND_CKSUM:
2377
      break;
2378 225 jeremybenn
    case BFD_MACH_O_LC_UUID:
2379
      if (bfd_mach_o_scan_read_uuid (abfd, command) != 0)
2380
        return -1;
2381
      break;
2382
    case BFD_MACH_O_LC_CODE_SIGNATURE:
2383
    case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
2384
      if (bfd_mach_o_scan_read_linkedit (abfd, command) != 0)
2385
        return -1;
2386
      break;
2387 24 jeremybenn
    default:
2388
      fprintf (stderr, "unable to read unknown load command 0x%lx\n",
2389
               (unsigned long) command->type);
2390
      break;
2391
    }
2392
 
2393
  return 0;
2394
}
2395
 
2396
static void
2397
bfd_mach_o_flatten_sections (bfd *abfd)
2398
{
2399 225 jeremybenn
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2400 24 jeremybenn
  long csect = 0;
2401
  unsigned long i, j;
2402
 
2403 225 jeremybenn
  /* Count total number of sections.  */
2404 24 jeremybenn
  mdata->nsects = 0;
2405
 
2406
  for (i = 0; i < mdata->header.ncmds; i++)
2407
    {
2408 225 jeremybenn
      if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
2409
          || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
2410 24 jeremybenn
        {
2411
          bfd_mach_o_segment_command *seg;
2412
 
2413
          seg = &mdata->commands[i].command.segment;
2414
          mdata->nsects += seg->nsects;
2415
        }
2416
    }
2417
 
2418 225 jeremybenn
  /* Allocate sections array.  */
2419 24 jeremybenn
  mdata->sections = bfd_alloc (abfd,
2420
                               mdata->nsects * sizeof (bfd_mach_o_section *));
2421 225 jeremybenn
 
2422
  /* Fill the array.  */
2423 24 jeremybenn
  csect = 0;
2424
 
2425
  for (i = 0; i < mdata->header.ncmds; i++)
2426
    {
2427 225 jeremybenn
      if (mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT
2428
          || mdata->commands[i].type == BFD_MACH_O_LC_SEGMENT_64)
2429 24 jeremybenn
        {
2430
          bfd_mach_o_segment_command *seg;
2431
 
2432
          seg = &mdata->commands[i].command.segment;
2433
          BFD_ASSERT (csect + seg->nsects <= mdata->nsects);
2434
 
2435
          for (j = 0; j < seg->nsects; j++)
2436
            mdata->sections[csect++] = &seg->sections[j];
2437
        }
2438
    }
2439
}
2440
 
2441
int
2442
bfd_mach_o_scan_start_address (bfd *abfd)
2443
{
2444 225 jeremybenn
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
2445 24 jeremybenn
  bfd_mach_o_thread_command *cmd = NULL;
2446
  unsigned long i;
2447
 
2448
  for (i = 0; i < mdata->header.ncmds; i++)
2449
    {
2450
      if ((mdata->commands[i].type == BFD_MACH_O_LC_THREAD) ||
2451
          (mdata->commands[i].type == BFD_MACH_O_LC_UNIXTHREAD))
2452
        {
2453
          if (cmd == NULL)
2454
            cmd = &mdata->commands[i].command.thread;
2455
          else
2456
            return 0;
2457
        }
2458
    }
2459
 
2460
  if (cmd == NULL)
2461
    return 0;
2462
 
2463
  for (i = 0; i < cmd->nflavours; i++)
2464
    {
2465
      if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_I386)
2466
          && (cmd->flavours[i].flavour
2467 225 jeremybenn
              == (unsigned long) BFD_MACH_O_x86_THREAD_STATE32))
2468 24 jeremybenn
        {
2469
          unsigned char buf[4];
2470
 
2471 225 jeremybenn
          if (bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET) != 0
2472
              || bfd_bread (buf, 4, abfd) != 4)
2473 24 jeremybenn
            return -1;
2474
 
2475
          abfd->start_address = bfd_h_get_32 (abfd, buf);
2476
        }
2477
      else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC)
2478
               && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE))
2479
        {
2480
          unsigned char buf[4];
2481
 
2482 225 jeremybenn
          if (bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET) != 0
2483
              || bfd_bread (buf, 4, abfd) != 4)
2484 24 jeremybenn
            return -1;
2485
 
2486
          abfd->start_address = bfd_h_get_32 (abfd, buf);
2487
        }
2488 225 jeremybenn
      else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_POWERPC_64)
2489
               && (cmd->flavours[i].flavour == BFD_MACH_O_PPC_THREAD_STATE64))
2490
        {
2491
          unsigned char buf[8];
2492
 
2493
          if (bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET) != 0
2494
              || bfd_bread (buf, 8, abfd) != 8)
2495
            return -1;
2496
 
2497
          abfd->start_address = bfd_h_get_64 (abfd, buf);
2498
        }
2499
      else if ((mdata->header.cputype == BFD_MACH_O_CPU_TYPE_X86_64)
2500
               && (cmd->flavours[i].flavour == BFD_MACH_O_x86_THREAD_STATE64))
2501
        {
2502
          unsigned char buf[8];
2503
 
2504
          if (bfd_seek (abfd, cmd->flavours[i].offset + (16 * 8), SEEK_SET) != 0
2505
              || bfd_bread (buf, 8, abfd) != 8)
2506
            return -1;
2507
 
2508
          abfd->start_address = bfd_h_get_64 (abfd, buf);
2509
        }
2510 24 jeremybenn
    }
2511
 
2512
  return 0;
2513
}
2514
 
2515
int
2516
bfd_mach_o_scan (bfd *abfd,
2517
                 bfd_mach_o_header *header,
2518
                 bfd_mach_o_data_struct *mdata)
2519
{
2520
  unsigned int i;
2521
  enum bfd_architecture cputype;
2522
  unsigned long cpusubtype;
2523 225 jeremybenn
  unsigned int hdrsize;
2524 24 jeremybenn
 
2525 225 jeremybenn
  hdrsize = mach_o_wide_p (header) ?
2526
    BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;
2527
 
2528 24 jeremybenn
  mdata->header = *header;
2529
 
2530 225 jeremybenn
  abfd->flags = abfd->flags & BFD_IN_MEMORY;
2531
  switch (header->filetype)
2532
    {
2533
    case BFD_MACH_O_MH_OBJECT:
2534
      abfd->flags |= HAS_RELOC;
2535
      break;
2536
    case BFD_MACH_O_MH_EXECUTE:
2537
      abfd->flags |= EXEC_P;
2538
      break;
2539
    case BFD_MACH_O_MH_DYLIB:
2540
    case BFD_MACH_O_MH_BUNDLE:
2541
      abfd->flags |= DYNAMIC;
2542
      break;
2543
    }
2544
 
2545 24 jeremybenn
  abfd->tdata.mach_o_data = mdata;
2546
 
2547
  bfd_mach_o_convert_architecture (header->cputype, header->cpusubtype,
2548
                                   &cputype, &cpusubtype);
2549
  if (cputype == bfd_arch_unknown)
2550
    {
2551
      fprintf (stderr, "bfd_mach_o_scan: unknown architecture 0x%lx/0x%lx\n",
2552
               header->cputype, header->cpusubtype);
2553
      return -1;
2554
    }
2555
 
2556
  bfd_set_arch_mach (abfd, cputype, cpusubtype);
2557
 
2558
  if (header->ncmds != 0)
2559
    {
2560 225 jeremybenn
      mdata->commands = bfd_alloc
2561
        (abfd, header->ncmds * sizeof (bfd_mach_o_load_command));
2562 24 jeremybenn
      if (mdata->commands == NULL)
2563
        return -1;
2564
 
2565
      for (i = 0; i < header->ncmds; i++)
2566
        {
2567
          bfd_mach_o_load_command *cur = &mdata->commands[i];
2568
 
2569
          if (i == 0)
2570 225 jeremybenn
            cur->offset = hdrsize;
2571 24 jeremybenn
          else
2572
            {
2573
              bfd_mach_o_load_command *prev = &mdata->commands[i - 1];
2574
              cur->offset = prev->offset + prev->len;
2575
            }
2576
 
2577
          if (bfd_mach_o_scan_read_command (abfd, cur) < 0)
2578
            return -1;
2579
        }
2580
    }
2581
 
2582
  if (bfd_mach_o_scan_start_address (abfd) < 0)
2583
    return -1;
2584
 
2585
  bfd_mach_o_flatten_sections (abfd);
2586
  return 0;
2587
}
2588
 
2589
bfd_boolean
2590 225 jeremybenn
bfd_mach_o_mkobject_init (bfd *abfd)
2591 24 jeremybenn
{
2592
  bfd_mach_o_data_struct *mdata = NULL;
2593
 
2594
  mdata = bfd_alloc (abfd, sizeof (bfd_mach_o_data_struct));
2595
  if (mdata == NULL)
2596
    return FALSE;
2597
  abfd->tdata.mach_o_data = mdata;
2598
 
2599
  mdata->header.magic = 0;
2600
  mdata->header.cputype = 0;
2601
  mdata->header.cpusubtype = 0;
2602
  mdata->header.filetype = 0;
2603
  mdata->header.ncmds = 0;
2604
  mdata->header.sizeofcmds = 0;
2605
  mdata->header.flags = 0;
2606
  mdata->header.byteorder = BFD_ENDIAN_UNKNOWN;
2607
  mdata->commands = NULL;
2608
  mdata->nsects = 0;
2609
  mdata->sections = NULL;
2610
 
2611
  return TRUE;
2612
}
2613
 
2614
const bfd_target *
2615 225 jeremybenn
bfd_mach_o_header_p (bfd *abfd,
2616
                     bfd_mach_o_filetype filetype,
2617
                     bfd_mach_o_cpu_type cputype)
2618 24 jeremybenn
{
2619
  struct bfd_preserve preserve;
2620
  bfd_mach_o_header header;
2621
 
2622
  preserve.marker = NULL;
2623 225 jeremybenn
  if (!bfd_mach_o_read_header (abfd, &header))
2624 24 jeremybenn
    goto wrong;
2625
 
2626
  if (! (header.byteorder == BFD_ENDIAN_BIG
2627
         || header.byteorder == BFD_ENDIAN_LITTLE))
2628
    {
2629
      fprintf (stderr, "unknown header byte-order value 0x%lx\n",
2630 225 jeremybenn
               (unsigned long) header.byteorder);
2631 24 jeremybenn
      goto wrong;
2632
    }
2633
 
2634
  if (! ((header.byteorder == BFD_ENDIAN_BIG
2635
          && abfd->xvec->byteorder == BFD_ENDIAN_BIG
2636
          && abfd->xvec->header_byteorder == BFD_ENDIAN_BIG)
2637
         || (header.byteorder == BFD_ENDIAN_LITTLE
2638
             && abfd->xvec->byteorder == BFD_ENDIAN_LITTLE
2639
             && abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE)))
2640
    goto wrong;
2641
 
2642 225 jeremybenn
  /* Check cputype and filetype.
2643
     In case of wildcard, do not accept magics that are handled by existing
2644
     targets.  */
2645
  if (cputype)
2646
    {
2647
      if (header.cputype != cputype)
2648
        goto wrong;
2649
    }
2650
  else
2651
    {
2652
      switch (header.cputype)
2653
        {
2654
        case BFD_MACH_O_CPU_TYPE_I386:
2655
          /* Handled by mach-o-i386 */
2656
          goto wrong;
2657
        default:
2658
          break;
2659
        }
2660
    }
2661
  if (filetype)
2662
    {
2663
      if (header.filetype != filetype)
2664
        goto wrong;
2665
    }
2666
  else
2667
    {
2668
      switch (header.filetype)
2669
        {
2670
        case BFD_MACH_O_MH_CORE:
2671
          /* Handled by core_p */
2672
          goto wrong;
2673
        default:
2674
          break;
2675
        }
2676
    }
2677
 
2678 24 jeremybenn
  preserve.marker = bfd_zalloc (abfd, sizeof (bfd_mach_o_data_struct));
2679
  if (preserve.marker == NULL
2680
      || !bfd_preserve_save (abfd, &preserve))
2681
    goto fail;
2682
 
2683
  if (bfd_mach_o_scan (abfd, &header,
2684
                       (bfd_mach_o_data_struct *) preserve.marker) != 0)
2685
    goto wrong;
2686
 
2687
  bfd_preserve_finish (abfd, &preserve);
2688
  return abfd->xvec;
2689
 
2690
 wrong:
2691
  bfd_set_error (bfd_error_wrong_format);
2692
 
2693
 fail:
2694
  if (preserve.marker != NULL)
2695
    bfd_preserve_restore (abfd, &preserve);
2696
  return NULL;
2697
}
2698
 
2699 225 jeremybenn
static const bfd_target *
2700
bfd_mach_o_gen_object_p (bfd *abfd)
2701 24 jeremybenn
{
2702 225 jeremybenn
  return bfd_mach_o_header_p (abfd, 0, 0);
2703
}
2704 24 jeremybenn
 
2705 225 jeremybenn
static const bfd_target *
2706
bfd_mach_o_gen_core_p (bfd *abfd)
2707
{
2708
  return bfd_mach_o_header_p (abfd, BFD_MACH_O_MH_CORE, 0);
2709 24 jeremybenn
}
2710
 
2711
typedef struct mach_o_fat_archentry
2712
{
2713
  unsigned long cputype;
2714
  unsigned long cpusubtype;
2715
  unsigned long offset;
2716
  unsigned long size;
2717
  unsigned long align;
2718
} mach_o_fat_archentry;
2719
 
2720
typedef struct mach_o_fat_data_struct
2721
{
2722
  unsigned long magic;
2723
  unsigned long nfat_arch;
2724
  mach_o_fat_archentry *archentries;
2725
} mach_o_fat_data_struct;
2726
 
2727
const bfd_target *
2728
bfd_mach_o_archive_p (bfd *abfd)
2729
{
2730
  mach_o_fat_data_struct *adata = NULL;
2731
  unsigned char buf[20];
2732
  unsigned long i;
2733
 
2734 225 jeremybenn
  if (bfd_seek (abfd, 0, SEEK_SET) != 0
2735
      || bfd_bread ((PTR) buf, 8, abfd) != 8)
2736 24 jeremybenn
    goto error;
2737
 
2738
  adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
2739
  if (adata == NULL)
2740
    goto error;
2741
 
2742
  adata->magic = bfd_getb32 (buf);
2743
  adata->nfat_arch = bfd_getb32 (buf + 4);
2744
  if (adata->magic != 0xcafebabe)
2745
    goto error;
2746 225 jeremybenn
  /* Avoid matching Java bytecode files, which have the same magic number.
2747
     In the Java bytecode file format this field contains the JVM version,
2748
     which starts at 43.0.  */
2749
  if (adata->nfat_arch > 30)
2750
    goto error;
2751 24 jeremybenn
 
2752 225 jeremybenn
  adata->archentries =
2753 24 jeremybenn
    bfd_alloc (abfd, adata->nfat_arch * sizeof (mach_o_fat_archentry));
2754
  if (adata->archentries == NULL)
2755
    goto error;
2756
 
2757
  for (i = 0; i < adata->nfat_arch; i++)
2758
    {
2759 225 jeremybenn
      if (bfd_seek (abfd, 8 + 20 * i, SEEK_SET) != 0
2760
          || bfd_bread ((PTR) buf, 20, abfd) != 20)
2761 24 jeremybenn
        goto error;
2762
      adata->archentries[i].cputype = bfd_getb32 (buf);
2763
      adata->archentries[i].cpusubtype = bfd_getb32 (buf + 4);
2764
      adata->archentries[i].offset = bfd_getb32 (buf + 8);
2765
      adata->archentries[i].size = bfd_getb32 (buf + 12);
2766
      adata->archentries[i].align = bfd_getb32 (buf + 16);
2767
    }
2768
 
2769
  abfd->tdata.mach_o_fat_data = adata;
2770
  return abfd->xvec;
2771
 
2772
 error:
2773
  if (adata != NULL)
2774
    bfd_release (abfd, adata);
2775
  bfd_set_error (bfd_error_wrong_format);
2776
  return NULL;
2777
}
2778
 
2779
bfd *
2780
bfd_mach_o_openr_next_archived_file (bfd *archive, bfd *prev)
2781
{
2782
  mach_o_fat_data_struct *adata;
2783
  mach_o_fat_archentry *entry = NULL;
2784
  unsigned long i;
2785 225 jeremybenn
  bfd *nbfd;
2786
  enum bfd_architecture arch_type;
2787
  unsigned long arch_subtype;
2788 24 jeremybenn
 
2789
  adata = (mach_o_fat_data_struct *) archive->tdata.mach_o_fat_data;
2790
  BFD_ASSERT (adata != NULL);
2791
 
2792
  /* Find index of previous entry.  */
2793
  if (prev == NULL)
2794
    i = 0;       /* Start at first one.  */
2795
  else
2796
    {
2797
      for (i = 0; i < adata->nfat_arch; i++)
2798
        {
2799 225 jeremybenn
          if (adata->archentries[i].offset == prev->origin)
2800 24 jeremybenn
            break;
2801
        }
2802
 
2803
      if (i == adata->nfat_arch)
2804
        {
2805
          /* Not found.  */
2806
          bfd_set_error (bfd_error_bad_value);
2807
          return NULL;
2808
        }
2809
    i++;        /* Get next entry.  */
2810
  }
2811
 
2812
  if (i >= adata->nfat_arch)
2813
    {
2814
      bfd_set_error (bfd_error_no_more_archived_files);
2815
      return NULL;
2816
    }
2817
 
2818
  entry = &adata->archentries[i];
2819 225 jeremybenn
  nbfd = _bfd_new_bfd_contained_in (archive);
2820
  if (nbfd == NULL)
2821
    return NULL;
2822
 
2823
  nbfd->origin = entry->offset;
2824
 
2825
  bfd_mach_o_convert_architecture (entry->cputype, entry->cpusubtype,
2826
                                   &arch_type, &arch_subtype);
2827
  /* Create the member filename.
2828
     Use FILENAME:ARCH_NAME.  */
2829
  {
2830
    char *s = NULL;
2831
    const char *arch_name;
2832
    size_t arch_file_len = strlen (bfd_get_filename (archive));
2833
 
2834
    arch_name = bfd_printable_arch_mach (arch_type, arch_subtype);
2835
    s = bfd_malloc (arch_file_len + 1 + strlen (arch_name) + 1);
2836
    if (s == NULL)
2837
      return NULL;
2838
    memcpy (s, bfd_get_filename (archive), arch_file_len);
2839
    s[arch_file_len] = ':';
2840
    strcpy (s + arch_file_len + 1, arch_name);
2841
    nbfd->filename = s;
2842
  }
2843
  nbfd->iostream = NULL;
2844
  bfd_set_arch_mach (nbfd, arch_type, arch_subtype);
2845
 
2846
  return nbfd;
2847
}
2848
 
2849
/* If ABFD format is FORMAT and architecture is ARCH, return it.
2850
   If ABFD is a fat image containing a member that corresponds to FORMAT
2851
   and ARCH, returns it.
2852
   In other case, returns NULL.
2853
   This function allows transparent uses of fat images.  */
2854
bfd *
2855
bfd_mach_o_fat_extract (bfd *abfd,
2856
                        bfd_format format,
2857
                        const bfd_arch_info_type *arch)
2858
{
2859
  bfd *res;
2860
  mach_o_fat_data_struct *adata;
2861
  unsigned int i;
2862
 
2863
  if (bfd_check_format (abfd, format))
2864 24 jeremybenn
    {
2865 225 jeremybenn
      if (bfd_get_arch_info (abfd) == arch)
2866
        return abfd;
2867
      return NULL;
2868
    }
2869
  if (!bfd_check_format (abfd, bfd_archive)
2870
      || abfd->xvec != &mach_o_fat_vec)
2871
    return NULL;
2872 24 jeremybenn
 
2873 225 jeremybenn
  /* This is a Mach-O fat image.  */
2874
  adata = (mach_o_fat_data_struct *) abfd->tdata.mach_o_fat_data;
2875
  BFD_ASSERT (adata != NULL);
2876
 
2877
  for (i = 0; i < adata->nfat_arch; i++)
2878
    {
2879
      struct mach_o_fat_archentry *e = &adata->archentries[i];
2880
      enum bfd_architecture cpu_type;
2881
      unsigned long cpu_subtype;
2882
 
2883
      bfd_mach_o_convert_architecture (e->cputype, e->cpusubtype,
2884
                                       &cpu_type, &cpu_subtype);
2885
      if (cpu_type != arch->arch || cpu_subtype != arch->mach)
2886
        continue;
2887
 
2888
      /* The architecture is found.  */
2889
      res = _bfd_new_bfd_contained_in (abfd);
2890
      if (res == NULL)
2891 24 jeremybenn
        return NULL;
2892
 
2893 225 jeremybenn
      res->origin = e->offset;
2894
 
2895
      res->filename = strdup (abfd->filename);
2896
      res->iostream = NULL;
2897
 
2898
      if (bfd_check_format (res, format))
2899
        {
2900
          BFD_ASSERT (bfd_get_arch_info (res) == arch);
2901
          return res;
2902
        }
2903
      bfd_close (res);
2904
      return NULL;
2905 24 jeremybenn
    }
2906
 
2907 225 jeremybenn
  return NULL;
2908 24 jeremybenn
}
2909
 
2910
int
2911
bfd_mach_o_lookup_section (bfd *abfd,
2912
                           asection *section,
2913
                           bfd_mach_o_load_command **mcommand,
2914
                           bfd_mach_o_section **msection)
2915
{
2916 225 jeremybenn
  struct mach_o_data_struct *md = bfd_mach_o_get_data (abfd);
2917 24 jeremybenn
  unsigned int i, j, num;
2918
 
2919
  bfd_mach_o_load_command *ncmd = NULL;
2920
  bfd_mach_o_section *nsect = NULL;
2921
 
2922
  BFD_ASSERT (mcommand != NULL);
2923
  BFD_ASSERT (msection != NULL);
2924
 
2925
  num = 0;
2926
  for (i = 0; i < md->header.ncmds; i++)
2927
    {
2928
      struct bfd_mach_o_load_command *cmd = &md->commands[i];
2929
      struct bfd_mach_o_segment_command *seg = NULL;
2930
 
2931 225 jeremybenn
      if (cmd->type != BFD_MACH_O_LC_SEGMENT
2932
          || cmd->type != BFD_MACH_O_LC_SEGMENT_64)
2933 24 jeremybenn
        continue;
2934
      seg = &cmd->command.segment;
2935
 
2936
      for (j = 0; j < seg->nsects; j++)
2937
        {
2938
          struct bfd_mach_o_section *sect = &seg->sections[j];
2939
 
2940
          if (sect->bfdsection == section)
2941
            {
2942
              if (num == 0)
2943 225 jeremybenn
                {
2944
                  nsect = sect;
2945
                  ncmd = cmd;
2946
                }
2947 24 jeremybenn
              num++;
2948
            }
2949
        }
2950
    }
2951
 
2952
  *mcommand = ncmd;
2953
  *msection = nsect;
2954
  return num;
2955
}
2956
 
2957
int
2958
bfd_mach_o_lookup_command (bfd *abfd,
2959
                           bfd_mach_o_load_command_type type,
2960
                           bfd_mach_o_load_command **mcommand)
2961
{
2962 225 jeremybenn
  struct mach_o_data_struct *md = bfd_mach_o_get_data (abfd);
2963 24 jeremybenn
  bfd_mach_o_load_command *ncmd = NULL;
2964
  unsigned int i, num;
2965
 
2966
  BFD_ASSERT (md != NULL);
2967
  BFD_ASSERT (mcommand != NULL);
2968
 
2969
  num = 0;
2970
  for (i = 0; i < md->header.ncmds; i++)
2971
    {
2972
      struct bfd_mach_o_load_command *cmd = &md->commands[i];
2973
 
2974
      if (cmd->type != type)
2975
        continue;
2976
 
2977
      if (num == 0)
2978
        ncmd = cmd;
2979
      num++;
2980
    }
2981
 
2982
  *mcommand = ncmd;
2983
  return num;
2984
}
2985
 
2986
unsigned long
2987
bfd_mach_o_stack_addr (enum bfd_mach_o_cpu_type type)
2988
{
2989
  switch (type)
2990
    {
2991
    case BFD_MACH_O_CPU_TYPE_MC680x0:
2992
      return 0x04000000;
2993
    case BFD_MACH_O_CPU_TYPE_MC88000:
2994
      return 0xffffe000;
2995
    case BFD_MACH_O_CPU_TYPE_POWERPC:
2996
      return 0xc0000000;
2997
    case BFD_MACH_O_CPU_TYPE_I386:
2998
      return 0xc0000000;
2999
    case BFD_MACH_O_CPU_TYPE_SPARC:
3000
      return 0xf0000000;
3001
    case BFD_MACH_O_CPU_TYPE_I860:
3002
      return 0;
3003
    case BFD_MACH_O_CPU_TYPE_HPPA:
3004
      return 0xc0000000 - 0x04000000;
3005
    default:
3006
      return 0;
3007
    }
3008
}
3009
 
3010 225 jeremybenn
typedef struct bfd_mach_o_xlat_name
3011
{
3012
  const char *name;
3013
  unsigned long val;
3014
}
3015
bfd_mach_o_xlat_name;
3016
 
3017
static void
3018
bfd_mach_o_print_flags (const bfd_mach_o_xlat_name *table,
3019
                        unsigned long val,
3020
                        FILE *file)
3021
{
3022
  int first = 1;
3023
 
3024
  for (; table->name; table++)
3025
    {
3026
      if (table->val & val)
3027
        {
3028
          if (!first)
3029
            fprintf (file, "+");
3030
          fprintf (file, "%s", table->name);
3031
          val &= ~table->val;
3032
          first = 0;
3033
        }
3034
    }
3035
  if (val)
3036
    {
3037
      if (!first)
3038
        fprintf (file, "+");
3039
      fprintf (file, "0x%lx", val);
3040
      return;
3041
    }
3042
  if (first)
3043
    fprintf (file, "-");
3044
}
3045
 
3046
static const char *
3047
bfd_mach_o_get_name (const bfd_mach_o_xlat_name *table, unsigned long val)
3048
{
3049
  for (; table->name; table++)
3050
    if (table->val == val)
3051
      return table->name;
3052
  return "*UNKNOWN*";
3053
}
3054
 
3055
static bfd_mach_o_xlat_name bfd_mach_o_cpu_name[] =
3056
{
3057
  { "vax", BFD_MACH_O_CPU_TYPE_VAX},
3058
  { "mc680x0", BFD_MACH_O_CPU_TYPE_MC680x0},
3059
  { "i386", BFD_MACH_O_CPU_TYPE_I386},
3060
  { "mips", BFD_MACH_O_CPU_TYPE_MIPS},
3061
  { "mc98000", BFD_MACH_O_CPU_TYPE_MC98000},
3062
  { "hppa", BFD_MACH_O_CPU_TYPE_HPPA},
3063
  { "arm", BFD_MACH_O_CPU_TYPE_ARM},
3064
  { "mc88000", BFD_MACH_O_CPU_TYPE_MC88000},
3065
  { "sparc", BFD_MACH_O_CPU_TYPE_SPARC},
3066
  { "i860", BFD_MACH_O_CPU_TYPE_I860},
3067
  { "alpha", BFD_MACH_O_CPU_TYPE_ALPHA},
3068
  { "powerpc", BFD_MACH_O_CPU_TYPE_POWERPC},
3069
  { "powerpc_64", BFD_MACH_O_CPU_TYPE_POWERPC_64},
3070
  { "x86_64", BFD_MACH_O_CPU_TYPE_X86_64},
3071
  { NULL, 0}
3072
};
3073
 
3074
static bfd_mach_o_xlat_name bfd_mach_o_filetype_name[] =
3075
{
3076
  { "object", BFD_MACH_O_MH_OBJECT},
3077
  { "execute", BFD_MACH_O_MH_EXECUTE},
3078
  { "fvmlib", BFD_MACH_O_MH_FVMLIB},
3079
  { "core", BFD_MACH_O_MH_CORE},
3080
  { "preload", BFD_MACH_O_MH_PRELOAD},
3081
  { "dylib", BFD_MACH_O_MH_DYLIB},
3082
  { "dylinker", BFD_MACH_O_MH_DYLINKER},
3083
  { "bundle", BFD_MACH_O_MH_BUNDLE},
3084
  { NULL, 0}
3085
};
3086
 
3087
static bfd_mach_o_xlat_name bfd_mach_o_header_flags_name[] =
3088
{
3089
  { "noundefs", BFD_MACH_O_MH_NOUNDEFS },
3090
  { "incrlink", BFD_MACH_O_MH_INCRLINK },
3091
  { "dyldlink", BFD_MACH_O_MH_DYLDLINK },
3092
  { "bindatload", BFD_MACH_O_MH_BINDATLOAD },
3093
  { "prebound", BFD_MACH_O_MH_PREBOUND },
3094
  { "split_segs", BFD_MACH_O_MH_SPLIT_SEGS },
3095
  { "lazy_init", BFD_MACH_O_MH_LAZY_INIT },
3096
  { "twolevel", BFD_MACH_O_MH_TWOLEVEL },
3097
  { "force_flat", BFD_MACH_O_MH_FORCE_FLAT },
3098
  { "nomultidefs", BFD_MACH_O_MH_NOMULTIDEFS },
3099
  { "nofixprebinding", BFD_MACH_O_MH_NOFIXPREBINDING },
3100
  { "prebindable", BFD_MACH_O_MH_PREBINDABLE },
3101
  { "allmodsbound", BFD_MACH_O_MH_ALLMODSBOUND },
3102
  { "subsections_via_symbols", BFD_MACH_O_MH_SUBSECTIONS_VIA_SYMBOLS },
3103
  { "canonical", BFD_MACH_O_MH_CANONICAL },
3104
  { "weak_defines", BFD_MACH_O_MH_WEAK_DEFINES },
3105
  { "binds_to_weak", BFD_MACH_O_MH_BINDS_TO_WEAK },
3106
  { "allow_stack_execution", BFD_MACH_O_MH_ALLOW_STACK_EXECUTION },
3107
  { "root_safe", BFD_MACH_O_MH_ROOT_SAFE },
3108
  { "setuid_safe", BFD_MACH_O_MH_SETUID_SAFE },
3109
  { "no_reexported_dylibs", BFD_MACH_O_MH_NO_REEXPORTED_DYLIBS },
3110
  { "pie", BFD_MACH_O_MH_PIE },
3111
  { NULL, 0}
3112
};
3113
 
3114
static bfd_mach_o_xlat_name bfd_mach_o_section_type_name[] =
3115
{
3116
  { "regular", BFD_MACH_O_S_REGULAR},
3117
  { "zerofill", BFD_MACH_O_S_ZEROFILL},
3118
  { "cstring_literals", BFD_MACH_O_S_CSTRING_LITERALS},
3119
  { "4byte_literals", BFD_MACH_O_S_4BYTE_LITERALS},
3120
  { "8byte_literals", BFD_MACH_O_S_8BYTE_LITERALS},
3121
  { "literal_pointers", BFD_MACH_O_S_LITERAL_POINTERS},
3122
  { "non_lazy_symbol_pointers", BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS},
3123
  { "lazy_symbol_pointers", BFD_MACH_O_S_LAZY_SYMBOL_POINTERS},
3124
  { "symbol_stubs", BFD_MACH_O_S_SYMBOL_STUBS},
3125
  { "mod_init_func_pointers", BFD_MACH_O_S_MOD_INIT_FUNC_POINTERS},
3126
  { "mod_fini_func_pointers", BFD_MACH_O_S_MOD_FINI_FUNC_POINTERS},
3127
  { "coalesced", BFD_MACH_O_S_COALESCED},
3128
  { "gb_zerofill", BFD_MACH_O_S_GB_ZEROFILL},
3129
  { "interposing", BFD_MACH_O_S_INTERPOSING},
3130
  { "16byte_literals", BFD_MACH_O_S_16BYTE_LITERALS},
3131
  { "dtrace_dof", BFD_MACH_O_S_DTRACE_DOF},
3132
  { "lazy_dylib_symbol_pointers", BFD_MACH_O_S_LAZY_DYLIB_SYMBOL_POINTERS},
3133
  { NULL, 0}
3134
};
3135
 
3136
static bfd_mach_o_xlat_name bfd_mach_o_section_attribute_name[] =
3137
{
3138
  { "loc_reloc", BFD_MACH_O_S_ATTR_LOC_RELOC },
3139
  { "ext_reloc", BFD_MACH_O_S_ATTR_EXT_RELOC },
3140
  { "some_instructions", BFD_MACH_O_S_ATTR_SOME_INSTRUCTIONS },
3141
  { "debug", BFD_MACH_O_S_ATTR_DEBUG },
3142
  { "modifying_code", BFD_MACH_O_S_SELF_MODIFYING_CODE },
3143
  { "live_support", BFD_MACH_O_S_ATTR_LIVE_SUPPORT },
3144
  { "no_dead_strip", BFD_MACH_O_S_ATTR_NO_DEAD_STRIP },
3145
  { "strip_static_syms", BFD_MACH_O_S_ATTR_STRIP_STATIC_SYMS },
3146
  { "no_toc", BFD_MACH_O_S_ATTR_NO_TOC },
3147
  { "pure_instructions", BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS },
3148
  { NULL, 0}
3149
};
3150
 
3151
static bfd_mach_o_xlat_name bfd_mach_o_load_command_name[] =
3152
{
3153
  { "segment", BFD_MACH_O_LC_SEGMENT},
3154
  { "symtab", BFD_MACH_O_LC_SYMTAB},
3155
  { "symseg", BFD_MACH_O_LC_SYMSEG},
3156
  { "thread", BFD_MACH_O_LC_THREAD},
3157
  { "unixthread", BFD_MACH_O_LC_UNIXTHREAD},
3158
  { "loadfvmlib", BFD_MACH_O_LC_LOADFVMLIB},
3159
  { "idfvmlib", BFD_MACH_O_LC_IDFVMLIB},
3160
  { "ident", BFD_MACH_O_LC_IDENT},
3161
  { "fvmfile", BFD_MACH_O_LC_FVMFILE},
3162
  { "prepage", BFD_MACH_O_LC_PREPAGE},
3163
  { "dysymtab", BFD_MACH_O_LC_DYSYMTAB},
3164
  { "load_dylib", BFD_MACH_O_LC_LOAD_DYLIB},
3165
  { "id_dylib", BFD_MACH_O_LC_ID_DYLIB},
3166
  { "load_dylinker", BFD_MACH_O_LC_LOAD_DYLINKER},
3167
  { "id_dylinker", BFD_MACH_O_LC_ID_DYLINKER},
3168
  { "prebound_dylib", BFD_MACH_O_LC_PREBOUND_DYLIB},
3169
  { "routines", BFD_MACH_O_LC_ROUTINES},
3170
  { "sub_framework", BFD_MACH_O_LC_SUB_FRAMEWORK},
3171
  { "sub_umbrella", BFD_MACH_O_LC_SUB_UMBRELLA},
3172
  { "sub_client", BFD_MACH_O_LC_SUB_CLIENT},
3173
  { "sub_library", BFD_MACH_O_LC_SUB_LIBRARY},
3174
  { "twolevel_hints", BFD_MACH_O_LC_TWOLEVEL_HINTS},
3175
  { "prebind_cksum", BFD_MACH_O_LC_PREBIND_CKSUM},
3176
  { "load_weak_dylib", BFD_MACH_O_LC_LOAD_WEAK_DYLIB},
3177
  { "segment_64", BFD_MACH_O_LC_SEGMENT_64},
3178
  { "routines_64", BFD_MACH_O_LC_ROUTINES_64},
3179
  { "uuid", BFD_MACH_O_LC_UUID},
3180
  { "rpath", BFD_MACH_O_LC_RPATH},
3181
  { "code_signature", BFD_MACH_O_LC_CODE_SIGNATURE},
3182
  { "segment_split_info", BFD_MACH_O_LC_SEGMENT_SPLIT_INFO},
3183
  { "reexport_dylib", BFD_MACH_O_LC_REEXPORT_DYLIB},
3184
  { "lazy_load_dylib", BFD_MACH_O_LC_LAZY_LOAD_DYLIB},
3185
  { "encryption_info", BFD_MACH_O_LC_ENCRYPTION_INFO},
3186
  { NULL, 0}
3187
};
3188
 
3189
static void
3190
bfd_mach_o_print_private_header (bfd *abfd, FILE *file)
3191
{
3192
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3193
  bfd_mach_o_header *h = &mdata->header;
3194
 
3195
  fprintf (file, _("Mach-O header:\n"));
3196
  fprintf (file, _(" magic     : %08lx\n"), h->magic);
3197
  fprintf (file, _(" cputype   : %08lx (%s)\n"), h->cputype,
3198
           bfd_mach_o_get_name (bfd_mach_o_cpu_name, h->cputype));
3199
  fprintf (file, _(" cpusubtype: %08lx\n"), h->cpusubtype);
3200
  fprintf (file, _(" filetype  : %08lx (%s)\n"),
3201
           h->filetype,
3202
           bfd_mach_o_get_name (bfd_mach_o_filetype_name, h->filetype));
3203
  fprintf (file, _(" ncmds     : %08lx\n"), h->ncmds);
3204
  fprintf (file, _(" sizeofcmds: %08lx\n"), h->sizeofcmds);
3205
  fprintf (file, _(" flags     : %08lx ("), h->flags);
3206
  bfd_mach_o_print_flags (bfd_mach_o_header_flags_name, h->flags, file);
3207
  fprintf (file, _(")\n"));
3208
  fprintf (file, _(" reserved  : %08x\n"), h->reserved);
3209
}
3210
 
3211
static void
3212
bfd_mach_o_print_section_map (bfd *abfd, FILE *file)
3213
{
3214
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3215
  unsigned int i, j;
3216
  unsigned int sec_nbr = 0;
3217
 
3218
  fprintf (file, _("Segments and Sections:\n"));
3219
  fprintf (file, _(" #: Segment name     Section name     Address\n"));
3220
 
3221
  for (i = 0; i < mdata->header.ncmds; i++)
3222
    {
3223
      bfd_mach_o_segment_command *seg;
3224
 
3225
      if (mdata->commands[i].type != BFD_MACH_O_LC_SEGMENT
3226
          && mdata->commands[i].type != BFD_MACH_O_LC_SEGMENT_64)
3227
        continue;
3228
 
3229
      seg = &mdata->commands[i].command.segment;
3230
 
3231
      fprintf (file, "[Segment %-16s ", seg->segname);
3232
      fprintf_vma (file, seg->vmaddr);
3233
      fprintf (file, "-");
3234
      fprintf_vma  (file, seg->vmaddr + seg->vmsize - 1);
3235
      fputc (' ', file);
3236
      fputc (seg->initprot & BFD_MACH_O_PROT_READ ? 'r' : '-', file);
3237
      fputc (seg->initprot & BFD_MACH_O_PROT_WRITE ? 'w' : '-', file);
3238
      fputc (seg->initprot & BFD_MACH_O_PROT_EXECUTE ? 'x' : '-', file);
3239
      fprintf (file, "]\n");
3240
      for (j = 0; j < seg->nsects; j++)
3241
        {
3242
          bfd_mach_o_section *sec = &seg->sections[j];
3243
          fprintf (file, "%02u: %-16s %-16s ", ++sec_nbr,
3244
                   sec->segname, sec->sectname);
3245
          fprintf_vma (file, sec->addr);
3246
          fprintf (file, " ");
3247
          fprintf_vma  (file, sec->size);
3248
          fprintf (file, " %08lx\n", sec->flags);
3249
        }
3250
    }
3251
}
3252
 
3253
/* Return the number of indirect symbols for a section.
3254
   Must be called only for symbol pointer section and symbol stubs
3255
   sections.  */
3256
 
3257
static unsigned int
3258
bfd_mach_o_section_get_nbr_indirect (bfd *abfd, bfd_mach_o_section *sec)
3259
{
3260
  unsigned int elsz;
3261
 
3262
  switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
3263
    {
3264
    case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
3265
    case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
3266
      elsz = bfd_mach_o_wide_p (abfd) ? 8 : 4;
3267
      return sec->size / elsz;
3268
    case BFD_MACH_O_S_SYMBOL_STUBS:
3269
      elsz = sec->reserved2;
3270
      if (elsz)
3271
        return sec->size / elsz;
3272
      else
3273
        return 0;
3274
    default:
3275
      BFD_FAIL ();
3276
      return 0;
3277
    }
3278
}
3279
 
3280
static void
3281
bfd_mach_o_print_section (bfd *abfd ATTRIBUTE_UNUSED,
3282
                          bfd_mach_o_section *sec, FILE *file)
3283
{
3284
  fprintf (file, " Section: %-16s %-16s (bfdname: %s)\n",
3285
           sec->sectname, sec->segname, sec->bfdsection->name);
3286
  fprintf (file, "  addr: ");
3287
  fprintf_vma (file, sec->addr);
3288
  fprintf (file, " size: ");
3289
  fprintf_vma  (file, sec->size);
3290
  fprintf (file, " offset: ");
3291
  fprintf_vma (file, sec->offset);
3292
  fprintf (file, "\n");
3293
  fprintf (file, "  align: %ld", sec->align);
3294
  fprintf (file, "  nreloc: %lu  reloff: ", sec->nreloc);
3295
  fprintf_vma (file, sec->reloff);
3296
  fprintf (file, "\n");
3297
  fprintf (file, "  flags: %08lx (type: %s", sec->flags,
3298
           bfd_mach_o_get_name (bfd_mach_o_section_type_name,
3299
                                sec->flags & BFD_MACH_O_SECTION_TYPE_MASK));
3300
  fprintf (file, " attr: ");
3301
  bfd_mach_o_print_flags (bfd_mach_o_section_attribute_name,
3302
                          sec->flags & BFD_MACH_O_SECTION_ATTRIBUTES_MASK,
3303
                          file);
3304
  fprintf (file, ")\n");
3305
  switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
3306
    {
3307
    case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
3308
    case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
3309
    case BFD_MACH_O_S_SYMBOL_STUBS:
3310
      fprintf (file, "  first indirect sym: %lu", sec->reserved1);
3311
      fprintf (file, " (%u entries)",
3312
               bfd_mach_o_section_get_nbr_indirect (abfd, sec));
3313
      break;
3314
    default:
3315
      fprintf (file, "  reserved1: 0x%lx", sec->reserved1);
3316
      break;
3317
    }
3318
  switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
3319
    {
3320
    case BFD_MACH_O_S_SYMBOL_STUBS:
3321
      fprintf (file, "  stub size: %lu", sec->reserved2);
3322
      break;
3323
    default:
3324
      fprintf (file, "  reserved2: 0x%lx", sec->reserved2);
3325
      break;
3326
    }
3327
  fprintf (file, "  reserved3: 0x%lx\n", sec->reserved3);
3328
}
3329
 
3330
static void
3331
bfd_mach_o_print_segment (bfd *abfd ATTRIBUTE_UNUSED,
3332
                          bfd_mach_o_load_command *cmd, FILE *file)
3333
{
3334
  bfd_mach_o_segment_command *seg = &cmd->command.segment;
3335
  unsigned int i;
3336
 
3337
  fprintf (file, " name: %s\n", *seg->segname ? seg->segname : "*none*");
3338
  fprintf (file, "    vmaddr: ");
3339
  fprintf_vma (file, seg->vmaddr);
3340
  fprintf (file, "   vmsize: ");
3341
  fprintf_vma  (file, seg->vmsize);
3342
  fprintf (file, "\n");
3343
  fprintf (file, "   fileoff: ");
3344
  fprintf_vma (file, seg->fileoff);
3345
  fprintf (file, " filesize: ");
3346
  fprintf_vma (file, (bfd_vma)seg->filesize);
3347
  fprintf (file, " endoff: ");
3348
  fprintf_vma (file, (bfd_vma)(seg->fileoff + seg->filesize));
3349
  fprintf (file, "\n");
3350
  fprintf (file, "   nsects: %lu  ", seg->nsects);
3351
  fprintf (file, " flags: %lx\n", seg->flags);
3352
  for (i = 0; i < seg->nsects; i++)
3353
    bfd_mach_o_print_section (abfd, &seg->sections[i], file);
3354
}
3355
 
3356
static void
3357
bfd_mach_o_print_dysymtab (bfd *abfd ATTRIBUTE_UNUSED,
3358
                           bfd_mach_o_load_command *cmd, FILE *file)
3359
{
3360
  bfd_mach_o_dysymtab_command *dysymtab = &cmd->command.dysymtab;
3361
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3362
  unsigned int i;
3363
 
3364
  fprintf (file, "              local symbols: idx: %10lu  num: %-8lu",
3365
           dysymtab->ilocalsym, dysymtab->nlocalsym);
3366
  fprintf (file, " (nxtidx: %lu)\n",
3367
           dysymtab->ilocalsym + dysymtab->nlocalsym);
3368
  fprintf (file, "           external symbols: idx: %10lu  num: %-8lu",
3369
           dysymtab->iextdefsym, dysymtab->nextdefsym);
3370
  fprintf (file, " (nxtidx: %lu)\n",
3371
           dysymtab->iextdefsym + dysymtab->nextdefsym);
3372
  fprintf (file, "          undefined symbols: idx: %10lu  num: %-8lu",
3373
           dysymtab->iundefsym, dysymtab->nundefsym);
3374
  fprintf (file, " (nxtidx: %lu)\n",
3375
           dysymtab->iundefsym + dysymtab->nundefsym);
3376
  fprintf (file, "           table of content: off: 0x%08lx  num: %-8lu",
3377
           dysymtab->tocoff, dysymtab->ntoc);
3378
  fprintf (file, " (endoff: 0x%08lx)\n",
3379
           dysymtab->tocoff
3380
           + dysymtab->ntoc * BFD_MACH_O_TABLE_OF_CONTENT_SIZE);
3381
  fprintf (file, "               module table: off: 0x%08lx  num: %-8lu",
3382
           dysymtab->modtaboff, dysymtab->nmodtab);
3383
  fprintf (file, " (endoff: 0x%08lx)\n",
3384
           dysymtab->modtaboff + dysymtab->nmodtab
3385
           * (mach_o_wide_p (&mdata->header) ?
3386
              BFD_MACH_O_DYLIB_MODULE_64_SIZE : BFD_MACH_O_DYLIB_MODULE_SIZE));
3387
  fprintf (file, "   external reference table: off: 0x%08lx  num: %-8lu",
3388
           dysymtab->extrefsymoff, dysymtab->nextrefsyms);
3389
  fprintf (file, " (endoff: 0x%08lx)\n",
3390
           dysymtab->extrefsymoff
3391
           + dysymtab->nextrefsyms * BFD_MACH_O_REFERENCE_SIZE);
3392
  fprintf (file, "      indirect symbol table: off: 0x%08lx  num: %-8lu",
3393
           dysymtab->indirectsymoff, dysymtab->nindirectsyms);
3394
  fprintf (file, " (endoff: 0x%08lx)\n",
3395
           dysymtab->indirectsymoff
3396
           + dysymtab->nindirectsyms * BFD_MACH_O_INDIRECT_SYMBOL_SIZE);
3397
  fprintf (file, "  external relocation table: off: 0x%08lx  num: %-8lu",
3398
           dysymtab->extreloff, dysymtab->nextrel);
3399
  fprintf (file, " (endoff: 0x%08lx)\n",
3400
           dysymtab->extreloff + dysymtab->nextrel * BFD_MACH_O_RELENT_SIZE);
3401
  fprintf (file, "     local relocation table: off: 0x%08lx  num: %-8lu",
3402
           dysymtab->locreloff, dysymtab->nlocrel);
3403
  fprintf (file, " (endoff: 0x%08lx)\n",
3404
           dysymtab->locreloff + dysymtab->nlocrel * BFD_MACH_O_RELENT_SIZE);
3405
 
3406
  if (dysymtab->ntoc > 0
3407
      || dysymtab->nindirectsyms > 0
3408
      || dysymtab->nextrefsyms > 0)
3409
    {
3410
      /* Try to read the symbols to display the toc or indirect symbols.  */
3411
      bfd_mach_o_scan_read_symtab_symbols (abfd);
3412
    }
3413
  else if (dysymtab->nmodtab > 0)
3414
    {
3415
      /* Try to read the strtab to display modules name.  */
3416
      bfd_mach_o_scan_read_symtab_strtab (abfd);
3417
    }
3418
 
3419
  for (i = 0; i < dysymtab->nmodtab; i++)
3420
    {
3421
      bfd_mach_o_dylib_module *module = &dysymtab->dylib_module[i];
3422
      fprintf (file, "  module %u:\n", i);
3423
      fprintf (file, "   name: %lu", module->module_name_idx);
3424
      if (mdata->symtab && mdata->symtab->strtab)
3425
        fprintf (file, ": %s",
3426
                 mdata->symtab->strtab + module->module_name_idx);
3427
      fprintf (file, "\n");
3428
      fprintf (file, "   extdefsym: idx: %8lu  num: %lu\n",
3429
               module->iextdefsym, module->nextdefsym);
3430
      fprintf (file, "      refsym: idx: %8lu  num: %lu\n",
3431
               module->irefsym, module->nrefsym);
3432
      fprintf (file, "    localsym: idx: %8lu  num: %lu\n",
3433
               module->ilocalsym, module->nlocalsym);
3434
      fprintf (file, "      extrel: idx: %8lu  num: %lu\n",
3435
               module->iextrel, module->nextrel);
3436
      fprintf (file, "        init: idx: %8u  num: %u\n",
3437
               module->iinit, module->ninit);
3438
      fprintf (file, "        term: idx: %8u  num: %u\n",
3439
               module->iterm, module->nterm);
3440
      fprintf (file, "   objc_module_info: addr: ");
3441
      fprintf_vma (file, module->objc_module_info_addr);
3442
      fprintf (file, "  size: %lu\n", module->objc_module_info_size);
3443
    }
3444
 
3445
  if (dysymtab->ntoc > 0)
3446
    {
3447
      bfd_mach_o_symtab_command *symtab = mdata->symtab;
3448
 
3449
      fprintf (file, "  table of content: (symbol/module)\n");
3450
      for (i = 0; i < dysymtab->ntoc; i++)
3451
        {
3452
          bfd_mach_o_dylib_table_of_content *toc = &dysymtab->dylib_toc[i];
3453
 
3454
          fprintf (file, "   %4u: ", i);
3455
          if (symtab && symtab->symbols && toc->symbol_index < symtab->nsyms)
3456
            {
3457
              const char *name = symtab->symbols[toc->symbol_index].symbol.name;
3458
              fprintf (file, "%s (%lu)", name ? name : "*invalid*",
3459
                       toc->symbol_index);
3460
            }
3461
          else
3462
            fprintf (file, "%lu", toc->symbol_index);
3463
 
3464
          fprintf (file, " / ");
3465
          if (symtab && symtab->strtab
3466
              && toc->module_index < dysymtab->nmodtab)
3467
            {
3468
              bfd_mach_o_dylib_module *mod;
3469
              mod = &dysymtab->dylib_module[toc->module_index];
3470
              fprintf (file, "%s (%lu)",
3471
                       symtab->strtab + mod->module_name_idx,
3472
                       toc->module_index);
3473
            }
3474
          else
3475
            fprintf (file, "%lu", toc->module_index);
3476
 
3477
          fprintf (file, "\n");
3478
        }
3479
    }
3480
 
3481
  if (dysymtab->nindirectsyms != 0)
3482
    {
3483
      fprintf (file, "  indirect symbols:\n");
3484
 
3485
      for (i = 0; i < mdata->nsects; i++)
3486
        {
3487
          bfd_mach_o_section *sec = mdata->sections[i];
3488
          unsigned int j, first, last;
3489
          bfd_mach_o_symtab_command *symtab = mdata->symtab;
3490
 
3491
          switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
3492
            {
3493
            case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
3494
            case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
3495
            case BFD_MACH_O_S_SYMBOL_STUBS:
3496
              first = sec->reserved1;
3497
              last = first + bfd_mach_o_section_get_nbr_indirect (abfd, sec);
3498
              fprintf (file, "  for section %s.%s:\n",
3499
                       sec->segname, sec->sectname);
3500
              for (j = first; j < last; j++)
3501
                {
3502
                  unsigned int isym = dysymtab->indirect_syms[j];
3503
 
3504
                  fprintf (file, "  %5u: 0x%08x (%u)", j, isym, isym);
3505
                  if (isym & BFD_MACH_O_INDIRECT_SYMBOL_LOCAL)
3506
                    fprintf (file, " LOCAL");
3507
                  if (isym & BFD_MACH_O_INDIRECT_SYMBOL_ABS)
3508
                    fprintf (file, " ABSOLUTE");
3509
                  if (symtab && symtab->symbols
3510
                      && isym < symtab->nsyms
3511
                      && symtab->symbols[isym].symbol.name)
3512
                    fprintf (file, " %s", symtab->symbols[isym].symbol.name);
3513
                  fprintf (file, "\n");
3514
                }
3515
              break;
3516
            default:
3517
              break;
3518
            }
3519
        }
3520
    }
3521
  if (dysymtab->nextrefsyms > 0)
3522
    {
3523
      bfd_mach_o_symtab_command *symtab = mdata->symtab;
3524
 
3525
      fprintf (file, "  external reference table: (symbol flags)\n");
3526
      for (i = 0; i < dysymtab->nextrefsyms; i++)
3527
        {
3528
          bfd_mach_o_dylib_reference *ref = &dysymtab->ext_refs[i];
3529
 
3530
          fprintf (file, "   %4u: %5lu 0x%02lx", i, ref->isym, ref->flags);
3531
          if (symtab && symtab->symbols
3532
              && ref->isym < symtab->nsyms
3533
              && symtab->symbols[ref->isym].symbol.name)
3534
            fprintf (file, " %s", symtab->symbols[ref->isym].symbol.name);
3535
          fprintf (file, "\n");
3536
        }
3537
    }
3538
 
3539
}
3540
 
3541
bfd_boolean
3542
bfd_mach_o_bfd_print_private_bfd_data (bfd *abfd, PTR ptr)
3543
{
3544
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3545
  FILE *file = (FILE *) ptr;
3546
  unsigned int i;
3547
 
3548
  bfd_mach_o_print_private_header (abfd, file);
3549
  fputc ('\n', file);
3550
 
3551
  for (i = 0; i < mdata->header.ncmds; i++)
3552
    {
3553
      bfd_mach_o_load_command *cmd = &mdata->commands[i];
3554
 
3555
      fprintf (file, "Load command %s:",
3556
               bfd_mach_o_get_name (bfd_mach_o_load_command_name, cmd->type));
3557
      switch (cmd->type)
3558
        {
3559
        case BFD_MACH_O_LC_SEGMENT:
3560
        case BFD_MACH_O_LC_SEGMENT_64:
3561
          bfd_mach_o_print_segment (abfd, cmd, file);
3562
          break;
3563
        case BFD_MACH_O_LC_UUID:
3564
          {
3565
            bfd_mach_o_uuid_command *uuid = &cmd->command.uuid;
3566
            unsigned int i;
3567
 
3568
            for (i = 0; i < sizeof (uuid->uuid); i++)
3569
              fprintf (file, " %02x", uuid->uuid[i]);
3570
            fputc ('\n', file);
3571
          }
3572
          break;
3573
        case BFD_MACH_O_LC_LOAD_DYLIB:
3574
        case BFD_MACH_O_LC_LOAD_WEAK_DYLIB:
3575
        case BFD_MACH_O_LC_REEXPORT_DYLIB:
3576
        case BFD_MACH_O_LC_ID_DYLIB:
3577
          {
3578
            bfd_mach_o_dylib_command *dylib = &cmd->command.dylib;
3579
            fprintf (file, " %s\n", dylib->name_str);
3580
            fprintf (file, "            time stamp: 0x%08lx\n",
3581
                     dylib->timestamp);
3582
            fprintf (file, "       current version: 0x%08lx\n",
3583
                     dylib->current_version);
3584
            fprintf (file, "  comptibility version: 0x%08lx\n",
3585
                     dylib->compatibility_version);
3586
            break;
3587
          }
3588
        case BFD_MACH_O_LC_LOAD_DYLINKER:
3589
        case BFD_MACH_O_LC_ID_DYLINKER:
3590
          fprintf (file, " %s\n", cmd->command.dylinker.name_str);
3591
          break;
3592
        case BFD_MACH_O_LC_SYMTAB:
3593
          {
3594
            bfd_mach_o_symtab_command *symtab = &cmd->command.symtab;
3595
            fprintf (file,
3596
                     "\n"
3597
                     "   symoff: 0x%08x    nsyms: %8u  (endoff: 0x%08x)\n",
3598
                     symtab->symoff, symtab->nsyms,
3599
                     symtab->symoff + symtab->nsyms
3600
                     * (mach_o_wide_p (&mdata->header)
3601
                        ? BFD_MACH_O_NLIST_64_SIZE : BFD_MACH_O_NLIST_SIZE));
3602
            fprintf (file,
3603
                     "   stroff: 0x%08x  strsize: %8u  (endoff: 0x%08x)\n",
3604
                     symtab->stroff, symtab->strsize,
3605
                     symtab->stroff + symtab->strsize);
3606
            break;
3607
          }
3608
        case BFD_MACH_O_LC_DYSYMTAB:
3609
          fprintf (file, "\n");
3610
          bfd_mach_o_print_dysymtab (abfd, cmd, file);
3611
          break;
3612
        case BFD_MACH_O_LC_CODE_SIGNATURE:
3613
        case BFD_MACH_O_LC_SEGMENT_SPLIT_INFO:
3614
          {
3615
            bfd_mach_o_linkedit_command *linkedit = &cmd->command.linkedit;
3616
            fprintf
3617
              (file, "\n"
3618
               "  dataoff: 0x%08lx  datasize: 0x%08lx  (endoff: 0x%08lx)\n",
3619
               linkedit->dataoff, linkedit->datasize,
3620
               linkedit->dataoff + linkedit->datasize);
3621
            break;
3622
          }
3623
        case BFD_MACH_O_LC_SUB_FRAMEWORK:
3624
        case BFD_MACH_O_LC_SUB_UMBRELLA:
3625
        case BFD_MACH_O_LC_SUB_LIBRARY:
3626
        case BFD_MACH_O_LC_SUB_CLIENT:
3627
          {
3628
            bfd_mach_o_str_command *str = &cmd->command.str;
3629
            fprintf (file, " %s\n", str->str);
3630
            break;
3631
          }
3632
        case BFD_MACH_O_LC_THREAD:
3633
        case BFD_MACH_O_LC_UNIXTHREAD:
3634
          {
3635
            bfd_mach_o_thread_command *thread = &cmd->command.thread;
3636
            unsigned int j;
3637
            bfd_mach_o_backend_data *bed = bfd_mach_o_get_backend_data (abfd);
3638
 
3639
            fprintf (file, " nflavours: %lu\n", thread->nflavours);
3640
            for (j = 0; j < thread->nflavours; j++)
3641
              {
3642
                bfd_mach_o_thread_flavour *flavour = &thread->flavours[j];
3643
 
3644
                fprintf (file, "  %2u: flavour: 0x%08lx  offset: 0x%08lx"
3645
                         "  size: 0x%08lx\n",
3646
                         j, flavour->flavour, flavour->offset,
3647
                         flavour->size);
3648
                if (bed->_bfd_mach_o_print_thread)
3649
                  {
3650
                    char *buf = bfd_malloc (flavour->size);
3651
 
3652
                    if (buf
3653
                        && bfd_seek (abfd, flavour->offset, SEEK_SET) == 0
3654
                        && (bfd_bread (buf, flavour->size, abfd)
3655
                            == flavour->size))
3656
                      (*bed->_bfd_mach_o_print_thread)(abfd, flavour,
3657
                                                       file, buf);
3658
                    free (buf);
3659
                  }
3660
              }
3661
            break;
3662
          }
3663
        default:
3664
          fprintf (file, "\n");
3665
          break;
3666
        }
3667
      fputc ('\n', file);
3668
    }
3669
 
3670
  bfd_mach_o_print_section_map (abfd, file);
3671
 
3672
  return TRUE;
3673
}
3674
 
3675 24 jeremybenn
int
3676
bfd_mach_o_core_fetch_environment (bfd *abfd,
3677
                                   unsigned char **rbuf,
3678
                                   unsigned int *rlen)
3679
{
3680 225 jeremybenn
  bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
3681 24 jeremybenn
  unsigned long stackaddr = bfd_mach_o_stack_addr (mdata->header.cputype);
3682
  unsigned int i = 0;
3683
 
3684
  for (i = 0; i < mdata->header.ncmds; i++)
3685
    {
3686
      bfd_mach_o_load_command *cur = &mdata->commands[i];
3687
      bfd_mach_o_segment_command *seg = NULL;
3688
 
3689
      if (cur->type != BFD_MACH_O_LC_SEGMENT)
3690
        continue;
3691
 
3692
      seg = &cur->command.segment;
3693
 
3694
      if ((seg->vmaddr + seg->vmsize) == stackaddr)
3695
        {
3696
          unsigned long start = seg->fileoff;
3697
          unsigned long end = seg->fileoff + seg->filesize;
3698
          unsigned char *buf = bfd_malloc (1024);
3699
          unsigned long size = 1024;
3700
 
3701
          for (;;)
3702
            {
3703
              bfd_size_type nread = 0;
3704
              unsigned long offset;
3705
              int found_nonnull = 0;
3706
 
3707
              if (size > (end - start))
3708
                size = (end - start);
3709
 
3710
              buf = bfd_realloc_or_free (buf, size);
3711
              if (buf == NULL)
3712
                return -1;
3713 225 jeremybenn
 
3714
              if (bfd_seek (abfd, end - size, SEEK_SET) != 0)
3715
                {
3716
                  free (buf);
3717
                  return -1;
3718
                }
3719
 
3720 24 jeremybenn
              nread = bfd_bread (buf, size, abfd);
3721
 
3722
              if (nread != size)
3723
                {
3724
                  free (buf);
3725
                  return -1;
3726
                }
3727
 
3728
              for (offset = 4; offset <= size; offset += 4)
3729
                {
3730
                  unsigned long val;
3731
 
3732
                  val = *((unsigned long *) (buf + size - offset));
3733
                  if (! found_nonnull)
3734
                    {
3735
                      if (val != 0)
3736
                        found_nonnull = 1;
3737
                    }
3738
                  else if (val == 0x0)
3739
                    {
3740
                      unsigned long bottom;
3741
                      unsigned long top;
3742
 
3743
                      bottom = seg->fileoff + seg->filesize - offset;
3744
                      top = seg->fileoff + seg->filesize - 4;
3745
                      *rbuf = bfd_malloc (top - bottom);
3746
                      *rlen = top - bottom;
3747
 
3748
                      memcpy (*rbuf, buf + size - *rlen, *rlen);
3749
                      free (buf);
3750
                      return 0;
3751
                    }
3752
                }
3753
 
3754
              if (size == (end - start))
3755
                break;
3756
 
3757
              size *= 2;
3758
            }
3759
 
3760
          free (buf);
3761
        }
3762
    }
3763
 
3764
  return -1;
3765
}
3766
 
3767
char *
3768
bfd_mach_o_core_file_failing_command (bfd *abfd)
3769
{
3770
  unsigned char *buf = NULL;
3771
  unsigned int len = 0;
3772
  int ret = -1;
3773
 
3774
  ret = bfd_mach_o_core_fetch_environment (abfd, &buf, &len);
3775
  if (ret < 0)
3776
    return NULL;
3777
 
3778
  return (char *) buf;
3779
}
3780
 
3781
int
3782
bfd_mach_o_core_file_failing_signal (bfd *abfd ATTRIBUTE_UNUSED)
3783
{
3784
  return 0;
3785
}
3786
 
3787 225 jeremybenn
#define bfd_mach_o_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup 
3788
#define bfd_mach_o_bfd_reloc_name_lookup _bfd_norelocs_bfd_reloc_name_lookup
3789
 
3790
#define bfd_mach_o_swap_reloc_in NULL
3791
#define bfd_mach_o_swap_reloc_out NULL
3792
#define bfd_mach_o_print_thread NULL
3793
 
3794 24 jeremybenn
#define TARGET_NAME             mach_o_be_vec
3795
#define TARGET_STRING           "mach-o-be"
3796
#define TARGET_BIG_ENDIAN       1
3797
#define TARGET_ARCHIVE          0
3798
#include "mach-o-target.c"
3799
 
3800
#undef TARGET_NAME
3801
#undef TARGET_STRING
3802
#undef TARGET_BIG_ENDIAN
3803
#undef TARGET_ARCHIVE
3804
 
3805
#define TARGET_NAME             mach_o_le_vec
3806
#define TARGET_STRING           "mach-o-le"
3807
#define TARGET_BIG_ENDIAN       0
3808
#define TARGET_ARCHIVE          0
3809
 
3810
#include "mach-o-target.c"
3811
 
3812
#undef TARGET_NAME
3813
#undef TARGET_STRING
3814
#undef TARGET_BIG_ENDIAN
3815
#undef TARGET_ARCHIVE
3816
 
3817
#define TARGET_NAME             mach_o_fat_vec
3818
#define TARGET_STRING           "mach-o-fat"
3819
#define TARGET_BIG_ENDIAN       1
3820
#define TARGET_ARCHIVE          1
3821
 
3822
#include "mach-o-target.c"
3823
 
3824
#undef TARGET_NAME
3825
#undef TARGET_STRING
3826
#undef TARGET_BIG_ENDIAN
3827
#undef TARGET_ARCHIVE

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.