digitalmars.D - DMD generated exes' size differ from PE headers
- bobef (2/2) Jan 26 2010 I'm walking the PE header sections to find out the file size. I find the...
- Walter Bright (2/8) Jan 26 2010 I don't know. Could be alignment padding.
- BCS (4/11) Jan 26 2010 What are the 4 bytes? Can you change what they are by tweaking things?
- bobef (2/19) Jan 27 2010 Yep, they seem to change.
- bobef (21/31) Jan 27 2010 I found some article on MSDN that says that SizeOfRawData includes the a...
I'm walking the PE header sections to find out the file size. I find the maximum PointerToRawData and add SizeOfRawData of the section to find where the exe ends. It works with the files I've tested. When compiling exe file with DMD 1.055 and -g there are additional four bytes after the last section. Why is that? Is SizeOfRawData wrong or there is something else at the end? Thanks
Jan 26 2010
bobef wrote:I'm walking the PE header sections to find out the file size. I find the maximum PointerToRawData and add SizeOfRawData of the section to find where the exe ends. It works with the files I've tested. When compiling exe file with DMD 1.055 and -g there are additional four bytes after the last section. Why is that? Is SizeOfRawData wrong or there is something else at the end?I don't know. Could be alignment padding.
Jan 26 2010
Hello bobef,I'm walking the PE header sections to find out the file size. I find the maximum PointerToRawData and add SizeOfRawData of the section to find where the exe ends. It works with the files I've tested. When compiling exe file with DMD 1.055 and -g there are additional four bytes after the last section. Why is that? Is SizeOfRawData wrong or there is something else at the end?What are the 4 bytes? Can you change what they are by tweaking things? -- <IXOYE><
Jan 26 2010
BCS Wrote:Hello bobef,Yep, they seem to change.I'm walking the PE header sections to find out the file size. I find the maximum PointerToRawData and add SizeOfRawData of the section to find where the exe ends. It works with the files I've tested. When compiling exe file with DMD 1.055 and -g there are additional four bytes after the last section. Why is that? Is SizeOfRawData wrong or there is something else at the end?What are the 4 bytes? Can you change what they are by tweaking things? -- <IXOYE><
Jan 27 2010
Walter Bright Wrote:bobef wrote:I found some article on MSDN that says that SizeOfRawData includes the alignment padding and something other was indicating the actual size, so this shouldn't be the case. I do like this: uint peFileSize(void[] data) { auto dos_header = cast(PIMAGE_DOS_HEADER)data; if(dos_header.e_magic != IMAGE_DOS_SIGNATURE) return -1; auto nt_header = cast(PIMAGE_NT_HEADERS)&data[dos_header.e_lfanew]; if(nt_header.Signature != IMAGE_NT_SIGNATURE) return -1; uint maxpointer, exesize; PIMAGE_SECTION_HEADER section = IMAGE_FIRST_SECTION(nt_header); for(uint i=0; i<nt_header.FileHeader.NumberOfSections; i++, section++) { if(section.PointerToRawData > maxpointer) { maxpointer = section.PointerToRawData; exesize = section.PointerToRawData + section.SizeOfRawData; } } return exesize; } and if I do exesize = section.PointerToRawData + section.SizeOfRawData + section.SizeOfRawData%nt_header.OptionalHeader.SectionAlignment; then I get wrong results don't know why.I'm walking the PE header sections to find out the file size. I find the maximum PointerToRawData and add SizeOfRawData of the section to find where the exe ends. It works with the files I've tested. When compiling exe file with DMD 1.055 and -g there are additional four bytes after the last section. Why is that? Is SizeOfRawData wrong or there is something else at the end?I don't know. Could be alignment padding.
Jan 27 2010