[PATCH] pc-bios/s390-ccw: link statically

Sertonix posted 1 patch 5 months, 1 week ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/DAJ1QOSAP9LS.342SQSM0UZU80@posteo.net
Maintainers: Christian Borntraeger <borntraeger@linux.ibm.com>, Thomas Huth <thuth@redhat.com>
pc-bios/s390-ccw/Makefile | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] pc-bios/s390-ccw: link statically
Posted by Sertonix 5 months, 1 week ago

Adding -pie to LDFLAGS caused s390-ccw.img to become dynamically linked.
By using -static-pie it will be linked statically like other bios.

This ensures that the build output doesn't change depending on the
default dynamic loader path of the toolchain.

Ref d884c86dcd3b s390/bios: Make the s390-ccw.img relocatable

Signed-off-by: Sertonix <sertonix@posteo.net>
---
 pc-bios/s390-ccw/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/pc-bios/s390-ccw/Makefile b/pc-bios/s390-ccw/Makefile
index dc69dd484f..a0f24c94a8 100644
--- a/pc-bios/s390-ccw/Makefile
+++ b/pc-bios/s390-ccw/Makefile
@@ -47,7 +47,7 @@ EXTRA_CFLAGS += -fwrapv -fno-strict-aliasing -fno-asynchronous-unwind-tables
 EXTRA_CFLAGS += -msoft-float
 EXTRA_CFLAGS += -std=gnu99
 EXTRA_CFLAGS += $(LIBC_INC) $(LIBNET_INC)
-EXTRA_LDFLAGS += -Wl,-pie -nostdlib -z noexecstack -z text
+EXTRA_LDFLAGS += -static-pie -nostdlib -z noexecstack -z text
 
 cc-test = $(CC) -Werror $1 -c -o /dev/null -xc /dev/null >/dev/null 2>/dev/null
 cc-option = if $(call cc-test, $1); then \
-- 
2.49.0
Re: [PATCH] pc-bios/s390-ccw: link statically
Posted by Michael Tokarev 5 months, 1 week ago
On 10.06.2025 20:58, Sertonix wrote:
> 
> Adding -pie to LDFLAGS caused s390-ccw.img to become dynamically linked.

Why do you think -pie causes it to become dynamically linked?

/mjt
Re: [PATCH] pc-bios/s390-ccw: link statically
Posted by Sertonix 5 months, 1 week ago
On Tue Jun 10, 2025 at 10:32 PM CEST, Michael Tokarev wrote:
> On 10.06.2025 20:58, Sertonix wrote:
>> 
>> Adding -pie to LDFLAGS caused s390-ccw.img to become dynamically linked.
>
> Why do you think -pie causes it to become dynamically linked?
>
> /mjt

The documentation (at least for gcc) states that the -pie option produces
a *dynamically* linked position independent executable. (And I verified
that the patch changes the resulting binary from dynamically linked to
statically linked).

Ref https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#index-pie
Re: [PATCH] pc-bios/s390-ccw: link statically
Posted by Michael Tokarev 5 months, 1 week ago
On 10.06.2025 23:36, Sertonix wrote:
> On Tue Jun 10, 2025 at 10:32 PM CEST, Michael Tokarev wrote:
>> On 10.06.2025 20:58, Sertonix wrote:
>>>
>>> Adding -pie to LDFLAGS caused s390-ccw.img to become dynamically linked.
>>
>> Why do you think -pie causes it to become dynamically linked?
>>
>> /mjt
> 
> The documentation (at least for gcc) states that the -pie option produces
> a *dynamically* linked position independent executable. (And I verified
> that the patch changes the resulting binary from dynamically linked to
> statically linked).

Ok.

Why I asked is because -pie by its own does not change "dynaminess" of
an executable.

-pie has been introduced for s390-ccw in commit d884c86dcd "s390/bios:
Make the s390-ccw.img relocatable" (9 Mar 2015).  Before this commit,
s390x-ccw.img has been dynamically linked too.  Now with current
master (commit bc98ffdc75), removing -Wl,-fpie from LDFLAGS does not
change the fact that the image is linked dynamically.

I don't know when s390-ccw.img has become a dynamic executable, maybe
it always has been - I haven't looked at earlier commits.  With this,
I've no idea how it works either, as a dynamic executable with an
interpreter etc.  But it's definitely not -pie added to LDFLAGS which
made it dynamically linked.

On the other hand, people sometimes confuse the result of -static-pie
as a dynamic executable, especially with older file(1) utility which
reported it as dynamically linked.

So my question was badly worded: what I wanted to ask is what makes
you think that adding -pie to the link line makes the resulting
executable to be linked dynamically.

With all the above in mind, I think the commit message is misleading
a bit.  But it'd be interesting to clarify how this image, being
a dynamic executable, worked all these years, and why it has to be
changed now.  I'm definitely not opposing to the change, I just am
curious about this aspect and want a correct commit message.

Thanks,

/mjt
Re: [PATCH] pc-bios/s390-ccw: link statically
Posted by Sertonix 5 months, 1 week ago
On Wed Jun 11, 2025 at 6:07 AM CEST, Michael Tokarev wrote:
> On 10.06.2025 23:36, Sertonix wrote:
>> On Tue Jun 10, 2025 at 10:32 PM CEST, Michael Tokarev wrote:
>>> On 10.06.2025 20:58, Sertonix wrote:
>>>>
>>>> Adding -pie to LDFLAGS caused s390-ccw.img to become dynamically linked.
>>>
>>> Why do you think -pie causes it to become dynamically linked?
>>>
>>> /mjt
>> 
>> The documentation (at least for gcc) states that the -pie option produces
>> a *dynamically* linked position independent executable. (And I verified
>> that the patch changes the resulting binary from dynamically linked to
>> statically linked).
>
> Ok.
>
> Why I asked is because -pie by its own does not change "dynaminess" of
> an executable.

I am uncertain what you mean with "dynaminess" but I mean the ELF file
type, whether or not an interpreter is set and some additional sections
(like .dynamic) exist.

> -pie has been introduced for s390-ccw in commit d884c86dcd "s390/bios:
> Make the s390-ccw.img relocatable" (9 Mar 2015).  Before this commit,
> s390x-ccw.img has been dynamically linked too.  Now with current
> master (commit bc98ffdc75), removing -Wl,-fpie from LDFLAGS does not
> change the fact that the image is linked dynamically.

The prebuild binary before d884c86dcd was added in 553ce81c31e4 and shows as
statically linked: (ET_EXEC is the ELF file type of static binaries)

~/src/qemu$ scanelf pc-bios/s390-ccw.img
 TYPE   FILE 
ET_EXEC pc-bios/s390-ccw.img 

and when the binary was rebuild after d884c86dcd in 2d5eeef1c0be it shows
as dynamically linked:

~/src/qemu$ scanelf pc-bios/s390-ccw.img
 TYPE   FILE 
ET_DYN pc-bios/s390-ccw.img 

(Same results with file(1) and readelf -l)

> On the other hand, people sometimes confuse the result of -static-pie
> as a dynamic executable, especially with older file(1) utility which
> reported it as dynamically linked.

Latest file(1) release:
$ file --version
file-5.46
magic file from /usr/share/misc/magic

> So my question was badly worded: what I wanted to ask is what makes
> you think that adding -pie to the link line makes the resulting
> executable to be linked dynamically.
>
> With all the above in mind, I think the commit message is misleading
> a bit.  But it'd be interesting to clarify how this image, being
> a dynamic executable, worked all these years, and why it has to be
> changed now.

As mentioned above this is only about the file metadata and doesn't
affect the binary code part of the binary. So it is probably just
ignored by qemu and shouldn't change the bhaviour of qemu. This is
mostly to fix the intend of the commit that added -pie and exclude a
libc dependant string from a binary that isn't libc dependant.

> I'm definitely not opposing to the change, I just am
> curious about this aspect and want a correct commit message.
>
> Thanks,
>
> /mjt
Re: [PATCH] pc-bios/s390-ccw: link statically
Posted by Thomas Huth 5 months ago
On 11/06/2025 07.54, Sertonix wrote:
> On Wed Jun 11, 2025 at 6:07 AM CEST, Michael Tokarev wrote:
>> On 10.06.2025 23:36, Sertonix wrote:
>>> On Tue Jun 10, 2025 at 10:32 PM CEST, Michael Tokarev wrote:
>>>> On 10.06.2025 20:58, Sertonix wrote:
>>>>>
>>>>> Adding -pie to LDFLAGS caused s390-ccw.img to become dynamically linked.
>>>>
>>>> Why do you think -pie causes it to become dynamically linked?
>>>>
>>>> /mjt
>>>
>>> The documentation (at least for gcc) states that the -pie option produces
>>> a *dynamically* linked position independent executable. (And I verified
>>> that the patch changes the resulting binary from dynamically linked to
>>> statically linked).
>>
>> Ok.
>>
>> Why I asked is because -pie by its own does not change "dynaminess" of
>> an executable.
> 
> I am uncertain what you mean with "dynaminess" but I mean the ELF file
> type, whether or not an interpreter is set and some additional sections
> (like .dynamic) exist.
> 
>> -pie has been introduced for s390-ccw in commit d884c86dcd "s390/bios:
>> Make the s390-ccw.img relocatable" (9 Mar 2015).  Before this commit,
>> s390x-ccw.img has been dynamically linked too.  Now with current
>> master (commit bc98ffdc75), removing -Wl,-fpie from LDFLAGS does not
>> change the fact that the image is linked dynamically.
> 
> The prebuild binary before d884c86dcd was added in 553ce81c31e4 and shows as
> statically linked: (ET_EXEC is the ELF file type of static binaries)
> 
> ~/src/qemu$ scanelf pc-bios/s390-ccw.img
>   TYPE   FILE
> ET_EXEC pc-bios/s390-ccw.img
> 
> and when the binary was rebuild after d884c86dcd in 2d5eeef1c0be it shows
> as dynamically linked:
> 
> ~/src/qemu$ scanelf pc-bios/s390-ccw.img
>   TYPE   FILE
> ET_DYN pc-bios/s390-ccw.img
> 
> (Same results with file(1) and readelf -l)

I was initially also surprised by this (since there was no "-static" flag in 
the Makefile before the change to "-pie"), but I can also reproduce these 
results. I didn't find anything related to that in the documentation, but it 
seems like "-nostdlib" without "-pie" causes the linker to emit a statically 
linked binary, and with "-pie" it's dynamically instead?

Anyway, this patch here looks fine to me, so I'll queue it for the next time 
when the s390-ccw.img needs an update (might take a while, though, so please 
be patient).

  Thanks,
   Thomas