Pieter de Jong wrote:
> you'll need to terminate the process in a clean way. This means use a
> signal or (preferred) the exit() systemcall. When you link with gcc,
> it'll add the startup code found in crt?.o which contains an exit call,
> but when using ld directly it just sees your object file.
I think you just answered two of our questions at once! :-)
> I added a working version, but my nasm 0.90 is probably a bit different
> from yours, since i couldn't get it to assemble as-is. no guarantees
> though, i don't speak nasm.
Some bugs exist in versions previous to 0.94, so says the author.
Things to do with stand-alone linking if I recall.
> mov ebx, 0h ; zero exit value
> mov eax, 1h ; syscall exit
> int 80h ; trap
> ret ;shouldn't get here
It works perfectly. In another message in LINUX, you had suggested some
resources for study; the kernel hacker's guide, the libc source, reading
disassembly listings from objdump and gdb (I use nasm's ndisasm), tracing
with systrace, and reading the elf headers in the executable (shudder).
To the other participants interested in linux assembly, here is Pieter's
original int80.s demo that he wrote for me. You'll see he uses the exit
call, but in not understanding what it was- I didn't.. ;-)
--------------------------------------------------------------------
/ From Pieter de Jong - int 0x80 on Linux (i386)
/
/ What follows is not recommended procedure, because facts can be
/ changed any minute, it isn't portable At All, and way too much work.
/ Using the C library, or even better, using GCC is the best thing to
/ do.
/
/ The kernel is not much more than a large collection of systemcalls.
/ There are 163 in my kernel. asm/unistd.h is the place to see how
/ they're numbered and how the user-space part of the call is done.
/
/ A systemcall takes it's arguments in registers. eax is for the
/ number of the call, and ebx, ecx etc. are the arguments. (maximum of
/ 5 on a i386). An int0x80 transfers control to the kernel, and the
/ systemcall code is executed. The result is in %eax.
/
/ > Q. How about a small demo of using int 0x80 under Linux?
/
/ sure. this will set alarm and loop forever on a i386 linux system.
/ The program will be interrupted by a signal. It's not useful for
/ anything, and i can't even guarantee it'll work on any other system
/ than mine. Maybe it will not run on a.out systems, but then again
/ it's not using libc, so it could.
/
/ If you want to try yourself, write a small C program, rework it so it
/ only uses linux systemcalls, and write to assembly.
///////////////////////////////////////////////////////////////////////
/ make the executable:
/ as -o al.o al.s
/ ld -e Entry -o al al.o
/ no startup code necessary,
/ should assemble&link to 297 bytes or 1 diskblock.
///////////////////////////////////////////////////////////////////////
/* i386 ASSEMBLY CODE */
.file "al.s"
.text /* all in one segment */
.align 16
.MSG:
.string "This program will loop forever\n"
.string "or is terminated asynchronically "
.string "by an alarm signal \n"
.string "Looping forever, waiting for a signal\n"
.string "in +/- 10 sec. \n"
.string "-------------------------------------\n\n"
.CHK:
.string "Shouldn't get here\n"
.globl Entry /* make entry point visible to linker */
.align 16
Entry:
/* write message to stdout first */
movl $181, %edx /* 181 chars to write, no strlen :-( */
movl $.MSG,%ecx /* adress of char buffer */
movl $0, %ebx /* write to stdout = fd 0 */
movl $4, %eax /* syscall write, see asm/unistd.h */
int $0x80 /* trap */
/* ignore return, set up alarm() */
movl $10, %ebx /* alarm in 10 seconds */
movl $27, %eax /* syscall alarm() */
int $0x80 /* trap */
/* create an endless loop */
.L1:
jmp .L1
/* ignore return, set up for exit() */
/* but shouldn't get here */
movl $19, %edx /* write 19 chars */
movl $.CHK, %ecx /* adress buffer *
movl $0, %ebx /* stdout */
movl $4, %eax /* syscall write */
int $0x80 /* trap */
movl $0, %ebx /* exit_success */
movl $1, %eax /* syscall exit */
int $0x80 /* trap */
ret /* never reached */
... The Moon is Waxing Crescent (24% of Full)
--- ifmail v.2.9-tx8.1 (i386-linux)
---------------
* Origin: (jvahn@short.circuit.com) (1:346/15.1)
|