[PATCH] D45508: Implement --ctors-in-init-array.

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 16 10:55:06 PDT 2022


MaskRay added a comment.

In D45508#3588228 <https://reviews.llvm.org/D45508#3588228>, @haampie wrote:

> @MaskRay, not sure if I follow
>
>> This is not so accurate.
>
> Do you mean it's gcc not libc? In any case, when using lld, constructors in object files compiled with default cross GCC < 12 configuration are not run:
>
>   $ cat foo.cc 
>   #include <iostream>
>   class Foo {
>   public:
>       Foo() { std::cout << "foo\n"; }
>   };
>   Foo foo;
>   
>   $ cat main.cc 
>   #include <iostream>
>   int main() {
>       std::cout << "hello\n";
>   }
>   
>   $ gcc --version
>   x86_64-linux-gnu-gcc (GCC) 8.1.0
>   
>   $ gcc -c foo.cc
>   $ clang++ main.cc foo.o 
>   $ ./a.out 
>   foo
>   hello
>   
>   $ clang++ -fuse-ld=lld main.cc foo.o 
>   $ ./a.out 
>   hello
>
> Is that expected or not?
>
> Edit: ah, I think I get it. GCC provides `ctrbegin.o` and `crtend.o` with the sentinel values, but if GCC is compiled with `--enable-initfini-array`, those are omitted. So you simply can't use object files with ctors if your GCC is compiled with `--enable-initfini-array`. And in the example above, the initial GCC was compiled without `--enable-initfini-array`, and the clang later uses a different system GCC under the hood which was compiled with `--enable-initfini-array`.
>
> In any case, it would be much more convenient if lld had the same behavior as ld, cause this is a pain to debug.

Expected. And your analysis is about right. It would be convenient few years ago, but the value is significantly low nowadays, now with the GCC configure.ac fixed.

Note: even several years ago, the GCC default configure.ac behavior may not value much, but that is mainly a "GCC default has diverged a lot from the practice" issue.
I agree that we want to make system GCC usable with lld, but that doesn't mean a random manual build of GCC forgetting `--enable-initfini-array`.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D45508/new/

https://reviews.llvm.org/D45508



More information about the llvm-commits mailing list