Move some internal headers to /reactos, set it as a default include path, move pseh...
[reactos.git] / reactos / ntoskrnl / mm / elf.inc.h
1 /* $Id$
2 */
3 #include <ntoskrnl.h>
4
5 #define NDEBUG
6 #include <internal/debug.h>
7
8 #include <reactos/exeformat.h>
9
10 #ifndef __ELF_WORD_SIZE
11 #error __ELF_WORD_SIZE must be defined
12 #endif
13
14 #ifndef MAXULONG
15 #define MAXULONG ((ULONG)(~1))
16 #endif
17
18 #include <elf/elf.h>
19
20 /* TODO: Intsafe should be made into a library, as it's generally useful */
21 static __inline BOOLEAN Intsafe_CanAddULongPtr
22 (
23 IN ULONG_PTR Addend1,
24 IN ULONG_PTR Addend2
25 )
26 {
27 return Addend1 <= (MAXULONG_PTR - Addend2);
28 }
29
30 #define Intsafe_CanAddSizeT Intsafe_CanAddULongPtr
31
32 static __inline BOOLEAN Intsafe_CanAddULong32
33 (
34 IN ULONG Addend1,
35 IN ULONG Addend2
36 )
37 {
38 return Addend1 <= (MAXULONG - Addend2);
39 }
40
41 static __inline BOOLEAN Intsafe_AddULong32
42 (
43 OUT PULONG32 Result,
44 IN ULONG Addend1,
45 IN ULONG Addend2
46 )
47 {
48 if(!Intsafe_CanAddULong32(Addend1, Addend2))
49 return FALSE;
50
51 *Result = Addend1 + Addend2;
52 return TRUE;
53 }
54
55 static __inline BOOLEAN Intsafe_CanAddULong64
56 (
57 IN ULONG64 Addend1,
58 IN ULONG64 Addend2
59 )
60 {
61 return Addend1 <= (((ULONG64)-1) - Addend2);
62 }
63
64 static __inline BOOLEAN Intsafe_AddULong64
65 (
66 OUT PULONG64 Result,
67 IN ULONG64 Addend1,
68 IN ULONG64 Addend2
69 )
70 {
71 if(!Intsafe_CanAddULong64(Addend1, Addend2))
72 return FALSE;
73
74 *Result = Addend1 + Addend2;
75 return TRUE;
76 }
77
78 static __inline BOOLEAN Intsafe_CanMulULong32
79 (
80 IN ULONG Factor1,
81 IN ULONG Factor2
82 )
83 {
84 return Factor1 <= (MAXULONG / Factor2);
85 }
86
87 static __inline BOOLEAN Intsafe_MulULong32
88 (
89 OUT PULONG32 Result,
90 IN ULONG Factor1,
91 IN ULONG Factor2
92 )
93 {
94 if(!Intsafe_CanMulULong32(Factor1, Factor2))
95 return FALSE;
96
97 *Result = Factor1 * Factor2;
98 return TRUE;
99 }
100
101 static __inline BOOLEAN Intsafe_CanOffsetPointer
102 (
103 IN CONST VOID * Pointer,
104 IN SIZE_T Offset
105 )
106 {
107 /* FIXME: (PVOID)MAXULONG_PTR isn't necessarily a valid address */
108 return Intsafe_CanAddULongPtr((ULONG_PTR)Pointer, Offset);
109 }
110
111 #if __ELF_WORD_SIZE == 32
112 #define ElfFmtpAddSize Intsafe_AddULong32
113 #define ElfFmtpReadAddr ElfFmtpReadULong
114 #define ElfFmtpReadOff ElfFmtpReadULong
115 #define ElfFmtpSafeReadAddr ElfFmtpSafeReadULong
116 #define ElfFmtpSafeReadOff ElfFmtpSafeReadULong
117 #define ElfFmtpSafeReadSize ElfFmtpSafeReadULong
118 #elif __ELF_WORD_SIZE == 64
119 #define ElfFmtpAddSize Intsafe_AddULong64
120 #define ElfFmtpReadAddr ElfFmtpReadULong64
121 #define ElfFmtpReadOff ElfFmtpReadULong64
122 #define ElfFmtpSafeReadAddr ElfFmtpSafeReadULong64
123 #define ElfFmtpSafeReadOff ElfFmtpSafeReadULong64
124 #define ElfFmtpSafeReadSize ElfFmtpSafeReadULong64
125 #endif
126
127 /* TODO: these are standard DDK/PSDK macros */
128 #define RtlRetrieveUlonglong(DST_, SRC_) \
129 (RtlCopyMemory((DST_), (SRC_), sizeof(ULONG64)))
130
131 #ifndef RTL_FIELD_SIZE
132 #define RTL_FIELD_SIZE(TYPE_, FIELD_) (sizeof(((TYPE_ *)0)->FIELD_))
133 #endif
134
135 #ifndef RTL_SIZEOF_THROUGH_FIELD
136 #define RTL_SIZEOF_THROUGH_FIELD(TYPE_, FIELD_) \
137 (FIELD_OFFSET(TYPE_, FIELD_) + RTL_FIELD_SIZE(TYPE_, FIELD_))
138 #endif
139
140 #ifndef RTL_CONTAINS_FIELD
141 #define RTL_CONTAINS_FIELD(P_, SIZE_, FIELD_) \
142 ((((char *)(P_)) + (SIZE_)) > (((char *)(&((P_)->FIELD_))) + sizeof((P_)->FIELD_)))
143 #endif
144
145 #define ELFFMT_FIELDS_EQUAL(TYPE1_, TYPE2_, FIELD_) \
146 ( \
147 (FIELD_OFFSET(TYPE1_, FIELD_) == FIELD_OFFSET(TYPE2_, FIELD_)) && \
148 (RTL_FIELD_SIZE(TYPE1_, FIELD_) == RTL_FIELD_SIZE(TYPE2_, FIELD_)) \
149 )
150
151 #define ELFFMT_MAKE_ULONG64(BYTE1_, BYTE2_, BYTE3_, BYTE4_, BYTE5_, BYTE6_, BYTE7_, BYTE8_) \
152 ( \
153 (((ULONG64)ELFFMT_MAKE_ULONG(BYTE1_, BYTE2_, BYTE3_, BYTE4_)) << 0) | \
154 (((ULONG64)ELFFMT_MAKE_ULONG(BYTE5_, BYTE6_, BYTE7_, BYTE8_)) << 32) \
155 )
156
157 #define ELFFMT_MAKE_ULONG(BYTE1_, BYTE2_, BYTE3_, BYTE4_) \
158 ( \
159 (((ULONG)ELFFMT_MAKE_USHORT(BYTE1_, BYTE2_)) << 0) | \
160 (((ULONG)ELFFMT_MAKE_USHORT(BYTE3_, BYTE4_)) << 16) \
161 )
162
163 #define ELFFMT_MAKE_USHORT(BYTE1_, BYTE2_) \
164 ( \
165 (((USHORT)(BYTE1_)) << 0) | \
166 (((USHORT)(BYTE2_)) << 8) \
167 )
168
169 static __inline ULONG64 ElfFmtpReadULong64
170 (
171 IN ULONG64 Input,
172 IN ULONG DataType
173 )
174 {
175 PBYTE p;
176
177 if(DataType == ELF_TARG_DATA)
178 return Input;
179
180 p = (PBYTE)&Input;
181
182 switch(DataType)
183 {
184 case ELFDATA2LSB: return ELFFMT_MAKE_ULONG64(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
185 case ELFDATA2MSB: return ELFFMT_MAKE_ULONG64(p[7], p[6], p[5], p[4], p[3], p[2], p[1], p[0]);
186 }
187
188 ASSERT(FALSE);
189 return (ULONG64)-1;
190 }
191
192 static __inline ULONG ElfFmtpReadULong
193 (
194 IN ULONG Input,
195 IN ULONG DataType
196 )
197 {
198 PBYTE p;
199
200 if(DataType == ELF_TARG_DATA)
201 return Input;
202
203 p = (PBYTE)&Input;
204
205 switch(DataType)
206 {
207 case ELFDATA2LSB: return ELFFMT_MAKE_ULONG(p[0], p[1], p[2], p[3]);
208 case ELFDATA2MSB: return ELFFMT_MAKE_ULONG(p[3], p[2], p[1], p[0]);
209 }
210
211 ASSERT(FALSE);
212 return (ULONG)-1;
213 }
214
215 static __inline USHORT ElfFmtpReadUShort
216 (
217 IN USHORT Input,
218 IN ULONG DataType
219 )
220 {
221 PBYTE p;
222
223 if(DataType == ELF_TARG_DATA)
224 return Input;
225
226 p = (PBYTE)&Input;
227
228 switch(DataType)
229 {
230 case ELFDATA2LSB: return ELFFMT_MAKE_USHORT(p[0], p[1]);
231 case ELFDATA2MSB: return ELFFMT_MAKE_USHORT(p[1], p[0]);
232 }
233
234 ASSERT(FALSE);
235 return (USHORT)-1;
236 }
237
238 static __inline ULONG64 ElfFmtpSafeReadULong64
239 (
240 IN CONST ULONG64 * Input,
241 IN ULONG DataType
242 )
243 {
244 PBYTE p;
245 ULONG64 nSafeInput;
246
247 RtlRetrieveUlonglong(&nSafeInput, Input);
248
249 p = (PBYTE)&nSafeInput;
250
251 switch(DataType)
252 {
253 case ELFDATA2LSB: return ELFFMT_MAKE_ULONG64(p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7]);
254 case ELFDATA2MSB: return ELFFMT_MAKE_ULONG64(p[7], p[6], p[5], p[4], p[3], p[2], p[1], p[0]);
255 }
256
257 ASSERT(FALSE);
258 return (ULONG64)-1;
259 }
260
261 static __inline ULONG ElfFmtpSafeReadULong
262 (
263 IN CONST ULONG32 * Input,
264 IN ULONG DataType
265 )
266 {
267 PBYTE p;
268 ULONG nSafeInput;
269
270 RtlRetrieveUlong(&nSafeInput, Input);
271
272 if(DataType == ELF_TARG_DATA)
273 return nSafeInput;
274
275 p = (PBYTE)&nSafeInput;
276
277 switch(DataType)
278 {
279 case ELFDATA2LSB: return ELFFMT_MAKE_ULONG(p[0], p[1], p[2], p[3]);
280 case ELFDATA2MSB: return ELFFMT_MAKE_ULONG(p[3], p[2], p[1], p[0]);
281 }
282
283 ASSERT(FALSE);
284 return (ULONG)-1;
285 }
286
287 static __inline BOOLEAN ElfFmtpIsPowerOf2(IN Elf_Addr Number)
288 {
289 if(Number == 0)
290 return FALSE;
291
292 while((Number % 2) == 0)
293 Number /= 2;
294
295 return Number == 1;
296 }
297
298 static __inline Elf_Addr ElfFmtpModPow2
299 (
300 IN Elf_Addr Address,
301 IN Elf_Addr Alignment
302 )
303 {
304 ASSERT(sizeof(Elf_Addr) == sizeof(Elf_Size));
305 ASSERT(sizeof(Elf_Addr) == sizeof(Elf_Off));
306 ASSERT(ElfFmtpIsPowerOf2(Alignment));
307 return Address & (Alignment - 1);
308 }
309
310 static __inline Elf_Addr ElfFmtpAlignDown
311 (
312 IN Elf_Addr Address,
313 IN Elf_Addr Alignment
314 )
315 {
316 ASSERT(sizeof(Elf_Addr) == sizeof(Elf_Size));
317 ASSERT(sizeof(Elf_Addr) == sizeof(Elf_Off));
318 ASSERT(ElfFmtpIsPowerOf2(Alignment));
319 return Address & ~(Alignment - 1);
320 }
321
322 static __inline BOOLEAN ElfFmtpAlignUp
323 (
324 OUT Elf_Addr * AlignedAddress,
325 IN Elf_Addr Address,
326 IN Elf_Addr Alignment
327 )
328 {
329 Elf_Addr nExcess = ElfFmtpModPow2(Address, Alignment);
330
331 if(nExcess == 0)
332 {
333 *AlignedAddress = Address;
334 return nExcess == 0;
335 }
336 else
337 return ElfFmtpAddSize(AlignedAddress, Address, Alignment - nExcess);
338 }
339
340 /*
341 References:
342 [1] Tool Interface Standards (TIS) Committee, "Executable and Linking Format
343 (ELF) Specification", Version 1.2
344 */
345 NTSTATUS NTAPI
346 #if __ELF_WORD_SIZE == 32
347 Elf32FmtCreateSection
348 #elif __ELF_WORD_SIZE == 64
349 Elf64FmtCreateSection
350 #endif
351 (
352 IN CONST VOID * FileHeader,
353 IN SIZE_T FileHeaderSize,
354 IN PVOID File,
355 OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject,
356 OUT PULONG Flags,
357 IN PEXEFMT_CB_READ_FILE ReadFileCb,
358 IN PEXEFMT_CB_ALLOCATE_SEGMENTS AllocateSegmentsCb
359 )
360 {
361 NTSTATUS nStatus;
362 const Elf_Ehdr * pehHeader;
363 const Elf_Phdr * pphPHdrs;
364 BOOLEAN fPageAligned;
365 ULONG nData;
366 ULONG nPHdrCount;
367 ULONG cbPHdrSize;
368 Elf_Off cbPHdrOffset;
369 PVOID pBuffer;
370 PMM_SECTION_SEGMENT pssSegments;
371 Elf_Addr nImageBase = 0;
372 Elf_Addr nEntryPoint;
373 ULONG32 nPrevVirtualEndOfSegment = 0;
374 ULONG i;
375 ULONG j;
376
377 (void)Intsafe_AddULong64;
378 (void)Intsafe_MulULong32;
379 (void)ElfFmtpReadULong64;
380 (void)ElfFmtpSafeReadULong64;
381 (void)ElfFmtpReadULong;
382
383 #define DIE(ARGS_) { DPRINT ARGS_; goto l_Return; }
384
385 pBuffer = NULL;
386
387 nStatus = STATUS_INVALID_IMAGE_FORMAT;
388
389 /* Ensure the file contains the full header */
390 /*
391 EXEFMT_LOAD_HEADER_SIZE is 8KB: enough to contain an ELF header (at least in
392 all the classes defined as of December 2004). If FileHeaderSize is less than
393 sizeof(Elf_Ehdr), it means the file itself is small enough not to contain a
394 full ELF header
395 */
396 ASSERT(sizeof(Elf_Ehdr) <= EXEFMT_LOAD_HEADER_SIZE);
397
398 if(FileHeaderSize < sizeof(Elf_Ehdr))
399 DIE(("The file is truncated, doesn't contain the full header\n"));
400
401 pehHeader = FileHeader;
402 ASSERT(((ULONG_PTR)pehHeader % TYPE_ALIGNMENT(Elf_Ehdr)) == 0);
403
404 nData = pehHeader->e_ident[EI_DATA];
405
406 /* Validate the header */
407 if(ElfFmtpReadUShort(pehHeader->e_ehsize, nData) < sizeof(Elf_Ehdr))
408 DIE(("Inconsistent value for e_ehsize\n"));
409
410 /* Calculate size and offset of the program headers */
411 cbPHdrSize = ElfFmtpReadUShort(pehHeader->e_phentsize, nData);
412
413 if(cbPHdrSize != sizeof(Elf_Phdr))
414 DIE(("Inconsistent value for e_phentsize\n"));
415
416 /* MAXUSHORT * MAXUSHORT < MAXULONG */
417 nPHdrCount = ElfFmtpReadUShort(pehHeader->e_phnum, nData);
418 ASSERT(Intsafe_CanMulULong32(cbPHdrSize, nPHdrCount));
419 cbPHdrSize *= nPHdrCount;
420
421 cbPHdrOffset = ElfFmtpReadOff(pehHeader->e_phoff, nData);
422
423 /* The initial header doesn't contain the program headers */
424 if(cbPHdrOffset > FileHeaderSize || cbPHdrSize > (FileHeaderSize - cbPHdrOffset))
425 {
426 NTSTATUS nReadStatus;
427 LARGE_INTEGER lnOffset;
428 PVOID pData;
429 ULONG cbReadSize;
430
431 /* Will worry about this when ELF128 comes */
432 ASSERT(sizeof(cbPHdrOffset) <= sizeof(lnOffset.QuadPart));
433
434 lnOffset.QuadPart = (LONG64)cbPHdrOffset;
435
436 /*
437 We can't support executable files larger than 8 Exabytes - it's a limitation
438 of the I/O system (only 63-bit offsets are supported). Quote:
439
440 [...] the total amount of printed material in the world is estimated to be
441 around a fifth of an exabyte. [...] [Source: Wikipedia]
442 */
443 if(lnOffset.u.HighPart < 0)
444 DIE(("The program header is too far into the file\n"));
445
446 nReadStatus = ReadFileCb
447 (
448 File,
449 &lnOffset,
450 cbPHdrSize,
451 &pData,
452 &pBuffer,
453 &cbReadSize
454 );
455
456 if(!NT_SUCCESS(nReadStatus))
457 {
458 nStatus = nReadStatus;
459 DIE(("ReadFile failed, status %08X\n", nStatus));
460 }
461
462 ASSERT(pData);
463 ASSERT(pBuffer);
464 ASSERT(Intsafe_CanOffsetPointer(pData, cbReadSize));
465
466 if(cbReadSize < cbPHdrSize)
467 DIE(("The file didn't contain the program headers\n"));
468
469 /* Force the buffer to be aligned */
470 if((ULONG_PTR)pData % TYPE_ALIGNMENT(Elf_Phdr))
471 {
472 ASSERT(((ULONG_PTR)pBuffer % TYPE_ALIGNMENT(Elf_Phdr)) == 0);
473 RtlMoveMemory(pBuffer, pData, cbPHdrSize);
474 pphPHdrs = pBuffer;
475 }
476 else
477 pphPHdrs = pData;
478 }
479 else
480 {
481 ASSERT(Intsafe_CanAddSizeT(cbPHdrOffset, 0));
482 ASSERT(Intsafe_CanOffsetPointer(FileHeader, cbPHdrOffset));
483 pphPHdrs = (PVOID)((ULONG_PTR)FileHeader + (ULONG_PTR)cbPHdrOffset);
484 }
485
486 /* Allocate the segments */
487 pssSegments = AllocateSegmentsCb(nPHdrCount);
488
489 if(pssSegments == NULL)
490 {
491 nStatus = STATUS_INSUFFICIENT_RESOURCES;
492 DIE(("Out of memory\n"));
493 }
494
495 ImageSectionObject->Segments = pssSegments;
496
497 fPageAligned = TRUE;
498
499 /* Fill in the segments */
500 for(i = 0, j = 0; i < nPHdrCount; ++ i)
501 {
502 switch(ElfFmtpSafeReadULong(&pphPHdrs[i].p_type, nData))
503 {
504 case PT_LOAD:
505 {
506 static const ULONG ProgramHeaderFlagsToProtect[8] =
507 {
508 PAGE_NOACCESS, /* 0 */
509 PAGE_EXECUTE_READ, /* PF_X */
510 PAGE_READWRITE, /* PF_W */
511 PAGE_EXECUTE_READWRITE, /* PF_X | PF_W */
512 PAGE_READONLY, /* PF_R */
513 PAGE_EXECUTE_READ, /* PF_X | PF_R */
514 PAGE_READWRITE, /* PF_W | PF_R */
515 PAGE_EXECUTE_READWRITE /* PF_X | PF_W | PF_R */
516 };
517
518 Elf_Size nAlignment;
519 Elf_Off nFileOffset;
520 Elf_Addr nVirtualAddr;
521 Elf_Size nAdj;
522 Elf_Size nVirtualSize;
523 Elf_Size nFileSize;
524
525 ASSERT(j <= nPHdrCount);
526
527 /* Retrieve and validate the segment alignment */
528 nAlignment = ElfFmtpSafeReadSize(&pphPHdrs[i].p_align, nData);
529
530 if(nAlignment == 0)
531 nAlignment = 1;
532 else if(!ElfFmtpIsPowerOf2(nAlignment))
533 DIE(("Alignment of loadable segment isn't a power of 2\n"));
534
535 if(nAlignment < PAGE_SIZE)
536 fPageAligned = FALSE;
537
538 /* Retrieve the addresses and calculate the adjustment */
539 nFileOffset = ElfFmtpSafeReadOff(&pphPHdrs[i].p_offset, nData);
540 nVirtualAddr = ElfFmtpSafeReadAddr(&pphPHdrs[i].p_vaddr, nData);
541
542 nAdj = ElfFmtpModPow2(nFileOffset, nAlignment);
543
544 if(nAdj != ElfFmtpModPow2(nVirtualAddr, nAlignment))
545 DIE(("File and memory address of loadable segment not congruent modulo alignment\n"));
546
547 /* Retrieve, adjust and align the file size and memory size */
548 if(!ElfFmtpAddSize(&nFileSize, ElfFmtpSafeReadSize(&pphPHdrs[i].p_filesz, nData), nAdj))
549 DIE(("Can't adjust the file size of loadable segment\n"));
550
551 if(!ElfFmtpAddSize(&nVirtualSize, ElfFmtpSafeReadSize(&pphPHdrs[i].p_memsz, nData), nAdj))
552 DIE(("Can't adjust the memory size of lodable segment\n"));
553
554 if(!ElfFmtpAlignUp(&nVirtualSize, nVirtualSize, nAlignment))
555 DIE(("Can't align the memory size of lodable segment\n"));
556
557 if(nFileSize > nVirtualSize)
558 nFileSize = nVirtualSize;
559
560 if(nVirtualSize > MAXULONG)
561 DIE(("Virtual image larger than 4GB\n"));
562
563 ASSERT(nFileSize <= MAXULONG);
564
565 pssSegments[j].Length = (ULONG)(nVirtualSize & 0xFFFFFFFF);
566 pssSegments[j].RawLength = (ULONG)(nFileSize & 0xFFFFFFFF);
567
568 /* File offset */
569 nFileOffset = ElfFmtpAlignDown(nFileOffset, nAlignment);
570
571 #if __ELF_WORD_SIZE >= 64
572 ASSERT(sizeof(nFileOffset) == sizeof(LONG64));
573
574 if(((LONG64)nFileOffset) < 0)
575 DIE(("File offset of loadable segment is too large\n"));
576 #endif
577
578 pssSegments[j].FileOffset = (LONG64)nFileOffset;
579
580 /* Virtual address */
581 nVirtualAddr = ElfFmtpAlignDown(nVirtualAddr, nAlignment);
582
583 if(j == 0)
584 {
585 /* First segment: its address is the base address of the image */
586 nImageBase = nVirtualAddr;
587 pssSegments[j].VirtualAddress = 0;
588
589 /* Several places make this assumption */
590 if(pssSegments[j].FileOffset != 0)
591 DIE(("First loadable segment doesn't contain the ELF header\n"));
592 }
593 else
594 {
595 Elf_Size nVirtualOffset;
596
597 /* Other segment: store the offset from the base address */
598 if(nVirtualAddr <= nImageBase)
599 DIE(("Loadable segments are not sorted\n"));
600
601 nVirtualOffset = nVirtualAddr - nImageBase;
602
603 if(nVirtualOffset > MAXULONG)
604 DIE(("Virtual image larger than 4GB\n"));
605
606 pssSegments[j].VirtualAddress = (ULONG)(nVirtualOffset & 0xFFFFFFFF);
607
608 if(pssSegments[j].VirtualAddress != nPrevVirtualEndOfSegment)
609 DIE(("Loadable segments are not sorted and contiguous\n"));
610 }
611
612 /* Memory protection */
613 pssSegments[j].Protection = ProgramHeaderFlagsToProtect
614 [
615 ElfFmtpSafeReadULong(&pphPHdrs[i].p_flags, nData) & (PF_R | PF_W | PF_X)
616 ];
617
618 /* Characteristics */
619 /*
620 TODO: need to add support for the shared, non-pageable, non-cacheable and
621 discardable attributes. This involves extensions to the ELF format, so it's
622 nothing to be taken lightly
623 */
624 if(pssSegments[j].Protection & PAGE_IS_EXECUTABLE)
625 {
626 ImageSectionObject->Executable = TRUE;
627 pssSegments[j].Characteristics = IMAGE_SCN_CNT_CODE;
628 }
629 else if(pssSegments[j].RawLength == 0)
630 pssSegments[j].Characteristics = IMAGE_SCN_CNT_UNINITIALIZED_DATA;
631 else
632 pssSegments[j].Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA;
633
634 /*
635 FIXME: see the TODO above. This is the safest way to load ELF drivers, for
636 now, if a bit wasteful of memory
637 */
638 pssSegments[j].Characteristics |= IMAGE_SCN_MEM_NOT_PAGED;
639
640 /* Copy-on-write */
641 pssSegments[j].WriteCopy = TRUE;
642
643 if(!Intsafe_AddULong32(&nPrevVirtualEndOfSegment, pssSegments[j].VirtualAddress, pssSegments[j].Length))
644 DIE(("Virtual image larger than 4GB\n"));
645
646 ++ j;
647 break;
648 }
649 }
650 }
651
652 if(j == 0)
653 DIE(("No loadable segments\n"));
654
655 ImageSectionObject->NrSegments = j;
656
657 *Flags =
658 EXEFMT_LOAD_ASSUME_SEGMENTS_SORTED |
659 EXEFMT_LOAD_ASSUME_SEGMENTS_NO_OVERLAP;
660
661 if(fPageAligned)
662 *Flags |= EXEFMT_LOAD_ASSUME_SEGMENTS_PAGE_ALIGNED;
663
664 nEntryPoint = ElfFmtpReadAddr(pehHeader->e_entry, nData);
665
666 if(nEntryPoint < nImageBase || nEntryPoint - nImageBase > nPrevVirtualEndOfSegment)
667 DIE(("Entry point not within the virtual image\n"));
668
669 ASSERT(nEntryPoint >= nImageBase);
670 ASSERT((nEntryPoint - nImageBase) <= MAXULONG);
671 ImageSectionObject->EntryPoint = nEntryPoint - nImageBase;
672
673 /* TODO: support Wine executables and read these values from nt_headers */
674 ImageSectionObject->ImageCharacteristics |=
675 IMAGE_FILE_EXECUTABLE_IMAGE |
676 IMAGE_FILE_LINE_NUMS_STRIPPED |
677 IMAGE_FILE_LOCAL_SYMS_STRIPPED |
678 (nImageBase > MAXULONG ? IMAGE_FILE_LARGE_ADDRESS_AWARE : 0) |
679 IMAGE_FILE_DEBUG_STRIPPED;
680
681 if(nData == ELFDATA2LSB)
682 ImageSectionObject->ImageCharacteristics |= IMAGE_FILE_BYTES_REVERSED_LO;
683 else if(nData == ELFDATA2MSB)
684 ImageSectionObject->ImageCharacteristics |= IMAGE_FILE_BYTES_REVERSED_HI;
685
686 /* Base address outside the possible address space */
687 if(nImageBase > MAXULONG_PTR)
688 ImageSectionObject->ImageBase = EXEFMT_LOAD_BASE_NONE;
689 /* Position-independent image, base address doesn't matter */
690 else if(nImageBase == 0)
691 ImageSectionObject->ImageBase = EXEFMT_LOAD_BASE_ANY;
692 /* Use the specified base address */
693 else
694 ImageSectionObject->ImageBase = (ULONG_PTR)nImageBase;
695
696 /* safest bet */
697 ImageSectionObject->Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
698 ImageSectionObject->MinorSubsystemVersion = 0;
699 ImageSectionObject->MajorSubsystemVersion = 4;
700
701 /* Success, at last */
702 nStatus = STATUS_SUCCESS;
703
704 l_Return:
705 if(pBuffer)
706 ExFreePool(pBuffer);
707
708 return nStatus;
709 }
710
711 /* EOF */