find text section

This commit is contained in:
Gregory 2019-08-10 17:56:26 +03:00
parent a748fe4da4
commit e125f85672
11 changed files with 192 additions and 53 deletions

26
.vscode/launch.json vendored
View file

@ -5,7 +5,7 @@
"version": "0.2.0",
"configurations": [
{
"name": "self read",
"name": "nm self read",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/debug_bld/ft_nm",
@ -21,7 +21,7 @@
}
},
{
"name": "orig nm read",
"name": "nm orig nm read",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/debug_bld/ft_nm",
@ -37,7 +37,7 @@
}
},
{
"name": "32bin",
"name": "nm 32bin",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/debug_bld/ft_nm",
@ -53,7 +53,7 @@
}
},
{
"name": "fat",
"name": "nm fat",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/debug_bld/ft_nm",
@ -69,7 +69,7 @@
}
},
{
"name": "ar",
"name": "nm ar",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/debug_bld/ft_nm",
@ -83,6 +83,22 @@
"osx": {
"MIMode": "lldb"
}
},
{
"name": "otool orig nm read",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/debug_bld/ft_otool",
"args": ["/usr/bin/nm"],
"preLaunchTask": "build",
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"osx": {
"MIMode": "lldb"
}
},
]
}

28
inc/ft_file.h Normal file
View file

@ -0,0 +1,28 @@
#ifndef FT_FILE
# define FT_FILE
typedef struct s_file
{
void *file;
uint32_t size;
int fd;
} t_file;
void init_file
(
t_file *file
);
void open_file
(
const char *filename,
t_file *file
);
void close_file
(
t_file *file
);
#endif

View file

@ -6,7 +6,7 @@
/* By: gtertysh <gtertysh@student.unit.ua> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/06 22:37:16 by foton #+# #+# */
/* Updated: 2019/07/27 20:07:37 by gtertysh ### ########.fr */
/* Updated: 2019/08/10 17:18:31 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,6 +18,7 @@
#include <mach-o/fat.h>
#include <ar.h>
#include <stddef.h>
#include "ft_file.h"
typedef struct symtab_command t_symtab_command;
typedef struct load_command t_load_command;
@ -31,19 +32,13 @@ typedef struct nlist t_nlist_32;
typedef struct section_64 t_section_64;
typedef struct section t_section_32;
typedef struct s_nm_file
{
void *file;
uint32_t size;
int fd;
} t_nm_file;
typedef struct s_nm_mach_64
{
t_mach_header_64 *header;
t_load_command *commands;
t_symtab_command *symbol_table_command;
t_segment_command_64 *text_command;
t_section_64 *text_section;
t_nlist_64 *symbol_table;
char *string_table;
} t_nm_mach_64;
@ -53,45 +48,30 @@ typedef struct s_nm_mach_32
t_mach_header_32 *header;
t_load_command *commands;
t_symtab_command *symbol_table_command;
t_segment_command_32 *text_command;
t_section_32 *text_section;
t_nlist_32 *symbol_table;
char *string_table;
} t_nm_mach_32;
void init_file
(
t_nm_file *file
);
void open_file
(
const char *filename,
t_nm_file *file
);
void close_file
(
t_nm_file *file
);
void fat
(
t_nm_file *file
t_file *file
);
void ar
(
t_nm_file *file
t_file *file
);
void macho64
(
t_nm_file *file
t_file *file
);
void macho32
(
t_nm_file *file
t_file *file
);
t_symtab_command *find_symbol_table_command
@ -110,7 +90,4 @@ void print_addr_32
size_t addr
);
#endif

View file

@ -39,6 +39,7 @@ nm_sources = [
]
otool_sources = [
'src/nm/nm_file.c',
'src/otool/main.c'
]
@ -58,6 +59,6 @@ ft_nm = executable(
include_directories: inc,
dependencies: libft_dep,
install: true,
)
)
#subdir('t')

View file

@ -3,12 +3,12 @@
#include <ar.h>
void ar(t_nm_file *file)
void ar(t_file *file)
{
struct ar_hdr *runner;
char *long_name;
size_t offset;
t_nm_file obj;
t_file obj;
runner = file->file + SARMAG;

View file

@ -2,11 +2,11 @@
#include "libft.h"
#include <stdio.h>
void fat(t_nm_file *file)
void fat(t_file *file)
{
struct fat_arch *arch_runner;
struct fat_header *header;
t_nm_file arch_file;
t_file arch_file;
uint32_t i;
header = (struct fat_header *)file->file;

View file

@ -89,7 +89,7 @@ static void print_symbol_table(t_nm_mach_32 *mach32)
}
}
void macho32(t_nm_file *file)
void macho32(t_file *file)
{
t_nm_mach_32 mach32;

View file

@ -89,7 +89,7 @@ static void print_symbol_table(t_nm_mach_64 *mach64)
}
}
void macho64(t_nm_file *file)
void macho64(t_file *file)
{
t_nm_mach_64 mach64;

View file

@ -6,7 +6,7 @@
/* By: gtertysh <gtertysh@student.unit.ua> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/13 14:52:27 by gtertysh #+# #+# */
/* Updated: 2019/07/31 21:37:00 by gtertysh ### ########.fr */
/* Updated: 2019/08/10 16:05:21 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,7 +16,7 @@
void hanldle_file(const char *filename)
{
t_nm_file file;
t_file file;
uint32_t magic;
// create dispatch table for this bullshit

View file

@ -8,14 +8,14 @@
#include <sys/mman.h>
#include <stdio.h>
void init_file(t_nm_file *file)
void init_file(t_file *file)
{
file->fd = -1;
file->file = NULL;
file->size = 0;
}
void open_file(const char *filename, t_nm_file *file)
void open_file(const char *filename, t_file *file)
{
struct stat stat_buff;
@ -42,7 +42,7 @@ void open_file(const char *filename, t_nm_file *file)
}
}
void close_file(t_nm_file *file)
void close_file(t_file *file)
{
close(file->fd);
munmap(file->file, file->size);

View file

@ -1,7 +1,124 @@
#include <mach-o/loader.h>
#include <mach-o/fat.h>
#include <ar.h>
#include "ft_file.h"
#include "ft_nm.h"
#include "libft.h"
int main()
t_segment_command_64 *find_text_command_64
(
t_load_command *lc,
uint32_t count
)
{
ft_putstr("hello from otool!");
uint32_t i;
t_segment_command_64 *text_command;
text_command = NULL;
i = 0;
while(i < count)
{
if (lc->cmd == LC_SEGMENT_64 &&
ft_strcmp(((t_segment_command_64 *)lc)->segname, SEG_TEXT) == 0)
{
text_command = (t_segment_command_64 *)lc;
break;
}
lc = (t_load_command *)((uintptr_t)lc + lc->cmdsize);
i++;
}
return text_command;
}
t_section_64 *find_text_section_64
(
t_segment_command_64 *text_segment
)
{
uint32_t i;
t_section_64 *text_section;
t_section_64 *runner;
i = 0;
text_section = NULL;
runner = (t_section_64 *)((uintptr_t)text_segment +
sizeof(t_segment_command_64));
while(i < text_segment->nsects)
{
if (ft_strcmp(runner->sectname, SECT_TEXT) == 0)
{
text_section = runner;
break;
}
runner = (t_section_64 *)((uintptr_t)runner + sizeof(t_section_64));
i++;
}
return (text_section);
}
void print_text_64
(
t_nm_mach_64 *mach64
)
{
ft_putstr(mach64->text_section->sectname);
ft_putstr("\n section size: ");
ft_putnbr(mach64->text_section->size);
ft_putstr("\n");
}
void otool64(t_file *file)
{
t_nm_mach_64 mach64;
mach64.header = (t_mach_header_64 *)file->file;
mach64.commands = (t_load_command *)(file->file + sizeof(t_mach_header_64));
mach64.text_command = find_text_command_64(mach64.commands,
mach64.header->ncmds);
mach64.text_section = find_text_section_64(mach64.text_command);
print_text_64(&mach64);
}
void hanldle_file(const char *filename)
{
t_file file;
uint32_t magic;
// create dispatch table for this bullshit
init_file(&file);
open_file(filename, &file);
magic = *(uint32_t *)file.file;
if (magic == MH_MAGIC_64)
otool64(&file);
// else if (magic == MH_MAGIC)
// macho32(&file);
// else if (magic == FAT_CIGAM)
// fat(&file);
// else if (ft_strncmp(file.file, ARMAG, SARMAG) == 0)
// ar(&file);
else
ft_putstr("invalid magic number.");
close_file(&file);
}
int main(int argc, char **argv)
{
int i;
if (argc == 1)
{
ft_putstr_fd("at least one file must be specified\n", STDERR_FILENO);
return (1);
}
i = 1;
while(i < argc)
{
ft_putstr(argv[i]);
ft_putstr(":\n");
hanldle_file(argv[i]);
i++;
}
return (0);
}