flat assembler
Message board for the users of flat assembler.

Index > Main > [fasmg] multibytes nop filler for package align

Author
Thread Post new topic Reply to topic
sylware



Joined: 23 Oct 2020
Posts: 407
Location: Marseille/France
sylware 20 Jan 2023, 12:48
I saw the align calm instruction from the "package" repository on github.

Any nice multibytes nop filler for it anywhere?
Post 20 Jan 2023, 12:48
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8340
Location: Kraków, Poland
Tomasz Grysztar 20 Jan 2023, 13:40
You could take ones used by HeavyThing:
Code:
struc bstr? bytes&
        virtual at 0
                db bytes
                load . : $ from 0
        end virtual
end struc

define aligncode aligncode
namespace aligncode
        ?15 bstr 0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, \
                 0x66, 0xf, 0x1f, 0x44, 0x00, 0x00
        ?14 bstr 0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, \
                 0xf, 0x1f, 0x44, 0x00, 0x00
        ?13 bstr 0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, \
                 0xf, 0x1f, 0x40, 0x00
        ?12 bstr 0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, \
                 0xf, 0x1f, 0x00
        ?11 bstr 0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, \
                 0x66, 0x90
        ?10 bstr 0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, \
                 0x90
        ?9 bstr 0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
        ?8 bstr 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
        ?7 bstr 0xf, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00
        ?6 bstr 0x66, 0xf, 0x1f, 0x44, 0x00, 0x00
        ?5 bstr 0xf, 0x1f, 0x44, 0x00, 0x00
        ?4 bstr 0xf, 0x1f, 0x40, 0x00
        ?3 bstr 0xf, 0x1f, 0x00
        ?2 bstr 0x66, 0x90
        ?1 bstr 0x90
        ?0 := ''
end namespace    
and a filler like:
Code:
calminstruction codepad length*
        compute length, length
        arrange length, =db =aligncode.length
        assemble length
end calminstruction    
Post 20 Jan 2023, 13:40
View user's profile Send private message Visit poster's website Reply with quote
sylware



Joined: 23 Oct 2020
Posts: 407
Location: Marseille/France
sylware 20 Jan 2023, 14:24
Thx! I'll give it a try with your package align calm instruction.
Post 20 Jan 2023, 14:24
View user's profile Send private message Reply with quote
sylware



Joined: 23 Oct 2020
Posts: 407
Location: Marseille/France
sylware 21 Jan 2023, 01:51
(edited with a slightly less quick and dirty version)

Did not manage to make work the utility code, then I wrote a brutal and simple one for my needs with my limited understanding of CALM:

Code:
calminstruction align_nops alignment_log2*
        local addr,aligned_up_addr,room,nop_bytes_n,cmd

        compute alignment_log2, alignment_log2

        check   ELF.SECTION_ALIGN mod (1 shl alignment_log2) = 0
        jyes    allowed
        arrange cmd, =err "section not aligned enough"
        assemble cmd
        exit
allowed:
        compute addr,$ scale 0
        compute aligned_up_addr, (addr + ((1 shl alignment_log2) - 1)) and (not ((1 shl alignment_log2) - 1))
addr_test:
        check  aligned_up_addr = addr
                jno do_fill
        exit
do_fill:
        compute room, aligned_up_addr - addr

        check room >= 16
                jyes head_body
tail:
        ; room < 16, tail processing, probably the most common.
        check room = 15
                jyes nop_bytes_n_15_fill
        check room = 14
                jyes nop_bytes_n_14_fill
        check room = 13
                jyes nop_bytes_n_13_fill
        check room = 12
                jyes nop_bytes_n_12_fill
        check room = 11
                jyes nop_bytes_n_11_fill
        check room = 10
                jyes nop_bytes_n_10_fill
        check room = 9
                jyes nop_bytes_n_9_fill
        check room = 8
                jyes nop_bytes_n_8_fill
        check room = 7
                jyes nop_bytes_n_7_fill
        check room = 6
                jyes nop_bytes_n_6_fill
        check room = 5
                jyes nop_bytes_n_5_fill
        check room = 4
                jyes nop_bytes_n_4_fill
        check room = 3
                jyes nop_bytes_n_3_fill
        check room = 2 
                jyes nop_bytes_n_2_fill
        jump nop_bytes_n_1_fill

head_body:
        ; room >= 16
        ; We re-align asap on a 16bytes pick/fetch window the head.
        check (addr and 0xf) = 0
                jyes nop_bytes_n_16_fill
        check (addr and 0xf) = 1
                jyes nop_bytes_n_15_fill
        check (addr and 0xf) = 2
                jyes nop_bytes_n_14_fill
        check (addr and 0xf) = 3
                jyes nop_bytes_n_13_fill
        check (addr and 0xf) = 4
                jyes nop_bytes_n_12_fill
        check (addr and 0xf) = 5
                jyes nop_bytes_n_11_fill
        check (addr and 0xf) = 6
                jyes nop_bytes_n_10_fill
        check (addr and 0xf) = 7
                jyes nop_bytes_n_9_fill
        check (addr and 0xf) = 8
                jyes nop_bytes_n_8_fill
        check (addr and 0xf) = 9
                jyes nop_bytes_n_7_fill
        check (addr and 0xf) = 10
                jyes nop_bytes_n_6_fill
        check (addr and 0xf) = 11
                jyes nop_bytes_n_5_fill
        check (addr and 0xf) = 12
                jyes nop_bytes_n_4_fill
        check (addr and 0xf) = 13
                jyes nop_bytes_n_3_fill
        check (addr and 0xf) = 14
                jyes nop_bytes_n_2_fill
        jump nop_bytes_n_1_fill

