Skip to content

Commit

Permalink
Fix MOD_CODE macro.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipon committed Feb 22, 2024
1 parent 31445c1 commit 83fe086
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 16 deletions.
File renamed without changes.
8 changes: 8 additions & 0 deletions cTools/include/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,13 @@
# define DLLIMPORT
#endif /* __WIN__ */

#ifndef __WIN__
# define PUSHSECTION ".pushsection"
# define POPSECTION ".popsection"
#else
# define PUSHSECTION ".section"
# define POPSECTION ".section .text"
#endif

#endif /* __OS_H */

6 changes: 3 additions & 3 deletions cTools/libs/binDynMod/binDynMod.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,21 @@
# include "fatMacho64DynMod.h"
#endif /* __MAC_OS_X__ */

BinDynMod binDynMod = {};
BinDynMod binDynMod;

static uint64_t
get_seed(void)
{
// Get seed for work with randomize adress space
BinSymPtr sym = binParser.getSymByName(binParser.bin, SYM_PREFIX"get_seed");
if (sym == NULL) {
STDERROR_PRINT("Cannot get the symbol "SYM_PREFIX"get_seed\n");
//STDERROR_PRINT("Cannot get the symbol "SYM_PREFIX"get_seed\n");
return (uint64_t)-1;
}

uint64_t sym_addr = binParser.getSSymAddr(sym);
if (sym_addr == (uint64_t)-1) {
STDERROR_PRINT("Cannot get an addr of symbol "SYM_PREFIX"get_seed\n");
//STDERROR_PRINT("Cannot get an addr of symbol "SYM_PREFIX"get_seed\n");
return (uint64_t)-1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* SOFTWARE.
*/

#include "os.h"
#include "test.h"
#include "file.h"
#include "comdef.h"
Expand Down Expand Up @@ -52,6 +53,7 @@ int main(int argc, char *argv[])
macho64PrintCodeSign(mf);
macho64PrintFixups(mf);

#ifdef __MAC_OS_X__
asm volatile (
"my_label:\n"
"1:\n"
Expand All @@ -61,6 +63,7 @@ int main(int argc, char *argv[])
".popsection\n"
::: "memory"
);
#endif /* __MAC_OS_X__ */

macho64Free(mf);

Expand Down
39 changes: 26 additions & 13 deletions cTools/libs/mod/mod_code/mod_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,43 @@
# define MOD_CODE_SECT_FLAGS "regular,pure_instructions"

#elif defined(__ELF__)
# define MOD_CODE_SEGSECT ".mod_code"
# define MOD_CODE_SECTION ".mod_code"
# define MOD_CODE_SECT_FLAGS "\"axw\""
# define MC_STRUCT_SEGSECT ".mc_struct"
# define MC_STRUCT_SECTION ".mc_struct"
# define MOD_SECT_FLAGS "\"aw\""

# define MOD_CODE_SEGSECT ".mod_code"
# define MOD_CODE_SECTION ".mod_code"
# define MOD_CODE_SECT_FLAGS "\"axw\""

#elif defined(__WIN__)
# define MC_STRUCT_SEGSECT ".mc_struct"
# define MC_STRUCT_SECTION ".mc_struct"
# define MOD_SECT_FLAGS "\"aw\""

# define MOD_CODE_SEGSECT ".mod_code"
# define MOD_CODE_SECTION ".mod_code"
# define MOD_CODE_SECT_FLAGS "\"axw\""

#else
# error "Uknonw binary format"

#endif

#if ARCH == AARCH64
#if AARCH64_DEFINED == 1
# define MOD_CODE(code) \
DEF_GUARD( \
asm volatile( \
"0:\n" \
".pushsection "MC_STRUCT_SEGSECT", "MOD_SECT_FLAGS"\n" \
PUSHSECTION" "MC_STRUCT_SEGSECT", "MOD_SECT_FLAGS"\n" \
".align 8\n" \
/* .insert_point */ \
".quad 0b\n" \
/* .start */ \
".quad 1f\n" \
/* .end */ \
".quad 2f\n" \
".popsection\n" \
".pushsection "MOD_CODE_SEGSECT", "MOD_CODE_SECT_FLAGS "\n" \
POPSECTION"\n" \
PUSHSECTION" "MOD_CODE_SEGSECT", "MOD_CODE_SECT_FLAGS "\n" \
"1:\n" \
".align 8\n" \
"stp x30, x29, [sp, #-16]!\n" \
Expand All @@ -72,25 +85,25 @@
"2:\n" \
"ldp x30, x29, [sp], #16\n" \
"ret\n" \
".popsection\n" \
POPSECTION"\n" \
::: "memory" \
); \
)
#elif ARCH == X86_64
#elif X86_64_DEFINED == 1
# define MOD_CODE(code) \
DEF_GUARD( \
asm volatile( \
"0:\n" \
".pushsection "MC_STRUCT_SEGSECT", "MOD_SECT_FLAGS"\n" \
PUSHSECTION" "MC_STRUCT_SEGSECT", "MOD_SECT_FLAGS"\n" \
".align 8\n" \
/* .insert_point */ \
".quad 0b\n" \
/* .start */ \
".quad 1f\n" \
/* .end */ \
".quad 2f\n" \
".popsection\n" \
".pushsection "MOD_CODE_SEGSECT", "MOD_CODE_SECT_FLAGS "\n" \
POPSECTION"\n" \
PUSHSECTION" "MOD_CODE_SEGSECT", "MOD_CODE_SECT_FLAGS "\n" \
"1:\n" \
".align 8\n" \
::: "memory" \
Expand All @@ -101,7 +114,7 @@
asm volatile( \
"2:\n" \
"ret\n" \
".popsection\n" \
POPSECTION"\n" \
::: "memory" \
); \
)
Expand Down

0 comments on commit 83fe086

Please sign in to comment.