Running clinical trials is ridiculously expensive, and most clinical trials don't result in a drug successfully going to market, which means successful drugs need to essentially subsidize the cost of (1) their own clinical trials and (2) all those other unsuccessful trials, to boot. So that hypothetical $40/yr sales price turns a profit if you account for the cost of materials and manufacturing, but not if you also account for the cost of the Very Expensive clinical trials that had to occur to get here.
Now, that said... is $42,000/yr a reasonable price? Fuck no. On a purely humanitarian level, it's unacceptable, and if we're just looking at Gilead's bottom line, I'm sure they could price this drug for a whole lot less and still recoup the cost of clinical trials (and then some). Gilead is certainly not hurting for profit here.
But even if the true "break-even cost" is closer to $4,000/yr, let's say, that's still way too expensive for most people in most parts of the world. Which is where I'd get on my soapbox about single-payer healthcare and how this research should be funded by tax dollars, but my legs are too tired :P