Introduce new .text.sorted.* sections.

Message ID caa076c6-e356-f5e3-756d-eb1bbf498293@suse.cz
State New
Headers show
Series
  • Introduce new .text.sorted.* sections.
Related show

Commit Message

Martin Liška Sept. 24, 2019, 12:05 p.m.
Hi.

Aim of the patch is to introduce a new .text subsection that
will be sorted by default. That can be leveraged in LTO where we
want to provide a specific sorting of symbols in a binary.

For more information about IPA reorder, please take a look:
https://gcc.gnu.org/ml/gcc-patches/2019-09/msg01142.html

Ready to be installed?
Thanks,
Martin

gold/ChangeLog:

2019-09-24  Martin Liska  <mliska@suse.cz>

	* layout.cc (Layout::special_ordering_of_input_section):
	Return index for .text.sorted.* sections.
	* testsuite/section_sorting_name.cc: Cover also .text.sorted
	subsections.
	* testsuite/section_sorting_name.sh: Likewise.

ld/ChangeLog:

2019-09-24  Martin Liska  <mliska@suse.cz>

	* scripttempl/arclinux.sc: Add .text.sorted.* which is sorted
	by default.
	* scripttempl/elf.sc: Likewise.
	* scripttempl/elf64bpf.sc: Likewise.
	* scripttempl/nds32elf.sc: Likewise.
	* testsuite/ld-arm/arm-no-rel-plt.ld: Expect .text.sorted.*
	in the default linker script.
	* testsuite/ld-arm/fdpic-main.ld: Likewise.
	* testsuite/ld-arm/fdpic-shared.ld: Likewise.
---
 gold/layout.cc                         |  9 ++++++---
 gold/testsuite/section_sorting_name.cc | 22 ++++++++++++++++++++++
 gold/testsuite/section_sorting_name.sh |  3 +++
 ld/scripttempl/arclinux.sc             |  1 +
 ld/scripttempl/elf.sc                  |  1 +
 ld/scripttempl/elf64bpf.sc             |  1 +
 ld/scripttempl/nds32elf.sc             |  1 +
 ld/testsuite/ld-arm/arm-no-rel-plt.ld  |  1 +
 ld/testsuite/ld-arm/fdpic-main.ld      |  1 +
 ld/testsuite/ld-arm/fdpic-shared.ld    |  1 +
 10 files changed, 38 insertions(+), 3 deletions(-)

Comments

Alan Modra Sept. 25, 2019, 1:34 p.m. | #1
On Tue, Sep 24, 2019 at 02:05:09PM +0200, Martin Liška wrote:
> Hi.

> 

> Aim of the patch is to introduce a new .text subsection that

> will be sorted by default. That can be leveraged in LTO where we

> want to provide a specific sorting of symbols in a binary.


This looks fine to me, even though the gold implementation can produce
different results on input sections that match ".text.sorted.*".  For
example, ".text.sorted.1" will be accepted by both ld.gold and ld.bfd,
but ld.bfd will sort it after ".text.sorted.02" while ld.gold would
sort it before.

-- 
Alan Modra
Australia Development Lab, IBM
Martin Liška Sept. 26, 2019, 8:48 a.m. | #2
On 9/25/19 3:34 PM, Alan Modra wrote:
> On Tue, Sep 24, 2019 at 02:05:09PM +0200, Martin Liška wrote:

>> Hi.

>>

>> Aim of the patch is to introduce a new .text subsection that

>> will be sorted by default. That can be leveraged in LTO where we

>> want to provide a specific sorting of symbols in a binary.

> 

> This looks fine to me, even though the gold implementation can produce

> different results on input sections that match ".text.sorted.*".  For

> example, ".text.sorted.1" will be accepted by both ld.gold and ld.bfd,

> but ld.bfd will sort it after ".text.sorted.02" while ld.gold would

> sort it before.


Hi.

You are right, it's not precisely 1:1 among ld.bfd and ld.gold. I guess
we can live with that.

May I take this as an approval to install the patch?
Thanks,
Martin

