~ubuntu-branches/ubuntu/utopic/binutils-arm64-cross/utopic

« back to all changes in this revision

Viewing changes to binutils-2.23.52.20130611/gas/config/obj-macho.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-06-20 17:38:09 UTC
  • Revision ID: package-import@ubuntu.com-20130620173809-app8lzgvymy5fg6c
Tags: 0.7
Build-depend on binutils-source (>= 2.23.52.20130620-1~).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Mach-O object file format
 
2
   Copyright 2009, 2011, 2012 Free Software Foundation, Inc.
 
3
 
 
4
   This file is part of GAS, the GNU Assembler.
 
5
 
 
6
   GAS is free software; you can redistribute it and/or modify
 
7
   it under the terms of the GNU General Public License as
 
8
   published by the Free Software Foundation; either version 3,
 
9
   or (at your option) any later version.
 
10
 
 
11
   GAS is distributed in the hope that it will be useful, but
 
12
   WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 
14
   the GNU General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU General Public License
 
17
   along with GAS; see the file COPYING.  If not, write to the Free
 
18
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
 
19
   02110-1301, USA.  */
 
20
 
 
21
/* Here we handle the mach-o directives that are common to all architectures.
 
22
 
 
23
   Most significant are mach-o named sections and a variety of symbol type
 
24
   decorations.  */
 
25
 
 
26
/* Mach-O supports multiple, named segments each of which may contain
 
27
   multiple named sections.  Thus the concept of subsectioning is 
 
28
   handled by (say) having a __TEXT segment with appropriate flags from
 
29
   which subsections are generated like __text, __const etc.  
 
30
   
 
31
   The well-known as short-hand section switch directives like .text, .data
 
32
   etc. are mapped onto predefined segment/section pairs using facilites
 
33
   supplied by the mach-o port of bfd.
 
34
   
 
35
   A number of additional mach-o short-hand section switch directives are
 
36
   also defined.  */
 
37
 
 
38
#define OBJ_HEADER "obj-macho.h"
 
39
 
 
40
#include "as.h"
 
41
#include "subsegs.h"
 
42
#include "symbols.h"
 
43
#include "write.h"
 
44
#include "mach-o.h"
 
45
#include "mach-o/loader.h"
 
46
#include "obj-macho.h"
 
47
 
 
48
#include <string.h>
 
49
 
 
50
/* Forward decls.  */
 
51
static segT obj_mach_o_segT_from_bfd_name (const char *, int);
 
52
 
 
53
/* TODO: Implement "-dynamic"/"-static" command line options.  */
 
54
 
 
55
static int obj_mach_o_is_static;
 
56
 
 
57
/* TODO: Implement the "-n" command line option to suppress the initial
 
58
   switch to the text segment.  */
 
59
 
 
60
static int obj_mach_o_start_with_text_section = 1;
 
61
 
 
62
/* Allow for special re-ordering on output.  */
 
63
 
 
64
static int obj_mach_o_seen_objc_section;
 
65
 
 
66
/* Start-up: At present, just create the sections we want.  */
 
67
void
 
68
mach_o_begin (void)
 
69
{
 
70
  /* Mach-O only defines the .text section by default, and even this can
 
71
     be suppressed by a flag.  In the latter event, the first code MUST
 
72
     be a section definition.  */
 
73
  if (obj_mach_o_start_with_text_section)
 
74
    {
 
75
      text_section = obj_mach_o_segT_from_bfd_name (TEXT_SECTION_NAME, 1);
 
76
      subseg_set (text_section, 0);
 
77
      if (obj_mach_o_is_static)
 
78
        {
 
79
          bfd_mach_o_section *mo_sec 
 
80
                        = bfd_mach_o_get_mach_o_section (text_section);
 
81
          mo_sec->flags &= ~BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS;
 
82
        }
 
83
    }
 
84
}
 
85
 
 
86
/* Remember the subsections_by_symbols state in case we need to reset
 
87
   the file flags.  */
 
88
 
 
89
static int obj_mach_o_subsections_by_symbols;
 
90
 
 
91
/* This will put at most 16 characters (terminated by a ',' or newline) from
 
92
   the input stream into dest.  If there are more than 16 chars before the
 
93
   delimiter, a warning is given and the string is truncated.  On completion of
 
94
   this function, input_line_pointer will point to the char after the ',' or 
 
95
   to the newline.  
 
96
   
 
97
   It trims leading and trailing space.  */
 
98
 
 
99
static int
 
100
collect_16char_name (char *dest, const char *msg, int require_comma)
 
101
{
 
102
  char c, *namstart;
 
103
 
 
104
  SKIP_WHITESPACE ();
 
105
  namstart = input_line_pointer;
 
106
 
 
107
  while ( (c = *input_line_pointer) != ',' 
 
108
         && !is_end_of_line[(unsigned char) c])
 
109
    input_line_pointer++;
 
110
 
 
111
  {
 
112
      int len = input_line_pointer - namstart; /* could be zero.  */
 
113
      /* lose any trailing space.  */  
 
114
      while (len > 0 && namstart[len-1] == ' ') 
 
115
        len--;
 
116
      if (len > 16)
 
117
        {
 
118
          *input_line_pointer = '\0'; /* make a temp string.  */
 
119
          as_bad (_("the %s name '%s' is too long (maximum 16 characters)"),
 
120
                     msg, namstart);
 
121
          *input_line_pointer = c; /* restore for printing.  */
 
122
          len = 16;
 
123
        }
 
124
      if (len > 0)
 
125
        memcpy (dest, namstart, len);
 
126
  }
 
127
 
 
128
  if (c != ',' && require_comma)
 
129
    {
 
130
      as_bad (_("expected a %s name followed by a `,'"), msg);
 
131
      return 1;
 
132
    }
 
133
 
 
134
  return 0;
 
135
}
 
136
 
 
137
static int
 
138
obj_mach_o_get_section_names (char *seg, char *sec,
 
139
                              unsigned segl, unsigned secl)
 
140
{
 
141
  /* Zero-length segment and section names are allowed.  */
 
142
  /* Parse segment name.  */
 
143
  memset (seg, 0, segl);
 
144
  if (collect_16char_name (seg, "segment", 1))
 
145
    {
 
146
      ignore_rest_of_line ();
 
147
      return 0;
 
148
    }
 
149
  input_line_pointer++; /* Skip the terminating ',' */
 
150
 
 
151
  /* Parse section name, which can be empty.  */
 
152
  memset (sec, 0, secl);
 
153
  collect_16char_name (sec, "section", 0);
 
154
  return 1;
 
155
}
 
156
 
 
157
/* Build (or get) a section from the mach-o description - which includes
 
158
   optional definitions for type, attributes, alignment and stub size.
 
159
   
 
160
   BFD supplies default values for sections which have a canonical name.  */
 
161
 
 
162
#define SECT_TYPE_SPECIFIED 0x0001
 
163
#define SECT_ATTR_SPECIFIED 0x0002
 
164
#define SECT_ALGN_SPECIFIED 0x0004
 
165
#define SECT_STUB_SPECIFIED 0x0008
 
166
 
 
167
static segT
 
168
obj_mach_o_make_or_get_sect (char * segname, char * sectname,
 
169
                             unsigned int specified_mask, 
 
170
                             unsigned int usectype, unsigned int usecattr,
 
171
                             unsigned int ualign, offsetT stub_size)
 
172
{
 
173
  unsigned int sectype, secattr, secalign;
 
174
  flagword oldflags, flags;
 
175
  const char *name;
 
176
  segT sec;
 
177
  bfd_mach_o_section *msect;
 
178
  const mach_o_section_name_xlat *xlat;
 
179
 
 
180
  /* This provides default bfd flags and default mach-o section type and
 
181
     attributes along with the canonical name.  */
 
182
  xlat = bfd_mach_o_section_data_for_mach_sect (stdoutput, segname, sectname);
 
183
 
 
184
  /* TODO: more checking of whether overides are acually allowed.  */
 
185
 
 
186
  if (xlat != NULL)
 
187
    {
 
188
      name = xstrdup (xlat->bfd_name);
 
189
      sectype = xlat->macho_sectype;
 
190
      if (specified_mask & SECT_TYPE_SPECIFIED)
 
191
        {
 
192
          if ((sectype == BFD_MACH_O_S_ZEROFILL
 
193
               || sectype == BFD_MACH_O_S_GB_ZEROFILL)
 
194
              && sectype != usectype)
 
195
            as_bad (_("cannot overide zerofill section type for `%s,%s'"),
 
196
                    segname, sectname);
 
197
          else
 
198
            sectype = usectype;
 
199
        }
 
200
      secattr = xlat->macho_secattr;
 
201
      secalign = xlat->sectalign;
 
202
      flags = xlat->bfd_flags;
 
203
    }
 
204
  else
 
205
    {
 
206
      /* There is no normal BFD section name for this section.  Create one.
 
207
         The name created doesn't really matter as it will never be written
 
208
         on disk.  */
 
209
      size_t seglen = strlen (segname);
 
210
      size_t sectlen = strlen (sectname);
 
211
      char *n;
 
212
 
 
213
      n = xmalloc (seglen + 1 + sectlen + 1);
 
214
      memcpy (n, segname, seglen);
 
215
      n[seglen] = '.';
 
216
      memcpy (n + seglen + 1, sectname, sectlen);
 
217
      n[seglen + 1 + sectlen] = 0;
 
218
      name = n;
 
219
      if (specified_mask & SECT_TYPE_SPECIFIED)
 
220
        sectype = usectype;
 
221
      else
 
222
        sectype = BFD_MACH_O_S_REGULAR;
 
223
      secattr = BFD_MACH_O_S_ATTR_NONE;
 
224
      secalign = 0;
 
225
      flags = SEC_NO_FLAGS;
 
226
    }
 
227
 
 
228
  /* For now, just use what the user provided.  */
 
229
 
 
230
  if (specified_mask & SECT_ATTR_SPECIFIED)
 
231
    secattr = usecattr;
 
232
 
 
233
  if (specified_mask & SECT_ALGN_SPECIFIED)
 
234
    secalign = ualign;
 
235
 
 
236
  /* Sub-segments don't exists as is on Mach-O.  */
 
237
  sec = subseg_new (name, 0);
 
238
 
 
239
  oldflags = bfd_get_section_flags (stdoutput, sec);
 
240
  msect = bfd_mach_o_get_mach_o_section (sec);
 
241
 
 
242
  if (oldflags == SEC_NO_FLAGS)
 
243
    {
 
244
      /* In the absence of canonical information, try to determine CODE and
 
245
         DEBUG section flags from the mach-o section data.  */
 
246
      if (flags == SEC_NO_FLAGS
 
247
          && (specified_mask & SECT_ATTR_SPECIFIED)
 
248
          && (secattr & BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS))
 
249
        flags |= SEC_CODE;
 
250
      
 
251
      if (flags == SEC_NO_FLAGS
 
252
          && (specified_mask & SECT_ATTR_SPECIFIED)
 
253
          && (secattr & BFD_MACH_O_S_ATTR_DEBUG))
 
254
        flags |= SEC_DEBUGGING;
 
255
 
 
256
      /* New, so just use the defaults or what's specified.  */
 
257
      if (! bfd_set_section_flags (stdoutput, sec, flags))
 
258
        as_warn (_("failed to set flags for \"%s\": %s"),
 
259
                 bfd_section_name (stdoutput, sec),
 
260
                 bfd_errmsg (bfd_get_error ()));
 
261
 
 
262
      strncpy (msect->segname, segname, sizeof (msect->segname));
 
263
      strncpy (msect->sectname, sectname, sizeof (msect->sectname));
 
264
 
 
265
      msect->align = secalign;
 
266
      msect->flags = sectype | secattr;
 
267
      
 
268
      if (sectype == BFD_MACH_O_S_ZEROFILL
 
269
          || sectype == BFD_MACH_O_S_GB_ZEROFILL)
 
270
        seg_info (sec)->bss = 1;
 
271
    }
 
272
  else if (flags != SEC_NO_FLAGS)
 
273
    {
 
274
      if (flags != oldflags
 
275
          || msect->flags != (secattr | sectype))
 
276
        as_warn (_("Ignoring changed section attributes for %s"), name);
 
277
    }
 
278
 
 
279
  if (specified_mask & SECT_STUB_SPECIFIED)
 
280
    /* At present, the stub size is not supplied from the BFD tables.  */
 
281
    msect->reserved2 = stub_size;
 
282
 
 
283
  return sec;
 
284
}
 
285
 
 
286
/* .section
 
287
 
 
288
   The '.section' specification syntax looks like:
 
289
   .section <segment> , <section> [, type [, attribs [, size]]]
 
290
 
 
291
   White space is allowed everywhere between elements.
 
292
 
 
293
   <segment> and <section> may be from 0 to 16 chars in length - they may
 
294
   contain spaces but leading and trailing space will be trimmed.  It is 
 
295
   mandatory that they be present (or that zero-length names are indicated
 
296
   by ",,").
 
297
 
 
298
   There is only a single section type for any entry.
 
299
 
 
300
   There may be multiple attributes, they are delimited by `+'.
 
301
 
 
302
   Not all section types and attributes are accepted by the Darwin system
 
303
   assemblers as user-specifiable - although, at present, we do here.  */
 
304
 
 
305
static void
 
306
obj_mach_o_section (int ignore ATTRIBUTE_UNUSED)
 
307
{
 
308
  unsigned int sectype = BFD_MACH_O_S_REGULAR;
 
309
  unsigned int specified_mask = 0;
 
310
  unsigned int secattr = 0;
 
311
  offsetT sizeof_stub = 0;
 
312
  segT new_seg;
 
313
  char segname[17];
 
314
  char sectname[17];
 
315
 
 
316
#ifdef md_flush_pending_output
 
317
  md_flush_pending_output ();
 
318
#endif
 
319
 
 
320
  /* Get the User's segment annd section names.  */
 
321
  if (! obj_mach_o_get_section_names (segname, sectname, 17, 17))
 
322
    return;
 
323
 
 
324
  /* Parse section type, if present.  */
 
325
  if (*input_line_pointer == ',')
 
326
    {
 
327
      char *p;
 
328
      char c;
 
329
      char tmpc;
 
330
      int len;
 
331
      input_line_pointer++;
 
332
      SKIP_WHITESPACE ();
 
333
      p = input_line_pointer;
 
334
      while ((c = *input_line_pointer) != ','
 
335
              && !is_end_of_line[(unsigned char) c])
 
336
        input_line_pointer++;
 
337
 
 
338
      len = input_line_pointer - p;
 
339
      /* strip trailing spaces.  */
 
340
      while (len > 0 && p[len-1] == ' ')
 
341
        len--;
 
342
      tmpc = p[len];
 
343
 
 
344
      /* Temporarily make a string from the token.  */
 
345
      p[len] = 0;
 
346
      sectype = bfd_mach_o_get_section_type_from_name (stdoutput, p);
 
347
      if (sectype > 255) /* Max Section ID == 255.  */
 
348
        {
 
349
          as_bad (_("unknown or invalid section type '%s'"), p);
 
350
          p[len] = tmpc;
 
351
          ignore_rest_of_line ();
 
352
          return;
 
353
        }
 
354
      else
 
355
        specified_mask |= SECT_TYPE_SPECIFIED;
 
356
      /* Restore.  */
 
357
      p[len] = tmpc;
 
358
 
 
359
      /* Parse attributes.
 
360
         TODO: check validity of attributes for section type.  */
 
361
      if ((specified_mask & SECT_TYPE_SPECIFIED)
 
362
          && c == ',')
 
363
        {
 
364
          do
 
365
            {
 
366
              int attr;
 
367
 
 
368
              /* Skip initial `,' and subsequent `+'.  */
 
369
              input_line_pointer++;
 
370
              SKIP_WHITESPACE ();
 
371
              p = input_line_pointer;
 
372
              while ((c = *input_line_pointer) != '+'
 
373
                      && c != ','
 
374
                      && !is_end_of_line[(unsigned char) c])
 
375
                input_line_pointer++;
 
376
 
 
377
              len = input_line_pointer - p;
 
378
              /* strip trailing spaces.  */
 
379
              while (len > 0 && p[len-1] == ' ')
 
380
                len--;
 
381
              tmpc = p[len];
 
382
 
 
383
              /* Temporarily make a string from the token.  */
 
384
              p[len] ='\0';
 
385
              attr = bfd_mach_o_get_section_attribute_from_name (p);
 
386
              if (attr == -1)
 
387
                {
 
388
                  as_bad (_("unknown or invalid section attribute '%s'"), p);
 
389
                  p[len] = tmpc;
 
390
                  ignore_rest_of_line ();
 
391
                  return;
 
392
                }
 
393
              else
 
394
                {
 
395
                  specified_mask |= SECT_ATTR_SPECIFIED;
 
396
                  secattr |= attr;
 
397
                }
 
398
              /* Restore.  */
 
399
              p[len] = tmpc;
 
400
            }
 
401
          while (*input_line_pointer == '+');
 
402
 
 
403
          /* Parse sizeof_stub.  */
 
404
          if ((specified_mask & SECT_ATTR_SPECIFIED) 
 
405
              && *input_line_pointer == ',')
 
406
            {
 
407
              if (sectype != BFD_MACH_O_S_SYMBOL_STUBS)
 
408
                {
 
409
                  as_bad (_("unexpected section size information"));
 
410
                  ignore_rest_of_line ();
 
411
                  return;
 
412
                }
 
413
 
 
414
              input_line_pointer++;
 
415
              sizeof_stub = get_absolute_expression ();
 
416
              specified_mask |= SECT_STUB_SPECIFIED;
 
417
            }
 
418
          else if ((specified_mask & SECT_ATTR_SPECIFIED) 
 
419
                   && sectype == BFD_MACH_O_S_SYMBOL_STUBS)
 
420
            {
 
421
              as_bad (_("missing sizeof_stub expression"));
 
422
              ignore_rest_of_line ();
 
423
              return;
 
424
            }
 
425
        }
 
426
    }
 
427
 
 
428
  new_seg = obj_mach_o_make_or_get_sect (segname, sectname, specified_mask, 
 
429
                                         sectype, secattr, 0 /*align */,
 
430
                                         sizeof_stub);
 
431
  if (new_seg != NULL)
 
432
    {
 
433
      subseg_set (new_seg, 0);
 
434
      demand_empty_rest_of_line ();
 
435
    }
 
436
}
 
437
 
 
438
/* .zerofill segname, sectname [, symbolname, size [, align]]
 
439
 
 
440
   Zerofill switches, temporarily, to a sect of type 'zerofill'.
 
441
 
 
442
   If a variable name is given, it defines that in the section.
 
443
   Otherwise it just creates the section if it doesn't exist.  */
 
444
 
 
445
static void
 
446
obj_mach_o_zerofill (int ignore ATTRIBUTE_UNUSED)
 
447
{
 
448
  char segname[17];
 
449
  char sectname[17];
 
450
  segT old_seg = now_seg;
 
451
  segT new_seg;
 
452
  symbolS *sym = NULL;
 
453
  unsigned int align = 0;
 
454
  unsigned int specified_mask = 0;
 
455
  offsetT size = 0;
 
456
 
 
457
#ifdef md_flush_pending_output
 
458
  md_flush_pending_output ();
 
459
#endif
 
460
 
 
461
  /* Get the User's segment annd section names.  */
 
462
  if (! obj_mach_o_get_section_names (segname, sectname, 17, 17))
 
463
    return;
 
464
 
 
465
  /* Parse variable definition, if present.  */
 
466
  if (*input_line_pointer == ',')
 
467
    {
 
468
      /* Parse symbol, size [.align] 
 
469
         We follow the method of s_common_internal, with the difference
 
470
         that the symbol cannot be a duplicate-common.  */
 
471
      char *name;
 
472
      char c;
 
473
      char *p;
 
474
      expressionS exp;
 
475
  
 
476
      input_line_pointer++; /* Skip ',' */
 
477
      SKIP_WHITESPACE ();
 
478
      name = input_line_pointer;
 
479
      c = get_symbol_end ();
 
480
      /* Just after name is now '\0'.  */
 
481
      p = input_line_pointer;
 
482
      *p = c;
 
483
 
 
484
      if (name == p)
 
485
        {
 
486
          as_bad (_("expected symbol name"));
 
487
          ignore_rest_of_line ();
 
488
          goto done;
 
489
        }
 
490
 
 
491
      SKIP_WHITESPACE ();  
 
492
      if (*input_line_pointer == ',')
 
493
        input_line_pointer++;
 
494
 
 
495
      expression_and_evaluate (&exp);
 
496
      if (exp.X_op != O_constant
 
497
          && exp.X_op != O_absent)
 
498
        {
 
499
            as_bad (_("bad or irreducible absolute expression"));
 
500
          ignore_rest_of_line ();
 
501
          goto done;
 
502
        }
 
503
      else if (exp.X_op == O_absent)
 
504
        {
 
505
          as_bad (_("missing size expression"));
 
506
          ignore_rest_of_line ();
 
507
          goto done;
 
508
        }
 
509
 
 
510
      size = exp.X_add_number;
 
511
      size &= ((offsetT) 2 << (stdoutput->arch_info->bits_per_address - 1)) - 1;
 
512
      if (exp.X_add_number != size || !exp.X_unsigned)
 
513
        {
 
514
          as_warn (_("size (%ld) out of range, ignored"),
 
515
                   (long) exp.X_add_number);
 
516
          ignore_rest_of_line ();
 
517
          goto done;
 
518
        }
 
519
 
 
520
     *p = 0; /* Make the name into a c string for err messages.  */
 
521
     sym = symbol_find_or_make (name);
 
522
     if (S_IS_DEFINED (sym) || symbol_equated_p (sym))
 
523
        {
 
524
          as_bad (_("symbol `%s' is already defined"), name);
 
525
          *p = c;
 
526
          ignore_rest_of_line ();
 
527
           goto done;
 
528
        }
 
529
 
 
530
      size = S_GET_VALUE (sym);
 
531
      if (size == 0)
 
532
        size = exp.X_add_number;
 
533
      else if (size != exp.X_add_number)
 
534
        as_warn (_("size of \"%s\" is already %ld; not changing to %ld"),
 
535
                   name, (long) size, (long) exp.X_add_number);
 
536
 
 
537
      *p = c;  /* Restore the termination char.  */
 
538
      
 
539
      SKIP_WHITESPACE ();  
 
540
      if (*input_line_pointer == ',')
 
541
        {
 
542
          align = (unsigned int) parse_align (0);
 
543
          if (align == (unsigned int) -1)
 
544
            {
 
545
              as_warn (_("align value not recognized, using size"));
 
546
              align = size;
 
547
            }
 
548
          if (align > 15)
 
549
            {
 
550
              as_warn (_("Alignment (%lu) too large: 15 assumed."),
 
551
                        (unsigned long)align);
 
552
              align = 15;
 
553
            }
 
554
          specified_mask |= SECT_ALGN_SPECIFIED;
 
555
        }
 
556
    }
 
557
 /* else just a section definition.  */
 
558
 
 
559
  specified_mask |= SECT_TYPE_SPECIFIED;
 
560
  new_seg = obj_mach_o_make_or_get_sect (segname, sectname, specified_mask, 
 
561
                                         BFD_MACH_O_S_ZEROFILL,
 
562
                                         BFD_MACH_O_S_ATTR_NONE,
 
563
                                         align, (offsetT) 0 /*stub size*/);
 
564
  if (new_seg == NULL)
 
565
    return;
 
566
 
 
567
  /* In case the user specifies the bss section by mach-o name.
 
568
     Create it on demand */
 
569
  if (strcmp (new_seg->name, BSS_SECTION_NAME) == 0
 
570
      && bss_section == NULL)
 
571
    bss_section = new_seg;
 
572
 
 
573
  subseg_set (new_seg, 0);
 
574
 
 
575
  if (sym != NULL)
 
576
    {
 
577
      char *pfrag;
 
578
 
 
579
      if (align)
 
580
        {
 
581
          record_alignment (new_seg, align);
 
582
          frag_align (align, 0, 0);
 
583
        }
 
584
 
 
585
      /* Detach from old frag.  */
 
586
      if (S_GET_SEGMENT (sym) == new_seg)
 
587
        symbol_get_frag (sym)->fr_symbol = NULL;
 
588
 
 
589
      symbol_set_frag (sym, frag_now);
 
590
      pfrag = frag_var (rs_org, 1, 1, 0, sym, size, NULL);
 
591
      *pfrag = 0;
 
592
 
 
593
      S_SET_SEGMENT (sym, new_seg);
 
594
      if (new_seg == bss_section)
 
595
        S_CLEAR_EXTERNAL (sym);
 
596
    }
 
597
 
 
598
done:
 
599
  /* switch back to the section that was current before the .zerofill.  */
 
600
  subseg_set (old_seg, 0);
 
601
}
 
