–write.c–
int main()
{
write(1,»EXAMPLE\n»,10);
}
Сохраним исходный текст в файле write.c, откомпилируем его компилятором GCC и выполним.
bash$ gcc write.c -o example —static
bash$ ./example
EXAMPLE
Все достаточно просто. Для того чтобы окончательно понять работу программы, воспользуемся утилитой
–
bash$ gdb ./example
GNU gdb 5.1
Copyright 2001 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public
License, and you are welcome to change it and/or distribute
copies of it under certain conditions.
Type “show copying” to see the conditions.
There is absolutely no warranty for GDB. Type “show
warranty” for details.
This GDB was configured as “i686-pc-linux-gnu”...
(gdb)
Может оказаться, что версия утилиты
(gdb) disas main
Dump of assembler code for function main:
0x80481e0
0x80481e1
0x80481e3
0x80481e6
0x80481e9
0x80481eb
0x80481f0
0x80481f2
0x80481f7
0x80481fa
0x80481fb
End of assembler dump.
Далее будет исследован выполняемый код функции
0x80481e9 0x80481eb
0x80481f0
Для просмотра кода функции
(gdb) disas __libc_write
Dump of assembler code for function __libc_write:
0x804cc60 <__libc_write>: push %EBX
0x804cc61 <__libc_write+1>: mov 0x10(%ESP,1),%EDX
0x804cc65 <__libc_write+5>: mov 0xc(%ESP,1),%ECX
0x804cc69 <__libc_write+9>: mov 0x8(%ESP,1),%EBX
0x804cc6d <__libc_write+13>: mov $0x4,%EAX
0x804cc72 <__libc_write+18>: int $0x80
0x804cc74 <__libc_write+20>: pop %EBX
0x804cc75 <__libc_write+21>: cmp $0xfffff001,%EAX
0x804cc7a <__libc_write+26>: jae 0x8052bb0 <__syscall_error>
0x804cc80 <__libc_write+32>: ret