Skip to contentSkip to conversation

common: Add is_standard_section() helper function

PATCH In review Pawel Wieczorkiewicz 4 participants 21 messages Apr 16, 2019 – Apr 30, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/6 common: Add is_standard_section() helper function

Detect standard (always to be included) sections via their section header type. The standard sections: ".shstrtab", ".symtab", ".strtab" are either of type SHT_SYMTAB or SHT_STRTAB.

---
 common.c             | 12 ++++++++++++
 common.h             |  1 +
 create-diff-object.c |  5 +----
 3 files changed, 14 insertions(+), 4 deletions(-)
common.c +12 -0
index bc63955..1fb07cb 100644
@@ -5,6 +5,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <stdbool.h>
 #include <gelf.h>
 
 #include "list.h"
@@ -258,6 +259,17 @@ int is_debug_section(struct section *sec)
 	return !strncmp(name, ".debug_", 7);
 }
 
+int is_standard_section(struct section *sec)
+{
+	switch (sec->sh.sh_type) {
+	case SHT_STRTAB:
+	case SHT_SYMTAB:
+		return true;
+	default:
+		return false;
+	}
+}
+
 /* returns the offset of the string in the string table */
 int offset_of_string(struct list_head *list, char *name)
 {
common.h +1 -0
index 7599fe7..cda690d 100644
@@ -150,6 +150,7 @@ struct symbol *find_symbol_by_name(struct list_head *list, const char *name);
 int is_text_section(struct section *sec);
 int is_debug_section(struct section *sec);
 int is_rela_section(struct section *sec);
+int is_standard_section(struct section *sec);
 int is_local_sym(struct symbol *sym);
 
 void rela_insn(struct section *sec, struct rela *rela, struct insn *insn);
create-diff-object.c +1 -4
index 82f777e..1e6e617 100644
@@ -1278,10 +1278,7 @@ static void kpatch_include_standard_elements(struct kpatch_elf *kelf)
 
 	list_for_each_entry(sec, &kelf->sections, list) {
 		/* include these sections even if they haven't changed */
-		if (!strcmp(sec->name, ".shstrtab") ||
-		    !strcmp(sec->name, ".strtab") ||
-		    !strcmp(sec->name, ".symtab") ||
-		    should_include_str_section(sec->name)) {
+		if (is_standard_section(sec) || should_include_str_section(sec->name)) {
 			sec->include = 1;
 			if (sec->secsym)
 				sec->secsym->include = 1;
Signature · 18 lines
2.16.5




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich
Ust-ID: DE 289 237 879
Eingetragen am Amtsgericht Charlottenburg HRB 149173 B



_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw

PATCH 2/6 common: Add is_referenced_section() helper function

This function checks if given section has an included corresponding RELA section and/or any of the symbols table symbols references the section. Section associated symbols are ignored here as there is always such a symbol for every section.

---
 common.c | 22 +++++++++++++++++++++-
 common.h |  1 +
 2 files changed, 22 insertions(+), 1 deletion(-)
common.c +21 -1
index 1fb07cb..c968299 100644
@@ -15,7 +15,7 @@
 
 int is_rela_section(struct section *sec)
 {
-	return (sec->sh.sh_type == SHT_RELA);
+	return sec && (sec->sh.sh_type == SHT_RELA);
 }
 
 int is_local_sym(struct symbol *sym)
@@ -270,6 +270,26 @@ int is_standard_section(struct section *sec)
 	}
 }
 
+int is_referenced_section(const struct section *sec, const struct kpatch_elf *kelf)
+{
+	struct symbol *sym;
+
+	if (is_rela_section(sec->rela) && sec->rela->include)
+		return true;
+
+	list_for_each_entry(sym, &kelf->symbols, list) {
+		if (!sym->include || !sym->sec)
+			continue;
+		/* Ignore section associated sections */
+		if (sym->type == STT_SECTION)
+			continue;
+		if (sym->sec->index == sec->index)
+			return true;
+	}
+
+	return false;
+}
+
 /* returns the offset of the string in the string table */
 int offset_of_string(struct list_head *list, char *name)
 {
common.h +1 -0
index cda690d..affe16b 100644
@@ -151,6 +151,7 @@ int is_text_section(struct section *sec);
 int is_debug_section(struct section *sec);
 int is_rela_section(struct section *sec);
 int is_standard_section(struct section *sec);
+int is_referenced_section(const struct section *sec, const struct kpatch_elf *kelf);
 int is_local_sym(struct symbol *sym);
 
 void rela_insn(struct section *sec, struct rela *rela, struct insn *insn);
Signature · 18 lines
2.16.5




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich
Ust-ID: DE 289 237 879
Eingetragen am Amtsgericht Charlottenburg HRB 149173 B



_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#2 lore raw

Re: [livepatch-build-tools part2 2/6] common: Add is_referenced_section() helper function

Quoted 28 lines · Pawel Wieczorkiewicz

This function checks if given section has an included corresponding RELA section and/or any of the symbols table symbols references the section. Section associated symbols are ignored here as there is always such a symbol for every section.

---
  common.c | 22 +++++++++++++++++++++-
  common.h |  1 +
  2 files changed, 22 insertions(+), 1 deletion(-)
common.c +1 -1
index 1fb07cb..c968299 100644
@@ -15,7 +15,7 @@
 
  int is_rela_section(struct section *sec)
  {
-	return (sec->sh.sh_type == SHT_RELA);
+	return sec && (sec->sh.sh_type == SHT_RELA);
  }
 
  int is_local_sym(struct symbol *sym)

common.c int is_standard_section(struct section *sec) · line 273

Quoted 4 lines
common.c +1 -0
@@ -270,6 +270,26 @@ int is_standard_section(struct section *sec)
  	}
  }
 
+int is_referenced_section(const struct section *sec, const struct kpatch_elf *kelf)

Let's keep to 80 chars where practical (and throughout the rest of the patches).

Quoted 4 lines
common.c +4 -0
+{
+	struct symbol *sym;
+
+	if (is_rela_section(sec->rela) && sec->rela->include)

common.c int is_standard_section(struct section *sec) · line 287

Quoted 10 lines
common.c +10 -0
+		return true;
+
+	list_for_each_entry(sym, &kelf->symbols, list) {
+		if (!sym->include || !sym->sec)
+			continue;
+		/* Ignore section associated sections */
+		if (sym->type == STT_SECTION)
+			continue;
+		if (sym->sec->index == sec->index)
+			return true;

You can simplify this check to sym->sec == sec like the rest of the code does.

Signature · 6 lines
Ross Lagerwall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#3 lore raw

Re: [livepatch-build-tools part2 2/6] common: Add is_referenced_section() helper function

Quoted 25 lines · Ross Lagerwall
Quoted 21 lines · Pawel Wieczorkiewicz

This function checks if given section has an included corresponding RELA section and/or any of the symbols table symbols references the section. Section associated symbols are ignored here as there is always such a symbol for every section.

---
 common.c | 22 +++++++++++++++++++++-
 common.h |  1 +
 2 files changed, 22 insertions(+), 1 deletion(-)
common.c +1 -1
index 1fb07cb..c968299 100644
@@ -15,7 +15,7 @@
   int is_rela_section(struct section *sec)
 {
-	return (sec->sh.sh_type == SHT_RELA);
+	return sec && (sec->sh.sh_type == SHT_RELA);
Quoted 8 lines

common.c int is_standard_section(struct section *sec) · line 272

Quoted 6 lines
common.c +0 -0
 }
   int is_local_sym(struct symbol *sym)
@@ -270,6 +270,26 @@ int is_standard_section(struct section *sec)
 	}
 }
 +int is_referenced_section(const struct section *sec, const struct kpatch_elf *kelf)

Let's keep to 80 chars where practical (and throughout the rest of the patches).

ACK. Will fix. Although, sometimes it makes the code pretty unreadable.

Quoted 17 lines
Quoted 5 lines
common.c +4 -0
+{
+	struct symbol *sym;
+
+	if (is_rela_section(sec->rela) && sec->rela->include)

common.c int is_standard_section(struct section *sec) · line 287

Quoted 10 lines
common.c +10 -0
+		return true;
+
+	list_for_each_entry(sym, &kelf->symbols, list) {
+		if (!sym->include || !sym->sec)
+			continue;
+		/* Ignore section associated sections */
+		if (sym->type == STT_SECTION)
+			continue;
+		if (sym->sec->index == sec->index)
+			return true;

You can simplify this check to sym->sec == sec like the rest of the code does.

Might be my paranoia again (or I am simply missing some obvious assumptions/invariants), but I explicitly wanted to check whether given section/symbol is referenced using other means than addresses.

Quoted 3 lines
Signature · 2 lines
-- 
Ross Lagerwall

Best Regards,
Pawel Wieczorkiewicz

Amazon Development Center Germany GmbH Krausenstr. 38
10117 Berlin
Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich
Ust-ID: DE 289 237 879
Eingetragen am Amtsgericht Charlottenburg HRB 149173 B

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw

PATCH 3/6 create-diff-object: Add is_special_section() helper function

In reply to Pawel Wieczorkiewicz

This function determines, based on the given section name, if the sections belongs to the special sections category.
It treats a name from the special_sections array as a prefix. Any sections whose name starts with special section prefix is considered a special section.

---
 common.c | 23 +++++++++++++++++++++++
 common.h |  1 +
 2 files changed, 24 insertions(+)
common.c +23 -0
index c968299..a2c860b 100644
@@ -290,6 +290,29 @@ int is_referenced_section(const struct section *sec, const struct kpatch_elf *ke
 	return false;
 }
 
+int is_special_section(struct section *sec)
+{
+	static struct special_section_names {
+		const char *name;
+	} names[] = {
+		{ .name		= ".bug_frames"                 },
+		{ .name		= ".fixup"                      },
+		{ .name		= ".ex_table"                   },
+		{ .name		= ".altinstructions"            },
+		{ .name		= ".altinstr_replacement"       },
+		{ .name		= ".livepatch.hooks"            },
+		{ .name         = NULL                          },
+	};
+	struct special_section_names *special;
+
+	for (special = names; special->name; special++) {
+		if (!strncmp(sec->name, special->name, strlen(special->name)))
+			return true;
+	}
+
+	return false;
+}
+
 /* returns the offset of the string in the string table */
 int offset_of_string(struct list_head *list, char *name)
 {
common.h +1 -0
index affe16b..6d38e88 100644
@@ -152,6 +152,7 @@ int is_debug_section(struct section *sec);
 int is_rela_section(struct section *sec);
 int is_standard_section(struct section *sec);
 int is_referenced_section(const struct section *sec, const struct kpatch_elf *kelf);
+int is_special_section(struct section *sec);
 int is_local_sym(struct symbol *sym);
 
 void rela_insn(struct section *sec, struct rela *rela, struct insn *insn);
Signature · 18 lines
2.16.5




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich
Ust-ID: DE 289 237 879
Eingetragen am Amtsgericht Charlottenburg HRB 149173 B



_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#5 lore raw

Re: [livepatch-build-tools part2 3/6] create-diff-object: Add is_special_section() helper function

Quoted 20 lines · Pawel Wieczorkiewicz

This function determines, based on the given section name, if the sections belongs to the special sections category.
It treats a name from the special_sections array as a prefix. Any sections whose name starts with special section prefix is considered a special section.

---
  common.c | 23 +++++++++++++++++++++++
  common.h |  1 +
  2 files changed, 24 insertions(+)
common.c +0 -0
index c968299..a2c860b 100644

common.c int is_referenced_section(const struct section *sec, const struct kpatch_elf *ke · line 293

Quoted 4 lines
common.c +1 -0
@@ -290,6 +290,29 @@ int is_referenced_section(const struct section *sec, const struct kpatch_elf *ke
  	return false;
  }
 
+int is_special_section(struct section *sec)

const for the parameter?

common.c int is_referenced_section(const struct section *sec, const struct kpatch_elf *ke · line 306

Quoted 13 lines
common.c +13 -0
+{
+	static struct special_section_names {
+		const char *name;
+	} names[] = {
+		{ .name		= ".bug_frames"                 },
+		{ .name		= ".fixup"                      },
+		{ .name		= ".ex_table"                   },
+		{ .name		= ".altinstructions"            },
+		{ .name		= ".altinstr_replacement"       },
+		{ .name		= ".livepatch.hooks"            },
+		{ .name         = NULL                          },
+	};
+	struct special_section_names *special;

Wouldn't it be better to reuse the existing special_sections array rather than partially duplicating it here?

common.c int is_referenced_section(const struct section *sec, const struct kpatch_elf *ke · line 310

Quoted 4 lines
common.c +4 -0
+
+	for (special = names; special->name; special++) {
+		if (!strncmp(sec->name, special->name, strlen(special->name)))
+			return true;

This check is not as precise as it could be, since ".altinstructionsfoo" would be considered special when it actually means nothing to this tool.

Signature · 6 lines
Ross Lagerwall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#6 lore raw

Re: [livepatch-build-tools part2 3/6] create-diff-object: Add is_special_section() helper function

Quoted 19 lines · Ross Lagerwall
Quoted 15 lines · Pawel Wieczorkiewicz

This function determines, based on the given section name, if the sections belongs to the special sections category.
It treats a name from the special_sections array as a prefix. Any sections whose name starts with special section prefix is considered a special section.

---
 common.c | 23 +++++++++++++++++++++++
 common.h |  1 +
 2 files changed, 24 insertions(+)
common.c +0 -0
index c968299..a2c860b 100644
Quoted 8 lines

common.c int is_referenced_section(const struct section *sec, const struct kpatch_elf *ke · line 292

Quoted 6 lines
common.c +0 -0
@@ -290,6 +290,29 @@ int is_referenced_section(const struct section *sec, const struct kpatch_elf *ke
 	return false;
 }
 +int is_special_section(struct section *sec)

const for the parameter?

ACK. Will add.

Quoted 16 lines
Quoted 4 lines
common.c +3 -0
+{
+	static struct special_section_names {
+		const char *name;

common.c int is_referenced_section(const struct section *sec, const struct kpatch_elf *ke · line 306

Quoted 10 lines
common.c +10 -0
+	} names[] = {
+		{ .name		= ".bug_frames"                 },
+		{ .name		= ".fixup"                      },
+		{ .name		= ".ex_table"                   },
+		{ .name		= ".altinstructions"            },
+		{ .name		= ".altinstr_replacement"       },
+		{ .name		= ".livepatch.hooks"            },
+		{ .name         = NULL                          },
+	};
+	struct special_section_names *special;

Wouldn't it be better to reuse the existing special_sections array rather than partially duplicating it here?

After looking at this code again, I think you’re right and it would be better. It would require to explicitly call out all sections to be considered special in the special_sections array, but this is actually what I need for further changes anyway.

ACK. Will fix.

Quoted 7 lines

common.c int is_referenced_section(const struct section *sec, const struct kpatch_elf *ke · line 310

Quoted 5 lines
common.c +4 -0
+
+	for (special = names; special->name; special++) {
+		if (!strncmp(sec->name, special->name, strlen(special->name)))
+			return true;

This check is not as precise as it could be, since ".altinstructionsfoo" would be considered special when it actually means nothing to this tool.

Given the above reasoning, that’s right.

ACK. Will fix.

Quoted 3 lines
Signature · 2 lines
-- 
Ross Lagerwall

Best Regards,
Pawel Wieczorkiewicz

Amazon Development Center Germany GmbH Krausenstr. 38
10117 Berlin
Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich
Ust-ID: DE 289 237 879
Eingetragen am Amtsgericht Charlottenburg HRB 149173 B

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#7 lore raw

PATCH 4/6 livepatch-build: detect special section group sizes

In reply to Pawel Wieczorkiewicz

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 structers 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

---
 create-diff-object.c | 60 ++++++++++++++++++++++++++++++++++++++++++++--------
 livepatch-build      | 23 ++++++++++++++++++++
 2 files changed, 74 insertions(+), 9 deletions(-)
create-diff-object.c +51 -9
index 1e6e617..b0b4dcb 100644
@@ -958,12 +958,54 @@ 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 bug_frames_3_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) : 16;
+    }
+
+    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;
+    }
+
+    printf("altinstr_size=%d\n", size);
+    return size;
+}
 
 /*
  * The rela groups in the .fixup section vary in size.  The beginning of each
@@ -1016,15 +1058,15 @@ 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",
livepatch-build +23 -0
index c057fa1..a6cae12 100755
@@ -284,6 +284,29 @@ echo "Output directory: ${OUTPUT}"
 echo "================================================"
 echo
 
+if [ -f "$XENSYMS" ]; then
+    echo "Reading special section data"
+    SPECIAL_VARS=$(readelf -wi "$XENSYMS" |
+               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
+fi
+
 if [ "${SKIP}" != "build" ]; then
     [ -e "${OUTPUT}" ] && die "Output directory exists"
     grep -q 'CONFIG_LIVEPATCH=y' "${CONFIGFILE}" || die "CONFIG_LIVEPATCH must be enabled"
Signature · 18 lines
2.16.5




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich
Ust-ID: DE 289 237 879
Eingetragen am Amtsgericht Charlottenburg HRB 149173 B



_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#8 lore raw

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

Quoted 4 lines · Pawel Wieczorkiewicz

Hard-coding the special section group sizes is unreliable. Instead, determine them dynamically by finding the related struct definitions in the DWARF metadata.

Quoted 8 lines

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 structers 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.

Oh this is so much better than the "solution" we coded.

Thank you!

Ross, will commit to repo unless you have concerns..

Quoted 143 lines

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

---
 create-diff-object.c | 60 ++++++++++++++++++++++++++++++++++++++++++++--------
 livepatch-build      | 23 ++++++++++++++++++++
 2 files changed, 74 insertions(+), 9 deletions(-)
create-diff-object.c +51 -9
index 1e6e617..b0b4dcb 100644
@@ -958,12 +958,54 @@ 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 bug_frames_3_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) : 16;
+    }
+
+    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;
+    }
+
+    printf("altinstr_size=%d\n", size);
+    return size;
+}
 
 /*
  * The rela groups in the .fixup section vary in size.  The beginning of each
@@ -1016,15 +1058,15 @@ 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",
livepatch-build +23 -0
index c057fa1..a6cae12 100755
@@ -284,6 +284,29 @@ echo "Output directory: ${OUTPUT}"
 echo "================================================"
 echo
 
+if [ -f "$XENSYMS" ]; then
+    echo "Reading special section data"
+    SPECIAL_VARS=$(readelf -wi "$XENSYMS" |
+               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
+fi
+
 if [ "${SKIP}" != "build" ]; then
     [ -e "${OUTPUT}" ] && die "Output directory exists"
     grep -q 'CONFIG_LIVEPATCH=y' "${CONFIGFILE}" || die "CONFIG_LIVEPATCH must be enabled"
Signature · 11 lines
2.16.5




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich
Ust-ID: DE 289 237 879
Eingetragen am Amtsgericht Charlottenburg HRB 149173 B

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#9 lore raw

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

Quoted 11 lines · Konrad Rzeszutek Wilk
Quoted 10 lines · Pawel Wieczorkiewicz

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 structers are obtained from xen-syms.

Quoted 8 lines
Quoted 2 lines

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

Oh this is so much better than the "solution" we coded.

Thank you!

Ross, will commit to repo unless you have concerns..

I have reviewed it with a few comments. Note that this is basically the same as Glenn Enright's recent patch ("livepatch-build-tools: Detect special section group sizes") but slightly more complete as it detects sizes for the bug frames too so it should probably be used instead.

Thanks,

Signature · 6 lines
Ross Lagerwall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#10 lore raw

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

Quoted 19 lines · Ross Lagerwall
Quoted 18 lines · Konrad Rzeszutek Wilk
Quoted 4 lines · Pawel Wieczorkiewicz

Hard-coding the special section group sizes is unreliable. Instead, determine them dynamically by finding the related struct definitions in the DWARF metadata.

Quoted 8 lines

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 structers 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.

Oh this is so much better than the "solution" we coded.

Thank you!

Quoted 7 lines
Quoted 1 line

Ross, will commit to repo unless you have concerns..

I have reviewed it with a few comments. Note that this is basically the same as Glenn Enright's recent patch ("livepatch-build-tools: Detect special section group sizes") but slightly more complete as it detects sizes for the bug frames too so it should probably be used instead.

Thanks,

This is a goodness. Glad to see an appropriate patch get in regardless of which patch is used.

Best, Glenn

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#11 lore raw

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

In reply to Pawel Wieczorkiewicz
Quoted 9 lines · Pawel Wieczorkiewicz

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 structers are obtained from xen-syms.

structers -> structures

Quoted 25 lines

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

---
  create-diff-object.c | 60 ++++++++++++++++++++++++++++++++++++++++++++--------
  livepatch-build      | 23 ++++++++++++++++++++
  2 files changed, 74 insertions(+), 9 deletions(-)
create-diff-object.c +0 -3
index 1e6e617..b0b4dcb 100644
@@ -958,12 +958,54 @@ 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; }

create-diff-object.c static void kpatch_mark_constant_labels_same(struct kpatch_elf *kelf) · line 967

Quoted 10 lines
create-diff-object.c +7 -3
-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;

Why did you remove the hard error here from the original kpatch commit? I think a hard error is preferable to guessing.

Quoted 84 lines
create-diff-object.c +44 -3
+    }
+
+    return size;
+}
+
+static int bug_frames_3_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) : 16;
+    }
+
+    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;
+    }
+
+    printf("altinstr_size=%d\n", size);
+    return size;
+}
 
  /*
   * The rela groups in the .fixup section vary in size.  The beginning of each
@@ -1016,15 +1058,15 @@ 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",
livepatch-build +13 -0
index c057fa1..a6cae12 100755
@@ -284,6 +284,29 @@ echo "Output directory: ${OUTPUT}"
  echo "================================================"
  echo
 
+if [ -f "$XENSYMS" ]; then
+    echo "Reading special section data"
+    SPECIAL_VARS=$(readelf -wi "$XENSYMS" |
+               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"

livepatch-build echo "Output directory: ${OUTPUT}" · line 309

Quoted 10 lines
livepatch-build +10 -0
+    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
+fi
+

If this hunk is moved after the call to build_full(), then it can be run directly on the xen-syms that has just been built which would allow dropping if [ -f "$XENSYMS" ]... and guaranteeing that *_STRUCT_SIZE are always set.

Signature · 6 lines
Ross Lagerwall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#12 lore raw

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

Quoted 149 lines · Ross Lagerwall
Quoted 9 lines · Pawel Wieczorkiewicz

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 structers are obtained from xen-syms.

structers -> structures

Quoted 36 lines

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

---
  create-diff-object.c | 60
++++++++++++++++++++++++++++++++++++++++++++--------
  livepatch-build      | 23 ++++++++++++++++++++

  2 files changed, 74 insertions(+), 9 deletions(-)
create-diff-object.c +0 -5
index 1e6e617..b0b4dcb 100644
@@ -958,12 +958,54 @@ 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; }

create-diff-object.c static void kpatch_mark_constant_labels_same(struct kpatch_elf *kelf) · line 967

Quoted 7 lines
create-diff-object.c +7 -0
+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;

Why did you remove the hard error here from the original kpatch commit? I think a hard error is preferable to guessing.

Quoted 90 lines

+    }

create-diff-object.c +29 -0
+
+    return size;
+}
+
+static int bug_frames_3_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) : 16;
+    }
+
+    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)

create-diff-object.c +11 -0
+{
+    static int size = 0;
+    char *str;
+    if (!size) {
+        str = getenv("ALT_STRUCT_SIZE");
+        size = str ? atoi(str) : 12;
+    }
+
+    printf("altinstr_size=%d\n", size);
+    return size;
+}

  /*
   * The rela groups in the .fixup section vary in size.  The beginning of each

create-diff-object.c +3 -3
@@ -1016,15 +1058,15 @@ 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",

livepatch-build +0 -0
index c057fa1..a6cae12 100755
@@ -284,6 +284,29 @@ echo "Output directory: ${OUTPUT}"

  echo "================================================" echo
+if [ -f "$XENSYMS" ]; then
+    echo "Reading special section data"
+    SPECIAL_VARS=$(readelf -wi "$XENSYMS" |
+               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

Quoted 9 lines

livepatch-build

Quoted 6 lines

+        if [[ ! $i -gt 0 ]] || [[ ! $i -le 16 ]]; then +            die "invalid special struct size $i" +        fi
+    done

livepatch-build +2 -0
+fi
+

If this hunk is moved after the call to build_full(), then it can be run directly on the xen-syms that has just been built which would allow dropping if [ -f "$XENSYMS" ]... and guaranteeing that *_STRUCT_SIZE are always set.

One more thing, previously:

bug_frames_0_group_size == 8
bug_frames_1_group_size == 8
bug_frames_2_group_size == 8
bug_frames_3_group_size == 16

And now:

bug_frames_0_group_size == BUG_STRUCT_SIZE
bug_frames_1_group_size == BUG_STRUCT_SIZE
bug_frames_2_group_size == BUG_STRUCT_SIZE
bug_frames_3_group_size == BUG_STRUCT_SIZE

This seems wrong to me. When reading special section data, should you detect e.g. BUG0_STRUCT_SIZE, BUG1_STRUCT_SIZE, ... ?

Signature · 6 lines
Ross Lagerwall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#13 lore raw

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

Quoted 9 lines · Ross Lagerwall
Quoted 5 lines · Ross Lagerwall
Quoted 4 lines · Pawel Wieczorkiewicz

Hard-coding the special section group sizes is unreliable. Instead, determine them dynamically by finding the related struct definitions in the DWARF metadata.

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines

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 structers are obtained from xen-syms.

structers -> structures

Thanks, will fix.

Quoted 27 lines
Quoted 27 lines
Quoted 27 lines

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

---
  create-diff-object.c | 60 ++++++++++++++++++++++++++++++++++++++++++++--------
  livepatch-build      | 23 ++++++++++++++++++++
  2 files changed, 74 insertions(+), 9 deletions(-)
create-diff-object.c +0 -6
index 1e6e617..b0b4dcb 100644
@@ -958,12 +958,54 @@ 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; }
Quoted 8 lines
Quoted 8 lines

create-diff-object.c static void kpatch_mark_constant_labels_same(struct kpatch_elf *kelf) · line 967

Quoted 7 lines
create-diff-object.c +7 -0
+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;

Why did you remove the hard error here from the original kpatch commit? I think a hard error is preferable to guessing.

Previously the sizes were hard-coded. I prefer to use them directly over failing hard here. If we could not come up with a sane defaults, than failing hard would be the only option. Anyway, I am cool to go either way upon your good judgment.

Quoted 101 lines
Quoted 85 lines
Quoted 85 lines
create-diff-object.c +44 -3
+    }
+
+    return size;
+}
+
+static int bug_frames_3_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) : 16;
+    }
+
+    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;
+    }
+
+    printf("altinstr_size=%d\n", size);
+    return size;
+}
  /*
   * The rela groups in the .fixup section vary in size.  The beginning of each
@@ -1016,15 +1058,15 @@ 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",
livepatch-build +16 -0
index c057fa1..a6cae12 100755
@@ -284,6 +284,29 @@ echo "Output directory: ${OUTPUT}"
  echo "================================================"
  echo
+if [ -f "$XENSYMS" ]; then
+    echo "Reading special section data"
+    SPECIAL_VARS=$(readelf -wi "$XENSYMS" |
+               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
Quoted 8 lines

livepatch-build echo "Output directory: ${OUTPUT}" · line 309

Quoted 7 lines
livepatch-build +7 -0
+    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
+fi
+

If this hunk is moved after the call to build_full(), then it can be run directly on the xen-syms that has just been built which would allow dropping if [ -f "$XENSYMS" ]... and guaranteeing that *_STRUCT_SIZE are always set.

One more thing, previously:

bug_frames_0_group_size == 8
bug_frames_1_group_size == 8
bug_frames_2_group_size == 8
bug_frames_3_group_size == 16

Quoted 9 lines

And now:

bug_frames_0_group_size == BUG_STRUCT_SIZE
bug_frames_1_group_size == BUG_STRUCT_SIZE
bug_frames_2_group_size == BUG_STRUCT_SIZE
bug_frames_3_group_size == BUG_STRUCT_SIZE

This seems wrong to me. When reading special section data, should you detect e.g. BUG0_STRUCT_SIZE, BUG1_STRUCT_SIZE, … ?

There is only one struct bug_frame definition in Xen:
Using pahole:

struct bug_frame {

        unsigned char              ud2[2];               /*     0     2 */
        unsigned char              ret;                  /*     2     1 */
        short unsigned int         id;                   /*     3     2 */
        /* size: 5, cachelines: 1, members: 3 */
        /* last cacheline: 5 bytes */
};

It’s size is 5, so fits into 8 bytes.

Definitions for all the 0, 1, 2, 3 groups use the same struct bug_frame:
Example grep:
include/asm-x86/bug.h:extern const struct bug_frame __start_bug_frames[],

include/asm-x86/bug.h:                              __stop_bug_frames_0[],
include/asm-x86/bug.h:                              __stop_bug_frames_1[],
include/asm-x86/bug.h:                              __stop_bug_frames_2[],
include/asm-x86/bug.h:                              __stop_bug_frames_3[];

So, the default group size of 16 bytes does not seem right.

Example grep:

$ objdump -g xen-syms|grep -A3 DW_TAG_structure_type|grep -A1 bug_frame|cut -f2- -d'>'|sort -u
--
   DW_AT_byte_size   : 8
   DW_AT_name        : (indirect string, offset: 0x2556): bug_frame
Quoted 2 lines
Signature · 1 line
Ross Lagerwall

Best Regards,
Pawel Wieczorkiewicz

Amazon Development Center Germany GmbH Krausenstr. 38
10117 Berlin
Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich
Ust-ID: DE 289 237 879
Eingetragen am Amtsgericht Charlottenburg HRB 149173 B

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#14 lore raw

PATCH 5/6 create-diff-object: Add new entries to special sections array

In reply to Pawel Wieczorkiewicz

Handle .livepatch.hooks* and .altinstr_replacement sections as the special sections with assigned group_size resolution function. By default each .livepatch.hooks* sections' entry is 8 bytes long (a pointer). The .altinstr_replacement section follows the .altinstructions section settings.

Allow to specify different .livepatch.hooks* section entry size using shell environment variable HOOK_STRUCT_SIZE.

Cleanup an incorrect indentation around group_size resulution functions.

---
 create-diff-object.c | 87 ++++++++++++++++++++++++++++++++--------------------
 1 file changed, 54 insertions(+), 33 deletions(-)
create-diff-object.c +54 -33
index b0b4dcb..f6060cd 100644
@@ -960,51 +960,64 @@ static void kpatch_mark_constant_labels_same(struct kpatch_elf *kelf)
 
 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 size = 0;
+	char *str;
+	if (!size) {
+		str = getenv("BUG_STRUCT_SIZE");
+		size = str ? atoi(str) : 8;
+	}
+
+	return size;
 }
 
 static int bug_frames_3_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) : 16;
-    }
-
-    return size;
+	static int size = 0;
+	char *str;
+	if (!size) {
+		str = getenv("BUG_STRUCT_SIZE");
+		size = str ? atoi(str) : 16;
+	}
+
+	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 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;
-    }
-
-    printf("altinstr_size=%d\n", size);
-    return size;
+	static int size = 0;
+	char *str;
+	if (!size) {
+		str = getenv("ALT_STRUCT_SIZE");
+		size = str ? atoi(str) : 12;
+	}
+
+	printf("altinstr_size=%d\n", size);
+	return size;
+}
+
+static int livepatch_hooks_group_size(struct kpatch_elf *kelf, int offset)
+{
+	static int size = 0;
+	char *str;
+	if (!size) {
+		str = getenv("HOOK_STRUCT_SIZE");
+		size = str ? atoi(str) : 8;
+	}
+
+	printf("livepatch_hooks_size=%d\n", size);
+	return size;
 }
 
 /*
@@ -1084,6 +1097,14 @@ static struct special_section special_sections[] = {
 		.name		= ".altinstructions",
 		.group_size	= altinstructions_group_size,
 	},
+	{
+		.name		= ".altinstr_replacement",
+		.group_size	= altinstructions_group_size,
+	},
+	{
+		.name		= ".livepatch.hooks",
+		.group_size	= livepatch_hooks_group_size,
+	},
 	{},
 };
 
Signature · 18 lines
2.16.5




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich
Ust-ID: DE 289 237 879
Eingetragen am Amtsgericht Charlottenburg HRB 149173 B



_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#15 lore raw

Re: [livepatch-build-tools part2 5/6] create-diff-object: Add new entries to special sections array

create-diff-object.c static void kpatch_mark_constant_labels_same(struct kpatch_elf *kelf)

Quoted 24 lines · Pawel Wieczorkiewicz

Handle .livepatch.hooks* and .altinstr_replacement sections as the special sections with assigned group_size resolution function. By default each .livepatch.hooks* sections' entry is 8 bytes long (a pointer). The .altinstr_replacement section follows the .altinstructions section settings.

Allow to specify different .livepatch.hooks* section entry size using shell environment variable HOOK_STRUCT_SIZE.

Cleanup an incorrect indentation around group_size resulution functions.

---
  create-diff-object.c | 87 ++++++++++++++++++++++++++++++++--------------------
  1 file changed, 54 insertions(+), 33 deletions(-)
create-diff-object.c +0 -0
index b0b4dcb..f6060cd 100644
@@ -960,51 +960,64 @@ static void kpatch_mark_constant_labels_same(struct kpatch_elf *kelf)

Fix the indentation in the patch which introduces the problem rather than fixing it in a subsequent patch.

Quoted 83 lines
create-diff-object.c +35 -33
  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 size = 0;
+	char *str;
+	if (!size) {
+		str = getenv("BUG_STRUCT_SIZE");
+		size = str ? atoi(str) : 8;
+	}
+
+	return size;
  }
 
  static int bug_frames_3_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) : 16;
-    }
-
-    return size;
+	static int size = 0;
+	char *str;
+	if (!size) {
+		str = getenv("BUG_STRUCT_SIZE");
+		size = str ? atoi(str) : 16;
+	}
+
+	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 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;
-    }
-
-    printf("altinstr_size=%d\n", size);
-    return size;
+	static int size = 0;
+	char *str;
+	if (!size) {
+		str = getenv("ALT_STRUCT_SIZE");
+		size = str ? atoi(str) : 12;
+	}
+
+	printf("altinstr_size=%d\n", size);
+	return size;
+}
+

create-diff-object.c static void kpatch_mark_constant_labels_same(struct kpatch_elf *kelf) · line 1019

Quoted 10 lines
create-diff-object.c +10 -0
+static int livepatch_hooks_group_size(struct kpatch_elf *kelf, int offset)
+{
+	static int size = 0;
+	char *str;
+	if (!size) {
+		str = getenv("HOOK_STRUCT_SIZE");
+		size = str ? atoi(str) : 8;
+	}
+
+	printf("livepatch_hooks_size=%d\n", size);

If you want to keep this debugging output, rather use log_debug().

Quoted 10 lines
create-diff-object.c +2 -0
+	return size;
  }
 
  /*
@@ -1084,6 +1097,14 @@ static struct special_section special_sections[] = {
  		.name		= ".altinstructions",
  		.group_size	= altinstructions_group_size,
  	},
+	{

create-diff-object.c static struct special_section special_sections[] = { · line 1110

Quoted 10 lines
create-diff-object.c +7 -0
+		.name		= ".altinstr_replacement",
+		.group_size	= altinstructions_group_size,
+	},
+	{
+		.name		= ".livepatch.hooks",
+		.group_size	= livepatch_hooks_group_size,
+	},
  	{},
  };
 

I don't understand the point of this change.

IIUC unlike .altinstructions, the .altinstr_replacement section does not have a fixed group size, so this change would not work other than by luck. If the goal was to prune bits of the .altinstr_replacement table that do not need to be included, you need to update the code in kpatch_process_special_sections() which has special handling for .altinstr_replacement. It needs to parse the updated, pruned .altinstructions section and regenerate .altinstr_replacement, including only what is needed.

I don't know why .livepatch.hooks is included in this list either since all hooks are always included and there is nothing to regenerate/prune.

Regards,

Signature · 6 lines
Ross Lagerwall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
author Reviewed-by Andra-Irina Paraschiv Reviewed-by Bjoern Doebel Reviewed-by Norbert Manthey
#16 lore raw

Re: [livepatch-build-tools part2 5/6] create-diff-object: Add new entries to special sections array

On 29. Apr 2019, at 17:47, Ross Lagerwall <[email protected]<mailto:[email protected]>> wrote:

On 4/16/19 1:07 PM, Pawel Wieczorkiewicz wrote:
Handle .livepatch.hooks* and .altinstr_replacement sections as the special sections with assigned group_size resolution function. By default each .livepatch.hooks* sections' entry is 8 bytes long (a pointer). The .altinstr_replacement section follows the .altinstructions section settings.
Allow to specify different .livepatch.hooks* section entry size using shell environment variable HOOK_STRUCT_SIZE.
Cleanup an incorrect indentation around group_size resulution functions.

---
 create-diff-object.c | 87 ++++++++++++++++++++++++++++++++--------------------
 1 file changed, 54 insertions(+), 33 deletions(-)
create-diff-object.c +0 -0
index b0b4dcb..f6060cd 100644
@@ -960,51 +960,64 @@ static void kpatch_mark_constant_labels_same(struct kpatch_elf *kelf)
 

Fix the indentation in the patch which introduces the problem rather than fixing it in a subsequent patch.

Oh, certainly. I forgot to update the commits involved properly.

ACK. Will fix.

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 size = 0;
+ char *str;
+ if (!size) {
+ str = getenv("BUG_STRUCT_SIZE");
+ size = str ? atoi(str) : 8;
+ }
+
+ return size;
}
static int bug_frames_3_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) : 16;
- }
-
- return size;
+ static int size = 0;
+ char *str;
+ if (!size) {
+ str = getenv("BUG_STRUCT_SIZE");
+ size = str ? atoi(str) : 16;
+ }
+
+ 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 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;
- }
-
- printf("altinstr_size=%d\n", size);
- return size;
+ static int size = 0;
+ char *str;
+ if (!size) {
+ str = getenv("ALT_STRUCT_SIZE");
+ size = str ? atoi(str) : 12;
+ }
+
+ printf("altinstr_size=%d\n", size);
+ return size;
+}
+
+static int livepatch_hooks_group_size(struct kpatch_elf *kelf, int offset) +{
+ static int size = 0;
+ char *str;
+ if (!size) {
+ str = getenv("HOOK_STRUCT_SIZE");
+ size = str ? atoi(str) : 8;
+ }
+
+ printf("livepatch_hooks_size=%d\n", size);

If you want to keep this debugging output, rather use log_debug().

ACK. Will fix.

+ return size;
}
/*
@@ -1084,6 +1097,14 @@ static struct special_section special_sections[] = {
.name = ".altinstructions",
.group_size = altinstructions_group_size,
},
+ {
+ .name = ".altinstr_replacement",
+ .group_size = altinstructions_group_size,
+ },
+ {
+ .name = ".livepatch.hooks",
+ .group_size = livepatch_hooks_group_size,
+ },
{},
};

I don't understand the point of this change.

IIUC unlike .altinstructions, the .altinstr_replacement section does not have a fixed group size, so this change would not work other than by luck. If

That’s true. The .altinstr_replacement group_size should be 0 (aka undefined).

ACK. Will fix.

the goal was to prune bits of the .altinstr_replacement table that do not need to be included, you need to update the code in kpatch_process_special_sections() which has special handling for .altinstr_replacement. It needs to parse the updated, pruned .altinstructions section and regenerate .altinstr_replacement, including only what is needed.

No this was not the goal. I am explaining it below.

I don't know why .livepatch.hooks is included in this list either since all hooks are always included and there is nothing to regenerate/prune.

As discussed when reviewing [1], I need a function telling which section is special. By special I mean:
a) should not be a subject of extra validation (See [2])
b) should not be a subject of STN_UNDEF purging (See [3])

Thus, I want to explicitly call out all "special" sections by their name and specify their group_size whenever applicable (I did it incorrectly for .altinstr_replacement, as you have spotted - thanks!) in the common array and use it with the helper function or for obtaining a group_size.

[1] [livepatch-build-tools part2 3/6] create-diff-object: Add is_special_section() helper function
[2] [livepatch-build-tools part3 2/3] create-diff-object: Extend patchability verification: STN_UNDEF
[3] [livepatch-build-tools part3 3/3] create-diff-object: Strip all undefined entires of known size

Regards,
--
Ross Lagerwall

Best Regards,
Pawel Wieczorkiewicz

Amazon Development Center Germany GmbH Krausenstr. 38
10117 Berlin
Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich
Ust-ID: DE 289 237 879
Eingetragen am Amtsgericht Charlottenburg HRB 149173 B

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#17 lore raw

PATCH 6/6 create-diff-object: Do not include all .rodata sections

In reply to Pawel Wieczorkiewicz

Starting with the "2af6f1aa6233 Fix patch creation with GCC 6.1+" commit all .rodata sections are included by default (regardless of whether they are needed or not).
During stacked hotpatch builds it leads to unnecessary duplication of the .rodata sections as each and every consecutive hotpatch contains all the .rodata section of previous hotpatches.

To prevent this situation, mark the .rodata section for inclusion only if they are referenced by any of the current hotpatch symbols (or a corresponding RELA section).

Extend patchability verification to detect all non-standard, non-rela, non-debug and non-special sections that are not referenced by any of the symbols or RELA sections.

---
 create-diff-object.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)
create-diff-object.c +26 -1
index f6060cd..f7eb421 100644
@@ -1341,7 +1341,7 @@ static void kpatch_include_standard_elements(struct kpatch_elf *kelf)
 
 	list_for_each_entry(sec, &kelf->sections, list) {
 		/* include these sections even if they haven't changed */
-		if (is_standard_section(sec) || should_include_str_section(sec->name)) {
+		if (is_standard_section(sec)) {
 			sec->include = 1;
 			if (sec->secsym)
 				sec->secsym->include = 1;
@@ -1352,6 +1352,19 @@ static void kpatch_include_standard_elements(struct kpatch_elf *kelf)
 	list_entry(kelf->symbols.next, struct symbol, list)->include = 1;
 }
 
+static void kpatch_include_standard_string_elements(struct kpatch_elf *kelf)
+{
+	struct section *sec;
+
+	list_for_each_entry(sec, &kelf->sections, list) {
+		if (should_include_str_section(sec->name) && is_referenced_section(sec, kelf)) {
+			sec->include = 1;
+			if (sec->secsym)
+				sec->secsym->include = 1;
+		}
+	}
+}
+
 #define inc_printf(fmt, ...) \
 	log_debug("%*s" fmt, recurselevel, "", ##__VA_ARGS__);
 
@@ -1531,6 +1544,16 @@ static void kpatch_verify_patchability(struct kpatch_elf *kelf)
 			errs++;
 		}
 
+		if (sec->include) {
+			if (!is_standard_section(sec) && !is_rela_section(sec) &&
+			    !is_debug_section(sec) && !is_special_section(sec)) {
+				if (!is_referenced_section(sec, kelf)) {
+					log_normal("section %s included, but not referenced\n", sec->name);
+					errs++;
+				}
+			}
+		}
+
 		/*
 		 * ensure we aren't including .data.* or .bss.*
 		 * (.data.unlikely is ok b/c it only has __warned vars)
@@ -2062,6 +2085,8 @@ int main(int argc, char *argv[])
 	kpatch_include_debug_sections(kelf_patched);
 	log_debug("Include hook elements\n");
 	kpatch_include_hook_elements(kelf_patched);
+	log_debug("Include standard string elements\n");
+	kpatch_include_standard_string_elements(kelf_patched);
 	log_debug("Include new globals\n");
 	new_globals_exist = kpatch_include_new_globals(kelf_patched);
 	log_debug("new_globals_exist = %d\n", new_globals_exist);
Signature · 18 lines
2.16.5




Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich
Ust-ID: DE 289 237 879
Eingetragen am Amtsgericht Charlottenburg HRB 149173 B



_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#18 lore raw

Re: [livepatch-build-tools part2 6/6] create-diff-object: Do not include all .rodata sections

Quoted 6 lines · Pawel Wieczorkiewicz

Starting with the "2af6f1aa6233 Fix patch creation with GCC 6.1+" commit all .rodata sections are included by default (regardless of whether they are needed or not).
During stacked hotpatch builds it leads to unnecessary duplication of the .rodata sections as each and every consecutive hotpatch contains all the .rodata section of previous hotpatches.

This commit message is a bit confusing.

1) All of this only applies to .rodata.str* sections. Other .rodata sections are handled separately.

2) The above commit _did not_ introduce the problem. Previous versions of GCC did not split .rodata.str sections by function which meant that the entire section was always included. The commit fixed patch creation and _maintained_ the existing behaviour for GCC 6.1+ by including all the .rodata.str* sections. This patch now includes only what is needed (but it should be noted that this would likely not be useful on older versions of GCC since they don't split .rodata.str properly).

Quoted 36 lines

To prevent this situation, mark the .rodata section for inclusion only if they are referenced by any of the current hotpatch symbols (or a corresponding RELA section).

Extend patchability verification to detect all non-standard, non-rela, non-debug and non-special sections that are not referenced by any of the symbols or RELA sections.

---
  create-diff-object.c | 27 ++++++++++++++++++++++++++-
  1 file changed, 26 insertions(+), 1 deletion(-)
create-diff-object.c +3 -1
index f6060cd..f7eb421 100644
@@ -1341,7 +1341,7 @@ static void kpatch_include_standard_elements(struct kpatch_elf *kelf)
 
  	list_for_each_entry(sec, &kelf->sections, list) {
  		/* include these sections even if they haven't changed */
-		if (is_standard_section(sec) || should_include_str_section(sec->name)) {
+		if (is_standard_section(sec)) {
  			sec->include = 1;
  			if (sec->secsym)
  				sec->secsym->include = 1;
@@ -1352,6 +1352,19 @@ static void kpatch_include_standard_elements(struct kpatch_elf *kelf)
  	list_entry(kelf->symbols.next, struct symbol, list)->include = 1;
  }
 
+static void kpatch_include_standard_string_elements(struct kpatch_elf *kelf)
+{

create-diff-object.c static void kpatch_include_standard_elements(struct kpatch_elf *kelf) · line 1366

Quoted 10 lines
create-diff-object.c +10 -0
+	struct section *sec;
+
+	list_for_each_entry(sec, &kelf->sections, list) {
+		if (should_include_str_section(sec->name) && is_referenced_section(sec, kelf)) {
+			sec->include = 1;
+			if (sec->secsym)
+				sec->secsym->include = 1;
+		}
+	}
+}

I think it would be simpler to just amend the previous function rather than introducing a new one. E.g.

... || (should_include_str_section(sec->name) && is_referenced_section(sec, kelf))

Regards,

Signature · 6 lines
Ross Lagerwall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#19 lore raw

Re: [livepatch-build-tools part2 6/6] create-diff-object: Do not include all .rodata sections

Quoted 11 lines · Ross Lagerwall
Quoted 6 lines · Pawel Wieczorkiewicz

Starting with the "2af6f1aa6233 Fix patch creation with GCC 6.1+" commit all .rodata sections are included by default (regardless of whether they are needed or not).
During stacked hotpatch builds it leads to unnecessary duplication of the .rodata sections as each and every consecutive hotpatch contains all the .rodata section of previous hotpatches.

Quoted 3 lines

This commit message is a bit confusing.

1) All of this only applies to .rodata.str* sections. Other .rodata sections are handled separately.

Yes, that’s right. I will make the commit message precise.

ACK. Will fix.

Quoted 2 lines

2) The above commit _did not_ introduce the problem. Previous versions of GCC did not split .rodata.str sections by function which meant that the entire section was always included. The commit fixed patch creation and _maintained_ the existing behaviour for GCC 6.1+ by including all the

I see, thanks for the clarification. I will update the wording to avoid confusion and incorrect attribution here.

Quoted 1 line

.rodata.str* sections. This patch now includes only what is needed (but it should be noted that this would likely not be useful on older versions of GCC since they don't split .rodata.str properly).

OK, I suppose users of older versions of GCC have to accept the fact.

Quoted 44 lines
Quoted 31 lines

To prevent this situation, mark the .rodata section for inclusion only if they are referenced by any of the current hotpatch symbols (or a corresponding RELA section).
Extend patchability verification to detect all non-standard, non-rela, non-debug and non-special sections that are not referenced by any of the symbols or RELA sections.

---
 create-diff-object.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)
create-diff-object.c +2 -1
index f6060cd..f7eb421 100644
@@ -1341,7 +1341,7 @@ static void kpatch_include_standard_elements(struct kpatch_elf *kelf)
   	list_for_each_entry(sec, &kelf->sections, list) {
 		/* include these sections even if they haven't changed */
-		if (is_standard_section(sec) || should_include_str_section(sec->name)) {
+		if (is_standard_section(sec)) {
 			sec->include = 1;
 			if (sec->secsym)
 				sec->secsym->include = 1;
@@ -1352,6 +1352,19 @@ static void kpatch_include_standard_elements(struct kpatch_elf *kelf)
 	list_entry(kelf->symbols.next, struct symbol, list)->include = 1;
 }
 +static void kpatch_include_standard_string_elements(struct kpatch_elf *kelf)
+{

create-diff-object.c static void kpatch_include_standard_elements(struct kpatch_elf *kelf) · line 1365

Quoted 10 lines
create-diff-object.c +10 -0
+	struct section *sec;
+
+	list_for_each_entry(sec, &kelf->sections, list) {
+		if (should_include_str_section(sec->name) && is_referenced_section(sec, kelf)) {
+			sec->include = 1;
+			if (sec->secsym)
+				sec->secsym->include = 1;
+		}
+	}
+}

I think it would be simpler to just amend the previous function rather than introducing a new one. E.g.

Quoted 1 line

... || (should_include_str_section(sec->name) && is_referenced_section(sec, kelf))

IIRC, it is unfortunately too early to do that from within kpatch_include_standard_elements(). I want to call the kpatch_include_standard_string_elements() after the following functions are called:
- kpatch_include_changed_functions()
- kpatch_include_debug_sections()
- kpatch_include_hook_elements()
because they may affect is_referenced_section() result.

Quoted 4 lines

Regards,

Signature · 1 line
Ross Lagerwall

Best Regards,
Pawel Wieczorkiewicz

Amazon Development Center Germany GmbH Krausenstr. 38
10117 Berlin
Geschaeftsfuehrer: Christian Schlaeger, Ralf Herbrich
Ust-ID: DE 289 237 879
Eingetragen am Amtsgericht Charlottenburg HRB 149173 B

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#20 lore raw
In reply to Pawel Wieczorkiewicz
Quoted 61 lines · Pawel Wieczorkiewicz

Detect standard (always to be included) sections via their section header type. The standard sections: ".shstrtab", ".symtab", ".strtab" are either of type SHT_SYMTAB or SHT_STRTAB.

---
  common.c             | 12 ++++++++++++
  common.h             |  1 +
  create-diff-object.c |  5 +----
  3 files changed, 14 insertions(+), 4 deletions(-)
common.c +12 -0
index bc63955..1fb07cb 100644
@@ -5,6 +5,7 @@
  #include <sys/stat.h>
  #include <fcntl.h>
  #include <unistd.h>
+#include <stdbool.h>
  #include <gelf.h>
 
  #include "list.h"
@@ -258,6 +259,17 @@ int is_debug_section(struct section *sec)
  	return !strncmp(name, ".debug_", 7);
  }
 
+int is_standard_section(struct section *sec)
+{
+	switch (sec->sh.sh_type) {
+	case SHT_STRTAB:
+	case SHT_SYMTAB:
+		return true;
+	default:
+		return false;
+	}
+}
+
  /* returns the offset of the string in the string table */
  int offset_of_string(struct list_head *list, char *name)
  {
common.h +1 -0
index 7599fe7..cda690d 100644
@@ -150,6 +150,7 @@ struct symbol *find_symbol_by_name(struct list_head *list, const char *name);
  int is_text_section(struct section *sec);
  int is_debug_section(struct section *sec);
  int is_rela_section(struct section *sec);
+int is_standard_section(struct section *sec);
  int is_local_sym(struct symbol *sym);
 
  void rela_insn(struct section *sec, struct rela *rela, struct insn *insn);
create-diff-object.c +0 -0
index 82f777e..1e6e617 100644

create-diff-object.c static void kpatch_include_standard_elements(struct kpatch_elf *kelf) · line 1281

Quoted 8 lines
create-diff-object.c +1 -4
@@ -1278,10 +1278,7 @@ static void kpatch_include_standard_elements(struct kpatch_elf *kelf)
 
  	list_for_each_entry(sec, &kelf->sections, list) {
  		/* include these sections even if they haven't changed */
-		if (!strcmp(sec->name, ".shstrtab") ||
-		    !strcmp(sec->name, ".strtab") ||
-		    !strcmp(sec->name, ".symtab") ||
-		    should_include_str_section(sec->name)) {
+		if (is_standard_section(sec) || should_include_str_section(sec->name)) {

Let's keep lines to 80 chars where feasible (1 tab == 8 spaces). Otherwise LGTM.

Signature · 6 lines
Ross Lagerwall

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel