From 9893ff92c453d891c6e7364a59a45a9a6f8fa12c Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Sun, 29 Mar 2020 18:49:50 +0300 Subject: libcamera: ipa_module: Use Span class to tie data and size MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The IPAModule class passes pointers to data and the corresponding size as thwo different variables to several functions. Tie them together in a Span. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- src/libcamera/ipa_module.cpp | 87 ++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/libcamera/ipa_module.cpp b/src/libcamera/ipa_module.cpp index d1c411e1..5b6af15f 100644 --- a/src/libcamera/ipa_module.cpp +++ b/src/libcamera/ipa_module.cpp @@ -18,9 +18,10 @@ #include #include #include -#include #include +#include + #include "file.h" #include "log.h" #include "pipeline_handler.h" @@ -43,27 +44,26 @@ LOG_DEFINE_CATEGORY(IPAModule) namespace { template -typename std::remove_extent_t *elfPointer(void *map, off_t offset, - size_t fileSize, size_t objSize) +typename std::remove_extent_t *elfPointer(Span elf, off_t offset, + size_t objSize) { size_t size = offset + objSize; - if (size > fileSize || size < objSize) + if (size > elf.size() || size < objSize) return nullptr; return reinterpret_cast *> - (static_cast(map) + offset); + (reinterpret_cast(elf.data()) + offset); } template -typename std::remove_extent_t *elfPointer(void *map, off_t offset, - size_t fileSize) +typename std::remove_extent_t *elfPointer(Span elf, off_t offset) { - return elfPointer(map, offset, fileSize, sizeof(T)); + return elfPointer(elf, offset, sizeof(T)); } -int elfVerifyIdent(void *map, size_t soSize) +int elfVerifyIdent(Span elf) { - char *e_ident = elfPointer(map, 0, soSize); + char *e_ident = elfPointer(elf, 0); if (!e_ident) return -ENOEXEC; @@ -89,38 +89,36 @@ int elfVerifyIdent(void *map, size_t soSize) /** * \brief Retrieve address and size of a symbol from an mmap'ed ELF file - * \param[in] map Address of mmap'ed ELF file - * \param[in] soSize Size of mmap'ed ELF file (in bytes) + * \param[in] elf Address and size of mmap'ed ELF file * \param[in] symbol Symbol name * - * \return zero or error code, address or nullptr, size of symbol or zero, - * respectively + * \return The memory region storing the symbol on success, or an empty span + * otherwise */ -std::tuple -elfLoadSymbol(void *map, size_t soSize, const char *symbol) +Span elfLoadSymbol(Span elf, const char *symbol) { - ElfW(Ehdr) *eHdr = elfPointer(map, 0, soSize); + ElfW(Ehdr) *eHdr = elfPointer(elf, 0); if (!eHdr) - return std::make_tuple(nullptr, 0); + return {}; off_t offset = eHdr->e_shoff + eHdr->e_shentsize * eHdr->e_shstrndx; - ElfW(Shdr) *sHdr = elfPointer(map, offset, soSize); + ElfW(Shdr) *sHdr = elfPointer(elf, offset); if (!sHdr) - return std::make_tuple(nullptr, 0); + return {}; off_t shnameoff = sHdr->sh_offset; /* Locate .dynsym section header. */ ElfW(Shdr) *dynsym = nullptr; for (unsigned int i = 0; i < eHdr->e_shnum; i++) { offset = eHdr->e_shoff + eHdr->e_shentsize * i; - sHdr = elfPointer(map, offset, soSize); + sHdr = elfPointer(elf, offset); if (!sHdr) - return std::make_tuple(nullptr, 0); + return {}; offset = shnameoff + sHdr->sh_name; - char *name = elfPointer(map, offset, soSize); + char *name = elfPointer(elf, offset); if (!name) - return std::make_tuple(nullptr, 0); + return {}; if (sHdr->sh_type == SHT_DYNSYM && !strcmp(name, ".dynsym")) { dynsym = sHdr; @@ -130,13 +128,13 @@ elfLoadSymbol(void *map, size_t soSize, const char *symbol) if (dynsym == nullptr) { LOG(IPAModule, Error) << "ELF has no .dynsym section"; - return std::make_tuple(nullptr, 0); + return {}; } offset = eHdr->e_shoff + eHdr->e_shentsize * dynsym->sh_link; - sHdr = elfPointer(map, offset, soSize); + sHdr = elfPointer(elf, offset); if (!sHdr) - return std::make_tuple(nullptr, 0); + return {}; off_t dynsym_nameoff = sHdr->sh_offset; /* Locate symbol in the .dynsym section. */ @@ -144,15 +142,14 @@ elfLoadSymbol(void *map, size_t soSize, const char *symbol) unsigned int dynsym_num = dynsym->sh_size / dynsym->sh_entsize; for (unsigned int i = 0; i < dynsym_num; i++) { offset = dynsym->sh_offset + dynsym->sh_entsize * i; - ElfW(Sym) *sym = elfPointer(map, offset, soSize); + ElfW(Sym) *sym = elfPointer(elf, offset); if (!sym) - return std::make_tuple(nullptr, 0); + return {}; offset = dynsym_nameoff + sym->st_name; - char *name = elfPointer(map, offset, soSize, - strlen(symbol) + 1); + char *name = elfPointer(elf, offset, strlen(symbol) + 1); if (!name) - return std::make_tuple(nullptr, 0); + return {}; if (!strcmp(name, symbol) && sym->st_info & STB_GLOBAL) { @@ -163,22 +160,22 @@ elfLoadSymbol(void *map, size_t soSize, const char *symbol) if (targetSymbol == nullptr) { LOG(IPAModule, Error) << "Symbol " << symbol << " not found"; - return std::make_tuple(nullptr, 0); + return {}; } /* Locate and return data of symbol. */ if (targetSymbol->st_shndx >= eHdr->e_shnum) - return std::make_tuple(nullptr, 0); + return {}; offset = eHdr->e_shoff + targetSymbol->st_shndx * eHdr->e_shentsize; - sHdr = elfPointer(map, offset, soSize); + sHdr = elfPointer(elf, offset); if (!sHdr) - return std::make_tuple(nullptr, 0); + return {}; offset = sHdr->sh_offset + (targetSymbol->st_value - sHdr->sh_addr); - char *data = elfPointer(map, offset, soSize, targetSymbol->st_size); + uint8_t *data = elfPointer(elf, offset, targetSymbol->st_size); if (!data) - return std::make_tuple(nullptr, 0); + return {}; - return std::make_tuple(data, targetSymbol->st_size); + return { data, targetSymbol->st_size }; } } /* namespace */ @@ -292,23 +289,19 @@ int IPAModule::loadIPAModuleInfo() } Span data = file.map(0, -1, File::MapPrivate); - int ret = elfVerifyIdent(data.data(), data.size()); + int ret = elfVerifyIdent(data); if (ret) { LOG(IPAModule, Error) << "IPA module is not an ELF file"; return ret; } - void *info = nullptr; - size_t infoSize; - - std::tie(info, infoSize) = elfLoadSymbol(data.data(), data.size(), - "ipaModuleInfo"); - if (!info || infoSize != sizeof(info_)) { + Span info = elfLoadSymbol(data, "ipaModuleInfo"); + if (info.size() != sizeof(info_)) { LOG(IPAModule, Error) << "IPA module has no valid info"; return -EINVAL; } - memcpy(&info_, info, infoSize); + memcpy(&info_, info.data(), info.size()); if (info_.moduleAPIVersion != IPA_MODULE_API_VERSION) { LOG(IPAModule, Error) << "IPA module API version mismatch"; -- cgit v1.2.1