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 151703 2005-10-26 18:46: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 <stddef.h> |
61#include <stdlib.h> 62#include <unistd.h> 63#include <string.h> 64#endif 65 66#include <compat/ndis/pe_var.h> 67 68static vm_offset_t pe_functbl_match(image_patch_table *, char *); --- 69 unchanged lines hidden (view full) --- 138 139 if (pe_is_nt_image(imgbase)) 140 return (EINVAL); 141 142 dos_hdr = (image_dos_header *)(imgbase); 143 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew); 144 145 bcopy ((char *)&nt_hdr->inh_optionalhdr, (char *)hdr, |
146 nt_hdr->inh_filehdr.ifh_optionalhdrlen); |
147 148 return(0); 149} 150 151/* 152 * Return a copy of the file header. Contains the number of 153 * sections in this image. 154 */ --- 10 unchanged lines hidden (view full) --- 165 return(EINVAL); 166 167 if (pe_is_nt_image(imgbase)) 168 return (EINVAL); 169 170 dos_hdr = (image_dos_header *)imgbase; 171 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew); 172 |
173 /* 174 * Note: the size of the nt_header is variable since it 175 * can contain optional fields, as indicated by ifh_optionalhdrlen. 176 * However it happens we're only interested in fields in the 177 * non-variant portion of the nt_header structure, so we don't 178 * bother copying the optional parts here. 179 */ 180 |
181 bcopy ((char *)&nt_hdr->inh_filehdr, (char *)hdr, 182 sizeof(image_file_header)); 183 184 return(0); 185} 186 187/* 188 * Return the header of the first section in this image (usually --- 12 unchanged lines hidden (view full) --- 201 if (imgbase == 0 || hdr == NULL) 202 return(EINVAL); 203 204 if (pe_is_nt_image(imgbase)) 205 return (EINVAL); 206 207 dos_hdr = (image_dos_header *)imgbase; 208 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew); |
209 sect_hdr = IMAGE_FIRST_SECTION(nt_hdr); |
210 211 bcopy ((char *)sect_hdr, (char *)hdr, sizeof(image_section_header)); 212 213 return(0); 214} 215 216/* 217 * Return the number of sections in this executable, or 0 on error. --- 65 unchanged lines hidden (view full) --- 283 284 if (pe_get_optional_header(imgbase, &opt_hdr)) 285 return(0); 286 287 sections = pe_numsections(imgbase); 288 289 dos_hdr = (image_dos_header *)imgbase; 290 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew); |
291 sect_hdr = IMAGE_FIRST_SECTION(nt_hdr); |
292 293 /* 294 * The test here is to see if the RVA falls somewhere 295 * inside the section, based on the section's start RVA 296 * and its length. However it seems sometimes the 297 * virtual length isn't enough to cover the entire 298 * area of the section. We fudge by taking into account 299 * the section alignment and rounding the section length --- 41 unchanged lines hidden (view full) --- 341 342 if (pe_is_nt_image(imgbase)) 343 return (EINVAL); 344 345 sections = pe_numsections(imgbase); 346 347 dos_hdr = (image_dos_header *)imgbase; 348 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew); |
349 sect_hdr = IMAGE_FIRST_SECTION(nt_hdr); |
350 351 for (i = 0; i < sections; i++) { 352 if (!strcmp ((char *)§_hdr->ish_name, name)) { 353 bcopy((char *)sect_hdr, (char *)hdr, 354 sizeof(image_section_header)); 355 return(0); 356 } else 357 sect_hdr++; --- 287 unchanged lines hidden --- |