a) I take it that the header is a part of the image, so you have to define it as section .text and use section .code for the code. Otherwise the header gets folded into the code section. If code is written without a section directive, then its default is given as section .text, section .text gets placed first in the image - 'follows=' doesn't work for trying to change its natural order.
b) the vstart is needed if you want addresses in the section to org at zero, otherwise the addresses follow the previous named section. ( all .text section are grouped, all .data sections are grouped, but .data follows .text, so the last .text section should end with the ALIGN ).
See if this gets you closer..
make file contained:
-f bin
-l exetest3.lst
-o exetest3.bin
-Z exetest3.err
exetest3.nsm
; exetest3.nsm
[MAP ALL exetest3.map] ;; use this to generate a MAP file of useful information.
SECTIONS equ 2
TIME_DATE equ 0
IMAGE_BASE equ 0x400000
SECTION_ALIGNMENT equ 0x1000
FILE_ALIGNMENT equ 0x200
BSS_SIZE equ 0
%define nagoa_round(size) ((size + SECTION_ALIGNMENT - 1) & ~(SECTION_ALIGNMENT - 1))
bits 32
org IMAGE_BASE
; ******************************************************************************************************
SECTION .text ;; if the header is part of the image, define it as section .text because .text is put
;; first in the image, and rename the code section as section .code
header:
dw "MZ" ; e_magic
dw 0 ; e_cblp
dw 0 ; e_cp
dw 0 ; e_crlc
dw 0 ; e_cparhdr
dw 0 ; e_minalloc
dw 0 ; e_maxalloc
dw 0 ; e_ss
dw 0 ; e_sp
dw 0 ; e_csum
dw 0 ; e_ip
dw 0 ; e_cs
dw 0 ; e_lsarlc
dw 0 ; e_ovno
dq 0 ; e_res
dw 0 ; e_oemid
dw 0 ; e_oeminfo
dq 0, 0 ; e_res2
dd imageHeader - IMAGE_BASE ; e_lfanew
imageHeader:
dd "PE" ; Signature
dw 0x014C ; Machine
dw SECTIONS ; NumberOfSections
dd TIME_DATE ; TimeDateStamp
dd 0 ; PointerToSymbolTable
dd 0 ; NumberOfSymbols
dw optionalHeader.SIZE ; SizeOfOptionalHeader
dw 0x0303 ; Characteristics
optionalHeader:
dw 0x10B ; Magic
db 0 ; MajorLinkerVersion
db 0 ; MinorLinkerVersion
dd nagoa_round(RAW_CODE.SIZE) ; SizeOfCode
dd nagoa_round(RAW_DATA.SIZE) ; SizeOfInitializedData
dd nagoa_round(BSS_SIZE) ; SizeOfUninitializedData
dd entryPoint - IMAGE_BASE ; AddressOfEntryPoint
dd code - IMAGE_BASE ; BaseOfCode
dd data - IMAGE_BASE ; BaseOfData
dd IMAGE_BASE ; ImageBase
dd SECTION_ALIGNMENT ; SectionAlignment
dd FILE_ALIGNMENT ; FileAlignment
dw 4 ; MajorOperatingSystemVersion
dw 0 ; MinorOperatingSystemVersion
dw 0 ; MajorImageVersion
dw 0 ; MinorImageVersion
dw 4 ; MajorSubsystemVersion
dw 0 ; MinorSubsystemVersion
dd 0 ; Win32VersionValue
;; dd IMAGE_END - IMAGE_BASE ; SizeOfImage
dd (RAW_HEADER.SIZE + RAW_CODE.SIZE + RAW_DATA.SIZE)
dd RAW_HEADER.SIZE ; SizeOfHeaders
dd 0 ; CheckSum
dw 2 ; Subsystem
dw 0 ; DllCharacteristics
dd 0x40000 ; SizeOfStackReserve
dd 0x6000 ; SizeOfStackCommit
dd 0x100000 ; SizeOfHeapReserve
dd 0x1000 ; SizeOfHeapCommit
dd 0 ; LoaderFlags
dd 16 ; NumberOfRvaAndSizes
dd 0, 0 ; IMAGE_DIRECTORY_ENTRY_EXPORT
dd 0, 0 ; IMAGE_DIRECTORY_ENTRY_IMPORT
dd 0, 0 ; IMAGE_DIRECTORY_ENTRY_RESOURCE
dd 0, 0 ; IMAGE_DIRECTORY_ENTRY_EXCEPTION
dd 0, 0 ; IMAGE_DIRECTORY_ENTRY_SECURITY
dd 0, 0 ; IMAGE_DIRECTORY_ENTRY_BASERELOC
dd 0, 0 ; IMAGE_DIRECTORY_ENTRY_DEBUG
dd 0, 0 ; IMAGE_DIRECTORY_ENTRY_COPYRIGHT
dd 0, 0 ; IMAGE_DIRECTORY_ENTRY_GLOBALPTR
dd 0, 0 ; IMAGE_DIRECTORY_ENTRY_TLS
dd 0, 0 ; IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
dd 0, 0 ; IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
dd 0, 0 ; IMAGE_DIRECTORY_ENTRY_IAT
dd 0, 0 ; IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT
dd 0, 0 ; IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
dd 0, 0 ; reserved
optionalHeader.SIZE equ $ - optionalHeader
sectionHeaders:
db ".text", 0, 0, 0 ; Name
dd nagoa_round(RAW_CODE.SIZE) ; VirtualSize
dd code ; VirtualAddress
dd RAW_CODE.SIZE ; SizeOfRawData
dd RAW_CODE.OFFSET ; PointerToRawData
dd 0 ; PointerToRelocations
dd 0 ; PointerToLinenumbers
dw 0 ; NumberOfRelocations
dw 0 ; NumberOfLinenumbers
dd 0x60000020 ; Characteristics
db ".data", 0, 0, 0 ; Name
dd nagoa_round(RAW_DATA.SIZE) ; VirtualSize
dd data ; VirtualAddress
dd RAW_DATA.SIZE ; SizeOfRawData
dd RAW_DATA.OFFSET ; PointerToRawData
dd 0 ; PointerToRelocations
dd 0 ; PointerToLinenumbers
dw 0 ; NumberOfRelocations
dw 0 ; NumberOfLinenumbers
dd 0xC0000040 ; Characteristics
align FILE_ALIGNMENT, db 0
RAW_HEADER.SIZE equ $ - header
; ******************************************************************************************************
;;section .text vstart=nagoa_round($)
section .code
code:
entryPoint:
xor eax, eax
ret
align FILE_ALIGNMENT, db 0
RAW_CODE.OFFSET equ RAW_HEADER.SIZE
RAW_CODE.SIZE equ $ - code ;; $-$$ also gives the size of section .code
; ******************************************************************************************************
;;section .data vstart=nagoa_round($)
section .data
data:
db "An unused string"
align FILE_ALIGNMENT, db 0
RAW_DATA.OFFSET equ RAW_CODE.OFFSET + RAW_CODE.SIZE
RAW_DATA.SIZE equ $ - data ;; $-$$ also gives the size of section .data
; ******************************************************************************************************
;;IMAGE_END equ nagoa_round($)
IMAGE_END equ $
;; -- eof --