From patchwork Fri Jun 19 21:42:49 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Matthias_M=C3=A4nnich?= X-Patchwork-Id: 39694 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C150F389852E; Fri, 19 Jun 2020 21:43:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C150F389852E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1592602994; bh=CThd4rR2K+mTS5GKqUuZdSUKCIF0FSd2J25nWB0vvcQ=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Help: List-Subscribe:From:Reply-To:Cc:From; b=gTHdkhNuetCtxozNsjNDI8oVmhBLQRad3eocZrG+dhIRopE07gG9Bo0RHd6/hdV8U nB1hGWkQpaWaGQYehPNM1Dau5NG3z0JV8TAPxfdAOLPTjTOzO7hdAleEKIdRWFgBsF 8HialMOy1W45PmjG4PlnoHLL9xVcpT/CXGsQ3OkA= X-Original-To: libabigail@sourceware.org Delivered-To: libabigail@sourceware.org Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by sourceware.org (Postfix) with ESMTPS id 1B440388E813 for ; Fri, 19 Jun 2020 21:43:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 1B440388E813 Received: by mail-qv1-xf4a.google.com with SMTP id j4so7836180qvt.20 for ; Fri, 19 Jun 2020 14:43:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=CThd4rR2K+mTS5GKqUuZdSUKCIF0FSd2J25nWB0vvcQ=; b=gFxZYDITR/4dNf0eFutxVar3fEUfguqkMI8+9HvvSHGWDwoCifa5YUpOVrtR5ZkFJv NbAc259vBeUr4508a4BZ5WGDqepuSQjcU1iicN1cQ8DY3d7W8S7/Z6FPbDtDn6tTBZaB 5ntGRp/TIpTGXgKPn+OAKF0VBZHpSpirNLMUXdvu/k3IS/dFur4Aw5CJIv0uQvLI1I4C A0Qx3Bgqs6V/jN7JCxAeqDh70ZsS0z954rT/tf45V94yjQfI5Y4m0j+mhtvZ+6Y7rQMb UkZpzad96tiE8zyzxpNwC+SkL2KIKO+J6Q+Ml6CIIck8tbsY0rKkaAjqbmfc/pXGB8EC IMoA== X-Gm-Message-State: AOAM532OWSuYMnETlBu2Df5/VKCnidwPeyA3wbYkOXX9SBNBDlQ4U8T/ 4XgUUqAFwQzlX2derh71FeNLs1c4bebw5WMokvWvG7eXc24NzSZh93Xl5+TsL150rxw7Usa1VsK I3yQtP8fFdpVvZ1XPAhMD7IpSc0XmQ7swRXubxiOk4efpCUmzG2qP2OcqvKn0pPN/NShO5dY= X-Google-Smtp-Source: ABdhPJzxuKpcPHWrDkWZSlJEtWXk37fT8mKg3pG6HDIG7x4APzglylwOcJ1dViwuiBwbnYHoMIhKiFFqZj5NIQ== X-Received: by 2002:ad4:47aa:: with SMTP id a10mr11207454qvz.61.1592602991724; Fri, 19 Jun 2020 14:43:11 -0700 (PDT) Date: Fri, 19 Jun 2020 23:42:49 +0200 Message-Id: <20200619214305.562-1-maennich@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.27.0.111.gc72c7da667-goog Subject: [PATCH v1 00/16] Refactor (k)symtab reader To: libabigail@sourceware.org X-Spam-Status: No, score=-18.2 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libabigail@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Mailing list of the Libabigail project List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-Patchwork-Original-From: Matthias Maennich via Libabigail From: =?utf-8?q?Matthias_M=C3=A4nnich?= Reply-To: Matthias Maennich Cc: maennich@google.com, gprocida@google.com, kernel-team@android.com Errors-To: libabigail-bounces@sourceware.org Sender: "Libabigail" The current implementation that reads the symtab and the ksymtab has grown over time from simple symtab reading to way more complex ksymtab reading including taking care of little details like position relative relocations, symbol namespaces, etc. Yet, more features are coming to the Linux kernels that make this parsing even more tricky: Further changes to the ksymtab layout and different needs to lookup symbols caused by features like LTO (causing RELA relocations in the ksymtab entries) and CFI (causing additional jump table symbols) that are highly confusing the meaning of ksymtab entries and make it increasingly challenging for a static analysis tool like libabigail to properly process the ksymtab values. This added complexity also adds more and more responsibilities to the read_context that already has a lot of different tasks to juggle. It gets increasingly difficult to ensure, further development in the dwarf reader can be done without subtly regressing existing functionality. Hence, attempt a refactoring (one could argue: rewrite, but a lot of functionality is just migrated out) of the symtab reading code. The first 2 commits set up some prerequisites, like a partial backport of std::optional and enabling std::bind and friends. Commit 3 and 4 modify abg-ir's elf_symbol to be able to carry 'is_suppressed' and 'is_in_ksymtab'. Commit 5 and 6 implement the new symtab reader. The abg-symtab-reader has been introduced as an instance decoupled from dwarf readers' read_context. This reduces the responsibilities of the dwarf reader and separates the functionality into a new compilation unit. It contains several components to make the main component 'symtab' easy to access and to query. Refer to the extensive commit message there for details. The actual core of the symtab reading has been taken as a base, but refactored where useful. The ksymtab reading could be simplified by processing the corresponding __ksymtab_* entries directly from symtab without the need to interpret the binary ksymtab sections. That also resolves issues with wrong ksymtab reading: Mapping from the ksymtab symbol address to the symtab entry might leave us with a non-main symbol and hence leads to incorrect results. E.g. symbols like strlen are implemented as __pi_strlen and are aliases to strlen in the kernel. Only by reading the ksymtab entries we can decide which symbol to keep. Otherwise we get indeterministic results. Furthermore, symbol whitelists might list one or the other leading to issues of suppressed symbols for which we might just see the wrong symbol and therefore suppress both from analysis. In addition, detecting the format of the ksymtab, requires the first entry to be a valid elf_symbol, which is not the case if filtered out via whitelist or suppression. Finally, features like CFI require name based lookup into the ksymtab and LTO with clang on aarch64 might make the ksymtab contain relocatable entries. This is additional complexity hitting the dwarf reader. Those are subtle issues that motivated this series. Conceptionally, the new reader works quite similar. Except for the way suppressions are applied: Instead of discarding symbols while reading, we flag symbols as suppressed and keep them around for lookup purposes. That resolves issues when dealing with symbol aliases. Commit 7 integrates the new symtab reader into the existing code - side by side with the current implementation. Commits 8 - 12 migrate more and more symtab users over to the new symtab reader, including the ksymtab functionality in commit 12 where the old implementation could be obsoleted. Commits 13 and 14 re-add the ppc64 support for ELFv1 binaries. Commits 15 and 16 remove now obsolete functionality and remove the now old implementation. Performance testing has been done with an 'allmodconfig' kernel config. That is the worst case for kernels and representing the 'distribution kernel' use case. During those tests, no significant performance impact could be measured. In addition, various Android Kernels in various configurations have been tested with this. The earlier added tests for reading symtab and ksymtab obviously pass. Cheers, Matthias Matthias Maennich (16): abg-cxx-compat: add simplified version of std::optional abg-cxx-compat: more support: std::bind and friends abg-ir: elf_symbol: add is_in_ksymtab field abg-ir: elf_symbol: add is_suppressed field dwarf-reader split: create abg-symtab-reader.{h,cc} and test case Refactor ELF symbol table reading by adding a new symtab reader Integrate new symtab reader into corpus and read_context corpus: make get_(undefined_)?_(var|fun)_symbols use the new symtab corpus: make get_unreferenced_(function|variable)_symbols use the new symtab abg-reader: avoid using the (var|function)_symbol_map dwarf-reader: read_context: use new symtab in *_symbols_is_exported Switch kernel stuff over to new symtab and drop unused code abg-elf-helpers: migrate ppc64 specific helpers symtab_reader: add support for ppc64 ELFv1 binaries abg-corpus: remove symbol maps and their setters dwarf reader: drop (now) unused code related symbol table reading include/Makefile.am | 3 +- include/abg-corpus.h | 24 +- include/abg-cxx-compat.h | 100 + include/abg-dwarf-reader.h | 6 - include/abg-fwd.h | 8 + include/abg-ir.h | 42 +- include/abg-symtab-reader.h | 416 +++ src/Makefile.am | 1 + src/abg-corpus-priv.h | 57 +- src/abg-corpus.cc | 645 ++--- src/abg-dwarf-reader.cc | 2331 ++--------------- src/abg-elf-helpers.cc | 186 ++ src/abg-elf-helpers.h | 8 + src/abg-ir.cc | 120 +- src/abg-reader.cc | 43 +- src/abg-symtab-reader.cc | 436 +++ src/abg-tools-utils.cc | 13 - src/abg-writer.cc | 38 +- tests/Makefile.am | 4 + .../data/test-read-dwarf/PR25007-sdhci.ko.abi | 5 - tests/data/test-symtab/basic/no_debug_info.c | 2 +- tests/data/test-symtab/basic/no_debug_info.so | Bin 15360 -> 15544 bytes tests/test-cxx-compat.cc | 51 + tests/test-symtab-reader.cc | 53 + tests/test-symtab.cc | 30 +- tools/abidw.cc | 2 - 26 files changed, 1914 insertions(+), 2710 deletions(-) create mode 100644 include/abg-symtab-reader.h create mode 100644 src/abg-symtab-reader.cc create mode 100644 tests/test-symtab-reader.cc