1 days work, still need to sort out archs for universal binaries

This commit is contained in:
Jack Halford 2017-10-30 18:38:24 +01:00
parent 33b7b25abe
commit 1103db79be
13 changed files with 197 additions and 25 deletions

31
nm-otool/42corr.sh Executable file
View file

@ -0,0 +1,31 @@
#!/bin/sh
make
##########
# NM
##########
#test_facile
./nmdiff.sh tests/test_facile
#test_moins_facile en 64 et 32
./nmdiff.sh tests/test_moins_facile
./nmdiff.sh tests/test_moins_facile_32-bit
#arguments mutilples
./nmdiff.sh tests/test_facile tests/test_facile
#fichiers objets
./nmdiff.sh objs/ft_nm.o
#bibliotheques dynamiques
./nmdiff.sh
#biblioteque universelle
./nmdiff.sh
##########
# OTOOL
##########

View file

@ -34,7 +34,10 @@ NM_OBJ = $(OBJ_DIR)ft_nm.o
OTOOL_OBJ = $(OBJ_DIR)ft_otool.o
SRC_BASE = \
bswap.c\
dump_symtab.c\
endianness.c\
fat.c\
ft_nm.c\
ft_otool.c\
mach/get_section.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/20 14:36:10 by jhalford #+# #+# */
/* Updated: 2017/10/30 12:00:14 by jhalford ### ########.fr */
/* Updated: 2017/10/30 16:37:24 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,9 +31,19 @@
# include "mach_64.h"
# include "mach.h"
# define IS_MACH_64(x) (x == MH_MAGIC_64 || x == MH_CIGAM_64)
# define IS_MACH_32(x) (x == MH_MAGIC || x == MH_CIGAM)
# define IS_FAT(x) (x == FAT_MAGIC || x == FAT_CIGAM)
# define IS_REV(x) (x == MH_CIGAM || x == MH_CIGAM_64 || x == FAT_CIGAM)
uint64_t endian(uint64_t n, uint8_t size);
uint8_t bswap_8(uint8_t x);
uint16_t bswap_16(uint16_t x);
uint32_t bswap_32(uint32_t x);
uint64_t bswap_64(uint64_t x);
void *fat_extract(struct fat_header *fat,
cpu_type_t cputype,
cpu_subtype_t cpusubtype);
#endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/20 15:41:59 by jhalford #+# #+# */
/* Updated: 2017/05/16 17:30:35 by ariard ### ########.fr */
/* Updated: 2017/10/30 15:43:39 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -1,5 +1,4 @@
#!/usr/bin/env zsh
make
diff <(./ft_nm $@) <(nm $@)
if [ $? -eq 0 ]; then
echo "OK ✅"

View file

@ -1,6 +1,5 @@
#!/usr/bin/env zsh
make
diff <(./ft_otool $@) <(otool -t $@)
diff <(./ft_otool $@) <(otool $@)
if [ $? -eq 0 ]; then
echo "OK ✅"
else

44
nm-otool/srcs/bswap.c Normal file
View file

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bswap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 15:20:32 by jhalford #+# #+# */
/* Updated: 2017/10/30 15:53:14 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
inline uint8_t bswap_8(uint8_t x)
{
return (x);
}
inline uint16_t bswap_16(uint16_t x)
{
return (( (x >> 8) & 0xffu) | (
(x & 0xffu) << 8));
}
inline uint32_t bswap_32(uint32_t x)
{
return (((x & 0xff000000u) >> 24)
| ((x & 0x00ff0000u) >> 8)
| ((x & 0x0000ff00u) << 8)
| ((x & 0x000000ffu) << 24));
}
inline uint64_t bswap_64(uint64_t x)
{
return (((x & 0xff00000000000000ull) >> 56)
| ((x & 0x00ff000000000000ull) >> 40)
| ((x & 0x0000ff0000000000ull) >> 24)
| ((x & 0x000000ff00000000ull) >> 8)
| ((x & 0x00000000ff000000ull) << 8)
| ((x & 0x0000000000ff0000ull) << 24)
| ((x & 0x000000000000ff00ull) << 40)
| ((x & 0x00000000000000ffull) << 56));
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* endianese.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 15:18:54 by jhalford #+# #+# */
/* Updated: 2017/10/30 15:58:55 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
int g_rev;
inline uint64_t endian(uint64_t n, uint8_t size)
{
if (g_rev)
{
if (size == 8)
return (bswap_8(n));
else if (size == 16)
return (bswap_16(n));
else if (size == 32)
return (bswap_32(n));
else if (size == 64)
return (bswap_64(n));
return (bswap_32(n));
}
return (n);
}

34
nm-otool/srcs/fat.c Normal file
View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fat.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 16:31:48 by jhalford #+# #+# */
/* Updated: 2017/10/30 17:49:28 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
void *fat_extract(struct fat_header *fat,
cpu_type_t cputype,
cpu_subtype_t cpusubtype)
{
uint32_t narch;
struct fat_arch *arch;
narch = endian(fat->nfat_arch, 32);
arch = (struct fat_arch*)(fat + 1);
while (narch--)
{
if (endian(arch->cputype, 32) & cputype
&& endian(arch->cpusubtype, 32) & cpusubtype)
{
return ((void *)fat + endian(arch->offset, 32));
}
++arch;
}
return (NULL);
}

View file

@ -6,14 +6,16 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/19 03:09:12 by jhalford #+# #+# */
/* Updated: 2017/10/30 12:26:03 by jhalford ### ########.fr */
/* Updated: 2017/10/30 18:36:32 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
#include <sys/utsname.h>
#define NM_USAGE "usage: nm [-agmnpruU] filename ..."
t_machodata *g_data = NULL;
int g_rev = 0;
t_cliopts g_nm_opts[] =
{
@ -29,23 +31,42 @@ t_cliopts g_nm_opts[] =
{0xff, "full", NM_FULL, 0, NULL, 0},
{'o', NULL, NM_OFORMAT, 0, NULL, 0},
{'m', NULL, NM_MFORMAT, 0, NULL, 0},
{'A', NULL, 0, 0, NULL, 0},
{'x', NULL, 0, 0, NULL, 0},
{'j', NULL, 0, 0, NULL, 0},
};
int nm_file(void *file, t_nmdata *data);
int nm_fat(struct fat_header *fat, t_nmdata *data)
{
void *arch;
struct utsname host;
uname(&host);
NXGetLocalArchInfo();
arch = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL);
if (!arch)
ft_printf("{red}fat doesn't have x86_64 !{eoc}");
else
{
uint32_t magic;
magic = *(int*)arch;
nm_file(arch, data);
}
return (0);
}
int nm_file(void *file, t_nmdata *data)
{
uint32_t magic;
magic = *(int*)file;
g_rev = IS_REV(magic);
if (IS_MACH_32(magic))
nm_mach(file, data);
else if (IS_MACH_64(magic))
nm_mach_64(file, data);
else if (IS_FAT(magic))
ft_printf("{red}unsupported arch:{eoc} magic=%#x(FAT)\n", magic);
nm_fat(file, data);
else
ft_printf("{red}unsupported arch:{eoc} magic=%#x\n", magic);
return (0);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/20 14:08:14 by jhalford #+# #+# */
/* Updated: 2017/10/30 12:26:10 by jhalford ### ########.fr */
/* Updated: 2017/10/30 17:31:44 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,12 +18,14 @@ t_cliopts g_otool_opts[] =
{'t', NULL, OTOOL_TEXT, 0, NULL, 0},
{'d', NULL, OTOOL_DATA, 0, NULL, 0},
};
int g_rev = 0;
void otool_file(void *file, t_otooldata *data)
{
uint32_t magic;
magic = *(int *)file;
g_rev = IS_REV(magic);
if (IS_MACH_32(magic))
otool_mach(file, data);
else if (IS_MACH_64(magic))
@ -44,7 +46,6 @@ int otool(int ac, char **av, t_otooldata data)
i = data.av_data - av;
while (i < ac && av[i])
{
if (!(data.flag & NM_OFORMAT) && ac - (data.av_data - av) > 1)
ft_printf("%s:\n", av[i]);
if ((fd = open((av[i]), O_RDONLY)) < 0)
return (1);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/23 16:06:44 by jhalford #+# #+# */
/* Updated: 2017/10/30 11:32:35 by jhalford ### ########.fr */
/* Updated: 2017/10/30 16:58:03 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,8 +20,8 @@ static void symtab_64_parse(t_machodata *data, struct symtab_command *symtab)
struct nlist_64 *array;
data->symtab = symtab;
stringtable = data->file + symtab->stroff;
array = (struct nlist_64*)(data->file + symtab->symoff);
stringtable = data->file + endian(symtab->stroff, 32);
array = (struct nlist_64*)(data->file + endian(symtab->symoff, 32));
i = -1;
while (++i < (int)symtab->nsyms)
{
@ -37,7 +37,7 @@ static void seg_64_parse(t_machodata *data, struct segment_command_64 *seg)
uint32_t i;
struct section_64 *sect;
nsects = seg->nsects;
nsects = endian(seg->nsects, 32);
sect = (void*)(seg + 1);
i = -1;
while (++i < nsects)
@ -55,17 +55,15 @@ void mach_64_parse(t_machodata *data)
struct mach_header_64 *header;
header = data->file;
ncmds = header->ncmds;
ncmds = endian(header->ncmds, 32);
lc = (void*)(header + 1);
i = -1;
while (++i < ncmds)
{
if (lc->cmd == LC_SYMTAB)
if (endian(lc->cmd, 32) == LC_SYMTAB)
symtab_64_parse(data, (struct symtab_command*)lc);
else if (lc->cmd == LC_DYSYMTAB)
data->dysymtab = (struct dysymtab_command*)lc;
else if (lc->cmd == LC_SEGMENT_64)
seg_64_parse(data, (struct segment_command_64*)lc);
lc = (void*)lc + lc->cmdsize;
lc = (void*)lc + endian(lc->cmdsize, 32);
}
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:03:04 by jhalford #+# #+# */
/* Updated: 2017/10/30 11:35:53 by jhalford ### ########.fr */
/* Updated: 2017/10/30 15:50:23 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */