[PATCH] libc function availability checker for a shared library

17 views
Skip to first unread message

Takuya ASADA

unread,
Mar 7, 2014, 10:50:02 PM3/7/14
to osv...@googlegroups.com, Takuya ASADA
This script list up missing libc function called from a shared library.
Usage:
./scripts/check-libcfunc-avail.sh release apps/memcached/memcached.so

Signed-off-by: Takuya ASADA <sy...@cloudius-systems.com>
---
scripts/check-libcfunc-avail.sh | 21 +++++++++++++++++++++
1 file changed, 21 insertions(+)
create mode 100755 scripts/check-libcfunc-avail.sh

diff --git a/scripts/check-libcfunc-avail.sh b/scripts/check-libcfunc-avail.sh
new file mode 100755
index 0000000..0a412ee
--- /dev/null
+++ b/scripts/check-libcfunc-avail.sh
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+if [ "$1" == "" -o "$2" == "" ]; then
+ echo "usage: $0 [release | debug] [app.so]"
+ exit 1
+fi
+
+MODE=$1
+APP=$2
+
+FUNCS=`objdump -T $APP | grep GLIBC|sed -e "s/.*GLIBC_[0-9.]* //"`
+for i in $FUNCS; do
+ if [ ! -f build/$MODE/loader.elf ]; then
+ echo "build/$MODE/loader.elf not found"
+ exit 1
+ fi
+ FOUND=`objdump -t build/$MODE/loader.elf | grep -e " $i$" | wc -l`
+ if [ $FOUND == 0 ]; then
+ echo "$i not found"
+ fi
+done
--
1.8.5.3

Nadav Har'El

unread,
Mar 9, 2014, 9:18:38 AM3/9/14
to Takuya ASADA, Osv Dev
On Sat, Mar 8, 2014 at 5:50 AM, Takuya ASADA <sy...@cloudius-systems.com> wrote:
This script list up missing libc function called from a shared library.
Usage:
./scripts/check-libcfunc-avail.sh release apps/memcached/memcached.so

Signed-off-by: Takuya ASADA <sy...@cloudius-systems.com>
---
 scripts/check-libcfunc-avail.sh | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
 create mode 100755 scripts/check-libcfunc-avail.sh

diff --git a/scripts/check-libcfunc-avail.sh b/scripts/check-libcfunc-avail.sh
new file mode 100755
index 0000000..0a412ee
--- /dev/null
+++ b/scripts/check-libcfunc-avail.sh
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+if [ "$1" == "" -o "$2" == "" ]; then

