From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: <3gpSeXggKCiEJ7BKKF9EDLLDIB.9LJIF878FD7FIPLRO9BT7OB.LOD@flex--maennich.bounces.google.com> 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 2C005384B0C1 for ; Tue, 21 Apr 2020 06:36:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 2C005384B0C1 Received: by mail-qv1-xf4a.google.com with SMTP id f4so12849842qvu.19 for ; Mon, 20 Apr 2020 23:36:51 -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:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=/1UmFixH/aqDF84bupRHs4joTvAwgG68/VVCwWD3THo=; b=UOzMRNhPoR2RqWMxVT8SaoOqqqMGzOC9z3owTa0o8lMULZm3vzSfhSJG08wQ7uEZZY g09lghEAKfJW9PXDwMwdm+l/HztCsKPvInt0kNTrbhLDAYlGddwLPhBit6te+vwF4ULV 5+KtMmFPsBOjcN+j4s3gEkYTQxyX5697Vvvd4Na0sB0xArxk+qJlevgHaQkxngu5GJCp +EnFCPkv/SMRz7hniTO9DbP6XgKVl7KvT0UeqmipCrWq3HVy17GNAzzz9GIuMzjw65Qb M2BQF2SjisFgtPVKN1w/N0dkHgjJxksmrIpby1pFTame+jCE9qVsLufT2cXaXC61i0/I waeQ== X-Gm-Message-State: AGi0PuYPYUyuDamZzHqNIt455nRpP7g/FJcgP1Nq94twzRhIEtyw2QMi ulmmvp+Rz5m9VzTLoP1GMraXKCK+QR8GtfvOMVq1Qw36YykHyvfnidvafZ+M92bx5eqrQeIIORI szWOa3HQ/wN/2OUKJyn5DRNu5BFemHfjysEjzNOm0L7y/+64FYPmrVHYzgpB/xl4dPgrr4OA= X-Google-Smtp-Source: APiQypIlJNAnisB+YxNOQu+//BHXaIqp8tiAkLlf9QIvECn3kiW9zSdDGoJYGDhGnEXGxjt5GJasI4wDX+osdw== X-Received: by 2002:ad4:4f01:: with SMTP id fb1mr19387095qvb.162.1587451010550; Mon, 20 Apr 2020 23:36:50 -0700 (PDT) Date: Tue, 21 Apr 2020 08:35:51 +0200 In-Reply-To: <20200421063551.222511-1-maennich@google.com> Message-Id: <20200421063551.222511-9-maennich@google.com> Mime-Version: 1.0 References: <20200420110846.218792-1-maennich@google.com> <20200421063551.222511-1-maennich@google.com> X-Mailer: git-send-email 2.26.1.301.g55bc3eb7cb9-goog Subject: [PATCH v2 8/8] abg-elf-helpers: migrate maybe_adjust_et_rel_sym_addr_to_abs_addr From: Matthias Maennich To: libabigail@sourceware.org Cc: dodji@seketeli.org, gprocida@google.com, kernel-team@android.com, maennich@google.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-32.8 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, 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: Libabigail mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 21 Apr 2020 06:36:52 -0000 Move maybe_adjust_et_rel_sym_addr_to_abs_addr to the elf-helpers. This function had two overloads GElf_Addr maybe_adjust_et_rel_sym_addr_to_abs_addr(GElf_Addr addr, Elf_Scn *section); GElf_Addr maybe_adjust_et_rel_sym_addr_to_abs_addr(GElf_Sym *sym); The former one is only ever called by the latter. Hence consolidate them into GElf_Addr maybe_adjust_et_rel_sym_addr_to_abs_addr(Elf* elf_handle, GElf_Sym* sym); to combine their functionality and preserve the outer interface. Just we add the Elf* handle argument as usual as we are out of read_context now. * src/abg-dwarf-reader.cc(maybe_adjust_et_rel_sym_addr_to_abs_addr): Move out functions (drop the wrapped overload completely). * src/abg-elf-helpers.cc(maybe_adjust_et_rel_sym_addr_to_abs_addr): New function. * src/abg-elf-helpers.h(maybe_adjust_et_rel_sym_addr_to_abs_addr): New function declaration. Reviewed-by: Giuliano Procida Signed-off-by: Matthias Maennich --- src/abg-dwarf-reader.cc | 73 ++++------------------------------------- src/abg-elf-helpers.cc | 40 ++++++++++++++++++++++ src/abg-elf-helpers.h | 3 ++ 3 files changed, 49 insertions(+), 67 deletions(-) diff --git a/src/abg-dwarf-reader.cc b/src/abg-dwarf-reader.cc index dc3332410b1e..c65c01776398 100644 --- a/src/abg-dwarf-reader.cc +++ b/src/abg-dwarf-reader.cc @@ -6425,7 +6425,8 @@ public: { GElf_Addr symbol_value = - maybe_adjust_et_rel_sym_addr_to_abs_addr(sym); + maybe_adjust_et_rel_sym_addr_to_abs_addr(elf_handle(), + sym); addr_elf_symbol_sptr_map_type::const_iterator it = fun_addr_sym_map_->find(symbol_value); @@ -6563,7 +6564,8 @@ public: else { GElf_Addr symbol_value = - maybe_adjust_et_rel_sym_addr_to_abs_addr(sym); + maybe_adjust_et_rel_sym_addr_to_abs_addr(elf_handle(), + sym); addr_elf_symbol_sptr_map_type::const_iterator it = var_addr_sym_map_->find(symbol_value); if (it == var_addr_sym_map_->end()) @@ -7147,8 +7149,8 @@ public: // the symbol value of native_symbol is relative to the // section that symbol is defined in. We need to translate it // into an absolute (okay, binary-relative, rather) address. - GElf_Addr symbol_address = - maybe_adjust_et_rel_sym_addr_to_abs_addr (&native_symbol); + GElf_Addr symbol_address = maybe_adjust_et_rel_sym_addr_to_abs_addr( + elf_handle(), &native_symbol); address_set_sptr set; if (symbol->is_function()) @@ -7523,69 +7525,6 @@ public: return addr; } - /// Translate a section-relative symbol address (i.e, symbol value) - /// into an absolute symbol address by adding the address of the - /// section the symbol belongs to, to the address value. - /// - /// This is useful when looking at symbol values coming from - /// relocatable files (of ET_REL kind). If the binary is not - /// ET_REL, then the function does nothing and returns the input - /// address unchanged. - /// - /// @param addr the symbol address to possibly translate. - /// - /// @param section the section the symbol which value is @p addr - /// belongs to. - /// - /// @return the section-relative address, translated into an - /// absolute address, if @p section is an ET_REL binary. Otherwise, - /// return @p addr, unchanged. - GElf_Addr - maybe_adjust_et_rel_sym_addr_to_abs_addr(GElf_Addr addr, Elf_Scn *section) - { - if (!section) - return addr; - - Elf* elf = elf_handle(); - GElf_Ehdr elf_header; - - if (!gelf_getehdr(elf, &elf_header)) - return addr; - - if (elf_header.e_type != ET_REL) - return addr; - - GElf_Shdr section_header; - if (!gelf_getshdr(section, §ion_header)) - return addr; - - return addr + section_header.sh_addr; - } - - /// Translate a section-relative symbol address (i.e, symbol value) - /// into an absolute symbol address by adding the address of the - /// section the symbol belongs to, to the address value. - /// - /// This is useful when looking at symbol values coming from - /// relocatable files (of ET_REL kind). If the binary is not - /// ET_REL, then the function does nothing and returns the input - /// address unchanged. - /// - /// @param sym the symbol whose address to possibly needs to be - /// translated. - /// - /// @return the section-relative address, translated into an - /// absolute address, if @p sym is from an ET_REL binary. - /// Otherwise, return the address of @p sym, unchanged. - GElf_Addr - maybe_adjust_et_rel_sym_addr_to_abs_addr(GElf_Sym *sym) - { - Elf_Scn *symbol_section = elf_getscn(elf_handle(), sym->st_shndx); - GElf_Addr result = sym->st_value; - result = maybe_adjust_et_rel_sym_addr_to_abs_addr(result, symbol_section); - return result; - } - /// Test if a given address is in a given section. /// /// @param addr the address to consider. diff --git a/src/abg-elf-helpers.cc b/src/abg-elf-helpers.cc index 895feb6f7768..02ecbcf250ed 100644 --- a/src/abg-elf-helpers.cc +++ b/src/abg-elf-helpers.cc @@ -987,5 +987,45 @@ is_dso(Elf* elf_handle) return elf_header.e_type == ET_DYN; } +/// Translate a section-relative symbol address (i.e, symbol value) +/// into an absolute symbol address by adding the address of the +/// section the symbol belongs to, to the address value. +/// +/// This is useful when looking at symbol values coming from +/// relocatable files (of ET_REL kind). If the binary is not +/// ET_REL, then the function does nothing and returns the input +/// address unchanged. +/// +/// @param elf_handle the elf handle for the binary to consider. +/// +/// @param sym the symbol whose address to possibly needs to be +/// translated. +/// +/// @return the section-relative address, translated into an +/// absolute address, if @p sym is from an ET_REL binary. +/// Otherwise, return the address of @p sym, unchanged. +GElf_Addr +maybe_adjust_et_rel_sym_addr_to_abs_addr(Elf* elf_handle, GElf_Sym* sym) +{ + Elf_Scn* symbol_section = elf_getscn(elf_handle, sym->st_shndx); + GElf_Addr addr = sym->st_value; + + if (!symbol_section) + return addr; + + GElf_Ehdr elf_header; + if (!gelf_getehdr(elf_handle, &elf_header)) + return addr; + + if (elf_header.e_type != ET_REL) + return addr; + + GElf_Shdr section_header; + if (!gelf_getshdr(symbol_section, §ion_header)) + return addr; + + return addr + section_header.sh_addr; +} + } // end namespace elf_helpers } // end namespace abigail diff --git a/src/abg-elf-helpers.h b/src/abg-elf-helpers.h index 6eff245a6a30..647c92703dfa 100644 --- a/src/abg-elf-helpers.h +++ b/src/abg-elf-helpers.h @@ -174,6 +174,9 @@ is_executable(Elf* elf_handle); bool is_dso(Elf* elf_handle); +GElf_Addr +maybe_adjust_et_rel_sym_addr_to_abs_addr(Elf* elf_handle, GElf_Sym* sym); + } // end namespace elf_helpers } // end namespace abigail -- 2.26.1.301.g55bc3eb7cb9-goog