+2019-04-30 Alan Modra <amodra@gmail.com>
+
+ * elf64-ppc.c (struct ppc64_elf_obj_tdata): Add has_gotrel.
+ (struct _ppc64_elf_section_data): Likewise.
+ (ppc64_elf_check_relocs): Set above fields.
+ (ppc64_elf_edit_toc): Add a pass over GOT relocs.
+ (ppc64_elf_relocate_section): Edit GOT indirect to GOT relative
+ when possible.
+
2019-04-26 H.J. Lu <hongjiu.lu@intel.com>
PR ld/24486
/* Set if toc/got ha relocs detected not using r2, or lo reloc
instruction not one we handle. */
unsigned int unexpected_toc_insn : 1;
+
+ /* Set if got relocs that can be optimised are present in this file. */
+ unsigned int has_gotrel : 1;
};
#define ppc64_elf_tdata(bfd) \
/* Flag set when PLTCALL relocs are detected. */
unsigned int has_pltcall:1;
+
+ /* Flag set when section has GOT relocations that can be optimised. */
+ unsigned int has_gotrel:1;
};
#define ppc64_elf_section_data(sec) \
tls_type = TLS_TLS | TLS_DTPREL;
dogottls:
sec->has_tls_reloc = 1;
- /* Fall through */
+ goto dogot;
- case R_PPC64_GOT16:
case R_PPC64_GOT16_DS:
case R_PPC64_GOT16_HA:
+ case R_PPC64_GOT16_LO_DS:
+ ppc64_elf_tdata (abfd)->has_gotrel = 1;
+ ppc64_elf_section_data (sec)->has_gotrel = 1;
+ /* Fall through. */
+
+ case R_PPC64_GOT16:
case R_PPC64_GOT16_HI:
case R_PPC64_GOT16_LO:
- case R_PPC64_GOT16_LO_DS:
+ dogot:
/* This symbol requires a global offset table entry. */
sec->has_toc_reloc = 1;
if (r_type == R_PPC64_GOT_TLSLD16
free (skip);
}
+ /* Look for cases where we can change an indirect GOT access to
+ a GOT relative access, possibly reducing the number of GOT
+ entries. */
+ for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
+ {
+ asection *sec;
+ Elf_Internal_Shdr *symtab_hdr;
+ Elf_Internal_Sym *local_syms;
+ Elf_Internal_Rela *relstart, *rel;
+ bfd_vma got;
+
+ if (!is_ppc64_elf (ibfd))
+ continue;
+
+ if (!ppc64_elf_tdata (ibfd)->has_gotrel)
+ continue;
+
+ sec = ppc64_elf_tdata (ibfd)->got;
+ got = sec->output_section->vma + sec->output_offset + 0x8000;
+
+ local_syms = NULL;
+ symtab_hdr = &elf_symtab_hdr (ibfd);
+
+ for (sec = ibfd->sections; sec != NULL; sec = sec->next)
+ {
+ if (sec->reloc_count == 0
+ || !ppc64_elf_section_data (sec)->has_gotrel
+ || discarded_section (sec))
+ continue;
+
+ relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
+ info->keep_memory);
+ if (relstart == NULL)
+ {
+ got_error_ret:
+ if (local_syms != NULL
+ && symtab_hdr->contents != (unsigned char *) local_syms)
+ free (local_syms);
+ if (sec != NULL
+ && relstart != NULL
+ && elf_section_data (sec)->relocs != relstart)
+ free (relstart);
+ return FALSE;
+ }
+
+ for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
+ {
+ enum elf_ppc64_reloc_type r_type;
+ unsigned long r_symndx;
+ Elf_Internal_Sym *sym;
+ asection *sym_sec;
+ struct elf_link_hash_entry *h;
+ struct got_entry *ent;
+ bfd_vma val;
+ unsigned char buf[4];
+ unsigned int insn;
+
+ r_type = ELF64_R_TYPE (rel->r_info);
+ switch (r_type)
+ {
+ default:
+ continue;
+
+ case R_PPC64_GOT16_DS:
+ case R_PPC64_GOT16_HA:
+ case R_PPC64_GOT16_LO_DS:
+ break;
+ }
+
+ r_symndx = ELF64_R_SYM (rel->r_info);
+ if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
+ r_symndx, ibfd))
+ goto got_error_ret;
+
+ if (!SYMBOL_REFERENCES_LOCAL (info, h))
+ continue;
+
+ if (h != NULL)
+ val = h->root.u.def.value;
+ else
+ val = sym->st_value;
+ val += rel->r_addend;
+ val += sym_sec->output_section->vma + sym_sec->output_offset;
+
+ switch (r_type)
+ {
+ default:
+ continue;
+
+ case R_PPC64_GOT16_DS:
+ if (val - got + 0x8000 >= 0x10000)
+ continue;
+ if (!bfd_get_section_contents (ibfd, sec, buf,
+ rel->r_offset & ~3, 4))
+ goto got_error_ret;
+ insn = bfd_get_32 (ibfd, buf);
+ if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
+ continue;
+ break;
+
+ case R_PPC64_GOT16_HA:
+ if (val - got + 0x80008000ULL >= 0x100000000ULL)
+ continue;
+
+ if (!bfd_get_section_contents (ibfd, sec, buf,
+ rel->r_offset & ~3, 4))
+ goto got_error_ret;
+ insn = bfd_get_32 (ibfd, buf);
+ if (((insn & ((0x3f << 26) | 0x1f << 16))
+ != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
+ continue;
+ break;
+
+ case R_PPC64_GOT16_LO_DS:
+ if (val - got + 0x80008000ULL >= 0x100000000ULL)
+ continue;
+ if (!bfd_get_section_contents (ibfd, sec, buf,
+ rel->r_offset & ~3, 4))
+ goto got_error_ret;
+ insn = bfd_get_32 (ibfd, buf);
+ if ((insn & (0x3f << 26 | 0x3)) != 58u << 26 /* ld */)
+ continue;
+ break;
+ }
+
+ if (h != NULL)
+ ent = h->got.glist;
+ else
+ {
+ struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
+ ent = local_got_ents[r_symndx];
+ }
+ for (; ent != NULL; ent = ent->next)
+ if (ent->addend == rel->r_addend
+ && ent->owner == ibfd
+ && ent->tls_type == 0)
+ break;
+ BFD_ASSERT (ent && ent->got.refcount > 0);
+ ent->got.refcount -= 1;
+ }
+
+ if (elf_section_data (sec)->relocs != relstart)
+ free (relstart);
+ }
+
+ if (local_syms != NULL
+ && symtab_hdr->contents != (unsigned char *) local_syms)
+ {
+ if (!info->keep_memory)
+ free (local_syms);
+ else
+ symtab_hdr->contents = (unsigned char *) local_syms;
+ }
+ }
+
return TRUE;
}
goto copy_reloc;
}
break;
+
+ case R_PPC64_GOT16_DS:
+ from = TOCstart + htab->sec_info[input_section->id].toc_off;
+ if (relocation + addend - from + 0x8000 < 0x10000
+ && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
+ {
+ insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
+ if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
+ {
+ insn += (14u << 26) - (58u << 26);
+ bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
+ r_type = R_PPC64_TOC16;
+ rel->r_info = ELF64_R_INFO (r_symndx, r_type);
+ }
+ }
+ break;
+
+ case R_PPC64_GOT16_LO_DS:
+ case R_PPC64_GOT16_HA:
+ from = TOCstart + htab->sec_info[input_section->id].toc_off;
+ if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
+ && SYMBOL_REFERENCES_LOCAL (info, &h->elf))
+ {
+ insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
+ if ((insn & (0x3f << 26 | 0x3)) == 58u << 26 /* ld */)
+ {
+ insn += (14u << 26) - (58u << 26);
+ bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
+ r_type = R_PPC64_TOC16_LO;
+ rel->r_info = ELF64_R_INFO (r_symndx, r_type);
+ }
+ else if ((insn & (0x3f << 26)) == 15u << 26 /* addis */)
+ {
+ r_type = R_PPC64_TOC16_HA;
+ rel->r_info = ELF64_R_INFO (r_symndx, r_type);
+ }
+ }
+ break;
}
/* Set `addend'. */
+2019-04-30 Alan Modra <amodra@gmail.com>
+
+ * testsuite/ld-powerpc/elfv2exe.d: Update.
+ * testsuite/ld-powerpc/elfv2so.d: Update.
+ * testsuite/ld-powerpc/tocopt.d: Update.
+ * testsuite/ld-powerpc/tocopt.s: Update.
+ * testsuite/ld-powerpc/tocopt5.d: Update.
+ * testsuite/ld-powerpc/tocopt5.s: Update.
+ * testsuite/ld-powerpc/tocopt7.d: Update.
+ * testsuite/ld-powerpc/tocopt7.s: Update.
+ * testsuite/ld-powerpc/tocopt8.d: Update.
+ * testsuite/ld-powerpc/tocopt8.s: Update.
+
2019-04-30 Alan Modra <amodra@gmail.com>
* ld.texi (How GNU properties are merged): Avoid pod2man error.
.*: (e8 62 80 08|08 80 62 e8) ld r3,-32760\(r2\)
.*: (4b .. .. ..|.. .. .. 4b) bl .*\.plt_branch\.f2>
.*: (60 00 00 00|00 00 00 60) nop
-.*: (e8 62 80 10|10 80 62 e8) ld r3,-32752\(r2\)
+.*: (38 62 80 10|10 80 62 38) addi r3,r2,-32752
.*: (48 .. .. ..|.. .. .. 48) bl 10008888 <f3>
.*: (60 00 00 00|00 00 00 60) nop
.*: (4b .. .. ..|.. .. .. 4b) bl .*\.plt_branch\.f4>
.* <.*\.plt_call\.f4>:
.*: (f8 41 00 18|18 00 41 f8) std r2,24\(r1\)
-.*: (e9 82 80 40|40 80 82 e9) ld r12,-32704\(r2\)
+.*: (e9 82 80 38|38 80 82 e9) ld r12,-32712\(r2\)
.*: (7d 89 03 a6|a6 03 89 7d) mtctr r12
.*: (4e 80 04 20|20 04 80 4e) bctr
\.\.\.
.* <.*\.plt_call\.f3>:
.*: (f8 41 00 18|18 00 41 f8) std r2,24\(r1\)
-.*: (e9 82 80 30|30 80 82 e9) ld r12,-32720\(r2\)
+.*: (e9 82 80 28|28 80 82 e9) ld r12,-32728\(r2\)
.*: (7d 89 03 a6|a6 03 89 7d) mtctr r12
.*: (4e 80 04 20|20 04 80 4e) bctr
\.\.\.
.* <.*\.plt_call\.f5>:
.*: (f8 41 00 18|18 00 41 f8) std r2,24\(r1\)
-.*: (e9 82 80 28|28 80 82 e9) ld r12,-32728\(r2\)
+.*: (e9 82 80 20|20 80 82 e9) ld r12,-32736\(r2\)
.*: (7d 89 03 a6|a6 03 89 7d) mtctr r12
.*: (4e 80 04 20|20 04 80 4e) bctr
\.\.\.
.* <.*\.plt_call\.f1>:
.*: (f8 41 00 18|18 00 41 f8) std r2,24\(r1\)
-.*: (e9 82 80 48|48 80 82 e9) ld r12,-32696\(r2\)
+.*: (e9 82 80 40|40 80 82 e9) ld r12,-32704\(r2\)
.*: (7d 89 03 a6|a6 03 89 7d) mtctr r12
.*: (4e 80 04 20|20 04 80 4e) bctr
\.\.\.
.* <.*\.plt_call\.f2>:
.*: (f8 41 00 18|18 00 41 f8) std r2,24\(r1\)
-.*: (e9 82 80 38|38 80 82 e9) ld r12,-32712\(r2\)
+.*: (e9 82 80 30|30 80 82 e9) ld r12,-32720\(r2\)
.*: (7d 89 03 a6|a6 03 89 7d) mtctr r12
.*: (4e 80 04 20|20 04 80 4e) bctr
\.\.\.
.*: (e8 62 80 08|08 80 62 e8) ld r3,-32760\(r2\)
.*: (4b .. .. ..|.. .. .. 4b) bl .*\.plt_call\.f2>
.*: (e8 41 00 18|18 00 41 e8) ld r2,24\(r1\)
-.*: (e8 62 80 10|10 80 62 e8) ld r3,-32752\(r2\)
+.*: (38 62 80 48|48 80 62 38) addi r3,r2,-32696
.*: (4b .. .. ..|.. .. .. 4b) bl .*\.plt_call\.f3>
.*: (e8 41 00 18|18 00 41 e8) ld r2,24\(r1\)
.*: (4b .. .. ..|.. .. .. 4b) bl .*\.plt_call\.f4>
.*: file format .*
Contents of section \.text:
- 100000b0 (3d220000|0000223d) (e9298018|188029e9) (3c820000|0000823c) (38a48020|2080a438) .*
- 100000c0 (e8c50000|0000c5e8) (3fa00000|0000a03f) (3bbd8028|2880bd3b) (7c62e82a|2ae8627c) .*
- 100000d0 (3d220000|0000223d) (39298033|33802939) (3c820000|0000823c) (38a48008|0880a438) .*
+ 100000b0 (3d220000|0000223d) (39298028|28802939) (3c820000|0000823c) (38a48018|1880a438) .*
+ 100000c0 (e8c50000|0000c5e8) (3fa00000|0000a03f) (3bbd8020|2080bd3b) (7c62e82a|2ae8627c) .*
+ 100000d0 (3d220000|0000223d) (3929802b|2b802939) (3c820000|0000823c) (38a48008|0880a438) .*
100000e0 (e8c50000|0000c5e8) (3fa00000|0000a03f) (3bbd8010|1080bd3b) (7c62e82a|2ae8627c) .*
Contents of section \.got:
- 10010100 (00000000|00810110) (10018100|00000000) (00000000|34010110) (10010134|00000000) .*
- 10010110 (00000000|35010110) (10010135|00000000) (00000000|30010110) (10010130|00000000) .*
- 10010120 (00000000|31010110) (10010131|00000000) (00000000|32010110) (10010132|00000000) .*
+ 10010100 (00000000|00810110) (10018100|00000000) (00000000|2c010110) (1001012c|00000000) .*
+ 10010110 (00000000|2d010110) (1001012d|00000000) (00000000|29010110) (10010129|00000000) .*
+ 10010120 (00000000|2a010110) (1001012a|00000000) .*
Contents of section \.sdata:
- 10010130 01020304 0506 .*
+ 10010128 01020304 0506 .*
.text
_start:
# no need for got entry, optimise to nop,addi
-# note: ld doesn't yet do got optimisation, so we get nop,ld
+# note that due to unexpected toc/got insns the full optimisation is
+# not done. see tocopt5 for test without unexpected insns.
addis 9,2,x1@got@ha
ld 9,x1@got@l(9)
# must keep got entry, optimise to nop,addi,ld
.*: file format .*
Contents of section \.text:
- 100000b0 (60000000|00000060) (e9228018|188022e9) (60000000|00000060) (38a28020|2080a238) .*
- 100000c0 (e8c50000|0000c5e8) (60000000|00000060) (3922802b|2b802239) (60000000|00000060) .*
+ 100000b0 (60000000|00000060) (39228020|20802239) (60000000|00000060) (38a28018|1880a238) .*
+ 100000c0 (e8c50000|0000c5e8) (60000000|00000060) (39228023|23802239) (60000000|00000060) .*
100000d0 (38a28008|0880a238) (e8c50000|0000c5e8) .*
Contents of section \.got:
- 10010100 (00000000|00810110) (10018100|00000000) (00000000|2c010110) (1001012c|00000000) .*
- 10010110 (00000000|2d010110) (1001012d|00000000) (00000000|28010110) (10010128|00000000) .*
- 10010120 (00000000|29010110) (10010129|00000000) .*
+ 10010100 (00000000|00810110) (10018100|00000000) (00000000|24010110) (10010124|00000000) .*
+ 10010110 (00000000|25010110) (10010125|00000000) (00000000|21010110) (10010121|00000000) .*
Contents of section \.sdata:
- 10010128 01020304 0506 .*
+ 10010120 01020304 0506 .*
.text
_start:
# no need for got entry, optimise to nop,addi
-# note: ld doesn't yet do got optimisation, so we get nop,ld
addis 9,2,x1@got@ha
ld 9,x1@got@l(9)
# must keep got entry, optimise to nop,addi,ld
.*: file format .*
Contents of section \.text:
- 100000b0 (0000223d|3d220000) (288029e9|e9298028) (0000823c|3c820000) (3080a438|38a48030) .*
+ 100000b0 (0000223d|3d220000) (40802939|39298040) (0000823c|3c820000) (3080a438|38a48030) .*
100000c0 (0000c5e8|e8c50000) (0000823f|3f820000) (29803ce9|e93c8029) (0000223d|3d220000) .*
100000d0 (108029e9|e9298010) (0000823c|3c820000) (1880a438|38a48018) (0000c5e8|e8c50000) .*
100000e0 (0000823f|3f820000) (11803ce9|e93c8011) (0000823c|3c820000) (3080a438|38a48030) .*
.text
_start:
# no need for got entry, optimise to nop,addi
-# note: ld doesn't yet do got optimisation, so we get nop,ld
addis 9,2,x1@got@ha
ld 9,x1@got@l(9)
# must keep got entry, optimise to nop,addi,ld
.*: file format .*
Contents of section \.text:
- 100000b0 (00000060|60000000) (108022e9|e9228010) (00000060|60000000) (1880a238|38a28018) .*
+ 100000b0 (00000060|60000000) (20802239|39228020) (00000060|60000000) (1080a238|38a28010) .*
100000c0 (0000c5e8|e8c50000) (00000060|60000000) (38802239|39228038) (00000060|60000000) .*
100000d0 (0880a238|38a28008) (0000c5e8|e8c50000) (00000060|60000000) (088022e9|e9228008) .*
- 100000e0 (00000060|60000000) (1880a238|38a28018) (0000c580|80c50000) (00000060|60000000) .*
+ 100000e0 (00000060|60000000) (1080a238|38a28010) (0000c580|80c50000) (00000060|60000000) .*
100000f0 (20802281|81228020) (00000060|60000000) (0880a238|38a28008) (0000c580|80c50000) .*
- 10000100 (00000060|60000000) (1880a238|38a28018) (0200c5e8|e8c50002) (00000060|60000000) .*
+ 10000100 (00000060|60000000) (1080a238|38a28010) (0200c5e8|e8c50002) (00000060|60000000) .*
10000110 (228022e9|e9228022) (00000060|60000000) (0880a238|38a28008) (0200c5e8|e8c50002) .*
- 10000120 (00000060|60000000) (1880a238|38a28018) (0000c5a0|a0c50000) (00000060|60000000) .*
+ 10000120 (00000060|60000000) (1080a238|38a28010) (0000c5a0|a0c50000) (00000060|60000000) .*
10000130 (208022a1|a1228020) (00000060|60000000) (0880a238|38a28008) (0000c5a0|a0c50000) .*
- 10000140 (00000060|60000000) (1880a238|38a28018) (0000c5a8|a8c50000) (00000060|60000000) .*
+ 10000140 (00000060|60000000) (1080a238|38a28010) (0000c5a8|a8c50000) (00000060|60000000) .*
10000150 (208022a9|a9228020) (00000060|60000000) (0880a238|38a28008) (0000c5a8|a8c50000) .*
- 10000160 (00000060|60000000) (1880a238|38a28018) (0000c588|88c50000) (00000060|60000000) .*
+ 10000160 (00000060|60000000) (1080a238|38a28010) (0000c588|88c50000) (00000060|60000000) .*
10000170 (20802289|89228020) (00000060|60000000) (0880a238|38a28008) (0000c588|88c50000) .*
- 10000180 (00000060|60000000) (1880a238|38a28018) (0000c5c0|c0c50000) (00000060|60000000) .*
+ 10000180 (00000060|60000000) (1080a238|38a28010) (0000c5c0|c0c50000) (00000060|60000000) .*
10000190 (208022c1|c1228020) (00000060|60000000) (0880a238|38a28008) (0000c5c0|c0c50000) .*
- 100001a0 (00000060|60000000) (1880a238|38a28018) (0000c5c8|c8c50000) (00000060|60000000) .*
+ 100001a0 (00000060|60000000) (1080a238|38a28010) (0000c5c8|c8c50000) (00000060|60000000) .*
100001b0 (208022c9|c9228020) (00000060|60000000) (0880a238|38a28008) (0000c5c8|c8c50000) .*
- 100001c0 (00000060|60000000) (1880a238|38a28018) (0100c5f4|f4c50001) (00000060|60000000) .*
+ 100001c0 (00000060|60000000) (1080a238|38a28010) (0100c5f4|f4c50001) (00000060|60000000) .*
100001d0 (218022f5|f5228021) (00000060|60000000) (2080a238|38a28020) (0100c5f4|f4c50001) .*
- 100001e0 (00000060|60000000) (1880a238|38a28018) (0200c5e4|e4c50002) (00000060|60000000) .*
+ 100001e0 (00000060|60000000) (1080a238|38a28010) (0200c5e4|e4c50002) (00000060|60000000) .*
100001f0 (228022e5|e5228022) (00000060|60000000) (0880a238|38a28008) (0200c5e4|e4c50002) .*
- 10000200 (00000060|60000000) (1880a238|38a28018) (0300c5e4|e4c50003) (00000060|60000000) .*
+ 10000200 (00000060|60000000) (1080a238|38a28010) (0300c5e4|e4c50003) (00000060|60000000) .*
10000210 (238022e5|e5228023) (00000060|60000000) (0880a238|38a28008) (0300c5e4|e4c50003) .*
10000220 (00000060|60000000) (208022f9|f9228020) (00000060|60000000) (0880a238|38a28008) .*
10000230 (0000c5f8|f8c50000) (00000060|60000000) (20802291|91228020) (00000060|60000000) .*
100002d0 (0300c5f4|f4c50003) .*
Contents of section \.got:
10010300 (00830110|00000000) (00000000|10018300) (40030110|00000000) (00000000|10010340) .*
- 10010310 (20030110|00000000) (00000000|10010320) (28030110|00000000) (00000000|10010328) .*
+ 10010310 (28030110|00000000) (00000000|10010328) .*
Contents of section \.sdata:
10010320 (01000000|00000000) (00000000|00000001) (02000000|00000000) (00000000|00000002) .*
10010330 (03000000|00000000) (00000000|00000003) (04000000|00000000) (00000000|00000004) .*
.text
_start:
# no need for got entry, optimise to nop,addi
-# note: ld doesn't yet do got optimisation, so we get nop,ld
addis 9,2,x1@got@ha
ld 9,x1@got@l(9)
# must keep got entry, optimise to nop,addi,ld