133965Sjdp/* Yet Another Try at encapsulating bsd object files in coff.
278828Sobrien   Copyright 1988, 1989, 1991 Free Software Foundation, Inc.
333965Sjdp   Written by Pace Willisson 12/9/88
433965Sjdp
533965Sjdp   This file is obsolete.  It needs to be converted to just define a bunch
633965Sjdp   of stuff that BFD can use to do coff-encapsulated files.  --gnu@cygnus.com
733965Sjdp
833965SjdpThis program is free software; you can redistribute it and/or modify
933965Sjdpit under the terms of the GNU General Public License as published by
1033965Sjdpthe Free Software Foundation; either version 2 of the License, or
1133965Sjdp(at your option) any later version.
1233965Sjdp
1333965SjdpThis program is distributed in the hope that it will be useful,
1433965Sjdpbut WITHOUT ANY WARRANTY; without even the implied warranty of
1533965SjdpMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1633965SjdpGNU General Public License for more details.
1733965Sjdp
1833965SjdpYou should have received a copy of the GNU General Public License
1933965Sjdpalong with this program; if not, write to the Free Software
20218822SdimFoundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
2133965Sjdp
2233965Sjdp/*
2333965Sjdp * We only use the coff headers to tell the kernel
2433965Sjdp * how to exec the file.  Therefore, the only fields that need to
2533965Sjdp * be filled in are the scnptr and vaddr for the text and data
2633965Sjdp * sections, and the vaddr for the bss.  As far as coff is concerned,
2733965Sjdp * there is no symbol table, relocation, or line numbers.
2833965Sjdp *
2933965Sjdp * A normal bsd header (struct exec) is placed after the coff headers,
3033965Sjdp * and before the real text.  I defined a the new fields 'a_machtype'
3133965Sjdp * and a_flags.  If a_machtype is M_386, and a_flags & A_ENCAP is
3233965Sjdp * true, then the bsd header is preceeded by a coff header.  Macros
3333965Sjdp * like N_TXTOFF and N_TXTADDR use this field to find the bsd header.
3433965Sjdp *
3533965Sjdp * The only problem is to track down the bsd exec header.  The
3633965Sjdp * macros HEADER_OFFSET, etc do this.
3733965Sjdp */
3833965Sjdp
3933965Sjdp#define N_FLAGS_COFF_ENCAPSULATE 0x20 /* coff header precedes bsd header */
4033965Sjdp
4133965Sjdp/* Describe the COFF header used for encapsulation.  */
4233965Sjdp
4333965Sjdpstruct coffheader
4433965Sjdp{
4533965Sjdp  /* filehdr */
4633965Sjdp  unsigned short f_magic;
4733965Sjdp  unsigned short f_nscns;
4833965Sjdp  long f_timdat;
4933965Sjdp  long f_symptr;
5033965Sjdp  long f_nsyms;
5133965Sjdp  unsigned short f_opthdr;
5233965Sjdp  unsigned short f_flags;
5333965Sjdp  /* aouthdr */
5433965Sjdp  short magic;
5533965Sjdp  short vstamp;
5633965Sjdp  long tsize;
5733965Sjdp  long dsize;
5833965Sjdp  long bsize;
5933965Sjdp  long entry;
6033965Sjdp  long text_start;
6133965Sjdp  long data_start;
6233965Sjdp  struct coffscn
6333965Sjdp    {
6433965Sjdp      char s_name[8];
6533965Sjdp      long s_paddr;
6633965Sjdp      long s_vaddr;
6733965Sjdp      long s_size;
6833965Sjdp      long s_scnptr;
6933965Sjdp      long s_relptr;
7033965Sjdp      long s_lnnoptr;
7133965Sjdp      unsigned short s_nreloc;
7233965Sjdp      unsigned short s_nlnno;
7333965Sjdp      long s_flags;
7433965Sjdp    } scns[3];
7533965Sjdp};
7633965Sjdp
7733965Sjdp/* Describe some of the parameters of the encapsulation,
7833965Sjdp   including how to find the encapsulated BSD header.  */
7933965Sjdp
8033965Sjdp/* FIXME, this is dumb.  The same tools can't handle a.outs for different
8133965Sjdp   architectures, just because COFF_MAGIC is different; so you need a
8233965Sjdp   separate GNU nm for every architecture!!?  Unfortunately, it needs to
8333965Sjdp   be this way, since the COFF_MAGIC value is determined by the kernel
8433965Sjdp   we're trying to fool here.  */
8533965Sjdp
8633965Sjdp#define COFF_MAGIC_I386 0514 /* I386MAGIC */
8733965Sjdp#define COFF_MAGIC_M68K 0520 /* MC68MAGIC */
8833965Sjdp
8933965Sjdp#ifdef COFF_MAGIC
9033965Sjdpshort __header_offset_temp;
9133965Sjdp#define HEADER_OFFSET(f) \
9233965Sjdp	(__header_offset_temp = 0, \
9333965Sjdp	 fread ((char *)&__header_offset_temp, sizeof (short), 1, (f)), \
9433965Sjdp	 fseek ((f), -sizeof (short), 1), \
9533965Sjdp	 __header_offset_temp==COFF_MAGIC ? sizeof(struct coffheader) : 0)
9633965Sjdp#else
9733965Sjdp#define HEADER_OFFSET(f) 0
9833965Sjdp#endif
9933965Sjdp
10033965Sjdp#define HEADER_SEEK(f) (fseek ((f), HEADER_OFFSET((f)), 1))
10133965Sjdp
10233965Sjdp/* Describe the characteristics of the BSD header
10333965Sjdp   that appears inside the encapsulation.  */
10433965Sjdp
10533965Sjdp/* Encapsulated coff files that are linked ZMAGIC have a text segment
10633965Sjdp   offset just past the header (and a matching TXTADDR), excluding
10733965Sjdp   the headers from the text segment proper but keeping the physical
10833965Sjdp   layout and the virtual memory layout page-aligned.
10933965Sjdp
11033965Sjdp   Non-encapsulated a.out files that are linked ZMAGIC have a text
11133965Sjdp   segment that starts at 0 and an N_TXTADR similarly offset to 0.
11233965Sjdp   They too are page-aligned with each other, but they include the
11333965Sjdp   a.out header as part of the text.
11433965Sjdp
11533965Sjdp   The _N_HDROFF gets sizeof struct exec added to it, so we have
11633965Sjdp   to compensate here.  See <a.out.gnu.h>.  */
11733965Sjdp
11833965Sjdp#undef _N_HDROFF
11933965Sjdp#undef N_TXTADDR
12033965Sjdp#undef N_DATADDR
12133965Sjdp
12233965Sjdp#define _N_HDROFF(x) ((N_FLAGS(x) & N_FLAGS_COFF_ENCAPSULATE) ? \
12333965Sjdp		      sizeof (struct coffheader) : 0)
12433965Sjdp
12533965Sjdp/* Address of text segment in memory after it is loaded.  */
12633965Sjdp#define N_TXTADDR(x) \
12733965Sjdp	((N_FLAGS(x) & N_FLAGS_COFF_ENCAPSULATE) ? \
12833965Sjdp	 sizeof (struct coffheader) + sizeof (struct exec) : 0)
12933965Sjdp#define SEGMENT_SIZE 0x400000
13033965Sjdp
13133965Sjdp#define N_DATADDR(x) \
13233965Sjdp	((N_FLAGS(x) & N_FLAGS_COFF_ENCAPSULATE) ? \
13333965Sjdp	 (SEGMENT_SIZE + ((N_TXTADDR(x)+(x).a_text-1) & ~(SEGMENT_SIZE-1))) : \
13433965Sjdp	 (N_TXTADDR(x)+(x).a_text))
135