day's work

This commit is contained in:
Jack Halford 2017-03-27 03:24:34 +02:00
parent 72a6016278
commit 5eee516d3a
15 changed files with 221 additions and 209 deletions

View file

@ -6,7 +6,7 @@
# By: jhalford <jack@crans.org> +#+ +:+ +#+ # # By: jhalford <jack@crans.org> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2017/02/19 03:29:38 by jhalford #+# #+# # # Created: 2017/02/19 03:29:38 by jhalford #+# #+# #
# Updated: 2017/03/23 17:04:19 by jhalford ### ########.fr # # Updated: 2017/03/26 15:37:30 by jhalford ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -41,8 +41,9 @@ ft_nm.c\
ft_otool.c\ ft_otool.c\
hexdump.c\ hexdump.c\
mach_o_parse.c\ mach_o_parse.c\
sym_dump.c\ symbol_filter.c\
sym_format.c\ symbol_format.c\
symbol_free.c\
symbol_init.c\ symbol_init.c\
symbol_sort.c symbol_sort.c
@ -54,7 +55,9 @@ INDEX = 0
OBJS := $(filter-out $(FT_NM_OBJ), $(OBJS)) OBJS := $(filter-out $(FT_NM_OBJ), $(OBJS))
OBJS := $(filter-out $(FT_OTOOL_OBJ), $(OBJS)) OBJS := $(filter-out $(FT_OTOOL_OBJ), $(OBJS))
all: $(NAME) all:
@make -C $(LIBFT_DIR)
@make -j $(NAME)
ft_nm: $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(FT_NM_OBJ) ft_nm: $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(FT_NM_OBJ)
@echo $(FT_NM_OBJ) @echo $(FT_NM_OBJ)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/20 14:36:10 by jhalford #+# #+# */ /* Created: 2017/02/20 14:36:10 by jhalford #+# #+# */
/* Updated: 2017/03/25 22:50:17 by jhalford ### ########.fr */ /* Updated: 2017/03/26 19:12:12 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -29,9 +29,23 @@
# define IS_MAGIC_64(x) (x == MH_MAGIC_64 || x == MH_CIGAM_64) # define IS_MAGIC_64(x) (x == MH_MAGIC_64 || x == MH_CIGAM_64)
# define IS_FAT(x) (x == FAT_MAGIC || x == FAT_CIGAM) # define IS_FAT(x) (x == FAT_MAGIC || x == FAT_CIGAM)
# define NM_NSORT (1 << 0) /*
# define NM_ASORT (1 << 1) ** sorting flags
# define NM_RSORT (1 << 1) */
# define NM_NOSORT (1 << 0)
# define NM_NSORT (1 << 1)
# define NM_ASORT (1 << 2)
# define NM_RSORT (1 << 3)
/*
** filtering flags
*/
# define NM_ALL (1 << 4)
/*
** formating flags
*/
# define NM_FULL (1 << 5)
typedef t_data_template t_nmdata; typedef t_data_template t_nmdata;
typedef enum e_symtype t_symtype; typedef enum e_symtype t_symtype;
@ -60,15 +74,16 @@ struct s_machodata
t_list *sects; t_list *sects;
t_list *symbols; t_list *symbols;
struct symtab_command *symtab; struct symtab_command *symtab;
struct dysymtab_command *dysymtab;
}; };
struct s_symbol struct s_symbol
{ {
t_symtype type;
int pos; int pos;
struct nlist_64 nlist; t_symtype type;
char *sect_name;
char *string; char *string;
struct nlist_64 *nlist;
struct section_64 *section;
}; };
struct s_symbolmap struct s_symbolmap
@ -87,16 +102,16 @@ int symbol_init(t_symbol *symbol,
char *stringtable, struct nlist_64 *array, int i); char *stringtable, struct nlist_64 *array, int i);
int symbol_set(t_symbol *symbol, t_machodata *data); int symbol_set(t_symbol *symbol, t_machodata *data);
int symbol_sort(t_list **syms, t_flag flag); int symbol_sort(t_list **syms, t_flag flag);
int symbol_filter(t_list **syms, t_flag flag);
void symbol_free(void *data, size_t size);
int sym_format(t_symbol *symbol); int symbol_format(t_symbol *symbol);
int symbol_format_full(t_symbol *symbol);
int sym_format_undf(t_symbolmap map, t_symbol *symbol); int sym_format_undf(t_symbolmap map, t_symbol *symbol);
int sym_format_text(t_symbolmap map, t_symbol *symbol); int sym_format_text(t_symbolmap map, t_symbol *symbol);
int sym_format_stab(t_symbolmap map, t_symbol *symbol); int sym_format_stab(t_symbolmap map, t_symbol *symbol);
void dump_symbol(t_machodata *data, t_symbol *symbol);
void mach_64_parse(t_machodata *data); void mach_64_parse(t_machodata *data);
void dump_segment_64(t_machodata *data, struct segment_command_64 *seg);
void dump_symtab(t_machodata *data, struct symtab_command *symtab);
void dump_dysymtab(t_machodata *data, struct dysymtab_command *dysymtab); void dump_dysymtab(t_machodata *data, struct dysymtab_command *dysymtab);
void *hexdump(void *addr, unsigned int offset, unsigned int size); void *hexdump(void *addr, unsigned int offset, unsigned int size);

