[PATCH 0/6] ld: Add "-z textonly" option to ELF linker

H.J. Lu hjl.tools@gmail.com
Tue Nov 21 22:28:00 GMT 2017


Text-only LOAD segment has the same requirement for segment alignment
and page sizes as GNU_RELRO segment.  But for GNU_RELRO segment, the
segment may not end at the same address of the end of data segment.  But
for text-only LOAD segment, it is exactly the same as text LOAD segment.

The new "-z textonly" option will turn on both text-only LOAD segment and
GNU_RELRO segment.  The new "-z notextonly" option will turn off only
text-only LOAD segment.  "-z relro" is updated not to turn off text-only
LOAD segment.  "-z norelro" is updated to turn off both GNU_RELRO segment
and text-only LOAD segment.

When there is a text-only 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 0x200ff0
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           0x000fd0 0x00200fd0 0x00200fd0 0x0002b 0x0002b R E 0x200000
  LOAD           0x001000 0x00201000 0x00201000 0x00058 0x00058 R   0x200000
  LOAD           0x200f80 0x00400f80 0x00400f80 0x000a0 0x000a0 RW  0x200000
  DYNAMIC        0x200f80 0x00400f80 0x00400f80 0x00080 0x00080 RW  0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x10
  GNU_RELRO      0x200f80 0x00400f80 0x00400f80 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

This is to prevent executing data in read-only sections as instructions.

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

Since there are more than 2 LOAD segments, the minimum file size is
bigger than the maximum page size which is 2MB (0x200000):

-rwxr-xr-x 1 hjl hjl 2104892 Nov 12 11:53 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 11836 Nov 12 13:22 libfoo.so

Elf file type is DYN (Shared object file)
Entry point 0x1ff0
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   0x1000
  LOAD           0x000fd0 0x00001fd0 0x00001fd0 0x0002b 0x0002b R E 0x1000
  LOAD           0x001000 0x00002000 0x00002000 0x00058 0x00058 R   0x1000
  LOAD           0x001f80 0x00002f80 0x00002f80 0x000a0 0x000a0 RW  0x1000
  DYNAMIC        0x001f80 0x00002f80 0x00002f80 0x00080 0x00080 RW  0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x10
  GNU_RELRO      0x001f80 0x00002f80 0x00002f80 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

This is the set of updated patches.  Tested on x86-64, x32 and i686.
Any comments or suggestions?

H.J. Lu (6):
  ld: Add "-z textonly" option to ELF linker
  ld: Add TEXT_SEGMENT_ALIGN/TEXT_SEGMENT_{RELRO_}END
  ld: Create a new LOAD segment for text-only LOAD segment
  ld/ppc/spu: Also set expld.textseg.phase to exp_seg_none
  ld: Add tests for -z textonly and -z notextonly
  x86-64: Add tests for -z textonly -z max-page-size=0x1000

 bfd/elf.c                            |  32 +++++++---
 include/bfdlink.h                    |   9 ++-
 ld/NEWS                              |   2 +
 ld/emultempl/elf32.em                | 104 +++++++++++++++++++++++++++++----
 ld/emultempl/ppc32elf.em             |   1 +
 ld/emultempl/ppc64elf.em             |   1 +
 ld/emultempl/spuelf.em               |   1 +
 ld/genscripts.sh                     |  76 +++++++++++++++++++++---
 ld/ld.texinfo                        |  10 +++-
 ld/ldexp.c                           |  18 +++++-
 ld/ldexp.h                           |   3 +
 ld/ldgram.y                          |   7 +++
 ld/ldlang.c                          | 109 ++++++++++++++++++++++++++---------
 ld/ldlex.l                           |   3 +
 ld/lexsup.c                          |   8 ++-
 ld/scripttempl/elf.sc                |  21 +++++++
 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 +++++++++++++++++++++++
 34 files changed, 627 insertions(+), 61 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