Essential oils come from just about all the different plant parts you can think of. Flowers for Neroli, Rose, Jasmine and others. Leaves/twigs for Petigrain, Melissa, the Mints, Bay and many many others. Lemon, orange, grapefruit etc. the rind of the fruit. Sandalwood the heartwood of the tree, Vetiver and some others the root of the plant and Benzoin, Frankincense and a few others the resin that oozes out of the wood.
Much as we might like to think so, these oils and their aromas are not produced purely for our benefit so that we can smell them and treat ourselves with them!
However, the evolutionary purpose of some is to be attractive, just not for us! The oils that come from flowers are there to attract insects and the time of day that the scent is mainly produced tells us something about which insects. Evening primrose, night scented stocks and others that produce their scent mainly at night are pollinated by moths. Other day time smelling flowers attract bees of differing kinds.
Most of the essential oil bearing roots are there for the opposite effect. Despite the fact that we might like them either to eat or for use in perfumes etc. are their to stop the roots being eaten. (In the case of Horse Radish by all except perverse species like humans!) The same is true of the oils contained in the foliage of many plants though in some cases there are still specialised pests that have evolved in such a way that they can ignore the toxins of the plant.
Some plants are even more clever and use their scent to attract species that prey on their pest species!
However while the oil of Sandalwood can be used to keep away some insects I have not been able to track down why the oil is most concentrated in the heartwood.