Skip to content

Commit 68d0636

Browse files
committed
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.
1 parent 8c5abdf commit 68d0636

File tree

5 files changed

+39
-12
lines changed

5 files changed

+39
-12
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
static int foo = 42;
2+
3+
static int bar = 42;
4+
5+
int baz(int x)
6+
{
7+
return x + 2;
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
show-symbol-table
4+
^Symbol\.\.\.\.\.\.: __CPROVER_file_local_main_c_foo$
5+
^Symbol\.\.\.\.\.\.: __CPROVER_file_local_main_c_bar$
6+
^Symbol\.\.\.\.\.\.: baz$
7+
^EXIT=0$
8+
^SIGNAL=0$
9+
--
10+
^warning: ignoring
11+
^Symbol\.\.\.\.\.\.: __CPROVER_file_local_main_c_baz::x$

src/goto-cc/compile.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ bool compilet::link(optionalt<symbol_tablet> &&symbol_table)
356356

357357
if(!keep_file_local.empty())
358358
{
359-
function_name_manglert<file_name_manglert> mangler(
359+
file_local_name_manglert<file_name_manglert> mangler(
360360
log.get_message_handler(),
361361
goto_model,
362362
file_local_mangle_suffix,
@@ -430,7 +430,7 @@ optionalt<symbol_tablet> compilet::compile()
430430

431431
if(!keep_file_local.empty())
432432
{
433-
function_name_manglert<file_name_manglert> mangler(
433+
file_local_name_manglert<file_name_manglert> mangler(
434434
log.get_message_handler(),
435435
file_goto_model,
436436
file_local_mangle_suffix,
@@ -660,7 +660,7 @@ static std::string csv_to_pattern(const std::list<std::string> &values)
660660
{
661661
std::string result;
662662

663-
for(const auto& csv : values)
663+
for(const auto &csv : values)
664664
{
665665
for(const auto &name : split_string(csv, ','))
666666
{

src/goto-programs/name_mangler.h

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@
2727
/// mangled name of its \ref symbolt argument, incorporating the second
2828
/// argument into the mangled name if possible.
2929
template <class MangleFun>
30-
class function_name_manglert
30+
class file_local_name_manglert
3131
{
3232
public:
3333
/// \param mh: handler to construct a log from
3434
/// \param gm: mangle names in gm's symbol table and goto program
3535
/// \param extra_info: a string to be included in each mangled name
3636
/// \param needs_mangling: a regular expression describing names that need to
3737
/// be mangled.
38-
function_name_manglert(
38+
file_local_name_manglert(
3939
message_handlert &mh,
4040
goto_modelt &gm,
4141
const std::string &extra_info,
@@ -48,7 +48,7 @@ class function_name_manglert
4848
{
4949
}
5050

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

69-
if(sym.type.id() != ID_code) // is not a function
69+
if(!sym.is_file_local)
7070
continue;
71-
if(sym.value.is_nil()) // has no body
71+
if(sym.is_type)
7272
continue;
73-
if(!sym.is_file_local)
73+
// no name mangling for functions without body
74+
if(sym.type.id() == ID_code && sym.value.is_nil())
75+
continue;
76+
if(sym.type.id() != ID_code && !sym.is_static_lifetime)
7477
continue;
7578
if(!std::regex_match(id2string(sym.name), needs_mangling))
7679
continue;
@@ -85,7 +88,8 @@ class function_name_manglert
8588
old_syms.push_back(sym_it);
8689

8790
rename.insert(sym.symbol_expr(), new_sym.symbol_expr());
88-
renamed_funs.insert(std::make_pair(sym.name, mangled));
91+
if(sym.type.id() == ID_code)
92+
renamed_funs.insert(std::make_pair(sym.name, mangled));
8993

9094
log.debug() << "Mangling: " << sym.name << " -> " << mangled << log.eom;
9195
}

src/linking/remove_internal_symbols.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,12 @@ void remove_internal_symbols(
189189
{
190190
// 'extern' symbols are only exported if there
191191
// is an initializer.
192-
if((has_initializer || !symbol.is_extern) &&
193-
!is_file_local)
192+
if(
193+
(has_initializer || !symbol.is_extern) &&
194+
(!is_file_local ||
195+
(symbol.is_static_lifetime && !keep_file_local.empty() &&
196+
std::regex_match(
197+
id2string(symbol.name), std::regex(keep_file_local)))))
194198
{
195199
get_symbols(ns, symbol, exported);
196200
}

0 commit comments

Comments
 (0)