602
 
 
603
static segT 
 
604
obj_mach_o_segT_from_bfd_name (const char *nam, int must_succeed)
 
605
{
 
606
  const mach_o_section_name_xlat *xlat;
 
607
  const char *segn;
 
608
  segT sec;
 
609
 
 
610
  /* BFD has tables of flags and default attributes for all the sections that
 
611
     have a 'canonical' name.  */
 
612
  xlat = bfd_mach_o_section_data_for_bfd_name (stdoutput, nam, &segn);
 
613
  if (xlat == NULL)
 
614
    {
 
615
      if (must_succeed)
 
616
        as_fatal (_("BFD is out of sync with GAS, "
 
617
                     "unhandled well-known section type `%s'"), nam);
 
618
      return NULL;
 
619
    }
 
620
 
 
621
  sec = bfd_get_section_by_name (stdoutput, nam);
 
622
  if (sec == NULL)
 
623
    {
 
624
      bfd_mach_o_section *msect;
 
625
 
 
626
      sec = subseg_force_new (xlat->bfd_name, 0);
 
627
 
 
628
      /* Set default type, attributes and alignment.  */
 
629
      msect = bfd_mach_o_get_mach_o_section (sec);
 
630
      msect->flags = xlat->macho_sectype | xlat->macho_secattr;
 
631
      msect->align = xlat->sectalign;
 
632
 
 
633
      if ((msect->flags & BFD_MACH_O_SECTION_TYPE_MASK) 
 
634
          == BFD_MACH_O_S_ZEROFILL)
 
635
        seg_info (sec)->bss = 1;
 
636
    }
 
637
 
 
638
  return sec;
 
639
}
 
640
 
 
641
static const char * const known_sections[] =
 
642
{
 
643
  /*  0 */ NULL,
 
644
  /* __TEXT */
 
645
  /*  1 */ ".const",
 
646
  /*  2 */ ".static_const",
 
647
  /*  3 */ ".cstring",
 
648
  /*  4 */ ".literal4",
 
649
  /*  5 */ ".literal8",
 
650
  /*  6 */ ".literal16",
 
651
  /*  7 */ ".constructor",
 
652
  /*  8 */ ".destructor",
 
653
  /*  9 */ ".eh_frame",
 
654
  /* __DATA */
 
655
  /* 10 */ ".const_data",
 
656
  /* 11 */ ".static_data",
 
657
  /* 12 */ ".mod_init_func",
 
658
  /* 13 */ ".mod_term_func",
 
659
  /* 14 */ ".dyld",
 
660
  /* 15 */ ".cfstring"
 
661
};
 
662
 
 
663
/* Interface for a known non-optional section directive.  */
 
664
 
 
665
static void
 
666
obj_mach_o_known_section (int sect_index)
 
667
{
 
668
  segT section;
 
669
 
 
670
#ifdef md_flush_pending_output
 
671
  md_flush_pending_output ();
 
672
#endif
 
673
 
 
674
  section = obj_mach_o_segT_from_bfd_name (known_sections[sect_index], 1);
 
675
  if (section != NULL)
 
676
    subseg_set (section, 0);
 
677
 
 
678
  /* else, we leave the section as it was; there was a fatal error anyway.  */
 
679
}
 
680
 
 
681
static const char * const objc_sections[] =
 
682
{
 
683
  /*  0 */ NULL,
 
684
  /*  1 */ ".objc_class",
 
685
  /*  2 */ ".objc_meta_class",
 
686
  /*  3 */ ".objc_cat_cls_meth",
 
687
  /*  4 */ ".objc_cat_inst_meth",
 
688
  /*  5 */ ".objc_protocol",
 
689
  /*  6 */ ".objc_string_object",
 
690
  /*  7 */ ".objc_cls_meth",
 
691
  /*  8 */ ".objc_inst_meth",
 
692
  /*  9 */ ".objc_cls_refs",
 
693
  /* 10 */ ".objc_message_refs",
 
694
  /* 11 */ ".objc_symbols",
 
695
  /* 12 */ ".objc_category",
 
696
  /* 13 */ ".objc_class_vars",
 
697
  /* 14 */ ".objc_instance_vars",
 
698
  /* 15 */ ".objc_module_info",
 
699
  /* 16 */ ".cstring", /* objc_class_names Alias for .cstring */
 
700
  /* 17 */ ".cstring", /* Alias objc_meth_var_types for .cstring */
 
701
  /* 18 */ ".cstring", /* objc_meth_var_names Alias for .cstring */
 
702
  /* 19 */ ".objc_selector_strs",
 
703
  /* 20 */ ".objc_image_info", /* extension.  */
 
704
  /* 21 */ ".objc_selector_fixup", /* extension.  */
 
705
  /* 22 */ ".objc1_class_ext", /* ObjC-1 extension.  */
 
706
  /* 23 */ ".objc1_property_list", /* ObjC-1 extension.  */
 
707
  /* 24 */ ".objc1_protocol_ext" /* ObjC-1 extension.  */
 
708
};
 
709
 
 
710
/* This currently does the same as known_sections, but kept separate for
 
711
   ease of maintenance.  */
 
712
 
 
713
static void
 
714
obj_mach_o_objc_section (int sect_index)
 
715
{
 
716
  segT section;
 
717
  
 
718
#ifdef md_flush_pending_output
 
719
  md_flush_pending_output ();
 
720
#endif
 
721
 
 
722
  section = obj_mach_o_segT_from_bfd_name (objc_sections[sect_index], 1);
 
723
  if (section != NULL)
 
724
    {
 
725
      obj_mach_o_seen_objc_section = 1; /* We need to ensure that certain
 
726
                                           sections are present and in the
 
727
                                           right order.  */
 
728
      subseg_set (section, 0);
 
729
    }
 
730
 
 
731
  /* else, we leave the section as it was; there was a fatal error anyway.  */
 
732
}
 
733
 
 
734
/* Debug section directives.  */
 
735
 
 
736
static const char * const debug_sections[] =
 
737
{
 
738
  /*  0 */ NULL,
 
739
  /* __DWARF */
 
740
  /*  1 */ ".debug_frame",
 
741
  /*  2 */ ".debug_info",
 
742
  /*  3 */ ".debug_abbrev",
 
743
  /*  4 */ ".debug_aranges",
 
744
  /*  5 */ ".debug_macinfo",
 
745
  /*  6 */ ".debug_line",
 
746
  /*  7 */ ".debug_loc",
 
747
  /*  8 */ ".debug_pubnames",
 
748
  /*  9 */ ".debug_pubtypes",
 
749
  /* 10 */ ".debug_str",
 
750
  /* 11 */ ".debug_ranges",
 
751
  /* 12 */ ".debug_macro"
 
752
};
 
753
 
 
754
/* ??? Maybe these should be conditional on gdwarf-*.
 
755
   It`s also likely that we will need to be able to set them from the cfi
 
756
   code.  */
 
757
 
 
758
static void
 
759
obj_mach_o_debug_section (int sect_index)
 
760
{
 
761
  segT section;
 
762
 
 
763
#ifdef md_flush_pending_output
 
764
  md_flush_pending_output ();
 
765
#endif
 
766
 
 
767
  section = obj_mach_o_segT_from_bfd_name (debug_sections[sect_index], 1);
 
768
  if (section != NULL)
 
769
    subseg_set (section, 0);
 
770
 
 
771
  /* else, we leave the section as it was; there was a fatal error anyway.  */
 
772
}
 
773
 
 
774
/* This could be moved to the tc-xx files, but there is so little dependency
 
775
   there, that the code might as well be shared.  */
 
776
 
 
777
struct opt_tgt_sect 
 
778
{
 
779
 const char *name;
 
780
 unsigned x86_val;
 
781
 unsigned ppc_val;
 
782
};
 
783
 
 
784
/* The extensions here are for specific sections that are generated by GCC
 
785
   and Darwin system tools, but don't have directives in the `system as'.  */
 
786
 
 
787
static const struct opt_tgt_sect tgt_sections[] =
 
788
{
 
789
  /*  0 */ { NULL, 0, 0},
 
790
  /*  1 */ { ".lazy_symbol_pointer", 0, 0},
 
791
  /*  2 */ { ".lazy_symbol_pointer2", 0, 0}, /* X86 - extension */
 
792
  /*  3 */ { ".lazy_symbol_pointer3", 0, 0}, /* X86 - extension */
 
793
  /*  4 */ { ".non_lazy_symbol_pointer", 0, 0},
 
794
  /*  5 */ { ".non_lazy_symbol_pointer_x86", 0, 0}, /* X86 - extension */
 
795
  /*  6 */ { ".symbol_stub", 16, 20},
 
796
  /*  7 */ { ".symbol_stub1", 0, 16}, /* PPC - extension */
 
797
  /*  8 */ { ".picsymbol_stub", 26, 36},
 
798
  /*  9 */ { ".picsymbol_stub1", 0, 32}, /* PPC - extension */
 
799
  /* 10 */ { ".picsymbol_stub2", 25, 0}, /* X86 - extension */
 
800
  /* 11 */ { ".picsymbol_stub3", 5, 0}, /* X86 - extension  */
 
801
};
 
802
 
 
803
/* Interface for an optional section directive.  */
 
804
 
 
805
static void
 
806
obj_mach_o_opt_tgt_section (int sect_index)
 
