diff --git a/nm-otool/includes/ft_nm_otool.h b/nm-otool/includes/ft_nm_otool.h index 38bf9af4..b4793a96 100644 --- a/nm-otool/includes/ft_nm_otool.h +++ b/nm-otool/includes/ft_nm_otool.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/20 14:36:10 by jhalford #+# #+# */ -/* Updated: 2017/10/07 17:43:29 by jhalford ### ########.fr */ +/* Updated: 2017/10/08 11:26:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,6 +35,7 @@ * -n numerical sort (as opposed to alphabetical) * -p don't sort */ + # define NM_NOSORT (1 << 0) # define NM_NSORT (1 << 1) # define NM_ASORT (1 << 2) @@ -47,6 +48,7 @@ * -u show only undefined symbols * -U filter-out undefined symbols */ + # define NM_ALL (1 << 4) # define NM_NO_LOCAL (1 << 5) # define NM_NO_UNDF (1 << 6) @@ -61,6 +63,7 @@ * get displayed as (undefined), (common), (absolute), and (indirect), * respectively. */ + # define NM_FULL (1 << 8) # define NM_OFORMAT (1 << 9) # define NM_MFORMAT (1 << 10) @@ -137,6 +140,4 @@ void symbol_format_full(t_symbol *symbol); void mach_64_parse(t_machodata *data); void dump_dysymtab(t_machodata *data, struct dysymtab_command *dysymtab); -void *hexdump(void *addr, unsigned int offset, unsigned int size); - #endif diff --git a/nm-otool/libft/srcs/str/ft_strequ.c b/nm-otool/libft/srcs/str/ft_strequ.c index 88e5580c..01decce0 100644 --- a/nm-otool/libft/srcs/str/ft_strequ.c +++ b/nm-otool/libft/srcs/str/ft_strequ.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/03 14:58:04 by jhalford #+# #+# */ -/* Updated: 2016/11/03 15:02:10 by jhalford ### ########.fr */ +/* Updated: 2017/10/08 10:39:45 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/nm-otool/srcs/ft_nm.c b/nm-otool/srcs/ft_nm.c index 8204e867..81995af6 100644 --- a/nm-otool/srcs/ft_nm.c +++ b/nm-otool/srcs/ft_nm.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/19 03:09:12 by jhalford #+# #+# */ -/* Updated: 2017/10/07 17:44:54 by jhalford ### ########.fr */ +/* Updated: 2017/10/08 11:33:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,28 +48,51 @@ void mach_64_dump(struct mach_header_64 *file, t_nmdata *data) ft_lstiter(mach.symbols, symbol_format, data); } -int nm(void *file, t_nmdata *data) +int nm_file(void *file, t_nmdata *data) { - uint32_t magic = *(int *)file; - int is_fat = IS_FAT(magic); - int is_64 = IS_MAGIC_64(magic); + uint32_t magic; - if (is_64) + magic = *(int*)file; + if (IS_MAGIC_64(magic)) mach_64_dump(file, data); - else if (is_fat) - ft_printf("{red}unsupported architecture:{eoc} magic = %#x (FAT)\n", magic); + else if (IS_FAT(magic)) + ft_printf("{red}unsupported arch:{eoc} magic=%#x(FAT)\n", magic); else - ft_printf("{red}unsupported architecture:{eoc} magic = %#x\n", magic); + ft_printf("{red}unsupported arch:{eoc} magic=%#x\n", magic); + return (0); +} + +int nm(int ac, char **av, t_nmdata data) +{ + int i; + struct stat buf; + int fd; + char *file; + + i = data.av_data - av; + while (i < ac && av[i]) + { + data.filename = av[i]; + if (!(data.flag & NM_OFORMAT) && ac - (data.av_data - av) > 1) + ft_printf("%c%s:\n", i - (data.av_data - av) ? '\n' : 0, av[i]); + if ((fd = open((data.filename), O_RDONLY)) < 0) + return (1); + if ((fstat(fd, &buf)) < 0) + return (1); + if ((file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) + == MAP_FAILED) + return (1); + nm_file(file, &data); + if (munmap(file, buf.st_size)) + return (1); + i++; + } return (0); } int main(int ac, char **av) { - char *file; t_nmdata data; - int fd; - int i; - struct stat buf; data.flag = NM_ASORT; if (cliopts_get(av, g_nm_opts, &data)) @@ -78,22 +101,5 @@ int main(int ac, char **av) ft_dprintf(2, NM_USAGE"\n"); return (1); } - i = data.av_data - av; - while (i < ac && av[i]) - { - if (!(data.flag & NM_OFORMAT) && ac - (data.av_data - av) > 1) - ft_printf("%c%s:\n", i - (data.av_data - av) ? '\n' : 0, av[i]); - if ((fd = open((data.filename = av[i]), O_RDONLY)) < 0) - return (1); - if ((fstat(fd, &buf)) < 0) - return (1); - if ((file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) - == MAP_FAILED) - return (1); - nm(file, &data); - if (munmap(file, buf.st_size)) - return (1); - i++; - } - return (0); + return (nm(ac, av, data)); } diff --git a/nm-otool/srcs/symbol_format.c b/nm-otool/srcs/symbol_format.c index 4fb625b5..d0d42108 100644 --- a/nm-otool/srcs/symbol_format.c +++ b/nm-otool/srcs/symbol_format.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/28 20:34:32 by jhalford #+# #+# */ -/* Updated: 2017/10/07 17:43:32 by jhalford ### ########.fr */ +/* Updated: 2017/10/08 11:34:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,10 +29,14 @@ void symbol_format_desc(t_symbol *symbol) { if (symbol->nlist->n_desc & REFERENCED_DYNAMICALLY) ft_printf(" [referenced dynamically]"); - ft_printf(" %sexternal", symbol->nlist->n_type & N_EXT ? "" : "non-"); - ft_printf("%s", - !(symbol->nlist->n_type & N_EXT) - && symbol->nlist->n_type & N_PEXT ? " (was a private externel)" : ""); + if (symbol->nlist->n_type & N_EXT) + ft_printf(" external"); + else + { + ft_printf(" non-external"); + if (symbol->nlist->n_type & N_PEXT) + ft_printf(" (was a private external)"); + } } void symbol_format_m(t_symbol *symbol) @@ -49,7 +53,7 @@ void symbol_format_m(t_symbol *symbol) void symbol_format_full(t_symbol *symbol) { - ft_printf("\t%03b|%b|%x|%b \t%i(%s) \t%04x", + ft_printf("\t%i %03b|%b|%x|%b \t%i(%s) \t%04x", symbol->pos, (symbol->nlist->n_type & N_STAB) >> 5, (symbol->nlist->n_type & N_PEXT) >> 4, diff --git a/nm-otool/srcs/symbol_init.c b/nm-otool/srcs/symbol_init.c index 475308d5..16bbd321 100644 --- a/nm-otool/srcs/symbol_init.c +++ b/nm-otool/srcs/symbol_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/25 21:22:06 by jhalford #+# #+# */ -/* Updated: 2017/10/07 18:35:41 by jhalford ### ########.fr */ +/* Updated: 2017/10/08 11:02:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,8 @@ t_machodata *g_data; -int symbol_init(t_symbol *symbol, char *stringtable, struct nlist_64 *array, int i) +int symbol_init(t_symbol *symbol, + char *stringtable, struct nlist_64 *array, int i) { symbol->type = 0; symbol->pos = i; @@ -23,34 +24,43 @@ int symbol_init(t_symbol *symbol, char *stringtable, struct nlist_64 *array, in return (0); } -int symbol_set(t_symbol *symbol, t_machodata *data) +static int symbol_gettype(int type_mask, + struct nlist_64 *nlist, struct section_64 *section) { - struct nlist_64 *nlist; - uint8_t n_type; - uint8_t type_mask; - - nlist = symbol->nlist; - n_type = symbol->nlist->n_type; - type_mask = n_type & N_TYPE; - symbol->section = symbol->nlist->n_sect ? - (*(struct section_64**)ft_lst_at(data->sects, symbol->nlist->n_sect - 1)->content) : NULL; - if (n_type & N_STAB) - symbol->type = SYM_STAB; - else if (type_mask == N_UNDF && n_type & N_EXT && nlist->n_value != 0) - symbol->type = SYM_COMMON; + if (nlist->n_type & N_STAB) + return (SYM_STAB); + else if (type_mask == N_UNDF && nlist->n_type & N_EXT && nlist->n_value) + return (SYM_COMMON); else if (type_mask == N_UNDF) - symbol->type = SYM_UNDF; + return (SYM_UNDF); else if (type_mask == N_ABS) - symbol->type = SYM_ABS; - else if (type_mask == N_SECT && ft_strcmp("__text", symbol->section->sectname) == 0) - symbol->type = SYM_TEXT; - else if (type_mask == N_SECT && ft_strcmp("__data", symbol->section->sectname) == 0) - symbol->type = SYM_DATA; - else if (type_mask == N_SECT && ft_strcmp("__bss", symbol->section->sectname) == 0) - symbol->type = SYM_BSS; + return (SYM_ABS); + else if (type_mask == N_SECT && ft_strequ("__text", section->sectname)) + return (SYM_TEXT); + else if (type_mask == N_SECT && ft_strequ("__data", section->sectname)) + return (SYM_DATA); + else if (type_mask == N_SECT && ft_strequ("__bss", section->sectname)) + return (SYM_BSS); else if (type_mask == N_INDR) - symbol->type = SYM_INDR; + return (SYM_INDR); else - symbol->type = SYM_OTHER; + return (SYM_OTHER); +} + +int symbol_set(t_symbol *sym, t_machodata *data) +{ + t_list *lst; + + if (sym->nlist->n_sect) + { + lst = ft_lst_at(data->sects, sym->nlist->n_sect - 1); + sym->section = *(struct section_64**)(lst->content); + } + else + sym->section = NULL; + sym->type = symbol_gettype( + sym->nlist->n_type & N_TYPE, + sym->nlist, + sym->section); return (0); }