Skip to content

Commit 760e60e

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 7f998c4 commit 760e60e

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
@@ -167,10 +167,6 @@ void ansi_c_declarationt::to_symbol(
167167
else if(get_is_extern()) // traditional GCC
168168
symbol.is_file_local=true;
169169
}
170-
171-
// GCC __attribute__((__used__)) - do not treat those as file-local
172-
if(get_is_used())
173-
symbol.is_file_local = false;
174170
}
175171
}
176172
else // non-function
@@ -184,10 +180,8 @@ void ansi_c_declarationt::to_symbol(
184180
(!symbol.is_static_lifetime && !get_is_extern()) ||
185181
get_is_thread_local();
186182

187-
symbol.is_file_local=
188-
symbol.is_macro ||
189-
(!get_is_global() && !get_is_extern()) ||
190-
(get_is_global() && get_is_static() && !get_is_used()) ||
191-
symbol.is_parameter;
183+
symbol.is_file_local =
184+
symbol.is_macro || (!get_is_global() && !get_is_extern()) ||
185+
(get_is_global() && get_is_static()) || symbol.is_parameter;
192186
}
193187
}

src/ansi-c/ansi_c_declaration.h

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

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

src/ansi-c/c_typecheck_base.cpp

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

20+
#include <goto-programs/name_mangler.h>
21+
2022
#include "c_storage_spec.h"
2123
#include "expr2c.h"
2224
#include "type2name.h"
@@ -670,7 +672,6 @@ void c_typecheck_baset::typecheck_declaration(
670672
declaration.set_is_register(full_spec.is_register);
671673
declaration.set_is_typedef(full_spec.is_typedef);
672674
declaration.set_is_weak(full_spec.is_weak);
673-
declaration.set_is_used(full_spec.is_used);
674675

675676
symbolt symbol;
676677
declaration.to_symbol(declarator, symbol);
@@ -700,6 +701,20 @@ void c_typecheck_baset::typecheck_declaration(
700701
symbol.is_macro=true;
701702
}
702703

704+
if(full_spec.is_used && symbol.is_file_local)
705+
{
706+
// GCC __attribute__((__used__)) - do not treat those as file-local, but
707+
// make sure the name is unique
708+
symbol.is_file_local = false;
709+
710+
symbolt symbol_for_renaming = symbol;
711+
if(!full_spec.asm_label.empty())
712+
symbol_for_renaming.name = full_spec.asm_label;
713+
full_spec.asm_label = djb_manglert{}(
714+
symbol_for_renaming,
715+
id2string(symbol_for_renaming.location.get_file()));
716+
}
717+
703718
if(full_spec.section.empty())
704719
apply_asm_label(full_spec.asm_label, symbol);
705720
else

0 commit comments

Comments
 (0)