Where is the connection open () in the libc source?

I basically need to configure multiple linux system call interfaces (like sys_open) for my purpose. I know very well the GNU Linker ld -wrap = symbol option and use this logic to modify the open () libc wrapper. While this serves a purpose, I really want to know where in the libc sources the actual implementation comes into play.

The next two places are my main suspects (note that fcntrl.h only has declarations)

  • GLIBC_DIR / I.O. / open.c
  • GLIBC_DIR / ports / sysdeps / Unix / SysV / Linux / general / open.c

Driver example:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
    int fd;

    if ((fd = open("sample.c", O_RDONLY)) == -1) {
        fprintf(stderr, "file not found\n");
        exit(1);
    }

    return 0;
}

      

Worried snippet:

main:
  401dd1:       bf 44 90 48 00          mov    $0x489044,%edi
  401dd6:       b8 00 00 00 00          mov    $0x0,%eax
  401ddb:       e8 10 03 03 00          callq  4320f0 <__libc_open>

......
......

 __libc_open:
  4320f0:       83 3d 69 8e 28 00 00    cmpl   $0x0,0x288e69(%rip)        
  4320f7:       75 14                   jne    43210d <__open_nocancel+0x14>

__open_nocancel:
  4320f9:       b8 02 00 00 00          mov    $0x2,%eax
  4320fe:       0f 05                   syscall 

      

For simplicity, I've prepared all libc binaries statically . Was also careful enough for GCC to pick up the custom libc.a correctly. I tried adding a puts statement, but the two source codes mentioned DO NOT run at all. Taking a look at the assembly of the executable [shown above], the sys_open call (0x2 in __open_nocancel) was somehow placed in the executable.

So my question is the following:

  • Where exactly in libc the open () logic logic is related to magic?
  • How can the linker successfully hook up the open () function when there is no function explicitly named open in the libc source tree?
+3


source to share


1 answer


Where, exactly in libc, is the open () code logic associated with magic?

Included from sysdeps/unix/syscall-template.S



How can the linker successfully hook up the open () function when there is no function explicitly named open in the libc source tree?

If you preprocess the source with the correct one -DSYSCALL_SYMBOL=...

, you will find that the source mentions open

.

+3


source







All Articles