Metabolic mechanisms for the evolution of stable symbiosis