From 480471f46c191fbea617dc9b48d253fe1e32c2e6 Mon Sep 17 00:00:00 2001 From: xuxin19 Date: Mon, 19 Aug 2024 16:17:23 +0800 Subject: [PATCH] sim:support sim elf and dynamic libs package in post build use the dynamic library .so of the binding compilation environment, and then modify the dynamic loader of elf at runtime to run sim elf across system versions 1.add post-build for sim build 2.collect dynamic .so and dynamic interpreter 3.add execution script for the entire package sim elf change text-segment to 0x50000000 because patchelf will modify the .dynamic section in elf, the upward alignment of elf will be less than 0x3fffffff, which will cause asan shadow memory overlap error Signed-off-by: xuxin19 --- boards/sim/sim/sim/CMakeLists.txt | 16 +++++++++ boards/sim/sim/sim/scripts/Make.defs | 13 +++++++ tools/simlaunch.sh | 54 ++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 tools/simlaunch.sh diff --git a/boards/sim/sim/sim/CMakeLists.txt b/boards/sim/sim/sim/CMakeLists.txt index d3daa2c07d..d311e36927 100644 --- a/boards/sim/sim/sim/CMakeLists.txt +++ b/boards/sim/sim/sim/CMakeLists.txt @@ -19,3 +19,19 @@ # ############################################################################## add_subdirectory(src) + +add_custom_target( + nuttx_post_build + DEPENDS nuttx + WORKING_DIRECTORY ${CMAKE_BINARY_DIR} + COMMAND ${CMAKE_COMMAND} -E make_directory sim-pac + COMMAND ${CMAKE_COMMAND} -E make_directory sim-pac/libs + COMMAND ${CMAKE_COMMAND} -E copy nuttx sim-pac/nuttx + COMMAND ldd sim-pac/nuttx | grep \"=> /\" | awk '{print $$3}' | xargs -I '{}' + cp -v '{}' sim-pac/libs + COMMAND readelf -l nuttx | grep "program interpreter" | awk -F':' '{print + $$2}' | cut -d"]" -f1 | xargs -I '{}' cp -v '{}' sim-pac + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/tools/simlaunch.sh + sim-pac + COMMAND tar -czf nuttx.tgz sim-pac + COMMENT "Pac SIM with dynamic libs in nuttx.tgz") diff --git a/boards/sim/sim/sim/scripts/Make.defs b/boards/sim/sim/sim/scripts/Make.defs index 2d03dd3200..5e80ac6d0c 100644 --- a/boards/sim/sim/sim/scripts/Make.defs +++ b/boards/sim/sim/sim/scripts/Make.defs @@ -323,3 +323,16 @@ else ifeq ($(CONFIG_HOST_MACOS),) ARCHPICFLAGS += -no-pie LDFLAGS += -Wl,-no-pie endif + +define POSTBUILD + $(Q)echo "Pac SIM with dynamic libs.."; + @ rm -rf sim-pac; + @ mkdir -p sim-pac/libs; + @ cp nuttx sim-pac/nuttx; + @ ldd sim-pac/nuttx | grep "=> /" | awk '{print $$3}' | xargs -I '{}' cp -v '{}' sim-pac/libs; + @ readelf -l nuttx | grep "program interpreter" | awk -F':' '{print $$2}'| cut -d"]" -f1 | xargs -I '{}' cp -v '{}' sim-pac; + @ cp $(TOPDIR)/tools/simlaunch.sh sim-pac; + @ tar -czf nuttx.tgz sim-pac; + $(Q)echo "SIM elf with dynamic libs archive in nuttx.tgz" + @ rm -rf sim-pac; +endef diff --git a/tools/simlaunch.sh b/tools/simlaunch.sh new file mode 100644 index 0000000000..0b13e4e0dc --- /dev/null +++ b/tools/simlaunch.sh @@ -0,0 +1,54 @@ +#!/usr/bin/env bash +# tools/simlaunch.sh +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to you under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +if ! command -v patchelf &> /dev/null; then + echo "patchelf does not installed:" 1>&2 + echo "sudo apt-get update" 1>&2 + echo "sudo apt-get install patchelf" 1>&2 + exit 1 +fi + +CWD=`pwd` + +if [ ! -d ${CWD}/libs ]; then + echo "Directory for nuttx libs does not exist in ${CWD}." 1>&2 + exit 1 +fi + +if [ ! -e ${CWD}/nuttx ]; then + echo "Nuttx elf does not exist in ${CWD}." 1>&2 + exit 1 +fi + +INTERPRETER=$(find "${CWD}" -maxdepth 1 -type f -name '*ld-linux*') + +if [ ! -n "${INTERPRETER}" ]; then + echo "No dynamic interpreter exist in ${CWD}." 1>&2 + exit 1 +fi + +CURRENT_INTERPRETER=$(readelf -l nuttx |grep "program interpreter" | awk -F':' '{print $2}'| cut -d"]" -f1 | awk '{$1=$1};1') + +if [ "$INTERPRETER" != "$CURRENT_INTERPRETER" ]; then + echo "Patch nuttx so interpreter to ${INTERPRETER}" + patchelf --set-interpreter "$INTERPRETER" nuttx +fi + +export LD_LIBRARY_PATH=${CWD}/libs +"${CWD}/nuttx"