1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /* This file is derived from the GAS 2.1.4 assembler control file.
3    The GAS product is under the GNU General Public License, version 2 or later.
4    As such, this file is also under that license.
5 
6    If the file format changes in the COFF object, this file should be
7    subsequently updated to reflect the changes.
8 
9    The actual loader module only uses a few of these structures. The full
10    set is documented here because I received the full set. If you wish
11    more information about COFF, then O'Reilly has a very excellent book.
12 */
13 
14 #define  E_SYMNMLEN  8   /* Number of characters in a symbol name         */
15 #define  E_FILNMLEN 14   /* Number of characters in a file name           */
16 #define  E_DIMNUM    4   /* Number of array dimensions in auxiliary entry */
17 
18 /*
19  * These defines are byte order independent. There is no alignment of fields
20  * permitted in the structures. Therefore they are declared as characters
21  * and the values loaded from the character positions. It also makes it
22  * nice to have it "endian" independent.
23  */
24 
25 /* Load a short int from the following tables with little-endian formats */
26 #define COFF_SHORT_L(ps) ((short)(((unsigned short)((unsigned char)ps[1])<<8)|\
27 				  ((unsigned short)((unsigned char)ps[0]))))
28 
29 /* Load a long int from the following tables with little-endian formats */
30 #define COFF_LONG_L(ps) (((long)(((unsigned long)((unsigned char)ps[3])<<24) |\
31 				 ((unsigned long)((unsigned char)ps[2])<<16) |\
32 				 ((unsigned long)((unsigned char)ps[1])<<8)  |\
33 				 ((unsigned long)((unsigned char)ps[0])))))
34 
35 /* Load a short int from the following tables with big-endian formats */
36 #define COFF_SHORT_H(ps) ((short)(((unsigned short)((unsigned char)ps[0])<<8)|\
37 				  ((unsigned short)((unsigned char)ps[1]))))
38 
39 /* Load a long int from the following tables with big-endian formats */
40 #define COFF_LONG_H(ps) (((long)(((unsigned long)((unsigned char)ps[0])<<24) |\
41 				 ((unsigned long)((unsigned char)ps[1])<<16) |\
42 				 ((unsigned long)((unsigned char)ps[2])<<8)  |\
43 				 ((unsigned long)((unsigned char)ps[3])))))
44 
45 /* These may be overridden later by brain dead implementations which generate
46    a big-endian header with little-endian data. In that case, generate a
47    replacement macro which tests a flag and uses either of the two above
48    as appropriate. */
49 
50 #define COFF_LONG(v)   COFF_LONG_L(v)
51 #define COFF_SHORT(v)  COFF_SHORT_L(v)
52 
53 /*** coff information for Intel 386/486.  */
54 
55 /********************** FILE HEADER **********************/
56 
57 struct COFF_filehdr {
58 	char f_magic[2];	/* magic number			*/
59 	char f_nscns[2];	/* number of sections		*/
60 	char f_timdat[4];	/* time & date stamp		*/
61 	char f_symptr[4];	/* file pointer to symtab	*/
62 	char f_nsyms[4];	/* number of symtab entries	*/
63 	char f_opthdr[2];	/* sizeof(optional hdr)		*/
64 	char f_flags[2];	/* flags			*/
65 };
66 
67 /*
68  *   Bits for f_flags:
69  *
70  *	F_RELFLG	relocation info stripped from file
71  *	F_EXEC		file is executable  (i.e. no unresolved external
72  *			references)
73  *	F_LNNO		line numbers stripped from file
74  *	F_LSYMS		local symbols stripped from file
75  *	F_MINMAL	this is a minimal object file (".m") output of fextract
76  *	F_UPDATE	this is a fully bound update file, output of ogen
77  *	F_SWABD		this file has had its bytes swabbed (in names)
78  *	F_AR16WR	this file has the byte ordering of an AR16WR
79  *			(e.g. 11/70) machine
80  *	F_AR32WR	this file has the byte ordering of an AR32WR machine
81  *			(e.g. vax and iNTEL 386)
82  *	F_AR32W		this file has the byte ordering of an AR32W machine
83  *			(e.g. 3b,maxi)
84  *	F_PATCH		file contains "patch" list in optional header
85  *	F_NODF		(minimal file only) no decision functions for
86  *			replaced functions
87  */
88 
89 #define  COFF_F_RELFLG		0000001
90 #define  COFF_F_EXEC		0000002
91 #define  COFF_F_LNNO		0000004
92 #define  COFF_F_LSYMS		0000010
93 #define  COFF_F_MINMAL		0000020
94 #define  COFF_F_UPDATE		0000040
95 #define  COFF_F_SWABD		0000100
96 #define  COFF_F_AR16WR		0000200
97 #define  COFF_F_AR32WR		0000400
98 #define  COFF_F_AR32W		0001000
99 #define  COFF_F_PATCH		0002000
100 #define  COFF_F_NODF		0002000
101 
102 #define	COFF_I386MAGIC	        0x14c   /* Linux's system    */
103 
104 #if 0   /* Perhaps, someday, these formats may be used.      */
105 #define COFF_I386PTXMAGIC	0x154
106 #define COFF_I386AIXMAGIC	0x175   /* IBM's AIX system  */
107 #define COFF_I386BADMAG(x) ((COFF_SHORT((x).f_magic) != COFF_I386MAGIC) \
108 			  && COFF_SHORT((x).f_magic) != COFF_I386PTXMAGIC \
109 			  && COFF_SHORT((x).f_magic) != COFF_I386AIXMAGIC)
110 #else
111 #define COFF_I386BADMAG(x) (COFF_SHORT((x).f_magic) != COFF_I386MAGIC)
112 #endif
113 
114 #define	COFF_FILHDR	struct COFF_filehdr
115 #define	COFF_FILHSZ	sizeof(COFF_FILHDR)
116 
117 /********************** AOUT "OPTIONAL HEADER" **********************/
118 
119 /* Linux COFF must have this "optional" header. Standard COFF has no entry
120    location for the "entry" point. They normally would start with the first
121    location of the .text section. This is not a good idea for linux. So,
122    the use of this "optional" header is not optional. It is required.
123 
124    Do not be tempted to assume that the size of the optional header is
125    a constant and simply index the next byte by the size of this structure.
126    Use the 'f_opthdr' field in the main coff header for the size of the
127    structure actually written to the file!!
128 */
129 
130 typedef struct
131 {
132   char 	magic[2];		/* type of file				 */
133   char	vstamp[2];		/* version stamp			 */
134   char	tsize[4];		/* text size in bytes, padded to FW bdry */
135   char	dsize[4];		/* initialized   data "   "		 */
136   char	bsize[4];		/* uninitialized data "   "		 */
137   char	entry[4];		/* entry pt.				 */
138   char 	text_start[4];		/* base of text used for this file       */
139   char 	data_start[4];		/* base of data used for this file       */
140 }
141 COFF_AOUTHDR;
142 
143 #define COFF_AOUTSZ (sizeof(COFF_AOUTHDR))
144 
145 #define COFF_STMAGIC	0401
146 #define COFF_OMAGIC     0404
147 #define COFF_JMAGIC     0407    /* dirty text and data image, can't share  */
148 #define COFF_DMAGIC     0410    /* dirty text segment, data aligned        */
149 #define COFF_ZMAGIC     0413    /* The proper magic number for executables  */
150 #define COFF_SHMAGIC	0443	/* shared library header                   */
151 
152 /********************** SECTION HEADER **********************/
153 
154 struct COFF_scnhdr {
155   char		s_name[8];	/* section name			    */
156   char		s_paddr[4];	/* physical address, aliased s_nlib */
157   char		s_vaddr[4];	/* virtual address		    */
158   char		s_size[4];	/* section size			    */
159   char		s_scnptr[4];	/* file ptr to raw data for section */
160   char		s_relptr[4];	/* file ptr to relocation	    */
161   char		s_lnnoptr[4];	/* file ptr to line numbers	    */
162   char		s_nreloc[2];	/* number of relocation entries	    */
163   char		s_nlnno[2];	/* number of line number entries    */
164   char		s_flags[4];	/* flags			    */
165 };
166 
167 #define	COFF_SCNHDR	struct COFF_scnhdr
168 #define	COFF_SCNHSZ	sizeof(COFF_SCNHDR)
169 
170 /*
171  * names of "special" sections
172  */
173 
174 #define COFF_TEXT	".text"
175 #define COFF_DATA	".data"
176 #define COFF_BSS	".bss"
177 #define COFF_COMMENT    ".comment"
178 #define COFF_LIB        ".lib"
179 
180 #define COFF_SECT_TEXT  0      /* Section for instruction code             */
181 #define COFF_SECT_DATA  1      /* Section for initialized globals          */
182 #define COFF_SECT_BSS   2      /* Section for un-initialized globals       */
183 #define COFF_SECT_REQD  3      /* Minimum number of sections for good file */
184 
185 #define COFF_STYP_REG     0x00 /* regular segment                          */
186 #define COFF_STYP_DSECT   0x01 /* dummy segment                            */
187 #define COFF_STYP_NOLOAD  0x02 /* no-load segment                          */
188 #define COFF_STYP_GROUP   0x04 /* group segment                            */
189 #define COFF_STYP_PAD     0x08 /* .pad segment                             */
190 #define COFF_STYP_COPY    0x10 /* copy section                             */
191 #define COFF_STYP_TEXT    0x20 /* .text segment                            */
192 #define COFF_STYP_DATA    0x40 /* .data segment                            */
193 #define COFF_STYP_BSS     0x80 /* .bss segment                             */
194 #define COFF_STYP_INFO   0x200 /* .comment section                         */
195 #define COFF_STYP_OVER   0x400 /* overlay section                          */
196 #define COFF_STYP_LIB    0x800 /* library section                          */
197 
198 /*
199  * Shared libraries have the following section header in the data field for
200  * each library.
201  */
202 
203 struct COFF_slib {
204   char		sl_entsz[4];	/* Size of this entry               */
205   char		sl_pathndx[4];	/* size of the header field         */
206 };
207 
208 #define	COFF_SLIBHD	struct COFF_slib
209 #define	COFF_SLIBSZ	sizeof(COFF_SLIBHD)
210 
211 /********************** LINE NUMBERS **********************/
212 
213 /* 1 line number entry for every "breakpointable" source line in a section.
214  * Line numbers are grouped on a per function basis; first entry in a function
215  * grouping will have l_lnno = 0 and in place of physical address will be the
216  * symbol table index of the function name.
217  */
218 
219 struct COFF_lineno {
220   union {
221     char l_symndx[4];	/* function name symbol index, iff l_lnno == 0*/
222     char l_paddr[4];	/* (physical) address of line number	*/
223   } l_addr;
224   char l_lnno[2];	/* line number		*/
225 };
226 
227 #define	COFF_LINENO	struct COFF_lineno
228 #define	COFF_LINESZ	6
229 
230 /********************** SYMBOLS **********************/
231 
232 #define COFF_E_SYMNMLEN	 8	/* # characters in a short symbol name	*/
233 #define COFF_E_FILNMLEN	14	/* # characters in a file name		*/
234 #define COFF_E_DIMNUM	 4	/* # array dimensions in auxiliary entry */
235 
236 /*
237  *  All symbols and sections have the following definition
238  */
239 
240 struct COFF_syment
241 {
242   union {
243     char e_name[E_SYMNMLEN];    /* Symbol name (first 8 characters) */
244     struct {
245       char e_zeroes[4];         /* Leading zeros */
246       char e_offset[4];         /* Offset if this is a header section */
247     } e;
248   } e;
249 
250   char e_value[4];              /* Value (address) of the segment */
251   char e_scnum[2];              /* Section number */
252   char e_type[2];               /* Type of section */
253   char e_sclass[1];             /* Loader class */
254   char e_numaux[1];             /* Number of auxiliary entries which follow */
255 };
256 
257 #define COFF_N_BTMASK	(0xf)   /* Mask for important class bits */
258 #define COFF_N_TMASK	(0x30)  /* Mask for important type bits  */
259 #define COFF_N_BTSHFT	(4)     /* # bits to shift class field   */
260 #define COFF_N_TSHIFT	(2)     /* # bits to shift type field    */
261 
262 /*
263  *  Auxiliary entries because the main table is too limiting.
264  */
265 
266 union COFF_auxent {
267 
268 /*
269  *  Debugger information
270  */
271 
272   struct {
273     char x_tagndx[4];	        /* str, un, or enum tag indx */
274     union {
275       struct {
276 	char  x_lnno[2];        /* declaration line number */
277 	char  x_size[2];        /* str/union/array size */
278       } x_lnsz;
279       char x_fsize[4];	        /* size of function */
280     } x_misc;
281 
282     union {
283       struct {		        /* if ISFCN, tag, or .bb */
284 	char x_lnnoptr[4];	/* ptr to fcn line # */
285 	char x_endndx[4];	/* entry ndx past block end */
286       } x_fcn;
287 
288       struct {		        /* if ISARY, up to 4 dimen. */
289 	char x_dimen[E_DIMNUM][2];
290       } x_ary;
291     } x_fcnary;
292 
293     char x_tvndx[2];	/* tv index */
294   } x_sym;
295 
296 /*
297  *   Source file names (debugger information)
298  */
299 
300   union {
301     char x_fname[E_FILNMLEN];
302     struct {
303       char x_zeroes[4];
304       char x_offset[4];
305     } x_n;
306   } x_file;
307 
308 /*
309  *   Section information
310  */
311 
312   struct {
313     char x_scnlen[4];	/* section length */
314     char x_nreloc[2];	/* # relocation entries */
315     char x_nlinno[2];	/* # line numbers */
316   } x_scn;
317 
318 /*
319  *   Transfer vector (branch table)
320  */
321 
322   struct {
323     char x_tvfill[4];	/* tv fill value */
324     char x_tvlen[2];	/* length of .tv */
325     char x_tvran[2][2];	/* tv range */
326   } x_tv;		/* info about .tv section (in auxent of symbol .tv)) */
327 };
328 
329 #define	COFF_SYMENT	struct COFF_syment
330 #define	COFF_SYMESZ	18
331 #define	COFF_AUXENT	union COFF_auxent
332 #define	COFF_AUXESZ	18
333 
334 #define COFF_ETEXT	"etext"
335 
336 /********************** RELOCATION DIRECTIVES **********************/
337 
338 struct COFF_reloc {
339   char r_vaddr[4];        /* Virtual address of item    */
340   char r_symndx[4];       /* Symbol index in the symtab */
341   char r_type[2];         /* Relocation type            */
342 };
343 
344 #define COFF_RELOC struct COFF_reloc
345 #define COFF_RELSZ 10
346 
347 #define COFF_DEF_DATA_SECTION_ALIGNMENT  4
348 #define COFF_DEF_BSS_SECTION_ALIGNMENT   4
349 #define COFF_DEF_TEXT_SECTION_ALIGNMENT  4
350 
351 /* For new sections we haven't heard of before */
352 #define COFF_DEF_SECTION_ALIGNMENT       4
353