3 * Bill Paul <wpaul@windriver.com>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
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.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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,
43 * so this module implements only enough routines to be able to parse the
44 * headers and sections of a .SYS object file and perform the necessary
45 * relocations and jump table patching to allow us to call into it
46 * (and to have it call back to us). Note that while this module
47 * can handle fixups for imported symbols, it knows nothing about
51 #include <sys/param.h>
52 #include <sys/types.h>
53 #include <sys/errno.h>
55 #include <sys/systm.h>
64 #include <compat/ndis/pe_var.h>
66 static vm_offset_t pe_functbl_match(image_patch_table *, char *);
69 * Check for an MS-DOS executable header. All Windows binaries
70 * have a small MS-DOS executable prepended to them to print out
71 * the "This program requires Windows" message. Even .SYS files
72 * have this header, in spite of the fact that you're can't actually
77 pe_get_dos_header(imgbase, hdr)
79 image_dos_header *hdr;
83 if (imgbase == 0 || hdr == NULL)
86 signature = *(uint16_t *)imgbase;
87 if (signature != IMAGE_DOS_SIGNATURE)
90 bcopy ((char *)imgbase, (char *)hdr, sizeof(image_dos_header));
96 * Verify that this image has a Windows NT PE signature.
100 pe_is_nt_image(imgbase)
104 image_dos_header *dos_hdr;
109 signature = *(uint16_t *)imgbase;
110 if (signature == IMAGE_DOS_SIGNATURE) {
111 dos_hdr = (image_dos_header *)imgbase;
112 signature = *(uint32_t *)(imgbase + dos_hdr->idh_lfanew);
113 if (signature == IMAGE_NT_SIGNATURE)
121 * Return a copy of the optional header. This contains the
122 * executable entry point and the directory listing which we
123 * need to find the relocations and imports later.
127 pe_get_optional_header(imgbase, hdr)
129 image_optional_header *hdr;
131 image_dos_header *dos_hdr;
132 image_nt_header *nt_hdr;
134 if (imgbase == 0 || hdr == NULL)
137 if (pe_is_nt_image(imgbase))
140 dos_hdr = (image_dos_header *)(imgbase);
141 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew);
143 bcopy ((char *)&nt_hdr->inh_optionalhdr, (char *)hdr,
144 nt_hdr->inh_filehdr.ifh_optionalhdrlen);
150 * Return a copy of the file header. Contains the number of
151 * sections in this image.
155 pe_get_file_header(imgbase, hdr)
157 image_file_header *hdr;
159 image_dos_header *dos_hdr;
160 image_nt_header *nt_hdr;
162 if (imgbase == 0 || hdr == NULL)
165 if (pe_is_nt_image(imgbase))
168 dos_hdr = (image_dos_header *)imgbase;
169 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew);
172 * Note: the size of the nt_header is variable since it
173 * can contain optional fields, as indicated by ifh_optionalhdrlen.
174 * However it happens we're only interested in fields in the
175 * non-variant portion of the nt_header structure, so we don't
176 * bother copying the optional parts here.
179 bcopy ((char *)&nt_hdr->inh_filehdr, (char *)hdr,
180 sizeof(image_file_header));
186 * Return the header of the first section in this image (usually
191 pe_get_section_header(imgbase, hdr)
193 image_section_header *hdr;
195 image_dos_header *dos_hdr;
196 image_nt_header *nt_hdr;
197 image_section_header *sect_hdr;
199 if (imgbase == 0 || hdr == NULL)
202 if (pe_is_nt_image(imgbase))
205 dos_hdr = (image_dos_header *)imgbase;
206 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew);
207 sect_hdr = IMAGE_FIRST_SECTION(nt_hdr);
209 bcopy ((char *)sect_hdr, (char *)hdr, sizeof(image_section_header));
215 * Return the number of sections in this executable, or 0 on error.
219 pe_numsections(imgbase)
222 image_file_header file_hdr;
224 if (pe_get_file_header(imgbase, &file_hdr))
227 return (file_hdr.ifh_numsections);
231 * Return the base address that this image was linked for.
232 * This helps us calculate relocation addresses later.
236 pe_imagebase(imgbase)
239 image_optional_header optional_hdr;
241 if (pe_get_optional_header(imgbase, &optional_hdr))
244 return (optional_hdr.ioh_imagebase);
248 * Return the offset of a given directory structure within the
249 * image. Directories reside within sections.
253 pe_directory_offset(imgbase, diridx)
257 image_optional_header opt_hdr;
260 if (pe_get_optional_header(imgbase, &opt_hdr))
263 if (diridx >= opt_hdr.ioh_rva_size_cnt)
266 dir = opt_hdr.ioh_datadir[diridx].idd_vaddr;
268 return (pe_translate_addr(imgbase, dir));
272 pe_translate_addr(imgbase, rva)
276 image_optional_header opt_hdr;
277 image_section_header *sect_hdr;
278 image_dos_header *dos_hdr;
279 image_nt_header *nt_hdr;
280 int i = 0, sections, fixedlen;
282 if (pe_get_optional_header(imgbase, &opt_hdr))
285 sections = pe_numsections(imgbase);
287 dos_hdr = (image_dos_header *)imgbase;
288 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew);
289 sect_hdr = IMAGE_FIRST_SECTION(nt_hdr);
292 * The test here is to see if the RVA falls somewhere
293 * inside the section, based on the section's start RVA
294 * and its length. However it seems sometimes the
295 * virtual length isn't enough to cover the entire
296 * area of the section. We fudge by taking into account
297 * the section alignment and rounding the section length
298 * up to a page boundary.
300 while (i++ < sections) {
301 fixedlen = sect_hdr->ish_misc.ish_vsize;
302 fixedlen += ((opt_hdr.ioh_sectalign - 1) -
303 sect_hdr->ish_misc.ish_vsize) &
304 (opt_hdr.ioh_sectalign - 1);
305 if (sect_hdr->ish_vaddr <= (uint32_t)rva &&
306 (sect_hdr->ish_vaddr + fixedlen) >
315 return ((vm_offset_t)(imgbase + rva - sect_hdr->ish_vaddr +
316 sect_hdr->ish_rawdataaddr));
320 * Get the section header for a particular section. Note that
321 * section names can be anything, but there are some standard
322 * ones (.text, .data, .rdata, .reloc).
326 pe_get_section(imgbase, hdr, name)
328 image_section_header *hdr;
331 image_dos_header *dos_hdr;
332 image_nt_header *nt_hdr;
333 image_section_header *sect_hdr;
337 if (imgbase == 0 || hdr == NULL)
340 if (pe_is_nt_image(imgbase))
343 sections = pe_numsections(imgbase);
345 dos_hdr = (image_dos_header *)imgbase;
346 nt_hdr = (image_nt_header *)(imgbase + dos_hdr->idh_lfanew);
347 sect_hdr = IMAGE_FIRST_SECTION(nt_hdr);
349 for (i = 0; i < sections; i++) {
350 if (!strcmp ((char *)§_hdr->ish_name, name)) {
351 bcopy((char *)sect_hdr, (char *)hdr,
352 sizeof(image_section_header));
362 * Apply the base relocations to this image. The relocation table
363 * resides within the .reloc section. Relocations are specified in
364 * blocks which refer to a particular page. We apply the relocations
365 * one page block at a time.
372 image_section_header sect;
373 image_base_reloc *relhdr;
382 base = pe_imagebase(imgbase);
383 pe_get_section(imgbase, §, ".text");
384 txt = pe_translate_addr(imgbase, sect.ish_vaddr);
385 delta = (uint32_t)(txt) - base - sect.ish_vaddr;
387 pe_get_section(imgbase, §, ".reloc");
389 relhdr = (image_base_reloc *)(imgbase + sect.ish_rawdataaddr);
392 count = (relhdr->ibr_blocksize -
393 (sizeof(uint32_t) * 2)) / sizeof(uint16_t);
394 for (i = 0; i < count; i++) {
395 rel = relhdr->ibr_rel[i];
396 switch (IMR_RELTYPE(rel)) {
397 case IMAGE_REL_BASED_ABSOLUTE:
399 case IMAGE_REL_BASED_HIGHLOW:
400 lloc = (uint32_t *)pe_translate_addr(imgbase,
401 relhdr->ibr_vaddr + IMR_RELOFFSET(rel));
402 *lloc = pe_translate_addr(imgbase,
405 case IMAGE_REL_BASED_HIGH:
406 sloc = (uint16_t *)pe_translate_addr(imgbase,
407 relhdr->ibr_vaddr + IMR_RELOFFSET(rel));
408 *sloc += (delta & 0xFFFF0000) >> 16;
410 case IMAGE_REL_BASED_LOW:
411 sloc = (uint16_t *)pe_translate_addr(imgbase,
412 relhdr->ibr_vaddr + IMR_RELOFFSET(rel));
413 *sloc += (delta & 0xFFFF);
415 case IMAGE_REL_BASED_DIR64:
416 qloc = (uint64_t *)pe_translate_addr(imgbase,
417 relhdr->ibr_vaddr + IMR_RELOFFSET(rel));
418 *qloc = pe_translate_addr(imgbase,
423 printf("[%d]reloc type: %d\n",i,
428 relhdr = (image_base_reloc *)((vm_offset_t)relhdr +
429 relhdr->ibr_blocksize);
430 } while (relhdr->ibr_blocksize);
436 * Return the import descriptor for a particular module. An image
437 * may be linked against several modules, typically HAL.dll, ntoskrnl.exe
438 * and NDIS.SYS. For each module, there is a list of imported function
439 * names and their addresses.
441 * Note: module names are case insensitive!
445 pe_get_import_descriptor(imgbase, desc, module)
447 image_import_descriptor *desc;
451 image_import_descriptor *imp_desc;
454 if (imgbase == 0 || module == NULL || desc == NULL)
457 offset = pe_directory_offset(imgbase, IMAGE_DIRECTORY_ENTRY_IMPORT);
461 imp_desc = (void *)offset;
463 while (imp_desc->iid_nameaddr) {
464 modname = (char *)pe_translate_addr(imgbase,
465 imp_desc->iid_nameaddr);
466 if (!strncasecmp(module, modname, strlen(module))) {
467 bcopy((char *)imp_desc, (char *)desc,
468 sizeof(image_import_descriptor));
478 pe_get_messagetable(imgbase, md)
480 message_resource_data **md;
482 image_resource_directory *rdir, *rtype;
483 image_resource_directory_entry *dent, *dent2;
484 image_resource_data_entry *rent;
491 offset = pe_directory_offset(imgbase, IMAGE_DIRECTORY_ENTRY_RESOURCE);
495 rdir = (image_resource_directory *)offset;
497 dent = (image_resource_directory_entry *)(offset +
498 sizeof(image_resource_directory));
500 for (i = 0; i < rdir->ird_id_entries; i++){
501 if (dent->irde_name != RT_MESSAGETABLE) {
506 while (dent2->irde_dataoff & RESOURCE_DIR_FLAG) {
507 rtype = (image_resource_directory *)(offset +
508 (dent2->irde_dataoff & ~RESOURCE_DIR_FLAG));
509 dent2 = (image_resource_directory_entry *)
511 sizeof(image_resource_directory));
513 rent = (image_resource_data_entry *)(offset +
514 dent2->irde_dataoff);
515 *md = (message_resource_data *)pe_translate_addr(imgbase,
524 pe_get_message(imgbase, id, str, len, flags)
531 message_resource_data *md = NULL;
532 message_resource_block *mb;
533 message_resource_entry *me;
536 pe_get_messagetable(imgbase, &md);
541 mb = (message_resource_block *)((uintptr_t)md +
542 sizeof(message_resource_data));
544 for (i = 0; i < md->mrd_numblocks; i++) {
545 if (id >= mb->mrb_lowid && id <= mb->mrb_highid) {
546 me = (message_resource_entry *)((uintptr_t)md +
548 for (i = id - mb->mrb_lowid; i > 0; i--)
549 me = (message_resource_entry *)((uintptr_t)me +
553 *flags = me->mre_flags;
563 * Find the function that matches a particular name. This doesn't
564 * need to be particularly speedy since it's only run when loading
565 * a module for the first time.
569 pe_functbl_match(functbl, name)
570 image_patch_table *functbl;
573 image_patch_table *p;
575 if (functbl == NULL || name == NULL)
580 while (p->ipt_name != NULL) {
581 if (!strcmp(p->ipt_name, name))
582 return ((vm_offset_t)p->ipt_wrap);
585 printf("no match for %s\n", name);
588 * Return the wrapper pointer for this routine.
589 * For x86, this is the same as the funcptr.
590 * For amd64, this points to a wrapper routine
591 * that does calling convention translation and
592 * then invokes the underlying routine.
594 return ((vm_offset_t)p->ipt_wrap);
598 * Patch the imported function addresses for a given module.
599 * The caller must specify the module name and provide a table
600 * of function pointers that will be patched into the jump table.
601 * Note that there are actually two copies of the jump table: one
602 * copy is left alone. In a .SYS file, the jump tables are usually
603 * merged into the INIT segment.
607 pe_patch_imports(imgbase, module, functbl)
610 image_patch_table *functbl;
612 image_import_descriptor imp_desc;
614 vm_offset_t *nptr, *fptr;
617 if (imgbase == 0 || module == NULL || functbl == NULL)
620 if (pe_get_import_descriptor(imgbase, &imp_desc, module))
623 nptr = (vm_offset_t *)pe_translate_addr(imgbase,
624 imp_desc.iid_import_name_table_addr);
625 fptr = (vm_offset_t *)pe_translate_addr(imgbase,
626 imp_desc.iid_import_address_table_addr);
628 while (nptr != NULL && pe_translate_addr(imgbase, *nptr)) {
629 fname = (char *)pe_translate_addr(imgbase, (*nptr) + 2);
630 func = pe_functbl_match(functbl, fname);