Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for macOS build #7

Closed
stek29 opened this issue Dec 13, 2017 · 14 comments
Closed

Add support for macOS build #7

stek29 opened this issue Dec 13, 2017 · 14 comments

Comments

@stek29
Copy link
Contributor

stek29 commented Dec 13, 2017

No description provided.

@stonedreamforest
Copy link

stonedreamforest commented Dec 13, 2017

readme.md:
1

@stek29
Copy link
Contributor Author

stek29 commented Dec 13, 2017

@stonedreamforest That's why I created an issue

@s3rvac s3rvac changed the title macOS build Add support for macOS build Dec 17, 2017
@mabrowning
Copy link

mabrowning commented Dec 19, 2017

I've successfully built bin2llvmir & llvmir2hll on macOS:

$ clang --version
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin17.2.0

I had to install a few packages, use homebrew's bison, fix a few compilation issues & add build script support.

brew install ncurses bison flex

flex and bison are keg only, so add their paths to PATH:

$ which bison flex
/usr/local/opt/bison/bin/bison
/usr/local/opt/flex/bin/flex

For the build script changes... there are so many submodules, I don't know where to start filing pull requests, so attached is a recursive diff of my source tree.

macOS.patch

Reproduced below:

diff --git a/deps/capstone2llvmir/deps/llvm/CMakeLists.txt b/deps/capstone2llvmir/deps/llvm/CMakeLists.txt
index 7a2ad0c..492056e 100644
--- a/deps/capstone2llvmir/deps/llvm/CMakeLists.txt
+++ b/deps/capstone2llvmir/deps/llvm/CMakeLists.txt
@@ -206,7 +206,10 @@ if(CMAKE_THREAD_LIBS_INIT)
 	target_link_libraries(llvm INTERFACE optimized ${CMAKE_THREAD_LIBS_INIT})
 endif()
 
-if(UNIX)
+if(APPLE)
+	target_link_libraries(llvm INTERFACE debug     ${ZLIB_LIBRARIES} curses )
+	target_link_libraries(llvm INTERFACE optimized ${ZLIB_LIBRARIES} curses )
+elseif(UNIX)
 	target_link_libraries(llvm INTERFACE debug     ${ZLIB_LIBRARIES} rt dl tinfo)
 	target_link_libraries(llvm INTERFACE optimized ${ZLIB_LIBRARIES} rt dl tinfo)
 endif()
diff --git a/deps/fileformat/deps/openssl/CMakeLists.txt b/deps/fileformat/deps/openssl/CMakeLists.txt
index afa11d3..ec62f93 100644
--- a/deps/fileformat/deps/openssl/CMakeLists.txt
+++ b/deps/fileformat/deps/openssl/CMakeLists.txt
@@ -15,6 +15,8 @@ endif()
 
 if(WIN32)
 	set(OS_WINDOWS 1)
+elseif(APPLE)
+    set(OS_MACOS 1)
 elseif(UNIX)
 	set(OS_LINUX 1)
 else()
@@ -27,7 +29,7 @@ if(OS_WINDOWS)
 	else()
 		message(ERROR "Unsupported compiler. Supported: MSVC.")
 	endif()
-else(OS_LINUX)
+elseif(OS_LINUX)
 	if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
 		set(COMPILER_GCC 1)
 	elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
@@ -46,6 +48,13 @@ if(OS_WINDOWS)
 			set(OPENSSL_CONFIGURE_ARCH "VC-WIN64A")
 		endif()
 	endif()
+elseif(OS_MACOS)
+    set(OPENSSL_MAKE make -j${CPUS})
+	if(ARCH_32BIT)
+        set(OPENSSL_CONFIGURE_ARCH "darwin-i386-cc")
+	elseif(ARCH_64BIT)
+        set(OPENSSL_CONFIGURE_ARCH "darwin64-x86_64-cc")
+	endif()
 elseif(OS_LINUX)
     set(OPENSSL_MAKE make -j${CPUS})
 	if(ARCH_32BIT)
