brk system call in inline assembly. 
Author Message
 brk system call in inline assembly.

Hi,
     Assuming I have no access to any of the C libraries
other that the system calls, how do I call the brk() function in
assembly
without getting a segmentation fault?

My efforts as shown below, give me a SIGSEGV in the _dl_pagesize()
function.
What can I do to correct it?

int main()
{
        __asm__ __volatile__ (  "movl $45,%eax\n\t"
                                        "movl $0,%ebx\n\t"
                                        "int $0x80");

Quote:
}

The '0' in ebx is to get the top of the current data segment. But
whatever value
I use I get a seg. fault.

Basically all I want to do is dynamically allocate memory using the
brk system call in assembly, without malloc or any other C library
wrapper. Technically the above method should work, right?

Cheers,
Anoop

PS. U'll really have to start from basics. I'm a novice at this. Also
, am
I posting this in the right group? If not, please advice.



Wed, 30 Nov 2005 20:08:02 GMT  
 brk system call in inline assembly.

Quote:

> Hi,
>      Assuming I have no access to any of the C libraries
> other that the system calls, how do I call the brk() function in
> assembly
> without getting a segmentation fault?

> My efforts as shown below, give me a SIGSEGV in the _dl_pagesize()
> function.
> What can I do to correct it?

> int main()
> {
>         __asm__ __volatile__ (  "movl $45,%eax\n\t"
>                                         "movl $0,%ebx\n\t"
>                                         "int $0x80");

> }

> The '0' in ebx is to get the top of the current data segment. But
> whatever value
> I use I get a seg. fault.

> Basically all I want to do is dynamically allocate memory using the
> brk system call in assembly, without malloc or any other C library
> wrapper. Technically the above method should work, right?

brk() sets the break; the above code sets the break to 0, which could
conceivably cause a SIGSEGV.  sbrk(0) will get the current break.  Have
you tried it in C?

I would recommend against using brk() and sbrk().  If you want to free
any memory, you will have to implement the equivalent of malloc() and
free() anyway, so why not use them, since someone else has already done
all the dirty work?



Thu, 01 Dec 2005 14:29:27 GMT  
 brk system call in inline assembly.

Quote:
>      Assuming I have no access to any of the C libraries
> other that the system calls, how do I call the brk() function
> in assembly without getting a segmentation fault?

> My efforts as shown below, give me a SIGSEGV in the _dl_pagesize()
> function. What can I do to correct it?

> int main()
> {
>    __asm__ __volatile__ (  "movl $45,%eax\n\t"
>                                    "movl $0,%ebx\n\t"
>                                    "int $0x80");
> }

> The '0' in ebx is to get the top of the current data segment. But
> whatever value I use I get a seg. fault.

> Basically all I want to do is dynamically allocate memory using the
> brk system call in assembly, without malloc or any other C library
> wrapper. Technically the above method should work, right?

Are you sure that brk(NULL) is supposed to return the top of the
current data segment?

I am not sure, but I do not think so:
http://nodevice.com/sections/ManIndex/man0100.html
http://www.linux-mag.com/2001-07/compile_02.html



Thu, 01 Dec 2005 19:26:58 GMT  
 brk system call in inline assembly.

Quote:

> >      Assuming I have no access to any of the C libraries
> > other that the system calls, how do I call the brk() function
> > in assembly without getting a segmentation fault?

> > My efforts as shown below, give me a SIGSEGV in the _dl_pagesize()
> > function. What can I do to correct it?

> > int main()
> > {
> >       __asm__ __volatile__ (  "movl $45,%eax\n\t"
> >                                       "movl $0,%ebx\n\t"
> >                                       "int $0x80");
> > }

> > The '0' in ebx is to get the top of the current data segment. But
> > whatever value I use I get a seg. fault.

> > Basically all I want to do is dynamically allocate memory using the
> > brk system call in assembly, without malloc or any other C library
> > wrapper. Technically the above method should work, right?

> Are you sure that brk(NULL) is supposed to return the top of the
> current data segment?

> I am not sure, but I do not think so:
> http://nodevice.com/sections/ManIndex/man0100.html
> http://www.linux-mag.com/2001-07/compile_02.html

If I do a malloc() instead, and then run an strace on the program, it
actually calls brk(0), to find the current top of the data segment.
Only after that it calls brk() with whatever argument necessary.


Fri, 02 Dec 2005 08:51:27 GMT  
 brk system call in inline assembly.

Quote:
>>>     Assuming I have no access to any of the C libraries
>>>other that the system calls, how do I call the brk() function
>>>in assembly without getting a segmentation fault?

>>>My efforts as shown below, give me a SIGSEGV in the _dl_pagesize()
>>>function. What can I do to correct it?

>>>int main()
>>>{
>>>    __asm__ __volatile__ (  "movl $45,%eax\n\t"
>>>                                    "movl $0,%ebx\n\t"
>>>                                    "int $0x80");
>>>}

>>>The '0' in ebx is to get the top of the current data segment. But
>>>whatever value I use I get a seg. fault.

>>>Basically all I want to do is dynamically allocate memory using the
>>>brk system call in assembly, without malloc or any other C library
>>>wrapper. Technically the above method should work, right?

>>Are you sure that brk(NULL) is supposed to return the top of the
>>current data segment?

>>I am not sure, but I do not think so:
>>http://nodevice.com/sections/ManIndex/man0100.html
>>http://www.linux-mag.com/2001-07/compile_02.html

> If I do a malloc() instead, and then run an strace on the program, it
> actually calls brk(0), to find the current top of the data segment.
> Only after that it calls brk() with whatever argument necessary.

Perhaps you should ask in a Linux-specific forum.

I think you want the equivalent of FreeBSD's edata:
http://www.freebsd.org/cgi/man.cgi?query=edata



Fri, 02 Dec 2005 21:18:29 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. how to call a c++ constructor in vc++ inline assembly

2. Win32: Calling API Function via Inline Assembly

3. Inline Assembly: Defining and Calling a Procedure

4. problem porting assembly codes from ms inline assembly to linux (gnu - AT&T syntax)

5. Pragma and inline assembly

6. inline assembly language, please help!!!

7. problems porting ms inline assembly codes to linux (gnu - AT&T syntax)

8. PB DOS inline assembly

9. Pragma and inline assembly

10. Pragma and inline assembly

11. Pragma and inline assembly

12. Pragma and inline assembly

 

 
Powered by phpBB® Forum Software