@@ -792,6 +792,16 @@ void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) {
792792 StackMapSection = Ctx->getCOFFSection (" .llvm_stackmaps" ,
793793 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
794794 COFF::IMAGE_SCN_MEM_READ);
795+
796+ // Set IMAGE_SCN_MEM_DISCARDABLE so that lld will not truncate section name.
797+ PseudoProbeSection = Ctx->getCOFFSection (
798+ " .pseudo_probe" , COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
799+ COFF::IMAGE_SCN_MEM_DISCARDABLE |
800+ COFF::IMAGE_SCN_MEM_READ);
801+ PseudoProbeDescSection = Ctx->getCOFFSection (
802+ " .pseudo_probe_desc" , COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
803+ COFF::IMAGE_SCN_MEM_DISCARDABLE |
804+ COFF::IMAGE_SCN_MEM_READ);
795805}
796806
797807void MCObjectFileInfo::initSPIRVMCObjectFileInfo (const Triple &T) {
@@ -1141,41 +1151,68 @@ MCObjectFileInfo::getKCFITrapSection(const MCSection &TextSec) const {
11411151
11421152MCSection *
11431153MCObjectFileInfo::getPseudoProbeSection (const MCSection &TextSec) const {
1144- if (Ctx->getObjectFileType () != MCContext::IsELF)
1145- return PseudoProbeSection;
1154+ auto ObjFileType = Ctx->getObjectFileType ();
1155+ if (ObjFileType == MCContext::IsELF) {
1156+ const auto &ElfSec = static_cast <const MCSectionELF &>(TextSec);
1157+ unsigned Flags = ELF::SHF_LINK_ORDER;
1158+ StringRef GroupName;
1159+ if (const MCSymbol *Group = ElfSec.getGroup ()) {
1160+ GroupName = Group->getName ();
1161+ Flags |= ELF::SHF_GROUP;
1162+ }
11461163
1147- const auto &ElfSec = static_cast <const MCSectionELF &>(TextSec);
1148- unsigned Flags = ELF::SHF_LINK_ORDER;
1149- StringRef GroupName;
1150- if (const MCSymbol *Group = ElfSec.getGroup ()) {
1151- GroupName = Group->getName ();
1152- Flags |= ELF::SHF_GROUP;
1164+ return Ctx->getELFSection (PseudoProbeSection->getName (), ELF::SHT_PROGBITS,
1165+ Flags, 0 , GroupName, true , ElfSec.getUniqueID (),
1166+ cast<MCSymbolELF>(TextSec.getBeginSymbol ()));
1167+ } else if (ObjFileType == MCContext::IsCOFF) {
1168+ StringRef COMDATSymName = " " ;
1169+ int Selection = 0 ;
1170+ unsigned Characteristics =
1171+ COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_DISCARDABLE |
1172+ COFF::IMAGE_SCN_MEM_READ | COFF::IMAGE_SCN_LNK_COMDAT;
1173+ auto &COFFSec = cast<MCSectionCOFF>(TextSec);
1174+ if (const MCSymbol *COMDATSym = COFFSec.getCOMDATSymbol ()) {
1175+ // Associate .pseudo_probe to its function section.
1176+ COMDATSymName = COMDATSym->getName ();
1177+ Selection = COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
1178+ }
1179+
1180+ return Ctx->getCOFFSection (PseudoProbeSection->getName (), Characteristics,
1181+ COMDATSymName, Selection, COFFSec.getUniqueID ());
11531182 }
11541183
1155- return Ctx->getELFSection (PseudoProbeSection->getName (), ELF::SHT_PROGBITS,
1156- Flags, 0 , GroupName, true , ElfSec.getUniqueID (),
1157- cast<MCSymbolELF>(TextSec.getBeginSymbol ()));
1184+ return PseudoProbeSection;
11581185}
11591186
11601187MCSection *
11611188MCObjectFileInfo::getPseudoProbeDescSection (StringRef FuncName) const {
1162- if (Ctx->getObjectFileType () == MCContext::IsELF) {
1163- // Create a separate comdat group for each function's descriptor in order
1164- // for the linker to deduplicate. The duplication, must be from different
1165- // tranlation unit, can come from:
1166- // 1. Inline functions defined in header files;
1167- // 2. ThinLTO imported funcions;
1168- // 3. Weak-linkage definitions.
1169- // Use a concatenation of the section name and the function name as the
1170- // group name so that descriptor-only groups won't be folded with groups of
1171- // code.
1172- if (Ctx->getTargetTriple ().supportsCOMDAT () && !FuncName.empty ()) {
1189+ // Create a separate comdat group for each function's descriptor in order
1190+ // for the linker to deduplicate. The duplication, must be from different
1191+ // tranlation unit, can come from:
1192+ // 1. Inline functions defined in header files;
1193+ // 2. ThinLTO imported funcions;
1194+ // 3. Weak-linkage definitions.
1195+ // Use a concatenation of the section name and the function name as the
1196+ // group name so that descriptor-only groups won't be folded with groups of
1197+ // code.
1198+ if (Ctx->getTargetTriple ().supportsCOMDAT () && !FuncName.empty ()) {
1199+ auto ObjFileType = Ctx->getObjectFileType ();
1200+ if (ObjFileType == MCContext::IsELF) {
11731201 auto *S = static_cast <MCSectionELF *>(PseudoProbeDescSection);
11741202 auto Flags = S->getFlags () | ELF::SHF_GROUP;
11751203 return Ctx->getELFSection (S->getName (), S->getType (), Flags,
11761204 S->getEntrySize (),
11771205 S->getName () + " _" + FuncName,
11781206 /* IsComdat=*/ true );
1207+ } else if (ObjFileType == MCContext::IsCOFF) {
1208+ unsigned Characteristics = COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
1209+ COFF::IMAGE_SCN_MEM_DISCARDABLE |
1210+ COFF::IMAGE_SCN_MEM_READ |
1211+ COFF::IMAGE_SCN_LNK_COMDAT;
1212+ auto *S = cast<MCSectionCOFF>(PseudoProbeDescSection);
1213+ std::string COMDATSymName = (S->getName () + " _" + FuncName).str ();
1214+ return Ctx->getCOFFSection (S->getName (), Characteristics, COMDATSymName,
1215+ COFF::IMAGE_COMDAT_SELECT_ANY);
11791216 }
11801217 }
11811218 return PseudoProbeDescSection;
0 commit comments