[llvm-dev] lld-link /ALIGN option doesn't work for the "execute in place (XIP)"

Shi, Steven via llvm-dev llvm-dev at lists.llvm.org
Sat Aug 3 07:14:28 PDT 2019


Hi Rui,
We meet a problem when enable the execute in place (XIP, https://en.wikipedia.org/wiki/Execute_in_place) 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:

lld\COFF\Writer.h
  static const int pageSize = 4096;
lld\COFF\Writer.cpp
  pe->SectionAlignment = pageSize;

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.

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.


Below are some steps to show the hardcode issue and code size impact:
$ cat main.c
int main()
{
  return 0;
}
$ "/home/jshi19/llvm/llvm-project/releaseinstall/bin/clang" -target i686-unknown-windows -fno-builtin -c -o main.obj  main.c
$ "/home/jshi19/llvm/llvm-project/releaseinstall/bin/lld-link" /OUT:main.dll /MACHINE:X86 /DLL /ENTRY:main /ALIGN:32 /FILEALIGN:32 main.obj
$ sudo apt install pev
$ readpe -H main.dll
...
    Alignment of sections:        0x1000
Alignment factor:                0x20
...
$ ll main.dll, see main.dll size is 544
-rwxrwxr-x 1 jshi19 jshi19 544 Aug  3 21:58 main.dll*

If have to change the FileAlignment to 4096 to match the SectionAlignment:
$ "/home/jshi19/llvm/llvm-project/releaseinstall/bin/lld-link" /OUT:main.dll /MACHINE:X86 /DLL /ENTRY:main /ALIGN:32 /FILEALIGN:4096 main.obj
$ readpe -H main.dll
...
    Alignment of sections:        0x1000
Alignment factor:                0x1000
...
$ ll main.dll, see main.dll size is 8192 which ~15 timer larger.
-rwxrwxr-x 1 jshi19 jshi19 8192 Aug  3 22:05 main.dll*



Thanks
Steven

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190803/4784345d/attachment.html>


More information about the llvm-dev mailing list