Maybe something like this (attached)?

This program opens up an exe and re-writes the stub. The resulting code will launch ".\16.exe" (IOW a file named 16.exe in the current directory) when run as 16 bit. It's got commenting and error checking.

It's not exactly what you were asking for, but perhaps it can give you what you need?

dw

On 12/5/2014 2:26 PM, bulk 88 wrote:
> Date: Fri, 5 Dec 2014 09:49:55 +0100
> From: ktiet...@googlemail.com
> To: mingw-w64-public@lists.sourceforge.net
> Subject: Re: [Mingw-w64-public] adding dos stub program with ld to PE binary
>
> 2014-12-05 6:45 GMT+01:00 bulk88 <bul...@hotmail.com>:
> > What is the GCC ld equivalent of Visual C's -stub ?
> > http://msdn.microsoft.com/en-us/library/7z0585h5.aspx I need to add a MZ
> > header-ed dos program to my PE binary instead of the default "This
> > program cannot be run in DOS mode" program.
> >
>
> Hmm, interesting option. AFAI know does ld do not provide such
> feature. It wouldn't be too hard to implement it.
> So sorry, for now this feature isn't present.
>
> Kai

I found the default dos header lives in _bfd_XXi_only_swap_filehdr_out() but that code is beyond what I can do.

// dos2.cpp : Re-writes stub in executable

#include <stdio.h>

#define BYTESINPARAGRAPH 16

typedef unsigned short WORD;
typedef long LONG;

typedef struct _IMAGE_DOS_HEADER {
    WORD   e_magic;
    WORD   e_cblp;
    WORD   e_cp;
    WORD   e_crlc;
    WORD   e_cparhdr;
    WORD   e_minalloc;
    WORD   e_maxalloc;
    WORD   e_ss;
    WORD   e_sp;
    WORD   e_csum;
    WORD   e_ip;
    WORD   e_cs;
    WORD   e_lfarlc;
    WORD   e_ovno;
    WORD   e_res[4];
    WORD   e_oemid;
    WORD   e_oeminfo;
    WORD   e_res2[10];
    LONG   e_lfanew;
  } IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;

/* Here's the code that's (currently) in newcode:

; Adjust the program size to 20 paragraphs
mov bx,20
mov ah,4a
int 21

; Set ds and es to cs
push cs
pop ds
push cs
pop es

; Exe name is at DS:DX
; Parameter block is at ES:BX
mov ax,4b00
mov bx,1a
mov dx,28
int 21

; Exit using return code from "EXEC" as exit code
mov ah, 4c
int 21

; Followed by 14 0 bytes for the parameter block
; and the asciiz file name (".\16.exe") to run.

*/

const unsigned char newcode[] = {
    0xBB, 0x20, 0x00, 0xB4, 0x4A, 0xCD, 0x21, 0x0E, 
    0x1F, 0x0E, 0x07, 0xB8, 0x00, 0x4B, 0xBB, 0x1A, 
    0x00, 0xBA, 0x28, 0x00, 0xCD, 0x21, 0xB4, 0x4C, 
    0xCD, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
    '.', '\\', '1', '6', '.', 'e', 'x', 'e', 0x00};

int main(int argc, char *argv[])
{
    if (sizeof(newcode) > 64)
    {
        printf("new code block is too big.\n");
        return 1;
    }

    if (argc != 2)
    {
        printf("Usage: dos2 <filename>\n\nRe-writes stub in executable.\n");
        return 1;
    }

    FILE *f = fopen(argv[1], "r+");
    if (f == NULL)
    {
        printf("Can't open %S.\n", argv[1]);
        return 1;
    }

    IMAGE_DOS_HEADER dh;
    size_t bytesread = fread(&dh, 1, sizeof(dh), f);

    if (bytesread < sizeof(dh))
    {
        printf("Only read %d bytes instead of %d.\n", bytesread, sizeof(dh));
        fclose(f);
        return 1;
    }

    if (dh.e_magic != 0x5A4D) // "MZ"
    {
        printf("Missing MZ signature.\n");
        fclose(f);
        return 1;
    }

    if (dh.e_cparhdr < 4)
    {
        printf("Header not big enough to be PE.\n");
        fclose(f);
        return 1;
    }

    if (dh.e_lfanew == 0)
    {
        printf("No new exe header.\n");
        fclose(f);
        return 1;
    }

    int seekres = fseek(f, dh.e_cparhdr * BYTESINPARAGRAPH, SEEK_SET);

    if (seekres != 0)
    {
        printf("Can't seek to offset %d.\n", dh.e_cparhdr * BYTESINPARAGRAPH);
        fclose(f);
        return 1;
    }

    int writeres = fwrite(newcode, 1, sizeof(newcode), f);

    if (writeres != sizeof(newcode))
    {
        printf("Only wrote %d bytes instead of %d.\n", writeres, 
sizeof(newcode));
        fclose(f);
        return 1;
    }

    fclose(f);
    printf("Operation succeeded!\n");

    return 0;
}
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to