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 X-Spam-Level: X-Spam-Status: No, score=-19.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id A393AC433F5 for ; Wed, 8 Sep 2021 13:38:02 +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 2D6B360295 for ; Wed, 8 Sep 2021 13:38:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 2D6B360295 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 D63DE83476; Wed, 8 Sep 2021 15:35:47 +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="R6QkvMvk"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0F3828338F; Wed, 8 Sep 2021 15:34:55 +0200 (CEST) Received: from mail-wr1-x42a.google.com (mail-wr1-x42a.google.com [IPv6:2a00:1450:4864:20::42a]) (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 2170983399 for ; Wed, 8 Sep 2021 15:34:41 +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-wr1-x42a.google.com with SMTP id g16so3372518wrb.3 for ; Wed, 08 Sep 2021 06:34:41 -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=r4rxl359MmQpydjD6Z2P22Xd0ULlGuLNauQLZPt6kTk=; b=R6QkvMvkol1obJGg2US7V1jDxhbKHD5iXxA7IcdAWzjJlkSVbLegIF8WZuU2uKWnWx ZRJb6NKUBLbOt06x86T+FVlmvHJnQrmQnENoZClOqf6NRPLzb/Kq1FJY9FRDgIJiBBN+ jrPfZJIbVRk5Cas2F170n0GcKl5b0rY6THIo0= 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=r4rxl359MmQpydjD6Z2P22Xd0ULlGuLNauQLZPt6kTk=; b=cKSM8MI/Q5nUGOvPheCeaizVMThri5M3GyJNDkg/IPA0EqiybFefvaWzuAmj5SSzuW aE59WUt0WNVFaUTx8XKIkJLXFkbD2sFAZ1n+eRG8MCMU0r1ypP3EBx4xtsZNjeyaPvq0 35U8YvDMjSm/FnaNZzUmdloUzMJHWWTUARaYXjB0r8pOkB6qd+si1skkt3AdKMblERJD L4WEZi//7Zu8CLwDE5yIk4i+SHyrJsD+HnWCn3pHcEYPxx4zv+senVwO2YApJnpGqYUQ Muc8WmfTZ1QI/5ThEQsUH3pyuUat1Lx0JQKnJfOTBKU5d+P1cNhgJZa9Vj2z6CP8axDa BBQQ== X-Gm-Message-State: AOAM532e4jr3j+JLZ76W39jaoVqp1heuu73nnWvlRJ9TNfBaRGi4GDcP SZYjvHynvx94yJfqjotU/CWlLjT7RpGRMtwg X-Google-Smtp-Source: ABdhPJzn0cx5TTg+m+a3QoQOSfrFNC4SqchxlT9MTfsnccX4dfaX+BB01xmhF9LDq5xBlkh7897t7Q== X-Received: by 2002:adf:80c8:: with SMTP id 66mr4247968wrl.65.1631108080291; Wed, 08 Sep 2021 06:34:40 -0700 (PDT) Received: from sjg1.roam.corp.google.com (82-69-54-180.dsl.in-addr.zen.co.uk. [82.69.54.180]) by smtp.gmail.com with ESMTPSA id l2sm2209065wmi.1.2021.09.08.06.34.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Sep 2021 06:34:39 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Ilias Apalodimas , Heinrich Schuchardt , Bin Meng , Tom Rini , Christian Melki , Simon Glass Subject: [PATCH 17/35] binman: Support reading the offset of an ELF-file symbol Date: Wed, 8 Sep 2021 07:33:47 -0600 Message-Id: <20210908133405.696481-5-sjg@chromium.org> X-Mailer: git-send-email 2.33.0.153.gba50c8fa24-goog In-Reply-To: <20210908133405.696481-1-sjg@chromium.org> References: <20210908133405.696481-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 --- 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('