From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by sourceware.org (Postfix) with ESMTPS id 3E8043857C62 for ; Sun, 20 Jun 2021 22:50:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3E8043857C62 Received: by mail-pg1-x533.google.com with SMTP id t9so12604004pgn.4 for ; Sun, 20 Jun 2021 15:50:32 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=wIfgEzm9Deqto472b8Kk70p0xRFyECCTii77H964HDU=; b=PFAUj/wY4i4QIiXl6Jn7Pq6ikd+3FujJkStO4CHOZZRvAhCS8NXcwS7swN3qVUYC6V pd7wOhZJp7AP0hqGeBVBkSTu9RU/fYnoDuyquo8r0cIqS16w+NN+MSTTWqjo8NGJ2xWB 8vXpFqqstkjGZhg1IwIOJlSpwXytnywSyU/YRWj18FHQ/8DEJOhm0uHJdQtQaI5uMSAX 9N+kXl4BZHwNyrGsMlGZLBiUnl3SvoEyjfZDXXDjhy5r6LlrhV9qhLMhy3Kz5XCuq/mU b/K3QK7AnQ17QnoRSOem8qOe09AmhnxM3iZUalo1Z80teny/kjMldJBdnoIEZkuxnfHT xhRw== X-Gm-Message-State: AOAM5329Ghp9jwJOoY0yvtASWEJouH2jDFuhigcWJQy4oo0k1HqGoP7i PhMd8bB+xYr8swvdzK5CWwbkVQ2o4Yg= X-Google-Smtp-Source: ABdhPJxnPhpG0uEJI9DJoVTFOYCrTY3RB0JP4z6RULFaJ42O+0r53lBkjddcqIc/SmkeFz+GUYo8pg== X-Received: by 2002:aa7:8509:0:b029:2e5:8cfe:bc17 with SMTP id v9-20020aa785090000b02902e58cfebc17mr16502487pfn.2.1624229431084; Sun, 20 Jun 2021 15:50:31 -0700 (PDT) Received: from gnu-cfl-2.localdomain ([172.56.39.115]) by smtp.gmail.com with ESMTPSA id s16sm13456532pfc.33.2021.06.20.15.50.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Jun 2021 15:50:30 -0700 (PDT) Received: from gnu-cfl-2.. (localhost [IPv6:::1]) by gnu-cfl-2.localdomain (Postfix) with ESMTP id 3EBC8C0361 for ; Sun, 20 Jun 2021 15:50:29 -0700 (PDT) From: "H.J. Lu" To: binutils@sourceware.org Subject: [PATCH 0/2] elf: Implement single global definition marker Date: Sun, 20 Jun 2021 15:50:27 -0700 Message-Id: <20210620225029.390239-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3024.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=no autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Sun, 20 Jun 2021 22:50:36 -0000 On systems with copy relocation: * A copy in executable is created for the definition in a shared library at run-time by ld.so. * The copy is referenced by executable and shared libraries. * Executable can access the copy directly. Issues are: * Overhead of a copy, time and space, may be visible at run-time. * Read-only data in the shared library becomes read-write copy in executable at run-time. * Local access to data with the STV_PROTECTED visibility in the shared library must use GOT. On systems without function descriptor, function pointers vary depending on where and how the functions are defined. * If the function is defined in executable, it can be the address of function body. * If the function, including the function with STV_PROTECTED visibility, is defined in the shared library, it can be the address of the PLT entry in executable or shared library. Issues are: * The address of function body may not be used as its function pointer. * ld.so needs to search loaded shared libraries for the function pointer of the function with STV_PROTECTED visibility. Here is a proposal to remove copy relocation and use canonical function pointer: 1. Accesses, including in PIE and non-PIE, to undefined symbols must use GOT. a. Linker may optimize out GOT access if the data is defined in PIE or non-PIE. 2. Read-only data in the shared library remain read-only at run-time 3. Address of global data with the STV_PROTECTED visibility in the shared library is the address of data body. a. Can use IP-relative access. b. May need GOT without IP-relative access. 4. For systems without function descriptor, a. All global function pointers of undefined functions in PIE and non-PIE must use GOT. Linker may optimize out GOT access if the function is defined in PIE or non-PIE. b. Function pointer of functions with the STV_PROTECTED visibility in executable and shared library is the address of function body. i. Can use IP-relative access. ii. May need GOT without IP-relative access. iii. Branches to undefined functions may use PLT. 5. Single global definition marker: Add GNU_PROPERTY_1_NEEDED: #define GNU_PROPERTY_1_NEEDED GNU_PROPERTY_UINT32_OR_LO to indicate the needed properties by the object file. Add GNU_PROPERTY_1_NEEDED_SINGLE_GLOBAL_DEFINITION: #define GNU_PROPERTY_1_NEEDED_SINGLE_GLOBAL_DEFINITION (1U << 0) to indicate that the object file requires canonical function pointers and cannot be used with copy relocation. a. Copy relocation should be disallowed at link-time and run-time. b. Canonical function pointers are required at link-time and run-tima Linker change: If any relocatable input files contain the single global definition marker: * Generate the single global definition marker in output. * Avoid copy relocation if possible. * Access to symbols with the STV_PROTECTED visibility is the same as local access. * For systems without function descriptor, function pointer is the address of function body. H.J. Lu (2): elf: Add GNU_PROPERTY_1_NEEDED elf: Add GNU_PROPERTY_1_NEEDED check bfd/elf-properties.c | 103 ++++++++++++-- bfd/elflink.c | 4 + binutils/readelf.c | 39 ++++++ include/bfdlink.h | 6 + include/elf/common.h | 7 + ld/NEWS | 3 + ld/emultempl/elf.em | 4 + ld/ld.texi | 12 ++ ld/ldmain.c | 1 + ld/lexsup.c | 5 + ld/testsuite/ld-elf/property-1_needed-1.s | 15 ++ ld/testsuite/ld-elf/property-1_needed-1a.d | 17 +++ ld/testsuite/ld-elf/property-1_needed-1b.d | 16 +++ ld/testsuite/ld-elf/property-1_needed-1c.d | 17 +++ ld/testsuite/ld-x86-64/protected-data-1.h | 11 ++ ld/testsuite/ld-x86-64/protected-data-1a.c | 40 ++++++ ld/testsuite/ld-x86-64/protected-data-1b.c | 59 ++++++++ ld/testsuite/ld-x86-64/protected-data-2a.S | 109 +++++++++++++++ ld/testsuite/ld-x86-64/protected-data-2b.S | 119 ++++++++++++++++ ld/testsuite/ld-x86-64/protected-func-2a.S | 68 +++++++++ ld/testsuite/ld-x86-64/protected-func-2b.S | 83 +++++++++++ ld/testsuite/ld-x86-64/protected-func-2c.c | 29 ++++ .../ld-x86-64/single-global-definition.rd | 6 + ld/testsuite/ld-x86-64/x86-64.exp | 131 ++++++++++++++++++ 24 files changed, 889 insertions(+), 15 deletions(-) create mode 100644 ld/testsuite/ld-elf/property-1_needed-1.s create mode 100644 ld/testsuite/ld-elf/property-1_needed-1a.d create mode 100644 ld/testsuite/ld-elf/property-1_needed-1b.d create mode 100644 ld/testsuite/ld-elf/property-1_needed-1c.d create mode 100644 ld/testsuite/ld-x86-64/protected-data-1.h create mode 100644 ld/testsuite/ld-x86-64/protected-data-1a.c create mode 100644 ld/testsuite/ld-x86-64/protected-data-1b.c create mode 100644 ld/testsuite/ld-x86-64/protected-data-2a.S create mode 100644 ld/testsuite/ld-x86-64/protected-data-2b.S create mode 100644 ld/testsuite/ld-x86-64/protected-func-2a.S create mode 100644 ld/testsuite/ld-x86-64/protected-func-2b.S create mode 100644 ld/testsuite/ld-x86-64/protected-func-2c.c create mode 100644 ld/testsuite/ld-x86-64/single-global-definition.rd -- 2.31.1