[01/13] ld: Define _edata, __bss_start, and _end only for executables

Message ID 20180601184357.10746-2-hjl.tools@gmail.com
State New
Headers show
Series
  • Define _edata, __bss_start, and _end only for executables
Related show

Commit Message

H.J. Lu June 1, 2018, 6:43 p.m.
_edata, __bss_start, and _end are defined for executables.  FreeBSD's
libc.so uses executable's _end to initialize curbrk.  But there is no
good reason to access values of _edata, __bss_start, and _end defined
in shared libraries.  We should define _edata, __bss_start, and _end
only for executables.

	PR ld/23161
	* scripttempl/elf.sc: Define _edata, edata, __bss_start, _end,
	end, ${EXECUTABLE_SYMBOLS}, ${OTHER_BSS_SYMBOLS},
	${OTHER_BSS_END_SYMBOLS} and ${OTHER_END_SYMBOLS} only for
	executables.
	* testsuite/ld-elf/pr23161.map: New file.
	* testsuite/ld-elf/pr23161.rd: Likewise.
	* testsuite/ld-elf/pr23161a.c: Likewise.
	* testsuite/ld-elf/pr23161b.c: Likewise.
	* testsuite/ld-elf/pr23162.rd: Remove  _edata, __bss_start, and
	_end.
	* testsuite/ld-elf/shared.exp: Run ld/23161 tests.
---
 ld/scripttempl/elf.sc           | 14 ++++++------
 ld/testsuite/ld-elf/pr23161.map |  4 ++++
 ld/testsuite/ld-elf/pr23161.rd  | 14 ++++++++++++
 ld/testsuite/ld-elf/pr23161a.c  |  9 ++++++++
 ld/testsuite/ld-elf/pr23161b.c  | 23 +++++++++++++++++++
 ld/testsuite/ld-elf/pr23162.rd  |  9 +-------
 ld/testsuite/ld-elf/shared.exp  | 40 +++++++++++++++++++++++++++++++++
 7 files changed, 98 insertions(+), 15 deletions(-)
 create mode 100644 ld/testsuite/ld-elf/pr23161.map
 create mode 100644 ld/testsuite/ld-elf/pr23161.rd
 create mode 100644 ld/testsuite/ld-elf/pr23161a.c
 create mode 100644 ld/testsuite/ld-elf/pr23161b.c

-- 
2.17.0

Patch

diff --git a/ld/scripttempl/elf.sc b/ld/scripttempl/elf.sc
index 5ccdd415f3..a9998f74c2 100644
--- a/ld/scripttempl/elf.sc
+++ b/ld/scripttempl/elf.sc
@@ -359,7 +359,7 @@  OUTPUT_ARCH(${OUTPUT_ARCH})
 ${RELOCATING+ENTRY(${ENTRY})}
 
 ${RELOCATING+${LIB_SEARCH_DIRS}}
