gitlab: Use --enable-werror for Clang builds.
[wine.git] / server / mapping.c
blobff99b45ce51a472ed96250c523cf62fc49da58a3
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 snprintf( name, 16, "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 int generate_dll_event( struct thread *thread, int code, struct memory_view *view )
409 if (!(view->flags & SEC_IMAGE)) return 0;
410 generate_debug_event( thread, code, view );
411 return 1;
414 /* add a view to the process list */
415 /* return 1 if this is the main exe view */
416 static int add_process_view( struct thread *thread, struct memory_view *view )
418 struct process *process = thread->process;
419 struct unicode_str name;
421 if (view->flags & SEC_IMAGE)
423 if (is_process_init_done( process ))
425 generate_dll_event( thread, DbgLoadDllStateChange, view );
427 else if (!(view->image.image_charact & IMAGE_FILE_DLL))
429 /* main exe */
430 free( process->image );
431 process->image = NULL;
432 if (get_view_nt_name( view, &name ) && (process->image = memdup( name.str, name.len )))
433 process->imagelen = name.len;
434 process->image_info = view->image;
435 list_add_head( &process->views, &view->entry );
436 return 1;
439 list_add_tail( &process->views, &view->entry );
440 return 0;
443 static void free_memory_view( struct memory_view *view )
445 if (view->fd) release_object( view->fd );
446 if (view->committed) release_object( view->committed );
447 if (view->shared) release_object( view->shared );
448 list_remove( &view->entry );
449 free( view );
452 /* free all mapped views at process exit */
453 void free_mapped_views( struct process *process )
455 struct list *ptr;
457 while ((ptr = list_head( &process->views )))
458 free_memory_view( LIST_ENTRY( ptr, struct memory_view, entry ));
461 /* find the shared PE mapping for a given mapping */
462 static struct shared_map *get_shared_file( struct fd *fd )
464 struct shared_map *ptr;
466 LIST_FOR_EACH_ENTRY( ptr, &shared_map_list, struct shared_map, entry )
467 if (is_same_file_fd( ptr->fd, fd ))
468 return (struct shared_map *)grab_object( ptr );
469 return NULL;
472 /* return the size of the memory mapping and file range of a given section */
473 static inline void get_section_sizes( const IMAGE_SECTION_HEADER *sec, size_t *map_size,
474 off_t *file_start, size_t *file_size )
476 static const unsigned int sector_align = 0x1ff;
478 if (!sec->Misc.VirtualSize) *map_size = ROUND_SIZE( sec->SizeOfRawData );
479 else *map_size = ROUND_SIZE( sec->Misc.VirtualSize );
481 *file_start = sec->PointerToRawData & ~sector_align;
482 *file_size = (sec->SizeOfRawData + (sec->PointerToRawData & sector_align) + sector_align) & ~sector_align;
483 if (*file_size > *map_size) *file_size = *map_size;
486 /* add a range to the committed list */
487 static void add_committed_range( struct memory_view *view, file_pos_t start, file_pos_t end )
489 unsigned int i, j;
490 struct ranges *committed = view->committed;
491 struct range *ranges;
493 if ((start & page_mask) || (end & page_mask) ||
494 start >= view->size || end >= view->size ||
495 start >= end)
497 set_error( STATUS_INVALID_PARAMETER );
498 return;
501 if (!committed) return; /* everything committed already */
503 start += view->start;
504 end += view->start;
506 for (i = 0, ranges = committed->ranges; i < committed->count; i++)
508 if (ranges[i].start > end) break;
509 if (ranges[i].end < start) continue;
510 if (ranges[i].start > start) ranges[i].start = start; /* extend downwards */
511 if (ranges[i].end < end) /* extend upwards and maybe merge with next */
513 for (j = i + 1; j < committed->count; j++)
515 if (ranges[j].start > end) break;
516 if (ranges[j].end > end) end = ranges[j].end;
518 if (j > i + 1)
520 memmove( &ranges[i + 1], &ranges[j], (committed->count - j) * sizeof(*ranges) );
521 committed->count -= j - (i + 1);
523 ranges[i].end = end;
525 return;
528 /* now add a new range */
530 if (committed->count == committed->max)
532 unsigned int new_size = committed->max * 2;
533 struct range *new_ptr = realloc( committed->ranges, new_size * sizeof(*new_ptr) );
534 if (!new_ptr) return;
535 committed->max = new_size;
536 ranges = committed->ranges = new_ptr;
538 memmove( &ranges[i + 1], &ranges[i], (committed->count - i) * sizeof(*ranges) );
539 ranges[i].start = start;
540 ranges[i].end = end;
541 committed->count++;
544 /* find the range containing start and return whether it's committed */
545 static int find_committed_range( struct memory_view *view, file_pos_t start, mem_size_t *size )
547 unsigned int i;
548 struct ranges *committed = view->committed;
549 struct range *ranges;
551 if ((start & page_mask) || start >= view->size)
553 set_error( STATUS_INVALID_PARAMETER );
554 return 0;
556 if (!committed) /* everything is committed */
558 *size = view->size - start;
559 return 1;
561 for (i = 0, ranges = committed->ranges; i < committed->count; i++)
563 if (ranges[i].start > view->start + start)
565 *size = min( ranges[i].start, view->start + view->size ) - (view->start + start);
566 return 0;
568 if (ranges[i].end > view->start + start)
570 *size = min( ranges[i].end, view->start + view->size ) - (view->start + start);
571 return 1;
574 *size = view->size - start;
575 return 0;
578 /* allocate and fill the temp file for a shared PE image mapping */
579 static int build_shared_mapping( struct mapping *mapping, int fd,
580 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
582 struct shared_map *shared;
583 struct file *file;
584 unsigned int i;
585 mem_size_t total_size;
586 size_t file_size, map_size, max_size;
587 off_t shared_pos, read_pos, write_pos;
588 char *buffer = NULL;
589 int shared_fd;
590 long toread;
592 /* compute the total size of the shared mapping */
594 total_size = max_size = 0;
595 for (i = 0; i < nb_sec; i++)
597 if ((sec[i].Characteristics & IMAGE_SCN_MEM_SHARED) &&
598 (sec[i].Characteristics & IMAGE_SCN_MEM_WRITE))
600 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
601 if (file_size > max_size) max_size = file_size;
602 total_size += map_size;
605 if (!total_size) return 1; /* nothing to do */
607 if ((mapping->shared = get_shared_file( mapping->fd ))) return 1;
609 /* create a temp file for the mapping */
611 if ((shared_fd = create_temp_file( total_size )) == -1) return 0;
612 if (!(file = create_file_for_fd( shared_fd, FILE_GENERIC_READ|FILE_GENERIC_WRITE, 0 ))) return 0;
614 if (!(buffer = malloc( max_size ))) goto error;
616 /* copy the shared sections data into the temp file */
618 shared_pos = 0;
619 for (i = 0; i < nb_sec; i++)
621 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_SHARED)) continue;
622 if (!(sec[i].Characteristics & IMAGE_SCN_MEM_WRITE)) continue;
623 get_section_sizes( &sec[i], &map_size, &read_pos, &file_size );
624 write_pos = shared_pos;
625 shared_pos += map_size;
626 if (!sec[i].PointerToRawData || !file_size) continue;
627 toread = file_size;
628 while (toread)
630 long res = pread( fd, buffer + file_size - toread, toread, read_pos );
631 if (!res && toread < 0x200) /* partial sector at EOF is not an error */
633 file_size -= toread;
634 break;
636 if (res <= 0) goto error;
637 toread -= res;
638 read_pos += res;
640 if (pwrite( shared_fd, buffer, file_size, write_pos ) != file_size) goto error;
643 if (!(shared = alloc_object( &shared_map_ops ))) goto error;
644 shared->fd = (struct fd *)grab_object( mapping->fd );
645 shared->file = file;
646 list_add_head( &shared_map_list, &shared->entry );
647 mapping->shared = shared;
648 free( buffer );
649 return 1;
651 error:
652 release_object( file );
653 free( buffer );
654 return 0;
657 /* load a data directory header from its section */
658 static int load_data_dir( void *dir, size_t dir_size, size_t va, size_t size, int unix_fd,
659 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
661 size_t map_size, file_size;
662 off_t file_start;
663 unsigned int i;
665 if (!va || !size) return 0;
667 for (i = 0; i < nb_sec; i++)
669 if (va < sec[i].VirtualAddress) continue;
670 if (sec[i].Misc.VirtualSize && va - sec[i].VirtualAddress >= sec[i].Misc.VirtualSize) continue;
671 get_section_sizes( &sec[i], &map_size, &file_start, &file_size );
672 if (size >= map_size) continue;
673 if (va - sec[i].VirtualAddress >= map_size - size) continue;
674 if (size > dir_size) size = dir_size;
675 if (size > file_size) size = file_size;
676 return pread( unix_fd, dir, size, file_start + va - sec[i].VirtualAddress );
678 return 0;
681 /* load the CLR header from its section */
682 static int load_clr_header( IMAGE_COR20_HEADER *hdr, size_t va, size_t size, int unix_fd,
683 IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
685 int ret = load_data_dir( hdr, sizeof(*hdr), va, size, unix_fd, sec, nb_sec );
687 if (ret <= 0) return 0;
688 if (ret < sizeof(*hdr)) memset( (char *)hdr + ret, 0, sizeof(*hdr) - ret );
689 return (hdr->MajorRuntimeVersion > COR_VERSION_MAJOR_V2 ||
690 (hdr->MajorRuntimeVersion == COR_VERSION_MAJOR_V2 &&
691 hdr->MinorRuntimeVersion >= COR_VERSION_MINOR));
694 /* load the LOAD_CONFIG header from its section */
695 static int load_cfg_header( IMAGE_LOAD_CONFIG_DIRECTORY64 *cfg, size_t va, size_t size,
696 int unix_fd, IMAGE_SECTION_HEADER *sec, unsigned int nb_sec )
698 unsigned int cfg_size;
699 int ret = load_data_dir( cfg, sizeof(*cfg), va, size, unix_fd, sec, nb_sec );
701 if (ret <= 0) return 0;
702 cfg_size = ret;
703 if (cfg_size < offsetof( IMAGE_LOAD_CONFIG_DIRECTORY64, Size ) + sizeof(cfg_size)) return 0;
704 if (cfg_size > cfg->Size) cfg_size = cfg->Size;
705 if (cfg_size < sizeof(*cfg)) memset( (char *)cfg + cfg_size, 0, sizeof(*cfg) - cfg_size );
706 return 1;
709 /* retrieve the mapping parameters for an executable (PE) image */
710 static unsigned int get_image_params( struct mapping *mapping, file_pos_t file_size, int unix_fd )
712 static const char builtin_signature[] = "Wine builtin DLL";
713 static const char fakedll_signature[] = "Wine placeholder DLL";
715 IMAGE_COR20_HEADER clr;
716 IMAGE_SECTION_HEADER sec[96];
717 struct
719 IMAGE_DOS_HEADER dos;
720 char buffer[32];
721 } mz;
722 struct
724 DWORD Signature;
725 IMAGE_FILE_HEADER FileHeader;
726 union
728 IMAGE_OPTIONAL_HEADER32 hdr32;
729 IMAGE_OPTIONAL_HEADER64 hdr64;
730 } opt;
731 } nt;
732 union
734 IMAGE_LOAD_CONFIG_DIRECTORY32 cfg32;
735 IMAGE_LOAD_CONFIG_DIRECTORY64 cfg64;
736 } cfg;
737 off_t pos;
738 int size, has_relocs;
739 size_t mz_size, clr_va = 0, clr_size = 0, cfg_va, cfg_size;
740 unsigned int i;
742 /* load the headers */
744 if (!file_size) return STATUS_INVALID_FILE_FOR_SECTION;
745 size = pread( unix_fd, &mz, sizeof(mz), 0 );
746 if (size < sizeof(mz.dos)) return STATUS_INVALID_IMAGE_NOT_MZ;
747 if (mz.dos.e_magic != IMAGE_DOS_SIGNATURE) return STATUS_INVALID_IMAGE_NOT_MZ;
748 mz_size = size;
749 pos = mz.dos.e_lfanew;
751 size = pread( unix_fd, &nt, sizeof(nt), pos );
752 if (size < sizeof(nt.Signature) + sizeof(nt.FileHeader)) return STATUS_INVALID_IMAGE_PROTECT;
753 if (size < sizeof(nt)) memset( (char *)&nt + size, 0, sizeof(nt) - size );
754 if (nt.Signature != IMAGE_NT_SIGNATURE)
756 IMAGE_OS2_HEADER *os2 = (IMAGE_OS2_HEADER *)&nt;
757 if (os2->ne_magic != IMAGE_OS2_SIGNATURE) return STATUS_INVALID_IMAGE_PROTECT;
758 if (os2->ne_exetyp == 2) return STATUS_INVALID_IMAGE_WIN_16;
759 if (os2->ne_exetyp == 5) return STATUS_INVALID_IMAGE_PROTECT;
760 return STATUS_INVALID_IMAGE_NE_FORMAT;
763 switch (nt.opt.hdr32.Magic)
765 case IMAGE_NT_OPTIONAL_HDR32_MAGIC:
766 if (!is_machine_32bit( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
767 if (!is_machine_supported( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
769 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_I386) /* non-x86 platforms are more strict */
771 if (nt.opt.hdr32.SectionAlignment & page_mask)
772 return STATUS_INVALID_IMAGE_FORMAT;
773 if (!(nt.opt.hdr32.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_NX_COMPAT))
774 return STATUS_INVALID_IMAGE_FORMAT;
775 if (!(nt.opt.hdr32.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE))
776 return STATUS_INVALID_IMAGE_FORMAT;
778 if (nt.opt.hdr32.NumberOfRvaAndSizes > IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR)
780 clr_va = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
781 clr_size = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
783 cfg_va = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].VirtualAddress;
784 cfg_size = nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].Size;
786 mapping->image.base = nt.opt.hdr32.ImageBase;
787 mapping->image.entry_point = nt.opt.hdr32.AddressOfEntryPoint;
788 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr32.SizeOfImage );
789 mapping->image.stack_size = nt.opt.hdr32.SizeOfStackReserve;
790 mapping->image.stack_commit = nt.opt.hdr32.SizeOfStackCommit;
791 mapping->image.subsystem = nt.opt.hdr32.Subsystem;
792 mapping->image.subsystem_minor = nt.opt.hdr32.MinorSubsystemVersion;
793 mapping->image.subsystem_major = nt.opt.hdr32.MajorSubsystemVersion;
794 mapping->image.osversion_minor = nt.opt.hdr32.MinorOperatingSystemVersion;
795 mapping->image.osversion_major = nt.opt.hdr32.MajorOperatingSystemVersion;
796 mapping->image.dll_charact = nt.opt.hdr32.DllCharacteristics;
797 mapping->image.contains_code = (nt.opt.hdr32.SizeOfCode ||
798 nt.opt.hdr32.AddressOfEntryPoint ||
799 nt.opt.hdr32.SectionAlignment & page_mask);
800 mapping->image.header_size = nt.opt.hdr32.SizeOfHeaders;
801 mapping->image.checksum = nt.opt.hdr32.CheckSum;
802 mapping->image.image_flags = 0;
804 has_relocs = (nt.opt.hdr32.NumberOfRvaAndSizes > IMAGE_DIRECTORY_ENTRY_BASERELOC &&
805 nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress &&
806 nt.opt.hdr32.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size &&
807 !(nt.FileHeader.Characteristics & IMAGE_FILE_RELOCS_STRIPPED));
808 if (nt.opt.hdr32.SectionAlignment & page_mask)
809 mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
810 else if ((nt.opt.hdr32.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
811 (has_relocs || mapping->image.contains_code) && !(clr_va && clr_size))
812 mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
813 break;
815 case IMAGE_NT_OPTIONAL_HDR64_MAGIC:
816 if (!is_machine_64bit( native_machine )) return STATUS_INVALID_IMAGE_WIN_64;
817 if (!is_machine_64bit( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
818 if (!is_machine_supported( nt.FileHeader.Machine )) return STATUS_INVALID_IMAGE_FORMAT;
820 if (nt.FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64) /* non-x86 platforms are more strict */
822 if (nt.opt.hdr64.SectionAlignment & page_mask)
823 return STATUS_INVALID_IMAGE_FORMAT;
824 if (!(nt.opt.hdr64.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_NX_COMPAT))
825 return STATUS_INVALID_IMAGE_FORMAT;
826 if (!(nt.opt.hdr64.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE))
827 return STATUS_INVALID_IMAGE_FORMAT;
829 if (nt.opt.hdr64.NumberOfRvaAndSizes > IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR)
831 clr_va = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
832 clr_size = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].Size;
834 cfg_va = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].VirtualAddress;
835 cfg_size = nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG].Size;
837 mapping->image.base = nt.opt.hdr64.ImageBase;
838 mapping->image.entry_point = nt.opt.hdr64.AddressOfEntryPoint;
839 mapping->image.map_size = ROUND_SIZE( nt.opt.hdr64.SizeOfImage );
840 mapping->image.stack_size = nt.opt.hdr64.SizeOfStackReserve;
841 mapping->image.stack_commit = nt.opt.hdr64.SizeOfStackCommit;
842 mapping->image.subsystem = nt.opt.hdr64.Subsystem;
843 mapping->image.subsystem_minor = nt.opt.hdr64.MinorSubsystemVersion;
844 mapping->image.subsystem_major = nt.opt.hdr64.MajorSubsystemVersion;
845 mapping->image.osversion_minor = nt.opt.hdr64.MinorOperatingSystemVersion;
846 mapping->image.osversion_major = nt.opt.hdr64.MajorOperatingSystemVersion;
847 mapping->image.dll_charact = nt.opt.hdr64.DllCharacteristics;
848 mapping->image.contains_code = (nt.opt.hdr64.SizeOfCode ||
849 nt.opt.hdr64.AddressOfEntryPoint ||
850 nt.opt.hdr64.SectionAlignment & page_mask);
851 mapping->image.header_size = nt.opt.hdr64.SizeOfHeaders;
852 mapping->image.checksum = nt.opt.hdr64.CheckSum;
853 mapping->image.image_flags = 0;
855 has_relocs = (nt.opt.hdr64.NumberOfRvaAndSizes > IMAGE_DIRECTORY_ENTRY_BASERELOC &&
856 nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress &&
857 nt.opt.hdr64.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].Size &&
858 !(nt.FileHeader.Characteristics & IMAGE_FILE_RELOCS_STRIPPED));
859 if (nt.opt.hdr64.SectionAlignment & page_mask)
860 mapping->image.image_flags |= IMAGE_FLAGS_ImageMappedFlat;
861 else if ((nt.opt.hdr64.DllCharacteristics & IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE) &&
862 (has_relocs || mapping->image.contains_code) && !(clr_va && clr_size))
863 mapping->image.image_flags |= IMAGE_FLAGS_ImageDynamicallyRelocated;
864 break;
866 default:
867 return STATUS_INVALID_IMAGE_FORMAT;
870 mapping->image.is_hybrid = 0;
871 mapping->image.padding = 0;
872 mapping->image.map_addr = get_fd_map_address( mapping->fd );
873 mapping->image.image_charact = nt.FileHeader.Characteristics;
874 mapping->image.machine = nt.FileHeader.Machine;
875 mapping->image.dbg_offset = nt.FileHeader.PointerToSymbolTable;
876 mapping->image.dbg_size = nt.FileHeader.NumberOfSymbols;
877 mapping->image.zerobits = 0; /* FIXME */
878 mapping->image.file_size = file_size;
879 mapping->image.loader_flags = clr_va && clr_size;
880 mapping->image.wine_builtin = (mz_size == sizeof(mz) &&
881 !memcmp( mz.buffer, builtin_signature, sizeof(builtin_signature) ));
882 mapping->image.wine_fakedll = (mz_size == sizeof(mz) &&
883 !memcmp( mz.buffer, fakedll_signature, sizeof(fakedll_signature) ));
885 /* load the section headers */
887 pos += sizeof(nt.Signature) + sizeof(nt.FileHeader) + nt.FileHeader.SizeOfOptionalHeader;
888 if (nt.FileHeader.NumberOfSections > ARRAY_SIZE( sec )) return STATUS_INVALID_IMAGE_FORMAT;
889 size = sizeof(*sec) * nt.FileHeader.NumberOfSections;
890 if (!mapping->size) mapping->size = mapping->image.map_size;
891 else if (mapping->size > mapping->image.map_size) return STATUS_SECTION_TOO_BIG;
892 if (pos + size > mapping->image.map_size) return STATUS_INVALID_FILE_FOR_SECTION;
893 if (pos + size > mapping->image.header_size) mapping->image.header_size = pos + size;
894 if (pread( unix_fd, sec, size, pos ) != size) return STATUS_INVALID_FILE_FOR_SECTION;
896 for (i = 0; i < nt.FileHeader.NumberOfSections && !mapping->image.contains_code; i++)
897 if (sec[i].Characteristics & IMAGE_SCN_MEM_EXECUTE) mapping->image.contains_code = 1;
899 if (load_clr_header( &clr, clr_va, clr_size, unix_fd, sec, nt.FileHeader.NumberOfSections ) &&
900 (clr.Flags & COMIMAGE_FLAGS_ILONLY))
902 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusILOnly;
903 if (nt.opt.hdr32.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
905 if (!(clr.Flags & COMIMAGE_FLAGS_32BITREQUIRED))
906 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusNativeReady;
907 if (clr.Flags & COMIMAGE_FLAGS_32BITPREFERRED)
908 mapping->image.image_flags |= IMAGE_FLAGS_ComPlusPrefer32bit;
912 if (load_cfg_header( &cfg.cfg64, cfg_va, cfg_size, unix_fd, sec, nt.FileHeader.NumberOfSections ))
914 if (nt.opt.hdr32.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC)
915 mapping->image.is_hybrid = !!cfg.cfg32.CHPEMetadataPointer;
916 else
917 mapping->image.is_hybrid = !!cfg.cfg64.CHPEMetadataPointer;
920 if (!build_shared_mapping( mapping, unix_fd, sec, nt.FileHeader.NumberOfSections ))
921 return STATUS_INVALID_FILE_FOR_SECTION;
923 return STATUS_SUCCESS;
926 static struct ranges *create_ranges(void)
928 struct ranges *ranges = alloc_object( &ranges_ops );
930 if (!ranges) return NULL;
931 ranges->count = 0;
932 ranges->max = 8;
933 if (!(ranges->ranges = mem_alloc( ranges->max * sizeof(*ranges->ranges) )))
935 release_object( ranges );
936 return NULL;
938 return ranges;
941 static unsigned int get_mapping_flags( obj_handle_t handle, unsigned int flags )
943 switch (flags & (SEC_IMAGE | SEC_RESERVE | SEC_COMMIT | SEC_FILE))
945 case SEC_IMAGE:
946 if (flags & (SEC_WRITECOMBINE | SEC_LARGE_PAGES)) break;
947 if (handle) return SEC_FILE | SEC_IMAGE;
948 set_error( STATUS_INVALID_FILE_FOR_SECTION );
949 return 0;
950 case SEC_COMMIT:
951 if (!handle) return flags;
952 /* fall through */
953 case SEC_RESERVE:
954 if (flags & SEC_LARGE_PAGES) break;
955 if (handle) return SEC_FILE | (flags & (SEC_NOCACHE | SEC_WRITECOMBINE));
956 return flags;
958 set_error( STATUS_INVALID_PARAMETER );
959 return 0;
963 static struct mapping *create_mapping( struct object *root, const struct unicode_str *name,
964 unsigned int attr, mem_size_t size, unsigned int flags,
965 obj_handle_t handle, unsigned int file_access,
966 const struct security_descriptor *sd )
968 struct mapping *mapping;
969 struct file *file;
970 struct fd *fd;
971 int unix_fd;
972 struct stat st;
974 if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd )))
975 return NULL;
976 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
977 return mapping; /* Nothing else to do */
979 mapping->size = size;
980 mapping->fd = NULL;
981 mapping->shared = NULL;
982 mapping->committed = NULL;
984 if (!(mapping->flags = get_mapping_flags( handle, flags ))) goto error;
986 if (handle)
988 const unsigned int sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
989 unsigned int mapping_access = FILE_MAPPING_ACCESS;
991 if (!(file = get_file_obj( current->process, handle, file_access ))) goto error;
992 fd = get_obj_fd( (struct object *)file );
994 /* file sharing rules for mappings are different so we use magic the access rights */
995 if (flags & SEC_IMAGE) mapping_access |= FILE_MAPPING_IMAGE;
996 else if (file_access & FILE_WRITE_DATA) mapping_access |= FILE_MAPPING_WRITE;
998 if (!(mapping->fd = get_fd_object_for_mapping( fd, mapping_access, sharing )))
1000 mapping->fd = dup_fd_object( fd, mapping_access, sharing, FILE_SYNCHRONOUS_IO_NONALERT );
1001 if (mapping->fd) set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
1003 release_object( file );
1004 release_object( fd );
1005 if (!mapping->fd) goto error;
1007 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
1008 if (fstat( unix_fd, &st ) == -1)
1010 file_set_error();
1011 goto error;
1013 if (flags & SEC_IMAGE)
1015 unsigned int err = get_image_params( mapping, st.st_size, unix_fd );
1016 if (!err) return mapping;
1017 set_error( err );
1018 goto error;
1020 if (!mapping->size)
1022 if (!(mapping->size = st.st_size))
1024 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
1025 goto error;
1028 else if (st.st_size < mapping->size)
1030 if (!(file_access & FILE_WRITE_DATA) || mapping->size >> 54 /* ntfs limit */)
1032 set_error( STATUS_SECTION_TOO_BIG );
1033 goto error;
1035 if (!grow_file( unix_fd, mapping->size )) goto error;
1038 else /* Anonymous mapping (no associated file) */
1040 if (!mapping->size)
1042 set_error( STATUS_INVALID_PARAMETER );
1043 goto error;
1045 if ((flags & SEC_RESERVE) && !(mapping->committed = create_ranges())) goto error;
1046 mapping->size = (mapping->size + page_mask) & ~((mem_size_t)page_mask);
1047 if ((unix_fd = create_temp_file( mapping->size )) == -1) goto error;
1048 if (!(mapping->fd = create_anonymous_fd( &mapping_fd_ops, unix_fd, &mapping->obj,
1049 FILE_SYNCHRONOUS_IO_NONALERT ))) goto error;
1050 allow_fd_caching( mapping->fd );
1052 return mapping;
1054 error:
1055 release_object( mapping );
1056 return NULL;
1059 /* create a read-only file mapping for the specified fd */
1060 struct mapping *create_fd_mapping( struct object *root, const struct unicode_str *name,
1061 struct fd *fd, unsigned int attr, const struct security_descriptor *sd )
1063 struct mapping *mapping;
1064 int unix_fd;
1065 struct stat st;
1067 if (!(mapping = create_named_object( root, &mapping_ops, name, attr, sd ))) return NULL;
1068 if (get_error() == STATUS_OBJECT_NAME_EXISTS) return mapping; /* Nothing else to do */
1070 mapping->shared = NULL;
1071 mapping->committed = NULL;
1072 mapping->flags = SEC_FILE;
1073 mapping->fd = (struct fd *)grab_object( fd );
1074 set_fd_user( mapping->fd, &mapping_fd_ops, NULL );
1076 if ((unix_fd = get_unix_fd( mapping->fd )) == -1) goto error;
1077 if (fstat( unix_fd, &st ) == -1)
1079 file_set_error();
1080 goto error;
1082 if (!(mapping->size = st.st_size))
1084 set_error( STATUS_MAPPED_FILE_SIZE_ZERO );
1085 goto error;
1087 return mapping;
1089 error:
1090 release_object( mapping );
1091 return NULL;
1094 static struct mapping *get_mapping_obj( struct process *process, obj_handle_t handle, unsigned int access )
1096 return (struct mapping *)get_handle_obj( process, handle, access, &mapping_ops );
1099 /* open a new file for the file descriptor backing the view */
1100 struct file *get_view_file( const struct memory_view *view, unsigned int access, unsigned int sharing )
1102 if (!view->fd) return NULL;
1103 return create_file_for_fd_obj( view->fd, access, sharing );
1106 /* get the image info for a SEC_IMAGE mapped view */
1107 const pe_image_info_t *get_view_image_info( const struct memory_view *view, client_ptr_t *base )
1109 if (!(view->flags & SEC_IMAGE)) return NULL;
1110 *base = view->base;
1111 return &view->image;
1114 /* get the file name for a mapped view */
1115 int get_view_nt_name( const struct memory_view *view, struct unicode_str *name )
1117 if (view->namelen) /* .so builtin */
1119 name->str = view->name;
1120 name->len = view->namelen;
1121 return 1;
1123 if (!view->fd) return 0;
1124 get_nt_name( view->fd, name );
1125 return 1;
1128 /* generate all startup events of a given process */
1129 void generate_startup_debug_events( struct process *process )
1131 struct memory_view *view;
1132 struct list *ptr = list_head( &process->views );
1133 struct thread *thread, *first_thread = get_process_first_thread( process );
1135 if (!ptr) return;
1136 view = LIST_ENTRY( ptr, struct memory_view, entry );
1137 generate_debug_event( first_thread, DbgCreateProcessStateChange, view );
1139 /* generate ntdll.dll load event */
1140 while (ptr && (ptr = list_next( &process->views, ptr )))
1142 view = LIST_ENTRY( ptr, struct memory_view, entry );
1143 if (generate_dll_event( first_thread, DbgLoadDllStateChange, view )) break;
1146 /* generate creation events */
1147 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
1149 if (thread != first_thread)
1150 generate_debug_event( thread, DbgCreateThreadStateChange, NULL );
1153 /* generate dll events (in loading order) */
1154 while (ptr && (ptr = list_next( &process->views, ptr )))
1156 view = LIST_ENTRY( ptr, struct memory_view, entry );
1157 generate_dll_event( first_thread, DbgLoadDllStateChange, view );
1161 static void mapping_dump( struct object *obj, int verbose )
1163 struct mapping *mapping = (struct mapping *)obj;
1164 assert( obj->ops == &mapping_ops );
1165 fprintf( stderr, "Mapping size=%08x%08x flags=%08x fd=%p shared=%p\n",
1166 (unsigned int)(mapping->size >> 32), (unsigned int)mapping->size,
1167 mapping->flags, mapping->fd, mapping->shared );
1170 static struct fd *mapping_get_fd( struct object *obj )
1172 struct mapping *mapping = (struct mapping *)obj;
1173 return (struct fd *)grab_object( mapping->fd );
1176 static void mapping_destroy( struct object *obj )
1178 struct mapping *mapping = (struct mapping *)obj;
1179 assert( obj->ops == &mapping_ops );
1180 if (mapping->fd) release_object( mapping->fd );
1181 if (mapping->committed) release_object( mapping->committed );
1182 if (mapping->shared) release_object( mapping->shared );
1185 static enum server_fd_type mapping_get_fd_type( struct fd *fd )
1187 return FD_TYPE_FILE;
1190 /* assign a mapping address to a PE image mapping */
1191 static client_ptr_t assign_map_address( struct mapping *mapping )
1193 unsigned int i;
1194 client_ptr_t ret;
1195 struct addr_range *range = (mapping->image.base >> 32) ? &ranges64 : &ranges32;
1196 mem_size_t size = (mapping->size + granularity_mask) & ~granularity_mask;
1198 if (!(mapping->image.image_charact & IMAGE_FILE_DLL)) return 0;
1200 if ((ret = get_fd_map_address( mapping->fd ))) return ret;
1202 size += granularity_mask + 1; /* leave some free space between mappings */
1204 for (i = 0; i < range->count; i++)
1206 if (range->free[i].size < size) continue;
1207 range->free[i].size -= size;
1208 ret = range->free[i].base + range->free[i].size;
1209 set_fd_map_address( mapping->fd, ret, size );
1210 return ret;
1212 return 0;
1215 /* free a PE mapping address range when the last mapping is closed */
1216 void free_map_addr( client_ptr_t base, mem_size_t size )
1218 unsigned int i;
1219 client_ptr_t end = base + size;
1220 struct addr_range *range = (base >> 32) ? &ranges64 : &ranges32;
1222 for (i = 0; i < range->count; i++)
1224 if (range->free[i].base > end) continue;
1225 if (range->free[i].base + range->free[i].size < base) break;
1226 if (range->free[i].base == end)
1228 if (i + 1 < range->count && range->free[i + 1].base + range->free[i + 1].size == base)
1230 size += range->free[i].size;
1231 range->count--;
1232 memmove( &range->free[i], &range->free[i + 1], (range->count - i) * sizeof(*range->free) );
1234 else range->free[i].base = base;
1236 range->free[i].size += size;
1237 return;
1240 if (range->count == range->size)
1242 unsigned int new_size = max( 256, range->size * 2 );
1243 void *new_free = realloc( range->free, new_size * sizeof(*range->free) );
1244 if (!new_free) return;
1245 range->size = new_size;
1246 range->free = new_free;
1248 memmove( &range->free[i + 1], &range->free[i], (range->count - i) * sizeof(*range->free) );
1249 range->free[i].base = base;
1250 range->free[i].size = size;
1251 range->count++;
1254 int get_page_size(void)
1256 return page_mask + 1;
1259 struct object *create_user_data_mapping( struct object *root, const struct unicode_str *name,
1260 unsigned int attr, const struct security_descriptor *sd )
1262 void *ptr;
1263 struct mapping *mapping;
1265 if (!(mapping = create_mapping( root, name, attr, sizeof(KSHARED_USER_DATA),
1266 SEC_COMMIT, 0, FILE_READ_DATA | FILE_WRITE_DATA, sd ))) return NULL;
1267 ptr = mmap( NULL, mapping->size, PROT_WRITE, MAP_SHARED, get_unix_fd( mapping->fd ), 0 );
1268 if (ptr != MAP_FAILED)
1270 user_shared_data = ptr;
1271 user_shared_data->SystemCall = 1;
1273 return &mapping->obj;
1276 /* create a file mapping */
1277 DECL_HANDLER(create_mapping)
1279 struct object *root;
1280 struct mapping *mapping;
1281 struct unicode_str name;
1282 const struct security_descriptor *sd;
1283 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1285 if (!objattr) return;
1287 if ((mapping = create_mapping( root, &name, objattr->attributes, req->size, req->flags,
1288 req->file_handle, req->file_access, sd )))
1290 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
1291 reply->handle = alloc_handle( current->process, &mapping->obj, req->access, objattr->attributes );
1292 else
1293 reply->handle = alloc_handle_no_access_check( current->process, &mapping->obj,
1294 req->access, objattr->attributes );
1295 release_object( mapping );
1298 if (root) release_object( root );
1301 /* open a handle to a mapping */
1302 DECL_HANDLER(open_mapping)
1304 struct unicode_str name = get_req_unicode_str();
1306 reply->handle = open_object( current->process, req->rootdir, req->access,
1307 &mapping_ops, &name, req->attributes );
1310 /* get a mapping information */
1311 DECL_HANDLER(get_mapping_info)
1313 struct mapping *mapping;
1315 if (!(mapping = get_mapping_obj( current->process, req->handle, req->access ))) return;
1317 reply->size = mapping->size;
1318 reply->flags = mapping->flags;
1320 if (mapping->flags & SEC_IMAGE)
1322 struct unicode_str name = { NULL, 0 };
1323 data_size_t size;
1324 void *data;
1326 if (mapping->fd) get_nt_name( mapping->fd, &name );
1327 size = min( sizeof(pe_image_info_t) + name.len, get_reply_max_size() );
1328 if ((data = set_reply_data_size( size )))
1330 memcpy( data, &mapping->image, min( sizeof(pe_image_info_t), size ));
1331 if (size > sizeof(pe_image_info_t))
1332 memcpy( (pe_image_info_t *)data + 1, name.str, size - sizeof(pe_image_info_t) );
1334 reply->total = sizeof(pe_image_info_t) + name.len;
1337 if (!(req->access & (SECTION_MAP_READ | SECTION_MAP_WRITE))) /* query only */
1339 release_object( mapping );
1340 return;
1343 if (mapping->shared)
1344 reply->shared_file = alloc_handle( current->process, mapping->shared->file,
1345 GENERIC_READ|GENERIC_WRITE, 0 );
1346 release_object( mapping );
1349 /* get the address to use to map an image mapping */
1350 DECL_HANDLER(get_image_map_address)
1352 struct mapping *mapping;
1354 if (!(mapping = get_mapping_obj( current->process, req->handle, SECTION_MAP_READ ))) return;
1356 if ((mapping->flags & SEC_IMAGE) &&
1357 (mapping->image.image_flags & IMAGE_FLAGS_ImageDynamicallyRelocated))
1359 if (!mapping->image.map_addr) mapping->image.map_addr = assign_map_address( mapping );
1360 reply->addr = mapping->image.map_addr;
1362 else set_error( STATUS_INVALID_PARAMETER );
1364 release_object( mapping );
1367 /* add a memory view in the current process */
1368 DECL_HANDLER(map_view)
1370 struct mapping *mapping;
1371 struct memory_view *view;
1373 if (!is_valid_view_addr( current->process, req->base, req->size ))
1375 set_error( STATUS_INVALID_PARAMETER );
1376 return;
1379 if (!(mapping = get_mapping_obj( current->process, req->mapping, req->access ))) return;
1381 if ((mapping->flags & SEC_IMAGE) ||
1382 req->start >= mapping->size ||
1383 req->start + req->size < req->start ||
1384 req->start + req->size > ((mapping->size + page_mask) & ~(mem_size_t)page_mask))
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->start = req->start;
1395 view->flags = mapping->flags;
1396 view->namelen = 0;
1397 view->fd = !is_fd_removable( mapping->fd ) ? (struct fd *)grab_object( mapping->fd ) : NULL;
1398 view->committed = mapping->committed ? (struct ranges *)grab_object( mapping->committed ) : NULL;
1399 view->shared = NULL;
1400 add_process_view( current, view );
1403 done:
1404 release_object( mapping );
1407 /* add a memory view for an image mapping in the current process */
1408 DECL_HANDLER(map_image_view)
1410 struct mapping *mapping;
1411 struct memory_view *view;
1413 if (!is_valid_view_addr( current->process, req->base, req->size ))
1415 set_error( STATUS_INVALID_PARAMETER );
1416 return;
1419 if (!(mapping = get_mapping_obj( current->process, req->mapping, SECTION_MAP_READ ))) return;
1421 if (!(mapping->flags & SEC_IMAGE) || req->size > mapping->image.map_size)
1423 set_error( STATUS_INVALID_PARAMETER );
1424 goto done;
1427 if ((view = mem_alloc( sizeof(*view) )))
1429 view->base = req->base;
1430 view->size = req->size;
1431 view->flags = mapping->flags;
1432 view->start = 0;
1433 view->namelen = 0;
1434 view->fd = !is_fd_removable( mapping->fd ) ? (struct fd *)grab_object( mapping->fd ) : NULL;
1435 view->committed = NULL;
1436 view->shared = mapping->shared ? (struct shared_map *)grab_object( mapping->shared ) : NULL;
1437 view->image = mapping->image;
1438 if (add_process_view( current, view ))
1440 current->entry_point = view->base + req->entry;
1441 current->process->machine = (view->image.image_flags & IMAGE_FLAGS_ComPlusNativeReady) ?
1442 native_machine : req->machine;
1445 if (view->base != (mapping->image.map_addr ? mapping->image.map_addr : mapping->image.base))
1446 set_error( STATUS_IMAGE_NOT_AT_BASE );
1447 if (req->machine != current->process->machine)
1449 /* on 32-bit, the native 64-bit machine is allowed */
1450 if (is_machine_64bit( current->process->machine ) || req->machine != native_machine)
1451 set_error( STATUS_IMAGE_MACHINE_TYPE_MISMATCH );
1455 done:
1456 release_object( mapping );
1459 /* add a memory view for a builtin dll in the current process */
1460 DECL_HANDLER(map_builtin_view)
1462 struct memory_view *view;
1463 const pe_image_info_t *image = get_req_data();
1464 data_size_t namelen = get_req_data_size() - sizeof(*image);
1466 if (get_req_data_size() < sizeof(*image) ||
1467 (namelen & (sizeof(WCHAR) - 1)) ||
1468 !is_valid_view_addr( current->process, image->base, image->map_size ))
1470 set_error( STATUS_INVALID_PARAMETER );
1471 return;
1474 if ((view = mem_alloc( sizeof(struct memory_view) + namelen )))
1476 memset( view, 0, sizeof(*view) );
1477 view->base = image->base;
1478 view->size = image->map_size;
1479 view->flags = SEC_IMAGE;
1480 view->image = *image;
1481 view->namelen = namelen;
1482 memcpy( view->name, image + 1, namelen );
1483 if (add_process_view( current, view ))
1485 current->entry_point = view->base + image->entry_point;
1486 current->process->machine = image->machine;
1491 /* unmap a memory view from the current process */
1492 DECL_HANDLER(unmap_view)
1494 struct memory_view *view = find_mapped_view( current->process, req->base );
1496 if (!view) return;
1497 generate_dll_event( current, DbgUnloadDllStateChange, view );
1498 free_memory_view( view );
1501 /* get information about a mapped image view */
1502 DECL_HANDLER(get_image_view_info)
1504 struct process *process;
1505 struct memory_view *view;
1507 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION ))) return;
1509 if ((view = find_mapped_addr( process, req->addr )) && (view->flags & SEC_IMAGE))
1511 reply->base = view->base;
1512 reply->size = view->size;
1515 release_object( process );
1518 /* get a range of committed pages in a file mapping */
1519 DECL_HANDLER(get_mapping_committed_range)
1521 struct memory_view *view = find_mapped_view( current->process, req->base );
1523 if (view) reply->committed = find_committed_range( view, req->offset, &reply->size );
1526 /* add a range to the committed pages in a file mapping */
1527 DECL_HANDLER(add_mapping_committed_range)
1529 struct memory_view *view = find_mapped_view( current->process, req->base );
1531 if (view) add_committed_range( view, req->offset, req->offset + req->size );
1534 /* check if two memory maps are for the same file */
1535 DECL_HANDLER(is_same_mapping)
1537 struct memory_view *view1 = find_mapped_view( current->process, req->base1 );
1538 struct memory_view *view2 = find_mapped_view( current->process, req->base2 );
1540 if (!view1 || !view2) return;
1541 if (!view1->fd || !view2->fd || !(view1->flags & SEC_IMAGE) || !is_same_file_fd( view1->fd, view2->fd ))
1542 set_error( STATUS_NOT_SAME_DEVICE );
1545 /* get the filename of a mapping */
1546 DECL_HANDLER(get_mapping_filename)
1548 struct process *process;
1549 struct memory_view *view;
1550 struct unicode_str name;
1552 if (!(process = get_process_from_handle( req->process, PROCESS_QUERY_INFORMATION ))) return;
1554 if ((view = find_mapped_addr( process, req->addr )) && get_view_nt_name( view, &name ))
1556 reply->len = name.len;
1557 if (name.len > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
1558 else if (!name.len) set_error( STATUS_FILE_INVALID );
1559 else set_reply_data( name.str, name.len );
1561 else set_error( STATUS_INVALID_ADDRESS );
1563 release_object( process );