/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1988 AT&T
* All Rights Reserved
*
* Copyright (c) 2012, Joyent, Inc. All rights reserved.
*/
/*
* Map file parsing and input section to output segment mapping.
*/
#include <stdio.h>
#include <string.h>
#include <debug.h>
#include "msg.h"
#include "_libld.h"
/*
* Each time a section is placed, the function set_addralign()
* is called. This function performs:
*
* - if the section is from an external file, check if this is empty or not.
* If not, we know the segment this section will belong needs a program
* header. (Of course, the program is needed only if this section falls
* into a loadable segment.)
* - compute the Least Common Multiplier for setting the segment alignment.
*/
static void
{
/* A discarded section has no influence on the output */
return;
/*
* If this section has data or will be assigned data
* later, mark this segment not-empty.
*/
return;
}
/*
* Return the first input descriptor for a given output descriptor,
* or NULL if there are none.
*/
Is_desc *
{
int i;
for (i = 0; i < OS_ISD_NUM; i++) {
if (aplist_nitems(ap_isdesc) > 0)
}
return (NULL);
}
/*
* Attach an input section to an output section
*
* entry:
* ofl - File descriptor
* osp - Output section descriptor
* isp - Input section descriptor
* mapfile_sort - True (1) if segment supports mapfile specified ordering
* of otherwise unordered input sections, and False (0) otherwise.
*
* exit:
* - The input section has been attached to the output section
* - If the input section is a candidate for string table merging,
* then it is appended to the output section's list of merge
* candidates (os_mstridescs).
*
* On success, returns True (1). On failure, False (0).
*/
static int
{
/*
* If section ordering was specified for an unordered section
* via the mapfile, then search in the OS_ISD_DEFAULT list
* and insert it in the specified position. Ordered sections
* are placed in ascending order before unordered sections
* (sections with an is_ordndx value of zero).
*
* If no mapfile ordering was specified, we append it in
* the usual way below.
*/
continue;
if (aplist_insert(
return (0);
do_append = 0;
break;
}
}
} else { /* Ordered section (via shdr flags) */
/* SHF_ORDERED uses sh_info, SHF_LINK_ORDERED uses sh_link */
if (shndx == SHN_BEFORE) {
} else {
}
}
/*
* If we didn't insert a section into the default list using
* mapfile specified ordering above, then append the input
* section to the appropriate list.
*/
return (0);
/*
* A section can be merged if the following are true:
* - The SHF_MERGE|SHF_STRINGS flags must be set
* - String table compression must not be disabled (-znocompstrtab)
* - Mapfile ordering must not have been used.
* - The section must not be ordered via section header flags.
* - It must not be the generated section being built to
* replace the sections on this list.
*/
(SHF_MERGE | SHF_STRINGS)) ||
!do_append ||
return (1);
/*
* Skip sections with (sh_entsize > 1) or (sh_addralign > 1).
*
* sh_entsize:
* We are currently only able to merge string tables containing
* strings with 1-byte (char) characters. Support for wide
* characters will require our string table compression code
* to be extended to handle larger character sizes.
*
* sh_addralign:
* Alignments greater than 1 would require our string table
* compression code to insert null bytes to move each
* string to the required alignment.
*/
return (1);
}
return (0);
/*
* The SHF_MERGE|SHF_STRINGS flags tell us that the program that
* created the section intended it to be mergeable. The
* FLG_IS_INSTRMRG flag says that we have done validity testing
* and decided that it is safe to act on that hint.
*/
return (1);
}
/*
* Determine whether this input COMDAT section already exists for the associated
* output section. If so, then discard this input section. Otherwise, this
* must be the first COMDAT section, thus it is kept for future comparisons.
*/
static uintptr_t
{
/*
* Sections to which COMDAT groups apply are FLG_IS_COMDAT but are
* discarded separately by the group logic so should never be
* discarded here.
*/
return (1);
/*
* Create a COMDAT avl tree for this output section if required.
*/
return (S_ERROR);
}
/*
* A standard COMDAT section uses the section name as search key.
*/
/*
* If this section hasn't already been identified as discarded,
* generate a suitable diagnostic.
*/
}
/*
* A discarded section does not require assignment to an output
* section. However, if relaxed relocations have been enabled
* (either from -z relaxreloc, or asserted with .gnu.linkonce
* processing), then this section must still be assigned to an
* output section so that the sloppy relocation logic will have
* the information necessary to do its work.
*/
return (0);
}
/*
* This is a new COMDAT section - so keep it.
*/
return (S_ERROR);
return (1);
}
/*
* Determine whether a GNU group COMDAT section name follows the convention
*
* section-name.symbol-name
*
* Each section within the input file is compared to see if the full section
* name matches the beginning of the COMDAT section, with a following '.'.
* A pointer to the symbol name, starting with the '.' is returned so that the
* caller can strip off the required section name.
*/
static char *
{
continue;
/*
* It's questionable whether this size should be cached in the
* Is_desc. However, this seems an infrequent operation and
* adding Is_desc members can escalate memory usage for large
* link-edits. For now, size the section name dynamically.
*/
}
return (NULL);
}
/*
* GNU .gnu.linkonce sections follow a naming convention that indicates the
* required association with an output section. Determine whether this input
* section follows the convention, and if so return the appropriate output
* section name.
*
* .gnu.linkonce.b.* -> .bss
* .gnu.linkonce.d.* -> .data
* .gnu.linkonce.l.* -> .ldata
* .gnu.linkonce.lb.* -> .lbss
* .gnu.linkonce.lr.* -> .lrodata
* .gnu.linkonce.r.* -> .rodata
* .gnu.linkonce.s.* -> .sdata
* .gnu.linkonce.s2.* -> .sdata2
* .gnu.linkonce.sb.* -> .sbss
* .gnu.linkonce.sb2.* -> .sbss2
* .gnu.linkonce.t.* -> .text
* .gnu.linkonce.tb.* -> .tbss
* .gnu.linkonce.td.* -> .tdata
* .gnu.linkonce.wi.* -> .debug_info
*/
static const char *
{
switch (*nstr) {
case 'b':
if (NSTR_CH1('.'))
return (MSG_ORIG(MSG_SCN_BSS));
break;
case 'd':
if (NSTR_CH1('.'))
return (MSG_ORIG(MSG_SCN_DATA));
break;
case 'l':
if (NSTR_CH1('.'))
return (MSG_ORIG(MSG_SCN_LDATA));
return (MSG_ORIG(MSG_SCN_LBSS));
return (MSG_ORIG(MSG_SCN_LRODATA));
break;
case 'r':
if (NSTR_CH1('.'))
return (MSG_ORIG(MSG_SCN_RODATA));
break;
case 's':
if (NSTR_CH1('.'))
return (MSG_ORIG(MSG_SCN_SDATA));
return (MSG_ORIG(MSG_SCN_SDATA2));
return (MSG_ORIG(MSG_SCN_SBSS));
return (MSG_ORIG(MSG_SCN_SBSS2));
break;
case 't':
if (NSTR_CH1('.'))
return (MSG_ORIG(MSG_SCN_TEXT));
return (MSG_ORIG(MSG_SCN_TBSS));
return (MSG_ORIG(MSG_SCN_TDATA));
break;
case 'w':
return (MSG_ORIG(MSG_SCN_DEBUG_INFO));
break;
default:
break;
}
/*
* No special name match found.
*/
return (ostr);
}
/*
* The GNU link-editor maps sections generated by the GNU compiler separately
* due to -ffunction-sections, -fdata-sections or for other reasons into the
* "normal" section represented.
*
* Sections are named .<main>.<symbol> where <main> is the usual section to
* which it should be mapped, and <symbol> is providing the unique name for
* the original section. Both parts of the name may contain periods, in cases
* contributes to does (such as .data.rel.ro)
*
*
* As a further complication, the GNU link-editor may or may not merge
* .ctors.* and .dtors.* into init_array and fini_array, rather than ctors and
* dtors. We do not implement this at this time.
*
* The GNU link editor may also arrange for sections with .local in their name
* to be mapped as above, but grouped together. We do not implement this (and
* do not merge them at all, to make this clear)
*
* This table is processed in order. Longer mappings must come first.
*/
static struct split_sec_mapping {
char *leader;
char *section;
} split_sec_mapping[] = {
};
static const char *
{
}
}
return (ostr);
}
/*
* Initialize a path info buffer for use with ld_place_section().
*
* entry:
* ofl - Output descriptor
* ifl - Descriptor for input file, or NULL if there is none.
* info - Address of buffer to be initialized.
*
* exit:
* If this is an input file, and if the entrance criteria list
* contains at least one criteria that has a non-empty file string
* match list (ec_files), then the block pointed at by info is
* initialized, and info is returned.
*
* a non-empty ec_files list, then NULL is returned. This is not
* an error --- the NULL is simply an optimization, understood by
* ld_place_path(), that allows it to skip unnecessary work.
*/
{
/*
* Return NULL if there is no input file (internally generated section)
* or if the entrance criteria list does not contain any items that will
* need to be compared to the path (all the ec_files lists are empty).
*/
return (NULL);
/*
* The basename is the final segment of the path, equivalent to
* the path itself if there are no '/' delimiters.
*/
else
/*
* For an archive, the object name is the member name, which is
* enclosed in () at the end of the name string. Otherwise, it is
* the same as the basename.
*/
/* There must be an archive member suffix delimited by parens */
} else {
}
return (info);
}
/*
* Compare an input section path to the file comparison list the given
* entrance criteria.
*
* entry:
* path_info - A non-NULL Place_path_info block for the file
* containing the input section, initialized by
* ld_place_path_info_init()
* enp - Entrance criteria with a non-empty ec_files list of file
* comparisons to be carried out.
*
* exit:
* Return TRUE if a match is seen, and FALSE otherwise.
*/
static Boolean
{
const char *cmp_str;
/*
* Determine the starting character, and # of characters,
* from the file path to compare against this entrance criteria
* file string.
*/
if (type == TYP_ECF_OBJNAME) {
} else {
/*
* If the entrance criteria specifies an archive member
* and the file does not, then there can be no match.
*/
continue;
if (type == TYP_ECF_PATH) {
} else { /* TYP_ECF_BASENAME */
}
/*
* If the entrance criteria does not specify an archive
* member and the file does, then a match just requires
* the paths (without the archive member) to match.
* Reduce the length to not include the ar member or
* the '(' that precedes it.
*/
}
/*
* Compare the resulting string to the one from the
* entrance criteria.
*/
return (TRUE);
}
return (FALSE);
}
/*
* Replace the section header for the given input section with a new section
* header of the specified type. All values in the replacement header other
* than the type retain their previous values.
*
* entry:
* isp - Input section to replace
* sh_type - New section type to apply
*
* exit:
* Returns the pointer to the new section header on success, and
* NULL for failure.
*/
static Shdr *
{
return (NULL);
return (shdr);
}
/*
* Issue a fatal warning for the given .eh_frame section, which
* cannot be merged with the existing .eh_frame output section.
*/
static void
{
/*
* Locate the .eh_frame output section, and use the first section
* assigned to it in the error message. The user can then compare
* the two sections to determine what attribute prevented the merge.
*/
continue;
if (aplist_nitems(lst) == 0)
continue;
return;
}
}
}
}
/*
* Place a section into the appropriate segment and output section.
*
* entry:
* ofl - File descriptor
* isp - Input section descriptor of section to be placed.
* path_info - NULL, or pointer to Place_path_info buffer initialized
* by ld_place_path_info_init() for the file associated to isp,
* for use in processing entrance criteria with non-empty
* file matching string list (ec_files)
* ident - Section identifier, used to order sections relative to
* others within the output segment.
* alt_os_name - If non-NULL, the name of the output section to place
* isp into. If NULL, input sections go to an output section
* with the same name as the input section.
*/
Os_desc *
int ident, const char *alt_os_name)
{
int os_ndx;
/*
* Define any sections that must be thought of as referenced. These
* sections may not be referenced externally in a manner ld(1) can
* discover, but they must be retained (ie. not removed by -zignore).
*/
MSG_SCN_INIT, /* MSG_ORIG(MSG_SCN_INIT) */
MSG_SCN_FINI, /* MSG_ORIG(MSG_SCN_FINI) */
MSG_SCN_EX_RANGES, /* MSG_ORIG(MSG_SCN_EX_RANGES) */
MSG_SCN_EX_SHARED, /* MSG_ORIG(MSG_SCN_EX_SHARED) */
MSG_SCN_CTORS, /* MSG_ORIG(MSG_SCN_CTORS) */
MSG_SCN_DTORS, /* MSG_ORIG(MSG_SCN_DTORS) */
MSG_SCN_EHFRAME, /* MSG_ORIG(MSG_SCN_EHFRAME) */
MSG_SCN_EHFRAME_HDR, /* MSG_ORIG(MSG_SCN_EHFRAME_HDR) */
MSG_SCN_JCR, /* MSG_ORIG(MSG_SCN_JCR) */
MSG_SCN_INITARRAY, /* MSG_ORIG(MSG_SCN_INITARRAY) */
MSG_SCN_FINIARRAY, /* MSG_ORIG(MSG_SCN_FINIARRAY) */
MSG_SCN_PREINITARRAY, /* MSG_ORIG(MSG_SCN_PREINITARRAY) */
0
};
/*
* If this section identifies group members, or this section indicates
* that it is a member of a group, determine whether the section is
* still required.
*/
/*
* If this group has been replaced by another group,
* then this section needs to be discarded.
*/
/*
* Since we're discarding the section, we
* can skip assigning it to an output section.
* The exception is that if the user
* specifies -z relaxreloc, then
* we need to assign the output section so
* that the sloppy relocation logic will have
* the information necessary to do its work.
*/
return (NULL);
}
}
/*
* SHT_GROUP sections can only be included into relocatable
* objects.
*/
return (NULL);
}
}
}
/*
* Always assign SHF_TLS sections to the DATA segment (and then the
* PT_TLS embedded inside of there).
*/
/*
* Traverse the entrance criteria list searching for a segment that
* matches the input section we have. If an entrance criterion is set
* then there must be an exact match. If we complete the loop without
* finding a segment, then sgp will be NULL.
*/
/* Disabled segments are not available for assignment */
continue;
/*
* If an entrance criteria doesn't have any of its fields
* set, it will match any section it is tested against.
* We set the FLG_EC_CATCHALL flag on these, primarily because
* it helps readers of our debug output to understand what
* the criteria means --- otherwise the user would just see
* that every field is 0, but might not understand the
* significance of that.
*
* Given that we set this flag, we can use it here as an
* optimization to short circuit all of the tests in this
* loop. Note however, that if we did not do this, the end
* result would be the same --- the empty criteria will sail
* past the following tests and reach the end of the loop.
*/
break;
}
continue;
if (enp->ec_attrmask &&
/* LINTED */
continue;
if (enp->ec_is_name &&
continue;
continue;
/* All entrance criteria tests passed */
break;
}
/*
* The final entrance criteria record is a FLG_EC_CATCHALL that points
* at the final predefined segment "extra", and this final segment is
* tagged FLG_SG_NODISABLE. Therefore, the above loop must always find
* a segment.
*/
/*
* Transfer the input section sorting key from the entrance criteria
* to the input section. A non-zero value means that the section
* will be sorted on this key amoung the other sections that have a
* non-zero key. These sorted sections are collectively placed at the
* head of the output section.
*
* If the sort key is 0, the section is placed after the sorted
* sections in the order they are encountered.
*/
/* Remember that this entrance criteria has placed a section */
/*
* If our caller has supplied an alternative name for the output
* section, then we defer to their request. Otherwise, the default
* is to use the same name as that of the input section being placed.
*
* The COMDAT, SHT_GROUP and GNU name translations that follow have
* the potential to alter this initial name.
*/
/*
* Solaris section names may follow the convention:
*
* section-name%symbol-name
*
* This convention has been used to order the layout of sections within
* segments for objects built with the compilers -xF option. However,
* the final object should not contain individual section headers for
* all such input sections, instead the symbol name is stripped from the
* name to establish the final output section name.
*
* This convention has also been followed for COMDAT and sections
* identified though SHT_GROUP data.
*
* Strip out the % from the section name for:
* - Non-relocatable objects
* - Relocatable objects if input section sorting is
* in force for the segment in question.
*/
}
}
/*
* When building relocatable objects, we must not redirect COMDAT
* section names into their outputs, such that our output object may
* be successfully used as an input object also requiring COMDAT
* processing
*/
/*
* GNU section names may follow the convention:
*
* .gnu.linkonce.*
*
* The .gnu.linkonce is a section naming convention that indicates a
* COMDAT requirement. Determine whether this section follows the GNU
* pattern, and if so, determine whether this section should be
* discarded or retained. The comparison of is_name[1] with 'g'
* is an optimization to skip using strncmp() too much. This is safe,
* because we know the name is not NULL, and therefore must have
* at least one character plus a NULL termination.
*/
MSG_SCN_GNU_LINKONCE_SIZE) == 0)) {
oname));
}
}
/*
* Explicitly identify this section type as COMDAT. Also,
* enable relaxed relocation processing, as this is typically
* a requirement with .gnu.linkonce sections.
*/
}
/*
* GNU section names may also follow the convention:
*
* section-name.symbol-name
*
* This convention is used when defining SHT_GROUP sections of type
* COMDAT. Thus, any group processing will have discovered any group
* sections, and this identification can be triggered by a pattern
* match section names.
*/
}
/*
* Enable relaxed relocation processing, as this is
* typically a requirement with GNU COMDAT sections.
*/
}
}
/*
* GNU section names named section-name.symbol-name which are not
* members of COMDAT groups are merged according to the behaviour of
* the GNU link-editor.
*
* See the description of gnu_split_sec().
*/
}
/*
* Assign a hash value now that the output section name has been
* finalized.
*/
/*
* Determine if output section ordering is turned on. If so, return
* the appropriate ordering index for the section. This information
* is derived from the Sg_desc->sg_os_order list that was built
* up from the Mapfile.
*
* A value of 0 for os_ndx means that the section is not sorted
* (i.e. is not found in the sg_os_order). The items in sg_os_order
* are in the desired sort order, so adding 1 to their alist index
* gives a suitable index for sorting.
*/
os_ndx = 0;
break;
}
}
}
/*
* Mask of section header flags to ignore when matching sections. We
* are more strict with relocatable objects, ignoring only the order
* flags, and keeping sections apart if they differ otherwise. This
* follows the policy that sections in a relative object should only
* be merged if their flags are the same, and avoids destroying
* information prematurely. For final products however, we ignore all
* flags that do not prevent a merge.
*/
/*
* Traverse the input section list for the output section we have been
* assigned. If we find a matching section simply add this new section.
*/
iidx = 0;
/*
* An input section matches an output section if:
* - The ident values match
* - The names match
* - Not a GROUP section
* - Not a DTrace dof section
* - Section types match
* - Matching section flags, after screening out the
* shflagmask flags.
*
* Section types are considered to match if any one of
* the following are true:
* - The type codes are the same
* - Both are .eh_frame sections (regardless of type code)
* - The input section is COMDAT, and the output section
* is SHT_PROGBITS.
*/
if ((ident == osp->os_identndx) &&
((shflags & ~shflagmask) ==
/*
* Process any COMDAT section, keeping the first and
* discarding all others.
*/
/*
* Set alignment
*/
/*
* If this section is a non-empty TLS section indicate
* that a PT_TLS program header is required.
*/
/*
* Insert the input section descriptor on the proper
* output section descriptor list.
*
* If this segment requires input section ordering,
* honor any mapfile specified ordering for otherwise
* unordered sections by setting the mapfile_sort
* argument of os_attach_isp() to True.
*/
/*
* If this input section and file is associated to an
* artificially referenced output section, make sure
* they are marked as referenced also. This ensures
* that this input section and file isn't eliminated
* when -zignore is in effect.
*
* See -zignore comments when creating a new output
* section below.
*/
if (((ifl &&
if (ifl)
}
return (osp);
}
/*
* Do we need to worry about section ordering?
*/
if (os_ndx) {
/* insert section here. */
break;
else {
continue;
}
} else {
/* insert section here. */
break;
}
continue;
}
/*
* If the new sections identifier is less than that of the
* present input section we need to insert the new section
* at this point.
*/
if (ident < osp->os_identndx)
break;
}
/*
* We are adding a new output section. Update the section header
* count and associated string size.
*
* If the input section triggering this output section has been marked
* for discard, and if no other non-discarded input section comes along
* to join it, then we will over count. We cannot know if this will
* happen or not until all input is seen. Set FLG_OF_AJDOSCNT to
* trigger a final count readjustment.
*/
ofl->ofl_shdrcnt++;
/*
* Create a new output section descriptor.
*/
/*
* Convert COMDAT section to PROGBITS as this the first section of the
* output section. Save any COMDAT section for later processing, as
* additional COMDAT sections that match this section need discarding.
*/
if (is_ehframe) {
/*
* Executable or sharable objects can have at most a single
* .eh_frame section. Detect attempts to create more than
* one. This occurs if the input sections have incompatible
* attributes.
*/
}
/*
* For .eh_frame sections, we always set the type to be the
* type specified by the ABI. This allows .eh_frame sections
* of type SHT_PROGBITS to be correctly merged with .eh_frame
* sections of the ABI-defined type (e.g. SHT_AMD64_UNWIND),
* with the output being of the ABI-defined type.
*/
} else {
}
if (is_ehframe)
/*
* See the translate_link() in update.c.
*/
}
/*
* When -zignore is in effect, user supplied sections and files that are
* not referenced from other sections, are eliminated from the object
* being produced. Some sections, although unreferenced, are special,
* and must not be eliminated. Determine if this new output section is
* one of those special sections, and if so mark it artificially as
* referenced. Any input section and file associated to this output
* section is also be marked as referenced, and thus won't be eliminated
* from the final output.
*/
DBG_ENABLED) {
}
break;
}
}
}
/*
* Sections of type SHT_GROUP are added to the ofl->ofl_osgroups list,
* so that they can be updated as a group later.
*/
AL_CNT_OFL_OSGROUPS) == NULL))
/*
* If this section is a non-empty TLS section indicate that a PT_TLS
* program header is required.
*/
/*
* If a non-allocatable section is going to be put into a loadable
* segment then turn on the allocate bit for this section and warn the
* user that we have done so. This could only happen through the use
* of a mapfile.
*/
}
/*
* Retain this sections identifier for future comparisons when placing
* a section (after all sections have been processed this variable will
* be used to hold the sections symbol index as we don't need to retain
* the identifier any more).
*/
osp->os_identndx = ident;
/*
* Set alignment.
*/
/*
* Insert the new section at the offset given by iidx. If no position
* for it was identified above, this will be index 0, causing the new
* section to be prepended to the beginning of the section list.
* Otherwise, it is the index following the section that was identified.
*/
return (osp);
}