wined3d: Store a fragment_caps structure in struct wined3d_d3d_info.
[wine.git] / server / mapping.c
bloba795dc4b38bd03ca269b7e54b610ebb4ab96a703
1 /*
2 * Server-side file mapping management
4 * Copyright (C) 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <assert.h>
24 #include <fcntl.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/mman.h>
31 #include <unistd.h>
33 #include "ntstatus.h"
34 #define WIN32_NO_STATUS
35 #include "windef.h"
36 #include "winternl.h"
37 #include "ddk/wdm.h"
39 #include "file.h"
40 #include "handle.h"
41 #include "thread.h"
42 #include "process.h"
43 #include "request.h"
44 #include "security.h"
46 /* list of memory ranges, used to store committed info */
47 struct ranges
49 struct object obj; /* object header */
50 unsigned int count; /* number of used ranges */
51 unsigned int max; /* number of allocated ranges */
52 struct range
54 file_pos_t start;
55 file_pos_t end;
56 } *ranges;
59 static void ranges_dump( struct object *obj, int verbose );
60 static void ranges_destroy( struct object *obj );
62 static const struct object_ops ranges_ops =
64 sizeof(struct ranges), /* size */
65 &no_type, /* type */
66 ranges_dump, /* dump */
67 no_add_queue, /* add_queue */
68 NULL, /* remove_queue */
69 NULL, /* signaled */
70 NULL, /* satisfied */
71 no_signal, /* signal */
72 no_get_fd, /* get_fd */
73 default_map_access, /* map_access */
74 default_get_sd, /* get_sd */
75 default_set_sd, /* set_sd */
76 no_get_full_name, /* get_full_name */
77 no_lookup_name, /* lookup_name */
78 no_link_name, /* link_name */
79 NULL, /* unlink_name */
80 no_open_file, /* open_file */
81 no_kernel_obj_list, /* get_kernel_obj_list */
82 no_close_handle, /* close_handle */
83 ranges_destroy /* destroy */
86 /* file backing the shared sections of a PE image mapping */
87 struct shared_map
89 struct object obj; /* object header */
90 struct fd *fd; /* file descriptor of the mapped PE file */
91 struct file *file; /* temp file holding the shared data */
92 struct list entry; /* entry in global shared maps list */
95 static void shared_map_dump( struct object *obj, int verbose );
96 static void shared_map_destroy( struct object *obj );
98 static const struct object_ops shared_map_ops =
100 sizeof(struct shared_map), /* size */
101 &no_type, /* type */
102 shared_map_dump, /* dump */
103 no_add_queue, /* add_queue */
104 NULL, /* remove_queue */
105 NULL, /* signaled */
106 NULL, /* satisfied */
107 no_signal, /* signal */
108 no_get_fd, /* get_fd */
109 default_map_access, /* map_access */
110 default_get_sd, /* get_sd */
111 default_set_sd, /* set_sd */
112 no_get_full_name, /* get_full_name */
113 no_lookup_name, /* lookup_name */
114 no_link_name, /* link_name */
115 NULL, /* unlink_name */
116 no_open_file, /* open_file */
117 no_kernel_obj_list, /* get_kernel_obj_list */
118 no_close_handle, /* close_handle */
119 shared_map_destroy /* destroy */
122 static struct list shared_map_list = LIST_INIT( shared_map_list );
124 /* memory view mapped in client address space */
125 struct memory_view
127 struct list entry; /* entry in per-process view list */
128 struct fd *fd; /* fd for mapped file */
129 struct ranges *committed; /* list of committed ranges in this mapping */
130 struct shared_map *shared; /* temp file for shared PE mapping */
131 pe_image_info_t image; /* image info (for PE image mapping) */
132 unsigned int flags; /* SEC_* flags */
133 client_ptr_t base; /* view base address (in process addr space) */
134 mem_size_t size; /* view size */
135 file_pos_t start; /* start offset in mapping */
136 data_size_t namelen;
137 WCHAR name[1]; /* filename for .so dll image views */
141 static const WCHAR mapping_name[] = {'S','e','c','t','i','o','n'};
143 struct type_descr mapping_type =
145 { mapping_name, sizeof(mapping_name) }, /* name */
146 SECTION_ALL_ACCESS | SYNCHRONIZE, /* valid_access */
147 { /* mapping */
148 STANDARD_RIGHTS_READ | SECTION_QUERY | SECTION_MAP_READ,
149 STANDARD_RIGHTS_WRITE | SECTION_MAP_WRITE,
150 STANDARD_RIGHTS_EXECUTE | SECTION_MAP_EXECUTE,
151 SECTION_ALL_ACCESS
155 struct mapping
157 struct object obj; /* object header */
158 mem_size_t size; /* mapping size */
159 unsigned int flags; /* SEC_* flags */
160 struct fd *fd; /* fd for mapped file */
161 pe_image_info_t image; /* image info (for PE image mapping) */
162 struct ranges *committed; /* list of committed ranges in this mapping */
163 struct shared_map *shared; /* temp file for shared PE mapping */
166 static void mapping_dump( struct object *obj, int verbose );
167 static struct fd *mapping_get_fd( struct object *obj );
168 static void mapping_destroy( struct object *obj );
169 static enum server_fd_type mapping_get_fd_type( struct fd *fd );
171 static const struct object_ops mapping_ops =
173 sizeof(struct mapping), /* size */
174 &mapping_type, /* type */
175 mapping_dump, /* dump */
176 no_add_queue, /* add_queue */
177 NULL, /* remove_queue */
178 NULL, /* signaled */
179 NULL, /* satisfied */
180 no_signal, /* signal */
181 mapping_get_fd, /* get_fd */
182 default_map_access, /* map_access */
183 default_get_sd, /* get_sd */
184 default_set_sd, /* set_sd */
185 default_get_full_name, /* get_full_name */
186 no_lookup_name, /* lookup_name */
187 directory_link_name, /* link_name */
188 default_unlink_name, /* unlink_name */
189 no_open_file, /* open_file */
190 no_kernel_obj_list, /* get_kernel_obj_list */
191 no_close_handle, /* close_handle */
192 mapping_destroy /* destroy */
195 static const struct fd_ops mapping_fd_ops =
197 default_fd_get_poll_events, /* get_poll_events */
198 default_poll_event, /* poll_event */
199 mapping_get_fd_type, /* get_fd_type */
200 no_fd_read, /* read */
201 no_fd_write, /* write */
202 no_fd_flush, /* flush */
203 no_fd_get_file_info, /* get_file_info */
204 no_fd_get_volume_info, /* get_volume_info */
205 no_fd_ioctl, /* ioctl */
206 default_fd_cancel_async, /* cancel_async */
207 no_fd_queue_async, /* queue_async */
208 default_fd_reselect_async /* reselect_async */
211 /* free address ranges for PE image mappings */
212 struct addr_range
214 unsigned int count;
215 unsigned int size;
216 struct
218 client_ptr_t base;
219 mem_size_t size;
220 } *free;
223 static size_t page_mask;
224 static const mem_size_t granularity_mask = 0xffff;
225 static struct addr_range ranges32;
226 static struct addr_range ranges64;
228 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
230 void init_memory(void)
232 page_mask = sysconf( _SC_PAGESIZE ) - 1;
233 free_map_addr( 0x60000000, 0x1c000000 );
234 free_map_addr( 0x600000000000, 0x100000000000 );
237 static void ranges_dump( struct object *obj, int verbose )
239 struct ranges *ranges = (struct ranges *)obj;
240 fprintf( stderr, "Memory ranges count=%u\n", ranges->count );
243 static void ranges_destroy( struct object *obj )
245 struct ranges *ranges = (struct ranges *)obj;
246 free( ranges->ranges );
249 static void shared_map_dump( struct object *obj, int verbose )
251 struct shared_map *shared = (struct shared_map *)obj;
252 fprintf( stderr, "Shared mapping fd=%p file=%p\n", shared->fd, shared->file );
255 static void shared_map_destroy( struct object *obj )
257 struct shared_map *shared = (struct shared_map *)obj;
259 release_object( shared->fd );
260 release_object( shared->file );
261 list_remove( &shared->entry );
264 /* extend a file beyond the current end of file */
265 int grow_file( int unix_fd, file_pos_t new_size )
267 static const char zero;
268 off_t size = new_size;
270 if (sizeof(new_size) > sizeof(size) && size != new_size)
272 set_error( STATUS_INVALID_PARAMETER );
273 return 0;
275 /* extend the file one byte beyond the requested size and then truncate it */
276 /* this should work around ftruncate implementations that can't extend files */
277 if (pwrite( unix_fd, &zero, 1, size ) != -1)
279 ftruncate( unix_fd, size );
280 return 1;
282 file_set_error();
283 return 0;
286 /* simplified version of mkstemps() */
287 static int make_temp_file( char name[16] )
289 static unsigned int value;
290 int i, fd = -1;
292 value += (current_time >> 16) + current_time;
293 for (i = 0; i < 0x8000 && fd < 0; i++, value += 7777)
295 sprintf( name, "tmpmap-%08x", value );
296 fd = open( name, O_RDWR | O_CREAT | O_EXCL, 0600 );
298 return fd;
301 /* check if the current directory allows exec mappings */
302 static int check_current_dir_for_exec(void)
304 int fd;
305 char tmpfn[16];
306 void *ret = MAP_FAILED;
308 fd = make_temp_file( tmpfn );
309 if (fd == -1) return 0;
310 if (grow_file( fd, 1 ))
312 ret = mmap( NULL, get_page_size(), PROT_READ | PROT_EXEC, MAP_PRIVATE, fd, 0 );
313 if (ret != MAP_FAILED) munmap( ret, get_page_size() );
315 close( fd );
316 unlink( tmpfn );
317 return (ret != MAP_FAILED);
320 /* create a temp file for anonymous mappings */
321 static int create_temp_file( file_pos_t size )
323 static int temp_dir_fd = -1;
324 char tmpfn[16];
325 int fd;
327 if (temp_dir_fd == -1)
329 temp_dir_fd = server_dir_fd;
330 if (!check_current_dir_for_exec())
332 /* the server dir is noexec, try the config dir instead */
333 fchdir( config_dir_fd );
334 if (check_current_dir_for_exec())
335 temp_dir_fd = config_dir_fd;
336 else /* neither works, fall back to server dir */
337 fchdir( server_dir_fd );
340 else if (temp_dir_fd != server_dir_fd) fchdir( temp_dir_fd );
342 fd = make_temp_file( tmpfn );
343 if (fd != -1)
345 if (!grow_file( fd, size ))
347 close( fd );
348 fd = -1;
350 unlink( tmpfn );
352 else file_set_error();
354 if (temp_dir_fd != server_dir_fd) fchdir( server_dir_fd );
355 return fd;
358 /* find a memory view from its base address */
359 struct memory_view *find_mapped_view( struct process *process, client_ptr_t base )
361 struct memory_view *view;
363 LIST_FOR_EACH_ENTRY( view, &process->views, struct memory_view, entry )
364 if (view->base == base) return view;
366 set_error( STATUS_NOT_MAPPED_VIEW );
367 return NULL;
370 /* find a memory view from any address inside it */
371 static struct memory_view *find_mapped_addr( struct process *process, client_ptr_t addr )
373 struct memory_view *view;
375 LIST_FOR_EACH_ENTRY( view, &process->views, struct memory_view, entry )
376 if (addr >= view->base && addr < view->base + view->size) return view;
378 set_error( STATUS_NOT_MAPPED_VIEW );
379 return NULL;
382 /* check if an address range is valid for creating a view */
383 static int is_valid_view_addr( struct process *process, client_ptr_t addr, mem_size_t size )
385 struct memory_view *view;
387 if (!size) return 0;
388 if (addr & page_mask) return 0;
389 if (addr + size < addr) return 0; /* overflow */
391 /* check for overlapping view */
392 LIST_FOR_EACH_ENTRY( view, &process->views, struct memory_view, entry )
394 if (view->base + view->size <= addr) continue;
395 if (view->base >= addr + size) continue;
396 return 0;
398 return 1;
401 /* get the main exe memory view */
402 struct memory_view *get_exe_view( struct process *process )
404 return LIST_ENTRY( list_head( &process->views ), struct memory_view, entry );
407 static void set_process_machine( struct process *process, struct memory_view *view )
409 if (view->image.image_flags & IMAGE_FLAGS_ComPlusNativeReady)
410 process->machine = native_machine;
411 else
412 process->machine = view->image.machine;
415 static int generate_dll_event( struct thread *thread, int code, struct memory_view *view )
417 if (!(view->flags & SEC_IMAGE)) return 0;
418 generate_debug_event( thread, code, view );
419 return 1;
422 /* add a view to the process list */
423 static void add_process_view( struct thread *thread, struct memory_view *view )
425 struct process *process = thread->process;
426 struct unicode_str name;
428 if (view->flags & SEC_IMAGE)
430 if (is_process_init_done( process ))
432 generate_dll_event( thread, DbgLoadDllStateChange, view );
434 else if (!(view->image.image_charact & IMAGE_FILE_DLL))
436 /* main exe */
437 set_process_machine( process, view );
438 list_add_head( &process->views, &view->entry );
440 free( process->image );
441 process->image = NULL;
442 if (get_view_nt_name( view, &name ) && (process->image = memdup( name.str, name.len )))
443 process->imagelen = name.len;
444 process->image_info = view->image;
445 return;
448 list_add_tail( &process->views, &view->entry );
451 static void free_memory_view( struct memory_view *view )
453 if (view->fd) release_object( view->fd );
454 if (view->committed) release_object( view->committed );
455 if (view->shared) release_object( view->shared );
456 list_remove( &view->entry );
457 free( view );
460 /* free all mapped views at process exit */
461 void free_mapped_views( struct process *process )
463 struct list *ptr;
465 while ((ptr = list_head( &process->views )))
466 free_memory_view( LIST_ENTRY( ptr, struct memory_view, entry ));
469 /* find the shared PE mapping for a given mapping */
470 static struct shared_map *get_shared_file( struct fd *fd )
472 struct shared_map *ptr;
474 LIST_FOR_EACH_ENTRY( ptr, &shared_map_list, struct shared_map, entry )
475 if (is_same_file_fd( ptr->fd, fd ))
476 return (struct shared_map *)grab_object( ptr );
477 return NULL;
480 /* return the size of the memory mapping and file range of a given section */
481 static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
482 off_t *file_start, size_t *file_size )
484 static const unsigned int sector_align = 0x1ff;
486 if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
487 else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
489 *file_start = sec->PointerToRawData & ~sector_align;
490 *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
491 if (*file_size > *map_size) *file_size = *map_size;
494 /* add a range to the committed list */
495 static void add_committed_range( struct memory_view *view, file_pos_t start, file_pos_t end )
497 unsigned int i, j;
498 struct ranges *committed = view->committed;
499 struct range *ranges;
501 if ((start & page_mask) || (end & page_mask) ||
502 start >= view->size || end >= view->size ||
503 start >= end)
505 set_error( STATUS_INVALID_PARAMETER );
506 return;
509 if (!committed) return; /* everything committed already */
511 start += view->start;
512 end += view->start;
514 for (i = 0, ranges = committed->ranges; i < committed->count; i++)
516 if (ranges[i].start > end) break;
517 if (ranges[i].end < start) continue;
518 if (ranges[i].start > start) ranges[i].start = start; /* extend downwards */
519 if (ranges[i].end < end) /* extend upwards and maybe merge with next */
521 for (j = i + 1; j < committed->count; j++)
523 if (ranges[j].start > end) break;
524 if (ranges[j].end > end) end = ranges[j].end;
526 if (j > i + 1)
528 memmove( &ranges[i + 1], &ranges[j], (committed->count - j) * sizeof(*ranges) );
529 committed->count -= j - (i + 1);
531 ranges[i].end = end;
533 return;
536 /* now add a new range */
538 if (committed->count == committed->max)
540 unsigned int new_size = committed->max * 2;
541 struct range *new_ptr = realloc( committed->ranges, new_size * sizeof(*new_ptr) );
542 if (!new_ptr) return;
543 committed->max = new_size;
544 ranges = committed->ranges = new_ptr;
546 memmove( &ranges[i + 1], &ranges[i], (committed->count - i) * sizeof(*ranges) );
547 ranges[i].start = start;
548 ranges[i].end = end;
549 committed->count++;
552 /* find the range containing start and return whether it's committed */
553 static int find_committed_range( struct memory_view *view, file_pos_t start, mem_size_t *size )
555 unsigned int i;
556 struct ranges *committed = view->committed;
557 struct range *ranges;
559 if ((start & page_mask) || start >= view->size)
561 set_error( STATUS_INVALID_PARAMETER );
562 return 0;
564 if (!committed) /* everything is committed */
566 *size = view->size - start;
567 return 1;
569 for (i = 0, ranges = committed->ranges; i < committed->count; i++)
571 if (ranges[i].start > view->start + start)
573 *size = min( ranges[i].start, view->start + view->size ) - (view->start + start);
574 return 0;
576 if (ranges[i].end > view->start + start)
578 *size = min( ranges[i].end, view->start + view->size ) - (view->start + start);
579 return 1;
582 *size = view->size - start;
583 return 0;
586 /* allocate and fill the temp file for a shared PE image mapping */
587 static int build_shared_mapping( struct mapping *mapping, int fd,
588 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
590 struct shared_map *shared;
591 struct file *file;
592 unsigned int i;
593 mem_size_t total_size;
594 size_t file_size, map_size, max_size;
595 off_t shared_pos, read_pos, write_pos;
596 char *buffer = NULL;
597 int shared_fd;
598 long toread;
600 /* compute the total size of the shared mapping */
602 total_size = max_size = 0;
603 for (i = 0; i < nb_sec; i++)
605 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
606 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
608 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
609 if (file_size > max_size) max_size = file_size;
610 total_size += map_size;
613 if (!total_size) return 1; /* nothing to do */
615 if ((mapping->shared = get_shared_file( mapping->fd ))) return 1;
617 /* create a temp file for the mapping */
619 if ((shared_fd = create_temp_file( total_size )) == -1) return 0;
620 if (!(file = create_file_for_fd( shared_fd, FILE_GENERIC_READ|FILE_GENERIC_WRITE, 0 ))) return 0;
622 if (!(buffer = malloc( max_size ))) goto error;
624 /* copy the shared sections data into the temp file */
626 shared_pos = 0;
627 for (i = 0; i < nb_sec; i++)
629 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
630 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
631 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
632 write_pos = shared_pos;
633 shared_pos += map_size;
634 if (!sec[i].PointerToRawData || !file_size) continue;
635 toread = file_size;
636 while (toread)
638 long res = pread( fd, buffer + file_size - toread, toread, read_pos );
639 if (!res && toread < 0x200) /* partial sector at EOF is not an error */
641 file_size -= toread;
642 break;
644 if (res <= 0) goto error;
645 toread -= res;
646 read_pos += res;
648 if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
651 if (!(shared = alloc_object( &shared_map_ops ))) goto error;
652 shared->fd = (struct fd *)grab_object( mapping->fd );
653 shared->file = file;
654 list_add_head( &shared_map_list, &shared->entry );
655 mapping->shared = shared;
656 free( buffer );
657 return 1;
659 error:
660 release_object( file );
661 free( buffer );
662 return 0;
665 /* load the CLR header from its section */
666 static int load_clr_header( IMAGE_COR20_HEADER *hdr, size_t va, size_t size, int unix_fd,
667 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
669 ssize_t ret;
670 size_t map_size, file_size;
671 off_t file_start;
672 unsigned int i;
674 if (!va || !size) return 0;
676 for (i = 0; i < nb_sec; i++)
678 if (va < sec[i].VirtualAddress) continue;
679 if (sec[i].Misc.VirtualSize && va - sec[i].VirtualAddress >= sec[i].Misc.VirtualSize) continue;
680 get_section_sizes( &sec[i], &map_size, &file_start, &file_size );
681 if (size >= map_size) continue;
682 if (va - sec[i].VirtualAddress >= map_size - size) continue;
683 file_size = min( file_size, map_size );
684 size = min( size, sizeof(*hdr) );
685 ret = pread( unix_fd, hdr, min( size, file_size ), file_start + va - sec[i].VirtualAddress );
686 if (ret <= 0) break;
687 if (ret < sizeof(*hdr)) memset( (char *)hdr + ret, 0, sizeof(*hdr) - ret );
688 return (hdr->MajorRuntimeVersion > COR_VERSION_MAJOR_V2 ||
689 (hdr->MajorRuntimeVersion == COR_VERSION_MAJOR_V2 &&
690 hdr->MinorRuntimeVersion >= COR_VERSION_MINOR));
692 return 0;
695 /* retrieve the mapping parameters for an executable (PE) image */
696 static unsigned int get_image_params( struct mapping *mapping, file_pos_t file_size, int unix_fd )
698 static const char builtin_signature[] = "Wine builtin DLL";
699 static const char fakedll_signature[] = "Wine placeholder DLL";
701 IMAGE_COR20_HEADER clr;
702 IMAGE_SECTION_HEADER sec[96];
703 struct
705 IMAGE_DOS_HEADER dos;
706 char buffer[32];
707 } mz;
708 struct
710 DWORD Signature;
711 IMAGE_FILE_HEADER FileHeader;
712 union
714 IMAGE_OPTIONAL_HEADER32 hdr32;
715 IMAGE_OPTIONAL_HEADER64 hdr64;
716 } opt;
717 } nt;
718 off_t pos;
719 int size, has_relocs;
720 size_t mz_size, clr_va = 0, clr_size = 0;
721 unsigned int i;
723 /* load the headers */
725 if (!file_size) return STATUS_INVALID_FILE_FOR_SECTION;
726 size = pread( unix_fd, &mz, sizeof(mz), 0 );
727 if (size < sizeof(mz.dos)) return STATUS_INVALID_IMAGE_NOT_MZ;
728 if (mz.dos.e_magic != IMAGE_DOS_SIGNATURE) return STATUS_INVALID_IMAGE_NOT_MZ;
729 mz_size = size;
730 pos = mz.dos.e_lfanew;
732 size = pread( unix_fd, &nt, sizeof(nt), pos );
733 if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) return STATUS_INVALID_IMAGE_PROTECT;
734 if (size < sizeof(nt)) memset( (char *)&nt + size, 0, sizeof(nt) - size );
735 if (nt.Signature != IMAGE_NT_SIGNATURE)
737 IMAGE_OS2_HEADER *os2 = (IMAGE_OS2_HEADER *)&nt;
738 if (os2->ne_magic != IMAGE_OS2_SIGNATURE) return STATUS_INVALID_IMAGE_PROTECT;
739 if (os2->ne_exetyp == 2) return STATUS_INVALID_IMAGE_WIN_16;
740 if (os2->ne_exetyp == 5) return STATUS_INVALID_IMAGE_PROTECT;
741 return STATUS_INVALID_IMAGE_NE_FORMAT;
744 switch (nt.opt.hdr32.Magic)
746 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
747 if (!is_machine_32bit( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
748 if (!is_machine_supported( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
750 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_I386) /* non-x86 platforms are more strict */
752 if (nt.opt.hdr32.SectionAlignment & page_mask)
753 return STATUS_INVALID_IMAGE_FORMAT;
754 if (!(nt.opt.hdr32.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_NX_COMPAT))
755 return STATUS_INVALID_IMAGE_FORMAT;
756 if (!(nt.opt.hdr32.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE))
757 return STATUS_INVALID_IMAGE_FORMAT;
759 if (nt.opt.hdr32.NumberOfRvaAndSizes > IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR)
761 clr_va = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
762 clr_size = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
764 mapping->image.base = nt.opt.hdr32.ImageBase;
765 mapping->image.entry_point = nt.opt.hdr32.AddressOfEntryPoint;
766 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
767 mapping->image.stack_size = nt.opt.hdr32.SizeOfStackReserve;
768 mapping->image.stack_commit = nt.opt.hdr32.SizeOfStackCommit;
769 mapping->image.subsystem = nt.opt.hdr32.Subsystem;
770 mapping->image.subsystem_minor = nt.opt.hdr32.MinorSubsystemVersion;
771 mapping->image.subsystem_major = nt.opt.hdr32.MajorSubsystemVersion;
772 mapping->image.osversion_minor = nt.opt.hdr32.MinorOperatingSystemVersion;
773 mapping->image.osversion_major = nt.opt.hdr32.MajorOperatingSystemVersion;
774 mapping->image.dll_charact = nt.opt.hdr32.DllCharacteristics;
775 mapping->image.contains_code = (nt.opt.hdr32.SizeOfCode ||
776 nt.opt.hdr32.AddressOfEntryPoint ||
777 nt.opt.hdr32.SectionAlignment & page_mask);
778 mapping->image.header_size = nt.opt.hdr32.SizeOfHeaders;
779 mapping->image.checksum = nt.opt.hdr32.CheckSum;
780 mapping->image.image_flags = 0;
782 has_relocs = (nt.opt.hdr32.NumberOfRvaAndSizes > IMAGE_DIRECTORY_ENTRY_BASERELOC &&
783 nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress &&
784 nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size &&
785 !(nt.FileHeader.Characteristics & IMAGE_FILE_RELOCS_STRIPPED));
786 if (nt.opt.hdr32.SectionAlignment & page_mask)
787 mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
788 else if ((nt.opt.hdr32.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
789 (has_relocs || mapping->image.contains_code) && !(clr_va && clr_size))
790 mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
791 break;
793 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
794 if (!is_machine_64bit( native_machine )) return STATUS_INVALID_IMAGE_WIN_64;
795 if (!is_machine_64bit( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
796 if (!is_machine_supported( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
798 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64) /* non-x86 platforms are more strict */
800 if (nt.opt.hdr64.SectionAlignment & page_mask)
801 return STATUS_INVALID_IMAGE_FORMAT;
802 if (!(nt.opt.hdr64.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_NX_COMPAT))
803 return STATUS_INVALID_IMAGE_FORMAT;
804 if (!(nt.opt.hdr64.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE))
805 return STATUS_INVALID_IMAGE_FORMAT;
807 if (nt.opt.hdr64.NumberOfRvaAndSizes > IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR)
809 clr_va = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
810 clr_size = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
812 mapping->image.base = nt.opt.hdr64.ImageBase;
813 mapping->image.entry_point = nt.opt.hdr64.AddressOfEntryPoint;
814 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
815 mapping->image.stack_size = nt.opt.hdr64.SizeOfStackReserve;
816 mapping->image.stack_commit = nt.opt.hdr64.SizeOfStackCommit;
817 mapping->image.subsystem = nt.opt.hdr64.Subsystem;
818 mapping->image.subsystem_minor = nt.opt.hdr64.MinorSubsystemVersion;
819 mapping->image.subsystem_major = nt.opt.hdr64.MajorSubsystemVersion;
820 mapping->image.osversion_minor = nt.opt.hdr64.MinorOperatingSystemVersion;
821 mapping->image.osversion_major = nt.opt.hdr64.MajorOperatingSystemVersion;
822 mapping->image.dll_charact = nt.opt.hdr64.DllCharacteristics;
823 mapping->image.contains_code = (nt.opt.hdr64.SizeOfCode ||
824 nt.opt.hdr64.AddressOfEntryPoint ||
825 nt.opt.hdr64.SectionAlignment & page_mask);
826 mapping->image.header_size = nt.opt.hdr64.SizeOfHeaders;
827 mapping->image.checksum = nt.opt.hdr64.CheckSum;
828 mapping->image.image_flags = 0;
830 has_relocs = (nt.opt.hdr64.NumberOfRvaAndSizes > IMAGE_DIRECTORY_ENTRY_BASERELOC &&
831 nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress &&
832 nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size &&
833 !(nt.FileHeader.Characteristics & IMAGE_FILE_RELOCS_STRIPPED));
834 if (nt.opt.hdr64.SectionAlignment & page_mask)
835 mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
836 else if ((nt.opt.hdr64.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
837 (has_relocs || mapping->image.contains_code) && !(clr_va && clr_size))
838 mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
839 break;
841 default:
842 return STATUS_INVALID_IMAGE_FORMAT;
845 mapping->image.map_addr = get_fd_map_address( mapping->fd );
846 mapping->image.image_charact = nt.FileHeader.Characteristics;
847 mapping->image.machine = nt.FileHeader.Machine;
848 mapping->image.dbg_offset = nt.FileHeader.PointerToSymbolTable;
849 mapping->image.dbg_size = nt.FileHeader.NumberOfSymbols;
850 mapping->image.zerobits = 0; /* FIXME */
851 mapping->image.file_size = file_size;
852 mapping->image.loader_flags = clr_va && clr_size;
853 mapping->image.wine_builtin = (mz_size == sizeof(mz) &&
854 !memcmp( mz.buffer, builtin_signature, sizeof(builtin_signature) ));
855 mapping->image.wine_fakedll = (mz_size == sizeof(mz) &&
856 !memcmp( mz.buffer, fakedll_signature, sizeof(fakedll_signature) ));
858 /* load the section headers */
860 pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
861 if (nt.FileHeader.NumberOfSections > ARRAY_SIZE( sec )) return STATUS_INVALID_IMAGE_FORMAT;
862 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
863 if (!mapping->size) mapping->size = mapping->image.map_size;
864 else if (mapping->size > mapping->image.map_size) return STATUS_SECTION_TOO_BIG;
865 if (pos + size > mapping->image.map_size) return STATUS_INVALID_FILE_FOR_SECTION;
866 if (pos + size > mapping->image.header_size) mapping->image.header_size = pos + size;
867 if (pread( unix_fd, sec, size, pos ) != size) return STATUS_INVALID_FILE_FOR_SECTION;
869 for (i = 0; i < nt.FileHeader.NumberOfSections && !mapping->image.contains_code; i++)
870 if (sec[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) mapping->image.contains_code = 1;
872 if (load_clr_header( &clr, clr_va, clr_size, unix_fd, sec, nt.FileHeader.NumberOfSections ) &&
873 (clr.Flags & COMIMAGE_FLAGS_ILONLY))
875 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusILOnly;
876 if (nt.opt.hdr32.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
878 if (!(clr.Flags & COMIMAGE_FLAGS_32BITREQUIRED))
879 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusNativeReady;
880 if (clr.Flags & COMIMAGE_FLAGS_32BITPREFERRED)
881 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusPrefer32bit;
885 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections ))
886 return STATUS_INVALID_FILE_FOR_SECTION;
888 return STATUS_SUCCESS;
891 static struct ranges *create_ranges(void)
893 struct ranges *ranges = alloc_object( &ranges_ops );
895 if (!ranges) return NULL;
896 ranges->count = 0;
897 ranges->max = 8;
898 if (!(ranges->ranges = mem_alloc( ranges->max * sizeof(*ranges->ranges) )))
900 release_object( ranges );
901 return NULL;
903 return ranges;
906 static unsigned int get_mapping_flags( obj_handle_t handle, unsigned int flags )
908 switch (flags & (SEC_IMAGE | SEC_RESERVE | SEC_COMMIT | SEC_FILE))
910 case SEC_IMAGE:
911 if (flags & (SEC_WRITECOMBINE | SEC_LARGE_PAGES)) break;
912 if (handle) return SEC_FILE | SEC_IMAGE;
913 set_error( STATUS_INVALID_FILE_FOR_SECTION );
914 return 0;
915 case SEC_COMMIT:
916 if (!handle) return flags;
917 /* fall through */
918 case SEC_RESERVE:
919 if (flags & SEC_LARGE_PAGES) break;
920 if (handle) return SEC_FILE | (flags & (SEC_NOCACHE | SEC_WRITECOMBINE));
921 return flags;
923 set_error( STATUS_INVALID_PARAMETER );
924 return 0;
928 static struct mapping *create_mapping( struct object *root, const struct unicode_str *name,
929 unsigned int attr, mem_size_t size, unsigned int flags,
930 obj_handle_t handle, unsigned int file_access,
931 const struct security_descriptor *sd )
933 struct mapping *mapping;
934 struct file *file;
935 struct fd *fd;
936 int unix_fd;
937 struct stat st;
939 if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd )))
940 return NULL;
941 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
942 return mapping; /* Nothing else to do */
944 mapping->size = size;
945 mapping->fd = NULL;
946 mapping->shared = NULL;
947 mapping->committed = NULL;
949 if (!(mapping->flags = get_mapping_flags( handle, flags ))) goto error;
951 if (handle)
953 const unsigned int sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
954 unsigned int mapping_access = FILE_MAPPING_ACCESS;
956 if (!(file = get_file_obj( current->process, handle, file_access ))) goto error;
957 fd = get_obj_fd( (struct object *)file );
959 /* file sharing rules for mappings are different so we use magic the access rights */
960 if (flags & SEC_IMAGE) mapping_access |= FILE_MAPPING_IMAGE;
961 else if (file_access & FILE_WRITE_DATA) mapping_access |= FILE_MAPPING_WRITE;
963 if (!(mapping->fd = get_fd_object_for_mapping( fd, mapping_access, sharing )))
965 mapping->fd = dup_fd_object( fd, mapping_access, sharing, FILE_SYNCHRONOUS_IO_NONALERT );
966 if (mapping->fd) set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
968 release_object( file );
969 release_object( fd );
970 if (!mapping->fd) goto error;
972 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
973 if (fstat( unix_fd, &st ) == -1)
975 file_set_error();
976 goto error;
978 if (flags & SEC_IMAGE)
980 unsigned int err = get_image_params( mapping, st.st_size, unix_fd );
981 if (!err) return mapping;
982 set_error( err );
983 goto error;
985 if (!mapping->size)
987 if (!(mapping->size = st.st_size))
989 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
990 goto error;
993 else if (st.st_size < mapping->size)
995 if (!(file_access & FILE_WRITE_DATA))
997 set_error( STATUS_SECTION_TOO_BIG );
998 goto error;
1000 if (!grow_file( unix_fd, mapping->size )) goto error;
1003 else /* Anonymous mapping (no associated file) */
1005 if (!mapping->size)
1007 set_error( STATUS_INVALID_PARAMETER );
1008 goto error;
1010 if ((flags & SEC_RESERVE) && !(mapping->committed = create_ranges())) goto error;
1011 mapping->size = (mapping->size + page_mask) & ~((mem_size_t)page_mask);
1012 if ((unix_fd = create_temp_file( mapping->size )) == -1) goto error;
1013 if (!(mapping->fd = create_anonymous_fd( &mapping_fd_ops, unix_fd, &mapping->obj,
1014 FILE_SYNCHRONOUS_IO_NONALERT ))) goto error;
1015 allow_fd_caching( mapping->fd );
1017 return mapping;
1019 error:
1020 release_object( mapping );
1021 return NULL;
1024 /* create a read-only file mapping for the specified fd */
1025 struct mapping *create_fd_mapping( struct object *root, const struct unicode_str *name,
1026 struct fd *fd, unsigned int attr, const struct security_descriptor *sd )
1028 struct mapping *mapping;
1029 int unix_fd;
1030 struct stat st;
1032 if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd ))) return NULL;
1033 if (get_error() == STATUS_OBJECT_NAME_EXISTS) return mapping; /* Nothing else to do */
1035 mapping->shared = NULL;
1036 mapping->committed = NULL;
1037 mapping->flags = SEC_FILE;
1038 mapping->fd = (struct fd *)grab_object( fd );
1039 set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
1041 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
1042 if (fstat( unix_fd, &st ) == -1)
1044 file_set_error();
1045 goto error;
1047 if (!(mapping->size = st.st_size))
1049 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
1050 goto error;
1052 return mapping;
1054 error:
1055 release_object( mapping );
1056 return NULL;
1059 static struct mapping *get_mapping_obj( struct process *process, obj_handle_t handle, unsigned int access )
1061 return (struct mapping *)get_handle_obj( process, handle, access, &mapping_ops );
1064 /* open a new file for the file descriptor backing the view */
1065 struct file *get_view_file( const struct memory_view *view, unsigned int access, unsigned int sharing )
1067 if (!view->fd) return NULL;
1068 return create_file_for_fd_obj( view->fd, access, sharing );
1071 /* get the image info for a SEC_IMAGE mapped view */
1072 const pe_image_info_t *get_view_image_info( const struct memory_view *view, client_ptr_t *base )
1074 if (!(view->flags & SEC_IMAGE)) return NULL;
1075 *base = view->base;
1076 return &view->image;
1079 /* get the file name for a mapped view */
1080 int get_view_nt_name( const struct memory_view *view, struct unicode_str *name )
1082 if (view->namelen) /* .so builtin */
1084 name->str = view->name;
1085 name->len = view->namelen;
1086 return 1;
1088 if (!view->fd) return 0;
1089 get_nt_name( view->fd, name );
1090 return 1;
1093 /* generate all startup events of a given process */
1094 void generate_startup_debug_events( struct process *process )
1096 struct memory_view *view;
1097 struct list *ptr = list_head( &process->views );
1098 struct thread *thread, *first_thread = get_process_first_thread( process );
1100 if (!ptr) return;
1101 view = LIST_ENTRY( ptr, struct memory_view, entry );
1102 generate_debug_event( first_thread, DbgCreateProcessStateChange, view );
1104 /* generate ntdll.dll load event */
1105 while (ptr && (ptr = list_next( &process->views, ptr )))
1107 view = LIST_ENTRY( ptr, struct memory_view, entry );
1108 if (generate_dll_event( first_thread, DbgLoadDllStateChange, view )) break;
1111 /* generate creation events */
1112 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1114 if (thread != first_thread)
1115 generate_debug_event( thread, DbgCreateThreadStateChange, NULL );
1118 /* generate dll events (in loading order) */
1119 while (ptr && (ptr = list_next( &process->views, ptr )))
1121 view = LIST_ENTRY( ptr, struct memory_view, entry );
1122 generate_dll_event( first_thread, DbgLoadDllStateChange, view );
1126 static void mapping_dump( struct object *obj, int verbose )
1128 struct mapping *mapping = (struct mapping *)obj;
1129 assert( obj->ops == &mapping_ops );
1130 fprintf( stderr, "Mapping size=%08x%08x flags=%08x fd=%p shared=%p\n",
1131 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
1132 mapping->flags, mapping->fd, mapping->shared );
1135 static struct fd *mapping_get_fd( struct object *obj )
1137 struct mapping *mapping = (struct mapping *)obj;
1138 return (struct fd *)grab_object( mapping->fd );
1141 static void mapping_destroy( struct object *obj )
1143 struct mapping *mapping = (struct mapping *)obj;
1144 assert( obj->ops == &mapping_ops );
1145 if (mapping->fd) release_object( mapping->fd );
1146 if (mapping->committed) release_object( mapping->committed );
1147 if (mapping->shared) release_object( mapping->shared );
1150 static enum server_fd_type mapping_get_fd_type( struct fd *fd )
1152 return FD_TYPE_FILE;
1155 /* assign a mapping address to a PE image mapping */
1156 static client_ptr_t assign_map_address( struct mapping *mapping )
1158 unsigned int i;
1159 client_ptr_t ret;
1160 struct addr_range *range = (mapping->image.base >> 32) ? &ranges64 : &ranges32;
1161 mem_size_t size = (mapping->size + granularity_mask) & ~granularity_mask;
1163 if (!(mapping->image.image_charact & IMAGE_FILE_DLL)) return 0;
1165 if ((ret = get_fd_map_address( mapping->fd ))) return ret;
1167 for (i = 0; i < range->count; i++)
1169 if (range->free[i].size < size) continue;
1170 range->free[i].size -= size;
1171 ret = range->free[i].base + range->free[i].size;
1172 set_fd_map_address( mapping->fd, ret, size );
1173 return ret;
1175 return 0;
1178 /* free a PE mapping address range when the last mapping is closed */
1179 void free_map_addr( client_ptr_t base, mem_size_t size )
1181 unsigned int i;
1182 client_ptr_t end = base + size;
1183 struct addr_range *range = (base >> 32) ? &ranges64 : &ranges32;
1185 for (i = 0; i < range->count; i++)
1187 if (range->free[i].base > end) continue;
1188 if (range->free[i].base + range->free[i].size < base) break;
1189 if (range->free[i].base == end)
1191 if (i + 1 < range->count && range->free[i + 1].base + range->free[i + 1].size == base)
1193 size += range->free[i].size;
1194 range->count--;
1195 memmove( &range->free[i], &range->free[i + 1], (range->count - i) * sizeof(*range->free) );
1197 else range->free[i].base = base;
1199 range->free[i].size += size;
1200 return;
1203 if (range->count == range->size)
1205 unsigned int new_size = max( 256, range->size * 2 );
1206 void *new_free = realloc( range->free, new_size * sizeof(*range->free) );
1207 if (!new_free) return;
1208 range->size = new_size;
1209 range->free = new_free;
1211 memmove( &range->free[i + 1], &range->free[i], (range->count - i) * sizeof(*range->free) );
1212 range->free[i].base = base;
1213 range->free[i].size = size;
1214 range->count++;
1217 int get_page_size(void)
1219 return page_mask + 1;
1222 struct object *create_user_data_mapping( struct object *root, const struct unicode_str *name,
1223 unsigned int attr, const struct security_descriptor *sd )
1225 void *ptr;
1226 struct mapping *mapping;
1228 if (!(mapping = create_mapping( root, name, attr, sizeof(KSHARED_USER_DATA),
1229 SEC_COMMIT, 0, FILE_READ_DATA | FILE_WRITE_DATA, sd ))) return NULL;
1230 ptr = mmap( NULL, mapping->size, PROT_WRITE, MAP_SHARED, get_unix_fd( mapping->fd ), 0 );
1231 if (ptr != MAP_FAILED)
1233 user_shared_data = ptr;
1234 user_shared_data->SystemCall = 1;
1236 return &mapping->obj;
1239 /* create a file mapping */
1240 DECL_HANDLER(create_mapping)
1242 struct object *root;
1243 struct mapping *mapping;
1244 struct unicode_str name;
1245 const struct security_descriptor *sd;
1246 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1248 if (!objattr) return;
1250 if ((mapping = create_mapping( root, &name, objattr->attributes, req->size, req->flags,
1251 req->file_handle, req->file_access, sd )))
1253 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1254 reply->handle = alloc_handle( current->process, &mapping->obj, req->access, objattr->attributes );
1255 else
1256 reply->handle = alloc_handle_no_access_check( current->process, &mapping->obj,
1257 req->access, objattr->attributes );
1258 release_object( mapping );
1261 if (root) release_object( root );
1264 /* open a handle to a mapping */
1265 DECL_HANDLER(open_mapping)
1267 struct unicode_str name = get_req_unicode_str();
1269 reply->handle = open_object( current->process, req->rootdir, req->access,
1270 &mapping_ops, &name, req->attributes );
1273 /* get a mapping information */
1274 DECL_HANDLER(get_mapping_info)
1276 struct mapping *mapping;
1278 if (!(mapping = get_mapping_obj( current->process, req->handle, req->access ))) return;
1280 reply->size = mapping->size;
1281 reply->flags = mapping->flags;
1283 if (mapping->flags & SEC_IMAGE)
1285 struct unicode_str name = { NULL, 0 };
1286 data_size_t size;
1287 void *data;
1289 if (mapping->fd) get_nt_name( mapping->fd, &name );
1290 size = min( sizeof(pe_image_info_t) + name.len, get_reply_max_size() );
1291 if ((data = set_reply_data_size( size )))
1293 memcpy( data, &mapping->image, min( sizeof(pe_image_info_t), size ));
1294 if (size > sizeof(pe_image_info_t))
1295 memcpy( (pe_image_info_t *)data + 1, name.str, size - sizeof(pe_image_info_t) );
1297 reply->total = sizeof(pe_image_info_t) + name.len;
1300 if (!(req->access & (SECTION_MAP_READ | SECTION_MAP_WRITE))) /* query only */
1302 release_object( mapping );
1303 return;
1306 if (mapping->shared)
1307 reply->shared_file = alloc_handle( current->process, mapping->shared->file,
1308 GENERIC_READ|GENERIC_WRITE, 0 );
1309 release_object( mapping );
1312 /* get the address to use to map an image mapping */
1313 DECL_HANDLER(get_image_map_address)
1315 struct mapping *mapping;
1317 if (!(mapping = get_mapping_obj( current->process, req->handle, SECTION_MAP_READ ))) return;
1319 if ((mapping->flags & SEC_IMAGE) &&
1320 (mapping->image.image_flags & IMAGE_FLAGS_ImageDynamicallyRelocated))
1322 if (!mapping->image.map_addr) mapping->image.map_addr = assign_map_address( mapping );
1323 reply->addr = mapping->image.map_addr;
1325 else set_error( STATUS_INVALID_PARAMETER );
1327 release_object( mapping );
1330 /* add a memory view in the current process */
1331 DECL_HANDLER(map_view)
1333 struct mapping *mapping;
1334 struct memory_view *view;
1336 if (!is_valid_view_addr( current->process, req->base, req->size ))
1338 set_error( STATUS_INVALID_PARAMETER );
1339 return;
1342 if (!(mapping = get_mapping_obj( current->process, req->mapping, req->access ))) return;
1344 if ((mapping->flags & SEC_IMAGE) ||
1345 req->start >= mapping->size ||
1346 req->start + req->size < req->start ||
1347 req->start + req->size > ((mapping->size + page_mask) & ~(mem_size_t)page_mask))
1349 set_error( STATUS_INVALID_PARAMETER );
1350 goto done;
1353 if ((view = mem_alloc( sizeof(*view) )))
1355 view->base = req->base;
1356 view->size = req->size;
1357 view->start = req->start;
1358 view->flags = mapping->flags;
1359 view->namelen = 0;
1360 view->fd = !is_fd_removable( mapping->fd ) ? (struct fd *)grab_object( mapping->fd ) : NULL;
1361 view->committed = mapping->committed ? (struct ranges *)grab_object( mapping->committed ) : NULL;
1362 view->shared = NULL;
1363 add_process_view( current, view );
1366 done:
1367 release_object( mapping );
1370 /* add a memory view for an image mapping in the current process */
1371 DECL_HANDLER(map_image_view)
1373 struct mapping *mapping;
1374 struct memory_view *view;
1376 if (!is_valid_view_addr( current->process, req->base, req->size ))
1378 set_error( STATUS_INVALID_PARAMETER );
1379 return;
1382 if (!(mapping = get_mapping_obj( current->process, req->mapping, SECTION_MAP_READ ))) return;
1384 if (!(mapping->flags & SEC_IMAGE) || req->size > mapping->image.map_size)
1386 set_error( STATUS_INVALID_PARAMETER );
1387 goto done;
1390 if ((view = mem_alloc( sizeof(*view) )))
1392 view->base = req->base;
1393 view->size = req->size;
1394 view->flags = mapping->flags;
1395 view->start = 0;
1396 view->namelen = 0;
1397 view->fd = !is_fd_removable( mapping->fd ) ? (struct fd *)grab_object( mapping->fd ) : NULL;
1398 view->committed = NULL;
1399 view->shared = mapping->shared ? (struct shared_map *)grab_object( mapping->shared ) : NULL;
1400 view->image = mapping->image;
1401 view->image.machine = req->machine;
1402 view->image.entry_point = req->entry;
1403 add_process_view( current, view );
1405 if (view->base != (mapping->image.map_addr ? mapping->image.map_addr : mapping->image.base))
1406 set_error( STATUS_IMAGE_NOT_AT_BASE );
1407 if (view->image.machine != current->process->machine)
1409 /* on 32-bit, the native 64-bit machine is allowed */
1410 if (is_machine_64bit( current->process->machine ) || view->image.machine != native_machine)
1411 set_error( STATUS_IMAGE_MACHINE_TYPE_MISMATCH );
1415 done:
1416 release_object( mapping );
1419 /* add a memory view for a builtin dll in the current process */
1420 DECL_HANDLER(map_builtin_view)
1422 struct memory_view *view;
1423 const pe_image_info_t *image = get_req_data();
1424 data_size_t namelen = get_req_data_size() - sizeof(*image);
1426 if (get_req_data_size() < sizeof(*image) ||
1427 (namelen & (sizeof(WCHAR) - 1)) ||
1428 !is_valid_view_addr( current->process, image->base, image->map_size ))
1430 set_error( STATUS_INVALID_PARAMETER );
1431 return;
1434 if ((view = mem_alloc( sizeof(struct memory_view) + namelen )))
1436 memset( view, 0, sizeof(*view) );
1437 view->base = image->base;
1438 view->size = image->map_size;
1439 view->flags = SEC_IMAGE;
1440 view->image = *image;
1441 view->namelen = namelen;
1442 memcpy( view->name, image + 1, namelen );
1443 add_process_view( current, view );
1447 /* unmap a memory view from the current process */
1448 DECL_HANDLER(unmap_view)
1450 struct memory_view *view = find_mapped_view( current->process, req->base );
1452 if (!view) return;
1453 generate_dll_event( current, DbgUnloadDllStateChange, view );
1454 free_memory_view( view );
1457 /* get information about a mapped image view */
1458 DECL_HANDLER(get_image_view_info)
1460 struct process *process;
1461 struct memory_view *view;
1463 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION ))) return;
1465 if ((view = find_mapped_addr( process, req->addr )) && (view->flags & SEC_IMAGE))
1467 reply->base = view->base;
1468 reply->size = view->size;
1471 release_object( process );
1474 /* get a range of committed pages in a file mapping */
1475 DECL_HANDLER(get_mapping_committed_range)
1477 struct memory_view *view = find_mapped_view( current->process, req->base );
1479 if (view) reply->committed = find_committed_range( view, req->offset, &reply->size );
1482 /* add a range to the committed pages in a file mapping */
1483 DECL_HANDLER(add_mapping_committed_range)
1485 struct memory_view *view = find_mapped_view( current->process, req->base );
1487 if (view) add_committed_range( view, req->offset, req->offset + req->size );
1490 /* check if two memory maps are for the same file */
1491 DECL_HANDLER(is_same_mapping)
1493 struct memory_view *view1 = find_mapped_view( current->process, req->base1 );
1494 struct memory_view *view2 = find_mapped_view( current->process, req->base2 );
1496 if (!view1 || !view2) return;
1497 if (!view1->fd || !view2->fd || !(view1->flags & SEC_IMAGE) || !is_same_file_fd( view1->fd, view2->fd ))
1498 set_error( STATUS_NOT_SAME_DEVICE );
1501 /* get the filename of a mapping */
1502 DECL_HANDLER(get_mapping_filename)
1504 struct process *process;
1505 struct memory_view *view;
1506 struct unicode_str name;
1508 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION ))) return;
1510 if ((view = find_mapped_addr( process, req->addr )) && get_view_nt_name( view, &name ))
1512 reply->len = name.len;
1513 if (name.len > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
1514 else if (!name.len) set_error( STATUS_FILE_INVALID );
1515 else set_reply_data( name.str, name.len );
1517 else set_error( STATUS_INVALID_ADDRESS );
1519 release_object( process );