COMDAT sections (or equivalent for ELF...)
Posted: Mon Oct 13, 2014 3:22 pm
I am in the process of developing an ahead-of-time compiler for the CLR. I am running into an issue with how to deal with generics (essentially the CLR version of C++ templates). Whilst I can compile them (methods and vtables/typeinfos) adjusting them for the relevant generic parameter, my problem comes with where to emit them in the output file (I currently use ELF32/64 as output file formats).
Take the following example: a generic type MyType<T> is defined in assembly A. It has a single method foo(). MyType is instantiated in A as MyType<int> and in assembly B also as MyType<int>. Thus the object files produced for both A and B both contain the method MyType<int>.foo(). Unfortunately, when I then come to link them together I get linker errors as the same label is used in both object files. I could get around this by prefixing each label with the object file it is instantiated in (e.g. A_MyType<int>.foo() ) but then I still get an issue with typeinfo structures as my type comparison algorithm (e.g. for casting or interface methods) relies on a direct comparison on the addresses of typeinfo objects (e.g. typeof(MyType<int>) called from A would not equal typeof(MyType<int>) called from B).
I understand this is accomplished in Microsoft's compiler suite for C++ templates by the use of COMDAT sections where the template method/typeinfo instantiation is emitted in a special COMDAT section, where similar ones are then merged together at link time. I further understand from a quick web search that this mechanism has been extended to ELF in gcc/llvm (as there are various patch proposals etc) but I cannot find anything particularly documenting the actual standards used here if any. In particular, it is not mentioned in the ELF docs (elf32/elf64/i386 processor supplement/amd64 processor supplement) but is mentioned in a HP-UX supplement which uses the special section type SHT_HP_COMDAT.
Does anyone have any further information on the implementation of COMDAT sections in ELF? Is the HP-UX way the defacto standard?
Many thanks in advance.
Regards,
John.
Take the following example: a generic type MyType<T> is defined in assembly A. It has a single method foo(). MyType is instantiated in A as MyType<int> and in assembly B also as MyType<int>. Thus the object files produced for both A and B both contain the method MyType<int>.foo(). Unfortunately, when I then come to link them together I get linker errors as the same label is used in both object files. I could get around this by prefixing each label with the object file it is instantiated in (e.g. A_MyType<int>.foo() ) but then I still get an issue with typeinfo structures as my type comparison algorithm (e.g. for casting or interface methods) relies on a direct comparison on the addresses of typeinfo objects (e.g. typeof(MyType<int>) called from A would not equal typeof(MyType<int>) called from B).
I understand this is accomplished in Microsoft's compiler suite for C++ templates by the use of COMDAT sections where the template method/typeinfo instantiation is emitted in a special COMDAT section, where similar ones are then merged together at link time. I further understand from a quick web search that this mechanism has been extended to ELF in gcc/llvm (as there are various patch proposals etc) but I cannot find anything particularly documenting the actual standards used here if any. In particular, it is not mentioned in the ELF docs (elf32/elf64/i386 processor supplement/amd64 processor supplement) but is mentioned in a HP-UX supplement which uses the special section type SHT_HP_COMDAT.
Does anyone have any further information on the implementation of COMDAT sections in ELF? Is the HP-UX way the defacto standard?
Many thanks in advance.
Regards,
John.