Skip to content
Advertisement

C sockets

The following C program calls select() to get a list of client sockets that have data in the socket buffer (using UNIX domain datagram sockets). It takes four parameters on call from a NASM program. The parameter fds_array[] is a 3-element integer array created in NASM and passed into the program.

JavaScript

The data are passed in from NASM in rdi, rsi, rdx and rcx. :

JavaScript

As written, the program hangs. When I step through it with gdb, it does its calculations correctly and the values in fds_array are as expected. But at the return line (return 0), gdb reports this:

JavaScript

So it says the variables nfds, rfds and fds_array cannot be accessed. But I just finished stepping through it and everything was correct.

My research turned up a number of Stack Overflow questions on that gdb error message, but they’re all fact-specific and I didn’t find any that fit these facts.

Thanks for any ideas on how to fix this error on exit.

Advertisement

Answer

Let’s look at this call…

JavaScript

In the System V AMD64 ABI, the arguments would be passed in:

  • rdi – ndfs
  • rsi – fds_array
  • rsp..rsp+127 – rfds
  • rdx – fd_count

It looks like you’re passing rfds as if it were a pointer. It’s not. It goes on the stack.

If this were Windows, it would be passed as a pointer. However, it sounds like you’re not using Windows, since you mention Unix domain sockets (although AF_UNIX is actually available on Windows).

If you ever need help calling a C function from assembly, just use Godbolt.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement