creating an .init section


I would like to create an .init section in the LLVM backend. Can anyone shed me the light on how to do it? Do I have to create it in the clang?



I don’t use ‘.init’, but I do use ‘.ctors’ and ‘.dtors’ for our target. This is implemented in ‘<Target>TargetObjectFile.cpp’, in my case as follows:

MCSection *SHAVETargetObjectFile::getStaticCtorSection(unsigned Priority,

const MCSymbol * /* KeySym */) const {

std::string ctorsSectionName;

if (Priority == 65535u)

ctorsSectionName = “.ctors”;


ctorsSectionName = std::string(“.ctors.”) + utostr(65535u - Priority);

return getSHAVESection(ctorsSectionName, DATA);


and in ‘SelectSectionForGlobal’ in the same file, I have:

if (const GlobalVariable *GV = dyn_cast(GV1)) {

const StringRef gvName = GV->getName();

bool isLLVMSpecial = StringSwitch(gvName)

.Case(“llvm.used”, true)

.Case(“llvm.metadata”, true)

.Case(“llvm.global_ctors”, true)

.Case(“llvm.global_dtors”, true)


if (isLLVMSpecial) {

if (gvName == “llvm.global_ctors”)

return getStaticCtorSection();

Then use ‘attribute((constructor))’ on the function definition you want to run in the initialisation phase.


Thank a lot Martin. I will give a try.

Have a good weekend,