Skip to content

Commit 92bf7c5

Browse files
committed
Follow-up to 090790a: attribute __used__ requires unique names
With 090790a we made sure that symbols marked with __attribute__((__used__)) are not discarded. Linking multiple files that use symbols marked as such still must not result in conflicting declarations on such symbols, thus use the DJB name mangler to generate unique names.
1 parent 1ab5de1 commit 92bf7c5

File tree

7 files changed

+49
-21
lines changed

7 files changed

+49
-21
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
static int foo __attribute__((used)) = 42;
2+
3+
int main()
4+
{
5+
return 0;
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
struct bar
2+
{
3+
char *ptr;
4+
};
5+
6+
static struct bar foo __attribute__((used))
7+
__attribute__((__section__(".ref.data")));
8+
9+
static struct bar foo __attribute__((used))
10+
__attribute__((__section__(".ref.data"))) = {0};
11+
12+
void use_foo()
13+
{
14+
__CPROVER_assert(foo.ptr == 0, "null");
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE gcc-only
2+
main.c
3+
other.c
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
--
7+
^warning: ignoring
8+
^CONVERSION ERROR$

regression/goto-instrument/gcc_attribute_used1/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ main.c
33

44
^EXIT=0$
55
^SIGNAL=0$
6-
^[[:space:]]*foo = 42;$
6+
^[[:space:]]*.*foo = 42;$
77
--
88
^warning: ignoring
99
^CONVERSION ERROR$

src/ansi-c/ansi_c_declaration.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,6 @@ void ansi_c_declarationt::to_symbol(
171171
else if(get_is_extern()) // traditional GCC
172172
symbol.is_file_local=true;
173173
}
174-
175-
// GCC __attribute__((__used__)) - do not treat those as file-local
176-
if(get_is_used())
177-
symbol.is_file_local = false;
178174
}
179175
}
180176
else // non-function
@@ -188,10 +184,8 @@ void ansi_c_declarationt::to_symbol(
188184
(!symbol.is_static_lifetime && !get_is_extern()) ||
189185
get_is_thread_local();
190186

191-
symbol.is_file_local=
192-
symbol.is_macro ||
193-
(!get_is_global() && !get_is_extern()) ||
194-
(get_is_global() && get_is_static() && !get_is_used()) ||
195-
symbol.is_parameter;
187+
symbol.is_file_local =
188+
symbol.is_macro || (!get_is_global() && !get_is_extern()) ||
189+
(get_is_global() && get_is_static()) || symbol.is_parameter;
196190
}
197191
}

src/ansi-c/ansi_c_declaration.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,16 +195,6 @@ class ansi_c_declarationt:public exprt
195195
set(ID_is_weak, is_weak);
196196
}
197197

198-
bool get_is_used() const
199-
{
200-
return get_bool(ID_is_used);
201-
}
202-
203-
void set_is_used(bool is_used)
204-
{
205-
set(ID_is_used, is_used);
206-
}
207-
208198
void to_symbol(
209199
const ansi_c_declaratort &,
210200
symbolt &symbol) const;

src/ansi-c/c_typecheck_base.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Author: Daniel Kroening, [email protected]
1515
#include <util/config.h>
1616
#include <util/std_types.h>
1717

18+
#include <goto-programs/name_mangler.h>
19+
1820
#include "ansi_c_declaration.h"
1921
#include "c_storage_spec.h"
2022
#include "expr2c.h"
@@ -655,7 +657,6 @@ void c_typecheck_baset::typecheck_declaration(
655657
declaration.set_is_register(full_spec.is_register);
656658
declaration.set_is_typedef(full_spec.is_typedef);
657659
declaration.set_is_weak(full_spec.is_weak);
658-
declaration.set_is_used(full_spec.is_used);
659660

660661
symbolt symbol;
661662
declaration.to_symbol(declarator, symbol);
@@ -685,6 +686,20 @@ void c_typecheck_baset::typecheck_declaration(
685686
symbol.is_macro=true;
686687
}
687688

689+
if(full_spec.is_used && symbol.is_file_local)
690+
{
691+
// GCC __attribute__((__used__)) - do not treat those as file-local, but
692+
// make sure the name is unique
693+
symbol.is_file_local = false;
694+
695+
symbolt symbol_for_renaming = symbol;
696+
if(!full_spec.asm_label.empty())
697+
symbol_for_renaming.name = full_spec.asm_label;
698+
full_spec.asm_label = djb_manglert{}(
699+
symbol_for_renaming,
700+
id2string(symbol_for_renaming.location.get_file()));
701+
}
702+
688703
if(full_spec.section.empty())
689704
apply_asm_label(full_spec.asm_label, symbol);
690705
else

0 commit comments

Comments
 (0)