From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 56A673857816 for ; Fri, 21 Jan 2022 18:26:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 56A673857816 Received: by mail-ot1-x336.google.com with SMTP id x31-20020a056830245f00b00599111c8b20so12813735otr.7 for ; Fri, 21 Jan 2022 10:26:39 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:in-reply-to :content-transfer-encoding; bh=jdPAJpqh+LzLNfkXudbRmr4uf6t4bBkMo2EaRRA8rJQ=; b=zYVjzNohTUTAE5wXHk998J82da5gRZlK9pK4GxuFvqio0bp1VnyPzoqWgnF/8z11X+ jD8vRaY1D7M/VDjny42YFnsP/Fr9pm7WffawH/R4tgefahfc/4dXYoz7EX6+zCAxwPgS 1+QM1DhQvKG4qG9RnQEHa7oqRZxa0/KK+tYb8FMybldRebcpcN8nLAF7gpgjcBHu72d/ YBtjBzVqtARSld+6uQfRhh5mahHY6XnHNqKxI1jy8mkBwRE/g+rUvsbg5+7+JyWQaSRe HVwyHVLLIEMoH1A503UGjpBzMMzlwPxvGkwwG9/euYfQmkZKfDlrbq8h6/OUuWMsvnGO XnyQ== X-Gm-Message-State: AOAM530OWGtsyIpdjUk5MwC7J5IW8a7IxCZWQ/OmOMNEOw04UecYOFX1 GP8ed4r6OShyNXNdERI65jUESHKItn+hXw== X-Google-Smtp-Source: ABdhPJxc6C3HgcZyP1rQXs1nXp6W5Yf0pT1gGxBpU9yXOD7Exs0cVtr9e34WtBNB8BzH/Rwjj8iG4Q== X-Received: by 2002:a9d:4a8:: with SMTP id 37mr3791841otm.83.1642789598707; Fri, 21 Jan 2022 10:26:38 -0800 (PST) Received: from ?IPV6:2804:431:c7cb:27f8:f8b7:bc61:9607:9ecb? ([2804:431:c7cb:27f8:f8b7:bc61:9607:9ecb]) by smtp.gmail.com with ESMTPSA id j17sm1297602otn.9.2022.01.21.10.26.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 21 Jan 2022 10:26:38 -0800 (PST) Message-ID: Date: Fri, 21 Jan 2022 15:26:36 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: [PATCH v8 1/4] elf: Properly align all PT_LOAD segments [BZ #28676] Content-Language: en-US To: "H.J. Lu" , libc-alpha@sourceware.org Cc: Florian Weimer References: <20220118215731.1320930-1-hjl.tools@gmail.com> <20220118215731.1320930-2-hjl.tools@gmail.com> From: Adhemerval Zanella In-Reply-To: <20220118215731.1320930-2-hjl.tools@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, 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: Fri, 21 Jan 2022 18:26:40 -0000 On 18/01/2022 18:57, 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 LGTM, thanks. Reviewed-by: Adhemerval Zanella > --- > 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 12744caf91..5b0ff41ee1 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 (powerof2 (ph->p_align) && 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 > @@ -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