ld: optionally emit _etext last, before .data

Message ID alpine.BSF.2.20.16.2007172055060.68146@arjuna.pair.com
State New
Headers show
Series
  • ld: optionally emit _etext last, before .data
Related show

Commit Message

Hans-Peter Nilsson July 18, 2020, 12:55 a.m.
So, here's my suggestion for making _init .. __etext cover .text +
.rodata (including things like the read-only exception tables) for
elf64mmix.  A quick web search gives that __etext (and friends) isn't
well defined, so each target can interpret the "end of text segment"
to their own liking.  It seems likely this change is also a better fit
than the default for other ports, at least those with .rodata after
.text in the same segment.

The presence of a separate rodata-segment is optional (and not true
for elf64mmix).  This is reflected in the name as SEPARATE_TEXT /
SEPARATE_CODE isn't considered, to keep it simple; each target has to
make sure their settings of variables make sense.

Ok to commit?

ld:
	* scripttempl/elf.sc (ETEXT_LAST_IN_RODATA_SEGMENT): New variable.
	* emulparams/elf64mmix.sh (ETEXT_LAST_IN_RODATA_SEGMENT): Define.
	* testsuite/ld-mmix/sec-1.d: Adjust.


brgds, H-P

Comments

Hans-Peter Nilsson July 18, 2020, 1:13 a.m. | #1
On Fri, 17 Jul 2020, Hans-Peter Nilsson wrote:

> So, here's my suggestion for making _init .. __etext cover .text +

> .rodata (including things like the read-only exception tables) for

> elf64mmix.


I forgot to mention that I re-built cris-elf and diff'd the
ldscripts to smoke test that this patch has absolutely no effect
there or for any other target not setting
ETEXT_LAST_IN_RODATA_SEGMENT and of course I redid the
"original" gcc test (building for elf, objcopying to mmo,
before/after patch) to check that it had the intended effect for
elf64mmix.  If the test-suite adjustment wasn't convincing...

>

> ld:

> 	* scripttempl/elf.sc (ETEXT_LAST_IN_RODATA_SEGMENT): New variable.

> 	* emulparams/elf64mmix.sh (ETEXT_LAST_IN_RODATA_SEGMENT): Define.

> 	* testsuite/ld-mmix/sec-1.d: Adjust.


brgds, H-P
Fangrui Song July 19, 2020, 12:16 a.m. | #2
On 2020-07-17, Hans-Peter Nilsson wrote:
>On Fri, 17 Jul 2020, Hans-Peter Nilsson wrote:

>

>> So, here's my suggestion for making _init .. __etext cover .text +

>> .rodata (including things like the read-only exception tables) for

>> elf64mmix.

>

>I forgot to mention that I re-built cris-elf and diff'd the

>ldscripts to smoke test that this patch has absolutely no effect

>there or for any other target not setting

>ETEXT_LAST_IN_RODATA_SEGMENT and of course I redid the

>"original" gcc test (building for elf, objcopying to mmo,

>before/after patch) to check that it had the intended effect for

>elf64mmix.  If the test-suite adjustment wasn't convincing...

>

>>

>> ld:

>> 	* scripttempl/elf.sc (ETEXT_LAST_IN_RODATA_SEGMENT): New variable.

>> 	* emulparams/elf64mmix.sh (ETEXT_LAST_IN_RODATA_SEGMENT): Define.

>> 	* testsuite/ld-mmix/sec-1.d: Adjust.

>

>brgds, H-P


I am in favor of this choice.

An effective way mitigating
https://sourceware.org/bugzilla/show_bug.cgi?id=24490
and
https://sourceware.org/bugzilla/show_bug.cgi?id=24491
is to move read-only sections (.rodata .eh_frame .eh_frame_hdr)
before text sections.

LLD has such a default layout and thus its _etext is after both .rodata and .text
Very few applications need to be fixed (see https://github.com/mruby/mruby/pull/4716 for an example)
H.J. Lu via Binutils July 19, 2020, 3:47 a.m. | #3
On Fri, Jul 17, 2020 at 08:55:56PM -0400, Hans-Peter Nilsson wrote:
> 	* scripttempl/elf.sc (ETEXT_LAST_IN_RODATA_SEGMENT): New variable.

> 	* emulparams/elf64mmix.sh (ETEXT_LAST_IN_RODATA_SEGMENT): Define.

> 	* testsuite/ld-mmix/sec-1.d: Adjust.


OK.

-- 
Alan Modra
Australia Development Lab, IBM

Patch

diff --git a/ld/emulparams/elf64mmix.sh b/ld/emulparams/elf64mmix.sh
index dffc6cc..75e24be 100644
--- a/ld/emulparams/elf64mmix.sh
+++ b/ld/emulparams/elf64mmix.sh
@@ -44,6 +44,10 @@  ${RELOCATING+
  PROVIDE (Main = DEFINED (Main) ? Main : (DEFINED (_start) ? _start : _start.));
 }"

+# We need a symbol at the end of the read-only data, which is
+# colocated with the code.  We make __etext fit.
+ETEXT_LAST_IN_RODATA_SEGMENT=" "
+
 OTHER_SECTIONS='
  .MMIX.reg_contents :
  {
diff --git a/ld/scripttempl/elf.sc b/ld/scripttempl/elf.sc
index 0b8b32a..eb74743 100644
--- a/ld/scripttempl/elf.sc
+++ b/ld/scripttempl/elf.sc
@@ -62,6 +62,9 @@ 
 #	OTHER_SYMBOLS - symbols to place right at the end of the script.
 #	ETEXT_NAME - name of a symbol for the end of the text section,
 #		normally etext.
+#	ETEXT_LAST_IN_RODATA_SEGMENT - emit ETEXT_NAME after all sections in
+#		the read-only data segment (which may or may not be equal to
+#		the code segment), instead of after just the code parts.
 #	SEPARATE_CODE - if set, .text and similar sections containing
 #		actual machine instructions must be in wholly disjoint
 #		pages from any other data, including headers
@@ -526,9 +529,9 @@  cat <<EOF
     KEEP (*(SORT_NONE(.fini)))
     ${RELOCATING+${FINI_END}}
   } ${FILL}
-  ${RELOCATING+PROVIDE (__${ETEXT_NAME} = .);}
-  ${RELOCATING+PROVIDE (_${ETEXT_NAME} = .);}
-  ${RELOCATING+PROVIDE (${ETEXT_NAME} = .);}
+  ${RELOCATING+${ETEXT_LAST_IN_RODATA_SEGMENT-PROVIDE (__${ETEXT_NAME} = .);}}
+  ${RELOCATING+${ETEXT_LAST_IN_RODATA_SEGMENT-PROVIDE (_${ETEXT_NAME} = .);}}
+  ${RELOCATING+${ETEXT_LAST_IN_RODATA_SEGMENT-PROVIDE (${ETEXT_NAME} = .);}}
   ${RELOCATING+${TEXT_SEGMENT_ALIGN}}
 EOF

@@ -574,6 +577,10 @@  cat <<EOF
   .exception_ranges ${RELOCATING-0} : ONLY_IF_RO { *(.exception_ranges${RELOCATING+*}) }
   ${TEXT_PLT+${PLT_NEXT_DATA+${PLT} ${OTHER_PLT_SECTIONS}}}

+  ${RELOCATING+${ETEXT_LAST_IN_RODATA_SEGMENT+PROVIDE (__${ETEXT_NAME} = .);}}
+  ${RELOCATING+${ETEXT_LAST_IN_RODATA_SEGMENT+PROVIDE (_${ETEXT_NAME} = .);}}
+  ${RELOCATING+${ETEXT_LAST_IN_RODATA_SEGMENT+PROVIDE (${ETEXT_NAME} = .);}}
+
   ${RELOCATING+/* Adjust the address for the data segment.  We want to adjust up to
      the same address within the page on the next page up.  */}
   ${CREATE_SHLIB-${CREATE_PIE-${RELOCATING+. = ${DATA_ADDR-${DATA_SEGMENT_ALIGN}};}}}
diff --git a/ld/testsuite/ld-mmix/sec-1.d b/ld/testsuite/ld-mmix/sec-1.d
index 5e31b3f..6f82c8e 100644
--- a/ld/testsuite/ld-mmix/sec-1.d
+++ b/ld/testsuite/ld-mmix/sec-1.d
@@ -24,7 +24,7 @@  Idx Name          Size      VMA               LMA               File off  Algn

 SYMBOL TABLE:
 #...
-0+1d g +\*ABS\* _etext
+0+30 g +\*ABS\* _etext
 #...
 2000000000000013 g +\*ABS\* __bss_start
 2000000000000013 g +\*ABS\* _edata