807
{
 
808
  const struct opt_tgt_sect *tgtsct = &tgt_sections[sect_index];
 
809
  segT section;
 
810
 
 
811
#ifdef md_flush_pending_output
 
812
  md_flush_pending_output ();
 
813
#endif
 
814
 
 
815
  section = obj_mach_o_segT_from_bfd_name (tgtsct->name, 0);
 
816
  if (section == NULL)
 
817
    {
 
818
      as_bad (_("%s is not used for the selected target"), tgtsct->name);
 
819
      /* Leave the section as it is.  */
 
820
    }
 
821
  else
 
822
    {
 
823
      bfd_mach_o_section *mo_sec = bfd_mach_o_get_mach_o_section (section);
 
824
      subseg_set (section, 0);
 
825
#if defined (TC_I386)
 
826
      mo_sec->reserved2 = tgtsct->x86_val;
 
827
#elif defined (TC_PPC)
 
828
      mo_sec->reserved2 = tgtsct->ppc_val;
 
829
#else
 
830
      mo_sec->reserved2 = 0;
 
831
#endif
 
832
    }
 
833
}
 
834
 
 
835
/* We don't necessarily have the three 'base' sections on mach-o.
 
836
   Normally, we would start up with only the 'text' section defined.
 
837
   However, even that can be suppressed with (TODO) c/l option "-n".
 
838
   Thus, we have to be able to create all three sections on-demand.  */
 
839
 
 
840
static void
 
841
obj_mach_o_base_section (int sect_index)
 
842
{
 
843
  segT section;
 
844
 
 
845
#ifdef md_flush_pending_output
 
846
  md_flush_pending_output ();
 
847
#endif
 
848
 
 
849
  /* We don't support numeric (or any other) qualifications on the
 
850
     well-known section shorthands.  */
 
851
  demand_empty_rest_of_line ();
 
852
 
 
853
  switch (sect_index)
 
854
    {
 
855
      /* Handle the three sections that are globally known within GAS.
 
856
         For Mach-O, these are created on demand rather than at startup.  */
 
857
      case 1:
 
858
        if (text_section == NULL)
 
859
          text_section = obj_mach_o_segT_from_bfd_name (TEXT_SECTION_NAME, 1);
 
860
        if (obj_mach_o_is_static)
 
861
          {
 
862
            bfd_mach_o_section *mo_sec
 
863
                = bfd_mach_o_get_mach_o_section (text_section);
 
864
            mo_sec->flags &= ~BFD_MACH_O_S_ATTR_PURE_INSTRUCTIONS;
 
865
          }
 
866
        section = text_section;
 
867
        break;
 
868
      case 2:
 
869
        if (data_section == NULL)
 
870
          data_section = obj_mach_o_segT_from_bfd_name (DATA_SECTION_NAME, 1);
 
871
        section = data_section;
 
872
        break;
 
873
      case 3:
 
874
        /* ??? maybe this achieves very little, as an addition.  */
 
875
        if (bss_section == NULL)
 
876
          {
 
877
            bss_section = obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME, 1);
 
878
            seg_info (bss_section)->bss = 1;
 
879
          }
 
880
        section = bss_section;
 
881
        break;
 
882
      default:
 
883
        as_fatal (_("internal error: base section index out of range"));
 
884
        return;
 
885
        break;
 
886
    }
 
887
  subseg_set (section, 0);
 
888
}
 
889
 
 
890
/* This finishes off parsing a .comm or .lcomm statement, which both can have
 
891
   an (optional) alignment field.  It also allows us to create the bss section
 
892
   on demand.  */
 
893
 
 
894
static symbolS *
 
895
obj_mach_o_common_parse (int is_local, symbolS *symbolP,
 
896
                         addressT size)
 
897
{
 
898
  addressT align = 0;
 
899
  bfd_mach_o_asymbol *s;
 
900
 
 
901
  SKIP_WHITESPACE ();  
 
902
 
 
903
  /* Both comm and lcomm take an optional alignment, as a power
 
904
     of two between 1 and 15.  */
 
905
  if (*input_line_pointer == ',')
 
906
    {
 
907
      /* We expect a power of 2.  */
 
908
      align = parse_align (0);
 
909
      if (align == (addressT) -1)
 
910
        return NULL;
 
911
      if (align > 15)
 
912
        {
 
913
          as_warn (_("Alignment (%lu) too large: 15 assumed."),
 
914
                  (unsigned long)align);
 
915
          align = 15;
 
916
        }
 
917
    }
 
918
 
 
919
  s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (symbolP);
 
920
  if (is_local)
 
921
    {
 
922
      /* Create the BSS section on demand.  */
 
923
      if (bss_section == NULL)
 
924
        {
 
925
          bss_section = obj_mach_o_segT_from_bfd_name (BSS_SECTION_NAME, 1);
 
926
          seg_info (bss_section)->bss = 1;        
 
927
        }
 
928
      bss_alloc (symbolP, size, align);
 
929
      s->n_type = BFD_MACH_O_N_SECT;
 
930
      S_CLEAR_EXTERNAL (symbolP);
 
931
    }
 
932
  else
 
933
    {
 
934
      S_SET_VALUE (symbolP, size);
 
935
      S_SET_ALIGN (symbolP, align);
 
936
      S_SET_EXTERNAL (symbolP);
 
937
      S_SET_SEGMENT (symbolP, bfd_com_section_ptr);
 
938
      s->n_type = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
 
939
    }
 
940
 
 
941
  /* This is a data object (whatever we choose that to mean).  */
 
942
  s->symbol.flags |= BSF_OBJECT;
 
943
 
 
944
  /* We've set symbol qualifiers, so validate if you can.  */
 
945
  s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED;
 
946
 
 
947
  return symbolP;
 
948
}
 
949
 
 
950
static void
 
951
obj_mach_o_comm (int is_local)
 
952
{
 
953
  s_comm_internal (is_local, obj_mach_o_common_parse);
 
954
}
 
955
 
 
956
/* Set properties that apply to the whole file.  At present, the only
 
957
   one defined, is subsections_via_symbols.  */
 
958
 
 
959
typedef enum obj_mach_o_file_properties {
 
960
  OBJ_MACH_O_FILE_PROP_NONE = 0,
 
961
  OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS,
 
962
  OBJ_MACH_O_FILE_PROP_MAX
 
963
} obj_mach_o_file_properties;
 
964
 
 
965
static void 
 
966
obj_mach_o_fileprop (int prop)
 
967
{
 
968
  if (prop < 0 || prop >= OBJ_MACH_O_FILE_PROP_MAX)
 
969
    as_fatal (_("internal error: bad file property ID %d"), prop);
 
970
    
 
971
  switch ((obj_mach_o_file_properties) prop)
 
972
    {
 
973
      case OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS:
 
974
        obj_mach_o_subsections_by_symbols = 1;
 
975
        if (!bfd_set_private_flags (stdoutput, 
 
976
                                    BFD_MACH_O_MH_SUBSECTIONS_VIA_SYMBOLS))
 
977
          as_bad (_("failed to set subsections by symbols"));
 
978
        demand_empty_rest_of_line ();
 
979
        break;
 
980
      default:
 
981
        break;
 
982
    }
 
983
}
 
984
 
 
985
/* Temporary markers for symbol reference data.  
 
986
   Lazy will remain in place.  */
 
987
#define LAZY 0x01
 
988
#define REFE 0x02
 
989
 
 
990
/* We have a bunch of qualifiers that may be applied to symbols.
 
991
   .globl is handled here so that we might make sure that conflicting qualifiers
 
992
   are caught where possible.  */
 
993
 
 
994
typedef enum obj_mach_o_symbol_type {
 
995
  OBJ_MACH_O_SYM_UNK = 0,
 
996
  OBJ_MACH_O_SYM_LOCAL = 1,
 
997
  OBJ_MACH_O_SYM_GLOBL = 2,
 
998
  OBJ_MACH_O_SYM_REFERENCE = 3,
 
999
  OBJ_MACH_O_SYM_WEAK_REF = 4,
 
1000
  OBJ_MACH_O_SYM_LAZY_REF = 5,
 
1001
  OBJ_MACH_O_SYM_WEAK_DEF = 6,
 
1002
  OBJ_MACH_O_SYM_PRIV_EXT = 7,
 
1003
  OBJ_MACH_O_SYM_NO_DEAD_STRIP = 8,
 
1004
  OBJ_MACH_O_SYM_WEAK = 9
 
1005
} obj_mach_o_symbol_type;
 
1006
 
 
1007
/* Set Mach-O-specific symbol qualifiers. */
 
1008
 
 
1009
static int
 
1010
obj_mach_o_set_symbol_qualifier (symbolS *sym, int type)
 
1011
{
 
1012
  int is_defined;
 
1013
  bfd_mach_o_asymbol *s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (sym);
 
1014
  bfd_mach_o_section *sec;
 
1015
  int sectype = -1;
 
1016
  int err = 0;
 
1017
 
 
1018
  /* If the symbol is defined, then we can do more rigorous checking on
 
1019
     the validity of the qualifiers.  Otherwise, we are stuck with waiting 
 
1020
     until it's defined - or until write the file.
 
1021
     
 
1022
     In certain cases (e.g. when a symbol qualifier is intended to introduce
 
1023
     an undefined symbol in a stubs section) we should check that the current
 
1024
     section is appropriate to the qualifier.  */
 
1025
 
 
1026
  is_defined = s->symbol.section != bfd_und_section_ptr;
 
1027
  if (is_defined)
 
1028
    sec = bfd_mach_o_get_mach_o_section (s->symbol.section) ;
 
1029
  else
 
1030
    sec = bfd_mach_o_get_mach_o_section (now_seg) ;
 
1031
 
 
1032
  if (sec != NULL)
 
1033
    sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
 
1034
 
 
1035
  switch ((obj_mach_o_symbol_type) type)
 
1036
    {
 
1037
      case OBJ_MACH_O_SYM_LOCAL:
 
1038
        /* This is an extension over the system tools.  */
 
1039
        if (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT))
 
1040
          {
 
1041
            as_bad (_("'%s' previously declared as '%s'."), s->symbol.name,
 
1042
                      (s->n_type & BFD_MACH_O_N_PEXT) ? "private extern"
 
1043
                                                      : "global" );
 
1044
            err = 1;
 
1045
          }
 
1046
        else
 
1047
          {
 
1048
            s->n_type &= ~BFD_MACH_O_N_EXT;
 
1049
            S_CLEAR_EXTERNAL (sym);
 
1050
          }
 
1051
        break;
 
1052
 
 
1053
      case OBJ_MACH_O_SYM_PRIV_EXT:
 
1054
        s->n_type |= BFD_MACH_O_N_PEXT ;
 
1055
        s->n_desc &= ~LAZY; /* The native tool switches this off too.  */
 
1056
        /* We follow the system tools in marking PEXT as also global.  */
 
1057
        /* Fall through.  */
 
1058
 
 
1059
      case OBJ_MACH_O_SYM_GLOBL:
 
1060
        /* It's not an error to define a symbol and then make it global.  */
 
1061
        s->n_type |= BFD_MACH_O_N_EXT;
 
1062
        S_SET_EXTERNAL (sym);
 
1063
        break;
 
1064
 
 
1065
      case OBJ_MACH_O_SYM_REFERENCE:
 
1066
        if (is_defined)
 
1067
          s->n_desc |= BFD_MACH_O_N_NO_DEAD_STRIP;
 
1068
        else
 
1069
          s->n_desc |= (REFE | BFD_MACH_O_N_NO_DEAD_STRIP);
 
1070
        break;
 
1071
 
 
1072
      case OBJ_MACH_O_SYM_LAZY_REF:
 
1073
        if (is_defined)
 
1074
          s->n_desc |= BFD_MACH_O_N_NO_DEAD_STRIP;
 
1075
        else
 
1076
          s->n_desc |= (REFE | LAZY | BFD_MACH_O_N_NO_DEAD_STRIP);
 
1077
        break;
 
1078
 
 
1079
      /* Force ld to retain the symbol - even if it appears unused.  */
 
1080
      case OBJ_MACH_O_SYM_NO_DEAD_STRIP:
 
1081
        s->n_desc |= BFD_MACH_O_N_NO_DEAD_STRIP ;
 
1082
        break;
 
1083
 
 
1084
      /* Mach-O's idea of weak ...  */
 
1085
      case OBJ_MACH_O_SYM_WEAK_REF:
 
1086
        s->n_desc |= BFD_MACH_O_N_WEAK_REF ;
 
1087
        break;
 
1088
 
 
1089
      case OBJ_MACH_O_SYM_WEAK_DEF:
 
1090
        if (is_defined && sectype != BFD_MACH_O_S_COALESCED)
 
1091
          {
 
1092
            as_bad (_("'%s' can't be a weak_definition (currently only"
 
1093
                      " supported in sections of type coalesced)"),
 
1094
                      s->symbol.name);
 
1095
            err = 1;
 
1096
          }
 
1097
        else
 
1098
          s->n_desc |= BFD_MACH_O_N_WEAK_DEF;
 
1099
        break;
 
1100
 
 
1101
      case OBJ_MACH_O_SYM_WEAK:
 
1102
        /* A generic 'weak' - we try to figure out what it means at
 
1103
           symbol frob time.  */
 
1104
        S_SET_WEAK (sym);
 
1105
        break;
 
1106
 
 
1107
      default:
 
1108
        break;
 
1109
    }
 
1110
 
 
1111
    /* We've seen some kind of qualifier - check validity if or when the entity
 
1112
     is defined.  */
 
1113
  s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED;
 
1114
  return err;
 
1115
}
 
1116
 
 
1117
/* Respond to symbol qualifiers.
 
1118
   All of the form:
 
1119
   .<qualifier> symbol [, symbol]*
 
1120
   a list of symbols is an extension over the Darwin system as.  */
 
1121
 
 
1122
static void
 
1123
obj_mach_o_sym_qual (int ntype)
 
1124
{
 
1125
  char *name;
 
1126
  char c;
 
1127
  symbolS *symbolP;
 
1128
 
 
1129
#ifdef md_flush_pending_output
 
1130
  md_flush_pending_output ();
 
1131
#endif
 
1132
 
 
1133
  do
 
1134
    {
 
1135
      name = input_line_pointer;
 
1136
      c = get_symbol_end ();
 
1137
      symbolP = symbol_find_or_make (name);
 
1138
      obj_mach_o_set_symbol_qualifier (symbolP, ntype);
 
1139
      *input_line_pointer = c;
 
1140
      SKIP_WHITESPACE ();
 
1141
      c = *input_line_pointer;
 
1142
      if (c == ',')
 
1143
        {
 
1144
          input_line_pointer++;
 
1145
          SKIP_WHITESPACE ();
 
1146
          if (is_end_of_line[(unsigned char) *input_line_pointer])
 
1147
            c = '\n';
 
1148
        }
 
1149
    }
 
1150
  while (c == ',');
 
1151
 
 
1152
  demand_empty_rest_of_line ();
 
1153
}
 
1154
 
 
1155
typedef struct obj_mach_o_indirect_sym
 
1156
{
 
1157
  symbolS *sym;
 
1158
  segT sect;
 
1159
  struct obj_mach_o_indirect_sym *next;
 
1160
} obj_mach_o_indirect_sym;
 
1161
 
 
1162
/* We store in order an maintain a pointer to the last one - to save reversing
 
1163
   later.  */
 
1164
obj_mach_o_indirect_sym *indirect_syms;
 
1165
obj_mach_o_indirect_sym *indirect_syms_tail;
 
1166
 
 
1167
static void
 
1168
obj_mach_o_indirect_symbol (int arg ATTRIBUTE_UNUSED)
 
1169
{
 
1170
  bfd_mach_o_section *sec = bfd_mach_o_get_mach_o_section (now_seg);
 
1171
 
 
1172
#ifdef md_flush_pending_output
 
1173
  md_flush_pending_output ();
 
1174
#endif
 
1175
 
 
1176
  if (obj_mach_o_is_static)
 
1177
    as_bad (_("use of .indirect_symbols requires `-dynamic'"));
 
1178
 
 
1179
  switch (sec->flags & BFD_MACH_O_SECTION_TYPE_MASK)
 
1180
    {
 
1181
      case BFD_MACH_O_S_SYMBOL_STUBS:
 
1182
      case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
 
1183
      case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
 
1184
        {
 
1185
          obj_mach_o_indirect_sym *isym;
 
1186
          char *name = input_line_pointer;
 
1187
          char c = get_symbol_end ();
 
1188
          symbolS *sym = symbol_find_or_make (name);
 
1189
          unsigned int elsize =
 
1190
                        bfd_mach_o_section_get_entry_size (stdoutput, sec);
 
1191
 
 
1192
          if (elsize == 0)
 
1193
            {
 
1194
              as_bad (_("attempt to add an indirect_symbol to a stub or"
 
1195
                        " reference section with a zero-sized element at %s"),
 
1196
                        name);
 
1197
              *input_line_pointer = c;
 
1198
              ignore_rest_of_line ();
 
1199
              return;
 
1200
          }
 
1201
          *input_line_pointer = c;
 
1202
 
 
1203
          /* The indirect symbols are validated after the symbol table is 
 
1204
             frozen, we must make sure that if a local symbol is used as an 
 
1205
             indirect, it is promoted to a 'real' one.  Fetching the bfd sym
 
1206
             achieves this.  */
 
1207
          symbol_get_bfdsym (sym);
 
1208
          isym = (obj_mach_o_indirect_sym *)
 
1209
                        xmalloc (sizeof (obj_mach_o_indirect_sym));
 
1210
 
 
1211
          /* Just record the data for now, we will validate it when we
 
1212
             compute the output in obj_mach_o_set_indirect_symbols.  */
 
1213
          isym->sym = sym;
 
1214
          isym->sect = now_seg;
 
1215
          isym->next = NULL;
 
1216
          if (indirect_syms == NULL)
 
1217
            indirect_syms = isym;
 
1218
          else
 
1219
            indirect_syms_tail->next = isym;
 
1220
          indirect_syms_tail = isym;
 
1221
        }
 
1222
        break;
 
1223
 
 
1224
      default:
 
1225
        as_bad (_("an .indirect_symbol must be in a symbol pointer"
 
1226
                  " or stub section."));
 
1227
        ignore_rest_of_line ();
 
1228
        return;
 
1229
    }
 
1230
  demand_empty_rest_of_line ();
 
1231
}
 
1232
 
 
1233
const pseudo_typeS mach_o_pseudo_table[] =
 
1234
{
 
1235
  /* Section directives.  */
 
1236
  { "comm", obj_mach_o_comm, 0 },
 
1237
  { "lcomm", obj_mach_o_comm, 1 },
 
1238
 
 
1239
  { "text", obj_mach_o_base_section, 1},
 
1240
  { "data", obj_mach_o_base_section, 2},
 
1241
  { "bss", obj_mach_o_base_section, 3},   /* extension */
 
1242
 
 
1243
  { "const", obj_mach_o_known_section, 1},
 
1244
  { "static_const", obj_mach_o_known_section, 2},
 
1245
  { "cstring", obj_mach_o_known_section, 3},
 
1246
  { "literal4", obj_mach_o_known_section, 4},
 
1247
  { "literal8", obj_mach_o_known_section, 5},
 
1248
  { "literal16", obj_mach_o_known_section, 6},
 
1249
  { "constructor", obj_mach_o_known_section, 7},
 
1250
  { "destructor", obj_mach_o_known_section, 8},
 
1251
  { "eh_frame", obj_mach_o_known_section, 9},
 
1252
 
 
1253
  { "const_data", obj_mach_o_known_section, 10},
 
1254
  { "static_data", obj_mach_o_known_section, 11},
 
1255
  { "mod_init_func", obj_mach_o_known_section, 12},
 
1256
  { "mod_term_func", obj_mach_o_known_section, 13},
 
1257
  { "dyld", obj_mach_o_known_section, 14},
 
1258
  { "cfstring", obj_mach_o_known_section, 15},
 
1259
 
 
1260
  { "objc_class", obj_mach_o_objc_section, 1},
 
1261
  { "objc_meta_class", obj_mach_o_objc_section, 2},
 
1262
  { "objc_cat_cls_meth", obj_mach_o_objc_section, 3},
 
1263
  { "objc_cat_inst_meth", obj_mach_o_objc_section, 4},
 
1264
  { "objc_protocol", obj_mach_o_objc_section, 5},
 
1265
  { "objc_string_object", obj_mach_o_objc_section, 6},
 
1266
  { "objc_cls_meth", obj_mach_o_objc_section, 7},
 
1267
  { "objc_inst_meth", obj_mach_o_objc_section, 8},
 
1268
  { "objc_cls_refs", obj_mach_o_objc_section, 9},
 
1269
  { "objc_message_refs", obj_mach_o_objc_section, 10},
 
1270
  { "objc_symbols", obj_mach_o_objc_section, 11},
 
1271
  { "objc_category", obj_mach_o_objc_section, 12},
 
1272
  { "objc_class_vars", obj_mach_o_objc_section, 13},
 
1273
  { "objc_instance_vars", obj_mach_o_objc_section, 14},
 
1274
  { "objc_module_info", obj_mach_o_objc_section, 15},
 
1275
  { "objc_class_names", obj_mach_o_objc_section, 16}, /* Alias for .cstring */
 
1276
  { "objc_meth_var_types", obj_mach_o_objc_section, 17}, /* Alias for .cstring */
 
1277
  { "objc_meth_var_names", obj_mach_o_objc_section, 18}, /* Alias for .cstring */
 
1278
  { "objc_selector_strs", obj_mach_o_objc_section, 19},
 
1279
  { "objc_image_info", obj_mach_o_objc_section, 20}, /* extension.  */
 
1280
  { "objc_selector_fixup", obj_mach_o_objc_section, 21}, /* extension.  */
 
1281
  { "objc1_class_ext", obj_mach_o_objc_section, 22}, /* ObjC-1 extension.  */
 
1282
  { "objc1_property_list", obj_mach_o_objc_section, 23}, /* ObjC-1 extension.  */
 
1283
  { "objc1_protocol_ext", obj_mach_o_objc_section, 24}, /* ObjC-1 extension.  */
 
1284
 
 
1285
  { "debug_frame", obj_mach_o_debug_section, 1}, /* extension.  */
 
1286
  { "debug_info", obj_mach_o_debug_section, 2}, /* extension.  */
 
1287
  { "debug_abbrev", obj_mach_o_debug_section, 3}, /* extension.  */
 
1288
  { "debug_aranges", obj_mach_o_debug_section, 4}, /* extension.  */
 
1289
  { "debug_macinfo", obj_mach_o_debug_section, 5}, /* extension.  */
 
1290
  { "debug_line", obj_mach_o_debug_section, 6}, /* extension.  */
 
1291
  { "debug_loc", obj_mach_o_debug_section, 7}, /* extension.  */
 
1292
  { "debug_pubnames", obj_mach_o_debug_section, 8}, /* extension.  */
 
1293
  { "debug_pubtypes", obj_mach_o_debug_section, 9}, /* extension.  */
 
1294
  { "debug_str", obj_mach_o_debug_section, 10}, /* extension.  */
 
1295
  { "debug_ranges", obj_mach_o_debug_section, 11}, /* extension.  */
 
1296
  { "debug_macro", obj_mach_o_debug_section, 12}, /* extension.  */
 
1297
  
 
1298
  { "lazy_symbol_pointer", obj_mach_o_opt_tgt_section, 1},
 
1299
  { "lazy_symbol_pointer2", obj_mach_o_opt_tgt_section, 2}, /* extension.  */
 
1300
  { "lazy_symbol_pointer3", obj_mach_o_opt_tgt_section, 3}, /* extension.  */
 
1301
  { "non_lazy_symbol_pointer", obj_mach_o_opt_tgt_section, 4},
 
1302
  { "non_lazy_symbol_pointer_x86", obj_mach_o_opt_tgt_section, 5}, /* extension.  */
 
1303
  { "symbol_stub", obj_mach_o_opt_tgt_section, 6},
 
1304
  { "symbol_stub1", obj_mach_o_opt_tgt_section, 7}, /* extension.  */
 
1305
  { "picsymbol_stub", obj_mach_o_opt_tgt_section, 8}, /* extension.  */
 
1306
  { "picsymbol_stub1", obj_mach_o_opt_tgt_section, 9}, /* extension.  */
 
1307
  { "picsymbol_stub2", obj_mach_o_opt_tgt_section, 4}, /* extension.  */
 
1308
  { "picsymbol_stub3", obj_mach_o_opt_tgt_section, 4}, /* extension.  */
 
1309
 
 
1310
  { "section", obj_mach_o_section, 0},
 
1311
  { "zerofill", obj_mach_o_zerofill, 0},
 
1312
 
 
1313
  /* Symbol qualifiers.  */
 
1314
  {"local",             obj_mach_o_sym_qual, OBJ_MACH_O_SYM_LOCAL},
 
1315
  {"globl",             obj_mach_o_sym_qual, OBJ_MACH_O_SYM_GLOBL},
 
1316
  {"reference",         obj_mach_o_sym_qual, OBJ_MACH_O_SYM_REFERENCE},
 
1317
  {"weak_reference",    obj_mach_o_sym_qual, OBJ_MACH_O_SYM_WEAK_REF},
 
1318
  {"lazy_reference",    obj_mach_o_sym_qual, OBJ_MACH_O_SYM_LAZY_REF},
 
1319
  {"weak_definition",   obj_mach_o_sym_qual, OBJ_MACH_O_SYM_WEAK_DEF},
 
1320
  {"private_extern",    obj_mach_o_sym_qual, OBJ_MACH_O_SYM_PRIV_EXT},
 
1321
  {"no_dead_strip",     obj_mach_o_sym_qual, OBJ_MACH_O_SYM_NO_DEAD_STRIP},
 
1322
  {"weak",              obj_mach_o_sym_qual, OBJ_MACH_O_SYM_WEAK}, /* ext */
 
1323
 
 
1324
  { "indirect_symbol",  obj_mach_o_indirect_symbol, 0},
 
1325
 
 
1326
  /* File flags.  */
 
1327
  { "subsections_via_symbols", obj_mach_o_fileprop, 
 
1328
                               OBJ_MACH_O_FILE_PROP_SUBSECTS_VIA_SYMS},
 
1329
 
 
1330
  {NULL, NULL, 0}
 
1331
};
 
