From 2600868097cb5b037c43d5c619c40d3328f15e4b Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Fri, 23 Feb 2024 17:14:57 +0100 Subject: [PATCH 01/22] Attempt to build `as.exe` on Windows --- build-llvm.cmd | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/build-llvm.cmd b/build-llvm.cmd index f7bd14a..048cd3d 100644 --- a/build-llvm.cmd +++ b/build-llvm.cmd @@ -3,6 +3,8 @@ set HOST=windows set BUILD_DIR=%MY_DIR%\xa-build set ARTIFACTS_DIR=%MY_DIR%\artifacts set SOURCE_DIR=%MY_DIR%external\llvm\llvm +set XA_TOOLS_SOURCE_DIR=%MY_DIR\src +set XA_TOOLS_BUILD_DIR=%MY_DIR\xa-build set PROJECTS=lld set TARGETS=X86;ARM;AArch64 @@ -75,3 +77,14 @@ for %%p in (%PDBS%) DO ( ) cd %MY_DIR% + +mkdir %XA_TOOLS_BUILD_DIR% +cd %XA_TOOLS_BUILD_DIR% + +cmake -G "Visual Studio 17 2022" -A x64 ^ + -DCMAKE_EXE_LINKER_FLAGS_INIT="/PROFILE /DYNAMICBASE /CETCOMPAT /guard:cf" ^ + -DCMAKE_BUILD_TYPE=Release ^ + -DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded" ^ + %XA_TOOLS_BUILD_DIR% + +msbuild /p:Configuration=Release From 75697d16df2fc156ff4412683c629d6665c5545d Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Fri, 23 Feb 2024 17:16:35 +0100 Subject: [PATCH 02/22] Build `gas` first, for quicker testing --- build-llvm.cmd | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/build-llvm.cmd b/build-llvm.cmd index 048cd3d..dcd4ed0 100644 --- a/build-llvm.cmd +++ b/build-llvm.cmd @@ -23,6 +23,20 @@ mkdir %HOST_BUILD_DIR% if exist %HOST_ARTIFACTS_DIR% (rmdir /S /Q %HOST_ARTIFACTS_DIR%) mkdir %HOST_ARTIFACTS_DIR%\bin +if exist %XA_TOOLS_BUILD_DIR% (rmdir /S / %XA_TOOLS_BUILD_DIR%) +mkdir %XA_TOOLS_BUILD_DIR% + +cd %XA_TOOLS_BUILD_DIR% + +cmake -G "Visual Studio 17 2022" -A x64 ^ + -DCMAKE_EXE_LINKER_FLAGS_INIT="/PROFILE /DYNAMICBASE /CETCOMPAT /guard:cf" ^ + -DCMAKE_BUILD_TYPE=Release ^ + -DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded" ^ + %XA_TOOLS_BUILD_DIR% + +msbuild /p:Configuration=Release +cd %MY_DIR + cd %HOST_BUILD_DIR% cmake --version @@ -77,14 +91,3 @@ for %%p in (%PDBS%) DO ( ) cd %MY_DIR% - -mkdir %XA_TOOLS_BUILD_DIR% -cd %XA_TOOLS_BUILD_DIR% - -cmake -G "Visual Studio 17 2022" -A x64 ^ - -DCMAKE_EXE_LINKER_FLAGS_INIT="/PROFILE /DYNAMICBASE /CETCOMPAT /guard:cf" ^ - -DCMAKE_BUILD_TYPE=Release ^ - -DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded" ^ - %XA_TOOLS_BUILD_DIR% - -msbuild /p:Configuration=Release From b32911b96dff854c2a038c6914b41a3122812c5d Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Fri, 23 Feb 2024 17:21:18 +0100 Subject: [PATCH 03/22] Syntax, fool --- build-llvm.cmd | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/build-llvm.cmd b/build-llvm.cmd index dcd4ed0..d58bd64 100644 --- a/build-llvm.cmd +++ b/build-llvm.cmd @@ -3,8 +3,8 @@ set HOST=windows set BUILD_DIR=%MY_DIR%\xa-build set ARTIFACTS_DIR=%MY_DIR%\artifacts set SOURCE_DIR=%MY_DIR%external\llvm\llvm -set XA_TOOLS_SOURCE_DIR=%MY_DIR\src -set XA_TOOLS_BUILD_DIR=%MY_DIR\xa-build +set XA_TOOLS_SOURCE_DIR=%MY_DIR%\src +set XA_TOOLS_BUILD_DIR=%MY_DIR%\xa-build set PROJECTS=lld set TARGETS=X86;ARM;AArch64 @@ -35,7 +35,7 @@ cmake -G "Visual Studio 17 2022" -A x64 ^ %XA_TOOLS_BUILD_DIR% msbuild /p:Configuration=Release -cd %MY_DIR +cd %MY_DIR% cd %HOST_BUILD_DIR% From 4fbea9e451cac766fac23d197b793f14a3531e07 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Fri, 23 Feb 2024 17:23:26 +0100 Subject: [PATCH 04/22] Copy pasta, yum --- build-llvm.cmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build-llvm.cmd b/build-llvm.cmd index d58bd64..2c3a5ff 100644 --- a/build-llvm.cmd +++ b/build-llvm.cmd @@ -32,7 +32,7 @@ cmake -G "Visual Studio 17 2022" -A x64 ^ -DCMAKE_EXE_LINKER_FLAGS_INIT="/PROFILE /DYNAMICBASE /CETCOMPAT /guard:cf" ^ -DCMAKE_BUILD_TYPE=Release ^ -DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded" ^ - %XA_TOOLS_BUILD_DIR% + %XA_TOOLS_SOURCE_DIR% msbuild /p:Configuration=Release cd %MY_DIR% From 3bb1fe9a73248c248c3975f05bb411fbd40663ff Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Fri, 23 Feb 2024 17:26:01 +0100 Subject: [PATCH 05/22] Make cmake happy --- build-llvm.cmd | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build-llvm.cmd b/build-llvm.cmd index 2c3a5ff..c8e4528 100644 --- a/build-llvm.cmd +++ b/build-llvm.cmd @@ -23,7 +23,7 @@ mkdir %HOST_BUILD_DIR% if exist %HOST_ARTIFACTS_DIR% (rmdir /S /Q %HOST_ARTIFACTS_DIR%) mkdir %HOST_ARTIFACTS_DIR%\bin -if exist %XA_TOOLS_BUILD_DIR% (rmdir /S / %XA_TOOLS_BUILD_DIR%) +if exist %XA_TOOLS_BUILD_DIR% (rmdir /S /Q %XA_TOOLS_BUILD_DIR%) mkdir %XA_TOOLS_BUILD_DIR% cd %XA_TOOLS_BUILD_DIR% @@ -32,6 +32,7 @@ cmake -G "Visual Studio 17 2022" -A x64 ^ -DCMAKE_EXE_LINKER_FLAGS_INIT="/PROFILE /DYNAMICBASE /CETCOMPAT /guard:cf" ^ -DCMAKE_BUILD_TYPE=Release ^ -DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded" ^ + -DBINUTILS_VERSION="2.38" ^ %XA_TOOLS_SOURCE_DIR% msbuild /p:Configuration=Release From 7400489250d12d3085485add7d3c947356c1f453 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Fri, 23 Feb 2024 17:31:35 +0100 Subject: [PATCH 06/22] Msbuild needs a project... lesseeee --- build-llvm.cmd | 2 ++ 1 file changed, 2 insertions(+) diff --git a/build-llvm.cmd b/build-llvm.cmd index c8e4528..54c2d85 100644 --- a/build-llvm.cmd +++ b/build-llvm.cmd @@ -35,6 +35,8 @@ cmake -G "Visual Studio 17 2022" -A x64 ^ -DBINUTILS_VERSION="2.38" ^ %XA_TOOLS_SOURCE_DIR% +dir + msbuild /p:Configuration=Release cd %MY_DIR% From a3dd2c766bd9bc20c9cb45fc0426db8b36e59e20 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Fri, 23 Feb 2024 17:36:49 +0100 Subject: [PATCH 07/22] Solution? --- build-llvm.cmd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build-llvm.cmd b/build-llvm.cmd index 54c2d85..ee0f4bb 100644 --- a/build-llvm.cmd +++ b/build-llvm.cmd @@ -37,7 +37,7 @@ cmake -G "Visual Studio 17 2022" -A x64 ^ dir -msbuild /p:Configuration=Release +msbuild /p:Configuration=Release xa-utilities.sln cd %MY_DIR% cd %HOST_BUILD_DIR% From eb7d9bdbb906d0bee86b312cac6f9df8b7e9696a Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Tue, 27 Feb 2024 16:01:23 +0100 Subject: [PATCH 08/22] Added the cxxopts submodule --- .gitmodules | 4 ++++ external/cxxopts | 1 + 2 files changed, 5 insertions(+) create mode 160000 external/cxxopts diff --git a/.gitmodules b/.gitmodules index 6cd0cd5..105eb84 100644 --- a/.gitmodules +++ b/.gitmodules @@ -3,3 +3,7 @@ url = https://github.com/llvm/llvm-project.git shallow = true branch = release/13.x +[submodule "cxxopts"] + path = external/cxxopts + url = https://github.com/jarro2783/cxxopts.git + branch = master diff --git a/external/cxxopts b/external/cxxopts new file mode 160000 index 0000000..4bf61f0 --- /dev/null +++ b/external/cxxopts @@ -0,0 +1 @@ +Subproject commit 4bf61f08697b110d9e3991864650a405b3dd515d From 5f5d8712459e590afafb7d6e3ce37005ee7a5ee2 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Tue, 27 Feb 2024 16:02:16 +0100 Subject: [PATCH 09/22] Don't use tip of master --- external/cxxopts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/external/cxxopts b/external/cxxopts index 4bf61f0..3bf2684 160000 --- a/external/cxxopts +++ b/external/cxxopts @@ -1 +1 @@ -Subproject commit 4bf61f08697b110d9e3991864650a405b3dd515d +Subproject commit 3bf268481da8208d171d8908e6491459de3651d7 From deda6574817a990f40dbe56d0f81f2395fac6060 Mon Sep 17 00:00:00 2001 From: Marek Habersack Date: Tue, 27 Feb 2024 18:47:52 +0100 Subject: [PATCH 10/22] Let's see if it builds on CI It builds locally. Links dynamically atm, to be fixed. --- build-llvm.cmd | 1 - src/CMakeLists.txt | 116 ++++++++++++------- src/gas/CMakeLists.txt | 15 +-- src/gas/gas.cc | 225 +++++++++++++++---------------------- src/gas/gas.hh | 16 +-- src/gas/llvm_mc_runner.cc | 1 - src/gas/process.cc | 10 +- src/gas/process.posix.cc | 6 +- src/gas/process.windows.cc | 2 +- 9 files changed, 190 insertions(+), 202 deletions(-) diff --git a/build-llvm.cmd b/build-llvm.cmd index ee0f4bb..5694e25 100644 --- a/build-llvm.cmd +++ b/build-llvm.cmd @@ -29,7 +29,6 @@ mkdir %XA_TOOLS_BUILD_DIR% cd %XA_TOOLS_BUILD_DIR% cmake -G "Visual Studio 17 2022" -A x64 ^ - -DCMAKE_EXE_LINKER_FLAGS_INIT="/PROFILE /DYNAMICBASE /CETCOMPAT /guard:cf" ^ -DCMAKE_BUILD_TYPE=Release ^ -DCMAKE_MSVC_RUNTIME_LIBRARY="MultiThreaded" ^ -DBINUTILS_VERSION="2.38" ^ diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0a397da..6f83326 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -85,58 +85,88 @@ macro(c_linker_has_flag _flag) _linker_has_flag(C ${_flag}) endmacro() -set(COMMON_COMPILER_ARGS - -fstack-protector-strong - -fstrict-return - -fno-strict-aliasing - -ffunction-sections - -funswitch-loops - -finline-limit=500 - -Wa,-noexecstack - -fpic - -g - -Wa,--noexecstack - -Wformat - -Werror=format-security - -Wall - -Wextra - -Wnull-dereference - -Wmisleading-indentation - -Wunused - -Wpedantic - -Wsuggest-final-types - -Wsuggest-final-methods - -Wint-to-pointer-cast - -Wunguarded-availability - -Wsuggest-override +if(UNIX) + set(COMMON_COMPILER_ARGS + -fstack-protector-strong + -fstrict-return + -fno-strict-aliasing + -ffunction-sections + -funswitch-loops + -finline-limit=500 + -Wa,-noexecstack + -fpic + -g + -Wa,--noexecstack + -Wformat + -Werror=format-security + -Wall + -Wextra + -Wnull-dereference + -Wmisleading-indentation + -Wunused + -Wpedantic + -Wsuggest-final-types + -Wsuggest-final-methods + -Wint-to-pointer-cast + -Wunguarded-availability + -Wsuggest-override ) -set(COMMON_LINKER_ARGS - -fstack-protector-strong - -static-libstdc++ - -static-libgcc - LINKER:-fstrict-return - LINKER:-z,now - LINKER:-z,relro - LINKER:-z,noexecstack + set(COMMON_LINKER_ARGS + -fstack-protector-strong + -static-libstdc++ + -static-libgcc + LINKER:-fstrict-return + LINKER:-z,now + LINKER:-z,relro + LINKER:-z,noexecstack ) -if(COMPILER_DIAG_COLOR) - list(APPEND COMMON_COMPILER_ARGS -fdiagnostics-color=always) -else() - list(APPEND COMMON_COMPILER_ARGS -fdiagnostics-color=auto) -endif() + if(COMPILER_DIAG_COLOR) + list(APPEND COMMON_COMPILER_ARGS -fdiagnostics-color=always) + else() + list(APPEND COMMON_COMPILER_ARGS -fdiagnostics-color=auto) + endif() + + if(IS_DEBUG) + list(APPEND COMMON_COMPILER_ARGS + -ggdb3 + -fno-omit-frame-pointer + -O0 + ) + else() + list(APPEND COMMON_COMPILER_ARGS -fomit-frame-pointer -O2) + endif() +else() # UNIX + set(COMMON_COMPILER_ARGS + /Qspectre + /sdl + /guard:cf + /Zi + ) + + set(COMMON_LINKER_ARGS + LINKER:/PROFILE + LINKER:/DYNAMICBASE + LINKER:/CETCOMPAT + LINKER:/guard:cf + ) + + if(IS_DEBUG) + list(APPEND COMMON_COMPILER_ARGS + /Od + ) + else() + list(APPEND COMMON_COMPILER_ARGS + /Ot + ) + endif() +endif() # WIN32 if(IS_DEBUG) add_compile_definitions(DEBUG) - list(APPEND COMMON_COMPILER_ARGS - -ggdb3 - -fno-omit-frame-pointer - -O0 - ) else() add_compile_definitions(NDEBUG) - list(APPEND COMMON_COMPILER_ARGS -fomit-frame-pointer -O2) endif() foreach(flag ${COMMON_COMPILER_ARGS}) diff --git a/src/gas/CMakeLists.txt b/src/gas/CMakeLists.txt index edb75c9..ff04b09 100644 --- a/src/gas/CMakeLists.txt +++ b/src/gas/CMakeLists.txt @@ -33,21 +33,14 @@ add_executable( ${GAS_DRIVER_SOURCES} ) -if(WIN32) - target_link_options( - as - PRIVATE - -static-libstdc++ - -static-libgcc - ) +# SYSTEM disables warnings produced by cxxopts headers, which we can do nothing about +target_include_directories(as SYSTEM PRIVATE ${CMAKE_SOURCE_DIR}/../external/cxxopts/include) +if(WIN32) target_link_libraries( as shlwapi - -static - winpthread - -dynamic - ) + ) else() set(TARGET_DIR "${CMAKE_BINARY_DIR}/bin") set(AS_NAME as) diff --git a/src/gas/gas.cc b/src/gas/gas.cc index ccbf345..e9e5d8a 100644 --- a/src/gas/gas.cc +++ b/src/gas/gas.cc @@ -1,9 +1,9 @@ // SPDX-License-Identifier: MIT -#include - #include -#include #include +#include + +#include #include "constants.hh" #include "gas.hh" @@ -11,12 +11,63 @@ using namespace xamarin::android::gas; +cxxopts::Options Gas::create_options () +{ + cxxopts::Options options (program_name(), PROGRAM_DESCRIPTION.data ()); + options + .positional_help ("[asmfile...]") + .set_width (80); + + options.add_options ("Supported GAS arguments") + ("o", "name the object-file output `arg` (default a.out)", cxxopts::value()) + ("warn", "don't suppress warnings", cxxopts::value()) + ("g,gen-debug", "generate debugging information", cxxopts::value()) + ("asmfile", "assembler source file(s)", cxxopts::value>()); + + options.add_options ("Wrapper options, not passed to `llvm-mc`") + ("h,help", "show this help screen", cxxopts::value()) + ("V", "show version", cxxopts::value()) + ("version", "show version and exit", cxxopts::value()); + + options.add_options ("Ignored by GAS and this wrapper") + ("divide", "ignored", cxxopts::value()) + ("k", "ignored", cxxopts::value()) + ("nocpp", "ignored", cxxopts::value()) + ("Qn", "ignored", cxxopts::value()) + ("Qy", "ignored", cxxopts::value()) + ("s", "ignored", cxxopts::value()) + ("w", "ignored", cxxopts::value()) + ("X", "ignored", cxxopts::value()); + + switch (target_arch ()) { + case TargetArchitecture::ARM32: + options.add_options ("Arm options") + ("mfpu", "assemble for FPU architecture ", cxxopts::value()); + break; + + case TargetArchitecture::X86: + case TargetArchitecture::X64: + options.add_options ("x86/x64 options") + ("32", "generate 32bit object", cxxopts::value()) + ("64", "generate 64bit object", cxxopts::value()); + break; + + default: + break; + } + + return options; +} + int Gas::usage (bool is_error, std::string const message) { if (!message.empty ()) { std::cerr << message << Constants::newline << Constants::newline; } + cxxopts::Options options = create_options (); + + std::cerr << options.help () << std::endl << std::endl; std::cerr << "`" << program_name () << "` takes a subset of arguments accepted by the GNU Assembler (gas) program." << Constants::newline << "Accepted options are limited to the ones used by Xamarin.Android and Mono/dotnet AOT, more can be added as-needed." << Constants::newline << "Some options are accepted but ignored, either because they are ignored by GAS as well or because they are used by" << Constants::newline @@ -25,31 +76,7 @@ int Gas::usage (bool is_error, std::string const message) << "Since `llvm-mc` does not support compiling multiple source files at the same time, this GAS behavior is emulated" << Constants::newline << "by running `llvm-mc` once per each input file and using the `ld` linker in the end to merge all the discrete output" << Constants::newline << "files into the single file indicated by the `-o` option." << Constants::newline << Constants::newline - << "Command line options are compatibile with GAS version " << BINUTILS_VERSION << Constants::newline << Constants::newline - << "Currently supported options are:" << Constants::newline << Constants::newline - << "All targets" << Constants::newline - << " -o FILE path to the output object file" << Constants::newline - << " --warn don't suppress warning messages" << Constants::newline - << " -g | --gen-debug generate debug information in the output object file" << Constants::newline << Constants::newline - << "x86/x86_64 targets" << Constants::newline - << " --32 output a 32-bit object [ignored, `llvm-mc` is always invoked for the right target]" << Constants::newline - << " --64 output a 64-bit object [ignored, as above]" << Constants::newline << Constants::newline - << "armeabi/arm32 targets" << Constants::newline - << " -mfpu=FPU select floating-point architecture for the target" << Constants::newline << Constants::newline - << "Ignored by GAS and this wrapper" << Constants::newline - << " --divide" << Constants::newline - << " -k" << Constants::newline - << " --nocpp" << Constants::newline - << " -Qn" << Constants::newline - << " -Qy" << Constants::newline - << " -s" << Constants::newline - << " -w" << Constants::newline - << " -X" << Constants::newline << Constants::newline - << "Wrapper options, not passed to `llvm-mc`" << Constants::newline - << " -h | --help show this help screen" << Constants::newline - << " -V show version" << Constants::newline - << " --version show version and exit " << Constants::newline - << Constants::newline; + << "Command line options are compatibile with GAS version " << BINUTILS_VERSION << Constants::newline << Constants::newline; return is_error ? 1 : 0; } @@ -171,126 +198,47 @@ int Gas::run (int argc, char **argv) return 0; } -std::vector