1440a403fSchristos /* ECOFF object file format.
2*b88e3e88Schristos Copyright (C) 1993-2020 Free Software Foundation, Inc.
3440a403fSchristos Contributed by Cygnus Support.
4440a403fSchristos This file was put together by Ian Lance Taylor <ian@cygnus.com>.
5440a403fSchristos
6440a403fSchristos This file is part of GAS.
7440a403fSchristos
8440a403fSchristos GAS is free software; you can redistribute it and/or modify
9440a403fSchristos it under the terms of the GNU General Public License as published by
10440a403fSchristos the Free Software Foundation; either version 3, or (at your option)
11440a403fSchristos any later version.
12440a403fSchristos
13440a403fSchristos GAS is distributed in the hope that it will be useful,
14440a403fSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
15440a403fSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16440a403fSchristos GNU General Public License for more details.
17440a403fSchristos
18440a403fSchristos You should have received a copy of the GNU General Public License
19440a403fSchristos along with GAS; see the file COPYING. If not, write to the Free
20440a403fSchristos Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21440a403fSchristos 02110-1301, USA. */
22440a403fSchristos
23440a403fSchristos #define OBJ_HEADER "obj-ecoff.h"
24440a403fSchristos #include "as.h"
25440a403fSchristos #include "coff/internal.h"
26440a403fSchristos #include "bfd/libcoff.h"
27440a403fSchristos #include "bfd/libecoff.h"
28*b88e3e88Schristos #include "bfd/ecoff-bfd.h"
29440a403fSchristos
30440a403fSchristos /* Almost all of the ECOFF support is actually in ecoff.c in the main
31440a403fSchristos gas directory. This file mostly just arranges to call that one at
32440a403fSchristos the right times. */
33440a403fSchristos
34440a403fSchristos /* Set section VMAs and GP values before reloc processing. */
35440a403fSchristos
36440a403fSchristos void
ecoff_frob_file_before_fix(void)37440a403fSchristos ecoff_frob_file_before_fix (void)
38440a403fSchristos {
39440a403fSchristos bfd_vma addr;
40440a403fSchristos asection *sec;
41440a403fSchristos
42440a403fSchristos /* Set the section VMA values. We force the .sdata and .sbss
43440a403fSchristos sections to the end to ensure that their VMA addresses are close
44440a403fSchristos together so that the GP register can address both of them. We
45440a403fSchristos put the .bss section after the .sbss section.
46440a403fSchristos
47440a403fSchristos Also, for the Alpha, we must sort the sections, to make sure they
48440a403fSchristos appear in the output file in the correct order. (Actually, maybe
49440a403fSchristos this is a job for BFD. But the VMAs computed would be out of
50440a403fSchristos whack if we computed them given our initial, random ordering.
51440a403fSchristos It's possible that that wouldn't break things; I could do some
52440a403fSchristos experimenting sometime and find out.
53440a403fSchristos
54440a403fSchristos This output ordering of sections is magic, on the Alpha, at
55440a403fSchristos least. The .lita section must come before .lit8 and .lit4,
56440a403fSchristos otherwise the OSF/1 linker may silently trash the .lit{4,8}
5706324dcfSchristos section contents. Also, .text must precede .rdata. These differ
58440a403fSchristos from the order described in some parts of the DEC OSF/1 Assembly
59440a403fSchristos Language Programmer's Guide, but that order doesn't seem to work
60440a403fSchristos with their linker.
61440a403fSchristos
62440a403fSchristos I don't know if section ordering on the MIPS is important. */
63440a403fSchristos
64440a403fSchristos static const char *const names[] =
65440a403fSchristos {
66440a403fSchristos /* text segment */
67440a403fSchristos ".text", ".rdata", ".init", ".fini",
68440a403fSchristos /* data segment */
69440a403fSchristos ".data", ".lita", ".lit8", ".lit4", ".sdata", ".got",
70440a403fSchristos /* bss segment */
71440a403fSchristos ".sbss", ".bss",
72440a403fSchristos };
73440a403fSchristos #define n_names ((int) (sizeof (names) / sizeof (names[0])))
74440a403fSchristos
75440a403fSchristos /* Sections that match names, order to be straightened out later. */
76440a403fSchristos asection *secs[n_names];
77440a403fSchristos int i;
78440a403fSchristos
79440a403fSchristos addr = 0;
80440a403fSchristos for (i = 0; i < n_names; i++)
81440a403fSchristos secs[i] = NULL;
82440a403fSchristos
83440a403fSchristos for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
84440a403fSchristos {
85440a403fSchristos for (i = 0; i < n_names; i++)
86440a403fSchristos if (!strcmp (sec->name, names[i]))
87440a403fSchristos {
88440a403fSchristos secs[i] = sec;
89440a403fSchristos bfd_section_list_remove (stdoutput, sec);
90440a403fSchristos break;
91440a403fSchristos }
92440a403fSchristos if (i == n_names)
93440a403fSchristos {
94*b88e3e88Schristos bfd_set_section_vma (sec, addr);
95*b88e3e88Schristos addr += bfd_section_size (sec);
96440a403fSchristos }
97440a403fSchristos }
98440a403fSchristos for (i = 0; i < n_names; i++)
99440a403fSchristos if (secs[i])
100440a403fSchristos {
101*b88e3e88Schristos bfd_set_section_vma (secs[i], addr);
102*b88e3e88Schristos addr += bfd_section_size (secs[i]);
103440a403fSchristos }
104440a403fSchristos for (i = n_names - 1; i >= 0; i--)
105440a403fSchristos if (secs[i])
106440a403fSchristos bfd_section_list_prepend (stdoutput, secs[i]);
107440a403fSchristos
108440a403fSchristos /* Fill in the register masks. */
109440a403fSchristos {
110440a403fSchristos unsigned long gprmask = 0;
111440a403fSchristos unsigned long fprmask = 0;
112440a403fSchristos unsigned long *cprmask = NULL;
113440a403fSchristos
114440a403fSchristos #ifdef TC_MIPS
115440a403fSchristos /* Fill in the MIPS register masks. It's probably not worth
116440a403fSchristos setting up a generic interface for this. */
117440a403fSchristos gprmask = mips_gprmask;
118440a403fSchristos cprmask = mips_cprmask;
119440a403fSchristos #endif
120440a403fSchristos
121440a403fSchristos #ifdef TC_ALPHA
122440a403fSchristos alpha_frob_ecoff_data ();
123440a403fSchristos
124440a403fSchristos if (! bfd_ecoff_set_gp_value (stdoutput, alpha_gp_value))
125440a403fSchristos as_fatal (_("Can't set GP value"));
126440a403fSchristos
127440a403fSchristos gprmask = alpha_gprmask;
128440a403fSchristos fprmask = alpha_fprmask;
129440a403fSchristos #endif
130440a403fSchristos
131440a403fSchristos if (! bfd_ecoff_set_regmasks (stdoutput, gprmask, fprmask, cprmask))
132440a403fSchristos as_fatal (_("Can't set register masks"));
133440a403fSchristos }
134440a403fSchristos }
135440a403fSchristos
136440a403fSchristos /* Swap out the symbols and debugging information for BFD. */
137440a403fSchristos
138440a403fSchristos void
ecoff_frob_file(void)139440a403fSchristos ecoff_frob_file (void)
140440a403fSchristos {
141440a403fSchristos const struct ecoff_debug_swap * const debug_swap
142440a403fSchristos = &ecoff_backend (stdoutput)->debug_swap;
143440a403fSchristos bfd_vma addr ATTRIBUTE_UNUSED;
144440a403fSchristos HDRR *hdr;
145440a403fSchristos char *buf;
146440a403fSchristos char *set;
147440a403fSchristos
148440a403fSchristos /* Build the ECOFF debugging information. */
149440a403fSchristos gas_assert (ecoff_data (stdoutput) != 0);
150440a403fSchristos hdr = &ecoff_data (stdoutput)->debug_info.symbolic_header;
151440a403fSchristos ecoff_build_debug (hdr, &buf, debug_swap);
152440a403fSchristos
153440a403fSchristos /* Finish up the ecoff_tdata structure. */
154440a403fSchristos set = buf;
155440a403fSchristos #define SET(ptr, count, type, size) \
156440a403fSchristos if (hdr->count == 0) \
157440a403fSchristos ecoff_data (stdoutput)->debug_info.ptr = NULL; \
158440a403fSchristos else \
159440a403fSchristos { \
160440a403fSchristos ecoff_data (stdoutput)->debug_info.ptr = (type) set; \
161440a403fSchristos set += hdr->count * size; \
162440a403fSchristos }
163440a403fSchristos
164440a403fSchristos SET (line, cbLine, unsigned char *, sizeof (unsigned char));
165440a403fSchristos SET (external_dnr, idnMax, void *, debug_swap->external_dnr_size);
166440a403fSchristos SET (external_pdr, ipdMax, void *, debug_swap->external_pdr_size);
167440a403fSchristos SET (external_sym, isymMax, void *, debug_swap->external_sym_size);
168440a403fSchristos SET (external_opt, ioptMax, void *, debug_swap->external_opt_size);
169440a403fSchristos SET (external_aux, iauxMax, union aux_ext *, sizeof (union aux_ext));
170440a403fSchristos SET (ss, issMax, char *, sizeof (char));
171440a403fSchristos SET (ssext, issExtMax, char *, sizeof (char));
172440a403fSchristos SET (external_rfd, crfd, void *, debug_swap->external_rfd_size);
173440a403fSchristos SET (external_fdr, ifdMax, void *, debug_swap->external_fdr_size);
174440a403fSchristos SET (external_ext, iextMax, void *, debug_swap->external_ext_size);
175440a403fSchristos #undef SET
176440a403fSchristos }
177440a403fSchristos
178440a403fSchristos /* This is called by the ECOFF code to set the external information
179440a403fSchristos for a symbol. We just pass it on to BFD, which expects the swapped
180440a403fSchristos information to be stored in the native field of the symbol. */
181440a403fSchristos
182440a403fSchristos void
obj_ecoff_set_ext(symbolS * sym,EXTR * ext)183440a403fSchristos obj_ecoff_set_ext (symbolS *sym, EXTR *ext)
184440a403fSchristos {
185440a403fSchristos const struct ecoff_debug_swap * const debug_swap
186440a403fSchristos = &ecoff_backend (stdoutput)->debug_swap;
187440a403fSchristos ecoff_symbol_type *esym;
188440a403fSchristos
189440a403fSchristos know (bfd_asymbol_flavour (symbol_get_bfdsym (sym))
190440a403fSchristos == bfd_target_ecoff_flavour);
191440a403fSchristos esym = ecoffsymbol (symbol_get_bfdsym (sym));
192440a403fSchristos esym->local = FALSE;
193440a403fSchristos esym->native = xmalloc (debug_swap->external_ext_size);
194440a403fSchristos (*debug_swap->swap_ext_out) (stdoutput, ext, esym->native);
195440a403fSchristos }
196440a403fSchristos
197440a403fSchristos static int
ecoff_sec_sym_ok_for_reloc(asection * sec ATTRIBUTE_UNUSED)198440a403fSchristos ecoff_sec_sym_ok_for_reloc (asection *sec ATTRIBUTE_UNUSED)
199440a403fSchristos {
200440a403fSchristos return 1;
201440a403fSchristos }
202440a403fSchristos
203440a403fSchristos static void
obj_ecoff_frob_symbol(symbolS * sym,int * puntp ATTRIBUTE_UNUSED)204440a403fSchristos obj_ecoff_frob_symbol (symbolS *sym, int *puntp ATTRIBUTE_UNUSED)
205440a403fSchristos {
206440a403fSchristos ecoff_frob_symbol (sym);
207440a403fSchristos }
208440a403fSchristos
209440a403fSchristos static void
ecoff_pop_insert(void)210440a403fSchristos ecoff_pop_insert (void)
211440a403fSchristos {
212440a403fSchristos pop_insert (obj_pseudo_table);
213440a403fSchristos }
214440a403fSchristos
215440a403fSchristos static int
ecoff_separate_stab_sections(void)216440a403fSchristos ecoff_separate_stab_sections (void)
217440a403fSchristos {
218440a403fSchristos return 0;
219440a403fSchristos }
220440a403fSchristos
221440a403fSchristos /* These are the pseudo-ops we support in this file. Only those
222440a403fSchristos relating to debugging information are supported here.
223440a403fSchristos
224440a403fSchristos The following pseudo-ops from the Kane and Heinrich MIPS book
22506324dcfSchristos should be defined here, but are currently unsupported: .bgnb,
22606324dcfSchristos .endb, .verstamp, .vreg.
227440a403fSchristos
228440a403fSchristos The following pseudo-ops from the Kane and Heinrich MIPS book are
229440a403fSchristos MIPS CPU specific, and should be defined by tc-mips.c: .alias,
230440a403fSchristos .extern, .galive, .gjaldef, .gjrlive, .livereg, .noalias, .option,
231440a403fSchristos .rdata, .sdata, .set.
232440a403fSchristos
233440a403fSchristos The following pseudo-ops from the Kane and Heinrich MIPS book are
234440a403fSchristos not MIPS CPU specific, but are also not ECOFF specific. I have
235440a403fSchristos only listed the ones which are not already in read.c. It's not
236440a403fSchristos completely clear where these should be defined, but tc-mips.c is
237440a403fSchristos probably the most reasonable place: .asciiz, .asm0, .endr, .err,
238440a403fSchristos .half, .lab, .repeat, .struct, .weakext. */
239440a403fSchristos
240440a403fSchristos const pseudo_typeS obj_pseudo_table[] =
241440a403fSchristos {
242440a403fSchristos /* COFF style debugging information. .ln is not used; .loc is used
243440a403fSchristos instead. */
244440a403fSchristos { "def", ecoff_directive_def, 0 },
245440a403fSchristos { "dim", ecoff_directive_dim, 0 },
246440a403fSchristos { "endef", ecoff_directive_endef, 0 },
247440a403fSchristos { "file", ecoff_directive_file, 0 },
248440a403fSchristos { "scl", ecoff_directive_scl, 0 },
249440a403fSchristos { "size", ecoff_directive_size, 0 },
250440a403fSchristos { "esize", ecoff_directive_size, 0 },
251440a403fSchristos { "tag", ecoff_directive_tag, 0 },
252440a403fSchristos { "type", ecoff_directive_type, 0 },
253440a403fSchristos { "etype", ecoff_directive_type, 0 },
254440a403fSchristos { "val", ecoff_directive_val, 0 },
255440a403fSchristos
256440a403fSchristos /* ECOFF specific debugging information. */
25706324dcfSchristos { "aent", ecoff_directive_ent, 1 },
258440a403fSchristos { "begin", ecoff_directive_begin, 0 },
259440a403fSchristos { "bend", ecoff_directive_bend, 0 },
260440a403fSchristos { "end", ecoff_directive_end, 0 },
261440a403fSchristos { "ent", ecoff_directive_ent, 0 },
262440a403fSchristos { "fmask", ecoff_directive_fmask, 0 },
263440a403fSchristos { "frame", ecoff_directive_frame, 0 },
264440a403fSchristos { "loc", ecoff_directive_loc, 0 },
265440a403fSchristos { "mask", ecoff_directive_mask, 0 },
266440a403fSchristos
267440a403fSchristos /* Other ECOFF directives. */
268440a403fSchristos { "extern", ecoff_directive_extern, 0 },
269440a403fSchristos
270440a403fSchristos #ifndef TC_MIPS
271440a403fSchristos /* For TC_MIPS, tc-mips.c adds this. */
272440a403fSchristos { "weakext", ecoff_directive_weakext, 0 },
273440a403fSchristos #endif
274440a403fSchristos
275440a403fSchristos /* These are used on Irix. I don't know how to implement them. */
276440a403fSchristos { "bgnb", s_ignore, 0 },
277440a403fSchristos { "endb", s_ignore, 0 },
278440a403fSchristos { "verstamp", s_ignore, 0 },
279440a403fSchristos
280440a403fSchristos /* Sentinel. */
281440a403fSchristos { NULL, s_ignore, 0 }
282440a403fSchristos };
283440a403fSchristos
284440a403fSchristos const struct format_ops ecoff_format_ops =
285440a403fSchristos {
286440a403fSchristos bfd_target_ecoff_flavour,
287440a403fSchristos 0, /* dfl_leading_underscore. */
288440a403fSchristos
289440a403fSchristos /* FIXME: A comment why emit_section_symbols is different here (1) from
290440a403fSchristos the single-format definition (0) would be in order. */
291440a403fSchristos 1, /* emit_section_symbols. */
292440a403fSchristos 0, /* begin. */
293440a403fSchristos ecoff_new_file,
294440a403fSchristos obj_ecoff_frob_symbol,
295440a403fSchristos ecoff_frob_file,
296440a403fSchristos 0, /* frob_file_before_adjust. */
297440a403fSchristos ecoff_frob_file_before_fix,
298440a403fSchristos 0, /* frob_file_after_relocs. */
299440a403fSchristos 0, /* s_get_size. */
300440a403fSchristos 0, /* s_set_size. */
301440a403fSchristos 0, /* s_get_align. */
302440a403fSchristos 0, /* s_set_align. */
303440a403fSchristos 0, /* s_get_other. */
304440a403fSchristos 0, /* s_set_other. */
305440a403fSchristos 0, /* s_get_desc. */
306440a403fSchristos 0, /* s_set_desc. */
307440a403fSchristos 0, /* s_get_type. */
308440a403fSchristos 0, /* s_set_type. */
309440a403fSchristos 0, /* copy_symbol_attributes. */
310440a403fSchristos ecoff_generate_asm_lineno,
311440a403fSchristos ecoff_stab,
312440a403fSchristos ecoff_separate_stab_sections,
313440a403fSchristos 0, /* init_stab_section. */
314440a403fSchristos ecoff_sec_sym_ok_for_reloc,
315440a403fSchristos ecoff_pop_insert,
316440a403fSchristos ecoff_set_ext,
317440a403fSchristos ecoff_read_begin_hook,
318440a403fSchristos ecoff_symbol_new_hook,
319440a403fSchristos ecoff_symbol_clone_hook,
320440a403fSchristos 0 /* adjust_symtab. */
321440a403fSchristos };
322