From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by sourceware.org (Postfix) with ESMTPS id 1E08E3857C4C for ; Tue, 18 Jan 2022 21:46:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1E08E3857C4C Received: by mail-pj1-x102e.google.com with SMTP id d15-20020a17090a110f00b001b4e7d27474so605358pja.2 for ; Tue, 18 Jan 2022 13:46:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=tYaDeT7l1pKvybrknoCDWd8PRCTI7SY6kp9YQCZQ2ag=; b=M6NXzHqgVK5NKsRf1EVCQqzfkXE+rQHvjwFLNAwe3cLaGXbh65R40y8pMg40PHaa3H a/LqqT8I6p50hCxKmGNmv8xBxQBSE2wjko6lzFNeWW8pNEmZDZt8wWr/TFz2BGHd2/Uf ZDA0Aby2enJN2dkmXgeMjfYoK3I5PXmehmXLdlSGpjjKE0/60lZdHovFWrykq84oBphG HtdjulKJQQC09XbqGOhMARMJJGwJXzfA10O1PLPl8DKU9IoIJKbsszFR0X0l6ZZovpeM A4wLTKJZbqxiwylS7bqot/QNgJ6NmX/r9spdT8ZGy10+8VpZbwOcmTPgseba00gDIkrg JtOw== X-Gm-Message-State: AOAM533GqheiUWn5q5uRUwckTpPgR/kVjgjsspMEbylDA8VGzT1sCAZN rSIb9j6SWPeDIpT5jvrBdjiYoMbe3b1cRNKDwpo= X-Google-Smtp-Source: ABdhPJyqz3BV9zDimGPGf/MM7SXbqguuJWRErzJULVURRhbkhjVn/3YXopBsJp7/WdbJ4hWKvPSM6uqbdB0jKDCrnsM= X-Received: by 2002:a17:902:e544:b0:14a:555c:add3 with SMTP id n4-20020a170902e54400b0014a555cadd3mr28803449plf.102.1642542405152; Tue, 18 Jan 2022 13:46:45 -0800 (PST) MIME-Version: 1.0 References: <20220103230433.1907240-1-hjl.tools@gmail.com> <20220103230433.1907240-2-hjl.tools@gmail.com> <0a5eb87b-9a3c-6b3b-a82c-58e872e7e42f@linaro.org> In-Reply-To: <0a5eb87b-9a3c-6b3b-a82c-58e872e7e42f@linaro.org> From: "H.J. Lu" Date: Tue, 18 Jan 2022 13:46:09 -0800 Message-ID: Subject: Re: [PATCH v7 1/4] elf: Properly align all PT_LOAD segments [BZ #28676] To: Adhemerval Zanella Cc: GNU C Library , Florian Weimer Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-3028.0 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.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 18 Jan 2022 21:46:47 -0000 On Tue, Jan 18, 2022 at 9:49 AM Adhemerval Zanella wrote: > > > > On 03/01/2022 20:04, H.J. Lu wrote: > > Linker may set p_align of a PT_LOAD segment larger than p_align of the > > first PT_LOAD segment to satisfy a section alignment: > > > > Elf file type is DYN (Shared object file) > > Entry point 0x0 > > There are 10 program headers, starting at offset 64 > > > > Program Headers: > > Type Offset VirtAddr PhysAddr > > FileSiz MemSiz Flags Align > > LOAD 0x0000000000000000 0x0000000000000000 0x0000000000000000 > > 0x0000000000000834 0x0000000000000834 R E 0x1000 > > LOAD 0x0000000000000e00 0x0000000000001e00 0x0000000000001e00 > > 0x0000000000000230 0x0000000000000230 RW 0x1000 > > LOAD 0x0000000000400000 0x0000000000400000 0x0000000000400000 > > 0x0000000000000004 0x0000000000000008 RW 0x400000 > > ... > > > > Section to Segment mapping: > > Segment Sections... > > 00 .note.gnu.property .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt .init .plt .plt.got .text .fini .rodata .eh_frame_hdr .eh_frame > > 01 .init_array .fini_array .data.rel.ro .dynamic .got .got.plt > > 02 .data .bss > > > > We should align the first PT_LOAD segment to the maximum p_align of all > > PT_LOAD segments, similar to the kernel commit: > > > > commit ce81bb256a224259ab686742a6284930cbe4f1fa > > Author: Chris Kennelly > > Date: Thu Oct 15 20:12:32 2020 -0700 > > > > fs/binfmt_elf: use PT_LOAD p_align values for suitable start address > > --- > > elf/dl-load.c | 9 ++++++++- > > 1 file changed, 8 insertions(+), 1 deletion(-) > > > > diff --git a/elf/dl-load.c b/elf/dl-load.c > > index ddc4295ef5..109bed3fb5 100644 > > --- a/elf/dl-load.c > > +++ b/elf/dl-load.c > > @@ -1101,6 +1101,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, > > size_t nloadcmds = 0; > > bool has_holes = false; > > bool empty_dynamic = false; > > + ElfW(Addr) p_align_max = 0; > > > > /* The struct is initialized to zero so this is not necessary: > > l->l_ld = 0; > > @@ -1146,7 +1147,9 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, > > c->mapend = ALIGN_UP (ph->p_vaddr + ph->p_filesz, GLRO(dl_pagesize)); > > c->dataend = ph->p_vaddr + ph->p_filesz; > > c->allocend = ph->p_vaddr + ph->p_memsz; > > - c->mapalign = ph->p_align; > > + /* Remember the maximum p_align. */ > > + if (ph->p_align > p_align_max) > > + p_align_max = ph->p_align; > > c->mapoff = ALIGN_DOWN (ph->p_offset, GLRO(dl_pagesize)); > > > > /* Determine whether there is a gap between the last segment > > Kernel also skips non-power of two alignment as invalid, should we do the same > to consider the max alignment? Fixed. > > @@ -1221,6 +1224,10 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, > > goto lose; > > } > > > > + /* Align all PT_LOAD segments to the maximum p_align. */ > > + for (size_t i = 0; i < nloadcmds; i++) > > + loadcmds[i].mapalign = p_align_max; > > + > > /* dlopen of an executable is not valid because it is not possible > > to perform proper relocations, handle static TLS, or run the > > ELF constructors. For PIE, the check needs the dynamic Thanks. -- H.J.