@ -1 +1 @@
Subproject commit 04b8d5cd090e86b0f9d8160b01e49d43666deddf Subproject commit 7cc44f76141812079294c2e063bba6b64fb30d98

View file

@ -6,30 +6,12 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/25 19:39:15 by jhalford #+# #+# */ /* Created: 2017/03/25 19:39:15 by jhalford #+# #+# */
/* Updated: 2017/03/25 22:44:38 by jhalford ### ########.fr */ /* Updated: 2017/03/26 15:22:52 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
void dump_symtab(t_machodata *data, struct symtab_command *symtab)
{
int i;
char *stringtable;
t_symbol symbol;
struct nlist_64 *array;
ft_printf("{blu}{inv}struct symtab_command{eoc}\n");
stringtable = data->file + symtab->stroff;
array = (struct nlist_64*)(data->file + symtab->symoff);
i = -1;
while (++i < (int)symtab->nsyms)
{
symbol_init(&symbol, stringtable, array, i);
dump_symbol(data, &symbol);
}
}
void dump_dysymtab(t_machodata *data, struct dysymtab_command *dysymtab) void dump_dysymtab(t_machodata *data, struct dysymtab_command *dysymtab)
{ {
(void)data; (void)data;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/23 15:56:37 by jhalford #+# #+# */ /* Created: 2017/03/23 15:56:37 by jhalford #+# #+# */
/* Updated: 2017/03/25 22:51:09 by jhalford ### ########.fr */ /* Updated: 2017/03/26 19:05:53 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,7 +31,7 @@ static void symtab_parse(t_machodata *data, struct symtab_command *symtab)
} }
} }
static void fetch_sects(t_machodata *data, struct segment_command_64 *seg) static void seg_64_parse(t_machodata *data, struct segment_command_64 *seg)
{ {
uint32_t nsects; uint32_t nsects;
uint32_t i; uint32_t i;
@ -41,11 +41,7 @@ static void fetch_sects(t_machodata *data, struct segment_command_64 *seg)
sect = (void*)(seg + 1); sect = (void*)(seg + 1);
for (i = 0; i < nsects; i++) for (i = 0; i < nsects; i++)
{ {
/* DG("sect at %p (%s)", sect, sect->sectname); */
/* DG("data->sects at %p", ft_lstlast(data->sects)); */
ft_lsteadd(&data->sects, ft_lstnew(&sect, sizeof(sect))); ft_lsteadd(&data->sects, ft_lstnew(&sect, sizeof(sect)));
/* DG("data->sects at %p -> %p", ft_lstlast(data->sects), *(struct section_64*)ft_lstlast(data->sects)->content); */
/* DG("------------------"); */
sect = sect + 1; sect = sect + 1;
} }
} }
@ -64,10 +60,10 @@ void mach_64_parse(t_machodata *data)
{ {
if (lc->cmd == LC_SYMTAB) if (lc->cmd == LC_SYMTAB)
symtab_parse(data, (struct symtab_command*)lc); symtab_parse(data, (struct symtab_command*)lc);
/* else if (lc->cmd == LC_DYSYMTAB) */ else if (lc->cmd == LC_DYSYMTAB)
/* data->dysymtab = (struct dysymtab_command*)lc; */ data->dysymtab = (struct dysymtab_command*)lc;
else if (lc->cmd == LC_SEGMENT_64) else if (lc->cmd == LC_SEGMENT_64)
fetch_sects(data, (struct segment_command_64*)lc); seg_64_parse(data, (struct segment_command_64*)lc);
lc = (void*)lc + lc->cmdsize; lc = (void*)lc + lc->cmdsize;
} }
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/19 03:09:12 by jhalford #+# #+# */ /* Created: 2017/02/19 03:09:12 by jhalford #+# #+# */
/* Updated: 2017/03/25 22:49:54 by jhalford ### ########.fr */ /* Updated: 2017/03/26 19:12:03 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,12 +17,13 @@ t_machodata *g_data = NULL;
t_cliopts g_nm_opts[] = t_cliopts g_nm_opts[] =
{ {
{'n', NULL, NM_NSORT, NM_ASORT, NULL, 0}, {'n', NULL, NM_NSORT, NM_ASORT, NULL, 0},
{'p', NULL, 0, NM_NSORT | NM_ASORT, NULL, 0}, {'p', NULL, NM_NOSORT, 0, NULL, 0},
{'r', NULL, NM_RSORT, 0, NULL, 0}, {'r', NULL, NM_RSORT, 0, NULL, 0},
{0xff, "full", NM_FULL | NM_ALL, 0, NULL, 0},
{'g', NULL, 0, 0, NULL, 0}, {'g', NULL, 0, 0, NULL, 0},
{'u', NULL, 0, 0, NULL, 0}, {'u', NULL, 0, 0, NULL, 0},
{'a', NULL, 0, 0, NULL, 0}, {'a', NULL, NM_ALL, 0, NULL, 0},
{'U', NULL, 0, 0, NULL, 0}, {'U', NULL, 0, 0, NULL, 0},
{'o', NULL, 0, 0, NULL, 0}, {'o', NULL, 0, 0, NULL, 0},
{'A', NULL, 0, 0, NULL, 0}, {'A', NULL, 0, 0, NULL, 0},
@ -34,14 +35,19 @@ t_cliopts g_nm_opts[] =
void mach_64_dump(struct mach_header_64 *file, t_nmdata *data) void mach_64_dump(struct mach_header_64 *file, t_nmdata *data)
{ {
t_machodata mach; t_machodata mach;
int (*format)();
mach.sects = NULL; mach.sects = NULL;
mach.symbols = NULL; mach.symbols = NULL;
mach.file = file; mach.file = file;
mach_64_parse(&mach); mach_64_parse(&mach);
dump_symtab(&mach, mach.symtab);
symbol_sort(&mach.symbols, data->flag); symbol_sort(&mach.symbols, data->flag);
ft_lstiter(mach.symbols, sym_format); symbol_filter(&mach.symbols, data->flag);
if (data->flag & NM_FULL)
format = symbol_format_full;
else
format = symbol_format;
ft_lstiter(mach.symbols, format);
} }
int nm(void *file, t_nmdata *data) int nm(void *file, t_nmdata *data)
@ -51,10 +57,7 @@ int nm(void *file, t_nmdata *data)
int is_64 = IS_MAGIC_64(magic); int is_64 = IS_MAGIC_64(magic);
if (is_64) if (is_64)
{
ft_printf("{red}unsupported architecture:{eoc} magic = %#x (FAT)\n", magic);
mach_64_dump(file, data); mach_64_dump(file, data);
}
else if (is_fat) else if (is_fat)
ft_printf("{red}unsupported architecture:{eoc} magic = %#x (FAT)\n", magic); ft_printf("{red}unsupported architecture:{eoc} magic = %#x (FAT)\n", magic);
else else
@ -70,10 +73,15 @@ int main(int ac, char **av)
int i; int i;
struct stat buf; struct stat buf;
g_argv = av;
data.flag = NM_ASORT; data.flag = NM_ASORT;
if (cliopts_get(av, g_nm_opts, &data)) if (cliopts_get(av, g_nm_opts, &data))
return (ft_dprintf(2, "USAGE PLACEHOLDER\n") * 0 + 1); {
i = 1; ft_perror();
ft_dprintf(2, "USAGE PLACEHOLDER\n");
return (1);
}
i = data.av_data - av;
while (i < ac && av[i]) while (i < ac && av[i])
{ {
if ((fd = open(av[i], O_RDONLY)) < 0) if ((fd = open(av[i], O_RDONLY)) < 0)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/20 14:08:14 by jhalford #+# #+# */ /* Created: 2017/02/20 14:08:14 by jhalford #+# #+# */
/* Updated: 2017/03/01 17:51:49 by jhalford ### ########.fr */ /* Updated: 2017/03/26 19:10:48 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -66,7 +66,7 @@ int main(int ac, char **av)
if (ac != 2) if (ac != 2)
{ {
ft_dprintf(2, "Please give me an arg\n"); ft_dprintf(2, "USAGE PLACEHOLDER\n");
return (1); return (1);
} }
if ((fd = open(av[1], O_RDONLY)) < 0) if ((fd = open(av[1], O_RDONLY)) < 0)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/20 15:14:33 by jhalford #+# #+# */ /* Created: 2017/02/20 15:14:33 by jhalford #+# #+# */
/* Updated: 2017/03/01 15:37:46 by jhalford ### ########.fr */ /* Updated: 2017/03/26 19:11:17 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -1,72 +0,0 @@
#include "ft_nm_otool.h"
void dump_section_64(struct section_64 *sect)
{
ft_printf("{blu}{inv}struct section_64{eoc} sectname=[%s]\n",
sect->sectname);
}
void dump_segment_64(t_machodata *data, struct segment_command_64 *seg)
{
uint32_t nsects;
uint32_t i;
struct section_64 *sect;
(void)data;
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_symbol(t_machodata *data, t_symbol *symbol)
{
uint8_t n_type; /* type flag, see below */
uint8_t n_sect; /* section number or NO_SECT */
uint16_t n_desc; /* see <mach-o/stab.h> */
uint64_t n_value; /* value of this symbol (or stab offset) */
char *sect_name;
n_type = symbol->nlist.n_type;
n_sect = symbol->nlist.n_sect;
n_desc = symbol->nlist.n_desc;
n_value = symbol->nlist.n_value;
DG("check");
sect_name = n_sect ?
(*(struct section_64**)ft_lst_at(data->sects, n_sect - 1)->content)->sectname : NULL;
DG("check2");
ft_printf("%i:\t%03b|%b|%x|%b\
\t%i(%s) \t%#06x \t%x %-20s\n",
symbol->pos,
(n_type & N_STAB) >> 5, (n_type & N_PEXT) >> 4,
n_type & N_TYPE, n_type & N_EXT,
n_sect, sect_name, n_desc, n_value,
symbol->string);
}
void dump_machheader_64(t_machodata *data)
{
uint32_t ncmds;
uint32_t i;
struct load_command *lc;
struct mach_header_64 *header;
header = data->file;
ncmds = header->ncmds;
lc = (void*)(header + 1);
for (i = 0; i < ncmds; i++)
{
if (lc->cmd == LC_SYMTAB)
dump_symtab(data, (struct symtab_command*)lc);
else if (lc->cmd == LC_DYSYMTAB)
dump_dysymtab(data, (struct dysymtab_command*)lc);
else if (lc->cmd == LC_SEGMENT_64)
dump_segment_64(data, (struct segment_command_64*)lc);
lc = (void*)lc + lc->cmdsize;
}
}

