[RFC,v2,3/3] kernel.bbclass: use common strip()

Message ID 20220111235840.81471-4-saul.wold@windriver.com
State Accepted, archived
Commit e8d9caede5f08154ca615fdaba676b7a4ae05b01
Headers show
Series Extend create-spdx to build kernel spdx info | expand

Commit Message

Saul Wold Jan. 11, 2022, 11:58 p.m. UTC
Re-use the runstrip() code from oe.packaging, for the kernel
stripping process. Since runstrip() is python the kernel do_strip()
need to be converted to Python also. The stripped kernel image
will be used for deployment in do_deploy(). This will allow the
package.bbclass to split an unstripped kernel binary for the debug
information and extended packagedata. The extended package data is
used by create-spdx.

Signed-off-by: Saul Wold <saul.wold@windriver.com>
---
 meta/classes/kernel.bbclass | 44 ++++++++++++++++---------------------
 1 file changed, 19 insertions(+), 25 deletions(-)

Comments

Bruce Ashfield Jan. 12, 2022, 2:30 p.m. UTC | #1
On Tue, Jan 11, 2022 at 6:59 PM Saul Wold <saul.wold@windriver.com> wrote:
>
> Re-use the runstrip() code from oe.packaging, for the kernel
> stripping process. Since runstrip() is python the kernel do_strip()
> need to be converted to Python also. The stripped kernel image
> will be used for deployment in do_deploy(). This will allow the
> package.bbclass to split an unstripped kernel binary for the debug
> information and extended packagedata. The extended package data is
> used by create-spdx.
>
> Signed-off-by: Saul Wold <saul.wold@windriver.com>
> ---
>  meta/classes/kernel.bbclass | 44 ++++++++++++++++---------------------
>  1 file changed, 19 insertions(+), 25 deletions(-)
>
> diff --git a/meta/classes/kernel.bbclass b/meta/classes/kernel.bbclass
> index 7685c6921fa..b2f9e3c8071 100644
> --- a/meta/classes/kernel.bbclass
> +++ b/meta/classes/kernel.bbclass
> @@ -700,30 +700,19 @@ do_kernel_link_images() {
>  }
>  addtask kernel_link_images after do_compile before do_strip
>
> -do_strip() {
> -       if [ -n "${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}" ]; then
> -               if ! (echo "${KERNEL_IMAGETYPES}" | grep -wq "vmlinux"); then
> -                       bbwarn "image type(s) will not be stripped (not supported): ${KERNEL_IMAGETYPES}"
> -                       return
> -               fi
> -
> -               cd ${B}
> -               headers=`"$CROSS_COMPILE"readelf -S ${KERNEL_OUTPUT_DIR}/vmlinux | \
> -                         grep "^ \{1,\}\[[0-9 ]\{1,\}\] [^ ]" | \
> -                         sed "s/^ \{1,\}\[[0-9 ]\{1,\}\] //" | \
> -                         gawk '{print $1}'`
> -
> -               for str in ${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}; do {
> -                       if ! (echo "$headers" | grep -q "^$str$"); then
> -                               bbwarn "Section not found: $str";
> -                       fi

We are losing this warning in the relocated / common routine. It looks
like something we could continue to detect and warn on, no ?

> -
> -                       "$CROSS_COMPILE"strip -s -R $str ${KERNEL_OUTPUT_DIR}/vmlinux
> -               }; done
> -
> -               bbnote "KERNEL_IMAGE_STRIP_EXTRA_SECTIONS is set, stripping sections:" \
> -                       "${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}"
> -       fi;
> +python do_strip() {
> +    import shutil
> +
> +    strip = d.getVar('STRIP')
> +    extra_sections = d.getVar('KERNEL_IMAGE_STRIP_EXTRA_SECTIONS')
> +    kernel_image = d.getVar('B') + "/" + d.getVar('KERNEL_OUTPUT_DIR') + "/vmlinux"
> +
> +    if (extra_sections is not None and kernel_image.find('boot/vmlinux') != -1):

Minor 'nit .. I'd probably just write that as: if extra_sections
(versus the is not None) .. but maybe you have it that way due to that
check not working.

> +        kernel_image_stripped = kernel_image + ".stripped"
> +        shutil.copy2(kernel_image, kernel_image_stripped)
> +        oe.package.runstrip((kernel_image_stripped, 8, strip, extra_sections))
> +        bb.note ("KERNEL_IMAGE_STRIP_EXTRA_SECTIONS is set, stripping sections: " + \
> +            extra_sections)

In theory, we could move the debug log to the common routine when it
detects the extra section argument

>  }
>  do_strip[dirs] = "${B}"
>
> @@ -768,7 +757,12 @@ kernel_do_deploy() {
>
>         for imageType in ${KERNEL_IMAGETYPES} ; do
>                 baseName=$imageType-${KERNEL_IMAGE_NAME}
> -               install -m 0644 ${KERNEL_OUTPUT_DIR}/$imageType $deployDir/$baseName${KERNEL_IMAGE_BIN_EXT}
> +
> +               if [ -s ${KERNEL_OUTPUT_DIR}/$imageType.stripped ] ; then
> +                       install -m 0644 ${KERNEL_OUTPUT_DIR}/$imageType.stripped $deployDir/$baseName${KERNEL_IMAGE_BIN_EXT}
> +               else
> +                       install -m 0644 ${KERNEL_OUTPUT_DIR}/$imageType $deployDir/$baseName${KERNEL_IMAGE_BIN_EXT}
> +               fi

Is there a scenario when the ".stripped" image isn't available ?
Wouldn't that already be an error, and so the deploy should just be an
error, versus copying the non-stripped one ?

Bruce

>                 if [ -n "${KERNEL_IMAGE_LINK_NAME}" ] ; then
>                         ln -sf $baseName${KERNEL_IMAGE_BIN_EXT} $deployDir/$imageType-${KERNEL_IMAGE_LINK_NAME}${KERNEL_IMAGE_BIN_EXT}
>                 fi
> --
> 2.31.1
>
Saul Wold Jan. 12, 2022, 3:42 p.m. UTC | #2
On 1/12/22 06:30, Bruce Ashfield wrote:
> On Tue, Jan 11, 2022 at 6:59 PM Saul Wold <saul.wold@windriver.com> wrote:
>>
>> Re-use the runstrip() code from oe.packaging, for the kernel
>> stripping process. Since runstrip() is python the kernel do_strip()
>> need to be converted to Python also. The stripped kernel image
>> will be used for deployment in do_deploy(). This will allow the
>> package.bbclass to split an unstripped kernel binary for the debug
>> information and extended packagedata. The extended package data is
>> used by create-spdx.
>>
>> Signed-off-by: Saul Wold <saul.wold@windriver.com>
>> ---
>>   meta/classes/kernel.bbclass | 44 ++++++++++++++++---------------------
>>   1 file changed, 19 insertions(+), 25 deletions(-)
>>
>> diff --git a/meta/classes/kernel.bbclass b/meta/classes/kernel.bbclass
>> index 7685c6921fa..b2f9e3c8071 100644
>> --- a/meta/classes/kernel.bbclass
>> +++ b/meta/classes/kernel.bbclass
>> @@ -700,30 +700,19 @@ do_kernel_link_images() {
>>   }
>>   addtask kernel_link_images after do_compile before do_strip
>>
>> -do_strip() {
>> -       if [ -n "${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}" ]; then
>> -               if ! (echo "${KERNEL_IMAGETYPES}" | grep -wq "vmlinux"); then
>> -                       bbwarn "image type(s) will not be stripped (not supported): ${KERNEL_IMAGETYPES}"
>> -                       return
>> -               fi
>> -
>> -               cd ${B}
>> -               headers=`"$CROSS_COMPILE"readelf -S ${KERNEL_OUTPUT_DIR}/vmlinux | \
>> -                         grep "^ \{1,\}\[[0-9 ]\{1,\}\] [^ ]" | \
>> -                         sed "s/^ \{1,\}\[[0-9 ]\{1,\}\] //" | \
>> -                         gawk '{print $1}'`
>> -
>> -               for str in ${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}; do {
>> -                       if ! (echo "$headers" | grep -q "^$str$"); then
>> -                               bbwarn "Section not found: $str";
>> -                       fi
> 
> We are losing this warning in the relocated / common routine. It looks
> like something we could continue to detect and warn on, no ?
> 
I guess do we care in this case?  We still do the strip regardless, the 
strip command does not complain about a missing section.

>> -
>> -                       "$CROSS_COMPILE"strip -s -R $str ${KERNEL_OUTPUT_DIR}/vmlinux
>> -               }; done
>> -
>> -               bbnote "KERNEL_IMAGE_STRIP_EXTRA_SECTIONS is set, stripping sections:" \
>> -                       "${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}"
>> -       fi;
>> +python do_strip() {
>> +    import shutil
>> +
>> +    strip = d.getVar('STRIP')
>> +    extra_sections = d.getVar('KERNEL_IMAGE_STRIP_EXTRA_SECTIONS')
>> +    kernel_image = d.getVar('B') + "/" + d.getVar('KERNEL_OUTPUT_DIR') + "/vmlinux"
>> +
>> +    if (extra_sections is not None and kernel_image.find('boot/vmlinux') != -1):
> 
> Minor 'nit .. I'd probably just write that as: if extra_sections
> (versus the is not None) .. but maybe you have it that way due to that
> check not working.
> 
>> +        kernel_image_stripped = kernel_image + ".stripped"
>> +        shutil.copy2(kernel_image, kernel_image_stripped)
>> +        oe.package.runstrip((kernel_image_stripped, 8, strip, extra_sections))
>> +        bb.note ("KERNEL_IMAGE_STRIP_EXTRA_SECTIONS is set, stripping sections: " + \
>> +            extra_sections)
> 
> In theory, we could move the debug log to the common routine when it
> detects the extra section argument
> 
Sure this can move to bb.debug(), will do that.

>>   }
>>   do_strip[dirs] = "${B}"
>>
>> @@ -768,7 +757,12 @@ kernel_do_deploy() {
>>
>>          for imageType in ${KERNEL_IMAGETYPES} ; do
>>                  baseName=$imageType-${KERNEL_IMAGE_NAME}
>> -               install -m 0644 ${KERNEL_OUTPUT_DIR}/$imageType $deployDir/$baseName${KERNEL_IMAGE_BIN_EXT}
>> +
>> +               if [ -s ${KERNEL_OUTPUT_DIR}/$imageType.stripped ] ; then
>> +                       install -m 0644 ${KERNEL_OUTPUT_DIR}/$imageType.stripped $deployDir/$baseName${KERNEL_IMAGE_BIN_EXT}
>> +               else
>> +                       install -m 0644 ${KERNEL_OUTPUT_DIR}/$imageType $deployDir/$baseName${KERNEL_IMAGE_BIN_EXT}
>> +               fi
> 
> Is there a scenario when the ".stripped" image isn't available ?
> Wouldn't that already be an error, and so the deploy should just be an
> error, versus copying the non-stripped one ?
> 
We only copy to .stripped when KERNEL_IMAGE_STRIP_EXTRA_SECTIONS is set, 
so in the non-set case we will have the un-stripped image that needs to 
get installed correctly.


Sau!
> Bruce
> 
>>                  if [ -n "${KERNEL_IMAGE_LINK_NAME}" ] ; then
>>                          ln -sf $baseName${KERNEL_IMAGE_BIN_EXT} $deployDir/$imageType-${KERNEL_IMAGE_LINK_NAME}${KERNEL_IMAGE_BIN_EXT}
>>                  fi
>> --
>> 2.31.1
>>
> 
>

Patch

diff --git a/meta/classes/kernel.bbclass b/meta/classes/kernel.bbclass
index 7685c6921fa..b2f9e3c8071 100644
--- a/meta/classes/kernel.bbclass
+++ b/meta/classes/kernel.bbclass
@@ -700,30 +700,19 @@  do_kernel_link_images() {
 }
 addtask kernel_link_images after do_compile before do_strip
 
-do_strip() {
-	if [ -n "${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}" ]; then
-		if ! (echo "${KERNEL_IMAGETYPES}" | grep -wq "vmlinux"); then
-			bbwarn "image type(s) will not be stripped (not supported): ${KERNEL_IMAGETYPES}"
-			return
-		fi
-
-		cd ${B}
-		headers=`"$CROSS_COMPILE"readelf -S ${KERNEL_OUTPUT_DIR}/vmlinux | \
-			  grep "^ \{1,\}\[[0-9 ]\{1,\}\] [^ ]" | \
-			  sed "s/^ \{1,\}\[[0-9 ]\{1,\}\] //" | \
-			  gawk '{print $1}'`
-
-		for str in ${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}; do {
-			if ! (echo "$headers" | grep -q "^$str$"); then
-				bbwarn "Section not found: $str";
-			fi
-
-			"$CROSS_COMPILE"strip -s -R $str ${KERNEL_OUTPUT_DIR}/vmlinux
-		}; done
-
-		bbnote "KERNEL_IMAGE_STRIP_EXTRA_SECTIONS is set, stripping sections:" \
-			"${KERNEL_IMAGE_STRIP_EXTRA_SECTIONS}"
-	fi;
+python do_strip() {
+    import shutil
+
+    strip = d.getVar('STRIP')
+    extra_sections = d.getVar('KERNEL_IMAGE_STRIP_EXTRA_SECTIONS')
+    kernel_image = d.getVar('B') + "/" + d.getVar('KERNEL_OUTPUT_DIR') + "/vmlinux"
+
+    if (extra_sections is not None and kernel_image.find('boot/vmlinux') != -1):
+        kernel_image_stripped = kernel_image + ".stripped"
+        shutil.copy2(kernel_image, kernel_image_stripped)
+        oe.package.runstrip((kernel_image_stripped, 8, strip, extra_sections))
+        bb.note ("KERNEL_IMAGE_STRIP_EXTRA_SECTIONS is set, stripping sections: " + \
+            extra_sections)
 }
 do_strip[dirs] = "${B}"
 
@@ -768,7 +757,12 @@  kernel_do_deploy() {
 
 	for imageType in ${KERNEL_IMAGETYPES} ; do
 		baseName=$imageType-${KERNEL_IMAGE_NAME}
-		install -m 0644 ${KERNEL_OUTPUT_DIR}/$imageType $deployDir/$baseName${KERNEL_IMAGE_BIN_EXT}
+
+		if [ -s ${KERNEL_OUTPUT_DIR}/$imageType.stripped ] ; then
+			install -m 0644 ${KERNEL_OUTPUT_DIR}/$imageType.stripped $deployDir/$baseName${KERNEL_IMAGE_BIN_EXT}
+		else
+			install -m 0644 ${KERNEL_OUTPUT_DIR}/$imageType $deployDir/$baseName${KERNEL_IMAGE_BIN_EXT}
+		fi
 		if [ -n "${KERNEL_IMAGE_LINK_NAME}" ] ; then
 			ln -sf $baseName${KERNEL_IMAGE_BIN_EXT} $deployDir/$imageType-${KERNEL_IMAGE_LINK_NAME}${KERNEL_IMAGE_BIN_EXT}
 		fi