Hacker News new | past | comments | ask | show | jobs | submit login

How exactly is the ordering of items across the section decided? Is it relatively random or is it based on the order that the object files are passed to the linker?



It's unspecified, and I've seen it change over time as the linker changes it's data structures.


This is true, to my knowledge, I would NOT relie on the order to be always consistent. But I think you can enforce the order in the linker file, i.e. load the arrays one after the other, and then the order should keep constant. But you still have several places that you need to keep synchronized and, really, most people don't look in the linker file at all.


You're right, you can manually specify the ordering of object files in the linker file. Though, like you say, this is rarely done because of how brittle it is.

I was referring more to the way shown above, where I've seen a linker change the list of object files from a sorted list of filenames, to a hash table of filenames, which obviously changed the ordering with which it iterated across all object files.


You can sort the array during program initialization, if you care about the order.


It's probably already in link order. If you have to know the names of the symbols in code to be able to sort them, it sort of defeats the purpose of the scheme in the first place.


You might want to sort them by some other criteria.

For example, the FreeBSD kernel's use of this construct embeds a well-defined priority[1], and the array is sorted by this priority value at boot time.[2]

[1]: https://github.com/freebsd/freebsd/blob/master/sys/sys/kerne...

[2]: https://github.com/freebsd/freebsd/blob/master/sys/kern/init...


Assume it's random. See also the static initialization order ‘fiasco’: https://isocpp.org/wiki/faq/ctors#static-init-order




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: