obj-ecoff.c revision 130561
1178828Sdfr/* ECOFF object file format.
2178828Sdfr   Copyright 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002
3178828Sdfr   Free Software Foundation, Inc.
4178828Sdfr   Contributed by Cygnus Support.
5178828Sdfr   This file was put together by Ian Lance Taylor <ian@cygnus.com>.
6178828Sdfr
7178828Sdfr   This file is part of GAS.
8178828Sdfr
9178828Sdfr   GAS is free software; you can redistribute it and/or modify
10178828Sdfr   it under the terms of the GNU General Public License as published by
11178828Sdfr   the Free Software Foundation; either version 2, or (at your option)
12178828Sdfr   any later version.
13178828Sdfr
14178828Sdfr   GAS is distributed in the hope that it will be useful,
15178828Sdfr   but WITHOUT ANY WARRANTY; without even the implied warranty of
16178828Sdfr   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17178828Sdfr   GNU General Public License for more details.
18178828Sdfr
19178828Sdfr   You should have received a copy of the GNU General Public License
20178828Sdfr   along with GAS; see the file COPYING.  If not, write to the Free
21178828Sdfr   Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22178828Sdfr   02111-1307, USA.  */
23178828Sdfr
24178828Sdfr#define OBJ_HEADER "obj-ecoff.h"
25178828Sdfr#include "as.h"
26178828Sdfr#include "coff/internal.h"
27178828Sdfr#include "bfd/libcoff.h"
28178828Sdfr#include "bfd/libecoff.h"
29178828Sdfr
30178828Sdfr/* Almost all of the ECOFF support is actually in ecoff.c in the main
31178828Sdfr   gas directory.  This file mostly just arranges to call that one at
32178828Sdfr   the right times.  */
33178828Sdfr
34178828Sdfrstatic int ecoff_sec_sym_ok_for_reloc PARAMS ((asection *));
35178828Sdfrstatic void obj_ecoff_frob_symbol PARAMS ((symbolS *, int *));
36178828Sdfrstatic void ecoff_pop_insert PARAMS ((void));
37178828Sdfrstatic int ecoff_separate_stab_sections PARAMS ((void));
38178828Sdfr
39178828Sdfr/* These are the pseudo-ops we support in this file.  Only those
40178828Sdfr   relating to debugging information are supported here.
41178828Sdfr
42178828Sdfr   The following pseudo-ops from the Kane and Heinrich MIPS book
43178828Sdfr   should be defined here, but are currently unsupported: .aent,
44178828Sdfr   .bgnb, .endb, .verstamp, .vreg.
45178828Sdfr
46178828Sdfr   The following pseudo-ops from the Kane and Heinrich MIPS book are
47178828Sdfr   MIPS CPU specific, and should be defined by tc-mips.c: .alias,
48178828Sdfr   .extern, .galive, .gjaldef, .gjrlive, .livereg, .noalias, .option,
49178828Sdfr   .rdata, .sdata, .set.
50178828Sdfr
51178828Sdfr   The following pseudo-ops from the Kane and Heinrich MIPS book are
52178828Sdfr   not MIPS CPU specific, but are also not ECOFF specific.  I have
53178828Sdfr   only listed the ones which are not already in read.c.  It's not
54178828Sdfr   completely clear where these should be defined, but tc-mips.c is
55178828Sdfr   probably the most reasonable place: .asciiz, .asm0, .endr, .err,
56178828Sdfr   .half, .lab, .repeat, .struct, .weakext.  */
57178828Sdfr
58178828Sdfrconst pseudo_typeS obj_pseudo_table[] =
59178828Sdfr{
60178828Sdfr  /* COFF style debugging information. .ln is not used; .loc is used
61178828Sdfr     instead.  */
62178828Sdfr  { "def",	ecoff_directive_def,	0 },
63178828Sdfr  { "dim",	ecoff_directive_dim,	0 },
64178828Sdfr  { "endef",	ecoff_directive_endef,	0 },
65178828Sdfr  { "file",	ecoff_directive_file,	0 },
66178828Sdfr  { "scl",	ecoff_directive_scl,	0 },
67178828Sdfr  { "size",	ecoff_directive_size,	0 },
68178828Sdfr  { "esize",	ecoff_directive_size,	0 },
69178828Sdfr  { "tag",	ecoff_directive_tag,	0 },
70178828Sdfr  { "type",	ecoff_directive_type,	0 },
71178828Sdfr  { "etype",	ecoff_directive_type,	0 },
72178828Sdfr  { "val",	ecoff_directive_val,	0 },
73178828Sdfr
74178828Sdfr  /* ECOFF specific debugging information.  */
75178828Sdfr  { "begin",	ecoff_directive_begin,	0 },
76178828Sdfr  { "bend",	ecoff_directive_bend,	0 },
77178828Sdfr  { "end",	ecoff_directive_end,	0 },
78178828Sdfr  { "ent",	ecoff_directive_ent,	0 },
79178828Sdfr  { "fmask",	ecoff_directive_fmask,	0 },
80178828Sdfr  { "frame",	ecoff_directive_frame,	0 },
81178828Sdfr  { "loc",	ecoff_directive_loc,	0 },
82178828Sdfr  { "mask",	ecoff_directive_mask,	0 },
83178828Sdfr
84178828Sdfr  /* Other ECOFF directives.  */
85178828Sdfr  { "extern",	ecoff_directive_extern,	0 },
86178828Sdfr
87178828Sdfr#ifndef TC_MIPS
88178828Sdfr  /* For TC_MIPS, tc-mips.c adds this.  */
89178828Sdfr  { "weakext",	ecoff_directive_weakext, 0 },
90178828Sdfr#endif
91178828Sdfr
92178828Sdfr  /* These are used on Irix.  I don't know how to implement them.  */
93178828Sdfr  { "bgnb",	s_ignore,		0 },
94  { "endb",	s_ignore,		0 },
95  { "verstamp",	s_ignore,		0 },
96
97  /* Sentinel.  */
98  { NULL,	s_ignore,		0 }
99};
100
101/* Set section VMAs and GP values before reloc processing.  */
102
103void
104ecoff_frob_file_before_fix ()
105{
106  bfd_vma addr;
107  asection **sec;
108
109  /* Set the section VMA values.  We force the .sdata and .sbss
110     sections to the end to ensure that their VMA addresses are close
111     together so that the GP register can address both of them.  We
112     put the .bss section after the .sbss section.
113
114     Also, for the Alpha, we must sort the sections, to make sure they
115     appear in the output file in the correct order.  (Actually, maybe
116     this is a job for BFD.  But the VMAs computed would be out of
117     whack if we computed them given our initial, random ordering.
118     It's possible that that wouldn't break things; I could do some
119     experimenting sometime and find out.
120
121     This output ordering of sections is magic, on the Alpha, at
122     least.  The .lita section must come before .lit8 and .lit4,
123     otherwise the OSF/1 linker may silently trash the .lit{4,8}
124     section contents.  Also, .text must preceed .rdata.  These differ
125     from the order described in some parts of the DEC OSF/1 Assembly
126     Language Programmer's Guide, but that order doesn't seem to work
127     with their linker.
128
129     I don't know if section ordering on the MIPS is important.  */
130
131  static const char *const names[] = {
132    /* text segment */
133    ".text", ".rdata", ".init", ".fini",
134    /* data segment */
135    ".data", ".lita", ".lit8", ".lit4", ".sdata", ".got",
136    /* bss segment */
137    ".sbss", ".bss",
138  };
139#define n_names ((int) (sizeof (names) / sizeof (names[0])))
140
141  /* Sections that match names, order to be straightened out later.  */
142  asection *secs[n_names];
143  int i;
144
145  addr = 0;
146  for (i = 0; i < n_names; i++)
147    secs[i] = 0;
148
149  for (sec = &stdoutput->sections; *sec != (asection *) NULL; )
150    {
151      for (i = 0; i < n_names; i++)
152	if (!strcmp ((*sec)->name, names[i]))
153	  {
154	    secs[i] = *sec;
155	    bfd_section_list_remove (stdoutput, sec);
156	    break;
157	  }
158      if (i == n_names)
159	{
160	  bfd_set_section_vma (stdoutput, *sec, addr);
161	  addr += bfd_section_size (stdoutput, *sec);
162	  sec = &(*sec)->next;
163	}
164    }
165  for (i = 0; i < n_names; i++)
166    if (secs[i])
167      {
168	bfd_set_section_vma (stdoutput, secs[i], addr);
169	addr += bfd_section_size (stdoutput, secs[i]);
170      }
171  for (i = n_names - 1; i >= 0; i--)
172    if (secs[i])
173      bfd_section_list_insert (stdoutput, &stdoutput->sections, secs[i]);
174
175  /* Fill in the register masks.  */
176  {
177    unsigned long gprmask = 0;
178    unsigned long fprmask = 0;
179    unsigned long *cprmask = NULL;
180
181#ifdef TC_MIPS
182    /* Fill in the MIPS register masks.  It's probably not worth
183       setting up a generic interface for this.  */
184    gprmask = mips_gprmask;
185    cprmask = mips_cprmask;
186#endif
187
188#ifdef TC_ALPHA
189    alpha_frob_ecoff_data ();
190
191    if (! bfd_ecoff_set_gp_value (stdoutput, alpha_gp_value))
192      as_fatal (_("Can't set GP value"));
193
194    gprmask = alpha_gprmask;
195    fprmask = alpha_fprmask;
196#endif
197
198    if (! bfd_ecoff_set_regmasks (stdoutput, gprmask, fprmask, cprmask))
199      as_fatal (_("Can't set register masks"));
200  }
201}
202
203/* Swap out the symbols and debugging information for BFD.  */
204
205void
206ecoff_frob_file ()
207{
208  const struct ecoff_debug_swap * const debug_swap
209    = &ecoff_backend (stdoutput)->debug_swap;
210  bfd_vma addr ATTRIBUTE_UNUSED;
211  HDRR *hdr;
212  char *buf;
213  char *set;
214
215  /* Build the ECOFF debugging information.  */
216  assert (ecoff_data (stdoutput) != 0);
217  hdr = &ecoff_data (stdoutput)->debug_info.symbolic_header;
218  ecoff_build_debug (hdr, &buf, debug_swap);
219
220  /* Finish up the ecoff_tdata structure.  */
221  set = buf;
222#define SET(ptr, count, type, size) \
223  if (hdr->count == 0) \
224    ecoff_data (stdoutput)->debug_info.ptr = (type) NULL; \
225  else \
226    { \
227      ecoff_data (stdoutput)->debug_info.ptr = (type) set; \
228      set += hdr->count * size; \
229    }
230
231  SET (line, cbLine, unsigned char *, sizeof (unsigned char));
232  SET (external_dnr, idnMax, PTR, debug_swap->external_dnr_size);
233  SET (external_pdr, ipdMax, PTR, debug_swap->external_pdr_size);
234  SET (external_sym, isymMax, PTR, debug_swap->external_sym_size);
235  SET (external_opt, ioptMax, PTR, debug_swap->external_opt_size);
236  SET (external_aux, iauxMax, union aux_ext *, sizeof (union aux_ext));
237  SET (ss, issMax, char *, sizeof (char));
238  SET (ssext, issExtMax, char *, sizeof (char));
239  SET (external_rfd, crfd, PTR, debug_swap->external_rfd_size);
240  SET (external_fdr, ifdMax, PTR, debug_swap->external_fdr_size);
241  SET (external_ext, iextMax, PTR, debug_swap->external_ext_size);
242#undef SET
243}
244
245/* This is called by the ECOFF code to set the external information
246   for a symbol.  We just pass it on to BFD, which expects the swapped
247   information to be stored in the native field of the symbol.  */
248
249void
250obj_ecoff_set_ext (sym, ext)
251     symbolS *sym;
252     EXTR *ext;
253{
254  const struct ecoff_debug_swap * const debug_swap
255    = &ecoff_backend (stdoutput)->debug_swap;
256  ecoff_symbol_type *esym;
257
258  know (bfd_asymbol_flavour (symbol_get_bfdsym (sym))
259	== bfd_target_ecoff_flavour);
260  esym = ecoffsymbol (symbol_get_bfdsym (sym));
261  esym->local = FALSE;
262  esym->native = xmalloc (debug_swap->external_ext_size);
263  (*debug_swap->swap_ext_out) (stdoutput, ext, esym->native);
264}
265
266static int
267ecoff_sec_sym_ok_for_reloc (sec)
268     asection *sec ATTRIBUTE_UNUSED;
269{
270  return 1;
271}
272
273static void
274obj_ecoff_frob_symbol (sym, puntp)
275     symbolS *sym;
276     int *puntp ATTRIBUTE_UNUSED;
277{
278  ecoff_frob_symbol (sym);
279}
280
281static void
282ecoff_pop_insert ()
283{
284  pop_insert (obj_pseudo_table);
285}
286
287static int
288ecoff_separate_stab_sections ()
289{
290  return 0;
291}
292
293const struct format_ops ecoff_format_ops =
294{
295  bfd_target_ecoff_flavour,
296  0,	/* dfl_leading_underscore */
297
298  /* FIXME: A comment why emit_section_symbols is different here (1) from
299     the single-format definition (0) would be in order.  */
300  1,	/* emit_section_symbols */
301  0,	/* begin */
302  ecoff_new_file,
303  obj_ecoff_frob_symbol,
304  ecoff_frob_file,
305  0,	/* frob_file_before_adjust */
306  ecoff_frob_file_before_fix,
307  0,	/* frob_file_after_relocs */
308  0,	/* s_get_size */
309  0,	/* s_set_size */
310  0,	/* s_get_align */
311  0,	/* s_set_align */
312  0,	/* s_get_other */
313  0,	/* s_set_other */
314  0,	/* s_get_desc */
315  0,	/* s_set_desc */
316  0,	/* s_get_type */
317  0,	/* s_set_type */
318  0,	/* copy_symbol_attributes */
319  ecoff_generate_asm_lineno,
320  ecoff_stab,
321  ecoff_separate_stab_sections,
322  0,	/* init_stab_section */
323  ecoff_sec_sym_ok_for_reloc,
324  ecoff_pop_insert,
325  ecoff_set_ext,
326  ecoff_read_begin_hook,
327  ecoff_symbol_new_hook
328};
329