diff mbox series

[livepatch-build-tools,part2,v2,4/6] livepatch-build: detect special section group sizes

Message ID 20190808122909.7162-1-wipawel@amazon.de (mailing list archive)
State New, archived
Headers show
Series None | expand

Commit Message

Wieczorkiewicz, Pawel Aug. 8, 2019, 12:29 p.m. UTC
Hard-coding the special section group sizes is unreliable. Instead,
determine them dynamically by finding the related struct definitions
in the DWARF metadata.

This is a livepatch backport of kpatch upstream commit [1]:
kpatch-build: detect special section group sizes 170449847136a48b19fc

Xen only deals with alt_instr, bug_frame and exception_table_entry
structures, so sizes of these structures are obtained from xen-syms.

This change is needed since with recent Xen the alt_instr structure
has changed size from 12 to 14 bytes.

[1] https://github.com/jpoimboe/kpatch/commit/170449847136a48b19fcceb19c1d4d257d386b56

Signed-off-by: Pawel Wieczorkiewicz <wipawel@amazon.de>
Reviewed-by: Bjoern Doebel <doebel@amazon.de>
Reviewed-by: Martin Mazein <amazein@amazon.de>
---
v2:
* Applied suggestions from Ross:
  - moved the livepatch-build hunk after build_full() call and
    re-using newly built xen-syms
  - dropped the redundant bug_frames_3_group_size()
---
 create-diff-object.c | 50 ++++++++++++++++++++++++++++++++++++++++----------
 livepatch-build      | 22 ++++++++++++++++++++++
 2 files changed, 62 insertions(+), 10 deletions(-)

Comments

Ross Lagerwall Aug. 16, 2019, 9:08 a.m. UTC | #1
On 8/8/19 1:29 PM, Pawel Wieczorkiewicz wrote:
> Hard-coding the special section group sizes is unreliable. Instead,
> determine them dynamically by finding the related struct definitions
> in the DWARF metadata.
> 
> This is a livepatch backport of kpatch upstream commit [1]:
> kpatch-build: detect special section group sizes 170449847136a48b19fc
> 
> Xen only deals with alt_instr, bug_frame and exception_table_entry
> structures, so sizes of these structures are obtained from xen-syms.
> 
> This change is needed since with recent Xen the alt_instr structure
> has changed size from 12 to 14 bytes.
> 
> [1] https://github.com/jpoimboe/kpatch/commit/170449847136a48b19fcceb19c1d4d257d386b56
> 
> Signed-off-by: Pawel Wieczorkiewicz <wipawel@amazon.de>
> Reviewed-by: Bjoern Doebel <doebel@amazon.de>
> Reviewed-by: Martin Mazein <amazein@amazon.de>
> ---
> v2:
> * Applied suggestions from Ross:
>    - moved the livepatch-build hunk after build_full() call and
>      re-using newly built xen-syms
>    - dropped the redundant bug_frames_3_group_size()
Reviewed-by: Ross Lagerwall <ross.lagerwall@citrix.com>
diff mbox series

Patch

diff --git a/create-diff-object.c b/create-diff-object.c
index 0df3fea..c6183c3 100644
--- a/create-diff-object.c
+++ b/create-diff-object.c
@@ -958,12 +958,42 @@  static void kpatch_mark_constant_labels_same(struct kpatch_elf *kelf)
 	}
 }
 
-static int bug_frames_0_group_size(struct kpatch_elf *kelf, int offset) { return 8; }
-static int bug_frames_1_group_size(struct kpatch_elf *kelf, int offset) { return 8; }
-static int bug_frames_2_group_size(struct kpatch_elf *kelf, int offset) { return 8; }
-static int bug_frames_3_group_size(struct kpatch_elf *kelf, int offset) { return 16; }
-static int ex_table_group_size(struct kpatch_elf *kelf, int offset) { return 8; }
-static int altinstructions_group_size(struct kpatch_elf *kelf, int offset) { return 12; }
+static int bug_frames_group_size(struct kpatch_elf *kelf, int offset)
+{
+	static int size = 0;
+	char *str;
+	if (!size) {
+		str = getenv("BUG_STRUCT_SIZE");
+		size = str ? atoi(str) : 8;
+	}
+
+	return size;
+}
+
+static int ex_table_group_size(struct kpatch_elf *kelf, int offset)
+{
+	static int size = 0;
+	char *str;
+	if (!size) {
+		str = getenv("EX_STRUCT_SIZE");
+		size = str ? atoi(str) : 8;
+	}
+
+	return size;
+}
+
+static int altinstructions_group_size(struct kpatch_elf *kelf, int offset)
+{
+	static int size = 0;
+	char *str;
+	if (!size) {
+		str = getenv("ALT_STRUCT_SIZE");
+		size = str ? atoi(str) : 12;
+	}
+
+	log_debug("altinstr_size=%d\n", size);
+	return size;
+}
 
 /*
  * The rela groups in the .fixup section vary in size.  The beginning of each
@@ -1016,19 +1046,19 @@  static int fixup_group_size(struct kpatch_elf *kelf, int offset)
 static struct special_section special_sections[] = {
 	{
 		.name		= ".bug_frames.0",
-		.group_size	= bug_frames_0_group_size,
+		.group_size	= bug_frames_group_size,
 	},
 	{
 		.name		= ".bug_frames.1",
-		.group_size	= bug_frames_1_group_size,
+		.group_size	= bug_frames_group_size,
 	},
 	{
 		.name		= ".bug_frames.2",
-		.group_size	= bug_frames_2_group_size,
+		.group_size	= bug_frames_group_size,
 	},
 	{
 		.name		= ".bug_frames.3",
-		.group_size	= bug_frames_3_group_size,
+		.group_size	= bug_frames_group_size,
 	},
 	{
 		.name		= ".fixup",
diff --git a/livepatch-build b/livepatch-build
index 3c4bf13..7068faf 100755
--- a/livepatch-build
+++ b/livepatch-build
@@ -315,6 +315,28 @@  if [ "${SKIP}" != "build" ]; then
     echo "Perform full initial build with ${CPUS} CPU(s)..."
     build_full
 
+    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 '
+               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}
+               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
+        die "can't find special struct size"
+    fi
+    for i in $ALT_STRUCT_SIZE $BUG_STRUCT_SIZE $EX_STRUCT_SIZE; do
+        if [[ ! $i -gt 0 ]] || [[ ! $i -le 16 ]]; then
+            die "invalid special struct size $i"
+        fi
+    done
+
     echo "Apply patch and build with ${CPUS} CPU(s)..."
     cd "$SRCDIR" || die
     patch -s -N -p1 -f --fuzz=0 < "$PATCHFILE" || die