xref: /freebsd/contrib/elftoolchain/libpe/libpe_init.c (revision 0fe0fe112fa6cc220f14a1a9196b51fdc79edc72)
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 
27*839529caSEd Maste #include <sys/stat.h>
28*839529caSEd Maste #include <assert.h>
29*839529caSEd Maste #include <errno.h>
30*839529caSEd Maste #include <stdlib.h>
31*839529caSEd Maste #include <unistd.h>
32*839529caSEd Maste 
33*839529caSEd Maste #include "_libpe.h"
34*839529caSEd Maste 
35*839529caSEd Maste ELFTC_VCSID("$Id: libpe_init.c 3312 2016-01-10 09:23:51Z kaiwang27 $");
36*839529caSEd Maste 
37*839529caSEd Maste int
libpe_open_object(PE * pe)38*839529caSEd Maste libpe_open_object(PE *pe)
39*839529caSEd Maste {
40*839529caSEd Maste 	struct stat sb;
41*839529caSEd Maste 	mode_t mode;
42*839529caSEd Maste 	char magic[sizeof(PE_DosHdr)];
43*839529caSEd Maste 
44*839529caSEd Maste 	if (fstat(pe->pe_fd, &sb) < 0)
45*839529caSEd Maste 		return (-1);
46*839529caSEd Maste 
47*839529caSEd Maste 	mode = sb.st_mode;
48*839529caSEd Maste 	pe->pe_fsize = (size_t) sb.st_size;
49*839529caSEd Maste 
50*839529caSEd Maste 	/* Reject unsupported file types. */
51*839529caSEd Maste 	if (!S_ISREG(mode) && !S_ISCHR(mode) && !S_ISFIFO(mode) &&
52*839529caSEd Maste 	    !S_ISSOCK(mode)) {
53*839529caSEd Maste 		errno = EINVAL;
54*839529caSEd Maste 		return (-1);
55*839529caSEd Maste 	}
56*839529caSEd Maste 
57*839529caSEd Maste 	/* Read/Write mode is not supported for non-regular file. */
58*839529caSEd Maste 	if (pe->pe_cmd == PE_C_RDWR && !S_ISREG(mode)) {
59*839529caSEd Maste 		errno = EINVAL;
60*839529caSEd Maste 		return (-1);
61*839529caSEd Maste 	}
62*839529caSEd Maste 
63*839529caSEd Maste 	/* The minimal file should at least contain a COFF header. */
64*839529caSEd Maste 	if (S_ISREG(mode) && pe->pe_fsize < sizeof(PE_CoffHdr)) {
65*839529caSEd Maste 		errno = ENOENT;
66*839529caSEd Maste 		return (-1);
67*839529caSEd Maste 	}
68*839529caSEd Maste 
69*839529caSEd Maste 	/*
70*839529caSEd Maste 	 * Search for MS-DOS header or COFF header.
71*839529caSEd Maste 	 */
72*839529caSEd Maste 
73*839529caSEd Maste 	if (read(pe->pe_fd, magic, 2) != 2) {
74*839529caSEd Maste 		errno = EIO;
75*839529caSEd Maste 		return (-1);
76*839529caSEd Maste 	}
77*839529caSEd Maste 
78*839529caSEd Maste 	if (magic[0] == 'M' && magic[1] == 'Z') {
79*839529caSEd Maste 		pe->pe_obj = PE_O_PE32;
80*839529caSEd Maste 		if (read(pe->pe_fd, &magic[2], sizeof(PE_DosHdr) - 2) !=
81*839529caSEd Maste 		    (ssize_t) sizeof(PE_DosHdr) - 2) {
82*839529caSEd Maste 			errno = EIO;
83*839529caSEd Maste 			return (-1);
84*839529caSEd Maste 		}
85*839529caSEd Maste 		return (libpe_parse_msdos_header(pe, magic));
86*839529caSEd Maste 
87*839529caSEd Maste 	} else if (magic[0] == 'P' && magic[1] == 'E') {
88*839529caSEd Maste 		if (read(pe->pe_fd, magic, 2) != 2) {
89*839529caSEd Maste 			errno = EIO;
90*839529caSEd Maste 			return (-1);
91*839529caSEd Maste 		}
92*839529caSEd Maste 		if (magic[0] == '\0' && magic[1] == '\0') {
93*839529caSEd Maste 			pe->pe_obj = PE_O_PE32;
94*839529caSEd Maste 			if (read(pe->pe_fd, magic, sizeof(PE_CoffHdr)) !=
95*839529caSEd Maste 			    (ssize_t) sizeof(PE_CoffHdr)) {
96*839529caSEd Maste 				errno = EIO;
97*839529caSEd Maste 				return (-1);
98*839529caSEd Maste 			}
99*839529caSEd Maste 			return (libpe_parse_coff_header(pe, magic));
100*839529caSEd Maste 		}
101*839529caSEd Maste 		errno = ENOENT;
102*839529caSEd Maste 		return (-1);
103*839529caSEd Maste 
104*839529caSEd Maste 	} else {
105*839529caSEd Maste 		pe->pe_obj = PE_O_COFF;
106*839529caSEd Maste 		if (read(pe->pe_fd, &magic[2], sizeof(PE_CoffHdr) - 2) !=
107*839529caSEd Maste 		    (ssize_t) sizeof(PE_CoffHdr) - 2) {
108*839529caSEd Maste 			errno = EIO;
109*839529caSEd Maste 			return (-1);
110*839529caSEd Maste 		}
111*839529caSEd Maste 		return (libpe_parse_coff_header(pe, magic));
112*839529caSEd Maste 	}
113*839529caSEd Maste }
114*839529caSEd Maste 
115*839529caSEd Maste void
libpe_release_object(PE * pe)116*839529caSEd Maste libpe_release_object(PE *pe)
117*839529caSEd Maste {
118*839529caSEd Maste 	PE_Scn *ps, *_ps;
119*839529caSEd Maste 
120*839529caSEd Maste 	if (pe->pe_dh)
121*839529caSEd Maste 		free(pe->pe_dh);
122*839529caSEd Maste 
123*839529caSEd Maste 	if (pe->pe_rh) {
124*839529caSEd Maste 		free(pe->pe_rh->rh_compid);
125*839529caSEd Maste 		free(pe->pe_rh->rh_cnt);
126*839529caSEd Maste 		free(pe->pe_rh);
127*839529caSEd Maste 	}
128*839529caSEd Maste 
129*839529caSEd Maste 	if (pe->pe_ch)
130*839529caSEd Maste 		free(pe->pe_ch);
131*839529caSEd Maste 
132*839529caSEd Maste 	if (pe->pe_oh)
133*839529caSEd Maste 		free(pe->pe_oh);
134*839529caSEd Maste 
135*839529caSEd Maste 	if (pe->pe_dd)
136*839529caSEd Maste 		free(pe->pe_dd);
137*839529caSEd Maste 
138*839529caSEd Maste 	if (pe->pe_stub)
139*839529caSEd Maste 		free(pe->pe_stub);
140*839529caSEd Maste 
141*839529caSEd Maste 	STAILQ_FOREACH_SAFE(ps, &pe->pe_scn, ps_next, _ps)
142*839529caSEd Maste 		libpe_release_scn(ps);
143*839529caSEd Maste 
144*839529caSEd Maste 	free(pe);
145*839529caSEd Maste }
146