3
votes

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, ...);
2
Any help please, I let-rally searched and still searching for days with no single result. - user16011868
The answer is not libc? - wxz
libc is where the wrapper is implemented I am asking about the one in kernel - user16011868
You should have asked what the correct answer was, just for your own information. - wxz
If you want a comment deleted as rude please flag it as such. Do not edit that as a (probably ignored) comment into your question. A flag has a much better chance of being noticed by a moderator - and sooner. You do have enough reputation for that privilege (flagging posts) and some to spare. - Yunnosch

2 Answers

4
votes

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".

-1
votes

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.