From patchwork Sun Jun 7 14:30:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 3978 Return-Path: Received: from o1.f.az.sendgrid.net (o1.f.az.sendgrid.net [208.117.55.132]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A33AC600F7 for ; Sun, 7 Jun 2020 16:30:19 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=uajain.com header.i=@uajain.com header.b="F+7phBrX"; dkim-atps=neutral DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=uajain.com; h=from:subject:in-reply-to:references:mime-version:to:cc: content-transfer-encoding:content-type; s=s1; bh=2jabfmk5QR7Db/ZzwTlL0K8EIHd6SdbXBkLwGfoEG7s=; b=F+7phBrXBYkwTX8PE9syy2FDZhtQNL9LFU0/e8Z/O5lhFty/S6Z0wSy8EJKZTEy4j7i3 9C5LZFCAQEgZ2BJUgxt1oS7/GGWuunu6T1FUmlOIXyCVcpE67V3p+Gh5064rHDM34ZASSo ZoLcGmBD8jN1wU1r2QTIFepKgDUZTiCn4= Received: by filterdrecv-p3mdw1-6f5df8956d-8rppr with SMTP id filterdrecv-p3mdw1-6f5df8956d-8rppr-20-5EDCF9FA-46 2020-06-07 14:30:18.473731592 +0000 UTC m=+324990.134891972 Received: from mail.uajain.com (unknown) by ismtpd0008p1maa1.sendgrid.net (SG) with ESMTP id JS1To19sQaaSs9PR6LSkpQ Sun, 07 Jun 2020 14:30:17.787 +0000 (UTC) From: Umang Jain Date: Sun, 07 Jun 2020 14:30:18 +0000 (UTC) Message-Id: <20200607143012.17752-2-email@uajain.com> In-Reply-To: <20200607143012.17752-1-email@uajain.com> References: <20200607143012.17752-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcGsFXrTC3nkaVUz7y/MXE3aIrlXxsCQ5m4ALiEPD7h7ovoYuI8rMAdRC0ZWithFTpYl7lK6P+7uq966qmJ9nDdeZ9aZ7jWmIbf9goYdnYdqiyyV8o9ESSKrU3mkJc4w3ibxSRTXOED1Je4hnpHyLxaybSH+01mZTLZOqZN9IEIFMDY1kGl442ZQQzkkfmEl9lai45zAzGZGjzD3itEisnYg== To: laurent.pinchart@ideasonboard.com, libcamera-devel@lists.libcamera.org, kieran.bingham@ideasonboard.com Subject: [libcamera-devel] [PATCH v3 1/2] libcamera: ipa_module: Share code to find section header of ELF header X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 07 Jun 2020 14:30:20 -0000 Refactor the code to find section into a common helper function. This commit introduces no functional changes. Signed-off-by: Umang Jain Reviewed-by: Laurent Pinchart --- src/libcamera/ipa_module.cpp | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/libcamera/ipa_module.cpp b/src/libcamera/ipa_module.cpp index 045c8fc..60aaa34 100644 --- a/src/libcamera/ipa_module.cpp +++ b/src/libcamera/ipa_module.cpp @@ -88,6 +88,15 @@ int elfVerifyIdent(Span elf) return 0; } +ElfW(Shdr) *elfSection(Span elf, ElfW(Ehdr) *eHdr, ElfW(Half) idx) +{ + if (idx >= eHdr->e_shnum) + return nullptr; + + off_t offset = eHdr->e_shoff + idx * eHdr->e_shentsize; + return elfPointer(elf, offset); +} + /** * \brief Retrieve address and size of a symbol from an mmap'ed ELF file * \param[in] elf Address and size of mmap'ed ELF file @@ -102,8 +111,7 @@ Span elfLoadSymbol(Span elf, const char *symbol) if (!eHdr) return {}; - off_t offset = eHdr->e_shoff + eHdr->e_shentsize * eHdr->e_shstrndx; - ElfW(Shdr) *sHdr = elfPointer(elf, offset); + ElfW(Shdr) *sHdr = elfSection(elf, eHdr, eHdr->e_shstrndx); if (!sHdr) return {}; off_t shnameoff = sHdr->sh_offset; @@ -111,12 +119,11 @@ Span elfLoadSymbol(Span elf, const char *symbol) /* 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(elf, offset); + sHdr = elfSection(elf, eHdr, i); if (!sHdr) return {}; - offset = shnameoff + sHdr->sh_name; + off_t offset = shnameoff + sHdr->sh_name; char *name = elfPointer(elf, offset); if (!name) return {}; @@ -132,8 +139,7 @@ Span elfLoadSymbol(Span elf, const char *symbol) return {}; } - offset = eHdr->e_shoff + eHdr->e_shentsize * dynsym->sh_link; - sHdr = elfPointer(elf, offset); + sHdr = elfSection(elf, eHdr, dynsym->sh_link); if (!sHdr) return {}; off_t dynsym_nameoff = sHdr->sh_offset; @@ -142,7 +148,7 @@ Span elfLoadSymbol(Span elf, const char *symbol) ElfW(Sym) *targetSymbol = nullptr; 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; + off_t offset = dynsym->sh_offset + dynsym->sh_entsize * i; ElfW(Sym) *sym = elfPointer(elf, offset); if (!sym) return {}; @@ -165,13 +171,10 @@ Span elfLoadSymbol(Span elf, const char *symbol) } /* Locate and return data of symbol. */ - if (targetSymbol->st_shndx >= eHdr->e_shnum) - return {}; - offset = eHdr->e_shoff + targetSymbol->st_shndx * eHdr->e_shentsize; - sHdr = elfPointer(elf, offset); + sHdr = elfSection(elf, eHdr, targetSymbol->st_shndx); if (!sHdr) return {}; - offset = sHdr->sh_offset + (targetSymbol->st_value - sHdr->sh_addr); + off_t offset = sHdr->sh_offset + (targetSymbol->st_value - sHdr->sh_addr); uint8_t *data = elfPointer(elf, offset, targetSymbol->st_size); if (!data) return {};