>
Alan Modra Sept. 27, 2019, 7:48 a.m. | #3
On Thu, Sep 26, 2019 at 10:48:12AM +0200, Martin Liška wrote:
> On 9/25/19 3:34 PM, Alan Modra wrote:

> > On Tue, Sep 24, 2019 at 02:05:09PM +0200, Martin Liška wrote:

> >> Hi.

> >>

> >> Aim of the patch is to introduce a new .text subsection that

> >> will be sorted by default. That can be leveraged in LTO where we

> >> want to provide a specific sorting of symbols in a binary.

> > 

> > This looks fine to me, even though the gold implementation can produce

> > different results on input sections that match ".text.sorted.*".  For

> > example, ".text.sorted.1" will be accepted by both ld.gold and ld.bfd,

> > but ld.bfd will sort it after ".text.sorted.02" while ld.gold would

> > sort it before.

> 

> Hi.

> 

> You are right, it's not precisely 1:1 among ld.bfd and ld.gold. I guess

> we can live with that.

> 

> May I take this as an approval to install the patch?


I can OK the ld.bfd changes, I'm not so sure about gold patches.  Cary
and Ian are the ones with that authority.

-- 
Alan Modra
Australia Development Lab, IBM
Martin Liška Nov. 4, 2019, 1:54 p.m. | #4
@Ian, @Cary: May I please ping the gold part?

Thanks,
Martin

On 9/27/19 9:48 AM, Alan Modra wrote:
> On Thu, Sep 26, 2019 at 10:48:12AM +0200, Martin Liška wrote:

>> On 9/25/19 3:34 PM, Alan Modra wrote:

>>> On Tue, Sep 24, 2019 at 02:05:09PM +0200, Martin Liška wrote:

>>>> Hi.

>>>>

>>>> Aim of the patch is to introduce a new .text subsection that

>>>> will be sorted by default. That can be leveraged in LTO where we

>>>> want to provide a specific sorting of symbols in a binary.

>>>

>>> This looks fine to me, even though the gold implementation can produce

>>> different results on input sections that match ".text.sorted.*".  For

>>> example, ".text.sorted.1" will be accepted by both ld.gold and ld.bfd,

>>> but ld.bfd will sort it after ".text.sorted.02" while ld.gold would

>>> sort it before.

>>

>> Hi.

>>

>> You are right, it's not precisely 1:1 among ld.bfd and ld.gold. I guess

>> we can live with that.

>>

>> May I take this as an approval to install the patch?

> 

> I can OK the ld.bfd changes, I'm not so sure about gold patches.  Cary

> and Ian are the ones with that authority.

>
Martin Liška Nov. 19, 2019, 12:30 p.m. | #5
On 11/4/19 2:54 PM, Martin Liška wrote:
> @Ian, @Cary: May I please ping the gold part?


PING^2

Thanks,
Martin

> 

> Thanks,

> Martin

> 

> On 9/27/19 9:48 AM, Alan Modra wrote:

>> On Thu, Sep 26, 2019 at 10:48:12AM +0200, Martin Liška wrote:

>>> On 9/25/19 3:34 PM, Alan Modra wrote:

>>>> On Tue, Sep 24, 2019 at 02:05:09PM +0200, Martin Liška wrote:

>>>>> Hi.

>>>>>

>>>>> Aim of the patch is to introduce a new .text subsection that

>>>>> will be sorted by default. That can be leveraged in LTO where we

>>>>> want to provide a specific sorting of symbols in a binary.

>>>>

>>>> This looks fine to me, even though the gold implementation can produce

>>>> different results on input sections that match ".text.sorted.*".  For

>>>> example, ".text.sorted.1" will be accepted by both ld.gold and ld.bfd,

>>>> but ld.bfd will sort it after ".text.sorted.02" while ld.gold would

>>>> sort it before.

>>>

>>> Hi.

>>>

>>> You are right, it's not precisely 1:1 among ld.bfd and ld.gold. I guess

>>> we can live with that.