View file

@ -1,55 +0,0 @@
#include "ft_nm_otool.h"
t_symbolmap g_symbolmap[] =
{
{'U', sym_format_undf},
{'A', NULL},
{'T', sym_format_text},
{'D', NULL},
{'B', NULL},
{'C', NULL},
{'-', sym_format_stab},
{'S', NULL},
{'I', NULL},
};
int sym_format_undf(t_symbolmap map, t_symbol *symbol)
{
ft_printf("%16s %c %s\n", " ", map.c, symbol->string);
return (0);
}
int sym_format_text(t_symbolmap map, t_symbol *symbol)
{
char c;
c = symbol->nlist.n_type & N_EXT ? 'T' : 't';
ft_printf("%016x %c %s\n", symbol->nlist.n_value, map.c, symbol->string);
return (0);
}
int sym_format_stab(t_symbolmap map, t_symbol *symbol)
{
struct nlist_64 stab;
stab = symbol->nlist;
ft_printf("%016x %c %02x %04b %#x %s\n",
stab.n_value, map.c,
stab.n_sect, stab.n_desc, stab.n_type,
symbol->string);
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("%016x %c %s\n",
symbol->nlist.n_value, map.c, symbol->string);
return (0);
}

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* symbol_filter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/27 03:23:32 by jhalford #+# #+# */
/* Updated: 2017/03/27 03:23:32 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
int cmp_symtype(t_symbol *sym, void *type)
{
return (sym->type - *(t_type*)type);
}
int symbol_filter(t_list **symbols, t_flag flag)
{
t_type symtype;
if (!(flag & NM_ALL) && (symtype = SYM_STAB))
ft_lst_filterout(symbols, &symtype, cmp_symtype, symbol_free);
return (0);
}

View file

@ -0,0 +1,71 @@
#include "ft_nm_otool.h"
t_symbolmap g_symbolmap[] =
{
{'U', sym_format_undf},
{'A', NULL},
{'T', sym_format_text},
{'D', NULL},
{'B', NULL},
{'C', NULL},
{'-', sym_format_stab},
{'S', NULL},
{'I', NULL},
};
int sym_format_undf(t_symbolmap map, t_symbol *symbol)
{
ft_printf("%16s %c %s\n", " ", map.c, symbol->string);
return (0);
}
int sym_format_text(t_symbolmap map, t_symbol *symbol)
{
char c;
c = symbol->nlist->n_type & N_EXT ? 'T' : 't';
ft_printf("%016llx %c %s\n", symbol->nlist->n_value, map.c, symbol->string);
return (0);
}
int sym_format_stab(t_symbolmap map, t_symbol *symbol)
{
struct nlist_64 *stab;
stab = symbol->nlist;
ft_printf("%016llx %c %02x %04b %#x %s\n",
stab->n_value, map.c,
stab->n_sect, stab->n_desc, stab->n_type,
symbol->string);
return (0);
}
int symbol_format_full(t_symbol *symbol)
{
ft_printf("%i:\t%03b|%b|%x|%b \t%i(%s) \t%#06x \t%llx %-20s\n",
symbol->pos,
(symbol->nlist->n_type & N_STAB) >> 5,
(symbol->nlist->n_type & N_PEXT) >> 4,
symbol->nlist->n_type & N_TYPE, symbol->nlist->n_type & N_EXT,
symbol->nlist->n_sect, symbol->section->sectname,
symbol->nlist->n_desc, symbol->nlist->n_value,
symbol->string);
return (0);
}
int symbol_format(t_symbol *symbol)
{
t_symbolmap map;
map = g_symbolmap[symbol->type];
if (map.format)
map.format(map, symbol);
else
{
ft_printf("%016llx %c %s\n",
symbol->nlist->n_value, map.c, symbol->string);
}
return (0);
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* symbol_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/26 17:06:23 by jhalford #+# #+# */
/* Updated: 2017/03/26 17:09:53 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
void symbol_free(void *data, size_t size)
{
t_symbol *symbol;
symbol = data;
(void)size;
if (symbol)
free(symbol);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/25 21:22:06 by jhalford #+# #+# */ /* Created: 2017/03/25 21:22:06 by jhalford #+# #+# */
/* Updated: 2017/03/25 22:53:07 by jhalford ### ########.fr */ /* Updated: 2017/03/26 16:50:13 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,32 +18,31 @@ int symbol_init(t_symbol *symbol, char *stringtable, struct nlist_64 *array, int
{ {
symbol->type = 0; symbol->type = 0;
symbol->pos = i; symbol->pos = i;
symbol->nlist = array[i]; symbol->nlist = array + i;
symbol->string = stringtable + array[i].n_un.n_strx; symbol->string = stringtable + array[i].n_un.n_strx;
return (0); return (0);
} }
int symbol_set(t_symbol *symbol, t_machodata *data) int symbol_set(t_symbol *symbol, t_machodata *data)
{ {
struct nlist_64 nlist; struct nlist_64 *nlist;
uint8_t n_type; uint8_t n_type;
uint8_t type_mask; uint8_t type_mask;
char *sect_name;
nlist = symbol->nlist; nlist = symbol->nlist;
n_type = symbol->nlist.n_type; n_type = symbol->nlist->n_type;
type_mask = n_type & N_TYPE; type_mask = n_type & N_TYPE;
symbol->sect_name = symbol->nlist.n_sect ? symbol->section = symbol->nlist->n_sect ?
(*(struct section_64**)ft_lst_at(data->sects, symbol->nlist.n_sect - 1)->content)->sectname : NULL; (*(struct section_64**)ft_lst_at(data->sects, symbol->nlist->n_sect - 1)->content) : NULL;
if (n_type & N_STAB) if (n_type & N_STAB)
symbol->type = SYM_STAB; symbol->type = SYM_STAB;
else if (type_mask == N_UNDF && n_type & N_EXT && nlist.n_value != 0) else if (type_mask == N_UNDF && n_type & N_EXT && nlist->n_value != 0)
symbol->type = SYM_COMMON; symbol->type = SYM_COMMON;
else if (type_mask == N_UNDF) else if (type_mask == N_UNDF)
symbol->type = SYM_UNDF; symbol->type = SYM_UNDF;
else if (type_mask == N_SECT && ft_strcmp("__text", sect_name) == 0) else if (type_mask == N_SECT && ft_strcmp("__TEXT", symbol->section->segname) == 0)
symbol->type = SYM_TEXT; symbol->type = SYM_TEXT;
else if (type_mask == N_SECT && ft_strcmp("__data", sect_name) == 0) else if (type_mask == N_SECT && ft_strcmp("__DATA", symbol->section->segname) == 0)
symbol->type = SYM_DATA; symbol->type = SYM_DATA;
else if (type_mask == N_ABS) else if (type_mask == N_ABS)
symbol->type = SYM_ABS; symbol->type = SYM_ABS;

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* symbol_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/26 15:03:55 by jhalford #+# #+# */
/* Updated: 2017/03/26 19:20:49 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
static int sym_asort(t_symbol *sym1, t_symbol *sym2) static int sym_asort(t_symbol *sym1, t_symbol *sym2)
@ -7,12 +19,14 @@ static int sym_asort(t_symbol *sym1, t_symbol *sym2)
static int sym_nsort(t_symbol *sym1, t_symbol *sym2) static int sym_nsort(t_symbol *sym1, t_symbol *sym2)
{ {
if (sym1->nlist.n_value == sym2->nlist.n_value) if (sym1->nlist->n_value == sym2->nlist->n_value)
return (ft_strcmp(sym1->string, sym2->string)); return (sym_asort(sym1, sym2));
return (sym1->nlist.n_value - sym2->nlist.n_value); return (sym1->nlist->n_value > sym2->nlist->n_value ? 1 : -1);
} }
int symbol_sort(t_list **syms, t_flag flag) int symbol_sort(t_list **syms, t_flag flag)
{
if (!(flag & NM_NOSORT))
{ {
if (flag & NM_ASORT) if (flag & NM_ASORT)
ft_lstsort(syms, sym_asort); ft_lstsort(syms, sym_asort);
@ -20,5 +34,6 @@ int symbol_sort(t_list **syms, t_flag flag)
ft_lstsort(syms, sym_nsort); ft_lstsort(syms, sym_nsort);
if (flag & NM_RSORT) if (flag & NM_RSORT)
ft_lst_reverse(syms); ft_lst_reverse(syms);
}
return (0); return (0);
} }