1332
 
 
1333
/* Determine the default n_type value for a symbol from its section.  */
 
1334
 
 
1335
static unsigned
 
1336
obj_mach_o_type_for_symbol (bfd_mach_o_asymbol *s)
 
1337
{
 
1338
  if (s->symbol.section == bfd_abs_section_ptr)
 
1339
    return BFD_MACH_O_N_ABS;
 
1340
  else if (s->symbol.section == bfd_com_section_ptr
 
1341
           || s->symbol.section == bfd_und_section_ptr)
 
1342
    return BFD_MACH_O_N_UNDF;
 
1343
  else
 
1344
    return BFD_MACH_O_N_SECT;
 
1345
}
 
1346
 
 
1347
void
 
1348
obj_mach_o_frob_colon (const char *name)
 
1349
{
 
1350
  if (!bfd_is_local_label_name (stdoutput, name))
 
1351
    {
 
1352
      /* A non-local label will create a new subsection, so start a new
 
1353
         frag.  */
 
1354
      frag_wane (frag_now);
 
1355
      frag_new (0);
 
1356
    }
 
1357
}
 
1358
 
 
1359
/* We need to check the correspondence between some kinds of symbols and their
 
1360
   sections.  Common and BSS vars will seen via the obj_macho_comm() function.
 
1361
   
 
1362
   The earlier we can pick up a problem, the better the diagnostics will be.
 
1363
   
 
1364
   However, when symbol type information is attached, the symbol section will
 
1365
   quite possibly be unknown.  So we are stuck with checking (most of the)
 
1366
   validity at the time the file is written (unfortunately, then one doesn't
 
1367
   get line number information in the diagnostic).  */
 
1368
 
 
1369
/* Here we pick up the case where symbol qualifiers have been applied that
 
1370
   are possibly incompatible with the section etc. that the symbol is defined
 
1371
   in.  */
 
1372
 
 
1373
void obj_mach_o_frob_label (struct symbol *sp)
 
1374
{
 
1375
  bfd_mach_o_asymbol *s;
 
1376
  unsigned base_type;
 
1377
  bfd_mach_o_section *sec;
 
1378
  int sectype = -1;
 
1379
 
 
1380
  if (!bfd_is_local_label_name (stdoutput, S_GET_NAME (sp)))
 
1381
    {
 
1382
      /* If this is a non-local label, it should have started a new sub-
 
1383
         section.  */
 
1384
      gas_assert (frag_now->obj_frag_data.subsection == NULL);
 
1385
      frag_now->obj_frag_data.subsection = sp;
 
1386
    }
 
1387
 
 
1388
  /* Leave local symbols alone.  */
 
1389
 
 
1390
  if (S_IS_LOCAL (sp))
 
1391
    return;
 
1392
 
 
1393
  s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (sp);
 
1394
  /* Leave debug symbols alone.  */
 
1395
  if ((s->n_type & BFD_MACH_O_N_STAB) != 0)
 
1396
    return;
 
1397
 
 
1398
  /* This is the base symbol type, that we mask in.  */
 
1399
  base_type = obj_mach_o_type_for_symbol (s);
 
1400
 
 
1401
  sec = bfd_mach_o_get_mach_o_section (s->symbol.section);  
 
1402
  if (sec != NULL)
 
1403
    sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
 
1404
 
 
1405
  /* If there is a pre-existing qualifier, we can make some checks about
 
1406
     validity now.  */
 
1407
 
 
1408
  if(s->symbol.udata.i == SYM_MACHO_FIELDS_NOT_VALIDATED)
 
1409
    {
 
1410
      if ((s->n_desc & BFD_MACH_O_N_WEAK_DEF)
 
1411
          && sectype != BFD_MACH_O_S_COALESCED)
 
1412
        as_bad (_("'%s' can't be a weak_definition (currently only supported"
 
1413
                  " in sections of type coalesced)"), s->symbol.name);
 
1414
 
 
1415
      /* Have we changed from an undefined to defined ref? */
 
1416
      s->n_desc &= ~(REFE | LAZY);
 
1417
    }
 
1418
 
 
1419
  s->n_type &= ~BFD_MACH_O_N_TYPE;
 
1420
  s->n_type |= base_type;
 
1421
}
 
1422
 
 
1423
/* This is the fall-back, we come here when we get to the end of the file and
 
1424
   the symbol is not defined - or there are combinations of qualifiers required
 
1425
   (e.g. global + weak_def).  */
 
1426
 
 
1427
int
 
1428
obj_mach_o_frob_symbol (struct symbol *sp)
 
