KasperskyOS Community Edition 1.3

Adding dynamic libraries to a KasperskyOS-based solution

There are two ways to add dynamic libraries to a KasperskyOS-based solution: by either automatic or manual search for the dynamic libraries required by the solution programs.

Automatic search for dynamic libraries

Use the PACK_DEPS_COPY_ONLY ON, PACK_DEPS_LIBS_PATH, and PACK_DEPS_COPY_TARGET parameters in the CMake commands build_kos_qemu_image() and build_kos_hw_image().

Dynamic libraries that are loaded into memory by calling the dlopen() function of the POSIX interface will not be found by an automatic search.

Example 1:

# This example creates a solution image for the hardware platform. # This solution image must include the storage drive image containing the dynamic # libraries. # Setting values of variables set (IMAGE_FS ${CMAKE_BINARY_DIR}/hdd) set (LIBS_PATH "${IMAGE_FS}/lib") set (DISK_IMG ramdisk0.img) if (blob_container_ENTITY_FOUND) # Configure the BlobContainer program set_target_properties (${blob_container_ENTITY} PROPERTIES DEPENDS_ON_ENTITY ${precompiled_vfsVfsRamFs} EXTRA_ENV " VFS_FILESYSTEM_BACKEND: client:kl.VfsRamFs") endif () # Create a storage drive image containing the dynamic libraries # The storage drive image will be created after completing the copylibs target, # which is created by the Cmake command build_kos_hw_image(). add_custom_command (OUTPUT ${DISK_IMG} DEPENDS copylibs COMMAND ${KL_SDK_ROOT_PATH}/common/prepare_hdd_img.sh -d ${IMAGE_FS} -s 64 -f ext4 -img ${DISK_IMG} COMMENT "Creating disk image '${DISK_IMG}' from files in '${IMAGE_FS}' ...") # Create a solution image for the hardware platform # The copylibs target indicates that the automatically found dynamic # libraries will be copied to the directory ${LIBS_PATH}. This directory will be included # in the storage drive image. The value ${DISK_IMG} is specified in the # IMAGE_FILES parameter, so the storage drive image will be created before the solution image is created, # and it will be included in the solution image. build_kos_hw_image (kos-image ... IMAGE_FILES ${ENTITIES_LIST} ${DISK_IMG} PACK_DEPS_COPY_ONLY ON PACK_DEPS_LIBS_PATH ${LIBS_PATH} PACK_DEPS_COPY_TARGET copylibs)

Example 2:

# This example to be run in QEMU creates a solution image and # a separate storage drive image containing the dynamic libraries, # and this example to be run on the hardware platform creates an SD card image # to which the solution image and dynamic libraries are added. # Set the values of variables used during the build # for the hardware platform set (HW_IMAGE_FS "${CMAKE_BINARY_DIR}/hdd_hw") set (HW_LIBS_PATH "${HW_IMAGE_FS}/lib") # Set the values of variables used during the build # for QEMU set (QEMU_IMAGE_FS "${CMAKE_BINARY_DIR}/hdd_qemu") set (QEMU_LIBS_PATH "${QEMU_IMAGE_FS}/lib") set (QEMU_DISK_IMAGE sdcard0.img) set (QEMU_FLAGS "-nic none -m 2048 -drive file=${QEMU_DISK_IMAGE},if=sd,format=raw") set (QEMU_DEPENDENCIES ${QEMU_DISK_IMAGE}) if (blob_container_ENTITY_FOUND) # Configure the BlobContainer program set_target_properties (${blob_container_ENTITY} PROPERTIES DEPENDS_ON_ENTITY ${precompiled_vfsVfsSdCardFs} EXTRA_ENV " VFS_FILESYSTEM_BACKEND: client:kl.VfsSdCardFs") # Create a storage drive image containing the dynamic libraries for QEMU add_custom_command (${QEMU_DISK_IMAGE} DEPENDS copylibs_qemu COMMAND ${KL_SDK_ROOT_PATH}/common/prepare_hdd_img.sh -d ${QEMU_IMAGE_FS} -s 64 -f fat32 -img ${QEMU_DISK_IMAGE} COMMENT "Creating disk image '${QEMU_DISK_IMAGE}' from files in '${QEMU_IMAGE_FS}' ...") # Create a solution image for QEMU # The copylibs_qemu target indicates that the automatically found dynamic # libraries will be copied to the directory ${QEMU_LIBS_PATH}. This directory will be included # in the storage drive image containing the dynamic libraries for QEMU. The value ${QEMU_FLAGS} # contains the -drive file=${QEMU_DISK_IMAGE},if=sd,format=raw parameters, which # are required for QEMU use of the storage drive image containing the dynamic # libraries. The value ${QEMU_DEPENDENCIES} is required to make sure that # the storage drive image containing the dynamic libraries is created before creation of the # solution image for QEMU. (The solution image will not include the storage drive image containing the # dynamic libraries, but you must add this target dependency # to create the storage drive image containing the dynamic libraries.) build_kos_qemu_image (kos-qemu-image ... QEMU_FLAGS "${QEMU_FLAGS}" QEMU_DEPENDENCIES "${QEMU_DEPENDENCIES}" PACK_DEPS_COPY_ONLY ON PACK_DEPS_LIBS_PATH "${QEMU_LIBS_PATH}" PACK_DEPS_COPY_TARGET copylibs_qemu IMAGE_FILES ${ENTITIES}) # Create a solution image for the hardware platform # The copylibs_hw target indicates that the automatically found dynamic # libraries will be copied to the directory ${HW_LIBS_PATH}. This directory will be included # in the SD card image that is created by the CMake command build_sd_image(). build_kos_hw_image (kos-image ... PACK_DEPS_COPY_ONLY ON PACK_DEPS_LIBS_PATH "${HW_LIBS_PATH}" PACK_DEPS_COPY_TARGET copylibs_hw IMAGE_FILES ${ENTITIES}) # The solution image for the hardware platform will be created after # the automatically found dynamic libraries are copied to the directory ${HW_LIBS_PATH}. # (The solution image will not include dynamic libraries, but you must add # this target dependency to complete copying prior to creation of # the SD card image.) add_dependencies (kos-image copylibs_hw) # Create the SD card image # The SD card image will be created based on the directory ${HW_IMAGE_FS}, # which includes the solution for the hardware platform and the directory # containing the dynamic libraries. The SD card image will be created # after creation of the solution image for the hardware platform. build_sd_image (sd-image KOS_IMAGE_TARGET kos-image IMAGE_FS ${HW_IMAGE_FS})

Manual search for dynamic libraries

You need to independently search for dynamic libraries required by the solution programs. This way, even dynamic libraries that were loaded into memory by calling the dlopen() function of the POSIX interface can be added to a solution.

Example 1:

# This example creates a solution image that includes # the libm.so dynamic library in addition to the executable files of programs. # Configure the VFS program set (VFS_SDCARD_ARGS "\ - -l - nodev /tmp ramfs 0 - -l - devfs /dev devfs 0 - -l - romfs /lib romfs ro") set_target_properties (${precompiled_vfsVfsSdCardFs} PROPERTIES EXTRA_ARGS ${VFS_SDCARD_ARGS}) if (blob_container_ENTITY_FOUND) # Configure the BlobContainer program set_target_properties (${blob_container_ENTITY} PROPERTIES DEPENDS_ON_ENTITY ${precompiled_vfsVfsSdCardFs} EXTRA_ENV " VFS_FILESYSTEM_BACKEND: client:kl.VfsSdCardFs") endif () if(PLATFORM_SUPPORTS_DYNAMIC_LINKING) # Get the full path to the libm.so dynamic library find_file(LIBM_SO_FILE libm.so PATH_SUFFIXES lib REQUIRED) set(EXTRA_FILES ${LIBM_SO_FILE}) else() set(EXTRA_FILES) endif() # Create a solution image for the hardware platform # The libm.so dynamic library will be added to the # solution image because the value ${EXTRA_FILES} # specified in the IMAGE_FILES parameter contains the full # path to this library. build_kos_hw_image(kos-image ... IMAGE_FILES ${ENTITIES} ${EXTRA_FILES})

Example 2:

# This example to be run in QEMU creates a solution image and # a separate storage drive image containing the libm.so dynamic library. # Setting values of variables set (QEMU_FLAGS "-nic none -m 2048") set (QEMU_DEPENDENCIES) if (PLATFORM_SUPPORTS_DYNAMIC_LINKING) # Setting values of variables set (IMAGE_FS "${CMAKE_BINARY_DIR}/hdd") set (LIBS_PATH "${IMAGE_FS}/lib") set (QEMU_DISK_IMAGE sdcard0.img) string (APPEND QEMU_FLAGS "-drive file=${QEMU_DISK_IMAGE},if=sd,format=raw") set (QEMU_DEPENDENCIES ${QEMU_DISK_IMAGE}) # Configure the BlobContainer program set_target_properties (${blob_container_ENTITY} PROPERTIES DEPENDS_ON_ENTITY ${precompiled_vfsVfsSdCardFs} EXTRA_ENV " VFS_FILESYSTEM_BACKEND: client:kl.VfsSdCardFs") # Get the full path to the libm.so dynamic library find_file (LIBM_SO_FILE libm.so PATH_SUFFIXES lib REQUIRED) # Create a storage drive image containing the libm.so dynamic library add_custom_command (OUTPUT ${QEMU_DISK_IMAGE} COMMAND ${CMAKE_COMMAND} -E make_directory ${LIBS_PATH} COMMAND ${CMAKE_COMMAND} -E copy ${LIBM_SO_FILE} ${LIBS_PATH} COMMAND ${KL_SDK_ROOT_PATH}/common/prepare_hdd_img.sh -d ${IMAGE_FS} -s 64 -f fat32 -img ${QEMU_DISK_IMAGE} COMMENT "Creating disk image '${QEMU_DISK_IMAGE}' from files in '${IMAGE_FS}' ...") endif () # Create a solution image for QEMU # The value ${QEMU_FLAGS} contains the -drive file=${QEMU_DISK_IMAGE},if=sd,format=raw parameters, # which are required for QEMU use of the storage drive image containing the # libm.so dynamic library. The value ${QEMU_DEPENDENCIES} is required to make sure that # the storage drive image containing the libm.so dynamic library is created before creation of the # solution image for QEMU. (The solution image will not include the storage drive image containing the # libm.so dynamic library, but you must add this target dependency # to create the storage drive image containing the libm.so dynamic library.) build_kos_qemu_image (kos-qemu-image ... QEMU_FLAGS "${QEMU_FLAGS}" QEMU_DEPENDENCIES "${QEMU_DEPENDENCIES}" IMAGE_FILES ${ENTITIES})