From patchwork Mon Jun 14 10:07:35 2021 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: 43845 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 56242395100D for ; Mon, 14 Jun 2021 10:08:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 56242395100D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1623665308; bh=Tk+zBGwlOn+bFm8HeRmxCerLqhY5eP0Dc0cht2N+2ow=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Help: List-Subscribe:From:Reply-To:Cc:From; b=wqi55/OIvD/l0cegQ7AXAWAE3wN/o42xxhWk0PGULyYpc0oHLLizshmrYbTBhiEhw Lny1nkZ6veIyUeDZjHPrYgFmCELUK30/iAA+JKKc06nGKrDjUTwbC7PqSy5MLBzO9Q XLQnFPTEWtjsOlnZhSTkFvSfYwt4IJy5tsugVJSg= X-Original-To: libabigail@sourceware.org Delivered-To: libabigail@sourceware.org Received: from mail-qv1-xf49.google.com (mail-qv1-xf49.google.com [IPv6:2607:f8b0:4864:20::f49]) by sourceware.org (Postfix) with ESMTPS id E5B57385802D for ; Mon, 14 Jun 2021 10:08:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E5B57385802D Received: by mail-qv1-xf49.google.com with SMTP id n3-20020a0cee630000b029020e62abfcbdso23218850qvs.16 for ; Mon, 14 Jun 2021 03:08:24 -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=Tk+zBGwlOn+bFm8HeRmxCerLqhY5eP0Dc0cht2N+2ow=; b=nU+dfU1R1P2FXr5Mt4FYXj67cTu5m1p5UuomjeFgpOD33NrwW9cD2SqclDOnFfiH01 +vgPvdWZZXeAbMOyhRc9OdCLVVeL6MOIlzCY6nWY9HBbSNG0ljJC97QsS9oVL8IUVioK j+EuE6q2vyvMioFbFpJgnUcACJVvzPqZNuQnGJYMSmJfDL9xk5TIXKiURIgFR9vuyjrd GkeuRvrjcGnU6za4wRBTdMabsfR8l9lDwFrr1a35dzl3PTEb4WM8gefJXC56uE1frt4R aQ0Kbd6grvNlCRqkNaPGvc4mPbx9yfCQ2gizxT9PzX6A+MzDx7b+XJALDkatVyfN7QSt D8ZQ== X-Gm-Message-State: AOAM530Dli3e1THCeTfml3TJX9n0c1zuNNyPaQwo6GFGVHNulYHWKpOZ KLCLV/UBzT1fysjZQD3lpclyPbBtp2dSPIvuYyq105d8WScHmPb/0XPhNKaenSNUyCwdxbuPYNj 52/Z71wjJ62kbyjVDiRytk7qY3gpKZ5z7BwA8xkTyqij/d6+AgatTDYg4LeU5blh7ef5PBr8= X-Google-Smtp-Source: ABdhPJxzr+7ADWD7YRDqhqY67hhYqFc+XmCO3KtiosMcg4AG7zsj6YNYZ/2IYMtvuel/YSiMGTO4LaIialGpcw== X-Received: from lux.lon.corp.google.com ([2a00:79e0:d:210:5a41:80b:e50f:3291]) (user=maennich job=sendgmr) by 2002:a0c:d7c4:: with SMTP id g4mr881933qvj.23.1623665304479; Mon, 14 Jun 2021 03:08:24 -0700 (PDT) Date: Mon, 14 Jun 2021 11:07:35 +0100 Message-Id: <20210614100736.1665072-1-maennich@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.32.0.272.g935e593368-goog Subject: [PATCH 1/2] elf-helpers: refactor find_symbol_table_section To: libabigail@sourceware.org X-Spam-Status: No, score=-22.2 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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, kernel-team@android.com Errors-To: libabigail-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libabigail" Refactor the acquisition of symtabs to explicitly provide functionality to get the .symtab and .dynsym sections. A later patch will make use of that to acquire .symtab while find_symbol_table_section() still provides .dynsym as default symbol table. This also adds a new overload to find_section to acquire the first section by type and adjusts find_symbol_table_section() to make use of those functions. * src/abg-elf-helpers.cc(find_section): New overload. (find_symtab_section): New function. (find_dynsym_section): New function. (find_symbol_table_section): Use new find_*_section functions. * src/abg-elf-helpers.h(find_section): New overload declaration. (find_symtab_section): New function declaration. (find_dynsym_section): New function declaration. Reviewed-by: Giuliano Procida Signed-off-by: Matthias Maennich --- src/abg-elf-helpers.cc | 59 ++++++++++++++++++++++++++++++++++-------- src/abg-elf-helpers.h | 9 +++++++ 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/src/abg-elf-helpers.cc b/src/abg-elf-helpers.cc index 480381dec77e..998675a6059a 100644 --- a/src/abg-elf-helpers.cc +++ b/src/abg-elf-helpers.cc @@ -331,6 +331,51 @@ find_section(Elf* elf_handle, const std::string& name, Elf64_Word section_type) return 0; } +/// Find and return a section by its type. +/// +/// @param elf_handle the elf handle to use. +/// +/// @param section_type the type of the section. This is the +/// Elf32_Shdr::sh_type (or Elf64_Shdr::sh_type) data member. +/// Examples of values of this parameter are SHT_PROGBITS or SHT_NOBITS. +/// +/// @return the section found, or nil if none was found. +Elf_Scn* +find_section(Elf* elf_handle, Elf64_Word section_type) +{ + Elf_Scn* section = nullptr; + while ((section = elf_nextscn(elf_handle, section)) != 0) + { + GElf_Shdr header_mem, *header; + header = gelf_getshdr(section, &header_mem); + if (header->sh_type == section_type) + break; + } + return section; +} + +/// Find and return the .symtab section +/// +/// @param elf_handle the elf handle to use. +/// +/// @return the section found, or nil if none was found +Elf_Scn* +find_symtab_section(Elf* elf_handle) +{ + return find_section(elf_handle, SHT_SYMTAB); +} + +/// Find and return the .symtab section +/// +/// @param elf_handle the elf handle to use. +/// +/// @return the section found, or nil if none was found +Elf_Scn* +find_dynsym_section(Elf* elf_handle) +{ + return find_section(elf_handle, SHT_DYNSYM); +} + /// Find the symbol table. /// /// If we are looking at a relocatable or executable file, this @@ -346,16 +391,8 @@ find_section(Elf* elf_handle, const std::string& name, Elf64_Word section_type) Elf_Scn* find_symbol_table_section(Elf* elf_handle) { - Elf_Scn* section = 0, *dynsym = 0, *sym_tab = 0; - while ((section = elf_nextscn(elf_handle, section)) != 0) - { - GElf_Shdr header_mem, *header; - header = gelf_getshdr(section, &header_mem); - if (header->sh_type == SHT_DYNSYM) - dynsym = section; - else if (header->sh_type == SHT_SYMTAB) - sym_tab = section; - } + Elf_Scn *dynsym = find_dynsym_section(elf_handle), + *sym_tab = find_symtab_section(elf_handle); if (dynsym || sym_tab) { @@ -367,7 +404,7 @@ find_symbol_table_section(Elf* elf_handle) else return dynsym ? dynsym : sym_tab; } - return NULL; + return nullptr; } /// Find the index (in the section headers table) of the symbol table diff --git a/src/abg-elf-helpers.h b/src/abg-elf-helpers.h index 96e03d26ccad..59ea0a74e45c 100644 --- a/src/abg-elf-helpers.h +++ b/src/abg-elf-helpers.h @@ -49,6 +49,15 @@ find_section(Elf* elf_handle, const std::string& name, Elf64_Word section_type); +Elf_Scn* +find_section(Elf* elf_handle, Elf64_Word section_type); + +Elf_Scn* +find_symtab_section(Elf* elf_handle); + +Elf_Scn* +find_dynsym_section(Elf* elf_handle); + Elf_Scn* find_symbol_table_section(Elf* elf_handle);