[PATCH 0/4] ld: Add "-z separate-code" option to ELF linker

H.J. Lu hjl.tools@gmail.com
Wed Jan 10 18:52:00 GMT 2018


The new "-z separate-code" option will generate separate code LOAD
segment which must be in wholly disjoint pages from any other data.

When generating separate code LOAD segment, create a new LOAD segment
if the previous section contains text and the current section doesn't
or vice versa:

Elf file type is DYN (Shared object file)
Entry point 0x200020
There are 7 program headers, starting at offset 52

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000000 0x00000000 0x00000000 0x00200 0x00200 R   0x200000
  LOAD           0x200000 0x00200000 0x00200000 0x00036 0x00036 R E 0x200000
  LOAD           0x400000 0x00400000 0x00400000 0x00064 0x00064 R   0x200000
  LOAD           0x400f80 0x00600f80 0x00600f80 0x000a0 0x000a0 RW  0x200000
  DYNAMIC        0x400f80 0x00600f80 0x00600f80 0x00080 0x00080 RW  0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x10
  GNU_RELRO      0x400f80 0x00600f80 0x00600f80 0x00080 0x00080 R   0x1

 Section to Segment mapping:
  Segment Sections...
   00     .hash .gnu.hash .dynsym .dynstr .rela.plt
   01     .plt .text
   02     .rodata .eh_frame
   03     .dynamic .got.plt
   04     .dynamic
   05
   06     .dynamic

to prevent fetching or executing data in code pages as instructions.

Also don't put a writable section in a read-only segment if there is a
RELRO segment.

Since code segment is aligned and padded to the maximum page size on
disk, the minimum file size is bigger than the maximum page size which
is 2MB (0x200000):

-rwxr-xr-x 1 hjl hjl 4201932 Jan 10 10:41 libfoo.so

"-z max-page-size=0x1000" can be used to reduce the maximum page size to
4KB (0x1000):

-rwxr-xr-x 1 hjl hjl   15820 Jan 10 10:44 libfoo.so

H.J. Lu (4):
  ld: Add "-z separate-code" option to ELF linker
  ld: Create a new LOAD segment for separate code segment
  ld: Add tests for -z separate-code and -z noseparate-code
  x86-64: Add tests for -z separate-code -z max-page-size=0x1000

 bfd/elf.c                            |  32 ++++++++---
 include/bfdlink.h                    |   3 ++
 ld/NEWS                              |   2 +
 ld/emultempl/elf32.em                | 100 +++++++++++++++++++++++++++++++----
 ld/genscripts.sh                     |  76 +++++++++++++++++++++++---
 ld/ld.texinfo                        |   7 +++
 ld/lexsup.c                          |   4 ++
 ld/scripttempl/elf.sc                |  22 ++++++--
 ld/testsuite/ld-elf/pr16322.d        |   2 +-
 ld/testsuite/ld-elf/pr22393-1.s      |  21 ++++++++
 ld/testsuite/ld-elf/pr22393-1a.d     |   9 ++++
 ld/testsuite/ld-elf/pr22393-1b.d     |   9 ++++
 ld/testsuite/ld-elf/pr22393-1c.d     |   9 ++++
 ld/testsuite/ld-elf/pr22393-1d.d     |   9 ++++
 ld/testsuite/ld-elf/pr22393-1e.d     |   9 ++++
 ld/testsuite/ld-elf/pr22393-1f.d     |   9 ++++
 ld/testsuite/ld-elf/pr22393-2a.c     |   7 +++
 ld/testsuite/ld-elf/pr22393-2a.rd    |   9 ++++
 ld/testsuite/ld-elf/pr22393-2b.c     |   7 +++
 ld/testsuite/ld-elf/pr22393-2b.rd    |   9 ++++
 ld/testsuite/ld-elf/shared.exp       |  71 +++++++++++++++++++++++++
 ld/testsuite/ld-x86-64/pr22393-3a.c  |   7 +++
 ld/testsuite/ld-x86-64/pr22393-3a.rd |   9 ++++
 ld/testsuite/ld-x86-64/pr22393-3b.c  |   7 +++
 ld/testsuite/ld-x86-64/pr22393-3b.rd |   9 ++++
 ld/testsuite/ld-x86-64/x86-64.exp    |  71 +++++++++++++++++++++++++
 26 files changed, 501 insertions(+), 28 deletions(-)
 create mode 100644 ld/testsuite/ld-elf/pr22393-1.s
 create mode 100644 ld/testsuite/ld-elf/pr22393-1a.d
 create mode 100644 ld/testsuite/ld-elf/pr22393-1b.d
 create mode 100644 ld/testsuite/ld-elf/pr22393-1c.d
 create mode 100644 ld/testsuite/ld-elf/pr22393-1d.d
 create mode 100644 ld/testsuite/ld-elf/pr22393-1e.d
 create mode 100644 ld/testsuite/ld-elf/pr22393-1f.d
 create mode 100644 ld/testsuite/ld-elf/pr22393-2a.c
 create mode 100644 ld/testsuite/ld-elf/pr22393-2a.rd
 create mode 100644 ld/testsuite/ld-elf/pr22393-2b.c
 create mode 100644 ld/testsuite/ld-elf/pr22393-2b.rd
 create mode 100644 ld/testsuite/ld-x86-64/pr22393-3a.c
 create mode 100644 ld/testsuite/ld-x86-64/pr22393-3a.rd
 create mode 100644 ld/testsuite/ld-x86-64/pr22393-3b.c
 create mode 100644 ld/testsuite/ld-x86-64/pr22393-3b.rd

-- 
2.14.3



More information about the Binutils mailing list