1/*-
2 * Copyright (c) 2003
3 * Bill Paul <wpaul@windriver.com>. 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
--- 17 unchanged lines hidden (view full) ---
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/compat/ndis/subr_pe.c 142387 2005-02-24 17:58:27Z wpaul $");
35
36/*
37 * This file contains routines for relocating and dynamically linking
38 * executable object code files in the Windows(r) PE (Portable Executable)
39 * format. In Windows, anything with a .EXE, .DLL or .SYS extention is
40 * considered an executable, and all such files have some structures in
41 * common. The PE format was apparently based largely on COFF but has
42 * mutated significantly over time. We are mainly concerned with .SYS files,
--- 9 unchanged lines hidden (view full) ---
52#include <sys/types.h>
53#include <sys/errno.h>
54#ifdef _KERNEL
55#include <sys/systm.h>
56extern int ndis_strncasecmp(const char *, const char *, size_t);
57#define strncasecmp(a, b, c) ndis_strncasecmp(a, b, c)
58#else
59#include <stdio.h>
60#include <stdlib.h>
61#include <unistd.h>
62#include <string.h>
63#endif
64
65#include <compat/ndis/pe_var.h>
66
67static vm_offset_t pe_functbl_match(image_patch_table *, char *);
--- 69 unchanged lines hidden (view full) ---
137
138 if (pe_is_nt_image(imgbase))
139 return (EINVAL);
140
141 dos_hdr = (image_dos_header *)(imgbase);
142 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew);
143
144 bcopy ((char *)&nt_hdr->inh_optionalhdr, (char *)hdr,
145 sizeof(image_optional_header));
146
147 return(0);
148}
149
150/*
151 * Return a copy of the file header. Contains the number of
152 * sections in this image.
153 */
--- 10 unchanged lines hidden (view full) ---
164 return(EINVAL);
165
166 if (pe_is_nt_image(imgbase))
167 return (EINVAL);
168
169 dos_hdr = (image_dos_header *)imgbase;
170 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew);
171
172 bcopy ((char *)&nt_hdr->inh_filehdr, (char *)hdr,
173 sizeof(image_file_header));
174
175 return(0);
176}
177
178/*
179 * Return the header of the first section in this image (usually
--- 12 unchanged lines hidden (view full) ---
192 if (imgbase == 0 || hdr == NULL)
193 return(EINVAL);
194
195 if (pe_is_nt_image(imgbase))
196 return (EINVAL);
197
198 dos_hdr = (image_dos_header *)imgbase;
199 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew);
200 sect_hdr = (image_section_header *)((vm_offset_t)nt_hdr +
201 sizeof(image_nt_header));
202
203 bcopy ((char *)sect_hdr, (char *)hdr, sizeof(image_section_header));
204
205 return(0);
206}
207
208/*
209 * Return the number of sections in this executable, or 0 on error.
--- 65 unchanged lines hidden (view full) ---
275
276 if (pe_get_optional_header(imgbase, &opt_hdr))
277 return(0);
278
279 sections = pe_numsections(imgbase);
280
281 dos_hdr = (image_dos_header *)imgbase;
282 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew);
283 sect_hdr = (image_section_header *)((vm_offset_t)nt_hdr +
284 sizeof(image_nt_header));
285
286 /*
287 * The test here is to see if the RVA falls somewhere
288 * inside the section, based on the section's start RVA
289 * and its length. However it seems sometimes the
290 * virtual length isn't enough to cover the entire
291 * area of the section. We fudge by taking into account
292 * the section alignment and rounding the section length
--- 41 unchanged lines hidden (view full) ---
334
335 if (pe_is_nt_image(imgbase))
336 return (EINVAL);
337
338 sections = pe_numsections(imgbase);
339
340 dos_hdr = (image_dos_header *)imgbase;
341 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew);
342 sect_hdr = (image_section_header *)((vm_offset_t)nt_hdr +
343 sizeof(image_nt_header));
344
345 for (i = 0; i < sections; i++) {
346 if (!strcmp ((char *)§_hdr->ish_name, name)) {
347 bcopy((char *)sect_hdr, (char *)hdr,
348 sizeof(image_section_header));
349 return(0);
350 } else
351 sect_hdr++;
--- 287 unchanged lines hidden ---
2 * Copyright (c) 2003
3 * Bill Paul <wpaul@windriver.com>. 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
--- 17 unchanged lines hidden (view full) ---
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/compat/ndis/subr_pe.c 142387 2005-02-24 17:58:27Z wpaul $");
35
36/*
37 * This file contains routines for relocating and dynamically linking
38 * executable object code files in the Windows(r) PE (Portable Executable)
39 * format. In Windows, anything with a .EXE, .DLL or .SYS extention is
40 * considered an executable, and all such files have some structures in
41 * common. The PE format was apparently based largely on COFF but has
42 * mutated significantly over time. We are mainly concerned with .SYS files,
--- 9 unchanged lines hidden (view full) ---
52#include <sys/types.h>
53#include <sys/errno.h>
54#ifdef _KERNEL
55#include <sys/systm.h>
56extern int ndis_strncasecmp(const char *, const char *, size_t);
57#define strncasecmp(a, b, c) ndis_strncasecmp(a, b, c)
58#else
59#include <stdio.h>
60#include <stdlib.h>
61#include <unistd.h>
62#include <string.h>
63#endif
64
65#include <compat/ndis/pe_var.h>
66
67static vm_offset_t pe_functbl_match(image_patch_table *, char *);
--- 69 unchanged lines hidden (view full) ---
137
138 if (pe_is_nt_image(imgbase))
139 return (EINVAL);
140
141 dos_hdr = (image_dos_header *)(imgbase);
142 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew);
143
144 bcopy ((char *)&nt_hdr->inh_optionalhdr, (char *)hdr,
145 sizeof(image_optional_header));
146
147 return(0);
148}
149
150/*
151 * Return a copy of the file header. Contains the number of
152 * sections in this image.
153 */
--- 10 unchanged lines hidden (view full) ---
164 return(EINVAL);
165
166 if (pe_is_nt_image(imgbase))
167 return (EINVAL);
168
169 dos_hdr = (image_dos_header *)imgbase;
170 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew);
171
172 bcopy ((char *)&nt_hdr->inh_filehdr, (char *)hdr,
173 sizeof(image_file_header));
174
175 return(0);
176}
177
178/*
179 * Return the header of the first section in this image (usually
--- 12 unchanged lines hidden (view full) ---
192 if (imgbase == 0 || hdr == NULL)
193 return(EINVAL);
194
195 if (pe_is_nt_image(imgbase))
196 return (EINVAL);
197
198 dos_hdr = (image_dos_header *)imgbase;
199 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew);
200 sect_hdr = (image_section_header *)((vm_offset_t)nt_hdr +
201 sizeof(image_nt_header));
202
203 bcopy ((char *)sect_hdr, (char *)hdr, sizeof(image_section_header));
204
205 return(0);
206}
207
208/*
209 * Return the number of sections in this executable, or 0 on error.
--- 65 unchanged lines hidden (view full) ---
275
276 if (pe_get_optional_header(imgbase, &opt_hdr))
277 return(0);
278
279 sections = pe_numsections(imgbase);
280
281 dos_hdr = (image_dos_header *)imgbase;
282 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew);
283 sect_hdr = (image_section_header *)((vm_offset_t)nt_hdr +
284 sizeof(image_nt_header));
285
286 /*
287 * The test here is to see if the RVA falls somewhere
288 * inside the section, based on the section's start RVA
289 * and its length. However it seems sometimes the
290 * virtual length isn't enough to cover the entire
291 * area of the section. We fudge by taking into account
292 * the section alignment and rounding the section length
--- 41 unchanged lines hidden (view full) ---
334
335 if (pe_is_nt_image(imgbase))
336 return (EINVAL);
337
338 sections = pe_numsections(imgbase);
339
340 dos_hdr = (image_dos_header *)imgbase;
341 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew);
342 sect_hdr = (image_section_header *)((vm_offset_t)nt_hdr +
343 sizeof(image_nt_header));
344
345 for (i = 0; i < sections; i++) {
346 if (!strcmp ((char *)§_hdr->ish_name, name)) {
347 bcopy((char *)sect_hdr, (char *)hdr,
348 sizeof(image_section_header));
349 return(0);
350 } else
351 sect_hdr++;
--- 287 unchanged lines hidden ---