Subject: MIPS PIC vs. profiling, what am I missing?
To: None <port-mips@netbsd.org>
From: Rafal Boni <rafal@mediaone.net>
List: port-mips
Date: 11/12/2001 15:09:56
Folks:
	The curent chapter in the series of "Rafal beats his head against the
	new toolchain" is titled "Why the hell do executables built with -pg
	all dump core with the new toolchain".

	Here's what happens:

		(1) A function that calls _mcount and uses .cprestore looks
		    like the following in macroized assembler:

			.globl	function
		    function:
			.cpload $25
			addiu $sp, $sp, -40
			.cprestore 36
			...
			sw $gp, 36($sp)
			...
			<load $t9 with address of _mcount>
			jalr $t9	    # _mcount
			subu $sp, $sp, 8    # _mcount pops 2 works from stack


		(2) This in turn expands the `jalr' bit into:

			jalr	$t9	    # _mcount
			nop			
			lw	$gp, 36($sp)
			subu 	$sp, $sp, 8

		Note that due to the way the .cprestore places the load of 
		$gp after the jump (but before the cleanup of $sp), the code
		gets the wrong value of $gp.

	It seems to me that the two (PIC code and _mcount-based profiling) are
	inherently incompatible as implemented.  I haven't yet gone digging
	as to whether (or why, if it does work) the old toolchain worked in
	this respect, but that's my next stop.

--rafal

----
Rafal Boni                                                   rafal@mediaone.net