Energy companies marched higher along with an upturn in the price of crude oil. Exxon Mobil rose 2.2 percent.
Stocks got the year off to a lousy start with the Dow Jones Industrial Average plunging almost 400 points in the first minutes of trading. The market clawed back the ground it lost and ended with modest gains.
The S&P 500 edged up 3 points, or 0.2 percent, to 2,510.
The Dow rose 18 points, or 0.1 percent, to 23,346. The Nasdaq rose 30 points, or 0.5 percent, to 6,665.