hanchenye-llvm-project/lld/ELF
Fangrui Song 5391f158c2 [ELF] Add -z separate-code and pad the last page of last PF_X PT_LOAD with traps only if -z separate-code is specified
This patch

1) adds -z separate-code and -z noseparate-code (default).
2) changes the condition that the last page of last PF_X PT_LOAD is
 padded with trap instructions.
 Current condition (after D33630): if there is no `SECTIONS` commands.
 After this change: if -z separate-code is specified.

-z separate-code was introduced to ld.bfd in 2018, to place the text
segment in its own pages. There is no overlap in pages between an
executable segment and a non-executable segment:

1) RX cannot load initial contents from R or RW(or non-SHF_ALLOC).
2) R and RW(or non-SHF_ALLOC) cannot load initial contents from RX.

lld's current status:

- Between R and RX: in `Writer<ELFT>::fixSectionAlignments()`, the start of a
  segment is always aligned to maxPageSize, so the initial contents loaded by R
  and RX do not overlap. I plan to allow overlaps in D64906 if -z noseparate-code
  is in effect.
- Between RX and RW(or non-SHF_ALLOC if RW doesn't exist):
  we currently unconditionally pad the last page to commonPageSize
  (defaults to 4096 on all targets we support).
  This patch will make it effective only if -z separate-code is specified.

-z separate-code is a dubious feature that intends to reduce the number
of ROP gadgets (which is actually ineffective because attackers can find
plenty of gadgets in the text segment, no need to find gadgets in
non-code regions).

With the overlapping PT_LOAD technique D64906, -z noseparate-code
removes two more alignments at segment boundaries than -z separate-code.
This saves at most defaultCommonPageSize*2 bytes, which are significant
on targets with large defaultCommonPageSize (AArch64/MIPS/PPC: 65536).

Issues/feedback on alignment at segment boundaries to help understand
the implication:

* binutils PR24490 (the situation on ld.bfd is worse because they have
  two R-- on both sides of R-E so more alignments.)

* In binutils, the 2018-02-27 commit "ld: Add --enable-separate-code" made -z separate-code the default on Linux.
  d969dea983
  In musl-cross-make, binutils is configured with --disable-separate-code
  to address size regressions caused by -z separate-code. (lld actually has the same
  issue, which I plan to fix in a future patch. The ld.bfd x86 status is
  worse because they default to max-page-size=0x200000).

* https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=237676 people want
  smaller code size. This patch will remove one alignment boundary.

* Stef O'Rear: I'm opposed to any kind of page alignment at the
  text/rodata line (having a partial page of text aliased as rodata and
  vice versa has no demonstrable harm, and I actually care about small
  systems).

So, make -z noseparate-code the default.

Reviewed By: ruiu

Differential Revision: https://reviews.llvm.org/D64903

llvm-svn: 367537
2019-08-01 09:58:25 +00:00
..
Arch [mips] Assign R_TLS type to the R_MIPS_TLS_TPREL_XXX relocations. NFC 2019-07-24 11:37:13 +00:00
AArch64ErrataFix.cpp [ELF] Fix variable names in comments after VariableName -> variableName change 2019-07-16 05:50:45 +00:00
AArch64ErrataFix.h [ELF] Fix variable names in comments after VariableName -> variableName change 2019-07-16 05:50:45 +00:00
CMakeLists.txt Fail early if an output file is not writable 2019-03-11 16:30:55 +00:00
CallGraphSort.cpp [ELF] Fix variable names in comments after VariableName -> variableName change 2019-07-16 05:50:45 +00:00
CallGraphSort.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
Config.h [ELF] Add -z separate-code and pad the last page of last PF_X PT_LOAD with traps only if -z separate-code is specified 2019-08-01 09:58:25 +00:00
DWARF.cpp [DWARF][RISCV] Add support for RISC-V relocations needed for debug info 2019-07-18 05:22:55 +00:00
DWARF.h [Coding style change] Rename variables so that they start with a lowercase letter 2019-07-10 05:00:37 +00:00
Driver.cpp [ELF] Add -z separate-code and pad the last page of last PF_X PT_LOAD with traps only if -z separate-code is specified 2019-08-01 09:58:25 +00:00
Driver.h [Coding style change] Rename variables so that they start with a lowercase letter 2019-07-10 05:00:37 +00:00
DriverUtils.cpp [Coding style change][lld] Rename variables for non-ELF ports 2019-07-11 05:40:30 +00:00
EhFrame.cpp [Coding style change] Rename variables so that they start with a lowercase letter 2019-07-10 05:00:37 +00:00
EhFrame.h [Coding style change] Rename variables so that they start with a lowercase letter 2019-07-10 05:00:37 +00:00
ICF.cpp [Coding style change][lld] Rename variables for non-ELF ports 2019-07-11 05:40:30 +00:00
ICF.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
InputFiles.cpp Return early. NFC. 2019-07-29 05:24:51 +00:00
InputFiles.h [ELF] Fix variable names in comments after VariableName -> variableName change 2019-07-16 05:50:45 +00:00
InputSection.cpp [mips] Assign R_TLS type to the R_MIPS_TLS_TPREL_XXX relocations. NFC 2019-07-24 11:37:13 +00:00
InputSection.h [ELF] Fix variable names in comments after VariableName -> variableName change 2019-07-16 05:50:45 +00:00
LTO.cpp [ELF] Support explicitly overriding relocation model in LTO 2019-07-20 21:59:47 +00:00
LTO.h [Coding style change] Rename variables so that they start with a lowercase letter 2019-07-10 05:00:37 +00:00
LinkerScript.cpp [ELF] Fix variable names in comments after VariableName -> variableName change 2019-07-16 05:50:45 +00:00
LinkerScript.h [Coding style change] Rename variables so that they start with a lowercase letter 2019-07-10 05:00:37 +00:00
MapFile.cpp [Coding style change] Rename variables so that they start with a lowercase letter 2019-07-10 05:00:37 +00:00
MapFile.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
MarkLive.cpp [Coding style change][lld] Rename variables for non-ELF ports 2019-07-11 05:40:30 +00:00
MarkLive.h Update the file headers across all of the LLVM projects in the monorepo 2019-01-19 08:50:56 +00:00
Options.td [lld] Add Visual Studio compatible diagnostics 2019-07-17 14:54:02 +00:00
OutputSections.cpp [Coding style change] Rename variables so that they start with a lowercase letter 2019-07-10 05:00:37 +00:00
OutputSections.h [Coding style change] Rename variables so that they start with a lowercase letter 2019-07-10 05:00:37 +00:00
README.md
Relocations.cpp [ELF] Detemplate maybeReportUndefined and copySectionsIntoPartitions 2019-07-26 14:57:53 +00:00
Relocations.h [Coding style change] Rename variables so that they start with a lowercase letter 2019-07-10 05:00:37 +00:00
ScriptLexer.cpp [Coding style change] Rename variables so that they start with a lowercase letter 2019-07-10 05:00:37 +00:00
ScriptLexer.h [Coding style change] Rename variables so that they start with a lowercase letter 2019-07-10 05:00:37 +00:00
ScriptParser.cpp Fix parameter name comments using clang-tidy. NFC. 2019-07-16 04:46:31 +00:00
ScriptParser.h [ELF] Fix variable names in comments after VariableName -> variableName change 2019-07-16 05:50:45 +00:00
SymbolTable.cpp [ELF] Fix variable names in comments after VariableName -> variableName change 2019-07-16 05:50:45 +00:00
SymbolTable.h [ELF] Handle non-glob patterns before glob patterns in version scripts & fix a corner case of --dynamic-list 2019-07-11 11:16:51 +00:00
Symbols.cpp [ELF] Simplify with dyn_cast_or_null. NFC 2019-07-26 16:29:15 +00:00
Symbols.h ld.lld: Demangle symbols from archives in diagnostics 2019-07-23 19:00:01 +00:00
SyntheticSections.cpp [ELF] Fix variable names in comments after VariableName -> variableName change 2019-07-16 05:50:45 +00:00
SyntheticSections.h [ELF] Fix variable names in comments after VariableName -> variableName change 2019-07-16 05:50:45 +00:00
Target.cpp [Coding style change] Rename variables so that they start with a lowercase letter 2019-07-10 05:00:37 +00:00
Target.h [ELF] Fix variable names in comments after VariableName -> variableName change 2019-07-16 05:50:45 +00:00
Thunks.cpp [ELF] Fix variable names in comments after VariableName -> variableName change 2019-07-16 05:50:45 +00:00
Thunks.h [Coding style change] Rename variables so that they start with a lowercase letter 2019-07-10 05:00:37 +00:00
Writer.cpp [ELF] Add -z separate-code and pad the last page of last PF_X PT_LOAD with traps only if -z separate-code is specified 2019-08-01 09:58:25 +00:00
Writer.h [Coding style change] Rename variables so that they start with a lowercase letter 2019-07-10 05:00:37 +00:00

README.md

See docs/NewLLD.rst