On 7/17/20 1:16 PM, 'Fangrui Song' via Generic System V Application Binary Interface wrote:
> Ali, the wording about SHN_BEFORE/SHN_AFTER on Linker and Libraries
> Guide is a bit unclear to me. Does Solaris ld support something similar
> to GNU ld's linker scripts?
Solaris uses a very different language than linker scripts.
We call them mapfiles. The original mapfile language was a
terrible thing that came with SVR4. I created a different,
I think better, one about 10 years ago, which I've blogged about.
http://www.linker-aliens.org/blogs/ali/entry/the_problem_s_with_solaris/
>
http://www.linker-aliens.org/blogs/ali/entry/a_new_mapfile_syntax_for/
http://www.linker-aliens.org/blogs/ali/entry/much_ado_about_nothing_stub/
http://www.linker-aliens.org/blogs/ali/entry/regex_and_glob_for_mapfiles/
The documentation for the current language is here:
https://docs.oracle.com/cd/E37838_01/html/E36783/man-m.html#scrolltoc
And the horrible original language:
https://docs.oracle.com/cd/E37838_01/html/E36783/chapter7-55900.html#scrolltoc
No, I don't expect you to read most of that. Probably the
blog marked with '>', or the current documentation are
really relevant here.
I believe that the GNU linker scripts are based on the similar
feature from Unix SVR3, which got dropped in SVR4 in favor of
the terrible language we inherited in Solaris.
-----
All you really need to know about SHN_BEFORE and SHN_AFTER is
that we regret them, and took steps to deprecate them a few
years ago. I will include the text of a comment I wrote then,
to help me remember the details, below.
The big problem is that they're not compatible with extended
section indexes. At the same time, the only users of them were
the Sun/Oracle Studio compilers, and we noted that when those
compilers got ported to Linux a few years ago, they were easily
able to do without them. So we support them for non-extended
objects as a backward compatibility measure, but they're
otherwise defunct.
Make sure you're looking at the Solaris 11.4 version of Linker and
Libraries, and not an older version.
https://docs.oracle.com/cd/E37838_01/html/E36783/man-s.html
We now say:
SHN_BEFORE, SHN_AFTER
Provide for initial and final section ordering in
conjunction with SHF_LINK_ORDER section flags. See
Figure 18, Table 18, ELF Section Attribute Flags.
SHN_BEFORE and SHN_AFTER are incompatible with objects
that use extended section indexes. They are considered
deprecated, and their use is discouraged. See Extended
Section Header.
To give you the full scoop on output section placement, the
Solaris ld divides the output section into 4 ranges, and then
sections are concatenated into the appropriate section in the
appropriate order:
/*
* Output sections contain lists of input sections that are assigned to them.
* These items fall into 4 categories:
* BEFORE - Ordered sections that specify SHN_BEFORE, in input order.
* ORDERED - Ordered sections that are sorted using unsorted sections
* as the sort key.
* DEFAULT - Sections that are placed into the output section
* in input order.
* AFTER - Ordered sections that specify SHN_AFTER, in input order.
*/
So today, all the action is in ORDERED and DEFAULT, which is why
I didn't mention BEFORE/AFTER in my earlier comments. I advise you
to pretend they don't exist.
If you want an example of baroque over engineering, you need not
look farther than the Sun SHF_ORDERED, and things like BEFORE/AFTER
that came with it. :-( SHF_LINK_ORDER was a welcome chance at
a do-over. See the notes below.
> If my previous .debug_info example can find its merit (if header/footer
> contribution can't be elegantly represented within the current
> SHF_LINK_ORDER framework), I guess we can still consider SHF_LLVM_ASSOCIATED
> (or SHF_GNU_ASSOCIATED if GNU people find it useful)
I think "elegantly" is arguable, but if you'd accept "dirt cheap",
then yes, I think that's fair.
I think I would implement ASSOCIATED rather than see things
fragment, but I'm voicing my opinion that it's a debatable
idea, and that we might be better off without it. It's probably
time for others to chime in at this point.
- Ali
--------------------------------------------
My notes about BEFORE/AFTER
/*
* Section Ordering History/Background:
*
* Historically, there have been two forms of section ordering in ELF,
* SHF_ORDERED, and SHF_LINK_ORDER. SHF_ORDERED is now EOL (End Of Life)
* and is no longer supported. However, the information below describing
* it is not found anywhere else, and is retained to provide historical
* detail and context for the parts that survive.
*
* SHF_ORDERED was invented at Sun in order to support the PowerPC port
* of Solaris 2.6, which used it for sorting tag words which describe
* the state of callee saves registers for given PC ranges. It was defined
* in the OS specific ELF section flag range. Some other values were defined
* at the same time:
* SHF_EXCLUDE - Section is to be excluded from executables or shared
* objects, and only kept in relocatable object output.
* SHN_BEFORE/SHN_AFTER - Sections are placed before/after all other
* sections, in the order they are encountered by the linker.
* Although initially conceived to support the PowerPC, the functionality
* was implemented for all platforms, and was later used to manage C++
* exceptions and stack unwinding. The PowerPC port never appeared, but
* SHF_ORDERED lived on, and was extended to the other platforms.
*
* SHF_LINK_ORDER was invented later by the wider ELF community, and is
* therefore assigned a value in the generic ELF section flag range. It is
* essentially a simpler version of SHF_ORDERED that dispenses with the section
* renaming feature. The Solaris implementation of SHF_LINK_ORDER uses
* SHF_EXCLUDE, and SHN_BEFORE/SHN_AFTER as well, but these Solaris-only
* extensions are not recognized by other implementations.
*
* Extended section indexes (shnum >= SHN_LORESERVE) were added, via the gABI,
* after SHN_BEFORE/SHN_AFTER. We participated in the gABI, but missed the
* fact that if the number of sections exceed SHN_LORESERVE, the meaning
* of the values assigned to SHN_BEFORE and SHN_AFTER become ambiguous,
* since you can't tell whether they are intended to refer to the special
* concept, or a section with that index. This issue was resolved with
*
* PSARC/2017/015 EOL SHF_ORDERED, Deprecate SHN_BEFORE/SHN_AFTER
*
* For backward compatibility with existing use from the C++ compilers for
* .exception_ranges sections, SHN_BEFORE/SHN_AFTER continue to be supported
* with SHF_LINK_ORDER, but only when (shnum <= SHN_LORESERVE). The compilers
* must switch away in order to support extended section indexes.
*
* -----
*
* SHF_ORDERED offered two distinct and separate abilities:
*
* (1) To specify the output section
* (2) To optionally be sorted relative to other sorted sections,
* using a non-sorted section as a sort key.
*
* To do this, it used both the sh_link, and sh_info fields:
*
* sh_link
* Specifies the output section to receive this input section.
* The sh_link field of an SHF_ORDERED section forms a linked list of
* sections, all of which must have identical section header flags
* (including SHF_ORDERED). The list is terminated by a final section
* with a sh_link that points at itself. All input sections in this list
* are assigned to the output section of the final section in the list.
* Hence, if a section points at itself, the effect is that it gets
* assigned to an output section in the usual default manner (i.e. an
* output section with the same name as the input). However, it can
* point at any arbitrary other section. This is a way to put a section
* with one name into an output section with a different name. It should
* be noted that this is of little value overall, because the link-editor
* now supports a more general feature for directing input sections
* to output sections: An input section named .text%foo will be sent to
* an output section named ".text", and this works for all sections,
* not just ordered ones.
*
* sh_info
* If sh_info is in the range (1 <= value < shnum), then this input section
* is added to the group of sorted sections. The section referenced by
* sh_info must be unsorted, and is used as the sort key.
*
* If sh_info is SHN_BEFORE or SHN_AFTER, it is put in the pre/post group,
* in the order it arrives (the before/after classes are not sorted).
*
* If sh_info is "invalid" (typically 0), then this section is added to
* the group of non-sorted sections, and goes into the output file in the
* order it arrives. This is not a valuable feature, as the same effect
* can be achieved more simply by not setting SHF_ORDERED at all.
*
* SHF_LINK_ORDER is a simplification of SHF_ORDERED. It uses sh_link to specify
* the section to use as a sort key and sh_info is set to 0. The standard
* ".text%foo" mechanism is used to direct input sections to output sections,
* and unordered sections indicate that by not setting SHF_LINK_ORDER.
*/