Index: lib/MC/MCObjectFileInfo.cpp =================================================================== --- lib/MC/MCObjectFileInfo.cpp +++ lib/MC/MCObjectFileInfo.cpp @@ -595,7 +595,18 @@ COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_MEM_WRITE, SectionKind::getData()); - bool IsWoA = T.getArch() == Triple::arm || T.getArch() == Triple::thumb; + // If we are under Windows and compiling for thumb, we need to add an extra + // flag to the .text section to inform the linker thumb code is present! (see + // below). + // The flag is IMAGE_SCN_MEM_16BIT, which is documented by Microsoft as + // "reserved for future use" (see + // https://msdn.microsoft.com/en-us/library/windows/hardware/gg463119.aspx), + // but the dumpbin.exe tool from Visual Studio confirms that this set the + // corresponding "thumb code" flag on a section. + // Without this flag, the linker will interpret the code inside this section + // as ARM code, and will fail at lots of things (starting by the + // relocations). + const bool IsThumb = T.getArch() == Triple::thumb; CommDirectiveSupportsAlignment = true; @@ -606,7 +617,7 @@ SectionKind::getBSS()); TextSection = Ctx->getCOFFSection( ".text", - (IsWoA ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0) | + (IsThumb ? COFF::IMAGE_SCN_MEM_16BIT : (COFF::SectionCharacteristics)0) | COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE | COFF::IMAGE_SCN_MEM_READ, SectionKind::getText());