Ok so I understand that the role of the "DOS Stub" in a PE file is to: firstly check if the file is being run in DOS mode, if it is then it displays the error "This file cannot be run in DOS mode".
If not, then it maps each of the files sections then loads it into memory, and then executes it.

Does anyone know of an open-source DOS Stub / loader that is equivelant to the windows one, or know how I would go about writing one?