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
22 #include "wine/port.h"
29 #ifdef HAVE_SYS_MMAN_H
30 # include <sys/mman.h>
35 #define WIN32_NO_STATUS
46 /* list of memory ranges, used to store committed info */
49 struct object obj
; /* object header */
50 unsigned int count
; /* number of used ranges */
51 unsigned int max
; /* number of allocated 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 ranges_dump
, /* dump */
66 no_get_type
, /* get_type */
67 no_add_queue
, /* add_queue */
68 NULL
, /* remove_queue */
71 no_signal
, /* signal */
72 no_get_fd
, /* get_fd */
73 no_map_access
, /* map_access */
74 default_get_sd
, /* get_sd */
75 default_set_sd
, /* set_sd */
76 no_lookup_name
, /* lookup_name */
77 no_link_name
, /* link_name */
78 NULL
, /* unlink_name */
79 no_open_file
, /* open_file */
80 no_close_handle
, /* close_handle */
81 ranges_destroy
/* destroy */
84 /* file backing the shared sections of a PE image mapping */
87 struct object obj
; /* object header */
88 struct fd
*fd
; /* file descriptor of the mapped PE file */
89 struct file
*file
; /* temp file holding the shared data */
90 struct list entry
; /* entry in global shared maps list */
93 static void shared_map_dump( struct object
*obj
, int verbose
);
94 static void shared_map_destroy( struct object
*obj
);
96 static const struct object_ops shared_map_ops
=
98 sizeof(struct shared_map
), /* size */
99 shared_map_dump
, /* dump */
100 no_get_type
, /* get_type */
101 no_add_queue
, /* add_queue */
102 NULL
, /* remove_queue */
104 NULL
, /* satisfied */
105 no_signal
, /* signal */
106 no_get_fd
, /* get_fd */
107 no_map_access
, /* map_access */
108 default_get_sd
, /* get_sd */
109 default_set_sd
, /* set_sd */
110 no_lookup_name
, /* lookup_name */
111 no_link_name
, /* link_name */
112 NULL
, /* unlink_name */
113 no_open_file
, /* open_file */
114 no_close_handle
, /* close_handle */
115 shared_map_destroy
/* destroy */
118 static struct list shared_map_list
= LIST_INIT( shared_map_list
);
120 /* memory view mapped in client address space */
123 struct list entry
; /* entry in per-process view list */
124 struct fd
*fd
; /* fd for mapped file */
125 struct ranges
*committed
; /* list of committed ranges in this mapping */
126 struct shared_map
*shared
; /* temp file for shared PE mapping */
127 unsigned int flags
; /* SEC_* flags */
128 client_ptr_t base
; /* view base address (in process addr space) */
129 mem_size_t size
; /* view size */
130 file_pos_t start
; /* start offset in mapping */
135 struct object obj
; /* object header */
136 mem_size_t size
; /* mapping size */
137 unsigned int flags
; /* SEC_* flags */
138 struct fd
*fd
; /* fd for mapped file */
139 pe_image_info_t image
; /* image info (for PE image mapping) */
140 struct ranges
*committed
; /* list of committed ranges in this mapping */
141 struct shared_map
*shared
; /* temp file for shared PE mapping */
144 static void mapping_dump( struct object
*obj
, int verbose
);
145 static struct object_type
*mapping_get_type( struct object
*obj
);
146 static struct fd
*mapping_get_fd( struct object
*obj
);
147 static unsigned int mapping_map_access( struct object
*obj
, unsigned int access
);
148 static void mapping_destroy( struct object
*obj
);
149 static enum server_fd_type
mapping_get_fd_type( struct fd
*fd
);
151 static const struct object_ops mapping_ops
=
153 sizeof(struct mapping
), /* size */
154 mapping_dump
, /* dump */
155 mapping_get_type
, /* get_type */
156 no_add_queue
, /* add_queue */
157 NULL
, /* remove_queue */
159 NULL
, /* satisfied */
160 no_signal
, /* signal */
161 mapping_get_fd
, /* get_fd */
162 mapping_map_access
, /* map_access */
163 default_get_sd
, /* get_sd */
164 default_set_sd
, /* set_sd */
165 no_lookup_name
, /* lookup_name */
166 directory_link_name
, /* link_name */
167 default_unlink_name
, /* unlink_name */
168 no_open_file
, /* open_file */
169 fd_close_handle
, /* close_handle */
170 mapping_destroy
/* destroy */
173 static const struct fd_ops mapping_fd_ops
=
175 default_fd_get_poll_events
, /* get_poll_events */
176 default_poll_event
, /* poll_event */
177 mapping_get_fd_type
, /* get_fd_type */
178 no_fd_read
, /* read */
179 no_fd_write
, /* write */
180 no_fd_flush
, /* flush */
181 no_fd_get_file_info
, /* get_file_info */
182 no_fd_get_volume_info
, /* get_volume_info */
183 no_fd_ioctl
, /* ioctl */
184 no_fd_queue_async
, /* queue_async */
185 default_fd_reselect_async
/* reselect_async */
188 static size_t page_mask
;
190 #define ROUND_SIZE(size) (((size) + page_mask) & ~page_mask)
193 static void ranges_dump( struct object
*obj
, int verbose
)
195 struct ranges
*ranges
= (struct ranges
*)obj
;
196 fprintf( stderr
, "Memory ranges count=%u\n", ranges
->count
);
199 static void ranges_destroy( struct object
*obj
)
201 struct ranges
*ranges
= (struct ranges
*)obj
;
202 free( ranges
->ranges
);
205 static void shared_map_dump( struct object
*obj
, int verbose
)
207 struct shared_map
*shared
= (struct shared_map
*)obj
;
208 fprintf( stderr
, "Shared mapping fd=%p file=%p\n", shared
->fd
, shared
->file
);
211 static void shared_map_destroy( struct object
*obj
)
213 struct shared_map
*shared
= (struct shared_map
*)obj
;
215 release_object( shared
->fd
);
216 release_object( shared
->file
);
217 list_remove( &shared
->entry
);
220 /* extend a file beyond the current end of file */
221 static int grow_file( int unix_fd
, file_pos_t new_size
)
223 static const char zero
;
224 off_t size
= new_size
;
226 if (sizeof(new_size
) > sizeof(size
) && size
!= new_size
)
228 set_error( STATUS_INVALID_PARAMETER
);
231 /* extend the file one byte beyond the requested size and then truncate it */
232 /* this should work around ftruncate implementations that can't extend files */
233 if (pwrite( unix_fd
, &zero
, 1, size
) != -1)
235 ftruncate( unix_fd
, size
);
242 /* check if the current directory allows exec mappings */
243 static int check_current_dir_for_exec(void)
246 char tmpfn
[] = "anonmap.XXXXXX";
247 void *ret
= MAP_FAILED
;
249 fd
= mkstemps( tmpfn
, 0 );
250 if (fd
== -1) return 0;
251 if (grow_file( fd
, 1 ))
253 ret
= mmap( NULL
, get_page_size(), PROT_READ
| PROT_EXEC
, MAP_PRIVATE
, fd
, 0 );
254 if (ret
!= MAP_FAILED
) munmap( ret
, get_page_size() );
258 return (ret
!= MAP_FAILED
);
261 /* create a temp file for anonymous mappings */
262 static int create_temp_file( file_pos_t size
)
264 static int temp_dir_fd
= -1;
265 char tmpfn
[] = "anonmap.XXXXXX";
268 if (temp_dir_fd
== -1)
270 temp_dir_fd
= server_dir_fd
;
271 if (!check_current_dir_for_exec())
273 /* the server dir is noexec, try the config dir instead */
274 fchdir( config_dir_fd
);
275 if (check_current_dir_for_exec())
276 temp_dir_fd
= config_dir_fd
;
277 else /* neither works, fall back to server dir */
278 fchdir( server_dir_fd
);
281 else if (temp_dir_fd
!= server_dir_fd
) fchdir( temp_dir_fd
);
283 fd
= mkstemps( tmpfn
, 0 );
286 if (!grow_file( fd
, size
))
293 else file_set_error();
295 if (temp_dir_fd
!= server_dir_fd
) fchdir( server_dir_fd
);
299 /* find a memory view from its base address */
300 static struct memory_view
*find_mapped_view( struct process
*process
, client_ptr_t base
)
302 struct memory_view
*view
;
304 LIST_FOR_EACH_ENTRY( view
, &process
->views
, struct memory_view
, entry
)
305 if (view
->base
== base
) return view
;
307 set_error( STATUS_NOT_MAPPED_VIEW
);
311 static void free_memory_view( struct memory_view
*view
)
313 if (view
->fd
) release_object( view
->fd
);
314 if (view
->committed
) release_object( view
->committed
);
315 if (view
->shared
) release_object( view
->shared
);
316 list_remove( &view
->entry
);
320 /* free all mapped views at process exit */
321 void free_mapped_views( struct process
*process
)
325 while ((ptr
= list_head( &process
->views
)))
326 free_memory_view( LIST_ENTRY( ptr
, struct memory_view
, entry
));
329 /* find the shared PE mapping for a given mapping */
330 static struct shared_map
*get_shared_file( struct fd
*fd
)
332 struct shared_map
*ptr
;
334 LIST_FOR_EACH_ENTRY( ptr
, &shared_map_list
, struct shared_map
, entry
)
335 if (is_same_file_fd( ptr
->fd
, fd
))
336 return (struct shared_map
*)grab_object( ptr
);
340 /* return the size of the memory mapping and file range of a given section */
341 static inline void get_section_sizes( const IMAGE_SECTION_HEADER
*sec
, size_t *map_size
,
342 off_t
*file_start
, size_t *file_size
)
344 static const unsigned int sector_align
= 0x1ff;
346 if (!sec
->Misc
.VirtualSize
) *map_size
= ROUND_SIZE( sec
->SizeOfRawData
);
347 else *map_size
= ROUND_SIZE( sec
->Misc
.VirtualSize
);
349 *file_start
= sec
->PointerToRawData
& ~sector_align
;
350 *file_size
= (sec
->SizeOfRawData
+ (sec
->PointerToRawData
& sector_align
) + sector_align
) & ~sector_align
;
351 if (*file_size
> *map_size
) *file_size
= *map_size
;
354 /* add a range to the committed list */
355 static void add_committed_range( struct memory_view
*view
, file_pos_t start
, file_pos_t end
)
358 struct ranges
*committed
= view
->committed
;
359 struct range
*ranges
;
361 if ((start
& page_mask
) || (end
& page_mask
) ||
362 start
>= view
->size
|| end
>= view
->size
||
365 set_error( STATUS_INVALID_PARAMETER
);
369 if (!committed
) return; /* everything committed already */
371 start
+= view
->start
;
374 for (i
= 0, ranges
= committed
->ranges
; i
< committed
->count
; i
++)
376 if (ranges
[i
].start
> end
) break;
377 if (ranges
[i
].end
< start
) continue;
378 if (ranges
[i
].start
> start
) ranges
[i
].start
= start
; /* extend downwards */
379 if (ranges
[i
].end
< end
) /* extend upwards and maybe merge with next */
381 for (j
= i
+ 1; j
< committed
->count
; j
++)
383 if (ranges
[j
].start
> end
) break;
384 if (ranges
[j
].end
> end
) end
= ranges
[j
].end
;
388 memmove( &ranges
[i
+ 1], &ranges
[j
], (committed
->count
- j
) * sizeof(*ranges
) );
389 committed
->count
-= j
- (i
+ 1);
396 /* now add a new range */
398 if (committed
->count
== committed
->max
)
400 unsigned int new_size
= committed
->max
* 2;
401 struct range
*new_ptr
= realloc( committed
->ranges
, new_size
* sizeof(*new_ptr
) );
402 if (!new_ptr
) return;
403 committed
->max
= new_size
;
404 committed
->ranges
= new_ptr
;
406 memmove( &ranges
[i
+ 1], &ranges
[i
], (committed
->count
- i
) * sizeof(*ranges
) );
407 ranges
[i
].start
= start
;
412 /* find the range containing start and return whether it's committed */
413 static int find_committed_range( struct memory_view
*view
, file_pos_t start
, mem_size_t
*size
)
416 struct ranges
*committed
= view
->committed
;
417 struct range
*ranges
;
419 if ((start
& page_mask
) || start
>= view
->size
)
421 set_error( STATUS_INVALID_PARAMETER
);
424 if (!committed
) /* everything is committed */
426 *size
= view
->size
- start
;
429 for (i
= 0, ranges
= committed
->ranges
; i
< committed
->count
; i
++)
431 if (ranges
[i
].start
> view
->start
+ start
)
433 *size
= min( ranges
[i
].start
, view
->start
+ view
->size
) - (view
->start
+ start
);
436 if (ranges
[i
].end
> view
->start
+ start
)
438 *size
= min( ranges
[i
].end
, view
->start
+ view
->size
) - (view
->start
+ start
);
442 *size
= view
->size
- start
;
446 /* allocate and fill the temp file for a shared PE image mapping */
447 static int build_shared_mapping( struct mapping
*mapping
, int fd
,
448 IMAGE_SECTION_HEADER
*sec
, unsigned int nb_sec
)
450 struct shared_map
*shared
;
453 mem_size_t total_size
;
454 size_t file_size
, map_size
, max_size
;
455 off_t shared_pos
, read_pos
, write_pos
;
460 /* compute the total size of the shared mapping */
462 total_size
= max_size
= 0;
463 for (i
= 0; i
< nb_sec
; i
++)
465 if ((sec
[i
].Characteristics
& IMAGE_SCN_MEM_SHARED
) &&
466 (sec
[i
].Characteristics
& IMAGE_SCN_MEM_WRITE
))
468 get_section_sizes( &sec
[i
], &map_size
, &read_pos
, &file_size
);
469 if (file_size
> max_size
) max_size
= file_size
;
470 total_size
+= map_size
;
473 if (!total_size
) return 1; /* nothing to do */
475 if ((mapping
->shared
= get_shared_file( mapping
->fd
))) return 1;
477 /* create a temp file for the mapping */
479 if ((shared_fd
= create_temp_file( total_size
)) == -1) return 0;
480 if (!(file
= create_file_for_fd( shared_fd
, FILE_GENERIC_READ
|FILE_GENERIC_WRITE
, 0 ))) return 0;
482 if (!(buffer
= malloc( max_size
))) goto error
;
484 /* copy the shared sections data into the temp file */
487 for (i
= 0; i
< nb_sec
; i
++)
489 if (!(sec
[i
].Characteristics
& IMAGE_SCN_MEM_SHARED
)) continue;
490 if (!(sec
[i
].Characteristics
& IMAGE_SCN_MEM_WRITE
)) continue;
491 get_section_sizes( &sec
[i
], &map_size
, &read_pos
, &file_size
);
492 write_pos
= shared_pos
;
493 shared_pos
+= map_size
;
494 if (!sec
[i
].PointerToRawData
|| !file_size
) continue;
498 long res
= pread( fd
, buffer
+ file_size
- toread
, toread
, read_pos
);
499 if (!res
&& toread
< 0x200) /* partial sector at EOF is not an error */
504 if (res
<= 0) goto error
;
508 if (pwrite( shared_fd
, buffer
, file_size
, write_pos
) != file_size
) goto error
;
511 if (!(shared
= alloc_object( &shared_map_ops
))) goto error
;
512 shared
->fd
= (struct fd
*)grab_object( mapping
->fd
);
514 list_add_head( &shared_map_list
, &shared
->entry
);
515 mapping
->shared
= shared
;
520 release_object( file
);
525 /* load the CLR header from its section */
526 static int load_clr_header( IMAGE_COR20_HEADER
*hdr
, size_t va
, size_t size
, int unix_fd
,
527 IMAGE_SECTION_HEADER
*sec
, unsigned int nb_sec
)
530 size_t map_size
, file_size
;
534 if (!va
|| !size
) return 0;
536 for (i
= 0; i
< nb_sec
; i
++)
538 if (va
< sec
[i
].VirtualAddress
) continue;
539 if (sec
[i
].Misc
.VirtualSize
&& va
- sec
[i
].VirtualAddress
>= sec
[i
].Misc
.VirtualSize
) continue;
540 get_section_sizes( &sec
[i
], &map_size
, &file_start
, &file_size
);
541 if (size
>= map_size
) continue;
542 if (va
- sec
[i
].VirtualAddress
>= map_size
- size
) continue;
543 file_size
= min( file_size
, map_size
);
544 size
= min( size
, sizeof(*hdr
) );
545 ret
= pread( unix_fd
, hdr
, min( size
, file_size
), file_start
+ va
- sec
[i
].VirtualAddress
);
547 if (ret
< sizeof(*hdr
)) memset( (char *)hdr
+ ret
, 0, sizeof(*hdr
) - ret
);
548 return (hdr
->MajorRuntimeVersion
> COR_VERSION_MAJOR_V2
||
549 (hdr
->MajorRuntimeVersion
== COR_VERSION_MAJOR_V2
&&
550 hdr
->MinorRuntimeVersion
>= COR_VERSION_MINOR
));
555 /* retrieve the mapping parameters for an executable (PE) image */
556 static unsigned int get_image_params( struct mapping
*mapping
, file_pos_t file_size
, int unix_fd
)
558 static const char fakedll_signature
[] = "Wine placeholder DLL";
560 IMAGE_COR20_HEADER clr
;
561 IMAGE_SECTION_HEADER sec
[96];
564 IMAGE_DOS_HEADER dos
;
565 char buffer
[sizeof(fakedll_signature
)];
570 IMAGE_FILE_HEADER FileHeader
;
573 IMAGE_OPTIONAL_HEADER32 hdr32
;
574 IMAGE_OPTIONAL_HEADER64 hdr64
;
579 size_t mz_size
, clr_va
, clr_size
;
580 unsigned int i
, cpu_mask
= get_supported_cpu_mask();
582 /* load the headers */
584 if (!file_size
) return STATUS_INVALID_FILE_FOR_SECTION
;
585 size
= pread( unix_fd
, &mz
, sizeof(mz
), 0 );
586 if (size
< sizeof(mz
.dos
)) return STATUS_INVALID_IMAGE_NOT_MZ
;
587 if (mz
.dos
.e_magic
!= IMAGE_DOS_SIGNATURE
) return STATUS_INVALID_IMAGE_NOT_MZ
;
589 pos
= mz
.dos
.e_lfanew
;
591 size
= pread( unix_fd
, &nt
, sizeof(nt
), pos
);
592 if (size
< sizeof(nt
.Signature
) + sizeof(nt
.FileHeader
)) return STATUS_INVALID_IMAGE_FORMAT
;
593 /* zero out Optional header in the case it's not present or partial */
594 size
= min( size
, sizeof(nt
.Signature
) + sizeof(nt
.FileHeader
) + nt
.FileHeader
.SizeOfOptionalHeader
);
595 if (size
< sizeof(nt
)) memset( (char *)&nt
+ size
, 0, sizeof(nt
) - size
);
596 if (nt
.Signature
!= IMAGE_NT_SIGNATURE
)
598 IMAGE_OS2_HEADER
*os2
= (IMAGE_OS2_HEADER
*)&nt
;
599 if (os2
->ne_magic
!= IMAGE_OS2_SIGNATURE
) return STATUS_INVALID_IMAGE_PROTECT
;
600 if (os2
->ne_exetyp
== 2) return STATUS_INVALID_IMAGE_WIN_16
;
601 if (os2
->ne_exetyp
== 5) return STATUS_INVALID_IMAGE_PROTECT
;
602 return STATUS_INVALID_IMAGE_NE_FORMAT
;
605 switch (nt
.opt
.hdr32
.Magic
)
607 case IMAGE_NT_OPTIONAL_HDR32_MAGIC
:
608 switch (nt
.FileHeader
.Machine
)
610 case IMAGE_FILE_MACHINE_I386
:
611 mapping
->image
.cpu
= CPU_x86
;
612 if (cpu_mask
& (CPU_FLAG(CPU_x86
) | CPU_FLAG(CPU_x86_64
))) break;
613 return STATUS_INVALID_IMAGE_FORMAT
;
614 case IMAGE_FILE_MACHINE_ARM
:
615 case IMAGE_FILE_MACHINE_THUMB
:
616 case IMAGE_FILE_MACHINE_ARMNT
:
617 mapping
->image
.cpu
= CPU_ARM
;
618 if (cpu_mask
& (CPU_FLAG(CPU_ARM
) | CPU_FLAG(CPU_ARM64
))) break;
619 return STATUS_INVALID_IMAGE_FORMAT
;
620 case IMAGE_FILE_MACHINE_POWERPC
:
621 mapping
->image
.cpu
= CPU_POWERPC
;
622 if (cpu_mask
& CPU_FLAG(CPU_POWERPC
)) break;
623 return STATUS_INVALID_IMAGE_FORMAT
;
625 return STATUS_INVALID_IMAGE_FORMAT
;
627 clr_va
= nt
.opt
.hdr32
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
;
628 clr_size
= nt
.opt
.hdr32
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].Size
;
630 mapping
->image
.base
= nt
.opt
.hdr32
.ImageBase
;
631 mapping
->image
.entry_point
= nt
.opt
.hdr32
.ImageBase
+ nt
.opt
.hdr32
.AddressOfEntryPoint
;
632 mapping
->image
.map_size
= ROUND_SIZE( nt
.opt
.hdr32
.SizeOfImage
);
633 mapping
->image
.stack_size
= nt
.opt
.hdr32
.SizeOfStackReserve
;
634 mapping
->image
.stack_commit
= nt
.opt
.hdr32
.SizeOfStackCommit
;
635 mapping
->image
.subsystem
= nt
.opt
.hdr32
.Subsystem
;
636 mapping
->image
.subsystem_low
= nt
.opt
.hdr32
.MinorSubsystemVersion
;
637 mapping
->image
.subsystem_high
= nt
.opt
.hdr32
.MajorSubsystemVersion
;
638 mapping
->image
.dll_charact
= nt
.opt
.hdr32
.DllCharacteristics
;
639 mapping
->image
.contains_code
= (nt
.opt
.hdr32
.SizeOfCode
||
640 nt
.opt
.hdr32
.AddressOfEntryPoint
||
641 nt
.opt
.hdr32
.SectionAlignment
& page_mask
);
642 mapping
->image
.header_size
= nt
.opt
.hdr32
.SizeOfHeaders
;
643 mapping
->image
.checksum
= nt
.opt
.hdr32
.CheckSum
;
644 mapping
->image
.image_flags
= 0;
645 if (nt
.opt
.hdr32
.SectionAlignment
& page_mask
)
646 mapping
->image
.image_flags
|= IMAGE_FLAGS_ImageMappedFlat
;
647 if ((nt
.opt
.hdr32
.DllCharacteristics
& IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE
) &&
648 mapping
->image
.contains_code
&& !(clr_va
&& clr_size
))
649 mapping
->image
.image_flags
|= IMAGE_FLAGS_ImageDynamicallyRelocated
;
652 case IMAGE_NT_OPTIONAL_HDR64_MAGIC
:
653 if (!(cpu_mask
& CPU_64BIT_MASK
)) return STATUS_INVALID_IMAGE_WIN_64
;
654 switch (nt
.FileHeader
.Machine
)
656 case IMAGE_FILE_MACHINE_AMD64
:
657 mapping
->image
.cpu
= CPU_x86_64
;
658 if (cpu_mask
& (CPU_FLAG(CPU_x86
) | CPU_FLAG(CPU_x86_64
))) break;
659 return STATUS_INVALID_IMAGE_FORMAT
;
660 case IMAGE_FILE_MACHINE_ARM64
:
661 mapping
->image
.cpu
= CPU_ARM64
;
662 if (cpu_mask
& (CPU_FLAG(CPU_ARM
) | CPU_FLAG(CPU_ARM64
))) break;
663 return STATUS_INVALID_IMAGE_FORMAT
;
665 return STATUS_INVALID_IMAGE_FORMAT
;
667 clr_va
= nt
.opt
.hdr64
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].VirtualAddress
;
668 clr_size
= nt
.opt
.hdr64
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
].Size
;
670 mapping
->image
.base
= nt
.opt
.hdr64
.ImageBase
;
671 mapping
->image
.entry_point
= nt
.opt
.hdr64
.ImageBase
+ nt
.opt
.hdr64
.AddressOfEntryPoint
;
672 mapping
->image
.map_size
= ROUND_SIZE( nt
.opt
.hdr64
.SizeOfImage
);
673 mapping
->image
.stack_size
= nt
.opt
.hdr64
.SizeOfStackReserve
;
674 mapping
->image
.stack_commit
= nt
.opt
.hdr64
.SizeOfStackCommit
;
675 mapping
->image
.subsystem
= nt
.opt
.hdr64
.Subsystem
;
676 mapping
->image
.subsystem_low
= nt
.opt
.hdr64
.MinorSubsystemVersion
;
677 mapping
->image
.subsystem_high
= nt
.opt
.hdr64
.MajorSubsystemVersion
;
678 mapping
->image
.dll_charact
= nt
.opt
.hdr64
.DllCharacteristics
;
679 mapping
->image
.contains_code
= (nt
.opt
.hdr64
.SizeOfCode
||
680 nt
.opt
.hdr64
.AddressOfEntryPoint
||
681 nt
.opt
.hdr64
.SectionAlignment
& page_mask
);
682 mapping
->image
.header_size
= nt
.opt
.hdr64
.SizeOfHeaders
;
683 mapping
->image
.checksum
= nt
.opt
.hdr64
.CheckSum
;
684 mapping
->image
.image_flags
= 0;
685 if (nt
.opt
.hdr64
.SectionAlignment
& page_mask
)
686 mapping
->image
.image_flags
|= IMAGE_FLAGS_ImageMappedFlat
;
687 if ((nt
.opt
.hdr64
.DllCharacteristics
& IMAGE_DLLCHARACTERISTICS_DYNAMIC_BASE
) &&
688 mapping
->image
.contains_code
&& !(clr_va
&& clr_size
))
689 mapping
->image
.image_flags
|= IMAGE_FLAGS_ImageDynamicallyRelocated
;
693 return STATUS_INVALID_IMAGE_FORMAT
;
696 mapping
->image
.image_charact
= nt
.FileHeader
.Characteristics
;
697 mapping
->image
.machine
= nt
.FileHeader
.Machine
;
698 mapping
->image
.zerobits
= 0; /* FIXME */
699 mapping
->image
.gp
= 0; /* FIXME */
700 mapping
->image
.file_size
= file_size
;
701 mapping
->image
.loader_flags
= clr_va
&& clr_size
;
702 if (mz_size
== sizeof(mz
) && !memcmp( mz
.buffer
, fakedll_signature
, sizeof(fakedll_signature
) ))
703 mapping
->image
.image_flags
|= IMAGE_FLAGS_WineFakeDll
;
705 /* load the section headers */
707 pos
+= sizeof(nt
.Signature
) + sizeof(nt
.FileHeader
) + nt
.FileHeader
.SizeOfOptionalHeader
;
708 if (nt
.FileHeader
.NumberOfSections
> ARRAY_SIZE( sec
)) return STATUS_INVALID_IMAGE_FORMAT
;
709 size
= sizeof(*sec
) * nt
.FileHeader
.NumberOfSections
;
710 if (!mapping
->size
) mapping
->size
= mapping
->image
.map_size
;
711 else if (mapping
->size
> mapping
->image
.map_size
) return STATUS_SECTION_TOO_BIG
;
712 if (pos
+ size
> mapping
->image
.map_size
) return STATUS_INVALID_FILE_FOR_SECTION
;
713 if (pos
+ size
> mapping
->image
.header_size
) mapping
->image
.header_size
= pos
+ size
;
714 if (pread( unix_fd
, sec
, size
, pos
) != size
) return STATUS_INVALID_FILE_FOR_SECTION
;
716 for (i
= 0; i
< nt
.FileHeader
.NumberOfSections
&& !mapping
->image
.contains_code
; i
++)
717 if (sec
[i
].Characteristics
& IMAGE_SCN_MEM_EXECUTE
) mapping
->image
.contains_code
= 1;
719 if (load_clr_header( &clr
, clr_va
, clr_size
, unix_fd
, sec
, nt
.FileHeader
.NumberOfSections
) &&
720 (clr
.Flags
& COMIMAGE_FLAGS_ILONLY
))
722 mapping
->image
.image_flags
|= IMAGE_FLAGS_ComPlusILOnly
;
723 if (nt
.opt
.hdr32
.Magic
== IMAGE_NT_OPTIONAL_HDR32_MAGIC
&&
724 !(clr
.Flags
& COMIMAGE_FLAGS_32BITREQUIRED
))
726 mapping
->image
.image_flags
|= IMAGE_FLAGS_ComPlusNativeReady
;
727 if (cpu_mask
& CPU_FLAG(CPU_x86_64
)) mapping
->image
.cpu
= CPU_x86_64
;
728 else if (cpu_mask
& CPU_FLAG(CPU_ARM64
)) mapping
->image
.cpu
= CPU_ARM64
;
732 if (!build_shared_mapping( mapping
, unix_fd
, sec
, nt
.FileHeader
.NumberOfSections
))
733 return STATUS_INVALID_FILE_FOR_SECTION
;
735 return STATUS_SUCCESS
;
738 static struct ranges
*create_ranges(void)
740 struct ranges
*ranges
= alloc_object( &ranges_ops
);
742 if (!ranges
) return NULL
;
745 if (!(ranges
->ranges
= mem_alloc( ranges
->max
* sizeof(*ranges
->ranges
) )))
747 release_object( ranges
);
753 static unsigned int get_mapping_flags( obj_handle_t handle
, unsigned int flags
)
755 switch (flags
& (SEC_IMAGE
| SEC_RESERVE
| SEC_COMMIT
| SEC_FILE
))
758 if (flags
& (SEC_WRITECOMBINE
| SEC_LARGE_PAGES
)) break;
759 if (handle
) return SEC_FILE
| SEC_IMAGE
;
760 set_error( STATUS_INVALID_FILE_FOR_SECTION
);
763 if (!handle
) return flags
;
766 if (flags
& SEC_LARGE_PAGES
) break;
767 if (handle
) return SEC_FILE
| (flags
& (SEC_NOCACHE
| SEC_WRITECOMBINE
));
770 set_error( STATUS_INVALID_PARAMETER
);
775 static struct object
*create_mapping( struct object
*root
, const struct unicode_str
*name
,
776 unsigned int attr
, mem_size_t size
, unsigned int flags
,
777 obj_handle_t handle
, unsigned int file_access
,
778 const struct security_descriptor
*sd
)
780 struct mapping
*mapping
;
786 if (!page_mask
) page_mask
= sysconf( _SC_PAGESIZE
) - 1;
788 if (!(mapping
= create_named_object( root
, &mapping_ops
, name
, attr
, sd
)))
790 if (get_error() == STATUS_OBJECT_NAME_EXISTS
)
791 return &mapping
->obj
; /* Nothing else to do */
793 mapping
->size
= size
;
795 mapping
->shared
= NULL
;
796 mapping
->committed
= NULL
;
798 if (!(mapping
->flags
= get_mapping_flags( handle
, flags
))) goto error
;
802 const unsigned int sharing
= FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE
;
803 unsigned int mapping_access
= FILE_MAPPING_ACCESS
;
805 if (!(file
= get_file_obj( current
->process
, handle
, file_access
))) goto error
;
806 fd
= get_obj_fd( (struct object
*)file
);
808 /* file sharing rules for mappings are different so we use magic the access rights */
809 if (flags
& SEC_IMAGE
) mapping_access
|= FILE_MAPPING_IMAGE
;
810 else if (file_access
& FILE_WRITE_DATA
) mapping_access
|= FILE_MAPPING_WRITE
;
812 if (!(mapping
->fd
= get_fd_object_for_mapping( fd
, mapping_access
, sharing
)))
814 mapping
->fd
= dup_fd_object( fd
, mapping_access
, sharing
, FILE_SYNCHRONOUS_IO_NONALERT
);
815 if (mapping
->fd
) set_fd_user( mapping
->fd
, &mapping_fd_ops
, NULL
);
817 release_object( file
);
818 release_object( fd
);
819 if (!mapping
->fd
) goto error
;
821 if ((unix_fd
= get_unix_fd( mapping
->fd
)) == -1) goto error
;
822 if (fstat( unix_fd
, &st
) == -1)
827 if (flags
& SEC_IMAGE
)
829 unsigned int err
= get_image_params( mapping
, st
.st_size
, unix_fd
);
830 if (!err
) return &mapping
->obj
;
836 if (!(mapping
->size
= st
.st_size
))
838 set_error( STATUS_MAPPED_FILE_SIZE_ZERO
);
842 else if (st
.st_size
< mapping
->size
)
844 if (!(file_access
& FILE_WRITE_DATA
))
846 set_error( STATUS_SECTION_TOO_BIG
);
849 if (!grow_file( unix_fd
, mapping
->size
)) goto error
;
852 else /* Anonymous mapping (no associated file) */
856 set_error( STATUS_INVALID_PARAMETER
);
859 if ((flags
& SEC_RESERVE
) && !(mapping
->committed
= create_ranges())) goto error
;
860 mapping
->size
= (mapping
->size
+ page_mask
) & ~((mem_size_t
)page_mask
);
861 if ((unix_fd
= create_temp_file( mapping
->size
)) == -1) goto error
;
862 if (!(mapping
->fd
= create_anonymous_fd( &mapping_fd_ops
, unix_fd
, &mapping
->obj
,
863 FILE_SYNCHRONOUS_IO_NONALERT
))) goto error
;
864 allow_fd_caching( mapping
->fd
);
866 return &mapping
->obj
;
869 release_object( mapping
);
873 struct mapping
*get_mapping_obj( struct process
*process
, obj_handle_t handle
, unsigned int access
)
875 return (struct mapping
*)get_handle_obj( process
, handle
, access
, &mapping_ops
);
878 /* open a new file for the file descriptor backing the mapping */
879 struct file
*get_mapping_file( struct process
*process
, client_ptr_t base
,
880 unsigned int access
, unsigned int sharing
)
882 struct memory_view
*view
= find_mapped_view( process
, base
);
884 if (!view
|| !view
->fd
) return NULL
;
885 return create_file_for_fd_obj( view
->fd
, access
, sharing
);
888 static void mapping_dump( struct object
*obj
, int verbose
)
890 struct mapping
*mapping
= (struct mapping
*)obj
;
891 assert( obj
->ops
== &mapping_ops
);
892 fprintf( stderr
, "Mapping size=%08x%08x flags=%08x fd=%p shared=%p\n",
893 (unsigned int)(mapping
->size
>> 32), (unsigned int)mapping
->size
,
894 mapping
->flags
, mapping
->fd
, mapping
->shared
);
897 static struct object_type
*mapping_get_type( struct object
*obj
)
899 static const WCHAR name
[] = {'S','e','c','t','i','o','n'};
900 static const struct unicode_str str
= { name
, sizeof(name
) };
901 return get_object_type( &str
);
904 static struct fd
*mapping_get_fd( struct object
*obj
)
906 struct mapping
*mapping
= (struct mapping
*)obj
;
907 return (struct fd
*)grab_object( mapping
->fd
);
910 static unsigned int mapping_map_access( struct object
*obj
, unsigned int access
)
912 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SECTION_QUERY
| SECTION_MAP_READ
;
913 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SECTION_MAP_WRITE
;
914 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
| SECTION_MAP_EXECUTE
;
915 if (access
& GENERIC_ALL
) access
|= SECTION_ALL_ACCESS
;
916 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
919 static void mapping_destroy( struct object
*obj
)
921 struct mapping
*mapping
= (struct mapping
*)obj
;
922 assert( obj
->ops
== &mapping_ops
);
923 if (mapping
->fd
) release_object( mapping
->fd
);
924 if (mapping
->committed
) release_object( mapping
->committed
);
925 if (mapping
->shared
) release_object( mapping
->shared
);
928 static enum server_fd_type
mapping_get_fd_type( struct fd
*fd
)
933 int get_page_size(void)
935 if (!page_mask
) page_mask
= sysconf( _SC_PAGESIZE
) - 1;
936 return page_mask
+ 1;
939 /* create a file mapping */
940 DECL_HANDLER(create_mapping
)
942 struct object
*root
, *obj
;
943 struct unicode_str name
;
944 const struct security_descriptor
*sd
;
945 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
947 if (!objattr
) return;
949 if ((obj
= create_mapping( root
, &name
, objattr
->attributes
, req
->size
, req
->flags
,
950 req
->file_handle
, req
->file_access
, sd
)))
952 if (get_error() == STATUS_OBJECT_NAME_EXISTS
)
953 reply
->handle
= alloc_handle( current
->process
, obj
, req
->access
, objattr
->attributes
);
955 reply
->handle
= alloc_handle_no_access_check( current
->process
, obj
,
956 req
->access
, objattr
->attributes
);
957 release_object( obj
);
960 if (root
) release_object( root
);
963 /* open a handle to a mapping */
964 DECL_HANDLER(open_mapping
)
966 struct unicode_str name
= get_req_unicode_str();
968 reply
->handle
= open_object( current
->process
, req
->rootdir
, req
->access
,
969 &mapping_ops
, &name
, req
->attributes
);
972 /* get a mapping information */
973 DECL_HANDLER(get_mapping_info
)
975 struct mapping
*mapping
;
977 if (!(mapping
= get_mapping_obj( current
->process
, req
->handle
, req
->access
))) return;
979 reply
->size
= mapping
->size
;
980 reply
->flags
= mapping
->flags
;
982 if (mapping
->flags
& SEC_IMAGE
)
983 set_reply_data( &mapping
->image
, min( sizeof(mapping
->image
), get_reply_max_size() ));
985 if (!(req
->access
& (SECTION_MAP_READ
| SECTION_MAP_WRITE
))) /* query only */
987 release_object( mapping
);
992 reply
->shared_file
= alloc_handle( current
->process
, mapping
->shared
->file
,
993 GENERIC_READ
|GENERIC_WRITE
, 0 );
994 release_object( mapping
);
997 /* add a memory view in the current process */
998 DECL_HANDLER(map_view
)
1000 struct mapping
*mapping
= NULL
;
1001 struct memory_view
*view
;
1003 if (!req
->size
|| (req
->base
& page_mask
) || req
->base
+ req
->size
< req
->base
) /* overflow */
1005 set_error( STATUS_INVALID_PARAMETER
);
1009 /* make sure we don't already have an overlapping view */
1010 LIST_FOR_EACH_ENTRY( view
, ¤t
->process
->views
, struct memory_view
, entry
)
1012 if (view
->base
+ view
->size
<= req
->base
) continue;
1013 if (view
->base
>= req
->base
+ req
->size
) continue;
1014 set_error( STATUS_INVALID_PARAMETER
);
1018 if (!(mapping
= get_mapping_obj( current
->process
, req
->mapping
, req
->access
))) return;
1020 if (mapping
->flags
& SEC_IMAGE
)
1022 if (req
->start
|| req
->size
> mapping
->image
.map_size
)
1024 set_error( STATUS_INVALID_PARAMETER
);
1028 else if (req
->start
>= mapping
->size
||
1029 req
->start
+ req
->size
< req
->start
||
1030 req
->start
+ req
->size
> ((mapping
->size
+ page_mask
) & ~(mem_size_t
)page_mask
))
1032 set_error( STATUS_INVALID_PARAMETER
);
1036 if ((view
= mem_alloc( sizeof(*view
) )))
1038 view
->base
= req
->base
;
1039 view
->size
= req
->size
;
1040 view
->start
= req
->start
;
1041 view
->flags
= mapping
->flags
;
1042 view
->fd
= !is_fd_removable( mapping
->fd
) ? (struct fd
*)grab_object( mapping
->fd
) : NULL
;
1043 view
->committed
= mapping
->committed
? (struct ranges
*)grab_object( mapping
->committed
) : NULL
;
1044 view
->shared
= mapping
->shared
? (struct shared_map
*)grab_object( mapping
->shared
) : NULL
;
1045 list_add_tail( ¤t
->process
->views
, &view
->entry
);
1049 release_object( mapping
);
1052 /* unmap a memory view from the current process */
1053 DECL_HANDLER(unmap_view
)
1055 struct memory_view
*view
= find_mapped_view( current
->process
, req
->base
);
1057 if (view
) free_memory_view( view
);
1060 /* get a range of committed pages in a file mapping */
1061 DECL_HANDLER(get_mapping_committed_range
)
1063 struct memory_view
*view
= find_mapped_view( current
->process
, req
->base
);
1065 if (view
) reply
->committed
= find_committed_range( view
, req
->offset
, &reply
->size
);
1068 /* add a range to the committed pages in a file mapping */
1069 DECL_HANDLER(add_mapping_committed_range
)
1071 struct memory_view
*view
= find_mapped_view( current
->process
, req
->base
);
1073 if (view
) add_committed_range( view
, req
->offset
, req
->offset
+ req
->size
);
1076 /* check if two memory maps are for the same file */
1077 DECL_HANDLER(is_same_mapping
)
1079 struct memory_view
*view1
= find_mapped_view( current
->process
, req
->base1
);
1080 struct memory_view
*view2
= find_mapped_view( current
->process
, req
->base2
);
1082 if (!view1
|| !view2
) return;
1083 if (!view1
->fd
|| !view2
->fd
||
1084 !(view1
->flags
& SEC_IMAGE
) || !(view2
->flags
& SEC_IMAGE
) ||
1085 !is_same_file_fd( view1
->fd
, view2
->fd
))
1086 set_error( STATUS_NOT_SAME_DEVICE
);