Skip to contentSkip to conversation

livepatch-build-tools: fixes for non GNU tools and alignment

PATCH In review Roger Pau Monne 2 participants 5 messages Nov 28, 2023 – Dec 6, 2023

lore
Share & tools
lore
#0 lore raw

v2 cover livepatch-build-tools: fixes for non GNU tools and alignment

Hello,

The series contains two fixes for using the tools on non GNU environments, plus one extra fix to account for section alignment when calculating old function size.

Thanks, Roger.

Roger Pau Monne (3):
livepatch-build-tools: do not use readlink -m option
livepatch-build-tools: remove usage of gawk
livepatch-build-tools: account for section alignment when calculating
function size

 common.h             |  2 ++
 create-diff-object.c |  5 +++++
 livepatch-build      | 38 +++++++++++++++++++++-----------------
 3 files changed, 28 insertions(+), 17 deletions(-)

base-commit: e588b7914e7afa3abb64b15a32fc2fdb57ded341

Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/3 livepatch-build-tools: do not use readlink -m option

Busybox readlink implementation only supports the -f option to follow symlinks, so adjust the logic in order to keep the same behaviour without using the -m option.

---
 livepatch-build | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)
livepatch-build +16 -13
index 91d203bda0eb..305644037ee7 100755
@@ -336,8 +336,8 @@ while [[ $# -gt 0 ]]; do
             ;;
         --xen-syms)
             shift
-            XENSYMS="$(readlink -m -- "$1")"
-            [ -f "$XENSYMS" ] || die "xen-syms file does not exist"
+            [ -f "$1" ] || die "xen-syms file does not exist"
+            XENSYMS="$(readlink -f -- "$1")"
             shift
             ;;
         --depends)
