This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix MIPS symbol difference calculation


When targetting MIPS, gas can miscalculate the difference between two symbols when used in %hi relocation operators.

e.g.
	.text
foo:
	li	$2, %hi(LAB3-LAB1)
LAB1:
	addiu	$2, %lo(LAB3-LAB1)
LAB2:
	nop
	nop
	nop
	nop
LAB3:
	.fill 32760
	li	$2, %hi(LAB2-LAB4)
LAB4:
	addiu	$2, %lo(LAB2-LAB4)

The problem occurs because fixups are created for all the relocation operators except %lo(LAB2-LAB4), because both LAB2 and LAB4 have been encountered by the time it is processed.

adjust_reloc_syms() in write.c changes these relocs to be relative to .text, so the second and third relocs effectively become:

%lo(.text-LAB1+0x18)
%hi(.text-LAB4+0x8)

mips_frob_file() in tc-mips.c then mistakenly pairs these two together when looking for a corresponding %lo for the %hi, because there is no corresponding %lo(LAB2-LAB4) fixup, and the earlier %lo appears to be an offset from the same symbol .text (ignoring the subtracted symbol). mips_frob_file() then overwrites the offset of the %hi with that of the %lo, so it becomes:

%hi(.text-LAB4+0x18)

This corrupts the offset, so fixup_segment() later ends up calculating the wrong value for this %hi.

This patch works around this by preventing %hi expressions from being processed by mips_frob_file() if it contains a subtracted symbol. This should be safe as the relocation-pairing is only necessary if the relocations actually make it into the output object file, but GAS cannot express the difference between two symbols as a relocation on MIPS, so any such fixups that are not fully resolvable will result in an error anyway.

fixup_has_matching_lo_p() is also modified to check the subtracted symbol to avoid mispairings of remaining %hi relocations with %lo relocations containing a subtracted symbol.

I have also added the above example as a DejaGnu test.

Kwok

Attachment: mips_hi_subsy.patch
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]