GNU Assembler (GAS) query

  • Follow


Hi all,

I have a requirement where I need to interface some assembly routine
with C program (without using inline assembly). In the C file, I make
a call to assembly routine while passing _quite_ a few arguments to
the routine. The arguments being passed in the C function call need to
be used locally in the  assembly routine. For this purpose, I need
local variables corresponding to each function argument and need to
move the stack value (being pushed due to function call) of an
argument to corresponding local variable. So at a high level whole
thing might look something like this:

//foo.c

x = asm_func(_a, _b, _c);


//bar.s

..section .data

a:
          .int 0
b:
          .int 0
c:
          .int 0

..section .text

..global asm_func

asm_func:

          movl 8(%ebp), %eax    # a = _a
          movl %eax, a

          movl 12(%ebp), %eax  # b = _b
          movl %eax, b

          movl 16(%ebp), %eax  # c = _c
          movl %eax, c


# remaining instructions follow

          .

          .

          .

          ret

Apparently, MASM has following syntax for achieving the above:

a equ [ebp + 8]

b equ [ebp + 12]

c equ [ebp + 16]

But GAS's .equ (or .equiv and .set) is not equivalent to MASM's equ
directive. Is there any other (cleaner) way in GAS for assigning the
function
arguments on stack to local variables in assembly code apart from
above?

Another doubt that I have is about writing SMC (self-modifying code)
in GAS.  To do SMC in assembly, I need to allow the code area to be
writable. I tried doing ".section .text[rwx]" but somehow it gives me
segmentation fault. In MASM, I think you can alias code and data
segments by using public keyword. I am looking for how to do it in
GAS. I was unable to find any directive in GAS which would allow me to
do that. I read through this article http://asm.sourceforge.net/articles/smc.html
on SMC in Linux but unfortunately the author uses NASM. He seems to be
setting the page attribute bits  to be writable etc. Is that the only
way of allowing SMC? Any ideas on this?

 Thanks,
  Viv

0
Reply thisismyidentity 2/27/2008 2:26:19 PM

For the macros, you can use the C preprocessor by renaming your source to .S
instead of .s and using gcc to compile it:

        gcc -c foo.S

Then you can use #defines et al.

For self modifying code, just use .data for those functions.

0
Reply DJ 2/27/2008 8:03:45 PM


thisismyidentity wrote:
> Hi all,
> 
> I have a requirement where I need to interface some assembly routine
> with C program (without using inline assembly). In the C file, I make
> a call to assembly routine while passing _quite_ a few arguments to
> the routine. The arguments being passed in the C function call need to
> be used locally in the  assembly routine. For this purpose, I need
> local variables corresponding to each function argument and need to
> move the stack value (being pushed due to function call) of an
> argument to corresponding local variable. So at a high level whole
> thing might look something like this:
> 
> //foo.c
> 
> x = asm_func(_a, _b, _c);
> 
> 
> //bar.s
> 
> .section .data
> 
> a:
>           .int 0
> b:
>           .int 0
> c:
>           .int 0
> 
> .section .text
> 
> .global asm_func
> 
> asm_func:
> 
>           movl 8(%ebp), %eax    # a = _a
>           movl %eax, a
> 
>           movl 12(%ebp), %eax  # b = _b
>           movl %eax, b
> 
>           movl 16(%ebp), %eax  # c = _c
>           movl %eax, c
> 
> 
> # remaining instructions follow
> 
>           .
> 
>           .
> 
>           .
> 
>           ret
> 
> Apparently, MASM has following syntax for achieving the above:
> 
> a equ [ebp + 8]
> 
> b equ [ebp + 12]
> 
> c equ [ebp + 16]
> 
> But GAS's .equ (or .equiv and .set) is not equivalent to MASM's equ
> directive. Is there any other (cleaner) way in GAS for assigning the
> function
> arguments on stack to local variables in assembly code apart from
> above?

I've seen it done like:

ARG1 = 8
ARG2 = 12
....

And access 'em as "ARG1(%ebp)"... Not quite as clean as you'd like... 
(could always use Nasm...)

> Another doubt that I have is about writing SMC (self-modifying code)
> in GAS.  To do SMC in assembly, I need to allow the code area to be
> writable. I tried doing ".section .text[rwx]" but somehow it gives me
> segmentation fault. In MASM, I think you can alias code and data
> segments by using public keyword. I am looking for how to do it in
> GAS. I was unable to find any directive in GAS which would allow me to
> do that. I read through this article http://asm.sourceforge.net/articles/smc.html
> on SMC in Linux but unfortunately the author uses NASM. He seems to be
> setting the page attribute bits  to be writable etc. Is that the only
> way of allowing SMC? Any ideas on this?

Using Nasm, I can do:

section .text write

and using objdump on the .o file, I can see that Nasm has obediently 
flagged my .text section as writeable. But after linking with ld, .text 
has become readonly - ld apparently "knows" that .text isn't supposed to 
be writeable. Probably a command line switch or a "script" would fix it. 
Naming the section "kode" or something works. If your "[rwx]" syntax is 
correct for Gas, the same trick should work(?). You can also copy the 
code to be modified to the stack, and run it from there, I guess...

Best,
Frank

0
Reply Frank 2/27/2008 8:38:58 PM

In message <xn8x16cgke.fsf@delorie.com>, DJ Delorie <dj@delorie.com> wrote:

> For self modifying code, just use .data for those functions.

Just putting code in .data won't work in many modern systems as .data will
be marked as non-executable. You may need to use sys_mprotect if using
Linux.

0
Reply Timothy 2/28/2008 10:56:02 AM

3 Replies
197 Views

(page loaded in 0.227 seconds)

Similiar Articles:













7/11/2012 4:45:26 AM


Reply: