Tuesday, December 27, 2016

How much is the elf64 in the window?

I thought this is interesting: https://channel9.msdn.com/Blogs/Seth-Juarez/Windows-Subsystem-for-Linux-Windows-and-Ubuntu-Interoperability In windows 10 anniversary edition Microsoft made it easier to run native ELF64 on operating system. ELF64 has been around since 1998 and is used in some firmware. So through using pico processes, a linux kernel level technology much like POSIX. I wonder if this would finally utalize the full power of multicore systems. Accordimg to the book How Computers work there must be a master thread that utalizes a single master core. It used to be true that not many applications were designed to use more than one thread or core. This new design paradigm provides emulation for the applications making it appear as though there is only one core while there might be 8 like the AMD fx8350. 
The Windows Subsystem for Linux was announced as a beta feature at //Build 2016. Since then, we have explored an architectural overview, how Pico Processes work ...

The following details setting up for and building Clang on Windows using Visual Studio:
Get the required tools:Subversion. Source code control program. Get it from: http://subversion.apache.org/packages.htmlCMake. This is used for generating Visual Studio solution and project files. Get it from:http://www.cmake.org/cmake/resources/software.htmlVisual Studio 2013 or laterPython. This is needed only if you will be running the tests (which is essential, if you will be developing for clang). Get it from:http://www.python.org/download/GnuWin32 tools These are also necessary for running the tests. (Note that the grep from MSYS or Cygwin doesn't work with the tests because of embedded double-quotes in the search strings. The GNU grep does work in this case.) Get them from http://getgnuwin32.sourceforge.net/.Check out LLVM:svn co http://llvm.org/svn/llvm-project/llvm/trunk llvmCheck out Clang:cd llvm\toolssvn cohttp://llvm.org/svn/llvm-project/cfe/trunk clang
The official home of the Python Programming Language

GetGnuWin32 – Maintaining a Gnuwin32 Package archive. You can download the gnuwin32 package maintenance utility on the sourceforge.net project portal.

The Apache Subversion project does not officially endorse or maintain any binary packages of the Subversion software. However, volunteers have created binary ...

Note: Some Clang tests are sensitive to the line endings. Ensure that checking out the files does not convert LF line endings to CR+LF. If you use git-svn, make sure your core.autocrlf setting is false.
Run CMake to generate the Visual Studio solution and project files:cd ..\.. (back to where you started)mkdir build (for building without polluting the source dir)cd buildIf you are using Visual Studio 2013: cmake -G "Visual Studio 12" ..\llvmSee the LLVM CMake guide for more information on other configuration options for CMake.The above, if successful, will have created an LLVM.sln file in the build directory.Build Clang:Open LLVM.sln in Visual Studio.Build the "clang" project for just the compiler driver and front end, or the "ALL_BUILD" project to build everything, including tools.Try it out (assuming you added llvm/debug/bin to your path). (See the running examples from above.)See Hacking on clang - Testing using Visual Studio on Windows for information on running regression tests on Windows.
Note that once you have checked out both llvm and clang, to synchronize to the latest code base, use the svn update command in both the llvm and llvm\tools\clang directories, as they are separate repositories
Pull down the cmonster wrapper for python using pip. https://github.com/axw/cmonster/blob/master/README.md
Use CMake to generate up-to-date project files:
Once CMake is installed then the simplest way is to just start the CMake GUI, select the directory where you have LLVM extracted to, and the default options should all be fine. One option you may really want to change, regardless of anything else, might be theCMAKE_INSTALL_PREFIX setting to select a directory to INSTALL to once compiling is complete, although installation is not mandatory for using LLVM. Another important option is LLVM_TARGETS_TO_BUILD, which controls the LLVM target architectures that are included on the build.If CMake complains that it cannot find the compiler, make sure that you have the Visual Studio C++ Tools installed, not just Visual Studio itself (trying to create a C++ project in Visual Studio will generally download the C++ tools if they haven’t already been).See the LLVM CMake guide for detailed information about how to configure the LLVM build.CMake generates project files for all build types. To select a specific build type, use the Configuration manager from the VS IDE or the/property:Configuration command line option when using MSBuild.
Start Visual Studio
In the directory you created the project files will have an llvm.sln file, just double-click on that to open Visual Studio.
Build the LLVM Suite:
The projects may still be built individually, but to build them all do not just select all of them in batch build (as some are meant as configuration projects), but rather select and build just the ALL_BUILD project to build everything, or the INSTALL project, which first builds the ALL_BUILD project, then installs the LLVM headers, libs, and other useful things to the directory set by theCMAKE_INSTALL_PREFIX setting when you first configured CMake.The Fibonacci project is a sample program that uses the JIT. Modify the project’s debugging properties to provide a numeric command line argument or run it from the command line. The program will print the corresponding fibonacci value.
Test LLVM in Visual Studio:
If %PATH% does not contain GnuWin32, you may specify LLVM_LIT_TOOLS_DIR on CMake for the path to GnuWin32.You can run LLVM tests by merely building the project “check”. The test results will be shown in the VS output window.
Test LLVM on the command line:
The LLVM tests can be run by changing directory to the llvm source directory and running:
C:\..\llvm> python ..\build\bin\llvm-lit --param build_config=Win32 --param build_mode=Debug --param llvm_site_config=../build/test/lit.site.cfg test
This example assumes that Python is in your PATH variable, you have built a Win32 Debug version of llvm with a standard out of line build. You should not see any unexpected failures, but will see many unsupported tests and expected failures.
A specific test or test directory can be run with:
C:\..\llvm> python ..\build\bin\llvm-lit --param build_config=Win32 --param build_mode=Debug --param llvm_site_config=../build/test/lit.site.cfg test/path/to/test
This portion of the document covers invoking Clang and LLVM with the options required so the sanitizers analyze Python with under its test suite. Two checkers are used - ASan and UBSan.
Because the sanitizers are runtime checkers, its best to have as many positive and negative self tests as possible. You can never have enough self tests.
The general idea is to compile and link with the sanitizer flags. At link time, Clang will include the needed runtime libraries. However, you can’t use CFLAGS and CXXFLAGS to pass the options through the compiler to the linker because the makefile rules for BUILDPYTHON, _testembed and _freeze_importlib don’t use the implicit variables.
As a workaround to the absence of flags to the linker, you can pass the sanitizer options by way of the compilers - CC and CXX. Passing the flags though the compiler is used below, but passing them through LDFLAGS is also reported to work.
26.4.1. Building Python
To begin, export the variables of interest with the desired sanitizers. Its OK to specify both sanitizers:
# ASan export CC="/usr/local/bin/clang -fsanitize=address" export CXX="/usr/local/bin/clang++ -fsanitize=address -fno-sanitize=vptr"
Or:
# UBSan export CC="/usr/local/bin/clang -fsanitize=undefined" export CXX="/usr/local/bin/clang++ -fsanitize=undefined -fno-sanitize=vptr"
The -fno-sanitize=vptr removes vtable checks that are part of UBSan from C++ projects due to noise. Its not needed with Python, but you will likely need it for other C++ projects.
After exporting CC and CXX, configure as normal:
$ ./configure checking build system type... x86_64-unknown-linux-gnu checking host system type... x86_64-unknown-linux-gnu checking for --enable-universalsdk... no checking for --with-universal-archs... 32-bit checking MACHDEP... linux checking for --without-gcc... no checking for gcc... /usr/local/bin/clang -fsanitize=undefined checking whether the C compiler works... yes ...
Next is a standard make (formatting added for clarity):
$ make /usr/local/bin/clang -fsanitize=undefined -c -Wno-unused-result -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE -o Modules/python.o ./Modules/python.c /usr/local/bin/clang -fsanitize=undefined -c -Wno-unused-result -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I. -IInclude -I./Include -DPy_BUILD_CORE -o Parser/acceler.o Parser/acceler.c ...
Finally is make test (formatting added for clarity):
Objects/longobject.c:39:42: runtime error: index -1 out of bounds for type 'PyLongObject [262]' Objects/tupleobject.c:188:13: runtime error: member access within misaligned address 0x2b76be018078 for type 'PyGC_Head' (aka 'union _gc_head'), which requires 16 byte alignment 0x2b76be018078: note: pointer points here 00 00 00 00 40 53 5a b6 76 2b 00 00 60 52 5a b6 ... ^ ...
If you are using the address sanitizer, its important to pipe the output throughasan_symbolize.py to get a good trace. For example, from Issue 20953 during compile (formatting added for clarity):
$ make test 2>&1 | asan_symbolize.py ... /usr/local/bin/clang -fsanitize=address -Xlinker -export-dynamic -o python Modules/python.o libpython3.3m.a -ldl -lutil /usr/local/ssl/lib/libssl.a /usr/local/ssl/lib/libcrypto.a -lm ./python -E -S -m sysconfig --generate-posix-vars ================================================================= ==24064==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x619000004020 at pc 0x4ed4b2 bp 0x7fff80fff010 sp 0x7fff80fff008 READ of size 4 at 0x619000004020 thread T0 #0 0x4ed4b1 in PyObject_Free Python-3.3.5/./Objects/obmalloc.c:987 #1 0x7a2141 in code_dealloc Python-3.3.5/./Objects/codeobject.c:359 #2 0x620c00 in PyImport_ImportFrozenModuleObject Python-3.3.5/./Python/import.c:1098 #3 0x620d5c in PyImport_ImportFrozenModule Python-3.3.5/./Python/import.c:1114 #4 0x63fd07 in import_init Python-3.3.5/./Python/pythonrun.c:206 #5 0x63f636 in _Py_InitializeEx_Private Python-3.3.5/./Python/pythonrun.c:369
First, create a simple C file, name it ‘hello.c‘:
#include <stdio.h> int main() { printf("hello world\n"); return 0; }
Next, compile the C file into an LLVM bitcode file:
C:\..> clang -c hello.c -emit-llvm -o hello.bc
This will create the result file hello.bc which is the LLVM bitcode that corresponds the compiled program and the library facilities that it required. You can execute this file directly using lli tool, compile it to native assembly with the llc, optimize or analyze it further with the opt tool, etc.
Alternatively you can directly output an executable with clang with:
C:\..> clang hello
The elf file is then created by default.
For information about generating a .elf filetype

No comments:

Post a Comment