Emulate MIPS firmware in qemu - embedded

I'm trying to emulate a firmware with qemu, but I'm getting an error while trying to execute it.
First of all I extracted the firmware filesystem to a folder, and copied de qemu-mipsel file to it:
bin cdrom dev etc home init lib linuxrc mnt opt proc qemu-mipsel root sbin sys tango tmp udev usr var
Then I executed the emulator with chroot:
chroot . ./qemu-mipsel ./bin/ls
And I get the following error:
chroot: failed to run command `./qemu-mipsel': No such file or directory
Googling the error I found that it means that qemu depends on libraries not included in the chroot environment so I searched for them:
linux-vdso.so.1 => (0x00007fffe79ff000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fcd9da57000)
libgthread-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgthread-2.0.so.0 (0x00007fcd9d855000)
libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007fcd9d55d000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fcd9d2db000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fcd9d0be000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fcd9cd34000)
/lib64/ld-linux-x86-64.so.2 (0x00007fcda007a000)
libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007fcd9caf7000)
And some libraries are missing in ./lib/:
linux-vdso.so.1
libgthread-2.0.so.0
libglib-2.0.so.0
/lib64/ld-linux-x86-64.so.2
libpcre.so.3
How could I solve this problem? Can I create a symbolic link to the system library or should I copy them? Also linux-vdso.so.1 is not present on the system, where could I get it? Regards.

linux-vdso.so.1 is in reality the kernel. The other files, you get them from your GNU/Linux distribution of choice (e.g. Debian, from the source packages eglibc, pcre3 and glib2.0). You must indeed copy them into the chroot. For /lib64/ld-linux-x86-64.so.2 the pathname must be exact, as that path is hard-coded into the binary.
It seems you’re running Debian already, from the multiarch’d paths, which is good, because you’ll need to distinguish between host and target libraries.
Alternatively, you can compile and link qemu-mipsel statically.

Related

Build llvm-ar statically

I'm trying to compile llvm-ar (and only that) from LLVM statically because I need a static ar implementation for another purpose. AFAICS the official LLVM building documentation doesn't discuss this usage, only the option to build with shared libs, which is disabled by default anyway. Adding -static to CLAGS or the CMake option LLVM CFLAGS doesn't change the build process at all.
This is the build process I'm running right now:
cmake -DLLVM_ENABLE_PROJECTS=llvm-ar -G "Unix Makefiles" ../llvm
make -j3 llvm-ar
which then builds a dynamic llvm-ar:
$ ldd bin/llvm-ar
linux-vdso.so.1 (0x00007fff46154000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007faa84536000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007faa84319000)
libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007faa840ef000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007faa83d66000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007faa839c8000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007faa837b0000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007faa833bf000)
/lib64/ld-linux-x86-64.so.2 (0x00007faa87163000)
How can I cause this target to be compiled statically? I'd also like to build with musl-gcc instead of the system default.

mod_wsgi shared library is over 1MB using pyenv

I work on an AWS lightsail server with a LAMP self-contained installation stack and I want to host a second web-app in django.
Tried to install mod_wsgi to a pyenv virtual environment (3.8.3 or 3.8-dev, both with shared libraries installed) using
export APXS=/opt/USER/apache2/bin/apxs
pip install mod_wsgi (tried with and w/o wheel)
but the module mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so that created is over 1MB in size.
-rwxrwxr-x 1 USER USER 1157792 Jun 21 20:15 mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so
ldd gives:
ldd mod_wsgi-py38.cpython-38-x86_64-linux-gnu.so
linux-vdso.so.1 => (0x00007ffc3e198000)
libpython3.8.so.1.0 => /home/USER/.pyenv/versions/3.8-dev/lib/libpython3.8.so.1.0 (0x00007fce67120000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fce66f03000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fce66b39000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fce66935000)
libutil.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007fce66732000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fce66429000)
/lib64/ld-linux-x86-64.so.2 (0x00007fce678f0000)
According to the manual https://modwsgi.readthedocs.io/en/develop/user-guides/installation-issues.html#lack-of-python-shared-library, that should be a problem for my server's memory performance.
Is there something else that I could do in order to get a ~250KB in size module as the docs describe it?
mod_wsgi.so size of 1.1MB is fine nowadays.
Newbie questions regarding installation and python shared library

Shared object library not listing particular shared object dependencies

Using target_link_libraries in my CMakeLists.txt file results in only some of the shared-object library dependencies being set in the built library.
In my CMakeLists.txt file I have this:
set(STIREN_LIBS)
list(APPEND STIREN_LIBS
/usr/lib/x86_64-linux-gnu/libcairo.so
/usr/lib/x86_64-linux-gnu/libboost_filesystem.so
/usr/lib/x86_64-linux-gnu/libboost_regex.so
/usr/lib/x86_64-linux-gnu/libicuuc.so
/usr/lib/x86_64-linux-gnu/libproj.so.12
/usr/lib/x86_64-linux-gnu/libharfbuzz.so
/usr/lib/x86_64-linux-gnu/libxml2.so
)
If I call target_link_libraries on that, as follows:
target_link_libraries(${PROJECT_NAME} PRIVATE ${STIREN_LIBS})
I get this as the result of objdump -p
Dynamic Section:
NEEDED libcairo.so.2
NEEDED libboost_filesystem.so.1.65.1
NEEDED libboost_regex.so.1.65.1
NEEDED libproj.so.12
NEEDED libxml2.so.2
NEEDED libstdc++.so.6
NEEDED libm.so.6
NEEDED libgcc_s.so.1
NEEDED libc.so.6
NEEDED ld-linux-x86-64.so.2
SONAME libstiren.so
It is missing the dependencies for libicuuc and lifharfbuzz. I confirm that those file paths are valid.
If I omit the target_link_libraries, I get this in the output of objdump -p
Dynamic Section:
NEEDED libstdc++.so.6
NEEDED libm.so.6
NEEDED libgcc_s.so.1
NEEDED libc.so.6
NEEDED ld-linux-x86-64.so.2
SONAME libstiren.so
So why is it that only some dependencies making it through?

Unable to link libssl to libcrypto

I have created an application where it uses libssl which apparently uses libcrypto. I have kept these libraries at a user-defined location /path/to/xyz/lib/
---------------------------------------------------------------------------------------------------------------------------------
I have written a CMake for the application which links the libraries as follows:
set(GCC_LINK_FLAGS "-L/path/to/xyz/lib -lcurl -lpthread -lcrypto -lssl //other libs")
add_executable(Foo ${APP_SOURCE_FILES})
target_link_libraries(Foo ${GCC_LINK_FLAGS})
The CMake didnot give any error and created the Makefile which creates the executable.
---------------------------------------------------------------------------------------------------------------------------------
But When I run the executable. it throws the below error
./Foo: error while loading shared libraries: libcrypto.so.3: cannot open shared object file: No such file or directory
I have checked the dynamic linking of libssl.so as follows and found the libcrypto isn't linked...
[root#localhost build]# ldd /path/to/xyz/lib/libssl.so.3
linux-vdso.so.1 => (0x00007fffa5fa7000)
libcrypto.so.3 => not found
libdl.so.2 => /lib64/libdl.so.2 (0x00007f17ea772000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f17ea556000)
libc.so.6 => /lib64/libc.so.6 (0x00007f17ea193000)
/lib64/ld-linux-x86-64.so.2 (0x0000563b0856f000)
I have checked the /path/to/xyz/lib/pkgconfig/libssl.pc file and found this suspicious for this error
prefix=/path/to/xyz/lib/
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include
Name: OpenSSL-libssl
Description: Secure Sockets Layer and cryptography libraries
Version: 3.0.0-dev
Requires.private: libcrypto
Libs: -L${libdir} -lssl
Cflags: -I${includedir}
QUESTION
What does Requires.private: libcrypto do?
And how to solve this error?
--------------------------------------------------------------EDIT--------------------------------------------------------------
I have Done following edits in the cmake file
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
set(CMAKE_INSTALL_RPATH "/path/to/xyz/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
add_executable(SpeechToText ${APP_SOURCE_FILES})
target_link_libraries(Foo ${GCC_LINK_FLAGS})
install (TARGETS Foo RUNTIME DESTINATION /path/to/project/directory)
I am getting segmentation fault by this change.
Install the project...
-- Install configuration: ""
-- Installing: /path/to/project/directory/Foo
-- Set runtime path of "/path/to/project/directory/Foo" to "/path/to/xyz/lib"
[root#localhost build]# ../Foo
Segmentation fault (core dumped)

cmake how to partial static link, partial dynamic link?

I'm working on a c++ project witch use mongodb as database.
I want to static link the mongodb driver.
I use this command to build the executable binary.
g++ -o ox one.cpp -pthread -Wall -std=c++11 \
-I /opt/mongo-cxx-driver/include/bsoncxx/v_noabi/ \
-I /opt/mongo-cxx-driver/include/mongocxx/v_noabi/ \
-L /opt/mongo-cxx-driver/lib/ \
-L /opt/mongo-c-driver/lib/ \
-Wl,-Bstatic -lmongocxx -lbsoncxx -lmongoc-1.0 -lbson-1.0 \
-Wl,-Bdynamic -lgcc_s -lstdc++ -lcrypto -lssl -lrt
ldd ox reports that mongodb driver has been static linked
linux-vdso.so.1 => (0x00007ffd1a99d000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f7f8e4e0000)
libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f7f8e15e000)
libcrypto.so.1.0.0 => /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 (0x00007f7f8dd19000)
libssl.so.1.0.0 => /lib/x86_64-linux-gnu/libssl.so.1.0.0 (0x00007f7f8dab0000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f7f8d8a8000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7f8d689000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7f8d2bf000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7f8cfb7000)
/lib64/ld-linux-x86-64.so.2 (0x000055fc81736000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7f8cdb2000)
Now, I can simply copy the ox file to another computer (same os, same version) to use it.
But, My question is that, How to write rule in CMakeList.txt, to tell cmake which libraries I want to static link, which libraries I want to dynamic link ?
If you wanna dynamically link a library, you must have a .so version of that library; likewise, if you wanna statically link a library, you must have a .a version of that library. Of course, this is for Linux like system