aboutsummaryrefslogtreecommitdiff
path: root/gas/config/obj-macho.c
blob: 5f1255d4f678528bc574ef5048422d647d78bed8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* Mach-O object file format
   Copyright 2009 Free Software Foundation, Inc.

   This file is part of GAS, the GNU Assembler.

   GAS is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 3,
   or (at your option) any later version.

   GAS is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
   the GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GAS; see the file COPYING.  If not, write to the Free
   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
   02110-1301, USA.  */

#define OBJ_HEADER "obj-macho.h"

#include "as.h"
#include "subsegs.h"
#include "symbols.h"
#include "write.h"
#include "mach-o.h"
#include "mach-o/loader.h"

static void
obj_mach_o_weak (int ignore ATTRIBUTE_UNUSED)
{
  char *name;
  int c;
  symbolS *symbolP;

  do
    {
      /* Get symbol name.  */
      name = input_line_pointer;
      c = get_symbol_end ();
      symbolP = symbol_find_or_make (name);
      S_SET_WEAK (symbolP);
      *input_line_pointer = c;
      SKIP_WHITESPACE ();

      if (c != ',')
        break;
      input_line_pointer++;
      SKIP_WHITESPACE ();
    }
  while (*input_line_pointer != '\n');
  demand_empty_rest_of_line ();
}

/* Parse:
   .section segname,sectname[,type[,attribute[,sizeof_stub]]]
*/

static void
obj_mach_o_section (int ignore ATTRIBUTE_UNUSED)
{
  char *p;
  char *segname;
  char *sectname;
  char c;
  int sectype = BFD_MACH_O_S_REGULAR;
  unsigned int secattr = 0;
  offsetT sizeof_stub = 0;
  const char *name;
  flagword oldflags, flags;
  asection *sec;

  /* Parse segment name.  */
  if (!is_name_beginner (*input_line_pointer))
    {
      as_bad (_("missing segment name"));
      ignore_rest_of_line ();
      return;
    }
  p = input_line_pointer;
  c = get_symbol_end ();
  segname = alloca (input_line_pointer - p + 1);
  strcpy (segname, p);
  *input_line_pointer = c;

  if (*input_line_pointer != ',')
    {
      as_bad (_("missing comma after segment name"));
      ignore_rest_of_line ();
      return;
    }
  input_line_pointer++;

  /* Parse section name.  */
  if (!is_name_beginner (*input_line_pointer))
    {
      as_bad (_("missing section name"));
      ignore_rest_of_line ();
      return;
    }
  p = input_line_pointer;
  c = get_symbol_end ();
  sectname = alloca (input_line_pointer - p + 1);
  strcpy (sectname, p);
  *input_line_pointer = c;

  /* Parse type.  */
  if (*input_line_pointer == ',')
    {
      input_line_pointer++;
      if (!is_name_beginner (*input_line_pointer))
        {
          as_bad (_("missing section type name"));
          ignore_rest_of_line ();
          return;
        }
      p = input_line_pointer;
      c = get_symbol_end ();

      sectype = bfd_mach_o_get_section_type_from_name (p);
      if (sectype == -1)
        {
          as_bad (_("unknown or invalid section type '%s'"), p);
          sectype = BFD_MACH_O_S_REGULAR;
        }
      *input_line_pointer = c;

      /* Parse attributes.  */
      if (*input_line_pointer == ',')
        {
          do
            {
              int attr;

              input_line_pointer++;

              if (!is_name_beginner (*input_line_pointer))
                {
                  as_bad (_("missing section attribute identifier"));
                  ignore_rest_of_line ();
                  break;
                }
              p = input_line_pointer;
              c = get_symbol_end ();

              attr = bfd_mach_o_get_section_attribute_from_name (p);
              if (attr == -1)
                as_bad (_("unknown or invalid section attribute '%s'"), p);
              else
                secattr |= attr;

              *input_line_pointer = c;
            }
          while (*input_line_pointer == '+');

          /* Parse sizeof_stub.  */
          if (*input_line_pointer == ',')
            {
              if (sectype != BFD_MACH_O_S_SYMBOL_STUBS)
                as_bad (_("unexpected sizeof_stub expression"));

              sizeof_stub = get_absolute_expression ();
            }
          else if (sectype == BFD_MACH_O_S_SYMBOL_STUBS)
            as_bad (_("missing sizeof_stub expression"));
        }
    }
  demand_empty_rest_of_line ();

  bfd_mach_o_normalize_section_name (segname, sectname, &name, &flags);
  if (name == NULL)
    {
      /* There is no normal BFD section name for this section.  Create one.
         The name created doesn't really matter as it will never be written
         on disk.  */
      size_t seglen = strlen (segname);
      size_t sectlen = strlen (sectname);
      char *n;

      n = xmalloc (seglen + 1 + sectlen + 1);
      memcpy (n, segname, seglen);
      n[seglen] = '.';
      memcpy (n + seglen + 1, sectname, sectlen);
      n[seglen + 1 + sectlen] = 0;
      name = n;
    }

#ifdef md_flush_pending_output
  md_flush_pending_output ();
#endif

  /* Sub-segments don't exists as is on Mach-O.  */
  sec = subseg_new (name, 0);

  oldflags = bfd_get_section_flags (stdoutput, sec);
  if (oldflags == SEC_NO_FLAGS)
    {
      bfd_mach_o_section *msect;

      if (! bfd_set_section_flags (stdoutput, sec, flags))
	as_warn (_("error setting flags for \"%s\": %s"),
		 bfd_section_name (stdoutput, sec),
		 bfd_errmsg (bfd_get_error ()));
      msect = bfd_mach_o_get_mach_o_section (sec);
      strncpy (msect->segname, segname, sizeof (msect->segname));
      msect->segname[16] = 0;
      strncpy (msect->sectname, sectname, sizeof (msect->sectname));
      msect->sectname[16] = 0;
      msect->flags = secattr | sectype;
      msect->reserved2 = sizeof_stub;
    }
  else if (flags != SEC_NO_FLAGS)
    {
      if (flags != oldflags)
	as_warn (_("Ignoring changed section attributes for %s"), name);
    }
}

