From c830b365f52bdd8dcaab604614658ce70d158055 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Fri, 12 Aug 2022 18:02:51 +0100 Subject: Add syscalls --- md/notes/undefined_c/titles.md | 79 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 9 deletions(-) (limited to 'md/notes') diff --git a/md/notes/undefined_c/titles.md b/md/notes/undefined_c/titles.md index 7753e65..513b533 100644 --- a/md/notes/undefined_c/titles.md +++ b/md/notes/undefined_c/titles.md @@ -635,6 +635,9 @@ So now executable runs libshare from local directory. Ofc there is possible to i ### Static library + + + ### Static binary Static binary don't use any shared libraries, and its possible to built it once and distribute on other platforms @@ -676,33 +679,91 @@ $ ldd static_elf Statically compiled file should work on most platforms. + -### Dynamic binary -Dynamic binary is required to load dynamically linked libraries, they must be in known location. -Replacing dynamic libraries with your own +## Basic usage +### File manipulation with libc -Point to custom location of dynamic library +Create file open data using libc functions +```c +#include +#include +#include +int main() { + FILE *f = fopen("file.txt","w+"); + char *s = "Hello"; + fwrite(s,1,strlen(s),f); + fclose(f); +} +``` +Open file and read data back -### stdin,stdout,stderr +```c +#include +#include +#include +int main() { + FILE *f = fopen("file.txt","r"); + char buf[128]; + int r; + r = fread(buf,1,128,f); + buf[r] = 0; + printf("->%s\n",buf,r); + fclose(f); +} +``` -### Styles +### File manipulation with syscalls +Now lets do the same without using libc functions using syscall function to directly use syscalls, +its also straightforward to rewrite example for assembly. +```c +#include +#include +#include +#include +int main(void) { + int fd = syscall(SYS_open, "sys.txt", O_CREAT|O_WRONLY, S_IRWXU|S_IRGRP|S_IXGRP); + char s[] = "hello sycall\n"; + syscall(SYS_write, fd, s, strlen(s)); + syscall(SYS_close, fd); + return 0; +} +``` -## Basic usage +Read data from file + +```c +#include +#include +#include +#include + +int main(void) { + int fd = syscall(SYS_open, "sys.txt", O_RDONLY); + char s[128]; + int r = syscall(SYS_read, fd, s, 128); + s[r] = 0; + syscall(SYS_close, fd); + syscall(SYS_write, 0, s, r); + return 0; +} +``` -### File manipulation with libc -### File manipulation with syscalls ## Base usage -- cgit v1.2.3