[PATCH] D24987: [ELF] Use MaxPageSize for aligning PT_LOAD

Rafael EspĂ­ndola via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 4 14:08:44 PDT 2016


>> If that is the case, it seems the correct fix is to just replace
>>
>> kernphys = CONSTANT (MAXPAGESIZE);
>>
>> with
>>
>> kernphys = 0x200000;
>>
>> or some other constant. You mentioned 640 kb, so maybe 0xa0000 would work too.
>
> Well, there are (at least) two reasons for this:
> 1) The need for below-640k memory
> 2) Aligning the kernel phys addr with a large page mapping
>
> I can certainly just set this to 0x200000 in the FreeBSD linker
> script, I just want to make sure we're diverging from ld.bfd for a
> good reason.

I think so. The full picture is that bfd has two page sizes,
COMMONPAGESIZE and MAXPAGESIZE. MAXPAGESIZE is used almost everywhere.
To avoid ending up with a 4 MB binary, the pages overlap on disk. For
example, with a trivial binary with just a .quad of data and text I
get

  LOAD           0x000000 0x0000000000000000 0x0000000000000000
0x0001e8 0x0001e8 R E 0x200000
  LOAD           0x000f50 0x0000000000200f50 0x0000000000200f50
0x0000b8 0x0000b8 RW  0x200000

So at runtime the will be two mappings, one at base and one at base +
0x200000, but they will point to offset 0 of the file.

The COMMONPAGESIZE is only used by DATA_SEGMENT_ALIGN. It is the
actual page size that is expected at runtime and the logic in
DATA_SEGMENT_ALIGN is to try to save a page in memory by wasting one
on disk.

The overlap (and the mixing of ro/rx data) has the disadvantage of
putting ro data in a executable page. We can implement that, but it is
not clear if we want it to be the default.

The current lld behaviour is to just not use COMMONPAGESIZE and set
MAXPAGESIZE to 4k. Other options include

* Using 2MB and living with 6MB binaries.
* Using COMMONPAGESIZE everywhere but linker scripts. That is what we
used to do, but it breaks compatibility with people using "-z
max-page-size=X" and expecting PT_LOAD to be aligned to X.
* Implement the page overlap logic so that we can use 2MB pages. If we
do this then we may as well merge ro and rx. Maybe do the overlap only
if not using --rosegment?

So if we can avoid changing this now and instead change the linker
script that would be awesome :-)

Cheers,
Rafael


More information about the llvm-commits mailing list