0

So I'm trying to get the time using ARM assembly and am having trouble doing so. This is the code that I have:

.data
    .balign 4
    time:
    .word 0

.text
    .global _start

_start:
    ldr r0, =time
    mov r7, #0xd
    svc #0

    mov r7, #1
    svc #0

However, when using GDB, the value at the address of the variable time is always 0. The return value in r0 after the first system call is always 0xffffffda. It never changes and I can assume that it isn't the time since epoch.

The information that I gathered this from is: https://w3challs.com/syscalls/?arch=arm_strong

I am trying to call the system call "time."

Note that I am programming on a Raspberry Pi 2 model B. The link links to arm_strong architecture and the architecture I am working with is an ARMv7. But essentially replacing the 9 with a 0 in the r7 requirement gets me the system call I want. So for "time" I use 0xd instead of 0x90000d.

What am I doing wrong that neither the return value nor the pointer I pass into the system call is getting me the value I expect? Eventually what I want to do is take the value and print it to the console.

artless noise
  • 21,212
  • 6
  • 68
  • 105
gshawm
  • 131
  • 1
  • 9
  • [-38 is `-ENOSYS`](http://lxr.free-electrons.com/source/include/uapi/asm-generic/errno.h#L17), which suggests it might be worth looking up the _actual_ appropriate syscall number for the kernel version you're running, rather than guessing from a wildly out-of-date reference. – Notlikethat Oct 02 '16 at 19:11
  • I've tried to look for information about that, but I have had no luck. How do you suggest I should go about looking for the actual appropriate syscall number? – gshawm Oct 02 '16 at 19:21
  • Here is a more accurate representation that I found: https://w3challs.com/syscalls/?arch=arm_thumb – gshawm Oct 02 '16 at 19:34
  • The zeroes for `time` are because of runtime linking. Dynamic linker will populate them when your program is executed. – domen Oct 03 '16 at 13:15
  • Everything looks correct to me. I suggest you just set 'R0' to NULL (0) and don't have Linux write to user space memory for you. The `time_t` fits in R0 and you can use it directly. Can you do that and see if the error code changes? Or you are successful. You could use the Linux 'asm/unistd.h' and get the 0xd constant from there as `__NR_time` (and `__NR_exit` too). The issue may have to do with the constant pool and data references. You might want to set an exit code. I guess the executable runs with out SEGV? – artless noise Oct 03 '16 at 14:12
  • See: [Linux static compile](http://stackoverflow.com/questions/24616226/how-can-i-select-a-static-library-to-be-linked-while-arm-cross-compiling) and [_start with thumb](http://stackoverflow.com/questions/20369440/can-start-be-the-thumb-function/20379933#20379933) as they might be helpful? – artless noise Oct 03 '16 at 14:23

1 Answers1

0

This is an alternative solution. As indicated by @artless noise, it appears that your code is correct; I am getting the same result as you.

Notwithstanding, an alternate solution may be to use the gettimeofday syscall; which does return Epoch; provided that this is your goal. Some comments within the Internet suggest that time and gettimeofday may be using different sources; so gettimeofday may not be as accurate.

I have tried using time() with a null and not null pointer and all results return 0xffffffda in r0, respectively.

  1 /*
  2         David @InfinitelyManic
  3         http://stackoverflow.com/questions/39820523/arm-assembly-time-system-call-unexpected-values
  4
  5         $ uname -a
  6         Linux raspberrypi 4.4.21-v7+ #911 SMP Thu Sep 15 14:22:38 BST 2016 armv7l GNU/Linux
  7
  8         $ cat /etc/os-release
  9         PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)"
 10
 11         $ less /usr/share/gdb/syscalls/arm-linux.xml
 12         <syscall name="time" number="13"/>
 13
 14         arm/EABI   swi 0x0              r7          r0
 15
 16         gcc -g stuff.s -o stuff
 17 */
 18
 19 .bss
 20 .data
 21         fmt:    .asciz  "%lu\n"
 22         t:      .zero 8
 23 .text
 24         .global main
 25         .include "mymac.s"
 26
 27 main:
 28         nop
 29         ldr r9,=t
 30
 31         bl _time0
 32         mov r1, r0
 33         bl write
 34
 35         bl _time1
 36         ldr r1, [r9]
 37         bl write
 38
 39 exit:
 40         mov r7, #1
 41         svc 0
 42
 43 write:
 44         push {r1-r3,lr}
 45         ldr r0,=fmt
 46         bl printf
 47         pop {r1-r3,pc}
 48
 49 _time0:
 50         push {r1-r3,lr}
 51         mov r7, #13             // time
 52         eor r0, r0
 53         svc 0
 54         pop {r1-r3,pc}
 55
 56 _time1:
 57         push {r1-r3,lr}
 58         mov r7, #78             // gettimeofday
 59         ldr r0,=t
 60         eor r1, r1
 61         svc 0
 62         pop {r1-r3,pc}

OUTPUT:

$ ./stuff
4294967258
1475617987
InfinitelyManic
  • 760
  • 1
  • 6
  • 13