From patchwork Fri Jun 5 15:09:15 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Umang Jain X-Patchwork-Id: 3955 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 36E92603C6 for ; Fri, 5 Jun 2020 17:09:17 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=uajain.com header.i=@uajain.com header.b="e7lGb34b"; 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=5URSrVwfItFKLaIVPc0Od4f5rDegonIvdwSZWkaMtwQ=; b=e7lGb34bkyKhvVSeleYsZUbaDJGVqLDU0W9BEzzyS2ZHAqnwbnHi+icw96EebPdQLW9H F+zY+ruvtcUiKHsuwPwK8Ta63ER4w/dDQ12Y+U1DPNjNvek/lc3+4Fwf0oem5zS7L/h6Ke vWsmC6IWY/HtLWNrDtN45NIibyO0rhb6s= Received: by filterdrecv-p3mdw1-6f5df8956d-kv7d9 with SMTP id filterdrecv-p3mdw1-6f5df8956d-kv7d9-18-5EDA601B-BE 2020-06-05 15:09:15.822666942 +0000 UTC m=+154531.627509264 Received: from mail.uajain.com (unknown) by ismtpd0001p1hnd1.sendgrid.net (SG) with ESMTP id LG82QwtEQbe3u7aRRDM7Jw Fri, 05 Jun 2020 15:09:15.363 +0000 (UTC) From: Umang Jain Date: Fri, 05 Jun 2020 15:09:15 +0000 (UTC) Message-Id: <20200605150858.116564-2-email@uajain.com> In-Reply-To: <20200605150858.116564-1-email@uajain.com> References: <20200605150858.116564-1-email@uajain.com> Mime-Version: 1.0 X-SG-EID: 1Q40EQ7YGir8a9gjSIAdTjhngY657NMk9ckeo4dbHZDiOpywc/L3L9rFqlwE4KPcQ/i1ji5k4eTSSnQ1oTeZ4rVORBi2+GzXl6lq1M8d1q8gY7dYCQcM1ji7xSIbstvM4si/OMaU2pcr7gdFwP8GdbYPBJLEH3wVW245GwRx9/o3nsqum/7vPRKSFYGRbpM3ZeS/J2je18jF9hfnWF65dkrfFyc5fRqdm5tRAc4eNDkmcoCHrKJXnvusZWeMf5xOa8mBgJN4JBVx8iTmJ3AJrg== To: laurent.pinchart@ideasonboard.com, libcamera-devel@lists.libcamera.org Subject: [libcamera-devel] [PATCH v2 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: Fri, 05 Jun 2020 15:09:17 -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..f54dd8b 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, unsigned int 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 {};