@@ -366,22 +366,20 @@ while [[ $# -gt 0 ]]; do
 done
 
 [ -z "$srcarg" ] && die "Xen directory not given"
+[ -d "$srcarg" ] || die "Xen directory does not exist"
 [ -z "$patcharg" ] && die "Patchfile not given"
+[ -f "$patcharg" ] || die "Patchfile does not exist"
 [ -z "$configarg" ] && die ".config not given"
+[ -f "$configarg" ] || die ".config does not exist"
 [ -z "$outputarg" ] && die "Output directory not given"
 [ -z "$DEPENDS" ] && die "Build-id dependency not given"
 [ -z "$XEN_DEPENDS" ] && die "Xen Build-id dependency not given"
 
-SRCDIR="$(readlink -m -- "$srcarg")"
+SRCDIR="$(readlink -f -- "$srcarg")"
 # We need an absolute path because we move around, but we need to
 # retain the name of the symlink (= realpath -s)
 PATCHFILE="$(readlink -f "$(dirname "$patcharg")")/$(basename "$patcharg")"
-CONFIGFILE="$(readlink -m -- "$configarg")"
-OUTPUT="$(readlink -m -- "$outputarg")"
-
-[ -d "${SRCDIR}" ] || die "Xen directory does not exist"
-[ -f "${PATCHFILE}" ] || die "Patchfile does not exist"
-[ -f "${CONFIGFILE}" ] || die ".config does not exist"
+CONFIGFILE="$(readlink -f -- "$configarg")"
 
 PATCHNAME=$(make_patch_name "${PATCHFILE}")
 
@@ -390,17 +388,20 @@ echo
 echo "Xen directory: ${SRCDIR}"
 echo "Patch file: ${PATCHFILE}"
 echo ".config file: ${CONFIGFILE}"
-echo "Output directory: ${OUTPUT}"
+echo "Output directory: $outputarg"
 echo "================================================"
 echo
 
 if [ "${SKIP}" != "build" ]; then
-    [ -e "${OUTPUT}" ] && die "Output directory exists"
+    # Make sure output directory doesn't exist, and create it.
+    [ -e "$outputarg" ] && die "Output directory exists"
+    mkdir -p "$outputarg" || die
+    OUTPUT="$(readlink -f -- "$outputarg")"
+
     grep -q 'CONFIG_LIVEPATCH=y' "${CONFIGFILE}" || die "CONFIG_LIVEPATCH must be enabled"
     cd "$SRCDIR" || die
     patch -s -N -p1 -f --fuzz=0 --dry-run < "$PATCHFILE" || die "Source patch file failed to apply"
 
-    mkdir -p "${OUTPUT}" || die
     cp -f "${CONFIGFILE}" "${OUTPUT}/.config"
     cp -f "${OUTPUT}/.config" "xen/.config"
 
@@ -453,7 +454,9 @@ if [ "${SKIP}" != "build" ]; then
 fi
 
 if [ "${SKIP}" != "diff" ]; then
-    [ -d "${OUTPUT}" ] || die "Output directory does not exist"
+    cd "${SCRIPTDIR}" || die
+    [ -d "$outputarg" ] || die "Output directory does not exist"
+    OUTPUT="$(readlink -f -- "$outputarg")"
 
     cd "${OUTPUT}" || die
     create_patch

base-commit: e588b7914e7afa3abb64b15a32fc2fdb57ded341

Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/3 livepatch-build-tools: remove usage of gawk

In reply to Roger Pau Monne

And instead use plain awk.

Since plain awk cannot do the conversion from hex to decimal, use the shell (bash) printf to convert the (maybe) hexadecimal output of readelf.

---
Changes since v1:
- Use shell printf to convert.

---
 livepatch-build | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
livepatch-build +5 -4
index 305644037ee7..e2ccce4f7fd7 100755
@@ -423,14 +423,15 @@ if [ "${SKIP}" != "build" ]; then
     echo "Reading special section data"
     # Using xen-syms built in the previous step by build_full().
     SPECIAL_VARS=$(readelf -wi "$OUTPUT/xen-syms" |
-               gawk --non-decimal-data '
+               awk '
                BEGIN { a = b = e = 0 }
                a == 0 && /DW_AT_name.* alt_instr/ {a = 1; next}
                b == 0 && /DW_AT_name.* bug_frame/ {b = 1; next}
                e == 0 && /DW_AT_name.* exception_table_entry/ {e = 1; next}
-               a == 1 {printf("export ALT_STRUCT_SIZE=%d\n", $4); a = 2}
-               b == 1 {printf("export BUG_STRUCT_SIZE=%d\n", $4); b = 2}
-               e == 1 {printf("export EX_STRUCT_SIZE=%d\n", $4); e = 2}
+               # Use shell printf to (possibly) convert from hex to decimal
+               a == 1 {printf("export ALT_STRUCT_SIZE=`printf \"%%d\" \"%s\"`\n", $4); a = 2}
+               b == 1 {printf("export BUG_STRUCT_SIZE=`printf \"%%d\" \"%s\"`\n", $4); b = 2}
+               e == 1 {printf("export EX_STRUCT_SIZE=`printf \"%%d\" \"%s\"`\n", $4); e = 2}
                a == 2 && b == 2 && e == 2 {exit}')
     [[ -n $SPECIAL_VARS ]] && eval "$SPECIAL_VARS"
     if [[ -z $ALT_STRUCT_SIZE ]] || [[ -z $BUG_STRUCT_SIZE ]] || [[ -z $EX_STRUCT_SIZE ]]; then
Signature · 1 line
2.43.0
#3 lore raw

PATCH v2 3/3 livepatch-build-tools: account for section alignment when calculating function size

In reply to Roger Pau Monne

Forcing function section alignment at the compiler level ensures that enough space is present in the text section so that at least a jump can be encoded in the old function body to switch to the newly loaded code payload.

Modify create-diff-object to account for any section alignment when calculating the size of the old function.

When used with a suitable -falign-function compiler parameter on the Xen hypervisor build this ensures that all functions have enough space to accommodate for any control flow instructions plus a jump.

Note that while the rounding could be applied to all (text) symbols, it's not required, as the size is only relevant for functions that are live patched, and hence only do the rounding when calculating the old (previous) function size.

---
Changes since v2:
- New in this version.

---
 common.h             | 2 ++
 create-diff-object.c | 5 +++++
 2 files changed, 7 insertions(+)
common.h +2 -0
index 0d3e5f1dd3b5..5ff9ef6ca8e9 100644
@@ -37,6 +37,8 @@ extern char *childobj;
 	list_add_tail(&(_new)->list, (_list)); \
 }
 
+#define ROUNDUP(x, a) (((x) + (a) - 1) & ~((a) - 1))
+
 enum loglevel {
 	DEBUG,
 	NORMAL
create-diff-object.c +5 -0
index d0e14e3a62bb..fed360a9aa68 100644
@@ -2015,6 +2015,11 @@ static void livepatch_create_patches_sections(struct kpatch_elf *kelf,
 					ERROR("lookup_global_symbol %s",
 					      sym->name);
 			}
+
+			/* Take into account section alignment for padding. */
+			result.size = ROUNDUP(result.size,
+					      sym->sec->sh.sh_addralign);
+
 			log_debug("lookup for %s @ 0x%016lx len %lu\n",
 			          sym->name, result.value, result.size);
 
Signature · 1 line
2.43.0
Reviewed-by on the v2 series
#4 lore raw
In reply to Roger Pau Monne
Quoted 21 lines · Roger Pau Monne

Hello,

The series contains two fixes for using the tools on non GNU environments, plus one extra fix to account for section alignment when calculating old function size.

Thanks, Roger.

Roger Pau Monne (3):
livepatch-build-tools: do not use readlink -m option
livepatch-build-tools: remove usage of gawk
livepatch-build-tools: account for section alignment when calculating
function size

 common.h             |  2 ++
 create-diff-object.c |  5 +++++
 livepatch-build      | 38 +++++++++++++++++++++-----------------
 3 files changed, 28 insertions(+), 17 deletions(-)
Quoted 4 lines

base-commit: e588b7914e7afa3abb64b15a32fc2fdb57ded341 --
2.43.0

... and applied, thanks.