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