1 /*
2  *	HT Editor
3  *	coff_s.h
4  *
5  *	Copyright (C) 1999-2002 Stefan Weyergraf
6  *
7  *	This program is free software; you can redistribute it and/or modify
8  *	it under the terms of the GNU General Public License version 2 as
9  *	published by the Free Software Foundation.
10  *
11  *	This program is distributed in the hope that it will be useful,
12  *	but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *	GNU General Public License for more details.
15  *
16  *	You should have received a copy of the GNU General Public License
17  *	along with this program; if not, write to the Free Software
18  *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */
20 
21 #ifndef __COFF_S_H_
22 #define __COFF_S_H_
23 
24 #include "io/types.h"
25 #include "tools.h"
26 
27 typedef unsigned int RVA;
28 
29 struct COFF_HEADER {
30 	uint16 machine;
31 	uint16 section_count;
32 	uint32 timestamp;
33 	uint32 symbol_table_offset;
34 	uint32 symbol_count;
35 	uint16 optional_header_size;
36 	uint16 characteristics;
37 } PACKED;
38 
39 #define COFF_RELOCS_STRIPPED		   	0x0001  // Relocation info stripped from file.
40 #define COFF_EXECUTABLE_IMAGE		   	0x0002  // File is executable  (i.e. no unresolved externel references).
41 #define COFF_LINE_NUMS_STRIPPED			0x0004  // Line nunbers stripped from file.
42 #define COFF_LOCAL_SYMS_STRIPPED	   	0x0008  // Local symbols stripped from file.
43 #define COFF_AGGRESIVE_WS_TRIM	  		0x0010  // Agressively trim working set
44 #define COFF_LARGE_ADDRESS	  		0x0020  // Large address aware
45 #define COFF_BYTES_REVERSED_LO	  		0x0080  // Bytes of machine uint16 are reversed.
46 #define COFF_32BIT_MACHINE			0x0100  // 32 bit uint16 machine.
47 #define COFF_DEBUG_STRIPPED		   	0x0200  // Debugging info stripped from file in .DBG file
48 #define COFF_REMOVABLE_RUN_FROM_SWAP   		0x0400  // If Image is on removable media, copy and run from the swap file.
49 #define COFF_NET_RUN_FROM_SWAP	   		0x0800  // If Image is on Net, copy and run from the swap file.
50 #define COFF_SYSTEM				0x1000  // System File.
51 #define COFF_DLL				0x2000  // File is a DLL.
52 #define COFF_UP_SYSTEM_ONLY		   	0x4000  // File should only be run on a UP machine
53 #define COFF_BYTES_REVERSED_HI	  		0x8000  // Bytes of machine uint16 are reversed.
54 
55 #define COFF_MACHINE_UNKNOWN		   	0
56 #define COFF_MACHINE_I386		   	0x14c   // Intel 386
57 #define COFF_MACHINE_I486		   	0x14d   // Intel 486
58 #define COFF_MACHINE_I586		  	0x14e   // Intel 586
59 #define COFF_MACHINE_R3000BE			0x160   // MIPS big-endian
60 #define COFF_MACHINE_R3000			0x162   // MIPS little-endian
61 #define COFF_MACHINE_R4000			0x166   // MIPS little-endian
62 #define COFF_MACHINE_R10000		   	0x168   // MIPS little-endian
63 #define COFF_MACHINE_ALPHA			0x184   // Alpha_AXP
64 #define COFF_MACHINE_SH3			0x1a2   // Hitachi SH3
65 #define COFF_MACHINE_SH4		   	0x1a6   // Hitachi SH4
66 #define COFF_MACHINE_ARM			0x1c0   // ARM
67 #define COFF_MACHINE_THUMB			0x1c2   // THUMB
68 #define COFF_MACHINE_POWERPC_BE		   	0x1df   // IBM PowerPC Big-Endian (?)
69 #define COFF_MACHINE_POWERPC_LE		   	0x1f0   // IBM PowerPC Little-Endian
70 #define COFF_MACHINE_POWERPC64_BE		0x1f2   // IBM PowerPC64 Big-Endian (?)
71 #define COFF_MACHINE_IA64			0x200   // Intel IA64
72 #define COFF_MACHINE_MIPS16			0x266   // MIPS16
73 #define COFF_MACHINE_68k			0x268   // Motorola 68k
74 #define COFF_MACHINE_ALPHA_AXP_64		0x284   // Alpha AXP 64
75 #define COFF_MACHINE_MIPSf			0x366   // MIPSf
76 #define COFF_MACHINE_MIPS16f			0x466   // MIPS16f
77 #define COFF_MACHINE_AMD64			0x8664  // AMD 64
78 
79 // FIXME: not yet implemented: XCOFF64, no sample file available
80 //#define COFF_MACHINE_POWERPC64_BE	   	0x1ef   // XCOFF 64Bit Big-Endian (PowerPC only ?)
81 
82 //
83 // Optional header format.
84 //
85 
86 #define COFF_OPTSIZE_0				0x00
87 
88 #define COFF_OPTSIZE_COFF32			0x1c
89 #define COFF_OPTSIZE_XCOFF32			0x48
90 
91 #define COFF_OPTSIZE_PE32			0xe0
92 
93 
94 #define COFF_OPTMAGIC_ROMIMAGE			0x107
95 
96 #define COFF_OPTMAGIC_PE32			0x10b
97 #define COFF_OPTMAGIC_COFF32			0x10b
98 
99 #define COFF_OPTMAGIC_PE64			0x20b
100 
101 struct	COFF_OPTIONAL_HEADER32 {
102 	uint16 magic;
103 	byte major_linker_version;
104 	byte minor_linker_version;
105 	uint32 code_size;
106 	uint32 data_size;
107 	uint32 bss_size;
108 	uint32 entrypoint_address;
109 	uint32 code_base;
110 	uint32 data_base;
111 } PACKED;
112 
113 /*
114  *	same as COFF_OPTIONAL_HEADER32 but no data_base
115  */
116 struct	COFF_OPTIONAL_HEADER64 {
117 	uint16 magic;
118 	byte major_linker_version;
119 	byte minor_linker_version;
120 	uint32 code_size;
121 	uint32 data_size;
122 	uint32 bss_size;
123 	uint32 entrypoint_address;
124 	uint32 code_base;
125 } PACKED;
126 
127 /*
128  *	Section header
129  */
130 
131 #define COFF_SIZEOF_SHORT_NAME			8
132 
133 struct COFF_SECTION_HEADER {				// Section Header. See page 19 - Microsoft Specification 6.0
134 	byte name[COFF_SIZEOF_SHORT_NAME];		// Name
135 	uint32 data_vsize;						// VirtualSize							// or data_phys_address !
136 	uint32 data_address;					// VirtualAddress
137 	uint32 data_size;						// SizeOfRawData
138 	uint32 data_offset;						// PointerToRawData
139 	uint32 relocation_offset;
140 	uint32 linenumber_offset;
141 	uint16 relocation_count;
142 	uint16 linenumber_count;
143 	uint32 characteristics;
144 } PACKED;
145 
146 #define COFF_SIZEOF_SECTION_HEADER		40
147 
148 /*
149  * Section characteristics.
150  */
151 
152 //      COFF_SCN_TYPE_REG			0x00000000  // Reserved.
153 //      COFF_SCN_TYPE_DSECT			0x00000001  // Reserved.
154 //      COFF_SCN_TYPE_NOLOAD			0x00000002  // Reserved.
155 //      COFF_SCN_TYPE_GROUP			0x00000004  // Reserved.
156 #define COFF_SCN_TYPE_NO_PAD			0x00000008  // Reserved.
157 //      COFF_SCN_TYPE_COPY			0x00000010  // Reserved.
158 
159 #define COFF_SCN_CNT_CODE			0x00000020	// Section contains code.
160 #define COFF_SCN_CNT_INITIALIZED_DATA		0x00000040	// Section contains initialized data.
161 #define COFF_SCN_CNT_UNINITIALIZED_DATA		0x00000080	// Section contains uninitialized data.
162 
163 #define COFF_SCN_LNK_OTHER			0x00000100	// Reserved.
164 #define COFF_SCN_LNK_INFO			0x00000200	// Section contains comments or some other type of information.
165 //      COFF_SCN_TYPE_OVER			0x00000400	// Reserved.
166 #define COFF_SCN_LNK_REMOVE			0x00000800	// Section contents will not become part of image.
167 #define COFF_SCN_LNK_COMDAT			0x00001000	// Section contents comdat.
168 //						0x00002000	// Reserved.
169 
170 //      COFF_SCN_MEM_PROTECTED - Obsolete	0x00004000
171 #define COFF_SCN_MEM_FARDATA			0x00008000
172 //      COFF_SCN_MEM_SYSHEAP  - Obsolete	0x00010000
173 #define COFF_SCN_MEM_PURGEABLE			0x00020000
174 #define COFF_SCN_MEM_16BIT			0x00020000
175 #define COFF_SCN_MEM_LOCKED			0x00040000
176 #define COFF_SCN_MEM_PRELOAD			0x00080000
177 
178 #define COFF_SCN_ALIGN_1BYTES			0x00100000	//
179 #define COFF_SCN_ALIGN_2BYTES			0x00200000	//
180 #define COFF_SCN_ALIGN_4BYTES			0x00300000	//
181 #define COFF_SCN_ALIGN_8BYTES			0x00400000	//
182 #define COFF_SCN_ALIGN_16BYTES		   	0x00500000	// Default alignment if no others are specified.
183 #define COFF_SCN_ALIGN_32BYTES		   	0x00600000	//
184 #define COFF_SCN_ALIGN_64BYTES		   	0x00700000	//
185 // Unused					0x00800000
186 
187 #define COFF_SCN_LNK_NRELOC_OVFL		0x01000000	// Section contains extended relocations.
188 #define COFF_SCN_MEM_DISCARDABLE		0x02000000	// Section can be discarded.
189 #define COFF_SCN_MEM_NOT_CACHED			0x04000000	// Section is not cachable.
190 #define COFF_SCN_MEM_NOT_PAGED			0x08000000	// Section is not pageable.
191 #define COFF_SCN_MEM_SHARED			0x10000000	// Section is shareable.
192 #define COFF_SCN_MEM_EXECUTE			0x20000000	// Section is executable.
193 #define COFF_SCN_MEM_READ			0x40000000	// Section is readable.
194 #define COFF_SCN_MEM_WRITE			0x80000000	// Section is writeable.
195 
196 extern byte COFF_HEADER_struct[];
197 extern byte COFF_OPTIONAL_HEADER32_struct[];
198 extern byte COFF_OPTIONAL_HEADER64_struct[];
199 extern byte COFF_SECTION_HEADER_struct[];
200 
201 #endif /* !__COFF_S_H_ */
202