some refactoring, separated dumping from fetching
This commit is contained in:
parent
83c2942694
commit
fcc8dc3536
9 changed files with 186 additions and 107 deletions
|
|
@ -40,7 +40,9 @@ ft_nm.c\
|
||||||
ft_otool.c\
|
ft_otool.c\
|
||||||
get_symbols.c\
|
get_symbols.c\
|
||||||
hexdump.c\
|
hexdump.c\
|
||||||
symprint.c
|
sym_dump.c\
|
||||||
|
sym_format.c\
|
||||||
|
sym_get.c
|
||||||
|
|
||||||
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
|
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
|
||||||
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
|
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))
|
||||||
|
|
|
||||||
|
|
@ -69,11 +69,14 @@ struct s_symbolmap
|
||||||
|
|
||||||
extern t_symbolmap g_symbolmap[];
|
extern t_symbolmap g_symbolmap[];
|
||||||
|
|
||||||
t_list *get_symbols(char *file);
|
t_list *sym_get(char *file);
|
||||||
int fetch_header(t_machodata *data, void *file);
|
int fetch_header(t_machodata *data, void *file);
|
||||||
|
|
||||||
int symprint(t_symbol *symbol);
|
int sym_format(t_symbol *symbol);
|
||||||
int symprint_text(t_symbol *symbol);
|
int sym_format_text(t_symbolmap map, t_symbol *symbol);
|
||||||
|
|
||||||
|
void dump_symtab(struct symtab_command *symtab, void *file);
|
||||||
|
void dump_dysymtab(struct dysymtab_command *dysymtab, void *file);
|
||||||
|
|
||||||
void *hexdump(void *addr, unsigned int offset, unsigned int size);
|
void *hexdump(void *addr, unsigned int offset, unsigned int size);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,7 +59,7 @@ int fetch_header(t_machodata *data, void *file)
|
||||||
|
|
||||||
if (is_64)
|
if (is_64)
|
||||||
fetch_machheader64(data, file);
|
fetch_machheader64(data, file);
|
||||||
if (is_fat)
|
else if (is_fat)
|
||||||
fetch_fatheader(data, file);
|
fetch_fatheader(data, file);
|
||||||
else
|
else
|
||||||
ft_printf("{red}unsupported architecture:{eoc} magic = %#x\n", magic);
|
ft_printf("{red}unsupported architecture:{eoc} magic = %#x\n", magic);
|
||||||
|
|
|
||||||
|
|
@ -12,48 +12,12 @@
|
||||||
|
|
||||||
#include "ft_nm_otool.h"
|
#include "ft_nm_otool.h"
|
||||||
|
|
||||||
t_symbolmap g_symbolmap[] =
|
|
||||||
{
|
|
||||||
{'U', NULL},
|
|
||||||
{'A', NULL},
|
|
||||||
{'T', symprint_text},
|
|
||||||
{'D', NULL},
|
|
||||||
{'B', NULL},
|
|
||||||
{'C', NULL},
|
|
||||||
{'-', NULL},
|
|
||||||
{'S', NULL},
|
|
||||||
{'I', NULL},
|
|
||||||
};
|
|
||||||
|
|
||||||
void dump_section_64(struct section_64 *sect)
|
|
||||||
{
|
|
||||||
ft_printf("section64: segname=[%s], sectname=[%s]\n",
|
|
||||||
sect->segname, sect->sectname);
|
|
||||||
}
|
|
||||||
|
|
||||||
void dump_segment_64(struct segment_command_64 *seg, void *file)
|
|
||||||
{
|
|
||||||
uint32_t nsects;
|
|
||||||
uint32_t i;
|
|
||||||
struct section_64 *sect;
|
|
||||||
|
|
||||||
(void)file;
|
|
||||||
nsects = seg->nsects;
|
|
||||||
ft_printf("LC_SEGMENT_64: segname=[%s], nsects=[%i]\n", seg->segname, nsects);
|
|
||||||
sect = (void*)(seg + 1);
|
|
||||||
for (i = 0; i < nsects; i++)
|
|
||||||
{
|
|
||||||
dump_section_64(sect);
|
|
||||||
sect = sect + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void nm(void *file)
|
void nm(void *file)
|
||||||
{
|
{
|
||||||
t_list *symbols;
|
t_list *symbols;
|
||||||
|
|
||||||
symbols = get_symbols(file);
|
symbols = sym_get(file);
|
||||||
ft_lstiter(symbols, symprint);
|
ft_lstiter(symbols, sym_format);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int ac, char **av)
|
int main(int ac, char **av)
|
||||||
|
|
|
||||||
|
|
@ -12,21 +12,20 @@
|
||||||
|
|
||||||
#include "ft_nm_otool.h"
|
#include "ft_nm_otool.h"
|
||||||
|
|
||||||
static t_flag get_symtype(uint32_t i, struct dysymtab_command *dysym)
|
static t_flag symtype(uint32_t i, struct dysymtab_command *dysym)
|
||||||
{
|
{
|
||||||
if (dysym->ilocalsym <= i && i <= dysym->ilocalsym + dysym->nlocalsym)
|
if (dysym->ilocalsym <= i && i < dysym->ilocalsym + dysym->nlocalsym)
|
||||||
return (SYM_DEBUG);
|
return (SYM_DEBUG);
|
||||||
if (dysym->iextdefsym <= i && i <= dysym->iextdefsym + dysym->nextdefsym)
|
if (dysym->iextdefsym <= i && i < dysym->iextdefsym + dysym->nextdefsym)
|
||||||
return (SYM_TEXT);
|
return (SYM_TEXT);
|
||||||
if (dysym->iundefsym <= i && i <= dysym->iundefsym + dysym->nundefsym)
|
if (dysym->iundefsym <= i && i < dysym->iundefsym + dysym->nundefsym)
|
||||||
return (SYM_UNDEF);
|
return (SYM_UNDEF);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_list *get_symbols(char *file)
|
t_list *sym_get(char *file)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int nsyms;
|
|
||||||
char *stringtable;
|
char *stringtable;
|
||||||
char *string;
|
char *string;
|
||||||
struct nlist_64 *array;
|
struct nlist_64 *array;
|
||||||
|
|
@ -36,41 +35,24 @@ t_list *get_symbols(char *file)
|
||||||
t_list *symbols;
|
t_list *symbols;
|
||||||
|
|
||||||
fetch_header(&data, file);
|
fetch_header(&data, file);
|
||||||
nsyms = data.symtab->nsyms;
|
|
||||||
array = (struct nlist_64*)(file + data.symtab->symoff);
|
|
||||||
stringtable = file + data.symtab->stroff;
|
stringtable = file + data.symtab->stroff;
|
||||||
ft_printf("LC_SYMTAB [%d] symbols:\n", data.symtab->nsyms);
|
|
||||||
i = -1;
|
|
||||||
symbols = NULL;
|
symbols = NULL;
|
||||||
while (++i < nsyms)
|
|
||||||
|
dump_symtab(data.symtab, file);
|
||||||
|
dump_dysymtab(data.dysymtab, file);
|
||||||
|
/* return (NULL); */
|
||||||
|
i = -1;
|
||||||
|
array = (struct nlist_64*)(file + data.symtab->symoff);
|
||||||
|
while (++i < (int)data.symtab->nsyms)
|
||||||
{
|
{
|
||||||
sym.name = stringtable + array[i].n_un.n_strx;
|
sym.name = stringtable + array[i].n_un.n_strx;
|
||||||
sym.type = get_symtype(i, data.dysymtab);
|
sym.type = symtype(i, data.dysymtab);
|
||||||
sym.value = 0;
|
sym.value = 0;
|
||||||
ft_lstadd(&symbols, ft_lstnew(&sym, sizeof(sym)));
|
ft_lsteadd(&symbols, ft_lstnew(&sym, sizeof(sym)));
|
||||||
}
|
}
|
||||||
return (symbols);
|
return (symbols);
|
||||||
ft_putendl("");
|
|
||||||
|
|
||||||
ft_printf("ilocalsym %i\n", data.dysymtab->ilocalsym);
|
|
||||||
ft_printf("nlocalsym %i\n", data.dysymtab->nlocalsym);
|
|
||||||
|
|
||||||
ft_printf("iextdefsym %i\n", data.dysymtab->iextdefsym);
|
|
||||||
ft_printf("nextdefsym %i\n", data.dysymtab->nextdefsym);
|
|
||||||
|
|
||||||
ft_printf("iundefsym %i\n", data.dysymtab->iundefsym);
|
|
||||||
ft_printf("nundefsym %i\n", data.dysymtab->nundefsym);
|
|
||||||
|
|
||||||
ft_printf("LC_DYSYMTAB ntoc=[%d]\n", data.dysymtab->ntoc);
|
|
||||||
ft_printf("LC_DYSYMTAB nmodtab=[%d]\n", data.dysymtab->nmodtab);
|
|
||||||
ft_printf("LC_DYSYMTAB nextrefsyms=[%d]\n", data.dysymtab->nextrefsyms);
|
|
||||||
ft_printf("LC_DYSYMTAB nmodtab=[%d]\n", data.dysymtab->nmodtab);
|
|
||||||
ft_printf("LC_DYSYMTAB nindirectsyms=[%d]\n", data.dysymtab->nindirectsyms);
|
|
||||||
ft_printf("LC_DYSYMTAB nextrel=[%d]\n", data.dysymtab->nextrel);
|
|
||||||
ft_printf("LC_DYSYMTAB nlocrel=[%d]\n", data.dysymtab->nlocrel);
|
|
||||||
ft_putendl("");
|
|
||||||
ref = (struct dylib_reference *)(file + data.dysymtab->indirectsymoff);
|
|
||||||
i = -1;
|
i = -1;
|
||||||
|
ref = (struct dylib_reference *)(file + data.dysymtab->indirectsymoff);
|
||||||
while (++i < (int)data.dysymtab->nindirectsyms)
|
while (++i < (int)data.dysymtab->nindirectsyms)
|
||||||
{
|
{
|
||||||
string = stringtable + array[ref->isym].n_un.n_strx;
|
string = stringtable + array[ref->isym].n_un.n_strx;
|
||||||
|
|
|
||||||
63
nm-otool/src/sym_dump.c
Normal file
63
nm-otool/src/sym_dump.c
Normal file
|
|
@ -0,0 +1,63 @@
|
||||||
|
#include "ft_nm_otool.h"
|
||||||
|
|
||||||
|
void dump_section_64(struct section_64 *sect)
|
||||||
|
{
|
||||||
|
ft_printf("{blu}{inv}struct section_64{eoc}: segname=[%s], sectname=[%s]\n",
|
||||||
|
sect->segname, sect->sectname);
|
||||||
|
}
|
||||||
|
|
||||||
|
void dump_segment_64(struct segment_command_64 *seg, void *file)
|
||||||
|
{
|
||||||
|
uint32_t nsects;
|
||||||
|
uint32_t i;
|
||||||
|
struct section_64 *sect;
|
||||||
|
|
||||||
|
(void)file;
|
||||||
|
nsects = seg->nsects;
|
||||||
|
ft_printf("{blu}{inv}struct segment_command_64{eoc}: segname=[%s], nsects=[%i]\n", seg->segname, nsects);
|
||||||
|
sect = (void*)(seg + 1);
|
||||||
|
for (i = 0; i < nsects; i++)
|
||||||
|
{
|
||||||
|
dump_section_64(sect);
|
||||||
|
sect = sect + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void dump_symtab(struct symtab_command *symtab, void *file)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char *stringtable;
|
||||||
|
char *string;
|
||||||
|
struct nlist_64 *array;
|
||||||
|
|
||||||
|
ft_printf("{blu}{inv}struct symtab_command{eoc}\n");
|
||||||
|
stringtable = file + symtab->stroff;
|
||||||
|
array = (struct nlist_64*)(file + symtab->symoff);
|
||||||
|
i = -1;
|
||||||
|
while (++i < (int)symtab->nsyms)
|
||||||
|
{
|
||||||
|
string = stringtable + array[i].n_un.n_strx;
|
||||||
|
ft_printf("%s\n", string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void dump_dysymtab(struct dysymtab_command *dysymtab, void *file)
|
||||||
|
{
|
||||||
|
(void)file;
|
||||||
|
ft_printf("{blu}{inv}struct dysymtab_command{eoc}\n");
|
||||||
|
ft_printf("ilocalsym %i\n", dysymtab->ilocalsym);
|
||||||
|
ft_printf("nlocalsym %i\n", dysymtab->nlocalsym);
|
||||||
|
ft_printf("iextdefsym %i\n", dysymtab->iextdefsym);
|
||||||
|
ft_printf("nextdefsym %i\n", dysymtab->nextdefsym);
|
||||||
|
ft_printf("iundefsym %i\n", dysymtab->iundefsym);
|
||||||
|
ft_printf("nundefsym %i\n", dysymtab->nundefsym);
|
||||||
|
ft_printf("---------------\n");
|
||||||
|
ft_printf("ntoc %i\n", dysymtab->ntoc);
|
||||||
|
ft_printf("nmodtab %i\n", dysymtab->nmodtab);
|
||||||
|
ft_printf("nextrefsyms %i\n", dysymtab->nextrefsyms);
|
||||||
|
ft_printf("nmodtab %i\n", dysymtab->nmodtab);
|
||||||
|
ft_printf("nindirectsims %i\n", dysymtab->nindirectsyms);
|
||||||
|
ft_printf("nextrel %i\n", dysymtab->nextrel);
|
||||||
|
ft_printf("nlocrel %i\n", dysymtab->nlocrel);
|
||||||
|
ft_putendl("");
|
||||||
|
}
|
||||||
32
nm-otool/src/sym_format.c
Normal file
32
nm-otool/src/sym_format.c
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
#include "ft_nm_otool.h"
|
||||||
|
|
||||||
|
t_symbolmap g_symbolmap[] =
|
||||||
|
{
|
||||||
|
{'U', NULL},
|
||||||
|
{'A', NULL},
|
||||||
|
{'T', sym_format_text},
|
||||||
|
{'D', NULL},
|
||||||
|
{'B', NULL},
|
||||||
|
{'C', NULL},
|
||||||
|
{'-', NULL},
|
||||||
|
{'S', NULL},
|
||||||
|
{'I', NULL},
|
||||||
|
};
|
||||||
|
|
||||||
|
int sym_format_text(t_symbolmap map, t_symbol *symbol)
|
||||||
|
{
|
||||||
|
ft_printf("%016x %c %s\n", symbol->value, map.c, symbol->name);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int sym_format(t_symbol *symbol)
|
||||||
|
{
|
||||||
|
t_symbolmap map;
|
||||||
|
|
||||||
|
map = g_symbolmap[symbol->type];
|
||||||
|
if (map.format)
|
||||||
|
map.format(map, symbol);
|
||||||
|
else
|
||||||
|
ft_printf("%16s %c %s\n", " ", map.c, symbol->name);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
64
nm-otool/src/sym_get.c
Normal file
64
nm-otool/src/sym_get.c
Normal file
|
|
@ -0,0 +1,64 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* dump_symtab.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2017/02/20 13:20:22 by jhalford #+# #+# */
|
||||||
|
/* Updated: 2017/03/23 16:49:03 by jhalford ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ft_nm_otool.h"
|
||||||
|
|
||||||
|
static t_flag get_symtype(uint32_t i, struct dysymtab_command *dysym)
|
||||||
|
{
|
||||||
|
if (dysym->ilocalsym <= i && i <= dysym->ilocalsym + dysym->nlocalsym)
|
||||||
|
return (SYM_DEBUG);
|
||||||
|
if (dysym->iextdefsym <= i && i <= dysym->iextdefsym + dysym->nextdefsym)
|
||||||
|
return (SYM_TEXT);
|
||||||
|
if (dysym->iundefsym <= i && i <= dysym->iundefsym + dysym->nundefsym)
|
||||||
|
return (SYM_UNDEF);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_list *get_symbols(char *file)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int nsyms;
|
||||||
|
char *stringtable;
|
||||||
|
char *string;
|
||||||
|
struct nlist_64 *array;
|
||||||
|
struct dylib_reference *ref;
|
||||||
|
t_symbol sym;
|
||||||
|
t_machodata data;
|
||||||
|
t_list *symbols;
|
||||||
|
|
||||||
|
fetch_header(&data, file);
|
||||||
|
nsyms = data.symtab->nsyms;
|
||||||
|
array = (struct nlist_64*)(file + data.symtab->symoff);
|
||||||
|
stringtable = file + data.symtab->stroff;
|
||||||
|
ft_printf("LC_SYMTAB [%d] symbols:\n", data.symtab->nsyms);
|
||||||
|
i = -1;
|
||||||
|
symbols = NULL;
|
||||||
|
while (++i < nsyms)
|
||||||
|
{
|
||||||
|
sym.name = stringtable + array[i].n_un.n_strx;
|
||||||
|
sym.type = get_symtype(i, data.dysymtab);
|
||||||
|
sym.value = 0;
|
||||||
|
ft_lstadd(&symbols, ft_lstnew(&sym, sizeof(sym)));
|
||||||
|
}
|
||||||
|
return (symbols);
|
||||||
|
ft_putendl("");
|
||||||
|
|
||||||
|
ref = (struct dylib_reference *)(file + data.dysymtab->indirectsymoff);
|
||||||
|
i = -1;
|
||||||
|
while (++i < (int)data.dysymtab->nindirectsyms)
|
||||||
|
{
|
||||||
|
string = stringtable + array[ref->isym].n_un.n_strx;
|
||||||
|
ft_printf("%i: %s,%#x\n", ref->isym, string, ref->flags);
|
||||||
|
ref += 1;
|
||||||
|
}
|
||||||
|
(void)file;
|
||||||
|
}
|
||||||
|
|
@ -1,31 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* symprint.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2017/03/23 16:05:55 by jhalford #+# #+# */
|
|
||||||
/* Updated: 2017/03/23 16:59:36 by jhalford ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "ft_nm_otool.h"
|
|
||||||
|
|
||||||
int symprint_text(t_symbol *symbol)
|
|
||||||
{
|
|
||||||
ft_printf("%16x %c %s\n", " ", symbol->value, symbol->name);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int symprint(t_symbol *symbol)
|
|
||||||
{
|
|
||||||
t_symbolmap map;
|
|
||||||
|
|
||||||
map = g_symbolmap[symbol->type];
|
|
||||||
if (map.format)
|
|
||||||
map.format(symbol);
|
|
||||||
else
|
|
||||||
ft_printf("%16s %c %s\n", " ", map.c, symbol->name);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue