user16011868
user16011868

Reputation:

Where is SYSCALL() implemented in Linux?

In my last job interview I was asked what seems to be a very straight forward simple question:

Q: In which library syscall (The one is kernel space not the wrapper in libc) is implemented?

A: I answered <unistd.h>

The interviewer told me that it's wrong and he is asking in which library it's implemented not in which header file it's declared.

Why is my answer false, what's the correct answer?

I searched the web for hours and nothing found at all, even writing man 2 syscall in shell gives:

   #include <unistd.h>
   #include <sys/syscall.h>   /* For SYS_xxx definitions */

   long syscall(long number, ...);

Upvotes: 5

Views: 2067

Answers (3)

mystic knight
mystic knight

Reputation: 1

Inside syscall.c, which is usually in the kernel source tree at: /arch/{arch_name}/syscall.c

Upvotes: 0

0___________
0___________

Reputation: 67979

syscall is a wrapper that actually loads the register and executes the instruction syscall on 64 bit x86 or int 80h or sysenter on 32 bit x86 and it is part of the standard library.

example:

syscall:
  endbr64 
  mov     rax,rdi
  mov     rdi,rsi
  mov     rsi,rdx
  mov     rdx,rcx
  mov     r10,r8
  mov     r8,r9
  mov     r9,QWORD PTR [rsp+0x8]
  syscall 

So the answer is that that syscall function is in the glibc.

In the kernel in the assembly file the syscall,sysentry instruction entry or int 80h interrupt handler (depending on the system implementation) does some stack magic, performs some checks and then calls the function which will handle the particular system call. Addresses of those functions are placed in the special table containing function pointers. But this part is very hard to be called the "library".

Upvotes: 6

GodDamn
GodDamn

Reputation: 159

A header file is of the form *.h, and would be added within a C programs source code. They are usually located in the directory /usr/include on Unix-like operating systems. Standard library header files typically don't actually provide the desired functions one might assume when including them, but rather they provide preprocessing macros, while if functions from a library are needed they also provide external linkage, allowing you to finally use those libraries hidden away elsewhere.

A library, unlike a header file you would include in source, can consist of a few different ideas, but would typically be linked to using your compiler's options and flags or passed to it as an argument. A library is typically either archived within /usr/lib, or distributed as a shared object by the developers of your system. You should likely be able to find what you're searching for though by researching up on some linker tools.

Upvotes: -1

Related Questions