I am currently attempting to cross-compile my C source code into a PE32+ executable, which hasn't yielded the greatest results.
I'm running macOS Ventura Beta 8 (macOS 13), using Clang which is bundled with LLVM (which was installed via the Homebrew tap).
Here is the command for compiling the source files:
/usr/local/opt/llvm/bin/clang \
--target=x86_64-unknown-windows-gnu \
-Wl,-e,_KernMain \
-o kernel.o \
src/Kernel/Kernel.c \
src/Kernel/Memory/KernMem.c \
src/Kernel/Graphics/KernGraphics.c \
-I/Users/kernel/Documents/edk2-master/edk2/MdePkg/Include/ \
-I/Users/kernel/Documents/edk2-master/edk2/MdePkg/Include/X64 \
-I/Users/kernel/Documents/edk2-master/edk2/KernelOSPkg/src/Common \
-I/Users/kernel/Downloads/mingw64/x86_64-w64-mingw32/include \
-L/Users/kernel/Documents/edk2-master/edk2/MdePkg/Library/ \
-L/Users/kernel/Downloads/mingw64/x86_64-w64-mingw32/lib \
-L/Users/kernel/Downloads/mingw64/x86_64-w64-mingw32/lib/ldscripts \
-L/usr/local/lib \
-fuse-ld="/usr/local/opt/llvm/bin/ld.lld"
It throws the following error:
lld: error: unable to find library -lgcc
lld: error: unable to find library -lgcc_eh
lld: error: unable to find library -lgcc
lld: error: unable to find library -lgcc_eh
clang-15: error: linker command failed with exit code 1 (use -v to see invocation)
CLANG info:
/usr/local/opt/llvm/bin/clang --version
Homebrew clang version 15.0.2
Target: x86_64-apple-darwin22.1.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin
ld.lld info:
/usr/local/opt/llvm/bin/ld.lld --version
Homebrew LLD 15.0.2 (compatible with GNU linkers)
The issue was that I didn't include the /path/to/mingw64/lib/gcc/x86_64-w64-mingw32/<version> directory, which included the libgcc.a and libgcc_eh.a files.
Though, I still do not know how to resolve the issue with an undefined symbol: WinMain. I will update this answer if I find a solution.
Error in question:
ld.lld: error: undefined symbol: WinMain
>>> referenced by libmingw32.a(lib64_libmingw32_a-crt0_c.o):(.text.startup)
clang-15: error: linker command failed with exit code 1 (use -v to see invocation)
Compilation command (does not compile successfully due to undefined symbol):
/usr/local/opt/llvm/bin/clang \
-target x86_64-unknown-windows-gnu \
-Wl,-e,KernMain \
-fuse-ld=lld \
-ffreestanding \
-o kernel.o \
src/Kernel/Kernel.c \
src/Kernel/Memory/KernMem.c \
src/Kernel/Graphics/KernGraphics.c \
-I/Users/kernel/Documents/edk2-master/edk2/MdePkg/Include/ \
-I/Users/kernel/Documents/edk2-master/edk2/MdePkg/Include/X64 \
-I/Users/kernel/Documents/edk2-master/edk2/KernelOSPkg/src/Common \
-I/Users/kernel/Downloads/mingw64/x86_64-w64-mingw32/include \
-L/Users/kernel/Downloads/mingw64/lib/gcc/x86_64-w64-mingw32/12.2.0 \
-L/Users/kernel/Documents/edk2-master/edk2/MdePkg/Library/ \
-L/Users/kernel/Downloads/mingw64/x86_64-w64-mingw32/lib \
-fshort-wchar
EDIT: To fix the aforementioned error with the undefined symbol, simply supply -nostdlib to the compiler.
Related
I'm trying to cross compile the newlib for ARM under the mingw-w64 environment.
Toolchain was downloaded from here: GNU Arm Embedded Toochain
Newlib cloned from its GIT repository
I'm running my configuration script from mingw64 shell:
#!/bin/sh
export PATH=$PATH:/d/Toolchain/gcc-arm-none-eabi-10.3-2021.10/bin
../newlib-cygwin/configure CROSS_COMPILE=arm-none-eabi- ARCH=arm CFLAGS="-specs=nosys.specs" \
--target=arm-none-eabi \
--prefix="/d/work/newlib-install/" \
--disable-newlib-supplied-syscalls \
--enable-newlib-reent-check-verify \
--enable-newlib-reent-small \
--enable-newlib-retargetable-locking \
--disable-newlib-fvwrite-in-streamio \
--disable-newlib-fseek-optimization \
--disable-newlib-wide-orient \
--enable-newlib-nano-malloc \
--disable-newlib-unbuf-stream-opt \
--enable-lite-exit \
--enable-newlib-global-atexit \
--enable-newlib-nano-formatted-io \
--disable-nls
And i get an error during configure:
configure: error: in `/d/work/newlib-build'
configure: error: C compiler cannot create executables
See `config.log' for more details.
And in config log:
Target: x86_64-w64-mingw32
Thread model: posix
gcc version 9.2.0 (Rev2, Built by MSYS2 project)
configure:4393: $? = 0
configure:4382: gcc -V >&5
gcc.exe: error: unrecognized command line option '-V'
gcc.exe: fatal error: no input files
compilation terminated.
configure:4393: $? = 1
configure:4382: gcc -qversion >&5
gcc.exe: error: unrecognized command line option '-qversion'; did you mean '--version'?
gcc.exe: fatal error: no input files
compilation terminated.
configure:4393: $? = 1
configure:4413: checking for C compiler default output file name
configure:4435: gcc -specs=nosys.specs conftest.c >&5
gcc.exe: fatal error: cannot read spec file 'nosys.specs': No such file or directory
compilation terminated.
It means that configure chooses my mingw64 gcc as target compiler instead of arm-none-eabi-gcc.
Is it possible to compile that in mingw64?
I am currently working on building Glib version 2.45.8 on CentOS 7 running on x86-64 targeting a custom distro based off Linux from Scratch running on x86-64. There is a problem linking with libffi which is version 3.2.1.
path/to/build/directory/bin/ld: warning: libc.so.6, needed by //lib/../libffi.so, not found (try using -rpath or -rpath-link)
path/to/build/directory/lib64/libffi.so: undefined reference to `free#GLIBC_2.2.5'
path/to/build/directory/lib64/libffi.so: undefined reference to `mkostemp#GLIBC_2.7'
(etc ... there are about 15 undefined references total)
path/to/build/directory/lib64/libffi.so: undefined reference to `__getdelim#GLIBC_2.2.5'
path/to/build/directory/lib64/libffi.so: undefined reference to `getenv#GLIBC_2.2.5'
Using -rpath or -rpath-link will not work because the libc.so.6 file does not exist anywhere in the file system for my build.
However I do have libc.so and libc.so.0 in path/to/build/directory/lib64/ the directory in which libc.so.6 cannot be found.
Here are my ./configure and make commmands.
glib_cv_stack_grows=no \
glib_cv_uscore=no \
ac_cv_func_posix_getpwuid_r=yes \
ac_cv_func_posix_getgrgid_r=yes \
LIBFFI_CFLAGS=-lffi \
LIBFFI_LIBS=-lffi \
ZLIB_CFLAGS=-lz \
ZLIB_LIBS=-lz \
PKG_CONFIG_LIBDIR=$TARG/lib/pkgconfig \
./configure --prefix=/ --host=x86_64-linux --with-libiconv
make -j32 LDFLAGS=-liconv
How do I get the correct libc.so to be built?
There we several things that had to be done to solve this problem.
The first thing I found out was that if software has a dependancy on libc.so.6 it was built against glibc.
However our toolchain for this build is using uClibc instead, which does not produce libc.so.6 when it is built.
The solution was to write the LIBFFI and ZLIB clags and libs to link to the libffi and zlib built with uClibc.
glib_cv_stack_grows=no \
glib_cv_uscore=no \
ac_cv_func_posix_getpwuid_r=yes \
ac_cv_func_posix_getgrgid_r=yes \
ZLIB_CFLAGS=-I$TARG/include \
ZLIB_LIBS="-L$TARG/lib -lz" \
LIBFFI_CFLAGS=-I$TARG/include \
LIBFFI_LIBS="$TARG/lib/libffi.a" \
PKG_CONFIG_LIBDIR=$TARG/lib/pkgconfig \
./configure --prefix=/ --host=x86_64-linux --with-libiconv
make -j32 LDFLAGS=-liconv
I'm using QT and the ed25519-donna lib to validate my signature.
Linux I'm using this lib => libssl-dev
MacOs I'm using this lib => openssl
So when I try to compile by Linux its work but when I try in MacOs the donna libs give a error:
openssl/rand.h is not found
I've this line in my Makefile:
LIBS = $(SUBLIBS) -L/home/laion/Desktop/lethean-gui/lethean/lib -lwallet_merged -lepee -lunbound -leasylogging -lboost_serialization -lboost_thread -lboost_system -lboost_date_time -lboost_filesystem -lboost_regex -lboost_chrono -lboost_program_options -lssl -lcrypto -Wl,-Bdynamic -Wl,-Bdynamic -lunwind -ldl -lQt5Quick -lQt5Widgets -lQt5Gui -lQt5Qml -lQt5Network -lQt5Core -lGL -lpthread
When you look to donna libs you can see a ed25519.c file and inside of that call the ed25519-randombytes.h, inside of this file has the openssl/rand.h include.
Inside of my .pro file I use this:
macx {
# mixing static and shared libs are not supported on mac
# CONFIG(static) {
# message("using static libraries")
# LIBS+= -Wl,-Bstatic
# }
LIBS+= \
-L/usr/local/lib \
-L/usr/local/opt/openssl/lib \
-L/usr/local/opt/boost/lib \
-lboost_serialization \
-lboost_thread-mt \
-lboost_system \
-lboost_system-mt \
-lboost_date_time \
-lboost_filesystem \
-lboost_regex \
-lboost_chrono \
-lboost_chrono-mt \
-lboost_program_options \
-lssl \
-lcrypto \
-ldl
INCLUDEPATH += /usr/local/opt/boost/include/
}
So, if I add this path in INCLUDEPATH:
/usr/local/opt/openssl/include \
/usr/local/opt/openssl/lib
I get a different error:
The program has unexpectedly finished.
// every donna file get this message
was built for newer osx version (10.13) than being linked (10.12)
For now I get this reference C++, Mac OS X, Xcode 8 : Compile Boost : Set deployment target to OS X 10.11 and I come back to let you know if works
FIXED << The problem was the version of Mac. To work with donna and openssl is necessary 10.12+ to compile
I'm building an uClibc i486 cross toolchain for an embedded system. The toolchain is built on Debian Wheezy (x86-64) and consists of the following components:
Binutils 2.24
GCC 4.7.3
Linux 3.2.54
uClibc 0.9.33.2
GMP 5.1.3
MPFR 3.1.2
MPC 1.0.2
These steps are used to build the toolchain:
Install Linux 3.2.54 headers
make mrproper
make \
ARCH=x86 \
INSTALL_HDR_PATH=/home/build-user/toolchain/i486-linux-uclibc \
headers_install
This step is successfully completed.
Build Binutils 2.24
Binutils is built in a separate directory as specified in the documentation.
./configure \
--prefix=/home/build-user/toolchain/ \
--target=i486-linux-uclibc
--with-sysroot=/home/build-user/toolchain/i486-linux-uclibc \
--with-lib-path=/home/build-user/toolchain/i486-linux-uclibc/lib \
--disable-nls \
--disable-multilib
make configure-host
make
make install
This step is successfully completed.
Build GMP 5.1.3
./configure \
--prefix=/home/build-user/toolchain/gmp \
--disable-shared \
--enable-static
make
make install
This step is successfully completed.
Build MPFR 3.1.2
./configure \
--prefix=/home/build-user/toolchain/mpfr \
--with-gmp=/home/build-user/toolchain/gmp \
--disable-shared \
--enable-static
make
make install
This step is successfully completed.
Build MPC 1.0.2
./configure \
--prefix=/home/build-user/toolchain/mpc \
--with-gmp=/home/build-user/toolchain/gmp \
--with-mpfr=/home/build-user/toolchain/mpfr \
--disable-shared \
--enable-static
make
make install
This step is successfully completed.
Build static GCC 4.7.3
GCC is built in a separate directory as specified in the documentation.
./configure \
--prefix=/home/build-user/toolchain \
--target=i486-linux-uclibc \
--with-arch=i486 \
--with-sysroot=/home/build-user/toolchain/i486-linux-uclibc \
--disable-multiarch \
--disable-nls \
--disable-shared \
--without-headers \
--with-newlib \
--disable-decimal-float \
--disable-libgomp \
--disable-libmudflap \
--disable-libssp \
--disable-libquadmath \
--disable-threads \
--enable-languages=c \
--disable-multilib \
--with-gmp=/home/build-user/toolchain/gmp \
--with-mpfr=/home/build-user/toolchain/mpfr \
--with-mpc=/home/build-user/toolchain/mpc
make all-gcc all-target-libgcc
make install-gcc install-target-libgcc
This step is successfully completed.
Build uClibc 0.9.33.2
The configuration file for uClibc is default with following options enabled:
TARGET_i386=y
CONFIG_486=y
UCLIBC_HAS_FENV=y
LINUXTHREADS_OLD=y
make \
ARCH=x86 \
DESTDIR=/home/build-user/toolchain/i486-linux-uclibc \
CROSS=i486-linux-uclibc- \
RUNTIME_PREFIX=/ \
DEVEL_PREFIX=/ \
KERNEL_HEADERS=/home/build-user/toolchain/i486-linux-uclibc/include \
install
This step is successfully completed.
Build final GCC 4.7.3
GCC is built in a separate directory as specified in the documentation.
./configure \
--prefix=/home/build-user/toolchain \
--target=i486-linux-uclibc \
--with-arch=i486 \
--with-sysroot=/home/build-user/toolchain/i486-linux-uclibc \
--with-native-system-header-dir=/include \
--disable-multiarch \
--disable-nls \
--enable-libssp \
--enable-c99 \
--disable-libgomp \
--enable-long-long \
--disable-libmudflap \
--enable-languages=c \
--disable-multilib \
--with-gmp=/home/build-user/toolchain/gmp \
--with-mpfr=/home/build-user/toolchain/mpfr \
--with-mpc=/home/build-user/toolchain/mpc
make
This step fails with the following errors:
/bin/bash /home/build-user/toolchain/packages/gcc-4.7.3/libgcc/../mkinstalldirs .
/home/build-user/toolchain/gcc-final-build/./gcc/xgcc -B/home/build-user/toolchain/gcc-final-build/./gcc/ -B/home/build-user/toolchain/i486-linux-uclibc/bin/ -B/home/build-user/toolchain/i486-linux-uclibc/lib/ -isystem /home/build-user/toolchain/i486-linux-uclibc/include -isystem /home/build-user/toolchain/i486-linux-uclibc/sys-include -O2 -g -O2 -DIN_GCC -DCROSS_DIRECTORY_STRUCTURE -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition -isystem ./include -fpic -g -DIN_LIBGCC2 -fbuilding-libgcc -fno-stack-protector -shared -nodefaultlibs -Wl,--soname=libgcc_s.so.1 -Wl,--version-script=libgcc.map -o ./libgcc_s.so.1.tmp -g -O2 -B./ _muldi3_s.o _negdi2_s.o _lshrdi3_s.o _ashldi3_s.o _ashrdi3_s.o _cmpdi2_s.o _ucmpdi2_s.o _clear_cache_s.o _trampoline_s.o __main_s.o _absvsi2_s.o _absvdi2_s.o _addvsi3_s.o _addvdi3_s.o _subvsi3_s.o _subvdi3_s.o _mulvsi3_s.o _mulvdi3_s.o _negvsi2_s.o _negvdi2_s.o _ctors_s.o _ffssi2_s.o _ffsdi2_s.o _clz_s.o _clzsi2_s.o _clzdi2_s.o _ctzsi2_s.o _ctzdi2_s.o _popcount_tab_s.o _popcountsi2_s.o _popcountdi2_s.o _paritysi2_s.o _paritydi2_s.o _powisf2_s.o _powidf2_s.o _powixf2_s.o _powitf2_s.o _mulsc3_s.o _muldc3_s.o _mulxc3_s.o _multc3_s.o _divsc3_s.o _divdc3_s.o _divxc3_s.o _divtc3_s.o _bswapsi2_s.o _bswapdi2_s.o _clrsbsi2_s.o _clrsbdi2_s.o _fixunssfsi_s.o _fixunsdfsi_s.o _fixunsxfsi_s.o _fixsfdi_s.o _fixdfdi_s.o _fixxfdi_s.o _fixunssfdi_s.o _fixunsdfdi_s.o _fixunsxfdi_s.o _floatdisf_s.o _floatdidf_s.o _floatdixf_s.o _floatundisf_s.o _floatundidf_s.o _floatundixf_s.o _divdi3_s.o _moddi3_s.o _udivdi3_s.o _umoddi3_s.o _udiv_w_sdiv_s.o _udivmoddi4_s.o tf-signs_s.o addtf3_s.o divtf3_s.o eqtf2_s.o getf2_s.o letf2_s.o multf3_s.o negtf2_s.o subtf3_s.o unordtf2_s.o fixtfsi_s.o fixunstfsi_s.o floatsitf_s.o floatunsitf_s.o fixtfdi_s.o fixunstfdi_s.o floatditf_s.o floatunditf_s.o extendsftf2_s.o extenddftf2_s.o extendxftf2_s.o trunctfsf2_s.o trunctfdf2_s.o trunctfxf2_s.o enable-execute-stack_s.o unwind-dw2_s.o unwind-dw2-fde-dip_s.o unwind-sjlj_s.o unwind-c_s.o emutls_s.o libgcc.a -lc && rm -f ./libgcc_s.so && if [ -f ./libgcc_s.so.1 ]; then mv -f ./libgcc_s.so.1 ./libgcc_s.so.1.backup; else true; fi && mv ./libgcc_s.so.1.tmp ./libgcc_s.so.1 && ln -s libgcc_s.so.1 ./libgcc_s.so
/home/build-user/toolchain/i486-linux-uclibc/bin/ld: cannot find /lib/libc.so.0
/home/build-user/toolchain/i486-linux-uclibc/bin/ld: cannot find /lib/uclibc_nonshared.a
/home/build-user/toolchain/i486-linux-uclibc/bin/ld: cannot find /lib/ld-uClibc.so.0
collect2: error: ld returned 1 exit status
make[2]: *** [libgcc_s.so] Error 1
make[2]: Leaving directory `/home/build-user/toolchain/gcc-final-build/i486-linux-uclibc/libgcc'
make[1]: *** [all-target-libgcc] Error 2
make[1]: Leaving directory `/home/build-user/toolchain/gcc-final-build'
make: *** [all] Error 2
I check the linker search path:
/home/build-user/toolchain/i486-linux-uclibc/bin/ld --verbose | grep SEARCH_DIR
SEARCH_DIR("/home/build-user/toolchain/i486-linux-uclibc/lib");
The path is correct, so I search for the allegedly missing files in the directory:
find /home/build-user/toolchain/i486-linux-uclibc/lib \( -name libc.so.0 -o -name uclibc_nonshared.a -o -name ld-uClibc.so.0 \)
/home/build-user/toolchain/i486-linux-uclibc/lib/uclibc_nonshared.a
/home/build-user/toolchain/i486-linux-uclibc/lib/ld-uClibc.so.0
/home/build-user/toolchain/i486-linux-uclibc/lib/libc.so.0
All the files are found in the directory.
Can someone tell me what I'm doing wrong?
This is a guess, but when I build GCC cross compilers, I use sysroot equal to prefix. This creates a special configuration for binutils and GCC which may help you here.
I've had a similar problem lately.
I've used: uClibc-ng-1.0.29, GCC-6.2.0, Linux from Analog Devices (4.6.0 or something close to it) and binutils-2.27. It was a cross-build for ARM. But I think it should also work in this case.
The problem is that when building uClibc, libc.so is not a real binary but a linker script that contains locations of libc.so.1, uclibc_nonshared.a and ld-uClibc.so.1. When uClibc is built the way you did, the linker script (libc.so) provides locations of these libraries with respect to /.
The solutions is to build uClibc like this:
D=home/build-user/toolchain/i486-linux-uclibc;
make \
ARCH=x86 \
DESTDIR=/ \
CROSS=i486-linux-uclibc- \
RUNTIME_PREFIX=${D}/ \
DEVEL_PREFIX=${D}/usr/ \
KERNEL_HEADERS=/home/build-user/toolchain/i486-linux-uclibc/include \
install
The eventual installation location remains the same, but the entrances in libc.so will contain now the correct paths to the missing/not-found libraries.
If you need for some reason RUNTIME_PREFIX to be the same as DEVEL_PREFIX then keep it. But uClibc's .config file suggests the way I proposed. Note that it may also affect building of GCC.
I have compiled Ffmpeg (1.0) with newt configuration:
./configure --disable-doc --disable-ffplay --disable-ffprobe --disable-ffserver --disable-avdevice --disable-avfilter --disable-pthreads --disable-everything --enable-muxer=flv --enable-encoder=flv --enable-encoder=h263 --disable-mmx --disable-shared --prefix=bin/ --disable-protocols --disable-network --disable-debug --disable-asm --disable-stripping
It compiled - no errors - headers and libs (static .a) are in place. (special experimental cigwin, experimental gcc, with no asm options, and no known by ffmpeg platform defines) (yet I have compiled and tested boost on it)
Now I try to compile my app. I get next exceptions:
../ffmpeg-1.0/bin/lib/libavcodec.a: error: undefined reference to 'exp'
../ffmpeg-1.0/bin/lib/libavcodec.a: error: undefined reference to 'log'
My compiler build line looks like this:
g++ -static -emit-swf -o CloudClient.swf -I../boost/boost_libraries/install-dir/include -I../ffmpeg-1.0/bin/include -L../boost/boost_libraries/install-dir/lib -L../ffmpeg-1.0/bin/lib \
timer.o \
audio_encoder.o \
audio_generator.o \
video_encoder.o \
video_generator_rainbow.o \
simple_synchronizer.o \
multiplexer.o \
transmitter.o \
graph_runner.o \
cloud_client.o \
-pthread \
-lswscale \
-lavutil \
-lavformat \
-lavcodec \
-lboost_system \
-lboost_date_time \
-lboost_thread
So as you see quite complex and I already have all object files compiled and ready... Only one thing left - link it all to ffmpeg (striped from ffmpeg version compiled with boost)
Tried adding -lm - no help...
Well here my question is - how to get list of libraries ffmpeg linked to (like -lm etc)?
If in Linux, try
ldd /path/to/ffmpeg-binary
or
ldd `which ffmpeg`