From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-f50.google.com (mail-pj1-f50.google.com [209.85.216.50]) by sourceware.org (Postfix) with ESMTPS id 4E8103858C3A for ; Tue, 11 Jan 2022 05:26:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4E8103858C3A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=maskray.me Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pj1-f50.google.com with SMTP id pj2so15359710pjb.2 for ; Mon, 10 Jan 2022 21:26:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=tMeUxxNfE4VyxErk24s78HhDF+S07s21Ju6lEwHkHjU=; b=k1ReqZefyODnjfTZxvmZ/rTCX/e665j35/LC+4WpT9Eu2dgivSUB1HGsEcCXOxLdY9 GDtbNqIgNEl1wHNqTgfphpDyLfyARcgdGkDl0oN5DZq5L/X5EED1SRqBlb2WYZ9LuYnE p6pRsMcuV9g1Vtxv4/2ieq/4xSvvof1OPotS+bYhI54GGBWj6oOdI8vrfaB3dutXiDht JEl8KM1DWBU/c7B1hwAQxN+5lW6Tixst7n0ld1V696eebhPaLL7nNAA4vwX5zFSrigsj KXnVumq8wl97zh5RsthrN3NuinBEiG5VKwv8mVbRcsHMwVO7BV3tAsB4xaUb1C+GjiuT DwNw== X-Gm-Message-State: AOAM532QLbEJ+m0GGjWSabP/lowLO4pb1Ct6k/Z0iSK0CMWOQ4MpQRy8 X3oB3f9EYYllAOXYMg0pwxRtwpcB10c= X-Google-Smtp-Source: ABdhPJwRvFuJlb8Mt07NZHPyg4tPisNx+FTaRNp0rZzeEiR+H7PXBBJua9dnZj7y579XQ1ifgyM/ZA== X-Received: by 2002:a05:6a00:2ac:b0:4bd:e223:3d1 with SMTP id q12-20020a056a0002ac00b004bde22303d1mr2915599pfs.86.1641878771339; Mon, 10 Jan 2022 21:26:11 -0800 (PST) Received: from localhost ([2601:647:6300:b760:dd42:ded5:6189:5c3b]) by smtp.gmail.com with ESMTPSA id h21sm6131523pfo.38.2022.01.10.21.26.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Jan 2022 21:26:11 -0800 (PST) Date: Mon, 10 Jan 2022 21:26:09 -0800 From: Fangrui Song To: "H.J. Lu" Cc: binutils@sourceware.org Subject: Re: [PATCH] elf: Remove the 1-page gap before the RELRO segment Message-ID: <20220111052609.neg3brvkhyqzy4vd@gmail.com> References: <20220111021241.1937265-1-hjl.tools@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20220111021241.1937265-1-hjl.tools@gmail.com> X-Spam-Status: No, score=-1.6 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_INFOUSMEBIZ, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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-List-Received-Date: Tue, 11 Jan 2022 05:26:14 -0000 On 2022-01-10, H.J. Lu via Binutils wrote: >The existing RELRO scheme may leave a 1-page gap before the RELRO segment >and align the end of the RELRO segment to the page size: > > [18] .eh_frame PROGBITS 408fa0 008fa0 005e80 00 A 0 0 8 > [19] .init_array INIT_ARRAY 410de0 00fde0 000008 08 WA 0 0 8 > [20] .fini_array FINI_ARRAY 410de8 00fde8 000008 08 WA 0 0 8 > [21] .dynamic DYNAMIC 410df0 00fdf0 000200 10 WA 7 0 8 > [22] .got PROGBITS 410ff0 00fff0 000010 08 WA 0 0 8 > [23] .got.plt PROGBITS 411000 010000 000048 08 WA 0 0 8 > >Instead, we can remove the 1-page gap if the maximum page size >= the >maximum section alignment: > > [18] .eh_frame PROGBITS 408fa0 008fa0 005e80 00 A 0 0 8 > [19] .init_array INIT_ARRAY 40fde0 00fde0 000008 08 WA 0 0 8 > [20] .fini_array FINI_ARRAY 40fde8 00fde8 000008 08 WA 0 0 8 > [21] .dynamic DYNAMIC 40fdf0 00fdf0 000200 10 WA 7 0 8 > [22] .got PROGBITS 40fff0 00fff0 000010 08 WA 0 0 8 > [23] .got.plt PROGBITS 410000 010000 000048 08 WA 0 0 8 > >Because the end of the RELRO segment is always aligned to the page size >and may not be moved, the RELRO segment size may be increased: > > [ 3] .dynstr STRTAB 000148 000148 000001 00 A 0 0 1 > [ 4] .eh_frame PROGBITS 000150 000150 000000 00 A 0 0 8 > [ 5] .init_array INIT_ARRAY 200150 000150 000010 08 WA 0 0 1 > [ 6] .fini_array FINI_ARRAY 200160 000160 000010 08 WA 0 0 1 > [ 7] .jcr PROGBITS 200170 000170 000008 00 WA 0 0 1 > [ 8] .data.rel.ro PROGBITS 200180 000180 000020 00 WA 0 0 16 > [ 9] .dynamic DYNAMIC 2001a0 0001a0 0001c0 10 WA 3 0 8 > [10] .got PROGBITS 200360 000360 0002a8 00 WA 0 0 8 > [11] .bss NOBITS 201000 000608 000840 00 WA 0 0 1 > >vs the old section layout: > > [ 3] .dynstr STRTAB 000148 000148 000001 00 A 0 0 1 > [ 4] .eh_frame PROGBITS 000150 000150 000000 00 A 0 0 8 > [ 5] .init_array INIT_ARRAY 200b48 000b48 000010 08 WA 0 0 1 > [ 6] .fini_array FINI_ARRAY 200b58 000b58 000010 08 WA 0 0 1 > [ 7] .jcr PROGBITS 200b68 000b68 000008 00 WA 0 0 1 > [ 8] .data.rel.ro PROGBITS 200b70 000b70 000020 00 WA 0 0 16 > [ 9] .dynamic DYNAMIC 200b90 000b90 0001c0 10 WA 3 0 8 > [10] .got PROGBITS 200d50 000d50 0002a8 00 WA 0 0 8 > [11] .bss NOBITS 201000 000ff8 000840 00 WA 0 0 1 > >But there is no 1-page gap. > [...} If you want to avoid a max-page-size alignment (at the end of PT_GNU_RELRO), you may adopt ld.lld's design I implemented: LOAD 0x000000 0x0000000000200000 0x0000000000200000 0xcaac4c 0xcaac4c R 0x1000 LOAD 0xcaac50 0x0000000000eabc50 0x0000000000eabc50 0x208a0c0 0x208a0c0 R E 0x1000 LOAD 0x2d34d10 0x0000000002f36d10 0x0000000002f36d10 0x1777e8 0x1777e8 RW 0x1000 match LOAD 0x2eac500 0x00000000030af500 0x00000000030af500 0x008038 0x064664 RW 0x1000 TLS 0x2d34d10 0x0000000002f35d10 0x0000000002f35d10 0x000000 0x000018 R 0x8 DYNAMIC 0x2ea5570 0x00000000030a7570 0x00000000030a7570 0x000240 0x000240 RW 0x8 GNU_RELRO 0x2d34d10 0x0000000002f36d10 0x0000000002f36d10 0x1777e8 0x1782f0 R 0x1 match GNU_EH_FRAME 0x89ce34 0x0000000000a9ce34 0x0000000000a9ce34 0x08586c 0x08586c R 0x4 GNU_STACK 0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW 0 NOTE 0x0002fc 0x00000000002002fc 0x00000000002002fc 0x000020 0x000020 R 0x4 The idea is to have 2 RW PT_LOAD with one exactly matching PT_GNU_RELRO.