Arguably, checking [ $# != 2 ] is more elegant ($# is the number of arguments, like argc-1 in C )

+    echo "usage: $0 [release | debug] [app.so]"
+    exit 1
+fi
+
+MODE=$1
+APP=$2
+
+FUNCS=`objdump -T $APP | grep GLIBC|sed -e "s/.*GLIBC_[0-9.]* //"`
+for i in $FUNCS; do
+    if [ ! -f build/$MODE/loader.elf ]; then
+        echo "build/$MODE/loader.elf not found"
+        exit 1
+    fi
+    FOUND=`objdump -t build/$MODE/loader.elf | grep -e " $i$" | wc -l`

You are doing here objdump again and again. I guess on a modern computer this is insignificant and the whole loop will not take much more than a few seconds, but it could have a few milliseconds...

The most efficient way to do this would be to run objdump loader.elf just once, put all the names in it in a hash table (bash has hashtables, or use python if you prefer) and then look for the functions in the hash table.

By the way, unrelated shell notes:
 1.  "grep -q" can be used to see if there's any match - no need for wc.
 2. normal grep, without "-e", already supports "$".

So you could have also did something equally slow to what you did but arguably nicer shell:

if objdump -t build/$MODE/loader.elf | grep -q " $i$"
then
    echo "$i not found"
fi


+    if [ $FOUND == 0 ]; then
+        echo "$i not found"
+    fi
+done
--
1.8.5.3

--
You received this message because you are subscribed to the Google Groups "OSv Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to osv-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Takuya ASADA

unread,
Mar 9, 2014, 4:33:26 AM3/9/14
to osv...@googlegroups.com, Takuya ASADA
This script list up missing libc function called from a shared library.
Usage:
./scripts/check-libcfunc-avail.sh release apps/memcached/memcached.so

Signed-off-by: Takuya ASADA <sy...@cloudius-systems.com>
---
scripts/check-libcfunc-avail.sh | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
create mode 100755 scripts/check-libcfunc-avail.sh

diff --git a/scripts/check-libcfunc-avail.sh b/scripts/check-libcfunc-avail.sh
new file mode 100755
index 0000000..0bca3aa
--- /dev/null
+++ b/scripts/check-libcfunc-avail.sh
@@ -0,0 +1,25 @@
+#!/bin/bash
+
+if [ $# != 2 ]; then
+ echo "usage: $0 [release | debug] [app.so]"
+ exit 1
+fi
+
+MODE=$1
+APP=$2
+
+if [ ! -f build/$MODE/loader.elf ]; then
+ echo "build/$MODE/loader.elf not found"
+ exit 1
+fi
+DUMPFILE=`mktemp`
+objdump -t build/$MODE/loader.elf > $DUMPFILE
+FUNCS=`objdump -T $APP | grep GLIBC|sed -e "s/.*GLIBC_[0-9.]* //"`
+for i in $FUNCS; do
+ grep -q " $i$" $DUMPFILE
+ FOUND=$?
+ if [ $FOUND != 0 ]; then
+ echo "$i not found"
+ fi
+done
+rm $DUMPFILE
--
1.8.5.3

Nadav Har'El

unread,
Mar 11, 2014, 2:45:36 AM3/11/14
to Takuya ASADA, Osv Dev
Looks better (could have been more efficient if you didn't read DUMPFILE again and again, but at this point, I don't think anyone cares).

Some minor shell style suggestions below.


On Sun, Mar 9, 2014 at 10:33 AM, Takuya ASADA <sy...@cloudius-systems.com> wrote:
This script list up missing libc function called from a shared library.
Usage:
./scripts/check-libcfunc-avail.sh release apps/memcached/memcached.so

Signed-off-by: Takuya ASADA <sy...@cloudius-systems.com>
---
 scripts/check-libcfunc-avail.sh | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
 create mode 100755 scripts/check-libcfunc-avail.sh

diff --git a/scripts/check-libcfunc-avail.sh b/scripts/check-libcfunc-avail.sh
new file mode 100755
index 0000000..0bca3aa
--- /dev/null
+++ b/scripts/check-libcfunc-avail.sh
@@ -0,0 +1,25 @@
+#!/bin/bash
+
+if [ $# != 2 ]; then
+    echo "usage: $0 [release | debug] [app.so]"

It is customary to print error messages to the standard error. Also, it is Unix tradition when printing the "Usage" to put in brackets *optional* arguments,  while here in fact the argument is not optional (see also http://pubs.opengroup.org/onlinepubs/009696899/basedefs/xbd_chap12.html). So I would prefer this:

    echo "Usage: $0 release|debug <app.so> " >&2

(the >&2 redirects the output to fd 2, which is the standard error).



+if [ ! -f build/$MODE/loader.elf ]; then
+    echo "build/$MODE/loader.elf not found"
+    exit 1
+fi
+DUMPFILE=`mktemp`

Please clean up the temporary file if the script is interrupted:

trap "rm $DUMPFILE" 1 2 3 15

(this runs that code if the shell is killed with SIGHUP, SIGINT, SIGQUIT or SIGTERM). 

+objdump -t build/$MODE/loader.elf > $DUMPFILE
+FUNCS=`objdump -T $APP | grep GLIBC|sed -e "s/.*GLIBC_[0-9.]* //"`
+for i in $FUNCS; do
+    grep -q " $i$" $DUMPFILE
+    FOUND=$?
+    if [ $FOUND != 0 ]; then

You can replace the above three lines by the simpler

    if grep -q " $i$" $DUMPFILE
    then

(if normally checks the exit code of the command - no need to save this exit code in a variable and then run test ("[") on it).
 
+        echo "$i not found"
+    fi
+done
+rm $DUMPFILE
--
1.8.5.3

--
You received this message because you are subscribed to the Google Groups "OSv Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to osv-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Nadav Har'El

unread,
Mar 11, 2014, 7:23:47 AM3/11/14
to Takuya ASADA, Osv Dev
On Tue, Mar 11, 2014 at 8:45 AM, Nadav Har'El <n...@cloudius-systems.com> wrote:


trap "rm $DUMPFILE" 1 2 3 15


Sorry, this should have been

 trap "rm -f $DUMPFILE; exit" 1 2 3 15

Otherwise, if we don't exit, the shell will continue after the signal instead of exiting like it should.

Takuya ASADA

unread,
Mar 10, 2014, 8:24:26 AM3/10/14
to osv...@googlegroups.com, Takuya ASADA
This script list up missing libc function called from a shared library.
Usage:
./scripts/check-libcfunc-avail.sh release apps/memcached/memcached.so

Signed-off-by: Takuya ASADA <sy...@cloudius-systems.com>
---
scripts/check-libcfunc-avail.sh | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
create mode 100755 scripts/check-libcfunc-avail.sh

diff --git a/scripts/check-libcfunc-avail.sh b/scripts/check-libcfunc-avail.sh
new file mode 100755
index 0000000..0bca3aa
--- /dev/null
+++ b/scripts/check-libcfunc-avail.sh
@@ -0,0 +1,25 @@
+#!/bin/bash
+
+if [ $# != 2 ]; then
+ echo "usage: $0 [release | debug] [app.so]"
+ exit 1
+fi
+
+MODE=$1
+APP=$2
+
+if [ ! -f build/$MODE/loader.elf ]; then
+ echo "build/$MODE/loader.elf not found"
+ exit 1
+fi
+DUMPFILE=`mktemp`
+objdump -t build/$MODE/loader.elf > $DUMPFILE
+FUNCS=`objdump -T $APP | grep GLIBC|sed -e "s/.*GLIBC_[0-9.]* //"`
+for i in $FUNCS; do
+ grep -q " $i$" $DUMPFILE
+ FOUND=$?
+ if [ $FOUND != 0 ]; then

Asias He

unread,
Mar 19, 2014, 4:06:04 AM3/19/14
to Takuya ASADA, Osv Dev
Ping.


--
You received this message because you are subscribed to the Google Groups "OSv Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to osv-dev+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Asias

Commit Bot

unread,
Mar 21, 2014, 4:59:47 AM3/21/14
to osv...@googlegroups.com, sy...@cloudius-systems.com
From: Takuya ASADA <sy...@cloudius-systems.com>

libc function availability checker for a shared library

This script list up missing libc function called from a shared library.
Usage:
./scripts/check-libcfunc-avail.sh release apps/memcached/memcached.so

Signed-off-by: Takuya ASADA <sy...@cloudius-systems.com>
Signed-off-by: Pekka Enberg <pen...@cloudius-systems.com>

---
diff --git a/scripts/check-libcfunc-avail.sh
b/scripts/check-libcfunc-avail.sh
--- a/scripts/check-libcfunc-avail.sh
Reply all
Reply to author
Forward
0 new messages