__used__ attributes in llvm-gcc's crtstuff.c

Hello,

I’m wondering why only some global static variables are marked with
used attributes in llvm-gcc/gcc/crtstuff.c.

GCC compiles crtstuff.c with -fno-toplevel-reorder option, which ensures that
unused static globals are not removed during optimization. However, since
LLVM does not support that option, I presume used attribute is used instead.

For example, CTOR_LIST[1] definition is changed as follows:

STATIC func_ptr CTOR_LIST[1]
/* LLVM LOCAL begin /
#ifdef llvm
attribute ((used, aligned(sizeof(func_ptr))))
#else
attribute ((unused, aligned(sizeof(func_ptr))))
#endif
/
LLVM LOCAL end */
= { (func_ptr) (-1) };

Note that unused just suppresses the warning that CTOR_LIST is not used.

My question is that why some global static variables, such as __frame_dummy_init_array_entry
and __do_global_dtors_aux_fini_array_entry, are not given used attributes.
It seems that llvm-gcc removes these variables and causes .init_array and .fini_array section
to be empty. Linker complains about this as in “ld: warning: .init_array section has zero size”.
Also, I verified that frame_dummy and __do_global_dtors_aux functions are not executed.

I have configured llvm-gcc for armv6 target and given --enable-initfini-array option.

I tried to give used attribute to every global static variable in crtstuff.c and
it caused no problem for me so far.

Would it be any problem in doing so?

Best regards,
Woongsik Choi

Hello,

__used__ attributes in llvm-gcc/gcc/crtstuff.c.
GCC compiles crtstuff.c with -fno-toplevel-reorder option, which ensures
that
unused static globals are not removed during optimization. However, since
LLVM does not support that option,

The problem is not that LLVM does not support this option. Though,
this is indeed so due to certain reason.
All the code is transformed into IR and transformations are performed
on it. It will be really bad for the
transformations to depend on some magic command line options. Even
more, consider e.g. you tried
to link two IR files - one with the code with -fno-toplevev-reorder
and one - without. What should be done
in such case?

I presume __used__ attribute is used instead.

This is correct, because this is what in fact the code should do here.

My question is that why some global static variables, such as
__frame_dummy_init_array_entry
and __do_global_dtors_aux_fini_array_entry, are not given __used__
attributes.

Just bug.

it caused no problem for me so far.
Would it be any problem in doing so?

Please prepare the patch and submit to llvm-commit. Thanks!

I have submitted the patch in the following llvm-commits article
http://lists.cs.uiuc.edu/pipermail/llvm-commits/Week-of-Mon-20101213/113762.html

Best regards,
Woongsik Choi

2010/12/15 Anton Korobeynikov <anton@korobeynikov.info>

My question is that why some global static variables, such as
__frame_dummy_init_array_entry
and __do_global_dtors_aux_fini_array_entry, are not given __used__
attributes.

...

I tried to give __used__ attribute to every global static variable in
crtstuff.c and
it caused no problem for me so far.

Note that upstream gcc has already marked more globals in crtstuff.c as
__used__:

http://gcc.gnu.org/viewcvs?view=revision&revision=159228
http://gcc.gnu.org/viewcvs?view=revision&revision=159279