xref: /netbsd/external/gpl3/gdb/dist/gas/config/obj-aout.c (revision 1424dfb3)
1*1424dfb3Schristos /* a.out object file format
2*1424dfb3Schristos    Copyright (C) 1989-2020 Free Software Foundation, Inc.
3*1424dfb3Schristos 
4*1424dfb3Schristos    This file is part of GAS, the GNU Assembler.
5*1424dfb3Schristos 
6*1424dfb3Schristos    GAS is free software; you can redistribute it and/or modify
7*1424dfb3Schristos    it under the terms of the GNU General Public License as
8*1424dfb3Schristos    published by the Free Software Foundation; either version 3,
9*1424dfb3Schristos    or (at your option) any later version.
10*1424dfb3Schristos 
11*1424dfb3Schristos    GAS is distributed in the hope that it will be useful, but
12*1424dfb3Schristos    WITHOUT ANY WARRANTY; without even the implied warranty of
13*1424dfb3Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
14*1424dfb3Schristos    the GNU General Public License for more details.
15*1424dfb3Schristos 
16*1424dfb3Schristos    You should have received a copy of the GNU General Public License
17*1424dfb3Schristos    along with GAS; see the file COPYING.  If not, write to the Free
18*1424dfb3Schristos    Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19*1424dfb3Schristos    02110-1301, USA.  */
20*1424dfb3Schristos 
21*1424dfb3Schristos #define OBJ_HEADER "obj-aout.h"
22*1424dfb3Schristos 
23*1424dfb3Schristos #include "as.h"
24*1424dfb3Schristos #undef NO_RELOC
25*1424dfb3Schristos #include "aout/aout64.h"
26*1424dfb3Schristos 
27*1424dfb3Schristos void
obj_aout_frob_symbol(symbolS * sym,int * punt ATTRIBUTE_UNUSED)28*1424dfb3Schristos obj_aout_frob_symbol (symbolS *sym, int *punt ATTRIBUTE_UNUSED)
29*1424dfb3Schristos {
30*1424dfb3Schristos   flagword flags;
31*1424dfb3Schristos   asection *sec;
32*1424dfb3Schristos   int type;
33*1424dfb3Schristos 
34*1424dfb3Schristos   flags = symbol_get_bfdsym (sym)->flags;
35*1424dfb3Schristos   type = aout_symbol (symbol_get_bfdsym (sym))->type;
36*1424dfb3Schristos   sec = S_GET_SEGMENT (sym);
37*1424dfb3Schristos 
38*1424dfb3Schristos   /* Only frob simple symbols this way right now.  */
39*1424dfb3Schristos   if (! (type & ~ (N_TYPE | N_EXT)))
40*1424dfb3Schristos     {
41*1424dfb3Schristos       if (type == (N_UNDF | N_EXT)
42*1424dfb3Schristos 	  && sec == bfd_abs_section_ptr)
43*1424dfb3Schristos 	{
44*1424dfb3Schristos 	  sec = bfd_und_section_ptr;
45*1424dfb3Schristos 	  S_SET_SEGMENT (sym, sec);
46*1424dfb3Schristos 	}
47*1424dfb3Schristos 
48*1424dfb3Schristos       if ((type & N_TYPE) != N_INDR
49*1424dfb3Schristos 	  && (type & N_TYPE) != N_SETA
50*1424dfb3Schristos 	  && (type & N_TYPE) != N_SETT
51*1424dfb3Schristos 	  && (type & N_TYPE) != N_SETD
52*1424dfb3Schristos 	  && (type & N_TYPE) != N_SETB
53*1424dfb3Schristos 	  && type != N_WARNING
54*1424dfb3Schristos 	  && (sec == bfd_abs_section_ptr
55*1424dfb3Schristos 	      || sec == bfd_und_section_ptr))
56*1424dfb3Schristos 	return;
57*1424dfb3Schristos       if (flags & BSF_EXPORT)
58*1424dfb3Schristos 	type |= N_EXT;
59*1424dfb3Schristos 
60*1424dfb3Schristos       switch (type & N_TYPE)
61*1424dfb3Schristos 	{
62*1424dfb3Schristos 	case N_SETA:
63*1424dfb3Schristos 	case N_SETT:
64*1424dfb3Schristos 	case N_SETD:
65*1424dfb3Schristos 	case N_SETB:
66*1424dfb3Schristos 	  /* Set the debugging flag for constructor symbols so that
67*1424dfb3Schristos 	     BFD leaves them alone.  */
68*1424dfb3Schristos 	  symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
69*1424dfb3Schristos 
70*1424dfb3Schristos 	  /* You can't put a common symbol in a set.  The way a set
71*1424dfb3Schristos 	     element works is that the symbol has a definition and a
72*1424dfb3Schristos 	     name, and the linker adds the definition to the set of
73*1424dfb3Schristos 	     that name.  That does not work for a common symbol,
74*1424dfb3Schristos 	     because the linker can't tell which common symbol the
75*1424dfb3Schristos 	     user means.  FIXME: Using as_bad here may be
76*1424dfb3Schristos 	     inappropriate, since the user may want to force a
77*1424dfb3Schristos 	     particular type without regard to the semantics of sets;
78*1424dfb3Schristos 	     on the other hand, we certainly don't want anybody to be
79*1424dfb3Schristos 	     mislead into thinking that their code will work.  */
80*1424dfb3Schristos 	  if (S_IS_COMMON (sym))
81*1424dfb3Schristos 	    as_bad (_("Attempt to put a common symbol into set %s"),
82*1424dfb3Schristos 		    S_GET_NAME (sym));
83*1424dfb3Schristos 	  /* Similarly, you can't put an undefined symbol in a set.  */
84*1424dfb3Schristos 	  else if (! S_IS_DEFINED (sym))
85*1424dfb3Schristos 	    as_bad (_("Attempt to put an undefined symbol into set %s"),
86*1424dfb3Schristos 		    S_GET_NAME (sym));
87*1424dfb3Schristos 
88*1424dfb3Schristos 	  break;
89*1424dfb3Schristos 	case N_INDR:
90*1424dfb3Schristos 	  /* Put indirect symbols in the indirect section.  */
91*1424dfb3Schristos 	  S_SET_SEGMENT (sym, bfd_ind_section_ptr);
92*1424dfb3Schristos 	  symbol_get_bfdsym (sym)->flags |= BSF_INDIRECT;
93*1424dfb3Schristos 	  if (type & N_EXT)
94*1424dfb3Schristos 	    {
95*1424dfb3Schristos 	      symbol_get_bfdsym (sym)->flags |= BSF_EXPORT;
96*1424dfb3Schristos 	      symbol_get_bfdsym (sym)->flags &=~ BSF_LOCAL;
97*1424dfb3Schristos 	    }
98*1424dfb3Schristos 	  break;
99*1424dfb3Schristos 	case N_WARNING:
100*1424dfb3Schristos 	  /* Mark warning symbols.  */
101*1424dfb3Schristos 	  symbol_get_bfdsym (sym)->flags |= BSF_WARNING;
102*1424dfb3Schristos 	  break;
103*1424dfb3Schristos 	}
104*1424dfb3Schristos     }
105*1424dfb3Schristos   else
106*1424dfb3Schristos     symbol_get_bfdsym (sym)->flags |= BSF_DEBUGGING;
107*1424dfb3Schristos 
108*1424dfb3Schristos   aout_symbol (symbol_get_bfdsym (sym))->type = type;
109*1424dfb3Schristos 
110*1424dfb3Schristos   /* Double check weak symbols.  */
111*1424dfb3Schristos   if (S_IS_WEAK (sym) && S_IS_COMMON (sym))
112*1424dfb3Schristos     as_bad (_("Symbol `%s' can not be both weak and common"),
113*1424dfb3Schristos 	    S_GET_NAME (sym));
114*1424dfb3Schristos }
115*1424dfb3Schristos 
116*1424dfb3Schristos /* Relocation processing may require knowing the VMAs of the sections.
117*1424dfb3Schristos    Writing to a section will cause the BFD back end to compute the
118*1424dfb3Schristos    VMAs.  This function also ensures that file size is large enough
119*1424dfb3Schristos    to cover a_text and a_data should text or data be the last section
120*1424dfb3Schristos    in the file.  */
121*1424dfb3Schristos 
122*1424dfb3Schristos void
obj_aout_frob_file_before_fix(void)123*1424dfb3Schristos obj_aout_frob_file_before_fix (void)
124*1424dfb3Schristos {
125*1424dfb3Schristos   asection *sec;
126*1424dfb3Schristos   bfd_vma *sizep = NULL;
127*1424dfb3Schristos   if ((sec = data_section)->size != 0)
128*1424dfb3Schristos     sizep = &exec_hdr (stdoutput)->a_data;
129*1424dfb3Schristos   else if ((sec = text_section)->size != 0)
130*1424dfb3Schristos     sizep = &exec_hdr (stdoutput)->a_text;
131*1424dfb3Schristos   if (sizep)
132*1424dfb3Schristos     {
133*1424dfb3Schristos       bfd_size_type size = sec->size;
134*1424dfb3Schristos       bfd_byte b = 0;
135*1424dfb3Schristos 
136*1424dfb3Schristos       gas_assert (bfd_set_section_contents (stdoutput, sec, &b, size - 1, 1));
137*1424dfb3Schristos 
138*1424dfb3Schristos       /* We don't know the aligned size until after VMAs and sizes are
139*1424dfb3Schristos 	 set on the bfd_set_section_contents call.  If that size is
140*1424dfb3Schristos 	 larger than the section then write again to ensure the file
141*1424dfb3Schristos 	 contents extend to cover the aligned size.  */
142*1424dfb3Schristos       if (*sizep > size)
143*1424dfb3Schristos 	{
144*1424dfb3Schristos 	  file_ptr pos = sec->filepos + *sizep;
145*1424dfb3Schristos 
146*1424dfb3Schristos 	  gas_assert (bfd_seek (stdoutput, pos - 1, SEEK_SET) == 0
147*1424dfb3Schristos 		      && bfd_bwrite (&b, 1, stdoutput) == 1);
148*1424dfb3Schristos 	}
149*1424dfb3Schristos     }
150*1424dfb3Schristos }
151*1424dfb3Schristos 
152*1424dfb3Schristos static void
obj_aout_line(int ignore ATTRIBUTE_UNUSED)153*1424dfb3Schristos obj_aout_line (int ignore ATTRIBUTE_UNUSED)
154*1424dfb3Schristos {
155*1424dfb3Schristos   /* Assume delimiter is part of expression.
156*1424dfb3Schristos      BSD4.2 as fails with delightful bug, so we
157*1424dfb3Schristos      are not being incompatible here.  */
158*1424dfb3Schristos   new_logical_line ((char *) NULL, (int) (get_absolute_expression ()));
159*1424dfb3Schristos   demand_empty_rest_of_line ();
160*1424dfb3Schristos }
161*1424dfb3Schristos 
162*1424dfb3Schristos /* Handle .weak.  This is a GNU extension.  */
163*1424dfb3Schristos 
164*1424dfb3Schristos static void
obj_aout_weak(int ignore ATTRIBUTE_UNUSED)165*1424dfb3Schristos obj_aout_weak (int ignore ATTRIBUTE_UNUSED)
166*1424dfb3Schristos {
167*1424dfb3Schristos   char *name;
168*1424dfb3Schristos   int c;
169*1424dfb3Schristos   symbolS *symbolP;
170*1424dfb3Schristos 
171*1424dfb3Schristos   do
172*1424dfb3Schristos     {
173*1424dfb3Schristos       c = get_symbol_name (&name);
174*1424dfb3Schristos       symbolP = symbol_find_or_make (name);
175*1424dfb3Schristos       (void) restore_line_pointer (c);
176*1424dfb3Schristos       SKIP_WHITESPACE ();
177*1424dfb3Schristos       S_SET_WEAK (symbolP);
178*1424dfb3Schristos       if (c == ',')
179*1424dfb3Schristos 	{
180*1424dfb3Schristos 	  input_line_pointer++;
181*1424dfb3Schristos 	  SKIP_WHITESPACE ();
182*1424dfb3Schristos 	  if (*input_line_pointer == '\n')
183*1424dfb3Schristos 	    c = '\n';
184*1424dfb3Schristos 	}
185*1424dfb3Schristos     }
186*1424dfb3Schristos   while (c == ',');
187*1424dfb3Schristos   demand_empty_rest_of_line ();
188*1424dfb3Schristos }
189*1424dfb3Schristos 
190*1424dfb3Schristos /* Handle .type.  On {Net,Open}BSD, this is used to set the n_other field,
191*1424dfb3Schristos    which is then apparently used when doing dynamic linking.  Older
192*1424dfb3Schristos    versions of gas ignored the .type pseudo-op, so we also ignore it if
193*1424dfb3Schristos    we can't parse it.  */
194*1424dfb3Schristos 
195*1424dfb3Schristos static void
obj_aout_type(int ignore ATTRIBUTE_UNUSED)196*1424dfb3Schristos obj_aout_type (int ignore ATTRIBUTE_UNUSED)
197*1424dfb3Schristos {
198*1424dfb3Schristos   char *name;
199*1424dfb3Schristos   int c;
200*1424dfb3Schristos   symbolS *sym;
201*1424dfb3Schristos 
202*1424dfb3Schristos   c = get_symbol_name (&name);
203*1424dfb3Schristos   sym = symbol_find_or_make (name);
204*1424dfb3Schristos   (void) restore_line_pointer (c);
205*1424dfb3Schristos   SKIP_WHITESPACE ();
206*1424dfb3Schristos   if (*input_line_pointer == ',')
207*1424dfb3Schristos     {
208*1424dfb3Schristos       ++input_line_pointer;
209*1424dfb3Schristos       SKIP_WHITESPACE ();
210*1424dfb3Schristos       if (*input_line_pointer == '@')
211*1424dfb3Schristos 	{
212*1424dfb3Schristos 	  ++input_line_pointer;
213*1424dfb3Schristos 	  if (strncmp (input_line_pointer, "object", 6) == 0)
214*1424dfb3Schristos 	    S_SET_OTHER (sym, 1);
215*1424dfb3Schristos 	  else if (strncmp (input_line_pointer, "function", 8) == 0)
216*1424dfb3Schristos 	    S_SET_OTHER (sym, 2);
217*1424dfb3Schristos 	}
218*1424dfb3Schristos     }
219*1424dfb3Schristos 
220*1424dfb3Schristos   /* Ignore everything else on the line.  */
221*1424dfb3Schristos   s_ignore (0);
222*1424dfb3Schristos }
223*1424dfb3Schristos 
224*1424dfb3Schristos /* Support for an AOUT emulation.  */
225*1424dfb3Schristos 
226*1424dfb3Schristos static void
aout_pop_insert(void)227*1424dfb3Schristos aout_pop_insert (void)
228*1424dfb3Schristos {
229*1424dfb3Schristos   pop_insert (aout_pseudo_table);
230*1424dfb3Schristos }
231*1424dfb3Schristos 
232*1424dfb3Schristos static int
obj_aout_s_get_other(symbolS * sym)233*1424dfb3Schristos obj_aout_s_get_other (symbolS *sym)
234*1424dfb3Schristos {
235*1424dfb3Schristos   return aout_symbol (symbol_get_bfdsym (sym))->other;
236*1424dfb3Schristos }
237*1424dfb3Schristos 
238*1424dfb3Schristos static void
obj_aout_s_set_other(symbolS * sym,int o)239*1424dfb3Schristos obj_aout_s_set_other (symbolS *sym, int o)
240*1424dfb3Schristos {
241*1424dfb3Schristos   aout_symbol (symbol_get_bfdsym (sym))->other = o;
242*1424dfb3Schristos }
243*1424dfb3Schristos 
244*1424dfb3Schristos static int
obj_aout_sec_sym_ok_for_reloc(asection * sec ATTRIBUTE_UNUSED)245*1424dfb3Schristos obj_aout_sec_sym_ok_for_reloc (asection *sec ATTRIBUTE_UNUSED)
246*1424dfb3Schristos {
247*1424dfb3Schristos   return obj_sec_sym_ok_for_reloc (sec);
248*1424dfb3Schristos }
249*1424dfb3Schristos 
250*1424dfb3Schristos static void
obj_aout_process_stab(segT seg ATTRIBUTE_UNUSED,int w,const char * s,int t,int o,int d)251*1424dfb3Schristos obj_aout_process_stab (segT seg ATTRIBUTE_UNUSED,
252*1424dfb3Schristos 		       int w,
253*1424dfb3Schristos 		       const char *s,
254*1424dfb3Schristos 		       int t,
255*1424dfb3Schristos 		       int o,
256*1424dfb3Schristos 		       int d)
257*1424dfb3Schristos {
258*1424dfb3Schristos   aout_process_stab (w, s, t, o, d);
259*1424dfb3Schristos }
260*1424dfb3Schristos 
261*1424dfb3Schristos static int
obj_aout_s_get_desc(symbolS * sym)262*1424dfb3Schristos obj_aout_s_get_desc (symbolS *sym)
263*1424dfb3Schristos {
264*1424dfb3Schristos   return aout_symbol (symbol_get_bfdsym (sym))->desc;
265*1424dfb3Schristos }
266*1424dfb3Schristos 
267*1424dfb3Schristos static void
obj_aout_s_set_desc(symbolS * sym,int d)268*1424dfb3Schristos obj_aout_s_set_desc (symbolS *sym, int d)
269*1424dfb3Schristos {
270*1424dfb3Schristos   aout_symbol (symbol_get_bfdsym (sym))->desc = d;
271*1424dfb3Schristos }
272*1424dfb3Schristos 
273*1424dfb3Schristos static int
obj_aout_s_get_type(symbolS * sym)274*1424dfb3Schristos obj_aout_s_get_type (symbolS *sym)
275*1424dfb3Schristos {
276*1424dfb3Schristos   return aout_symbol (symbol_get_bfdsym (sym))->type;
277*1424dfb3Schristos }
278*1424dfb3Schristos 
279*1424dfb3Schristos static void
obj_aout_s_set_type(symbolS * sym,int t)280*1424dfb3Schristos obj_aout_s_set_type (symbolS *sym, int t)
281*1424dfb3Schristos {
282*1424dfb3Schristos   aout_symbol (symbol_get_bfdsym (sym))->type = t;
283*1424dfb3Schristos }
284*1424dfb3Schristos 
285*1424dfb3Schristos static int
obj_aout_separate_stab_sections(void)286*1424dfb3Schristos obj_aout_separate_stab_sections (void)
287*1424dfb3Schristos {
288*1424dfb3Schristos   return 0;
289*1424dfb3Schristos }
290*1424dfb3Schristos 
291*1424dfb3Schristos /* When changed, make sure these table entries match the single-format
292*1424dfb3Schristos    definitions in obj-aout.h.  */
293*1424dfb3Schristos 
294*1424dfb3Schristos const struct format_ops aout_format_ops =
295*1424dfb3Schristos {
296*1424dfb3Schristos   bfd_target_aout_flavour,
297*1424dfb3Schristos   1,	/* dfl_leading_underscore.  */
298*1424dfb3Schristos   0,	/* emit_section_symbols.  */
299*1424dfb3Schristos   0,	/* begin.  */
300*1424dfb3Schristos   0,	/* app_file.  */
301*1424dfb3Schristos   obj_aout_frob_symbol,
302*1424dfb3Schristos   0,	/* frob_file.  */
303*1424dfb3Schristos   0,	/* frob_file_before_adjust.  */
304*1424dfb3Schristos   obj_aout_frob_file_before_fix,
305*1424dfb3Schristos   0,	/* frob_file_after_relocs.  */
306*1424dfb3Schristos   0,	/* s_get_size.  */
307*1424dfb3Schristos   0,	/* s_set_size.  */
308*1424dfb3Schristos   0,	/* s_get_align.  */
309*1424dfb3Schristos   0,	/* s_set_align.  */
310*1424dfb3Schristos   obj_aout_s_get_other,
311*1424dfb3Schristos   obj_aout_s_set_other,
312*1424dfb3Schristos   obj_aout_s_get_desc,
313*1424dfb3Schristos   obj_aout_s_set_desc,
314*1424dfb3Schristos   obj_aout_s_get_type,
315*1424dfb3Schristos   obj_aout_s_set_type,
316*1424dfb3Schristos   0,	/* copy_symbol_attributes.  */
317*1424dfb3Schristos   0,	/* generate_asm_lineno.  */
318*1424dfb3Schristos   obj_aout_process_stab,
319*1424dfb3Schristos   obj_aout_separate_stab_sections,
320*1424dfb3Schristos   0,	/* init_stab_section.  */
321*1424dfb3Schristos   obj_aout_sec_sym_ok_for_reloc,
322*1424dfb3Schristos   aout_pop_insert,
323*1424dfb3Schristos   0,	/* ecoff_set_ext.  */
324*1424dfb3Schristos   0,	/* read_begin_hook.  */
325*1424dfb3Schristos   0,	/* symbol_new_hook.  */
326*1424dfb3Schristos   0,	/* symbol_clone_hook.  */
327*1424dfb3Schristos   0	/* adjust_symtab.  */
328*1424dfb3Schristos };
329*1424dfb3Schristos 
330*1424dfb3Schristos const pseudo_typeS aout_pseudo_table[] =
331*1424dfb3Schristos {
332*1424dfb3Schristos   {"line", obj_aout_line, 0},	/* Source code line number.  */
333*1424dfb3Schristos   {"ln", obj_aout_line, 0},	/* COFF line number that we use anyway.  */
334*1424dfb3Schristos 
335*1424dfb3Schristos   {"weak", obj_aout_weak, 0},	/* Mark symbol as weak.  */
336*1424dfb3Schristos 
337*1424dfb3Schristos   {"type", obj_aout_type, 0},
338*1424dfb3Schristos 
339*1424dfb3Schristos   /* coff debug pseudos (ignored) */
340*1424dfb3Schristos   {"def", s_ignore, 0},
341*1424dfb3Schristos   {"dim", s_ignore, 0},
342*1424dfb3Schristos   {"endef", s_ignore, 0},
343*1424dfb3Schristos   {"ident", s_ignore, 0},
344*1424dfb3Schristos   {"line", s_ignore, 0},
345*1424dfb3Schristos   {"ln", s_ignore, 0},
346*1424dfb3Schristos   {"scl", s_ignore, 0},
347*1424dfb3Schristos   {"size", s_ignore, 0},
348*1424dfb3Schristos   {"tag", s_ignore, 0},
349*1424dfb3Schristos   {"val", s_ignore, 0},
350*1424dfb3Schristos   {"version", s_ignore, 0},
351*1424dfb3Schristos 
352*1424dfb3Schristos   {"optim", s_ignore, 0},	/* For sun386i cc (?).  */
353*1424dfb3Schristos 
354*1424dfb3Schristos   /* other stuff */
355*1424dfb3Schristos   {"ABORT", s_abort, 0},
356*1424dfb3Schristos 
357*1424dfb3Schristos   {NULL, NULL, 0}
358*1424dfb3Schristos };
359