Conditional SWI and read only .text section -ASM-


dmarschal

Member
Joined
Feb 13, 2009
Messages
116
Location
Hungary
Website
www.marschalgrips.com
Hello,

I'm coding a multi-threaded game and I found some weird things.

Tested on Gig and RB Pandoras (latest fw), both in silver case if that matters.


First, conditional SWI does not work. Please see the attached code. It's just an example to demonstrate the issue with SWIPL.


sys_exit =1
sys_write=4
console =1
sys_open =5
o_wr =1
sys_close=6

@ macro definitions
.macro invoke iint
mov r7,#\iint
swi 0
.endm

.macro exit rretval
mov r0,#\rretval
invoke sys_exit
.endm

.macro print sstr
b 3f
1: .ascii "\sstr"
2: .align
3:
mov r0,#console
adr r1,1b
mov r2,#(2b-1b)
invoke sys_write
.endm

.data

dsp_dev: .asciz "/dev/dsp"
.align

.text
.globl _start
_start:
ldr r0,dsp_dev_ofs
mov r1,#o_wr
invoke sys_open
sub r12,r0,#0
bpl 11f
print "can't open dsp dev\n"
exit 0
dsp_dev_ofs: .word dsp_dev
11:

print "all ok\n"

subs r0,r12,#0
mov r7,#sys_close
swipl 0 @ <<<<<<<< SWIPL

exit 0
.bss
.end


Copy-paste and save as swipl.s.

To assemble & link:
 


as swipl.s
ld a.out -o swipl


"strace ./swipl" gives this error:
 


syscall: unknown syscall trap 0x5f000000


What's wrong with it? ARM ARM clearly states that SWI can run conditionally.

Second, .text section is read-only. Was it changed to "ro" in 3.x kernel? Why?

Third. There are also issues with sys_nanosleep, sys_gettimeofday and termios & child process  but it needs more debugging.


What's your thoughts?
 
Last edited by a moderator:
1. Actually it works fine, it's just missing feature of strace, all libraries use unconditional swi/svc, so nobody bothered to add support for conditional ones to strace. Just add a long sleep before exit, and run "ls -l /proc/`pidof ./swipl`/fd" to see all open files, you'll see that /dev/dsp is closed.

2. .text on Linux is almost always ro, that's even true on something like GP2X.

3. sleep works for me, here is my modification to see if your program closes files:

Code:
...
swipl 0            @ <<<<<<<< SWIPL
  ldr r0,=slt
  invoke 162
exit 0
 
slt:
.word 30
.word 0
 
notaz, Thank you for the clarification! Everything works fine now. I had to relocate a few 'words' to .data section to remain writable.

You're right, files were closed fine, it was the strace utility. I hope they will not remove these instructions from objdump.

Soon I will release a tetris clone with full asm source included. (-:

I 'ported' it from x86 asm. I've written the original code 6 years ago on my HP LX200.

Here is the tile rotation macro. I think it's the simplest possible.


.macro rotate_tile
 mov r0,r0, ror #2
.endm
 

I still need to add sound effects and pack the whole thing into a pnd to be ready.
 
(tip) You can use "op_runfbapp ./yourapp" if you want to just draw to /dev/fbX, it will take care of keeping X away from redrawing the screen.
 
Currently the stripped executable is about 5800 bytes. The text-mode version (made to test game logic, timings, line removal, etc) beats a "Hello World!" written in C.

I am using op_runfbapp to avoid X event handling. I had to play a little with clone flags and now the second thread does not send garbage to screen.
 
Back
Top