>>>

>>> May I take this as an approval to install the patch?

>>

>> I can OK the ld.bfd changes, I'm not so sure about gold patches.  Cary

>> and Ian are the ones with that authority.

>>

>

Patch

diff --git a/gold/layout.cc b/gold/layout.cc
index 194d088c2a..6f3c0f39d5 100644
--- a/gold/layout.cc
+++ b/gold/layout.cc
@@ -1132,12 +1132,15 @@  Layout::special_ordering_of_input_section(const char* name)
     ".text.hot"
   };
 
-  for (size_t i = 0;
-       i < sizeof(text_section_sort) / sizeof(text_section_sort[0]);
-       i++)
+  unsigned scount = sizeof(text_section_sort) / sizeof(text_section_sort[0]);
+  for (size_t i = 0; i < scount; i++)
     if (is_prefix_of(text_section_sort[i], name))
       return i;
 
+  int order;
+  if (sscanf (name, ".text.sorted.%d", &order) == 1)
+    return order + scount;
+
   return -1;
 }
 
diff --git a/gold/testsuite/section_sorting_name.cc b/gold/testsuite/section_sorting_name.cc
index 3a1d9baac3..d72684fd5b 100644
--- a/gold/testsuite/section_sorting_name.cc
+++ b/gold/testsuite/section_sorting_name.cc
@@ -50,6 +50,28 @@  int hot_foo_0002()
   return 1;
 }
 
+extern "C"
+__attribute__ ((section(".text.sorted.0002")))
+int sorted_foo_0002()
+{
+  return 1;
+}
+
+extern "C"
+__attribute__ ((section(".text.sorted.0001")))
+int sorted_foo_0001()
+{
+  return 1;
+}
+
+extern "C"
+__attribute__ ((section(".text.sorted.0003")))
+int sorted_foo_0003()
+{
+  return 1;
+}
+
+
 int vdata_0002  __attribute__((section(".data.0002"))) = 2;
 int vbss_0002 __attribute__((section(".bss.0002"))) = 0;
 
