We give formulae for the Ozsváth-Szabó invariants of 4-manifolds X obtained by fiber sum of two manifolds M 1 , M 2 along surfaces † 1 , † 2 having trivial normal bundle and genus g 1. The formulae follow from a general theorem on the Ozsváth-Szabó invariants of the result of gluing two 4-manifolds along a common boundary, which is phrased in terms of relative invariants of the pieces. These relative invariants take values in a version of Heegaard Floer homology with coefficients in modules over certain Novikov rings; the fiber sum formula follows from the theorem that this "perturbed" version of Heegaard Floer theory recovers the usual Ozsváth-Szabó invariants, when the 4-manifold in question has b 57R58; 57M99