From 4b8377e7dbd9e53149268685ba4810a7b09fb0cb Mon Sep 17 00:00:00 2001 From: "Maciej W. Rozycki" Date: Mon, 9 Jul 2018 21:30:44 +0100 Subject: [PATCH] MIPS/BFD: Do not redirect to discarded lazy binding stubs Correct a MIPS/BFD linker issue with dynamic symbol and corresponding GOT entry values being redirected to lazy binding stubs where the stubs section has been discarded by assigning to the `/DISCARD/' output section in the linker script used. The issue manifests itself by the values entered being relative to the absolute section, which is what any discarded sections are internally assigned in the linker. For the `stub-dynsym-2.s' piece of code included as a test case with this change this issue results in the dynamic symbol table and the GOT looking like: Symbol table '.dynsym' contains 3 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000 0 NOTYPE LOCAL DEFAULT UND 1: 00000010 0 FUNC GLOBAL DEFAULT UND bar 2: 00000000 0 FUNC GLOBAL DEFAULT UND foo Primary GOT: Canonical gp value: 00097ff0 Reserved entries: Address Access Initial Purpose 00090000 -32752(gp) 00000000 Lazy resolver 00090004 -32748(gp) 80000000 Module pointer (GNU extension) Global entries: Address Access Initial Sym.Val. Type Ndx Name 00090008 -32744(gp) 00000010 00000010 FUNC UND bar 0009000c -32740(gp) 00000000 00000000 FUNC UND foo if assembled to regular MIPS code, or: Symbol table '.dynsym' contains 3 entries: Num: Value Size Type Bind Vis Ndx Name 0: 00000000 0 NOTYPE LOCAL DEFAULT UND 1: 0000000d 0 FUNC GLOBAL DEFAULT UND bar 2: 00000001 0 FUNC GLOBAL DEFAULT UND foo Primary GOT: Canonical gp value: 00097ff0 Reserved entries: Address Access Initial Purpose 00090000 -32752(gp) 00000000 Lazy resolver 00090004 -32748(gp) 80000000 Module pointer (GNU extension) Global entries: Address Access Initial Sym.Val. Type Ndx Name 00090008 -32744(gp) 0000000d 0000000d FUNC UND bar 0009000c -32740(gp) 00000001 00000001 FUNC UND foo if assembled to microMIPS code. Symbol values and GOT entries record the offset into the inexistent stubs section and the ISA bit rather than zero, which would be the case if a lazy binding stub was not used for other reasons, such as the value of the symbol being taken for a purpose other than making a function call (e.g. an R_MIPS_GOT16 relocation). Correct the issue by refraining from redirecting symbols to lazy binding stubs if the stubs section is going to be discarded. bfd/ * elfxx-mips.c (_bfd_mips_elf_adjust_dynamic_symbol): Don't set `->needs_lazy_stub' if the stubs output section is the absolute section. ld/ * testsuite/ld-mips-elf/stub-dynsym-2.dd: New test. * testsuite/ld-mips-elf/stub-dynsym-2.gd: New test. * testsuite/ld-mips-elf/stub-dynsym-2.sd: New test. * testsuite/ld-mips-elf/stub-dynsym-discard-2.gd: New test. * testsuite/ld-mips-elf/stub-dynsym-discard-2.sd: New test. * testsuite/ld-mips-elf/stub-dynsym-micromips-2.dd: New test. * testsuite/ld-mips-elf/stub-dynsym-micromips-2.gd: New test. * testsuite/ld-mips-elf/stub-dynsym-micromips-2.sd: New test. * testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.dd: New test. * testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.gd: New test. * testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.sd: New test. * testsuite/ld-mips-elf/stub-dynsym-2.ld: New test linker script. * testsuite/ld-mips-elf/stub-dynsym-discard-2.ld: New test linker script. * testsuite/ld-mips-elf/mips-elf.exp: Run the new tests. --- bfd/ChangeLog | 6 ++++ bfd/elfxx-mips.c | 3 +- ld/ChangeLog | 22 ++++++++++++ ld/testsuite/ld-mips-elf/mips-elf.exp | 36 +++++++++++++++++++ ld/testsuite/ld-mips-elf/stub-dynsym-2.dd | 17 +++++++++ ld/testsuite/ld-mips-elf/stub-dynsym-2.gd | 12 +++++++ ld/testsuite/ld-mips-elf/stub-dynsym-2.ld | 21 +++++++++++ ld/testsuite/ld-mips-elf/stub-dynsym-2.s | 2 ++ ld/testsuite/ld-mips-elf/stub-dynsym-2.sd | 5 +++ .../ld-mips-elf/stub-dynsym-discard-2.gd | 12 +++++++ .../ld-mips-elf/stub-dynsym-discard-2.ld | 22 ++++++++++++ .../ld-mips-elf/stub-dynsym-discard-2.sd | 5 +++ .../ld-mips-elf/stub-dynsym-micromips-2.dd | 16 +++++++++ .../ld-mips-elf/stub-dynsym-micromips-2.gd | 12 +++++++ .../ld-mips-elf/stub-dynsym-micromips-2.sd | 5 +++ .../stub-dynsym-micromips-insn32-2.dd | 17 +++++++++ .../stub-dynsym-micromips-insn32-2.gd | 12 +++++++ .../stub-dynsym-micromips-insn32-2.sd | 5 +++ 18 files changed, 229 insertions(+), 1 deletion(-) create mode 100644 ld/testsuite/ld-mips-elf/stub-dynsym-2.dd create mode 100644 ld/testsuite/ld-mips-elf/stub-dynsym-2.gd create mode 100644 ld/testsuite/ld-mips-elf/stub-dynsym-2.ld create mode 100644 ld/testsuite/ld-mips-elf/stub-dynsym-2.s create mode 100644 ld/testsuite/ld-mips-elf/stub-dynsym-2.sd create mode 100644 ld/testsuite/ld-mips-elf/stub-dynsym-discard-2.gd create mode 100644 ld/testsuite/ld-mips-elf/stub-dynsym-discard-2.ld create mode 100644 ld/testsuite/ld-mips-elf/stub-dynsym-discard-2.sd create mode 100644 ld/testsuite/ld-mips-elf/stub-dynsym-micromips-2.dd create mode 100644 ld/testsuite/ld-mips-elf/stub-dynsym-micromips-2.gd create mode 100644 ld/testsuite/ld-mips-elf/stub-dynsym-micromips-2.sd create mode 100644 ld/testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.dd create mode 100644 ld/testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.gd create mode 100644 ld/testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.sd diff --git a/bfd/ChangeLog b/bfd/ChangeLog index 4b0454d8a8d..7e8cf8caf98 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,9 @@ +2018-07-09 Maciej W. Rozycki + + * elfxx-mips.c (_bfd_mips_elf_adjust_dynamic_symbol): Don't set + `->needs_lazy_stub' if the stubs output section is the absolute + section. + 2018-07-09 H.J. Lu PR ld/23388 diff --git a/bfd/elfxx-mips.c b/bfd/elfxx-mips.c index c33b276ceff..990e3a1e551 100644 --- a/bfd/elfxx-mips.c +++ b/bfd/elfxx-mips.c @@ -9043,7 +9043,8 @@ _bfd_mips_elf_adjust_dynamic_symbol (struct bfd_link_info *info, the symbol to the stub location. This is required to make function pointers compare as equal between the normal executable and the shared library. */ - if (!h->def_regular) + if (!h->def_regular + && !bfd_is_abs_section (htab->sstubs->output_section)) { hmips->needs_lazy_stub = TRUE; htab->lazy_stub_count++; diff --git a/ld/ChangeLog b/ld/ChangeLog index c146f5cf937..7d382b9f4a1 100644 --- a/ld/ChangeLog +++ b/ld/ChangeLog @@ -1,3 +1,25 @@ +2018-07-09 Maciej W. Rozycki + + * testsuite/ld-mips-elf/stub-dynsym-2.dd: New test. + * testsuite/ld-mips-elf/stub-dynsym-2.gd: New test. + * testsuite/ld-mips-elf/stub-dynsym-2.sd: New test. + * testsuite/ld-mips-elf/stub-dynsym-discard-2.gd: New test. + * testsuite/ld-mips-elf/stub-dynsym-discard-2.sd: New test. + * testsuite/ld-mips-elf/stub-dynsym-micromips-2.dd: New test. + * testsuite/ld-mips-elf/stub-dynsym-micromips-2.gd: New test. + * testsuite/ld-mips-elf/stub-dynsym-micromips-2.sd: New test. + * testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.dd: New + test. + * testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.gd: New + test. + * testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.sd: New + test. + * testsuite/ld-mips-elf/stub-dynsym-2.ld: New test linker + script. + * testsuite/ld-mips-elf/stub-dynsym-discard-2.ld: New test + linker script. + * testsuite/ld-mips-elf/mips-elf.exp: Run the new tests. + 2018-07-09 Maciej W. Rozycki * testsuite/lib/ld-lib.exp (run_dump_test): Call `unsupported' diff --git a/ld/testsuite/ld-mips-elf/mips-elf.exp b/ld/testsuite/ld-mips-elf/mips-elf.exp index ddd90f25ee2..8b0594afc54 100644 --- a/ld/testsuite/ld-mips-elf/mips-elf.exp +++ b/ld/testsuite/ld-mips-elf/mips-elf.exp @@ -807,6 +807,42 @@ if { $linux_gnu } { "stub-dynsym$suffix-1-$dynsym.d"]] \ "stub-dynsym$suffix-1-$dynsym"]] } + run_ld_link_tests [list \ + [list \ + "Retained stubs for dynsyms ($isa)" \ + "-shared -melf32btsmip -T stub-dynsym-2.ld $lflag" "" \ + "-EB $aflag -32 -KPIC" \ + [list "stub-dynsym-2.s"] \ + [list \ + [list \ + "objdump" \ + "-dz -j .MIPS.stubs" \ + "stub-dynsym$suffix-2.dd"] \ + [list \ + "readelf" \ + "--dyn-syms" \ + "stub-dynsym$suffix-2.sd"] \ + [list \ + "readelf" \ + "-A" \ + "stub-dynsym$suffix-2.gd"]] \ + "stub-dynsym$suffix-2"] \ + [list \ + "Discarded stubs for dynsyms ($isa)" \ + "-shared -melf32btsmip -T stub-dynsym-discard-2.ld $lflag" \ + "" \ + "-EB $aflag -32 -KPIC" \ + [list "stub-dynsym-2.s"] \ + [list \ + [list \ + "readelf" \ + "--dyn-syms" \ + "stub-dynsym-discard-2.sd"] \ + [list \ + "readelf" \ + "-A" \ + "stub-dynsym-discard-2.gd"]] \ + "stub-dynsym-discard$suffix-2"]] } } diff --git a/ld/testsuite/ld-mips-elf/stub-dynsym-2.dd b/ld/testsuite/ld-mips-elf/stub-dynsym-2.dd new file mode 100644 index 00000000000..6013a4763e6 --- /dev/null +++ b/ld/testsuite/ld-mips-elf/stub-dynsym-2.dd @@ -0,0 +1,17 @@ +.*: +file format .*mips.* + +Disassembly of section \.MIPS\.stubs: + +000800ec <_MIPS_STUBS_>: + 800ec: 8f998010 lw t9,-32752\(gp\) + 800f0: 03e07825 move t7,ra + 800f4: 0320f809 jalr t9 + 800f8: 24180002 li t8,2 + 800fc: 8f998010 lw t9,-32752\(gp\) + 80100: 03e07825 move t7,ra + 80104: 0320f809 jalr t9 + 80108: 24180001 li t8,1 + 8010c: 00000000 nop + 80110: 00000000 nop + 80114: 00000000 nop + 80118: 00000000 nop diff --git a/ld/testsuite/ld-mips-elf/stub-dynsym-2.gd b/ld/testsuite/ld-mips-elf/stub-dynsym-2.gd new file mode 100644 index 00000000000..411cdf12bfd --- /dev/null +++ b/ld/testsuite/ld-mips-elf/stub-dynsym-2.gd @@ -0,0 +1,12 @@ +Primary GOT: + Canonical gp value: 00097ff0 + + Reserved entries: + Address Access Initial Purpose + 00090000 -32752\(gp\) 00000000 Lazy resolver + 00090004 -32748\(gp\) 80000000 Module pointer \(GNU extension\) + + Global entries: + Address Access Initial Sym\.Val\. Type Ndx Name + 00090008 -32744\(gp\) 000800fc 000800fc FUNC UND bar + 0009000c -32740\(gp\) 000800ec 000800ec FUNC UND foo diff --git a/ld/testsuite/ld-mips-elf/stub-dynsym-2.ld b/ld/testsuite/ld-mips-elf/stub-dynsym-2.ld new file mode 100644 index 00000000000..a385012f4ae --- /dev/null +++ b/ld/testsuite/ld-mips-elf/stub-dynsym-2.ld @@ -0,0 +1,21 @@ +SECTIONS +{ + . = 0x80000; + .interp : { *(.interp) } + .hash : { *(.hash) } + .dynsym : { *(.dynsym) } + .dynstr : { *(.dynstr) } + .rel.dyn : { *(.rel.dyn) } + .MIPS.stubs : { *(.MIPS.stubs) } + .text : { *(.text) } + + . = ALIGN (0x10000); + HIDDEN (_gp = . + 0x7ff0); + .got : { *(.got) } + + .symtab : { *(.symtab) } + .strtab : { *(.strtab) } + .shstrtab : { *(.shstrtab) } + + /DISCARD/ : { *(.reginfo) *(.MIPS.abiflags) *(.gnu.attributes) } +} diff --git a/ld/testsuite/ld-mips-elf/stub-dynsym-2.s b/ld/testsuite/ld-mips-elf/stub-dynsym-2.s new file mode 100644 index 00000000000..9c5f166d693 --- /dev/null +++ b/ld/testsuite/ld-mips-elf/stub-dynsym-2.s @@ -0,0 +1,2 @@ + lw $25,%call16(foo)($gp) + lw $25,%call16(bar)($gp) diff --git a/ld/testsuite/ld-mips-elf/stub-dynsym-2.sd b/ld/testsuite/ld-mips-elf/stub-dynsym-2.sd new file mode 100644 index 00000000000..4960ae7ed35 --- /dev/null +++ b/ld/testsuite/ld-mips-elf/stub-dynsym-2.sd @@ -0,0 +1,5 @@ +Symbol table '\.dynsym' contains 3 entries: + Num: Value Size Type Bind Vis Ndx Name + 0: 00000000 0 NOTYPE LOCAL DEFAULT UND + 1: 000800fc 0 FUNC GLOBAL DEFAULT UND bar + 2: 000800ec 0 FUNC GLOBAL DEFAULT UND foo diff --git a/ld/testsuite/ld-mips-elf/stub-dynsym-discard-2.gd b/ld/testsuite/ld-mips-elf/stub-dynsym-discard-2.gd new file mode 100644 index 00000000000..f56553ac0cb --- /dev/null +++ b/ld/testsuite/ld-mips-elf/stub-dynsym-discard-2.gd @@ -0,0 +1,12 @@ +Primary GOT: + Canonical gp value: 00097ff0 + + Reserved entries: + Address Access Initial Purpose + 00090000 -32752\(gp\) 00000000 Lazy resolver + 00090004 -32748\(gp\) 80000000 Module pointer \(GNU extension\) + + Global entries: + Address Access Initial Sym\.Val\. Type Ndx Name + 00090008 -32744\(gp\) 00000000 00000000 FUNC UND bar + 0009000c -32740\(gp\) 00000000 00000000 FUNC UND foo diff --git a/ld/testsuite/ld-mips-elf/stub-dynsym-discard-2.ld b/ld/testsuite/ld-mips-elf/stub-dynsym-discard-2.ld new file mode 100644 index 00000000000..e6d89bd3459 --- /dev/null +++ b/ld/testsuite/ld-mips-elf/stub-dynsym-discard-2.ld @@ -0,0 +1,22 @@ +SECTIONS +{ + . = 0x80000; + .interp : { *(.interp) } + .hash : { *(.hash) } + .dynsym : { *(.dynsym) } + .dynstr : { *(.dynstr) } + .rel.dyn : { *(.rel.dyn) } + .text : { *(.text) } + + . = ALIGN (0x10000); + HIDDEN (_gp = . + 0x7ff0); + .got : { *(.got) } + + .symtab : { *(.symtab) } + .strtab : { *(.strtab) } + .shstrtab : { *(.shstrtab) } + + /DISCARD/ : { + *(.reginfo) *(.MIPS.abiflags) *(.MIPS.stubs) *(.gnu.attributes) + } +} diff --git a/ld/testsuite/ld-mips-elf/stub-dynsym-discard-2.sd b/ld/testsuite/ld-mips-elf/stub-dynsym-discard-2.sd new file mode 100644 index 00000000000..f78f9391162 --- /dev/null +++ b/ld/testsuite/ld-mips-elf/stub-dynsym-discard-2.sd @@ -0,0 +1,5 @@ +Symbol table '\.dynsym' contains 3 entries: + Num: Value Size Type Bind Vis Ndx Name + 0: 00000000 0 NOTYPE LOCAL DEFAULT UND + 1: 00000000 0 FUNC GLOBAL DEFAULT UND bar + 2: 00000000 0 FUNC GLOBAL DEFAULT UND foo diff --git a/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-2.dd b/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-2.dd new file mode 100644 index 00000000000..7c3bf25a854 --- /dev/null +++ b/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-2.dd @@ -0,0 +1,16 @@ +.*: +file format .*mips.* + +Disassembly of section \.MIPS\.stubs: + +000800ec <_MIPS_STUBS_>: + 800ec: ff3c 8010 lw t9,-32752\(gp\) + 800f0: 0dff move t7,ra + 800f2: 45d9 jalr t9 + 800f4: 3300 0002 li t8,2 + 800f8: ff3c 8010 lw t9,-32752\(gp\) + 800fc: 0dff move t7,ra + 800fe: 45d9 jalr t9 + 80100: 3300 0001 li t8,1 + 80104: 0000 0000 nop + 80108: 0000 0000 nop + 8010c: 0000 0000 nop diff --git a/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-2.gd b/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-2.gd new file mode 100644 index 00000000000..2691d1db968 --- /dev/null +++ b/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-2.gd @@ -0,0 +1,12 @@ +Primary GOT: + Canonical gp value: 00097ff0 + + Reserved entries: + Address Access Initial Purpose + 00090000 -32752\(gp\) 00000000 Lazy resolver + 00090004 -32748\(gp\) 80000000 Module pointer \(GNU extension\) + + Global entries: + Address Access Initial Sym\.Val\. Type Ndx Name + 00090008 -32744\(gp\) 000800f9 000800f9 FUNC UND bar + 0009000c -32740\(gp\) 000800ed 000800ed FUNC UND foo diff --git a/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-2.sd b/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-2.sd new file mode 100644 index 00000000000..d0b8d49a7fc --- /dev/null +++ b/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-2.sd @@ -0,0 +1,5 @@ +Symbol table '\.dynsym' contains 3 entries: + Num: Value Size Type Bind Vis Ndx Name + 0: 00000000 0 NOTYPE LOCAL DEFAULT UND + 1: 000800f9 0 FUNC GLOBAL DEFAULT UND bar + 2: 000800ed 0 FUNC GLOBAL DEFAULT UND foo diff --git a/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.dd b/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.dd new file mode 100644 index 00000000000..8c3374131ad --- /dev/null +++ b/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.dd @@ -0,0 +1,17 @@ +.*: +file format .*mips.* + +Disassembly of section \.MIPS\.stubs: + +000800ec <_MIPS_STUBS_>: + 800ec: ff3c 8010 lw t9,-32752\(gp\) + 800f0: 001f 7a90 move t7,ra + 800f4: 03f9 0f3c jalr t9 + 800f8: 3300 0002 li t8,2 + 800fc: ff3c 8010 lw t9,-32752\(gp\) + 80100: 001f 7a90 move t7,ra + 80104: 03f9 0f3c jalr t9 + 80108: 3300 0001 li t8,1 + 8010c: 0000 0000 nop + 80110: 0000 0000 nop + 80114: 0000 0000 nop + 80118: 0000 0000 nop diff --git a/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.gd b/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.gd new file mode 100644 index 00000000000..1ebc071cd70 --- /dev/null +++ b/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.gd @@ -0,0 +1,12 @@ +Primary GOT: + Canonical gp value: 00097ff0 + + Reserved entries: + Address Access Initial Purpose + 00090000 -32752\(gp\) 00000000 Lazy resolver + 00090004 -32748\(gp\) 80000000 Module pointer \(GNU extension\) + + Global entries: + Address Access Initial Sym\.Val\. Type Ndx Name + 00090008 -32744\(gp\) 000800fd 000800fd FUNC UND bar + 0009000c -32740\(gp\) 000800ed 000800ed FUNC UND foo diff --git a/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.sd b/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.sd new file mode 100644 index 00000000000..860fd485567 --- /dev/null +++ b/ld/testsuite/ld-mips-elf/stub-dynsym-micromips-insn32-2.sd @@ -0,0 +1,5 @@ +Symbol table '\.dynsym' contains 3 entries: + Num: Value Size Type Bind Vis Ndx Name + 0: 00000000 0 NOTYPE LOCAL DEFAULT UND + 1: 000800fd 0 FUNC GLOBAL DEFAULT UND bar + 2: 000800ed 0 FUNC GLOBAL DEFAULT UND foo -- 2.30.2