The Sun's chemical composition was gotten from the interstellar medium. The hydrogen and most of the helium in the Sun would have been produced by Big Bang nucleosynthesis in the first 20 minutes of the universe. The heavier elements were produced by stars that died before the Sun was formed. The heavier elements were released into the interstellar medium when the star exploded as a supernovae.