[LLVMdev] Question about ctors, dtors and sections on Windows

Joe Groff arcata at gmail.com
Fri Sep 14 17:42:17 PDT 2012


On Mon, Sep 10, 2012 at 12:05 PM, Kai <kai at redstar.de> wrote:
> On Windows, functions from llvm.global_ctors are placed in section .CRT$XCU,
> which is automatically called by the MS C Runtime. However, functions from
> llvm.global_dtors are placed in section .dtors which is unknown to the
> runtime. Therefore only ctors are called. Is this expected behaviour?
>
> My expectation was that the dtors placed in .CRT$XTU which are the C
> terminator functions. Maybe there is a way to customize this?

Hi Kai. I submitted the patch to use the .CRT$XCU section for ctors on
Win32; I did not do so for dtors because I was unaware of .CRT$XTU. My
apologies. If you look in lib/MC/MCObjectFileInfo.cpp:417, you can see
the following logic:

```

  if (T.getOS() == Triple::Win32) {
    StaticCtorSection =
      Ctx->getCOFFSection(".CRT$XCU",
                          COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
                          COFF::IMAGE_SCN_MEM_READ,
                          SectionKind::getReadOnly());
  } else {
    StaticCtorSection =
      Ctx->getCOFFSection(".ctors",
                          COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
                          COFF::IMAGE_SCN_MEM_READ |
                          COFF::IMAGE_SCN_MEM_WRITE,
                          SectionKind::getDataRel());
  }
```

The logic just needs to be replicated to set StaticDtorSection
appropriately for Win32 targets. Sorry again for the oversight.

-Joe



More information about the llvm-dev mailing list