Joab was a threat to Solomon's authority and the continued dynasty of the house of David.
In Joab's establishing his loyalty, David could not ethically eliminate Joab by killing him. Joab's loyalty was disingenuous. He acted strategically to prove David wrong, actions which appear to be mistakes but are not. But those strategic moves tied David down in the public opinion of Israel that made it impossible to ethically get rid of Joab.
David saw thro Joab's strategy.
However, Joab would have seen that his loyalty should be rewarded feudalistically. Joab's agenda would divide Israel and allow Joab to establish a fiefdom for his descendants, threatening the tribal allocation and unity of Israel. Perhaps, Joab wanted to establish his own dynasty to replace that of David's.
Why David could not eliminate Joab himself
There are two possibilities concerning the role of Joab.
He was a politically ambitious disingenuous hero.
Solomon was not beholden to Israeli public opinion and therefore had the freehand to eliminate Joab.
He was a prophet with a task to accomplish, and everything Joab did was for the preservation of the house of Israel. Joab never made a mistake. It was David who made mistakes.
OTOH, David recognized the Divine purpose of Joab and therefore was constrained from killing Joab. But Solomon was outside the plan the LORD had for Israel.
Joab was an agent to terminate the house of David
Joab could have been an agent of Divine purposes to terminate the house of David to preempt all the problems that the house of David subsequent rulers would cause. Just as Jacob defeated the Divine wrestler, so did David wrestled and won but consequently hurt the future of Israel.
Joab was to restore the representative Democracy advocated by Jethro to Moses.
The hand of the LORD was on David. The LORD would not let David build the temple because He did not want that temple. Building the 1st temple would contaminate and dilute the legacy of David and render meaningless all the blood that David had shed. That is, rather than the blood on David's hands contaminating the temple, building the temple would contaminate the David's having shed blood to establish the unity of Israel. The only Temple to be built was the one blueprinted in Ezekiel.
But Solomon was outside the plan of the LORD and the by-product of an illegitimate relation, so he was unconstrained to build the temple or eliminate Joab. Like Eve, David saw the leeway of being given a choice and ate of the fruit. His choice of continuing his personal dynasty brought travails, trials and tribulation to the house of Israel for thousands of years.
The futility of having a king
Recalling 1Samuel chap 8, When Israel desired for a king "like other nations", Samuel warned and urged Israel not to have a king. He warned Israel of all the bad things that a King would do to Israel.
Joab was supposed to terminate the dynasty of David and his task abrogated by David's advice to Solomon.
The futility of having a messiah
Unfortunately, for more than 2000 years, we did not pay attention to the significance of the prophecy laid out by Samuel in 1Samuel 8. No one takes this prophecy seriously. But so far its consequences have been disastrous.
For thousands of years, Jews had desired for an idolistic annointed-king dictator, just "like the other peoples". Fulfillment of the Sabbath is the only contract, interface and path to salvation - not a messiah.
So, as Samuel was consoled by the LORD, "the people have not rejected you, but they have rejected Me" in choosing to have a King.
Instead of choosing representative Democracy, we clamour for a king and messiah.
Just look back at history and see all the divisiveness, disasters and massacres upon the Jewish people every time someone claims to be the messiah-king, just as David made a bad choice of continuing his personal dynasty by having Joab killed.