1

I am trying socket programming for ARM, however I am not able to understand how the values for the arguments are decided. For example this is the link for Azeria Labs enter image description here I understand that sys call for ARM register R7 gets it hence its 281 in this case and arguments are passed using R0, R1, R2, R3. But here how do you decide the values for R0(AF_INET) as 2 and R1(SOCK_STREAM) as 1 while creating socket(AF_INET, SOCK_STREAM, 0) Finding system call was easy

$ grep socket /usr/include/asm/unistd-common.h
#define __NR_socket                 (__NR_SYSCALL_BASE+281)
#define __NR_socketpair             (__NR_SYSCALL_BASE+288)

Similarly is there a way to find the values for the arguments? I found an another resource which was for X86 Assembly which also has similar approach.

%assign SOCK_STREAM         1
%assign AF_INET             2
%assign SYS_socketcall      102
%assign SYS_SOCKET          1
%assign SYS_CONNECT         3
%assign SYS_SEND            9
%assign SYS_RECV            10

section .text
  global _start

;--------------------------------------------------
;Functions to make things easier. :]
;--------------------------------------------------
_socket:
  mov [cArray+0], dword AF_INET
  mov [cArray+4], dword SOCK_STREAM
  mov [cArray+8], dword 0
  mov eax, SYS_socketcall
  mov ebx, SYS_SOCKET
  mov ecx, cArray
  int 0x80
  ret

Kindly let me know. Thank you.

Linux alarmpi 4.4.34+ #3 Thu Dec 1 14:44:23 IST 2016 armv6l GNU/Linux

Kanan Jarrus
  • 607
  • 1
  • 12
  • 26
  • Do not post pictures of code. Please replace your pictures of code with text. I have downvoted your question and will retract my downvote once all pictures have been replaced. – fuz Aug 31 '19 at 13:19
  • @fuz I have done that now. – Kanan Jarrus Aug 31 '19 at 13:28
  • You run `man socket` and look at the API. For instance, the first argument is the **domain**, which can be `AF_INET` or the value 2. All ARM syscalls (`swi`) on Linux place the function in `r7` and then arguments in `r0`, `r1`, `r2`, ... This question has been asked many times, the only unique part is using `socket` or network programming which is just a subset of other questions. – artless noise Aug 31 '19 at 13:44
  • Possible duplicate of [In linux, how to do system calls through GNU ARM assembly](https://stackoverflow.com/questions/19285992/in-linux-how-to-do-system-calls-through-gnu-arm-assembly) – artless noise Aug 31 '19 at 13:44
  • [Here is a question](https://stackoverflow.com/questions/24616226/how-can-i-select-a-static-library-to-be-linked-while-arm-cross-compiling) which using gcc inline assembler to make syscalls. This shows a direct relation between a 'C' function prototype and the registers to use [as per here](https://stackoverflow.com/questions/53903726/can-more-than-seven-arguments-be-passed-to-system-call-in-arm-linux) or `man syscall`. – artless noise Aug 31 '19 at 13:56
  • @artlessnoise My doubt is how do you decide that R0 gets the 2 for AF_INET and R1 gets 1 for SOCK_STREAM when creating socket. Syscall is pretty straight forward as R7 always gets the syscall number hence 281. I don't understand how R0 which is the first argument and R1 which is the second argument gets its respective values? – Kanan Jarrus Aug 31 '19 at 14:02
  • @artlessnoise quote "For instance, the first argument is the domain, which can be AF_INET or the value 2" how did you decide the value 2 for AF_INET ? That is my question – Kanan Jarrus Aug 31 '19 at 14:05
  • 1
    @KananJarrus These numbers are defined in the system's header files. For Linux, check out `/usr/include/x86_64-linux-gnu/bits/socket.h`. – fuz Aug 31 '19 at 18:54
  • Would you be clear about the host operating system you intend running this on? This is a part of the ABI that is by no means standard. – marko Aug 31 '19 at 22:57
  • @fuz Thank you very much. I was able to get the values from **/usr/include/bits/socket.h** and **/usr/include/bits/socket.h** `$grep STREAM /usr/include/bits/socket_type.h SOCK_STREAM = 1, /* Sequenced, reliable, connection-based #define SOCK_STREAM SOCK_STREAM ::~ $grep INET /usr/include/bits/socket.h #define PF_INET 2 /* IP protocol family. */ #define PF_INET6 10 /* IP version 6. */ #define AF_INET PF_INET #define AF_INET6 PF_INET6` – Kanan Jarrus Sep 01 '19 at 03:39
  • @marko I am running Arch Linux on ARMv6l architecture – Kanan Jarrus Sep 01 '19 at 03:46

0 Answers0