From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yb1-xb34.google.com (mail-yb1-xb34.google.com [IPv6:2607:f8b0:4864:20::b34]) by sourceware.org (Postfix) with ESMTPS id 1F2FE385842D for ; Tue, 28 May 2024 12:24:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1F2FE385842D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1F2FE385842D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::b34 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716899095; cv=none; b=UYysgM4ItVC9Jz75CrHSuD9gcGp6iQo1RtOCFG45FteCoqXv6Poq16d8xSf8PnKXF2AWHZ2fklDC0SrE4hAg0o1/yqy3o9RpVKALIcYqmi3/0l1OnNwhU9hpZrLJuXFIQIL3UaMljOfV+POdkh0nDBAdUYPmv3eFu23VqqCVDBg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716899095; c=relaxed/simple; bh=lR7o2CecRt64PQIaegQzk2y6DHQs1c8W9nyCJTgOSwQ=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=NwcXBIRLs/iCJozhSNXGJsZ6JUmJuFXoBegGltytw33NXhGS3hObXjoJvvrcHw+W8djbxe5svc3s/sVC/DWzwJ5PAC+MJeO3In4EQNm4msovNT41ZEpFKqb/BLLe5PLcZJoRnOpShDavib7WLs+5ydpu5fnLkasn/bRVNE7rNiE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yb1-xb34.google.com with SMTP id 3f1490d57ef6-df79380ffceso1828984276.1 for ; Tue, 28 May 2024 05:24:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716899092; x=1717503892; darn=sourceware.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=CkdQgziNefyebcqb5zDqP1qoV/sANTs/r8seHkmxn6k=; b=RpTLh3Mehz9U7ao5f93guZHM2g5gfpby62odQLs8A5Kzv4Cq0kT4BUYgVPPZ9mgLSr 75+JD+w9wyb/HhUuqZIVryzWagPohBYNCvGkMZADpDNtYBxd53KL6qoBYP2QWeuStkwu i4BlelQWQYCzAbQFaNw1QduSJ7JrrXChVEDQkaZMwR3JaF4+Ro+SPASlePeF1G8LQQ4+ Faz54a1RwTf6zdsx4w3zVVxIuFG6klT+v4wty43TaX4XZNn91e8sl/4UxxdQ32xCAu1b SaEOThakZ6d3d8Utl3NDvLqRzHYVhYlBTPAAUZBs6ZLmSnkbxLmMkUREWAVcOkk1sCWp L3gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716899092; x=1717503892; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=CkdQgziNefyebcqb5zDqP1qoV/sANTs/r8seHkmxn6k=; b=pZWFZtaSRmVtPQMpIndBawYiuvMrLjCyVKjJjH0zlcjaaEsk+J4/qHTla8P6enhHnd 1SyntxT33Pl4knfNyEW2+lEYYpSfIu13dWeP7osEkRAKYUkCfzhAnWruzqDT7Nn19Cbz ONSPpk+WPjehj3sSbKSJeZfm1+dMvWPXVyh4E1f3UdADb8knUAmY0aZCmi0MkMkO1rLY fn8WlFfkbp5axE2SJiN0UX3oSXpYqX8seClAlQuX1BMzGxfHDt2ATm9Z/LaY3T06/U7Z TOL208jVUyG+uU1agLhZHW9BE+JDvYVJN7AQSUdoHxfxDBE6w1+XR/8ADUC2QL1E4tnf gMMg== X-Gm-Message-State: AOJu0YwGXb8eBgkPZBUqHaYoovwekGwAgDTgJQkMBqjYs71MAgl4W34u I3D+T15RLHJ2UKdCGbfJE9C0ApH8dW/0JO6FTWtOcJp8MVDF3cuPkvdzKmk/uWcq86U2oCCgbdK AsWbmycjdFbK4nYmICw70Gpn0DVY= X-Google-Smtp-Source: AGHT+IFV1zL+FzmQFm7XJ6CV0qQtEQkYpCdvrvdveHswktKx76beYSm4Zy3LaqoXwnqYWFBPBHKcq4PPEnZwN3F5Iw0= X-Received: by 2002:a25:c846:0:b0:de6:a86c:b84e with SMTP id 3f1490d57ef6-df542254824mr11034946276.16.1716899092397; Tue, 28 May 2024 05:24:52 -0700 (PDT) MIME-Version: 1.0 References: <20240528042803.2007198-1-maskray@google.com> In-Reply-To: <20240528042803.2007198-1-maskray@google.com> From: "H.J. Lu" Date: Tue, 28 May 2024 05:24:16 -0700 Message-ID: Subject: Re: [PATCH] Don't set ET_EXEC for -pie -Ttext-segment= (PR31795) To: Fangrui Song Cc: binutils@sourceware.org, mintsuki@protonmail.com, Fangrui Song Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3018.7 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,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 List-Id: On Mon, May 27, 2024 at 9:28=E2=80=AFPM Fangrui Song w= rote: > > From: Fangrui Song > > Commit 58e7ebacdd97c858834c07c7dce098aeacd500fb added the special case > that sets ET_EXEC for -pie -Ttext-segment=3D$non_zero. This was to > support a way[1] to ensure that the address of the first byte is >=3D > -Ttext-segment=3D. > > % cat a.c > #include > int main() { printf("%p\n", main); } > % gcc -pie a.c -fuse-ld=3Dbfd -Wl,--no-relax,-Ttext-segment=3D0x60000= 0000000 -o a > % ./a > 0x600000001139 > % ./a > 0x600000001139 # no ASLR > > While changing ET_DYN to ET_EXEC fulfills the address requirement, it > disables ASLR, which may be undesired. (While Linux kernel/glibc do not > provide the address>=3Dp_vaddr guarantee, this is technically feasible to > support.) > > Since commit e8f6c2a5bab10b039a12b69a30a8248c91161e11, --no-pie has been > available and we can do the following to use crtbeginS.o and ET_EXEC. > (Using crtbegin.o could lead to R_X86_64_32/R_X86_64_32S relocation > issues with a high address.) > > % gcc -pie -Wl,-no-pie a.c -fuse-ld=3Dbfd -Wl,--no-relax,-Ttext-segme= nt=3D0x600000000000 -o a > % ./a > 0x600000001139 > % ./a > 0x600000001139 # no ASLR > > [1]: https://sourceware.org/pipermail/binutils/2013-December/083381.html > --- > bfd/elf.c | 26 +++----------------------- > ld/testsuite/ld-pie/vaddr-1.d | 2 +- > 2 files changed, 4 insertions(+), 24 deletions(-) > > diff --git a/bfd/elf.c b/bfd/elf.c > index 74236a658fd..a8a9be53c17 100644 > --- a/bfd/elf.c > +++ b/bfd/elf.c > @@ -7150,35 +7150,15 @@ _bfd_elf_init_file_header (bfd *abfd, > return true; > } > > -/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=3D. > - > - FIXME: We used to have code here to sort the PT_LOAD segments into > +/* FIXME: We used to have code here to sort the PT_LOAD segments into > ascending order, as per the ELF spec. But this breaks some programs, > including the Linux kernel. But really either the spec should be > changed or the programs updated. */ > > bool > -_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info) > +_bfd_elf_modify_headers (bfd *obfd ATTRIBUTE_UNUSED, > + struct bfd_link_info *link_info ATTRIBUTE_UNUSED= ) > { > - if (link_info !=3D NULL && bfd_link_pie (link_info)) > - { > - Elf_Internal_Ehdr *i_ehdrp =3D elf_elfheader (obfd); > - unsigned int num_segments =3D i_ehdrp->e_phnum; > - struct elf_obj_tdata *tdata =3D elf_tdata (obfd); > - Elf_Internal_Phdr *segment =3D tdata->phdr; > - Elf_Internal_Phdr *end_segment =3D &segment[num_segments]; > - > - /* Find the lowest p_vaddr in PT_LOAD segments. */ > - bfd_vma p_vaddr =3D (bfd_vma) -1; > - for (; segment < end_segment; segment++) > - if (segment->p_type =3D=3D PT_LOAD && p_vaddr > segment->p_vaddr) > - p_vaddr =3D segment->p_vaddr; > - > - /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD > - segments is non-zero. */ > - if (p_vaddr) > - i_ehdrp->e_type =3D ET_EXEC; This is required to create a small model executable guaranteed to be loaded at an address specified with -Wl,-Ttext-segment=3D0x100000000 on x86-64. This change will break such programs. > - } > return true; > } > > diff --git a/ld/testsuite/ld-pie/vaddr-1.d b/ld/testsuite/ld-pie/vaddr-1.= d > index 7b5f9922718..98eff841193 100644 > --- a/ld/testsuite/ld-pie/vaddr-1.d > +++ b/ld/testsuite/ld-pie/vaddr-1.d > @@ -5,5 +5,5 @@ > > ELF Header: > #... > - Type: EXEC \(Executable file\) > + Type: DYN \(Position-Independent Executab= le file\) > #pass > -- > 2.45.1.288.g0e0cd299f1-goog > --=20 H.J.