1/* ECOFF object file format.
2   Copyright (C) 1993-2017 Free Software Foundation, Inc.
3   Contributed by Cygnus Support.
4   This file was put together by Ian Lance Taylor <ian@cygnus.com>.
5
6   This file is part of GAS.
7
8   GAS is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 3, or (at your option)
11   any later version.
12
13   GAS is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with GAS; see the file COPYING.  If not, write to the Free
20   Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21   02110-1301, USA.  */
22
23#define OBJ_HEADER "obj-ecoff.h"
24#include "as.h"
25#include "coff/internal.h"
26#include "bfd/libcoff.h"
27#include "bfd/libecoff.h"
28
29/* Almost all of the ECOFF support is actually in ecoff.c in the main
30   gas directory.  This file mostly just arranges to call that one at
31   the right times.  */
32
33/* Set section VMAs and GP values before reloc processing.  */
34
35void
36ecoff_frob_file_before_fix (void)
37{
38  bfd_vma addr;
39  asection *sec;
40
41  /* Set the section VMA values.  We force the .sdata and .sbss
42     sections to the end to ensure that their VMA addresses are close
43     together so that the GP register can address both of them.  We
44     put the .bss section after the .sbss section.
45
46     Also, for the Alpha, we must sort the sections, to make sure they
47     appear in the output file in the correct order.  (Actually, maybe
48     this is a job for BFD.  But the VMAs computed would be out of
49     whack if we computed them given our initial, random ordering.
50     It's possible that that wouldn't break things; I could do some
51     experimenting sometime and find out.
52
53     This output ordering of sections is magic, on the Alpha, at
54     least.  The .lita section must come before .lit8 and .lit4,
55     otherwise the OSF/1 linker may silently trash the .lit{4,8}
56     section contents.  Also, .text must precede .rdata.  These differ
57     from the order described in some parts of the DEC OSF/1 Assembly
58     Language Programmer's Guide, but that order doesn't seem to work
59     with their linker.
60
61     I don't know if section ordering on the MIPS is important.  */
62
63  static const char *const names[] =
64  {
65    /* text segment */
66    ".text", ".rdata", ".init", ".fini",
67    /* data segment */
68    ".data", ".lita", ".lit8", ".lit4", ".sdata", ".got",
69    /* bss segment */
70    ".sbss", ".bss",
71  };
72#define n_names ((int) (sizeof (names) / sizeof (names[0])))
73
74  /* Sections that match names, order to be straightened out later.  */
75  asection *secs[n_names];
76  int i;
77
78  addr = 0;
79  for (i = 0; i < n_names; i++)
80    secs[i] = NULL;
81
82  for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
83    {
84      for (i = 0; i < n_names; i++)
85	if (!strcmp (sec->name, names[i]))
86	  {
87	    secs[i] = sec;
88	    bfd_section_list_remove (stdoutput, sec);
89	    break;
90	  }
91      if (i == n_names)
92	{
93	  bfd_set_section_vma (stdoutput, sec, addr);
94	  addr += bfd_section_size (stdoutput, sec);
95	}
96    }
97  for (i = 0; i < n_names; i++)
98    if (secs[i])
99      {
100	bfd_set_section_vma (stdoutput, secs[i], addr);
101	addr += bfd_section_size (stdoutput, secs[i]);
102      }
103  for (i = n_names - 1; i >= 0; i--)
104    if (secs[i])
105      bfd_section_list_prepend (stdoutput, secs[i]);
106
107  /* Fill in the register masks.  */
108  {
109    unsigned long gprmask = 0;
110    unsigned long fprmask = 0;
111    unsigned long *cprmask = NULL;
112
113#ifdef TC_MIPS
114    /* Fill in the MIPS register masks.  It's probably not worth
115       setting up a generic interface for this.  */
116    gprmask = mips_gprmask;
117    cprmask = mips_cprmask;
118#endif
119
120#ifdef TC_ALPHA
121    alpha_frob_ecoff_data ();
122
123    if (! bfd_ecoff_set_gp_value (stdoutput, alpha_gp_value))
124      as_fatal (_("Can't set GP value"));
125
126    gprmask = alpha_gprmask;
127    fprmask = alpha_fprmask;
128#endif
129
130    if (! bfd_ecoff_set_regmasks (stdoutput, gprmask, fprmask, cprmask))
131      as_fatal (_("Can't set register masks"));
132  }
133}
134
135/* Swap out the symbols and debugging information for BFD.  */
136
137void
138ecoff_frob_file (void)
139{
140  const struct ecoff_debug_swap * const debug_swap
141    = &ecoff_backend (stdoutput)->debug_swap;
142  bfd_vma addr ATTRIBUTE_UNUSED;
143  HDRR *hdr;
144  char *buf;
145  char *set;
146
147  /* Build the ECOFF debugging information.  */
148  gas_assert (ecoff_data (stdoutput) != 0);
149  hdr = &ecoff_data (stdoutput)->debug_info.symbolic_header;
150  ecoff_build_debug (hdr, &buf, debug_swap);
151
152  /* Finish up the ecoff_tdata structure.  */
153  set = buf;
154#define SET(ptr, count, type, size) \
155  if (hdr->count == 0) \
156    ecoff_data (stdoutput)->debug_info.ptr = NULL; \
157  else \
158    { \
159      ecoff_data (stdoutput)->debug_info.ptr = (type) set; \
160      set += hdr->count * size; \
161    }
162
163  SET (line, cbLine, unsigned char *, sizeof (unsigned char));
164  SET (external_dnr, idnMax, void *, debug_swap->external_dnr_size);
165  SET (external_pdr, ipdMax, void *, debug_swap->external_pdr_size);
166  SET (external_sym, isymMax, void *, debug_swap->external_sym_size);
167  SET (external_opt, ioptMax, void *, debug_swap->external_opt_size);
168  SET (external_aux, iauxMax, union aux_ext *, sizeof (union aux_ext));
169  SET (ss, issMax, char *, sizeof (char));
170  SET (ssext, issExtMax, char *, sizeof (char));
171  SET (external_rfd, crfd, void *, debug_swap->external_rfd_size);
172  SET (external_fdr, ifdMax, void *, debug_swap->external_fdr_size);
173  SET (external_ext, iextMax, void *, debug_swap->external_ext_size);
174#undef SET
175}
176
177/* This is called by the ECOFF code to set the external information
178   for a symbol.  We just pass it on to BFD, which expects the swapped
179   information to be stored in the native field of the symbol.  */
180
181void
182obj_ecoff_set_ext (symbolS *sym, EXTR *ext)
183{
184  const struct ecoff_debug_swap * const debug_swap
185    = &ecoff_backend (stdoutput)->debug_swap;
186  ecoff_symbol_type *esym;
187
188  know (bfd_asymbol_flavour (symbol_get_bfdsym (sym))
189	== bfd_target_ecoff_flavour);
190  esym = ecoffsymbol (symbol_get_bfdsym (sym));
191  esym->local = FALSE;
192  esym->native = xmalloc (debug_swap->external_ext_size);
193  (*debug_swap->swap_ext_out) (stdoutput, ext, esym->native);
194}
195
196static int
197ecoff_sec_sym_ok_for_reloc (asection *sec ATTRIBUTE_UNUSED)
198{
199  return 1;
200}
201
202static void
203obj_ecoff_frob_symbol (symbolS *sym, int *puntp ATTRIBUTE_UNUSED)
204{
205  ecoff_frob_symbol (sym);
206}
207
208static void
209ecoff_pop_insert (void)
210{
211  pop_insert (obj_pseudo_table);
212}
213
214static int
215ecoff_separate_stab_sections (void)
216{
217  return 0;
218}
219
220/* These are the pseudo-ops we support in this file.  Only those
221   relating to debugging information are supported here.
222
223   The following pseudo-ops from the Kane and Heinrich MIPS book
224   should be defined here, but are currently unsupported: .aent,
225   .bgnb, .endb, .verstamp, .vreg.
226
227   The following pseudo-ops from the Kane and Heinrich MIPS book are
228   MIPS CPU specific, and should be defined by tc-mips.c: .alias,
229   .extern, .galive, .gjaldef, .gjrlive, .livereg, .noalias, .option,
230   .rdata, .sdata, .set.
231
232   The following pseudo-ops from the Kane and Heinrich MIPS book are
233   not MIPS CPU specific, but are also not ECOFF specific.  I have
234   only listed the ones which are not already in read.c.  It's not
235   completely clear where these should be defined, but tc-mips.c is
236   probably the most reasonable place: .asciiz, .asm0, .endr, .err,
237   .half, .lab, .repeat, .struct, .weakext.  */
238
239const pseudo_typeS obj_pseudo_table[] =
240{
241  /* COFF style debugging information. .ln is not used; .loc is used
242     instead.  */
243  { "def",	ecoff_directive_def,	0 },
244  { "dim",	ecoff_directive_dim,	0 },
245  { "endef",	ecoff_directive_endef,	0 },
246  { "file",	ecoff_directive_file,	0 },
247  { "scl",	ecoff_directive_scl,	0 },
248  { "size",	ecoff_directive_size,	0 },
249  { "esize",	ecoff_directive_size,	0 },
250  { "tag",	ecoff_directive_tag,	0 },
251  { "type",	ecoff_directive_type,	0 },
252  { "etype",	ecoff_directive_type,	0 },
253  { "val",	ecoff_directive_val,	0 },
254
255  /* ECOFF specific debugging information.  */
256  { "begin",	ecoff_directive_begin,	0 },
257  { "bend",	ecoff_directive_bend,	0 },
258  { "end",	ecoff_directive_end,	0 },
259  { "ent",	ecoff_directive_ent,	0 },
260  { "fmask",	ecoff_directive_fmask,	0 },
261  { "frame",	ecoff_directive_frame,	0 },
262  { "loc",	ecoff_directive_loc,	0 },
263  { "mask",	ecoff_directive_mask,	0 },
264
265  /* Other ECOFF directives.  */
266  { "extern",	ecoff_directive_extern,	0 },
267
268#ifndef TC_MIPS
269  /* For TC_MIPS, tc-mips.c adds this.  */
270  { "weakext",	ecoff_directive_weakext, 0 },
271#endif
272
273  /* These are used on Irix.  I don't know how to implement them.  */
274  { "bgnb",	s_ignore,		0 },
275  { "endb",	s_ignore,		0 },
276  { "verstamp",	s_ignore,		0 },
277
278  /* Sentinel.  */
279  { NULL,	s_ignore,		0 }
280};
281
282const struct format_ops ecoff_format_ops =
283{
284  bfd_target_ecoff_flavour,
285  0,	/* dfl_leading_underscore.  */
286
287  /* FIXME: A comment why emit_section_symbols is different here (1) from
288     the single-format definition (0) would be in order.  */
289  1,	/* emit_section_symbols.  */
290  0,	/* begin.  */
291  ecoff_new_file,
292  obj_ecoff_frob_symbol,
293  ecoff_frob_file,
294  0,	/* frob_file_before_adjust.  */
295  ecoff_frob_file_before_fix,
296  0,	/* frob_file_after_relocs.  */
297  0,	/* s_get_size.  */
298  0,	/* s_set_size.  */
299  0,	/* s_get_align.  */
300  0,	/* s_set_align.  */
301  0,	/* s_get_other.  */
302  0,	/* s_set_other.  */
303  0,	/* s_get_desc.  */
304  0,	/* s_set_desc.  */
305  0,	/* s_get_type.  */
306  0,	/* s_set_type.  */
307  0,	/* copy_symbol_attributes.  */
308  ecoff_generate_asm_lineno,
309  ecoff_stab,
310  ecoff_separate_stab_sections,
311  0,	/* init_stab_section.  */
312  ecoff_sec_sym_ok_for_reloc,
313  ecoff_pop_insert,
314  ecoff_set_ext,
315  ecoff_read_begin_hook,
316  ecoff_symbol_new_hook,
317  ecoff_symbol_clone_hook,
318  0	/* adjust_symtab.  */
319};
320