diff --git a/.gitignore b/.gitignore index 1af5e91..857ecf6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,9 @@ *.o *.a *.so -.vscode +.vscode/* +!.vscode/launch.json +!.vscode/tasks.json /bld /debug_bld /out diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..cc49c1d --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "self read", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/debug_bld/ft_nm", + "args": ["ft_nm"], + "preLaunchTask": "build", + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "osx": { + "MIMode": "lldb" + } + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..19fd5c2 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,12 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "type": "shell", + "command": "ninja -C debug_bld" + } + ] +} \ No newline at end of file diff --git a/meson.build b/meson.build index 9b3729d..991c61d 100644 --- a/meson.build +++ b/meson.build @@ -35,6 +35,7 @@ libft = subproject('libft') libft_dep = libft.get_variable('libft_dep') + ft_nm = executable( 'ft_nm', sources, diff --git a/src/main.c b/src/main.c index e5522e1..f671a75 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,42 @@ #include "libft.h" +#include +#include +#include -int main(void) +int main(int argc, char **argv) { - ft_putstr("Hello world!\n"); + if (argc != 2) + { + ft_putstr("usage\n"); + return (1); + } + int fd = open(argv[1], O_RDONLY); + if (fd == -1) + { + ft_putstr("can't open file\n"); + return (1); + } + struct stat stat_buff; + stat(argv[1], &stat_buff); + if (!S_ISREG(stat_buff.st_mode)) + { + ft_putstr("not a regular file\n"); + return (1); + } + Elf64_Ehdr elf_header; + read(fd, &elf_header, sizeof(Elf64_Ehdr)); + if (elf_header.e_ident[EI_MAG0] != ELFMAG0 || + elf_header.e_ident[EI_MAG1] != ELFMAG1 || + elf_header.e_ident[EI_MAG2] != ELFMAG2) + { + ft_putstr("not a valid magic number for elf binary file\n"); + return (1); + } + if (elf_header.e_ident[EI_CLASS] != ELFCLASS64) + { + ft_putstr("sorry, only 64bit elf binaries for now"); + return (1); + } + ft_putstr("symbol table\n"); return(0); }