diff --git a/gold/testsuite/section_sorting_name.sh b/gold/testsuite/section_sorting_name.sh
index ae44570f7f..fa48aac1cf 100755
--- a/gold/testsuite/section_sorting_name.sh
+++ b/gold/testsuite/section_sorting_name.sh
@@ -59,6 +59,9 @@  END {
 check section_sorting_name.stdout "hot_foo_0001" "hot_foo_0002"
 check section_sorting_name.stdout "hot_foo_0002" "hot_foo_0003"
 
+check section_sorting_name.stdout "sorted_foo_0001" "sorted_foo_0002"
+check section_sorting_name.stdout "sorted_foo_0002" "sorted_foo_0003"
+
 check section_sorting_name.stdout "vdata_0001" "vdata_0002"
 check section_sorting_name.stdout "vdata_0002" "vdata_0003"
 
diff --git a/ld/scripttempl/arclinux.sc b/ld/scripttempl/arclinux.sc
index e13969ede0..41e8ccdf1b 100644
--- a/ld/scripttempl/arclinux.sc
+++ b/ld/scripttempl/arclinux.sc
@@ -491,6 +491,7 @@  cat <<EOF
     ${RELOCATING+*(.text.exit .text.exit.*)}
     ${RELOCATING+*(.text.startup .text.startup.*)}
     ${RELOCATING+*(.text.hot .text.hot.*)}
+    ${RELOCATING+*(SORT(.text.sorted.*))}
     *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
     /* .gnu.warning sections are handled specially by elf.em.  */
     *(.gnu.warning)
diff --git a/ld/scripttempl/elf.sc b/ld/scripttempl/elf.sc
index c3ad467bff..0d61881185 100644
--- a/ld/scripttempl/elf.sc
+++ b/ld/scripttempl/elf.sc
@@ -514,6 +514,7 @@  cat <<EOF
     ${RELOCATING+*(.text.exit .text.exit.*)}
     ${RELOCATING+*(.text.startup .text.startup.*)}
     ${RELOCATING+*(.text.hot .text.hot.*)}
+    ${RELOCATING+*(SORT(.text.sorted.*))}
     *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
     /* .gnu.warning sections are handled specially by elf.em.  */
     *(.gnu.warning)
diff --git a/ld/scripttempl/elf64bpf.sc b/ld/scripttempl/elf64bpf.sc
index de73775349..7937a41d2c 100644
--- a/ld/scripttempl/elf64bpf.sc
+++ b/ld/scripttempl/elf64bpf.sc
@@ -512,6 +512,7 @@  cat <<EOF
     ${RELOCATING+*(.text.exit .text.exit.*)}
     ${RELOCATING+*(.text.startup .text.startup.*)}
     ${RELOCATING+*(.text.hot .text.hot.*)}
+    ${RELOCATING+*(SORT(.text.sorted.*))}
     *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
     /* .gnu.warning sections are handled specially by elf.em.  */
     *(.gnu.warning)
diff --git a/ld/scripttempl/nds32elf.sc b/ld/scripttempl/nds32elf.sc
index 065c984f70..8d8d6e3f74 100644
--- a/ld/scripttempl/nds32elf.sc
+++ b/ld/scripttempl/nds32elf.sc
@@ -438,6 +438,7 @@  cat <<EOF
     ${RELOCATING+*(.text.exit .text.exit.*)}
     ${RELOCATING+*(.text.startup .text.startup.*)}
     ${RELOCATING+*(.text.hot .text.hot.*)}
+    ${RELOCATING+*(SORT(.text.sorted.*))}
     *(.text .stub${RELOCATING+ .text.* .gnu.linkonce.t.*})
     /* .gnu.warning sections are handled specially by elf.em.  */
     *(.gnu.warning)
diff --git a/ld/testsuite/ld-arm/arm-no-rel-plt.ld b/ld/testsuite/ld-arm/arm-no-rel-plt.ld
index d56e8203ad..14c1aeb439 100644
--- a/ld/testsuite/ld-arm/arm-no-rel-plt.ld
+++ b/ld/testsuite/ld-arm/arm-no-rel-plt.ld
@@ -65,6 +65,7 @@  SECTIONS
     *(.text.exit .text.exit.*)
     *(.text.startup .text.startup.*)
     *(.text.hot .text.hot.*)
+    *(SORT(.text.sorted.*))
     *(.text .stub .text.* .gnu.linkonce.t.*)
     /* .gnu.warning sections are handled specially by elf.em.  */
     *(.gnu.warning)
diff --git a/ld/testsuite/ld-arm/fdpic-main.ld b/ld/testsuite/ld-arm/fdpic-main.ld
index d19a589d6c..b01b630fea 100644
--- a/ld/testsuite/ld-arm/fdpic-main.ld
+++ b/ld/testsuite/ld-arm/fdpic-main.ld
@@ -76,6 +76,7 @@  SECTIONS
     *(.text.exit .text.exit.*)
     *(.text.startup .text.startup.*)
     *(.text.hot .text.hot.*)
+    *(SORT(.text.sorted.*))
     *(.text .stub .text.* .gnu.linkonce.t.*)
     /* .gnu.warning sections are handled specially by elf.em.  */
     *(.gnu.warning)
diff --git a/ld/testsuite/ld-arm/fdpic-shared.ld b/ld/testsuite/ld-arm/fdpic-shared.ld
index b1e262d829..b710ffa745 100644
--- a/ld/testsuite/ld-arm/fdpic-shared.ld
+++ b/ld/testsuite/ld-arm/fdpic-shared.ld
@@ -67,6 +67,7 @@  SECTIONS
     *(.text.exit .text.exit.*)
     *(.text.startup .text.startup.*)
     *(.text.hot .text.hot.*)
+    *(SORT(.text.sorted.*))
     *(.text .stub .text.* .gnu.linkonce.t.*)
     /* .gnu.warning sections are handled specially by elf.em.  */
     *(.gnu.warning)