Skip to content

Commit

Permalink
i#58 MacOS: assembly support
Browse files Browse the repository at this point in the history
The default assembler uses Intel mnemonics yet swaps the source and
destination order.  So for now I'm requiring a recent nasm (recent for
64-bit support) to be installed.

Several other wacky differences (including kind of shocking shortcomings in
cpp) required tweaking how assembly is done on Mac.

SVN-Revision: 2063
  • Loading branch information
derekbruening committed Apr 15, 2013
1 parent 121b374 commit 48f8d6a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
9 changes: 7 additions & 2 deletions core/x86/asm_defines.asm
Expand Up @@ -148,15 +148,20 @@ ASSUME fs:_DATA @N@\
# define END_FUNC(symbol) /* nothing */
# define DECLARE_GLOBAL(symbol) global symbol
# define GLOBAL_LABEL(label) label
# define ADDRTAKEN_LABEL(label) label
# define BYTE byte
# define WORD word
# define DWORD dword
# define QWORD qword
# define SYMREF(sym) [sym]
# ifdef X64
# define SYMREF(sym) [rel sym]
# else
# define SYMREF(sym) [sym]
# endif
# define HEX(n) 0x##n
# define SEGMEM(seg,mem) [seg:mem]
# define DECL_EXTERN(symbol) EXTERN symbol
# define RAW(n) error_not_implemented
# define RAW(n) DB HEX(n) @N@
# define DECLARE_FUNC_SEH(symbol) DECLARE_FUNC(symbol)
# define PUSH_SEH(reg) push reg
# define PUSH_NONCALLEE_SEH(reg) push reg
Expand Down
7 changes: 5 additions & 2 deletions core/x86/x86.asm
Expand Up @@ -439,7 +439,7 @@ GLOBAL_LABEL(dr_app_start:)
sub REG_XSP, FRAME_ALIGNMENT - ARG_SZ /* Maintain alignment. */