struct known_section
{
  const char *name;
  unsigned int flags;
};

static const struct known_section known_sections[] =
  {
    /* 0 */ { NULL, 0},
    /* 1 */ { ".cstring", BFD_MACH_O_S_CSTRING_LITERALS }
  };

static void
obj_mach_o_known_section (int sect_index)
{
  const struct known_section *sect = &known_sections[sect_index];
  asection *old_sec;
  segT sec;

#ifdef md_flush_pending_output
  md_flush_pending_output ();
#endif

  old_sec = bfd_get_section_by_name (stdoutput, sect->name);
  if (old_sec)
    {
      /* Section already present.  */
      sec = old_sec;
      subseg_set (sec, 0);
    }
  else
    {
      bfd_mach_o_section *msect;

      sec = subseg_force_new (sect->name, 0);

      /* Set default flags.  */
      msect = bfd_mach_o_get_mach_o_section (sec);
      msect->flags = sect->flags;
    }
}

/* Called from read.c:s_comm after we've parsed .comm symbol, size.
   Parse a possible alignment value.  */

static symbolS *
obj_mach_o_common_parse (int ignore ATTRIBUTE_UNUSED,
                         symbolS *symbolP, addressT size)
{
  addressT align = 0;

  if (*input_line_pointer == ',')
    {
      align = parse_align (0);
      if (align == (addressT) -1)
	return NULL;
    }

  S_SET_VALUE (symbolP, size);
  S_SET_EXTERNAL (symbolP);
  S_SET_SEGMENT (symbolP, bfd_com_section_ptr);

  symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;

  return symbolP;
}

static void
obj_mach_o_comm (int ignore ATTRIBUTE_UNUSED)
{
  s_comm_internal (ignore, obj_mach_o_common_parse);
}

static void
obj_mach_o_subsections_via_symbols (int arg ATTRIBUTE_UNUSED)
{
  /* Currently ignore it.  */
  demand_empty_rest_of_line ();
}

const pseudo_typeS mach_o_pseudo_table[] =
{
  { "weak", obj_mach_o_weak, 0},
  { "section", obj_mach_o_section, 0},
  { "cstring", obj_mach_o_known_section, 1},
  { "lcomm", s_lcomm, 1 },
  { "comm", obj_mach_o_comm, 0 },
  { "subsections_via_symbols", obj_mach_o_subsections_via_symbols, 0 },

  {NULL, NULL, 0}
};