Skip to content

Commit 9298526

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 d932d6f commit 9298526

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:]]*ASSIGN foo := 42$
6+
^[[:space:]]*ASSIGN .*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
@@ -173,10 +173,6 @@ void ansi_c_declarationt::to_symbol(
173173
else if(get_is_extern()) // traditional GCC
174174
symbol.is_file_local=true;
175175
}
176-
177-
// GCC __attribute__((__used__)) - do not treat those as file-local
178-
if(get_is_used())
179-
symbol.is_file_local = false;
180176
}
181177
}
182178
else // non-function
@@ -190,10 +186,8 @@ void ansi_c_declarationt::to_symbol(
190186
(!symbol.is_static_lifetime && !get_is_extern()) ||
191187
get_is_thread_local();
192188

193-
symbol.is_file_local=
194-
symbol.is_macro ||
195-
(!get_is_global() && !get_is_extern()) ||
196-
(get_is_global() && get_is_static() && !get_is_used()) ||
197-
symbol.is_parameter;
189+
symbol.is_file_local =
190+
symbol.is_macro || (!get_is_global() && !get_is_extern()) ||
191+
(get_is_global() && get_is_static()) || symbol.is_parameter;
198192
}
199193
}

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
@@ -19,6 +19,8 @@ Author: Daniel Kroening, [email protected]
1919
#include <util/std_types.h>
2020
#include <util/symbol_table_base.h>
2121

22+
#include <goto-programs/name_mangler.h>
23+
2224
#include "ansi_c_declaration.h"
2325
#include "c_storage_spec.h"
2426
#include "expr2c.h"
@@ -758,7 +760,6 @@ void c_typecheck_baset::typecheck_declaration(
758760
declaration.set_is_register(full_spec.is_register);
759761
declaration.set_is_typedef(full_spec.is_typedef);
760762
declaration.set_is_weak(full_spec.is_weak);
761-
declaration.set_is_used(full_spec.is_used);
762763

763764
symbolt symbol;
764765
declaration.to_symbol(declarator, symbol);
@@ -788,6 +789,20 @@ void c_typecheck_baset::typecheck_declaration(
788789
symbol.is_macro=true;
789790
}
790791

792+
if(full_spec.is_used && symbol.is_file_local)
793+
{
794+
// GCC __attribute__((__used__)) - do not treat those as file-local, but
795+
// make sure the name is unique
796+
symbol.is_file_local = false;
797+
798+
symbolt symbol_for_renaming = symbol;
799+
if(!full_spec.asm_label.empty())
800+
symbol_for_renaming.name = full_spec.asm_label;
801+
full_spec.asm_label = djb_manglert{}(
802+
symbol_for_renaming,
803+
id2string(symbol_for_renaming.location.get_file()));
804+
}
805+
791806
if(full_spec.section.empty())
792807
apply_asm_label(full_spec.asm_label, symbol);
793808
else

0 commit comments

Comments
 (0)