1429
{
 
1430
  bfd_mach_o_asymbol *s;
 
1431
  unsigned base_type;
 
1432
  bfd_mach_o_section *sec;
 
1433
  int sectype = -1;
 
1434
 
 
1435
  /* Leave local symbols alone.  */
 
1436
  if (S_IS_LOCAL (sp))
 
1437
    return 0;
 
1438
 
 
1439
  s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (sp);
 
1440
  /* Leave debug symbols alone.  */
 
1441
  if ((s->n_type & BFD_MACH_O_N_STAB) != 0)
 
1442
    return 0;
 
1443
 
 
1444
  base_type = obj_mach_o_type_for_symbol (s);
 
1445
  sec = bfd_mach_o_get_mach_o_section (s->symbol.section);  
 
1446
  if (sec != NULL)
 
1447
    sectype = sec->flags & BFD_MACH_O_SECTION_TYPE_MASK;
 
1448
 
 
1449
  if (s->symbol.section == bfd_und_section_ptr)
 
1450
    {
 
1451
      /* ??? Do we really gain much from implementing this as well as the
 
1452
         mach-o specific ones?  */
 
1453
      if (s->symbol.flags & BSF_WEAK)
 
1454
        s->n_desc |= BFD_MACH_O_N_WEAK_REF;
 
1455
 
 
1456
      /* Undefined syms, become extern.  */
 
1457
      s->n_type |= BFD_MACH_O_N_EXT;
 
1458
      S_SET_EXTERNAL (sp);
 
1459
    }
 
1460
  else if (s->symbol.section == bfd_com_section_ptr)
 
1461
    {
 
1462
      /* ... so do comm.  */
 
1463
      s->n_type |= BFD_MACH_O_N_EXT;
 
1464
      S_SET_EXTERNAL (sp);
 
1465
    }
 
1466
  else
 
1467
    {
 
1468
      if ((s->symbol.flags & BSF_WEAK)
 
1469
           && (sectype == BFD_MACH_O_S_COALESCED)
 
1470
           && (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
 
1471
        s->n_desc |= BFD_MACH_O_N_WEAK_DEF;
 
1472
/* ??? we should do this - but then that reveals that the semantics of weak
 
1473
       are different from what's supported in mach-o object files.
 
1474
      else
 
1475
        as_bad (_("'%s' can't be a weak_definition."),
 
1476
                s->symbol.name); */
 
1477
    }
 
1478
 
 
1479
  if (s->symbol.udata.i == SYM_MACHO_FIELDS_UNSET)
 
1480
    {
 
1481
      /* Anything here that should be added that is non-standard.  */
 
1482
      s->n_desc &= ~BFD_MACH_O_REFERENCE_MASK;
 
1483
      s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED;
 
1484
    }    
 
1485
  else if (s->symbol.udata.i == SYM_MACHO_FIELDS_NOT_VALIDATED)
 
1486
    {
 
1487
      /* Try to validate any combinations.  */
 
1488
      if (s->n_desc & BFD_MACH_O_N_WEAK_DEF)
 
1489
        {
 
1490
          if (s->symbol.section == bfd_und_section_ptr)
 
1491
            as_bad (_("'%s' can't be a weak_definition (since it is"
 
1492
                      " undefined)"), s->symbol.name);
 
1493
          else if (sectype != BFD_MACH_O_S_COALESCED)
 
1494
            as_bad (_("'%s' can't be a weak_definition (currently only supported"
 
1495
                      " in sections of type coalesced)"), s->symbol.name);
 
1496
          else if (! (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT)))
 
1497
            as_bad (_("Non-global symbol: '%s' can't be a weak_definition."),
 
1498
                    s->symbol.name);
 
1499
        }
 
1500
 
 
1501
    }
 
1502
  else
 
1503
    as_bad (_("internal error: [%s] unexpected code [%lx] in frob symbol"),
 
1504
            s->symbol.name, (unsigned long)s->symbol.udata.i);
 
1505
 
 
1506
  s->n_type &= ~BFD_MACH_O_N_TYPE;
 
1507
  s->n_type |= base_type;
 
1508
 
 
1509
  if (s->symbol.flags & BSF_GLOBAL)
 
1510
    s->n_type |= BFD_MACH_O_N_EXT;
 
1511
 
 
1512
  /* This cuts both ways - we promote some things to external above.  */
 
1513
  if (s->n_type & (BFD_MACH_O_N_PEXT | BFD_MACH_O_N_EXT))
 
1514
    S_SET_EXTERNAL (sp);
 
1515
 
 
1516
  return 0;
 
1517
}
 
1518
 
 
1519
/* Support stabs for mach-o.  */
 
1520
 
 
1521
void
 
1522
obj_mach_o_process_stab (int what, const char *string,
 
1523
                         int type, int other, int desc)
 
1524
{
 
1525
  symbolS *symbolP;
 
1526
  bfd_mach_o_asymbol *s;
 
1527
 
 
1528
  switch (what)
 
1529
    {
 
1530
      case 'd':
 
1531
        symbolP = symbol_new ("", now_seg, frag_now_fix (), frag_now);
 
1532
        /* Special stabd NULL name indicator.  */
 
1533
        S_SET_NAME (symbolP, NULL);
 
1534
        break;
 
1535
 
 
1536
      case 'n':
 
1537
      case 's':
 
1538
        symbolP = symbol_new (string, undefined_section, (valueT) 0,
 
1539
                              &zero_address_frag);
 
1540
        pseudo_set (symbolP);
 
1541
        break;
 
1542
 
 
1543
      default:
 
1544
        as_bad(_("unrecognized stab type '%c'"), (char)what);
 
1545
        abort ();
 
1546
        break;
 
1547
    }
 
1548
 
 
1549
  s = (bfd_mach_o_asymbol *) symbol_get_bfdsym (symbolP);
 
1550
  s->n_type = type;
 
1551
  s->n_desc = desc;
 
1552
  /* For stabd, this will eventually get overwritten by the section number.  */
 
1553
  s->n_sect = other;
 
1554
 
 
1555
  /* It's a debug symbol.  */
 
1556
  s->symbol.flags |= BSF_DEBUGGING;
 
1557
  
 
1558
  /* We've set it - so check it, if you can, but don't try to create the
 
1559
     flags.  */
 
1560
  s->symbol.udata.i = SYM_MACHO_FIELDS_NOT_VALIDATED;
 
1561
}
 
1562
 
 
1563
/* This is a place to check for any errors that we can't detect until we know
 
1564
   what remains undefined at the end of assembly.  */
 
1565
 
 
1566
static void
 
1567
obj_mach_o_check_before_writing (bfd *abfd ATTRIBUTE_UNUSED,
 
1568
                                 asection *sec,
 
1569
                                 void *unused ATTRIBUTE_UNUSED)
 
1570
{
 
1571
  fixS *fixP;
 
1572
  struct frchain *frchp;
 
1573
  segment_info_type *seginfo = seg_info (sec);
 
1574
 
 
1575
  if (seginfo == NULL)
 
1576
    return;
 
1577
 
 
1578
  /* We are not allowed subtractions where either of the operands is
 
1579
     undefined.  So look through the frags for any fixes to check.  */
 
1580
  for (frchp = seginfo->frchainP; frchp != NULL; frchp = frchp->frch_next)
 
1581
   for (fixP = frchp->fix_root; fixP != NULL; fixP = fixP->fx_next)
 
1582
    {
 
1583
      if (fixP->fx_addsy != NULL
 
1584
          && fixP->fx_subsy != NULL
 
1585
          && (! S_IS_DEFINED (fixP->fx_addsy)
 
1586
              || ! S_IS_DEFINED (fixP->fx_subsy)))
 
1587
        {
 
1588
          segT add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
 
1589
          segT sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
 
1590
 
 
1591
          if (! S_IS_DEFINED (fixP->fx_addsy)
 
1592
              && S_IS_DEFINED (fixP->fx_subsy))
 
1593
            {
 
1594
              as_bad_where (fixP->fx_file, fixP->fx_line,
 
1595
                _("`%s' can't be undefined in `%s' - `%s' {%s section}"),
 
1596
                S_GET_NAME (fixP->fx_addsy), S_GET_NAME (fixP->fx_addsy),
 
1597
                S_GET_NAME (fixP->fx_subsy), segment_name (sub_symbol_segment));
 
1598
            }
 
1599
          else if (! S_IS_DEFINED (fixP->fx_subsy)
 
1600
                   && S_IS_DEFINED (fixP->fx_addsy))
 
1601
            {
 
1602
              as_bad_where (fixP->fx_file, fixP->fx_line,
 
1603
                _("`%s' can't be undefined in `%s' {%s section} - `%s'"),
 
1604
                S_GET_NAME (fixP->fx_subsy), S_GET_NAME (fixP->fx_addsy),
 
1605
                segment_name (add_symbol_segment), S_GET_NAME (fixP->fx_subsy));
 
1606
            }
 
1607
          else
 
1608
            {
 
1609
              as_bad_where (fixP->fx_file, fixP->fx_line,
 
1610
                _("`%s' and `%s' can't be undefined in `%s' - `%s'"),
 
1611
                S_GET_NAME (fixP->fx_addsy), S_GET_NAME (fixP->fx_subsy),
 
1612
                S_GET_NAME (fixP->fx_addsy), S_GET_NAME (fixP->fx_subsy));
 
1613
            }
 
1614
        }
 
1615
    }
 
1616
}
 
1617
 
 
1618
/* Do any checks that we can't complete without knowing what's undefined.  */
 
1619
void
 
1620
obj_mach_o_pre_output_hook (void)
 
1621
{
 
1622
  bfd_map_over_sections (stdoutput, obj_mach_o_check_before_writing, (char *) 0);
 
1623
}
 
1624
 
 
1625
/* Here we count up frags in each subsection (where a sub-section is defined
 
1626
   as starting with a non-local symbol).
 
1627
   Note that, if there are no non-local symbols in a section, all the frags will
 
1628
   be attached as one anonymous subsection.  */
 
1629
 
 
1630
static void
 
1631
obj_mach_o_set_subsections (bfd *abfd ATTRIBUTE_UNUSED,
 
1632
                            asection *sec,
 
1633
                            void *unused ATTRIBUTE_UNUSED)
 
1634
{
 
1635
  segment_info_type *seginfo = seg_info (sec);
 
1636
  symbolS *cur_subsection = NULL;
 
1637
  struct obj_mach_o_symbol_data *cur_subsection_data = NULL;
 
1638
  fragS *frag;
 
1639
  frchainS *chain;
 
1640
 
 
1641
  /* Protect against sections not created by gas.  */
 
1642
  if (seginfo == NULL)
 
1643
    return;
 
1644
 
 
1645
  /* Attach every frag to a subsection.  */
 
1646
  for (chain = seginfo->frchainP; chain != NULL; chain = chain->frch_next)
 
1647
    for (frag = chain->frch_root; frag != NULL; frag = frag->fr_next)
 
1648
      {
 
1649
        if (frag->obj_frag_data.subsection == NULL)
 
1650
          frag->obj_frag_data.subsection = cur_subsection;
 
1651
        else
 
1652
          {
 
1653
            cur_subsection = frag->obj_frag_data.subsection;
 
1654
            cur_subsection_data = symbol_get_obj (cur_subsection);
 
1655
            cur_subsection_data->subsection_size = 0;
 
1656
          }
 
1657
        if (cur_subsection_data != NULL)
 
1658
          {
 
1659
            /* Update subsection size.  */
 
1660
            cur_subsection_data->subsection_size += frag->fr_fix;
 
1661
          }
 
1662
      }
 
1663
}
 
1664
 
 
1665
/* Handle mach-o subsections-via-symbols counting up frags belonging to each 
 
1666
   sub-section.  */
 
1667
 
 
1668
void
 
1669
obj_mach_o_pre_relax_hook (void)
 
1670
{
 
1671
  bfd_map_over_sections (stdoutput, obj_mach_o_set_subsections, (char *) 0);
 
1672
}
 
1673
 
 
1674
/* Zerofill and GB Zerofill sections must be sorted to follow all other
 
1675
   sections in their segments.
 
1676
 
 
1677
   The native 'as' leaves the sections physically in the order they appear in
 
1678
   the source, and adjusts the section VMAs to meet the constraint.
 
1679
   
 
1680
   We follow this for now - if nothing else, it makes comparison easier.
 
1681
 
 
1682
   An alternative implementation would be to sort the sections as ld requires.
 
1683
   It might be advantageous to implement such a scheme in the future (or even
 
1684
   to make the style of section ordering user-selectable).  */
 
1685
 
 
1686
typedef struct obj_mach_o_set_vma_data
 
1687
{
 
1688
  bfd_vma vma;
 
1689
  unsigned vma_pass;
 
1690
  unsigned zerofill_seen;
 
1691
  unsigned gb_zerofill_seen;
 
1692
} obj_mach_o_set_vma_data;
 
1693
 
 
1694
/* We do (possibly) three passes through to set the vma, so that:
 
1695
 
 
1696
   zerofill sections get VMAs after all others in their segment
 
1697
   GB zerofill get VMAs last.
 
1698
   
 
1699
   As we go, we notice if we see any Zerofill or GB Zerofill sections, so that
 
1700
   we can skip the additional passes if there's nothing to do.  */
 
1701
 
 
1702
static void
 
1703
obj_mach_o_set_section_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *v_p)
 
1704
{
 
1705
  bfd_mach_o_section *ms = bfd_mach_o_get_mach_o_section (sec);
 
1706
  unsigned bfd_align = bfd_get_section_alignment (abfd, sec);
 
1707
  obj_mach_o_set_vma_data *p = (struct obj_mach_o_set_vma_data *)v_p;
 
1708
  unsigned sectype = (ms->flags & BFD_MACH_O_SECTION_TYPE_MASK);
 
1709
  unsigned zf;
 
1710
 
 
1711
  zf = 0;
 
1712
  if (sectype == BFD_MACH_O_S_ZEROFILL)
 
1713
    {
 
1714
      zf = 1;
 
1715
      p->zerofill_seen = zf;
 
1716
    }
 
1717
  else if (sectype == BFD_MACH_O_S_GB_ZEROFILL)
 
1718
    {
 
1719
      zf = 2;
 
1720
      p->gb_zerofill_seen = zf;
 
1721
    }
 
1722
 
 
1723
  if (p->vma_pass != zf)
 
1724
    return;
 
1725
 
 
1726
  /* We know the section size now - so make a vma for the section just
 
1727
     based on order.  */
 
1728
  ms->size = bfd_get_section_size (sec);
 
1729
  
 
1730
  /* Make sure that the align agrees, and set to the largest value chosen.  */
 
1731
  ms->align = ms->align > bfd_align ? ms->align : bfd_align;
 
1732
  bfd_set_section_alignment (abfd, sec, ms->align);
 
1733
  
 
1734
  p->vma += (1 << ms->align) - 1;
 
1735
  p->vma &= ~((1 << ms->align) - 1);
 
1736
  ms->addr = p->vma;
 
1737
  bfd_set_section_vma (abfd, sec, p->vma);
 
1738
  p->vma += ms->size;
 
1739
}
 
