parse elf_header

This commit is contained in:
Gregory 2019-06-03 23:36:37 +03:00
parent a3264249e8
commit b9b93a9104
5 changed files with 77 additions and 3 deletions

4
.gitignore vendored
View file

@ -1,7 +1,9 @@
*.o *.o
*.a *.a
*.so *.so
.vscode .vscode/*
!.vscode/launch.json
!.vscode/tasks.json
/bld /bld
/debug_bld /debug_bld
/out /out

24
.vscode/launch.json vendored Normal file
View file

@ -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"
}
}
]
}

12
.vscode/tasks.json vendored Normal file
View file

@ -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"
}
]
}

View file

@ -35,6 +35,7 @@ libft = subproject('libft')
libft_dep = libft.get_variable('libft_dep') libft_dep = libft.get_variable('libft_dep')
ft_nm = executable( ft_nm = executable(
'ft_nm', 'ft_nm',
sources, sources,

View file

@ -1,7 +1,42 @@
#include "libft.h" #include "libft.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <elf.h>
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); return(0);
} }