As interpreted by the Gemara, the passage in Kohelet (2:8) describes how King Solomon tells of the pomp and ceremony that went on in his realm – there were singers with musical instruments, pools and bathhouses and – according to the Sages in Israel – great chariots. The Babylonian Sages interpreted the end of the pasuk as referring to demons, which is understood by the Gemara as being essential for the building of Solomon’s Temple. In I (6:7) we learn that the entire beit ha-mikdash was built out of full stones that had not been split by normal means, using metal implements. Rather, it was a miraculous shamir (שמיר – a worm that could cut through anything) that was able to divide the stone.
When King Solomon searched for the shamir he captured a male and female demon in an attempt to get them to inform him of its location. They referred him to Ashmadai, king of the demons. The Gemara describes how King Solomon sent Benayahu ben Yehoyadah who tracked down Ashmadai, who informed him that the shamir had been entrusted to the tarnegola bara or the “wild rooster” – the bird that we know as a hoopoe and is the national bird of Israel. Benayahu covered a hoopoe nest with glass and when the mother came to feed her young, finding the nest covered she dropped the shamir on the glass to open it. This was Benayahu’s opportunity to secure the shamir for use in building the Temple.
Obviously stories like this one are difficult to accept at face value, and in Sefer Pardes Rimonim we find that that Rabbi Shem-Tov ibn Shaprut offers an explanation that views this story as allegory, and that the various demons, birds, etc. all refer to philosophers and sages who spent their time in seclusion in the desert contemplating matters of secrets and mysticism.