nop_bytes_n_16_fill:
        arrange cmd, =db        0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, \
                                0x66, 0xf, 0x1f, 0x44, 0x00, 0x00, \
                                0x90
        compute nop_bytes_n, 16
        jump next_addr

nop_bytes_n_15_fill:
        arrange cmd, =db        0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, \
                                0x66, 0xf, 0x1f, 0x44, 0x00, 0x00
        compute nop_bytes_n, 15
        jump next_addr

nop_bytes_n_14_fill:
        arrange cmd, =db        0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, \
                                0xf, 0x1f, 0x44, 0x00, 0x00
        compute nop_bytes_n, 14
        jump next_addr

nop_bytes_n_13_fill:
        arrange cmd, =db        0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, \
                                0xf, 0x1f, 0x40, 0x00
        compute nop_bytes_n, 13
        jump next_addr

nop_bytes_n_12_fill:
        arrange cmd, =db        0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, \
                                0xf, 0x1f, 0x00
        compute nop_bytes_n, 12
        jump next_addr

nop_bytes_n_11_fill:
        arrange cmd, =db        0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, \
                                0x66, 0x90
        compute nop_bytes_n, 11
        jump next_addr

nop_bytes_n_10_fill:
        arrange cmd, =db        0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00, \
                                0x90
        compute nop_bytes_n, 10
        jump next_addr

nop_bytes_n_9_fill:
        arrange cmd, =db        0x66, 0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
        compute nop_bytes_n, 9
        jump next_addr

nop_bytes_n_8_fill:
        arrange cmd, =db        0xf, 0x1f, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
        compute nop_bytes_n, 8
        jump next_addr

nop_bytes_n_7_fill:
        arrange cmd, =db        0xf, 0x1f, 0x80, 0x00, 0x00, 0x00, 0x00
        compute nop_bytes_n, 7
        jump next_addr

nop_bytes_n_6_fill:
        arrange cmd, =db        0x66, 0xf, 0x1f, 0x44, 0x00, 0x00
        compute nop_bytes_n, 6
        jump next_addr

nop_bytes_n_5_fill:
        arrange cmd, =db        0xf, 0x1f, 0x44, 0x00, 0x00
        compute nop_bytes_n, 5
        jump next_addr

nop_bytes_n_4_fill:
        arrange cmd, =db        0xf, 0x1f, 0x40, 0x00
        compute nop_bytes_n, 4
        jump next_addr

nop_bytes_n_3_fill:
        arrange cmd, =db        0xf, 0x1f, 0x00
        compute nop_bytes_n, 3
        jump next_addr

nop_bytes_n_2_fill:
        arrange cmd, =db        0x66, 0x90
        compute nop_bytes_n, 2
        jump next_addr

nop_bytes_n_1_fill:
        arrange cmd, =db        0x90
        compute nop_bytes_n, 1
next_addr:
        assemble cmd
        compute addr, addr + nop_bytes_n
        jump addr_test
end calminstruction    


Last edited by sylware on 21 Jan 2023, 16:53; edited 1 time in total
Post 21 Jan 2023, 01:51
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8340
Location: Kraków, Poland
Tomasz Grysztar 21 Jan 2023, 09:19
sylware wrote:
Did not manage to make work the utility code
Did it not work because of conflict between the syntax of "align" used by some of the fasm-compatible formatters and the universal one? I think I should do something to unify the syntax, or at least make it non-conflicting. Maybe I should preserve the syntax like:
Code:
align 16, 0    
and a for a customized filler macro require some additional modifier, like:
Code:
align 16, nops #    
In fact, this new syntax could even allow to specify where exactly to put the count of bytes in the invoked command:
Code:
align 16, db # dup 90h    
It should be easy to implement.
Post 21 Jan 2023, 09:19
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8340
Location: Kraków, Poland
Tomasz Grysztar 21 Jan 2023, 09:51
I updated the package: https://github.com/tgrysztar/fasmg/commit/9695d490a2d3b7457841a85f9a3cf1fe14125851
Now you can use it with the macro this way:
Code:
align 16, codepad #    
and it maintains compatibility with the standard formatters. Under relocatable formats you may need to add some "align.assume" statements, though.
Post 21 Jan 2023, 09:51
View user's profile Send private message Visit poster's website Reply with quote
sylware



Joined: 23 Oct 2020
Posts: 407
Location: Marseille/France
sylware 21 Jan 2023, 12:17
(the calm macro was edited with a less quick and dirty version)

Since we would expect code sections to be aligned at least on a code pick/fetch window (power of 2 >= 16bytes), my filler should re-align to a 16bytes window first and I miss a 16bytes block filler.

Sorry, but I don't understand how your universal align works with elf64.inc (which does have its own align).


Last edited by sylware on 21 Jan 2023, 16:54; edited 1 time in total
Post 21 Jan 2023, 12:17
View user's profile Send private message Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8340
Location: Kraków, Poland
Tomasz Grysztar 21 Jan 2023, 12:36
sylware wrote:
Sorry, but I don't understand how your universal align works with elf64.inc (which does have its own align).
A working example:
Code:
include 'format/format.inc'

format ELF64

include 'align.inc'


section '.text' executable align 16
align.assume $, 16

start:
        int3

        align   16, db # dup 90h
myproc:    
You could add a SECTION wrapper to synchronize the alignment settings automatically:
Code:
include 'format/format.inc'

format ELF64

include 'align.inc'

macro section? declaration*
        section declaration
        match any =align? number, declaration
                align.assume $, number
        else
                align.assume $, 4
        end match
end macro
postpone
        purge section?
end postpone


section '.text' executable align 16

start:
        int3

        align   16, db # dup 90h
myproc:    
Post 21 Jan 2023, 12:36
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.