xref: /freebsd/contrib/elftoolchain/libpe/_libpe.h (revision 1c05a6ea6b849ff95e539c31adea887c644a6a01)
1 /*-
2  * Copyright (c) 2015 Kai Wang
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $Id: _libpe.h 3312 2016-01-10 09:23:51Z kaiwang27 $
27  */
28 
29 #ifndef	__LIBPE_H_
30 #define	__LIBPE_H_
31 
32 #include <sys/types.h>
33 #include <sys/queue.h>
34 
35 #include "libpe.h"
36 
37 #include "_elftc.h"
38 
39 typedef struct _PE_SecBuf {
40 	PE_Buffer	 sb_pb;		/* application buffer */
41 	PE_Scn		*sb_ps;		/* PE_Scn pointer */
42 	unsigned int	 sb_flags;	/* buffer flags */
43 	STAILQ_ENTRY(_PE_SecBuf) sb_next;
44 } PE_SecBuf;
45 
46 struct _PE_Scn {
47 	PE		*ps_pe;		/* PE descriptor */
48 	PE_SecHdr	 ps_sh;		/* section header */
49 	unsigned int	 ps_ndx;	/* 1-based section index */
50 	unsigned int	 ps_flags;	/* section flags */
51 	unsigned int	 ps_falign;	/* section file alignment */
52 	STAILQ_HEAD(, _PE_SecBuf) ps_b; /* buffer list */
53 	STAILQ_ENTRY(_PE_Scn) ps_next;
54 };
55 
56 struct _PE {
57 	int		 pe_fd;		/* file descriptor */
58 	PE_Cmd		 pe_cmd;	/* open mode */
59 	PE_Object	 pe_obj;	/* PE32/PE32+/COFF */
60 	size_t		 pe_fsize;	/* file size */
61 	unsigned int	 pe_flags;	/* library flags */
62 	PE_DosHdr	*pe_dh;		/* MS-DOS header */
63 	char		*pe_stub;	/* MS-DOS stub */
64 	size_t		 pe_stub_ex;	/* MS-DOS stub len (exclude hdr) */
65 	char		*pe_stub_app;	/* MS-DOS stub (app supplied) */
66 	size_t		 pe_stub_app_sz; /* MS-DOS stub len (app supplied) */
67 	PE_RichHdr	*pe_rh;		/* rich header */
68 	char		*pe_rh_start;	/* pointer to rich header */
69 	PE_CoffHdr	*pe_ch;		/* COFF header */
70 	PE_OptHdr	*pe_oh;		/* optional header */
71 	PE_DataDir	*pe_dd;		/* data directories */
72 	unsigned int	 pe_nscn;	/* num. of sections */
73 	char		*pe_symtab;	/* COFF symbol table */
74 	size_t		 pe_symbtab_sz; /* size of symbol table */
75 	unsigned int	 pe_nsym;	/* num. of symbols */
76 	unsigned int	 pe_rvamax;	/* maximum RVA */
77 	STAILQ_HEAD(, _PE_Scn) pe_scn;	/* section list */
78 };
79 
80 /* Library internal flags  */
81 #define	LIBPE_F_API_MASK		0x000FFFU
82 #define	LIBPE_F_SPECIAL_FILE		0x001000U
83 #define	LIBPE_F_BAD_DOS_HEADER		0x002000U
84 #define	LIBPE_F_BAD_PE_HEADER		0x004000U
85 #define	LIBPE_F_BAD_COFF_HEADER		0x008000U
86 #define	LIBPE_F_BAD_OPT_HEADER		0x010000U
87 #define	LIBPE_F_BAD_SEC_HEADER		0x020000U
88 #define	LIBPE_F_LOAD_DOS_STUB		0x040000U
89 #define	LIBPE_F_FD_DONE			0x080000U
90 #define	LIBPE_F_DIRTY_DOS_HEADER	0x100000U
91 #define	LIBPE_F_DIRTY_COFF_HEADER	0x200000U
92 #define	LIBPE_F_DIRTY_OPT_HEADER	0x400000U
93 #define	LIBPE_F_DIRTY_SEC_HEADER	0x800000U
94 
95 /* Internal section flags */
96 #define	LIBPE_F_LOAD_SECTION		0x1000U
97 #define	LIBPE_F_STRIP_SECTION		0x2000U
98 
99 /* Internal buffer flags */
100 #define	LIBPE_F_BUFFER_MALLOCED		0x1000U
101 
102 /* Library internal defines */
103 #define	PE_DOS_MAGIC		0x5a4dU
104 #define	PE_RICH_TEXT		"Rich"
105 #define	PE_RICH_HIDDEN		0x536e6144U /* DanS */
106 #define	PE_SIGNATURE		0x4550U	   /* PE\0\0 */
107 #define	PE_COFF_OPT_SIZE_32	224
108 #define	PE_COFF_OPT_SIZE_32P	240
109 #define	PE_SYM_ENTRY_SIZE	18
110 
111 /* Encode/Decode macros */
112 #if defined(ELFTC_NEED_BYTEORDER_EXTENSIONS)
113 static  __inline uint16_t
114 le16dec(const void *pp)
115 {
116 	unsigned char const *p = (unsigned char const *)pp;
117 
118 	return ((p[1] << 8) | p[0]);
119 }
120 
121 static __inline uint32_t
122 le32dec(const void *pp)
123 {
124 	unsigned char const *p = (unsigned char const *)pp;
125 
126 	return ((p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0]);
127 }
128 
129 static __inline uint64_t
130 le64dec(const void *pp)
131 {
132 	unsigned char const *p = (unsigned char const *)pp;
133 
134 	return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
135 }
136 
137 static __inline void
138 le16enc(void *pp, uint16_t u)
139 {
140 	unsigned char *p = (unsigned char *)pp;
141 
142 	p[0] = u & 0xff;
143 	p[1] = (u >> 8) & 0xff;
144 }
145 
146 static __inline void
147 le32enc(void *pp, uint32_t u)
148 {
149 	unsigned char *p = (unsigned char *)pp;
150 
151 	p[0] = u & 0xff;
152 	p[1] = (u >> 8) & 0xff;
153 	p[2] = (u >> 16) & 0xff;
154 	p[3] = (u >> 24) & 0xff;
155 }
156 
157 static __inline void
158 le64enc(void *pp, uint64_t u)
159 {
160 	unsigned char *p = (unsigned char *)pp;
161 
162 	le32enc(p, (uint32_t)(u & 0xffffffffU));
163 	le32enc(p + 4, (uint32_t)(u >> 32));
164 }
165 #endif	/* ELFTC_NEED_BYTEORDER_EXTENSIONS */
166 
167 #define	PE_READ16(p,v)	do {			\
168 	(v) = le16dec((p));			\
169 	(p) += 2;				\
170 } while(0)
171 
172 #define	PE_READ32(p,v)	do {			\
173 	(v) = le32dec((p));			\
174 	(p) += 4;				\
175 } while(0)
176 
177 #define	PE_WRITE16(p,v) do {			\
178 	le16enc((p), (v));			\
179 	(p) += 2;				\
180 } while(0)
181 
182 #define	PE_WRITE32(p,v) do {			\
183 	le32enc((p), (v));			\
184 	(p) += 4;				\
185 } while(0)
186 
187 
188 /* Internal function declarations */
189 off_t		libpe_align(PE *, off_t, size_t);
190 PE_SecBuf	*libpe_alloc_buffer(PE_Scn *, size_t);
191 PE_Scn		*libpe_alloc_scn(PE *);
192 int		libpe_load_all_sections(PE *);
193 int		libpe_load_section(PE *, PE_Scn *);
194 int		libpe_open_object(PE *);
195 int		libpe_pad(PE *, size_t);
196 int		libpe_parse_msdos_header(PE *, char *);
197 int		libpe_parse_coff_header(PE *, char *);
198 int		libpe_parse_rich_header(PE *);
199 int		libpe_parse_section_headers(PE *);
200 int		libpe_read_msdos_stub(PE *);
201 void		libpe_release_buffer(PE_SecBuf *);
202 void		libpe_release_object(PE *);
203 void		libpe_release_scn(PE_Scn *);
204 size_t		libpe_resync_buffers(PE_Scn *);
205 int		libpe_resync_sections(PE *, off_t);
206 int		libpe_write_buffers(PE_Scn *);
207 off_t		libpe_write_coff_header(PE *, off_t);
208 off_t		libpe_write_msdos_stub(PE *, off_t);
209 off_t		libpe_write_pe_header(PE *, off_t);
210 off_t		libpe_write_sections(PE *, off_t);
211 off_t		libpe_write_section_headers(PE *, off_t);
212 
213 #endif	/* !__LIBPE_H_ */
214