[PATCH] D61201: [LLD][ELF] Full support for -n (--nmagic) and -N (--omagic)

Peter Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 26 10:25:15 PDT 2019


peter.smith created this revision.
peter.smith added reviewers: ruiu, grimar, MaskRay.
Herald added subscribers: kristof.beyls, arichardson, javed.absar.
Herald added a reviewer: espindola.

The -n (--nmagic) and -N (--omagic) options are used in non-paged environments such as embedded systems. They have the following documented
behavior:
--nmagic disables page alignment of OutputSections and disables linking against dynamic libraries.
--omagic is the same as --nmagic but additionally makes the text and data sections to be readable and writeable.
In addition to the documented behavior:

- We don't generate a PT_RELO as there are no pages.
- Unless explicitly instructed to by PHDRS we do not allocate the headers in the first program segment.
- The linkerscript expressions that refer to the max and common page size are unaffected.

fixes pr40542 https://bugs.llvm.org/show_bug.cgi?id=40542

Our previous implementation of -N in D26888 <https://reviews.llvm.org/D26888> just made the text and data sections to be readable and writeable as this was sufficient for the use-case needed at the time. This change builds upon that to add the remaining behavior from GNU ld.bfd.

Implementation Notes:
I originally thought that this would be as simple as -n = --max-page-size=1, unfortunately we have both MaxPageSize which is configurable and Target->PageSize (often known as common-page-size) that isn't. It could be possible to make Target->PageSize configurable via implementing -z common-page-size, however while looking into GNU ld, it seems like the linkerscript expressions like COMMONPAGESIZE and MAXPAGESIZE are not affected by --nmagic or --omagic.

It is worth mentioning that it is not clear that LLD -n will give sensible results with the builtin layout which assumes paging. In practice using -n will require a linker script. I've tested against the aarch64 linux vdso which uses -n with its own linker script and it does give the required result.

Considered alternatives:

- Implement -z common-page-size (handle Target->PageSize in the way that we handle Config->MaxPageSize) and set both common and max page sizes to 1. This leaves the page calculations untouched but would also affect COMMONPAGESIZE and MAXPAGESIZE and other linker script expressions. This could break some scripts.
- Make specific versions of alignTo and alignDown for page alignment rather than calling them directly and then test for Config->Paged. I rejected this as we would need at least 4 functions to handle the combination of (To, Down) and (PageSize, MaxPageSize).


https://reviews.llvm.org/D61201

Files:
  ELF/Config.h
  ELF/Driver.cpp
  ELF/LinkerScript.cpp
  ELF/Options.td
  ELF/Writer.cpp
  test/ELF/aarch64-script-nmagic.s
  test/ELF/relro-omagic.s
  test/ELF/segments.s

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61201.196876.patch
Type: text/x-patch
Size: 12559 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190426/2906bae1/attachment.bin>


More information about the llvm-commits mailing list