32bit binary

This commit is contained in:
Gregory 2019-07-27 18:11:42 +03:00
parent 3935cb38a8
commit 6159ea6e71
8 changed files with 196 additions and 20 deletions

16
.vscode/launch.json vendored
View file

@ -32,6 +32,22 @@
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"osx": {
"MIMode": "lldb"
}
},
{
"name": "32bin",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/debug_bld/ft_nm",
"args": ["t/test_binaries/test32"],
"preLaunchTask": "build",
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"osx": {
"MIMode": "lldb"
}

View file

@ -6,7 +6,7 @@
/* By: gtertysh <gtertysh@student.unit.ua> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/05/06 22:37:16 by foton #+# #+# */
/* Updated: 2019/07/20 16:17:47 by gtertysh ### ########.fr */
/* Updated: 2019/07/27 18:10:09 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,13 +15,20 @@
#include <mach-o/loader.h>
#include <mach-o/nlist.h>
#include <stddef.h>
typedef struct load_command t_load_command;
typedef struct mach_header_64 t_mach_header_64;
typedef struct symtab_command t_symtab_command;
typedef struct load_command t_load_command;
typedef struct mach_header_64 t_mach_header_64;
typedef struct mach_header t_mach_header_32;
typedef struct segment_command_64 t_segment_command_64;
typedef struct segment_command t_segment_command_32;
typedef struct nlist_64 t_nlist_64;
typedef struct nlist t_nlist_32;
typedef struct section_64 t_section_64;
typedef struct section t_section_32;
typedef struct s_nm_file
{
@ -40,6 +47,15 @@ typedef struct s_nm_mach_64
char *string_table;
} t_nm_mach_64;
typedef struct s_nm_mach_32
{
t_mach_header_32 *header;
t_load_command *commands;
t_symtab_command *symbol_table_command;
t_nlist_32 *symbol_table;
char *string_table;
} t_nm_mach_32;
void init_file
(
t_nm_file *file
@ -63,6 +79,11 @@ void macho64
t_nm_file *file
);
void macho32
(
t_nm_file *file
);
t_symtab_command *find_symbol_table_command
(
t_load_command *lc,
@ -71,9 +92,15 @@ t_symtab_command *find_symbol_table_command
void print_addr
(
void *addr
size_t addr
);
void print_addr_32
(
size_t addr
);
#endif

View file

@ -31,6 +31,7 @@ sources = [
'src/main.c',
'src/nm_file.c',
'src/macho64.c',
'src/macho32.c',
'src/print_address.c',
'src/find_symbol_table_command.c',
]

105
src/macho32.c Normal file
View file

@ -0,0 +1,105 @@
#include "ft_nm.h"
#include "libft.h"
#include <stddef.h>
static t_section_32 *find_section
(
t_nm_mach_32 *mach32,
uint32_t index
)
{
uint32_t i;
uint32_t acc;
t_load_command *lc;
t_section_32 *sections;
t_segment_command_32 *segment;
lc = mach32->commands;
acc = 0;
i = 0;
while(i < mach32->header->ncmds)
{
if (lc->cmd == LC_SEGMENT)
{
segment = (t_segment_command_32 *)lc;
if (segment->nsects + acc >= index)
{
sections = (t_section_32 *)((uintptr_t)segment +
sizeof(t_segment_command_32));
return (sections + index - acc - 1);
}
acc += segment->nsects;
}
lc = (t_load_command *)((uintptr_t)lc + lc->cmdsize);
i++;
}
return NULL;
}
static void print_symbol_table(t_nm_mach_32 *mach32)
{
uint32_t j;
t_nlist_32 symbol;
t_section_32 *section;
j = 0;
while(j < mach32->symbol_table_command->nsyms)
{
symbol = mach32->symbol_table[j];
int type = symbol.n_type & N_TYPE;
int external = symbol.n_type & N_EXT;
int debug = (symbol.n_type & N_STAB) != 0;
int offset = external ? 0 : 32;
char *name = mach32->string_table + symbol.n_un.n_strx;
if (debug)
{
j++;
continue;
}
// some shit herustic should be used
// to determine to print address or not
if (symbol.n_value)
print_addr_32(symbol.n_value);
else
ft_putstr(" ");
ft_putchar(' ');
if (type == N_UNDF)
ft_putchar('U' + offset);
if (type == N_ABS)
ft_putchar('A' + offset);
if (type == N_SECT)
{
section = find_section(mach32, symbol.n_sect);
if(ft_strcmp(SECT_TEXT, section->sectname) == 0)
ft_putchar('T' + offset);
else if(ft_strcmp(SECT_DATA, section->sectname) == 0)
ft_putchar('D' + offset);
else if(ft_strcmp(SECT_BSS, section->sectname) == 0)
ft_putchar('B' + offset);
else
ft_putchar('S' + offset);
}
ft_putchar(' ');
ft_putstr(name);
ft_putstr("\n");
j++;
}
}
void macho32(t_nm_file *file)
{
t_nm_mach_32 mach32;
mach32.header = (t_mach_header_32 *)file->file;
mach32.commands = (t_load_command *)(file->file + sizeof(t_mach_header_32));
mach32.symbol_table_command = find_symbol_table_command(mach32.commands,
mach32.header->ncmds);
mach32.symbol_table = (t_nlist_32 *)
(mach32.symbol_table_command->symoff + file->file);
mach32.string_table = (char *)
(mach32.symbol_table_command->stroff + file->file);
print_symbol_table(&mach32);
}

View file

@ -2,7 +2,7 @@
#include "libft.h"
#include <stddef.h>
t_section_64 *find_section
static t_section_64 *find_section
(
t_nm_mach_64 *mach64,
uint32_t index
@ -36,7 +36,7 @@ t_section_64 *find_section
return NULL;
}
void print_symbol_table(t_nm_mach_64 *mach64)
static void print_symbol_table(t_nm_mach_64 *mach64)
{
uint32_t j;
t_nlist_64 symbol;
@ -62,7 +62,7 @@ void print_symbol_table(t_nm_mach_64 *mach64)
// some shit herustic should be used
// to determine to print address or not
if (symbol.n_value)
print_addr((void *)symbol.n_value);
print_addr(symbol.n_value);
else
ft_putstr(" ");
ft_putchar(' ');
@ -72,7 +72,6 @@ void print_symbol_table(t_nm_mach_64 *mach64)
ft_putchar('A' + offset);
if (type == N_SECT)
{
// lookup in which section symbol is located
section = find_section(mach64, symbol.n_sect);
if(ft_strcmp(SECT_TEXT, section->sectname) == 0)
ft_putchar('T' + offset);
@ -102,8 +101,5 @@ void macho64(t_nm_file *file)
(mach64.symbol_table_command->symoff + file->file);
mach64.string_table = (char *)
(mach64.symbol_table_command->stroff + file->file);
ft_putstr("\n");
ft_putstr(file->filename);
ft_putstr(":\n");
print_symbol_table(&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/20 15:21:14 by gtertysh ### ########.fr */
/* Updated: 2019/07/27 17:57:52 by gtertysh ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,26 +24,38 @@ void hanldle_file(const char *filename)
magic = *(uint32_t *)file.file;
if (magic == MH_MAGIC_64)
macho64(&file);
// else if (magic == MH_MAGIC)
// handle_32(file);
// else if (magic == MH_CIGAM || magic == MH_CIGAM_64)
// ft_putstr("do not support big endian binaries.");
else if (magic == MH_MAGIC)
macho32(&file);
else
ft_putstr("invalid magic number.");
close_file(&file);
}
void print_name(const char *name)
{
ft_putstr("\n");
ft_putstr(name);
ft_putstr(":\n");
}
int main(int argc, char **argv)
{
int i;
if (argc == 1)
hanldle_file("a.out");
else if (argc == 2)
hanldle_file(argv[1]);
else
{
i = 1;
while (i < argc)
hanldle_file(argv[i++]);
{
print_name(argv[i]);
hanldle_file(argv[i]);
i++;
}
}
return (0);
}

View file

@ -2,17 +2,36 @@
#include "libft.h"
#include <stddef.h>
void print_addr(void *addr)
void print_addr(size_t addr)
{
size_t i;
size_t bit_4;
size_t count;
count = sizeof(addr) * 2;
count = sizeof(size_t) * 2;
i = 0;
while (i < count)
{
bit_4 = ((size_t)addr >> ((count - i - 1) * 4)) & 0xf;
bit_4 = (addr >> ((count - i - 1) * 4)) & 0xf;
if (bit_4 < 10)
ft_putnbr(bit_4);
else
ft_putchar('a' + bit_4 - 10);
i++;
}
}
void print_addr_32(size_t addr)
{
size_t i;
size_t bit_4;
size_t count;
count = sizeof(size_t);
i = 0;
while (i < count)
{
bit_4 = (addr >> ((count - i - 1) * 4)) & 0xf;
if (bit_4 < 10)
ft_putnbr(bit_4);
else

BIN
t/test_binaries/test32 Executable file

Binary file not shown.