diff mbox

[1/4] elfwrapper: Add new class for the elfwrapper function.

Message ID f861489cc59297a72117ff262b5a2078116f6870.1385470877.git.christian.braunersorensen@prevas.dk
State Accepted
Delegated to: Esben Haabendal
Headers show

Commit Message

christian.braunersorensen@prevas.dk Nov. 26, 2013, 1:03 p.m. UTC
From: Christian Sørensen <christian.braunersorensen@prevas.dk>

Signed-off-by: Christian Sørensen <christian.braunersorensen@prevas.dk>
---
 classes/elfwrapper.oeclass | 77 ++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 77 insertions(+)
 create mode 100644 classes/elfwrapper.oeclass

Comments

Esben Haabendal Nov. 28, 2013, 10:11 a.m. UTC | #1
Merged to master, thanks.

I merged in the latest changes to sdk-image.oeclass from Morten to the
new elfwrapper.oeclass on top of this.

/Esben
diff mbox

Patch

diff --git a/classes/elfwrapper.oeclass b/classes/elfwrapper.oeclass
new file mode 100644
index 0000000..33ab1a0
--- /dev/null
+++ b/classes/elfwrapper.oeclass
@@ -0,0 +1,77 @@ 
+## Class for wrapping elf
+##
+## Has included fixes for non-static images
+## toolchains w.r.t. ensuring that the correct libraries are chosen when using
+## binary tools.
+##
+## @var SDK_IMAGE_ELF_SOWRAP_DIRS If set, ensures that the correct libraries
+##      is used when executing binary files. This is ofcause only relevant for
+##      non-static toolchains. Moves the binary tools to ., and uses a wrapper
+##      that uses ld-linux to ensure that the correct libraries is chosen.
+
+IMAGE_ELF_SOWRAP_DIRS ?= "${base_bindir} ${bindir}"
+IMAGE_ELF_SOWRAP_LD_SO ?= "/lib/ld-linux*.so.*"
+
+RSTAGE_FIXUP_FUNCS += "${IMAGE_PREPROCESS_ELF_SOWRAP}"
+IMAGE_PREPROCESS_ELF_SOWRAP = ""
+IMAGE_PREPROCESS_ELF_SOWRAP:HOST_BINFMT_elf = " image_preprocess_elf_sowrap"
+def image_preprocess_elf_sowrap(d):
+    import stat
+    import magic
+
+    filemagic = magic.open(magic.MAGIC_NONE)
+    filemagic.load()
+    host_elf_re = re.compile(d.get("HOST_ELF"))
+    ld_so = d.get("IMAGE_ELF_SOWRAP_LD_SO")
+
+    def is_elf(path):
+        filetype = filemagic.file(path)
+        return bool(host_elf_re.match(filetype))
+
+    def is_static(path):
+        filetype = filemagic.file(path)
+        static_re = re.compile(".*statically.*")
+        return bool(static_re.match(filetype))
+
+    def sowrap_dir(dir, recursive=False):
+        if not os.path.exists(dir):
+            return True
+        assert os.path.isdir(dir)
+        ok = True
+        for file in os.listdir(dir):
+            path = os.path.join(dir, file)
+            if os.path.islink(path):
+                continue
+            if os.path.isdir(path):
+                if recursive:
+                    sowrap_dir(path, recursive)
+                continue
+            dotpath = "%s/.%s"%(os.path.dirname(path), os.path.basename(path))
+            if os.path.exists(dotpath):
+                print "ERROR: file already exists:", os.path.join(dir, path)
+                ok = False
+                continue
+            if is_elf(path) and is_static(path):
+                continue
+            os.rename(path, dotpath)
+            with open(path, "w") as wrapper:
+                dirparts = len(os.path.dirname(path).split('/'))
+                relative_root = "/".join([".."] * dirparts)
+                wrapper.write("#!/bin/sh\n")
+                wrapper.write("$(dirname $0)/%s%s $(dirname $0)/%s $*\n"%(
+                        relative_root, ld_so, os.path.basename(dotpath)))
+            os.chmod(path, stat.S_IRWXU|stat.S_IRWXG|stat.S_IROTH|stat.S_IXOTH)
+        return True
+
+    bindirs = set(d.get("IMAGE_ELF_SOWRAP_DIRS").split())
+    for dir in bindirs:
+        recursive=False
+        if dir.endswith("//"):
+            recursive=True
+        dir = dir.strip("/")
+        rc = sowrap_dir(dir, recursive)
+        if not rc:
+            filemagic.close()
+            return rc
+    filemagic.close()
+    return