On 4. Apr 2023, at 09:23, Ard Biesheuvel <ardb@kernel.org> wrote:
On Mon, 3 Apr 2023 at 22:33, Rebecca Cran <rebecca@bsdio.com> wrote:
As part of my work on the toolchain definitions, I've come across a
situation where ld.lld fails to align sections correctly, due to it
being invoked via clang with the '-n' option, which causes GenFw to fail
with "Section address not aligned to its own alignment.".
Stupid question: if it breaks stuff, why do you use -n ?The following messages are printed:
ld.lld: warning: -z common-page-size set, but paging disabled by omagic
or nmagic ld.lld: warning: address (0x558) of section .data is not a
multiple of alignment (16)
I tracked the problem down to GccBase.lds and ClangBase.lds, which have:
/* * The alignment of the .data section should be less than or equal to
the * alignment of the .text section. This ensures that the relative
offset * between these sections is the same in the ELF and the PE/COFF
versions of * this binary. */ .data ALIGN(ALIGNOF(.text)) :
ALIGN(CONSTANT(COMMONPAGESIZE)) { *(.data .data.* .gnu.linkonce.d.*)
*(.bss .bss.*) }
I can work around the problem by removing "ALIGN(ALIGNOF(.text)", but
I'm wondering if our use of COMMONPAGESIZE/MAXPAGESIZE is correct.
What do you mean by 'correct'? The intent is clearly to declare themapping granule size, and for SEC/PEI binaries that execute in placefrom flash, the MMU page size is not the most useful quantum here.
We pass in a value of 0x20, 0x40 or 0x1000 to "-z common-page-size" with
0x20 being the most common value.
Given the page size of the target will never be 32 bytes, the following
comment on https://reviews.llvm.org/D61688 makes sense:
"There is at least one linkerscript in Tianocore edk2 that (ab)uses -n
-z common-page-size=0x20 to use CONSTANT(COMMONPAGESIZE) as if it were a
preprocessor macro set with -D in the compiler. The usual approach to
this is to pre-process the linkerscript."
'Page size' is highly context specific, and toolchain people are(imho) usually quite quick to call something abuse if it does notmatch their narrow definition of how a compiler or linker should beused. For the same reason, it has been so difficult to get a compilerto understand that the desire for position independent code does notimply that we want GOTs, or care about ELF symbol preemption orcopy-on-write footprint of relocatable segments. In general, the baremetal use case (which includes EFI) is quite poorly understood by manypeople working on toolchains.
I'm wondering what the correct approach is here: should we do something
similar to how we set PECOFF_HEADER_SIZE and define a SECTION_ALIGNMENT
symbol?
We cannot, that is the reason for using the page size switches here:using a symbol to set the location pointer is fine, but using a symbolto set the alignment of a section is not.Or, as discussed on Discord should we just use
CONSTANT(MAXPAGESIZE) and ignore how it's normally used to specify the
maximum allowable page size for an executable?
Note that (when I last checked), the only effect of setting -zxxx-page-size is that those macros assume the associated value in thelinker script. Nothing else in the linker changes behavior (with theexception of the warning you are seeing)
Probably, but they still have conventional meanings (which is why LLVM implemented one but not the other). common-page-size is supposed to be the page size you are most likely to encounter with your product [1]. Apparently, it’s supposed to be used for link-time optimisations, such as internal data alignment (e.g., DATA_SEGMENT_ALIGN [1]). Thus using max-page-size for both GCC and LLVM makes sense to me.
So claiming abuse because the provided value does not match the page
size of an OS that might also run on the same system is strenuous, and
I think our use of it is fine. AIUI, the reason for having
ClangBase.lds in addition to GccBase.lds is the fact that LLD does not
support common but only max page size, so I think it should be fine to
merge the two, and use max-page-size everywhere.
If that works, +1
Best regards,
Marvin