Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

System Call Curiosity



Hello,

I'm sorry if this question is a little trivial, but I've asked about on some assembler lists and no one seemed to have an answer.

I've found a curiosity, and was wondering if anyone can shed any light on why it happens.

The code below compiles and runs without problems on OpenBSD and Linux.

---------- cut here -------------
.data
msg:
.string "Hello world!\n"
len = . - msg
.text
.global _start
_start:
li 0,4
li 3,1
lis 4,msg@ha
addi 4,4,msg@l
li 5,len
sc
li 0,1
li 3,0
sc
---------- cut here -------------

With a little fiddling of the syntax I got converted it to Darwin

---------- cut here -------------
.data
.cstring
.align 2
msg:
.asciz "Hello world!\n"
len = . - msg
.text
.align 2
.globl _start
_start:
li r0,4
li r3,1
lis r4,ha16(msg)
ori r4,r4,lo16(msg)
li r5,len
sc
li r0,1
li r3,0
sc
---------- cut here -------------

this compiles jus fine, using : -

% as hello-ppc.s

I then link using : -

% ld -s -o Hello a.out

then I run it from the command line : -

% ./Hello

I get the result I am looking for : -

Hello world!

and then : -

Segmentation fault

bugger ! it's trying to access memory not allocated to it.

when I tried to have a look at it in 'gdb' I got the same thing : -

Program received signal EXC_BAD_ACCESS, Could not access memory.
0x00658c5c in ??

so I look for the crash log and I see

Exception: EXC_BAD_ACCESS (0x0001)
Codes: KERN_INVALID_ADDRESS (0x0001) at 0x00658c5c

Thread 0 Crashed:

PPC Thread State:
srr0: 0x00658c5c srr1: 0x4000d030 vrsave: 0x00000000
xer: 0x00000000 lr: 0x00000000 ctr: 0x00000000 mq: 0x00000000
r0: 0x00000004 r1: 0xbffffea4 r2: 0x00000000 r3: 0x0000000e
r4: 0x00000000 r5: 0x0000000e r6: 0x00000000 r7: 0x00000000
r8: 0x00000000 r9: 0x00000000 r10: 0x00000000 r11: 0x00000000
r12: 0x00000000 r13: 0x00000000 r14: 0x00000000 r15: 0x00000000
r16: 0x00000000 r17: 0x00000000 r18: 0x00000000 r19: 0x00000000
r20: 0x00000000 r21: 0x00000000 r22: 0x00000000 r23: 0x00000000
r24: 0x00000000 r25: 0x00000000 r26: 0x00000000 r27: 0x00000000
r28: 0x00000000 r29: 0x00000000 r30: 0x00000000 r31: 0x00000000

the interesting thing here is that R0 contains 4 and it should contain 1.
it seems to be hanging around after the last system call.

so I decided to add a nop after the first system call ...

---------- cut here -------------
.data
.cstring
.align 2
msg:
.asciz "Hello world!\n"
len = . - msg
.text
.align 2
.globl _start
_start:
li r0,4
li r3,1
lis r4,ha16(msg)
ori r4,r4,lo16(msg)
li r5,len
sc
nop
li r0,1
li r3,0
sc
---------- cut here -------------

... and no problems.

Has anyone any ideas why this happens ??



S a m
_______________________________________________
darwin-development mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/darwin-development
Do not post admin requests to the list. They will be ignored.



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.