| 1 | /* Another try at encapsulating bsd object files in coff. |
| 2 | Copyright (C) 1988, 1989, Free Software Foundation, Inc. |
| 3 | Written by Pace Willisson 12/9/88 |
| 4 | |
| 5 | This file is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 1, or (at your option) |
| 8 | any later version. |
| 9 | |
| 10 | This file is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this file; if not, write to the Free Software |
| 17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
| 18 | |
| 19 | /* |
| 20 | * This time, we will only use the coff headers to tell the kernel |
| 21 | * how to exec the file. Therefore, the only fields that need to |
| 22 | * be filled in are the scnptr and vaddr for the text and data |
| 23 | * sections, and the vaddr for the bss. As far as coff is concerned, |
| 24 | * there is no symbol table, relocation, or line numbers. |
| 25 | * |
| 26 | * A normal bsd header (struct exec) is placed after the coff headers, |
| 27 | * and before the real text. I defined a the new fields 'a_machtype' |
| 28 | * and a_flags. If a_machtype is M_386, and a_flags & A_ENCAP is |
| 29 | * true, then the bsd header is preceeded by a coff header. Macros |
| 30 | * like N_TXTOFF and N_TXTADDR use this field to find the bsd header. |
| 31 | * |
| 32 | * The only problem is to track down the bsd exec header. The |
| 33 | * macros HEADER_OFFSET, etc do this. Look at nm.c, dis.c, etc |
| 34 | * for examples. |
| 35 | */ |
| 36 | |
| 37 | #include "a.out.gnu.h" |
| 38 | |
| 39 | #define N_FLAGS_COFF_ENCAPSULATE 0x20 /* coff header precedes bsd header */ |
| 40 | |
| 41 | /* Describe the COFF header used for encapsulation. */ |
| 42 | |
| 43 | struct coffheader |
| 44 | { |
| 45 | /* filehdr */ |
| 46 | unsigned short f_magic; |
| 47 | unsigned short f_nscns; |
| 48 | long f_timdat; |
| 49 | long f_symptr; |
| 50 | long f_nsyms; |
| 51 | unsigned short f_opthdr; |
| 52 | unsigned short f_flags; |
| 53 | /* aouthdr */ |
| 54 | short magic; |
| 55 | short vstamp; |
| 56 | long tsize; |
| 57 | long dsize; |
| 58 | long bsize; |
| 59 | long entry; |
| 60 | long text_start; |
| 61 | long data_start; |
| 62 | struct coffscn |
| 63 | { |
| 64 | char s_name[8]; |
| 65 | long s_paddr; |
| 66 | long s_vaddr; |
| 67 | long s_size; |
| 68 | long s_scnptr; |
| 69 | long s_relptr; |
| 70 | long s_lnnoptr; |
| 71 | unsigned short s_nreloc; |
| 72 | unsigned short s_nlnno; |
| 73 | long s_flags; |
| 74 | } scns[3]; |
| 75 | }; |
| 76 | \f |
| 77 | /* Describe some of the parameters of the encapsulation, |
| 78 | including how to find the encapsulated BSD header. */ |
| 79 | |
| 80 | #ifdef i386 |
| 81 | #define COFF_MAGIC 0514 /* I386MAGIC */ |
| 82 | #endif |
| 83 | #ifdef m68k |
| 84 | #define COFF_MAGIC 0520 /* MC68MAGIC */ |
| 85 | #endif |
| 86 | |
| 87 | #ifdef COFF_MAGIC |
| 88 | short __header_offset_temp; |
| 89 | #define HEADER_OFFSET(f) \ |
| 90 | (__header_offset_temp = 0, \ |
| 91 | fread ((char *)&__header_offset_temp, sizeof (short), 1, (f)), \ |
| 92 | fseek ((f), -sizeof (short), 1), \ |
| 93 | __header_offset_temp==COFF_MAGIC ? sizeof(struct coffheader) : 0) |
| 94 | |
| 95 | #define HEADER_OFFSET_FD(fd) \ |
| 96 | (__header_offset_temp = 0, \ |
| 97 | read ((fd), (char *)&__header_offset_temp, sizeof (short)), \ |
| 98 | lseek ((fd), -sizeof (short), 1), \ |
| 99 | __header_offset_temp==COFF_MAGIC ? sizeof(struct coffheader) : 0) |
| 100 | |
| 101 | |
| 102 | #else |
| 103 | #define HEADER_OFFSET(f) 0 |
| 104 | #define HEADER_OFFSET_FD(fd) 0 |
| 105 | #endif |
| 106 | |
| 107 | #define HEADER_SEEK(f) (fseek ((f), HEADER_OFFSET((f)), 1)) |
| 108 | #define HEADER_SEEK_FD(fd) (lseek ((fd), HEADER_OFFSET_FD((fd)), 1)) |
| 109 | |
| 110 | \f |
| 111 | /* Describe the characteristics of the BSD header |
| 112 | that appears inside the encapsulation. */ |
| 113 | |
| 114 | #undef _N_HDROFF |
| 115 | #undef N_TXTADDR |
| 116 | #undef N_DATADDR |
| 117 | |
| 118 | #define _N_HDROFF(x) ((N_FLAGS(x) & N_FLAGS_COFF_ENCAPSULATE) ? \ |
| 119 | sizeof (struct coffheader) : 0) |
| 120 | |
| 121 | /* Address of text segment in memory after it is loaded. */ |
| 122 | #define N_TXTADDR(x) \ |
| 123 | ((N_FLAGS(x) & N_FLAGS_COFF_ENCAPSULATE) ? \ |
| 124 | sizeof (struct coffheader) + sizeof (struct exec) : 0) |
| 125 | #define SEGMENT_SIZE 0x400000 |
| 126 | |
| 127 | #define N_DATADDR(x) \ |
| 128 | ((N_FLAGS(x) & N_FLAGS_COFF_ENCAPSULATE) ? \ |
| 129 | (SEGMENT_SIZE + ((N_TXTADDR(x)+(x).a_text-1) & ~(SEGMENT_SIZE-1))) : \ |
| 130 | (N_TXTADDR(x)+(x).a_text)) |