linux - From where in libc source code, is open() getting linked? -
i need customize few linux system call interfaces (say sys_open) purpose. aware of gnu linker ld --wrap=symbol option , use logic alter open() libc wrapper. although serves purpose, want know in libc source codes, actual implementation comes play.
the following 2 places major suspects (note fcntrl.h has declarations)
- glibc_dir/io/open.c
- glibc_dir/ports/sysdeps/unix/sysv/linux/generic/open.c
sample driver:
#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; }
concerned 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, had prepared libc sources executable statically. careful enough make gcc rightly pick custom libc.a. tried adding puts statement mentioned 2 source codes not getting invoked @ all. taking @ assembly of executable [shown above], sys_open call (0x2 in __open_nocancel) has been somehow placed in executable.
so question following:
- from in libc, open()-related code logic magically come?
- how linker able hook open() function when there no function explicitly named open in libc source tree?
from in libc, open()-related code logic magically come?
in comes sysdeps/unix/syscall-template.s
how linker able hook open() function when there no function explicitly named open in libc source tree?
if preprocess above source correct -dsyscall_symbol=...
, you'll discover there is mention of open
in source.
Comments
Post a Comment