From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 484E4C433F5 for ; Sat, 25 Sep 2021 00:36:14 +0000 (UTC) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id BB8606125F for ; Sat, 25 Sep 2021 00:36:13 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org BB8606125F Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=lists.denx.de Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 19AF48354E; Sat, 25 Sep 2021 02:35:23 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="Ndc0eCef"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 77003834FA; Sat, 25 Sep 2021 02:32:16 +0200 (CEST) Received: from mail-ot1-x32a.google.com (mail-ot1-x32a.google.com [IPv6:2607:f8b0:4864:20::32a]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 8B887834F4 for ; Sat, 25 Sep 2021 02:31:21 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-ot1-x32a.google.com with SMTP id x33-20020a9d37a4000000b0054733a85462so15445346otb.10 for ; Fri, 24 Sep 2021 17:31:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=VD5k/ZwIjufQBHidzKdcuaK428yhHDdNShrDGwWowIg=; b=Ndc0eCef6iRDs1pUVRjbhQ4XetltWojXal6TO6E9pv41eZr9+BDJ9gYVtpd3wYLmjK x/Ira6HfyqNDEdo5DynBSZe08nK5C5EoEG87qhzRqjGBDcJfGbezHh0eN8YlfMNBOKgJ wGtnMVKVm9ntIWhI1x2JBCe4YIc7LuzUXp+yo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=VD5k/ZwIjufQBHidzKdcuaK428yhHDdNShrDGwWowIg=; b=xTlJwhxrjtGOt+sefRfcvzv8tnj2EhxgVtDDiTec+ahBkojst5Cmm87WS93t8VDttN enDTdzznUX85KHymlLcR/hN/tE2u7lSsKqX4niYeTkQMW1x39dgBT1fmwnGsV/LQZm1D GiTgW9lier492DB/V+Y0XT2h5NVzjWyBgKk0N/I/pfbOFm6V5bYnzEWfrU+F1yGj7A6l ScuGvnFj9ODThQUQK6tssUe2tPwQCiQrr9vRVrQSGyK9z3ti6NDipsD0okx2UXSfyVWT ZfYnkCESRBhV4oHRyKfiUBl2FTae+0v2CrDXvsMlR/hgNBkQmeXb0PweNME1WkAVSNEj 1aOw== X-Gm-Message-State: AOAM533abGw+wgpGsoY+M1It2Y7XWxQTXVhAukoU8SiHRSGt0pFL/7jP kTNg2e0+/XZefoyZs19f5dfjSrt4ABeAhA== X-Google-Smtp-Source: ABdhPJzKRq8d/ypJGt4okuvmfdDajQrsnU4y17F7uO7h5z8b5D+1BUJztKN+aeTHxNnCIXlNtG3sPg== X-Received: by 2002:a05:6830:43a0:: with SMTP id s32mr6685552otv.284.1632529879778; Fri, 24 Sep 2021 17:31:19 -0700 (PDT) Received: from kiwi.bld.corp.google.com (c-67-190-101-114.hsd1.co.comcast.net. [67.190.101.114]) by smtp.gmail.com with ESMTPSA id y83sm2491462oia.47.2021.09.24.17.31.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Sep 2021 17:31:19 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Heinrich Schuchardt , Bin Meng , Christian Melki , Tom Rini , Ilias Apalodimas , Simon Glass Subject: [PATCH v2 20/39] binman: Support reading the offset of an ELF-file symbol Date: Fri, 24 Sep 2021 18:30:36 -0600 Message-Id: <20210925003055.759305-8-sjg@chromium.org> X-Mailer: git-send-email 2.33.0.685.g46640cef36-goog In-Reply-To: <20210925003055.759305-1-sjg@chromium.org> References: <20210925003055.759305-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.2 at phobos.denx.de X-Virus-Status: Clean Binman needs to be able to update the contents of an ELF file after it has been build. To support this, add a function to locate the position of a symbol's contents within the file. Fix the comments on bss_data.c and Symbol while we are here. Signed-off-by: Simon Glass --- (no changes since v1) tools/binman/elf.py | 53 ++++++++++++++++++++++++++++++-- tools/binman/elf_test.py | 37 ++++++++++++++++++++++ tools/binman/test/Makefile | 5 ++- tools/binman/test/bss_data.c | 2 +- tools/binman/test/embed_data.c | 16 ++++++++++ tools/binman/test/embed_data.lds | 23 ++++++++++++++ 6 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 tools/binman/test/embed_data.c create mode 100644 tools/binman/test/embed_data.lds diff --git a/tools/binman/elf.py b/tools/binman/elf.py index 03b49d7163c..4aca4f847ce 100644 --- a/tools/binman/elf.py +++ b/tools/binman/elf.py @@ -24,7 +24,14 @@ try: except: # pragma: no cover ELF_TOOLS = False -Symbol = namedtuple('Symbol', ['section', 'address', 'size', 'weak']) +# Information about an EFL symbol: +# section (str): Name of the section containing this symbol +# address (int): Address of the symbol (its value) +# size (int): Size of the symbol in bytes +# weak (bool): True if the symbol is weak +# offset (int or None): Offset of the symbol's data in the ELF file, or None if +# not known +Symbol = namedtuple('Symbol', ['section', 'address', 'size', 'weak', 'offset']) # Information about an ELF file: # data: Extracted program contents of ELF file (this would be loaded by an @@ -71,8 +78,48 @@ def GetSymbols(fname, patterns): section, size = parts[:2] if len(parts) > 2: name = parts[2] if parts[2] != '.hidden' else parts[3] - syms[name] = Symbol(section, int(value, 16), int(size,16), - flags[1] == 'w') + syms[name] = Symbol(section, int(value, 16), int(size, 16), + flags[1] == 'w', None) + + # Sort dict by address + return OrderedDict(sorted(syms.items(), key=lambda x: x[1].address)) + +def GetSymbolFileOffset(fname, patterns): + """Get the symbols from an ELF file + + Args: + fname: Filename of the ELF file to read + patterns: List of regex patterns to search for, each a string + + Returns: + None, if the file does not exist, or Dict: + key: Name of symbol + value: Hex value of symbol + """ + def _GetFileOffset(elf, addr): + for seg in elf.iter_segments(): + seg_end = seg['p_vaddr'] + seg['p_filesz'] + if seg.header['p_type'] == 'PT_LOAD': + if addr >= seg['p_vaddr'] and addr < seg_end: + return addr - seg['p_vaddr'] + seg['p_offset'] + + if not ELF_TOOLS: + raise ValueError('Python elftools package is not available') + + syms = {} + with open(fname, 'rb') as fd: + elf = ELFFile(fd) + + re_syms = re.compile('|'.join(patterns)) + for section in elf.iter_sections(): + if isinstance(section, SymbolTableSection): + for symbol in section.iter_symbols(): + if not re_syms or re_syms.search(symbol.name): + addr = symbol.entry['st_value'] + syms[symbol.name] = Symbol( + section.name, addr, symbol.entry['st_size'], + symbol.entry['st_info']['bind'] == 'STB_WEAK', + _GetFileOffset(elf, addr)) # Sort dict by address return OrderedDict(sorted(syms.items(), key=lambda x: x[1].address)) diff --git a/tools/binman/elf_test.py b/tools/binman/elf_test.py index bcccd78c0a1..ac69a95b654 100644 --- a/tools/binman/elf_test.py +++ b/tools/binman/elf_test.py @@ -6,6 +6,7 @@ import os import shutil +import struct import sys import tempfile import unittest @@ -221,6 +222,42 @@ class TestElf(unittest.TestCase): elf.DecodeElf(data, load + 2)) shutil.rmtree(outdir) + def testEmbedData(self): + """Test for the GetSymbolFileOffset() function""" + if not elf.ELF_TOOLS: + self.skipTest('Python elftools not available') + + fname = self.ElfTestFile('embed_data') + offset = elf.GetSymbolFileOffset(fname, ['embed_start', 'embed_end']) + start = offset['embed_start'].offset + end = offset['embed_end'].offset + data = tools.ReadFile(fname) + embed_data = data[start:end] + expect = struct.pack('