3

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, ...);
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Any help please, I let-rally searched and still searching for days with no single result. –  May 25 '21 at 00:23
  • The answer is not libc? – wxz May 25 '21 at 00:44
  • libc is where the wrapper is implemented I am asking about the one in kernel –  May 25 '21 at 00:55
  • You should have asked what the correct answer was, just for your own information. – wxz May 25 '21 at 01:12
  • All good, I actually think the opposite. Asking for the right answer (if they're openly saying you're wrong) and discussing it shows you're interested and the interviewer can at least understand your thought process better. Next time :) – wxz May 25 '21 at 01:16
  • There is no function syscall in kernel. – 0___________ May 25 '21 at 01:52
  • I will quote the exact question (hope I didn't change its meaning) In which library the system call syscall is implemented? @0___________ –  May 25 '21 at 02:05
  • there is no system call syscall. system call is the way to access kernel services. you can access it by executing special processor instruction or software interrupt. To do not write it every time in inline assembler wrapper function `syscall` was written by good people from glibc dev team. BTW there are much more wrappers for the particular system calls. Using the function s`yscall` is not the very best way for many reasons. – 0___________ May 25 '21 at 02:13
  • 1
    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 May 25 '21 at 09:40
  • If the interview was asking which library `syscall` is implemented in, but also asking about "the one in kernel space not the wrapper in libc" then the question does not make sense because the kernel is not a library. – Ian Abbott May 25 '21 at 13:35

2 Answers2

5

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

0___________
  • 60,014
  • 4
  • 34
  • 74
-1

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.

GodDamn
  • 161
  • 1
  • 8
  • 2
    Sorry but this is rather a comment than an answer, I got the difference between header and library and as I have mentioned this didn't help me find the library file. –  May 25 '21 at 00:46
  • Your system library is located inside /usr/lib/ I don't know your exact PC make, model, version etc. but I definitely gave you the location to find your answer. What distro are you even using? Have you checked ld-linux.so? If you can't find the answer with what I have shared than, like I have stated in the post as well. Research a linker and you will find your answer. I can't exactly tell you every library or application you have installed on your computer. But I wish you luck in your research bud! – GodDamn May 25 '21 at 01:01
  • ubuntu 14.0.5 kernel 4.15.18 btw this isn't an existing directory /usr/lib/ –  May 25 '21 at 01:07