/* grab exec state and pass as param in a priv_mcontext_t struct */
PUSH_PRIV_MCXT([FRAME_ALIGNMENT - ARG_SZ + REG_XSP -
PUSH_PRIV_MCXT(PTRSZ [FRAME_ALIGNMENT - ARG_SZ + REG_XSP -
PUSH_PRIV_MCXT_PRE_PC_SHIFT]) /* return address as pc */

/* do the rest in C */
Expand Down Expand Up @@ -472,7 +472,7 @@ GLOBAL_LABEL(dynamorio_app_take_over:)
sub REG_XSP, FRAME_ALIGNMENT - ARG_SZ /* Maintain alignment. */

/* grab exec state and pass as param in a priv_mcontext_t struct */
PUSH_PRIV_MCXT([FRAME_ALIGNMENT - ARG_SZ + REG_XSP -
PUSH_PRIV_MCXT(PTRSZ [FRAME_ALIGNMENT - ARG_SZ + REG_XSP -
PUSH_PRIV_MCXT_PRE_PC_SHIFT]) /* return address as pc */

/* do the rest in C */
Expand Down Expand Up @@ -2208,6 +2208,8 @@ make_val_word_size:
jmp do_memset
END_FUNC(memset)


# ifndef MACOS /* XXX: attribute alias issue, plus using nasm */
/* gcc emits calls to these *_chk variants in release builds when the size of
* dst is known at compile time. In C, the caller is responsible for cleaning
* up arguments on the stack, so we alias these *_chk routines to the non-chk
Expand All @@ -2220,6 +2222,7 @@ make_val_word_size:
.global __memset_chk
.hidden __memset_chk
.set __memset_chk,memset
# endif


/* Replacement for _dl_runtime_resolve() used for catching module transitions
Expand Down
7 changes: 7 additions & 0 deletions make/CMake_asm.cmake
Expand Up @@ -34,4 +34,11 @@ string(REGEX REPLACE
"\@N\@"
"\n"
string "${string}")
# MacOS "cpp -E" on macros with token pasting leaves ## in place!
# And while "gcc -E" does not, it has the linker warning, so we just
# remove them here.
string(REGEX REPLACE
"##"
""
string "${string}")
file(WRITE ${file} "${string}")
4 changes: 3 additions & 1 deletion make/configure.cmake.h
Expand Up @@ -219,7 +219,9 @@
# define NOLIBC
#endif

#ifdef UNIX
#ifdef MACOS
# define ASSEMBLE_WITH_NASM
#elif defined(UNIX)
# define ASSEMBLE_WITH_GAS
#else
# define ASSEMBLE_WITH_MASM
Expand Down
38 changes: 30 additions & 8 deletions make/cpp2asm_support.cmake
Expand Up @@ -162,7 +162,22 @@ if (NOT "${CMAKE_GENERATOR}" MATCHES "Visual Studio")
enable_language(ASM)
endif ()

if (UNIX)
if (APPLE)
# See above: we can't use CMAKE_ASM_COMPILER and require nasm.
find_program(NASM nasm DOC "path to nasm assembler")
if (NOT NASM)
message(FATAL_ERROR "nasm assembler not found: required to build")
endif (NOT NASM)
message(STATUS "Found nasm: ${NASM}")
if (X64)
set(ASM_FLAGS "${ASM_FLAGS} -fmacho64")
else (X64)
set(ASM_FLAGS "${ASM_FLAGS} -fmacho32")
endif (X64)
if (DEBUG)
set(ASM_FLAGS "${ASM_FLAGS} -g")
endif (DEBUG)
elseif (UNIX)
set(ASM_FLAGS "-mmnemonic=intel -msyntax=intel -mnaked-reg --noexecstack")
if (X64)
set(ASM_FLAGS "${ASM_FLAGS} --64")
Expand All @@ -173,7 +188,7 @@ if (UNIX)
if (DEBUG)
set(ASM_FLAGS "${ASM_FLAGS} -g")
endif (DEBUG)
else (UNIX)
else ()
if (X64)
find_program(CMAKE_ASM_COMPILER ml64.exe HINTS "${cl_path}" DOC "path to assembler")
else (X64)
Expand All @@ -189,11 +204,11 @@ else (UNIX)
set(ASM_DBG "")
endif ()
set(ASM_FLAGS "/nologo ${ASM_DBG}")
endif (UNIX)
endif ()

if (APPLE)
# XXX: xcode assembler doesn't seem to take any kind of --help or --version
# so for now we assume it supports Intel
# XXX: xcode assembler uses Intel mnemonics but opposite src,dst order!
# We rely on having nasm installed as a workaround.
set(CMAKE_ASM_SUPPORTS_INTEL_SYNTAX ON)
endif (APPLE)
if (UNIX AND NOT APPLE)
Expand Down Expand Up @@ -230,7 +245,14 @@ endif (UNIX AND NOT APPLE)
##################################################
# Assembler build rule for Makefile generators

if (UNIX)
if (APPLE)
# Despite the docs, -o does not work: cpp prints to stdout.
set(CMAKE_ASM_COMPILE_OBJECT
"${CMAKE_CPP} ${CMAKE_CPP_FLAGS} <FLAGS> <DEFINES> -E <SOURCE> > <OBJECT>.s"
"<CMAKE_COMMAND> -Dfile=<OBJECT>.s -P \"${cpp2asm_newline_script_path}\""
"<NASM> ${ASM_FLAGS} -o <OBJECT> <OBJECT>.s"
)
elseif (UNIX)
# we used to have ".ifdef FOO" and to not have it turn into ".ifdef 1" we'd say
# "-DFOO=FOO", but we now use exclusively preprocessor defines, which is good
# since our defines are mostly in configure.h where we can't as easily tweak them
Expand All @@ -253,7 +275,7 @@ if (UNIX)
# comes up empty for me.
"<CMAKE_ASM_COMPILER> ${ASM_FLAGS} -o <OBJECT> <OBJECT>.s"
)
else (UNIX)
else ()
# Even if we didn't preprocess we'd need our own rule since cmake doesn't
# support ml.
set(CMAKE_ASM_COMPILE_OBJECT
Expand All @@ -269,7 +291,7 @@ else (UNIX)
"<CMAKE_COMMAND> -Dfile=<OBJECT>.s -P \"${cpp2asm_newline_script_path}\""
"<CMAKE_ASM_COMPILER> ${ASM_FLAGS} /c /Fo<OBJECT> <OBJECT>.s"
)
endif (UNIX)
endif ()

##################################################
# Assembler build commands for Visual Studio generators and
Expand Down

0 comments on commit 48f8d6a

Please sign in to comment.