From patchwork Tue Jun 4 17:02:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Clifton X-Patchwork-Id: 91503 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D389539832D8 for ; Tue, 4 Jun 2024 17:03:06 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id BA12B39832D1 for ; Tue, 4 Jun 2024 17:02:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org BA12B39832D1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org BA12B39832D1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717520557; cv=none; b=Paba6oHLBTxhfD/T4ei75U6ZWbCEWZzXbhCcDCxlebgHJTpchqIKwTMHTYj3wS/OE7k4J3FqgSlB90lWEltYrgPszpYx4gnq3Bd2Te5NIj3onUL3+gx713HD90nTmFg0SDNg//fOf7lZasJGcT5SdC4VDe4PUQCVlmOkWsOx8co= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1717520557; c=relaxed/simple; bh=Yh2EDt+jwd3++58BLfTghoF+llLYdJZQ2FDQqb+Vm1c=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=VvKORc8eI2tjhZC/v+srZy+iu6sD0D3BByMzNzTJgdwaFWcDoYW8fTtYNUBi1saVYwQr9kGqP4uutToIj2P5JjGuNIk1xx273LDPMVqWB2ILxhtHR3LGGr+8Y8zYZ0RqOydExdfU8qBxVPqlsZxEajvS3MgtULXIEsk2gOLbDhQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1717520554; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type; bh=5kWSqOW4NoezXBmSkTn6L8aZs82vBjKyQp2SLNSNasU=; b=Fjn75VMXYPDnc89gSTf+zr0kskoja58mTgWXclNz4uyHBucYcZNuAU74sQ2tUtf9i9w0IR 0mkwxonHh1IJYfOs+EjxlR9iVKNY79XrQnNk0jgPZhiOBPr0pWj6AoroyHDP5vXKTAUtM7 DUmPVe+085x2jxSwT++xZictwD8BXEs= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-517-o7JJ74XLNy2armse47DtRQ-1; Tue, 04 Jun 2024 13:02:32 -0400 X-MC-Unique: o7JJ74XLNy2armse47DtRQ-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 08C6829ABA01; Tue, 4 Jun 2024 17:02:32 +0000 (UTC) Received: from prancer.redhat.com (unknown [10.39.194.235]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D8359492BDB; Tue, 4 Jun 2024 17:02:30 +0000 (UTC) From: Nick Clifton To: binutils@sourceware.org, i@maskray.me Cc: jakub@redhat.com, carlos@redhat.com, fweimer@redhat.com Subject: RFC: PR 30907: BFD linker option to allow read-only data in code segments Date: Tue, 04 Jun 2024 18:02:29 +0100 Message-ID: <87ed9c3al6.fsf@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-9.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces+patchwork=sourceware.org@sourceware.org Hi Guys, Attached is an experimental patch to add new option the bfd linker: -z rodata-in-code. If used the option allows read only data to be placed into a code segment. This is only effective if the -z separate-code option is in effect. When used it has the ability to reduce the number of loadable segments from 4 to 3, which can have a big effect on the overall size of an executable. (See PR 30907 for more discussion on this). Here is an example: $ cat test.c extern int printf (const char *, ...); int i = 42; const int * j = & i; int main (void) { return printf ("hello world %d\n", * j); } $ gcc test.c -Wl,-z,separate-code $ readelf -Wl a.out [...] LOAD 0x000000 0x0000000000400000 0x0000000000400000 0x000510 0x000510 R 0x1000 LOAD 0x001000 0x0000000000401000 0x0000000000401000 0x000155 0x000155 R E 0x1000 LOAD 0x002000 0x0000000000402000 0x0000000000402000 0x0000dc 0x0000dc R 0x1000 LOAD 0x002df8 0x0000000000403df8 0x0000000000403df8 0x000228 0x000230 RW 0x1000 [...] Section to Segment mapping: [...] 02 .interp .note.gnu.property .note.gnu.build-id .note.ABI-tag .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt 03 .init .plt .text .fini 04 .rodata .eh_frame_hdr .eh_frame 05 .init_array .fini_array .dynamic .got .got.plt .data .bss $ gcc test.c -Wl,-z,separate-code -Wl,-z,rodata-in-code $ readelf -Wl a.out [...] LOAD 0x000000 0x0000000000400000 0x0000000000400000 0x0004d8 0x0004d8 R 0x1000 LOAD 0x001000 0x0000000000401000 0x0000000000401000 0x0010dc 0x0010dc R E 0x1000 LOAD 0x002df8 0x0000000000403df8 0x0000000000403df8 0x000228 0x000230 RW 0x1000 [...] Section to Segment mapping: [...] 02 .interp .note.gnu.property .note.gnu.build-id .note.ABI-tag .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .rela.plt 03 .init .plt .text .fini .rodata .eh_frame_hdr .eh_frame 04 .init_array .fini_array .dynamic .got .got.plt .data .bss The patch is incomplete. It does not have any documentation or test cases yet. But I wanted to see if the general idea was going to cause concern for people, and to see if I had missed anything obvious. Thoughts, suggestions ? Cheers Nick diff --git a/bfd/elf.c b/bfd/elf.c index 74236a658fd..1414c973532 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -5443,6 +5443,14 @@ _bfd_elf_map_sections_to_segments (bfd *abfd, } else if (info != NULL && info->separate_code + && info->rodata_in_code + && executable != ((hdr->flags & (SEC_CODE | SEC_READONLY)) != 0)) + { + new_segment = true; + } + else if (info != NULL + && info->separate_code + && ! info->rodata_in_code && executable != ((hdr->flags & SEC_CODE) != 0)) { new_segment = true; diff --git a/include/bfdlink.h b/include/bfdlink.h index eac07d78364..2411b53485f 100644 --- a/include/bfdlink.h +++ b/include/bfdlink.h @@ -420,9 +420,12 @@ struct bfd_link_info relocations. */ unsigned int enable_dt_relr: 1; - /* TRUE if separate code segment should be created. */ + /* TRUE if a separate code segment should be created. */ unsigned int separate_code: 1; + /* TRUE if a read-only data is allowed in the code segment. */ + unsigned int rodata_in_code: 1; + /* Nonzero if .eh_frame_hdr section and PT_GNU_EH_FRAME ELF segment should be created. 1 for DWARF2 tables, 2 for compact tables. */ unsigned int eh_frame_hdr_type: 2; diff --git a/ld/emultempl/elf.em b/ld/emultempl/elf.em index 55a870f7d2d..617581308ad 100644 --- a/ld/emultempl/elf.em +++ b/ld/emultempl/elf.em @@ -852,6 +852,10 @@ fragment <