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