diff --git a/deps/fileformat/include/fileformat/file_format/intel_hex/intel_hex_parser/intel_hex_parser.h b/deps/fileformat/include/fileformat/file_format/intel_hex/intel_hex_parser/intel_hex_parser.h
index 1db74fe..2a52c7a 100644
--- a/deps/fileformat/include/fileformat/file_format/intel_hex/intel_hex_parser/intel_hex_parser.h
+++ b/deps/fileformat/include/fileformat/file_format/intel_hex/intel_hex_parser/intel_hex_parser.h
@@ -31,7 +31,7 @@ class IntelHexSection
 
 		/// @name Operators
 		/// @{
-		bool operator<(IntelHexSection const &a);
+		bool operator<(IntelHexSection const &a) const;
 		/// @}
 };
 
diff --git a/deps/fileformat/src/ar-extractor/archive_wrapper.cpp b/deps/fileformat/src/ar-extractor/archive_wrapper.cpp
index 3a654eb..86e2b10 100644
--- a/deps/fileformat/src/ar-extractor/archive_wrapper.cpp
+++ b/deps/fileformat/src/ar-extractor/archive_wrapper.cpp
@@ -227,7 +227,7 @@ bool ArchiveWrapper::getJsonList(
 			Value object(kObjectType);
 			object.AddMember("name", Value(outName.c_str(), allocator).Move(), allocator);
 			if (numbers) {
-				object.AddMember("index", counter++, allocator);
+				object.AddMember("index", (uint64_t)counter++, allocator);
 			}
 			objects.PushBack(object, allocator);
 		}
diff --git a/deps/fileformat/src/fileformat/file_format/intel_hex/intel_hex_parser/intel_hex_parser.cpp b/deps/fileformat/src/fileformat/file_format/intel_hex/intel_hex_parser/intel_hex_parser.cpp
index 5c73935..12704da 100644
--- a/deps/fileformat/src/fileformat/file_format/intel_hex/intel_hex_parser/intel_hex_parser.cpp
+++ b/deps/fileformat/src/fileformat/file_format/intel_hex/intel_hex_parser/intel_hex_parser.cpp
@@ -32,7 +32,7 @@ IntelHexSection::~IntelHexSection()
 /**
  * operator <
  */
-bool IntelHexSection::operator<(const IntelHexSection &a)
+bool IntelHexSection::operator<(const IntelHexSection &a) const
 {
 	return address < a.address;
 }
diff --git a/deps/fileformat/src/unpackertool/CMakeLists.txt b/deps/fileformat/src/unpackertool/CMakeLists.txt
index 94ce8d5..0c7ba44 100644
--- a/deps/fileformat/src/unpackertool/CMakeLists.txt
+++ b/deps/fileformat/src/unpackertool/CMakeLists.txt
@@ -16,7 +16,9 @@ if(NOT TARGET unpackertool)
 	# 3. The libraries that are used in plugins under Windows are linked to plugins themselves in plugins/*/CMakeLists.txt
 	# 4. The libraries that are used both by core and plugins need to be linked as whole-archive under Linux, and under Windows like in 1 and 3 simultaneously
 	target_link_libraries(unpackertool cpdetect)
-	if(UNIX)
+    if(APPLE)
+		target_link_libraries(unpackertool -rdynamic -Wl,-force_load tl-cpputils unpacker loader pelib dl)
+    elseif(UNIX)
 		target_link_libraries(unpackertool -rdynamic -Wl,-whole-archive tl-cpputils unpacker loader pelib -Wl,-no-whole-archive dl)
 	elseif(MSVC)
 		target_link_libraries(unpackertool shlwapi)
diff --git a/deps/fileformat/src/unpackertool/plugins/mpress/CMakeLists.txt b/deps/fileformat/src/unpackertool/plugins/mpress/CMakeLists.txt
index c90c342..950faa6 100644
--- a/deps/fileformat/src/unpackertool/plugins/mpress/CMakeLists.txt
+++ b/deps/fileformat/src/unpackertool/plugins/mpress/CMakeLists.txt
@@ -9,6 +9,8 @@ target_include_directories(mpress PUBLIC ${PROJECT_SOURCE_DIR}/src/)
 # Plugin related libraries are linked to the plugin on Windows
 if(MSVC)
 	target_link_libraries(mpress unpacker)