-${RELOCATING+${EXECUTABLE_SYMBOLS}}
+${RELOCATING+${CREATE_SHLIB-${EXECUTABLE_SYMBOLS}}}
 ${RELOCATING+${INPUT_FILES}}
 ${RELOCATING- /* For some reason, the Solaris linker makes bad executables
   if gld -r is used and the intermediate file has sections starting
@@ -637,10 +637,10 @@  cat <<EOF
   ${SDATA_GOT+${OTHER_GOT_SECTIONS}}
   ${DATA_SDATA-${SDATA}}
   ${DATA_SDATA-${OTHER_SDATA_SECTIONS}}
-  ${RELOCATING+${DATA_END_SYMBOLS-${USER_LABEL_PREFIX}_edata = .; PROVIDE (${USER_LABEL_PREFIX}edata = .);}}
+  ${RELOCATING+${CREATE_SHLIB-${DATA_END_SYMBOLS-${USER_LABEL_PREFIX}_edata = .; PROVIDE (${USER_LABEL_PREFIX}edata = .);}}}
   ${RELOCATING+. = .;}
-  ${RELOCATING+${USER_LABEL_PREFIX}__bss_start = .;}
-  ${RELOCATING+${OTHER_BSS_SYMBOLS}}
+  ${RELOCATING+${CREATE_SHLIB-${USER_LABEL_PREFIX}__bss_start = .;}}
+  ${RELOCATING+${CREATE_SHLIB-${OTHER_BSS_SYMBOLS}}}
   ${DATA_SDATA-${SBSS}}
   ${BSS_PLT+${PLT}}
   .${BSS_NAME}          ${RELOCATING-0} :
@@ -657,7 +657,7 @@  cat <<EOF
   }
   ${OTHER_BSS_SECTIONS}
   ${LARGE_BSS_AFTER_BSS+${LARGE_BSS}}
-  ${RELOCATING+${OTHER_BSS_END_SYMBOLS}}
+  ${RELOCATING+${CREATE_SHLIB-${OTHER_BSS_END_SYMBOLS}}}
   ${RELOCATING+. = ALIGN(${ALIGNMENT});}
 EOF
 
@@ -671,8 +671,8 @@  cat <<EOF
   ${LARGE_SECTIONS}
   ${LARGE_BSS_AFTER_BSS-${LARGE_BSS}}
   ${RELOCATING+. = ALIGN(${ALIGNMENT});}
-  ${RELOCATING+${OTHER_END_SYMBOLS}}
-  ${RELOCATING+${END_SYMBOLS-${USER_LABEL_PREFIX}_end = .; PROVIDE (${USER_LABEL_PREFIX}end = .);}}
+  ${RELOCATING+${CREATE_SHLIB-${OTHER_END_SYMBOLS}}}
+  ${RELOCATING+${CREATE_SHLIB-${END_SYMBOLS-${USER_LABEL_PREFIX}_end = .; PROVIDE (${USER_LABEL_PREFIX}end = .);}}}
   ${RELOCATING+${DATA_SEGMENT_END}}
   ${TINY_DATA_SECTION}
   ${TINY_BSS_SECTION}
diff --git a/ld/testsuite/ld-elf/pr23161.map b/ld/testsuite/ld-elf/pr23161.map
new file mode 100644
index 0000000000..cc817dc52a
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr23161.map
@@ -0,0 +1,4 @@ 
+FOO {
+  global:
+    *;
+};
diff --git a/ld/testsuite/ld-elf/pr23161.rd b/ld/testsuite/ld-elf/pr23161.rd
new file mode 100644
index 0000000000..c8529a515f
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr23161.rd
@@ -0,0 +1,14 @@ 
+Relocation section '\.rel(a|)\.plt' at offset 0x[0-9a-f]+ contains 1 entry:
+ +Offset +Info +Type +Sym.* Value +Sym.* Name( \+ Addend|)
+[a-f0-9]+ +[0-9a-f]+ +R_.*_JUMP_SLOT +[a-f0-9]+ +foo(@FOO|)( \+ 0|)
+
+Symbol table '\.dynsym' contains [0-9]+ entries:
+ +Num: +Value +Size Type +Bind +Vis +Ndx Name
+ +0: 0+ +0 +NOTYPE +LOCAL +DEFAULT +UND +
+#...
+ +[0-9]+: +[a-f0-9]+ +0 +NOTYPE +GLOBAL +DEFAULT +[0-9]+ +_edata
+#...
+ +[0-9]+: +[a-f0-9]+ +0 +NOTYPE +GLOBAL +DEFAULT +[0-9]+ +_end
+#...
+ +[0-9]+: +[a-f0-9]+ +0 +NOTYPE +GLOBAL +DEFAULT +[0-9]+ +__bss_start
+#...
diff --git a/ld/testsuite/ld-elf/pr23161a.c b/ld/testsuite/ld-elf/pr23161a.c
new file mode 100644
index 0000000000..707ed04bfd
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr23161a.c
@@ -0,0 +1,9 @@ 
+extern char *_end;
+extern char *_edata;
+extern char *__bss_start;
+
+int
+foo (void)
+{
+  return _end[0] + _edata[0] + __bss_start[0];
+}
diff --git a/ld/testsuite/ld-elf/pr23161b.c b/ld/testsuite/ld-elf/pr23161b.c
new file mode 100644
index 0000000000..0112f771b1
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr23161b.c
@@ -0,0 +1,23 @@ 
+static char *_edata_p;
+static char *_end_p;
+static char *__bss_start_p;
+extern char *_end;
+extern char *_edata;
+extern char *__bss_start;
+
+extern int foo (void);
+
+void
+bar (void)
+{
+  _edata_p = (char*) &_edata;
+  _end_p = (char*) &_end;
+  __bss_start_p = (char*) &__bss_start;
+}
+
+void
+_start ()
+{
+  bar ();
+  foo ();
+}
diff --git a/ld/testsuite/ld-elf/pr23162.rd b/ld/testsuite/ld-elf/pr23162.rd
index 3ac848fe05..48351ffea7 100644
--- a/ld/testsuite/ld-elf/pr23162.rd
+++ b/ld/testsuite/ld-elf/pr23162.rd
@@ -1,12 +1,5 @@ 
 There are no relocations in this file\.
 
-Symbol table '\.dynsym' contains [0-9]+ entries:
+Symbol table '\.dynsym' contains 1 entry:
  +Num: +Value +Size Type +Bind +Vis +Ndx Name
  +0: 0+ +0 +NOTYPE +LOCAL +DEFAULT +UND +
-#...
- +[0-9]+: +[a-f0-9]+ +0 +NOTYPE +GLOBAL +DEFAULT +[0-9]+ +_edata
-#...
- +[0-9]+: +[a-f0-9]+ +0 +NOTYPE +GLOBAL +DEFAULT +[0-9]+ +_end
-#...
- +[0-9]+: +[a-f0-9]+ +0 +NOTYPE +GLOBAL +DEFAULT +[0-9]+ +__bss_start
-#...
diff --git a/ld/testsuite/ld-elf/shared.exp b/ld/testsuite/ld-elf/shared.exp
index ef517bac42..9403363860 100644
--- a/ld/testsuite/ld-elf/shared.exp
+++ b/ld/testsuite/ld-elf/shared.exp
@@ -1393,5 +1393,45 @@  if { ([istarget "i?86-*-*"]
 	    "pr23162b" \
 	    "-fPIC -O0" \
 	] \
+	[list \
+	    "Build libpr23161a.so" \
+	    "-shared" \
+	    "" \
+	    "$AFLAGS_PIC" \
+	    { pr23161a.c } \
+	    "" \
+	    "libpr23161a.so" \
+	    "-fPIC -O2" \
+	] \
+	[list \
+	    "Build pr23161a" \
+	    "-pie --no-as-needed tmpdir/libpr23161a.so" \
+	    "" \
+	    "-mrelax-relocations=yes" \
+	    { pr23161b.c } \
+	    {{readelf {--dyn-syms -rW} pr23161.rd}} \
+	    "pr23161a" \
+	    "-fPIC -O0" \
+	] \
+	[list \
+	    "Build libpr23161b.so" \
+	    "-shared --version-script=pr23161.map" \
+	    "" \
+	    "$AFLAGS_PIC" \
+	    { pr23161a.c } \
+	    "" \
+	    "libpr23161b.so" \
+	    "-fPIC -O2" \
+	] \
+	[list \
+	    "Build pr23161b" \
+	    "-pie --no-as-needed tmpdir/libpr23161b.so" \
+	    "" \
+	    "-mrelax-relocations=yes" \
+	    { pr23161b.c } \
+	    {{readelf {--dyn-syms -rW} pr23161.rd}} \
+	    "pr23161b" \
+	    "-fPIC -O0" \
+	] \
     ]
 }