R_MIPS_PC16 relocation handling, a proposal

Maciej W. Rozycki macro@ds2.pg.gda.pl
Thu Oct 14 08:59:00 GMT 1999

On Wed, 13 Oct 1999, Geoff Keating wrote:

> I have already implemented this as a GNU-specific extension which does
> include the shift, as part of more general work to make embedded-pic
> work on MIPS.  I had to add other relocs anyway, so there was no
> benefit on trying to be tricky for just this one.

 You refer to the code in ECOFF backend, don't you?  Or some private work?

> I'm pretty sure your patch wouldn't work with the current assembler.

 Have you actually checked?  I've optimized the patch against a CVS
snapshot dated Oct 8, 1999.  Pretty current, I suppose.

> My testcase looked like:
> # Check GNU-specific embedded relocs, for ELF.
> 	.text
> 	.set noreorder
> 	nop
> l2:	jal	g1		# R_MIPS_GNU_REL16_S2	g1   -1
> 	nop
> 	b	g2		# R_MIPS_GNU_REL16_S2	g2   -1
> 	nop
> 	b	g2		# R_MIPS_GNU_REL16_S2	g2   -1
> 	nop
> 	jal	l1		# R_MIPS_GNU_REL16_S2	.foo 4
> 	nop
> 	jal	l2		# -9
> 	nop
> 	b	l1+8		# R_MIPS_GNU_REL16_S2	.foo C
> 	nop
> l3:	
> 	b	l2		# -D
> 	nop
> 	.section ".foo","ax",@progbits
> 	nop
> l1:
> 	nop
> This is a cut-down version, I hope I've updated the comments
> correctly; the numbers are the addend as stored in the file.

 You'd better show the result of `objdump -Sr' -- this way the exact
opcodes generated would be shown.  Actually jal is a jump and not a
branch and requires a R_MIPS_26 reloc.  It was probably converted to "bal"
due to "-KPIC" option for as.

 Anyway, pointing back to the opcode is the only right way for PC-relative
relocs that cannot cover the whole address space.  So "-1" is OK and with
my patch I get exactly the same result.

> The assembler was doing the pcrel handling wrong.  IMHO, the
> assembler's generic pcrel handling is broken, or at least very
> suboptimal for ELF, but it's possible to work around it.

 Yes, there are problems.  I've been dealing with R_386_PC16 and R_386_PC8
relocs some time ago (see the ChangeLog) and there were some nasties but
it can be done right with some caution.  Basically I had to repeat the
same steps for MIPS this time.

> I will be submitting a patch for this work Real Soon Now.

 Fine.  I certainly do not insist on my version but I'd like to see this
issue resolved.


+  Maciej W. Rozycki, Technical University of Gdansk, Poland   +
+        e-mail: macro@ds2.pg.gda.pl, PGP key available        +

More information about the Binutils mailing list