Shi, Steven via llvm-dev
2019-Jan-23 15:47 UTC
[llvm-dev] Why -pie option force LLD to output shared obj file type, not executable?
Hello Rui,
I'm enabling the LLD in the Uefi firmware edk2 build. I meet a problem about
the -pie option and cannot output the executable type obj file correctly. I need
your advice.
The Uefi firmware executable binary is the position independent + small code
mode in 64bits. So we always add the options "-Wl,-pie -mcmodel=small"
in our clang build toolchain. These options work well with binutils LD, but
cannot work with LLD. I see the LLD uses the below code to force the output obj
file as shared obj type if link with -pie. Is there any way to let LLD output
executable type obj with -pie option? I'm OK if I have to link twice, first
output DYN and then covert to EXEC in some way. Could you give some link command
examples on it?
lld\ELF\Writer.cpp
static uint16_t getELFType() {
if (Config->Pic)
return ET_DYN;
if (Config->Relocatable)
return ET_REL;
return ET_EXEC;
}
Thanks
Steven
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20190123/3d35f540/attachment.html>
Peter Smith via llvm-dev
2019-Jan-23 16:13 UTC
[llvm-dev] Why -pie option force LLD to output shared obj file type, not executable?
Hello Steven, I'm not Rui, but I may be able to help. We recently ran into something like this on AArch64 with pr39810 lld would not accept --pie and --shared simultaneously. In general even on ld.bfd use of the --pie linker option sets the file type to ET_DYN, ld.bfd has a special case when the start of the text section is non 0 (I think). This was described in https://sourceware.org/ml/binutils/2013-12/msg00081.html . As far as I know this was for non-position independent executables that had been compiled -fPIE just to get the small code model. In the AArch64 case the problem was that --shared was being used to work around ld.bfd using ET_EXEC, unfortunately it seems like you need the ET_EXEC behaviour? If so I don't think that there are any flags that you can give to lld to change this. My understanding is that the only difference in the file ld.bfd generates is the e_type field in the ELF header. It would be trivial to write a quick tool to change the field from ET_DYN to ET_EXEC. Hope this is of some use. Peter On Wed, 23 Jan 2019 at 15:47, Shi, Steven via llvm-dev <llvm-dev at lists.llvm.org> wrote:> > Hello Rui, > > I’m enabling the LLD in the Uefi firmware edk2 build. I meet a problem about the -pie option and cannot output the executable type obj file correctly. I need your advice. > > The Uefi firmware executable binary is the position independent + small code mode in 64bits. So we always add the options “-Wl,-pie -mcmodel=small” in our clang build toolchain. These options work well with binutils LD, but cannot work with LLD. I see the LLD uses the below code to force the output obj file as shared obj type if link with -pie. Is there any way to let LLD output executable type obj with -pie option? I’m OK if I have to link twice, first output DYN and then covert to EXEC in some way. Could you give some link command examples on it? > > > > lld\ELF\Writer.cpp > > static uint16_t getELFType() { > > if (Config->Pic) > > return ET_DYN; > > if (Config->Relocatable) > > return ET_REL; > > return ET_EXEC; > > } > > > > > > Thanks > > Steven > > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
Rui Ueyama via llvm-dev
2019-Jan-23 19:01 UTC
[llvm-dev] Why -pie option force LLD to output shared obj file type, not executable?
lld sets ET_DYN instead of ET_EXEC if `-pic` is specified, which is
basically the same behavior except the case that Peter explained.
Unfortunately, there's no way to set ET_EXEC using lld, but the
"type"
field is 2 byte long and at offset 16, so you can edit it using a binary
editor to change it from ET_DYN to ET_EXEC (although it's super hacky).
If you have GNU sed, you can do with the following command to change the
field value:
sed -E -i -e '1s/^(.{16})../\1\x2\x0/' your-executable-file
Does that work for you?
On Wed, Jan 23, 2019 at 7:47 AM Shi, Steven <steven.shi at intel.com>
wrote:
> Hello Rui,
>
> I’m enabling the LLD in the Uefi firmware edk2 build. I meet a problem
> about the -pie option and cannot output the executable type obj file
> correctly. I need your advice.
>
> The Uefi firmware executable binary is the position independent + small
> code mode in 64bits. So we always add the options “-Wl,-pie -mcmodel=small”
> in our clang build toolchain. These options work well with binutils LD, but
> cannot work with LLD. I see the LLD uses the below code to force the output
> obj file as shared obj type if link with -pie. Is there any way to let LLD
> output executable type obj with -pie option? I’m OK if I have to link
> twice, first output DYN and then covert to EXEC in some way. Could you give
> some link command examples on it?
>
>
>
> lld\ELF\Writer.cpp
>
> static uint16_t getELFType() {
>
> if (Config->Pic)
>
> return ET_DYN;
>
> if (Config->Relocatable)
>
> return ET_REL;
>
> return ET_EXEC;
>
> }
>
>
>
>
>
> Thanks
>
> Steven
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20190123/b61f284f/attachment.html>
Shi, Steven via llvm-dev
2019-Jan-28 12:59 UTC
[llvm-dev] Why -pie option force LLD to output shared obj file type, not executable?
Hi Peter, Rui
Thank you for the info.
Thanks
Steven
From: Rui Ueyama [mailto:ruiu at google.com]
Sent: Thursday, January 24, 2019 3:02 AM
To: Shi, Steven <steven.shi at intel.com>
Cc: llvm-dev at lists.llvm.org
Subject: Re: Why -pie option force LLD to output shared obj file type, not
executable?
lld sets ET_DYN instead of ET_EXEC if `-pic` is specified, which is basically
the same behavior except the case that Peter explained. Unfortunately,
there's no way to set ET_EXEC using lld, but the "type" field is 2
byte long and at offset 16, so you can edit it using a binary editor to change
it from ET_DYN to ET_EXEC (although it's super hacky).
If you have GNU sed, you can do with the following command to change the field
value:
sed -E -i -e '1s/^(.{16})../\1\x2\x0/' your-executable-file
Does that work for you?
On Wed, Jan 23, 2019 at 7:47 AM Shi, Steven <steven.shi at
intel.com<mailto:steven.shi at intel.com>> wrote:
Hello Rui,
I’m enabling the LLD in the Uefi firmware edk2 build. I meet a problem about the
-pie option and cannot output the executable type obj file correctly. I need
your advice.
The Uefi firmware executable binary is the position independent + small code
mode in 64bits. So we always add the options “-Wl,-pie -mcmodel=small” in our
clang build toolchain. These options work well with binutils LD, but cannot work
with LLD. I see the LLD uses the below code to force the output obj file as
shared obj type if link with -pie. Is there any way to let LLD output executable
type obj with -pie option? I’m OK if I have to link twice, first output DYN and
then covert to EXEC in some way. Could you give some link command examples on
it?
lld\ELF\Writer.cpp
static uint16_t getELFType() {
if (Config->Pic)
return ET_DYN;
if (Config->Relocatable)
return ET_REL;
return ET_EXEC;
}
Thanks
Steven
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://lists.llvm.org/pipermail/llvm-dev/attachments/20190128/3eab252a/attachment.html>