<div dir="ltr">Hi Steven,<div><br></div><div>I need to take a look in more detail, but I believe that magic number is hard-coded without any particular reason and we can change that to match the behavior of MSVC linker.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Aug 3, 2019 at 11:14 PM Shi, Steven <<a href="mailto:steven.shi@intel.com">steven.shi@intel.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div lang="EN-US">
<div class="gmail-m_-2290075823571032418WordSection1">
<p class="MsoNormal">Hi Rui,<u></u><u></u></p>
<p class="MsoNormal">We meet a problem when enable the <b>execute in place</b> (XIP,
<a href="https://en.wikipedia.org/wiki/Execute_in_place" target="_blank">https://en.wikipedia.org/wiki/Execute_in_place</a>) for uefi firmware with lld-link. We need to set the COFF executable file SectionAlignment through the lld-link /ALIGN option, but we find the SectionAlignment
is hardcoded to 4096 which cause the /ALIGN option doesn’t work at all. Below is the hardcode in lld:<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:"Courier New"">lld\COFF\Writer.h<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:"Courier New""> static const int pageSize = 4096;<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:"Courier New"">lld\COFF\Writer.cpp<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:10pt;font-family:"Courier New""> pe->SectionAlignment = pageSize;<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">The uefi firmware need to directly run in ROM device (in XIP way) before the system memory is ready. These XIP executable files are not loaded in memory and their section running alignment is just same as the alignment in file. With MSVC
link.exe, we can use the /ALIGN option to set SectionAlignment same as FileAlignment to ensure the XIP executable file has correct running alignment. But with lld-link, we cannot change the XIP file SectionAlignment value 4096 because it is hardcoded. So,
we have to change the FileAlignment to 4096 to match the SectionAlignment instead, but the big FileAlignment value (4096) cause the firmware XIP code size is +10 times larger than normal, which is not affordable for firmware.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Could we not hardcode the SectionAlignment as 4096 in COFF file and let the lld-link respect the user’s /ALIGN option value? We can set the 4096 as the default SectionAlignment value.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Below are some steps to show the hardcode issue and code size impact:<u></u><u></u></p>
<p class="MsoNormal">$ cat main.c<u></u><u></u></p>
<p class="MsoNormal">int main()<u></u><u></u></p>
<p class="MsoNormal">{<u></u><u></u></p>
<p class="MsoNormal"> return 0;<u></u><u></u></p>
<p class="MsoNormal">}<u></u><u></u></p>
<p class="MsoNormal">$ "/home/jshi19/llvm/llvm-project/releaseinstall/bin/clang" -target i686-unknown-windows -fno-builtin -c -o main.obj main.c<u></u><u></u></p>
<p class="MsoNormal">$ "/home/jshi19/llvm/llvm-project/releaseinstall/bin/lld-link" /OUT:main.dll /MACHINE:X86 /DLL /ENTRY:main /ALIGN:32 /FILEALIGN:32 main.obj<u></u><u></u></p>
<p class="MsoNormal">$ sudo apt install pev<u></u><u></u></p>
<p class="MsoNormal">$ readpe -H main.dll<u></u><u></u></p>
<p class="MsoNormal">…<u></u><u></u></p>
<p class="MsoNormal"> Alignment of sections: <span style="color:red">0x1000</span><u></u><u></u></p>
<p class="MsoNormal" style="text-indent:9pt">Alignment factor: 0x20<u></u><u></u></p>
<p class="MsoNormal">…<u></u><u></u></p>
<p class="MsoNormal">$ ll main.dll, see main.dll size is 544<u></u><u></u></p>
<p class="MsoNormal">-rwxrwxr-x 1 jshi19 jshi19 <span style="color:red">544 </span>
Aug 3 21:58 main.dll*<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">If have to change the FileAlignment to 4096 to match the SectionAlignment:<u></u><u></u></p>
<p class="MsoNormal">$ "/home/jshi19/llvm/llvm-project/releaseinstall/bin/lld-link" /OUT:main.dll /MACHINE:X86 /DLL /ENTRY:main /ALIGN:32 /FILEALIGN:<span style="color:red">4096
</span>main.obj<u></u><u></u></p>
<p class="MsoNormal">$ readpe -H main.dll<u></u><u></u></p>
<p class="MsoNormal">…<u></u><u></u></p>
<p class="MsoNormal"> Alignment of sections: <span style="color:red">0x1000</span><u></u><u></u></p>
<p class="MsoNormal" style="text-indent:9pt">Alignment factor: <span style="color:red">
0x1000</span><u></u><u></u></p>
<p class="MsoNormal">…<u></u><u></u></p>
<p class="MsoNormal">$ ll main.dll, see main.dll size is 8192 which ~15 timer larger.<u></u><u></u></p>
<p class="MsoNormal">-rwxrwxr-x 1 jshi19 jshi19 <span style="color:red">8192 </span>
Aug 3 22:05 main.dll*<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Thanks<u></u><u></u></p>
<p class="MsoNormal">Steven <u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</blockquote></div>