- KdDebuggerNotPresent should be FALSE by default.
[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 ((ULONG_PTR)(P_) + (ULONG_PTR)(SIZE_) > (ULONG_PTR)&((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 PUCHAR p;
176
177 if(DataType == ELF_TARG_DATA)
178 return Input;
179
180 p = (PUCHAR)&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 PUCHAR p;
199
200 if(DataType == ELF_TARG_DATA)
201 return Input;
202
203 p = (PUCHAR)&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 PUCHAR p;
222
223 if(DataType == ELF_TARG_DATA)
224 return Input;
225
226 p = (PUCHAR)&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 PUCHAR p;
245 ULONG64 nSafeInput;
246
247 RtlRetrieveUlonglong(&nSafeInput, Input);
248
249 p = (PUCHAR)&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 PUCHAR p;
268 ULONG nSafeInput;
269 union
270 {
271 CONST ULONG32 *ConstInput;
272 ULONG32 *Input;
273 }pInput = {Input};
274
275 RtlRetrieveUlong(&nSafeInput, pInput.Input);
276
277 if(DataType == ELF_TARG_DATA)
278 return nSafeInput;
279
280 p = (PUCHAR)&nSafeInput;
281
282 switch(DataType)
283 {
284 case ELFDATA2LSB: return ELFFMT_MAKE_ULONG(p[0], p[1], p[2], p[3]);
285 case ELFDATA2MSB: return ELFFMT_MAKE_ULONG(p[3], p[2], p[1], p[0]);
286 }
287
288 ASSERT(FALSE);
289 return (ULONG)-1;
290 }
291
292 static __inline BOOLEAN ElfFmtpIsPowerOf2(IN Elf_Addr Number)
293 {
294 if(Number == 0)
295 return FALSE;
296
297 return (Number & (Number - 1)) == 0;
298 }
299
300 static __inline Elf_Addr ElfFmtpModPow2
301 (
302 IN Elf_Addr Address,
303 IN Elf_Addr Alignment
304 )
305 {
306 ASSERT(sizeof(Elf_Addr) == sizeof(Elf_Size));
307 ASSERT(sizeof(Elf_Addr) == sizeof(Elf_Off));
308 ASSERT(ElfFmtpIsPowerOf2(Alignment));
309 return Address & (Alignment - 1);
310 }
311
312 static __inline Elf_Addr ElfFmtpAlignDown
313 (
314 IN Elf_Addr Address,
315 IN Elf_Addr Alignment
316 )
317 {
318 ASSERT(sizeof(Elf_Addr) == sizeof(Elf_Size));
319 ASSERT(sizeof(Elf_Addr) == sizeof(Elf_Off));
320 ASSERT(ElfFmtpIsPowerOf2(Alignment));
321 return Address & ~(Alignment - 1);
322 }
323
324 static __inline BOOLEAN ElfFmtpAlignUp
325 (
326 OUT Elf_Addr * AlignedAddress,
327 IN Elf_Addr Address,
328 IN Elf_Addr Alignment
329 )
330 {
331 Elf_Addr nExcess = ElfFmtpModPow2(Address, Alignment);
332
333 if(nExcess == 0)
334 {
335 *AlignedAddress = Address;
336 return nExcess == 0;
337 }
338 else
339 return ElfFmtpAddSize(AlignedAddress, Address, Alignment - nExcess);
340 }
341
342 /*
343 References:
344 [1] Tool Interface Standards (TIS) Committee, "Executable and Linking Format
345 (ELF) Specification", Version 1.2
346 */
347 NTSTATUS NTAPI
348 #if __ELF_WORD_SIZE == 32
349 Elf32FmtCreateSection
350 #elif __ELF_WORD_SIZE == 64
351 Elf64FmtCreateSection
352 #endif
353 (
354 IN CONST VOID * FileHeader,
355 IN SIZE_T FileHeaderSize,
356 IN PVOID File,
357 OUT PMM_IMAGE_SECTION_OBJECT ImageSectionObject,
358 OUT PULONG Flags,
359 IN PEXEFMT_CB_READ_FILE ReadFileCb,
360 IN PEXEFMT_CB_ALLOCATE_SEGMENTS AllocateSegmentsCb
361 )
362 {
363 NTSTATUS nStatus;
364 const Elf_Ehdr * pehHeader;
365 const Elf_Phdr * pphPHdrs;
366 BOOLEAN fPageAligned;
367 ULONG nData;
368 ULONG nPHdrCount;
369 ULONG cbPHdrSize;
370 Elf_Off cbPHdrOffset;
371 PVOID pBuffer;
372 PMM_SECTION_SEGMENT pssSegments;
373 Elf_Addr nImageBase = 0;
374 Elf_Addr nEntryPoint;
375 ULONG32 nPrevVirtualEndOfSegment = 0;
376 ULONG i;
377 ULONG j;
378
379 (void)Intsafe_AddULong64;
380 (void)Intsafe_MulULong32;
381 (void)ElfFmtpReadULong64;
382 (void)ElfFmtpSafeReadULong64;
383 (void)ElfFmtpReadULong;
384
385 #define DIE(ARGS_) { DPRINT ARGS_; goto l_Return; }
386
387 pBuffer = NULL;
388
389 nStatus = STATUS_INVALID_IMAGE_FORMAT;
390
391 /* Ensure the file contains the full header */
392 /*
393 EXEFMT_LOAD_HEADER_SIZE is 8KB: enough to contain an ELF header (at least in
394 all the classes defined as of December 2004). If FileHeaderSize is less than
395 sizeof(Elf_Ehdr), it means the file itself is small enough not to contain a
396 full ELF header
397 */
398 ASSERT(sizeof(Elf_Ehdr) <= EXEFMT_LOAD_HEADER_SIZE);
399
400 if(FileHeaderSize < sizeof(Elf_Ehdr))
401 DIE(("The file is truncated, doesn't contain the full header\n"));
402
403 pehHeader = FileHeader;
404 ASSERT(((ULONG_PTR)pehHeader % TYPE_ALIGNMENT(Elf_Ehdr)) == 0);
405
406 nData = pehHeader->e_ident[EI_DATA];
407
408 /* Validate the header */
409 if(ElfFmtpReadUShort(pehHeader->e_ehsize, nData) < sizeof(Elf_Ehdr))
410 DIE(("Inconsistent value for e_ehsize\n"));
411
412 /* Calculate size and offset of the program headers */
413 cbPHdrSize = ElfFmtpReadUShort(pehHeader->e_phentsize, nData);
414
415 if(cbPHdrSize != sizeof(Elf_Phdr))
416 DIE(("Inconsistent value for e_phentsize\n"));
417
418 /* MAXUSHORT * MAXUSHORT < MAXULONG */
419 nPHdrCount = ElfFmtpReadUShort(pehHeader->e_phnum, nData);
420 ASSERT(Intsafe_CanMulULong32(cbPHdrSize, nPHdrCount));
421 cbPHdrSize *= nPHdrCount;
422
423 cbPHdrOffset = ElfFmtpReadOff(pehHeader->e_phoff, nData);
424
425 /* The initial header doesn't contain the program headers */
426 if(cbPHdrOffset > FileHeaderSize || cbPHdrSize > (FileHeaderSize - cbPHdrOffset))
427 {
428 NTSTATUS nReadStatus;
429 LARGE_INTEGER lnOffset;
430 PVOID pData;
431 ULONG cbReadSize;
432
433 /* Will worry about this when ELF128 comes */
434 ASSERT(sizeof(cbPHdrOffset) <= sizeof(lnOffset.QuadPart));
435
436 lnOffset.QuadPart = (LONG64)cbPHdrOffset;
437
438 /*
439 We can't support executable files larger than 8 Exabytes - it's a limitation
440 of the I/O system (only 63-bit offsets are supported). Quote:
441
442 [...] the total amount of printed material in the world is estimated to be
443 around a fifth of an exabyte. [...] [Source: Wikipedia]
444 */
445 if(lnOffset.u.HighPart < 0)
446 DIE(("The program header is too far into the file\n"));
447
448 nReadStatus = ReadFileCb
449 (
450 File,
451 &lnOffset,
452 cbPHdrSize,
453 &pData,
454 &pBuffer,
455 &cbReadSize
456 );
457
458 if(!NT_SUCCESS(nReadStatus))
459 {
460 nStatus = nReadStatus;
461 DIE(("ReadFile failed, status %08X\n", nStatus));
462 }
463
464 ASSERT(pData);
465 ASSERT(pBuffer);
466 ASSERT(Intsafe_CanOffsetPointer(pData, cbReadSize));
467
468 if(cbReadSize < cbPHdrSize)
469 DIE(("The file didn't contain the program headers\n"));
470
471 /* Force the buffer to be aligned */
472 if((ULONG_PTR)pData % TYPE_ALIGNMENT(Elf_Phdr))
473 {
474 ASSERT(((ULONG_PTR)pBuffer % TYPE_ALIGNMENT(Elf_Phdr)) == 0);
475 RtlMoveMemory(pBuffer, pData, cbPHdrSize);
476 pphPHdrs = pBuffer;
477 }
478 else
479 pphPHdrs = pData;
480 }
481 else
482 {
483 ASSERT(Intsafe_CanAddSizeT(cbPHdrOffset, 0));
484 ASSERT(Intsafe_CanOffsetPointer(FileHeader, cbPHdrOffset));
485 pphPHdrs = (PVOID)((ULONG_PTR)FileHeader + (ULONG_PTR)cbPHdrOffset);
486 }
487
488 /* Allocate the segments */
489 pssSegments = AllocateSegmentsCb(nPHdrCount);
490
491 if(pssSegments == NULL)
492 {
493 nStatus = STATUS_INSUFFICIENT_RESOURCES;
494 DIE(("Out of memory\n"));
495 }
496
497 ImageSectionObject->Segments = pssSegments;
498
499 fPageAligned = TRUE;
500
501 /* Fill in the segments */
502 for(i = 0, j = 0; i < nPHdrCount; ++ i)
503 {
504 switch(ElfFmtpSafeReadULong(&pphPHdrs[i].p_type, nData))
505 {
506 case PT_LOAD:
507 {
508 static const ULONG ProgramHeaderFlagsToProtect[8] =
509 {
510 PAGE_NOACCESS, /* 0 */
511 PAGE_EXECUTE_READ, /* PF_X */
512 PAGE_READWRITE, /* PF_W */
513 PAGE_EXECUTE_READWRITE, /* PF_X | PF_W */
514 PAGE_READONLY, /* PF_R */
515 PAGE_EXECUTE_READ, /* PF_X | PF_R */
516 PAGE_READWRITE, /* PF_W | PF_R */
517 PAGE_EXECUTE_READWRITE /* PF_X | PF_W | PF_R */
518 };
519
520 Elf_Size nAlignment;
521 Elf_Off nFileOffset;
522 Elf_Addr nVirtualAddr;
523 Elf_Size nAdj;
524 Elf_Size nVirtualSize = 0;
525 Elf_Size nFileSize = 0;
526
527 ASSERT(j <= nPHdrCount);
528
529 /* Retrieve and validate the segment alignment */
530 nAlignment = ElfFmtpSafeReadSize(&pphPHdrs[i].p_align, nData);
531
532 if(nAlignment == 0)
533 nAlignment = 1;
534 else if(!ElfFmtpIsPowerOf2(nAlignment))
535 DIE(("Alignment of loadable segment isn't a power of 2\n"));
536
537 if(nAlignment < PAGE_SIZE)
538 fPageAligned = FALSE;
539
540 /* Retrieve the addresses and calculate the adjustment */
541 nFileOffset = ElfFmtpSafeReadOff(&pphPHdrs[i].p_offset, nData);
542 nVirtualAddr = ElfFmtpSafeReadAddr(&pphPHdrs[i].p_vaddr, nData);
543
544 nAdj = ElfFmtpModPow2(nFileOffset, nAlignment);
545
546 if(nAdj != ElfFmtpModPow2(nVirtualAddr, nAlignment))
547 DIE(("File and memory address of loadable segment not congruent modulo alignment\n"));
548
549 /* Retrieve, adjust and align the file size and memory size */
550 if(!ElfFmtpAddSize(&nFileSize, ElfFmtpSafeReadSize(&pphPHdrs[i].p_filesz, nData), nAdj))
551 DIE(("Can't adjust the file size of loadable segment\n"));
552
553 if(!ElfFmtpAddSize(&nVirtualSize, ElfFmtpSafeReadSize(&pphPHdrs[i].p_memsz, nData), nAdj))
554 DIE(("Can't adjust the memory size of lodable segment\n"));
555
556 if(!ElfFmtpAlignUp(&nVirtualSize, nVirtualSize, nAlignment))
557 DIE(("Can't align the memory size of lodable segment\n"));
558
559 if(nFileSize > nVirtualSize)
560 nFileSize = nVirtualSize;
561
562 if(nVirtualSize > MAXULONG)
563 DIE(("Virtual image larger than 4GB\n"));
564
565 ASSERT(nFileSize <= MAXULONG);
566
567 pssSegments[j].Length = (ULONG)(nVirtualSize & 0xFFFFFFFF);
568 pssSegments[j].RawLength = (ULONG)(nFileSize & 0xFFFFFFFF);
569
570 /* File offset */
571 nFileOffset = ElfFmtpAlignDown(nFileOffset, nAlignment);
572
573 #if __ELF_WORD_SIZE >= 64
574 ASSERT(sizeof(nFileOffset) == sizeof(LONG64));
575
576 if(((LONG64)nFileOffset) < 0)
577 DIE(("File offset of loadable segment is too large\n"));
578 #endif
579
580 pssSegments[j].FileOffset = (LONG64)nFileOffset;
581
582 /* Virtual address */
583 nVirtualAddr = ElfFmtpAlignDown(nVirtualAddr, nAlignment);
584
585 if(j == 0)
586 {
587 /* First segment: its address is the base address of the image */
588 nImageBase = nVirtualAddr;
589 pssSegments[j].VirtualAddress = 0;
590
591 /* Several places make this assumption */
592 if(pssSegments[j].FileOffset != 0)
593 DIE(("First loadable segment doesn't contain the ELF header\n"));
594 }
595 else
596 {
597 Elf_Size nVirtualOffset;
598
599 /* Other segment: store the offset from the base address */
600 if(nVirtualAddr <= nImageBase)
601 DIE(("Loadable segments are not sorted\n"));
602
603 nVirtualOffset = nVirtualAddr - nImageBase;
604
605 if(nVirtualOffset > MAXULONG)
606 DIE(("Virtual image larger than 4GB\n"));
607
608 pssSegments[j].VirtualAddress = (ULONG)(nVirtualOffset & 0xFFFFFFFF);
609
610 if(pssSegments[j].VirtualAddress != nPrevVirtualEndOfSegment)
611 DIE(("Loadable segments are not sorted and contiguous\n"));
612 }
613
614 /* Memory protection */
615 pssSegments[j].Protection = ProgramHeaderFlagsToProtect
616 [
617 ElfFmtpSafeReadULong(&pphPHdrs[i].p_flags, nData) & (PF_R | PF_W | PF_X)
618 ];
619
620 /* Characteristics */
621 /*
622 TODO: need to add support for the shared, non-pageable, non-cacheable and
623 discardable attributes. This involves extensions to the ELF format, so it's
624 nothing to be taken lightly
625 */
626 if(pssSegments[j].Protection & PAGE_IS_EXECUTABLE)
627 {
628 ImageSectionObject->Executable = TRUE;
629 pssSegments[j].Characteristics = IMAGE_SCN_CNT_CODE;
630 }
631 else if(pssSegments[j].RawLength == 0)
632 pssSegments[j].Characteristics = IMAGE_SCN_CNT_UNINITIALIZED_DATA;
633 else
634 pssSegments[j].Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA;
635
636 /*
637 FIXME: see the TODO above. This is the safest way to load ELF drivers, for
638 now, if a bit wasteful of memory
639 */
640 pssSegments[j].Characteristics |= IMAGE_SCN_MEM_NOT_PAGED;
641
642 /* Copy-on-write */
643 pssSegments[j].WriteCopy = TRUE;
644
645 if(!Intsafe_AddULong32(&nPrevVirtualEndOfSegment, pssSegments[j].VirtualAddress, pssSegments[j].Length))
646 DIE(("Virtual image larger than 4GB\n"));
647
648 ++ j;
649 break;
650 }
651 }
652 }
653
654 if(j == 0)
655 DIE(("No loadable segments\n"));
656
657 ImageSectionObject->NrSegments = j;
658
659 *Flags =
660 EXEFMT_LOAD_ASSUME_SEGMENTS_SORTED |
661 EXEFMT_LOAD_ASSUME_SEGMENTS_NO_OVERLAP;
662
663 if(fPageAligned)
664 *Flags |= EXEFMT_LOAD_ASSUME_SEGMENTS_PAGE_ALIGNED;
665
666 nEntryPoint = ElfFmtpReadAddr(pehHeader->e_entry, nData);
667
668 if(nEntryPoint < nImageBase || nEntryPoint - nImageBase > nPrevVirtualEndOfSegment)
669 DIE(("Entry point not within the virtual image\n"));
670
671 ASSERT(nEntryPoint >= nImageBase);
672 ASSERT((nEntryPoint - nImageBase) <= MAXULONG);
673 ImageSectionObject->EntryPoint = nEntryPoint - nImageBase;
674
675 /* TODO: support Wine executables and read these values from nt_headers */
676 ImageSectionObject->ImageCharacteristics |=
677 IMAGE_FILE_EXECUTABLE_IMAGE |
678 IMAGE_FILE_LINE_NUMS_STRIPPED |
679 IMAGE_FILE_LOCAL_SYMS_STRIPPED |
680 (nImageBase > MAXULONG ? IMAGE_FILE_LARGE_ADDRESS_AWARE : 0) |
681 IMAGE_FILE_DEBUG_STRIPPED;
682
683 if(nData == ELFDATA2LSB)
684 ImageSectionObject->ImageCharacteristics |= IMAGE_FILE_BYTES_REVERSED_LO;
685 else if(nData == ELFDATA2MSB)
686 ImageSectionObject->ImageCharacteristics |= IMAGE_FILE_BYTES_REVERSED_HI;
687
688 /* Base address outside the possible address space */
689 if(nImageBase > MAXULONG_PTR)
690 ImageSectionObject->ImageBase = EXEFMT_LOAD_BASE_NONE;
691 /* Position-independent image, base address doesn't matter */
692 else if(nImageBase == 0)
693 ImageSectionObject->ImageBase = EXEFMT_LOAD_BASE_ANY;
694 /* Use the specified base address */
695 else
696 ImageSectionObject->ImageBase = (ULONG_PTR)nImageBase;
697
698 /* safest bet */
699 ImageSectionObject->Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI;
700 ImageSectionObject->MinorSubsystemVersion = 0;
701 ImageSectionObject->MajorSubsystemVersion = 4;
702
703 /* Success, at last */
704 nStatus = STATUS_SUCCESS;
705
706 l_Return:
707 if(pBuffer)
708 ExFreePool(pBuffer);
709
710 return nStatus;
711 }
712
713 /* EOF */