public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andreas Robinson <andr345@gmail.com>
To: sam@ravnborg.org, rusty@rustcorp.com.au
Cc: linux-kernel@vger.kernel.org
Subject: [RFC PATCH 5/6] scripts: new module preprocessor for static linking
Date: Sun, 15 Feb 2009 19:20:27 +0100	[thread overview]
Message-ID: <1234722028-8110-6-git-send-email-andr345@gmail.com> (raw)
In-Reply-To: <1234722028-8110-1-git-send-email-andr345@gmail.com>

This script strips unneeded sections, resolves duplicate
symbols and concatenates multiple modules into a single
object file suitable for linking with vmlinux.

Problem: Debug symbols must be stripped or else objcopy
won't remove any sections.
---
 scripts/ld_extmodules.sh |  149 ++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 149 insertions(+), 0 deletions(-)
 create mode 100755 scripts/ld_extmodules.sh

diff --git a/scripts/ld_extmodules.sh b/scripts/ld_extmodules.sh
new file mode 100755
index 0000000..6049bfd
--- /dev/null
+++ b/scripts/ld_extmodules.sh
@@ -0,0 +1,149 @@
+#!/bin/sh
+# Copyright (C) 2009 Andreas Robinson <andr345@gmail.com>
+#
+# Released under the terms of the GNU GPL v2 or later.
+
+# Usage ###############################
+
+if [ "$#" -ne "2" ]
+then
+
+cat << EOF
+
+Prepare and link a set of modules for static linking with the kernel.
+
+Usage: `basename $0` <modules.lst> <output.o>
+
+<modules.lst> List of modules to link, in insertion order.
+              This is a plain text file with one pathname per line.
+<output.o>    Output object file.
+              You can use modinfo or "readelf -p .modinfo" to read
+              the table of contents.
+
+If the target architecture differs from your host, you must set the
+CROSS_COMPILE environment variable. It is used as a prefix for the
+binutil commands.
+
+EOF
+
+exit 1
+fi
+
+#CROSS_COMPILE=
+LD=${CROSS_COMPILE}ld
+NM=${CROSS_COMPILE}nm
+STRIP=${CROSS_COMPILE}strip
+OBJCOPY=${CROSS_COMPILE}objcopy
+OBJDUMP=${CROSS_COMPILE}objdump
+READELF=${CROSS_COMPILE}readelf
+
+# Helper functions ####################
+
+# Simple "modinfo -F". No need to depend on module-init-tools.
+# $1 = module
+# $2 = field name.
+my_modinfo()
+{
+	${READELF} $1 -p .modinfo | grep -o "$2=.*" | sed s/"$2="//g
+}
+
+# Returns true if module $1 has a GPL compatible license.
+# If a module has more than one license tag, (some do),
+# this function will only look at the first one.
+is_gpl_compatible()
+{
+	license=`my_modinfo $1 license | head -n 1`
+	[ "$license" = "GPL" ] ||
+		[ "$license" = "GPL v2" ] ||
+		[ "$license" = "GPL and additional rights" ] ||
+		[ "$license" = "Dual BSD/GPL" ] ||
+		[ "$license" = "Dual MIT/GPL" ] ||
+		[ "$license" = "Dual MPL/GPL" ]
+}
+
+# Returns true if ELF-file $1 has a section named $2 .
+have_section()
+{
+	[ "`${OBJDUMP} -h $1 | grep -o $2`" != "" ]
+}
+
+# Main ################################
+
+rm -f .tmp_mod* $2
+
+cat $1 | while read module;
+do
+	# Cull unhandled files
+
+	if [ ! -f "$module" ]
+	then
+		echo Skipped: $module - file not found.
+		continue
+	fi
+
+	if ! have_section $module .gnu.linkonce.this_module
+	then
+		echo Skipped: $module - not a kernel module.
+		continue
+	fi
+
+	if ! is_gpl_compatible $module
+	then
+		echo Skipped: $module - not GPL compatible.
+		continue
+	fi
+
+	if ! have_section $module .mod_initcall.init
+	then
+		echo "Warning: `basename $module` has no " \
+			".mod_initcall.init section."
+		echo "         The kernel will not call this " \
+			"module's init function."
+	fi
+
+	# Add to table of contents. (module pathname and srcversion.)
+	printf "module=$module `my_modinfo $module srcversion`\0" \
+		>> .tmp_mod_toc.txt
+
+	# Prepare module for linking with the kernel
+
+	${NM} $module 2> /dev/null | \
+		grep -o "__mod_.*_device_table" > .tmp_mod_devtbl
+
+	${LD} -r --defsym __this_module=0 $module -o .tmp_mod1.o
+
+	${OBJCOPY} --strip-debug                                \
+		--remove-section .gnu.linkonce.this_module      \
+		--remove-section .modinfo                       \
+		--localize-symbol init_module                   \
+		--localize-symbol cleanup_module                \
+		--localize-symbol __this_module                 \
+		.tmp_mod1.o
+
+	if [ ! -f .tmp_mod1.o ]
+	then
+		echo Skipped: $module - objcopy failed.
+		continue
+	fi
+
+	if [ -s .tmp_mod_devtbl ]
+	then
+		${OBJCOPY} --strip-symbols .tmp_mod_devtbl .tmp_mod1.o
+	fi
+
+	# Merge modules into one file.
+
+	if [ ! -f $2 ]; then
+		mv .tmp_mod1.o $2
+	else
+		${LD} -r $2 .tmp_mod1.o -o .tmp_mod2.o
+		mv .tmp_mod2.o $2
+	fi
+
+	echo Merged: $module
+done
+
+# Insert table of contents
+
+${OBJCOPY} --add-section .modinfo=.tmp_mod_toc.txt $2
+
-- 
1.5.6.3


  parent reply	other threads:[~2009-02-15 18:22 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-15 18:20 [RFC PATCH 0/6] module, kbuild: Faster boot with custom kernel Andreas Robinson
2009-02-15 18:20 ` [RFC PATCH 1/6] New option: Static linking of external modules Andreas Robinson
2009-02-15 18:20 ` [RFC PATCH 2/6] module: add module ELF section with module_init() pointer Andreas Robinson
2009-02-15 18:20 ` [RFC PATCH 3/6] module: always prefix module parameters with the module name Andreas Robinson
2009-02-15 18:20 ` [RFC PATCH 4/6] kbuild: allow linking of an external object into vmlinux Andreas Robinson
2009-02-15 18:20 ` Andreas Robinson [this message]
2009-02-15 18:20 ` [RFC PATCH 6/6] kbuild: enable relinking of vmlinux without full kernel tree Andreas Robinson
2009-02-16 22:51 ` [RFC PATCH 0/6] module, kbuild: Faster boot with custom kernel Rusty Russell
2009-02-17 10:42   ` Andreas Robinson
2009-02-17 11:53     ` Kay Sievers
2009-02-18  4:58       ` Rusty Russell
2009-02-18  9:15         ` Kay Sievers
2009-02-18 10:25           ` Andreas Robinson
2009-02-20  0:37             ` Andreas Robinson
2009-02-20  1:55               ` Kay Sievers
2009-02-21 11:43                 ` Andreas Robinson
2009-03-02 14:32                   ` Andreas Robinson
2009-03-02 15:59                     ` Kay Sievers
2009-03-02 16:20                     ` Arjan van de Ven
2009-03-02 16:29                       ` Kay Sievers
2009-03-02 18:27                         ` Arjan van de Ven
2009-03-02 21:41                           ` Andreas Robinson
2009-03-04 18:47                           ` Andreas Robinson
2009-03-06  0:18                             ` Arjan van de Ven
2009-03-06 15:15                               ` Andreas Robinson
2009-03-06 15:45                                 ` Arjan van de Ven
2009-03-08 10:47                                   ` Andreas Robinson
2009-03-08 16:01                                     ` Arjan van de Ven
2009-03-08 20:13                                       ` [PATCH] sata_nv: add a module parameter to enable async scanning Andreas Robinson
2009-03-09 17:12                                       ` [RFC PATCH 0/6] module, kbuild: Faster boot with custom kernel Will Newton
2009-03-06  7:05                       ` fastboot kernel parameter Sitsofe Wheeler
2009-03-06 11:23                         ` Arjan van de Ven
2009-02-24  1:27               ` [RFC PATCH 0/6] module, kbuild: Faster boot with custom kernel Rusty Russell
2009-02-18 11:57           ` Rusty Russell
2009-02-18 13:57             ` Kay Sievers
2009-02-19 11:15               ` Rusty Russell
2009-02-19 11:41                 ` Kay Sievers
2009-02-19 20:48                   ` Kay Sievers
2009-02-19 21:59                     ` Kay Sievers
2009-02-20  0:58                       ` Rusty Russell
2009-02-20  1:33                         ` Kay Sievers
2009-02-24  1:39                           ` Rusty Russell
2009-02-20 11:32                         ` Rusty Russell
2009-02-23 16:42                           ` Kay Sievers
2009-02-25  7:03                             ` Rusty Russell
2009-02-25 18:12                               ` Kay Sievers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1234722028-8110-6-git-send-email-andr345@gmail.com \
    --to=andr345@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rusty@rustcorp.com.au \
    --cc=sam@ravnborg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox