I recently discovered that ThinLTO native objects links sometimes
occur in a different order than the objects were selected out of
static libraries by the native linker. The ThinLTO ModuleMap
is built (e.g. for gold-plugin) in the original order the objects are
listed in the library. However, the linker may select for inclusion
a later object in a static library first, based on the strong references
it satisfies. It's copies of weak symbols may then be prevailing instead
of an earlier object in the same library. However, the native objects
are linked in the order they were added to the ModuleMap, which would
result in the wrong copy of a weak function being selected in the final
native link for ThinLTO, resulting in incorrect behavior.
Since the linker doesn't provide the native object ordering to the
plugin, we can deduce the correct ordering among modules defining
the same weak symbol based on the prevailing symbols
in the combined index. With this patch we use a stable topological
sort to sort the ModuleMap based on the partial ordering provided by
the combined index.
That assuming that it is possible, i.e. that a linker will always ask for the first symbol to be the prevailing one (if it is not the case you may not be able to generate a topological sort)