find text section
This commit is contained in:
parent
a748fe4da4
commit
e125f85672
11 changed files with 192 additions and 53 deletions
26
.vscode/launch.json
vendored
26
.vscode/launch.json
vendored
|
@ -5,7 +5,7 @@
|
||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "self read",
|
"name": "nm self read",
|
||||||
"type": "cppdbg",
|
"type": "cppdbg",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/debug_bld/ft_nm",
|
"program": "${workspaceFolder}/debug_bld/ft_nm",
|
||||||
|
@ -21,7 +21,7 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "orig nm read",
|
"name": "nm orig nm read",
|
||||||
"type": "cppdbg",
|
"type": "cppdbg",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/debug_bld/ft_nm",
|
"program": "${workspaceFolder}/debug_bld/ft_nm",
|
||||||
|
@ -37,7 +37,7 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "32bin",
|
"name": "nm 32bin",
|
||||||
"type": "cppdbg",
|
"type": "cppdbg",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/debug_bld/ft_nm",
|
"program": "${workspaceFolder}/debug_bld/ft_nm",
|
||||||
|
@ -53,7 +53,7 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "fat",
|
"name": "nm fat",
|
||||||
"type": "cppdbg",
|
"type": "cppdbg",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/debug_bld/ft_nm",
|
"program": "${workspaceFolder}/debug_bld/ft_nm",
|
||||||
|
@ -69,7 +69,7 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "ar",
|
"name": "nm ar",
|
||||||
"type": "cppdbg",
|
"type": "cppdbg",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"program": "${workspaceFolder}/debug_bld/ft_nm",
|
"program": "${workspaceFolder}/debug_bld/ft_nm",
|
||||||
|
@ -83,6 +83,22 @@
|
||||||
"osx": {
|
"osx": {
|
||||||
"MIMode": "lldb"
|
"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
28
inc/ft_file.h
Normal 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
|
43
inc/ft_nm.h
43
inc/ft_nm.h
|
@ -6,7 +6,7 @@
|
||||||
/* By: gtertysh <gtertysh@student.unit.ua> +#+ +:+ +#+ */
|
/* By: gtertysh <gtertysh@student.unit.ua> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/05/06 22:37:16 by foton #+# #+# */
|
/* 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 <mach-o/fat.h>
|
||||||
#include <ar.h>
|
#include <ar.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include "ft_file.h"
|
||||||
|
|
||||||
typedef struct symtab_command t_symtab_command;
|
typedef struct symtab_command t_symtab_command;
|
||||||
typedef struct load_command t_load_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_64 t_section_64;
|
||||||
typedef struct section t_section_32;
|
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
|
typedef struct s_nm_mach_64
|
||||||
{
|
{
|
||||||
t_mach_header_64 *header;
|
t_mach_header_64 *header;
|
||||||
t_load_command *commands;
|
t_load_command *commands;
|
||||||
t_symtab_command *symbol_table_command;
|
t_symtab_command *symbol_table_command;
|
||||||
|
t_segment_command_64 *text_command;
|
||||||
|
t_section_64 *text_section;
|
||||||
t_nlist_64 *symbol_table;
|
t_nlist_64 *symbol_table;
|
||||||
char *string_table;
|
char *string_table;
|
||||||
} t_nm_mach_64;
|
} t_nm_mach_64;
|
||||||
|
@ -53,45 +48,30 @@ typedef struct s_nm_mach_32
|
||||||
t_mach_header_32 *header;
|
t_mach_header_32 *header;
|
||||||
t_load_command *commands;
|
t_load_command *commands;
|
||||||
t_symtab_command *symbol_table_command;
|
t_symtab_command *symbol_table_command;
|
||||||
|
t_segment_command_32 *text_command;
|
||||||
|
t_section_32 *text_section;
|
||||||
t_nlist_32 *symbol_table;
|
t_nlist_32 *symbol_table;
|
||||||
char *string_table;
|
char *string_table;
|
||||||
} t_nm_mach_32;
|
} 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
|
void fat
|
||||||
(
|
(
|
||||||
t_nm_file *file
|
t_file *file
|
||||||
);
|
);
|
||||||
|
|
||||||
void ar
|
void ar
|
||||||
(
|
(
|
||||||
t_nm_file *file
|
t_file *file
|
||||||
);
|
);
|
||||||
|
|
||||||
void macho64
|
void macho64
|
||||||
(
|
(
|
||||||
t_nm_file *file
|
t_file *file
|
||||||
);
|
);
|
||||||
|
|
||||||
void macho32
|
void macho32
|
||||||
(
|
(
|
||||||
t_nm_file *file
|
t_file *file
|
||||||
);
|
);
|
||||||
|
|
||||||
t_symtab_command *find_symbol_table_command
|
t_symtab_command *find_symbol_table_command
|
||||||
|
@ -110,7 +90,4 @@ void print_addr_32
|
||||||
size_t addr
|
size_t addr
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -39,6 +39,7 @@ nm_sources = [
|
||||||
]
|
]
|
||||||
|
|
||||||
otool_sources = [
|
otool_sources = [
|
||||||
|
'src/nm/nm_file.c',
|
||||||
'src/otool/main.c'
|
'src/otool/main.c'
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -58,6 +59,6 @@ ft_nm = executable(
|
||||||
include_directories: inc,
|
include_directories: inc,
|
||||||
dependencies: libft_dep,
|
dependencies: libft_dep,
|
||||||
install: true,
|
install: true,
|
||||||
)
|
|
||||||
|
|
||||||
|
)
|
||||||
#subdir('t')
|
#subdir('t')
|
||||||
|
|
|
@ -3,12 +3,12 @@
|
||||||
#include <ar.h>
|
#include <ar.h>
|
||||||
|
|
||||||
|
|
||||||
void ar(t_nm_file *file)
|
void ar(t_file *file)
|
||||||
{
|
{
|
||||||
struct ar_hdr *runner;
|
struct ar_hdr *runner;
|
||||||
char *long_name;
|
char *long_name;
|
||||||
size_t offset;
|
size_t offset;
|
||||||
t_nm_file obj;
|
t_file obj;
|
||||||
|
|
||||||
|
|
||||||
runner = file->file + SARMAG;
|
runner = file->file + SARMAG;
|
||||||
|
|
|
@ -2,11 +2,11 @@
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
void fat(t_nm_file *file)
|
void fat(t_file *file)
|
||||||
{
|
{
|
||||||
struct fat_arch *arch_runner;
|
struct fat_arch *arch_runner;
|
||||||
struct fat_header *header;
|
struct fat_header *header;
|
||||||
t_nm_file arch_file;
|
t_file arch_file;
|
||||||
uint32_t i;
|
uint32_t i;
|
||||||
|
|
||||||
header = (struct fat_header *)file->file;
|
header = (struct fat_header *)file->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;
|
t_nm_mach_32 mach32;
|
||||||
|
|
||||||
|
|
|
@ -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;
|
t_nm_mach_64 mach64;
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: gtertysh <gtertysh@student.unit.ua> +#+ +:+ +#+ */
|
/* By: gtertysh <gtertysh@student.unit.ua> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/07/13 14:52:27 by gtertysh #+# #+# */
|
/* 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)
|
void hanldle_file(const char *filename)
|
||||||
{
|
{
|
||||||
t_nm_file file;
|
t_file file;
|
||||||
uint32_t magic;
|
uint32_t magic;
|
||||||
|
|
||||||
// create dispatch table for this bullshit
|
// create dispatch table for this bullshit
|
||||||
|
|
|
@ -8,14 +8,14 @@
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
void init_file(t_nm_file *file)
|
void init_file(t_file *file)
|
||||||
{
|
{
|
||||||
file->fd = -1;
|
file->fd = -1;
|
||||||
file->file = NULL;
|
file->file = NULL;
|
||||||
file->size = 0;
|
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;
|
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);
|
close(file->fd);
|
||||||
munmap(file->file, file->size);
|
munmap(file->file, file->size);
|
||||||
|
|
121
src/otool/main.c
121
src/otool/main.c
|
@ -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"
|
#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);
|
return (0);
|
||||||
}
|
}
|
Loading…
Reference in a new issue