History

Reinstate attribute alias handling

commit 2a0167a merged alias and asm symbol renaming, but broke semantics of aliases, see testcase. Basically the difference between the two is that an asm rename doesn't generate a new symbol, i.e. with int foo __asm__("bar"); all source reference to 'foo' will be to 'bar', nothing of the name 'foo' will remain in the object file, and for instance reference to 'foo' from other compilation units won't be resolved to this one. Aliases OTOH create an additional symbol. With: void target (void) { return; } void afunc (void) __attribute__((alias("target"))); reference to 'afunc' will remain 'afunc' in the object file. It will generate two symbols, 'afunc' and 'target' referring to the same entity. This difference matters if other compilation units make references to 'afunc'. A side requirement of this is that for alias to work that the target symbol needs to be defined in the same unit. For TCC we even require a stricter variant: it must be defined before the alias is created. Now, with this I merely re-instated the old flow of events before above commit. It didn't seem useful anymore to place both names in the asm_label member of attributes, and the asm_label member of Sym now again only needs the hold the __asm__ rename. It also follows that tcc_predefs.h can't make use of attribute alias to e.g. map __builtin_memcpy to __bound_memcpy (simply because the latter isn't defined in all units), but rather must use __asm__ renaming, which in turn means that the underscore handling needs to be done by hand.

Michael Matz committed 6 years ago

Commit 352e1d01 parent 727e24c7 files · +80 −10Browse files

Unified · Split

7 files
  1. tcc.h +1 −0
  2. tccgen.c +20 −6
  3. tccpp.c +9 −4
  4. tests/tests2/120+_alias.c +15 −0
  5. tests/tests2/120_alias.c +25 −0
  6. tests/tests2/120_alias.expect +5 −0
  7. tests/tests2/Makefile +5 −0
tcc.h+1−0
@@ -643,6 +643,7 @@ typedef struct AttributeDef { · 642 unmodified lines
643643 struct FuncAttr f;
644644 struct Section *section;
645645 Sym *cleanup_func;
646+ int alias_target; /* token */
646647 int asm_label; /* associated asm label */
647648 char attr_mode; /* __attribute__((__mode__(...))) */
648649 } AttributeDef;
tccgen.c+20−6
@@ -939,10 +939,8 @@ ST_FUNC void put_extern_sym2(Sym *sym, int sh_num, · 938 unmodified lines
939939 #endif
940940
941941 if (sym->asm_label) {
942− name = get_tok_str(sym->asm_label & ~SYM_FIELD, NULL);
943− /* with SYM_FIELD it was __attribute__((alias("..."))) actually */
944− if (!(sym->asm_label & SYM_FIELD))
945− can_add_underscore = 0;
942+ name = get_tok_str(sym->asm_label, NULL);
943+ can_add_underscore = 0;
946944 }
947945
948946 if (tcc_state->leading_underscore && can_add_underscore) {
@@ -1524,6 +1522,8 @@ static void merge_attr(AttributeDef *ad, AttributeDef *ad1) · 575 unmodified lines
15241522
15251523 if (ad1->section)
15261524 ad->section = ad1->section;
1525+ if (ad1->alias_target)
1526+ ad->alias_target = ad1->alias_target;
15271527 if (ad1->asm_label)
15281528 ad->asm_label = ad1->asm_label;
15291529 if (ad1->attr_mode)
@@ -4147,8 +4147,8 @@ redo: · 2617 unmodified lines
41474147 case TOK_ALIAS2:
41484148 skip('(');
41494149 parse_mult_str(&astr, "alias(\"target\")");
4150− ad->asm_label = /* save string as token, for later */
4151− tok_alloc((char*)astr.data, astr.size-1)->tok | SYM_FIELD;
4150+ ad->alias_target = /* save string as token, for later */
4151+ tok_alloc((char*)astr.data, astr.size-1)->tok;
41524152 skip(')');
41534153 cstr_free(&astr);
41544154 break;
@@ -8459,6 +8459,20 @@ found: · 4304 unmodified lines
84598459 /* external variable or function */
84608460 type.t |= VT_EXTERN;
84618461 sym = external_sym(v, &type, r, &ad);
8462+ if (ad.alias_target) {
8463+ /* Aliases need to be emitted when their target
8464+ symbol is emitted, even if perhaps unreferenced.
8465+ We only support the case where the base is
8466+ already defined, otherwise we would need
8467+ deferring to emit the aliases until the end of
8468+ the compile unit. */
8469+ Sym *alias_target = sym_find(ad.alias_target);
8470+ ElfSym *esym = elfsym(alias_target);
8471+ if (!esym)
8472+ tcc_error("unsupported forward __alias__ attribute");
8473+ put_extern_sym2(sym, esym->st_shndx,
8474+ esym->st_value, esym->st_size, 0);
8475+ }
84628476 } else {
84638477 if (type.t & VT_STATIC)
84648478 r |= VT_CONST;
tccpp.c+9−4
@@ -3673,16 +3673,21 @@ static void tcc_predefs(CString *cstr) · 3672 unmodified lines
36733673 "#ifndef __builtin_va_copy\n"
36743674 "#define __builtin_va_copy(dest,src) (dest)=(src)\n"
36753675 "#endif\n"
3676+ "#ifdef __leading_underscore\n"
3677+ "#define RENAME(X) __asm__(\"_\"X)\n"
3678+ "#else\n"
3679+ "#define RENAME(X) __asm__(X)\n"
3680+ "#endif\n"
36763681 /* TCC BBUILTIN AND BOUNDS ALIASES */
36773682 "#ifdef __BOUNDS_CHECKING_ON\n"
3678− "#define __BUILTINBC(ret,name,params) ret __builtin_##name params __attribute__((alias(\"__bound_\"#name)));\n"
3679− "#define __BOUND(ret,name,params) ret name params __attribute__((alias(\"__bound_\"#name)));\n"
3683+ "#define __BUILTINBC(ret,name,params) ret __builtin_##name params RENAME(\"__bound_\"#name);\n"
3684+ "#define __BOUND(ret,name,params) ret name params RENAME(\"__bound_\"#name);\n"
36803685 "#else\n"
3681− "#define __BUILTINBC(ret,name,params) ret __builtin_##name params __attribute__((alias(#name)));\n"
3686+ "#define __BUILTINBC(ret,name,params) ret __builtin_##name params RENAME(#name);\n"
36823687 "#define __BOUND(ret,name,params)\n"
36833688 "#endif\n"
36843689 "#define __BOTH(ret,name,params) __BUILTINBC(ret,name,params)__BOUND(ret,name,params)\n"
3685− "#define __BUILTIN(ret,name,params) ret __builtin_##name params __attribute__((alias(\"\"#name)));\n"
3690+ "#define __BUILTIN(ret,name,params) ret __builtin_##name params RENAME(#name);\n"
36863691 "__BOTH(void*,memcpy,(void*,const void*,__SIZE_TYPE__))\n"
36873692 "__BOTH(void*,memmove,(void*,const void*,__SIZE_TYPE__))\n"
36883693 "__BOTH(void*,memset,(void*,int,__SIZE_TYPE__))\n"
tests/tests2/120+_alias.c+15−0
@@ -0,0 +1,15 @@
1+extern int printf (const char *, ...);
2+extern void target(void);
3+extern void alias_for_target(void);
4+extern void asm_for_target(void);
5+
6+void inunit2(void);
7+
8+void inunit2(void)
9+{
10+ target();
11+ alias_for_target();
12+ /* This symbol is not supposed to be available in this unit:
13+ asm_for_target();
14+ */
15+}
tests/tests2/120_alias.c+25−0
@@ -0,0 +1,25 @@
1+/* Check semantics of various constructs to generate renamed symbols. */
2+extern int printf (const char *, ...);
3+void target(void);
4+void target(void) {
5+ printf("in target function\n");
6+}
7+
8+void alias_for_target(void) __attribute__((alias("target")));
9+void asm_for_target(void) __asm__("target");
10+
11+/* This is not supposed to compile, alias targets must be defined in the
12+ same unit. In TCC they even must be defined before the reference
13+void alias_for_undef(void) __attribute__((alias("undefined")));
14+*/
15+
16+extern void inunit2(void);
17+
18+int main(void)
19+{
20+ target();
21+ alias_for_target();
22+ asm_for_target();
23+ inunit2();
24+ return 0;
25+}
tests/tests2/120_alias.expect+5−0
@@ -0,0 +1,5 @@
1+in target function
2+in target function
3+in target function
4+in target function
5+in target function
tests/tests2/Makefile+5−0
@@ -74,6 +74,11 @@ GEN-ALWAYS = · 73 unmodified lines
7474 104_inline.test : FLAGS += $(subst 104,104+,$1)
7575 104_inline.test : GEN = $(GEN-TCC)
7676
77+# this test needs two files, and we want to invoke the linker
78+120_alias.test : FLAGS += $(subst 120,120+,$1)
79+120_alias.test : GEN = $(GEN-TCC)
80+120_alias.test : NORUN = true
81+
7782 # this test needs pthread
7883 106_pthread.test: FLAGS += -pthread
7984 106_pthread.test: NORUN = true

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t