
Preface
This post is a continuation of the previous post on compiling a native win32 console application for Windows Phone 8.1 platform that runs over telnet connection provided by CMD Injector.
Next up we will find a way to compile a K&R style classic “Hello World” program for the platform using standard C library instead of trying to avoid using it as we did in the previous experiment.
We have a good base set of development kits to explore with and see what works and what does not.
The Adventure Continued
CRT Code & Linker
To begin moving to a more standard Hello World, let’s replace our previous C code with the following set:
#define WINAPI_FAMILY WINAPI_FAMILY_DESKTOP_APP
#define _ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE 1
#include <stdio.h>
int main(void) {
printf("Hello, Windows Phone!\n");
return 0;
}
For the standard C Runtime we have the static libcmt.lib available in the ARM WDK for Windows 8.1. It makes sense to try and use it because it should bundle up
everything that we need going forward. The challenge will be that the particular libcmt.lib has been built for Windows RT/desktop ARM and may call some
functions that do not exist on Windows Phone 8.1 platform.
The compilation command for the hello.c remains the same as before:
cl /nologo /W3 /O1 /c /MT /D_WIN32_WINNT=0x0603 /D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE=1 /I"C:\Program Files (x86)\Windows Kits\8.1\Include\um" /I"C:\Program Files (x86)\Windows Kits\8.1\Include\shared" /I"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include" hello.c /Fohello.obj
Let’s add the library to our linker call:
link /NOLOGO /MACHINE:ARM /SUBSYSTEM:CONSOLE /ENTRY:mainCRTStartup /NODEFAULTLIB /LIBPATH:"C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\arm" /LIBPATH:"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\lib\arm" hello.obj mincore.lib libcmt.lib /OUT:hello.exe
The key changes to the old here are:
- Addition of libcmt.lib and its lib path
- Changed /ENTRY:main to /ENTRY:mainCRTStartup so the CRT initializes properly before calling the main function
First Attempt & Stubs
When linking the more standard C code against libcmt.lib I got the following linker error:
libcmt.lib(winapisupp.obj) : error LNK2019: unresolved external symbol __imp_GetCurrentPackageId referenced in function __IsPackagedAppHelper
libcmt.lib(mlock.obj) : error LNK2019: unresolved external symbol __imp_FatalAppExitA referenced in function _lockerr_exit
hello.exe : fatal error LNK1120: 2 unresolved externals
This indicates that we are thus far only missing two symbols! At a quick glance, I could not find a library from the currently installed set of development kits that would contain these, so stubs needed to be written. Luckily these are simple functions to implement as stubs. Here’s how I implemented them:
stubs.c
#define WINAPI_FAMILY WINAPI_FAMILY_DESKTOP_APP
#define _ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE 1
#include <windows.h>
// Stub - WP8.1 has no package identity for native executables
LONG WINAPI GetCurrentPackageId(UINT32 *bufferLength, BYTE *buffer) {
return APPMODEL_ERROR_NO_PACKAGE;
}
// Stub - redirect to ExitProcess
VOID WINAPI FatalAppExitA(UINT uAction, LPCSTR lpMessageText) {
ExitProcess(1);
}
If we cannot find libraries that contain what we need, we will need to maintain a set of stubs. If and when I get deeper into this, I may build a git repository with all the common stubs in place, but for now this blog post will do as we only needed two.
Here’s how to compile the stubs.c file into an object file for linking:
cl /nologo /W3 /O1 /c /MT /D_WIN32_WINNT=0x0603 /D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE=1 /I"C:\Program Files (x86)\Windows Kits\8.1\Include\um" /I"C:\Program Files (x86)\Windows Kits\8.1\Include\shared" /I"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include" stubs.c /Fostubs.obj
And then we link the generated object file together with our Hello World code:
link /NOLOGO /MACHINE:ARM /SUBSYSTEM:CONSOLE /ENTRY:mainCRTStartup /NODEFAULTLIB /LIBPATH:"C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\arm" /LIBPATH:"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\lib\arm" hello.obj stubs.obj mincore.lib libcmt.lib /OUT:hello.exe
The compilation of stubs.c yielded the following warnings:
stubs.c(11) : warning C4273: 'FatalAppExitA' : inconsistent dll linkage
C:\Program Files (x86)\Windows Kits\8.1\Include\um\winbase.h(3454) : see previous definition of 'FatalAppExitA' and following when linking: libcmt.lib(winapisupp.obj) : warning LNK4217: locally defined symbol GetCurrentPackageId imported in function __IsPackagedAppHelper
libcmt.lib(mlock.obj) : warning LNK4217: locally defined symbol FatalAppExitA imported in function _lockerr_exit
These were harmless.
LNK4217meant that the linker found our local stub definitions and used them instead of imported ones, which is exactly what we wantedC4273meant our stub declaration conflicted with the header’s __declspec(dllimport) decoration,, which is harmless in this case
To fix these warnings, I updated the stubs.c file as follows:
#define WINAPI_FAMILY WINAPI_FAMILY_DESKTOP_APP
#define _ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE 1
// Override dllimport before including headers
#define _KERNEL32_
#define _APPMODEL_
#include <windows.h>
LONG WINAPI GetCurrentPackageId(UINT32 *bufferLength, BYTE *buffer) {
return APPMODEL_ERROR_NO_PACKAGE;
}
VOID WINAPI FatalAppExitA(UINT uAction, LPCSTR lpMessageText) {
ExitProcess(1);
}
With all of these in place, the linker produced a working executable that ran on the phone.
Summary
We now have a working standard C library on Windows Phone 8.1. This means CRT is available for porting more software that runs on the system level and not in the regular app sandbox. This is just the first step on a longer journey. It is expected that more stubs and workarounds may be needed as more software is compiled for the platform. Ideally anything missing will be found in one of the lib files that come with the SDKs and WDKs.
Build System
Build any .c file:
cl /nologo /W3 /O1 /c /MT /D_WIN32_WINNT=0x0603 /D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE=1 /I"C:\Program Files (x86)\Windows Kits\8.1\Include\um" /I"C:\Program Files (x86)\Windows Kits\8.1\Include\shared" /I"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include" yourfile.c /FoyourFile.obj
Link:
link /NOLOGO /MACHINE:ARM /SUBSYSTEM:CONSOLE /ENTRY:mainCRTStartup /NODEFAULTLIB /LIBPATH:"C:\Program Files (x86)\Windows Kits\8.1\Lib\winv6.3\um\arm" /LIBPATH:"C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\lib\arm" yourfile.obj stubs.obj mincore.lib libcmt.lib /OUT:yourfile.exe
stubs.obj always needs to be linked to satisfy any missing CRT dependencies.
Final Thoughts
This is the foundation that enables further exploration and developments. My plans next would be to explore if we could use LLVM and binutils instead of Visual Studio for the compiler and linker. Having a working open source toolchain would help in porting more open source software for the platform as those tend to expect something else than a Microsoft compiler/linker.