eGospodarka.pl
eGospodarka.pl poleca

eGospodarka.plGrupypl.comp.programming(announce) organic asm › Re: [OT] (announce) organic asm
  • Data: 2017-09-11 16:55:15
    Temat: Re: [OT] (announce) organic asm
    Od: fir <p...@g...com> szukaj wiadomości tego autora
    [ pokaż wszystkie nagłówki ]

    ok bss ez dodalem, (wystarczy sekcje data zadeklarowac jako o rezerwe wiekszą a
    rozmiar w pliku podac jako tylko tym z wypelnionymi danymi i po prostu uzywac tej
    resztki jako ststycznego bss-ramu

    w sumie pewnie moge wkleiic ten kod ktory nagywa header exe na dysk - u mnie to sa
    tylko minimalne pola tkniete ale dziala



    void BuildAndSavePE(char* filename)
    {
    FILE *file = fopen(filename, "wb");

    ////////////////////////////////////////////////////
    ////////////////////// pe headers /////////////////////////////

    IMAGE_DOS_HEADER dosHeader= {0};
    dosHeader.e_magic = 0x5a4d; // MZ
    dosHeader.e_lfanew = 0x00000040; //pe header offset in file

    //0x40
    IMAGE_NT_HEADERS ntHeader= {0};
    ntHeader.Signature = 0x00004550; //PE 0 0

    IMAGE_FILE_HEADER header= {0};
    header.Machine = 0x014c; //i386
    header.NumberOfSections = 3;
    header.SizeOfOptionalHeader = 0x00e0; //not sure
    header.Characteristics = 0x0002 + 0x0100; //executable + 32bit_machine
    //0x58
    IMAGE_OPTIONAL_HEADER opHeader= {0}; //Optional Header of PE files present in
    NT Header structure
    opHeader.Magic = 0x010b;
    opHeader.AddressOfEntryPoint = Give_CodeSection_RamOffset();
    opHeader.ImageBase = pe_base_ram_adr;
    opHeader.SectionAlignment = ram_section_alignment;
    opHeader.FileAlignment = file_section_alignment;
    opHeader.MajorSubsystemVersion = 4;
    opHeader.SizeOfImage = GiveRamImageSize(bss_reserve);
    opHeader.SizeOfHeaders = file_section_alignment;

    if(show_console)
    opHeader.Subsystem = 3; //2-gui, 3 - console
    else
    opHeader.Subsystem = 2; //2-gui, 3 - console

    opHeader.NumberOfRvaAndSizes = 16;

    //0x0138
    opHeader.DataDirectory[1].VirtualAddress = Give_ImportsSection_RamOffset();

    ////////////////////////////////////////////////////
    /////
    ////////////////////////////////////////////////////
    ///////////////

    IMAGE_SECTION_HEADER secHeaderCode= {0};

    secHeaderCode.Name[0] = '.';
    secHeaderCode.Name[1] = 'c';
    secHeaderCode.Name[2] = 'o';
    secHeaderCode.Name[3] = 'd';
    secHeaderCode.Name[4] = 'e';

    secHeaderCode.Misc.VirtualSize = AlignNumberUp( Give_Code_UnalignedSize() ,
    ram_section_alignment);
    secHeaderCode.VirtualAddress = Give_CodeSection_RamOffset();

    secHeaderCode.SizeOfRawData = AlignNumberUp( Give_Code_UnalignedSize() ,
    file_section_alignment);

    secHeaderCode.PointerToRawData = Give_CodeSection_FileOffset();

    secHeaderCode.Characteristics = 0x00000020 + 0x20000000 + 0x40000000;

    /////
    IMAGE_SECTION_HEADER secHeaderImports= {0};

    secHeaderImports.Name[0] = '.';
    secHeaderImports.Name[1] = 'i';
    secHeaderImports.Name[2] = 'm';
    secHeaderImports.Name[3] = 'p';
    secHeaderImports.Name[4] = 'o';
    secHeaderImports.Name[5] = 'r';
    secHeaderImports.Name[6] = 't';
    secHeaderImports.Name[7] = 's';

    secHeaderImports.Misc.VirtualSize = AlignNumberUp( Give_Imports_UnalignedSize()
    , ram_section_alignment);
    secHeaderImports.VirtualAddress = Give_ImportsSection_RamOffset();
    secHeaderImports.SizeOfRawData = AlignNumberUp( Give_Imports_UnalignedSize() ,
    file_section_alignment);;
    secHeaderImports.PointerToRawData = Give_ImportsSection_FileOffset();
    secHeaderImports.Characteristics = 0x00000040 + 0x40000000;


    //////
    IMAGE_SECTION_HEADER secHeaderData= {0};

    secHeaderData.Name[0] = '.';
    secHeaderData.Name[1] = 'd';
    secHeaderData.Name[2] = 'a';
    secHeaderData.Name[3] = 't';
    secHeaderData.Name[4] = 'a';


    secHeaderData.Misc.VirtualSize = AlignNumberUp( Give_Data_UnalignedSize() ,
    ram_section_alignment)
    + AlignNumberUp( bss_reserve ,
    ram_section_alignment) ;
    secHeaderData.VirtualAddress = Give_DataSection_RamOffset() ;
    secHeaderData.SizeOfRawData = AlignNumberUp( Give_Data_UnalignedSize() ,
    file_section_alignment);
    secHeaderData.PointerToRawData = Give_DataSection_FileOffset();
    secHeaderData.Characteristics = 0x00000040 + 0x40000000 +0x80000000 ;

    ////////

    fwrite((char*)&dosHeader, 1, sizeof(dosHeader), file);

    fwrite((char*)&ntHeader, 1, 4, file);
    fwrite((char*)&header, 1, sizeof(header), file);
    fwrite((char*)&opHeader, 1, sizeof(opHeader), file);
    fwrite((char*)&secHeaderCode, 1, sizeof(secHeaderCode), file);
    fwrite((char*)&secHeaderImports, 1, sizeof(secHeaderImports), file);
    fwrite((char*)&secHeaderData, 1, sizeof(secHeaderData), file);


    int header_size = + sizeof(dosHeader)
    + 4
    + sizeof(header)
    + sizeof(opHeader)
    + sizeof(secHeaderCode)
    + sizeof(secHeaderImports)
    + sizeof(secHeaderData);

    ////////////////////////////////////////////////////
    ////////////////////// code /////////////////////////////
    fseek( file, Give_CodeSection_FileOffset(), SEEK_SET );
    fwrite(code, 1, AlignNumberUp( Give_Code_UnalignedSize() ,
    file_section_alignment), file);

    fseek( file, Give_ImportsSection_FileOffset(), SEEK_SET );
    fwrite((char*)idata, 1, AlignNumberUp( Give_Imports_UnalignedSize() ,
    file_section_alignment), file);

    fseek( file, Give_DataSection_FileOffset(), SEEK_SET );
    fwrite(data, 1, AlignNumberUp( Give_Data_UnalignedSize() ,
    file_section_alignment), file);

    ///// /// save last byte and exit
    char zero =0;
    fseek( file, GiveFileSize() - 1, SEEK_SET );
    fwrite((char*)&zero, 1, 1, file);
    fclose(file);

    }


    dobrze ze ten asmik dziala i w sumie nie wykluczone ze napisze jeszcze pare tekstow
    na ten temat
    1) bo to ciekawsze niz hallo worldy a rownoczesnie nie jakos strasznie duze
    spokojnie daloby sie omowic nawet na grupie w calosci (big hallo world)
    2) jest dobrym krokiem wyjscia do innych ciekawych tematow

Podziel się

Poleć ten post znajomemu poleć

Wydrukuj ten post drukuj


Następne wpisy z tego wątku

Najnowsze wątki z tej grupy


Najnowsze wątki

Szukaj w grupach

Eksperci egospodarka.pl

1 1 1

Wpisz nazwę miasta, dla którego chcesz znaleźć jednostkę ZUS.

Wzory dokumentów

Bezpłatne wzory dokumentów i formularzy.
Wyszukaj i pobierz za darmo: