Skip to content

Commit

Permalink
Drop klpe_ prefix for extern function symbols
Browse files Browse the repository at this point in the history
Signed-off-by: Marcos Paulo de Souza <[email protected]>
  • Loading branch information
marcosps committed Jul 23, 2024
1 parent 17e3908 commit 425e5b9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 21 deletions.
50 changes: 33 additions & 17 deletions libcextract/SymbolExternalizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,36 @@ class ExternalizerVisitor: public RecursiveASTVisitor<ExternalizerVisitor>
if (!sym->Done) {
std::string sym_name = decl->getName().str();

/* If we found the first instance of the function we want to externalize,
then proceed to annotate the Decl so we can later decide what to do with
it. */
const std::string new_name = EXTERNALIZED_PREFIX + sym_name;
DeclaratorDecl *new_decl = SE.Create_Externalized_Var(decl, new_name);
sym->NewName = new_name;
sym->OldDecl = decl;
sym->NewDecl = new_decl;
SE.Log.push_back({.OldName = sym_name,
.NewName = new_name,
.Type = type});


/* If we are dealing with a symbol that is a function, and we are
* extracting code with IBT set, do not change the function now with a
* prefix. This helps to reduce the number of expanded headers, and also
* helps to match the code extracted with teh original source code.
*/
if (SE.Ibt && dyn_cast<FunctionDecl>(decl)) {
sym->NewName = sym_name;
sym->OldDecl = decl;
sym->NewDecl = decl;
sym->Wrap = false;
SE.Log.push_back({.OldName = sym_name,
.NewName = sym_name,
.Type = type});


} else {
/* If we found the first instance of the function we want to externalize,
then proceed to annotate the Decl so we can later decide what to do with
it. */
const std::string new_name = EXTERNALIZED_PREFIX + sym_name;
DeclaratorDecl *new_decl = SE.Create_Externalized_Var(decl, new_name);
sym->NewName = new_name;
sym->OldDecl = decl;
sym->NewDecl = new_decl;
sym->Wrap = !SE.Ibt;
SE.Log.push_back({.OldName = sym_name,
.NewName = new_name,
.Type = type});
}
sym->Done = true;
sym->Wrap = !SE.Ibt;
}
} else if (type == ExternalizationType::WEAK) {
/* Now checks if this is a function or a variable delcaration. */
Expand Down Expand Up @@ -224,7 +239,7 @@ class ExternalizerVisitor: public RecursiveASTVisitor<ExternalizerVisitor>

/* We must be careful to ensure that the reference we got is actually
written cleanly, e.g. it doesn't come from a macro expansion. */
if (sym_name == PrettyPrint::Get_Source_Text(range)) {
if (sym_name == PrettyPrint::Get_Source_Text(range) && sym->Needs_Sym_Rename()) {
/* Issue a text modification. */
SE.Replace_Text(range, sym->getUseName(), 100);
}
Expand Down Expand Up @@ -722,7 +737,7 @@ void SymbolExternalizer::Rewrite_Macros(void)

if (!maybe_macro && !MacroWalker::Is_Identifier_Macro_Argument(info, id_info)) {
SymbolUpdateStatus *sym = getSymbolsUpdateStatus(id_info->getName());
if (sym)
if (sym && sym->Needs_Sym_Rename())
Replace_Text(SourceRange(tok.getLocation(), tok.getLastLoc()), sym->getUseName(), 10);
}
}
Expand All @@ -734,7 +749,8 @@ void SymbolExternalizer::Rewrite_Macros(void)
for (auto &tok_range : ranges) {
// At this point, tok_range will contain a valid symbol
SymbolUpdateStatus *sym = getSymbolsUpdateStatus(tok_range.first);
Replace_Text(tok_range.second, sym->getUseName(), 10);
if (sym->Needs_Sym_Rename())
Replace_Text(tok_range.second, sym->getUseName(), 10);
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions libcextract/SymbolExternalizer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ struct SymbolUpdateStatus
return OldDecl && NewDecl && FirstUse;
}

/* For IBT, NewDecl and OldDecl are the same if the symbols is a function, so
don't replace text if the name didn't change. */
inline bool Needs_Sym_Rename(void) {
return NewDecl != OldDecl;
}

void Dump(SourceManager &SM)
{
llvm::outs() << "SymbolUpdateStatus at 0x" << this << '\n';
Expand Down
2 changes: 1 addition & 1 deletion testsuite/linux/ibt-1.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ int f(void)
return 0;
}

/* { dg-final { scan-tree-dump "u32 klpe_crc32c|u32 \(klpe_crc32c\)" } } */
/* { dg-final { scan-tree-dump "u32 crc32c\(" } } */
/* { dg-final { scan-tree-dump "KLP_RELOC_SYMBOL\(libcrc32c, libcrc32c, crc32c\)" } } */
/* { dg-final { scan-tree-dump-not "\(\*klpe_crc32c\)" } } */
2 changes: 1 addition & 1 deletion testsuite/linux/ibt-2.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ int f(void)
return 0;
}

/* { dg-final { scan-tree-dump "u32 klpe_crc32c|u32 \(klpe_crc32c\)" } } */
/* { dg-final { scan-tree-dump "u32 crc32c\(" } } */
/* { dg-final { scan-tree-dump "KLP_RELOC_SYMBOL\(libcrc32c, libcrc32c, crc32c\)" } } */
/* { dg-final { scan-tree-dump-not "\(\*klpe_crc32c\)" } } */
4 changes: 2 additions & 2 deletions testsuite/linux/ibt-typeof-1.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ int f(void)
return 0;
}

/* { dg-final { scan-tree-dump "u32 klpe_crc32c|u32 \(klpe_crc32c\)" } } */
/* { dg-final { scan-tree-dump "u32 crc32c\(" } } */
/* { dg-final { scan-tree-dump "KLP_RELOC_SYMBOL\(libcrc32c, libcrc32c, crc32c\)" } } */
/* { dg-final { scan-tree-dump "typeof\(klpe_crc32c\)" } } */
/* { dg-final { scan-tree-dump "typeof\(crc32c\)" } } */
/* { dg-final { scan-tree-dump-not "\(\*klpe_crc32c\)" } } */

0 comments on commit 425e5b9

Please sign in to comment.