The rapper’s dense, blistering lyrics have courted controversy in the past, with some accusing him of writing homophobic and misogynistic verses.

And this is not the first time that Eminem has tackled politics in his songs: He has also taken aim at Bill Clinton, Hillary Clinton and George W. Bush. Last October, he released the freestyle track “Campaign Speech,” in which he called Mr. Trump “a loose cannon who’s blunt with his hand on the button.”

Eminem is thought to be gearing up for the release this year of his eighth major-label album and first since “The Marshall Mathers LP 2” in 2013. (A longtime producer for the rapper said earlier this month that the new music was “done.”) In the last few years, he has popped up for occasional guest verses and soundtrack songs — he is featured on Pink’s “Revenge,” out Friday — but has largely remained in the shadows, away from celebrity and social media.

“Sometimes I think that if I get comfortable or set in my ways of doing something, maybe I should step back for a minute and figure out how to mix it up a little bit,” he told The New York Times in 2015.

Other hip-hop artists and rappers have also targeted the president in their lyrics and music videos. The California rapper YG released a single last August called “FDT,” in which he repeatedly curses Mr. Trump’s name. Kendrick Lamar lashed out at Mr. Trump in his tracks “The Heart Part 4” and “XXX.” Snoop Dogg’s music video “Lavender,” in which the rapper aimed a toy pistol at a clown resembling Mr. Trump, earned a rebuke and a call for “jail time!” from the president himself in March.