This is the mail archive of the
binutils@sourceware.org
mailing list for the binutils project.
[PATCH 1/2] ld/x86: Define _edata, __bss_start, and _end only for executables
- From: "H.J. Lu" <hjl dot tools at gmail dot com>
- To: binutils at sourceware dot org
- Date: Tue, 5 Jun 2018 05:48:06 -0700
- Subject: [PATCH 1/2] ld/x86: Define _edata, __bss_start, and _end only for executables
- References: <20180605124807.22990-1-hjl.tools@gmail.com>
_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. To exclude _edata, __bss_start, _end and end
from shared libraries, EXCLUDE_SHLIB_SYMBOLS should be set.
PR ld/23161
* emulparams/elf32_x86_64.sh (EXCLUDE_SHLIB_SYMBOLS): New.
* emulparams/elf_i386.sh (EXCLUDE_SHLIB_SYMBOLS): Likewise.
* emulparams/elf_iamcu.sh (EXCLUDE_SHLIB_SYMBOLS): Likewise.
* emulparams/elf_k1om.sh (EXCLUDE_SHLIB_SYMBOLS): Likewise.
* emulparams/elf_l1om.sh (EXCLUDE_SHLIB_SYMBOLS): Likewise.
* emulparams/elf_x86_64.sh (EXCLUDE_SHLIB_SYMBOLS): Likewise.
* scripttempl/elf.sc: Define _edata, edata, __bss_start, _end
and end only for executables if EXCLUDE_SHLIB_SYMBOLS is set.
* 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/emulparams/elf32_x86_64.sh | 1 +
ld/emulparams/elf_i386.sh | 1 +
ld/emulparams/elf_iamcu.sh | 1 +
ld/emulparams/elf_k1om.sh | 1 +
ld/emulparams/elf_l1om.sh | 1 +
ld/emulparams/elf_x86_64.sh | 1 +
ld/scripttempl/elf.sc | 12 +++++++---
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 +++++++++++++++++++++++++++++++++
13 files changed, 106 insertions(+), 11 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
diff --git a/ld/emulparams/elf32_x86_64.sh b/ld/emulparams/elf32_x86_64.sh
index 9ce6533bd9..bb34a2a223 100644
--- a/ld/emulparams/elf32_x86_64.sh
+++ b/ld/emulparams/elf32_x86_64.sh
@@ -16,6 +16,7 @@ MACHINE=
TEMPLATE_NAME=elf32
GENERATE_SHLIB_SCRIPT=yes
GENERATE_PIE_SCRIPT=yes
+EXCLUDE_SHLIB_SYMBOLS=yes
NO_SMALL_DATA=yes
LARGE_SECTIONS=yes
LARGE_BSS_AFTER_BSS=
diff --git a/ld/emulparams/elf_i386.sh b/ld/emulparams/elf_i386.sh
index 2cef106e3d..51bb6f9f31 100644
--- a/ld/emulparams/elf_i386.sh
+++ b/ld/emulparams/elf_i386.sh
@@ -14,6 +14,7 @@ MACHINE=
TEMPLATE_NAME=elf32
GENERATE_SHLIB_SCRIPT=yes
GENERATE_PIE_SCRIPT=yes
+EXCLUDE_SHLIB_SYMBOLS=yes
NO_SMALL_DATA=yes
SEPARATE_GOTPLT="SIZEOF (.got.plt) >= 12 ? 12 : 0"
IREL_IN_PLT=
diff --git a/ld/emulparams/elf_iamcu.sh b/ld/emulparams/elf_iamcu.sh
index d910b653c3..bf99476369 100644
--- a/ld/emulparams/elf_iamcu.sh
+++ b/ld/emulparams/elf_iamcu.sh
@@ -14,6 +14,7 @@ COMPILE_IN=yes
TEMPLATE_NAME=elf32
GENERATE_SHLIB_SCRIPT=yes
GENERATE_PIE_SCRIPT=yes
+EXCLUDE_SHLIB_SYMBOLS=yes
NO_SMALL_DATA=yes
SEPARATE_GOTPLT="SIZEOF (.got.plt) >= 12 ? 12 : 0"
IREL_IN_PLT=
diff --git a/ld/emulparams/elf_k1om.sh b/ld/emulparams/elf_k1om.sh
index 494efcc6c9..c2dd370fb4 100644
--- a/ld/emulparams/elf_k1om.sh
+++ b/ld/emulparams/elf_k1om.sh
@@ -15,6 +15,7 @@ COMPILE_IN=yes
TEMPLATE_NAME=elf32
GENERATE_SHLIB_SCRIPT=yes
GENERATE_PIE_SCRIPT=yes
+EXCLUDE_SHLIB_SYMBOLS=yes
NO_SMALL_DATA=yes
LARGE_SECTIONS=yes
LARGE_BSS_AFTER_BSS=
diff --git a/ld/emulparams/elf_l1om.sh b/ld/emulparams/elf_l1om.sh
index b1158791f4..b7aab70200 100644
--- a/ld/emulparams/elf_l1om.sh
+++ b/ld/emulparams/elf_l1om.sh
@@ -15,6 +15,7 @@ COMPILE_IN=yes
TEMPLATE_NAME=elf32
GENERATE_SHLIB_SCRIPT=yes
GENERATE_PIE_SCRIPT=yes
+EXCLUDE_SHLIB_SYMBOLS=yes
NO_SMALL_DATA=yes
LARGE_SECTIONS=yes
LARGE_BSS_AFTER_BSS=
diff --git a/ld/emulparams/elf_x86_64.sh b/ld/emulparams/elf_x86_64.sh
index d8c6e54894..28dcf78cb4 100644
--- a/ld/emulparams/elf_x86_64.sh
+++ b/ld/emulparams/elf_x86_64.sh
@@ -16,6 +16,7 @@ MACHINE=
TEMPLATE_NAME=elf32
GENERATE_SHLIB_SCRIPT=yes
GENERATE_PIE_SCRIPT=yes
+EXCLUDE_SHLIB_SYMBOLS=yes
NO_SMALL_DATA=yes
LARGE_SECTIONS=yes
LARGE_BSS_AFTER_BSS=
diff --git a/ld/scripttempl/elf.sc b/ld/scripttempl/elf.sc
index 5ccdd415f3..88a0e9617c 100644
--- a/ld/scripttempl/elf.sc
+++ b/ld/scripttempl/elf.sc
@@ -72,6 +72,8 @@
# USER_LABEL_PREFIX - prefix to add to user-visible symbols.
# RODATA_NAME, SDATA_NAME, SBSS_NAME, BSS_NAME - base parts of names
# for standard sections, without initial "." or suffixes.
+# EXCLUDE_SHLIB_SYMBOLS - exclude _edata, __bss_start, _end and end
+# from shared libraries.
#
# When adding sections, do note that the names of some sections are used
# when specifying the start address of the next.
@@ -339,6 +341,10 @@ else
SIZEOF_HEADERS_CODE=
fi
+# If EXCLUDE_SHLIB_SYMBOLS is set, set NO_SHLIB_SYMBOLS when creating
+# shared library.
+test -n "${EXCLUDE_SHLIB_SYMBOLS}" && test -n "${CREATE_SHLIB}" && NO_SHLIB_SYMBOLS=" "
+
# If this is for an embedded system, don't add SIZEOF_HEADERS.
if [ -z "$EMBEDDED" ]; then
test -z "${TEXT_BASE_ADDRESS}" && TEXT_BASE_ADDRESS="${TEXT_START_ADDR}${SIZEOF_HEADERS_CODE}"
@@ -637,9 +643,9 @@ 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+${NO_SHLIB_SYMBOLS-${DATA_END_SYMBOLS-${USER_LABEL_PREFIX}_edata = .; PROVIDE (${USER_LABEL_PREFIX}edata = .);}}}
${RELOCATING+. = .;}
- ${RELOCATING+${USER_LABEL_PREFIX}__bss_start = .;}
+ ${RELOCATING+${NO_SHLIB_SYMBOLS-${USER_LABEL_PREFIX}__bss_start = .;}}
${RELOCATING+${OTHER_BSS_SYMBOLS}}
${DATA_SDATA-${SBSS}}
${BSS_PLT+${PLT}}
@@ -672,7 +678,7 @@ cat <<EOF
${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+${NO_SHLIB_SYMBOLS-${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" \
+ ] \
]
}
--
2.17.1