From patchwork Tue Nov 15 14:57:17 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Brett Werling X-Patchwork-Id: 20422 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2774067wru; Tue, 15 Nov 2022 06:57:55 -0800 (PST) X-Google-Smtp-Source: AA0mqf5wBoerpOMQQPj6XSXIoOPzaoSW7JuQRiMwRNVhOFahhOKA3bAfzLe813x+GrHRgxFpnquT X-Received: by 2002:a17:906:339b:b0:78d:3862:4488 with SMTP id v27-20020a170906339b00b0078d38624488mr13539417eja.683.1668524275658; Tue, 15 Nov 2022 06:57:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668524275; cv=none; d=google.com; s=arc-20160816; b=mZ8wgGkjbMr2sUnrqcGv3pJzKyyuubE61f3JaHT2EFrj/sCAUXEC4FLVcxaheujLVm H7gh5ydad4JFtKQ/PebPe5vUWHHE7bPhmvFWZ6FWe/r4DdUrDknUJe6FDc4Fj+t4thMt unnikVHr6VmV7Mnjub6S5UECZI5RCOTCfgTVFoWi1hlu0aEYDtkT+EZ53A73nf0rjzIj YLxhG3P/NFQ0YVN7Nm4DzoKT6r1IIgvbcGBu151pDV1SrNOlBGBdIQ/svHQTKRM5v/Wk 48Z0B6g0DJk+0X/raJTQwO0XD/WUnZ6To5NP/rwuSBZ2UPwiYYsTNZPPl89ib0IBo0PC EDAQ== 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=2IVA30MD7/wTydTEEEiVqUSrPfQat5Mi5qtZY8yHKlA=; b=aA/R25p1cr+xV1hF0SudFgnZbooTWwHATK7Tyy8pyBllWugDM7PyA1plZ2BMgClJnX Jx+xAiZPqVfyy2h4FYwfgqUNqpZ7wtSxMzuZ99qDplf0Fja1rWgXcBieHUdK0PXMSjh6 oA/ZZKn691Y+x3cSt8GECVVwsQZsTzOrm1FzAR2pq328jMXejBOZWm1ohOLiFdz0iA1i eKWX9ojzwu5yJZDMWroknjYzEXEP8FgyhTu/P4ZoFMr8lYmpaE2Rn9s5WFGdDFJ+iiBW ziRIiBDMZsHpPswAhx827P2EUsQg48C4a64TNruwHPHy7FJvO/grhPMiWdxQRjdFh7Vb psvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=UScf++mc; 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 gb18-20020a170907961200b0079194d8b1f6si14319248ejc.892.2022.11.15.06.57.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 06:57:55 -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=UScf++mc; 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 6CCB93896C29 for ; Tue, 15 Nov 2022 14:57:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6CCB93896C29 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1668524274; bh=2IVA30MD7/wTydTEEEiVqUSrPfQat5Mi5qtZY8yHKlA=; 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=UScf++mcJ2Dtlhx6sv1IgVogF+c3ZzpG6ASdEzI0ugc71uevPR1RamhRz81b4G6T3 /P1sHaXHdYkS9FaZuF4EXDptG8jAaJE6jcHvH/iviED4rOK/KY8uKS7/tjPorjYYLr 2sl+5eclR6oz39bL1e1OHZ2kG7efWC14oHEDEWQA= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-ot1-x336.google.com (mail-ot1-x336.google.com [IPv6:2607:f8b0:4864:20::336]) by sourceware.org (Postfix) with ESMTPS id B6E343896C18 for ; Tue, 15 Nov 2022 14:57:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B6E343896C18 Received: by mail-ot1-x336.google.com with SMTP id l42-20020a9d1b2d000000b0066c6366fbc3so8647763otl.3 for ; Tue, 15 Nov 2022 06:57:45 -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=2IVA30MD7/wTydTEEEiVqUSrPfQat5Mi5qtZY8yHKlA=; b=OAVuarmQYI87cLglTe+FOqqsIPvOWJMAsNCUmRnKAQM4AEguSpuCj5X84hwoJZpWDq I0q5NeYlMh8x82I1yASpFjl5Dld9osaX/VTyJGUura1VFaXDb6iOZUR6NlJiCIcGJSSv gvrm2fc+jfgEH/i/nTxcQfMNkyAQIc1qW47kALxg/zH0IZQLjzgIPLhl6Lc1uy671zuj tRqEjSuVBRG/FSkOUfJiLmELAD4HyVT8T6bkilBklQMjPExQshprWVOuXyixaLzBcWMk kznjgRBYoJ/B8NA2EFmYW8q/87J/0hzqBq9mPbg8p9JbqgnUctFM+ATwE5p+28qI/wj0 hCmQ== X-Gm-Message-State: ANoB5pnaOMOeCFGRrBQXp6gs3zYfksZayUbhjKToPJiDRcJXVOrabp/z AAZF1QkwJsL/6jyVCVuJE+EqfqJElwNjBg== X-Received: by 2002:a9d:865:0:b0:66c:8ba4:f618 with SMTP id 92-20020a9d0865000000b0066c8ba4f618mr9186324oty.330.1668524264777; Tue, 15 Nov 2022 06:57:44 -0800 (PST) Received: from ola-9d10b5e-vm.ad.garmin.com ([204.77.163.55]) by smtp.gmail.com with ESMTPSA id h3-20020a9d7983000000b0063b24357269sm5417560otm.13.2022.11.15.06.57.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 06:57:44 -0800 (PST) To: binutils@sourceware.org Cc: Brett Werling Subject: [PATCH] readelf: use fseeko64 or fseeko if possible Date: Tue, 15 Nov 2022 08:57:17 -0600 Message-Id: <20221115145717.64948-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.8 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?1749574510819731012?= 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. In all cases, the offset argument given is cast accoridngly. 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 | 47 ++++++++++++++++++++++++++++------------------ 3 files changed, 36 insertions(+), 19 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 c8323539a21..3e5e4f29ff2 100644 --- a/binutils/readelf.c +++ b/binutils/readelf.c @@ -177,6 +177,17 @@ #define offsetof(TYPE, MEMBER) ((size_t) &(((TYPE *) 0)->MEMBER)) #endif +#if defined (HAVE_FSEEKO64) +#define FSEEK_FUNC fseeko64 +#define FSEEK_OFF_TYPE off64_t +#elif defined (HAVE_FSEEKO) +#define FSEEK_FUNC fseeko +#define FSEEK_OFF_TYPE off_t +#else +#define FSEEK_FUNC fseek +#define FSEEK_OFF_TYPE long +#endif + typedef struct elf_section_list { Elf_Internal_Shdr * hdr; @@ -479,7 +490,7 @@ get_data (void *var, return NULL; } - if (fseek (filedata->handle, filedata->archive_file_offset + offset, + if (FSEEK_FUNC (filedata->handle, (FSEEK_OFF_TYPE)filedata->archive_file_offset + offset, SEEK_SET)) { if (reason) @@ -6262,8 +6273,8 @@ 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, - filedata->archive_file_offset + (long) segment->p_offset, + || FSEEK_FUNC (filedata->handle, + (FSEEK_OFF_TYPE)filedata->archive_file_offset + (long) segment->p_offset, SEEK_SET)) error (_("Unable to find program interpreter name\n")); else @@ -11035,8 +11046,8 @@ get_num_dynamic_syms (Filedata * filedata) && filedata->file_header.e_ident[EI_CLASS] == ELFCLASS64) hash_ent_size = 8; - if (fseek (filedata->handle, - (filedata->archive_file_offset + if (FSEEK_FUNC (filedata->handle, + ((FSEEK_OFF_TYPE)filedata->archive_file_offset + offset_from_vma (filedata, filedata->dynamic_info[DT_HASH], sizeof nb + sizeof nc)), SEEK_SET)) @@ -11088,8 +11099,8 @@ get_num_dynamic_syms (Filedata * filedata) uint64_t buckets_vma; unsigned long hn; - if (fseek (filedata->handle, - (filedata->archive_file_offset + if (FSEEK_FUNC (filedata->handle, + ((FSEEK_OFF_TYPE)filedata->archive_file_offset + offset_from_vma (filedata, filedata->dynamic_info_DT_GNU_HASH, sizeof nb)), @@ -11114,8 +11125,8 @@ get_num_dynamic_syms (Filedata * filedata) else buckets_vma += bitmaskwords * 8; - if (fseek (filedata->handle, - (filedata->archive_file_offset + if (FSEEK_FUNC (filedata->handle, + ((FSEEK_OFF_TYPE)filedata->archive_file_offset + offset_from_vma (filedata, buckets_vma, 4)), SEEK_SET)) { @@ -11144,8 +11155,8 @@ get_num_dynamic_syms (Filedata * filedata) maxchain -= filedata->gnusymidx; - if (fseek (filedata->handle, - (filedata->archive_file_offset + if (FSEEK_FUNC (filedata->handle, + ((FSEEK_OFF_TYPE)filedata->archive_file_offset + offset_from_vma (filedata, buckets_vma + 4 * (filedata->ngnubuckets + maxchain), @@ -11171,8 +11182,8 @@ get_num_dynamic_syms (Filedata * filedata) } while ((byte_get (nb, 4) & 1) == 0); - if (fseek (filedata->handle, - (filedata->archive_file_offset + if (FSEEK_FUNC (filedata->handle, + ((FSEEK_OFF_TYPE)filedata->archive_file_offset + offset_from_vma (filedata, (buckets_vma + 4 * filedata->ngnubuckets), 4)), @@ -11190,8 +11201,8 @@ get_num_dynamic_syms (Filedata * filedata) if (filedata->dynamic_info_DT_MIPS_XHASH) { - if (fseek (filedata->handle, - (filedata->archive_file_offset + if (FSEEK_FUNC (filedata->handle, + ((FSEEK_OFF_TYPE)filedata->archive_file_offset + offset_from_vma (filedata, (buckets_vma + 4 * (filedata->ngnubuckets + maxchain)), 4)), @@ -22546,7 +22557,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, (FSEEK_OFF_TYPE)current_pos, SEEK_SET) != 0) { error (_("%s: failed to seek back to start of object files " "in the archive\n"), @@ -22573,7 +22584,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, (FSEEK_OFF_TYPE)arch.next_arhdr_offset, SEEK_SET) != 0) { error (_("%s: failed to seek to next archive header\n"), arch.file_name); @@ -22683,7 +22694,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, (FSEEK_OFF_TYPE)filedata->archive_file_offset, SEEK_SET) != 0) { error (_("%s: failed to seek to archive member.\n"),