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

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 15 11:23:08 PDT 2022


MaskRay added a comment.

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

> Would be nice to have this (enabled by default, like in `binutils ld`), since due to an oversight, GCC 12 is the first GCC to actually enable `.init_array` for cross-compilers targeting Linux. [1]
>
> That leads to annoying issues where building clang with such a GCC < 12 produces a `libclang_rt.profile.a` with `.ctors`, so that `clang -fprofile-generate -fuse-ld=lld` simply does not work: it ends up mixing `.ctors` from the runtime library and `.init_array` from the application, glibc doesn't execute the `.ctors` bits, but those contain the constructor of a global that initializes profiling. [2]
>
> This is not an issue with `ld`, since it generates ELF files with just `.init_array` by default. So `clang -fprofile-generate -fuse-ld=ld` works just fine.
>
> [1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100896
> [2] https://github.com/JuliaLang/julia/pull/45641

This patch may be more relevant few years ago but at this point, it mostly just adds come complexity which will need to be cleaned up soon.

One can run `objcopy --rename-section .ctors=.init_array --rename-section .dtors=.fini_array a.o` to fix object files using .ctors/.dtors.

Note that the upstream GCC configuration deviates considerably with what some distros use in practice.
Many specify `--enable-initfini-array` explicitly. 
(Deviate this topic a bit: if you'd like to help, there is a default-pie thing https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103398)

> glibc doesn't execute the `.ctors` bits.

This is not so accurate. See https://maskray.me/blog/2021-11-07-init-ctors-init-array#ctors-and-.dtors


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

https://reviews.llvm.org/D45508



More information about the llvm-commits mailing list