Skip to content

Commit

Permalink
rewriter: Redeclare static functions that have their address taken wi…
Browse files Browse the repository at this point in the history
…th __used__

When we make call gates for static functions that have their address taken, the
static function is only referenced from the asm for the call gate. When
compiling with optimizations dead code elimination may get rid of the static
functions unless they are marked with __attribute__((__used__)). The rewriter
previously prepended this attribute in this case to avoid that. This commit
removes the prepended attribute and instead redeclares these functions with that
attribute. Closes #414 and fixes a test added for #428.
  • Loading branch information
ayrtonm committed Oct 11, 2024
1 parent 504ba14 commit 9f17f75
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 19 deletions.
4 changes: 3 additions & 1 deletion runtime/libia2/include/ia2.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
#define IA2_CALL(opaque, id) opaque
#define IA2_CAST(func, ty) (ty) (void *) func
#else
#define IA2_DEFINE_WRAPPER(func) IA2_DEFINE_WRAPPER_##func
#define IA2_DEFINE_WRAPPER(func) \
typeof(func) func __attribute__((__used__)); \
IA2_DEFINE_WRAPPER_##func
#define IA2_SIGHANDLER(func) ia2_sighandler_##func
/// Create a wrapped signal handler for `sa_sigaction`
///
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ add_subdirectory(shared_data)
add_subdirectory(global_fn_ptr)
add_subdirectory(rewrite_macros)
add_subdirectory(sighandler)
add_subdirectory(static_addr_taken)

# The following tests are not supported on ARM64 yet
if (NOT LIBIA2_AARCH64)
Expand Down
19 changes: 1 addition & 18 deletions tools/rewriter/SourceRewriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,24 +666,6 @@ class FnPtrExpr : public RefactoringCallback {

auto [it, new_fn] = internal_addr_taken_fns[filename].insert(
std::make_pair(fn_name, new_type));

// TODO: Note that this only checks if a function is added to the
// internal_addr_taken_fns map. To make the rewriter idempotent we should
// check for an existing used attribute.
if (new_fn) {
auto decl_start = fn_decl->getBeginLoc();
if (!decl_start.isFileID()) {
llvm::errs() << "Error: non-file loc for function " << fn_name << '\n';
} else {
Replacement old_used_attr(sm, decl_start, 0,
llvm::StringRef("__attribute__((used)) "));
Replacement used_attr = replace_new_file(filename, old_used_attr);
auto err = file_replacements[filename].add(used_attr);
if (err) {
llvm::errs() << "Error adding replacements: " << err << '\n';
}
}
}
}

// This check must come after modifying the maps in this pass but before the
Expand All @@ -701,6 +683,7 @@ class FnPtrExpr : public RefactoringCallback {
return;
}

// Add the IA2_FN annotation around the function pointer expression
clang::CharSourceRange expansion_range = sm.getExpansionRange(loc);
Replacement old_r{sm, expansion_range, new_expr};
Replacement r = replace_new_file(filename, old_r);
Expand Down

0 comments on commit 9f17f75

Please sign in to comment.