[llvm-bugs] [Bug 48106] New: Clang handles .ctors and .init_array priority differently from GCC

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Nov 6 19:57:56 PST 2020


https://bugs.llvm.org/show_bug.cgi?id=48106

            Bug ID: 48106
           Summary: Clang handles .ctors and .init_array priority
                    differently from GCC
           Product: clang
           Version: unspecified
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: -New Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: dlj at google.com
                CC: htmldeveloper at gmail.com, llvm-bugs at lists.llvm.org,
                    neeilans at live.com, richard-llvm at metafoo.co.uk

Under GCC and BFD ld, .ctors and .init_array with priorities are sorted to run
in a specific order.

Example 1: https://compiler-explorer.com/z/9xTh1e
Example 2: https://compiler-explorer.com/z/chTc5n
(nearly identical examples as bug 48105, except that Example 2 does compile
with GCC)

Per https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46770, I believe that order is
intended to be:

.ctors.* (class-type variables with 'init_priority(<value in [101, 65535]>)',
sorted lexicographically with suffix of (65535-init_priority))
.ctors
.init_array.* (the suffix is a number, and these are sorted numerically)
.init_array

(You can also check with your friendly local
/usr/lib/x86_64-linux-gnu/ldscripts/elf32_x86_64.x or similar.)

However, based on Example 1, when both .init_array and .ctors sections are
present, the actual behavior from GCC appears to be:

.ctors.*
.init_array.*
.init_array
.ctors

Notably, .ctors.00000 (the section name which would correspond to
init_priority(65535)) runs before .init_array.0.


Also in Example 1, Gold merges priority sections, so, for example, this
ordering:

'.init_array.101' and '.ctors.65434' (in some order)
'.init_array.65535' and '.ctors.00000' and '.ctors' (in some order)
'.init_array'
'.ctors'

seems valid. (Merging corresponding priorities of .ctors.* and .init_array.*
seems to satisfy much of the discussion on
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=46770.)



Based on Example 2, GCC's order with _only_ .ctors.* and .ctors is:

.ctors.{65535..65435} (which would correspond to init_priority(0..100), but
those are only allowed for use by the implementation)
.ctors.{65434..00001} (which correspond to init_priority(101..65534))
{.ctors.00000 .ctors} (which correspond to init_priority(65535) and no
init_priority, respectively)

In particular, I believe .ctors.00000 and .ctors are being treated as the same
equivalence class, because of this sequence:

.ctors.00000  (the section name corresponding to init_priority(65535))
X::X          (corresponds to .ctors)
X::X init_priority(65535)  (corresponds to .ctors.00000)
.ctors        (corresponds to no init_priority)



However, Clang produces results I can't quite explain. Again, using Example 2
above, Clang with BFD ld produce this output (slightly edited for clarity):

.ctors.65535
.init_array.0
.init_array.2
.ctors.65434
.init_array.101
X::X init_priority(101)
.ctors.00000
.init_array.65535
.ctors
.init_array
X::X init_priority(65535)
X::X


This seems odd, for two reasons:

1. Clang seems to be either merging or mis-attributing these sections, because
I would expect BFD ld to retain the same overall sequence as from GCC (i.e.,
all .ctors.* before .init_array.*, etc.).

2. Based on GCC in Example 1, I would expect this sequence (from BFD ld):
    .ctors.00000
    .init_array.65535
    .ctors
    .init_array
    X::X init_priority(65535)
    X::X
to be more like:
    <other .ctors.*>       <- added for completeness
    .ctors.00000
    X::X init_priority(65535)  <- moved
    <other .init_array.*>  <- added for completeness
    .init_array.65535
    .init_array
    .ctors                 <- moved
    X::X

and this sequence (from Gold):
    .ctors.00000
    .init_array.65535
    .init_array
    X::X init_priority(65535)
    X::X
    .ctors
to be more like:
    .ctors.00000
    X::X init_priority(65535)  <- moved
    .init_array.65535
    .init_array
    .ctors
    X::X

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20201107/d05f89b1/attachment-0001.html>


More information about the llvm-bugs mailing list