From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-oa1-x2a.google.com with SMTP id 586e51a60fabf-14263779059so1527862fac.1 for ; Thu, 17 Nov 2022 06:34:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=F8tnhdADf6/ZF693H4+gkJA0cV4ZNzpejbBGYlDf69M=; b=U+MvivlNjoavKgRDeNPpVecvJsB7cI7reu86ueCWL6XIHMMfC/HcoMHYDXrZZtwpaI mVWbIxerF+ZFAh/SM5CtBjg8rRtG74gTDpczJVUBMVFdNWFbBsKVx6nbvtf2/TX5h7eR 8A2UApZEL3m4pAOIEa7sFalll7GKlGNp7npu4V34eRo0bVS13u1jAQHpQW3crr1afdYD gTEbZHTDMsBhTn4fsgWo21/K1cW8q0tXhTRw98tluS5cHTw5y8lsqwk5UCT4b2alYziT 93XdpmZcNKZKog9yc6HbS5ZLn+0ZFAQd76pF9e7/44CYwBqrabjLwbz3o2DmURIlKOaY 9tgA== 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-Google-Smtp-Source: AA0mqf70ke5ZimG3LQ5KfSChcaRjyBnG3UnS2J4zkRwc6I5N7KftbtKLTwsMaQo2GGOzX+wLDBzO+w== 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) From: Brett Werling 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 Content-Transfer-Encoding: 8bit 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 List-Id: 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"), -- 2.38.1