diff --git a/libcextract/SymbolExternalizer.cpp b/libcextract/SymbolExternalizer.cpp index 26f04d7..955bf75 100644 --- a/libcextract/SymbolExternalizer.cpp +++ b/libcextract/SymbolExternalizer.cpp @@ -123,21 +123,36 @@ class ExternalizerVisitor: public RecursiveASTVisitor 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(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. */ @@ -224,7 +239,7 @@ class ExternalizerVisitor: public RecursiveASTVisitor /* 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); } @@ -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); } } @@ -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); } } } diff --git a/libcextract/SymbolExternalizer.hh b/libcextract/SymbolExternalizer.hh index fff6ec2..319be4a 100644 --- a/libcextract/SymbolExternalizer.hh +++ b/libcextract/SymbolExternalizer.hh @@ -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'; diff --git a/testsuite/linux/ibt-1.c b/testsuite/linux/ibt-1.c index bbe9ee9..e290024 100644 --- a/testsuite/linux/ibt-1.c +++ b/testsuite/linux/ibt-1.c @@ -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\)" } } */ diff --git a/testsuite/linux/ibt-2.c b/testsuite/linux/ibt-2.c index bbe9ee9..e290024 100644 --- a/testsuite/linux/ibt-2.c +++ b/testsuite/linux/ibt-2.c @@ -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\)" } } */ diff --git a/testsuite/linux/ibt-typeof-1.c b/testsuite/linux/ibt-typeof-1.c index e7ff0db..8670e7a 100644 --- a/testsuite/linux/ibt-typeof-1.c +++ b/testsuite/linux/ibt-typeof-1.c @@ -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\)" } } */