Skip to content

Commit

Permalink
do not use constructor to enable archs, so code is more portable. sug…
Browse files Browse the repository at this point in the history
…gested by Alex Ionescu
  • Loading branch information
aquynh committed Jan 19, 2014
1 parent a580d92 commit c272e9d
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 20 deletions.
2 changes: 1 addition & 1 deletion arch/AArch64/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static void destroy(cs_struct *handle)
{
}

static void __attribute__ ((constructor)) __init_arm64__()
void AArch64_enable(void)
{
arch_init[CS_ARCH_ARM64] = init;
arch_option[CS_ARCH_ARM64] = option;
Expand Down
4 changes: 1 addition & 3 deletions arch/ARM/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include "ARMInstPrinter.h"
#include "mapping.h"

void enable_arm() {};

static cs_err init(cs_struct *ud)
{
MCRegisterInfo *mri = cs_mem_malloc(sizeof(*mri));
Expand Down Expand Up @@ -48,7 +46,7 @@ static void destroy(cs_struct *handle)
{
}

static void __attribute__ ((constructor)) __init_arm__()
void ARM_enable(void)
{
arch_init[CS_ARCH_ARM] = init;
arch_option[CS_ARCH_ARM] = option;
Expand Down
2 changes: 1 addition & 1 deletion arch/Mips/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static void destroy(cs_struct *handle)
{
}

static void __attribute__ ((constructor)) __init_mips__()
void Mips_enable(void)
{
arch_init[CS_ARCH_MIPS] = init;
arch_option[CS_ARCH_MIPS] = option;
Expand Down
2 changes: 1 addition & 1 deletion arch/PowerPC/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static void destroy(cs_struct *handle)
{
}

static void __attribute__ ((constructor)) __init_mips__()
void PPC_enable(void)
{
arch_init[CS_ARCH_PPC] = init;
arch_option[CS_ARCH_PPC] = option;
Expand Down
2 changes: 1 addition & 1 deletion arch/X86/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static void destroy(cs_struct *handle)
{
}

static void __attribute__ ((constructor)) __init_x86__()
void X86_enable(void)
{
arch_init[CS_ARCH_X86] = init;
arch_option[CS_ARCH_X86] = option;
Expand Down
34 changes: 21 additions & 13 deletions cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,36 @@ cs_err (*arch_init[MAX_ARCH])(cs_struct *) = { NULL };
cs_err (*arch_option[MAX_ARCH]) (cs_struct *, cs_opt_type, size_t value) = { NULL };
void (*arch_destroy[MAX_ARCH]) (cs_struct *) = { NULL };

// we need this trick to enable module constructors in static lib
extern void enable_arm();
extern void enable_arm64();
extern void enable_mips();
extern void enable_x86();
extern void enable_powerpc();

void enable_construct()
extern void ARM_enable(void);
extern void AArch64_enable(void);
extern void Mips_enable(void);
extern void X86_enable(void);
extern void PPC_enable(void);

static void archs_enable(void)
{
static bool initialized = false;

if (initialized)
return;

#ifdef CAPSTONE_HAS_ARM
enable_arm();
ARM_enable();
#endif
#ifdef CAPSTONE_HAS_ARM64
enable_arm64();
AArch64_enable();
#endif
#ifdef CAPSTONE_HAS_MIPS
enable_mips();
Mips_enable();
#endif
#ifdef CAPSTONE_HAS_X86
enable_x86();
X86_enable();
#endif
#ifdef CAPSTONE_HAS_POWERPC
enable_powerpc();
PPC_enable();
#endif

initialized = true;
}

unsigned int all_arch = 0;
Expand Down Expand Up @@ -119,6 +125,8 @@ cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
// with cs_option(CS_OPT_MEM)
return CS_ERR_MEMSETUP;

archs_enable();

if (arch < CS_ARCH_MAX && arch_init[arch]) {
cs_struct *ud;

Expand Down

0 comments on commit c272e9d

Please sign in to comment.