Skip to content

export-file-local-symbol(s): support file-local objects #6356

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
export-file-local-symbol(s): support file-local objects
Previously, only function names were mangled and exported. To enable
access to file-local objects (of static lifetime), also support mangling
their names.
  • Loading branch information
tautschnig committed Sep 21, 2021
commit 49b7690e50f23e115d619f54725cfe370a46adf1
8 changes: 8 additions & 0 deletions regression/goto-cc-file-local/objects/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
static int foo = 42;

static int bar = 42;

int baz(int x)
{
return x + 2;
}
11 changes: 11 additions & 0 deletions regression/goto-cc-file-local/objects/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CORE
main.c
show-symbol-table
^Symbol\.\.\.\.\.\.: __CPROVER_file_local_main_c_foo$
^Symbol\.\.\.\.\.\.: __CPROVER_file_local_main_c_bar$
^Symbol\.\.\.\.\.\.: baz$
^EXIT=0$
^SIGNAL=0$
--
^warning: ignoring
^Symbol\.\.\.\.\.\.: __CPROVER_file_local_main_c_baz::x$
4 changes: 2 additions & 2 deletions src/goto-cc/compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ bool compilet::link(optionalt<symbol_tablet> &&symbol_table)

if(!keep_file_local.empty())
{
function_name_manglert<file_name_manglert> mangler(
file_local_name_manglert<file_name_manglert> mangler(
log.get_message_handler(),
goto_model,
file_local_mangle_suffix,
Expand Down Expand Up @@ -430,7 +430,7 @@ optionalt<symbol_tablet> compilet::compile()

if(!keep_file_local.empty())
{
function_name_manglert<file_name_manglert> mangler(
file_local_name_manglert<file_name_manglert> mangler(
log.get_message_handler(),
file_goto_model,
file_local_mangle_suffix,
Expand Down
18 changes: 11 additions & 7 deletions src/goto-programs/name_mangler.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
/// mangled name of its \ref symbolt argument, incorporating the second
/// argument into the mangled name if possible.
template <class MangleFun>
class function_name_manglert
class file_local_name_manglert
{
public:
/// \param mh: handler to construct a log from
/// \param gm: mangle names in gm's symbol table and goto program
/// \param extra_info: a string to be included in each mangled name
/// \param needs_mangling: a regular expression describing names that need to
/// be mangled.
function_name_manglert(
file_local_name_manglert(
message_handlert &mh,
goto_modelt &gm,
const std::string &extra_info,
Expand All @@ -48,7 +48,7 @@ class function_name_manglert
{
}

/// \brief Mangle all file-local function symbols in the program
/// \brief Mangle all file-local non-type global-scoped symbols in the program
///
/// The way in which the symbols will be mangled is decided by which mangler
/// type this object is instantiated with, e.g. DJB_manglert mangles the path
Expand All @@ -66,11 +66,14 @@ class function_name_manglert
{
const symbolt &sym = sym_it->second;

if(sym.type.id() != ID_code) // is not a function
if(!sym.is_file_local)
continue;
if(sym.value.is_nil()) // has no body
if(sym.is_type)
continue;
if(!sym.is_file_local)
// no name mangling for functions without body
if(sym.type.id() == ID_code && sym.value.is_nil())
continue;
if(sym.type.id() != ID_code && !sym.is_static_lifetime)
continue;
if(!std::regex_match(id2string(sym.name), needs_mangling))
continue;
Expand All @@ -85,7 +88,8 @@ class function_name_manglert
old_syms.push_back(sym_it);

rename.insert(sym.symbol_expr(), new_sym.symbol_expr());
renamed_funs.insert(std::make_pair(sym.name, mangled));
if(sym.type.id() == ID_code)
renamed_funs.insert(std::make_pair(sym.name, mangled));

log.debug() << "Mangling: " << sym.name << " -> " << mangled << log.eom;
}
Expand Down
8 changes: 6 additions & 2 deletions src/linking/remove_internal_symbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,12 @@ void remove_internal_symbols(
{
// 'extern' symbols are only exported if there
// is an initializer.
if((has_initializer || !symbol.is_extern) &&
!is_file_local)
if(
(has_initializer || !symbol.is_extern) &&
(!is_file_local ||
(symbol.is_static_lifetime && !keep_file_local.empty() &&
std::regex_match(
id2string(symbol.name), std::regex(keep_file_local)))))
{
get_symbols(ns, symbol, exported);
}
Expand Down