1740
 
 
1741
/* (potentially) three passes over the sections, setting VMA.  We skip the 
 
1742
  {gb}zerofill passes if we didn't see any of the relevant sections.  */
 
1743
 
 
1744
void obj_mach_o_post_relax_hook (void)
 
1745
{
 
1746
  obj_mach_o_set_vma_data d;
 
1747
 
 
1748
  memset (&d, 0, sizeof (d));
 
1749
  
 
1750
  bfd_map_over_sections (stdoutput, obj_mach_o_set_section_vma, (char *) &d);
 
1751
  if ((d.vma_pass = d.zerofill_seen) != 0)
 
1752
    bfd_map_over_sections (stdoutput, obj_mach_o_set_section_vma, (char *) &d);
 
1753
  if ((d.vma_pass = d.gb_zerofill_seen) != 0)
 
1754
    bfd_map_over_sections (stdoutput, obj_mach_o_set_section_vma, (char *) &d);
 
1755
}
 
1756
 
 
1757
static void
 
1758
obj_mach_o_set_indirect_symbols (bfd *abfd, asection *sec,
 
1759
                                 void *xxx ATTRIBUTE_UNUSED)
 
1760
{
 
1761
  bfd_vma sect_size = bfd_section_size (abfd, sec);
 
1762
  bfd_mach_o_section *ms = bfd_mach_o_get_mach_o_section (sec);
 
1763
  unsigned lazy = 0;
 
1764
 
 
1765
  /* See if we have any indirect syms to consider.  */
 
1766
  if (indirect_syms == NULL)
 
1767
    return;
 
1768
 
 
1769
  /* Process indirect symbols.
 
1770
     Check for errors, if OK attach them as a flat array to the section
 
1771
     for which they are defined.  */
 
1772
 
 
1773
  switch (ms->flags & BFD_MACH_O_SECTION_TYPE_MASK)
 
1774
    {
 
1775
      case BFD_MACH_O_S_SYMBOL_STUBS:
 
1776
      case BFD_MACH_O_S_LAZY_SYMBOL_POINTERS:
 
1777
        lazy = LAZY;
 
1778
        /* Fall through.  */
 
1779
      case BFD_MACH_O_S_NON_LAZY_SYMBOL_POINTERS:
 
1780
        {
 
1781
          unsigned int nactual = 0;
 
1782
          unsigned int ncalc;
 
1783
          obj_mach_o_indirect_sym *isym;
 
1784
          obj_mach_o_indirect_sym *list = NULL;
 
1785
          obj_mach_o_indirect_sym *list_tail = NULL;
 
1786
          unsigned long eltsiz = 
 
1787
                        bfd_mach_o_section_get_entry_size (abfd, ms);
 
1788
 
 
1789
          for (isym = indirect_syms; isym != NULL; isym = isym->next)
 
1790
            {
 
1791
              if (isym->sect == sec)
 
1792
                {
 
1793
                  nactual++;
 
1794
                  if (list == NULL)
 
1795
                    list = isym;
 
1796
                  else
 
1797
                    list_tail->next = isym;
 
1798
                  list_tail = isym;
 
1799
                }
 
1800
            }
 
1801
 
 
1802
          /* If none are in this section, stop here.  */
 
1803
          if (nactual == 0)
 
1804
            break;
 
1805
 
 
1806
          /* If we somehow added indirect symbols to a section with a zero
 
1807
             entry size, we're dead ... */
 
1808
          gas_assert (eltsiz != 0);
 
1809
 
 
1810
          ncalc = (unsigned int) (sect_size / eltsiz);
 
1811
          if (nactual != ncalc)
 
1812
            as_bad (_("the number of .indirect_symbols defined in section %s"
 
1813
                      " does not match the number expected (%d defined, %d"
 
1814
                      " expected)"), sec->name, nactual, ncalc);
 
1815
          else
 
1816
            {
 
1817
              unsigned n;
 
1818
              bfd_mach_o_asymbol *sym;
 
1819
              ms->indirect_syms =
 
1820
                        bfd_zalloc (abfd,
 
1821
                                    nactual * sizeof (bfd_mach_o_asymbol *));
 
1822
 
 
1823
              if (ms->indirect_syms == NULL)
 
1824
                {
 
1825
                  as_fatal (_("internal error: failed to allocate %d indirect"
 
1826
                              "symbol pointers"), nactual);
 
1827
                }
 
1828
              
 
1829
              for (isym = list, n = 0; isym != NULL; isym = isym->next, n++)
 
1830
                {
 
1831
                  sym = (bfd_mach_o_asymbol *)symbol_get_bfdsym (isym->sym);
 
1832
                  /* Array is init to NULL & NULL signals a local symbol
 
1833
                     If the section is lazy-bound, we need to keep the
 
1834
                     reference to the symbol, since dyld can override.
 
1835
                     
 
1836
                     Absolute symbols are handled specially.  */
 
1837
                  if (sym->symbol.section == bfd_abs_section_ptr)
 
1838
                    ms->indirect_syms[n] = sym;
 
1839
                  else if (S_IS_LOCAL (isym->sym) && ! lazy)
 
1840
                    ;
 
1841
                  else
 
1842
                    {
 
1843
                      if (sym == NULL)
 
1844
                        ;
 
1845
                      /* If the symbols is external ...  */
 
1846
                      else if (S_IS_EXTERNAL (isym->sym)
 
1847
                               || (sym->n_type & BFD_MACH_O_N_EXT)
 
1848
                               || ! S_IS_DEFINED (isym->sym)
 
1849
                               || lazy)
 
1850
                        {
 
1851
                          sym->n_desc &= ~LAZY;
 
1852
                          /* ... it can be lazy, if not defined or hidden.  */
 
1853
                          if ((sym->n_type & BFD_MACH_O_N_TYPE) 
 
1854
                               == BFD_MACH_O_N_UNDF 
 
1855
                              && ! (sym->n_type & BFD_MACH_O_N_PEXT)
 
1856
                              && (sym->n_type & BFD_MACH_O_N_EXT))
 
1857
                            sym->n_desc |= lazy;
 
1858
                          ms->indirect_syms[n] = sym;
 
1859
                        }
 
1860
                    }
 
1861
                }
 
1862
            }
 
1863
        }
 
1864
        break;
 
1865
 
 
1866
      default:
 
1867
        break;
 
1868
    }
 
1869
}
 
1870
 
 
1871
/* The process of relocation could alter what's externally visible, thus we
 
1872
   leave setting the indirect symbols until last.  */
 
1873
 
 
1874
void
 
1875
obj_mach_o_frob_file_after_relocs (void)
 
1876
{
 
1877
  bfd_map_over_sections (stdoutput, obj_mach_o_set_indirect_symbols, (char *) 0);
 
1878
}
 
1879
 
 
1880
/* Reverse relocations order to make ld happy.  */
 
1881
 
 
1882
void
 
1883
obj_mach_o_reorder_section_relocs (asection *sec, arelent **rels, unsigned int n)
 
1884
{
 
1885
  unsigned int i;
 
1886
  unsigned int max = n / 2;
 
1887
 
 
1888
  for (i = 0; i < max; i++)
 
1889
    {
 
1890
      arelent *r = rels[i];
 
1891
      rels[i] = rels[n - i - 1];
 
1892
      rels[n - i - 1] = r;
 
1893
    }
 
1894
  bfd_set_reloc (stdoutput, sec, rels, n);
 
1895
}
 
1896
 
 
1897
/* Relocation rules are different in frame sections.  */
 
1898
 
 
1899
static int
 
1900
obj_mach_o_is_frame_section (segT sec)
 
1901
{
 
1902
  int l;
 
1903
  l = strlen (segment_name (sec));
 
1904
  if ((l == 9 && strncmp (".eh_frame", segment_name (sec), 9) == 0)
 
1905
       || (l == 12 && strncmp (".debug_frame", segment_name (sec), 12) == 0))
 
1906
    return 1;
 
1907
  return 0;
 
1908
}
 
1909
 
 
1910
/* Unless we're in a frame section, we need to force relocs to be generated for
 
1911
   local subtractions.  We might eliminate them later (if they are within the
 
1912
   same sub-section) but we don't know that at the point that this decision is
 
1913
   being made.  */
 
1914
 
 
1915
int
 
1916
obj_mach_o_allow_local_subtract (expressionS * left ATTRIBUTE_UNUSED, 
 
1917
                                 expressionS * right ATTRIBUTE_UNUSED,
 
1918
                                 segT seg)
 
1919
{
 
1920
  /* Don't interfere if it's one of the GAS internal sections.  */
 
1921
  if (! SEG_NORMAL (seg))
 
1922
    return 1;
 
1923
 
 
1924
  /* Allow in frame sections, otherwise emit a reloc.  */
 
1925
  return obj_mach_o_is_frame_section (seg);
 
1926
}
 
1927
 
 
1928
int
 
1929
obj_mach_o_in_different_subsection (symbolS *a, symbolS *b)
 
1930
{
 
1931
  fragS *fa;
 
1932
  fragS *fb;
 
1933
 
 
1934
  if (S_GET_SEGMENT (a) != S_GET_SEGMENT (b)
 
1935
      || !S_IS_DEFINED (a)
 
1936
      || !S_IS_DEFINED (b))
 
1937
    {
 
1938
      /* Not in the same segment, or undefined symbol.  */
 
1939
      return 1;
 
1940
    }
 
1941
 
 
1942
  fa = symbol_get_frag (a);
 
1943
  fb = symbol_get_frag (b);
 
1944
  if (fa == NULL || fb == NULL)
 
1945
    {
 
1946
      /* One of the symbols is not in a subsection.  */
 
1947
      return 1;
 
1948
    }
 
1949
 
 
1950
  return fa->obj_frag_data.subsection != fb->obj_frag_data.subsection;
 
1951
}
 
1952
 
 
1953
int
 
1954
obj_mach_o_force_reloc_sub_same (fixS *fix, segT seg)
 
1955
{
 
1956
  if (! SEG_NORMAL (seg))
 
1957
    return 1;
 
1958
  return obj_mach_o_in_different_subsection (fix->fx_addsy, fix->fx_subsy);
 
1959
}
 
1960
 
 
1961
int
 
1962
obj_mach_o_force_reloc_sub_local (fixS *fix, segT seg ATTRIBUTE_UNUSED)
 
1963
{
 
1964
  return obj_mach_o_in_different_subsection (fix->fx_addsy, fix->fx_subsy);
 
1965
}
 
1966
 
 
1967
int
 
1968
obj_mach_o_force_reloc (fixS *fix)
 
1969
{
 
1970
  if (generic_force_reloc (fix))
 
1971
    return 1;
 
1972
 
 
1973
  /* Force a reloc if the target is not in the same subsection.
 
1974
     FIXME: handle (a - b) where a and b belongs to the same subsection ?  */
 
1975
  if (fix->fx_addsy != NULL)
 
1976
    {
 
1977
      symbolS *subsec = fix->fx_frag->obj_frag_data.subsection;
 
1978
      symbolS *targ = fix->fx_addsy;
 
1979
 
 
1980
      /* There might be no subsections at all.  */
 
1981
      if (subsec == NULL)
 
1982
        return 0;
 
1983
 
 
1984
      if (S_GET_SEGMENT (targ) == absolute_section)
 
1985
        return 0;
 
1986
 
 
1987
      return obj_mach_o_in_different_subsection (targ, subsec);
 
1988
    }
 
1989
  return 0;
 
1990
}