+elseif(APPLE)
+	target_link_libraries(mpress unpacker)
 endif()
 install(TARGETS mpress
 	LIBRARY DESTINATION bin/unpacker-plugins
diff --git a/deps/fileformat/src/unpackertool/plugins/upx/CMakeLists.txt b/deps/fileformat/src/unpackertool/plugins/upx/CMakeLists.txt
index f4f34a9..94c5c78 100644
--- a/deps/fileformat/src/unpackertool/plugins/upx/CMakeLists.txt
+++ b/deps/fileformat/src/unpackertool/plugins/upx/CMakeLists.txt
@@ -21,6 +21,8 @@ target_include_directories(upx PUBLIC ${PROJECT_SOURCE_DIR}/src/)
 # Plugin related libraries are linked to the plugin on Windows
 if(MSVC)
 	target_link_libraries(upx tl-cpputils unpacker)
+elseif(APPLE)
+	target_link_libraries(upx tl-cpputils unpacker)
 endif()
 install(TARGETS upx
 	LIBRARY DESTINATION bin/unpacker-plugins
diff --git a/deps/fnc-patterns/deps/fileformat/deps/openssl/CMakeLists.txt b/deps/fnc-patterns/deps/fileformat/deps/openssl/CMakeLists.txt
index afa11d3..ec62f93 100644
--- a/deps/fnc-patterns/deps/fileformat/deps/openssl/CMakeLists.txt
+++ b/deps/fnc-patterns/deps/fileformat/deps/openssl/CMakeLists.txt
@@ -15,6 +15,8 @@ endif()
 
 if(WIN32)
 	set(OS_WINDOWS 1)
+elseif(APPLE)
+    set(OS_MACOS 1)
 elseif(UNIX)
 	set(OS_LINUX 1)
 else()
@@ -27,7 +29,7 @@ if(OS_WINDOWS)
 	else()
 		message(ERROR "Unsupported compiler. Supported: MSVC.")
 	endif()
-else(OS_LINUX)
+elseif(OS_LINUX)
 	if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
 		set(COMPILER_GCC 1)
 	elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
@@ -46,6 +48,13 @@ if(OS_WINDOWS)
 			set(OPENSSL_CONFIGURE_ARCH "VC-WIN64A")
 		endif()
 	endif()
+elseif(OS_MACOS)
+    set(OPENSSL_MAKE make -j${CPUS})
+	if(ARCH_32BIT)
+        set(OPENSSL_CONFIGURE_ARCH "darwin-i386-cc")
+	elseif(ARCH_64BIT)
+        set(OPENSSL_CONFIGURE_ARCH "darwin64-x86_64-cc")
+	endif()
 elseif(OS_LINUX)
     set(OPENSSL_MAKE make -j${CPUS})
 	if(ARCH_32BIT)
diff --git a/src/bin2llvmirtool/CMakeLists.txt b/src/bin2llvmirtool/CMakeLists.txt
index 77b21d2..7d754fa 100644
--- a/src/bin2llvmirtool/CMakeLists.txt
+++ b/src/bin2llvmirtool/CMakeLists.txt
@@ -14,6 +14,8 @@ if(MSVC)
 	# its properties, second as path to library to link it as a whole.
 	target_link_libraries(bin2llvmirtool bin2llvmir -WHOLEARCHIVE:$<TARGET_FILE_NAME:bin2llvmir>)
 	set_target_properties(bin2llvmirtool PROPERTIES LINK_FLAGS "/FORCE:MULTIPLE")
+elseif(APPLE)
+    target_link_libraries(bin2llvmirtool -Wl,-force_load bin2llvmir )
 else() # Linux/MSYS2
 	target_link_libraries(bin2llvmirtool -Wl,--whole-archive bin2llvmir -Wl,--no-whole-archive)
 endif()
diff --git a/src/llvmir2hlltool/CMakeLists.txt b/src/llvmir2hlltool/CMakeLists.txt
index 72468e2..21c554c 100644
--- a/src/llvmir2hlltool/CMakeLists.txt
+++ b/src/llvmir2hlltool/CMakeLists.txt
@@ -13,6 +13,8 @@ if(MSVC)
 	# state 'llvmir2hll' twice in target_link_libraries(), first as a target to get
 	# its properties, second as path to library to link it as a whole.
 	target_link_libraries(llvmir2hlltool llvmir2hll -WHOLEARCHIVE:$<TARGET_FILE_NAME:llvmir2hll>)
+elseif(APPLE) # macOS
+	target_link_libraries(llvmir2hlltool -Wl,-force_load llvmir2hll)
 else() # Linux/MSYS2
 	target_link_libraries(llvmir2hlltool -Wl,--whole-archive llvmir2hll -Wl,--no-whole-archive)
 endif()


Now I just need to figure out how to use it...

@stek29
Copy link
Contributor Author

stek29 commented Dec 20, 2017

@mabrowning but why?.. there's #17, everything was done already :}

@mabrowning
Copy link

Pulled down the code a week ago when first announced, started porting to macOS, came back a week later and didn't check if anyone else had done the work. Finished up and posted my results..... oops!

@metthal
Copy link
Member

metthal commented Dec 20, 2017

I have merged all PRs related to macOS build in all our repositories. Let me know if I missed anything. Also, I added disclaimer that we do not officially support macOS, because we have no way to check whether macOS build is passing or not. We will change that as soon as we will get some macOS machine. Thank you all for assistance.

@sarnau
Copy link

sarnau commented Dec 21, 2017

At least one fix seems to be missing:

Scanning dependencies of target unpackertool
[ 60%] Building CXX object deps/fileformat/src/unpackertool/CMakeFiles/unpackertool.dir/arg_handler.cpp.o
[ 60%] Building CXX object deps/fileformat/src/unpackertool/CMakeFiles/unpackertool.dir/unpacker.cpp.o
[ 60%] Building CXX object deps/fileformat/src/unpackertool/CMakeFiles/unpackertool.dir/plugin_mgr.cpp.o
[ 60%] Linking CXX executable unpacker
ld: unknown option: -force-load
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [deps/fileformat/src/unpackertool/unpacker] Error 1
make[1]: *** [deps/fileformat/src/unpackertool/CMakeFiles/unpackertool.dir/all] Error 2
make: *** [all] Error 2

@metthal
Copy link
Member

metthal commented Dec 21, 2017

Try now (master at 5832921). I noticed that the linker flag is actually called force_load, not force-load.

@sarnau
Copy link

sarnau commented Dec 23, 2017

Yes, it now compiles without any local changes. Great job!

@newkit
Copy link

newkit commented Dec 31, 2017

Just out of curiousity - how did you avoid that the original Apple flexversion got picked up? My build was failing until I renamed the original Apple files.

@mabrowning
Copy link

mabrowning commented Dec 31, 2017 via email

@newkit
Copy link

newkit commented Dec 31, 2017

Okay, thanks for clarification. Method 1 was what I was doing but apperently that was not enough or maybe the order (appending at the end / inserting at beginnung of $PATH) is key.

@stek29
Copy link
Contributor Author

stek29 commented Jan 1, 2018

@newkit Reading the README might be useful.

# Apple ships old Flex & Bison, so Homebrew versions should be used.
export CMAKE_INCLUDE_PATH="/usr/local/opt/flex/include"
export CMAKE_LIBRARY_PATH="/usr/local/opt/flex/lib;/usr/local/opt/bison/lib"
export PATH="/usr/local/opt/flex/bin:/usr/local/opt/bison/bin:$PATH"

@newkit
Copy link

newkit commented Jan 1, 2018

I know this sounds like I am unable to do proper RTFM'ing. But I just double checked. I had set everything up as described in the README and ran into the issue due to the old flex version picked up by yaramod.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants