@@ -502,8 +502,7 @@ class LowerTypeTestsModule {
502
502
uint8_t *exportTypeId (StringRef TypeId, const TypeIdLowering &TIL);
503
503
TypeIdLowering importTypeId (StringRef TypeId);
504
504
void importTypeTest (CallInst *CI);
505
- void importFunction (Function *F, bool isJumpTableCanonical,
506
- std::vector<GlobalAlias *> &AliasesToErase);
505
+ void importFunction (Function *F, bool isJumpTableCanonical);
507
506
508
507
BitSetInfo
509
508
buildBitSet (Metadata *TypeId,
@@ -1103,9 +1102,8 @@ void LowerTypeTestsModule::maybeReplaceComdat(Function *F,
1103
1102
1104
1103
// ThinLTO backend: the function F has a jump table entry; update this module
1105
1104
// accordingly. isJumpTableCanonical describes the type of the jump table entry.
1106
- void LowerTypeTestsModule::importFunction (
1107
- Function *F, bool isJumpTableCanonical,
1108
- std::vector<GlobalAlias *> &AliasesToErase) {
1105
+ void LowerTypeTestsModule::importFunction (Function *F,
1106
+ bool isJumpTableCanonical) {
1109
1107
assert (F->getType ()->getAddressSpace () == 0 );
1110
1108
1111
1109
GlobalValue::VisibilityTypes Visibility = F->getVisibility ();
@@ -1135,23 +1133,23 @@ void LowerTypeTestsModule::importFunction(
1135
1133
} else {
1136
1134
F->setName (Name + " .cfi" );
1137
1135
maybeReplaceComdat (F, Name);
1138
- F->setLinkage (GlobalValue::ExternalLinkage);
1139
1136
FDecl = Function::Create (F->getFunctionType (), GlobalValue::ExternalLinkage,
1140
1137
F->getAddressSpace (), Name, &M);
1141
1138
FDecl->setVisibility (Visibility);
1142
1139
Visibility = GlobalValue::HiddenVisibility;
1143
1140
1144
- // Delete aliases pointing to this function, they'll be re-created in the
1145
- // merged output. Don't do it yet though because ScopedSaveAliaseesAndUsed
1146
- // will want to reset the aliasees first .
1141
+ // Update aliases pointing to this function to also include the ".cfi" suffix,
1142
+ // We expect the jump table entry to either point to the real function or an
1143
+ // alias. Redirect all other users to the jump table entry .
1147
1144
for (auto &U : F->uses ()) {
1148
1145
if (auto *A = dyn_cast<GlobalAlias>(U.getUser ())) {
1146
+ std::string AliasName = A->getName ().str () + " .cfi" ;
1149
1147
Function *AliasDecl = Function::Create (
1150
1148
F->getFunctionType (), GlobalValue::ExternalLinkage,
1151
1149
F->getAddressSpace (), " " , &M);
1152
1150
AliasDecl->takeName (A);
1153
1151
A->replaceAllUsesWith (AliasDecl);
1154
- AliasesToErase. push_back (A );
1152
+ A-> setName (AliasName );
1155
1153
}
1156
1154
}
1157
1155
}
@@ -2077,16 +2075,13 @@ bool LowerTypeTestsModule::lower() {
2077
2075
Decls.push_back (&F);
2078
2076
}
2079
2077
2080
- std::vector<GlobalAlias *> AliasesToErase;
2081
2078
{
2082
2079
ScopedSaveAliaseesAndUsed S (M);
2083
2080
for (auto *F : Defs)
2084
- importFunction (F, /* isJumpTableCanonical*/ true , AliasesToErase );
2081
+ importFunction (F, /* isJumpTableCanonical*/ true );
2085
2082
for (auto *F : Decls)
2086
- importFunction (F, /* isJumpTableCanonical*/ false , AliasesToErase );
2083
+ importFunction (F, /* isJumpTableCanonical*/ false );
2087
2084
}
2088
- for (GlobalAlias *GA : AliasesToErase)
2089
- GA->eraseFromParent ();
2090
2085
2091
2086
return true ;
2092
2087
}
@@ -2137,6 +2132,18 @@ bool LowerTypeTestsModule::lower() {
2137
2132
if (auto Alias = dyn_cast<AliasSummary>(RefGVS.get ()))
2138
2133
AddressTaken.insert (Alias->getAliaseeGUID ());
2139
2134
}
2135
+ auto IsAddressTaken = [&](GlobalValue::GUID GUID) {
2136
+ if (AddressTaken.count (GUID))
2137
+ return true ;
2138
+ auto VI = ExportSummary->getValueInfo (GUID);
2139
+ if (!VI)
2140
+ return false ;
2141
+ for (auto &I : VI.getSummaryList ())
2142
+ if (auto Alias = dyn_cast<AliasSummary>(I.get ()))
2143
+ if (AddressTaken.count (Alias->getAliaseeGUID ()))
2144
+ return true ;
2145
+ return false ;
2146
+ };
2140
2147
for (auto *FuncMD : CfiFunctionsMD->operands ()) {
2141
2148
assert (FuncMD->getNumOperands () >= 2 );
2142
2149
StringRef FunctionName =
@@ -2153,7 +2160,7 @@ bool LowerTypeTestsModule::lower() {
2153
2160
// have no live references (and are not exported with cross-DSO CFI.)
2154
2161
if (!ExportSummary->isGUIDLive (GUID))
2155
2162
continue ;
2156
- if (!AddressTaken. count (GUID)) {
2163
+ if (!IsAddressTaken (GUID)) {
2157
2164
if (!CrossDsoCfi || Linkage != CFL_Definition)
2158
2165
continue ;
2159
2166
@@ -2227,6 +2234,43 @@ bool LowerTypeTestsModule::lower() {
2227
2234
}
2228
2235
}
2229
2236
2237
+ struct AliasToCreate {
2238
+ Function *Alias;
2239
+ std::string TargetName;
2240
+ };
2241
+ std::vector<AliasToCreate> AliasesToCreate;
2242
+
2243
+ // Parse alias data to replace stand-in function declarations for aliases
2244
+ // with an alias to the intended target.
2245
+ if (ExportSummary) {
2246
+ if (NamedMDNode *AliasesMD = M.getNamedMetadata (" aliases" )) {
2247
+ for (auto *AliasMD : AliasesMD->operands ()) {
2248
+ SmallVector<Function *> Aliases;
2249
+ for (Metadata *MD : AliasMD->operands ()) {
2250
+ auto *MDS = dyn_cast<MDString>(MD);
2251
+ if (!MDS)
2252
+ continue ;
2253
+ StringRef AliasName = MDS->getString ();
2254
+ if (!ExportedFunctions.count (AliasName))
2255
+ continue ;
2256
+ auto *AliasF = M.getFunction (AliasName);
2257
+ if (AliasF)
2258
+ Aliases.push_back (AliasF);
2259
+ }
2260
+
2261
+ if (Aliases.empty ())
2262
+ continue ;
2263
+
2264
+ for (unsigned I = 1 ; I != Aliases.size (); ++I) {
2265
+ auto *AliasF = Aliases[I];
2266
+ ExportedFunctions.erase (AliasF->getName ());
2267
+ AliasesToCreate.push_back (
2268
+ {AliasF, std::string (Aliases[0 ]->getName ())});
2269
+ }
2270
+ }
2271
+ }
2272
+ }
2273
+
2230
2274
DenseMap<GlobalObject *, GlobalTypeMember *> GlobalTypeMembers;
2231
2275
for (GlobalObject &GO : M.global_objects ()) {
2232
2276
if (isa<GlobalVariable>(GO) && GO.isDeclarationForLinker ())
@@ -2414,47 +2458,16 @@ bool LowerTypeTestsModule::lower() {
2414
2458
2415
2459
allocateByteArrays ();
2416
2460
2417
- // Parse alias data to replace stand-in function declarations for aliases
2418
- // with an alias to the intended target.
2419
- if (ExportSummary) {
2420
- if (NamedMDNode *AliasesMD = M.getNamedMetadata (" aliases" )) {
2421
- for (auto *AliasMD : AliasesMD->operands ()) {
2422
- assert (AliasMD->getNumOperands () >= 4 );
2423
- StringRef AliasName =
2424
- cast<MDString>(AliasMD->getOperand (0 ))->getString ();
2425
- StringRef Aliasee = cast<MDString>(AliasMD->getOperand (1 ))->getString ();
2426
-
2427
- if (auto It = ExportedFunctions.find (Aliasee);
2428
- It == ExportedFunctions.end () ||
2429
- It->second .Linkage != CFL_Definition || !M.getNamedAlias (Aliasee))
2430
- continue ;
2431
-
2432
- GlobalValue::VisibilityTypes Visibility =
2433
- static_cast <GlobalValue::VisibilityTypes>(
2434
- cast<ConstantAsMetadata>(AliasMD->getOperand (2 ))
2435
- ->getValue ()
2436
- ->getUniqueInteger ()
2437
- .getZExtValue ());
2438
- bool Weak =
2439
- static_cast <bool >(cast<ConstantAsMetadata>(AliasMD->getOperand (3 ))
2440
- ->getValue ()
2441
- ->getUniqueInteger ()
2442
- .getZExtValue ());
2443
-
2444
- auto *Alias = GlobalAlias::create (" " , M.getNamedAlias (Aliasee));
2445
- Alias->setVisibility (Visibility);
2446
- if (Weak)
2447
- Alias->setLinkage (GlobalValue::WeakAnyLinkage);
2448
-
2449
- if (auto *F = M.getFunction (AliasName)) {
2450
- Alias->takeName (F);
2451
- F->replaceAllUsesWith (Alias);
2452
- F->eraseFromParent ();
2453
- } else {
2454
- Alias->setName (AliasName);
2455
- }
2456
- }
2457
- }
2461
+ for (auto A : AliasesToCreate) {
2462
+ auto *Target = M.getNamedValue (A.TargetName );
2463
+ if (!isa<GlobalAlias>(Target))
2464
+ continue ;
2465
+ auto *AliasGA = GlobalAlias::create (" " , Target);
2466
+ AliasGA->setVisibility (A.Alias ->getVisibility ());
2467
+ AliasGA->setLinkage (A.Alias ->getLinkage ());
2468
+ AliasGA->takeName (A.Alias );
2469
+ A.Alias ->replaceAllUsesWith (AliasGA);
2470
+ A.Alias ->eraseFromParent ();
2458
2471
}
2459
2472
2460
2473
// Emit .symver directives for exported functions, if they exist.
0 commit comments