Show a patch.

GET /api/1.1/patches/1183/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 1183,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/1183/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/1183/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20190510232235.8724-1-paul.elder@ideasonboard.com>",
    "date": "2019-05-10T23:22:34",
    "name": "[libcamera-devel,1/2] libcamera: lib_loader: add shared library loader",
    "commit_ref": null,
    "pull_url": null,
    "state": "rejected",
    "archived": false,
    "hash": "3caf12ae8f7aa76e72c3772693082474b48b7b80",
    "submitter": {
        "id": 17,
        "url": "https://patchwork.libcamera.org/api/1.1/people/17/?format=api",
        "name": "Paul Elder",
        "email": "paul.elder@ideasonboard.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/1183/mbox/",
    "series": [
        {
            "id": 302,
            "url": "https://patchwork.libcamera.org/api/1.1/series/302/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=302",
            "date": "2019-05-10T23:22:34",
            "name": "[libcamera-devel,1/2] libcamera: lib_loader: add shared library loader",
            "version": 1,
            "mbox": "https://patchwork.libcamera.org/series/302/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/1183/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/1183/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<paul.elder@ideasonboard.com>",
        "Received": [
            "from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 9221260E68\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 11 May 2019 01:22:49 +0200 (CEST)",
            "from localhost.localdomain (unknown [96.44.9.117])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id C158D2DF;\n\tSat, 11 May 2019 01:22:48 +0200 (CEST)"
        ],
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1557530569;\n\tbh=6i0vBzhqiVZbXXrbFF9v6hdecuUyGR8ZUECieYJ5IhI=;\n\th=From:To:Cc:Subject:Date:From;\n\tb=CzUJhWrAIUjDPFD/ZIez07Ne5yxU207o8u1Uqhhprfwh7S8+ejRhdUO+NMASOM95t\n\tpkSCAOvn9MbNPSyxfBh0Fe4VKONUVDWcoFYF0Qb8pweXhHNRckZNh997WN0cZk0kLW\n\t5OHxrnqu7gjEC6j8THeTSAERlJUXFqzhpiQUwlss=",
        "From": "Paul Elder <paul.elder@ideasonboard.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Fri, 10 May 2019 19:22:34 -0400",
        "Message-Id": "<20190510232235.8724-1-paul.elder@ideasonboard.com>",
        "X-Mailer": "git-send-email 2.20.1",
        "MIME-Version": "1.0",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH 1/2] libcamera: lib_loader: add shared\n\tlibrary loader",
        "X-BeenThere": "libcamera-devel@lists.libcamera.org",
        "X-Mailman-Version": "2.1.23",
        "Precedence": "list",
        "List-Id": "<libcamera-devel.lists.libcamera.org>",
        "List-Unsubscribe": "<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>",
        "List-Archive": "<https://lists.libcamera.org/pipermail/libcamera-devel/>",
        "List-Post": "<mailto:libcamera-devel@lists.libcamera.org>",
        "List-Help": "<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>",
        "List-Subscribe": "<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>",
        "X-List-Received-Date": "Fri, 10 May 2019 23:22:49 -0000"
    },
    "content": "Implement a class to load a struct IPAModuleInfo of a given symbol name\nfrom a .so shared object library.\n\nSigned-off-by: Paul Elder <paul.elder@ideasonboard.com>\n---\nI will remove and clean up the comments in lib_loader.h, but I've left\nthem there for now, although I would like comments on the API (and the\ncommented out unload()).\n\nI will also add logging later, as well as checking (guessing) if the\nbitness is acceptable or not.\n\nThe generics to allow duck-typing of the ELF structs was the best I\ncould think of with my limited C++ knowledge of how to deal with the\ndifferent size and field layouts of the 32-bit and 64-bit structs.\n\n src/libcamera/include/lib_loader.h |  59 ++++++++++\n src/libcamera/lib_loader.cpp       | 175 +++++++++++++++++++++++++++++\n src/libcamera/meson.build          |   2 +\n 3 files changed, 236 insertions(+)\n create mode 100644 src/libcamera/include/lib_loader.h\n create mode 100644 src/libcamera/lib_loader.cpp",
    "diff": "diff --git a/src/libcamera/include/lib_loader.h b/src/libcamera/include/lib_loader.h\nnew file mode 100644\nindex 0000000..d8eb100\n--- /dev/null\n+++ b/src/libcamera/include/lib_loader.h\n@@ -0,0 +1,59 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * lib_loader.h - load shared libraries at runtime\n+ */\n+#ifndef __LIBCAMERA_LIB_LOADER_H__\n+#define __LIBCAMERA_LIB_LOADER_H__\n+\n+#include <string>\n+\n+namespace libcamera {\n+\n+class LibLoader\n+{\n+public:\n+\tstruct IPAModuleInfo {\n+\t\tchar name[256];\n+\t\tunsigned int version;\n+\t};\n+\n+\t// doesn't actually do anything?\n+\texplicit LibLoader(const std::string &lib_path);\n+\n+\t// if you try it while it's already loaded it'll just load it again,\n+\t// no big deal\n+\t/* returns 0 on success, err code on failure */\n+\tint loadIPAModuleInfo(const std::string &symbol);\n+\n+\tbool isLoaded() const;\n+\n+\t// just a getter (or returns nullptr if module hasn't been loaded)\n+\tstruct IPAModuleInfo getIPAModuleInfo() const;\n+\n+\t// necessary to allow multiple users of an instance of LibLoader\n+\t/* returns 0 on success, err code on failure, pos num of others loading the lib */\n+\t//int unload();\n+\n+\t//void (*)() resolve(const std::string &symbol);\n+\t// resolve(const std::string &lib_path, const std::string symbol)\n+\n+private:\n+\tstruct IPAModuleInfo info_;\n+\n+\tbool loaded_;\n+\tstd::string lib_path_;\n+\n+\tint bitclass_;\n+\n+\tint loadElfIdent(int fd);\n+\n+\ttemplate<class elfHeader, class secHeader, class symHeader>\n+\tint loadSymbol(struct IPAModuleInfo *dst, int fd,\n+\t\t       const std::string &symbol);\n+};\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_LIB_LOADER_H__ */\ndiff --git a/src/libcamera/lib_loader.cpp b/src/libcamera/lib_loader.cpp\nnew file mode 100644\nindex 0000000..d7788d6\n--- /dev/null\n+++ b/src/libcamera/lib_loader.cpp\n@@ -0,0 +1,175 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * lib_loader.cpp - load shared libraries at runtime\n+ */\n+\n+#include \"lib_loader.h\"\n+\n+#include <elf.h>\n+#include <errno.h>\n+#include <fcntl.h>\n+#include <string.h>\n+#include <sys/mman.h>\n+#include <sys/stat.h>\n+#include <sys/types.h>\n+#include <unistd.h>\n+\n+#include <iostream>\n+\n+#include \"log.h\"\n+\n+namespace libcamera {\n+\n+LOG_DECLARE_CATEGORY(LibLoader)\n+\n+LibLoader::LibLoader(const std::string &lib_path)\n+\t: lib_path_(lib_path)\n+{\n+}\n+\n+int LibLoader::loadElfIdent(int fd)\n+{\n+\tint ret = lseek(fd, 0, SEEK_SET);\n+\tif (ret < 0)\n+\t\treturn errno;\n+\n+\tunsigned char e_ident[EI_NIDENT];\n+\tret = read(fd, e_ident, EI_NIDENT);\n+\tif (ret < 0)\n+\t\treturn errno;\n+\n+\tif (!(e_ident[EI_MAG0] == ELFMAG0 &&\n+\t      e_ident[EI_MAG1] == ELFMAG1 &&\n+\t      e_ident[EI_MAG2] == ELFMAG2 &&\n+\t      e_ident[EI_MAG3] == ELFMAG3) ||\n+\t    e_ident[EI_VERSION] != EV_CURRENT)\n+\t\treturn -1;\n+\n+\tif (e_ident[EI_CLASS] == ELFCLASS32 ||\n+\t    e_ident[EI_CLASS] == ELFCLASS64)\n+\t\tbitclass_ = e_ident[EI_CLASS];\n+\telse\n+\t\treturn -1;\n+\n+\tint a = 1;\n+\tbool little_endian = *(char *)&a == 1;\n+\tif (!(e_ident[EI_DATA] == ELFDATA2LSB ||\n+\t      e_ident[EI_DATA] == ELFDATA2MSB) ||\n+\t    !(e_ident[EI_DATA] == (little_endian ? ELFDATA2LSB : ELFDATA2MSB)))\n+\t\treturn -1;\n+\n+\treturn 0;\n+}\n+\n+template<class elfHeader, class secHeader, class symHeader>\n+int LibLoader::loadSymbol(struct LibLoader::IPAModuleInfo *dst, int fd,\n+\t\t\t  const std::string &symbol)\n+{\n+\tunsigned long soSize = lseek(fd, 0, SEEK_END);\n+\tunsigned char *map = (unsigned char *)mmap(NULL, soSize, PROT_READ,\n+\t\t\t\t\t\t   MAP_PRIVATE, fd, 0);\n+\tif (map == MAP_FAILED)\n+\t\treturn errno;\n+\n+\telfHeader *eHdr = (elfHeader *)map;\n+\n+\tunsigned long shnameoff =\n+\t\t((secHeader *)(map + eHdr->e_shoff +\n+\t\t\t       eHdr->e_shentsize * eHdr->e_shstrndx))\n+\t\t\t->sh_offset;\n+\n+\t// walk section header table, find .dynsym\n+\tbool found = false;\n+\tsecHeader *dynsym;\n+\tfor (unsigned int i = 0; i < eHdr->e_shnum; i++) {\n+\t\tunsigned long i_shoff = eHdr->e_shoff + eHdr->e_shentsize * i;\n+\t\tsecHeader *sHdr = (secHeader *)(map + i_shoff);\n+\n+\t\tchar *name = (char *)map + shnameoff + sHdr->sh_name;\n+\n+\t\tif (sHdr->sh_type == SHT_DYNSYM && !strcmp(name, \".dynsym\")) {\n+\t\t\tfound = true;\n+\t\t\tdynsym = sHdr;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tif (!found)\n+\t\treturn -1;\n+\n+\tunsigned long dynsym_nameoff =\n+\t\t((secHeader *)(map + eHdr->e_shoff +\n+\t\t\t       eHdr->e_shentsize * dynsym->sh_link))\n+\t\t\t->sh_offset;\n+\n+\t// walk dynsym symbol table, find desired symbol\n+\tfound = false;\n+\tsymHeader *target_symbol;\n+\tunsigned int dynsym_num = dynsym->sh_size / dynsym->sh_entsize;\n+\tfor (unsigned int i = 0; i < dynsym_num; i++) {\n+\t\tunsigned long i_symoff = dynsym->sh_offset + dynsym->sh_entsize * i;\n+\t\tsymHeader *sym = (symHeader *)(map + i_symoff);\n+\n+\t\tchar *name = (char *)map + dynsym_nameoff + sym->st_name;\n+\n+\t\tif (!strcmp(name, symbol.c_str()) &&\n+\t\t    sym->st_info & STB_GLOBAL &&\n+\t\t    sym->st_size == sizeof(struct IPAModuleInfo)) {\n+\t\t\tfound = true;\n+\t\t\ttarget_symbol = sym;\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tif (!found)\n+\t\treturn -1;\n+\n+\tsecHeader *target_section =\n+\t\t(secHeader *)(map + eHdr->e_shoff +\n+\t\t\t      target_symbol->st_shndx * eHdr->e_shentsize);\n+\tunsigned long final_addr = target_section->sh_offset +\n+\t\t\t\t   (target_symbol->st_value - target_section->sh_addr);\n+\tmemcpy(dst, map + final_addr, sizeof(struct IPAModuleInfo));\n+\n+\treturn 0;\n+}\n+\n+int LibLoader::loadIPAModuleInfo(const std::string &symbol)\n+{\n+\tint fd = open(lib_path_.c_str(), O_RDONLY);\n+\tif (fd < 0)\n+\t\treturn fd;\n+\n+\tint ret = loadElfIdent(fd);\n+\tif (ret)\n+\t\tgoto close;\n+\n+\tif (bitclass_ == ELFCLASS32)\n+\t\tret = loadSymbol<Elf32_Ehdr, Elf32_Shdr, Elf32_Sym>(&info_, fd, symbol);\n+\telse if (bitclass_ == ELFCLASS64)\n+\t\tret = loadSymbol<Elf64_Ehdr, Elf64_Shdr, Elf64_Sym>(&info_, fd, symbol);\n+\telse\n+\t\tret = -1;\n+\tif (ret)\n+\t\tgoto close;\n+\n+\tloaded_ = true;\n+\n+close:\n+\tclose(fd);\n+\treturn ret;\n+}\n+\n+bool LibLoader::isLoaded() const\n+{\n+\treturn loaded_;\n+}\n+\n+struct LibLoader::IPAModuleInfo LibLoader::getIPAModuleInfo() const\n+{\n+\treturn info_;\n+}\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex 8796f49..4e7ab10 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -10,6 +10,7 @@ libcamera_sources = files([\n     'event_notifier.cpp',\n     'formats.cpp',\n     'geometry.cpp',\n+    'lib_loader.cpp',\n     'log.cpp',\n     'media_device.cpp',\n     'media_object.cpp',\n@@ -31,6 +32,7 @@ libcamera_headers = files([\n     'include/device_enumerator_udev.h',\n     'include/event_dispatcher_poll.h',\n     'include/formats.h',\n+    'include/lib_loader.h',\n     'include/log.h',\n     'include/media_device.h',\n     'include/media_object.h',\n",
    "prefixes": [
        "libcamera-devel",
        "1/2"
    ]
}