@@ -849,6 +849,16 @@ void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) {
849849 StackMapSection = Ctx->getCOFFSection (" .llvm_stackmaps" ,
850850 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
851851 COFF::IMAGE_SCN_MEM_READ);
852+
853+ // Set IMAGE_SCN_MEM_DISCARDABLE so that lld will not truncate section name.
854+ PseudoProbeSection = Ctx->getCOFFSection (
855+ " .pseudo_probe" , COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
856+ COFF::IMAGE_SCN_MEM_DISCARDABLE |
857+ COFF::IMAGE_SCN_MEM_READ);
858+ PseudoProbeDescSection = Ctx->getCOFFSection (
859+ " .pseudo_probe_desc" , COFF::IMAGE_SCN_CNT_INITIALIZED_DATA |
860+ COFF::IMAGE_SCN_MEM_DISCARDABLE |
861+ COFF::IMAGE_SCN_MEM_READ);
852862}
853863
854864void MCObjectFileInfo::initSPIRVMCObjectFileInfo (const Triple &T) {
@@ -1220,44 +1230,68 @@ MCObjectFileInfo::getKCFITrapSection(const MCSection &TextSec) const {
12201230
12211231MCSection *
12221232MCObjectFileInfo::getPseudoProbeSection (const MCSection &TextSec) const {
1223- if (Ctx->getObjectFileType () != MCContext::IsELF)
1224- return PseudoProbeSection;
1225-
1226- const auto &ElfSec = static_cast <const MCSectionELF &>(TextSec);
1227- unsigned Flags = ELF::SHF_LINK_ORDER;
1228- StringRef GroupName;
1229- if (const MCSymbol *Group = ElfSec.getGroup ()) {
1230- GroupName = Group->getName ();
1231- Flags |= ELF::SHF_GROUP;
1233+ auto ObjFileType = Ctx->getObjectFileType ();
1234+ if (ObjFileType == MCContext::IsELF) {
1235+ const auto &ElfSec = static_cast <const MCSectionELF &>(TextSec);
1236+ unsigned Flags = ELF::SHF_LINK_ORDER;
1237+ StringRef GroupName;
1238+ if (const MCSymbol *Group = ElfSec.getGroup ()) {
1239+ GroupName = Group->getName ();
1240+ Flags |= ELF::SHF_GROUP;
1241+ }
1242+ return Ctx->getELFSection (
1243+ PseudoProbeSection->getName (), ELF::SHT_PROGBITS, Flags, 0 , GroupName,
1244+ true , ElfSec.getUniqueID (),
1245+ static_cast <const MCSymbolELF *>(TextSec.getBeginSymbol ()));
1246+ } else if (ObjFileType == MCContext::IsCOFF) {
1247+ StringRef COMDATSymName = " " ;
1248+ int Selection = 0 ;
1249+ unsigned Characteristics =
1250+ static_cast <MCSectionCOFF *>(PseudoProbeSection)->getCharacteristics ();
1251+ const auto &COFFSec = static_cast <const MCSectionCOFF &>(TextSec);
1252+ if (const MCSymbol *COMDATSym = COFFSec.getCOMDATSymbol ()) {
1253+ // Associate .pseudo_probe to its function section.
1254+ COMDATSymName = COMDATSym->getName ();
1255+ Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
1256+ Selection = COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE;
1257+ }
1258+ return Ctx->getCOFFSection (PseudoProbeSection->getName (), Characteristics,
1259+ COMDATSymName, Selection, COFFSec.getUniqueID ());
12321260 }
12331261
1234- return Ctx->getELFSection (
1235- PseudoProbeSection->getName (), ELF::SHT_PROGBITS, Flags, 0 , GroupName,
1236- true , ElfSec.getUniqueID (),
1237- static_cast <const MCSymbolELF *>(TextSec.getBeginSymbol ()));
1262+ return PseudoProbeSection;
12381263}
12391264
12401265MCSection *
12411266MCObjectFileInfo::getPseudoProbeDescSection (StringRef FuncName) const {
1242- if (Ctx->getObjectFileType () == MCContext::IsELF) {
1243- // Create a separate comdat group for each function's descriptor in order
1244- // for the linker to deduplicate. The duplication, must be from different
1245- // tranlation unit, can come from:
1246- // 1. Inline functions defined in header files;
1247- // 2. ThinLTO imported funcions;
1248- // 3. Weak-linkage definitions.
1249- // Use a concatenation of the section name and the function name as the
1250- // group name so that descriptor-only groups won't be folded with groups of
1251- // code.
1252- if (Ctx->getTargetTriple ().supportsCOMDAT () && !FuncName.empty ()) {
1253- auto *S = static_cast <MCSectionELF *>(PseudoProbeDescSection);
1254- auto Flags = S->getFlags () | ELF::SHF_GROUP;
1255- return Ctx->getELFSection (S->getName (), S->getType (), Flags,
1256- S->getEntrySize (),
1257- S->getName () + " _" + FuncName,
1258- /* IsComdat=*/ true );
1259- }
1267+ if (!Ctx->getTargetTriple ().supportsCOMDAT () || FuncName.empty ())
1268+ return PseudoProbeDescSection;
1269+
1270+ // Create a separate comdat group for each function's descriptor in order
1271+ // for the linker to deduplicate. The duplication, must be from different
1272+ // tranlation unit, can come from:
1273+ // 1. Inline functions defined in header files;
1274+ // 2. ThinLTO imported funcions;
1275+ // 3. Weak-linkage definitions.
1276+ // Use a concatenation of the section name and the function name as the
1277+ // group name so that descriptor-only groups won't be folded with groups of
1278+ // code.
1279+ auto ObjFileType = Ctx->getObjectFileType ();
1280+ if (ObjFileType == MCContext::IsELF) {
1281+ auto *S = static_cast <MCSectionELF *>(PseudoProbeDescSection);
1282+ auto Flags = S->getFlags () | ELF::SHF_GROUP;
1283+ return Ctx->getELFSection (S->getName (), S->getType (), Flags,
1284+ S->getEntrySize (), S->getName () + " _" + FuncName,
1285+ /* IsComdat=*/ true );
1286+ } else if (ObjFileType == MCContext::IsCOFF) {
1287+ auto *S = static_cast <MCSectionCOFF *>(PseudoProbeDescSection);
1288+ unsigned Characteristics =
1289+ S->getCharacteristics () | COFF::IMAGE_SCN_LNK_COMDAT;
1290+ std::string COMDATSymName = (S->getName () + " _" + FuncName).str ();
1291+ return Ctx->getCOFFSection (S->getName (), Characteristics, COMDATSymName,
1292+ COFF::IMAGE_COMDAT_SELECT_EXACT_MATCH);
12601293 }
1294+
12611295 return PseudoProbeDescSection;
12621296}
12631297
0 commit comments