Bug 24676 - [RISCV] Redundant R_RISCV_DTPMOD* R_RISCV_DTPREL* resulted from Global Dynamic -> Local Exec relaxation
Summary: [RISCV] Redundant R_RISCV_DTPMOD* R_RISCV_DTPREL* resulted from Global Dynami...
Status: NEW
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.33
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on: 24673
Blocks:
  Show dependency treegraph
 
Reported: 2019-06-13 07:02 UTC by Fangrui Song
Modified: 2020-04-20 22:26 UTC (History)
2 users (show)

See Also:
Host:
Target: riscv*-*-*
Build:
Last reconfirmed: 2019-06-13 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fangrui Song 2019-06-13 07:02:16 UTC
RISC-V psABI doesn't specify TLS relaxation. Though the code sequences are not
relaxed, dynamic relocations should be omitted for GD->LE relaxation.

% cat a.s
la.tls.gd a0,a
call __tls_get_addr@plt

la.tls.gd a0,b
call __tls_get_addr@plt

.section .tbss
.globl a
a:
.zero 4

% cat b.s
.section .tbss
.globl b
b:
.zero 4


riscv-gcc -fuse-ld=bfd-nostdlib b.s -shared -o b.so
riscv-gcc -fuse-ld=bfd -nostdlib a.s b.so -o a -Wl,--defsym=__tls_get_addr=0
readelf -Wr a

## DTPMOD32/DTPREL32 of a are redundant because a is non-preemptable
0001122c  00000106 R_RISCV_TLS_DTPMOD32   00000000   b + 0
00011230  00000108 R_RISCV_TLS_DTPREL32   00000000   b + 0
00011234  00000206 R_RISCV_TLS_DTPMOD32   00000000   a + 0
00011238  00000208 R_RISCV_TLS_DTPREL32   00000000   a + 0
Comment 1 Jim Wilson 2019-06-13 20:43:21 UTC
Assuming that the MIPS port is handling this right, then the completely untested fix is as follows

diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c
index 69cadaa..ac55563 100644
--- a/bfd/elfnn-riscv.c
+++ b/bfd/elfnn-riscv.c
@@ -2176,13 +2176,14 @@ riscv_elf_relocate_section (bfd *output_bfd,
 		  pic = bfd_link_pic (info);
 
 		  if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, pic, h)
-		      && (!pic || !SYMBOL_REFERENCES_LOCAL (info, h)))
+		      && (bfd_link_dll (info)
+			  || !SYMBOL_REFERENCES_LOCAL (info, h)))
 		    indx = h->dynindx;
 		}
 
 	      /* The GOT entries have not been initialized yet.  Do it
 		 now, and emit any relocations.  */
-	      if ((bfd_link_pic (info) || indx != 0)
+	      if ((bfd_link_dll (info) || indx != 0)
 		  && (h == NULL
 		      || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
 		      || h->root.type != bfd_link_hash_undefweak))

Note that this necessarily includes the fix for bug 24673.  With this fix, I now get
0000000000000000  0000000000000000 R_RISCV_NONE                              0
0000000000000000  0000000000000000 R_RISCV_NONE                              0
0000000000012018  0000000100000007 R_RISCV_TLS_DTPMOD64   0000000000000000 b + 0
0000000000012020  0000000100000009 R_RISCV_TLS_DTPREL64   0000000000000000 b + 0

Also note as in bug 24673 we preallocated space for the TLS relocs for a, which is why we now have R_RISCV_NONE relocs.
Comment 2 Khem Raj 2020-04-20 22:26:54 UTC
I am seeing bunch of packages which are showing the R_RISCV_NONE issue

For detail list see the do_package_qa failures here [1]

[1] http://errors.yoctoproject.org/Errors/Build/101576/