Openembedded Core Discussions
 help / color / mirror / Atom feed
From: Olof Johansson <olof.johansson@axis.com>
To: openembedded-core@lists.openembedded.org
Cc: Olof Johansson <olofjn@axis.com>
Subject: [PATCH 1/5] lib/oe/package.py: Expose is_elf
Date: Fri,  1 Dec 2017 16:50:20 +0100	[thread overview]
Message-ID: <20171201155024.3002-2-olofjn@axis.com> (raw)
In-Reply-To: <20171201155024.3002-1-olofjn@axis.com>

is_elf/isELF had copies in both staging.bbclass and package.bbclass.
After recent refactoring in staging.bbclass (involving breaking out the
isELF function to is_elf in lib/oe/package.py), the implementions
diverged. It would be beneficial to make everybody use this one
implementation, so let's expose it here for others to use.

Signed-off-by: Olof Johansson <olofjn@axis.com>
---
 meta/lib/oe/package.py | 88 +++++++++++++++++++++++++++++++-------------------
 1 file changed, 54 insertions(+), 34 deletions(-)

diff --git a/meta/lib/oe/package.py b/meta/lib/oe/package.py
index 1e5c3aa8e1..f1f9333e0f 100644
--- a/meta/lib/oe/package.py
+++ b/meta/lib/oe/package.py
@@ -1,3 +1,6 @@
+import mmap
+import oe.utils
+
 def runstrip(arg):
     # Function to strip a single file, called from split_and_strip_files below
     # A working 'file' (one which works on the target architecture)
@@ -44,6 +47,56 @@ def runstrip(arg):
 
     return
 
+# Detect .ko module by searching for "vermagic=" string
+def is_kernel_module(path):
+    with open(path) as f:
+        return mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ).find(b"vermagic=") >= 0
+
+def _is_elf_error(msg):
+    bb.error('is_elf: %s' % msg)
+
+def is_elf(path, on_error=_is_elf_error):
+    """
+    Determine if a given file is an ELF archive (and other attributes),
+    using the file utility.
+
+    :param path: str, path of potential ELF file
+    :param on_error: callable, gets called when an error occurs.
+                     the callback takes a message parameter. A
+                     default error handler is provided that prints
+                     the message with 'bb.error'.
+
+    is_elf returns a bitstring of flags, corresponding to various
+    properties:
+
+    *  1: ELF
+    *  2: stripped
+    *  4: executable
+    *  8: shared library
+    * 16: kernel module
+
+    A return value of 0 means that the file is not an ELF file.
+    """
+    ret, result = oe.utils.getstatusoutput(
+        "file \"%s\"" % path.replace("\"", "\\\""))
+
+    if ret:
+        error_cb('"file %s" failed')
+        return
+
+    if not "ELF" in result:
+        return 0
+
+    exec_type = 1
+    if "not stripped" not in result:
+        exec_type |= 2
+    if "executable" in result:
+        exec_type |= 4
+    if "shared" in result:
+        exec_type |= 8
+    if "relocatable" in result and is_kernel_module(path):
+        exec_type |= 16
+    return exec_type
 
 def strip_execs(pn, dstdir, strip_cmd, libdir, base_libdir, qa_already_stripped=False):
     """
@@ -56,40 +109,7 @@ def strip_execs(pn, dstdir, strip_cmd, libdir, base_libdir, qa_already_stripped=
     :param qa_already_stripped: Set to True if already-stripped' in ${INSANE_SKIP}
     This is for proper logging and messages only.
     """
-    import stat, errno, oe.path, oe.utils, mmap
-
-    # Detect .ko module by searching for "vermagic=" string
-    def is_kernel_module(path):
-        with open(path) as f:
-            return mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ).find(b"vermagic=") >= 0
-
-    # Return type (bits):
-    # 0 - not elf
-    # 1 - ELF
-    # 2 - stripped
-    # 4 - executable
-    # 8 - shared library
-    # 16 - kernel module
-    def is_elf(path):
-        exec_type = 0
-        ret, result = oe.utils.getstatusoutput(
-            "file \"%s\"" % path.replace("\"", "\\\""))
-
-        if ret:
-            bb.error("split_and_strip_files: 'file %s' failed" % path)
-            return exec_type
-
-        if "ELF" in result:
-            exec_type |= 1
-            if "not stripped" not in result:
-                exec_type |= 2
-            if "executable" in result:
-                exec_type |= 4
-            if "shared" in result:
-                exec_type |= 8
-            if "relocatable" in result and is_kernel_module(path):
-                exec_type |= 16
-        return exec_type
+    import stat, errno, oe.path, oe.utils
 
     elffiles = {}
     inodes = {}
-- 
2.11.0



  reply	other threads:[~2017-12-01 15:59 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-01 15:50 [PATCH 0/5] Improve isELF, gets triggered by ELF anywhere in pathname Olof Johansson
2017-12-01 15:50 ` Olof Johansson [this message]
2017-12-04  9:34   ` [PATCH 1/5] lib/oe/package.py: Expose is_elf Olof Johansson
2017-12-01 15:50 ` [PATCH 2/5] package.bbclass: Make use of common is_elf function Olof Johansson
2017-12-01 15:50 ` [PATCH 3/5] lib/oe/package.py: is_elf: Don't let filename influence filetype Olof Johansson
2017-12-01 15:50 ` [PATCH 4/5] lib/oe/package.py: is_elf: Disallow shell specials to be expanded Olof Johansson
2017-12-01 15:50 ` [PATCH 5/5] lib/oe/package.py: is_elf: Make it less prone to false positives Olof Johansson
2017-12-01 17:43   ` Mark Hatle
2017-12-01 21:13     ` Olof Johansson
2017-12-04 10:00       ` Olof Johansson
2017-12-04 19:22         ` Mark Hatle
2017-12-04 12:36   ` Burton, Ross
2017-12-04 15:30     ` Olof Johansson
2017-12-04 15:33       ` Burton, Ross
2017-12-06 21:38         ` Burton, Ross
2017-12-18 11:06           ` Olof Johansson
2017-12-18 11:28             ` Burton, Ross
2017-12-18 12:00               ` Burton, Ross
2017-12-20 11:05                 ` Olof Johansson

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=20171201155024.3002-2-olofjn@axis.com \
    --to=olof.johansson@axis.com \
    --cc=olofjn@axis.com \
    --cc=openembedded-core@lists.openembedded.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