From patchwork Thu Nov 17 14:34:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brett Werling X-Patchwork-Id: 21682 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp432958wrr; Thu, 17 Nov 2022 06:34:51 -0800 (PST) X-Google-Smtp-Source: AA0mqf7UKbk1hd9rsNnCrSC7aAz03V+9SQFgBSE5v6TU7lyryoIn4vGm0iSAzHM7/02HozgQ4QYj X-Received: by 2002:a50:fb8e:0:b0:459:9e3:c0b0 with SMTP id e14-20020a50fb8e000000b0045909e3c0b0mr2319559edq.279.1668695691325; Thu, 17 Nov 2022 06:34:51 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668695691; cv=none; d=google.com; s=arc-20160816; b=AIsgfV5cS5fRSAON666KJIewW3Y1jInqzrijVHAc0eGXG53k3zIoJkh5SJgG0LuFba sffqq4+n4RyJ7sOQmAkq/Gxv2JkPF4aQcSW0/FKEhctBVn5MvEhTwhvS75ivN4gOKgh6 l9wUgf4rkgngDI8CHPBE2tu2W1gtBjAFI72ap1etjrINJ4WrfgvWx0vgDq0TvjEKFgsi NosYOvP1iRGBsDnF83ifnzL7UaZUO/T+ox1aNsPlLrtVq7d2uvuXYmq0H5cRYzZcmx8E cZz0f8p3UbKE7LY1fCP6jt+f7L0ZcVNSj7HviC/kTVAbr9z6qFjjnxPwIzxufg72b2nX PBeQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=F8tnhdADf6/ZF693H4+gkJA0cV4ZNzpejbBGYlDf69M=; b=ATq3syJZtUWuxRli04bOGJbU8EcNIv8rJ0WolOpW+40IvCU5STNJ7xnifxXljeJnZl 1KD5yDB/fnTZ+EqXzFeorNIHDBH0M98xOTfDphpw2+SUAzlRAQ2ZVT1eNBFtgh2QDG82 vnQOArzbcEOYwy7PNtsQI72QLUK6iRQ8RpRsGVkgndrNwNIvOEa96Kne1PC5kqWQjz1+ kXNjwy9vjesCiOPVR/BHLMbTm9tqkkQw7lKtnnVqLhed2xjCroMAw2ePT2itMjMtB+s+ FE9P4kisjTGilJ/axRFX1Z8gE6KOn75R6oNINC1r7AXtud94XCi8h8I7EfJywRxkgxoG LQww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=wSdMppo0; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id e15-20020a17090658cf00b007801a579899si690519ejs.448.2022.11.17.06.34.51 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 06:34:51 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=wSdMppo0; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6A74D3AA900E for ; Thu, 17 Nov 2022 14:34:49 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6A74D3AA900E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1668695689; bh=F8tnhdADf6/ZF693H4+gkJA0cV4ZNzpejbBGYlDf69M=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=wSdMppo0k+O1By3ywLdHlIVxY7FGs0S4zJgmCKrl+eS0sWn4fJtfTFH7E3vpmUdqF h+J+cJirhYv3tifY5qp+vanyCJcZY0c3GlXVhAk59Pui4CThI1xliftthBc8pkTiN/ kCDIfoOmzmQeeu0l9zeHe1SVla9DrfO8Uy+sASJc= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-oa1-x2a.google.com (mail-oa1-x2a.google.com [IPv6:2001:4860:4864:20::2a]) by sourceware.org (Postfix) with ESMTPS id 269573AA88B2 for ; Thu, 17 Nov 2022 14:34:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 269573AA88B2 Received: by mail-oa1-x2a.google.com with SMTP id 586e51a60fabf-14263779059so1527862fac.1 for ; Thu, 17 Nov 2022 06:34:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=F8tnhdADf6/ZF693H4+gkJA0cV4ZNzpejbBGYlDf69M=; b=arjtgZ637iahWuJVw7UHUWjnvfQEDtodzvXy2m4EadclvhfYKf7ln4AI+RK9iLPj/O 2+ozCaWlYSAbZu//h03XySaMpz21cxpuEPF0IDRlDxYEnPTKDcIMQCGKEu8wMYicwm9E pJWnO4OmaMzkV5ZlIoa1Dj5BSf9ziYBIPy5f7wivyWlFwvlfYh9eEyHBA+op4rqtT2UB OU0GA7HQWKXtIo9ZT0V+j7lDU0h0FR8LIbS04dCRBfnoLhlbDi1Xz/oIVp5iXXlZ+Ggp TcdNWdxaBfs21JE9chOoAx3kVeUpv++SphMKvV4bOm8TfaovJ6ypuECQjOdZBFNdJCwn Oijw== X-Gm-Message-State: ANoB5plyX1ZvNfGJSHq671QQioyjPZrUqZll/KGltPVz3UJAZ0vt82xx 6fdNfknMI5kMNp2J+NApXwSwntQ8zge2ZA== X-Received: by 2002:a05:6871:4504:b0:13c:87dc:d9e7 with SMTP id nj4-20020a056871450400b0013c87dcd9e7mr4301152oab.149.1668695679194; Thu, 17 Nov 2022 06:34:39 -0800 (PST) Received: from ola-9d10b5e-vm.ad.garmin.com ([204.77.163.55]) by smtp.gmail.com with ESMTPSA id b19-20020a4ac293000000b0049f0671a23asm377810ooq.9.2022.11.17.06.34.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Nov 2022 06:34:38 -0800 (PST) To: binutils@sourceware.org Cc: Brett Werling Subject: [PATCH] readelf: use fseeko64 or fseeko if possible Date: Thu, 17 Nov 2022 08:34:19 -0600 Message-Id: <20221117143419.19571-1-bwerl.dev@gmail.com> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221114150348.112815-1-bwerl.dev@gmail.com> References: <20221114150348.112815-1-bwerl.dev@gmail.com> MIME-Version: 1.0 X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Brett Werling via Binutils From: Brett Werling Reply-To: Brett Werling Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749574510819731012?= X-GMAIL-MSGID: =?utf-8?q?1749754253453415575?= Changes readelf to make use first of fseeko64 and then fseeko, depending on which of those is available. If neither is available, reverts to the previous behavior of using fseek. This is necessary when building readelf for LLP64 systems, where a long will only be 32 bits wide. If the elf file in question is >= 2 GiB, that is greater than the max long value and therefore fseek will fail indicating that the offset is negative. On such systems, making use of fseeko64 or fseeko will result in the ability so seek past the 2 GiB max long boundary. --- binutils/config.in | 6 ++++++ binutils/configure | 2 +- binutils/readelf.c | 30 +++++++++++++++++++----------- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/binutils/config.in b/binutils/config.in index 4d22a80971b..91fe00af777 100644 --- a/binutils/config.in +++ b/binutils/config.in @@ -67,6 +67,12 @@ /* Define to 1 if you have the header file. */ #undef HAVE_FCNTL_H +/* Define to 1 if you have the `fseeko' function. */ +#undef HAVE_FSEEKO + +/* Define to 1 if you have the `fseeko64' function. */ +#undef HAVE_FSEEKO64 + /* Define to 1 if you have the `getc_unlocked' function. */ #undef HAVE_GETC_UNLOCKED diff --git a/binutils/configure b/binutils/configure index 6176d699e57..46519a31701 100755 --- a/binutils/configure +++ b/binutils/configure @@ -13155,7 +13155,7 @@ $as_echo "#define HAVE_MMAP 1" >>confdefs.h fi rm -f conftest.mmap conftest.txt -for ac_func in getc_unlocked mkdtemp mkstemp utimensat utimes +for ac_func in getc_unlocked mkdtemp mkstemp utimensat utimes fseeko fseeko64 do : as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh` ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var" diff --git a/binutils/readelf.c b/binutils/readelf.c index 6b2cbbcbb1b..044022ec334 100644 --- a/binutils/readelf.c +++ b/binutils/readelf.c @@ -178,6 +178,14 @@ #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER)) #endif +#if defined (HAVE_FSEEKO64) +#define FSEEK_FUNC fseeko64 +#elif defined (HAVE_FSEEKO) +#define FSEEK_FUNC fseeko +#else +#define FSEEK_FUNC fseek +#endif + typedef struct elf_section_list { Elf_Internal_Shdr * hdr; @@ -482,7 +490,7 @@ get_data (void *var, return NULL; } - if (fseek (filedata->handle, filedata->archive_file_offset + offset, + if (FSEEK_FUNC (filedata->handle, filedata->archive_file_offset + offset, SEEK_SET)) { if (reason) @@ -6283,7 +6291,7 @@ the .dynamic section is not the same as the dynamic segment\n")); if (segment->p_offset >= filedata->file_size || segment->p_filesz > filedata->file_size - segment->p_offset || segment->p_filesz - 1 >= (size_t) -2 - || fseek (filedata->handle, + || FSEEK_FUNC (filedata->handle, filedata->archive_file_offset + (long) segment->p_offset, SEEK_SET)) error (_("Unable to find program interpreter name\n")); @@ -11056,7 +11064,7 @@ get_num_dynamic_syms (Filedata * filedata) && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64) hash_ent_size = 8; - if (fseek (filedata->handle, + if (FSEEK_FUNC (filedata->handle, (filedata->archive_file_offset + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH], sizeof nb + sizeof nc)), @@ -11109,7 +11117,7 @@ get_num_dynamic_syms (Filedata * filedata) uint64_t buckets_vma; unsigned long hn; - if (fseek (filedata->handle, + if (FSEEK_FUNC (filedata->handle, (filedata->archive_file_offset + offset_from_vma (filedata, filedata->dynamic_info_DT_GNU_HASH, @@ -11135,7 +11143,7 @@ get_num_dynamic_syms (Filedata * filedata) else buckets_vma += bitmaskwords * 8; - if (fseek (filedata->handle, + if (FSEEK_FUNC (filedata->handle, (filedata->archive_file_offset + offset_from_vma (filedata, buckets_vma, 4)), SEEK_SET)) @@ -11165,7 +11173,7 @@ get_num_dynamic_syms (Filedata * filedata) maxchain -= filedata->gnusymidx; - if (fseek (filedata->handle, + if (FSEEK_FUNC (filedata->handle, (filedata->archive_file_offset + offset_from_vma (filedata, buckets_vma + 4 * (filedata->ngnubuckets @@ -11192,7 +11200,7 @@ get_num_dynamic_syms (Filedata * filedata) } while ((byte_get (nb, 4) & 1) == 0); - if (fseek (filedata->handle, + if (FSEEK_FUNC (filedata->handle, (filedata->archive_file_offset + offset_from_vma (filedata, (buckets_vma + 4 * filedata->ngnubuckets), @@ -11211,7 +11219,7 @@ get_num_dynamic_syms (Filedata * filedata) if (filedata->dynamic_info_DT_MIPS_XHASH) { - if (fseek (filedata->handle, + if (FSEEK_FUNC (filedata->handle, (filedata->archive_file_offset + offset_from_vma (filedata, (buckets_vma + 4 * (filedata->ngnubuckets @@ -22610,7 +22618,7 @@ process_archive (Filedata * filedata, bool is_thin_archive) ret = false; } - if (fseek (filedata->handle, current_pos, SEEK_SET) != 0) + if (FSEEK_FUNC (filedata->handle, current_pos, SEEK_SET) != 0) { error (_("%s: failed to seek back to start of object files " "in the archive\n"), @@ -22637,7 +22645,7 @@ process_archive (Filedata * filedata, bool is_thin_archive) char * qualified_name; /* Read the next archive header. */ - if (fseek (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0) + if (FSEEK_FUNC (filedata->handle, arch.next_arhdr_offset, SEEK_SET) != 0) { error (_("%s: failed to seek to next archive header\n"), arch.file_name); @@ -22747,7 +22755,7 @@ process_archive (Filedata * filedata, bool is_thin_archive) /* The nested archive file will have been opened and setup by get_archive_member_name. */ - if (fseek (nested_arch.file, filedata->archive_file_offset, + if (FSEEK_FUNC (nested_arch.file, filedata->archive_file_offset, SEEK_SET) != 0) { error (_("%s: failed to seek to archive member.\n"),